knex 0.21.20 → 0.21.21

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 (141) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/CONTRIBUTING.md +184 -184
  3. package/LICENSE +22 -22
  4. package/README.md +95 -95
  5. package/bin/cli.js +414 -414
  6. package/bin/utils/cli-config-utils.js +151 -151
  7. package/bin/utils/constants.js +7 -7
  8. package/bin/utils/migrationsLister.js +37 -37
  9. package/knex.js +8 -8
  10. package/lib/client.js +413 -413
  11. package/lib/config-resolver.js +61 -61
  12. package/lib/constants.js +44 -44
  13. package/lib/dialects/mssql/index.js +390 -390
  14. package/lib/dialects/mssql/query/compiler.js +444 -444
  15. package/lib/dialects/mssql/schema/columncompiler.js +103 -103
  16. package/lib/dialects/mssql/schema/compiler.js +59 -59
  17. package/lib/dialects/mssql/schema/tablecompiler.js +245 -245
  18. package/lib/dialects/mssql/transaction.js +97 -97
  19. package/lib/dialects/mysql/index.js +191 -191
  20. package/lib/dialects/mysql/query/compiler.js +142 -142
  21. package/lib/dialects/mysql/schema/columncompiler.js +171 -171
  22. package/lib/dialects/mysql/schema/compiler.js +60 -60
  23. package/lib/dialects/mysql/schema/tablecompiler.js +262 -262
  24. package/lib/dialects/mysql/transaction.js +48 -48
  25. package/lib/dialects/mysql2/index.js +35 -35
  26. package/lib/dialects/mysql2/transaction.js +46 -46
  27. package/lib/dialects/oracle/DEAD_CODE.md +5 -5
  28. package/lib/dialects/oracle/formatter.js +20 -20
  29. package/lib/dialects/oracle/index.js +79 -79
  30. package/lib/dialects/oracle/query/compiler.js +327 -327
  31. package/lib/dialects/oracle/schema/columnbuilder.js +18 -18
  32. package/lib/dialects/oracle/schema/columncompiler.js +139 -139
  33. package/lib/dialects/oracle/schema/compiler.js +81 -81
  34. package/lib/dialects/oracle/schema/tablecompiler.js +165 -165
  35. package/lib/dialects/oracle/schema/trigger.js +126 -126
  36. package/lib/dialects/oracle/utils.js +86 -86
  37. package/lib/dialects/oracledb/index.js +489 -489
  38. package/lib/dialects/oracledb/query/compiler.js +363 -363
  39. package/lib/dialects/oracledb/schema/columncompiler.js +35 -35
  40. package/lib/dialects/oracledb/transaction.js +76 -76
  41. package/lib/dialects/oracledb/utils.js +14 -14
  42. package/lib/dialects/postgres/index.js +319 -319
  43. package/lib/dialects/postgres/query/compiler.js +206 -206
  44. package/lib/dialects/postgres/schema/columncompiler.js +125 -125
  45. package/lib/dialects/postgres/schema/compiler.js +109 -109
  46. package/lib/dialects/postgres/schema/tablecompiler.js +183 -183
  47. package/lib/dialects/redshift/index.js +73 -73
  48. package/lib/dialects/redshift/query/compiler.js +119 -119
  49. package/lib/dialects/redshift/schema/columnbuilder.js +20 -20
  50. package/lib/dialects/redshift/schema/columncompiler.js +60 -60
  51. package/lib/dialects/redshift/schema/compiler.js +14 -14
  52. package/lib/dialects/redshift/schema/tablecompiler.js +123 -123
  53. package/lib/dialects/redshift/transaction.js +18 -18
  54. package/lib/dialects/sqlite3/formatter.js +21 -21
  55. package/lib/dialects/sqlite3/index.js +169 -169
  56. package/lib/dialects/sqlite3/query/compiler.js +222 -222
  57. package/lib/dialects/sqlite3/schema/columncompiler.js +27 -27
  58. package/lib/dialects/sqlite3/schema/compiler.js +49 -49
  59. package/lib/dialects/sqlite3/schema/ddl.js +525 -525
  60. package/lib/dialects/sqlite3/schema/tablecompiler.js +238 -238
  61. package/lib/formatter.js +295 -295
  62. package/lib/functionhelper.js +14 -14
  63. package/lib/helpers.js +92 -92
  64. package/lib/index.js +3 -3
  65. package/lib/interface.js +115 -115
  66. package/lib/knex.js +42 -42
  67. package/lib/logger.js +76 -76
  68. package/lib/migrate/MigrationGenerator.js +82 -82
  69. package/lib/migrate/Migrator.js +611 -611
  70. package/lib/migrate/configuration-merger.js +60 -60
  71. package/lib/migrate/migrate-stub.js +17 -17
  72. package/lib/migrate/migration-list-resolver.js +36 -36
  73. package/lib/migrate/sources/fs-migrations.js +99 -99
  74. package/lib/migrate/stub/cjs.stub +15 -15
  75. package/lib/migrate/stub/coffee.stub +13 -13
  76. package/lib/migrate/stub/eg.stub +14 -14
  77. package/lib/migrate/stub/js.stub +15 -15
  78. package/lib/migrate/stub/knexfile-coffee.stub +34 -34
  79. package/lib/migrate/stub/knexfile-eg.stub +43 -43
  80. package/lib/migrate/stub/knexfile-js.stub +44 -44
  81. package/lib/migrate/stub/knexfile-ls.stub +35 -35
  82. package/lib/migrate/stub/knexfile-ts.stub +44 -44
  83. package/lib/migrate/stub/ls.stub +14 -14
  84. package/lib/migrate/stub/ts.stub +21 -21
  85. package/lib/migrate/table-creator.js +67 -67
  86. package/lib/migrate/table-resolver.js +27 -27
  87. package/lib/query/builder.js +1372 -1372
  88. package/lib/query/compiler.js +889 -889
  89. package/lib/query/constants.js +13 -13
  90. package/lib/query/joinclause.js +263 -263
  91. package/lib/query/methods.js +92 -92
  92. package/lib/query/string.js +190 -190
  93. package/lib/raw.js +188 -188
  94. package/lib/ref.js +39 -39
  95. package/lib/runner.js +285 -285
  96. package/lib/schema/builder.js +82 -82
  97. package/lib/schema/columnbuilder.js +117 -117
  98. package/lib/schema/columncompiler.js +177 -177
  99. package/lib/schema/compiler.js +101 -101
  100. package/lib/schema/helpers.js +51 -51
  101. package/lib/schema/tablebuilder.js +288 -288
  102. package/lib/schema/tablecompiler.js +296 -296
  103. package/lib/seed/Seeder.js +203 -203
  104. package/lib/seed/seed-stub.js +13 -13
  105. package/lib/seed/stub/coffee.stub +9 -9
  106. package/lib/seed/stub/eg.stub +11 -11
  107. package/lib/seed/stub/js.stub +13 -13
  108. package/lib/seed/stub/ls.stub +11 -11
  109. package/lib/seed/stub/ts.stub +13 -13
  110. package/lib/transaction.js +363 -363
  111. package/lib/util/batchInsert.js +59 -59
  112. package/lib/util/delay.js +6 -6
  113. package/lib/util/fake-client.js +9 -9
  114. package/lib/util/finally-mixin.js +13 -13
  115. package/lib/util/fs.js +76 -76
  116. package/lib/util/import-file.js +13 -13
  117. package/lib/util/is-module-type.js +14 -14
  118. package/lib/util/is.js +32 -32
  119. package/lib/util/make-knex.js +338 -338
  120. package/lib/util/nanoid.js +29 -29
  121. package/lib/util/noop.js +1 -1
  122. package/lib/util/parse-connection.js +66 -66
  123. package/lib/util/save-async-stack.js +14 -14
  124. package/lib/util/template.js +52 -52
  125. package/lib/util/timeout.js +29 -29
  126. package/lib/util/timestamp.js +16 -16
  127. package/package.json +1 -1
  128. package/scripts/build.js +125 -125
  129. package/scripts/docker-compose.yml +111 -111
  130. package/scripts/next-release-howto.md +24 -24
  131. package/scripts/release.sh +34 -34
  132. package/scripts/runkit-example.js +34 -34
  133. package/scripts/stress-test/README.txt +18 -18
  134. package/scripts/stress-test/docker-compose.yml +47 -47
  135. package/scripts/stress-test/knex-stress-test.js +196 -196
  136. package/scripts/stress-test/mysql2-random-hanging-every-now-and-then.js +145 -145
  137. package/scripts/stress-test/mysql2-sudden-exit-without-error.js +100 -100
  138. package/scripts/stress-test/reconnect-test-mysql-based-drivers.js +184 -184
  139. package/types/index.d.ts +2249 -2249
  140. package/types/result.d.ts +27 -27
  141. package/types/tables.d.ts +4 -4
