forked from hiddentao/squel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsquel.min.js
24 lines (21 loc) · 9.91 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
/*
Copyright (c) 2012 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 a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u=Array.prototype.slice,v=Object.prototype.hasOwnProperty,w=function(a,b){return function(){return a.apply(b,arguments)}},x=function(a,b){function d(){this.constructor=a}for(var c in b)v.call(b,c)&&(a[c]=b[c]);return d.prototype=b.prototype,a.prototype=new d,a.__super__=b.prototype,a};t=function(){var a,b,c,d,e,f,g;a=arguments[0],c=2<=arguments.length?u.call(arguments,1):[];if(c)for(f=0,g=c.length;f<g;f++){d=c[f];if(d)for(b in d){if(!v.call(d,b))continue;e=d[b],a[b]=e}}return a},d=function(){function b(){this.toString=w(this.toString,this),this.or=w(this.or,this),this.and=w(this.and,this),this.end=w(this.end,this),this.or_begin=w(this.or_begin,this),this.and_begin=w(this.and_begin,this);var a=this;this.tree={parent:null,nodes:[]},this.current=this.tree,this._begin=function(b){var c;return c={type:b,parent:a.current,nodes:[]},a.current.nodes.push(c),a.current=a.current.nodes[a.current.nodes.length-1],a}}var a;return b.prototype.tree=null,b.prototype.current=null,b.prototype.and_begin=function(){return this._begin("AND")},b.prototype.or_begin=function(){return this._begin("OR")},b.prototype.end=function(){if(!this.current.parent)throw new Error("begin() needs to be called");return this.current=this.current.parent,this},b.prototype.and=function(a){if(!a||"string"!=typeof a)throw new Error("expr must be a string");return this.current.nodes.push({type:"AND",expr:a}),this},b.prototype.or=function(a){if(!a||"string"!=typeof a)throw new Error("expr must be a string");return this.current.nodes.push({type:"OR",expr:a}),this},b.prototype.toString=function(){if(null!==this.current.parent)throw new Error("end() needs to be called");return a(this.tree)},a=function(b){var c,d,e,f,g,h;e="",h=b.nodes;for(f=0,g=h.length;f<g;f++)c=h[f],c.expr!=null?d=c.expr:(d=a(c),""!==d&&(d="("+d+")")),""!==d&&(""!==e&&(e+=" "+c.type+" "),e+=d);return e},b}(),a=b={usingValuePlaceholders:!1},k=function(a){var b;if(a&&a.constructor&&a.constructor.toString){b=a.constructor.toString().match(/function\s*(\w+)/);if(b&&b.length===2)return b[1]}},e=k(new d),m=function(a){var b;b=typeof a;if(e!==k(a)&&"string"!==b)throw new Error("condition must be a string or Expression instance");return"Expression"===b&&(a=a.toString()),a},p=function(a,b){if("string"!=typeof a)throw new Error(""+b+" must be a string");return a},n=function(a){return p(a,"field name")},q=function(a){return p(a,"table name")},l=function(a){return p(a,"alias")},o=function(a){a=parseInt(a);if(0>a)throw new Error("limit/offset must be >=0");return a},r=function(a){var b;b=typeof a;if(null!==a&&"string"!==b&&"number"!==b&&"boolean"!==b)throw new Error("field value must be a string, number, boolean or null");return a},j=function(a,b){return null===a?a="NULL":"boolean"==typeof a?a=a?"TRUE":"FALSE":"number"!=typeof a&&!1===b.usingValuePlaceholders&&(a='"'+a+'"'),a},i=function(){function a(){this.limitString=w(this.limitString,this),this.orderString=w(this.orderString,this),this.whereString=w(this.whereString,this),this.limit=w(this.limit,this),this.order=w(this.order,this),this.where=w(this.where,this),this.wheres=[],this.orders=[]}return a.prototype.wheres=null,a.prototype.orders=null,a.prototype.limits=null,a.prototype.where=function(a){return a=m(a),""!==a&&this.wheres.push(a),this},a.prototype.order=function(a,b){return b==null&&(b=!0),a=n(a),this.orders.push({field:a,dir:b?"ASC":"DESC"}),this},a.prototype.limit=function(a){return a=o(a),this.limits=a,this},a.prototype.whereString=function(){return 0<this.wheres.length?" WHERE ("+this.wheres.join(") AND (")+")":""},a.prototype.orderString=function(){var a,b,c,d,e;if(0<this.orders.length){b="",e=this.orders;for(c=0,d=e.length;c<d;c++)a=e[c],""!==b&&(b+=", "),b+=""+a.field+" "+a.dir;return" ORDER BY "+b}return""},a.prototype.limitString=function(){return this.limits?" LIMIT "+this.limits:""},a}(),g=function(a){function b(){this.toString=w(this.toString,this),this.offset=w(this.offset,this),this.group=w(this.group,this),this.outer_join=w(this.outer_join,this),this.right_join=w(this.right_join,this),this.left_join=w(this.left_join,this),this.join=w(this.join,this),this.field=w(this.field,this),this.from=w(this.from,this),this.distinct=w(this.distinct,this);var a=this;b.__super__.constructor.apply(this,arguments),this.froms=[],this.fields=[],this.joins=[],this.groups=[],this._join=function(b,c,d,e){return c=q(c),d&&(d=l(d)),e&&(e=m(e)),a.joins.push({type:b,table:c,alias:d,condition:e}),a}}return x(b,a),b.prototype.froms=null,b.prototype.fields=null,b.prototype.joins=null,b.prototype.groups=null,b.prototype.offsets=null,b.prototype.useDistinct=!1,b.prototype.distinct=function(){return this.useDistinct=!0,this},b.prototype.from=function(a,b){return b==null&&(b=null),a=q(a),b&&(b=l(b)),this.froms.push({name:a,alias:b}),this},b.prototype.field=function(a,b){return b==null&&(b=null),a=n(a),b&&(b=l(b)),this.fields.push({field:a,alias:b}),this},b.prototype.join=function(a,b,c){return b==null&&(b=null),c==null&&(c=null),this._join("INNER",a,b,c)},b.prototype.left_join=function(a,b,c){return b==null&&(b=null),c==null&&(c=null),this._join("LEFT",a,b,c)},b.prototype.right_join=function(a,b,c){return b==null&&(b=null),c==null&&(c=null),this._join("RIGHT",a,b,c)},b.prototype.outer_join=function(a,b,c){return b==null&&(b=null),c==null&&(c=null),this._join("OUTER",a,b,c)},b.prototype.group=function(a){return a=n(a),this.groups.push(a),this},b.prototype.offset=function(a){return a=o(a),this.offsets=a,this},b.prototype.toString=function(){var a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u;if(0>=this.froms.length)throw new Error("from() needs to be called");g="SELECT ",this.useDistinct&&(g+="DISTINCT "),c="",r=this.fields;for(j=0,n=r.length;j<n;j++)b=r[j],""!==c&&(c+=", "),c+=b.field,b.alias&&(c+=' AS "'+b.alias+'"');g+=""===c?"*":c,i="",s=this.froms;for(k=0,o=s.length;k<o;k++)h=s[k],""!==i&&(i+=", "),i+=h.name,h.alias&&(i+=" `"+h.alias+"`");g+=" FROM "+i,f="",t=this.joins;for(l=0,p=t.length;l<p;l++)e=t[l],f+=" "+e.type+" JOIN "+e.table,e.alias&&(f+=" `"+e.alias+"`"),e.condition&&(f+=" ON ("+e.condition+")");g+=f,g+=this.whereString();if(0<this.groups.length){d="",u=this.groups;for(m=0,q=u.length;m<q;m++)a=u[m],""!==d&&(d+=", "),d+=a;g+=" GROUP BY "+d}return g+=this.orderString(),g+=this.limitString(),this.offsets&&(g+=" OFFSET "+this.offsets),g},b}(i),h=function(a){function c(a){this.toString=w(this.toString,this),this.set=w(this.set,this),this.table=w(this.table,this),c.__super__.constructor.apply(this,arguments),this.tables=[],this.fields={},this.options=t({},b,a)}return x(c,a),c.prototype.tables=null,c.prototype.fields=null,c.prototype.options=null,c.prototype.table=function(a,b){return b==null&&(b=null),a=q(a),b&&(b=l(b)),this.tables.push({name:a,alias:b}),this},c.prototype.set=function(a,b){return a=n(a),b=r(b),this.fields[a]=b,this},c.prototype.toString=function(){var a,b,c,d,e,f,g,h,i,k,l;if(0>=this.tables.length)throw new Error("table() needs to be called");b=function(){var b,c;b=this.fields,c=[];for(a in b){if(!v.call(b,a))continue;c.push(a)}return c}.call(this);if(0>=b.length)throw new Error("set() needs to be called");d="UPDATE ",f="",l=this.tables;for(g=0,i=l.length;g<i;g++)e=l[g],""!==f&&(f+=", "),f+=e.name,e.alias&&(f+=" AS `"+e.alias+"`");d+=f,c="";for(h=0,k=b.length;h<k;h++)a=b[h],""!==c&&(c+=", "),c+=""+a+" = "+j(this.fields[a],this.options);return d+=" SET "+c,d+=this.whereString(),d+=this.orderString(),d+=this.limitString(),d},c}(i),c=function(a){function b(){this.toString=w(this.toString,this),this.from=w(this.from,this),b.__super__.constructor.apply(this,arguments)}return x(b,a),b.prototype.table=null,b.prototype.from=function(a){return a=q(a),this.table=a,this},b.prototype.toString=function(){var a;if(!this.table)throw new Error("from() needs to be called");return a="DELETE FROM "+this.table,a+=this.whereString(),a+=this.orderString(),a+=this.limitString(),a},b}(i),f=function(){function b(b){this.toString=w(this.toString,this),this.set=w(this.set,this),this.into=w(this.into,this),this.fields={},this.options=t({},a,b)}return b.prototype.table=null,b.prototype.fields=null,b.prototype.options=null,b.prototype.into=function(a){return a=q(a),this.table=a,this},b.prototype.set=function(a,b){return a=n(a),b=r(b),this.fields[a]=b,this},b.prototype.toString=function(){var a,b,c,d,e,f,g;if(!this.table)throw new Error("into() needs to be called");b=function(){var a,b;a=this.fields,b=[];for(d in a){if(!v.call(a,d))continue;b.push(d)}return b}.call(this);if(0>=b.length)throw new Error("set() needs to be called");c="",e="";for(f=0,g=b.length;f<g;f++)a=b[f],""!==c&&(c+=", "),c+=a,""!==e&&(e+=", "),e+=j(this.fields[a],this.options);return"INSERT INTO "+this.table+" ("+c+") VALUES ("+e+")"},b}(),s={expr:function(){return new d},select:function(){return new g},update:function(a){return new h(a)},insert:function(a){return new f(a)},"delete":function(){return new c}},typeof module!="undefined"&&module!==null&&(module.exports=s),typeof window!="undefined"&&window!==null&&(window.squel=s)})).call(this);