Skip to content
Browse files

Added new test for UPDATE query builder.

  • Loading branch information...
1 parent d5d7203 commit 901a2cfab8565a2a3a4288bbef2f00ccdb761a35 @hiddentao committed Nov 2, 2012
Showing with 187 additions and 17 deletions.
  1. +1 −4 docs/squel.html
  2. +0 −6 squel.js
  3. +1 −1 squel.min.js
  4. +0 −4 src/squel.coffee
  5. +0 −2 test/select.test.coffee
  6. +185 −0 test/update.test.coffee
View
5 docs/squel.html
@@ -261,10 +261,7 @@
<p>Note that the query builder does not check the final query string for correctness.</p>
-<p>All the build methods in this object return the object instance for chained method calling purposes.</p> </td> <td class="code"> <div class="highlight"><pre><span class="k">class</span> <span class="nx">Update</span> <span class="k">extends</span> <span class="nx">WhereOrderLimit</span>
- <span class="nv">tables: </span><span class="kc">null</span>
- <span class="nv">fields: </span><span class="kc">null</span>
- <span class="nv">options: </span><span class="kc">null</span></pre></div> </td> </tr> <tr id="section-59"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-59">&#182;</a> </div> <p>options: see DefaultBuilderOptions</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">constructor: </span><span class="nf">(options) -&gt;</span>
+<p>All the build methods in this object return the object instance for chained method calling purposes.</p> </td> <td class="code"> <div class="highlight"><pre><span class="k">class</span> <span class="nx">Update</span> <span class="k">extends</span> <span class="nx">WhereOrderLimit</span></pre></div> </td> </tr> <tr id="section-59"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-59">&#182;</a> </div> <p>options: see DefaultBuilderOptions</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">constructor: </span><span class="nf">(options) -&gt;</span>
<span class="k">super</span>
<span class="vi">@tables = </span><span class="p">[]</span>
<span class="vi">@fields = </span><span class="p">{}</span>
View
6 squel.js
@@ -559,12 +559,6 @@ OTHER DEALINGS IN THE SOFTWARE.
__extends(Update, _super);
- Update.prototype.tables = null;
-
- Update.prototype.fields = null;
-
- Update.prototype.options = null;
-
function Update(options) {
this.toString = __bind(this.toString, this);
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=[].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},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}(),e=t={usingValuePlaceholders:!1},o=function(){function e(){}return e.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},e.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},e.prototype._sanitizeName=function(e,t){if("string"!=typeof e)throw new Error(""+t+" must be a string");return e},e.prototype._sanitizeField=function(e){return this._sanitizeName(e,"field name")},e.prototype._sanitizeTable=function(e){return this._sanitizeName(e,"table name")},e.prototype._sanitizeAlias=function(e){return this._sanitizeName(e,"alias")},e.prototype._sanitizeLimitOffset=function(e){e=parseInt(e);if(0>e||isNaN(e))throw new Error("limit/offset must be >=0");return e},e.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},e.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},e}(),f=function(e){function t(){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.apply(this,arguments),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(){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.apply(this,arguments),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(){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.apply(this,arguments),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 n(e){this.toString=d(this.toString,this),this.set=d(this.set,this),this.table=d(this.table,this),n.__super__.constructor.apply(this,arguments),this.tables=[],this.fields={},this.options=c({},t,e)}return v(n,e),n.prototype.tables=null,n.prototype.fields=null,n.prototype.options=null,n.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},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,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},n}(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){return e=this._sanitizeTable(e),this.table=e,this},t.prototype.toString=function(){var e;if(!this.table)throw new Error("from() needs to be called");return e="DELETE FROM "+this.table,e+=this._joinString(),e+=this._whereString(),e+=this._orderString(),e+=this._limitString(),e},t}(s),i=function(t){function n(t){this.toString=d(this.toString,this),this.set=d(this.set,this),this.into=d(this.into,this),n.__super__.constructor.apply(this,arguments),this.fields={},this.options=c({},e,t)}return v(n,t),n.prototype.table=null,n.prototype.fields=null,n.prototype.options=null,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(!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+")"},n}(o),l={expr:function(){return new r},select:function(){return new u},update:function(e){return new a(e)},insert:function(e){return new i(e)},"delete":function(){return new n},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);
+*/(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},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}(),e=t={usingValuePlaceholders:!1},o=function(){function e(){}return e.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},e.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},e.prototype._sanitizeName=function(e,t){if("string"!=typeof e)throw new Error(""+t+" must be a string");return e},e.prototype._sanitizeField=function(e){return this._sanitizeName(e,"field name")},e.prototype._sanitizeTable=function(e){return this._sanitizeName(e,"table name")},e.prototype._sanitizeAlias=function(e){return this._sanitizeName(e,"alias")},e.prototype._sanitizeLimitOffset=function(e){e=parseInt(e);if(0>e||isNaN(e))throw new Error("limit/offset must be >=0");return e},e.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},e.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},e}(),f=function(e){function t(){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.apply(this,arguments),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(){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.apply(this,arguments),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(){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.apply(this,arguments),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 n(e){this.toString=d(this.toString,this),this.set=d(this.set,this),this.table=d(this.table,this),n.__super__.constructor.apply(this,arguments),this.tables=[],this.fields={},this.options=c({},t,e)}return v(n,e),n.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},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,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},n}(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){return e=this._sanitizeTable(e),this.table=e,this},t.prototype.toString=function(){var e;if(!this.table)throw new Error("from() needs to be called");return e="DELETE FROM "+this.table,e+=this._joinString(),e+=this._whereString(),e+=this._orderString(),e+=this._limitString(),e},t}(s),i=function(t){function n(t){this.toString=d(this.toString,this),this.set=d(this.set,this),this.into=d(this.into,this),n.__super__.constructor.apply(this,arguments),this.fields={},this.options=c({},e,t)}return v(n,t),n.prototype.table=null,n.prototype.fields=null,n.prototype.options=null,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(!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+")"},n}(o),l={expr:function(){return new r},select:function(){return new u},update:function(e){return new a(e)},insert:function(e){return new i(e)},"delete":function(){return new n},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
4 src/squel.coffee
@@ -466,10 +466,6 @@ class Select extends JoinWhereOrderLimit
#
# All the build methods in this object return the object instance for chained method calling purposes.
class Update extends WhereOrderLimit
- tables: null
- fields: null
- options: null
-
# options: see DefaultBuilderOptions
constructor: (options) ->
super
View
2 test/select.test.coffee
@@ -207,8 +207,6 @@ test['SELECT builder'] =
'build query':
beforeEach: ->
- @_joinString = @_whereString = @_orderString = @_limitString = ''
-
test.mocker.spy(@inst, '_joinString')
test.mocker.spy(@inst, '_whereString')
test.mocker.spy(@inst, '_orderString')
View
185 test/update.test.coffee
@@ -0,0 +1,185 @@
+###
+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.
+###
+
+
+{test, assert, expect, should} = require './testbase'
+squel = require "../src/squel"
+
+
+
+test['UPDATE builder'] =
+ beforeEach: ->
+ @inst = squel.update()
+
+ 'instanceof WhereOrderLimit': ->
+ assert.instanceOf @inst, squel.WhereOrderLimit
+
+ 'default field values': ->
+ assert.same [], @inst.tables
+ assert.same {}, @inst.fields
+ assert.same { usingValuePlaceholders: false }, @inst.options
+
+ 'constructor':
+ 'override options': ->
+ @inst = squel.update
+ usingValuePlaceholders: true
+ dummy: true
+
+ assert.same [], @inst.tables
+ assert.same {}, @inst.fields
+ assert.same { usingValuePlaceholders: true, dummy: true }, @inst.options
+
+
+ '>> table()':
+ beforeEach: ->
+ test.mocker.spy(@inst, '_sanitizeTable')
+ test.mocker.spy(@inst, '_sanitizeAlias')
+
+ 'args: ()': ->
+ assert.throws (=> @inst.table()), 'table name must be a string'
+ assert.ok @inst._sanitizeTable.calledWithExactly(undefined)
+
+ 'args: (table)':
+ beforeEach: ->
+ @ret = @inst.table('table')
+
+ 'update internal state': ->
+ assert.same @ret, @inst
+ assert.same @inst.tables, [
+ {
+ name: 'table'
+ alias: null
+ }
+ ]
+
+ assert.ok @inst._sanitizeTable.calledWithExactly('table')
+ assert.ok @inst._sanitizeAlias.notCalled
+
+ '>> args(table2)': ->
+ assert.same @inst.table('table2'), @inst
+ assert.same @inst.tables, [
+ {
+ name: 'table'
+ alias: null
+ }
+ {
+ name: 'table2'
+ alias: null
+ }
+ ]
+
+ 'args: (table, alias)': ->
+ @inst.table('table', 'alias')
+
+ assert.same @inst.tables, [
+ {
+ name: 'table'
+ alias: 'alias'
+ }
+ ]
+
+ assert.ok @inst._sanitizeTable.calledWithExactly('table')
+ assert.ok @inst._sanitizeAlias.calledWithExactly('alias')
+
+
+ '>> set()':
+ beforeEach: ->
+ test.mocker.spy(@inst, '_sanitizeField')
+ test.mocker.spy(@inst, '_sanitizeValue')
+
+ 'args: ()': ->
+ assert.throws (=> @inst.set()), 'field name must be a string'
+ assert.ok @inst._sanitizeField.calledWithExactly(undefined)
+
+ 'args: (field)': ->
+ assert.throws (=> @inst.set('field')), 'field value must be a string, number, boolean or null'
+ assert.ok @inst._sanitizeField.calledWithExactly('field')
+ assert.ok @inst._sanitizeValue.calledWithExactly(undefined)
+
+ 'args: (field, null)':
+ beforeEach: ->
+ @ret = @inst.set('field', null)
+
+ 'update internal state': ->
+ assert.same @ret, @inst
+ assert.same @inst.fields, { 'field': null }
+ assert.ok @inst._sanitizeField.calledWithExactly('field')
+ assert.ok @inst._sanitizeValue.calledWithExactly(null)
+
+ '>> args: (field, 1)':
+ beforeEach: ->
+ @ret = @inst.set('field', 1)
+
+ 'update internal state': ->
+ assert.same @ret, @inst
+ assert.same @inst.fields, { 'field': 1 }
+ assert.ok @inst._sanitizeField.calledWithExactly('field')
+ assert.ok @inst._sanitizeValue.calledWithExactly(1)
+
+
+ 'build query':
+ beforeEach: ->
+ test.mocker.spy(@inst, '_whereString')
+ test.mocker.spy(@inst, '_orderString')
+ test.mocker.spy(@inst, '_limitString')
+
+ 'need to call table() first': ->
+ assert.throws (=> @inst.toString()), 'table() needs to be called'
+
+ 'need to call set() first': ->
+ @inst.table('table')
+ assert.throws (=> @inst.toString()), 'set() needs to be called'
+
+ '>> from(table, t1).set(field, 1)':
+ beforeEach: -> @inst.table('table', 't1').set('field', 1)
+ toString: ->
+ assert.same @inst.toString(), 'UPDATE table AS `t1` SET field = 1'
+ assert.ok @inst._whereString.calledOnce
+ assert.ok @inst._orderString.calledOnce
+ assert.ok @inst._limitString.calledOnce
+
+ '>> table(table2) >> set(field2, 2)':
+ beforeEach: -> @inst.table('table2').set('field2', 2)
+ toString: ->
+ assert.same @inst.toString(), 'UPDATE table AS `t1`, table2 SET field = 1, field2 = 2'
+
+ '>> where(a = 1)':
+ beforeEach: -> @inst.where('a = 1')
+ toString: ->
+ assert.same @inst.toString(), 'UPDATE table AS `t1`, table2 SET field = 1, field2 = 2 WHERE (a = 1)'
+
+ '>> order(a, true)':
+ beforeEach: -> @inst.order('a', true)
+ toString: ->
+ assert.same @inst.toString(), 'UPDATE table AS `t1`, table2 SET field = 1, field2 = 2 WHERE (a = 1) ORDER BY a ASC'
+
+ '>> limit(2)':
+ beforeEach: -> @inst.limit(2)
+ toString: ->
+ assert.same @inst.toString(), 'UPDATE table AS `t1`, table2 SET field = 1, field2 = 2 WHERE (a = 1) ORDER BY a ASC LIMIT 2'
+
+
+
+module?.exports[require('path').basename(__filename)] = test

0 comments on commit 901a2cf

Please sign in to comment.
Something went wrong with that request. Please try again.