forked from hiddentao/squel
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsquel-basic.min.js
2 lines (2 loc) · 31.4 KB
/
squel-basic.min.js
1
2
/*! squel | https://github.com/hiddentao/squel | BSD license */(function(){var a,b,c,d,e,f,g,h,i=[].slice,j={}.hasOwnProperty,k=function(a,b){return function(){return a.apply(b,arguments)}},l=function(a,b){function c(){this.constructor=a}for(var d in b)j.call(b,d)&&(a[d]=b[d]);return c.prototype=b.prototype,a.prototype=new c,a.__super__=b.prototype,a};f=function(){var a,b,c,d,e,f,g;if(a=arguments[0],c=2<=arguments.length?i.call(arguments,1):[])for(f=0,g=c.length;g>f;f++)if(d=c[f])for(b in d)j.call(d,b)&&(e=d[b],a[b]=e);return a},h=function(a){return a?a.constructor.prototype===Object.prototype:!1},g=function(a){return a.constructor.prototype===Array.prototype},e=function(a){var b,c,d;if(!a)return a;if("function"==typeof a.clone)return a.clone();if(h(a)||g(a)){c=new a.constructor;for(b in a)j.call(a,b)&&(d=a[b],"function"!=typeof d&&(c[b]=e(d)));return c}return JSON.parse(JSON.stringify(a))},b=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})},a=function(){var a,b,c,d,e,f,g,h;for(d=arguments[0],a=2<=arguments.length?i.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},d=function(c){var d,g,m,n,o,p,q,r;return null==c&&(c=null),d={},d.DefaultQueryBuilderOptions={autoQuoteTableNames:!1,autoQuoteFieldNames:!1,autoQuoteAliasNames:!0,useAsForTableAliasNames:!1,nameQuoteCharacter:"`",tableAliasQuoteCharacter:"`",fieldAliasQuoteCharacter:'"',valueHandlers:[],parameterCharacter:"?",numberedParameters:!1,numberedParametersPrefix:"$",numberedParametersStartAt:1,replaceSingleQuotes:!1,singleQuoteReplacement:"''",separator:" "},d.globalValueHandlers=[],d.registerValueHandler=function(a,c){return b(d.globalValueHandlers,a,c)},d.Cloneable=function(){function a(){}return a.prototype.clone=function(){var a;return a=new this.constructor,f(a,e(f({},this)))},a}(),d.BaseBuilder=function(c){function e(a){this._sanitizeNestableQuery=k(this._sanitizeNestableQuery,this);var b;b=JSON.parse(JSON.stringify(d.DefaultQueryBuilderOptions)),this.options=f({},b,a)}return l(e,c),e.prototype.registerValueHandler=function(a,c){return b(this.options.valueHandlers,a,c),this},e.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},e.prototype._sanitizeCondition=function(a){if(!(a instanceof d.Expression)&&"string"!=typeof a)throw new Error("condition must be a string or Expression instance");return a},e.prototype._sanitizeName=function(a,b){if("string"!=typeof a)throw new Error(""+b+" must be a string");return a},e.prototype._sanitizeField=function(a,b){var c;return null==b&&(b={}),a instanceof d.QueryBuilder?a="("+a+")":(a=this._sanitizeName(a,"field name"),this.options.autoQuoteFieldNames&&(c=this.options.nameQuoteCharacter,a=b.ignorePeriodsForFieldNameQuotes?""+c+a+c:a.split(".").map(function(a){return"*"===a?a:""+c+a+c}).join("."))),a},e.prototype._sanitizeNestableQuery=function(a){if(a instanceof d.QueryBuilder&&a.isNestable())return a;throw new Error("must be a nestable query, e.g. SELECT")},e.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},e.prototype._sanitizeTableAlias=function(a){var b;return b=this._sanitizeName(a,"table alias"),this.options.autoQuoteAliasNames&&(b=""+this.options.tableAliasQuoteCharacter+b+this.options.tableAliasQuoteCharacter),this.options.useAsForTableAliasNames?"AS "+b:b},e.prototype._sanitizeFieldAlias=function(a){var b;return b=this._sanitizeName(a,"field alias"),this.options.autoQuoteAliasNames?""+this.options.fieldAliasQuoteCharacter+b+this.options.fieldAliasQuoteCharacter:b},e.prototype._sanitizeLimitOffset=function(a){if(a=parseInt(a),0>a||isNaN(a))throw new Error("limit/offset must be >= 0");return a},e.prototype._sanitizeValue=function(b){var c,e;if(c=typeof b,null===b);else if("string"===c||"number"===c||"boolean"===c);else if(b instanceof d.QueryBuilder&&b.isNestable());else if(b instanceof d.FunctionBlock);else if(e=void 0!==a(b,this.options.valueHandlers,d.globalValueHandlers),!e)throw new Error("field value must be a string, number, boolean, null or one of the registered custom value types");return b},e.prototype._escapeValue=function(a){return!0!==this.options.replaceSingleQuotes?a:a.replace(/\'/g,this.options.singleQuoteReplacement)},e.prototype._formatCustomValue=function(b,c){var e;return null==c&&(c=!1),e=a(b,this.options.valueHandlers,d.globalValueHandlers),e&&(b=e(b,c)),b},e.prototype._formatValueAsParam=function(a){var b,c=this;return Array.isArray(a)?a.map(function(a){return c._formatValueAsParam(a)}):a instanceof d.QueryBuilder&&a.isNestable()?(a.updateOptions({nestedBuilder:!0}),b=a.toParam()):a instanceof d.Expression?b=a.toParam():this._formatCustomValue(a,!0)},e.prototype._formatValue=function(a,b){var c,e,f=this;return null==b&&(b={}),c=this._formatCustomValue(a),c!==a?"("+c+")":(Array.isArray(a)?(a=a.map(function(a){return f._formatValue(a)}),a="("+a.join(", ")+")"):null===a?a="NULL":"boolean"==typeof a?a=a?"TRUE":"FALSE":a instanceof d.QueryBuilder?a="("+a+")":a instanceof d.Expression?a="("+a+")":"number"!=typeof a&&(b.dontQuote?a=""+a:(e=this._escapeValue(a),a="'"+e+"'")),a)},e}(d.Cloneable),d.Expression=function(a){function b(a){var c,e=this;b.__super__.constructor.call(this),c=JSON.parse(JSON.stringify(d.DefaultQueryBuilderOptions)),this.options=f({},c,a),this.tree={nodes:[]},this.current=this.tree,this.stack=[],this._begin=function(a){var b,c;return c={type:a,nodes:[]},b=e._current(),e.stack.push(b.nodes.length),b.nodes.push(c),e},this._current=function(){var a,b,c,d;for(a=e.tree,b=c=0,d=e.stack.length;d>=0?d>c:c>d;b=d>=0?++c:--c)a=a.nodes[e.stack[b]];return a}}return l(b,a),b.prototype.tree=null,b.prototype.stack=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.stack.length)throw new Error("begin() needs to be called");return this.stack.pop(),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(this.stack.length)throw new Error("end() needs to be called");return this._toString(this.tree)},b.prototype.toParam=function(){if(this.stack.length)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,l=this;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(this.options.parameterCharacter,"("+d.text+")")):g=g.concat(d),Array.isArray(c.para)&&(e=Array.apply(null,new Array(c.para.length)).map(function(){return l.options.parameterCharacter}),f=f.replace(this.options.parameterCharacter,"("+e.join(", ")+")"))):f=f.replace(this.options.parameterCharacter,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}(d.BaseBuilder),d.Case=function(a){function b(a,c){null==c&&(c={}),b.__super__.constructor.call(this),h(a)&&(c=a,a=null),a&&(this.fieldName=this._sanitizeField(a)),this.options=f({},d.DefaultQueryBuilderOptions,c),this.cases=[]}return l(b,a),b.prototype.cases=null,b.prototype.elseValue=null,b.prototype.when=function(){var a,b;return a=arguments[0],b=2<=arguments.length?i.call(arguments,1):[],this.cases.unshift({expression:a,values:b}),this},b.prototype.then=function(a){if(0===this.cases.length)throw new Error("when() needs to be called first");return this.cases[0].result=a,this},b.prototype["else"]=function(a){return this.elseValue=a,this},b.prototype.toString=function(){return this._toString(this.cases,this.elseValue)},b.prototype.toParam=function(){return this._toString(this.cases,this.elseValue,!0)},b.prototype._toString=function(a,b,c){var e,f,g=this;return null==c&&(c=!1),0===a.length?this._formatValue(b):(f=[],a=a.map(function(a){var b,e;return b=new d.AbstractConditionBlock("WHEN"),b._condition.apply(b,[a.expression].concat(a.values)),e="",c?(b=b.buildParam(),e=b.text,f=f.concat(b.values)):e=b.buildStr(),e+" THEN "+g._formatValue(a.result)}),e=a.join(" ")+" ELSE "+this._formatValue(b)+" END",this.fieldName&&(e=this.fieldName+" "+e),e="CASE "+e,c?{text:e,values:f}:e)},b}(d.BaseBuilder),d.Block=function(a){function b(){return g=b.__super__.constructor.apply(this,arguments)}return l(b,a),b.prototype.exposedMethods=function(){var a,b,c;b={};for(a in this)c=this[a],"function"!=typeof c||"_"===a.charAt(0)||d.Block.prototype[a]||(b[a]=c);return b},b.prototype.buildStr=function(a){return""},b.prototype.buildParam=function(a){return{text:this.buildStr(a),values:[]}},b}(d.BaseBuilder),d.StringBlock=function(a){function b(a,c){b.__super__.constructor.call(this,a),this.str=c}return l(b,a),b.prototype.buildStr=function(a){return this.str},b}(d.Block),d.AbstractValueBlock=function(a){function b(a){b.__super__.constructor.call(this,a),this._str="",this._values=[]}return l(b,a),b.prototype._setValue=function(){var a,b;return a=arguments[0],b=2<=arguments.length?i.call(arguments,1):[],this._str=a,this._values=b,this},b.prototype.buildStr=function(a){var b,c,d,e,f,g,h;for(e=this._str,c="",f=[].concat(this._values),d=g=0,h=e.length;h>=0?h>g:g>h;d=h>=0?++g:--g)b=e.charAt(d),this.options.parameterCharacter===b&&0<f.length&&(b=f.shift()),c+=b;return c},b.prototype.buildParam=function(a){return{text:this._str,values:this._values}},b}(d.Block),d.FunctionBlock=function(a){function b(){return m=b.__super__.constructor.apply(this,arguments)}return l(b,a),b.prototype["function"]=function(){var a,b;return a=arguments[0],b=2<=arguments.length?i.call(arguments,1):[],this._setValue.apply(this,[a].concat(b))},b}(d.AbstractValueBlock),d.fval=function(){var a,b,c;return b=arguments[0],c=2<=arguments.length?i.call(arguments,1):[],a=new d.FunctionBlock,a["function"].apply(a,[b].concat(c))},d.registerValueHandler(d.FunctionBlock,function(a,b){return null==b&&(b=!1),b?a.buildParam():a.buildStr()}),d.AbstractTableBlock=function(a){function b(a){b.__super__.constructor.call(this,a),this.tables=[]}return l(b,a),b.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})},b.prototype._hasTable=function(){return 0<this.tables.length},b.prototype.buildStr=function(a){var b,c,d,e,f;if(!this._hasTable())return"";for(c="",f=this.tables,d=0,e=f.length;e>d;d++)b=f[d],""!==c&&(c+=", "),c+="string"==typeof b.table?b.table:"("+b.table+")",b.alias&&(c+=" "+b.alias);return c},b.prototype._buildParam=function(a,b){var c,e,f,g,h,i,j,k,l,m,n,o,p,q;if(null==b&&(b=null),h={text:"",values:[]},g=[],f="",!this._hasTable())return h;for(p=this.tables,j=0,m=p.length;m>j;j++)c=p[j],"string"==typeof c.table?e={text:""+c.table,values:[]}:c.table instanceof d.QueryBuilder?(c.table.updateOptions({nestedBuilder:!0}),e=c.table.toParam()):(c.updateOptions({nestedBuilder:!0}),e=c.buildParam(a)),e.table=c,g.push(e);for(k=0,n=g.length;n>k;k++)for(e=g[k],""!==f?f+=", ":null!=b&&""!==b&&(f+=""+b+" "+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},b.prototype.buildParam=function(a){return this._buildParam(a)},b}(d.Block),d.UpdateTableBlock=function(a){function b(){return n=b.__super__.constructor.apply(this,arguments)}return l(b,a),b.prototype.table=function(a,b){return null==b&&(b=null),this._table(a,b)},b}(d.AbstractTableBlock),d.FromTableBlock=function(a){function b(){return o=b.__super__.constructor.apply(this,arguments)}return l(b,a),b.prototype.from=function(a,b){return null==b&&(b=null),this._table(a,b)},b.prototype.buildStr=function(a){var c;return c=b.__super__.buildStr.call(this,a),c.length?"FROM "+c:""},b.prototype.buildParam=function(a){return this._buildParam(a,"FROM")},b}(d.AbstractTableBlock),d.IntoTableBlock=function(a){function b(a){b.__super__.constructor.call(this,a),this.table=null}return l(b,a),b.prototype.into=function(a){return this.table=this._sanitizeTable(a,!1)},b.prototype.buildStr=function(a){if(!this.table)throw new Error("into() needs to be called");return"INTO "+this.table},b}(d.Block),d.GetFieldBlock=function(a){function b(a){b.__super__.constructor.call(this,a),this._fieldAliases={},this._fields=[]}return l(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){var e;return null==b&&(b=null),null==c&&(c={}),b&&(b=this._sanitizeFieldAlias(b)),this._fieldAliases[a]!==b?(e={alias:b},a instanceof d.Case?e.func=a:e.name=this._sanitizeField(a,c),c.aggregation&&(e.aggregation=c.aggregation),this._fieldAliases[a]=b,this._fields.push(e)):void 0},b.prototype.buildStr=function(a){return this._build(a)},b.prototype.buildParam=function(a){return this._build(a,!0)},b.prototype._build=function(a,b){var c,e,f,g,h,i,j;if(null==b&&(b=!1),!a.getBlock(d.FromTableBlock)._hasTable())return b?{text:"",values:[]}:"";for(f="",g=[],j=this._fields,h=0,i=j.length;i>h;h++)e=j[h],""!==f&&(f+=", "),e.aggregation&&(f+=e.aggregation+"("),e.func?b?(c=e.func.toParam(),f+=c.text,g=g.concat(c.values)):f+=e.func.toString():f+=e.name,e.aggregation&&(f+=")"),e.alias&&(f+=" AS "+e.alias);return""===f&&(f="*"),b?{text:f,values:g}:f},b}(d.Block),d.AbstractSetFieldBlock=function(a){function b(a){b.__super__.constructor.call(this,a),this.fieldOptions=[],this.fields=[],this.values=[]}return l(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)j.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(j.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}(d.Block),d.SetFieldBlock=function(a){function b(){return p=b.__super__.constructor.apply(this,arguments)}return l(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(a){var b,c,d,e,f,g,h;if(0>=this.fields.length)throw new Error("set() needs to be called");for(e="",d=g=0,h=this.fields.length;h>=0?h>g:g>h;d=h>=0?++g:--g)b=this.fields[d],""!==e&&(e+=", "),f=this.values[0][d],c=this.fieldOptions[0][d],e+="undefined"==typeof f?b:""+b+" = "+this._formatValue(f,c);return"SET "+e},b.prototype.buildParam=function(a){var b,c,d,e,f,g,h,i,j,k,l,m;if(0>=this.fields.length)throw new Error("set() needs to be called");for(e="",g=[],c=i=0,l=this.fields.length;l>=0?l>i:i>l;c=l>=0?++i:--i)if(b=this.fields[c],""!==e&&(e+=", "),h=this.values[0][c],"undefined"==typeof h)e+=b;else if(d=this._formatValueAsParam(h),null!=(null!=d?d.text:void 0))for(e+=""+b+" = ("+d.text+")",m=d.values,j=0,k=m.length;k>j;j++)f=m[j],g.push(f);else e+=""+b+" = "+this.options.parameterCharacter,g.push(d);return{text:"SET "+e,values:g}},b}(d.AbstractSetFieldBlock),d.InsertFieldValueBlock=function(a){function b(){return q=b.__super__.constructor.apply(this,arguments)}return l(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=this.options.parameterCharacter,d.push(c);"string"==typeof g[a]?g[a]+=", "+e:g[a]=""+e}return{vals:g,params:d}},b.prototype.buildStr=function(a){return 0>=this.fields.length?"":"("+this.fields.join(", ")+") VALUES ("+this._buildVals().join("), (")+")"},b.prototype.buildParam=function(a){var b,c,d,e,f,g,h;if(0>=this.fields.length)return{text:"",values:[]};for(d="",g=this._buildValParams(),e=g.vals,c=g.params,b=f=0,h=this.fields.length;h>=0?h>f:f>h;b=h>=0?++f:--f)""!==d&&(d+=", "),d+=this.fields[b];return{text:"("+d+") VALUES ("+e.join("), (")+")",values:c}},b}(d.AbstractSetFieldBlock),d.InsertFieldsFromQueryBlock=function(a){function b(a){b.__super__.constructor.call(this,a),this._fields=[],this._query=null}return l(b,a),b.prototype.fromQuery=function(a,b){var c=this;return this._fields=a.map(function(a){return c._sanitizeField(a)}),this._query=this._sanitizeNestableQuery(b)},b.prototype.buildStr=function(a){return 0>=this._fields.length?"":"("+this._fields.join(", ")+") ("+this._query.toString()+")"},b.prototype.buildParam=function(a){var b;return 0>=this._fields.length?{text:"",values:[]}:(b=this._query.toParam(),{text:"("+this._fields.join(", ")+") ("+b.text+")",values:b.values})},b}(d.Block),d.DistinctBlock=function(a){function b(a){b.__super__.constructor.call(this,a),this.useDistinct=!1}return l(b,a),b.prototype.distinct=function(){return this.useDistinct=!0},b.prototype.buildStr=function(a){return this.useDistinct?"DISTINCT":""},b}(d.Block),d.GroupByBlock=function(a){function b(a){b.__super__.constructor.call(this,a),this.groups=[]}return l(b,a),b.prototype.group=function(a){return a=this._sanitizeField(a),this.groups.push(a)},b.prototype.buildStr=function(a){var b,c,d,e,f;if(c="",0<this.groups.length){for(f=this.groups,d=0,e=f.length;e>d;d++)b=f[d],""!==c&&(c+=", "),c+=b;c="GROUP BY "+c}return c},b}(d.Block),d.OffsetBlock=function(a){function b(a){b.__super__.constructor.call(this,a),this.offsets=null}return l(b,a),b.prototype.offset=function(a){return a=this._sanitizeLimitOffset(a),this.offsets=a},b.prototype.buildStr=function(a){return this.offsets?"OFFSET "+this.offsets:""},b}(d.Block),d.AbstractConditionBlock=function(a){function b(a,c){this.conditionVerb=a,b.__super__.constructor.call(this,c),this.conditions=[]}return l(b,a),b.prototype._condition=function(){var a,b,c,e,f,g,h,j,k,l,m,n,o,p;if(b=arguments[0],l=2<=arguments.length?i.call(arguments,1):[],b=this._sanitizeCondition(b),c="",e=[],b instanceof d.Expression)k=b.toParam(),c=k.text,e=k.values;else for(f=m=0,p=b.length;p>=0?p>m:m>p;f=p>=0?++m:--m)if(a=b.charAt(f),this.options.parameterCharacter===a&&0<l.length)if(j=l.shift(),Array.isArray(j)){for(g=[],n=0,o=j.length;o>n;n++)h=j[n],g.push(this._sanitizeValue(h));e=e.concat(g),c+="("+function(){var a,b,c;for(c=[],a=0,b=g.length;b>a;a++)h=g[a],c.push(this.options.parameterCharacter);return c}.call(this).join(", ")+")"}else c+=this.options.parameterCharacter,e.push(this._sanitizeValue(j));else c+=a;return""!==c?this.conditions.push({text:c,values:e}):void 0},b.prototype.buildStr=function(a){var b,c,d,e,f,g,h,i,j,k;if(0>=this.conditions.length)return"";for(d="",j=this.conditions,g=0,i=j.length;i>g;g++)if(c=j[g],""!==d&&(d+=") AND ("),0<c.values.length)for(f=0,e=h=0,k=c.text.length;k>=0?k>h:h>k;e=k>=0?++h:--h)b=c.text.charAt(e),d+=this.options.parameterCharacter===b?this._formatValue(c.values[f++]):b;else d+=c.text;return""+this.conditionVerb+" ("+d+")"},b.prototype.buildParam=function(a){var b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r;if(g={text:"",values:[]},0>=this.conditions.length)return g;for(c="",p=this.conditions,j=0,m=p.length;m>j;j++){for(b=p[j],""!==c&&(c+=") AND ("),h=b.text.split(this.options.parameterCharacter),d=0,q=b.values,k=0,n=q.length;n>k;k++){if(i=q[k],null!=h[d]&&(c+=""+h[d]),e=this._formatValueAsParam(i),null!=(null!=e?e.text:void 0))for(c+="("+e.text+")",r=e.values,l=0,o=r.length;o>l;l++)f=r[l],g.values.push(f);else c+=this.options.parameterCharacter,g.values.push(e);d+=1}null!=h[d]&&(c+=""+h[d])}return g.text=""+this.conditionVerb+" ("+c+")",g},b}(d.Block),d.WhereBlock=function(a){function b(a){b.__super__.constructor.call(this,"WHERE",a)}return l(b,a),b.prototype.where=function(){var a,b;return a=arguments[0],b=2<=arguments.length?i.call(arguments,1):[],this._condition.apply(this,[a].concat(i.call(b)))},b}(d.AbstractConditionBlock),d.HavingBlock=function(a){function b(a){b.__super__.constructor.call(this,"HAVING",a)}return l(b,a),b.prototype.having=function(){var a,b;return a=arguments[0],b=2<=arguments.length?i.call(arguments,1):[],this._condition.apply(this,[a].concat(i.call(b)))},b}(d.AbstractConditionBlock),d.OrderByBlock=function(a){function b(a){b.__super__.constructor.call(this,a),this.orders=[],this._values=[]}return l(b,a),b.prototype.order=function(){var a,b,c;return b=arguments[0],a=arguments[1],c=3<=arguments.length?i.call(arguments,2):[],b=this._sanitizeField(b),void 0===a&&(a=!0),null!==a&&(a=!!a),this._values=c,this.orders.push({field:b,dir:a})},b.prototype._buildStr=function(a){var b,c,d,e,f,g,h,i,j,k,l;if(null==a&&(a=!1),0<this.orders.length){for(g=0,f="",k=this.orders,h=0,j=k.length;j>h;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+=this.options.parameterCharacter===b?this._formatValue(this._values[g++]):b;f+=""+c,null!==e.dir&&(f+=" "+(e.dir?"ASC":"DESC"))}return"ORDER BY "+f}return""},b.prototype.buildStr=function(a){return this._buildStr()},b.prototype.buildParam=function(a){var b=this;return{text:this._buildStr(!0),values:this._values.map(function(a){return b._formatValueAsParam(a)})}},b}(d.Block),d.LimitBlock=function(a){function b(a){b.__super__.constructor.call(this,a),this.limits=null}return l(b,a),b.prototype.limit=function(a){return a=this._sanitizeLimitOffset(a),this.limits=a},b.prototype.buildStr=function(a){return this.limits||0===this.limits?"LIMIT "+this.limits:""},b}(d.Block),d.JoinBlock=function(a){function b(a){b.__super__.constructor.call(this,a),this.joins=[]}return l(b,a),b.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},b.prototype.left_join=function(a,b,c){return null==b&&(b=null),null==c&&(c=null),this.join(a,b,c,"LEFT")},b.prototype.right_join=function(a,b,c){return null==b&&(b=null),null==c&&(c=null),this.join(a,b,c,"RIGHT")},b.prototype.outer_join=function(a,b,c){return null==b&&(b=null),null==c&&(c=null),this.join(a,b,c,"OUTER")},b.prototype.left_outer_join=function(a,b,c){return null==b&&(b=null),null==c&&(c=null),this.join(a,b,c,"LEFT OUTER")},b.prototype.full_join=function(a,b,c){return null==b&&(b=null),null==c&&(c=null),this.join(a,b,c,"FULL")},b.prototype.cross_join=function(a,b,c){return null==b&&(b=null),null==c&&(c=null),this.join(a,b,c,"CROSS")},b.prototype.buildStr=function(a){var b,c,d,e,f;for(c="",f=this.joins||[],d=0,e=f.length;e>d;d++)b=f[d],""!==c&&(c+=" "),c+=""+b.type+" JOIN ",c+="string"==typeof b.table?b.table:"("+b.table+")",b.alias&&(c+=" "+b.alias),b.condition&&(c+=" ON ("+b.condition+")");return c},b.prototype.buildParam=function(a){var b,c,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++)b=p[j],"string"==typeof b.table?f={text:""+b.table,values:[]}:b.table instanceof d.QueryBuilder?(b.table.updateOptions({nestedBuilder:!0}),f=b.table.toParam()):(b.updateOptions({nestedBuilder:!0}),f=b.buildParam(a)),b.condition instanceof d.Expression?(c=b.condition.toParam(),f.condition=c.text,f.values=f.values.concat(c.values)):f.condition=b.condition,f.join=b,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},b}(d.Block),d.UnionBlock=function(a){function b(a){b.__super__.constructor.call(this,a),this.unions=[]}return l(b,a),b.prototype.union=function(a,b){return null==b&&(b="UNION"),a=this._sanitizeTable(a,!0),this.unions.push({type:b,table:a}),this},b.prototype.union_all=function(a){return this.union(a,"UNION ALL")},b.prototype.buildStr=function(a){var b,c,d,e,f;for(c="",f=this.unions||[],d=0,e=f.length;e>d;d++)b=f[d],""!==c&&(c+=" "),c+=""+b.type+" ",c+="string"==typeof b.table?b.table:"("+b.table+")";return c},b.prototype.buildParam=function(a){var b,c,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++)b=o[i],"string"==typeof b.table?c={text:""+b.table,values:[]}:b.table instanceof d.QueryBuilder?(b.table.updateOptions({nestedBuilder:!0}),c=b.table.toParam()):(b.updateOptions({nestedBuilder:!0}),c=b.buildParam(a)),c.type=b.type,e.push(c);for(j=0,m=e.length;m>j;j++)for(c=e[j],""!==g&&(g+=" "),g+=""+c.type+" ("+c.text+")",p=c.values,k=0,n=p.length;n>k;k++)h=p[k],f.values.push(this._formatCustomValue(h));return f.text+=g,f},b}(d.Block),d.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 l(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,e,g;for(this.options=f({},this.options,a),e=this.blocks,g=[],c=0,d=e.length;d>c;c++)b=e[c],g.push(b.options=f({},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 0<a.length}).join(this.options.separator)},b.prototype.toParam=function(a){var b,c,d,e,g,h,i,j=this;return null==a&&(a=void 0),e=this.options,null!=a&&(this.options=f({},this.options,a)),h={text:"",values:[]},c=function(){var a,c,d,e;for(d=this.blocks,e=[],a=0,c=d.length;c>a;a++)b=d[a],e.push(b.buildParam(this));return e}.call(this),h.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 0<a.length}).join(this.options.separator),h.values=(i=[]).concat.apply(i,function(){var a,d,e;for(e=[],a=0,d=c.length;d>a;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=new RegExp("\\"+this.options.parameterCharacter,"g"),h.text=h.text.replace(g,function(){return""+j.options.numberedParametersPrefix+d++})),this.options=e,h},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.prototype.getBlock=function(a){return this.blocks.filter(function(b){return b instanceof a})[0]},b}(d.BaseBuilder),d.Select=function(a){function b(a,c){null==c&&(c=null),c||(c=[new d.StringBlock(a,"SELECT"),new d.FunctionBlock(a),new d.DistinctBlock(a),new d.GetFieldBlock(a),new d.FromTableBlock(f({},a,{allowNested:!0})),new d.JoinBlock(f({},a,{allowNested:!0})),new d.WhereBlock(a),new d.GroupByBlock(a),new d.HavingBlock(a),new d.OrderByBlock(a),new d.LimitBlock(a),new d.OffsetBlock(a),new d.UnionBlock(f({},a,{allowNested:!0}))]),b.__super__.constructor.call(this,a,c)}return l(b,a),b.prototype.isNestable=function(){return!0},b}(d.QueryBuilder),d.Update=function(a){function b(a,c){null==c&&(c=null),c||(c=[new d.StringBlock(a,"UPDATE"),new d.UpdateTableBlock(a),new d.SetFieldBlock(a),new d.WhereBlock(a),new d.OrderByBlock(a),new d.LimitBlock(a)]),b.__super__.constructor.call(this,a,c)}return l(b,a),b}(d.QueryBuilder),d.Delete=function(a){function b(a,c){null==c&&(c=null),c||(c=[new d.StringBlock(a,"DELETE"),new d.FromTableBlock(f({},a,{singleTable:!0})),new d.JoinBlock(a),new d.WhereBlock(a),new d.OrderByBlock(a),new d.LimitBlock(a)]),b.__super__.constructor.call(this,a,c)}return l(b,a),b}(d.QueryBuilder),d.Insert=function(a){function b(a,c){null==c&&(c=null),c||(c=[new d.StringBlock(a,"INSERT"),new d.IntoTableBlock(a),new d.InsertFieldValueBlock(a),new d.InsertFieldsFromQueryBlock(a)]),b.__super__.constructor.call(this,a,c)}return l(b,a),b}(d.QueryBuilder),r={VERSION:"4.3.1",flavour:c,expr:function(a){return new d.Expression(a)},"case":function(a,b){return new d.Case(a,b)},select:function(a,b){return new d.Select(a,b)},update:function(a,b){return new d.Update(a,b)},insert:function(a,b){return new d.Insert(a,b)},"delete":function(a,b){return new d.Delete(a,b)},registerValueHandler:d.registerValueHandler,fval:d.fval},r.remove=r["delete"],r.cls=d,r},c=d(),("undefined"!=typeof define&&null!==define?define.amd:void 0)?define(function(){return c}):("undefined"!=typeof module&&null!==module?module.exports:void 0)?module.exports=c:"undefined"!=typeof window&&null!==window&&(window.squel=c),c.flavours={},c.useFlavour=function(a){var b;if(null==a&&(a=null),!a)return c;
if(c.flavours[a]instanceof Function)return b=d(a),c.flavours[a].call(null,b),b;throw new Error("Flavour not available: "+a)}}).call(this);