forked from hiddentao/squel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsquel.min.js
25 lines (22 loc) · 15.6 KB
/
squel.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Generated by CoffeeScript 1.3.3
/*
Copyright (c) 2012-2013 Ramesh Nair (hiddentao.com)
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/(function(){var e,t,n,r,i,s=[].slice,o={}.hasOwnProperty,u=function(e,t){function r(){this.constructor=e}for(var n in t)o.call(t,n)&&(e[n]=t[n]);return r.prototype=t.prototype,e.prototype=new r,e.__super__=t.prototype,e};e={},i=function(){var e,t,n,r,i,u,a;e=arguments[0],n=2<=arguments.length?s.call(arguments,1):[];if(n)for(u=0,a=n.length;u<a;u++){r=n[u];if(r)for(t in r){if(!o.call(r,t))continue;i=r[t],e[t]=i}}return e},e.DefaultQueryBuilderOptions={autoQuoteTableNames:!1,autoQuoteFieldNames:!1,autoQuoteAliasNames:!0,nameQuoteCharacter:"`",tableAliasQuoteCharacter:"`",fieldAliasQuoteCharacter:'"',usingValuePlaceholders:!1,valueHandlers:[]},e.globalValueHandlers=[],n=function(e,t,n){var r,i,s;if("function"!=typeof t)throw new Error("type must be a class constructor");if("function"!=typeof n)throw new Error("handler must be a function");for(i=0,s=e.length;i<s;i++){r=e[i];if(r.type===t){r.handler=n;return}}return e.push({type:t,handler:n})},t=function(){var e,t,n,r,i,o,u,a;r=arguments[0],e=2<=arguments.length?s.call(arguments,1):[];for(i=0,u=e.length;i<u;i++){t=e[i];for(o=0,a=t.length;o<a;o++){n=t[o];if(r instanceof n.type)return n.handler}}return void 0},e.registerValueHandler=function(t,r){return n(e.globalValueHandlers,t,r)},e.Cloneable=function(){function e(){}return e.prototype.clone=function(){var e;return e=new this.constructor,i(e,JSON.parse(JSON.stringify(this)))},e}(),e.BaseBuilder=function(r){function s(t){var n;n=JSON.parse(JSON.stringify(e.DefaultQueryBuilderOptions)),this.options=i({},n,t)}return u(s,r),s.prototype.registerValueHandler=function(e,t){return n(this.options.valueHandlers,e,t),this},s.prototype._getObjectClassName=function(e){var t;if(e&&e.constructor&&e.constructor.toString){t=e.constructor.toString().match(/function\s*(\w+)/);if(t&&t.length===2)return t[1]}return void 0},s.prototype._sanitizeCondition=function(t){t instanceof e.Expression&&(t=t.toString());if("string"!=typeof t)throw new Error("condition must be a string or Expression instance");return t},s.prototype._sanitizeName=function(e,t){if("string"!=typeof e)throw new Error(""+t+" must be a string");return e},s.prototype._sanitizeField=function(e){var t;return t=this._sanitizeName(e,"field name"),this.options.autoQuoteFieldNames?""+this.options.nameQuoteCharacter+t+this.options.nameQuoteCharacter:t},s.prototype._sanitizeTable=function(t,n){var r;n==null&&(n=!1);if(n){if("string"!=typeof t){if(t instanceof e.QueryBuilder&&t.isNestable())return t;throw new Error("table name must be a string or a nestable query instance")}r=t}else r=this._sanitizeName(t,"table name");return this.options.autoQuoteTableNames?""+this.options.nameQuoteCharacter+r+this.options.nameQuoteCharacter:r},s.prototype._sanitizeTableAlias=function(e){var t;return t=this._sanitizeName(e,"table alias"),this.options.autoQuoteAliasNames?""+this.options.tableAliasQuoteCharacter+t+this.options.tableAliasQuoteCharacter:t},s.prototype._sanitizeFieldAlias=function(e){var t;return t=this._sanitizeName(e,"field alias"),this.options.autoQuoteAliasNames?""+this.options.fieldAliasQuoteCharacter+t+this.options.fieldAliasQuoteCharacter:t},s.prototype._sanitizeLimitOffset=function(e){e=parseInt(e);if(0>e||isNaN(e))throw new Error("limit/offset must be >= 0");return e},s.prototype._sanitizeValue=function(n){var r,i;r=typeof n;if(null!==n&&"string"!==r&&"number"!==r&&"boolean"!==r){i=void 0!==t(n,this.options.valueHandlers,e.globalValueHandlers);if(!i)throw new Error("field value must be a string, number, boolean, null or one of the registered custom value types")}return n},s.prototype._formatValue=function(n){var r;return r=t(n,this.options.valueHandlers,e.globalValueHandlers),r&&(n=r(n)),null===n?n="NULL":"boolean"==typeof n?n=n?"TRUE":"FALSE":"number"!=typeof n&&!1===this.options.usingValuePlaceholders&&(n="'"+n+"'"),n},s}(e.Cloneable),e.Expression=function(){function t(){var e=this;this.tree={parent:null,nodes:[]},this.current=this.tree,this._begin=function(t){var n;return n={type:t,parent:e.current,nodes:[]},e.current.nodes.push(n),e.current=e.current.nodes[e.current.nodes.length-1],e}}var e;return t.prototype.tree=null,t.prototype.current=null,t.prototype.and_begin=function(){return this._begin("AND")},t.prototype.or_begin=function(){return this._begin("OR")},t.prototype.end=function(){if(!this.current.parent)throw new Error("begin() needs to be called");return this.current=this.current.parent,this},t.prototype.and=function(e){if(!e||"string"!=typeof e)throw new Error("expr must be a string");return this.current.nodes.push({type:"AND",expr:e}),this},t.prototype.or=function(e){if(!e||"string"!=typeof e)throw new Error("expr must be a string");return this.current.nodes.push({type:"OR",expr:e}),this},t.prototype.toString=function(){if(null!==this.current.parent)throw new Error("end() needs to be called");return e(this.tree)},e=function(t){var n,r,i,s,o,u;i="",u=t.nodes;for(s=0,o=u.length;s<o;s++)n=u[s],n.expr!=null?r=n.expr:(r=e(n),""!==r&&(r="("+r+")")),""!==r&&(""!==i&&(i+=" "+n.type+" "),i+=r);return i},t}(),e.Block=function(t){function n(){return n.__super__.constructor.apply(this,arguments)}return u(n,t),n.prototype.exposedMethods=function(){var t,n,r;n={};for(t in this)r=this[t],typeof r=="function"&&t.charAt(0)!=="_"&&!e.Block.prototype[t]&&(n[t]=r);return n},n.prototype.buildStr=function(e){return""},n}(e.BaseBuilder),e.StringBlock=function(e){function t(e,n){t.__super__.constructor.call(this,e),this.str=n}return u(t,e),t.prototype.buildStr=function(e){return this.str},t}(e.Block),e.AbstractTableBlock=function(e){function t(e){t.__super__.constructor.call(this,e),this.tables=[]}return u(t,e),t.prototype._table=function(e,t){return t==null&&(t=null),t&&(t=this._sanitizeTableAlias(t)),e=this._sanitizeTable(e,this.options.allowNested||!1),this.options.singleTable&&(this.tables=[]),this.tables.push({table:e,alias:t})},t.prototype.buildStr=function(e){var t,n,r,i,s;if(0>=this.tables.length)throw new Error("_table() needs to be called");n="",s=this.tables;for(r=0,i=s.length;r<i;r++)t=s[r],""!==n&&(n+=", "),"string"==typeof t.table?n+=t.table:n+="("+t.table+")",t.alias&&(n+=" "+t.alias);return n},t}(e.Block),e.UpdateTableBlock=function(e){function t(){return t.__super__.constructor.apply(this,arguments)}return u(t,e),t.prototype.table=function(e,t){return t==null&&(t=null),this._table(e,t)},t}(e.AbstractTableBlock),e.FromTableBlock=function(e){function t(){return t.__super__.constructor.apply(this,arguments)}return u(t,e),t.prototype.from=function(e,t){return t==null&&(t=null),this._table(e,t)},t.prototype.buildStr=function(e){var n;if(0>=this.tables.length)throw new Error("from() needs to be called");return n=t.__super__.buildStr.call(this,e),"FROM "+n},t}(e.AbstractTableBlock),e.IntoTableBlock=function(e){function t(e){t.__super__.constructor.call(this,e),this.table=null}return u(t,e),t.prototype.into=function(e){return this.table=this._sanitizeTable(e,!1)},t.prototype.buildStr=function(e){if(!this.table)throw new Error("into() needs to be called");return"INTO "+this.table},t}(e.Block),e.GetFieldBlock=function(e){function t(e){t.__super__.constructor.call(this,e),this.fields=[]}return u(t,e),t.prototype.field=function(e,t){return t==null&&(t=null),e=this._sanitizeField(e),t&&(t=this._sanitizeFieldAlias(t)),this.fields.push({name:e,alias:t})},t.prototype.buildStr=function(e){var t,n,r,i,s;n="",s=this.fields;for(r=0,i=s.length;r<i;r++)t=s[r],""!==n&&(n+=", "),n+=t.name,t.alias&&(n+=" AS "+t.alias);return""===n?"*":n},t}(e.Block),e.SetFieldBlock=function(e){function t(e){t.__super__.constructor.call(this,e),this.fields={}}return u(t,e),t.prototype.set=function(e,t){return e=this._sanitizeField(e),t=this._sanitizeValue(t),this.fields[e]=t,this},t.prototype.buildStr=function(e){var t,n,r,i,s;n=function(){var e,n;e=this.fields,n=[];for(t in e){if(!o.call(e,t))continue;n.push(t)}return n}.call(this);if(0>=n.length)throw new Error("set() needs to be called");r="";for(i=0,s=n.length;i<s;i++)t=n[i],""!==r&&(r+=", "),r+=""+t+" = "+this._formatValue(this.fields[t]);return"SET "+r},t}(e.Block),e.InsertFieldValueBlock=function(e){function t(e){t.__super__.constructor.call(this,e),this.fields={}}return u(t,e),t.prototype.buildStr=function(e){var t,n,r,i,s,u,a;n=function(){var e,t;e=this.fields,t=[];for(i in e){if(!o.call(e,i))continue;t.push(i)}return t}.call(this);if(0>=n.length)throw new Error("set() needs to be called");r="",s="";for(u=0,a=n.length;u<a;u++)t=n[u],""!==r&&(r+=", "),r+=t,""!==s&&(s+=", "),s+=this._formatValue(this.fields[t]);return"("+r+") VALUES ("+s+")"},t}(e.SetFieldBlock),e.DistinctBlock=function(e){function t(e){t.__super__.constructor.call(this,e),this.useDistinct=!1}return u(t,e),t.prototype.distinct=function(){return this.useDistinct=!0},t.prototype.buildStr=function(e){return this.useDistinct?"DISTINCT":""},t}(e.Block),e.GroupByBlock=function(e){function t(e){t.__super__.constructor.call(this,e),this.groups=[]}return u(t,e),t.prototype.group=function(e){return e=this._sanitizeField(e),this.groups.push(e)},t.prototype.buildStr=function(e){var t,n,r,i,s;n="";if(0<this.groups.length){s=this.groups;for(r=0,i=s.length;r<i;r++)t=s[r],""!==n&&(n+=", "),n+=t;n="GROUP BY "+n}return n},t}(e.Block),e.OffsetBlock=function(e){function t(e){t.__super__.constructor.call(this,e),this.offsets=null}return u(t,e),t.prototype.offset=function(e){return e=this._sanitizeLimitOffset(e),this.offsets=e},t.prototype.buildStr=function(e){return this.offsets?"OFFSET "+this.offsets:""},t}(e.Block),e.WhereBlock=function(e){function t(e){t.__super__.constructor.call(this,e),this.wheres=[]}return u(t,e),t.prototype.where=function(){var e,t,n,r,i,o,u,a,f;e=arguments[0],i=2<=arguments.length?s.call(arguments,1):[],e=this._sanitizeCondition(e);for(o=0,a=i.length;o<a;o++){r=i[o];if(Array.isArray(r)){t=[];for(u=0,f=r.length;u<f;u++)n=r[u],t.push(this._formatValue(this._sanitizeValue(n)));r="("+t.join(", ")+")"}else r=this._formatValue(this._sanitizeValue(r));e=e.replace("?",r)}if(""!==e)return this.wheres.push(e)},t.prototype.buildStr=function(e){return 0<this.wheres.length?"WHERE ("+this.wheres.join(") AND (")+")":""},t}(e.Block),e.OrderByBlock=function(e){function t(e){t.__super__.constructor.call(this,e),this.orders=[]}return u(t,e),t.prototype.order=function(e,t){return t==null&&(t=!0),e=this._sanitizeField(e),this.orders.push({field:e,dir:t?!0:!1})},t.prototype.buildStr=function(e){var t,n,r,i,s;if(0<this.orders.length){n="",s=this.orders;for(r=0,i=s.length;r<i;r++)t=s[r],""!==n&&(n+=", "),n+=""+t.field+" "+(t.dir?"ASC":"DESC");return"ORDER BY "+n}return""},t}(e.Block),e.LimitBlock=function(e){function t(e){t.__super__.constructor.call(this,e),this.limits=null}return u(t,e),t.prototype.limit=function(e){return e=this._sanitizeLimitOffset(e),this.limits=e},t.prototype.buildStr=function(e){return this.limits?"LIMIT "+this.limits:""},t}(e.Block),e.JoinBlock=function(e){function t(e){t.__super__.constructor.call(this,e),this.joins=[]}return u(t,e),t.prototype.join=function(e,t,n,r){return t==null&&(t=null),n==null&&(n=null),r==null&&(r="INNER"),e=this._sanitizeTable(e,!0),t&&(t=this._sanitizeTableAlias(t)),n&&(n=this._sanitizeCondition(n)),this.joins.push({type:r,table:e,alias:t,condition:n}),this},t.prototype.left_join=function(e,t,n){return t==null&&(t=null),n==null&&(n=null),this.join(e,t,n,"LEFT")},t.prototype.right_join=function(e,t,n){return t==null&&(t=null),n==null&&(n=null),this.join(e,t,n,"RIGHT")},t.prototype.outer_join=function(e,t,n){return t==null&&(t=null),n==null&&(n=null),this.join(e,t,n,"OUTER")},t.prototype.buildStr=function(e){var t,n,r,i,s;n="",s=this.joins||[];for(r=0,i=s.length;r<i;r++)t=s[r],n!==""&&(n+=" "),n+=""+t.type+" JOIN ","string"==typeof t.table?n+=t.table:n+="("+t.table+")",t.alias&&(n+=" "+t.alias),t.condition&&(n+=" ON ("+t.condition+")");return n},t}(e.Block),e.QueryBuilder=function(e){function t(e,n){var r,i,s,o,u,a,f,l,c=this;t.__super__.constructor.call(this,e),this.blocks=n||[],f=this.blocks;for(u=0,a=f.length;u<a;u++){r=f[u],l=r.exposedMethods(),o=function(e,t,n){return c[t]=function(){return n.apply(e,arguments),c}};for(s in l){i=l[s];if(this[s]!=null)throw new Error(""+this._getObjectClassName(this)+" already has a builder method called: "+s);o(r,s,i)}}}return u(t,e),t.prototype.registerValueHandler=function(e,n){var r,i,s,o;o=this.blocks;for(i=0,s=o.length;i<s;i++)r=o[i],r.registerValueHandler(e,n);return t.__super__.registerValueHandler.call(this,e,n),this},t.prototype.updateOptions=function(e){var t,n,r,s,o;this.options=i({},this.options,e),s=this.blocks,o=[];for(n=0,r=s.length;n<r;n++)t=s[n],o.push(t.options=i({},t.options,e));return o},t.prototype.toString=function(){var e;return function(){var t,n,r,i;r=this.blocks,i=[];for(t=0,n=r.length;t<n;t++)e=r[t],i.push(e.buildStr(this));return i}.call(this).filter(function(e){return 0<e.length}).join(" ")},t.prototype.clone=function(){var e;return new this.constructor(this.options,function(){var t,n,r,i;r=this.blocks,i=[];for(t=0,n=r.length;t<n;t++)e=r[t],i.push(e.clone());return i}.call(this))},t.prototype.isNestable=function(){return!1},t}(e.BaseBuilder),e.Select=function(t){function n(t,r){r==null&&(r=null),r||(r=[new e.StringBlock(t,"SELECT"),new e.DistinctBlock(t),new e.GetFieldBlock(t),new e.FromTableBlock(i({},t,{allowNested:!0})),new e.JoinBlock(i({},t,{allowNested:!0})),new e.WhereBlock(t),new e.GroupByBlock(t),new e.OrderByBlock(t),new e.LimitBlock(t),new e.OffsetBlock(t)]),n.__super__.constructor.call(this,t,r)}return u(n,t),n.prototype.isNestable=function(){return!0},n}(e.QueryBuilder),e.Update=function(t){function n(t,r){r==null&&(r=null),r||(r=[new e.StringBlock(t,"UPDATE"),new e.UpdateTableBlock(t),new e.SetFieldBlock(t),new e.WhereBlock(t),new e.OrderByBlock(t),new e.LimitBlock(t)]),n.__super__.constructor.call(this,t,r)}return u(n,t),n}(e.QueryBuilder),e.Delete=function(t){function n(t,r){r==null&&(r=null),r||(r=[new e.StringBlock(t,"DELETE"),new e.FromTableBlock(i({},t,{singleTable:!0})),new e.JoinBlock(t),new e.WhereBlock(t),new e.OrderByBlock(t),new e.LimitBlock(t)]),n.__super__.constructor.call(this,t,r)}return u(n,t),n}(e.QueryBuilder),e.Insert=function(t){function n(t,r){r==null&&(r=null),r||(r=[new e.StringBlock(t,"INSERT"),new e.IntoTableBlock(t),new e.InsertFieldValueBlock(t)]),n.__super__.constructor.call(this,t,r)}return u(n,t),n}(e.QueryBuilder),r={expr:function(){return new e.Expression},select:function(t,n){return new e.Select(t,n)},update:function(t,n){return new e.Update(t,n)},insert:function(t,n){return new e.Insert(t,n)},"delete":function(t,n){return new e.Delete(t,n)},registerValueHandler:e.registerValueHandler},r.remove=r["delete"],r.cls=e,(typeof define!="undefined"&&define!==null?define.amd:void 0)?define(function(){return r}):(typeof module!="undefined"&&module!==null?module.exports:void 0)?module.exports=r:typeof window!="undefined"&&window!==null&&(window.squel=r)}).call(this);