Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Improved _sanitizeCondition logic such that it survives JS minificati…

…on in tact.
  • Loading branch information...
commit 8e652e2f8b19af81f74b5e07904ee16d10c9ca9b 1 parent 624136e
@hiddentao authored
View
11 docs/squel.html
@@ -37,13 +37,12 @@
<span class="nv">arr = </span><span class="nx">obj</span><span class="p">.</span><span class="nx">constructor</span><span class="p">.</span><span class="nx">toString</span><span class="p">().</span><span class="nx">match</span> <span class="sr">/function\s*(\w+)/</span><span class="p">;</span>
<span class="k">if</span> <span class="nx">arr</span> <span class="o">&amp;&amp;</span> <span class="nx">arr</span><span class="p">.</span><span class="nx">length</span> <span class="o">is</span> <span class="mi">2</span>
<span class="k">return</span> <span class="nx">arr</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
- <span class="k">return</span> <span class="kc">undefined</span></pre></div> </td> </tr> <tr id="section-16"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-16">&#182;</a> </div> <p>Sanitize the given condition.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_sanitizeCondition: </span><span class="nf">(condition) -&gt;</span>
- <span class="nv">t = </span><span class="k">typeof</span> <span class="nx">condition</span>
- <span class="nv">c = </span><span class="nx">@_getObjectClassName</span><span class="p">(</span><span class="nx">condition</span><span class="p">)</span>
-
- <span class="k">if</span> <span class="s">&#39;Expression&#39;</span> <span class="o">isnt</span> <span class="nx">c</span> <span class="o">and</span> <span class="s">&#39;Expression&#39;</span> <span class="o">isnt</span> <span class="nx">t</span> <span class="o">and</span> <span class="s">&quot;string&quot;</span> <span class="o">isnt</span> <span class="nx">t</span>
- <span class="k">throw</span> <span class="k">new</span> <span class="nb">Error</span> <span class="s">&quot;condition must be a string or Expression instance&quot;</span></pre></div> </td> </tr> <tr id="section-17"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-17">&#182;</a> </div> <p>If it's an expression builder instance then convert it to string form.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="s">&#39;Expression&#39;</span> <span class="o">is</span> <span class="nx">t</span> <span class="o">or</span> <span class="s">&#39;Expression&#39;</span> <span class="o">is</span> <span class="nx">c</span>
+ <span class="k">return</span> <span class="kc">undefined</span></pre></div> </td> </tr> <tr id="section-16"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-16">&#182;</a> </div> <p>Sanitize the given condition.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_sanitizeCondition: </span><span class="nf">(condition) -&gt;</span></pre></div> </td> </tr> <tr id="section-17"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-17">&#182;</a> </div> <p>If it's an expression builder instance then convert it to string form.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="nx">condition</span> <span class="k">instanceof</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">Expression</span>
<span class="nv">condition = </span><span class="nx">condition</span><span class="p">.</span><span class="nx">toString</span><span class="p">()</span>
+
+ <span class="k">if</span> <span class="s">&quot;string&quot;</span> <span class="o">isnt</span> <span class="k">typeof</span> <span class="nx">condition</span>
+ <span class="k">throw</span> <span class="k">new</span> <span class="nb">Error</span> <span class="s">&quot;condition must be a string or Expression instance&quot;</span>
+
<span class="nx">condition</span></pre></div> </td> </tr> <tr id="section-18"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-18">&#182;</a> </div> <p>Sanitize the given name.
The 'type' parameter is used to construct a meaningful error message in case validation fails.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_sanitizeName: </span><span class="nf">(value, type) -&gt;</span>
<span class="k">if</span> <span class="s">&quot;string&quot;</span> <span class="o">isnt</span> <span class="k">typeof</span> <span class="nx">value</span>
View
11 squel.js
@@ -93,15 +93,12 @@ OTHER DEALINGS IN THE SOFTWARE.
};
BaseBuilder.prototype._sanitizeCondition = function(condition) {
- var c, t;
- t = typeof condition;
- c = this._getObjectClassName(condition);
- if ('Expression' !== c && 'Expression' !== t && "string" !== t) {
- throw new Error("condition must be a string or Expression instance");
- }
- if ('Expression' === t || 'Expression' === c) {
+ if (condition instanceof cls.Expression) {
condition = condition.toString();
}
+ if ("string" !== typeof condition) {
+ throw new Error("condition must be a string or Expression instance");
+ }
return condition;
};
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=[].slice,i={}.hasOwnProperty,s=function(e,t){function r(){this.constructor=e}for(var n in t)i.call(t,n)&&(e[n]=t[n]);return r.prototype=t.prototype,e.prototype=new r,e.__super__=t.prototype,e};e={},n=function(){var e,t,n,s,o,u,a;e=arguments[0],n=2<=arguments.length?r.call(arguments,1):[];if(n)for(u=0,a=n.length;u<a;u++){s=n[u];if(s)for(t in s){if(!i.call(s,t))continue;o=s[t],e[t]=o}}return e},e.DefaultQueryBuilderOptions={autoQuoteTableNames:!1,autoQuoteFieldNames:!1,nameQuoteCharacter:"`",usingValuePlaceholders:!1},e.Cloneable=function(){function e(){}return e.prototype.clone=function(){var e;return e=new this.constructor,n(e,JSON.parse(JSON.stringify(this)))},e}(),e.BaseBuilder=function(t){function r(t){this.options=n({},e.DefaultQueryBuilderOptions,t)}return s(r,t),r.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},r.prototype._sanitizeCondition=function(e){var t,n;n=typeof e,t=this._getObjectClassName(e);if("Expression"!==t&&"Expression"!==n&&"string"!==n)throw new Error("condition must be a string or Expression instance");if("Expression"===n||"Expression"===t)e=e.toString();return e},r.prototype._sanitizeName=function(e,t){if("string"!=typeof e)throw new Error(""+t+" must be a string");return e},r.prototype._sanitizeField=function(e){var t;return t=this._sanitizeName(e,"field name"),this.options.autoQuoteFieldNames?""+this.options.nameQuoteCharacter+t+this.options.nameQuoteCharacter:t},r.prototype._sanitizeTable=function(e){var t;return t=this._sanitizeName(e,"table name"),this.options.autoQuoteTableNames?""+this.options.nameQuoteCharacter+t+this.options.nameQuoteCharacter:t},r.prototype._sanitizeAlias=function(e){return this._sanitizeName(e,"alias")},r.prototype._sanitizeLimitOffset=function(e){e=parseInt(e);if(0>e||isNaN(e))throw new Error("limit/offset must be >=0");return e},r.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},r.prototype._formatValue=function(e){return null===e?e="NULL":"boolean"==typeof e?e=e?"TRUE":"FALSE":"number"!=typeof e&&!1===this.options.usingValuePlaceholders&&(e="'"+e+"'"),e},r}(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 s(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 s(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 s(t,e),t.prototype._table=function(e,t){return t==null&&(t=null),e=this._sanitizeTable(e),t&&(t=this._sanitizeAlias(t)),this.options.singleTable&&(this.tables=[]),this.tables.push({name: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+=", "),n+=t.name,t.alias&&(n+=" AS `"+t.alias+"`");return n},t}(e.Block),e.UpdateTableBlock=function(e){function t(){return t.__super__.constructor.apply(this,arguments)}return s(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 s(t,e),t.prototype.from=function(e,t){return t==null&&(t=null),this._table(e,t)},t.prototype.buildStr=function(e){var t,n,r,i,s;if(0>=this.tables.length)throw new Error("from() needs to be called");n="",s=this.tables;for(r=0,i=s.length;r<i;r++)t=s[r],""!==n&&(n+=", "),n+=t.name,t.alias&&(n+=" `"+t.alias+"`");return"FROM "+n},t}(e.AbstractTableBlock),e.IntoTableBlock=function(e){function t(e){t.__super__.constructor.call(this,e),this.table=null}return s(t,e),t.prototype.into=function(e){return this.table=this._sanitizeTable(e)},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 s(t,e),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})},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 s(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,s,o;n=function(){var e,n;e=this.fields,n=[];for(t in e){if(!i.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(s=0,o=n.length;s<o;s++)t=n[s],""!==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 s(t,e),t.prototype.buildStr=function(e){var t,n,r,s,o,u,a;n=function(){var e,t;e=this.fields,t=[];for(s in e){if(!i.call(e,s))continue;t.push(s)}return t}.call(this);if(0>=n.length)throw new Error("set() needs to be called");r="",o="";for(u=0,a=n.length;u<a;u++)t=n[u],""!==r&&(r+=", "),r+=t,""!==o&&(o+=", "),o+=this._formatValue(this.fields[t]);return"("+r+") VALUES ("+o+")"},t}(e.SetFieldBlock),e.DistinctBlock=function(e){function t(e){t.__super__.constructor.call(this,e),this.useDistinct=!1}return s(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 s(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 s(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 s(t,e),t.prototype.where=function(e){e=this._sanitizeCondition(e);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 s(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 s(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 s(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),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.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 "+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 s(t,e),t.prototype.updateOptions=function(e){var t,r,i,s,o;this.options=n({},this.options,e),s=this.blocks,o=[];for(r=0,i=s.length;r<i;r++)t=s[r],o.push(t.options=n({},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}(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(t),new e.JoinBlock(t),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 s(n,t),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 s(n,t),n}(e.QueryBuilder),e.Delete=function(t){function r(t,i){i==null&&(i=null),i||(i=[new e.StringBlock(t,"DELETE"),new e.FromTableBlock(n({},t,{singleTable:!0})),new e.JoinBlock(t),new e.WhereBlock(t),new e.OrderByBlock(t),new e.LimitBlock(t)]),r.__super__.constructor.call(this,t,i)}return s(r,t),r}(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 s(n,t),n}(e.QueryBuilder),t={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)}},t.remove=t["delete"],t.cls=e,(typeof define!="undefined"&&define!==null?define.amd:void 0)?define(function(){return t}):(typeof module!="undefined"&&module!==null?module.exports:void 0)?module.exports=t:typeof window!="undefined"&&window!==null&&(window.squel=t)}).call(this);
+*/(function(){var e,t,n,r=[].slice,i={}.hasOwnProperty,s=function(e,t){function r(){this.constructor=e}for(var n in t)i.call(t,n)&&(e[n]=t[n]);return r.prototype=t.prototype,e.prototype=new r,e.__super__=t.prototype,e};e={},n=function(){var e,t,n,s,o,u,a;e=arguments[0],n=2<=arguments.length?r.call(arguments,1):[];if(n)for(u=0,a=n.length;u<a;u++){s=n[u];if(s)for(t in s){if(!i.call(s,t))continue;o=s[t],e[t]=o}}return e},e.DefaultQueryBuilderOptions={autoQuoteTableNames:!1,autoQuoteFieldNames:!1,nameQuoteCharacter:"`",usingValuePlaceholders:!1},e.Cloneable=function(){function e(){}return e.prototype.clone=function(){var e;return e=new this.constructor,n(e,JSON.parse(JSON.stringify(this)))},e}(),e.BaseBuilder=function(t){function r(t){this.options=n({},e.DefaultQueryBuilderOptions,t)}return s(r,t),r.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},r.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},r.prototype._sanitizeName=function(e,t){if("string"!=typeof e)throw new Error(""+t+" must be a string");return e},r.prototype._sanitizeField=function(e){var t;return t=this._sanitizeName(e,"field name"),this.options.autoQuoteFieldNames?""+this.options.nameQuoteCharacter+t+this.options.nameQuoteCharacter:t},r.prototype._sanitizeTable=function(e){var t;return t=this._sanitizeName(e,"table name"),this.options.autoQuoteTableNames?""+this.options.nameQuoteCharacter+t+this.options.nameQuoteCharacter:t},r.prototype._sanitizeAlias=function(e){return this._sanitizeName(e,"alias")},r.prototype._sanitizeLimitOffset=function(e){e=parseInt(e);if(0>e||isNaN(e))throw new Error("limit/offset must be >=0");return e},r.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},r.prototype._formatValue=function(e){return null===e?e="NULL":"boolean"==typeof e?e=e?"TRUE":"FALSE":"number"!=typeof e&&!1===this.options.usingValuePlaceholders&&(e="'"+e+"'"),e},r}(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 s(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 s(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 s(t,e),t.prototype._table=function(e,t){return t==null&&(t=null),e=this._sanitizeTable(e),t&&(t=this._sanitizeAlias(t)),this.options.singleTable&&(this.tables=[]),this.tables.push({name: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+=", "),n+=t.name,t.alias&&(n+=" AS `"+t.alias+"`");return n},t}(e.Block),e.UpdateTableBlock=function(e){function t(){return t.__super__.constructor.apply(this,arguments)}return s(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 s(t,e),t.prototype.from=function(e,t){return t==null&&(t=null),this._table(e,t)},t.prototype.buildStr=function(e){var t,n,r,i,s;if(0>=this.tables.length)throw new Error("from() needs to be called");n="",s=this.tables;for(r=0,i=s.length;r<i;r++)t=s[r],""!==n&&(n+=", "),n+=t.name,t.alias&&(n+=" `"+t.alias+"`");return"FROM "+n},t}(e.AbstractTableBlock),e.IntoTableBlock=function(e){function t(e){t.__super__.constructor.call(this,e),this.table=null}return s(t,e),t.prototype.into=function(e){return this.table=this._sanitizeTable(e)},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 s(t,e),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})},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 s(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,s,o;n=function(){var e,n;e=this.fields,n=[];for(t in e){if(!i.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(s=0,o=n.length;s<o;s++)t=n[s],""!==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 s(t,e),t.prototype.buildStr=function(e){var t,n,r,s,o,u,a;n=function(){var e,t;e=this.fields,t=[];for(s in e){if(!i.call(e,s))continue;t.push(s)}return t}.call(this);if(0>=n.length)throw new Error("set() needs to be called");r="",o="";for(u=0,a=n.length;u<a;u++)t=n[u],""!==r&&(r+=", "),r+=t,""!==o&&(o+=", "),o+=this._formatValue(this.fields[t]);return"("+r+") VALUES ("+o+")"},t}(e.SetFieldBlock),e.DistinctBlock=function(e){function t(e){t.__super__.constructor.call(this,e),this.useDistinct=!1}return s(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 s(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 s(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 s(t,e),t.prototype.where=function(e){e=this._sanitizeCondition(e);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 s(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 s(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 s(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),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.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 "+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 s(t,e),t.prototype.updateOptions=function(e){var t,r,i,s,o;this.options=n({},this.options,e),s=this.blocks,o=[];for(r=0,i=s.length;r<i;r++)t=s[r],o.push(t.options=n({},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}(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(t),new e.JoinBlock(t),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 s(n,t),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 s(n,t),n}(e.QueryBuilder),e.Delete=function(t){function r(t,i){i==null&&(i=null),i||(i=[new e.StringBlock(t,"DELETE"),new e.FromTableBlock(n({},t,{singleTable:!0})),new e.JoinBlock(t),new e.WhereBlock(t),new e.OrderByBlock(t),new e.LimitBlock(t)]),r.__super__.constructor.call(this,t,i)}return s(r,t),r}(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 s(n,t),n}(e.QueryBuilder),t={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)}},t.remove=t["delete"],t.cls=e,(typeof define!="undefined"&&define!==null?define.amd:void 0)?define(function(){return t}):(typeof module!="undefined"&&module!==null?module.exports:void 0)?module.exports=t:typeof window!="undefined"&&window!==null&&(window.squel=t)}).call(this);
View
11 src/squel.coffee
@@ -85,14 +85,13 @@ class cls.BaseBuilder extends cls.Cloneable
# Sanitize the given condition.
_sanitizeCondition: (condition) ->
- t = typeof condition
- c = @_getObjectClassName(condition)
-
- if 'Expression' isnt c and 'Expression' isnt t and "string" isnt t
- throw new Error "condition must be a string or Expression instance"
# If it's an expression builder instance then convert it to string form.
- if 'Expression' is t or 'Expression' is c
+ if condition instanceof cls.Expression
condition = condition.toString()
+
+ if "string" isnt typeof condition
+ throw new Error "condition must be a string or Expression instance"
+
condition
View
4 test/baseclasses.test.coffee
@@ -122,7 +122,6 @@ test['Builder base class'] =
'empty expression': ->
e = squel.expr()
assert.same "", @inst._sanitizeCondition(e)
- assert.ok @inst._getObjectClassName.calledWithExactly(e)
'non-empty expression': ->
e = squel.expr()
.and("s.name <> 'Fred'")
@@ -131,17 +130,14 @@ test['Builder base class'] =
.or("s.id = 6")
.end()
assert.same "s.name <> 'Fred' OR (s.id = 5 OR s.id = 6)", @inst._sanitizeCondition(e)
- assert.ok @inst._getObjectClassName.calledWithExactly(e)
'if string': ->
s = 'BLA BLA'
assert.same 'BLA BLA', @inst._sanitizeCondition(s)
- assert.ok @inst._getObjectClassName.calledWithExactly(s)
'if neither Expression nor String': ->
testFn = => @inst._sanitizeCondition(1)
assert.throws testFn, 'condition must be a string or Expression instance'
- assert.ok @inst._getObjectClassName.calledWithExactly(1)
'_sanitizeName':
Please sign in to comment.
Something went wrong with that request. Please try again.