Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added INSERT builder tests.

Fixed Base Query builders tests.
  • Loading branch information...
commit 5050a427555f627c67bc3822fa56aa13d4eed49a 1 parent 9d61eaa
@hiddentao authored
View
6 docs/squel.html
@@ -312,11 +312,9 @@
<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">Insert</span> <span class="k">extends</span> <span class="nx">QueryBuilder</span>
- <span class="nv">table: </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-78"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-78">&#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">Insert</span> <span class="k">extends</span> <span class="nx">QueryBuilder</span></pre></div> </td> </tr> <tr id="section-78"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-78">&#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">@table = </span><span class="kc">null</span>
<span class="vi">@fields = </span><span class="p">{}</span>
<span class="vi">@options = </span><span class="nx">_extend</span> <span class="p">{},</span> <span class="nx">DefaultInsertBuilderOptions</span><span class="p">,</span> <span class="nx">options</span></pre></div> </td> </tr> <tr id="section-79"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-79">&#182;</a> </div> <p>The table to insert into.
This will override any previously set value.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">into: </span><span class="p">(</span><span class="nx">table</span><span class="p">)</span> <span class="o">=&gt;</span>
View
7 squel.js
@@ -684,12 +684,6 @@ OTHER DEALINGS IN THE SOFTWARE.
__extends(Insert, _super);
- Insert.prototype.table = null;
-
- Insert.prototype.fields = null;
-
- Insert.prototype.options = null;
-
function Insert(options) {
this.toString = __bind(this.toString, this);
@@ -697,6 +691,7 @@ OTHER DEALINGS IN THE SOFTWARE.
this.into = __bind(this.into, this);
Insert.__super__.constructor.apply(this, arguments);
+ this.table = null;
this.fields = {};
this.options = _extend({}, DefaultInsertBuilderOptions, options);
}
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.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.table=null,this.fields={},this.options=c({},e,t)}return v(n,t),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
5 src/squel.coffee
@@ -578,13 +578,10 @@ class Delete extends JoinWhereOrderLimit
#
# All the build methods in this object return the object instance for chained method calling purposes.
class Insert extends QueryBuilder
- table: null
- fields: null
- options: null
-
# options: see DefaultBuilderOptions
constructor: (options) ->
super
+ @table = null
@fields = {}
@options = _extend {}, DefaultInsertBuilderOptions, options
View
30 test/BaseQueryBuilders.test.coffee
@@ -322,42 +322,42 @@ test['WhereOrderLimit base class'] =
assert.same 2, @inst.limits
- '>> whereString()':
+ '>> _whereString()':
'no clauses': ->
@inst.wheres = []
- assert.same @inst.whereString(), ""
+ assert.same @inst._whereString(), ""
'1 clause': ->
@inst.wheres = ['a']
- assert.same @inst.whereString(), " WHERE (a)"
+ assert.same @inst._whereString(), " WHERE (a)"
'>1 clauses': ->
@inst.wheres = ['a', 'b']
- assert.same @inst.whereString(), " WHERE (a) AND (b)"
+ assert.same @inst._whereString(), " WHERE (a) AND (b)"
- '>> orderString()':
+ '>> _orderString()':
'no clauses': ->
@inst.orders = []
- assert.same @inst.orderString(), ""
+ assert.same @inst._orderString(), ""
'1 clause': ->
@inst.orders = [{ field: 'a', dir: 'ASC' }]
- assert.same @inst.orderString(), " ORDER BY a ASC"
+ assert.same @inst._orderString(), " ORDER BY a ASC"
'>1 clauses': ->
@inst.orders = [{ field: 'a', dir: 'ASC' }, { field: 'b', dir: 'DESC' }]
- assert.same @inst.orderString(), " ORDER BY a ASC, b DESC"
+ assert.same @inst._orderString(), " ORDER BY a ASC, b DESC"
- '>> limitString()':
+ '>> _limitString()':
'not set': ->
@inst.limits = null
- assert.same @inst.limitString(), ""
+ assert.same @inst._limitString(), ""
'set': ->
@inst.limits = 2
- assert.same @inst.limitString(), " LIMIT 2"
+ assert.same @inst._limitString(), " LIMIT 2"
@@ -507,21 +507,21 @@ test['JoinWhereOrderLimit base class'] =
assert.ok @inst.join.calledWithExactly('table', 'alias', 'condition', 'OUTER')
- '>> joinString()':
+ '>> _joinString()':
beforeEach: -> @inst.joins = []
'no joins': ->
- assert.same @inst.joinString(), ""
+ assert.same @inst._joinString(), ""
'1 join': ->
@inst.left_join('table')
- assert.same @inst.joinString(), " LEFT JOIN table"
+ assert.same @inst._joinString(), " LEFT JOIN table"
'>1 joins': ->
@inst.left_join('table')
@inst.right_join('table2', 'a2')
@inst.join('table3', null, 'c3')
- assert.same @inst.joinString(), " LEFT JOIN table RIGHT JOIN table2 `a2` INNER JOIN table3 ON (c3)"
+ assert.same @inst._joinString(), " LEFT JOIN table RIGHT JOIN table2 `a2` INNER JOIN table3 ON (c3)"
View
153 test/insert.test.coffee
@@ -0,0 +1,153 @@
+###
+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['INSERT builder'] =
+ beforeEach: ->
+ @inst = squel.insert()
+
+ 'instanceof QueryBuilder': ->
+ assert.instanceOf @inst, squel.QueryBuilder
+
+ 'default field values': ->
+ assert.same null, @inst.table
+ assert.same {}, @inst.fields
+ assert.same { usingValuePlaceholders: false }, @inst.options
+
+ 'constructor':
+ 'override options': ->
+ @inst = squel.insert
+ usingValuePlaceholders: true
+ dummy: true
+
+ assert.same null, @inst.table
+ assert.same {}, @inst.fields
+ assert.same { usingValuePlaceholders: true, dummy: true }, @inst.options
+
+
+ '>> into()':
+ beforeEach: ->
+ test.mocker.spy(@inst, '_sanitizeTable')
+
+ 'args: ()': ->
+ assert.throws (=> @inst.into()), 'table name must be a string'
+ assert.ok @inst._sanitizeTable.calledWithExactly(undefined)
+
+ 'args: (table)':
+ beforeEach: ->
+ @ret = @inst.into('table')
+
+ 'update internal state': ->
+ assert.same @ret, @inst
+ assert.same @inst.table, 'table'
+
+ assert.ok @inst._sanitizeTable.calledWithExactly('table')
+
+ '>> args(table2)': ->
+ assert.same @inst.into('table2'), @inst
+ assert.same @inst.table, 'table2'
+
+
+ '>> 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':
+ 'need to call into() first': ->
+ assert.throws (=> @inst.toString()), 'into() needs to be called'
+
+ 'need to call set() first': ->
+ @inst.into('table')
+ assert.throws (=> @inst.toString()), 'set() needs to be called'
+
+ '>> into(table).set(field, 1)':
+ beforeEach: -> @inst.into('table').set('field', 1)
+ toString: ->
+ assert.same @inst.toString(), 'INSERT INTO table (field) VALUES (1)'
+
+ '>> set(field2, 1.2)':
+ beforeEach: -> @inst.set('field2', 1.2)
+ toString: ->
+ assert.same @inst.toString(), 'INSERT INTO table (field, field2) VALUES (1, 1.2)'
+
+ '>> set(field2, "str")':
+ beforeEach: -> @inst.set('field2', 'str')
+ toString: ->
+ assert.same @inst.toString(), 'INSERT INTO table (field, field2) VALUES (1, \'str\')'
+
+ 'and when using value placeholders': ->
+ @inst.options.usingValuePlaceholders = true
+ @inst.set('field2', 'str')
+ assert.same @inst.toString(), 'INSERT INTO table (field, field2) VALUES (1, str)'
+
+ '>> set(field2, true)':
+ beforeEach: -> @inst.set('field2', true)
+ toString: ->
+ assert.same @inst.toString(), 'INSERT INTO table (field, field2) VALUES (1, TRUE)'
+
+ '>> set(field2, null)':
+ beforeEach: -> @inst.set('field2', null)
+ toString: ->
+ assert.same @inst.toString(), 'INSERT INTO table (field, field2) VALUES (1, NULL)'
+
+
+
+module?.exports[require('path').basename(__filename)] = test
View
49 test/update.test.coffee
@@ -160,25 +160,50 @@ test['UPDATE builder'] =
assert.ok @inst._orderString.calledOnce
assert.ok @inst._limitString.calledOnce
- '>> table(table2) >> set(field2, 2)':
- beforeEach: -> @inst.table('table2').set('field2', 2)
+ '>> set(field2, 1.2)':
+ beforeEach: -> @inst.set('field2', 1.2)
toString: ->
- assert.same @inst.toString(), 'UPDATE table AS `t1`, table2 SET field = 1, field2 = 2'
+ assert.same @inst.toString(), 'UPDATE table AS `t1` SET field = 1, field2 = 1.2'
- '>> where(a = 1)':
- beforeEach: -> @inst.where('a = 1')
+ '>> set(field2, true)':
+ beforeEach: -> @inst.set('field2', true)
+ toString: ->
+ assert.same @inst.toString(), 'UPDATE table AS `t1` SET field = 1, field2 = TRUE'
+
+ '>> set(field2, "str")':
+ beforeEach: -> @inst.set('field2', 'str')
+ toString: ->
+ assert.same @inst.toString(), 'UPDATE table AS `t1` SET field = 1, field2 = \'str\''
+
+ 'and when using value placeholders': ->
+ @inst.options.usingValuePlaceholders = true
+ @inst.set('field2', 'str')
+ assert.same @inst.toString(), 'UPDATE table AS `t1` SET field = 1, field2 = str'
+
+ '>> set(field2, null)':
+ beforeEach: -> @inst.set('field2', null)
+ toString: ->
+ assert.same @inst.toString(), 'UPDATE table AS `t1` SET field = 1, field2 = NULL'
+
+ '>> table(table2)':
+ beforeEach: -> @inst.table('table2')
toString: ->
- assert.same @inst.toString(), 'UPDATE table AS `t1`, table2 SET field = 1, field2 = 2 WHERE (a = 1)'
+ assert.same @inst.toString(), 'UPDATE table AS `t1`, table2 SET field = 1, field2 = NULL'
- '>> order(a, true)':
- beforeEach: -> @inst.order('a', true)
+ '>> 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 BY a ASC'
+ assert.same @inst.toString(), 'UPDATE table AS `t1`, table2 SET field = 1, field2 = NULL WHERE (a = 1)'
- '>> limit(2)':
- beforeEach: -> @inst.limit(2)
+ '>> 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'
+ assert.same @inst.toString(), 'UPDATE table AS `t1`, table2 SET field = 1, field2 = NULL 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 = NULL WHERE (a = 1) ORDER BY a ASC LIMIT 2'
Please sign in to comment.
Something went wrong with that request. Please try again.