Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

QueryBuilder base class, exporting query builders for hackability.

  • Loading branch information...
commit 471d30e308b8079489650fd5e595d9aeada492df 1 parent 785696e
@nl0 authored
Showing with 90 additions and 54 deletions.
  1. +51 −44 docs/squel.html
  2. +24 −6 squel.js
  3. +1 −1  squel.min.js
  4. +14 −3 src/squel.coffee
View
95 docs/squel.html
@@ -117,37 +117,37 @@
<span class="k">else</span> <span class="k">if</span> <span class="s">&quot;number&quot;</span> <span class="o">isnt</span> <span class="k">typeof</span> <span class="nx">value</span>
<span class="k">if</span> <span class="kc">false</span> <span class="o">is</span> <span class="nx">options</span><span class="p">.</span><span class="nx">usingValuePlaceholders</span>
<span class="nv">value = </span><span class="s">&quot;&#39;</span><span class="si">#{</span><span class="nx">value</span><span class="si">}</span><span class="s">&#39;&quot;</span>
- <span class="nx">value</span></pre></div> </td> </tr> <tr id="section-27"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-27">&#182;</a> </div> <p>Base class for query builders which support WHERE, ORDER and LIMIT clauses.</p> </td> <td class="code"> <div class="highlight"><pre><span class="k">class</span> <span class="nx">WhereOrderLimit</span>
+ <span class="nx">value</span></pre></div> </td> </tr> <tr id="section-27"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-27">&#182;</a> </div> <p>Base class for all query builders</p> </td> <td class="code"> <div class="highlight"><pre><span class="k">class</span> <span class="nx">QueryBuilder</span></pre></div> </td> </tr> <tr id="section-28"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-28">&#182;</a> </div> <p>Base class for query builders which support WHERE, ORDER and LIMIT clauses.</p> </td> <td class="code"> <div class="highlight"><pre><span class="k">class</span> <span class="nx">WhereOrderLimit</span> <span class="k">extends</span> <span class="nx">QueryBuilder</span>
<span class="nv">wheres: </span><span class="kc">null</span>
<span class="nv">orders: </span><span class="kc">null</span>
<span class="nv">limits: </span><span class="kc">null</span>
<span class="nv">constructor: </span><span class="o">-&gt;</span>
<span class="vi">@wheres = </span><span class="p">[]</span>
- <span class="vi">@orders = </span><span class="p">[]</span></pre></div> </td> </tr> <tr id="section-28"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-28">&#182;</a> </div> <p>Add a WHERE condition.</p>
+ <span class="vi">@orders = </span><span class="p">[]</span></pre></div> </td> </tr> <tr id="section-29"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-29">&#182;</a> </div> <p>Add a WHERE condition.</p>
<p>When the final query is constructed all the WHERE conditions are combined using the intersection (AND) operator.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">where: </span><span class="p">(</span><span class="nx">condition</span><span class="p">)</span> <span class="o">=&gt;</span>
<span class="nv">condition = </span><span class="nx">sanitizeCondition</span><span class="p">(</span><span class="nx">condition</span><span class="p">)</span>
<span class="k">if</span> <span class="s">&quot;&quot;</span> <span class="o">isnt</span> <span class="nx">condition</span>
<span class="nx">@wheres</span><span class="p">.</span><span class="nx">push</span> <span class="nx">condition</span>
- <span class="nx">@</span></pre></div> </td> </tr> <tr id="section-29"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-29">&#182;</a> </div> <p>Add an ORDER BY transformation for the given field in the given order.</p>
+ <span class="nx">@</span></pre></div> </td> </tr> <tr id="section-30"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-30">&#182;</a> </div> <p>Add an ORDER BY transformation for the given field in the given order.</p>
<p>To specify descending order pass false for the 'asc' parameter.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">order: </span><span class="p">(</span><span class="nx">field</span><span class="p">,</span> <span class="nv">asc = </span><span class="kc">true</span><span class="p">)</span> <span class="o">=&gt;</span>
<span class="nv">field = </span><span class="nx">sanitizeField</span> <span class="nx">field</span>
<span class="nx">@orders</span><span class="p">.</span><span class="nx">push</span>
<span class="nv">field: </span><span class="nx">field</span>
<span class="nv">dir: </span><span class="k">if</span> <span class="nx">asc</span> <span class="k">then</span> <span class="s">&quot;ASC&quot;</span> <span class="k">else</span> <span class="s">&quot;DESC&quot;</span>
- <span class="nx">@</span></pre></div> </td> </tr> <tr id="section-30"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-30">&#182;</a> </div> <p>Set the LIMIT transformation.</p>
+ <span class="nx">@</span></pre></div> </td> </tr> <tr id="section-31"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-31">&#182;</a> </div> <p>Set the LIMIT transformation.</p>
<p>Call this will override the previously set limit for this query. Also note that Passing 0 for 'max' will remove
the limit.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">limit: </span><span class="p">(</span><span class="nx">max</span><span class="p">)</span> <span class="o">=&gt;</span>
<span class="nv">max = </span><span class="nx">sanitizeLimitOffset</span> <span class="nx">max</span>
<span class="vi">@limits = </span><span class="nx">max</span>
- <span class="nx">@</span></pre></div> </td> </tr> <tr id="section-31"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-31">&#182;</a> </div> <p>Get string representation of WHERE clause, if any</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">whereString: </span><span class="o">=&gt;</span>
+ <span class="nx">@</span></pre></div> </td> </tr> <tr id="section-32"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-32">&#182;</a> </div> <p>Get string representation of WHERE clause, if any</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">whereString: </span><span class="o">=&gt;</span>
<span class="k">if</span> <span class="mi">0</span> <span class="o">&lt;</span> <span class="nx">@wheres</span><span class="p">.</span><span class="nx">length</span>
<span class="s">&quot; WHERE (&quot;</span> <span class="o">+</span> <span class="nx">@wheres</span><span class="p">.</span><span class="nx">join</span><span class="p">(</span><span class="s">&quot;) AND (&quot;</span><span class="p">)</span> <span class="o">+</span> <span class="s">&quot;)&quot;</span>
<span class="k">else</span>
- <span class="s">&quot;&quot;</span></pre></div> </td> </tr> <tr id="section-32"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-32">&#182;</a> </div> <p>Get string representation of ORDER BY clause, if any</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">orderString: </span><span class="o">=&gt;</span>
+ <span class="s">&quot;&quot;</span></pre></div> </td> </tr> <tr id="section-33"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-33">&#182;</a> </div> <p>Get string representation of ORDER BY clause, if any</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">orderString: </span><span class="o">=&gt;</span>
<span class="k">if</span> <span class="mi">0</span> <span class="o">&lt;</span> <span class="nx">@orders</span><span class="p">.</span><span class="nx">length</span>
<span class="nv">orders = </span><span class="s">&quot;&quot;</span>
<span class="k">for</span> <span class="nx">o</span> <span class="k">in</span> <span class="nx">@orders</span>
@@ -155,11 +155,11 @@
<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-33"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-33">&#182;</a> </div> <p>Get string representation of LIMIT clause, if any</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">limitString: </span><span class="o">=&gt;</span>
+ <span class="s">&quot;&quot;</span></pre></div> </td> </tr> <tr id="section-34"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-34">&#182;</a> </div> <p>Get string representation of LIMIT clause, if any</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">limitString: </span><span class="o">=&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-34"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-34">&#182;</a> </div> <p>A SELECT query builder.</p>
+ <span class="s">&quot;&quot;</span></pre></div> </td> </tr> <tr id="section-35"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-35">&#182;</a> </div> <p>A SELECT query builder.</p>
<p>Note that the query builder does not check the final query string for correctness.</p>
@@ -176,7 +176,7 @@
<span class="vi">@froms = </span><span class="p">[]</span>
<span class="vi">@fields = </span><span class="p">[]</span>
<span class="vi">@joins = </span><span class="p">[]</span>
- <span class="vi">@groups = </span><span class="p">[]</span></pre></div> </td> </tr> <tr id="section-35"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-35">&#182;</a> </div> <p>Add a JOIN with the given table.</p>
+ <span class="vi">@groups = </span><span class="p">[]</span></pre></div> </td> </tr> <tr id="section-36"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-36">&#182;</a> </div> <p>Add a JOIN with the given table.</p>
<p>'type' must be either one of inner, outer, left or right. Default is 'inner'.</p>
@@ -196,9 +196,9 @@
<span class="nv">table: </span><span class="nx">table</span>
<span class="nv">alias: </span><span class="nx">alias</span>
<span class="nv">condition: </span><span class="nx">condition</span>
- <span class="nx">@</span></pre></div> </td> </tr> <tr id="section-36"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-36">&#182;</a> </div> <p>Add the DISTINCT keyword to this query.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">distinct: </span><span class="o">=&gt;</span>
+ <span class="nx">@</span></pre></div> </td> </tr> <tr id="section-37"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-37">&#182;</a> </div> <p>Add the DISTINCT keyword to this 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="nx">@</span></pre></div> </td> </tr> <tr id="section-37"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-37">&#182;</a> </div> <p>Read data from the given table.</p>
+ <span class="nx">@</span></pre></div> </td> </tr> <tr id="section-38"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-38">&#182;</a> </div> <p>Read data from the given table.</p>
<p>An alias may also be specified for the table.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">from: </span><span class="p">(</span><span class="nx">table</span><span class="p">,</span> <span class="nv">alias = </span><span class="kc">null</span><span class="p">)</span> <span class="o">=&gt;</span>
<span class="nv">table = </span><span class="nx">sanitizeTable</span><span class="p">(</span><span class="nx">table</span><span class="p">)</span>
@@ -207,7 +207,7 @@
<span class="nx">@froms</span><span class="p">.</span><span class="nx">push</span>
<span class="nv">name: </span><span class="nx">table</span>
<span class="nv">alias: </span><span class="nx">alias</span>
- <span class="nx">@</span></pre></div> </td> </tr> <tr id="section-38"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-38">&#182;</a> </div> <p>Add the given field to the final result set.</p>
+ <span class="nx">@</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>Add the given field to the final result set.</p>
<p>The 'field' parameter does not necessarily have to be a fieldname. It can use database functions too,
e.g. DATE_FORMAT(a.started, "%H")</p>
@@ -219,59 +219,59 @@
<span class="nx">@fields</span><span class="p">.</span><span class="nx">push</span>
<span class="nv">field: </span><span class="nx">field</span>
<span class="nv">alias: </span><span class="nx">alias</span>
- <span class="nx">@</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>Add an INNER JOIN with the given table.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">join: </span><span class="p">(</span><span class="nx">table</span><span class="p">,</span> <span class="nv">alias = </span><span class="kc">null</span><span class="p">,</span> <span class="nv">condition = </span><span class="kc">null</span><span class="p">)</span> <span class="o">=&gt;</span>
- <span class="nx">@_join</span> <span class="s">&#39;INNER&#39;</span><span class="p">,</span> <span class="nx">table</span><span class="p">,</span> <span class="nx">alias</span><span class="p">,</span> <span class="nx">condition</span></pre></div> </td> </tr> <tr id="section-40"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-40">&#182;</a> </div> <p>Add a LEFT JOIN with the given table.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">left_join: </span><span class="p">(</span><span class="nx">table</span><span class="p">,</span> <span class="nv">alias = </span><span class="kc">null</span><span class="p">,</span> <span class="nv">condition = </span><span class="kc">null</span><span class="p">)</span> <span class="o">=&gt;</span>
- <span class="nx">@_join</span> <span class="s">&#39;LEFT&#39;</span><span class="p">,</span> <span class="nx">table</span><span class="p">,</span> <span class="nx">alias</span><span class="p">,</span> <span class="nx">condition</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>Add a RIGHT JOIN with the given table.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">right_join: </span><span class="p">(</span><span class="nx">table</span><span class="p">,</span> <span class="nv">alias = </span><span class="kc">null</span><span class="p">,</span> <span class="nv">condition = </span><span class="kc">null</span><span class="p">)</span> <span class="o">=&gt;</span>
- <span class="nx">@_join</span> <span class="s">&#39;RIGHT&#39;</span><span class="p">,</span> <span class="nx">table</span><span class="p">,</span> <span class="nx">alias</span><span class="p">,</span> <span class="nx">condition</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>Add an OUTER JOIN with the given table.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">outer_join: </span><span class="p">(</span><span class="nx">table</span><span class="p">,</span> <span class="nv">alias = </span><span class="kc">null</span><span class="p">,</span> <span class="nv">condition = </span><span class="kc">null</span><span class="p">)</span> <span class="o">=&gt;</span>
- <span class="nx">@_join</span> <span class="s">&#39;OUTER&#39;</span><span class="p">,</span> <span class="nx">table</span><span class="p">,</span> <span class="nx">alias</span><span class="p">,</span> <span class="nx">condition</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>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>
+ <span class="nx">@</span></pre></div> </td> </tr> <tr id="section-40"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-40">&#182;</a> </div> <p>Add an INNER JOIN with the given table.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">join: </span><span class="p">(</span><span class="nx">table</span><span class="p">,</span> <span class="nv">alias = </span><span class="kc">null</span><span class="p">,</span> <span class="nv">condition = </span><span class="kc">null</span><span class="p">)</span> <span class="o">=&gt;</span>
+ <span class="nx">@_join</span> <span class="s">&#39;INNER&#39;</span><span class="p">,</span> <span class="nx">table</span><span class="p">,</span> <span class="nx">alias</span><span class="p">,</span> <span class="nx">condition</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>Add a LEFT JOIN with the given table.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">left_join: </span><span class="p">(</span><span class="nx">table</span><span class="p">,</span> <span class="nv">alias = </span><span class="kc">null</span><span class="p">,</span> <span class="nv">condition = </span><span class="kc">null</span><span class="p">)</span> <span class="o">=&gt;</span>
+ <span class="nx">@_join</span> <span class="s">&#39;LEFT&#39;</span><span class="p">,</span> <span class="nx">table</span><span class="p">,</span> <span class="nx">alias</span><span class="p">,</span> <span class="nx">condition</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>Add a RIGHT JOIN with the given table.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">right_join: </span><span class="p">(</span><span class="nx">table</span><span class="p">,</span> <span class="nv">alias = </span><span class="kc">null</span><span class="p">,</span> <span class="nv">condition = </span><span class="kc">null</span><span class="p">)</span> <span class="o">=&gt;</span>
+ <span class="nx">@_join</span> <span class="s">&#39;RIGHT&#39;</span><span class="p">,</span> <span class="nx">table</span><span class="p">,</span> <span class="nx">alias</span><span class="p">,</span> <span class="nx">condition</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>Add an OUTER JOIN with the given table.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">outer_join: </span><span class="p">(</span><span class="nx">table</span><span class="p">,</span> <span class="nv">alias = </span><span class="kc">null</span><span class="p">,</span> <span class="nv">condition = </span><span class="kc">null</span><span class="p">)</span> <span class="o">=&gt;</span>
+ <span class="nx">@_join</span> <span class="s">&#39;OUTER&#39;</span><span class="p">,</span> <span class="nx">table</span><span class="p">,</span> <span class="nx">alias</span><span class="p">,</span> <span class="nx">condition</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>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>
<span class="nv">field = </span><span class="nx">sanitizeField</span> <span class="nx">field</span>
<span class="nx">@groups</span><span class="p">.</span><span class="nx">push</span> <span class="nx">field</span>
- <span class="nx">@</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>Set the OFFSET transformation.</p>
+ <span class="nx">@</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>Set the OFFSET transformation.</p>
<p>Call this will override the previously set offset for this query. Also note that Passing 0 for 'max' will remove
the offset.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">offset: </span><span class="p">(</span><span class="nx">start</span><span class="p">)</span> <span class="o">=&gt;</span>
<span class="nv">start = </span><span class="nx">sanitizeLimitOffset</span> <span class="nx">start</span>
<span class="vi">@offsets = </span><span class="nx">start</span>
- <span class="nx">@</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>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></pre></div> </td> </tr> <tr id="section-46"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-46">&#182;</a> </div> <p>basic checks</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="mi">0</span> <span class="o">&gt;=</span> <span class="nx">@froms</span><span class="p">.</span><span class="nx">length</span>
+ <span class="nx">@</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>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></pre></div> </td> </tr> <tr id="section-47"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-47">&#182;</a> </div> <p>basic checks</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="mi">0</span> <span class="o">&gt;=</span> <span class="nx">@froms</span><span class="p">.</span><span class="nx">length</span>
<span class="k">throw</span> <span class="k">new</span> <span class="nb">Error</span> <span class="s">&quot;from() needs to be called&quot;</span>
- <span class="nv">ret = </span><span class="s">&quot;SELECT &quot;</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>distinct</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">ret</span> <span class="o">+=</span> <span class="s">&quot;DISTINCT &quot;</span> <span class="k">if</span> <span class="nx">@useDistinct</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>fields</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">fields = </span><span class="s">&quot;&quot;</span>
+ <span class="nv">ret = </span><span class="s">&quot;SELECT &quot;</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>distinct</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">ret</span> <span class="o">+=</span> <span class="s">&quot;DISTINCT &quot;</span> <span class="k">if</span> <span class="nx">@useDistinct</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>fields</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">fields = </span><span class="s">&quot;&quot;</span>
<span class="k">for</span> <span class="nx">field</span> <span class="k">in</span> <span class="nx">@fields</span>
<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="nx">field</span><span class="p">.</span><span class="nx">field</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="nx">ret</span> <span class="o">+=</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-49"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-49">&#182;</a> </div> <p>tables</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">tables = </span><span class="s">&quot;&quot;</span>
+ <span class="nx">ret</span> <span class="o">+=</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>tables</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">tables = </span><span class="s">&quot;&quot;</span>
<span class="k">for</span> <span class="nx">table</span> <span class="k">in</span> <span class="nx">@froms</span>
<span class="nx">tables</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">tables</span>
<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="nx">ret</span> <span class="o">+=</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-50"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-50">&#182;</a> </div> <p>joins</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">joins = </span><span class="s">&quot;&quot;</span>
+ <span class="nx">ret</span> <span class="o">+=</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-51"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-51">&#182;</a> </div> <p>joins</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">joins = </span><span class="s">&quot;&quot;</span>
<span class="k">for</span> <span class="nx">j</span> <span class="k">in</span> <span class="nx">@joins</span>
<span class="nx">joins</span> <span class="o">+=</span> <span class="s">&quot; </span><span class="si">#{</span><span class="nx">j</span><span class="p">.</span><span class="nx">type</span><span class="si">}</span><span class="s"> JOIN </span><span class="si">#{</span><span class="nx">j</span><span class="p">.</span><span class="nx">table</span><span class="si">}</span><span class="s">&quot;</span>
<span class="nx">joins</span> <span class="o">+=</span> <span class="s">&quot; `</span><span class="si">#{</span><span class="nx">j</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">j</span><span class="p">.</span><span class="nx">alias</span>
<span class="nx">joins</span> <span class="o">+=</span> <span class="s">&quot; ON (</span><span class="si">#{</span><span class="nx">j</span><span class="p">.</span><span class="nx">condition</span><span class="si">}</span><span class="s">)&quot;</span> <span class="k">if</span> <span class="nx">j</span><span class="p">.</span><span class="nx">condition</span>
- <span class="nx">ret</span> <span class="o">+=</span> <span class="nx">joins</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>where</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">ret</span> <span class="o">+=</span> <span class="nx">@whereString</span><span class="p">()</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>group by</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="mi">0</span> <span class="o">&lt;</span> <span class="nx">@groups</span><span class="p">.</span><span class="nx">length</span>
+ <span class="nx">ret</span> <span class="o">+=</span> <span class="nx">joins</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>where</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">ret</span> <span class="o">+=</span> <span class="nx">@whereString</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>group by</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="mi">0</span> <span class="o">&lt;</span> <span class="nx">@groups</span><span class="p">.</span><span class="nx">length</span>
<span class="nv">groups = </span><span class="s">&quot;&quot;</span>
<span class="k">for</span> <span class="nx">f</span> <span class="k">in</span> <span class="nx">@groups</span>
<span class="nx">groups</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">groups</span>
<span class="nx">groups</span> <span class="o">+=</span> <span class="nx">f</span>
- <span class="nx">ret</span> <span class="o">+=</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></pre></div> </td> </tr> <tr id="section-53"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-53">&#182;</a> </div> <p>order by</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">ret</span> <span class="o">+=</span> <span class="nx">@orderString</span><span class="p">()</span></pre></div> </td> </tr> <tr id="section-54"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-54">&#182;</a> </div> <p>limit</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">ret</span> <span class="o">+=</span> <span class="nx">@limitString</span><span class="p">()</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>offset</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">ret</span> <span class="o">+=</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">if</span> <span class="nx">@offsets</span>
+ <span class="nx">ret</span> <span class="o">+=</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></pre></div> </td> </tr> <tr id="section-54"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-54">&#182;</a> </div> <p>order by</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">ret</span> <span class="o">+=</span> <span class="nx">@orderString</span><span class="p">()</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>limit</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">ret</span> <span class="o">+=</span> <span class="nx">@limitString</span><span class="p">()</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>offset</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">ret</span> <span class="o">+=</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">if</span> <span class="nx">@offsets</span>
- <span class="nx">ret</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>An UPDATE query builder.</p>
+ <span class="nx">ret</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>An UPDATE query builder.</p>
<p>Note that the query builder does not check the final query string for correctness.</p>
<p>All the build methods in this object return the object instance for chained method calling purposes.</p> </td> <td class="code"> <div class="highlight"><pre><span class="k">class</span> <span class="nx">Update</span> <span class="k">extends</span> <span class="nx">WhereOrderLimit</span>
<span class="nv">tables: </span><span class="kc">null</span>
<span class="nv">fields: </span><span class="kc">null</span>
- <span class="nv">options: </span><span class="kc">null</span></pre></div> </td> </tr> <tr id="section-57"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-57">&#182;</a> </div> <p>options: see DefaultBuilderOptions</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">constructor: </span><span class="nf">(options) -&gt;</span>
+ <span class="nv">options: </span><span class="kc">null</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>options: see DefaultBuilderOptions</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">constructor: </span><span class="nf">(options) -&gt;</span>
<span class="k">super</span>
<span class="vi">@tables = </span><span class="p">[]</span>
<span class="vi">@fields = </span><span class="p">{}</span>
- <span class="vi">@options = </span><span class="nx">_extend</span> <span class="p">{},</span> <span class="nx">DefaultUpdateBuilderOptions</span><span class="p">,</span> <span class="nx">options</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>Update the given table.</p>
+ <span class="vi">@options = </span><span class="nx">_extend</span> <span class="p">{},</span> <span class="nx">DefaultUpdateBuilderOptions</span><span class="p">,</span> <span class="nx">options</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>Update the given table.</p>
<p>An alias may also be specified for the table.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">table: </span><span class="p">(</span><span class="nx">table</span><span class="p">,</span> <span class="nv">alias = </span><span class="kc">null</span><span class="p">)</span> <span class="o">=&gt;</span>
<span class="nv">table = </span><span class="nx">sanitizeTable</span><span class="p">(</span><span class="nx">table</span><span class="p">)</span>
@@ -280,61 +280,61 @@
<span class="nx">@tables</span><span class="p">.</span><span class="nx">push</span>
<span class="nv">name: </span><span class="nx">table</span>
<span class="nv">alias: </span><span class="nx">alias</span>
- <span class="nx">@</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>Update the given field with the given value.
+ <span class="nx">@</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>Update the given field with the given value.
This will override any previously set value for the given field.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">set: </span><span class="p">(</span><span class="nx">field</span><span class="p">,</span> <span class="nx">value</span><span class="p">)</span> <span class="o">=&gt;</span>
<span class="nv">field = </span><span class="nx">sanitizeField</span> <span class="nx">field</span>
<span class="nv">value = </span><span class="nx">sanitizeValue</span> <span class="nx">value</span>
<span class="nx">@fields</span><span class="p">[</span><span class="nx">field</span><span class="p">]</span> <span class="o">=</span> <span class="nx">value</span>
- <span class="nx">@</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>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></pre></div> </td> </tr> <tr id="section-61"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-61">&#182;</a> </div> <p>basic checks</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="mi">0</span> <span class="o">&gt;=</span> <span class="nx">@tables</span><span class="p">.</span><span class="nx">length</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;table() needs to be called&quot;</span>
+ <span class="nx">@</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>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></pre></div> </td> </tr> <tr id="section-62"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-62">&#182;</a> </div> <p>basic checks</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="mi">0</span> <span class="o">&gt;=</span> <span class="nx">@tables</span><span class="p">.</span><span class="nx">length</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;table() needs to be called&quot;</span>
<span class="nv">fieldNames = </span><span class="p">(</span><span class="nx">field</span> <span class="k">for</span> <span class="nx">own</span> <span class="nx">field</span> <span class="k">of</span> <span class="nx">@fields</span><span class="p">)</span>
<span class="k">if</span> <span class="mi">0</span> <span class="o">&gt;=</span> <span class="nx">fieldNames</span><span class="p">.</span><span class="nx">length</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;set() needs to be called&quot;</span>
- <span class="nv">ret = </span><span class="s">&quot;UPDATE &quot;</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>tables</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">tables = </span><span class="s">&quot;&quot;</span>
+ <span class="nv">ret = </span><span class="s">&quot;UPDATE &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>tables</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">tables = </span><span class="s">&quot;&quot;</span>
<span class="k">for</span> <span class="nx">table</span> <span class="k">in</span> <span class="nx">@tables</span>
<span class="nx">tables</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">tables</span>
<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">ret</span> <span class="o">+=</span> <span class="nx">tables</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>fields</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">fields = </span><span class="s">&quot;&quot;</span>
+ <span class="nx">ret</span> <span class="o">+=</span> <span class="nx">tables</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>fields</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">fields = </span><span class="s">&quot;&quot;</span>
<span class="k">for</span> <span class="nx">field</span> <span class="k">in</span> <span class="nx">fieldNames</span>
<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="nx">@options</span><span class="p">)</span><span class="si">}</span><span class="s">&quot;</span>
- <span class="nx">ret</span> <span class="o">+=</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-64"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-64">&#182;</a> </div> <p>where</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">ret</span> <span class="o">+=</span> <span class="nx">@whereString</span><span class="p">()</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>order by</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">ret</span> <span class="o">+=</span> <span class="nx">@orderString</span><span class="p">()</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>limit</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">ret</span> <span class="o">+=</span> <span class="nx">@limitString</span><span class="p">()</span>
+ <span class="nx">ret</span> <span class="o">+=</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-65"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-65">&#182;</a> </div> <p>where</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">ret</span> <span class="o">+=</span> <span class="nx">@whereString</span><span class="p">()</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>order by</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">ret</span> <span class="o">+=</span> <span class="nx">@orderString</span><span class="p">()</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>limit</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">ret</span> <span class="o">+=</span> <span class="nx">@limitString</span><span class="p">()</span>
- <span class="nx">ret</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>A DELETE query builder.</p>
+ <span class="nx">ret</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>A DELETE query builder.</p>
<p>Note that the query builder does not check the final query string for correctness.</p>
<p>All the build methods in this object return the object instance for chained method calling purposes.</p> </td> <td class="code"> <div class="highlight"><pre><span class="k">class</span> <span class="nx">Delete</span> <span class="k">extends</span> <span class="nx">WhereOrderLimit</span>
- <span class="nv">table: </span><span class="kc">null</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>The table to delete from.
+ <span class="nv">table: </span><span class="kc">null</span></pre></div> </td> </tr> <tr id="section-69"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-69">&#182;</a> </div> <p>The table to delete from.
Calling this will override any previously set value.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">from: </span><span class="p">(</span><span class="nx">table</span><span class="p">)</span> <span class="o">=&gt;</span>
<span class="nv">table = </span><span class="nx">sanitizeTable</span><span class="p">(</span><span class="nx">table</span><span class="p">)</span>
<span class="vi">@table = </span><span class="nx">table</span>
- <span class="nx">@</span></pre></div> </td> </tr> <tr id="section-69"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-69">&#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></pre></div> </td> </tr> <tr id="section-70"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-70">&#182;</a> </div> <p>basic checks</p> </td> <td class="code"> <div class="highlight"><pre> <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;from() needs to be called&quot;</span>
+ <span class="nx">@</span></pre></div> </td> </tr> <tr id="section-70"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-70">&#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></pre></div> </td> </tr> <tr id="section-71"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-71">&#182;</a> </div> <p>basic checks</p> </td> <td class="code"> <div class="highlight"><pre> <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;from() needs to be called&quot;</span>
- <span class="nv">ret = </span><span class="s">&quot;DELETE FROM </span><span class="si">#{</span><span class="nx">@table</span><span class="si">}</span><span class="s">&quot;</span></pre></div> </td> </tr> <tr id="section-71"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-71">&#182;</a> </div> <p>where</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">ret</span> <span class="o">+=</span> <span class="nx">@whereString</span><span class="p">()</span></pre></div> </td> </tr> <tr id="section-72"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-72">&#182;</a> </div> <p>order by</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">ret</span> <span class="o">+=</span> <span class="nx">@orderString</span><span class="p">()</span></pre></div> </td> </tr> <tr id="section-73"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-73">&#182;</a> </div> <p>limit</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">ret</span> <span class="o">+=</span> <span class="nx">@limitString</span><span class="p">()</span>
+ <span class="nv">ret = </span><span class="s">&quot;DELETE FROM </span><span class="si">#{</span><span class="nx">@table</span><span class="si">}</span><span class="s">&quot;</span></pre></div> </td> </tr> <tr id="section-72"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-72">&#182;</a> </div> <p>where</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">ret</span> <span class="o">+=</span> <span class="nx">@whereString</span><span class="p">()</span></pre></div> </td> </tr> <tr id="section-73"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-73">&#182;</a> </div> <p>order by</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">ret</span> <span class="o">+=</span> <span class="nx">@orderString</span><span class="p">()</span></pre></div> </td> </tr> <tr id="section-74"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-74">&#182;</a> </div> <p>limit</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">ret</span> <span class="o">+=</span> <span class="nx">@limitString</span><span class="p">()</span>
- <span class="nx">ret</span></pre></div> </td> </tr> <tr id="section-74"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-74">&#182;</a> </div> <p>An INSERT query builder.</p>
+ <span class="nx">ret</span></pre></div> </td> </tr> <tr id="section-75"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-75">&#182;</a> </div> <p>An INSERT query builder.</p>
<p>Note that the query builder does not check the final query string for correctness.</p>
-<p>All the build methods in this object return the object instance for chained method calling purposes.</p> </td> <td class="code"> <div class="highlight"><pre><span class="k">class</span> <span class="nx">Insert</span>
+<p>All the build methods in this object return the object instance for chained method calling purposes.</p> </td> <td class="code"> <div class="highlight"><pre><span class="k">class</span> <span class="nx">Insert</span> <span class="k">extends</span> <span class="nx">QueryBuilder</span>
<span class="nv">table: </span><span class="kc">null</span>
<span class="nv">fields: </span><span class="kc">null</span>
- <span class="nv">options: </span><span class="kc">null</span></pre></div> </td> </tr> <tr id="section-75"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-75">&#182;</a> </div> <p>options: see DefaultBuilderOptions</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">constructor: </span><span class="nf">(options) -&gt;</span>
+ <span class="nv">options: </span><span class="kc">null</span></pre></div> </td> </tr> <tr id="section-76"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-76">&#182;</a> </div> <p>options: see DefaultBuilderOptions</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">constructor: </span><span class="nf">(options) -&gt;</span>
<span class="vi">@fields = </span><span class="p">{}</span>
- <span class="vi">@options = </span><span class="nx">_extend</span> <span class="p">{},</span> <span class="nx">DefaultInsertBuilderOptions</span><span class="p">,</span> <span class="nx">options</span></pre></div> </td> </tr> <tr id="section-76"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-76">&#182;</a> </div> <p>The table to insert into.
+ <span class="vi">@options = </span><span class="nx">_extend</span> <span class="p">{},</span> <span class="nx">DefaultInsertBuilderOptions</span><span class="p">,</span> <span class="nx">options</span></pre></div> </td> </tr> <tr id="section-77"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-77">&#182;</a> </div> <p>The table to insert into.
This will override any previously set value.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">into: </span><span class="p">(</span><span class="nx">table</span><span class="p">)</span> <span class="o">=&gt;</span>
<span class="nv">table = </span><span class="nx">sanitizeTable</span><span class="p">(</span><span class="nx">table</span><span class="p">)</span>
<span class="vi">@table = </span><span class="nx">table</span>
- <span class="nx">@</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>Set the given field to the given value.
+ <span class="nx">@</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>Set the given field to the given value.
This will override any previously set value for the given field.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">set: </span><span class="p">(</span><span class="nx">field</span><span class="p">,</span> <span class="nx">value</span><span class="p">)</span> <span class="o">=&gt;</span>
<span class="nv">field = </span><span class="nx">sanitizeField</span> <span class="nx">field</span>
<span class="nv">value = </span><span class="nx">sanitizeValue</span> <span class="nx">value</span>
<span class="nx">@fields</span><span class="p">[</span><span class="nx">field</span><span class="p">]</span> <span class="o">=</span> <span class="nx">value</span>
- <span class="nx">@</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>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></pre></div> </td> </tr> <tr id="section-79"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-79">&#182;</a> </div> <p>basic checks</p> </td> <td class="code"> <div class="highlight"><pre> <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">@</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>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></pre></div> </td> </tr> <tr id="section-80"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-80">&#182;</a> </div> <p>basic checks</p> </td> <td class="code"> <div class="highlight"><pre> <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="nv">fieldNames = </span><span class="p">(</span><span class="nx">name</span> <span class="k">for</span> <span class="nx">own</span> <span class="nx">name</span> <span class="k">of</span> <span class="nx">@fields</span><span class="p">)</span>
- <span class="k">if</span> <span class="mi">0</span> <span class="o">&gt;=</span> <span class="nx">fieldNames</span><span class="p">.</span><span class="nx">length</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;set() needs to be called&quot;</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>fields</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">fields = </span><span class="s">&quot;&quot;</span>
+ <span class="k">if</span> <span class="mi">0</span> <span class="o">&gt;=</span> <span class="nx">fieldNames</span><span class="p">.</span><span class="nx">length</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;set() needs to be called&quot;</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>fields</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">fields = </span><span class="s">&quot;&quot;</span>
<span class="nv">values = </span><span class="s">&quot;&quot;</span>
<span class="k">for</span> <span class="nx">field</span> <span class="k">in</span> <span class="nx">fieldNames</span>
<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>
@@ -342,12 +342,19 @@
<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="nx">@options</span><span class="p">)</span>
- <span class="s">&quot;INSERT INTO </span><span class="si">#{</span><span class="nx">@table</span><span class="si">}</span><span class="s"> (</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-81"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-81">&#182;</a> </div> <p>Export everything as easily usable methods.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_export =</span>
+ <span class="s">&quot;INSERT INTO </span><span class="si">#{</span><span class="nx">@table</span><span class="si">}</span><span class="s"> (</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-82"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-82">&#182;</a> </div> <p>Export everything as easily usable methods.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_export = </span><span class="p">{</span>
<span class="nv">expr: </span><span class="o">-&gt;</span> <span class="k">new</span> <span class="nx">Expression</span>
<span class="nv">select: </span><span class="o">-&gt;</span> <span class="k">new</span> <span class="nx">Select</span>
<span class="nv">update: </span><span class="nf">(options) -&gt;</span> <span class="k">new</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">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="o">-&gt;</span> <span class="k">new</span> <span class="nx">Delete</span>
+ <span class="nx">Expression</span>
+ <span class="nx">QueryBuilder</span>
+ <span class="nx">Select</span>
+ <span class="nx">Update</span>
+ <span class="nx">Insert</span>
+ <span class="nx">Delete</span>
+<span class="p">}</span>
<span class="nx">module</span><span class="o">?</span><span class="p">.</span><span class="nv">exports = </span><span class="nx">_export</span>
<span class="nb">window</span><span class="o">?</span><span class="p">.</span><span class="nv">squel = </span><span class="nx">_export</span>
View
30 squel.js
@@ -27,7 +27,7 @@ OTHER DEALINGS IN THE SOFTWARE.
(function() {
- var DefaultInsertBuilderOptions, DefaultUpdateBuilderOptions, Delete, Expression, ExpressionClassName, Insert, Select, Update, WhereOrderLimit, formatValue, getObjectClassName, sanitizeAlias, sanitizeCondition, sanitizeField, sanitizeLimitOffset, sanitizeName, sanitizeTable, sanitizeValue, _export, _extend,
+ var DefaultInsertBuilderOptions, DefaultUpdateBuilderOptions, Delete, Expression, ExpressionClassName, Insert, QueryBuilder, Select, Update, WhereOrderLimit, formatValue, getObjectClassName, sanitizeAlias, sanitizeCondition, sanitizeField, sanitizeLimitOffset, sanitizeName, sanitizeTable, sanitizeValue, _export, _extend,
__slice = [].slice,
__hasProp = {}.hasOwnProperty,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
@@ -241,7 +241,17 @@ OTHER DEALINGS IN THE SOFTWARE.
return value;
};
- WhereOrderLimit = (function() {
+ QueryBuilder = (function() {
+
+ function QueryBuilder() {}
+
+ return QueryBuilder;
+
+ })();
+
+ WhereOrderLimit = (function(_super) {
+
+ __extends(WhereOrderLimit, _super);
WhereOrderLimit.prototype.wheres = null;
@@ -327,7 +337,7 @@ OTHER DEALINGS IN THE SOFTWARE.
return WhereOrderLimit;
- })();
+ })(QueryBuilder);
Select = (function(_super) {
@@ -680,7 +690,9 @@ OTHER DEALINGS IN THE SOFTWARE.
})(WhereOrderLimit);
- Insert = (function() {
+ Insert = (function(_super) {
+
+ __extends(Insert, _super);
Insert.prototype.table = null;
@@ -747,7 +759,7 @@ OTHER DEALINGS IN THE SOFTWARE.
return Insert;
- })();
+ })(QueryBuilder);
_export = {
expr: function() {
@@ -764,7 +776,13 @@ OTHER DEALINGS IN THE SOFTWARE.
},
"delete": function() {
return new Delete;
- }
+ },
+ Expression: Expression,
+ QueryBuilder: QueryBuilder,
+ Select: Select,
+ Update: Update,
+ Insert: Insert,
+ Delete: Delete
};
if (typeof module !== "undefined" && module !== null) {
View
2  squel.min.js
@@ -22,4 +22,4 @@ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
-*/(function(){var e,t,n,r,i,s,o,u,a,f,l,c,h,p,d,v,m,g,y,b,w=[].slice,E={}.hasOwnProperty,S=function(e,t){return function(){return e.apply(t,arguments)}},x=function(e,t){function r(){this.constructor=e}for(var n in t)E.call(t,n)&&(e[n]=t[n]);return r.prototype=t.prototype,e.prototype=new r,e.__super__=t.prototype,e};b=function(){var e,t,n,r,i,s,o;e=arguments[0],n=2<=arguments.length?w.call(arguments,1):[];if(n)for(s=0,o=n.length;s<o;s++){r=n[s];if(r)for(t in r){if(!E.call(r,t))continue;i=r[t],e[t]=i}}return e},r=function(){function t(){this.toString=S(this.toString,this),this.or=S(this.or,this),this.and=S(this.and,this),this.end=S(this.end,this),this.or_begin=S(this.or_begin,this),this.and_begin=S(this.and_begin,this);var e=this;this.tree={parent:null,nodes:[]},this.current=this.tree,this._begin=function(t){var n;return n={type:t,parent:e.current,nodes:[]},e.current.nodes.push(n),e.current=e.current.nodes[e.current.nodes.length-1],e}}var e;return t.prototype.tree=null,t.prototype.current=null,t.prototype.and_begin=function(){return this._begin("AND")},t.prototype.or_begin=function(){return this._begin("OR")},t.prototype.end=function(){if(!this.current.parent)throw new Error("begin() needs to be called");return this.current=this.current.parent,this},t.prototype.and=function(e){if(!e||"string"!=typeof e)throw new Error("expr must be a string");return this.current.nodes.push({type:"AND",expr:e}),this},t.prototype.or=function(e){if(!e||"string"!=typeof e)throw new Error("expr must be a string");return this.current.nodes.push({type:"OR",expr:e}),this},t.prototype.toString=function(){if(null!==this.current.parent)throw new Error("end() needs to be called");return e(this.tree)},e=function(t){var n,r,i,s,o,u;i="",u=t.nodes;for(s=0,o=u.length;s<o;s++)n=u[s],n.expr!=null?r=n.expr:(r=e(n),""!==r&&(r="("+r+")")),""!==r&&(""!==i&&(i+=" "+n.type+" "),i+=r);return i},t}(),e=t={usingValuePlaceholders:!1},l=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},i=l(new r),h=function(e){var t;t=typeof e;if(i!==l(e)&&"string"!==t)throw new Error("condition must be a string or Expression instance");return"Expression"===t&&(e=e.toString()),e},v=function(e,t){if("string"!=typeof e)throw new Error(""+t+" must be a string");return e},p=function(e){return v(e,"field name")},m=function(e){return v(e,"table name")},c=function(e){return v(e,"alias")},d=function(e){e=parseInt(e);if(0>e)throw new Error("limit/offset must be >=0");return e},g=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},f=function(e,t){return null===e?e="NULL":"boolean"==typeof e?e=e?"TRUE":"FALSE":"number"!=typeof e&&!1===t.usingValuePlaceholders&&(e="'"+e+"'"),e},a=function(){function e(){this.limitString=S(this.limitString,this),this.orderString=S(this.orderString,this),this.whereString=S(this.whereString,this),this.limit=S(this.limit,this),this.order=S(this.order,this),this.where=S(this.where,this),this.wheres=[],this.orders=[]}return e.prototype.wheres=null,e.prototype.orders=null,e.prototype.limits=null,e.prototype.where=function(e){return e=h(e),""!==e&&this.wheres.push(e),this},e.prototype.order=function(e,t){return t==null&&(t=!0),e=p(e),this.orders.push({field:e,dir:t?"ASC":"DESC"}),this},e.prototype.limit=function(e){return e=d(e),this.limits=e,this},e.prototype.whereString=function(){return 0<this.wheres.length?" WHERE ("+this.wheres.join(") AND (")+")":""},e.prototype.orderString=function(){var e,t,n,r,i;if(0<this.orders.length){t="",i=this.orders;for(n=0,r=i.length;n<r;n++)e=i[n],""!==t&&(t+=", "),t+=""+e.field+" "+e.dir;return" ORDER BY "+t}return""},e.prototype.limitString=function(){return this.limits?" LIMIT "+this.limits:""},e}(),o=function(e){function t(){this.toString=S(this.toString,this),this.offset=S(this.offset,this),this.group=S(this.group,this),this.outer_join=S(this.outer_join,this),this.right_join=S(this.right_join,this),this.left_join=S(this.left_join,this),this.join=S(this.join,this),this.field=S(this.field,this),this.from=S(this.from,this),this.distinct=S(this.distinct,this);var e=this;t.__super__.constructor.apply(this,arguments),this.froms=[],this.fields=[],this.joins=[],this.groups=[],this._join=function(t,n,r,i){return n=m(n),r&&(r=c(r)),i&&(i=h(i)),e.joins.push({type:t,table:n,alias:r,condition:i}),e}}return x(t,e),t.prototype.froms=null,t.prototype.fields=null,t.prototype.joins=null,t.prototype.groups=null,t.prototype.offsets=null,t.prototype.useDistinct=!1,t.prototype.distinct=function(){return this.useDistinct=!0,this},t.prototype.from=function(e,t){return t==null&&(t=null),e=m(e),t&&(t=c(t)),this.froms.push({name:e,alias:t}),this},t.prototype.field=function(e,t){return t==null&&(t=null),e=p(e),t&&(t=c(t)),this.fields.push({field:e,alias:t}),this},t.prototype.join=function(e,t,n){return t==null&&(t=null),n==null&&(n=null),this._join("INNER",e,t,n)},t.prototype.left_join=function(e,t,n){return t==null&&(t=null),n==null&&(n=null),this._join("LEFT",e,t,n)},t.prototype.right_join=function(e,t,n){return t==null&&(t=null),n==null&&(n=null),this._join("RIGHT",e,t,n)},t.prototype.outer_join=function(e,t,n){return t==null&&(t=null),n==null&&(n=null),this._join("OUTER",e,t,n)},t.prototype.group=function(e){return e=p(e),this.groups.push(e),this},t.prototype.offset=function(e){return e=d(e),this.offsets=e,this},t.prototype.toString=function(){var e,t,n,r,i,s,o,u,a,f,l,c,h,p,d,v,m,g,y,b,w;if(0>=this.froms.length)throw new Error("from() needs to be called");o="SELECT ",this.useDistinct&&(o+="DISTINCT "),n="",g=this.fields;for(f=0,p=g.length;f<p;f++)t=g[f],""!==n&&(n+=", "),n+=t.field,t.alias&&(n+=' AS "'+t.alias+'"');o+=""===n?"*":n,a="",y=this.froms;for(l=0,d=y.length;l<d;l++)u=y[l],""!==a&&(a+=", "),a+=u.name,u.alias&&(a+=" `"+u.alias+"`");o+=" FROM "+a,s="",b=this.joins;for(c=0,v=b.length;c<v;c++)i=b[c],s+=" "+i.type+" JOIN "+i.table,i.alias&&(s+=" `"+i.alias+"`"),i.condition&&(s+=" ON ("+i.condition+")");o+=s,o+=this.whereString();if(0<this.groups.length){r="",w=this.groups;for(h=0,m=w.length;h<m;h++)e=w[h],""!==r&&(r+=", "),r+=e;o+=" GROUP BY "+r}return o+=this.orderString(),o+=this.limitString(),this.offsets&&(o+=" OFFSET "+this.offsets),o},t}(a),u=function(e){function n(e){this.toString=S(this.toString,this),this.set=S(this.set,this),this.table=S(this.table,this),n.__super__.constructor.apply(this,arguments),this.tables=[],this.fields={},this.options=b({},t,e)}return x(n,e),n.prototype.tables=null,n.prototype.fields=null,n.prototype.options=null,n.prototype.table=function(e,t){return t==null&&(t=null),e=m(e),t&&(t=c(t)),this.tables.push({name:e,alias:t}),this},n.prototype.set=function(e,t){return e=p(e),t=g(t),this.fields[e]=t,this},n.prototype.toString=function(){var e,t,n,r,i,s,o,u,a,l,c;if(0>=this.tables.length)throw new Error("table() needs to be called");t=function(){var t,n;t=this.fields,n=[];for(e in t){if(!E.call(t,e))continue;n.push(e)}return n}.call(this);if(0>=t.length)throw new Error("set() needs to be called");r="UPDATE ",s="",c=this.tables;for(o=0,a=c.length;o<a;o++)i=c[o],""!==s&&(s+=", "),s+=i.name,i.alias&&(s+=" AS `"+i.alias+"`");r+=s,n="";for(u=0,l=t.length;u<l;u++)e=t[u],""!==n&&(n+=", "),n+=""+e+" = "+f(this.fields[e],this.options);return r+=" SET "+n,r+=this.whereString(),r+=this.orderString(),r+=this.limitString(),r},n}(a),n=function(e){function t(){return this.toString=S(this.toString,this),this.from=S(this.from,this),t.__super__.constructor.apply(this,arguments)}return x(t,e),t.prototype.table=null,t.prototype.from=function(e){return e=m(e),this.table=e,this},t.prototype.toString=function(){var e;if(!this.table)throw new Error("from() needs to be called");return e="DELETE FROM "+this.table,e+=this.whereString(),e+=this.orderString(),e+=this.limitString(),e},t}(a),s=function(){function t(t){this.toString=S(this.toString,this),this.set=S(this.set,this),this.into=S(this.into,this),this.fields={},this.options=b({},e,t)}return t.prototype.table=null,t.prototype.fields=null,t.prototype.options=null,t.prototype.into=function(e){return e=m(e),this.table=e,this},t.prototype.set=function(e,t){return e=p(e),t=g(t),this.fields[e]=t,this},t.prototype.toString=function(){var e,t,n,r,i,s,o;if(!this.table)throw new Error("into() needs to be called");t=function(){var e,t;e=this.fields,t=[];for(r in e){if(!E.call(e,r))continue;t.push(r)}return t}.call(this);if(0>=t.length)throw new Error("set() needs to be called");n="",i="";for(s=0,o=t.length;s<o;s++)e=t[s],""!==n&&(n+=", "),n+=e,""!==i&&(i+=", "),i+=f(this.fields[e],this.options);return"INSERT INTO "+this.table+" ("+n+") VALUES ("+i+")"},t}(),y={expr:function(){return new r},select:function(){return new o},update:function(e){return new u(e)},insert:function(e){return new s(e)},"delete":function(){return new n}},typeof module!="undefined"&&module!==null&&(module.exports=y),typeof window!="undefined"&&window!==null&&(window.squel=y)}).call(this);
+*/(function(){var e,t,n,r,i,s,o,u,a,f,l,c,h,p,d,v,m,g,y,b,w,E=[].slice,S={}.hasOwnProperty,x=function(e,t){return function(){return e.apply(t,arguments)}},T=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};w=function(){var e,t,n,r,i,s,o;e=arguments[0],n=2<=arguments.length?E.call(arguments,1):[];if(n)for(s=0,o=n.length;s<o;s++){r=n[s];if(r)for(t in r){if(!S.call(r,t))continue;i=r[t],e[t]=i}}return e},r=function(){function t(){this.toString=x(this.toString,this),this.or=x(this.or,this),this.and=x(this.and,this),this.end=x(this.end,this),this.or_begin=x(this.or_begin,this),this.and_begin=x(this.and_begin,this);var e=this;this.tree={parent:null,nodes:[]},this.current=this.tree,this._begin=function(t){var n;return n={type:t,parent:e.current,nodes:[]},e.current.nodes.push(n),e.current=e.current.nodes[e.current.nodes.length-1],e}}var e;return t.prototype.tree=null,t.prototype.current=null,t.prototype.and_begin=function(){return this._begin("AND")},t.prototype.or_begin=function(){return this._begin("OR")},t.prototype.end=function(){if(!this.current.parent)throw new Error("begin() needs to be called");return this.current=this.current.parent,this},t.prototype.and=function(e){if(!e||"string"!=typeof e)throw new Error("expr must be a string");return this.current.nodes.push({type:"AND",expr:e}),this},t.prototype.or=function(e){if(!e||"string"!=typeof e)throw new Error("expr must be a string");return this.current.nodes.push({type:"OR",expr:e}),this},t.prototype.toString=function(){if(null!==this.current.parent)throw new Error("end() needs to be called");return e(this.tree)},e=function(t){var n,r,i,s,o,u;i="",u=t.nodes;for(s=0,o=u.length;s<o;s++)n=u[s],n.expr!=null?r=n.expr:(r=e(n),""!==r&&(r="("+r+")")),""!==r&&(""!==i&&(i+=" "+n.type+" "),i+=r);return i},t}(),e=t={usingValuePlaceholders:!1},c=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},i=c(new r),p=function(e){var t;t=typeof e;if(i!==c(e)&&"string"!==t)throw new Error("condition must be a string or Expression instance");return"Expression"===t&&(e=e.toString()),e},m=function(e,t){if("string"!=typeof e)throw new Error(""+t+" must be a string");return e},d=function(e){return m(e,"field name")},g=function(e){return m(e,"table name")},h=function(e){return m(e,"alias")},v=function(e){e=parseInt(e);if(0>e)throw new Error("limit/offset must be >=0");return e},y=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},l=function(e,t){return null===e?e="NULL":"boolean"==typeof e?e=e?"TRUE":"FALSE":"number"!=typeof e&&!1===t.usingValuePlaceholders&&(e="'"+e+"'"),e},o=function(){function e(){}return e}(),f=function(e){function t(){this.limitString=x(this.limitString,this),this.orderString=x(this.orderString,this),this.whereString=x(this.whereString,this),this.limit=x(this.limit,this),this.order=x(this.order,this),this.where=x(this.where,this),this.wheres=[],this.orders=[]}return T(t,e),t.prototype.wheres=null,t.prototype.orders=null,t.prototype.limits=null,t.prototype.where=function(e){return e=p(e),""!==e&&this.wheres.push(e),this},t.prototype.order=function(e,t){return t==null&&(t=!0),e=d(e),this.orders.push({field:e,dir:t?"ASC":"DESC"}),this},t.prototype.limit=function(e){return e=v(e),this.limits=e,this},t.prototype.whereString=function(){return 0<this.wheres.length?" WHERE ("+this.wheres.join(") AND (")+")":""},t.prototype.orderString=function(){var e,t,n,r,i;if(0<this.orders.length){t="",i=this.orders;for(n=0,r=i.length;n<r;n++)e=i[n],""!==t&&(t+=", "),t+=""+e.field+" "+e.dir;return" ORDER BY "+t}return""},t.prototype.limitString=function(){return this.limits?" LIMIT "+this.limits:""},t}(o),u=function(e){function t(){this.toString=x(this.toString,this),this.offset=x(this.offset,this),this.group=x(this.group,this),this.outer_join=x(this.outer_join,this),this.right_join=x(this.right_join,this),this.left_join=x(this.left_join,this),this.join=x(this.join,this),this.field=x(this.field,this),this.from=x(this.from,this),this.distinct=x(this.distinct,this);var e=this;t.__super__.constructor.apply(this,arguments),this.froms=[],this.fields=[],this.joins=[],this.groups=[],this._join=function(t,n,r,i){return n=g(n),r&&(r=h(r)),i&&(i=p(i)),e.joins.push({type:t,table:n,alias:r,condition:i}),e}}return T(t,e),t.prototype.froms=null,t.prototype.fields=null,t.prototype.joins=null,t.prototype.groups=null,t.prototype.offsets=null,t.prototype.useDistinct=!1,t.prototype.distinct=function(){return this.useDistinct=!0,this},t.prototype.from=function(e,t){return t==null&&(t=null),e=g(e),t&&(t=h(t)),this.froms.push({name:e,alias:t}),this},t.prototype.field=function(e,t){return t==null&&(t=null),e=d(e),t&&(t=h(t)),this.fields.push({field:e,alias:t}),this},t.prototype.join=function(e,t,n){return t==null&&(t=null),n==null&&(n=null),this._join("INNER",e,t,n)},t.prototype.left_join=function(e,t,n){return t==null&&(t=null),n==null&&(n=null),this._join("LEFT",e,t,n)},t.prototype.right_join=function(e,t,n){return t==null&&(t=null),n==null&&(n=null),this._join("RIGHT",e,t,n)},t.prototype.outer_join=function(e,t,n){return t==null&&(t=null),n==null&&(n=null),this._join("OUTER",e,t,n)},t.prototype.group=function(e){return e=d(e),this.groups.push(e),this},t.prototype.offset=function(e){return e=v(e),this.offsets=e,this},t.prototype.toString=function(){var e,t,n,r,i,s,o,u,a,f,l,c,h,p,d,v,m,g,y,b,w;if(0>=this.froms.length)throw new Error("from() needs to be called");o="SELECT ",this.useDistinct&&(o+="DISTINCT "),n="",g=this.fields;for(f=0,p=g.length;f<p;f++)t=g[f],""!==n&&(n+=", "),n+=t.field,t.alias&&(n+=' AS "'+t.alias+'"');o+=""===n?"*":n,a="",y=this.froms;for(l=0,d=y.length;l<d;l++)u=y[l],""!==a&&(a+=", "),a+=u.name,u.alias&&(a+=" `"+u.alias+"`");o+=" FROM "+a,s="",b=this.joins;for(c=0,v=b.length;c<v;c++)i=b[c],s+=" "+i.type+" JOIN "+i.table,i.alias&&(s+=" `"+i.alias+"`"),i.condition&&(s+=" ON ("+i.condition+")");o+=s,o+=this.whereString();if(0<this.groups.length){r="",w=this.groups;for(h=0,m=w.length;h<m;h++)e=w[h],""!==r&&(r+=", "),r+=e;o+=" GROUP BY "+r}return o+=this.orderString(),o+=this.limitString(),this.offsets&&(o+=" OFFSET "+this.offsets),o},t}(f),a=function(e){function n(e){this.toString=x(this.toString,this),this.set=x(this.set,this),this.table=x(this.table,this),n.__super__.constructor.apply(this,arguments),this.tables=[],this.fields={},this.options=w({},t,e)}return T(n,e),n.prototype.tables=null,n.prototype.fields=null,n.prototype.options=null,n.prototype.table=function(e,t){return t==null&&(t=null),e=g(e),t&&(t=h(t)),this.tables.push({name:e,alias:t}),this},n.prototype.set=function(e,t){return e=d(e),t=y(t),this.fields[e]=t,this},n.prototype.toString=function(){var e,t,n,r,i,s,o,u,a,f,c;if(0>=this.tables.length)throw new Error("table() needs to be called");t=function(){var t,n;t=this.fields,n=[];for(e in t){if(!S.call(t,e))continue;n.push(e)}return n}.call(this);if(0>=t.length)throw new Error("set() needs to be called");r="UPDATE ",s="",c=this.tables;for(o=0,a=c.length;o<a;o++)i=c[o],""!==s&&(s+=", "),s+=i.name,i.alias&&(s+=" AS `"+i.alias+"`");r+=s,n="";for(u=0,f=t.length;u<f;u++)e=t[u],""!==n&&(n+=", "),n+=""+e+" = "+l(this.fields[e],this.options);return r+=" SET "+n,r+=this.whereString(),r+=this.orderString(),r+=this.limitString(),r},n}(f),n=function(e){function t(){return this.toString=x(this.toString,this),this.from=x(this.from,this),t.__super__.constructor.apply(this,arguments)}return T(t,e),t.prototype.table=null,t.prototype.from=function(e){return e=g(e),this.table=e,this},t.prototype.toString=function(){var e;if(!this.table)throw new Error("from() needs to be called");return e="DELETE FROM "+this.table,e+=this.whereString(),e+=this.orderString(),e+=this.limitString(),e},t}(f),s=function(t){function n(t){this.toString=x(this.toString,this),this.set=x(this.set,this),this.into=x(this.into,this),this.fields={},this.options=w({},e,t)}return T(n,t),n.prototype.table=null,n.prototype.fields=null,n.prototype.options=null,n.prototype.into=function(e){return e=g(e),this.table=e,this},n.prototype.set=function(e,t){return e=d(e),t=y(t),this.fields[e]=t,this},n.prototype.toString=function(){var e,t,n,r,i,s,o;if(!this.table)throw new Error("into() needs to be called");t=function(){var e,t;e=this.fields,t=[];for(r in e){if(!S.call(e,r))continue;t.push(r)}return t}.call(this);if(0>=t.length)throw new Error("set() needs to be called");n="",i="";for(s=0,o=t.length;s<o;s++)e=t[s],""!==n&&(n+=", "),n+=e,""!==i&&(i+=", "),i+=l(this.fields[e],this.options);return"INSERT INTO "+this.table+" ("+n+") VALUES ("+i+")"},n}(o),b={expr:function(){return new r},select:function(){return new u},update:function(e){return new a(e)},insert:function(e){return new s(e)},"delete":function(){return new n},Expression:r,QueryBuilder:o,Select:u,Update:a,Insert:s,Delete:n},typeof module!="undefined"&&module!==null&&(module.exports=b),typeof window!="undefined"&&window!==null&&(window.squel=b)}).call(this);
View
17 src/squel.coffee
@@ -203,9 +203,12 @@ formatValue = (value, options) ->
value
+# Base class for all query builders
+class QueryBuilder
+
# Base class for query builders which support WHERE, ORDER and LIMIT clauses.
-class WhereOrderLimit
+class WhereOrderLimit extends QueryBuilder
wheres: null
orders: null
limits: null
@@ -570,7 +573,7 @@ class Delete extends WhereOrderLimit
# Note that the query builder does not check the final query string for correctness.
#
# All the build methods in this object return the object instance for chained method calling purposes.
-class Insert
+class Insert extends QueryBuilder
table: null
fields: null
options: null
@@ -617,11 +620,19 @@ class Insert
# Export everything as easily usable methods.
-_export =
+_export = {
expr: -> new Expression
select: -> new Select
update: (options) -> new Update(options)
insert: (options) -> new Insert(options)
delete: -> new Delete
+ Expression
+ QueryBuilder
+ Select
+ Update
+ Insert
+ Delete
+}
module?.exports = _export
window?.squel = _export
+
Please sign in to comment.
Something went wrong with that request. Please try again.