Skip to content
Browse files

Got clone()-ing working again.

  • Loading branch information...
1 parent 3e0c684 commit 419a366ce94ad8e2b060a5d6e6c7ff5c3ef1e62c @hiddentao committed Mar 27, 2013
View
3 README.md
@@ -10,7 +10,8 @@ A flexible and powerful SQL query string builder for Javascript.
* Supports the standard SQL queries: SELECT, UPDATE, INSERT and DELETE.
* Can be customized to support non-standard queries.
* Uses method chaining for ease of use.
-* Well tested (~230 tests).
+* Well tested (~240 tests).
+* Small: ~3.6 KB minified and gzipped
## Installation
View
40 docs/squel.html
@@ -431,9 +431,10 @@
<span class="vi">@options = </span><span class="nx">_extend</span><span class="p">({},</span> <span class="nx">@options</span><span class="p">,</span> <span class="nx">options</span><span class="p">)</span>
<span class="k">for</span> <span class="nx">block</span> <span class="k">in</span> <span class="nx">@blocks</span>
<span class="nv">block.options = </span><span class="nx">_extend</span><span class="p">({},</span> <span class="nx">block</span><span class="p">.</span><span class="nx">options</span><span class="p">,</span> <span class="nx">options</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-77"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-77">&#182;</a> </div> <p>Get the final fully constructed query string.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">toString: </span><span class="o">-&gt;</span>
- <span class="p">(</span><span class="nx">block</span><span class="p">.</span><span class="nx">buildStr</span><span class="p">(</span><span class="nx">@</span><span class="p">)</span> <span class="k">for</span> <span class="nx">block</span> <span class="k">in</span> <span class="nx">@blocks</span><span class="p">).</span><span class="nx">filter</span><span class="p">(</span> <span class="nf">(v) -&gt;</span> <span class="k">return</span> <span class="p">(</span><span class="mi">0</span> <span class="o">&lt;</span> <span class="nx">v</span><span class="p">.</span><span class="nx">length</span><span class="p">)).</span><span class="nx">join</span><span class="p">(</span><span class="s">&#39; &#39;</span><span class="p">)</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>SELECT query builder.</p> </td> <td class="code"> <div class="highlight"><pre><span class="k">class</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">Select</span> <span class="k">extends</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">QueryBuilder</span>
- <span class="nv">constructor: </span><span class="nf">(options) -&gt;</span>
- <span class="nv">blocks = </span><span class="p">[</span>
+ <span class="p">(</span><span class="nx">block</span><span class="p">.</span><span class="nx">buildStr</span><span class="p">(</span><span class="nx">@</span><span class="p">)</span> <span class="k">for</span> <span class="nx">block</span> <span class="k">in</span> <span class="nx">@blocks</span><span class="p">).</span><span class="nx">filter</span><span class="p">(</span> <span class="nf">(v) -&gt;</span> <span class="k">return</span> <span class="p">(</span><span class="mi">0</span> <span class="o">&lt;</span> <span class="nx">v</span><span class="p">.</span><span class="nx">length</span><span class="p">)).</span><span class="nx">join</span><span class="p">(</span><span class="s">&#39; &#39;</span><span class="p">)</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>Deep clone</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">clone: </span><span class="o">-&gt;</span>
+ <span class="k">new</span> <span class="nx">@constructor</span> <span class="nx">@options</span><span class="p">,</span> <span class="p">(</span><span class="nx">block</span><span class="p">.</span><span class="nx">clone</span><span class="p">()</span> <span class="k">for</span> <span class="nx">block</span> <span class="k">in</span> <span class="nx">@blocks</span><span class="p">)</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>SELECT query builder.</p> </td> <td class="code"> <div class="highlight"><pre><span class="k">class</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">Select</span> <span class="k">extends</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">QueryBuilder</span>
+ <span class="nv">constructor: </span><span class="nf">(options, blocks = null) -&gt;</span>
+ <span class="nx">blocks</span> <span class="o">or=</span> <span class="p">[</span>
<span class="k">new</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">StringBlock</span><span class="p">(</span><span class="nx">options</span><span class="p">,</span> <span class="s">&#39;SELECT&#39;</span><span class="p">),</span>
<span class="k">new</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">DistinctBlock</span><span class="p">(</span><span class="nx">options</span><span class="p">),</span>
<span class="k">new</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">GetFieldBlock</span><span class="p">(</span><span class="nx">options</span><span class="p">),</span>
@@ -446,9 +447,9 @@
<span class="k">new</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">OffsetBlock</span><span class="p">(</span><span class="nx">options</span><span class="p">)</span>
<span class="p">]</span>
- <span class="k">super</span> <span class="nx">options</span><span class="p">,</span> <span class="nx">blocks</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>UPDATE query builder.</p> </td> <td class="code"> <div class="highlight"><pre><span class="k">class</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">Update</span> <span class="k">extends</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">QueryBuilder</span>
- <span class="nv">constructor: </span><span class="nf">(options) -&gt;</span>
- <span class="nv">blocks = </span><span class="p">[</span>
+ <span class="k">super</span> <span class="nx">options</span><span class="p">,</span> <span class="nx">blocks</span></pre></div> </td> </tr> <tr id="section-80"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-80">&#182;</a> </div> <p>UPDATE query builder.</p> </td> <td class="code"> <div class="highlight"><pre><span class="k">class</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">Update</span> <span class="k">extends</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">QueryBuilder</span>
+ <span class="nv">constructor: </span><span class="nf">(options, blocks = null) -&gt;</span>
+ <span class="nx">blocks</span> <span class="o">or=</span> <span class="p">[</span>
<span class="k">new</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">StringBlock</span><span class="p">(</span><span class="nx">options</span><span class="p">,</span> <span class="s">&#39;UPDATE&#39;</span><span class="p">),</span>
<span class="k">new</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">UpdateTableBlock</span><span class="p">(</span><span class="nx">options</span><span class="p">),</span>
<span class="k">new</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">SetFieldBlock</span><span class="p">(</span><span class="nx">options</span><span class="p">),</span>
@@ -457,9 +458,9 @@
<span class="k">new</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">LimitBlock</span><span class="p">(</span><span class="nx">options</span><span class="p">)</span>
<span class="p">]</span>
- <span class="k">super</span> <span class="nx">options</span><span class="p">,</span> <span class="nx">blocks</span></pre></div> </td> </tr> <tr id="section-80"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-80">&#182;</a> </div> <p>DELETE query builder.</p> </td> <td class="code"> <div class="highlight"><pre><span class="k">class</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">Delete</span> <span class="k">extends</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">QueryBuilder</span>
- <span class="nv">constructor: </span><span class="nf">(options) -&gt;</span>
- <span class="nv">blocks = </span><span class="p">[</span>
+ <span class="k">super</span> <span class="nx">options</span><span class="p">,</span> <span class="nx">blocks</span></pre></div> </td> </tr> <tr id="section-81"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-81">&#182;</a> </div> <p>DELETE query builder.</p> </td> <td class="code"> <div class="highlight"><pre><span class="k">class</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">Delete</span> <span class="k">extends</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">QueryBuilder</span>
+ <span class="nv">constructor: </span><span class="nf">(options, blocks = null) -&gt;</span>
+ <span class="nx">blocks</span> <span class="o">or=</span> <span class="p">[</span>
<span class="k">new</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">StringBlock</span><span class="p">(</span><span class="nx">options</span><span class="p">,</span> <span class="s">&#39;DELETE&#39;</span><span class="p">),</span>
<span class="k">new</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">FromTableBlock</span><span class="p">(</span> <span class="nx">_extend</span><span class="p">({},</span> <span class="nx">options</span><span class="p">,</span> <span class="p">{</span> <span class="nv">singleTable: </span><span class="kc">true</span> <span class="p">})</span> <span class="p">),</span>
<span class="k">new</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">JoinBlock</span><span class="p">(</span><span class="nx">options</span><span class="p">),</span>
@@ -468,27 +469,26 @@
<span class="k">new</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">LimitBlock</span><span class="p">(</span><span class="nx">options</span><span class="p">),</span>
<span class="p">]</span>
- <span class="k">super</span> <span class="nx">options</span><span class="p">,</span> <span class="nx">blocks</span></pre></div> </td> </tr> <tr id="section-81"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-81">&#182;</a> </div> <p>An INSERT query builder.</p> </td> <td class="code"> <div class="highlight"><pre><span class="k">class</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">Insert</span> <span class="k">extends</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">QueryBuilder</span>
- <span class="nv">constructor: </span><span class="nf">(options) -&gt;</span>
- <span class="nv">blocks = </span><span class="p">[</span>
+ <span class="k">super</span> <span class="nx">options</span><span class="p">,</span> <span class="nx">blocks</span></pre></div> </td> </tr> <tr id="section-82"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-82">&#182;</a> </div> <p>An INSERT query builder.</p> </td> <td class="code"> <div class="highlight"><pre><span class="k">class</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">Insert</span> <span class="k">extends</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">QueryBuilder</span>
+ <span class="nv">constructor: </span><span class="nf">(options, blocks = null) -&gt;</span>
+ <span class="nx">blocks</span> <span class="o">or=</span> <span class="p">[</span>
<span class="k">new</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">StringBlock</span><span class="p">(</span><span class="nx">options</span><span class="p">,</span> <span class="s">&#39;INSERT&#39;</span><span class="p">),</span>
<span class="k">new</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">IntoTableBlock</span><span class="p">(</span><span class="nx">options</span><span class="p">),</span>
<span class="k">new</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">InsertFieldValueBlock</span><span class="p">(</span><span class="nx">options</span><span class="p">)</span>
<span class="p">]</span>
- <span class="k">super</span> <span class="nx">options</span><span class="p">,</span> <span class="nx">blocks</span></pre></div> </td> </tr> <tr id="section-82"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-82">&#182;</a> </div> <h2>---------------------------------------------------------------------------------------------------------</h2>
+ <span class="k">super</span> <span class="nx">options</span><span class="p">,</span> <span class="nx">blocks</span></pre></div> </td> </tr> <tr id="section-83"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-83">&#182;</a> </div> <h2>---------------------------------------------------------------------------------------------------------</h2>
<h2>Exported API</h2>
<hr /> </td> <td class="code"> <div class="highlight"><pre><span class="nv">squel =</span>
- <span class="nv">expr: </span><span class="o">-&gt;</span> <span class="k">new</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">Expression</span>
- <span class="nv">select: </span><span class="nf">(options) -&gt;</span> <span class="k">new</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">Select</span><span class="p">(</span><span class="nx">options</span><span class="p">)</span>
- <span class="nv">update: </span><span class="nf">(options) -&gt;</span> <span class="k">new</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">Update</span><span class="p">(</span><span class="nx">options</span><span class="p">)</span>
- <span class="nv">insert: </span><span class="nf">(options) -&gt;</span> <span class="k">new</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">Insert</span><span class="p">(</span><span class="nx">options</span><span class="p">)</span>
- <span class="k">delete</span><span class="o">:</span> <span class="nf">(options) -&gt;</span> <span class="k">new</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">Delete</span><span class="p">(</span><span class="nx">options</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-83"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-83">&#182;</a> </div> <p>defaults</p> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-84"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-84">&#182;</a> </div> <p>aliases</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">squel.remove = </span><span class="nx">squel</span><span class="p">.</span><span class="k">delete</span></pre></div> </td> </tr> <tr id="section-85"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-85">&#182;</a> </div> <p>classes</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">squel.cls = </span><span class="nx">cls</span></pre></div> </td> </tr> <tr id="section-86"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-86">&#182;</a> </div> <p>AMD</p> </td> <td class="code"> <div class="highlight"><pre><span class="k">if</span> <span class="nx">define</span><span class="o">?</span><span class="p">.</span><span class="nx">amd</span>
+ <span class="nv">expr: </span><span class="o">-&gt;</span> <span class="k">new</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">Expression</span></pre></div> </td> </tr> <tr id="section-84"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-84">&#182;</a> </div> <p>Don't have a space-efficient elegant-way of .apply()-ing to constructors, so we specify the args</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">select: </span><span class="nf">(options, blocks) -&gt;</span> <span class="k">new</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">Select</span><span class="p">(</span><span class="nx">options</span><span class="p">,</span> <span class="nx">blocks</span><span class="p">)</span>
+ <span class="nv">update: </span><span class="nf">(options, blocks) -&gt;</span> <span class="k">new</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">Update</span><span class="p">(</span><span class="nx">options</span><span class="p">,</span> <span class="nx">blocks</span><span class="p">)</span>
+ <span class="nv">insert: </span><span class="nf">(options, blocks) -&gt;</span> <span class="k">new</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">Insert</span><span class="p">(</span><span class="nx">options</span><span class="p">,</span> <span class="nx">blocks</span><span class="p">)</span>
+ <span class="k">delete</span><span class="o">:</span> <span class="nf">(options, blocks) -&gt;</span> <span class="k">new</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">Delete</span><span class="p">(</span><span class="nx">options</span><span class="p">,</span> <span class="nx">blocks</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-85"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-85">&#182;</a> </div> <p>aliases</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">squel.remove = </span><span class="nx">squel</span><span class="p">.</span><span class="k">delete</span></pre></div> </td> </tr> <tr id="section-86"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-86">&#182;</a> </div> <p>classes</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">squel.cls = </span><span class="nx">cls</span></pre></div> </td> </tr> <tr id="section-87"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-87">&#182;</a> </div> <p>AMD</p> </td> <td class="code"> <div class="highlight"><pre><span class="k">if</span> <span class="nx">define</span><span class="o">?</span><span class="p">.</span><span class="nx">amd</span>
<span class="nx">define</span> <span class="o">-&gt;</span>
- <span class="k">return</span> <span class="nx">squel</span></pre></div> </td> </tr> <tr id="section-87"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-87">&#182;</a> </div> <p>CommonJS</p> </td> <td class="code"> <div class="highlight"><pre><span class="k">else</span> <span class="k">if</span> <span class="nx">module</span><span class="o">?</span><span class="p">.</span><span class="nx">exports</span>
- <span class="nv">module.exports = </span><span class="nx">squel</span></pre></div> </td> </tr> <tr id="section-88"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-88">&#182;</a> </div> <p>Browser</p> </td> <td class="code"> <div class="highlight"><pre><span class="k">else</span>
+ <span class="k">return</span> <span class="nx">squel</span></pre></div> </td> </tr> <tr id="section-88"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-88">&#182;</a> </div> <p>CommonJS</p> </td> <td class="code"> <div class="highlight"><pre><span class="k">else</span> <span class="k">if</span> <span class="nx">module</span><span class="o">?</span><span class="p">.</span><span class="nx">exports</span>
+ <span class="nv">module.exports = </span><span class="nx">squel</span></pre></div> </td> </tr> <tr id="section-89"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-89">&#182;</a> </div> <p>Browser</p> </td> <td class="code"> <div class="highlight"><pre><span class="k">else</span>
<span class="nb">window</span><span class="o">?</span><span class="p">.</span><span class="nv">squel = </span><span class="nx">squel</span>
</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>
View
64 squel.js
@@ -913,6 +913,20 @@ OTHER DEALINGS IN THE SOFTWARE.
}).join(' ');
};
+ QueryBuilder.prototype.clone = function() {
+ var block;
+ return new this.constructor(this.options, (function() {
+ var _i, _len, _ref, _results;
+ _ref = this.blocks;
+ _results = [];
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
+ block = _ref[_i];
+ _results.push(block.clone());
+ }
+ return _results;
+ }).call(this));
+ };
+
return QueryBuilder;
})(cls.BaseBuilder);
@@ -921,9 +935,11 @@ OTHER DEALINGS IN THE SOFTWARE.
__extends(Select, _super);
- function Select(options) {
- var blocks;
- blocks = [new cls.StringBlock(options, 'SELECT'), new cls.DistinctBlock(options), new cls.GetFieldBlock(options), new cls.FromTableBlock(options), new cls.JoinBlock(options), new cls.WhereBlock(options), new cls.GroupByBlock(options), new cls.OrderByBlock(options), new cls.LimitBlock(options), new cls.OffsetBlock(options)];
+ function Select(options, blocks) {
+ if (blocks == null) {
+ blocks = null;
+ }
+ blocks || (blocks = [new cls.StringBlock(options, 'SELECT'), new cls.DistinctBlock(options), new cls.GetFieldBlock(options), new cls.FromTableBlock(options), new cls.JoinBlock(options), new cls.WhereBlock(options), new cls.GroupByBlock(options), new cls.OrderByBlock(options), new cls.LimitBlock(options), new cls.OffsetBlock(options)]);
Select.__super__.constructor.call(this, options, blocks);
}
@@ -935,9 +951,11 @@ OTHER DEALINGS IN THE SOFTWARE.
__extends(Update, _super);
- function Update(options) {
- var blocks;
- blocks = [new cls.StringBlock(options, 'UPDATE'), new cls.UpdateTableBlock(options), new cls.SetFieldBlock(options), new cls.WhereBlock(options), new cls.OrderByBlock(options), new cls.LimitBlock(options)];
+ function Update(options, blocks) {
+ if (blocks == null) {
+ blocks = null;
+ }
+ blocks || (blocks = [new cls.StringBlock(options, 'UPDATE'), new cls.UpdateTableBlock(options), new cls.SetFieldBlock(options), new cls.WhereBlock(options), new cls.OrderByBlock(options), new cls.LimitBlock(options)]);
Update.__super__.constructor.call(this, options, blocks);
}
@@ -949,13 +967,15 @@ OTHER DEALINGS IN THE SOFTWARE.
__extends(Delete, _super);
- function Delete(options) {
- var blocks;
- blocks = [
+ function Delete(options, blocks) {
+ if (blocks == null) {
+ blocks = null;
+ }
+ blocks || (blocks = [
new cls.StringBlock(options, 'DELETE'), new cls.FromTableBlock(_extend({}, options, {
singleTable: true
})), new cls.JoinBlock(options), new cls.WhereBlock(options), new cls.OrderByBlock(options), new cls.LimitBlock(options)
- ];
+ ]);
Delete.__super__.constructor.call(this, options, blocks);
}
@@ -967,9 +987,11 @@ OTHER DEALINGS IN THE SOFTWARE.
__extends(Insert, _super);
- function Insert(options) {
- var blocks;
- blocks = [new cls.StringBlock(options, 'INSERT'), new cls.IntoTableBlock(options), new cls.InsertFieldValueBlock(options)];
+ function Insert(options, blocks) {
+ if (blocks == null) {
+ blocks = null;
+ }
+ blocks || (blocks = [new cls.StringBlock(options, 'INSERT'), new cls.IntoTableBlock(options), new cls.InsertFieldValueBlock(options)]);
Insert.__super__.constructor.call(this, options, blocks);
}
@@ -981,17 +1003,17 @@ OTHER DEALINGS IN THE SOFTWARE.
expr: function() {
return new cls.Expression;
},
- select: function(options) {
- return new cls.Select(options);
+ select: function(options, blocks) {
+ return new cls.Select(options, blocks);
},
- update: function(options) {
- return new cls.Update(options);
+ update: function(options, blocks) {
+ return new cls.Update(options, blocks);
},
- insert: function(options) {
- return new cls.Insert(options);
+ insert: function(options, blocks) {
+ return new cls.Insert(options, blocks);
},
- "delete": function(options) {
- return new cls.Delete(options);
+ "delete": function(options, blocks) {
+ return new cls.Delete(options, blocks);
}
};
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&&"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}(e.BaseBuilder),e.Select=function(t){function n(t){var 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){var 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){var 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){var 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){return new e.Select(t)},update:function(t){return new e.Update(t)},insert:function(t){return new e.Insert(t)},"delete":function(t){return new e.Delete(t)}},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(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},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
0 src/blocks.coffee
No changes.
View
33 src/squel.coffee
@@ -705,11 +705,15 @@ class cls.QueryBuilder extends cls.BaseBuilder
block.options = _extend({}, block.options, options)
-
# Get the final fully constructed query string.
toString: ->
(block.buildStr(@) for block in @blocks).filter( (v) -> return (0 < v.length)).join(' ')
+ # Deep clone
+ clone: ->
+ new @constructor @options, (block.clone() for block in @blocks)
+
+
@@ -719,8 +723,8 @@ class cls.QueryBuilder extends cls.BaseBuilder
# SELECT query builder.
class cls.Select extends cls.QueryBuilder
- constructor: (options) ->
- blocks = [
+ constructor: (options, blocks = null) ->
+ blocks or= [
new cls.StringBlock(options, 'SELECT'),
new cls.DistinctBlock(options),
new cls.GetFieldBlock(options),
@@ -740,8 +744,8 @@ class cls.Select extends cls.QueryBuilder
# UPDATE query builder.
class cls.Update extends cls.QueryBuilder
- constructor: (options) ->
- blocks = [
+ constructor: (options, blocks = null) ->
+ blocks or= [
new cls.StringBlock(options, 'UPDATE'),
new cls.UpdateTableBlock(options),
new cls.SetFieldBlock(options),
@@ -758,8 +762,8 @@ class cls.Update extends cls.QueryBuilder
# DELETE query builder.
class cls.Delete extends cls.QueryBuilder
- constructor: (options) ->
- blocks = [
+ constructor: (options, blocks = null) ->
+ blocks or= [
new cls.StringBlock(options, 'DELETE'),
new cls.FromTableBlock( _extend({}, options, { singleTable: true }) ),
new cls.JoinBlock(options),
@@ -777,8 +781,8 @@ class cls.Delete extends cls.QueryBuilder
# An INSERT query builder.
#
class cls.Insert extends cls.QueryBuilder
- constructor: (options) ->
- blocks = [
+ constructor: (options, blocks = null) ->
+ blocks or= [
new cls.StringBlock(options, 'INSERT'),
new cls.IntoTableBlock(options),
new cls.InsertFieldValueBlock(options)
@@ -796,12 +800,11 @@ class cls.Insert extends cls.QueryBuilder
squel =
expr: -> new cls.Expression
- select: (options) -> new cls.Select(options)
- update: (options) -> new cls.Update(options)
- insert: (options) -> new cls.Insert(options)
- delete: (options) -> new cls.Delete(options)
-
-# defaults
+ # Don't have a space-efficient elegant-way of .apply()-ing to constructors, so we specify the args
+ select: (options, blocks) -> new cls.Select(options, blocks)
+ update: (options, blocks) -> new cls.Update(options, blocks)
+ insert: (options, blocks) -> new cls.Insert(options, blocks)
+ delete: (options, blocks) -> new cls.Delete(options, blocks)
# aliases
View
14 test/baseclasses.test.coffee
@@ -429,6 +429,20 @@ test['QueryBuilder base class'] =
assert.ok buildStrSpy.calledOn(@inst.blocks[2])
+ 'cloning':
+ 'blocks get cloned properly': ->
+ blockCloneSpy = test.mocker.spy(squel.cls.StringBlock.prototype, 'clone')
+
+ @inst.blocks = [
+ new squel.cls.StringBlock({}, 'TEST')
+ ]
+
+ newinst = @inst.clone()
+ @inst.blocks[0].str = 'TEST2'
+
+ assert.same 'TEST', newinst.blocks[0].buildStr()
+
+
View
9 test/blocks.test.coffee
@@ -69,6 +69,15 @@ test['Blocks'] =
assert.ok undefined is _.find (name for name of @inst.exposedMethods()), (name) ->
return name is 'buildStr'
+ 'cloning copies the options over': ->
+ @inst.options.dummy = true;
+
+ newinst = @inst.clone()
+
+ @inst.options.dummy = false;
+
+ assert.same true, newinst.options.dummy
+
'StringBlock':
View
50 test/custom.test.coffee
@@ -30,61 +30,20 @@ test = testCreator()
-test['Custom queries and blocks'] =
- beforeEach: ->
- @originalSelect = squel.select
-
- afterEach: ->
- squel.select = @originalSelect
-
- 'customize existing query with new block': ->
- # Create a date format block, as a way of telling the db what date format to use when outputting dates.
- # (Note: we are just pretending that we can tell the db this!)
- class DateFormatBlock extends squel.cls.Block
- dateFormat: (formatStr) =>
- @format = formatStr
-
- buildStr: ->
- "USING DATEFORMAT (#{@format})"
-
- # Custom SELECT query
- class CustomSelect extends squel.cls.QueryBuilder
- constructor: (options) ->
- blocks = [
- new squel.cls.StringBlock(options, 'SELECT'),
- new squel.cls.DistinctBlock(options),
- new squel.cls.GetFieldBlock(options),
- new squel.cls.FromTableBlock(options),
- new squel.cls.JoinBlock(options),
- new squel.cls.WhereBlock(options),
- new DateFormatBlock(options),
- new squel.cls.GroupByBlock(options),
- new squel.cls.OrderByBlock(options),
- new squel.cls.LimitBlock(options),
- new squel.cls.OffsetBlock(options)
- ]
-
- super options, blocks
-
- # Override squel.select()
- squel.select = (options) -> new CustomSelect(options)
-
- assert.same 'SELECT * FROM students USING DATEFORMAT (yyyy-mm-dd)', squel.select().from('students').dateFormat('yyyy-mm-dd').toString()
-
+test['Custom queries'] =
'custom query': ->
-
class CommandBlock extends squel.cls.Block
- command: (command) =>
+ command: (command) ->
@_command = command
- compress: =>
+ compress: ->
@command('compress')
buildStr: ->
if (!@_command or 0 is @_command.length) then throw new Error 'command() must be called'
@_command.toUpperCase()
class ParamBlock extends squel.cls.Block
- param: (param) =>
+ param: (param) ->
@param = param
buildStr: ->
if @param then @param else ""
@@ -107,5 +66,4 @@ test['Custom queries and blocks'] =
-
module?.exports[require('path').basename(__filename)] = test
View
14 test/delete.test.coffee
@@ -32,7 +32,8 @@ test = testCreator()
test['DELETE builder'] =
beforeEach: ->
- @inst = squel.delete()
+ @func = squel.delete
+ @inst = @func()
'instanceof QueryBuilder': ->
assert.instanceOf @inst, squel.cls.QueryBuilder
@@ -50,6 +51,11 @@ test['DELETE builder'] =
for block in @inst.blocks
assert.same expectedOptions, block.options
+ 'override blocks': ->
+ block = new squel.cls.StringBlock('SELECT')
+ @inst = @func {}, [block]
+ assert.same [block], @inst.blocks
+
'build query':
'need to call from() first': ->
@@ -85,6 +91,12 @@ test['DELETE builder'] =
toString: ->
assert.same @inst.toString(), 'DELETE FROM table2 `t2` INNER JOIN other_table `o` ON (o.id = t2.id) WHERE (a = 1) ORDER BY a ASC LIMIT 2'
+ 'cloning': ->
+ newinst = @inst.from('students').limit(10).clone()
+ newinst.limit(20)
+
+ assert.same 'DELETE FROM students LIMIT 10', @inst.toString()
+ assert.same 'DELETE FROM students LIMIT 20', newinst.toString()
module?.exports[require('path').basename(__filename)] = test
View
14 test/insert.test.coffee
@@ -32,7 +32,8 @@ test = testCreator()
test['INSERT builder'] =
beforeEach: ->
- @inst = squel.insert()
+ @func = squel.insert
+ @inst = @func()
'instanceof QueryBuilder': ->
assert.instanceOf @inst, squel.cls.QueryBuilder
@@ -50,6 +51,11 @@ test['INSERT builder'] =
for block in @inst.blocks
assert.same expectedOptions, block.options
+ 'override blocks': ->
+ block = new squel.cls.StringBlock('SELECT')
+ @inst = @func {}, [block]
+ assert.same [block], @inst.blocks
+
'build query':
'need to call into() first': ->
@@ -90,6 +96,12 @@ test['INSERT builder'] =
toString: ->
assert.same @inst.toString(), 'INSERT INTO table (field, field2) VALUES (1, NULL)'
+ 'cloning': ->
+ newinst = @inst.into('students').set('field', 1).clone()
+ newinst.set('field', 2).set('field2', true)
+
+ assert.same 'INSERT INTO students (field) VALUES (1)', @inst.toString()
+ assert.same 'INSERT INTO students (field, field2) VALUES (2, TRUE)', newinst.toString()
module?.exports[require('path').basename(__filename)] = test
View
14 test/select.test.coffee
@@ -32,7 +32,8 @@ test = testCreator()
test['SELECT builder'] =
beforeEach: ->
- @inst = squel.select()
+ @func = squel.select
+ @inst = @func()
'instanceof QueryBuilder': ->
assert.instanceOf @inst, squel.cls.QueryBuilder
@@ -50,6 +51,11 @@ test['SELECT builder'] =
for block in @inst.blocks
assert.same expectedOptions, block.options
+ 'override blocks': ->
+ block = new squel.cls.StringBlock('SELECT')
+ @inst = @func {}, [block]
+ assert.same [block], @inst.blocks
+
'build query':
'need to call from() first': ->
assert.throws (=> @inst.toString()), 'from() needs to be called'
@@ -99,6 +105,12 @@ test['SELECT builder'] =
toString: ->
assert.same @inst.toString(), 'SELECT DISTINCT field1 AS "fa1", field2 FROM table, table2 `alias2` INNER JOIN other_table WHERE (a = 1) GROUP BY field, field2 ORDER BY a ASC LIMIT 2 OFFSET 3'
+ 'cloning': ->
+ newinst = @inst.from('students').limit(10).clone()
+ newinst.limit(20)
+
+ assert.same 'SELECT * FROM students LIMIT 10', @inst.toString()
+ assert.same 'SELECT * FROM students LIMIT 20', newinst.toString()
module?.exports[require('path').basename(__filename)] = test
View
17 test/update.test.coffee
@@ -32,7 +32,8 @@ test = testCreator()
test['UPDATE builder'] =
beforeEach: ->
- @inst = squel.update()
+ @func = squel.update
+ @inst = @func()
'instanceof QueryBuilder': ->
assert.instanceOf @inst, squel.cls.QueryBuilder
@@ -50,7 +51,12 @@ test['UPDATE builder'] =
for block in @inst.blocks
assert.same expectedOptions, block.options
-
+ 'override blocks': ->
+ block = new squel.cls.StringBlock('SELECT')
+ @inst = @func {}, [block]
+ assert.same [block], @inst.blocks
+
+
'build query':
'need to call table() first': ->
assert.throws (=> @inst.toString()), 'table() needs to be called'
@@ -111,5 +117,12 @@ test['UPDATE builder'] =
assert.same @inst.toString(), 'UPDATE table AS `t1`, table2 SET field = 1, field2 = NULL WHERE (a = 1) ORDER BY a ASC LIMIT 2'
+ 'cloning': ->
+ newinst = @inst.table('students').set('field', 1).clone()
+ newinst.set('field', 2).set('field2', true)
+
+ assert.same 'UPDATE students SET field = 1', @inst.toString()
+ assert.same 'UPDATE students SET field = 2, field2 = TRUE', newinst.toString()
+
module?.exports[require('path').basename(__filename)] = test

0 comments on commit 419a366

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