pgsql-deparser 13.15.0 → 13.18.0
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 +94 -45
- package/package.json +25 -73
- package/src/deparser/deparser.ts +10474 -0
- package/src/deparser/index.ts +14 -0
- package/src/deparser/utils/list-utils.ts +27 -0
- package/src/deparser/utils/quote-utils.ts +86 -0
- package/src/deparser/utils/sql-formatter.ts +37 -0
- package/src/deparser/visitors/base.ts +145 -0
- package/src/index.ts +27 -3
- package/src/v13-to-v14.ts +2887 -0
- package/src/v13-to-v17-direct.ts +79 -0
- package/src/v14-to-v15.ts +1624 -0
- package/src/v15-to-v16.ts +3355 -0
- package/src/v16-to-v17.ts +1897 -0
- package/tsconfig.esm.json +8 -0
- package/tsconfig.json +26 -0
- package/LICENSE +0 -21
- package/main/deparser.js +0 -3481
- package/main/index.js +0 -10
- package/main/utils/index.js +0 -97
- package/module/deparser.js +0 -3478
- package/module/index.js +0 -3
- package/module/utils/index.js +0 -90
- package/src/deparser.ts +0 -4220
- package/src/utils/index.ts +0 -92
- package/types/deparser.d.ts +0 -119
- package/types/index.d.ts +0 -3
- package/types/utils/index.d.ts +0 -4
package/main/deparser.js
DELETED
|
@@ -1,3481 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
// @ts-nocheck
|
|
4
|
-
const util_1 = require("util");
|
|
5
|
-
const pgsql_enums_1 = require("pgsql-enums");
|
|
6
|
-
let TAB_CHAR = '\t';
|
|
7
|
-
let NEWLINE_CHAR = '\n';
|
|
8
|
-
const isEmptyObject = (obj) => {
|
|
9
|
-
return !obj || (typeof obj === 'object' && !Object.keys(obj).length);
|
|
10
|
-
};
|
|
11
|
-
const dotty = require('dotty');
|
|
12
|
-
const fail = (type, node) => {
|
|
13
|
-
throw new Error((0, util_1.format)('Unhandled %s node: %s', type, JSON.stringify(node)));
|
|
14
|
-
};
|
|
15
|
-
// select word from pg_get_keywords() where catcode = 'R';
|
|
16
|
-
const RESERVED_WORDS = new Set([
|
|
17
|
-
'all',
|
|
18
|
-
'analyse',
|
|
19
|
-
'analyze',
|
|
20
|
-
'and',
|
|
21
|
-
'any',
|
|
22
|
-
'array',
|
|
23
|
-
'as',
|
|
24
|
-
'asc',
|
|
25
|
-
'asymmetric',
|
|
26
|
-
'both',
|
|
27
|
-
'case',
|
|
28
|
-
'cast',
|
|
29
|
-
'check',
|
|
30
|
-
'collate',
|
|
31
|
-
'column',
|
|
32
|
-
'constraint',
|
|
33
|
-
'create',
|
|
34
|
-
'current_catalog',
|
|
35
|
-
'current_date',
|
|
36
|
-
'current_role',
|
|
37
|
-
'current_time',
|
|
38
|
-
'current_timestamp',
|
|
39
|
-
'current_user',
|
|
40
|
-
'default',
|
|
41
|
-
'deferrable',
|
|
42
|
-
'desc',
|
|
43
|
-
'distinct',
|
|
44
|
-
'do',
|
|
45
|
-
'else',
|
|
46
|
-
'end',
|
|
47
|
-
'except',
|
|
48
|
-
'false',
|
|
49
|
-
'fetch',
|
|
50
|
-
'for',
|
|
51
|
-
'foreign',
|
|
52
|
-
'from',
|
|
53
|
-
'grant',
|
|
54
|
-
'group',
|
|
55
|
-
'having',
|
|
56
|
-
'in',
|
|
57
|
-
'initially',
|
|
58
|
-
'intersect',
|
|
59
|
-
'into',
|
|
60
|
-
'lateral',
|
|
61
|
-
'leading',
|
|
62
|
-
'limit',
|
|
63
|
-
'localtime',
|
|
64
|
-
'localtimestamp',
|
|
65
|
-
'not',
|
|
66
|
-
'null',
|
|
67
|
-
'offset',
|
|
68
|
-
'on',
|
|
69
|
-
'only',
|
|
70
|
-
'or',
|
|
71
|
-
'order',
|
|
72
|
-
'placing',
|
|
73
|
-
'primary',
|
|
74
|
-
'references',
|
|
75
|
-
'returning',
|
|
76
|
-
'select',
|
|
77
|
-
'session_user',
|
|
78
|
-
'some',
|
|
79
|
-
'symmetric',
|
|
80
|
-
'table',
|
|
81
|
-
'then',
|
|
82
|
-
'to',
|
|
83
|
-
'trailing',
|
|
84
|
-
'true',
|
|
85
|
-
'union',
|
|
86
|
-
'unique',
|
|
87
|
-
'user',
|
|
88
|
-
'using',
|
|
89
|
-
'variadic',
|
|
90
|
-
'when',
|
|
91
|
-
'where',
|
|
92
|
-
'window',
|
|
93
|
-
'with'
|
|
94
|
-
]);
|
|
95
|
-
// https://github.com/pganalyze/libpg_query/blob/b2790f8140721ff7f047167ecd7d44267b0a3880/src/postgres/include/storage/lockdefs.h
|
|
96
|
-
const LOCK_MODES = {
|
|
97
|
-
1: 'ACCESS SHARE',
|
|
98
|
-
2: 'ROW SHARE',
|
|
99
|
-
3: 'ROW EXCLUSIVE',
|
|
100
|
-
4: 'SHARE UPDATE EXCLUSIVE',
|
|
101
|
-
5: 'SHARE',
|
|
102
|
-
6: 'SHARE ROW',
|
|
103
|
-
7: 'EXCLUSIVE',
|
|
104
|
-
8: 'ACCESS EXCLUSIVE'
|
|
105
|
-
};
|
|
106
|
-
const isReserved = (value) => RESERVED_WORDS.has(value.toLowerCase());
|
|
107
|
-
// has uppercase and lowercase, or non word characters
|
|
108
|
-
const needsQuotesRegex = /[a-z]+[\W\w]*[A-Z]+|[A-Z]+[\W\w]*[a-z]+|\W/;
|
|
109
|
-
// usually the AST lowercases all the things, so if we
|
|
110
|
-
// have both, the author most likely used double quotes
|
|
111
|
-
const needsQuotes = (value) => needsQuotesRegex.test(value) || isReserved(value);
|
|
112
|
-
const compact = (o) => o.filter((e) => {
|
|
113
|
-
const isFalsy = !e;
|
|
114
|
-
return !isFalsy && e.toString().length;
|
|
115
|
-
});
|
|
116
|
-
const flatten = (o) => {
|
|
117
|
-
const flattened = [];
|
|
118
|
-
for (let i = 0; i < o.length; i++) {
|
|
119
|
-
if (Array.isArray(o[i])) {
|
|
120
|
-
for (let j = 0; j < o[i].length; j++) {
|
|
121
|
-
flattened.push(o[i][j]);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
else {
|
|
125
|
-
flattened.push(o[i]);
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
return flattened;
|
|
129
|
-
};
|
|
130
|
-
const inverted = (o) => {
|
|
131
|
-
const objInverted = {};
|
|
132
|
-
const keys = Object.keys(o);
|
|
133
|
-
for (let i = 0; i < keys.length; i++) {
|
|
134
|
-
const key = keys[i];
|
|
135
|
-
objInverted[o[key]] = key;
|
|
136
|
-
}
|
|
137
|
-
return objInverted;
|
|
138
|
-
};
|
|
139
|
-
const parens = (string) => {
|
|
140
|
-
return '(' + string + ')';
|
|
141
|
-
};
|
|
142
|
-
const indent = (text, count = 1) => text;
|
|
143
|
-
const unwrapList = (obj) => obj !== undefined && obj.List !== undefined ? obj.List.items : obj;
|
|
144
|
-
class Deparser {
|
|
145
|
-
static deparse(query, opts) {
|
|
146
|
-
return new Deparser(query, opts).deparseQuery();
|
|
147
|
-
}
|
|
148
|
-
constructor(tree, opts = {}) {
|
|
149
|
-
this.tree = tree;
|
|
150
|
-
if (opts.hasOwnProperty('newline')) {
|
|
151
|
-
NEWLINE_CHAR = opts.newline;
|
|
152
|
-
}
|
|
153
|
-
if (opts.hasOwnProperty('tab')) {
|
|
154
|
-
TAB_CHAR = opts.tab;
|
|
155
|
-
}
|
|
156
|
-
if (!Array.isArray(this.tree))
|
|
157
|
-
this.tree = [this.tree];
|
|
158
|
-
}
|
|
159
|
-
deparseQuery() {
|
|
160
|
-
return this.tree
|
|
161
|
-
.map((node) => this.deparse(node))
|
|
162
|
-
.join(NEWLINE_CHAR + NEWLINE_CHAR);
|
|
163
|
-
}
|
|
164
|
-
deparseNodes(nodes, context) {
|
|
165
|
-
return unwrapList(nodes).map((node) => {
|
|
166
|
-
const unwrapped = unwrapList(node);
|
|
167
|
-
return Array.isArray(unwrapped)
|
|
168
|
-
? this.list(unwrapped, ', ', '', context)
|
|
169
|
-
: this.deparse(node, context);
|
|
170
|
-
});
|
|
171
|
-
}
|
|
172
|
-
deparseReturningList(list, context) {
|
|
173
|
-
return unwrapList(list)
|
|
174
|
-
.map((returning) => this.deparse(returning.ResTarget.val, context) +
|
|
175
|
-
(returning.ResTarget.name
|
|
176
|
-
? ' AS ' + this.quote(returning.ResTarget.name)
|
|
177
|
-
: ''))
|
|
178
|
-
.join(',');
|
|
179
|
-
}
|
|
180
|
-
list(nodes, separator = ', ', prefix = '', context) {
|
|
181
|
-
if (!nodes) {
|
|
182
|
-
return '';
|
|
183
|
-
}
|
|
184
|
-
return this.deparseNodes(nodes, context)
|
|
185
|
-
.map((l) => `${prefix}${l}`)
|
|
186
|
-
.join(separator);
|
|
187
|
-
}
|
|
188
|
-
listQuotes(nodes, separator = ', ') {
|
|
189
|
-
return this.list(unwrapList(nodes), separator)
|
|
190
|
-
.split(separator)
|
|
191
|
-
.map((a) => this.quote(a.trim()))
|
|
192
|
-
.join(separator);
|
|
193
|
-
}
|
|
194
|
-
quote(value) {
|
|
195
|
-
if (value == null) {
|
|
196
|
-
return null;
|
|
197
|
-
}
|
|
198
|
-
const unwrapped = unwrapList(value);
|
|
199
|
-
if (Array.isArray(unwrapped)) {
|
|
200
|
-
return unwrapped.map((o) => this.quote(o));
|
|
201
|
-
}
|
|
202
|
-
if (needsQuotes(value)) {
|
|
203
|
-
return '"' + value + '"';
|
|
204
|
-
}
|
|
205
|
-
return value;
|
|
206
|
-
}
|
|
207
|
-
// SELECT encode(E'''123\\000\\001', 'base64')
|
|
208
|
-
escape(literal) {
|
|
209
|
-
return "'" + literal.replace(/'/g, "''") + "'";
|
|
210
|
-
}
|
|
211
|
-
getPgCatalogTypeName(typeName, size) {
|
|
212
|
-
switch (typeName) {
|
|
213
|
-
case 'bpchar':
|
|
214
|
-
if (size != null) {
|
|
215
|
-
return 'char';
|
|
216
|
-
}
|
|
217
|
-
// return `pg_catalog.bpchar` below so that the following is symmetric
|
|
218
|
-
// SELECT char 'c' = char 'c' AS true
|
|
219
|
-
return 'pg_catalog.bpchar';
|
|
220
|
-
case 'varchar':
|
|
221
|
-
return 'varchar';
|
|
222
|
-
case 'numeric':
|
|
223
|
-
return 'numeric';
|
|
224
|
-
case 'bool':
|
|
225
|
-
return 'boolean';
|
|
226
|
-
case 'int2':
|
|
227
|
-
return 'smallint';
|
|
228
|
-
case 'int4':
|
|
229
|
-
return 'int';
|
|
230
|
-
case 'int8':
|
|
231
|
-
return 'bigint';
|
|
232
|
-
case 'real':
|
|
233
|
-
return 'pg_catalog.float4';
|
|
234
|
-
case 'time':
|
|
235
|
-
return 'time';
|
|
236
|
-
case 'timestamp':
|
|
237
|
-
return 'timestamp';
|
|
238
|
-
case 'interval':
|
|
239
|
-
return 'interval';
|
|
240
|
-
case 'bit':
|
|
241
|
-
return 'bit';
|
|
242
|
-
default:
|
|
243
|
-
return 'pg_catalog.' + typeName;
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
type(names, args) {
|
|
247
|
-
const catalogAndType = unwrapList(names).map((name) => this.deparse(name));
|
|
248
|
-
const catalog = catalogAndType[0];
|
|
249
|
-
const type = catalogAndType[1];
|
|
250
|
-
const mods = (name, size) => {
|
|
251
|
-
if (size != null) {
|
|
252
|
-
return name + '(' + size + ')';
|
|
253
|
-
}
|
|
254
|
-
return name;
|
|
255
|
-
};
|
|
256
|
-
// handle the special "char" (in quotes) type
|
|
257
|
-
if (catalog === 'char' && !type) {
|
|
258
|
-
return mods('"char"', args);
|
|
259
|
-
}
|
|
260
|
-
if (catalog === 'pg_catalog' && type === 'char') {
|
|
261
|
-
return mods('pg_catalog."char"', args);
|
|
262
|
-
}
|
|
263
|
-
if (catalog !== 'pg_catalog') {
|
|
264
|
-
return mods(this.listQuotes(names, '.'), args);
|
|
265
|
-
}
|
|
266
|
-
const res = this.getPgCatalogTypeName(type, args);
|
|
267
|
-
return mods(res, args);
|
|
268
|
-
}
|
|
269
|
-
deparse(item, context) {
|
|
270
|
-
if (item == null) {
|
|
271
|
-
return null;
|
|
272
|
-
}
|
|
273
|
-
if (typeof item === 'number' || item instanceof Number) {
|
|
274
|
-
return item;
|
|
275
|
-
}
|
|
276
|
-
const type = Object.keys(item)[0];
|
|
277
|
-
const node = item[type];
|
|
278
|
-
if (this[type] == null) {
|
|
279
|
-
throw new Error(type + ' is not implemented: ' + JSON.stringify(node));
|
|
280
|
-
}
|
|
281
|
-
return this[type](node, context);
|
|
282
|
-
}
|
|
283
|
-
RawStmt(node, context = {}) {
|
|
284
|
-
if (node.stmt_len) {
|
|
285
|
-
return this.deparse(node.stmt, context) + ';';
|
|
286
|
-
}
|
|
287
|
-
return this.deparse(node.stmt, context);
|
|
288
|
-
}
|
|
289
|
-
RuleStmt(node, context = {}) {
|
|
290
|
-
const output = [];
|
|
291
|
-
output.push('CREATE');
|
|
292
|
-
output.push('RULE');
|
|
293
|
-
if (node.rulename === '_RETURN') {
|
|
294
|
-
// special rules
|
|
295
|
-
output.push('"_RETURN"');
|
|
296
|
-
}
|
|
297
|
-
else {
|
|
298
|
-
output.push(node.rulename);
|
|
299
|
-
}
|
|
300
|
-
output.push('AS');
|
|
301
|
-
output.push('ON');
|
|
302
|
-
switch (node.event) {
|
|
303
|
-
case 'CMD_SELECT':
|
|
304
|
-
output.push('SELECT');
|
|
305
|
-
break;
|
|
306
|
-
case 'CMD_UPDATE':
|
|
307
|
-
output.push('UPDATE');
|
|
308
|
-
break;
|
|
309
|
-
case 'CMD_INSERT':
|
|
310
|
-
output.push('INSERT');
|
|
311
|
-
break;
|
|
312
|
-
case 'CMD_DELETE':
|
|
313
|
-
output.push('DELETE');
|
|
314
|
-
break;
|
|
315
|
-
default:
|
|
316
|
-
throw new Error('event type not yet implemented for RuleStmt');
|
|
317
|
-
}
|
|
318
|
-
output.push('TO');
|
|
319
|
-
output.push(this.RangeVar(node.relation, context));
|
|
320
|
-
if (node.whereClause) {
|
|
321
|
-
output.push('WHERE');
|
|
322
|
-
output.push(this.deparse(node.whereClause, context));
|
|
323
|
-
}
|
|
324
|
-
output.push('DO');
|
|
325
|
-
if (node.instead) {
|
|
326
|
-
output.push('INSTEAD');
|
|
327
|
-
}
|
|
328
|
-
const actions = unwrapList(node.actions);
|
|
329
|
-
if (!actions || !actions.length) {
|
|
330
|
-
output.push('NOTHING');
|
|
331
|
-
}
|
|
332
|
-
else {
|
|
333
|
-
// TODO how do multiple actions happen?
|
|
334
|
-
output.push(this.deparse(actions[0], context));
|
|
335
|
-
}
|
|
336
|
-
return output.join(' ');
|
|
337
|
-
}
|
|
338
|
-
A_Expr(node, context = {}) {
|
|
339
|
-
const output = [];
|
|
340
|
-
const nodeName = unwrapList(node.name);
|
|
341
|
-
switch (node.kind) {
|
|
342
|
-
case 'AEXPR_OP': {
|
|
343
|
-
let operator;
|
|
344
|
-
if (node.lexpr) {
|
|
345
|
-
// PARENS
|
|
346
|
-
if (node.lexpr !== undefined && node.lexpr.A_Expr !== undefined) {
|
|
347
|
-
output.push(parens(this.deparse(node.lexpr, context)));
|
|
348
|
-
}
|
|
349
|
-
else {
|
|
350
|
-
output.push(this.deparse(node.lexpr, context));
|
|
351
|
-
}
|
|
352
|
-
}
|
|
353
|
-
if (nodeName.length > 1) {
|
|
354
|
-
const schema = this.deparse(nodeName[0], context);
|
|
355
|
-
operator = this.deparse(nodeName[1], context);
|
|
356
|
-
output.push(`OPERATOR(${schema}.${operator})`);
|
|
357
|
-
}
|
|
358
|
-
else {
|
|
359
|
-
operator = this.deparse(nodeName[0], context);
|
|
360
|
-
output.push(operator);
|
|
361
|
-
}
|
|
362
|
-
if (node.rexpr) {
|
|
363
|
-
// PARENS
|
|
364
|
-
if (node.rexpr !== undefined && node.rexpr.A_Expr !== undefined) {
|
|
365
|
-
output.push(parens(this.deparse(node.rexpr, context)));
|
|
366
|
-
}
|
|
367
|
-
else {
|
|
368
|
-
output.push(this.deparse(node.rexpr, context));
|
|
369
|
-
}
|
|
370
|
-
}
|
|
371
|
-
if (output.length === 2) {
|
|
372
|
-
return output.join('');
|
|
373
|
-
}
|
|
374
|
-
if (['->', '->>'].includes(operator)) {
|
|
375
|
-
return output.join('');
|
|
376
|
-
}
|
|
377
|
-
return output.join(' ');
|
|
378
|
-
}
|
|
379
|
-
case 'AEXPR_OP_ANY':
|
|
380
|
-
/* scalar op ANY (array) */
|
|
381
|
-
output.push(this.deparse(node.lexpr, context));
|
|
382
|
-
output.push((0, util_1.format)('ANY (%s)', this.deparse(node.rexpr, context)));
|
|
383
|
-
return output.join(` ${this.deparse(nodeName[0], context)} `);
|
|
384
|
-
case 'AEXPR_OP_ALL':
|
|
385
|
-
/* scalar op ALL (array) */
|
|
386
|
-
output.push(this.deparse(node.lexpr, context));
|
|
387
|
-
output.push((0, util_1.format)('ALL (%s)', this.deparse(node.rexpr, context)));
|
|
388
|
-
return output.join(` ${this.deparse(nodeName[0], context)} `);
|
|
389
|
-
case 'AEXPR_DISTINCT':
|
|
390
|
-
/* IS DISTINCT FROM - name must be "=" */
|
|
391
|
-
return (0, util_1.format)('%s IS DISTINCT FROM %s', this.deparse(node.lexpr, context), this.deparse(node.rexpr, context));
|
|
392
|
-
case 'AEXPR_NOT_DISTINCT':
|
|
393
|
-
/* IS NOT DISTINCT FROM - name must be "=" */
|
|
394
|
-
return (0, util_1.format)('%s IS NOT DISTINCT FROM %s', this.deparse(node.lexpr, context), this.deparse(node.rexpr, context));
|
|
395
|
-
case 'AEXPR_NULLIF':
|
|
396
|
-
/* NULLIF - name must be "=" */
|
|
397
|
-
return (0, util_1.format)('NULLIF(%s, %s)', this.deparse(node.lexpr, context), this.deparse(node.rexpr, context));
|
|
398
|
-
case 'AEXPR_OF': {
|
|
399
|
-
/* IS [NOT] OF - name must be "=" or "<>" */
|
|
400
|
-
const op = nodeName[0].String.str === '=' ? 'IS OF' : 'IS NOT OF';
|
|
401
|
-
return (0, util_1.format)('%s %s (%s)', this.deparse(node.lexpr, context), op, this.list(node.rexpr, ', ', '', context));
|
|
402
|
-
}
|
|
403
|
-
case 'AEXPR_IN': {
|
|
404
|
-
/* [NOT] IN - name must be "=" or "<>" */
|
|
405
|
-
const operator = nodeName[0].String.str === '=' ? 'IN' : 'NOT IN';
|
|
406
|
-
return (0, util_1.format)('%s %s (%s)', this.deparse(node.lexpr, context), operator, this.list(node.rexpr, ', ', '', context));
|
|
407
|
-
}
|
|
408
|
-
case 'AEXPR_LIKE':
|
|
409
|
-
/* [NOT] LIKE - name must be "~~" or "!~~" */
|
|
410
|
-
output.push(this.deparse(node.lexpr, context));
|
|
411
|
-
if (nodeName[0].String.str === '!~~') {
|
|
412
|
-
output.push((0, util_1.format)('NOT LIKE (%s)', this.deparse(node.rexpr, context)));
|
|
413
|
-
}
|
|
414
|
-
else {
|
|
415
|
-
output.push((0, util_1.format)('LIKE (%s)', this.deparse(node.rexpr, context)));
|
|
416
|
-
}
|
|
417
|
-
return output.join(' ');
|
|
418
|
-
case 'AEXPR_ILIKE':
|
|
419
|
-
/* [NOT] ILIKE - name must be "~~*" or "!~~*" */
|
|
420
|
-
output.push(this.deparse(node.lexpr, context));
|
|
421
|
-
if (nodeName[0].String.str === '!~~*') {
|
|
422
|
-
output.push((0, util_1.format)('NOT ILIKE (%s)', this.deparse(node.rexpr, context)));
|
|
423
|
-
}
|
|
424
|
-
else {
|
|
425
|
-
output.push((0, util_1.format)('ILIKE (%s)', this.deparse(node.rexpr, context)));
|
|
426
|
-
}
|
|
427
|
-
return output.join(' ');
|
|
428
|
-
case 'AEXPR_SIMILAR':
|
|
429
|
-
// SIMILAR TO emits a similar_escape FuncCall node with the first argument
|
|
430
|
-
output.push(this.deparse(node.lexpr, context));
|
|
431
|
-
if (nodeName[0].String.str === '~') {
|
|
432
|
-
if (unwrapList(node.rexpr.FuncCall.args).length > 1) {
|
|
433
|
-
output.push((0, util_1.format)('SIMILAR TO %s ESCAPE %s', this.deparse(unwrapList(node.rexpr.FuncCall.args)[0], context), this.deparse(unwrapList(node.rexpr.FuncCall.args)[1], context)));
|
|
434
|
-
}
|
|
435
|
-
else {
|
|
436
|
-
output.push((0, util_1.format)('SIMILAR TO %s', this.deparse(unwrapList(node.rexpr.FuncCall.args)[0], context)));
|
|
437
|
-
}
|
|
438
|
-
}
|
|
439
|
-
else {
|
|
440
|
-
if (unwrapList(node.rexpr.FuncCall.args).length > 1) {
|
|
441
|
-
output.push((0, util_1.format)('NOT SIMILAR TO %s ESCAPE %s', this.deparse(unwrapList(node.rexpr.FuncCall.args)[0], context), this.deparse(unwrapList(node.rexpr.FuncCall.args)[1], context)));
|
|
442
|
-
}
|
|
443
|
-
else {
|
|
444
|
-
output.push((0, util_1.format)('NOT SIMILAR TO %s', this.deparse(unwrapList(node.rexpr.FuncCall.args)[0], context)));
|
|
445
|
-
}
|
|
446
|
-
}
|
|
447
|
-
return output.join(' ');
|
|
448
|
-
case 'AEXPR_BETWEEN':
|
|
449
|
-
output.push(this.deparse(node.lexpr, context));
|
|
450
|
-
output.push((0, util_1.format)('BETWEEN %s AND %s', this.deparse(unwrapList(node.rexpr)[0], context), this.deparse(unwrapList(node.rexpr)[1], context)));
|
|
451
|
-
return output.join(' ');
|
|
452
|
-
case 'AEXPR_NOT_BETWEEN':
|
|
453
|
-
output.push(this.deparse(node.lexpr, context));
|
|
454
|
-
output.push((0, util_1.format)('NOT BETWEEN %s AND %s', this.deparse(unwrapList(node.rexpr)[0], context), this.deparse(unwrapList(node.rexpr)[1], context)));
|
|
455
|
-
return output.join(' ');
|
|
456
|
-
case 'AEXPR_BETWEEN_SYM':
|
|
457
|
-
output.push(this.deparse(node.lexpr, context));
|
|
458
|
-
output.push((0, util_1.format)('BETWEEN SYMMETRIC %s AND %s', this.deparse(unwrapList(node.rexpr)[0], context), this.deparse(unwrapList(node.rexpr)[1], context)));
|
|
459
|
-
return output.join(' ');
|
|
460
|
-
case 'AEXPR_NOT_BETWEEN_SYM':
|
|
461
|
-
output.push(this.deparse(node.lexpr, context));
|
|
462
|
-
output.push((0, util_1.format)('NOT BETWEEN SYMMETRIC %s AND %s', this.deparse(unwrapList(node.rexpr)[0], context), this.deparse(unwrapList(node.rexpr)[1], context)));
|
|
463
|
-
return output.join(' ');
|
|
464
|
-
// case 15:
|
|
465
|
-
// AEXPR_PAREN
|
|
466
|
-
default:
|
|
467
|
-
return fail('A_Expr', node);
|
|
468
|
-
}
|
|
469
|
-
}
|
|
470
|
-
Alias(node, context = {}) {
|
|
471
|
-
const name = node.aliasname;
|
|
472
|
-
const output = ['AS'];
|
|
473
|
-
if (node.colnames) {
|
|
474
|
-
output.push(this.quote(name) + parens(this.listQuotes(node.colnames)));
|
|
475
|
-
}
|
|
476
|
-
else {
|
|
477
|
-
output.push(this.quote(name));
|
|
478
|
-
}
|
|
479
|
-
return output.join(' ');
|
|
480
|
-
}
|
|
481
|
-
A_ArrayExpr(node) {
|
|
482
|
-
return (0, util_1.format)('ARRAY[%s]', this.list(node.elements));
|
|
483
|
-
}
|
|
484
|
-
A_Const(node, context = {}) {
|
|
485
|
-
if (node.val.String) {
|
|
486
|
-
return this.escape(this.deparse(node.val, context));
|
|
487
|
-
}
|
|
488
|
-
return this.deparse(node.val, context);
|
|
489
|
-
}
|
|
490
|
-
A_Indices(node, context = {}) {
|
|
491
|
-
if (node.lidx) {
|
|
492
|
-
return (0, util_1.format)('[%s:%s]', this.deparse(node.lidx, context), this.deparse(node.uidx, context));
|
|
493
|
-
}
|
|
494
|
-
return (0, util_1.format)('[%s]', this.deparse(node.uidx, context));
|
|
495
|
-
}
|
|
496
|
-
A_Indirection(node, context = {}) {
|
|
497
|
-
const output = [`(${this.deparse(node.arg, context)})`];
|
|
498
|
-
// TODO(zhm) figure out the actual rules for when a '.' is needed
|
|
499
|
-
//
|
|
500
|
-
// select a.b[0] from a;
|
|
501
|
-
// select (select row(1)).*
|
|
502
|
-
// select c2[2].f2 from comptable
|
|
503
|
-
// select c2.a[2].f2[1].f3[0].a1 from comptable
|
|
504
|
-
const indirection = unwrapList(node.indirection);
|
|
505
|
-
for (let i = 0; i < indirection.length; i++) {
|
|
506
|
-
const subnode = indirection[i];
|
|
507
|
-
if (subnode.String || subnode.A_Star) {
|
|
508
|
-
const value = subnode.A_Star ? '*' : this.quote(subnode.String.str);
|
|
509
|
-
output.push(`.${value}`);
|
|
510
|
-
}
|
|
511
|
-
else {
|
|
512
|
-
output.push(this.deparse(subnode, context));
|
|
513
|
-
}
|
|
514
|
-
}
|
|
515
|
-
return output.join('');
|
|
516
|
-
}
|
|
517
|
-
A_Star(node) {
|
|
518
|
-
return '*';
|
|
519
|
-
}
|
|
520
|
-
BitString(node) {
|
|
521
|
-
const prefix = node.str[0];
|
|
522
|
-
return `${prefix}'${node.str.substring(1)}'`;
|
|
523
|
-
}
|
|
524
|
-
BoolExpr(node, context = {}) {
|
|
525
|
-
let fmt_str = '%s';
|
|
526
|
-
if (context.bool) {
|
|
527
|
-
fmt_str = '(%s)';
|
|
528
|
-
}
|
|
529
|
-
const ctx = Object.assign({}, context);
|
|
530
|
-
ctx.bool = true;
|
|
531
|
-
switch (node.boolop) {
|
|
532
|
-
case 'AND_EXPR':
|
|
533
|
-
return (0, util_1.format)(fmt_str, this.list(node.args, ' AND ', '', ctx));
|
|
534
|
-
case 'OR_EXPR':
|
|
535
|
-
return (0, util_1.format)(fmt_str, this.list(node.args, ' OR ', '', ctx));
|
|
536
|
-
case 'NOT_EXPR':
|
|
537
|
-
return (0, util_1.format)('NOT (%s)', this.deparse(unwrapList(node.args)[0], context));
|
|
538
|
-
default:
|
|
539
|
-
return fail('BoolExpr', node);
|
|
540
|
-
}
|
|
541
|
-
}
|
|
542
|
-
BooleanTest(node, context = {}) {
|
|
543
|
-
const output = [];
|
|
544
|
-
const ctx = Object.assign({}, context);
|
|
545
|
-
ctx.bool = true;
|
|
546
|
-
output.push(this.deparse(node.arg, ctx));
|
|
547
|
-
switch (node.booltesttype) {
|
|
548
|
-
case 'IS_TRUE':
|
|
549
|
-
output.push('IS TRUE');
|
|
550
|
-
break;
|
|
551
|
-
case 'IS_NOT_TRUE':
|
|
552
|
-
output.push('IS NOT TRUE');
|
|
553
|
-
break;
|
|
554
|
-
case 'IS_FALSE':
|
|
555
|
-
output.push('IS FALSE');
|
|
556
|
-
break;
|
|
557
|
-
case 'IS_NOT_FALSE':
|
|
558
|
-
output.push('IS NOT FALSE');
|
|
559
|
-
break;
|
|
560
|
-
case 'IS_UNKNOWN':
|
|
561
|
-
output.push('IS UNKNOWN');
|
|
562
|
-
break;
|
|
563
|
-
case 'IS_NOT_UNKNOWN':
|
|
564
|
-
output.push('IS NOT UNKNOWN');
|
|
565
|
-
break;
|
|
566
|
-
}
|
|
567
|
-
return output.join(' ');
|
|
568
|
-
}
|
|
569
|
-
CaseExpr(node, context = {}) {
|
|
570
|
-
const output = ['CASE'];
|
|
571
|
-
if (node.arg) {
|
|
572
|
-
output.push(this.deparse(node.arg, context));
|
|
573
|
-
}
|
|
574
|
-
const args = unwrapList(node.args);
|
|
575
|
-
for (let i = 0; i < args.length; i++) {
|
|
576
|
-
output.push(this.deparse(args[i], context));
|
|
577
|
-
}
|
|
578
|
-
if (node.defresult) {
|
|
579
|
-
output.push('ELSE');
|
|
580
|
-
output.push(this.deparse(node.defresult, context));
|
|
581
|
-
}
|
|
582
|
-
output.push('END');
|
|
583
|
-
return output.join(' ');
|
|
584
|
-
}
|
|
585
|
-
CoalesceExpr(node, context = {}) {
|
|
586
|
-
return (0, util_1.format)('COALESCE(%s)', this.list(node.args, ', ', '', context));
|
|
587
|
-
}
|
|
588
|
-
CollateClause(node, context = {}) {
|
|
589
|
-
const output = [];
|
|
590
|
-
if (node.arg) {
|
|
591
|
-
output.push(this.deparse(node.arg, context));
|
|
592
|
-
}
|
|
593
|
-
output.push('COLLATE');
|
|
594
|
-
if (node.collname) {
|
|
595
|
-
output.push(this.quote(this.deparseNodes(node.collname, context)));
|
|
596
|
-
}
|
|
597
|
-
return output.join(' ');
|
|
598
|
-
}
|
|
599
|
-
CompositeTypeStmt(node, context = {}) {
|
|
600
|
-
const output = [];
|
|
601
|
-
output.push('CREATE TYPE');
|
|
602
|
-
output.push(this.RangeVar(node.typevar, context));
|
|
603
|
-
output.push('AS');
|
|
604
|
-
output.push('(');
|
|
605
|
-
output.push(this.list(node.coldeflist, `,${NEWLINE_CHAR}`, TAB_CHAR, context));
|
|
606
|
-
output.push(')');
|
|
607
|
-
return output.join(' ');
|
|
608
|
-
}
|
|
609
|
-
RenameStmt(node, context = {}) {
|
|
610
|
-
const output = [];
|
|
611
|
-
if (node.renameType === 'OBJECT_FUNCTION' ||
|
|
612
|
-
node.renameType === 'OBJECT_FOREIGN_TABLE' ||
|
|
613
|
-
node.renameType === 'OBJECT_FDW' ||
|
|
614
|
-
node.renameType === 'OBJECT_FOREIGN_SERVER') {
|
|
615
|
-
output.push('ALTER');
|
|
616
|
-
output.push((0, pgsql_enums_1.objtypeName)(node.renameType));
|
|
617
|
-
if (node.missing_ok) {
|
|
618
|
-
output.push('IF EXISTS');
|
|
619
|
-
}
|
|
620
|
-
output.push(this.deparse(node.object, context));
|
|
621
|
-
output.push('RENAME');
|
|
622
|
-
output.push('TO');
|
|
623
|
-
output.push(this.quote(node.newname));
|
|
624
|
-
}
|
|
625
|
-
else if (node.renameType === 'OBJECT_ATTRIBUTE') {
|
|
626
|
-
output.push('ALTER');
|
|
627
|
-
output.push((0, pgsql_enums_1.objtypeName)(node.relationType));
|
|
628
|
-
if (node.missing_ok) {
|
|
629
|
-
output.push('IF EXISTS');
|
|
630
|
-
}
|
|
631
|
-
output.push(this.RangeVar(node.relation, context));
|
|
632
|
-
output.push('RENAME');
|
|
633
|
-
output.push((0, pgsql_enums_1.objtypeName)(node.renameType));
|
|
634
|
-
output.push(this.quote(node.subname));
|
|
635
|
-
output.push('TO');
|
|
636
|
-
output.push(this.quote(node.newname));
|
|
637
|
-
}
|
|
638
|
-
else if (node.renameType === 'OBJECT_DOMAIN' ||
|
|
639
|
-
node.renameType === 'OBJECT_TYPE') {
|
|
640
|
-
output.push('ALTER');
|
|
641
|
-
output.push((0, pgsql_enums_1.objtypeName)(node.renameType));
|
|
642
|
-
if (node.missing_ok) {
|
|
643
|
-
output.push('IF EXISTS');
|
|
644
|
-
}
|
|
645
|
-
const typObj = {
|
|
646
|
-
TypeName: {
|
|
647
|
-
names: node.object
|
|
648
|
-
}
|
|
649
|
-
};
|
|
650
|
-
output.push(this.deparse(typObj, context));
|
|
651
|
-
output.push('RENAME');
|
|
652
|
-
output.push('TO');
|
|
653
|
-
output.push(this.quote(node.newname));
|
|
654
|
-
}
|
|
655
|
-
else if (node.renameType === 'OBJECT_SCHEMA') {
|
|
656
|
-
output.push('ALTER');
|
|
657
|
-
output.push((0, pgsql_enums_1.objtypeName)(node.renameType));
|
|
658
|
-
if (node.missing_ok) {
|
|
659
|
-
output.push('IF EXISTS');
|
|
660
|
-
}
|
|
661
|
-
output.push(this.quote(node.subname));
|
|
662
|
-
output.push('RENAME');
|
|
663
|
-
output.push('TO');
|
|
664
|
-
output.push(this.quote(node.newname));
|
|
665
|
-
}
|
|
666
|
-
else if (node.renameType === 'OBJECT_DOMCONSTRAINT') {
|
|
667
|
-
output.push('ALTER');
|
|
668
|
-
output.push('DOMAIN');
|
|
669
|
-
if (node.missing_ok) {
|
|
670
|
-
output.push('IF EXISTS');
|
|
671
|
-
}
|
|
672
|
-
const typObj = {
|
|
673
|
-
TypeName: {
|
|
674
|
-
names: node.object
|
|
675
|
-
}
|
|
676
|
-
};
|
|
677
|
-
output.push(this.deparse(typObj, context));
|
|
678
|
-
output.push('RENAME CONSTRAINT');
|
|
679
|
-
output.push(this.quote(node.subname));
|
|
680
|
-
output.push('TO');
|
|
681
|
-
output.push(this.quote(node.newname));
|
|
682
|
-
}
|
|
683
|
-
else {
|
|
684
|
-
output.push('ALTER');
|
|
685
|
-
output.push('TABLE');
|
|
686
|
-
if (node.missing_ok) {
|
|
687
|
-
output.push('IF EXISTS');
|
|
688
|
-
}
|
|
689
|
-
output.push(this.RangeVar(node.relation, context));
|
|
690
|
-
output.push('RENAME');
|
|
691
|
-
output.push(this.quote(node.subname));
|
|
692
|
-
output.push('TO');
|
|
693
|
-
output.push(this.quote(node.newname));
|
|
694
|
-
}
|
|
695
|
-
if (node.behavior === 'DROP_CASCADE') {
|
|
696
|
-
output.push('CASCADE');
|
|
697
|
-
}
|
|
698
|
-
return output.join(' ');
|
|
699
|
-
}
|
|
700
|
-
AlterOwnerStmt(node, context = {}) {
|
|
701
|
-
const output = [];
|
|
702
|
-
output.push('ALTER');
|
|
703
|
-
output.push((0, pgsql_enums_1.objtypeName)(node.objectType));
|
|
704
|
-
const unwrapped = unwrapList(node.object);
|
|
705
|
-
if (Array.isArray(unwrapped)) {
|
|
706
|
-
output.push(this.listQuotes(unwrapped, '.'));
|
|
707
|
-
}
|
|
708
|
-
else {
|
|
709
|
-
output.push(this.deparse(node.object, context));
|
|
710
|
-
}
|
|
711
|
-
output.push('OWNER TO');
|
|
712
|
-
output.push(this.RoleSpec(node.newowner, context));
|
|
713
|
-
return output.join(' ');
|
|
714
|
-
}
|
|
715
|
-
AlterObjectSchemaStmt(node, context = {}) {
|
|
716
|
-
const output = [];
|
|
717
|
-
if (node.objectType === 'OBJECT_TABLE') {
|
|
718
|
-
output.push('ALTER');
|
|
719
|
-
output.push((0, pgsql_enums_1.objtypeName)(node.objectType));
|
|
720
|
-
if (node.missing_ok) {
|
|
721
|
-
output.push('IF EXISTS');
|
|
722
|
-
}
|
|
723
|
-
output.push(this.RangeVar(node.relation, context));
|
|
724
|
-
output.push('SET SCHEMA');
|
|
725
|
-
output.push(this.quote(node.newschema));
|
|
726
|
-
}
|
|
727
|
-
else {
|
|
728
|
-
output.push('ALTER');
|
|
729
|
-
output.push((0, pgsql_enums_1.objtypeName)(node.objectType));
|
|
730
|
-
if (node.missing_ok) {
|
|
731
|
-
output.push('IF EXISTS');
|
|
732
|
-
}
|
|
733
|
-
const unwrapped = unwrapList(node.object);
|
|
734
|
-
if (Array.isArray(unwrapped)) {
|
|
735
|
-
output.push(this.listQuotes(unwrapped, '.'));
|
|
736
|
-
}
|
|
737
|
-
else {
|
|
738
|
-
output.push(this.deparse(node.object, context));
|
|
739
|
-
}
|
|
740
|
-
output.push('SET SCHEMA');
|
|
741
|
-
output.push(this.quote(node.newschema));
|
|
742
|
-
}
|
|
743
|
-
return output.join(' ');
|
|
744
|
-
}
|
|
745
|
-
ColumnDef(node, context = {}) {
|
|
746
|
-
const output = [this.quote(node.colname)];
|
|
747
|
-
output.push(this.TypeName(node.typeName, context));
|
|
748
|
-
if (node.raw_default) {
|
|
749
|
-
output.push('USING');
|
|
750
|
-
output.push(this.deparse(node.raw_default, context));
|
|
751
|
-
}
|
|
752
|
-
if (node.constraints) {
|
|
753
|
-
output.push(this.list(node.constraints, ' ', '', context));
|
|
754
|
-
}
|
|
755
|
-
if (node.collClause) {
|
|
756
|
-
output.push('COLLATE');
|
|
757
|
-
const str = unwrapList(node.collClause.collname)[0].String.str;
|
|
758
|
-
output.push(this.quote(str));
|
|
759
|
-
}
|
|
760
|
-
return compact(output).join(' ');
|
|
761
|
-
}
|
|
762
|
-
SQLValueFunction(node) {
|
|
763
|
-
if (node.op === 'SVFOP_CURRENT_DATE') {
|
|
764
|
-
return 'CURRENT_DATE';
|
|
765
|
-
}
|
|
766
|
-
if (node.op === 'SVFOP_CURRENT_TIMESTAMP') {
|
|
767
|
-
return 'CURRENT_TIMESTAMP';
|
|
768
|
-
}
|
|
769
|
-
if (node.op === 'SVFOP_CURRENT_USER') {
|
|
770
|
-
return 'CURRENT_USER';
|
|
771
|
-
}
|
|
772
|
-
if (node.op === 'SVFOP_SESSION_USER') {
|
|
773
|
-
return 'SESSION_USER';
|
|
774
|
-
}
|
|
775
|
-
throw new Error(`op=${node.op} SQLValueFunction not implemented`);
|
|
776
|
-
}
|
|
777
|
-
ColumnRef(node, context = {}) {
|
|
778
|
-
const KEYWORDS = ['old', 'new'];
|
|
779
|
-
const fields = unwrapList(node.fields).map((field) => {
|
|
780
|
-
if (field.String) {
|
|
781
|
-
const value = this.deparse(field, context);
|
|
782
|
-
if (context === 'trigger' && KEYWORDS.includes(value.toLowerCase())) {
|
|
783
|
-
return value.toUpperCase();
|
|
784
|
-
}
|
|
785
|
-
return this.quote(value);
|
|
786
|
-
}
|
|
787
|
-
return this.deparse(field, context);
|
|
788
|
-
});
|
|
789
|
-
return fields.join('.');
|
|
790
|
-
}
|
|
791
|
-
CommentStmt(node, context = {}) {
|
|
792
|
-
const output = [];
|
|
793
|
-
output.push('COMMENT');
|
|
794
|
-
output.push('ON');
|
|
795
|
-
output.push((0, pgsql_enums_1.objtypeName)(node.objtype));
|
|
796
|
-
const object = unwrapList(node.object);
|
|
797
|
-
if (node.objtype === 'OBJECT_CAST') {
|
|
798
|
-
output.push('(');
|
|
799
|
-
output.push(this.deparse(object[0], context));
|
|
800
|
-
output.push('AS');
|
|
801
|
-
output.push(this.deparse(object[1], context));
|
|
802
|
-
output.push(')');
|
|
803
|
-
}
|
|
804
|
-
else if (node.objtype === 'OBJECT_DOMCONSTRAINT') {
|
|
805
|
-
output.push(this.deparse(object[1], context));
|
|
806
|
-
output.push('ON');
|
|
807
|
-
output.push('DOMAIN');
|
|
808
|
-
output.push(this.deparse(object[0], context));
|
|
809
|
-
}
|
|
810
|
-
else if (node.objtype === 'OBJECT_OPCLASS' ||
|
|
811
|
-
node.objtype === 'OBJECT_OPFAMILY') {
|
|
812
|
-
output.push(this.deparse(object[1], context));
|
|
813
|
-
output.push('USING');
|
|
814
|
-
output.push(this.deparse(object[0], context));
|
|
815
|
-
}
|
|
816
|
-
else if (node.objtype === 'OBJECT_OPERATOR') {
|
|
817
|
-
output.push(this.deparse(object, 'noquotes'));
|
|
818
|
-
}
|
|
819
|
-
else if (node.objtype === 'OBJECT_POLICY') {
|
|
820
|
-
output.push(this.deparse(object[1], context));
|
|
821
|
-
output.push('ON');
|
|
822
|
-
output.push(this.deparse(object[0], context));
|
|
823
|
-
}
|
|
824
|
-
else if (node.objtype === 'OBJECT_ROLE') {
|
|
825
|
-
output.push(this.deparse(object, context));
|
|
826
|
-
}
|
|
827
|
-
else if (node.objtype === 'OBJECT_RULE') {
|
|
828
|
-
output.push(this.deparse(object[1], context));
|
|
829
|
-
output.push('ON');
|
|
830
|
-
output.push(this.deparse(object[0], context));
|
|
831
|
-
}
|
|
832
|
-
else if (node.objtype === 'OBJECT_TABCONSTRAINT') {
|
|
833
|
-
if (object.length === 3) {
|
|
834
|
-
output.push(this.deparse(object[2], context));
|
|
835
|
-
output.push('ON');
|
|
836
|
-
output.push(this.deparse(object[0], context) +
|
|
837
|
-
'.' +
|
|
838
|
-
this.deparse(object[1], context));
|
|
839
|
-
}
|
|
840
|
-
else {
|
|
841
|
-
output.push(this.deparse(object[1], context));
|
|
842
|
-
output.push('ON');
|
|
843
|
-
output.push(this.deparse(object[0], context));
|
|
844
|
-
}
|
|
845
|
-
}
|
|
846
|
-
else if (node.objtype === 'OBJECT_TRANSFORM') {
|
|
847
|
-
output.push('FOR');
|
|
848
|
-
output.push(this.deparse(object[0], context));
|
|
849
|
-
output.push('LANGUAGE');
|
|
850
|
-
output.push(this.deparse(object[1], context));
|
|
851
|
-
}
|
|
852
|
-
else if (node.objtype === 'OBJECT_TRIGGER') {
|
|
853
|
-
output.push(this.deparse(object[1], context));
|
|
854
|
-
output.push('ON');
|
|
855
|
-
output.push(this.deparse(object[0], context));
|
|
856
|
-
}
|
|
857
|
-
else {
|
|
858
|
-
if (node.objtype === 'OBJECT_LARGEOBJECT') {
|
|
859
|
-
output.push(dotty.get(node, 'object.Integer.ival'));
|
|
860
|
-
}
|
|
861
|
-
else if (object instanceof Array) {
|
|
862
|
-
output.push(this.listQuotes(object, '.'));
|
|
863
|
-
}
|
|
864
|
-
else {
|
|
865
|
-
output.push(this.deparse(object, context));
|
|
866
|
-
}
|
|
867
|
-
if (node.objargs) {
|
|
868
|
-
output.push('(');
|
|
869
|
-
output.push(this.list(node.objargs, ', ', '', context));
|
|
870
|
-
output.push(')');
|
|
871
|
-
}
|
|
872
|
-
}
|
|
873
|
-
output.push('IS');
|
|
874
|
-
const escapeComment = (str) => {
|
|
875
|
-
return str.replace(/\\/g, '\\');
|
|
876
|
-
};
|
|
877
|
-
if (node.comment) {
|
|
878
|
-
if (/[^a-zA-Z0-9]/.test(node.comment)) {
|
|
879
|
-
// special chars we care about...
|
|
880
|
-
output.push(`E'${escapeComment(node.comment)}'`);
|
|
881
|
-
}
|
|
882
|
-
else {
|
|
883
|
-
// find a double \\n or \\ something...
|
|
884
|
-
output.push(`'${node.comment}'`);
|
|
885
|
-
}
|
|
886
|
-
}
|
|
887
|
-
else {
|
|
888
|
-
output.push('NULL');
|
|
889
|
-
}
|
|
890
|
-
return output.join(' ');
|
|
891
|
-
}
|
|
892
|
-
CommonTableExpr(node, context = {}) {
|
|
893
|
-
const output = [];
|
|
894
|
-
output.push(node.ctename);
|
|
895
|
-
if (node.aliascolnames) {
|
|
896
|
-
const colnames = this.quote(this.deparseNodes(node.aliascolnames, context));
|
|
897
|
-
output.push(`(${colnames.join(', ')})`);
|
|
898
|
-
}
|
|
899
|
-
output.push('AS');
|
|
900
|
-
if (node.ctematerialized === 'CTEMaterializeAlways') {
|
|
901
|
-
output.push('MATERIALIZED');
|
|
902
|
-
}
|
|
903
|
-
else if (node.ctematerialized === 'CTEMaterializeNever') {
|
|
904
|
-
output.push('NOT MATERIALIZED');
|
|
905
|
-
}
|
|
906
|
-
output.push((0, util_1.format)('(%s)', this.deparse(node.ctequery)));
|
|
907
|
-
return output.join(' ');
|
|
908
|
-
}
|
|
909
|
-
DefineStmt(node, context = {}) {
|
|
910
|
-
const output = [];
|
|
911
|
-
output.push('CREATE');
|
|
912
|
-
if (node.replace) {
|
|
913
|
-
output.push('OR REPLACE');
|
|
914
|
-
}
|
|
915
|
-
switch (node.kind) {
|
|
916
|
-
case 'OBJECT_AGGREGATE':
|
|
917
|
-
output.push('AGGREGATE');
|
|
918
|
-
break;
|
|
919
|
-
case 'OBJECT_OPERATOR':
|
|
920
|
-
output.push('OPERATOR');
|
|
921
|
-
break;
|
|
922
|
-
case 'OBJECT_TYPE':
|
|
923
|
-
output.push('TYPE');
|
|
924
|
-
break;
|
|
925
|
-
case 'OBJECT_TSPARSER':
|
|
926
|
-
output.push('TEXT SEARCH PARSER');
|
|
927
|
-
break;
|
|
928
|
-
case 'OBJECT_TSDICTIONARY':
|
|
929
|
-
output.push('TEXT SEARCH DICTIONARY');
|
|
930
|
-
break;
|
|
931
|
-
case 'OBJECT_TSTEMPLATE':
|
|
932
|
-
output.push('TEXT SEARCH TEMPLATE');
|
|
933
|
-
break;
|
|
934
|
-
case 'OBJECT_TSCONFIGURATION':
|
|
935
|
-
output.push('TEXT SEARCH CONFIGURATION');
|
|
936
|
-
break;
|
|
937
|
-
case 'OBJECT_COLLATION':
|
|
938
|
-
output.push('COLLATION');
|
|
939
|
-
break;
|
|
940
|
-
default:
|
|
941
|
-
throw new Error('DefineStmt not recognized');
|
|
942
|
-
}
|
|
943
|
-
if (node.if_not_exists) {
|
|
944
|
-
output.push('IF NOT EXISTS');
|
|
945
|
-
}
|
|
946
|
-
switch (node.kind) {
|
|
947
|
-
case 'OBJECT_AGGREGATE':
|
|
948
|
-
// output.push(this.deparse(node.defnames));
|
|
949
|
-
output.push(this.list(node.defnames, '.', '', context));
|
|
950
|
-
break;
|
|
951
|
-
case 'OBJECT_OPERATOR':
|
|
952
|
-
output.push(this.list(node.defnames, '.', '', context));
|
|
953
|
-
// output.push(this.deparse(node.defnames));
|
|
954
|
-
break;
|
|
955
|
-
case 'OBJECT_TYPE':
|
|
956
|
-
case 'OBJECT_TSPARSER':
|
|
957
|
-
case 'OBJECT_TSDICTIONARY':
|
|
958
|
-
case 'OBJECT_TSTEMPLATE':
|
|
959
|
-
case 'OBJECT_TSCONFIGURATION':
|
|
960
|
-
case 'OBJECT_COLLATION':
|
|
961
|
-
output.push(this.deparse(node.defnames));
|
|
962
|
-
break;
|
|
963
|
-
default:
|
|
964
|
-
throw new Error('DefineStmt not recognized');
|
|
965
|
-
}
|
|
966
|
-
if (!node.oldstyle && node.kind == 'OBJECT_AGGREGATE') {
|
|
967
|
-
output.push('(');
|
|
968
|
-
output.push(`${this.listQuotes(node.args[0], ',')}`);
|
|
969
|
-
output.push(')');
|
|
970
|
-
}
|
|
971
|
-
const definition = unwrapList(node.definition);
|
|
972
|
-
if (definition.length > 0) {
|
|
973
|
-
output.push('(');
|
|
974
|
-
for (let n = 0; n < definition.length; n++) {
|
|
975
|
-
const defElement = definition[n].DefElem;
|
|
976
|
-
output.push(defElement.defname);
|
|
977
|
-
if (defElement.arg) {
|
|
978
|
-
output.push('=');
|
|
979
|
-
output.push(this.deparse(defElement.arg));
|
|
980
|
-
}
|
|
981
|
-
if (n !== definition.length - 1) {
|
|
982
|
-
output.push(',');
|
|
983
|
-
}
|
|
984
|
-
}
|
|
985
|
-
output.push(')');
|
|
986
|
-
}
|
|
987
|
-
return output.join(' ');
|
|
988
|
-
}
|
|
989
|
-
DefElem(node, context = {}) {
|
|
990
|
-
if (node.defname === 'transaction_isolation') {
|
|
991
|
-
return (0, util_1.format)('ISOLATION LEVEL %s', node.arg.A_Const.val.String.str.toUpperCase());
|
|
992
|
-
}
|
|
993
|
-
if (node.defname === 'transaction_read_only') {
|
|
994
|
-
return node.arg.A_Const.val.Integer.ival === 0
|
|
995
|
-
? 'READ WRITE'
|
|
996
|
-
: 'READ ONLY';
|
|
997
|
-
}
|
|
998
|
-
if (node.defname === 'transaction_deferrable') {
|
|
999
|
-
return node.arg.A_Const.val.Integer.ival === 0
|
|
1000
|
-
? 'NOT DEFERRABLE'
|
|
1001
|
-
: 'DEFERRABLE';
|
|
1002
|
-
}
|
|
1003
|
-
if (node.defname === 'set') {
|
|
1004
|
-
return this.deparse(node.arg, context);
|
|
1005
|
-
}
|
|
1006
|
-
let name = node.defname;
|
|
1007
|
-
if (node.defnamespace) {
|
|
1008
|
-
name = `${node.defnamespace}.${node.defname}`;
|
|
1009
|
-
}
|
|
1010
|
-
if (context === 'generated' || context === 'sequence') {
|
|
1011
|
-
switch (name) {
|
|
1012
|
-
case 'start': {
|
|
1013
|
-
const start = this.deparse(node.arg, context);
|
|
1014
|
-
return `START WITH ${start}`;
|
|
1015
|
-
}
|
|
1016
|
-
case 'increment': {
|
|
1017
|
-
const inc = this.deparse(node.arg, context);
|
|
1018
|
-
if (context === 'sequence') {
|
|
1019
|
-
// we need 'simple' so it doesn't wrap negative numbers in parens
|
|
1020
|
-
return `${name} ${this.deparse(node.arg, 'simple')}`;
|
|
1021
|
-
}
|
|
1022
|
-
return `INCREMENT BY ${inc}`;
|
|
1023
|
-
}
|
|
1024
|
-
case 'sequence_name': {
|
|
1025
|
-
return `SEQUENCE NAME ${this.listQuotes(node.arg, '.')}`;
|
|
1026
|
-
}
|
|
1027
|
-
case 'cycle': {
|
|
1028
|
-
const on = this.deparse(node.arg, context) + '' === '1';
|
|
1029
|
-
return on ? 'CYCLE' : 'NO CYCLE';
|
|
1030
|
-
}
|
|
1031
|
-
case 'minvalue': {
|
|
1032
|
-
const off = !node.hasOwnProperty('arg');
|
|
1033
|
-
return off
|
|
1034
|
-
? 'NO MINVALUE'
|
|
1035
|
-
: `${name} ${this.deparse(node.arg, 'simple')}`;
|
|
1036
|
-
}
|
|
1037
|
-
case 'maxvalue': {
|
|
1038
|
-
const off = !node.hasOwnProperty('arg');
|
|
1039
|
-
return off
|
|
1040
|
-
? 'NO MAXVALUE'
|
|
1041
|
-
: `${name} ${this.deparse(node.arg, 'simple')}`;
|
|
1042
|
-
}
|
|
1043
|
-
// alter
|
|
1044
|
-
case 'owned_by': {
|
|
1045
|
-
const output = [];
|
|
1046
|
-
unwrapList(node.arg).forEach((opt) => {
|
|
1047
|
-
output.push(this.quote(this.deparse(opt, context)));
|
|
1048
|
-
});
|
|
1049
|
-
return `OWNED BY ${output.join('.')}`;
|
|
1050
|
-
}
|
|
1051
|
-
// alter
|
|
1052
|
-
case 'restart': {
|
|
1053
|
-
if (node.arg) {
|
|
1054
|
-
return `RESTART WITH ${this.deparse(node.arg, context)}`;
|
|
1055
|
-
}
|
|
1056
|
-
return `RESTART`;
|
|
1057
|
-
}
|
|
1058
|
-
default:
|
|
1059
|
-
if (node.arg) {
|
|
1060
|
-
// we need 'simple' so it doesn't wrap negative numbers in parens
|
|
1061
|
-
return `${name} ${this.deparse(node.arg, 'simple')}`;
|
|
1062
|
-
}
|
|
1063
|
-
}
|
|
1064
|
-
}
|
|
1065
|
-
else if (context === 'explain') {
|
|
1066
|
-
if (node.arg) {
|
|
1067
|
-
return `${name} ${this.deparse(node.arg)}`;
|
|
1068
|
-
}
|
|
1069
|
-
}
|
|
1070
|
-
else if (node.arg) {
|
|
1071
|
-
return `${name} = ${this.deparse(node.arg, context)}`;
|
|
1072
|
-
}
|
|
1073
|
-
return name;
|
|
1074
|
-
}
|
|
1075
|
-
DoStmt(node) {
|
|
1076
|
-
return `DO $$${NEWLINE_CHAR} ${dotty
|
|
1077
|
-
.get(node, 'args.0.DefElem.arg.String.str')
|
|
1078
|
-
.trim()} $$`;
|
|
1079
|
-
}
|
|
1080
|
-
Float(node) {
|
|
1081
|
-
// wrap negative numbers in parens, SELECT (-2147483648)::int4 * (-1)::int4
|
|
1082
|
-
if (node.str[0] === '-') {
|
|
1083
|
-
return `(${node.str})`;
|
|
1084
|
-
}
|
|
1085
|
-
return node.str;
|
|
1086
|
-
}
|
|
1087
|
-
FuncCall(node, context = {}) {
|
|
1088
|
-
const output = [];
|
|
1089
|
-
let params = [];
|
|
1090
|
-
if (node.args) {
|
|
1091
|
-
params = unwrapList(node.args).map((item) => {
|
|
1092
|
-
return this.deparse(item, context);
|
|
1093
|
-
});
|
|
1094
|
-
}
|
|
1095
|
-
// COUNT(*)
|
|
1096
|
-
if (node.agg_star) {
|
|
1097
|
-
params.push('*');
|
|
1098
|
-
}
|
|
1099
|
-
const name = this.list(node.funcname, '.', '', context);
|
|
1100
|
-
const order = [];
|
|
1101
|
-
const withinGroup = node.agg_within_group;
|
|
1102
|
-
if (node.agg_order) {
|
|
1103
|
-
order.push('ORDER BY');
|
|
1104
|
-
order.push(this.list(node.agg_order, ', ', '', context));
|
|
1105
|
-
}
|
|
1106
|
-
const call = [];
|
|
1107
|
-
call.push(name + '(');
|
|
1108
|
-
if (node.agg_distinct) {
|
|
1109
|
-
call.push('DISTINCT ');
|
|
1110
|
-
}
|
|
1111
|
-
// prepend variadic before the last parameter
|
|
1112
|
-
// SELECT CONCAT('|', VARIADIC ARRAY['1','2','3'])
|
|
1113
|
-
if (node.func_variadic) {
|
|
1114
|
-
params[params.length - 1] = 'VARIADIC ' + params[params.length - 1];
|
|
1115
|
-
}
|
|
1116
|
-
call.push(params.join(', '));
|
|
1117
|
-
if (order.length && !withinGroup) {
|
|
1118
|
-
call.push(' ');
|
|
1119
|
-
call.push(order.join(' '));
|
|
1120
|
-
}
|
|
1121
|
-
call.push(')');
|
|
1122
|
-
output.push(compact(call).join(''));
|
|
1123
|
-
if (order.length && withinGroup) {
|
|
1124
|
-
output.push('WITHIN GROUP');
|
|
1125
|
-
output.push(parens(order.join(' ')));
|
|
1126
|
-
}
|
|
1127
|
-
if (node.agg_filter != null) {
|
|
1128
|
-
output.push((0, util_1.format)('FILTER (WHERE %s)', this.deparse(node.agg_filter, context)));
|
|
1129
|
-
}
|
|
1130
|
-
if (node.over != null) {
|
|
1131
|
-
output.push((0, util_1.format)('OVER %s', this.WindowDef(node.over, context)));
|
|
1132
|
-
}
|
|
1133
|
-
return output.join(' ');
|
|
1134
|
-
}
|
|
1135
|
-
GroupingFunc(node, context = {}) {
|
|
1136
|
-
return 'GROUPING(' + this.list(node.args, ', ', '', context) + ')';
|
|
1137
|
-
}
|
|
1138
|
-
GroupingSet(node, context = {}) {
|
|
1139
|
-
switch (node.kind) {
|
|
1140
|
-
case 'GROUPING_SET_EMPTY':
|
|
1141
|
-
return '()';
|
|
1142
|
-
case 'GROUPING_SET_SIMPLE':
|
|
1143
|
-
return fail('GroupingSet', node);
|
|
1144
|
-
case 'GROUPING_SET_ROLLUP':
|
|
1145
|
-
return 'ROLLUP (' + this.list(node.content, ', ', '', context) + ')';
|
|
1146
|
-
case 'GROUPING_SET_CUBE':
|
|
1147
|
-
return 'CUBE (' + this.list(node.content, ', ', '', context) + ')';
|
|
1148
|
-
case 'GROUPING_SET_SETS':
|
|
1149
|
-
return ('GROUPING SETS (' + this.list(node.content, ', ', '', context) + ')');
|
|
1150
|
-
default:
|
|
1151
|
-
return fail('GroupingSet', node);
|
|
1152
|
-
}
|
|
1153
|
-
}
|
|
1154
|
-
IndexStmt(node, context = {}) {
|
|
1155
|
-
const output = [];
|
|
1156
|
-
output.push('CREATE');
|
|
1157
|
-
if (node.unique) {
|
|
1158
|
-
output.push('UNIQUE');
|
|
1159
|
-
}
|
|
1160
|
-
output.push('INDEX');
|
|
1161
|
-
if (node.concurrent) {
|
|
1162
|
-
output.push('CONCURRENTLY');
|
|
1163
|
-
}
|
|
1164
|
-
if (node.idxname) {
|
|
1165
|
-
output.push(node.idxname);
|
|
1166
|
-
}
|
|
1167
|
-
output.push('ON');
|
|
1168
|
-
output.push(this.RangeVar(node.relation, context));
|
|
1169
|
-
if (node.accessMethod) {
|
|
1170
|
-
const accessMethod = node.accessMethod.toUpperCase();
|
|
1171
|
-
if (accessMethod !== 'BTREE') {
|
|
1172
|
-
output.push('USING');
|
|
1173
|
-
output.push(accessMethod);
|
|
1174
|
-
}
|
|
1175
|
-
}
|
|
1176
|
-
if (node.indexParams) {
|
|
1177
|
-
output.push('(');
|
|
1178
|
-
output.push(this.list(node.indexParams, ', ', '', context));
|
|
1179
|
-
output.push(')');
|
|
1180
|
-
}
|
|
1181
|
-
if (node.indexIncludingParams) {
|
|
1182
|
-
output.push('INCLUDE (');
|
|
1183
|
-
output.push(this.list(node.indexIncludingParams, ', ', '', context));
|
|
1184
|
-
output.push(')');
|
|
1185
|
-
}
|
|
1186
|
-
if (node.whereClause) {
|
|
1187
|
-
output.push('WHERE');
|
|
1188
|
-
output.push(this.deparse(node.whereClause, context));
|
|
1189
|
-
}
|
|
1190
|
-
return output.join(' ');
|
|
1191
|
-
}
|
|
1192
|
-
IndexElem(node, context = {}) {
|
|
1193
|
-
const output = [];
|
|
1194
|
-
if (node.name) {
|
|
1195
|
-
output.push(node.name);
|
|
1196
|
-
if (node.ordering === 'SORTBY_DESC') {
|
|
1197
|
-
output.push('DESC');
|
|
1198
|
-
}
|
|
1199
|
-
else if (node.ordering === 'SORTBY_ASC') {
|
|
1200
|
-
output.push('ASC');
|
|
1201
|
-
}
|
|
1202
|
-
return output.join(' ');
|
|
1203
|
-
}
|
|
1204
|
-
if (node.expr) {
|
|
1205
|
-
return this.deparse(node.expr, context);
|
|
1206
|
-
}
|
|
1207
|
-
return fail('IndexElem', node);
|
|
1208
|
-
}
|
|
1209
|
-
InsertStmt(node, context = {}) {
|
|
1210
|
-
const output = [];
|
|
1211
|
-
if (node.withClause) {
|
|
1212
|
-
output.push(this.WithClause(node.withClause, context));
|
|
1213
|
-
}
|
|
1214
|
-
output.push('INSERT INTO');
|
|
1215
|
-
output.push(this.RangeVar(node.relation, context));
|
|
1216
|
-
const cols = unwrapList(node.cols);
|
|
1217
|
-
if (cols && cols.length) {
|
|
1218
|
-
output.push('(');
|
|
1219
|
-
output.push(this.list(cols, ', ', '', context));
|
|
1220
|
-
output.push(')');
|
|
1221
|
-
}
|
|
1222
|
-
if (node.selectStmt) {
|
|
1223
|
-
output.push(this.deparse(node.selectStmt, context));
|
|
1224
|
-
}
|
|
1225
|
-
else {
|
|
1226
|
-
output.push('DEFAULT VALUES');
|
|
1227
|
-
}
|
|
1228
|
-
if (node.onConflictClause) {
|
|
1229
|
-
const clause = node.onConflictClause;
|
|
1230
|
-
output.push('ON CONFLICT');
|
|
1231
|
-
if (clause.infer.indexElems) {
|
|
1232
|
-
output.push('(');
|
|
1233
|
-
output.push(this.list(clause.infer.indexElems, ', ', '', context));
|
|
1234
|
-
output.push(')');
|
|
1235
|
-
}
|
|
1236
|
-
else if (clause.infer.conname) {
|
|
1237
|
-
output.push('ON CONSTRAINT');
|
|
1238
|
-
output.push(clause.infer.conname);
|
|
1239
|
-
}
|
|
1240
|
-
switch (clause.action) {
|
|
1241
|
-
case 'ONCONFLICT_NOTHING':
|
|
1242
|
-
output.push('DO NOTHING');
|
|
1243
|
-
break;
|
|
1244
|
-
case 'ONCONFLICT_UPDATE':
|
|
1245
|
-
output.push('DO');
|
|
1246
|
-
output.push(this.UpdateStmt(clause, context));
|
|
1247
|
-
break;
|
|
1248
|
-
default:
|
|
1249
|
-
throw new Error('unhandled CONFLICT CLAUSE');
|
|
1250
|
-
}
|
|
1251
|
-
}
|
|
1252
|
-
if (node.returningList) {
|
|
1253
|
-
output.push('RETURNING');
|
|
1254
|
-
output.push(this.deparseReturningList(node.returningList, context));
|
|
1255
|
-
}
|
|
1256
|
-
return output.join(' ');
|
|
1257
|
-
}
|
|
1258
|
-
SetToDefault(node) {
|
|
1259
|
-
return 'DEFAULT';
|
|
1260
|
-
}
|
|
1261
|
-
MultiAssignRef(node, context = {}) {
|
|
1262
|
-
const output = [];
|
|
1263
|
-
output.push(this.deparse(node.source, context));
|
|
1264
|
-
return output.join(' ');
|
|
1265
|
-
}
|
|
1266
|
-
DeleteStmt(node, context = {}) {
|
|
1267
|
-
const output = [''];
|
|
1268
|
-
if (node.withClause) {
|
|
1269
|
-
output.push(this.WithClause(node.withClause, context));
|
|
1270
|
-
}
|
|
1271
|
-
output.push('DELETE');
|
|
1272
|
-
output.push('FROM');
|
|
1273
|
-
output.push(this.RangeVar(node.relation, context));
|
|
1274
|
-
if (node.usingClause) {
|
|
1275
|
-
output.push('USING');
|
|
1276
|
-
output.push(this.list(node.usingClause, ', ', '', context));
|
|
1277
|
-
}
|
|
1278
|
-
if (node.whereClause) {
|
|
1279
|
-
output.push('WHERE');
|
|
1280
|
-
output.push(this.deparse(node.whereClause, context));
|
|
1281
|
-
}
|
|
1282
|
-
if (node.returningList) {
|
|
1283
|
-
output.push('RETURNING');
|
|
1284
|
-
output.push(this.deparseReturningList(node.returningList, context));
|
|
1285
|
-
}
|
|
1286
|
-
return output.join(' ');
|
|
1287
|
-
}
|
|
1288
|
-
UpdateStmt(node, context = {}) {
|
|
1289
|
-
const output = [];
|
|
1290
|
-
if (node.withClause) {
|
|
1291
|
-
output.push(this.WithClause(node.withClause, context));
|
|
1292
|
-
}
|
|
1293
|
-
output.push('UPDATE');
|
|
1294
|
-
if (node.relation) {
|
|
1295
|
-
// onConflictClause no relation..
|
|
1296
|
-
output.push(this.RangeVar(node.relation, context));
|
|
1297
|
-
}
|
|
1298
|
-
output.push('SET');
|
|
1299
|
-
const targetList = unwrapList(node.targetList);
|
|
1300
|
-
if (targetList && targetList.length) {
|
|
1301
|
-
if (targetList[0].ResTarget &&
|
|
1302
|
-
targetList[0].ResTarget.val &&
|
|
1303
|
-
targetList[0].ResTarget.val.MultiAssignRef) {
|
|
1304
|
-
output.push('(');
|
|
1305
|
-
output.push(targetList.map((target) => target.ResTarget.name).join(','));
|
|
1306
|
-
output.push(')');
|
|
1307
|
-
output.push('=');
|
|
1308
|
-
output.push(this.deparse(targetList[0].ResTarget.val, context));
|
|
1309
|
-
}
|
|
1310
|
-
else {
|
|
1311
|
-
output.push(targetList.map((target) => this.deparse(target, 'update')).join(','));
|
|
1312
|
-
}
|
|
1313
|
-
}
|
|
1314
|
-
if (node.fromClause) {
|
|
1315
|
-
output.push('FROM');
|
|
1316
|
-
output.push(this.list(node.fromClause, ', ', '', context));
|
|
1317
|
-
}
|
|
1318
|
-
if (node.whereClause) {
|
|
1319
|
-
output.push('WHERE');
|
|
1320
|
-
output.push(this.deparse(node.whereClause, context));
|
|
1321
|
-
}
|
|
1322
|
-
if (node.returningList) {
|
|
1323
|
-
output.push('RETURNING');
|
|
1324
|
-
output.push(this.deparseReturningList(node.returningList, context));
|
|
1325
|
-
}
|
|
1326
|
-
return output.join(' ');
|
|
1327
|
-
}
|
|
1328
|
-
Integer(node, context = {}) {
|
|
1329
|
-
if (node.ival < 0 && context !== 'simple') {
|
|
1330
|
-
return `(${node.ival})`;
|
|
1331
|
-
}
|
|
1332
|
-
return node.ival.toString();
|
|
1333
|
-
}
|
|
1334
|
-
IntoClause(node, context = {}) {
|
|
1335
|
-
return this.RangeVar(node.rel, context);
|
|
1336
|
-
}
|
|
1337
|
-
JoinExpr(node, context = {}) {
|
|
1338
|
-
const output = [];
|
|
1339
|
-
output.push(this.deparse(node.larg, context));
|
|
1340
|
-
if (node.isNatural) {
|
|
1341
|
-
output.push('NATURAL');
|
|
1342
|
-
}
|
|
1343
|
-
let join = null;
|
|
1344
|
-
switch (true) {
|
|
1345
|
-
case node.jointype === 'JOIN_INNER' && node.quals != null:
|
|
1346
|
-
join = 'INNER JOIN';
|
|
1347
|
-
break;
|
|
1348
|
-
case node.jointype === 'JOIN_INNER' &&
|
|
1349
|
-
!node.isNatural &&
|
|
1350
|
-
!(node.quals != null) &&
|
|
1351
|
-
!(node.usingClause != null):
|
|
1352
|
-
join = 'CROSS JOIN';
|
|
1353
|
-
break;
|
|
1354
|
-
case node.jointype === 'JOIN_INNER':
|
|
1355
|
-
join = 'JOIN';
|
|
1356
|
-
break;
|
|
1357
|
-
case node.jointype === 'JOIN_LEFT':
|
|
1358
|
-
join = 'LEFT OUTER JOIN';
|
|
1359
|
-
break;
|
|
1360
|
-
case node.jointype === 'JOIN_FULL':
|
|
1361
|
-
join = 'FULL OUTER JOIN';
|
|
1362
|
-
break;
|
|
1363
|
-
case node.jointype === 'JOIN_RIGHT':
|
|
1364
|
-
join = 'RIGHT OUTER JOIN';
|
|
1365
|
-
break;
|
|
1366
|
-
default:
|
|
1367
|
-
fail('JoinExpr', node);
|
|
1368
|
-
break;
|
|
1369
|
-
}
|
|
1370
|
-
output.push(join);
|
|
1371
|
-
if (node.rarg) {
|
|
1372
|
-
// wrap nested join expressions in parens to make the following symmetric:
|
|
1373
|
-
// select * from int8_tbl x cross join (int4_tbl x cross join lateral (select x.f1) ss)
|
|
1374
|
-
if (node.rarg.JoinExpr != null && !(node.rarg.JoinExpr.alias != null)) {
|
|
1375
|
-
output.push(`(${this.deparse(node.rarg, context)})`);
|
|
1376
|
-
}
|
|
1377
|
-
else {
|
|
1378
|
-
output.push(this.deparse(node.rarg, context));
|
|
1379
|
-
}
|
|
1380
|
-
}
|
|
1381
|
-
if (node.quals) {
|
|
1382
|
-
output.push(`ON ${this.deparse(node.quals, context)}`);
|
|
1383
|
-
}
|
|
1384
|
-
if (node.usingClause) {
|
|
1385
|
-
const using = this.quote(this.deparseNodes(node.usingClause, context)).join(', ');
|
|
1386
|
-
output.push(`USING (${using})`);
|
|
1387
|
-
}
|
|
1388
|
-
const wrapped = node.rarg.JoinExpr != null || node.alias
|
|
1389
|
-
? '(' + output.join(' ') + ')'
|
|
1390
|
-
: output.join(' ');
|
|
1391
|
-
if (node.alias) {
|
|
1392
|
-
return wrapped + ' ' + this.Alias(node.alias, context);
|
|
1393
|
-
}
|
|
1394
|
-
return wrapped;
|
|
1395
|
-
}
|
|
1396
|
-
LockingClause(node, context = {}) {
|
|
1397
|
-
const output = [];
|
|
1398
|
-
switch (node.strength) {
|
|
1399
|
-
case 'LCS_NONE':
|
|
1400
|
-
output.push('NONE');
|
|
1401
|
-
break;
|
|
1402
|
-
case 'LCS_FORKEYSHARE':
|
|
1403
|
-
output.push('FOR KEY SHARE');
|
|
1404
|
-
break;
|
|
1405
|
-
case 'LCS_FORSHARE':
|
|
1406
|
-
output.push('FOR SHARE');
|
|
1407
|
-
break;
|
|
1408
|
-
case 'LCS_FORNOKEYUPDATE':
|
|
1409
|
-
output.push('FOR NO KEY UPDATE');
|
|
1410
|
-
break;
|
|
1411
|
-
case 'LCS_FORUPDATE':
|
|
1412
|
-
output.push('FOR UPDATE');
|
|
1413
|
-
break;
|
|
1414
|
-
default:
|
|
1415
|
-
return fail('LockingClause', node);
|
|
1416
|
-
}
|
|
1417
|
-
if (node.lockedRels) {
|
|
1418
|
-
output.push('OF');
|
|
1419
|
-
output.push(this.list(node.lockedRels, ', ', '', context));
|
|
1420
|
-
}
|
|
1421
|
-
return output.join(' ');
|
|
1422
|
-
}
|
|
1423
|
-
LockStmt(node, context = {}) {
|
|
1424
|
-
const output = ['LOCK'];
|
|
1425
|
-
output.push(this.list(node.relations, ', ', '', { lock: true }));
|
|
1426
|
-
output.push('IN');
|
|
1427
|
-
output.push(LOCK_MODES[node.mode]);
|
|
1428
|
-
output.push('MODE');
|
|
1429
|
-
if (node.nowait) {
|
|
1430
|
-
output.push('NOWAIT');
|
|
1431
|
-
}
|
|
1432
|
-
return output.join(' ');
|
|
1433
|
-
}
|
|
1434
|
-
MinMaxExpr(node, context = {}) {
|
|
1435
|
-
const output = [];
|
|
1436
|
-
if (node.op === 'IS_GREATEST') {
|
|
1437
|
-
output.push('GREATEST');
|
|
1438
|
-
}
|
|
1439
|
-
else {
|
|
1440
|
-
output.push('LEAST');
|
|
1441
|
-
}
|
|
1442
|
-
output.push(parens(this.list(node.args, ', ', '', context)));
|
|
1443
|
-
return output.join('');
|
|
1444
|
-
}
|
|
1445
|
-
NamedArgExpr(node, context = {}) {
|
|
1446
|
-
const output = [];
|
|
1447
|
-
output.push(node.name);
|
|
1448
|
-
output.push(':=');
|
|
1449
|
-
output.push(this.deparse(node.arg, context));
|
|
1450
|
-
return output.join(' ');
|
|
1451
|
-
}
|
|
1452
|
-
Null(node) {
|
|
1453
|
-
return 'NULL';
|
|
1454
|
-
}
|
|
1455
|
-
NullTest(node, context = {}) {
|
|
1456
|
-
const output = [this.deparse(node.arg, context)];
|
|
1457
|
-
if (node.nulltesttype === 'IS_NULL') {
|
|
1458
|
-
output.push('IS NULL');
|
|
1459
|
-
}
|
|
1460
|
-
else if (node.nulltesttype === 'IS_NOT_NULL') {
|
|
1461
|
-
output.push('IS NOT NULL');
|
|
1462
|
-
}
|
|
1463
|
-
return output.join(' ');
|
|
1464
|
-
}
|
|
1465
|
-
ParamRef(node) {
|
|
1466
|
-
if (node.number >= 0) {
|
|
1467
|
-
return ['$', node.number].join('');
|
|
1468
|
-
}
|
|
1469
|
-
return '?';
|
|
1470
|
-
}
|
|
1471
|
-
RangeFunction(node, context = {}) {
|
|
1472
|
-
const output = [];
|
|
1473
|
-
if (node.lateral) {
|
|
1474
|
-
output.push('LATERAL');
|
|
1475
|
-
}
|
|
1476
|
-
const funcs = [];
|
|
1477
|
-
const functions = unwrapList(node.functions);
|
|
1478
|
-
for (let i = 0; i < functions.length; i++) {
|
|
1479
|
-
const funcCall = unwrapList(functions[i]);
|
|
1480
|
-
const call = [this.deparse(funcCall[0], context)];
|
|
1481
|
-
const secondFuncCall = unwrapList(funcCall[1]);
|
|
1482
|
-
if (secondFuncCall && secondFuncCall.length) {
|
|
1483
|
-
call.push((0, util_1.format)('AS (%s)', this.list(secondFuncCall, ', ', '', context)));
|
|
1484
|
-
}
|
|
1485
|
-
funcs.push(call.join(' '));
|
|
1486
|
-
}
|
|
1487
|
-
const calls = funcs.join(', ');
|
|
1488
|
-
if (node.is_rowsfrom) {
|
|
1489
|
-
output.push(`ROWS FROM (${calls})`);
|
|
1490
|
-
}
|
|
1491
|
-
else {
|
|
1492
|
-
output.push(calls);
|
|
1493
|
-
}
|
|
1494
|
-
if (node.ordinality) {
|
|
1495
|
-
output.push('WITH ORDINALITY');
|
|
1496
|
-
}
|
|
1497
|
-
if (node.alias) {
|
|
1498
|
-
output.push(this.Alias(node.alias, context));
|
|
1499
|
-
}
|
|
1500
|
-
if (node.coldeflist) {
|
|
1501
|
-
const defList = this.list(node.coldeflist, ', ', '', context);
|
|
1502
|
-
if (!node.alias) {
|
|
1503
|
-
output.push(` AS (${defList})`);
|
|
1504
|
-
}
|
|
1505
|
-
else {
|
|
1506
|
-
output.push(`(${defList})`);
|
|
1507
|
-
}
|
|
1508
|
-
}
|
|
1509
|
-
return output.join(' ');
|
|
1510
|
-
}
|
|
1511
|
-
RangeSubselect(node, context = {}) {
|
|
1512
|
-
let output = '';
|
|
1513
|
-
if (node.lateral) {
|
|
1514
|
-
output += 'LATERAL ';
|
|
1515
|
-
}
|
|
1516
|
-
output += parens(this.deparse(node.subquery, context));
|
|
1517
|
-
if (node.alias) {
|
|
1518
|
-
return output + ' ' + this.Alias(node.alias, context);
|
|
1519
|
-
}
|
|
1520
|
-
return output;
|
|
1521
|
-
}
|
|
1522
|
-
RangeTableSample(node, context = {}) {
|
|
1523
|
-
const output = [];
|
|
1524
|
-
output.push(this.deparse(node.relation, context));
|
|
1525
|
-
output.push('TABLESAMPLE');
|
|
1526
|
-
output.push(this.deparse(unwrapList(node.method)[0], context));
|
|
1527
|
-
if (node.args) {
|
|
1528
|
-
output.push(parens(this.list(node.args, ', ', '', context)));
|
|
1529
|
-
}
|
|
1530
|
-
if (node.repeatable) {
|
|
1531
|
-
output.push('REPEATABLE(' + this.deparse(node.repeatable, context) + ')');
|
|
1532
|
-
}
|
|
1533
|
-
return output.join(' ');
|
|
1534
|
-
}
|
|
1535
|
-
RangeVar(node, context = {}) {
|
|
1536
|
-
const output = [];
|
|
1537
|
-
if (node.inhOpt === 0) {
|
|
1538
|
-
output.push('ONLY');
|
|
1539
|
-
}
|
|
1540
|
-
if (!node.inh && (context.lock || context === 'truncate')) {
|
|
1541
|
-
output.push('ONLY');
|
|
1542
|
-
}
|
|
1543
|
-
if (node.relpersistence === 'u') {
|
|
1544
|
-
output.push('UNLOGGED');
|
|
1545
|
-
}
|
|
1546
|
-
if (node.relpersistence === 't' && context !== 'view') {
|
|
1547
|
-
output.push('TEMPORARY TABLE');
|
|
1548
|
-
}
|
|
1549
|
-
if (node.schemaname != null) {
|
|
1550
|
-
output.push(`${this.quote(node.schemaname)}.${this.quote(node.relname)}`);
|
|
1551
|
-
}
|
|
1552
|
-
else {
|
|
1553
|
-
output.push(this.quote(node.relname));
|
|
1554
|
-
}
|
|
1555
|
-
if (node.alias) {
|
|
1556
|
-
output.push(this.Alias(node.alias, context));
|
|
1557
|
-
}
|
|
1558
|
-
return output.join(' ');
|
|
1559
|
-
}
|
|
1560
|
-
ResTarget(node, context = {}) {
|
|
1561
|
-
if (context === 'select') {
|
|
1562
|
-
return compact([
|
|
1563
|
-
this.deparse(node.val, context),
|
|
1564
|
-
this.quote(node.name)
|
|
1565
|
-
]).join(' AS ');
|
|
1566
|
-
}
|
|
1567
|
-
else if (context === 'update') {
|
|
1568
|
-
return compact([node.name, this.deparse(node.val, context)]).join(' = ');
|
|
1569
|
-
}
|
|
1570
|
-
else if (!(node.val != null)) {
|
|
1571
|
-
return this.quote(node.name);
|
|
1572
|
-
}
|
|
1573
|
-
return fail('ResTarget', node);
|
|
1574
|
-
}
|
|
1575
|
-
RowExpr(node, context = {}) {
|
|
1576
|
-
if (node.row_format === 'COERCE_IMPLICIT_CAST') {
|
|
1577
|
-
return parens(this.list(node.args, ', ', '', context));
|
|
1578
|
-
}
|
|
1579
|
-
return (0, util_1.format)('ROW(%s)', this.list(node.args, ', ', '', context));
|
|
1580
|
-
}
|
|
1581
|
-
ExplainStmt(node, context = {}) {
|
|
1582
|
-
const output = [];
|
|
1583
|
-
output.push('EXPLAIN');
|
|
1584
|
-
if (node.options) {
|
|
1585
|
-
output.push('(');
|
|
1586
|
-
output.push(this.list(node.options, ', ', '', 'explain'));
|
|
1587
|
-
output.push(')');
|
|
1588
|
-
}
|
|
1589
|
-
output.push(this.deparse(node.query, context));
|
|
1590
|
-
return output.join(' ');
|
|
1591
|
-
}
|
|
1592
|
-
SelectStmt(node, context = {}) {
|
|
1593
|
-
const output = [];
|
|
1594
|
-
if (node.withClause) {
|
|
1595
|
-
output.push(this.WithClause(node.withClause, context));
|
|
1596
|
-
}
|
|
1597
|
-
if (node.op === 'SETOP_NONE') {
|
|
1598
|
-
// VALUES select's don't get SELECT
|
|
1599
|
-
if (node.valuesLists == null) {
|
|
1600
|
-
output.push('SELECT');
|
|
1601
|
-
}
|
|
1602
|
-
}
|
|
1603
|
-
else {
|
|
1604
|
-
output.push(parens(this.SelectStmt(node.larg, context)));
|
|
1605
|
-
switch (node.op) {
|
|
1606
|
-
case 'SETOP_NONE':
|
|
1607
|
-
output.push('NONE');
|
|
1608
|
-
break;
|
|
1609
|
-
case 'SETOP_UNION':
|
|
1610
|
-
output.push('UNION');
|
|
1611
|
-
break;
|
|
1612
|
-
case 'SETOP_INTERSECT':
|
|
1613
|
-
output.push('INTERSECT');
|
|
1614
|
-
break;
|
|
1615
|
-
case 'SETOP_EXCEPT':
|
|
1616
|
-
output.push('EXCEPT');
|
|
1617
|
-
break;
|
|
1618
|
-
default:
|
|
1619
|
-
throw new Error('bad SelectStmt op');
|
|
1620
|
-
}
|
|
1621
|
-
if (node.all) {
|
|
1622
|
-
output.push('ALL');
|
|
1623
|
-
}
|
|
1624
|
-
output.push(parens(this.SelectStmt(node.rarg, context)));
|
|
1625
|
-
}
|
|
1626
|
-
if (node.distinctClause) {
|
|
1627
|
-
const distinctClause = unwrapList(node.distinctClause);
|
|
1628
|
-
if (!isEmptyObject(distinctClause[0])
|
|
1629
|
-
// new change distinctClause can be {}
|
|
1630
|
-
) {
|
|
1631
|
-
output.push('DISTINCT ON');
|
|
1632
|
-
const clause = distinctClause
|
|
1633
|
-
.map((e) => this.deparse(e, 'select'))
|
|
1634
|
-
.join(`,${NEWLINE_CHAR}`);
|
|
1635
|
-
output.push(`(${clause})`);
|
|
1636
|
-
}
|
|
1637
|
-
else {
|
|
1638
|
-
output.push('DISTINCT');
|
|
1639
|
-
}
|
|
1640
|
-
}
|
|
1641
|
-
if (node.targetList) {
|
|
1642
|
-
output.push(indent(unwrapList(node.targetList)
|
|
1643
|
-
.map((e) => this.deparse(e, 'select'))
|
|
1644
|
-
.join(`,${NEWLINE_CHAR}`)));
|
|
1645
|
-
}
|
|
1646
|
-
if (node.intoClause) {
|
|
1647
|
-
output.push('INTO');
|
|
1648
|
-
output.push(indent(this.IntoClause(node.intoClause, context)));
|
|
1649
|
-
}
|
|
1650
|
-
if (node.fromClause) {
|
|
1651
|
-
output.push('FROM');
|
|
1652
|
-
output.push(indent(unwrapList(node.fromClause)
|
|
1653
|
-
.map((e) => this.deparse(e, 'from'))
|
|
1654
|
-
.join(`,${NEWLINE_CHAR}`)));
|
|
1655
|
-
}
|
|
1656
|
-
if (node.whereClause) {
|
|
1657
|
-
output.push('WHERE');
|
|
1658
|
-
output.push(indent(this.deparse(node.whereClause, context)));
|
|
1659
|
-
}
|
|
1660
|
-
if (node.valuesLists) {
|
|
1661
|
-
output.push('VALUES');
|
|
1662
|
-
const lists = unwrapList(node.valuesLists).map((list) => {
|
|
1663
|
-
return `(${this.list(list, ', ', '', context)})`;
|
|
1664
|
-
});
|
|
1665
|
-
output.push(lists.join(', '));
|
|
1666
|
-
}
|
|
1667
|
-
if (node.groupClause) {
|
|
1668
|
-
output.push('GROUP BY');
|
|
1669
|
-
output.push(indent(unwrapList(node.groupClause)
|
|
1670
|
-
.map((e) => this.deparse(e, 'group'))
|
|
1671
|
-
.join(`,${NEWLINE_CHAR}`)));
|
|
1672
|
-
}
|
|
1673
|
-
if (node.havingClause) {
|
|
1674
|
-
output.push('HAVING');
|
|
1675
|
-
output.push(indent(this.deparse(node.havingClause, context)));
|
|
1676
|
-
}
|
|
1677
|
-
if (node.windowClause) {
|
|
1678
|
-
output.push('WINDOW');
|
|
1679
|
-
const windows = [];
|
|
1680
|
-
const windowClause = unwrapList(node.windowClause);
|
|
1681
|
-
for (let i = 0; i < windowClause.length; i++) {
|
|
1682
|
-
const w = windowClause[i];
|
|
1683
|
-
const window = [];
|
|
1684
|
-
if (w.WindowDef.name) {
|
|
1685
|
-
window.push(this.quote(w.WindowDef.name) + ' AS');
|
|
1686
|
-
}
|
|
1687
|
-
window.push(parens(this.deparse(w, 'window')));
|
|
1688
|
-
windows.push(window.join(' '));
|
|
1689
|
-
}
|
|
1690
|
-
output.push(windows.join(', '));
|
|
1691
|
-
}
|
|
1692
|
-
if (node.sortClause) {
|
|
1693
|
-
output.push('ORDER BY');
|
|
1694
|
-
output.push(indent(unwrapList(node.sortClause)
|
|
1695
|
-
.map((e) => this.deparse(e, 'sort'))
|
|
1696
|
-
.join(`,${NEWLINE_CHAR}`)));
|
|
1697
|
-
}
|
|
1698
|
-
if (node.limitCount) {
|
|
1699
|
-
output.push('LIMIT');
|
|
1700
|
-
output.push(indent(this.deparse(node.limitCount, context)));
|
|
1701
|
-
}
|
|
1702
|
-
if (node.limitOffset) {
|
|
1703
|
-
output.push('OFFSET');
|
|
1704
|
-
output.push(indent(this.deparse(node.limitOffset, context)));
|
|
1705
|
-
}
|
|
1706
|
-
if (node.lockingClause) {
|
|
1707
|
-
node.lockingClause.forEach((item) => {
|
|
1708
|
-
return output.push(this.deparse(item, context));
|
|
1709
|
-
});
|
|
1710
|
-
}
|
|
1711
|
-
return output.join(' ');
|
|
1712
|
-
}
|
|
1713
|
-
TruncateStmt(node, context = {}) {
|
|
1714
|
-
const output = ['TRUNCATE TABLE'];
|
|
1715
|
-
output.push(this.list(node.relations, ', ', '', 'truncate'));
|
|
1716
|
-
if (node.restart_seqs) {
|
|
1717
|
-
output.push('RESTART IDENTITY');
|
|
1718
|
-
}
|
|
1719
|
-
if (node.behavior === 'DROP_CASCADE') {
|
|
1720
|
-
output.push('CASCADE');
|
|
1721
|
-
}
|
|
1722
|
-
return output.join(' ');
|
|
1723
|
-
}
|
|
1724
|
-
AlterDefaultPrivilegesStmt(node, context = {}) {
|
|
1725
|
-
const output = [];
|
|
1726
|
-
output.push('ALTER DEFAULT PRIVILEGES');
|
|
1727
|
-
const options = unwrapList(dotty.get(node, 'options'));
|
|
1728
|
-
if (options) {
|
|
1729
|
-
const elem = options.find((el) => el.hasOwnProperty('DefElem'));
|
|
1730
|
-
const elemDefElemArg = unwrapList(elem.DefElem.arg);
|
|
1731
|
-
if (elem.DefElem.defname === 'schemas') {
|
|
1732
|
-
output.push('IN SCHEMA');
|
|
1733
|
-
output.push(elemDefElemArg[0].String.str);
|
|
1734
|
-
}
|
|
1735
|
-
if (elem.DefElem.defname === 'roles') {
|
|
1736
|
-
output.push('FOR ROLE');
|
|
1737
|
-
const roleSpec = elemDefElemArg[0];
|
|
1738
|
-
output.push(this.deparse(roleSpec, context));
|
|
1739
|
-
}
|
|
1740
|
-
output.push(NEWLINE_CHAR);
|
|
1741
|
-
}
|
|
1742
|
-
output.push(this.GrantStmt(node.action, context));
|
|
1743
|
-
return output.join(' ');
|
|
1744
|
-
}
|
|
1745
|
-
AlterTableStmt(node, context = {}) {
|
|
1746
|
-
const output = [];
|
|
1747
|
-
const ctx = Object.assign({}, context);
|
|
1748
|
-
output.push('ALTER');
|
|
1749
|
-
if (node.relkind === 'OBJECT_TABLE') {
|
|
1750
|
-
output.push('TABLE');
|
|
1751
|
-
const inh = dotty.get(node, 'relation.inh');
|
|
1752
|
-
if (!inh) {
|
|
1753
|
-
output.push('ONLY');
|
|
1754
|
-
}
|
|
1755
|
-
}
|
|
1756
|
-
else if (node.relkind === 'OBJECT_TYPE') {
|
|
1757
|
-
output.push('TYPE');
|
|
1758
|
-
}
|
|
1759
|
-
else {
|
|
1760
|
-
fail('AlterTableStmt', node);
|
|
1761
|
-
}
|
|
1762
|
-
if (node.missing_ok) {
|
|
1763
|
-
output.push('IF EXISTS');
|
|
1764
|
-
}
|
|
1765
|
-
ctx.alterType = node.relkind;
|
|
1766
|
-
output.push(this.RangeVar(node.relation, ctx));
|
|
1767
|
-
output.push(this.list(node.cmds, ', ', '', ctx));
|
|
1768
|
-
return output.join(' ');
|
|
1769
|
-
}
|
|
1770
|
-
AlterTableCmd(node, context = {}) {
|
|
1771
|
-
const output = [];
|
|
1772
|
-
let subType = 'COLUMN';
|
|
1773
|
-
if (context && context.alterType === 'OBJECT_TYPE') {
|
|
1774
|
-
subType = 'ATTRIBUTE';
|
|
1775
|
-
}
|
|
1776
|
-
if (node.subtype === 'AT_AddColumn') {
|
|
1777
|
-
output.push('ADD');
|
|
1778
|
-
output.push(subType);
|
|
1779
|
-
if (node.missing_ok) {
|
|
1780
|
-
output.push('IF NOT EXISTS');
|
|
1781
|
-
}
|
|
1782
|
-
output.push(this.quote(node.name));
|
|
1783
|
-
output.push(this.deparse(node.def, context));
|
|
1784
|
-
}
|
|
1785
|
-
else if (node.subtype === 'AT_ColumnDefault') {
|
|
1786
|
-
output.push('ALTER');
|
|
1787
|
-
output.push(subType);
|
|
1788
|
-
output.push(this.quote(node.name));
|
|
1789
|
-
if (node.def) {
|
|
1790
|
-
output.push('SET DEFAULT');
|
|
1791
|
-
output.push(this.deparse(node.def, context));
|
|
1792
|
-
}
|
|
1793
|
-
else {
|
|
1794
|
-
output.push('DROP DEFAULT');
|
|
1795
|
-
}
|
|
1796
|
-
}
|
|
1797
|
-
else if (node.subtype === 'AT_DropNotNull') {
|
|
1798
|
-
output.push('ALTER');
|
|
1799
|
-
output.push(subType);
|
|
1800
|
-
output.push(this.quote(node.name));
|
|
1801
|
-
output.push('DROP NOT NULL');
|
|
1802
|
-
}
|
|
1803
|
-
else if (node.subtype === 'AT_SetNotNull') {
|
|
1804
|
-
output.push('ALTER');
|
|
1805
|
-
output.push(subType);
|
|
1806
|
-
output.push(this.quote(node.name));
|
|
1807
|
-
output.push('SET NOT NULL');
|
|
1808
|
-
}
|
|
1809
|
-
else if (node.subtype === 'AT_SetStatistics') {
|
|
1810
|
-
output.push('ALTER');
|
|
1811
|
-
output.push(this.quote(node.name));
|
|
1812
|
-
output.push('SET STATISTICS');
|
|
1813
|
-
output.push(dotty.get(node, 'def.Integer.ival'));
|
|
1814
|
-
}
|
|
1815
|
-
else if (node.subtype === 'AT_SetOptions') {
|
|
1816
|
-
output.push('ALTER');
|
|
1817
|
-
output.push(subType);
|
|
1818
|
-
output.push(this.quote(node.name));
|
|
1819
|
-
output.push('SET');
|
|
1820
|
-
output.push('(');
|
|
1821
|
-
output.push(this.list(node.def, ', ', '', context));
|
|
1822
|
-
output.push(')');
|
|
1823
|
-
}
|
|
1824
|
-
else if (node.subtype === 'AT_SetStorage') {
|
|
1825
|
-
output.push('ALTER');
|
|
1826
|
-
output.push(this.quote(node.name));
|
|
1827
|
-
output.push('SET STORAGE');
|
|
1828
|
-
if (node.def) {
|
|
1829
|
-
output.push(this.deparse(node.def, context));
|
|
1830
|
-
}
|
|
1831
|
-
else {
|
|
1832
|
-
output.push('PLAIN');
|
|
1833
|
-
}
|
|
1834
|
-
}
|
|
1835
|
-
else if (node.subtype === 'AT_DropColumn') {
|
|
1836
|
-
output.push('DROP');
|
|
1837
|
-
output.push(subType);
|
|
1838
|
-
if (node.missing_ok) {
|
|
1839
|
-
output.push('IF EXISTS');
|
|
1840
|
-
}
|
|
1841
|
-
output.push(this.quote(node.name));
|
|
1842
|
-
}
|
|
1843
|
-
else if (node.subtype === 'AT_AddConstraint') {
|
|
1844
|
-
// output.push('ADD CONSTRAINT');
|
|
1845
|
-
output.push('ADD');
|
|
1846
|
-
output.push(this.deparse(node.def, context));
|
|
1847
|
-
}
|
|
1848
|
-
else if (node.subtype === 'AT_ValidateConstraint') {
|
|
1849
|
-
output.push('VALIDATE CONSTRAINT');
|
|
1850
|
-
output.push(this.quote(node.name, context));
|
|
1851
|
-
}
|
|
1852
|
-
else if (node.subtype === 'AT_DropConstraint') {
|
|
1853
|
-
output.push('DROP CONSTRAINT');
|
|
1854
|
-
if (node.missing_ok) {
|
|
1855
|
-
output.push('IF EXISTS');
|
|
1856
|
-
}
|
|
1857
|
-
output.push(this.quote(node.name));
|
|
1858
|
-
}
|
|
1859
|
-
else if (node.subtype === 'AT_AlterColumnType') {
|
|
1860
|
-
output.push('ALTER');
|
|
1861
|
-
output.push(subType);
|
|
1862
|
-
output.push(this.quote(node.name));
|
|
1863
|
-
output.push('TYPE');
|
|
1864
|
-
output.push(this.deparse(node.def, context));
|
|
1865
|
-
}
|
|
1866
|
-
else if (node.subtype === 'AT_ChangeOwner') {
|
|
1867
|
-
output.push('OWNER');
|
|
1868
|
-
output.push('TO');
|
|
1869
|
-
output.push(this.RoleSpec(node.newowner, context));
|
|
1870
|
-
}
|
|
1871
|
-
else if (node.subtype === 'AT_ClusterOn') {
|
|
1872
|
-
output.push('CLUSTER ON');
|
|
1873
|
-
output.push(this.quote(node.name));
|
|
1874
|
-
}
|
|
1875
|
-
else if (node.subtype === 'AT_DropCluster') {
|
|
1876
|
-
output.push('SET WITHOUT CLUSTER');
|
|
1877
|
-
}
|
|
1878
|
-
else if (node.subtype === 'AT_AddOids') {
|
|
1879
|
-
output.push('SET WITH OIDS');
|
|
1880
|
-
}
|
|
1881
|
-
else if (node.subtype === 'AT_DropOids') {
|
|
1882
|
-
output.push('SET WITHOUT OIDS');
|
|
1883
|
-
}
|
|
1884
|
-
else if (node.subtype === 'AT_SetRelOptions') {
|
|
1885
|
-
output.push('SET');
|
|
1886
|
-
output.push('(');
|
|
1887
|
-
output.push(this.list(node.def, ', ', '', context));
|
|
1888
|
-
output.push(')');
|
|
1889
|
-
}
|
|
1890
|
-
else if (node.subtype === 'AT_ResetRelOptions') {
|
|
1891
|
-
output.push('RESET');
|
|
1892
|
-
output.push('(');
|
|
1893
|
-
output.push(this.list(node.def, ', ', '', context));
|
|
1894
|
-
output.push(')');
|
|
1895
|
-
}
|
|
1896
|
-
else if (node.subtype === 'AT_AddIdentity') {
|
|
1897
|
-
output.push('ALTER COLUMN');
|
|
1898
|
-
output.push(this.quote(node.name));
|
|
1899
|
-
output.push('ADD');
|
|
1900
|
-
output.push(this.deparse(node.def, context));
|
|
1901
|
-
}
|
|
1902
|
-
else if (node.subtype === 'AT_AddInherit') {
|
|
1903
|
-
output.push('INHERIT');
|
|
1904
|
-
output.push(this.deparse(node.def, context));
|
|
1905
|
-
}
|
|
1906
|
-
else if (node.subtype === 'AT_DropInherit') {
|
|
1907
|
-
output.push('NO INHERIT');
|
|
1908
|
-
output.push(this.deparse(node.def, context));
|
|
1909
|
-
}
|
|
1910
|
-
else if (node.subtype === 'AT_AddOf') {
|
|
1911
|
-
output.push('OF');
|
|
1912
|
-
output.push(this.deparse(node.def, context));
|
|
1913
|
-
}
|
|
1914
|
-
else if (node.subtype === 'AT_DropOf') {
|
|
1915
|
-
output.push('NOT OF');
|
|
1916
|
-
//output.push(this.deparse(node.def));
|
|
1917
|
-
}
|
|
1918
|
-
else if (node.subtype === 'AT_EnableRowSecurity') {
|
|
1919
|
-
output.push('ENABLE ROW LEVEL SECURITY');
|
|
1920
|
-
}
|
|
1921
|
-
else if (node.subtype === 'AT_DisableRowSecurity') {
|
|
1922
|
-
output.push('DISABLE ROW LEVEL SECURITY');
|
|
1923
|
-
}
|
|
1924
|
-
else if (node.subtype === 'AT_ForceRowSecurity') {
|
|
1925
|
-
output.push('FORCE ROW SECURITY');
|
|
1926
|
-
}
|
|
1927
|
-
else if (node.subtype === 'AT_NoForceRowSecurity') {
|
|
1928
|
-
output.push('NO FORCE ROW SECURITY');
|
|
1929
|
-
}
|
|
1930
|
-
if (node.behavior === 'DROP_CASCADE') {
|
|
1931
|
-
output.push('CASCADE');
|
|
1932
|
-
}
|
|
1933
|
-
return output.join(' ');
|
|
1934
|
-
}
|
|
1935
|
-
CreateEnumStmt(node, context = {}) {
|
|
1936
|
-
const output = [];
|
|
1937
|
-
output.push('CREATE TYPE');
|
|
1938
|
-
output.push(this.list(node.typeName, '.', '', context));
|
|
1939
|
-
output.push('AS ENUM');
|
|
1940
|
-
output.push(`(${NEWLINE_CHAR}`);
|
|
1941
|
-
const vals = unwrapList(node.vals).map((val) => {
|
|
1942
|
-
return { String: { str: `'${val.String.str}'` } };
|
|
1943
|
-
});
|
|
1944
|
-
output.push(this.list(vals, `,${NEWLINE_CHAR}`, TAB_CHAR));
|
|
1945
|
-
output.push(`${NEWLINE_CHAR})`);
|
|
1946
|
-
return output.join(' ');
|
|
1947
|
-
}
|
|
1948
|
-
AlterEnumStmt(node, context = {}) {
|
|
1949
|
-
const output = [];
|
|
1950
|
-
output.push('ALTER TYPE');
|
|
1951
|
-
const typObj = {
|
|
1952
|
-
TypeName: {
|
|
1953
|
-
names: node.typeName
|
|
1954
|
-
}
|
|
1955
|
-
};
|
|
1956
|
-
output.push(this.deparse(typObj, context));
|
|
1957
|
-
if (node.newVal) {
|
|
1958
|
-
output.push('ADD VALUE');
|
|
1959
|
-
const result = node.newVal.replace(/'/g, "''");
|
|
1960
|
-
output.push(`'${result}'`);
|
|
1961
|
-
}
|
|
1962
|
-
if (node.newValNeighbor) {
|
|
1963
|
-
if (node.newValIsAfter) {
|
|
1964
|
-
output.push('AFTER');
|
|
1965
|
-
}
|
|
1966
|
-
else {
|
|
1967
|
-
output.push('BEFORE');
|
|
1968
|
-
}
|
|
1969
|
-
const result = node.newValNeighbor.replace(/'/g, "''");
|
|
1970
|
-
output.push(`'${result}'`);
|
|
1971
|
-
}
|
|
1972
|
-
if (node.behavior === 'DROP_CASCADE') {
|
|
1973
|
-
output.push('CASCADE');
|
|
1974
|
-
}
|
|
1975
|
-
return output.join(' ');
|
|
1976
|
-
}
|
|
1977
|
-
AlterDomainStmt(node, context = {}) {
|
|
1978
|
-
const output = [];
|
|
1979
|
-
output.push('ALTER DOMAIN');
|
|
1980
|
-
const typObj = {
|
|
1981
|
-
TypeName: {
|
|
1982
|
-
names: node.typeName
|
|
1983
|
-
}
|
|
1984
|
-
};
|
|
1985
|
-
output.push(this.deparse(typObj, context));
|
|
1986
|
-
if (node.subtype === 'C') {
|
|
1987
|
-
output.push('ADD');
|
|
1988
|
-
output.push(this.deparse(node.def, context));
|
|
1989
|
-
}
|
|
1990
|
-
else if (node.subtype === 'V') {
|
|
1991
|
-
output.push('VALIDATE');
|
|
1992
|
-
output.push('CONSTRAINT');
|
|
1993
|
-
output.push(this.quote(node.name));
|
|
1994
|
-
}
|
|
1995
|
-
else if (node.subtype === 'X') {
|
|
1996
|
-
output.push('DROP');
|
|
1997
|
-
output.push('CONSTRAINT');
|
|
1998
|
-
output.push(this.quote(node.name));
|
|
1999
|
-
}
|
|
2000
|
-
if (node.behavior === 'DROP_CASCADE') {
|
|
2001
|
-
output.push('CASCADE');
|
|
2002
|
-
}
|
|
2003
|
-
return output.join(' ');
|
|
2004
|
-
}
|
|
2005
|
-
CreateExtensionStmt(node) {
|
|
2006
|
-
const output = [];
|
|
2007
|
-
output.push('CREATE EXTENSION');
|
|
2008
|
-
if (node.if_not_exists) {
|
|
2009
|
-
output.push('IF NOT EXISTS');
|
|
2010
|
-
}
|
|
2011
|
-
output.push(this.quote(node.extname));
|
|
2012
|
-
if (node.options) {
|
|
2013
|
-
node.options.forEach((opt) => {
|
|
2014
|
-
if (opt.DefElem.defname === 'cascade' &&
|
|
2015
|
-
opt.DefElem.arg.Integer.ival === 1) {
|
|
2016
|
-
output.push('CASCADE');
|
|
2017
|
-
}
|
|
2018
|
-
if (opt.DefElem.defname === 'schema') {
|
|
2019
|
-
output.push('WITH SCHEMA');
|
|
2020
|
-
output.push(this.quote(this.deparse(opt.DefElem.arg)));
|
|
2021
|
-
}
|
|
2022
|
-
});
|
|
2023
|
-
}
|
|
2024
|
-
return output.join(' ');
|
|
2025
|
-
}
|
|
2026
|
-
DropStmt(node, context = {}) {
|
|
2027
|
-
const output = [];
|
|
2028
|
-
output.push('DROP');
|
|
2029
|
-
output.push((0, pgsql_enums_1.objtypeName)(node.removeType));
|
|
2030
|
-
if (node.missing_ok) {
|
|
2031
|
-
output.push('IF EXISTS');
|
|
2032
|
-
}
|
|
2033
|
-
const stmts = [];
|
|
2034
|
-
const objects = unwrapList(node.objects);
|
|
2035
|
-
for (let s = 0; s < objects.length; s++) {
|
|
2036
|
-
const children = unwrapList(objects[s]);
|
|
2037
|
-
const stmt = [];
|
|
2038
|
-
if (node.removeType === 'OBJECT_TABLE' ||
|
|
2039
|
-
node.removeType === 'OBJECT_CONVERSION' ||
|
|
2040
|
-
node.removeType === 'OBJECT_COLLATION' ||
|
|
2041
|
-
node.removeType === 'OBJECT_MATVIEW' ||
|
|
2042
|
-
node.removeType === 'OBJECT_INDEX' ||
|
|
2043
|
-
node.removeType === 'OBJECT_FOREIGN_TABLE') {
|
|
2044
|
-
if (children.length === 1) {
|
|
2045
|
-
stmt.push(this.quote(this.deparse(children[0])));
|
|
2046
|
-
}
|
|
2047
|
-
else if (children.length === 2) {
|
|
2048
|
-
stmt.push(this.listQuotes(children, '.'));
|
|
2049
|
-
}
|
|
2050
|
-
else {
|
|
2051
|
-
throw new Error('bad case 2 drop stmt' + JSON.stringify(node, null, 2));
|
|
2052
|
-
}
|
|
2053
|
-
}
|
|
2054
|
-
else if (node.removeType === 'OBJECT_SCHEMA') {
|
|
2055
|
-
stmt.push(this.quote(this.deparse(children)));
|
|
2056
|
-
}
|
|
2057
|
-
else if (node.removeType === 'OBJECT_SEQUENCE') {
|
|
2058
|
-
stmt.push(this.listQuotes(children, '.'));
|
|
2059
|
-
}
|
|
2060
|
-
else if (node.removeType === 'OBJECT_POLICY') {
|
|
2061
|
-
if (children.length === 2) {
|
|
2062
|
-
stmt.push(this.quote(this.deparse(children[1], context)));
|
|
2063
|
-
stmt.push('ON');
|
|
2064
|
-
stmt.push(this.quote(this.deparse(children[0], context)));
|
|
2065
|
-
}
|
|
2066
|
-
else if (children.length === 3) {
|
|
2067
|
-
stmt.push(this.quote(this.deparse(children[2], context)));
|
|
2068
|
-
stmt.push('ON');
|
|
2069
|
-
stmt.push(this.listQuotes([children[0], children[1]], '.'));
|
|
2070
|
-
}
|
|
2071
|
-
else {
|
|
2072
|
-
throw new Error('bad drop policy stmt: ' + JSON.stringify(node, null, 2));
|
|
2073
|
-
}
|
|
2074
|
-
}
|
|
2075
|
-
else if (node.removeType === 'OBJECT_TRIGGER') {
|
|
2076
|
-
if (children.length === 2) {
|
|
2077
|
-
stmt.push(this.quote(this.deparse(children[1], context)));
|
|
2078
|
-
stmt.push('ON');
|
|
2079
|
-
stmt.push(this.quote(this.deparse(children[0], context)));
|
|
2080
|
-
}
|
|
2081
|
-
else if (children.length === 3) {
|
|
2082
|
-
stmt.push(this.quote(this.deparse(children[2], context)));
|
|
2083
|
-
stmt.push('ON');
|
|
2084
|
-
stmt.push(this.listQuotes([children[0], children[1]], '.'));
|
|
2085
|
-
}
|
|
2086
|
-
else {
|
|
2087
|
-
throw new Error('bad drop trigger stmt: ' + JSON.stringify(node, null, 2));
|
|
2088
|
-
}
|
|
2089
|
-
}
|
|
2090
|
-
else if (node.removeType === 'OBJECT_RULE') {
|
|
2091
|
-
if (children.length === 2) {
|
|
2092
|
-
stmt.push(this.quote(this.deparse(children[1], context)));
|
|
2093
|
-
stmt.push('ON');
|
|
2094
|
-
stmt.push(this.quote(this.deparse(children[0], context)));
|
|
2095
|
-
}
|
|
2096
|
-
else if (children.length === 3) {
|
|
2097
|
-
stmt.push(this.quote(this.deparse(children[2], context)));
|
|
2098
|
-
stmt.push('ON');
|
|
2099
|
-
stmt.push(this.listQuotes([children[0], children[1]], '.'));
|
|
2100
|
-
}
|
|
2101
|
-
else {
|
|
2102
|
-
throw new Error('bad drop rule stmt: ' + JSON.stringify(node, null, 2));
|
|
2103
|
-
}
|
|
2104
|
-
}
|
|
2105
|
-
else if (node.removeType === 'OBJECT_VIEW') {
|
|
2106
|
-
if (children.length === 1) {
|
|
2107
|
-
stmt.push(this.quote(this.deparse(children[0], context)));
|
|
2108
|
-
}
|
|
2109
|
-
else if (children.length === 2) {
|
|
2110
|
-
stmt.push(this.listQuotes(children, '.'));
|
|
2111
|
-
}
|
|
2112
|
-
else {
|
|
2113
|
-
throw new Error('bad drop value stmt: ' + JSON.stringify(node, null, 2));
|
|
2114
|
-
}
|
|
2115
|
-
// } else if (node.removeType === 'OBJECT_OPERATOR') {
|
|
2116
|
-
}
|
|
2117
|
-
else if (node.removeType === 'OBJECT_CAST') {
|
|
2118
|
-
stmt.push('(');
|
|
2119
|
-
stmt.push(this.deparse(children[0], context));
|
|
2120
|
-
stmt.push('AS');
|
|
2121
|
-
stmt.push(this.deparse(children[1], context));
|
|
2122
|
-
stmt.push(')');
|
|
2123
|
-
// } else if (node.removeType === 'OBJECT_OPERATOR') {
|
|
2124
|
-
// stmt.push(this.deparse(children, 'noquotes')); // in this case children is not an array
|
|
2125
|
-
}
|
|
2126
|
-
else if (node.removeType === 'OBJECT_AGGREGATE') {
|
|
2127
|
-
stmt.push(this.deparse(children, context)); // in this case children is not an array
|
|
2128
|
-
}
|
|
2129
|
-
else if (node.removeType === 'OBJECT_FDW') {
|
|
2130
|
-
stmt.push(this.deparse(children, context)); // in this case children is not an array
|
|
2131
|
-
}
|
|
2132
|
-
else if (node.removeType === 'OBJECT_FOREIGN_SERVER') {
|
|
2133
|
-
stmt.push(this.deparse(children, context)); // in this case children is not an array
|
|
2134
|
-
}
|
|
2135
|
-
else if (node.removeType === 'OBJECT_EXTENSION') {
|
|
2136
|
-
stmt.push(this.deparse(children, context)); // in this case children is not an array
|
|
2137
|
-
}
|
|
2138
|
-
else if (node.removeType === 'OBJECT_DOMAIN') {
|
|
2139
|
-
stmt.push(this.deparse(children, context)); // in this case children is not an array
|
|
2140
|
-
}
|
|
2141
|
-
else if (node.removeType === 'OBJECT_FUNCTION') {
|
|
2142
|
-
stmt.push(this.deparse(children, context)); // in this case children is not an array
|
|
2143
|
-
}
|
|
2144
|
-
else if (node.removeType === 'OBJECT_TYPE') {
|
|
2145
|
-
stmt.push(this.deparse(children, context)); // in this case children is not an array
|
|
2146
|
-
}
|
|
2147
|
-
else {
|
|
2148
|
-
throw new Error('bad drop stmt: ' + JSON.stringify(node, null, 2));
|
|
2149
|
-
}
|
|
2150
|
-
stmts.push(stmt.join(' '));
|
|
2151
|
-
}
|
|
2152
|
-
output.push(stmts.join(','));
|
|
2153
|
-
if (node.behavior === 'DROP_CASCADE') {
|
|
2154
|
-
output.push('CASCADE');
|
|
2155
|
-
}
|
|
2156
|
-
return output.join(' ');
|
|
2157
|
-
}
|
|
2158
|
-
CreatePolicyStmt(node, context = {}) {
|
|
2159
|
-
const output = [];
|
|
2160
|
-
output.push('CREATE POLICY');
|
|
2161
|
-
output.push(this.quote(node.policy_name));
|
|
2162
|
-
if (node.table) {
|
|
2163
|
-
output.push('ON');
|
|
2164
|
-
output.push(this.RangeVar(node.table, context));
|
|
2165
|
-
}
|
|
2166
|
-
if (node.permissive) {
|
|
2167
|
-
// permissive is the default!
|
|
2168
|
-
}
|
|
2169
|
-
else {
|
|
2170
|
-
output.push('AS');
|
|
2171
|
-
output.push('RESTRICTIVE');
|
|
2172
|
-
}
|
|
2173
|
-
if (node.cmd_name) {
|
|
2174
|
-
output.push('FOR');
|
|
2175
|
-
output.push(node.cmd_name.toUpperCase());
|
|
2176
|
-
}
|
|
2177
|
-
output.push('TO');
|
|
2178
|
-
output.push(this.list(node.roles));
|
|
2179
|
-
if (node.qual) {
|
|
2180
|
-
output.push('USING');
|
|
2181
|
-
output.push('(');
|
|
2182
|
-
output.push(this.deparse(node.qual, context));
|
|
2183
|
-
output.push(')');
|
|
2184
|
-
}
|
|
2185
|
-
if (node.with_check) {
|
|
2186
|
-
output.push('WITH CHECK');
|
|
2187
|
-
output.push('(');
|
|
2188
|
-
output.push(this.deparse(node.with_check, context));
|
|
2189
|
-
output.push(')');
|
|
2190
|
-
}
|
|
2191
|
-
return output.join(' ');
|
|
2192
|
-
}
|
|
2193
|
-
AlterPolicyStmt(node, context = {}) {
|
|
2194
|
-
const output = [];
|
|
2195
|
-
output.push('ALTER POLICY');
|
|
2196
|
-
output.push(this.quote(node.policy_name));
|
|
2197
|
-
if (node.table) {
|
|
2198
|
-
output.push('ON');
|
|
2199
|
-
output.push(this.RangeVar(node.table, context));
|
|
2200
|
-
}
|
|
2201
|
-
output.push('TO');
|
|
2202
|
-
output.push(this.list(node.roles));
|
|
2203
|
-
if (node.qual) {
|
|
2204
|
-
output.push('USING');
|
|
2205
|
-
output.push('(');
|
|
2206
|
-
output.push(this.deparse(node.qual, context));
|
|
2207
|
-
output.push(')');
|
|
2208
|
-
}
|
|
2209
|
-
if (node.with_check) {
|
|
2210
|
-
output.push('WITH CHECK');
|
|
2211
|
-
output.push('(');
|
|
2212
|
-
output.push(this.deparse(node.with_check, context));
|
|
2213
|
-
output.push(')');
|
|
2214
|
-
}
|
|
2215
|
-
return output.join(' ');
|
|
2216
|
-
}
|
|
2217
|
-
ViewStmt(node, context = {}) {
|
|
2218
|
-
const output = [];
|
|
2219
|
-
output.push('CREATE');
|
|
2220
|
-
if (node.replace)
|
|
2221
|
-
output.push('OR REPLACE');
|
|
2222
|
-
if (node.view.relpersistence === 't') {
|
|
2223
|
-
output.push('TEMPORARY');
|
|
2224
|
-
}
|
|
2225
|
-
output.push('VIEW');
|
|
2226
|
-
output.push(this.RangeVar(node.view, 'view'));
|
|
2227
|
-
if (node.aliases) {
|
|
2228
|
-
output.push('(');
|
|
2229
|
-
output.push(this.list(node.aliases, ', ', '', context));
|
|
2230
|
-
output.push(')');
|
|
2231
|
-
}
|
|
2232
|
-
output.push('AS');
|
|
2233
|
-
output.push(this.deparse(node.query, context));
|
|
2234
|
-
if (node.withCheckOption === 'LOCAL_CHECK_OPTION') {
|
|
2235
|
-
output.push('WITH LOCAL CHECK OPTION');
|
|
2236
|
-
}
|
|
2237
|
-
else if (node.withCheckOption === 'CASCADED_CHECK_OPTION') {
|
|
2238
|
-
output.push('WITH CASCADED CHECK OPTION');
|
|
2239
|
-
}
|
|
2240
|
-
return output.join(' ');
|
|
2241
|
-
}
|
|
2242
|
-
CreateSeqStmt(node, context = {}) {
|
|
2243
|
-
const output = [];
|
|
2244
|
-
output.push('CREATE SEQUENCE');
|
|
2245
|
-
output.push(this.RangeVar(node.sequence, context));
|
|
2246
|
-
const options = unwrapList(node.options);
|
|
2247
|
-
if (options && options.length) {
|
|
2248
|
-
options.forEach((opt) => {
|
|
2249
|
-
output.push(this.deparse(opt, 'sequence'));
|
|
2250
|
-
});
|
|
2251
|
-
}
|
|
2252
|
-
return output.join(' ');
|
|
2253
|
-
}
|
|
2254
|
-
AlterSeqStmt(node, context = {}) {
|
|
2255
|
-
const output = [];
|
|
2256
|
-
output.push('ALTER SEQUENCE');
|
|
2257
|
-
output.push(this.RangeVar(node.sequence, context));
|
|
2258
|
-
const options = unwrapList(node.options);
|
|
2259
|
-
if (options && options.length) {
|
|
2260
|
-
options.forEach((opt) => {
|
|
2261
|
-
output.push(this.deparse(opt, 'sequence'));
|
|
2262
|
-
});
|
|
2263
|
-
}
|
|
2264
|
-
return output.join(' ');
|
|
2265
|
-
}
|
|
2266
|
-
CreateTableAsStmt(node, context = {}) {
|
|
2267
|
-
const output = ['CREATE'];
|
|
2268
|
-
const relpersistence = dotty.get(node, 'into.rel.relpersistence');
|
|
2269
|
-
if (node.relkind === 'OBJECT_MATVIEW') {
|
|
2270
|
-
output.push('MATERIALIZED VIEW');
|
|
2271
|
-
}
|
|
2272
|
-
else if (relpersistence !== 't') {
|
|
2273
|
-
output.push('TABLE');
|
|
2274
|
-
if (node.if_not_exists) {
|
|
2275
|
-
output.push('IF NOT EXISTS');
|
|
2276
|
-
}
|
|
2277
|
-
}
|
|
2278
|
-
output.push(this.IntoClause(node.into, context));
|
|
2279
|
-
output.push('AS');
|
|
2280
|
-
output.push(this.deparse(node.query, context));
|
|
2281
|
-
return output.join(' ');
|
|
2282
|
-
}
|
|
2283
|
-
CreateTrigStmt(node, context = {}) {
|
|
2284
|
-
const output = [];
|
|
2285
|
-
output.push('CREATE');
|
|
2286
|
-
if (node.isconstraint) {
|
|
2287
|
-
output.push('CONSTRAINT');
|
|
2288
|
-
}
|
|
2289
|
-
output.push('TRIGGER');
|
|
2290
|
-
output.push(this.quote(node.trigname));
|
|
2291
|
-
output.push(NEWLINE_CHAR);
|
|
2292
|
-
// int16 timing; BEFORE, AFTER, or INSTEAD
|
|
2293
|
-
if (node.timing === 64) {
|
|
2294
|
-
output.push('INSTEAD OF');
|
|
2295
|
-
}
|
|
2296
|
-
else if (node.timing === 2) {
|
|
2297
|
-
output.push('BEFORE');
|
|
2298
|
-
}
|
|
2299
|
-
else {
|
|
2300
|
-
output.push('AFTER');
|
|
2301
|
-
}
|
|
2302
|
-
// int16 events; "OR" of INSERT/UPDATE/DELETE/TRUNCATE
|
|
2303
|
-
// 4 = 0b000100 (insert)
|
|
2304
|
-
// 8 = 0b001000 (delete)
|
|
2305
|
-
// 16 = 0b010000 (update)
|
|
2306
|
-
// 32 = 0b100000 (TRUNCATE)
|
|
2307
|
-
const TRIGGER_EVENTS = {
|
|
2308
|
-
INSERT: 4,
|
|
2309
|
-
DELETE: 8,
|
|
2310
|
-
UPDATE: 16,
|
|
2311
|
-
TRUNCATE: 32
|
|
2312
|
-
};
|
|
2313
|
-
const events = [];
|
|
2314
|
-
if ((TRIGGER_EVENTS.INSERT & node.events) === TRIGGER_EVENTS.INSERT) {
|
|
2315
|
-
events.push('INSERT');
|
|
2316
|
-
}
|
|
2317
|
-
if ((TRIGGER_EVENTS.UPDATE & node.events) === TRIGGER_EVENTS.UPDATE) {
|
|
2318
|
-
events.push('UPDATE');
|
|
2319
|
-
}
|
|
2320
|
-
if ((TRIGGER_EVENTS.DELETE & node.events) === TRIGGER_EVENTS.DELETE) {
|
|
2321
|
-
events.push('DELETE');
|
|
2322
|
-
}
|
|
2323
|
-
if ((TRIGGER_EVENTS.TRUNCATE & node.events) === TRIGGER_EVENTS.TRUNCATE) {
|
|
2324
|
-
events.push('TRUNCATE');
|
|
2325
|
-
}
|
|
2326
|
-
// events
|
|
2327
|
-
output.push(events.join(' OR '));
|
|
2328
|
-
// columns
|
|
2329
|
-
if (node.columns) {
|
|
2330
|
-
output.push('OF');
|
|
2331
|
-
output.push(this.list(node.columns, ', ', '', context));
|
|
2332
|
-
}
|
|
2333
|
-
// ON
|
|
2334
|
-
output.push('ON');
|
|
2335
|
-
output.push(this.RangeVar(node.relation, context));
|
|
2336
|
-
output.push(NEWLINE_CHAR);
|
|
2337
|
-
if (node.transitionRels) {
|
|
2338
|
-
output.push('REFERENCING');
|
|
2339
|
-
node.transitionRels.forEach(({ TriggerTransition }) => {
|
|
2340
|
-
if (TriggerTransition.isNew === true &&
|
|
2341
|
-
TriggerTransition.isTable === true) {
|
|
2342
|
-
output.push(`NEW TABLE AS ${TriggerTransition.name}`);
|
|
2343
|
-
}
|
|
2344
|
-
else if (TriggerTransition.isNew !== true &&
|
|
2345
|
-
TriggerTransition.isTable === true) {
|
|
2346
|
-
output.push(`OLD TABLE AS ${TriggerTransition.name}`);
|
|
2347
|
-
}
|
|
2348
|
-
});
|
|
2349
|
-
}
|
|
2350
|
-
// opts
|
|
2351
|
-
if (node.deferrable || node.initdeferred) {
|
|
2352
|
-
if (node.deferrable) {
|
|
2353
|
-
output.push('DEFERRABLE');
|
|
2354
|
-
}
|
|
2355
|
-
if (node.deferrable) {
|
|
2356
|
-
output.push('INITIALLY DEFERRED');
|
|
2357
|
-
}
|
|
2358
|
-
output.push(NEWLINE_CHAR);
|
|
2359
|
-
}
|
|
2360
|
-
if (node.row) {
|
|
2361
|
-
output.push(`FOR EACH ROW${NEWLINE_CHAR}`);
|
|
2362
|
-
}
|
|
2363
|
-
else {
|
|
2364
|
-
output.push(`FOR EACH STATEMENT${NEWLINE_CHAR}`);
|
|
2365
|
-
}
|
|
2366
|
-
if (node.whenClause) {
|
|
2367
|
-
output.push('WHEN');
|
|
2368
|
-
output.push('(');
|
|
2369
|
-
output.push(this.deparse(node.whenClause, 'trigger'));
|
|
2370
|
-
output.push(')');
|
|
2371
|
-
output.push(NEWLINE_CHAR);
|
|
2372
|
-
}
|
|
2373
|
-
output.push('EXECUTE PROCEDURE');
|
|
2374
|
-
output.push(this.listQuotes(node.funcname).split(',').join('.'));
|
|
2375
|
-
output.push('(');
|
|
2376
|
-
let args = [];
|
|
2377
|
-
if (node.args) {
|
|
2378
|
-
args = unwrapList(node.args);
|
|
2379
|
-
}
|
|
2380
|
-
// seems that it's only parsing strings?
|
|
2381
|
-
args = args
|
|
2382
|
-
.map((arg) => {
|
|
2383
|
-
if (arg.String !== undefined && arg.String.str !== undefined) {
|
|
2384
|
-
return `'${arg.String.str}'`;
|
|
2385
|
-
}
|
|
2386
|
-
return this.deparse(arg, context);
|
|
2387
|
-
})
|
|
2388
|
-
.filter((a) => a);
|
|
2389
|
-
output.push(args.join(','));
|
|
2390
|
-
output.push(')');
|
|
2391
|
-
return output.join(' ');
|
|
2392
|
-
}
|
|
2393
|
-
CreateDomainStmt(node, context = {}) {
|
|
2394
|
-
const output = [];
|
|
2395
|
-
output.push('CREATE DOMAIN');
|
|
2396
|
-
output.push(this.list(node.domainname, '.', '', context));
|
|
2397
|
-
output.push('AS');
|
|
2398
|
-
output.push(this.TypeName(node.typeName, context));
|
|
2399
|
-
if (node.constraints) {
|
|
2400
|
-
output.push(this.list(node.constraints, ', ', '', context));
|
|
2401
|
-
}
|
|
2402
|
-
return output.join(' ');
|
|
2403
|
-
}
|
|
2404
|
-
CreateStmt(node, context = {}) {
|
|
2405
|
-
const output = [];
|
|
2406
|
-
const relpersistence = dotty.get(node, 'relation.relpersistence');
|
|
2407
|
-
if (relpersistence === 't') {
|
|
2408
|
-
output.push('CREATE');
|
|
2409
|
-
}
|
|
2410
|
-
else {
|
|
2411
|
-
output.push('CREATE TABLE');
|
|
2412
|
-
if (node.if_not_exists) {
|
|
2413
|
-
output.push('IF NOT EXISTS');
|
|
2414
|
-
}
|
|
2415
|
-
}
|
|
2416
|
-
output.push(this.RangeVar(node.relation, context));
|
|
2417
|
-
output.push(`(${NEWLINE_CHAR}`);
|
|
2418
|
-
output.push(this.list(node.tableElts, `,${NEWLINE_CHAR}`, TAB_CHAR, context));
|
|
2419
|
-
output.push(`${NEWLINE_CHAR})`);
|
|
2420
|
-
if (node.hasOwnProperty('inhRelations')) {
|
|
2421
|
-
output.push('INHERITS');
|
|
2422
|
-
output.push('(');
|
|
2423
|
-
output.push(this.list(node.inhRelations, ', ', '', context));
|
|
2424
|
-
output.push(')');
|
|
2425
|
-
}
|
|
2426
|
-
if (node.options) {
|
|
2427
|
-
// TODO was this deprecated?
|
|
2428
|
-
node.options.forEach((opt) => {
|
|
2429
|
-
if (dotty.get(opt, 'DefElem.defname') === 'oids') {
|
|
2430
|
-
if (Number(dotty.get(opt, 'DefElem.arg.Integer.ival')) === 1) {
|
|
2431
|
-
output.push('WITH OIDS');
|
|
2432
|
-
}
|
|
2433
|
-
else {
|
|
2434
|
-
output.push('WITHOUT OIDS');
|
|
2435
|
-
}
|
|
2436
|
-
}
|
|
2437
|
-
});
|
|
2438
|
-
}
|
|
2439
|
-
return output.join(' ');
|
|
2440
|
-
}
|
|
2441
|
-
ConstraintStmt(node) {
|
|
2442
|
-
const output = [];
|
|
2443
|
-
const constraint = (0, pgsql_enums_1.getConstraintFromConstrType)(node.contype);
|
|
2444
|
-
if (node.conname) {
|
|
2445
|
-
output.push('CONSTRAINT');
|
|
2446
|
-
output.push(node.conname);
|
|
2447
|
-
if (!node.pktable) {
|
|
2448
|
-
output.push(constraint);
|
|
2449
|
-
}
|
|
2450
|
-
}
|
|
2451
|
-
else if (node.contype === 'CONSTR_IDENTITY') {
|
|
2452
|
-
// IDENTITY
|
|
2453
|
-
output.push('GENERATED');
|
|
2454
|
-
if (node.generated_when == 'a') {
|
|
2455
|
-
output.push('ALWAYS AS');
|
|
2456
|
-
}
|
|
2457
|
-
else {
|
|
2458
|
-
output.push('BY DEFAULT AS');
|
|
2459
|
-
}
|
|
2460
|
-
output.push('IDENTITY');
|
|
2461
|
-
const options = unwrapList(node.options);
|
|
2462
|
-
if (options && options.length) {
|
|
2463
|
-
output.push('(');
|
|
2464
|
-
output.push(this.list(options, ' ', '', 'generated'));
|
|
2465
|
-
output.push(')');
|
|
2466
|
-
}
|
|
2467
|
-
}
|
|
2468
|
-
else if (node.contype === 'CONSTR_GENERATED') {
|
|
2469
|
-
output.push('GENERATED');
|
|
2470
|
-
if (node.generated_when == 'a') {
|
|
2471
|
-
output.push('ALWAYS AS');
|
|
2472
|
-
}
|
|
2473
|
-
}
|
|
2474
|
-
else {
|
|
2475
|
-
output.push(constraint);
|
|
2476
|
-
}
|
|
2477
|
-
return output.join(' ');
|
|
2478
|
-
}
|
|
2479
|
-
ReferenceConstraint(node, context = {}) {
|
|
2480
|
-
const output = [];
|
|
2481
|
-
if (node.pk_attrs && node.fk_attrs) {
|
|
2482
|
-
if (node.conname) {
|
|
2483
|
-
output.push('CONSTRAINT');
|
|
2484
|
-
output.push(node.conname);
|
|
2485
|
-
}
|
|
2486
|
-
output.push('FOREIGN KEY');
|
|
2487
|
-
output.push('(');
|
|
2488
|
-
output.push(this.listQuotes(node.fk_attrs));
|
|
2489
|
-
output.push(')');
|
|
2490
|
-
output.push('REFERENCES');
|
|
2491
|
-
output.push(this.RangeVar(node.pktable, context));
|
|
2492
|
-
output.push('(');
|
|
2493
|
-
output.push(this.listQuotes(node.pk_attrs));
|
|
2494
|
-
output.push(')');
|
|
2495
|
-
}
|
|
2496
|
-
else if (node.pk_attrs) {
|
|
2497
|
-
output.push(this.ConstraintStmt(node, context));
|
|
2498
|
-
output.push(this.RangeVar(node.pktable, context));
|
|
2499
|
-
output.push('(');
|
|
2500
|
-
output.push(this.listQuotes(node.pk_attrs));
|
|
2501
|
-
output.push(')');
|
|
2502
|
-
}
|
|
2503
|
-
else if (node.fk_attrs) {
|
|
2504
|
-
if (node.conname) {
|
|
2505
|
-
output.push('CONSTRAINT');
|
|
2506
|
-
output.push(node.conname);
|
|
2507
|
-
}
|
|
2508
|
-
output.push('FOREIGN KEY');
|
|
2509
|
-
output.push('(');
|
|
2510
|
-
output.push(this.listQuotes(node.fk_attrs));
|
|
2511
|
-
output.push(')');
|
|
2512
|
-
output.push('REFERENCES');
|
|
2513
|
-
output.push(this.RangeVar(node.pktable, context));
|
|
2514
|
-
}
|
|
2515
|
-
else {
|
|
2516
|
-
output.push(this.ConstraintStmt(node, context));
|
|
2517
|
-
output.push(this.RangeVar(node.pktable, context));
|
|
2518
|
-
}
|
|
2519
|
-
return output.join(' ');
|
|
2520
|
-
}
|
|
2521
|
-
ExclusionConstraint(node, context = {}) {
|
|
2522
|
-
const output = [];
|
|
2523
|
-
function getExclusionGroup(nde) {
|
|
2524
|
-
const exclusions = unwrapList(nde.exclusions);
|
|
2525
|
-
const a = exclusions.map((excl) => {
|
|
2526
|
-
const firstExcl = unwrapList(excl)[0];
|
|
2527
|
-
if (firstExcl.IndexElem.name) {
|
|
2528
|
-
return firstExcl.IndexElem.name;
|
|
2529
|
-
}
|
|
2530
|
-
return firstExcl.IndexElem.expr
|
|
2531
|
-
? this.deparse(firstExcl.IndexElem.expr, context)
|
|
2532
|
-
: null;
|
|
2533
|
-
});
|
|
2534
|
-
const b = exclusions.map((excl) => this.deparse(unwrapList(unwrapList(excl)[1])[0], context));
|
|
2535
|
-
const stmts = a.map((_v, i) => `${a[i]} WITH ${b[i]}`);
|
|
2536
|
-
return stmts.join(', ');
|
|
2537
|
-
}
|
|
2538
|
-
if (node.exclusions && node.access_method) {
|
|
2539
|
-
output.push('USING');
|
|
2540
|
-
output.push(node.access_method);
|
|
2541
|
-
output.push('(');
|
|
2542
|
-
output.push(getExclusionGroup.call(this, node));
|
|
2543
|
-
output.push(')');
|
|
2544
|
-
}
|
|
2545
|
-
return output.join(' ');
|
|
2546
|
-
}
|
|
2547
|
-
Constraint(node, context = {}) {
|
|
2548
|
-
const output = [];
|
|
2549
|
-
if (node.contype === 'CONSTR_FOREIGN') {
|
|
2550
|
-
output.push(this.ReferenceConstraint(node, context));
|
|
2551
|
-
}
|
|
2552
|
-
else {
|
|
2553
|
-
output.push(this.ConstraintStmt(node, context));
|
|
2554
|
-
}
|
|
2555
|
-
if (node.keys) {
|
|
2556
|
-
output.push('(');
|
|
2557
|
-
output.push(this.listQuotes(node.keys));
|
|
2558
|
-
output.push(')');
|
|
2559
|
-
}
|
|
2560
|
-
if (node.raw_expr) {
|
|
2561
|
-
output.push('(');
|
|
2562
|
-
output.push(this.deparse(node.raw_expr, context));
|
|
2563
|
-
output.push(')');
|
|
2564
|
-
if (node.contype == 'CONSTR_GENERATED') {
|
|
2565
|
-
output.push('STORED');
|
|
2566
|
-
}
|
|
2567
|
-
}
|
|
2568
|
-
if (node.fk_del_action) {
|
|
2569
|
-
switch (node.fk_del_action) {
|
|
2570
|
-
case 'r':
|
|
2571
|
-
output.push('ON DELETE RESTRICT');
|
|
2572
|
-
break;
|
|
2573
|
-
case 'c':
|
|
2574
|
-
output.push('ON DELETE CASCADE');
|
|
2575
|
-
break;
|
|
2576
|
-
case 'n':
|
|
2577
|
-
output.push('ON DELETE SET NULL');
|
|
2578
|
-
break;
|
|
2579
|
-
case 'd':
|
|
2580
|
-
output.push('ON DELETE SET DEFAULT');
|
|
2581
|
-
break;
|
|
2582
|
-
case 'a':
|
|
2583
|
-
// output.push('ON DELETE NO ACTION');
|
|
2584
|
-
break;
|
|
2585
|
-
default:
|
|
2586
|
-
}
|
|
2587
|
-
}
|
|
2588
|
-
if (node.fk_upd_action) {
|
|
2589
|
-
switch (node.fk_upd_action) {
|
|
2590
|
-
case 'r':
|
|
2591
|
-
output.push('ON UPDATE RESTRICT');
|
|
2592
|
-
break;
|
|
2593
|
-
case 'c':
|
|
2594
|
-
output.push('ON UPDATE CASCADE');
|
|
2595
|
-
break;
|
|
2596
|
-
case 'n':
|
|
2597
|
-
output.push('ON UPDATE SET NULL');
|
|
2598
|
-
break;
|
|
2599
|
-
case 'd':
|
|
2600
|
-
output.push('ON UPDATE SET DEFAULT');
|
|
2601
|
-
break;
|
|
2602
|
-
case 'a':
|
|
2603
|
-
// output.push('ON UPDATE NO ACTION');
|
|
2604
|
-
break;
|
|
2605
|
-
default:
|
|
2606
|
-
}
|
|
2607
|
-
}
|
|
2608
|
-
if (node.fk_matchtype === 'f') {
|
|
2609
|
-
output.push('MATCH FULL');
|
|
2610
|
-
}
|
|
2611
|
-
if (node.is_no_inherit === true) {
|
|
2612
|
-
output.push('NO INHERIT');
|
|
2613
|
-
}
|
|
2614
|
-
if (node.skip_validation === true) {
|
|
2615
|
-
output.push('NOT VALID');
|
|
2616
|
-
}
|
|
2617
|
-
if (node.contype === 'CONSTR_EXCLUSION') {
|
|
2618
|
-
output.push(this.ExclusionConstraint(node, context));
|
|
2619
|
-
}
|
|
2620
|
-
if (node.deferrable) {
|
|
2621
|
-
output.push('deferrable');
|
|
2622
|
-
}
|
|
2623
|
-
return output.join(' ');
|
|
2624
|
-
}
|
|
2625
|
-
AccessPriv(node) {
|
|
2626
|
-
const output = [];
|
|
2627
|
-
if (node.priv_name) {
|
|
2628
|
-
output.push(node.priv_name.toUpperCase());
|
|
2629
|
-
}
|
|
2630
|
-
else {
|
|
2631
|
-
output.push('ALL');
|
|
2632
|
-
}
|
|
2633
|
-
if (node.cols) {
|
|
2634
|
-
output.push('(');
|
|
2635
|
-
output.push(this.listQuotes(node.cols));
|
|
2636
|
-
output.push(')');
|
|
2637
|
-
}
|
|
2638
|
-
return output.join(' ');
|
|
2639
|
-
}
|
|
2640
|
-
VariableSetStmt(node) {
|
|
2641
|
-
switch (node.kind) {
|
|
2642
|
-
case 'VAR_SET_VALUE':
|
|
2643
|
-
return (0, util_1.format)('SET %s%s = %s', node.is_local ? 'LOCAL ' : '', node.name, this.deparseNodes(node.args, 'simple').join(', '));
|
|
2644
|
-
case 'VAR_SET_DEFAULT':
|
|
2645
|
-
return (0, util_1.format)('SET %s TO DEFAULT', node.name);
|
|
2646
|
-
case 'VAR_SET_CURRENT':
|
|
2647
|
-
return (0, util_1.format)('SET %s FROM CURRENT', node.name);
|
|
2648
|
-
case 'VAR_SET_MULTI': {
|
|
2649
|
-
const name = {
|
|
2650
|
-
TRANSACTION: 'TRANSACTION',
|
|
2651
|
-
'SESSION CHARACTERISTICS': 'SESSION CHARACTERISTICS AS TRANSACTION'
|
|
2652
|
-
}[node.name];
|
|
2653
|
-
return (0, util_1.format)('SET %s %s', name, this.deparseNodes(node.args, 'simple').join(', '));
|
|
2654
|
-
}
|
|
2655
|
-
case 'VAR_RESET':
|
|
2656
|
-
return (0, util_1.format)('RESET %s', node.name);
|
|
2657
|
-
case 'VAR_RESET_ALL':
|
|
2658
|
-
return 'RESET ALL';
|
|
2659
|
-
default:
|
|
2660
|
-
return fail('VariableSetKind', node);
|
|
2661
|
-
}
|
|
2662
|
-
}
|
|
2663
|
-
VariableShowStmt(node) {
|
|
2664
|
-
return (0, util_1.format)('SHOW %s', node.name);
|
|
2665
|
-
}
|
|
2666
|
-
FuncWithArgs(node, context = {}) {
|
|
2667
|
-
const output = [];
|
|
2668
|
-
output.push(this.deparse(unwrapList(node.funcname)[0], context));
|
|
2669
|
-
output.push('(');
|
|
2670
|
-
output.push(this.list(node.funcargs, ', ', '', context));
|
|
2671
|
-
output.push(')');
|
|
2672
|
-
return output.join(' ');
|
|
2673
|
-
}
|
|
2674
|
-
FunctionParameter(node, context = {}) {
|
|
2675
|
-
const output = [];
|
|
2676
|
-
if (node.mode === 'FUNC_PARAM_VARIADIC') {
|
|
2677
|
-
output.push('VARIADIC');
|
|
2678
|
-
}
|
|
2679
|
-
if (node.mode === 'FUNC_PARAM_OUT') {
|
|
2680
|
-
output.push('OUT');
|
|
2681
|
-
}
|
|
2682
|
-
if (node.mode === 'FUNC_PARAM_INOUT') {
|
|
2683
|
-
output.push('INOUT');
|
|
2684
|
-
}
|
|
2685
|
-
output.push(node.name);
|
|
2686
|
-
output.push(this.TypeName(node.argType, context));
|
|
2687
|
-
if (node.defexpr) {
|
|
2688
|
-
output.push('DEFAULT');
|
|
2689
|
-
output.push(this.deparse(node.defexpr, context));
|
|
2690
|
-
}
|
|
2691
|
-
return output.join(' ');
|
|
2692
|
-
}
|
|
2693
|
-
CreateFunctionStmt(node, context = {}) {
|
|
2694
|
-
const output = [];
|
|
2695
|
-
output.push('CREATE');
|
|
2696
|
-
if (node.replace) {
|
|
2697
|
-
output.push('OR REPLACE');
|
|
2698
|
-
}
|
|
2699
|
-
output.push('FUNCTION');
|
|
2700
|
-
output.push(unwrapList(node.funcname)
|
|
2701
|
-
.map((name) => this.deparse(name, context))
|
|
2702
|
-
.join('.'));
|
|
2703
|
-
output.push('(');
|
|
2704
|
-
let parameters = [];
|
|
2705
|
-
if (node.parameters) {
|
|
2706
|
-
parameters = unwrapList(node.parameters);
|
|
2707
|
-
}
|
|
2708
|
-
const parametersList = parameters.filter(({ FunctionParameter }) => FunctionParameter.mode === 'FUNC_PARAM_VARIADIC' ||
|
|
2709
|
-
FunctionParameter.mode === 'FUNC_PARAM_OUT' ||
|
|
2710
|
-
FunctionParameter.mode === 'FUNC_PARAM_INOUT' ||
|
|
2711
|
-
FunctionParameter.mode === 'FUNC_PARAM_IN');
|
|
2712
|
-
output.push(this.list(parametersList));
|
|
2713
|
-
output.push(')');
|
|
2714
|
-
const returns = parameters.filter(({ FunctionParameter }) => FunctionParameter.mode === 'FUNC_PARAM_TABLE');
|
|
2715
|
-
if (returns.length > 0) {
|
|
2716
|
-
output.push('RETURNS');
|
|
2717
|
-
output.push('TABLE');
|
|
2718
|
-
output.push('(');
|
|
2719
|
-
output.push(this.list(returns, ', ', '', context));
|
|
2720
|
-
output.push(')');
|
|
2721
|
-
}
|
|
2722
|
-
else if (node.returnType) {
|
|
2723
|
-
output.push('RETURNS');
|
|
2724
|
-
output.push(this.TypeName(node.returnType, context));
|
|
2725
|
-
}
|
|
2726
|
-
node.options.forEach((option, i) => {
|
|
2727
|
-
if (option && option.DefElem) {
|
|
2728
|
-
let value = '';
|
|
2729
|
-
switch (option.DefElem.defname) {
|
|
2730
|
-
case 'as':
|
|
2731
|
-
value = this.deparse(unwrapList(option.DefElem.arg)[0], context);
|
|
2732
|
-
output.push(`AS $EOFCODE$${value}$EOFCODE$`);
|
|
2733
|
-
break;
|
|
2734
|
-
case 'language':
|
|
2735
|
-
value = this.deparse(option.DefElem.arg, context);
|
|
2736
|
-
output.push('LANGUAGE');
|
|
2737
|
-
output.push(value);
|
|
2738
|
-
break;
|
|
2739
|
-
case 'security':
|
|
2740
|
-
output.push('SECURITY');
|
|
2741
|
-
value = Number(option.DefElem.arg.Integer.ival);
|
|
2742
|
-
if (value > 0) {
|
|
2743
|
-
output.push('DEFINER');
|
|
2744
|
-
}
|
|
2745
|
-
else {
|
|
2746
|
-
output.push('INVOKER');
|
|
2747
|
-
}
|
|
2748
|
-
break;
|
|
2749
|
-
case 'leakproof':
|
|
2750
|
-
value = Number(option.DefElem.arg.Integer.ival);
|
|
2751
|
-
if (value > 0) {
|
|
2752
|
-
output.push('LEAKPROOF');
|
|
2753
|
-
}
|
|
2754
|
-
break;
|
|
2755
|
-
case 'window':
|
|
2756
|
-
value = Number(option.DefElem.arg.Integer.ival);
|
|
2757
|
-
if (value > 0) {
|
|
2758
|
-
output.push('WINDOW');
|
|
2759
|
-
}
|
|
2760
|
-
break;
|
|
2761
|
-
case 'strict':
|
|
2762
|
-
value = Number(option.DefElem.arg.Integer.ival);
|
|
2763
|
-
if (value > 0) {
|
|
2764
|
-
output.push('STRICT');
|
|
2765
|
-
}
|
|
2766
|
-
else {
|
|
2767
|
-
output.push('CALLED ON NULL INPUT');
|
|
2768
|
-
}
|
|
2769
|
-
break;
|
|
2770
|
-
case 'set':
|
|
2771
|
-
output.push(this.deparse(option, context));
|
|
2772
|
-
break;
|
|
2773
|
-
case 'volatility':
|
|
2774
|
-
value = this.deparse(option.DefElem.arg, context);
|
|
2775
|
-
output.push(value.toUpperCase());
|
|
2776
|
-
break;
|
|
2777
|
-
default:
|
|
2778
|
-
}
|
|
2779
|
-
}
|
|
2780
|
-
});
|
|
2781
|
-
return output.join(' ');
|
|
2782
|
-
}
|
|
2783
|
-
CreateSchemaStmt(node) {
|
|
2784
|
-
const output = [];
|
|
2785
|
-
output.push('CREATE');
|
|
2786
|
-
if (node.replace) {
|
|
2787
|
-
output.push('OR REPLACE');
|
|
2788
|
-
}
|
|
2789
|
-
output.push('SCHEMA');
|
|
2790
|
-
if (node.if_not_exists) {
|
|
2791
|
-
output.push('IF NOT EXISTS');
|
|
2792
|
-
}
|
|
2793
|
-
output.push(node.schemaname);
|
|
2794
|
-
return output.join(' ');
|
|
2795
|
-
}
|
|
2796
|
-
RoleSpec(node) {
|
|
2797
|
-
switch (node.roletype) {
|
|
2798
|
-
case 'ROLESPEC_CSTRING':
|
|
2799
|
-
return this.quote(node.rolename);
|
|
2800
|
-
case 'ROLESPEC_CURRENT_USER':
|
|
2801
|
-
return 'CURRENT_USER';
|
|
2802
|
-
case 'ROLESPEC_SESSION_USER':
|
|
2803
|
-
return 'SESSION_USER';
|
|
2804
|
-
case 'ROLESPEC_PUBLIC':
|
|
2805
|
-
return 'PUBLIC';
|
|
2806
|
-
default:
|
|
2807
|
-
return fail('RoleSpec', node);
|
|
2808
|
-
}
|
|
2809
|
-
}
|
|
2810
|
-
GrantStmt(node) {
|
|
2811
|
-
const output = [];
|
|
2812
|
-
const getTypeFromNode = (nodeObj) => {
|
|
2813
|
-
switch (nodeObj.objtype) {
|
|
2814
|
-
case 'OBJECT_TABLE':
|
|
2815
|
-
if (nodeObj.targtype === 'ACL_TARGET_ALL_IN_SCHEMA') {
|
|
2816
|
-
return 'ALL TABLES IN SCHEMA';
|
|
2817
|
-
}
|
|
2818
|
-
if (nodeObj.targtype === 'ACL_TARGET_DEFAULTS') {
|
|
2819
|
-
return 'TABLES';
|
|
2820
|
-
}
|
|
2821
|
-
// todo could be view
|
|
2822
|
-
return 'TABLE';
|
|
2823
|
-
case 'OBJECT_SEQUENCE':
|
|
2824
|
-
if (nodeObj.targtype === 'ACL_TARGET_ALL_IN_SCHEMA') {
|
|
2825
|
-
return 'ALL SEQUENCES IN SCHEMA';
|
|
2826
|
-
}
|
|
2827
|
-
if (nodeObj.targtype === 'ACL_TARGET_DEFAULTS') {
|
|
2828
|
-
return 'SEQUENCES';
|
|
2829
|
-
}
|
|
2830
|
-
return 'SEQUENCE';
|
|
2831
|
-
case 'OBJECT_DATABASE':
|
|
2832
|
-
return 'DATABASE';
|
|
2833
|
-
case 'OBJECT_DOMAIN':
|
|
2834
|
-
return 'DOMAIN';
|
|
2835
|
-
case 'OBJECT_FDW':
|
|
2836
|
-
return 'FOREIGN DATA WRAPPER';
|
|
2837
|
-
case 'OBJECT_FOREIGN_SERVER':
|
|
2838
|
-
return 'FOREIGN SERVER';
|
|
2839
|
-
case 'OBJECT_FUNCTION':
|
|
2840
|
-
if (nodeObj.targtype === 'ACL_TARGET_ALL_IN_SCHEMA') {
|
|
2841
|
-
return 'ALL FUNCTIONS IN SCHEMA';
|
|
2842
|
-
}
|
|
2843
|
-
if (nodeObj.targtype === 'ACL_TARGET_DEFAULTS') {
|
|
2844
|
-
return 'FUNCTIONS';
|
|
2845
|
-
}
|
|
2846
|
-
return 'FUNCTION';
|
|
2847
|
-
case 'OBJECT_LANGUAGE':
|
|
2848
|
-
return 'LANGUAGE';
|
|
2849
|
-
case 'OBJECT_LARGEOBJECT':
|
|
2850
|
-
return 'LARGE OBJECT';
|
|
2851
|
-
case 'OBJECT_SCHEMA':
|
|
2852
|
-
return 'SCHEMA';
|
|
2853
|
-
case 'OBJECT_TABLESPACE':
|
|
2854
|
-
return 'TABLESPACE';
|
|
2855
|
-
case 'OBJECT_TYPE':
|
|
2856
|
-
return 'TYPE';
|
|
2857
|
-
default:
|
|
2858
|
-
}
|
|
2859
|
-
return fail('GrantStmt', node);
|
|
2860
|
-
};
|
|
2861
|
-
if (node.objtype !== 'OBJECT_COLUMN') {
|
|
2862
|
-
if (!node.is_grant) {
|
|
2863
|
-
output.push('REVOKE');
|
|
2864
|
-
if (node.grant_option) {
|
|
2865
|
-
output.push('GRANT OPTION');
|
|
2866
|
-
output.push('FOR');
|
|
2867
|
-
}
|
|
2868
|
-
if (node.privileges) {
|
|
2869
|
-
output.push(this.list(node.privileges));
|
|
2870
|
-
}
|
|
2871
|
-
else {
|
|
2872
|
-
output.push('ALL');
|
|
2873
|
-
}
|
|
2874
|
-
output.push('ON');
|
|
2875
|
-
output.push(getTypeFromNode(node));
|
|
2876
|
-
output.push(this.list(node.objects));
|
|
2877
|
-
output.push('FROM');
|
|
2878
|
-
output.push(this.list(node.grantees));
|
|
2879
|
-
}
|
|
2880
|
-
else {
|
|
2881
|
-
output.push('GRANT');
|
|
2882
|
-
if (node.privileges) {
|
|
2883
|
-
output.push(this.list(node.privileges));
|
|
2884
|
-
}
|
|
2885
|
-
else {
|
|
2886
|
-
output.push('ALL');
|
|
2887
|
-
}
|
|
2888
|
-
output.push('ON');
|
|
2889
|
-
output.push(getTypeFromNode(node));
|
|
2890
|
-
output.push(this.list(node.objects));
|
|
2891
|
-
output.push('TO');
|
|
2892
|
-
output.push(this.list(node.grantees));
|
|
2893
|
-
if (node.grant_option) {
|
|
2894
|
-
output.push('WITH GRANT OPTION');
|
|
2895
|
-
}
|
|
2896
|
-
}
|
|
2897
|
-
if (node.behavior === 'DROP_CASCADE') {
|
|
2898
|
-
output.push('CASCADE');
|
|
2899
|
-
}
|
|
2900
|
-
}
|
|
2901
|
-
return output.join(' ');
|
|
2902
|
-
}
|
|
2903
|
-
GrantRoleStmt(node, context = {}) {
|
|
2904
|
-
const output = [];
|
|
2905
|
-
if (!node.is_grant) {
|
|
2906
|
-
output.push('REVOKE');
|
|
2907
|
-
output.push(this.list(node.granted_roles, ', ', '', context));
|
|
2908
|
-
output.push('FROM');
|
|
2909
|
-
output.push(this.list(node.grantee_roles, ', ', '', context));
|
|
2910
|
-
}
|
|
2911
|
-
else {
|
|
2912
|
-
output.push('GRANT');
|
|
2913
|
-
output.push(this.list(node.granted_roles, ', ', '', context));
|
|
2914
|
-
output.push('TO');
|
|
2915
|
-
output.push(this.list(node.grantee_roles, ', ', '', context));
|
|
2916
|
-
}
|
|
2917
|
-
if (node.admin_opt) {
|
|
2918
|
-
output.push('WITH ADMIN OPTION');
|
|
2919
|
-
}
|
|
2920
|
-
return output.join(' ');
|
|
2921
|
-
}
|
|
2922
|
-
CreateRoleStmt(node, context = {}) {
|
|
2923
|
-
const output = [];
|
|
2924
|
-
const roleOption = (nodeObj, i, val1, val2) => {
|
|
2925
|
-
const val = Number(dotty.get(unwrapList(nodeObj.options), `${i}.DefElem.arg.Integer.ival`));
|
|
2926
|
-
if (val > 0) {
|
|
2927
|
-
output.push(val1);
|
|
2928
|
-
}
|
|
2929
|
-
else {
|
|
2930
|
-
output.push(val2);
|
|
2931
|
-
}
|
|
2932
|
-
};
|
|
2933
|
-
output.push('CREATE');
|
|
2934
|
-
switch (node.stmt_type) {
|
|
2935
|
-
case 'ROLESTMT_USER':
|
|
2936
|
-
output.push('USER');
|
|
2937
|
-
break;
|
|
2938
|
-
case 'ROLESTMT_GROUP':
|
|
2939
|
-
output.push('GROUP');
|
|
2940
|
-
break;
|
|
2941
|
-
default:
|
|
2942
|
-
output.push('ROLE');
|
|
2943
|
-
}
|
|
2944
|
-
output.push(`"${node.role}"`);
|
|
2945
|
-
if (node.options) {
|
|
2946
|
-
const options = unwrapList(node.options);
|
|
2947
|
-
const opts = dotty.search(options, '*.DefElem.defname');
|
|
2948
|
-
if (opts.length === 1 && opts[0] === 'addroleto') {
|
|
2949
|
-
// only one case
|
|
2950
|
-
}
|
|
2951
|
-
else {
|
|
2952
|
-
output.push('WITH');
|
|
2953
|
-
}
|
|
2954
|
-
opts.forEach((option, i) => {
|
|
2955
|
-
let value = '';
|
|
2956
|
-
switch (option) {
|
|
2957
|
-
case 'canlogin':
|
|
2958
|
-
roleOption(node, i, 'LOGIN', 'NOLOGIN');
|
|
2959
|
-
break;
|
|
2960
|
-
case 'addroleto':
|
|
2961
|
-
output.push('IN ROLE');
|
|
2962
|
-
output.push(dotty
|
|
2963
|
-
.search(flatten(dotty.search(options, `${i}.DefElem.arg`).map(unwrapList)), '*.RoleSpec.rolename')
|
|
2964
|
-
.join(','));
|
|
2965
|
-
break;
|
|
2966
|
-
case 'password':
|
|
2967
|
-
output.push('PASSWORD');
|
|
2968
|
-
value = dotty.get(options, `${i}.DefElem.arg.String.str`);
|
|
2969
|
-
output.push(`'${value}'`);
|
|
2970
|
-
break;
|
|
2971
|
-
case 'adminmembers':
|
|
2972
|
-
output.push('ADMIN');
|
|
2973
|
-
output.push(this.list(options[i].DefElem.arg, ', ', '', context));
|
|
2974
|
-
break;
|
|
2975
|
-
case 'rolemembers':
|
|
2976
|
-
output.push('USER');
|
|
2977
|
-
output.push(this.list(options[i].DefElem.arg, ', ', '', context));
|
|
2978
|
-
break;
|
|
2979
|
-
case 'createdb':
|
|
2980
|
-
roleOption(node, i, 'CREATEDB', 'NOCREATEDB');
|
|
2981
|
-
break;
|
|
2982
|
-
case 'isreplication':
|
|
2983
|
-
roleOption(node, i, 'REPLICATION', 'NOREPLICATION');
|
|
2984
|
-
break;
|
|
2985
|
-
case 'bypassrls':
|
|
2986
|
-
roleOption(node, i, 'BYPASSRLS', 'NOBYPASSRLS');
|
|
2987
|
-
break;
|
|
2988
|
-
case 'inherit':
|
|
2989
|
-
roleOption(node, i, 'INHERIT', 'NOINHERIT');
|
|
2990
|
-
break;
|
|
2991
|
-
case 'superuser':
|
|
2992
|
-
roleOption(node, i, 'SUPERUSER', 'NOSUPERUSER');
|
|
2993
|
-
break;
|
|
2994
|
-
case 'createrole':
|
|
2995
|
-
roleOption(node, i, 'CREATEROLE', 'NOCREATEROLE');
|
|
2996
|
-
break;
|
|
2997
|
-
case 'validUntil':
|
|
2998
|
-
output.push('VALID UNTIL');
|
|
2999
|
-
value = dotty.get(options[i], `DefElem.arg.String.str`);
|
|
3000
|
-
output.push(`'${value}'`);
|
|
3001
|
-
break;
|
|
3002
|
-
default:
|
|
3003
|
-
}
|
|
3004
|
-
});
|
|
3005
|
-
}
|
|
3006
|
-
return output.join(' ');
|
|
3007
|
-
}
|
|
3008
|
-
TransactionStmt(node, context = {}) {
|
|
3009
|
-
const output = [];
|
|
3010
|
-
const begin = (nodeOpts) => {
|
|
3011
|
-
const options = unwrapList(nodeOpts.options);
|
|
3012
|
-
const opts = options ? dotty.search(options, '*.DefElem.defname') : [];
|
|
3013
|
-
if (opts.includes('transaction_read_only')) {
|
|
3014
|
-
const index = opts.indexOf('transaction_read_only');
|
|
3015
|
-
const obj = options[index];
|
|
3016
|
-
let set = false;
|
|
3017
|
-
const flag = Number(this.deparse(dotty.get(obj, 'DefElem.arg'), context));
|
|
3018
|
-
if (flag > 0) {
|
|
3019
|
-
set = true;
|
|
3020
|
-
}
|
|
3021
|
-
if (set) {
|
|
3022
|
-
return 'BEGIN TRANSACTION READ ONLY';
|
|
3023
|
-
}
|
|
3024
|
-
return 'BEGIN TRANSACTION READ WRITE';
|
|
3025
|
-
}
|
|
3026
|
-
if (opts.includes('transaction_isolation')) {
|
|
3027
|
-
const index = opts.indexOf('transaction_isolation');
|
|
3028
|
-
const obj = options[index];
|
|
3029
|
-
const lopts = this.deparse(dotty.get(obj, 'DefElem.arg'), context).replace(/['"]+/g, '');
|
|
3030
|
-
return `BEGIN TRANSACTION ISOLATION LEVEL ${lopts.toUpperCase()}`;
|
|
3031
|
-
}
|
|
3032
|
-
return 'BEGIN';
|
|
3033
|
-
};
|
|
3034
|
-
const start = (nodeOpts) => {
|
|
3035
|
-
const options = unwrapList(nodeOpts.options);
|
|
3036
|
-
const opts = options ? dotty.search(options, '*.DefElem.defname') : [];
|
|
3037
|
-
if (opts.includes('transaction_read_only')) {
|
|
3038
|
-
const index = opts.indexOf('transaction_read_only');
|
|
3039
|
-
const obj = options[index];
|
|
3040
|
-
let set = false;
|
|
3041
|
-
const flag = Number(this.deparse(dotty.get(obj, 'DefElem.arg'), context));
|
|
3042
|
-
if (flag > 0) {
|
|
3043
|
-
set = true;
|
|
3044
|
-
}
|
|
3045
|
-
if (set) {
|
|
3046
|
-
return 'START TRANSACTION READ ONLY';
|
|
3047
|
-
}
|
|
3048
|
-
return 'START TRANSACTION READ WRITE';
|
|
3049
|
-
}
|
|
3050
|
-
return 'START TRANSACTION';
|
|
3051
|
-
};
|
|
3052
|
-
const nodeOptions = unwrapList(node.options);
|
|
3053
|
-
switch (node.kind) {
|
|
3054
|
-
case 'TRANS_STMT_BEGIN':
|
|
3055
|
-
return begin(node);
|
|
3056
|
-
case 'TRANS_STMT_START':
|
|
3057
|
-
return start(node);
|
|
3058
|
-
case 'TRANS_STMT_COMMIT':
|
|
3059
|
-
return 'COMMIT';
|
|
3060
|
-
case 'TRANS_STMT_ROLLBACK':
|
|
3061
|
-
return 'ROLLBACK';
|
|
3062
|
-
case 'TRANS_STMT_SAVEPOINT':
|
|
3063
|
-
output.push('SAVEPOINT');
|
|
3064
|
-
output.push(this.deparse(nodeOptions[0].DefElem.arg, context));
|
|
3065
|
-
break;
|
|
3066
|
-
case 'TRANS_STMT_RELEASE':
|
|
3067
|
-
output.push('RELEASE SAVEPOINT');
|
|
3068
|
-
output.push(this.deparse(nodeOptions[0].DefElem.arg, context));
|
|
3069
|
-
break;
|
|
3070
|
-
case 'TRANS_STMT_ROLLBACK_TO':
|
|
3071
|
-
output.push('ROLLBACK TO');
|
|
3072
|
-
output.push(this.deparse(nodeOptions[0].DefElem.arg, context));
|
|
3073
|
-
break;
|
|
3074
|
-
case 'TRANS_STMT_PREPARE':
|
|
3075
|
-
output.push('PREPARE TRANSACTION');
|
|
3076
|
-
output.push(`'${node.gid}'`);
|
|
3077
|
-
break;
|
|
3078
|
-
case 'TRANS_STMT_COMMIT_PREPARED':
|
|
3079
|
-
output.push('COMMIT PREPARED');
|
|
3080
|
-
output.push(`'${node.gid}'`);
|
|
3081
|
-
break;
|
|
3082
|
-
case 'TRANS_STMT_ROLLBACK_PREPARED':
|
|
3083
|
-
output.push('ROLLBACK PREPARED');
|
|
3084
|
-
output.push(`'${node.gid}'`);
|
|
3085
|
-
break;
|
|
3086
|
-
default:
|
|
3087
|
-
}
|
|
3088
|
-
return output.join(' ');
|
|
3089
|
-
}
|
|
3090
|
-
SortBy(node, context = {}) {
|
|
3091
|
-
const output = [];
|
|
3092
|
-
output.push(this.deparse(node.node, context));
|
|
3093
|
-
switch (node.sortby_dir) {
|
|
3094
|
-
case 'SORTBY_ASC':
|
|
3095
|
-
output.push('ASC');
|
|
3096
|
-
break;
|
|
3097
|
-
case 'SORTBY_DESC':
|
|
3098
|
-
output.push('DESC');
|
|
3099
|
-
break;
|
|
3100
|
-
case 'SORTBY_USING':
|
|
3101
|
-
output.push(`USING ${this.deparseNodes(node.useOp, context)}`);
|
|
3102
|
-
break;
|
|
3103
|
-
case 'SORTBY_DEFAULT':
|
|
3104
|
-
break;
|
|
3105
|
-
default:
|
|
3106
|
-
return fail('SortBy', node);
|
|
3107
|
-
}
|
|
3108
|
-
if (node.sortby_nulls === 'SORTBY_NULLS_FIRST') {
|
|
3109
|
-
output.push('NULLS FIRST');
|
|
3110
|
-
}
|
|
3111
|
-
if (node.sortby_nulls === 'SORTBY_NULLS_LAST') {
|
|
3112
|
-
output.push('NULLS LAST');
|
|
3113
|
-
}
|
|
3114
|
-
return output.join(' ');
|
|
3115
|
-
}
|
|
3116
|
-
ObjectWithArgs(node, context = {}) {
|
|
3117
|
-
const output = [];
|
|
3118
|
-
if (context === 'noquotes') {
|
|
3119
|
-
output.push(this.list(node.objname, ', ', '', context));
|
|
3120
|
-
}
|
|
3121
|
-
else {
|
|
3122
|
-
output.push(this.listQuotes(node.objname, '.'));
|
|
3123
|
-
}
|
|
3124
|
-
const objargs = unwrapList(node.objargs);
|
|
3125
|
-
if (objargs && objargs.length) {
|
|
3126
|
-
output.push('(');
|
|
3127
|
-
output.push(objargs
|
|
3128
|
-
.map((arg) => {
|
|
3129
|
-
if (isEmptyObject(arg)) {
|
|
3130
|
-
return 'NONE';
|
|
3131
|
-
}
|
|
3132
|
-
return this.deparse(arg, context);
|
|
3133
|
-
})
|
|
3134
|
-
.join(','));
|
|
3135
|
-
output.push(')');
|
|
3136
|
-
}
|
|
3137
|
-
else if (!node.args_unspecified) {
|
|
3138
|
-
output.push('()');
|
|
3139
|
-
}
|
|
3140
|
-
return output.join(' ');
|
|
3141
|
-
}
|
|
3142
|
-
String(node) {
|
|
3143
|
-
return node.str;
|
|
3144
|
-
}
|
|
3145
|
-
SubLink(node, context = {}) {
|
|
3146
|
-
switch (true) {
|
|
3147
|
-
case node.subLinkType === 'EXISTS_SUBLINK':
|
|
3148
|
-
return (0, util_1.format)('EXISTS (%s)', this.deparse(node.subselect, context));
|
|
3149
|
-
case node.subLinkType === 'ALL_SUBLINK':
|
|
3150
|
-
return (0, util_1.format)('%s %s ALL (%s)', this.deparse(node.testexpr, context), this.deparse(node.operName[0], context), this.deparse(node.subselect, context));
|
|
3151
|
-
case node.subLinkType === 'ANY_SUBLINK' && !(node.operName != null):
|
|
3152
|
-
return (0, util_1.format)('%s IN (%s)', this.deparse(node.testexpr, context), this.deparse(node.subselect, context));
|
|
3153
|
-
case node.subLinkType === 'ANY_SUBLINK':
|
|
3154
|
-
return (0, util_1.format)('%s %s ANY (%s)', this.deparse(node.testexpr, context), this.deparse(node.operName[0], context), this.deparse(node.subselect, context));
|
|
3155
|
-
case node.subLinkType === 'ROWCOMPARE_SUBLINK':
|
|
3156
|
-
return (0, util_1.format)('%s %s (%s)', this.deparse(node.testexpr, context), this.deparse(node.operName[0], context), this.deparse(node.subselect, context));
|
|
3157
|
-
case node.subLinkType === 'EXPR_SUBLINK':
|
|
3158
|
-
return (0, util_1.format)('(%s)', this.deparse(node.subselect, context));
|
|
3159
|
-
case node.subLinkType === 'MULTIEXPR_SUBLINK':
|
|
3160
|
-
// TODO(zhm) what is this?
|
|
3161
|
-
return fail('SubLink', node);
|
|
3162
|
-
// MULTIEXPR_SUBLINK
|
|
3163
|
-
// format('(%s)', @deparse(node.subselect))
|
|
3164
|
-
case node.subLinkType === 'ARRAY_SUBLINK':
|
|
3165
|
-
return (0, util_1.format)('ARRAY (%s)', this.deparse(node.subselect, context));
|
|
3166
|
-
default:
|
|
3167
|
-
return fail('SubLink', node);
|
|
3168
|
-
}
|
|
3169
|
-
}
|
|
3170
|
-
TypeCast(node, context = {}) {
|
|
3171
|
-
const type = this.TypeName(node.typeName, context);
|
|
3172
|
-
let arg = this.deparse(node.arg, context);
|
|
3173
|
-
if (node.arg !== undefined && node.arg.A_Expr !== undefined) {
|
|
3174
|
-
arg = (0, util_1.format)('(%s)', arg);
|
|
3175
|
-
}
|
|
3176
|
-
if (type === 'boolean') {
|
|
3177
|
-
const value = dotty.get(node, 'arg.A_Const.val.String.str');
|
|
3178
|
-
if (value === 'f') {
|
|
3179
|
-
return 'FALSE';
|
|
3180
|
-
}
|
|
3181
|
-
if (value === 't') {
|
|
3182
|
-
return 'TRUE';
|
|
3183
|
-
}
|
|
3184
|
-
}
|
|
3185
|
-
return (0, util_1.format)('%s::%s', arg, type);
|
|
3186
|
-
}
|
|
3187
|
-
TypeName(node, context = {}) {
|
|
3188
|
-
const names = unwrapList(node.names);
|
|
3189
|
-
if (names[names.length - 1].String.str === 'interval') {
|
|
3190
|
-
return this.deparseInterval(node);
|
|
3191
|
-
}
|
|
3192
|
-
const output = [];
|
|
3193
|
-
if (node.setof) {
|
|
3194
|
-
output.push('SETOF');
|
|
3195
|
-
}
|
|
3196
|
-
let args = null;
|
|
3197
|
-
if (node.typmods != null) {
|
|
3198
|
-
args = unwrapList(node.typmods).map((item) => {
|
|
3199
|
-
return this.deparse(item, context);
|
|
3200
|
-
});
|
|
3201
|
-
}
|
|
3202
|
-
const type = [];
|
|
3203
|
-
type.push(this.type(names, args && args.join(', ')));
|
|
3204
|
-
if (node.arrayBounds != null) {
|
|
3205
|
-
type.push('[]');
|
|
3206
|
-
}
|
|
3207
|
-
output.push(type.join(''));
|
|
3208
|
-
return output.join(' ');
|
|
3209
|
-
}
|
|
3210
|
-
CaseWhen(node, context = {}) {
|
|
3211
|
-
const output = ['WHEN'];
|
|
3212
|
-
output.push(this.deparse(node.expr, context));
|
|
3213
|
-
output.push('THEN');
|
|
3214
|
-
output.push(this.deparse(node.result, context));
|
|
3215
|
-
return output.join(' ');
|
|
3216
|
-
}
|
|
3217
|
-
WindowDef(node, context = {}) {
|
|
3218
|
-
const output = [];
|
|
3219
|
-
if (context !== 'window') {
|
|
3220
|
-
if (node.name) {
|
|
3221
|
-
output.push(node.name);
|
|
3222
|
-
}
|
|
3223
|
-
}
|
|
3224
|
-
const empty = !(node.partitionClause != null) && !(node.orderClause != null);
|
|
3225
|
-
const frameOptions = this.deparseFrameOptions(node.frameOptions, node.refname, node.startOffset, node.endOffset);
|
|
3226
|
-
if (empty &&
|
|
3227
|
-
context !== 'window' &&
|
|
3228
|
-
!(node.name != null) &&
|
|
3229
|
-
frameOptions.length === 0) {
|
|
3230
|
-
return '()';
|
|
3231
|
-
}
|
|
3232
|
-
const windowParts = [];
|
|
3233
|
-
let useParens = false;
|
|
3234
|
-
if (node.partitionClause) {
|
|
3235
|
-
const partition = ['PARTITION BY'];
|
|
3236
|
-
const clause = unwrapList(node.partitionClause).map((item) => this.deparse(item, context));
|
|
3237
|
-
partition.push(clause.join(', '));
|
|
3238
|
-
windowParts.push(partition.join(' '));
|
|
3239
|
-
useParens = true;
|
|
3240
|
-
}
|
|
3241
|
-
if (node.orderClause) {
|
|
3242
|
-
windowParts.push('ORDER BY');
|
|
3243
|
-
const orders = unwrapList(node.orderClause).map((item) => {
|
|
3244
|
-
return this.deparse(item);
|
|
3245
|
-
});
|
|
3246
|
-
windowParts.push(orders.join(', '));
|
|
3247
|
-
useParens = true;
|
|
3248
|
-
}
|
|
3249
|
-
if (frameOptions.length) {
|
|
3250
|
-
useParens = true;
|
|
3251
|
-
windowParts.push(frameOptions);
|
|
3252
|
-
}
|
|
3253
|
-
if (useParens && context !== 'window') {
|
|
3254
|
-
return output.join(' ') + ' (' + windowParts.join(' ') + ')';
|
|
3255
|
-
}
|
|
3256
|
-
return output.join(' ') + windowParts.join(' ');
|
|
3257
|
-
}
|
|
3258
|
-
WithClause(node, context = {}) {
|
|
3259
|
-
const output = ['WITH'];
|
|
3260
|
-
if (node.recursive) {
|
|
3261
|
-
output.push('RECURSIVE');
|
|
3262
|
-
}
|
|
3263
|
-
output.push(this.list(node.ctes, ', ', '', context));
|
|
3264
|
-
return output.join(' ');
|
|
3265
|
-
}
|
|
3266
|
-
CopyStmt(node, context = {}) {
|
|
3267
|
-
const output = ['COPY'];
|
|
3268
|
-
output.push('(' + this.deparse(node.query, context) + ')');
|
|
3269
|
-
output.push('TO');
|
|
3270
|
-
output.push(`'${node.filename}'`);
|
|
3271
|
-
const options = unwrapList(node.options);
|
|
3272
|
-
if (options?.length > 0 && options[0].DefElem.defname === 'format') {
|
|
3273
|
-
output.push(`(FORMAT '${this.deparse(options[0].DefElem.arg)}')`);
|
|
3274
|
-
}
|
|
3275
|
-
return output.join(' ');
|
|
3276
|
-
}
|
|
3277
|
-
CallStmt(node, context = {}) {
|
|
3278
|
-
const output = ['CALL'];
|
|
3279
|
-
output.push(this.deparse(unwrapList(node.funccall.funcname)[0]));
|
|
3280
|
-
const funccallArgs = unwrapList(node.funccall.args);
|
|
3281
|
-
if (funccallArgs && funccallArgs.length) {
|
|
3282
|
-
// we have arguments
|
|
3283
|
-
output.push('(' + this.list(funccallArgs, ', ', '', context) + ')');
|
|
3284
|
-
}
|
|
3285
|
-
else {
|
|
3286
|
-
// just close parens
|
|
3287
|
-
output.push('()');
|
|
3288
|
-
}
|
|
3289
|
-
return output.join(' ');
|
|
3290
|
-
}
|
|
3291
|
-
deparseFrameOptions(options, refName, startOffset, endOffset) {
|
|
3292
|
-
// https://github.com/pganalyze/libpg_query/blob/442b1748d06364ecd3779bc558899176c02efaf0/src/postgres/include/nodes/parsenodes.h#L505-L522
|
|
3293
|
-
const FRAMEOPTION_NONDEFAULT = 0x00001; /* any specified? */
|
|
3294
|
-
const FRAMEOPTION_RANGE = 0x00002; /* RANGE behavior */
|
|
3295
|
-
const FRAMEOPTION_ROWS = 0x00004; /* ROWS behavior */
|
|
3296
|
-
const FRAMEOPTION_GROUPS = 0x00008; /* GROUPS behavior */
|
|
3297
|
-
const FRAMEOPTION_BETWEEN = 0x00010; /* BETWEEN given? */
|
|
3298
|
-
const FRAMEOPTION_START_UNBOUNDED_PRECEDING = 0x00020; /* start is U. P. */
|
|
3299
|
-
const FRAMEOPTION_END_UNBOUNDED_PRECEDING = 0x00040; /* (disallowed) */
|
|
3300
|
-
const FRAMEOPTION_START_UNBOUNDED_FOLLOWING = 0x00080; /* (disallowed) */
|
|
3301
|
-
const FRAMEOPTION_END_UNBOUNDED_FOLLOWING = 0x00100; /* end is U. F. */
|
|
3302
|
-
const FRAMEOPTION_START_CURRENT_ROW = 0x00200; /* start is C. R. */
|
|
3303
|
-
const FRAMEOPTION_END_CURRENT_ROW = 0x00400; /* end is C. R. */
|
|
3304
|
-
const FRAMEOPTION_START_OFFSET_PRECEDING = 0x00800; /* start is O. P. */
|
|
3305
|
-
const FRAMEOPTION_END_OFFSET_PRECEDING = 0x01000; /* end is O. P. */
|
|
3306
|
-
const FRAMEOPTION_START_OFFSET_FOLLOWING = 0x02000; /* start is O. F. */
|
|
3307
|
-
const FRAMEOPTION_END_OFFSET_FOLLOWING = 0x04000; /* end is O. F. */
|
|
3308
|
-
const FRAMEOPTION_EXCLUDE_CURRENT_ROW = 0x08000; /* omit C.R. */
|
|
3309
|
-
const FRAMEOPTION_EXCLUDE_GROUP = 0x10000; /* omit C.R. & peers */
|
|
3310
|
-
const FRAMEOPTION_EXCLUDE_TIES = 0x20000; /* omit C.R.'s peers */
|
|
3311
|
-
// const FRAMEOPTION_START_OFFSET =
|
|
3312
|
-
// FRAMEOPTION_START_OFFSET_PRECEDING | FRAMEOPTION_START_OFFSET_FOLLOWING;
|
|
3313
|
-
// const FRAMEOPTION_END_OFFSET =
|
|
3314
|
-
// FRAMEOPTION_END_OFFSET_PRECEDING | FRAMEOPTION_END_OFFSET_FOLLOWING;
|
|
3315
|
-
// const FRAMEOPTION_EXCLUSION =
|
|
3316
|
-
// FRAMEOPTION_EXCLUDE_CURRENT_ROW |
|
|
3317
|
-
// FRAMEOPTION_EXCLUDE_GROUP |
|
|
3318
|
-
// FRAMEOPTION_EXCLUDE_TIES;
|
|
3319
|
-
// const FRAMEOPTION_DEFAULTS =
|
|
3320
|
-
// FRAMEOPTION_RANGE |
|
|
3321
|
-
// FRAMEOPTION_START_UNBOUNDED_PRECEDING |
|
|
3322
|
-
// FRAMEOPTION_END_CURRENT_ROW;
|
|
3323
|
-
if (!(options & FRAMEOPTION_NONDEFAULT)) {
|
|
3324
|
-
return '';
|
|
3325
|
-
}
|
|
3326
|
-
const output = [];
|
|
3327
|
-
if (refName != null) {
|
|
3328
|
-
output.push(refName);
|
|
3329
|
-
}
|
|
3330
|
-
if (options & FRAMEOPTION_RANGE) {
|
|
3331
|
-
output.push('RANGE');
|
|
3332
|
-
}
|
|
3333
|
-
if (options & FRAMEOPTION_ROWS) {
|
|
3334
|
-
output.push('ROWS');
|
|
3335
|
-
}
|
|
3336
|
-
const between = options & FRAMEOPTION_BETWEEN;
|
|
3337
|
-
if (between) {
|
|
3338
|
-
output.push('BETWEEN');
|
|
3339
|
-
}
|
|
3340
|
-
if (options & FRAMEOPTION_START_UNBOUNDED_PRECEDING) {
|
|
3341
|
-
output.push('UNBOUNDED PRECEDING');
|
|
3342
|
-
}
|
|
3343
|
-
if (options & FRAMEOPTION_START_UNBOUNDED_FOLLOWING) {
|
|
3344
|
-
output.push('UNBOUNDED FOLLOWING');
|
|
3345
|
-
}
|
|
3346
|
-
if (options & FRAMEOPTION_START_CURRENT_ROW) {
|
|
3347
|
-
output.push('CURRENT ROW');
|
|
3348
|
-
}
|
|
3349
|
-
if (options & FRAMEOPTION_START_OFFSET_PRECEDING) {
|
|
3350
|
-
output.push(this.deparse(startOffset) + ' PRECEDING');
|
|
3351
|
-
}
|
|
3352
|
-
if (options & FRAMEOPTION_START_OFFSET_FOLLOWING) {
|
|
3353
|
-
output.push(this.deparse(startOffset) + ' FOLLOWING');
|
|
3354
|
-
}
|
|
3355
|
-
if (between) {
|
|
3356
|
-
output.push('AND');
|
|
3357
|
-
if (options & FRAMEOPTION_END_UNBOUNDED_PRECEDING) {
|
|
3358
|
-
output.push('UNBOUNDED PRECEDING');
|
|
3359
|
-
}
|
|
3360
|
-
if (options & FRAMEOPTION_END_UNBOUNDED_FOLLOWING) {
|
|
3361
|
-
output.push('UNBOUNDED FOLLOWING');
|
|
3362
|
-
}
|
|
3363
|
-
if (options & FRAMEOPTION_END_CURRENT_ROW) {
|
|
3364
|
-
output.push('CURRENT ROW');
|
|
3365
|
-
}
|
|
3366
|
-
if (options & FRAMEOPTION_END_OFFSET_PRECEDING) {
|
|
3367
|
-
output.push(this.deparse(endOffset) + ' PRECEDING');
|
|
3368
|
-
}
|
|
3369
|
-
if (options & FRAMEOPTION_END_OFFSET_FOLLOWING) {
|
|
3370
|
-
output.push(this.deparse(endOffset) + ' FOLLOWING');
|
|
3371
|
-
}
|
|
3372
|
-
}
|
|
3373
|
-
return output.join(' ');
|
|
3374
|
-
}
|
|
3375
|
-
deparseInterval(node) {
|
|
3376
|
-
const type = ['interval'];
|
|
3377
|
-
if (node.arrayBounds != null) {
|
|
3378
|
-
type.push('[]');
|
|
3379
|
-
}
|
|
3380
|
-
if (node.typmods) {
|
|
3381
|
-
const nodeTypmods = unwrapList(node.typmods);
|
|
3382
|
-
const typmods = nodeTypmods.map((item) => this.deparse(item));
|
|
3383
|
-
let intervals = this.interval(typmods[0]);
|
|
3384
|
-
// SELECT interval(0) '1 day 01:23:45.6789'
|
|
3385
|
-
if (nodeTypmods[0] &&
|
|
3386
|
-
nodeTypmods[0].A_Const &&
|
|
3387
|
-
nodeTypmods[0].A_Const.val.Integer.ival === 32767 &&
|
|
3388
|
-
nodeTypmods[1] &&
|
|
3389
|
-
nodeTypmods[1].A_Const != null) {
|
|
3390
|
-
intervals = [`(${nodeTypmods[1].A_Const.val.Integer.ival})`];
|
|
3391
|
-
}
|
|
3392
|
-
else {
|
|
3393
|
-
intervals = unwrapList(intervals).map((part) => {
|
|
3394
|
-
if (part === 'second' && typmods.length === 2) {
|
|
3395
|
-
return 'second(' + typmods[typmods.length - 1] + ')';
|
|
3396
|
-
}
|
|
3397
|
-
return part;
|
|
3398
|
-
});
|
|
3399
|
-
}
|
|
3400
|
-
type.push(intervals.join(' to '));
|
|
3401
|
-
}
|
|
3402
|
-
return type.join(' ');
|
|
3403
|
-
}
|
|
3404
|
-
interval(mask) {
|
|
3405
|
-
// ported from https://github.com/lfittl/pg_query/blob/master/lib/pg_query/deparse/interval.rb
|
|
3406
|
-
if (this.MASKS == null) {
|
|
3407
|
-
this.MASKS = {
|
|
3408
|
-
0: 'RESERV',
|
|
3409
|
-
1: 'MONTH',
|
|
3410
|
-
2: 'YEAR',
|
|
3411
|
-
3: 'DAY',
|
|
3412
|
-
4: 'JULIAN',
|
|
3413
|
-
5: 'TZ',
|
|
3414
|
-
6: 'DTZ',
|
|
3415
|
-
7: 'DYNTZ',
|
|
3416
|
-
8: 'IGNORE_DTF',
|
|
3417
|
-
9: 'AMPM',
|
|
3418
|
-
10: 'HOUR',
|
|
3419
|
-
11: 'MINUTE',
|
|
3420
|
-
12: 'SECOND',
|
|
3421
|
-
13: 'MILLISECOND',
|
|
3422
|
-
14: 'MICROSECOND',
|
|
3423
|
-
15: 'DOY',
|
|
3424
|
-
16: 'DOW',
|
|
3425
|
-
17: 'UNITS',
|
|
3426
|
-
18: 'ADBC',
|
|
3427
|
-
19: 'AGO',
|
|
3428
|
-
20: 'ABS_BEFORE',
|
|
3429
|
-
21: 'ABS_AFTER',
|
|
3430
|
-
22: 'ISODATE',
|
|
3431
|
-
23: 'ISOTIME',
|
|
3432
|
-
24: 'WEEK',
|
|
3433
|
-
25: 'DECADE',
|
|
3434
|
-
26: 'CENTURY',
|
|
3435
|
-
27: 'MILLENNIUM',
|
|
3436
|
-
28: 'DTZMOD'
|
|
3437
|
-
};
|
|
3438
|
-
}
|
|
3439
|
-
if (this.BITS == null) {
|
|
3440
|
-
this.BITS = inverted(this.MASKS);
|
|
3441
|
-
}
|
|
3442
|
-
if (this.INTERVALS == null) {
|
|
3443
|
-
this.INTERVALS = {};
|
|
3444
|
-
this.INTERVALS[1 << this.BITS.YEAR] = ['year'];
|
|
3445
|
-
this.INTERVALS[1 << this.BITS.MONTH] = ['month'];
|
|
3446
|
-
this.INTERVALS[1 << this.BITS.DAY] = ['day'];
|
|
3447
|
-
this.INTERVALS[1 << this.BITS.HOUR] = ['hour'];
|
|
3448
|
-
this.INTERVALS[1 << this.BITS.MINUTE] = ['minute'];
|
|
3449
|
-
this.INTERVALS[1 << this.BITS.SECOND] = ['second'];
|
|
3450
|
-
this.INTERVALS[(1 << this.BITS.YEAR) | (1 << this.BITS.MONTH)] = [
|
|
3451
|
-
'year',
|
|
3452
|
-
'month'
|
|
3453
|
-
];
|
|
3454
|
-
this.INTERVALS[(1 << this.BITS.DAY) | (1 << this.BITS.HOUR)] = [
|
|
3455
|
-
'day',
|
|
3456
|
-
'hour'
|
|
3457
|
-
];
|
|
3458
|
-
this.INTERVALS[(1 << this.BITS.DAY) | (1 << this.BITS.HOUR) | (1 << this.BITS.MINUTE)] = ['day', 'minute'];
|
|
3459
|
-
this.INTERVALS[(1 << this.BITS.DAY) |
|
|
3460
|
-
(1 << this.BITS.HOUR) |
|
|
3461
|
-
(1 << this.BITS.MINUTE) |
|
|
3462
|
-
(1 << this.BITS.SECOND)] = ['day', 'second'];
|
|
3463
|
-
this.INTERVALS[(1 << this.BITS.HOUR) | (1 << this.BITS.MINUTE)] = [
|
|
3464
|
-
'hour',
|
|
3465
|
-
'minute'
|
|
3466
|
-
];
|
|
3467
|
-
this.INTERVALS[(1 << this.BITS.HOUR) |
|
|
3468
|
-
(1 << this.BITS.MINUTE) |
|
|
3469
|
-
(1 << this.BITS.SECOND)] = ['hour', 'second'];
|
|
3470
|
-
this.INTERVALS[(1 << this.BITS.MINUTE) | (1 << this.BITS.SECOND)] = [
|
|
3471
|
-
'minute',
|
|
3472
|
-
'second'
|
|
3473
|
-
];
|
|
3474
|
-
// utils/timestamp.h
|
|
3475
|
-
// #define INTERVAL_FULL_RANGE (0x7FFF)
|
|
3476
|
-
this.INTERVALS[(this.INTERVAL_FULL_RANGE = '32767')] = [];
|
|
3477
|
-
}
|
|
3478
|
-
return this.INTERVALS[mask.toString()];
|
|
3479
|
-
}
|
|
3480
|
-
}
|
|
3481
|
-
exports.default = Deparser;
|