@@ -1,363 +1,363 @@
1
- const clone = require('lodash/clone');
2
- const each = require('lodash/each');
3
- const isEmpty = require('lodash/isEmpty');
4
- const isPlainObject = require('lodash/isPlainObject');
5
- const Oracle_Compiler = require('../../oracle/query/compiler');
6
- const ReturningHelper = require('../utils').ReturningHelper;
7
- const BlobHelper = require('../utils').BlobHelper;
8
- const { isString } = require('../../../util/is');
9
-
10
- class Oracledb_Compiler extends Oracle_Compiler {
11
- constructor(client, builder) {
12
- super(client, builder);
13
- }
14
-
15
- // Compiles an "insert" query, allowing for multiple
16
- // inserts using a single query statement.
17
- insert() {
18
- const self = this;
19
- const outBindPrep = this._prepOutbindings(
20
- this.single.insert,
21
- this.single.returning
22
- );
23
- const outBinding = outBindPrep.outBinding;
24
- const returning = outBindPrep.returning;
25
- const insertValues = outBindPrep.values;
26
-
27
- if (
28
- Array.isArray(insertValues) &&
29
- insertValues.length === 1 &&
30
- isEmpty(insertValues[0])
31
- ) {
32
- const returningFragment = this.single.returning
33
- ? ' (' + this.formatter.wrap(this.single.returning) + ')'
34
- : '';
35
-
36
- return this._addReturningToSqlAndConvert(
37
- 'insert into ' +
38
- this.tableName +
39
- returningFragment +
40
- ' values (default)',
41
- outBinding[0],
42
- this.tableName,
43
- returning
44
- );
45
- }
46
-
47
- if (
48
- isEmpty(this.single.insert) &&
49
- typeof this.single.insert !== 'function'
50
- ) {
51
- return '';
52
- }
53
-
54
- const insertData = this._prepInsert(insertValues);
55
-
56
- const sql = {};
57
-
58
- if (isString(insertData)) {
59
- return this._addReturningToSqlAndConvert(
60
- 'insert into ' + this.tableName + ' ' + insertData,
61
- outBinding[0],
62
- this.tableName,
63
- returning
64
- );
65
- }
66
-
67
- if (insertData.values.length === 1) {
68
- return this._addReturningToSqlAndConvert(
69
- 'insert into ' +
70
- this.tableName +
71
- ' (' +
72
- this.formatter.columnize(insertData.columns) +
73
- ') values (' +
74
- this.formatter.parameterize(insertData.values[0]) +
75
- ')',
76
- outBinding[0],
77
- this.tableName,
78
- returning
79
- );
80
- }
81
-
82
- const insertDefaultsOnly = insertData.columns.length === 0;
83
- sql.returning = returning;
84
- sql.sql =
85
- 'begin ' +
86
- insertData.values
87
- .map(function (value, index) {
88
- const parameterizedValues = !insertDefaultsOnly
89
- ? self.formatter.parameterize(value, self.client.valueForUndefined)
90
- : '';
91
- let subSql = 'insert into ' + self.tableName;
92
-
93
- if (insertDefaultsOnly) {
94
- // No columns given so only the default value
95
- subSql +=
96
- ' (' +
97
- self.formatter.wrap(self.single.returning) +
98
- ') values (default)';
99
- } else {
100
- subSql +=
101
- ' (' +
102
- self.formatter.columnize(insertData.columns) +
103
- ') values (' +
104
- parameterizedValues +
105
- ')';
106
- }
107
-
108
- let returningClause = '';
109
- let intoClause = '';
110
- // ToDo review if this code is still needed or could be dropped
111
- // eslint-disable-next-line no-unused-vars
112
- let usingClause = '';
113
- let outClause = '';
114
-
115
- each(value, function (val) {
116
- if (!(val instanceof BlobHelper)) {
117
- usingClause += ' ?,';
118
- }
119
- });
120
- // eslint-disable-next-line no-unused-vars
121
- usingClause = usingClause.slice(0, -1);
122
-
123
- // Build returning and into clauses
124
- outBinding[index].forEach(function (ret) {
125
- const columnName = ret.columnName || ret;
126
- returningClause += self.formatter.wrap(columnName) + ',';
127
- intoClause += ' ?,';
128
- outClause += ' out ?,';
129
-
130
- // Add Helpers to bindings
131
- if (ret instanceof BlobHelper) {
132
- return self.formatter.bindings.push(ret);
133
- }
134
- self.formatter.bindings.push(new ReturningHelper(columnName));
135
- });
136
-
137
- // Strip last comma
138
- returningClause = returningClause.slice(0, -1);
139
- intoClause = intoClause.slice(0, -1);
140
- outClause = outClause.slice(0, -1);
141
-
142
- if (returningClause && intoClause) {
143
- subSql += ' returning ' + returningClause + ' into' + intoClause;
144
- }
145
-
146
- // Pre bind position because subSql is an execute immediate parameter
147
- // later position binding will only convert the ? params
148
- subSql = self.formatter.client.positionBindings(subSql);
149
- const parameterizedValuesWithoutDefaultAndBlob = parameterizedValues
150
- .replace('DEFAULT, ', '')
151
- .replace(', DEFAULT', '')
152
- .replace('EMPTY_BLOB(), ', '')
153
- .replace(', EMPTY_BLOB()', '');
154
- return (
155
- "execute immediate '" +
156
- subSql.replace(/'/g, "''") +
157
- (parameterizedValuesWithoutDefaultAndBlob || value
158
- ? "' using "
159
- : '') +
160
- parameterizedValuesWithoutDefaultAndBlob +
161
- (parameterizedValuesWithoutDefaultAndBlob && outClause ? ',' : '') +
162
- outClause +
163
- ';'
164
- );
165
- })
166
- .join(' ') +
167
- 'end;';
168
-
169
- sql.outBinding = outBinding;
170
- if (returning[0] === '*') {
171
- // Generate select statement with special order by
172
- // to keep the order because 'in (..)' may change the order
173
- sql.returningSql = function () {
174
- return (
175
- 'select * from ' +
176
- self.tableName +
177
- ' where ROWID in (' +
178
- this.outBinding
179
- .map(function (v, i) {
180
- return ':' + (i + 1);
181
- })
182
- .join(', ') +
183
- ')' +
184
- ' order by case ROWID ' +
185
- this.outBinding
186
- .map(function (v, i) {
187
- return 'when CHARTOROWID(:' + (i + 1) + ') then ' + i;
188
- })
189
- .join(' ') +
190
- ' end'
191
- );
192
- };
193
- }
194
-
195
- return sql;
196
- }
197
-
198
- _addReturningToSqlAndConvert(sql, outBinding, tableName, returning) {
199
- const self = this;
200
- const res = {
201
- sql: sql,
202
- };
203
-
204
- if (!outBinding) {
205
- return res;
206
- }
207
- const returningValues = Array.isArray(outBinding)
208
- ? outBinding
209
- : [outBinding];
210
- let returningClause = '';
211
- let intoClause = '';
212
- // Build returning and into clauses
213
- returningValues.forEach(function (ret) {
214
- const columnName = ret.columnName || ret;
215
- returningClause += self.formatter.wrap(columnName) + ',';
216
- intoClause += '?,';
217
-
218
- // Add Helpers to bindings
219
- if (ret instanceof BlobHelper) {
220
- return self.formatter.bindings.push(ret);
221
- }
222
- self.formatter.bindings.push(new ReturningHelper(columnName));
223
- });
224
- res.sql = sql;
225
-
226
- // Strip last comma
227
- returningClause = returningClause.slice(0, -1);
228
- intoClause = intoClause.slice(0, -1);
229
- if (returningClause && intoClause) {
230
- res.sql += ' returning ' + returningClause + ' into ' + intoClause;
231
- }
232
- res.outBinding = [outBinding];
233
- if (returning[0] === '*') {
234
- res.returningSql = function () {
235
- return 'select * from ' + self.tableName + ' where ROWID = :1';
236
- };
237
- }
238
- res.returning = returning;
239
-
240
- return res;
241
- }
242
-
243
- _prepOutbindings(paramValues, paramReturning) {
244
- const result = {};
245
- let params = paramValues || [];
246
- let returning = paramReturning || [];
247
- if (!Array.isArray(params) && isPlainObject(paramValues)) {
248
- params = [params];
249
- }
250
- // Always wrap returning argument in array
251
- if (returning && !Array.isArray(returning)) {
252
- returning = [returning];
253
- }
254
-
255
- const outBinding = [];
256
- // Handle Buffer value as Blob
257
- each(params, function (values, index) {
258
- if (returning[0] === '*') {
259
- outBinding[index] = ['ROWID'];
260
- } else {
261
- outBinding[index] = clone(returning);
262
- }
263
- each(values, function (value, key) {
264
- if (value instanceof Buffer) {
265
- values[key] = new BlobHelper(key, value);
266
-
267
- // Delete blob duplicate in returning
268
- const blobIndex = outBinding[index].indexOf(key);
269
- if (blobIndex >= 0) {
270
- outBinding[index].splice(blobIndex, 1);
271
- values[key].returning = true;
272
- }
273
- outBinding[index].push(values[key]);
274
- }
275
- if (value === undefined) {
276
- delete params[index][key];
277
- }
278
- });
279
- });
280
- result.returning = returning;
281
- result.outBinding = outBinding;
282
- result.values = params;
283
- return result;
284
- }
285
-
286
- update() {
287
- const self = this;
288
- const sql = {};
289
- const outBindPrep = this._prepOutbindings(
290
- this.single.update || this.single.counter,
291
- this.single.returning
292
- );
293
- const outBinding = outBindPrep.outBinding;
294
- const returning = outBindPrep.returning;
295
-
296
- const updates = this._prepUpdate(this.single.update);
297
- const where = this.where();
298
-
299
- let returningClause = '';
300
- let intoClause = '';
301
-
302
- if (isEmpty(updates) && typeof this.single.update !== 'function') {
303
- return '';
304
- }
305
-
306
- // Build returning and into clauses
307
- outBinding.forEach(function (out) {
308
- out.forEach(function (ret) {
309
- const columnName = ret.columnName || ret;
310
- returningClause += self.formatter.wrap(columnName) + ',';
311
- intoClause += ' ?,';
312
-
313
- // Add Helpers to bindings
314
- if (ret instanceof BlobHelper) {
315
- return self.formatter.bindings.push(ret);
316
- }
317
- self.formatter.bindings.push(new ReturningHelper(columnName));
318
- });
319
- });
320
- // Strip last comma
321
- returningClause = returningClause.slice(0, -1);
322
- intoClause = intoClause.slice(0, -1);
323
-
324
- sql.outBinding = outBinding;
325
- sql.returning = returning;
326
- sql.sql =
327
- 'update ' +
328
- this.tableName +
329
- ' set ' +
330
- updates.join(', ') +
331
- (where ? ' ' + where : '');
332
- if (outBinding.length && !isEmpty(outBinding[0])) {
333
- sql.sql += ' returning ' + returningClause + ' into' + intoClause;
334
- }
335
- if (returning[0] === '*') {
336
- sql.returningSql = function () {
337
- let sql = 'select * from ' + self.tableName;
338
- const modifiedRowsCount = this.rowsAffected.length || this.rowsAffected;
339
- let returningSqlIn = ' where ROWID in (';
340
- let returningSqlOrderBy = ') order by case ROWID ';
341
-
342
- // Needs special order by because in(...) change result order
343
- for (let i = 0; i < modifiedRowsCount; i++) {
344
- if (this.returning[0] === '*') {
345
- returningSqlIn += ':' + (i + 1) + ', ';
346
- returningSqlOrderBy +=
347
- 'when CHARTOROWID(:' + (i + 1) + ') then ' + i + ' ';
348
- }
349
- }
350
- if (this.returning[0] === '*') {
351
- this.returning = this.returning.slice(0, -1);
352
- returningSqlIn = returningSqlIn.slice(0, -2);
353
- returningSqlOrderBy = returningSqlOrderBy.slice(0, -1);
354
- }
355
- return (sql += returningSqlIn + returningSqlOrderBy + ' end');
356
- };
357
- }
358
-
359
- return sql;
360
- }
361
- }
362
-
363
- module.exports = Oracledb_Compiler;
1
+ const clone = require('lodash/clone');
2
+ const each = require('lodash/each');
3
+ const isEmpty = require('lodash/isEmpty');
4
+ const isPlainObject = require('lodash/isPlainObject');
5
+ const Oracle_Compiler = require('../../oracle/query/compiler');
6
+ const ReturningHelper = require('../utils').ReturningHelper;
7
+ const BlobHelper = require('../utils').BlobHelper;
8
+ const { isString } = require('../../../util/is');
9
+
10
+ class Oracledb_Compiler extends Oracle_Compiler {
11
+ constructor(client, builder) {
12
+ super(client, builder);
13
+ }
14
+
15
+ // Compiles an "insert" query, allowing for multiple
16
+ // inserts using a single query statement.
17
+ insert() {
18
+ const self = this;
19
+ const outBindPrep = this._prepOutbindings(
20
+ this.single.insert,
21
+ this.single.returning
22
+ );
23
+ const outBinding = outBindPrep.outBinding;
24
+ const returning = outBindPrep.returning;
25
+ const insertValues = outBindPrep.values;
26
+
27
+ if (
28
+ Array.isArray(insertValues) &&
29
+ insertValues.length === 1 &&
30
+ isEmpty(insertValues[0])
31
+ ) {
32
+ const returningFragment = this.single.returning
33
+ ? ' (' + this.formatter.wrap(this.single.returning) + ')'
34
+ : '';
35
+
36
+ return this._addReturningToSqlAndConvert(
37
+ 'insert into ' +
38
+ this.tableName +
39
+ returningFragment +
40
+ ' values (default)',
41
+ outBinding[0],
42
+ this.tableName,
43
+ returning
44
+ );
45
+ }
46
+
47
+ if (
48
+ isEmpty(this.single.insert) &&
49
+ typeof this.single.insert !== 'function'
50
+ ) {
51
+ return '';
52
+ }
53
+
54
+ const insertData = this._prepInsert(insertValues);
55
+
56
+ const sql = {};
57
+
58
+ if (isString(insertData)) {
59
+ return this._addReturningToSqlAndConvert(
60
+ 'insert into ' + this.tableName + ' ' + insertData,
61
+ outBinding[0],
62
+ this.tableName,
63
+ returning
64
+ );
65
+ }
66
+
67
+ if (insertData.values.length === 1) {
68
+ return this._addReturningToSqlAndConvert(
69
+ 'insert into ' +
70
+ this.tableName +
71
+ ' (' +
72
+ this.formatter.columnize(insertData.columns) +
73
+ ') values (' +
74
+ this.formatter.parameterize(insertData.values[0]) +
75
+ ')',
76
+ outBinding[0],
77
+ this.tableName,
78
+ returning
79
+ );
80
+ }
81
+
82
+ const insertDefaultsOnly = insertData.columns.length === 0;
83
+ sql.returning = returning;
84
+ sql.sql =
85
+ 'begin ' +
86
+ insertData.values
87
+ .map(function (value, index) {
88
+ const parameterizedValues = !insertDefaultsOnly
89
+ ? self.formatter.parameterize(value, self.client.valueForUndefined)
90
+ : '';
91
+ let subSql = 'insert into ' + self.tableName;
92
+
93
+ if (insertDefaultsOnly) {
94
+ // No columns given so only the default value
95
+ subSql +=
96
+ ' (' +
97
+ self.formatter.wrap(self.single.returning) +
98
+ ') values (default)';
99
+ } else {
100
+ subSql +=
101
+ ' (' +
102
+ self.formatter.columnize(insertData.columns) +
103
+ ') values (' +
104
+ parameterizedValues +
105
+ ')';
106
+ }
107
+
108
+ let returningClause = '';
109
+ let intoClause = '';
110
+ // ToDo review if this code is still needed or could be dropped
111
+ // eslint-disable-next-line no-unused-vars
112
+ let usingClause = '';
113
+ let outClause = '';
114
+
115
+ each(value, function (val) {
116
+ if (!(val instanceof BlobHelper)) {
117
+ usingClause += ' ?,';
118
+ }
119
+ });
120
+ // eslint-disable-next-line no-unused-vars
121
+ usingClause = usingClause.slice(0, -1);
122
+
123
+ // Build returning and into clauses
124
+ outBinding[index].forEach(function (ret) {
125
+ const columnName = ret.columnName || ret;
126
+ returningClause += self.formatter.wrap(columnName) + ',';
127
+ intoClause += ' ?,';
128
+ outClause += ' out ?,';
129
+
130
+ // Add Helpers to bindings
131
+ if (ret instanceof BlobHelper) {
132
+ return self.formatter.bindings.push(ret);
133
+ }
134
+ self.formatter.bindings.push(new ReturningHelper(columnName));
135
+ });
136
+
137
+ // Strip last comma
138
+ returningClause = returningClause.slice(0, -1);
139
+ intoClause = intoClause.slice(0, -1);
140
+ outClause = outClause.slice(0, -1);
141
+
142
+ if (returningClause && intoClause) {
143
+ subSql += ' returning ' + returningClause + ' into' + intoClause;
144
+ }
145
+
146
+ // Pre bind position because subSql is an execute immediate parameter
147
+ // later position binding will only convert the ? params
148
+ subSql = self.formatter.client.positionBindings(subSql);
149
+ const parameterizedValuesWithoutDefaultAndBlob = parameterizedValues
150
+ .replace('DEFAULT, ', '')
151
+ .replace(', DEFAULT', '')
152
+ .replace('EMPTY_BLOB(), ', '')
153
+ .replace(', EMPTY_BLOB()', '');
154
+ return (
155
+ "execute immediate '" +
156
+ subSql.replace(/'/g, "''") +
157
+ (parameterizedValuesWithoutDefaultAndBlob || value
158
+ ? "' using "
159
+ : '') +
160
+ parameterizedValuesWithoutDefaultAndBlob +
161
+ (parameterizedValuesWithoutDefaultAndBlob && outClause ? ',' : '') +
162
+ outClause +
163
+ ';'
164
+ );
165
+ })
166
+ .join(' ') +
167
+ 'end;';
168
+
169
+ sql.outBinding = outBinding;
170
+ if (returning[0] === '*') {
171
+ // Generate select statement with special order by
172
+ // to keep the order because 'in (..)' may change the order
173
+ sql.returningSql = function () {
174
+ return (
175
+ 'select * from ' +
176
+ self.tableName +
177
+ ' where ROWID in (' +
178
+ this.outBinding
179
+ .map(function (v, i) {
180
+ return ':' + (i + 1);
181
+ })
182
+ .join(', ') +
183
+ ')' +
184
+ ' order by case ROWID ' +
185
+ this.outBinding
186
+ .map(function (v, i) {
187
+ return 'when CHARTOROWID(:' + (i + 1) + ') then ' + i;
188
+ })
189
+ .join(' ') +
190
+ ' end'
191
+ );
192
+ };
193
+ }
194
+
195
+ return sql;
196
+ }
197
+
198
+ _addReturningToSqlAndConvert(sql, outBinding, tableName, returning) {
199
+ const self = this;
200
+ const res = {
201
+ sql: sql,
202
+ };
203
+
204
+ if (!outBinding) {
205
+ return res;
206
+ }
207
+ const returningValues = Array.isArray(outBinding)
208
+ ? outBinding
209
+ : [outBinding];
210
+ let returningClause = '';
211
+ let intoClause = '';
212
+ // Build returning and into clauses
213
+ returningValues.forEach(function (ret) {
214
+ const columnName = ret.columnName || ret;
215
+ returningClause += self.formatter.wrap(columnName) + ',';
216
+ intoClause += '?,';
217
+
218
+ // Add Helpers to bindings
219
+ if (ret instanceof BlobHelper) {
220
+ return self.formatter.bindings.push(ret);
221
+ }
222
+ self.formatter.bindings.push(new ReturningHelper(columnName));
223
+ });
224
+ res.sql = sql;
225
+
226
+ // Strip last comma
227
+ returningClause = returningClause.slice(0, -1);
228
+ intoClause = intoClause.slice(0, -1);
229
+ if (returningClause && intoClause) {
230
+ res.sql += ' returning ' + returningClause + ' into ' + intoClause;
231
+ }
232
+ res.outBinding = [outBinding];
233
+ if (returning[0] === '*') {
234
+ res.returningSql = function () {
235
+ return 'select * from ' + self.tableName + ' where ROWID = :1';
236
+ };
237
+ }
238
+ res.returning = returning;
239
+
240
+ return res;
241
+ }
242
+
243
+ _prepOutbindings(paramValues, paramReturning) {
244
+ const result = {};
245
+ let params = paramValues || [];
246
+ let returning = paramReturning || [];
247
+ if (!Array.isArray(params) && isPlainObject(paramValues)) {
248
+ params = [params];
249
+ }
250
+ // Always wrap returning argument in array
251
+ if (returning && !Array.isArray(returning)) {
252
+ returning = [returning];
253
+ }
254
+
255
+ const outBinding = [];
256
+ // Handle Buffer value as Blob
257
+ each(params, function (values, index) {
258
+ if (returning[0] === '*') {
259
+ outBinding[index] = ['ROWID'];
260
+ } else {
261
+ outBinding[index] = clone(returning);
262
+ }
263
+ each(values, function (value, key) {
264
+ if (value instanceof Buffer) {
265
+ values[key] = new BlobHelper(key, value);
266
+
267
+ // Delete blob duplicate in returning
268
+ const blobIndex = outBinding[index].indexOf(key);
269
+ if (blobIndex >= 0) {
270
+ outBinding[index].splice(blobIndex, 1);
271
+ values[key].returning = true;
272
+ }
273
+ outBinding[index].push(values[key]);
274
+ }
275
+ if (value === undefined) {
276
+ delete params[index][key];
277
+ }
278
+ });
279
+ });
280
+ result.returning = returning;
281
+ result.outBinding = outBinding;
282
+ result.values = params;
283
+ return result;
284
+ }
285
+
286
+ update() {
287
+ const self = this;
288
+ const sql = {};
289
+ const outBindPrep = this._prepOutbindings(
290
+ this.single.update || this.single.counter,
291
+ this.single.returning
292
+ );
293
+ const outBinding = outBindPrep.outBinding;
294
+ const returning = outBindPrep.returning;
295
+
296
+ const updates = this._prepUpdate(this.single.update);
297
+ const where = this.where();
298
+
299
+ let returningClause = '';
300
+ let intoClause = '';
301
+
302
+ if (isEmpty(updates) && typeof this.single.update !== 'function') {
303
+ return '';
304
+ }
305
+
306
+ // Build returning and into clauses
307
+ outBinding.forEach(function (out) {
308
+ out.forEach(function (ret) {
309
+ const columnName = ret.columnName || ret;
310
+ returningClause += self.formatter.wrap(columnName) + ',';
311
+ intoClause += ' ?,';
312
+
313
+ // Add Helpers to bindings
314
+ if (ret instanceof BlobHelper) {
315
+ return self.formatter.bindings.push(ret);
316
+ }
317
+ self.formatter.bindings.push(new ReturningHelper(columnName));
318
+ });
319
+ });
320
+ // Strip last comma
321
+ returningClause = returningClause.slice(0, -1);
322
+ intoClause = intoClause.slice(0, -1);
323
+
324
+ sql.outBinding = outBinding;
325
+ sql.returning = returning;
326
+ sql.sql =
327
+ 'update ' +
328
+ this.tableName +
329
+ ' set ' +
330
+ updates.join(', ') +
331
+ (where ? ' ' + where : '');
332
+ if (outBinding.length && !isEmpty(outBinding[0])) {
333
+ sql.sql += ' returning ' + returningClause + ' into' + intoClause;
334
+ }
335
+ if (returning[0] === '*') {
336
+ sql.returningSql = function () {
337
+ let sql = 'select * from ' + self.tableName;
338
+ const modifiedRowsCount = this.rowsAffected.length || this.rowsAffected;
339
+ let returningSqlIn = ' where ROWID in (';
340
+ let returningSqlOrderBy = ') order by case ROWID ';
341
+
342
+ // Needs special order by because in(...) change result order
343
+ for (let i = 0; i < modifiedRowsCount; i++) {
344
+ if (this.returning[0] === '*') {
345
+ returningSqlIn += ':' + (i + 1) + ', ';
346
+ returningSqlOrderBy +=
347
+ 'when CHARTOROWID(:' + (i + 1) + ') then ' + i + ' ';
348
+ }
349
+ }
350
+ if (this.returning[0] === '*') {
351
+ this.returning = this.returning.slice(0, -1);
352
+ returningSqlIn = returningSqlIn.slice(0, -2);
353
+ returningSqlOrderBy = returningSqlOrderBy.slice(0, -1);
354
+ }
355
+ return (sql += returningSqlIn + returningSqlOrderBy + ' end');
356
+ };
357
+ }
358
+
359
+ return sql;
360
+ }
361
+ }
362
+
363
+ module.exports = Oracledb_Compiler;