@memberjunction/sql-converter 6.2.0-edge.0 → 6.2.0-edge.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/IncrementalBaker.d.ts +7 -1
- package/dist/IncrementalBaker.d.ts.map +1 -1
- package/dist/IncrementalBaker.js +16 -6
- package/dist/IncrementalBaker.js.map +1 -1
- package/dist/MigrationConverter.d.ts +4 -0
- package/dist/MigrationConverter.d.ts.map +1 -1
- package/dist/MigrationConverter.js +21 -13
- package/dist/MigrationConverter.js.map +1 -1
- package/dist/MigrationSplitter.d.ts +4 -0
- package/dist/MigrationSplitter.d.ts.map +1 -1
- package/dist/MigrationSplitter.js +11 -3
- package/dist/MigrationSplitter.js.map +1 -1
- package/dist/MigrationStatementSplitter.d.ts +4 -0
- package/dist/MigrationStatementSplitter.d.ts.map +1 -1
- package/dist/MigrationStatementSplitter.js +10 -2
- package/dist/MigrationStatementSplitter.js.map +1 -1
- package/dist/index.d.ts +6 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -6
- package/dist/index.js.map +1 -1
- package/dist/rules/AlterTableRule.js +8 -8
- package/dist/rules/BatchConverter.d.ts +4 -0
- package/dist/rules/BatchConverter.d.ts.map +1 -1
- package/dist/rules/BatchConverter.js +23 -15
- package/dist/rules/BatchConverter.js.map +1 -1
- package/dist/rules/ConditionalDDLRule.js +7 -7
- package/dist/rules/CoreMetadataBooleanColumns.d.ts +2 -0
- package/dist/rules/CoreMetadataBooleanColumns.d.ts.map +1 -1
- package/dist/rules/CoreMetadataBooleanColumns.js +5 -1
- package/dist/rules/CoreMetadataBooleanColumns.js.map +1 -1
- package/dist/rules/CreateIndexRule.js +2 -2
- package/dist/rules/CreateTableRule.js +5 -5
- package/dist/rules/DeclareDmlBlockRule.js +15 -15
- package/dist/rules/DialectHeaderBuilder.d.ts +4 -0
- package/dist/rules/DialectHeaderBuilder.d.ts.map +1 -1
- package/dist/rules/DialectHeaderBuilder.js +10 -2
- package/dist/rules/DialectHeaderBuilder.js.map +1 -1
- package/dist/rules/ExecBlockRule.js +4 -4
- package/dist/rules/ExpressionHelpers.d.ts +46 -0
- package/dist/rules/ExpressionHelpers.d.ts.map +1 -1
- package/dist/rules/ExpressionHelpers.js +178 -33
- package/dist/rules/ExpressionHelpers.js.map +1 -1
- package/dist/rules/FunctionRule.js +13 -13
- package/dist/rules/GrantRule.js +2 -2
- package/dist/rules/InsertRule.js +18 -18
- package/dist/rules/ParityReporter.d.ts +2 -0
- package/dist/rules/ParityReporter.d.ts.map +1 -1
- package/dist/rules/ParityReporter.js +5 -1
- package/dist/rules/ParityReporter.js.map +1 -1
- package/dist/rules/PostProcessor.d.ts +2 -0
- package/dist/rules/PostProcessor.d.ts.map +1 -1
- package/dist/rules/PostProcessor.js +8 -4
- package/dist/rules/PostProcessor.js.map +1 -1
- package/dist/rules/ProcedureToFunctionRule.js +17 -17
- package/dist/rules/SequenceDeduplicator.d.ts +2 -0
- package/dist/rules/SequenceDeduplicator.d.ts.map +1 -1
- package/dist/rules/SequenceDeduplicator.js +5 -1
- package/dist/rules/SequenceDeduplicator.js.map +1 -1
- package/dist/rules/StatementClassifier.d.ts +2 -0
- package/dist/rules/StatementClassifier.d.ts.map +1 -1
- package/dist/rules/StatementClassifier.js +6 -2
- package/dist/rules/StatementClassifier.js.map +1 -1
- package/dist/rules/SubSplitter.d.ts +2 -0
- package/dist/rules/SubSplitter.d.ts.map +1 -1
- package/dist/rules/SubSplitter.js +5 -1
- package/dist/rules/SubSplitter.js.map +1 -1
- package/dist/rules/TSQLToPostgresRules.d.ts +4 -0
- package/dist/rules/TSQLToPostgresRules.d.ts.map +1 -1
- package/dist/rules/TSQLToPostgresRules.js +10 -2
- package/dist/rules/TSQLToPostgresRules.js.map +1 -1
- package/dist/rules/TriggerRule.js +3 -3
- package/dist/rules/TypeResolver.d.ts +6 -0
- package/dist/rules/TypeResolver.d.ts.map +1 -1
- package/dist/rules/TypeResolver.js +16 -4
- package/dist/rules/TypeResolver.js.map +1 -1
- package/dist/rules/ViewRule.js +16 -16
- package/dist/rules/index.d.ts +10 -10
- package/dist/rules/index.d.ts.map +1 -1
- package/dist/rules/index.js +10 -10
- package/dist/rules/index.js.map +1 -1
- package/dist/rules/types.d.ts +6 -0
- package/dist/rules/types.d.ts.map +1 -1
- package/dist/rules/types.js +17 -5
- package/dist/rules/types.js.map +1 -1
- package/package.json +3 -3
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { ResolveType } from './TypeResolver.js';
|
|
2
|
+
import { RemoveNPrefix } from './ExpressionHelpers.js';
|
|
3
3
|
import { POSTGRESQL_PROCEDURE_PARAM_LIMIT } from './ProcedureToFunctionRule.js';
|
|
4
4
|
export class ExecBlockRule {
|
|
5
5
|
constructor() {
|
|
@@ -284,7 +284,7 @@ export class ExecBlockRule {
|
|
|
284
284
|
const tsqlType = m[2].trim();
|
|
285
285
|
return {
|
|
286
286
|
name: `p_${m[1]}`,
|
|
287
|
-
pgType:
|
|
287
|
+
pgType: ResolveType(tsqlType),
|
|
288
288
|
};
|
|
289
289
|
}
|
|
290
290
|
/** Find where the DECLARE section ends (first SET at start of line) */
|
|
@@ -477,7 +477,7 @@ export class ExecBlockRule {
|
|
|
477
477
|
convertValue(value) {
|
|
478
478
|
let result = value;
|
|
479
479
|
// Remove N prefix from string literals
|
|
480
|
-
result =
|
|
480
|
+
result = RemoveNPrefix(result);
|
|
481
481
|
// Convert CAST types: CAST(x AS NVARCHAR(MAX)) → CAST(x AS TEXT)
|
|
482
482
|
result = result.replace(/\bAS\s+NVARCHAR\s*\(\s*MAX\s*\)/gi, 'AS TEXT');
|
|
483
483
|
result = result.replace(/\bAS\s+NVARCHAR\s*\(\s*(\d+)\s*\)/gi, 'AS VARCHAR($1)');
|
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
* Apply a transformation function only to SQL code segments,
|
|
3
3
|
* preserving string literals and comments unchanged.
|
|
4
4
|
*/
|
|
5
|
+
export declare function TransformCodeOnly(sql: string, transform: (code: string) => string): string;
|
|
6
|
+
/** @deprecated Use {@link TransformCodeOnly}. */
|
|
5
7
|
export declare function transformCodeOnly(sql: string, transform: (code: string) => string): string;
|
|
6
8
|
/**
|
|
7
9
|
* Escape every regex metacharacter in `literal` so it can be interpolated into a
|
|
@@ -12,6 +14,8 @@ export declare function transformCodeOnly(sql: string, transform: (code: string)
|
|
|
12
14
|
* pattern means — a `$` becomes an end-anchor, so the pattern matches nothing and
|
|
13
15
|
* the conversion quietly emits nothing. See issue #3171.
|
|
14
16
|
*/
|
|
17
|
+
export declare function EscapeRegExp(literal: string): string;
|
|
18
|
+
/** @deprecated Use {@link EscapeRegExp}. */
|
|
15
19
|
export declare function escapeRegExp(literal: string): string;
|
|
16
20
|
/**
|
|
17
21
|
* Remove comments, keeping code and string literals intact.
|
|
@@ -65,26 +69,36 @@ export declare function QuoteConstraintNames(sql: string): string;
|
|
|
65
69
|
* identical across both so reviewers reading converter output recognize
|
|
66
70
|
* the pattern at a glance.
|
|
67
71
|
*/
|
|
72
|
+
export declare function EmitDropOverloadsBlock(funcName: string, schema?: string): string;
|
|
73
|
+
/** @deprecated Use {@link EmitDropOverloadsBlock}. */
|
|
68
74
|
export declare function emitDropOverloadsBlock(funcName: string, schema?: string): string;
|
|
69
75
|
/** Convert [schema].[name] bracket identifiers to schema."name" double-quote format.
|
|
70
76
|
* Also converts T-SQL temp table #name references to PostgreSQL equivalents.
|
|
71
77
|
* Skips content inside SQL string literals and comments. */
|
|
78
|
+
export declare function ConvertIdentifiers(sql: string): string;
|
|
79
|
+
/** @deprecated Use {@link ConvertIdentifiers}. */
|
|
72
80
|
export declare function convertIdentifiers(sql: string): string;
|
|
73
81
|
/**
|
|
74
82
|
* Convert DATEADD(unit, num, date) → (date + num * INTERVAL '1 unit')
|
|
75
83
|
* Convert DATEDIFF(unit, start, end) → EXTRACT/age patterns
|
|
76
84
|
* Convert DATEPART(unit, date) → EXTRACT(field FROM date)
|
|
77
85
|
*/
|
|
86
|
+
export declare function ConvertDateFunctions(sql: string): string;
|
|
87
|
+
/** @deprecated Use {@link ConvertDateFunctions}. */
|
|
78
88
|
export declare function convertDateFunctions(sql: string): string;
|
|
79
89
|
/**
|
|
80
90
|
* Convert CHARINDEX(substr, str[, start]) → POSITION(substr IN str)
|
|
81
91
|
* 3-arg form: → (POSITION(substr IN SUBSTRING(str FROM start)) + start - 1)
|
|
82
92
|
*/
|
|
93
|
+
export declare function ConvertCharIndex(sql: string): string;
|
|
94
|
+
/** @deprecated Use {@link ConvertCharIndex}. */
|
|
83
95
|
export declare function convertCharIndex(sql: string): string;
|
|
84
96
|
/**
|
|
85
97
|
* Convert STUFF(string, start, length, replacement) →
|
|
86
98
|
* OVERLAY(string PLACING replacement FROM start FOR length)
|
|
87
99
|
*/
|
|
100
|
+
export declare function ConvertStuff(sql: string): string;
|
|
101
|
+
/** @deprecated Use {@link ConvertStuff}. */
|
|
88
102
|
export declare function convertStuff(sql: string): string;
|
|
89
103
|
/**
|
|
90
104
|
* Convert T-SQL string concatenation with + to PostgreSQL || operator.
|
|
@@ -96,17 +110,23 @@ export declare function convertStuff(sql: string): string;
|
|
|
96
110
|
* uses column type information to disambiguate "ColA" + "ColB" —
|
|
97
111
|
* converting to || only when at least one side is a known string type.
|
|
98
112
|
*/
|
|
113
|
+
export declare function ConvertStringConcat(sql: string, tableColumns?: Map<string, Map<string, string>>): string;
|
|
114
|
+
/** @deprecated Use {@link ConvertStringConcat}. */
|
|
99
115
|
export declare function convertStringConcat(sql: string, tableColumns?: Map<string, Map<string, string>>): string;
|
|
100
116
|
/**
|
|
101
117
|
* Convert IIF(condition, true_val, false_val) → CASE WHEN condition THEN true_val ELSE false_val END
|
|
102
118
|
* Uses paren-aware argument splitting to handle nested function calls.
|
|
103
119
|
*/
|
|
120
|
+
export declare function ConvertIIF(sql: string): string;
|
|
121
|
+
/** @deprecated Use {@link ConvertIIF}. */
|
|
104
122
|
export declare function convertIIF(sql: string): string;
|
|
105
123
|
/**
|
|
106
124
|
* Convert SELECT TOP N ... to SELECT ... LIMIT N.
|
|
107
125
|
* Moves TOP N from after SELECT to LIMIT N at end of statement.
|
|
108
126
|
* Skips matches inside string literals and comments.
|
|
109
127
|
*/
|
|
128
|
+
export declare function ConvertTopToLimit(sql: string): string;
|
|
129
|
+
/** @deprecated Use {@link ConvertTopToLimit}. */
|
|
110
130
|
export declare function convertTopToLimit(sql: string): string;
|
|
111
131
|
/**
|
|
112
132
|
* Convert common T-SQL CAST patterns to PostgreSQL types.
|
|
@@ -119,11 +139,15 @@ export declare function convertTopToLimit(sql: string): string;
|
|
|
119
139
|
* exist`. We strip quotes from known T-SQL type names first so the
|
|
120
140
|
* existing patterns below match.
|
|
121
141
|
*/
|
|
142
|
+
export declare function ConvertCastTypes(sql: string): string;
|
|
143
|
+
/** @deprecated Use {@link ConvertCastTypes}. */
|
|
122
144
|
export declare function convertCastTypes(sql: string): string;
|
|
123
145
|
/**
|
|
124
146
|
* Convert T-SQL CONVERT(type, expr[, style]) → CAST(expr AS mapped_type).
|
|
125
147
|
* Drops the optional style parameter.
|
|
126
148
|
*/
|
|
149
|
+
export declare function ConvertConvertFunction(sql: string): string;
|
|
150
|
+
/** @deprecated Use {@link ConvertConvertFunction}. */
|
|
127
151
|
export declare function convertConvertFunction(sql: string): string;
|
|
128
152
|
/**
|
|
129
153
|
* Remove T-SQL's N' unicode-string prefix.
|
|
@@ -142,8 +166,12 @@ export declare function convertConvertFunction(sql: string): string;
|
|
|
142
166
|
* operator, comma, equals, start of line). This leaves N' inside a
|
|
143
167
|
* string alone.
|
|
144
168
|
*/
|
|
169
|
+
export declare function RemoveNPrefix(sql: string): string;
|
|
170
|
+
/** @deprecated Use {@link RemoveNPrefix}. */
|
|
145
171
|
export declare function removeNPrefix(sql: string): string;
|
|
146
172
|
/** Remove COLLATE clauses */
|
|
173
|
+
export declare function RemoveCollate(sql: string): string;
|
|
174
|
+
/** @deprecated Use {@link RemoveCollate}. */
|
|
147
175
|
export declare function removeCollate(sql: string): string;
|
|
148
176
|
/**
|
|
149
177
|
* Quote bare PascalCase identifiers in SQL while preserving string literals
|
|
@@ -151,8 +179,12 @@ export declare function removeCollate(sql: string): string;
|
|
|
151
179
|
* and only quotes identifiers in code segments.
|
|
152
180
|
* Used by InsertRule to quote column names in INSERT/UPDATE/DELETE statements.
|
|
153
181
|
*/
|
|
182
|
+
export declare function QuotePascalCaseIdentifiers(sql: string): string;
|
|
183
|
+
/** @deprecated Use {@link QuotePascalCaseIdentifiers}. */
|
|
154
184
|
export declare function quotePascalCaseIdentifiers(sql: string): string;
|
|
155
185
|
/** Common function replacements */
|
|
186
|
+
export declare function ConvertCommonFunctions(sql: string): string;
|
|
187
|
+
/** @deprecated Use {@link ConvertCommonFunctions}. */
|
|
156
188
|
export declare function convertCommonFunctions(sql: string): string;
|
|
157
189
|
/**
|
|
158
190
|
* Convert SQL Server BIT comparisons (`"Col" = 1` / `= 0`) to PostgreSQL boolean
|
|
@@ -167,7 +199,11 @@ export declare function convertCommonFunctions(sql: string): string;
|
|
|
167
199
|
* AND never a non-boolean type in any other table, so an integer column that
|
|
168
200
|
* merely shares a name with a boolean column elsewhere is left untouched.
|
|
169
201
|
*/
|
|
202
|
+
export declare function CollectBooleanColumnNames(tableColumns: Map<string, Map<string, string>>): Set<string>;
|
|
203
|
+
/** @deprecated Use {@link CollectBooleanColumnNames}. */
|
|
170
204
|
export declare function collectBooleanColumnNames(tableColumns: Map<string, Map<string, string>>): Set<string>;
|
|
205
|
+
export declare function ConvertBooleanLiteralComparisons(sql: string, tableColumns: Map<string, Map<string, string>>): string;
|
|
206
|
+
/** @deprecated Use {@link ConvertBooleanLiteralComparisons}. */
|
|
171
207
|
export declare function convertBooleanLiteralComparisons(sql: string, tableColumns: Map<string, Map<string, string>>): string;
|
|
172
208
|
/**
|
|
173
209
|
* Convert SQL Server JSON functions to PostgreSQL equivalents:
|
|
@@ -179,6 +215,8 @@ export declare function convertBooleanLiteralComparisons(sql: string, tableColum
|
|
|
179
215
|
* so it is order-independent relative to identifier quoting. PG 16+ provides the IS JSON
|
|
180
216
|
* predicate; JSON_VALUE's `$.`-rooted path is rewritten to jsonb path-extraction operators.
|
|
181
217
|
*/
|
|
218
|
+
export declare function ConvertJsonFunctions(sql: string): string;
|
|
219
|
+
/** @deprecated Use {@link ConvertJsonFunctions}. */
|
|
182
220
|
export declare function convertJsonFunctions(sql: string): string;
|
|
183
221
|
/**
|
|
184
222
|
* Cast SQL Server BIT literals (0/1) to PG boolean (FALSE/TRUE) in `INSERT INTO
|
|
@@ -195,6 +233,14 @@ export declare function convertJsonFunctions(sql: string): string;
|
|
|
195
233
|
* rewriting only the first would silently leave the rest as integer literals.
|
|
196
234
|
* Each statement's VALUES region is bounded by its own top-level `;`, so an
|
|
197
235
|
* intervening statement can never be rewritten with the wrong table's positions.
|
|
236
|
+
*
|
|
237
|
+
* The `INSERT INTO table (...) SELECT <list> [FROM/WHERE ...]` form is rewritten too, in the
|
|
238
|
+
* select list only. CodeGen writes its EntityPermission grants that way
|
|
239
|
+
* (`SELECT <EntityID>, <RoleID>, 'Allow', 1, 0, 0, 0, ... WHERE NOT EXISTS (...)`), and before
|
|
240
|
+
* this every migration registering a new entity failed on apply with
|
|
241
|
+
* `column "CanRead" is of type boolean but expression is of type integer`.
|
|
198
242
|
*/
|
|
243
|
+
export declare function CastBooleanInsertValues(sql: string, tableColumns: Map<string, Map<string, string>>): string;
|
|
244
|
+
/** @deprecated Use {@link CastBooleanInsertValues}. */
|
|
199
245
|
export declare function castBooleanInsertValues(sql: string, tableColumns: Map<string, Map<string, string>>): string;
|
|
200
246
|
//# sourceMappingURL=ExpressionHelpers.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ExpressionHelpers.d.ts","sourceRoot":"","sources":["../../src/rules/ExpressionHelpers.ts"],"names":[],"mappings":"AAiFA;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,G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AAgB,uBAAuB,CACrC,GAAG,EAAE,MAAM,EACX,YAAY,EAAE,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,GAC7C,MAAM,CAER"}
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* convert_charindex, convert_stuff, convert_string_concat, convert_iif,
|
|
5
5
|
* convert_top_to_limit, etc.
|
|
6
6
|
*/
|
|
7
|
-
import {
|
|
7
|
+
import { ResolveInlineType } from './TypeResolver.js';
|
|
8
8
|
/**
|
|
9
9
|
* Split SQL into segments of code, string literals, and comments.
|
|
10
10
|
* String literals (single-quoted, with '' escaping) and comments (-- and block)
|
|
@@ -78,13 +78,17 @@ function segmentSQL(sql) {
|
|
|
78
78
|
* Apply a transformation function only to SQL code segments,
|
|
79
79
|
* preserving string literals and comments unchanged.
|
|
80
80
|
*/
|
|
81
|
-
export function
|
|
81
|
+
export function TransformCodeOnly(sql, transform) {
|
|
82
82
|
return segmentSQL(sql).map(seg => {
|
|
83
83
|
if (seg.type !== 'code')
|
|
84
84
|
return seg.text;
|
|
85
85
|
return transform(seg.text);
|
|
86
86
|
}).join('');
|
|
87
87
|
}
|
|
88
|
+
/** @deprecated Use {@link TransformCodeOnly}. */
|
|
89
|
+
export function transformCodeOnly(sql, transform) {
|
|
90
|
+
return TransformCodeOnly(sql, transform);
|
|
91
|
+
}
|
|
88
92
|
/**
|
|
89
93
|
* Escape every regex metacharacter in `literal` so it can be interpolated into a
|
|
90
94
|
* `RegExp` and match itself.
|
|
@@ -94,9 +98,13 @@ export function transformCodeOnly(sql, transform) {
|
|
|
94
98
|
* pattern means — a `$` becomes an end-anchor, so the pattern matches nothing and
|
|
95
99
|
* the conversion quietly emits nothing. See issue #3171.
|
|
96
100
|
*/
|
|
97
|
-
export function
|
|
101
|
+
export function EscapeRegExp(literal) {
|
|
98
102
|
return literal.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
99
103
|
}
|
|
104
|
+
/** @deprecated Use {@link EscapeRegExp}. */
|
|
105
|
+
export function escapeRegExp(literal) {
|
|
106
|
+
return EscapeRegExp(literal);
|
|
107
|
+
}
|
|
100
108
|
/**
|
|
101
109
|
* Remove comments, keeping code and string literals intact.
|
|
102
110
|
*
|
|
@@ -134,7 +142,7 @@ export function StripComments(sql) {
|
|
|
134
142
|
* through one module.
|
|
135
143
|
*/
|
|
136
144
|
export function QuoteConstraintNames(sql) {
|
|
137
|
-
return
|
|
145
|
+
return TransformCodeOnly(sql, (code) => code.replace(/\bCONSTRAINT\s+(?!IF\s+EXISTS\b)([A-Za-z_]\w*)\b/gi, (match, name) => (/[A-Z]/.test(name) ? `CONSTRAINT "${name}"` : match)));
|
|
138
146
|
}
|
|
139
147
|
/**
|
|
140
148
|
* Emit a DO-block that drops every overload of a function in a given schema.
|
|
@@ -155,7 +163,7 @@ export function QuoteConstraintNames(sql) {
|
|
|
155
163
|
* identical across both so reviewers reading converter output recognize
|
|
156
164
|
* the pattern at a glance.
|
|
157
165
|
*/
|
|
158
|
-
export function
|
|
166
|
+
export function EmitDropOverloadsBlock(funcName, schema = '__mj') {
|
|
159
167
|
return (`DO $$ DECLARE r record;\n` +
|
|
160
168
|
`BEGIN\n` +
|
|
161
169
|
` FOR r IN SELECT oid::regprocedure AS sig FROM pg_proc\n` +
|
|
@@ -165,11 +173,15 @@ export function emitDropOverloadsBlock(funcName, schema = '__mj') {
|
|
|
165
173
|
` END LOOP;\n` +
|
|
166
174
|
`END $$;\n`);
|
|
167
175
|
}
|
|
176
|
+
/** @deprecated Use {@link EmitDropOverloadsBlock}. */
|
|
177
|
+
export function emitDropOverloadsBlock(funcName, schema = '__mj') {
|
|
178
|
+
return EmitDropOverloadsBlock(funcName, schema);
|
|
179
|
+
}
|
|
168
180
|
/** Convert [schema].[name] bracket identifiers to schema."name" double-quote format.
|
|
169
181
|
* Also converts T-SQL temp table #name references to PostgreSQL equivalents.
|
|
170
182
|
* Skips content inside SQL string literals and comments. */
|
|
171
|
-
export function
|
|
172
|
-
return
|
|
183
|
+
export function ConvertIdentifiers(sql) {
|
|
184
|
+
return TransformCodeOnly(sql, (code) => {
|
|
173
185
|
// Temp table: CREATE TABLE #name → CREATE TEMP TABLE "name"
|
|
174
186
|
code = code.replace(/\bCREATE\s+TABLE\s+#(\w+)/gi, 'CREATE TEMP TABLE "$1"');
|
|
175
187
|
// Strip # from remaining temp object references: #name → "name"
|
|
@@ -192,18 +204,26 @@ export function convertIdentifiers(sql) {
|
|
|
192
204
|
return code;
|
|
193
205
|
});
|
|
194
206
|
}
|
|
207
|
+
/** @deprecated Use {@link ConvertIdentifiers}. */
|
|
208
|
+
export function convertIdentifiers(sql) {
|
|
209
|
+
return ConvertIdentifiers(sql);
|
|
210
|
+
}
|
|
195
211
|
/**
|
|
196
212
|
* Convert DATEADD(unit, num, date) → (date + num * INTERVAL '1 unit')
|
|
197
213
|
* Convert DATEDIFF(unit, start, end) → EXTRACT/age patterns
|
|
198
214
|
* Convert DATEPART(unit, date) → EXTRACT(field FROM date)
|
|
199
215
|
*/
|
|
200
|
-
export function
|
|
216
|
+
export function ConvertDateFunctions(sql) {
|
|
201
217
|
sql = convertDateAdd(sql);
|
|
202
218
|
sql = convertDateDiff(sql);
|
|
203
219
|
sql = convertDatePart(sql);
|
|
204
220
|
sql = convertSimpleDateFunctions(sql);
|
|
205
221
|
return sql;
|
|
206
222
|
}
|
|
223
|
+
/** @deprecated Use {@link ConvertDateFunctions}. */
|
|
224
|
+
export function convertDateFunctions(sql) {
|
|
225
|
+
return ConvertDateFunctions(sql);
|
|
226
|
+
}
|
|
207
227
|
/**
|
|
208
228
|
* Convert T-SQL shorthand date functions to PostgreSQL EXTRACT.
|
|
209
229
|
* YEAR(expr) → EXTRACT(YEAR FROM expr)
|
|
@@ -375,7 +395,7 @@ function convertDatePart(sql) {
|
|
|
375
395
|
* Convert CHARINDEX(substr, str[, start]) → POSITION(substr IN str)
|
|
376
396
|
* 3-arg form: → (POSITION(substr IN SUBSTRING(str FROM start)) + start - 1)
|
|
377
397
|
*/
|
|
378
|
-
export function
|
|
398
|
+
export function ConvertCharIndex(sql) {
|
|
379
399
|
return sql.replace(/\bCHARINDEX\s*\(\s*([^,]+)\s*,\s*([^,)]+)(?:\s*,\s*([^)]+))?\s*\)/gi, (_match, substr, str, start) => {
|
|
380
400
|
const subTrimmed = substr.trim();
|
|
381
401
|
const strTrimmed = str.trim();
|
|
@@ -386,15 +406,23 @@ export function convertCharIndex(sql) {
|
|
|
386
406
|
return `POSITION(${subTrimmed} IN ${strTrimmed})`;
|
|
387
407
|
});
|
|
388
408
|
}
|
|
409
|
+
/** @deprecated Use {@link ConvertCharIndex}. */
|
|
410
|
+
export function convertCharIndex(sql) {
|
|
411
|
+
return ConvertCharIndex(sql);
|
|
412
|
+
}
|
|
389
413
|
/**
|
|
390
414
|
* Convert STUFF(string, start, length, replacement) →
|
|
391
415
|
* OVERLAY(string PLACING replacement FROM start FOR length)
|
|
392
416
|
*/
|
|
393
|
-
export function
|
|
417
|
+
export function ConvertStuff(sql) {
|
|
394
418
|
return sql.replace(/\bSTUFF\s*\(\s*([^,]+)\s*,\s*([^,]+)\s*,\s*([^,]+)\s*,\s*([^)]+)\s*\)/gi, (_match, str, start, length, replacement) => {
|
|
395
419
|
return `OVERLAY(${str.trim()} PLACING ${replacement.trim()} FROM ${start.trim()} FOR ${length.trim()})`;
|
|
396
420
|
});
|
|
397
421
|
}
|
|
422
|
+
/** @deprecated Use {@link ConvertStuff}. */
|
|
423
|
+
export function convertStuff(sql) {
|
|
424
|
+
return ConvertStuff(sql);
|
|
425
|
+
}
|
|
398
426
|
/** PG type strings that represent textual/string data */
|
|
399
427
|
const PG_STRING_TYPES = new Set(['TEXT', 'VARCHAR', 'CHAR', 'UUID', 'XML']);
|
|
400
428
|
/**
|
|
@@ -434,7 +462,7 @@ function isStringColumn(ident, tableColumns) {
|
|
|
434
462
|
* uses column type information to disambiguate "ColA" + "ColB" —
|
|
435
463
|
* converting to || only when at least one side is a known string type.
|
|
436
464
|
*/
|
|
437
|
-
export function
|
|
465
|
+
export function ConvertStringConcat(sql, tableColumns) {
|
|
438
466
|
const segments = segmentSQL(sql);
|
|
439
467
|
for (let i = 0; i < segments.length; i++) {
|
|
440
468
|
if (segments[i].type !== 'code')
|
|
@@ -496,11 +524,15 @@ export function convertStringConcat(sql, tableColumns) {
|
|
|
496
524
|
}
|
|
497
525
|
return segments.map(s => s.text).join('');
|
|
498
526
|
}
|
|
527
|
+
/** @deprecated Use {@link ConvertStringConcat}. */
|
|
528
|
+
export function convertStringConcat(sql, tableColumns) {
|
|
529
|
+
return ConvertStringConcat(sql, tableColumns);
|
|
530
|
+
}
|
|
499
531
|
/**
|
|
500
532
|
* Convert IIF(condition, true_val, false_val) → CASE WHEN condition THEN true_val ELSE false_val END
|
|
501
533
|
* Uses paren-aware argument splitting to handle nested function calls.
|
|
502
534
|
*/
|
|
503
|
-
export function
|
|
535
|
+
export function ConvertIIF(sql) {
|
|
504
536
|
let iterations = 0;
|
|
505
537
|
const maxIterations = 50;
|
|
506
538
|
while (/\bIIF\s*\(/i.test(sql) && iterations < maxIterations) {
|
|
@@ -531,6 +563,10 @@ export function convertIIF(sql) {
|
|
|
531
563
|
}
|
|
532
564
|
return sql;
|
|
533
565
|
}
|
|
566
|
+
/** @deprecated Use {@link ConvertIIF}. */
|
|
567
|
+
export function convertIIF(sql) {
|
|
568
|
+
return ConvertIIF(sql);
|
|
569
|
+
}
|
|
534
570
|
/** Find the position of the matching closing parenthesis, respecting nesting and string literals */
|
|
535
571
|
function findMatchingParen(sql, openPos) {
|
|
536
572
|
let depth = 0;
|
|
@@ -607,7 +643,7 @@ function splitTopLevelCommas(str) {
|
|
|
607
643
|
* Moves TOP N from after SELECT to LIMIT N at end of statement.
|
|
608
644
|
* Skips matches inside string literals and comments.
|
|
609
645
|
*/
|
|
610
|
-
export function
|
|
646
|
+
export function ConvertTopToLimit(sql) {
|
|
611
647
|
// Build protected ranges from string literals and comments
|
|
612
648
|
const segments = segmentSQL(sql);
|
|
613
649
|
const protectedRanges = [];
|
|
@@ -664,6 +700,10 @@ export function convertTopToLimit(sql) {
|
|
|
664
700
|
}
|
|
665
701
|
return sql;
|
|
666
702
|
}
|
|
703
|
+
/** @deprecated Use {@link ConvertTopToLimit}. */
|
|
704
|
+
export function convertTopToLimit(sql) {
|
|
705
|
+
return ConvertTopToLimit(sql);
|
|
706
|
+
}
|
|
667
707
|
/**
|
|
668
708
|
* Convert common T-SQL CAST patterns to PostgreSQL types.
|
|
669
709
|
* Used in views, procedures, and expressions.
|
|
@@ -675,7 +715,7 @@ export function convertTopToLimit(sql) {
|
|
|
675
715
|
* exist`. We strip quotes from known T-SQL type names first so the
|
|
676
716
|
* existing patterns below match.
|
|
677
717
|
*/
|
|
678
|
-
export function
|
|
718
|
+
export function ConvertCastTypes(sql) {
|
|
679
719
|
// Strip quotes from quoted T-SQL type tokens produced by convertIdentifiers
|
|
680
720
|
// when the source SQL had bracket-wrapped types (e.g. CAST(x AS [INT])).
|
|
681
721
|
const quotedTypes = [
|
|
@@ -701,11 +741,15 @@ export function convertCastTypes(sql) {
|
|
|
701
741
|
sql = sql.replace(/\bAS\s+INT\b/gi, 'AS INTEGER');
|
|
702
742
|
return sql;
|
|
703
743
|
}
|
|
744
|
+
/** @deprecated Use {@link ConvertCastTypes}. */
|
|
745
|
+
export function convertCastTypes(sql) {
|
|
746
|
+
return ConvertCastTypes(sql);
|
|
747
|
+
}
|
|
704
748
|
/**
|
|
705
749
|
* Convert T-SQL CONVERT(type, expr[, style]) → CAST(expr AS mapped_type).
|
|
706
750
|
* Drops the optional style parameter.
|
|
707
751
|
*/
|
|
708
|
-
export function
|
|
752
|
+
export function ConvertConvertFunction(sql) {
|
|
709
753
|
// 3-arg: CONVERT(type, expr, style)
|
|
710
754
|
sql = sql.replace(/\bCONVERT\s*\(\s*(\w+(?:\s*\([^)]*\))?)\s*,\s*([^,)]+)\s*,\s*[^)]+\)/gi, (_match, type, expr) => {
|
|
711
755
|
return `CAST(${expr.trim()} AS ${mapInlineType(type.trim())})`;
|
|
@@ -716,12 +760,16 @@ export function convertConvertFunction(sql) {
|
|
|
716
760
|
});
|
|
717
761
|
return sql;
|
|
718
762
|
}
|
|
763
|
+
/** @deprecated Use {@link ConvertConvertFunction}. */
|
|
764
|
+
export function convertConvertFunction(sql) {
|
|
765
|
+
return ConvertConvertFunction(sql);
|
|
766
|
+
}
|
|
719
767
|
/**
|
|
720
768
|
* Map a T-SQL type name to PostgreSQL for inline CAST/CONVERT usage.
|
|
721
769
|
* Delegates to the centralized TypeResolver for consistent mapping.
|
|
722
770
|
*/
|
|
723
771
|
function mapInlineType(tsqlType) {
|
|
724
|
-
return
|
|
772
|
+
return ResolveInlineType(tsqlType);
|
|
725
773
|
}
|
|
726
774
|
/**
|
|
727
775
|
* Remove T-SQL's N' unicode-string prefix.
|
|
@@ -740,16 +788,24 @@ function mapInlineType(tsqlType) {
|
|
|
740
788
|
* operator, comma, equals, start of line). This leaves N' inside a
|
|
741
789
|
* string alone.
|
|
742
790
|
*/
|
|
743
|
-
export function
|
|
791
|
+
export function RemoveNPrefix(sql) {
|
|
744
792
|
// Anchors: start of string, or after one of the "string-start" context chars.
|
|
745
793
|
// Whitespace, comma, paren, comparison operators, brackets, semicolon.
|
|
746
794
|
return sql.replace(/(^|[\s(,=<>!+\-*\/[;])N'/g, "$1'");
|
|
747
795
|
}
|
|
796
|
+
/** @deprecated Use {@link RemoveNPrefix}. */
|
|
797
|
+
export function removeNPrefix(sql) {
|
|
798
|
+
return RemoveNPrefix(sql);
|
|
799
|
+
}
|
|
748
800
|
/** Remove COLLATE clauses */
|
|
749
|
-
export function
|
|
801
|
+
export function RemoveCollate(sql) {
|
|
750
802
|
return sql.replace(/\s+COLLATE\s+SQL_Latin1_General_CP1_CI_AS/gi, '')
|
|
751
803
|
.replace(/\s+COLLATE\s+\S+/gi, '');
|
|
752
804
|
}
|
|
805
|
+
/** @deprecated Use {@link RemoveCollate}. */
|
|
806
|
+
export function removeCollate(sql) {
|
|
807
|
+
return RemoveCollate(sql);
|
|
808
|
+
}
|
|
753
809
|
/** SQL keywords that should NOT be quoted by quotePascalCaseIdentifiers */
|
|
754
810
|
const PASCAL_QUOTE_KEYWORDS = new Set([
|
|
755
811
|
'SELECT', 'FROM', 'WHERE', 'AND', 'OR', 'NOT', 'NULL', 'IN', 'IS',
|
|
@@ -794,8 +850,8 @@ const PASCAL_QUOTE_KEYWORDS = new Set([
|
|
|
794
850
|
* and only quotes identifiers in code segments.
|
|
795
851
|
* Used by InsertRule to quote column names in INSERT/UPDATE/DELETE statements.
|
|
796
852
|
*/
|
|
797
|
-
export function
|
|
798
|
-
return
|
|
853
|
+
export function QuotePascalCaseIdentifiers(sql) {
|
|
854
|
+
return TransformCodeOnly(sql, (code) => {
|
|
799
855
|
return code.replace(/(?<!")(?<!\w)([A-Z]\w*)(?!")(?!\w)/g, (match, word) => {
|
|
800
856
|
if (PASCAL_QUOTE_KEYWORDS.has(word.toUpperCase()))
|
|
801
857
|
return match;
|
|
@@ -803,8 +859,12 @@ export function quotePascalCaseIdentifiers(sql) {
|
|
|
803
859
|
});
|
|
804
860
|
});
|
|
805
861
|
}
|
|
862
|
+
/** @deprecated Use {@link QuotePascalCaseIdentifiers}. */
|
|
863
|
+
export function quotePascalCaseIdentifiers(sql) {
|
|
864
|
+
return QuotePascalCaseIdentifiers(sql);
|
|
865
|
+
}
|
|
806
866
|
/** Common function replacements */
|
|
807
|
-
export function
|
|
867
|
+
export function ConvertCommonFunctions(sql) {
|
|
808
868
|
sql = sql.replace(/\bISNULL\s*\(/gi, 'COALESCE(');
|
|
809
869
|
sql = sql.replace(/\bGETUTCDATE\s*\(\s*\)/gi, 'NOW()');
|
|
810
870
|
sql = sql.replace(/\bGETDATE\s*\(\s*\)/gi, 'NOW()');
|
|
@@ -827,6 +887,10 @@ export function convertCommonFunctions(sql) {
|
|
|
827
887
|
sql = sql.replace(/^([ \t]*)MERGE[ \t]+(?!INTO\b)/gim, '$1MERGE INTO ');
|
|
828
888
|
return sql;
|
|
829
889
|
}
|
|
890
|
+
/** @deprecated Use {@link ConvertCommonFunctions}. */
|
|
891
|
+
export function convertCommonFunctions(sql) {
|
|
892
|
+
return ConvertCommonFunctions(sql);
|
|
893
|
+
}
|
|
830
894
|
/**
|
|
831
895
|
* Convert SQL Server BIT comparisons (`"Col" = 1` / `= 0`) to PostgreSQL boolean
|
|
832
896
|
* literals (`"Col" = TRUE` / `= FALSE`) for columns known to be BOOLEAN.
|
|
@@ -840,7 +904,7 @@ export function convertCommonFunctions(sql) {
|
|
|
840
904
|
* AND never a non-boolean type in any other table, so an integer column that
|
|
841
905
|
* merely shares a name with a boolean column elsewhere is left untouched.
|
|
842
906
|
*/
|
|
843
|
-
export function
|
|
907
|
+
export function CollectBooleanColumnNames(tableColumns) {
|
|
844
908
|
const boolNames = new Set();
|
|
845
909
|
const nonBoolNames = new Set();
|
|
846
910
|
for (const cols of tableColumns.values()) {
|
|
@@ -856,15 +920,27 @@ export function collectBooleanColumnNames(tableColumns) {
|
|
|
856
920
|
boolNames.delete(n);
|
|
857
921
|
return boolNames;
|
|
858
922
|
}
|
|
859
|
-
|
|
860
|
-
|
|
923
|
+
/** @deprecated Use {@link CollectBooleanColumnNames}. */
|
|
924
|
+
export function collectBooleanColumnNames(tableColumns) {
|
|
925
|
+
return CollectBooleanColumnNames(tableColumns);
|
|
926
|
+
}
|
|
927
|
+
export function ConvertBooleanLiteralComparisons(sql, tableColumns) {
|
|
928
|
+
const boolNames = CollectBooleanColumnNames(tableColumns);
|
|
861
929
|
if (boolNames.size === 0)
|
|
862
930
|
return sql;
|
|
931
|
+
const asBool = (v) => (v === '1' ? 'TRUE' : 'FALSE');
|
|
863
932
|
// `(?![\w.])` guards against matching inside a larger number/identifier
|
|
864
933
|
// (e.g. `= 10`, `= 1.5`).
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
934
|
+
const bare = sql.replace(/"(\w+)"\s*(=|<>|!=)\s*([01])(?![\w.])/g, (match, col, op, val) => boolNames.has(col.toLowerCase()) ? `"${col}" ${op} ${asBool(val)}` : match);
|
|
935
|
+
// SQL Server `ISNULL(f.IsPrimaryKey, 0) = 0` transpiles to `COALESCE("f"."IsPrimaryKey", 0) = 0`,
|
|
936
|
+
// which PostgreSQL rejects with `COALESCE types boolean and integer cannot be matched`. CodeGen's
|
|
937
|
+
// search-flag hygiene block writes exactly this, so both literals are rewritten when the wrapped
|
|
938
|
+
// column (optionally alias-qualified) is boolean.
|
|
939
|
+
return bare.replace(/COALESCE\(\s*((?:"\w+"\.)?"(\w+)")\s*,\s*([01])\s*\)\s*(=|<>|!=)\s*([01])(?![\w.])/gi, (match, ref, col, dflt, op, val) => boolNames.has(col.toLowerCase()) ? `COALESCE(${ref}, ${asBool(dflt)}) ${op} ${asBool(val)}` : match);
|
|
940
|
+
}
|
|
941
|
+
/** @deprecated Use {@link ConvertBooleanLiteralComparisons}. */
|
|
942
|
+
export function convertBooleanLiteralComparisons(sql, tableColumns) {
|
|
943
|
+
return ConvertBooleanLiteralComparisons(sql, tableColumns);
|
|
868
944
|
}
|
|
869
945
|
/**
|
|
870
946
|
* Find the index just past the matching close paren for the `(` at `openPos`.
|
|
@@ -942,7 +1018,7 @@ function splitArgs(body) {
|
|
|
942
1018
|
* so it is order-independent relative to identifier quoting. PG 16+ provides the IS JSON
|
|
943
1019
|
* predicate; JSON_VALUE's `$.`-rooted path is rewritten to jsonb path-extraction operators.
|
|
944
1020
|
*/
|
|
945
|
-
export function
|
|
1021
|
+
export function ConvertJsonFunctions(sql) {
|
|
946
1022
|
sql = convertNamedJsonCall(sql, 'JSON_VALUE', (args, after) => {
|
|
947
1023
|
if (args.length < 2)
|
|
948
1024
|
return null;
|
|
@@ -974,6 +1050,10 @@ export function convertJsonFunctions(sql) {
|
|
|
974
1050
|
});
|
|
975
1051
|
return sql;
|
|
976
1052
|
}
|
|
1053
|
+
/** @deprecated Use {@link ConvertJsonFunctions}. */
|
|
1054
|
+
export function convertJsonFunctions(sql) {
|
|
1055
|
+
return ConvertJsonFunctions(sql);
|
|
1056
|
+
}
|
|
977
1057
|
/**
|
|
978
1058
|
* Locate every call to `name(` (bare or double-quoted), extract its balanced
|
|
979
1059
|
* argument list, and let `build` produce the replacement text plus how many
|
|
@@ -1020,9 +1100,15 @@ function convertNamedJsonCall(sql, name, build) {
|
|
|
1020
1100
|
* rewriting only the first would silently leave the rest as integer literals.
|
|
1021
1101
|
* Each statement's VALUES region is bounded by its own top-level `;`, so an
|
|
1022
1102
|
* intervening statement can never be rewritten with the wrong table's positions.
|
|
1103
|
+
*
|
|
1104
|
+
* The `INSERT INTO table (...) SELECT <list> [FROM/WHERE ...]` form is rewritten too, in the
|
|
1105
|
+
* select list only. CodeGen writes its EntityPermission grants that way
|
|
1106
|
+
* (`SELECT <EntityID>, <RoleID>, 'Allow', 1, 0, 0, 0, ... WHERE NOT EXISTS (...)`), and before
|
|
1107
|
+
* this every migration registering a new entity failed on apply with
|
|
1108
|
+
* `column "CanRead" is of type boolean but expression is of type integer`.
|
|
1023
1109
|
*/
|
|
1024
|
-
export function
|
|
1025
|
-
const re = /INSERT\s+INTO\s+(?:\w+\.)?"?(\w+)"?\s*\(([^)]*)\)\s*VALUES/gi;
|
|
1110
|
+
export function CastBooleanInsertValues(sql, tableColumns) {
|
|
1111
|
+
const re = /INSERT\s+INTO\s+(?:\w+\.)?"?(\w+)"?\s*\(([^)]*)\)\s*(VALUES|SELECT)\b/gi;
|
|
1026
1112
|
let out = '';
|
|
1027
1113
|
let cursor = 0;
|
|
1028
1114
|
let m;
|
|
@@ -1032,12 +1118,20 @@ export function castBooleanInsertValues(sql, tableColumns) {
|
|
|
1032
1118
|
out += sql.slice(cursor, headEnd);
|
|
1033
1119
|
const body = sql.slice(headEnd, valuesEnd);
|
|
1034
1120
|
const boolPos = booleanColumnPositions(m[1], m[2], tableColumns);
|
|
1035
|
-
|
|
1121
|
+
const isSelect = m[3].toUpperCase() === 'SELECT';
|
|
1122
|
+
if (boolPos.size === 0)
|
|
1123
|
+
out += body;
|
|
1124
|
+
else
|
|
1125
|
+
out += isSelect ? rewriteSelectList(body, boolPos) : rewriteValuesTuples(body, boolPos);
|
|
1036
1126
|
cursor = valuesEnd;
|
|
1037
1127
|
re.lastIndex = valuesEnd;
|
|
1038
1128
|
}
|
|
1039
1129
|
return cursor === 0 ? sql : out + sql.slice(cursor);
|
|
1040
1130
|
}
|
|
1131
|
+
/** @deprecated Use {@link CastBooleanInsertValues}. */
|
|
1132
|
+
export function castBooleanInsertValues(sql, tableColumns) {
|
|
1133
|
+
return CastBooleanInsertValues(sql, tableColumns);
|
|
1134
|
+
}
|
|
1041
1135
|
/** Ordinal positions in an INSERT column list whose column is BOOLEAN on PG. */
|
|
1042
1136
|
function booleanColumnPositions(table, columnList, tableColumns) {
|
|
1043
1137
|
const positions = new Set();
|
|
@@ -1116,13 +1210,64 @@ function rewriteValuesTuples(text, boolPos) {
|
|
|
1116
1210
|
}
|
|
1117
1211
|
/** Rewrite `0`/`1` → `FALSE`/`TRUE` at boolean positions within one `(...)` tuple. */
|
|
1118
1212
|
function rewriteTuple(tuple, boolPos) {
|
|
1119
|
-
|
|
1213
|
+
return '(' + rewriteListItems(tuple.slice(1, -1), boolPos) + ')';
|
|
1214
|
+
}
|
|
1215
|
+
/** Rewrite `0`/`1` → `FALSE`/`TRUE` at boolean positions in a comma-separated value list. */
|
|
1216
|
+
function rewriteListItems(list, boolPos) {
|
|
1217
|
+
const vals = splitTopLevelValues(list);
|
|
1120
1218
|
for (let k = 0; k < vals.length; k++) {
|
|
1121
1219
|
if (!boolPos.has(k))
|
|
1122
1220
|
continue;
|
|
1123
1221
|
vals[k] = vals[k].replace(/^(\s*)0(\s*)$/, '$1FALSE$2').replace(/^(\s*)1(\s*)$/, '$1TRUE$2');
|
|
1124
1222
|
}
|
|
1125
|
-
return
|
|
1223
|
+
return vals.join(',');
|
|
1224
|
+
}
|
|
1225
|
+
/** Rewrite the select list of an `INSERT ... SELECT`; the FROM/WHERE tail passes through untouched. */
|
|
1226
|
+
function rewriteSelectList(body, boolPos) {
|
|
1227
|
+
const listEnd = selectListEnd(body);
|
|
1228
|
+
return rewriteListItems(body.slice(0, listEnd), boolPos) + body.slice(listEnd);
|
|
1229
|
+
}
|
|
1230
|
+
/**
|
|
1231
|
+
* Index where a select list ends: the first top-level FROM / WHERE / GROUP / ORDER / UNION /
|
|
1232
|
+
* LIMIT keyword, or the statement's `;`. String-, comment- and paren-aware, so a keyword inside
|
|
1233
|
+
* a subquery, a quoted value or a comment never ends the list early.
|
|
1234
|
+
*/
|
|
1235
|
+
function selectListEnd(body) {
|
|
1236
|
+
const keyword = /^(FROM|WHERE|GROUP|ORDER|UNION|LIMIT)\b/i;
|
|
1237
|
+
let depth = 0, inStr = false;
|
|
1238
|
+
for (let i = 0; i < body.length; i++) {
|
|
1239
|
+
const c = body[i];
|
|
1240
|
+
if (inStr) {
|
|
1241
|
+
if (c === "'") {
|
|
1242
|
+
if (body[i + 1] === "'")
|
|
1243
|
+
i++;
|
|
1244
|
+
else
|
|
1245
|
+
inStr = false;
|
|
1246
|
+
}
|
|
1247
|
+
continue;
|
|
1248
|
+
}
|
|
1249
|
+
if (c === '/' && body[i + 1] === '*') {
|
|
1250
|
+
const e = body.indexOf('*/', i + 2);
|
|
1251
|
+
i = e === -1 ? body.length : e + 1;
|
|
1252
|
+
continue;
|
|
1253
|
+
}
|
|
1254
|
+
if (c === '-' && body[i + 1] === '-') {
|
|
1255
|
+
const e = body.indexOf('\n', i);
|
|
1256
|
+
i = e === -1 ? body.length : e;
|
|
1257
|
+
continue;
|
|
1258
|
+
}
|
|
1259
|
+
if (c === "'")
|
|
1260
|
+
inStr = true;
|
|
1261
|
+
else if (c === '(')
|
|
1262
|
+
depth++;
|
|
1263
|
+
else if (c === ')')
|
|
1264
|
+
depth--;
|
|
1265
|
+
else if (c === ';' && depth <= 0)
|
|
1266
|
+
return i;
|
|
1267
|
+
else if (depth === 0 && /\s/.test(body[i - 1] ?? ' ') && keyword.test(body.slice(i)))
|
|
1268
|
+
return i;
|
|
1269
|
+
}
|
|
1270
|
+
return body.length;
|
|
1126
1271
|
}
|
|
1127
1272
|
/**
|
|
1128
1273
|
* Split a tuple body on top-level commas, respecting single-quoted strings, nested parens AND SQL
|