New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

squel

Package Overview
Dependencies
Maintainers
1
Versions
78
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

squel - npm Package Compare versions

Comparing version 1.0.7 to 1.1.0

test/blocks.test.coffee

5

CHANGELOG.md
# Changelog for [squel](https://github.com/hiddentao/squel)
## 27 Mar 2013 (1.1)
* Squel can now be customized to include proprietary commands and queries.
* AMD support added.
## 4 Jan 2013 (1.0.6)

@@ -5,0 +10,0 @@ * Squel can now be told to auto-quote table and field names.

2

package.json
{
"name": "squel",
"description": "SQL query string builder",
"version": "1.0.7",
"version": "1.1.0",
"author": "Ramesh Nair <ram@hiddentao.com> (http://www.hiddentao.com/)",

@@ -6,0 +6,0 @@ "contributors": [

@@ -5,3 +5,3 @@ # squel - SQL query string builder for Javascript

A simple, well tested SQL query string builder for Javascript.
A flexible and powerful SQL query string builder for Javascript.

@@ -11,6 +11,7 @@ ## Features

* Works in node.js and in the browser.
* Supports the construction of all standard SQL queries: SELECT, UPDATE, INSERT and DELETE.
* Supports the standard SQL queries: SELECT, UPDATE, INSERT and DELETE.
* Can be customized to support non-standard queries.
* Uses method chaining for ease of use.
* Well tested (~200 tests).
* Small: ~3 KB when minified and gzipped.
* Well tested (~240 tests).
* Small: ~3.6 KB minified and gzipped

@@ -147,3 +148,71 @@ ## Installation

**Custom queries**
Squel allows you to override the built-in query builders with your own as well as create your own types of queries:
// ------------------------------------------------------
// Setup the PRAGMA query builder
// ------------------------------------------------------
var util = require('util'); // to use util.inherits() from node.js
var CommandBlock = function() {};
util.inherits(CommandBlock, squel.cls.Block);
// private method - will not get exposed within the query builder
CommandBlock.prototype._command = function(_command) {
this._command = _command;
}
// public method - will get exposed within the query builder
CommandBlock.prototype.compress = function() {
this._command('compress');
};
CommandBlock.prototype.buildStr = function() {
return this._command.toUpperCase();
};
// generic parameter block
var ParamBlock = function() {};
util.inherits(ParamBlock, squel.cls.Block);
ParamBlock.prototype.param = function(p) {
this._p = p;
};
ParamBlock.prototype.buildStr = function() {
return this._p;
};
// pragma query builder
var PragmaQuery = function(options) {
squel.cls.QueryBuilder.call(this, options, [
new squel.cls.StringBlock(options, 'PRAGMA'),
new CommandBlock(),
new ParamBlock()
]);
};
util.inherits(PragmaQuery, squel.cls.QueryBuilder);
// convenience method (we can override built-in squel methods this way too)
squel.pragma = function(options) {
return new PragmaQuery(options)
};
// ------------------------------------------------------
// Build a PRAGMA query
// ------------------------------------------------------
squel.pragma()
.compress()
.param('test')
.toString();
// 'PRAGMA COMPRESS test'
## Documentation

@@ -150,0 +219,0 @@

@@ -30,8 +30,9 @@ // Generated by CoffeeScript 1.3.3

(function() {
var Cloneable, DefaultQueryBuilderOptions, Delete, Expression, Insert, JoinWhereOrderLimit, QueryBuilder, Select, Update, WhereOrderLimit, _export, _extend,
var cls, squel, _extend,
__slice = [].slice,
__hasProp = {}.hasOwnProperty,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
__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; };
cls = {};
_extend = function() {

@@ -55,4 +56,11 @@ var dst, k, sources, src, v, _i, _len;

Cloneable = (function() {
cls.DefaultQueryBuilderOptions = {
autoQuoteTableNames: false,
autoQuoteFieldNames: false,
nameQuoteCharacter: '`',
usingValuePlaceholders: false
};
cls.Cloneable = (function() {
function Cloneable() {}

@@ -70,22 +78,104 @@

Expression = (function() {
var _toString;
cls.BaseBuilder = (function(_super) {
Expression.prototype.tree = null;
__extends(BaseBuilder, _super);
Expression.prototype.current = null;
function BaseBuilder(options) {
this.options = _extend({}, cls.DefaultQueryBuilderOptions, options);
}
function Expression() {
this.toString = __bind(this.toString, this);
BaseBuilder.prototype._getObjectClassName = function(obj) {
var arr;
if (obj && obj.constructor && obj.constructor.toString) {
arr = obj.constructor.toString().match(/function\s*(\w+)/);
if (arr && arr.length === 2) {
return arr[1];
}
}
return void 0;
};
this.or = __bind(this.or, this);
BaseBuilder.prototype._sanitizeCondition = function(condition) {
if (condition instanceof cls.Expression) {
condition = condition.toString();
}
if ("string" !== typeof condition) {
throw new Error("condition must be a string or Expression instance");
}
return condition;
};
this.and = __bind(this.and, this);
BaseBuilder.prototype._sanitizeName = function(value, type) {
if ("string" !== typeof value) {
throw new Error("" + type + " must be a string");
}
return value;
};
this.end = __bind(this.end, this);
BaseBuilder.prototype._sanitizeField = function(item) {
var sanitized;
sanitized = this._sanitizeName(item, "field name");
if (this.options.autoQuoteFieldNames) {
return "" + this.options.nameQuoteCharacter + sanitized + this.options.nameQuoteCharacter;
} else {
return sanitized;
}
};
this.or_begin = __bind(this.or_begin, this);
BaseBuilder.prototype._sanitizeTable = function(item) {
var sanitized;
sanitized = this._sanitizeName(item, "table name");
if (this.options.autoQuoteTableNames) {
return "" + this.options.nameQuoteCharacter + sanitized + this.options.nameQuoteCharacter;
} else {
return sanitized;
}
};
this.and_begin = __bind(this.and_begin, this);
BaseBuilder.prototype._sanitizeAlias = function(item) {
return this._sanitizeName(item, "alias");
};
BaseBuilder.prototype._sanitizeLimitOffset = function(value) {
value = parseInt(value);
if (0 > value || isNaN(value)) {
throw new Error("limit/offset must be >=0");
}
return value;
};
BaseBuilder.prototype._sanitizeValue = function(item) {
var t;
t = typeof item;
if (null !== item && "string" !== t && "number" !== t && "boolean" !== t) {
throw new Error("field value must be a string, number, boolean or null");
}
return item;
};
BaseBuilder.prototype._formatValue = function(value) {
if (null === value) {
value = "NULL";
} else if ("boolean" === typeof value) {
value = value ? "TRUE" : "FALSE";
} else if ("number" !== typeof value) {
if (false === this.options.usingValuePlaceholders) {
value = "'" + value + "'";
}
}
return value;
};
return BaseBuilder;
})(cls.Cloneable);
cls.Expression = (function() {
var _toString;
Expression.prototype.tree = null;
Expression.prototype.current = null;
function Expression() {
var _this = this;

@@ -183,137 +273,441 @@ this.tree = {

DefaultQueryBuilderOptions = {
autoQuoteTableNames: false,
autoQuoteFieldNames: false,
nameQuoteCharacter: '`',
usingValuePlaceholders: false
};
cls.Block = (function(_super) {
QueryBuilder = (function(_super) {
__extends(Block, _super);
__extends(QueryBuilder, _super);
function Block() {
return Block.__super__.constructor.apply(this, arguments);
}
function QueryBuilder(options) {
this.options = _extend({}, DefaultQueryBuilderOptions, options);
Block.prototype.exposedMethods = function() {
var attr, ret, value;
ret = {};
for (attr in this) {
value = this[attr];
if (typeof value === "function" && attr.charAt(0) !== '_' && !cls.Block.prototype[attr]) {
ret[attr] = value;
}
}
return ret;
};
Block.prototype.buildStr = function(queryBuilder) {
return '';
};
return Block;
})(cls.BaseBuilder);
cls.StringBlock = (function(_super) {
__extends(StringBlock, _super);
function StringBlock(options, str) {
StringBlock.__super__.constructor.call(this, options);
this.str = str;
}
QueryBuilder.prototype._getObjectClassName = function(obj) {
var arr;
if (obj && obj.constructor && obj.constructor.toString) {
arr = obj.constructor.toString().match(/function\s*(\w+)/);
if (arr && arr.length === 2) {
return arr[1];
StringBlock.prototype.buildStr = function(queryBuilder) {
return this.str;
};
return StringBlock;
})(cls.Block);
cls.AbstractTableBlock = (function(_super) {
__extends(AbstractTableBlock, _super);
function AbstractTableBlock(options) {
AbstractTableBlock.__super__.constructor.call(this, options);
this.tables = [];
}
AbstractTableBlock.prototype._table = function(table, alias) {
if (alias == null) {
alias = null;
}
table = this._sanitizeTable(table);
if (alias) {
alias = this._sanitizeAlias(alias);
}
if (this.options.singleTable) {
this.tables = [];
}
return this.tables.push({
name: table,
alias: alias
});
};
AbstractTableBlock.prototype.buildStr = function(queryBuilder) {
var table, tables, _i, _len, _ref;
if (0 >= this.tables.length) {
throw new Error("table() needs to be called");
}
tables = "";
_ref = this.tables;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
table = _ref[_i];
if ("" !== tables) {
tables += ", ";
}
tables += table.name;
if (table.alias) {
tables += " AS `" + table.alias + "`";
}
}
return void 0;
return tables;
};
QueryBuilder.prototype._sanitizeCondition = function(condition) {
var c, t;
t = typeof condition;
c = this._getObjectClassName(condition);
if ('Expression' !== c && "string" !== t) {
throw new Error("condition must be a string or Expression instance");
return AbstractTableBlock;
})(cls.Block);
cls.UpdateTableBlock = (function(_super) {
__extends(UpdateTableBlock, _super);
function UpdateTableBlock() {
return UpdateTableBlock.__super__.constructor.apply(this, arguments);
}
UpdateTableBlock.prototype.table = function(table, alias) {
if (alias == null) {
alias = null;
}
if ('Expression' === t || 'Expression' === c) {
condition = condition.toString();
return this._table(table, alias);
};
return UpdateTableBlock;
})(cls.AbstractTableBlock);
cls.FromTableBlock = (function(_super) {
__extends(FromTableBlock, _super);
function FromTableBlock() {
return FromTableBlock.__super__.constructor.apply(this, arguments);
}
FromTableBlock.prototype.from = function(table, alias) {
if (alias == null) {
alias = null;
}
return condition;
return this._table(table, alias);
};
QueryBuilder.prototype._sanitizeName = function(value, type) {
if ("string" !== typeof value) {
throw new Error("" + type + " must be a string");
FromTableBlock.prototype.buildStr = function(queryBuilder) {
var table, tables, _i, _len, _ref;
if (0 >= this.tables.length) {
throw new Error("from() needs to be called");
}
return value;
tables = "";
_ref = this.tables;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
table = _ref[_i];
if ("" !== tables) {
tables += ", ";
}
tables += table.name;
if (table.alias) {
tables += " `" + table.alias + "`";
}
}
return "FROM " + tables;
};
QueryBuilder.prototype._sanitizeField = function(item) {
var sanitized;
sanitized = this._sanitizeName(item, "field name");
if (this.options.autoQuoteFieldNames) {
return "" + this.options.nameQuoteCharacter + sanitized + this.options.nameQuoteCharacter;
} else {
return sanitized;
return FromTableBlock;
})(cls.AbstractTableBlock);
cls.IntoTableBlock = (function(_super) {
__extends(IntoTableBlock, _super);
function IntoTableBlock(options) {
IntoTableBlock.__super__.constructor.call(this, options);
this.table = null;
}
IntoTableBlock.prototype.into = function(table) {
return this.table = this._sanitizeTable(table);
};
IntoTableBlock.prototype.buildStr = function(queryBuilder) {
if (!this.table) {
throw new Error("into() needs to be called");
}
return "INTO " + this.table;
};
QueryBuilder.prototype._sanitizeTable = function(item) {
var sanitized;
sanitized = this._sanitizeName(item, "table name");
if (this.options.autoQuoteTableNames) {
return "" + this.options.nameQuoteCharacter + sanitized + this.options.nameQuoteCharacter;
return IntoTableBlock;
})(cls.Block);
cls.GetFieldBlock = (function(_super) {
__extends(GetFieldBlock, _super);
function GetFieldBlock(options) {
GetFieldBlock.__super__.constructor.call(this, options);
this.fields = [];
}
GetFieldBlock.prototype.field = function(field, alias) {
if (alias == null) {
alias = null;
}
field = this._sanitizeField(field);
if (alias) {
alias = this._sanitizeAlias(alias);
}
return this.fields.push({
name: field,
alias: alias
});
};
GetFieldBlock.prototype.buildStr = function(queryBuilder) {
var field, fields, _i, _len, _ref;
fields = "";
_ref = this.fields;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
field = _ref[_i];
if ("" !== fields) {
fields += ", ";
}
fields += field.name;
if (field.alias) {
fields += " AS \"" + field.alias + "\"";
}
}
if ("" === fields) {
return "*";
} else {
return sanitized;
return fields;
}
};
QueryBuilder.prototype._sanitizeAlias = function(item) {
return this._sanitizeName(item, "alias");
return GetFieldBlock;
})(cls.Block);
cls.SetFieldBlock = (function(_super) {
__extends(SetFieldBlock, _super);
function SetFieldBlock(options) {
SetFieldBlock.__super__.constructor.call(this, options);
this.fields = {};
}
SetFieldBlock.prototype.set = function(field, value) {
field = this._sanitizeField(field);
value = this._sanitizeValue(value);
this.fields[field] = value;
return this;
};
QueryBuilder.prototype._sanitizeLimitOffset = function(value) {
value = parseInt(value);
if (0 > value || isNaN(value)) {
throw new Error("limit/offset must be >=0");
SetFieldBlock.prototype.buildStr = function(queryBuilder) {
var field, fieldNames, fields, _i, _len;
fieldNames = (function() {
var _ref, _results;
_ref = this.fields;
_results = [];
for (field in _ref) {
if (!__hasProp.call(_ref, field)) continue;
_results.push(field);
}
return _results;
}).call(this);
if (0 >= fieldNames.length) {
throw new Error("set() needs to be called");
}
return value;
fields = "";
for (_i = 0, _len = fieldNames.length; _i < _len; _i++) {
field = fieldNames[_i];
if ("" !== fields) {
fields += ", ";
}
fields += "" + field + " = " + (this._formatValue(this.fields[field]));
}
return "SET " + fields;
};
QueryBuilder.prototype._sanitizeValue = function(item) {
var t;
t = typeof item;
if (null !== item && "string" !== t && "number" !== t && "boolean" !== t) {
throw new Error("field value must be a string, number, boolean or null");
return SetFieldBlock;
})(cls.Block);
cls.InsertFieldValueBlock = (function(_super) {
__extends(InsertFieldValueBlock, _super);
function InsertFieldValueBlock(options) {
InsertFieldValueBlock.__super__.constructor.call(this, options);
this.fields = {};
}
InsertFieldValueBlock.prototype.buildStr = function(queryBuilder) {
var field, fieldNames, fields, name, values, _i, _len;
fieldNames = (function() {
var _ref, _results;
_ref = this.fields;
_results = [];
for (name in _ref) {
if (!__hasProp.call(_ref, name)) continue;
_results.push(name);
}
return _results;
}).call(this);
if (0 >= fieldNames.length) {
throw new Error("set() needs to be called");
}
return item;
fields = "";
values = "";
for (_i = 0, _len = fieldNames.length; _i < _len; _i++) {
field = fieldNames[_i];
if ("" !== fields) {
fields += ", ";
}
fields += field;
if ("" !== values) {
values += ", ";
}
values += this._formatValue(this.fields[field]);
}
return "(" + fields + ") VALUES (" + values + ")";
};
QueryBuilder.prototype._formatValue = function(value) {
if (null === value) {
value = "NULL";
} else if ("boolean" === typeof value) {
value = value ? "TRUE" : "FALSE";
} else if ("number" !== typeof value) {
if (false === this.options.usingValuePlaceholders) {
value = "'" + value + "'";
return InsertFieldValueBlock;
})(cls.SetFieldBlock);
cls.DistinctBlock = (function(_super) {
__extends(DistinctBlock, _super);
function DistinctBlock(options) {
DistinctBlock.__super__.constructor.call(this, options);
this.useDistinct = false;
}
DistinctBlock.prototype.distinct = function() {
return this.useDistinct = true;
};
DistinctBlock.prototype.buildStr = function(queryBuilder) {
if (this.useDistinct) {
return "DISTINCT";
} else {
return "";
}
};
return DistinctBlock;
})(cls.Block);
cls.GroupByBlock = (function(_super) {
__extends(GroupByBlock, _super);
function GroupByBlock(options) {
GroupByBlock.__super__.constructor.call(this, options);
this.groups = [];
}
GroupByBlock.prototype.group = function(field) {
field = this._sanitizeField(field);
return this.groups.push(field);
};
GroupByBlock.prototype.buildStr = function(queryBuilder) {
var f, groups, _i, _len, _ref;
groups = "";
if (0 < this.groups.length) {
_ref = this.groups;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
f = _ref[_i];
if ("" !== groups) {
groups += ", ";
}
groups += f;
}
groups = "GROUP BY " + groups;
}
return value;
return groups;
};
return QueryBuilder;
return GroupByBlock;
})(Cloneable);
})(cls.Block);
WhereOrderLimit = (function(_super) {
cls.OffsetBlock = (function(_super) {
__extends(WhereOrderLimit, _super);
__extends(OffsetBlock, _super);
function WhereOrderLimit(options) {
this._limitString = __bind(this._limitString, this);
function OffsetBlock(options) {
OffsetBlock.__super__.constructor.call(this, options);
this.offsets = null;
}
this._orderString = __bind(this._orderString, this);
OffsetBlock.prototype.offset = function(start) {
start = this._sanitizeLimitOffset(start);
return this.offsets = start;
};
this._whereString = __bind(this._whereString, this);
OffsetBlock.prototype.buildStr = function(queryBuilder) {
if (this.offsets) {
return "OFFSET " + this.offsets;
} else {
return "";
}
};
this.limit = __bind(this.limit, this);
return OffsetBlock;
this.order = __bind(this.order, this);
})(cls.Block);
this.where = __bind(this.where, this);
WhereOrderLimit.__super__.constructor.call(this, options);
cls.WhereBlock = (function(_super) {
__extends(WhereBlock, _super);
function WhereBlock(options) {
WhereBlock.__super__.constructor.call(this, options);
this.wheres = [];
this.orders = [];
this.limits = null;
}
WhereOrderLimit.prototype.where = function(condition) {
WhereBlock.prototype.where = function(condition) {
condition = this._sanitizeCondition(condition);
if ("" !== condition) {
this.wheres.push(condition);
return this.wheres.push(condition);
}
return this;
};
WhereOrderLimit.prototype.order = function(field, asc) {
WhereBlock.prototype.buildStr = function(queryBuilder) {
if (0 < this.wheres.length) {
return "WHERE (" + this.wheres.join(") AND (") + ")";
} else {
return "";
}
};
return WhereBlock;
})(cls.Block);
cls.OrderByBlock = (function(_super) {
__extends(OrderByBlock, _super);
function OrderByBlock(options) {
OrderByBlock.__super__.constructor.call(this, options);
this.orders = [];
}
OrderByBlock.prototype.order = function(field, asc) {
if (asc == null) {

@@ -323,24 +717,9 @@ asc = true;

field = this._sanitizeField(field);
this.orders.push({
return this.orders.push({
field: field,
dir: asc ? "ASC" : "DESC"
dir: asc ? true : false
});
return this;
};
WhereOrderLimit.prototype.limit = function(max) {
max = this._sanitizeLimitOffset(max);
this.limits = max;
return this;
};
WhereOrderLimit.prototype._whereString = function() {
if (0 < this.wheres.length) {
return " WHERE (" + this.wheres.join(") AND (") + ")";
} else {
return "";
}
};
WhereOrderLimit.prototype._orderString = function() {
OrderByBlock.prototype.buildStr = function(queryBuilder) {
var o, orders, _i, _len, _ref;

@@ -355,5 +734,5 @@ if (0 < this.orders.length) {

}
orders += "" + o.field + " " + o.dir;
orders += "" + o.field + " " + (o.dir ? 'ASC' : 'DESC');
}
return " ORDER BY " + orders;
return "ORDER BY " + orders;
} else {

@@ -364,5 +743,23 @@ return "";

WhereOrderLimit.prototype._limitString = function() {
return OrderByBlock;
})(cls.Block);
cls.LimitBlock = (function(_super) {
__extends(LimitBlock, _super);
function LimitBlock(options) {
LimitBlock.__super__.constructor.call(this, options);
this.limits = null;
}
LimitBlock.prototype.limit = function(max) {
max = this._sanitizeLimitOffset(max);
return this.limits = max;
};
LimitBlock.prototype.buildStr = function(queryBuilder) {
if (this.limits) {
return " LIMIT " + this.limits;
return "LIMIT " + this.limits;
} else {

@@ -373,25 +770,22 @@ return "";

return WhereOrderLimit;
return LimitBlock;
})(QueryBuilder);
})(cls.Block);
JoinWhereOrderLimit = (function(_super) {
cls.JoinBlock = (function(_super) {
__extends(JoinWhereOrderLimit, _super);
__extends(JoinBlock, _super);
function JoinWhereOrderLimit(options) {
this._joinString = __bind(this._joinString, this);
this.outer_join = __bind(this.outer_join, this);
this.right_join = __bind(this.right_join, this);
this.left_join = __bind(this.left_join, this);
this.join = __bind(this.join, this);
JoinWhereOrderLimit.__super__.constructor.call(this, options);
function JoinBlock(options) {
JoinBlock.__super__.constructor.call(this, options);
this.joins = [];
}
JoinWhereOrderLimit.prototype.join = function(table, alias, condition, type) {
JoinBlock.prototype.join = function(table, alias, condition, type) {
if (alias == null) {
alias = null;
}
if (condition == null) {
condition = null;
}
if (type == null) {

@@ -416,3 +810,3 @@ type = 'INNER';

JoinWhereOrderLimit.prototype.left_join = function(table, alias, condition) {
JoinBlock.prototype.left_join = function(table, alias, condition) {
if (alias == null) {

@@ -427,3 +821,3 @@ alias = null;

JoinWhereOrderLimit.prototype.right_join = function(table, alias, condition) {
JoinBlock.prototype.right_join = function(table, alias, condition) {
if (alias == null) {

@@ -438,3 +832,3 @@ alias = null;

JoinWhereOrderLimit.prototype.outer_join = function(table, alias, condition) {
JoinBlock.prototype.outer_join = function(table, alias, condition) {
if (alias == null) {

@@ -449,3 +843,3 @@ alias = null;

JoinWhereOrderLimit.prototype._joinString = function() {
JoinBlock.prototype.buildStr = function(queryBuilder) {
var j, joins, _i, _len, _ref;

@@ -456,3 +850,6 @@ joins = "";

j = _ref[_i];
joins += " " + j.type + " JOIN " + j.table;
if (joins !== "") {
joins += " ";
}
joins += "" + j.type + " JOIN " + j.table;
if (j.alias) {

@@ -468,375 +865,183 @@ joins += " `" + j.alias + "`";

return JoinWhereOrderLimit;
return JoinBlock;
})(WhereOrderLimit);
})(cls.Block);
Select = (function(_super) {
cls.QueryBuilder = (function(_super) {
__extends(Select, _super);
__extends(QueryBuilder, _super);
function Select(options) {
this.toString = __bind(this.toString, this);
this.offset = __bind(this.offset, this);
this.group = __bind(this.group, this);
this.field = __bind(this.field, this);
this.from = __bind(this.from, this);
this.distinct = __bind(this.distinct, this);
Select.__super__.constructor.call(this, options);
this.froms = [];
this.fields = [];
this.groups = [];
this.offsets = null;
this.useDistinct = false;
function QueryBuilder(options, blocks) {
var block, methodBody, methodName, _fn, _i, _len, _ref, _ref1,
_this = this;
QueryBuilder.__super__.constructor.call(this, options);
this.blocks = blocks || [];
_ref = this.blocks;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
block = _ref[_i];
_ref1 = block.exposedMethods();
_fn = function(block, name, body) {
return _this[name] = function() {
body.apply(block, arguments);
return _this;
};
};
for (methodName in _ref1) {
methodBody = _ref1[methodName];
if (this[methodName] != null) {
throw new Error("" + (this._getObjectClassName(this)) + " already has a builder method called: " + methodName);
}
_fn(block, methodName, methodBody);
}
}
}
Select.prototype.distinct = function() {
this.useDistinct = true;
return this;
};
Select.prototype.from = function(table, alias) {
if (alias == null) {
alias = null;
QueryBuilder.prototype.updateOptions = function(options) {
var block, _i, _len, _ref, _results;
this.options = _extend({}, this.options, options);
_ref = this.blocks;
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
block = _ref[_i];
_results.push(block.options = _extend({}, block.options, options));
}
table = this._sanitizeTable(table);
if (alias) {
alias = this._sanitizeAlias(alias);
}
this.froms.push({
name: table,
alias: alias
});
return this;
return _results;
};
Select.prototype.field = function(field, alias) {
if (alias == null) {
alias = null;
}
field = this._sanitizeField(field);
if (alias) {
alias = this._sanitizeAlias(alias);
}
this.fields.push({
name: field,
alias: alias
});
return this;
QueryBuilder.prototype.toString = function() {
var block;
return ((function() {
var _i, _len, _ref, _results;
_ref = this.blocks;
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
block = _ref[_i];
_results.push(block.buildStr(this));
}
return _results;
}).call(this)).filter(function(v) {
return 0 < v.length;
}).join(' ');
};
Select.prototype.group = function(field) {
field = this._sanitizeField(field);
this.groups.push(field);
return this;
};
Select.prototype.offset = function(start) {
start = this._sanitizeLimitOffset(start);
this.offsets = start;
return this;
};
Select.prototype.toString = function() {
var f, field, fields, groups, ret, table, tables, _i, _j, _k, _len, _len1, _len2, _ref, _ref1, _ref2;
if (0 >= this.froms.length) {
throw new Error("from() needs to be called");
}
ret = "SELECT ";
if (this.useDistinct) {
ret += "DISTINCT ";
}
fields = "";
_ref = this.fields;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
field = _ref[_i];
if ("" !== fields) {
fields += ", ";
QueryBuilder.prototype.clone = function() {
var block;
return new this.constructor(this.options, (function() {
var _i, _len, _ref, _results;
_ref = this.blocks;
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
block = _ref[_i];
_results.push(block.clone());
}
fields += field.name;
if (field.alias) {
fields += " AS \"" + field.alias + "\"";
}
}
ret += "" === fields ? "*" : fields;
tables = "";
_ref1 = this.froms;
for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
table = _ref1[_j];
if ("" !== tables) {
tables += ", ";
}
tables += table.name;
if (table.alias) {
tables += " `" + table.alias + "`";
}
}
ret += " FROM " + tables;
ret += this._joinString();
ret += this._whereString();
if (0 < this.groups.length) {
groups = "";
_ref2 = this.groups;
for (_k = 0, _len2 = _ref2.length; _k < _len2; _k++) {
f = _ref2[_k];
if ("" !== groups) {
groups += ", ";
}
groups += f;
}
ret += " GROUP BY " + groups;
}
ret += this._orderString();
ret += this._limitString();
if (this.offsets) {
ret += " OFFSET " + this.offsets;
}
return ret;
return _results;
}).call(this));
};
return Select;
return QueryBuilder;
})(JoinWhereOrderLimit);
})(cls.BaseBuilder);
Update = (function(_super) {
cls.Select = (function(_super) {
__extends(Update, _super);
__extends(Select, _super);
function Update(options) {
this.toString = __bind(this.toString, this);
function Select(options, blocks) {
if (blocks == null) {
blocks = null;
}
blocks || (blocks = [new cls.StringBlock(options, 'SELECT'), new cls.DistinctBlock(options), new cls.GetFieldBlock(options), new cls.FromTableBlock(options), new cls.JoinBlock(options), new cls.WhereBlock(options), new cls.GroupByBlock(options), new cls.OrderByBlock(options), new cls.LimitBlock(options), new cls.OffsetBlock(options)]);
Select.__super__.constructor.call(this, options, blocks);
}
this.set = __bind(this.set, this);
return Select;
this.table = __bind(this.table, this);
Update.__super__.constructor.call(this, options);
this.tables = [];
this.fields = {};
}
})(cls.QueryBuilder);
Update.prototype.table = function(table, alias) {
if (alias == null) {
alias = null;
}
table = this._sanitizeTable(table);
if (alias) {
alias = this._sanitizeAlias(alias);
}
this.tables.push({
name: table,
alias: alias
});
return this;
};
cls.Update = (function(_super) {
Update.prototype.set = function(field, value) {
field = this._sanitizeField(field);
value = this._sanitizeValue(value);
this.fields[field] = value;
return this;
};
__extends(Update, _super);
Update.prototype.toString = function() {
var field, fieldNames, fields, ret, table, tables, _i, _j, _len, _len1, _ref;
if (0 >= this.tables.length) {
throw new Error("table() needs to be called");
function Update(options, blocks) {
if (blocks == null) {
blocks = null;
}
fieldNames = (function() {
var _ref, _results;
_ref = this.fields;
_results = [];
for (field in _ref) {
if (!__hasProp.call(_ref, field)) continue;
_results.push(field);
}
return _results;
}).call(this);
if (0 >= fieldNames.length) {
throw new Error("set() needs to be called");
}
ret = "UPDATE ";
tables = "";
_ref = this.tables;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
table = _ref[_i];
if ("" !== tables) {
tables += ", ";
}
tables += table.name;
if (table.alias) {
tables += " AS `" + table.alias + "`";
}
}
ret += tables;
fields = "";
for (_j = 0, _len1 = fieldNames.length; _j < _len1; _j++) {
field = fieldNames[_j];
if ("" !== fields) {
fields += ", ";
}
fields += "" + field + " = " + (this._formatValue(this.fields[field]));
}
ret += " SET " + fields;
ret += this._whereString();
ret += this._orderString();
ret += this._limitString();
return ret;
};
blocks || (blocks = [new cls.StringBlock(options, 'UPDATE'), new cls.UpdateTableBlock(options), new cls.SetFieldBlock(options), new cls.WhereBlock(options), new cls.OrderByBlock(options), new cls.LimitBlock(options)]);
Update.__super__.constructor.call(this, options, blocks);
}
return Update;
})(WhereOrderLimit);
})(cls.QueryBuilder);
Delete = (function(_super) {
cls.Delete = (function(_super) {
__extends(Delete, _super);
function Delete() {
this.toString = __bind(this.toString, this);
this.from = __bind(this.from, this);
return Delete.__super__.constructor.apply(this, arguments);
function Delete(options, blocks) {
if (blocks == null) {
blocks = null;
}
blocks || (blocks = [
new cls.StringBlock(options, 'DELETE'), new cls.FromTableBlock(_extend({}, options, {
singleTable: true
})), new cls.JoinBlock(options), new cls.WhereBlock(options), new cls.OrderByBlock(options), new cls.LimitBlock(options)
]);
Delete.__super__.constructor.call(this, options, blocks);
}
Delete.prototype.table = null;
Delete.prototype.from = function(table, alias) {
table = this._sanitizeTable(table);
if (alias) {
alias = this._sanitizeAlias(alias);
}
this.table = {
name: table,
alias: alias
};
return this;
};
Delete.prototype.toString = function() {
var ret;
if (!this.table) {
throw new Error("from() needs to be called");
}
ret = "DELETE FROM " + this.table.name;
if (this.table.alias) {
ret += " `" + this.table.alias + "`";
}
ret += this._joinString();
ret += this._whereString();
ret += this._orderString();
ret += this._limitString();
return ret;
};
return Delete;
})(JoinWhereOrderLimit);
})(cls.QueryBuilder);
Insert = (function(_super) {
cls.Insert = (function(_super) {
__extends(Insert, _super);
function Insert(options) {
this.toString = __bind(this.toString, this);
this.set = __bind(this.set, this);
this.into = __bind(this.into, this);
Insert.__super__.constructor.call(this, options);
this.table = null;
this.fields = {};
function Insert(options, blocks) {
if (blocks == null) {
blocks = null;
}
blocks || (blocks = [new cls.StringBlock(options, 'INSERT'), new cls.IntoTableBlock(options), new cls.InsertFieldValueBlock(options)]);
Insert.__super__.constructor.call(this, options, blocks);
}
Insert.prototype.into = function(table) {
table = this._sanitizeTable(table);
this.table = table;
return this;
};
Insert.prototype.set = function(field, value) {
field = this._sanitizeField(field);
value = this._sanitizeValue(value);
this.fields[field] = value;
return this;
};
Insert.prototype.toString = function() {
var field, fieldNames, fields, name, values, _i, _len;
if (!this.table) {
throw new Error("into() needs to be called");
}
fieldNames = (function() {
var _ref, _results;
_ref = this.fields;
_results = [];
for (name in _ref) {
if (!__hasProp.call(_ref, name)) continue;
_results.push(name);
}
return _results;
}).call(this);
if (0 >= fieldNames.length) {
throw new Error("set() needs to be called");
}
fields = "";
values = "";
for (_i = 0, _len = fieldNames.length; _i < _len; _i++) {
field = fieldNames[_i];
if ("" !== fields) {
fields += ", ";
}
fields += field;
if ("" !== values) {
values += ", ";
}
values += this._formatValue(this.fields[field]);
}
return "INSERT INTO " + this.table + " (" + fields + ") VALUES (" + values + ")";
};
return Insert;
})(QueryBuilder);
})(cls.QueryBuilder);
_export = {
squel = {
expr: function() {
return new Expression;
return new cls.Expression;
},
select: function(options) {
return new Select(options);
select: function(options, blocks) {
return new cls.Select(options, blocks);
},
update: function(options) {
return new Update(options);
update: function(options, blocks) {
return new cls.Update(options, blocks);
},
insert: function(options) {
return new Insert(options);
insert: function(options, blocks) {
return new cls.Insert(options, blocks);
},
"delete": function(options) {
return new Delete(options);
},
DefaultQueryBuilderOptions: DefaultQueryBuilderOptions,
Cloneable: Cloneable,
Expression: Expression,
QueryBuilder: QueryBuilder,
WhereOrderLimit: WhereOrderLimit,
JoinWhereOrderLimit: JoinWhereOrderLimit,
Select: Select,
Update: Update,
Insert: Insert,
Delete: Delete
"delete": function(options, blocks) {
return new cls.Delete(options, blocks);
}
};
_export.remove = _export["delete"];
squel.remove = squel["delete"];
if (typeof module !== "undefined" && module !== null) {
module.exports = _export;
}
squel.cls = cls;
if (typeof window !== "undefined" && window !== null) {
window.squel = _export;
if (typeof define !== "undefined" && define !== null ? define.amd : void 0) {
define(function() {
return squel;
});
} else if (typeof module !== "undefined" && module !== null ? module.exports : void 0) {
module.exports = squel;
} else {
if (typeof window !== "undefined" && window !== null) {
window.squel = squel;
}
}
}).call(this);

@@ -25,2 +25,2 @@ // Generated by CoffeeScript 1.3.3

OTHER DEALINGS IN THE SOFTWARE.
*/(function(){var e,t,n,r,i,s,o,u,a,f,l,c,h=[].slice,p={}.hasOwnProperty,d=function(e,t){return function(){return e.apply(t,arguments)}},v=function(e,t){function r(){this.constructor=e}for(var n in t)p.call(t,n)&&(e[n]=t[n]);return r.prototype=t.prototype,e.prototype=new r,e.__super__=t.prototype,e};c=function(){var e,t,n,r,i,s,o;e=arguments[0],n=2<=arguments.length?h.call(arguments,1):[];if(n)for(s=0,o=n.length;s<o;s++){r=n[s];if(r)for(t in r){if(!p.call(r,t))continue;i=r[t],e[t]=i}}return e},e=function(){function e(){}return e.prototype.clone=function(){var e;return e=new this.constructor,c(e,JSON.parse(JSON.stringify(this)))},e}(),r=function(){function t(){this.toString=d(this.toString,this),this.or=d(this.or,this),this.and=d(this.and,this),this.end=d(this.end,this),this.or_begin=d(this.or_begin,this),this.and_begin=d(this.and_begin,this);var e=this;this.tree={parent:null,nodes:[]},this.current=this.tree,this._begin=function(t){var n;return n={type:t,parent:e.current,nodes:[]},e.current.nodes.push(n),e.current=e.current.nodes[e.current.nodes.length-1],e}}var e;return t.prototype.tree=null,t.prototype.current=null,t.prototype.and_begin=function(){return this._begin("AND")},t.prototype.or_begin=function(){return this._begin("OR")},t.prototype.end=function(){if(!this.current.parent)throw new Error("begin() needs to be called");return this.current=this.current.parent,this},t.prototype.and=function(e){if(!e||"string"!=typeof e)throw new Error("expr must be a string");return this.current.nodes.push({type:"AND",expr:e}),this},t.prototype.or=function(e){if(!e||"string"!=typeof e)throw new Error("expr must be a string");return this.current.nodes.push({type:"OR",expr:e}),this},t.prototype.toString=function(){if(null!==this.current.parent)throw new Error("end() needs to be called");return e(this.tree)},e=function(t){var n,r,i,s,o,u;i="",u=t.nodes;for(s=0,o=u.length;s<o;s++)n=u[s],n.expr!=null?r=n.expr:(r=e(n),""!==r&&(r="("+r+")")),""!==r&&(""!==i&&(i+=" "+n.type+" "),i+=r);return i},t}(),t={autoQuoteTableNames:!1,autoQuoteFieldNames:!1,nameQuoteCharacter:"`",usingValuePlaceholders:!1},o=function(e){function n(e){this.options=c({},t,e)}return v(n,e),n.prototype._getObjectClassName=function(e){var t;if(e&&e.constructor&&e.constructor.toString){t=e.constructor.toString().match(/function\s*(\w+)/);if(t&&t.length===2)return t[1]}return void 0},n.prototype._sanitizeCondition=function(e){var t,n;n=typeof e,t=this._getObjectClassName(e);if("Expression"!==t&&"string"!==n)throw new Error("condition must be a string or Expression instance");if("Expression"===n||"Expression"===t)e=e.toString();return e},n.prototype._sanitizeName=function(e,t){if("string"!=typeof e)throw new Error(""+t+" must be a string");return e},n.prototype._sanitizeField=function(e){var t;return t=this._sanitizeName(e,"field name"),this.options.autoQuoteFieldNames?""+this.options.nameQuoteCharacter+t+this.options.nameQuoteCharacter:t},n.prototype._sanitizeTable=function(e){var t;return t=this._sanitizeName(e,"table name"),this.options.autoQuoteTableNames?""+this.options.nameQuoteCharacter+t+this.options.nameQuoteCharacter:t},n.prototype._sanitizeAlias=function(e){return this._sanitizeName(e,"alias")},n.prototype._sanitizeLimitOffset=function(e){e=parseInt(e);if(0>e||isNaN(e))throw new Error("limit/offset must be >=0");return e},n.prototype._sanitizeValue=function(e){var t;t=typeof e;if(null!==e&&"string"!==t&&"number"!==t&&"boolean"!==t)throw new Error("field value must be a string, number, boolean or null");return e},n.prototype._formatValue=function(e){return null===e?e="NULL":"boolean"==typeof e?e=e?"TRUE":"FALSE":"number"!=typeof e&&!1===this.options.usingValuePlaceholders&&(e="'"+e+"'"),e},n}(e),f=function(e){function t(e){this._limitString=d(this._limitString,this),this._orderString=d(this._orderString,this),this._whereString=d(this._whereString,this),this.limit=d(this.limit,this),this.order=d(this.order,this),this.where=d(this.where,this),t.__super__.constructor.call(this,e),this.wheres=[],this.orders=[],this.limits=null}return v(t,e),t.prototype.where=function(e){return e=this._sanitizeCondition(e),""!==e&&this.wheres.push(e),this},t.prototype.order=function(e,t){return t==null&&(t=!0),e=this._sanitizeField(e),this.orders.push({field:e,dir:t?"ASC":"DESC"}),this},t.prototype.limit=function(e){return e=this._sanitizeLimitOffset(e),this.limits=e,this},t.prototype._whereString=function(){return 0<this.wheres.length?" WHERE ("+this.wheres.join(") AND (")+")":""},t.prototype._orderString=function(){var e,t,n,r,i;if(0<this.orders.length){t="",i=this.orders;for(n=0,r=i.length;n<r;n++)e=i[n],""!==t&&(t+=", "),t+=""+e.field+" "+e.dir;return" ORDER BY "+t}return""},t.prototype._limitString=function(){return this.limits?" LIMIT "+this.limits:""},t}(o),s=function(e){function t(e){this._joinString=d(this._joinString,this),this.outer_join=d(this.outer_join,this),this.right_join=d(this.right_join,this),this.left_join=d(this.left_join,this),this.join=d(this.join,this),t.__super__.constructor.call(this,e),this.joins=[]}return v(t,e),t.prototype.join=function(e,t,n,r){return r==null&&(r="INNER"),e=this._sanitizeTable(e),t&&(t=this._sanitizeAlias(t)),n&&(n=this._sanitizeCondition(n)),this.joins.push({type:r,table:e,alias:t,condition:n}),this},t.prototype.left_join=function(e,t,n){return t==null&&(t=null),n==null&&(n=null),this.join(e,t,n,"LEFT")},t.prototype.right_join=function(e,t,n){return t==null&&(t=null),n==null&&(n=null),this.join(e,t,n,"RIGHT")},t.prototype.outer_join=function(e,t,n){return t==null&&(t=null),n==null&&(n=null),this.join(e,t,n,"OUTER")},t.prototype._joinString=function(){var e,t,n,r,i;t="",i=this.joins||[];for(n=0,r=i.length;n<r;n++)e=i[n],t+=" "+e.type+" JOIN "+e.table,e.alias&&(t+=" `"+e.alias+"`"),e.condition&&(t+=" ON ("+e.condition+")");return t},t}(f),u=function(e){function t(e){this.toString=d(this.toString,this),this.offset=d(this.offset,this),this.group=d(this.group,this),this.field=d(this.field,this),this.from=d(this.from,this),this.distinct=d(this.distinct,this),t.__super__.constructor.call(this,e),this.froms=[],this.fields=[],this.groups=[],this.offsets=null,this.useDistinct=!1}return v(t,e),t.prototype.distinct=function(){return this.useDistinct=!0,this},t.prototype.from=function(e,t){return t==null&&(t=null),e=this._sanitizeTable(e),t&&(t=this._sanitizeAlias(t)),this.froms.push({name:e,alias:t}),this},t.prototype.field=function(e,t){return t==null&&(t=null),e=this._sanitizeField(e),t&&(t=this._sanitizeAlias(t)),this.fields.push({name:e,alias:t}),this},t.prototype.group=function(e){return e=this._sanitizeField(e),this.groups.push(e),this},t.prototype.offset=function(e){return e=this._sanitizeLimitOffset(e),this.offsets=e,this},t.prototype.toString=function(){var e,t,n,r,i,s,o,u,a,f,l,c,h,p,d,v;if(0>=this.froms.length)throw new Error("from() needs to be called");i="SELECT ",this.useDistinct&&(i+="DISTINCT "),n="",p=this.fields;for(u=0,l=p.length;u<l;u++)t=p[u],""!==n&&(n+=", "),n+=t.name,t.alias&&(n+=' AS "'+t.alias+'"');i+=""===n?"*":n,o="",d=this.froms;for(a=0,c=d.length;a<c;a++)s=d[a],""!==o&&(o+=", "),o+=s.name,s.alias&&(o+=" `"+s.alias+"`");i+=" FROM "+o,i+=this._joinString(),i+=this._whereString();if(0<this.groups.length){r="",v=this.groups;for(f=0,h=v.length;f<h;f++)e=v[f],""!==r&&(r+=", "),r+=e;i+=" GROUP BY "+r}return i+=this._orderString(),i+=this._limitString(),this.offsets&&(i+=" OFFSET "+this.offsets),i},t}(s),a=function(e){function t(e){this.toString=d(this.toString,this),this.set=d(this.set,this),this.table=d(this.table,this),t.__super__.constructor.call(this,e),this.tables=[],this.fields={}}return v(t,e),t.prototype.table=function(e,t){return t==null&&(t=null),e=this._sanitizeTable(e),t&&(t=this._sanitizeAlias(t)),this.tables.push({name:e,alias:t}),this},t.prototype.set=function(e,t){return e=this._sanitizeField(e),t=this._sanitizeValue(t),this.fields[e]=t,this},t.prototype.toString=function(){var e,t,n,r,i,s,o,u,a,f,l;if(0>=this.tables.length)throw new Error("table() needs to be called");t=function(){var t,n;t=this.fields,n=[];for(e in t){if(!p.call(t,e))continue;n.push(e)}return n}.call(this);if(0>=t.length)throw new Error("set() needs to be called");r="UPDATE ",s="",l=this.tables;for(o=0,a=l.length;o<a;o++)i=l[o],""!==s&&(s+=", "),s+=i.name,i.alias&&(s+=" AS `"+i.alias+"`");r+=s,n="";for(u=0,f=t.length;u<f;u++)e=t[u],""!==n&&(n+=", "),n+=""+e+" = "+this._formatValue(this.fields[e]);return r+=" SET "+n,r+=this._whereString(),r+=this._orderString(),r+=this._limitString(),r},t}(f),n=function(e){function t(){return this.toString=d(this.toString,this),this.from=d(this.from,this),t.__super__.constructor.apply(this,arguments)}return v(t,e),t.prototype.table=null,t.prototype.from=function(e,t){return e=this._sanitizeTable(e),t&&(t=this._sanitizeAlias(t)),this.table={name:e,alias:t},this},t.prototype.toString=function(){var e;if(!this.table)throw new Error("from() needs to be called");return e="DELETE FROM "+this.table.name,this.table.alias&&(e+=" `"+this.table.alias+"`"),e+=this._joinString(),e+=this._whereString(),e+=this._orderString(),e+=this._limitString(),e},t}(s),i=function(e){function t(e){this.toString=d(this.toString,this),this.set=d(this.set,this),this.into=d(this.into,this),t.__super__.constructor.call(this,e),this.table=null,this.fields={}}return v(t,e),t.prototype.into=function(e){return e=this._sanitizeTable(e),this.table=e,this},t.prototype.set=function(e,t){return e=this._sanitizeField(e),t=this._sanitizeValue(t),this.fields[e]=t,this},t.prototype.toString=function(){var e,t,n,r,i,s,o;if(!this.table)throw new Error("into() needs to be called");t=function(){var e,t;e=this.fields,t=[];for(r in e){if(!p.call(e,r))continue;t.push(r)}return t}.call(this);if(0>=t.length)throw new Error("set() needs to be called");n="",i="";for(s=0,o=t.length;s<o;s++)e=t[s],""!==n&&(n+=", "),n+=e,""!==i&&(i+=", "),i+=this._formatValue(this.fields[e]);return"INSERT INTO "+this.table+" ("+n+") VALUES ("+i+")"},t}(o),l={expr:function(){return new r},select:function(e){return new u(e)},update:function(e){return new a(e)},insert:function(e){return new i(e)},"delete":function(e){return new n(e)},DefaultQueryBuilderOptions:t,Cloneable:e,Expression:r,QueryBuilder:o,WhereOrderLimit:f,JoinWhereOrderLimit:s,Select:u,Update:a,Insert:i,Delete:n},l.remove=l["delete"],typeof module!="undefined"&&module!==null&&(module.exports=l),typeof window!="undefined"&&window!==null&&(window.squel=l)}).call(this);
*/(function(){var e,t,n,r=[].slice,i={}.hasOwnProperty,s=function(e,t){function r(){this.constructor=e}for(var n in t)i.call(t,n)&&(e[n]=t[n]);return r.prototype=t.prototype,e.prototype=new r,e.__super__=t.prototype,e};e={},n=function(){var e,t,n,s,o,u,a;e=arguments[0],n=2<=arguments.length?r.call(arguments,1):[];if(n)for(u=0,a=n.length;u<a;u++){s=n[u];if(s)for(t in s){if(!i.call(s,t))continue;o=s[t],e[t]=o}}return e},e.DefaultQueryBuilderOptions={autoQuoteTableNames:!1,autoQuoteFieldNames:!1,nameQuoteCharacter:"`",usingValuePlaceholders:!1},e.Cloneable=function(){function e(){}return e.prototype.clone=function(){var e;return e=new this.constructor,n(e,JSON.parse(JSON.stringify(this)))},e}(),e.BaseBuilder=function(t){function r(t){this.options=n({},e.DefaultQueryBuilderOptions,t)}return s(r,t),r.prototype._getObjectClassName=function(e){var t;if(e&&e.constructor&&e.constructor.toString){t=e.constructor.toString().match(/function\s*(\w+)/);if(t&&t.length===2)return t[1]}return void 0},r.prototype._sanitizeCondition=function(t){t instanceof e.Expression&&(t=t.toString());if("string"!=typeof t)throw new Error("condition must be a string or Expression instance");return t},r.prototype._sanitizeName=function(e,t){if("string"!=typeof e)throw new Error(""+t+" must be a string");return e},r.prototype._sanitizeField=function(e){var t;return t=this._sanitizeName(e,"field name"),this.options.autoQuoteFieldNames?""+this.options.nameQuoteCharacter+t+this.options.nameQuoteCharacter:t},r.prototype._sanitizeTable=function(e){var t;return t=this._sanitizeName(e,"table name"),this.options.autoQuoteTableNames?""+this.options.nameQuoteCharacter+t+this.options.nameQuoteCharacter:t},r.prototype._sanitizeAlias=function(e){return this._sanitizeName(e,"alias")},r.prototype._sanitizeLimitOffset=function(e){e=parseInt(e);if(0>e||isNaN(e))throw new Error("limit/offset must be >=0");return e},r.prototype._sanitizeValue=function(e){var t;t=typeof e;if(null!==e&&"string"!==t&&"number"!==t&&"boolean"!==t)throw new Error("field value must be a string, number, boolean or null");return e},r.prototype._formatValue=function(e){return null===e?e="NULL":"boolean"==typeof e?e=e?"TRUE":"FALSE":"number"!=typeof e&&!1===this.options.usingValuePlaceholders&&(e="'"+e+"'"),e},r}(e.Cloneable),e.Expression=function(){function t(){var e=this;this.tree={parent:null,nodes:[]},this.current=this.tree,this._begin=function(t){var n;return n={type:t,parent:e.current,nodes:[]},e.current.nodes.push(n),e.current=e.current.nodes[e.current.nodes.length-1],e}}var e;return t.prototype.tree=null,t.prototype.current=null,t.prototype.and_begin=function(){return this._begin("AND")},t.prototype.or_begin=function(){return this._begin("OR")},t.prototype.end=function(){if(!this.current.parent)throw new Error("begin() needs to be called");return this.current=this.current.parent,this},t.prototype.and=function(e){if(!e||"string"!=typeof e)throw new Error("expr must be a string");return this.current.nodes.push({type:"AND",expr:e}),this},t.prototype.or=function(e){if(!e||"string"!=typeof e)throw new Error("expr must be a string");return this.current.nodes.push({type:"OR",expr:e}),this},t.prototype.toString=function(){if(null!==this.current.parent)throw new Error("end() needs to be called");return e(this.tree)},e=function(t){var n,r,i,s,o,u;i="",u=t.nodes;for(s=0,o=u.length;s<o;s++)n=u[s],n.expr!=null?r=n.expr:(r=e(n),""!==r&&(r="("+r+")")),""!==r&&(""!==i&&(i+=" "+n.type+" "),i+=r);return i},t}(),e.Block=function(t){function n(){return n.__super__.constructor.apply(this,arguments)}return s(n,t),n.prototype.exposedMethods=function(){var t,n,r;n={};for(t in this)r=this[t],typeof r=="function"&&t.charAt(0)!=="_"&&!e.Block.prototype[t]&&(n[t]=r);return n},n.prototype.buildStr=function(e){return""},n}(e.BaseBuilder),e.StringBlock=function(e){function t(e,n){t.__super__.constructor.call(this,e),this.str=n}return s(t,e),t.prototype.buildStr=function(e){return this.str},t}(e.Block),e.AbstractTableBlock=function(e){function t(e){t.__super__.constructor.call(this,e),this.tables=[]}return s(t,e),t.prototype._table=function(e,t){return t==null&&(t=null),e=this._sanitizeTable(e),t&&(t=this._sanitizeAlias(t)),this.options.singleTable&&(this.tables=[]),this.tables.push({name:e,alias:t})},t.prototype.buildStr=function(e){var t,n,r,i,s;if(0>=this.tables.length)throw new Error("table() needs to be called");n="",s=this.tables;for(r=0,i=s.length;r<i;r++)t=s[r],""!==n&&(n+=", "),n+=t.name,t.alias&&(n+=" AS `"+t.alias+"`");return n},t}(e.Block),e.UpdateTableBlock=function(e){function t(){return t.__super__.constructor.apply(this,arguments)}return s(t,e),t.prototype.table=function(e,t){return t==null&&(t=null),this._table(e,t)},t}(e.AbstractTableBlock),e.FromTableBlock=function(e){function t(){return t.__super__.constructor.apply(this,arguments)}return s(t,e),t.prototype.from=function(e,t){return t==null&&(t=null),this._table(e,t)},t.prototype.buildStr=function(e){var t,n,r,i,s;if(0>=this.tables.length)throw new Error("from() needs to be called");n="",s=this.tables;for(r=0,i=s.length;r<i;r++)t=s[r],""!==n&&(n+=", "),n+=t.name,t.alias&&(n+=" `"+t.alias+"`");return"FROM "+n},t}(e.AbstractTableBlock),e.IntoTableBlock=function(e){function t(e){t.__super__.constructor.call(this,e),this.table=null}return s(t,e),t.prototype.into=function(e){return this.table=this._sanitizeTable(e)},t.prototype.buildStr=function(e){if(!this.table)throw new Error("into() needs to be called");return"INTO "+this.table},t}(e.Block),e.GetFieldBlock=function(e){function t(e){t.__super__.constructor.call(this,e),this.fields=[]}return s(t,e),t.prototype.field=function(e,t){return t==null&&(t=null),e=this._sanitizeField(e),t&&(t=this._sanitizeAlias(t)),this.fields.push({name:e,alias:t})},t.prototype.buildStr=function(e){var t,n,r,i,s;n="",s=this.fields;for(r=0,i=s.length;r<i;r++)t=s[r],""!==n&&(n+=", "),n+=t.name,t.alias&&(n+=' AS "'+t.alias+'"');return""===n?"*":n},t}(e.Block),e.SetFieldBlock=function(e){function t(e){t.__super__.constructor.call(this,e),this.fields={}}return s(t,e),t.prototype.set=function(e,t){return e=this._sanitizeField(e),t=this._sanitizeValue(t),this.fields[e]=t,this},t.prototype.buildStr=function(e){var t,n,r,s,o;n=function(){var e,n;e=this.fields,n=[];for(t in e){if(!i.call(e,t))continue;n.push(t)}return n}.call(this);if(0>=n.length)throw new Error("set() needs to be called");r="";for(s=0,o=n.length;s<o;s++)t=n[s],""!==r&&(r+=", "),r+=""+t+" = "+this._formatValue(this.fields[t]);return"SET "+r},t}(e.Block),e.InsertFieldValueBlock=function(e){function t(e){t.__super__.constructor.call(this,e),this.fields={}}return s(t,e),t.prototype.buildStr=function(e){var t,n,r,s,o,u,a;n=function(){var e,t;e=this.fields,t=[];for(s in e){if(!i.call(e,s))continue;t.push(s)}return t}.call(this);if(0>=n.length)throw new Error("set() needs to be called");r="",o="";for(u=0,a=n.length;u<a;u++)t=n[u],""!==r&&(r+=", "),r+=t,""!==o&&(o+=", "),o+=this._formatValue(this.fields[t]);return"("+r+") VALUES ("+o+")"},t}(e.SetFieldBlock),e.DistinctBlock=function(e){function t(e){t.__super__.constructor.call(this,e),this.useDistinct=!1}return s(t,e),t.prototype.distinct=function(){return this.useDistinct=!0},t.prototype.buildStr=function(e){return this.useDistinct?"DISTINCT":""},t}(e.Block),e.GroupByBlock=function(e){function t(e){t.__super__.constructor.call(this,e),this.groups=[]}return s(t,e),t.prototype.group=function(e){return e=this._sanitizeField(e),this.groups.push(e)},t.prototype.buildStr=function(e){var t,n,r,i,s;n="";if(0<this.groups.length){s=this.groups;for(r=0,i=s.length;r<i;r++)t=s[r],""!==n&&(n+=", "),n+=t;n="GROUP BY "+n}return n},t}(e.Block),e.OffsetBlock=function(e){function t(e){t.__super__.constructor.call(this,e),this.offsets=null}return s(t,e),t.prototype.offset=function(e){return e=this._sanitizeLimitOffset(e),this.offsets=e},t.prototype.buildStr=function(e){return this.offsets?"OFFSET "+this.offsets:""},t}(e.Block),e.WhereBlock=function(e){function t(e){t.__super__.constructor.call(this,e),this.wheres=[]}return s(t,e),t.prototype.where=function(e){e=this._sanitizeCondition(e);if(""!==e)return this.wheres.push(e)},t.prototype.buildStr=function(e){return 0<this.wheres.length?"WHERE ("+this.wheres.join(") AND (")+")":""},t}(e.Block),e.OrderByBlock=function(e){function t(e){t.__super__.constructor.call(this,e),this.orders=[]}return s(t,e),t.prototype.order=function(e,t){return t==null&&(t=!0),e=this._sanitizeField(e),this.orders.push({field:e,dir:t?!0:!1})},t.prototype.buildStr=function(e){var t,n,r,i,s;if(0<this.orders.length){n="",s=this.orders;for(r=0,i=s.length;r<i;r++)t=s[r],""!==n&&(n+=", "),n+=""+t.field+" "+(t.dir?"ASC":"DESC");return"ORDER BY "+n}return""},t}(e.Block),e.LimitBlock=function(e){function t(e){t.__super__.constructor.call(this,e),this.limits=null}return s(t,e),t.prototype.limit=function(e){return e=this._sanitizeLimitOffset(e),this.limits=e},t.prototype.buildStr=function(e){return this.limits?"LIMIT "+this.limits:""},t}(e.Block),e.JoinBlock=function(e){function t(e){t.__super__.constructor.call(this,e),this.joins=[]}return s(t,e),t.prototype.join=function(e,t,n,r){return t==null&&(t=null),n==null&&(n=null),r==null&&(r="INNER"),e=this._sanitizeTable(e),t&&(t=this._sanitizeAlias(t)),n&&(n=this._sanitizeCondition(n)),this.joins.push({type:r,table:e,alias:t,condition:n}),this},t.prototype.left_join=function(e,t,n){return t==null&&(t=null),n==null&&(n=null),this.join(e,t,n,"LEFT")},t.prototype.right_join=function(e,t,n){return t==null&&(t=null),n==null&&(n=null),this.join(e,t,n,"RIGHT")},t.prototype.outer_join=function(e,t,n){return t==null&&(t=null),n==null&&(n=null),this.join(e,t,n,"OUTER")},t.prototype.buildStr=function(e){var t,n,r,i,s;n="",s=this.joins||[];for(r=0,i=s.length;r<i;r++)t=s[r],n!==""&&(n+=" "),n+=""+t.type+" JOIN "+t.table,t.alias&&(n+=" `"+t.alias+"`"),t.condition&&(n+=" ON ("+t.condition+")");return n},t}(e.Block),e.QueryBuilder=function(e){function t(e,n){var r,i,s,o,u,a,f,l,c=this;t.__super__.constructor.call(this,e),this.blocks=n||[],f=this.blocks;for(u=0,a=f.length;u<a;u++){r=f[u],l=r.exposedMethods(),o=function(e,t,n){return c[t]=function(){return n.apply(e,arguments),c}};for(s in l){i=l[s];if(this[s]!=null)throw new Error(""+this._getObjectClassName(this)+" already has a builder method called: "+s);o(r,s,i)}}}return s(t,e),t.prototype.updateOptions=function(e){var t,r,i,s,o;this.options=n({},this.options,e),s=this.blocks,o=[];for(r=0,i=s.length;r<i;r++)t=s[r],o.push(t.options=n({},t.options,e));return o},t.prototype.toString=function(){var e;return function(){var t,n,r,i;r=this.blocks,i=[];for(t=0,n=r.length;t<n;t++)e=r[t],i.push(e.buildStr(this));return i}.call(this).filter(function(e){return 0<e.length}).join(" ")},t.prototype.clone=function(){var e;return new this.constructor(this.options,function(){var t,n,r,i;r=this.blocks,i=[];for(t=0,n=r.length;t<n;t++)e=r[t],i.push(e.clone());return i}.call(this))},t}(e.BaseBuilder),e.Select=function(t){function n(t,r){r==null&&(r=null),r||(r=[new e.StringBlock(t,"SELECT"),new e.DistinctBlock(t),new e.GetFieldBlock(t),new e.FromTableBlock(t),new e.JoinBlock(t),new e.WhereBlock(t),new e.GroupByBlock(t),new e.OrderByBlock(t),new e.LimitBlock(t),new e.OffsetBlock(t)]),n.__super__.constructor.call(this,t,r)}return s(n,t),n}(e.QueryBuilder),e.Update=function(t){function n(t,r){r==null&&(r=null),r||(r=[new e.StringBlock(t,"UPDATE"),new e.UpdateTableBlock(t),new e.SetFieldBlock(t),new e.WhereBlock(t),new e.OrderByBlock(t),new e.LimitBlock(t)]),n.__super__.constructor.call(this,t,r)}return s(n,t),n}(e.QueryBuilder),e.Delete=function(t){function r(t,i){i==null&&(i=null),i||(i=[new e.StringBlock(t,"DELETE"),new e.FromTableBlock(n({},t,{singleTable:!0})),new e.JoinBlock(t),new e.WhereBlock(t),new e.OrderByBlock(t),new e.LimitBlock(t)]),r.__super__.constructor.call(this,t,i)}return s(r,t),r}(e.QueryBuilder),e.Insert=function(t){function n(t,r){r==null&&(r=null),r||(r=[new e.StringBlock(t,"INSERT"),new e.IntoTableBlock(t),new e.InsertFieldValueBlock(t)]),n.__super__.constructor.call(this,t,r)}return s(n,t),n}(e.QueryBuilder),t={expr:function(){return new e.Expression},select:function(t,n){return new e.Select(t,n)},update:function(t,n){return new e.Update(t,n)},insert:function(t,n){return new e.Insert(t,n)},"delete":function(t,n){return new e.Delete(t,n)}},t.remove=t["delete"],t.cls=e,(typeof define!="undefined"&&define!==null?define.amd:void 0)?define(function(){return t}):(typeof module!="undefined"&&module!==null?module.exports:void 0)?module.exports=t:typeof window!="undefined"&&window!==null&&(window.squel=t)}).call(this);

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc