pg-to-sqlite 0.5.11 → 0.5.13
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/pg-sqlite-compiler/compiler.d.ts +17 -0
- package/dist/pg-sqlite-compiler/compiler.d.ts.map +1 -0
- package/dist/pg-sqlite-compiler/compiler.js +80 -0
- package/dist/pg-sqlite-compiler/compiler.js.map +1 -0
- package/dist/pg-sqlite-compiler/index.d.ts +1 -16
- package/dist/pg-sqlite-compiler/index.d.ts.map +1 -1
- package/dist/pg-sqlite-compiler/index.js +2 -79
- package/dist/pg-sqlite-compiler/index.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { CompileOptions, CompileResult } from './types.js';
|
|
2
|
+
export declare class CompileError extends Error {
|
|
3
|
+
readonly warnings: CompileResult['warnings'];
|
|
4
|
+
readonly sql: string;
|
|
5
|
+
constructor(warnings: CompileResult['warnings'], sql: string);
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Compile a single PG SQL statement into a SQLite-compatible statement.
|
|
9
|
+
*
|
|
10
|
+
* The compiler is best-effort: it applies the registered passes and emits via
|
|
11
|
+
* pgsql-deparser. Some PG-isms are not translatable; those produce warnings
|
|
12
|
+
* but the SQL is still emitted (caller can decide to reject or run anyway).
|
|
13
|
+
*/
|
|
14
|
+
export declare function compile(pgSql: string, opts?: CompileOptions): CompileResult;
|
|
15
|
+
export declare function compileMany(pgSqls: string[], opts?: CompileOptions): CompileResult[];
|
|
16
|
+
export type { CompileOptions, CompileResult, CompileWarning, SchemaInfo, } from './types.js';
|
|
17
|
+
//# sourceMappingURL=compiler.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compiler.d.ts","sourceRoot":"","sources":["../../src/pg-sqlite-compiler/compiler.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,cAAc,EAAE,aAAa,EAAc,MAAM,YAAY,CAAA;AA0B3E,qBAAa,YAAa,SAAQ,KAAK;IAEnC,QAAQ,CAAC,QAAQ,EAAE,aAAa,CAAC,UAAU,CAAC;IAC5C,QAAQ,CAAC,GAAG,EAAE,MAAM;gBADX,QAAQ,EAAE,aAAa,CAAC,UAAU,CAAC,EACnC,GAAG,EAAE,MAAM;CAKvB;AAYD;;;;;;GAMG;AACH,wBAAgB,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,GAAE,cAAmB,GAAG,aAAa,CAqC/E;AAED,wBAAgB,WAAW,CACzB,MAAM,EAAE,MAAM,EAAE,EAChB,IAAI,GAAE,cAAmB,GACxB,aAAa,EAAE,CAEjB;AAED,YAAY,EACV,cAAc,EACd,aAAa,EACb,cAAc,EACd,UAAU,GACX,MAAM,YAAY,CAAA"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { deparseSync, parseSync } from 'pgsql-parser';
|
|
2
|
+
import { markSQLiteKeywordIdentifiers, restoreSQLiteKeywordIdentifierMarkers, } from '../sqlite-keyword-identifiers.js';
|
|
3
|
+
import { runPasses } from './passes/index.js';
|
|
4
|
+
const DEFAULT_VERSION = 170004;
|
|
5
|
+
const NOOP_SCHEMA = {
|
|
6
|
+
getColumnType: () => undefined,
|
|
7
|
+
getEnum: () => undefined,
|
|
8
|
+
isEnumValue: () => false,
|
|
9
|
+
getTableColumns: () => undefined,
|
|
10
|
+
};
|
|
11
|
+
function stripTrailingSemicolon(s) {
|
|
12
|
+
let i = s.length;
|
|
13
|
+
while (i > 0 && (s[i - 1] === ';' || s[i - 1] === ' ' || s[i - 1] === '\n'))
|
|
14
|
+
i--;
|
|
15
|
+
return s.slice(0, i);
|
|
16
|
+
}
|
|
17
|
+
function normalizeEscapeStringLiterals(sql) {
|
|
18
|
+
// pgsql-deparser prefixes any string containing a backslash with PostgreSQL's
|
|
19
|
+
// E syntax and doubles each backslash. SQLite strings keep backslashes
|
|
20
|
+
// literally and reject the E prefix, so restore the AST value before emit.
|
|
21
|
+
return sql.replace(/\bE'((?:''|[^'])*)'/g, (_literal, body) => {
|
|
22
|
+
return `'${body.replaceAll('\\\\', '\\')}'`;
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
export class CompileError extends Error {
|
|
26
|
+
warnings;
|
|
27
|
+
sql;
|
|
28
|
+
constructor(warnings, sql) {
|
|
29
|
+
super(`pg-to-sqlite compile failed with ${warnings.length} warning(s)`);
|
|
30
|
+
this.warnings = warnings;
|
|
31
|
+
this.sql = sql;
|
|
32
|
+
this.name = 'CompileError';
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
function throwIfStrict(opts, warnings, sql) {
|
|
36
|
+
if (opts.strict && warnings.length > 0) {
|
|
37
|
+
throw new CompileError(warnings, sql);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Compile a single PG SQL statement into a SQLite-compatible statement.
|
|
42
|
+
*
|
|
43
|
+
* The compiler is best-effort: it applies the registered passes and emits via
|
|
44
|
+
* pgsql-deparser. Some PG-isms are not translatable; those produce warnings
|
|
45
|
+
* but the SQL is still emitted (caller can decide to reject or run anyway).
|
|
46
|
+
*/
|
|
47
|
+
export function compile(pgSql, opts = {}) {
|
|
48
|
+
const schema = opts.schema ?? NOOP_SCHEMA;
|
|
49
|
+
const version = opts.pgVersion ?? DEFAULT_VERSION;
|
|
50
|
+
const passes = opts.passes;
|
|
51
|
+
const warnings = [];
|
|
52
|
+
const trimmed = stripTrailingSemicolon(pgSql.trim());
|
|
53
|
+
if (!trimmed)
|
|
54
|
+
return { sql: '', warnings };
|
|
55
|
+
// parseSync returns a ParseResult: { version, stmts: [{ stmt: {...} }, ...] }
|
|
56
|
+
const parsed = parseSync(trimmed);
|
|
57
|
+
const stmts = Array.isArray(parsed?.stmts) ? parsed.stmts : [];
|
|
58
|
+
if (stmts.length === 0) {
|
|
59
|
+
const result = {
|
|
60
|
+
sql: trimmed,
|
|
61
|
+
warnings: [{ kind: 'parse-empty', message: 'no statements parsed' }],
|
|
62
|
+
};
|
|
63
|
+
throwIfStrict(opts, result.warnings, result.sql);
|
|
64
|
+
return result;
|
|
65
|
+
}
|
|
66
|
+
// Run all passes on each top-level RawStmt entry (so passes can walk from root).
|
|
67
|
+
for (let i = 0; i < stmts.length; i++) {
|
|
68
|
+
runPasses(stmts[i], { schema, warnings, passes });
|
|
69
|
+
}
|
|
70
|
+
throwIfStrict(opts, warnings, trimmed);
|
|
71
|
+
const quotedByMarker = markSQLiteKeywordIdentifiers(stmts);
|
|
72
|
+
const emitted = normalizeEscapeStringLiterals(restoreSQLiteKeywordIdentifierMarkers(deparseSync({ version: parsed.version ?? version, stmts }), quotedByMarker));
|
|
73
|
+
const sql = stripTrailingSemicolon(emitted.trim());
|
|
74
|
+
throwIfStrict(opts, warnings, sql);
|
|
75
|
+
return { sql, warnings };
|
|
76
|
+
}
|
|
77
|
+
export function compileMany(pgSqls, opts = {}) {
|
|
78
|
+
return pgSqls.map((s) => compile(s, opts));
|
|
79
|
+
}
|
|
80
|
+
//# sourceMappingURL=compiler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compiler.js","sourceRoot":"","sources":["../../src/pg-sqlite-compiler/compiler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,cAAc,CAAA;AAErD,OAAO,EACL,4BAA4B,EAC5B,qCAAqC,GACtC,MAAM,kCAAkC,CAAA;AACzC,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAA;AAI7C,MAAM,eAAe,GAAG,MAAM,CAAA;AAE9B,MAAM,WAAW,GAAe;IAC9B,aAAa,EAAE,GAAG,EAAE,CAAC,SAAS;IAC9B,OAAO,EAAE,GAAG,EAAE,CAAC,SAAS;IACxB,WAAW,EAAE,GAAG,EAAE,CAAC,KAAK;IACxB,eAAe,EAAE,GAAG,EAAE,CAAC,SAAS;CACjC,CAAA;AAED,SAAS,sBAAsB,CAAC,CAAS;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O,GAAG,6BAA6B,CAC3C,qCAAqC,CACnC,WAAW,CAAC,EAAE,OAAO,EAAE,MAAM,CAAC,OAAO,IAAI,OAAO,EAAE,KAAK,EAAS,CAAC,EACjE,cAAc,CACf,CACF,CAAA;IACD,MAAM,GAAG,GAAG,sBAAsB,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC,CAAA;IAClD,aAAa,CAAC,IAAI,EAAE,QAAQ,EAAE,GAAG,CAAC,CAAA;IAClC,OAAO,EAAE,GAAG,EAAE,QAAQ,EAAE,CAAA;AAC1B,CAAC;AAED,MAAM,UAAU,WAAW,CACzB,MAAgB,EAChB,OAAuB,EAAE;IAEzB,OAAO,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,OAAO,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,CAAA;AAC5C,CAAC"}
|
|
@@ -1,17 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
export declare class CompileError extends Error {
|
|
3
|
-
readonly warnings: CompileResult['warnings'];
|
|
4
|
-
readonly sql: string;
|
|
5
|
-
constructor(warnings: CompileResult['warnings'], sql: string);
|
|
6
|
-
}
|
|
7
|
-
/**
|
|
8
|
-
* Compile a single PG SQL statement into a SQLite-compatible statement.
|
|
9
|
-
*
|
|
10
|
-
* The compiler is best-effort: it applies the registered passes and emits via
|
|
11
|
-
* pgsql-deparser. Some PG-isms are not translatable; those produce warnings
|
|
12
|
-
* but the SQL is still emitted (caller can decide to reject or run anyway).
|
|
13
|
-
*/
|
|
14
|
-
export declare function compile(pgSql: string, opts?: CompileOptions): CompileResult;
|
|
15
|
-
export declare function compileMany(pgSqls: string[], opts?: CompileOptions): CompileResult[];
|
|
16
|
-
export type { CompileOptions, CompileResult, CompileWarning, SchemaInfo, } from './types.js';
|
|
1
|
+
export * from './compiler.js';
|
|
17
2
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/pg-sqlite-compiler/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/pg-sqlite-compiler/index.ts"],"names":[],"mappings":"AAaA,cAAc,eAAe,CAAA"}
|
|
@@ -7,84 +7,7 @@
|
|
|
7
7
|
* compile(pgSql, opts?) → { sql, warnings }
|
|
8
8
|
* compileMany(pgSqls, opts?) → results[]
|
|
9
9
|
*/
|
|
10
|
-
import {
|
|
11
|
-
import { markSQLiteKeywordIdentifiers, restoreSQLiteKeywordIdentifierMarkers, } from '../sqlite-keyword-identifiers.js';
|
|
12
|
-
import { runPasses } from './passes/index.js';
|
|
10
|
+
import { loadModule } from 'pgsql-parser';
|
|
13
11
|
await loadModule();
|
|
14
|
-
|
|
15
|
-
const NOOP_SCHEMA = {
|
|
16
|
-
getColumnType: () => undefined,
|
|
17
|
-
getEnum: () => undefined,
|
|
18
|
-
isEnumValue: () => false,
|
|
19
|
-
getTableColumns: () => undefined,
|
|
20
|
-
};
|
|
21
|
-
function stripTrailingSemicolon(s) {
|
|
22
|
-
let i = s.length;
|
|
23
|
-
while (i > 0 && (s[i - 1] === ';' || s[i - 1] === ' ' || s[i - 1] === '\n'))
|
|
24
|
-
i--;
|
|
25
|
-
return s.slice(0, i);
|
|
26
|
-
}
|
|
27
|
-
function normalizeEscapeStringLiterals(sql) {
|
|
28
|
-
// pgsql-deparser prefixes any string containing a backslash with PostgreSQL's
|
|
29
|
-
// E syntax and doubles each backslash. SQLite strings keep backslashes
|
|
30
|
-
// literally and reject the E prefix, so restore the AST value before emit.
|
|
31
|
-
return sql.replace(/\bE'((?:''|[^'])*)'/g, (_literal, body) => {
|
|
32
|
-
return `'${body.replaceAll('\\\\', '\\')}'`;
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
|
-
export class CompileError extends Error {
|
|
36
|
-
warnings;
|
|
37
|
-
sql;
|
|
38
|
-
constructor(warnings, sql) {
|
|
39
|
-
super(`pg-to-sqlite compile failed with ${warnings.length} warning(s)`);
|
|
40
|
-
this.warnings = warnings;
|
|
41
|
-
this.sql = sql;
|
|
42
|
-
this.name = 'CompileError';
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
function throwIfStrict(opts, warnings, sql) {
|
|
46
|
-
if (opts.strict && warnings.length > 0) {
|
|
47
|
-
throw new CompileError(warnings, sql);
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* Compile a single PG SQL statement into a SQLite-compatible statement.
|
|
52
|
-
*
|
|
53
|
-
* The compiler is best-effort: it applies the registered passes and emits via
|
|
54
|
-
* pgsql-deparser. Some PG-isms are not translatable; those produce warnings
|
|
55
|
-
* but the SQL is still emitted (caller can decide to reject or run anyway).
|
|
56
|
-
*/
|
|
57
|
-
export function compile(pgSql, opts = {}) {
|
|
58
|
-
const schema = opts.schema ?? NOOP_SCHEMA;
|
|
59
|
-
const version = opts.pgVersion ?? DEFAULT_VERSION;
|
|
60
|
-
const passes = opts.passes;
|
|
61
|
-
const warnings = [];
|
|
62
|
-
const trimmed = stripTrailingSemicolon(pgSql.trim());
|
|
63
|
-
if (!trimmed)
|
|
64
|
-
return { sql: '', warnings };
|
|
65
|
-
// parseSync returns a ParseResult: { version, stmts: [{ stmt: {...} }, ...] }
|
|
66
|
-
const parsed = parseSync(trimmed);
|
|
67
|
-
const stmts = Array.isArray(parsed?.stmts) ? parsed.stmts : [];
|
|
68
|
-
if (stmts.length === 0) {
|
|
69
|
-
const result = {
|
|
70
|
-
sql: trimmed,
|
|
71
|
-
warnings: [{ kind: 'parse-empty', message: 'no statements parsed' }],
|
|
72
|
-
};
|
|
73
|
-
throwIfStrict(opts, result.warnings, result.sql);
|
|
74
|
-
return result;
|
|
75
|
-
}
|
|
76
|
-
// Run all passes on each top-level RawStmt entry (so passes can walk from root).
|
|
77
|
-
for (let i = 0; i < stmts.length; i++) {
|
|
78
|
-
runPasses(stmts[i], { schema, warnings, passes });
|
|
79
|
-
}
|
|
80
|
-
throwIfStrict(opts, warnings, trimmed);
|
|
81
|
-
const quotedByMarker = markSQLiteKeywordIdentifiers(stmts);
|
|
82
|
-
const emitted = normalizeEscapeStringLiterals(restoreSQLiteKeywordIdentifierMarkers(deparseSync({ version: parsed.version ?? version, stmts }), quotedByMarker));
|
|
83
|
-
const sql = stripTrailingSemicolon(emitted.trim());
|
|
84
|
-
throwIfStrict(opts, warnings, sql);
|
|
85
|
-
return { sql, warnings };
|
|
86
|
-
}
|
|
87
|
-
export function compileMany(pgSqls, opts = {}) {
|
|
88
|
-
return pgSqls.map((s) => compile(s, opts));
|
|
89
|
-
}
|
|
12
|
+
export * from './compiler.js';
|
|
90
13
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/pg-sqlite-compiler/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/pg-sqlite-compiler/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAA;AAEzC,MAAM,UAAU,EAAE,CAAA;AAElB,cAAc,eAAe,CAAA"}
|