UP | HOME

Squash JavaScript SQL Libarary

by Lang 2009-10-29 Thu

Squash is a JavaScript library for expressing SQL queries using syntax rather than string concatenation. The expression objects can be combined more easily than strings, and ensure that data is typed correctly.

Squash is loosely modeled on LINQ, but is a great deal more limited at the moment. I developed it because I'm working in classic ASP and wanted a layer of reasonable abstraction to distance my code from the eccentric database layout imposed by the ORM.

R1

by Lang 2010-01-22 Fri

Squash depends on sprintf (for formatting the date type). Try tests running in the browser, and see the squash code.

Plugins

The squash.fn namespace exports the internal statement object's prototype, and can be used to add statements to the squash language. This is roughly the same mechanism that jQuery uses; it's a nice way to keep the end queries concise and declarative.

Using the ADO database drivers, the RecordSet object's property MaxRecords limits the result set, and not all databases support an SQL variation of a limiting command. I like leaving limits in the statement, it simplifies library separation. So, I added a plugin:

squash.fn.limit = function (maxrecords) {
  var sql = this._clone();
  sql.env.maxrecords = maxrecords || dds.maxrecords;
  return sql;
};

The limit command marks the statement with a maxrecords property, and the database-specific driver's execute method reads the limit back out.

Drivers

A squash driver is an interface supporting the methods field, type, and wherein. Field and type are called with the table and field name. Field returns a mapped field name and type returns a type object, an interface with the methods tosql and toval. Wherein handles the bulk identifier lookup issue, which varied a lot in my small sample of database backends.

A more complete driver interface would require the driver to act as an interpreter so that it would be possible to build a backend that did not map to SQL.

It's up to some other program to actually call the driver and squash object in order to execute a database lookup.

R2

by Lang 2010-03-11 Thu

A rewrite focusing on simplification. The code. Tests.

Each statement is isomorphic with a closure, implemented as an object in order to allow inspection and manipulation of arguments at interpretation rather than at binding. Something like shift and reset might be appropriate, if I can figure it out. This is motivated almost entirely by SQL statement limits.

Unclosure

A late-bound closure. The closure contains a proc, args, and k; and implements the the call method which execute the stack of calls using the apply procedure provided by an env object.

The unclosure object prototype contains the public method interface, also bound to squash.fn.

Env

Implements apply and receive, the final continuation. apply can be used to trim recursion or selectively execute the unclosed calls in the statement.

Squash

The public constructor returns a correctly formatted object.

Date: 2010-06-11 11:41:09

HTML generated by org-mode 6.33x in emacs 23