From c1d2b2e46c8b733ef3b1f528ff2acf42f7e221ca Mon Sep 17 00:00:00 2001 From: nic Date: Tue, 29 Mar 2016 22:43:58 +0200 Subject: [PATCH 1/4] added auto-quoting ability to WhereBlock.where(), HavingBlock.having(), Expression.or() and Expression.and() by providing an alternate method signature (field, operator, ...params) --- squel-basic.js | 104 +++++++++++++-------- squel-basic.min.js | 4 +- squel.js | 104 +++++++++++++-------- squel.min.js | 4 +- src/core.js | 175 ++++++++++++++++++++--------------- test/blocks.test.coffee | 40 ++++++++ test/expressions.test.coffee | 13 +++ 7 files changed, 293 insertions(+), 151 deletions(-) diff --git a/squel-basic.js b/squel-basic.js index fc7c29a6..3a8bacf6 100644 --- a/squel-basic.js +++ b/squel-basic.js @@ -503,7 +503,7 @@ function _buildSquel() { return value; } - /** + /** * Format given value for inclusion into parameter values array. */ @@ -583,10 +583,10 @@ function _buildSquel() { return str; } - /** - * Build given string and its corresponding parameter values into + /** + * Build given string and its corresponding parameter values into * output. - * + * * @param {String} str * @param {Array} values * @param {Object} [options] Additional options. @@ -666,10 +666,10 @@ function _buildSquel() { }; } - /** - * Build all given strings and their corresponding parameter values into + /** + * Build all given strings and their corresponding parameter values into * output. - * + * * @param {Array} strings * @param {Array} strValues array of value arrays corresponding to each string. * @param {Object} [options] Additional options. @@ -713,7 +713,7 @@ function _buildSquel() { /** * Get parameterized representation of this instance. - * + * * @param {Object} [options] Options. * @param {Boolean} [options.buildParameterized] Whether to build paramterized string. Default is false. * @param {Boolean} [options.nested] Whether this expression is nested within another. @@ -771,10 +771,10 @@ function _buildSquel() { * * SQL expressions are used in WHERE and ON clauses to filter data by various criteria. * - * Expressions can be nested. Nested expression contains can themselves - * contain nested expressions. When rendered a nested expression will be + * Expressions can be nested. Nested expression contains can themselves + * contain nested expressions. When rendered a nested expression will be * fully contained within brackets. - * + * * All the build methods in this object return the object instance for chained method calling purposes. */ cls.Expression = function (_cls$BaseBuilder) { @@ -796,41 +796,55 @@ function _buildSquel() { _createClass(_class3, [{ key: 'and', - value: function and(expr) { - for (var _len3 = arguments.length, params = Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) { - params[_key3 - 1] = arguments[_key3]; + value: function and(field, operator) { + for (var _len3 = arguments.length, params = Array(_len3 > 2 ? _len3 - 2 : 0), _key3 = 2; _key3 < _len3; _key3++) { + params[_key3 - 2] = arguments[_key3]; } - expr = this._sanitizeExpression(expr); - - this._nodes.push({ - type: 'AND', - expr: expr, - para: params - }); - - return this; + return this._add('AND', field, operator, params); } // Combine the current expression with the given expression using the union operator (OR). }, { key: 'or', - value: function or(expr) { - for (var _len4 = arguments.length, params = Array(_len4 > 1 ? _len4 - 1 : 0), _key4 = 1; _key4 < _len4; _key4++) { - params[_key4 - 1] = arguments[_key4]; + value: function or(field, operator) { + for (var _len4 = arguments.length, params = Array(_len4 > 2 ? _len4 - 2 : 0), _key4 = 2; _key4 < _len4; _key4++) { + params[_key4 - 2] = arguments[_key4]; } - expr = this._sanitizeExpression(expr); + return this._add('OR', field, operator, params); + } + }, { + key: '_add', + value: function _add(type, field, operator, params) { + var validOperators = ['=', '<', '>', '<=', '>=', '<>', '!=', 'in', 'not in', 'like', 'not like']; + var expr = void 0; + if (typeof field === 'string' && typeof operator === 'string' && -1 != validOperators.indexOf(operator.toLowerCase())) { + expr = this._buildExpression(field, operator); + } else { + expr = field; + params.unshift(operator); + } + + this._sanitizeExpression(field); this._nodes.push({ - type: 'OR', + type: type, expr: expr, para: params }); return this; } + }, { + key: '_buildExpression', + value: function _buildExpression(field, operator) { + var escapedKey = this._formatFieldName(field); + var paramChar = cls.DefaultQueryBuilderOptions.parameterCharacter; + var condition = escapedKey + ' ' + operator + ' ' + paramChar; + return condition; + } }, { key: '_toParamString', value: function _toParamString() { @@ -1989,7 +2003,7 @@ function _buildSquel() { cls.AbstractConditionBlock = function (_cls$Block10) { _inherits(_class20, _cls$Block10); - /** + /** * @param {String} options.verb The condition verb. */ @@ -2092,12 +2106,20 @@ function _buildSquel() { _createClass(_class21, [{ key: 'where', - value: function where(condition) { - for (var _len8 = arguments.length, values = Array(_len8 > 1 ? _len8 - 1 : 0), _key8 = 1; _key8 < _len8; _key8++) { - values[_key8 - 1] = arguments[_key8]; + value: function where(field, operator) { + for (var _len8 = arguments.length, values = Array(_len8 > 2 ? _len8 - 2 : 0), _key8 = 2; _key8 < _len8; _key8++) { + values[_key8 - 2] = arguments[_key8]; } - this._condition.apply(this, [condition].concat(values)); + var validOperators = ['=', '<', '>', '<=', '>=', '<>', '!=', 'in', 'not in', 'like', 'not like']; + if (typeof field === 'string' && typeof operator === 'string' && -1 != validOperators.indexOf(operator.toLowerCase())) { + // key - operator - value + var expr = new cls.Expression(); + this._condition(expr._add('AND', field, operator, values)); + } else { + // default squel-behaviour without auto-quoting + this._condition.apply(this, arguments); + } } }]); @@ -2118,12 +2140,20 @@ function _buildSquel() { _createClass(_class22, [{ key: 'having', - value: function having(condition) { - for (var _len9 = arguments.length, values = Array(_len9 > 1 ? _len9 - 1 : 0), _key9 = 1; _key9 < _len9; _key9++) { - values[_key9 - 1] = arguments[_key9]; + value: function having(field, operator) { + for (var _len9 = arguments.length, values = Array(_len9 > 2 ? _len9 - 2 : 0), _key9 = 2; _key9 < _len9; _key9++) { + values[_key9 - 2] = arguments[_key9]; } - this._condition.apply(this, [condition].concat(values)); + var validOperators = ['=', '<', '>', '<=', '>=', '<>', '!=', 'in', 'not in', 'like', 'not like']; + if (typeof field === 'string' && typeof operator === 'string' && -1 != validOperators.indexOf(operator.toLowerCase())) { + // key - operator - value + var expr = new cls.Expression(); + this._condition(expr._add('AND', field, operator, values)); + } else { + // default squel-behaviour without auto-quoting + this._condition.apply(this, arguments); + } } }]); diff --git a/squel-basic.min.js b/squel-basic.min.js index 19307640..72b8d807 100644 --- a/squel-basic.min.js +++ b/squel-basic.min.js @@ -1,2 +1,2 @@ -/*! squel | https://github.com/hiddentao/squel | BSD license */!function(t,e){"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?module.exports=e():t.squel=e()}(this,function(){"use strict";function t(t){if(Array.isArray(t)){for(var e=0,r=Array(t.length);e1?e-1:0),n=1;e>n;n++)r[n-1]=arguments[n];if(t&&r){var i=!0,a=!1,o=void 0;try{for(var l,u=function(){var e=l.value;"object"===("undefined"==typeof e?"undefined":y(e))&&Object.getOwnPropertyNames(e).forEach(function(r){"function"!=typeof e[r]&&(t[r]=e[r])})},s=r[Symbol.iterator]();!(i=(l=s.next()).done);i=!0)u()}catch(f){a=!0,o=f}finally{try{!i&&s["return"]&&s["return"]()}finally{if(a)throw o}}}return t}function o(t){return t&&t.constructor.prototype===Object.prototype}function l(t){return t&&t.constructor.prototype===Array.prototype}function u(t){if(t&&t.constructor&&t.constructor.toString){var e=t.constructor.toString().match(/function\s*(\w+)/);if(e&&2===e.length)return e[1]}}function s(t){if(!t)return t;if("function"==typeof t.clone)return t.clone();if(!o(t)&&!l(t))return JSON.parse(JSON.stringify(t));var e=function(){var e=new t.constructor;return Object.getOwnPropertyNames(t).forEach(function(r){"function"!=typeof t[r]&&(e[r]=s(t[r]))}),{v:e}}();return"object"===("undefined"==typeof e?"undefined":y(e))?e.v:void 0}function f(t,e,r){var n="undefined"==typeof e?"undefined":y(e);if("function"!==n&&"string"!==n)throw new Error("type must be a class constructor or string");if("function"!=typeof r)throw new Error("handler must be a function");for(var i in t){var a=t[i];if(a.type===e)return void(a.handler=r)}t.push({type:e,handler:r})}function c(t){for(var e=arguments.length,r=Array(e>1?e-1:0),n=1;e>n;n++)r[n-1]=arguments[n];for(var i in r){var a=r[i];for(var o in a){var l=a[o];if(("undefined"==typeof t?"undefined":y(t))===l.type||"string"!=typeof l.type&&t instanceof l.type)return l.handler}}}function h(){var h=arguments.length<=0||void 0===arguments[0]?null:arguments[0],p={_getObjectClassName:u};p.DefaultQueryBuilderOptions={autoQuoteTableNames:!1,autoQuoteFieldNames:!1,autoQuoteAliasNames:!0,useAsForTableAliasNames:!1,nameQuoteCharacter:"`",tableAliasQuoteCharacter:"`",fieldAliasQuoteCharacter:'"',valueHandlers:[],parameterCharacter:"?",numberedParameters:!1,numberedParametersPrefix:"$",numberedParametersStartAt:1,replaceSingleQuotes:!1,singleQuoteReplacement:"''",separator:" "},p.globalValueHandlers=[],p.registerValueHandler=function(t,e){f(p.globalValueHandlers,t,e)},p.Cloneable=function(){function t(){n(this,t)}return d(t,[{key:"clone",value:function(){var t=new this.constructor;return a(t,s(a({},this)))}}]),t}(),p.BaseBuilder=function(i){function o(t){n(this,o);var r=e(this,Object.getPrototypeOf(o).call(this)),i=JSON.parse(JSON.stringify(p.DefaultQueryBuilderOptions));return r.options=a({},i,t),r}return r(o,i),d(o,[{key:"registerValueHandler",value:function(t,e){return f(this.options.valueHandlers,t,e),this}},{key:"_sanitizeExpression",value:function(t){if(!(t instanceof p.Expression)&&"string"!=typeof t)throw new Error("expression must be a string or Expression instance");return t}},{key:"_sanitizeName",value:function(t,e){if("string"!=typeof t)throw new Error(e+" must be a string");return t}},{key:"_sanitizeField",value:function(t){return t instanceof p.BaseBuilder||(t=this._sanitizeName(t,"field name")),t}},{key:"_sanitizeQueryBuilder",value:function(t){if(t instanceof p.QueryBuilder)return t;throw new Error("must be a QueryBuilder instance")}},{key:"_sanitizeTable",value:function(t){if("string"!=typeof t)try{t=this._sanitizeQueryBuilder(t)}catch(e){throw new Error("table name must be a string or a query builder")}else t=this._sanitizeName(t,"table");return t}},{key:"_sanitizeTableAlias",value:function(t){return this._sanitizeName(t,"table alias")}},{key:"_sanitizeFieldAlias",value:function(t){return this._sanitizeName(t,"field alias")}},{key:"_sanitizeLimitOffset",value:function(t){if(t=parseInt(t),0>t||isNaN(t))throw new Error("limit/offset must be >= 0");return t}},{key:"_sanitizeValue",value:function(t){var e="undefined"==typeof t?"undefined":y(t);if(null===t);else if("string"===e||"number"===e||"boolean"===e);else if(t instanceof p.BaseBuilder);else{var r=!!c(t,this.options.valueHandlers,p.globalValueHandlers);if(!r)throw new Error("field value must be a string, number, boolean, null or one of the registered custom value types")}return t}},{key:"_escapeValue",value:function(t){return this.options.replaceSingleQuotes?t.replace(/\'/g,this.options.singleQuoteReplacement):t}},{key:"_formatTableName",value:function(t){if(this.options.autoQuoteTableNames){var e=this.options.nameQuoteCharacter;t=""+e+t+e}return t}},{key:"_formatFieldAlias",value:function(t){if(this.options.autoQuoteAliasNames){var e=this.options.fieldAliasQuoteCharacter;t=""+e+t+e}return t}},{key:"_formatTableAlias",value:function(t){if(this.options.autoQuoteAliasNames){var e=this.options.tableAliasQuoteCharacter;t=""+e+t+e}return this.options.useAsForTableAliasNames?"AS "+t:t}},{key:"_formatFieldName",value:function(t){var e=this,r=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];return this.options.autoQuoteFieldNames&&!function(){var n=e.options.nameQuoteCharacter;t=r.ignorePeriodsForFieldNameQuotes?""+n+t+n:t.split(".").map(function(t){return"*"===t?t:""+n+t+n}).join(".")}(),t}},{key:"_formatCustomValue",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?!1:arguments[1],r=c(t,this.options.valueHandlers,p.globalValueHandlers);return r&&(t=r(t,e)),t}},{key:"_formatValueForParamArray",value:function(t){var e=this;return l(t)?t.map(function(t){return e._formatValueForParamArray(t)}):this._formatCustomValue(t,!0)}},{key:"_formatValueForQueryString",value:function(t){var e=this,r=arguments.length<=1||void 0===arguments[1]?{}:arguments[1],n=this._formatCustomValue(t);if(n!==t)return this._applyNestingFormatting(n);if(l(t))t=t.map(function(t){return e._formatValueForQueryString(t)}),t=this._applyNestingFormatting(t.join(", "));else{var i="undefined"==typeof t?"undefined":y(t);if(null===t)t="NULL";else if("boolean"===i)t=t?"TRUE":"FALSE";else if(t instanceof p.BaseBuilder)t=this._applyNestingFormatting(t.toString());else if("number"!==i)if(r.dontQuote)t=""+t;else{var a=this._escapeValue(t);t="'"+a+"'"}}return t}},{key:"_applyNestingFormatting",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?!0:arguments[1];return t&&"string"==typeof t&&e&&("("!==t.charAt(0)||")"!==t.charAt(t.length-1))?"("+t+")":t}},{key:"_buildString",value:function(e,r){var n=arguments.length<=2||void 0===arguments[2]?{}:arguments[2],i=n.nested,a=n.buildParameterized,o=n.formattingOptions;r=r||[],e=e||"";for(var u="",s=-1,f=[],c=this.options.parameterCharacter,h=0;e.length>h;)if(e.substr(h,c.length)===c){var v=r[++s];if(a)if(v instanceof p.BaseBuilder){var d=v._toParamString({buildParameterized:a,nested:!0});u+=d.text,f.push.apply(f,t(d.values))}else if(v=this._formatValueForParamArray(v),l(v)){var y=v.map(function(){return c}).join(", ");u+="("+y+")",f.push.apply(f,t(v))}else u+=c,f.push(v);else u+=this._formatValueForQueryString(v,o);h+=c.length}else u+=e.charAt(h),h++;return{text:this._applyNestingFormatting(u,!!i),values:f}}},{key:"_buildManyStrings",value:function(e,r){var n=arguments.length<=2||void 0===arguments[2]?{}:arguments[2],i=[],a=[];for(var o in e){var l=e[o],u=r[o],s=this._buildString(l,u,{buildParameterized:n.buildParameterized,nested:!1}),f=s.text,c=s.values;i.push(f),a.push.apply(a,t(c))}return i=i.join(this.options.separator),{text:i.length?this._applyNestingFormatting(i,!!n.nested):"",values:a}}},{key:"_toParamString",value:function(t){throw new Error("Not yet implemented")}},{key:"toString",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return this._toParamString(t).text}},{key:"toParam",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return this._toParamString(a({},t,{buildParameterized:!0}))}}]),o}(p.Cloneable),p.Expression=function(i){function a(t){n(this,a);var r=e(this,Object.getPrototypeOf(a).call(this,t));return r._nodes=[],r}return r(a,i),d(a,[{key:"and",value:function(t){for(var e=arguments.length,r=Array(e>1?e-1:0),n=1;e>n;n++)r[n-1]=arguments[n];return t=this._sanitizeExpression(t),this._nodes.push({type:"AND",expr:t,para:r}),this}},{key:"or",value:function(t){for(var e=arguments.length,r=Array(e>1?e-1:0),n=1;e>n;n++)r[n-1]=arguments[n];return t=this._sanitizeExpression(t),this._nodes.push({type:"OR",expr:t,para:r}),this}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r=[],n=[],i=!0,a=!1,o=void 0;try{for(var l,u=this._nodes[Symbol.iterator]();!(i=(l=u.next()).done);i=!0){var s=l.value,f=s.type,c=s.expr,h=s.para,v=c instanceof p.Expression?c._toParamString({buildParameterized:e.buildParameterized,nested:!0}):this._buildString(c,h,{buildParameterized:e.buildParameterized}),d=v.text,y=v.values;r.length&&r.push(f),r.push(d),n.push.apply(n,t(y))}}catch(g){a=!0,o=g}finally{try{!i&&u["return"]&&u["return"]()}finally{if(a)throw o}}return r=r.join(" "),{text:this._applyNestingFormatting(r,!!e.nested),values:n}}}]),a}(p.BaseBuilder),p.Case=function(l){function u(t){var r=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];n(this,u);var i=e(this,Object.getPrototypeOf(u).call(this,r));return o(t)&&(r=t,t=null),t&&(i._fieldName=i._sanitizeField(t)),i.options=a({},p.DefaultQueryBuilderOptions,r),i._cases=[],i._elseValue=null,i}return r(u,l),d(u,[{key:"when",value:function(t){for(var e=arguments.length,r=Array(e>1?e-1:0),n=1;e>n;n++)r[n-1]=arguments[n];return this._cases.unshift({expression:t,values:r}),this}},{key:"then",value:function(t){if(0==this._cases.length)throw new Error("when() needs to be called first");return this._cases[0].result=t,this}},{key:"else",value:function(t){return this._elseValue=t,this}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r="",n=[],a=!0,o=!1,l=void 0;try{for(var u,s=this._cases[Symbol.iterator]();!(a=(u=s.next()).done);a=!0){var f=u.value,c=f.expression,h=f.values,v=f.result;r=i(r," ");var d=this._buildString(c,h,{buildParameterized:e.buildParameterized,nested:!0});r+="WHEN "+d.text+" THEN "+this._formatValueForQueryString(v),n.push.apply(n,t(d.values))}}catch(y){o=!0,l=y}finally{try{!a&&s["return"]&&s["return"]()}finally{if(o)throw l}}return r.length?(r+=" ELSE "+this._formatValueForQueryString(this._elseValue)+" END",this._fieldName&&(r=this._fieldName+" "+r),r="CASE "+r):r=this._formatValueForQueryString(this._elseValue),{text:r,values:n}}}]),u}(p.BaseBuilder),p.Block=function(t){function i(t){return n(this,i),e(this,Object.getPrototypeOf(i).call(this,t))}return r(i,t),d(i,[{key:"exposedMethods",value:function(){for(var t={},e=this;e;)Object.getOwnPropertyNames(e).forEach(function(r){"constructor"===r||"function"!=typeof e[r]||"_"===r.charAt(0)||p.Block.prototype[r]||(t[r]=e[r])}),e=Object.getPrototypeOf(e);return t}}]),i}(p.BaseBuilder),p.StringBlock=function(t){function i(t,r){n(this,i);var a=e(this,Object.getPrototypeOf(i).call(this,t));return a._str=r,a}return r(i,t),d(i,[{key:"_toParamString",value:function(){arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return{text:this._str,values:[]}}}]),i}(p.Block),p.FunctionBlock=function(t){function i(t){n(this,i);var r=e(this,Object.getPrototypeOf(i).call(this,t));return r._strings=[],r._values=[],r}return r(i,t),d(i,[{key:"function",value:function(t){this._strings.push(t);for(var e=arguments.length,r=Array(e>1?e-1:0),n=1;e>n;n++)r[n-1]=arguments[n];this._values.push(r)}},{key:"_toParamString",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return this._buildManyStrings(this._strings,this._values,t)}}]),i}(p.Block),p.registerValueHandler(p.FunctionBlock,function(t){var e=arguments.length<=1||void 0===arguments[1]?!1:arguments[1];return e?t.toParam():t.toString()}),p.AbstractTableBlock=function(a){function o(t,r){n(this,o);var i=e(this,Object.getPrototypeOf(o).call(this,t));return i._tables=[],i}return r(o,a),d(o,[{key:"_table",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1];e=e?this._sanitizeTableAlias(e):e,t=this._sanitizeTable(t),this.options.singleTable&&(this._tables=[]),this._tables.push({table:t,alias:e})}},{key:"_hasTable",value:function(){return 01)throw new Error("Cannot set multiple rows of fields this way.");"undefined"!=typeof e&&(e=this._sanitizeValue(e)),t=this._sanitizeField(t);var n=this._fields.indexOf(t);-1===n&&(this._fields.push(t),n=this._fields.length-1),this._values[0][n]=e,this._valueOptions[0][n]=r}},{key:"_setFields",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];if("object"!==("undefined"==typeof t?"undefined":y(t)))throw new Error("Expected an object but got "+("undefined"==typeof t?"undefined":y(t)));for(var r in t)this._set(r,t[r],e)}},{key:"_setFieldsRows",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];if(!l(t))throw new Error("Expected an array of objects but got "+("undefined"==typeof t?"undefined":y(t)));this._reset();for(var r in t){var n=t[r];for(var i in n){var a=n[i];i=this._sanitizeField(i),a=this._sanitizeValue(a);var o=this._fields.indexOf(i);if(r>0&&-1===o)throw new Error("All fields in subsequent rows must match the fields in the first row");-1===o&&(this._fields.push(i),o=this._fields.length-1),l(this._values[r])||(this._values[r]=[],this._valueOptions[r]=[]),this._values[r][o]=a,this._valueOptions[r][o]=e}}}}]),i}(p.Block),p.SetFieldBlock=function(a){function o(){return n(this,o),e(this,Object.getPrototypeOf(o).apply(this,arguments))}return r(o,a),d(o,[{key:"set",value:function(t,e,r){this._set(t,e,r)}},{key:"setFields",value:function(t,e){this._setFields(t,e)}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r=e.buildParameterized;if(0>=this._fields.length)throw new Error("set() needs to be called");var n="",a=[];for(var o in this._fields){n=i(n,", ");var l=this._fields[o],u=this._values[0][o];if("undefined"==typeof u)n+=l;else{var s=this._buildString(l+" = "+this.options.parameterCharacter,[u],{buildParameterized:r,formattingOptions:this._valueOptions[0][o]});n+=s.text,a.push.apply(a,t(s.values))}}return{text:"SET "+n,values:a}}}]),o}(p.AbstractSetFieldBlock),p.InsertFieldValueBlock=function(a){function o(){return n(this,o),e(this,Object.getPrototypeOf(o).apply(this,arguments))}return r(o,a),d(o,[{key:"set",value:function(t,e){var r=arguments.length<=2||void 0===arguments[2]?{}:arguments[2];this._set(t,e,r)}},{key:"setFields",value:function(t,e){this._setFields(t,e)}},{key:"setFieldsRows",value:function(t,e){this._setFieldsRows(t,e)}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r=e.buildParameterized,n=this._fields.join(", "),a=[],o=[];for(var l in this._values){a[l]="";for(var u in this._values[l]){var s=this._buildString(this.options.parameterCharacter,[this._values[l][u]],{buildParameterized:r,formattingOptions:this._valueOptions[l][u]});o.push.apply(o,t(s.values)),a[l]=i(a[l],", "),a[l]+=s.text}}return{text:n.length?"("+n+") VALUES ("+a.join("), (")+")":"",values:o}}}]),o}(p.AbstractSetFieldBlock),p.InsertFieldsFromQueryBlock=function(t){function i(t){n(this,i);var r=e(this,Object.getPrototypeOf(i).call(this,t));return r._fields=[],r._query=null,r}return r(i,t),d(i,[{key:"fromQuery",value:function(t,e){var r=this;this._fields=t.map(function(t){return r._sanitizeField(t)}),this._query=this._sanitizeQueryBuilder(e)}},{key:"_toParamString",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],e="",r=[];if(this._fields.length&&this._query){var n=this._query._toParamString({buildParameterized:t.buildParameterized,nested:!0}),i=n.text,a=n.values;e="("+this._fields.join(", ")+") ("+i+")",r=a}return{text:e,values:r}}}]),i}(p.Block),p.DistinctBlock=function(t){function i(){return n(this,i),e(this,Object.getPrototypeOf(i).apply(this,arguments))}return r(i,t),d(i,[{key:"distinct",value:function(){this._useDistinct=!0}},{key:"_toParamString",value:function(){return{text:this._useDistinct?"DISTINCT":"",values:[]}}}]),i}(p.Block),p.GroupByBlock=function(t){function i(t){n(this,i);var r=e(this,Object.getPrototypeOf(i).call(this,t));return r._groups=[],r}return r(i,t),d(i,[{key:"group",value:function(t){this._groups.push(this._sanitizeField(t))}},{key:"_toParamString",value:function(){arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return{text:this._groups.length?"GROUP BY "+this._groups.join(", "):"",values:[]}}}]),i}(p.Block),p.OffsetBlock=function(t){function i(t){n(this,i);var r=e(this,Object.getPrototypeOf(i).call(this,t));return r._offsets=null,r}return r(i,t),d(i,[{key:"offset",value:function(t){this._offsets=this._sanitizeLimitOffset(t)}},{key:"_toParamString",value:function(){return{text:this._offsets?"OFFSET "+this._offsets:"",values:[]}}}]),i}(p.Block),p.AbstractConditionBlock=function(a){function o(t){n(this,o);var r=e(this,Object.getPrototypeOf(o).call(this,t));return r._conditions=[],r}return r(o,a),d(o,[{key:"_condition",value:function(t){for(var e=arguments.length,r=Array(e>1?e-1:0),n=1;e>n;n++)r[n-1]=arguments[n];t=this._sanitizeExpression(t),this._conditions.push({expr:t,values:r})}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r="",n=[],a=!0,o=!1,l=void 0;try{for(var u,s=this._conditions[Symbol.iterator]();!(a=(u=s.next()).done);a=!0){var f=u.value,c=f.expr,h=f.values;r=i(r,") AND (");var v=c instanceof p.Expression?c._toParamString({buildParameterized:e.buildParameterized}):this._buildString(c,h,{buildParameterized:e.buildParameterized});r+=v.text,n.push.apply(n,t(v.values))}}catch(d){o=!0,l=d}finally{try{!a&&s["return"]&&s["return"]()}finally{if(o)throw l}}return{text:r.length?this.options.verb+" ("+r+")":"",values:n}}}]),o}(p.Block),p.WhereBlock=function(t){function i(t){return n(this,i),e(this,Object.getPrototypeOf(i).call(this,a({},t,{verb:"WHERE"})))}return r(i,t),d(i,[{key:"where",value:function(t){for(var e=arguments.length,r=Array(e>1?e-1:0),n=1;e>n;n++)r[n-1]=arguments[n];this._condition.apply(this,[t].concat(r))}}]),i}(p.AbstractConditionBlock),p.HavingBlock=function(t){function i(t){return n(this,i),e(this,Object.getPrototypeOf(i).call(this,a({},t,{verb:"HAVING"})))}return r(i,t),d(i,[{key:"having",value:function(t){for(var e=arguments.length,r=Array(e>1?e-1:0),n=1;e>n;n++)r[n-1]=arguments[n];this._condition.apply(this,[t].concat(r))}}]),i}(p.AbstractConditionBlock),p.OrderByBlock=function(a){function o(t){n(this,o);var r=e(this,Object.getPrototypeOf(o).call(this,t));return r._orders=[],r}return r(o,a),d(o,[{key:"order",value:function(t,e){for(var r=arguments.length,n=Array(r>2?r-2:0),i=2;r>i;i++)n[i-2]=arguments[i];t=this._sanitizeField(t),e=void 0===e?!0:e,e=null!==e?!!e:e,this._orders.push({field:t,dir:e,values:n})}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r="",n=[],a=!0,o=!1,l=void 0;try{for(var u,s=this._orders[Symbol.iterator]();!(a=(u=s.next()).done);a=!0){var f=u.value,c=f.field,h=f.dir,v=f.values;r=i(r,", ");var d=this._buildString(c,v,{buildParameterized:e.buildParameterized});r+=d.text,n.push.apply(n,t(d.values)),null!==h&&(r+=" "+(h?"ASC":"DESC"))}}catch(y){o=!0,l=y}finally{try{!a&&s["return"]&&s["return"]()}finally{if(o)throw l}}return{text:r.length?"ORDER BY "+r:"",values:n}}}]),o}(p.Block),p.LimitBlock=function(t){function i(t){n(this,i);var r=e(this,Object.getPrototypeOf(i).call(this,t));return r._limit=null,r}return r(i,t),d(i,[{key:"limit",value:function(t){this._limit=this._sanitizeLimitOffset(t)}},{key:"_toParamString",value:function(){return{text:null!==this._limit?"LIMIT "+this._limit:"",values:[]}}}]),i}(p.Block),p.JoinBlock=function(a){function o(t){n(this,o);var r=e(this,Object.getPrototypeOf(o).call(this,t));return r._joins=[],r}return r(o,a),d(o,[{key:"join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],r=arguments.length<=2||void 0===arguments[2]?null:arguments[2],n=arguments.length<=3||void 0===arguments[3]?"INNER":arguments[3];t=this._sanitizeTable(t,!0),e=e?this._sanitizeTableAlias(e):e,r=r?this._sanitizeExpression(r):r,this._joins.push({type:n,table:t,alias:e,condition:r})}},{key:"left_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],r=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,r,"LEFT")}},{key:"right_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],r=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,r,"RIGHT")}},{key:"outer_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],r=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,r,"OUTER")}},{key:"left_outer_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],r=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,r,"LEFT OUTER")}},{key:"full_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],r=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,r,"FULL")}},{key:"cross_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],r=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,r,"CROSS")}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r="",n=[],a=!0,o=!1,l=void 0;try{for(var u,s=this._joins[Symbol.iterator]();!(a=(u=s.next()).done);a=!0){var f=u.value,c=f.type,h=f.table,v=f.alias,d=f.condition;r=i(r,this.options.separator);var y=void 0;if(h instanceof p.BaseBuilder){var g=h._toParamString({buildParameterized:e.buildParameterized,nested:!0});n.push.apply(n,t(g.values)),y=g.text}else y=this._formatTableName(h);if(r+=c+" JOIN "+y,v&&(r+=" "+this._formatTableAlias(v)),d){r+=" ON ";var _=void 0;_=d instanceof p.Expression?d._toParamString({buildParameterized:e.buildParameterized}):this._buildString(d,[],{buildParameterized:e.buildParameterized}),r+=this._applyNestingFormatting(_.text),n.push.apply(n,t(_.values))}}}catch(m){o=!0,l=m}finally{try{!a&&s["return"]&&s["return"]()}finally{if(o)throw l}}return{text:r,values:n}}}]),o}(p.Block),p.UnionBlock=function(a){function o(t){n(this,o);var r=e(this,Object.getPrototypeOf(o).call(this,t));return r._unions=[],r}return r(o,a),d(o,[{key:"union",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?"UNION":arguments[1];t=this._sanitizeTable(t),this._unions.push({type:e,table:t})}},{key:"union_all",value:function(t){this.union(t,"UNION ALL")}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r="",n=[],a=!0,o=!1,l=void 0;try{for(var u,s=this._unions[Symbol.iterator]();!(a=(u=s.next()).done);a=!0){var f=u.value,c=f.type,h=f.table;r=i(r,this.options.separator);var v=void 0;if(h instanceof p.BaseBuilder){var d=h._toParamString({buildParameterized:e.buildParameterized,nested:!0});v=d.text,n.push.apply(n,t(d.values))}else r=this._formatTableName(h);r+=c+" "+v}}catch(y){o=!0,l=y}finally{try{!a&&s["return"]&&s["return"]()}finally{if(o)throw l}}return{text:r,values:n}}}]),o}(p.Block),p.QueryBuilder=function(i){function o(t,r){n(this,o);var i=e(this,Object.getPrototypeOf(o).call(this,t));i.blocks=r||[];var a=!0,l=!1,u=void 0;try{for(var s,f=i.blocks[Symbol.iterator]();!(a=(s=f.next()).done);a=!0){var c=s.value,h=c.exposedMethods();for(var v in h){var d=h[v];if(void 0!==i[v])throw new Error("Builder already has a builder method called: "+v);!function(t,e,r){i[e]=function(){for(var e=arguments.length,n=Array(e),a=0;e>a;a++)n[a]=arguments[a];return r.call.apply(r,[t].concat(n)),i}}(c,v,d)}}}catch(y){l=!0,u=y}finally{try{!a&&f["return"]&&f["return"]()}finally{if(l)throw u}}return i}return r(o,i),d(o,[{key:"registerValueHandler",value:function(t,e){var r=!0,n=!1,i=void 0;try{for(var a,l=this.blocks[Symbol.iterator]();!(r=(a=l.next()).done);r=!0){var u=a.value;u.registerValueHandler(t,e)}}catch(s){n=!0,i=s}finally{try{!r&&l["return"]&&l["return"]()}finally{if(n)throw i}}return v(Object.getPrototypeOf(o.prototype),"registerValueHandler",this).call(this,t,e),this}},{key:"updateOptions",value:function(t){this.options=a({},this.options,t);var e=!0,r=!1,n=void 0;try{for(var i,o=this.blocks[Symbol.iterator]();!(e=(i=o.next()).done);e=!0){var l=i.value;l.options=a({},l.options,t)}}catch(u){r=!0,n=u}finally{try{!e&&o["return"]&&o["return"]()}finally{if(r)throw n}}}},{key:"_toParamString",value:function(){var e,r=this,n=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];n=a({},this.options,n);var i=this.blocks.map(function(t){return t._toParamString({buildParameterized:n.buildParameterized,queryBuilder:r})}),o=i.map(function(t){return t.text}),l=i.map(function(t){return t.values}),u=o.filter(function(t){return 01?e-1:0),n=1;e>n;n++)r[n-1]=arguments[n];if(t&&r){var i=!0,a=!1,o=void 0;try{for(var l,u=function(){var e=l.value;"object"===("undefined"==typeof e?"undefined":y(e))&&Object.getOwnPropertyNames(e).forEach(function(r){"function"!=typeof e[r]&&(t[r]=e[r])})},s=r[Symbol.iterator]();!(i=(l=s.next()).done);i=!0)u()}catch(f){a=!0,o=f}finally{try{!i&&s["return"]&&s["return"]()}finally{if(a)throw o}}}return t}function o(t){return t&&t.constructor.prototype===Object.prototype}function l(t){return t&&t.constructor.prototype===Array.prototype}function u(t){if(t&&t.constructor&&t.constructor.toString){var e=t.constructor.toString().match(/function\s*(\w+)/);if(e&&2===e.length)return e[1]}}function s(t){if(!t)return t;if("function"==typeof t.clone)return t.clone();if(!o(t)&&!l(t))return JSON.parse(JSON.stringify(t));var e=function(){var e=new t.constructor;return Object.getOwnPropertyNames(t).forEach(function(r){"function"!=typeof t[r]&&(e[r]=s(t[r]))}),{v:e}}();return"object"===("undefined"==typeof e?"undefined":y(e))?e.v:void 0}function f(t,e,r){var n="undefined"==typeof e?"undefined":y(e);if("function"!==n&&"string"!==n)throw new Error("type must be a class constructor or string");if("function"!=typeof r)throw new Error("handler must be a function");for(var i in t){var a=t[i];if(a.type===e)return void(a.handler=r)}t.push({type:e,handler:r})}function c(t){for(var e=arguments.length,r=Array(e>1?e-1:0),n=1;e>n;n++)r[n-1]=arguments[n];for(var i in r){var a=r[i];for(var o in a){var l=a[o];if(("undefined"==typeof t?"undefined":y(t))===l.type||"string"!=typeof l.type&&t instanceof l.type)return l.handler}}}function h(){var h=arguments.length<=0||void 0===arguments[0]?null:arguments[0],p={_getObjectClassName:u};p.DefaultQueryBuilderOptions={autoQuoteTableNames:!1,autoQuoteFieldNames:!1,autoQuoteAliasNames:!0,useAsForTableAliasNames:!1,nameQuoteCharacter:"`",tableAliasQuoteCharacter:"`",fieldAliasQuoteCharacter:'"',valueHandlers:[],parameterCharacter:"?",numberedParameters:!1,numberedParametersPrefix:"$",numberedParametersStartAt:1,replaceSingleQuotes:!1,singleQuoteReplacement:"''",separator:" "},p.globalValueHandlers=[],p.registerValueHandler=function(t,e){f(p.globalValueHandlers,t,e)},p.Cloneable=function(){function t(){n(this,t)}return d(t,[{key:"clone",value:function(){var t=new this.constructor;return a(t,s(a({},this)))}}]),t}(),p.BaseBuilder=function(i){function o(t){n(this,o);var r=e(this,Object.getPrototypeOf(o).call(this)),i=JSON.parse(JSON.stringify(p.DefaultQueryBuilderOptions));return r.options=a({},i,t),r}return r(o,i),d(o,[{key:"registerValueHandler",value:function(t,e){return f(this.options.valueHandlers,t,e),this}},{key:"_sanitizeExpression",value:function(t){if(!(t instanceof p.Expression)&&"string"!=typeof t)throw new Error("expression must be a string or Expression instance");return t}},{key:"_sanitizeName",value:function(t,e){if("string"!=typeof t)throw new Error(e+" must be a string");return t}},{key:"_sanitizeField",value:function(t){return t instanceof p.BaseBuilder||(t=this._sanitizeName(t,"field name")),t}},{key:"_sanitizeQueryBuilder",value:function(t){if(t instanceof p.QueryBuilder)return t;throw new Error("must be a QueryBuilder instance")}},{key:"_sanitizeTable",value:function(t){if("string"!=typeof t)try{t=this._sanitizeQueryBuilder(t)}catch(e){throw new Error("table name must be a string or a query builder")}else t=this._sanitizeName(t,"table");return t}},{key:"_sanitizeTableAlias",value:function(t){return this._sanitizeName(t,"table alias")}},{key:"_sanitizeFieldAlias",value:function(t){return this._sanitizeName(t,"field alias")}},{key:"_sanitizeLimitOffset",value:function(t){if(t=parseInt(t),0>t||isNaN(t))throw new Error("limit/offset must be >= 0");return t}},{key:"_sanitizeValue",value:function(t){var e="undefined"==typeof t?"undefined":y(t);if(null===t);else if("string"===e||"number"===e||"boolean"===e);else if(t instanceof p.BaseBuilder);else{var r=!!c(t,this.options.valueHandlers,p.globalValueHandlers);if(!r)throw new Error("field value must be a string, number, boolean, null or one of the registered custom value types")}return t}},{key:"_escapeValue",value:function(t){return this.options.replaceSingleQuotes?t.replace(/\'/g,this.options.singleQuoteReplacement):t}},{key:"_formatTableName",value:function(t){if(this.options.autoQuoteTableNames){var e=this.options.nameQuoteCharacter;t=""+e+t+e}return t}},{key:"_formatFieldAlias",value:function(t){if(this.options.autoQuoteAliasNames){var e=this.options.fieldAliasQuoteCharacter;t=""+e+t+e}return t}},{key:"_formatTableAlias",value:function(t){if(this.options.autoQuoteAliasNames){var e=this.options.tableAliasQuoteCharacter;t=""+e+t+e}return this.options.useAsForTableAliasNames?"AS "+t:t}},{key:"_formatFieldName",value:function(t){var e=this,r=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];return this.options.autoQuoteFieldNames&&!function(){var n=e.options.nameQuoteCharacter;t=r.ignorePeriodsForFieldNameQuotes?""+n+t+n:t.split(".").map(function(t){return"*"===t?t:""+n+t+n}).join(".")}(),t}},{key:"_formatCustomValue",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?!1:arguments[1],r=c(t,this.options.valueHandlers,p.globalValueHandlers);return r&&(t=r(t,e)),t}},{key:"_formatValueForParamArray",value:function(t){var e=this;return l(t)?t.map(function(t){return e._formatValueForParamArray(t)}):this._formatCustomValue(t,!0)}},{key:"_formatValueForQueryString",value:function(t){var e=this,r=arguments.length<=1||void 0===arguments[1]?{}:arguments[1],n=this._formatCustomValue(t);if(n!==t)return this._applyNestingFormatting(n);if(l(t))t=t.map(function(t){return e._formatValueForQueryString(t)}),t=this._applyNestingFormatting(t.join(", "));else{var i="undefined"==typeof t?"undefined":y(t);if(null===t)t="NULL";else if("boolean"===i)t=t?"TRUE":"FALSE";else if(t instanceof p.BaseBuilder)t=this._applyNestingFormatting(t.toString());else if("number"!==i)if(r.dontQuote)t=""+t;else{var a=this._escapeValue(t);t="'"+a+"'"}}return t}},{key:"_applyNestingFormatting",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?!0:arguments[1];return t&&"string"==typeof t&&e&&("("!==t.charAt(0)||")"!==t.charAt(t.length-1))?"("+t+")":t}},{key:"_buildString",value:function(e,r){var n=arguments.length<=2||void 0===arguments[2]?{}:arguments[2],i=n.nested,a=n.buildParameterized,o=n.formattingOptions;r=r||[],e=e||"";for(var u="",s=-1,f=[],c=this.options.parameterCharacter,h=0;e.length>h;)if(e.substr(h,c.length)===c){var v=r[++s];if(a)if(v instanceof p.BaseBuilder){var d=v._toParamString({buildParameterized:a,nested:!0});u+=d.text,f.push.apply(f,t(d.values))}else if(v=this._formatValueForParamArray(v),l(v)){var y=v.map(function(){return c}).join(", ");u+="("+y+")",f.push.apply(f,t(v))}else u+=c,f.push(v);else u+=this._formatValueForQueryString(v,o);h+=c.length}else u+=e.charAt(h),h++;return{text:this._applyNestingFormatting(u,!!i),values:f}}},{key:"_buildManyStrings",value:function(e,r){var n=arguments.length<=2||void 0===arguments[2]?{}:arguments[2],i=[],a=[];for(var o in e){var l=e[o],u=r[o],s=this._buildString(l,u,{buildParameterized:n.buildParameterized,nested:!1}),f=s.text,c=s.values;i.push(f),a.push.apply(a,t(c))}return i=i.join(this.options.separator),{text:i.length?this._applyNestingFormatting(i,!!n.nested):"",values:a}}},{key:"_toParamString",value:function(t){throw new Error("Not yet implemented")}},{key:"toString",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return this._toParamString(t).text}},{key:"toParam",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return this._toParamString(a({},t,{buildParameterized:!0}))}}]),o}(p.Cloneable),p.Expression=function(i){function a(t){n(this,a);var r=e(this,Object.getPrototypeOf(a).call(this,t));return r._nodes=[],r}return r(a,i),d(a,[{key:"and",value:function(t,e){for(var r=arguments.length,n=Array(r>2?r-2:0),i=2;r>i;i++)n[i-2]=arguments[i];return this._add("AND",t,e,n)}},{key:"or",value:function(t,e){for(var r=arguments.length,n=Array(r>2?r-2:0),i=2;r>i;i++)n[i-2]=arguments[i];return this._add("OR",t,e,n)}},{key:"_add",value:function(t,e,r,n){var i=["=","<",">","<=",">=","<>","!=","in","not in","like","not like"],a=void 0;return"string"==typeof e&&"string"==typeof r&&-1!=i.indexOf(r.toLowerCase())?a=this._buildExpression(e,r):(a=e,n.unshift(r)),this._sanitizeExpression(e),this._nodes.push({type:t,expr:a,para:n}),this}},{key:"_buildExpression",value:function(t,e){var r=this._formatFieldName(t),n=p.DefaultQueryBuilderOptions.parameterCharacter,i=r+" "+e+" "+n;return i}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r=[],n=[],i=!0,a=!1,o=void 0;try{for(var l,u=this._nodes[Symbol.iterator]();!(i=(l=u.next()).done);i=!0){var s=l.value,f=s.type,c=s.expr,h=s.para,v=c instanceof p.Expression?c._toParamString({buildParameterized:e.buildParameterized,nested:!0}):this._buildString(c,h,{buildParameterized:e.buildParameterized}),d=v.text,y=v.values;r.length&&r.push(f),r.push(d),n.push.apply(n,t(y))}}catch(g){a=!0,o=g}finally{try{!i&&u["return"]&&u["return"]()}finally{if(a)throw o}}return r=r.join(" "),{text:this._applyNestingFormatting(r,!!e.nested),values:n}}}]),a}(p.BaseBuilder),p.Case=function(l){function u(t){var r=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];n(this,u);var i=e(this,Object.getPrototypeOf(u).call(this,r));return o(t)&&(r=t,t=null),t&&(i._fieldName=i._sanitizeField(t)),i.options=a({},p.DefaultQueryBuilderOptions,r),i._cases=[],i._elseValue=null,i}return r(u,l),d(u,[{key:"when",value:function(t){for(var e=arguments.length,r=Array(e>1?e-1:0),n=1;e>n;n++)r[n-1]=arguments[n];return this._cases.unshift({expression:t,values:r}),this}},{key:"then",value:function(t){if(0==this._cases.length)throw new Error("when() needs to be called first");return this._cases[0].result=t,this}},{key:"else",value:function(t){return this._elseValue=t,this}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r="",n=[],a=!0,o=!1,l=void 0;try{for(var u,s=this._cases[Symbol.iterator]();!(a=(u=s.next()).done);a=!0){var f=u.value,c=f.expression,h=f.values,v=f.result;r=i(r," ");var d=this._buildString(c,h,{buildParameterized:e.buildParameterized,nested:!0});r+="WHEN "+d.text+" THEN "+this._formatValueForQueryString(v),n.push.apply(n,t(d.values))}}catch(y){o=!0,l=y}finally{try{!a&&s["return"]&&s["return"]()}finally{if(o)throw l}}return r.length?(r+=" ELSE "+this._formatValueForQueryString(this._elseValue)+" END",this._fieldName&&(r=this._fieldName+" "+r),r="CASE "+r):r=this._formatValueForQueryString(this._elseValue),{text:r,values:n}}}]),u}(p.BaseBuilder),p.Block=function(t){function i(t){return n(this,i),e(this,Object.getPrototypeOf(i).call(this,t))}return r(i,t),d(i,[{key:"exposedMethods",value:function(){for(var t={},e=this;e;)Object.getOwnPropertyNames(e).forEach(function(r){"constructor"===r||"function"!=typeof e[r]||"_"===r.charAt(0)||p.Block.prototype[r]||(t[r]=e[r])}),e=Object.getPrototypeOf(e);return t}}]),i}(p.BaseBuilder),p.StringBlock=function(t){function i(t,r){n(this,i);var a=e(this,Object.getPrototypeOf(i).call(this,t));return a._str=r,a}return r(i,t),d(i,[{key:"_toParamString",value:function(){arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return{text:this._str,values:[]}}}]),i}(p.Block),p.FunctionBlock=function(t){function i(t){n(this,i);var r=e(this,Object.getPrototypeOf(i).call(this,t));return r._strings=[],r._values=[],r}return r(i,t),d(i,[{key:"function",value:function(t){this._strings.push(t);for(var e=arguments.length,r=Array(e>1?e-1:0),n=1;e>n;n++)r[n-1]=arguments[n];this._values.push(r)}},{key:"_toParamString",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return this._buildManyStrings(this._strings,this._values,t)}}]),i}(p.Block),p.registerValueHandler(p.FunctionBlock,function(t){var e=arguments.length<=1||void 0===arguments[1]?!1:arguments[1];return e?t.toParam():t.toString()}),p.AbstractTableBlock=function(a){function o(t,r){n(this,o);var i=e(this,Object.getPrototypeOf(o).call(this,t));return i._tables=[],i}return r(o,a),d(o,[{key:"_table",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1];e=e?this._sanitizeTableAlias(e):e,t=this._sanitizeTable(t),this.options.singleTable&&(this._tables=[]),this._tables.push({table:t,alias:e})}},{key:"_hasTable",value:function(){return 01)throw new Error("Cannot set multiple rows of fields this way.");"undefined"!=typeof e&&(e=this._sanitizeValue(e)),t=this._sanitizeField(t);var n=this._fields.indexOf(t);-1===n&&(this._fields.push(t),n=this._fields.length-1),this._values[0][n]=e,this._valueOptions[0][n]=r}},{key:"_setFields",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];if("object"!==("undefined"==typeof t?"undefined":y(t)))throw new Error("Expected an object but got "+("undefined"==typeof t?"undefined":y(t)));for(var r in t)this._set(r,t[r],e)}},{key:"_setFieldsRows",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];if(!l(t))throw new Error("Expected an array of objects but got "+("undefined"==typeof t?"undefined":y(t)));this._reset();for(var r in t){var n=t[r];for(var i in n){var a=n[i];i=this._sanitizeField(i),a=this._sanitizeValue(a);var o=this._fields.indexOf(i);if(r>0&&-1===o)throw new Error("All fields in subsequent rows must match the fields in the first row");-1===o&&(this._fields.push(i),o=this._fields.length-1),l(this._values[r])||(this._values[r]=[],this._valueOptions[r]=[]),this._values[r][o]=a,this._valueOptions[r][o]=e}}}}]),i}(p.Block),p.SetFieldBlock=function(a){function o(){return n(this,o),e(this,Object.getPrototypeOf(o).apply(this,arguments))}return r(o,a),d(o,[{key:"set",value:function(t,e,r){this._set(t,e,r)}},{key:"setFields",value:function(t,e){this._setFields(t,e)}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r=e.buildParameterized;if(0>=this._fields.length)throw new Error("set() needs to be called");var n="",a=[];for(var o in this._fields){n=i(n,", ");var l=this._fields[o],u=this._values[0][o];if("undefined"==typeof u)n+=l;else{var s=this._buildString(l+" = "+this.options.parameterCharacter,[u],{buildParameterized:r,formattingOptions:this._valueOptions[0][o]});n+=s.text,a.push.apply(a,t(s.values))}}return{text:"SET "+n,values:a}}}]),o}(p.AbstractSetFieldBlock),p.InsertFieldValueBlock=function(a){function o(){return n(this,o),e(this,Object.getPrototypeOf(o).apply(this,arguments))}return r(o,a),d(o,[{key:"set",value:function(t,e){var r=arguments.length<=2||void 0===arguments[2]?{}:arguments[2];this._set(t,e,r)}},{key:"setFields",value:function(t,e){this._setFields(t,e)}},{key:"setFieldsRows",value:function(t,e){this._setFieldsRows(t,e)}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r=e.buildParameterized,n=this._fields.join(", "),a=[],o=[];for(var l in this._values){a[l]="";for(var u in this._values[l]){var s=this._buildString(this.options.parameterCharacter,[this._values[l][u]],{buildParameterized:r,formattingOptions:this._valueOptions[l][u]});o.push.apply(o,t(s.values)),a[l]=i(a[l],", "),a[l]+=s.text}}return{text:n.length?"("+n+") VALUES ("+a.join("), (")+")":"",values:o}}}]),o}(p.AbstractSetFieldBlock),p.InsertFieldsFromQueryBlock=function(t){function i(t){n(this,i);var r=e(this,Object.getPrototypeOf(i).call(this,t));return r._fields=[],r._query=null,r}return r(i,t),d(i,[{key:"fromQuery",value:function(t,e){var r=this;this._fields=t.map(function(t){return r._sanitizeField(t)}),this._query=this._sanitizeQueryBuilder(e)}},{key:"_toParamString",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],e="",r=[];if(this._fields.length&&this._query){var n=this._query._toParamString({buildParameterized:t.buildParameterized,nested:!0}),i=n.text,a=n.values;e="("+this._fields.join(", ")+") ("+i+")",r=a}return{text:e,values:r}}}]),i}(p.Block),p.DistinctBlock=function(t){function i(){return n(this,i),e(this,Object.getPrototypeOf(i).apply(this,arguments))}return r(i,t),d(i,[{key:"distinct",value:function(){this._useDistinct=!0}},{key:"_toParamString",value:function(){return{text:this._useDistinct?"DISTINCT":"",values:[]}}}]),i}(p.Block),p.GroupByBlock=function(t){function i(t){n(this,i);var r=e(this,Object.getPrototypeOf(i).call(this,t));return r._groups=[],r}return r(i,t),d(i,[{key:"group",value:function(t){this._groups.push(this._sanitizeField(t))}},{key:"_toParamString",value:function(){arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return{text:this._groups.length?"GROUP BY "+this._groups.join(", "):"",values:[]}}}]),i}(p.Block),p.OffsetBlock=function(t){function i(t){n(this,i);var r=e(this,Object.getPrototypeOf(i).call(this,t));return r._offsets=null,r}return r(i,t),d(i,[{key:"offset",value:function(t){this._offsets=this._sanitizeLimitOffset(t)}},{key:"_toParamString",value:function(){return{text:this._offsets?"OFFSET "+this._offsets:"",values:[]}}}]),i}(p.Block),p.AbstractConditionBlock=function(a){function o(t){n(this,o);var r=e(this,Object.getPrototypeOf(o).call(this,t));return r._conditions=[],r}return r(o,a),d(o,[{key:"_condition",value:function(t){for(var e=arguments.length,r=Array(e>1?e-1:0),n=1;e>n;n++)r[n-1]=arguments[n];t=this._sanitizeExpression(t),this._conditions.push({expr:t,values:r})}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r="",n=[],a=!0,o=!1,l=void 0;try{for(var u,s=this._conditions[Symbol.iterator]();!(a=(u=s.next()).done);a=!0){var f=u.value,c=f.expr,h=f.values;r=i(r,") AND (");var v=c instanceof p.Expression?c._toParamString({buildParameterized:e.buildParameterized}):this._buildString(c,h,{buildParameterized:e.buildParameterized});r+=v.text,n.push.apply(n,t(v.values))}}catch(d){o=!0,l=d}finally{try{!a&&s["return"]&&s["return"]()}finally{if(o)throw l}}return{text:r.length?this.options.verb+" ("+r+")":"",values:n}}}]),o}(p.Block),p.WhereBlock=function(t){function i(t){return n(this,i),e(this,Object.getPrototypeOf(i).call(this,a({},t,{verb:"WHERE"})))}return r(i,t),d(i,[{key:"where",value:function(t,e){for(var r=arguments.length,n=Array(r>2?r-2:0),i=2;r>i;i++)n[i-2]=arguments[i];var a=["=","<",">","<=",">=","<>","!=","in","not in","like","not like"];if("string"==typeof t&&"string"==typeof e&&-1!=a.indexOf(e.toLowerCase())){var o=new p.Expression;this._condition(o._add("AND",t,e,n))}else this._condition.apply(this,arguments)}}]),i}(p.AbstractConditionBlock),p.HavingBlock=function(t){function i(t){return n(this,i),e(this,Object.getPrototypeOf(i).call(this,a({},t,{verb:"HAVING"})))}return r(i,t),d(i,[{key:"having",value:function(t,e){for(var r=arguments.length,n=Array(r>2?r-2:0),i=2;r>i;i++)n[i-2]=arguments[i];var a=["=","<",">","<=",">=","<>","!=","in","not in","like","not like"];if("string"==typeof t&&"string"==typeof e&&-1!=a.indexOf(e.toLowerCase())){var o=new p.Expression;this._condition(o._add("AND",t,e,n))}else this._condition.apply(this,arguments)}}]),i}(p.AbstractConditionBlock),p.OrderByBlock=function(a){function o(t){n(this,o);var r=e(this,Object.getPrototypeOf(o).call(this,t));return r._orders=[],r}return r(o,a),d(o,[{key:"order",value:function(t,e){for(var r=arguments.length,n=Array(r>2?r-2:0),i=2;r>i;i++)n[i-2]=arguments[i];t=this._sanitizeField(t),e=void 0===e?!0:e,e=null!==e?!!e:e,this._orders.push({field:t,dir:e,values:n})}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r="",n=[],a=!0,o=!1,l=void 0;try{for(var u,s=this._orders[Symbol.iterator]();!(a=(u=s.next()).done);a=!0){var f=u.value,c=f.field,h=f.dir,v=f.values;r=i(r,", ");var d=this._buildString(c,v,{buildParameterized:e.buildParameterized});r+=d.text,n.push.apply(n,t(d.values)),null!==h&&(r+=" "+(h?"ASC":"DESC"))}}catch(y){o=!0,l=y}finally{try{!a&&s["return"]&&s["return"]()}finally{if(o)throw l}}return{text:r.length?"ORDER BY "+r:"",values:n}}}]),o}(p.Block),p.LimitBlock=function(t){function i(t){n(this,i);var r=e(this,Object.getPrototypeOf(i).call(this,t));return r._limit=null,r}return r(i,t),d(i,[{key:"limit",value:function(t){this._limit=this._sanitizeLimitOffset(t)}},{key:"_toParamString",value:function(){return{text:null!==this._limit?"LIMIT "+this._limit:"",values:[]}}}]),i}(p.Block),p.JoinBlock=function(a){function o(t){n(this,o);var r=e(this,Object.getPrototypeOf(o).call(this,t));return r._joins=[],r}return r(o,a),d(o,[{key:"join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],r=arguments.length<=2||void 0===arguments[2]?null:arguments[2],n=arguments.length<=3||void 0===arguments[3]?"INNER":arguments[3];t=this._sanitizeTable(t,!0),e=e?this._sanitizeTableAlias(e):e,r=r?this._sanitizeExpression(r):r,this._joins.push({type:n,table:t,alias:e,condition:r})}},{key:"left_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],r=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,r,"LEFT")}},{key:"right_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],r=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,r,"RIGHT")}},{key:"outer_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],r=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,r,"OUTER")}},{key:"left_outer_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],r=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,r,"LEFT OUTER")}},{key:"full_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],r=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,r,"FULL")}},{key:"cross_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],r=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,r,"CROSS")}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r="",n=[],a=!0,o=!1,l=void 0;try{for(var u,s=this._joins[Symbol.iterator]();!(a=(u=s.next()).done);a=!0){var f=u.value,c=f.type,h=f.table,v=f.alias,d=f.condition;r=i(r,this.options.separator);var y=void 0;if(h instanceof p.BaseBuilder){var g=h._toParamString({buildParameterized:e.buildParameterized,nested:!0});n.push.apply(n,t(g.values)),y=g.text}else y=this._formatTableName(h);if(r+=c+" JOIN "+y,v&&(r+=" "+this._formatTableAlias(v)),d){r+=" ON ";var _=void 0;_=d instanceof p.Expression?d._toParamString({buildParameterized:e.buildParameterized}):this._buildString(d,[],{buildParameterized:e.buildParameterized}),r+=this._applyNestingFormatting(_.text),n.push.apply(n,t(_.values))}}}catch(m){o=!0,l=m}finally{try{!a&&s["return"]&&s["return"]()}finally{if(o)throw l}}return{text:r,values:n}}}]),o}(p.Block),p.UnionBlock=function(a){function o(t){n(this,o);var r=e(this,Object.getPrototypeOf(o).call(this,t));return r._unions=[],r}return r(o,a),d(o,[{key:"union",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?"UNION":arguments[1];t=this._sanitizeTable(t),this._unions.push({type:e,table:t})}},{key:"union_all",value:function(t){this.union(t,"UNION ALL")}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r="",n=[],a=!0,o=!1,l=void 0;try{for(var u,s=this._unions[Symbol.iterator]();!(a=(u=s.next()).done);a=!0){var f=u.value,c=f.type,h=f.table;r=i(r,this.options.separator);var v=void 0;if(h instanceof p.BaseBuilder){var d=h._toParamString({buildParameterized:e.buildParameterized,nested:!0});v=d.text,n.push.apply(n,t(d.values))}else r=this._formatTableName(h);r+=c+" "+v}}catch(y){o=!0,l=y}finally{try{!a&&s["return"]&&s["return"]()}finally{if(o)throw l}}return{text:r,values:n}}}]),o}(p.Block),p.QueryBuilder=function(i){function o(t,r){n(this,o);var i=e(this,Object.getPrototypeOf(o).call(this,t));i.blocks=r||[];var a=!0,l=!1,u=void 0;try{for(var s,f=i.blocks[Symbol.iterator]();!(a=(s=f.next()).done);a=!0){var c=s.value,h=c.exposedMethods();for(var v in h){var d=h[v];if(void 0!==i[v])throw new Error("Builder already has a builder method called: "+v);!function(t,e,r){i[e]=function(){for(var e=arguments.length,n=Array(e),a=0;e>a;a++)n[a]=arguments[a];return r.call.apply(r,[t].concat(n)),i}}(c,v,d)}}}catch(y){l=!0,u=y}finally{try{!a&&f["return"]&&f["return"]()}finally{if(l)throw u}}return i}return r(o,i),d(o,[{key:"registerValueHandler",value:function(t,e){var r=!0,n=!1,i=void 0;try{for(var a,l=this.blocks[Symbol.iterator]();!(r=(a=l.next()).done);r=!0){var u=a.value;u.registerValueHandler(t,e)}}catch(s){n=!0,i=s}finally{try{!r&&l["return"]&&l["return"]()}finally{if(n)throw i}}return v(Object.getPrototypeOf(o.prototype),"registerValueHandler",this).call(this,t,e),this}},{key:"updateOptions",value:function(t){this.options=a({},this.options,t);var e=!0,r=!1,n=void 0;try{for(var i,o=this.blocks[Symbol.iterator]();!(e=(i=o.next()).done);e=!0){var l=i.value;l.options=a({},l.options,t)}}catch(u){r=!0,n=u}finally{try{!e&&o["return"]&&o["return"]()}finally{if(r)throw n}}}},{key:"_toParamString",value:function(){var e,r=this,n=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];n=a({},this.options,n);var i=this.blocks.map(function(t){return t._toParamString({buildParameterized:n.buildParameterized,queryBuilder:r})}),o=i.map(function(t){return t.text}),l=i.map(function(t){return t.values}),u=o.filter(function(t){return 0 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) { - params[_key3 - 1] = arguments[_key3]; + value: function and(field, operator) { + for (var _len3 = arguments.length, params = Array(_len3 > 2 ? _len3 - 2 : 0), _key3 = 2; _key3 < _len3; _key3++) { + params[_key3 - 2] = arguments[_key3]; } - expr = this._sanitizeExpression(expr); - - this._nodes.push({ - type: 'AND', - expr: expr, - para: params - }); - - return this; + return this._add('AND', field, operator, params); } // Combine the current expression with the given expression using the union operator (OR). }, { key: 'or', - value: function or(expr) { - for (var _len4 = arguments.length, params = Array(_len4 > 1 ? _len4 - 1 : 0), _key4 = 1; _key4 < _len4; _key4++) { - params[_key4 - 1] = arguments[_key4]; + value: function or(field, operator) { + for (var _len4 = arguments.length, params = Array(_len4 > 2 ? _len4 - 2 : 0), _key4 = 2; _key4 < _len4; _key4++) { + params[_key4 - 2] = arguments[_key4]; } - expr = this._sanitizeExpression(expr); + return this._add('OR', field, operator, params); + } + }, { + key: '_add', + value: function _add(type, field, operator, params) { + var validOperators = ['=', '<', '>', '<=', '>=', '<>', '!=', 'in', 'not in', 'like', 'not like']; + var expr = void 0; + if (typeof field === 'string' && typeof operator === 'string' && -1 != validOperators.indexOf(operator.toLowerCase())) { + expr = this._buildExpression(field, operator); + } else { + expr = field; + params.unshift(operator); + } + + this._sanitizeExpression(field); this._nodes.push({ - type: 'OR', + type: type, expr: expr, para: params }); return this; } + }, { + key: '_buildExpression', + value: function _buildExpression(field, operator) { + var escapedKey = this._formatFieldName(field); + var paramChar = cls.DefaultQueryBuilderOptions.parameterCharacter; + var condition = escapedKey + ' ' + operator + ' ' + paramChar; + return condition; + } }, { key: '_toParamString', value: function _toParamString() { @@ -1989,7 +2003,7 @@ function _buildSquel() { cls.AbstractConditionBlock = function (_cls$Block10) { _inherits(_class20, _cls$Block10); - /** + /** * @param {String} options.verb The condition verb. */ @@ -2092,12 +2106,20 @@ function _buildSquel() { _createClass(_class21, [{ key: 'where', - value: function where(condition) { - for (var _len8 = arguments.length, values = Array(_len8 > 1 ? _len8 - 1 : 0), _key8 = 1; _key8 < _len8; _key8++) { - values[_key8 - 1] = arguments[_key8]; + value: function where(field, operator) { + for (var _len8 = arguments.length, values = Array(_len8 > 2 ? _len8 - 2 : 0), _key8 = 2; _key8 < _len8; _key8++) { + values[_key8 - 2] = arguments[_key8]; } - this._condition.apply(this, [condition].concat(values)); + var validOperators = ['=', '<', '>', '<=', '>=', '<>', '!=', 'in', 'not in', 'like', 'not like']; + if (typeof field === 'string' && typeof operator === 'string' && -1 != validOperators.indexOf(operator.toLowerCase())) { + // key - operator - value + var expr = new cls.Expression(); + this._condition(expr._add('AND', field, operator, values)); + } else { + // default squel-behaviour without auto-quoting + this._condition.apply(this, arguments); + } } }]); @@ -2118,12 +2140,20 @@ function _buildSquel() { _createClass(_class22, [{ key: 'having', - value: function having(condition) { - for (var _len9 = arguments.length, values = Array(_len9 > 1 ? _len9 - 1 : 0), _key9 = 1; _key9 < _len9; _key9++) { - values[_key9 - 1] = arguments[_key9]; + value: function having(field, operator) { + for (var _len9 = arguments.length, values = Array(_len9 > 2 ? _len9 - 2 : 0), _key9 = 2; _key9 < _len9; _key9++) { + values[_key9 - 2] = arguments[_key9]; } - this._condition.apply(this, [condition].concat(values)); + var validOperators = ['=', '<', '>', '<=', '>=', '<>', '!=', 'in', 'not in', 'like', 'not like']; + if (typeof field === 'string' && typeof operator === 'string' && -1 != validOperators.indexOf(operator.toLowerCase())) { + // key - operator - value + var expr = new cls.Expression(); + this._condition(expr._add('AND', field, operator, values)); + } else { + // default squel-behaviour without auto-quoting + this._condition.apply(this, arguments); + } } }]); diff --git a/squel.min.js b/squel.min.js index 095e9551..5f581d49 100644 --- a/squel.min.js +++ b/squel.min.js @@ -1,2 +1,2 @@ -/*! squel | https://github.com/hiddentao/squel | BSD license */!function(t,e){"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?module.exports=e():t.squel=e()}(this,function(){"use strict";function t(t){if(Array.isArray(t)){for(var e=0,n=Array(t.length);e1?e-1:0),r=1;e>r;r++)n[r-1]=arguments[r];if(t&&n){var i=!0,o=!1,a=void 0;try{for(var l,u=function(){var e=l.value;"object"===("undefined"==typeof e?"undefined":p(e))&&Object.getOwnPropertyNames(e).forEach(function(n){"function"!=typeof e[n]&&(t[n]=e[n])})},s=n[Symbol.iterator]();!(i=(l=s.next()).done);i=!0)u()}catch(c){o=!0,a=c}finally{try{!i&&s["return"]&&s["return"]()}finally{if(o)throw a}}}return t}function a(t){return t&&t.constructor.prototype===Object.prototype}function l(t){return t&&t.constructor.prototype===Array.prototype}function u(t){if(t&&t.constructor&&t.constructor.toString){var e=t.constructor.toString().match(/function\s*(\w+)/);if(e&&2===e.length)return e[1]}}function s(t){if(!t)return t;if("function"==typeof t.clone)return t.clone();if(!a(t)&&!l(t))return JSON.parse(JSON.stringify(t));var e=function(){var e=new t.constructor;return Object.getOwnPropertyNames(t).forEach(function(n){"function"!=typeof t[n]&&(e[n]=s(t[n]))}),{v:e}}();return"object"===("undefined"==typeof e?"undefined":p(e))?e.v:void 0}function c(t,e,n){var r="undefined"==typeof e?"undefined":p(e);if("function"!==r&&"string"!==r)throw new Error("type must be a class constructor or string");if("function"!=typeof n)throw new Error("handler must be a function");for(var i in t){var o=t[i];if(o.type===e)return void(o.handler=n)}t.push({type:e,handler:n})}function f(t){for(var e=arguments.length,n=Array(e>1?e-1:0),r=1;e>r;r++)n[r-1]=arguments[r];for(var i in n){var o=n[i];for(var a in o){var l=o[a];if(("undefined"==typeof t?"undefined":p(t))===l.type||"string"!=typeof l.type&&t instanceof l.type)return l.handler}}}function h(){var h=arguments.length<=0||void 0===arguments[0]?null:arguments[0],y={_getObjectClassName:u};y.DefaultQueryBuilderOptions={autoQuoteTableNames:!1,autoQuoteFieldNames:!1,autoQuoteAliasNames:!0,useAsForTableAliasNames:!1,nameQuoteCharacter:"`",tableAliasQuoteCharacter:"`",fieldAliasQuoteCharacter:'"',valueHandlers:[],parameterCharacter:"?",numberedParameters:!1,numberedParametersPrefix:"$",numberedParametersStartAt:1,replaceSingleQuotes:!1,singleQuoteReplacement:"''",separator:" "},y.globalValueHandlers=[],y.registerValueHandler=function(t,e){c(y.globalValueHandlers,t,e)},y.Cloneable=function(){function t(){r(this,t)}return d(t,[{key:"clone",value:function(){var t=new this.constructor;return o(t,s(o({},this)))}}]),t}(),y.BaseBuilder=function(i){function a(t){r(this,a);var n=e(this,Object.getPrototypeOf(a).call(this)),i=JSON.parse(JSON.stringify(y.DefaultQueryBuilderOptions));return n.options=o({},i,t),n}return n(a,i),d(a,[{key:"registerValueHandler",value:function(t,e){return c(this.options.valueHandlers,t,e),this}},{key:"_sanitizeExpression",value:function(t){if(!(t instanceof y.Expression)&&"string"!=typeof t)throw new Error("expression must be a string or Expression instance");return t}},{key:"_sanitizeName",value:function(t,e){if("string"!=typeof t)throw new Error(e+" must be a string");return t}},{key:"_sanitizeField",value:function(t){return t instanceof y.BaseBuilder||(t=this._sanitizeName(t,"field name")),t}},{key:"_sanitizeQueryBuilder",value:function(t){if(t instanceof y.QueryBuilder)return t;throw new Error("must be a QueryBuilder instance")}},{key:"_sanitizeTable",value:function(t){if("string"!=typeof t)try{t=this._sanitizeQueryBuilder(t)}catch(e){throw new Error("table name must be a string or a query builder")}else t=this._sanitizeName(t,"table");return t}},{key:"_sanitizeTableAlias",value:function(t){return this._sanitizeName(t,"table alias")}},{key:"_sanitizeFieldAlias",value:function(t){return this._sanitizeName(t,"field alias")}},{key:"_sanitizeLimitOffset",value:function(t){if(t=parseInt(t),0>t||isNaN(t))throw new Error("limit/offset must be >= 0");return t}},{key:"_sanitizeValue",value:function(t){var e="undefined"==typeof t?"undefined":p(t);if(null===t);else if("string"===e||"number"===e||"boolean"===e);else if(t instanceof y.BaseBuilder);else{var n=!!f(t,this.options.valueHandlers,y.globalValueHandlers);if(!n)throw new Error("field value must be a string, number, boolean, null or one of the registered custom value types")}return t}},{key:"_escapeValue",value:function(t){return this.options.replaceSingleQuotes?t.replace(/\'/g,this.options.singleQuoteReplacement):t}},{key:"_formatTableName",value:function(t){if(this.options.autoQuoteTableNames){var e=this.options.nameQuoteCharacter;t=""+e+t+e}return t}},{key:"_formatFieldAlias",value:function(t){if(this.options.autoQuoteAliasNames){var e=this.options.fieldAliasQuoteCharacter;t=""+e+t+e}return t}},{key:"_formatTableAlias",value:function(t){if(this.options.autoQuoteAliasNames){var e=this.options.tableAliasQuoteCharacter;t=""+e+t+e}return this.options.useAsForTableAliasNames?"AS "+t:t}},{key:"_formatFieldName",value:function(t){var e=this,n=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];return this.options.autoQuoteFieldNames&&!function(){var r=e.options.nameQuoteCharacter;t=n.ignorePeriodsForFieldNameQuotes?""+r+t+r:t.split(".").map(function(t){return"*"===t?t:""+r+t+r}).join(".")}(),t}},{key:"_formatCustomValue",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?!1:arguments[1],n=f(t,this.options.valueHandlers,y.globalValueHandlers);return n&&(t=n(t,e)),t}},{key:"_formatValueForParamArray",value:function(t){var e=this;return l(t)?t.map(function(t){return e._formatValueForParamArray(t)}):this._formatCustomValue(t,!0)}},{key:"_formatValueForQueryString",value:function(t){var e=this,n=arguments.length<=1||void 0===arguments[1]?{}:arguments[1],r=this._formatCustomValue(t);if(r!==t)return this._applyNestingFormatting(r);if(l(t))t=t.map(function(t){return e._formatValueForQueryString(t)}),t=this._applyNestingFormatting(t.join(", "));else{var i="undefined"==typeof t?"undefined":p(t);if(null===t)t="NULL";else if("boolean"===i)t=t?"TRUE":"FALSE";else if(t instanceof y.BaseBuilder)t=this._applyNestingFormatting(t.toString());else if("number"!==i)if(n.dontQuote)t=""+t;else{var o=this._escapeValue(t);t="'"+o+"'"}}return t}},{key:"_applyNestingFormatting",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?!0:arguments[1];return t&&"string"==typeof t&&e&&("("!==t.charAt(0)||")"!==t.charAt(t.length-1))?"("+t+")":t}},{key:"_buildString",value:function(e,n){var r=arguments.length<=2||void 0===arguments[2]?{}:arguments[2],i=r.nested,o=r.buildParameterized,a=r.formattingOptions;n=n||[],e=e||"";for(var u="",s=-1,c=[],f=this.options.parameterCharacter,h=0;e.length>h;)if(e.substr(h,f.length)===f){var v=n[++s];if(o)if(v instanceof y.BaseBuilder){var d=v._toParamString({buildParameterized:o,nested:!0});u+=d.text,c.push.apply(c,t(d.values))}else if(v=this._formatValueForParamArray(v),l(v)){var p=v.map(function(){return f}).join(", ");u+="("+p+")",c.push.apply(c,t(v))}else u+=f,c.push(v);else u+=this._formatValueForQueryString(v,a);h+=f.length}else u+=e.charAt(h),h++;return{text:this._applyNestingFormatting(u,!!i),values:c}}},{key:"_buildManyStrings",value:function(e,n){var r=arguments.length<=2||void 0===arguments[2]?{}:arguments[2],i=[],o=[];for(var a in e){var l=e[a],u=n[a],s=this._buildString(l,u,{buildParameterized:r.buildParameterized,nested:!1}),c=s.text,f=s.values;i.push(c),o.push.apply(o,t(f))}return i=i.join(this.options.separator),{text:i.length?this._applyNestingFormatting(i,!!r.nested):"",values:o}}},{key:"_toParamString",value:function(t){throw new Error("Not yet implemented")}},{key:"toString",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return this._toParamString(t).text}},{key:"toParam",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return this._toParamString(o({},t,{buildParameterized:!0}))}}]),a}(y.Cloneable),y.Expression=function(i){function o(t){r(this,o);var n=e(this,Object.getPrototypeOf(o).call(this,t));return n._nodes=[],n}return n(o,i),d(o,[{key:"and",value:function(t){for(var e=arguments.length,n=Array(e>1?e-1:0),r=1;e>r;r++)n[r-1]=arguments[r];return t=this._sanitizeExpression(t),this._nodes.push({type:"AND",expr:t,para:n}),this}},{key:"or",value:function(t){for(var e=arguments.length,n=Array(e>1?e-1:0),r=1;e>r;r++)n[r-1]=arguments[r];return t=this._sanitizeExpression(t),this._nodes.push({type:"OR",expr:t,para:n}),this}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n=[],r=[],i=!0,o=!1,a=void 0;try{for(var l,u=this._nodes[Symbol.iterator]();!(i=(l=u.next()).done);i=!0){var s=l.value,c=s.type,f=s.expr,h=s.para,v=f instanceof y.Expression?f._toParamString({buildParameterized:e.buildParameterized,nested:!0}):this._buildString(f,h,{buildParameterized:e.buildParameterized}),d=v.text,p=v.values;n.length&&n.push(c),n.push(d),r.push.apply(r,t(p))}}catch(g){o=!0,a=g}finally{try{!i&&u["return"]&&u["return"]()}finally{if(o)throw a}}return n=n.join(" "),{text:this._applyNestingFormatting(n,!!e.nested),values:r}}}]),o}(y.BaseBuilder),y.Case=function(l){function u(t){var n=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];r(this,u);var i=e(this,Object.getPrototypeOf(u).call(this,n));return a(t)&&(n=t,t=null),t&&(i._fieldName=i._sanitizeField(t)),i.options=o({},y.DefaultQueryBuilderOptions,n),i._cases=[],i._elseValue=null,i}return n(u,l),d(u,[{key:"when",value:function(t){for(var e=arguments.length,n=Array(e>1?e-1:0),r=1;e>r;r++)n[r-1]=arguments[r];return this._cases.unshift({expression:t,values:n}),this}},{key:"then",value:function(t){if(0==this._cases.length)throw new Error("when() needs to be called first");return this._cases[0].result=t,this}},{key:"else",value:function(t){return this._elseValue=t,this}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n="",r=[],o=!0,a=!1,l=void 0;try{for(var u,s=this._cases[Symbol.iterator]();!(o=(u=s.next()).done);o=!0){var c=u.value,f=c.expression,h=c.values,v=c.result;n=i(n," ");var d=this._buildString(f,h,{buildParameterized:e.buildParameterized,nested:!0});n+="WHEN "+d.text+" THEN "+this._formatValueForQueryString(v),r.push.apply(r,t(d.values))}}catch(p){a=!0,l=p}finally{try{!o&&s["return"]&&s["return"]()}finally{if(a)throw l}}return n.length?(n+=" ELSE "+this._formatValueForQueryString(this._elseValue)+" END",this._fieldName&&(n=this._fieldName+" "+n),n="CASE "+n):n=this._formatValueForQueryString(this._elseValue),{text:n,values:r}}}]),u}(y.BaseBuilder),y.Block=function(t){function i(t){return r(this,i),e(this,Object.getPrototypeOf(i).call(this,t))}return n(i,t),d(i,[{key:"exposedMethods",value:function(){for(var t={},e=this;e;)Object.getOwnPropertyNames(e).forEach(function(n){"constructor"===n||"function"!=typeof e[n]||"_"===n.charAt(0)||y.Block.prototype[n]||(t[n]=e[n])}),e=Object.getPrototypeOf(e);return t}}]),i}(y.BaseBuilder),y.StringBlock=function(t){function i(t,n){r(this,i);var o=e(this,Object.getPrototypeOf(i).call(this,t));return o._str=n,o}return n(i,t),d(i,[{key:"_toParamString",value:function(){arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return{text:this._str,values:[]}}}]),i}(y.Block),y.FunctionBlock=function(t){function i(t){r(this,i);var n=e(this,Object.getPrototypeOf(i).call(this,t));return n._strings=[],n._values=[],n}return n(i,t),d(i,[{key:"function",value:function(t){this._strings.push(t);for(var e=arguments.length,n=Array(e>1?e-1:0),r=1;e>r;r++)n[r-1]=arguments[r];this._values.push(n)}},{key:"_toParamString",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return this._buildManyStrings(this._strings,this._values,t)}}]),i}(y.Block),y.registerValueHandler(y.FunctionBlock,function(t){var e=arguments.length<=1||void 0===arguments[1]?!1:arguments[1];return e?t.toParam():t.toString()}),y.AbstractTableBlock=function(o){function a(t,n){r(this,a);var i=e(this,Object.getPrototypeOf(a).call(this,t));return i._tables=[],i}return n(a,o),d(a,[{key:"_table",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1];e=e?this._sanitizeTableAlias(e):e,t=this._sanitizeTable(t),this.options.singleTable&&(this._tables=[]),this._tables.push({table:t,alias:e})}},{key:"_hasTable",value:function(){return 01)throw new Error("Cannot set multiple rows of fields this way.");"undefined"!=typeof e&&(e=this._sanitizeValue(e)),t=this._sanitizeField(t);var r=this._fields.indexOf(t);-1===r&&(this._fields.push(t),r=this._fields.length-1),this._values[0][r]=e,this._valueOptions[0][r]=n}},{key:"_setFields",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];if("object"!==("undefined"==typeof t?"undefined":p(t)))throw new Error("Expected an object but got "+("undefined"==typeof t?"undefined":p(t)));for(var n in t)this._set(n,t[n],e)}},{key:"_setFieldsRows",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];if(!l(t))throw new Error("Expected an array of objects but got "+("undefined"==typeof t?"undefined":p(t)));this._reset();for(var n in t){var r=t[n];for(var i in r){var o=r[i];i=this._sanitizeField(i),o=this._sanitizeValue(o);var a=this._fields.indexOf(i);if(n>0&&-1===a)throw new Error("All fields in subsequent rows must match the fields in the first row");-1===a&&(this._fields.push(i),a=this._fields.length-1),l(this._values[n])||(this._values[n]=[],this._valueOptions[n]=[]),this._values[n][a]=o,this._valueOptions[n][a]=e}}}}]),i}(y.Block),y.SetFieldBlock=function(o){function a(){return r(this,a),e(this,Object.getPrototypeOf(a).apply(this,arguments))}return n(a,o),d(a,[{key:"set",value:function(t,e,n){this._set(t,e,n)}},{key:"setFields",value:function(t,e){this._setFields(t,e)}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n=e.buildParameterized;if(0>=this._fields.length)throw new Error("set() needs to be called");var r="",o=[];for(var a in this._fields){r=i(r,", ");var l=this._fields[a],u=this._values[0][a];if("undefined"==typeof u)r+=l;else{var s=this._buildString(l+" = "+this.options.parameterCharacter,[u],{buildParameterized:n,formattingOptions:this._valueOptions[0][a]});r+=s.text,o.push.apply(o,t(s.values))}}return{text:"SET "+r,values:o}}}]),a}(y.AbstractSetFieldBlock),y.InsertFieldValueBlock=function(o){function a(){return r(this,a),e(this,Object.getPrototypeOf(a).apply(this,arguments))}return n(a,o),d(a,[{key:"set",value:function(t,e){var n=arguments.length<=2||void 0===arguments[2]?{}:arguments[2];this._set(t,e,n)}},{key:"setFields",value:function(t,e){this._setFields(t,e)}},{key:"setFieldsRows",value:function(t,e){this._setFieldsRows(t,e)}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n=e.buildParameterized,r=this._fields.join(", "),o=[],a=[];for(var l in this._values){o[l]="";for(var u in this._values[l]){var s=this._buildString(this.options.parameterCharacter,[this._values[l][u]],{buildParameterized:n,formattingOptions:this._valueOptions[l][u]});a.push.apply(a,t(s.values)),o[l]=i(o[l],", "),o[l]+=s.text}}return{text:r.length?"("+r+") VALUES ("+o.join("), (")+")":"",values:a}}}]),a}(y.AbstractSetFieldBlock),y.InsertFieldsFromQueryBlock=function(t){function i(t){r(this,i);var n=e(this,Object.getPrototypeOf(i).call(this,t));return n._fields=[],n._query=null,n}return n(i,t),d(i,[{key:"fromQuery",value:function(t,e){var n=this;this._fields=t.map(function(t){return n._sanitizeField(t)}),this._query=this._sanitizeQueryBuilder(e)}},{key:"_toParamString",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],e="",n=[];if(this._fields.length&&this._query){var r=this._query._toParamString({buildParameterized:t.buildParameterized,nested:!0}),i=r.text,o=r.values;e="("+this._fields.join(", ")+") ("+i+")",n=o}return{text:e,values:n}}}]),i}(y.Block),y.DistinctBlock=function(t){function i(){return r(this,i),e(this,Object.getPrototypeOf(i).apply(this,arguments))}return n(i,t),d(i,[{key:"distinct",value:function(){this._useDistinct=!0}},{key:"_toParamString",value:function(){return{text:this._useDistinct?"DISTINCT":"",values:[]}}}]),i}(y.Block),y.GroupByBlock=function(t){function i(t){r(this,i);var n=e(this,Object.getPrototypeOf(i).call(this,t));return n._groups=[],n}return n(i,t),d(i,[{key:"group",value:function(t){this._groups.push(this._sanitizeField(t))}},{key:"_toParamString",value:function(){arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return{text:this._groups.length?"GROUP BY "+this._groups.join(", "):"",values:[]}}}]),i}(y.Block),y.OffsetBlock=function(t){function i(t){r(this,i);var n=e(this,Object.getPrototypeOf(i).call(this,t));return n._offsets=null,n}return n(i,t),d(i,[{key:"offset",value:function(t){this._offsets=this._sanitizeLimitOffset(t)}},{key:"_toParamString",value:function(){return{text:this._offsets?"OFFSET "+this._offsets:"",values:[]}}}]),i}(y.Block),y.AbstractConditionBlock=function(o){function a(t){r(this,a);var n=e(this,Object.getPrototypeOf(a).call(this,t));return n._conditions=[],n}return n(a,o),d(a,[{key:"_condition",value:function(t){for(var e=arguments.length,n=Array(e>1?e-1:0),r=1;e>r;r++)n[r-1]=arguments[r];t=this._sanitizeExpression(t),this._conditions.push({expr:t,values:n})}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n="",r=[],o=!0,a=!1,l=void 0;try{for(var u,s=this._conditions[Symbol.iterator]();!(o=(u=s.next()).done);o=!0){var c=u.value,f=c.expr,h=c.values;n=i(n,") AND (");var v=f instanceof y.Expression?f._toParamString({buildParameterized:e.buildParameterized}):this._buildString(f,h,{buildParameterized:e.buildParameterized});n+=v.text,r.push.apply(r,t(v.values))}}catch(d){a=!0,l=d}finally{try{!o&&s["return"]&&s["return"]()}finally{if(a)throw l}}return{text:n.length?this.options.verb+" ("+n+")":"",values:r}}}]),a}(y.Block),y.WhereBlock=function(t){function i(t){return r(this,i),e(this,Object.getPrototypeOf(i).call(this,o({},t,{verb:"WHERE"})))}return n(i,t),d(i,[{key:"where",value:function(t){for(var e=arguments.length,n=Array(e>1?e-1:0),r=1;e>r;r++)n[r-1]=arguments[r];this._condition.apply(this,[t].concat(n))}}]),i}(y.AbstractConditionBlock),y.HavingBlock=function(t){function i(t){return r(this,i),e(this,Object.getPrototypeOf(i).call(this,o({},t,{verb:"HAVING"})))}return n(i,t),d(i,[{key:"having",value:function(t){for(var e=arguments.length,n=Array(e>1?e-1:0),r=1;e>r;r++)n[r-1]=arguments[r];this._condition.apply(this,[t].concat(n))}}]),i}(y.AbstractConditionBlock),y.OrderByBlock=function(o){function a(t){r(this,a);var n=e(this,Object.getPrototypeOf(a).call(this,t));return n._orders=[],n}return n(a,o),d(a,[{key:"order",value:function(t,e){for(var n=arguments.length,r=Array(n>2?n-2:0),i=2;n>i;i++)r[i-2]=arguments[i];t=this._sanitizeField(t),e=void 0===e?!0:e,e=null!==e?!!e:e,this._orders.push({field:t,dir:e,values:r})}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n="",r=[],o=!0,a=!1,l=void 0;try{for(var u,s=this._orders[Symbol.iterator]();!(o=(u=s.next()).done);o=!0){var c=u.value,f=c.field,h=c.dir,v=c.values;n=i(n,", ");var d=this._buildString(f,v,{buildParameterized:e.buildParameterized});n+=d.text,r.push.apply(r,t(d.values)),null!==h&&(n+=" "+(h?"ASC":"DESC"))}}catch(p){a=!0,l=p}finally{try{!o&&s["return"]&&s["return"]()}finally{if(a)throw l}}return{text:n.length?"ORDER BY "+n:"",values:r}}}]),a}(y.Block),y.LimitBlock=function(t){function i(t){r(this,i);var n=e(this,Object.getPrototypeOf(i).call(this,t));return n._limit=null,n}return n(i,t),d(i,[{key:"limit",value:function(t){this._limit=this._sanitizeLimitOffset(t)}},{key:"_toParamString",value:function(){return{text:null!==this._limit?"LIMIT "+this._limit:"",values:[]}}}]),i}(y.Block),y.JoinBlock=function(o){function a(t){r(this,a);var n=e(this,Object.getPrototypeOf(a).call(this,t));return n._joins=[],n}return n(a,o),d(a,[{key:"join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],n=arguments.length<=2||void 0===arguments[2]?null:arguments[2],r=arguments.length<=3||void 0===arguments[3]?"INNER":arguments[3];t=this._sanitizeTable(t,!0),e=e?this._sanitizeTableAlias(e):e,n=n?this._sanitizeExpression(n):n,this._joins.push({type:r,table:t,alias:e,condition:n})}},{key:"left_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],n=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,n,"LEFT")}},{key:"right_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],n=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,n,"RIGHT")}},{key:"outer_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],n=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,n,"OUTER")}},{key:"left_outer_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],n=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,n,"LEFT OUTER")}},{key:"full_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],n=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,n,"FULL")}},{key:"cross_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],n=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,n,"CROSS")}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n="",r=[],o=!0,a=!1,l=void 0;try{for(var u,s=this._joins[Symbol.iterator]();!(o=(u=s.next()).done);o=!0){var c=u.value,f=c.type,h=c.table,v=c.alias,d=c.condition;n=i(n,this.options.separator);var p=void 0;if(h instanceof y.BaseBuilder){var g=h._toParamString({buildParameterized:e.buildParameterized,nested:!0});r.push.apply(r,t(g.values)),p=g.text}else p=this._formatTableName(h);if(n+=f+" JOIN "+p,v&&(n+=" "+this._formatTableAlias(v)),d){n+=" ON ";var _=void 0;_=d instanceof y.Expression?d._toParamString({buildParameterized:e.buildParameterized}):this._buildString(d,[],{buildParameterized:e.buildParameterized}),n+=this._applyNestingFormatting(_.text),r.push.apply(r,t(_.values))}}}catch(m){a=!0,l=m}finally{try{!o&&s["return"]&&s["return"]()}finally{if(a)throw l}}return{text:n,values:r}}}]),a}(y.Block),y.UnionBlock=function(o){function a(t){r(this,a);var n=e(this,Object.getPrototypeOf(a).call(this,t));return n._unions=[],n}return n(a,o),d(a,[{key:"union",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?"UNION":arguments[1];t=this._sanitizeTable(t),this._unions.push({type:e,table:t})}},{key:"union_all",value:function(t){this.union(t,"UNION ALL")}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n="",r=[],o=!0,a=!1,l=void 0;try{for(var u,s=this._unions[Symbol.iterator]();!(o=(u=s.next()).done);o=!0){var c=u.value,f=c.type,h=c.table;n=i(n,this.options.separator);var v=void 0;if(h instanceof y.BaseBuilder){var d=h._toParamString({buildParameterized:e.buildParameterized,nested:!0});v=d.text,r.push.apply(r,t(d.values))}else n=this._formatTableName(h);n+=f+" "+v}}catch(p){a=!0,l=p}finally{try{!o&&s["return"]&&s["return"]()}finally{if(a)throw l}}return{text:n,values:r}}}]),a}(y.Block),y.QueryBuilder=function(i){function a(t,n){r(this,a);var i=e(this,Object.getPrototypeOf(a).call(this,t));i.blocks=n||[];var o=!0,l=!1,u=void 0;try{for(var s,c=i.blocks[Symbol.iterator]();!(o=(s=c.next()).done);o=!0){var f=s.value,h=f.exposedMethods();for(var v in h){var d=h[v];if(void 0!==i[v])throw new Error("Builder already has a builder method called: "+v);!function(t,e,n){i[e]=function(){for(var e=arguments.length,r=Array(e),o=0;e>o;o++)r[o]=arguments[o];return n.call.apply(n,[t].concat(r)),i}}(f,v,d)}}}catch(p){l=!0,u=p}finally{try{!o&&c["return"]&&c["return"]()}finally{if(l)throw u}}return i}return n(a,i),d(a,[{key:"registerValueHandler",value:function(t,e){var n=!0,r=!1,i=void 0;try{for(var o,l=this.blocks[Symbol.iterator]();!(n=(o=l.next()).done);n=!0){var u=o.value;u.registerValueHandler(t,e)}}catch(s){r=!0,i=s}finally{try{!n&&l["return"]&&l["return"]()}finally{if(r)throw i}}return v(Object.getPrototypeOf(a.prototype),"registerValueHandler",this).call(this,t,e),this}},{key:"updateOptions",value:function(t){this.options=o({},this.options,t);var e=!0,n=!1,r=void 0;try{for(var i,a=this.blocks[Symbol.iterator]();!(e=(i=a.next()).done);e=!0){var l=i.value;l.options=o({},l.options,t)}}catch(u){n=!0,r=u}finally{try{!e&&a["return"]&&a["return"]()}finally{if(n)throw r}}}},{key:"_toParamString",value:function(){var e,n=this,r=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];r=o({},this.options,r);var i=this.blocks.map(function(t){return t._toParamString({buildParameterized:r.buildParameterized,queryBuilder:n})}),a=i.map(function(t){return t.text}),l=i.map(function(t){return t.values}),u=a.filter(function(t){return 0=this._fields.length)throw new Error("set() needs to be called");var e=v(Object.getPrototypeOf(i.prototype),"_toParamString",this).call(this,t);if(e.text.length&&01?e-1:0),r=1;e>r;r++)n[r-1]=arguments[r];if(t&&n){var i=!0,o=!1,a=void 0;try{for(var l,u=function(){var e=l.value;"object"===("undefined"==typeof e?"undefined":p(e))&&Object.getOwnPropertyNames(e).forEach(function(n){"function"!=typeof e[n]&&(t[n]=e[n])})},s=n[Symbol.iterator]();!(i=(l=s.next()).done);i=!0)u()}catch(c){o=!0,a=c}finally{try{!i&&s["return"]&&s["return"]()}finally{if(o)throw a}}}return t}function a(t){return t&&t.constructor.prototype===Object.prototype}function l(t){return t&&t.constructor.prototype===Array.prototype}function u(t){if(t&&t.constructor&&t.constructor.toString){var e=t.constructor.toString().match(/function\s*(\w+)/);if(e&&2===e.length)return e[1]}}function s(t){if(!t)return t;if("function"==typeof t.clone)return t.clone();if(!a(t)&&!l(t))return JSON.parse(JSON.stringify(t));var e=function(){var e=new t.constructor;return Object.getOwnPropertyNames(t).forEach(function(n){"function"!=typeof t[n]&&(e[n]=s(t[n]))}),{v:e}}();return"object"===("undefined"==typeof e?"undefined":p(e))?e.v:void 0}function c(t,e,n){var r="undefined"==typeof e?"undefined":p(e);if("function"!==r&&"string"!==r)throw new Error("type must be a class constructor or string");if("function"!=typeof n)throw new Error("handler must be a function");for(var i in t){var o=t[i];if(o.type===e)return void(o.handler=n)}t.push({type:e,handler:n})}function f(t){for(var e=arguments.length,n=Array(e>1?e-1:0),r=1;e>r;r++)n[r-1]=arguments[r];for(var i in n){var o=n[i];for(var a in o){var l=o[a];if(("undefined"==typeof t?"undefined":p(t))===l.type||"string"!=typeof l.type&&t instanceof l.type)return l.handler}}}function h(){var h=arguments.length<=0||void 0===arguments[0]?null:arguments[0],y={_getObjectClassName:u};y.DefaultQueryBuilderOptions={autoQuoteTableNames:!1,autoQuoteFieldNames:!1,autoQuoteAliasNames:!0,useAsForTableAliasNames:!1,nameQuoteCharacter:"`",tableAliasQuoteCharacter:"`",fieldAliasQuoteCharacter:'"',valueHandlers:[],parameterCharacter:"?",numberedParameters:!1,numberedParametersPrefix:"$",numberedParametersStartAt:1,replaceSingleQuotes:!1,singleQuoteReplacement:"''",separator:" "},y.globalValueHandlers=[],y.registerValueHandler=function(t,e){c(y.globalValueHandlers,t,e)},y.Cloneable=function(){function t(){r(this,t)}return d(t,[{key:"clone",value:function(){var t=new this.constructor;return o(t,s(o({},this)))}}]),t}(),y.BaseBuilder=function(i){function a(t){r(this,a);var n=e(this,Object.getPrototypeOf(a).call(this)),i=JSON.parse(JSON.stringify(y.DefaultQueryBuilderOptions));return n.options=o({},i,t),n}return n(a,i),d(a,[{key:"registerValueHandler",value:function(t,e){return c(this.options.valueHandlers,t,e),this}},{key:"_sanitizeExpression",value:function(t){if(!(t instanceof y.Expression)&&"string"!=typeof t)throw new Error("expression must be a string or Expression instance");return t}},{key:"_sanitizeName",value:function(t,e){if("string"!=typeof t)throw new Error(e+" must be a string");return t}},{key:"_sanitizeField",value:function(t){return t instanceof y.BaseBuilder||(t=this._sanitizeName(t,"field name")),t}},{key:"_sanitizeQueryBuilder",value:function(t){if(t instanceof y.QueryBuilder)return t;throw new Error("must be a QueryBuilder instance")}},{key:"_sanitizeTable",value:function(t){if("string"!=typeof t)try{t=this._sanitizeQueryBuilder(t)}catch(e){throw new Error("table name must be a string or a query builder")}else t=this._sanitizeName(t,"table");return t}},{key:"_sanitizeTableAlias",value:function(t){return this._sanitizeName(t,"table alias")}},{key:"_sanitizeFieldAlias",value:function(t){return this._sanitizeName(t,"field alias")}},{key:"_sanitizeLimitOffset",value:function(t){if(t=parseInt(t),0>t||isNaN(t))throw new Error("limit/offset must be >= 0");return t}},{key:"_sanitizeValue",value:function(t){var e="undefined"==typeof t?"undefined":p(t);if(null===t);else if("string"===e||"number"===e||"boolean"===e);else if(t instanceof y.BaseBuilder);else{var n=!!f(t,this.options.valueHandlers,y.globalValueHandlers);if(!n)throw new Error("field value must be a string, number, boolean, null or one of the registered custom value types")}return t}},{key:"_escapeValue",value:function(t){return this.options.replaceSingleQuotes?t.replace(/\'/g,this.options.singleQuoteReplacement):t}},{key:"_formatTableName",value:function(t){if(this.options.autoQuoteTableNames){var e=this.options.nameQuoteCharacter;t=""+e+t+e}return t}},{key:"_formatFieldAlias",value:function(t){if(this.options.autoQuoteAliasNames){var e=this.options.fieldAliasQuoteCharacter;t=""+e+t+e}return t}},{key:"_formatTableAlias",value:function(t){if(this.options.autoQuoteAliasNames){var e=this.options.tableAliasQuoteCharacter;t=""+e+t+e}return this.options.useAsForTableAliasNames?"AS "+t:t}},{key:"_formatFieldName",value:function(t){var e=this,n=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];return this.options.autoQuoteFieldNames&&!function(){var r=e.options.nameQuoteCharacter;t=n.ignorePeriodsForFieldNameQuotes?""+r+t+r:t.split(".").map(function(t){return"*"===t?t:""+r+t+r}).join(".")}(),t}},{key:"_formatCustomValue",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?!1:arguments[1],n=f(t,this.options.valueHandlers,y.globalValueHandlers);return n&&(t=n(t,e)),t}},{key:"_formatValueForParamArray",value:function(t){var e=this;return l(t)?t.map(function(t){return e._formatValueForParamArray(t)}):this._formatCustomValue(t,!0)}},{key:"_formatValueForQueryString",value:function(t){var e=this,n=arguments.length<=1||void 0===arguments[1]?{}:arguments[1],r=this._formatCustomValue(t);if(r!==t)return this._applyNestingFormatting(r);if(l(t))t=t.map(function(t){return e._formatValueForQueryString(t)}),t=this._applyNestingFormatting(t.join(", "));else{var i="undefined"==typeof t?"undefined":p(t);if(null===t)t="NULL";else if("boolean"===i)t=t?"TRUE":"FALSE";else if(t instanceof y.BaseBuilder)t=this._applyNestingFormatting(t.toString());else if("number"!==i)if(n.dontQuote)t=""+t;else{var o=this._escapeValue(t);t="'"+o+"'"}}return t}},{key:"_applyNestingFormatting",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?!0:arguments[1];return t&&"string"==typeof t&&e&&("("!==t.charAt(0)||")"!==t.charAt(t.length-1))?"("+t+")":t}},{key:"_buildString",value:function(e,n){var r=arguments.length<=2||void 0===arguments[2]?{}:arguments[2],i=r.nested,o=r.buildParameterized,a=r.formattingOptions;n=n||[],e=e||"";for(var u="",s=-1,c=[],f=this.options.parameterCharacter,h=0;e.length>h;)if(e.substr(h,f.length)===f){var v=n[++s];if(o)if(v instanceof y.BaseBuilder){var d=v._toParamString({buildParameterized:o,nested:!0});u+=d.text,c.push.apply(c,t(d.values))}else if(v=this._formatValueForParamArray(v),l(v)){var p=v.map(function(){return f}).join(", ");u+="("+p+")",c.push.apply(c,t(v))}else u+=f,c.push(v);else u+=this._formatValueForQueryString(v,a);h+=f.length}else u+=e.charAt(h),h++;return{text:this._applyNestingFormatting(u,!!i),values:c}}},{key:"_buildManyStrings",value:function(e,n){var r=arguments.length<=2||void 0===arguments[2]?{}:arguments[2],i=[],o=[];for(var a in e){var l=e[a],u=n[a],s=this._buildString(l,u,{buildParameterized:r.buildParameterized,nested:!1}),c=s.text,f=s.values;i.push(c),o.push.apply(o,t(f))}return i=i.join(this.options.separator),{text:i.length?this._applyNestingFormatting(i,!!r.nested):"",values:o}}},{key:"_toParamString",value:function(t){throw new Error("Not yet implemented")}},{key:"toString",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return this._toParamString(t).text}},{key:"toParam",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return this._toParamString(o({},t,{buildParameterized:!0}))}}]),a}(y.Cloneable),y.Expression=function(i){function o(t){r(this,o);var n=e(this,Object.getPrototypeOf(o).call(this,t));return n._nodes=[],n}return n(o,i),d(o,[{key:"and",value:function(t,e){for(var n=arguments.length,r=Array(n>2?n-2:0),i=2;n>i;i++)r[i-2]=arguments[i];return this._add("AND",t,e,r)}},{key:"or",value:function(t,e){for(var n=arguments.length,r=Array(n>2?n-2:0),i=2;n>i;i++)r[i-2]=arguments[i];return this._add("OR",t,e,r)}},{key:"_add",value:function(t,e,n,r){var i=["=","<",">","<=",">=","<>","!=","in","not in","like","not like"],o=void 0;return"string"==typeof e&&"string"==typeof n&&-1!=i.indexOf(n.toLowerCase())?o=this._buildExpression(e,n):(o=e,r.unshift(n)),this._sanitizeExpression(e),this._nodes.push({type:t,expr:o,para:r}),this}},{key:"_buildExpression",value:function(t,e){var n=this._formatFieldName(t),r=y.DefaultQueryBuilderOptions.parameterCharacter,i=n+" "+e+" "+r;return i}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n=[],r=[],i=!0,o=!1,a=void 0;try{for(var l,u=this._nodes[Symbol.iterator]();!(i=(l=u.next()).done);i=!0){var s=l.value,c=s.type,f=s.expr,h=s.para,v=f instanceof y.Expression?f._toParamString({buildParameterized:e.buildParameterized,nested:!0}):this._buildString(f,h,{buildParameterized:e.buildParameterized}),d=v.text,p=v.values;n.length&&n.push(c),n.push(d),r.push.apply(r,t(p))}}catch(g){o=!0,a=g}finally{try{!i&&u["return"]&&u["return"]()}finally{if(o)throw a}}return n=n.join(" "),{text:this._applyNestingFormatting(n,!!e.nested),values:r}}}]),o}(y.BaseBuilder),y.Case=function(l){function u(t){var n=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];r(this,u);var i=e(this,Object.getPrototypeOf(u).call(this,n));return a(t)&&(n=t,t=null),t&&(i._fieldName=i._sanitizeField(t)),i.options=o({},y.DefaultQueryBuilderOptions,n),i._cases=[],i._elseValue=null,i}return n(u,l),d(u,[{key:"when",value:function(t){for(var e=arguments.length,n=Array(e>1?e-1:0),r=1;e>r;r++)n[r-1]=arguments[r];return this._cases.unshift({expression:t,values:n}),this}},{key:"then",value:function(t){if(0==this._cases.length)throw new Error("when() needs to be called first");return this._cases[0].result=t,this}},{key:"else",value:function(t){return this._elseValue=t,this}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n="",r=[],o=!0,a=!1,l=void 0;try{for(var u,s=this._cases[Symbol.iterator]();!(o=(u=s.next()).done);o=!0){var c=u.value,f=c.expression,h=c.values,v=c.result;n=i(n," ");var d=this._buildString(f,h,{buildParameterized:e.buildParameterized,nested:!0});n+="WHEN "+d.text+" THEN "+this._formatValueForQueryString(v),r.push.apply(r,t(d.values))}}catch(p){a=!0,l=p}finally{try{!o&&s["return"]&&s["return"]()}finally{if(a)throw l}}return n.length?(n+=" ELSE "+this._formatValueForQueryString(this._elseValue)+" END",this._fieldName&&(n=this._fieldName+" "+n),n="CASE "+n):n=this._formatValueForQueryString(this._elseValue),{text:n,values:r}}}]),u}(y.BaseBuilder),y.Block=function(t){function i(t){return r(this,i),e(this,Object.getPrototypeOf(i).call(this,t))}return n(i,t),d(i,[{key:"exposedMethods",value:function(){for(var t={},e=this;e;)Object.getOwnPropertyNames(e).forEach(function(n){"constructor"===n||"function"!=typeof e[n]||"_"===n.charAt(0)||y.Block.prototype[n]||(t[n]=e[n])}),e=Object.getPrototypeOf(e);return t}}]),i}(y.BaseBuilder),y.StringBlock=function(t){function i(t,n){r(this,i);var o=e(this,Object.getPrototypeOf(i).call(this,t));return o._str=n,o}return n(i,t),d(i,[{key:"_toParamString",value:function(){arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return{text:this._str,values:[]}}}]),i}(y.Block),y.FunctionBlock=function(t){function i(t){r(this,i);var n=e(this,Object.getPrototypeOf(i).call(this,t));return n._strings=[],n._values=[],n}return n(i,t),d(i,[{key:"function",value:function(t){this._strings.push(t);for(var e=arguments.length,n=Array(e>1?e-1:0),r=1;e>r;r++)n[r-1]=arguments[r];this._values.push(n)}},{key:"_toParamString",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return this._buildManyStrings(this._strings,this._values,t)}}]),i}(y.Block),y.registerValueHandler(y.FunctionBlock,function(t){var e=arguments.length<=1||void 0===arguments[1]?!1:arguments[1];return e?t.toParam():t.toString()}),y.AbstractTableBlock=function(o){function a(t,n){r(this,a);var i=e(this,Object.getPrototypeOf(a).call(this,t));return i._tables=[],i}return n(a,o),d(a,[{key:"_table",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1];e=e?this._sanitizeTableAlias(e):e,t=this._sanitizeTable(t),this.options.singleTable&&(this._tables=[]),this._tables.push({table:t,alias:e})}},{key:"_hasTable",value:function(){return 01)throw new Error("Cannot set multiple rows of fields this way.");"undefined"!=typeof e&&(e=this._sanitizeValue(e)),t=this._sanitizeField(t);var r=this._fields.indexOf(t);-1===r&&(this._fields.push(t),r=this._fields.length-1),this._values[0][r]=e,this._valueOptions[0][r]=n}},{key:"_setFields",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];if("object"!==("undefined"==typeof t?"undefined":p(t)))throw new Error("Expected an object but got "+("undefined"==typeof t?"undefined":p(t)));for(var n in t)this._set(n,t[n],e)}},{key:"_setFieldsRows",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];if(!l(t))throw new Error("Expected an array of objects but got "+("undefined"==typeof t?"undefined":p(t)));this._reset();for(var n in t){var r=t[n];for(var i in r){var o=r[i];i=this._sanitizeField(i),o=this._sanitizeValue(o);var a=this._fields.indexOf(i);if(n>0&&-1===a)throw new Error("All fields in subsequent rows must match the fields in the first row");-1===a&&(this._fields.push(i),a=this._fields.length-1),l(this._values[n])||(this._values[n]=[],this._valueOptions[n]=[]),this._values[n][a]=o,this._valueOptions[n][a]=e}}}}]),i}(y.Block),y.SetFieldBlock=function(o){function a(){return r(this,a),e(this,Object.getPrototypeOf(a).apply(this,arguments))}return n(a,o),d(a,[{key:"set",value:function(t,e,n){this._set(t,e,n)}},{key:"setFields",value:function(t,e){this._setFields(t,e)}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n=e.buildParameterized;if(0>=this._fields.length)throw new Error("set() needs to be called");var r="",o=[];for(var a in this._fields){r=i(r,", ");var l=this._fields[a],u=this._values[0][a];if("undefined"==typeof u)r+=l;else{var s=this._buildString(l+" = "+this.options.parameterCharacter,[u],{buildParameterized:n,formattingOptions:this._valueOptions[0][a]});r+=s.text,o.push.apply(o,t(s.values))}}return{text:"SET "+r,values:o}}}]),a}(y.AbstractSetFieldBlock),y.InsertFieldValueBlock=function(o){function a(){return r(this,a),e(this,Object.getPrototypeOf(a).apply(this,arguments))}return n(a,o),d(a,[{key:"set",value:function(t,e){var n=arguments.length<=2||void 0===arguments[2]?{}:arguments[2];this._set(t,e,n)}},{key:"setFields",value:function(t,e){this._setFields(t,e)}},{key:"setFieldsRows",value:function(t,e){this._setFieldsRows(t,e)}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n=e.buildParameterized,r=this._fields.join(", "),o=[],a=[];for(var l in this._values){o[l]="";for(var u in this._values[l]){var s=this._buildString(this.options.parameterCharacter,[this._values[l][u]],{buildParameterized:n,formattingOptions:this._valueOptions[l][u]});a.push.apply(a,t(s.values)),o[l]=i(o[l],", "),o[l]+=s.text}}return{text:r.length?"("+r+") VALUES ("+o.join("), (")+")":"",values:a}}}]),a}(y.AbstractSetFieldBlock),y.InsertFieldsFromQueryBlock=function(t){function i(t){r(this,i);var n=e(this,Object.getPrototypeOf(i).call(this,t));return n._fields=[],n._query=null,n}return n(i,t),d(i,[{key:"fromQuery",value:function(t,e){var n=this;this._fields=t.map(function(t){return n._sanitizeField(t)}),this._query=this._sanitizeQueryBuilder(e)}},{key:"_toParamString",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],e="",n=[];if(this._fields.length&&this._query){var r=this._query._toParamString({buildParameterized:t.buildParameterized,nested:!0}),i=r.text,o=r.values;e="("+this._fields.join(", ")+") ("+i+")",n=o}return{text:e,values:n}}}]),i}(y.Block),y.DistinctBlock=function(t){function i(){return r(this,i),e(this,Object.getPrototypeOf(i).apply(this,arguments))}return n(i,t),d(i,[{key:"distinct",value:function(){this._useDistinct=!0}},{key:"_toParamString",value:function(){return{text:this._useDistinct?"DISTINCT":"",values:[]}}}]),i}(y.Block),y.GroupByBlock=function(t){function i(t){r(this,i);var n=e(this,Object.getPrototypeOf(i).call(this,t));return n._groups=[],n}return n(i,t),d(i,[{key:"group",value:function(t){this._groups.push(this._sanitizeField(t))}},{key:"_toParamString",value:function(){arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return{text:this._groups.length?"GROUP BY "+this._groups.join(", "):"",values:[]}}}]),i}(y.Block),y.OffsetBlock=function(t){function i(t){r(this,i);var n=e(this,Object.getPrototypeOf(i).call(this,t));return n._offsets=null,n}return n(i,t),d(i,[{key:"offset",value:function(t){this._offsets=this._sanitizeLimitOffset(t)}},{key:"_toParamString",value:function(){return{text:this._offsets?"OFFSET "+this._offsets:"",values:[]}}}]),i}(y.Block),y.AbstractConditionBlock=function(o){function a(t){r(this,a);var n=e(this,Object.getPrototypeOf(a).call(this,t));return n._conditions=[],n}return n(a,o),d(a,[{key:"_condition",value:function(t){for(var e=arguments.length,n=Array(e>1?e-1:0),r=1;e>r;r++)n[r-1]=arguments[r];t=this._sanitizeExpression(t),this._conditions.push({expr:t,values:n})}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n="",r=[],o=!0,a=!1,l=void 0;try{for(var u,s=this._conditions[Symbol.iterator]();!(o=(u=s.next()).done);o=!0){var c=u.value,f=c.expr,h=c.values;n=i(n,") AND (");var v=f instanceof y.Expression?f._toParamString({buildParameterized:e.buildParameterized}):this._buildString(f,h,{buildParameterized:e.buildParameterized});n+=v.text,r.push.apply(r,t(v.values))}}catch(d){a=!0,l=d}finally{try{!o&&s["return"]&&s["return"]()}finally{if(a)throw l}}return{text:n.length?this.options.verb+" ("+n+")":"",values:r}}}]),a}(y.Block),y.WhereBlock=function(t){function i(t){return r(this,i),e(this,Object.getPrototypeOf(i).call(this,o({},t,{verb:"WHERE"})))}return n(i,t),d(i,[{key:"where",value:function(t,e){for(var n=arguments.length,r=Array(n>2?n-2:0),i=2;n>i;i++)r[i-2]=arguments[i];var o=["=","<",">","<=",">=","<>","!=","in","not in","like","not like"];if("string"==typeof t&&"string"==typeof e&&-1!=o.indexOf(e.toLowerCase())){var a=new y.Expression;this._condition(a._add("AND",t,e,r))}else this._condition.apply(this,arguments)}}]),i}(y.AbstractConditionBlock),y.HavingBlock=function(t){function i(t){return r(this,i),e(this,Object.getPrototypeOf(i).call(this,o({},t,{verb:"HAVING"})))}return n(i,t),d(i,[{key:"having",value:function(t,e){for(var n=arguments.length,r=Array(n>2?n-2:0),i=2;n>i;i++)r[i-2]=arguments[i];var o=["=","<",">","<=",">=","<>","!=","in","not in","like","not like"];if("string"==typeof t&&"string"==typeof e&&-1!=o.indexOf(e.toLowerCase())){var a=new y.Expression;this._condition(a._add("AND",t,e,r))}else this._condition.apply(this,arguments)}}]),i}(y.AbstractConditionBlock),y.OrderByBlock=function(o){function a(t){r(this,a);var n=e(this,Object.getPrototypeOf(a).call(this,t));return n._orders=[],n}return n(a,o),d(a,[{key:"order",value:function(t,e){for(var n=arguments.length,r=Array(n>2?n-2:0),i=2;n>i;i++)r[i-2]=arguments[i];t=this._sanitizeField(t),e=void 0===e?!0:e,e=null!==e?!!e:e,this._orders.push({field:t,dir:e,values:r})}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n="",r=[],o=!0,a=!1,l=void 0;try{for(var u,s=this._orders[Symbol.iterator]();!(o=(u=s.next()).done);o=!0){var c=u.value,f=c.field,h=c.dir,v=c.values;n=i(n,", ");var d=this._buildString(f,v,{buildParameterized:e.buildParameterized});n+=d.text,r.push.apply(r,t(d.values)),null!==h&&(n+=" "+(h?"ASC":"DESC"))}}catch(p){a=!0,l=p}finally{try{!o&&s["return"]&&s["return"]()}finally{if(a)throw l}}return{text:n.length?"ORDER BY "+n:"",values:r}}}]),a}(y.Block),y.LimitBlock=function(t){function i(t){r(this,i);var n=e(this,Object.getPrototypeOf(i).call(this,t));return n._limit=null,n}return n(i,t),d(i,[{key:"limit",value:function(t){this._limit=this._sanitizeLimitOffset(t)}},{key:"_toParamString",value:function(){return{text:null!==this._limit?"LIMIT "+this._limit:"",values:[]}}}]),i}(y.Block),y.JoinBlock=function(o){function a(t){r(this,a);var n=e(this,Object.getPrototypeOf(a).call(this,t));return n._joins=[],n}return n(a,o),d(a,[{key:"join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],n=arguments.length<=2||void 0===arguments[2]?null:arguments[2],r=arguments.length<=3||void 0===arguments[3]?"INNER":arguments[3];t=this._sanitizeTable(t,!0),e=e?this._sanitizeTableAlias(e):e,n=n?this._sanitizeExpression(n):n,this._joins.push({type:r,table:t,alias:e,condition:n})}},{key:"left_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],n=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,n,"LEFT")}},{key:"right_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],n=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,n,"RIGHT")}},{key:"outer_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],n=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,n,"OUTER")}},{key:"left_outer_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],n=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,n,"LEFT OUTER")}},{key:"full_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],n=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,n,"FULL")}},{key:"cross_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],n=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,n,"CROSS")}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n="",r=[],o=!0,a=!1,l=void 0;try{for(var u,s=this._joins[Symbol.iterator]();!(o=(u=s.next()).done);o=!0){var c=u.value,f=c.type,h=c.table,v=c.alias,d=c.condition;n=i(n,this.options.separator);var p=void 0;if(h instanceof y.BaseBuilder){var g=h._toParamString({buildParameterized:e.buildParameterized,nested:!0});r.push.apply(r,t(g.values)),p=g.text}else p=this._formatTableName(h);if(n+=f+" JOIN "+p,v&&(n+=" "+this._formatTableAlias(v)),d){n+=" ON ";var _=void 0;_=d instanceof y.Expression?d._toParamString({buildParameterized:e.buildParameterized}):this._buildString(d,[],{buildParameterized:e.buildParameterized}),n+=this._applyNestingFormatting(_.text),r.push.apply(r,t(_.values))}}}catch(m){a=!0,l=m}finally{try{!o&&s["return"]&&s["return"]()}finally{if(a)throw l}}return{text:n,values:r}}}]),a}(y.Block),y.UnionBlock=function(o){function a(t){r(this,a);var n=e(this,Object.getPrototypeOf(a).call(this,t));return n._unions=[],n}return n(a,o),d(a,[{key:"union",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?"UNION":arguments[1];t=this._sanitizeTable(t),this._unions.push({type:e,table:t})}},{key:"union_all",value:function(t){this.union(t,"UNION ALL")}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n="",r=[],o=!0,a=!1,l=void 0;try{for(var u,s=this._unions[Symbol.iterator]();!(o=(u=s.next()).done);o=!0){var c=u.value,f=c.type,h=c.table;n=i(n,this.options.separator);var v=void 0;if(h instanceof y.BaseBuilder){var d=h._toParamString({buildParameterized:e.buildParameterized,nested:!0});v=d.text,r.push.apply(r,t(d.values))}else n=this._formatTableName(h);n+=f+" "+v}}catch(p){a=!0,l=p}finally{try{!o&&s["return"]&&s["return"]()}finally{if(a)throw l}}return{text:n,values:r}}}]),a}(y.Block),y.QueryBuilder=function(i){function a(t,n){r(this,a);var i=e(this,Object.getPrototypeOf(a).call(this,t));i.blocks=n||[];var o=!0,l=!1,u=void 0;try{for(var s,c=i.blocks[Symbol.iterator]();!(o=(s=c.next()).done);o=!0){var f=s.value,h=f.exposedMethods();for(var v in h){var d=h[v];if(void 0!==i[v])throw new Error("Builder already has a builder method called: "+v);!function(t,e,n){i[e]=function(){for(var e=arguments.length,r=Array(e),o=0;e>o;o++)r[o]=arguments[o];return n.call.apply(n,[t].concat(r)),i}}(f,v,d)}}}catch(p){l=!0,u=p}finally{try{!o&&c["return"]&&c["return"]()}finally{if(l)throw u}}return i}return n(a,i),d(a,[{key:"registerValueHandler",value:function(t,e){var n=!0,r=!1,i=void 0;try{for(var o,l=this.blocks[Symbol.iterator]();!(n=(o=l.next()).done);n=!0){var u=o.value;u.registerValueHandler(t,e)}}catch(s){r=!0,i=s}finally{try{!n&&l["return"]&&l["return"]()}finally{if(r)throw i}}return v(Object.getPrototypeOf(a.prototype),"registerValueHandler",this).call(this,t,e),this}},{key:"updateOptions",value:function(t){this.options=o({},this.options,t);var e=!0,n=!1,r=void 0;try{for(var i,a=this.blocks[Symbol.iterator]();!(e=(i=a.next()).done);e=!0){var l=i.value;l.options=o({},l.options,t)}}catch(u){n=!0,r=u}finally{try{!e&&a["return"]&&a["return"]()}finally{if(n)throw r}}}},{key:"_toParamString",value:function(){var e,n=this,r=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];r=o({},this.options,r);var i=this.blocks.map(function(t){return t._toParamString({buildParameterized:r.buildParameterized,queryBuilder:n})}),a=i.map(function(t){return t.text}),l=i.map(function(t){return t.values}),u=a.filter(function(t){return 0=this._fields.length)throw new Error("set() needs to be called");var e=v(Object.getPrototypeOf(i.prototype),"_toParamString",this).call(this,t);if(e.text.length&&0', '<=', '>=', '<>', '!=', 'in', 'not in', 'like', 'not like']; + let expr; + if (typeof field === 'string' && typeof operator === 'string' && -1 != validOperators.indexOf(operator.toLowerCase())) { + expr = this._buildExpression(field, operator); + } else { + expr = field; + params.unshift(operator); + } + this._sanitizeExpression(field); this._nodes.push({ - type: 'OR', + type: type, expr: expr, - para: params, + para: params }); return this; } + _buildExpression(field, operator) { + let escapedKey = this._formatFieldName(field); + let paramChar = cls.DefaultQueryBuilderOptions.parameterCharacter; + let condition = `${escapedKey} ${operator} ${paramChar}`; + return condition; + } + _toParamString (options = {}) { let totalStr = [], @@ -723,7 +736,7 @@ function _buildSquel(flavour = null) { let { type, expr, para } = node; - let { text, values } = (expr instanceof cls.Expression) + let { text, values } = (expr instanceof cls.Expression) ? expr._toParamString({ buildParameterized: options.buildParameterized, nested: true, @@ -752,7 +765,7 @@ function _buildSquel(flavour = null) { } - + /* # --------------------------------------------------------------------------------------------------------- # --------------------------------------------------------------------------------------------------------- @@ -784,7 +797,7 @@ function _buildSquel(flavour = null) { this.options = _extend({}, cls.DefaultQueryBuilderOptions, options); this._cases = []; - this._elseValue = null; + this._elseValue = null; } when (expression, ...values) { @@ -802,7 +815,7 @@ function _buildSquel(flavour = null) { } this._cases[0].result = result; - + return this; } @@ -844,7 +857,7 @@ function _buildSquel(flavour = null) { return { text: totalStr, values: totalValues, - }; + }; } } @@ -892,8 +905,8 @@ function _buildSquel(flavour = null) { while (obj) { Object.getOwnPropertyNames(obj).forEach(function(prop) { if ('constructor' !== prop - && typeof obj[prop] === "function" - && prop.charAt(0) !== '_' + && typeof obj[prop] === "function" + && prop.charAt(0) !== '_' && !cls.Block.prototype[prop]) { ret[prop] = obj[prop]; @@ -902,7 +915,7 @@ function _buildSquel(flavour = null) { obj = Object.getPrototypeOf(obj); }; - + return ret; } } @@ -934,7 +947,7 @@ function _buildSquel(flavour = null) { this._strings = []; this._values = []; - } + } function (str, ...values) { this._strings.push(str); @@ -984,7 +997,7 @@ function _buildSquel(flavour = null) { this._tables.push({ table: table, - alias: alias, + alias: alias, }); } @@ -1014,7 +1027,7 @@ function _buildSquel(flavour = null) { }); tableStr = text; - totalValues.push(...values); + totalValues.push(...values); } if (alias) { @@ -1058,7 +1071,7 @@ function _buildSquel(flavour = null) { // FROM table cls.FromTableBlock = class extends cls.AbstractTableBlock { constructor (options) { - super(_extend({}, options, { + super(_extend({}, options, { prefix: 'FROM', })); } @@ -1077,7 +1090,7 @@ function _buildSquel(flavour = null) { // INTO table cls.IntoTableBlock = class extends cls.AbstractTableBlock { constructor (options) { - super(_extend({}, options, { + super(_extend({}, options, { prefix: 'INTO', singleTable: true, })); @@ -1168,7 +1181,7 @@ function _buildSquel(flavour = null) { for (let field of this._fields) { totalStr = _pad(totalStr, ", "); - + let { name, alias, options } = field; if (typeof name === 'string') { @@ -1287,7 +1300,7 @@ function _buildSquel(flavour = null) { // Add field only if it hasn't been added before if (-1 === index) { this._fields.push(field); - index = this._fields.length - 1; + index = this._fields.length - 1; } // The first value added needs to add the array @@ -1336,7 +1349,7 @@ function _buildSquel(flavour = null) { } else { let ret = this._buildString( - `${field} = ${this.options.parameterCharacter}`, + `${field} = ${this.options.parameterCharacter}`, [value], { buildParameterized: buildParameterized, @@ -1349,9 +1362,9 @@ function _buildSquel(flavour = null) { } } - return { - text: `SET ${totalStr}`, - values: totalValues + return { + text: `SET ${totalStr}`, + values: totalValues }; } @@ -1374,7 +1387,7 @@ function _buildSquel(flavour = null) { _toParamString (options = {}) { let { buildParameterized } = options; - + let fieldString = this._fields.join(', '), valueStrings = [], totalValues = []; @@ -1383,24 +1396,24 @@ function _buildSquel(flavour = null) { valueStrings[i] = ''; for (let j in this._values[i]) { - let ret = + let ret = this._buildString(this.options.parameterCharacter, [this._values[i][j]], { buildParameterized: buildParameterized, formattingOptions: this._valueOptions[i][j], }); - totalValues.push(...ret.values); + totalValues.push(...ret.values); valueStrings[i] = _pad(valueStrings[i], ', '); valueStrings[i] += ret.text; } } - return { - text: fieldString.length + return { + text: fieldString.length ? `(${fieldString}) VALUES (${valueStrings.join('), (')})` : '', - values: totalValues + values: totalValues }; } @@ -1517,7 +1530,7 @@ function _buildSquel(flavour = null) { // Abstract condition base class cls.AbstractConditionBlock = class extends cls.Block { - /** + /** * @param {String} options.verb The condition verb. */ constructor (options) { @@ -1550,7 +1563,7 @@ function _buildSquel(flavour = null) { for (let { expr, values } of this._conditions) { totalStr = _pad(totalStr, ') AND ('); - let ret = (expr instanceof cls.Expression) + let ret = (expr instanceof cls.Expression) ? expr._toParamString({ buildParameterized: options.buildParameterized, }) @@ -1579,8 +1592,16 @@ function _buildSquel(flavour = null) { })); } - where (condition, ...values) { - this._condition(condition, ...values); + where (field, operator, ...values) { + let validOperators = ['=', '<', '>', '<=', '>=', '<>', '!=', 'in', 'not in', 'like', 'not like']; + if (typeof field === 'string' && typeof operator === 'string' && -1 != validOperators.indexOf(operator.toLowerCase())) { + // key - operator - value + let expr = new cls.Expression(); + this._condition(expr._add('AND', field, operator, values)); + } else { + // default squel-behaviour without auto-quoting + this._condition.apply(this, arguments); + } } } @@ -1593,8 +1614,16 @@ function _buildSquel(flavour = null) { })); } - having (condition, ...values) { - this._condition(condition, ...values); + having (field, operator, ...values) { + let validOperators = ['=', '<', '>', '<=', '>=', '<>', '!=', 'in', 'not in', 'like', 'not like']; + if (typeof field === 'string' && typeof operator === 'string' && -1 != validOperators.indexOf(operator.toLowerCase())) { + // key - operator - value + let expr = new cls.Expression(); + this._condition(expr._add('AND', field, operator, values)); + } else { + // default squel-behaviour without auto-quoting + this._condition.apply(this, arguments); + } } } @@ -1620,7 +1649,7 @@ function _buildSquel(flavour = null) { this._orders.push({ field: field, - dir: asc, + dir: asc, values: values, }); } @@ -1742,7 +1771,7 @@ function _buildSquel(flavour = null) { _toParamString (options = {}) { - let totalStr = "", + let totalStr = "", totalValues = []; for (let {type, table, alias, condition} of this._joins) { @@ -1756,7 +1785,7 @@ function _buildSquel(flavour = null) { nested: true }); - totalValues.push(...ret.values); + totalValues.push(...ret.values); tableStr = ret.text; } else { tableStr = this._formatTableName(table); @@ -1916,7 +1945,7 @@ function _buildSquel(flavour = null) { for (let block of this.blocks) { block.registerValueHandler(type, handler); } - + super.registerValueHandler(type, handler); return this; @@ -1958,15 +1987,15 @@ function _buildSquel(flavour = null) { if (!options.nested) { if (options.numberedParameters) { - let i = (undefined !== options.numberedParametersStartAt) - ? options.numberedParametersStartAt + let i = (undefined !== options.numberedParametersStartAt) + ? options.numberedParametersStartAt : 1; // construct regex for searching const regex = options.parameterCharacter.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"); totalStr = totalStr.replace( - new RegExp(regex, 'g'), + new RegExp(regex, 'g'), function() { return `${options.numberedParametersPrefix}${i++}`; } @@ -2018,7 +2047,7 @@ function _buildSquel(flavour = null) { ]; super(options, blocks); - } + } } diff --git a/test/blocks.test.coffee b/test/blocks.test.coffee index c5b74ccb..06c06318 100644 --- a/test/blocks.test.coffee +++ b/test/blocks.test.coffee @@ -1083,6 +1083,26 @@ test['Blocks'] = values: [10, 2, 3, 4, 5, 6] } + 'output with quoting': + beforeEach: -> + squel.cls.DefaultQueryBuilderOptions.autoQuoteFieldNames = true; + subquery = new squel.cls.Select() + subquery.field('col1').from('table1').where('field1', '=', 10) + @inst.where('a', 'in', subquery) + @inst.where('d', 'in', [4, 5, 6]) + 'non-parameterized': -> + assert.same @inst._toParamString(), { + text: 'WHERE (`a` in (SELECT `col1` FROM table1 WHERE (`field1` = 10))) AND (`d` in (4, 5, 6))' + values: [] + } + 'parameterized': -> + assert.same @inst._toParamString(buildParameterized: true), { + text: 'WHERE (`a` in (SELECT `col1` FROM table1 WHERE (`field1` = ?))) AND (`d` in (?, ?, ?))' + values: [10, 4, 5, 6] + } + afterEach: -> + squel.cls.DefaultQueryBuilderOptions.autoQuoteFieldNames = false; + 'HavingBlock': @@ -1123,6 +1143,26 @@ test['Blocks'] = values: [10, 2, 3, 4, 5, 6] } + 'output with quoting': + beforeEach: -> + squel.cls.DefaultQueryBuilderOptions.autoQuoteFieldNames = true; + subquery = new squel.cls.Select() + subquery.field('col1').from('table1').where('field1', '=', 10) + @inst.having('a', 'in', subquery) + @inst.having('d', 'in', [4, 5, 6]) + 'non-parameterized': -> + assert.same @inst._toParamString(), { + text: 'HAVING (`a` in (SELECT `col1` FROM table1 WHERE (`field1` = 10))) AND (`d` in (4, 5, 6))' + values: [] + } + 'parameterized': -> + assert.same @inst._toParamString(buildParameterized: true), { + text: 'HAVING (`a` in (SELECT `col1` FROM table1 WHERE (`field1` = ?))) AND (`d` in (?, ?, ?))' + values: [10, 4, 5, 6] + } + afterEach: -> + squel.cls.DefaultQueryBuilderOptions.autoQuoteFieldNames = false; + 'OrderByBlock': beforeEach: -> diff --git a/test/expressions.test.coffee b/test/expressions.test.coffee index 893fae92..d3791b70 100644 --- a/test/expressions.test.coffee +++ b/test/expressions.test.coffee @@ -177,6 +177,19 @@ test['Expression builder base class'] = values: [3, '4', false, 2, null, 'str'] } + 'and("test", "<>", 3)': + beforeEach: -> + @inst = squel.expr({ autoQuoteFieldNames: true }) + @inst.and("test", "<>", 3) + + '>> toString()': -> + assert.same @inst.toString(), '`test` <> 3' + + '>> toParam()': -> + assert.same @inst.toParam(), { + text: '`test` <> ?' + values: [3] + } 'or("test = 3")': beforeEach: -> From 6d540aa91839b326576d5c12c196b7df4007ed40 Mon Sep 17 00:00:00 2001 From: nic Date: Tue, 29 Mar 2016 23:21:01 +0200 Subject: [PATCH 2/4] added auto-quoting ability to WhereBlock.where(), HavingBlock.having(), Expression.or() and Expression.and() by providing an alternate method signature (field, operator, ...params) --- squel-basic.js | 6 +++--- squel-basic.min.js | 4 ++-- squel.js | 6 +++--- squel.min.js | 4 ++-- src/core.js | 6 +++--- test/blocks.test.coffee | 12 ++++-------- test/select.test.coffee | 24 ++++++++++++++++++++++++ 7 files changed, 41 insertions(+), 21 deletions(-) diff --git a/squel-basic.js b/squel-basic.js index 3a8bacf6..7c3aeae1 100644 --- a/squel-basic.js +++ b/squel-basic.js @@ -841,7 +841,7 @@ function _buildSquel() { key: '_buildExpression', value: function _buildExpression(field, operator) { var escapedKey = this._formatFieldName(field); - var paramChar = cls.DefaultQueryBuilderOptions.parameterCharacter; + var paramChar = this.options.parameterCharacter; var condition = escapedKey + ' ' + operator + ' ' + paramChar; return condition; } @@ -2114,7 +2114,7 @@ function _buildSquel() { var validOperators = ['=', '<', '>', '<=', '>=', '<>', '!=', 'in', 'not in', 'like', 'not like']; if (typeof field === 'string' && typeof operator === 'string' && -1 != validOperators.indexOf(operator.toLowerCase())) { // key - operator - value - var expr = new cls.Expression(); + var expr = new cls.Expression(this.options); this._condition(expr._add('AND', field, operator, values)); } else { // default squel-behaviour without auto-quoting @@ -2148,7 +2148,7 @@ function _buildSquel() { var validOperators = ['=', '<', '>', '<=', '>=', '<>', '!=', 'in', 'not in', 'like', 'not like']; if (typeof field === 'string' && typeof operator === 'string' && -1 != validOperators.indexOf(operator.toLowerCase())) { // key - operator - value - var expr = new cls.Expression(); + var expr = new cls.Expression(this.options); this._condition(expr._add('AND', field, operator, values)); } else { // default squel-behaviour without auto-quoting diff --git a/squel-basic.min.js b/squel-basic.min.js index 72b8d807..f9c7dbe0 100644 --- a/squel-basic.min.js +++ b/squel-basic.min.js @@ -1,2 +1,2 @@ -/*! squel | https://github.com/hiddentao/squel | BSD license */!function(t,e){"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?module.exports=e():t.squel=e()}(this,function(){"use strict";function t(t){if(Array.isArray(t)){for(var e=0,r=Array(t.length);e1?e-1:0),n=1;e>n;n++)r[n-1]=arguments[n];if(t&&r){var i=!0,a=!1,o=void 0;try{for(var l,u=function(){var e=l.value;"object"===("undefined"==typeof e?"undefined":y(e))&&Object.getOwnPropertyNames(e).forEach(function(r){"function"!=typeof e[r]&&(t[r]=e[r])})},s=r[Symbol.iterator]();!(i=(l=s.next()).done);i=!0)u()}catch(f){a=!0,o=f}finally{try{!i&&s["return"]&&s["return"]()}finally{if(a)throw o}}}return t}function o(t){return t&&t.constructor.prototype===Object.prototype}function l(t){return t&&t.constructor.prototype===Array.prototype}function u(t){if(t&&t.constructor&&t.constructor.toString){var e=t.constructor.toString().match(/function\s*(\w+)/);if(e&&2===e.length)return e[1]}}function s(t){if(!t)return t;if("function"==typeof t.clone)return t.clone();if(!o(t)&&!l(t))return JSON.parse(JSON.stringify(t));var e=function(){var e=new t.constructor;return Object.getOwnPropertyNames(t).forEach(function(r){"function"!=typeof t[r]&&(e[r]=s(t[r]))}),{v:e}}();return"object"===("undefined"==typeof e?"undefined":y(e))?e.v:void 0}function f(t,e,r){var n="undefined"==typeof e?"undefined":y(e);if("function"!==n&&"string"!==n)throw new Error("type must be a class constructor or string");if("function"!=typeof r)throw new Error("handler must be a function");for(var i in t){var a=t[i];if(a.type===e)return void(a.handler=r)}t.push({type:e,handler:r})}function c(t){for(var e=arguments.length,r=Array(e>1?e-1:0),n=1;e>n;n++)r[n-1]=arguments[n];for(var i in r){var a=r[i];for(var o in a){var l=a[o];if(("undefined"==typeof t?"undefined":y(t))===l.type||"string"!=typeof l.type&&t instanceof l.type)return l.handler}}}function h(){var h=arguments.length<=0||void 0===arguments[0]?null:arguments[0],p={_getObjectClassName:u};p.DefaultQueryBuilderOptions={autoQuoteTableNames:!1,autoQuoteFieldNames:!1,autoQuoteAliasNames:!0,useAsForTableAliasNames:!1,nameQuoteCharacter:"`",tableAliasQuoteCharacter:"`",fieldAliasQuoteCharacter:'"',valueHandlers:[],parameterCharacter:"?",numberedParameters:!1,numberedParametersPrefix:"$",numberedParametersStartAt:1,replaceSingleQuotes:!1,singleQuoteReplacement:"''",separator:" "},p.globalValueHandlers=[],p.registerValueHandler=function(t,e){f(p.globalValueHandlers,t,e)},p.Cloneable=function(){function t(){n(this,t)}return d(t,[{key:"clone",value:function(){var t=new this.constructor;return a(t,s(a({},this)))}}]),t}(),p.BaseBuilder=function(i){function o(t){n(this,o);var r=e(this,Object.getPrototypeOf(o).call(this)),i=JSON.parse(JSON.stringify(p.DefaultQueryBuilderOptions));return r.options=a({},i,t),r}return r(o,i),d(o,[{key:"registerValueHandler",value:function(t,e){return f(this.options.valueHandlers,t,e),this}},{key:"_sanitizeExpression",value:function(t){if(!(t instanceof p.Expression)&&"string"!=typeof t)throw new Error("expression must be a string or Expression instance");return t}},{key:"_sanitizeName",value:function(t,e){if("string"!=typeof t)throw new Error(e+" must be a string");return t}},{key:"_sanitizeField",value:function(t){return t instanceof p.BaseBuilder||(t=this._sanitizeName(t,"field name")),t}},{key:"_sanitizeQueryBuilder",value:function(t){if(t instanceof p.QueryBuilder)return t;throw new Error("must be a QueryBuilder instance")}},{key:"_sanitizeTable",value:function(t){if("string"!=typeof t)try{t=this._sanitizeQueryBuilder(t)}catch(e){throw new Error("table name must be a string or a query builder")}else t=this._sanitizeName(t,"table");return t}},{key:"_sanitizeTableAlias",value:function(t){return this._sanitizeName(t,"table alias")}},{key:"_sanitizeFieldAlias",value:function(t){return this._sanitizeName(t,"field alias")}},{key:"_sanitizeLimitOffset",value:function(t){if(t=parseInt(t),0>t||isNaN(t))throw new Error("limit/offset must be >= 0");return t}},{key:"_sanitizeValue",value:function(t){var e="undefined"==typeof t?"undefined":y(t);if(null===t);else if("string"===e||"number"===e||"boolean"===e);else if(t instanceof p.BaseBuilder);else{var r=!!c(t,this.options.valueHandlers,p.globalValueHandlers);if(!r)throw new Error("field value must be a string, number, boolean, null or one of the registered custom value types")}return t}},{key:"_escapeValue",value:function(t){return this.options.replaceSingleQuotes?t.replace(/\'/g,this.options.singleQuoteReplacement):t}},{key:"_formatTableName",value:function(t){if(this.options.autoQuoteTableNames){var e=this.options.nameQuoteCharacter;t=""+e+t+e}return t}},{key:"_formatFieldAlias",value:function(t){if(this.options.autoQuoteAliasNames){var e=this.options.fieldAliasQuoteCharacter;t=""+e+t+e}return t}},{key:"_formatTableAlias",value:function(t){if(this.options.autoQuoteAliasNames){var e=this.options.tableAliasQuoteCharacter;t=""+e+t+e}return this.options.useAsForTableAliasNames?"AS "+t:t}},{key:"_formatFieldName",value:function(t){var e=this,r=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];return this.options.autoQuoteFieldNames&&!function(){var n=e.options.nameQuoteCharacter;t=r.ignorePeriodsForFieldNameQuotes?""+n+t+n:t.split(".").map(function(t){return"*"===t?t:""+n+t+n}).join(".")}(),t}},{key:"_formatCustomValue",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?!1:arguments[1],r=c(t,this.options.valueHandlers,p.globalValueHandlers);return r&&(t=r(t,e)),t}},{key:"_formatValueForParamArray",value:function(t){var e=this;return l(t)?t.map(function(t){return e._formatValueForParamArray(t)}):this._formatCustomValue(t,!0)}},{key:"_formatValueForQueryString",value:function(t){var e=this,r=arguments.length<=1||void 0===arguments[1]?{}:arguments[1],n=this._formatCustomValue(t);if(n!==t)return this._applyNestingFormatting(n);if(l(t))t=t.map(function(t){return e._formatValueForQueryString(t)}),t=this._applyNestingFormatting(t.join(", "));else{var i="undefined"==typeof t?"undefined":y(t);if(null===t)t="NULL";else if("boolean"===i)t=t?"TRUE":"FALSE";else if(t instanceof p.BaseBuilder)t=this._applyNestingFormatting(t.toString());else if("number"!==i)if(r.dontQuote)t=""+t;else{var a=this._escapeValue(t);t="'"+a+"'"}}return t}},{key:"_applyNestingFormatting",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?!0:arguments[1];return t&&"string"==typeof t&&e&&("("!==t.charAt(0)||")"!==t.charAt(t.length-1))?"("+t+")":t}},{key:"_buildString",value:function(e,r){var n=arguments.length<=2||void 0===arguments[2]?{}:arguments[2],i=n.nested,a=n.buildParameterized,o=n.formattingOptions;r=r||[],e=e||"";for(var u="",s=-1,f=[],c=this.options.parameterCharacter,h=0;e.length>h;)if(e.substr(h,c.length)===c){var v=r[++s];if(a)if(v instanceof p.BaseBuilder){var d=v._toParamString({buildParameterized:a,nested:!0});u+=d.text,f.push.apply(f,t(d.values))}else if(v=this._formatValueForParamArray(v),l(v)){var y=v.map(function(){return c}).join(", ");u+="("+y+")",f.push.apply(f,t(v))}else u+=c,f.push(v);else u+=this._formatValueForQueryString(v,o);h+=c.length}else u+=e.charAt(h),h++;return{text:this._applyNestingFormatting(u,!!i),values:f}}},{key:"_buildManyStrings",value:function(e,r){var n=arguments.length<=2||void 0===arguments[2]?{}:arguments[2],i=[],a=[];for(var o in e){var l=e[o],u=r[o],s=this._buildString(l,u,{buildParameterized:n.buildParameterized,nested:!1}),f=s.text,c=s.values;i.push(f),a.push.apply(a,t(c))}return i=i.join(this.options.separator),{text:i.length?this._applyNestingFormatting(i,!!n.nested):"",values:a}}},{key:"_toParamString",value:function(t){throw new Error("Not yet implemented")}},{key:"toString",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return this._toParamString(t).text}},{key:"toParam",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return this._toParamString(a({},t,{buildParameterized:!0}))}}]),o}(p.Cloneable),p.Expression=function(i){function a(t){n(this,a);var r=e(this,Object.getPrototypeOf(a).call(this,t));return r._nodes=[],r}return r(a,i),d(a,[{key:"and",value:function(t,e){for(var r=arguments.length,n=Array(r>2?r-2:0),i=2;r>i;i++)n[i-2]=arguments[i];return this._add("AND",t,e,n)}},{key:"or",value:function(t,e){for(var r=arguments.length,n=Array(r>2?r-2:0),i=2;r>i;i++)n[i-2]=arguments[i];return this._add("OR",t,e,n)}},{key:"_add",value:function(t,e,r,n){var i=["=","<",">","<=",">=","<>","!=","in","not in","like","not like"],a=void 0;return"string"==typeof e&&"string"==typeof r&&-1!=i.indexOf(r.toLowerCase())?a=this._buildExpression(e,r):(a=e,n.unshift(r)),this._sanitizeExpression(e),this._nodes.push({type:t,expr:a,para:n}),this}},{key:"_buildExpression",value:function(t,e){var r=this._formatFieldName(t),n=p.DefaultQueryBuilderOptions.parameterCharacter,i=r+" "+e+" "+n;return i}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r=[],n=[],i=!0,a=!1,o=void 0;try{for(var l,u=this._nodes[Symbol.iterator]();!(i=(l=u.next()).done);i=!0){var s=l.value,f=s.type,c=s.expr,h=s.para,v=c instanceof p.Expression?c._toParamString({buildParameterized:e.buildParameterized,nested:!0}):this._buildString(c,h,{buildParameterized:e.buildParameterized}),d=v.text,y=v.values;r.length&&r.push(f),r.push(d),n.push.apply(n,t(y))}}catch(g){a=!0,o=g}finally{try{!i&&u["return"]&&u["return"]()}finally{if(a)throw o}}return r=r.join(" "),{text:this._applyNestingFormatting(r,!!e.nested),values:n}}}]),a}(p.BaseBuilder),p.Case=function(l){function u(t){var r=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];n(this,u);var i=e(this,Object.getPrototypeOf(u).call(this,r));return o(t)&&(r=t,t=null),t&&(i._fieldName=i._sanitizeField(t)),i.options=a({},p.DefaultQueryBuilderOptions,r),i._cases=[],i._elseValue=null,i}return r(u,l),d(u,[{key:"when",value:function(t){for(var e=arguments.length,r=Array(e>1?e-1:0),n=1;e>n;n++)r[n-1]=arguments[n];return this._cases.unshift({expression:t,values:r}),this}},{key:"then",value:function(t){if(0==this._cases.length)throw new Error("when() needs to be called first");return this._cases[0].result=t,this}},{key:"else",value:function(t){return this._elseValue=t,this}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r="",n=[],a=!0,o=!1,l=void 0;try{for(var u,s=this._cases[Symbol.iterator]();!(a=(u=s.next()).done);a=!0){var f=u.value,c=f.expression,h=f.values,v=f.result;r=i(r," ");var d=this._buildString(c,h,{buildParameterized:e.buildParameterized,nested:!0});r+="WHEN "+d.text+" THEN "+this._formatValueForQueryString(v),n.push.apply(n,t(d.values))}}catch(y){o=!0,l=y}finally{try{!a&&s["return"]&&s["return"]()}finally{if(o)throw l}}return r.length?(r+=" ELSE "+this._formatValueForQueryString(this._elseValue)+" END",this._fieldName&&(r=this._fieldName+" "+r),r="CASE "+r):r=this._formatValueForQueryString(this._elseValue),{text:r,values:n}}}]),u}(p.BaseBuilder),p.Block=function(t){function i(t){return n(this,i),e(this,Object.getPrototypeOf(i).call(this,t))}return r(i,t),d(i,[{key:"exposedMethods",value:function(){for(var t={},e=this;e;)Object.getOwnPropertyNames(e).forEach(function(r){"constructor"===r||"function"!=typeof e[r]||"_"===r.charAt(0)||p.Block.prototype[r]||(t[r]=e[r])}),e=Object.getPrototypeOf(e);return t}}]),i}(p.BaseBuilder),p.StringBlock=function(t){function i(t,r){n(this,i);var a=e(this,Object.getPrototypeOf(i).call(this,t));return a._str=r,a}return r(i,t),d(i,[{key:"_toParamString",value:function(){arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return{text:this._str,values:[]}}}]),i}(p.Block),p.FunctionBlock=function(t){function i(t){n(this,i);var r=e(this,Object.getPrototypeOf(i).call(this,t));return r._strings=[],r._values=[],r}return r(i,t),d(i,[{key:"function",value:function(t){this._strings.push(t);for(var e=arguments.length,r=Array(e>1?e-1:0),n=1;e>n;n++)r[n-1]=arguments[n];this._values.push(r)}},{key:"_toParamString",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return this._buildManyStrings(this._strings,this._values,t)}}]),i}(p.Block),p.registerValueHandler(p.FunctionBlock,function(t){var e=arguments.length<=1||void 0===arguments[1]?!1:arguments[1];return e?t.toParam():t.toString()}),p.AbstractTableBlock=function(a){function o(t,r){n(this,o);var i=e(this,Object.getPrototypeOf(o).call(this,t));return i._tables=[],i}return r(o,a),d(o,[{key:"_table",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1];e=e?this._sanitizeTableAlias(e):e,t=this._sanitizeTable(t),this.options.singleTable&&(this._tables=[]),this._tables.push({table:t,alias:e})}},{key:"_hasTable",value:function(){return 01)throw new Error("Cannot set multiple rows of fields this way.");"undefined"!=typeof e&&(e=this._sanitizeValue(e)),t=this._sanitizeField(t);var n=this._fields.indexOf(t);-1===n&&(this._fields.push(t),n=this._fields.length-1),this._values[0][n]=e,this._valueOptions[0][n]=r}},{key:"_setFields",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];if("object"!==("undefined"==typeof t?"undefined":y(t)))throw new Error("Expected an object but got "+("undefined"==typeof t?"undefined":y(t)));for(var r in t)this._set(r,t[r],e)}},{key:"_setFieldsRows",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];if(!l(t))throw new Error("Expected an array of objects but got "+("undefined"==typeof t?"undefined":y(t)));this._reset();for(var r in t){var n=t[r];for(var i in n){var a=n[i];i=this._sanitizeField(i),a=this._sanitizeValue(a);var o=this._fields.indexOf(i);if(r>0&&-1===o)throw new Error("All fields in subsequent rows must match the fields in the first row");-1===o&&(this._fields.push(i),o=this._fields.length-1),l(this._values[r])||(this._values[r]=[],this._valueOptions[r]=[]),this._values[r][o]=a,this._valueOptions[r][o]=e}}}}]),i}(p.Block),p.SetFieldBlock=function(a){function o(){return n(this,o),e(this,Object.getPrototypeOf(o).apply(this,arguments))}return r(o,a),d(o,[{key:"set",value:function(t,e,r){this._set(t,e,r)}},{key:"setFields",value:function(t,e){this._setFields(t,e)}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r=e.buildParameterized;if(0>=this._fields.length)throw new Error("set() needs to be called");var n="",a=[];for(var o in this._fields){n=i(n,", ");var l=this._fields[o],u=this._values[0][o];if("undefined"==typeof u)n+=l;else{var s=this._buildString(l+" = "+this.options.parameterCharacter,[u],{buildParameterized:r,formattingOptions:this._valueOptions[0][o]});n+=s.text,a.push.apply(a,t(s.values))}}return{text:"SET "+n,values:a}}}]),o}(p.AbstractSetFieldBlock),p.InsertFieldValueBlock=function(a){function o(){return n(this,o),e(this,Object.getPrototypeOf(o).apply(this,arguments))}return r(o,a),d(o,[{key:"set",value:function(t,e){var r=arguments.length<=2||void 0===arguments[2]?{}:arguments[2];this._set(t,e,r)}},{key:"setFields",value:function(t,e){this._setFields(t,e)}},{key:"setFieldsRows",value:function(t,e){this._setFieldsRows(t,e)}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r=e.buildParameterized,n=this._fields.join(", "),a=[],o=[];for(var l in this._values){a[l]="";for(var u in this._values[l]){var s=this._buildString(this.options.parameterCharacter,[this._values[l][u]],{buildParameterized:r,formattingOptions:this._valueOptions[l][u]});o.push.apply(o,t(s.values)),a[l]=i(a[l],", "),a[l]+=s.text}}return{text:n.length?"("+n+") VALUES ("+a.join("), (")+")":"",values:o}}}]),o}(p.AbstractSetFieldBlock),p.InsertFieldsFromQueryBlock=function(t){function i(t){n(this,i);var r=e(this,Object.getPrototypeOf(i).call(this,t));return r._fields=[],r._query=null,r}return r(i,t),d(i,[{key:"fromQuery",value:function(t,e){var r=this;this._fields=t.map(function(t){return r._sanitizeField(t)}),this._query=this._sanitizeQueryBuilder(e)}},{key:"_toParamString",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],e="",r=[];if(this._fields.length&&this._query){var n=this._query._toParamString({buildParameterized:t.buildParameterized,nested:!0}),i=n.text,a=n.values;e="("+this._fields.join(", ")+") ("+i+")",r=a}return{text:e,values:r}}}]),i}(p.Block),p.DistinctBlock=function(t){function i(){return n(this,i),e(this,Object.getPrototypeOf(i).apply(this,arguments))}return r(i,t),d(i,[{key:"distinct",value:function(){this._useDistinct=!0}},{key:"_toParamString",value:function(){return{text:this._useDistinct?"DISTINCT":"",values:[]}}}]),i}(p.Block),p.GroupByBlock=function(t){function i(t){n(this,i);var r=e(this,Object.getPrototypeOf(i).call(this,t));return r._groups=[],r}return r(i,t),d(i,[{key:"group",value:function(t){this._groups.push(this._sanitizeField(t))}},{key:"_toParamString",value:function(){arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return{text:this._groups.length?"GROUP BY "+this._groups.join(", "):"",values:[]}}}]),i}(p.Block),p.OffsetBlock=function(t){function i(t){n(this,i);var r=e(this,Object.getPrototypeOf(i).call(this,t));return r._offsets=null,r}return r(i,t),d(i,[{key:"offset",value:function(t){this._offsets=this._sanitizeLimitOffset(t)}},{key:"_toParamString",value:function(){return{text:this._offsets?"OFFSET "+this._offsets:"",values:[]}}}]),i}(p.Block),p.AbstractConditionBlock=function(a){function o(t){n(this,o);var r=e(this,Object.getPrototypeOf(o).call(this,t));return r._conditions=[],r}return r(o,a),d(o,[{key:"_condition",value:function(t){for(var e=arguments.length,r=Array(e>1?e-1:0),n=1;e>n;n++)r[n-1]=arguments[n];t=this._sanitizeExpression(t),this._conditions.push({expr:t,values:r})}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r="",n=[],a=!0,o=!1,l=void 0;try{for(var u,s=this._conditions[Symbol.iterator]();!(a=(u=s.next()).done);a=!0){var f=u.value,c=f.expr,h=f.values;r=i(r,") AND (");var v=c instanceof p.Expression?c._toParamString({buildParameterized:e.buildParameterized}):this._buildString(c,h,{buildParameterized:e.buildParameterized});r+=v.text,n.push.apply(n,t(v.values))}}catch(d){o=!0,l=d}finally{try{!a&&s["return"]&&s["return"]()}finally{if(o)throw l}}return{text:r.length?this.options.verb+" ("+r+")":"",values:n}}}]),o}(p.Block),p.WhereBlock=function(t){function i(t){return n(this,i),e(this,Object.getPrototypeOf(i).call(this,a({},t,{verb:"WHERE"})))}return r(i,t),d(i,[{key:"where",value:function(t,e){for(var r=arguments.length,n=Array(r>2?r-2:0),i=2;r>i;i++)n[i-2]=arguments[i];var a=["=","<",">","<=",">=","<>","!=","in","not in","like","not like"];if("string"==typeof t&&"string"==typeof e&&-1!=a.indexOf(e.toLowerCase())){var o=new p.Expression;this._condition(o._add("AND",t,e,n))}else this._condition.apply(this,arguments)}}]),i}(p.AbstractConditionBlock),p.HavingBlock=function(t){function i(t){return n(this,i),e(this,Object.getPrototypeOf(i).call(this,a({},t,{verb:"HAVING"})))}return r(i,t),d(i,[{key:"having",value:function(t,e){for(var r=arguments.length,n=Array(r>2?r-2:0),i=2;r>i;i++)n[i-2]=arguments[i];var a=["=","<",">","<=",">=","<>","!=","in","not in","like","not like"];if("string"==typeof t&&"string"==typeof e&&-1!=a.indexOf(e.toLowerCase())){var o=new p.Expression;this._condition(o._add("AND",t,e,n))}else this._condition.apply(this,arguments)}}]),i}(p.AbstractConditionBlock),p.OrderByBlock=function(a){function o(t){n(this,o);var r=e(this,Object.getPrototypeOf(o).call(this,t));return r._orders=[],r}return r(o,a),d(o,[{key:"order",value:function(t,e){for(var r=arguments.length,n=Array(r>2?r-2:0),i=2;r>i;i++)n[i-2]=arguments[i];t=this._sanitizeField(t),e=void 0===e?!0:e,e=null!==e?!!e:e,this._orders.push({field:t,dir:e,values:n})}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r="",n=[],a=!0,o=!1,l=void 0;try{for(var u,s=this._orders[Symbol.iterator]();!(a=(u=s.next()).done);a=!0){var f=u.value,c=f.field,h=f.dir,v=f.values;r=i(r,", ");var d=this._buildString(c,v,{buildParameterized:e.buildParameterized});r+=d.text,n.push.apply(n,t(d.values)),null!==h&&(r+=" "+(h?"ASC":"DESC"))}}catch(y){o=!0,l=y}finally{try{!a&&s["return"]&&s["return"]()}finally{if(o)throw l}}return{text:r.length?"ORDER BY "+r:"",values:n}}}]),o}(p.Block),p.LimitBlock=function(t){function i(t){n(this,i);var r=e(this,Object.getPrototypeOf(i).call(this,t));return r._limit=null,r}return r(i,t),d(i,[{key:"limit",value:function(t){this._limit=this._sanitizeLimitOffset(t)}},{key:"_toParamString",value:function(){return{text:null!==this._limit?"LIMIT "+this._limit:"",values:[]}}}]),i}(p.Block),p.JoinBlock=function(a){function o(t){n(this,o);var r=e(this,Object.getPrototypeOf(o).call(this,t));return r._joins=[],r}return r(o,a),d(o,[{key:"join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],r=arguments.length<=2||void 0===arguments[2]?null:arguments[2],n=arguments.length<=3||void 0===arguments[3]?"INNER":arguments[3];t=this._sanitizeTable(t,!0),e=e?this._sanitizeTableAlias(e):e,r=r?this._sanitizeExpression(r):r,this._joins.push({type:n,table:t,alias:e,condition:r})}},{key:"left_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],r=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,r,"LEFT")}},{key:"right_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],r=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,r,"RIGHT")}},{key:"outer_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],r=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,r,"OUTER")}},{key:"left_outer_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],r=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,r,"LEFT OUTER")}},{key:"full_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],r=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,r,"FULL")}},{key:"cross_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],r=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,r,"CROSS")}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r="",n=[],a=!0,o=!1,l=void 0;try{for(var u,s=this._joins[Symbol.iterator]();!(a=(u=s.next()).done);a=!0){var f=u.value,c=f.type,h=f.table,v=f.alias,d=f.condition;r=i(r,this.options.separator);var y=void 0;if(h instanceof p.BaseBuilder){var g=h._toParamString({buildParameterized:e.buildParameterized,nested:!0});n.push.apply(n,t(g.values)),y=g.text}else y=this._formatTableName(h);if(r+=c+" JOIN "+y,v&&(r+=" "+this._formatTableAlias(v)),d){r+=" ON ";var _=void 0;_=d instanceof p.Expression?d._toParamString({buildParameterized:e.buildParameterized}):this._buildString(d,[],{buildParameterized:e.buildParameterized}),r+=this._applyNestingFormatting(_.text),n.push.apply(n,t(_.values))}}}catch(m){o=!0,l=m}finally{try{!a&&s["return"]&&s["return"]()}finally{if(o)throw l}}return{text:r,values:n}}}]),o}(p.Block),p.UnionBlock=function(a){function o(t){n(this,o);var r=e(this,Object.getPrototypeOf(o).call(this,t));return r._unions=[],r}return r(o,a),d(o,[{key:"union",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?"UNION":arguments[1];t=this._sanitizeTable(t),this._unions.push({type:e,table:t})}},{key:"union_all",value:function(t){this.union(t,"UNION ALL")}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r="",n=[],a=!0,o=!1,l=void 0;try{for(var u,s=this._unions[Symbol.iterator]();!(a=(u=s.next()).done);a=!0){var f=u.value,c=f.type,h=f.table;r=i(r,this.options.separator);var v=void 0;if(h instanceof p.BaseBuilder){var d=h._toParamString({buildParameterized:e.buildParameterized,nested:!0});v=d.text,n.push.apply(n,t(d.values))}else r=this._formatTableName(h);r+=c+" "+v}}catch(y){o=!0,l=y}finally{try{!a&&s["return"]&&s["return"]()}finally{if(o)throw l}}return{text:r,values:n}}}]),o}(p.Block),p.QueryBuilder=function(i){function o(t,r){n(this,o);var i=e(this,Object.getPrototypeOf(o).call(this,t));i.blocks=r||[];var a=!0,l=!1,u=void 0;try{for(var s,f=i.blocks[Symbol.iterator]();!(a=(s=f.next()).done);a=!0){var c=s.value,h=c.exposedMethods();for(var v in h){var d=h[v];if(void 0!==i[v])throw new Error("Builder already has a builder method called: "+v);!function(t,e,r){i[e]=function(){for(var e=arguments.length,n=Array(e),a=0;e>a;a++)n[a]=arguments[a];return r.call.apply(r,[t].concat(n)),i}}(c,v,d)}}}catch(y){l=!0,u=y}finally{try{!a&&f["return"]&&f["return"]()}finally{if(l)throw u}}return i}return r(o,i),d(o,[{key:"registerValueHandler",value:function(t,e){var r=!0,n=!1,i=void 0;try{for(var a,l=this.blocks[Symbol.iterator]();!(r=(a=l.next()).done);r=!0){var u=a.value;u.registerValueHandler(t,e)}}catch(s){n=!0,i=s}finally{try{!r&&l["return"]&&l["return"]()}finally{if(n)throw i}}return v(Object.getPrototypeOf(o.prototype),"registerValueHandler",this).call(this,t,e),this}},{key:"updateOptions",value:function(t){this.options=a({},this.options,t);var e=!0,r=!1,n=void 0;try{for(var i,o=this.blocks[Symbol.iterator]();!(e=(i=o.next()).done);e=!0){var l=i.value;l.options=a({},l.options,t)}}catch(u){r=!0,n=u}finally{try{!e&&o["return"]&&o["return"]()}finally{if(r)throw n}}}},{key:"_toParamString",value:function(){var e,r=this,n=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];n=a({},this.options,n);var i=this.blocks.map(function(t){return t._toParamString({buildParameterized:n.buildParameterized,queryBuilder:r})}),o=i.map(function(t){return t.text}),l=i.map(function(t){return t.values}),u=o.filter(function(t){return 01?e-1:0),n=1;e>n;n++)r[n-1]=arguments[n];if(t&&r){var i=!0,a=!1,o=void 0;try{for(var l,u=function(){var e=l.value;"object"===("undefined"==typeof e?"undefined":y(e))&&Object.getOwnPropertyNames(e).forEach(function(r){"function"!=typeof e[r]&&(t[r]=e[r])})},s=r[Symbol.iterator]();!(i=(l=s.next()).done);i=!0)u()}catch(f){a=!0,o=f}finally{try{!i&&s["return"]&&s["return"]()}finally{if(a)throw o}}}return t}function o(t){return t&&t.constructor.prototype===Object.prototype}function l(t){return t&&t.constructor.prototype===Array.prototype}function u(t){if(t&&t.constructor&&t.constructor.toString){var e=t.constructor.toString().match(/function\s*(\w+)/);if(e&&2===e.length)return e[1]}}function s(t){if(!t)return t;if("function"==typeof t.clone)return t.clone();if(!o(t)&&!l(t))return JSON.parse(JSON.stringify(t));var e=function(){var e=new t.constructor;return Object.getOwnPropertyNames(t).forEach(function(r){"function"!=typeof t[r]&&(e[r]=s(t[r]))}),{v:e}}();return"object"===("undefined"==typeof e?"undefined":y(e))?e.v:void 0}function f(t,e,r){var n="undefined"==typeof e?"undefined":y(e);if("function"!==n&&"string"!==n)throw new Error("type must be a class constructor or string");if("function"!=typeof r)throw new Error("handler must be a function");for(var i in t){var a=t[i];if(a.type===e)return void(a.handler=r)}t.push({type:e,handler:r})}function c(t){for(var e=arguments.length,r=Array(e>1?e-1:0),n=1;e>n;n++)r[n-1]=arguments[n];for(var i in r){var a=r[i];for(var o in a){var l=a[o];if(("undefined"==typeof t?"undefined":y(t))===l.type||"string"!=typeof l.type&&t instanceof l.type)return l.handler}}}function h(){var h=arguments.length<=0||void 0===arguments[0]?null:arguments[0],p={_getObjectClassName:u};p.DefaultQueryBuilderOptions={autoQuoteTableNames:!1,autoQuoteFieldNames:!1,autoQuoteAliasNames:!0,useAsForTableAliasNames:!1,nameQuoteCharacter:"`",tableAliasQuoteCharacter:"`",fieldAliasQuoteCharacter:'"',valueHandlers:[],parameterCharacter:"?",numberedParameters:!1,numberedParametersPrefix:"$",numberedParametersStartAt:1,replaceSingleQuotes:!1,singleQuoteReplacement:"''",separator:" "},p.globalValueHandlers=[],p.registerValueHandler=function(t,e){f(p.globalValueHandlers,t,e)},p.Cloneable=function(){function t(){n(this,t)}return d(t,[{key:"clone",value:function(){var t=new this.constructor;return a(t,s(a({},this)))}}]),t}(),p.BaseBuilder=function(i){function o(t){n(this,o);var r=e(this,Object.getPrototypeOf(o).call(this)),i=JSON.parse(JSON.stringify(p.DefaultQueryBuilderOptions));return r.options=a({},i,t),r}return r(o,i),d(o,[{key:"registerValueHandler",value:function(t,e){return f(this.options.valueHandlers,t,e),this}},{key:"_sanitizeExpression",value:function(t){if(!(t instanceof p.Expression)&&"string"!=typeof t)throw new Error("expression must be a string or Expression instance");return t}},{key:"_sanitizeName",value:function(t,e){if("string"!=typeof t)throw new Error(e+" must be a string");return t}},{key:"_sanitizeField",value:function(t){return t instanceof p.BaseBuilder||(t=this._sanitizeName(t,"field name")),t}},{key:"_sanitizeQueryBuilder",value:function(t){if(t instanceof p.QueryBuilder)return t;throw new Error("must be a QueryBuilder instance")}},{key:"_sanitizeTable",value:function(t){if("string"!=typeof t)try{t=this._sanitizeQueryBuilder(t)}catch(e){throw new Error("table name must be a string or a query builder")}else t=this._sanitizeName(t,"table");return t}},{key:"_sanitizeTableAlias",value:function(t){return this._sanitizeName(t,"table alias")}},{key:"_sanitizeFieldAlias",value:function(t){return this._sanitizeName(t,"field alias")}},{key:"_sanitizeLimitOffset",value:function(t){if(t=parseInt(t),0>t||isNaN(t))throw new Error("limit/offset must be >= 0");return t}},{key:"_sanitizeValue",value:function(t){var e="undefined"==typeof t?"undefined":y(t);if(null===t);else if("string"===e||"number"===e||"boolean"===e);else if(t instanceof p.BaseBuilder);else{var r=!!c(t,this.options.valueHandlers,p.globalValueHandlers);if(!r)throw new Error("field value must be a string, number, boolean, null or one of the registered custom value types")}return t}},{key:"_escapeValue",value:function(t){return this.options.replaceSingleQuotes?t.replace(/\'/g,this.options.singleQuoteReplacement):t}},{key:"_formatTableName",value:function(t){if(this.options.autoQuoteTableNames){var e=this.options.nameQuoteCharacter;t=""+e+t+e}return t}},{key:"_formatFieldAlias",value:function(t){if(this.options.autoQuoteAliasNames){var e=this.options.fieldAliasQuoteCharacter;t=""+e+t+e}return t}},{key:"_formatTableAlias",value:function(t){if(this.options.autoQuoteAliasNames){var e=this.options.tableAliasQuoteCharacter;t=""+e+t+e}return this.options.useAsForTableAliasNames?"AS "+t:t}},{key:"_formatFieldName",value:function(t){var e=this,r=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];return this.options.autoQuoteFieldNames&&!function(){var n=e.options.nameQuoteCharacter;t=r.ignorePeriodsForFieldNameQuotes?""+n+t+n:t.split(".").map(function(t){return"*"===t?t:""+n+t+n}).join(".")}(),t}},{key:"_formatCustomValue",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?!1:arguments[1],r=c(t,this.options.valueHandlers,p.globalValueHandlers);return r&&(t=r(t,e)),t}},{key:"_formatValueForParamArray",value:function(t){var e=this;return l(t)?t.map(function(t){return e._formatValueForParamArray(t)}):this._formatCustomValue(t,!0)}},{key:"_formatValueForQueryString",value:function(t){var e=this,r=arguments.length<=1||void 0===arguments[1]?{}:arguments[1],n=this._formatCustomValue(t);if(n!==t)return this._applyNestingFormatting(n);if(l(t))t=t.map(function(t){return e._formatValueForQueryString(t)}),t=this._applyNestingFormatting(t.join(", "));else{var i="undefined"==typeof t?"undefined":y(t);if(null===t)t="NULL";else if("boolean"===i)t=t?"TRUE":"FALSE";else if(t instanceof p.BaseBuilder)t=this._applyNestingFormatting(t.toString());else if("number"!==i)if(r.dontQuote)t=""+t;else{var a=this._escapeValue(t);t="'"+a+"'"}}return t}},{key:"_applyNestingFormatting",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?!0:arguments[1];return t&&"string"==typeof t&&e&&("("!==t.charAt(0)||")"!==t.charAt(t.length-1))?"("+t+")":t}},{key:"_buildString",value:function(e,r){var n=arguments.length<=2||void 0===arguments[2]?{}:arguments[2],i=n.nested,a=n.buildParameterized,o=n.formattingOptions;r=r||[],e=e||"";for(var u="",s=-1,f=[],c=this.options.parameterCharacter,h=0;e.length>h;)if(e.substr(h,c.length)===c){var v=r[++s];if(a)if(v instanceof p.BaseBuilder){var d=v._toParamString({buildParameterized:a,nested:!0});u+=d.text,f.push.apply(f,t(d.values))}else if(v=this._formatValueForParamArray(v),l(v)){var y=v.map(function(){return c}).join(", ");u+="("+y+")",f.push.apply(f,t(v))}else u+=c,f.push(v);else u+=this._formatValueForQueryString(v,o);h+=c.length}else u+=e.charAt(h),h++;return{text:this._applyNestingFormatting(u,!!i),values:f}}},{key:"_buildManyStrings",value:function(e,r){var n=arguments.length<=2||void 0===arguments[2]?{}:arguments[2],i=[],a=[];for(var o in e){var l=e[o],u=r[o],s=this._buildString(l,u,{buildParameterized:n.buildParameterized,nested:!1}),f=s.text,c=s.values;i.push(f),a.push.apply(a,t(c))}return i=i.join(this.options.separator),{text:i.length?this._applyNestingFormatting(i,!!n.nested):"",values:a}}},{key:"_toParamString",value:function(t){throw new Error("Not yet implemented")}},{key:"toString",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return this._toParamString(t).text}},{key:"toParam",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return this._toParamString(a({},t,{buildParameterized:!0}))}}]),o}(p.Cloneable),p.Expression=function(i){function a(t){n(this,a);var r=e(this,Object.getPrototypeOf(a).call(this,t));return r._nodes=[],r}return r(a,i),d(a,[{key:"and",value:function(t,e){for(var r=arguments.length,n=Array(r>2?r-2:0),i=2;r>i;i++)n[i-2]=arguments[i];return this._add("AND",t,e,n)}},{key:"or",value:function(t,e){for(var r=arguments.length,n=Array(r>2?r-2:0),i=2;r>i;i++)n[i-2]=arguments[i];return this._add("OR",t,e,n)}},{key:"_add",value:function(t,e,r,n){var i=["=","<",">","<=",">=","<>","!=","in","not in","like","not like"],a=void 0;return"string"==typeof e&&"string"==typeof r&&-1!=i.indexOf(r.toLowerCase())?a=this._buildExpression(e,r):(a=e,n.unshift(r)),this._sanitizeExpression(e),this._nodes.push({type:t,expr:a,para:n}),this}},{key:"_buildExpression",value:function(t,e){var r=this._formatFieldName(t),n=this.options.parameterCharacter,i=r+" "+e+" "+n;return i}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r=[],n=[],i=!0,a=!1,o=void 0;try{for(var l,u=this._nodes[Symbol.iterator]();!(i=(l=u.next()).done);i=!0){var s=l.value,f=s.type,c=s.expr,h=s.para,v=c instanceof p.Expression?c._toParamString({buildParameterized:e.buildParameterized,nested:!0}):this._buildString(c,h,{buildParameterized:e.buildParameterized}),d=v.text,y=v.values;r.length&&r.push(f),r.push(d),n.push.apply(n,t(y))}}catch(g){a=!0,o=g}finally{try{!i&&u["return"]&&u["return"]()}finally{if(a)throw o}}return r=r.join(" "),{text:this._applyNestingFormatting(r,!!e.nested),values:n}}}]),a}(p.BaseBuilder),p.Case=function(l){function u(t){var r=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];n(this,u);var i=e(this,Object.getPrototypeOf(u).call(this,r));return o(t)&&(r=t,t=null),t&&(i._fieldName=i._sanitizeField(t)),i.options=a({},p.DefaultQueryBuilderOptions,r),i._cases=[],i._elseValue=null,i}return r(u,l),d(u,[{key:"when",value:function(t){for(var e=arguments.length,r=Array(e>1?e-1:0),n=1;e>n;n++)r[n-1]=arguments[n];return this._cases.unshift({expression:t,values:r}),this}},{key:"then",value:function(t){if(0==this._cases.length)throw new Error("when() needs to be called first");return this._cases[0].result=t,this}},{key:"else",value:function(t){return this._elseValue=t,this}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r="",n=[],a=!0,o=!1,l=void 0;try{for(var u,s=this._cases[Symbol.iterator]();!(a=(u=s.next()).done);a=!0){var f=u.value,c=f.expression,h=f.values,v=f.result;r=i(r," ");var d=this._buildString(c,h,{buildParameterized:e.buildParameterized,nested:!0});r+="WHEN "+d.text+" THEN "+this._formatValueForQueryString(v),n.push.apply(n,t(d.values))}}catch(y){o=!0,l=y}finally{try{!a&&s["return"]&&s["return"]()}finally{if(o)throw l}}return r.length?(r+=" ELSE "+this._formatValueForQueryString(this._elseValue)+" END",this._fieldName&&(r=this._fieldName+" "+r),r="CASE "+r):r=this._formatValueForQueryString(this._elseValue),{text:r,values:n}}}]),u}(p.BaseBuilder),p.Block=function(t){function i(t){return n(this,i),e(this,Object.getPrototypeOf(i).call(this,t))}return r(i,t),d(i,[{key:"exposedMethods",value:function(){for(var t={},e=this;e;)Object.getOwnPropertyNames(e).forEach(function(r){"constructor"===r||"function"!=typeof e[r]||"_"===r.charAt(0)||p.Block.prototype[r]||(t[r]=e[r])}),e=Object.getPrototypeOf(e);return t}}]),i}(p.BaseBuilder),p.StringBlock=function(t){function i(t,r){n(this,i);var a=e(this,Object.getPrototypeOf(i).call(this,t));return a._str=r,a}return r(i,t),d(i,[{key:"_toParamString",value:function(){arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return{text:this._str,values:[]}}}]),i}(p.Block),p.FunctionBlock=function(t){function i(t){n(this,i);var r=e(this,Object.getPrototypeOf(i).call(this,t));return r._strings=[],r._values=[],r}return r(i,t),d(i,[{key:"function",value:function(t){this._strings.push(t);for(var e=arguments.length,r=Array(e>1?e-1:0),n=1;e>n;n++)r[n-1]=arguments[n];this._values.push(r)}},{key:"_toParamString",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return this._buildManyStrings(this._strings,this._values,t)}}]),i}(p.Block),p.registerValueHandler(p.FunctionBlock,function(t){var e=arguments.length<=1||void 0===arguments[1]?!1:arguments[1];return e?t.toParam():t.toString()}),p.AbstractTableBlock=function(a){function o(t,r){n(this,o);var i=e(this,Object.getPrototypeOf(o).call(this,t));return i._tables=[],i}return r(o,a),d(o,[{key:"_table",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1];e=e?this._sanitizeTableAlias(e):e,t=this._sanitizeTable(t),this.options.singleTable&&(this._tables=[]),this._tables.push({table:t,alias:e})}},{key:"_hasTable",value:function(){return 01)throw new Error("Cannot set multiple rows of fields this way.");"undefined"!=typeof e&&(e=this._sanitizeValue(e)),t=this._sanitizeField(t);var n=this._fields.indexOf(t);-1===n&&(this._fields.push(t),n=this._fields.length-1),this._values[0][n]=e,this._valueOptions[0][n]=r}},{key:"_setFields",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];if("object"!==("undefined"==typeof t?"undefined":y(t)))throw new Error("Expected an object but got "+("undefined"==typeof t?"undefined":y(t)));for(var r in t)this._set(r,t[r],e)}},{key:"_setFieldsRows",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];if(!l(t))throw new Error("Expected an array of objects but got "+("undefined"==typeof t?"undefined":y(t)));this._reset();for(var r in t){var n=t[r];for(var i in n){var a=n[i];i=this._sanitizeField(i),a=this._sanitizeValue(a);var o=this._fields.indexOf(i);if(r>0&&-1===o)throw new Error("All fields in subsequent rows must match the fields in the first row");-1===o&&(this._fields.push(i),o=this._fields.length-1),l(this._values[r])||(this._values[r]=[],this._valueOptions[r]=[]),this._values[r][o]=a,this._valueOptions[r][o]=e}}}}]),i}(p.Block),p.SetFieldBlock=function(a){function o(){return n(this,o),e(this,Object.getPrototypeOf(o).apply(this,arguments))}return r(o,a),d(o,[{key:"set",value:function(t,e,r){this._set(t,e,r)}},{key:"setFields",value:function(t,e){this._setFields(t,e)}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r=e.buildParameterized;if(0>=this._fields.length)throw new Error("set() needs to be called");var n="",a=[];for(var o in this._fields){n=i(n,", ");var l=this._fields[o],u=this._values[0][o];if("undefined"==typeof u)n+=l;else{var s=this._buildString(l+" = "+this.options.parameterCharacter,[u],{buildParameterized:r,formattingOptions:this._valueOptions[0][o]});n+=s.text,a.push.apply(a,t(s.values))}}return{text:"SET "+n,values:a}}}]),o}(p.AbstractSetFieldBlock),p.InsertFieldValueBlock=function(a){function o(){return n(this,o),e(this,Object.getPrototypeOf(o).apply(this,arguments))}return r(o,a),d(o,[{key:"set",value:function(t,e){var r=arguments.length<=2||void 0===arguments[2]?{}:arguments[2];this._set(t,e,r)}},{key:"setFields",value:function(t,e){this._setFields(t,e)}},{key:"setFieldsRows",value:function(t,e){this._setFieldsRows(t,e)}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r=e.buildParameterized,n=this._fields.join(", "),a=[],o=[];for(var l in this._values){a[l]="";for(var u in this._values[l]){var s=this._buildString(this.options.parameterCharacter,[this._values[l][u]],{buildParameterized:r,formattingOptions:this._valueOptions[l][u]});o.push.apply(o,t(s.values)),a[l]=i(a[l],", "),a[l]+=s.text}}return{text:n.length?"("+n+") VALUES ("+a.join("), (")+")":"",values:o}}}]),o}(p.AbstractSetFieldBlock),p.InsertFieldsFromQueryBlock=function(t){function i(t){n(this,i);var r=e(this,Object.getPrototypeOf(i).call(this,t));return r._fields=[],r._query=null,r}return r(i,t),d(i,[{key:"fromQuery",value:function(t,e){var r=this;this._fields=t.map(function(t){return r._sanitizeField(t)}),this._query=this._sanitizeQueryBuilder(e)}},{key:"_toParamString",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],e="",r=[];if(this._fields.length&&this._query){var n=this._query._toParamString({buildParameterized:t.buildParameterized,nested:!0}),i=n.text,a=n.values;e="("+this._fields.join(", ")+") ("+i+")",r=a}return{text:e,values:r}}}]),i}(p.Block),p.DistinctBlock=function(t){function i(){return n(this,i),e(this,Object.getPrototypeOf(i).apply(this,arguments))}return r(i,t),d(i,[{key:"distinct",value:function(){this._useDistinct=!0}},{key:"_toParamString",value:function(){return{text:this._useDistinct?"DISTINCT":"",values:[]}}}]),i}(p.Block),p.GroupByBlock=function(t){function i(t){n(this,i);var r=e(this,Object.getPrototypeOf(i).call(this,t));return r._groups=[],r}return r(i,t),d(i,[{key:"group",value:function(t){this._groups.push(this._sanitizeField(t))}},{key:"_toParamString",value:function(){arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return{text:this._groups.length?"GROUP BY "+this._groups.join(", "):"",values:[]}}}]),i}(p.Block),p.OffsetBlock=function(t){function i(t){n(this,i);var r=e(this,Object.getPrototypeOf(i).call(this,t));return r._offsets=null,r}return r(i,t),d(i,[{key:"offset",value:function(t){this._offsets=this._sanitizeLimitOffset(t)}},{key:"_toParamString",value:function(){return{text:this._offsets?"OFFSET "+this._offsets:"",values:[]}}}]),i}(p.Block),p.AbstractConditionBlock=function(a){function o(t){n(this,o);var r=e(this,Object.getPrototypeOf(o).call(this,t));return r._conditions=[],r}return r(o,a),d(o,[{key:"_condition",value:function(t){for(var e=arguments.length,r=Array(e>1?e-1:0),n=1;e>n;n++)r[n-1]=arguments[n];t=this._sanitizeExpression(t),this._conditions.push({expr:t,values:r})}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r="",n=[],a=!0,o=!1,l=void 0;try{for(var u,s=this._conditions[Symbol.iterator]();!(a=(u=s.next()).done);a=!0){var f=u.value,c=f.expr,h=f.values;r=i(r,") AND (");var v=c instanceof p.Expression?c._toParamString({buildParameterized:e.buildParameterized}):this._buildString(c,h,{buildParameterized:e.buildParameterized});r+=v.text,n.push.apply(n,t(v.values))}}catch(d){o=!0,l=d}finally{try{!a&&s["return"]&&s["return"]()}finally{if(o)throw l}}return{text:r.length?this.options.verb+" ("+r+")":"",values:n}}}]),o}(p.Block),p.WhereBlock=function(t){function i(t){return n(this,i),e(this,Object.getPrototypeOf(i).call(this,a({},t,{verb:"WHERE"})))}return r(i,t),d(i,[{key:"where",value:function(t,e){for(var r=arguments.length,n=Array(r>2?r-2:0),i=2;r>i;i++)n[i-2]=arguments[i];var a=["=","<",">","<=",">=","<>","!=","in","not in","like","not like"];if("string"==typeof t&&"string"==typeof e&&-1!=a.indexOf(e.toLowerCase())){var o=new p.Expression(this.options);this._condition(o._add("AND",t,e,n))}else this._condition.apply(this,arguments)}}]),i}(p.AbstractConditionBlock),p.HavingBlock=function(t){function i(t){return n(this,i),e(this,Object.getPrototypeOf(i).call(this,a({},t,{verb:"HAVING"})))}return r(i,t),d(i,[{key:"having",value:function(t,e){for(var r=arguments.length,n=Array(r>2?r-2:0),i=2;r>i;i++)n[i-2]=arguments[i];var a=["=","<",">","<=",">=","<>","!=","in","not in","like","not like"];if("string"==typeof t&&"string"==typeof e&&-1!=a.indexOf(e.toLowerCase())){var o=new p.Expression(this.options);this._condition(o._add("AND",t,e,n))}else this._condition.apply(this,arguments)}}]),i}(p.AbstractConditionBlock),p.OrderByBlock=function(a){function o(t){n(this,o);var r=e(this,Object.getPrototypeOf(o).call(this,t));return r._orders=[],r}return r(o,a),d(o,[{key:"order",value:function(t,e){for(var r=arguments.length,n=Array(r>2?r-2:0),i=2;r>i;i++)n[i-2]=arguments[i];t=this._sanitizeField(t),e=void 0===e?!0:e,e=null!==e?!!e:e,this._orders.push({field:t,dir:e,values:n})}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r="",n=[],a=!0,o=!1,l=void 0;try{for(var u,s=this._orders[Symbol.iterator]();!(a=(u=s.next()).done);a=!0){var f=u.value,c=f.field,h=f.dir,v=f.values;r=i(r,", ");var d=this._buildString(c,v,{buildParameterized:e.buildParameterized});r+=d.text,n.push.apply(n,t(d.values)),null!==h&&(r+=" "+(h?"ASC":"DESC"))}}catch(y){o=!0,l=y}finally{try{!a&&s["return"]&&s["return"]()}finally{if(o)throw l}}return{text:r.length?"ORDER BY "+r:"",values:n}}}]),o}(p.Block),p.LimitBlock=function(t){function i(t){n(this,i);var r=e(this,Object.getPrototypeOf(i).call(this,t));return r._limit=null,r}return r(i,t),d(i,[{key:"limit",value:function(t){this._limit=this._sanitizeLimitOffset(t)}},{key:"_toParamString",value:function(){return{text:null!==this._limit?"LIMIT "+this._limit:"",values:[]}}}]),i}(p.Block),p.JoinBlock=function(a){function o(t){n(this,o);var r=e(this,Object.getPrototypeOf(o).call(this,t));return r._joins=[],r}return r(o,a),d(o,[{key:"join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],r=arguments.length<=2||void 0===arguments[2]?null:arguments[2],n=arguments.length<=3||void 0===arguments[3]?"INNER":arguments[3];t=this._sanitizeTable(t,!0),e=e?this._sanitizeTableAlias(e):e,r=r?this._sanitizeExpression(r):r,this._joins.push({type:n,table:t,alias:e,condition:r})}},{key:"left_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],r=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,r,"LEFT")}},{key:"right_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],r=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,r,"RIGHT")}},{key:"outer_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],r=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,r,"OUTER")}},{key:"left_outer_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],r=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,r,"LEFT OUTER")}},{key:"full_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],r=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,r,"FULL")}},{key:"cross_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],r=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,r,"CROSS")}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r="",n=[],a=!0,o=!1,l=void 0;try{for(var u,s=this._joins[Symbol.iterator]();!(a=(u=s.next()).done);a=!0){var f=u.value,c=f.type,h=f.table,v=f.alias,d=f.condition;r=i(r,this.options.separator);var y=void 0;if(h instanceof p.BaseBuilder){var g=h._toParamString({buildParameterized:e.buildParameterized,nested:!0});n.push.apply(n,t(g.values)),y=g.text}else y=this._formatTableName(h);if(r+=c+" JOIN "+y,v&&(r+=" "+this._formatTableAlias(v)),d){r+=" ON ";var _=void 0;_=d instanceof p.Expression?d._toParamString({buildParameterized:e.buildParameterized}):this._buildString(d,[],{buildParameterized:e.buildParameterized}),r+=this._applyNestingFormatting(_.text),n.push.apply(n,t(_.values))}}}catch(m){o=!0,l=m}finally{try{!a&&s["return"]&&s["return"]()}finally{if(o)throw l}}return{text:r,values:n}}}]),o}(p.Block),p.UnionBlock=function(a){function o(t){n(this,o);var r=e(this,Object.getPrototypeOf(o).call(this,t));return r._unions=[],r}return r(o,a),d(o,[{key:"union",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?"UNION":arguments[1];t=this._sanitizeTable(t),this._unions.push({type:e,table:t})}},{key:"union_all",value:function(t){this.union(t,"UNION ALL")}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r="",n=[],a=!0,o=!1,l=void 0;try{for(var u,s=this._unions[Symbol.iterator]();!(a=(u=s.next()).done);a=!0){var f=u.value,c=f.type,h=f.table;r=i(r,this.options.separator);var v=void 0;if(h instanceof p.BaseBuilder){var d=h._toParamString({buildParameterized:e.buildParameterized,nested:!0});v=d.text,n.push.apply(n,t(d.values))}else r=this._formatTableName(h);r+=c+" "+v}}catch(y){o=!0,l=y}finally{try{!a&&s["return"]&&s["return"]()}finally{if(o)throw l}}return{text:r,values:n}}}]),o}(p.Block),p.QueryBuilder=function(i){function o(t,r){n(this,o);var i=e(this,Object.getPrototypeOf(o).call(this,t));i.blocks=r||[];var a=!0,l=!1,u=void 0;try{for(var s,f=i.blocks[Symbol.iterator]();!(a=(s=f.next()).done);a=!0){var c=s.value,h=c.exposedMethods();for(var v in h){var d=h[v];if(void 0!==i[v])throw new Error("Builder already has a builder method called: "+v);!function(t,e,r){i[e]=function(){for(var e=arguments.length,n=Array(e),a=0;e>a;a++)n[a]=arguments[a];return r.call.apply(r,[t].concat(n)),i}}(c,v,d)}}}catch(y){l=!0,u=y}finally{try{!a&&f["return"]&&f["return"]()}finally{if(l)throw u}}return i}return r(o,i),d(o,[{key:"registerValueHandler",value:function(t,e){var r=!0,n=!1,i=void 0;try{for(var a,l=this.blocks[Symbol.iterator]();!(r=(a=l.next()).done);r=!0){var u=a.value;u.registerValueHandler(t,e)}}catch(s){n=!0,i=s}finally{try{!r&&l["return"]&&l["return"]()}finally{if(n)throw i}}return v(Object.getPrototypeOf(o.prototype),"registerValueHandler",this).call(this,t,e),this}},{key:"updateOptions",value:function(t){this.options=a({},this.options,t);var e=!0,r=!1,n=void 0;try{for(var i,o=this.blocks[Symbol.iterator]();!(e=(i=o.next()).done);e=!0){var l=i.value;l.options=a({},l.options,t)}}catch(u){r=!0,n=u}finally{try{!e&&o["return"]&&o["return"]()}finally{if(r)throw n}}}},{key:"_toParamString",value:function(){var e,r=this,n=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];n=a({},this.options,n);var i=this.blocks.map(function(t){return t._toParamString({buildParameterized:n.buildParameterized,queryBuilder:r})}),o=i.map(function(t){return t.text}),l=i.map(function(t){return t.values}),u=o.filter(function(t){return 0', '<=', '>=', '<>', '!=', 'in', 'not in', 'like', 'not like']; if (typeof field === 'string' && typeof operator === 'string' && -1 != validOperators.indexOf(operator.toLowerCase())) { // key - operator - value - var expr = new cls.Expression(); + var expr = new cls.Expression(this.options); this._condition(expr._add('AND', field, operator, values)); } else { // default squel-behaviour without auto-quoting @@ -2148,7 +2148,7 @@ function _buildSquel() { var validOperators = ['=', '<', '>', '<=', '>=', '<>', '!=', 'in', 'not in', 'like', 'not like']; if (typeof field === 'string' && typeof operator === 'string' && -1 != validOperators.indexOf(operator.toLowerCase())) { // key - operator - value - var expr = new cls.Expression(); + var expr = new cls.Expression(this.options); this._condition(expr._add('AND', field, operator, values)); } else { // default squel-behaviour without auto-quoting diff --git a/squel.min.js b/squel.min.js index 5f581d49..d7108e9f 100644 --- a/squel.min.js +++ b/squel.min.js @@ -1,2 +1,2 @@ -/*! squel | https://github.com/hiddentao/squel | BSD license */!function(t,e){"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?module.exports=e():t.squel=e()}(this,function(){"use strict";function t(t){if(Array.isArray(t)){for(var e=0,n=Array(t.length);e1?e-1:0),r=1;e>r;r++)n[r-1]=arguments[r];if(t&&n){var i=!0,o=!1,a=void 0;try{for(var l,u=function(){var e=l.value;"object"===("undefined"==typeof e?"undefined":p(e))&&Object.getOwnPropertyNames(e).forEach(function(n){"function"!=typeof e[n]&&(t[n]=e[n])})},s=n[Symbol.iterator]();!(i=(l=s.next()).done);i=!0)u()}catch(c){o=!0,a=c}finally{try{!i&&s["return"]&&s["return"]()}finally{if(o)throw a}}}return t}function a(t){return t&&t.constructor.prototype===Object.prototype}function l(t){return t&&t.constructor.prototype===Array.prototype}function u(t){if(t&&t.constructor&&t.constructor.toString){var e=t.constructor.toString().match(/function\s*(\w+)/);if(e&&2===e.length)return e[1]}}function s(t){if(!t)return t;if("function"==typeof t.clone)return t.clone();if(!a(t)&&!l(t))return JSON.parse(JSON.stringify(t));var e=function(){var e=new t.constructor;return Object.getOwnPropertyNames(t).forEach(function(n){"function"!=typeof t[n]&&(e[n]=s(t[n]))}),{v:e}}();return"object"===("undefined"==typeof e?"undefined":p(e))?e.v:void 0}function c(t,e,n){var r="undefined"==typeof e?"undefined":p(e);if("function"!==r&&"string"!==r)throw new Error("type must be a class constructor or string");if("function"!=typeof n)throw new Error("handler must be a function");for(var i in t){var o=t[i];if(o.type===e)return void(o.handler=n)}t.push({type:e,handler:n})}function f(t){for(var e=arguments.length,n=Array(e>1?e-1:0),r=1;e>r;r++)n[r-1]=arguments[r];for(var i in n){var o=n[i];for(var a in o){var l=o[a];if(("undefined"==typeof t?"undefined":p(t))===l.type||"string"!=typeof l.type&&t instanceof l.type)return l.handler}}}function h(){var h=arguments.length<=0||void 0===arguments[0]?null:arguments[0],y={_getObjectClassName:u};y.DefaultQueryBuilderOptions={autoQuoteTableNames:!1,autoQuoteFieldNames:!1,autoQuoteAliasNames:!0,useAsForTableAliasNames:!1,nameQuoteCharacter:"`",tableAliasQuoteCharacter:"`",fieldAliasQuoteCharacter:'"',valueHandlers:[],parameterCharacter:"?",numberedParameters:!1,numberedParametersPrefix:"$",numberedParametersStartAt:1,replaceSingleQuotes:!1,singleQuoteReplacement:"''",separator:" "},y.globalValueHandlers=[],y.registerValueHandler=function(t,e){c(y.globalValueHandlers,t,e)},y.Cloneable=function(){function t(){r(this,t)}return d(t,[{key:"clone",value:function(){var t=new this.constructor;return o(t,s(o({},this)))}}]),t}(),y.BaseBuilder=function(i){function a(t){r(this,a);var n=e(this,Object.getPrototypeOf(a).call(this)),i=JSON.parse(JSON.stringify(y.DefaultQueryBuilderOptions));return n.options=o({},i,t),n}return n(a,i),d(a,[{key:"registerValueHandler",value:function(t,e){return c(this.options.valueHandlers,t,e),this}},{key:"_sanitizeExpression",value:function(t){if(!(t instanceof y.Expression)&&"string"!=typeof t)throw new Error("expression must be a string or Expression instance");return t}},{key:"_sanitizeName",value:function(t,e){if("string"!=typeof t)throw new Error(e+" must be a string");return t}},{key:"_sanitizeField",value:function(t){return t instanceof y.BaseBuilder||(t=this._sanitizeName(t,"field name")),t}},{key:"_sanitizeQueryBuilder",value:function(t){if(t instanceof y.QueryBuilder)return t;throw new Error("must be a QueryBuilder instance")}},{key:"_sanitizeTable",value:function(t){if("string"!=typeof t)try{t=this._sanitizeQueryBuilder(t)}catch(e){throw new Error("table name must be a string or a query builder")}else t=this._sanitizeName(t,"table");return t}},{key:"_sanitizeTableAlias",value:function(t){return this._sanitizeName(t,"table alias")}},{key:"_sanitizeFieldAlias",value:function(t){return this._sanitizeName(t,"field alias")}},{key:"_sanitizeLimitOffset",value:function(t){if(t=parseInt(t),0>t||isNaN(t))throw new Error("limit/offset must be >= 0");return t}},{key:"_sanitizeValue",value:function(t){var e="undefined"==typeof t?"undefined":p(t);if(null===t);else if("string"===e||"number"===e||"boolean"===e);else if(t instanceof y.BaseBuilder);else{var n=!!f(t,this.options.valueHandlers,y.globalValueHandlers);if(!n)throw new Error("field value must be a string, number, boolean, null or one of the registered custom value types")}return t}},{key:"_escapeValue",value:function(t){return this.options.replaceSingleQuotes?t.replace(/\'/g,this.options.singleQuoteReplacement):t}},{key:"_formatTableName",value:function(t){if(this.options.autoQuoteTableNames){var e=this.options.nameQuoteCharacter;t=""+e+t+e}return t}},{key:"_formatFieldAlias",value:function(t){if(this.options.autoQuoteAliasNames){var e=this.options.fieldAliasQuoteCharacter;t=""+e+t+e}return t}},{key:"_formatTableAlias",value:function(t){if(this.options.autoQuoteAliasNames){var e=this.options.tableAliasQuoteCharacter;t=""+e+t+e}return this.options.useAsForTableAliasNames?"AS "+t:t}},{key:"_formatFieldName",value:function(t){var e=this,n=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];return this.options.autoQuoteFieldNames&&!function(){var r=e.options.nameQuoteCharacter;t=n.ignorePeriodsForFieldNameQuotes?""+r+t+r:t.split(".").map(function(t){return"*"===t?t:""+r+t+r}).join(".")}(),t}},{key:"_formatCustomValue",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?!1:arguments[1],n=f(t,this.options.valueHandlers,y.globalValueHandlers);return n&&(t=n(t,e)),t}},{key:"_formatValueForParamArray",value:function(t){var e=this;return l(t)?t.map(function(t){return e._formatValueForParamArray(t)}):this._formatCustomValue(t,!0)}},{key:"_formatValueForQueryString",value:function(t){var e=this,n=arguments.length<=1||void 0===arguments[1]?{}:arguments[1],r=this._formatCustomValue(t);if(r!==t)return this._applyNestingFormatting(r);if(l(t))t=t.map(function(t){return e._formatValueForQueryString(t)}),t=this._applyNestingFormatting(t.join(", "));else{var i="undefined"==typeof t?"undefined":p(t);if(null===t)t="NULL";else if("boolean"===i)t=t?"TRUE":"FALSE";else if(t instanceof y.BaseBuilder)t=this._applyNestingFormatting(t.toString());else if("number"!==i)if(n.dontQuote)t=""+t;else{var o=this._escapeValue(t);t="'"+o+"'"}}return t}},{key:"_applyNestingFormatting",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?!0:arguments[1];return t&&"string"==typeof t&&e&&("("!==t.charAt(0)||")"!==t.charAt(t.length-1))?"("+t+")":t}},{key:"_buildString",value:function(e,n){var r=arguments.length<=2||void 0===arguments[2]?{}:arguments[2],i=r.nested,o=r.buildParameterized,a=r.formattingOptions;n=n||[],e=e||"";for(var u="",s=-1,c=[],f=this.options.parameterCharacter,h=0;e.length>h;)if(e.substr(h,f.length)===f){var v=n[++s];if(o)if(v instanceof y.BaseBuilder){var d=v._toParamString({buildParameterized:o,nested:!0});u+=d.text,c.push.apply(c,t(d.values))}else if(v=this._formatValueForParamArray(v),l(v)){var p=v.map(function(){return f}).join(", ");u+="("+p+")",c.push.apply(c,t(v))}else u+=f,c.push(v);else u+=this._formatValueForQueryString(v,a);h+=f.length}else u+=e.charAt(h),h++;return{text:this._applyNestingFormatting(u,!!i),values:c}}},{key:"_buildManyStrings",value:function(e,n){var r=arguments.length<=2||void 0===arguments[2]?{}:arguments[2],i=[],o=[];for(var a in e){var l=e[a],u=n[a],s=this._buildString(l,u,{buildParameterized:r.buildParameterized,nested:!1}),c=s.text,f=s.values;i.push(c),o.push.apply(o,t(f))}return i=i.join(this.options.separator),{text:i.length?this._applyNestingFormatting(i,!!r.nested):"",values:o}}},{key:"_toParamString",value:function(t){throw new Error("Not yet implemented")}},{key:"toString",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return this._toParamString(t).text}},{key:"toParam",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return this._toParamString(o({},t,{buildParameterized:!0}))}}]),a}(y.Cloneable),y.Expression=function(i){function o(t){r(this,o);var n=e(this,Object.getPrototypeOf(o).call(this,t));return n._nodes=[],n}return n(o,i),d(o,[{key:"and",value:function(t,e){for(var n=arguments.length,r=Array(n>2?n-2:0),i=2;n>i;i++)r[i-2]=arguments[i];return this._add("AND",t,e,r)}},{key:"or",value:function(t,e){for(var n=arguments.length,r=Array(n>2?n-2:0),i=2;n>i;i++)r[i-2]=arguments[i];return this._add("OR",t,e,r)}},{key:"_add",value:function(t,e,n,r){var i=["=","<",">","<=",">=","<>","!=","in","not in","like","not like"],o=void 0;return"string"==typeof e&&"string"==typeof n&&-1!=i.indexOf(n.toLowerCase())?o=this._buildExpression(e,n):(o=e,r.unshift(n)),this._sanitizeExpression(e),this._nodes.push({type:t,expr:o,para:r}),this}},{key:"_buildExpression",value:function(t,e){var n=this._formatFieldName(t),r=y.DefaultQueryBuilderOptions.parameterCharacter,i=n+" "+e+" "+r;return i}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n=[],r=[],i=!0,o=!1,a=void 0;try{for(var l,u=this._nodes[Symbol.iterator]();!(i=(l=u.next()).done);i=!0){var s=l.value,c=s.type,f=s.expr,h=s.para,v=f instanceof y.Expression?f._toParamString({buildParameterized:e.buildParameterized,nested:!0}):this._buildString(f,h,{buildParameterized:e.buildParameterized}),d=v.text,p=v.values;n.length&&n.push(c),n.push(d),r.push.apply(r,t(p))}}catch(g){o=!0,a=g}finally{try{!i&&u["return"]&&u["return"]()}finally{if(o)throw a}}return n=n.join(" "),{text:this._applyNestingFormatting(n,!!e.nested),values:r}}}]),o}(y.BaseBuilder),y.Case=function(l){function u(t){var n=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];r(this,u);var i=e(this,Object.getPrototypeOf(u).call(this,n));return a(t)&&(n=t,t=null),t&&(i._fieldName=i._sanitizeField(t)),i.options=o({},y.DefaultQueryBuilderOptions,n),i._cases=[],i._elseValue=null,i}return n(u,l),d(u,[{key:"when",value:function(t){for(var e=arguments.length,n=Array(e>1?e-1:0),r=1;e>r;r++)n[r-1]=arguments[r];return this._cases.unshift({expression:t,values:n}),this}},{key:"then",value:function(t){if(0==this._cases.length)throw new Error("when() needs to be called first");return this._cases[0].result=t,this}},{key:"else",value:function(t){return this._elseValue=t,this}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n="",r=[],o=!0,a=!1,l=void 0;try{for(var u,s=this._cases[Symbol.iterator]();!(o=(u=s.next()).done);o=!0){var c=u.value,f=c.expression,h=c.values,v=c.result;n=i(n," ");var d=this._buildString(f,h,{buildParameterized:e.buildParameterized,nested:!0});n+="WHEN "+d.text+" THEN "+this._formatValueForQueryString(v),r.push.apply(r,t(d.values))}}catch(p){a=!0,l=p}finally{try{!o&&s["return"]&&s["return"]()}finally{if(a)throw l}}return n.length?(n+=" ELSE "+this._formatValueForQueryString(this._elseValue)+" END",this._fieldName&&(n=this._fieldName+" "+n),n="CASE "+n):n=this._formatValueForQueryString(this._elseValue),{text:n,values:r}}}]),u}(y.BaseBuilder),y.Block=function(t){function i(t){return r(this,i),e(this,Object.getPrototypeOf(i).call(this,t))}return n(i,t),d(i,[{key:"exposedMethods",value:function(){for(var t={},e=this;e;)Object.getOwnPropertyNames(e).forEach(function(n){"constructor"===n||"function"!=typeof e[n]||"_"===n.charAt(0)||y.Block.prototype[n]||(t[n]=e[n])}),e=Object.getPrototypeOf(e);return t}}]),i}(y.BaseBuilder),y.StringBlock=function(t){function i(t,n){r(this,i);var o=e(this,Object.getPrototypeOf(i).call(this,t));return o._str=n,o}return n(i,t),d(i,[{key:"_toParamString",value:function(){arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return{text:this._str,values:[]}}}]),i}(y.Block),y.FunctionBlock=function(t){function i(t){r(this,i);var n=e(this,Object.getPrototypeOf(i).call(this,t));return n._strings=[],n._values=[],n}return n(i,t),d(i,[{key:"function",value:function(t){this._strings.push(t);for(var e=arguments.length,n=Array(e>1?e-1:0),r=1;e>r;r++)n[r-1]=arguments[r];this._values.push(n)}},{key:"_toParamString",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return this._buildManyStrings(this._strings,this._values,t)}}]),i}(y.Block),y.registerValueHandler(y.FunctionBlock,function(t){var e=arguments.length<=1||void 0===arguments[1]?!1:arguments[1];return e?t.toParam():t.toString()}),y.AbstractTableBlock=function(o){function a(t,n){r(this,a);var i=e(this,Object.getPrototypeOf(a).call(this,t));return i._tables=[],i}return n(a,o),d(a,[{key:"_table",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1];e=e?this._sanitizeTableAlias(e):e,t=this._sanitizeTable(t),this.options.singleTable&&(this._tables=[]),this._tables.push({table:t,alias:e})}},{key:"_hasTable",value:function(){return 01)throw new Error("Cannot set multiple rows of fields this way.");"undefined"!=typeof e&&(e=this._sanitizeValue(e)),t=this._sanitizeField(t);var r=this._fields.indexOf(t);-1===r&&(this._fields.push(t),r=this._fields.length-1),this._values[0][r]=e,this._valueOptions[0][r]=n}},{key:"_setFields",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];if("object"!==("undefined"==typeof t?"undefined":p(t)))throw new Error("Expected an object but got "+("undefined"==typeof t?"undefined":p(t)));for(var n in t)this._set(n,t[n],e)}},{key:"_setFieldsRows",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];if(!l(t))throw new Error("Expected an array of objects but got "+("undefined"==typeof t?"undefined":p(t)));this._reset();for(var n in t){var r=t[n];for(var i in r){var o=r[i];i=this._sanitizeField(i),o=this._sanitizeValue(o);var a=this._fields.indexOf(i);if(n>0&&-1===a)throw new Error("All fields in subsequent rows must match the fields in the first row");-1===a&&(this._fields.push(i),a=this._fields.length-1),l(this._values[n])||(this._values[n]=[],this._valueOptions[n]=[]),this._values[n][a]=o,this._valueOptions[n][a]=e}}}}]),i}(y.Block),y.SetFieldBlock=function(o){function a(){return r(this,a),e(this,Object.getPrototypeOf(a).apply(this,arguments))}return n(a,o),d(a,[{key:"set",value:function(t,e,n){this._set(t,e,n)}},{key:"setFields",value:function(t,e){this._setFields(t,e)}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n=e.buildParameterized;if(0>=this._fields.length)throw new Error("set() needs to be called");var r="",o=[];for(var a in this._fields){r=i(r,", ");var l=this._fields[a],u=this._values[0][a];if("undefined"==typeof u)r+=l;else{var s=this._buildString(l+" = "+this.options.parameterCharacter,[u],{buildParameterized:n,formattingOptions:this._valueOptions[0][a]});r+=s.text,o.push.apply(o,t(s.values))}}return{text:"SET "+r,values:o}}}]),a}(y.AbstractSetFieldBlock),y.InsertFieldValueBlock=function(o){function a(){return r(this,a),e(this,Object.getPrototypeOf(a).apply(this,arguments))}return n(a,o),d(a,[{key:"set",value:function(t,e){var n=arguments.length<=2||void 0===arguments[2]?{}:arguments[2];this._set(t,e,n)}},{key:"setFields",value:function(t,e){this._setFields(t,e)}},{key:"setFieldsRows",value:function(t,e){this._setFieldsRows(t,e)}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n=e.buildParameterized,r=this._fields.join(", "),o=[],a=[];for(var l in this._values){o[l]="";for(var u in this._values[l]){var s=this._buildString(this.options.parameterCharacter,[this._values[l][u]],{buildParameterized:n,formattingOptions:this._valueOptions[l][u]});a.push.apply(a,t(s.values)),o[l]=i(o[l],", "),o[l]+=s.text}}return{text:r.length?"("+r+") VALUES ("+o.join("), (")+")":"",values:a}}}]),a}(y.AbstractSetFieldBlock),y.InsertFieldsFromQueryBlock=function(t){function i(t){r(this,i);var n=e(this,Object.getPrototypeOf(i).call(this,t));return n._fields=[],n._query=null,n}return n(i,t),d(i,[{key:"fromQuery",value:function(t,e){var n=this;this._fields=t.map(function(t){return n._sanitizeField(t)}),this._query=this._sanitizeQueryBuilder(e)}},{key:"_toParamString",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],e="",n=[];if(this._fields.length&&this._query){var r=this._query._toParamString({buildParameterized:t.buildParameterized,nested:!0}),i=r.text,o=r.values;e="("+this._fields.join(", ")+") ("+i+")",n=o}return{text:e,values:n}}}]),i}(y.Block),y.DistinctBlock=function(t){function i(){return r(this,i),e(this,Object.getPrototypeOf(i).apply(this,arguments))}return n(i,t),d(i,[{key:"distinct",value:function(){this._useDistinct=!0}},{key:"_toParamString",value:function(){return{text:this._useDistinct?"DISTINCT":"",values:[]}}}]),i}(y.Block),y.GroupByBlock=function(t){function i(t){r(this,i);var n=e(this,Object.getPrototypeOf(i).call(this,t));return n._groups=[],n}return n(i,t),d(i,[{key:"group",value:function(t){this._groups.push(this._sanitizeField(t))}},{key:"_toParamString",value:function(){arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return{text:this._groups.length?"GROUP BY "+this._groups.join(", "):"",values:[]}}}]),i}(y.Block),y.OffsetBlock=function(t){function i(t){r(this,i);var n=e(this,Object.getPrototypeOf(i).call(this,t));return n._offsets=null,n}return n(i,t),d(i,[{key:"offset",value:function(t){this._offsets=this._sanitizeLimitOffset(t)}},{key:"_toParamString",value:function(){return{text:this._offsets?"OFFSET "+this._offsets:"",values:[]}}}]),i}(y.Block),y.AbstractConditionBlock=function(o){function a(t){r(this,a);var n=e(this,Object.getPrototypeOf(a).call(this,t));return n._conditions=[],n}return n(a,o),d(a,[{key:"_condition",value:function(t){for(var e=arguments.length,n=Array(e>1?e-1:0),r=1;e>r;r++)n[r-1]=arguments[r];t=this._sanitizeExpression(t),this._conditions.push({expr:t,values:n})}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n="",r=[],o=!0,a=!1,l=void 0;try{for(var u,s=this._conditions[Symbol.iterator]();!(o=(u=s.next()).done);o=!0){var c=u.value,f=c.expr,h=c.values;n=i(n,") AND (");var v=f instanceof y.Expression?f._toParamString({buildParameterized:e.buildParameterized}):this._buildString(f,h,{buildParameterized:e.buildParameterized});n+=v.text,r.push.apply(r,t(v.values))}}catch(d){a=!0,l=d}finally{try{!o&&s["return"]&&s["return"]()}finally{if(a)throw l}}return{text:n.length?this.options.verb+" ("+n+")":"",values:r}}}]),a}(y.Block),y.WhereBlock=function(t){function i(t){return r(this,i),e(this,Object.getPrototypeOf(i).call(this,o({},t,{verb:"WHERE"})))}return n(i,t),d(i,[{key:"where",value:function(t,e){for(var n=arguments.length,r=Array(n>2?n-2:0),i=2;n>i;i++)r[i-2]=arguments[i];var o=["=","<",">","<=",">=","<>","!=","in","not in","like","not like"];if("string"==typeof t&&"string"==typeof e&&-1!=o.indexOf(e.toLowerCase())){var a=new y.Expression;this._condition(a._add("AND",t,e,r))}else this._condition.apply(this,arguments)}}]),i}(y.AbstractConditionBlock),y.HavingBlock=function(t){function i(t){return r(this,i),e(this,Object.getPrototypeOf(i).call(this,o({},t,{verb:"HAVING"})))}return n(i,t),d(i,[{key:"having",value:function(t,e){for(var n=arguments.length,r=Array(n>2?n-2:0),i=2;n>i;i++)r[i-2]=arguments[i];var o=["=","<",">","<=",">=","<>","!=","in","not in","like","not like"];if("string"==typeof t&&"string"==typeof e&&-1!=o.indexOf(e.toLowerCase())){var a=new y.Expression;this._condition(a._add("AND",t,e,r))}else this._condition.apply(this,arguments)}}]),i}(y.AbstractConditionBlock),y.OrderByBlock=function(o){function a(t){r(this,a);var n=e(this,Object.getPrototypeOf(a).call(this,t));return n._orders=[],n}return n(a,o),d(a,[{key:"order",value:function(t,e){for(var n=arguments.length,r=Array(n>2?n-2:0),i=2;n>i;i++)r[i-2]=arguments[i];t=this._sanitizeField(t),e=void 0===e?!0:e,e=null!==e?!!e:e,this._orders.push({field:t,dir:e,values:r})}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n="",r=[],o=!0,a=!1,l=void 0;try{for(var u,s=this._orders[Symbol.iterator]();!(o=(u=s.next()).done);o=!0){var c=u.value,f=c.field,h=c.dir,v=c.values;n=i(n,", ");var d=this._buildString(f,v,{buildParameterized:e.buildParameterized});n+=d.text,r.push.apply(r,t(d.values)),null!==h&&(n+=" "+(h?"ASC":"DESC"))}}catch(p){a=!0,l=p}finally{try{!o&&s["return"]&&s["return"]()}finally{if(a)throw l}}return{text:n.length?"ORDER BY "+n:"",values:r}}}]),a}(y.Block),y.LimitBlock=function(t){function i(t){r(this,i);var n=e(this,Object.getPrototypeOf(i).call(this,t));return n._limit=null,n}return n(i,t),d(i,[{key:"limit",value:function(t){this._limit=this._sanitizeLimitOffset(t)}},{key:"_toParamString",value:function(){return{text:null!==this._limit?"LIMIT "+this._limit:"",values:[]}}}]),i}(y.Block),y.JoinBlock=function(o){function a(t){r(this,a);var n=e(this,Object.getPrototypeOf(a).call(this,t));return n._joins=[],n}return n(a,o),d(a,[{key:"join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],n=arguments.length<=2||void 0===arguments[2]?null:arguments[2],r=arguments.length<=3||void 0===arguments[3]?"INNER":arguments[3];t=this._sanitizeTable(t,!0),e=e?this._sanitizeTableAlias(e):e,n=n?this._sanitizeExpression(n):n,this._joins.push({type:r,table:t,alias:e,condition:n})}},{key:"left_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],n=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,n,"LEFT")}},{key:"right_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],n=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,n,"RIGHT")}},{key:"outer_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],n=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,n,"OUTER")}},{key:"left_outer_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],n=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,n,"LEFT OUTER")}},{key:"full_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],n=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,n,"FULL")}},{key:"cross_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],n=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,n,"CROSS")}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n="",r=[],o=!0,a=!1,l=void 0;try{for(var u,s=this._joins[Symbol.iterator]();!(o=(u=s.next()).done);o=!0){var c=u.value,f=c.type,h=c.table,v=c.alias,d=c.condition;n=i(n,this.options.separator);var p=void 0;if(h instanceof y.BaseBuilder){var g=h._toParamString({buildParameterized:e.buildParameterized,nested:!0});r.push.apply(r,t(g.values)),p=g.text}else p=this._formatTableName(h);if(n+=f+" JOIN "+p,v&&(n+=" "+this._formatTableAlias(v)),d){n+=" ON ";var _=void 0;_=d instanceof y.Expression?d._toParamString({buildParameterized:e.buildParameterized}):this._buildString(d,[],{buildParameterized:e.buildParameterized}),n+=this._applyNestingFormatting(_.text),r.push.apply(r,t(_.values))}}}catch(m){a=!0,l=m}finally{try{!o&&s["return"]&&s["return"]()}finally{if(a)throw l}}return{text:n,values:r}}}]),a}(y.Block),y.UnionBlock=function(o){function a(t){r(this,a);var n=e(this,Object.getPrototypeOf(a).call(this,t));return n._unions=[],n}return n(a,o),d(a,[{key:"union",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?"UNION":arguments[1];t=this._sanitizeTable(t),this._unions.push({type:e,table:t})}},{key:"union_all",value:function(t){this.union(t,"UNION ALL")}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n="",r=[],o=!0,a=!1,l=void 0;try{for(var u,s=this._unions[Symbol.iterator]();!(o=(u=s.next()).done);o=!0){var c=u.value,f=c.type,h=c.table;n=i(n,this.options.separator);var v=void 0;if(h instanceof y.BaseBuilder){var d=h._toParamString({buildParameterized:e.buildParameterized,nested:!0});v=d.text,r.push.apply(r,t(d.values))}else n=this._formatTableName(h);n+=f+" "+v}}catch(p){a=!0,l=p}finally{try{!o&&s["return"]&&s["return"]()}finally{if(a)throw l}}return{text:n,values:r}}}]),a}(y.Block),y.QueryBuilder=function(i){function a(t,n){r(this,a);var i=e(this,Object.getPrototypeOf(a).call(this,t));i.blocks=n||[];var o=!0,l=!1,u=void 0;try{for(var s,c=i.blocks[Symbol.iterator]();!(o=(s=c.next()).done);o=!0){var f=s.value,h=f.exposedMethods();for(var v in h){var d=h[v];if(void 0!==i[v])throw new Error("Builder already has a builder method called: "+v);!function(t,e,n){i[e]=function(){for(var e=arguments.length,r=Array(e),o=0;e>o;o++)r[o]=arguments[o];return n.call.apply(n,[t].concat(r)),i}}(f,v,d)}}}catch(p){l=!0,u=p}finally{try{!o&&c["return"]&&c["return"]()}finally{if(l)throw u}}return i}return n(a,i),d(a,[{key:"registerValueHandler",value:function(t,e){var n=!0,r=!1,i=void 0;try{for(var o,l=this.blocks[Symbol.iterator]();!(n=(o=l.next()).done);n=!0){var u=o.value;u.registerValueHandler(t,e)}}catch(s){r=!0,i=s}finally{try{!n&&l["return"]&&l["return"]()}finally{if(r)throw i}}return v(Object.getPrototypeOf(a.prototype),"registerValueHandler",this).call(this,t,e),this}},{key:"updateOptions",value:function(t){this.options=o({},this.options,t);var e=!0,n=!1,r=void 0;try{for(var i,a=this.blocks[Symbol.iterator]();!(e=(i=a.next()).done);e=!0){var l=i.value;l.options=o({},l.options,t)}}catch(u){n=!0,r=u}finally{try{!e&&a["return"]&&a["return"]()}finally{if(n)throw r}}}},{key:"_toParamString",value:function(){var e,n=this,r=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];r=o({},this.options,r);var i=this.blocks.map(function(t){return t._toParamString({buildParameterized:r.buildParameterized,queryBuilder:n})}),a=i.map(function(t){return t.text}),l=i.map(function(t){return t.values}),u=a.filter(function(t){return 0=this._fields.length)throw new Error("set() needs to be called");var e=v(Object.getPrototypeOf(i.prototype),"_toParamString",this).call(this,t);if(e.text.length&&01?e-1:0),r=1;e>r;r++)n[r-1]=arguments[r];if(t&&n){var i=!0,o=!1,a=void 0;try{for(var l,u=function(){var e=l.value;"object"===("undefined"==typeof e?"undefined":p(e))&&Object.getOwnPropertyNames(e).forEach(function(n){"function"!=typeof e[n]&&(t[n]=e[n])})},s=n[Symbol.iterator]();!(i=(l=s.next()).done);i=!0)u()}catch(c){o=!0,a=c}finally{try{!i&&s["return"]&&s["return"]()}finally{if(o)throw a}}}return t}function a(t){return t&&t.constructor.prototype===Object.prototype}function l(t){return t&&t.constructor.prototype===Array.prototype}function u(t){if(t&&t.constructor&&t.constructor.toString){var e=t.constructor.toString().match(/function\s*(\w+)/);if(e&&2===e.length)return e[1]}}function s(t){if(!t)return t;if("function"==typeof t.clone)return t.clone();if(!a(t)&&!l(t))return JSON.parse(JSON.stringify(t));var e=function(){var e=new t.constructor;return Object.getOwnPropertyNames(t).forEach(function(n){"function"!=typeof t[n]&&(e[n]=s(t[n]))}),{v:e}}();return"object"===("undefined"==typeof e?"undefined":p(e))?e.v:void 0}function c(t,e,n){var r="undefined"==typeof e?"undefined":p(e);if("function"!==r&&"string"!==r)throw new Error("type must be a class constructor or string");if("function"!=typeof n)throw new Error("handler must be a function");for(var i in t){var o=t[i];if(o.type===e)return void(o.handler=n)}t.push({type:e,handler:n})}function f(t){for(var e=arguments.length,n=Array(e>1?e-1:0),r=1;e>r;r++)n[r-1]=arguments[r];for(var i in n){var o=n[i];for(var a in o){var l=o[a];if(("undefined"==typeof t?"undefined":p(t))===l.type||"string"!=typeof l.type&&t instanceof l.type)return l.handler}}}function h(){var h=arguments.length<=0||void 0===arguments[0]?null:arguments[0],y={_getObjectClassName:u};y.DefaultQueryBuilderOptions={autoQuoteTableNames:!1,autoQuoteFieldNames:!1,autoQuoteAliasNames:!0,useAsForTableAliasNames:!1,nameQuoteCharacter:"`",tableAliasQuoteCharacter:"`",fieldAliasQuoteCharacter:'"',valueHandlers:[],parameterCharacter:"?",numberedParameters:!1,numberedParametersPrefix:"$",numberedParametersStartAt:1,replaceSingleQuotes:!1,singleQuoteReplacement:"''",separator:" "},y.globalValueHandlers=[],y.registerValueHandler=function(t,e){c(y.globalValueHandlers,t,e)},y.Cloneable=function(){function t(){r(this,t)}return d(t,[{key:"clone",value:function(){var t=new this.constructor;return o(t,s(o({},this)))}}]),t}(),y.BaseBuilder=function(i){function a(t){r(this,a);var n=e(this,Object.getPrototypeOf(a).call(this)),i=JSON.parse(JSON.stringify(y.DefaultQueryBuilderOptions));return n.options=o({},i,t),n}return n(a,i),d(a,[{key:"registerValueHandler",value:function(t,e){return c(this.options.valueHandlers,t,e),this}},{key:"_sanitizeExpression",value:function(t){if(!(t instanceof y.Expression)&&"string"!=typeof t)throw new Error("expression must be a string or Expression instance");return t}},{key:"_sanitizeName",value:function(t,e){if("string"!=typeof t)throw new Error(e+" must be a string");return t}},{key:"_sanitizeField",value:function(t){return t instanceof y.BaseBuilder||(t=this._sanitizeName(t,"field name")),t}},{key:"_sanitizeQueryBuilder",value:function(t){if(t instanceof y.QueryBuilder)return t;throw new Error("must be a QueryBuilder instance")}},{key:"_sanitizeTable",value:function(t){if("string"!=typeof t)try{t=this._sanitizeQueryBuilder(t)}catch(e){throw new Error("table name must be a string or a query builder")}else t=this._sanitizeName(t,"table");return t}},{key:"_sanitizeTableAlias",value:function(t){return this._sanitizeName(t,"table alias")}},{key:"_sanitizeFieldAlias",value:function(t){return this._sanitizeName(t,"field alias")}},{key:"_sanitizeLimitOffset",value:function(t){if(t=parseInt(t),0>t||isNaN(t))throw new Error("limit/offset must be >= 0");return t}},{key:"_sanitizeValue",value:function(t){var e="undefined"==typeof t?"undefined":p(t);if(null===t);else if("string"===e||"number"===e||"boolean"===e);else if(t instanceof y.BaseBuilder);else{var n=!!f(t,this.options.valueHandlers,y.globalValueHandlers);if(!n)throw new Error("field value must be a string, number, boolean, null or one of the registered custom value types")}return t}},{key:"_escapeValue",value:function(t){return this.options.replaceSingleQuotes?t.replace(/\'/g,this.options.singleQuoteReplacement):t}},{key:"_formatTableName",value:function(t){if(this.options.autoQuoteTableNames){var e=this.options.nameQuoteCharacter;t=""+e+t+e}return t}},{key:"_formatFieldAlias",value:function(t){if(this.options.autoQuoteAliasNames){var e=this.options.fieldAliasQuoteCharacter;t=""+e+t+e}return t}},{key:"_formatTableAlias",value:function(t){if(this.options.autoQuoteAliasNames){var e=this.options.tableAliasQuoteCharacter;t=""+e+t+e}return this.options.useAsForTableAliasNames?"AS "+t:t}},{key:"_formatFieldName",value:function(t){var e=this,n=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];return this.options.autoQuoteFieldNames&&!function(){var r=e.options.nameQuoteCharacter;t=n.ignorePeriodsForFieldNameQuotes?""+r+t+r:t.split(".").map(function(t){return"*"===t?t:""+r+t+r}).join(".")}(),t}},{key:"_formatCustomValue",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?!1:arguments[1],n=f(t,this.options.valueHandlers,y.globalValueHandlers);return n&&(t=n(t,e)),t}},{key:"_formatValueForParamArray",value:function(t){var e=this;return l(t)?t.map(function(t){return e._formatValueForParamArray(t)}):this._formatCustomValue(t,!0)}},{key:"_formatValueForQueryString",value:function(t){var e=this,n=arguments.length<=1||void 0===arguments[1]?{}:arguments[1],r=this._formatCustomValue(t);if(r!==t)return this._applyNestingFormatting(r);if(l(t))t=t.map(function(t){return e._formatValueForQueryString(t)}),t=this._applyNestingFormatting(t.join(", "));else{var i="undefined"==typeof t?"undefined":p(t);if(null===t)t="NULL";else if("boolean"===i)t=t?"TRUE":"FALSE";else if(t instanceof y.BaseBuilder)t=this._applyNestingFormatting(t.toString());else if("number"!==i)if(n.dontQuote)t=""+t;else{var o=this._escapeValue(t);t="'"+o+"'"}}return t}},{key:"_applyNestingFormatting",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?!0:arguments[1];return t&&"string"==typeof t&&e&&("("!==t.charAt(0)||")"!==t.charAt(t.length-1))?"("+t+")":t}},{key:"_buildString",value:function(e,n){var r=arguments.length<=2||void 0===arguments[2]?{}:arguments[2],i=r.nested,o=r.buildParameterized,a=r.formattingOptions;n=n||[],e=e||"";for(var u="",s=-1,c=[],f=this.options.parameterCharacter,h=0;e.length>h;)if(e.substr(h,f.length)===f){var v=n[++s];if(o)if(v instanceof y.BaseBuilder){var d=v._toParamString({buildParameterized:o,nested:!0});u+=d.text,c.push.apply(c,t(d.values))}else if(v=this._formatValueForParamArray(v),l(v)){var p=v.map(function(){return f}).join(", ");u+="("+p+")",c.push.apply(c,t(v))}else u+=f,c.push(v);else u+=this._formatValueForQueryString(v,a);h+=f.length}else u+=e.charAt(h),h++;return{text:this._applyNestingFormatting(u,!!i),values:c}}},{key:"_buildManyStrings",value:function(e,n){var r=arguments.length<=2||void 0===arguments[2]?{}:arguments[2],i=[],o=[];for(var a in e){var l=e[a],u=n[a],s=this._buildString(l,u,{buildParameterized:r.buildParameterized,nested:!1}),c=s.text,f=s.values;i.push(c),o.push.apply(o,t(f))}return i=i.join(this.options.separator),{text:i.length?this._applyNestingFormatting(i,!!r.nested):"",values:o}}},{key:"_toParamString",value:function(t){throw new Error("Not yet implemented")}},{key:"toString",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return this._toParamString(t).text}},{key:"toParam",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return this._toParamString(o({},t,{buildParameterized:!0}))}}]),a}(y.Cloneable),y.Expression=function(i){function o(t){r(this,o);var n=e(this,Object.getPrototypeOf(o).call(this,t));return n._nodes=[],n}return n(o,i),d(o,[{key:"and",value:function(t,e){for(var n=arguments.length,r=Array(n>2?n-2:0),i=2;n>i;i++)r[i-2]=arguments[i];return this._add("AND",t,e,r)}},{key:"or",value:function(t,e){for(var n=arguments.length,r=Array(n>2?n-2:0),i=2;n>i;i++)r[i-2]=arguments[i];return this._add("OR",t,e,r)}},{key:"_add",value:function(t,e,n,r){var i=["=","<",">","<=",">=","<>","!=","in","not in","like","not like"],o=void 0;return"string"==typeof e&&"string"==typeof n&&-1!=i.indexOf(n.toLowerCase())?o=this._buildExpression(e,n):(o=e,r.unshift(n)),this._sanitizeExpression(e),this._nodes.push({type:t,expr:o,para:r}),this}},{key:"_buildExpression",value:function(t,e){var n=this._formatFieldName(t),r=this.options.parameterCharacter,i=n+" "+e+" "+r;return i}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n=[],r=[],i=!0,o=!1,a=void 0;try{for(var l,u=this._nodes[Symbol.iterator]();!(i=(l=u.next()).done);i=!0){var s=l.value,c=s.type,f=s.expr,h=s.para,v=f instanceof y.Expression?f._toParamString({buildParameterized:e.buildParameterized,nested:!0}):this._buildString(f,h,{buildParameterized:e.buildParameterized}),d=v.text,p=v.values;n.length&&n.push(c),n.push(d),r.push.apply(r,t(p))}}catch(g){o=!0,a=g}finally{try{!i&&u["return"]&&u["return"]()}finally{if(o)throw a}}return n=n.join(" "),{text:this._applyNestingFormatting(n,!!e.nested),values:r}}}]),o}(y.BaseBuilder),y.Case=function(l){function u(t){var n=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];r(this,u);var i=e(this,Object.getPrototypeOf(u).call(this,n));return a(t)&&(n=t,t=null),t&&(i._fieldName=i._sanitizeField(t)),i.options=o({},y.DefaultQueryBuilderOptions,n),i._cases=[],i._elseValue=null,i}return n(u,l),d(u,[{key:"when",value:function(t){for(var e=arguments.length,n=Array(e>1?e-1:0),r=1;e>r;r++)n[r-1]=arguments[r];return this._cases.unshift({expression:t,values:n}),this}},{key:"then",value:function(t){if(0==this._cases.length)throw new Error("when() needs to be called first");return this._cases[0].result=t,this}},{key:"else",value:function(t){return this._elseValue=t,this}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n="",r=[],o=!0,a=!1,l=void 0;try{for(var u,s=this._cases[Symbol.iterator]();!(o=(u=s.next()).done);o=!0){var c=u.value,f=c.expression,h=c.values,v=c.result;n=i(n," ");var d=this._buildString(f,h,{buildParameterized:e.buildParameterized,nested:!0});n+="WHEN "+d.text+" THEN "+this._formatValueForQueryString(v),r.push.apply(r,t(d.values))}}catch(p){a=!0,l=p}finally{try{!o&&s["return"]&&s["return"]()}finally{if(a)throw l}}return n.length?(n+=" ELSE "+this._formatValueForQueryString(this._elseValue)+" END",this._fieldName&&(n=this._fieldName+" "+n),n="CASE "+n):n=this._formatValueForQueryString(this._elseValue),{text:n,values:r}}}]),u}(y.BaseBuilder),y.Block=function(t){function i(t){return r(this,i),e(this,Object.getPrototypeOf(i).call(this,t))}return n(i,t),d(i,[{key:"exposedMethods",value:function(){for(var t={},e=this;e;)Object.getOwnPropertyNames(e).forEach(function(n){"constructor"===n||"function"!=typeof e[n]||"_"===n.charAt(0)||y.Block.prototype[n]||(t[n]=e[n])}),e=Object.getPrototypeOf(e);return t}}]),i}(y.BaseBuilder),y.StringBlock=function(t){function i(t,n){r(this,i);var o=e(this,Object.getPrototypeOf(i).call(this,t));return o._str=n,o}return n(i,t),d(i,[{key:"_toParamString",value:function(){arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return{text:this._str,values:[]}}}]),i}(y.Block),y.FunctionBlock=function(t){function i(t){r(this,i);var n=e(this,Object.getPrototypeOf(i).call(this,t));return n._strings=[],n._values=[],n}return n(i,t),d(i,[{key:"function",value:function(t){this._strings.push(t);for(var e=arguments.length,n=Array(e>1?e-1:0),r=1;e>r;r++)n[r-1]=arguments[r];this._values.push(n)}},{key:"_toParamString",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return this._buildManyStrings(this._strings,this._values,t)}}]),i}(y.Block),y.registerValueHandler(y.FunctionBlock,function(t){var e=arguments.length<=1||void 0===arguments[1]?!1:arguments[1];return e?t.toParam():t.toString()}),y.AbstractTableBlock=function(o){function a(t,n){r(this,a);var i=e(this,Object.getPrototypeOf(a).call(this,t));return i._tables=[],i}return n(a,o),d(a,[{key:"_table",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1];e=e?this._sanitizeTableAlias(e):e,t=this._sanitizeTable(t),this.options.singleTable&&(this._tables=[]),this._tables.push({table:t,alias:e})}},{key:"_hasTable",value:function(){return 01)throw new Error("Cannot set multiple rows of fields this way.");"undefined"!=typeof e&&(e=this._sanitizeValue(e)),t=this._sanitizeField(t);var r=this._fields.indexOf(t);-1===r&&(this._fields.push(t),r=this._fields.length-1),this._values[0][r]=e,this._valueOptions[0][r]=n}},{key:"_setFields",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];if("object"!==("undefined"==typeof t?"undefined":p(t)))throw new Error("Expected an object but got "+("undefined"==typeof t?"undefined":p(t)));for(var n in t)this._set(n,t[n],e)}},{key:"_setFieldsRows",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];if(!l(t))throw new Error("Expected an array of objects but got "+("undefined"==typeof t?"undefined":p(t)));this._reset();for(var n in t){var r=t[n];for(var i in r){var o=r[i];i=this._sanitizeField(i),o=this._sanitizeValue(o);var a=this._fields.indexOf(i);if(n>0&&-1===a)throw new Error("All fields in subsequent rows must match the fields in the first row");-1===a&&(this._fields.push(i),a=this._fields.length-1),l(this._values[n])||(this._values[n]=[],this._valueOptions[n]=[]),this._values[n][a]=o,this._valueOptions[n][a]=e}}}}]),i}(y.Block),y.SetFieldBlock=function(o){function a(){return r(this,a),e(this,Object.getPrototypeOf(a).apply(this,arguments))}return n(a,o),d(a,[{key:"set",value:function(t,e,n){this._set(t,e,n)}},{key:"setFields",value:function(t,e){this._setFields(t,e)}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n=e.buildParameterized;if(0>=this._fields.length)throw new Error("set() needs to be called");var r="",o=[];for(var a in this._fields){r=i(r,", ");var l=this._fields[a],u=this._values[0][a];if("undefined"==typeof u)r+=l;else{var s=this._buildString(l+" = "+this.options.parameterCharacter,[u],{buildParameterized:n,formattingOptions:this._valueOptions[0][a]});r+=s.text,o.push.apply(o,t(s.values))}}return{text:"SET "+r,values:o}}}]),a}(y.AbstractSetFieldBlock),y.InsertFieldValueBlock=function(o){function a(){return r(this,a),e(this,Object.getPrototypeOf(a).apply(this,arguments))}return n(a,o),d(a,[{key:"set",value:function(t,e){var n=arguments.length<=2||void 0===arguments[2]?{}:arguments[2];this._set(t,e,n)}},{key:"setFields",value:function(t,e){this._setFields(t,e)}},{key:"setFieldsRows",value:function(t,e){this._setFieldsRows(t,e)}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n=e.buildParameterized,r=this._fields.join(", "),o=[],a=[];for(var l in this._values){o[l]="";for(var u in this._values[l]){var s=this._buildString(this.options.parameterCharacter,[this._values[l][u]],{buildParameterized:n,formattingOptions:this._valueOptions[l][u]});a.push.apply(a,t(s.values)),o[l]=i(o[l],", "),o[l]+=s.text}}return{text:r.length?"("+r+") VALUES ("+o.join("), (")+")":"",values:a}}}]),a}(y.AbstractSetFieldBlock),y.InsertFieldsFromQueryBlock=function(t){function i(t){r(this,i);var n=e(this,Object.getPrototypeOf(i).call(this,t));return n._fields=[],n._query=null,n}return n(i,t),d(i,[{key:"fromQuery",value:function(t,e){var n=this;this._fields=t.map(function(t){return n._sanitizeField(t)}),this._query=this._sanitizeQueryBuilder(e)}},{key:"_toParamString",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],e="",n=[];if(this._fields.length&&this._query){var r=this._query._toParamString({buildParameterized:t.buildParameterized,nested:!0}),i=r.text,o=r.values;e="("+this._fields.join(", ")+") ("+i+")",n=o}return{text:e,values:n}}}]),i}(y.Block),y.DistinctBlock=function(t){function i(){return r(this,i),e(this,Object.getPrototypeOf(i).apply(this,arguments))}return n(i,t),d(i,[{key:"distinct",value:function(){this._useDistinct=!0}},{key:"_toParamString",value:function(){return{text:this._useDistinct?"DISTINCT":"",values:[]}}}]),i}(y.Block),y.GroupByBlock=function(t){function i(t){r(this,i);var n=e(this,Object.getPrototypeOf(i).call(this,t));return n._groups=[],n}return n(i,t),d(i,[{key:"group",value:function(t){this._groups.push(this._sanitizeField(t))}},{key:"_toParamString",value:function(){arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return{text:this._groups.length?"GROUP BY "+this._groups.join(", "):"",values:[]}}}]),i}(y.Block),y.OffsetBlock=function(t){function i(t){r(this,i);var n=e(this,Object.getPrototypeOf(i).call(this,t));return n._offsets=null,n}return n(i,t),d(i,[{key:"offset",value:function(t){this._offsets=this._sanitizeLimitOffset(t)}},{key:"_toParamString",value:function(){return{text:this._offsets?"OFFSET "+this._offsets:"",values:[]}}}]),i}(y.Block),y.AbstractConditionBlock=function(o){function a(t){r(this,a);var n=e(this,Object.getPrototypeOf(a).call(this,t));return n._conditions=[],n}return n(a,o),d(a,[{key:"_condition",value:function(t){for(var e=arguments.length,n=Array(e>1?e-1:0),r=1;e>r;r++)n[r-1]=arguments[r];t=this._sanitizeExpression(t),this._conditions.push({expr:t,values:n})}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n="",r=[],o=!0,a=!1,l=void 0;try{for(var u,s=this._conditions[Symbol.iterator]();!(o=(u=s.next()).done);o=!0){var c=u.value,f=c.expr,h=c.values;n=i(n,") AND (");var v=f instanceof y.Expression?f._toParamString({buildParameterized:e.buildParameterized}):this._buildString(f,h,{buildParameterized:e.buildParameterized});n+=v.text,r.push.apply(r,t(v.values))}}catch(d){a=!0,l=d}finally{try{!o&&s["return"]&&s["return"]()}finally{if(a)throw l}}return{text:n.length?this.options.verb+" ("+n+")":"",values:r}}}]),a}(y.Block),y.WhereBlock=function(t){function i(t){return r(this,i),e(this,Object.getPrototypeOf(i).call(this,o({},t,{verb:"WHERE"})))}return n(i,t),d(i,[{key:"where",value:function(t,e){for(var n=arguments.length,r=Array(n>2?n-2:0),i=2;n>i;i++)r[i-2]=arguments[i];var o=["=","<",">","<=",">=","<>","!=","in","not in","like","not like"];if("string"==typeof t&&"string"==typeof e&&-1!=o.indexOf(e.toLowerCase())){var a=new y.Expression(this.options);this._condition(a._add("AND",t,e,r))}else this._condition.apply(this,arguments)}}]),i}(y.AbstractConditionBlock),y.HavingBlock=function(t){function i(t){return r(this,i),e(this,Object.getPrototypeOf(i).call(this,o({},t,{verb:"HAVING"})))}return n(i,t),d(i,[{key:"having",value:function(t,e){for(var n=arguments.length,r=Array(n>2?n-2:0),i=2;n>i;i++)r[i-2]=arguments[i];var o=["=","<",">","<=",">=","<>","!=","in","not in","like","not like"];if("string"==typeof t&&"string"==typeof e&&-1!=o.indexOf(e.toLowerCase())){var a=new y.Expression(this.options);this._condition(a._add("AND",t,e,r))}else this._condition.apply(this,arguments)}}]),i}(y.AbstractConditionBlock),y.OrderByBlock=function(o){function a(t){r(this,a);var n=e(this,Object.getPrototypeOf(a).call(this,t));return n._orders=[],n}return n(a,o),d(a,[{key:"order",value:function(t,e){for(var n=arguments.length,r=Array(n>2?n-2:0),i=2;n>i;i++)r[i-2]=arguments[i];t=this._sanitizeField(t),e=void 0===e?!0:e,e=null!==e?!!e:e,this._orders.push({field:t,dir:e,values:r})}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n="",r=[],o=!0,a=!1,l=void 0;try{for(var u,s=this._orders[Symbol.iterator]();!(o=(u=s.next()).done);o=!0){var c=u.value,f=c.field,h=c.dir,v=c.values;n=i(n,", ");var d=this._buildString(f,v,{buildParameterized:e.buildParameterized});n+=d.text,r.push.apply(r,t(d.values)),null!==h&&(n+=" "+(h?"ASC":"DESC"))}}catch(p){a=!0,l=p}finally{try{!o&&s["return"]&&s["return"]()}finally{if(a)throw l}}return{text:n.length?"ORDER BY "+n:"",values:r}}}]),a}(y.Block),y.LimitBlock=function(t){function i(t){r(this,i);var n=e(this,Object.getPrototypeOf(i).call(this,t));return n._limit=null,n}return n(i,t),d(i,[{key:"limit",value:function(t){this._limit=this._sanitizeLimitOffset(t)}},{key:"_toParamString",value:function(){return{text:null!==this._limit?"LIMIT "+this._limit:"",values:[]}}}]),i}(y.Block),y.JoinBlock=function(o){function a(t){r(this,a);var n=e(this,Object.getPrototypeOf(a).call(this,t));return n._joins=[],n}return n(a,o),d(a,[{key:"join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],n=arguments.length<=2||void 0===arguments[2]?null:arguments[2],r=arguments.length<=3||void 0===arguments[3]?"INNER":arguments[3];t=this._sanitizeTable(t,!0),e=e?this._sanitizeTableAlias(e):e,n=n?this._sanitizeExpression(n):n,this._joins.push({type:r,table:t,alias:e,condition:n})}},{key:"left_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],n=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,n,"LEFT")}},{key:"right_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],n=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,n,"RIGHT")}},{key:"outer_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],n=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,n,"OUTER")}},{key:"left_outer_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],n=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,n,"LEFT OUTER")}},{key:"full_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],n=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,n,"FULL")}},{key:"cross_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],n=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,n,"CROSS")}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n="",r=[],o=!0,a=!1,l=void 0;try{for(var u,s=this._joins[Symbol.iterator]();!(o=(u=s.next()).done);o=!0){var c=u.value,f=c.type,h=c.table,v=c.alias,d=c.condition;n=i(n,this.options.separator);var p=void 0;if(h instanceof y.BaseBuilder){var g=h._toParamString({buildParameterized:e.buildParameterized,nested:!0});r.push.apply(r,t(g.values)),p=g.text}else p=this._formatTableName(h);if(n+=f+" JOIN "+p,v&&(n+=" "+this._formatTableAlias(v)),d){n+=" ON ";var _=void 0;_=d instanceof y.Expression?d._toParamString({buildParameterized:e.buildParameterized}):this._buildString(d,[],{buildParameterized:e.buildParameterized}),n+=this._applyNestingFormatting(_.text),r.push.apply(r,t(_.values))}}}catch(m){a=!0,l=m}finally{try{!o&&s["return"]&&s["return"]()}finally{if(a)throw l}}return{text:n,values:r}}}]),a}(y.Block),y.UnionBlock=function(o){function a(t){r(this,a);var n=e(this,Object.getPrototypeOf(a).call(this,t));return n._unions=[],n}return n(a,o),d(a,[{key:"union",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?"UNION":arguments[1];t=this._sanitizeTable(t),this._unions.push({type:e,table:t})}},{key:"union_all",value:function(t){this.union(t,"UNION ALL")}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n="",r=[],o=!0,a=!1,l=void 0;try{for(var u,s=this._unions[Symbol.iterator]();!(o=(u=s.next()).done);o=!0){var c=u.value,f=c.type,h=c.table;n=i(n,this.options.separator);var v=void 0;if(h instanceof y.BaseBuilder){var d=h._toParamString({buildParameterized:e.buildParameterized,nested:!0});v=d.text,r.push.apply(r,t(d.values))}else n=this._formatTableName(h);n+=f+" "+v}}catch(p){a=!0,l=p}finally{try{!o&&s["return"]&&s["return"]()}finally{if(a)throw l}}return{text:n,values:r}}}]),a}(y.Block),y.QueryBuilder=function(i){function a(t,n){r(this,a);var i=e(this,Object.getPrototypeOf(a).call(this,t));i.blocks=n||[];var o=!0,l=!1,u=void 0;try{for(var s,c=i.blocks[Symbol.iterator]();!(o=(s=c.next()).done);o=!0){var f=s.value,h=f.exposedMethods();for(var v in h){var d=h[v];if(void 0!==i[v])throw new Error("Builder already has a builder method called: "+v);!function(t,e,n){i[e]=function(){for(var e=arguments.length,r=Array(e),o=0;e>o;o++)r[o]=arguments[o];return n.call.apply(n,[t].concat(r)),i}}(f,v,d)}}}catch(p){l=!0,u=p}finally{try{!o&&c["return"]&&c["return"]()}finally{if(l)throw u}}return i}return n(a,i),d(a,[{key:"registerValueHandler",value:function(t,e){var n=!0,r=!1,i=void 0;try{for(var o,l=this.blocks[Symbol.iterator]();!(n=(o=l.next()).done);n=!0){var u=o.value;u.registerValueHandler(t,e)}}catch(s){r=!0,i=s}finally{try{!n&&l["return"]&&l["return"]()}finally{if(r)throw i}}return v(Object.getPrototypeOf(a.prototype),"registerValueHandler",this).call(this,t,e),this}},{key:"updateOptions",value:function(t){this.options=o({},this.options,t);var e=!0,n=!1,r=void 0;try{for(var i,a=this.blocks[Symbol.iterator]();!(e=(i=a.next()).done);e=!0){var l=i.value;l.options=o({},l.options,t)}}catch(u){n=!0,r=u}finally{try{!e&&a["return"]&&a["return"]()}finally{if(n)throw r}}}},{key:"_toParamString",value:function(){var e,n=this,r=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];r=o({},this.options,r);var i=this.blocks.map(function(t){return t._toParamString({buildParameterized:r.buildParameterized,queryBuilder:n})}),a=i.map(function(t){return t.text}),l=i.map(function(t){return t.values}),u=a.filter(function(t){return 0=this._fields.length)throw new Error("set() needs to be called");var e=v(Object.getPrototypeOf(i.prototype),"_toParamString",this).call(this,t);if(e.text.length&&0', '<=', '>=', '<>', '!=', 'in', 'not in', 'like', 'not like']; if (typeof field === 'string' && typeof operator === 'string' && -1 != validOperators.indexOf(operator.toLowerCase())) { // key - operator - value - let expr = new cls.Expression(); + let expr = new cls.Expression(this.options); this._condition(expr._add('AND', field, operator, values)); } else { // default squel-behaviour without auto-quoting @@ -1618,7 +1618,7 @@ function _buildSquel(flavour = null) { let validOperators = ['=', '<', '>', '<=', '>=', '<>', '!=', 'in', 'not in', 'like', 'not like']; if (typeof field === 'string' && typeof operator === 'string' && -1 != validOperators.indexOf(operator.toLowerCase())) { // key - operator - value - let expr = new cls.Expression(); + let expr = new cls.Expression(this.options); this._condition(expr._add('AND', field, operator, values)); } else { // default squel-behaviour without auto-quoting diff --git a/test/blocks.test.coffee b/test/blocks.test.coffee index 06c06318..6e939669 100644 --- a/test/blocks.test.coffee +++ b/test/blocks.test.coffee @@ -1085,8 +1085,8 @@ test['Blocks'] = 'output with quoting': beforeEach: -> - squel.cls.DefaultQueryBuilderOptions.autoQuoteFieldNames = true; - subquery = new squel.cls.Select() + @inst = new @cls({ autoQuoteFieldNames: true }) + subquery = new squel.cls.Select({ autoQuoteFieldNames: true }) subquery.field('col1').from('table1').where('field1', '=', 10) @inst.where('a', 'in', subquery) @inst.where('d', 'in', [4, 5, 6]) @@ -1100,8 +1100,6 @@ test['Blocks'] = text: 'WHERE (`a` in (SELECT `col1` FROM table1 WHERE (`field1` = ?))) AND (`d` in (?, ?, ?))' values: [10, 4, 5, 6] } - afterEach: -> - squel.cls.DefaultQueryBuilderOptions.autoQuoteFieldNames = false; @@ -1145,8 +1143,8 @@ test['Blocks'] = 'output with quoting': beforeEach: -> - squel.cls.DefaultQueryBuilderOptions.autoQuoteFieldNames = true; - subquery = new squel.cls.Select() + @inst = new @cls({ autoQuoteFieldNames: true }) + subquery = new squel.cls.Select({ autoQuoteFieldNames: true }) subquery.field('col1').from('table1').where('field1', '=', 10) @inst.having('a', 'in', subquery) @inst.having('d', 'in', [4, 5, 6]) @@ -1160,8 +1158,6 @@ test['Blocks'] = text: 'HAVING (`a` in (SELECT `col1` FROM table1 WHERE (`field1` = ?))) AND (`d` in (?, ?, ?))' values: [10, 4, 5, 6] } - afterEach: -> - squel.cls.DefaultQueryBuilderOptions.autoQuoteFieldNames = false; 'OrderByBlock': diff --git a/test/select.test.coffee b/test/select.test.coffee index fed31a6b..3fe4c79f 100644 --- a/test/select.test.coffee +++ b/test/select.test.coffee @@ -428,5 +428,29 @@ test['SELECT builder'] = ] } + 'Auto-Quote Query': + beforeEach: -> + @qry1 = squel.select({ autoQuoteFieldNames: true }).field('name').from('students').where('age', '>', 15) + @qry2 = squel.select({ autoQuoteFieldNames: true }).field('name').from('students').where('age', '<', 6) + @qry3 = squel.select({ autoQuoteFieldNames: true }).field('name').from('students').where('age', '=', 8) + @qry4 = squel.select({ autoQuoteFieldNames: true }).field('name').from('students').where('age', 'IN', [2, 10]) + @qry1.union(@qry2) + @qry1.union(@qry3) + @qry4.union_all(@qry1) + toString: -> + assert.same @qry4.toString(), """ + SELECT `name` FROM students WHERE (`age` IN (2, 10)) UNION ALL (SELECT `name` FROM students WHERE (`age` > 15) UNION (SELECT `name` FROM students WHERE (`age` < 6)) UNION (SELECT `name` FROM students WHERE (`age` = 8))) + """ + toParam: -> + assert.same @qry4.toParam({ "numberedParameters": true}), { + "text": "SELECT `name` FROM students WHERE (`age` IN ($1, $2)) UNION ALL (SELECT `name` FROM students WHERE (`age` > $3) UNION (SELECT `name` FROM students WHERE (`age` < $4)) UNION (SELECT `name` FROM students WHERE (`age` = $5)))" + "values": [ + 2 + 10 + 15 + 6 + 8 + ] + } module?.exports[require('path').basename(__filename)] = test From ecb0062f08ec7ab3109c9182f1bbcb0117ab947b Mon Sep 17 00:00:00 2001 From: nic Date: Wed, 30 Mar 2016 12:49:58 +0200 Subject: [PATCH 3/4] Added 'is' and 'is not' to valid operators --- squel-basic.js | 6 +++--- squel-basic.min.js | 4 ++-- squel.js | 6 +++--- squel.min.js | 4 ++-- src/core.js | 6 +++--- 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/squel-basic.js b/squel-basic.js index 7c3aeae1..4c9b306b 100644 --- a/squel-basic.js +++ b/squel-basic.js @@ -819,7 +819,7 @@ function _buildSquel() { key: '_add', value: function _add(type, field, operator, params) { - var validOperators = ['=', '<', '>', '<=', '>=', '<>', '!=', 'in', 'not in', 'like', 'not like']; + var validOperators = ['=', '<', '>', '<=', '>=', '<>', '!=', 'in', 'not in', 'like', 'not like', 'is', 'is not']; var expr = void 0; if (typeof field === 'string' && typeof operator === 'string' && -1 != validOperators.indexOf(operator.toLowerCase())) { expr = this._buildExpression(field, operator); @@ -2111,7 +2111,7 @@ function _buildSquel() { values[_key8 - 2] = arguments[_key8]; } - var validOperators = ['=', '<', '>', '<=', '>=', '<>', '!=', 'in', 'not in', 'like', 'not like']; + var validOperators = ['=', '<', '>', '<=', '>=', '<>', '!=', 'in', 'not in', 'like', 'not like', 'is', 'is not']; if (typeof field === 'string' && typeof operator === 'string' && -1 != validOperators.indexOf(operator.toLowerCase())) { // key - operator - value var expr = new cls.Expression(this.options); @@ -2145,7 +2145,7 @@ function _buildSquel() { values[_key9 - 2] = arguments[_key9]; } - var validOperators = ['=', '<', '>', '<=', '>=', '<>', '!=', 'in', 'not in', 'like', 'not like']; + var validOperators = ['=', '<', '>', '<=', '>=', '<>', '!=', 'in', 'not in', 'like', 'not like', 'is', 'is not']; if (typeof field === 'string' && typeof operator === 'string' && -1 != validOperators.indexOf(operator.toLowerCase())) { // key - operator - value var expr = new cls.Expression(this.options); diff --git a/squel-basic.min.js b/squel-basic.min.js index f9c7dbe0..5d92f7eb 100644 --- a/squel-basic.min.js +++ b/squel-basic.min.js @@ -1,2 +1,2 @@ -/*! squel | https://github.com/hiddentao/squel | BSD license */!function(t,e){"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?module.exports=e():t.squel=e()}(this,function(){"use strict";function t(t){if(Array.isArray(t)){for(var e=0,r=Array(t.length);e1?e-1:0),n=1;e>n;n++)r[n-1]=arguments[n];if(t&&r){var i=!0,a=!1,o=void 0;try{for(var l,u=function(){var e=l.value;"object"===("undefined"==typeof e?"undefined":y(e))&&Object.getOwnPropertyNames(e).forEach(function(r){"function"!=typeof e[r]&&(t[r]=e[r])})},s=r[Symbol.iterator]();!(i=(l=s.next()).done);i=!0)u()}catch(f){a=!0,o=f}finally{try{!i&&s["return"]&&s["return"]()}finally{if(a)throw o}}}return t}function o(t){return t&&t.constructor.prototype===Object.prototype}function l(t){return t&&t.constructor.prototype===Array.prototype}function u(t){if(t&&t.constructor&&t.constructor.toString){var e=t.constructor.toString().match(/function\s*(\w+)/);if(e&&2===e.length)return e[1]}}function s(t){if(!t)return t;if("function"==typeof t.clone)return t.clone();if(!o(t)&&!l(t))return JSON.parse(JSON.stringify(t));var e=function(){var e=new t.constructor;return Object.getOwnPropertyNames(t).forEach(function(r){"function"!=typeof t[r]&&(e[r]=s(t[r]))}),{v:e}}();return"object"===("undefined"==typeof e?"undefined":y(e))?e.v:void 0}function f(t,e,r){var n="undefined"==typeof e?"undefined":y(e);if("function"!==n&&"string"!==n)throw new Error("type must be a class constructor or string");if("function"!=typeof r)throw new Error("handler must be a function");for(var i in t){var a=t[i];if(a.type===e)return void(a.handler=r)}t.push({type:e,handler:r})}function c(t){for(var e=arguments.length,r=Array(e>1?e-1:0),n=1;e>n;n++)r[n-1]=arguments[n];for(var i in r){var a=r[i];for(var o in a){var l=a[o];if(("undefined"==typeof t?"undefined":y(t))===l.type||"string"!=typeof l.type&&t instanceof l.type)return l.handler}}}function h(){var h=arguments.length<=0||void 0===arguments[0]?null:arguments[0],p={_getObjectClassName:u};p.DefaultQueryBuilderOptions={autoQuoteTableNames:!1,autoQuoteFieldNames:!1,autoQuoteAliasNames:!0,useAsForTableAliasNames:!1,nameQuoteCharacter:"`",tableAliasQuoteCharacter:"`",fieldAliasQuoteCharacter:'"',valueHandlers:[],parameterCharacter:"?",numberedParameters:!1,numberedParametersPrefix:"$",numberedParametersStartAt:1,replaceSingleQuotes:!1,singleQuoteReplacement:"''",separator:" "},p.globalValueHandlers=[],p.registerValueHandler=function(t,e){f(p.globalValueHandlers,t,e)},p.Cloneable=function(){function t(){n(this,t)}return d(t,[{key:"clone",value:function(){var t=new this.constructor;return a(t,s(a({},this)))}}]),t}(),p.BaseBuilder=function(i){function o(t){n(this,o);var r=e(this,Object.getPrototypeOf(o).call(this)),i=JSON.parse(JSON.stringify(p.DefaultQueryBuilderOptions));return r.options=a({},i,t),r}return r(o,i),d(o,[{key:"registerValueHandler",value:function(t,e){return f(this.options.valueHandlers,t,e),this}},{key:"_sanitizeExpression",value:function(t){if(!(t instanceof p.Expression)&&"string"!=typeof t)throw new Error("expression must be a string or Expression instance");return t}},{key:"_sanitizeName",value:function(t,e){if("string"!=typeof t)throw new Error(e+" must be a string");return t}},{key:"_sanitizeField",value:function(t){return t instanceof p.BaseBuilder||(t=this._sanitizeName(t,"field name")),t}},{key:"_sanitizeQueryBuilder",value:function(t){if(t instanceof p.QueryBuilder)return t;throw new Error("must be a QueryBuilder instance")}},{key:"_sanitizeTable",value:function(t){if("string"!=typeof t)try{t=this._sanitizeQueryBuilder(t)}catch(e){throw new Error("table name must be a string or a query builder")}else t=this._sanitizeName(t,"table");return t}},{key:"_sanitizeTableAlias",value:function(t){return this._sanitizeName(t,"table alias")}},{key:"_sanitizeFieldAlias",value:function(t){return this._sanitizeName(t,"field alias")}},{key:"_sanitizeLimitOffset",value:function(t){if(t=parseInt(t),0>t||isNaN(t))throw new Error("limit/offset must be >= 0");return t}},{key:"_sanitizeValue",value:function(t){var e="undefined"==typeof t?"undefined":y(t);if(null===t);else if("string"===e||"number"===e||"boolean"===e);else if(t instanceof p.BaseBuilder);else{var r=!!c(t,this.options.valueHandlers,p.globalValueHandlers);if(!r)throw new Error("field value must be a string, number, boolean, null or one of the registered custom value types")}return t}},{key:"_escapeValue",value:function(t){return this.options.replaceSingleQuotes?t.replace(/\'/g,this.options.singleQuoteReplacement):t}},{key:"_formatTableName",value:function(t){if(this.options.autoQuoteTableNames){var e=this.options.nameQuoteCharacter;t=""+e+t+e}return t}},{key:"_formatFieldAlias",value:function(t){if(this.options.autoQuoteAliasNames){var e=this.options.fieldAliasQuoteCharacter;t=""+e+t+e}return t}},{key:"_formatTableAlias",value:function(t){if(this.options.autoQuoteAliasNames){var e=this.options.tableAliasQuoteCharacter;t=""+e+t+e}return this.options.useAsForTableAliasNames?"AS "+t:t}},{key:"_formatFieldName",value:function(t){var e=this,r=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];return this.options.autoQuoteFieldNames&&!function(){var n=e.options.nameQuoteCharacter;t=r.ignorePeriodsForFieldNameQuotes?""+n+t+n:t.split(".").map(function(t){return"*"===t?t:""+n+t+n}).join(".")}(),t}},{key:"_formatCustomValue",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?!1:arguments[1],r=c(t,this.options.valueHandlers,p.globalValueHandlers);return r&&(t=r(t,e)),t}},{key:"_formatValueForParamArray",value:function(t){var e=this;return l(t)?t.map(function(t){return e._formatValueForParamArray(t)}):this._formatCustomValue(t,!0)}},{key:"_formatValueForQueryString",value:function(t){var e=this,r=arguments.length<=1||void 0===arguments[1]?{}:arguments[1],n=this._formatCustomValue(t);if(n!==t)return this._applyNestingFormatting(n);if(l(t))t=t.map(function(t){return e._formatValueForQueryString(t)}),t=this._applyNestingFormatting(t.join(", "));else{var i="undefined"==typeof t?"undefined":y(t);if(null===t)t="NULL";else if("boolean"===i)t=t?"TRUE":"FALSE";else if(t instanceof p.BaseBuilder)t=this._applyNestingFormatting(t.toString());else if("number"!==i)if(r.dontQuote)t=""+t;else{var a=this._escapeValue(t);t="'"+a+"'"}}return t}},{key:"_applyNestingFormatting",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?!0:arguments[1];return t&&"string"==typeof t&&e&&("("!==t.charAt(0)||")"!==t.charAt(t.length-1))?"("+t+")":t}},{key:"_buildString",value:function(e,r){var n=arguments.length<=2||void 0===arguments[2]?{}:arguments[2],i=n.nested,a=n.buildParameterized,o=n.formattingOptions;r=r||[],e=e||"";for(var u="",s=-1,f=[],c=this.options.parameterCharacter,h=0;e.length>h;)if(e.substr(h,c.length)===c){var v=r[++s];if(a)if(v instanceof p.BaseBuilder){var d=v._toParamString({buildParameterized:a,nested:!0});u+=d.text,f.push.apply(f,t(d.values))}else if(v=this._formatValueForParamArray(v),l(v)){var y=v.map(function(){return c}).join(", ");u+="("+y+")",f.push.apply(f,t(v))}else u+=c,f.push(v);else u+=this._formatValueForQueryString(v,o);h+=c.length}else u+=e.charAt(h),h++;return{text:this._applyNestingFormatting(u,!!i),values:f}}},{key:"_buildManyStrings",value:function(e,r){var n=arguments.length<=2||void 0===arguments[2]?{}:arguments[2],i=[],a=[];for(var o in e){var l=e[o],u=r[o],s=this._buildString(l,u,{buildParameterized:n.buildParameterized,nested:!1}),f=s.text,c=s.values;i.push(f),a.push.apply(a,t(c))}return i=i.join(this.options.separator),{text:i.length?this._applyNestingFormatting(i,!!n.nested):"",values:a}}},{key:"_toParamString",value:function(t){throw new Error("Not yet implemented")}},{key:"toString",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return this._toParamString(t).text}},{key:"toParam",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return this._toParamString(a({},t,{buildParameterized:!0}))}}]),o}(p.Cloneable),p.Expression=function(i){function a(t){n(this,a);var r=e(this,Object.getPrototypeOf(a).call(this,t));return r._nodes=[],r}return r(a,i),d(a,[{key:"and",value:function(t,e){for(var r=arguments.length,n=Array(r>2?r-2:0),i=2;r>i;i++)n[i-2]=arguments[i];return this._add("AND",t,e,n)}},{key:"or",value:function(t,e){for(var r=arguments.length,n=Array(r>2?r-2:0),i=2;r>i;i++)n[i-2]=arguments[i];return this._add("OR",t,e,n)}},{key:"_add",value:function(t,e,r,n){var i=["=","<",">","<=",">=","<>","!=","in","not in","like","not like"],a=void 0;return"string"==typeof e&&"string"==typeof r&&-1!=i.indexOf(r.toLowerCase())?a=this._buildExpression(e,r):(a=e,n.unshift(r)),this._sanitizeExpression(e),this._nodes.push({type:t,expr:a,para:n}),this}},{key:"_buildExpression",value:function(t,e){var r=this._formatFieldName(t),n=this.options.parameterCharacter,i=r+" "+e+" "+n;return i}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r=[],n=[],i=!0,a=!1,o=void 0;try{for(var l,u=this._nodes[Symbol.iterator]();!(i=(l=u.next()).done);i=!0){var s=l.value,f=s.type,c=s.expr,h=s.para,v=c instanceof p.Expression?c._toParamString({buildParameterized:e.buildParameterized,nested:!0}):this._buildString(c,h,{buildParameterized:e.buildParameterized}),d=v.text,y=v.values;r.length&&r.push(f),r.push(d),n.push.apply(n,t(y))}}catch(g){a=!0,o=g}finally{try{!i&&u["return"]&&u["return"]()}finally{if(a)throw o}}return r=r.join(" "),{text:this._applyNestingFormatting(r,!!e.nested),values:n}}}]),a}(p.BaseBuilder),p.Case=function(l){function u(t){var r=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];n(this,u);var i=e(this,Object.getPrototypeOf(u).call(this,r));return o(t)&&(r=t,t=null),t&&(i._fieldName=i._sanitizeField(t)),i.options=a({},p.DefaultQueryBuilderOptions,r),i._cases=[],i._elseValue=null,i}return r(u,l),d(u,[{key:"when",value:function(t){for(var e=arguments.length,r=Array(e>1?e-1:0),n=1;e>n;n++)r[n-1]=arguments[n];return this._cases.unshift({expression:t,values:r}),this}},{key:"then",value:function(t){if(0==this._cases.length)throw new Error("when() needs to be called first");return this._cases[0].result=t,this}},{key:"else",value:function(t){return this._elseValue=t,this}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r="",n=[],a=!0,o=!1,l=void 0;try{for(var u,s=this._cases[Symbol.iterator]();!(a=(u=s.next()).done);a=!0){var f=u.value,c=f.expression,h=f.values,v=f.result;r=i(r," ");var d=this._buildString(c,h,{buildParameterized:e.buildParameterized,nested:!0});r+="WHEN "+d.text+" THEN "+this._formatValueForQueryString(v),n.push.apply(n,t(d.values))}}catch(y){o=!0,l=y}finally{try{!a&&s["return"]&&s["return"]()}finally{if(o)throw l}}return r.length?(r+=" ELSE "+this._formatValueForQueryString(this._elseValue)+" END",this._fieldName&&(r=this._fieldName+" "+r),r="CASE "+r):r=this._formatValueForQueryString(this._elseValue),{text:r,values:n}}}]),u}(p.BaseBuilder),p.Block=function(t){function i(t){return n(this,i),e(this,Object.getPrototypeOf(i).call(this,t))}return r(i,t),d(i,[{key:"exposedMethods",value:function(){for(var t={},e=this;e;)Object.getOwnPropertyNames(e).forEach(function(r){"constructor"===r||"function"!=typeof e[r]||"_"===r.charAt(0)||p.Block.prototype[r]||(t[r]=e[r])}),e=Object.getPrototypeOf(e);return t}}]),i}(p.BaseBuilder),p.StringBlock=function(t){function i(t,r){n(this,i);var a=e(this,Object.getPrototypeOf(i).call(this,t));return a._str=r,a}return r(i,t),d(i,[{key:"_toParamString",value:function(){arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return{text:this._str,values:[]}}}]),i}(p.Block),p.FunctionBlock=function(t){function i(t){n(this,i);var r=e(this,Object.getPrototypeOf(i).call(this,t));return r._strings=[],r._values=[],r}return r(i,t),d(i,[{key:"function",value:function(t){this._strings.push(t);for(var e=arguments.length,r=Array(e>1?e-1:0),n=1;e>n;n++)r[n-1]=arguments[n];this._values.push(r)}},{key:"_toParamString",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return this._buildManyStrings(this._strings,this._values,t)}}]),i}(p.Block),p.registerValueHandler(p.FunctionBlock,function(t){var e=arguments.length<=1||void 0===arguments[1]?!1:arguments[1];return e?t.toParam():t.toString()}),p.AbstractTableBlock=function(a){function o(t,r){n(this,o);var i=e(this,Object.getPrototypeOf(o).call(this,t));return i._tables=[],i}return r(o,a),d(o,[{key:"_table",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1];e=e?this._sanitizeTableAlias(e):e,t=this._sanitizeTable(t),this.options.singleTable&&(this._tables=[]),this._tables.push({table:t,alias:e})}},{key:"_hasTable",value:function(){return 01)throw new Error("Cannot set multiple rows of fields this way.");"undefined"!=typeof e&&(e=this._sanitizeValue(e)),t=this._sanitizeField(t);var n=this._fields.indexOf(t);-1===n&&(this._fields.push(t),n=this._fields.length-1),this._values[0][n]=e,this._valueOptions[0][n]=r}},{key:"_setFields",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];if("object"!==("undefined"==typeof t?"undefined":y(t)))throw new Error("Expected an object but got "+("undefined"==typeof t?"undefined":y(t)));for(var r in t)this._set(r,t[r],e)}},{key:"_setFieldsRows",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];if(!l(t))throw new Error("Expected an array of objects but got "+("undefined"==typeof t?"undefined":y(t)));this._reset();for(var r in t){var n=t[r];for(var i in n){var a=n[i];i=this._sanitizeField(i),a=this._sanitizeValue(a);var o=this._fields.indexOf(i);if(r>0&&-1===o)throw new Error("All fields in subsequent rows must match the fields in the first row");-1===o&&(this._fields.push(i),o=this._fields.length-1),l(this._values[r])||(this._values[r]=[],this._valueOptions[r]=[]),this._values[r][o]=a,this._valueOptions[r][o]=e}}}}]),i}(p.Block),p.SetFieldBlock=function(a){function o(){return n(this,o),e(this,Object.getPrototypeOf(o).apply(this,arguments))}return r(o,a),d(o,[{key:"set",value:function(t,e,r){this._set(t,e,r)}},{key:"setFields",value:function(t,e){this._setFields(t,e)}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r=e.buildParameterized;if(0>=this._fields.length)throw new Error("set() needs to be called");var n="",a=[];for(var o in this._fields){n=i(n,", ");var l=this._fields[o],u=this._values[0][o];if("undefined"==typeof u)n+=l;else{var s=this._buildString(l+" = "+this.options.parameterCharacter,[u],{buildParameterized:r,formattingOptions:this._valueOptions[0][o]});n+=s.text,a.push.apply(a,t(s.values))}}return{text:"SET "+n,values:a}}}]),o}(p.AbstractSetFieldBlock),p.InsertFieldValueBlock=function(a){function o(){return n(this,o),e(this,Object.getPrototypeOf(o).apply(this,arguments))}return r(o,a),d(o,[{key:"set",value:function(t,e){var r=arguments.length<=2||void 0===arguments[2]?{}:arguments[2];this._set(t,e,r)}},{key:"setFields",value:function(t,e){this._setFields(t,e)}},{key:"setFieldsRows",value:function(t,e){this._setFieldsRows(t,e)}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r=e.buildParameterized,n=this._fields.join(", "),a=[],o=[];for(var l in this._values){a[l]="";for(var u in this._values[l]){var s=this._buildString(this.options.parameterCharacter,[this._values[l][u]],{buildParameterized:r,formattingOptions:this._valueOptions[l][u]});o.push.apply(o,t(s.values)),a[l]=i(a[l],", "),a[l]+=s.text}}return{text:n.length?"("+n+") VALUES ("+a.join("), (")+")":"",values:o}}}]),o}(p.AbstractSetFieldBlock),p.InsertFieldsFromQueryBlock=function(t){function i(t){n(this,i);var r=e(this,Object.getPrototypeOf(i).call(this,t));return r._fields=[],r._query=null,r}return r(i,t),d(i,[{key:"fromQuery",value:function(t,e){var r=this;this._fields=t.map(function(t){return r._sanitizeField(t)}),this._query=this._sanitizeQueryBuilder(e)}},{key:"_toParamString",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],e="",r=[];if(this._fields.length&&this._query){var n=this._query._toParamString({buildParameterized:t.buildParameterized,nested:!0}),i=n.text,a=n.values;e="("+this._fields.join(", ")+") ("+i+")",r=a}return{text:e,values:r}}}]),i}(p.Block),p.DistinctBlock=function(t){function i(){return n(this,i),e(this,Object.getPrototypeOf(i).apply(this,arguments))}return r(i,t),d(i,[{key:"distinct",value:function(){this._useDistinct=!0}},{key:"_toParamString",value:function(){return{text:this._useDistinct?"DISTINCT":"",values:[]}}}]),i}(p.Block),p.GroupByBlock=function(t){function i(t){n(this,i);var r=e(this,Object.getPrototypeOf(i).call(this,t));return r._groups=[],r}return r(i,t),d(i,[{key:"group",value:function(t){this._groups.push(this._sanitizeField(t))}},{key:"_toParamString",value:function(){arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return{text:this._groups.length?"GROUP BY "+this._groups.join(", "):"",values:[]}}}]),i}(p.Block),p.OffsetBlock=function(t){function i(t){n(this,i);var r=e(this,Object.getPrototypeOf(i).call(this,t));return r._offsets=null,r}return r(i,t),d(i,[{key:"offset",value:function(t){this._offsets=this._sanitizeLimitOffset(t)}},{key:"_toParamString",value:function(){return{text:this._offsets?"OFFSET "+this._offsets:"",values:[]}}}]),i}(p.Block),p.AbstractConditionBlock=function(a){function o(t){n(this,o);var r=e(this,Object.getPrototypeOf(o).call(this,t));return r._conditions=[],r}return r(o,a),d(o,[{key:"_condition",value:function(t){for(var e=arguments.length,r=Array(e>1?e-1:0),n=1;e>n;n++)r[n-1]=arguments[n];t=this._sanitizeExpression(t),this._conditions.push({expr:t,values:r})}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r="",n=[],a=!0,o=!1,l=void 0;try{for(var u,s=this._conditions[Symbol.iterator]();!(a=(u=s.next()).done);a=!0){var f=u.value,c=f.expr,h=f.values;r=i(r,") AND (");var v=c instanceof p.Expression?c._toParamString({buildParameterized:e.buildParameterized}):this._buildString(c,h,{buildParameterized:e.buildParameterized});r+=v.text,n.push.apply(n,t(v.values))}}catch(d){o=!0,l=d}finally{try{!a&&s["return"]&&s["return"]()}finally{if(o)throw l}}return{text:r.length?this.options.verb+" ("+r+")":"",values:n}}}]),o}(p.Block),p.WhereBlock=function(t){function i(t){return n(this,i),e(this,Object.getPrototypeOf(i).call(this,a({},t,{verb:"WHERE"})))}return r(i,t),d(i,[{key:"where",value:function(t,e){for(var r=arguments.length,n=Array(r>2?r-2:0),i=2;r>i;i++)n[i-2]=arguments[i];var a=["=","<",">","<=",">=","<>","!=","in","not in","like","not like"];if("string"==typeof t&&"string"==typeof e&&-1!=a.indexOf(e.toLowerCase())){var o=new p.Expression(this.options);this._condition(o._add("AND",t,e,n))}else this._condition.apply(this,arguments)}}]),i}(p.AbstractConditionBlock),p.HavingBlock=function(t){function i(t){return n(this,i),e(this,Object.getPrototypeOf(i).call(this,a({},t,{verb:"HAVING"})))}return r(i,t),d(i,[{key:"having",value:function(t,e){for(var r=arguments.length,n=Array(r>2?r-2:0),i=2;r>i;i++)n[i-2]=arguments[i];var a=["=","<",">","<=",">=","<>","!=","in","not in","like","not like"];if("string"==typeof t&&"string"==typeof e&&-1!=a.indexOf(e.toLowerCase())){var o=new p.Expression(this.options);this._condition(o._add("AND",t,e,n))}else this._condition.apply(this,arguments)}}]),i}(p.AbstractConditionBlock),p.OrderByBlock=function(a){function o(t){n(this,o);var r=e(this,Object.getPrototypeOf(o).call(this,t));return r._orders=[],r}return r(o,a),d(o,[{key:"order",value:function(t,e){for(var r=arguments.length,n=Array(r>2?r-2:0),i=2;r>i;i++)n[i-2]=arguments[i];t=this._sanitizeField(t),e=void 0===e?!0:e,e=null!==e?!!e:e,this._orders.push({field:t,dir:e,values:n})}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r="",n=[],a=!0,o=!1,l=void 0;try{for(var u,s=this._orders[Symbol.iterator]();!(a=(u=s.next()).done);a=!0){var f=u.value,c=f.field,h=f.dir,v=f.values;r=i(r,", ");var d=this._buildString(c,v,{buildParameterized:e.buildParameterized});r+=d.text,n.push.apply(n,t(d.values)),null!==h&&(r+=" "+(h?"ASC":"DESC"))}}catch(y){o=!0,l=y}finally{try{!a&&s["return"]&&s["return"]()}finally{if(o)throw l}}return{text:r.length?"ORDER BY "+r:"",values:n}}}]),o}(p.Block),p.LimitBlock=function(t){function i(t){n(this,i);var r=e(this,Object.getPrototypeOf(i).call(this,t));return r._limit=null,r}return r(i,t),d(i,[{key:"limit",value:function(t){this._limit=this._sanitizeLimitOffset(t)}},{key:"_toParamString",value:function(){return{text:null!==this._limit?"LIMIT "+this._limit:"",values:[]}}}]),i}(p.Block),p.JoinBlock=function(a){function o(t){n(this,o);var r=e(this,Object.getPrototypeOf(o).call(this,t));return r._joins=[],r}return r(o,a),d(o,[{key:"join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],r=arguments.length<=2||void 0===arguments[2]?null:arguments[2],n=arguments.length<=3||void 0===arguments[3]?"INNER":arguments[3];t=this._sanitizeTable(t,!0),e=e?this._sanitizeTableAlias(e):e,r=r?this._sanitizeExpression(r):r,this._joins.push({type:n,table:t,alias:e,condition:r})}},{key:"left_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],r=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,r,"LEFT")}},{key:"right_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],r=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,r,"RIGHT")}},{key:"outer_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],r=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,r,"OUTER")}},{key:"left_outer_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],r=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,r,"LEFT OUTER")}},{key:"full_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],r=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,r,"FULL")}},{key:"cross_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],r=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,r,"CROSS")}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r="",n=[],a=!0,o=!1,l=void 0;try{for(var u,s=this._joins[Symbol.iterator]();!(a=(u=s.next()).done);a=!0){var f=u.value,c=f.type,h=f.table,v=f.alias,d=f.condition;r=i(r,this.options.separator);var y=void 0;if(h instanceof p.BaseBuilder){var g=h._toParamString({buildParameterized:e.buildParameterized,nested:!0});n.push.apply(n,t(g.values)),y=g.text}else y=this._formatTableName(h);if(r+=c+" JOIN "+y,v&&(r+=" "+this._formatTableAlias(v)),d){r+=" ON ";var _=void 0;_=d instanceof p.Expression?d._toParamString({buildParameterized:e.buildParameterized}):this._buildString(d,[],{buildParameterized:e.buildParameterized}),r+=this._applyNestingFormatting(_.text),n.push.apply(n,t(_.values))}}}catch(m){o=!0,l=m}finally{try{!a&&s["return"]&&s["return"]()}finally{if(o)throw l}}return{text:r,values:n}}}]),o}(p.Block),p.UnionBlock=function(a){function o(t){n(this,o);var r=e(this,Object.getPrototypeOf(o).call(this,t));return r._unions=[],r}return r(o,a),d(o,[{key:"union",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?"UNION":arguments[1];t=this._sanitizeTable(t),this._unions.push({type:e,table:t})}},{key:"union_all",value:function(t){this.union(t,"UNION ALL")}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r="",n=[],a=!0,o=!1,l=void 0;try{for(var u,s=this._unions[Symbol.iterator]();!(a=(u=s.next()).done);a=!0){var f=u.value,c=f.type,h=f.table;r=i(r,this.options.separator);var v=void 0;if(h instanceof p.BaseBuilder){var d=h._toParamString({buildParameterized:e.buildParameterized,nested:!0});v=d.text,n.push.apply(n,t(d.values))}else r=this._formatTableName(h);r+=c+" "+v}}catch(y){o=!0,l=y}finally{try{!a&&s["return"]&&s["return"]()}finally{if(o)throw l}}return{text:r,values:n}}}]),o}(p.Block),p.QueryBuilder=function(i){function o(t,r){n(this,o);var i=e(this,Object.getPrototypeOf(o).call(this,t));i.blocks=r||[];var a=!0,l=!1,u=void 0;try{for(var s,f=i.blocks[Symbol.iterator]();!(a=(s=f.next()).done);a=!0){var c=s.value,h=c.exposedMethods();for(var v in h){var d=h[v];if(void 0!==i[v])throw new Error("Builder already has a builder method called: "+v);!function(t,e,r){i[e]=function(){for(var e=arguments.length,n=Array(e),a=0;e>a;a++)n[a]=arguments[a];return r.call.apply(r,[t].concat(n)),i}}(c,v,d)}}}catch(y){l=!0,u=y}finally{try{!a&&f["return"]&&f["return"]()}finally{if(l)throw u}}return i}return r(o,i),d(o,[{key:"registerValueHandler",value:function(t,e){var r=!0,n=!1,i=void 0;try{for(var a,l=this.blocks[Symbol.iterator]();!(r=(a=l.next()).done);r=!0){var u=a.value;u.registerValueHandler(t,e)}}catch(s){n=!0,i=s}finally{try{!r&&l["return"]&&l["return"]()}finally{if(n)throw i}}return v(Object.getPrototypeOf(o.prototype),"registerValueHandler",this).call(this,t,e),this}},{key:"updateOptions",value:function(t){this.options=a({},this.options,t);var e=!0,r=!1,n=void 0;try{for(var i,o=this.blocks[Symbol.iterator]();!(e=(i=o.next()).done);e=!0){var l=i.value;l.options=a({},l.options,t)}}catch(u){r=!0,n=u}finally{try{!e&&o["return"]&&o["return"]()}finally{if(r)throw n}}}},{key:"_toParamString",value:function(){var e,r=this,n=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];n=a({},this.options,n);var i=this.blocks.map(function(t){return t._toParamString({buildParameterized:n.buildParameterized,queryBuilder:r})}),o=i.map(function(t){return t.text}),l=i.map(function(t){return t.values}),u=o.filter(function(t){return 01?e-1:0),n=1;e>n;n++)r[n-1]=arguments[n];if(t&&r){var i=!0,a=!1,o=void 0;try{for(var l,s=function(){var e=l.value;"object"===("undefined"==typeof e?"undefined":y(e))&&Object.getOwnPropertyNames(e).forEach(function(r){"function"!=typeof e[r]&&(t[r]=e[r])})},u=r[Symbol.iterator]();!(i=(l=u.next()).done);i=!0)s()}catch(f){a=!0,o=f}finally{try{!i&&u["return"]&&u["return"]()}finally{if(a)throw o}}}return t}function o(t){return t&&t.constructor.prototype===Object.prototype}function l(t){return t&&t.constructor.prototype===Array.prototype}function s(t){if(t&&t.constructor&&t.constructor.toString){var e=t.constructor.toString().match(/function\s*(\w+)/);if(e&&2===e.length)return e[1]}}function u(t){if(!t)return t;if("function"==typeof t.clone)return t.clone();if(!o(t)&&!l(t))return JSON.parse(JSON.stringify(t));var e=function(){var e=new t.constructor;return Object.getOwnPropertyNames(t).forEach(function(r){"function"!=typeof t[r]&&(e[r]=u(t[r]))}),{v:e}}();return"object"===("undefined"==typeof e?"undefined":y(e))?e.v:void 0}function f(t,e,r){var n="undefined"==typeof e?"undefined":y(e);if("function"!==n&&"string"!==n)throw new Error("type must be a class constructor or string");if("function"!=typeof r)throw new Error("handler must be a function");for(var i in t){var a=t[i];if(a.type===e)return void(a.handler=r)}t.push({type:e,handler:r})}function c(t){for(var e=arguments.length,r=Array(e>1?e-1:0),n=1;e>n;n++)r[n-1]=arguments[n];for(var i in r){var a=r[i];for(var o in a){var l=a[o];if(("undefined"==typeof t?"undefined":y(t))===l.type||"string"!=typeof l.type&&t instanceof l.type)return l.handler}}}function h(){var h=arguments.length<=0||void 0===arguments[0]?null:arguments[0],p={_getObjectClassName:s};p.DefaultQueryBuilderOptions={autoQuoteTableNames:!1,autoQuoteFieldNames:!1,autoQuoteAliasNames:!0,useAsForTableAliasNames:!1,nameQuoteCharacter:"`",tableAliasQuoteCharacter:"`",fieldAliasQuoteCharacter:'"',valueHandlers:[],parameterCharacter:"?",numberedParameters:!1,numberedParametersPrefix:"$",numberedParametersStartAt:1,replaceSingleQuotes:!1,singleQuoteReplacement:"''",separator:" "},p.globalValueHandlers=[],p.registerValueHandler=function(t,e){f(p.globalValueHandlers,t,e)},p.Cloneable=function(){function t(){n(this,t)}return d(t,[{key:"clone",value:function(){var t=new this.constructor;return a(t,u(a({},this)))}}]),t}(),p.BaseBuilder=function(i){function o(t){n(this,o);var r=e(this,Object.getPrototypeOf(o).call(this)),i=JSON.parse(JSON.stringify(p.DefaultQueryBuilderOptions));return r.options=a({},i,t),r}return r(o,i),d(o,[{key:"registerValueHandler",value:function(t,e){return f(this.options.valueHandlers,t,e),this}},{key:"_sanitizeExpression",value:function(t){if(!(t instanceof p.Expression)&&"string"!=typeof t)throw new Error("expression must be a string or Expression instance");return t}},{key:"_sanitizeName",value:function(t,e){if("string"!=typeof t)throw new Error(e+" must be a string");return t}},{key:"_sanitizeField",value:function(t){return t instanceof p.BaseBuilder||(t=this._sanitizeName(t,"field name")),t}},{key:"_sanitizeQueryBuilder",value:function(t){if(t instanceof p.QueryBuilder)return t;throw new Error("must be a QueryBuilder instance")}},{key:"_sanitizeTable",value:function(t){if("string"!=typeof t)try{t=this._sanitizeQueryBuilder(t)}catch(e){throw new Error("table name must be a string or a query builder")}else t=this._sanitizeName(t,"table");return t}},{key:"_sanitizeTableAlias",value:function(t){return this._sanitizeName(t,"table alias")}},{key:"_sanitizeFieldAlias",value:function(t){return this._sanitizeName(t,"field alias")}},{key:"_sanitizeLimitOffset",value:function(t){if(t=parseInt(t),0>t||isNaN(t))throw new Error("limit/offset must be >= 0");return t}},{key:"_sanitizeValue",value:function(t){var e="undefined"==typeof t?"undefined":y(t);if(null===t);else if("string"===e||"number"===e||"boolean"===e);else if(t instanceof p.BaseBuilder);else{var r=!!c(t,this.options.valueHandlers,p.globalValueHandlers);if(!r)throw new Error("field value must be a string, number, boolean, null or one of the registered custom value types")}return t}},{key:"_escapeValue",value:function(t){return this.options.replaceSingleQuotes?t.replace(/\'/g,this.options.singleQuoteReplacement):t}},{key:"_formatTableName",value:function(t){if(this.options.autoQuoteTableNames){var e=this.options.nameQuoteCharacter;t=""+e+t+e}return t}},{key:"_formatFieldAlias",value:function(t){if(this.options.autoQuoteAliasNames){var e=this.options.fieldAliasQuoteCharacter;t=""+e+t+e}return t}},{key:"_formatTableAlias",value:function(t){if(this.options.autoQuoteAliasNames){var e=this.options.tableAliasQuoteCharacter;t=""+e+t+e}return this.options.useAsForTableAliasNames?"AS "+t:t}},{key:"_formatFieldName",value:function(t){var e=this,r=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];return this.options.autoQuoteFieldNames&&!function(){var n=e.options.nameQuoteCharacter;t=r.ignorePeriodsForFieldNameQuotes?""+n+t+n:t.split(".").map(function(t){return"*"===t?t:""+n+t+n}).join(".")}(),t}},{key:"_formatCustomValue",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?!1:arguments[1],r=c(t,this.options.valueHandlers,p.globalValueHandlers);return r&&(t=r(t,e)),t}},{key:"_formatValueForParamArray",value:function(t){var e=this;return l(t)?t.map(function(t){return e._formatValueForParamArray(t)}):this._formatCustomValue(t,!0)}},{key:"_formatValueForQueryString",value:function(t){var e=this,r=arguments.length<=1||void 0===arguments[1]?{}:arguments[1],n=this._formatCustomValue(t);if(n!==t)return this._applyNestingFormatting(n);if(l(t))t=t.map(function(t){return e._formatValueForQueryString(t)}),t=this._applyNestingFormatting(t.join(", "));else{var i="undefined"==typeof t?"undefined":y(t);if(null===t)t="NULL";else if("boolean"===i)t=t?"TRUE":"FALSE";else if(t instanceof p.BaseBuilder)t=this._applyNestingFormatting(t.toString());else if("number"!==i)if(r.dontQuote)t=""+t;else{var a=this._escapeValue(t);t="'"+a+"'"}}return t}},{key:"_applyNestingFormatting",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?!0:arguments[1];return t&&"string"==typeof t&&e&&("("!==t.charAt(0)||")"!==t.charAt(t.length-1))?"("+t+")":t}},{key:"_buildString",value:function(e,r){var n=arguments.length<=2||void 0===arguments[2]?{}:arguments[2],i=n.nested,a=n.buildParameterized,o=n.formattingOptions;r=r||[],e=e||"";for(var s="",u=-1,f=[],c=this.options.parameterCharacter,h=0;e.length>h;)if(e.substr(h,c.length)===c){var v=r[++u];if(a)if(v instanceof p.BaseBuilder){var d=v._toParamString({buildParameterized:a,nested:!0});s+=d.text,f.push.apply(f,t(d.values))}else if(v=this._formatValueForParamArray(v),l(v)){var y=v.map(function(){return c}).join(", ");s+="("+y+")",f.push.apply(f,t(v))}else s+=c,f.push(v);else s+=this._formatValueForQueryString(v,o);h+=c.length}else s+=e.charAt(h),h++;return{text:this._applyNestingFormatting(s,!!i),values:f}}},{key:"_buildManyStrings",value:function(e,r){var n=arguments.length<=2||void 0===arguments[2]?{}:arguments[2],i=[],a=[];for(var o in e){var l=e[o],s=r[o],u=this._buildString(l,s,{buildParameterized:n.buildParameterized,nested:!1}),f=u.text,c=u.values;i.push(f),a.push.apply(a,t(c))}return i=i.join(this.options.separator),{text:i.length?this._applyNestingFormatting(i,!!n.nested):"",values:a}}},{key:"_toParamString",value:function(t){throw new Error("Not yet implemented")}},{key:"toString",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return this._toParamString(t).text}},{key:"toParam",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return this._toParamString(a({},t,{buildParameterized:!0}))}}]),o}(p.Cloneable),p.Expression=function(i){function a(t){n(this,a);var r=e(this,Object.getPrototypeOf(a).call(this,t));return r._nodes=[],r}return r(a,i),d(a,[{key:"and",value:function(t,e){for(var r=arguments.length,n=Array(r>2?r-2:0),i=2;r>i;i++)n[i-2]=arguments[i];return this._add("AND",t,e,n)}},{key:"or",value:function(t,e){for(var r=arguments.length,n=Array(r>2?r-2:0),i=2;r>i;i++)n[i-2]=arguments[i];return this._add("OR",t,e,n)}},{key:"_add",value:function(t,e,r,n){var i=["=","<",">","<=",">=","<>","!=","in","not in","like","not like","is","is not"],a=void 0;return"string"==typeof e&&"string"==typeof r&&-1!=i.indexOf(r.toLowerCase())?a=this._buildExpression(e,r):(a=e,n.unshift(r)),this._sanitizeExpression(e),this._nodes.push({type:t,expr:a,para:n}),this}},{key:"_buildExpression",value:function(t,e){var r=this._formatFieldName(t),n=this.options.parameterCharacter,i=r+" "+e+" "+n;return i}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r=[],n=[],i=!0,a=!1,o=void 0;try{for(var l,s=this._nodes[Symbol.iterator]();!(i=(l=s.next()).done);i=!0){var u=l.value,f=u.type,c=u.expr,h=u.para,v=c instanceof p.Expression?c._toParamString({buildParameterized:e.buildParameterized,nested:!0}):this._buildString(c,h,{buildParameterized:e.buildParameterized}),d=v.text,y=v.values;r.length&&r.push(f),r.push(d),n.push.apply(n,t(y))}}catch(g){a=!0,o=g}finally{try{!i&&s["return"]&&s["return"]()}finally{if(a)throw o}}return r=r.join(" "),{text:this._applyNestingFormatting(r,!!e.nested),values:n}}}]),a}(p.BaseBuilder),p.Case=function(l){function s(t){var r=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];n(this,s);var i=e(this,Object.getPrototypeOf(s).call(this,r));return o(t)&&(r=t,t=null),t&&(i._fieldName=i._sanitizeField(t)),i.options=a({},p.DefaultQueryBuilderOptions,r),i._cases=[],i._elseValue=null,i}return r(s,l),d(s,[{key:"when",value:function(t){for(var e=arguments.length,r=Array(e>1?e-1:0),n=1;e>n;n++)r[n-1]=arguments[n];return this._cases.unshift({expression:t,values:r}),this}},{key:"then",value:function(t){if(0==this._cases.length)throw new Error("when() needs to be called first");return this._cases[0].result=t,this}},{key:"else",value:function(t){return this._elseValue=t,this}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r="",n=[],a=!0,o=!1,l=void 0;try{for(var s,u=this._cases[Symbol.iterator]();!(a=(s=u.next()).done);a=!0){var f=s.value,c=f.expression,h=f.values,v=f.result;r=i(r," ");var d=this._buildString(c,h,{buildParameterized:e.buildParameterized,nested:!0});r+="WHEN "+d.text+" THEN "+this._formatValueForQueryString(v),n.push.apply(n,t(d.values))}}catch(y){o=!0,l=y}finally{try{!a&&u["return"]&&u["return"]()}finally{if(o)throw l}}return r.length?(r+=" ELSE "+this._formatValueForQueryString(this._elseValue)+" END",this._fieldName&&(r=this._fieldName+" "+r),r="CASE "+r):r=this._formatValueForQueryString(this._elseValue),{text:r,values:n}}}]),s}(p.BaseBuilder),p.Block=function(t){function i(t){return n(this,i),e(this,Object.getPrototypeOf(i).call(this,t))}return r(i,t),d(i,[{key:"exposedMethods",value:function(){for(var t={},e=this;e;)Object.getOwnPropertyNames(e).forEach(function(r){"constructor"===r||"function"!=typeof e[r]||"_"===r.charAt(0)||p.Block.prototype[r]||(t[r]=e[r])}),e=Object.getPrototypeOf(e);return t}}]),i}(p.BaseBuilder),p.StringBlock=function(t){function i(t,r){n(this,i);var a=e(this,Object.getPrototypeOf(i).call(this,t));return a._str=r,a}return r(i,t),d(i,[{key:"_toParamString",value:function(){arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return{text:this._str,values:[]}}}]),i}(p.Block),p.FunctionBlock=function(t){function i(t){n(this,i);var r=e(this,Object.getPrototypeOf(i).call(this,t));return r._strings=[],r._values=[],r}return r(i,t),d(i,[{key:"function",value:function(t){this._strings.push(t);for(var e=arguments.length,r=Array(e>1?e-1:0),n=1;e>n;n++)r[n-1]=arguments[n];this._values.push(r)}},{key:"_toParamString",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return this._buildManyStrings(this._strings,this._values,t)}}]),i}(p.Block),p.registerValueHandler(p.FunctionBlock,function(t){var e=arguments.length<=1||void 0===arguments[1]?!1:arguments[1];return e?t.toParam():t.toString()}),p.AbstractTableBlock=function(a){function o(t,r){n(this,o);var i=e(this,Object.getPrototypeOf(o).call(this,t));return i._tables=[],i}return r(o,a),d(o,[{key:"_table",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1];e=e?this._sanitizeTableAlias(e):e,t=this._sanitizeTable(t),this.options.singleTable&&(this._tables=[]),this._tables.push({table:t,alias:e})}},{key:"_hasTable",value:function(){return 01)throw new Error("Cannot set multiple rows of fields this way.");"undefined"!=typeof e&&(e=this._sanitizeValue(e)),t=this._sanitizeField(t);var n=this._fields.indexOf(t);-1===n&&(this._fields.push(t),n=this._fields.length-1),this._values[0][n]=e,this._valueOptions[0][n]=r}},{key:"_setFields",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];if("object"!==("undefined"==typeof t?"undefined":y(t)))throw new Error("Expected an object but got "+("undefined"==typeof t?"undefined":y(t)));for(var r in t)this._set(r,t[r],e)}},{key:"_setFieldsRows",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];if(!l(t))throw new Error("Expected an array of objects but got "+("undefined"==typeof t?"undefined":y(t)));this._reset();for(var r in t){var n=t[r];for(var i in n){var a=n[i];i=this._sanitizeField(i),a=this._sanitizeValue(a);var o=this._fields.indexOf(i);if(r>0&&-1===o)throw new Error("All fields in subsequent rows must match the fields in the first row");-1===o&&(this._fields.push(i),o=this._fields.length-1),l(this._values[r])||(this._values[r]=[],this._valueOptions[r]=[]),this._values[r][o]=a,this._valueOptions[r][o]=e}}}}]),i}(p.Block),p.SetFieldBlock=function(a){function o(){return n(this,o),e(this,Object.getPrototypeOf(o).apply(this,arguments))}return r(o,a),d(o,[{key:"set",value:function(t,e,r){this._set(t,e,r)}},{key:"setFields",value:function(t,e){this._setFields(t,e)}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r=e.buildParameterized;if(0>=this._fields.length)throw new Error("set() needs to be called");var n="",a=[];for(var o in this._fields){n=i(n,", ");var l=this._fields[o],s=this._values[0][o];if("undefined"==typeof s)n+=l;else{var u=this._buildString(l+" = "+this.options.parameterCharacter,[s],{buildParameterized:r,formattingOptions:this._valueOptions[0][o]});n+=u.text,a.push.apply(a,t(u.values))}}return{text:"SET "+n,values:a}}}]),o}(p.AbstractSetFieldBlock),p.InsertFieldValueBlock=function(a){function o(){return n(this,o),e(this,Object.getPrototypeOf(o).apply(this,arguments))}return r(o,a),d(o,[{key:"set",value:function(t,e){var r=arguments.length<=2||void 0===arguments[2]?{}:arguments[2];this._set(t,e,r)}},{key:"setFields",value:function(t,e){this._setFields(t,e)}},{key:"setFieldsRows",value:function(t,e){this._setFieldsRows(t,e)}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r=e.buildParameterized,n=this._fields.join(", "),a=[],o=[];for(var l in this._values){a[l]="";for(var s in this._values[l]){var u=this._buildString(this.options.parameterCharacter,[this._values[l][s]],{buildParameterized:r,formattingOptions:this._valueOptions[l][s]});o.push.apply(o,t(u.values)),a[l]=i(a[l],", "),a[l]+=u.text}}return{text:n.length?"("+n+") VALUES ("+a.join("), (")+")":"",values:o}}}]),o}(p.AbstractSetFieldBlock),p.InsertFieldsFromQueryBlock=function(t){function i(t){n(this,i);var r=e(this,Object.getPrototypeOf(i).call(this,t));return r._fields=[],r._query=null,r}return r(i,t),d(i,[{key:"fromQuery",value:function(t,e){var r=this;this._fields=t.map(function(t){return r._sanitizeField(t)}),this._query=this._sanitizeQueryBuilder(e)}},{key:"_toParamString",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],e="",r=[];if(this._fields.length&&this._query){var n=this._query._toParamString({buildParameterized:t.buildParameterized,nested:!0}),i=n.text,a=n.values;e="("+this._fields.join(", ")+") ("+i+")",r=a}return{text:e,values:r}}}]),i}(p.Block),p.DistinctBlock=function(t){function i(){return n(this,i),e(this,Object.getPrototypeOf(i).apply(this,arguments))}return r(i,t),d(i,[{key:"distinct",value:function(){this._useDistinct=!0}},{key:"_toParamString",value:function(){return{text:this._useDistinct?"DISTINCT":"",values:[]}}}]),i}(p.Block),p.GroupByBlock=function(t){function i(t){n(this,i);var r=e(this,Object.getPrototypeOf(i).call(this,t));return r._groups=[],r}return r(i,t),d(i,[{key:"group",value:function(t){this._groups.push(this._sanitizeField(t))}},{key:"_toParamString",value:function(){arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return{text:this._groups.length?"GROUP BY "+this._groups.join(", "):"",values:[]}}}]),i}(p.Block),p.OffsetBlock=function(t){function i(t){n(this,i);var r=e(this,Object.getPrototypeOf(i).call(this,t));return r._offsets=null,r}return r(i,t),d(i,[{key:"offset",value:function(t){this._offsets=this._sanitizeLimitOffset(t)}},{key:"_toParamString",value:function(){return{text:this._offsets?"OFFSET "+this._offsets:"",values:[]}}}]),i}(p.Block),p.AbstractConditionBlock=function(a){function o(t){n(this,o);var r=e(this,Object.getPrototypeOf(o).call(this,t));return r._conditions=[],r}return r(o,a),d(o,[{key:"_condition",value:function(t){for(var e=arguments.length,r=Array(e>1?e-1:0),n=1;e>n;n++)r[n-1]=arguments[n];t=this._sanitizeExpression(t),this._conditions.push({expr:t,values:r})}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r="",n=[],a=!0,o=!1,l=void 0;try{for(var s,u=this._conditions[Symbol.iterator]();!(a=(s=u.next()).done);a=!0){var f=s.value,c=f.expr,h=f.values;r=i(r,") AND (");var v=c instanceof p.Expression?c._toParamString({buildParameterized:e.buildParameterized}):this._buildString(c,h,{buildParameterized:e.buildParameterized});r+=v.text,n.push.apply(n,t(v.values))}}catch(d){o=!0,l=d}finally{try{!a&&u["return"]&&u["return"]()}finally{if(o)throw l}}return{text:r.length?this.options.verb+" ("+r+")":"",values:n}}}]),o}(p.Block),p.WhereBlock=function(t){function i(t){return n(this,i),e(this,Object.getPrototypeOf(i).call(this,a({},t,{verb:"WHERE"})))}return r(i,t),d(i,[{key:"where",value:function(t,e){for(var r=arguments.length,n=Array(r>2?r-2:0),i=2;r>i;i++)n[i-2]=arguments[i];var a=["=","<",">","<=",">=","<>","!=","in","not in","like","not like","is","is not"];if("string"==typeof t&&"string"==typeof e&&-1!=a.indexOf(e.toLowerCase())){var o=new p.Expression(this.options);this._condition(o._add("AND",t,e,n))}else this._condition.apply(this,arguments)}}]),i}(p.AbstractConditionBlock),p.HavingBlock=function(t){function i(t){return n(this,i),e(this,Object.getPrototypeOf(i).call(this,a({},t,{verb:"HAVING"})))}return r(i,t),d(i,[{key:"having",value:function(t,e){for(var r=arguments.length,n=Array(r>2?r-2:0),i=2;r>i;i++)n[i-2]=arguments[i];var a=["=","<",">","<=",">=","<>","!=","in","not in","like","not like","is","is not"];if("string"==typeof t&&"string"==typeof e&&-1!=a.indexOf(e.toLowerCase())){var o=new p.Expression(this.options);this._condition(o._add("AND",t,e,n))}else this._condition.apply(this,arguments)}}]),i}(p.AbstractConditionBlock),p.OrderByBlock=function(a){function o(t){n(this,o);var r=e(this,Object.getPrototypeOf(o).call(this,t));return r._orders=[],r}return r(o,a),d(o,[{key:"order",value:function(t,e){for(var r=arguments.length,n=Array(r>2?r-2:0),i=2;r>i;i++)n[i-2]=arguments[i];t=this._sanitizeField(t),e=void 0===e?!0:e,e=null!==e?!!e:e,this._orders.push({field:t,dir:e,values:n})}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r="",n=[],a=!0,o=!1,l=void 0;try{for(var s,u=this._orders[Symbol.iterator]();!(a=(s=u.next()).done);a=!0){var f=s.value,c=f.field,h=f.dir,v=f.values;r=i(r,", ");var d=this._buildString(c,v,{buildParameterized:e.buildParameterized});r+=d.text,n.push.apply(n,t(d.values)),null!==h&&(r+=" "+(h?"ASC":"DESC"))}}catch(y){o=!0,l=y}finally{try{!a&&u["return"]&&u["return"]()}finally{if(o)throw l}}return{text:r.length?"ORDER BY "+r:"",values:n}}}]),o}(p.Block),p.LimitBlock=function(t){function i(t){n(this,i);var r=e(this,Object.getPrototypeOf(i).call(this,t));return r._limit=null,r}return r(i,t),d(i,[{key:"limit",value:function(t){this._limit=this._sanitizeLimitOffset(t)}},{key:"_toParamString",value:function(){return{text:null!==this._limit?"LIMIT "+this._limit:"",values:[]}}}]),i}(p.Block),p.JoinBlock=function(a){function o(t){n(this,o);var r=e(this,Object.getPrototypeOf(o).call(this,t));return r._joins=[],r}return r(o,a),d(o,[{key:"join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],r=arguments.length<=2||void 0===arguments[2]?null:arguments[2],n=arguments.length<=3||void 0===arguments[3]?"INNER":arguments[3];t=this._sanitizeTable(t,!0),e=e?this._sanitizeTableAlias(e):e,r=r?this._sanitizeExpression(r):r,this._joins.push({type:n,table:t,alias:e,condition:r})}},{key:"left_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],r=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,r,"LEFT")}},{key:"right_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],r=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,r,"RIGHT")}},{key:"outer_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],r=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,r,"OUTER")}},{key:"left_outer_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],r=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,r,"LEFT OUTER")}},{key:"full_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],r=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,r,"FULL")}},{key:"cross_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],r=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,r,"CROSS")}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r="",n=[],a=!0,o=!1,l=void 0;try{for(var s,u=this._joins[Symbol.iterator]();!(a=(s=u.next()).done);a=!0){var f=s.value,c=f.type,h=f.table,v=f.alias,d=f.condition;r=i(r,this.options.separator);var y=void 0;if(h instanceof p.BaseBuilder){var g=h._toParamString({buildParameterized:e.buildParameterized,nested:!0});n.push.apply(n,t(g.values)),y=g.text}else y=this._formatTableName(h);if(r+=c+" JOIN "+y,v&&(r+=" "+this._formatTableAlias(v)),d){r+=" ON ";var _=void 0;_=d instanceof p.Expression?d._toParamString({buildParameterized:e.buildParameterized}):this._buildString(d,[],{buildParameterized:e.buildParameterized}),r+=this._applyNestingFormatting(_.text),n.push.apply(n,t(_.values))}}}catch(m){o=!0,l=m}finally{try{!a&&u["return"]&&u["return"]()}finally{if(o)throw l}}return{text:r,values:n}}}]),o}(p.Block),p.UnionBlock=function(a){function o(t){n(this,o);var r=e(this,Object.getPrototypeOf(o).call(this,t));return r._unions=[],r}return r(o,a),d(o,[{key:"union",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?"UNION":arguments[1];t=this._sanitizeTable(t),this._unions.push({type:e,table:t})}},{key:"union_all",value:function(t){this.union(t,"UNION ALL")}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r="",n=[],a=!0,o=!1,l=void 0;try{for(var s,u=this._unions[Symbol.iterator]();!(a=(s=u.next()).done);a=!0){var f=s.value,c=f.type,h=f.table;r=i(r,this.options.separator);var v=void 0;if(h instanceof p.BaseBuilder){var d=h._toParamString({buildParameterized:e.buildParameterized,nested:!0});v=d.text,n.push.apply(n,t(d.values))}else r=this._formatTableName(h);r+=c+" "+v}}catch(y){o=!0,l=y}finally{try{!a&&u["return"]&&u["return"]()}finally{if(o)throw l}}return{text:r,values:n}}}]),o}(p.Block),p.QueryBuilder=function(i){function o(t,r){n(this,o);var i=e(this,Object.getPrototypeOf(o).call(this,t));i.blocks=r||[];var a=!0,l=!1,s=void 0;try{for(var u,f=i.blocks[Symbol.iterator]();!(a=(u=f.next()).done);a=!0){var c=u.value,h=c.exposedMethods();for(var v in h){var d=h[v];if(void 0!==i[v])throw new Error("Builder already has a builder method called: "+v);!function(t,e,r){i[e]=function(){for(var e=arguments.length,n=Array(e),a=0;e>a;a++)n[a]=arguments[a];return r.call.apply(r,[t].concat(n)),i}}(c,v,d)}}}catch(y){l=!0,s=y}finally{try{!a&&f["return"]&&f["return"]()}finally{if(l)throw s}}return i}return r(o,i),d(o,[{key:"registerValueHandler",value:function(t,e){var r=!0,n=!1,i=void 0;try{for(var a,l=this.blocks[Symbol.iterator]();!(r=(a=l.next()).done);r=!0){var s=a.value;s.registerValueHandler(t,e)}}catch(u){n=!0,i=u}finally{try{!r&&l["return"]&&l["return"]()}finally{if(n)throw i}}return v(Object.getPrototypeOf(o.prototype),"registerValueHandler",this).call(this,t,e),this}},{key:"updateOptions",value:function(t){this.options=a({},this.options,t);var e=!0,r=!1,n=void 0;try{for(var i,o=this.blocks[Symbol.iterator]();!(e=(i=o.next()).done);e=!0){var l=i.value;l.options=a({},l.options,t)}}catch(s){r=!0,n=s}finally{try{!e&&o["return"]&&o["return"]()}finally{if(r)throw n}}}},{key:"_toParamString",value:function(){var e,r=this,n=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];n=a({},this.options,n);var i=this.blocks.map(function(t){return t._toParamString({buildParameterized:n.buildParameterized,queryBuilder:r})}),o=i.map(function(t){return t.text}),l=i.map(function(t){return t.values}),s=o.filter(function(t){return 0', '<=', '>=', '<>', '!=', 'in', 'not in', 'like', 'not like']; + var validOperators = ['=', '<', '>', '<=', '>=', '<>', '!=', 'in', 'not in', 'like', 'not like', 'is', 'is not']; var expr = void 0; if (typeof field === 'string' && typeof operator === 'string' && -1 != validOperators.indexOf(operator.toLowerCase())) { expr = this._buildExpression(field, operator); @@ -2111,7 +2111,7 @@ function _buildSquel() { values[_key8 - 2] = arguments[_key8]; } - var validOperators = ['=', '<', '>', '<=', '>=', '<>', '!=', 'in', 'not in', 'like', 'not like']; + var validOperators = ['=', '<', '>', '<=', '>=', '<>', '!=', 'in', 'not in', 'like', 'not like', 'is', 'is not']; if (typeof field === 'string' && typeof operator === 'string' && -1 != validOperators.indexOf(operator.toLowerCase())) { // key - operator - value var expr = new cls.Expression(this.options); @@ -2145,7 +2145,7 @@ function _buildSquel() { values[_key9 - 2] = arguments[_key9]; } - var validOperators = ['=', '<', '>', '<=', '>=', '<>', '!=', 'in', 'not in', 'like', 'not like']; + var validOperators = ['=', '<', '>', '<=', '>=', '<>', '!=', 'in', 'not in', 'like', 'not like', 'is', 'is not']; if (typeof field === 'string' && typeof operator === 'string' && -1 != validOperators.indexOf(operator.toLowerCase())) { // key - operator - value var expr = new cls.Expression(this.options); diff --git a/squel.min.js b/squel.min.js index d7108e9f..e0abd048 100644 --- a/squel.min.js +++ b/squel.min.js @@ -1,2 +1,2 @@ -/*! squel | https://github.com/hiddentao/squel | BSD license */!function(t,e){"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?module.exports=e():t.squel=e()}(this,function(){"use strict";function t(t){if(Array.isArray(t)){for(var e=0,n=Array(t.length);e1?e-1:0),r=1;e>r;r++)n[r-1]=arguments[r];if(t&&n){var i=!0,o=!1,a=void 0;try{for(var l,u=function(){var e=l.value;"object"===("undefined"==typeof e?"undefined":p(e))&&Object.getOwnPropertyNames(e).forEach(function(n){"function"!=typeof e[n]&&(t[n]=e[n])})},s=n[Symbol.iterator]();!(i=(l=s.next()).done);i=!0)u()}catch(c){o=!0,a=c}finally{try{!i&&s["return"]&&s["return"]()}finally{if(o)throw a}}}return t}function a(t){return t&&t.constructor.prototype===Object.prototype}function l(t){return t&&t.constructor.prototype===Array.prototype}function u(t){if(t&&t.constructor&&t.constructor.toString){var e=t.constructor.toString().match(/function\s*(\w+)/);if(e&&2===e.length)return e[1]}}function s(t){if(!t)return t;if("function"==typeof t.clone)return t.clone();if(!a(t)&&!l(t))return JSON.parse(JSON.stringify(t));var e=function(){var e=new t.constructor;return Object.getOwnPropertyNames(t).forEach(function(n){"function"!=typeof t[n]&&(e[n]=s(t[n]))}),{v:e}}();return"object"===("undefined"==typeof e?"undefined":p(e))?e.v:void 0}function c(t,e,n){var r="undefined"==typeof e?"undefined":p(e);if("function"!==r&&"string"!==r)throw new Error("type must be a class constructor or string");if("function"!=typeof n)throw new Error("handler must be a function");for(var i in t){var o=t[i];if(o.type===e)return void(o.handler=n)}t.push({type:e,handler:n})}function f(t){for(var e=arguments.length,n=Array(e>1?e-1:0),r=1;e>r;r++)n[r-1]=arguments[r];for(var i in n){var o=n[i];for(var a in o){var l=o[a];if(("undefined"==typeof t?"undefined":p(t))===l.type||"string"!=typeof l.type&&t instanceof l.type)return l.handler}}}function h(){var h=arguments.length<=0||void 0===arguments[0]?null:arguments[0],y={_getObjectClassName:u};y.DefaultQueryBuilderOptions={autoQuoteTableNames:!1,autoQuoteFieldNames:!1,autoQuoteAliasNames:!0,useAsForTableAliasNames:!1,nameQuoteCharacter:"`",tableAliasQuoteCharacter:"`",fieldAliasQuoteCharacter:'"',valueHandlers:[],parameterCharacter:"?",numberedParameters:!1,numberedParametersPrefix:"$",numberedParametersStartAt:1,replaceSingleQuotes:!1,singleQuoteReplacement:"''",separator:" "},y.globalValueHandlers=[],y.registerValueHandler=function(t,e){c(y.globalValueHandlers,t,e)},y.Cloneable=function(){function t(){r(this,t)}return d(t,[{key:"clone",value:function(){var t=new this.constructor;return o(t,s(o({},this)))}}]),t}(),y.BaseBuilder=function(i){function a(t){r(this,a);var n=e(this,Object.getPrototypeOf(a).call(this)),i=JSON.parse(JSON.stringify(y.DefaultQueryBuilderOptions));return n.options=o({},i,t),n}return n(a,i),d(a,[{key:"registerValueHandler",value:function(t,e){return c(this.options.valueHandlers,t,e),this}},{key:"_sanitizeExpression",value:function(t){if(!(t instanceof y.Expression)&&"string"!=typeof t)throw new Error("expression must be a string or Expression instance");return t}},{key:"_sanitizeName",value:function(t,e){if("string"!=typeof t)throw new Error(e+" must be a string");return t}},{key:"_sanitizeField",value:function(t){return t instanceof y.BaseBuilder||(t=this._sanitizeName(t,"field name")),t}},{key:"_sanitizeQueryBuilder",value:function(t){if(t instanceof y.QueryBuilder)return t;throw new Error("must be a QueryBuilder instance")}},{key:"_sanitizeTable",value:function(t){if("string"!=typeof t)try{t=this._sanitizeQueryBuilder(t)}catch(e){throw new Error("table name must be a string or a query builder")}else t=this._sanitizeName(t,"table");return t}},{key:"_sanitizeTableAlias",value:function(t){return this._sanitizeName(t,"table alias")}},{key:"_sanitizeFieldAlias",value:function(t){return this._sanitizeName(t,"field alias")}},{key:"_sanitizeLimitOffset",value:function(t){if(t=parseInt(t),0>t||isNaN(t))throw new Error("limit/offset must be >= 0");return t}},{key:"_sanitizeValue",value:function(t){var e="undefined"==typeof t?"undefined":p(t);if(null===t);else if("string"===e||"number"===e||"boolean"===e);else if(t instanceof y.BaseBuilder);else{var n=!!f(t,this.options.valueHandlers,y.globalValueHandlers);if(!n)throw new Error("field value must be a string, number, boolean, null or one of the registered custom value types")}return t}},{key:"_escapeValue",value:function(t){return this.options.replaceSingleQuotes?t.replace(/\'/g,this.options.singleQuoteReplacement):t}},{key:"_formatTableName",value:function(t){if(this.options.autoQuoteTableNames){var e=this.options.nameQuoteCharacter;t=""+e+t+e}return t}},{key:"_formatFieldAlias",value:function(t){if(this.options.autoQuoteAliasNames){var e=this.options.fieldAliasQuoteCharacter;t=""+e+t+e}return t}},{key:"_formatTableAlias",value:function(t){if(this.options.autoQuoteAliasNames){var e=this.options.tableAliasQuoteCharacter;t=""+e+t+e}return this.options.useAsForTableAliasNames?"AS "+t:t}},{key:"_formatFieldName",value:function(t){var e=this,n=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];return this.options.autoQuoteFieldNames&&!function(){var r=e.options.nameQuoteCharacter;t=n.ignorePeriodsForFieldNameQuotes?""+r+t+r:t.split(".").map(function(t){return"*"===t?t:""+r+t+r}).join(".")}(),t}},{key:"_formatCustomValue",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?!1:arguments[1],n=f(t,this.options.valueHandlers,y.globalValueHandlers);return n&&(t=n(t,e)),t}},{key:"_formatValueForParamArray",value:function(t){var e=this;return l(t)?t.map(function(t){return e._formatValueForParamArray(t)}):this._formatCustomValue(t,!0)}},{key:"_formatValueForQueryString",value:function(t){var e=this,n=arguments.length<=1||void 0===arguments[1]?{}:arguments[1],r=this._formatCustomValue(t);if(r!==t)return this._applyNestingFormatting(r);if(l(t))t=t.map(function(t){return e._formatValueForQueryString(t)}),t=this._applyNestingFormatting(t.join(", "));else{var i="undefined"==typeof t?"undefined":p(t);if(null===t)t="NULL";else if("boolean"===i)t=t?"TRUE":"FALSE";else if(t instanceof y.BaseBuilder)t=this._applyNestingFormatting(t.toString());else if("number"!==i)if(n.dontQuote)t=""+t;else{var o=this._escapeValue(t);t="'"+o+"'"}}return t}},{key:"_applyNestingFormatting",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?!0:arguments[1];return t&&"string"==typeof t&&e&&("("!==t.charAt(0)||")"!==t.charAt(t.length-1))?"("+t+")":t}},{key:"_buildString",value:function(e,n){var r=arguments.length<=2||void 0===arguments[2]?{}:arguments[2],i=r.nested,o=r.buildParameterized,a=r.formattingOptions;n=n||[],e=e||"";for(var u="",s=-1,c=[],f=this.options.parameterCharacter,h=0;e.length>h;)if(e.substr(h,f.length)===f){var v=n[++s];if(o)if(v instanceof y.BaseBuilder){var d=v._toParamString({buildParameterized:o,nested:!0});u+=d.text,c.push.apply(c,t(d.values))}else if(v=this._formatValueForParamArray(v),l(v)){var p=v.map(function(){return f}).join(", ");u+="("+p+")",c.push.apply(c,t(v))}else u+=f,c.push(v);else u+=this._formatValueForQueryString(v,a);h+=f.length}else u+=e.charAt(h),h++;return{text:this._applyNestingFormatting(u,!!i),values:c}}},{key:"_buildManyStrings",value:function(e,n){var r=arguments.length<=2||void 0===arguments[2]?{}:arguments[2],i=[],o=[];for(var a in e){var l=e[a],u=n[a],s=this._buildString(l,u,{buildParameterized:r.buildParameterized,nested:!1}),c=s.text,f=s.values;i.push(c),o.push.apply(o,t(f))}return i=i.join(this.options.separator),{text:i.length?this._applyNestingFormatting(i,!!r.nested):"",values:o}}},{key:"_toParamString",value:function(t){throw new Error("Not yet implemented")}},{key:"toString",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return this._toParamString(t).text}},{key:"toParam",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return this._toParamString(o({},t,{buildParameterized:!0}))}}]),a}(y.Cloneable),y.Expression=function(i){function o(t){r(this,o);var n=e(this,Object.getPrototypeOf(o).call(this,t));return n._nodes=[],n}return n(o,i),d(o,[{key:"and",value:function(t,e){for(var n=arguments.length,r=Array(n>2?n-2:0),i=2;n>i;i++)r[i-2]=arguments[i];return this._add("AND",t,e,r)}},{key:"or",value:function(t,e){for(var n=arguments.length,r=Array(n>2?n-2:0),i=2;n>i;i++)r[i-2]=arguments[i];return this._add("OR",t,e,r)}},{key:"_add",value:function(t,e,n,r){var i=["=","<",">","<=",">=","<>","!=","in","not in","like","not like"],o=void 0;return"string"==typeof e&&"string"==typeof n&&-1!=i.indexOf(n.toLowerCase())?o=this._buildExpression(e,n):(o=e,r.unshift(n)),this._sanitizeExpression(e),this._nodes.push({type:t,expr:o,para:r}),this}},{key:"_buildExpression",value:function(t,e){var n=this._formatFieldName(t),r=this.options.parameterCharacter,i=n+" "+e+" "+r;return i}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n=[],r=[],i=!0,o=!1,a=void 0;try{for(var l,u=this._nodes[Symbol.iterator]();!(i=(l=u.next()).done);i=!0){var s=l.value,c=s.type,f=s.expr,h=s.para,v=f instanceof y.Expression?f._toParamString({buildParameterized:e.buildParameterized,nested:!0}):this._buildString(f,h,{buildParameterized:e.buildParameterized}),d=v.text,p=v.values;n.length&&n.push(c),n.push(d),r.push.apply(r,t(p))}}catch(g){o=!0,a=g}finally{try{!i&&u["return"]&&u["return"]()}finally{if(o)throw a}}return n=n.join(" "),{text:this._applyNestingFormatting(n,!!e.nested),values:r}}}]),o}(y.BaseBuilder),y.Case=function(l){function u(t){var n=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];r(this,u);var i=e(this,Object.getPrototypeOf(u).call(this,n));return a(t)&&(n=t,t=null),t&&(i._fieldName=i._sanitizeField(t)),i.options=o({},y.DefaultQueryBuilderOptions,n),i._cases=[],i._elseValue=null,i}return n(u,l),d(u,[{key:"when",value:function(t){for(var e=arguments.length,n=Array(e>1?e-1:0),r=1;e>r;r++)n[r-1]=arguments[r];return this._cases.unshift({expression:t,values:n}),this}},{key:"then",value:function(t){if(0==this._cases.length)throw new Error("when() needs to be called first");return this._cases[0].result=t,this}},{key:"else",value:function(t){return this._elseValue=t,this}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n="",r=[],o=!0,a=!1,l=void 0;try{for(var u,s=this._cases[Symbol.iterator]();!(o=(u=s.next()).done);o=!0){var c=u.value,f=c.expression,h=c.values,v=c.result;n=i(n," ");var d=this._buildString(f,h,{buildParameterized:e.buildParameterized,nested:!0});n+="WHEN "+d.text+" THEN "+this._formatValueForQueryString(v),r.push.apply(r,t(d.values))}}catch(p){a=!0,l=p}finally{try{!o&&s["return"]&&s["return"]()}finally{if(a)throw l}}return n.length?(n+=" ELSE "+this._formatValueForQueryString(this._elseValue)+" END",this._fieldName&&(n=this._fieldName+" "+n),n="CASE "+n):n=this._formatValueForQueryString(this._elseValue),{text:n,values:r}}}]),u}(y.BaseBuilder),y.Block=function(t){function i(t){return r(this,i),e(this,Object.getPrototypeOf(i).call(this,t))}return n(i,t),d(i,[{key:"exposedMethods",value:function(){for(var t={},e=this;e;)Object.getOwnPropertyNames(e).forEach(function(n){"constructor"===n||"function"!=typeof e[n]||"_"===n.charAt(0)||y.Block.prototype[n]||(t[n]=e[n])}),e=Object.getPrototypeOf(e);return t}}]),i}(y.BaseBuilder),y.StringBlock=function(t){function i(t,n){r(this,i);var o=e(this,Object.getPrototypeOf(i).call(this,t));return o._str=n,o}return n(i,t),d(i,[{key:"_toParamString",value:function(){arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return{text:this._str,values:[]}}}]),i}(y.Block),y.FunctionBlock=function(t){function i(t){r(this,i);var n=e(this,Object.getPrototypeOf(i).call(this,t));return n._strings=[],n._values=[],n}return n(i,t),d(i,[{key:"function",value:function(t){this._strings.push(t);for(var e=arguments.length,n=Array(e>1?e-1:0),r=1;e>r;r++)n[r-1]=arguments[r];this._values.push(n)}},{key:"_toParamString",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return this._buildManyStrings(this._strings,this._values,t)}}]),i}(y.Block),y.registerValueHandler(y.FunctionBlock,function(t){var e=arguments.length<=1||void 0===arguments[1]?!1:arguments[1];return e?t.toParam():t.toString()}),y.AbstractTableBlock=function(o){function a(t,n){r(this,a);var i=e(this,Object.getPrototypeOf(a).call(this,t));return i._tables=[],i}return n(a,o),d(a,[{key:"_table",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1];e=e?this._sanitizeTableAlias(e):e,t=this._sanitizeTable(t),this.options.singleTable&&(this._tables=[]),this._tables.push({table:t,alias:e})}},{key:"_hasTable",value:function(){return 01)throw new Error("Cannot set multiple rows of fields this way.");"undefined"!=typeof e&&(e=this._sanitizeValue(e)),t=this._sanitizeField(t);var r=this._fields.indexOf(t);-1===r&&(this._fields.push(t),r=this._fields.length-1),this._values[0][r]=e,this._valueOptions[0][r]=n}},{key:"_setFields",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];if("object"!==("undefined"==typeof t?"undefined":p(t)))throw new Error("Expected an object but got "+("undefined"==typeof t?"undefined":p(t)));for(var n in t)this._set(n,t[n],e)}},{key:"_setFieldsRows",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];if(!l(t))throw new Error("Expected an array of objects but got "+("undefined"==typeof t?"undefined":p(t)));this._reset();for(var n in t){var r=t[n];for(var i in r){var o=r[i];i=this._sanitizeField(i),o=this._sanitizeValue(o);var a=this._fields.indexOf(i);if(n>0&&-1===a)throw new Error("All fields in subsequent rows must match the fields in the first row");-1===a&&(this._fields.push(i),a=this._fields.length-1),l(this._values[n])||(this._values[n]=[],this._valueOptions[n]=[]),this._values[n][a]=o,this._valueOptions[n][a]=e}}}}]),i}(y.Block),y.SetFieldBlock=function(o){function a(){return r(this,a),e(this,Object.getPrototypeOf(a).apply(this,arguments))}return n(a,o),d(a,[{key:"set",value:function(t,e,n){this._set(t,e,n)}},{key:"setFields",value:function(t,e){this._setFields(t,e)}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n=e.buildParameterized;if(0>=this._fields.length)throw new Error("set() needs to be called");var r="",o=[];for(var a in this._fields){r=i(r,", ");var l=this._fields[a],u=this._values[0][a];if("undefined"==typeof u)r+=l;else{var s=this._buildString(l+" = "+this.options.parameterCharacter,[u],{buildParameterized:n,formattingOptions:this._valueOptions[0][a]});r+=s.text,o.push.apply(o,t(s.values))}}return{text:"SET "+r,values:o}}}]),a}(y.AbstractSetFieldBlock),y.InsertFieldValueBlock=function(o){function a(){return r(this,a),e(this,Object.getPrototypeOf(a).apply(this,arguments))}return n(a,o),d(a,[{key:"set",value:function(t,e){var n=arguments.length<=2||void 0===arguments[2]?{}:arguments[2];this._set(t,e,n)}},{key:"setFields",value:function(t,e){this._setFields(t,e)}},{key:"setFieldsRows",value:function(t,e){this._setFieldsRows(t,e)}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n=e.buildParameterized,r=this._fields.join(", "),o=[],a=[];for(var l in this._values){o[l]="";for(var u in this._values[l]){var s=this._buildString(this.options.parameterCharacter,[this._values[l][u]],{buildParameterized:n,formattingOptions:this._valueOptions[l][u]});a.push.apply(a,t(s.values)),o[l]=i(o[l],", "),o[l]+=s.text}}return{text:r.length?"("+r+") VALUES ("+o.join("), (")+")":"",values:a}}}]),a}(y.AbstractSetFieldBlock),y.InsertFieldsFromQueryBlock=function(t){function i(t){r(this,i);var n=e(this,Object.getPrototypeOf(i).call(this,t));return n._fields=[],n._query=null,n}return n(i,t),d(i,[{key:"fromQuery",value:function(t,e){var n=this;this._fields=t.map(function(t){return n._sanitizeField(t)}),this._query=this._sanitizeQueryBuilder(e)}},{key:"_toParamString",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],e="",n=[];if(this._fields.length&&this._query){var r=this._query._toParamString({buildParameterized:t.buildParameterized,nested:!0}),i=r.text,o=r.values;e="("+this._fields.join(", ")+") ("+i+")",n=o}return{text:e,values:n}}}]),i}(y.Block),y.DistinctBlock=function(t){function i(){return r(this,i),e(this,Object.getPrototypeOf(i).apply(this,arguments))}return n(i,t),d(i,[{key:"distinct",value:function(){this._useDistinct=!0}},{key:"_toParamString",value:function(){return{text:this._useDistinct?"DISTINCT":"",values:[]}}}]),i}(y.Block),y.GroupByBlock=function(t){function i(t){r(this,i);var n=e(this,Object.getPrototypeOf(i).call(this,t));return n._groups=[],n}return n(i,t),d(i,[{key:"group",value:function(t){this._groups.push(this._sanitizeField(t))}},{key:"_toParamString",value:function(){arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return{text:this._groups.length?"GROUP BY "+this._groups.join(", "):"",values:[]}}}]),i}(y.Block),y.OffsetBlock=function(t){function i(t){r(this,i);var n=e(this,Object.getPrototypeOf(i).call(this,t));return n._offsets=null,n}return n(i,t),d(i,[{key:"offset",value:function(t){this._offsets=this._sanitizeLimitOffset(t)}},{key:"_toParamString",value:function(){return{text:this._offsets?"OFFSET "+this._offsets:"",values:[]}}}]),i}(y.Block),y.AbstractConditionBlock=function(o){function a(t){r(this,a);var n=e(this,Object.getPrototypeOf(a).call(this,t));return n._conditions=[],n}return n(a,o),d(a,[{key:"_condition",value:function(t){for(var e=arguments.length,n=Array(e>1?e-1:0),r=1;e>r;r++)n[r-1]=arguments[r];t=this._sanitizeExpression(t),this._conditions.push({expr:t,values:n})}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n="",r=[],o=!0,a=!1,l=void 0;try{for(var u,s=this._conditions[Symbol.iterator]();!(o=(u=s.next()).done);o=!0){var c=u.value,f=c.expr,h=c.values;n=i(n,") AND (");var v=f instanceof y.Expression?f._toParamString({buildParameterized:e.buildParameterized}):this._buildString(f,h,{buildParameterized:e.buildParameterized});n+=v.text,r.push.apply(r,t(v.values))}}catch(d){a=!0,l=d}finally{try{!o&&s["return"]&&s["return"]()}finally{if(a)throw l}}return{text:n.length?this.options.verb+" ("+n+")":"",values:r}}}]),a}(y.Block),y.WhereBlock=function(t){function i(t){return r(this,i),e(this,Object.getPrototypeOf(i).call(this,o({},t,{verb:"WHERE"})))}return n(i,t),d(i,[{key:"where",value:function(t,e){for(var n=arguments.length,r=Array(n>2?n-2:0),i=2;n>i;i++)r[i-2]=arguments[i];var o=["=","<",">","<=",">=","<>","!=","in","not in","like","not like"];if("string"==typeof t&&"string"==typeof e&&-1!=o.indexOf(e.toLowerCase())){var a=new y.Expression(this.options);this._condition(a._add("AND",t,e,r))}else this._condition.apply(this,arguments)}}]),i}(y.AbstractConditionBlock),y.HavingBlock=function(t){function i(t){return r(this,i),e(this,Object.getPrototypeOf(i).call(this,o({},t,{verb:"HAVING"})))}return n(i,t),d(i,[{key:"having",value:function(t,e){for(var n=arguments.length,r=Array(n>2?n-2:0),i=2;n>i;i++)r[i-2]=arguments[i];var o=["=","<",">","<=",">=","<>","!=","in","not in","like","not like"];if("string"==typeof t&&"string"==typeof e&&-1!=o.indexOf(e.toLowerCase())){var a=new y.Expression(this.options);this._condition(a._add("AND",t,e,r))}else this._condition.apply(this,arguments)}}]),i}(y.AbstractConditionBlock),y.OrderByBlock=function(o){function a(t){r(this,a);var n=e(this,Object.getPrototypeOf(a).call(this,t));return n._orders=[],n}return n(a,o),d(a,[{key:"order",value:function(t,e){for(var n=arguments.length,r=Array(n>2?n-2:0),i=2;n>i;i++)r[i-2]=arguments[i];t=this._sanitizeField(t),e=void 0===e?!0:e,e=null!==e?!!e:e,this._orders.push({field:t,dir:e,values:r})}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n="",r=[],o=!0,a=!1,l=void 0;try{for(var u,s=this._orders[Symbol.iterator]();!(o=(u=s.next()).done);o=!0){var c=u.value,f=c.field,h=c.dir,v=c.values;n=i(n,", ");var d=this._buildString(f,v,{buildParameterized:e.buildParameterized});n+=d.text,r.push.apply(r,t(d.values)),null!==h&&(n+=" "+(h?"ASC":"DESC"))}}catch(p){a=!0,l=p}finally{try{!o&&s["return"]&&s["return"]()}finally{if(a)throw l}}return{text:n.length?"ORDER BY "+n:"",values:r}}}]),a}(y.Block),y.LimitBlock=function(t){function i(t){r(this,i);var n=e(this,Object.getPrototypeOf(i).call(this,t));return n._limit=null,n}return n(i,t),d(i,[{key:"limit",value:function(t){this._limit=this._sanitizeLimitOffset(t)}},{key:"_toParamString",value:function(){return{text:null!==this._limit?"LIMIT "+this._limit:"",values:[]}}}]),i}(y.Block),y.JoinBlock=function(o){function a(t){r(this,a);var n=e(this,Object.getPrototypeOf(a).call(this,t));return n._joins=[],n}return n(a,o),d(a,[{key:"join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],n=arguments.length<=2||void 0===arguments[2]?null:arguments[2],r=arguments.length<=3||void 0===arguments[3]?"INNER":arguments[3];t=this._sanitizeTable(t,!0),e=e?this._sanitizeTableAlias(e):e,n=n?this._sanitizeExpression(n):n,this._joins.push({type:r,table:t,alias:e,condition:n})}},{key:"left_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],n=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,n,"LEFT")}},{key:"right_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],n=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,n,"RIGHT")}},{key:"outer_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],n=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,n,"OUTER")}},{key:"left_outer_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],n=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,n,"LEFT OUTER")}},{key:"full_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],n=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,n,"FULL")}},{key:"cross_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],n=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,n,"CROSS")}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n="",r=[],o=!0,a=!1,l=void 0;try{for(var u,s=this._joins[Symbol.iterator]();!(o=(u=s.next()).done);o=!0){var c=u.value,f=c.type,h=c.table,v=c.alias,d=c.condition;n=i(n,this.options.separator);var p=void 0;if(h instanceof y.BaseBuilder){var g=h._toParamString({buildParameterized:e.buildParameterized,nested:!0});r.push.apply(r,t(g.values)),p=g.text}else p=this._formatTableName(h);if(n+=f+" JOIN "+p,v&&(n+=" "+this._formatTableAlias(v)),d){n+=" ON ";var _=void 0;_=d instanceof y.Expression?d._toParamString({buildParameterized:e.buildParameterized}):this._buildString(d,[],{buildParameterized:e.buildParameterized}),n+=this._applyNestingFormatting(_.text),r.push.apply(r,t(_.values))}}}catch(m){a=!0,l=m}finally{try{!o&&s["return"]&&s["return"]()}finally{if(a)throw l}}return{text:n,values:r}}}]),a}(y.Block),y.UnionBlock=function(o){function a(t){r(this,a);var n=e(this,Object.getPrototypeOf(a).call(this,t));return n._unions=[],n}return n(a,o),d(a,[{key:"union",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?"UNION":arguments[1];t=this._sanitizeTable(t),this._unions.push({type:e,table:t})}},{key:"union_all",value:function(t){this.union(t,"UNION ALL")}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n="",r=[],o=!0,a=!1,l=void 0;try{for(var u,s=this._unions[Symbol.iterator]();!(o=(u=s.next()).done);o=!0){var c=u.value,f=c.type,h=c.table;n=i(n,this.options.separator);var v=void 0;if(h instanceof y.BaseBuilder){var d=h._toParamString({buildParameterized:e.buildParameterized,nested:!0});v=d.text,r.push.apply(r,t(d.values))}else n=this._formatTableName(h);n+=f+" "+v}}catch(p){a=!0,l=p}finally{try{!o&&s["return"]&&s["return"]()}finally{if(a)throw l}}return{text:n,values:r}}}]),a}(y.Block),y.QueryBuilder=function(i){function a(t,n){r(this,a);var i=e(this,Object.getPrototypeOf(a).call(this,t));i.blocks=n||[];var o=!0,l=!1,u=void 0;try{for(var s,c=i.blocks[Symbol.iterator]();!(o=(s=c.next()).done);o=!0){var f=s.value,h=f.exposedMethods();for(var v in h){var d=h[v];if(void 0!==i[v])throw new Error("Builder already has a builder method called: "+v);!function(t,e,n){i[e]=function(){for(var e=arguments.length,r=Array(e),o=0;e>o;o++)r[o]=arguments[o];return n.call.apply(n,[t].concat(r)),i}}(f,v,d)}}}catch(p){l=!0,u=p}finally{try{!o&&c["return"]&&c["return"]()}finally{if(l)throw u}}return i}return n(a,i),d(a,[{key:"registerValueHandler",value:function(t,e){var n=!0,r=!1,i=void 0;try{for(var o,l=this.blocks[Symbol.iterator]();!(n=(o=l.next()).done);n=!0){var u=o.value;u.registerValueHandler(t,e)}}catch(s){r=!0,i=s}finally{try{!n&&l["return"]&&l["return"]()}finally{if(r)throw i}}return v(Object.getPrototypeOf(a.prototype),"registerValueHandler",this).call(this,t,e),this}},{key:"updateOptions",value:function(t){this.options=o({},this.options,t);var e=!0,n=!1,r=void 0;try{for(var i,a=this.blocks[Symbol.iterator]();!(e=(i=a.next()).done);e=!0){var l=i.value;l.options=o({},l.options,t)}}catch(u){n=!0,r=u}finally{try{!e&&a["return"]&&a["return"]()}finally{if(n)throw r}}}},{key:"_toParamString",value:function(){var e,n=this,r=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];r=o({},this.options,r);var i=this.blocks.map(function(t){return t._toParamString({buildParameterized:r.buildParameterized,queryBuilder:n})}),a=i.map(function(t){return t.text}),l=i.map(function(t){return t.values}),u=a.filter(function(t){return 0=this._fields.length)throw new Error("set() needs to be called");var e=v(Object.getPrototypeOf(i.prototype),"_toParamString",this).call(this,t);if(e.text.length&&01?e-1:0),r=1;e>r;r++)n[r-1]=arguments[r];if(t&&n){var i=!0,o=!1,a=void 0;try{for(var l,u=function(){var e=l.value;"object"===("undefined"==typeof e?"undefined":p(e))&&Object.getOwnPropertyNames(e).forEach(function(n){"function"!=typeof e[n]&&(t[n]=e[n])})},s=n[Symbol.iterator]();!(i=(l=s.next()).done);i=!0)u()}catch(c){o=!0,a=c}finally{try{!i&&s["return"]&&s["return"]()}finally{if(o)throw a}}}return t}function a(t){return t&&t.constructor.prototype===Object.prototype}function l(t){return t&&t.constructor.prototype===Array.prototype}function u(t){if(t&&t.constructor&&t.constructor.toString){var e=t.constructor.toString().match(/function\s*(\w+)/);if(e&&2===e.length)return e[1]}}function s(t){if(!t)return t;if("function"==typeof t.clone)return t.clone();if(!a(t)&&!l(t))return JSON.parse(JSON.stringify(t));var e=function(){var e=new t.constructor;return Object.getOwnPropertyNames(t).forEach(function(n){"function"!=typeof t[n]&&(e[n]=s(t[n]))}),{v:e}}();return"object"===("undefined"==typeof e?"undefined":p(e))?e.v:void 0}function c(t,e,n){var r="undefined"==typeof e?"undefined":p(e);if("function"!==r&&"string"!==r)throw new Error("type must be a class constructor or string");if("function"!=typeof n)throw new Error("handler must be a function");for(var i in t){var o=t[i];if(o.type===e)return void(o.handler=n)}t.push({type:e,handler:n})}function f(t){for(var e=arguments.length,n=Array(e>1?e-1:0),r=1;e>r;r++)n[r-1]=arguments[r];for(var i in n){var o=n[i];for(var a in o){var l=o[a];if(("undefined"==typeof t?"undefined":p(t))===l.type||"string"!=typeof l.type&&t instanceof l.type)return l.handler}}}function h(){var h=arguments.length<=0||void 0===arguments[0]?null:arguments[0],y={_getObjectClassName:u};y.DefaultQueryBuilderOptions={autoQuoteTableNames:!1,autoQuoteFieldNames:!1,autoQuoteAliasNames:!0,useAsForTableAliasNames:!1,nameQuoteCharacter:"`",tableAliasQuoteCharacter:"`",fieldAliasQuoteCharacter:'"',valueHandlers:[],parameterCharacter:"?",numberedParameters:!1,numberedParametersPrefix:"$",numberedParametersStartAt:1,replaceSingleQuotes:!1,singleQuoteReplacement:"''",separator:" "},y.globalValueHandlers=[],y.registerValueHandler=function(t,e){c(y.globalValueHandlers,t,e)},y.Cloneable=function(){function t(){r(this,t)}return d(t,[{key:"clone",value:function(){var t=new this.constructor;return o(t,s(o({},this)))}}]),t}(),y.BaseBuilder=function(i){function a(t){r(this,a);var n=e(this,Object.getPrototypeOf(a).call(this)),i=JSON.parse(JSON.stringify(y.DefaultQueryBuilderOptions));return n.options=o({},i,t),n}return n(a,i),d(a,[{key:"registerValueHandler",value:function(t,e){return c(this.options.valueHandlers,t,e),this}},{key:"_sanitizeExpression",value:function(t){if(!(t instanceof y.Expression)&&"string"!=typeof t)throw new Error("expression must be a string or Expression instance");return t}},{key:"_sanitizeName",value:function(t,e){if("string"!=typeof t)throw new Error(e+" must be a string");return t}},{key:"_sanitizeField",value:function(t){return t instanceof y.BaseBuilder||(t=this._sanitizeName(t,"field name")),t}},{key:"_sanitizeQueryBuilder",value:function(t){if(t instanceof y.QueryBuilder)return t;throw new Error("must be a QueryBuilder instance")}},{key:"_sanitizeTable",value:function(t){if("string"!=typeof t)try{t=this._sanitizeQueryBuilder(t)}catch(e){throw new Error("table name must be a string or a query builder")}else t=this._sanitizeName(t,"table");return t}},{key:"_sanitizeTableAlias",value:function(t){return this._sanitizeName(t,"table alias")}},{key:"_sanitizeFieldAlias",value:function(t){return this._sanitizeName(t,"field alias")}},{key:"_sanitizeLimitOffset",value:function(t){if(t=parseInt(t),0>t||isNaN(t))throw new Error("limit/offset must be >= 0");return t}},{key:"_sanitizeValue",value:function(t){var e="undefined"==typeof t?"undefined":p(t);if(null===t);else if("string"===e||"number"===e||"boolean"===e);else if(t instanceof y.BaseBuilder);else{var n=!!f(t,this.options.valueHandlers,y.globalValueHandlers);if(!n)throw new Error("field value must be a string, number, boolean, null or one of the registered custom value types")}return t}},{key:"_escapeValue",value:function(t){return this.options.replaceSingleQuotes?t.replace(/\'/g,this.options.singleQuoteReplacement):t}},{key:"_formatTableName",value:function(t){if(this.options.autoQuoteTableNames){var e=this.options.nameQuoteCharacter;t=""+e+t+e}return t}},{key:"_formatFieldAlias",value:function(t){if(this.options.autoQuoteAliasNames){var e=this.options.fieldAliasQuoteCharacter;t=""+e+t+e}return t}},{key:"_formatTableAlias",value:function(t){if(this.options.autoQuoteAliasNames){var e=this.options.tableAliasQuoteCharacter;t=""+e+t+e}return this.options.useAsForTableAliasNames?"AS "+t:t}},{key:"_formatFieldName",value:function(t){var e=this,n=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];return this.options.autoQuoteFieldNames&&!function(){var r=e.options.nameQuoteCharacter;t=n.ignorePeriodsForFieldNameQuotes?""+r+t+r:t.split(".").map(function(t){return"*"===t?t:""+r+t+r}).join(".")}(),t}},{key:"_formatCustomValue",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?!1:arguments[1],n=f(t,this.options.valueHandlers,y.globalValueHandlers);return n&&(t=n(t,e)),t}},{key:"_formatValueForParamArray",value:function(t){var e=this;return l(t)?t.map(function(t){return e._formatValueForParamArray(t)}):this._formatCustomValue(t,!0)}},{key:"_formatValueForQueryString",value:function(t){var e=this,n=arguments.length<=1||void 0===arguments[1]?{}:arguments[1],r=this._formatCustomValue(t);if(r!==t)return this._applyNestingFormatting(r);if(l(t))t=t.map(function(t){return e._formatValueForQueryString(t)}),t=this._applyNestingFormatting(t.join(", "));else{var i="undefined"==typeof t?"undefined":p(t);if(null===t)t="NULL";else if("boolean"===i)t=t?"TRUE":"FALSE";else if(t instanceof y.BaseBuilder)t=this._applyNestingFormatting(t.toString());else if("number"!==i)if(n.dontQuote)t=""+t;else{var o=this._escapeValue(t);t="'"+o+"'"}}return t}},{key:"_applyNestingFormatting",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?!0:arguments[1];return t&&"string"==typeof t&&e&&("("!==t.charAt(0)||")"!==t.charAt(t.length-1))?"("+t+")":t}},{key:"_buildString",value:function(e,n){var r=arguments.length<=2||void 0===arguments[2]?{}:arguments[2],i=r.nested,o=r.buildParameterized,a=r.formattingOptions;n=n||[],e=e||"";for(var u="",s=-1,c=[],f=this.options.parameterCharacter,h=0;e.length>h;)if(e.substr(h,f.length)===f){var v=n[++s];if(o)if(v instanceof y.BaseBuilder){var d=v._toParamString({buildParameterized:o,nested:!0});u+=d.text,c.push.apply(c,t(d.values))}else if(v=this._formatValueForParamArray(v),l(v)){var p=v.map(function(){return f}).join(", ");u+="("+p+")",c.push.apply(c,t(v))}else u+=f,c.push(v);else u+=this._formatValueForQueryString(v,a);h+=f.length}else u+=e.charAt(h),h++;return{text:this._applyNestingFormatting(u,!!i),values:c}}},{key:"_buildManyStrings",value:function(e,n){var r=arguments.length<=2||void 0===arguments[2]?{}:arguments[2],i=[],o=[];for(var a in e){var l=e[a],u=n[a],s=this._buildString(l,u,{buildParameterized:r.buildParameterized,nested:!1}),c=s.text,f=s.values;i.push(c),o.push.apply(o,t(f))}return i=i.join(this.options.separator),{text:i.length?this._applyNestingFormatting(i,!!r.nested):"",values:o}}},{key:"_toParamString",value:function(t){throw new Error("Not yet implemented")}},{key:"toString",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return this._toParamString(t).text}},{key:"toParam",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return this._toParamString(o({},t,{buildParameterized:!0}))}}]),a}(y.Cloneable),y.Expression=function(i){function o(t){r(this,o);var n=e(this,Object.getPrototypeOf(o).call(this,t));return n._nodes=[],n}return n(o,i),d(o,[{key:"and",value:function(t,e){for(var n=arguments.length,r=Array(n>2?n-2:0),i=2;n>i;i++)r[i-2]=arguments[i];return this._add("AND",t,e,r)}},{key:"or",value:function(t,e){for(var n=arguments.length,r=Array(n>2?n-2:0),i=2;n>i;i++)r[i-2]=arguments[i];return this._add("OR",t,e,r)}},{key:"_add",value:function(t,e,n,r){var i=["=","<",">","<=",">=","<>","!=","in","not in","like","not like","is","is not"],o=void 0;return"string"==typeof e&&"string"==typeof n&&-1!=i.indexOf(n.toLowerCase())?o=this._buildExpression(e,n):(o=e,r.unshift(n)),this._sanitizeExpression(e),this._nodes.push({type:t,expr:o,para:r}),this}},{key:"_buildExpression",value:function(t,e){var n=this._formatFieldName(t),r=this.options.parameterCharacter,i=n+" "+e+" "+r;return i}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n=[],r=[],i=!0,o=!1,a=void 0;try{for(var l,u=this._nodes[Symbol.iterator]();!(i=(l=u.next()).done);i=!0){var s=l.value,c=s.type,f=s.expr,h=s.para,v=f instanceof y.Expression?f._toParamString({buildParameterized:e.buildParameterized,nested:!0}):this._buildString(f,h,{buildParameterized:e.buildParameterized}),d=v.text,p=v.values;n.length&&n.push(c),n.push(d),r.push.apply(r,t(p))}}catch(g){o=!0,a=g}finally{try{!i&&u["return"]&&u["return"]()}finally{if(o)throw a}}return n=n.join(" "),{text:this._applyNestingFormatting(n,!!e.nested),values:r}}}]),o}(y.BaseBuilder),y.Case=function(l){function u(t){var n=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];r(this,u);var i=e(this,Object.getPrototypeOf(u).call(this,n));return a(t)&&(n=t,t=null),t&&(i._fieldName=i._sanitizeField(t)),i.options=o({},y.DefaultQueryBuilderOptions,n),i._cases=[],i._elseValue=null,i}return n(u,l),d(u,[{key:"when",value:function(t){for(var e=arguments.length,n=Array(e>1?e-1:0),r=1;e>r;r++)n[r-1]=arguments[r];return this._cases.unshift({expression:t,values:n}),this}},{key:"then",value:function(t){if(0==this._cases.length)throw new Error("when() needs to be called first");return this._cases[0].result=t,this}},{key:"else",value:function(t){return this._elseValue=t,this}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n="",r=[],o=!0,a=!1,l=void 0;try{for(var u,s=this._cases[Symbol.iterator]();!(o=(u=s.next()).done);o=!0){var c=u.value,f=c.expression,h=c.values,v=c.result;n=i(n," ");var d=this._buildString(f,h,{buildParameterized:e.buildParameterized,nested:!0});n+="WHEN "+d.text+" THEN "+this._formatValueForQueryString(v),r.push.apply(r,t(d.values))}}catch(p){a=!0,l=p}finally{try{!o&&s["return"]&&s["return"]()}finally{if(a)throw l}}return n.length?(n+=" ELSE "+this._formatValueForQueryString(this._elseValue)+" END",this._fieldName&&(n=this._fieldName+" "+n),n="CASE "+n):n=this._formatValueForQueryString(this._elseValue),{text:n,values:r}}}]),u}(y.BaseBuilder),y.Block=function(t){function i(t){return r(this,i),e(this,Object.getPrototypeOf(i).call(this,t))}return n(i,t),d(i,[{key:"exposedMethods",value:function(){for(var t={},e=this;e;)Object.getOwnPropertyNames(e).forEach(function(n){"constructor"===n||"function"!=typeof e[n]||"_"===n.charAt(0)||y.Block.prototype[n]||(t[n]=e[n])}),e=Object.getPrototypeOf(e);return t}}]),i}(y.BaseBuilder),y.StringBlock=function(t){function i(t,n){r(this,i);var o=e(this,Object.getPrototypeOf(i).call(this,t));return o._str=n,o}return n(i,t),d(i,[{key:"_toParamString",value:function(){arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return{text:this._str,values:[]}}}]),i}(y.Block),y.FunctionBlock=function(t){function i(t){r(this,i);var n=e(this,Object.getPrototypeOf(i).call(this,t));return n._strings=[],n._values=[],n}return n(i,t),d(i,[{key:"function",value:function(t){this._strings.push(t);for(var e=arguments.length,n=Array(e>1?e-1:0),r=1;e>r;r++)n[r-1]=arguments[r];this._values.push(n)}},{key:"_toParamString",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return this._buildManyStrings(this._strings,this._values,t)}}]),i}(y.Block),y.registerValueHandler(y.FunctionBlock,function(t){var e=arguments.length<=1||void 0===arguments[1]?!1:arguments[1];return e?t.toParam():t.toString()}),y.AbstractTableBlock=function(o){function a(t,n){r(this,a);var i=e(this,Object.getPrototypeOf(a).call(this,t));return i._tables=[],i}return n(a,o),d(a,[{key:"_table",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1];e=e?this._sanitizeTableAlias(e):e,t=this._sanitizeTable(t),this.options.singleTable&&(this._tables=[]),this._tables.push({table:t,alias:e})}},{key:"_hasTable",value:function(){return 01)throw new Error("Cannot set multiple rows of fields this way.");"undefined"!=typeof e&&(e=this._sanitizeValue(e)),t=this._sanitizeField(t);var r=this._fields.indexOf(t);-1===r&&(this._fields.push(t),r=this._fields.length-1),this._values[0][r]=e,this._valueOptions[0][r]=n}},{key:"_setFields",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];if("object"!==("undefined"==typeof t?"undefined":p(t)))throw new Error("Expected an object but got "+("undefined"==typeof t?"undefined":p(t)));for(var n in t)this._set(n,t[n],e)}},{key:"_setFieldsRows",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];if(!l(t))throw new Error("Expected an array of objects but got "+("undefined"==typeof t?"undefined":p(t)));this._reset();for(var n in t){var r=t[n];for(var i in r){var o=r[i];i=this._sanitizeField(i),o=this._sanitizeValue(o);var a=this._fields.indexOf(i);if(n>0&&-1===a)throw new Error("All fields in subsequent rows must match the fields in the first row");-1===a&&(this._fields.push(i),a=this._fields.length-1),l(this._values[n])||(this._values[n]=[],this._valueOptions[n]=[]),this._values[n][a]=o,this._valueOptions[n][a]=e}}}}]),i}(y.Block),y.SetFieldBlock=function(o){function a(){return r(this,a),e(this,Object.getPrototypeOf(a).apply(this,arguments))}return n(a,o),d(a,[{key:"set",value:function(t,e,n){this._set(t,e,n)}},{key:"setFields",value:function(t,e){this._setFields(t,e)}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n=e.buildParameterized;if(0>=this._fields.length)throw new Error("set() needs to be called");var r="",o=[];for(var a in this._fields){r=i(r,", ");var l=this._fields[a],u=this._values[0][a];if("undefined"==typeof u)r+=l;else{var s=this._buildString(l+" = "+this.options.parameterCharacter,[u],{buildParameterized:n,formattingOptions:this._valueOptions[0][a]});r+=s.text,o.push.apply(o,t(s.values))}}return{text:"SET "+r,values:o}}}]),a}(y.AbstractSetFieldBlock),y.InsertFieldValueBlock=function(o){function a(){return r(this,a),e(this,Object.getPrototypeOf(a).apply(this,arguments))}return n(a,o),d(a,[{key:"set",value:function(t,e){var n=arguments.length<=2||void 0===arguments[2]?{}:arguments[2];this._set(t,e,n)}},{key:"setFields",value:function(t,e){this._setFields(t,e)}},{key:"setFieldsRows",value:function(t,e){this._setFieldsRows(t,e)}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n=e.buildParameterized,r=this._fields.join(", "),o=[],a=[];for(var l in this._values){o[l]="";for(var u in this._values[l]){var s=this._buildString(this.options.parameterCharacter,[this._values[l][u]],{buildParameterized:n,formattingOptions:this._valueOptions[l][u]});a.push.apply(a,t(s.values)),o[l]=i(o[l],", "),o[l]+=s.text}}return{text:r.length?"("+r+") VALUES ("+o.join("), (")+")":"",values:a}}}]),a}(y.AbstractSetFieldBlock),y.InsertFieldsFromQueryBlock=function(t){function i(t){r(this,i);var n=e(this,Object.getPrototypeOf(i).call(this,t));return n._fields=[],n._query=null,n}return n(i,t),d(i,[{key:"fromQuery",value:function(t,e){var n=this;this._fields=t.map(function(t){return n._sanitizeField(t)}),this._query=this._sanitizeQueryBuilder(e)}},{key:"_toParamString",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],e="",n=[];if(this._fields.length&&this._query){var r=this._query._toParamString({buildParameterized:t.buildParameterized,nested:!0}),i=r.text,o=r.values;e="("+this._fields.join(", ")+") ("+i+")",n=o}return{text:e,values:n}}}]),i}(y.Block),y.DistinctBlock=function(t){function i(){return r(this,i),e(this,Object.getPrototypeOf(i).apply(this,arguments))}return n(i,t),d(i,[{key:"distinct",value:function(){this._useDistinct=!0}},{key:"_toParamString",value:function(){return{text:this._useDistinct?"DISTINCT":"",values:[]}}}]),i}(y.Block),y.GroupByBlock=function(t){function i(t){r(this,i);var n=e(this,Object.getPrototypeOf(i).call(this,t));return n._groups=[],n}return n(i,t),d(i,[{key:"group",value:function(t){this._groups.push(this._sanitizeField(t))}},{key:"_toParamString",value:function(){arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return{text:this._groups.length?"GROUP BY "+this._groups.join(", "):"",values:[]}}}]),i}(y.Block),y.OffsetBlock=function(t){function i(t){r(this,i);var n=e(this,Object.getPrototypeOf(i).call(this,t));return n._offsets=null,n}return n(i,t),d(i,[{key:"offset",value:function(t){this._offsets=this._sanitizeLimitOffset(t)}},{key:"_toParamString",value:function(){return{text:this._offsets?"OFFSET "+this._offsets:"",values:[]}}}]),i}(y.Block),y.AbstractConditionBlock=function(o){function a(t){r(this,a);var n=e(this,Object.getPrototypeOf(a).call(this,t));return n._conditions=[],n}return n(a,o),d(a,[{key:"_condition",value:function(t){for(var e=arguments.length,n=Array(e>1?e-1:0),r=1;e>r;r++)n[r-1]=arguments[r];t=this._sanitizeExpression(t),this._conditions.push({expr:t,values:n})}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n="",r=[],o=!0,a=!1,l=void 0;try{for(var u,s=this._conditions[Symbol.iterator]();!(o=(u=s.next()).done);o=!0){var c=u.value,f=c.expr,h=c.values;n=i(n,") AND (");var v=f instanceof y.Expression?f._toParamString({buildParameterized:e.buildParameterized}):this._buildString(f,h,{buildParameterized:e.buildParameterized});n+=v.text,r.push.apply(r,t(v.values))}}catch(d){a=!0,l=d}finally{try{!o&&s["return"]&&s["return"]()}finally{if(a)throw l}}return{text:n.length?this.options.verb+" ("+n+")":"",values:r}}}]),a}(y.Block),y.WhereBlock=function(t){function i(t){return r(this,i),e(this,Object.getPrototypeOf(i).call(this,o({},t,{verb:"WHERE"})))}return n(i,t),d(i,[{key:"where",value:function(t,e){for(var n=arguments.length,r=Array(n>2?n-2:0),i=2;n>i;i++)r[i-2]=arguments[i];var o=["=","<",">","<=",">=","<>","!=","in","not in","like","not like","is","is not"];if("string"==typeof t&&"string"==typeof e&&-1!=o.indexOf(e.toLowerCase())){var a=new y.Expression(this.options);this._condition(a._add("AND",t,e,r))}else this._condition.apply(this,arguments)}}]),i}(y.AbstractConditionBlock),y.HavingBlock=function(t){function i(t){return r(this,i),e(this,Object.getPrototypeOf(i).call(this,o({},t,{verb:"HAVING"})))}return n(i,t),d(i,[{key:"having",value:function(t,e){for(var n=arguments.length,r=Array(n>2?n-2:0),i=2;n>i;i++)r[i-2]=arguments[i];var o=["=","<",">","<=",">=","<>","!=","in","not in","like","not like","is","is not"];if("string"==typeof t&&"string"==typeof e&&-1!=o.indexOf(e.toLowerCase())){var a=new y.Expression(this.options);this._condition(a._add("AND",t,e,r))}else this._condition.apply(this,arguments)}}]),i}(y.AbstractConditionBlock),y.OrderByBlock=function(o){function a(t){r(this,a);var n=e(this,Object.getPrototypeOf(a).call(this,t));return n._orders=[],n}return n(a,o),d(a,[{key:"order",value:function(t,e){for(var n=arguments.length,r=Array(n>2?n-2:0),i=2;n>i;i++)r[i-2]=arguments[i];t=this._sanitizeField(t),e=void 0===e?!0:e,e=null!==e?!!e:e,this._orders.push({field:t,dir:e,values:r})}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n="",r=[],o=!0,a=!1,l=void 0;try{for(var u,s=this._orders[Symbol.iterator]();!(o=(u=s.next()).done);o=!0){var c=u.value,f=c.field,h=c.dir,v=c.values;n=i(n,", ");var d=this._buildString(f,v,{buildParameterized:e.buildParameterized});n+=d.text,r.push.apply(r,t(d.values)),null!==h&&(n+=" "+(h?"ASC":"DESC"))}}catch(p){a=!0,l=p}finally{try{!o&&s["return"]&&s["return"]()}finally{if(a)throw l}}return{text:n.length?"ORDER BY "+n:"",values:r}}}]),a}(y.Block),y.LimitBlock=function(t){function i(t){r(this,i);var n=e(this,Object.getPrototypeOf(i).call(this,t));return n._limit=null,n}return n(i,t),d(i,[{key:"limit",value:function(t){this._limit=this._sanitizeLimitOffset(t)}},{key:"_toParamString",value:function(){return{text:null!==this._limit?"LIMIT "+this._limit:"",values:[]}}}]),i}(y.Block),y.JoinBlock=function(o){function a(t){r(this,a);var n=e(this,Object.getPrototypeOf(a).call(this,t));return n._joins=[],n}return n(a,o),d(a,[{key:"join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],n=arguments.length<=2||void 0===arguments[2]?null:arguments[2],r=arguments.length<=3||void 0===arguments[3]?"INNER":arguments[3];t=this._sanitizeTable(t,!0),e=e?this._sanitizeTableAlias(e):e,n=n?this._sanitizeExpression(n):n,this._joins.push({type:r,table:t,alias:e,condition:n})}},{key:"left_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],n=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,n,"LEFT")}},{key:"right_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],n=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,n,"RIGHT")}},{key:"outer_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],n=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,n,"OUTER")}},{key:"left_outer_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],n=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,n,"LEFT OUTER")}},{key:"full_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],n=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,n,"FULL")}},{key:"cross_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],n=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,n,"CROSS")}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n="",r=[],o=!0,a=!1,l=void 0;try{for(var u,s=this._joins[Symbol.iterator]();!(o=(u=s.next()).done);o=!0){var c=u.value,f=c.type,h=c.table,v=c.alias,d=c.condition;n=i(n,this.options.separator);var p=void 0;if(h instanceof y.BaseBuilder){var g=h._toParamString({buildParameterized:e.buildParameterized,nested:!0});r.push.apply(r,t(g.values)),p=g.text}else p=this._formatTableName(h);if(n+=f+" JOIN "+p,v&&(n+=" "+this._formatTableAlias(v)),d){n+=" ON ";var _=void 0;_=d instanceof y.Expression?d._toParamString({buildParameterized:e.buildParameterized}):this._buildString(d,[],{buildParameterized:e.buildParameterized}),n+=this._applyNestingFormatting(_.text),r.push.apply(r,t(_.values))}}}catch(m){a=!0,l=m}finally{try{!o&&s["return"]&&s["return"]()}finally{if(a)throw l}}return{text:n,values:r}}}]),a}(y.Block),y.UnionBlock=function(o){function a(t){r(this,a);var n=e(this,Object.getPrototypeOf(a).call(this,t));return n._unions=[],n}return n(a,o),d(a,[{key:"union",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?"UNION":arguments[1];t=this._sanitizeTable(t),this._unions.push({type:e,table:t})}},{key:"union_all",value:function(t){this.union(t,"UNION ALL")}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n="",r=[],o=!0,a=!1,l=void 0;try{for(var u,s=this._unions[Symbol.iterator]();!(o=(u=s.next()).done);o=!0){var c=u.value,f=c.type,h=c.table;n=i(n,this.options.separator);var v=void 0;if(h instanceof y.BaseBuilder){var d=h._toParamString({buildParameterized:e.buildParameterized,nested:!0});v=d.text,r.push.apply(r,t(d.values))}else n=this._formatTableName(h);n+=f+" "+v}}catch(p){a=!0,l=p}finally{try{!o&&s["return"]&&s["return"]()}finally{if(a)throw l}}return{text:n,values:r}}}]),a}(y.Block),y.QueryBuilder=function(i){function a(t,n){r(this,a);var i=e(this,Object.getPrototypeOf(a).call(this,t));i.blocks=n||[];var o=!0,l=!1,u=void 0;try{for(var s,c=i.blocks[Symbol.iterator]();!(o=(s=c.next()).done);o=!0){var f=s.value,h=f.exposedMethods();for(var v in h){var d=h[v];if(void 0!==i[v])throw new Error("Builder already has a builder method called: "+v);!function(t,e,n){i[e]=function(){for(var e=arguments.length,r=Array(e),o=0;e>o;o++)r[o]=arguments[o];return n.call.apply(n,[t].concat(r)),i}}(f,v,d)}}}catch(p){l=!0,u=p}finally{try{!o&&c["return"]&&c["return"]()}finally{if(l)throw u}}return i}return n(a,i),d(a,[{key:"registerValueHandler",value:function(t,e){var n=!0,r=!1,i=void 0;try{for(var o,l=this.blocks[Symbol.iterator]();!(n=(o=l.next()).done);n=!0){var u=o.value;u.registerValueHandler(t,e)}}catch(s){r=!0,i=s}finally{try{!n&&l["return"]&&l["return"]()}finally{if(r)throw i}}return v(Object.getPrototypeOf(a.prototype),"registerValueHandler",this).call(this,t,e),this}},{key:"updateOptions",value:function(t){this.options=o({},this.options,t);var e=!0,n=!1,r=void 0;try{for(var i,a=this.blocks[Symbol.iterator]();!(e=(i=a.next()).done);e=!0){var l=i.value;l.options=o({},l.options,t)}}catch(u){n=!0,r=u}finally{try{!e&&a["return"]&&a["return"]()}finally{if(n)throw r}}}},{key:"_toParamString",value:function(){var e,n=this,r=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];r=o({},this.options,r);var i=this.blocks.map(function(t){return t._toParamString({buildParameterized:r.buildParameterized,queryBuilder:n})}),a=i.map(function(t){return t.text}),l=i.map(function(t){return t.values}),u=a.filter(function(t){return 0=this._fields.length)throw new Error("set() needs to be called");var e=v(Object.getPrototypeOf(i.prototype),"_toParamString",this).call(this,t);if(e.text.length&&0', '<=', '>=', '<>', '!=', 'in', 'not in', 'like', 'not like']; + let validOperators = ['=', '<', '>', '<=', '>=', '<>', '!=', 'in', 'not in', 'like', 'not like', 'is', 'is not']; let expr; if (typeof field === 'string' && typeof operator === 'string' && -1 != validOperators.indexOf(operator.toLowerCase())) { expr = this._buildExpression(field, operator); @@ -1593,7 +1593,7 @@ function _buildSquel(flavour = null) { } where (field, operator, ...values) { - let validOperators = ['=', '<', '>', '<=', '>=', '<>', '!=', 'in', 'not in', 'like', 'not like']; + let validOperators = ['=', '<', '>', '<=', '>=', '<>', '!=', 'in', 'not in', 'like', 'not like', 'is', 'is not']; if (typeof field === 'string' && typeof operator === 'string' && -1 != validOperators.indexOf(operator.toLowerCase())) { // key - operator - value let expr = new cls.Expression(this.options); @@ -1615,7 +1615,7 @@ function _buildSquel(flavour = null) { } having (field, operator, ...values) { - let validOperators = ['=', '<', '>', '<=', '>=', '<>', '!=', 'in', 'not in', 'like', 'not like']; + let validOperators = ['=', '<', '>', '<=', '>=', '<>', '!=', 'in', 'not in', 'like', 'not like', 'is', 'is not']; if (typeof field === 'string' && typeof operator === 'string' && -1 != validOperators.indexOf(operator.toLowerCase())) { // key - operator - value let expr = new cls.Expression(this.options); From 1f603abc2eb5e22c3ebf137b0b4bde946ac47e9c Mon Sep 17 00:00:00 2001 From: nic Date: Wed, 30 Mar 2016 13:05:08 +0200 Subject: [PATCH 4/4] fixed issue with sanitizing expression --- squel-basic.js | 2 +- squel-basic.min.js | 2 +- squel.js | 2 +- squel.min.js | 2 +- src/core.js | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/squel-basic.js b/squel-basic.js index 4c9b306b..97e0e8f9 100644 --- a/squel-basic.js +++ b/squel-basic.js @@ -828,7 +828,7 @@ function _buildSquel() { params.unshift(operator); } - this._sanitizeExpression(field); + expr = this._sanitizeExpression(expr); this._nodes.push({ type: type, expr: expr, diff --git a/squel-basic.min.js b/squel-basic.min.js index 5d92f7eb..2b0b30fd 100644 --- a/squel-basic.min.js +++ b/squel-basic.min.js @@ -1,2 +1,2 @@ -/*! squel | https://github.com/hiddentao/squel | BSD license */!function(t,e){"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?module.exports=e():t.squel=e()}(this,function(){"use strict";function t(t){if(Array.isArray(t)){for(var e=0,r=Array(t.length);e1?e-1:0),n=1;e>n;n++)r[n-1]=arguments[n];if(t&&r){var i=!0,a=!1,o=void 0;try{for(var l,s=function(){var e=l.value;"object"===("undefined"==typeof e?"undefined":y(e))&&Object.getOwnPropertyNames(e).forEach(function(r){"function"!=typeof e[r]&&(t[r]=e[r])})},u=r[Symbol.iterator]();!(i=(l=u.next()).done);i=!0)s()}catch(f){a=!0,o=f}finally{try{!i&&u["return"]&&u["return"]()}finally{if(a)throw o}}}return t}function o(t){return t&&t.constructor.prototype===Object.prototype}function l(t){return t&&t.constructor.prototype===Array.prototype}function s(t){if(t&&t.constructor&&t.constructor.toString){var e=t.constructor.toString().match(/function\s*(\w+)/);if(e&&2===e.length)return e[1]}}function u(t){if(!t)return t;if("function"==typeof t.clone)return t.clone();if(!o(t)&&!l(t))return JSON.parse(JSON.stringify(t));var e=function(){var e=new t.constructor;return Object.getOwnPropertyNames(t).forEach(function(r){"function"!=typeof t[r]&&(e[r]=u(t[r]))}),{v:e}}();return"object"===("undefined"==typeof e?"undefined":y(e))?e.v:void 0}function f(t,e,r){var n="undefined"==typeof e?"undefined":y(e);if("function"!==n&&"string"!==n)throw new Error("type must be a class constructor or string");if("function"!=typeof r)throw new Error("handler must be a function");for(var i in t){var a=t[i];if(a.type===e)return void(a.handler=r)}t.push({type:e,handler:r})}function c(t){for(var e=arguments.length,r=Array(e>1?e-1:0),n=1;e>n;n++)r[n-1]=arguments[n];for(var i in r){var a=r[i];for(var o in a){var l=a[o];if(("undefined"==typeof t?"undefined":y(t))===l.type||"string"!=typeof l.type&&t instanceof l.type)return l.handler}}}function h(){var h=arguments.length<=0||void 0===arguments[0]?null:arguments[0],p={_getObjectClassName:s};p.DefaultQueryBuilderOptions={autoQuoteTableNames:!1,autoQuoteFieldNames:!1,autoQuoteAliasNames:!0,useAsForTableAliasNames:!1,nameQuoteCharacter:"`",tableAliasQuoteCharacter:"`",fieldAliasQuoteCharacter:'"',valueHandlers:[],parameterCharacter:"?",numberedParameters:!1,numberedParametersPrefix:"$",numberedParametersStartAt:1,replaceSingleQuotes:!1,singleQuoteReplacement:"''",separator:" "},p.globalValueHandlers=[],p.registerValueHandler=function(t,e){f(p.globalValueHandlers,t,e)},p.Cloneable=function(){function t(){n(this,t)}return d(t,[{key:"clone",value:function(){var t=new this.constructor;return a(t,u(a({},this)))}}]),t}(),p.BaseBuilder=function(i){function o(t){n(this,o);var r=e(this,Object.getPrototypeOf(o).call(this)),i=JSON.parse(JSON.stringify(p.DefaultQueryBuilderOptions));return r.options=a({},i,t),r}return r(o,i),d(o,[{key:"registerValueHandler",value:function(t,e){return f(this.options.valueHandlers,t,e),this}},{key:"_sanitizeExpression",value:function(t){if(!(t instanceof p.Expression)&&"string"!=typeof t)throw new Error("expression must be a string or Expression instance");return t}},{key:"_sanitizeName",value:function(t,e){if("string"!=typeof t)throw new Error(e+" must be a string");return t}},{key:"_sanitizeField",value:function(t){return t instanceof p.BaseBuilder||(t=this._sanitizeName(t,"field name")),t}},{key:"_sanitizeQueryBuilder",value:function(t){if(t instanceof p.QueryBuilder)return t;throw new Error("must be a QueryBuilder instance")}},{key:"_sanitizeTable",value:function(t){if("string"!=typeof t)try{t=this._sanitizeQueryBuilder(t)}catch(e){throw new Error("table name must be a string or a query builder")}else t=this._sanitizeName(t,"table");return t}},{key:"_sanitizeTableAlias",value:function(t){return this._sanitizeName(t,"table alias")}},{key:"_sanitizeFieldAlias",value:function(t){return this._sanitizeName(t,"field alias")}},{key:"_sanitizeLimitOffset",value:function(t){if(t=parseInt(t),0>t||isNaN(t))throw new Error("limit/offset must be >= 0");return t}},{key:"_sanitizeValue",value:function(t){var e="undefined"==typeof t?"undefined":y(t);if(null===t);else if("string"===e||"number"===e||"boolean"===e);else if(t instanceof p.BaseBuilder);else{var r=!!c(t,this.options.valueHandlers,p.globalValueHandlers);if(!r)throw new Error("field value must be a string, number, boolean, null or one of the registered custom value types")}return t}},{key:"_escapeValue",value:function(t){return this.options.replaceSingleQuotes?t.replace(/\'/g,this.options.singleQuoteReplacement):t}},{key:"_formatTableName",value:function(t){if(this.options.autoQuoteTableNames){var e=this.options.nameQuoteCharacter;t=""+e+t+e}return t}},{key:"_formatFieldAlias",value:function(t){if(this.options.autoQuoteAliasNames){var e=this.options.fieldAliasQuoteCharacter;t=""+e+t+e}return t}},{key:"_formatTableAlias",value:function(t){if(this.options.autoQuoteAliasNames){var e=this.options.tableAliasQuoteCharacter;t=""+e+t+e}return this.options.useAsForTableAliasNames?"AS "+t:t}},{key:"_formatFieldName",value:function(t){var e=this,r=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];return this.options.autoQuoteFieldNames&&!function(){var n=e.options.nameQuoteCharacter;t=r.ignorePeriodsForFieldNameQuotes?""+n+t+n:t.split(".").map(function(t){return"*"===t?t:""+n+t+n}).join(".")}(),t}},{key:"_formatCustomValue",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?!1:arguments[1],r=c(t,this.options.valueHandlers,p.globalValueHandlers);return r&&(t=r(t,e)),t}},{key:"_formatValueForParamArray",value:function(t){var e=this;return l(t)?t.map(function(t){return e._formatValueForParamArray(t)}):this._formatCustomValue(t,!0)}},{key:"_formatValueForQueryString",value:function(t){var e=this,r=arguments.length<=1||void 0===arguments[1]?{}:arguments[1],n=this._formatCustomValue(t);if(n!==t)return this._applyNestingFormatting(n);if(l(t))t=t.map(function(t){return e._formatValueForQueryString(t)}),t=this._applyNestingFormatting(t.join(", "));else{var i="undefined"==typeof t?"undefined":y(t);if(null===t)t="NULL";else if("boolean"===i)t=t?"TRUE":"FALSE";else if(t instanceof p.BaseBuilder)t=this._applyNestingFormatting(t.toString());else if("number"!==i)if(r.dontQuote)t=""+t;else{var a=this._escapeValue(t);t="'"+a+"'"}}return t}},{key:"_applyNestingFormatting",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?!0:arguments[1];return t&&"string"==typeof t&&e&&("("!==t.charAt(0)||")"!==t.charAt(t.length-1))?"("+t+")":t}},{key:"_buildString",value:function(e,r){var n=arguments.length<=2||void 0===arguments[2]?{}:arguments[2],i=n.nested,a=n.buildParameterized,o=n.formattingOptions;r=r||[],e=e||"";for(var s="",u=-1,f=[],c=this.options.parameterCharacter,h=0;e.length>h;)if(e.substr(h,c.length)===c){var v=r[++u];if(a)if(v instanceof p.BaseBuilder){var d=v._toParamString({buildParameterized:a,nested:!0});s+=d.text,f.push.apply(f,t(d.values))}else if(v=this._formatValueForParamArray(v),l(v)){var y=v.map(function(){return c}).join(", ");s+="("+y+")",f.push.apply(f,t(v))}else s+=c,f.push(v);else s+=this._formatValueForQueryString(v,o);h+=c.length}else s+=e.charAt(h),h++;return{text:this._applyNestingFormatting(s,!!i),values:f}}},{key:"_buildManyStrings",value:function(e,r){var n=arguments.length<=2||void 0===arguments[2]?{}:arguments[2],i=[],a=[];for(var o in e){var l=e[o],s=r[o],u=this._buildString(l,s,{buildParameterized:n.buildParameterized,nested:!1}),f=u.text,c=u.values;i.push(f),a.push.apply(a,t(c))}return i=i.join(this.options.separator),{text:i.length?this._applyNestingFormatting(i,!!n.nested):"",values:a}}},{key:"_toParamString",value:function(t){throw new Error("Not yet implemented")}},{key:"toString",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return this._toParamString(t).text}},{key:"toParam",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return this._toParamString(a({},t,{buildParameterized:!0}))}}]),o}(p.Cloneable),p.Expression=function(i){function a(t){n(this,a);var r=e(this,Object.getPrototypeOf(a).call(this,t));return r._nodes=[],r}return r(a,i),d(a,[{key:"and",value:function(t,e){for(var r=arguments.length,n=Array(r>2?r-2:0),i=2;r>i;i++)n[i-2]=arguments[i];return this._add("AND",t,e,n)}},{key:"or",value:function(t,e){for(var r=arguments.length,n=Array(r>2?r-2:0),i=2;r>i;i++)n[i-2]=arguments[i];return this._add("OR",t,e,n)}},{key:"_add",value:function(t,e,r,n){var i=["=","<",">","<=",">=","<>","!=","in","not in","like","not like","is","is not"],a=void 0;return"string"==typeof e&&"string"==typeof r&&-1!=i.indexOf(r.toLowerCase())?a=this._buildExpression(e,r):(a=e,n.unshift(r)),this._sanitizeExpression(e),this._nodes.push({type:t,expr:a,para:n}),this}},{key:"_buildExpression",value:function(t,e){var r=this._formatFieldName(t),n=this.options.parameterCharacter,i=r+" "+e+" "+n;return i}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r=[],n=[],i=!0,a=!1,o=void 0;try{for(var l,s=this._nodes[Symbol.iterator]();!(i=(l=s.next()).done);i=!0){var u=l.value,f=u.type,c=u.expr,h=u.para,v=c instanceof p.Expression?c._toParamString({buildParameterized:e.buildParameterized,nested:!0}):this._buildString(c,h,{buildParameterized:e.buildParameterized}),d=v.text,y=v.values;r.length&&r.push(f),r.push(d),n.push.apply(n,t(y))}}catch(g){a=!0,o=g}finally{try{!i&&s["return"]&&s["return"]()}finally{if(a)throw o}}return r=r.join(" "),{text:this._applyNestingFormatting(r,!!e.nested),values:n}}}]),a}(p.BaseBuilder),p.Case=function(l){function s(t){var r=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];n(this,s);var i=e(this,Object.getPrototypeOf(s).call(this,r));return o(t)&&(r=t,t=null),t&&(i._fieldName=i._sanitizeField(t)),i.options=a({},p.DefaultQueryBuilderOptions,r),i._cases=[],i._elseValue=null,i}return r(s,l),d(s,[{key:"when",value:function(t){for(var e=arguments.length,r=Array(e>1?e-1:0),n=1;e>n;n++)r[n-1]=arguments[n];return this._cases.unshift({expression:t,values:r}),this}},{key:"then",value:function(t){if(0==this._cases.length)throw new Error("when() needs to be called first");return this._cases[0].result=t,this}},{key:"else",value:function(t){return this._elseValue=t,this}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r="",n=[],a=!0,o=!1,l=void 0;try{for(var s,u=this._cases[Symbol.iterator]();!(a=(s=u.next()).done);a=!0){var f=s.value,c=f.expression,h=f.values,v=f.result;r=i(r," ");var d=this._buildString(c,h,{buildParameterized:e.buildParameterized,nested:!0});r+="WHEN "+d.text+" THEN "+this._formatValueForQueryString(v),n.push.apply(n,t(d.values))}}catch(y){o=!0,l=y}finally{try{!a&&u["return"]&&u["return"]()}finally{if(o)throw l}}return r.length?(r+=" ELSE "+this._formatValueForQueryString(this._elseValue)+" END",this._fieldName&&(r=this._fieldName+" "+r),r="CASE "+r):r=this._formatValueForQueryString(this._elseValue),{text:r,values:n}}}]),s}(p.BaseBuilder),p.Block=function(t){function i(t){return n(this,i),e(this,Object.getPrototypeOf(i).call(this,t))}return r(i,t),d(i,[{key:"exposedMethods",value:function(){for(var t={},e=this;e;)Object.getOwnPropertyNames(e).forEach(function(r){"constructor"===r||"function"!=typeof e[r]||"_"===r.charAt(0)||p.Block.prototype[r]||(t[r]=e[r])}),e=Object.getPrototypeOf(e);return t}}]),i}(p.BaseBuilder),p.StringBlock=function(t){function i(t,r){n(this,i);var a=e(this,Object.getPrototypeOf(i).call(this,t));return a._str=r,a}return r(i,t),d(i,[{key:"_toParamString",value:function(){arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return{text:this._str,values:[]}}}]),i}(p.Block),p.FunctionBlock=function(t){function i(t){n(this,i);var r=e(this,Object.getPrototypeOf(i).call(this,t));return r._strings=[],r._values=[],r}return r(i,t),d(i,[{key:"function",value:function(t){this._strings.push(t);for(var e=arguments.length,r=Array(e>1?e-1:0),n=1;e>n;n++)r[n-1]=arguments[n];this._values.push(r)}},{key:"_toParamString",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return this._buildManyStrings(this._strings,this._values,t)}}]),i}(p.Block),p.registerValueHandler(p.FunctionBlock,function(t){var e=arguments.length<=1||void 0===arguments[1]?!1:arguments[1];return e?t.toParam():t.toString()}),p.AbstractTableBlock=function(a){function o(t,r){n(this,o);var i=e(this,Object.getPrototypeOf(o).call(this,t));return i._tables=[],i}return r(o,a),d(o,[{key:"_table",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1];e=e?this._sanitizeTableAlias(e):e,t=this._sanitizeTable(t),this.options.singleTable&&(this._tables=[]),this._tables.push({table:t,alias:e})}},{key:"_hasTable",value:function(){return 01)throw new Error("Cannot set multiple rows of fields this way.");"undefined"!=typeof e&&(e=this._sanitizeValue(e)),t=this._sanitizeField(t);var n=this._fields.indexOf(t);-1===n&&(this._fields.push(t),n=this._fields.length-1),this._values[0][n]=e,this._valueOptions[0][n]=r}},{key:"_setFields",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];if("object"!==("undefined"==typeof t?"undefined":y(t)))throw new Error("Expected an object but got "+("undefined"==typeof t?"undefined":y(t)));for(var r in t)this._set(r,t[r],e)}},{key:"_setFieldsRows",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];if(!l(t))throw new Error("Expected an array of objects but got "+("undefined"==typeof t?"undefined":y(t)));this._reset();for(var r in t){var n=t[r];for(var i in n){var a=n[i];i=this._sanitizeField(i),a=this._sanitizeValue(a);var o=this._fields.indexOf(i);if(r>0&&-1===o)throw new Error("All fields in subsequent rows must match the fields in the first row");-1===o&&(this._fields.push(i),o=this._fields.length-1),l(this._values[r])||(this._values[r]=[],this._valueOptions[r]=[]),this._values[r][o]=a,this._valueOptions[r][o]=e}}}}]),i}(p.Block),p.SetFieldBlock=function(a){function o(){return n(this,o),e(this,Object.getPrototypeOf(o).apply(this,arguments))}return r(o,a),d(o,[{key:"set",value:function(t,e,r){this._set(t,e,r)}},{key:"setFields",value:function(t,e){this._setFields(t,e)}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r=e.buildParameterized;if(0>=this._fields.length)throw new Error("set() needs to be called");var n="",a=[];for(var o in this._fields){n=i(n,", ");var l=this._fields[o],s=this._values[0][o];if("undefined"==typeof s)n+=l;else{var u=this._buildString(l+" = "+this.options.parameterCharacter,[s],{buildParameterized:r,formattingOptions:this._valueOptions[0][o]});n+=u.text,a.push.apply(a,t(u.values))}}return{text:"SET "+n,values:a}}}]),o}(p.AbstractSetFieldBlock),p.InsertFieldValueBlock=function(a){function o(){return n(this,o),e(this,Object.getPrototypeOf(o).apply(this,arguments))}return r(o,a),d(o,[{key:"set",value:function(t,e){var r=arguments.length<=2||void 0===arguments[2]?{}:arguments[2];this._set(t,e,r)}},{key:"setFields",value:function(t,e){this._setFields(t,e)}},{key:"setFieldsRows",value:function(t,e){this._setFieldsRows(t,e)}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r=e.buildParameterized,n=this._fields.join(", "),a=[],o=[];for(var l in this._values){a[l]="";for(var s in this._values[l]){var u=this._buildString(this.options.parameterCharacter,[this._values[l][s]],{buildParameterized:r,formattingOptions:this._valueOptions[l][s]});o.push.apply(o,t(u.values)),a[l]=i(a[l],", "),a[l]+=u.text}}return{text:n.length?"("+n+") VALUES ("+a.join("), (")+")":"",values:o}}}]),o}(p.AbstractSetFieldBlock),p.InsertFieldsFromQueryBlock=function(t){function i(t){n(this,i);var r=e(this,Object.getPrototypeOf(i).call(this,t));return r._fields=[],r._query=null,r}return r(i,t),d(i,[{key:"fromQuery",value:function(t,e){var r=this;this._fields=t.map(function(t){return r._sanitizeField(t)}),this._query=this._sanitizeQueryBuilder(e)}},{key:"_toParamString",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],e="",r=[];if(this._fields.length&&this._query){var n=this._query._toParamString({buildParameterized:t.buildParameterized,nested:!0}),i=n.text,a=n.values;e="("+this._fields.join(", ")+") ("+i+")",r=a}return{text:e,values:r}}}]),i}(p.Block),p.DistinctBlock=function(t){function i(){return n(this,i),e(this,Object.getPrototypeOf(i).apply(this,arguments))}return r(i,t),d(i,[{key:"distinct",value:function(){this._useDistinct=!0}},{key:"_toParamString",value:function(){return{text:this._useDistinct?"DISTINCT":"",values:[]}}}]),i}(p.Block),p.GroupByBlock=function(t){function i(t){n(this,i);var r=e(this,Object.getPrototypeOf(i).call(this,t));return r._groups=[],r}return r(i,t),d(i,[{key:"group",value:function(t){this._groups.push(this._sanitizeField(t))}},{key:"_toParamString",value:function(){arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return{text:this._groups.length?"GROUP BY "+this._groups.join(", "):"",values:[]}}}]),i}(p.Block),p.OffsetBlock=function(t){function i(t){n(this,i);var r=e(this,Object.getPrototypeOf(i).call(this,t));return r._offsets=null,r}return r(i,t),d(i,[{key:"offset",value:function(t){this._offsets=this._sanitizeLimitOffset(t)}},{key:"_toParamString",value:function(){return{text:this._offsets?"OFFSET "+this._offsets:"",values:[]}}}]),i}(p.Block),p.AbstractConditionBlock=function(a){function o(t){n(this,o);var r=e(this,Object.getPrototypeOf(o).call(this,t));return r._conditions=[],r}return r(o,a),d(o,[{key:"_condition",value:function(t){for(var e=arguments.length,r=Array(e>1?e-1:0),n=1;e>n;n++)r[n-1]=arguments[n];t=this._sanitizeExpression(t),this._conditions.push({expr:t,values:r})}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r="",n=[],a=!0,o=!1,l=void 0;try{for(var s,u=this._conditions[Symbol.iterator]();!(a=(s=u.next()).done);a=!0){var f=s.value,c=f.expr,h=f.values;r=i(r,") AND (");var v=c instanceof p.Expression?c._toParamString({buildParameterized:e.buildParameterized}):this._buildString(c,h,{buildParameterized:e.buildParameterized});r+=v.text,n.push.apply(n,t(v.values))}}catch(d){o=!0,l=d}finally{try{!a&&u["return"]&&u["return"]()}finally{if(o)throw l}}return{text:r.length?this.options.verb+" ("+r+")":"",values:n}}}]),o}(p.Block),p.WhereBlock=function(t){function i(t){return n(this,i),e(this,Object.getPrototypeOf(i).call(this,a({},t,{verb:"WHERE"})))}return r(i,t),d(i,[{key:"where",value:function(t,e){for(var r=arguments.length,n=Array(r>2?r-2:0),i=2;r>i;i++)n[i-2]=arguments[i];var a=["=","<",">","<=",">=","<>","!=","in","not in","like","not like","is","is not"];if("string"==typeof t&&"string"==typeof e&&-1!=a.indexOf(e.toLowerCase())){var o=new p.Expression(this.options);this._condition(o._add("AND",t,e,n))}else this._condition.apply(this,arguments)}}]),i}(p.AbstractConditionBlock),p.HavingBlock=function(t){function i(t){return n(this,i),e(this,Object.getPrototypeOf(i).call(this,a({},t,{verb:"HAVING"})))}return r(i,t),d(i,[{key:"having",value:function(t,e){for(var r=arguments.length,n=Array(r>2?r-2:0),i=2;r>i;i++)n[i-2]=arguments[i];var a=["=","<",">","<=",">=","<>","!=","in","not in","like","not like","is","is not"];if("string"==typeof t&&"string"==typeof e&&-1!=a.indexOf(e.toLowerCase())){var o=new p.Expression(this.options);this._condition(o._add("AND",t,e,n))}else this._condition.apply(this,arguments)}}]),i}(p.AbstractConditionBlock),p.OrderByBlock=function(a){function o(t){n(this,o);var r=e(this,Object.getPrototypeOf(o).call(this,t));return r._orders=[],r}return r(o,a),d(o,[{key:"order",value:function(t,e){for(var r=arguments.length,n=Array(r>2?r-2:0),i=2;r>i;i++)n[i-2]=arguments[i];t=this._sanitizeField(t),e=void 0===e?!0:e,e=null!==e?!!e:e,this._orders.push({field:t,dir:e,values:n})}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r="",n=[],a=!0,o=!1,l=void 0;try{for(var s,u=this._orders[Symbol.iterator]();!(a=(s=u.next()).done);a=!0){var f=s.value,c=f.field,h=f.dir,v=f.values;r=i(r,", ");var d=this._buildString(c,v,{buildParameterized:e.buildParameterized});r+=d.text,n.push.apply(n,t(d.values)),null!==h&&(r+=" "+(h?"ASC":"DESC"))}}catch(y){o=!0,l=y}finally{try{!a&&u["return"]&&u["return"]()}finally{if(o)throw l}}return{text:r.length?"ORDER BY "+r:"",values:n}}}]),o}(p.Block),p.LimitBlock=function(t){function i(t){n(this,i);var r=e(this,Object.getPrototypeOf(i).call(this,t));return r._limit=null,r}return r(i,t),d(i,[{key:"limit",value:function(t){this._limit=this._sanitizeLimitOffset(t)}},{key:"_toParamString",value:function(){return{text:null!==this._limit?"LIMIT "+this._limit:"",values:[]}}}]),i}(p.Block),p.JoinBlock=function(a){function o(t){n(this,o);var r=e(this,Object.getPrototypeOf(o).call(this,t));return r._joins=[],r}return r(o,a),d(o,[{key:"join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],r=arguments.length<=2||void 0===arguments[2]?null:arguments[2],n=arguments.length<=3||void 0===arguments[3]?"INNER":arguments[3];t=this._sanitizeTable(t,!0),e=e?this._sanitizeTableAlias(e):e,r=r?this._sanitizeExpression(r):r,this._joins.push({type:n,table:t,alias:e,condition:r})}},{key:"left_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],r=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,r,"LEFT")}},{key:"right_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],r=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,r,"RIGHT")}},{key:"outer_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],r=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,r,"OUTER")}},{key:"left_outer_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],r=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,r,"LEFT OUTER")}},{key:"full_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],r=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,r,"FULL")}},{key:"cross_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],r=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,r,"CROSS")}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r="",n=[],a=!0,o=!1,l=void 0;try{for(var s,u=this._joins[Symbol.iterator]();!(a=(s=u.next()).done);a=!0){var f=s.value,c=f.type,h=f.table,v=f.alias,d=f.condition;r=i(r,this.options.separator);var y=void 0;if(h instanceof p.BaseBuilder){var g=h._toParamString({buildParameterized:e.buildParameterized,nested:!0});n.push.apply(n,t(g.values)),y=g.text}else y=this._formatTableName(h);if(r+=c+" JOIN "+y,v&&(r+=" "+this._formatTableAlias(v)),d){r+=" ON ";var _=void 0;_=d instanceof p.Expression?d._toParamString({buildParameterized:e.buildParameterized}):this._buildString(d,[],{buildParameterized:e.buildParameterized}),r+=this._applyNestingFormatting(_.text),n.push.apply(n,t(_.values))}}}catch(m){o=!0,l=m}finally{try{!a&&u["return"]&&u["return"]()}finally{if(o)throw l}}return{text:r,values:n}}}]),o}(p.Block),p.UnionBlock=function(a){function o(t){n(this,o);var r=e(this,Object.getPrototypeOf(o).call(this,t));return r._unions=[],r}return r(o,a),d(o,[{key:"union",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?"UNION":arguments[1];t=this._sanitizeTable(t),this._unions.push({type:e,table:t})}},{key:"union_all",value:function(t){this.union(t,"UNION ALL")}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r="",n=[],a=!0,o=!1,l=void 0;try{for(var s,u=this._unions[Symbol.iterator]();!(a=(s=u.next()).done);a=!0){var f=s.value,c=f.type,h=f.table;r=i(r,this.options.separator);var v=void 0;if(h instanceof p.BaseBuilder){var d=h._toParamString({buildParameterized:e.buildParameterized,nested:!0});v=d.text,n.push.apply(n,t(d.values))}else r=this._formatTableName(h);r+=c+" "+v}}catch(y){o=!0,l=y}finally{try{!a&&u["return"]&&u["return"]()}finally{if(o)throw l}}return{text:r,values:n}}}]),o}(p.Block),p.QueryBuilder=function(i){function o(t,r){n(this,o);var i=e(this,Object.getPrototypeOf(o).call(this,t));i.blocks=r||[];var a=!0,l=!1,s=void 0;try{for(var u,f=i.blocks[Symbol.iterator]();!(a=(u=f.next()).done);a=!0){var c=u.value,h=c.exposedMethods();for(var v in h){var d=h[v];if(void 0!==i[v])throw new Error("Builder already has a builder method called: "+v);!function(t,e,r){i[e]=function(){for(var e=arguments.length,n=Array(e),a=0;e>a;a++)n[a]=arguments[a];return r.call.apply(r,[t].concat(n)),i}}(c,v,d)}}}catch(y){l=!0,s=y}finally{try{!a&&f["return"]&&f["return"]()}finally{if(l)throw s}}return i}return r(o,i),d(o,[{key:"registerValueHandler",value:function(t,e){var r=!0,n=!1,i=void 0;try{for(var a,l=this.blocks[Symbol.iterator]();!(r=(a=l.next()).done);r=!0){var s=a.value;s.registerValueHandler(t,e)}}catch(u){n=!0,i=u}finally{try{!r&&l["return"]&&l["return"]()}finally{if(n)throw i}}return v(Object.getPrototypeOf(o.prototype),"registerValueHandler",this).call(this,t,e),this}},{key:"updateOptions",value:function(t){this.options=a({},this.options,t);var e=!0,r=!1,n=void 0;try{for(var i,o=this.blocks[Symbol.iterator]();!(e=(i=o.next()).done);e=!0){var l=i.value;l.options=a({},l.options,t)}}catch(s){r=!0,n=s}finally{try{!e&&o["return"]&&o["return"]()}finally{if(r)throw n}}}},{key:"_toParamString",value:function(){var e,r=this,n=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];n=a({},this.options,n);var i=this.blocks.map(function(t){return t._toParamString({buildParameterized:n.buildParameterized,queryBuilder:r})}),o=i.map(function(t){return t.text}),l=i.map(function(t){return t.values}),s=o.filter(function(t){return 01?e-1:0),n=1;e>n;n++)r[n-1]=arguments[n];if(t&&r){var i=!0,a=!1,o=void 0;try{for(var l,s=function(){var e=l.value;"object"===("undefined"==typeof e?"undefined":y(e))&&Object.getOwnPropertyNames(e).forEach(function(r){"function"!=typeof e[r]&&(t[r]=e[r])})},u=r[Symbol.iterator]();!(i=(l=u.next()).done);i=!0)s()}catch(f){a=!0,o=f}finally{try{!i&&u["return"]&&u["return"]()}finally{if(a)throw o}}}return t}function o(t){return t&&t.constructor.prototype===Object.prototype}function l(t){return t&&t.constructor.prototype===Array.prototype}function s(t){if(t&&t.constructor&&t.constructor.toString){var e=t.constructor.toString().match(/function\s*(\w+)/);if(e&&2===e.length)return e[1]}}function u(t){if(!t)return t;if("function"==typeof t.clone)return t.clone();if(!o(t)&&!l(t))return JSON.parse(JSON.stringify(t));var e=function(){var e=new t.constructor;return Object.getOwnPropertyNames(t).forEach(function(r){"function"!=typeof t[r]&&(e[r]=u(t[r]))}),{v:e}}();return"object"===("undefined"==typeof e?"undefined":y(e))?e.v:void 0}function f(t,e,r){var n="undefined"==typeof e?"undefined":y(e);if("function"!==n&&"string"!==n)throw new Error("type must be a class constructor or string");if("function"!=typeof r)throw new Error("handler must be a function");for(var i in t){var a=t[i];if(a.type===e)return void(a.handler=r)}t.push({type:e,handler:r})}function c(t){for(var e=arguments.length,r=Array(e>1?e-1:0),n=1;e>n;n++)r[n-1]=arguments[n];for(var i in r){var a=r[i];for(var o in a){var l=a[o];if(("undefined"==typeof t?"undefined":y(t))===l.type||"string"!=typeof l.type&&t instanceof l.type)return l.handler}}}function h(){var h=arguments.length<=0||void 0===arguments[0]?null:arguments[0],p={_getObjectClassName:s};p.DefaultQueryBuilderOptions={autoQuoteTableNames:!1,autoQuoteFieldNames:!1,autoQuoteAliasNames:!0,useAsForTableAliasNames:!1,nameQuoteCharacter:"`",tableAliasQuoteCharacter:"`",fieldAliasQuoteCharacter:'"',valueHandlers:[],parameterCharacter:"?",numberedParameters:!1,numberedParametersPrefix:"$",numberedParametersStartAt:1,replaceSingleQuotes:!1,singleQuoteReplacement:"''",separator:" "},p.globalValueHandlers=[],p.registerValueHandler=function(t,e){f(p.globalValueHandlers,t,e)},p.Cloneable=function(){function t(){n(this,t)}return d(t,[{key:"clone",value:function(){var t=new this.constructor;return a(t,u(a({},this)))}}]),t}(),p.BaseBuilder=function(i){function o(t){n(this,o);var r=e(this,Object.getPrototypeOf(o).call(this)),i=JSON.parse(JSON.stringify(p.DefaultQueryBuilderOptions));return r.options=a({},i,t),r}return r(o,i),d(o,[{key:"registerValueHandler",value:function(t,e){return f(this.options.valueHandlers,t,e),this}},{key:"_sanitizeExpression",value:function(t){if(!(t instanceof p.Expression)&&"string"!=typeof t)throw new Error("expression must be a string or Expression instance");return t}},{key:"_sanitizeName",value:function(t,e){if("string"!=typeof t)throw new Error(e+" must be a string");return t}},{key:"_sanitizeField",value:function(t){return t instanceof p.BaseBuilder||(t=this._sanitizeName(t,"field name")),t}},{key:"_sanitizeQueryBuilder",value:function(t){if(t instanceof p.QueryBuilder)return t;throw new Error("must be a QueryBuilder instance")}},{key:"_sanitizeTable",value:function(t){if("string"!=typeof t)try{t=this._sanitizeQueryBuilder(t)}catch(e){throw new Error("table name must be a string or a query builder")}else t=this._sanitizeName(t,"table");return t}},{key:"_sanitizeTableAlias",value:function(t){return this._sanitizeName(t,"table alias")}},{key:"_sanitizeFieldAlias",value:function(t){return this._sanitizeName(t,"field alias")}},{key:"_sanitizeLimitOffset",value:function(t){if(t=parseInt(t),0>t||isNaN(t))throw new Error("limit/offset must be >= 0");return t}},{key:"_sanitizeValue",value:function(t){var e="undefined"==typeof t?"undefined":y(t);if(null===t);else if("string"===e||"number"===e||"boolean"===e);else if(t instanceof p.BaseBuilder);else{var r=!!c(t,this.options.valueHandlers,p.globalValueHandlers);if(!r)throw new Error("field value must be a string, number, boolean, null or one of the registered custom value types")}return t}},{key:"_escapeValue",value:function(t){return this.options.replaceSingleQuotes?t.replace(/\'/g,this.options.singleQuoteReplacement):t}},{key:"_formatTableName",value:function(t){if(this.options.autoQuoteTableNames){var e=this.options.nameQuoteCharacter;t=""+e+t+e}return t}},{key:"_formatFieldAlias",value:function(t){if(this.options.autoQuoteAliasNames){var e=this.options.fieldAliasQuoteCharacter;t=""+e+t+e}return t}},{key:"_formatTableAlias",value:function(t){if(this.options.autoQuoteAliasNames){var e=this.options.tableAliasQuoteCharacter;t=""+e+t+e}return this.options.useAsForTableAliasNames?"AS "+t:t}},{key:"_formatFieldName",value:function(t){var e=this,r=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];return this.options.autoQuoteFieldNames&&!function(){var n=e.options.nameQuoteCharacter;t=r.ignorePeriodsForFieldNameQuotes?""+n+t+n:t.split(".").map(function(t){return"*"===t?t:""+n+t+n}).join(".")}(),t}},{key:"_formatCustomValue",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?!1:arguments[1],r=c(t,this.options.valueHandlers,p.globalValueHandlers);return r&&(t=r(t,e)),t}},{key:"_formatValueForParamArray",value:function(t){var e=this;return l(t)?t.map(function(t){return e._formatValueForParamArray(t)}):this._formatCustomValue(t,!0)}},{key:"_formatValueForQueryString",value:function(t){var e=this,r=arguments.length<=1||void 0===arguments[1]?{}:arguments[1],n=this._formatCustomValue(t);if(n!==t)return this._applyNestingFormatting(n);if(l(t))t=t.map(function(t){return e._formatValueForQueryString(t)}),t=this._applyNestingFormatting(t.join(", "));else{var i="undefined"==typeof t?"undefined":y(t);if(null===t)t="NULL";else if("boolean"===i)t=t?"TRUE":"FALSE";else if(t instanceof p.BaseBuilder)t=this._applyNestingFormatting(t.toString());else if("number"!==i)if(r.dontQuote)t=""+t;else{var a=this._escapeValue(t);t="'"+a+"'"}}return t}},{key:"_applyNestingFormatting",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?!0:arguments[1];return t&&"string"==typeof t&&e&&("("!==t.charAt(0)||")"!==t.charAt(t.length-1))?"("+t+")":t}},{key:"_buildString",value:function(e,r){var n=arguments.length<=2||void 0===arguments[2]?{}:arguments[2],i=n.nested,a=n.buildParameterized,o=n.formattingOptions;r=r||[],e=e||"";for(var s="",u=-1,f=[],c=this.options.parameterCharacter,h=0;e.length>h;)if(e.substr(h,c.length)===c){var v=r[++u];if(a)if(v instanceof p.BaseBuilder){var d=v._toParamString({buildParameterized:a,nested:!0});s+=d.text,f.push.apply(f,t(d.values))}else if(v=this._formatValueForParamArray(v),l(v)){var y=v.map(function(){return c}).join(", ");s+="("+y+")",f.push.apply(f,t(v))}else s+=c,f.push(v);else s+=this._formatValueForQueryString(v,o);h+=c.length}else s+=e.charAt(h),h++;return{text:this._applyNestingFormatting(s,!!i),values:f}}},{key:"_buildManyStrings",value:function(e,r){var n=arguments.length<=2||void 0===arguments[2]?{}:arguments[2],i=[],a=[];for(var o in e){var l=e[o],s=r[o],u=this._buildString(l,s,{buildParameterized:n.buildParameterized,nested:!1}),f=u.text,c=u.values;i.push(f),a.push.apply(a,t(c))}return i=i.join(this.options.separator),{text:i.length?this._applyNestingFormatting(i,!!n.nested):"",values:a}}},{key:"_toParamString",value:function(t){throw new Error("Not yet implemented")}},{key:"toString",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return this._toParamString(t).text}},{key:"toParam",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return this._toParamString(a({},t,{buildParameterized:!0}))}}]),o}(p.Cloneable),p.Expression=function(i){function a(t){n(this,a);var r=e(this,Object.getPrototypeOf(a).call(this,t));return r._nodes=[],r}return r(a,i),d(a,[{key:"and",value:function(t,e){for(var r=arguments.length,n=Array(r>2?r-2:0),i=2;r>i;i++)n[i-2]=arguments[i];return this._add("AND",t,e,n)}},{key:"or",value:function(t,e){for(var r=arguments.length,n=Array(r>2?r-2:0),i=2;r>i;i++)n[i-2]=arguments[i];return this._add("OR",t,e,n)}},{key:"_add",value:function(t,e,r,n){var i=["=","<",">","<=",">=","<>","!=","in","not in","like","not like","is","is not"],a=void 0;return"string"==typeof e&&"string"==typeof r&&-1!=i.indexOf(r.toLowerCase())?a=this._buildExpression(e,r):(a=e,n.unshift(r)),a=this._sanitizeExpression(a),this._nodes.push({type:t,expr:a,para:n}),this}},{key:"_buildExpression",value:function(t,e){var r=this._formatFieldName(t),n=this.options.parameterCharacter,i=r+" "+e+" "+n;return i}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r=[],n=[],i=!0,a=!1,o=void 0;try{for(var l,s=this._nodes[Symbol.iterator]();!(i=(l=s.next()).done);i=!0){var u=l.value,f=u.type,c=u.expr,h=u.para,v=c instanceof p.Expression?c._toParamString({buildParameterized:e.buildParameterized,nested:!0}):this._buildString(c,h,{buildParameterized:e.buildParameterized}),d=v.text,y=v.values;r.length&&r.push(f),r.push(d),n.push.apply(n,t(y))}}catch(g){a=!0,o=g}finally{try{!i&&s["return"]&&s["return"]()}finally{if(a)throw o}}return r=r.join(" "),{text:this._applyNestingFormatting(r,!!e.nested),values:n}}}]),a}(p.BaseBuilder),p.Case=function(l){function s(t){var r=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];n(this,s);var i=e(this,Object.getPrototypeOf(s).call(this,r));return o(t)&&(r=t,t=null),t&&(i._fieldName=i._sanitizeField(t)),i.options=a({},p.DefaultQueryBuilderOptions,r),i._cases=[],i._elseValue=null,i}return r(s,l),d(s,[{key:"when",value:function(t){for(var e=arguments.length,r=Array(e>1?e-1:0),n=1;e>n;n++)r[n-1]=arguments[n];return this._cases.unshift({expression:t,values:r}),this}},{key:"then",value:function(t){if(0==this._cases.length)throw new Error("when() needs to be called first");return this._cases[0].result=t,this}},{key:"else",value:function(t){return this._elseValue=t,this}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r="",n=[],a=!0,o=!1,l=void 0;try{for(var s,u=this._cases[Symbol.iterator]();!(a=(s=u.next()).done);a=!0){var f=s.value,c=f.expression,h=f.values,v=f.result;r=i(r," ");var d=this._buildString(c,h,{buildParameterized:e.buildParameterized,nested:!0});r+="WHEN "+d.text+" THEN "+this._formatValueForQueryString(v),n.push.apply(n,t(d.values))}}catch(y){o=!0,l=y}finally{try{!a&&u["return"]&&u["return"]()}finally{if(o)throw l}}return r.length?(r+=" ELSE "+this._formatValueForQueryString(this._elseValue)+" END",this._fieldName&&(r=this._fieldName+" "+r),r="CASE "+r):r=this._formatValueForQueryString(this._elseValue),{text:r,values:n}}}]),s}(p.BaseBuilder),p.Block=function(t){function i(t){return n(this,i),e(this,Object.getPrototypeOf(i).call(this,t))}return r(i,t),d(i,[{key:"exposedMethods",value:function(){for(var t={},e=this;e;)Object.getOwnPropertyNames(e).forEach(function(r){"constructor"===r||"function"!=typeof e[r]||"_"===r.charAt(0)||p.Block.prototype[r]||(t[r]=e[r])}),e=Object.getPrototypeOf(e);return t}}]),i}(p.BaseBuilder),p.StringBlock=function(t){function i(t,r){n(this,i);var a=e(this,Object.getPrototypeOf(i).call(this,t));return a._str=r,a}return r(i,t),d(i,[{key:"_toParamString",value:function(){arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return{text:this._str,values:[]}}}]),i}(p.Block),p.FunctionBlock=function(t){function i(t){n(this,i);var r=e(this,Object.getPrototypeOf(i).call(this,t));return r._strings=[],r._values=[],r}return r(i,t),d(i,[{key:"function",value:function(t){this._strings.push(t);for(var e=arguments.length,r=Array(e>1?e-1:0),n=1;e>n;n++)r[n-1]=arguments[n];this._values.push(r)}},{key:"_toParamString",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return this._buildManyStrings(this._strings,this._values,t)}}]),i}(p.Block),p.registerValueHandler(p.FunctionBlock,function(t){var e=arguments.length<=1||void 0===arguments[1]?!1:arguments[1];return e?t.toParam():t.toString()}),p.AbstractTableBlock=function(a){function o(t,r){n(this,o);var i=e(this,Object.getPrototypeOf(o).call(this,t));return i._tables=[],i}return r(o,a),d(o,[{key:"_table",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1];e=e?this._sanitizeTableAlias(e):e,t=this._sanitizeTable(t),this.options.singleTable&&(this._tables=[]),this._tables.push({table:t,alias:e})}},{key:"_hasTable",value:function(){return 01)throw new Error("Cannot set multiple rows of fields this way.");"undefined"!=typeof e&&(e=this._sanitizeValue(e)),t=this._sanitizeField(t);var n=this._fields.indexOf(t);-1===n&&(this._fields.push(t),n=this._fields.length-1),this._values[0][n]=e,this._valueOptions[0][n]=r}},{key:"_setFields",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];if("object"!==("undefined"==typeof t?"undefined":y(t)))throw new Error("Expected an object but got "+("undefined"==typeof t?"undefined":y(t)));for(var r in t)this._set(r,t[r],e)}},{key:"_setFieldsRows",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];if(!l(t))throw new Error("Expected an array of objects but got "+("undefined"==typeof t?"undefined":y(t)));this._reset();for(var r in t){var n=t[r];for(var i in n){var a=n[i];i=this._sanitizeField(i),a=this._sanitizeValue(a);var o=this._fields.indexOf(i);if(r>0&&-1===o)throw new Error("All fields in subsequent rows must match the fields in the first row");-1===o&&(this._fields.push(i),o=this._fields.length-1),l(this._values[r])||(this._values[r]=[],this._valueOptions[r]=[]),this._values[r][o]=a,this._valueOptions[r][o]=e}}}}]),i}(p.Block),p.SetFieldBlock=function(a){function o(){return n(this,o),e(this,Object.getPrototypeOf(o).apply(this,arguments))}return r(o,a),d(o,[{key:"set",value:function(t,e,r){this._set(t,e,r)}},{key:"setFields",value:function(t,e){this._setFields(t,e)}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r=e.buildParameterized;if(0>=this._fields.length)throw new Error("set() needs to be called");var n="",a=[];for(var o in this._fields){n=i(n,", ");var l=this._fields[o],s=this._values[0][o];if("undefined"==typeof s)n+=l;else{var u=this._buildString(l+" = "+this.options.parameterCharacter,[s],{buildParameterized:r,formattingOptions:this._valueOptions[0][o]});n+=u.text,a.push.apply(a,t(u.values))}}return{text:"SET "+n,values:a}}}]),o}(p.AbstractSetFieldBlock),p.InsertFieldValueBlock=function(a){function o(){return n(this,o),e(this,Object.getPrototypeOf(o).apply(this,arguments))}return r(o,a),d(o,[{key:"set",value:function(t,e){var r=arguments.length<=2||void 0===arguments[2]?{}:arguments[2];this._set(t,e,r)}},{key:"setFields",value:function(t,e){this._setFields(t,e)}},{key:"setFieldsRows",value:function(t,e){this._setFieldsRows(t,e)}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r=e.buildParameterized,n=this._fields.join(", "),a=[],o=[];for(var l in this._values){a[l]="";for(var s in this._values[l]){var u=this._buildString(this.options.parameterCharacter,[this._values[l][s]],{buildParameterized:r,formattingOptions:this._valueOptions[l][s]});o.push.apply(o,t(u.values)),a[l]=i(a[l],", "),a[l]+=u.text}}return{text:n.length?"("+n+") VALUES ("+a.join("), (")+")":"",values:o}}}]),o}(p.AbstractSetFieldBlock),p.InsertFieldsFromQueryBlock=function(t){function i(t){n(this,i);var r=e(this,Object.getPrototypeOf(i).call(this,t));return r._fields=[],r._query=null,r}return r(i,t),d(i,[{key:"fromQuery",value:function(t,e){var r=this;this._fields=t.map(function(t){return r._sanitizeField(t)}),this._query=this._sanitizeQueryBuilder(e)}},{key:"_toParamString",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],e="",r=[];if(this._fields.length&&this._query){var n=this._query._toParamString({buildParameterized:t.buildParameterized,nested:!0}),i=n.text,a=n.values;e="("+this._fields.join(", ")+") ("+i+")",r=a}return{text:e,values:r}}}]),i}(p.Block),p.DistinctBlock=function(t){function i(){return n(this,i),e(this,Object.getPrototypeOf(i).apply(this,arguments))}return r(i,t),d(i,[{key:"distinct",value:function(){this._useDistinct=!0}},{key:"_toParamString",value:function(){return{text:this._useDistinct?"DISTINCT":"",values:[]}}}]),i}(p.Block),p.GroupByBlock=function(t){function i(t){n(this,i);var r=e(this,Object.getPrototypeOf(i).call(this,t));return r._groups=[],r}return r(i,t),d(i,[{key:"group",value:function(t){this._groups.push(this._sanitizeField(t))}},{key:"_toParamString",value:function(){arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return{text:this._groups.length?"GROUP BY "+this._groups.join(", "):"",values:[]}}}]),i}(p.Block),p.OffsetBlock=function(t){function i(t){n(this,i);var r=e(this,Object.getPrototypeOf(i).call(this,t));return r._offsets=null,r}return r(i,t),d(i,[{key:"offset",value:function(t){this._offsets=this._sanitizeLimitOffset(t)}},{key:"_toParamString",value:function(){return{text:this._offsets?"OFFSET "+this._offsets:"",values:[]}}}]),i}(p.Block),p.AbstractConditionBlock=function(a){function o(t){n(this,o);var r=e(this,Object.getPrototypeOf(o).call(this,t));return r._conditions=[],r}return r(o,a),d(o,[{key:"_condition",value:function(t){for(var e=arguments.length,r=Array(e>1?e-1:0),n=1;e>n;n++)r[n-1]=arguments[n];t=this._sanitizeExpression(t),this._conditions.push({expr:t,values:r})}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r="",n=[],a=!0,o=!1,l=void 0;try{for(var s,u=this._conditions[Symbol.iterator]();!(a=(s=u.next()).done);a=!0){var f=s.value,c=f.expr,h=f.values;r=i(r,") AND (");var v=c instanceof p.Expression?c._toParamString({buildParameterized:e.buildParameterized}):this._buildString(c,h,{buildParameterized:e.buildParameterized});r+=v.text,n.push.apply(n,t(v.values))}}catch(d){o=!0,l=d}finally{try{!a&&u["return"]&&u["return"]()}finally{if(o)throw l}}return{text:r.length?this.options.verb+" ("+r+")":"",values:n}}}]),o}(p.Block),p.WhereBlock=function(t){function i(t){return n(this,i),e(this,Object.getPrototypeOf(i).call(this,a({},t,{verb:"WHERE"})))}return r(i,t),d(i,[{key:"where",value:function(t,e){for(var r=arguments.length,n=Array(r>2?r-2:0),i=2;r>i;i++)n[i-2]=arguments[i];var a=["=","<",">","<=",">=","<>","!=","in","not in","like","not like","is","is not"];if("string"==typeof t&&"string"==typeof e&&-1!=a.indexOf(e.toLowerCase())){var o=new p.Expression(this.options);this._condition(o._add("AND",t,e,n))}else this._condition.apply(this,arguments)}}]),i}(p.AbstractConditionBlock),p.HavingBlock=function(t){function i(t){return n(this,i),e(this,Object.getPrototypeOf(i).call(this,a({},t,{verb:"HAVING"})))}return r(i,t),d(i,[{key:"having",value:function(t,e){for(var r=arguments.length,n=Array(r>2?r-2:0),i=2;r>i;i++)n[i-2]=arguments[i];var a=["=","<",">","<=",">=","<>","!=","in","not in","like","not like","is","is not"];if("string"==typeof t&&"string"==typeof e&&-1!=a.indexOf(e.toLowerCase())){var o=new p.Expression(this.options);this._condition(o._add("AND",t,e,n))}else this._condition.apply(this,arguments)}}]),i}(p.AbstractConditionBlock),p.OrderByBlock=function(a){function o(t){n(this,o);var r=e(this,Object.getPrototypeOf(o).call(this,t));return r._orders=[],r}return r(o,a),d(o,[{key:"order",value:function(t,e){for(var r=arguments.length,n=Array(r>2?r-2:0),i=2;r>i;i++)n[i-2]=arguments[i];t=this._sanitizeField(t),e=void 0===e?!0:e,e=null!==e?!!e:e,this._orders.push({field:t,dir:e,values:n})}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r="",n=[],a=!0,o=!1,l=void 0;try{for(var s,u=this._orders[Symbol.iterator]();!(a=(s=u.next()).done);a=!0){var f=s.value,c=f.field,h=f.dir,v=f.values;r=i(r,", ");var d=this._buildString(c,v,{buildParameterized:e.buildParameterized});r+=d.text,n.push.apply(n,t(d.values)),null!==h&&(r+=" "+(h?"ASC":"DESC"))}}catch(y){o=!0,l=y}finally{try{!a&&u["return"]&&u["return"]()}finally{if(o)throw l}}return{text:r.length?"ORDER BY "+r:"",values:n}}}]),o}(p.Block),p.LimitBlock=function(t){function i(t){n(this,i);var r=e(this,Object.getPrototypeOf(i).call(this,t));return r._limit=null,r}return r(i,t),d(i,[{key:"limit",value:function(t){this._limit=this._sanitizeLimitOffset(t)}},{key:"_toParamString",value:function(){return{text:null!==this._limit?"LIMIT "+this._limit:"",values:[]}}}]),i}(p.Block),p.JoinBlock=function(a){function o(t){n(this,o);var r=e(this,Object.getPrototypeOf(o).call(this,t));return r._joins=[],r}return r(o,a),d(o,[{key:"join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],r=arguments.length<=2||void 0===arguments[2]?null:arguments[2],n=arguments.length<=3||void 0===arguments[3]?"INNER":arguments[3];t=this._sanitizeTable(t,!0),e=e?this._sanitizeTableAlias(e):e,r=r?this._sanitizeExpression(r):r,this._joins.push({type:n,table:t,alias:e,condition:r})}},{key:"left_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],r=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,r,"LEFT")}},{key:"right_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],r=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,r,"RIGHT")}},{key:"outer_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],r=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,r,"OUTER")}},{key:"left_outer_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],r=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,r,"LEFT OUTER")}},{key:"full_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],r=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,r,"FULL")}},{key:"cross_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],r=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,r,"CROSS")}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r="",n=[],a=!0,o=!1,l=void 0;try{for(var s,u=this._joins[Symbol.iterator]();!(a=(s=u.next()).done);a=!0){var f=s.value,c=f.type,h=f.table,v=f.alias,d=f.condition;r=i(r,this.options.separator);var y=void 0;if(h instanceof p.BaseBuilder){var g=h._toParamString({buildParameterized:e.buildParameterized,nested:!0});n.push.apply(n,t(g.values)),y=g.text}else y=this._formatTableName(h);if(r+=c+" JOIN "+y,v&&(r+=" "+this._formatTableAlias(v)),d){r+=" ON ";var _=void 0;_=d instanceof p.Expression?d._toParamString({buildParameterized:e.buildParameterized}):this._buildString(d,[],{buildParameterized:e.buildParameterized}),r+=this._applyNestingFormatting(_.text),n.push.apply(n,t(_.values))}}}catch(m){o=!0,l=m}finally{try{!a&&u["return"]&&u["return"]()}finally{if(o)throw l}}return{text:r,values:n}}}]),o}(p.Block),p.UnionBlock=function(a){function o(t){n(this,o);var r=e(this,Object.getPrototypeOf(o).call(this,t));return r._unions=[],r}return r(o,a),d(o,[{key:"union",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?"UNION":arguments[1];t=this._sanitizeTable(t),this._unions.push({type:e,table:t})}},{key:"union_all",value:function(t){this.union(t,"UNION ALL")}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r="",n=[],a=!0,o=!1,l=void 0;try{for(var s,u=this._unions[Symbol.iterator]();!(a=(s=u.next()).done);a=!0){var f=s.value,c=f.type,h=f.table;r=i(r,this.options.separator);var v=void 0;if(h instanceof p.BaseBuilder){var d=h._toParamString({buildParameterized:e.buildParameterized,nested:!0});v=d.text,n.push.apply(n,t(d.values))}else r=this._formatTableName(h);r+=c+" "+v}}catch(y){o=!0,l=y}finally{try{!a&&u["return"]&&u["return"]()}finally{if(o)throw l}}return{text:r,values:n}}}]),o}(p.Block),p.QueryBuilder=function(i){function o(t,r){n(this,o);var i=e(this,Object.getPrototypeOf(o).call(this,t));i.blocks=r||[];var a=!0,l=!1,s=void 0;try{for(var u,f=i.blocks[Symbol.iterator]();!(a=(u=f.next()).done);a=!0){var c=u.value,h=c.exposedMethods();for(var v in h){var d=h[v];if(void 0!==i[v])throw new Error("Builder already has a builder method called: "+v);!function(t,e,r){i[e]=function(){for(var e=arguments.length,n=Array(e),a=0;e>a;a++)n[a]=arguments[a];return r.call.apply(r,[t].concat(n)),i}}(c,v,d)}}}catch(y){l=!0,s=y}finally{try{!a&&f["return"]&&f["return"]()}finally{if(l)throw s}}return i}return r(o,i),d(o,[{key:"registerValueHandler",value:function(t,e){var r=!0,n=!1,i=void 0;try{for(var a,l=this.blocks[Symbol.iterator]();!(r=(a=l.next()).done);r=!0){var s=a.value;s.registerValueHandler(t,e)}}catch(u){n=!0,i=u}finally{try{!r&&l["return"]&&l["return"]()}finally{if(n)throw i}}return v(Object.getPrototypeOf(o.prototype),"registerValueHandler",this).call(this,t,e),this}},{key:"updateOptions",value:function(t){this.options=a({},this.options,t);var e=!0,r=!1,n=void 0;try{for(var i,o=this.blocks[Symbol.iterator]();!(e=(i=o.next()).done);e=!0){var l=i.value;l.options=a({},l.options,t)}}catch(s){r=!0,n=s}finally{try{!e&&o["return"]&&o["return"]()}finally{if(r)throw n}}}},{key:"_toParamString",value:function(){var e,r=this,n=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];n=a({},this.options,n);var i=this.blocks.map(function(t){return t._toParamString({buildParameterized:n.buildParameterized,queryBuilder:r})}),o=i.map(function(t){return t.text}),l=i.map(function(t){return t.values}),s=o.filter(function(t){return 01?e-1:0),r=1;e>r;r++)n[r-1]=arguments[r];if(t&&n){var i=!0,o=!1,a=void 0;try{for(var l,u=function(){var e=l.value;"object"===("undefined"==typeof e?"undefined":p(e))&&Object.getOwnPropertyNames(e).forEach(function(n){"function"!=typeof e[n]&&(t[n]=e[n])})},s=n[Symbol.iterator]();!(i=(l=s.next()).done);i=!0)u()}catch(c){o=!0,a=c}finally{try{!i&&s["return"]&&s["return"]()}finally{if(o)throw a}}}return t}function a(t){return t&&t.constructor.prototype===Object.prototype}function l(t){return t&&t.constructor.prototype===Array.prototype}function u(t){if(t&&t.constructor&&t.constructor.toString){var e=t.constructor.toString().match(/function\s*(\w+)/);if(e&&2===e.length)return e[1]}}function s(t){if(!t)return t;if("function"==typeof t.clone)return t.clone();if(!a(t)&&!l(t))return JSON.parse(JSON.stringify(t));var e=function(){var e=new t.constructor;return Object.getOwnPropertyNames(t).forEach(function(n){"function"!=typeof t[n]&&(e[n]=s(t[n]))}),{v:e}}();return"object"===("undefined"==typeof e?"undefined":p(e))?e.v:void 0}function c(t,e,n){var r="undefined"==typeof e?"undefined":p(e);if("function"!==r&&"string"!==r)throw new Error("type must be a class constructor or string");if("function"!=typeof n)throw new Error("handler must be a function");for(var i in t){var o=t[i];if(o.type===e)return void(o.handler=n)}t.push({type:e,handler:n})}function f(t){for(var e=arguments.length,n=Array(e>1?e-1:0),r=1;e>r;r++)n[r-1]=arguments[r];for(var i in n){var o=n[i];for(var a in o){var l=o[a];if(("undefined"==typeof t?"undefined":p(t))===l.type||"string"!=typeof l.type&&t instanceof l.type)return l.handler}}}function h(){var h=arguments.length<=0||void 0===arguments[0]?null:arguments[0],y={_getObjectClassName:u};y.DefaultQueryBuilderOptions={autoQuoteTableNames:!1,autoQuoteFieldNames:!1,autoQuoteAliasNames:!0,useAsForTableAliasNames:!1,nameQuoteCharacter:"`",tableAliasQuoteCharacter:"`",fieldAliasQuoteCharacter:'"',valueHandlers:[],parameterCharacter:"?",numberedParameters:!1,numberedParametersPrefix:"$",numberedParametersStartAt:1,replaceSingleQuotes:!1,singleQuoteReplacement:"''",separator:" "},y.globalValueHandlers=[],y.registerValueHandler=function(t,e){c(y.globalValueHandlers,t,e)},y.Cloneable=function(){function t(){r(this,t)}return d(t,[{key:"clone",value:function(){var t=new this.constructor;return o(t,s(o({},this)))}}]),t}(),y.BaseBuilder=function(i){function a(t){r(this,a);var n=e(this,Object.getPrototypeOf(a).call(this)),i=JSON.parse(JSON.stringify(y.DefaultQueryBuilderOptions));return n.options=o({},i,t),n}return n(a,i),d(a,[{key:"registerValueHandler",value:function(t,e){return c(this.options.valueHandlers,t,e),this}},{key:"_sanitizeExpression",value:function(t){if(!(t instanceof y.Expression)&&"string"!=typeof t)throw new Error("expression must be a string or Expression instance");return t}},{key:"_sanitizeName",value:function(t,e){if("string"!=typeof t)throw new Error(e+" must be a string");return t}},{key:"_sanitizeField",value:function(t){return t instanceof y.BaseBuilder||(t=this._sanitizeName(t,"field name")),t}},{key:"_sanitizeQueryBuilder",value:function(t){if(t instanceof y.QueryBuilder)return t;throw new Error("must be a QueryBuilder instance")}},{key:"_sanitizeTable",value:function(t){if("string"!=typeof t)try{t=this._sanitizeQueryBuilder(t)}catch(e){throw new Error("table name must be a string or a query builder")}else t=this._sanitizeName(t,"table");return t}},{key:"_sanitizeTableAlias",value:function(t){return this._sanitizeName(t,"table alias")}},{key:"_sanitizeFieldAlias",value:function(t){return this._sanitizeName(t,"field alias")}},{key:"_sanitizeLimitOffset",value:function(t){if(t=parseInt(t),0>t||isNaN(t))throw new Error("limit/offset must be >= 0");return t}},{key:"_sanitizeValue",value:function(t){var e="undefined"==typeof t?"undefined":p(t);if(null===t);else if("string"===e||"number"===e||"boolean"===e);else if(t instanceof y.BaseBuilder);else{var n=!!f(t,this.options.valueHandlers,y.globalValueHandlers);if(!n)throw new Error("field value must be a string, number, boolean, null or one of the registered custom value types")}return t}},{key:"_escapeValue",value:function(t){return this.options.replaceSingleQuotes?t.replace(/\'/g,this.options.singleQuoteReplacement):t}},{key:"_formatTableName",value:function(t){if(this.options.autoQuoteTableNames){var e=this.options.nameQuoteCharacter;t=""+e+t+e}return t}},{key:"_formatFieldAlias",value:function(t){if(this.options.autoQuoteAliasNames){var e=this.options.fieldAliasQuoteCharacter;t=""+e+t+e}return t}},{key:"_formatTableAlias",value:function(t){if(this.options.autoQuoteAliasNames){var e=this.options.tableAliasQuoteCharacter;t=""+e+t+e}return this.options.useAsForTableAliasNames?"AS "+t:t}},{key:"_formatFieldName",value:function(t){var e=this,n=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];return this.options.autoQuoteFieldNames&&!function(){var r=e.options.nameQuoteCharacter;t=n.ignorePeriodsForFieldNameQuotes?""+r+t+r:t.split(".").map(function(t){return"*"===t?t:""+r+t+r}).join(".")}(),t}},{key:"_formatCustomValue",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?!1:arguments[1],n=f(t,this.options.valueHandlers,y.globalValueHandlers);return n&&(t=n(t,e)),t}},{key:"_formatValueForParamArray",value:function(t){var e=this;return l(t)?t.map(function(t){return e._formatValueForParamArray(t)}):this._formatCustomValue(t,!0)}},{key:"_formatValueForQueryString",value:function(t){var e=this,n=arguments.length<=1||void 0===arguments[1]?{}:arguments[1],r=this._formatCustomValue(t);if(r!==t)return this._applyNestingFormatting(r);if(l(t))t=t.map(function(t){return e._formatValueForQueryString(t)}),t=this._applyNestingFormatting(t.join(", "));else{var i="undefined"==typeof t?"undefined":p(t);if(null===t)t="NULL";else if("boolean"===i)t=t?"TRUE":"FALSE";else if(t instanceof y.BaseBuilder)t=this._applyNestingFormatting(t.toString());else if("number"!==i)if(n.dontQuote)t=""+t;else{var o=this._escapeValue(t);t="'"+o+"'"}}return t}},{key:"_applyNestingFormatting",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?!0:arguments[1];return t&&"string"==typeof t&&e&&("("!==t.charAt(0)||")"!==t.charAt(t.length-1))?"("+t+")":t}},{key:"_buildString",value:function(e,n){var r=arguments.length<=2||void 0===arguments[2]?{}:arguments[2],i=r.nested,o=r.buildParameterized,a=r.formattingOptions;n=n||[],e=e||"";for(var u="",s=-1,c=[],f=this.options.parameterCharacter,h=0;e.length>h;)if(e.substr(h,f.length)===f){var v=n[++s];if(o)if(v instanceof y.BaseBuilder){var d=v._toParamString({buildParameterized:o,nested:!0});u+=d.text,c.push.apply(c,t(d.values))}else if(v=this._formatValueForParamArray(v),l(v)){var p=v.map(function(){return f}).join(", ");u+="("+p+")",c.push.apply(c,t(v))}else u+=f,c.push(v);else u+=this._formatValueForQueryString(v,a);h+=f.length}else u+=e.charAt(h),h++;return{text:this._applyNestingFormatting(u,!!i),values:c}}},{key:"_buildManyStrings",value:function(e,n){var r=arguments.length<=2||void 0===arguments[2]?{}:arguments[2],i=[],o=[];for(var a in e){var l=e[a],u=n[a],s=this._buildString(l,u,{buildParameterized:r.buildParameterized,nested:!1}),c=s.text,f=s.values;i.push(c),o.push.apply(o,t(f))}return i=i.join(this.options.separator),{text:i.length?this._applyNestingFormatting(i,!!r.nested):"",values:o}}},{key:"_toParamString",value:function(t){throw new Error("Not yet implemented")}},{key:"toString",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return this._toParamString(t).text}},{key:"toParam",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return this._toParamString(o({},t,{buildParameterized:!0}))}}]),a}(y.Cloneable),y.Expression=function(i){function o(t){r(this,o);var n=e(this,Object.getPrototypeOf(o).call(this,t));return n._nodes=[],n}return n(o,i),d(o,[{key:"and",value:function(t,e){for(var n=arguments.length,r=Array(n>2?n-2:0),i=2;n>i;i++)r[i-2]=arguments[i];return this._add("AND",t,e,r)}},{key:"or",value:function(t,e){for(var n=arguments.length,r=Array(n>2?n-2:0),i=2;n>i;i++)r[i-2]=arguments[i];return this._add("OR",t,e,r)}},{key:"_add",value:function(t,e,n,r){var i=["=","<",">","<=",">=","<>","!=","in","not in","like","not like","is","is not"],o=void 0;return"string"==typeof e&&"string"==typeof n&&-1!=i.indexOf(n.toLowerCase())?o=this._buildExpression(e,n):(o=e,r.unshift(n)),this._sanitizeExpression(e),this._nodes.push({type:t,expr:o,para:r}),this}},{key:"_buildExpression",value:function(t,e){var n=this._formatFieldName(t),r=this.options.parameterCharacter,i=n+" "+e+" "+r;return i}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n=[],r=[],i=!0,o=!1,a=void 0;try{for(var l,u=this._nodes[Symbol.iterator]();!(i=(l=u.next()).done);i=!0){var s=l.value,c=s.type,f=s.expr,h=s.para,v=f instanceof y.Expression?f._toParamString({buildParameterized:e.buildParameterized,nested:!0}):this._buildString(f,h,{buildParameterized:e.buildParameterized}),d=v.text,p=v.values;n.length&&n.push(c),n.push(d),r.push.apply(r,t(p))}}catch(g){o=!0,a=g}finally{try{!i&&u["return"]&&u["return"]()}finally{if(o)throw a}}return n=n.join(" "),{text:this._applyNestingFormatting(n,!!e.nested),values:r}}}]),o}(y.BaseBuilder),y.Case=function(l){function u(t){var n=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];r(this,u);var i=e(this,Object.getPrototypeOf(u).call(this,n));return a(t)&&(n=t,t=null),t&&(i._fieldName=i._sanitizeField(t)),i.options=o({},y.DefaultQueryBuilderOptions,n),i._cases=[],i._elseValue=null,i}return n(u,l),d(u,[{key:"when",value:function(t){for(var e=arguments.length,n=Array(e>1?e-1:0),r=1;e>r;r++)n[r-1]=arguments[r];return this._cases.unshift({expression:t,values:n}),this}},{key:"then",value:function(t){if(0==this._cases.length)throw new Error("when() needs to be called first");return this._cases[0].result=t,this}},{key:"else",value:function(t){return this._elseValue=t,this}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n="",r=[],o=!0,a=!1,l=void 0;try{for(var u,s=this._cases[Symbol.iterator]();!(o=(u=s.next()).done);o=!0){var c=u.value,f=c.expression,h=c.values,v=c.result;n=i(n," ");var d=this._buildString(f,h,{buildParameterized:e.buildParameterized,nested:!0});n+="WHEN "+d.text+" THEN "+this._formatValueForQueryString(v),r.push.apply(r,t(d.values))}}catch(p){a=!0,l=p}finally{try{!o&&s["return"]&&s["return"]()}finally{if(a)throw l}}return n.length?(n+=" ELSE "+this._formatValueForQueryString(this._elseValue)+" END",this._fieldName&&(n=this._fieldName+" "+n),n="CASE "+n):n=this._formatValueForQueryString(this._elseValue),{text:n,values:r}}}]),u}(y.BaseBuilder),y.Block=function(t){function i(t){return r(this,i),e(this,Object.getPrototypeOf(i).call(this,t))}return n(i,t),d(i,[{key:"exposedMethods",value:function(){for(var t={},e=this;e;)Object.getOwnPropertyNames(e).forEach(function(n){"constructor"===n||"function"!=typeof e[n]||"_"===n.charAt(0)||y.Block.prototype[n]||(t[n]=e[n])}),e=Object.getPrototypeOf(e);return t}}]),i}(y.BaseBuilder),y.StringBlock=function(t){function i(t,n){r(this,i);var o=e(this,Object.getPrototypeOf(i).call(this,t));return o._str=n,o}return n(i,t),d(i,[{key:"_toParamString",value:function(){arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return{text:this._str,values:[]}}}]),i}(y.Block),y.FunctionBlock=function(t){function i(t){r(this,i);var n=e(this,Object.getPrototypeOf(i).call(this,t));return n._strings=[],n._values=[],n}return n(i,t),d(i,[{key:"function",value:function(t){this._strings.push(t);for(var e=arguments.length,n=Array(e>1?e-1:0),r=1;e>r;r++)n[r-1]=arguments[r];this._values.push(n)}},{key:"_toParamString",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return this._buildManyStrings(this._strings,this._values,t)}}]),i}(y.Block),y.registerValueHandler(y.FunctionBlock,function(t){var e=arguments.length<=1||void 0===arguments[1]?!1:arguments[1];return e?t.toParam():t.toString()}),y.AbstractTableBlock=function(o){function a(t,n){r(this,a);var i=e(this,Object.getPrototypeOf(a).call(this,t));return i._tables=[],i}return n(a,o),d(a,[{key:"_table",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1];e=e?this._sanitizeTableAlias(e):e,t=this._sanitizeTable(t),this.options.singleTable&&(this._tables=[]),this._tables.push({table:t,alias:e})}},{key:"_hasTable",value:function(){return 01)throw new Error("Cannot set multiple rows of fields this way.");"undefined"!=typeof e&&(e=this._sanitizeValue(e)),t=this._sanitizeField(t);var r=this._fields.indexOf(t);-1===r&&(this._fields.push(t),r=this._fields.length-1),this._values[0][r]=e,this._valueOptions[0][r]=n}},{key:"_setFields",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];if("object"!==("undefined"==typeof t?"undefined":p(t)))throw new Error("Expected an object but got "+("undefined"==typeof t?"undefined":p(t)));for(var n in t)this._set(n,t[n],e)}},{key:"_setFieldsRows",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];if(!l(t))throw new Error("Expected an array of objects but got "+("undefined"==typeof t?"undefined":p(t)));this._reset();for(var n in t){var r=t[n];for(var i in r){var o=r[i];i=this._sanitizeField(i),o=this._sanitizeValue(o);var a=this._fields.indexOf(i);if(n>0&&-1===a)throw new Error("All fields in subsequent rows must match the fields in the first row");-1===a&&(this._fields.push(i),a=this._fields.length-1),l(this._values[n])||(this._values[n]=[],this._valueOptions[n]=[]),this._values[n][a]=o,this._valueOptions[n][a]=e}}}}]),i}(y.Block),y.SetFieldBlock=function(o){function a(){return r(this,a),e(this,Object.getPrototypeOf(a).apply(this,arguments))}return n(a,o),d(a,[{key:"set",value:function(t,e,n){this._set(t,e,n)}},{key:"setFields",value:function(t,e){this._setFields(t,e)}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n=e.buildParameterized;if(0>=this._fields.length)throw new Error("set() needs to be called");var r="",o=[];for(var a in this._fields){r=i(r,", ");var l=this._fields[a],u=this._values[0][a];if("undefined"==typeof u)r+=l;else{var s=this._buildString(l+" = "+this.options.parameterCharacter,[u],{buildParameterized:n,formattingOptions:this._valueOptions[0][a]});r+=s.text,o.push.apply(o,t(s.values))}}return{text:"SET "+r,values:o}}}]),a}(y.AbstractSetFieldBlock),y.InsertFieldValueBlock=function(o){function a(){return r(this,a),e(this,Object.getPrototypeOf(a).apply(this,arguments))}return n(a,o),d(a,[{key:"set",value:function(t,e){var n=arguments.length<=2||void 0===arguments[2]?{}:arguments[2];this._set(t,e,n)}},{key:"setFields",value:function(t,e){this._setFields(t,e)}},{key:"setFieldsRows",value:function(t,e){this._setFieldsRows(t,e)}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n=e.buildParameterized,r=this._fields.join(", "),o=[],a=[];for(var l in this._values){o[l]="";for(var u in this._values[l]){var s=this._buildString(this.options.parameterCharacter,[this._values[l][u]],{buildParameterized:n,formattingOptions:this._valueOptions[l][u]});a.push.apply(a,t(s.values)),o[l]=i(o[l],", "),o[l]+=s.text}}return{text:r.length?"("+r+") VALUES ("+o.join("), (")+")":"",values:a}}}]),a}(y.AbstractSetFieldBlock),y.InsertFieldsFromQueryBlock=function(t){function i(t){r(this,i);var n=e(this,Object.getPrototypeOf(i).call(this,t));return n._fields=[],n._query=null,n}return n(i,t),d(i,[{key:"fromQuery",value:function(t,e){var n=this;this._fields=t.map(function(t){return n._sanitizeField(t)}),this._query=this._sanitizeQueryBuilder(e)}},{key:"_toParamString",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],e="",n=[];if(this._fields.length&&this._query){var r=this._query._toParamString({buildParameterized:t.buildParameterized,nested:!0}),i=r.text,o=r.values;e="("+this._fields.join(", ")+") ("+i+")",n=o}return{text:e,values:n}}}]),i}(y.Block),y.DistinctBlock=function(t){function i(){return r(this,i),e(this,Object.getPrototypeOf(i).apply(this,arguments))}return n(i,t),d(i,[{key:"distinct",value:function(){this._useDistinct=!0}},{key:"_toParamString",value:function(){return{text:this._useDistinct?"DISTINCT":"",values:[]}}}]),i}(y.Block),y.GroupByBlock=function(t){function i(t){r(this,i);var n=e(this,Object.getPrototypeOf(i).call(this,t));return n._groups=[],n}return n(i,t),d(i,[{key:"group",value:function(t){this._groups.push(this._sanitizeField(t))}},{key:"_toParamString",value:function(){arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return{text:this._groups.length?"GROUP BY "+this._groups.join(", "):"",values:[]}}}]),i}(y.Block),y.OffsetBlock=function(t){function i(t){r(this,i);var n=e(this,Object.getPrototypeOf(i).call(this,t));return n._offsets=null,n}return n(i,t),d(i,[{key:"offset",value:function(t){this._offsets=this._sanitizeLimitOffset(t)}},{key:"_toParamString",value:function(){return{text:this._offsets?"OFFSET "+this._offsets:"",values:[]}}}]),i}(y.Block),y.AbstractConditionBlock=function(o){function a(t){r(this,a);var n=e(this,Object.getPrototypeOf(a).call(this,t));return n._conditions=[],n}return n(a,o),d(a,[{key:"_condition",value:function(t){for(var e=arguments.length,n=Array(e>1?e-1:0),r=1;e>r;r++)n[r-1]=arguments[r];t=this._sanitizeExpression(t),this._conditions.push({expr:t,values:n})}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n="",r=[],o=!0,a=!1,l=void 0;try{for(var u,s=this._conditions[Symbol.iterator]();!(o=(u=s.next()).done);o=!0){var c=u.value,f=c.expr,h=c.values;n=i(n,") AND (");var v=f instanceof y.Expression?f._toParamString({buildParameterized:e.buildParameterized}):this._buildString(f,h,{buildParameterized:e.buildParameterized});n+=v.text,r.push.apply(r,t(v.values))}}catch(d){a=!0,l=d}finally{try{!o&&s["return"]&&s["return"]()}finally{if(a)throw l}}return{text:n.length?this.options.verb+" ("+n+")":"",values:r}}}]),a}(y.Block),y.WhereBlock=function(t){function i(t){return r(this,i),e(this,Object.getPrototypeOf(i).call(this,o({},t,{verb:"WHERE"})))}return n(i,t),d(i,[{key:"where",value:function(t,e){for(var n=arguments.length,r=Array(n>2?n-2:0),i=2;n>i;i++)r[i-2]=arguments[i];var o=["=","<",">","<=",">=","<>","!=","in","not in","like","not like","is","is not"];if("string"==typeof t&&"string"==typeof e&&-1!=o.indexOf(e.toLowerCase())){var a=new y.Expression(this.options);this._condition(a._add("AND",t,e,r))}else this._condition.apply(this,arguments)}}]),i}(y.AbstractConditionBlock),y.HavingBlock=function(t){function i(t){return r(this,i),e(this,Object.getPrototypeOf(i).call(this,o({},t,{verb:"HAVING"})))}return n(i,t),d(i,[{key:"having",value:function(t,e){for(var n=arguments.length,r=Array(n>2?n-2:0),i=2;n>i;i++)r[i-2]=arguments[i];var o=["=","<",">","<=",">=","<>","!=","in","not in","like","not like","is","is not"];if("string"==typeof t&&"string"==typeof e&&-1!=o.indexOf(e.toLowerCase())){var a=new y.Expression(this.options);this._condition(a._add("AND",t,e,r))}else this._condition.apply(this,arguments)}}]),i}(y.AbstractConditionBlock),y.OrderByBlock=function(o){function a(t){r(this,a);var n=e(this,Object.getPrototypeOf(a).call(this,t));return n._orders=[],n}return n(a,o),d(a,[{key:"order",value:function(t,e){for(var n=arguments.length,r=Array(n>2?n-2:0),i=2;n>i;i++)r[i-2]=arguments[i];t=this._sanitizeField(t),e=void 0===e?!0:e,e=null!==e?!!e:e,this._orders.push({field:t,dir:e,values:r})}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n="",r=[],o=!0,a=!1,l=void 0;try{for(var u,s=this._orders[Symbol.iterator]();!(o=(u=s.next()).done);o=!0){var c=u.value,f=c.field,h=c.dir,v=c.values;n=i(n,", ");var d=this._buildString(f,v,{buildParameterized:e.buildParameterized});n+=d.text,r.push.apply(r,t(d.values)),null!==h&&(n+=" "+(h?"ASC":"DESC"))}}catch(p){a=!0,l=p}finally{try{!o&&s["return"]&&s["return"]()}finally{if(a)throw l}}return{text:n.length?"ORDER BY "+n:"",values:r}}}]),a}(y.Block),y.LimitBlock=function(t){function i(t){r(this,i);var n=e(this,Object.getPrototypeOf(i).call(this,t));return n._limit=null,n}return n(i,t),d(i,[{key:"limit",value:function(t){this._limit=this._sanitizeLimitOffset(t)}},{key:"_toParamString",value:function(){return{text:null!==this._limit?"LIMIT "+this._limit:"",values:[]}}}]),i}(y.Block),y.JoinBlock=function(o){function a(t){r(this,a);var n=e(this,Object.getPrototypeOf(a).call(this,t));return n._joins=[],n}return n(a,o),d(a,[{key:"join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],n=arguments.length<=2||void 0===arguments[2]?null:arguments[2],r=arguments.length<=3||void 0===arguments[3]?"INNER":arguments[3];t=this._sanitizeTable(t,!0),e=e?this._sanitizeTableAlias(e):e,n=n?this._sanitizeExpression(n):n,this._joins.push({type:r,table:t,alias:e,condition:n})}},{key:"left_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],n=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,n,"LEFT")}},{key:"right_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],n=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,n,"RIGHT")}},{key:"outer_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],n=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,n,"OUTER")}},{key:"left_outer_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],n=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,n,"LEFT OUTER")}},{key:"full_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],n=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,n,"FULL")}},{key:"cross_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],n=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,n,"CROSS")}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n="",r=[],o=!0,a=!1,l=void 0;try{for(var u,s=this._joins[Symbol.iterator]();!(o=(u=s.next()).done);o=!0){var c=u.value,f=c.type,h=c.table,v=c.alias,d=c.condition;n=i(n,this.options.separator);var p=void 0;if(h instanceof y.BaseBuilder){var g=h._toParamString({buildParameterized:e.buildParameterized,nested:!0});r.push.apply(r,t(g.values)),p=g.text}else p=this._formatTableName(h);if(n+=f+" JOIN "+p,v&&(n+=" "+this._formatTableAlias(v)),d){n+=" ON ";var _=void 0;_=d instanceof y.Expression?d._toParamString({buildParameterized:e.buildParameterized}):this._buildString(d,[],{buildParameterized:e.buildParameterized}),n+=this._applyNestingFormatting(_.text),r.push.apply(r,t(_.values))}}}catch(m){a=!0,l=m}finally{try{!o&&s["return"]&&s["return"]()}finally{if(a)throw l}}return{text:n,values:r}}}]),a}(y.Block),y.UnionBlock=function(o){function a(t){r(this,a);var n=e(this,Object.getPrototypeOf(a).call(this,t));return n._unions=[],n}return n(a,o),d(a,[{key:"union",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?"UNION":arguments[1];t=this._sanitizeTable(t),this._unions.push({type:e,table:t})}},{key:"union_all",value:function(t){this.union(t,"UNION ALL")}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n="",r=[],o=!0,a=!1,l=void 0;try{for(var u,s=this._unions[Symbol.iterator]();!(o=(u=s.next()).done);o=!0){var c=u.value,f=c.type,h=c.table;n=i(n,this.options.separator);var v=void 0;if(h instanceof y.BaseBuilder){var d=h._toParamString({buildParameterized:e.buildParameterized,nested:!0});v=d.text,r.push.apply(r,t(d.values))}else n=this._formatTableName(h);n+=f+" "+v}}catch(p){a=!0,l=p}finally{try{!o&&s["return"]&&s["return"]()}finally{if(a)throw l}}return{text:n,values:r}}}]),a}(y.Block),y.QueryBuilder=function(i){function a(t,n){r(this,a);var i=e(this,Object.getPrototypeOf(a).call(this,t));i.blocks=n||[];var o=!0,l=!1,u=void 0;try{for(var s,c=i.blocks[Symbol.iterator]();!(o=(s=c.next()).done);o=!0){var f=s.value,h=f.exposedMethods();for(var v in h){var d=h[v];if(void 0!==i[v])throw new Error("Builder already has a builder method called: "+v);!function(t,e,n){i[e]=function(){for(var e=arguments.length,r=Array(e),o=0;e>o;o++)r[o]=arguments[o];return n.call.apply(n,[t].concat(r)),i}}(f,v,d)}}}catch(p){l=!0,u=p}finally{try{!o&&c["return"]&&c["return"]()}finally{if(l)throw u}}return i}return n(a,i),d(a,[{key:"registerValueHandler",value:function(t,e){var n=!0,r=!1,i=void 0;try{for(var o,l=this.blocks[Symbol.iterator]();!(n=(o=l.next()).done);n=!0){var u=o.value;u.registerValueHandler(t,e)}}catch(s){r=!0,i=s}finally{try{!n&&l["return"]&&l["return"]()}finally{if(r)throw i}}return v(Object.getPrototypeOf(a.prototype),"registerValueHandler",this).call(this,t,e),this}},{key:"updateOptions",value:function(t){this.options=o({},this.options,t);var e=!0,n=!1,r=void 0;try{for(var i,a=this.blocks[Symbol.iterator]();!(e=(i=a.next()).done);e=!0){var l=i.value;l.options=o({},l.options,t)}}catch(u){n=!0,r=u}finally{try{!e&&a["return"]&&a["return"]()}finally{if(n)throw r}}}},{key:"_toParamString",value:function(){var e,n=this,r=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];r=o({},this.options,r);var i=this.blocks.map(function(t){return t._toParamString({buildParameterized:r.buildParameterized,queryBuilder:n})}),a=i.map(function(t){return t.text}),l=i.map(function(t){return t.values}),u=a.filter(function(t){return 01?e-1:0),r=1;e>r;r++)n[r-1]=arguments[r];if(t&&n){var i=!0,o=!1,a=void 0;try{for(var l,u=function(){var e=l.value;"object"===("undefined"==typeof e?"undefined":p(e))&&Object.getOwnPropertyNames(e).forEach(function(n){"function"!=typeof e[n]&&(t[n]=e[n])})},s=n[Symbol.iterator]();!(i=(l=s.next()).done);i=!0)u()}catch(c){o=!0,a=c}finally{try{!i&&s["return"]&&s["return"]()}finally{if(o)throw a}}}return t}function a(t){return t&&t.constructor.prototype===Object.prototype}function l(t){return t&&t.constructor.prototype===Array.prototype}function u(t){if(t&&t.constructor&&t.constructor.toString){var e=t.constructor.toString().match(/function\s*(\w+)/);if(e&&2===e.length)return e[1]}}function s(t){if(!t)return t;if("function"==typeof t.clone)return t.clone();if(!a(t)&&!l(t))return JSON.parse(JSON.stringify(t));var e=function(){var e=new t.constructor;return Object.getOwnPropertyNames(t).forEach(function(n){"function"!=typeof t[n]&&(e[n]=s(t[n]))}),{v:e}}();return"object"===("undefined"==typeof e?"undefined":p(e))?e.v:void 0}function c(t,e,n){var r="undefined"==typeof e?"undefined":p(e);if("function"!==r&&"string"!==r)throw new Error("type must be a class constructor or string");if("function"!=typeof n)throw new Error("handler must be a function");for(var i in t){var o=t[i];if(o.type===e)return void(o.handler=n)}t.push({type:e,handler:n})}function f(t){for(var e=arguments.length,n=Array(e>1?e-1:0),r=1;e>r;r++)n[r-1]=arguments[r];for(var i in n){var o=n[i];for(var a in o){var l=o[a];if(("undefined"==typeof t?"undefined":p(t))===l.type||"string"!=typeof l.type&&t instanceof l.type)return l.handler}}}function h(){var h=arguments.length<=0||void 0===arguments[0]?null:arguments[0],y={_getObjectClassName:u};y.DefaultQueryBuilderOptions={autoQuoteTableNames:!1,autoQuoteFieldNames:!1,autoQuoteAliasNames:!0,useAsForTableAliasNames:!1,nameQuoteCharacter:"`",tableAliasQuoteCharacter:"`",fieldAliasQuoteCharacter:'"',valueHandlers:[],parameterCharacter:"?",numberedParameters:!1,numberedParametersPrefix:"$",numberedParametersStartAt:1,replaceSingleQuotes:!1,singleQuoteReplacement:"''",separator:" "},y.globalValueHandlers=[],y.registerValueHandler=function(t,e){c(y.globalValueHandlers,t,e)},y.Cloneable=function(){function t(){r(this,t)}return d(t,[{key:"clone",value:function(){var t=new this.constructor;return o(t,s(o({},this)))}}]),t}(),y.BaseBuilder=function(i){function a(t){r(this,a);var n=e(this,Object.getPrototypeOf(a).call(this)),i=JSON.parse(JSON.stringify(y.DefaultQueryBuilderOptions));return n.options=o({},i,t),n}return n(a,i),d(a,[{key:"registerValueHandler",value:function(t,e){return c(this.options.valueHandlers,t,e),this}},{key:"_sanitizeExpression",value:function(t){if(!(t instanceof y.Expression)&&"string"!=typeof t)throw new Error("expression must be a string or Expression instance");return t}},{key:"_sanitizeName",value:function(t,e){if("string"!=typeof t)throw new Error(e+" must be a string");return t}},{key:"_sanitizeField",value:function(t){return t instanceof y.BaseBuilder||(t=this._sanitizeName(t,"field name")),t}},{key:"_sanitizeQueryBuilder",value:function(t){if(t instanceof y.QueryBuilder)return t;throw new Error("must be a QueryBuilder instance")}},{key:"_sanitizeTable",value:function(t){if("string"!=typeof t)try{t=this._sanitizeQueryBuilder(t)}catch(e){throw new Error("table name must be a string or a query builder")}else t=this._sanitizeName(t,"table");return t}},{key:"_sanitizeTableAlias",value:function(t){return this._sanitizeName(t,"table alias")}},{key:"_sanitizeFieldAlias",value:function(t){return this._sanitizeName(t,"field alias")}},{key:"_sanitizeLimitOffset",value:function(t){if(t=parseInt(t),0>t||isNaN(t))throw new Error("limit/offset must be >= 0");return t}},{key:"_sanitizeValue",value:function(t){var e="undefined"==typeof t?"undefined":p(t);if(null===t);else if("string"===e||"number"===e||"boolean"===e);else if(t instanceof y.BaseBuilder);else{var n=!!f(t,this.options.valueHandlers,y.globalValueHandlers);if(!n)throw new Error("field value must be a string, number, boolean, null or one of the registered custom value types")}return t}},{key:"_escapeValue",value:function(t){return this.options.replaceSingleQuotes?t.replace(/\'/g,this.options.singleQuoteReplacement):t}},{key:"_formatTableName",value:function(t){if(this.options.autoQuoteTableNames){var e=this.options.nameQuoteCharacter;t=""+e+t+e}return t}},{key:"_formatFieldAlias",value:function(t){if(this.options.autoQuoteAliasNames){var e=this.options.fieldAliasQuoteCharacter;t=""+e+t+e}return t}},{key:"_formatTableAlias",value:function(t){if(this.options.autoQuoteAliasNames){var e=this.options.tableAliasQuoteCharacter;t=""+e+t+e}return this.options.useAsForTableAliasNames?"AS "+t:t}},{key:"_formatFieldName",value:function(t){var e=this,n=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];return this.options.autoQuoteFieldNames&&!function(){var r=e.options.nameQuoteCharacter;t=n.ignorePeriodsForFieldNameQuotes?""+r+t+r:t.split(".").map(function(t){return"*"===t?t:""+r+t+r}).join(".")}(),t}},{key:"_formatCustomValue",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?!1:arguments[1],n=f(t,this.options.valueHandlers,y.globalValueHandlers);return n&&(t=n(t,e)),t}},{key:"_formatValueForParamArray",value:function(t){var e=this;return l(t)?t.map(function(t){return e._formatValueForParamArray(t)}):this._formatCustomValue(t,!0)}},{key:"_formatValueForQueryString",value:function(t){var e=this,n=arguments.length<=1||void 0===arguments[1]?{}:arguments[1],r=this._formatCustomValue(t);if(r!==t)return this._applyNestingFormatting(r);if(l(t))t=t.map(function(t){return e._formatValueForQueryString(t)}),t=this._applyNestingFormatting(t.join(", "));else{var i="undefined"==typeof t?"undefined":p(t);if(null===t)t="NULL";else if("boolean"===i)t=t?"TRUE":"FALSE";else if(t instanceof y.BaseBuilder)t=this._applyNestingFormatting(t.toString());else if("number"!==i)if(n.dontQuote)t=""+t;else{var o=this._escapeValue(t);t="'"+o+"'"}}return t}},{key:"_applyNestingFormatting",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?!0:arguments[1];return t&&"string"==typeof t&&e&&("("!==t.charAt(0)||")"!==t.charAt(t.length-1))?"("+t+")":t}},{key:"_buildString",value:function(e,n){var r=arguments.length<=2||void 0===arguments[2]?{}:arguments[2],i=r.nested,o=r.buildParameterized,a=r.formattingOptions;n=n||[],e=e||"";for(var u="",s=-1,c=[],f=this.options.parameterCharacter,h=0;e.length>h;)if(e.substr(h,f.length)===f){var v=n[++s];if(o)if(v instanceof y.BaseBuilder){var d=v._toParamString({buildParameterized:o,nested:!0});u+=d.text,c.push.apply(c,t(d.values))}else if(v=this._formatValueForParamArray(v),l(v)){var p=v.map(function(){return f}).join(", ");u+="("+p+")",c.push.apply(c,t(v))}else u+=f,c.push(v);else u+=this._formatValueForQueryString(v,a);h+=f.length}else u+=e.charAt(h),h++;return{text:this._applyNestingFormatting(u,!!i),values:c}}},{key:"_buildManyStrings",value:function(e,n){var r=arguments.length<=2||void 0===arguments[2]?{}:arguments[2],i=[],o=[];for(var a in e){var l=e[a],u=n[a],s=this._buildString(l,u,{buildParameterized:r.buildParameterized,nested:!1}),c=s.text,f=s.values;i.push(c),o.push.apply(o,t(f))}return i=i.join(this.options.separator),{text:i.length?this._applyNestingFormatting(i,!!r.nested):"",values:o}}},{key:"_toParamString",value:function(t){throw new Error("Not yet implemented")}},{key:"toString",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return this._toParamString(t).text}},{key:"toParam",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return this._toParamString(o({},t,{buildParameterized:!0}))}}]),a}(y.Cloneable),y.Expression=function(i){function o(t){r(this,o);var n=e(this,Object.getPrototypeOf(o).call(this,t));return n._nodes=[],n}return n(o,i),d(o,[{key:"and",value:function(t,e){for(var n=arguments.length,r=Array(n>2?n-2:0),i=2;n>i;i++)r[i-2]=arguments[i];return this._add("AND",t,e,r)}},{key:"or",value:function(t,e){for(var n=arguments.length,r=Array(n>2?n-2:0),i=2;n>i;i++)r[i-2]=arguments[i];return this._add("OR",t,e,r)}},{key:"_add",value:function(t,e,n,r){var i=["=","<",">","<=",">=","<>","!=","in","not in","like","not like","is","is not"],o=void 0;return"string"==typeof e&&"string"==typeof n&&-1!=i.indexOf(n.toLowerCase())?o=this._buildExpression(e,n):(o=e,r.unshift(n)),o=this._sanitizeExpression(o),this._nodes.push({type:t,expr:o,para:r}),this}},{key:"_buildExpression",value:function(t,e){var n=this._formatFieldName(t),r=this.options.parameterCharacter,i=n+" "+e+" "+r;return i}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n=[],r=[],i=!0,o=!1,a=void 0;try{for(var l,u=this._nodes[Symbol.iterator]();!(i=(l=u.next()).done);i=!0){var s=l.value,c=s.type,f=s.expr,h=s.para,v=f instanceof y.Expression?f._toParamString({buildParameterized:e.buildParameterized,nested:!0}):this._buildString(f,h,{buildParameterized:e.buildParameterized}),d=v.text,p=v.values;n.length&&n.push(c),n.push(d),r.push.apply(r,t(p))}}catch(g){o=!0,a=g}finally{try{!i&&u["return"]&&u["return"]()}finally{if(o)throw a}}return n=n.join(" "),{text:this._applyNestingFormatting(n,!!e.nested),values:r}}}]),o}(y.BaseBuilder),y.Case=function(l){function u(t){var n=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];r(this,u);var i=e(this,Object.getPrototypeOf(u).call(this,n));return a(t)&&(n=t,t=null),t&&(i._fieldName=i._sanitizeField(t)),i.options=o({},y.DefaultQueryBuilderOptions,n),i._cases=[],i._elseValue=null,i}return n(u,l),d(u,[{key:"when",value:function(t){for(var e=arguments.length,n=Array(e>1?e-1:0),r=1;e>r;r++)n[r-1]=arguments[r];return this._cases.unshift({expression:t,values:n}),this}},{key:"then",value:function(t){if(0==this._cases.length)throw new Error("when() needs to be called first");return this._cases[0].result=t,this}},{key:"else",value:function(t){return this._elseValue=t,this}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n="",r=[],o=!0,a=!1,l=void 0;try{for(var u,s=this._cases[Symbol.iterator]();!(o=(u=s.next()).done);o=!0){var c=u.value,f=c.expression,h=c.values,v=c.result;n=i(n," ");var d=this._buildString(f,h,{buildParameterized:e.buildParameterized,nested:!0});n+="WHEN "+d.text+" THEN "+this._formatValueForQueryString(v),r.push.apply(r,t(d.values))}}catch(p){a=!0,l=p}finally{try{!o&&s["return"]&&s["return"]()}finally{if(a)throw l}}return n.length?(n+=" ELSE "+this._formatValueForQueryString(this._elseValue)+" END",this._fieldName&&(n=this._fieldName+" "+n),n="CASE "+n):n=this._formatValueForQueryString(this._elseValue),{text:n,values:r}}}]),u}(y.BaseBuilder),y.Block=function(t){function i(t){return r(this,i),e(this,Object.getPrototypeOf(i).call(this,t))}return n(i,t),d(i,[{key:"exposedMethods",value:function(){for(var t={},e=this;e;)Object.getOwnPropertyNames(e).forEach(function(n){"constructor"===n||"function"!=typeof e[n]||"_"===n.charAt(0)||y.Block.prototype[n]||(t[n]=e[n])}),e=Object.getPrototypeOf(e);return t}}]),i}(y.BaseBuilder),y.StringBlock=function(t){function i(t,n){r(this,i);var o=e(this,Object.getPrototypeOf(i).call(this,t));return o._str=n,o}return n(i,t),d(i,[{key:"_toParamString",value:function(){arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return{text:this._str,values:[]}}}]),i}(y.Block),y.FunctionBlock=function(t){function i(t){r(this,i);var n=e(this,Object.getPrototypeOf(i).call(this,t));return n._strings=[],n._values=[],n}return n(i,t),d(i,[{key:"function",value:function(t){this._strings.push(t);for(var e=arguments.length,n=Array(e>1?e-1:0),r=1;e>r;r++)n[r-1]=arguments[r];this._values.push(n)}},{key:"_toParamString",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return this._buildManyStrings(this._strings,this._values,t)}}]),i}(y.Block),y.registerValueHandler(y.FunctionBlock,function(t){var e=arguments.length<=1||void 0===arguments[1]?!1:arguments[1];return e?t.toParam():t.toString()}),y.AbstractTableBlock=function(o){function a(t,n){r(this,a);var i=e(this,Object.getPrototypeOf(a).call(this,t));return i._tables=[],i}return n(a,o),d(a,[{key:"_table",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1];e=e?this._sanitizeTableAlias(e):e,t=this._sanitizeTable(t),this.options.singleTable&&(this._tables=[]),this._tables.push({table:t,alias:e})}},{key:"_hasTable",value:function(){return 01)throw new Error("Cannot set multiple rows of fields this way.");"undefined"!=typeof e&&(e=this._sanitizeValue(e)),t=this._sanitizeField(t);var r=this._fields.indexOf(t);-1===r&&(this._fields.push(t),r=this._fields.length-1),this._values[0][r]=e,this._valueOptions[0][r]=n}},{key:"_setFields",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];if("object"!==("undefined"==typeof t?"undefined":p(t)))throw new Error("Expected an object but got "+("undefined"==typeof t?"undefined":p(t)));for(var n in t)this._set(n,t[n],e)}},{key:"_setFieldsRows",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];if(!l(t))throw new Error("Expected an array of objects but got "+("undefined"==typeof t?"undefined":p(t)));this._reset();for(var n in t){var r=t[n];for(var i in r){var o=r[i];i=this._sanitizeField(i),o=this._sanitizeValue(o);var a=this._fields.indexOf(i);if(n>0&&-1===a)throw new Error("All fields in subsequent rows must match the fields in the first row");-1===a&&(this._fields.push(i),a=this._fields.length-1),l(this._values[n])||(this._values[n]=[],this._valueOptions[n]=[]),this._values[n][a]=o,this._valueOptions[n][a]=e}}}}]),i}(y.Block),y.SetFieldBlock=function(o){function a(){return r(this,a),e(this,Object.getPrototypeOf(a).apply(this,arguments))}return n(a,o),d(a,[{key:"set",value:function(t,e,n){this._set(t,e,n)}},{key:"setFields",value:function(t,e){this._setFields(t,e)}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n=e.buildParameterized;if(0>=this._fields.length)throw new Error("set() needs to be called");var r="",o=[];for(var a in this._fields){r=i(r,", ");var l=this._fields[a],u=this._values[0][a];if("undefined"==typeof u)r+=l;else{var s=this._buildString(l+" = "+this.options.parameterCharacter,[u],{buildParameterized:n,formattingOptions:this._valueOptions[0][a]});r+=s.text,o.push.apply(o,t(s.values))}}return{text:"SET "+r,values:o}}}]),a}(y.AbstractSetFieldBlock),y.InsertFieldValueBlock=function(o){function a(){return r(this,a),e(this,Object.getPrototypeOf(a).apply(this,arguments))}return n(a,o),d(a,[{key:"set",value:function(t,e){var n=arguments.length<=2||void 0===arguments[2]?{}:arguments[2];this._set(t,e,n)}},{key:"setFields",value:function(t,e){this._setFields(t,e)}},{key:"setFieldsRows",value:function(t,e){this._setFieldsRows(t,e)}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n=e.buildParameterized,r=this._fields.join(", "),o=[],a=[];for(var l in this._values){o[l]="";for(var u in this._values[l]){var s=this._buildString(this.options.parameterCharacter,[this._values[l][u]],{buildParameterized:n,formattingOptions:this._valueOptions[l][u]});a.push.apply(a,t(s.values)),o[l]=i(o[l],", "),o[l]+=s.text}}return{text:r.length?"("+r+") VALUES ("+o.join("), (")+")":"",values:a}}}]),a}(y.AbstractSetFieldBlock),y.InsertFieldsFromQueryBlock=function(t){function i(t){r(this,i);var n=e(this,Object.getPrototypeOf(i).call(this,t));return n._fields=[],n._query=null,n}return n(i,t),d(i,[{key:"fromQuery",value:function(t,e){var n=this;this._fields=t.map(function(t){return n._sanitizeField(t)}),this._query=this._sanitizeQueryBuilder(e)}},{key:"_toParamString",value:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],e="",n=[];if(this._fields.length&&this._query){var r=this._query._toParamString({buildParameterized:t.buildParameterized,nested:!0}),i=r.text,o=r.values;e="("+this._fields.join(", ")+") ("+i+")",n=o}return{text:e,values:n}}}]),i}(y.Block),y.DistinctBlock=function(t){function i(){return r(this,i),e(this,Object.getPrototypeOf(i).apply(this,arguments))}return n(i,t),d(i,[{key:"distinct",value:function(){this._useDistinct=!0}},{key:"_toParamString",value:function(){return{text:this._useDistinct?"DISTINCT":"",values:[]}}}]),i}(y.Block),y.GroupByBlock=function(t){function i(t){r(this,i);var n=e(this,Object.getPrototypeOf(i).call(this,t));return n._groups=[],n}return n(i,t),d(i,[{key:"group",value:function(t){this._groups.push(this._sanitizeField(t))}},{key:"_toParamString",value:function(){arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return{text:this._groups.length?"GROUP BY "+this._groups.join(", "):"",values:[]}}}]),i}(y.Block),y.OffsetBlock=function(t){function i(t){r(this,i);var n=e(this,Object.getPrototypeOf(i).call(this,t));return n._offsets=null,n}return n(i,t),d(i,[{key:"offset",value:function(t){this._offsets=this._sanitizeLimitOffset(t)}},{key:"_toParamString",value:function(){return{text:this._offsets?"OFFSET "+this._offsets:"",values:[]}}}]),i}(y.Block),y.AbstractConditionBlock=function(o){function a(t){r(this,a);var n=e(this,Object.getPrototypeOf(a).call(this,t));return n._conditions=[],n}return n(a,o),d(a,[{key:"_condition",value:function(t){for(var e=arguments.length,n=Array(e>1?e-1:0),r=1;e>r;r++)n[r-1]=arguments[r];t=this._sanitizeExpression(t),this._conditions.push({expr:t,values:n})}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n="",r=[],o=!0,a=!1,l=void 0;try{for(var u,s=this._conditions[Symbol.iterator]();!(o=(u=s.next()).done);o=!0){var c=u.value,f=c.expr,h=c.values;n=i(n,") AND (");var v=f instanceof y.Expression?f._toParamString({buildParameterized:e.buildParameterized}):this._buildString(f,h,{buildParameterized:e.buildParameterized});n+=v.text,r.push.apply(r,t(v.values))}}catch(d){a=!0,l=d}finally{try{!o&&s["return"]&&s["return"]()}finally{if(a)throw l}}return{text:n.length?this.options.verb+" ("+n+")":"",values:r}}}]),a}(y.Block),y.WhereBlock=function(t){function i(t){return r(this,i),e(this,Object.getPrototypeOf(i).call(this,o({},t,{verb:"WHERE"})))}return n(i,t),d(i,[{key:"where",value:function(t,e){for(var n=arguments.length,r=Array(n>2?n-2:0),i=2;n>i;i++)r[i-2]=arguments[i];var o=["=","<",">","<=",">=","<>","!=","in","not in","like","not like","is","is not"];if("string"==typeof t&&"string"==typeof e&&-1!=o.indexOf(e.toLowerCase())){var a=new y.Expression(this.options);this._condition(a._add("AND",t,e,r))}else this._condition.apply(this,arguments)}}]),i}(y.AbstractConditionBlock),y.HavingBlock=function(t){function i(t){return r(this,i),e(this,Object.getPrototypeOf(i).call(this,o({},t,{verb:"HAVING"})))}return n(i,t),d(i,[{key:"having",value:function(t,e){for(var n=arguments.length,r=Array(n>2?n-2:0),i=2;n>i;i++)r[i-2]=arguments[i];var o=["=","<",">","<=",">=","<>","!=","in","not in","like","not like","is","is not"];if("string"==typeof t&&"string"==typeof e&&-1!=o.indexOf(e.toLowerCase())){var a=new y.Expression(this.options);this._condition(a._add("AND",t,e,r))}else this._condition.apply(this,arguments)}}]),i}(y.AbstractConditionBlock),y.OrderByBlock=function(o){function a(t){r(this,a);var n=e(this,Object.getPrototypeOf(a).call(this,t));return n._orders=[],n}return n(a,o),d(a,[{key:"order",value:function(t,e){for(var n=arguments.length,r=Array(n>2?n-2:0),i=2;n>i;i++)r[i-2]=arguments[i];t=this._sanitizeField(t),e=void 0===e?!0:e,e=null!==e?!!e:e,this._orders.push({field:t,dir:e,values:r})}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n="",r=[],o=!0,a=!1,l=void 0;try{for(var u,s=this._orders[Symbol.iterator]();!(o=(u=s.next()).done);o=!0){var c=u.value,f=c.field,h=c.dir,v=c.values;n=i(n,", ");var d=this._buildString(f,v,{buildParameterized:e.buildParameterized});n+=d.text,r.push.apply(r,t(d.values)),null!==h&&(n+=" "+(h?"ASC":"DESC"))}}catch(p){a=!0,l=p}finally{try{!o&&s["return"]&&s["return"]()}finally{if(a)throw l}}return{text:n.length?"ORDER BY "+n:"",values:r}}}]),a}(y.Block),y.LimitBlock=function(t){function i(t){r(this,i);var n=e(this,Object.getPrototypeOf(i).call(this,t));return n._limit=null,n}return n(i,t),d(i,[{key:"limit",value:function(t){this._limit=this._sanitizeLimitOffset(t)}},{key:"_toParamString",value:function(){return{text:null!==this._limit?"LIMIT "+this._limit:"",values:[]}}}]),i}(y.Block),y.JoinBlock=function(o){function a(t){r(this,a);var n=e(this,Object.getPrototypeOf(a).call(this,t));return n._joins=[],n}return n(a,o),d(a,[{key:"join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],n=arguments.length<=2||void 0===arguments[2]?null:arguments[2],r=arguments.length<=3||void 0===arguments[3]?"INNER":arguments[3];t=this._sanitizeTable(t,!0),e=e?this._sanitizeTableAlias(e):e,n=n?this._sanitizeExpression(n):n,this._joins.push({type:r,table:t,alias:e,condition:n})}},{key:"left_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],n=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,n,"LEFT")}},{key:"right_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],n=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,n,"RIGHT")}},{key:"outer_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],n=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,n,"OUTER")}},{key:"left_outer_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],n=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,n,"LEFT OUTER")}},{key:"full_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],n=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,n,"FULL")}},{key:"cross_join",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?null:arguments[1],n=arguments.length<=2||void 0===arguments[2]?null:arguments[2];this.join(t,e,n,"CROSS")}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n="",r=[],o=!0,a=!1,l=void 0;try{for(var u,s=this._joins[Symbol.iterator]();!(o=(u=s.next()).done);o=!0){var c=u.value,f=c.type,h=c.table,v=c.alias,d=c.condition;n=i(n,this.options.separator);var p=void 0;if(h instanceof y.BaseBuilder){var g=h._toParamString({buildParameterized:e.buildParameterized,nested:!0});r.push.apply(r,t(g.values)),p=g.text}else p=this._formatTableName(h);if(n+=f+" JOIN "+p,v&&(n+=" "+this._formatTableAlias(v)),d){n+=" ON ";var _=void 0;_=d instanceof y.Expression?d._toParamString({buildParameterized:e.buildParameterized}):this._buildString(d,[],{buildParameterized:e.buildParameterized}),n+=this._applyNestingFormatting(_.text),r.push.apply(r,t(_.values))}}}catch(m){a=!0,l=m}finally{try{!o&&s["return"]&&s["return"]()}finally{if(a)throw l}}return{text:n,values:r}}}]),a}(y.Block),y.UnionBlock=function(o){function a(t){r(this,a);var n=e(this,Object.getPrototypeOf(a).call(this,t));return n._unions=[],n}return n(a,o),d(a,[{key:"union",value:function(t){var e=arguments.length<=1||void 0===arguments[1]?"UNION":arguments[1];t=this._sanitizeTable(t),this._unions.push({type:e,table:t})}},{key:"union_all",value:function(t){this.union(t,"UNION ALL")}},{key:"_toParamString",value:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n="",r=[],o=!0,a=!1,l=void 0;try{for(var u,s=this._unions[Symbol.iterator]();!(o=(u=s.next()).done);o=!0){var c=u.value,f=c.type,h=c.table;n=i(n,this.options.separator);var v=void 0;if(h instanceof y.BaseBuilder){var d=h._toParamString({buildParameterized:e.buildParameterized,nested:!0});v=d.text,r.push.apply(r,t(d.values))}else n=this._formatTableName(h);n+=f+" "+v}}catch(p){a=!0,l=p}finally{try{!o&&s["return"]&&s["return"]()}finally{if(a)throw l}}return{text:n,values:r}}}]),a}(y.Block),y.QueryBuilder=function(i){function a(t,n){r(this,a);var i=e(this,Object.getPrototypeOf(a).call(this,t));i.blocks=n||[];var o=!0,l=!1,u=void 0;try{for(var s,c=i.blocks[Symbol.iterator]();!(o=(s=c.next()).done);o=!0){var f=s.value,h=f.exposedMethods();for(var v in h){var d=h[v];if(void 0!==i[v])throw new Error("Builder already has a builder method called: "+v);!function(t,e,n){i[e]=function(){for(var e=arguments.length,r=Array(e),o=0;e>o;o++)r[o]=arguments[o];return n.call.apply(n,[t].concat(r)),i}}(f,v,d)}}}catch(p){l=!0,u=p}finally{try{!o&&c["return"]&&c["return"]()}finally{if(l)throw u}}return i}return n(a,i),d(a,[{key:"registerValueHandler",value:function(t,e){var n=!0,r=!1,i=void 0;try{for(var o,l=this.blocks[Symbol.iterator]();!(n=(o=l.next()).done);n=!0){var u=o.value;u.registerValueHandler(t,e)}}catch(s){r=!0,i=s}finally{try{!n&&l["return"]&&l["return"]()}finally{if(r)throw i}}return v(Object.getPrototypeOf(a.prototype),"registerValueHandler",this).call(this,t,e),this}},{key:"updateOptions",value:function(t){this.options=o({},this.options,t);var e=!0,n=!1,r=void 0;try{for(var i,a=this.blocks[Symbol.iterator]();!(e=(i=a.next()).done);e=!0){var l=i.value;l.options=o({},l.options,t)}}catch(u){n=!0,r=u}finally{try{!e&&a["return"]&&a["return"]()}finally{if(n)throw r}}}},{key:"_toParamString",value:function(){var e,n=this,r=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];r=o({},this.options,r);var i=this.blocks.map(function(t){return t._toParamString({buildParameterized:r.buildParameterized,queryBuilder:n})}),a=i.map(function(t){return t.text}),l=i.map(function(t){return t.values}),u=a.filter(function(t){return 0=this._fields.length)throw new Error("set() needs to be called");var e=v(Object.getPrototypeOf(i.prototype),"_toParamString",this).call(this,t);if(e.text.length&&0