knex 2.4.1 → 2.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (187) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/CONTRIBUTING.md +194 -194
  3. package/LICENSE +22 -22
  4. package/README.md +148 -148
  5. package/UPGRADING.md +233 -233
  6. package/bin/cli.js +473 -472
  7. package/bin/utils/cli-config-utils.js +210 -210
  8. package/bin/utils/constants.js +7 -7
  9. package/bin/utils/migrationsLister.js +37 -37
  10. package/knex.js +23 -23
  11. package/lib/builder-interface-augmenter.js +120 -120
  12. package/lib/client.js +475 -475
  13. package/lib/constants.js +61 -61
  14. package/lib/dialects/better-sqlite3/index.js +72 -72
  15. package/lib/dialects/cockroachdb/crdb-columncompiler.js +14 -14
  16. package/lib/dialects/cockroachdb/crdb-querybuilder.js +11 -11
  17. package/lib/dialects/cockroachdb/crdb-querycompiler.js +122 -122
  18. package/lib/dialects/cockroachdb/crdb-tablecompiler.js +37 -37
  19. package/lib/dialects/cockroachdb/crdb-viewcompiler.js +15 -15
  20. package/lib/dialects/cockroachdb/index.js +86 -86
  21. package/lib/dialects/mssql/index.js +495 -495
  22. package/lib/dialects/mssql/mssql-formatter.js +34 -34
  23. package/lib/dialects/mssql/query/mssql-querycompiler.js +600 -600
  24. package/lib/dialects/mssql/schema/mssql-columncompiler.js +185 -185
  25. package/lib/dialects/mssql/schema/mssql-compiler.js +91 -91
  26. package/lib/dialects/mssql/schema/mssql-tablecompiler.js +378 -378
  27. package/lib/dialects/mssql/schema/mssql-viewcompiler.js +55 -55
  28. package/lib/dialects/mssql/transaction.js +176 -176
  29. package/lib/dialects/mysql/index.js +201 -201
  30. package/lib/dialects/mysql/query/mysql-querycompiler.js +274 -274
  31. package/lib/dialects/mysql/schema/mysql-columncompiler.js +193 -193
  32. package/lib/dialects/mysql/schema/mysql-compiler.js +60 -60
  33. package/lib/dialects/mysql/schema/mysql-tablecompiler.js +381 -381
  34. package/lib/dialects/mysql/schema/mysql-viewbuilder.js +21 -21
  35. package/lib/dialects/mysql/schema/mysql-viewcompiler.js +15 -15
  36. package/lib/dialects/mysql/transaction.js +46 -46
  37. package/lib/dialects/mysql2/index.js +33 -33
  38. package/lib/dialects/mysql2/transaction.js +44 -44
  39. package/lib/dialects/oracle/DEAD_CODE.md +5 -5
  40. package/lib/dialects/oracle/index.js +92 -92
  41. package/lib/dialects/oracle/query/oracle-querycompiler.js +342 -342
  42. package/lib/dialects/oracle/schema/internal/incrementUtils.js +20 -20
  43. package/lib/dialects/oracle/schema/internal/trigger.js +135 -135
  44. package/lib/dialects/oracle/schema/oracle-columnbuilder.js +17 -17
  45. package/lib/dialects/oracle/schema/oracle-columncompiler.js +126 -126
  46. package/lib/dialects/oracle/schema/oracle-compiler.js +122 -122
  47. package/lib/dialects/oracle/schema/oracle-tablecompiler.js +190 -190
  48. package/lib/dialects/oracle/utils.js +87 -87
  49. package/lib/dialects/oracledb/index.js +327 -327
  50. package/lib/dialects/oracledb/query/oracledb-querycompiler.js +481 -481
  51. package/lib/dialects/oracledb/schema/oracledb-columncompiler.js +55 -55
  52. package/lib/dialects/oracledb/schema/oracledb-tablecompiler.js +19 -19
  53. package/lib/dialects/oracledb/schema/oracledb-viewbuilder.js +13 -13
  54. package/lib/dialects/oracledb/schema/oracledb-viewcompiler.js +19 -19
  55. package/lib/dialects/oracledb/transaction.js +98 -98
  56. package/lib/dialects/oracledb/utils.js +208 -208
  57. package/lib/dialects/pgnative/index.js +60 -60
  58. package/lib/dialects/postgres/execution/pg-transaction.js +12 -12
  59. package/lib/dialects/postgres/index.js +358 -358
  60. package/lib/dialects/postgres/query/pg-querybuilder.js +38 -38
  61. package/lib/dialects/postgres/query/pg-querycompiler.js +395 -395
  62. package/lib/dialects/postgres/schema/pg-columncompiler.js +156 -156
  63. package/lib/dialects/postgres/schema/pg-compiler.js +138 -136
  64. package/lib/dialects/postgres/schema/pg-tablecompiler.js +299 -299
  65. package/lib/dialects/postgres/schema/pg-viewbuilder.js +21 -21
  66. package/lib/dialects/postgres/schema/pg-viewcompiler.js +35 -35
  67. package/lib/dialects/redshift/index.js +86 -86
  68. package/lib/dialects/redshift/query/redshift-querycompiler.js +163 -163
  69. package/lib/dialects/redshift/schema/redshift-columnbuilder.js +22 -22
  70. package/lib/dialects/redshift/schema/redshift-columncompiler.js +67 -67
  71. package/lib/dialects/redshift/schema/redshift-compiler.js +14 -14
  72. package/lib/dialects/redshift/schema/redshift-tablecompiler.js +122 -122
  73. package/lib/dialects/redshift/schema/redshift-viewcompiler.js +11 -11
  74. package/lib/dialects/redshift/transaction.js +25 -25
  75. package/lib/dialects/sqlite3/execution/sqlite-transaction.js +18 -18
  76. package/lib/dialects/sqlite3/index.js +250 -250
  77. package/lib/dialects/sqlite3/query/sqlite-querybuilder.js +33 -33
  78. package/lib/dialects/sqlite3/query/sqlite-querycompiler.js +334 -334
  79. package/lib/dialects/sqlite3/schema/ddl.js +400 -400
  80. package/lib/dialects/sqlite3/schema/internal/compiler.js +327 -327
  81. package/lib/dialects/sqlite3/schema/internal/parser-combinator.js +161 -161
  82. package/lib/dialects/sqlite3/schema/internal/parser.js +638 -638
  83. package/lib/dialects/sqlite3/schema/internal/sqlite-ddl-operations.js +41 -41
  84. package/lib/dialects/sqlite3/schema/internal/tokenizer.js +38 -38
  85. package/lib/dialects/sqlite3/schema/internal/utils.js +12 -12
  86. package/lib/dialects/sqlite3/schema/sqlite-columncompiler.js +50 -50
  87. package/lib/dialects/sqlite3/schema/sqlite-compiler.js +80 -80
  88. package/lib/dialects/sqlite3/schema/sqlite-tablecompiler.js +347 -347
  89. package/lib/dialects/sqlite3/schema/sqlite-viewcompiler.js +40 -40
  90. package/lib/execution/batch-insert.js +51 -51
  91. package/lib/execution/internal/delay.js +6 -6
  92. package/lib/execution/internal/ensure-connection-callback.js +41 -41
  93. package/lib/execution/internal/query-executioner.js +62 -62
  94. package/lib/execution/runner.js +307 -307
  95. package/lib/execution/transaction.js +401 -401
  96. package/lib/formatter/formatterUtils.js +42 -42
  97. package/lib/formatter/rawFormatter.js +84 -84
  98. package/lib/formatter/wrappingFormatter.js +250 -250
  99. package/lib/formatter.js +25 -25
  100. package/lib/index.js +3 -3
  101. package/lib/knex-builder/FunctionHelper.js +54 -54
  102. package/lib/knex-builder/Knex.js +59 -59
  103. package/lib/knex-builder/internal/config-resolver.js +57 -57
  104. package/lib/knex-builder/internal/parse-connection.js +87 -87
  105. package/lib/knex-builder/make-knex.js +340 -340
  106. package/lib/logger.js +76 -76
  107. package/lib/migrations/common/MigrationsLoader.js +36 -36
  108. package/lib/migrations/migrate/MigrationGenerator.js +84 -82
  109. package/lib/migrations/migrate/Migrator.js +598 -598
  110. package/lib/migrations/migrate/migrate-stub.js +17 -17
  111. package/lib/migrations/migrate/migration-list-resolver.js +33 -33
  112. package/lib/migrations/migrate/migrator-configuration-merger.js +58 -58
  113. package/lib/migrations/migrate/sources/fs-migrations.js +74 -74
  114. package/lib/migrations/migrate/stub/cjs.stub +15 -15
  115. package/lib/migrations/migrate/stub/coffee.stub +13 -13
  116. package/lib/migrations/migrate/stub/eg.stub +14 -14
  117. package/lib/migrations/migrate/stub/js-schema.stub +22 -22
  118. package/lib/migrations/migrate/stub/js.stub +22 -22
  119. package/lib/migrations/migrate/stub/knexfile-coffee.stub +34 -34
  120. package/lib/migrations/migrate/stub/knexfile-eg.stub +43 -43
  121. package/lib/migrations/migrate/stub/knexfile-js.stub +47 -47
  122. package/lib/migrations/migrate/stub/knexfile-ls.stub +35 -35
  123. package/lib/migrations/migrate/stub/knexfile-ts.stub +47 -47
  124. package/lib/migrations/migrate/stub/ls.stub +14 -14
  125. package/lib/migrations/migrate/stub/mjs.stub +23 -23
  126. package/lib/migrations/migrate/stub/ts-schema.stub +21 -21
  127. package/lib/migrations/migrate/stub/ts.stub +21 -21
  128. package/lib/migrations/migrate/table-creator.js +77 -77
  129. package/lib/migrations/migrate/table-resolver.js +27 -27
  130. package/lib/migrations/seed/Seeder.js +137 -137
  131. package/lib/migrations/seed/seed-stub.js +13 -13
  132. package/lib/migrations/seed/seeder-configuration-merger.js +60 -60
  133. package/lib/migrations/seed/sources/fs-seeds.js +65 -65
  134. package/lib/migrations/seed/stub/coffee.stub +9 -9
  135. package/lib/migrations/seed/stub/eg.stub +11 -11
  136. package/lib/migrations/seed/stub/js.stub +13 -13
  137. package/lib/migrations/seed/stub/ls.stub +11 -11
  138. package/lib/migrations/seed/stub/mjs.stub +12 -12
  139. package/lib/migrations/seed/stub/ts.stub +13 -13
  140. package/lib/migrations/util/fs.js +86 -86
  141. package/lib/migrations/util/import-file.js +12 -12
  142. package/lib/migrations/util/is-module-type.js +9 -9
  143. package/lib/migrations/util/template.js +52 -52
  144. package/lib/migrations/util/timestamp.js +14 -14
  145. package/lib/query/analytic.js +52 -52
  146. package/lib/query/constants.js +15 -15
  147. package/lib/query/joinclause.js +270 -270
  148. package/lib/query/method-constants.js +135 -135
  149. package/lib/query/querybuilder.js +1794 -1794
  150. package/lib/query/querycompiler.js +1580 -1580
  151. package/lib/raw.js +139 -139
  152. package/lib/ref.js +39 -39
  153. package/lib/schema/builder.js +115 -114
  154. package/lib/schema/columnbuilder.js +146 -145
  155. package/lib/schema/columncompiler.js +307 -307
  156. package/lib/schema/compiler.js +187 -187
  157. package/lib/schema/internal/helpers.js +55 -55
  158. package/lib/schema/tablebuilder.js +376 -375
  159. package/lib/schema/tablecompiler.js +433 -433
  160. package/lib/schema/viewbuilder.js +92 -93
  161. package/lib/schema/viewcompiler.js +138 -138
  162. package/lib/util/finally-mixin.js +13 -13
  163. package/lib/util/helpers.js +95 -95
  164. package/lib/util/is.js +32 -32
  165. package/lib/util/nanoid.js +40 -40
  166. package/lib/util/noop.js +1 -1
  167. package/lib/util/save-async-stack.js +14 -14
  168. package/lib/util/string.js +190 -190
  169. package/lib/util/timeout.js +29 -29
  170. package/package.json +8 -6
  171. package/scripts/build.js +125 -125
  172. package/scripts/clean.js +31 -29
  173. package/scripts/docker-compose.yml +152 -152
  174. package/scripts/next-release-howto.md +24 -24
  175. package/scripts/oracledb-install-driver-libs.sh +82 -82
  176. package/scripts/release.sh +34 -34
  177. package/scripts/runkit-example.js +34 -34
  178. package/scripts/stress-test/README.txt +18 -18
  179. package/scripts/stress-test/docker-compose.yml +57 -57
  180. package/scripts/stress-test/knex-stress-test.js +208 -208
  181. package/scripts/stress-test/mysql2-random-hanging-every-now-and-then.js +145 -145
  182. package/scripts/stress-test/mysql2-sudden-exit-without-error.js +100 -100
  183. package/scripts/stress-test/reconnect-test-mysql-based-drivers.js +184 -184
  184. package/scripts/update_gitignore_for_tsc_output.js +90 -86
  185. package/types/index.d.ts +3233 -3233
  186. package/types/result.d.ts +27 -27
  187. package/types/tables.d.ts +4 -4
