From 2308f21843b205dceafd551c6c7e131a594fc27c Mon Sep 17 00:00:00 2001 From: Giorgio Aresu Date: Tue, 26 Jul 2016 23:03:08 +0200 Subject: [PATCH 1/2] Bring WITH clause support to mssql flavor --- src/mssql.js | 40 ++++++++++++++++++++++++++++++++++++++-- test/mssql.test.coffee | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 2 deletions(-) diff --git a/src/mssql.js b/src/mssql.js index 3c1e3af0..90803907 100644 --- a/src/mssql.js +++ b/src/mssql.js @@ -11,7 +11,7 @@ squel.flavours['mssql'] = function(_squel) { }); - // LIMIT, OFFSET x and TOP x + //ļæ½LIMIT, OFFSET x and TOP x cls.MssqlLimitOffsetTopBlock = class extends cls.Block { constructor (options) { super(options); @@ -87,7 +87,7 @@ squel.flavours['mssql'] = function(_squel) { let str = ""; if (this._parent._offsets) { - str = `OFFSET ${this._parent._offsets} ROWS`; + str = `OFFSET ${this._parent._offsets} ROWS`; } return { @@ -235,6 +235,7 @@ squel.flavours['mssql'] = function(_squel) { let limitOffsetTopBlock = new cls.MssqlLimitOffsetTopBlock(options); blocks = blocks || [ + new cls.WithBlock(options), new cls.StringBlock(options, 'SELECT'), new cls.DistinctBlock(options), limitOffsetTopBlock.TOP(), @@ -261,6 +262,7 @@ squel.flavours['mssql'] = function(_squel) { cls.Update = class extends cls.QueryBuilder { constructor (options, blocks = null) { blocks = blocks || [ + new cls.WithBlock(options), new cls.StringBlock(options, 'UPDATE'), new cls.MssqlUpdateTopBlock(options), new cls.UpdateTableBlock(options), @@ -281,6 +283,7 @@ squel.flavours['mssql'] = function(_squel) { cls.Delete = class extends cls.QueryBuilder { constructor (options, blocks = null) { blocks = blocks || [ + new cls.WithBlock(options), new cls.StringBlock(options, 'DELETE'), new cls.TargetTableBlock(options), new cls.FromTableBlock( _extend({}, options, { singleTable: true }) ), @@ -300,6 +303,7 @@ squel.flavours['mssql'] = function(_squel) { cls.Insert = class extends cls.QueryBuilder { constructor (options, blocks = null) { blocks = blocks || [ + new cls.WithBlock(options), new cls.StringBlock(options, 'INSERT'), new cls.IntoTableBlock(options), new cls.MssqlInsertFieldValueBlock(options), @@ -310,6 +314,38 @@ squel.flavours['mssql'] = function(_squel) { } } + // WITH + cls.WithBlock = class extends cls.Block { + constructor (options) { + super(options); + this._tables = []; + } + + with (alias, table) { + this._tables.push({alias, table}); + } + + _toParamString(options = {}) { + var parts = []; + var values = []; + + for (let {alias, table} of this._tables) { + let ret = table._toParamString({ + buildParameterized: options.buildParameterized, + nested: true + }); + + parts.push(`${alias} AS ${ret.text}`); + values.push(...ret.values); + } + + return { + text: parts.length ? `WITH ${parts.join(', ')}` : '', + values + }; + } + } + diff --git a/test/mssql.test.coffee b/test/mssql.test.coffee index c0e23987..098ccf84 100644 --- a/test/mssql.test.coffee +++ b/test/mssql.test.coffee @@ -138,6 +138,39 @@ test['MSSQL flavour'] = toString: -> assert.same @upt.toString(), 'DELETE FROM table OUTPUT DELETED.id AS ident, DELETED.name AS naming WHERE (a = 1)' + 'cte queries': + beforeEach: -> + @sel = squel.select() + @sel2 = squel.select() + @sel3 = squel.select() + + '>> query1.with(alias, query2)': + beforeEach: -> + @sel.from('table1').where('field1 = ?', 1) + @sel2.from('table2').where('field2 = ?', 2) + @sel.with('someAlias', @sel2) + toString: -> + assert.same @sel.toString(), 'WITH someAlias AS (SELECT * FROM table2 WHERE (field2 = 2)) SELECT * FROM table1 WHERE (field1 = 1)' + toParam: -> + assert.same @sel.toParam(), { + "text": 'WITH someAlias AS (SELECT * FROM table2 WHERE (field2 = ?)) SELECT * FROM table1 WHERE (field1 = ?)' + "values": [2, 1] + } + + '>> query1.with(alias1, query2).with(alias2, query2)': + beforeEach: -> + @sel.from('table1').where('field1 = ?', 1) + @sel2.from('table2').where('field2 = ?', 2) + @sel3.from('table3').where('field3 = ?', 3) + @sel.with('someAlias', @sel2).with('anotherAlias', @sel3) + toString: -> + assert.same @sel.toString(), 'WITH someAlias AS (SELECT * FROM table2 WHERE (field2 = 2)), anotherAlias AS (SELECT * FROM table3 WHERE (field3 = 3)) SELECT * FROM table1 WHERE (field1 = 1)' + toParam: -> + assert.same @sel.toParam(), { + "text": 'WITH someAlias AS (SELECT * FROM table2 WHERE (field2 = ?)), anotherAlias AS (SELECT * FROM table3 WHERE (field3 = ?)) SELECT * FROM table1 WHERE (field1 = ?)' + "values": [2, 3, 1] + } + 'Default query builder options': -> assert.same { autoQuoteTableNames: false From 3d8fa45138ab9e2a8b41f4804cb53f839e249562 Mon Sep 17 00:00:00 2001 From: Giorgio Aresu Date: Tue, 26 Jul 2016 23:03:22 +0200 Subject: [PATCH 2/2] Add dist files --- dist/squel-basic.js | 23 ++-- dist/squel-basic.min.js | 2 +- dist/squel.js | 275 ++++++++++++++++++++++++++-------------- dist/squel.min.js | 4 +- 4 files changed, 192 insertions(+), 112 deletions(-) diff --git a/dist/squel-basic.js b/dist/squel-basic.js index 1e6daf89..e6c3a47c 100644 --- a/dist/squel-basic.js +++ b/dist/squel-basic.js @@ -332,7 +332,6 @@ function _buildSquel() { * Constructor. * this.param {Object} options Overriding one or more of `cls.DefaultQueryBuilderOptions`. */ - function _class2(options) { _classCallCheck(this, _class2); @@ -460,16 +459,16 @@ function _buildSquel() { if (null === item) { // null is allowed } else if ("string" === itemType || "number" === itemType || "boolean" === itemType) { - // primitives are allowed - } else if (item instanceof cls.BaseBuilder) { - // Builders allowed - } else { - var typeIsValid = !!getValueHandler(item, this.options.valueHandlers, cls.globalValueHandlers); + // primitives are allowed + } else if (item instanceof cls.BaseBuilder) { + // Builders allowed + } else { + var typeIsValid = !!getValueHandler(item, this.options.valueHandlers, cls.globalValueHandlers); - if (!typeIsValid) { - throw new Error("field value must be a string, number, boolean, null or one of the registered custom value types"); - } - } + if (!typeIsValid) { + throw new Error("field value must be a string, number, boolean, null or one of the registered custom value types"); + } + } return item; } @@ -842,7 +841,6 @@ function _buildSquel() { _inherits(_class3, _cls$BaseBuilder); // Initialise the expression. - function _class3(options) { _classCallCheck(this, _class3); @@ -1240,7 +1238,6 @@ function _buildSquel() { * @param {Boolean} [options.singleTable] If true then only allow one table spec. * @param {String} [options.prefix] String prefix for output. */ - function _class8(options, prefix) { _classCallCheck(this, _class8); @@ -2073,7 +2070,6 @@ function _buildSquel() { /** * @param {String} options.verb The condition verb. */ - function _class21(options) { _classCallCheck(this, _class21); @@ -2655,7 +2651,6 @@ function _buildSquel() { # # blocks - array of cls.BaseBuilderBlock instances to build the query with. */ - function _class28(options, blocks) { _classCallCheck(this, _class28); diff --git a/dist/squel-basic.min.js b/dist/squel-basic.min.js index eb88167c..b69cda1a 100644 --- a/dist/squel-basic.min.js +++ b/dist/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){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");var i=!0,a=!1,o=void 0;try{for(var l,u=t[Symbol.iterator]();!(i=(l=u.next()).done);i=!0){var s=l.value;if(s.type===e)return void(s.handler=r)}}catch(f){a=!0,o=f}finally{try{!i&&u["return"]&&u["return"]()}finally{if(a)throw o}}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];var i=!0,a=!1,o=void 0;try{for(var l,u=r[Symbol.iterator]();!(i=(l=u.next()).done);i=!0){var s=l.value,f=!0,c=!1,h=void 0;try{for(var v,d=s[Symbol.iterator]();!(f=(v=d.next()).done);f=!0){var p=v.value;if(("undefined"==typeof t?"undefined":y(t))===p.type||"string"!=typeof p.type&&t instanceof p.type)return p.handler}}catch(g){c=!0,h=g}finally{try{!f&&d["return"]&&d["return"]()}finally{if(c)throw h}}}}catch(g){a=!0,o=g}finally{try{!i&&u["return"]&&u["return"]()}finally{if(a)throw o}}}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:" ",stringFormatter:null},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:"_sanitizeBaseBuilder",value:function(t){if(t instanceof p.BaseBuilder)return t;throw new Error("must be a BaseBuilder instance")}},{key:"_sanitizeTable",value:function(t){if("string"!=typeof t)try{t=this._sanitizeBaseBuilder(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("string"===i&&this.options.stringFormatter)return this.options.stringFormatter(t);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){for(var n=arguments.length<=2||void 0===arguments[2]?{}:arguments[2],i=[],a=[],o=0;e.length>o;++o){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=0;t.length>r;++r){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");for(var n="",a=[],o=0;ol.indexOf("=")&&(l=l+" = "+this.options.parameterCharacter);var s=this._buildString(l,[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(){for(var e=this,r=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n=r.buildParameterized,a=this._fields.map(function(t){return e._formatFieldName(t)}).join(", "),o=[],l=[],u=0;u1?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=[],i=!0,a=!1,o=void 0;try{for(var l,u=this._conditions[Symbol.iterator]();!(i=(l=u.next()).done);i=!0){var s=l.value,f=s.expr,c=s.values,h=f instanceof p.Expression?f._toParamString({buildParameterized:e.buildParameterized}):this._buildString(f,c,{buildParameterized:e.buildParameterized});h.text.length&&r.push(h.text),n.push.apply(n,t(h.values))}}catch(v){a=!0,o=v}finally{try{!i&&u["return"]&&u["return"]()}finally{if(a)throw o}}return r.length&&(r=r.join(") AND (")),{text:r.length?this.options.verb+" ("+r+")":"",values:n}}}]),a}(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 m=void 0;m=d instanceof p.Expression?d._toParamString({buildParameterized:e.buildParameterized}):this._buildString(d,[],{buildParameterized:e.buildParameterized}),r+=this._applyNestingFormatting(m.text),n.push.apply(n,t(m.values))}}}catch(b){o=!0,l=b}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;n1?e-1:0),n=1;nt||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])&&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("string"===i&&this.options.stringFormatter)return this.options.stringFormatter(t);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]||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){for(var n=arguments.length<=2||void 0===arguments[2]?{}:arguments[2],i=[],a=[],o=0;e.length>o;++o){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;n1?e-1:0),n=1;n1?e-1:0),n=1;n1?e-1:0),n=1;n1)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=0;t.length>r;++r){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(0=this._fields.length)throw new Error("set() needs to be called");for(var n="",a=[],o=0;ol.indexOf("=")&&(l=l+" = "+this.options.parameterCharacter);var s=this._buildString(l,[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(){for(var e=this,r=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n=r.buildParameterized,a=this._fields.map(function(t){return e._formatFieldName(t)}).join(", "),o=[],l=[],u=0;u1?e-1:0),n=1;n1?e-1:0),n=1;n1?e-1:0),n=1;n2?r-2:0),i=2;i1?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":y(e))&&Object.getOwnPropertyNames(e).forEach(function(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":y(e))?e.v:void 0}function c(t,e,n){var r="undefined"==typeof e?"undefined":y(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");var i=!0,o=!1,a=void 0;try{for(var l,u=t[Symbol.iterator]();!(i=(l=u.next()).done);i=!0){var s=l.value;if(s.type===e)return void(s.handler=n)}}catch(c){o=!0,a=c}finally{try{!i&&u["return"]&&u["return"]()}finally{if(o)throw a}}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];var i=!0,o=!1,a=void 0;try{for(var l,u=n[Symbol.iterator]();!(i=(l=u.next()).done);i=!0){var s=l.value,c=!0,f=!1,h=void 0;try{for(var v,d=s[Symbol.iterator]();!(c=(v=d.next()).done);c=!0){var p=v.value;if(("undefined"==typeof t?"undefined":y(t))===p.type||"string"!=typeof p.type&&t instanceof p.type)return p.handler}}catch(g){f=!0,h=g}finally{try{!c&&d["return"]&&d["return"]()}finally{if(f)throw h}}}}catch(g){o=!0,a=g}finally{try{!i&&u["return"]&&u["return"]()}finally{if(o)throw a}}}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:" ",stringFormatter:null},p.globalValueHandlers=[],p.registerValueHandler=function(t,e){c(p.globalValueHandlers,t,e)},p.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}(),p.BaseBuilder=function(i){function a(t){r(this,a);var n=e(this,Object.getPrototypeOf(a).call(this)),i=JSON.parse(JSON.stringify(p.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 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:"_sanitizeBaseBuilder",value:function(t){if(t instanceof p.BaseBuilder)return t;throw new Error("must be a BaseBuilder instance")}},{key:"_sanitizeTable",value:function(t){if("string"!=typeof t)try{t=this._sanitizeBaseBuilder(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 n=!!f(t,this.options.valueHandlers,p.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,p.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":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("string"===i&&this.options.stringFormatter)return this.options.stringFormatter(t);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 p.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 y=v.map(function(){return f}).join(", ");u+="("+y+")",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){for(var r=arguments.length<=2||void 0===arguments[2]?{}:arguments[2],i=[],o=[],a=0;e.length>a;++a){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}(p.Cloneable),p.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 p.Expression?f._toParamString({buildParameterized:e.buildParameterized,nested:!0}):this._buildString(f,h,{buildParameterized:e.buildParameterized}),d=v.text,y=v.values;n.length&&n.push(c),n.push(d),r.push.apply(r,t(y))}}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}(p.BaseBuilder),p.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({},p.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(y){a=!0,l=y}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}(p.BaseBuilder),p.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)||p.Block.prototype[n]||(t[n]=e[n])}),e=Object.getPrototypeOf(e);return t}}]),i}(p.BaseBuilder),p.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}(p.Block),p.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}(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(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":y(t)))throw new Error("Expected an object but got "+("undefined"==typeof t?"undefined":y(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":y(t)));this._reset();for(var n=0;t.length>n;++n){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}(p.Block),p.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");for(var r="",o=[],a=0;al.indexOf("=")&&(l=l+" = "+this.options.parameterCharacter);var s=this._buildString(l,[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}(p.AbstractSetFieldBlock),p.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(){for(var e=this,n=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r=n.buildParameterized,o=this._fields.map(function(t){return e._formatFieldName(t)}).join(", "),a=[],l=[],u=0;u1?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=[],i=!0,o=!1,a=void 0;try{for(var l,u=this._conditions[Symbol.iterator]();!(i=(l=u.next()).done);i=!0){var s=l.value,c=s.expr,f=s.values,h=c instanceof p.Expression?c._toParamString({buildParameterized:e.buildParameterized}):this._buildString(c,f,{buildParameterized:e.buildParameterized});h.text.length&&n.push(h.text),r.push.apply(r,t(h.values))}}catch(v){o=!0,a=v}finally{try{!i&&u["return"]&&u["return"]()}finally{if(o)throw a}}return n.length&&(n=n.join(") AND (")),{text:n.length?this.options.verb+" ("+n+")":"",values:r}}}]),o}(p.Block),p.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}(p.AbstractConditionBlock),p.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}(p.AbstractConditionBlock),p.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(y){a=!0,l=y}finally{try{!o&&s["return"]&&s["return"]()}finally{if(a)throw l}}return{text:n.length?"ORDER BY "+n:"",values:r}}}]),a}(p.Block),p.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}(p.Block),p.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 y=void 0;if(h instanceof p.BaseBuilder){var g=h._toParamString({buildParameterized:e.buildParameterized,nested:!0});r.push.apply(r,t(g.values)),y=g.text}else y=this._formatTableName(h);if(n+=f+" JOIN "+y,v&&(n+=" "+this._formatTableAlias(v)),d){n+=" ON ";var _=void 0;_=d instanceof p.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}(p.Block),p.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 p.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(y){a=!0,l=y}finally{try{!o&&s["return"]&&s["return"]()}finally{if(a)throw l}}return{text:n,values:r}}}]),a}(p.Block),p.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(y){l=!0,u=y}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 0r;r++)n[r]=arguments[r];n.forEach(function(e){t._distinctFields.push(t._sanitizeField(e))})}},{key:"_toParamString",value:function(){var t="";return this._useDistinct&&(t="DISTINCT",this._distinctFields.length&&(t+=" ON ("+this._distinctFields.join(", ")+")")),{text:t,values:[]}}}]),i}(a.Block),a.Select=function(t){function i(t){var n=arguments.length<=1||void 0===arguments[1]?null:arguments[1];return r(this,i),n=n||[new a.WithBlock(t),new a.StringBlock(t,"SELECT"),new a.FunctionBlock(t),new a.DistinctOnBlock(t),new a.GetFieldBlock(t),new a.FromTableBlock(t),new a.JoinBlock(t),new a.WhereBlock(t),new a.GroupByBlock(t),new a.HavingBlock(t),new a.OrderByBlock(t),new a.LimitBlock(t),new a.OffsetBlock(t),new a.UnionBlock(t)],e(this,Object.getPrototypeOf(i).call(this,t,n))}return n(i,t),i}(a.QueryBuilder),a.Insert=function(t){function i(t){var n=arguments.length<=1||void 0===arguments[1]?null:arguments[1];return r(this,i),n=n||[new a.WithBlock(t),new a.StringBlock(t,"INSERT"),new a.IntoTableBlock(t),new a.InsertFieldValueBlock(t),new a.InsertFieldsFromQueryBlock(t),new a.ReturningBlock(t)],e(this,Object.getPrototypeOf(i).call(this,t,n))}return n(i,t),i}(a.QueryBuilder),a.Update=function(t){function i(t){var n=arguments.length<=1||void 0===arguments[1]?null:arguments[1];return r(this,i),n=n||[new a.WithBlock(t),new a.StringBlock(t,"UPDATE"),new a.UpdateTableBlock(t),new a.SetFieldBlock(t),new a.FromTableBlock(t),new a.WhereBlock(t),new a.OrderByBlock(t),new a.LimitBlock(t),new a.ReturningBlock(t)],e(this,Object.getPrototypeOf(i).call(this,t,n))}return n(i,t),i}(a.QueryBuilder),a.Delete=function(t){function i(t){var n=arguments.length<=1||void 0===arguments[1]?null:arguments[1];return r(this,i),n=n||[new a.WithBlock(t),new a.StringBlock(t,"DELETE"),new a.TargetTableBlock(t),new a.FromTableBlock(o({},t,{singleTable:!0})),new a.JoinBlock(t),new a.WhereBlock(t),new a.OrderByBlock(t),new a.LimitBlock(t),new a.ReturningBlock(t)],e(this,Object.getPrototypeOf(i).call(this,t,n))}return n(i,t),i}(a.QueryBuilder)},p}); \ No newline at end of file +/*! 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;r1?e-1:0),r=1;rt||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 n=!!f(t,this.options.valueHandlers,p.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])&&arguments[1],n=f(t,this.options.valueHandlers,p.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":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("string"===i&&this.options.stringFormatter)return this.options.stringFormatter(t);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]||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 p.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 y=v.map(function(){return f}).join(", ");u+="("+y+")",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){for(var r=arguments.length<=2||void 0===arguments[2]?{}:arguments[2],i=[],o=[],a=0;e.length>a;++a){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}(p.Cloneable),p.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;r1?e-1:0),r=1;r1?e-1:0),r=1;r1?e-1:0),r=1;r1)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":y(t)))throw new Error("Expected an object but got "+("undefined"==typeof t?"undefined":y(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":y(t)));this._reset();for(var n=0;t.length>n;++n){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(0=this._fields.length)throw new Error("set() needs to be called");for(var r="",o=[],a=0;al.indexOf("=")&&(l=l+" = "+this.options.parameterCharacter);var s=this._buildString(l,[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}(p.AbstractSetFieldBlock),p.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(){for(var e=this,n=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],r=n.buildParameterized,o=this._fields.map(function(t){return e._formatFieldName(t)}).join(", "),a=[],l=[],u=0;u1?e-1:0),r=1;r1?e-1:0),r=1;r1?e-1:0),r=1;r2?n-2:0),i=2;i