Permalink
Browse files

Renamed "autoQuotes" parameter to "usingValuePlaceholders".

  • Loading branch information...
1 parent 5e64891 commit d4bf23b48c94e720c4646c2fda0ba842d580f345 Ram committed Jan 27, 2012
Showing with 179 additions and 170 deletions.
  1. +120 −123 docs/squel.html
  2. +28 −20 squel.js
  3. +1 −1 squel.min.js
  4. +26 −22 src/squel.coffee
  5. +2 −2 test/insert.coffee
  6. +2 −2 test/update.coffee
View
243 docs/squel.html
120 additions, 123 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
View
48 squel.js
@@ -25,18 +25,26 @@ OTHER DEALINGS IN THE SOFTWARE.
*/
(function() {
- var Delete, Expression, ExpressionClassName, Insert, Select, Update, WhereOrderLimit, formatValue, getObjectClassName, sanitizeAlias, sanitizeCondition, sanitizeField, sanitizeLimitOffset, sanitizeName, sanitizeTable, sanitizeValue, _export, _extend,
+ var DefaultInsertBuilderOptions, DefaultUpdateBuilderOptions, Delete, Expression, ExpressionClassName, Insert, Select, Update, WhereOrderLimit, formatValue, getObjectClassName, sanitizeAlias, sanitizeCondition, sanitizeField, sanitizeLimitOffset, sanitizeName, sanitizeTable, sanitizeValue, _export, _extend,
+ __slice = Array.prototype.slice,
__hasProp = Object.prototype.hasOwnProperty,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; };
- _extend = function(dst, src) {
- var k, v;
- if (src == null) src = {};
- for (k in src) {
- if (!__hasProp.call(src, k)) continue;
- v = src[k];
- dst[k] = v;
+ _extend = function() {
+ var dst, k, sources, src, v, _i, _len;
+ dst = arguments[0], sources = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
+ if (sources) {
+ for (_i = 0, _len = sources.length; _i < _len; _i++) {
+ src = sources[_i];
+ if (src) {
+ for (k in src) {
+ if (!__hasProp.call(src, k)) continue;
+ v = src[k];
+ dst[k] = v;
+ }
+ }
+ }
}
return dst;
};
@@ -141,6 +149,10 @@ OTHER DEALINGS IN THE SOFTWARE.
})();
+ DefaultInsertBuilderOptions = DefaultUpdateBuilderOptions = {
+ usingValuePlaceholders: false
+ };
+
getObjectClassName = function(obj) {
var arr;
if (obj && obj.constructor && obj.constructor.toString) {
@@ -195,13 +207,13 @@ OTHER DEALINGS IN THE SOFTWARE.
return item;
};
- formatValue = function(value, formattingOptions) {
+ formatValue = function(value, options) {
if (null === value) {
value = "NULL";
} else if ("boolean" === typeof value) {
value = value ? "TRUE" : "FALSE";
} else if ("number" !== typeof value) {
- if (formattingOptions.autoQuotes) value = "\"" + value + "\"";
+ if (false === options.usingValuePlaceholders) value = "\"" + value + "\"";
}
return value;
};
@@ -453,17 +465,15 @@ OTHER DEALINGS IN THE SOFTWARE.
Update.prototype.fields = null;
- Update.prototype.formattingOptions = null;
+ Update.prototype.options = null;
function Update(options) {
this.toString = __bind(this.toString, this);
this.set = __bind(this.set, this);
this.table = __bind(this.table, this); Update.__super__.constructor.apply(this, arguments);
this.tables = [];
this.fields = {};
- this.formattingOptions = _extend({
- autoQuotes: true
- }, options);
+ this.options = _extend({}, DefaultUpdateBuilderOptions, options);
}
Update.prototype.table = function(table, alias) {
@@ -512,7 +522,7 @@ OTHER DEALINGS IN THE SOFTWARE.
for (_j = 0, _len2 = fieldNames.length; _j < _len2; _j++) {
field = fieldNames[_j];
if ("" !== fields) fields += ", ";
- fields += "" + field + " = " + (formatValue(this.fields[field], this.formattingOptions));
+ fields += "" + field + " = " + (formatValue(this.fields[field], this.options));
}
ret += " SET " + fields;
ret += this.whereString();
@@ -563,15 +573,13 @@ OTHER DEALINGS IN THE SOFTWARE.
Insert.prototype.fields = null;
- Insert.prototype.formattingOptions = null;
+ Insert.prototype.options = null;
function Insert(options) {
this.toString = __bind(this.toString, this);
this.set = __bind(this.set, this);
this.into = __bind(this.into, this); this.fields = {};
- this.formattingOptions = _extend({
- autoQuotes: true
- }, options);
+ this.options = _extend({}, DefaultInsertBuilderOptions, options);
}
Insert.prototype.into = function(table) {
@@ -608,7 +616,7 @@ OTHER DEALINGS IN THE SOFTWARE.
if ("" !== fields) fields += ", ";
fields += field;
if ("" !== values) values += ", ";
- values += formatValue(this.fields[field], this.formattingOptions);
+ values += formatValue(this.fields[field], this.options);
}
return "INSERT INTO " + this.table + " (" + fields + ") VALUES (" + values + ")";
};
View
2 squel.min.js
@@ -21,4 +21,4 @@ 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=Object.prototype.hasOwnProperty,t=function(a,b){return function(){return a.apply(b,arguments)}},u=function(a,b){function d(){this.constructor=a}for(var c in b)s.call(b,c)&&(a[c]=b[c]);return d.prototype=b.prototype,a.prototype=new d,a.__super__=b.prototype,a};r=function(a,b){var c,d;b==null&&(b={});for(c in b){if(!s.call(b,c))continue;d=b[c],a[c]=d}return a},b=function(){function b(){this.toString=t(this.toString,this),this.or=t(this.or,this),this.and=t(this.and,this),this.end=t(this.end,this),this.or_begin=t(this.or_begin,this),this.and_begin=t(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}(),i=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]}},c=i(new b),k=function(a){var b;b=typeof a;if(c!==i(a)&&"string"!==b)throw new Error("condition must be a string or Expression instance");return"Expression"===b&&(a=a.toString()),a},n=function(a,b){if("string"!=typeof a)throw new Error(""+b+" must be a string");return a},l=function(a){return n(a,"field name")},o=function(a){return n(a,"table name")},j=function(a){return n(a,"alias")},m=function(a){a=parseInt(a);if(0>a)throw new Error("limit/offset must be >=0");return a},p=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},h=function(a,b){return null===a?a="NULL":"boolean"==typeof a?a=a?"TRUE":"FALSE":"number"!=typeof a&&b.autoQuotes&&(a='"'+a+'"'),a},g=function(){function a(){this.limitString=t(this.limitString,this),this.orderString=t(this.orderString,this),this.whereString=t(this.whereString,this),this.limit=t(this.limit,this),this.order=t(this.order,this),this.where=t(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=k(a),""!==a&&this.wheres.push(a),this},a.prototype.order=function(a,b){return b==null&&(b=!0),a=l(a),this.orders.push({field:a,dir:b?"ASC":"DESC"}),this},a.prototype.limit=function(a){return a=m(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}(),e=function(a){function b(){this.toString=t(this.toString,this),this.offset=t(this.offset,this),this.group=t(this.group,this),this.outer_join=t(this.outer_join,this),this.right_join=t(this.right_join,this),this.left_join=t(this.left_join,this),this.join=t(this.join,this),this.field=t(this.field,this),this.from=t(this.from,this),this.distinct=t(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=o(c),d&&(d=j(d)),e&&(e=k(e)),a.joins.push({type:b,table:c,alias:d,condition:e}),a}}return u(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=o(a),b&&(b=j(b)),this.froms.push({name:a,alias:b}),this},b.prototype.field=function(a,b){return b==null&&(b=null),a=l(a),b&&(b=j(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=l(a),this.groups.push(a),this},b.prototype.offset=function(a){return a=m(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}(g),f=function(a){function b(a){this.toString=t(this.toString,this),this.set=t(this.set,this),this.table=t(this.table,this),b.__super__.constructor.apply(this,arguments),this.tables=[],this.fields={},this.formattingOptions=r({autoQuotes:!0},a)}return u(b,a),b.prototype.tables=null,b.prototype.fields=null,b.prototype.formattingOptions=null,b.prototype.table=function(a,b){return b==null&&(b=null),a=o(a),b&&(b=j(b)),this.tables.push({name:a,alias:b}),this},b.prototype.set=function(a,b){return a=l(a),b=p(b),this.fields[a]=b,this},b.prototype.toString=function(){var a,b,c,d,e,f,g,i,j,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(!s.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,j=l.length;g<j;g++)e=l[g],""!==f&&(f+=", "),f+=e.name,e.alias&&(f+=" AS `"+e.alias+"`");d+=f,c="";for(i=0,k=b.length;i<k;i++)a=b[i],""!==c&&(c+=", "),c+=""+a+" = "+h(this.fields[a],this.formattingOptions);return d+=" SET "+c,d+=this.whereString(),d+=this.orderString(),d+=this.limitString(),d},b}(g),a=function(a){function b(){this.toString=t(this.toString,this),this.from=t(this.from,this),b.__super__.constructor.apply(this,arguments)}return u(b,a),b.prototype.table=null,b.prototype.from=function(a){return a=o(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}(g),d=function(){function a(a){this.toString=t(this.toString,this),this.set=t(this.set,this),this.into=t(this.into,this),this.fields={},this.formattingOptions=r({autoQuotes:!0},a)}return a.prototype.table=null,a.prototype.fields=null,a.prototype.formattingOptions=null,a.prototype.into=function(a){return a=o(a),this.table=a,this},a.prototype.set=function(a,b){return a=l(a),b=p(b),this.fields[a]=b,this},a.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(!s.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+=h(this.fields[a],this.formattingOptions);return"INSERT INTO "+this.table+" ("+c+") VALUES ("+e+")"},a}(),q={expr:function(){return new b},select:function(){return new e},update:function(a){return new f(a)},insert:function(a){return new d(a)},"delete":function(){return new a}},typeof module!="undefined"&&module!==null&&(module.exports=q),typeof window!="undefined"&&window!==null&&(window.squel=q)})).call(this);
+*/((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);
View
48 src/squel.coffee
@@ -24,10 +24,13 @@ OTHER DEALINGS IN THE SOFTWARE.
###
-# Extend given object's with another object's properties, overriding existing ones if necessary
-_extend = (dst, src = {}) ->
- for own k,v of src
- dst[k] = v
+# Extend given object's with other objects' properties, overriding existing ones if necessary
+_extend = (dst, sources...) ->
+ if sources
+ for src in sources
+ if src
+ for own k,v of src
+ dst[k] = v
dst
@@ -134,6 +137,14 @@ class Expression
str
+# Default builder options.
+DefaultInsertBuilderOptions = DefaultUpdateBuilderOptions =
+ # If true then field values will not be rendered inside quotes so as to allow for field value placeholders (for
+ # parameterized querying).
+ usingValuePlaceholders: false
+
+
+
# Get class name of given object.
getObjectClassName = (obj) ->
if obj && obj.constructor && obj.constructor.toString
@@ -180,15 +191,14 @@ sanitizeValue = (item) ->
# Format the given field value for inclusion into the query string
#
-# formattingOptions:
-# autoQuotes: if true (default) then string field values are automatically single-quoted; if false then not.
-formatValue = (value, formattingOptions) ->
+# options: see DefaultBuilderOptions
+formatValue = (value, options) ->
if null is value
value = "NULL"
else if "boolean" is typeof value
value = if value then "TRUE" else "FALSE"
else if "number" isnt typeof value
- if formattingOptions.autoQuotes
+ if false is options.usingValuePlaceholders
value = "\"#{value}\""
value
@@ -450,17 +460,14 @@ class Select extends WhereOrderLimit
class Update extends WhereOrderLimit
tables: null
fields: null
- formattingOptions: null
+ options: null
- # Options can currently include the following keys:
- # autoQuotes: if true (default) then string field values are automatically single-quoted; if false then not.
+ # options: see DefaultBuilderOptions
constructor: (options) ->
super
@tables = []
@fields = {}
- @formattingOptions = _extend({
- autoQuotes: true
- }, options)
+ @options = _extend {}, DefaultUpdateBuilderOptions, options
# Update the given table.
@@ -506,7 +513,7 @@ class Update extends WhereOrderLimit
fields = ""
for field in fieldNames
fields += ", " if "" isnt fields
- fields += "#{field} = #{formatValue(@fields[field], @formattingOptions)}"
+ fields += "#{field} = #{formatValue(@fields[field], @options)}"
ret += " SET #{fields}"
# where
@@ -566,15 +573,12 @@ class Delete extends WhereOrderLimit
class Insert
table: null
fields: null
- formattingOptions: null
+ options: null
- # Options can currently include the following keys:
- # autoQuotes: if true (default) then string field values are automatically single-quoted; if false then not.
+ # options: see DefaultBuilderOptions
constructor: (options) ->
@fields = {}
- @formattingOptions = _extend({
- autoQuotes: true
- }, options)
+ @options = _extend {}, DefaultInsertBuilderOptions, options
# The table to insert into.
@@ -606,7 +610,7 @@ class Insert
fields += ", " if "" isnt fields
fields += field
values += ", " if "" isnt values
- values += formatValue(@fields[field], @formattingOptions)
+ values += formatValue(@fields[field], @options)
"INSERT INTO #{@table} (#{fields}) VALUES (#{values})"
View
4 test/insert.coffee
@@ -96,8 +96,8 @@ suite.addBatch
suite.addBatch
- 'when auto-quotes are turned off':
- topic: -> inst(autoQuotes: false)
+ 'when using value placeholders':
+ topic: -> inst(usingValuePlaceholders: true)
'when into("test") is called':
topic: (obj) -> obj.into("test")
'when set("f1",1) is called':
View
4 test/update.coffee
@@ -135,8 +135,8 @@ suite.addBatch
suite.addBatch
- 'when auto-quotes are turned off':
- topic: -> inst(autoQuotes: off)
+ 'when using value placeholders':
+ topic: -> inst(usingValuePlaceholders: true)
'when table("test") is called':
topic: (obj) -> obj.table("test")
'when set("f1",1) is called':

0 comments on commit d4bf23b

Please sign in to comment.