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
|
@@ -8,9 +8,9 @@
|
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
10
|
const npm = {
|
|
11
|
-
os: require(
|
|
12
|
-
utils: require(
|
|
13
|
-
text: require(
|
|
11
|
+
os: require('os'),
|
|
12
|
+
utils: require('../utils'),
|
|
13
|
+
text: require('../text')
|
|
14
14
|
};
|
|
15
15
|
|
|
16
16
|
/**
|
|
@@ -36,9 +36,9 @@ const queryResultErrorCode = {
|
|
|
36
36
|
};
|
|
37
37
|
|
|
38
38
|
const errorMessages = [
|
|
39
|
-
{name:
|
|
40
|
-
{name:
|
|
41
|
-
{name:
|
|
39
|
+
{name: 'noData', message: npm.text.noData},
|
|
40
|
+
{name: 'notEmpty', message: npm.text.notEmpty},
|
|
41
|
+
{name: 'multiple', message: npm.text.multiple}
|
|
42
42
|
];
|
|
43
43
|
|
|
44
44
|
/**
|
|
@@ -154,16 +154,16 @@ QueryResultError.prototype.toString = function (level) {
|
|
|
154
154
|
const gap0 = npm.utils.messageGap(level),
|
|
155
155
|
gap1 = npm.utils.messageGap(level + 1),
|
|
156
156
|
lines = [
|
|
157
|
-
|
|
158
|
-
gap1 +
|
|
159
|
-
gap1 +
|
|
160
|
-
gap1 +
|
|
161
|
-
gap1 +
|
|
157
|
+
'QueryResultError {',
|
|
158
|
+
gap1 + 'code: queryResultErrorCode.' + errorMessages[this.code].name,
|
|
159
|
+
gap1 + 'message: "' + this.message + '"',
|
|
160
|
+
gap1 + 'received: ' + this.received,
|
|
161
|
+
gap1 + 'query: ' + (typeof this.query === 'string' ? '"' + this.query + '"' : npm.utils.toJson(this.query))
|
|
162
162
|
];
|
|
163
163
|
if (this.values !== undefined) {
|
|
164
|
-
lines.push(gap1 +
|
|
164
|
+
lines.push(gap1 + 'values: ' + npm.utils.toJson(this.values));
|
|
165
165
|
}
|
|
166
|
-
lines.push(gap0 +
|
|
166
|
+
lines.push(gap0 + '}');
|
|
167
167
|
return lines.join(npm.os.EOL);
|
|
168
168
|
};
|
|
169
169
|
|
package/lib/events.js
CHANGED
|
@@ -7,11 +7,11 @@
|
|
|
7
7
|
* Removal or modification of this copyright notice is prohibited.
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
const {ColorConsole} = require(
|
|
10
|
+
const {ColorConsole} = require('./utils/color');
|
|
11
11
|
|
|
12
12
|
const npm = {
|
|
13
|
-
main: require(
|
|
14
|
-
utils: require(
|
|
13
|
+
main: require('./'),
|
|
14
|
+
utils: require('./utils')
|
|
15
15
|
};
|
|
16
16
|
|
|
17
17
|
/////////////////////////////////
|
|
@@ -57,14 +57,14 @@ class Events {
|
|
|
57
57
|
* };
|
|
58
58
|
*/
|
|
59
59
|
static connect(ctx, client, useCount) {
|
|
60
|
-
if (typeof ctx.options.connect ===
|
|
60
|
+
if (typeof ctx.options.connect === 'function') {
|
|
61
61
|
try {
|
|
62
62
|
ctx.options.connect({client, dc: ctx.dc, useCount});
|
|
63
63
|
} catch (e) {
|
|
64
64
|
// have to silence errors here;
|
|
65
65
|
// cannot allow unhandled errors while connecting to the database,
|
|
66
66
|
// as it will break the connection logic;
|
|
67
|
-
Events.unexpected(
|
|
67
|
+
Events.unexpected('connect', e);
|
|
68
68
|
}
|
|
69
69
|
}
|
|
70
70
|
}
|
|
@@ -99,14 +99,14 @@ class Events {
|
|
|
99
99
|
* };
|
|
100
100
|
*/
|
|
101
101
|
static disconnect(ctx, client) {
|
|
102
|
-
if (typeof ctx.options.disconnect ===
|
|
102
|
+
if (typeof ctx.options.disconnect === 'function') {
|
|
103
103
|
try {
|
|
104
104
|
ctx.options.disconnect({client, dc: ctx.dc});
|
|
105
105
|
} catch (e) {
|
|
106
106
|
// have to silence errors here;
|
|
107
107
|
// cannot allow unhandled errors while disconnecting from the database,
|
|
108
108
|
// as it will break the disconnection logic;
|
|
109
|
-
Events.unexpected(
|
|
109
|
+
Events.unexpected('disconnect', e);
|
|
110
110
|
}
|
|
111
111
|
}
|
|
112
112
|
}
|
|
@@ -135,7 +135,7 @@ class Events {
|
|
|
135
135
|
* };
|
|
136
136
|
*/
|
|
137
137
|
static query(options, context) {
|
|
138
|
-
if (typeof options.query ===
|
|
138
|
+
if (typeof options.query === 'function') {
|
|
139
139
|
try {
|
|
140
140
|
options.query(context);
|
|
141
141
|
} catch (e) {
|
|
@@ -210,7 +210,7 @@ class Events {
|
|
|
210
210
|
* }
|
|
211
211
|
*/
|
|
212
212
|
static receive(options, data, result, ctx) {
|
|
213
|
-
if (typeof options.receive ===
|
|
213
|
+
if (typeof options.receive === 'function') {
|
|
214
214
|
try {
|
|
215
215
|
options.receive({data, result, ctx});
|
|
216
216
|
} catch (e) {
|
|
@@ -256,12 +256,12 @@ class Events {
|
|
|
256
256
|
*
|
|
257
257
|
*/
|
|
258
258
|
static task(options, context) {
|
|
259
|
-
if (typeof options.task ===
|
|
259
|
+
if (typeof options.task === 'function') {
|
|
260
260
|
try {
|
|
261
261
|
options.task(context);
|
|
262
262
|
} catch (e) {
|
|
263
263
|
// silencing the error, to avoid breaking the task;
|
|
264
|
-
Events.unexpected(
|
|
264
|
+
Events.unexpected('task', e);
|
|
265
265
|
}
|
|
266
266
|
}
|
|
267
267
|
}
|
|
@@ -301,12 +301,12 @@ class Events {
|
|
|
301
301
|
*
|
|
302
302
|
*/
|
|
303
303
|
static transact(options, context) {
|
|
304
|
-
if (typeof options.transact ===
|
|
304
|
+
if (typeof options.transact === 'function') {
|
|
305
305
|
try {
|
|
306
306
|
options.transact(context);
|
|
307
307
|
} catch (e) {
|
|
308
308
|
// silencing the error, to avoid breaking the transaction;
|
|
309
|
-
Events.unexpected(
|
|
309
|
+
Events.unexpected('transact', e);
|
|
310
310
|
}
|
|
311
311
|
}
|
|
312
312
|
}
|
|
@@ -352,14 +352,14 @@ class Events {
|
|
|
352
352
|
*
|
|
353
353
|
*/
|
|
354
354
|
static error(options, err, context) {
|
|
355
|
-
if (typeof options.error ===
|
|
355
|
+
if (typeof options.error === 'function') {
|
|
356
356
|
try {
|
|
357
357
|
options.error(err, context);
|
|
358
358
|
} catch (e) {
|
|
359
359
|
// have to silence errors here;
|
|
360
360
|
// throwing unhandled errors while handling an error
|
|
361
361
|
// notification is simply not acceptable.
|
|
362
|
-
Events.unexpected(
|
|
362
|
+
Events.unexpected('error', e);
|
|
363
363
|
}
|
|
364
364
|
}
|
|
365
365
|
}
|
|
@@ -450,14 +450,14 @@ class Events {
|
|
|
450
450
|
*
|
|
451
451
|
*/
|
|
452
452
|
static extend(options, obj, dc) {
|
|
453
|
-
if (typeof options.extend ===
|
|
453
|
+
if (typeof options.extend === 'function') {
|
|
454
454
|
try {
|
|
455
455
|
options.extend.call(obj, obj, dc);
|
|
456
456
|
} catch (e) {
|
|
457
457
|
// have to silence errors here;
|
|
458
458
|
// the result of throwing unhandled errors while
|
|
459
459
|
// extending the protocol would be unpredictable.
|
|
460
|
-
Events.unexpected(
|
|
460
|
+
Events.unexpected('extend', e);
|
|
461
461
|
}
|
|
462
462
|
}
|
|
463
463
|
}
|
package/lib/formatting.js
CHANGED
|
@@ -7,12 +7,12 @@
|
|
|
7
7
|
* Removal or modification of this copyright notice is prohibited.
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
const {assert} = require(
|
|
10
|
+
const {assert} = require('./assert');
|
|
11
11
|
|
|
12
12
|
const npm = {
|
|
13
|
-
pgUtils: require(
|
|
14
|
-
patterns: require(
|
|
15
|
-
utils: require(
|
|
13
|
+
pgUtils: require('pg/lib/utils'),
|
|
14
|
+
patterns: require('./patterns'),
|
|
15
|
+
utils: require('./utils')
|
|
16
16
|
};
|
|
17
17
|
|
|
18
18
|
// Format Modification Flags;
|
|
@@ -41,8 +41,8 @@ const fmMap = {
|
|
|
41
41
|
|
|
42
42
|
// Global symbols for Custom Type Formatting:
|
|
43
43
|
const ctfSymbols = {
|
|
44
|
-
toPostgres: Symbol.for(
|
|
45
|
-
rawType: Symbol.for(
|
|
44
|
+
toPostgres: Symbol.for('ctf.toPostgres'),
|
|
45
|
+
rawType: Symbol.for('ctf.rawType')
|
|
46
46
|
};
|
|
47
47
|
|
|
48
48
|
const maxVariable = 100000; // maximum supported variable is '$100000'
|
|
@@ -51,7 +51,7 @@ const maxVariable = 100000; // maximum supported variable is '$100000'
|
|
|
51
51
|
// Converts a single value into its Postgres format.
|
|
52
52
|
function formatValue({value, fm, cc, options}) {
|
|
53
53
|
|
|
54
|
-
if (typeof value ===
|
|
54
|
+
if (typeof value === 'function') {
|
|
55
55
|
return formatValue({value: resolveFunc(value, cc), fm, cc});
|
|
56
56
|
}
|
|
57
57
|
|
|
@@ -81,18 +81,18 @@ function formatValue({value, fm, cc, options}) {
|
|
|
81
81
|
|
|
82
82
|
if (isNull(value)) {
|
|
83
83
|
throwIfRaw(isRaw);
|
|
84
|
-
return
|
|
84
|
+
return 'null';
|
|
85
85
|
}
|
|
86
86
|
|
|
87
87
|
switch (typeof value) {
|
|
88
|
-
case
|
|
88
|
+
case 'string':
|
|
89
89
|
return $to.text(value, isRaw);
|
|
90
|
-
case
|
|
90
|
+
case 'boolean':
|
|
91
91
|
return $to.bool(value);
|
|
92
|
-
case
|
|
93
|
-
case
|
|
92
|
+
case 'number':
|
|
93
|
+
case 'bigint':
|
|
94
94
|
return $to.number(value);
|
|
95
|
-
case
|
|
95
|
+
case 'symbol':
|
|
96
96
|
throw new TypeError(`Type Symbol has no meaning for PostgreSQL: ${value.toString()}`);
|
|
97
97
|
default:
|
|
98
98
|
if (value instanceof Date) {
|
|
@@ -117,12 +117,12 @@ function formatValue({value, fm, cc, options}) {
|
|
|
117
117
|
// Top-level empty arrays are formatted as literal '{}' to avoid the necessity of explicit type casting,
|
|
118
118
|
// as the server cannot automatically infer the type of empty non-literal array.
|
|
119
119
|
function formatArray(array, options) {
|
|
120
|
-
const loop = a =>
|
|
120
|
+
const loop = a => '[' + a.map(value => Array.isArray(value) ? loop(value) : formatValue({
|
|
121
121
|
value,
|
|
122
122
|
options
|
|
123
|
-
})).join() +
|
|
124
|
-
const prefix = options && options.capSQL ?
|
|
125
|
-
return array.length ? (prefix + loop(array)) :
|
|
123
|
+
})).join() + ']';
|
|
124
|
+
const prefix = options && options.capSQL ? 'ARRAY' : 'array';
|
|
125
|
+
return array.length ? (prefix + loop(array)) : '\'{}\'';
|
|
126
126
|
}
|
|
127
127
|
|
|
128
128
|
///////////////////////////////////////////////////////////////////
|
|
@@ -131,10 +131,10 @@ function formatCSV(values, options) {
|
|
|
131
131
|
if (Array.isArray(values)) {
|
|
132
132
|
return values.map(value => formatValue({value, options})).join();
|
|
133
133
|
}
|
|
134
|
-
if (typeof values ===
|
|
134
|
+
if (typeof values === 'object' && values !== null) {
|
|
135
135
|
return Object.keys(values).map(v => formatValue({value: values[v], options})).join();
|
|
136
136
|
}
|
|
137
|
-
return values === undefined ?
|
|
137
|
+
return values === undefined ? '' : formatValue({value: values, options});
|
|
138
138
|
}
|
|
139
139
|
|
|
140
140
|
///////////////////////////////
|
|
@@ -142,9 +142,9 @@ function formatCSV(values, options) {
|
|
|
142
142
|
const formatAs = {
|
|
143
143
|
|
|
144
144
|
object({query, obj, raw, options}) {
|
|
145
|
-
options = options && typeof options ===
|
|
145
|
+
options = options && typeof options === 'object' ? options : {};
|
|
146
146
|
return query.replace(npm.patterns.namedParameters, name => {
|
|
147
|
-
const v = formatAs.stripName(name.replace(/^\$[{(<[/]|[\s})>\]/]/g,
|
|
147
|
+
const v = formatAs.stripName(name.replace(/^\$[{(<[/]|[\s})>\]/]/g, ''), raw),
|
|
148
148
|
c = npm.utils.getIfHas(obj, v.name);
|
|
149
149
|
if (!c.valid) {
|
|
150
150
|
throw new Error(`Invalid property name '${v.name}'.`);
|
|
@@ -152,11 +152,11 @@ const formatAs = {
|
|
|
152
152
|
if (c.has) {
|
|
153
153
|
return formatValue({value: c.value, fm: v.fm, cc: c.target, options});
|
|
154
154
|
}
|
|
155
|
-
if (v.name ===
|
|
155
|
+
if (v.name === 'this') {
|
|
156
156
|
return formatValue({value: obj, fm: v.fm, options});
|
|
157
157
|
}
|
|
158
|
-
if (
|
|
159
|
-
const d = options.def, value = typeof d ===
|
|
158
|
+
if ('def' in options) {
|
|
159
|
+
const d = options.def, value = typeof d === 'function' ? d.call(obj, v.name, obj) : d;
|
|
160
160
|
return formatValue({value, fm: v.fm, cc: obj, options});
|
|
161
161
|
}
|
|
162
162
|
if (options.partial) {
|
|
@@ -168,7 +168,7 @@ const formatAs = {
|
|
|
168
168
|
},
|
|
169
169
|
|
|
170
170
|
array({query, array, raw, options}) {
|
|
171
|
-
options = options && typeof options ===
|
|
171
|
+
options = options && typeof options === 'object' ? options : {};
|
|
172
172
|
return query.replace(npm.patterns.multipleValues, name => {
|
|
173
173
|
const v = formatAs.stripName(name.substr(1), raw);
|
|
174
174
|
const idx = v.name - 1;
|
|
@@ -178,8 +178,8 @@ const formatAs = {
|
|
|
178
178
|
if (idx < array.length) {
|
|
179
179
|
return formatValue({value: array[idx], fm: v.fm, options});
|
|
180
180
|
}
|
|
181
|
-
if (
|
|
182
|
-
const d = options.def, value = typeof d ===
|
|
181
|
+
if ('def' in options) {
|
|
182
|
+
const d = options.def, value = typeof d === 'function' ? d.call(array, idx, array) : d;
|
|
183
183
|
return formatValue({value, fm: v.fm, options});
|
|
184
184
|
}
|
|
185
185
|
if (options.partial) {
|
|
@@ -223,13 +223,13 @@ function isNull(value) {
|
|
|
223
223
|
function getCTF(value) {
|
|
224
224
|
if (!isNull(value)) {
|
|
225
225
|
let toPostgres = value[ctfSymbols.toPostgres], rawType = !!value[ctfSymbols.rawType];
|
|
226
|
-
if (typeof toPostgres !==
|
|
226
|
+
if (typeof toPostgres !== 'function') {
|
|
227
227
|
toPostgres = value.toPostgres;
|
|
228
228
|
rawType = !!value.rawType;
|
|
229
229
|
}
|
|
230
|
-
if (typeof toPostgres ===
|
|
231
|
-
if (toPostgres.constructor.name !==
|
|
232
|
-
throw new Error(
|
|
230
|
+
if (typeof toPostgres === 'function') {
|
|
231
|
+
if (toPostgres.constructor.name !== 'Function') {
|
|
232
|
+
throw new Error('CTF does not support asynchronous toPostgres functions.');
|
|
233
233
|
}
|
|
234
234
|
return {toPostgres, rawType};
|
|
235
235
|
}
|
|
@@ -247,26 +247,26 @@ function wrapText(text) {
|
|
|
247
247
|
// Replaces each single-quote symbol ' with two,
|
|
248
248
|
// for compliance with PostgreSQL strings.
|
|
249
249
|
function safeText(text) {
|
|
250
|
-
return text.replace(/'/g,
|
|
250
|
+
return text.replace(/'/g, '\'\'');
|
|
251
251
|
}
|
|
252
252
|
|
|
253
253
|
/////////////////////////////////////////////
|
|
254
254
|
// Throws an exception, if flag 'raw' is set.
|
|
255
255
|
function throwIfRaw(raw) {
|
|
256
256
|
if (raw) {
|
|
257
|
-
throw new TypeError(
|
|
257
|
+
throw new TypeError('Values null/undefined cannot be used as raw text.');
|
|
258
258
|
}
|
|
259
259
|
}
|
|
260
260
|
|
|
261
261
|
/////////////////////////////////////////////////////////////////////////////
|
|
262
262
|
// Recursively resolves parameter-function, with an optional Calling Context.
|
|
263
263
|
function resolveFunc(value, cc) {
|
|
264
|
-
while (typeof value ===
|
|
265
|
-
if (value.constructor.name !==
|
|
264
|
+
while (typeof value === 'function') {
|
|
265
|
+
if (value.constructor.name !== 'Function') {
|
|
266
266
|
// Constructor name for asynchronous functions have different names:
|
|
267
267
|
// - 'GeneratorFunction' for ES6 generators
|
|
268
268
|
// - 'AsyncFunction' for ES7 async functions
|
|
269
|
-
throw new Error(
|
|
269
|
+
throw new Error('Cannot use asynchronous functions with query formatting.');
|
|
270
270
|
}
|
|
271
271
|
value = value.call(cc, cc);
|
|
272
272
|
}
|
|
@@ -282,15 +282,15 @@ function resolveFunc(value, cc) {
|
|
|
282
282
|
// and where * is any of the supported open-close pairs: {}, (), [], <>, //
|
|
283
283
|
//
|
|
284
284
|
function formatQuery(query, values, raw, options) {
|
|
285
|
-
if (typeof query !==
|
|
286
|
-
throw new TypeError(
|
|
285
|
+
if (typeof query !== 'string') {
|
|
286
|
+
throw new TypeError('Parameter \'query\' must be a text string.');
|
|
287
287
|
}
|
|
288
288
|
const ctf = getCTF(values);
|
|
289
289
|
if (ctf) {
|
|
290
290
|
// Custom Type Formatting
|
|
291
291
|
return formatQuery(query, resolveFunc(ctf.toPostgres, values), raw || ctf.rawType, options);
|
|
292
292
|
}
|
|
293
|
-
if (typeof values ===
|
|
293
|
+
if (typeof values === 'object' && values !== null) {
|
|
294
294
|
if (Array.isArray(values)) {
|
|
295
295
|
// $1, $2,... formatting to be applied;
|
|
296
296
|
return formatAs.array({query, array: values, raw, options});
|
|
@@ -307,7 +307,7 @@ function formatQuery(query, values, raw, options) {
|
|
|
307
307
|
//////////////////////////////////////////////////////
|
|
308
308
|
// Formats a function or stored procedure call query;
|
|
309
309
|
function formatEntity(entity, values, {capSQL, type}) {
|
|
310
|
-
let prefix = type ===
|
|
310
|
+
let prefix = type === 'func' ? 'select * from' : 'call';
|
|
311
311
|
if (capSQL) {
|
|
312
312
|
prefix = prefix.toUpperCase();
|
|
313
313
|
}
|
|
@@ -315,7 +315,7 @@ function formatEntity(entity, values, {capSQL, type}) {
|
|
|
315
315
|
}
|
|
316
316
|
|
|
317
317
|
function formatSqlName(name) {
|
|
318
|
-
return `"${name.replace(/"/g,
|
|
318
|
+
return `"${name.replace(/"/g, '""')}"`;
|
|
319
319
|
}
|
|
320
320
|
|
|
321
321
|
/**
|
|
@@ -426,9 +426,9 @@ const $as = {
|
|
|
426
426
|
value = resolveFunc(value);
|
|
427
427
|
if (isNull(value)) {
|
|
428
428
|
throwIfRaw(raw);
|
|
429
|
-
return
|
|
429
|
+
return 'null';
|
|
430
430
|
}
|
|
431
|
-
if (typeof value !==
|
|
431
|
+
if (typeof value !== 'string') {
|
|
432
432
|
value = value.toString();
|
|
433
433
|
}
|
|
434
434
|
return $to.text(value, raw);
|
|
@@ -477,16 +477,16 @@ const $as = {
|
|
|
477
477
|
name(name) {
|
|
478
478
|
name = resolveFunc(name);
|
|
479
479
|
if (name) {
|
|
480
|
-
if (typeof name ===
|
|
480
|
+
if (typeof name === 'string') {
|
|
481
481
|
return /^\s*\*(\s*)$/.test(name) ? name : formatSqlName(name);
|
|
482
482
|
}
|
|
483
|
-
if (typeof name ===
|
|
483
|
+
if (typeof name === 'object') {
|
|
484
484
|
const keys = Array.isArray(name) ? name : Object.keys(name);
|
|
485
485
|
if (!keys.length) {
|
|
486
|
-
throw new Error(
|
|
486
|
+
throw new Error('Cannot retrieve sql names from an empty array/object.');
|
|
487
487
|
}
|
|
488
488
|
return keys.map(value => {
|
|
489
|
-
if (!value || typeof value !==
|
|
489
|
+
if (!value || typeof value !== 'string') {
|
|
490
490
|
throw new Error(`Invalid sql name: ${npm.utils.toJson(value)}`);
|
|
491
491
|
}
|
|
492
492
|
return formatSqlName(value);
|
|
@@ -529,16 +529,16 @@ const $as = {
|
|
|
529
529
|
*/
|
|
530
530
|
alias(name) {
|
|
531
531
|
name = resolveFunc(name);
|
|
532
|
-
if (name && typeof name ===
|
|
533
|
-
return name.split(
|
|
532
|
+
if (name && typeof name === 'string') {
|
|
533
|
+
return name.split('.')
|
|
534
534
|
.filter(f => f)
|
|
535
535
|
.map(a => {
|
|
536
536
|
const m = a.match(/^([a-z_][a-z0-9_$]*|[A-Z_][A-Z0-9_$]*)$/);
|
|
537
537
|
if (m && m[0] === a) {
|
|
538
538
|
return a;
|
|
539
539
|
}
|
|
540
|
-
return `"${a.replace(/"/g,
|
|
541
|
-
}).join(
|
|
540
|
+
return `"${a.replace(/"/g, '""')}"`;
|
|
541
|
+
}).join('.');
|
|
542
542
|
}
|
|
543
543
|
throw new TypeError(`Invalid sql alias: ${npm.utils.toJson(name)}`);
|
|
544
544
|
},
|
|
@@ -564,7 +564,7 @@ const $as = {
|
|
|
564
564
|
value(value) {
|
|
565
565
|
value = resolveFunc(value);
|
|
566
566
|
if (isNull(value)) {
|
|
567
|
-
throw new TypeError(
|
|
567
|
+
throw new TypeError('Open values cannot be null or undefined.');
|
|
568
568
|
}
|
|
569
569
|
return safeText(formatValue({value, fm: fmFlags.raw}));
|
|
570
570
|
},
|
|
@@ -588,7 +588,7 @@ const $as = {
|
|
|
588
588
|
obj = resolveFunc(obj);
|
|
589
589
|
if (isNull(obj)) {
|
|
590
590
|
throwIfRaw(raw);
|
|
591
|
-
return
|
|
591
|
+
return 'null';
|
|
592
592
|
}
|
|
593
593
|
if (obj instanceof Buffer) {
|
|
594
594
|
return $to.buffer(obj, raw);
|
|
@@ -609,7 +609,7 @@ const $as = {
|
|
|
609
609
|
bool(value) {
|
|
610
610
|
value = resolveFunc(value);
|
|
611
611
|
if (isNull(value)) {
|
|
612
|
-
return
|
|
612
|
+
return 'null';
|
|
613
613
|
}
|
|
614
614
|
return $to.bool(value);
|
|
615
615
|
},
|
|
@@ -632,7 +632,7 @@ const $as = {
|
|
|
632
632
|
d = resolveFunc(d);
|
|
633
633
|
if (isNull(d)) {
|
|
634
634
|
throwIfRaw(raw);
|
|
635
|
-
return
|
|
635
|
+
return 'null';
|
|
636
636
|
}
|
|
637
637
|
if (d instanceof Date) {
|
|
638
638
|
return $to.date(d, raw);
|
|
@@ -654,10 +654,10 @@ const $as = {
|
|
|
654
654
|
number(num) {
|
|
655
655
|
num = resolveFunc(num);
|
|
656
656
|
if (isNull(num)) {
|
|
657
|
-
return
|
|
657
|
+
return 'null';
|
|
658
658
|
}
|
|
659
659
|
const t = typeof num;
|
|
660
|
-
if (t !==
|
|
660
|
+
if (t !== 'number' && t !== 'bigint') {
|
|
661
661
|
throw new TypeError(`${wrapText(num)} is not a number.`);
|
|
662
662
|
}
|
|
663
663
|
return $to.number(num);
|
|
@@ -683,10 +683,10 @@ const $as = {
|
|
|
683
683
|
* @returns {string}
|
|
684
684
|
*/
|
|
685
685
|
array(arr, options) {
|
|
686
|
-
options = assert(options, [
|
|
686
|
+
options = assert(options, ['capSQL']);
|
|
687
687
|
arr = resolveFunc(arr);
|
|
688
688
|
if (isNull(arr)) {
|
|
689
|
-
return
|
|
689
|
+
return 'null';
|
|
690
690
|
}
|
|
691
691
|
if (Array.isArray(arr)) {
|
|
692
692
|
return $to.array(arr, options);
|
|
@@ -737,7 +737,7 @@ const $as = {
|
|
|
737
737
|
data = resolveFunc(data);
|
|
738
738
|
if (isNull(data)) {
|
|
739
739
|
throwIfRaw(raw);
|
|
740
|
-
return
|
|
740
|
+
return 'null';
|
|
741
741
|
}
|
|
742
742
|
return $to.json(data, raw);
|
|
743
743
|
},
|
|
@@ -761,9 +761,9 @@ const $as = {
|
|
|
761
761
|
func(func, raw, cc) {
|
|
762
762
|
if (isNull(func)) {
|
|
763
763
|
throwIfRaw(raw);
|
|
764
|
-
return
|
|
764
|
+
return 'null';
|
|
765
765
|
}
|
|
766
|
-
if (typeof func !==
|
|
766
|
+
if (typeof func !== 'function') {
|
|
767
767
|
throw new TypeError(`${wrapText(func)} is not a function.`);
|
|
768
768
|
}
|
|
769
769
|
const fm = raw ? fmFlags.raw : null;
|
|
@@ -850,7 +850,7 @@ const $as = {
|
|
|
850
850
|
* The function will throw an error, if any occurs during formatting.
|
|
851
851
|
*/
|
|
852
852
|
format(query, values, options) {
|
|
853
|
-
options = assert(options, [
|
|
853
|
+
options = assert(options, ['capSQL', 'partial', 'def']);
|
|
854
854
|
const ctf = getCTF(query);
|
|
855
855
|
if (ctf) {
|
|
856
856
|
query = ctf.toPostgres.call(query, query);
|
|
@@ -868,10 +868,10 @@ const $to = {
|
|
|
868
868
|
return formatCSV(resolveFunc(values), options);
|
|
869
869
|
},
|
|
870
870
|
bool(value) {
|
|
871
|
-
return value ?
|
|
871
|
+
return value ? 'true' : 'false';
|
|
872
872
|
},
|
|
873
873
|
buffer(obj, raw) {
|
|
874
|
-
const s = `\\x${obj.toString(
|
|
874
|
+
const s = `\\x${obj.toString('hex')}`;
|
|
875
875
|
return raw ? s : wrapText(s);
|
|
876
876
|
},
|
|
877
877
|
date(d, raw) {
|
|
@@ -883,7 +883,7 @@ const $to = {
|
|
|
883
883
|
return raw ? s : wrapText(safeText(s));
|
|
884
884
|
},
|
|
885
885
|
number(num) {
|
|
886
|
-
if (typeof num ===
|
|
886
|
+
if (typeof num === 'bigint' || Number.isFinite(num)) {
|
|
887
887
|
return num.toString();
|
|
888
888
|
}
|
|
889
889
|
// Converting NaN/+Infinity/-Infinity according to Postgres documentation:
|
|
@@ -891,12 +891,12 @@ const $to = {
|
|
|
891
891
|
//
|
|
892
892
|
// NOTE: strings for 'NaN'/'+Infinity'/'-Infinity' are not case-sensitive.
|
|
893
893
|
if (num === Number.POSITIVE_INFINITY) {
|
|
894
|
-
return wrapText(
|
|
894
|
+
return wrapText('+Infinity');
|
|
895
895
|
}
|
|
896
896
|
if (num === Number.NEGATIVE_INFINITY) {
|
|
897
|
-
return wrapText(
|
|
897
|
+
return wrapText('-Infinity');
|
|
898
898
|
}
|
|
899
|
-
return wrapText(
|
|
899
|
+
return wrapText('NaN');
|
|
900
900
|
},
|
|
901
901
|
text(value, raw) {
|
|
902
902
|
return raw ? value : wrapText(safeText(value));
|