index.js
[ hits: 11, misses: 0, sloc: 11, coverage: 100.00% ] [+]
- (function() {
- var query, queryType, _i, _len, _ref;
- exports.dialects = require('./dialects');
- exports.DEFAULT = require('./dialects/common').DEFAULT;
- String.prototype.ucfirst = function() {
- return this[0].toUpperCase() + this.slice(1);
- };
- _ref = ['select', 'insert'];
- for (_i = 0, _len = _ref.length; _i < _len; _i++) {
- queryType = _ref[_i];
- query = require("./" + queryType);
- exports[queryType] = exports[queryType.toUpperCase()] = exports[queryType.ucfirst()] = query;
- }
- }).call(this);
dialects/index.js
[ hits: 2, misses: 0, sloc: 2, coverage: 100.00% ] [+]
- (function() {
- module.exports = {
- mysql: require('./mysql'),
- "default": require('./mysql'),
- JOIN_TYPES: require('./common').JOIN_TYPES
- };
- }).call(this);
dialects/mysql.js
[ hits: 103, misses: 13, sloc: 116, coverage: 88.79% ] [+]
- (function() {
- var DEFAULT, DEFAULT_JOIN, JOIN_TYPES, PLACEHOLDER, common, fields, from, group, limit, order, renderBoundParam, renderClause, renderInsertParams, where, _ref;
- var __indexOf = Array.prototype.indexOf || function(item) {
- for (var i = 0, l = this.length; i < l; i++) {
- if (this[i] === item) return i;
- }
- return -1;
- };
- common = require('./common');
- _ref = ['JOIN_TYPES', 'DEFAULT'].map(function(k) {
- return common[k];
- }), JOIN_TYPES = _ref[0], DEFAULT = _ref[1];
- PLACEHOLDER = '?';
- DEFAULT_JOIN = JOIN_TYPES.INNER;
- exports.renderSelect = function(qs) {
- return "SELECT " + [fields, from, where, group, order, limit].map(function(f) {
- return f(qs);
- }).join('');
- };
- fields = function(qs) {
- var fs, tbl, tbl_fields, _ref2;
- fs = [];
- _ref2 = qs.fields;
- for (tbl in _ref2) {
- tbl_fields = _ref2[tbl];
- if (tbl_fields == null) {
- continue;
- }
- if (tbl_fields.length) {
- tbl_fields.forEach(function(f) {
- return fs.push("" + tbl + "." + f);
- });
- } else {
- fs.push("" + tbl + ".*");
- }
- }
- return fs.join(', ');
- };
- from = exports.from = function(qs) {
- var alias, clause, i, ret, table, tables, type;
- i = 0;
- tables = (function() {
- var _i, _len, _ref2, _ref3, _results;
- _ref2 = qs.tableStack;
- _results = [];
- for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
- _ref3 = _ref2[_i], table = _ref3[0], alias = _ref3[1], type = _ref3[2], clause = _ref3[3];
- if (type === 'NOP') {
- continue;
- }
- ret = i++ ? "" + (type.toUpperCase()) + " JOIN " + table : table;
- if (table !== alias) {
- ret += " AS " + alias;
- }
- if (clause != null) {
- ret += " ON " + (renderClause(clause, function(v) {
- return v;
- }));
- }
- _results.push(ret);
- }
- return _results;
- })();
- return ' FROM ' + tables.join(' ');
- };
- where = exports.where = function(qs) {
- if (qs.where.length) {
- return " WHERE " + (renderClause(qs.where));
- } else {
- return "";
- }
- };
- group = exports.group = function(qs) {
- if (qs.groupings.length) {
- return " GROUP BY " + (qs.groupings.map(function(g) {
- return g.table + '.' + g.field;
- }).join(', '));
- } else {
- return "";
- }
- };
- order = exports.order = function(qs) {
- if (qs.order.length) {
- return " ORDER BY " + (qs.order.map(function(o) {
- return o.table + '.' + o.field + (o.direction ? ' ' + o.direction : '');
- }).join(', '));
- } else {
- return "";
- }
- };
- limit = function(qs) {
- if (qs.limit != null) {
- return " LIMIT " + qs.limit;
- } else {
- return "";
- }
- };
- renderBoundParam = function(v) {
- if (v && v.constructor === Array) {
- return "(" + (v.map(function() {
- return PLACEHOLDER;
- }).join(', ')) + ")";
- } else {
- return PLACEHOLDER;
- }
- };
- exports.renderClause = renderClause = function(input, renderValue) {
- var render;
- if (renderValue == null) {
- renderValue = renderBoundParam;
- }
- render = function(clause) {
- if ((clause != null) && clause.constructor === Array) {
- return "" + (clause.map(render).join(' AND '));
- } else if (typeof clause === 'object') {
- if (clause.op === 'multi') {
- return "(" + (clause.clauses.map(render).join(clause.glue)) + ")";
- } else {
- return "" + clause.table + "." + clause.field + " " + clause.op + " " + (renderValue(clause.value));
- }
- } else {
- throw new Error("Unexpected clause type, this is probably a bug");
- }
- };
- return render(input);
- };
- exports.joinOp = exports.whereOp = function(op) {
- switch (op.toLowerCase()) {
- case 'ne':
- case '!=':
- case '<>':
- return '!=';
- case 'eq':
- case '=':
- return '=';
- case 'lt':
- case '<':
- return '<';
- case 'gt':
- case '>':
- return '>';
- case 'lte':
- case '<=':
- return '<=';
- case 'gte':
- case '>=':
- return '>=';
- case 'in':
- return 'IN';
- default:
- throw new Error("Unsupported comparison operator: " + op);
- }
- };
- exports.joinType = function(type) {
- if (!type) {
- return 'INNER';
- }
- type = type.toUpperCase();
- if (__indexOf.call(JOIN_TYPES, type) >= 0) {
- return type;
- } else {
- throw new Error("Unsupported JOIN type " + type);
- }
- };
- exports.renderInsert = function(qs) {
- return "INSERT INTO " + qs.table + " (" + (qs.fields.join(', ')) + ") VALUES " + (renderInsertParams(qs));
- };
- renderInsertParams = function(qs) {
- var rows;
- rows = (function() {
- var _i, _j, _ref2, _ref3, _results, _results2;
- _results = [];
- for (_i = 1, _ref2 = qs.parameters.length / qs.fields.length; 1 <= _ref2 ? _i <= _ref2 : _i >= _ref2; 1 <= _ref2 ? _i++ : _i--) {
- _results.push(renderBoundParam((function() {
- _results2 = [];
- for (var _j = 1, _ref3 = qs.fields.length; 1 <= _ref3 ? _j <= _ref3 : _j >= _ref3; 1 <= _ref3 ? _j++ : _j--){ _results2.push(_j); }
- return _results2;
- }).apply(this)));
- }
- return _results;
- })();
- return rows.join(", ");
- };
- exports.renderInsertSelect = function(qs) {
- return "INSERT INTO " + table + " " + (qs.fromQuery.toSql());
- };
- }).call(this);
dialects/common.js
[ hits: 3, misses: 0, sloc: 3, coverage: 100.00% ] [+]
- (function() {
- exports.JOIN_TYPES = ['INNER', 'LEFT INNER', 'RIGHT INNER', 'OUTER', 'LEFT OUTER', 'RIGHT OUTER', 'FULL OUTER', 'NATURAL'];
- exports.DEFAULT = "I'll be a UUID one day";
- }).call(this);
select.js
[ hits: 51, misses: 3, sloc: 54, coverage: 94.44% ] [+]
- (function() {
- var SUDQuery, Select, fluid, normalize;
- var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
- for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
- function ctor() { this.constructor = child; }
- ctor.prototype = parent.prototype;
- child.prototype = new ctor;
- child.__super__ = parent.prototype;
- return child;
- }, __slice = Array.prototype.slice;
- fluid = require('./fluid');
- SUDQuery = require('./sud-query');
- normalize = require('./normalize');
- module.exports = Select = (function() {
- __extends(Select, SUDQuery);
- function Select(table, opts) {
- if (opts == null) {
- opts = {};
- }
- Select.__super__.constructor.call(this, table, opts);
- this.s.queryType = 'Select';
- this.s.order = [];
- this.s.groupings = [];
- }
- Select.prototype.orderBy = fluid(function() {
- var args, orderings, _ref;
- args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
- orderings = normalize.orderings(args, this.lastTable(), this.dialect.order);
- return (_ref = this.s.order).push.apply(_ref, orderings);
- });
- Select.prototype.groupBy = function() {
- var field, fields, groupings, _ref;
- fields = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
- groupings = (function() {
- var _i, _len, _results;
- _results = [];
- for (_i = 0, _len = fields.length; _i < _len; _i++) {
- field = fields[_i];
- _results.push(normalize.fieldAndTable({
- table: this.lastTable(),
- field: field
- }));
- }
- return _results;
- }).call(this);
- return (_ref = this.s.groupings).push.apply(_ref, groupings);
- };
- Select.prototype.limit = fluid(function(l) {
- return this.s.limit = l;
- });
- return Select;
- })();
- Select.from = function(tbl, fields, opts) {
- var select, _ref;
- if (tbl.constructor === Select) {
- throw new Error("Inner queries not supported yet");
- }
- if ((fields != null) && ((_ref = fields.constructor) !== String && _ref !== Array)) {
- opts = fields;
- fields = null;
- }
- select = new Select(tbl, opts);
- if (fields != null) {
- switch (fields.constructor) {
- case String:
- select.fields(fields);
- break;
- case Array:
- select.fields.apply(select, fields);
- }
- }
- return select;
- };
- }).call(this);
fluid.js
[ hits: 4, misses: 0, sloc: 4, coverage: 100.00% ] [+]
- module.exports = function (fn) {
- return function() {
- fn.apply(this, arguments)
- return this
- }
- }
sud-query.js
[ hits: 107, misses: 7, sloc: 114, coverage: 93.86% ] [+]
- (function() {
- var Query, SUDQuery, fluid, normalize, unknown;
- var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
- for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
- function ctor() { this.constructor = child; }
- ctor.prototype = parent.prototype;
- child.prototype = new ctor;
- child.__super__ = parent.prototype;
- return child;
- }, __slice = Array.prototype.slice;
- fluid = require('./fluid');
- Query = require('./base-query');
- normalize = require('./normalize');
- module.exports = SUDQuery = (function() {
- __extends(SUDQuery, Query);
- function SUDQuery(table, opts) {
- var alias, _ref;
- SUDQuery.__super__.constructor.call(this, opts);
- this.s.fields = {};
- this.s.includedAliases = {};
- this.s.tableStack = [];
- this.s.where = [];
- this.s.parameters = [];
- if (table != null) {
- _ref = this.aliasPair(table), table = _ref[0], alias = _ref[1];
- this.s.fields[alias] = [];
- this.pushTable(table, alias);
- }
- }
- SUDQuery.prototype.fields = fluid(function() {
- var al, alias, f, field, fields, n, table, _base, _i, _len, _name, _ref, _ref2, _results;
- fields = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
- alias = !(fields[1] && fields[1].constructor === Array) ? this.lastTable() : ((_ref = this.aliasPair(fields.shift()), table = _ref[0], al = _ref[1], _ref), !this.includesAlias(al) ? unknown('table', table) : void 0);
- if (fields.length === 0) {
- return this.s.fields[alias] = null;
- }
- _results = [];
- for (_i = 0, _len = fields.length; _i < _len; _i++) {
- f = fields[_i];
- n = normalize.fieldAndTable({
- table: alias,
- field: f
- });
- field = this.resolve.field(n.table, n.field);
- if ((_ref2 = (_base = this.s.fields)[_name = n.table]) == null) {
- _base[_name] = [];
- }
- _results.push(this.s.fields[n.table].push(field));
- }
- return _results;
- });
- SUDQuery.prototype.field = SUDQuery.fields;
- SUDQuery.prototype.join = fluid(function(tbl, opts) {
- var alias, clause, table, type, _ref;
- if (opts == null) {
- opts = {};
- }
- _ref = this.aliasPair(tbl), table = _ref[0], alias = _ref[1];
- if (this.includesAlias(alias)) {
- throw new Error("Table alias is not unique: " + alias);
- }
- type = this.dialect.joinType(opts.type);
- if ((clause = opts.on) != null) {
- if (clause.constructor !== Array) {
- clause = [clause];
- }
- clause = normalize.clauses(clause, alias, this.dialect.joinOp);
- if (clause.length > 1) {
- clause = {
- op: 'multi',
- glue: ' AND ',
- clauses: clause
- };
- } else {
- clause = clause[0];
- }
- }
- this.pushTable(table, alias, type, clause);
- if (opts.fields != null) {
- return this.fields.apply(this, opts.fields);
- }
- });
- SUDQuery.prototype.from = fluid(function(alias) {
- var table;
- if (table = this.includesAlias(alias)) {
- return this.pushTable(table, alias, 'NOP');
- } else {
- return unknown('table', table);
- }
- });
- SUDQuery.prototype.where = fluid(function(alias, clause) {
- var normalized, table, _ref, _ref2;
- if (!(clause != null)) {
- clause = alias;
- alias = this.lastTable();
- } else {
- _ref = this.aliasPair(alias), table = _ref[0], alias = _ref[1];
- }
- if (this.includesAlias(alias) == null) {
- unknown('table', tbl);
- }
- normalized = normalize.clauses([clause], alias, this.dialect.whereOp);
- (_ref2 = this.s.where).push.apply(_ref2, normalized);
- return this.pushParams(normalized);
- });
- SUDQuery.prototype.or = fluid(function() {
- var args, clauses;
- args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
- clauses = normalize.clauses(args, this.lastTable(), this.dialect.whereOp);
- this.s.where.push({
- op: 'multi',
- glue: ' OR ',
- clauses: clauses
- });
- return this.pushParams(clauses);
- });
- SUDQuery.prototype.aliasPair = function(table) {
- var a, t;
- if ('object' === typeof table && Object.keys(table).length === 1) {
- return ((function() {
- var _results;
- _results = [];
- for (a in table) {
- t = table[a];
- _results.push([this.resolve.table(t), a]);
- }
- return _results;
- }).call(this))[0];
- } else {
- t = this.resolve.table(table);
- return [t, t];
- }
- };
- SUDQuery.prototype.pushTable = function(table, alias, type, clause) {
- if (table === "t1t1") {
- throw new Error("here");
- }
- if (type !== 'NOP') {
- this.s.includedAliases[alias] = table;
- }
- return this.s.tableStack.push([table, alias, type, clause]);
- };
- SUDQuery.prototype.pushParams = function(clauses) {
- var clause, _i, _len, _ref, _results;
- _results = [];
- for (_i = 0, _len = clauses.length; _i < _len; _i++) {
- clause = clauses[_i];
- _results.push(clause.op === 'multi' ? (sys.puts("pushParam recursing" + clause.clauses), this.pushParams(clause.clauses)) : clause.op === 'IN' ? (_ref = this.s.parameters).push.apply(_ref, clause.value) : this.s.parameters.push(clause.value));
- }
- return _results;
- };
- SUDQuery.prototype.lastTable = function() {
- return this.s.tableStack[this.s.tableStack.length - 1][1];
- };
- SUDQuery.prototype.includesAlias = function(a) {
- return this.s.includedAliases[a];
- };
- return SUDQuery;
- })();
- unknown = function(type, val) {
- throw new Error("Unknown " + type + ": " + val);
- };
- }).call(this);
base-query.js
[ hits: 44, misses: 4, sloc: 48, coverage: 91.67% ] [+]
- (function() {
- var Query, copy, dialects, fluid, passthrough_resolver;
- var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
- fluid = require('./fluid');
- dialects = require('./dialects');
- module.exports = Query = (function() {
- function Query(opts) {
- this.dialect = opts.dialect || dialects["default"];
- this.resolve = opts.resolver || passthrough_resolver;
- this.s = {};
- }
- Query.prototype.clone = function() {
- var child;
- child = new this.constructor;
- child.dialect = this.dialect;
- child.resolve = this.resolve;
- child.s = copy(this.s);
- return child;
- };
- Query.prototype.visit = fluid(function(fn) {
- if (fn != null) {
- return fn.call(this, this);
- }
- });
- Query.prototype.toSql = function() {
- return this.dialect["render" + this.s.queryType](this.s);
- };
- Query.prototype.execute = function(conn, cb) {
- if (conn['acquire'] != null) {
- return conn.acquire(__bind(function(c) {
- return this.execute(c, cb);
- }, this));
- } else {
- return conn.query(this.toSql(), this.s.parameters, cb);
- }
- };
- Query.prototype.toString = function() {
- return "[Query \"" + (this.toSql().substring(0, 20)) + "\"]";
- };
- return Query;
- })();
- passthrough_resolver = {
- table: function(n) {
- return n;
- },
- field: function(t, n) {
- return n;
- }
- };
- copy = function(thing) {
- var c, k, obj, v;
- if (thing === null) {
- return null;
- } else if (thing.constructor === Array) {
- c = [];
- c.push.apply(c, thing);
- return c;
- } else if ('object' === typeof thing) {
- obj = {};
- for (k in thing) {
- v = thing[k];
- obj[k] = copy(v);
- }
- return obj;
- } else {
- return thing;
- }
- };
- }).call(this);
normalize.js
[ hits: 38, misses: 3, sloc: 41, coverage: 92.68% ] [+]
- (function() {
- var norm;
- module.exports = norm = {
- clauses: function(clauses, table, normalizeOp) {
- var clause, constraint, fld, normalized, op, val, _i, _len;
- normalized = [];
- for (_i = 0, _len = clauses.length; _i < _len; _i++) {
- clause = clauses[_i];
- for (fld in clause) {
- constraint = clause[fld];
- if ('object' === typeof constraint) {
- for (op in constraint) {
- val = constraint[op];
- op = normalizeOp(op);
- normalized.push(norm.fieldAndTable({
- field: fld,
- op: op,
- value: val,
- table: table
- }));
- }
- } else {
- normalized.push(norm.fieldAndTable({
- field: fld,
- op: '=',
- value: constraint,
- table: table
- }));
- }
- }
- }
- return normalized;
- },
- orderings: function(orderings, table) {
- var add, direction, field, normalized, ordering, _i, _len, _ref;
- normalized = [];
- add = function(field, direction) {
- direction = (function() {
- switch ((direction || '').toLowerCase()) {
- case 'asc':
- case 'ascending':
- return 'ASC';
- case 'desc':
- case 'descending':
- return 'DESC';
- case '':
- return '';
- default:
- throw new Error("Unsupported ordering direction " + direction);
- }
- })();
- return normalized.push(norm.fieldAndTable({
- field: field,
- table: table,
- direction: direction
- }));
- };
- for (_i = 0, _len = orderings.length; _i < _len; _i++) {
- ordering = orderings[_i];
- if ('string' === typeof ordering) {
- _ref = ordering.split(/\ +/), field = _ref[0], direction = _ref[1];
- add(field, direction);
- } else {
- for (field in ordering) {
- direction = ordering[field];
- add(field, direction);
- }
- }
- }
- return normalized;
- },
- fieldAndTable: function(tableField) {
- var field, table, _ref;
- _ref = tableField.field.split('.'), table = _ref[0], field = _ref[1];
- if (field != null) {
- tableField.table = table;
- tableField.field = field;
- }
- return tableField;
- }
- };
- }).call(this);
insert.js
[ hits: 64, misses: 9, sloc: 73, coverage: 87.67% ] [+]
- (function() {
- var DEFAULT, Insert, Query, fluid;
- var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
- for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
- function ctor() { this.constructor = child; }
- ctor.prototype = parent.prototype;
- child.prototype = new ctor;
- child.__super__ = parent.prototype;
- return child;
- }, __slice = Array.prototype.slice;
- fluid = require('./fluid');
- Query = require('./base-query');
- DEFAULT = require('./dialects/common').DEFAULT;
- module.exports = Insert = (function() {
- var reset;
- __extends(Insert, Query);
- reset = function(fn) {
- return function() {
- var args;
- args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
- this.s.fields = [];
- this.s.parameters = [];
- return fn.apply(null, args);
- };
- };
- function Insert(table, opts) {
- if (opts == null) {
- opts = {};
- }
- Insert.__super__.constructor.call(this, opts);
- this.s.queryType = 'Insert';
- this.s.table = this.resolve.table(table);
- this.s.fields = [];
- this.s.parameters = [];
- }
- Insert.prototype.fields = fluid(function() {
- var fields;
- fields = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
- return this.s.fields = fields;
- });
- Insert.prototype.addRows = fluid(function() {
- var row, rows, _i, _len, _results;
- rows = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
- _results = [];
- for (_i = 0, _len = rows.length; _i < _len; _i++) {
- row = rows[_i];
- _results.push(row.constructor === Array ? this.addRowArray(row) : this.addRowObject(row));
- }
- return _results;
- });
- Insert.prototype.addRow = function(row) {
- return this.addRows(row);
- };
- Insert.prototype.addRowArray = function(row) {
- var count, _ref;
- if (this.s.fields != null) {
- count = this.s.fields.length;
- }
- if (!count) {
- throw new Error("Cannot insert from array without first setting fields");
- }
- if (row.length !== count) {
- throw new Error("Wrong number of values in array, expected " + this.s.fields);
- }
- return (_ref = this.s.parameters).push.apply(_ref, row);
- };
- Insert.prototype.checkFields = function(cb) {};
- Insert.prototype.addRowObject = function(row) {
- var array, f, _ref;
- if (!this.s.fields || this.s.fields.length === 0) {
- this.s.fields = Object.keys(row);
- }
- array = (function() {
- var _i, _len, _ref, _results;
- _ref = this.s.fields;
- _results = [];
- for (_i = 0, _len = _ref.length; _i < _len; _i++) {
- f = _ref[_i];
- _results.push((row[f] != null) || row[f] === null ? row[f] : DEFAULT);
- }
- return _results;
- }).call(this);
- return (_ref = this.s.parameters).push.apply(_ref, array);
- };
- Insert.prototype.fromQuery = fluid(function(query) {
- this.s.queryType = 'InsertSelect';
- return this.s.fromQuery = query;
- });
- return Insert;
- })();
- Insert.into = function(tbl, opts) {
- return new Insert(tbl, opts);
- };
- }).call(this);