diff --git a/squel-basic.js b/squel-basic.js index d03ea66f..671c5322 100644 --- a/squel-basic.js +++ b/squel-basic.js @@ -25,9 +25,10 @@ OTHER DEALINGS IN THE SOFTWARE. (function() { - var cls, getValueHandler, registerValueHandler, squel, _extend, _ref, _ref1, _ref2, _ref3, _ref4, _without, + var cls, getValueHandler, registerValueHandler, squel, _extend, _ref, _ref1, _ref2, _ref3, _ref4, _ref5, _without, __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 = {}; @@ -135,6 +136,7 @@ OTHER DEALINGS IN THE SOFTWARE. __extends(BaseBuilder, _super); function BaseBuilder(options) { + this._sanitizeNestableQuery = __bind(this._sanitizeNestableQuery, this); var defaults; defaults = JSON.parse(JSON.stringify(cls.DefaultQueryBuilderOptions)); this.options = _extend({}, defaults, options); @@ -199,18 +201,28 @@ OTHER DEALINGS IN THE SOFTWARE. return item; }; + BaseBuilder.prototype._sanitizeNestableQuery = function(item) { + if (item instanceof cls.QueryBuilder && item.isNestable()) { + return item; + } + throw new Error("must be a nestable query, e.g. SELECT"); + }; + BaseBuilder.prototype._sanitizeTable = function(item, allowNested) { - var sanitized; + var e, sanitized; if (allowNested == null) { allowNested = false; } if (allowNested) { if ("string" === typeof item) { sanitized = item; - } else if (item instanceof cls.QueryBuilder && item.isNestable()) { - return item; } else { - throw new Error("table name must be a string or a nestable query instance"); + try { + sanitized = this._sanitizeNestableQuery(item); + } catch (_error) { + e = _error; + throw new Error("table name must be a string or a nestable query instance"); + } } } else { sanitized = this._sanitizeName(item, 'table name'); @@ -1144,6 +1156,49 @@ OTHER DEALINGS IN THE SOFTWARE. })(cls.AbstractSetFieldBlock); + cls.InsertFieldSelectQueryBlock = (function(_super) { + __extends(InsertFieldSelectQueryBlock, _super); + + function InsertFieldSelectQueryBlock() { + _ref5 = InsertFieldSelectQueryBlock.__super__.constructor.apply(this, arguments); + return _ref5; + } + + InsertFieldSelectQueryBlock.prototype.fromSelect = function(fields, selectQuery) { + this._fields = fields; + return this._query = this._sanitizeNestableQuery(selectQuery); + }; + + InsertFieldSelectQueryBlock.prototype.buildStr = function(queryBuilder) { + if (0 >= this.fields.length) { + throw new Error("set() needs to be called"); + } + return "(" + (this.fields.join(', ')) + ") VALUES (" + (this._buildVals().join('), (')) + ")"; + }; + + InsertFieldSelectQueryBlock.prototype.buildParam = function(queryBuilder) { + var i, params, str, vals, _i, _ref6, _ref7; + if (0 >= this.fields.length) { + throw new Error("set() needs to be called"); + } + str = ""; + _ref6 = this._buildValParams(), vals = _ref6.vals, params = _ref6.params; + for (i = _i = 0, _ref7 = this.fields.length; 0 <= _ref7 ? _i < _ref7 : _i > _ref7; i = 0 <= _ref7 ? ++_i : --_i) { + if ("" !== str) { + str += ", "; + } + str += this.fields[i]; + } + return { + text: "(" + str + ") VALUES (" + (vals.join('), (')) + ")", + values: params + }; + }; + + return InsertFieldSelectQueryBlock; + + })(cls.Block); + cls.DistinctBlock = (function(_super) { __extends(DistinctBlock, _super); @@ -1182,12 +1237,12 @@ OTHER DEALINGS IN THE SOFTWARE. }; GroupByBlock.prototype.buildStr = function(queryBuilder) { - var f, groups, _i, _len, _ref5; + var f, groups, _i, _len, _ref6; groups = ""; if (0 < this.groups.length) { - _ref5 = this.groups; - for (_i = 0, _len = _ref5.length; _i < _len; _i++) { - f = _ref5[_i]; + _ref6 = this.groups; + for (_i = 0, _len = _ref6.length; _i < _len; _i++) { + f = _ref6[_i]; if ("" !== groups) { groups += ", "; } @@ -1236,7 +1291,7 @@ OTHER DEALINGS IN THE SOFTWARE. } WhereBlock.prototype.where = function() { - var c, condition, finalCondition, finalValues, idx, inValues, item, nextValue, t, values, _i, _j, _len, _ref5; + var c, condition, finalCondition, finalValues, idx, inValues, item, nextValue, t, values, _i, _j, _len, _ref6; condition = arguments[0], values = 2 <= arguments.length ? __slice.call(arguments, 1) : []; condition = this._sanitizeCondition(condition); finalCondition = ""; @@ -1246,7 +1301,7 @@ OTHER DEALINGS IN THE SOFTWARE. finalCondition = t.text; finalValues = t.values; } else { - for (idx = _i = 0, _ref5 = condition.length; 0 <= _ref5 ? _i < _ref5 : _i > _ref5; idx = 0 <= _ref5 ? ++_i : --_i) { + for (idx = _i = 0, _ref6 = condition.length; 0 <= _ref6 ? _i < _ref6 : _i > _ref6; idx = 0 <= _ref6 ? ++_i : --_i) { c = condition.charAt(idx); if ('?' === c && 0 < values.length) { nextValue = values.shift(); @@ -1284,20 +1339,20 @@ OTHER DEALINGS IN THE SOFTWARE. }; WhereBlock.prototype.buildStr = function(queryBuilder) { - var c, idx, pIndex, where, whereStr, _i, _j, _len, _ref5, _ref6; + var c, idx, pIndex, where, whereStr, _i, _j, _len, _ref6, _ref7; if (0 >= this.wheres.length) { return ""; } whereStr = ""; - _ref5 = this.wheres; - for (_i = 0, _len = _ref5.length; _i < _len; _i++) { - where = _ref5[_i]; + _ref6 = this.wheres; + for (_i = 0, _len = _ref6.length; _i < _len; _i++) { + where = _ref6[_i]; if ("" !== whereStr) { whereStr += ") AND ("; } if (0 < where.values.length) { pIndex = 0; - for (idx = _j = 0, _ref6 = where.text.length; 0 <= _ref6 ? _j < _ref6 : _j > _ref6; idx = 0 <= _ref6 ? ++_j : --_j) { + for (idx = _j = 0, _ref7 = where.text.length; 0 <= _ref7 ? _j < _ref7 : _j > _ref7; idx = 0 <= _ref7 ? ++_j : --_j) { c = where.text.charAt(idx); if ('?' === c) { whereStr += this._formatValue(where.values[pIndex++]); @@ -1313,7 +1368,7 @@ OTHER DEALINGS IN THE SOFTWARE. }; WhereBlock.prototype.buildParam = function(queryBuilder) { - var i, p, qv, ret, str, v, where, whereStr, _i, _j, _k, _len, _len1, _len2, _ref5, _ref6, _ref7; + var i, p, qv, ret, str, v, where, whereStr, _i, _j, _k, _len, _len1, _len2, _ref6, _ref7, _ref8; ret = { text: "", values: [] @@ -1322,26 +1377,26 @@ OTHER DEALINGS IN THE SOFTWARE. return ret; } whereStr = ""; - _ref5 = this.wheres; - for (_i = 0, _len = _ref5.length; _i < _len; _i++) { - where = _ref5[_i]; + _ref6 = this.wheres; + for (_i = 0, _len = _ref6.length; _i < _len; _i++) { + where = _ref6[_i]; if ("" !== whereStr) { whereStr += ") AND ("; } str = where.text.split('?'); i = 0; - _ref6 = where.values; - for (_j = 0, _len1 = _ref6.length; _j < _len1; _j++) { - v = _ref6[_j]; + _ref7 = where.values; + for (_j = 0, _len1 = _ref7.length; _j < _len1; _j++) { + v = _ref7[_j]; if (str[i] != null) { whereStr += "" + str[i]; } p = this._formatValueAsParam(v); if (((p != null ? p.text : void 0) != null)) { whereStr += "(" + p.text + ")"; - _ref7 = p.values; - for (_k = 0, _len2 = _ref7.length; _k < _len2; _k++) { - qv = _ref7[_k]; + _ref8 = p.values; + for (_k = 0, _len2 = _ref8.length; _k < _len2; _k++) { + qv = _ref8[_k]; ret.values.push(qv); } } else { @@ -1386,22 +1441,22 @@ OTHER DEALINGS IN THE SOFTWARE. }; OrderByBlock.prototype._buildStr = function(toParam) { - var c, fstr, idx, o, orders, pIndex, _i, _j, _len, _ref5, _ref6; + var c, fstr, idx, o, orders, pIndex, _i, _j, _len, _ref6, _ref7; if (toParam == null) { toParam = false; } if (0 < this.orders.length) { pIndex = 0; orders = ""; - _ref5 = this.orders; - for (_i = 0, _len = _ref5.length; _i < _len; _i++) { - o = _ref5[_i]; + _ref6 = this.orders; + for (_i = 0, _len = _ref6.length; _i < _len; _i++) { + o = _ref6[_i]; if ("" !== orders) { orders += ", "; } fstr = ""; if (!toParam) { - for (idx = _j = 0, _ref6 = o.field.length; 0 <= _ref6 ? _j < _ref6 : _j > _ref6; idx = 0 <= _ref6 ? ++_j : --_j) { + for (idx = _j = 0, _ref7 = o.field.length; 0 <= _ref7 ? _j < _ref7 : _j > _ref7; idx = 0 <= _ref7 ? ++_j : --_j) { c = o.field.charAt(idx); if ('?' === c) { fstr += this._formatValue(this._values[pIndex++]); @@ -1548,11 +1603,11 @@ OTHER DEALINGS IN THE SOFTWARE. }; JoinBlock.prototype.buildStr = function(queryBuilder) { - var j, joins, _i, _len, _ref5; + var j, joins, _i, _len, _ref6; joins = ""; - _ref5 = this.joins || []; - for (_i = 0, _len = _ref5.length; _i < _len; _i++) { - j = _ref5[_i]; + _ref6 = this.joins || []; + for (_i = 0, _len = _ref6.length; _i < _len; _i++) { + j = _ref6[_i]; if (joins !== "") { joins += " "; } @@ -1573,7 +1628,7 @@ OTHER DEALINGS IN THE SOFTWARE. }; JoinBlock.prototype.buildParam = function(queryBuilder) { - var blk, cp, joinStr, p, params, ret, v, _i, _j, _k, _len, _len1, _len2, _ref5, _ref6; + var blk, cp, joinStr, p, params, ret, v, _i, _j, _k, _len, _len1, _len2, _ref6, _ref7; ret = { text: "", values: [] @@ -1583,9 +1638,9 @@ OTHER DEALINGS IN THE SOFTWARE. if (0 >= this.joins.length) { return ret; } - _ref5 = this.joins; - for (_i = 0, _len = _ref5.length; _i < _len; _i++) { - blk = _ref5[_i]; + _ref6 = this.joins; + for (_i = 0, _len = _ref6.length; _i < _len; _i++) { + blk = _ref6[_i]; if ("string" === typeof blk.table) { p = { "text": "" + blk.table, @@ -1629,9 +1684,9 @@ OTHER DEALINGS IN THE SOFTWARE. if (p.condition) { joinStr += " ON (" + p.condition + ")"; } - _ref6 = p.values; - for (_k = 0, _len2 = _ref6.length; _k < _len2; _k++) { - v = _ref6[_k]; + _ref7 = p.values; + for (_k = 0, _len2 = _ref7.length; _k < _len2; _k++) { + v = _ref7[_k]; ret.values.push(this._formatCustomValue(v)); } } @@ -1668,11 +1723,11 @@ OTHER DEALINGS IN THE SOFTWARE. }; UnionBlock.prototype.buildStr = function(queryBuilder) { - var j, unionStr, _i, _len, _ref5; + var j, unionStr, _i, _len, _ref6; unionStr = ""; - _ref5 = this.unions || []; - for (_i = 0, _len = _ref5.length; _i < _len; _i++) { - j = _ref5[_i]; + _ref6 = this.unions || []; + for (_i = 0, _len = _ref6.length; _i < _len; _i++) { + j = _ref6[_i]; if (unionStr !== "") { unionStr += " "; } @@ -1687,7 +1742,7 @@ OTHER DEALINGS IN THE SOFTWARE. }; UnionBlock.prototype.buildParam = function(queryBuilder) { - var blk, p, params, ret, unionStr, v, _i, _j, _k, _len, _len1, _len2, _ref5, _ref6; + var blk, p, params, ret, unionStr, v, _i, _j, _k, _len, _len1, _len2, _ref6, _ref7; ret = { text: "", values: [] @@ -1697,9 +1752,9 @@ OTHER DEALINGS IN THE SOFTWARE. if (0 >= this.unions.length) { return ret; } - _ref5 = this.unions || []; - for (_i = 0, _len = _ref5.length; _i < _len; _i++) { - blk = _ref5[_i]; + _ref6 = this.unions || []; + for (_i = 0, _len = _ref6.length; _i < _len; _i++) { + blk = _ref6[_i]; if ("string" === typeof blk.table) { p = { "text": "" + blk.table, @@ -1725,9 +1780,9 @@ OTHER DEALINGS IN THE SOFTWARE. unionStr += " "; } unionStr += "" + p.type + " (" + p.text + ")"; - _ref6 = p.values; - for (_k = 0, _len2 = _ref6.length; _k < _len2; _k++) { - v = _ref6[_k]; + _ref7 = p.values; + for (_k = 0, _len2 = _ref7.length; _k < _len2; _k++) { + v = _ref7[_k]; ret.values.push(this._formatCustomValue(v)); } } @@ -1743,22 +1798,22 @@ OTHER DEALINGS IN THE SOFTWARE. __extends(QueryBuilder, _super); function QueryBuilder(options, blocks) { - var block, methodBody, methodName, _fn, _i, _len, _ref5, _ref6, + var block, methodBody, methodName, _fn, _i, _len, _ref6, _ref7, _this = this; QueryBuilder.__super__.constructor.call(this, options); this.blocks = blocks || []; - _ref5 = this.blocks; - for (_i = 0, _len = _ref5.length; _i < _len; _i++) { - block = _ref5[_i]; - _ref6 = block.exposedMethods(); + _ref6 = this.blocks; + for (_i = 0, _len = _ref6.length; _i < _len; _i++) { + block = _ref6[_i]; + _ref7 = block.exposedMethods(); _fn = function(block, name, body) { return _this[name] = function() { body.apply(block, arguments); return _this; }; }; - for (methodName in _ref6) { - methodBody = _ref6[methodName]; + for (methodName in _ref7) { + methodBody = _ref7[methodName]; if (this[methodName] != null) { throw new Error("" + (this._getObjectClassName(this)) + " already has a builder method called: " + methodName); } @@ -1768,10 +1823,10 @@ OTHER DEALINGS IN THE SOFTWARE. } QueryBuilder.prototype.registerValueHandler = function(type, handler) { - var block, _i, _len, _ref5; - _ref5 = this.blocks; - for (_i = 0, _len = _ref5.length; _i < _len; _i++) { - block = _ref5[_i]; + var block, _i, _len, _ref6; + _ref6 = this.blocks; + for (_i = 0, _len = _ref6.length; _i < _len; _i++) { + block = _ref6[_i]; block.registerValueHandler(type, handler); } QueryBuilder.__super__.registerValueHandler.call(this, type, handler); @@ -1779,12 +1834,12 @@ OTHER DEALINGS IN THE SOFTWARE. }; QueryBuilder.prototype.updateOptions = function(options) { - var block, _i, _len, _ref5, _results; + var block, _i, _len, _ref6, _results; this.options = _extend({}, this.options, options); - _ref5 = this.blocks; + _ref6 = this.blocks; _results = []; - for (_i = 0, _len = _ref5.length; _i < _len; _i++) { - block = _ref5[_i]; + for (_i = 0, _len = _ref6.length; _i < _len; _i++) { + block = _ref6[_i]; _results.push(block.options = _extend({}, block.options, options)); } return _results; @@ -1793,11 +1848,11 @@ OTHER DEALINGS IN THE SOFTWARE. QueryBuilder.prototype.toString = function() { var block; return ((function() { - var _i, _len, _ref5, _results; - _ref5 = this.blocks; + var _i, _len, _ref6, _results; + _ref6 = this.blocks; _results = []; - for (_i = 0, _len = _ref5.length; _i < _len; _i++) { - block = _ref5[_i]; + for (_i = 0, _len = _ref6.length; _i < _len; _i++) { + block = _ref6[_i]; _results.push(block.buildStr(this)); } return _results; @@ -1807,7 +1862,7 @@ OTHER DEALINGS IN THE SOFTWARE. }; QueryBuilder.prototype.toParam = function(options) { - var block, blocks, i, old, result, _ref5; + var block, blocks, i, old, result, _ref6; if (options == null) { options = void 0; } @@ -1820,11 +1875,11 @@ OTHER DEALINGS IN THE SOFTWARE. values: [] }; blocks = (function() { - var _i, _len, _ref5, _results; - _ref5 = this.blocks; + var _i, _len, _ref6, _results; + _ref6 = this.blocks; _results = []; - for (_i = 0, _len = _ref5.length; _i < _len; _i++) { - block = _ref5[_i]; + for (_i = 0, _len = _ref6.length; _i < _len; _i++) { + block = _ref6[_i]; _results.push(block.buildParam(this)); } return _results; @@ -1840,7 +1895,7 @@ OTHER DEALINGS IN THE SOFTWARE. })()).filter(function(v) { return 0 < v.length; }).join(this.options.separator); - result.values = (_ref5 = []).concat.apply(_ref5, (function() { + result.values = (_ref6 = []).concat.apply(_ref6, (function() { var _i, _len, _results; _results = []; for (_i = 0, _len = blocks.length; _i < _len; _i++) { @@ -1867,11 +1922,11 @@ OTHER DEALINGS IN THE SOFTWARE. QueryBuilder.prototype.clone = function() { var block; return new this.constructor(this.options, (function() { - var _i, _len, _ref5, _results; - _ref5 = this.blocks; + var _i, _len, _ref6, _results; + _ref6 = this.blocks; _results = []; - for (_i = 0, _len = _ref5.length; _i < _len; _i++) { - block = _ref5[_i]; + for (_i = 0, _len = _ref6.length; _i < _len; _i++) { + block = _ref6[_i]; _results.push(block.clone()); } return _results; diff --git a/squel-basic.min.js b/squel-basic.min.js index 5b5024ee..7bf3d815 100644 --- a/squel-basic.min.js +++ b/squel-basic.min.js @@ -1 +1 @@ -/*! squel | https://github.com/hiddentao/squel | BSD license */(function(){var a,b,c,d,e,f,g,h,i,j,k,l=[].slice,m={}.hasOwnProperty,n=function(a,b){function c(){this.constructor=a}for(var d in b)m.call(b,d)&&(a[d]=b[d]);return c.prototype=b.prototype,a.prototype=new c,a.__super__=b.prototype,a};a={},e=function(){var a,b,c,d,e,f,g;if(a=arguments[0],c=2<=arguments.length?l.call(arguments,1):[])for(f=0,g=c.length;g>f;f++)if(d=c[f])for(b in d)m.call(d,b)&&(e=d[b],a[b]=e);return a},k=function(){var a,b,c,d,f,g;for(b=arguments[0],d=2<=arguments.length?l.call(arguments,1):[],a=e({},b),f=0,g=d.length;g>f;f++)c=d[f],delete a[c];return a},a.DefaultQueryBuilderOptions={autoQuoteTableNames:!1,autoQuoteFieldNames:!1,autoQuoteAliasNames:!0,nameQuoteCharacter:"`",tableAliasQuoteCharacter:"`",fieldAliasQuoteCharacter:'"',valueHandlers:[],numberedParameters:!1,numberedParametersStartAt:1,replaceSingleQuotes:!1,singleQuoteReplacement:"''",separator:" "},a.globalValueHandlers=[],c=function(a,b,c){var d,e,f;if("function"!=typeof b&&"string"!=typeof b)throw new Error("type must be a class constructor or string denoting 'typeof' result");if("function"!=typeof c)throw new Error("handler must be a function");for(e=0,f=a.length;f>e;e++)if(d=a[e],d.type===b)return void(d.handler=c);return a.push({type:b,handler:c})},b=function(){var a,b,c,d,e,f,g,h;for(d=arguments[0],a=2<=arguments.length?l.call(arguments,1):[],e=0,g=a.length;g>e;e++)for(b=a[e],f=0,h=b.length;h>f;f++)if(c=b[f],c.type===typeof d||"string"!=typeof c.type&&d instanceof c.type)return c.handler;return void 0},a.registerValueHandler=function(b,d){return c(a.globalValueHandlers,b,d)},a.Cloneable=function(){function a(){}return a.prototype.clone=function(){var a;return a=new this.constructor,e(a,JSON.parse(JSON.stringify(this)))},a}(),a.BaseBuilder=function(d){function f(b){var c;c=JSON.parse(JSON.stringify(a.DefaultQueryBuilderOptions)),this.options=e({},c,b)}return n(f,d),f.prototype.registerValueHandler=function(a,b){return c(this.options.valueHandlers,a,b),this},f.prototype._getObjectClassName=function(a){var b;return a&&a.constructor&&a.constructor.toString&&(b=a.constructor.toString().match(/function\s*(\w+)/),b&&2===b.length)?b[1]:void 0},f.prototype._sanitizeCondition=function(b){if(!(b instanceof a.Expression)&&"string"!=typeof b)throw new Error("condition must be a string or Expression instance");return b},f.prototype._sanitizeName=function(a,b){if("string"!=typeof a)throw new Error(""+b+" must be a string");return a},f.prototype._sanitizeField=function(b,c){var d;return null==c&&(c={}),b instanceof a.QueryBuilder?b="("+b+")":(b=this._sanitizeName(b,"field name"),this.options.autoQuoteFieldNames&&(d=this.options.nameQuoteCharacter,b=c.ignorePeriodsForFieldNameQuotes?""+d+b+d:b.split(".").map(function(a){return"*"===a?a:""+d+a+d}).join("."))),b},f.prototype._sanitizeTable=function(b,c){var d;if(null==c&&(c=!1),c){if("string"!=typeof b){if(b instanceof a.QueryBuilder&&b.isNestable())return b;throw new Error("table name must be a string or a nestable query instance")}d=b}else d=this._sanitizeName(b,"table name");return this.options.autoQuoteTableNames?""+this.options.nameQuoteCharacter+d+this.options.nameQuoteCharacter:d},f.prototype._sanitizeTableAlias=function(a){var b;return b=this._sanitizeName(a,"table alias"),this.options.autoQuoteAliasNames?""+this.options.tableAliasQuoteCharacter+b+this.options.tableAliasQuoteCharacter:b},f.prototype._sanitizeFieldAlias=function(a){var b;return b=this._sanitizeName(a,"field alias"),this.options.autoQuoteAliasNames?""+this.options.fieldAliasQuoteCharacter+b+this.options.fieldAliasQuoteCharacter:b},f.prototype._sanitizeLimitOffset=function(a){if(a=parseInt(a),0>a||isNaN(a))throw new Error("limit/offset must be >= 0");return a},f.prototype._sanitizeValue=function(c){var d,e;if(d=typeof c,null===c);else if("string"===d||"number"===d||"boolean"===d);else if(c instanceof a.QueryBuilder&&c.isNestable());else if(e=void 0!==b(c,this.options.valueHandlers,a.globalValueHandlers),!e)throw new Error("field value must be a string, number, boolean, null or one of the registered custom value types");return c},f.prototype._escapeValue=function(a){return!0!==this.options.replaceSingleQuotes?a:a.replace(/\'/g,this.options.singleQuoteReplacement)},f.prototype._formatCustomValue=function(c){var d;return d=b(c,this.options.valueHandlers,a.globalValueHandlers),d&&(c=d(c)),c},f.prototype._formatValueAsParam=function(b){var c,d=this;return Array.isArray(b)?b.map(function(a){return d._formatValueAsParam(a)}):b instanceof a.QueryBuilder&&b.isNestable()?(b.updateOptions({nestedBuilder:!0}),c=b.toParam()):b instanceof a.Expression?c=b.toParam():this._formatCustomValue(b)},f.prototype._formatValue=function(b,c){var d=this;return null==c&&(c={}),b=this._formatCustomValue(b),Array.isArray(b)?(b=b.map(function(a){return d._formatValue(a)}),b="("+b.join(", ")+")"):null===b?b="NULL":"boolean"==typeof b?b=b?"TRUE":"FALSE":b instanceof a.QueryBuilder?b="("+b+")":b instanceof a.Expression?b="("+b+")":"number"!=typeof b&&(b=this._escapeValue(b),b=c.dontQuote?""+b:"'"+b+"'"),b},f}(a.Cloneable),a.Expression=function(a){function b(){var a=this;b.__super__.constructor.call(this),this.tree={parent:null,nodes:[]},this.current=this.tree,this._begin=function(b){var c;return c={type:b,parent:a.current,nodes:[]},a.current.nodes.push(c),a.current=a.current.nodes[a.current.nodes.length-1],a}}return n(b,a),b.prototype.tree=null,b.prototype.current=null,b.prototype.and_begin=function(){return this._begin("AND")},b.prototype.or_begin=function(){return this._begin("OR")},b.prototype.end=function(){if(!this.current.parent)throw new Error("begin() needs to be called");return this.current=this.current.parent,this},b.prototype.and=function(a,b){if(!a||"string"!=typeof a)throw new Error("expr must be a string");return this.current.nodes.push({type:"AND",expr:a,para:b}),this},b.prototype.or=function(a,b){if(!a||"string"!=typeof a)throw new Error("expr must be a string");return this.current.nodes.push({type:"OR",expr:a,para:b}),this},b.prototype.toString=function(){if(null!==this.current.parent)throw new Error("end() needs to be called");return this._toString(this.tree)},b.prototype.toParam=function(){if(null!==this.current.parent)throw new Error("end() needs to be called");return this._toString(this.tree,!0)},b.prototype._toString=function(a,b){var c,d,e,f,g,h,i,j,k;for(null==b&&(b=!1),h="",g=[],k=a.nodes,i=0,j=k.length;j>i;i++)c=k[i],null!=c.expr?(f=c.expr,void 0!==c.para&&(b?(d=this._formatValueAsParam(c.para),null!=(null!=d?d.text:void 0)?(g=g.concat(d.values),f=f.replace("?","("+d.text+")")):g=g.concat(d),Array.isArray(c.para)&&(e=Array.apply(null,new Array(c.para.length)).map(function(){return"?"}),f=f.replace("?","("+e.join(", ")+")"))):f=f.replace("?",this._formatValue(c.para)))):(f=this._toString(c,b),b&&(g=g.concat(f.values),f=f.text),""!==f&&(f="("+f+")")),""!==f&&(""!==h&&(h+=" "+c.type+" "),h+=f);return b?{text:h,values:g}:h},b.prototype.clone=function(){var a,b;return a=new this.constructor,(b=function(c){var d,e,f,g,h;for(g=c.nodes,h=[],e=0,f=g.length;f>e;e++)d=g[e],null!=d.expr?h.push(a.current.nodes.push(JSON.parse(JSON.stringify(d)))):(a._begin(d.type),b(d),h.push(!this.current===d?a.end():void 0));return h})(this.tree),a},b}(a.BaseBuilder),a.Block=function(b){function c(){return f=c.__super__.constructor.apply(this,arguments)}return n(c,b),c.prototype.exposedMethods=function(){var b,c,d;c={};for(b in this)d=this[b],"function"!=typeof d||"_"===b.charAt(0)||a.Block.prototype[b]||(c[b]=d);return c},c.prototype.buildStr=function(){return""},c.prototype.buildParam=function(a){return{text:this.buildStr(a),values:[]}},c}(a.BaseBuilder),a.StringBlock=function(a){function b(a,c){b.__super__.constructor.call(this,a),this.str=c}return n(b,a),b.prototype.buildStr=function(){return this.str},b}(a.Block),a.AbstractValueBlock=function(a){function b(a){b.__super__.constructor.call(this,a),this._val=null}return n(b,a),b.prototype._setValue=function(a){return this._val=a},b.prototype.buildStr=function(){return this._val?this._val:""},b}(a.Block),a.AbstractTableBlock=function(b){function c(a){c.__super__.constructor.call(this,a),this.tables=[]}return n(c,b),c.prototype._table=function(a,b){return null==b&&(b=null),b&&(b=this._sanitizeTableAlias(b)),a=this._sanitizeTable(a,this.options.allowNested||!1),this.options.singleTable&&(this.tables=[]),this.tables.push({table:a,alias:b})},c.prototype.buildStr=function(){var a,b,c,d,e;if(0>=this.tables.length)throw new Error("_table() needs to be called");for(b="",e=this.tables,c=0,d=e.length;d>c;c++)a=e[c],""!==b&&(b+=", "),b+="string"==typeof a.table?a.table:"("+a.table+")",a.alias&&(b+=" "+a.alias);return b},c.prototype._buildParam=function(b,c){var d,e,f,g,h,i,j,k,l,m,n,o,p,q;if(null==c&&(c=null),h={text:"",values:[]},g=[],f="",0>=this.tables.length)return h;for(p=this.tables,j=0,m=p.length;m>j;j++)d=p[j],"string"==typeof d.table?e={text:""+d.table,values:[]}:d.table instanceof a.QueryBuilder?(d.table.updateOptions({nestedBuilder:!0}),e=d.table.toParam()):(d.updateOptions({nestedBuilder:!0}),e=d.buildParam(b)),e.table=d,g.push(e);for(k=0,n=g.length;n>k;k++)for(e=g[k],""!==f?f+=", ":null!=c&&""!==c&&(f+=""+c+" "+f),f+="string"==typeof e.table.table?""+e.text:"("+e.text+")",null!=e.table.alias&&(f+=" "+e.table.alias),q=e.values,l=0,o=q.length;o>l;l++)i=q[l],h.values.push(this._formatCustomValue(i));return h.text+=f,h},c.prototype.buildParam=function(a){return this._buildParam(a)},c}(a.Block),a.UpdateTableBlock=function(a){function b(){return g=b.__super__.constructor.apply(this,arguments)}return n(b,a),b.prototype.table=function(a,b){return null==b&&(b=null),this._table(a,b)},b}(a.AbstractTableBlock),a.FromTableBlock=function(a){function b(){return h=b.__super__.constructor.apply(this,arguments)}return n(b,a),b.prototype.from=function(a,b){return null==b&&(b=null),this._table(a,b)},b.prototype.buildStr=function(a){var c;if(0>=this.tables.length)throw new Error("from() needs to be called");return c=b.__super__.buildStr.call(this,a),"FROM "+c},b.prototype.buildParam=function(a){if(0>=this.tables.length)throw new Error("from() needs to be called");return this._buildParam(a,"FROM")},b}(a.AbstractTableBlock),a.IntoTableBlock=function(a){function b(a){b.__super__.constructor.call(this,a),this.table=null}return n(b,a),b.prototype.into=function(a){return this.table=this._sanitizeTable(a,!1)},b.prototype.buildStr=function(){if(!this.table)throw new Error("into() needs to be called");return"INTO "+this.table},b}(a.Block),a.GetFieldBlock=function(a){function b(a){b.__super__.constructor.call(this,a),this._fieldAliases={},this._fields=[]}return n(b,a),b.prototype.fields=function(a,b){var c,d,e,f,g,h;if(null==b&&(b={}),Array.isArray(a)){for(g=[],e=0,f=a.length;f>e;e++)d=a[e],g.push(this.field(d,null,b));return g}h=[];for(d in a)c=a[d],h.push(this.field(d,c,b));return h},b.prototype.field=function(a,b,c){return null==b&&(b=null),null==c&&(c={}),a=this._sanitizeField(a,c),b&&(b=this._sanitizeFieldAlias(b)),this._fieldAliases[a]!==b?(this._fieldAliases[a]=b,this._fields.push({name:a,alias:b})):void 0},b.prototype.buildStr=function(){var a,b,c,d,e;for(b="",e=this._fields,c=0,d=e.length;d>c;c++)a=e[c],""!==b&&(b+=", "),b+=a.name,a.alias&&(b+=" AS "+a.alias);return""===b?"*":b},b}(a.Block),a.AbstractSetFieldBlock=function(a){function b(a){b.__super__.constructor.call(this,a),this.fieldOptions=[],this.fields=[],this.values=[]}return n(b,a),b.prototype._set=function(a,b,c){var d;if(null==c&&(c={}),this.values.length>1)throw new Error("Cannot call set or setFields on multiple rows of fields.");return void 0!==b&&(b=this._sanitizeValue(b)),d=this.fields.indexOf(this._sanitizeField(a,c)),-1!==d?(this.values[0][d]=b,this.fieldOptions[0][d]=c):(this.fields.push(this._sanitizeField(a,c)),d=this.fields.length-1,Array.isArray(this.values[0])?(this.values[0][d]=b,this.fieldOptions[0][d]=c):(this.values.push([b]),this.fieldOptions.push([c]))),this},b.prototype._setFields=function(a,b){var c;if(null==b&&(b={}),"object"!=typeof a)throw new Error("Expected an object but got "+typeof a);for(c in a)m.call(a,c)&&this._set(c,a[c],b);return this},b.prototype._setFieldsRows=function(a,b){var c,d,e,f,g,h,i;if(null==b&&(b={}),!Array.isArray(a))throw new Error("Expected an array of objects but got "+typeof a);for(this.fields=[],this.values=[],d=g=0,h=a.length;h>=0?h>g:g>h;d=h>=0?++g:--g){i=a[d];for(c in i)if(m.call(i,c)){if(e=this.fields.indexOf(this._sanitizeField(c,b)),d>0&&-1===e)throw new Error("All fields in subsequent rows must match the fields in the first row");-1===e&&(this.fields.push(this._sanitizeField(c,b)),e=this.fields.length-1),f=this._sanitizeValue(a[d][c]),Array.isArray(this.values[d])?(this.values[d][e]=f,this.fieldOptions[d][e]=b):(this.values[d]=[f],this.fieldOptions[d]=[b])}}return this},b.prototype.buildStr=function(){throw new Error("Not yet implemented")},b.prototype.buildParam=function(){throw new Error("Not yet implemented")},b}(a.Block),a.SetFieldBlock=function(a){function b(){return i=b.__super__.constructor.apply(this,arguments)}return n(b,a),b.prototype.set=function(a,b,c){return this._set(a,b,c)},b.prototype.setFields=function(a,b){return this._setFields(a,b)},b.prototype.buildStr=function(){var a,b,c,d,e,f,g;if(0>=this.fields.length)throw new Error("set() needs to be called");for(d="",c=f=0,g=this.fields.length;g>=0?g>f:f>g;c=g>=0?++f:--f)a=this.fields[c],""!==d&&(d+=", "),e=this.values[0][c],b=this.fieldOptions[0][c],d+="undefined"==typeof e?a:""+a+" = "+this._formatValue(e,b);return"SET "+d},b.prototype.buildParam=function(){var a,b,c,d,e,f,g,h,i,j,k,l;if(0>=this.fields.length)throw new Error("set() needs to be called");for(d="",f=[],b=h=0,k=this.fields.length;k>=0?k>h:h>k;b=k>=0?++h:--h)if(a=this.fields[b],""!==d&&(d+=", "),g=this.values[0][b],"undefined"==typeof g)d+=a;else if(c=this._formatValueAsParam(g),null!=(null!=c?c.text:void 0))for(d+=""+a+" = ("+c.text+")",l=c.values,i=0,j=l.length;j>i;i++)e=l[i],f.push(e);else d+=""+a+" = ?",f.push(c);return{text:"SET "+d,values:f}},b}(a.AbstractSetFieldBlock),a.InsertFieldValueBlock=function(a){function b(){return j=b.__super__.constructor.apply(this,arguments)}return n(b,a),b.prototype.set=function(a,b,c){return null==c&&(c={}),this._set(a,b,c)},b.prototype.setFields=function(a,b){return this._setFields(a,b)},b.prototype.setFieldsRows=function(a,b){return this._setFieldsRows(a,b)},b.prototype._buildVals=function(){var a,b,c,d,e,f,g,h;for(d=[],b=e=0,g=this.values.length;g>=0?g>e:e>g;b=g>=0?++e:--e)for(c=f=0,h=this.values[b].length;h>=0?h>f:f>h;c=h>=0?++f:--f)a=this._formatValue(this.values[b][c],this.fieldOptions[b][c]),"string"==typeof d[b]?d[b]+=", "+a:d[b]=""+a;return d},b.prototype._buildValParams=function(){var a,b,c,d,e,f,g,h,i,j,k,l,m,n;for(g=[],d=[],a=h=0,l=this.values.length;l>=0?l>h:h>l;a=l>=0?++h:--h)for(b=i=0,m=this.values[a].length;m>=0?m>i:i>m;b=m>=0?++i:--i){if(c=this._formatValueAsParam(this.values[a][b]),null!=(null!=c?c.text:void 0))for(e=c.text,n=c.values,j=0,k=n.length;k>j;j++)f=n[j],d.push(f);else e="?",d.push(c);"string"==typeof g[a]?g[a]+=", "+e:g[a]=""+e}return{vals:g,params:d}},b.prototype.buildStr=function(){if(0>=this.fields.length)throw new Error("set() needs to be called");return"("+this.fields.join(", ")+") VALUES ("+this._buildVals().join("), (")+")"},b.prototype.buildParam=function(){var a,b,c,d,e,f,g;if(0>=this.fields.length)throw new Error("set() needs to be called");for(c="",f=this._buildValParams(),d=f.vals,b=f.params,a=e=0,g=this.fields.length;g>=0?g>e:e>g;a=g>=0?++e:--e)""!==c&&(c+=", "),c+=this.fields[a];return{text:"("+c+") VALUES ("+d.join("), (")+")",values:b}},b}(a.AbstractSetFieldBlock),a.DistinctBlock=function(a){function b(a){b.__super__.constructor.call(this,a),this.useDistinct=!1}return n(b,a),b.prototype.distinct=function(){return this.useDistinct=!0},b.prototype.buildStr=function(){return this.useDistinct?"DISTINCT":""},b}(a.Block),a.GroupByBlock=function(a){function b(a){b.__super__.constructor.call(this,a),this.groups=[]}return n(b,a),b.prototype.group=function(a){return a=this._sanitizeField(a),this.groups.push(a)},b.prototype.buildStr=function(){var a,b,c,d,e;if(b="",0c;c++)a=e[c],""!==b&&(b+=", "),b+=a;b="GROUP BY "+b}return b},b}(a.Block),a.OffsetBlock=function(a){function b(a){b.__super__.constructor.call(this,a),this.offsets=null}return n(b,a),b.prototype.offset=function(a){return a=this._sanitizeLimitOffset(a),this.offsets=a},b.prototype.buildStr=function(){return this.offsets?"OFFSET "+this.offsets:""},b}(a.Block),a.WhereBlock=function(b){function c(a){c.__super__.constructor.call(this,a),this.wheres=[]}return n(c,b),c.prototype.where=function(){var b,c,d,e,f,g,h,i,j,k,m,n,o,p;if(c=arguments[0],k=2<=arguments.length?l.call(arguments,1):[],c=this._sanitizeCondition(c),d="",e=[],c instanceof a.Expression)j=c.toParam(),d=j.text,e=j.values;else for(f=m=0,p=c.length;p>=0?p>m:m>p;f=p>=0?++m:--m)if(b=c.charAt(f),"?"===b&&0n;n++)h=i[n],g.push(this._sanitizeValue(h));e=e.concat(g),d+="("+function(){var a,b,c;for(c=[],a=0,b=g.length;b>a;a++)h=g[a],c.push("?");return c}().join(", ")+")"}else d+="?",e.push(this._sanitizeValue(i));else d+=b;return""!==d?this.wheres.push({text:d,values:e}):void 0},c.prototype.buildStr=function(){var a,b,c,d,e,f,g,h,i,j;if(0>=this.wheres.length)return"";for(e="",i=this.wheres,f=0,h=i.length;h>f;f++)if(d=i[f],""!==e&&(e+=") AND ("),0=0?j>g:g>j;b=j>=0?++g:--g)a=d.text.charAt(b),e+="?"===a?this._formatValue(d.values[c++]):a;else e+=d.text;return"WHERE ("+e+")"},c.prototype.buildParam=function(){var a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q;if(d={text:"",values:[]},0>=this.wheres.length)return d;for(h="",o=this.wheres,i=0,l=o.length;l>i;i++){for(g=o[i],""!==h&&(h+=") AND ("),e=g.text.split("?"),a=0,p=g.values,j=0,m=p.length;m>j;j++){if(f=p[j],null!=e[a]&&(h+=""+e[a]),b=this._formatValueAsParam(f),null!=(null!=b?b.text:void 0))for(h+="("+b.text+")",q=b.values,k=0,n=q.length;n>k;k++)c=q[k],d.values.push(c);else h+="?",d.values.push(b);a+=1}null!=e[a]&&(h+=""+e[a])}return d.text="WHERE ("+h+")",d},c}(a.Block),a.OrderByBlock=function(a){function b(a){b.__super__.constructor.call(this,a),this.orders=[],this._values=[]}return n(b,a),b.prototype.order=function(){var a,b,c;return b=arguments[0],a=arguments[1],c=3<=arguments.length?l.call(arguments,2):[],null==a&&(a=!0),b=this._sanitizeField(b),this._values=c,this.orders.push({field:b,dir:a?!0:!1})},b.prototype._buildStr=function(a){var b,c,d,e,f,g,h,i,j,k,l;if(null==a&&(a=!1),0h;h++){if(e=k[h],""!==f&&(f+=", "),c="",a)c=e.field;else for(d=i=0,l=e.field.length;l>=0?l>i:i>l;d=l>=0?++i:--i)b=e.field.charAt(d),c+="?"===b?this._formatValue(this._values[g++]):b;f+=""+c+" "+(e.dir?"ASC":"DESC")}return"ORDER BY "+f}return""},b.prototype.buildStr=function(){return this._buildStr()},b.prototype.buildParam=function(){var a=this;return{text:this._buildStr(!0),values:this._values.map(function(b){return a._formatValueAsParam(b)})}},b}(a.Block),a.LimitBlock=function(a){function b(a){b.__super__.constructor.call(this,a),this.limits=null}return n(b,a),b.prototype.limit=function(a){return a=this._sanitizeLimitOffset(a),this.limits=a},b.prototype.buildStr=function(){return this.limits?"LIMIT "+this.limits:""},b}(a.Block),a.JoinBlock=function(b){function c(a){c.__super__.constructor.call(this,a),this.joins=[]}return n(c,b),c.prototype.join=function(a,b,c,d){return null==b&&(b=null),null==c&&(c=null),null==d&&(d="INNER"),a=this._sanitizeTable(a,!0),b&&(b=this._sanitizeTableAlias(b)),c&&(c=this._sanitizeCondition(c)),this.joins.push({type:d,table:a,alias:b,condition:c}),this},c.prototype.left_join=function(a,b,c){return null==b&&(b=null),null==c&&(c=null),this.join(a,b,c,"LEFT")},c.prototype.right_join=function(a,b,c){return null==b&&(b=null),null==c&&(c=null),this.join(a,b,c,"RIGHT")},c.prototype.outer_join=function(a,b,c){return null==b&&(b=null),null==c&&(c=null),this.join(a,b,c,"OUTER")},c.prototype.left_outer_join=function(a,b,c){return null==b&&(b=null),null==c&&(c=null),this.join(a,b,c,"LEFT OUTER")},c.prototype.full_join=function(a,b,c){return null==b&&(b=null),null==c&&(c=null),this.join(a,b,c,"FULL")},c.prototype.buildStr=function(){var a,b,c,d,e;for(b="",e=this.joins||[],c=0,d=e.length;d>c;c++)a=e[c],""!==b&&(b+=" "),b+=""+a.type+" JOIN ",b+="string"==typeof a.table?a.table:"("+a.table+")",a.alias&&(b+=" "+a.alias),a.condition&&(b+=" ON ("+a.condition+")");return b},c.prototype.buildParam=function(b){var c,d,e,f,g,h,i,j,k,l,m,n,o,p,q;if(h={text:"",values:[]},g=[],e="",0>=this.joins.length)return h;for(p=this.joins,j=0,m=p.length;m>j;j++)c=p[j],"string"==typeof c.table?f={text:""+c.table,values:[]}:c.table instanceof a.QueryBuilder?(c.table.updateOptions({nestedBuilder:!0}),f=c.table.toParam()):(c.updateOptions({nestedBuilder:!0}),f=c.buildParam(b)),c.condition instanceof a.Expression?(d=c.condition.toParam(),f.condition=d.text,f.values=f.values.concat(d.values)):f.condition=c.condition,f.join=c,g.push(f);for(k=0,n=g.length;n>k;k++)for(f=g[k],""!==e&&(e+=" "),e+=""+f.join.type+" JOIN ",e+="string"==typeof f.join.table?f.text:"("+f.text+")",f.join.alias&&(e+=" "+f.join.alias),f.condition&&(e+=" ON ("+f.condition+")"),q=f.values,l=0,o=q.length;o>l;l++)i=q[l],h.values.push(this._formatCustomValue(i));return h.text+=e,h},c}(a.Block),a.UnionBlock=function(b){function c(a){c.__super__.constructor.call(this,a),this.unions=[]}return n(c,b),c.prototype.union=function(a,b){return null==b&&(b="UNION"),a=this._sanitizeTable(a,!0),this.unions.push({type:b,table:a}),this},c.prototype.union_all=function(a){return this.union(a,"UNION ALL")},c.prototype.buildStr=function(){var a,b,c,d,e;for(b="",e=this.unions||[],c=0,d=e.length;d>c;c++)a=e[c],""!==b&&(b+=" "),b+=""+a.type+" ",b+="string"==typeof a.table?a.table:"("+a.table+")";return b},c.prototype.buildParam=function(b){var c,d,e,f,g,h,i,j,k,l,m,n,o,p;if(f={text:"",values:[]},e=[],g="",0>=this.unions.length)return f;for(o=this.unions||[],i=0,l=o.length;l>i;i++)c=o[i],"string"==typeof c.table?d={text:""+c.table,values:[]}:c.table instanceof a.QueryBuilder?(c.table.updateOptions({nestedBuilder:!0}),d=c.table.toParam()):(c.updateOptions({nestedBuilder:!0}),d=c.buildParam(b)),d.type=c.type,e.push(d);for(j=0,m=e.length;m>j;j++)for(d=e[j],""!==g&&(g+=" "),g+=""+d.type+" ("+d.text+")",p=d.values,k=0,n=p.length;n>k;k++)h=p[k],f.values.push(this._formatCustomValue(h));return f.text+=g,f},c}(a.Block),a.QueryBuilder=function(a){function b(a,c){var d,e,f,g,h,i,j,k,l=this;for(b.__super__.constructor.call(this,a),this.blocks=c||[],j=this.blocks,h=0,i=j.length;i>h;h++){d=j[h],k=d.exposedMethods(),g=function(a,b,c){return l[b]=function(){return c.apply(a,arguments),l}};for(f in k){if(e=k[f],null!=this[f])throw new Error(""+this._getObjectClassName(this)+" already has a builder method called: "+f);g(d,f,e)}}}return n(b,a),b.prototype.registerValueHandler=function(a,c){var d,e,f,g;for(g=this.blocks,e=0,f=g.length;f>e;e++)d=g[e],d.registerValueHandler(a,c);return b.__super__.registerValueHandler.call(this,a,c),this},b.prototype.updateOptions=function(a){var b,c,d,f,g;for(this.options=e({},this.options,a),f=this.blocks,g=[],c=0,d=f.length;d>c;c++)b=f[c],g.push(b.options=e({},b.options,a));return g},b.prototype.toString=function(){var a;return function(){var b,c,d,e;for(d=this.blocks,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.buildStr(this));return e}.call(this).filter(function(a){return 0a;a++)b=d[a],e.push(b.buildParam(this));return e}.call(this),g.text=function(){var a,d,e;for(e=[],a=0,d=c.length;d>a;a++)b=c[a],e.push(b.text);return e}().filter(function(a){return 0a;a++)b=c[a],e.push(b.values);return e}()),null==this.options.nestedBuilder&&(this.options.numberedParameters||null!=(null!=a?a.numberedParametersStartAt:void 0))&&(d=1,null!=this.options.numberedParametersStartAt&&(d=this.options.numberedParametersStartAt),g.text=g.text.replace(/\?/g,function(){return"$"+d++})),this.options=f,g},b.prototype.clone=function(){var a;return new this.constructor(this.options,function(){var b,c,d,e;for(d=this.blocks,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.clone());return e}.call(this))},b.prototype.isNestable=function(){return!1},b}(a.BaseBuilder),a.Select=function(b){function c(b,d){null==d&&(d=null),d||(d=[new a.StringBlock(b,"SELECT"),new a.DistinctBlock(b),new a.GetFieldBlock(b),new a.FromTableBlock(e({},b,{allowNested:!0})),new a.JoinBlock(e({},b,{allowNested:!0})),new a.WhereBlock(b),new a.GroupByBlock(b),new a.OrderByBlock(b),new a.LimitBlock(b),new a.OffsetBlock(b),new a.UnionBlock(e({},b,{allowNested:!0}))]),c.__super__.constructor.call(this,b,d)}return n(c,b),c.prototype.isNestable=function(){return!0},c}(a.QueryBuilder),a.Update=function(b){function c(b,d){null==d&&(d=null),d||(d=[new a.StringBlock(b,"UPDATE"),new a.UpdateTableBlock(b),new a.SetFieldBlock(b),new a.WhereBlock(b),new a.OrderByBlock(b),new a.LimitBlock(b)]),c.__super__.constructor.call(this,b,d)}return n(c,b),c}(a.QueryBuilder),a.Delete=function(b){function c(b,d){null==d&&(d=null),d||(d=[new a.StringBlock(b,"DELETE"),new a.FromTableBlock(e({},b,{singleTable:!0})),new a.JoinBlock(b),new a.WhereBlock(b),new a.OrderByBlock(b),new a.LimitBlock(b)]),c.__super__.constructor.call(this,b,d)}return n(c,b),c}(a.QueryBuilder),a.Insert=function(b){function c(b,d){null==d&&(d=null),d||(d=[new a.StringBlock(b,"INSERT"),new a.IntoTableBlock(b),new a.InsertFieldValueBlock(b)]),c.__super__.constructor.call(this,b,d)}return n(c,b),c}(a.QueryBuilder),d={VERSION:"3.10.0",expr:function(){return new a.Expression},select:function(b,c){return new a.Select(b,c)},update:function(b,c){return new a.Update(b,c)},insert:function(b,c){return new a.Insert(b,c)},"delete":function(b,c){return new a.Delete(b,c)},registerValueHandler:a.registerValueHandler},d.remove=d["delete"],d.cls=a,("undefined"!=typeof define&&null!==define?define.amd:void 0)?define(function(){return d}):("undefined"!=typeof module&&null!==module?module.exports:void 0)?module.exports=d:"undefined"!=typeof window&&null!==window&&(window.squel=d),d.flavours={},d.useFlavour=function(a){if(!(d.flavours[a]instanceof Function))throw new Error("Flavour not available: "+a);return d.flavours[a].call(null,d),d}}).call(this); \ No newline at end of file +/*! squel | https://github.com/hiddentao/squel | BSD license */(function(){var a,b,c,d,e,f,g,h,i,j,k,l,m=[].slice,n={}.hasOwnProperty,o=function(a,b){return function(){return a.apply(b,arguments)}},p=function(a,b){function c(){this.constructor=a}for(var d in b)n.call(b,d)&&(a[d]=b[d]);return c.prototype=b.prototype,a.prototype=new c,a.__super__=b.prototype,a};a={},e=function(){var a,b,c,d,e,f,g;if(a=arguments[0],c=2<=arguments.length?m.call(arguments,1):[])for(f=0,g=c.length;g>f;f++)if(d=c[f])for(b in d)n.call(d,b)&&(e=d[b],a[b]=e);return a},l=function(){var a,b,c,d,f,g;for(b=arguments[0],d=2<=arguments.length?m.call(arguments,1):[],a=e({},b),f=0,g=d.length;g>f;f++)c=d[f],delete a[c];return a},a.DefaultQueryBuilderOptions={autoQuoteTableNames:!1,autoQuoteFieldNames:!1,autoQuoteAliasNames:!0,nameQuoteCharacter:"`",tableAliasQuoteCharacter:"`",fieldAliasQuoteCharacter:'"',valueHandlers:[],numberedParameters:!1,numberedParametersStartAt:1,replaceSingleQuotes:!1,singleQuoteReplacement:"''",separator:" "},a.globalValueHandlers=[],c=function(a,b,c){var d,e,f;if("function"!=typeof b&&"string"!=typeof b)throw new Error("type must be a class constructor or string denoting 'typeof' result");if("function"!=typeof c)throw new Error("handler must be a function");for(e=0,f=a.length;f>e;e++)if(d=a[e],d.type===b)return void(d.handler=c);return a.push({type:b,handler:c})},b=function(){var a,b,c,d,e,f,g,h;for(d=arguments[0],a=2<=arguments.length?m.call(arguments,1):[],e=0,g=a.length;g>e;e++)for(b=a[e],f=0,h=b.length;h>f;f++)if(c=b[f],c.type===typeof d||"string"!=typeof c.type&&d instanceof c.type)return c.handler;return void 0},a.registerValueHandler=function(b,d){return c(a.globalValueHandlers,b,d)},a.Cloneable=function(){function a(){}return a.prototype.clone=function(){var a;return a=new this.constructor,e(a,JSON.parse(JSON.stringify(this)))},a}(),a.BaseBuilder=function(d){function f(b){this._sanitizeNestableQuery=o(this._sanitizeNestableQuery,this);var c;c=JSON.parse(JSON.stringify(a.DefaultQueryBuilderOptions)),this.options=e({},c,b)}return p(f,d),f.prototype.registerValueHandler=function(a,b){return c(this.options.valueHandlers,a,b),this},f.prototype._getObjectClassName=function(a){var b;return a&&a.constructor&&a.constructor.toString&&(b=a.constructor.toString().match(/function\s*(\w+)/),b&&2===b.length)?b[1]:void 0},f.prototype._sanitizeCondition=function(b){if(!(b instanceof a.Expression)&&"string"!=typeof b)throw new Error("condition must be a string or Expression instance");return b},f.prototype._sanitizeName=function(a,b){if("string"!=typeof a)throw new Error(""+b+" must be a string");return a},f.prototype._sanitizeField=function(b,c){var d;return null==c&&(c={}),b instanceof a.QueryBuilder?b="("+b+")":(b=this._sanitizeName(b,"field name"),this.options.autoQuoteFieldNames&&(d=this.options.nameQuoteCharacter,b=c.ignorePeriodsForFieldNameQuotes?""+d+b+d:b.split(".").map(function(a){return"*"===a?a:""+d+a+d}).join("."))),b},f.prototype._sanitizeNestableQuery=function(b){if(b instanceof a.QueryBuilder&&b.isNestable())return b;throw new Error("must be a nestable query, e.g. SELECT")},f.prototype._sanitizeTable=function(a,b){var c,d;if(null==b&&(b=!1),b)if("string"==typeof a)d=a;else try{d=this._sanitizeNestableQuery(a)}catch(e){throw c=e,new Error("table name must be a string or a nestable query instance")}else d=this._sanitizeName(a,"table name");return this.options.autoQuoteTableNames?""+this.options.nameQuoteCharacter+d+this.options.nameQuoteCharacter:d},f.prototype._sanitizeTableAlias=function(a){var b;return b=this._sanitizeName(a,"table alias"),this.options.autoQuoteAliasNames?""+this.options.tableAliasQuoteCharacter+b+this.options.tableAliasQuoteCharacter:b},f.prototype._sanitizeFieldAlias=function(a){var b;return b=this._sanitizeName(a,"field alias"),this.options.autoQuoteAliasNames?""+this.options.fieldAliasQuoteCharacter+b+this.options.fieldAliasQuoteCharacter:b},f.prototype._sanitizeLimitOffset=function(a){if(a=parseInt(a),0>a||isNaN(a))throw new Error("limit/offset must be >= 0");return a},f.prototype._sanitizeValue=function(c){var d,e;if(d=typeof c,null===c);else if("string"===d||"number"===d||"boolean"===d);else if(c instanceof a.QueryBuilder&&c.isNestable());else if(e=void 0!==b(c,this.options.valueHandlers,a.globalValueHandlers),!e)throw new Error("field value must be a string, number, boolean, null or one of the registered custom value types");return c},f.prototype._escapeValue=function(a){return!0!==this.options.replaceSingleQuotes?a:a.replace(/\'/g,this.options.singleQuoteReplacement)},f.prototype._formatCustomValue=function(c){var d;return d=b(c,this.options.valueHandlers,a.globalValueHandlers),d&&(c=d(c)),c},f.prototype._formatValueAsParam=function(b){var c,d=this;return Array.isArray(b)?b.map(function(a){return d._formatValueAsParam(a)}):b instanceof a.QueryBuilder&&b.isNestable()?(b.updateOptions({nestedBuilder:!0}),c=b.toParam()):b instanceof a.Expression?c=b.toParam():this._formatCustomValue(b)},f.prototype._formatValue=function(b,c){var d=this;return null==c&&(c={}),b=this._formatCustomValue(b),Array.isArray(b)?(b=b.map(function(a){return d._formatValue(a)}),b="("+b.join(", ")+")"):null===b?b="NULL":"boolean"==typeof b?b=b?"TRUE":"FALSE":b instanceof a.QueryBuilder?b="("+b+")":b instanceof a.Expression?b="("+b+")":"number"!=typeof b&&(b=this._escapeValue(b),b=c.dontQuote?""+b:"'"+b+"'"),b},f}(a.Cloneable),a.Expression=function(a){function b(){var a=this;b.__super__.constructor.call(this),this.tree={parent:null,nodes:[]},this.current=this.tree,this._begin=function(b){var c;return c={type:b,parent:a.current,nodes:[]},a.current.nodes.push(c),a.current=a.current.nodes[a.current.nodes.length-1],a}}return p(b,a),b.prototype.tree=null,b.prototype.current=null,b.prototype.and_begin=function(){return this._begin("AND")},b.prototype.or_begin=function(){return this._begin("OR")},b.prototype.end=function(){if(!this.current.parent)throw new Error("begin() needs to be called");return this.current=this.current.parent,this},b.prototype.and=function(a,b){if(!a||"string"!=typeof a)throw new Error("expr must be a string");return this.current.nodes.push({type:"AND",expr:a,para:b}),this},b.prototype.or=function(a,b){if(!a||"string"!=typeof a)throw new Error("expr must be a string");return this.current.nodes.push({type:"OR",expr:a,para:b}),this},b.prototype.toString=function(){if(null!==this.current.parent)throw new Error("end() needs to be called");return this._toString(this.tree)},b.prototype.toParam=function(){if(null!==this.current.parent)throw new Error("end() needs to be called");return this._toString(this.tree,!0)},b.prototype._toString=function(a,b){var c,d,e,f,g,h,i,j,k;for(null==b&&(b=!1),h="",g=[],k=a.nodes,i=0,j=k.length;j>i;i++)c=k[i],null!=c.expr?(f=c.expr,void 0!==c.para&&(b?(d=this._formatValueAsParam(c.para),null!=(null!=d?d.text:void 0)?(g=g.concat(d.values),f=f.replace("?","("+d.text+")")):g=g.concat(d),Array.isArray(c.para)&&(e=Array.apply(null,new Array(c.para.length)).map(function(){return"?"}),f=f.replace("?","("+e.join(", ")+")"))):f=f.replace("?",this._formatValue(c.para)))):(f=this._toString(c,b),b&&(g=g.concat(f.values),f=f.text),""!==f&&(f="("+f+")")),""!==f&&(""!==h&&(h+=" "+c.type+" "),h+=f);return b?{text:h,values:g}:h},b.prototype.clone=function(){var a,b;return a=new this.constructor,(b=function(c){var d,e,f,g,h;for(g=c.nodes,h=[],e=0,f=g.length;f>e;e++)d=g[e],null!=d.expr?h.push(a.current.nodes.push(JSON.parse(JSON.stringify(d)))):(a._begin(d.type),b(d),h.push(!this.current===d?a.end():void 0));return h})(this.tree),a},b}(a.BaseBuilder),a.Block=function(b){function c(){return f=c.__super__.constructor.apply(this,arguments)}return p(c,b),c.prototype.exposedMethods=function(){var b,c,d;c={};for(b in this)d=this[b],"function"!=typeof d||"_"===b.charAt(0)||a.Block.prototype[b]||(c[b]=d);return c},c.prototype.buildStr=function(){return""},c.prototype.buildParam=function(a){return{text:this.buildStr(a),values:[]}},c}(a.BaseBuilder),a.StringBlock=function(a){function b(a,c){b.__super__.constructor.call(this,a),this.str=c}return p(b,a),b.prototype.buildStr=function(){return this.str},b}(a.Block),a.AbstractValueBlock=function(a){function b(a){b.__super__.constructor.call(this,a),this._val=null}return p(b,a),b.prototype._setValue=function(a){return this._val=a},b.prototype.buildStr=function(){return this._val?this._val:""},b}(a.Block),a.AbstractTableBlock=function(b){function c(a){c.__super__.constructor.call(this,a),this.tables=[]}return p(c,b),c.prototype._table=function(a,b){return null==b&&(b=null),b&&(b=this._sanitizeTableAlias(b)),a=this._sanitizeTable(a,this.options.allowNested||!1),this.options.singleTable&&(this.tables=[]),this.tables.push({table:a,alias:b})},c.prototype.buildStr=function(){var a,b,c,d,e;if(0>=this.tables.length)throw new Error("_table() needs to be called");for(b="",e=this.tables,c=0,d=e.length;d>c;c++)a=e[c],""!==b&&(b+=", "),b+="string"==typeof a.table?a.table:"("+a.table+")",a.alias&&(b+=" "+a.alias);return b},c.prototype._buildParam=function(b,c){var d,e,f,g,h,i,j,k,l,m,n,o,p,q;if(null==c&&(c=null),h={text:"",values:[]},g=[],f="",0>=this.tables.length)return h;for(p=this.tables,j=0,m=p.length;m>j;j++)d=p[j],"string"==typeof d.table?e={text:""+d.table,values:[]}:d.table instanceof a.QueryBuilder?(d.table.updateOptions({nestedBuilder:!0}),e=d.table.toParam()):(d.updateOptions({nestedBuilder:!0}),e=d.buildParam(b)),e.table=d,g.push(e);for(k=0,n=g.length;n>k;k++)for(e=g[k],""!==f?f+=", ":null!=c&&""!==c&&(f+=""+c+" "+f),f+="string"==typeof e.table.table?""+e.text:"("+e.text+")",null!=e.table.alias&&(f+=" "+e.table.alias),q=e.values,l=0,o=q.length;o>l;l++)i=q[l],h.values.push(this._formatCustomValue(i));return h.text+=f,h},c.prototype.buildParam=function(a){return this._buildParam(a)},c}(a.Block),a.UpdateTableBlock=function(a){function b(){return g=b.__super__.constructor.apply(this,arguments)}return p(b,a),b.prototype.table=function(a,b){return null==b&&(b=null),this._table(a,b)},b}(a.AbstractTableBlock),a.FromTableBlock=function(a){function b(){return h=b.__super__.constructor.apply(this,arguments)}return p(b,a),b.prototype.from=function(a,b){return null==b&&(b=null),this._table(a,b)},b.prototype.buildStr=function(a){var c;if(0>=this.tables.length)throw new Error("from() needs to be called");return c=b.__super__.buildStr.call(this,a),"FROM "+c},b.prototype.buildParam=function(a){if(0>=this.tables.length)throw new Error("from() needs to be called");return this._buildParam(a,"FROM")},b}(a.AbstractTableBlock),a.IntoTableBlock=function(a){function b(a){b.__super__.constructor.call(this,a),this.table=null}return p(b,a),b.prototype.into=function(a){return this.table=this._sanitizeTable(a,!1)},b.prototype.buildStr=function(){if(!this.table)throw new Error("into() needs to be called");return"INTO "+this.table},b}(a.Block),a.GetFieldBlock=function(a){function b(a){b.__super__.constructor.call(this,a),this._fieldAliases={},this._fields=[]}return p(b,a),b.prototype.fields=function(a,b){var c,d,e,f,g,h;if(null==b&&(b={}),Array.isArray(a)){for(g=[],e=0,f=a.length;f>e;e++)d=a[e],g.push(this.field(d,null,b));return g}h=[];for(d in a)c=a[d],h.push(this.field(d,c,b));return h},b.prototype.field=function(a,b,c){return null==b&&(b=null),null==c&&(c={}),a=this._sanitizeField(a,c),b&&(b=this._sanitizeFieldAlias(b)),this._fieldAliases[a]!==b?(this._fieldAliases[a]=b,this._fields.push({name:a,alias:b})):void 0},b.prototype.buildStr=function(){var a,b,c,d,e;for(b="",e=this._fields,c=0,d=e.length;d>c;c++)a=e[c],""!==b&&(b+=", "),b+=a.name,a.alias&&(b+=" AS "+a.alias);return""===b?"*":b},b}(a.Block),a.AbstractSetFieldBlock=function(a){function b(a){b.__super__.constructor.call(this,a),this.fieldOptions=[],this.fields=[],this.values=[]}return p(b,a),b.prototype._set=function(a,b,c){var d;if(null==c&&(c={}),this.values.length>1)throw new Error("Cannot call set or setFields on multiple rows of fields.");return void 0!==b&&(b=this._sanitizeValue(b)),d=this.fields.indexOf(this._sanitizeField(a,c)),-1!==d?(this.values[0][d]=b,this.fieldOptions[0][d]=c):(this.fields.push(this._sanitizeField(a,c)),d=this.fields.length-1,Array.isArray(this.values[0])?(this.values[0][d]=b,this.fieldOptions[0][d]=c):(this.values.push([b]),this.fieldOptions.push([c]))),this},b.prototype._setFields=function(a,b){var c;if(null==b&&(b={}),"object"!=typeof a)throw new Error("Expected an object but got "+typeof a);for(c in a)n.call(a,c)&&this._set(c,a[c],b);return this},b.prototype._setFieldsRows=function(a,b){var c,d,e,f,g,h,i;if(null==b&&(b={}),!Array.isArray(a))throw new Error("Expected an array of objects but got "+typeof a);for(this.fields=[],this.values=[],d=g=0,h=a.length;h>=0?h>g:g>h;d=h>=0?++g:--g){i=a[d];for(c in i)if(n.call(i,c)){if(e=this.fields.indexOf(this._sanitizeField(c,b)),d>0&&-1===e)throw new Error("All fields in subsequent rows must match the fields in the first row");-1===e&&(this.fields.push(this._sanitizeField(c,b)),e=this.fields.length-1),f=this._sanitizeValue(a[d][c]),Array.isArray(this.values[d])?(this.values[d][e]=f,this.fieldOptions[d][e]=b):(this.values[d]=[f],this.fieldOptions[d]=[b])}}return this},b.prototype.buildStr=function(){throw new Error("Not yet implemented")},b.prototype.buildParam=function(){throw new Error("Not yet implemented")},b}(a.Block),a.SetFieldBlock=function(a){function b(){return i=b.__super__.constructor.apply(this,arguments)}return p(b,a),b.prototype.set=function(a,b,c){return this._set(a,b,c)},b.prototype.setFields=function(a,b){return this._setFields(a,b)},b.prototype.buildStr=function(){var a,b,c,d,e,f,g;if(0>=this.fields.length)throw new Error("set() needs to be called");for(d="",c=f=0,g=this.fields.length;g>=0?g>f:f>g;c=g>=0?++f:--f)a=this.fields[c],""!==d&&(d+=", "),e=this.values[0][c],b=this.fieldOptions[0][c],d+="undefined"==typeof e?a:""+a+" = "+this._formatValue(e,b);return"SET "+d},b.prototype.buildParam=function(){var a,b,c,d,e,f,g,h,i,j,k,l;if(0>=this.fields.length)throw new Error("set() needs to be called");for(d="",f=[],b=h=0,k=this.fields.length;k>=0?k>h:h>k;b=k>=0?++h:--h)if(a=this.fields[b],""!==d&&(d+=", "),g=this.values[0][b],"undefined"==typeof g)d+=a;else if(c=this._formatValueAsParam(g),null!=(null!=c?c.text:void 0))for(d+=""+a+" = ("+c.text+")",l=c.values,i=0,j=l.length;j>i;i++)e=l[i],f.push(e);else d+=""+a+" = ?",f.push(c);return{text:"SET "+d,values:f}},b}(a.AbstractSetFieldBlock),a.InsertFieldValueBlock=function(a){function b(){return j=b.__super__.constructor.apply(this,arguments)}return p(b,a),b.prototype.set=function(a,b,c){return null==c&&(c={}),this._set(a,b,c)},b.prototype.setFields=function(a,b){return this._setFields(a,b)},b.prototype.setFieldsRows=function(a,b){return this._setFieldsRows(a,b)},b.prototype._buildVals=function(){var a,b,c,d,e,f,g,h;for(d=[],b=e=0,g=this.values.length;g>=0?g>e:e>g;b=g>=0?++e:--e)for(c=f=0,h=this.values[b].length;h>=0?h>f:f>h;c=h>=0?++f:--f)a=this._formatValue(this.values[b][c],this.fieldOptions[b][c]),"string"==typeof d[b]?d[b]+=", "+a:d[b]=""+a;return d},b.prototype._buildValParams=function(){var a,b,c,d,e,f,g,h,i,j,k,l,m,n;for(g=[],d=[],a=h=0,l=this.values.length;l>=0?l>h:h>l;a=l>=0?++h:--h)for(b=i=0,m=this.values[a].length;m>=0?m>i:i>m;b=m>=0?++i:--i){if(c=this._formatValueAsParam(this.values[a][b]),null!=(null!=c?c.text:void 0))for(e=c.text,n=c.values,j=0,k=n.length;k>j;j++)f=n[j],d.push(f);else e="?",d.push(c);"string"==typeof g[a]?g[a]+=", "+e:g[a]=""+e}return{vals:g,params:d}},b.prototype.buildStr=function(){if(0>=this.fields.length)throw new Error("set() needs to be called");return"("+this.fields.join(", ")+") VALUES ("+this._buildVals().join("), (")+")"},b.prototype.buildParam=function(){var a,b,c,d,e,f,g;if(0>=this.fields.length)throw new Error("set() needs to be called");for(c="",f=this._buildValParams(),d=f.vals,b=f.params,a=e=0,g=this.fields.length;g>=0?g>e:e>g;a=g>=0?++e:--e)""!==c&&(c+=", "),c+=this.fields[a];return{text:"("+c+") VALUES ("+d.join("), (")+")",values:b}},b}(a.AbstractSetFieldBlock),a.InsertFieldSelectQueryBlock=function(a){function b(){return k=b.__super__.constructor.apply(this,arguments)}return p(b,a),b.prototype.fromSelect=function(a,b){return this._fields=a,this._query=this._sanitizeNestableQuery(b)},b.prototype.buildStr=function(){if(0>=this.fields.length)throw new Error("set() needs to be called");return"("+this.fields.join(", ")+") VALUES ("+this._buildVals().join("), (")+")"},b.prototype.buildParam=function(){var a,b,c,d,e,f,g;if(0>=this.fields.length)throw new Error("set() needs to be called");for(c="",f=this._buildValParams(),d=f.vals,b=f.params,a=e=0,g=this.fields.length;g>=0?g>e:e>g;a=g>=0?++e:--e)""!==c&&(c+=", "),c+=this.fields[a];return{text:"("+c+") VALUES ("+d.join("), (")+")",values:b}},b}(a.Block),a.DistinctBlock=function(a){function b(a){b.__super__.constructor.call(this,a),this.useDistinct=!1}return p(b,a),b.prototype.distinct=function(){return this.useDistinct=!0},b.prototype.buildStr=function(){return this.useDistinct?"DISTINCT":""},b}(a.Block),a.GroupByBlock=function(a){function b(a){b.__super__.constructor.call(this,a),this.groups=[]}return p(b,a),b.prototype.group=function(a){return a=this._sanitizeField(a),this.groups.push(a)},b.prototype.buildStr=function(){var a,b,c,d,e;if(b="",0c;c++)a=e[c],""!==b&&(b+=", "),b+=a;b="GROUP BY "+b}return b},b}(a.Block),a.OffsetBlock=function(a){function b(a){b.__super__.constructor.call(this,a),this.offsets=null}return p(b,a),b.prototype.offset=function(a){return a=this._sanitizeLimitOffset(a),this.offsets=a},b.prototype.buildStr=function(){return this.offsets?"OFFSET "+this.offsets:""},b}(a.Block),a.WhereBlock=function(b){function c(a){c.__super__.constructor.call(this,a),this.wheres=[]}return p(c,b),c.prototype.where=function(){var b,c,d,e,f,g,h,i,j,k,l,n,o,p;if(c=arguments[0],k=2<=arguments.length?m.call(arguments,1):[],c=this._sanitizeCondition(c),d="",e=[],c instanceof a.Expression)j=c.toParam(),d=j.text,e=j.values;else for(f=l=0,p=c.length;p>=0?p>l:l>p;f=p>=0?++l:--l)if(b=c.charAt(f),"?"===b&&0n;n++)h=i[n],g.push(this._sanitizeValue(h));e=e.concat(g),d+="("+function(){var a,b,c;for(c=[],a=0,b=g.length;b>a;a++)h=g[a],c.push("?");return c}().join(", ")+")"}else d+="?",e.push(this._sanitizeValue(i));else d+=b;return""!==d?this.wheres.push({text:d,values:e}):void 0},c.prototype.buildStr=function(){var a,b,c,d,e,f,g,h,i,j;if(0>=this.wheres.length)return"";for(e="",i=this.wheres,f=0,h=i.length;h>f;f++)if(d=i[f],""!==e&&(e+=") AND ("),0=0?j>g:g>j;b=j>=0?++g:--g)a=d.text.charAt(b),e+="?"===a?this._formatValue(d.values[c++]):a;else e+=d.text;return"WHERE ("+e+")"},c.prototype.buildParam=function(){var a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q;if(d={text:"",values:[]},0>=this.wheres.length)return d;for(h="",o=this.wheres,i=0,l=o.length;l>i;i++){for(g=o[i],""!==h&&(h+=") AND ("),e=g.text.split("?"),a=0,p=g.values,j=0,m=p.length;m>j;j++){if(f=p[j],null!=e[a]&&(h+=""+e[a]),b=this._formatValueAsParam(f),null!=(null!=b?b.text:void 0))for(h+="("+b.text+")",q=b.values,k=0,n=q.length;n>k;k++)c=q[k],d.values.push(c);else h+="?",d.values.push(b);a+=1}null!=e[a]&&(h+=""+e[a])}return d.text="WHERE ("+h+")",d},c}(a.Block),a.OrderByBlock=function(a){function b(a){b.__super__.constructor.call(this,a),this.orders=[],this._values=[]}return p(b,a),b.prototype.order=function(){var a,b,c;return b=arguments[0],a=arguments[1],c=3<=arguments.length?m.call(arguments,2):[],null==a&&(a=!0),b=this._sanitizeField(b),this._values=c,this.orders.push({field:b,dir:a?!0:!1})},b.prototype._buildStr=function(a){var b,c,d,e,f,g,h,i,j,k,l;if(null==a&&(a=!1),0h;h++){if(e=k[h],""!==f&&(f+=", "),c="",a)c=e.field;else for(d=i=0,l=e.field.length;l>=0?l>i:i>l;d=l>=0?++i:--i)b=e.field.charAt(d),c+="?"===b?this._formatValue(this._values[g++]):b;f+=""+c+" "+(e.dir?"ASC":"DESC")}return"ORDER BY "+f}return""},b.prototype.buildStr=function(){return this._buildStr()},b.prototype.buildParam=function(){var a=this;return{text:this._buildStr(!0),values:this._values.map(function(b){return a._formatValueAsParam(b)})}},b}(a.Block),a.LimitBlock=function(a){function b(a){b.__super__.constructor.call(this,a),this.limits=null}return p(b,a),b.prototype.limit=function(a){return a=this._sanitizeLimitOffset(a),this.limits=a},b.prototype.buildStr=function(){return this.limits?"LIMIT "+this.limits:""},b}(a.Block),a.JoinBlock=function(b){function c(a){c.__super__.constructor.call(this,a),this.joins=[]}return p(c,b),c.prototype.join=function(a,b,c,d){return null==b&&(b=null),null==c&&(c=null),null==d&&(d="INNER"),a=this._sanitizeTable(a,!0),b&&(b=this._sanitizeTableAlias(b)),c&&(c=this._sanitizeCondition(c)),this.joins.push({type:d,table:a,alias:b,condition:c}),this},c.prototype.left_join=function(a,b,c){return null==b&&(b=null),null==c&&(c=null),this.join(a,b,c,"LEFT")},c.prototype.right_join=function(a,b,c){return null==b&&(b=null),null==c&&(c=null),this.join(a,b,c,"RIGHT")},c.prototype.outer_join=function(a,b,c){return null==b&&(b=null),null==c&&(c=null),this.join(a,b,c,"OUTER")},c.prototype.left_outer_join=function(a,b,c){return null==b&&(b=null),null==c&&(c=null),this.join(a,b,c,"LEFT OUTER")},c.prototype.full_join=function(a,b,c){return null==b&&(b=null),null==c&&(c=null),this.join(a,b,c,"FULL")},c.prototype.buildStr=function(){var a,b,c,d,e;for(b="",e=this.joins||[],c=0,d=e.length;d>c;c++)a=e[c],""!==b&&(b+=" "),b+=""+a.type+" JOIN ",b+="string"==typeof a.table?a.table:"("+a.table+")",a.alias&&(b+=" "+a.alias),a.condition&&(b+=" ON ("+a.condition+")");return b},c.prototype.buildParam=function(b){var c,d,e,f,g,h,i,j,k,l,m,n,o,p,q;if(h={text:"",values:[]},g=[],e="",0>=this.joins.length)return h;for(p=this.joins,j=0,m=p.length;m>j;j++)c=p[j],"string"==typeof c.table?f={text:""+c.table,values:[]}:c.table instanceof a.QueryBuilder?(c.table.updateOptions({nestedBuilder:!0}),f=c.table.toParam()):(c.updateOptions({nestedBuilder:!0}),f=c.buildParam(b)),c.condition instanceof a.Expression?(d=c.condition.toParam(),f.condition=d.text,f.values=f.values.concat(d.values)):f.condition=c.condition,f.join=c,g.push(f);for(k=0,n=g.length;n>k;k++)for(f=g[k],""!==e&&(e+=" "),e+=""+f.join.type+" JOIN ",e+="string"==typeof f.join.table?f.text:"("+f.text+")",f.join.alias&&(e+=" "+f.join.alias),f.condition&&(e+=" ON ("+f.condition+")"),q=f.values,l=0,o=q.length;o>l;l++)i=q[l],h.values.push(this._formatCustomValue(i));return h.text+=e,h},c}(a.Block),a.UnionBlock=function(b){function c(a){c.__super__.constructor.call(this,a),this.unions=[]}return p(c,b),c.prototype.union=function(a,b){return null==b&&(b="UNION"),a=this._sanitizeTable(a,!0),this.unions.push({type:b,table:a}),this},c.prototype.union_all=function(a){return this.union(a,"UNION ALL")},c.prototype.buildStr=function(){var a,b,c,d,e;for(b="",e=this.unions||[],c=0,d=e.length;d>c;c++)a=e[c],""!==b&&(b+=" "),b+=""+a.type+" ",b+="string"==typeof a.table?a.table:"("+a.table+")";return b},c.prototype.buildParam=function(b){var c,d,e,f,g,h,i,j,k,l,m,n,o,p;if(f={text:"",values:[]},e=[],g="",0>=this.unions.length)return f;for(o=this.unions||[],i=0,l=o.length;l>i;i++)c=o[i],"string"==typeof c.table?d={text:""+c.table,values:[]}:c.table instanceof a.QueryBuilder?(c.table.updateOptions({nestedBuilder:!0}),d=c.table.toParam()):(c.updateOptions({nestedBuilder:!0}),d=c.buildParam(b)),d.type=c.type,e.push(d);for(j=0,m=e.length;m>j;j++)for(d=e[j],""!==g&&(g+=" "),g+=""+d.type+" ("+d.text+")",p=d.values,k=0,n=p.length;n>k;k++)h=p[k],f.values.push(this._formatCustomValue(h));return f.text+=g,f},c}(a.Block),a.QueryBuilder=function(a){function b(a,c){var d,e,f,g,h,i,j,k,l=this;for(b.__super__.constructor.call(this,a),this.blocks=c||[],j=this.blocks,h=0,i=j.length;i>h;h++){d=j[h],k=d.exposedMethods(),g=function(a,b,c){return l[b]=function(){return c.apply(a,arguments),l}};for(f in k){if(e=k[f],null!=this[f])throw new Error(""+this._getObjectClassName(this)+" already has a builder method called: "+f);g(d,f,e)}}}return p(b,a),b.prototype.registerValueHandler=function(a,c){var d,e,f,g;for(g=this.blocks,e=0,f=g.length;f>e;e++)d=g[e],d.registerValueHandler(a,c);return b.__super__.registerValueHandler.call(this,a,c),this},b.prototype.updateOptions=function(a){var b,c,d,f,g;for(this.options=e({},this.options,a),f=this.blocks,g=[],c=0,d=f.length;d>c;c++)b=f[c],g.push(b.options=e({},b.options,a));return g},b.prototype.toString=function(){var a;return function(){var b,c,d,e;for(d=this.blocks,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.buildStr(this));return e}.call(this).filter(function(a){return 0a;a++)b=d[a],e.push(b.buildParam(this));return e}.call(this),g.text=function(){var a,d,e;for(e=[],a=0,d=c.length;d>a;a++)b=c[a],e.push(b.text);return e}().filter(function(a){return 0a;a++)b=c[a],e.push(b.values);return e}()),null==this.options.nestedBuilder&&(this.options.numberedParameters||null!=(null!=a?a.numberedParametersStartAt:void 0))&&(d=1,null!=this.options.numberedParametersStartAt&&(d=this.options.numberedParametersStartAt),g.text=g.text.replace(/\?/g,function(){return"$"+d++})),this.options=f,g},b.prototype.clone=function(){var a;return new this.constructor(this.options,function(){var b,c,d,e;for(d=this.blocks,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.clone());return e}.call(this))},b.prototype.isNestable=function(){return!1},b}(a.BaseBuilder),a.Select=function(b){function c(b,d){null==d&&(d=null),d||(d=[new a.StringBlock(b,"SELECT"),new a.DistinctBlock(b),new a.GetFieldBlock(b),new a.FromTableBlock(e({},b,{allowNested:!0})),new a.JoinBlock(e({},b,{allowNested:!0})),new a.WhereBlock(b),new a.GroupByBlock(b),new a.OrderByBlock(b),new a.LimitBlock(b),new a.OffsetBlock(b),new a.UnionBlock(e({},b,{allowNested:!0}))]),c.__super__.constructor.call(this,b,d)}return p(c,b),c.prototype.isNestable=function(){return!0},c}(a.QueryBuilder),a.Update=function(b){function c(b,d){null==d&&(d=null),d||(d=[new a.StringBlock(b,"UPDATE"),new a.UpdateTableBlock(b),new a.SetFieldBlock(b),new a.WhereBlock(b),new a.OrderByBlock(b),new a.LimitBlock(b)]),c.__super__.constructor.call(this,b,d)}return p(c,b),c}(a.QueryBuilder),a.Delete=function(b){function c(b,d){null==d&&(d=null),d||(d=[new a.StringBlock(b,"DELETE"),new a.FromTableBlock(e({},b,{singleTable:!0})),new a.JoinBlock(b),new a.WhereBlock(b),new a.OrderByBlock(b),new a.LimitBlock(b)]),c.__super__.constructor.call(this,b,d)}return p(c,b),c}(a.QueryBuilder),a.Insert=function(b){function c(b,d){null==d&&(d=null),d||(d=[new a.StringBlock(b,"INSERT"),new a.IntoTableBlock(b),new a.InsertFieldValueBlock(b)]),c.__super__.constructor.call(this,b,d)}return p(c,b),c}(a.QueryBuilder),d={VERSION:"3.10.0",expr:function(){return new a.Expression},select:function(b,c){return new a.Select(b,c)},update:function(b,c){return new a.Update(b,c)},insert:function(b,c){return new a.Insert(b,c)},"delete":function(b,c){return new a.Delete(b,c)},registerValueHandler:a.registerValueHandler},d.remove=d["delete"],d.cls=a,("undefined"!=typeof define&&null!==define?define.amd:void 0)?define(function(){return d}):("undefined"!=typeof module&&null!==module?module.exports:void 0)?module.exports=d:"undefined"!=typeof window&&null!==window&&(window.squel=d),d.flavours={},d.useFlavour=function(a){if(!(d.flavours[a]instanceof Function))throw new Error("Flavour not available: "+a);return d.flavours[a].call(null,d),d}}).call(this); \ No newline at end of file diff --git a/squel.js b/squel.js index 00f18633..904a1cce 100644 --- a/squel.js +++ b/squel.js @@ -25,11 +25,11 @@ OTHER DEALINGS IN THE SOFTWARE. (function() { - var cls, getValueHandler, registerValueHandler, squel, _extend, _ref, _ref1, _ref2, _ref3, _ref4, _without, + var cls, getValueHandler, registerValueHandler, squel, _extend, _ref, _ref1, _ref2, _ref3, _ref4, _ref5, _without, __slice = [].slice, __hasProp = {}.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; }, - __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; + __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 = {}; @@ -136,6 +136,7 @@ OTHER DEALINGS IN THE SOFTWARE. __extends(BaseBuilder, _super); function BaseBuilder(options) { + this._sanitizeNestableQuery = __bind(this._sanitizeNestableQuery, this); var defaults; defaults = JSON.parse(JSON.stringify(cls.DefaultQueryBuilderOptions)); this.options = _extend({}, defaults, options); @@ -200,18 +201,28 @@ OTHER DEALINGS IN THE SOFTWARE. return item; }; + BaseBuilder.prototype._sanitizeNestableQuery = function(item) { + if (item instanceof cls.QueryBuilder && item.isNestable()) { + return item; + } + throw new Error("must be a nestable query, e.g. SELECT"); + }; + BaseBuilder.prototype._sanitizeTable = function(item, allowNested) { - var sanitized; + var e, sanitized; if (allowNested == null) { allowNested = false; } if (allowNested) { if ("string" === typeof item) { sanitized = item; - } else if (item instanceof cls.QueryBuilder && item.isNestable()) { - return item; } else { - throw new Error("table name must be a string or a nestable query instance"); + try { + sanitized = this._sanitizeNestableQuery(item); + } catch (_error) { + e = _error; + throw new Error("table name must be a string or a nestable query instance"); + } } } else { sanitized = this._sanitizeName(item, 'table name'); @@ -1145,6 +1156,49 @@ OTHER DEALINGS IN THE SOFTWARE. })(cls.AbstractSetFieldBlock); + cls.InsertFieldSelectQueryBlock = (function(_super) { + __extends(InsertFieldSelectQueryBlock, _super); + + function InsertFieldSelectQueryBlock() { + _ref5 = InsertFieldSelectQueryBlock.__super__.constructor.apply(this, arguments); + return _ref5; + } + + InsertFieldSelectQueryBlock.prototype.fromSelect = function(fields, selectQuery) { + this._fields = fields; + return this._query = this._sanitizeNestableQuery(selectQuery); + }; + + InsertFieldSelectQueryBlock.prototype.buildStr = function(queryBuilder) { + if (0 >= this.fields.length) { + throw new Error("set() needs to be called"); + } + return "(" + (this.fields.join(', ')) + ") VALUES (" + (this._buildVals().join('), (')) + ")"; + }; + + InsertFieldSelectQueryBlock.prototype.buildParam = function(queryBuilder) { + var i, params, str, vals, _i, _ref6, _ref7; + if (0 >= this.fields.length) { + throw new Error("set() needs to be called"); + } + str = ""; + _ref6 = this._buildValParams(), vals = _ref6.vals, params = _ref6.params; + for (i = _i = 0, _ref7 = this.fields.length; 0 <= _ref7 ? _i < _ref7 : _i > _ref7; i = 0 <= _ref7 ? ++_i : --_i) { + if ("" !== str) { + str += ", "; + } + str += this.fields[i]; + } + return { + text: "(" + str + ") VALUES (" + (vals.join('), (')) + ")", + values: params + }; + }; + + return InsertFieldSelectQueryBlock; + + })(cls.Block); + cls.DistinctBlock = (function(_super) { __extends(DistinctBlock, _super); @@ -1183,12 +1237,12 @@ OTHER DEALINGS IN THE SOFTWARE. }; GroupByBlock.prototype.buildStr = function(queryBuilder) { - var f, groups, _i, _len, _ref5; + var f, groups, _i, _len, _ref6; groups = ""; if (0 < this.groups.length) { - _ref5 = this.groups; - for (_i = 0, _len = _ref5.length; _i < _len; _i++) { - f = _ref5[_i]; + _ref6 = this.groups; + for (_i = 0, _len = _ref6.length; _i < _len; _i++) { + f = _ref6[_i]; if ("" !== groups) { groups += ", "; } @@ -1237,7 +1291,7 @@ OTHER DEALINGS IN THE SOFTWARE. } WhereBlock.prototype.where = function() { - var c, condition, finalCondition, finalValues, idx, inValues, item, nextValue, t, values, _i, _j, _len, _ref5; + var c, condition, finalCondition, finalValues, idx, inValues, item, nextValue, t, values, _i, _j, _len, _ref6; condition = arguments[0], values = 2 <= arguments.length ? __slice.call(arguments, 1) : []; condition = this._sanitizeCondition(condition); finalCondition = ""; @@ -1247,7 +1301,7 @@ OTHER DEALINGS IN THE SOFTWARE. finalCondition = t.text; finalValues = t.values; } else { - for (idx = _i = 0, _ref5 = condition.length; 0 <= _ref5 ? _i < _ref5 : _i > _ref5; idx = 0 <= _ref5 ? ++_i : --_i) { + for (idx = _i = 0, _ref6 = condition.length; 0 <= _ref6 ? _i < _ref6 : _i > _ref6; idx = 0 <= _ref6 ? ++_i : --_i) { c = condition.charAt(idx); if ('?' === c && 0 < values.length) { nextValue = values.shift(); @@ -1285,20 +1339,20 @@ OTHER DEALINGS IN THE SOFTWARE. }; WhereBlock.prototype.buildStr = function(queryBuilder) { - var c, idx, pIndex, where, whereStr, _i, _j, _len, _ref5, _ref6; + var c, idx, pIndex, where, whereStr, _i, _j, _len, _ref6, _ref7; if (0 >= this.wheres.length) { return ""; } whereStr = ""; - _ref5 = this.wheres; - for (_i = 0, _len = _ref5.length; _i < _len; _i++) { - where = _ref5[_i]; + _ref6 = this.wheres; + for (_i = 0, _len = _ref6.length; _i < _len; _i++) { + where = _ref6[_i]; if ("" !== whereStr) { whereStr += ") AND ("; } if (0 < where.values.length) { pIndex = 0; - for (idx = _j = 0, _ref6 = where.text.length; 0 <= _ref6 ? _j < _ref6 : _j > _ref6; idx = 0 <= _ref6 ? ++_j : --_j) { + for (idx = _j = 0, _ref7 = where.text.length; 0 <= _ref7 ? _j < _ref7 : _j > _ref7; idx = 0 <= _ref7 ? ++_j : --_j) { c = where.text.charAt(idx); if ('?' === c) { whereStr += this._formatValue(where.values[pIndex++]); @@ -1314,7 +1368,7 @@ OTHER DEALINGS IN THE SOFTWARE. }; WhereBlock.prototype.buildParam = function(queryBuilder) { - var i, p, qv, ret, str, v, where, whereStr, _i, _j, _k, _len, _len1, _len2, _ref5, _ref6, _ref7; + var i, p, qv, ret, str, v, where, whereStr, _i, _j, _k, _len, _len1, _len2, _ref6, _ref7, _ref8; ret = { text: "", values: [] @@ -1323,26 +1377,26 @@ OTHER DEALINGS IN THE SOFTWARE. return ret; } whereStr = ""; - _ref5 = this.wheres; - for (_i = 0, _len = _ref5.length; _i < _len; _i++) { - where = _ref5[_i]; + _ref6 = this.wheres; + for (_i = 0, _len = _ref6.length; _i < _len; _i++) { + where = _ref6[_i]; if ("" !== whereStr) { whereStr += ") AND ("; } str = where.text.split('?'); i = 0; - _ref6 = where.values; - for (_j = 0, _len1 = _ref6.length; _j < _len1; _j++) { - v = _ref6[_j]; + _ref7 = where.values; + for (_j = 0, _len1 = _ref7.length; _j < _len1; _j++) { + v = _ref7[_j]; if (str[i] != null) { whereStr += "" + str[i]; } p = this._formatValueAsParam(v); if (((p != null ? p.text : void 0) != null)) { whereStr += "(" + p.text + ")"; - _ref7 = p.values; - for (_k = 0, _len2 = _ref7.length; _k < _len2; _k++) { - qv = _ref7[_k]; + _ref8 = p.values; + for (_k = 0, _len2 = _ref8.length; _k < _len2; _k++) { + qv = _ref8[_k]; ret.values.push(qv); } } else { @@ -1387,22 +1441,22 @@ OTHER DEALINGS IN THE SOFTWARE. }; OrderByBlock.prototype._buildStr = function(toParam) { - var c, fstr, idx, o, orders, pIndex, _i, _j, _len, _ref5, _ref6; + var c, fstr, idx, o, orders, pIndex, _i, _j, _len, _ref6, _ref7; if (toParam == null) { toParam = false; } if (0 < this.orders.length) { pIndex = 0; orders = ""; - _ref5 = this.orders; - for (_i = 0, _len = _ref5.length; _i < _len; _i++) { - o = _ref5[_i]; + _ref6 = this.orders; + for (_i = 0, _len = _ref6.length; _i < _len; _i++) { + o = _ref6[_i]; if ("" !== orders) { orders += ", "; } fstr = ""; if (!toParam) { - for (idx = _j = 0, _ref6 = o.field.length; 0 <= _ref6 ? _j < _ref6 : _j > _ref6; idx = 0 <= _ref6 ? ++_j : --_j) { + for (idx = _j = 0, _ref7 = o.field.length; 0 <= _ref7 ? _j < _ref7 : _j > _ref7; idx = 0 <= _ref7 ? ++_j : --_j) { c = o.field.charAt(idx); if ('?' === c) { fstr += this._formatValue(this._values[pIndex++]); @@ -1549,11 +1603,11 @@ OTHER DEALINGS IN THE SOFTWARE. }; JoinBlock.prototype.buildStr = function(queryBuilder) { - var j, joins, _i, _len, _ref5; + var j, joins, _i, _len, _ref6; joins = ""; - _ref5 = this.joins || []; - for (_i = 0, _len = _ref5.length; _i < _len; _i++) { - j = _ref5[_i]; + _ref6 = this.joins || []; + for (_i = 0, _len = _ref6.length; _i < _len; _i++) { + j = _ref6[_i]; if (joins !== "") { joins += " "; } @@ -1574,7 +1628,7 @@ OTHER DEALINGS IN THE SOFTWARE. }; JoinBlock.prototype.buildParam = function(queryBuilder) { - var blk, cp, joinStr, p, params, ret, v, _i, _j, _k, _len, _len1, _len2, _ref5, _ref6; + var blk, cp, joinStr, p, params, ret, v, _i, _j, _k, _len, _len1, _len2, _ref6, _ref7; ret = { text: "", values: [] @@ -1584,9 +1638,9 @@ OTHER DEALINGS IN THE SOFTWARE. if (0 >= this.joins.length) { return ret; } - _ref5 = this.joins; - for (_i = 0, _len = _ref5.length; _i < _len; _i++) { - blk = _ref5[_i]; + _ref6 = this.joins; + for (_i = 0, _len = _ref6.length; _i < _len; _i++) { + blk = _ref6[_i]; if ("string" === typeof blk.table) { p = { "text": "" + blk.table, @@ -1630,9 +1684,9 @@ OTHER DEALINGS IN THE SOFTWARE. if (p.condition) { joinStr += " ON (" + p.condition + ")"; } - _ref6 = p.values; - for (_k = 0, _len2 = _ref6.length; _k < _len2; _k++) { - v = _ref6[_k]; + _ref7 = p.values; + for (_k = 0, _len2 = _ref7.length; _k < _len2; _k++) { + v = _ref7[_k]; ret.values.push(this._formatCustomValue(v)); } } @@ -1669,11 +1723,11 @@ OTHER DEALINGS IN THE SOFTWARE. }; UnionBlock.prototype.buildStr = function(queryBuilder) { - var j, unionStr, _i, _len, _ref5; + var j, unionStr, _i, _len, _ref6; unionStr = ""; - _ref5 = this.unions || []; - for (_i = 0, _len = _ref5.length; _i < _len; _i++) { - j = _ref5[_i]; + _ref6 = this.unions || []; + for (_i = 0, _len = _ref6.length; _i < _len; _i++) { + j = _ref6[_i]; if (unionStr !== "") { unionStr += " "; } @@ -1688,7 +1742,7 @@ OTHER DEALINGS IN THE SOFTWARE. }; UnionBlock.prototype.buildParam = function(queryBuilder) { - var blk, p, params, ret, unionStr, v, _i, _j, _k, _len, _len1, _len2, _ref5, _ref6; + var blk, p, params, ret, unionStr, v, _i, _j, _k, _len, _len1, _len2, _ref6, _ref7; ret = { text: "", values: [] @@ -1698,9 +1752,9 @@ OTHER DEALINGS IN THE SOFTWARE. if (0 >= this.unions.length) { return ret; } - _ref5 = this.unions || []; - for (_i = 0, _len = _ref5.length; _i < _len; _i++) { - blk = _ref5[_i]; + _ref6 = this.unions || []; + for (_i = 0, _len = _ref6.length; _i < _len; _i++) { + blk = _ref6[_i]; if ("string" === typeof blk.table) { p = { "text": "" + blk.table, @@ -1726,9 +1780,9 @@ OTHER DEALINGS IN THE SOFTWARE. unionStr += " "; } unionStr += "" + p.type + " (" + p.text + ")"; - _ref6 = p.values; - for (_k = 0, _len2 = _ref6.length; _k < _len2; _k++) { - v = _ref6[_k]; + _ref7 = p.values; + for (_k = 0, _len2 = _ref7.length; _k < _len2; _k++) { + v = _ref7[_k]; ret.values.push(this._formatCustomValue(v)); } } @@ -1744,22 +1798,22 @@ OTHER DEALINGS IN THE SOFTWARE. __extends(QueryBuilder, _super); function QueryBuilder(options, blocks) { - var block, methodBody, methodName, _fn, _i, _len, _ref5, _ref6, + var block, methodBody, methodName, _fn, _i, _len, _ref6, _ref7, _this = this; QueryBuilder.__super__.constructor.call(this, options); this.blocks = blocks || []; - _ref5 = this.blocks; - for (_i = 0, _len = _ref5.length; _i < _len; _i++) { - block = _ref5[_i]; - _ref6 = block.exposedMethods(); + _ref6 = this.blocks; + for (_i = 0, _len = _ref6.length; _i < _len; _i++) { + block = _ref6[_i]; + _ref7 = block.exposedMethods(); _fn = function(block, name, body) { return _this[name] = function() { body.apply(block, arguments); return _this; }; }; - for (methodName in _ref6) { - methodBody = _ref6[methodName]; + for (methodName in _ref7) { + methodBody = _ref7[methodName]; if (this[methodName] != null) { throw new Error("" + (this._getObjectClassName(this)) + " already has a builder method called: " + methodName); } @@ -1769,10 +1823,10 @@ OTHER DEALINGS IN THE SOFTWARE. } QueryBuilder.prototype.registerValueHandler = function(type, handler) { - var block, _i, _len, _ref5; - _ref5 = this.blocks; - for (_i = 0, _len = _ref5.length; _i < _len; _i++) { - block = _ref5[_i]; + var block, _i, _len, _ref6; + _ref6 = this.blocks; + for (_i = 0, _len = _ref6.length; _i < _len; _i++) { + block = _ref6[_i]; block.registerValueHandler(type, handler); } QueryBuilder.__super__.registerValueHandler.call(this, type, handler); @@ -1780,12 +1834,12 @@ OTHER DEALINGS IN THE SOFTWARE. }; QueryBuilder.prototype.updateOptions = function(options) { - var block, _i, _len, _ref5, _results; + var block, _i, _len, _ref6, _results; this.options = _extend({}, this.options, options); - _ref5 = this.blocks; + _ref6 = this.blocks; _results = []; - for (_i = 0, _len = _ref5.length; _i < _len; _i++) { - block = _ref5[_i]; + for (_i = 0, _len = _ref6.length; _i < _len; _i++) { + block = _ref6[_i]; _results.push(block.options = _extend({}, block.options, options)); } return _results; @@ -1794,11 +1848,11 @@ OTHER DEALINGS IN THE SOFTWARE. QueryBuilder.prototype.toString = function() { var block; return ((function() { - var _i, _len, _ref5, _results; - _ref5 = this.blocks; + var _i, _len, _ref6, _results; + _ref6 = this.blocks; _results = []; - for (_i = 0, _len = _ref5.length; _i < _len; _i++) { - block = _ref5[_i]; + for (_i = 0, _len = _ref6.length; _i < _len; _i++) { + block = _ref6[_i]; _results.push(block.buildStr(this)); } return _results; @@ -1808,7 +1862,7 @@ OTHER DEALINGS IN THE SOFTWARE. }; QueryBuilder.prototype.toParam = function(options) { - var block, blocks, i, old, result, _ref5; + var block, blocks, i, old, result, _ref6; if (options == null) { options = void 0; } @@ -1821,11 +1875,11 @@ OTHER DEALINGS IN THE SOFTWARE. values: [] }; blocks = (function() { - var _i, _len, _ref5, _results; - _ref5 = this.blocks; + var _i, _len, _ref6, _results; + _ref6 = this.blocks; _results = []; - for (_i = 0, _len = _ref5.length; _i < _len; _i++) { - block = _ref5[_i]; + for (_i = 0, _len = _ref6.length; _i < _len; _i++) { + block = _ref6[_i]; _results.push(block.buildParam(this)); } return _results; @@ -1841,7 +1895,7 @@ OTHER DEALINGS IN THE SOFTWARE. })()).filter(function(v) { return 0 < v.length; }).join(this.options.separator); - result.values = (_ref5 = []).concat.apply(_ref5, (function() { + result.values = (_ref6 = []).concat.apply(_ref6, (function() { var _i, _len, _results; _results = []; for (_i = 0, _len = blocks.length; _i < _len; _i++) { @@ -1868,11 +1922,11 @@ OTHER DEALINGS IN THE SOFTWARE. QueryBuilder.prototype.clone = function() { var block; return new this.constructor(this.options, (function() { - var _i, _len, _ref5, _results; - _ref5 = this.blocks; + var _i, _len, _ref6, _results; + _ref6 = this.blocks; _results = []; - for (_i = 0, _len = _ref5.length; _i < _len; _i++) { - block = _ref5[_i]; + for (_i = 0, _len = _ref6.length; _i < _len; _i++) { + block = _ref6[_i]; _results.push(block.clone()); } return _results; @@ -2138,14 +2192,14 @@ OTHER DEALINGS IN THE SOFTWARE. squel.flavours['mysql'] = function() { - var _ref5, _ref6; + var _ref6, _ref7; cls = squel.cls; cls.TargetTableBlock = (function(_super) { __extends(TargetTableBlock, _super); function TargetTableBlock() { - _ref5 = TargetTableBlock.__super__.constructor.apply(this, arguments); - return _ref5; + _ref6 = TargetTableBlock.__super__.constructor.apply(this, arguments); + return _ref6; } TargetTableBlock.prototype.target = function(table) { @@ -2159,8 +2213,8 @@ OTHER DEALINGS IN THE SOFTWARE. __extends(MysqlOnDuplicateKeyUpdateBlock, _super); function MysqlOnDuplicateKeyUpdateBlock() { - _ref6 = MysqlOnDuplicateKeyUpdateBlock.__super__.constructor.apply(this, arguments); - return _ref6; + _ref7 = MysqlOnDuplicateKeyUpdateBlock.__super__.constructor.apply(this, arguments); + return _ref7; } MysqlOnDuplicateKeyUpdateBlock.prototype.onDupUpdate = function(field, value, options) { @@ -2168,9 +2222,9 @@ OTHER DEALINGS IN THE SOFTWARE. }; MysqlOnDuplicateKeyUpdateBlock.prototype.buildStr = function() { - var field, fieldOptions, i, str, value, _i, _ref7; + var field, fieldOptions, i, str, value, _i, _ref8; str = ""; - for (i = _i = 0, _ref7 = this.fields.length; 0 <= _ref7 ? _i < _ref7 : _i > _ref7; i = 0 <= _ref7 ? ++_i : --_i) { + for (i = _i = 0, _ref8 = this.fields.length; 0 <= _ref8 ? _i < _ref8 : _i > _ref8; i = 0 <= _ref8 ? ++_i : --_i) { field = this.fields[i]; if ("" !== str) { str += ", "; @@ -2191,10 +2245,10 @@ OTHER DEALINGS IN THE SOFTWARE. }; MysqlOnDuplicateKeyUpdateBlock.prototype.buildParam = function(queryBuilder) { - var field, i, str, vals, value, _i, _ref7; + var field, i, str, vals, value, _i, _ref8; str = ""; vals = []; - for (i = _i = 0, _ref7 = this.fields.length; 0 <= _ref7 ? _i < _ref7 : _i > _ref7; i = 0 <= _ref7 ? ++_i : --_i) { + for (i = _i = 0, _ref8 = this.fields.length; 0 <= _ref8 ? _i < _ref8 : _i > _ref8; i = 0 <= _ref8 ? ++_i : --_i) { field = this.fields[i]; if ("" !== str) { str += ", "; @@ -2302,7 +2356,7 @@ OTHER DEALINGS IN THE SOFTWARE. return "" + (date.getUTCFullYear()) + "-" + (date.getUTCMonth() + 1) + "-" + (date.getUTCDate()) + " " + (date.getUTCHours()) + ":" + (date.getUTCMinutes()) + ":" + (date.getUTCSeconds()); }); cls.MssqlLimitOffsetTopBlock = (function(_super) { - var LimitBlock, OffsetBlock, ParentBlock, TopBlock, _limit, _ref5, _ref6, _ref7; + var LimitBlock, OffsetBlock, ParentBlock, TopBlock, _limit, _ref6, _ref7, _ref8; __extends(MssqlLimitOffsetTopBlock, _super); @@ -2333,8 +2387,8 @@ OTHER DEALINGS IN THE SOFTWARE. __extends(LimitBlock, _super1); function LimitBlock() { - _ref5 = LimitBlock.__super__.constructor.apply(this, arguments); - return _ref5; + _ref6 = LimitBlock.__super__.constructor.apply(this, arguments); + return _ref6; } LimitBlock.prototype.limit = _limit; @@ -2355,8 +2409,8 @@ OTHER DEALINGS IN THE SOFTWARE. __extends(TopBlock, _super1); function TopBlock() { - _ref6 = TopBlock.__super__.constructor.apply(this, arguments); - return _ref6; + _ref7 = TopBlock.__super__.constructor.apply(this, arguments); + return _ref7; } TopBlock.prototype.top = _limit; @@ -2378,8 +2432,8 @@ OTHER DEALINGS IN THE SOFTWARE. function OffsetBlock() { this.offset = __bind(this.offset, this); - _ref7 = OffsetBlock.__super__.constructor.apply(this, arguments); - return _ref7; + _ref8 = OffsetBlock.__super__.constructor.apply(this, arguments); + return _ref8; } OffsetBlock.prototype.offset = function(start) { @@ -2477,13 +2531,13 @@ OTHER DEALINGS IN THE SOFTWARE. }; MssqlInsertFieldValueBlock.prototype.buildParam = function(queryBuilder) { - var i, params, str, vals, _i, _ref5, _ref6; + var i, params, str, vals, _i, _ref6, _ref7; if (0 >= this.fields.length) { throw new Error("set() needs to be called"); } str = ""; - _ref5 = this._buildValParams(), vals = _ref5.vals, params = _ref5.params; - for (i = _i = 0, _ref6 = this.fields.length; 0 <= _ref6 ? _i < _ref6 : _i > _ref6; i = 0 <= _ref6 ? ++_i : --_i) { + _ref6 = this._buildValParams(), vals = _ref6.vals, params = _ref6.params; + for (i = _i = 0, _ref7 = this.fields.length; 0 <= _ref7 ? _i < _ref7 : _i > _ref7; i = 0 <= _ref7 ? ++_i : --_i) { if ("" !== str) { str += ", "; } @@ -2531,12 +2585,12 @@ OTHER DEALINGS IN THE SOFTWARE. }; MssqlUpdateDeleteOutputBlock.prototype.buildStr = function(queryBuilder) { - var output, outputs, _i, _len, _ref5; + var output, outputs, _i, _len, _ref6; outputs = ""; if (this._outputs.length > 0) { - _ref5 = this._outputs; - for (_i = 0, _len = _ref5.length; _i < _len; _i++) { - output = _ref5[_i]; + _ref6 = this._outputs; + for (_i = 0, _len = _ref6.length; _i < _len; _i++) { + output = _ref6[_i]; if ("" !== outputs) { outputs += ", "; } diff --git a/squel.min.js b/squel.min.js index ddfed442..fe14edf7 100644 --- a/squel.min.js +++ b/squel.min.js @@ -1,2 +1,2 @@ -/*! squel | https://github.com/hiddentao/squel | BSD license */(function(){var a,b,c,d,e,f,g,h,i,j,k,l=[].slice,m={}.hasOwnProperty,n=function(a,b){function c(){this.constructor=a}for(var d in b)m.call(b,d)&&(a[d]=b[d]);return c.prototype=b.prototype,a.prototype=new c,a.__super__=b.prototype,a},o=function(a,b){return function(){return a.apply(b,arguments)}};a={},e=function(){var a,b,c,d,e,f,g;if(a=arguments[0],c=2<=arguments.length?l.call(arguments,1):[])for(f=0,g=c.length;g>f;f++)if(d=c[f])for(b in d)m.call(d,b)&&(e=d[b],a[b]=e);return a},k=function(){var a,b,c,d,f,g;for(b=arguments[0],d=2<=arguments.length?l.call(arguments,1):[],a=e({},b),f=0,g=d.length;g>f;f++)c=d[f],delete a[c];return a},a.DefaultQueryBuilderOptions={autoQuoteTableNames:!1,autoQuoteFieldNames:!1,autoQuoteAliasNames:!0,nameQuoteCharacter:"`",tableAliasQuoteCharacter:"`",fieldAliasQuoteCharacter:'"',valueHandlers:[],numberedParameters:!1,numberedParametersStartAt:1,replaceSingleQuotes:!1,singleQuoteReplacement:"''",separator:" "},a.globalValueHandlers=[],c=function(a,b,c){var d,e,f;if("function"!=typeof b&&"string"!=typeof b)throw new Error("type must be a class constructor or string denoting 'typeof' result");if("function"!=typeof c)throw new Error("handler must be a function");for(e=0,f=a.length;f>e;e++)if(d=a[e],d.type===b)return void(d.handler=c);return a.push({type:b,handler:c})},b=function(){var a,b,c,d,e,f,g,h;for(d=arguments[0],a=2<=arguments.length?l.call(arguments,1):[],e=0,g=a.length;g>e;e++)for(b=a[e],f=0,h=b.length;h>f;f++)if(c=b[f],c.type===typeof d||"string"!=typeof c.type&&d instanceof c.type)return c.handler;return void 0},a.registerValueHandler=function(b,d){return c(a.globalValueHandlers,b,d)},a.Cloneable=function(){function a(){}return a.prototype.clone=function(){var a;return a=new this.constructor,e(a,JSON.parse(JSON.stringify(this)))},a}(),a.BaseBuilder=function(d){function f(b){var c;c=JSON.parse(JSON.stringify(a.DefaultQueryBuilderOptions)),this.options=e({},c,b)}return n(f,d),f.prototype.registerValueHandler=function(a,b){return c(this.options.valueHandlers,a,b),this},f.prototype._getObjectClassName=function(a){var b;return a&&a.constructor&&a.constructor.toString&&(b=a.constructor.toString().match(/function\s*(\w+)/),b&&2===b.length)?b[1]:void 0},f.prototype._sanitizeCondition=function(b){if(!(b instanceof a.Expression)&&"string"!=typeof b)throw new Error("condition must be a string or Expression instance");return b},f.prototype._sanitizeName=function(a,b){if("string"!=typeof a)throw new Error(""+b+" must be a string");return a},f.prototype._sanitizeField=function(b,c){var d;return null==c&&(c={}),b instanceof a.QueryBuilder?b="("+b+")":(b=this._sanitizeName(b,"field name"),this.options.autoQuoteFieldNames&&(d=this.options.nameQuoteCharacter,b=c.ignorePeriodsForFieldNameQuotes?""+d+b+d:b.split(".").map(function(a){return"*"===a?a:""+d+a+d}).join("."))),b},f.prototype._sanitizeTable=function(b,c){var d;if(null==c&&(c=!1),c){if("string"!=typeof b){if(b instanceof a.QueryBuilder&&b.isNestable())return b;throw new Error("table name must be a string or a nestable query instance")}d=b}else d=this._sanitizeName(b,"table name");return this.options.autoQuoteTableNames?""+this.options.nameQuoteCharacter+d+this.options.nameQuoteCharacter:d},f.prototype._sanitizeTableAlias=function(a){var b;return b=this._sanitizeName(a,"table alias"),this.options.autoQuoteAliasNames?""+this.options.tableAliasQuoteCharacter+b+this.options.tableAliasQuoteCharacter:b},f.prototype._sanitizeFieldAlias=function(a){var b;return b=this._sanitizeName(a,"field alias"),this.options.autoQuoteAliasNames?""+this.options.fieldAliasQuoteCharacter+b+this.options.fieldAliasQuoteCharacter:b},f.prototype._sanitizeLimitOffset=function(a){if(a=parseInt(a),0>a||isNaN(a))throw new Error("limit/offset must be >= 0");return a},f.prototype._sanitizeValue=function(c){var d,e;if(d=typeof c,null===c);else if("string"===d||"number"===d||"boolean"===d);else if(c instanceof a.QueryBuilder&&c.isNestable());else if(e=void 0!==b(c,this.options.valueHandlers,a.globalValueHandlers),!e)throw new Error("field value must be a string, number, boolean, null or one of the registered custom value types");return c},f.prototype._escapeValue=function(a){return!0!==this.options.replaceSingleQuotes?a:a.replace(/\'/g,this.options.singleQuoteReplacement)},f.prototype._formatCustomValue=function(c){var d;return d=b(c,this.options.valueHandlers,a.globalValueHandlers),d&&(c=d(c)),c},f.prototype._formatValueAsParam=function(b){var c,d=this;return Array.isArray(b)?b.map(function(a){return d._formatValueAsParam(a)}):b instanceof a.QueryBuilder&&b.isNestable()?(b.updateOptions({nestedBuilder:!0}),c=b.toParam()):b instanceof a.Expression?c=b.toParam():this._formatCustomValue(b)},f.prototype._formatValue=function(b,c){var d=this;return null==c&&(c={}),b=this._formatCustomValue(b),Array.isArray(b)?(b=b.map(function(a){return d._formatValue(a)}),b="("+b.join(", ")+")"):null===b?b="NULL":"boolean"==typeof b?b=b?"TRUE":"FALSE":b instanceof a.QueryBuilder?b="("+b+")":b instanceof a.Expression?b="("+b+")":"number"!=typeof b&&(b=this._escapeValue(b),b=c.dontQuote?""+b:"'"+b+"'"),b},f}(a.Cloneable),a.Expression=function(a){function b(){var a=this;b.__super__.constructor.call(this),this.tree={parent:null,nodes:[]},this.current=this.tree,this._begin=function(b){var c;return c={type:b,parent:a.current,nodes:[]},a.current.nodes.push(c),a.current=a.current.nodes[a.current.nodes.length-1],a}}return n(b,a),b.prototype.tree=null,b.prototype.current=null,b.prototype.and_begin=function(){return this._begin("AND")},b.prototype.or_begin=function(){return this._begin("OR")},b.prototype.end=function(){if(!this.current.parent)throw new Error("begin() needs to be called");return this.current=this.current.parent,this},b.prototype.and=function(a,b){if(!a||"string"!=typeof a)throw new Error("expr must be a string");return this.current.nodes.push({type:"AND",expr:a,para:b}),this},b.prototype.or=function(a,b){if(!a||"string"!=typeof a)throw new Error("expr must be a string");return this.current.nodes.push({type:"OR",expr:a,para:b}),this},b.prototype.toString=function(){if(null!==this.current.parent)throw new Error("end() needs to be called");return this._toString(this.tree)},b.prototype.toParam=function(){if(null!==this.current.parent)throw new Error("end() needs to be called");return this._toString(this.tree,!0)},b.prototype._toString=function(a,b){var c,d,e,f,g,h,i,j,k;for(null==b&&(b=!1),h="",g=[],k=a.nodes,i=0,j=k.length;j>i;i++)c=k[i],null!=c.expr?(f=c.expr,void 0!==c.para&&(b?(d=this._formatValueAsParam(c.para),null!=(null!=d?d.text:void 0)?(g=g.concat(d.values),f=f.replace("?","("+d.text+")")):g=g.concat(d),Array.isArray(c.para)&&(e=Array.apply(null,new Array(c.para.length)).map(function(){return"?"}),f=f.replace("?","("+e.join(", ")+")"))):f=f.replace("?",this._formatValue(c.para)))):(f=this._toString(c,b),b&&(g=g.concat(f.values),f=f.text),""!==f&&(f="("+f+")")),""!==f&&(""!==h&&(h+=" "+c.type+" "),h+=f);return b?{text:h,values:g}:h},b.prototype.clone=function(){var a,b;return a=new this.constructor,(b=function(c){var d,e,f,g,h;for(g=c.nodes,h=[],e=0,f=g.length;f>e;e++)d=g[e],null!=d.expr?h.push(a.current.nodes.push(JSON.parse(JSON.stringify(d)))):(a._begin(d.type),b(d),h.push(!this.current===d?a.end():void 0));return h})(this.tree),a},b}(a.BaseBuilder),a.Block=function(b){function c(){return f=c.__super__.constructor.apply(this,arguments)}return n(c,b),c.prototype.exposedMethods=function(){var b,c,d;c={};for(b in this)d=this[b],"function"!=typeof d||"_"===b.charAt(0)||a.Block.prototype[b]||(c[b]=d);return c},c.prototype.buildStr=function(){return""},c.prototype.buildParam=function(a){return{text:this.buildStr(a),values:[]}},c}(a.BaseBuilder),a.StringBlock=function(a){function b(a,c){b.__super__.constructor.call(this,a),this.str=c}return n(b,a),b.prototype.buildStr=function(){return this.str},b}(a.Block),a.AbstractValueBlock=function(a){function b(a){b.__super__.constructor.call(this,a),this._val=null}return n(b,a),b.prototype._setValue=function(a){return this._val=a},b.prototype.buildStr=function(){return this._val?this._val:""},b}(a.Block),a.AbstractTableBlock=function(b){function c(a){c.__super__.constructor.call(this,a),this.tables=[]}return n(c,b),c.prototype._table=function(a,b){return null==b&&(b=null),b&&(b=this._sanitizeTableAlias(b)),a=this._sanitizeTable(a,this.options.allowNested||!1),this.options.singleTable&&(this.tables=[]),this.tables.push({table:a,alias:b})},c.prototype.buildStr=function(){var a,b,c,d,e;if(0>=this.tables.length)throw new Error("_table() needs to be called");for(b="",e=this.tables,c=0,d=e.length;d>c;c++)a=e[c],""!==b&&(b+=", "),b+="string"==typeof a.table?a.table:"("+a.table+")",a.alias&&(b+=" "+a.alias);return b},c.prototype._buildParam=function(b,c){var d,e,f,g,h,i,j,k,l,m,n,o,p,q;if(null==c&&(c=null),h={text:"",values:[]},g=[],f="",0>=this.tables.length)return h;for(p=this.tables,j=0,m=p.length;m>j;j++)d=p[j],"string"==typeof d.table?e={text:""+d.table,values:[]}:d.table instanceof a.QueryBuilder?(d.table.updateOptions({nestedBuilder:!0}),e=d.table.toParam()):(d.updateOptions({nestedBuilder:!0}),e=d.buildParam(b)),e.table=d,g.push(e);for(k=0,n=g.length;n>k;k++)for(e=g[k],""!==f?f+=", ":null!=c&&""!==c&&(f+=""+c+" "+f),f+="string"==typeof e.table.table?""+e.text:"("+e.text+")",null!=e.table.alias&&(f+=" "+e.table.alias),q=e.values,l=0,o=q.length;o>l;l++)i=q[l],h.values.push(this._formatCustomValue(i));return h.text+=f,h},c.prototype.buildParam=function(a){return this._buildParam(a)},c}(a.Block),a.UpdateTableBlock=function(a){function b(){return g=b.__super__.constructor.apply(this,arguments)}return n(b,a),b.prototype.table=function(a,b){return null==b&&(b=null),this._table(a,b)},b}(a.AbstractTableBlock),a.FromTableBlock=function(a){function b(){return h=b.__super__.constructor.apply(this,arguments)}return n(b,a),b.prototype.from=function(a,b){return null==b&&(b=null),this._table(a,b)},b.prototype.buildStr=function(a){var c;if(0>=this.tables.length)throw new Error("from() needs to be called");return c=b.__super__.buildStr.call(this,a),"FROM "+c},b.prototype.buildParam=function(a){if(0>=this.tables.length)throw new Error("from() needs to be called");return this._buildParam(a,"FROM")},b}(a.AbstractTableBlock),a.IntoTableBlock=function(a){function b(a){b.__super__.constructor.call(this,a),this.table=null}return n(b,a),b.prototype.into=function(a){return this.table=this._sanitizeTable(a,!1)},b.prototype.buildStr=function(){if(!this.table)throw new Error("into() needs to be called");return"INTO "+this.table},b}(a.Block),a.GetFieldBlock=function(a){function b(a){b.__super__.constructor.call(this,a),this._fieldAliases={},this._fields=[]}return n(b,a),b.prototype.fields=function(a,b){var c,d,e,f,g,h;if(null==b&&(b={}),Array.isArray(a)){for(g=[],e=0,f=a.length;f>e;e++)d=a[e],g.push(this.field(d,null,b));return g}h=[];for(d in a)c=a[d],h.push(this.field(d,c,b));return h},b.prototype.field=function(a,b,c){return null==b&&(b=null),null==c&&(c={}),a=this._sanitizeField(a,c),b&&(b=this._sanitizeFieldAlias(b)),this._fieldAliases[a]!==b?(this._fieldAliases[a]=b,this._fields.push({name:a,alias:b})):void 0},b.prototype.buildStr=function(){var a,b,c,d,e;for(b="",e=this._fields,c=0,d=e.length;d>c;c++)a=e[c],""!==b&&(b+=", "),b+=a.name,a.alias&&(b+=" AS "+a.alias);return""===b?"*":b},b}(a.Block),a.AbstractSetFieldBlock=function(a){function b(a){b.__super__.constructor.call(this,a),this.fieldOptions=[],this.fields=[],this.values=[]}return n(b,a),b.prototype._set=function(a,b,c){var d;if(null==c&&(c={}),this.values.length>1)throw new Error("Cannot call set or setFields on multiple rows of fields.");return void 0!==b&&(b=this._sanitizeValue(b)),d=this.fields.indexOf(this._sanitizeField(a,c)),-1!==d?(this.values[0][d]=b,this.fieldOptions[0][d]=c):(this.fields.push(this._sanitizeField(a,c)),d=this.fields.length-1,Array.isArray(this.values[0])?(this.values[0][d]=b,this.fieldOptions[0][d]=c):(this.values.push([b]),this.fieldOptions.push([c]))),this},b.prototype._setFields=function(a,b){var c;if(null==b&&(b={}),"object"!=typeof a)throw new Error("Expected an object but got "+typeof a);for(c in a)m.call(a,c)&&this._set(c,a[c],b);return this},b.prototype._setFieldsRows=function(a,b){var c,d,e,f,g,h,i;if(null==b&&(b={}),!Array.isArray(a))throw new Error("Expected an array of objects but got "+typeof a);for(this.fields=[],this.values=[],d=g=0,h=a.length;h>=0?h>g:g>h;d=h>=0?++g:--g){i=a[d];for(c in i)if(m.call(i,c)){if(e=this.fields.indexOf(this._sanitizeField(c,b)),d>0&&-1===e)throw new Error("All fields in subsequent rows must match the fields in the first row");-1===e&&(this.fields.push(this._sanitizeField(c,b)),e=this.fields.length-1),f=this._sanitizeValue(a[d][c]),Array.isArray(this.values[d])?(this.values[d][e]=f,this.fieldOptions[d][e]=b):(this.values[d]=[f],this.fieldOptions[d]=[b])}}return this},b.prototype.buildStr=function(){throw new Error("Not yet implemented")},b.prototype.buildParam=function(){throw new Error("Not yet implemented")},b}(a.Block),a.SetFieldBlock=function(a){function b(){return i=b.__super__.constructor.apply(this,arguments)}return n(b,a),b.prototype.set=function(a,b,c){return this._set(a,b,c)},b.prototype.setFields=function(a,b){return this._setFields(a,b)},b.prototype.buildStr=function(){var a,b,c,d,e,f,g;if(0>=this.fields.length)throw new Error("set() needs to be called");for(d="",c=f=0,g=this.fields.length;g>=0?g>f:f>g;c=g>=0?++f:--f)a=this.fields[c],""!==d&&(d+=", "),e=this.values[0][c],b=this.fieldOptions[0][c],d+="undefined"==typeof e?a:""+a+" = "+this._formatValue(e,b);return"SET "+d},b.prototype.buildParam=function(){var a,b,c,d,e,f,g,h,i,j,k,l;if(0>=this.fields.length)throw new Error("set() needs to be called");for(d="",f=[],b=h=0,k=this.fields.length;k>=0?k>h:h>k;b=k>=0?++h:--h)if(a=this.fields[b],""!==d&&(d+=", "),g=this.values[0][b],"undefined"==typeof g)d+=a;else if(c=this._formatValueAsParam(g),null!=(null!=c?c.text:void 0))for(d+=""+a+" = ("+c.text+")",l=c.values,i=0,j=l.length;j>i;i++)e=l[i],f.push(e);else d+=""+a+" = ?",f.push(c);return{text:"SET "+d,values:f}},b}(a.AbstractSetFieldBlock),a.InsertFieldValueBlock=function(a){function b(){return j=b.__super__.constructor.apply(this,arguments)}return n(b,a),b.prototype.set=function(a,b,c){return null==c&&(c={}),this._set(a,b,c)},b.prototype.setFields=function(a,b){return this._setFields(a,b)},b.prototype.setFieldsRows=function(a,b){return this._setFieldsRows(a,b)},b.prototype._buildVals=function(){var a,b,c,d,e,f,g,h;for(d=[],b=e=0,g=this.values.length;g>=0?g>e:e>g;b=g>=0?++e:--e)for(c=f=0,h=this.values[b].length;h>=0?h>f:f>h;c=h>=0?++f:--f)a=this._formatValue(this.values[b][c],this.fieldOptions[b][c]),"string"==typeof d[b]?d[b]+=", "+a:d[b]=""+a;return d},b.prototype._buildValParams=function(){var a,b,c,d,e,f,g,h,i,j,k,l,m,n;for(g=[],d=[],a=h=0,l=this.values.length;l>=0?l>h:h>l;a=l>=0?++h:--h)for(b=i=0,m=this.values[a].length;m>=0?m>i:i>m;b=m>=0?++i:--i){if(c=this._formatValueAsParam(this.values[a][b]),null!=(null!=c?c.text:void 0))for(e=c.text,n=c.values,j=0,k=n.length;k>j;j++)f=n[j],d.push(f);else e="?",d.push(c);"string"==typeof g[a]?g[a]+=", "+e:g[a]=""+e}return{vals:g,params:d}},b.prototype.buildStr=function(){if(0>=this.fields.length)throw new Error("set() needs to be called");return"("+this.fields.join(", ")+") VALUES ("+this._buildVals().join("), (")+")"},b.prototype.buildParam=function(){var a,b,c,d,e,f,g;if(0>=this.fields.length)throw new Error("set() needs to be called");for(c="",f=this._buildValParams(),d=f.vals,b=f.params,a=e=0,g=this.fields.length;g>=0?g>e:e>g;a=g>=0?++e:--e)""!==c&&(c+=", "),c+=this.fields[a];return{text:"("+c+") VALUES ("+d.join("), (")+")",values:b}},b}(a.AbstractSetFieldBlock),a.DistinctBlock=function(a){function b(a){b.__super__.constructor.call(this,a),this.useDistinct=!1}return n(b,a),b.prototype.distinct=function(){return this.useDistinct=!0},b.prototype.buildStr=function(){return this.useDistinct?"DISTINCT":""},b}(a.Block),a.GroupByBlock=function(a){function b(a){b.__super__.constructor.call(this,a),this.groups=[]}return n(b,a),b.prototype.group=function(a){return a=this._sanitizeField(a),this.groups.push(a)},b.prototype.buildStr=function(){var a,b,c,d,e;if(b="",0c;c++)a=e[c],""!==b&&(b+=", "),b+=a;b="GROUP BY "+b}return b},b}(a.Block),a.OffsetBlock=function(a){function b(a){b.__super__.constructor.call(this,a),this.offsets=null}return n(b,a),b.prototype.offset=function(a){return a=this._sanitizeLimitOffset(a),this.offsets=a},b.prototype.buildStr=function(){return this.offsets?"OFFSET "+this.offsets:""},b}(a.Block),a.WhereBlock=function(b){function c(a){c.__super__.constructor.call(this,a),this.wheres=[]}return n(c,b),c.prototype.where=function(){var b,c,d,e,f,g,h,i,j,k,m,n,o,p;if(c=arguments[0],k=2<=arguments.length?l.call(arguments,1):[],c=this._sanitizeCondition(c),d="",e=[],c instanceof a.Expression)j=c.toParam(),d=j.text,e=j.values;else for(f=m=0,p=c.length;p>=0?p>m:m>p;f=p>=0?++m:--m)if(b=c.charAt(f),"?"===b&&0n;n++)h=i[n],g.push(this._sanitizeValue(h));e=e.concat(g),d+="("+function(){var a,b,c;for(c=[],a=0,b=g.length;b>a;a++)h=g[a],c.push("?");return c}().join(", ")+")"}else d+="?",e.push(this._sanitizeValue(i));else d+=b;return""!==d?this.wheres.push({text:d,values:e}):void 0},c.prototype.buildStr=function(){var a,b,c,d,e,f,g,h,i,j;if(0>=this.wheres.length)return"";for(e="",i=this.wheres,f=0,h=i.length;h>f;f++)if(d=i[f],""!==e&&(e+=") AND ("),0=0?j>g:g>j;b=j>=0?++g:--g)a=d.text.charAt(b),e+="?"===a?this._formatValue(d.values[c++]):a;else e+=d.text;return"WHERE ("+e+")"},c.prototype.buildParam=function(){var a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q;if(d={text:"",values:[]},0>=this.wheres.length)return d;for(h="",o=this.wheres,i=0,l=o.length;l>i;i++){for(g=o[i],""!==h&&(h+=") AND ("),e=g.text.split("?"),a=0,p=g.values,j=0,m=p.length;m>j;j++){if(f=p[j],null!=e[a]&&(h+=""+e[a]),b=this._formatValueAsParam(f),null!=(null!=b?b.text:void 0))for(h+="("+b.text+")",q=b.values,k=0,n=q.length;n>k;k++)c=q[k],d.values.push(c);else h+="?",d.values.push(b);a+=1}null!=e[a]&&(h+=""+e[a])}return d.text="WHERE ("+h+")",d},c}(a.Block),a.OrderByBlock=function(a){function b(a){b.__super__.constructor.call(this,a),this.orders=[],this._values=[]}return n(b,a),b.prototype.order=function(){var a,b,c;return b=arguments[0],a=arguments[1],c=3<=arguments.length?l.call(arguments,2):[],null==a&&(a=!0),b=this._sanitizeField(b),this._values=c,this.orders.push({field:b,dir:a?!0:!1})},b.prototype._buildStr=function(a){var b,c,d,e,f,g,h,i,j,k,l;if(null==a&&(a=!1),0h;h++){if(e=k[h],""!==f&&(f+=", "),c="",a)c=e.field;else for(d=i=0,l=e.field.length;l>=0?l>i:i>l;d=l>=0?++i:--i)b=e.field.charAt(d),c+="?"===b?this._formatValue(this._values[g++]):b;f+=""+c+" "+(e.dir?"ASC":"DESC")}return"ORDER BY "+f}return""},b.prototype.buildStr=function(){return this._buildStr()},b.prototype.buildParam=function(){var a=this;return{text:this._buildStr(!0),values:this._values.map(function(b){return a._formatValueAsParam(b)})}},b}(a.Block),a.LimitBlock=function(a){function b(a){b.__super__.constructor.call(this,a),this.limits=null}return n(b,a),b.prototype.limit=function(a){return a=this._sanitizeLimitOffset(a),this.limits=a},b.prototype.buildStr=function(){return this.limits?"LIMIT "+this.limits:""},b}(a.Block),a.JoinBlock=function(b){function c(a){c.__super__.constructor.call(this,a),this.joins=[]}return n(c,b),c.prototype.join=function(a,b,c,d){return null==b&&(b=null),null==c&&(c=null),null==d&&(d="INNER"),a=this._sanitizeTable(a,!0),b&&(b=this._sanitizeTableAlias(b)),c&&(c=this._sanitizeCondition(c)),this.joins.push({type:d,table:a,alias:b,condition:c}),this},c.prototype.left_join=function(a,b,c){return null==b&&(b=null),null==c&&(c=null),this.join(a,b,c,"LEFT")},c.prototype.right_join=function(a,b,c){return null==b&&(b=null),null==c&&(c=null),this.join(a,b,c,"RIGHT")},c.prototype.outer_join=function(a,b,c){return null==b&&(b=null),null==c&&(c=null),this.join(a,b,c,"OUTER")},c.prototype.left_outer_join=function(a,b,c){return null==b&&(b=null),null==c&&(c=null),this.join(a,b,c,"LEFT OUTER")},c.prototype.full_join=function(a,b,c){return null==b&&(b=null),null==c&&(c=null),this.join(a,b,c,"FULL")},c.prototype.buildStr=function(){var a,b,c,d,e;for(b="",e=this.joins||[],c=0,d=e.length;d>c;c++)a=e[c],""!==b&&(b+=" "),b+=""+a.type+" JOIN ",b+="string"==typeof a.table?a.table:"("+a.table+")",a.alias&&(b+=" "+a.alias),a.condition&&(b+=" ON ("+a.condition+")");return b},c.prototype.buildParam=function(b){var c,d,e,f,g,h,i,j,k,l,m,n,o,p,q;if(h={text:"",values:[]},g=[],e="",0>=this.joins.length)return h;for(p=this.joins,j=0,m=p.length;m>j;j++)c=p[j],"string"==typeof c.table?f={text:""+c.table,values:[]}:c.table instanceof a.QueryBuilder?(c.table.updateOptions({nestedBuilder:!0}),f=c.table.toParam()):(c.updateOptions({nestedBuilder:!0}),f=c.buildParam(b)),c.condition instanceof a.Expression?(d=c.condition.toParam(),f.condition=d.text,f.values=f.values.concat(d.values)):f.condition=c.condition,f.join=c,g.push(f);for(k=0,n=g.length;n>k;k++)for(f=g[k],""!==e&&(e+=" "),e+=""+f.join.type+" JOIN ",e+="string"==typeof f.join.table?f.text:"("+f.text+")",f.join.alias&&(e+=" "+f.join.alias),f.condition&&(e+=" ON ("+f.condition+")"),q=f.values,l=0,o=q.length;o>l;l++)i=q[l],h.values.push(this._formatCustomValue(i));return h.text+=e,h},c}(a.Block),a.UnionBlock=function(b){function c(a){c.__super__.constructor.call(this,a),this.unions=[]}return n(c,b),c.prototype.union=function(a,b){return null==b&&(b="UNION"),a=this._sanitizeTable(a,!0),this.unions.push({type:b,table:a}),this},c.prototype.union_all=function(a){return this.union(a,"UNION ALL")},c.prototype.buildStr=function(){var a,b,c,d,e;for(b="",e=this.unions||[],c=0,d=e.length;d>c;c++)a=e[c],""!==b&&(b+=" "),b+=""+a.type+" ",b+="string"==typeof a.table?a.table:"("+a.table+")";return b},c.prototype.buildParam=function(b){var c,d,e,f,g,h,i,j,k,l,m,n,o,p;if(f={text:"",values:[]},e=[],g="",0>=this.unions.length)return f;for(o=this.unions||[],i=0,l=o.length;l>i;i++)c=o[i],"string"==typeof c.table?d={text:""+c.table,values:[]}:c.table instanceof a.QueryBuilder?(c.table.updateOptions({nestedBuilder:!0}),d=c.table.toParam()):(c.updateOptions({nestedBuilder:!0}),d=c.buildParam(b)),d.type=c.type,e.push(d);for(j=0,m=e.length;m>j;j++)for(d=e[j],""!==g&&(g+=" "),g+=""+d.type+" ("+d.text+")",p=d.values,k=0,n=p.length;n>k;k++)h=p[k],f.values.push(this._formatCustomValue(h));return f.text+=g,f},c}(a.Block),a.QueryBuilder=function(a){function b(a,c){var d,e,f,g,h,i,j,k,l=this;for(b.__super__.constructor.call(this,a),this.blocks=c||[],j=this.blocks,h=0,i=j.length;i>h;h++){d=j[h],k=d.exposedMethods(),g=function(a,b,c){return l[b]=function(){return c.apply(a,arguments),l}};for(f in k){if(e=k[f],null!=this[f])throw new Error(""+this._getObjectClassName(this)+" already has a builder method called: "+f);g(d,f,e)}}}return n(b,a),b.prototype.registerValueHandler=function(a,c){var d,e,f,g;for(g=this.blocks,e=0,f=g.length;f>e;e++)d=g[e],d.registerValueHandler(a,c);return b.__super__.registerValueHandler.call(this,a,c),this},b.prototype.updateOptions=function(a){var b,c,d,f,g;for(this.options=e({},this.options,a),f=this.blocks,g=[],c=0,d=f.length;d>c;c++)b=f[c],g.push(b.options=e({},b.options,a));return g},b.prototype.toString=function(){var a;return function(){var b,c,d,e;for(d=this.blocks,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.buildStr(this));return e}.call(this).filter(function(a){return 0a;a++)b=d[a],e.push(b.buildParam(this));return e}.call(this),g.text=function(){var a,d,e;for(e=[],a=0,d=c.length;d>a;a++)b=c[a],e.push(b.text);return e}().filter(function(a){return 0a;a++)b=c[a],e.push(b.values);return e}()),null==this.options.nestedBuilder&&(this.options.numberedParameters||null!=(null!=a?a.numberedParametersStartAt:void 0))&&(d=1,null!=this.options.numberedParametersStartAt&&(d=this.options.numberedParametersStartAt),g.text=g.text.replace(/\?/g,function(){return"$"+d++})),this.options=f,g},b.prototype.clone=function(){var a;return new this.constructor(this.options,function(){var b,c,d,e;for(d=this.blocks,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.clone());return e}.call(this))},b.prototype.isNestable=function(){return!1},b}(a.BaseBuilder),a.Select=function(b){function c(b,d){null==d&&(d=null),d||(d=[new a.StringBlock(b,"SELECT"),new a.DistinctBlock(b),new a.GetFieldBlock(b),new a.FromTableBlock(e({},b,{allowNested:!0})),new a.JoinBlock(e({},b,{allowNested:!0})),new a.WhereBlock(b),new a.GroupByBlock(b),new a.OrderByBlock(b),new a.LimitBlock(b),new a.OffsetBlock(b),new a.UnionBlock(e({},b,{allowNested:!0}))]),c.__super__.constructor.call(this,b,d)}return n(c,b),c.prototype.isNestable=function(){return!0},c}(a.QueryBuilder),a.Update=function(b){function c(b,d){null==d&&(d=null),d||(d=[new a.StringBlock(b,"UPDATE"),new a.UpdateTableBlock(b),new a.SetFieldBlock(b),new a.WhereBlock(b),new a.OrderByBlock(b),new a.LimitBlock(b)]),c.__super__.constructor.call(this,b,d)}return n(c,b),c}(a.QueryBuilder),a.Delete=function(b){function c(b,d){null==d&&(d=null),d||(d=[new a.StringBlock(b,"DELETE"),new a.FromTableBlock(e({},b,{singleTable:!0})),new a.JoinBlock(b),new a.WhereBlock(b),new a.OrderByBlock(b),new a.LimitBlock(b)]),c.__super__.constructor.call(this,b,d)}return n(c,b),c}(a.QueryBuilder),a.Insert=function(b){function c(b,d){null==d&&(d=null),d||(d=[new a.StringBlock(b,"INSERT"),new a.IntoTableBlock(b),new a.InsertFieldValueBlock(b)]),c.__super__.constructor.call(this,b,d)}return n(c,b),c}(a.QueryBuilder),d={VERSION:"3.10.0",expr:function(){return new a.Expression},select:function(b,c){return new a.Select(b,c)},update:function(b,c){return new a.Update(b,c)},insert:function(b,c){return new a.Insert(b,c)},"delete":function(b,c){return new a.Delete(b,c)},registerValueHandler:a.registerValueHandler},d.remove=d["delete"],d.cls=a,("undefined"!=typeof define&&null!==define?define.amd:void 0)?define(function(){return d}):("undefined"!=typeof module&&null!==module?module.exports:void 0)?module.exports=d:"undefined"!=typeof window&&null!==window&&(window.squel=d),d.flavours={},d.useFlavour=function(a){if(!(d.flavours[a]instanceof Function))throw new Error("Flavour not available: "+a);return d.flavours[a].call(null,d),d},d.flavours.postgres=function(){return a=d.cls,a.DefaultQueryBuilderOptions.numberedParameters=!0,a.DefaultQueryBuilderOptions.numberedParametersStartAt=1,a.ReturningBlock=function(a){function b(a){b.__super__.constructor.call(this,a),this._str=null}return n(b,a),b.prototype.returning=function(a){return this._str=this._sanitizeField(a)},b.prototype.buildStr=function(){return this._str?"RETURNING "+this._str:""},b}(a.Block),a.Insert=function(b){function c(b,d){null==d&&(d=null),d||(d=[new a.StringBlock(b,"INSERT"),new a.IntoTableBlock(b),new a.InsertFieldValueBlock(b),new a.ReturningBlock(b)]),c.__super__.constructor.call(this,b,d)}return n(c,b),c}(a.QueryBuilder),a.Update=function(b){function c(b,d){null==d&&(d=null),d||(d=[new a.StringBlock(b,"UPDATE"),new a.UpdateTableBlock(b),new a.SetFieldBlock(b),new a.WhereBlock(b),new a.OrderByBlock(b),new a.LimitBlock(b),new a.ReturningBlock(b)]),c.__super__.constructor.call(this,b,d)}return n(c,b),c}(a.QueryBuilder),a.Delete=function(b){function c(b,d){null==d&&(d=null),d||(d=[new a.StringBlock(b,"DELETE"),new a.FromTableBlock(e({},b,{singleTable:!0})),new a.JoinBlock(b),new a.WhereBlock(b),new a.OrderByBlock(b),new a.LimitBlock(b),new a.ReturningBlock(b)]),c.__super__.constructor.call(this,b,d)}return n(c,b),c}(a.QueryBuilder)},d.flavours.mysql=function(){var b,c;return a=d.cls,a.TargetTableBlock=function(a){function c(){return b=c.__super__.constructor.apply(this,arguments)}return n(c,a),c.prototype.target=function(a){return this._setValue(this._sanitizeTable(a))},c}(a.AbstractValueBlock),a.MysqlOnDuplicateKeyUpdateBlock=function(a){function b(){return c=b.__super__.constructor.apply(this,arguments)}return n(b,a),b.prototype.onDupUpdate=function(a,b,c){return this._set(a,b,c)},b.prototype.buildStr=function(){var a,b,c,d,e,f,g;for(d="",c=f=0,g=this.fields.length;g>=0?g>f:f>g;c=g>=0?++f:--f)a=this.fields[c],""!==d&&(d+=", "),e=this.values[0][c],b=this.fieldOptions[0][c],d+="undefined"==typeof e?a:""+a+" = "+this._formatValue(e,b);return""===d?"":"ON DUPLICATE KEY UPDATE "+d},b.prototype.buildParam=function(){var a,b,c,d,e,f,g;for(c="",d=[],b=f=0,g=this.fields.length;g>=0?g>f:f>g;b=g>=0?++f:--f)a=this.fields[b],""!==c&&(c+=", "),e=this.values[0][b],"undefined"==typeof e?c+=a:(c+=""+a+" = ?",d.push(this._formatValueAsParam(e)));return{text:""===c?"":"ON DUPLICATE KEY UPDATE "+c,values:d}},b}(a.AbstractSetFieldBlock),a.Insert=function(b){function c(b,d){null==d&&(d=null),d||(d=[new a.StringBlock(b,"INSERT"),new a.IntoTableBlock(b),new a.InsertFieldValueBlock(b),new a.MysqlOnDuplicateKeyUpdateBlock(b)]),c.__super__.constructor.call(this,b,d)}return n(c,b),c}(a.QueryBuilder),a.Delete=function(b){function c(b,d){null==d&&(d=null),d||(d=[new a.StringBlock(b,"DELETE"),new a.TargetTableBlock(b),new a.FromTableBlock(e({},b,{singleTable:!0})),new a.JoinBlock(b),new a.WhereBlock(b),new a.OrderByBlock(b),new a.LimitBlock(b)]),c.__super__.constructor.call(this,b,d)}return n(c,b),c}(a.QueryBuilder)},e=function(){var a,b,c,d,e,f,g;if(a=arguments[0],c=2<=arguments.length?l.call(arguments,1):[])for(f=0,g=c.length;g>f;f++)if(d=c[f])for(b in d)m.call(d,b)&&(e=d[b],a[b]=e);return a},d.flavours.mssql=function(){return a=d.cls,a.DefaultQueryBuilderOptions.replaceSingleQuotes=!0,a.DefaultQueryBuilderOptions.autoQuoteAliasNames=!1,d.registerValueHandler(Date,function(a){return""+a.getUTCFullYear()+"-"+(a.getUTCMonth()+1)+"-"+a.getUTCDate()+" "+a.getUTCHours()+":"+a.getUTCMinutes()+":"+a.getUTCSeconds()}),a.MssqlLimitOffsetTopBlock=function(b){function c(a){c.__super__.constructor.call(this,a),this.limits=null,this.offsets=null}var d,e,f,g,h,i,j,k;return n(c,b),h=function(a){return a=this._sanitizeLimitOffset(a),this._parent.limits=a},f=function(a){function b(a){b.__super__.constructor.call(this,a.options),this._parent=a}return n(b,a),b}(a.Block),d=function(a){function b(){return i=b.__super__.constructor.apply(this,arguments)}return n(b,a),b.prototype.limit=h,b.prototype.buildStr=function(){return this._parent.limits&&this._parent.offsets?"FETCH NEXT "+this._parent.limits+" ROWS ONLY":""},b}(f),g=function(a){function b(){return j=b.__super__.constructor.apply(this,arguments)}return n(b,a),b.prototype.top=h,b.prototype.buildStr=function(){return this._parent.limits&&!this._parent.offsets?"TOP ("+this._parent.limits+")":""},b}(f),e=function(a){function b(){return this.offset=o(this.offset,this),k=b.__super__.constructor.apply(this,arguments)}return n(b,a),b.prototype.offset=function(a){return a=this._sanitizeLimitOffset(a),this._parent.offsets=a},b.prototype.buildStr=function(){return this._parent.offsets?"OFFSET "+this._parent.offsets+" ROWS":""},b}(f),c.prototype.LIMIT=function(a){return this.constructor(a),new d(this)},c.prototype.TOP=function(a){return this.constructor(a),new g(this)},c.prototype.OFFSET=function(a){return this.constructor(a),new e(this)},c}.call(this,a.Block),a.MssqlUpdateTopBlock=function(a){function b(a){b.__super__.constructor.call(this,a),this.limits=null}var c;return n(b,a),c=function(a){return a=this._sanitizeLimitOffset(a),this.limits=a},b.prototype.limit=c,b.prototype.top=c,b.prototype.buildStr=function(){return this.limits?"TOP ("+this.limits+")":"" -},b}(a.Block),a.MssqlInsertFieldValueBlock=function(a){function b(a){b.__super__.constructor.call(this,a),this.outputs=[]}return n(b,a),b.prototype.output=function(a){var b,c,d,e;if("string"==typeof a)return this.outputs.push("INSERTED."+this._sanitizeField(a));for(e=[],c=0,d=a.length;d>c;c++)b=a[c],e.push(this.outputs.push("INSERTED."+this._sanitizeField(b)));return e},b.prototype.buildStr=function(){if(0>=this.fields.length)throw new Error("set() needs to be called");return"("+this.fields.join(", ")+") "+(0!==this.outputs.length?"OUTPUT "+this.outputs.join(", ")+" ":"")+"VALUES ("+this._buildVals().join("), (")+")"},b.prototype.buildParam=function(){var a,b,c,d,e,f,g;if(0>=this.fields.length)throw new Error("set() needs to be called");for(c="",f=this._buildValParams(),d=f.vals,b=f.params,a=e=0,g=this.fields.length;g>=0?g>e:e>g;a=g>=0?++e:--e)""!==c&&(c+=", "),c+=this.fields[a];return{text:"("+c+") "+(0!==this.outputs.length?"OUTPUT "+this.outputs.join(", ")+" ":"")+"VALUES ("+d.join("), (")+")",values:b}},b}(a.InsertFieldValueBlock),a.MssqlUpdateDeleteOutputBlock=function(a){function b(a){b.__super__.constructor.call(this,a),this._outputs=[]}return n(b,a),b.prototype.outputs=function(a){var b,c,d;d=[];for(c in a)b=a[c],d.push(this.output(c,b));return d},b.prototype.output=function(a,b){return null==b&&(b=null),a=this._sanitizeField(a),b&&(b=this._sanitizeFieldAlias(b)),this._outputs.push({name:"INSERTED."+a,alias:b})},b.prototype.buildStr=function(){var a,b,c,d,e;if(b="",this._outputs.length>0){for(e=this._outputs,c=0,d=e.length;d>c;c++)a=e[c],""!==b&&(b+=", "),b+=a.name,a.alias&&(b+=" AS "+a.alias);b="OUTPUT "+b}return b},b}(a.Block),a.Select=function(b){function c(b,d){var f;null==d&&(d=null),f=new a.MssqlLimitOffsetTopBlock(b),d||(d=[new a.StringBlock(b,"SELECT"),new a.DistinctBlock(b),f.TOP(b),new a.GetFieldBlock(b),new a.FromTableBlock(e({},b,{allowNested:!0})),new a.JoinBlock(e({},b,{allowNested:!0})),new a.WhereBlock(b),new a.GroupByBlock(b),new a.OrderByBlock(b),f.OFFSET(b),f.LIMIT(b),new a.UnionBlock(e({},b,{allowNested:!0}))]),c.__super__.constructor.call(this,b,d)}return n(c,b),c.prototype.isNestable=function(){return!0},c}(a.QueryBuilder),a.Update=function(b){function c(b,d){null==d&&(d=null),d||(d=[new a.StringBlock(b,"UPDATE"),new a.MssqlUpdateTopBlock(b),new a.UpdateTableBlock(b),new a.SetFieldBlock(b),new a.MssqlUpdateDeleteOutputBlock(b),new a.WhereBlock(b)]),c.__super__.constructor.call(this,b,d)}return n(c,b),c}(a.QueryBuilder),a.Delete=function(b){function c(b,d){null==d&&(d=null),d||(d=[new a.StringBlock(b,"DELETE"),new a.FromTableBlock(e({},b,{singleTable:!0})),new a.JoinBlock(b),new a.MssqlUpdateDeleteOutputBlock(b),new a.WhereBlock(b),new a.OrderByBlock(b),new a.LimitBlock(b)]),c.__super__.constructor.call(this,b,d)}return n(c,b),c}(a.QueryBuilder),a.Insert=function(b){function c(b,d){null==d&&(d=null),d||(d=[new a.StringBlock(b,"INSERT"),new a.IntoTableBlock(b),new a.MssqlInsertFieldValueBlock(b)]),c.__super__.constructor.call(this,b,d)}return n(c,b),c}(a.QueryBuilder)}}).call(this); \ No newline at end of file +/*! squel | https://github.com/hiddentao/squel | BSD license */(function(){var a,b,c,d,e,f,g,h,i,j,k,l,m=[].slice,n={}.hasOwnProperty,o=function(a,b){return function(){return a.apply(b,arguments)}},p=function(a,b){function c(){this.constructor=a}for(var d in b)n.call(b,d)&&(a[d]=b[d]);return c.prototype=b.prototype,a.prototype=new c,a.__super__=b.prototype,a};a={},e=function(){var a,b,c,d,e,f,g;if(a=arguments[0],c=2<=arguments.length?m.call(arguments,1):[])for(f=0,g=c.length;g>f;f++)if(d=c[f])for(b in d)n.call(d,b)&&(e=d[b],a[b]=e);return a},l=function(){var a,b,c,d,f,g;for(b=arguments[0],d=2<=arguments.length?m.call(arguments,1):[],a=e({},b),f=0,g=d.length;g>f;f++)c=d[f],delete a[c];return a},a.DefaultQueryBuilderOptions={autoQuoteTableNames:!1,autoQuoteFieldNames:!1,autoQuoteAliasNames:!0,nameQuoteCharacter:"`",tableAliasQuoteCharacter:"`",fieldAliasQuoteCharacter:'"',valueHandlers:[],numberedParameters:!1,numberedParametersStartAt:1,replaceSingleQuotes:!1,singleQuoteReplacement:"''",separator:" "},a.globalValueHandlers=[],c=function(a,b,c){var d,e,f;if("function"!=typeof b&&"string"!=typeof b)throw new Error("type must be a class constructor or string denoting 'typeof' result");if("function"!=typeof c)throw new Error("handler must be a function");for(e=0,f=a.length;f>e;e++)if(d=a[e],d.type===b)return void(d.handler=c);return a.push({type:b,handler:c})},b=function(){var a,b,c,d,e,f,g,h;for(d=arguments[0],a=2<=arguments.length?m.call(arguments,1):[],e=0,g=a.length;g>e;e++)for(b=a[e],f=0,h=b.length;h>f;f++)if(c=b[f],c.type===typeof d||"string"!=typeof c.type&&d instanceof c.type)return c.handler;return void 0},a.registerValueHandler=function(b,d){return c(a.globalValueHandlers,b,d)},a.Cloneable=function(){function a(){}return a.prototype.clone=function(){var a;return a=new this.constructor,e(a,JSON.parse(JSON.stringify(this)))},a}(),a.BaseBuilder=function(d){function f(b){this._sanitizeNestableQuery=o(this._sanitizeNestableQuery,this);var c;c=JSON.parse(JSON.stringify(a.DefaultQueryBuilderOptions)),this.options=e({},c,b)}return p(f,d),f.prototype.registerValueHandler=function(a,b){return c(this.options.valueHandlers,a,b),this},f.prototype._getObjectClassName=function(a){var b;return a&&a.constructor&&a.constructor.toString&&(b=a.constructor.toString().match(/function\s*(\w+)/),b&&2===b.length)?b[1]:void 0},f.prototype._sanitizeCondition=function(b){if(!(b instanceof a.Expression)&&"string"!=typeof b)throw new Error("condition must be a string or Expression instance");return b},f.prototype._sanitizeName=function(a,b){if("string"!=typeof a)throw new Error(""+b+" must be a string");return a},f.prototype._sanitizeField=function(b,c){var d;return null==c&&(c={}),b instanceof a.QueryBuilder?b="("+b+")":(b=this._sanitizeName(b,"field name"),this.options.autoQuoteFieldNames&&(d=this.options.nameQuoteCharacter,b=c.ignorePeriodsForFieldNameQuotes?""+d+b+d:b.split(".").map(function(a){return"*"===a?a:""+d+a+d}).join("."))),b},f.prototype._sanitizeNestableQuery=function(b){if(b instanceof a.QueryBuilder&&b.isNestable())return b;throw new Error("must be a nestable query, e.g. SELECT")},f.prototype._sanitizeTable=function(a,b){var c,d;if(null==b&&(b=!1),b)if("string"==typeof a)d=a;else try{d=this._sanitizeNestableQuery(a)}catch(e){throw c=e,new Error("table name must be a string or a nestable query instance")}else d=this._sanitizeName(a,"table name");return this.options.autoQuoteTableNames?""+this.options.nameQuoteCharacter+d+this.options.nameQuoteCharacter:d},f.prototype._sanitizeTableAlias=function(a){var b;return b=this._sanitizeName(a,"table alias"),this.options.autoQuoteAliasNames?""+this.options.tableAliasQuoteCharacter+b+this.options.tableAliasQuoteCharacter:b},f.prototype._sanitizeFieldAlias=function(a){var b;return b=this._sanitizeName(a,"field alias"),this.options.autoQuoteAliasNames?""+this.options.fieldAliasQuoteCharacter+b+this.options.fieldAliasQuoteCharacter:b},f.prototype._sanitizeLimitOffset=function(a){if(a=parseInt(a),0>a||isNaN(a))throw new Error("limit/offset must be >= 0");return a},f.prototype._sanitizeValue=function(c){var d,e;if(d=typeof c,null===c);else if("string"===d||"number"===d||"boolean"===d);else if(c instanceof a.QueryBuilder&&c.isNestable());else if(e=void 0!==b(c,this.options.valueHandlers,a.globalValueHandlers),!e)throw new Error("field value must be a string, number, boolean, null or one of the registered custom value types");return c},f.prototype._escapeValue=function(a){return!0!==this.options.replaceSingleQuotes?a:a.replace(/\'/g,this.options.singleQuoteReplacement)},f.prototype._formatCustomValue=function(c){var d;return d=b(c,this.options.valueHandlers,a.globalValueHandlers),d&&(c=d(c)),c},f.prototype._formatValueAsParam=function(b){var c,d=this;return Array.isArray(b)?b.map(function(a){return d._formatValueAsParam(a)}):b instanceof a.QueryBuilder&&b.isNestable()?(b.updateOptions({nestedBuilder:!0}),c=b.toParam()):b instanceof a.Expression?c=b.toParam():this._formatCustomValue(b)},f.prototype._formatValue=function(b,c){var d=this;return null==c&&(c={}),b=this._formatCustomValue(b),Array.isArray(b)?(b=b.map(function(a){return d._formatValue(a)}),b="("+b.join(", ")+")"):null===b?b="NULL":"boolean"==typeof b?b=b?"TRUE":"FALSE":b instanceof a.QueryBuilder?b="("+b+")":b instanceof a.Expression?b="("+b+")":"number"!=typeof b&&(b=this._escapeValue(b),b=c.dontQuote?""+b:"'"+b+"'"),b},f}(a.Cloneable),a.Expression=function(a){function b(){var a=this;b.__super__.constructor.call(this),this.tree={parent:null,nodes:[]},this.current=this.tree,this._begin=function(b){var c;return c={type:b,parent:a.current,nodes:[]},a.current.nodes.push(c),a.current=a.current.nodes[a.current.nodes.length-1],a}}return p(b,a),b.prototype.tree=null,b.prototype.current=null,b.prototype.and_begin=function(){return this._begin("AND")},b.prototype.or_begin=function(){return this._begin("OR")},b.prototype.end=function(){if(!this.current.parent)throw new Error("begin() needs to be called");return this.current=this.current.parent,this},b.prototype.and=function(a,b){if(!a||"string"!=typeof a)throw new Error("expr must be a string");return this.current.nodes.push({type:"AND",expr:a,para:b}),this},b.prototype.or=function(a,b){if(!a||"string"!=typeof a)throw new Error("expr must be a string");return this.current.nodes.push({type:"OR",expr:a,para:b}),this},b.prototype.toString=function(){if(null!==this.current.parent)throw new Error("end() needs to be called");return this._toString(this.tree)},b.prototype.toParam=function(){if(null!==this.current.parent)throw new Error("end() needs to be called");return this._toString(this.tree,!0)},b.prototype._toString=function(a,b){var c,d,e,f,g,h,i,j,k;for(null==b&&(b=!1),h="",g=[],k=a.nodes,i=0,j=k.length;j>i;i++)c=k[i],null!=c.expr?(f=c.expr,void 0!==c.para&&(b?(d=this._formatValueAsParam(c.para),null!=(null!=d?d.text:void 0)?(g=g.concat(d.values),f=f.replace("?","("+d.text+")")):g=g.concat(d),Array.isArray(c.para)&&(e=Array.apply(null,new Array(c.para.length)).map(function(){return"?"}),f=f.replace("?","("+e.join(", ")+")"))):f=f.replace("?",this._formatValue(c.para)))):(f=this._toString(c,b),b&&(g=g.concat(f.values),f=f.text),""!==f&&(f="("+f+")")),""!==f&&(""!==h&&(h+=" "+c.type+" "),h+=f);return b?{text:h,values:g}:h},b.prototype.clone=function(){var a,b;return a=new this.constructor,(b=function(c){var d,e,f,g,h;for(g=c.nodes,h=[],e=0,f=g.length;f>e;e++)d=g[e],null!=d.expr?h.push(a.current.nodes.push(JSON.parse(JSON.stringify(d)))):(a._begin(d.type),b(d),h.push(!this.current===d?a.end():void 0));return h})(this.tree),a},b}(a.BaseBuilder),a.Block=function(b){function c(){return f=c.__super__.constructor.apply(this,arguments)}return p(c,b),c.prototype.exposedMethods=function(){var b,c,d;c={};for(b in this)d=this[b],"function"!=typeof d||"_"===b.charAt(0)||a.Block.prototype[b]||(c[b]=d);return c},c.prototype.buildStr=function(){return""},c.prototype.buildParam=function(a){return{text:this.buildStr(a),values:[]}},c}(a.BaseBuilder),a.StringBlock=function(a){function b(a,c){b.__super__.constructor.call(this,a),this.str=c}return p(b,a),b.prototype.buildStr=function(){return this.str},b}(a.Block),a.AbstractValueBlock=function(a){function b(a){b.__super__.constructor.call(this,a),this._val=null}return p(b,a),b.prototype._setValue=function(a){return this._val=a},b.prototype.buildStr=function(){return this._val?this._val:""},b}(a.Block),a.AbstractTableBlock=function(b){function c(a){c.__super__.constructor.call(this,a),this.tables=[]}return p(c,b),c.prototype._table=function(a,b){return null==b&&(b=null),b&&(b=this._sanitizeTableAlias(b)),a=this._sanitizeTable(a,this.options.allowNested||!1),this.options.singleTable&&(this.tables=[]),this.tables.push({table:a,alias:b})},c.prototype.buildStr=function(){var a,b,c,d,e;if(0>=this.tables.length)throw new Error("_table() needs to be called");for(b="",e=this.tables,c=0,d=e.length;d>c;c++)a=e[c],""!==b&&(b+=", "),b+="string"==typeof a.table?a.table:"("+a.table+")",a.alias&&(b+=" "+a.alias);return b},c.prototype._buildParam=function(b,c){var d,e,f,g,h,i,j,k,l,m,n,o,p,q;if(null==c&&(c=null),h={text:"",values:[]},g=[],f="",0>=this.tables.length)return h;for(p=this.tables,j=0,m=p.length;m>j;j++)d=p[j],"string"==typeof d.table?e={text:""+d.table,values:[]}:d.table instanceof a.QueryBuilder?(d.table.updateOptions({nestedBuilder:!0}),e=d.table.toParam()):(d.updateOptions({nestedBuilder:!0}),e=d.buildParam(b)),e.table=d,g.push(e);for(k=0,n=g.length;n>k;k++)for(e=g[k],""!==f?f+=", ":null!=c&&""!==c&&(f+=""+c+" "+f),f+="string"==typeof e.table.table?""+e.text:"("+e.text+")",null!=e.table.alias&&(f+=" "+e.table.alias),q=e.values,l=0,o=q.length;o>l;l++)i=q[l],h.values.push(this._formatCustomValue(i));return h.text+=f,h},c.prototype.buildParam=function(a){return this._buildParam(a)},c}(a.Block),a.UpdateTableBlock=function(a){function b(){return g=b.__super__.constructor.apply(this,arguments)}return p(b,a),b.prototype.table=function(a,b){return null==b&&(b=null),this._table(a,b)},b}(a.AbstractTableBlock),a.FromTableBlock=function(a){function b(){return h=b.__super__.constructor.apply(this,arguments)}return p(b,a),b.prototype.from=function(a,b){return null==b&&(b=null),this._table(a,b)},b.prototype.buildStr=function(a){var c;if(0>=this.tables.length)throw new Error("from() needs to be called");return c=b.__super__.buildStr.call(this,a),"FROM "+c},b.prototype.buildParam=function(a){if(0>=this.tables.length)throw new Error("from() needs to be called");return this._buildParam(a,"FROM")},b}(a.AbstractTableBlock),a.IntoTableBlock=function(a){function b(a){b.__super__.constructor.call(this,a),this.table=null}return p(b,a),b.prototype.into=function(a){return this.table=this._sanitizeTable(a,!1)},b.prototype.buildStr=function(){if(!this.table)throw new Error("into() needs to be called");return"INTO "+this.table},b}(a.Block),a.GetFieldBlock=function(a){function b(a){b.__super__.constructor.call(this,a),this._fieldAliases={},this._fields=[]}return p(b,a),b.prototype.fields=function(a,b){var c,d,e,f,g,h;if(null==b&&(b={}),Array.isArray(a)){for(g=[],e=0,f=a.length;f>e;e++)d=a[e],g.push(this.field(d,null,b));return g}h=[];for(d in a)c=a[d],h.push(this.field(d,c,b));return h},b.prototype.field=function(a,b,c){return null==b&&(b=null),null==c&&(c={}),a=this._sanitizeField(a,c),b&&(b=this._sanitizeFieldAlias(b)),this._fieldAliases[a]!==b?(this._fieldAliases[a]=b,this._fields.push({name:a,alias:b})):void 0},b.prototype.buildStr=function(){var a,b,c,d,e;for(b="",e=this._fields,c=0,d=e.length;d>c;c++)a=e[c],""!==b&&(b+=", "),b+=a.name,a.alias&&(b+=" AS "+a.alias);return""===b?"*":b},b}(a.Block),a.AbstractSetFieldBlock=function(a){function b(a){b.__super__.constructor.call(this,a),this.fieldOptions=[],this.fields=[],this.values=[]}return p(b,a),b.prototype._set=function(a,b,c){var d;if(null==c&&(c={}),this.values.length>1)throw new Error("Cannot call set or setFields on multiple rows of fields.");return void 0!==b&&(b=this._sanitizeValue(b)),d=this.fields.indexOf(this._sanitizeField(a,c)),-1!==d?(this.values[0][d]=b,this.fieldOptions[0][d]=c):(this.fields.push(this._sanitizeField(a,c)),d=this.fields.length-1,Array.isArray(this.values[0])?(this.values[0][d]=b,this.fieldOptions[0][d]=c):(this.values.push([b]),this.fieldOptions.push([c]))),this},b.prototype._setFields=function(a,b){var c;if(null==b&&(b={}),"object"!=typeof a)throw new Error("Expected an object but got "+typeof a);for(c in a)n.call(a,c)&&this._set(c,a[c],b);return this},b.prototype._setFieldsRows=function(a,b){var c,d,e,f,g,h,i;if(null==b&&(b={}),!Array.isArray(a))throw new Error("Expected an array of objects but got "+typeof a);for(this.fields=[],this.values=[],d=g=0,h=a.length;h>=0?h>g:g>h;d=h>=0?++g:--g){i=a[d];for(c in i)if(n.call(i,c)){if(e=this.fields.indexOf(this._sanitizeField(c,b)),d>0&&-1===e)throw new Error("All fields in subsequent rows must match the fields in the first row");-1===e&&(this.fields.push(this._sanitizeField(c,b)),e=this.fields.length-1),f=this._sanitizeValue(a[d][c]),Array.isArray(this.values[d])?(this.values[d][e]=f,this.fieldOptions[d][e]=b):(this.values[d]=[f],this.fieldOptions[d]=[b])}}return this},b.prototype.buildStr=function(){throw new Error("Not yet implemented")},b.prototype.buildParam=function(){throw new Error("Not yet implemented")},b}(a.Block),a.SetFieldBlock=function(a){function b(){return i=b.__super__.constructor.apply(this,arguments)}return p(b,a),b.prototype.set=function(a,b,c){return this._set(a,b,c)},b.prototype.setFields=function(a,b){return this._setFields(a,b)},b.prototype.buildStr=function(){var a,b,c,d,e,f,g;if(0>=this.fields.length)throw new Error("set() needs to be called");for(d="",c=f=0,g=this.fields.length;g>=0?g>f:f>g;c=g>=0?++f:--f)a=this.fields[c],""!==d&&(d+=", "),e=this.values[0][c],b=this.fieldOptions[0][c],d+="undefined"==typeof e?a:""+a+" = "+this._formatValue(e,b);return"SET "+d},b.prototype.buildParam=function(){var a,b,c,d,e,f,g,h,i,j,k,l;if(0>=this.fields.length)throw new Error("set() needs to be called");for(d="",f=[],b=h=0,k=this.fields.length;k>=0?k>h:h>k;b=k>=0?++h:--h)if(a=this.fields[b],""!==d&&(d+=", "),g=this.values[0][b],"undefined"==typeof g)d+=a;else if(c=this._formatValueAsParam(g),null!=(null!=c?c.text:void 0))for(d+=""+a+" = ("+c.text+")",l=c.values,i=0,j=l.length;j>i;i++)e=l[i],f.push(e);else d+=""+a+" = ?",f.push(c);return{text:"SET "+d,values:f}},b}(a.AbstractSetFieldBlock),a.InsertFieldValueBlock=function(a){function b(){return j=b.__super__.constructor.apply(this,arguments)}return p(b,a),b.prototype.set=function(a,b,c){return null==c&&(c={}),this._set(a,b,c)},b.prototype.setFields=function(a,b){return this._setFields(a,b)},b.prototype.setFieldsRows=function(a,b){return this._setFieldsRows(a,b)},b.prototype._buildVals=function(){var a,b,c,d,e,f,g,h;for(d=[],b=e=0,g=this.values.length;g>=0?g>e:e>g;b=g>=0?++e:--e)for(c=f=0,h=this.values[b].length;h>=0?h>f:f>h;c=h>=0?++f:--f)a=this._formatValue(this.values[b][c],this.fieldOptions[b][c]),"string"==typeof d[b]?d[b]+=", "+a:d[b]=""+a;return d},b.prototype._buildValParams=function(){var a,b,c,d,e,f,g,h,i,j,k,l,m,n;for(g=[],d=[],a=h=0,l=this.values.length;l>=0?l>h:h>l;a=l>=0?++h:--h)for(b=i=0,m=this.values[a].length;m>=0?m>i:i>m;b=m>=0?++i:--i){if(c=this._formatValueAsParam(this.values[a][b]),null!=(null!=c?c.text:void 0))for(e=c.text,n=c.values,j=0,k=n.length;k>j;j++)f=n[j],d.push(f);else e="?",d.push(c);"string"==typeof g[a]?g[a]+=", "+e:g[a]=""+e}return{vals:g,params:d}},b.prototype.buildStr=function(){if(0>=this.fields.length)throw new Error("set() needs to be called");return"("+this.fields.join(", ")+") VALUES ("+this._buildVals().join("), (")+")"},b.prototype.buildParam=function(){var a,b,c,d,e,f,g;if(0>=this.fields.length)throw new Error("set() needs to be called");for(c="",f=this._buildValParams(),d=f.vals,b=f.params,a=e=0,g=this.fields.length;g>=0?g>e:e>g;a=g>=0?++e:--e)""!==c&&(c+=", "),c+=this.fields[a];return{text:"("+c+") VALUES ("+d.join("), (")+")",values:b}},b}(a.AbstractSetFieldBlock),a.InsertFieldSelectQueryBlock=function(a){function b(){return k=b.__super__.constructor.apply(this,arguments)}return p(b,a),b.prototype.fromSelect=function(a,b){return this._fields=a,this._query=this._sanitizeNestableQuery(b)},b.prototype.buildStr=function(){if(0>=this.fields.length)throw new Error("set() needs to be called");return"("+this.fields.join(", ")+") VALUES ("+this._buildVals().join("), (")+")"},b.prototype.buildParam=function(){var a,b,c,d,e,f,g;if(0>=this.fields.length)throw new Error("set() needs to be called");for(c="",f=this._buildValParams(),d=f.vals,b=f.params,a=e=0,g=this.fields.length;g>=0?g>e:e>g;a=g>=0?++e:--e)""!==c&&(c+=", "),c+=this.fields[a];return{text:"("+c+") VALUES ("+d.join("), (")+")",values:b}},b}(a.Block),a.DistinctBlock=function(a){function b(a){b.__super__.constructor.call(this,a),this.useDistinct=!1}return p(b,a),b.prototype.distinct=function(){return this.useDistinct=!0},b.prototype.buildStr=function(){return this.useDistinct?"DISTINCT":""},b}(a.Block),a.GroupByBlock=function(a){function b(a){b.__super__.constructor.call(this,a),this.groups=[]}return p(b,a),b.prototype.group=function(a){return a=this._sanitizeField(a),this.groups.push(a)},b.prototype.buildStr=function(){var a,b,c,d,e;if(b="",0c;c++)a=e[c],""!==b&&(b+=", "),b+=a;b="GROUP BY "+b}return b},b}(a.Block),a.OffsetBlock=function(a){function b(a){b.__super__.constructor.call(this,a),this.offsets=null}return p(b,a),b.prototype.offset=function(a){return a=this._sanitizeLimitOffset(a),this.offsets=a},b.prototype.buildStr=function(){return this.offsets?"OFFSET "+this.offsets:""},b}(a.Block),a.WhereBlock=function(b){function c(a){c.__super__.constructor.call(this,a),this.wheres=[]}return p(c,b),c.prototype.where=function(){var b,c,d,e,f,g,h,i,j,k,l,n,o,p;if(c=arguments[0],k=2<=arguments.length?m.call(arguments,1):[],c=this._sanitizeCondition(c),d="",e=[],c instanceof a.Expression)j=c.toParam(),d=j.text,e=j.values;else for(f=l=0,p=c.length;p>=0?p>l:l>p;f=p>=0?++l:--l)if(b=c.charAt(f),"?"===b&&0n;n++)h=i[n],g.push(this._sanitizeValue(h));e=e.concat(g),d+="("+function(){var a,b,c;for(c=[],a=0,b=g.length;b>a;a++)h=g[a],c.push("?");return c}().join(", ")+")"}else d+="?",e.push(this._sanitizeValue(i));else d+=b;return""!==d?this.wheres.push({text:d,values:e}):void 0},c.prototype.buildStr=function(){var a,b,c,d,e,f,g,h,i,j;if(0>=this.wheres.length)return"";for(e="",i=this.wheres,f=0,h=i.length;h>f;f++)if(d=i[f],""!==e&&(e+=") AND ("),0=0?j>g:g>j;b=j>=0?++g:--g)a=d.text.charAt(b),e+="?"===a?this._formatValue(d.values[c++]):a;else e+=d.text;return"WHERE ("+e+")"},c.prototype.buildParam=function(){var a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q;if(d={text:"",values:[]},0>=this.wheres.length)return d;for(h="",o=this.wheres,i=0,l=o.length;l>i;i++){for(g=o[i],""!==h&&(h+=") AND ("),e=g.text.split("?"),a=0,p=g.values,j=0,m=p.length;m>j;j++){if(f=p[j],null!=e[a]&&(h+=""+e[a]),b=this._formatValueAsParam(f),null!=(null!=b?b.text:void 0))for(h+="("+b.text+")",q=b.values,k=0,n=q.length;n>k;k++)c=q[k],d.values.push(c);else h+="?",d.values.push(b);a+=1}null!=e[a]&&(h+=""+e[a])}return d.text="WHERE ("+h+")",d},c}(a.Block),a.OrderByBlock=function(a){function b(a){b.__super__.constructor.call(this,a),this.orders=[],this._values=[]}return p(b,a),b.prototype.order=function(){var a,b,c;return b=arguments[0],a=arguments[1],c=3<=arguments.length?m.call(arguments,2):[],null==a&&(a=!0),b=this._sanitizeField(b),this._values=c,this.orders.push({field:b,dir:a?!0:!1})},b.prototype._buildStr=function(a){var b,c,d,e,f,g,h,i,j,k,l;if(null==a&&(a=!1),0h;h++){if(e=k[h],""!==f&&(f+=", "),c="",a)c=e.field;else for(d=i=0,l=e.field.length;l>=0?l>i:i>l;d=l>=0?++i:--i)b=e.field.charAt(d),c+="?"===b?this._formatValue(this._values[g++]):b;f+=""+c+" "+(e.dir?"ASC":"DESC")}return"ORDER BY "+f}return""},b.prototype.buildStr=function(){return this._buildStr()},b.prototype.buildParam=function(){var a=this;return{text:this._buildStr(!0),values:this._values.map(function(b){return a._formatValueAsParam(b)})}},b}(a.Block),a.LimitBlock=function(a){function b(a){b.__super__.constructor.call(this,a),this.limits=null}return p(b,a),b.prototype.limit=function(a){return a=this._sanitizeLimitOffset(a),this.limits=a},b.prototype.buildStr=function(){return this.limits?"LIMIT "+this.limits:""},b}(a.Block),a.JoinBlock=function(b){function c(a){c.__super__.constructor.call(this,a),this.joins=[]}return p(c,b),c.prototype.join=function(a,b,c,d){return null==b&&(b=null),null==c&&(c=null),null==d&&(d="INNER"),a=this._sanitizeTable(a,!0),b&&(b=this._sanitizeTableAlias(b)),c&&(c=this._sanitizeCondition(c)),this.joins.push({type:d,table:a,alias:b,condition:c}),this},c.prototype.left_join=function(a,b,c){return null==b&&(b=null),null==c&&(c=null),this.join(a,b,c,"LEFT")},c.prototype.right_join=function(a,b,c){return null==b&&(b=null),null==c&&(c=null),this.join(a,b,c,"RIGHT")},c.prototype.outer_join=function(a,b,c){return null==b&&(b=null),null==c&&(c=null),this.join(a,b,c,"OUTER")},c.prototype.left_outer_join=function(a,b,c){return null==b&&(b=null),null==c&&(c=null),this.join(a,b,c,"LEFT OUTER")},c.prototype.full_join=function(a,b,c){return null==b&&(b=null),null==c&&(c=null),this.join(a,b,c,"FULL")},c.prototype.buildStr=function(){var a,b,c,d,e;for(b="",e=this.joins||[],c=0,d=e.length;d>c;c++)a=e[c],""!==b&&(b+=" "),b+=""+a.type+" JOIN ",b+="string"==typeof a.table?a.table:"("+a.table+")",a.alias&&(b+=" "+a.alias),a.condition&&(b+=" ON ("+a.condition+")");return b},c.prototype.buildParam=function(b){var c,d,e,f,g,h,i,j,k,l,m,n,o,p,q;if(h={text:"",values:[]},g=[],e="",0>=this.joins.length)return h;for(p=this.joins,j=0,m=p.length;m>j;j++)c=p[j],"string"==typeof c.table?f={text:""+c.table,values:[]}:c.table instanceof a.QueryBuilder?(c.table.updateOptions({nestedBuilder:!0}),f=c.table.toParam()):(c.updateOptions({nestedBuilder:!0}),f=c.buildParam(b)),c.condition instanceof a.Expression?(d=c.condition.toParam(),f.condition=d.text,f.values=f.values.concat(d.values)):f.condition=c.condition,f.join=c,g.push(f);for(k=0,n=g.length;n>k;k++)for(f=g[k],""!==e&&(e+=" "),e+=""+f.join.type+" JOIN ",e+="string"==typeof f.join.table?f.text:"("+f.text+")",f.join.alias&&(e+=" "+f.join.alias),f.condition&&(e+=" ON ("+f.condition+")"),q=f.values,l=0,o=q.length;o>l;l++)i=q[l],h.values.push(this._formatCustomValue(i));return h.text+=e,h},c}(a.Block),a.UnionBlock=function(b){function c(a){c.__super__.constructor.call(this,a),this.unions=[]}return p(c,b),c.prototype.union=function(a,b){return null==b&&(b="UNION"),a=this._sanitizeTable(a,!0),this.unions.push({type:b,table:a}),this},c.prototype.union_all=function(a){return this.union(a,"UNION ALL")},c.prototype.buildStr=function(){var a,b,c,d,e;for(b="",e=this.unions||[],c=0,d=e.length;d>c;c++)a=e[c],""!==b&&(b+=" "),b+=""+a.type+" ",b+="string"==typeof a.table?a.table:"("+a.table+")";return b},c.prototype.buildParam=function(b){var c,d,e,f,g,h,i,j,k,l,m,n,o,p;if(f={text:"",values:[]},e=[],g="",0>=this.unions.length)return f;for(o=this.unions||[],i=0,l=o.length;l>i;i++)c=o[i],"string"==typeof c.table?d={text:""+c.table,values:[]}:c.table instanceof a.QueryBuilder?(c.table.updateOptions({nestedBuilder:!0}),d=c.table.toParam()):(c.updateOptions({nestedBuilder:!0}),d=c.buildParam(b)),d.type=c.type,e.push(d);for(j=0,m=e.length;m>j;j++)for(d=e[j],""!==g&&(g+=" "),g+=""+d.type+" ("+d.text+")",p=d.values,k=0,n=p.length;n>k;k++)h=p[k],f.values.push(this._formatCustomValue(h));return f.text+=g,f},c}(a.Block),a.QueryBuilder=function(a){function b(a,c){var d,e,f,g,h,i,j,k,l=this;for(b.__super__.constructor.call(this,a),this.blocks=c||[],j=this.blocks,h=0,i=j.length;i>h;h++){d=j[h],k=d.exposedMethods(),g=function(a,b,c){return l[b]=function(){return c.apply(a,arguments),l}};for(f in k){if(e=k[f],null!=this[f])throw new Error(""+this._getObjectClassName(this)+" already has a builder method called: "+f);g(d,f,e)}}}return p(b,a),b.prototype.registerValueHandler=function(a,c){var d,e,f,g;for(g=this.blocks,e=0,f=g.length;f>e;e++)d=g[e],d.registerValueHandler(a,c);return b.__super__.registerValueHandler.call(this,a,c),this},b.prototype.updateOptions=function(a){var b,c,d,f,g;for(this.options=e({},this.options,a),f=this.blocks,g=[],c=0,d=f.length;d>c;c++)b=f[c],g.push(b.options=e({},b.options,a));return g},b.prototype.toString=function(){var a;return function(){var b,c,d,e;for(d=this.blocks,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.buildStr(this));return e}.call(this).filter(function(a){return 0a;a++)b=d[a],e.push(b.buildParam(this));return e}.call(this),g.text=function(){var a,d,e;for(e=[],a=0,d=c.length;d>a;a++)b=c[a],e.push(b.text);return e}().filter(function(a){return 0a;a++)b=c[a],e.push(b.values);return e}()),null==this.options.nestedBuilder&&(this.options.numberedParameters||null!=(null!=a?a.numberedParametersStartAt:void 0))&&(d=1,null!=this.options.numberedParametersStartAt&&(d=this.options.numberedParametersStartAt),g.text=g.text.replace(/\?/g,function(){return"$"+d++})),this.options=f,g},b.prototype.clone=function(){var a;return new this.constructor(this.options,function(){var b,c,d,e;for(d=this.blocks,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.clone());return e}.call(this))},b.prototype.isNestable=function(){return!1},b}(a.BaseBuilder),a.Select=function(b){function c(b,d){null==d&&(d=null),d||(d=[new a.StringBlock(b,"SELECT"),new a.DistinctBlock(b),new a.GetFieldBlock(b),new a.FromTableBlock(e({},b,{allowNested:!0})),new a.JoinBlock(e({},b,{allowNested:!0})),new a.WhereBlock(b),new a.GroupByBlock(b),new a.OrderByBlock(b),new a.LimitBlock(b),new a.OffsetBlock(b),new a.UnionBlock(e({},b,{allowNested:!0}))]),c.__super__.constructor.call(this,b,d)}return p(c,b),c.prototype.isNestable=function(){return!0},c}(a.QueryBuilder),a.Update=function(b){function c(b,d){null==d&&(d=null),d||(d=[new a.StringBlock(b,"UPDATE"),new a.UpdateTableBlock(b),new a.SetFieldBlock(b),new a.WhereBlock(b),new a.OrderByBlock(b),new a.LimitBlock(b)]),c.__super__.constructor.call(this,b,d)}return p(c,b),c}(a.QueryBuilder),a.Delete=function(b){function c(b,d){null==d&&(d=null),d||(d=[new a.StringBlock(b,"DELETE"),new a.FromTableBlock(e({},b,{singleTable:!0})),new a.JoinBlock(b),new a.WhereBlock(b),new a.OrderByBlock(b),new a.LimitBlock(b)]),c.__super__.constructor.call(this,b,d)}return p(c,b),c}(a.QueryBuilder),a.Insert=function(b){function c(b,d){null==d&&(d=null),d||(d=[new a.StringBlock(b,"INSERT"),new a.IntoTableBlock(b),new a.InsertFieldValueBlock(b)]),c.__super__.constructor.call(this,b,d)}return p(c,b),c}(a.QueryBuilder),d={VERSION:"3.10.0",expr:function(){return new a.Expression},select:function(b,c){return new a.Select(b,c)},update:function(b,c){return new a.Update(b,c)},insert:function(b,c){return new a.Insert(b,c)},"delete":function(b,c){return new a.Delete(b,c)},registerValueHandler:a.registerValueHandler},d.remove=d["delete"],d.cls=a,("undefined"!=typeof define&&null!==define?define.amd:void 0)?define(function(){return d}):("undefined"!=typeof module&&null!==module?module.exports:void 0)?module.exports=d:"undefined"!=typeof window&&null!==window&&(window.squel=d),d.flavours={},d.useFlavour=function(a){if(!(d.flavours[a]instanceof Function))throw new Error("Flavour not available: "+a);return d.flavours[a].call(null,d),d},d.flavours.postgres=function(){return a=d.cls,a.DefaultQueryBuilderOptions.numberedParameters=!0,a.DefaultQueryBuilderOptions.numberedParametersStartAt=1,a.ReturningBlock=function(a){function b(a){b.__super__.constructor.call(this,a),this._str=null}return p(b,a),b.prototype.returning=function(a){return this._str=this._sanitizeField(a)},b.prototype.buildStr=function(){return this._str?"RETURNING "+this._str:""},b}(a.Block),a.Insert=function(b){function c(b,d){null==d&&(d=null),d||(d=[new a.StringBlock(b,"INSERT"),new a.IntoTableBlock(b),new a.InsertFieldValueBlock(b),new a.ReturningBlock(b)]),c.__super__.constructor.call(this,b,d)}return p(c,b),c}(a.QueryBuilder),a.Update=function(b){function c(b,d){null==d&&(d=null),d||(d=[new a.StringBlock(b,"UPDATE"),new a.UpdateTableBlock(b),new a.SetFieldBlock(b),new a.WhereBlock(b),new a.OrderByBlock(b),new a.LimitBlock(b),new a.ReturningBlock(b)]),c.__super__.constructor.call(this,b,d)}return p(c,b),c}(a.QueryBuilder),a.Delete=function(b){function c(b,d){null==d&&(d=null),d||(d=[new a.StringBlock(b,"DELETE"),new a.FromTableBlock(e({},b,{singleTable:!0})),new a.JoinBlock(b),new a.WhereBlock(b),new a.OrderByBlock(b),new a.LimitBlock(b),new a.ReturningBlock(b)]),c.__super__.constructor.call(this,b,d)}return p(c,b),c}(a.QueryBuilder)},d.flavours.mysql=function(){var b,c;return a=d.cls,a.TargetTableBlock=function(a){function c(){return b=c.__super__.constructor.apply(this,arguments)}return p(c,a),c.prototype.target=function(a){return this._setValue(this._sanitizeTable(a))},c}(a.AbstractValueBlock),a.MysqlOnDuplicateKeyUpdateBlock=function(a){function b(){return c=b.__super__.constructor.apply(this,arguments)}return p(b,a),b.prototype.onDupUpdate=function(a,b,c){return this._set(a,b,c)},b.prototype.buildStr=function(){var a,b,c,d,e,f,g;for(d="",c=f=0,g=this.fields.length;g>=0?g>f:f>g;c=g>=0?++f:--f)a=this.fields[c],""!==d&&(d+=", "),e=this.values[0][c],b=this.fieldOptions[0][c],d+="undefined"==typeof e?a:""+a+" = "+this._formatValue(e,b);return""===d?"":"ON DUPLICATE KEY UPDATE "+d},b.prototype.buildParam=function(){var a,b,c,d,e,f,g;for(c="",d=[],b=f=0,g=this.fields.length;g>=0?g>f:f>g;b=g>=0?++f:--f)a=this.fields[b],""!==c&&(c+=", "),e=this.values[0][b],"undefined"==typeof e?c+=a:(c+=""+a+" = ?",d.push(this._formatValueAsParam(e)));return{text:""===c?"":"ON DUPLICATE KEY UPDATE "+c,values:d}},b}(a.AbstractSetFieldBlock),a.Insert=function(b){function c(b,d){null==d&&(d=null),d||(d=[new a.StringBlock(b,"INSERT"),new a.IntoTableBlock(b),new a.InsertFieldValueBlock(b),new a.MysqlOnDuplicateKeyUpdateBlock(b)]),c.__super__.constructor.call(this,b,d)}return p(c,b),c}(a.QueryBuilder),a.Delete=function(b){function c(b,d){null==d&&(d=null),d||(d=[new a.StringBlock(b,"DELETE"),new a.TargetTableBlock(b),new a.FromTableBlock(e({},b,{singleTable:!0})),new a.JoinBlock(b),new a.WhereBlock(b),new a.OrderByBlock(b),new a.LimitBlock(b)]),c.__super__.constructor.call(this,b,d)}return p(c,b),c}(a.QueryBuilder)},e=function(){var a,b,c,d,e,f,g;if(a=arguments[0],c=2<=arguments.length?m.call(arguments,1):[])for(f=0,g=c.length;g>f;f++)if(d=c[f])for(b in d)n.call(d,b)&&(e=d[b],a[b]=e);return a},d.flavours.mssql=function(){return a=d.cls,a.DefaultQueryBuilderOptions.replaceSingleQuotes=!0,a.DefaultQueryBuilderOptions.autoQuoteAliasNames=!1,d.registerValueHandler(Date,function(a){return""+a.getUTCFullYear()+"-"+(a.getUTCMonth()+1)+"-"+a.getUTCDate()+" "+a.getUTCHours()+":"+a.getUTCMinutes()+":"+a.getUTCSeconds()}),a.MssqlLimitOffsetTopBlock=function(b){function c(a){c.__super__.constructor.call(this,a),this.limits=null,this.offsets=null}var d,e,f,g,h,i,j,k;return p(c,b),h=function(a){return a=this._sanitizeLimitOffset(a),this._parent.limits=a},f=function(a){function b(a){b.__super__.constructor.call(this,a.options),this._parent=a}return p(b,a),b}(a.Block),d=function(a){function b(){return i=b.__super__.constructor.apply(this,arguments)}return p(b,a),b.prototype.limit=h,b.prototype.buildStr=function(){return this._parent.limits&&this._parent.offsets?"FETCH NEXT "+this._parent.limits+" ROWS ONLY":""},b}(f),g=function(a){function b(){return j=b.__super__.constructor.apply(this,arguments)}return p(b,a),b.prototype.top=h,b.prototype.buildStr=function(){return this._parent.limits&&!this._parent.offsets?"TOP ("+this._parent.limits+")":"" +},b}(f),e=function(a){function b(){return this.offset=o(this.offset,this),k=b.__super__.constructor.apply(this,arguments)}return p(b,a),b.prototype.offset=function(a){return a=this._sanitizeLimitOffset(a),this._parent.offsets=a},b.prototype.buildStr=function(){return this._parent.offsets?"OFFSET "+this._parent.offsets+" ROWS":""},b}(f),c.prototype.LIMIT=function(a){return this.constructor(a),new d(this)},c.prototype.TOP=function(a){return this.constructor(a),new g(this)},c.prototype.OFFSET=function(a){return this.constructor(a),new e(this)},c}.call(this,a.Block),a.MssqlUpdateTopBlock=function(a){function b(a){b.__super__.constructor.call(this,a),this.limits=null}var c;return p(b,a),c=function(a){return a=this._sanitizeLimitOffset(a),this.limits=a},b.prototype.limit=c,b.prototype.top=c,b.prototype.buildStr=function(){return this.limits?"TOP ("+this.limits+")":""},b}(a.Block),a.MssqlInsertFieldValueBlock=function(a){function b(a){b.__super__.constructor.call(this,a),this.outputs=[]}return p(b,a),b.prototype.output=function(a){var b,c,d,e;if("string"==typeof a)return this.outputs.push("INSERTED."+this._sanitizeField(a));for(e=[],c=0,d=a.length;d>c;c++)b=a[c],e.push(this.outputs.push("INSERTED."+this._sanitizeField(b)));return e},b.prototype.buildStr=function(){if(0>=this.fields.length)throw new Error("set() needs to be called");return"("+this.fields.join(", ")+") "+(0!==this.outputs.length?"OUTPUT "+this.outputs.join(", ")+" ":"")+"VALUES ("+this._buildVals().join("), (")+")"},b.prototype.buildParam=function(){var a,b,c,d,e,f,g;if(0>=this.fields.length)throw new Error("set() needs to be called");for(c="",f=this._buildValParams(),d=f.vals,b=f.params,a=e=0,g=this.fields.length;g>=0?g>e:e>g;a=g>=0?++e:--e)""!==c&&(c+=", "),c+=this.fields[a];return{text:"("+c+") "+(0!==this.outputs.length?"OUTPUT "+this.outputs.join(", ")+" ":"")+"VALUES ("+d.join("), (")+")",values:b}},b}(a.InsertFieldValueBlock),a.MssqlUpdateDeleteOutputBlock=function(a){function b(a){b.__super__.constructor.call(this,a),this._outputs=[]}return p(b,a),b.prototype.outputs=function(a){var b,c,d;d=[];for(c in a)b=a[c],d.push(this.output(c,b));return d},b.prototype.output=function(a,b){return null==b&&(b=null),a=this._sanitizeField(a),b&&(b=this._sanitizeFieldAlias(b)),this._outputs.push({name:"INSERTED."+a,alias:b})},b.prototype.buildStr=function(){var a,b,c,d,e;if(b="",this._outputs.length>0){for(e=this._outputs,c=0,d=e.length;d>c;c++)a=e[c],""!==b&&(b+=", "),b+=a.name,a.alias&&(b+=" AS "+a.alias);b="OUTPUT "+b}return b},b}(a.Block),a.Select=function(b){function c(b,d){var f;null==d&&(d=null),f=new a.MssqlLimitOffsetTopBlock(b),d||(d=[new a.StringBlock(b,"SELECT"),new a.DistinctBlock(b),f.TOP(b),new a.GetFieldBlock(b),new a.FromTableBlock(e({},b,{allowNested:!0})),new a.JoinBlock(e({},b,{allowNested:!0})),new a.WhereBlock(b),new a.GroupByBlock(b),new a.OrderByBlock(b),f.OFFSET(b),f.LIMIT(b),new a.UnionBlock(e({},b,{allowNested:!0}))]),c.__super__.constructor.call(this,b,d)}return p(c,b),c.prototype.isNestable=function(){return!0},c}(a.QueryBuilder),a.Update=function(b){function c(b,d){null==d&&(d=null),d||(d=[new a.StringBlock(b,"UPDATE"),new a.MssqlUpdateTopBlock(b),new a.UpdateTableBlock(b),new a.SetFieldBlock(b),new a.MssqlUpdateDeleteOutputBlock(b),new a.WhereBlock(b)]),c.__super__.constructor.call(this,b,d)}return p(c,b),c}(a.QueryBuilder),a.Delete=function(b){function c(b,d){null==d&&(d=null),d||(d=[new a.StringBlock(b,"DELETE"),new a.FromTableBlock(e({},b,{singleTable:!0})),new a.JoinBlock(b),new a.MssqlUpdateDeleteOutputBlock(b),new a.WhereBlock(b),new a.OrderByBlock(b),new a.LimitBlock(b)]),c.__super__.constructor.call(this,b,d)}return p(c,b),c}(a.QueryBuilder),a.Insert=function(b){function c(b,d){null==d&&(d=null),d||(d=[new a.StringBlock(b,"INSERT"),new a.IntoTableBlock(b),new a.MssqlInsertFieldValueBlock(b)]),c.__super__.constructor.call(this,b,d)}return p(c,b),c}(a.QueryBuilder)}}).call(this); \ No newline at end of file diff --git a/src/squel.coffee b/src/squel.coffee index fbb2791d..443e344f 100644 --- a/src/squel.coffee +++ b/src/squel.coffee @@ -189,15 +189,21 @@ class cls.BaseBuilder extends cls.Cloneable item + + _sanitizeNestableQuery: (item) => + return item if item instanceof cls.QueryBuilder and item.isNestable() + throw new Error "must be a nestable query, e.g. SELECT" + + _sanitizeTable: (item, allowNested = false) -> if allowNested if "string" is typeof item sanitized = item - else if item instanceof cls.QueryBuilder and item.isNestable() - # allow nested queries - return item - else - throw new Error "table name must be a string or a nestable query instance" + else + try + sanitized = @_sanitizeNestableQuery(item) + catch e + throw new Error "table name must be a string or a nestable query instance" else sanitized = @_sanitizeName item, 'table name' @@ -931,6 +937,31 @@ class cls.InsertFieldValueBlock extends cls.AbstractSetFieldBlock +# (INSERT INTO) ... field ... (SELECT ... FROM ...) +class cls.InsertFieldSelectQueryBlock extends cls.Block + fromSelect: (fields, selectQuery) -> + @_fields = fields + @_query = @_sanitizeNestableQuery(selectQuery) + + buildStr: (queryBuilder) -> + if 0 >= @fields.length then throw new Error "set() needs to be called" + + "(#{@fields.join(', ')}) VALUES (#{@_buildVals().join('), (')})" + + buildParam: (queryBuilder) -> + if 0 >= @fields.length then throw new Error "set() needs to be called" + + # fields + str = "" + {vals, params} = @_buildValParams() + for i in [0...@fields.length] + str += ", " if "" isnt str + str += @fields[i] + + { text: "(#{str}) VALUES (#{vals.join('), (')})", values: params } + + + # DISTINCT class cls.DistinctBlock extends cls.Block @@ -1508,7 +1539,7 @@ class cls.Insert extends cls.QueryBuilder blocks or= [ new cls.StringBlock(options, 'INSERT'), new cls.IntoTableBlock(options), - new cls.InsertFieldValueBlock(options) + new cls.InsertFieldValueBlock(options), ] super options, blocks diff --git a/test/baseclasses.test.coffee b/test/baseclasses.test.coffee index 91ac3489..8b18bdf3 100644 --- a/test/baseclasses.test.coffee +++ b/test/baseclasses.test.coffee @@ -315,6 +315,23 @@ test['Builder base class'] = assert.same '(SELECT MAX(score) FROM scores)', @inst._sanitizeField(s) + '_sanitizeNestableQuery': + 'is not query builder': -> + assert.throws (=> @inst._sanitizeNestableQuery(null)), 'must be a nestable query, e.g. SELECT' + + 'is not a nestable query builder': -> + qry = squel.select() + stub = test.mocker.stub qry, 'isNestable', -> false + + assert.throws (=> @inst._sanitizeNestableQuery(qry)), 'must be a nestable query, e.g. SELECT' + + 'is not a nestable query builder': -> + qry = squel.select() + stub = test.mocker.stub qry, 'isNestable', -> true + + assert.same qry, @inst._sanitizeNestableQuery(qry) + + '_sanitizeTable': 'nesting allowed': 'string': ->