Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Completed tests for all Blocks.

Todo: tests for Builder and QueryBuilder classes as well as newly refactored query builders.

Todo: tests to ensure that customised query builders and custom blocks work fine.
  • Loading branch information...
commit cd5475fb52d6eadd093e3a653c949a6421eb5891 1 parent 77be046
@hiddentao authored
View
30 docs/squel.html
@@ -154,7 +154,7 @@
<p>If you wish to customize how queries get built or add proprietary query phrases and content then it is recommended
that you do so using one or more custom building blocks.</p>
-<p>Original idea posted in https://github.com/hiddentao/export/issues/10#issuecomment-15016427</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">BuildingBlock</span> <span class="k">extends</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">BaseBuilder</span></pre></div> </td> </tr> <tr id="section-39"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-39">&#182;</a> </div> <p>Get input methods to expose within the query builder.</p>
+<p>Original idea posted in https://github.com/hiddentao/export/issues/10#issuecomment-15016427</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">Block</span> <span class="k">extends</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">BaseBuilder</span></pre></div> </td> </tr> <tr id="section-39"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-39">&#182;</a> </div> <p>Get input methods to expose within the query builder.</p>
<p>By default all methods except the following get returned:
methods prefixed with _
@@ -175,13 +175,13 @@
<p>@param queryBuilder cls.QueryBuilder a reference to the query builder that owns this block.</p>
<p>@return String the string representing this block</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">buildStr: </span><span class="nf">(queryBuilder) -&gt;</span>
- <span class="s">&#39;&#39;</span></pre></div> </td> </tr> <tr id="section-41"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-41">&#182;</a> </div> <p>A String which always gets output</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">StringBlock</span> <span class="k">extends</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">BuildingBlock</span>
+ <span class="s">&#39;&#39;</span></pre></div> </td> </tr> <tr id="section-41"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-41">&#182;</a> </div> <p>A String which always gets output</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">StringBlock</span> <span class="k">extends</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">Block</span>
<span class="nv">constructor: </span><span class="nf">(options, str) -&gt;</span>
<span class="k">super</span> <span class="nx">options</span>
<span class="vi">@str = </span><span class="nx">str</span>
<span class="nv">buildStr: </span><span class="nf">(queryBuilder) -&gt;</span>
- <span class="nx">@str</span></pre></div> </td> </tr> <tr id="section-42"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-42">&#182;</a> </div> <p>FROM table</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">FromTableBlock</span> <span class="k">extends</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">BuildingBlock</span>
+ <span class="nx">@str</span></pre></div> </td> </tr> <tr id="section-42"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-42">&#182;</a> </div> <p>FROM table</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">FromTableBlock</span> <span class="k">extends</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">Block</span>
<span class="nv">constructor: </span><span class="nf">(options) -&gt;</span>
<span class="k">super</span> <span class="nx">options</span>
<span class="vi">@froms = </span><span class="p">[]</span></pre></div> </td> </tr> <tr id="section-43"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-43">&#182;</a> </div> <p>Read data from the given table.</p>
@@ -204,7 +204,7 @@
<span class="nx">tables</span> <span class="o">+=</span> <span class="nx">table</span><span class="p">.</span><span class="nx">name</span>
<span class="nx">tables</span> <span class="o">+=</span> <span class="s">&quot; `</span><span class="si">#{</span><span class="nx">table</span><span class="p">.</span><span class="nx">alias</span><span class="si">}</span><span class="s">`&quot;</span> <span class="k">if</span> <span class="nx">table</span><span class="p">.</span><span class="nx">alias</span>
- <span class="s">&quot;FROM </span><span class="si">#{</span><span class="nx">tables</span><span class="si">}</span><span class="s">&quot;</span></pre></div> </td> </tr> <tr id="section-44"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-44">&#182;</a> </div> <p>UPDATE table</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">UpdateTableBlock</span> <span class="k">extends</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">BuildingBlock</span>
+ <span class="s">&quot;FROM </span><span class="si">#{</span><span class="nx">tables</span><span class="si">}</span><span class="s">&quot;</span></pre></div> </td> </tr> <tr id="section-44"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-44">&#182;</a> </div> <p>UPDATE table</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">UpdateTableBlock</span> <span class="k">extends</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">Block</span>
<span class="nv">constructor: </span><span class="nf">(options) -&gt;</span>
<span class="k">super</span> <span class="nx">options</span>
<span class="vi">@tables = </span><span class="p">[]</span></pre></div> </td> </tr> <tr id="section-45"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-45">&#182;</a> </div> <p>Update given table.</p>
@@ -226,7 +226,7 @@
<span class="nx">tables</span> <span class="o">+=</span> <span class="nx">table</span><span class="p">.</span><span class="nx">name</span>
<span class="nx">tables</span> <span class="o">+=</span> <span class="s">&quot; AS `</span><span class="si">#{</span><span class="nx">table</span><span class="p">.</span><span class="nx">alias</span><span class="si">}</span><span class="s">`&quot;</span> <span class="k">if</span> <span class="nx">table</span><span class="p">.</span><span class="nx">alias</span>
- <span class="nx">tables</span></pre></div> </td> </tr> <tr id="section-46"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-46">&#182;</a> </div> <p>INTO table</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">IntoTableBlock</span> <span class="k">extends</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">BuildingBlock</span>
+ <span class="nx">tables</span></pre></div> </td> </tr> <tr id="section-46"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-46">&#182;</a> </div> <p>INTO table</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">IntoTableBlock</span> <span class="k">extends</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">Block</span>
<span class="nv">constructor: </span><span class="nf">(options) -&gt;</span>
<span class="k">super</span> <span class="nx">options</span>
<span class="vi">@table = </span><span class="kc">null</span></pre></div> </td> </tr> <tr id="section-47"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-47">&#182;</a> </div> <p>Into given table.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">table: </span><span class="nf">(table) -&gt;</span>
@@ -234,7 +234,7 @@
<span class="nv">buildStr: </span><span class="nf">(queryBuilder) -&gt;</span>
<span class="k">if</span> <span class="o">not</span> <span class="nx">@table</span> <span class="k">then</span> <span class="k">throw</span> <span class="k">new</span> <span class="nb">Error</span> <span class="s">&quot;into() needs to be called&quot;</span>
- <span class="nx">@table</span></pre></div> </td> </tr> <tr id="section-48"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-48">&#182;</a> </div> <p>Get field</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">GetFieldBlock</span> <span class="k">extends</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">BuildingBlock</span>
+ <span class="nx">@table</span></pre></div> </td> </tr> <tr id="section-48"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-48">&#182;</a> </div> <p>Get field</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">GetFieldBlock</span> <span class="k">extends</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">Block</span>
<span class="nv">constructor: </span><span class="nf">(options) -&gt;</span>
<span class="k">super</span> <span class="nx">options</span>
<span class="vi">@fields = </span><span class="p">[]</span></pre></div> </td> </tr> <tr id="section-49"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-49">&#182;</a> </div> <p>Add the given field to the final result set.</p>
@@ -257,7 +257,7 @@
<span class="nx">fields</span> <span class="o">+=</span> <span class="nx">field</span><span class="p">.</span><span class="nx">name</span>
<span class="nx">fields</span> <span class="o">+=</span> <span class="s">&quot; AS \&quot;</span><span class="si">#{</span><span class="nx">field</span><span class="p">.</span><span class="nx">alias</span><span class="si">}</span><span class="s">\&quot;&quot;</span> <span class="k">if</span> <span class="nx">field</span><span class="p">.</span><span class="nx">alias</span>
- <span class="k">if</span> <span class="s">&quot;&quot;</span> <span class="o">is</span> <span class="nx">fields</span> <span class="k">then</span> <span class="s">&quot;*&quot;</span> <span class="k">else</span> <span class="nx">fields</span></pre></div> </td> </tr> <tr id="section-50"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-50">&#182;</a> </div> <p>SET field=value</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">SetFieldBlock</span> <span class="k">extends</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">BuildingBlock</span>
+ <span class="k">if</span> <span class="s">&quot;&quot;</span> <span class="o">is</span> <span class="nx">fields</span> <span class="k">then</span> <span class="s">&quot;*&quot;</span> <span class="k">else</span> <span class="nx">fields</span></pre></div> </td> </tr> <tr id="section-50"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-50">&#182;</a> </div> <p>SET field=value</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">SetFieldBlock</span> <span class="k">extends</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">Block</span>
<span class="nv">constructor: </span><span class="nf">(options) -&gt;</span>
<span class="k">super</span> <span class="nx">options</span>
<span class="vi">@fields = </span><span class="p">{}</span></pre></div> </td> </tr> <tr id="section-51"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-51">&#182;</a> </div> <p>Update the given field with the given value.
@@ -276,7 +276,7 @@
<span class="nx">fields</span> <span class="o">+=</span> <span class="s">&quot;, &quot;</span> <span class="k">if</span> <span class="s">&quot;&quot;</span> <span class="o">isnt</span> <span class="nx">fields</span>
<span class="nx">fields</span> <span class="o">+=</span> <span class="s">&quot;</span><span class="si">#{</span><span class="nx">field</span><span class="si">}</span><span class="s"> = </span><span class="si">#{</span><span class="nx">@_formatValue</span><span class="p">(</span><span class="nx">@fields</span><span class="p">[</span><span class="nx">field</span><span class="p">])</span><span class="si">}</span><span class="s">&quot;</span>
- <span class="s">&quot;SET </span><span class="si">#{</span><span class="nx">fields</span><span class="si">}</span><span class="s">&quot;</span></pre></div> </td> </tr> <tr id="section-52"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-52">&#182;</a> </div> <p>INSERT INTO ... field ... value</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">InsertIntoFieldBlock</span> <span class="k">extends</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">BuildingBlock</span>
+ <span class="s">&quot;SET </span><span class="si">#{</span><span class="nx">fields</span><span class="si">}</span><span class="s">&quot;</span></pre></div> </td> </tr> <tr id="section-52"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-52">&#182;</a> </div> <p>INSERT INTO ... field ... value</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">InsertIntoFieldBlock</span> <span class="k">extends</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">Block</span>
<span class="nv">constructor: </span><span class="nf">(options) -&gt;</span>
<span class="k">super</span> <span class="nx">options</span>
<span class="vi">@fields = </span><span class="p">{}</span></pre></div> </td> </tr> <tr id="section-53"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-53">&#182;</a> </div> <p>Update the given field with the given value.
@@ -296,14 +296,14 @@
<span class="nx">values</span> <span class="o">+=</span> <span class="s">&quot;, &quot;</span> <span class="k">if</span> <span class="s">&quot;&quot;</span> <span class="o">isnt</span> <span class="nx">values</span>
<span class="nx">values</span> <span class="o">+=</span> <span class="nx">@_formatValue</span><span class="p">(</span><span class="nx">@fields</span><span class="p">[</span><span class="nx">field</span><span class="p">])</span>
- <span class="s">&quot;(</span><span class="si">#{</span><span class="nx">fields</span><span class="si">}</span><span class="s">) VALUES (</span><span class="si">#{</span><span class="nx">values</span><span class="si">}</span><span class="s">)&quot;</span></pre></div> </td> </tr> <tr id="section-55"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-55">&#182;</a> </div> <p>DISTINCT</p> </td> <td class="code"> <div class="highlight"><pre><span class="k">class</span> <span class="nx">DistinctBlock</span> <span class="k">extends</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">BuildingBlock</span>
+ <span class="s">&quot;(</span><span class="si">#{</span><span class="nx">fields</span><span class="si">}</span><span class="s">) VALUES (</span><span class="si">#{</span><span class="nx">values</span><span class="si">}</span><span class="s">)&quot;</span></pre></div> </td> </tr> <tr id="section-55"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-55">&#182;</a> </div> <p>DISTINCT</p> </td> <td class="code"> <div class="highlight"><pre><span class="k">class</span> <span class="nx">DistinctBlock</span> <span class="k">extends</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">Block</span>
<span class="nv">constructor: </span><span class="nf">(options) -&gt;</span>
<span class="k">super</span> <span class="nx">options</span>
<span class="vi">@use_distinct = </span><span class="kc">false</span></pre></div> </td> </tr> <tr id="section-56"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-56">&#182;</a> </div> <p>Add the DISTINCT keyword to the query.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">distinct: </span><span class="o">-&gt;</span>
<span class="vi">@useDistinct = </span><span class="kc">true</span>
<span class="nv">buildStr: </span><span class="nf">(queryBuilder) -&gt;</span>
- <span class="k">if</span> <span class="nx">@useDistinct</span> <span class="k">then</span> <span class="s">&quot;DISTINCT&quot;</span> <span class="k">else</span> <span class="s">&quot;&quot;</span></pre></div> </td> </tr> <tr id="section-57"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-57">&#182;</a> </div> <p>GROUP BY</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">GroupByBlock</span> <span class="k">extends</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">BuildingBlock</span>
+ <span class="k">if</span> <span class="nx">@useDistinct</span> <span class="k">then</span> <span class="s">&quot;DISTINCT&quot;</span> <span class="k">else</span> <span class="s">&quot;&quot;</span></pre></div> </td> </tr> <tr id="section-57"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-57">&#182;</a> </div> <p>GROUP BY</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">GroupByBlock</span> <span class="k">extends</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">Block</span>
<span class="nv">constructor: </span><span class="nf">(options) -&gt;</span>
<span class="k">super</span> <span class="nx">options</span>
<span class="vi">@groups = </span><span class="p">[]</span></pre></div> </td> </tr> <tr id="section-58"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-58">&#182;</a> </div> <p>Add a GROUP BY transformation for the given field.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">group: </span><span class="p">(</span><span class="nx">field</span><span class="p">)</span> <span class="o">=&gt;</span>
@@ -319,7 +319,7 @@
<span class="nx">groups</span> <span class="o">+=</span> <span class="nx">f</span>
<span class="nv">groups = </span><span class="s">&quot;GROUP BY </span><span class="si">#{</span><span class="nx">groups</span><span class="si">}</span><span class="s">&quot;</span>
- <span class="nx">groups</span></pre></div> </td> </tr> <tr id="section-59"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-59">&#182;</a> </div> <p>OFFSET x</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">OffsetBlock</span> <span class="k">extends</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">BuildingBlock</span>
+ <span class="nx">groups</span></pre></div> </td> </tr> <tr id="section-59"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-59">&#182;</a> </div> <p>OFFSET x</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">OffsetBlock</span> <span class="k">extends</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">Block</span>
<span class="nv">constructor: </span><span class="nf">(options) -&gt;</span>
<span class="k">super</span> <span class="nx">options</span>
<span class="vi">@offsets = </span><span class="kc">null</span></pre></div> </td> </tr> <tr id="section-60"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-60">&#182;</a> </div> <p>Set the OFFSET transformation.</p>
@@ -330,7 +330,7 @@
<span class="vi">@offsets = </span><span class="nx">start</span>
<span class="nv">buildStr: </span><span class="nf">(queryBuilder) -&gt;</span>
- <span class="k">if</span> <span class="p">(</span><span class="nx">@offsets</span><span class="p">)</span> <span class="s">&quot;OFFSET </span><span class="si">#{</span><span class="nx">@offsets</span><span class="si">}</span><span class="s">&quot;</span> <span class="k">else</span> <span class="s">&quot;&quot;</span></pre></div> </td> </tr> <tr id="section-61"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-61">&#182;</a> </div> <p>WHERE</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">WhereBlock</span> <span class="k">extends</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">BuildingBlock</span>
+ <span class="k">if</span> <span class="p">(</span><span class="nx">@offsets</span><span class="p">)</span> <span class="s">&quot;OFFSET </span><span class="si">#{</span><span class="nx">@offsets</span><span class="si">}</span><span class="s">&quot;</span> <span class="k">else</span> <span class="s">&quot;&quot;</span></pre></div> </td> </tr> <tr id="section-61"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-61">&#182;</a> </div> <p>WHERE</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">WhereBlock</span> <span class="k">extends</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">Block</span>
<span class="nv">constructor: </span><span class="nf">(options) -&gt;</span>
<span class="k">super</span> <span class="nx">options</span>
<span class="vi">@wheres = </span><span class="p">[]</span></pre></div> </td> </tr> <tr id="section-62"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-62">&#182;</a> </div> <p>Add a WHERE condition.</p>
@@ -341,7 +341,7 @@
<span class="nx">@wheres</span><span class="p">.</span><span class="nx">push</span> <span class="nx">condition</span>
<span class="nv">buildStr: </span><span class="nf">(queryBuilder) -&gt;</span>
- <span class="k">if</span> <span class="p">(</span><span class="nx">@offsets</span><span class="p">)</span> <span class="s">&quot;OFFSET </span><span class="si">#{</span><span class="nx">@offsets</span><span class="si">}</span><span class="s">&quot;</span> <span class="k">else</span> <span class="s">&quot;&quot;</span></pre></div> </td> </tr> <tr id="section-63"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-63">&#182;</a> </div> <p>ORDER BY</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">OrderByBlock</span> <span class="k">extends</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">BuildingBlock</span>
+ <span class="k">if</span> <span class="p">(</span><span class="nx">@offsets</span><span class="p">)</span> <span class="s">&quot;OFFSET </span><span class="si">#{</span><span class="nx">@offsets</span><span class="si">}</span><span class="s">&quot;</span> <span class="k">else</span> <span class="s">&quot;&quot;</span></pre></div> </td> </tr> <tr id="section-63"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-63">&#182;</a> </div> <p>ORDER BY</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">OrderByBlock</span> <span class="k">extends</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">Block</span>
<span class="nv">constructor: </span><span class="nf">(options) -&gt;</span>
<span class="k">super</span> <span class="nx">options</span>
<span class="vi">@orders = </span><span class="p">[]</span></pre></div> </td> </tr> <tr id="section-64"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-64">&#182;</a> </div> <p>Add an ORDER BY transformation for the given field in the given order.</p>
@@ -360,7 +360,7 @@
<span class="nx">orders</span> <span class="o">+=</span> <span class="s">&quot;</span><span class="si">#{</span><span class="nx">o</span><span class="p">.</span><span class="nx">field</span><span class="si">}</span><span class="s"> </span><span class="si">#{</span><span class="nx">o</span><span class="p">.</span><span class="nx">dir</span><span class="si">}</span><span class="s">&quot;</span>
<span class="s">&quot;ORDER BY </span><span class="si">#{</span><span class="nx">orders</span><span class="si">}</span><span class="s">&quot;</span>
<span class="k">else</span>
- <span class="s">&quot;&quot;</span></pre></div> </td> </tr> <tr id="section-65"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-65">&#182;</a> </div> <p>LIMIT</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">LimitBlock</span> <span class="k">extends</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">BuildingBlock</span>
+ <span class="s">&quot;&quot;</span></pre></div> </td> </tr> <tr id="section-65"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-65">&#182;</a> </div> <p>LIMIT</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">LimitBlock</span> <span class="k">extends</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">Block</span>
<span class="nv">constructor: </span><span class="nf">(options) -&gt;</span>
<span class="k">super</span> <span class="nx">options</span>
<span class="vi">@limits = </span><span class="kc">null</span></pre></div> </td> </tr> <tr id="section-66"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-66">&#182;</a> </div> <p>Set the LIMIT transformation.</p>
@@ -372,7 +372,7 @@
<span class="nv">buildStr: </span><span class="nf">(queryBuilder) -&gt;</span>
- <span class="k">if</span> <span class="nx">@limits</span> <span class="s">&quot;LIMIT </span><span class="si">#{</span><span class="nx">@limits</span><span class="si">}</span><span class="s">&quot;</span> <span class="k">else</span> <span class="s">&quot;&quot;</span></pre></div> </td> </tr> <tr id="section-67"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-67">&#182;</a> </div> <p>JOIN</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">JoinBlock</span> <span class="k">extends</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">BuildingBlock</span>
+ <span class="k">if</span> <span class="nx">@limits</span> <span class="s">&quot;LIMIT </span><span class="si">#{</span><span class="nx">@limits</span><span class="si">}</span><span class="s">&quot;</span> <span class="k">else</span> <span class="s">&quot;&quot;</span></pre></div> </td> </tr> <tr id="section-67"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-67">&#182;</a> </div> <p>JOIN</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">JoinBlock</span> <span class="k">extends</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">Block</span>
<span class="nv">constructor: </span><span class="nf">(options) -&gt;</span>
<span class="k">super</span> <span class="nx">options</span>
<span class="vi">@joins = </span><span class="p">[]</span></pre></div> </td> </tr> <tr id="section-68"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-68">&#182;</a> </div> <p>Add a JOIN with the given table.</p>
View
42 squel.js
@@ -283,15 +283,15 @@ OTHER DEALINGS IN THE SOFTWARE.
})();
- cls.BuildingBlock = (function(_super) {
+ cls.Block = (function(_super) {
- __extends(BuildingBlock, _super);
+ __extends(Block, _super);
- function BuildingBlock() {
- return BuildingBlock.__super__.constructor.apply(this, arguments);
+ function Block() {
+ return Block.__super__.constructor.apply(this, arguments);
}
- BuildingBlock.prototype.exposedMethods = function() {
+ Block.prototype.exposedMethods = function() {
var attr, ret, value;
ret = {};
for (attr in this) {
@@ -306,11 +306,11 @@ OTHER DEALINGS IN THE SOFTWARE.
return ret;
};
- BuildingBlock.prototype.buildStr = function(queryBuilder) {
+ Block.prototype.buildStr = function(queryBuilder) {
return '';
};
- return BuildingBlock;
+ return Block;
})(cls.BaseBuilder);
@@ -329,7 +329,7 @@ OTHER DEALINGS IN THE SOFTWARE.
return StringBlock;
- })(cls.BuildingBlock);
+ })(cls.Block);
cls.FromTableBlock = (function(_super) {
@@ -376,7 +376,7 @@ OTHER DEALINGS IN THE SOFTWARE.
return FromTableBlock;
- })(cls.BuildingBlock);
+ })(cls.Block);
cls.UpdateTableBlock = (function(_super) {
@@ -423,7 +423,7 @@ OTHER DEALINGS IN THE SOFTWARE.
return UpdateTableBlock;
- })(cls.BuildingBlock);
+ })(cls.Block);
cls.IntoTableBlock = (function(_super) {
@@ -447,7 +447,7 @@ OTHER DEALINGS IN THE SOFTWARE.
return IntoTableBlock;
- })(cls.BuildingBlock);
+ })(cls.Block);
cls.GetFieldBlock = (function(_super) {
@@ -496,7 +496,7 @@ OTHER DEALINGS IN THE SOFTWARE.
return GetFieldBlock;
- })(cls.BuildingBlock);
+ })(cls.Block);
cls.SetFieldBlock = (function(_super) {
@@ -543,7 +543,7 @@ OTHER DEALINGS IN THE SOFTWARE.
return SetFieldBlock;
- })(cls.BuildingBlock);
+ })(cls.Block);
cls.InsertIntoFieldBlock = (function(_super) {
@@ -595,7 +595,7 @@ OTHER DEALINGS IN THE SOFTWARE.
return InsertIntoFieldBlock;
- })(cls.BuildingBlock);
+ })(cls.Block);
DistinctBlock = (function(_super) {
@@ -620,7 +620,7 @@ OTHER DEALINGS IN THE SOFTWARE.
return DistinctBlock;
- })(cls.BuildingBlock);
+ })(cls.Block);
cls.GroupByBlock = (function(_super) {
@@ -656,7 +656,7 @@ OTHER DEALINGS IN THE SOFTWARE.
return GroupByBlock;
- })(cls.BuildingBlock);
+ })(cls.Block);
cls.OffsetBlock = (function(_super) {
@@ -683,7 +683,7 @@ OTHER DEALINGS IN THE SOFTWARE.
return OffsetBlock;
- })(cls.BuildingBlock);
+ })(cls.Block);
cls.WhereBlock = (function(_super) {
@@ -712,7 +712,7 @@ OTHER DEALINGS IN THE SOFTWARE.
return WhereBlock;
- })(cls.BuildingBlock);
+ })(cls.Block);
cls.OrderByBlock = (function(_super) {
@@ -755,7 +755,7 @@ OTHER DEALINGS IN THE SOFTWARE.
return OrderByBlock;
- })(cls.BuildingBlock);
+ })(cls.Block);
cls.LimitBlock = (function(_super) {
@@ -782,7 +782,7 @@ OTHER DEALINGS IN THE SOFTWARE.
return LimitBlock;
- })(cls.BuildingBlock);
+ })(cls.Block);
cls.JoinBlock = (function(_super) {
@@ -869,7 +869,7 @@ OTHER DEALINGS IN THE SOFTWARE.
return JoinBlock;
- })(cls.BuildingBlock);
+ })(cls.Block);
cls.QueryBuilder = (function(_super) {
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=[].slice,s={}.hasOwnProperty,o=function(e,t){function r(){this.constructor=e}for(var n in t)s.call(t,n)&&(e[n]=t[n]);return r.prototype=t.prototype,e.prototype=new r,e.__super__=t.prototype,e},u=function(e,t){return function(){return e.apply(t,arguments)}};t={},r=function(){var e,t,n,r,o,u,a;e=arguments[0],n=2<=arguments.length?i.call(arguments,1):[];if(n)for(u=0,a=n.length;u<a;u++){r=n[u];if(r)for(t in r){if(!s.call(r,t))continue;o=r[t],e[t]=o}}return e},t.DefaultQueryBuilderOptions={autoQuoteTableNames:!1,autoQuoteFieldNames:!1,nameQuoteCharacter:"`",usingValuePlaceholders:!1},t.Cloneable=function(){function e(){}return e.prototype.clone=function(){var e;return e=new this.constructor,r(e,JSON.parse(JSON.stringify(this)))},e}(),t.BaseBuilder=function(e){function n(e){this.options=r({},t.DefaultQueryBuilderOptions,e)}return o(n,e),n.prototype._getObjectClassName=function(e){var t;if(e&&e.constructor&&e.constructor.toString){t=e.constructor.toString().match(/function\s*(\w+)/);if(t&&t.length===2)return t[1]}return void 0},n.prototype._sanitizeCondition=function(e){var t,n;n=typeof e,t=this._getObjectClassName(e);if("cls.Expression"!==t&&"string"!==n)throw new Error("condition must be a string or cls.Expression instance");if("cls.Expression"===n||"cls.Expression"===t)e=e.toString();return e},n.prototype._sanitizeName=function(e,t){if("string"!=typeof e)throw new Error(""+t+" must be a string");return e},n.prototype._sanitizeField=function(e){var t;return t=this._sanitizeName(e,"field name"),this.options.autoQuoteFieldNames?""+this.options.nameQuoteCharacter+t+this.options.nameQuoteCharacter:t},n.prototype._sanitizeTable=function(e){var t;return t=this._sanitizeName(e,"table name"),this.options.autoQuoteTableNames?""+this.options.nameQuoteCharacter+t+this.options.nameQuoteCharacter:t},n.prototype._sanitizeAlias=function(e){return this._sanitizeName(e,"alias")},n.prototype._sanitizeLimitOffset=function(e){e=parseInt(e);if(0>e||isNaN(e))throw new Error("limit/offset must be >=0");return e},n.prototype._sanitizeValue=function(e){var t;t=typeof e;if(null!==e&&"string"!==t&&"number"!==t&&"boolean"!==t)throw new Error("field value must be a string, number, boolean or null");return e},n.prototype._formatValue=function(e){return null===e?e="NULL":"boolean"==typeof e?e=e?"TRUE":"FALSE":"number"!=typeof e&&!1===this.options.usingValuePlaceholders&&(e="'"+e+"'"),e},n}(t.Cloneable),t.Expression=function(){function t(){this.toString=u(this.toString,this),this.or=u(this.or,this),this.and=u(this.and,this),this.end=u(this.end,this),this.or_begin=u(this.or_begin,this),this.and_begin=u(this.and_begin,this);var e=this;this.tree={parent:null,nodes:[]},this.current=this.tree,this._begin=function(t){var n;return n={type:t,parent:e.current,nodes:[]},e.current.nodes.push(n),e.current=e.current.nodes[e.current.nodes.length-1],e}}var e;return t.prototype.tree=null,t.prototype.current=null,t.prototype.and_begin=function(){return this._begin("AND")},t.prototype.or_begin=function(){return this._begin("OR")},t.prototype.end=function(){if(!this.current.parent)throw new Error("begin() needs to be called");return this.current=this.current.parent,this},t.prototype.and=function(e){if(!e||"string"!=typeof e)throw new Error("expr must be a string");return this.current.nodes.push({type:"AND",expr:e}),this},t.prototype.or=function(e){if(!e||"string"!=typeof e)throw new Error("expr must be a string");return this.current.nodes.push({type:"OR",expr:e}),this},t.prototype.toString=function(){if(null!==this.current.parent)throw new Error("end() needs to be called");return e(this.tree)},e=function(t){var n,r,i,s,o,u;i="",u=t.nodes;for(s=0,o=u.length;s<o;s++)n=u[s],n.expr!=null?r=n.expr:(r=e(n),""!==r&&(r="("+r+")")),""!==r&&(""!==i&&(i+=" "+n.type+" "),i+=r);return i},t}(),t.BuildingBlock=function(e){function t(){return t.__super__.constructor.apply(this,arguments)}return o(t,e),t.prototype.exposedMethods=function(){var e,t,n;t={};for(e in this){if(!s.call(this,e))continue;n=this[e],typeof n=="Function"&&e.charAt(0)!=="_"&&e!=="buildStr"&&(t[e]=n)}return t},t.prototype.buildStr=function(e){return""},t}(t.BaseBuilder),t.StringBlock=function(e){function t(e,n){t.__super__.constructor.call(this,e),this.str=n}return o(t,e),t.prototype.buildStr=function(e){return this.str},t}(t.BuildingBlock),t.FromTableBlock=function(e){function t(e){t.__super__.constructor.call(this,e),this.froms=[]}return o(t,e),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})},t.prototype.buildStr=function(e){var t,n,r,i,s;if(0>=this.froms.length)throw new Error("from() needs to be called");n="",s=this.froms;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}(t.BuildingBlock),t.UpdateTableBlock=function(e){function t(e){t.__super__.constructor.call(this,e),this.tables=[]}return o(t,e),t.prototype.table=function(e,t){return t==null&&(t=null),e=this._sanitizeTable(e),t&&(t=this._sanitizeAlias(t)),this.tables.push({name:e,alias:t})},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}(t.BuildingBlock),t.IntoTableBlock=function(e){function t(e){t.__super__.constructor.call(this,e),this.table=null}return o(t,e),t.prototype.table=function(e){return e=this._sanitizeTable(e)},t.prototype.buildStr=function(e){if(!this.table)throw new Error("into() needs to be called");return this.table},t}(t.BuildingBlock),t.GetFieldBlock=function(e){function t(e){this.field=u(this.field,this),t.__super__.constructor.call(this,e),this.fields=[]}return o(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}(t.BuildingBlock),t.SetFieldBlock=function(e){function t(e){this.set=u(this.set,this),t.__super__.constructor.call(this,e),this.fields={}}return o(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,i,o;n=function(){var e,n;e=this.fields,n=[];for(t in e){if(!s.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(i=0,o=n.length;i<o;i++)t=n[i],""!==r&&(r+=", "),r+=""+t+" = "+this._formatValue(this.fields[t]);return"SET "+r},t}(t.BuildingBlock),t.InsertIntoFieldBlock=function(e){function t(e){this.set=u(this.set,this),t.__super__.constructor.call(this,e),this.fields={}}return o(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,i,o,u,a;n=function(){var e,t;e=this.fields,t=[];for(i in e){if(!s.call(e,i))continue;t.push(i)}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}(t.BuildingBlock),e=function(e){function t(e){t.__super__.constructor.call(this,e),this.use_distinct=!1}return o(t,e),t.prototype.distinct=function(){return this.useDistinct=!0},t.prototype.buildStr=function(e){return this.useDistinct?"DISTINCT":""},t}(t.BuildingBlock),t.GroupByBlock=function(e){function t(e){this.group=u(this.group,this),t.__super__.constructor.call(this,e),this.groups=[]}return o(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}(t.BuildingBlock),t.OffsetBlock=function(e){function t(e){this.offset=u(this.offset,this),t.__super__.constructor.call(this,e),this.offsets=null}return o(t,e),t.prototype.offset=function(e){return e=this._sanitizeLimitOffset(e),this.offsets=e},t.prototype.buildStr=function(e){if(!this.offsets("OFFSET "+this.offsets))return""},t}(t.BuildingBlock),t.WhereBlock=function(e){function t(e){this.where=u(this.where,this),t.__super__.constructor.call(this,e),this.wheres=[]}return o(t,e),t.prototype.where=function(e){e=this._sanitizeCondition(e);if(""!==e)return this.wheres.push(e)},t.prototype.buildStr=function(e){if(!this.offsets("OFFSET "+this.offsets))return""},t}(t.BuildingBlock),t.OrderByBlock=function(e){function t(e){this.order=u(this.order,this),t.__super__.constructor.call(this,e),this.orders=[]}return o(t,e),t.prototype.order=function(e,t){return t==null&&(t=!0),e=this._sanitizeField(e),this.orders.push({field:e,dir:t?"ASC":"DESC"})},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;return"ORDER BY "+n}return""},t}(t.BuildingBlock),t.LimitBlock=function(e){function t(e){this.limit=u(this.limit,this),t.__super__.constructor.call(this,e),this.limits=null}return o(t,e),t.prototype.limit=function(e){return e=this._sanitizeLimitOffset(e),this.limits=e},t.prototype.buildStr=function(e){if(!this.limits("LIMIT "+this.limits))return""},t}(t.BuildingBlock),t.JoinBlock=function(e){function t(e){this.outer_join=u(this.outer_join,this),this.right_join=u(this.right_join,this),this.left_join=u(this.left_join,this),this.join=u(this.join,this),t.__super__.constructor.call(this,e),this.joins=[]}return o(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.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+=" "+t.type+" JOIN "+t.table,t.alias&&(n+=" `"+t.alias+"`"),t.condition&&(n+=" ON ("+t.condition+")");return n},t}(t.BuildingBlock),t.QueryBuilder=function(e){function t(e){var t,n,r,i,o,u,a,f,l=this;this.block=e,a=this.blocks;for(o=0,u=a.length;o<u;o++){t=a[o],f=t.exposedMethods(),i=function(e,t){return l[e]=function(){return t.apply(l,arguments),l}};for(r in f){if(!s.call(f,r))continue;n=f[r];if(this[r]!=null)throw new Error(_getObjectClassName(this)+("already has a builder method called "+r));i(r,n)}}({toString:function(){return function(){var e,n,r,i;r=this.blocks,i=[];for(e=0,n=r.length;e<n;e++)t=r[e],i.push(t.buildStr(this));return i}.call(l).join(" ")}})}return o(t,e),t}(t.BaseBuilder),t.Select=function(e){function n(e){var r;r=[new t.StringBlock(e,"SELECT"),new t.GetFieldBlock(e),new t.FromTableBlock(e),new t.JoinBlock(e),new t.WhereBlock(e),new t.GroupByBlock(e),new t.OrderByBlock(e),new t.LimitBlock(e),new t.OffsetBlock(e)],n.__super__.constructor.call(this,r)}return o(n,e),n}(t.QueryBuilder),t.Update=function(e){function n(e){var r;r=[new t.StringBlock(e,"UPDATE"),new t.UpdateTableBlock(e),new t.SetFieldBlock(e),new t.WhereBlock(e),new t.OrderByBlock(e),new t.LimitBlock(e)],n.__super__.constructor.call(this,r)}return o(n,e),n}(t.QueryBuilder),t.Delete=function(e){function n(e){var r;r=[new t.StringBlock(e,"DELETE"),new t.FromTableBlock(e),new t.JoinBlock(e),new t.WhereBlock(e),new t.OrderByBlock(e),new t.LimitBlock(e)],n.__super__.constructor.call(this,r)}return o(n,e),n}(t.QueryBuilder),t.Insert=function(e){function n(e){var r;r=[new t.StringBlock(e,"INSERT"),new t.IntoTableBlock(e),new t.InsertIntoFieldBlock(e)],n.__super__.constructor.call(this,r)}return o(n,e),n}(t.BaseBuilder),n={expr:function(){return new t.Expression},select:function(e){return new t.Select(e)},update:function(e){return new t.Update(e)},insert:function(e){return new t.Insert(e)},"delete":function(e){return new t.Delete(e)}},n.remove=n["delete"],n.classes=t,(typeof define!="undefined"&&define!==null?define.amd:void 0)?define(function(){return n}):(typeof module!="undefined"&&module!==null?module.exports:void 0)?module.export=n:typeof window!="undefined"&&window!==null&&(window.squel=n)}).call(this);
+*/(function(){var e,t,n,r,i=[].slice,s={}.hasOwnProperty,o=function(e,t){function r(){this.constructor=e}for(var n in t)s.call(t,n)&&(e[n]=t[n]);return r.prototype=t.prototype,e.prototype=new r,e.__super__=t.prototype,e},u=function(e,t){return function(){return e.apply(t,arguments)}};t={},r=function(){var e,t,n,r,o,u,a;e=arguments[0],n=2<=arguments.length?i.call(arguments,1):[];if(n)for(u=0,a=n.length;u<a;u++){r=n[u];if(r)for(t in r){if(!s.call(r,t))continue;o=r[t],e[t]=o}}return e},t.DefaultQueryBuilderOptions={autoQuoteTableNames:!1,autoQuoteFieldNames:!1,nameQuoteCharacter:"`",usingValuePlaceholders:!1},t.Cloneable=function(){function e(){}return e.prototype.clone=function(){var e;return e=new this.constructor,r(e,JSON.parse(JSON.stringify(this)))},e}(),t.BaseBuilder=function(e){function n(e){this.options=r({},t.DefaultQueryBuilderOptions,e)}return o(n,e),n.prototype._getObjectClassName=function(e){var t;if(e&&e.constructor&&e.constructor.toString){t=e.constructor.toString().match(/function\s*(\w+)/);if(t&&t.length===2)return t[1]}return void 0},n.prototype._sanitizeCondition=function(e){var t,n;n=typeof e,t=this._getObjectClassName(e);if("cls.Expression"!==t&&"string"!==n)throw new Error("condition must be a string or cls.Expression instance");if("cls.Expression"===n||"cls.Expression"===t)e=e.toString();return e},n.prototype._sanitizeName=function(e,t){if("string"!=typeof e)throw new Error(""+t+" must be a string");return e},n.prototype._sanitizeField=function(e){var t;return t=this._sanitizeName(e,"field name"),this.options.autoQuoteFieldNames?""+this.options.nameQuoteCharacter+t+this.options.nameQuoteCharacter:t},n.prototype._sanitizeTable=function(e){var t;return t=this._sanitizeName(e,"table name"),this.options.autoQuoteTableNames?""+this.options.nameQuoteCharacter+t+this.options.nameQuoteCharacter:t},n.prototype._sanitizeAlias=function(e){return this._sanitizeName(e,"alias")},n.prototype._sanitizeLimitOffset=function(e){e=parseInt(e);if(0>e||isNaN(e))throw new Error("limit/offset must be >=0");return e},n.prototype._sanitizeValue=function(e){var t;t=typeof e;if(null!==e&&"string"!==t&&"number"!==t&&"boolean"!==t)throw new Error("field value must be a string, number, boolean or null");return e},n.prototype._formatValue=function(e){return null===e?e="NULL":"boolean"==typeof e?e=e?"TRUE":"FALSE":"number"!=typeof e&&!1===this.options.usingValuePlaceholders&&(e="'"+e+"'"),e},n}(t.Cloneable),t.Expression=function(){function t(){this.toString=u(this.toString,this),this.or=u(this.or,this),this.and=u(this.and,this),this.end=u(this.end,this),this.or_begin=u(this.or_begin,this),this.and_begin=u(this.and_begin,this);var e=this;this.tree={parent:null,nodes:[]},this.current=this.tree,this._begin=function(t){var n;return n={type:t,parent:e.current,nodes:[]},e.current.nodes.push(n),e.current=e.current.nodes[e.current.nodes.length-1],e}}var e;return t.prototype.tree=null,t.prototype.current=null,t.prototype.and_begin=function(){return this._begin("AND")},t.prototype.or_begin=function(){return this._begin("OR")},t.prototype.end=function(){if(!this.current.parent)throw new Error("begin() needs to be called");return this.current=this.current.parent,this},t.prototype.and=function(e){if(!e||"string"!=typeof e)throw new Error("expr must be a string");return this.current.nodes.push({type:"AND",expr:e}),this},t.prototype.or=function(e){if(!e||"string"!=typeof e)throw new Error("expr must be a string");return this.current.nodes.push({type:"OR",expr:e}),this},t.prototype.toString=function(){if(null!==this.current.parent)throw new Error("end() needs to be called");return e(this.tree)},e=function(t){var n,r,i,s,o,u;i="",u=t.nodes;for(s=0,o=u.length;s<o;s++)n=u[s],n.expr!=null?r=n.expr:(r=e(n),""!==r&&(r="("+r+")")),""!==r&&(""!==i&&(i+=" "+n.type+" "),i+=r);return i},t}(),t.Block=function(e){function t(){return t.__super__.constructor.apply(this,arguments)}return o(t,e),t.prototype.exposedMethods=function(){var e,t,n;t={};for(e in this){if(!s.call(this,e))continue;n=this[e],typeof n=="Function"&&e.charAt(0)!=="_"&&e!=="buildStr"&&(t[e]=n)}return t},t.prototype.buildStr=function(e){return""},t}(t.BaseBuilder),t.StringBlock=function(e){function t(e,n){t.__super__.constructor.call(this,e),this.str=n}return o(t,e),t.prototype.buildStr=function(e){return this.str},t}(t.Block),t.FromTableBlock=function(e){function t(e){t.__super__.constructor.call(this,e),this.froms=[]}return o(t,e),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})},t.prototype.buildStr=function(e){var t,n,r,i,s;if(0>=this.froms.length)throw new Error("from() needs to be called");n="",s=this.froms;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}(t.Block),t.UpdateTableBlock=function(e){function t(e){t.__super__.constructor.call(this,e),this.tables=[]}return o(t,e),t.prototype.table=function(e,t){return t==null&&(t=null),e=this._sanitizeTable(e),t&&(t=this._sanitizeAlias(t)),this.tables.push({name:e,alias:t})},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}(t.Block),t.IntoTableBlock=function(e){function t(e){t.__super__.constructor.call(this,e),this.table=null}return o(t,e),t.prototype.table=function(e){return e=this._sanitizeTable(e)},t.prototype.buildStr=function(e){if(!this.table)throw new Error("into() needs to be called");return this.table},t}(t.Block),t.GetFieldBlock=function(e){function t(e){this.field=u(this.field,this),t.__super__.constructor.call(this,e),this.fields=[]}return o(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}(t.Block),t.SetFieldBlock=function(e){function t(e){this.set=u(this.set,this),t.__super__.constructor.call(this,e),this.fields={}}return o(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,i,o;n=function(){var e,n;e=this.fields,n=[];for(t in e){if(!s.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(i=0,o=n.length;i<o;i++)t=n[i],""!==r&&(r+=", "),r+=""+t+" = "+this._formatValue(this.fields[t]);return"SET "+r},t}(t.Block),t.InsertIntoFieldBlock=function(e){function t(e){this.set=u(this.set,this),t.__super__.constructor.call(this,e),this.fields={}}return o(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,i,o,u,a;n=function(){var e,t;e=this.fields,t=[];for(i in e){if(!s.call(e,i))continue;t.push(i)}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}(t.Block),e=function(e){function t(e){t.__super__.constructor.call(this,e),this.use_distinct=!1}return o(t,e),t.prototype.distinct=function(){return this.useDistinct=!0},t.prototype.buildStr=function(e){return this.useDistinct?"DISTINCT":""},t}(t.Block),t.GroupByBlock=function(e){function t(e){this.group=u(this.group,this),t.__super__.constructor.call(this,e),this.groups=[]}return o(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}(t.Block),t.OffsetBlock=function(e){function t(e){this.offset=u(this.offset,this),t.__super__.constructor.call(this,e),this.offsets=null}return o(t,e),t.prototype.offset=function(e){return e=this._sanitizeLimitOffset(e),this.offsets=e},t.prototype.buildStr=function(e){if(!this.offsets("OFFSET "+this.offsets))return""},t}(t.Block),t.WhereBlock=function(e){function t(e){this.where=u(this.where,this),t.__super__.constructor.call(this,e),this.wheres=[]}return o(t,e),t.prototype.where=function(e){e=this._sanitizeCondition(e);if(""!==e)return this.wheres.push(e)},t.prototype.buildStr=function(e){if(!this.offsets("OFFSET "+this.offsets))return""},t}(t.Block),t.OrderByBlock=function(e){function t(e){this.order=u(this.order,this),t.__super__.constructor.call(this,e),this.orders=[]}return o(t,e),t.prototype.order=function(e,t){return t==null&&(t=!0),e=this._sanitizeField(e),this.orders.push({field:e,dir:t?"ASC":"DESC"})},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;return"ORDER BY "+n}return""},t}(t.Block),t.LimitBlock=function(e){function t(e){this.limit=u(this.limit,this),t.__super__.constructor.call(this,e),this.limits=null}return o(t,e),t.prototype.limit=function(e){return e=this._sanitizeLimitOffset(e),this.limits=e},t.prototype.buildStr=function(e){if(!this.limits("LIMIT "+this.limits))return""},t}(t.Block),t.JoinBlock=function(e){function t(e){this.outer_join=u(this.outer_join,this),this.right_join=u(this.right_join,this),this.left_join=u(this.left_join,this),this.join=u(this.join,this),t.__super__.constructor.call(this,e),this.joins=[]}return o(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.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+=" "+t.type+" JOIN "+t.table,t.alias&&(n+=" `"+t.alias+"`"),t.condition&&(n+=" ON ("+t.condition+")");return n},t}(t.Block),t.QueryBuilder=function(e){function t(e){var t,n,r,i,o,u,a,f,l=this;this.block=e,a=this.blocks;for(o=0,u=a.length;o<u;o++){t=a[o],f=t.exposedMethods(),i=function(e,t){return l[e]=function(){return t.apply(l,arguments),l}};for(r in f){if(!s.call(f,r))continue;n=f[r];if(this[r]!=null)throw new Error(_getObjectClassName(this)+("already has a builder method called "+r));i(r,n)}}({toString:function(){return function(){var e,n,r,i;r=this.blocks,i=[];for(e=0,n=r.length;e<n;e++)t=r[e],i.push(t.buildStr(this));return i}.call(l).join(" ")}})}return o(t,e),t}(t.BaseBuilder),t.Select=function(e){function n(e){var r;r=[new t.StringBlock(e,"SELECT"),new t.GetFieldBlock(e),new t.FromTableBlock(e),new t.JoinBlock(e),new t.WhereBlock(e),new t.GroupByBlock(e),new t.OrderByBlock(e),new t.LimitBlock(e),new t.OffsetBlock(e)],n.__super__.constructor.call(this,r)}return o(n,e),n}(t.QueryBuilder),t.Update=function(e){function n(e){var r;r=[new t.StringBlock(e,"UPDATE"),new t.UpdateTableBlock(e),new t.SetFieldBlock(e),new t.WhereBlock(e),new t.OrderByBlock(e),new t.LimitBlock(e)],n.__super__.constructor.call(this,r)}return o(n,e),n}(t.QueryBuilder),t.Delete=function(e){function n(e){var r;r=[new t.StringBlock(e,"DELETE"),new t.FromTableBlock(e),new t.JoinBlock(e),new t.WhereBlock(e),new t.OrderByBlock(e),new t.LimitBlock(e)],n.__super__.constructor.call(this,r)}return o(n,e),n}(t.QueryBuilder),t.Insert=function(e){function n(e){var r;r=[new t.StringBlock(e,"INSERT"),new t.IntoTableBlock(e),new t.InsertIntoFieldBlock(e)],n.__super__.constructor.call(this,r)}return o(n,e),n}(t.BaseBuilder),n={expr:function(){return new t.Expression},select:function(e){return new t.Select(e)},update:function(e){return new t.Update(e)},insert:function(e){return new t.Insert(e)},"delete":function(e){return new t.Delete(e)}},n.remove=n["delete"],n.classes=t,(typeof define!="undefined"&&define!==null?define.amd:void 0)?define(function(){return n}):(typeof module!="undefined"&&module!==null?module.exports:void 0)?module.export=n:typeof window!="undefined"&&window!==null&&(window.squel=n)}).call(this);
View
54 src/squel.coffee
@@ -281,7 +281,7 @@ class cls.Expression
# that you do so using one or more custom building blocks.
#
# Original idea posted in https://github.com/hiddentao/export/issues/10#issuecomment-15016427
-class cls.BuildingBlock extends cls.BaseBuilder
+class cls.Block extends cls.BaseBuilder
# Get input methods to expose within the query builder.
#
# By default all methods except the following get returned:
@@ -311,7 +311,7 @@ class cls.BuildingBlock extends cls.BaseBuilder
# A String which always gets output
-class cls.StringBlock extends cls.BuildingBlock
+class cls.StringBlock extends cls.Block
constructor: (options, str) ->
super options
@str = str
@@ -322,7 +322,7 @@ class cls.StringBlock extends cls.BuildingBlock
# FROM table
-class cls.FromTableBlock extends cls.BuildingBlock
+class cls.FromTableBlock extends cls.Block
constructor: (options) ->
super options
@froms = []
@@ -353,7 +353,7 @@ class cls.FromTableBlock extends cls.BuildingBlock
# UPDATE table
-class cls.UpdateTableBlock extends cls.BuildingBlock
+class cls.UpdateTableBlock extends cls.Block
constructor: (options) ->
super options
@tables = []
@@ -382,14 +382,14 @@ class cls.UpdateTableBlock extends cls.BuildingBlock
# INTO table
-class cls.IntoTableBlock extends cls.BuildingBlock
+class cls.IntoTableBlock extends cls.Block
constructor: (options) ->
super options
@table = null
# Into given table.
- table: (table) =>
- table = @_sanitizeTable(table)
+ into: (table) =>
+ @table = @_sanitizeTable(table)
buildStr: (queryBuilder) ->
if not @table then throw new Error "into() needs to be called"
@@ -398,7 +398,7 @@ class cls.IntoTableBlock extends cls.BuildingBlock
# Get field
-class cls.GetFieldBlock extends cls.BuildingBlock
+class cls.GetFieldBlock extends cls.Block
constructor: (options) ->
super options
@fields = []
@@ -429,7 +429,7 @@ class cls.GetFieldBlock extends cls.BuildingBlock
# SET field=value
-class cls.SetFieldBlock extends cls.BuildingBlock
+class cls.SetFieldBlock extends cls.Block
constructor: (options) ->
super options
@fields = {}
@@ -455,7 +455,7 @@ class cls.SetFieldBlock extends cls.BuildingBlock
# INSERT INTO ... field ... value
-class cls.InsertIntoFieldBlock extends cls.BuildingBlock
+class cls.InsertIntoFieldBlock extends cls.Block
constructor: (options) ->
super options
@fields = {}
@@ -487,7 +487,7 @@ class cls.InsertIntoFieldBlock extends cls.BuildingBlock
# DISTINCT
-class cls.DistinctBlock extends cls.BuildingBlock
+class cls.DistinctBlock extends cls.Block
constructor: (options) ->
super options
@useDistinct = false
@@ -502,7 +502,7 @@ class cls.DistinctBlock extends cls.BuildingBlock
# GROUP BY
-class cls.GroupByBlock extends cls.BuildingBlock
+class cls.GroupByBlock extends cls.Block
constructor: (options) ->
super options
@groups = []
@@ -525,7 +525,7 @@ class cls.GroupByBlock extends cls.BuildingBlock
# OFFSET x
-class cls.OffsetBlock extends cls.BuildingBlock
+class cls.OffsetBlock extends cls.Block
constructor: (options) ->
super options
@offsets = null
@@ -539,11 +539,11 @@ class cls.OffsetBlock extends cls.BuildingBlock
@offsets = start
buildStr: (queryBuilder) ->
- if (@offsets) "OFFSET #{@offsets}" else ""
+ if @offsets then "OFFSET #{@offsets}" else ""
# WHERE
-class cls.WhereBlock extends cls.BuildingBlock
+class cls.WhereBlock extends cls.Block
constructor: (options) ->
super options
@wheres = []
@@ -557,11 +557,11 @@ class cls.WhereBlock extends cls.BuildingBlock
@wheres.push condition
buildStr: (queryBuilder) ->
- if (@offsets) "OFFSET #{@offsets}" else ""
+ if 0 < @wheres.length then "WHERE (" + @wheres.join(") AND (") + ")" else ""
# ORDER BY
-class cls.OrderByBlock extends cls.BuildingBlock
+class cls.OrderByBlock extends cls.Block
constructor: (options) ->
super options
@orders = []
@@ -573,21 +573,21 @@ class cls.OrderByBlock extends cls.BuildingBlock
field = @_sanitizeField(field)
@orders.push
field: field
- dir: if asc then "ASC" else "DESC"
+ dir: if asc then true else false
buildStr: (queryBuilder) ->
if 0 < @orders.length
orders = ""
for o in @orders
orders += ", " if "" isnt orders
- orders += "#{o.field} #{o.dir}"
+ orders += "#{o.field} #{if o.dir then 'ASC' else 'DESC'}"
"ORDER BY #{orders}"
else
""
# LIMIT
-class cls.LimitBlock extends cls.BuildingBlock
+class cls.LimitBlock extends cls.Block
constructor: (options) ->
super options
@limits = null
@@ -602,12 +602,12 @@ class cls.LimitBlock extends cls.BuildingBlock
buildStr: (queryBuilder) ->
- if @limits "LIMIT #{@limits}" else ""
+ if @limits then "LIMIT #{@limits}" else ""
# JOIN
-class cls.JoinBlock extends cls.BuildingBlock
+class cls.JoinBlock extends cls.Block
constructor: (options) ->
super options
@joins = []
@@ -620,12 +620,11 @@ class cls.JoinBlock extends cls.BuildingBlock
# 'alias' is an optional alias for the table name.
#
# 'condition' is an optional condition (containing an SQL expression) for the JOIN. If this is an instance of
- # an expression builder then it will only get evaluated during the final query string construction phase in
- # toString().
+ # an expression builder then it gets evaluated straight away.
#
# 'type' must be either one of INNER, OUTER, LEFT or RIGHT. Default is 'INNER'.
#
- join: (table, alias, condition, type = 'INNER') =>
+ join: (table, alias = null, condition = null, type = 'INNER') =>
table = @_sanitizeTable(table)
alias = @_sanitizeAlias(alias) if alias
condition = @_sanitizeCondition(condition) if condition
@@ -657,7 +656,8 @@ class cls.JoinBlock extends cls.BuildingBlock
joins = ""
for j in (@joins or [])
- joins += " #{j.type} JOIN #{j.table}"
+ if joins isnt "" then joins += " "
+ joins += "#{j.type} JOIN #{j.table}"
joins += " `#{j.alias}`" if j.alias
joins += " ON (#{j.condition})" if j.condition
@@ -700,7 +700,7 @@ class cls.QueryBuilder extends cls.BaseBuilder
# Get the final fully constructed query string.
toString: =>
- (block.buildStr(@) for block in @blocks).join(" ")
+ (block.buildStr(@) for block in @blocks).join(' ')
View
874 test/blocks.test.coffee
@@ -31,17 +31,28 @@ test = testCreator()
test['Blocks'] =
- 'BuildingBlock':
+ 'Block base class':
beforeEach: ->
- @inst = new squel.classes.BuildingBlock()
+ @inst = new squel.classes.Block()
'instanceof of BaseBuilder': ->
assert.instanceOf @inst, squel.classes.BaseBuilder
- 'buildStr': ->
+ 'options': ->
+ expectedOptions = _.extend {}, squel.classes.DefaultQueryBuilderOptions,
+ usingValuePlaceholders: true
+ dummy: true
+
+ @inst = new squel.classes.Block
+ usingValuePlaceholders: true
+ dummy: true
+
+ assert.same expectedOptions, @inst.options
+
+ 'buildStr()': ->
assert.same '', @inst.buildStr()
- 'exposedMethods':
+ 'exposedMethods()':
'returns methods': ->
@inst['method1'] = -> return false
@inst['method2'] = -> return false
@@ -62,13 +73,858 @@ test['Blocks'] =
'StringBlock':
beforeEach: ->
- @inst = new squel.classes.StringBlock()
+ @cls = squel.classes.StringBlock
+ @inst = new @cls
+
+ 'instanceof of Block': ->
+ assert.instanceOf @inst, squel.classes.Block
+
+ 'calls base constructor': ->
+ spy = test.mocker.spy(squel.classes.Block.prototype, 'constructor')
+
+ @inst = new @cls
+ dummy: true
+
+ assert.ok spy.calledWithExactly
+ dummy:true
+
+ 'buildStr()':
+ 'returns the string as is': ->
+ @inst = new @cls {}, 'TAG'
+
+ assert.same 'TAG', @inst.buildStr()
+
+
+
+ 'FromTableBlock':
+ beforeEach: ->
+ @cls = squel.classes.FromTableBlock
+ @inst = new @cls()
+
+ 'instanceof of Block': ->
+ assert.instanceOf @inst, squel.classes.Block
+
+ 'calls base constructor': ->
+ spy = test.mocker.spy(squel.classes.Block.prototype, 'constructor')
+
+ @inst = new @cls
+ dummy: true
+
+ assert.ok spy.calledWithExactly
+ dummy:true
+
+ 'initial field values': ->
+ assert.same [], @inst.froms
+
+ 'from()':
+ 'saves inputs': ->
+ @inst.from('table1')
+ @inst.from('table2', 'alias2')
+ @inst.from('table3')
+
+ expectedFroms = [
+ {
+ name: 'table1',
+ alias: null
+ },
+ {
+ name: 'table2',
+ alias: 'alias2'
+ },
+ {
+ name: 'table3',
+ alias: null
+ }
+ ]
+
+ assert.same expectedFroms, @inst.froms
+
+ 'sanitizes inputs': ->
+ sanitizeTableSpy = test.mocker.stub @cls.prototype, '_sanitizeTable', -> return '_t'
+ sanitizeAliasSpy = test.mocker.stub @cls.prototype, '_sanitizeAlias', -> return '_a'
+
+ @inst.from('table', 'alias')
+
+ assert.ok sanitizeTableSpy.calledWithExactly 'table'
+ assert.ok sanitizeAliasSpy.calledWithExactly 'alias'
+
+ assert.same [ { name: '_t', alias: '_a' }], @inst.froms
+
+ 'buildStr()':
+ 'requires at least one table to have been provided': ->
+ try
+ @inst.buildStr()
+ throw new Error 'should not reach here'
+ catch err
+ assert.same 'Error: from() needs to be called', err.toString()
+
+ 'returns formatted query phrase': ->
+ @inst.from('table1')
+ @inst.from('table2', 'alias2')
+ @inst.from('table3')
+
+ assert.same 'FROM table1, table2 `alias2`, table3', @inst.buildStr()
+
+
+
+
+ 'UpdateTableBlock':
+ beforeEach: ->
+ @cls = squel.classes.UpdateTableBlock
+ @inst = new @cls()
+
+ 'instanceof of Block': ->
+ assert.instanceOf @inst, squel.classes.Block
+
+ 'calls base constructor': ->
+ spy = test.mocker.spy(squel.classes.Block.prototype, 'constructor')
+
+ @inst = new @cls
+ dummy: true
+
+ assert.ok spy.calledWithExactly
+ dummy:true
+
+ 'initial field values': ->
+ assert.same [], @inst.tables
+
+ 'table()':
+ 'saves inputs': ->
+ @inst.table('table1')
+ @inst.table('table2', 'alias2')
+ @inst.table('table3')
+
+ expected = [
+ {
+ name: 'table1',
+ alias: null
+ },
+ {
+ name: 'table2',
+ alias: 'alias2'
+ },
+ {
+ name: 'table3',
+ alias: null
+ }
+ ]
+
+ assert.same expected, @inst.tables
+
+ 'sanitizes inputs': ->
+ sanitizeTableSpy = test.mocker.stub @cls.prototype, '_sanitizeTable', -> return '_t'
+ sanitizeAliasSpy = test.mocker.stub @cls.prototype, '_sanitizeAlias', -> return '_a'
+
+ @inst.table('table', 'alias')
+
+ assert.ok sanitizeTableSpy.calledWithExactly 'table'
+ assert.ok sanitizeAliasSpy.calledWithExactly 'alias'
+
+ assert.same [ { name: '_t', alias: '_a' }], @inst.tables
+
+ 'buildStr()':
+ 'requires at least one table to have been provided': ->
+ try
+ @inst.buildStr()
+ throw new Error 'should not reach here'
+ catch err
+ assert.same 'Error: table() needs to be called', err.toString()
+
+ 'returns formatted query phrase': ->
+ @inst.table('table1')
+ @inst.table('table2', 'alias2')
+ @inst.table('table3')
+
+ assert.same 'table1, table2 AS `alias2`, table3', @inst.buildStr()
+
+
+
+
+ 'IntoTableBlock':
+ beforeEach: ->
+ @cls = squel.classes.IntoTableBlock
+ @inst = new @cls()
+
+ 'instanceof of Block': ->
+ assert.instanceOf @inst, squel.classes.Block
+
+ 'calls base constructor': ->
+ spy = test.mocker.spy(squel.classes.Block.prototype, 'constructor')
+
+ @inst = new @cls
+ dummy: true
+
+ assert.ok spy.calledWithExactly
+ dummy:true
+
+ 'initial field values': ->
+ assert.same null, @inst.table
+
+ 'into()':
+ 'saves inputs': ->
+ @inst.into('table1')
+ @inst.into('table2')
+ @inst.into('table3')
+
+ assert.same 'table3', @inst.table
+
+ 'sanitizes inputs': ->
+ sanitizeTableSpy = test.mocker.stub @cls.prototype, '_sanitizeTable', -> return '_t'
+
+ @inst.into('table')
+
+ assert.ok sanitizeTableSpy.calledWithExactly 'table'
+
+ assert.same '_t', @inst.table
+
+ 'buildStr()':
+ 'requires table to have been provided': ->
+ try
+ @inst.buildStr()
+ throw new Error 'should not reach here'
+ catch err
+ assert.same 'Error: into() needs to be called', err.toString()
+
+ 'returns formatted query phrase': ->
+ @inst.into('table1')
+
+ assert.same 'table1', @inst.buildStr()
+
+
+
+
+ 'GetFieldBlock':
+ beforeEach: ->
+ @cls = squel.classes.GetFieldBlock
+ @inst = new @cls()
+
+ 'instanceof of Block': ->
+ assert.instanceOf @inst, squel.classes.Block
+
+ 'calls base constructor': ->
+ spy = test.mocker.spy(squel.classes.Block.prototype, 'constructor')
+
+ @inst = new @cls
+ dummy: true
+
+ assert.ok spy.calledWithExactly
+ dummy:true
+
+ 'initial field values': ->
+ assert.same [], @inst.fields
+
+ 'field()':
+ 'saves inputs': ->
+ @inst.field('field1')
+ @inst.field('field2', 'alias2')
+ @inst.field('field3')
+
+ expected = [
+ {
+ name: 'field1',
+ alias: null
+ },
+ {
+ name: 'field2',
+ alias: 'alias2'
+ },
+ {
+ name: 'field3',
+ alias: null
+ }
+ ]
+
+ assert.same expected, @inst.fields
+
+ 'sanitizes inputs': ->
+ sanitizeFieldSpy = test.mocker.stub @cls.prototype, '_sanitizeField', -> return '_f'
+ sanitizeAliasSpy = test.mocker.stub @cls.prototype, '_sanitizeAlias', -> return '_a'
+
+ @inst.field('field1', 'alias1')
+
+ assert.ok sanitizeFieldSpy.calledWithExactly 'field1'
+ assert.ok sanitizeAliasSpy.calledWithExactly 'alias1'
+
+ assert.same [ { name: '_f', alias: '_a' } ], @inst.fields
+
+ 'buildStr()':
+ 'returns all fields when none provided': ->
+ @inst.fields = []
+ assert.same '*', @inst.buildStr()
+
+ 'returns formatted query phrase': ->
+ @inst.field('field1')
+ @inst.field('field2', 'alias2')
+ @inst.field('field3')
+
+ assert.same 'field1, field2 AS "alias2", field3', @inst.buildStr()
+
+
+
+
+ 'SetFieldBlock':
+ beforeEach: ->
+ @cls = squel.classes.SetFieldBlock
+ @inst = new @cls()
+
+ 'instanceof of Block': ->
+ assert.instanceOf @inst, squel.classes.Block
+
+ 'calls base constructor': ->
+ spy = test.mocker.spy(squel.classes.Block.prototype, 'constructor')
+
+ @inst = new @cls
+ dummy: true
+
+ assert.ok spy.calledWithExactly
+ dummy:true
+
+ 'initial field values': ->
+ assert.same [], @inst.fields
+
+ 'set()':
+ 'saves inputs': ->
+ @inst.set('field1', 'value1')
+ @inst.set('field2', 'value2')
+ @inst.set('field3', 'value3')
+
+ expected =
+ 'field1': 'value1',
+ 'field2': 'value2',
+ 'field3': 'value3'
+
+ assert.same expected, @inst.fields
+
+ 'sanitizes inputs': ->
+ sanitizeFieldSpy = test.mocker.stub @cls.prototype, '_sanitizeField', -> return '_f'
+ sanitizeValueSpy = test.mocker.stub @cls.prototype, '_sanitizeValue', -> return '_v'
+
+ @inst.set('field1', 'value1')
+
+ assert.ok sanitizeFieldSpy.calledWithExactly 'field1'
+ assert.ok sanitizeValueSpy.calledWithExactly 'value1'
+
+ assert.same { '_f': '_v' }, @inst.fields
+
+ 'buildStr()':
+ 'needs at least one field to have been provided': ->
+ @inst.fields = []
+ try
+ @inst.buildStr()
+ throw new Error 'should not reach here'
+ catch err
+ assert.same 'Error: set() needs to be called', err.toString()
+
+ 'calls formatValue() for each field value': ->
+ formatValueSpy = test.mocker.stub @cls.prototype, '_formatValue', (v) -> return "[#{v}]"
+
+ @inst.fields =
+ 'field1': 'value1',
+ 'field2': 'value2',
+ 'field3': 'value3'
+
+ assert.same 'SET field1 = [value1], field2 = [value2], field3 = [value3]', @inst.buildStr()
+
+ assert.ok formatValueSpy.calledThrice
+ assert.ok formatValueSpy.calledWithExactly 'value1'
+ assert.ok formatValueSpy.calledWithExactly 'value2'
+ assert.ok formatValueSpy.calledWithExactly 'value3'
+
+
+
+
+
+ 'InsertIntoFieldBlock':
+ beforeEach: ->
+ @cls = squel.classes.InsertIntoFieldBlock
+ @inst = new @cls()
+
+ 'instanceof of Block': ->
+ assert.instanceOf @inst, squel.classes.Block
+
+ 'calls base constructor': ->
+ spy = test.mocker.spy(squel.classes.Block.prototype, 'constructor')
+
+ @inst = new @cls
+ dummy: true
+
+ assert.ok spy.calledWithExactly
+ dummy:true
+
+ 'initial field values': ->
+ assert.same [], @inst.fields
+
+ 'set()':
+ 'saves inputs': ->
+ @inst.set('field1', 'value1')
+ @inst.set('field2', 'value2')
+ @inst.set('field3', 'value3')
+
+ expected =
+ 'field1': 'value1',
+ 'field2': 'value2',
+ 'field3': 'value3'
+
+ assert.same expected, @inst.fields
+
+ 'sanitizes inputs': ->
+ sanitizeFieldSpy = test.mocker.stub @cls.prototype, '_sanitizeField', -> return '_f'
+ sanitizeValueSpy = test.mocker.stub @cls.prototype, '_sanitizeValue', -> return '_v'
+
+ @inst.set('field1', 'value1')
+
+ assert.ok sanitizeFieldSpy.calledWithExactly 'field1'
+ assert.ok sanitizeValueSpy.calledWithExactly 'value1'
+
+ assert.same { '_f': '_v' }, @inst.fields
+
+ 'buildStr()':
+ 'needs at least one field to have been provided': ->
+ @inst.fields = []
+ try
+ @inst.buildStr()
+ throw new Error 'should not reach here'
+ catch err
+ assert.same 'Error: set() needs to be called', err.toString()
+
+ 'calls formatValue() for each field value': ->
+ formatValueSpy = test.mocker.stub @cls.prototype, '_formatValue', (v) -> return "[#{v}]"
+
+ @inst.fields =
+ 'field1': 'value1',
+ 'field2': 'value2',
+ 'field3': 'value3'
+
+ assert.same '(field1, field2, field3) VALUES ([value1], [value2], [value3])', @inst.buildStr()
+
+ assert.ok formatValueSpy.calledThrice
+ assert.ok formatValueSpy.calledWithExactly 'value1'
+ assert.ok formatValueSpy.calledWithExactly 'value2'
+ assert.ok formatValueSpy.calledWithExactly 'value3'
+
+
+
+
+ 'DistinctBlock':
+ beforeEach: ->
+ @cls = squel.classes.DistinctBlock
+ @inst = new @cls()
+
+ 'instanceof of Block': ->
+ assert.instanceOf @inst, squel.classes.Block
+
+ 'calls base constructor': ->
+ spy = test.mocker.spy(squel.classes.Block.prototype, 'constructor')
+
+ @inst = new @cls
+ dummy: true
+
+ assert.ok spy.calledWithExactly
+ dummy:true
+
+ 'initial field values': ->
+ assert.same false, @inst.useDistinct
+
+ 'distinct()':
+ 'sets the flat': ->
+ @inst.distinct()
+
+ assert.same true, @inst.useDistinct
+
+ @inst.distinct()
+
+ assert.same true, @inst.useDistinct
+
+ 'buildStr()':
+ 'output nothing if not set': ->
+ @inst.useDistinct = false
+ assert.same '', @inst.buildStr()
+
+ 'output DISTINCT if set': ->
+ @inst.useDistinct = true
+ assert.same 'DISTINCT', @inst.buildStr()
+
+
+
+
+ 'GroupByBlock':
+ beforeEach: ->
+ @cls = squel.classes.GroupByBlock
+ @inst = new @cls()
+
+ 'instanceof of Block': ->
+ assert.instanceOf @inst, squel.classes.Block
+
+ 'calls base constructor': ->
+ spy = test.mocker.spy(squel.classes.Block.prototype, 'constructor')
+
+ @inst = new @cls
+ dummy: true
+
+ assert.ok spy.calledWithExactly
+ dummy:true
+
+ 'initial field values': ->
+ assert.same [], @inst.groups
+
+ 'group()':
+ 'adds to list': ->
+ @inst.group('field1')
+ @inst.group('field2')
+
+ assert.same ['field1', 'field2'], @inst.groups
+
+ 'sanitizes inputs': ->
+ sanitizeFieldSpy = test.mocker.stub @cls.prototype, '_sanitizeField', -> return '_f'
+
+ @inst.group('field1')
+
+ assert.ok sanitizeFieldSpy.calledWithExactly 'field1'
+
+ assert.same ['_f'], @inst.groups
+
+
+ 'buildStr()':
+ 'output nothing if no fields set': ->
+ @inst.groups = []
+ assert.same '', @inst.buildStr()
+
+ 'output GROUP BY': ->
+ @inst.group('field1')
+ @inst.group('field2')
+
+ assert.same 'GROUP BY field1, field2', @inst.buildStr()
+
+
+
+
+ 'OffsetBlock':
+ beforeEach: ->
+ @cls = squel.classes.OffsetBlock
+ @inst = new @cls()
+
+ 'instanceof of Block': ->
+ assert.instanceOf @inst, squel.classes.Block
+
+ 'calls base constructor': ->
+ spy = test.mocker.spy(squel.classes.Block.prototype, 'constructor')
+
+ @inst = new @cls
+ dummy: true
+
+ assert.ok spy.calledWithExactly
+ dummy:true
+
+ 'initial field values': ->
+ assert.same null, @inst.offsets
+
+ 'offset()':
+ 'set value': ->
+ @inst.offset(1)
+
+ assert.same 1, @inst.offsets
+
+ @inst.offset(22)
+
+ assert.same 22, @inst.offsets
+
+ 'sanitizes inputs': ->
+ sanitizeSpy = test.mocker.stub @cls.prototype, '_sanitizeLimitOffset', -> return 234
+
+ @inst.offset(23)
+
+ assert.ok sanitizeSpy.calledWithExactly 23
+
+ assert.same 234, @inst.offsets
+
+ 'buildStr()':
+ 'output nothing if not set': ->
+ @inst.offsets = null
+ assert.same '', @inst.buildStr()
+
+ 'output OFFSET': ->
+ @inst.offset(12)
+
+ assert.same 'OFFSET 12', @inst.buildStr()
+
+
+
+ 'WhereBlock':
+ beforeEach: ->
+ @cls = squel.classes.WhereBlock
+ @inst = new @cls()
+
+ 'instanceof of Block': ->
+ assert.instanceOf @inst, squel.classes.Block
+
+ 'calls base constructor': ->
+ spy = test.mocker.spy(squel.classes.Block.prototype, 'constructor')
+
+ @inst = new @cls
+ dummy: true
+
+ assert.ok spy.calledWithExactly
+ dummy:true
+
+ 'initial field values': ->
+ assert.same [], @inst.wheres
+
+ 'where()':
+ 'adds to list': ->
+ @inst.where('a = 1')
+ @inst.where('b = 2 OR c = 3')
+
+ assert.same [
+ 'a = 1',
+ 'b = 2 OR c = 3'
+ ], @inst.wheres
+
+ 'sanitizes inputs': ->
+ sanitizeFieldSpy = test.mocker.stub @cls.prototype, '_sanitizeCondition', -> return '_c'
+
+ @inst.where('a = 1')
+
+ assert.ok sanitizeFieldSpy.calledWithExactly 'a = 1'
+
+ assert.same ['_c'], @inst.wheres
+
+
+ 'buildStr()':
+ 'output nothing if no conditions set': ->
+ @inst.wheres = []
+ assert.same '', @inst.buildStr()
+
+ 'output WHERE ': ->
+ @inst.where('a = 1')
+ @inst.where('b = 2 OR c = 3')
+
+ assert.same 'WHERE (a = 1) AND (b = 2 OR c = 3)', @inst.buildStr()
+
+
+
+ 'OrderByBlock':
+ beforeEach: ->
+ @cls = squel.classes.OrderByBlock
+ @inst = new @cls()
+
+ 'instanceof of Block': ->
+ assert.instanceOf @inst, squel.classes.Block
+
+ 'calls base constructor': ->
+ spy = test.mocker.spy(squel.classes.Block.prototype, 'constructor')
+
+ @inst = new @cls
+ dummy: true
+
+ assert.ok spy.calledWithExactly
+ dummy:true
+
+ 'initial field values': ->
+ assert.same [], @inst.orders
+
+ 'order()':
+ 'adds to list': ->
+ @inst.order('field1')
+ @inst.order('field2', false)
+ @inst.order('field3', true)
+
+ expected = [
+ {
+ field: 'field1',
+ dir: true
+ },
+ {
+ field: 'field2',
+ dir: false
+ },
+ {
+ field: 'field3',
+ dir: true
+ }
+ ]
+
+ assert.same expected, @inst.orders
+
+ 'sanitizes inputs': ->
+ sanitizeFieldSpy = test.mocker.stub @cls.prototype, '_sanitizeField', -> return '_f'
+
+ @inst.order('field1')
+
+ assert.ok sanitizeFieldSpy.calledWithExactly 'field1'
+
+ assert.same [ { field: '_f', dir: true } ], @inst.orders
+
+
+ 'buildStr()':
+ 'output nothing if nothing set': ->
+ @inst.orders = []
+ assert.same '', @inst.buildStr()
+
+ 'output ORDER BY': ->
+ @inst.order('field1')
+ @inst.order('field2', false)
+ @inst.order('field3', true)
+
+ assert.same 'ORDER BY field1 ASC, field2 DESC, field3 ASC', @inst.buildStr()
+
+
+
+
+ 'LimitBlock':
+ beforeEach: ->
+ @cls = squel.classes.LimitBlock
+ @inst = new @cls()
+
+ 'instanceof of Block': ->
+ assert.instanceOf @inst, squel.classes.Block
+
+ 'calls base constructor': ->
+ spy = test.mocker.spy(squel.classes.Block.prototype, 'constructor')
+
+ @inst = new @cls
+ dummy: true
+
+ assert.ok spy.calledWithExactly
+ dummy:true
+
+ 'initial field values': ->
+ assert.same null, @inst.limits
+
+ 'limit()':
+ 'set value': ->
+ @inst.limit(1)
+
+ assert.same 1, @inst.limits
+
+ @inst.limit(22)
+
+ assert.same 22, @inst.limits
+
+ 'sanitizes inputs': ->
+ sanitizeSpy = test.mocker.stub @cls.prototype, '_sanitizeLimitOffset', -> return 234
+
+ @inst.limit(23)
+
+ assert.ok sanitizeSpy.calledWithExactly 23
+
+ assert.same 234, @inst.limits
+
+ 'buildStr()':
+ 'output nothing if not set': ->
+ @inst.limits = null
+ assert.same '', @inst.buildStr()
+
+ 'output nothing if set to 0': ->
+ @inst.limit(0)
+ assert.same '', @inst.buildStr()
+
+ 'output LIMIT': ->
+ @inst.limit(12)
+
+ assert.same 'LIMIT 12', @inst.buildStr()
+
+
+
+ 'JoinBlock':
+ beforeEach: ->
+ @cls = squel.classes.JoinBlock
+ @inst = new @cls()
+
+ 'instanceof of Block': ->
+ assert.instanceOf @inst, squel.classes.Block
+
+ 'calls base constructor': ->
+ spy = test.mocker.spy(squel.classes.Block.prototype, 'constructor')
+
+ @inst = new @cls
+ dummy: true
+
+ assert.ok spy.calledWithExactly
+ dummy:true
+
+ 'initial field values': ->
+ assert.same [], @inst.joins
+
+ 'join()':
+ 'adds to list': ->
+ @inst.join('table1')
+ @inst.join('table2', null, 'b = 1', 'LEFT')
+ @inst.join('table3', 'alias3', 'c = 1', 'RIGHT')
+ @inst.join('table4', 'alias4', 'd = 1', 'OUTER')
+
+ expected = [
+ {
+ type: 'INNER',
+ table: 'table1',
+ alias: null,
+ condition: null
+ },
+ {
+ type: 'LEFT',
+ table: 'table2',
+ alias: null,
+ condition: 'b = 1'
+ },
+ {
+ type: 'RIGHT',
+ table: 'table3',
+ alias: 'alias3',
+ condition: 'c = 1'
+ },
+ {
+ type: 'OUTER',
+ table: 'table4',
+ alias: 'alias4',
+ condition: 'd = 1'
+ }
+ ]
+
+ assert.same expected, @inst.joins
+
+ 'sanitizes inputs': ->
+ sanitizeTableSpy = test.mocker.stub @cls.prototype, '_sanitizeTable', -> return '_t'
+ sanitizeAliasSpy = test.mocker.stub @cls.prototype, '_sanitizeAlias', -> return '_a'
+ sanitizeConditionSpy = test.mocker.stub @cls.prototype, '_sanitizeCondition', -> return '_c'
+
+ @inst.join('table1', 'alias1', 'a = 1')
+
+ assert.ok sanitizeTableSpy.calledWithExactly 'table1'
+ assert.ok sanitizeAliasSpy.calledWithExactly 'alias1'
+ assert.ok sanitizeConditionSpy.calledWithExactly 'a = 1'
+
+ expected = [
+ {
+ type: 'INNER',
+ table: '_t',
+ alias: '_a',
+ condition: '_c'
+ }
+ ]
+
+ assert.same expected, @inst.joins
+
+
+ 'left_join()':
+ 'calls join()': ->
+ joinSpy = test.mocker.stub(@inst, 'join')
+
+ @inst.left_join('t', 'a', 'c')
+
+ assert.ok joinSpy.calledOnce
+ assert.ok joinSpy.calledWithExactly('t', 'a', 'c', 'LEFT')
+
+
+ 'buildStr()':
+ 'output nothing if nothing set': ->
+ @inst.joins = []
+ assert.same '', @inst.buildStr()
- 'instanceof of BuildingBlock': ->
- assert.instanceOf @inst, squel.classes.BuildingBlock
+ 'output JOINs': ->
+ @inst.join('table1')
+ @inst.join('table2', null, 'b = 1', 'LEFT')
+ @inst.join('table3', 'alias3', 'c = 1', 'RIGHT')
- 'outputs String': ->
- assert.instanceOf @inst, squel.classes.BuildingBlock
+ assert.same 'INNER JOIN table1 LEFT JOIN table2 ON (b = 1) RIGHT JOIN table3 `alias3` ON (c = 1)', @inst.buildStr()
Please sign in to comment.
Something went wrong with that request. Please try again.