This repository has been archived by the owner on Nov 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 231
/
squel.html
369 lines (318 loc) · 86.7 KB
/
squel.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
<!DOCTYPE html> <html> <head> <title>squel.coffee</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th class="docs"> <h1> squel.coffee </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-1">¶</a> </div> <h1>#</h1> </td> <td class="code"> <div class="highlight"><pre><span class="nx">Copyright</span> <span class="p">(</span><span class="nx">c</span><span class="p">)</span> <span class="mi">2012</span> <span class="nx">Ramesh</span> <span class="nx">Nair</span> <span class="p">(</span><span class="nx">hiddentao</span><span class="p">.</span><span class="nx">com</span><span class="p">)</span>
<span class="nx">Permission</span> <span class="o">is</span> <span class="nx">hereby</span> <span class="nx">granted</span><span class="p">,</span> <span class="nx">free</span> <span class="k">of</span> <span class="nx">charge</span><span class="p">,</span> <span class="nx">to</span> <span class="nx">any</span> <span class="nx">person</span>
<span class="nx">obtaining</span> <span class="nx">a</span> <span class="nx">copy</span> <span class="k">of</span> <span class="k">this</span> <span class="nx">software</span> <span class="o">and</span> <span class="nx">associated</span> <span class="nx">documentation</span>
<span class="nx">files</span> <span class="p">(</span><span class="nx">the</span> <span class="s">"Software"</span><span class="p">),</span> <span class="nx">to</span> <span class="nx">deal</span> <span class="k">in</span> <span class="nx">the</span> <span class="nx">Software</span> <span class="nx">without</span>
<span class="nx">restriction</span><span class="p">,</span> <span class="nx">including</span> <span class="nx">without</span> <span class="nx">limitation</span> <span class="nx">the</span> <span class="nx">rights</span> <span class="nx">to</span> <span class="nx">use</span><span class="p">,</span>
<span class="nx">copy</span><span class="p">,</span> <span class="nx">modify</span><span class="p">,</span> <span class="nx">merge</span><span class="p">,</span> <span class="nx">publish</span><span class="p">,</span> <span class="nx">distribute</span><span class="p">,</span> <span class="nx">sublicense</span><span class="p">,</span> <span class="o">and/or</span> <span class="nx">sell</span>
<span class="nx">copies</span> <span class="k">of</span> <span class="nx">the</span> <span class="nx">Software</span><span class="p">,</span> <span class="o">and</span> <span class="nx">to</span> <span class="nx">permit</span> <span class="nx">persons</span> <span class="nx">to</span> <span class="nx">whom</span> <span class="nx">the</span>
<span class="nx">Software</span> <span class="o">is</span> <span class="nx">furnished</span> <span class="nx">to</span> <span class="nx">do</span> <span class="nx">so</span><span class="p">,</span> <span class="nx">subject</span> <span class="nx">to</span> <span class="nx">the</span> <span class="nx">following</span>
<span class="nv">conditions:</span>
<span class="nx">The</span> <span class="nx">above</span> <span class="nx">copyright</span> <span class="nx">notice</span> <span class="o">and</span> <span class="k">this</span> <span class="nx">permission</span> <span class="nx">notice</span> <span class="nx">shall</span> <span class="nx">be</span>
<span class="nx">included</span> <span class="k">in</span> <span class="nx">all</span> <span class="nx">copies</span> <span class="o">or</span> <span class="nx">substantial</span> <span class="nx">portions</span> <span class="k">of</span> <span class="nx">the</span> <span class="nx">Software</span><span class="p">.</span>
<span class="nx">THE</span> <span class="nx">SOFTWARE</span> <span class="nx">IS</span> <span class="nx">PROVIDED</span> <span class="s">"AS IS"</span><span class="p">,</span> <span class="nx">WITHOUT</span> <span class="nx">WARRANTY</span> <span class="nx">OF</span> <span class="nx">ANY</span> <span class="nx">KIND</span><span class="p">,</span>
<span class="nx">EXPRESS</span> <span class="nx">OR</span> <span class="nx">IMPLIED</span><span class="p">,</span> <span class="nx">INCLUDING</span> <span class="nx">BUT</span> <span class="nx">NOT</span> <span class="nx">LIMITED</span> <span class="nx">TO</span> <span class="nx">THE</span> <span class="nx">WARRANTIES</span>
<span class="nx">OF</span> <span class="nx">MERCHANTABILITY</span><span class="p">,</span> <span class="nx">FITNESS</span> <span class="nx">FOR</span> <span class="nx">A</span> <span class="nx">PARTICULAR</span> <span class="nx">PURPOSE</span> <span class="nx">AND</span>
<span class="nx">NONINFRINGEMENT</span><span class="p">.</span> <span class="nx">IN</span> <span class="nx">NO</span> <span class="nx">EVENT</span> <span class="nx">SHALL</span> <span class="nx">THE</span> <span class="nx">AUTHORS</span> <span class="nx">OR</span> <span class="nx">COPYRIGHT</span>
<span class="nx">HOLDERS</span> <span class="nx">BE</span> <span class="nx">LIABLE</span> <span class="nx">FOR</span> <span class="nx">ANY</span> <span class="nx">CLAIM</span><span class="p">,</span> <span class="nx">DAMAGES</span> <span class="nx">OR</span> <span class="nx">OTHER</span> <span class="nx">LIABILITY</span><span class="p">,</span>
<span class="nx">WHETHER</span> <span class="nx">IN</span> <span class="nx">AN</span> <span class="nx">ACTION</span> <span class="nx">OF</span> <span class="nx">CONTRACT</span><span class="p">,</span> <span class="nx">TORT</span> <span class="nx">OR</span> <span class="nx">OTHERWISE</span><span class="p">,</span> <span class="nx">ARISING</span>
<span class="nx">FROM</span><span class="p">,</span> <span class="nx">OUT</span> <span class="nx">OF</span> <span class="nx">OR</span> <span class="nx">IN</span> <span class="nx">CONNECTION</span> <span class="nx">WITH</span> <span class="nx">THE</span> <span class="nx">SOFTWARE</span> <span class="nx">OR</span> <span class="nx">THE</span> <span class="nx">USE</span> <span class="nx">OR</span>
<span class="nx">OTHER</span> <span class="nx">DEALINGS</span> <span class="nx">IN</span> <span class="nx">THE</span> <span class="nx">SOFTWARE</span><span class="p">.</span></pre></div> </td> </tr> <tr id="section-2"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-2">¶</a> </div> <h1>#</h1> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-3">¶</a> </div> <p>Extend given object's with other objects' properties, overriding existing ones if necessary</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_extend = </span><span class="nf">(dst, sources...) -></span>
<span class="k">if</span> <span class="nx">sources</span>
<span class="k">for</span> <span class="nx">src</span> <span class="k">in</span> <span class="nx">sources</span>
<span class="k">if</span> <span class="nx">src</span>
<span class="k">for</span> <span class="nx">own</span> <span class="nx">k</span><span class="p">,</span><span class="nx">v</span> <span class="k">of</span> <span class="nx">src</span>
<span class="nx">dst</span><span class="p">[</span><span class="nx">k</span><span class="p">]</span> <span class="o">=</span> <span class="nx">v</span>
<span class="nx">dst</span></pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-4">¶</a> </div> <p>An SQL expression builder.</p>
<p>SQL expressions are used in WHERE and ON clauses to filter data by various criteria.</p>
<p>This builder works by building up the expression as a hierarchical tree of nodes. The toString() method then
traverses this tree in order to build the final expression string.</p>
<p>Expressions can be nested. Nested expression contains can themselves contain nested expressions.
When rendered a nested expression will be fully contained within brackets.</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">Expression</span></pre></div> </td> </tr> <tr id="section-5"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-5">¶</a> </div> <p>The expression tree.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">tree: </span><span class="kc">null</span></pre></div> </td> </tr> <tr id="section-6"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-6">¶</a> </div> <p>The part of the expression tree we're currently working on.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">current: </span><span class="kc">null</span></pre></div> </td> </tr> <tr id="section-7"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-7">¶</a> </div> <p>Initialise the expression.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">constructor: </span><span class="o">-></span>
<span class="vi">@tree =</span>
<span class="nv">parent: </span><span class="kc">null</span>
<span class="nv">nodes: </span><span class="p">[]</span>
<span class="vi">@current = </span><span class="nx">@tree</span></pre></div> </td> </tr> <tr id="section-8"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-8">¶</a> </div> <p>Begin a nested expression and combine it with the current expression using the given operator.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="vi">@_begin = </span><span class="p">(</span><span class="nx">op</span><span class="p">)</span> <span class="o">=></span>
<span class="nv">new_tree =</span>
<span class="nv">type: </span><span class="nx">op</span>
<span class="nv">parent: </span><span class="nx">@current</span>
<span class="nv">nodes: </span><span class="p">[]</span>
<span class="nx">@current</span><span class="p">.</span><span class="nx">nodes</span><span class="p">.</span><span class="nx">push</span> <span class="nx">new_tree</span>
<span class="vi">@current = </span><span class="nx">@current</span><span class="p">.</span><span class="nx">nodes</span><span class="p">[</span><span class="nx">@current</span><span class="p">.</span><span class="nx">nodes</span><span class="p">.</span><span class="nx">length</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
<span class="nx">@</span></pre></div> </td> </tr> <tr id="section-9"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-9">¶</a> </div> <p>Begin a nested expression and combine it with the current expression using the intersection operator (AND).</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">and_begin: </span><span class="o">=></span>
<span class="nx">@_begin</span> <span class="s">'AND'</span></pre></div> </td> </tr> <tr id="section-10"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-10">¶</a> </div> <p>Begin a nested expression and combine it with the current expression using the union operator (OR).</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">or_begin: </span><span class="o">=></span>
<span class="nx">@_begin</span> <span class="s">'OR'</span></pre></div> </td> </tr> <tr id="section-11"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-11">¶</a> </div> <p>End the current compound expression.</p>
<p>This will throw an error if begin() hasn't been called yet.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">end: </span><span class="o">=></span>
<span class="k">if</span> <span class="o">not</span> <span class="nx">@current</span><span class="p">.</span><span class="nx">parent</span>
<span class="k">throw</span> <span class="k">new</span> <span class="nb">Error</span> <span class="s">"begin() needs to be called"</span>
<span class="vi">@current = </span><span class="nx">@current</span><span class="p">.</span><span class="nx">parent</span>
<span class="nx">@</span></pre></div> </td> </tr> <tr id="section-12"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-12">¶</a> </div> <p>Combine the current expression with the given expression using the intersection operator (AND).</p> </td> <td class="code"> <div class="highlight"><pre> <span class="o">and:</span> <span class="p">(</span><span class="nx">expr</span><span class="p">)</span> <span class="o">=></span>
<span class="k">if</span> <span class="o">not</span> <span class="nx">expr</span> <span class="o">or</span> <span class="s">"string"</span> <span class="o">isnt</span> <span class="k">typeof</span> <span class="nx">expr</span>
<span class="k">throw</span> <span class="k">new</span> <span class="nb">Error</span> <span class="s">"expr must be a string"</span>
<span class="nx">@current</span><span class="p">.</span><span class="nx">nodes</span><span class="p">.</span><span class="nx">push</span>
<span class="nv">type: </span><span class="s">'AND'</span>
<span class="nv">expr: </span><span class="nx">expr</span>
<span class="nx">@</span></pre></div> </td> </tr> <tr id="section-13"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-13">¶</a> </div> <p>Combine the current expression with the given expression using the union operator (OR).</p> </td> <td class="code"> <div class="highlight"><pre> <span class="o">or:</span> <span class="p">(</span><span class="nx">expr</span><span class="p">)</span> <span class="o">=></span>
<span class="k">if</span> <span class="o">not</span> <span class="nx">expr</span> <span class="o">or</span> <span class="s">"string"</span> <span class="o">isnt</span> <span class="k">typeof</span> <span class="nx">expr</span>
<span class="k">throw</span> <span class="k">new</span> <span class="nb">Error</span> <span class="s">"expr must be a string"</span>
<span class="nx">@current</span><span class="p">.</span><span class="nx">nodes</span><span class="p">.</span><span class="nx">push</span>
<span class="nv">type: </span><span class="s">'OR'</span>
<span class="nv">expr: </span><span class="nx">expr</span>
<span class="nx">@</span></pre></div> </td> </tr> <tr id="section-14"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-14">¶</a> </div> <p>Get the final fully constructed expression string.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">toString: </span><span class="o">=></span>
<span class="k">if</span> <span class="kc">null</span> <span class="o">isnt</span> <span class="nx">@current</span><span class="p">.</span><span class="nx">parent</span>
<span class="k">throw</span> <span class="k">new</span> <span class="nb">Error</span> <span class="s">"end() needs to be called"</span>
<span class="nx">_toString</span> <span class="nx">@tree</span></pre></div> </td> </tr> <tr id="section-15"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-15">¶</a> </div> <p>Get a string representation of the given expression tree node.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_toString = </span><span class="nf">(node) -></span>
<span class="nv">str = </span><span class="s">""</span>
<span class="k">for</span> <span class="nx">child</span> <span class="k">in</span> <span class="nx">node</span><span class="p">.</span><span class="nx">nodes</span>
<span class="k">if</span> <span class="nx">child</span><span class="p">.</span><span class="nx">expr</span><span class="o">?</span>
<span class="nv">nodeStr = </span><span class="nx">child</span><span class="p">.</span><span class="nx">expr</span>
<span class="k">else</span>
<span class="nv">nodeStr = </span><span class="nx">_toString</span><span class="p">(</span><span class="nx">child</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-16"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-16">¶</a> </div> <p>wrap nested expressions in brackets</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="s">""</span> <span class="o">isnt</span> <span class="nx">nodeStr</span>
<span class="nv">nodeStr = </span><span class="s">"("</span> <span class="o">+</span> <span class="nx">nodeStr</span> <span class="o">+</span> <span class="s">")"</span>
<span class="k">if</span> <span class="s">""</span> <span class="o">isnt</span> <span class="nx">nodeStr</span></pre></div> </td> </tr> <tr id="section-17"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-17">¶</a> </div> <p>if this isn't first expression then add the operator</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="s">""</span> <span class="o">isnt</span> <span class="nx">str</span> <span class="k">then</span> <span class="nx">str</span> <span class="o">+=</span> <span class="s">" "</span> <span class="o">+</span> <span class="nx">child</span><span class="p">.</span><span class="nx">type</span> <span class="o">+</span> <span class="s">" "</span>
<span class="nx">str</span> <span class="o">+=</span> <span class="nx">nodeStr</span>
<span class="nx">str</span></pre></div> </td> </tr> <tr id="section-18"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-18">¶</a> </div> <p>Default builder options.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">DefaultInsertBuilderOptions = DefaultUpdateBuilderOptions =</span></pre></div> </td> </tr> <tr id="section-19"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-19">¶</a> </div> <p>If true then field values will not be rendered inside quotes so as to allow for field value placeholders (for
parameterized querying).</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">usingValuePlaceholders: </span><span class="kc">false</span></pre></div> </td> </tr> <tr id="section-20"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-20">¶</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-21"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-21">¶</a> </div> <p>Get class name of given object.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_getObjectClassName: </span><span class="nf">(obj) -></span>
<span class="k">if</span> <span class="nx">obj</span> <span class="o">&&</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">constructor</span> <span class="o">&&</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">constructor</span><span class="p">.</span><span class="nx">toString</span>
<span class="nv">arr = </span><span class="nx">obj</span><span class="p">.</span><span class="nx">constructor</span><span class="p">.</span><span class="nx">toString</span><span class="p">().</span><span class="nx">match</span> <span class="sr">/function\s*(\w+)/</span><span class="p">;</span>
<span class="k">if</span> <span class="nx">arr</span> <span class="o">&&</span> <span class="nx">arr</span><span class="p">.</span><span class="nx">length</span> <span class="o">is</span> <span class="mi">2</span>
<span class="k">return</span> <span class="nx">arr</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
<span class="k">return</span> <span class="kc">undefined</span></pre></div> </td> </tr> <tr id="section-22"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-22">¶</a> </div> <p>Sanitize the given condition.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_sanitizeCondition: </span><span class="nf">(condition) -></span>
<span class="nv">t = </span><span class="k">typeof</span> <span class="nx">condition</span>
<span class="nv">c = </span><span class="nx">@_getObjectClassName</span><span class="p">(</span><span class="nx">condition</span><span class="p">)</span>
<span class="k">if</span> <span class="s">'Expression'</span> <span class="o">isnt</span> <span class="nx">c</span> <span class="o">and</span> <span class="s">"string"</span> <span class="o">isnt</span> <span class="nx">t</span>
<span class="k">throw</span> <span class="k">new</span> <span class="nb">Error</span> <span class="s">"condition must be a string or Expression instance"</span></pre></div> </td> </tr> <tr id="section-23"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-23">¶</a> </div> <p>If it's an expression builder instance then convert it to string form.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="s">'Expression'</span> <span class="o">is</span> <span class="nx">t</span> <span class="o">or</span> <span class="s">'Expression'</span> <span class="o">is</span> <span class="nx">c</span>
<span class="nv">condition = </span><span class="nx">condition</span><span class="p">.</span><span class="nx">toString</span><span class="p">()</span>
<span class="nx">condition</span></pre></div> </td> </tr> <tr id="section-24"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-24">¶</a> </div> <p>Sanitize the given name.
The 'type' parameter is used to construct a meaningful error message in case validation fails.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_sanitizeName: </span><span class="nf">(value, type) -></span>
<span class="k">if</span> <span class="s">"string"</span> <span class="o">isnt</span> <span class="k">typeof</span> <span class="nx">value</span>
<span class="k">throw</span> <span class="k">new</span> <span class="nb">Error</span> <span class="s">"</span><span class="si">#{</span><span class="nx">type</span><span class="si">}</span><span class="s"> must be a string"</span>
<span class="nx">value</span>
<span class="nv">_sanitizeField: </span><span class="nf">(item) -></span> <span class="nx">@_sanitizeName</span> <span class="nx">item</span><span class="p">,</span> <span class="s">"field name"</span>
<span class="nv">_sanitizeTable: </span><span class="nf">(item) -></span> <span class="nx">@_sanitizeName</span> <span class="nx">item</span><span class="p">,</span> <span class="s">"table name"</span>
<span class="nv">_sanitizeAlias: </span><span class="nf">(item) -></span> <span class="nx">@_sanitizeName</span> <span class="nx">item</span><span class="p">,</span> <span class="s">"alias"</span></pre></div> </td> </tr> <tr id="section-25"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-25">¶</a> </div> <p>Sanitize the given limit/offset value.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_sanitizeLimitOffset: </span><span class="nf">(value) -></span>
<span class="nv">value = </span><span class="nb">parseInt</span><span class="p">(</span><span class="nx">value</span><span class="p">)</span>
<span class="k">if</span> <span class="mi">0</span> <span class="o">></span> <span class="nx">value</span>
<span class="k">throw</span> <span class="k">new</span> <span class="nb">Error</span> <span class="s">"limit/offset must be >=0"</span>
<span class="nx">value</span></pre></div> </td> </tr> <tr id="section-26"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-26">¶</a> </div> <p>Santize the given field value</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_sanitizeValue: </span><span class="nf">(item) -></span>
<span class="nv">t = </span><span class="k">typeof</span> <span class="nx">item</span>
<span class="k">if</span> <span class="kc">null</span> <span class="o">isnt</span> <span class="nx">item</span> <span class="o">and</span> <span class="s">"string"</span> <span class="o">isnt</span> <span class="nx">t</span> <span class="o">and</span> <span class="s">"number"</span> <span class="o">isnt</span> <span class="nx">t</span> <span class="o">and</span> <span class="s">"boolean"</span> <span class="o">isnt</span> <span class="nx">t</span>
<span class="k">throw</span> <span class="k">new</span> <span class="nb">Error</span> <span class="s">"field value must be a string, number, boolean or null"</span>
<span class="nx">item</span></pre></div> </td> </tr> <tr id="section-27"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-27">¶</a> </div> <p>Format the given field value for inclusion into the query string</p>
<p>options: see DefaultBuilderOptions</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_formatValue: </span><span class="nf">(value, options) -></span>
<span class="k">if</span> <span class="kc">null</span> <span class="o">is</span> <span class="nx">value</span>
<span class="nv">value = </span><span class="s">"NULL"</span>
<span class="k">else</span> <span class="k">if</span> <span class="s">"boolean"</span> <span class="o">is</span> <span class="k">typeof</span> <span class="nx">value</span>
<span class="nv">value = </span><span class="k">if</span> <span class="nx">value</span> <span class="k">then</span> <span class="s">"TRUE"</span> <span class="k">else</span> <span class="s">"FALSE"</span>
<span class="k">else</span> <span class="k">if</span> <span class="s">"number"</span> <span class="o">isnt</span> <span class="k">typeof</span> <span class="nx">value</span>
<span class="k">if</span> <span class="o">not</span> <span class="nx">options</span> <span class="o">or</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">"'</span><span class="si">#{</span><span class="nx">value</span><span class="si">}</span><span class="s">'"</span>
<span class="nx">value</span></pre></div> </td> </tr> <tr id="section-28"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-28">¶</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">-></span>
<span class="k">super</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-29"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-29">¶</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">=></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">""</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-30"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-30">¶</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">=></span>
<span class="nv">field = </span><span class="nx">@_sanitizeField</span><span class="p">(</span><span class="nx">field</span><span class="p">)</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">"ASC"</span> <span class="k">else</span> <span class="s">"DESC"</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">¶</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">=></span>
<span class="nv">max = </span><span class="nx">@_sanitizeLimitOffset</span><span class="p">(</span><span class="nx">max</span><span class="p">)</span>
<span class="vi">@limits = </span><span class="nx">max</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">¶</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">=></span>
<span class="k">if</span> <span class="mi">0</span> <span class="o"><</span> <span class="nx">@wheres</span><span class="p">.</span><span class="nx">length</span>
<span class="s">" WHERE ("</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">") AND ("</span><span class="p">)</span> <span class="o">+</span> <span class="s">")"</span>
<span class="k">else</span>
<span class="s">""</span></pre></div> </td> </tr> <tr id="section-33"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-33">¶</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">=></span>
<span class="k">if</span> <span class="mi">0</span> <span class="o"><</span> <span class="nx">@orders</span><span class="p">.</span><span class="nx">length</span>
<span class="nv">orders = </span><span class="s">""</span>
<span class="k">for</span> <span class="nx">o</span> <span class="k">in</span> <span class="nx">@orders</span>
<span class="nx">orders</span> <span class="o">+=</span> <span class="s">", "</span> <span class="k">if</span> <span class="s">""</span> <span class="o">isnt</span> <span class="nx">orders</span>
<span class="nx">orders</span> <span class="o">+=</span> <span class="s">"</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">"</span>
<span class="s">" ORDER BY </span><span class="si">#{</span><span class="nx">orders</span><span class="si">}</span><span class="s">"</span>
<span class="k">else</span>
<span class="s">""</span></pre></div> </td> </tr> <tr id="section-34"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-34">¶</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">=></span>
<span class="k">if</span> <span class="nx">@limits</span>
<span class="s">" LIMIT </span><span class="si">#{</span><span class="nx">@limits</span><span class="si">}</span><span class="s">"</span>
<span class="k">else</span>
<span class="s">""</span></pre></div> </td> </tr> <tr id="section-35"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-35">¶</a> </div> <p>Base class for query builders with JOIN clauses.</p> </td> <td class="code"> <div class="highlight"><pre><span class="k">class</span> <span class="nx">JoinWhereOrderLimit</span> <span class="k">extends</span> <span class="nx">WhereOrderLimit</span>
<span class="nv">joins: </span><span class="kc">null</span>
<span class="nv">constructor: </span><span class="o">-></span>
<span class="k">super</span></pre></div> </td> </tr> <tr id="section-36"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-36">¶</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>
<p>'table' is the name of the table to join with.</p>
<p>'alias' is an optional alias for the table name.</p>
<p>'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().</p> </td> <td class="code"> <div class="highlight"><pre> <span class="vi">@_join = </span><span class="p">(</span><span class="nx">type</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><span class="p">)</span> <span class="o">=></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="nv">alias = </span><span class="nx">@_sanitizeAlias</span><span class="p">(</span><span class="nx">alias</span><span class="p">)</span> <span class="k">if</span> <span class="nx">alias</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="nx">condition</span>
<span class="nx">@joins</span><span class="p">.</span><span class="nx">push</span>
<span class="nv">type: </span><span class="nx">type</span>
<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-37"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-37">¶</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">=></span>
<span class="nx">@_join</span> <span class="s">'INNER'</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-38"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-38">¶</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">=></span>
<span class="nx">@_join</span> <span class="s">'LEFT'</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-39"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-39">¶</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">=></span>
<span class="nx">@_join</span> <span class="s">'RIGHT'</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">¶</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">=></span>
<span class="nx">@_join</span> <span class="s">'OUTER'</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">¶</a> </div> <p>Get string representation of JOIN clauses, if any</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">joinString: </span><span class="o">=></span>
<span class="nv">joins = </span><span class="s">""</span>
<span class="k">for</span> <span class="nx">j</span> <span class="k">in</span> <span class="p">(</span><span class="nx">@joins</span> <span class="o">or</span> <span class="p">[])</span>
<span class="nx">joins</span> <span class="o">+=</span> <span class="s">" </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">"</span>
<span class="nx">joins</span> <span class="o">+=</span> <span class="s">" `</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">`"</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">" 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">)"</span> <span class="k">if</span> <span class="nx">j</span><span class="p">.</span><span class="nx">condition</span>
<span class="nx">joins</span></pre></div> </td> </tr> <tr id="section-42"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-42">¶</a> </div> <p>A SELECT 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">Select</span> <span class="k">extends</span> <span class="nx">JoinWhereOrderLimit</span>
<span class="nv">froms: </span><span class="kc">null</span>
<span class="nv">fields: </span><span class="kc">null</span>
<span class="nv">groups: </span><span class="kc">null</span>
<span class="nv">offsets: </span><span class="kc">null</span>
<span class="nv">useDistinct: </span><span class="kc">false</span>
<span class="nv">constructor: </span><span class="o">-></span>
<span class="k">super</span>
<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-43"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-43">¶</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">=></span>
<span class="vi">@useDistinct = </span><span class="kc">true</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">¶</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">=></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="nv">alias = </span><span class="nx">@_sanitizeAlias</span><span class="p">(</span><span class="nx">alias</span><span class="p">)</span> <span class="k">if</span> <span class="nx">alias</span>
<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-45"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-45">¶</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>
<p>An alias may also be specified for this field.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">field: </span><span class="p">(</span><span class="nx">field</span><span class="p">,</span> <span class="nv">alias = </span><span class="kc">null</span><span class="p">)</span> <span class="o">=></span>
<span class="nv">field = </span><span class="nx">@_sanitizeField</span><span class="p">(</span><span class="nx">field</span><span class="p">)</span>
<span class="nv">alias = </span><span class="nx">@_sanitizeAlias</span><span class="p">(</span><span class="nx">alias</span><span class="p">)</span> <span class="k">if</span> <span class="nx">alias</span>
<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-46"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-46">¶</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">=></span>
<span class="nv">field = </span><span class="nx">@_sanitizeField</span><span class="p">(</span><span class="nx">field</span><span class="p">)</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-47"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-47">¶</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">=></span>
<span class="nv">start = </span><span class="nx">@_sanitizeLimitOffset</span><span class="p">(</span><span class="nx">start</span><span class="p">)</span>
<span class="vi">@offsets = </span><span class="nx">start</span>
<span class="nx">@</span></pre></div> </td> </tr> <tr id="section-48"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-48">¶</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">=></span></pre></div> </td> </tr> <tr id="section-49"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-49">¶</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">>=</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">"from() needs to be called"</span>
<span class="nv">ret = </span><span class="s">"SELECT "</span></pre></div> </td> </tr> <tr id="section-50"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-50">¶</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">"DISTINCT "</span> <span class="k">if</span> <span class="nx">@useDistinct</span></pre></div> </td> </tr> <tr id="section-51"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-51">¶</a> </div> <p>fields</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">fields = </span><span class="s">""</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">", "</span> <span class="k">if</span> <span class="s">""</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">" AS \"</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">\""</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">""</span> <span class="o">is</span> <span class="nx">fields</span> <span class="k">then</span> <span class="s">"*"</span> <span class="k">else</span> <span class="nx">fields</span></pre></div> </td> </tr> <tr id="section-52"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-52">¶</a> </div> <p>tables</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">tables = </span><span class="s">""</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">", "</span> <span class="k">if</span> <span class="s">""</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">" `</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">`"</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">" FROM </span><span class="si">#{</span><span class="nx">tables</span><span class="si">}</span><span class="s">"</span></pre></div> </td> </tr> <tr id="section-53"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-53">¶</a> </div> <p>joins</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">ret</span> <span class="o">+=</span> <span class="nx">@joinString</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">¶</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-55"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-55">¶</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"><</span> <span class="nx">@groups</span><span class="p">.</span><span class="nx">length</span>
<span class="nv">groups = </span><span class="s">""</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">", "</span> <span class="k">if</span> <span class="s">""</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">" GROUP BY </span><span class="si">#{</span><span class="nx">groups</span><span class="si">}</span><span class="s">"</span></pre></div> </td> </tr> <tr id="section-56"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-56">¶</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-57"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-57">¶</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-58"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-58">¶</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">" OFFSET </span><span class="si">#{</span><span class="nx">@offsets</span><span class="si">}</span><span class="s">"</span> <span class="k">if</span> <span class="nx">@offsets</span>
<span class="nx">ret</span></pre></div> </td> </tr> <tr id="section-59"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-59">¶</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-60"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-60">¶</a> </div> <p>options: see DefaultBuilderOptions</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">constructor: </span><span class="nf">(options) -></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-61"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-61">¶</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">=></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="nv">alias = </span><span class="nx">@_sanitizeAlias</span><span class="p">(</span><span class="nx">alias</span><span class="p">)</span> <span class="k">if</span> <span class="nx">alias</span>
<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-62"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-62">¶</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">=></span>
<span class="nv">field = </span><span class="nx">@_sanitizeField</span><span class="p">(</span><span class="nx">field</span><span class="p">)</span>
<span class="nv">value = </span><span class="nx">@_sanitizeValue</span><span class="p">(</span><span class="nx">value</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="o">=</span> <span class="nx">value</span>
<span class="nx">@</span></pre></div> </td> </tr> <tr id="section-63"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-63">¶</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">=></span></pre></div> </td> </tr> <tr id="section-64"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-64">¶</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">>=</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">"table() needs to be called"</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">>=</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">"set() needs to be called"</span>
<span class="nv">ret = </span><span class="s">"UPDATE "</span></pre></div> </td> </tr> <tr id="section-65"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-65">¶</a> </div> <p>tables</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">tables = </span><span class="s">""</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">", "</span> <span class="k">if</span> <span class="s">""</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">" 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">`"</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-66"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-66">¶</a> </div> <p>fields</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">fields = </span><span class="s">""</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">", "</span> <span class="k">if</span> <span class="s">""</span> <span class="o">isnt</span> <span class="nx">fields</span>
<span class="nx">fields</span> <span class="o">+=</span> <span class="s">"</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">"</span>
<span class="nx">ret</span> <span class="o">+=</span> <span class="s">" SET </span><span class="si">#{</span><span class="nx">fields</span><span class="si">}</span><span class="s">"</span></pre></div> </td> </tr> <tr id="section-67"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-67">¶</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-68"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-68">¶</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-69"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-69">¶</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-70"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-70">¶</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">JoinWhereOrderLimit</span>
<span class="nv">table: </span><span class="kc">null</span></pre></div> </td> </tr> <tr id="section-71"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-71">¶</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">=></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-72"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-72">¶</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">=></span></pre></div> </td> </tr> <tr id="section-73"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-73">¶</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">"from() needs to be called"</span>
<span class="nv">ret = </span><span class="s">"DELETE FROM </span><span class="si">#{</span><span class="nx">@table</span><span class="si">}</span><span class="s">"</span></pre></div> </td> </tr> <tr id="section-74"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-74">¶</a> </div> <p>joins</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">ret</span> <span class="o">+=</span> <span class="nx">@joinString</span><span class="p">()</span></pre></div> </td> </tr> <tr id="section-75"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-75">¶</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-76"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-76">¶</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-77"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-77">¶</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-78"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-78">¶</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> <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-79"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-79">¶</a> </div> <p>options: see DefaultBuilderOptions</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">constructor: </span><span class="nf">(options) -></span>
<span class="k">super</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-80"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-80">¶</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">=></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-81"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-81">¶</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">=></span>
<span class="nv">field = </span><span class="nx">@_sanitizeField</span><span class="p">(</span><span class="nx">field</span><span class="p">)</span>
<span class="nv">value = </span><span class="nx">@_sanitizeValue</span><span class="p">(</span><span class="nx">value</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="o">=</span> <span class="nx">value</span>
<span class="nx">@</span></pre></div> </td> </tr> <tr id="section-82"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-82">¶</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">=></span></pre></div> </td> </tr> <tr id="section-83"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-83">¶</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">"into() needs to be called"</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">>=</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">"set() needs to be called"</span></pre></div> </td> </tr> <tr id="section-84"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-84">¶</a> </div> <p>fields</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">fields = </span><span class="s">""</span>
<span class="nv">values = </span><span class="s">""</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">", "</span> <span class="k">if</span> <span class="s">""</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="nx">values</span> <span class="o">+=</span> <span class="s">", "</span> <span class="k">if</span> <span class="s">""</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">"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">)"</span></pre></div> </td> </tr> <tr id="section-85"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-85">¶</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">-></span> <span class="k">new</span> <span class="nx">Expression</span>
<span class="nv">select: </span><span class="o">-></span> <span class="k">new</span> <span class="nx">Select</span>
<span class="nv">update: </span><span class="nf">(options) -></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) -></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">-></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>
</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>