pgsql-deparser 16.1.1 → 16.2.1
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 +12 -7
- package/deparser/deparser.d.ts +31 -28
- package/deparser/deparser.js +1253 -814
- package/deparser/index.d.ts +3 -1
- package/deparser/index.js +5 -1
- package/deparser/utils/sql-formatter.d.ts +8 -0
- package/deparser/utils/sql-formatter.js +104 -1
- package/esm/deparser/deparser.js +1236 -797
- package/esm/deparser/index.js +3 -1
- package/esm/deparser/utils/sql-formatter.js +103 -1
- package/esm/index.js +40 -7
- package/esm/v16-to-v17.js +50 -1
- package/esm/v16-to-v18-direct.js +55 -0
- package/esm/v17-to-v18.js +209 -0
- package/index.d.ts +12 -4
- package/index.js +43 -6
- package/package.json +3 -2
- package/v16-to-v17.d.ts +3 -0
- package/v16-to-v17.js +50 -1
- package/v16-to-v18-direct.d.ts +22 -0
- package/v16-to-v18-direct.js +59 -0
- package/v17-to-v18.d.ts +55 -0
- package/v17-to-v18.js +213 -0
package/esm/deparser/index.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Auto-generated file with types stripped for better tree-shaking
|
|
3
3
|
* DO NOT EDIT - Generated by strip-deparser-types.ts
|
|
4
4
|
*/
|
|
5
|
-
import { Deparser } from
|
|
5
|
+
import { Deparser } from './deparser';
|
|
6
6
|
const deparseMethod = Deparser.deparse;
|
|
7
7
|
// Export the original sync version as deparseSync
|
|
8
8
|
export const deparseSync = deparseMethod;
|
|
@@ -11,3 +11,5 @@ export const deparse = async (...args) => {
|
|
|
11
11
|
return deparseMethod(...args);
|
|
12
12
|
};
|
|
13
13
|
export { Deparser };
|
|
14
|
+
export { splitSqlLines } from './utils/sql-formatter';
|
|
15
|
+
export { QuoteUtils } from '@pgsql/quotes';
|
|
@@ -2,6 +2,108 @@
|
|
|
2
2
|
* Auto-generated file with types stripped for better tree-shaking
|
|
3
3
|
* DO NOT EDIT - Generated by strip-deparser-types.ts
|
|
4
4
|
*/
|
|
5
|
+
const isIdentChar = (ch) => ch !== undefined && (/[A-Za-z0-9_$]/.test(ch) || ch.charCodeAt(0) >= 0x80);
|
|
6
|
+
const DOLLAR_TAG = /\$(?:[A-Za-z_\u0080-\uffff][A-Za-z0-9_\u0080-\uffff]*)?\$/y;
|
|
7
|
+
/**
|
|
8
|
+
* Splits SQL text into lines, breaking only at newlines that sit between
|
|
9
|
+
* tokens. A newline inside a string literal ('...', E'...', $tag$...$tag$),
|
|
10
|
+
* a quoted identifier or a block comment is part of that token's value and
|
|
11
|
+
* stays inside its line, so callers can re-indent the lines without changing
|
|
12
|
+
* what the SQL means.
|
|
13
|
+
*/
|
|
14
|
+
export function splitSqlLines(text, newlineChar = '\n') {
|
|
15
|
+
if (!newlineChar || !text.includes(newlineChar)) {
|
|
16
|
+
return [text];
|
|
17
|
+
}
|
|
18
|
+
const lines = [];
|
|
19
|
+
const length = text.length;
|
|
20
|
+
let lineStart = 0;
|
|
21
|
+
let i = 0;
|
|
22
|
+
while (i < length) {
|
|
23
|
+
const ch = text[i];
|
|
24
|
+
if (ch === "'") {
|
|
25
|
+
const prev = text[i - 1];
|
|
26
|
+
const backslashEscapes = (prev === 'E' || prev === 'e') && !isIdentChar(text[i - 2]);
|
|
27
|
+
i++;
|
|
28
|
+
while (i < length) {
|
|
29
|
+
if (backslashEscapes && text[i] === '\\') {
|
|
30
|
+
i += 2;
|
|
31
|
+
}
|
|
32
|
+
else if (text[i] === "'") {
|
|
33
|
+
if (text[i + 1] === "'") {
|
|
34
|
+
i += 2;
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
break;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
i++;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
i++;
|
|
45
|
+
}
|
|
46
|
+
else if (ch === '"') {
|
|
47
|
+
i++;
|
|
48
|
+
while (i < length) {
|
|
49
|
+
if (text[i] === '"') {
|
|
50
|
+
if (text[i + 1] === '"') {
|
|
51
|
+
i += 2;
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
i++;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
i++;
|
|
62
|
+
}
|
|
63
|
+
else if (ch === '$' && !isIdentChar(text[i - 1])) {
|
|
64
|
+
DOLLAR_TAG.lastIndex = i;
|
|
65
|
+
const tag = DOLLAR_TAG.exec(text);
|
|
66
|
+
if (tag) {
|
|
67
|
+
const close = text.indexOf(tag[0], i + tag[0].length);
|
|
68
|
+
i = close === -1 ? length : close + tag[0].length;
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
i++;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
else if (ch === '-' && text[i + 1] === '-') {
|
|
75
|
+
const end = text.indexOf('\n', i);
|
|
76
|
+
i = end === -1 ? length : end;
|
|
77
|
+
}
|
|
78
|
+
else if (ch === '/' && text[i + 1] === '*') {
|
|
79
|
+
let depth = 1;
|
|
80
|
+
i += 2;
|
|
81
|
+
while (i < length && depth > 0) {
|
|
82
|
+
if (text[i] === '/' && text[i + 1] === '*') {
|
|
83
|
+
depth++;
|
|
84
|
+
i += 2;
|
|
85
|
+
}
|
|
86
|
+
else if (text[i] === '*' && text[i + 1] === '/') {
|
|
87
|
+
depth--;
|
|
88
|
+
i += 2;
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
i++;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
else if (text.startsWith(newlineChar, i)) {
|
|
96
|
+
lines.push(text.slice(lineStart, i));
|
|
97
|
+
i += newlineChar.length;
|
|
98
|
+
lineStart = i;
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
i++;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
lines.push(text.slice(lineStart));
|
|
105
|
+
return lines;
|
|
106
|
+
}
|
|
5
107
|
export class SqlFormatter {
|
|
6
108
|
newlineChar;
|
|
7
109
|
tabChar;
|
|
@@ -19,7 +121,7 @@ export class SqlFormatter {
|
|
|
19
121
|
return text;
|
|
20
122
|
}
|
|
21
123
|
const indentation = this.tabChar.repeat(count);
|
|
22
|
-
return text
|
|
124
|
+
return splitSqlLines(text, this.newlineChar).map(line => line.trim() ? indentation + line : line).join(this.newlineChar);
|
|
23
125
|
}
|
|
24
126
|
parens(content) {
|
|
25
127
|
return `(${content})`;
|
package/esm/index.js
CHANGED
|
@@ -2,14 +2,47 @@
|
|
|
2
2
|
* Deparser for PostgreSQL version 16
|
|
3
3
|
* Auto-generated by generate-version-deparsers.ts
|
|
4
4
|
*/
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
const tx = new
|
|
5
|
+
import { Deparser as Deparser18 } from './deparser';
|
|
6
|
+
import { PG16ToPG18Transformer } from './v16-to-v18-direct';
|
|
7
|
+
const tx = new PG16ToPG18Transformer();
|
|
8
|
+
const bare = (names, name) => Array.isArray(names) && names.length === 1 && (names[0]?.String?.sval ?? names[0]?.String?.str) === name;
|
|
9
|
+
const v16to17 = tx.v16to17;
|
|
10
|
+
const upgradeTypeName = v16to17.TypeName.bind(v16to17);
|
|
11
|
+
v16to17.TypeName = (node, context) => {
|
|
12
|
+
const out = upgradeTypeName(node, context);
|
|
13
|
+
if (bare(node.names, 'json'))
|
|
14
|
+
out.TypeName.names = [{ String: { sval: 'json' } }];
|
|
15
|
+
return out;
|
|
16
|
+
};
|
|
17
|
+
const upgradeTypeCast = v16to17.TypeCast.bind(v16to17);
|
|
18
|
+
v16to17.TypeCast = (node, context) => {
|
|
19
|
+
const out = upgradeTypeCast(node, context);
|
|
20
|
+
const typeName = out.TypeCast.typeName?.TypeName ?? out.TypeCast.typeName;
|
|
21
|
+
if (typeName && bare((node.typeName?.TypeName ?? node.typeName)?.names, 'json')) {
|
|
22
|
+
typeName.names = [{ String: { sval: 'json' } }];
|
|
23
|
+
}
|
|
24
|
+
return out;
|
|
25
|
+
};
|
|
26
|
+
const toAst18 = (query) => Array.isArray(query) ? query.map(node => tx.transform(node)) : tx.transform(query);
|
|
27
|
+
export class Deparser extends Deparser18 {
|
|
28
|
+
constructor(tree, opts = {}) {
|
|
29
|
+
super(toAst18(tree), opts);
|
|
30
|
+
}
|
|
31
|
+
static deparse(query, opts = {}) {
|
|
32
|
+
return new Deparser(query, opts).deparseQuery();
|
|
33
|
+
}
|
|
34
|
+
DefElem(node, context) {
|
|
35
|
+
if (!node.arg && node.defaction !== 'DEFELEM_DROP' && context.parentNodeTypes.includes('AlterOperatorStmt')) {
|
|
36
|
+
return `${node.defname} = NONE`;
|
|
37
|
+
}
|
|
38
|
+
return super.DefElem(node, context);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
8
41
|
export async function deparse(query, opts) {
|
|
9
|
-
|
|
10
|
-
return await deparse17(ast17, opts);
|
|
42
|
+
return Deparser.deparse(query, opts);
|
|
11
43
|
}
|
|
12
44
|
export function deparseSync(query, opts) {
|
|
13
|
-
|
|
14
|
-
return deparseSync17(ast17, opts);
|
|
45
|
+
return Deparser.deparse(query, opts);
|
|
15
46
|
}
|
|
47
|
+
export { splitSqlLines } from './deparser/utils/sql-formatter';
|
|
48
|
+
export { QuoteUtils } from '@pgsql/quotes';
|
package/esm/v16-to-v17.js
CHANGED
|
@@ -652,6 +652,12 @@ export class V16ToV17Transformer {
|
|
|
652
652
|
if (node.storage !== undefined) {
|
|
653
653
|
result.storage = node.storage;
|
|
654
654
|
}
|
|
655
|
+
if (node.storage_name !== undefined) {
|
|
656
|
+
result.storage_name = node.storage_name;
|
|
657
|
+
}
|
|
658
|
+
if (node.compression !== undefined) {
|
|
659
|
+
result.compression = node.compression;
|
|
660
|
+
}
|
|
655
661
|
if (node.raw_default !== undefined) {
|
|
656
662
|
result.raw_default = this.transform(node.raw_default, context);
|
|
657
663
|
}
|
|
@@ -1110,7 +1116,50 @@ export class V16ToV17Transformer {
|
|
|
1110
1116
|
return { AlterOpFamilyStmt: node };
|
|
1111
1117
|
}
|
|
1112
1118
|
MergeStmt(node, context) {
|
|
1113
|
-
|
|
1119
|
+
const result = {};
|
|
1120
|
+
if (node.relation !== undefined) {
|
|
1121
|
+
result.relation = node.relation;
|
|
1122
|
+
}
|
|
1123
|
+
if (node.sourceRelation !== undefined) {
|
|
1124
|
+
result.sourceRelation = this.transform(node.sourceRelation, context);
|
|
1125
|
+
}
|
|
1126
|
+
if (node.joinCondition !== undefined) {
|
|
1127
|
+
result.joinCondition = this.transform(node.joinCondition, context);
|
|
1128
|
+
}
|
|
1129
|
+
if (node.mergeWhenClauses !== undefined) {
|
|
1130
|
+
result.mergeWhenClauses = Array.isArray(node.mergeWhenClauses)
|
|
1131
|
+
? node.mergeWhenClauses.map(item => this.transform(item, context))
|
|
1132
|
+
: this.transform(node.mergeWhenClauses, context);
|
|
1133
|
+
}
|
|
1134
|
+
if (node.withClause !== undefined) {
|
|
1135
|
+
result.withClause = this.WithClause(node.withClause, context);
|
|
1136
|
+
}
|
|
1137
|
+
return { MergeStmt: result };
|
|
1138
|
+
}
|
|
1139
|
+
MergeWhenClause(node, context) {
|
|
1140
|
+
const result = {};
|
|
1141
|
+
// v17 replaces the boolean matched with matchKind
|
|
1142
|
+
result.matchKind = node.matched ? 'MERGE_WHEN_MATCHED' : 'MERGE_WHEN_NOT_MATCHED_BY_TARGET';
|
|
1143
|
+
if (node.commandType !== undefined) {
|
|
1144
|
+
result.commandType = node.commandType;
|
|
1145
|
+
}
|
|
1146
|
+
if (node.override !== undefined) {
|
|
1147
|
+
result.override = node.override;
|
|
1148
|
+
}
|
|
1149
|
+
if (node.condition !== undefined) {
|
|
1150
|
+
result.condition = this.transform(node.condition, context);
|
|
1151
|
+
}
|
|
1152
|
+
if (node.targetList !== undefined) {
|
|
1153
|
+
result.targetList = Array.isArray(node.targetList)
|
|
1154
|
+
? node.targetList.map(item => this.transform(item, context))
|
|
1155
|
+
: this.transform(node.targetList, context);
|
|
1156
|
+
}
|
|
1157
|
+
if (node.values !== undefined) {
|
|
1158
|
+
result.values = Array.isArray(node.values)
|
|
1159
|
+
? node.values.map(item => this.transform(item, context))
|
|
1160
|
+
: this.transform(node.values, context);
|
|
1161
|
+
}
|
|
1162
|
+
return { MergeWhenClause: result };
|
|
1114
1163
|
}
|
|
1115
1164
|
AlterTableMoveAllStmt(node, context) {
|
|
1116
1165
|
return { AlterTableMoveAllStmt: node };
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Auto-generated file with types stripped for better tree-shaking
|
|
3
|
+
* DO NOT EDIT - Generated by strip-direct-transformer-types.ts
|
|
4
|
+
*/
|
|
5
|
+
import { V16ToV17Transformer } from "./v16-to-v17";
|
|
6
|
+
import { V17ToV18Transformer } from "./v17-to-v18";
|
|
7
|
+
/**
|
|
8
|
+
* Direct transformer from PG16 to PG18
|
|
9
|
+
* This transformer chains v16->...->v18 transformations
|
|
10
|
+
*/
|
|
11
|
+
export class PG16ToPG18Transformer {
|
|
12
|
+
v16to17 = new V16ToV17Transformer();
|
|
13
|
+
v17to18 = new V17ToV18Transformer();
|
|
14
|
+
transform(node) {
|
|
15
|
+
if (this.isParseResult(node)) {
|
|
16
|
+
// Transform through the chain: v16->v17->v18
|
|
17
|
+
const v17Stmts = node.stmts.map((stmtWrapper) => {
|
|
18
|
+
if (stmtWrapper.stmt) {
|
|
19
|
+
const stmt = this.v16to17.transform(stmtWrapper.stmt, { parentNodeTypes: [] });
|
|
20
|
+
return { ...stmtWrapper, stmt };
|
|
21
|
+
}
|
|
22
|
+
return stmtWrapper;
|
|
23
|
+
});
|
|
24
|
+
const v18Stmts = v17Stmts.map((stmtWrapper) => {
|
|
25
|
+
if (stmtWrapper.stmt) {
|
|
26
|
+
const stmt = this.v17to18.transform(stmtWrapper.stmt, { parentNodeTypes: [] });
|
|
27
|
+
return { ...stmtWrapper, stmt };
|
|
28
|
+
}
|
|
29
|
+
return stmtWrapper;
|
|
30
|
+
});
|
|
31
|
+
return {
|
|
32
|
+
...node,
|
|
33
|
+
version: 180004, // PG18 version
|
|
34
|
+
stmts: v18Stmts
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
// Otherwise, transform as a regular node through the chain
|
|
38
|
+
const v17Node = this.v16to17.transform(node, { parentNodeTypes: [] });
|
|
39
|
+
return this.v17to18.transform(v17Node, { parentNodeTypes: [] });
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Transform a single statement from PG16 to PG18
|
|
43
|
+
* @deprecated Use transform() instead, which handles all node types
|
|
44
|
+
*/
|
|
45
|
+
transformStatement(stmt) {
|
|
46
|
+
const v17Stmt = this.v16to17.transform(stmt, { parentNodeTypes: [] });
|
|
47
|
+
return this.v17to18.transform(v17Stmt, { parentNodeTypes: [] });
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Type guard to check if a node is a ParseResult
|
|
51
|
+
*/
|
|
52
|
+
isParseResult(node) {
|
|
53
|
+
return node && typeof node === 'object' && 'version' in node && 'stmts' in node;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Auto-generated file with types stripped for better tree-shaking
|
|
3
|
+
* DO NOT EDIT - Generated by strip-transformer-types.ts
|
|
4
|
+
*/
|
|
5
|
+
// @ts-nocheck
|
|
6
|
+
/**
|
|
7
|
+
* V17 to V18 AST Transformer
|
|
8
|
+
* Transforms PostgreSQL v17 AST nodes to v18 format
|
|
9
|
+
*
|
|
10
|
+
* Structural changes in v18:
|
|
11
|
+
* - InsertStmt/UpdateStmt/DeleteStmt/MergeStmt: returningList -> returningClause { exprs }
|
|
12
|
+
* - Constraint: new is_enforced flag (true by default for CHECK/FOREIGN/NOTNULL)
|
|
13
|
+
* - Constraint: CONSTR_GENERATED gains generated_kind ('s' = STORED, 'v' = VIRTUAL)
|
|
14
|
+
* - Column-level NOT NULL constraints gain initially_valid: true
|
|
15
|
+
*/
|
|
16
|
+
export class V17ToV18Transformer {
|
|
17
|
+
transform(node, context = { parentNodeTypes: [] }) {
|
|
18
|
+
if (node == null) {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
if (typeof node === 'number' || node instanceof Number) {
|
|
22
|
+
return node;
|
|
23
|
+
}
|
|
24
|
+
if (typeof node === 'string') {
|
|
25
|
+
return node;
|
|
26
|
+
}
|
|
27
|
+
try {
|
|
28
|
+
return this.visit(node, context);
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
const nodeType = Object.keys(node)[0];
|
|
32
|
+
throw new Error(`Error transforming ${nodeType}: ${error.message}`);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
visit(node, context = { parentNodeTypes: [] }) {
|
|
36
|
+
if (node && typeof node === 'object' && 'version' in node && 'stmts' in node) {
|
|
37
|
+
return this.ParseResult(node, context);
|
|
38
|
+
}
|
|
39
|
+
const nodeType = this.getNodeType(node);
|
|
40
|
+
// Handle empty objects
|
|
41
|
+
if (!nodeType) {
|
|
42
|
+
return {};
|
|
43
|
+
}
|
|
44
|
+
const nodeData = this.getNodeData(node);
|
|
45
|
+
const methodName = nodeType;
|
|
46
|
+
if (typeof this[methodName] === 'function') {
|
|
47
|
+
const childContext = {
|
|
48
|
+
...context,
|
|
49
|
+
parentNodeTypes: [...context.parentNodeTypes, nodeType]
|
|
50
|
+
};
|
|
51
|
+
const result = this[methodName](nodeData, childContext);
|
|
52
|
+
return result;
|
|
53
|
+
}
|
|
54
|
+
// No specific method: recurse into children so nested statements
|
|
55
|
+
// (e.g. DML inside CTEs) still get transformed
|
|
56
|
+
return this.walkValue(node, context);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Generic recursive walk that re-dispatches wrapped nodes with a
|
|
60
|
+
* dedicated transformer method and deep-copies everything else.
|
|
61
|
+
*/
|
|
62
|
+
walkValue(value, context) {
|
|
63
|
+
if (value == null || typeof value !== 'object') {
|
|
64
|
+
return value;
|
|
65
|
+
}
|
|
66
|
+
if (Array.isArray(value)) {
|
|
67
|
+
return value.map(item => this.walkValue(item, context));
|
|
68
|
+
}
|
|
69
|
+
const keys = Object.keys(value);
|
|
70
|
+
if (keys.length === 1 && typeof this[keys[0]] === 'function') {
|
|
71
|
+
// Wrapped node with a dedicated transformer method
|
|
72
|
+
return this.visit(value, context);
|
|
73
|
+
}
|
|
74
|
+
const result = {};
|
|
75
|
+
for (const key of keys) {
|
|
76
|
+
result[key] = this.walkValue(value[key], context);
|
|
77
|
+
}
|
|
78
|
+
return result;
|
|
79
|
+
}
|
|
80
|
+
getNodeType(node) {
|
|
81
|
+
return Object.keys(node)[0];
|
|
82
|
+
}
|
|
83
|
+
getNodeData(node) {
|
|
84
|
+
const keys = Object.keys(node);
|
|
85
|
+
if (keys.length === 1 && typeof node[keys[0]] === 'object') {
|
|
86
|
+
return node[keys[0]];
|
|
87
|
+
}
|
|
88
|
+
return node;
|
|
89
|
+
}
|
|
90
|
+
ParseResult(node, context) {
|
|
91
|
+
if (node && typeof node === 'object' && 'version' in node && 'stmts' in node) {
|
|
92
|
+
return {
|
|
93
|
+
version: 180004, // PG18 version
|
|
94
|
+
stmts: node.stmts.map((stmt) => {
|
|
95
|
+
if (stmt && typeof stmt === 'object' && 'stmt' in stmt) {
|
|
96
|
+
return {
|
|
97
|
+
...stmt,
|
|
98
|
+
stmt: this.transform(stmt.stmt, context)
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
return this.transform(stmt, context);
|
|
102
|
+
})
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
return node;
|
|
106
|
+
}
|
|
107
|
+
RawStmt(node, context) {
|
|
108
|
+
const result = {};
|
|
109
|
+
if (node.stmt !== undefined) {
|
|
110
|
+
result.stmt = this.transform(node.stmt, context);
|
|
111
|
+
}
|
|
112
|
+
if (node.stmt_location !== undefined) {
|
|
113
|
+
result.stmt_location = node.stmt_location;
|
|
114
|
+
}
|
|
115
|
+
if (node.stmt_len !== undefined) {
|
|
116
|
+
result.stmt_len = node.stmt_len;
|
|
117
|
+
}
|
|
118
|
+
return { RawStmt: result };
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Convert a v17 returningList into a v18 ReturningClause
|
|
122
|
+
*/
|
|
123
|
+
transformReturningList(returningList, context) {
|
|
124
|
+
return {
|
|
125
|
+
exprs: returningList.map(item => this.transform(item, context))
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
transformDmlStatement(nodeType, node, context) {
|
|
129
|
+
const result = {};
|
|
130
|
+
for (const key of Object.keys(node)) {
|
|
131
|
+
if (key === 'returningList') {
|
|
132
|
+
result.returningClause = this.transformReturningList(node.returningList, context);
|
|
133
|
+
}
|
|
134
|
+
else {
|
|
135
|
+
result[key] = this.walkValue(node[key], context);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return { [nodeType]: result };
|
|
139
|
+
}
|
|
140
|
+
InsertStmt(node, context) {
|
|
141
|
+
return this.transformDmlStatement('InsertStmt', node, context);
|
|
142
|
+
}
|
|
143
|
+
UpdateStmt(node, context) {
|
|
144
|
+
return this.transformDmlStatement('UpdateStmt', node, context);
|
|
145
|
+
}
|
|
146
|
+
DeleteStmt(node, context) {
|
|
147
|
+
return this.transformDmlStatement('DeleteStmt', node, context);
|
|
148
|
+
}
|
|
149
|
+
MergeStmt(node, context) {
|
|
150
|
+
return this.transformDmlStatement('MergeStmt', node, context);
|
|
151
|
+
}
|
|
152
|
+
AlterTableCmd(node, context) {
|
|
153
|
+
const result = {};
|
|
154
|
+
for (const key of Object.keys(node)) {
|
|
155
|
+
result[key] = this.walkValue(node[key], context);
|
|
156
|
+
}
|
|
157
|
+
// v18 wraps ALTER CONSTRAINT definitions in the new ATAlterConstraint node
|
|
158
|
+
if (node.subtype === 'AT_AlterConstraint' && node.def?.Constraint) {
|
|
159
|
+
const constraint = node.def.Constraint;
|
|
160
|
+
const alterConstraint = {};
|
|
161
|
+
if (constraint.conname !== undefined) {
|
|
162
|
+
alterConstraint.conname = constraint.conname;
|
|
163
|
+
}
|
|
164
|
+
alterConstraint.is_enforced = true;
|
|
165
|
+
// v17 ALTER CONSTRAINT could only change deferrability
|
|
166
|
+
alterConstraint.alterDeferrability = true;
|
|
167
|
+
if (constraint.deferrable !== undefined) {
|
|
168
|
+
alterConstraint.deferrable = constraint.deferrable;
|
|
169
|
+
}
|
|
170
|
+
if (constraint.initdeferred !== undefined) {
|
|
171
|
+
alterConstraint.initdeferred = constraint.initdeferred;
|
|
172
|
+
}
|
|
173
|
+
result.def = { ATAlterConstraint: alterConstraint };
|
|
174
|
+
}
|
|
175
|
+
return { AlterTableCmd: result };
|
|
176
|
+
}
|
|
177
|
+
VariableSetStmt(node, context) {
|
|
178
|
+
const result = {};
|
|
179
|
+
for (const key of Object.keys(node)) {
|
|
180
|
+
result[key] = this.walkValue(node[key], context);
|
|
181
|
+
}
|
|
182
|
+
// v18 marks SET TRANSACTION / SESSION CHARACTERISTICS args as jumbled
|
|
183
|
+
if (node.kind === 'VAR_SET_MULTI' && (node.name === 'TRANSACTION' || node.name === 'SESSION CHARACTERISTICS')) {
|
|
184
|
+
result.jumble_args = true;
|
|
185
|
+
}
|
|
186
|
+
return { VariableSetStmt: result };
|
|
187
|
+
}
|
|
188
|
+
Constraint(node, context) {
|
|
189
|
+
const result = {};
|
|
190
|
+
for (const key of Object.keys(node)) {
|
|
191
|
+
result[key] = this.walkValue(node[key], context);
|
|
192
|
+
}
|
|
193
|
+
// v18 constraints are enforced by default; v17 grammar had no ENFORCED syntax
|
|
194
|
+
if (node.contype === 'CONSTR_CHECK' || node.contype === 'CONSTR_FOREIGN') {
|
|
195
|
+
result.is_enforced = true;
|
|
196
|
+
}
|
|
197
|
+
if (node.contype === 'CONSTR_NOTNULL') {
|
|
198
|
+
// v17 only produces column-level NOT NULL constraints, which in v18
|
|
199
|
+
// carry is_enforced and initially_valid
|
|
200
|
+
result.is_enforced = true;
|
|
201
|
+
result.initially_valid = true;
|
|
202
|
+
}
|
|
203
|
+
if (node.contype === 'CONSTR_GENERATED' && result.generated_kind === undefined) {
|
|
204
|
+
// v17 only supports STORED generated columns
|
|
205
|
+
result.generated_kind = 's';
|
|
206
|
+
}
|
|
207
|
+
return { Constraint: result };
|
|
208
|
+
}
|
|
209
|
+
}
|
package/index.d.ts
CHANGED
|
@@ -3,7 +3,15 @@
|
|
|
3
3
|
* Auto-generated by generate-version-deparsers.ts
|
|
4
4
|
*/
|
|
5
5
|
import { Node, ParseResult } from '@pgsql/types';
|
|
6
|
-
import { DeparserOptions } from './deparser';
|
|
7
|
-
|
|
8
|
-
export declare
|
|
9
|
-
|
|
6
|
+
import { Deparser as Deparser18, DeparserOptions } from './deparser';
|
|
7
|
+
type Query = Node | Node[] | ParseResult;
|
|
8
|
+
export declare class Deparser extends Deparser18 {
|
|
9
|
+
constructor(tree: Query, opts?: DeparserOptions);
|
|
10
|
+
static deparse(query: Query, opts?: DeparserOptions): string;
|
|
11
|
+
DefElem(node: any, context: any): string;
|
|
12
|
+
}
|
|
13
|
+
export declare function deparse(query: Query, opts?: DeparserOptions): Promise<string>;
|
|
14
|
+
export declare function deparseSync(query: Query, opts?: DeparserOptions): string;
|
|
15
|
+
export { DeparserOptions };
|
|
16
|
+
export { splitSqlLines } from './deparser/utils/sql-formatter';
|
|
17
|
+
export { QuoteUtils } from '@pgsql/quotes';
|
package/index.js
CHANGED
|
@@ -4,16 +4,53 @@
|
|
|
4
4
|
* Auto-generated by generate-version-deparsers.ts
|
|
5
5
|
*/
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.QuoteUtils = exports.splitSqlLines = exports.Deparser = void 0;
|
|
7
8
|
exports.deparse = deparse;
|
|
8
9
|
exports.deparseSync = deparseSync;
|
|
9
10
|
const deparser_1 = require("./deparser");
|
|
10
|
-
const
|
|
11
|
-
const tx = new
|
|
11
|
+
const v16_to_v18_direct_1 = require("./v16-to-v18-direct");
|
|
12
|
+
const tx = new v16_to_v18_direct_1.PG16ToPG18Transformer();
|
|
13
|
+
const bare = (names, name) => Array.isArray(names) && names.length === 1 && (names[0]?.String?.sval ?? names[0]?.String?.str) === name;
|
|
14
|
+
const v16to17 = tx.v16to17;
|
|
15
|
+
const upgradeTypeName = v16to17.TypeName.bind(v16to17);
|
|
16
|
+
v16to17.TypeName = (node, context) => {
|
|
17
|
+
const out = upgradeTypeName(node, context);
|
|
18
|
+
if (bare(node.names, 'json'))
|
|
19
|
+
out.TypeName.names = [{ String: { sval: 'json' } }];
|
|
20
|
+
return out;
|
|
21
|
+
};
|
|
22
|
+
const upgradeTypeCast = v16to17.TypeCast.bind(v16to17);
|
|
23
|
+
v16to17.TypeCast = (node, context) => {
|
|
24
|
+
const out = upgradeTypeCast(node, context);
|
|
25
|
+
const typeName = out.TypeCast.typeName?.TypeName ?? out.TypeCast.typeName;
|
|
26
|
+
if (typeName && bare((node.typeName?.TypeName ?? node.typeName)?.names, 'json')) {
|
|
27
|
+
typeName.names = [{ String: { sval: 'json' } }];
|
|
28
|
+
}
|
|
29
|
+
return out;
|
|
30
|
+
};
|
|
31
|
+
const toAst18 = (query) => Array.isArray(query) ? query.map(node => tx.transform(node)) : tx.transform(query);
|
|
32
|
+
class Deparser extends deparser_1.Deparser {
|
|
33
|
+
constructor(tree, opts = {}) {
|
|
34
|
+
super(toAst18(tree), opts);
|
|
35
|
+
}
|
|
36
|
+
static deparse(query, opts = {}) {
|
|
37
|
+
return new Deparser(query, opts).deparseQuery();
|
|
38
|
+
}
|
|
39
|
+
DefElem(node, context) {
|
|
40
|
+
if (!node.arg && node.defaction !== 'DEFELEM_DROP' && context.parentNodeTypes.includes('AlterOperatorStmt')) {
|
|
41
|
+
return `${node.defname} = NONE`;
|
|
42
|
+
}
|
|
43
|
+
return super.DefElem(node, context);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
exports.Deparser = Deparser;
|
|
12
47
|
async function deparse(query, opts) {
|
|
13
|
-
|
|
14
|
-
return await (0, deparser_1.deparse)(ast17, opts);
|
|
48
|
+
return Deparser.deparse(query, opts);
|
|
15
49
|
}
|
|
16
50
|
function deparseSync(query, opts) {
|
|
17
|
-
|
|
18
|
-
return (0, deparser_1.deparseSync)(ast17, opts);
|
|
51
|
+
return Deparser.deparse(query, opts);
|
|
19
52
|
}
|
|
53
|
+
var sql_formatter_1 = require("./deparser/utils/sql-formatter");
|
|
54
|
+
Object.defineProperty(exports, "splitSqlLines", { enumerable: true, get: function () { return sql_formatter_1.splitSqlLines; } });
|
|
55
|
+
var quotes_1 = require("@pgsql/quotes");
|
|
56
|
+
Object.defineProperty(exports, "QuoteUtils", { enumerable: true, get: function () { return quotes_1.QuoteUtils; } });
|
package/package.json
CHANGED
package/v16-to-v17.d.ts
CHANGED
|
@@ -496,6 +496,9 @@ export declare class V16ToV17Transformer {
|
|
|
496
496
|
MergeStmt(node: any, context: any): {
|
|
497
497
|
MergeStmt: any;
|
|
498
498
|
};
|
|
499
|
+
MergeWhenClause(node: any, context: any): {
|
|
500
|
+
MergeWhenClause: any;
|
|
501
|
+
};
|
|
499
502
|
AlterTableMoveAllStmt(node: any, context: any): {
|
|
500
503
|
AlterTableMoveAllStmt: any;
|
|
501
504
|
};
|