@@ -1,1794 +1,1794 @@
1
- // Builder
2
- // -------
3
- const assert = require('assert');
4
- const { EventEmitter } = require('events');
5
- const assign = require('lodash/assign');
6
- const clone = require('lodash/clone');
7
- const each = require('lodash/each');
8
- const isEmpty = require('lodash/isEmpty');
9
- const isPlainObject = require('lodash/isPlainObject');
10
- const last = require('lodash/last');
11
- const reject = require('lodash/reject');
12
- const tail = require('lodash/tail');
13
- const toArray = require('lodash/toArray');
14
-
15
- const { addQueryContext, normalizeArr } = require('../util/helpers');
16
- const JoinClause = require('./joinclause');
17
- const Analytic = require('./analytic');
18
- const saveAsyncStack = require('../util/save-async-stack');
19
- const {
20
- isBoolean,
21
- isNumber,
22
- isObject,
23
- isString,
24
- isFunction,
25
- } = require('../util/is');
26
-
27
- const { lockMode, waitMode } = require('./constants');
28
- const {
29
- augmentWithBuilderInterface,
30
- } = require('../builder-interface-augmenter');
31
-
32
- const SELECT_COMMANDS = new Set(['pluck', 'first', 'select']);
33
- const CLEARABLE_STATEMENTS = new Set([
34
- 'with',
35
- 'select',
36
- 'columns',
37
- 'hintComments',
38
- 'where',
39
- 'union',
40
- 'join',
41
- 'group',
42
- 'order',
43
- 'having',
44
- 'limit',
45
- 'offset',
46
- 'counter',
47
- 'counters',
48
- ]);
49
- const LOCK_MODES = new Set([
50
- lockMode.forShare,
51
- lockMode.forUpdate,
52
- lockMode.forNoKeyUpdate,
53
- lockMode.forKeyShare,
54
- ]);
55
-
56
- // Typically called from `knex.builder`,
57
- // start a new query building chain.
58
- class Builder extends EventEmitter {
59
- constructor(client) {
60
- super();
61
- this.client = client;
62
- this.and = this;
63
- this._single = {};
64
- this._statements = [];
65
- this._method = 'select';
66
- if (client.config) {
67
- saveAsyncStack(this, 5);
68
- this._debug = client.config.debug;
69
- }
70
- // Internal flags used in the builder.
71
- this._joinFlag = 'inner';
72
- this._boolFlag = 'and';
73
- this._notFlag = false;
74
- this._asColumnFlag = false;
75
- }
76
-
77
- toString() {
78
- return this.toQuery();
79
- }
80
-
81
- // Convert the current query "toSQL"
82
- toSQL(method, tz) {
83
- return this.client.queryCompiler(this).toSQL(method || this._method, tz);
84
- }
85
-
86
- // Create a shallow clone of the current query builder.
87
- clone() {
88
- const cloned = new this.constructor(this.client);
89
- cloned._method = this._method;
90
- cloned._single = clone(this._single);
91
- cloned._statements = clone(this._statements);
92
- cloned._debug = this._debug;
93
-
94
- // `_option` is assigned by the `Interface` mixin.
95
- if (this._options !== undefined) {
96
- cloned._options = clone(this._options);
97
- }
98
- if (this._queryContext !== undefined) {
99
- cloned._queryContext = clone(this._queryContext);
100
- }
101
- if (this._connection !== undefined) {
102
- cloned._connection = this._connection;
103
- }
104
-
105
- return cloned;
106
- }
107
-
108
- timeout(ms, { cancel } = {}) {
109
- if (isNumber(ms) && ms > 0) {
110
- this._timeout = ms;
111
- if (cancel) {
112
- this.client.assertCanCancelQuery();
113
- this._cancelOnTimeout = true;
114
- }
115
- }
116
- return this;
117
- }
118
-
119
- // With
120
- // ------
121
- isValidStatementArg(statement) {
122
- return (
123
- typeof statement === 'function' ||
124
- statement instanceof Builder ||
125
- (statement && statement.isRawInstance)
126
- );
127
- }
128
-
129
- _validateWithArgs(alias, statementOrColumnList, nothingOrStatement, method) {
130
- const [query, columnList] =
131
- typeof nothingOrStatement === 'undefined'
132
- ? [statementOrColumnList, undefined]
133
- : [nothingOrStatement, statementOrColumnList];
134
- if (typeof alias !== 'string') {
135
- throw new Error(`${method}() first argument must be a string`);
136
- }
137
-
138
- if (this.isValidStatementArg(query) && typeof columnList === 'undefined') {
139
- // Validated as two-arg variant (alias, statement).
140
- return;
141
- }
142
-
143
- // Attempt to interpret as three-arg variant (alias, columnList, statement).
144
- const isNonEmptyNameList =
145
- Array.isArray(columnList) &&
146
- columnList.length > 0 &&
147
- columnList.every((it) => typeof it === 'string');
148
- if (!isNonEmptyNameList) {
149
- throw new Error(
150
- `${method}() second argument must be a statement or non-empty column name list.`
151
- );
152
- }
153
-
154
- if (this.isValidStatementArg(query)) {
155
- return;
156
- }
157
- throw new Error(
158
- `${method}() third argument must be a function / QueryBuilder or a raw when its second argument is a column name list`
159
- );
160
- }
161
-
162
- with(alias, statementOrColumnList, nothingOrStatement) {
163
- this._validateWithArgs(
164
- alias,
165
- statementOrColumnList,
166
- nothingOrStatement,
167
- 'with'
168
- );
169
- return this.withWrapped(alias, statementOrColumnList, nothingOrStatement);
170
- }
171
-
172
- withMaterialized(alias, statementOrColumnList, nothingOrStatement) {
173
- throw new Error('With materialized is not supported by this dialect');
174
- }
175
-
176
- withNotMaterialized(alias, statementOrColumnList, nothingOrStatement) {
177
- throw new Error('With materialized is not supported by this dialect');
178
- }
179
-
180
- // Helper for compiling any advanced `with` queries.
181
- withWrapped(alias, statementOrColumnList, nothingOrStatement, materialized) {
182
- const [query, columnList] =
183
- typeof nothingOrStatement === 'undefined'
184
- ? [statementOrColumnList, undefined]
185
- : [nothingOrStatement, statementOrColumnList];
186
- const statement = {
187
- grouping: 'with',
188
- type: 'withWrapped',
189
- alias: alias,
190
- columnList,
191
- value: query,
192
- };
193
- if (materialized !== undefined) {
194
- statement.materialized = materialized;
195
- }
196
- this._statements.push(statement);
197
- return this;
198
- }
199
-
200
- // With Recursive
201
- // ------
202
-
203
- withRecursive(alias, statementOrColumnList, nothingOrStatement) {
204
- this._validateWithArgs(
205
- alias,
206
- statementOrColumnList,
207
- nothingOrStatement,
208
- 'withRecursive'
209
- );
210
- return this.withRecursiveWrapped(
211
- alias,
212
- statementOrColumnList,
213
- nothingOrStatement
214
- );
215
- }
216
-
217
- // Helper for compiling any advanced `withRecursive` queries.
218
- withRecursiveWrapped(alias, statementOrColumnList, nothingOrStatement) {
219
- this.withWrapped(alias, statementOrColumnList, nothingOrStatement);
220
- this._statements[this._statements.length - 1].recursive = true;
221
- return this;
222
- }
223
-
224
- // Select
225
- // ------
226
-
227
- // Adds a column or columns to the list of "columns"
228
- // being selected on the query.
229
- columns(column) {
230
- if (!column && column !== 0) return this;
231
- this._statements.push({
232
- grouping: 'columns',
233
- value: normalizeArr(...arguments),
234
- });
235
- return this;
236
- }
237
-
238
- // Allow for a sub-select to be explicitly aliased as a column,
239
- // without needing to compile the query in a where.
240
- as(column) {
241
- this._single.as = column;
242
- return this;
243
- }
244
-
245
- // Adds a single hint or an array of hits to the list of "hintComments" on the query.
246
- hintComment(hints) {
247
- hints = Array.isArray(hints) ? hints : [hints];
248
- if (hints.some((hint) => !isString(hint))) {
249
- throw new Error('Hint comment must be a string');
250
- }
251
- if (hints.some((hint) => hint.includes('/*') || hint.includes('*/'))) {
252
- throw new Error('Hint comment cannot include "/*" or "*/"');
253
- }
254
- if (hints.some((hint) => hint.includes('?'))) {
255
- throw new Error('Hint comment cannot include "?"');
256
- }
257
- this._statements.push({
258
- grouping: 'hintComments',
259
- value: hints,
260
- });
261
- return this;
262
- }
263
-
264
- // Prepends the `schemaName` on `tableName` defined by `.table` and `.join`.
265
- withSchema(schemaName) {
266
- this._single.schema = schemaName;
267
- return this;
268
- }
269
-
270
- // Sets the `tableName` on the query.
271
- // Alias to "from" for select and "into" for insert statements
272
- // e.g. builder.insert({a: value}).into('tableName')
273
- // `options`: options object containing keys:
274
- // - `only`: whether the query should use SQL's ONLY to not return
275
- // inheriting table data. Defaults to false.
276
- table(tableName, options = {}) {
277
- this._single.table = tableName;
278
- this._single.only = options.only === true;
279
- return this;
280
- }
281
-
282
- // Adds a `distinct` clause to the query.
283
- distinct(...args) {
284
- this._statements.push({
285
- grouping: 'columns',
286
- value: normalizeArr(...args),
287
- distinct: true,
288
- });
289
- return this;
290
- }
291
-
292
- distinctOn(...args) {
293
- if (isEmpty(args)) {
294
- throw new Error('distinctOn requires at least on argument');
295
- }
296
- this._statements.push({
297
- grouping: 'columns',
298
- value: normalizeArr(...args),
299
- distinctOn: true,
300
- });
301
- return this;
302
- }
303
-
304
- // Adds a join clause to the query, allowing for advanced joins
305
- // with an anonymous function as the second argument.
306
- join(table, first, ...args) {
307
- let join;
308
- const schema =
309
- table instanceof Builder || typeof table === 'function'
310
- ? undefined
311
- : this._single.schema;
312
- const joinType = this._joinType();
313
- if (typeof first === 'function') {
314
- join = new JoinClause(table, joinType, schema);
315
- first.call(join, join);
316
- } else if (joinType === 'raw') {
317
- join = new JoinClause(this.client.raw(table, first), 'raw');
318
- } else {
319
- join = new JoinClause(table, joinType, schema);
320
- if (first) {
321
- join.on(first, ...args);
322
- }
323
- }
324
- this._statements.push(join);
325
- return this;
326
- }
327
-
328
- using(tables) {
329
- throw new Error(
330
- "'using' function is only available in PostgreSQL dialect with Delete statements."
331
- );
332
- }
333
-
334
- // JOIN blocks:
335
- innerJoin(...args) {
336
- return this._joinType('inner').join(...args);
337
- }
338
-
339
- leftJoin(...args) {
340
- return this._joinType('left').join(...args);
341
- }
342
-
343
- leftOuterJoin(...args) {
344
- return this._joinType('left outer').join(...args);
345
- }
346
-
347
- rightJoin(...args) {
348
- return this._joinType('right').join(...args);
349
- }
350
-
351
- rightOuterJoin(...args) {
352
- return this._joinType('right outer').join(...args);
353
- }
354
-
355
- outerJoin(...args) {
356
- return this._joinType('outer').join(...args);
357
- }
358
-
359
- fullOuterJoin(...args) {
360
- return this._joinType('full outer').join(...args);
361
- }
362
-
363
- crossJoin(...args) {
364
- return this._joinType('cross').join(...args);
365
- }
366
-
367
- joinRaw(...args) {
368
- return this._joinType('raw').join(...args);
369
- }
370
-
371
- // Where modifiers:
372
- get or() {
373
- return this._bool('or');
374
- }
375
-
376
- get not() {
377
- return this._not(true);
378
- }
379
-
380
- // The where function can be used in several ways:
381
- // The most basic is `where(key, value)`, which expands to
382
- // where key = value.
383
- where(column, operator, value) {
384
- const argsLength = arguments.length;
385
-
386
- // Support "where true || where false"
387
- if (column === false || column === true) {
388
- return this.where(1, '=', column ? 1 : 0);
389
- }
390
-
391
- // Check if the column is a function, in which case it's
392
- // a where statement wrapped in parens.
393
- if (typeof column === 'function') {
394
- return this.whereWrapped(column);
395
- }
396
-
397
- // Allows `where({id: 2})` syntax.
398
- if (isObject(column) && !column.isRawInstance)
399
- return this._objectWhere(column);
400
-
401
- // Allow a raw statement to be passed along to the query.
402
- if (column && column.isRawInstance && argsLength === 1)
403
- return this.whereRaw(column);
404
-
405
- // Enable the where('key', value) syntax, only when there
406
- // are explicitly two arguments passed, so it's not possible to
407
- // do where('key', '!=') and have that turn into where key != null
408
- if (argsLength === 2) {
409
- value = operator;
410
- operator = '=';
411
-
412
- // If the value is null, and it's a two argument query,
413
- // we assume we're going for a `whereNull`.
414
- if (value === null) {
415
- return this.whereNull(column);
416
- }
417
- }
418
-
419
- // lower case the operator for comparison purposes
420
- const checkOperator = `${operator}`.toLowerCase().trim();
421
-
422
- // If there are 3 arguments, check whether 'in' is one of them.
423
- if (argsLength === 3) {
424
- if (checkOperator === 'in' || checkOperator === 'not in') {
425
- return this._not(checkOperator === 'not in').whereIn(column, value);
426
- }
427
- if (checkOperator === 'between' || checkOperator === 'not between') {
428
- return this._not(checkOperator === 'not between').whereBetween(
429
- column,
430
- value
431
- );
432
- }
433
- }
434
-
435
- // If the value is still null, check whether they're meaning
436
- // where value is null
437
- if (value === null) {
438
- // Check for .where(key, 'is', null) or .where(key, 'is not', 'null');
439
- if (checkOperator === 'is' || checkOperator === 'is not') {
440
- return this._not(checkOperator === 'is not').whereNull(column);
441
- }
442
- }
443
-
444
- // Push onto the where statement stack.
445
- this._statements.push({
446
- grouping: 'where',
447
- type: 'whereBasic',
448
- column,
449
- operator,
450
- value,
451
- not: this._not(),
452
- bool: this._bool(),
453
- asColumn: this._asColumnFlag,
454
- });
455
- return this;
456
- }
457
-
458
- whereColumn(...args) {
459
- this._asColumnFlag = true;
460
- this.where(...args);
461
- this._asColumnFlag = false;
462
- return this;
463
- }
464
-
465
- // Adds an `or where` clause to the query.
466
- orWhere(column, ...args) {
467
- this._bool('or');
468
- const obj = column;
469
- if (isObject(obj) && !obj.isRawInstance) {
470
- return this.whereWrapped(function () {
471
- for (const key in obj) {
472
- this.andWhere(key, obj[key]);
473
- }
474
- });
475
- }
476
- return this.where(column, ...args);
477
- }
478
-
479
- orWhereColumn(column, ...args) {
480
- this._bool('or');
481
- const obj = column;
482
- if (isObject(obj) && !obj.isRawInstance) {
483
- return this.whereWrapped(function () {
484
- for (const key in obj) {
485
- this.andWhereColumn(key, '=', obj[key]);
486
- }
487
- });
488
- }
489
- return this.whereColumn(column, ...args);
490
- }
491
-
492
- // Adds an `not where` clause to the query.
493
- whereNot(column, ...args) {
494
- if (args.length >= 2) {
495
- if (args[0] === 'in' || args[0] === 'between') {
496
- this.client.logger.warn(
497
- 'whereNot is not suitable for "in" and "between" type subqueries. You should use "not in" and "not between" instead.'
498
- );
499
- }
500
- }
501
- return this._not(true).where(column, ...args);
502
- }
503
-
504
- whereNotColumn(...args) {
505
- return this._not(true).whereColumn(...args);
506
- }
507
-
508
- // Adds an `or not where` clause to the query.
509
- orWhereNot(...args) {
510
- return this._bool('or').whereNot(...args);
511
- }
512
-
513
- orWhereNotColumn(...args) {
514
- return this._bool('or').whereNotColumn(...args);
515
- }
516
-
517
- // Processes an object literal provided in a "where" clause.
518
- _objectWhere(obj) {
519
- const boolVal = this._bool();
520
- const notVal = this._not() ? 'Not' : '';
521
- for (const key in obj) {
522
- this[boolVal + 'Where' + notVal](key, obj[key]);
523
- }
524
- return this;
525
- }
526
-
527
- // Adds a raw `where` clause to the query.
528
- whereRaw(sql, bindings) {
529
- const raw = sql.isRawInstance ? sql : this.client.raw(sql, bindings);
530
-
531
- this._statements.push({
532
- grouping: 'where',
533
- type: 'whereRaw',
534
- value: raw,
535
- not: this._not(),
536
- bool: this._bool(),
537
- });
538
- return this;
539
- }
540
-
541
- orWhereRaw(sql, bindings) {
542
- return this._bool('or').whereRaw(sql, bindings);
543
- }
544
-
545
- // Helper for compiling any advanced `where` queries.
546
- whereWrapped(callback) {
547
- this._statements.push({
548
- grouping: 'where',
549
- type: 'whereWrapped',
550
- value: callback,
551
- not: this._not(),
552
- bool: this._bool(),
553
- });
554
- return this;
555
- }
556
-
557
- // Adds a `where exists` clause to the query.
558
- whereExists(callback) {
559
- this._statements.push({
560
- grouping: 'where',
561
- type: 'whereExists',
562
- value: callback,
563
- not: this._not(),
564
- bool: this._bool(),
565
- });
566
- return this;
567
- }
568
-
569
- // Adds an `or where exists` clause to the query.
570
- orWhereExists(callback) {
571
- return this._bool('or').whereExists(callback);
572
- }
573
-
574
- // Adds a `where not exists` clause to the query.
575
- whereNotExists(callback) {
576
- return this._not(true).whereExists(callback);
577
- }
578
-
579
- // Adds a `or where not exists` clause to the query.
580
- orWhereNotExists(callback) {
581
- return this._bool('or').whereNotExists(callback);
582
- }
583
-
584
- // Adds a `where in` clause to the query.
585
- whereIn(column, values) {
586
- if (Array.isArray(values) && isEmpty(values))
587
- return this.where(this._not());
588
- this._statements.push({
589
- grouping: 'where',
590
- type: 'whereIn',
591
- column,
592
- value: values,
593
- not: this._not(),
594
- bool: this._bool(),
595
- });
596
- return this;
597
- }
598
-
599
- // Adds a `or where in` clause to the query.
600
- orWhereIn(column, values) {
601
- return this._bool('or').whereIn(column, values);
602
- }
603
-
604
- // Adds a `where not in` clause to the query.
605
- whereNotIn(column, values) {
606
- return this._not(true).whereIn(column, values);
607
- }
608
-
609
- // Adds a `or where not in` clause to the query.
610
- orWhereNotIn(column, values) {
611
- return this._bool('or')._not(true).whereIn(column, values);
612
- }
613
-
614
- // Adds a `where null` clause to the query.
615
- whereNull(column) {
616
- this._statements.push({
617
- grouping: 'where',
618
- type: 'whereNull',
619
- column,
620
- not: this._not(),
621
- bool: this._bool(),
622
- });
623
- return this;
624
- }
625
-
626
- // Adds a `or where null` clause to the query.
627
- orWhereNull(column) {
628
- return this._bool('or').whereNull(column);
629
- }
630
-
631
- // Adds a `where not null` clause to the query.
632
- whereNotNull(column) {
633
- return this._not(true).whereNull(column);
634
- }
635
-
636
- // Adds a `or where not null` clause to the query.
637
- orWhereNotNull(column) {
638
- return this._bool('or').whereNotNull(column);
639
- }
640
-
641
- // Adds a `where between` clause to the query.
642
- whereBetween(column, values) {
643
- assert(
644
- Array.isArray(values),
645
- 'The second argument to whereBetween must be an array.'
646
- );
647
- assert(
648
- values.length === 2,
649
- 'You must specify 2 values for the whereBetween clause'
650
- );
651
- this._statements.push({
652
- grouping: 'where',
653
- type: 'whereBetween',
654
- column,
655
- value: values,
656
- not: this._not(),
657
- bool: this._bool(),
658
- });
659
- return this;
660
- }
661
-
662
- // Adds a `where not between` clause to the query.
663
- whereNotBetween(column, values) {
664
- return this._not(true).whereBetween(column, values);
665
- }
666
-
667
- // Adds a `or where between` clause to the query.
668
- orWhereBetween(column, values) {
669
- return this._bool('or').whereBetween(column, values);
670
- }
671
-
672
- // Adds a `or where not between` clause to the query.
673
- orWhereNotBetween(column, values) {
674
- return this._bool('or').whereNotBetween(column, values);
675
- }
676
-
677
- _whereLike(type, column, value) {
678
- this._statements.push({
679
- grouping: 'where',
680
- type: type,
681
- column,
682
- value: value,
683
- not: this._not(),
684
- bool: this._bool(),
685
- asColumn: this._asColumnFlag,
686
- });
687
- return this;
688
- }
689
-
690
- // Adds a `where like` clause to the query.
691
- whereLike(column, value) {
692
- return this._whereLike('whereLike', column, value);
693
- }
694
-
695
- // Adds a `or where like` clause to the query.
696
- orWhereLike(column, value) {
697
- return this._bool('or')._whereLike('whereLike', column, value);
698
- }
699
-
700
- // Adds a `where ilike` clause to the query.
701
- whereILike(column, value) {
702
- return this._whereLike('whereILike', column, value);
703
- }
704
-
705
- // Adds a `or where ilike` clause to the query.
706
- orWhereILike(column, value) {
707
- return this._bool('or')._whereLike('whereILike', column, value);
708
- }
709
-
710
- // Adds a `group by` clause to the query.
711
- groupBy(item) {
712
- if (item && item.isRawInstance) {
713
- return this.groupByRaw.apply(this, arguments);
714
- }
715
- this._statements.push({
716
- grouping: 'group',
717
- type: 'groupByBasic',
718
- value: normalizeArr(...arguments),
719
- });
720
- return this;
721
- }
722
-
723
- // Adds a raw `group by` clause to the query.
724
- groupByRaw(sql, bindings) {
725
- const raw = sql.isRawInstance ? sql : this.client.raw(sql, bindings);
726
- this._statements.push({
727
- grouping: 'group',
728
- type: 'groupByRaw',
729
- value: raw,
730
- });
731
- return this;
732
- }
733
-
734
- // Adds a `order by` clause to the query.
735
- orderBy(column, direction, nulls = '') {
736
- if (Array.isArray(column)) {
737
- return this._orderByArray(column);
738
- }
739
- this._statements.push({
740
- grouping: 'order',
741
- type: 'orderByBasic',
742
- value: column,
743
- direction,
744
- nulls,
745
- });
746
- return this;
747
- }
748
-
749
- // Adds a `order by` with multiple columns to the query.
750
- _orderByArray(columnDefs) {
751
- for (let i = 0; i < columnDefs.length; i++) {
752
- const columnInfo = columnDefs[i];
753
- if (isObject(columnInfo)) {
754
- this._statements.push({
755
- grouping: 'order',
756
- type: 'orderByBasic',
757
- value: columnInfo['column'],
758
- direction: columnInfo['order'],
759
- nulls: columnInfo['nulls'],
760
- });
761
- } else if (isString(columnInfo)) {
762
- this._statements.push({
763
- grouping: 'order',
764
- type: 'orderByBasic',
765
- value: columnInfo,
766
- });
767
- }
768
- }
769
- return this;
770
- }
771
-
772
- // Add a raw `order by` clause to the query.
773
- orderByRaw(sql, bindings) {
774
- const raw = sql.isRawInstance ? sql : this.client.raw(sql, bindings);
775
- this._statements.push({
776
- grouping: 'order',
777
- type: 'orderByRaw',
778
- value: raw,
779
- });
780
- return this;
781
- }
782
-
783
- _union(clause, args) {
784
- let callbacks = args[0];
785
- let wrap = args[1];
786
- if (args.length === 1 || (args.length === 2 && isBoolean(wrap))) {
787
- if (!Array.isArray(callbacks)) {
788
- callbacks = [callbacks];
789
- }
790
- for (let i = 0, l = callbacks.length; i < l; i++) {
791
- this._statements.push({
792
- grouping: 'union',
793
- clause: clause,
794
- value: callbacks[i],
795
- wrap: wrap || false,
796
- });
797
- }
798
- } else {
799
- callbacks = toArray(args).slice(0, args.length - 1);
800
- wrap = args[args.length - 1];
801
- if (!isBoolean(wrap)) {
802
- callbacks.push(wrap);
803
- wrap = false;
804
- }
805
- this._union(clause, [callbacks, wrap]);
806
- }
807
- return this;
808
- }
809
-
810
- // Add a union statement to the query.
811
- union(...args) {
812
- return this._union('union', args);
813
- }
814
-
815
- // Adds a union all statement to the query.
816
- unionAll(...args) {
817
- return this._union('union all', args);
818
- }
819
-
820
- // Adds an intersect statement to the query
821
- intersect(callbacks, wrap) {
822
- if (arguments.length === 1 || (arguments.length === 2 && isBoolean(wrap))) {
823
- if (!Array.isArray(callbacks)) {
824
- callbacks = [callbacks];
825
- }
826
- for (let i = 0, l = callbacks.length; i < l; i++) {
827
- this._statements.push({
828
- grouping: 'union',
829
- clause: 'intersect',
830
- value: callbacks[i],
831
- wrap: wrap || false,
832
- });
833
- }
834
- } else {
835
- callbacks = toArray(arguments).slice(0, arguments.length - 1);
836
- wrap = arguments[arguments.length - 1];
837
- if (!isBoolean(wrap)) {
838
- callbacks.push(wrap);
839
- wrap = false;
840
- }
841
- this.intersect(callbacks, wrap);
842
- }
843
- return this;
844
- }
845
-
846
- // Adds a `having` clause to the query.
847
- having(column, operator, value) {
848
- if (column.isRawInstance && arguments.length === 1) {
849
- return this.havingRaw(column);
850
- }
851
-
852
- // Check if the column is a function, in which case it's
853
- // a having statement wrapped in parens.
854
- if (typeof column === 'function') {
855
- return this.havingWrapped(column);
856
- }
857
-
858
- this._statements.push({
859
- grouping: 'having',
860
- type: 'havingBasic',
861
- column,
862
- operator,
863
- value,
864
- bool: this._bool(),
865
- not: this._not(),
866
- });
867
- return this;
868
- }
869
-
870
- orHaving(column, ...args) {
871
- this._bool('or');
872
- const obj = column;
873
- if (isObject(obj) && !obj.isRawInstance) {
874
- return this.havingWrapped(function () {
875
- for (const key in obj) {
876
- this.andHaving(key, obj[key]);
877
- }
878
- });
879
- }
880
- return this.having(column, ...args);
881
- }
882
-
883
- // Helper for compiling any advanced `having` queries.
884
- havingWrapped(callback) {
885
- this._statements.push({
886
- grouping: 'having',
887
- type: 'havingWrapped',
888
- value: callback,
889
- bool: this._bool(),
890
- not: this._not(),
891
- });
892
- return this;
893
- }
894
-
895
- havingNull(column) {
896
- this._statements.push({
897
- grouping: 'having',
898
- type: 'havingNull',
899
- column,
900
- not: this._not(),
901
- bool: this._bool(),
902
- });
903
- return this;
904
- }
905
-
906
- orHavingNull(callback) {
907
- return this._bool('or').havingNull(callback);
908
- }
909
-
910
- havingNotNull(callback) {
911
- return this._not(true).havingNull(callback);
912
- }
913
-
914
- orHavingNotNull(callback) {
915
- return this._not(true)._bool('or').havingNull(callback);
916
- }
917
-
918
- havingExists(callback) {
919
- this._statements.push({
920
- grouping: 'having',
921
- type: 'havingExists',
922
- value: callback,
923
- not: this._not(),
924
- bool: this._bool(),
925
- });
926
- return this;
927
- }
928
-
929
- orHavingExists(callback) {
930
- return this._bool('or').havingExists(callback);
931
- }
932
-
933
- havingNotExists(callback) {
934
- return this._not(true).havingExists(callback);
935
- }
936
-
937
- orHavingNotExists(callback) {
938
- return this._not(true)._bool('or').havingExists(callback);
939
- }
940
-
941
- havingBetween(column, values) {
942
- assert(
943
- Array.isArray(values),
944
- 'The second argument to havingBetween must be an array.'
945
- );
946
- assert(
947
- values.length === 2,
948
- 'You must specify 2 values for the havingBetween clause'
949
- );
950
- this._statements.push({
951
- grouping: 'having',
952
- type: 'havingBetween',
953
- column,
954
- value: values,
955
- not: this._not(),
956
- bool: this._bool(),
957
- });
958
- return this;
959
- }
960
-
961
- orHavingBetween(column, values) {
962
- return this._bool('or').havingBetween(column, values);
963
- }
964
-
965
- havingNotBetween(column, values) {
966
- return this._not(true).havingBetween(column, values);
967
- }
968
-
969
- orHavingNotBetween(column, values) {
970
- return this._not(true)._bool('or').havingBetween(column, values);
971
- }
972
-
973
- havingIn(column, values) {
974
- if (Array.isArray(values) && isEmpty(values))
975
- return this.where(this._not());
976
- this._statements.push({
977
- grouping: 'having',
978
- type: 'havingIn',
979
- column,
980
- value: values,
981
- not: this._not(),
982
- bool: this._bool(),
983
- });
984
- return this;
985
- }
986
-
987
- // Adds a `or where in` clause to the query.
988
- orHavingIn(column, values) {
989
- return this._bool('or').havingIn(column, values);
990
- }
991
-
992
- // Adds a `where not in` clause to the query.
993
- havingNotIn(column, values) {
994
- return this._not(true).havingIn(column, values);
995
- }
996
-
997
- // Adds a `or where not in` clause to the query.
998
- orHavingNotIn(column, values) {
999
- return this._bool('or')._not(true).havingIn(column, values);
1000
- }
1001
-
1002
- // Adds a raw `having` clause to the query.
1003
- havingRaw(sql, bindings) {
1004
- const raw = sql.isRawInstance ? sql : this.client.raw(sql, bindings);
1005
- this._statements.push({
1006
- grouping: 'having',
1007
- type: 'havingRaw',
1008
- value: raw,
1009
- bool: this._bool(),
1010
- not: this._not(),
1011
- });
1012
- return this;
1013
- }
1014
-
1015
- orHavingRaw(sql, bindings) {
1016
- return this._bool('or').havingRaw(sql, bindings);
1017
- }
1018
-
1019
- // set the skip binding parameter (= insert the raw value in the query) for an attribute.
1020
- _setSkipBinding(attribute, options) {
1021
- let skipBinding = options;
1022
- if (isObject(options)) {
1023
- skipBinding = options.skipBinding;
1024
- }
1025
- this._single.skipBinding = this._single.skipBinding || {};
1026
- this._single.skipBinding[attribute] = skipBinding;
1027
- }
1028
-
1029
- // Only allow a single "offset" to be set for the current query.
1030
- offset(value, options) {
1031
- if (value == null || value.isRawInstance || value instanceof Builder) {
1032
- // Builder for backward compatibility
1033
- this._single.offset = value;
1034
- } else {
1035
- const val = parseInt(value, 10);
1036
- if (isNaN(val)) {
1037
- this.client.logger.warn('A valid integer must be provided to offset');
1038
- } else if (val < 0) {
1039
- throw new Error(`A non-negative integer must be provided to offset.`);
1040
- } else {
1041
- this._single.offset = val;
1042
- }
1043
- }
1044
- this._setSkipBinding('offset', options);
1045
- return this;
1046
- }
1047
-
1048
- // Only allow a single "limit" to be set for the current query.
1049
- limit(value, options) {
1050
- const val = parseInt(value, 10);
1051
- if (isNaN(val)) {
1052
- this.client.logger.warn('A valid integer must be provided to limit');
1053
- } else {
1054
- this._single.limit = val;
1055
- this._setSkipBinding('limit', options);
1056
- }
1057
- return this;
1058
- }
1059
-
1060
- // Retrieve the "count" result of the query.
1061
- count(column, options) {
1062
- return this._aggregate('count', column || '*', options);
1063
- }
1064
-
1065
- // Retrieve the minimum value of a given column.
1066
- min(column, options) {
1067
- return this._aggregate('min', column, options);
1068
- }
1069
-
1070
- // Retrieve the maximum value of a given column.
1071
- max(column, options) {
1072
- return this._aggregate('max', column, options);
1073
- }
1074
-
1075
- // Retrieve the sum of the values of a given column.
1076
- sum(column, options) {
1077
- return this._aggregate('sum', column, options);
1078
- }
1079
-
1080
- // Retrieve the average of the values of a given column.
1081
- avg(column, options) {
1082
- return this._aggregate('avg', column, options);
1083
- }
1084
-
1085
- // Retrieve the "count" of the distinct results of the query.
1086
- countDistinct(...columns) {
1087
- let options;
1088
- if (columns.length > 1 && isPlainObject(last(columns))) {
1089
- [options] = columns.splice(columns.length - 1, 1);
1090
- }
1091
-
1092
- if (!columns.length) {
1093
- columns = '*';
1094
- } else if (columns.length === 1) {
1095
- columns = columns[0];
1096
- }
1097
-
1098
- return this._aggregate('count', columns, { ...options, distinct: true });
1099
- }
1100
-
1101
- // Retrieve the sum of the distinct values of a given column.
1102
- sumDistinct(column, options) {
1103
- return this._aggregate('sum', column, { ...options, distinct: true });
1104
- }
1105
-
1106
- // Retrieve the vg of the distinct results of the query.
1107
- avgDistinct(column, options) {
1108
- return this._aggregate('avg', column, { ...options, distinct: true });
1109
- }
1110
-
1111
- // Increments a column's value by the specified amount.
1112
- increment(column, amount = 1) {
1113
- if (isObject(column)) {
1114
- for (const key in column) {
1115
- this._counter(key, column[key]);
1116
- }
1117
-
1118
- return this;
1119
- }
1120
-
1121
- return this._counter(column, amount);
1122
- }
1123
-
1124
- // Decrements a column's value by the specified amount.
1125
- decrement(column, amount = 1) {
1126
- if (isObject(column)) {
1127
- for (const key in column) {
1128
- this._counter(key, -column[key]);
1129
- }
1130
-
1131
- return this;
1132
- }
1133
-
1134
- return this._counter(column, -amount);
1135
- }
1136
-
1137
- // Clears increments/decrements
1138
- clearCounters() {
1139
- this._single.counter = {};
1140
- return this;
1141
- }
1142
-
1143
- // Sets the values for a `select` query, informing that only the first
1144
- // row should be returned (limit 1).
1145
- first(...args) {
1146
- if (this._method && this._method !== 'select') {
1147
- throw new Error(`Cannot chain .first() on "${this._method}" query`);
1148
- }
1149
-
1150
- this.select(normalizeArr(...args));
1151
- this._method = 'first';
1152
- this.limit(1);
1153
- return this;
1154
- }
1155
-
1156
- // Use existing connection to execute the query
1157
- // Same value that client.acquireConnection() for an according client returns should be passed
1158
- connection(_connection) {
1159
- this._connection = _connection;
1160
- this.client.processPassedConnection(_connection);
1161
- return this;
1162
- }
1163
-
1164
- // Pluck a column from a query.
1165
- pluck(column) {
1166
- if (this._method && this._method !== 'select') {
1167
- throw new Error(`Cannot chain .pluck() on "${this._method}" query`);
1168
- }
1169
-
1170
- this._method = 'pluck';
1171
- this._single.pluck = column;
1172
- this._statements.push({
1173
- grouping: 'columns',
1174
- type: 'pluck',
1175
- value: column,
1176
- });
1177
- return this;
1178
- }
1179
-
1180
- // Deprecated. Remove everything from select clause
1181
- clearSelect() {
1182
- this._clearGrouping('columns');
1183
- return this;
1184
- }
1185
-
1186
- // Deprecated. Remove everything from where clause
1187
- clearWhere() {
1188
- this._clearGrouping('where');
1189
- return this;
1190
- }
1191
-
1192
- // Deprecated. Remove everything from group clause
1193
- clearGroup() {
1194
- this._clearGrouping('group');
1195
- return this;
1196
- }
1197
-
1198
- // Deprecated. Remove everything from order clause
1199
- clearOrder() {
1200
- this._clearGrouping('order');
1201
- return this;
1202
- }
1203
-
1204
- // Deprecated. Remove everything from having clause
1205
- clearHaving() {
1206
- this._clearGrouping('having');
1207
- return this;
1208
- }
1209
-
1210
- // Remove everything from statement clause
1211
- clear(statement) {
1212
- if (!CLEARABLE_STATEMENTS.has(statement))
1213
- throw new Error(`Knex Error: unknown statement '${statement}'`);
1214
- if (statement.startsWith('counter')) return this.clearCounters();
1215
- if (statement === 'select') {
1216
- statement = 'columns';
1217
- }
1218
- this._clearGrouping(statement);
1219
- return this;
1220
- }
1221
-
1222
- // Insert & Update
1223
- // ------
1224
-
1225
- // Sets the values for an `insert` query.
1226
- insert(values, returning, options) {
1227
- this._method = 'insert';
1228
- if (!isEmpty(returning)) this.returning(returning, options);
1229
- this._single.insert = values;
1230
- return this;
1231
- }
1232
-
1233
- // Sets the values for an `update`, allowing for both
1234
- // `.update(key, value, [returning])` and `.update(obj, [returning])` syntaxes.
1235
- update(values, returning, options) {
1236
- let ret;
1237
- const obj = this._single.update || {};
1238
- this._method = 'update';
1239
- if (isString(values)) {
1240
- if (isPlainObject(returning)) {
1241
- obj[values] = JSON.stringify(returning);
1242
- } else {
1243
- obj[values] = returning;
1244
- }
1245
- if (arguments.length > 2) {
1246
- ret = arguments[2];
1247
- }
1248
- } else {
1249
- const keys = Object.keys(values);
1250
- if (this._single.update) {
1251
- this.client.logger.warn('Update called multiple times with objects.');
1252
- }
1253
- let i = -1;
1254
- while (++i < keys.length) {
1255
- obj[keys[i]] = values[keys[i]];
1256
- }
1257
- ret = arguments[1];
1258
- }
1259
- if (!isEmpty(ret)) this.returning(ret, options);
1260
- this._single.update = obj;
1261
- return this;
1262
- }
1263
-
1264
- // Sets the returning value for the query.
1265
- returning(returning, options) {
1266
- this._single.returning = returning;
1267
- this._single.options = options;
1268
- return this;
1269
- }
1270
-
1271
- onConflict(columns) {
1272
- if (typeof columns === 'string') {
1273
- columns = [columns];
1274
- }
1275
- return new OnConflictBuilder(this, columns || true);
1276
- }
1277
-
1278
- // Delete
1279
- // ------
1280
-
1281
- // Executes a delete statement on the query;
1282
- delete(ret, options) {
1283
- this._method = 'del';
1284
- if (!isEmpty(ret)) this.returning(ret, options);
1285
- return this;
1286
- }
1287
-
1288
- // Truncates a table, ends the query chain.
1289
- truncate(tableName) {
1290
- this._method = 'truncate';
1291
- if (tableName) {
1292
- this._single.table = tableName;
1293
- }
1294
- return this;
1295
- }
1296
-
1297
- // Retrieves columns for the table specified by `knex(tableName)`
1298
- columnInfo(column) {
1299
- this._method = 'columnInfo';
1300
- this._single.columnInfo = column;
1301
- return this;
1302
- }
1303
-
1304
- // Set a lock for update constraint.
1305
- forUpdate(...tables) {
1306
- this._single.lock = lockMode.forUpdate;
1307
- if (tables.length === 1 && Array.isArray(tables[0])) {
1308
- this._single.lockTables = tables[0];
1309
- } else {
1310
- this._single.lockTables = tables;
1311
- }
1312
- return this;
1313
- }
1314
-
1315
- // Set a lock for share constraint.
1316
- forShare(...tables) {
1317
- this._single.lock = lockMode.forShare;
1318
- this._single.lockTables = tables;
1319
- return this;
1320
- }
1321
-
1322
- // Set a lock for no key update constraint.
1323
- forNoKeyUpdate(...tables) {
1324
- this._single.lock = lockMode.forNoKeyUpdate;
1325
- this._single.lockTables = tables;
1326
- return this;
1327
- }
1328
-
1329
- // Set a lock for key share constraint.
1330
- forKeyShare(...tables) {
1331
- this._single.lock = lockMode.forKeyShare;
1332
- this._single.lockTables = tables;
1333
- return this;
1334
- }
1335
-
1336
- // Skips locked rows when using a lock constraint.
1337
- skipLocked() {
1338
- if (!this._isSelectQuery()) {
1339
- throw new Error(`Cannot chain .skipLocked() on "${this._method}" query!`);
1340
- }
1341
- if (!this._hasLockMode()) {
1342
- throw new Error(
1343
- '.skipLocked() can only be used after a call to .forShare() or .forUpdate()!'
1344
- );
1345
- }
1346
- if (this._single.waitMode === waitMode.noWait) {
1347
- throw new Error('.skipLocked() cannot be used together with .noWait()!');
1348
- }
1349
- this._single.waitMode = waitMode.skipLocked;
1350
- return this;
1351
- }
1352
-
1353
- // Causes error when acessing a locked row instead of waiting for it to be released.
1354
- noWait() {
1355
- if (!this._isSelectQuery()) {
1356
- throw new Error(`Cannot chain .noWait() on "${this._method}" query!`);
1357
- }
1358
- if (!this._hasLockMode()) {
1359
- throw new Error(
1360
- '.noWait() can only be used after a call to .forShare() or .forUpdate()!'
1361
- );
1362
- }
1363
- if (this._single.waitMode === waitMode.skipLocked) {
1364
- throw new Error('.noWait() cannot be used together with .skipLocked()!');
1365
- }
1366
- this._single.waitMode = waitMode.noWait;
1367
- return this;
1368
- }
1369
-
1370
- // Takes a JS object of methods to call and calls them
1371
- fromJS(obj) {
1372
- each(obj, (val, key) => {
1373
- if (typeof this[key] !== 'function') {
1374
- this.client.logger.warn(`Knex Error: unknown key ${key}`);
1375
- }
1376
- if (Array.isArray(val)) {
1377
- this[key].apply(this, val);
1378
- } else {
1379
- this[key](val);
1380
- }
1381
- });
1382
- return this;
1383
- }
1384
-
1385
- fromRaw(sql, bindings) {
1386
- const raw = sql.isRawInstance ? sql : this.client.raw(sql, bindings);
1387
- return this.from(raw);
1388
- }
1389
-
1390
- // Passes query to provided callback function, useful for e.g. composing
1391
- // domain-specific helpers
1392
- modify(callback) {
1393
- callback.apply(this, [this].concat(tail(arguments)));
1394
- return this;
1395
- }
1396
-
1397
- upsert(values, returning, options) {
1398
- throw new Error(
1399
- `Upsert is not yet supported for dialect ${this.client.dialect}`
1400
- );
1401
- }
1402
-
1403
- // JSON support functions
1404
- _json(nameFunction, params) {
1405
- this._statements.push({
1406
- grouping: 'columns',
1407
- type: 'json',
1408
- method: nameFunction,
1409
- params: params,
1410
- });
1411
- return this;
1412
- }
1413
-
1414
- jsonExtract() {
1415
- const column = arguments[0];
1416
- let path;
1417
- let alias;
1418
- let singleValue = true;
1419
-
1420
- // We use arguments to have the signatures :
1421
- // - column (string or array)
1422
- // - column + path
1423
- // - column + path + alias
1424
- // - column + path + alias + singleValue
1425
- // - column array + singleValue
1426
- if (arguments.length >= 2) {
1427
- path = arguments[1];
1428
- }
1429
- if (arguments.length >= 3) {
1430
- alias = arguments[2];
1431
- }
1432
- if (arguments.length === 4) {
1433
- singleValue = arguments[3];
1434
- }
1435
- if (
1436
- arguments.length === 2 &&
1437
- Array.isArray(arguments[0]) &&
1438
- isBoolean(arguments[1])
1439
- ) {
1440
- singleValue = arguments[1];
1441
- }
1442
- return this._json('jsonExtract', {
1443
- column: column,
1444
- path: path,
1445
- alias: alias,
1446
- singleValue, // boolean used only in MSSQL to use function for extract value instead of object/array.
1447
- });
1448
- }
1449
-
1450
- jsonSet(column, path, value, alias) {
1451
- return this._json('jsonSet', {
1452
- column: column,
1453
- path: path,
1454
- value: value,
1455
- alias: alias,
1456
- });
1457
- }
1458
-
1459
- jsonInsert(column, path, value, alias) {
1460
- return this._json('jsonInsert', {
1461
- column: column,
1462
- path: path,
1463
- value: value,
1464
- alias: alias,
1465
- });
1466
- }
1467
-
1468
- jsonRemove(column, path, alias) {
1469
- return this._json('jsonRemove', {
1470
- column: column,
1471
- path: path,
1472
- alias: alias,
1473
- });
1474
- }
1475
-
1476
- // Wheres for JSON
1477
- _isJsonObject(jsonValue) {
1478
- return isObject(jsonValue) && !(jsonValue instanceof Builder);
1479
- }
1480
-
1481
- _whereJsonWrappedValue(type, column, value) {
1482
- const whereJsonClause = {
1483
- grouping: 'where',
1484
- type: type,
1485
- column,
1486
- value: value,
1487
- not: this._not(),
1488
- bool: this._bool(),
1489
- asColumn: this._asColumnFlag,
1490
- };
1491
- if (arguments[3]) {
1492
- whereJsonClause.operator = arguments[3];
1493
- }
1494
- if (arguments[4]) {
1495
- whereJsonClause.jsonPath = arguments[4];
1496
- }
1497
- this._statements.push(whereJsonClause);
1498
- }
1499
-
1500
- whereJsonObject(column, value) {
1501
- this._whereJsonWrappedValue('whereJsonObject', column, value);
1502
- return this;
1503
- }
1504
-
1505
- orWhereJsonObject(column, value) {
1506
- return this._bool('or').whereJsonObject(column, value);
1507
- }
1508
-
1509
- whereNotJsonObject(column, value) {
1510
- return this._not(true).whereJsonObject(column, value);
1511
- }
1512
-
1513
- orWhereNotJsonObject(column, value) {
1514
- return this._bool('or').whereNotJsonObject(column, value);
1515
- }
1516
-
1517
- whereJsonPath(column, path, operator, value) {
1518
- this._whereJsonWrappedValue('whereJsonPath', column, value, operator, path);
1519
- return this;
1520
- }
1521
-
1522
- orWhereJsonPath(column, path, operator, value) {
1523
- return this._bool('or').whereJsonPath(column, path, operator, value);
1524
- }
1525
-
1526
- // Json superset wheres
1527
- whereJsonSupersetOf(column, value) {
1528
- this._whereJsonWrappedValue('whereJsonSupersetOf', column, value);
1529
- return this;
1530
- }
1531
-
1532
- whereJsonNotSupersetOf(column, value) {
1533
- return this._not(true).whereJsonSupersetOf(column, value);
1534
- }
1535
-
1536
- orWhereJsonSupersetOf(column, value) {
1537
- return this._bool('or').whereJsonSupersetOf(column, value);
1538
- }
1539
-
1540
- orWhereJsonNotSupersetOf(column, value) {
1541
- return this._bool('or').whereJsonNotSupersetOf(column, value);
1542
- }
1543
-
1544
- // Json subset wheres
1545
- whereJsonSubsetOf(column, value) {
1546
- this._whereJsonWrappedValue('whereJsonSubsetOf', column, value);
1547
- return this;
1548
- }
1549
-
1550
- whereJsonNotSubsetOf(column, value) {
1551
- return this._not(true).whereJsonSubsetOf(column, value);
1552
- }
1553
-
1554
- orWhereJsonSubsetOf(column, value) {
1555
- return this._bool('or').whereJsonSubsetOf(column, value);
1556
- }
1557
-
1558
- orWhereJsonNotSubsetOf(column, value) {
1559
- return this._bool('or').whereJsonNotSubsetOf(column, value);
1560
- }
1561
-
1562
- whereJsonHasNone(column, values) {
1563
- this._not(true).whereJsonHasAll(column, values);
1564
- return this;
1565
- }
1566
-
1567
- // end of wheres for JSON
1568
-
1569
- _analytic(alias, second, third) {
1570
- let analytic;
1571
- const { schema } = this._single;
1572
- const method = this._analyticMethod();
1573
- alias = typeof alias === 'string' ? alias : null;
1574
-
1575
- assert(
1576
- typeof second === 'function' ||
1577
- second.isRawInstance ||
1578
- Array.isArray(second) ||
1579
- typeof second === 'string' ||
1580
- typeof second === 'object',
1581
- `The second argument to an analytic function must be either a function, a raw,
1582
- an array of string or object, an object or a single string.`
1583
- );
1584
-
1585
- if (third) {
1586
- assert(
1587
- Array.isArray(third) ||
1588
- typeof third === 'string' ||
1589
- typeof third === 'object',
1590
- 'The third argument to an analytic function must be either a string, an array of string or object or an object.'
1591
- );
1592
- }
1593
-
1594
- if (isFunction(second)) {
1595
- analytic = new Analytic(method, schema, alias);
1596
- second.call(analytic, analytic);
1597
- } else if (second.isRawInstance) {
1598
- const raw = second;
1599
- analytic = {
1600
- grouping: 'columns',
1601
- type: 'analytic',
1602
- method: method,
1603
- raw: raw,
1604
- alias: alias,
1605
- };
1606
- } else {
1607
- const order = !Array.isArray(second) ? [second] : second;
1608
- let partitions = third || [];
1609
- partitions = !Array.isArray(partitions) ? [partitions] : partitions;
1610
- analytic = {
1611
- grouping: 'columns',
1612
- type: 'analytic',
1613
- method: method,
1614
- order: order,
1615
- alias: alias,
1616
- partitions: partitions,
1617
- };
1618
- }
1619
- this._statements.push(analytic);
1620
- return this;
1621
- }
1622
-
1623
- rank(...args) {
1624
- return this._analyticMethod('rank')._analytic(...args);
1625
- }
1626
-
1627
- denseRank(...args) {
1628
- return this._analyticMethod('dense_rank')._analytic(...args);
1629
- }
1630
-
1631
- rowNumber(...args) {
1632
- return this._analyticMethod('row_number')._analytic(...args);
1633
- }
1634
-
1635
- // ----------------------------------------------------------------------
1636
-
1637
- // Helper for the incrementing/decrementing queries.
1638
- _counter(column, amount) {
1639
- amount = parseFloat(amount);
1640
-
1641
- this._method = 'update';
1642
-
1643
- this._single.counter = this._single.counter || {};
1644
-
1645
- this._single.counter[column] = amount;
1646
-
1647
- return this;
1648
- }
1649
-
1650
- // Helper to get or set the "boolFlag" value.
1651
- _bool(val) {
1652
- if (arguments.length === 1) {
1653
- this._boolFlag = val;
1654
- return this;
1655
- }
1656
- const ret = this._boolFlag;
1657
- this._boolFlag = 'and';
1658
- return ret;
1659
- }
1660
-
1661
- // Helper to get or set the "notFlag" value.
1662
- _not(val) {
1663
- if (arguments.length === 1) {
1664
- this._notFlag = val;
1665
- return this;
1666
- }
1667
- const ret = this._notFlag;
1668
- this._notFlag = false;
1669
- return ret;
1670
- }
1671
-
1672
- // Helper to get or set the "joinFlag" value.
1673
- _joinType(val) {
1674
- if (arguments.length === 1) {
1675
- this._joinFlag = val;
1676
- return this;
1677
- }
1678
- const ret = this._joinFlag || 'inner';
1679
- this._joinFlag = 'inner';
1680
- return ret;
1681
- }
1682
-
1683
- _analyticMethod(val) {
1684
- if (arguments.length === 1) {
1685
- this._analyticFlag = val;
1686
- return this;
1687
- }
1688
- return this._analyticFlag || 'row_number';
1689
- }
1690
-
1691
- // Helper for compiling any aggregate queries.
1692
- _aggregate(method, column, options = {}) {
1693
- this._statements.push({
1694
- grouping: 'columns',
1695
- type: column.isRawInstance ? 'aggregateRaw' : 'aggregate',
1696
- method,
1697
- value: column,
1698
- aggregateDistinct: options.distinct || false,
1699
- alias: options.as,
1700
- });
1701
- return this;
1702
- }
1703
-
1704
- // Helper function for clearing or reseting a grouping type from the builder
1705
- _clearGrouping(grouping) {
1706
- if (grouping in this._single) {
1707
- this._single[grouping] = undefined;
1708
- } else {
1709
- this._statements = reject(this._statements, { grouping });
1710
- }
1711
- }
1712
-
1713
- // Helper function that checks if the builder will emit a select query
1714
- _isSelectQuery() {
1715
- return SELECT_COMMANDS.has(this._method);
1716
- }
1717
-
1718
- // Helper function that checks if the query has a lock mode set
1719
- _hasLockMode() {
1720
- return LOCK_MODES.has(this._single.lock);
1721
- }
1722
- }
1723
-
1724
- Builder.prototype.select = Builder.prototype.columns;
1725
- Builder.prototype.column = Builder.prototype.columns;
1726
- Builder.prototype.andWhereNot = Builder.prototype.whereNot;
1727
- Builder.prototype.andWhereNotColumn = Builder.prototype.whereNotColumn;
1728
- Builder.prototype.andWhere = Builder.prototype.where;
1729
- Builder.prototype.andWhereColumn = Builder.prototype.whereColumn;
1730
- Builder.prototype.andWhereRaw = Builder.prototype.whereRaw;
1731
- Builder.prototype.andWhereBetween = Builder.prototype.whereBetween;
1732
- Builder.prototype.andWhereNotBetween = Builder.prototype.whereNotBetween;
1733
- Builder.prototype.andWhereJsonObject = Builder.prototype.whereJsonObject;
1734
- Builder.prototype.andWhereNotJsonObject = Builder.prototype.whereJsonObject;
1735
- Builder.prototype.andWhereJsonPath = Builder.prototype.whereJsonPath;
1736
- Builder.prototype.andWhereLike = Builder.prototype.whereLike;
1737
- Builder.prototype.andWhereILike = Builder.prototype.whereILike;
1738
- Builder.prototype.andHaving = Builder.prototype.having;
1739
- Builder.prototype.andHavingIn = Builder.prototype.havingIn;
1740
- Builder.prototype.andHavingNotIn = Builder.prototype.havingNotIn;
1741
- Builder.prototype.andHavingNull = Builder.prototype.havingNull;
1742
- Builder.prototype.andHavingNotNull = Builder.prototype.havingNotNull;
1743
- Builder.prototype.andHavingExists = Builder.prototype.havingExists;
1744
- Builder.prototype.andHavingNotExists = Builder.prototype.havingNotExists;
1745
- Builder.prototype.andHavingBetween = Builder.prototype.havingBetween;
1746
- Builder.prototype.andHavingNotBetween = Builder.prototype.havingNotBetween;
1747
- Builder.prototype.from = Builder.prototype.table;
1748
- Builder.prototype.into = Builder.prototype.table;
1749
- Builder.prototype.del = Builder.prototype.delete;
1750
-
1751
- // Attach all of the top level promise methods that should be chainable.
1752
- augmentWithBuilderInterface(Builder);
1753
- addQueryContext(Builder);
1754
-
1755
- Builder.extend = (methodName, fn) => {
1756
- if (Object.prototype.hasOwnProperty.call(Builder.prototype, methodName)) {
1757
- throw new Error(
1758
- `Can't extend QueryBuilder with existing method ('${methodName}').`
1759
- );
1760
- }
1761
-
1762
- assign(Builder.prototype, { [methodName]: fn });
1763
- };
1764
-
1765
- // Sub-builder for onConflict clauses
1766
- class OnConflictBuilder {
1767
- constructor(builder, columns) {
1768
- this.builder = builder;
1769
- this._columns = columns;
1770
- }
1771
-
1772
- // Sets insert query to ignore conflicts
1773
- ignore() {
1774
- this.builder._single.onConflict = this._columns;
1775
- this.builder._single.ignore = true;
1776
- return this.builder;
1777
- }
1778
-
1779
- // Sets insert query to update on conflict
1780
- merge(updates) {
1781
- this.builder._single.onConflict = this._columns;
1782
- this.builder._single.merge = { updates };
1783
- return this.builder;
1784
- }
1785
-
1786
- // Prevent
1787
- then() {
1788
- throw new Error(
1789
- 'Incomplete onConflict clause. .onConflict() must be directly followed by either .merge() or .ignore()'
1790
- );
1791
- }
1792
- }
1793
-
1794
- module.exports = Builder;
1
+ // Builder
2
+ // -------
3
+ const assert = require('assert');
4
+ const { EventEmitter } = require('events');
5
+ const assign = require('lodash/assign');
6
+ const clone = require('lodash/clone');
7
+ const each = require('lodash/each');
8
+ const isEmpty = require('lodash/isEmpty');
9
+ const isPlainObject = require('lodash/isPlainObject');
10
+ const last = require('lodash/last');
11
+ const reject = require('lodash/reject');
12
+ const tail = require('lodash/tail');
13
+ const toArray = require('lodash/toArray');
14
+
15
+ const { addQueryContext, normalizeArr } = require('../util/helpers');
16
+ const JoinClause = require('./joinclause');
17
+ const Analytic = require('./analytic');
18
+ const saveAsyncStack = require('../util/save-async-stack');
19
+ const {
20
+ isBoolean,
21
+ isNumber,
22
+ isObject,
23
+ isString,
24
+ isFunction,
25
+ } = require('../util/is');
26
+
27
+ const { lockMode, waitMode } = require('./constants');
28
+ const {
29
+ augmentWithBuilderInterface,
30
+ } = require('../builder-interface-augmenter');
31
+
32
+ const SELECT_COMMANDS = new Set(['pluck', 'first', 'select']);
33
+ const CLEARABLE_STATEMENTS = new Set([
34
+ 'with',
35
+ 'select',
36
+ 'columns',
37
+ 'hintComments',
38
+ 'where',
39
+ 'union',
40
+ 'join',
41
+ 'group',
42
+ 'order',
43
+ 'having',
44
+ 'limit',
45
+ 'offset',
46
+ 'counter',
47
+ 'counters',
48
+ ]);
49
+ const LOCK_MODES = new Set([
50
+ lockMode.forShare,
51
+ lockMode.forUpdate,
52
+ lockMode.forNoKeyUpdate,
53
+ lockMode.forKeyShare,
54
+ ]);
55
+
56
+ // Typically called from `knex.builder`,
57
+ // start a new query building chain.
58
+ class Builder extends EventEmitter {
59
+ constructor(client) {
60
+ super();
61
+ this.client = client;
62
+ this.and = this;
63
+ this._single = {};
64
+ this._statements = [];
65
+ this._method = 'select';
66
+ if (client.config) {
67
+ saveAsyncStack(this, 5);
68
+ this._debug = client.config.debug;
69
+ }
70
+ // Internal flags used in the builder.
71
+ this._joinFlag = 'inner';
72
+ this._boolFlag = 'and';
73
+ this._notFlag = false;
74
+ this._asColumnFlag = false;
75
+ }
76
+
77
+ toString() {
78
+ return this.toQuery();
79
+ }
80
+
81
+ // Convert the current query "toSQL"
82
+ toSQL(method, tz) {
83
+ return this.client.queryCompiler(this).toSQL(method || this._method, tz);
84
+ }
85
+
86
+ // Create a shallow clone of the current query builder.
87
+ clone() {
88
+ const cloned = new this.constructor(this.client);
89
+ cloned._method = this._method;
90
+ cloned._single = clone(this._single);
91
+ cloned._statements = clone(this._statements);
92
+ cloned._debug = this._debug;
93
+
94
+ // `_option` is assigned by the `Interface` mixin.
95
+ if (this._options !== undefined) {
96
+ cloned._options = clone(this._options);
97
+ }
98
+ if (this._queryContext !== undefined) {
99
+ cloned._queryContext = clone(this._queryContext);
100
+ }
101
+ if (this._connection !== undefined) {
102
+ cloned._connection = this._connection;
103
+ }
104
+
105
+ return cloned;
106
+ }
107
+
108
+ timeout(ms, { cancel } = {}) {
109
+ if (isNumber(ms) && ms > 0) {
110
+ this._timeout = ms;
111
+ if (cancel) {
112
+ this.client.assertCanCancelQuery();
113
+ this._cancelOnTimeout = true;
114
+ }
115
+ }
116
+ return this;
117
+ }
118
+
119
+ // With
120
+ // ------
121
+ isValidStatementArg(statement) {
122
+ return (
123
+ typeof statement === 'function' ||
124
+ statement instanceof Builder ||
125
+ (statement && statement.isRawInstance)
126
+ );
127
+ }
128
+
129
+ _validateWithArgs(alias, statementOrColumnList, nothingOrStatement, method) {
130
+ const [query, columnList] =
131
+ typeof nothingOrStatement === 'undefined'
132
+ ? [statementOrColumnList, undefined]
133
+ : [nothingOrStatement, statementOrColumnList];
134
+ if (typeof alias !== 'string') {
135
+ throw new Error(`${method}() first argument must be a string`);
136
+ }
137
+
138
+ if (this.isValidStatementArg(query) && typeof columnList === 'undefined') {
139
+ // Validated as two-arg variant (alias, statement).
140
+ return;
141
+ }
142
+
143
+ // Attempt to interpret as three-arg variant (alias, columnList, statement).
144
+ const isNonEmptyNameList =
145
+ Array.isArray(columnList) &&
146
+ columnList.length > 0 &&
147
+ columnList.every((it) => typeof it === 'string');
148
+ if (!isNonEmptyNameList) {
149
+ throw new Error(
150
+ `${method}() second argument must be a statement or non-empty column name list.`
151
+ );
152
+ }
153
+
154
+ if (this.isValidStatementArg(query)) {
155
+ return;
156
+ }
157
+ throw new Error(
158
+ `${method}() third argument must be a function / QueryBuilder or a raw when its second argument is a column name list`
159
+ );
160
+ }
161
+
162
+ with(alias, statementOrColumnList, nothingOrStatement) {
163
+ this._validateWithArgs(
164
+ alias,
165
+ statementOrColumnList,
166
+ nothingOrStatement,
167
+ 'with'
168
+ );
169
+ return this.withWrapped(alias, statementOrColumnList, nothingOrStatement);
170
+ }
171
+
172
+ withMaterialized(alias, statementOrColumnList, nothingOrStatement) {
173
+ throw new Error('With materialized is not supported by this dialect');
174
+ }
175
+
176
+ withNotMaterialized(alias, statementOrColumnList, nothingOrStatement) {
177
+ throw new Error('With materialized is not supported by this dialect');
178
+ }
179
+
180
+ // Helper for compiling any advanced `with` queries.
181
+ withWrapped(alias, statementOrColumnList, nothingOrStatement, materialized) {
182
+ const [query, columnList] =
183
+ typeof nothingOrStatement === 'undefined'
184
+ ? [statementOrColumnList, undefined]
185
+ : [nothingOrStatement, statementOrColumnList];
186
+ const statement = {
187
+ grouping: 'with',
188
+ type: 'withWrapped',
189
+ alias: alias,
190
+ columnList,
191
+ value: query,
192
+ };
193
+ if (materialized !== undefined) {
194
+ statement.materialized = materialized;
195
+ }
196
+ this._statements.push(statement);
197
+ return this;
198
+ }
199
+
200
+ // With Recursive
201
+ // ------
202
+
203
+ withRecursive(alias, statementOrColumnList, nothingOrStatement) {
204
+ this._validateWithArgs(
205
+ alias,
206
+ statementOrColumnList,
207
+ nothingOrStatement,
208
+ 'withRecursive'
209
+ );
210
+ return this.withRecursiveWrapped(
211
+ alias,
212
+ statementOrColumnList,
213
+ nothingOrStatement
214
+ );
215
+ }
216
+
217
+ // Helper for compiling any advanced `withRecursive` queries.
218
+ withRecursiveWrapped(alias, statementOrColumnList, nothingOrStatement) {
219
+ this.withWrapped(alias, statementOrColumnList, nothingOrStatement);
220
+ this._statements[this._statements.length - 1].recursive = true;
221
+ return this;
222
+ }
223
+
224
+ // Select
225
+ // ------
226
+
227
+ // Adds a column or columns to the list of "columns"
228
+ // being selected on the query.
229
+ columns(column) {
230
+ if (!column && column !== 0) return this;
231
+ this._statements.push({
232
+ grouping: 'columns',
233
+ value: normalizeArr(...arguments),
234
+ });
235
+ return this;
236
+ }
237
+
238
+ // Allow for a sub-select to be explicitly aliased as a column,
239
+ // without needing to compile the query in a where.
240
+ as(column) {
241
+ this._single.as = column;
242
+ return this;
243
+ }
244
+
245
+ // Adds a single hint or an array of hits to the list of "hintComments" on the query.
246
+ hintComment(hints) {
247
+ hints = Array.isArray(hints) ? hints : [hints];
248
+ if (hints.some((hint) => !isString(hint))) {
249
+ throw new Error('Hint comment must be a string');
250
+ }
251
+ if (hints.some((hint) => hint.includes('/*') || hint.includes('*/'))) {
252
+ throw new Error('Hint comment cannot include "/*" or "*/"');
253
+ }
254
+ if (hints.some((hint) => hint.includes('?'))) {
255
+ throw new Error('Hint comment cannot include "?"');
256
+ }
257
+ this._statements.push({
258
+ grouping: 'hintComments',
259
+ value: hints,
260
+ });
261
+ return this;
262
+ }
263
+
264
+ // Prepends the `schemaName` on `tableName` defined by `.table` and `.join`.
265
+ withSchema(schemaName) {
266
+ this._single.schema = schemaName;
267
+ return this;
268
+ }
269
+
270
+ // Sets the `tableName` on the query.
271
+ // Alias to "from" for select and "into" for insert statements
272
+ // e.g. builder.insert({a: value}).into('tableName')
273
+ // `options`: options object containing keys:
274
+ // - `only`: whether the query should use SQL's ONLY to not return
275
+ // inheriting table data. Defaults to false.
276
+ table(tableName, options = {}) {
277
+ this._single.table = tableName;
278
+ this._single.only = options.only === true;
279
+ return this;
280
+ }
281
+
282
+ // Adds a `distinct` clause to the query.
283
+ distinct(...args) {
284
+ this._statements.push({
285
+ grouping: 'columns',
286
+ value: normalizeArr(...args),
287
+ distinct: true,
288
+ });
289
+ return this;
290
+ }
291
+
292
+ distinctOn(...args) {
293
+ if (isEmpty(args)) {
294
+ throw new Error('distinctOn requires at least on argument');
295
+ }
296
+ this._statements.push({
297
+ grouping: 'columns',
298
+ value: normalizeArr(...args),
299
+ distinctOn: true,
300
+ });
301
+ return this;
302
+ }
303
+
304
+ // Adds a join clause to the query, allowing for advanced joins
305
+ // with an anonymous function as the second argument.
306
+ join(table, first, ...args) {
307
+ let join;
308
+ const schema =
309
+ table instanceof Builder || typeof table === 'function'
310
+ ? undefined
311
+ : this._single.schema;
312
+ const joinType = this._joinType();
313
+ if (typeof first === 'function') {
314
+ join = new JoinClause(table, joinType, schema);
315
+ first.call(join, join);
316
+ } else if (joinType === 'raw') {
317
+ join = new JoinClause(this.client.raw(table, first), 'raw');
318
+ } else {
319
+ join = new JoinClause(table, joinType, schema);
320
+ if (first) {
321
+ join.on(first, ...args);
322
+ }
323
+ }
324
+ this._statements.push(join);
325
+ return this;
326
+ }
327
+
328
+ using(tables) {
329
+ throw new Error(
330
+ "'using' function is only available in PostgreSQL dialect with Delete statements."
331
+ );
332
+ }
333
+
334
+ // JOIN blocks:
335
+ innerJoin(...args) {
336
+ return this._joinType('inner').join(...args);
337
+ }
338
+
339
+ leftJoin(...args) {
340
+ return this._joinType('left').join(...args);
341
+ }
342
+
343
+ leftOuterJoin(...args) {
344
+ return this._joinType('left outer').join(...args);
345
+ }
346
+
347
+ rightJoin(...args) {
348
+ return this._joinType('right').join(...args);
349
+ }
350
+
351
+ rightOuterJoin(...args) {
352
+ return this._joinType('right outer').join(...args);
353
+ }
354
+
355
+ outerJoin(...args) {
356
+ return this._joinType('outer').join(...args);
357
+ }
358
+
359
+ fullOuterJoin(...args) {
360
+ return this._joinType('full outer').join(...args);
361
+ }
362
+
363
+ crossJoin(...args) {
364
+ return this._joinType('cross').join(...args);
365
+ }
366
+
367
+ joinRaw(...args) {
368
+ return this._joinType('raw').join(...args);
369
+ }
370
+
371
+ // Where modifiers:
372
+ get or() {
373
+ return this._bool('or');
374
+ }
375
+
376
+ get not() {
377
+ return this._not(true);
378
+ }
379
+
380
+ // The where function can be used in several ways:
381
+ // The most basic is `where(key, value)`, which expands to
382
+ // where key = value.
383
+ where(column, operator, value) {
384
+ const argsLength = arguments.length;
385
+
386
+ // Support "where true || where false"
387
+ if (column === false || column === true) {
388
+ return this.where(1, '=', column ? 1 : 0);
389
+ }
390
+
391
+ // Check if the column is a function, in which case it's
392
+ // a where statement wrapped in parens.
393
+ if (typeof column === 'function') {
394
+ return this.whereWrapped(column);
395
+ }
396
+
397
+ // Allows `where({id: 2})` syntax.
398
+ if (isObject(column) && !column.isRawInstance)
399
+ return this._objectWhere(column);
400
+
401
+ // Allow a raw statement to be passed along to the query.
402
+ if (column && column.isRawInstance && argsLength === 1)
403
+ return this.whereRaw(column);
404
+
405
+ // Enable the where('key', value) syntax, only when there
406
+ // are explicitly two arguments passed, so it's not possible to
407
+ // do where('key', '!=') and have that turn into where key != null
408
+ if (argsLength === 2) {
409
+ value = operator;
410
+ operator = '=';
411
+
412
+ // If the value is null, and it's a two argument query,
413
+ // we assume we're going for a `whereNull`.
414
+ if (value === null) {
415
+ return this.whereNull(column);
416
+ }
417
+ }
418
+
419
+ // lower case the operator for comparison purposes
420
+ const checkOperator = `${operator}`.toLowerCase().trim();
421
+
422
+ // If there are 3 arguments, check whether 'in' is one of them.
423
+ if (argsLength === 3) {
424
+ if (checkOperator === 'in' || checkOperator === 'not in') {
425
+ return this._not(checkOperator === 'not in').whereIn(column, value);
426
+ }
427
+ if (checkOperator === 'between' || checkOperator === 'not between') {
428
+ return this._not(checkOperator === 'not between').whereBetween(
429
+ column,
430
+ value
431
+ );
432
+ }
433
+ }
434
+
435
+ // If the value is still null, check whether they're meaning
436
+ // where value is null
437
+ if (value === null) {
438
+ // Check for .where(key, 'is', null) or .where(key, 'is not', 'null');
439
+ if (checkOperator === 'is' || checkOperator === 'is not') {
440
+ return this._not(checkOperator === 'is not').whereNull(column);
441
+ }
442
+ }
443
+
444
+ // Push onto the where statement stack.
445
+ this._statements.push({
446
+ grouping: 'where',
447
+ type: 'whereBasic',
448
+ column,
449
+ operator,
450
+ value,
451
+ not: this._not(),
452
+ bool: this._bool(),
453
+ asColumn: this._asColumnFlag,
454
+ });
455
+ return this;
456
+ }
457
+
458
+ whereColumn(...args) {
459
+ this._asColumnFlag = true;
460
+ this.where(...args);
461
+ this._asColumnFlag = false;
462
+ return this;
463
+ }
464
+
465
+ // Adds an `or where` clause to the query.
466
+ orWhere(column, ...args) {
467
+ this._bool('or');
468
+ const obj = column;
469
+ if (isObject(obj) && !obj.isRawInstance) {
470
+ return this.whereWrapped(function () {
471
+ for (const key in obj) {
472
+ this.andWhere(key, obj[key]);
473
+ }
474
+ });
475
+ }
476
+ return this.where(column, ...args);
477
+ }
478
+
479
+ orWhereColumn(column, ...args) {
480
+ this._bool('or');
481
+ const obj = column;
482
+ if (isObject(obj) && !obj.isRawInstance) {
483
+ return this.whereWrapped(function () {
484
+ for (const key in obj) {
485
+ this.andWhereColumn(key, '=', obj[key]);
486
+ }
487
+ });
488
+ }
489
+ return this.whereColumn(column, ...args);
490
+ }
491
+
492
+ // Adds an `not where` clause to the query.
493
+ whereNot(column, ...args) {
494
+ if (args.length >= 2) {
495
+ if (args[0] === 'in' || args[0] === 'between') {
496
+ this.client.logger.warn(
497
+ 'whereNot is not suitable for "in" and "between" type subqueries. You should use "not in" and "not between" instead.'
498
+ );
499
+ }
500
+ }
501
+ return this._not(true).where(column, ...args);
502
+ }
503
+
504
+ whereNotColumn(...args) {
505
+ return this._not(true).whereColumn(...args);
506
+ }
507
+
508
+ // Adds an `or not where` clause to the query.
509
+ orWhereNot(...args) {
510
+ return this._bool('or').whereNot(...args);
511
+ }
512
+
513
+ orWhereNotColumn(...args) {
514
+ return this._bool('or').whereNotColumn(...args);
515
+ }
516
+
517
+ // Processes an object literal provided in a "where" clause.
518
+ _objectWhere(obj) {
519
+ const boolVal = this._bool();
520
+ const notVal = this._not() ? 'Not' : '';
521
+ for (const key in obj) {
522
+ this[boolVal + 'Where' + notVal](key, obj[key]);
523
+ }
524
+ return this;
525
+ }
526
+
527
+ // Adds a raw `where` clause to the query.
528
+ whereRaw(sql, bindings) {
529
+ const raw = sql.isRawInstance ? sql : this.client.raw(sql, bindings);
530
+
531
+ this._statements.push({
532
+ grouping: 'where',
533
+ type: 'whereRaw',
534
+ value: raw,
535
+ not: this._not(),
536
+ bool: this._bool(),
537
+ });
538
+ return this;
539
+ }
540
+
541
+ orWhereRaw(sql, bindings) {
542
+ return this._bool('or').whereRaw(sql, bindings);
543
+ }
544
+
545
+ // Helper for compiling any advanced `where` queries.
546
+ whereWrapped(callback) {
547
+ this._statements.push({
548
+ grouping: 'where',
549
+ type: 'whereWrapped',
550
+ value: callback,
551
+ not: this._not(),
552
+ bool: this._bool(),
553
+ });
554
+ return this;
555
+ }
556
+
557
+ // Adds a `where exists` clause to the query.
558
+ whereExists(callback) {
559
+ this._statements.push({
560
+ grouping: 'where',
561
+ type: 'whereExists',
562
+ value: callback,
563
+ not: this._not(),
564
+ bool: this._bool(),
565
+ });
566
+ return this;
567
+ }
568
+
569
+ // Adds an `or where exists` clause to the query.
570
+ orWhereExists(callback) {
571
+ return this._bool('or').whereExists(callback);
572
+ }
573
+
574
+ // Adds a `where not exists` clause to the query.
575
+ whereNotExists(callback) {
576
+ return this._not(true).whereExists(callback);
577
+ }
578
+
579
+ // Adds a `or where not exists` clause to the query.
580
+ orWhereNotExists(callback) {
581
+ return this._bool('or').whereNotExists(callback);
582
+ }
583
+
584
+ // Adds a `where in` clause to the query.
585
+ whereIn(column, values) {
586
+ if (Array.isArray(values) && isEmpty(values))
587
+ return this.where(this._not());
588
+ this._statements.push({
589
+ grouping: 'where',
590
+ type: 'whereIn',
591
+ column,
592
+ value: values,
593
+ not: this._not(),
594
+ bool: this._bool(),
595
+ });
596
+ return this;
597
+ }
598
+
599
+ // Adds a `or where in` clause to the query.
600
+ orWhereIn(column, values) {
601
+ return this._bool('or').whereIn(column, values);
602
+ }
603
+
604
+ // Adds a `where not in` clause to the query.
605
+ whereNotIn(column, values) {
606
+ return this._not(true).whereIn(column, values);
607
+ }
608
+
609
+ // Adds a `or where not in` clause to the query.
610
+ orWhereNotIn(column, values) {
611
+ return this._bool('or')._not(true).whereIn(column, values);
612
+ }
613
+
614
+ // Adds a `where null` clause to the query.
615
+ whereNull(column) {
616
+ this._statements.push({
617
+ grouping: 'where',
618
+ type: 'whereNull',
619
+ column,
620
+ not: this._not(),
621
+ bool: this._bool(),
622
+ });
623
+ return this;
624
+ }
625
+
626
+ // Adds a `or where null` clause to the query.
627
+ orWhereNull(column) {
628
+ return this._bool('or').whereNull(column);
629
+ }
630
+
631
+ // Adds a `where not null` clause to the query.
632
+ whereNotNull(column) {
633
+ return this._not(true).whereNull(column);
634
+ }
635
+
636
+ // Adds a `or where not null` clause to the query.
637
+ orWhereNotNull(column) {
638
+ return this._bool('or').whereNotNull(column);
639
+ }
640
+
641
+ // Adds a `where between` clause to the query.
642
+ whereBetween(column, values) {
643
+ assert(
644
+ Array.isArray(values),
645
+ 'The second argument to whereBetween must be an array.'
646
+ );
647
+ assert(
648
+ values.length === 2,
649
+ 'You must specify 2 values for the whereBetween clause'
650
+ );
651
+ this._statements.push({
652
+ grouping: 'where',
653
+ type: 'whereBetween',
654
+ column,
655
+ value: values,
656
+ not: this._not(),
657
+ bool: this._bool(),
658
+ });
659
+ return this;
660
+ }
661
+
662
+ // Adds a `where not between` clause to the query.
663
+ whereNotBetween(column, values) {
664
+ return this._not(true).whereBetween(column, values);
665
+ }
666
+
667
+ // Adds a `or where between` clause to the query.
668
+ orWhereBetween(column, values) {
669
+ return this._bool('or').whereBetween(column, values);
670
+ }
671
+
672
+ // Adds a `or where not between` clause to the query.
673
+ orWhereNotBetween(column, values) {
674
+ return this._bool('or').whereNotBetween(column, values);
675
+ }
676
+
677
+ _whereLike(type, column, value) {
678
+ this._statements.push({
679
+ grouping: 'where',
680
+ type: type,
681
+ column,
682
+ value: value,
683
+ not: this._not(),
684
+ bool: this._bool(),
685
+ asColumn: this._asColumnFlag,
686
+ });
687
+ return this;
688
+ }
689
+
690
+ // Adds a `where like` clause to the query.
691
+ whereLike(column, value) {
692
+ return this._whereLike('whereLike', column, value);
693
+ }
694
+
695
+ // Adds a `or where like` clause to the query.
696
+ orWhereLike(column, value) {
697
+ return this._bool('or')._whereLike('whereLike', column, value);
698
+ }
699
+
700
+ // Adds a `where ilike` clause to the query.
701
+ whereILike(column, value) {
702
+ return this._whereLike('whereILike', column, value);
703
+ }
704
+
705
+ // Adds a `or where ilike` clause to the query.
706
+ orWhereILike(column, value) {
707
+ return this._bool('or')._whereLike('whereILike', column, value);
708
+ }
709
+
710
+ // Adds a `group by` clause to the query.
711
+ groupBy(item) {
712
+ if (item && item.isRawInstance) {
713
+ return this.groupByRaw.apply(this, arguments);
714
+ }
715
+ this._statements.push({
716
+ grouping: 'group',
717
+ type: 'groupByBasic',
718
+ value: normalizeArr(...arguments),
719
+ });
720
+ return this;
721
+ }
722
+
723
+ // Adds a raw `group by` clause to the query.
724
+ groupByRaw(sql, bindings) {
725
+ const raw = sql.isRawInstance ? sql : this.client.raw(sql, bindings);
726
+ this._statements.push({
727
+ grouping: 'group',
728
+ type: 'groupByRaw',
729
+ value: raw,
730
+ });
731
+ return this;
732
+ }
733
+
734
+ // Adds a `order by` clause to the query.
735
+ orderBy(column, direction, nulls = '') {
736
+ if (Array.isArray(column)) {
737
+ return this._orderByArray(column);
738
+ }
739
+ this._statements.push({
740
+ grouping: 'order',
741
+ type: 'orderByBasic',
742
+ value: column,
743
+ direction,
744
+ nulls,
745
+ });
746
+ return this;
747
+ }
748
+
749
+ // Adds a `order by` with multiple columns to the query.
750
+ _orderByArray(columnDefs) {
751
+ for (let i = 0; i < columnDefs.length; i++) {
752
+ const columnInfo = columnDefs[i];
753
+ if (isObject(columnInfo)) {
754
+ this._statements.push({
755
+ grouping: 'order',
756
+ type: 'orderByBasic',
757
+ value: columnInfo['column'],
758
+ direction: columnInfo['order'],
759
+ nulls: columnInfo['nulls'],
760
+ });
761
+ } else if (isString(columnInfo)) {
762
+ this._statements.push({
763
+ grouping: 'order',
764
+ type: 'orderByBasic',
765
+ value: columnInfo,
766
+ });
767
+ }
768
+ }
769
+ return this;
770
+ }
771
+
772
+ // Add a raw `order by` clause to the query.
773
+ orderByRaw(sql, bindings) {
774
+ const raw = sql.isRawInstance ? sql : this.client.raw(sql, bindings);
775
+ this._statements.push({
776
+ grouping: 'order',
777
+ type: 'orderByRaw',
778
+ value: raw,
779
+ });
780
+ return this;
781
+ }
782
+
783
+ _union(clause, args) {
784
+ let callbacks = args[0];
785
+ let wrap = args[1];
786
+ if (args.length === 1 || (args.length === 2 && isBoolean(wrap))) {
787
+ if (!Array.isArray(callbacks)) {
788
+ callbacks = [callbacks];
789
+ }
790
+ for (let i = 0, l = callbacks.length; i < l; i++) {
791
+ this._statements.push({
792
+ grouping: 'union',
793
+ clause: clause,
794
+ value: callbacks[i],
795
+ wrap: wrap || false,
796
+ });
797
+ }
798
+ } else {
799
+ callbacks = toArray(args).slice(0, args.length - 1);
800
+ wrap = args[args.length - 1];
801
+ if (!isBoolean(wrap)) {
802
+ callbacks.push(wrap);
803
+ wrap = false;
804
+ }
805
+ this._union(clause, [callbacks, wrap]);
806
+ }
807
+ return this;
808
+ }
809
+
810
+ // Add a union statement to the query.
811
+ union(...args) {
812
+ return this._union('union', args);
813
+ }
814
+
815
+ // Adds a union all statement to the query.
816
+ unionAll(...args) {
817
+ return this._union('union all', args);
818
+ }
819
+
820
+ // Adds an intersect statement to the query
821
+ intersect(callbacks, wrap) {
822
+ if (arguments.length === 1 || (arguments.length === 2 && isBoolean(wrap))) {
823
+ if (!Array.isArray(callbacks)) {
824
+ callbacks = [callbacks];
825
+ }
826
+ for (let i = 0, l = callbacks.length; i < l; i++) {
827
+ this._statements.push({
828
+ grouping: 'union',
829
+ clause: 'intersect',
830
+ value: callbacks[i],
831
+ wrap: wrap || false,
832
+ });
833
+ }
834
+ } else {
835
+ callbacks = toArray(arguments).slice(0, arguments.length - 1);
836
+ wrap = arguments[arguments.length - 1];
837
+ if (!isBoolean(wrap)) {
838
+ callbacks.push(wrap);
839
+ wrap = false;
840
+ }
841
+ this.intersect(callbacks, wrap);
842
+ }
843
+ return this;
844
+ }
845
+
846
+ // Adds a `having` clause to the query.
847
+ having(column, operator, value) {
848
+ if (column.isRawInstance && arguments.length === 1) {
849
+ return this.havingRaw(column);
850
+ }
851
+
852
+ // Check if the column is a function, in which case it's
853
+ // a having statement wrapped in parens.
854
+ if (typeof column === 'function') {
855
+ return this.havingWrapped(column);
856
+ }
857
+
858
+ this._statements.push({
859
+ grouping: 'having',
860
+ type: 'havingBasic',
861
+ column,
862
+ operator,
863
+ value,
864
+ bool: this._bool(),
865
+ not: this._not(),
866
+ });
867
+ return this;
868
+ }
869
+
870
+ orHaving(column, ...args) {
871
+ this._bool('or');
872
+ const obj = column;
873
+ if (isObject(obj) && !obj.isRawInstance) {
874
+ return this.havingWrapped(function () {
875
+ for (const key in obj) {
876
+ this.andHaving(key, obj[key]);
877
+ }
878
+ });
879
+ }
880
+ return this.having(column, ...args);
881
+ }
882
+
883
+ // Helper for compiling any advanced `having` queries.
884
+ havingWrapped(callback) {
885
+ this._statements.push({
886
+ grouping: 'having',
887
+ type: 'havingWrapped',
888
+ value: callback,
889
+ bool: this._bool(),
890
+ not: this._not(),
891
+ });
892
+ return this;
893
+ }
894
+
895
+ havingNull(column) {
896
+ this._statements.push({
897
+ grouping: 'having',
898
+ type: 'havingNull',
899
+ column,
900
+ not: this._not(),
901
+ bool: this._bool(),
902
+ });
903
+ return this;
904
+ }
905
+
906
+ orHavingNull(callback) {
907
+ return this._bool('or').havingNull(callback);
908
+ }
909
+
910
+ havingNotNull(callback) {
911
+ return this._not(true).havingNull(callback);
912
+ }
913
+
914
+ orHavingNotNull(callback) {
915
+ return this._not(true)._bool('or').havingNull(callback);
916
+ }
917
+
918
+ havingExists(callback) {
919
+ this._statements.push({
920
+ grouping: 'having',
921
+ type: 'havingExists',
922
+ value: callback,
923
+ not: this._not(),
924
+ bool: this._bool(),
925
+ });
926
+ return this;
927
+ }
928
+
929
+ orHavingExists(callback) {
930
+ return this._bool('or').havingExists(callback);
931
+ }
932
+
933
+ havingNotExists(callback) {
934
+ return this._not(true).havingExists(callback);
935
+ }
936
+
937
+ orHavingNotExists(callback) {
938
+ return this._not(true)._bool('or').havingExists(callback);
939
+ }
940
+
941
+ havingBetween(column, values) {
942
+ assert(
943
+ Array.isArray(values),
944
+ 'The second argument to havingBetween must be an array.'
945
+ );
946
+ assert(
947
+ values.length === 2,
948
+ 'You must specify 2 values for the havingBetween clause'
949
+ );
950
+ this._statements.push({
951
+ grouping: 'having',
952
+ type: 'havingBetween',
953
+ column,
954
+ value: values,
955
+ not: this._not(),
956
+ bool: this._bool(),
957
+ });
958
+ return this;
959
+ }
960
+
961
+ orHavingBetween(column, values) {
962
+ return this._bool('or').havingBetween(column, values);
963
+ }
964
+
965
+ havingNotBetween(column, values) {
966
+ return this._not(true).havingBetween(column, values);
967
+ }
968
+
969
+ orHavingNotBetween(column, values) {
970
+ return this._not(true)._bool('or').havingBetween(column, values);
971
+ }
972
+
973
+ havingIn(column, values) {
974
+ if (Array.isArray(values) && isEmpty(values))
975
+ return this.where(this._not());
976
+ this._statements.push({
977
+ grouping: 'having',
978
+ type: 'havingIn',
979
+ column,
980
+ value: values,
981
+ not: this._not(),
982
+ bool: this._bool(),
983
+ });
984
+ return this;
985
+ }
986
+
987
+ // Adds a `or where in` clause to the query.
988
+ orHavingIn(column, values) {
989
+ return this._bool('or').havingIn(column, values);
990
+ }
991
+
992
+ // Adds a `where not in` clause to the query.
993
+ havingNotIn(column, values) {
994
+ return this._not(true).havingIn(column, values);
995
+ }
996
+
997
+ // Adds a `or where not in` clause to the query.
998
+ orHavingNotIn(column, values) {
999
+ return this._bool('or')._not(true).havingIn(column, values);
1000
+ }
1001
+
1002
+ // Adds a raw `having` clause to the query.
1003
+ havingRaw(sql, bindings) {
1004
+ const raw = sql.isRawInstance ? sql : this.client.raw(sql, bindings);
1005
+ this._statements.push({
1006
+ grouping: 'having',
1007
+ type: 'havingRaw',
1008
+ value: raw,
1009
+ bool: this._bool(),
1010
+ not: this._not(),
1011
+ });
1012
+ return this;
1013
+ }
1014
+
1015
+ orHavingRaw(sql, bindings) {
1016
+ return this._bool('or').havingRaw(sql, bindings);
1017
+ }
1018
+
1019
+ // set the skip binding parameter (= insert the raw value in the query) for an attribute.
1020
+ _setSkipBinding(attribute, options) {
1021
+ let skipBinding = options;
1022
+ if (isObject(options)) {
1023
+ skipBinding = options.skipBinding;
1024
+ }
1025
+ this._single.skipBinding = this._single.skipBinding || {};
1026
+ this._single.skipBinding[attribute] = skipBinding;
1027
+ }
1028
+
1029
+ // Only allow a single "offset" to be set for the current query.
1030
+ offset(value, options) {
1031
+ if (value == null || value.isRawInstance || value instanceof Builder) {
1032
+ // Builder for backward compatibility
1033
+ this._single.offset = value;
1034
+ } else {
1035
+ const val = parseInt(value, 10);
1036
+ if (isNaN(val)) {
1037
+ this.client.logger.warn('A valid integer must be provided to offset');
1038
+ } else if (val < 0) {
1039
+ throw new Error(`A non-negative integer must be provided to offset.`);
1040
+ } else {
1041
+ this._single.offset = val;
1042
+ }
1043
+ }
1044
+ this._setSkipBinding('offset', options);
1045
+ return this;
1046
+ }
1047
+
1048
+ // Only allow a single "limit" to be set for the current query.
1049
+ limit(value, options) {
1050
+ const val = parseInt(value, 10);
1051
+ if (isNaN(val)) {
1052
+ this.client.logger.warn('A valid integer must be provided to limit');
1053
+ } else {
1054
+ this._single.limit = val;
1055
+ this._setSkipBinding('limit', options);
1056
+ }
1057
+ return this;
1058
+ }
1059
+
1060
+ // Retrieve the "count" result of the query.
1061
+ count(column, options) {
1062
+ return this._aggregate('count', column || '*', options);
1063
+ }
1064
+
1065
+ // Retrieve the minimum value of a given column.
1066
+ min(column, options) {
1067
+ return this._aggregate('min', column, options);
1068
+ }
1069
+
1070
+ // Retrieve the maximum value of a given column.
1071
+ max(column, options) {
1072
+ return this._aggregate('max', column, options);
1073
+ }
1074
+
1075
+ // Retrieve the sum of the values of a given column.
1076
+ sum(column, options) {
1077
+ return this._aggregate('sum', column, options);
1078
+ }
1079
+
1080
+ // Retrieve the average of the values of a given column.
1081
+ avg(column, options) {
1082
+ return this._aggregate('avg', column, options);
1083
+ }
1084
+
1085
+ // Retrieve the "count" of the distinct results of the query.
1086
+ countDistinct(...columns) {
1087
+ let options;
1088
+ if (columns.length > 1 && isPlainObject(last(columns))) {
1089
+ [options] = columns.splice(columns.length - 1, 1);
1090
+ }
1091
+
1092
+ if (!columns.length) {
1093
+ columns = '*';
1094
+ } else if (columns.length === 1) {
1095
+ columns = columns[0];
1096
+ }
1097
+
1098
+ return this._aggregate('count', columns, { ...options, distinct: true });
1099
+ }
1100
+
1101
+ // Retrieve the sum of the distinct values of a given column.
1102
+ sumDistinct(column, options) {
1103
+ return this._aggregate('sum', column, { ...options, distinct: true });
1104
+ }
1105
+
1106
+ // Retrieve the vg of the distinct results of the query.
1107
+ avgDistinct(column, options) {
1108
+ return this._aggregate('avg', column, { ...options, distinct: true });
1109
+ }
1110
+
1111
+ // Increments a column's value by the specified amount.
1112
+ increment(column, amount = 1) {
1113
+ if (isObject(column)) {
1114
+ for (const key in column) {
1115
+ this._counter(key, column[key]);
1116
+ }
1117
+
1118
+ return this;
1119
+ }
1120
+
1121
+ return this._counter(column, amount);
1122
+ }
1123
+
1124
+ // Decrements a column's value by the specified amount.
1125
+ decrement(column, amount = 1) {
1126
+ if (isObject(column)) {
1127
+ for (const key in column) {
1128
+ this._counter(key, -column[key]);
1129
+ }
1130
+
1131
+ return this;
1132
+ }
1133
+
1134
+ return this._counter(column, -amount);
1135
+ }
1136
+
1137
+ // Clears increments/decrements
1138
+ clearCounters() {
1139
+ this._single.counter = {};
1140
+ return this;
1141
+ }
1142
+
1143
+ // Sets the values for a `select` query, informing that only the first
1144
+ // row should be returned (limit 1).
1145
+ first(...args) {
1146
+ if (this._method && this._method !== 'select') {
1147
+ throw new Error(`Cannot chain .first() on "${this._method}" query`);
1148
+ }
1149
+
1150
+ this.select(normalizeArr(...args));
1151
+ this._method = 'first';
1152
+ this.limit(1);
1153
+ return this;
1154
+ }
1155
+
1156
+ // Use existing connection to execute the query
1157
+ // Same value that client.acquireConnection() for an according client returns should be passed
1158
+ connection(_connection) {
1159
+ this._connection = _connection;
1160
+ this.client.processPassedConnection(_connection);
1161
+ return this;
1162
+ }
1163
+
1164
+ // Pluck a column from a query.
1165
+ pluck(column) {
1166
+ if (this._method && this._method !== 'select') {
1167
+ throw new Error(`Cannot chain .pluck() on "${this._method}" query`);
1168
+ }
1169
+
1170
+ this._method = 'pluck';
1171
+ this._single.pluck = column;
1172
+ this._statements.push({
1173
+ grouping: 'columns',
1174
+ type: 'pluck',
1175
+ value: column,
1176
+ });
1177
+ return this;
1178
+ }
1179
+
1180
+ // Deprecated. Remove everything from select clause
1181
+ clearSelect() {
1182
+ this._clearGrouping('columns');
1183
+ return this;
1184
+ }
1185
+
1186
+ // Deprecated. Remove everything from where clause
1187
+ clearWhere() {
1188
+ this._clearGrouping('where');
1189
+ return this;
1190
+ }
1191
+
1192
+ // Deprecated. Remove everything from group clause
1193
+ clearGroup() {
1194
+ this._clearGrouping('group');
1195
+ return this;
1196
+ }
1197
+
1198
+ // Deprecated. Remove everything from order clause
1199
+ clearOrder() {
1200
+ this._clearGrouping('order');
1201
+ return this;
1202
+ }
1203
+
1204
+ // Deprecated. Remove everything from having clause
1205
+ clearHaving() {
1206
+ this._clearGrouping('having');
1207
+ return this;
1208
+ }
1209
+
1210
+ // Remove everything from statement clause
1211
+ clear(statement) {
1212
+ if (!CLEARABLE_STATEMENTS.has(statement))
1213
+ throw new Error(`Knex Error: unknown statement '${statement}'`);
1214
+ if (statement.startsWith('counter')) return this.clearCounters();
1215
+ if (statement === 'select') {
1216
+ statement = 'columns';
1217
+ }
1218
+ this._clearGrouping(statement);
1219
+ return this;
1220
+ }
1221
+
1222
+ // Insert & Update
1223
+ // ------
1224
+
1225
+ // Sets the values for an `insert` query.
1226
+ insert(values, returning, options) {
1227
+ this._method = 'insert';
1228
+ if (!isEmpty(returning)) this.returning(returning, options);
1229
+ this._single.insert = values;
1230
+ return this;
1231
+ }
1232
+
1233
+ // Sets the values for an `update`, allowing for both
1234
+ // `.update(key, value, [returning])` and `.update(obj, [returning])` syntaxes.
1235
+ update(values, returning, options) {
1236
+ let ret;
1237
+ const obj = this._single.update || {};
1238
+ this._method = 'update';
1239
+ if (isString(values)) {
1240
+ if (isPlainObject(returning)) {
1241
+ obj[values] = JSON.stringify(returning);
1242
+ } else {
1243
+ obj[values] = returning;
1244
+ }
1245
+ if (arguments.length > 2) {
1246
+ ret = arguments[2];
1247
+ }
1248
+ } else {
1249
+ const keys = Object.keys(values);
1250
+ if (this._single.update) {
1251
+ this.client.logger.warn('Update called multiple times with objects.');
1252
+ }
1253
+ let i = -1;
1254
+ while (++i < keys.length) {
1255
+ obj[keys[i]] = values[keys[i]];
1256
+ }
1257
+ ret = arguments[1];
1258
+ }
1259
+ if (!isEmpty(ret)) this.returning(ret, options);
1260
+ this._single.update = obj;
1261
+ return this;
1262
+ }
1263
+
1264
+ // Sets the returning value for the query.
1265
+ returning(returning, options) {
1266
+ this._single.returning = returning;
1267
+ this._single.options = options;
1268
+ return this;
1269
+ }
1270
+
1271
+ onConflict(columns) {
1272
+ if (typeof columns === 'string') {
1273
+ columns = [columns];
1274
+ }
1275
+ return new OnConflictBuilder(this, columns || true);
1276
+ }
1277
+
1278
+ // Delete
1279
+ // ------
1280
+
1281
+ // Executes a delete statement on the query;
1282
+ delete(ret, options) {
1283
+ this._method = 'del';
1284
+ if (!isEmpty(ret)) this.returning(ret, options);
1285
+ return this;
1286
+ }
1287
+
1288
+ // Truncates a table, ends the query chain.
1289
+ truncate(tableName) {
1290
+ this._method = 'truncate';
1291
+ if (tableName) {
1292
+ this._single.table = tableName;
1293
+ }
1294
+ return this;
1295
+ }
1296
+
1297
+ // Retrieves columns for the table specified by `knex(tableName)`
1298
+ columnInfo(column) {
1299
+ this._method = 'columnInfo';
1300
+ this._single.columnInfo = column;
1301
+ return this;
1302
+ }
1303
+
1304
+ // Set a lock for update constraint.
1305
+ forUpdate(...tables) {
1306
+ this._single.lock = lockMode.forUpdate;
1307
+ if (tables.length === 1 && Array.isArray(tables[0])) {
1308
+ this._single.lockTables = tables[0];
1309
+ } else {
1310
+ this._single.lockTables = tables;
1311
+ }
1312
+ return this;
1313
+ }
1314
+
1315
+ // Set a lock for share constraint.
1316
+ forShare(...tables) {
1317
+ this._single.lock = lockMode.forShare;
1318
+ this._single.lockTables = tables;
1319
+ return this;
1320
+ }
1321
+
1322
+ // Set a lock for no key update constraint.
1323
+ forNoKeyUpdate(...tables) {
1324
+ this._single.lock = lockMode.forNoKeyUpdate;
1325
+ this._single.lockTables = tables;
1326
+ return this;
1327
+ }
1328
+
1329
+ // Set a lock for key share constraint.
1330
+ forKeyShare(...tables) {
1331
+ this._single.lock = lockMode.forKeyShare;
1332
+ this._single.lockTables = tables;
1333
+ return this;
1334
+ }
1335
+
1336
+ // Skips locked rows when using a lock constraint.
1337
+ skipLocked() {
1338
+ if (!this._isSelectQuery()) {
1339
+ throw new Error(`Cannot chain .skipLocked() on "${this._method}" query!`);
1340
+ }
1341
+ if (!this._hasLockMode()) {
1342
+ throw new Error(
1343
+ '.skipLocked() can only be used after a call to .forShare() or .forUpdate()!'
1344
+ );
1345
+ }
1346
+ if (this._single.waitMode === waitMode.noWait) {
1347
+ throw new Error('.skipLocked() cannot be used together with .noWait()!');
1348
+ }
1349
+ this._single.waitMode = waitMode.skipLocked;
1350
+ return this;
1351
+ }
1352
+
1353
+ // Causes error when acessing a locked row instead of waiting for it to be released.
1354
+ noWait() {
1355
+ if (!this._isSelectQuery()) {
1356
+ throw new Error(`Cannot chain .noWait() on "${this._method}" query!`);
1357
+ }
1358
+ if (!this._hasLockMode()) {
1359
+ throw new Error(
1360
+ '.noWait() can only be used after a call to .forShare() or .forUpdate()!'
1361
+ );
1362
+ }
1363
+ if (this._single.waitMode === waitMode.skipLocked) {
1364
+ throw new Error('.noWait() cannot be used together with .skipLocked()!');
1365
+ }
1366
+ this._single.waitMode = waitMode.noWait;
1367
+ return this;
1368
+ }
1369
+
1370
+ // Takes a JS object of methods to call and calls them
1371
+ fromJS(obj) {
1372
+ each(obj, (val, key) => {
1373
+ if (typeof this[key] !== 'function') {
1374
+ this.client.logger.warn(`Knex Error: unknown key ${key}`);
1375
+ }
1376
+ if (Array.isArray(val)) {
1377
+ this[key].apply(this, val);
1378
+ } else {
1379
+ this[key](val);
1380
+ }
1381
+ });
1382
+ return this;
1383
+ }
1384
+
1385
+ fromRaw(sql, bindings) {
1386
+ const raw = sql.isRawInstance ? sql : this.client.raw(sql, bindings);
1387
+ return this.from(raw);
1388
+ }
1389
+
1390
+ // Passes query to provided callback function, useful for e.g. composing
1391
+ // domain-specific helpers
1392
+ modify(callback) {
1393
+ callback.apply(this, [this].concat(tail(arguments)));
1394
+ return this;
1395
+ }
1396
+
1397
+ upsert(values, returning, options) {
1398
+ throw new Error(
1399
+ `Upsert is not yet supported for dialect ${this.client.dialect}`
1400
+ );
1401
+ }
1402
+
1403
+ // JSON support functions
1404
+ _json(nameFunction, params) {
1405
+ this._statements.push({
1406
+ grouping: 'columns',
1407
+ type: 'json',
1408
+ method: nameFunction,
1409
+ params: params,
1410
+ });
1411
+ return this;
1412
+ }
1413
+
1414
+ jsonExtract() {
1415
+ const column = arguments[0];
1416
+ let path;
1417
+ let alias;
1418
+ let singleValue = true;
1419
+
1420
+ // We use arguments to have the signatures :
1421
+ // - column (string or array)
1422
+ // - column + path
1423
+ // - column + path + alias
1424
+ // - column + path + alias + singleValue
1425
+ // - column array + singleValue
1426
+ if (arguments.length >= 2) {
1427
+ path = arguments[1];
1428
+ }
1429
+ if (arguments.length >= 3) {
1430
+ alias = arguments[2];
1431
+ }
1432
+ if (arguments.length === 4) {
1433
+ singleValue = arguments[3];
1434
+ }
1435
+ if (
1436
+ arguments.length === 2 &&
1437
+ Array.isArray(arguments[0]) &&
1438
+ isBoolean(arguments[1])
1439
+ ) {
1440
+ singleValue = arguments[1];
1441
+ }
1442
+ return this._json('jsonExtract', {
1443
+ column: column,
1444
+ path: path,
1445
+ alias: alias,
1446
+ singleValue, // boolean used only in MSSQL to use function for extract value instead of object/array.
1447
+ });
1448
+ }
1449
+
1450
+ jsonSet(column, path, value, alias) {
1451
+ return this._json('jsonSet', {
1452
+ column: column,
1453
+ path: path,
1454
+ value: value,
1455
+ alias: alias,
1456
+ });
1457
+ }
1458
+
1459
+ jsonInsert(column, path, value, alias) {
1460
+ return this._json('jsonInsert', {
1461
+ column: column,
1462
+ path: path,
1463
+ value: value,
1464
+ alias: alias,
1465
+ });
1466
+ }
1467
+
1468
+ jsonRemove(column, path, alias) {
1469
+ return this._json('jsonRemove', {
1470
+ column: column,
1471
+ path: path,
1472
+ alias: alias,
1473
+ });
1474
+ }
1475
+
1476
+ // Wheres for JSON
1477
+ _isJsonObject(jsonValue) {
1478
+ return isObject(jsonValue) && !(jsonValue instanceof Builder);
1479
+ }
1480
+
1481
+ _whereJsonWrappedValue(type, column, value) {
1482
+ const whereJsonClause = {
1483
+ grouping: 'where',
1484
+ type: type,
1485
+ column,
1486
+ value: value,
1487
+ not: this._not(),
1488
+ bool: this._bool(),
1489
+ asColumn: this._asColumnFlag,
1490
+ };
1491
+ if (arguments[3]) {
1492
+ whereJsonClause.operator = arguments[3];
1493
+ }
1494
+ if (arguments[4]) {
1495
+ whereJsonClause.jsonPath = arguments[4];
1496
+ }
1497
+ this._statements.push(whereJsonClause);
1498
+ }
1499
+
1500
+ whereJsonObject(column, value) {
1501
+ this._whereJsonWrappedValue('whereJsonObject', column, value);
1502
+ return this;
1503
+ }
1504
+
1505
+ orWhereJsonObject(column, value) {
1506
+ return this._bool('or').whereJsonObject(column, value);
1507
+ }
1508
+
1509
+ whereNotJsonObject(column, value) {
1510
+ return this._not(true).whereJsonObject(column, value);
1511
+ }
1512
+
1513
+ orWhereNotJsonObject(column, value) {
1514
+ return this._bool('or').whereNotJsonObject(column, value);
1515
+ }
1516
+
1517
+ whereJsonPath(column, path, operator, value) {
1518
+ this._whereJsonWrappedValue('whereJsonPath', column, value, operator, path);
1519
+ return this;
1520
+ }
1521
+
1522
+ orWhereJsonPath(column, path, operator, value) {
1523
+ return this._bool('or').whereJsonPath(column, path, operator, value);
1524
+ }
1525
+
1526
+ // Json superset wheres
1527
+ whereJsonSupersetOf(column, value) {
1528
+ this._whereJsonWrappedValue('whereJsonSupersetOf', column, value);
1529
+ return this;
1530
+ }
1531
+
1532
+ whereJsonNotSupersetOf(column, value) {
1533
+ return this._not(true).whereJsonSupersetOf(column, value);
1534
+ }
1535
+
1536
+ orWhereJsonSupersetOf(column, value) {
1537
+ return this._bool('or').whereJsonSupersetOf(column, value);
1538
+ }
1539
+
1540
+ orWhereJsonNotSupersetOf(column, value) {
1541
+ return this._bool('or').whereJsonNotSupersetOf(column, value);
1542
+ }
1543
+
1544
+ // Json subset wheres
1545
+ whereJsonSubsetOf(column, value) {
1546
+ this._whereJsonWrappedValue('whereJsonSubsetOf', column, value);
1547
+ return this;
1548
+ }
1549
+
1550
+ whereJsonNotSubsetOf(column, value) {
1551
+ return this._not(true).whereJsonSubsetOf(column, value);
1552
+ }
1553
+
1554
+ orWhereJsonSubsetOf(column, value) {
1555
+ return this._bool('or').whereJsonSubsetOf(column, value);
1556
+ }
1557
+
1558
+ orWhereJsonNotSubsetOf(column, value) {
1559
+ return this._bool('or').whereJsonNotSubsetOf(column, value);
1560
+ }
1561
+
1562
+ whereJsonHasNone(column, values) {
1563
+ this._not(true).whereJsonHasAll(column, values);
1564
+ return this;
1565
+ }
1566
+
1567
+ // end of wheres for JSON
1568
+
1569
+ _analytic(alias, second, third) {
1570
+ let analytic;
1571
+ const { schema } = this._single;
1572
+ const method = this._analyticMethod();
1573
+ alias = typeof alias === 'string' ? alias : null;
1574
+
1575
+ assert(
1576
+ typeof second === 'function' ||
1577
+ second.isRawInstance ||
1578
+ Array.isArray(second) ||
1579
+ typeof second === 'string' ||
1580
+ typeof second === 'object',
1581
+ `The second argument to an analytic function must be either a function, a raw,
1582
+ an array of string or object, an object or a single string.`
1583
+ );
1584
+
1585
+ if (third) {
1586
+ assert(
1587
+ Array.isArray(third) ||
1588
+ typeof third === 'string' ||
1589
+ typeof third === 'object',
1590
+ 'The third argument to an analytic function must be either a string, an array of string or object or an object.'
1591
+ );
1592
+ }
1593
+
1594
+ if (isFunction(second)) {
1595
+ analytic = new Analytic(method, schema, alias);
1596
+ second.call(analytic, analytic);
1597
+ } else if (second.isRawInstance) {
1598
+ const raw = second;
1599
+ analytic = {
1600
+ grouping: 'columns',
1601
+ type: 'analytic',
1602
+ method: method,
1603
+ raw: raw,
1604
+ alias: alias,
1605
+ };
1606
+ } else {
1607
+ const order = !Array.isArray(second) ? [second] : second;
1608
+ let partitions = third || [];
1609
+ partitions = !Array.isArray(partitions) ? [partitions] : partitions;
1610
+ analytic = {
1611
+ grouping: 'columns',
1612
+ type: 'analytic',
1613
+ method: method,
1614
+ order: order,
1615
+ alias: alias,
1616
+ partitions: partitions,
1617
+ };
1618
+ }
1619
+ this._statements.push(analytic);
1620
+ return this;
1621
+ }
1622
+
1623
+ rank(...args) {
1624
+ return this._analyticMethod('rank')._analytic(...args);
1625
+ }
1626
+
1627
+ denseRank(...args) {
1628
+ return this._analyticMethod('dense_rank')._analytic(...args);
1629
+ }
1630
+
1631
+ rowNumber(...args) {
1632
+ return this._analyticMethod('row_number')._analytic(...args);
1633
+ }
1634
+
1635
+ // ----------------------------------------------------------------------
1636
+
1637
+ // Helper for the incrementing/decrementing queries.
1638
+ _counter(column, amount) {
1639
+ amount = parseFloat(amount);
1640
+
1641
+ this._method = 'update';
1642
+
1643
+ this._single.counter = this._single.counter || {};
1644
+
1645
+ this._single.counter[column] = amount;
1646
+
1647
+ return this;
1648
+ }
1649
+
1650
+ // Helper to get or set the "boolFlag" value.
1651
+ _bool(val) {
1652
+ if (arguments.length === 1) {
1653
+ this._boolFlag = val;
1654
+ return this;
1655
+ }
1656
+ const ret = this._boolFlag;
1657
+ this._boolFlag = 'and';
1658
+ return ret;
1659
+ }
1660
+
1661
+ // Helper to get or set the "notFlag" value.
1662
+ _not(val) {
1663
+ if (arguments.length === 1) {
1664
+ this._notFlag = val;
1665
+ return this;
1666
+ }
1667
+ const ret = this._notFlag;
1668
+ this._notFlag = false;
1669
+ return ret;
1670
+ }
1671
+
1672
+ // Helper to get or set the "joinFlag" value.
1673
+ _joinType(val) {
1674
+ if (arguments.length === 1) {
1675
+ this._joinFlag = val;
1676
+ return this;
1677
+ }
1678
+ const ret = this._joinFlag || 'inner';
1679
+ this._joinFlag = 'inner';
1680
+ return ret;
1681
+ }
1682
+
1683
+ _analyticMethod(val) {
1684
+ if (arguments.length === 1) {
1685
+ this._analyticFlag = val;
1686
+ return this;
1687
+ }
1688
+ return this._analyticFlag || 'row_number';
1689
+ }
1690
+
1691
+ // Helper for compiling any aggregate queries.
1692
+ _aggregate(method, column, options = {}) {
1693
+ this._statements.push({
1694
+ grouping: 'columns',
1695
+ type: column.isRawInstance ? 'aggregateRaw' : 'aggregate',
1696
+ method,
1697
+ value: column,
1698
+ aggregateDistinct: options.distinct || false,
1699
+ alias: options.as,
1700
+ });
1701
+ return this;
1702
+ }
1703
+
1704
+ // Helper function for clearing or reseting a grouping type from the builder
1705
+ _clearGrouping(grouping) {
1706
+ if (grouping in this._single) {
1707
+ this._single[grouping] = undefined;
1708
+ } else {
1709
+ this._statements = reject(this._statements, { grouping });
1710
+ }
1711
+ }
1712
+
1713
+ // Helper function that checks if the builder will emit a select query
1714
+ _isSelectQuery() {
1715
+ return SELECT_COMMANDS.has(this._method);
1716
+ }
1717
+
1718
+ // Helper function that checks if the query has a lock mode set
1719
+ _hasLockMode() {
1720
+ return LOCK_MODES.has(this._single.lock);
1721
+ }
1722
+ }
1723
+
1724
+ Builder.prototype.select = Builder.prototype.columns;
1725
+ Builder.prototype.column = Builder.prototype.columns;
1726
+ Builder.prototype.andWhereNot = Builder.prototype.whereNot;
1727
+ Builder.prototype.andWhereNotColumn = Builder.prototype.whereNotColumn;
1728
+ Builder.prototype.andWhere = Builder.prototype.where;
1729
+ Builder.prototype.andWhereColumn = Builder.prototype.whereColumn;
1730
+ Builder.prototype.andWhereRaw = Builder.prototype.whereRaw;
1731
+ Builder.prototype.andWhereBetween = Builder.prototype.whereBetween;
1732
+ Builder.prototype.andWhereNotBetween = Builder.prototype.whereNotBetween;
1733
+ Builder.prototype.andWhereJsonObject = Builder.prototype.whereJsonObject;
1734
+ Builder.prototype.andWhereNotJsonObject = Builder.prototype.whereJsonObject;
1735
+ Builder.prototype.andWhereJsonPath = Builder.prototype.whereJsonPath;
1736
+ Builder.prototype.andWhereLike = Builder.prototype.whereLike;
1737
+ Builder.prototype.andWhereILike = Builder.prototype.whereILike;
1738
+ Builder.prototype.andHaving = Builder.prototype.having;
1739
+ Builder.prototype.andHavingIn = Builder.prototype.havingIn;
1740
+ Builder.prototype.andHavingNotIn = Builder.prototype.havingNotIn;
1741
+ Builder.prototype.andHavingNull = Builder.prototype.havingNull;
1742
+ Builder.prototype.andHavingNotNull = Builder.prototype.havingNotNull;
1743
+ Builder.prototype.andHavingExists = Builder.prototype.havingExists;
1744
+ Builder.prototype.andHavingNotExists = Builder.prototype.havingNotExists;
1745
+ Builder.prototype.andHavingBetween = Builder.prototype.havingBetween;
1746
+ Builder.prototype.andHavingNotBetween = Builder.prototype.havingNotBetween;
1747
+ Builder.prototype.from = Builder.prototype.table;
1748
+ Builder.prototype.into = Builder.prototype.table;
1749
+ Builder.prototype.del = Builder.prototype.delete;
1750
+
1751
+ // Attach all of the top level promise methods that should be chainable.
1752
+ augmentWithBuilderInterface(Builder);
1753
+ addQueryContext(Builder);
1754
+
1755
+ Builder.extend = (methodName, fn) => {
1756
+ if (Object.prototype.hasOwnProperty.call(Builder.prototype, methodName)) {
1757
+ throw new Error(
1758
+ `Can't extend QueryBuilder with existing method ('${methodName}').`
1759
+ );
1760
+ }
1761
+
1762
+ assign(Builder.prototype, { [methodName]: fn });
1763
+ };
1764
+
1765
+ // Sub-builder for onConflict clauses
1766
+ class OnConflictBuilder {
1767
+ constructor(builder, columns) {
1768
+ this.builder = builder;
1769
+ this._columns = columns;
1770
+ }
1771
+
1772
+ // Sets insert query to ignore conflicts
1773
+ ignore() {
1774
+ this.builder._single.onConflict = this._columns;
1775
+ this.builder._single.ignore = true;
1776
+ return this.builder;
1777
+ }
1778
+
1779
+ // Sets insert query to update on conflict
1780
+ merge(updates) {
1781
+ this.builder._single.onConflict = this._columns;
1782
+ this.builder._single.merge = { updates };
1783
+ return this.builder;
1784
+ }
1785
+
1786
+ // Prevent
1787
+ then() {
1788
+ throw new Error(
1789
+ 'Incomplete onConflict clause. .onConflict() must be directly followed by either .merge() or .ignore()'
1790
+ );
1791
+ }
1792
+ }
1793
+
1794
+ module.exports = Builder;