pg-promise 11.4.0 → 11.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.
- package/README.md +42 -42
- package/lib/assert.js +1 -1
- package/lib/connect.js +17 -17
- package/lib/database-pool.js +5 -5
- package/lib/database.js +37 -37
- package/lib/errors/index.js +4 -4
- package/lib/errors/parameterized-query-error.js +12 -12
- package/lib/errors/prepared-statement-error.js +13 -13
- package/lib/errors/query-file-error.js +10 -10
- package/lib/errors/query-result-error.js +13 -13
- package/lib/events.js +17 -17
- package/lib/formatting.js +69 -69
- package/lib/helpers/column-set.js +29 -29
- package/lib/helpers/column.js +39 -39
- package/lib/helpers/index.js +4 -4
- package/lib/helpers/methods/concat.js +9 -9
- package/lib/helpers/methods/index.js +5 -5
- package/lib/helpers/methods/insert.js +15 -15
- package/lib/helpers/methods/sets.js +5 -5
- package/lib/helpers/methods/update.js +22 -22
- package/lib/helpers/methods/values.js +10 -10
- package/lib/helpers/table-name.js +7 -7
- package/lib/index.js +3 -3
- package/lib/inner-state.js +3 -3
- package/lib/main.js +32 -32
- package/lib/patterns.js +1 -1
- package/lib/promise-adapter.js +12 -12
- package/lib/promise-parser.js +7 -7
- package/lib/query-file.js +21 -21
- package/lib/query.js +19 -19
- package/lib/stream.js +11 -11
- package/lib/task.js +25 -25
- package/lib/text.js +17 -17
- package/lib/tx-mode.js +14 -14
- package/lib/types/index.js +3 -3
- package/lib/types/parameterized-query.js +16 -16
- package/lib/types/prepared-statement.js +18 -18
- package/lib/types/server-formatting.js +3 -3
- package/lib/utils/color.js +1 -1
- package/lib/utils/index.js +20 -20
- package/lib/utils/public.js +26 -26
- package/package.json +5 -5
- package/typescript/pg-subset.d.ts +1 -1
|
@@ -7,15 +7,15 @@
|
|
|
7
7
|
* Removal or modification of this copyright notice is prohibited.
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
const {InnerState} = require(
|
|
11
|
-
const {assert} = require(
|
|
12
|
-
const {TableName} = require(
|
|
13
|
-
const {Column} = require(
|
|
10
|
+
const {InnerState} = require('../inner-state');
|
|
11
|
+
const {assert} = require('../assert');
|
|
12
|
+
const {TableName} = require('./table-name');
|
|
13
|
+
const {Column} = require('./column');
|
|
14
14
|
|
|
15
15
|
const npm = {
|
|
16
|
-
os: require(
|
|
17
|
-
utils: require(
|
|
18
|
-
formatting: require(
|
|
16
|
+
os: require('os'),
|
|
17
|
+
utils: require('../utils'),
|
|
18
|
+
formatting: require('../formatting')
|
|
19
19
|
};
|
|
20
20
|
|
|
21
21
|
/**
|
|
@@ -168,11 +168,11 @@ class ColumnSet extends InnerState {
|
|
|
168
168
|
constructor(columns, opt) {
|
|
169
169
|
super();
|
|
170
170
|
|
|
171
|
-
if (!columns || typeof columns !==
|
|
172
|
-
throw new TypeError(
|
|
171
|
+
if (!columns || typeof columns !== 'object') {
|
|
172
|
+
throw new TypeError('Invalid parameter \'columns\' specified.');
|
|
173
173
|
}
|
|
174
174
|
|
|
175
|
-
opt = assert(opt, [
|
|
175
|
+
opt = assert(opt, ['table', 'inherit']);
|
|
176
176
|
|
|
177
177
|
if (!npm.utils.isNull(opt.table)) {
|
|
178
178
|
this.table = (opt.table instanceof TableName) ? opt.table : new TableName(opt.table);
|
|
@@ -234,7 +234,7 @@ class ColumnSet extends InnerState {
|
|
|
234
234
|
const c = this.columns[i];
|
|
235
235
|
// ColumnSet is simple when the source objects require no preparation,
|
|
236
236
|
// and should be used directly:
|
|
237
|
-
if (c.prop || c.init ||
|
|
237
|
+
if (c.prop || c.init || 'def' in c) {
|
|
238
238
|
this._inner.isSimple = false;
|
|
239
239
|
break;
|
|
240
240
|
}
|
|
@@ -329,12 +329,12 @@ class ColumnSet extends InnerState {
|
|
|
329
329
|
*/
|
|
330
330
|
ColumnSet.prototype.assign = function (options) {
|
|
331
331
|
const _i = this._inner;
|
|
332
|
-
const hasPrefix = options && options.prefix && typeof options.prefix ===
|
|
332
|
+
const hasPrefix = options && options.prefix && typeof options.prefix === 'string';
|
|
333
333
|
if (_i.updates && !hasPrefix) {
|
|
334
334
|
return _i.updates;
|
|
335
335
|
}
|
|
336
336
|
let dynamic = hasPrefix;
|
|
337
|
-
const hasSource = options && options.source && typeof options.source ===
|
|
337
|
+
const hasSource = options && options.source && typeof options.source === 'object';
|
|
338
338
|
let list = this.columns.filter(c => {
|
|
339
339
|
if (c.cnd) {
|
|
340
340
|
return false;
|
|
@@ -351,8 +351,8 @@ ColumnSet.prototype.assign = function (options) {
|
|
|
351
351
|
return true;
|
|
352
352
|
});
|
|
353
353
|
|
|
354
|
-
const prefix = hasPrefix ? npm.formatting.as.alias(options.prefix) +
|
|
355
|
-
list = list.map(c => prefix + c.escapedName +
|
|
354
|
+
const prefix = hasPrefix ? npm.formatting.as.alias(options.prefix) + '.' : '';
|
|
355
|
+
list = list.map(c => prefix + c.escapedName + '=' + c.variable + c.castText).join();
|
|
356
356
|
|
|
357
357
|
if (!dynamic) {
|
|
358
358
|
_i.updates = list;
|
|
@@ -400,13 +400,13 @@ ColumnSet.prototype.assign = function (options) {
|
|
|
400
400
|
*
|
|
401
401
|
*/
|
|
402
402
|
ColumnSet.prototype.assignColumns = function (options) {
|
|
403
|
-
options = assert(options, [
|
|
404
|
-
const skip = (typeof options.skip ===
|
|
405
|
-
const from = (typeof options.from ===
|
|
406
|
-
const to = (typeof options.to ===
|
|
407
|
-
const iterator = typeof skip ===
|
|
403
|
+
options = assert(options, ['from', 'to', 'skip']);
|
|
404
|
+
const skip = (typeof options.skip === 'string' && [options.skip]) || ((Array.isArray(options.skip) || typeof options.skip === 'function') && options.skip);
|
|
405
|
+
const from = (typeof options.from === 'string' && options.from && (npm.formatting.as.alias(options.from) + '.')) || '';
|
|
406
|
+
const to = (typeof options.to === 'string' && options.to && (npm.formatting.as.alias(options.to) + '.')) || '';
|
|
407
|
+
const iterator = typeof skip === 'function' ? c => !skip.call(c, c) : c => skip.indexOf(c.name) === -1;
|
|
408
408
|
const cols = skip ? this.columns.filter(iterator) : this.columns;
|
|
409
|
-
return cols.map(c => to + c.escapedName +
|
|
409
|
+
return cols.map(c => to + c.escapedName + '=' + from + c.escapedName).join();
|
|
410
410
|
};
|
|
411
411
|
|
|
412
412
|
/**
|
|
@@ -583,7 +583,7 @@ ColumnSet.prototype.prepare = function (source) {
|
|
|
583
583
|
if (c.init) {
|
|
584
584
|
target[a.name] = c.init.call(source, a);
|
|
585
585
|
} else {
|
|
586
|
-
if (a.exists ||
|
|
586
|
+
if (a.exists || 'def' in c) {
|
|
587
587
|
target[a.name] = a.value;
|
|
588
588
|
}
|
|
589
589
|
}
|
|
@@ -600,7 +600,7 @@ function colDesc(column, source) {
|
|
|
600
600
|
if (a.exists) {
|
|
601
601
|
a.value = source[a.name];
|
|
602
602
|
} else {
|
|
603
|
-
a.value =
|
|
603
|
+
a.value = 'def' in column ? column.def : undefined;
|
|
604
604
|
}
|
|
605
605
|
return a;
|
|
606
606
|
}
|
|
@@ -622,21 +622,21 @@ ColumnSet.prototype.toString = function (level) {
|
|
|
622
622
|
const gap0 = npm.utils.messageGap(level),
|
|
623
623
|
gap1 = npm.utils.messageGap(level + 1),
|
|
624
624
|
lines = [
|
|
625
|
-
|
|
625
|
+
'ColumnSet {'
|
|
626
626
|
];
|
|
627
627
|
if (this.table) {
|
|
628
|
-
lines.push(gap1 +
|
|
628
|
+
lines.push(gap1 + 'table: ' + this.table);
|
|
629
629
|
}
|
|
630
630
|
if (this.columns.length) {
|
|
631
|
-
lines.push(gap1 +
|
|
631
|
+
lines.push(gap1 + 'columns: [');
|
|
632
632
|
this.columns.forEach(c => {
|
|
633
633
|
lines.push(c.toString(2));
|
|
634
634
|
});
|
|
635
|
-
lines.push(gap1 +
|
|
635
|
+
lines.push(gap1 + ']');
|
|
636
636
|
} else {
|
|
637
|
-
lines.push(gap1 +
|
|
637
|
+
lines.push(gap1 + 'columns: []');
|
|
638
638
|
}
|
|
639
|
-
lines.push(gap0 +
|
|
639
|
+
lines.push(gap0 + '}');
|
|
640
640
|
return lines.join(npm.os.EOL);
|
|
641
641
|
};
|
|
642
642
|
|
package/lib/helpers/column.js
CHANGED
|
@@ -7,14 +7,14 @@
|
|
|
7
7
|
* Removal or modification of this copyright notice is prohibited.
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
const {InnerState} = require(
|
|
11
|
-
const {assert} = require(
|
|
10
|
+
const {InnerState} = require('../inner-state');
|
|
11
|
+
const {assert} = require('../assert');
|
|
12
12
|
|
|
13
13
|
const npm = {
|
|
14
|
-
os: require(
|
|
15
|
-
utils: require(
|
|
16
|
-
formatting: require(
|
|
17
|
-
patterns: require(
|
|
14
|
+
os: require('os'),
|
|
15
|
+
utils: require('../utils'),
|
|
16
|
+
formatting: require('../formatting'),
|
|
17
|
+
patterns: require('../patterns')
|
|
18
18
|
};
|
|
19
19
|
|
|
20
20
|
/**
|
|
@@ -132,18 +132,18 @@ class Column extends InnerState {
|
|
|
132
132
|
constructor(col) {
|
|
133
133
|
super();
|
|
134
134
|
|
|
135
|
-
if (typeof col ===
|
|
135
|
+
if (typeof col === 'string') {
|
|
136
136
|
const info = parseColumn(col);
|
|
137
137
|
this.name = info.name;
|
|
138
|
-
if (
|
|
138
|
+
if ('mod' in info) {
|
|
139
139
|
this.mod = info.mod;
|
|
140
140
|
}
|
|
141
|
-
if (
|
|
141
|
+
if ('cnd' in info) {
|
|
142
142
|
this.cnd = info.cnd;
|
|
143
143
|
}
|
|
144
144
|
} else {
|
|
145
|
-
col = assert(col, [
|
|
146
|
-
if (
|
|
145
|
+
col = assert(col, ['name', 'prop', 'mod', 'cast', 'cnd', 'def', 'init', 'skip']);
|
|
146
|
+
if ('name' in col) {
|
|
147
147
|
if (!npm.utils.isText(col.name)) {
|
|
148
148
|
throw new TypeError(`Invalid 'name' value: ${npm.utils.toJson(col.name)}. A non-empty string was expected.`);
|
|
149
149
|
}
|
|
@@ -165,7 +165,7 @@ class Column extends InnerState {
|
|
|
165
165
|
}
|
|
166
166
|
}
|
|
167
167
|
if (!npm.utils.isNull(col.mod)) {
|
|
168
|
-
if (typeof col.mod !==
|
|
168
|
+
if (typeof col.mod !== 'string' || !isValidMod(col.mod)) {
|
|
169
169
|
throw new TypeError(`Invalid 'mod' value: ${npm.utils.toJson(col.mod)}.`);
|
|
170
170
|
}
|
|
171
171
|
this.mod = col.mod; // optional format modifier;
|
|
@@ -173,25 +173,25 @@ class Column extends InnerState {
|
|
|
173
173
|
if (!npm.utils.isNull(col.cast)) {
|
|
174
174
|
this.cast = parseCast(col.cast); // optional SQL type casting
|
|
175
175
|
}
|
|
176
|
-
if (
|
|
176
|
+
if ('cnd' in col) {
|
|
177
177
|
this.cnd = !!col.cnd;
|
|
178
178
|
}
|
|
179
|
-
if (
|
|
179
|
+
if ('def' in col) {
|
|
180
180
|
this.def = col.def; // optional default
|
|
181
181
|
}
|
|
182
|
-
if (typeof col.init ===
|
|
182
|
+
if (typeof col.init === 'function') {
|
|
183
183
|
this.init = col.init; // optional value override (overrides 'def' also)
|
|
184
184
|
}
|
|
185
|
-
if (typeof col.skip ===
|
|
185
|
+
if (typeof col.skip === 'function') {
|
|
186
186
|
this.skip = col.skip;
|
|
187
187
|
}
|
|
188
188
|
} else {
|
|
189
|
-
throw new TypeError(
|
|
189
|
+
throw new TypeError('Invalid column details.');
|
|
190
190
|
}
|
|
191
191
|
}
|
|
192
192
|
|
|
193
|
-
const variable =
|
|
194
|
-
const castText = this.cast ? (
|
|
193
|
+
const variable = '${' + (this.prop || this.name) + (this.mod || '') + '}';
|
|
194
|
+
const castText = this.cast ? ('::' + this.cast) : '';
|
|
195
195
|
const escapedName = npm.formatting.as.name(this.name);
|
|
196
196
|
|
|
197
197
|
this.extendState({variable, castText, escapedName});
|
|
@@ -251,8 +251,8 @@ class Column extends InnerState {
|
|
|
251
251
|
}
|
|
252
252
|
|
|
253
253
|
function parseCast(name) {
|
|
254
|
-
if (typeof name ===
|
|
255
|
-
const s = name.replace(/^[:\s]*|\s*$/g,
|
|
254
|
+
if (typeof name === 'string') {
|
|
255
|
+
const s = name.replace(/^[:\s]*|\s*$/g, '');
|
|
256
256
|
if (s) {
|
|
257
257
|
return s;
|
|
258
258
|
}
|
|
@@ -264,7 +264,7 @@ function parseColumn(name) {
|
|
|
264
264
|
const m = name.match(npm.patterns.validColumn);
|
|
265
265
|
if (m && m[0] === name) {
|
|
266
266
|
const res = {};
|
|
267
|
-
if (name[0] ===
|
|
267
|
+
if (name[0] === '?') {
|
|
268
268
|
res.cnd = true;
|
|
269
269
|
name = name.substr(1);
|
|
270
270
|
}
|
|
@@ -306,31 +306,31 @@ Column.prototype.toString = function (level) {
|
|
|
306
306
|
const gap0 = npm.utils.messageGap(level),
|
|
307
307
|
gap1 = npm.utils.messageGap(level + 1),
|
|
308
308
|
lines = [
|
|
309
|
-
gap0 +
|
|
310
|
-
gap1 +
|
|
309
|
+
gap0 + 'Column {',
|
|
310
|
+
gap1 + 'name: ' + npm.utils.toJson(this.name)
|
|
311
311
|
];
|
|
312
|
-
if (
|
|
313
|
-
lines.push(gap1 +
|
|
312
|
+
if ('prop' in this) {
|
|
313
|
+
lines.push(gap1 + 'prop: ' + npm.utils.toJson(this.prop));
|
|
314
314
|
}
|
|
315
|
-
if (
|
|
316
|
-
lines.push(gap1 +
|
|
315
|
+
if ('mod' in this) {
|
|
316
|
+
lines.push(gap1 + 'mod: ' + npm.utils.toJson(this.mod));
|
|
317
317
|
}
|
|
318
|
-
if (
|
|
319
|
-
lines.push(gap1 +
|
|
318
|
+
if ('cast' in this) {
|
|
319
|
+
lines.push(gap1 + 'cast: ' + npm.utils.toJson(this.cast));
|
|
320
320
|
}
|
|
321
|
-
if (
|
|
322
|
-
lines.push(gap1 +
|
|
321
|
+
if ('cnd' in this) {
|
|
322
|
+
lines.push(gap1 + 'cnd: ' + npm.utils.toJson(this.cnd));
|
|
323
323
|
}
|
|
324
|
-
if (
|
|
325
|
-
lines.push(gap1 +
|
|
324
|
+
if ('def' in this) {
|
|
325
|
+
lines.push(gap1 + 'def: ' + npm.utils.toJson(this.def));
|
|
326
326
|
}
|
|
327
|
-
if (
|
|
328
|
-
lines.push(gap1 +
|
|
327
|
+
if ('init' in this) {
|
|
328
|
+
lines.push(gap1 + 'init: [Function]');
|
|
329
329
|
}
|
|
330
|
-
if (
|
|
331
|
-
lines.push(gap1 +
|
|
330
|
+
if ('skip' in this) {
|
|
331
|
+
lines.push(gap1 + 'skip: [Function]');
|
|
332
332
|
}
|
|
333
|
-
lines.push(gap0 +
|
|
333
|
+
lines.push(gap0 + '}');
|
|
334
334
|
return lines.join(npm.os.EOL);
|
|
335
335
|
};
|
|
336
336
|
|
package/lib/helpers/index.js
CHANGED
|
@@ -7,10 +7,10 @@
|
|
|
7
7
|
* Removal or modification of this copyright notice is prohibited.
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
const {Column} = require(
|
|
11
|
-
const {ColumnSet} = require(
|
|
12
|
-
const {TableName} = require(
|
|
13
|
-
const method = require(
|
|
10
|
+
const {Column} = require('./column');
|
|
11
|
+
const {ColumnSet} = require('./column-set');
|
|
12
|
+
const {TableName} = require('./table-name');
|
|
13
|
+
const method = require('./methods');
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
16
|
* @namespace helpers
|
|
@@ -7,10 +7,10 @@
|
|
|
7
7
|
* Removal or modification of this copyright notice is prohibited.
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
const {QueryFile} = require(
|
|
10
|
+
const {QueryFile} = require('../../query-file');
|
|
11
11
|
|
|
12
12
|
const npm = {
|
|
13
|
-
formatting: require(
|
|
13
|
+
formatting: require('../../formatting')
|
|
14
14
|
};
|
|
15
15
|
|
|
16
16
|
/**
|
|
@@ -50,22 +50,22 @@ const npm = {
|
|
|
50
50
|
*/
|
|
51
51
|
function concat(queries, capSQL) {
|
|
52
52
|
if (!Array.isArray(queries)) {
|
|
53
|
-
throw new TypeError(
|
|
53
|
+
throw new TypeError('Parameter \'queries\' must be an array.');
|
|
54
54
|
}
|
|
55
55
|
const fmOptions = {capSQL};
|
|
56
56
|
const all = queries.map((q, index) => {
|
|
57
|
-
if (typeof q ===
|
|
57
|
+
if (typeof q === 'string') {
|
|
58
58
|
// a simple query string without parameters:
|
|
59
59
|
return clean(q);
|
|
60
60
|
}
|
|
61
|
-
if (q && typeof q ===
|
|
61
|
+
if (q && typeof q === 'object') {
|
|
62
62
|
if (q instanceof QueryFile) {
|
|
63
63
|
// QueryFile object:
|
|
64
64
|
return clean(q[npm.formatting.as.ctf.toPostgres]());
|
|
65
65
|
}
|
|
66
|
-
if (
|
|
66
|
+
if ('query' in q) {
|
|
67
67
|
// object {query, values, options}:
|
|
68
|
-
let opt = q.options && typeof q.options ===
|
|
68
|
+
let opt = q.options && typeof q.options === 'object' ? q.options : {};
|
|
69
69
|
opt = opt.capSQL === undefined ? Object.assign(opt, fmOptions) : opt;
|
|
70
70
|
return clean(npm.formatting.as.format(q.query, q.values, opt));
|
|
71
71
|
}
|
|
@@ -73,12 +73,12 @@ function concat(queries, capSQL) {
|
|
|
73
73
|
throw new Error(`Invalid query element at index ${index}.`);
|
|
74
74
|
});
|
|
75
75
|
|
|
76
|
-
return all.filter(q => q).join(
|
|
76
|
+
return all.filter(q => q).join(';');
|
|
77
77
|
}
|
|
78
78
|
|
|
79
79
|
function clean(q) {
|
|
80
80
|
// removes from the query all leading and trailing symbols ' ', '\t' and ';'
|
|
81
|
-
return q.replace(/^[\s;]*|[\s;]*$/g,
|
|
81
|
+
return q.replace(/^[\s;]*|[\s;]*$/g, '');
|
|
82
82
|
}
|
|
83
83
|
|
|
84
84
|
module.exports = {concat};
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
const {concat} = require(
|
|
2
|
-
const {insert} = require(
|
|
3
|
-
const {update} = require(
|
|
4
|
-
const {values} = require(
|
|
5
|
-
const {sets} = require(
|
|
1
|
+
const {concat} = require('./concat');
|
|
2
|
+
const {insert} = require('./insert');
|
|
3
|
+
const {update} = require('./update');
|
|
4
|
+
const {values} = require('./values');
|
|
5
|
+
const {sets} = require('./sets');
|
|
6
6
|
|
|
7
7
|
module.exports = {
|
|
8
8
|
concat,
|
|
@@ -7,12 +7,12 @@
|
|
|
7
7
|
* Removal or modification of this copyright notice is prohibited.
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
const {TableName} = require(
|
|
11
|
-
const {ColumnSet} = require(
|
|
10
|
+
const {TableName} = require('../table-name');
|
|
11
|
+
const {ColumnSet} = require('../column-set');
|
|
12
12
|
|
|
13
13
|
const npm = {
|
|
14
|
-
formatting: require(
|
|
15
|
-
utils: require(
|
|
14
|
+
formatting: require('../../formatting'),
|
|
15
|
+
utils: require('../../utils')
|
|
16
16
|
};
|
|
17
17
|
|
|
18
18
|
/**
|
|
@@ -90,14 +90,14 @@ const npm = {
|
|
|
90
90
|
*/
|
|
91
91
|
function insert(data, columns, table, capSQL) {
|
|
92
92
|
|
|
93
|
-
if (!data || typeof data !==
|
|
94
|
-
throw new TypeError(
|
|
93
|
+
if (!data || typeof data !== 'object') {
|
|
94
|
+
throw new TypeError('Invalid parameter \'data\' specified.');
|
|
95
95
|
}
|
|
96
96
|
|
|
97
97
|
const isArray = Array.isArray(data);
|
|
98
98
|
|
|
99
99
|
if (isArray && !data.length) {
|
|
100
|
-
throw new TypeError(
|
|
100
|
+
throw new TypeError('Cannot generate an INSERT from an empty array.');
|
|
101
101
|
}
|
|
102
102
|
|
|
103
103
|
if (columns instanceof ColumnSet) {
|
|
@@ -106,17 +106,17 @@ function insert(data, columns, table, capSQL) {
|
|
|
106
106
|
}
|
|
107
107
|
} else {
|
|
108
108
|
if (isArray && npm.utils.isNull(columns)) {
|
|
109
|
-
throw new TypeError(
|
|
109
|
+
throw new TypeError('Parameter \'columns\' is required when inserting multiple records.');
|
|
110
110
|
}
|
|
111
111
|
columns = new ColumnSet(columns || data);
|
|
112
112
|
}
|
|
113
113
|
|
|
114
114
|
if (!columns.columns.length) {
|
|
115
|
-
throw new Error(
|
|
115
|
+
throw new Error('Cannot generate an INSERT without any columns.');
|
|
116
116
|
}
|
|
117
117
|
|
|
118
118
|
if (!table) {
|
|
119
|
-
throw new Error(
|
|
119
|
+
throw new Error('Table name is unknown.');
|
|
120
120
|
}
|
|
121
121
|
|
|
122
122
|
if (!(table instanceof TableName)) {
|
|
@@ -131,18 +131,18 @@ function insert(data, columns, table, capSQL) {
|
|
|
131
131
|
|
|
132
132
|
if (isArray) {
|
|
133
133
|
return query + data.map((d, index) => {
|
|
134
|
-
if (!d || typeof d !==
|
|
134
|
+
if (!d || typeof d !== 'object') {
|
|
135
135
|
throw new Error(`Invalid insert object at index ${index}.`);
|
|
136
136
|
}
|
|
137
|
-
return
|
|
137
|
+
return '(' + format(columns.variables, columns.prepare(d), fmOptions) + ')';
|
|
138
138
|
}).join();
|
|
139
139
|
}
|
|
140
|
-
return query +
|
|
140
|
+
return query + '(' + format(columns.variables, columns.prepare(data), fmOptions) + ')';
|
|
141
141
|
}
|
|
142
142
|
|
|
143
143
|
const sql = {
|
|
144
|
-
lowCase:
|
|
145
|
-
capCase:
|
|
144
|
+
lowCase: 'insert into $1^($2^) values',
|
|
145
|
+
capCase: 'INSERT INTO $1^($2^) VALUES'
|
|
146
146
|
};
|
|
147
147
|
|
|
148
148
|
module.exports = {insert};
|
|
@@ -7,11 +7,11 @@
|
|
|
7
7
|
* Removal or modification of this copyright notice is prohibited.
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
const {ColumnSet} = require(
|
|
10
|
+
const {ColumnSet} = require('../column-set');
|
|
11
11
|
|
|
12
12
|
const npm = {
|
|
13
|
-
format: require(
|
|
14
|
-
utils: require(
|
|
13
|
+
format: require('../../formatting').as.format,
|
|
14
|
+
utils: require('../../utils')
|
|
15
15
|
};
|
|
16
16
|
|
|
17
17
|
/**
|
|
@@ -66,8 +66,8 @@ const npm = {
|
|
|
66
66
|
*/
|
|
67
67
|
function sets(data, columns, capSQL) {
|
|
68
68
|
|
|
69
|
-
if (!data || typeof data !==
|
|
70
|
-
throw new TypeError(
|
|
69
|
+
if (!data || typeof data !== 'object' || Array.isArray(data)) {
|
|
70
|
+
throw new TypeError('Invalid parameter \'data\' specified.');
|
|
71
71
|
}
|
|
72
72
|
|
|
73
73
|
if (!(columns instanceof ColumnSet)) {
|
|
@@ -7,13 +7,13 @@
|
|
|
7
7
|
* Removal or modification of this copyright notice is prohibited.
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
const {assert} = require(
|
|
11
|
-
const {TableName} = require(
|
|
12
|
-
const {ColumnSet} = require(
|
|
10
|
+
const {assert} = require('../../assert');
|
|
11
|
+
const {TableName} = require('../table-name');
|
|
12
|
+
const {ColumnSet} = require('../column-set');
|
|
13
13
|
|
|
14
14
|
const npm = {
|
|
15
|
-
formatting: require(
|
|
16
|
-
utils: require(
|
|
15
|
+
formatting: require('../../formatting'),
|
|
16
|
+
utils: require('../../utils')
|
|
17
17
|
};
|
|
18
18
|
|
|
19
19
|
/**
|
|
@@ -147,14 +147,14 @@ const npm = {
|
|
|
147
147
|
*/
|
|
148
148
|
function update(data, columns, table, options, capSQL) {
|
|
149
149
|
|
|
150
|
-
if (!data || typeof data !==
|
|
151
|
-
throw new TypeError(
|
|
150
|
+
if (!data || typeof data !== 'object') {
|
|
151
|
+
throw new TypeError('Invalid parameter \'data\' specified.');
|
|
152
152
|
}
|
|
153
153
|
|
|
154
154
|
const isArray = Array.isArray(data);
|
|
155
155
|
|
|
156
156
|
if (isArray && !data.length) {
|
|
157
|
-
throw new TypeError(
|
|
157
|
+
throw new TypeError('Cannot generate an UPDATE from an empty array.');
|
|
158
158
|
}
|
|
159
159
|
|
|
160
160
|
if (columns instanceof ColumnSet) {
|
|
@@ -163,20 +163,20 @@ function update(data, columns, table, options, capSQL) {
|
|
|
163
163
|
}
|
|
164
164
|
} else {
|
|
165
165
|
if (isArray && npm.utils.isNull(columns)) {
|
|
166
|
-
throw new TypeError(
|
|
166
|
+
throw new TypeError('Parameter \'columns\' is required when updating multiple records.');
|
|
167
167
|
}
|
|
168
168
|
columns = new ColumnSet(columns || data);
|
|
169
169
|
}
|
|
170
170
|
|
|
171
|
-
options = assert(options, [
|
|
171
|
+
options = assert(options, ['tableAlias', 'valueAlias', 'emptyUpdate']);
|
|
172
172
|
|
|
173
173
|
const format = npm.formatting.as.format,
|
|
174
|
-
useEmptyUpdate =
|
|
174
|
+
useEmptyUpdate = 'emptyUpdate' in options,
|
|
175
175
|
fmOptions = {capSQL};
|
|
176
176
|
|
|
177
177
|
if (isArray) {
|
|
178
|
-
const tableAlias = npm.formatting.as.alias(npm.utils.isNull(options.tableAlias) ?
|
|
179
|
-
const valueAlias = npm.formatting.as.alias(npm.utils.isNull(options.valueAlias) ?
|
|
178
|
+
const tableAlias = npm.formatting.as.alias(npm.utils.isNull(options.tableAlias) ? 't' : options.tableAlias);
|
|
179
|
+
const valueAlias = npm.formatting.as.alias(npm.utils.isNull(options.valueAlias) ? 'v' : options.valueAlias);
|
|
180
180
|
|
|
181
181
|
const q = capSQL ? sql.multi.capCase : sql.multi.lowCase;
|
|
182
182
|
|
|
@@ -188,13 +188,13 @@ function update(data, columns, table, options, capSQL) {
|
|
|
188
188
|
|
|
189
189
|
checkTable();
|
|
190
190
|
|
|
191
|
-
const targetCols = actualColumns.map(c => c.escapedName +
|
|
191
|
+
const targetCols = actualColumns.map(c => c.escapedName + '=' + valueAlias + '.' + c.escapedName).join();
|
|
192
192
|
|
|
193
193
|
const values = data.map((d, index) => {
|
|
194
|
-
if (!d || typeof d !==
|
|
194
|
+
if (!d || typeof d !== 'object') {
|
|
195
195
|
throw new Error(`Invalid update object at index ${index}.`);
|
|
196
196
|
}
|
|
197
|
-
return
|
|
197
|
+
return '(' + format(columns.variables, columns.prepare(d), fmOptions) + ')';
|
|
198
198
|
}).join();
|
|
199
199
|
|
|
200
200
|
return format(q, [table.name, tableAlias, targetCols, values, valueAlias, columns.names], fmOptions);
|
|
@@ -217,7 +217,7 @@ function update(data, columns, table, options, capSQL) {
|
|
|
217
217
|
table = new TableName(table);
|
|
218
218
|
}
|
|
219
219
|
if (!table) {
|
|
220
|
-
throw new Error(
|
|
220
|
+
throw new Error('Table name is unknown.');
|
|
221
221
|
}
|
|
222
222
|
}
|
|
223
223
|
|
|
@@ -226,19 +226,19 @@ function update(data, columns, table, options, capSQL) {
|
|
|
226
226
|
if (useEmptyUpdate) {
|
|
227
227
|
return true;
|
|
228
228
|
}
|
|
229
|
-
throw new Error(
|
|
229
|
+
throw new Error('Cannot generate an UPDATE without any columns.');
|
|
230
230
|
}
|
|
231
231
|
}
|
|
232
232
|
}
|
|
233
233
|
|
|
234
234
|
const sql = {
|
|
235
235
|
single: {
|
|
236
|
-
lowCase:
|
|
237
|
-
capCase:
|
|
236
|
+
lowCase: 'update $1^ set ',
|
|
237
|
+
capCase: 'UPDATE $1^ SET '
|
|
238
238
|
},
|
|
239
239
|
multi: {
|
|
240
|
-
lowCase:
|
|
241
|
-
capCase:
|
|
240
|
+
lowCase: 'update $1^ as $2^ set $3^ from (values$4^) as $5^($6^)',
|
|
241
|
+
capCase: 'UPDATE $1^ AS $2^ SET $3^ FROM (VALUES$4^) AS $5^($6^)'
|
|
242
242
|
}
|
|
243
243
|
};
|
|
244
244
|
|
|
@@ -7,11 +7,11 @@
|
|
|
7
7
|
* Removal or modification of this copyright notice is prohibited.
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
const {ColumnSet} = require(
|
|
10
|
+
const {ColumnSet} = require('../column-set');
|
|
11
11
|
|
|
12
12
|
const npm = {
|
|
13
|
-
formatting: require(
|
|
14
|
-
utils: require(
|
|
13
|
+
formatting: require('../../formatting'),
|
|
14
|
+
utils: require('../../utils')
|
|
15
15
|
};
|
|
16
16
|
|
|
17
17
|
/**
|
|
@@ -81,21 +81,21 @@ const npm = {
|
|
|
81
81
|
*/
|
|
82
82
|
function values(data, columns, capSQL) {
|
|
83
83
|
|
|
84
|
-
if (!data || typeof data !==
|
|
85
|
-
throw new TypeError(
|
|
84
|
+
if (!data || typeof data !== 'object') {
|
|
85
|
+
throw new TypeError('Invalid parameter \'data\' specified.');
|
|
86
86
|
}
|
|
87
87
|
|
|
88
88
|
const isArray = Array.isArray(data);
|
|
89
89
|
|
|
90
90
|
if (!(columns instanceof ColumnSet)) {
|
|
91
91
|
if (isArray && npm.utils.isNull(columns)) {
|
|
92
|
-
throw new TypeError(
|
|
92
|
+
throw new TypeError('Parameter \'columns\' is required when generating multi-row values.');
|
|
93
93
|
}
|
|
94
94
|
columns = new ColumnSet(columns || data);
|
|
95
95
|
}
|
|
96
96
|
|
|
97
97
|
if (!columns.columns.length) {
|
|
98
|
-
throw new Error(
|
|
98
|
+
throw new Error('Cannot generate values without any columns.');
|
|
99
99
|
}
|
|
100
100
|
|
|
101
101
|
const format = npm.formatting.as.format,
|
|
@@ -103,13 +103,13 @@ function values(data, columns, capSQL) {
|
|
|
103
103
|
|
|
104
104
|
if (isArray) {
|
|
105
105
|
return data.map((d, index) => {
|
|
106
|
-
if (!d || typeof d !==
|
|
106
|
+
if (!d || typeof d !== 'object') {
|
|
107
107
|
throw new Error(`Invalid object at index ${index}.`);
|
|
108
108
|
}
|
|
109
|
-
return
|
|
109
|
+
return '(' + format(columns.variables, columns.prepare(d), fmOptions) + ')';
|
|
110
110
|
}).join();
|
|
111
111
|
}
|
|
112
|
-
return
|
|
112
|
+
return '(' + format(columns.variables, columns.prepare(data), fmOptions) + ')';
|
|
113
113
|
}
|
|
114
114
|
|
|
115
115
|
module.exports = {values};
|