Browse files

Refactored query builder options into one shared amongst all builders.

QueryBuilder base class now sets up the builder options.
  • Loading branch information...
1 parent ebcd928 commit 6f83c347fc6e5130b622035e640b61b42508666d @hiddentao committed Jan 4, 2013
Showing with 145 additions and 109 deletions.
  1. +69 −63 docs/squel.html
  2. +2 −1 package.json
  3. +20 −18 squel.js
  4. +1 −1 squel.min.js
  5. +37 −20 src/squel.coffee
  6. +8 −3 test/insert.test.coffee
  7. +1 −0 test/testbase.coffee
  8. +7 −3 test/update.test.coffee
View
132 docs/squel.html
69 additions, 63 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
View
3 package.json
@@ -14,7 +14,8 @@
"uglify-js": "1.3",
"mocha": "1.6.0",
"chai": "1.3.0",
- "sinon": "1.5.0"
+ "sinon": "1.5.0",
+ "underscore": "1.4.3"
},
"keywords": ["sql", "database", "rdbms"],
"main": "squel",
View
38 squel.js
@@ -27,7 +27,7 @@ OTHER DEALINGS IN THE SOFTWARE.
(function() {
- var Cloneable, DefaultInsertBuilderOptions, DefaultUpdateBuilderOptions, Delete, Expression, Insert, JoinWhereOrderLimit, QueryBuilder, Select, Update, WhereOrderLimit, _export, _extend,
+ var Cloneable, DefaultQueryBuilderOptions, Delete, Expression, Insert, JoinWhereOrderLimit, QueryBuilder, Select, Update, WhereOrderLimit, _export, _extend,
__slice = [].slice,
__hasProp = {}.hasOwnProperty,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
@@ -177,16 +177,19 @@ OTHER DEALINGS IN THE SOFTWARE.
})();
- DefaultInsertBuilderOptions = DefaultUpdateBuilderOptions = {
+ DefaultQueryBuilderOptions = {
+ autoQuoteTableNames: false,
+ autoQuoteFieldNames: false,
+ nameQuoteCharacter: '`',
usingValuePlaceholders: false
};
QueryBuilder = (function(_super) {
__extends(QueryBuilder, _super);
- function QueryBuilder() {
- return QueryBuilder.__super__.constructor.apply(this, arguments);
+ function QueryBuilder(options) {
+ this.options = _extend({}, DefaultQueryBuilderOptions, options);
}
QueryBuilder.prototype._getObjectClassName = function(obj) {
@@ -270,7 +273,7 @@ OTHER DEALINGS IN THE SOFTWARE.
__extends(WhereOrderLimit, _super);
- function WhereOrderLimit() {
+ function WhereOrderLimit(options) {
this._limitString = __bind(this._limitString, this);
this._orderString = __bind(this._orderString, this);
@@ -282,7 +285,7 @@ OTHER DEALINGS IN THE SOFTWARE.
this.order = __bind(this.order, this);
this.where = __bind(this.where, this);
- WhereOrderLimit.__super__.constructor.apply(this, arguments);
+ WhereOrderLimit.__super__.constructor.call(this, options);
this.wheres = [];
this.orders = [];
this.limits = null;
@@ -356,7 +359,7 @@ OTHER DEALINGS IN THE SOFTWARE.
__extends(JoinWhereOrderLimit, _super);
- function JoinWhereOrderLimit() {
+ function JoinWhereOrderLimit(options) {
this._joinString = __bind(this._joinString, this);
this.outer_join = __bind(this.outer_join, this);
@@ -366,7 +369,7 @@ OTHER DEALINGS IN THE SOFTWARE.
this.left_join = __bind(this.left_join, this);
this.join = __bind(this.join, this);
- JoinWhereOrderLimit.__super__.constructor.apply(this, arguments);
+ JoinWhereOrderLimit.__super__.constructor.call(this, options);
this.joins = [];
}
@@ -445,7 +448,7 @@ OTHER DEALINGS IN THE SOFTWARE.
__extends(Select, _super);
- function Select() {
+ function Select(options) {
this.toString = __bind(this.toString, this);
this.offset = __bind(this.offset, this);
@@ -457,7 +460,7 @@ OTHER DEALINGS IN THE SOFTWARE.
this.from = __bind(this.from, this);
this.distinct = __bind(this.distinct, this);
- Select.__super__.constructor.apply(this, arguments);
+ Select.__super__.constructor.call(this, options);
this.froms = [];
this.fields = [];
this.groups = [];
@@ -583,10 +586,9 @@ OTHER DEALINGS IN THE SOFTWARE.
this.set = __bind(this.set, this);
this.table = __bind(this.table, this);
- Update.__super__.constructor.apply(this, arguments);
+ Update.__super__.constructor.call(this, options);
this.tables = [];
this.fields = {};
- this.options = _extend({}, DefaultUpdateBuilderOptions, options);
}
Update.prototype.table = function(table, alias) {
@@ -717,10 +719,9 @@ OTHER DEALINGS IN THE SOFTWARE.
this.set = __bind(this.set, this);
this.into = __bind(this.into, this);
- Insert.__super__.constructor.apply(this, arguments);
+ Insert.__super__.constructor.call(this, options);
this.table = null;
this.fields = {};
- this.options = _extend({}, DefaultInsertBuilderOptions, options);
}
Insert.prototype.into = function(table) {
@@ -778,18 +779,19 @@ OTHER DEALINGS IN THE SOFTWARE.
expr: function() {
return new Expression;
},
- select: function() {
- return new Select;
+ select: function(options) {
+ return new Select(options);
},
update: function(options) {
return new Update(options);
},
insert: function(options) {
return new Insert(options);
},
- "delete": function() {
- return new Delete;
+ "delete": function(options) {
+ return new Delete(options);
},
+ DefaultQueryBuilderOptions: DefaultQueryBuilderOptions,
Cloneable: Cloneable,
Expression: Expression,
QueryBuilder: QueryBuilder,
View
2 squel.min.js
@@ -22,4 +22,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 e,t,n,r,i,s,o,u,a,f,l,c,h,p=[].slice,d={}.hasOwnProperty,v=function(e,t){return function(){return e.apply(t,arguments)}},m=function(e,t){function r(){this.constructor=e}for(var n in t)d.call(t,n)&&(e[n]=t[n]);return r.prototype=t.prototype,e.prototype=new r,e.__super__=t.prototype,e};h=function(){var e,t,n,r,i,s,o;e=arguments[0],n=2<=arguments.length?p.call(arguments,1):[];if(n)for(s=0,o=n.length;s<o;s++){r=n[s];if(r)for(t in r){if(!d.call(r,t))continue;i=r[t],e[t]=i}}return e},e=function(){function e(){}return e.prototype.clone=function(){var e;return e=new this.constructor,h(e,JSON.parse(JSON.stringify(this)))},e}(),i=function(){function t(){this.toString=v(this.toString,this),this.or=v(this.or,this),this.and=v(this.and,this),this.end=v(this.end,this),this.or_begin=v(this.or_begin,this),this.and_begin=v(this.and_begin,this);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}(),t=n={usingValuePlaceholders:!1},u=function(e){function t(){return t.__super__.constructor.apply(this,arguments)}return m(t,e),t.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},t.prototype._sanitizeCondition=function(e){var t,n;n=typeof e,t=this._getObjectClassName(e);if("Expression"!==t&&"string"!==n)throw new Error("condition must be a string or Expression instance");if("Expression"===n||"Expression"===t)e=e.toString();return e},t.prototype._sanitizeName=function(e,t){if("string"!=typeof e)throw new Error(""+t+" must be a string");return e},t.prototype._sanitizeField=function(e){return this._sanitizeName(e,"field name")},t.prototype._sanitizeTable=function(e){return this._sanitizeName(e,"table name")},t.prototype._sanitizeAlias=function(e){return this._sanitizeName(e,"alias")},t.prototype._sanitizeLimitOffset=function(e){e=parseInt(e);if(0>e||isNaN(e))throw new Error("limit/offset must be >=0");return e},t.prototype._sanitizeValue=function(e){var t;t=typeof e;if(null!==e&&"string"!==t&&"number"!==t&&"boolean"!==t)throw new Error("field value must be a string, number, boolean or null");return e},t.prototype._formatValue=function(e,t){return null===e?e="NULL":"boolean"==typeof e?e=e?"TRUE":"FALSE":"number"!=typeof e&&(!t||!1===t.usingValuePlaceholders)&&(e="'"+e+"'"),e},t}(e),l=function(e){function t(){this._limitString=v(this._limitString,this),this._orderString=v(this._orderString,this),this._whereString=v(this._whereString,this),this.limit=v(this.limit,this),this.order=v(this.order,this),this.where=v(this.where,this),t.__super__.constructor.apply(this,arguments),this.wheres=[],this.orders=[],this.limits=null}return m(t,e),t.prototype.where=function(e){return e=this._sanitizeCondition(e),""!==e&&this.wheres.push(e),this},t.prototype.order=function(e,t){return t==null&&(t=!0),e=this._sanitizeField(e),this.orders.push({field:e,dir:t?"ASC":"DESC"}),this},t.prototype.limit=function(e){return e=this._sanitizeLimitOffset(e),this.limits=e,this},t.prototype._whereString=function(){return 0<this.wheres.length?" WHERE ("+this.wheres.join(") AND (")+")":""},t.prototype._orderString=function(){var e,t,n,r,i;if(0<this.orders.length){t="",i=this.orders;for(n=0,r=i.length;n<r;n++)e=i[n],""!==t&&(t+=", "),t+=""+e.field+" "+e.dir;return" ORDER BY "+t}return""},t.prototype._limitString=function(){return this.limits?" LIMIT "+this.limits:""},t}(u),o=function(e){function t(){this._joinString=v(this._joinString,this),this.outer_join=v(this.outer_join,this),this.right_join=v(this.right_join,this),this.left_join=v(this.left_join,this),this.join=v(this.join,this),t.__super__.constructor.apply(this,arguments),this.joins=[]}return m(t,e),t.prototype.join=function(e,t,n,r){return r==null&&(r="INNER"),e=this._sanitizeTable(e),t&&(t=this._sanitizeAlias(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._joinString=function(){var e,t,n,r,i;t="",i=this.joins||[];for(n=0,r=i.length;n<r;n++)e=i[n],t+=" "+e.type+" JOIN "+e.table,e.alias&&(t+=" `"+e.alias+"`"),e.condition&&(t+=" ON ("+e.condition+")");return t},t}(l),a=function(e){function t(){this.toString=v(this.toString,this),this.offset=v(this.offset,this),this.group=v(this.group,this),this.field=v(this.field,this),this.from=v(this.from,this),this.distinct=v(this.distinct,this),t.__super__.constructor.apply(this,arguments),this.froms=[],this.fields=[],this.groups=[],this.offsets=null,this.useDistinct=!1}return m(t,e),t.prototype.distinct=function(){return this.useDistinct=!0,this},t.prototype.from=function(e,t){return t==null&&(t=null),e=this._sanitizeTable(e),t&&(t=this._sanitizeAlias(t)),this.froms.push({name:e,alias:t}),this},t.prototype.field=function(e,t){return t==null&&(t=null),e=this._sanitizeField(e),t&&(t=this._sanitizeAlias(t)),this.fields.push({name:e,alias:t}),this},t.prototype.group=function(e){return e=this._sanitizeField(e),this.groups.push(e),this},t.prototype.offset=function(e){return e=this._sanitizeLimitOffset(e),this.offsets=e,this},t.prototype.toString=function(){var e,t,n,r,i,s,o,u,a,f,l,c,h,p,d,v;if(0>=this.froms.length)throw new Error("from() needs to be called");i="SELECT ",this.useDistinct&&(i+="DISTINCT "),n="",p=this.fields;for(u=0,l=p.length;u<l;u++)t=p[u],""!==n&&(n+=", "),n+=t.name,t.alias&&(n+=' AS "'+t.alias+'"');i+=""===n?"*":n,o="",d=this.froms;for(a=0,c=d.length;a<c;a++)s=d[a],""!==o&&(o+=", "),o+=s.name,s.alias&&(o+=" `"+s.alias+"`");i+=" FROM "+o,i+=this._joinString(),i+=this._whereString();if(0<this.groups.length){r="",v=this.groups;for(f=0,h=v.length;f<h;f++)e=v[f],""!==r&&(r+=", "),r+=e;i+=" GROUP BY "+r}return i+=this._orderString(),i+=this._limitString(),this.offsets&&(i+=" OFFSET "+this.offsets),i},t}(o),f=function(e){function t(e){this.toString=v(this.toString,this),this.set=v(this.set,this),this.table=v(this.table,this),t.__super__.constructor.apply(this,arguments),this.tables=[],this.fields={},this.options=h({},n,e)}return m(t,e),t.prototype.table=function(e,t){return t==null&&(t=null),e=this._sanitizeTable(e),t&&(t=this._sanitizeAlias(t)),this.tables.push({name:e,alias:t}),this},t.prototype.set=function(e,t){return e=this._sanitizeField(e),t=this._sanitizeValue(t),this.fields[e]=t,this},t.prototype.toString=function(){var e,t,n,r,i,s,o,u,a,f,l;if(0>=this.tables.length)throw new Error("table() needs to be called");t=function(){var t,n;t=this.fields,n=[];for(e in t){if(!d.call(t,e))continue;n.push(e)}return n}.call(this);if(0>=t.length)throw new Error("set() needs to be called");r="UPDATE ",s="",l=this.tables;for(o=0,a=l.length;o<a;o++)i=l[o],""!==s&&(s+=", "),s+=i.name,i.alias&&(s+=" AS `"+i.alias+"`");r+=s,n="";for(u=0,f=t.length;u<f;u++)e=t[u],""!==n&&(n+=", "),n+=""+e+" = "+this._formatValue(this.fields[e],this.options);return r+=" SET "+n,r+=this._whereString(),r+=this._orderString(),r+=this._limitString(),r},t}(l),r=function(e){function t(){return this.toString=v(this.toString,this),this.from=v(this.from,this),t.__super__.constructor.apply(this,arguments)}return m(t,e),t.prototype.table=null,t.prototype.from=function(e,t){return e=this._sanitizeTable(e),t&&(t=this._sanitizeAlias(t)),this.table={name:e,alias:t},this},t.prototype.toString=function(){var e;if(!this.table)throw new Error("from() needs to be called");return e="DELETE FROM "+this.table.name,this.table.alias&&(e+=" `"+this.table.alias+"`"),e+=this._joinString(),e+=this._whereString(),e+=this._orderString(),e+=this._limitString(),e},t}(o),s=function(e){function n(e){this.toString=v(this.toString,this),this.set=v(this.set,this),this.into=v(this.into,this),n.__super__.constructor.apply(this,arguments),this.table=null,this.fields={},this.options=h({},t,e)}return m(n,e),n.prototype.into=function(e){return e=this._sanitizeTable(e),this.table=e,this},n.prototype.set=function(e,t){return e=this._sanitizeField(e),t=this._sanitizeValue(t),this.fields[e]=t,this},n.prototype.toString=function(){var e,t,n,r,i,s,o;if(!this.table)throw new Error("into() needs to be called");t=function(){var e,t;e=this.fields,t=[];for(r in e){if(!d.call(e,r))continue;t.push(r)}return t}.call(this);if(0>=t.length)throw new Error("set() needs to be called");n="",i="";for(s=0,o=t.length;s<o;s++)e=t[s],""!==n&&(n+=", "),n+=e,""!==i&&(i+=", "),i+=this._formatValue(this.fields[e],this.options);return"INSERT INTO "+this.table+" ("+n+") VALUES ("+i+")"},n}(u),c={expr:function(){return new i},select:function(){return new a},update:function(e){return new f(e)},insert:function(e){return new s(e)},"delete":function(){return new r},Cloneable:e,Expression:i,QueryBuilder:u,WhereOrderLimit:l,JoinWhereOrderLimit:o,Select:a,Update:f,Insert:s,Delete:r},typeof module!="undefined"&&module!==null&&(module.exports=c),typeof window!="undefined"&&window!==null&&(window.squel=c)}).call(this);
+*/(function(){var e,t,n,r,i,s,o,u,a,f,l,c,h=[].slice,p={}.hasOwnProperty,d=function(e,t){return function(){return e.apply(t,arguments)}},v=function(e,t){function r(){this.constructor=e}for(var n in t)p.call(t,n)&&(e[n]=t[n]);return r.prototype=t.prototype,e.prototype=new r,e.__super__=t.prototype,e};c=function(){var e,t,n,r,i,s,o;e=arguments[0],n=2<=arguments.length?h.call(arguments,1):[];if(n)for(s=0,o=n.length;s<o;s++){r=n[s];if(r)for(t in r){if(!p.call(r,t))continue;i=r[t],e[t]=i}}return e},e=function(){function e(){}return e.prototype.clone=function(){var e;return e=new this.constructor,c(e,JSON.parse(JSON.stringify(this)))},e}(),r=function(){function t(){this.toString=d(this.toString,this),this.or=d(this.or,this),this.and=d(this.and,this),this.end=d(this.end,this),this.or_begin=d(this.or_begin,this),this.and_begin=d(this.and_begin,this);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}(),t={autoQuoteTableNames:!1,autoQuoteFieldNames:!1,nameQuoteCharacter:"`",usingValuePlaceholders:!1},o=function(e){function n(e){this.options=c({},t,e)}return v(n,e),n.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},n.prototype._sanitizeCondition=function(e){var t,n;n=typeof e,t=this._getObjectClassName(e);if("Expression"!==t&&"string"!==n)throw new Error("condition must be a string or Expression instance");if("Expression"===n||"Expression"===t)e=e.toString();return e},n.prototype._sanitizeName=function(e,t){if("string"!=typeof e)throw new Error(""+t+" must be a string");return e},n.prototype._sanitizeField=function(e){return this._sanitizeName(e,"field name")},n.prototype._sanitizeTable=function(e){return this._sanitizeName(e,"table name")},n.prototype._sanitizeAlias=function(e){return this._sanitizeName(e,"alias")},n.prototype._sanitizeLimitOffset=function(e){e=parseInt(e);if(0>e||isNaN(e))throw new Error("limit/offset must be >=0");return e},n.prototype._sanitizeValue=function(e){var t;t=typeof e;if(null!==e&&"string"!==t&&"number"!==t&&"boolean"!==t)throw new Error("field value must be a string, number, boolean or null");return e},n.prototype._formatValue=function(e,t){return null===e?e="NULL":"boolean"==typeof e?e=e?"TRUE":"FALSE":"number"!=typeof e&&(!t||!1===t.usingValuePlaceholders)&&(e="'"+e+"'"),e},n}(e),f=function(e){function t(e){this._limitString=d(this._limitString,this),this._orderString=d(this._orderString,this),this._whereString=d(this._whereString,this),this.limit=d(this.limit,this),this.order=d(this.order,this),this.where=d(this.where,this),t.__super__.constructor.call(this,e),this.wheres=[],this.orders=[],this.limits=null}return v(t,e),t.prototype.where=function(e){return e=this._sanitizeCondition(e),""!==e&&this.wheres.push(e),this},t.prototype.order=function(e,t){return t==null&&(t=!0),e=this._sanitizeField(e),this.orders.push({field:e,dir:t?"ASC":"DESC"}),this},t.prototype.limit=function(e){return e=this._sanitizeLimitOffset(e),this.limits=e,this},t.prototype._whereString=function(){return 0<this.wheres.length?" WHERE ("+this.wheres.join(") AND (")+")":""},t.prototype._orderString=function(){var e,t,n,r,i;if(0<this.orders.length){t="",i=this.orders;for(n=0,r=i.length;n<r;n++)e=i[n],""!==t&&(t+=", "),t+=""+e.field+" "+e.dir;return" ORDER BY "+t}return""},t.prototype._limitString=function(){return this.limits?" LIMIT "+this.limits:""},t}(o),s=function(e){function t(e){this._joinString=d(this._joinString,this),this.outer_join=d(this.outer_join,this),this.right_join=d(this.right_join,this),this.left_join=d(this.left_join,this),this.join=d(this.join,this),t.__super__.constructor.call(this,e),this.joins=[]}return v(t,e),t.prototype.join=function(e,t,n,r){return r==null&&(r="INNER"),e=this._sanitizeTable(e),t&&(t=this._sanitizeAlias(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._joinString=function(){var e,t,n,r,i;t="",i=this.joins||[];for(n=0,r=i.length;n<r;n++)e=i[n],t+=" "+e.type+" JOIN "+e.table,e.alias&&(t+=" `"+e.alias+"`"),e.condition&&(t+=" ON ("+e.condition+")");return t},t}(f),u=function(e){function t(e){this.toString=d(this.toString,this),this.offset=d(this.offset,this),this.group=d(this.group,this),this.field=d(this.field,this),this.from=d(this.from,this),this.distinct=d(this.distinct,this),t.__super__.constructor.call(this,e),this.froms=[],this.fields=[],this.groups=[],this.offsets=null,this.useDistinct=!1}return v(t,e),t.prototype.distinct=function(){return this.useDistinct=!0,this},t.prototype.from=function(e,t){return t==null&&(t=null),e=this._sanitizeTable(e),t&&(t=this._sanitizeAlias(t)),this.froms.push({name:e,alias:t}),this},t.prototype.field=function(e,t){return t==null&&(t=null),e=this._sanitizeField(e),t&&(t=this._sanitizeAlias(t)),this.fields.push({name:e,alias:t}),this},t.prototype.group=function(e){return e=this._sanitizeField(e),this.groups.push(e),this},t.prototype.offset=function(e){return e=this._sanitizeLimitOffset(e),this.offsets=e,this},t.prototype.toString=function(){var e,t,n,r,i,s,o,u,a,f,l,c,h,p,d,v;if(0>=this.froms.length)throw new Error("from() needs to be called");i="SELECT ",this.useDistinct&&(i+="DISTINCT "),n="",p=this.fields;for(u=0,l=p.length;u<l;u++)t=p[u],""!==n&&(n+=", "),n+=t.name,t.alias&&(n+=' AS "'+t.alias+'"');i+=""===n?"*":n,o="",d=this.froms;for(a=0,c=d.length;a<c;a++)s=d[a],""!==o&&(o+=", "),o+=s.name,s.alias&&(o+=" `"+s.alias+"`");i+=" FROM "+o,i+=this._joinString(),i+=this._whereString();if(0<this.groups.length){r="",v=this.groups;for(f=0,h=v.length;f<h;f++)e=v[f],""!==r&&(r+=", "),r+=e;i+=" GROUP BY "+r}return i+=this._orderString(),i+=this._limitString(),this.offsets&&(i+=" OFFSET "+this.offsets),i},t}(s),a=function(e){function t(e){this.toString=d(this.toString,this),this.set=d(this.set,this),this.table=d(this.table,this),t.__super__.constructor.call(this,e),this.tables=[],this.fields={}}return v(t,e),t.prototype.table=function(e,t){return t==null&&(t=null),e=this._sanitizeTable(e),t&&(t=this._sanitizeAlias(t)),this.tables.push({name:e,alias:t}),this},t.prototype.set=function(e,t){return e=this._sanitizeField(e),t=this._sanitizeValue(t),this.fields[e]=t,this},t.prototype.toString=function(){var e,t,n,r,i,s,o,u,a,f,l;if(0>=this.tables.length)throw new Error("table() needs to be called");t=function(){var t,n;t=this.fields,n=[];for(e in t){if(!p.call(t,e))continue;n.push(e)}return n}.call(this);if(0>=t.length)throw new Error("set() needs to be called");r="UPDATE ",s="",l=this.tables;for(o=0,a=l.length;o<a;o++)i=l[o],""!==s&&(s+=", "),s+=i.name,i.alias&&(s+=" AS `"+i.alias+"`");r+=s,n="";for(u=0,f=t.length;u<f;u++)e=t[u],""!==n&&(n+=", "),n+=""+e+" = "+this._formatValue(this.fields[e],this.options);return r+=" SET "+n,r+=this._whereString(),r+=this._orderString(),r+=this._limitString(),r},t}(f),n=function(e){function t(){return this.toString=d(this.toString,this),this.from=d(this.from,this),t.__super__.constructor.apply(this,arguments)}return v(t,e),t.prototype.table=null,t.prototype.from=function(e,t){return e=this._sanitizeTable(e),t&&(t=this._sanitizeAlias(t)),this.table={name:e,alias:t},this},t.prototype.toString=function(){var e;if(!this.table)throw new Error("from() needs to be called");return e="DELETE FROM "+this.table.name,this.table.alias&&(e+=" `"+this.table.alias+"`"),e+=this._joinString(),e+=this._whereString(),e+=this._orderString(),e+=this._limitString(),e},t}(s),i=function(e){function t(e){this.toString=d(this.toString,this),this.set=d(this.set,this),this.into=d(this.into,this),t.__super__.constructor.call(this,e),this.table=null,this.fields={}}return v(t,e),t.prototype.into=function(e){return e=this._sanitizeTable(e),this.table=e,this},t.prototype.set=function(e,t){return e=this._sanitizeField(e),t=this._sanitizeValue(t),this.fields[e]=t,this},t.prototype.toString=function(){var e,t,n,r,i,s,o;if(!this.table)throw new Error("into() needs to be called");t=function(){var e,t;e=this.fields,t=[];for(r in e){if(!p.call(e,r))continue;t.push(r)}return t}.call(this);if(0>=t.length)throw new Error("set() needs to be called");n="",i="";for(s=0,o=t.length;s<o;s++)e=t[s],""!==n&&(n+=", "),n+=e,""!==i&&(i+=", "),i+=this._formatValue(this.fields[e],this.options);return"INSERT INTO "+this.table+" ("+n+") VALUES ("+i+")"},t}(o),l={expr:function(){return new r},select:function(e){return new u(e)},update:function(e){return new a(e)},insert:function(e){return new i(e)},"delete":function(e){return new n(e)},DefaultQueryBuilderOptions:t,Cloneable:e,Expression:r,QueryBuilder:o,WhereOrderLimit:f,JoinWhereOrderLimit:s,Select:u,Update:a,Insert:i,Delete:n},typeof module!="undefined"&&module!==null&&(module.exports=l),typeof window!="undefined"&&window!==null&&(window.squel=l)}).call(this);
View
57 src/squel.coffee
@@ -147,17 +147,35 @@ 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
+
+
+# Default query builder options
+DefaultQueryBuilderOptions =
+ # If true then table names will be rendered inside quotes. The quote character used is configurable via the
+ # nameQuoteCharacter option.
+ autoQuoteTableNames: false
+ # If true then field names will rendered inside quotes. The quote character used is configurable via the
+ # nameQuoteCharacter option.
+ autoQuoteFieldNames: false
+ # The quote character used for when quoting table and field names
+ nameQuoteCharacter: '`'
+ # If true then field values will not be rendered inside quotes so as to allow for field value placeholders (for
+ # parameterized querying).
+ usingValuePlaceholders: false
# Base class for all query builders
class QueryBuilder extends Cloneable
+ # Constructor
+ #
+ # options is an Object overriding one or more of DefaultQueryBuilderOptions
+ #
+ constructor: (options) ->
+ @options = _extend {}, DefaultQueryBuilderOptions, options
+
+
# Get class name of given object.
_getObjectClassName: (obj) ->
if obj && obj.constructor && obj.constructor.toString
@@ -222,8 +240,8 @@ class QueryBuilder extends Cloneable
# Base class for query builders which support WHERE, ORDER and LIMIT clauses.
class WhereOrderLimit extends QueryBuilder
- constructor: ->
- super
+ constructor: (options) ->
+ super options
@wheres = []
@orders = []
@limits = null
@@ -288,8 +306,8 @@ class WhereOrderLimit extends QueryBuilder
# Base class for query builders with JOIN clauses.
class JoinWhereOrderLimit extends WhereOrderLimit
- constructor: ->
- super
+ constructor: (options) ->
+ super options
@joins = []
# Add a JOIN with the given table.
@@ -352,8 +370,8 @@ class JoinWhereOrderLimit extends WhereOrderLimit
#
# All the build methods in this object return the object instance for chained method calling purposes.
class Select extends JoinWhereOrderLimit
- constructor: ->
- super
+ constructor: (options) ->
+ super options
@froms = []
@fields = []
@groups = []
@@ -476,12 +494,10 @@ class Select extends JoinWhereOrderLimit
#
# All the build methods in this object return the object instance for chained method calling purposes.
class Update extends WhereOrderLimit
- # options: see DefaultBuilderOptions
constructor: (options) ->
- super
+ super options
@tables = []
@fields = {}
- @options = _extend {}, DefaultUpdateBuilderOptions, options
# Update the given table.
@@ -593,12 +609,11 @@ class Delete extends JoinWhereOrderLimit
#
# All the build methods in this object return the object instance for chained method calling purposes.
class Insert extends QueryBuilder
- # options: see DefaultBuilderOptions
+ # options: see DefaultQueryBuilderOptions
constructor: (options) ->
- super
+ super options
@table = null
@fields = {}
- @options = _extend {}, DefaultInsertBuilderOptions, options
# The table to insert into.
@@ -636,13 +651,14 @@ class Insert extends QueryBuilder
-# Export everything as easily usable methods.
+# Export as easily usable methods.
_export = {
expr: -> new Expression
- select: -> new Select
+ select: (options) -> new Select(options)
update: (options) -> new Update(options)
insert: (options) -> new Insert(options)
- delete: -> new Delete
+ delete: (options) -> new Delete(options)
+ DefaultQueryBuilderOptions
Cloneable
Expression
QueryBuilder
@@ -653,6 +669,7 @@ _export = {
Insert
Delete
}
+
module?.exports = _export
window?.squel = _export
View
11 test/insert.test.coffee
@@ -25,7 +25,7 @@ OTHER DEALINGS IN THE SOFTWARE.
squel = require "../src/squel"
-{testCreator, assert, expect, should} = require './testbase'
+{_, testCreator, assert, expect, should} = require './testbase'
test = testCreator()
@@ -40,7 +40,7 @@ test['INSERT builder'] =
'default field values': ->
assert.same null, @inst.table
assert.same {}, @inst.fields
- assert.same { usingValuePlaceholders: false }, @inst.options
+ assert.same squel.DefaultQueryBuilderOptions, @inst.options
'constructor':
'override options': ->
@@ -50,7 +50,12 @@ test['INSERT builder'] =
assert.same null, @inst.table
assert.same {}, @inst.fields
- assert.same { usingValuePlaceholders: true, dummy: true }, @inst.options
+
+ expectedOptions = _.extend {}, squel.DefaultQueryBuilderOptions,
+ usingValuePlaceholders: true
+ dummy: true
+
+ assert.same expectedOptions, @inst.options
'>> into()':
View
1 test/testbase.coffee
@@ -57,6 +57,7 @@ testCreator = ->
module?.exports =
+ _: require('underscore')
testCreator: testCreator
assert: assert
expect: expect
View
10 test/update.test.coffee
@@ -25,7 +25,7 @@ OTHER DEALINGS IN THE SOFTWARE.
squel = require "../src/squel"
-{testCreator, assert, expect, should} = require './testbase'
+{_, testCreator, assert, expect, should} = require './testbase'
test = testCreator()
@@ -40,7 +40,7 @@ test['UPDATE builder'] =
'default field values': ->
assert.same [], @inst.tables
assert.same {}, @inst.fields
- assert.same { usingValuePlaceholders: false }, @inst.options
+ assert.same squel.DefaultQueryBuilderOptions, @inst.options
'constructor':
'override options': ->
@@ -50,7 +50,11 @@ test['UPDATE builder'] =
assert.same [], @inst.tables
assert.same {}, @inst.fields
- assert.same { usingValuePlaceholders: true, dummy: true }, @inst.options
+
+ expectedOptions = _.extend {}, squel.DefaultQueryBuilderOptions,
+ usingValuePlaceholders: true
+ dummy: true
+ assert.same expectedOptions, @inst.options
'>> table()':

0 comments on commit 6f83c34

Please sign in to comment.