@aaqu/fromcubes-db 0.0.0-stage → 0.1.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/LICENSE +201 -0
- package/NOTICE +24 -0
- package/README.md +442 -2
- package/SECURITY.md +75 -0
- package/THIRD-PARTY-NOTICES.md +151 -0
- package/examples/Quick Start/All Databases.json +1719 -0
- package/examples/Quick Start/ClickHouse.json +278 -0
- package/examples/Quick Start/CockroachDB.json +280 -0
- package/examples/Quick Start/Firebird.json +278 -0
- package/examples/Quick Start/MariaDB.json +281 -0
- package/examples/Quick Start/MySQL.json +281 -0
- package/examples/Quick Start/Oracle.json +280 -0
- package/examples/Quick Start/PostgreSQL.json +280 -0
- package/examples/Quick Start/SQL Server.json +282 -0
- package/examples/Quick Start/SQLite.json +278 -0
- package/examples/Quick Start/TiDB.json +281 -0
- package/examples/Quick Start/YugabyteDB.json +280 -0
- package/examples/SQLite Sensor Log With Portal.json +391 -0
- package/examples/Select With Parameters.json +130 -0
- package/index.js +32 -0
- package/lib/adapters/clickhouse.js +254 -0
- package/lib/adapters/firebird.js +416 -0
- package/lib/adapters/index.js +37 -0
- package/lib/adapters/mssql.js +178 -0
- package/lib/adapters/mysql.js +171 -0
- package/lib/adapters/oracle.js +272 -0
- package/lib/adapters/postgres.js +161 -0
- package/lib/adapters/read.js +57 -0
- package/lib/adapters/sqlite.js +193 -0
- package/lib/adapters/time.js +89 -0
- package/lib/bound.js +29 -0
- package/lib/editor-api.js +134 -0
- package/lib/editor-preview.js +191 -0
- package/lib/errors.js +66 -0
- package/lib/params.js +966 -0
- package/lib/pool.js +391 -0
- package/lib/settings.js +302 -0
- package/lib/sql/compile.js +160 -0
- package/lib/sql/dialect.js +194 -0
- package/lib/sql/parse.js +202 -0
- package/lib/sql/types.js +1071 -0
- package/lib/sqlite-files.js +137 -0
- package/nodes/connection.html +728 -0
- package/nodes/connection.js +179 -0
- package/nodes/editor/ark-pixel.LICENSE.txt +96 -0
- package/nodes/editor/ark-pixel.woff2 +0 -0
- package/nodes/editor/mark.svg +5 -0
- package/nodes/editor/sql-editor.css +604 -0
- package/nodes/editor/sql-editor.js +1490 -0
- package/nodes/editor/sql-model.js +203 -0
- package/nodes/icons/fromcubes-db.svg +24 -0
- package/nodes/query.html +662 -0
- package/nodes/query.js +195 -0
- package/package.json +81 -4
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const { ParameterError } = require("../errors");
|
|
4
|
+
const { parseSql } = require("./parse");
|
|
5
|
+
const { byDatabase } = require("./dialect");
|
|
6
|
+
|
|
7
|
+
// All three tables are keyed by engine, and looked up through byDatabase, so a
|
|
8
|
+
// product that ever needs its own binding rule can be added by id without
|
|
9
|
+
// touching the code below.
|
|
10
|
+
|
|
11
|
+
const BINDING = {
|
|
12
|
+
postgres: "positional",
|
|
13
|
+
mysql: "positional",
|
|
14
|
+
sqlite: "positional",
|
|
15
|
+
mssql: "named",
|
|
16
|
+
oracle: "named",
|
|
17
|
+
firebird: "positional",
|
|
18
|
+
clickhouse: "named",
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/** Whether one bound value can be referenced by several placeholders. */
|
|
22
|
+
const REUSABLE = {
|
|
23
|
+
postgres: true,
|
|
24
|
+
mysql: false, // `?` is strictly positional
|
|
25
|
+
sqlite: false, // same
|
|
26
|
+
mssql: true,
|
|
27
|
+
oracle: true,
|
|
28
|
+
firebird: false, // `?` is strictly positional
|
|
29
|
+
clickhouse: true,
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/** How a bound value is spelled: index for positional, key for named. */
|
|
33
|
+
const PLACEHOLDER = {
|
|
34
|
+
postgres: (index) => `$${index}`,
|
|
35
|
+
mysql: () => "?",
|
|
36
|
+
sqlite: () => "?",
|
|
37
|
+
mssql: (key) => `@${key}`,
|
|
38
|
+
oracle: (key) => `:${key}`,
|
|
39
|
+
firebird: () => "?",
|
|
40
|
+
// One fixed type for every value, so the text depends on the SQL alone: the
|
|
41
|
+
// server converts the string to the column's type. Never the declared type —
|
|
42
|
+
// that is a check, not SQL.
|
|
43
|
+
clickhouse: (key) => `{${key}:Nullable(String)}`,
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Turns SQL with `:name` placeholders into what the driver expects, binding
|
|
48
|
+
* every value as a parameter. The emitted text depends only on the SQL, never
|
|
49
|
+
* on the values.
|
|
50
|
+
*
|
|
51
|
+
* @param {string|import('./parse').ParsedSql} sql
|
|
52
|
+
* @param {Record<string, unknown>} values
|
|
53
|
+
* @param {string} dialect
|
|
54
|
+
* @param {{ undefinedAsNull?: boolean }} [options]
|
|
55
|
+
* @returns {{ dialect: string, binding: 'positional'|'named', text: string, values: unknown[]|Record<string, unknown> }}
|
|
56
|
+
*/
|
|
57
|
+
function compile(sql, values, dialect, options = {}) {
|
|
58
|
+
const parsed = typeof sql === "string" ? parseSql(sql, dialect) : sql;
|
|
59
|
+
if (parsed.dialect !== dialect) {
|
|
60
|
+
throw new ParameterError(`SQL was parsed for "${parsed.dialect}" but compiled for "${dialect}"`, { dialect });
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
assertAllPresent(parsed, values, dialect, options);
|
|
64
|
+
|
|
65
|
+
const binding = byDatabase(BINDING, dialect);
|
|
66
|
+
const reusable = byDatabase(REUSABLE, dialect);
|
|
67
|
+
const spell = byDatabase(PLACEHOLDER, dialect);
|
|
68
|
+
if (!binding || !spell) throw new ParameterError(`Unknown dialect: ${dialect}`, { dialect });
|
|
69
|
+
|
|
70
|
+
const positional = [];
|
|
71
|
+
const named = {};
|
|
72
|
+
/** name -> placeholder text already emitted, for dialects that allow reuse */
|
|
73
|
+
const emitted = new Map();
|
|
74
|
+
|
|
75
|
+
let text = "";
|
|
76
|
+
|
|
77
|
+
const bind = (value) => {
|
|
78
|
+
if (binding === "positional") {
|
|
79
|
+
positional.push(value);
|
|
80
|
+
return spell(positional.length);
|
|
81
|
+
}
|
|
82
|
+
const key = `p${Object.keys(named).length + 1}`;
|
|
83
|
+
named[key] = value;
|
|
84
|
+
return spell(key);
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
for (const token of parsed.tokens) {
|
|
88
|
+
if (token.kind === "text") {
|
|
89
|
+
text += token.text;
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const cached = reusable ? emitted.get(token.name) : undefined;
|
|
94
|
+
if (cached !== undefined) {
|
|
95
|
+
text += cached;
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const raw = values[token.name];
|
|
100
|
+
const value = raw === undefined ? null : raw;
|
|
101
|
+
|
|
102
|
+
let placeholder;
|
|
103
|
+
if (token.spread) {
|
|
104
|
+
const list = Array.isArray(value) ? value : [];
|
|
105
|
+
// `IN ()` is a syntax error everywhere, and an empty list should match
|
|
106
|
+
// nothing. NULL is a constant, so this stays injection-free.
|
|
107
|
+
placeholder = list.length === 0 ? "NULL" : list.map(bind).join(", ");
|
|
108
|
+
} else {
|
|
109
|
+
placeholder = bind(value);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (reusable) emitted.set(token.name, placeholder);
|
|
113
|
+
text += placeholder;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return binding === "positional"
|
|
117
|
+
? { dialect, binding, text, values: positional }
|
|
118
|
+
: { dialect, binding, text, values: named };
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function assertAllPresent(parsed, values, dialect, options) {
|
|
122
|
+
const missing = [];
|
|
123
|
+
const notArray = [];
|
|
124
|
+
|
|
125
|
+
for (const name of parsed.names) {
|
|
126
|
+
const present = Object.prototype.hasOwnProperty.call(values, name);
|
|
127
|
+
const value = values[name];
|
|
128
|
+
|
|
129
|
+
if (!present || value === undefined) {
|
|
130
|
+
if (options.undefinedAsNull !== true) missing.push(name);
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
if (parsed.spreadNames.has(name) && !Array.isArray(value)) {
|
|
134
|
+
notArray.push(name);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (missing.length > 0) {
|
|
139
|
+
throw new ParameterError(
|
|
140
|
+
`Missing value for parameter${missing.length > 1 ? "s" : ""}: ${missing.map((n) => `:${n}`).join(", ")}`,
|
|
141
|
+
{ dialect },
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
if (notArray.length > 0) {
|
|
145
|
+
throw new ParameterError(
|
|
146
|
+
`Spread parameter${notArray.length > 1 ? "s" : ""} ${notArray
|
|
147
|
+
.map((n) => `:...${n}`)
|
|
148
|
+
.join(", ")} must be given an array`,
|
|
149
|
+
{ dialect },
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** Supplied values that no placeholder references — used for node warnings. */
|
|
155
|
+
function unusedValueKeys(parsed, values) {
|
|
156
|
+
const used = new Set(parsed.names);
|
|
157
|
+
return Object.keys(values).filter((k) => !used.has(k));
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
module.exports = { compile, unusedValueKeys };
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Engines: the SQL behaviours this package actually implements — lexis,
|
|
5
|
+
* placeholder binding, driver, settings builder.
|
|
6
|
+
*/
|
|
7
|
+
const DIALECTS = ["postgres", "mssql", "oracle", "mysql", "sqlite", "firebird", "clickhouse"];
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Products: what the user picks in the dialog. Each one is its own choice with
|
|
11
|
+
* its own name, port and saved value, even where two of them currently run on
|
|
12
|
+
* the same engine and the same npm driver — MariaDB is not MySQL, and
|
|
13
|
+
* CockroachDB is not PostgreSQL, they merely behave alike today.
|
|
14
|
+
*
|
|
15
|
+
* Keeping them apart is what leaves the door open: the day MariaDB needs its
|
|
16
|
+
* own settings builder, adapter or binding rule, it gets an entry keyed by
|
|
17
|
+
* `mariadb` in that table and nothing else has to move. Merging them into one
|
|
18
|
+
* option would mean unpicking the choice from every saved flow first.
|
|
19
|
+
*
|
|
20
|
+
* This table is the only list of products. The edit dialog builds its picker
|
|
21
|
+
* from it over `GET fromcubes-db/products` rather than holding a second copy.
|
|
22
|
+
*
|
|
23
|
+
* @typedef {Object} Product
|
|
24
|
+
* @property {string} engine which entry in DIALECTS drives it
|
|
25
|
+
* @property {string} label shown in the picker and above query nodes
|
|
26
|
+
* @property {number} [defaultPort]
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
/** @type {Record<string, Product>} */
|
|
30
|
+
const PRODUCTS = {
|
|
31
|
+
postgres: { engine: "postgres", label: "PostgreSQL", defaultPort: 5432 },
|
|
32
|
+
cockroachdb: { engine: "postgres", label: "CockroachDB", defaultPort: 26257 },
|
|
33
|
+
yugabytedb: { engine: "postgres", label: "YugabyteDB", defaultPort: 5433 },
|
|
34
|
+
mysql: { engine: "mysql", label: "MySQL", defaultPort: 3306 },
|
|
35
|
+
mariadb: { engine: "mysql", label: "MariaDB", defaultPort: 3306 },
|
|
36
|
+
tidb: { engine: "mysql", label: "TiDB", defaultPort: 4000 },
|
|
37
|
+
mssql: { engine: "mssql", label: "SQL Server", defaultPort: 1433 },
|
|
38
|
+
oracle: { engine: "oracle", label: "Oracle", defaultPort: 1521 },
|
|
39
|
+
sqlite: { engine: "sqlite", label: "SQLite" },
|
|
40
|
+
firebird: { engine: "firebird", label: "Firebird", defaultPort: 3050 },
|
|
41
|
+
clickhouse: { engine: "clickhouse", label: "ClickHouse", defaultPort: 8123 },
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const PRODUCT_IDS = Object.keys(PRODUCTS);
|
|
45
|
+
|
|
46
|
+
function isProduct(value) {
|
|
47
|
+
return typeof value === "string" && Object.prototype.hasOwnProperty.call(PRODUCTS, value);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function isDialect(value) {
|
|
51
|
+
return typeof value === "string" && DIALECTS.includes(value);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* The engine behind a product. Engine ids pass through unchanged, so every
|
|
56
|
+
* lookup below accepts either and a caller never has to know which it holds.
|
|
57
|
+
*/
|
|
58
|
+
function engineFor(value) {
|
|
59
|
+
if (isProduct(value)) return PRODUCTS[value].engine;
|
|
60
|
+
if (isDialect(value)) return value;
|
|
61
|
+
throw new Error(`Unknown database: ${value}`);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Looks `value` up in a per-database table, preferring an entry keyed by the
|
|
66
|
+
* product over the one keyed by its engine. That preference is the escape
|
|
67
|
+
* hatch: give `mariadb` its own entry and it stops sharing MySQL's.
|
|
68
|
+
*/
|
|
69
|
+
function byDatabase(table, value) {
|
|
70
|
+
if (Object.prototype.hasOwnProperty.call(table, value)) return table[value];
|
|
71
|
+
// Deliberately does not throw on an unknown value: every caller already has a
|
|
72
|
+
// better error to give than this helper would.
|
|
73
|
+
const engine = isProduct(value) ? PRODUCTS[value].engine : value;
|
|
74
|
+
return Object.prototype.hasOwnProperty.call(table, engine) ? table[engine] : undefined;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Lexical rules that decide where a placeholder is *not* a placeholder. Getting
|
|
79
|
+
* these wrong is how `created::text` (Postgres cast) or `[a:b]` (MSSQL
|
|
80
|
+
* identifier) turn into bogus bind variables.
|
|
81
|
+
*
|
|
82
|
+
* @typedef {Object} DialectLexis
|
|
83
|
+
* @property {boolean} bracketQuotedIdentifiers `[ident]`
|
|
84
|
+
* @property {boolean} backtickQuotedIdentifiers `` `ident` ``
|
|
85
|
+
* @property {boolean} dollarQuotedStrings `$tag$ ... $tag$`
|
|
86
|
+
* @property {boolean} backslashEscapes `\'` inside string literals
|
|
87
|
+
* @property {boolean} hashLineComments `#` starts a comment
|
|
88
|
+
* @property {boolean} nestedBlockComments block comments nest
|
|
89
|
+
* @property {boolean} doubleColonCast `::` is an operator (a cast, a T-SQL
|
|
90
|
+
* static call), never a placeholder
|
|
91
|
+
* @property {[string, string]} identifierQuote
|
|
92
|
+
*/
|
|
93
|
+
|
|
94
|
+
/** @type {Record<string, DialectLexis>} */
|
|
95
|
+
const LEXIS = {
|
|
96
|
+
postgres: {
|
|
97
|
+
bracketQuotedIdentifiers: false,
|
|
98
|
+
backtickQuotedIdentifiers: false,
|
|
99
|
+
dollarQuotedStrings: true,
|
|
100
|
+
backslashEscapes: false,
|
|
101
|
+
hashLineComments: false,
|
|
102
|
+
nestedBlockComments: true,
|
|
103
|
+
doubleColonCast: true,
|
|
104
|
+
identifierQuote: ['"', '"'],
|
|
105
|
+
},
|
|
106
|
+
mssql: {
|
|
107
|
+
bracketQuotedIdentifiers: true,
|
|
108
|
+
backtickQuotedIdentifiers: false,
|
|
109
|
+
dollarQuotedStrings: false,
|
|
110
|
+
backslashEscapes: false,
|
|
111
|
+
hashLineComments: false,
|
|
112
|
+
nestedBlockComments: true,
|
|
113
|
+
// Not a cast here, but `::` is just as much an operator: a static method
|
|
114
|
+
// call (geography::STGeomFromText) or a scope (GRANT ... ON SCHEMA::dbo).
|
|
115
|
+
// Read as a placeholder it became a parameter named STGeomFromText.
|
|
116
|
+
doubleColonCast: true,
|
|
117
|
+
identifierQuote: ["[", "]"],
|
|
118
|
+
},
|
|
119
|
+
oracle: {
|
|
120
|
+
bracketQuotedIdentifiers: false,
|
|
121
|
+
backtickQuotedIdentifiers: false,
|
|
122
|
+
dollarQuotedStrings: false,
|
|
123
|
+
backslashEscapes: false,
|
|
124
|
+
hashLineComments: false,
|
|
125
|
+
nestedBlockComments: false,
|
|
126
|
+
doubleColonCast: false,
|
|
127
|
+
identifierQuote: ['"', '"'],
|
|
128
|
+
},
|
|
129
|
+
mysql: {
|
|
130
|
+
bracketQuotedIdentifiers: false,
|
|
131
|
+
backtickQuotedIdentifiers: true,
|
|
132
|
+
dollarQuotedStrings: false,
|
|
133
|
+
backslashEscapes: true,
|
|
134
|
+
hashLineComments: true,
|
|
135
|
+
nestedBlockComments: false,
|
|
136
|
+
doubleColonCast: false,
|
|
137
|
+
identifierQuote: ["`", "`"],
|
|
138
|
+
},
|
|
139
|
+
sqlite: {
|
|
140
|
+
// SQLite accepts [brackets] and `backticks` as identifier quotes for
|
|
141
|
+
// compatibility with SQL Server and MySQL.
|
|
142
|
+
bracketQuotedIdentifiers: true,
|
|
143
|
+
backtickQuotedIdentifiers: true,
|
|
144
|
+
dollarQuotedStrings: false,
|
|
145
|
+
backslashEscapes: false,
|
|
146
|
+
hashLineComments: false,
|
|
147
|
+
nestedBlockComments: false,
|
|
148
|
+
doubleColonCast: false,
|
|
149
|
+
identifierQuote: ['"', '"'],
|
|
150
|
+
},
|
|
151
|
+
// Firebird: SQL-standard quoting, `--` and non-nesting block comments, no
|
|
152
|
+
// backslash escapes, no `::`. `:name` exists only inside PSQL bodies, which
|
|
153
|
+
// a DSQL statement from this node is not.
|
|
154
|
+
firebird: {
|
|
155
|
+
bracketQuotedIdentifiers: false,
|
|
156
|
+
backtickQuotedIdentifiers: false,
|
|
157
|
+
dollarQuotedStrings: false,
|
|
158
|
+
backslashEscapes: false,
|
|
159
|
+
hashLineComments: false,
|
|
160
|
+
nestedBlockComments: false,
|
|
161
|
+
doubleColonCast: false,
|
|
162
|
+
identifierQuote: ['"', '"'],
|
|
163
|
+
},
|
|
164
|
+
// ClickHouse: "double" or `backtick` identifiers, 'strings' with backslash
|
|
165
|
+
// escapes, `--` and `#` line comments, nesting block comments (measured:
|
|
166
|
+
// `/* a /* b */ c */` is one comment), $$heredoc$$ strings and `::` casts.
|
|
167
|
+
clickhouse: {
|
|
168
|
+
bracketQuotedIdentifiers: false,
|
|
169
|
+
backtickQuotedIdentifiers: true,
|
|
170
|
+
dollarQuotedStrings: true,
|
|
171
|
+
backslashEscapes: true,
|
|
172
|
+
hashLineComments: true,
|
|
173
|
+
nestedBlockComments: true,
|
|
174
|
+
doubleColonCast: true,
|
|
175
|
+
identifierQuote: ["`", "`"],
|
|
176
|
+
},
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
function lexisFor(dialect) {
|
|
180
|
+
const lexis = byDatabase(LEXIS, dialect);
|
|
181
|
+
if (!lexis) throw new Error(`Unknown dialect: ${dialect}`);
|
|
182
|
+
return lexis;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
module.exports = {
|
|
186
|
+
DIALECTS,
|
|
187
|
+
PRODUCTS,
|
|
188
|
+
PRODUCT_IDS,
|
|
189
|
+
isDialect,
|
|
190
|
+
isProduct,
|
|
191
|
+
engineFor,
|
|
192
|
+
byDatabase,
|
|
193
|
+
lexisFor,
|
|
194
|
+
};
|
package/lib/sql/parse.js
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const { SqlSyntaxError } = require("../errors");
|
|
4
|
+
const { lexisFor } = require("./dialect");
|
|
5
|
+
|
|
6
|
+
const IDENT_START = /[A-Za-z_]/;
|
|
7
|
+
const IDENT_PART = /[A-Za-z0-9_$]/;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @typedef {{ kind: 'text', text: string } | { kind: 'param', name: string, spread: boolean }} SqlToken
|
|
11
|
+
* @typedef {{ dialect: string, tokens: SqlToken[], names: string[], spreadNames: Set<string> }} ParsedSql
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Splits SQL into literal text and named placeholders.
|
|
16
|
+
*
|
|
17
|
+
* `:name` binds one value.
|
|
18
|
+
* `:...name` expands an array into a value list, for `IN (...)`. Spelled out
|
|
19
|
+
* explicitly so that binding a real array column (Postgres
|
|
20
|
+
* `text[]`) stays possible with plain `:name`.
|
|
21
|
+
*
|
|
22
|
+
* Placeholders inside string literals, quoted identifiers and comments are left
|
|
23
|
+
* alone — there they are data or names, not parameters.
|
|
24
|
+
*
|
|
25
|
+
* @param {string} sql
|
|
26
|
+
* @param {string} dialect
|
|
27
|
+
* @returns {ParsedSql}
|
|
28
|
+
*/
|
|
29
|
+
function parseSql(sql, dialect) {
|
|
30
|
+
if (typeof sql !== "string") {
|
|
31
|
+
throw new SqlSyntaxError("SQL must be a string", { dialect });
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const lex = lexisFor(dialect);
|
|
35
|
+
/** @type {SqlToken[]} */
|
|
36
|
+
const tokens = [];
|
|
37
|
+
const names = [];
|
|
38
|
+
const spreadNames = new Set();
|
|
39
|
+
|
|
40
|
+
let text = "";
|
|
41
|
+
let i = 0;
|
|
42
|
+
|
|
43
|
+
const flush = () => {
|
|
44
|
+
if (text !== "") {
|
|
45
|
+
tokens.push({ kind: "text", text });
|
|
46
|
+
text = "";
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
while (i < sql.length) {
|
|
51
|
+
const ch = sql[i];
|
|
52
|
+
const next = sql[i + 1];
|
|
53
|
+
|
|
54
|
+
// --- comments -------------------------------------------------------
|
|
55
|
+
if ((ch === "-" && next === "-") || (lex.hashLineComments && ch === "#")) {
|
|
56
|
+
const end = sql.indexOf("\n", i);
|
|
57
|
+
const stop = end === -1 ? sql.length : end;
|
|
58
|
+
text += sql.slice(i, stop);
|
|
59
|
+
i = stop;
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (ch === "/" && next === "*") {
|
|
64
|
+
const start = i;
|
|
65
|
+
let depth = 0;
|
|
66
|
+
while (i < sql.length) {
|
|
67
|
+
// A second `/*` opens a level only where comments nest; elsewhere it
|
|
68
|
+
// is text inside the comment, and the first `*/` closes it.
|
|
69
|
+
if (sql[i] === "/" && sql[i + 1] === "*" && (depth === 0 || lex.nestedBlockComments)) {
|
|
70
|
+
depth++;
|
|
71
|
+
i += 2;
|
|
72
|
+
} else if (sql[i] === "*" && sql[i + 1] === "/") {
|
|
73
|
+
depth--;
|
|
74
|
+
i += 2;
|
|
75
|
+
if (depth === 0) break;
|
|
76
|
+
if (!lex.nestedBlockComments) break;
|
|
77
|
+
} else {
|
|
78
|
+
i++;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if (depth > 0) throw new SqlSyntaxError("Unterminated block comment in SQL", { dialect });
|
|
82
|
+
text += sql.slice(start, i);
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// --- string literals ------------------------------------------------
|
|
87
|
+
if (ch === "'") {
|
|
88
|
+
const start = i;
|
|
89
|
+
i++;
|
|
90
|
+
let closed = false;
|
|
91
|
+
while (i < sql.length) {
|
|
92
|
+
if (lex.backslashEscapes && sql[i] === "\\") {
|
|
93
|
+
i += 2;
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
if (sql[i] === "'") {
|
|
97
|
+
if (sql[i + 1] === "'") {
|
|
98
|
+
i += 2; // '' escape
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
i++;
|
|
102
|
+
closed = true;
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
i++;
|
|
106
|
+
}
|
|
107
|
+
if (!closed) throw new SqlSyntaxError("Unterminated string literal in SQL", { dialect });
|
|
108
|
+
text += sql.slice(start, i);
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (lex.dollarQuotedStrings && ch === "$") {
|
|
113
|
+
const tag = matchDollarTag(sql, i);
|
|
114
|
+
if (tag !== null) {
|
|
115
|
+
const close = sql.indexOf(tag, i + tag.length);
|
|
116
|
+
if (close === -1) {
|
|
117
|
+
throw new SqlSyntaxError(`Unterminated dollar-quoted string (${tag}) in SQL`, {
|
|
118
|
+
dialect,
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
const stop = close + tag.length;
|
|
122
|
+
text += sql.slice(i, stop);
|
|
123
|
+
i = stop;
|
|
124
|
+
continue;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// --- quoted identifiers ---------------------------------------------
|
|
129
|
+
if (ch === '"' || (lex.backtickQuotedIdentifiers && ch === "`") || (lex.bracketQuotedIdentifiers && ch === "[")) {
|
|
130
|
+
const end = consumeDelimited(sql, i, ch === "[" ? "]" : ch, dialect);
|
|
131
|
+
text += sql.slice(i, end);
|
|
132
|
+
i = end;
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// --- placeholders ----------------------------------------------------
|
|
137
|
+
if (ch === ":") {
|
|
138
|
+
if (lex.doubleColonCast && next === ":") {
|
|
139
|
+
text += "::";
|
|
140
|
+
i += 2;
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const spread = sql.startsWith("...", i + 1);
|
|
145
|
+
const nameStart = i + 1 + (spread ? 3 : 0);
|
|
146
|
+
const first = sql[nameStart];
|
|
147
|
+
|
|
148
|
+
if (first !== undefined && IDENT_START.test(first)) {
|
|
149
|
+
let end = nameStart + 1;
|
|
150
|
+
while (end < sql.length && IDENT_PART.test(sql[end])) end++;
|
|
151
|
+
const name = sql.slice(nameStart, end);
|
|
152
|
+
|
|
153
|
+
flush();
|
|
154
|
+
tokens.push({ kind: "param", name, spread });
|
|
155
|
+
if (!names.includes(name)) names.push(name);
|
|
156
|
+
if (spread) spreadNames.add(name);
|
|
157
|
+
i = end;
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// A lone ':' (Oracle positional `:1`, `a ? b : c`) stays literal text.
|
|
162
|
+
text += ch;
|
|
163
|
+
i++;
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
text += ch;
|
|
168
|
+
i++;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
flush();
|
|
172
|
+
|
|
173
|
+
return { dialect, tokens, names, spreadNames };
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Scans a `$tag$` opener and returns it, or null when `$` is just an identifier
|
|
178
|
+
* character (`$1`, `my$col`).
|
|
179
|
+
*/
|
|
180
|
+
function matchDollarTag(sql, from) {
|
|
181
|
+
let i = from + 1;
|
|
182
|
+
while (i < sql.length && /[A-Za-z0-9_]/.test(sql[i])) i++;
|
|
183
|
+
return sql[i] === "$" ? sql.slice(from, i + 1) : null;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** Index just past the closing delimiter. A doubled close character escapes it. */
|
|
187
|
+
function consumeDelimited(sql, from, close, dialect) {
|
|
188
|
+
let i = from + 1;
|
|
189
|
+
while (i < sql.length) {
|
|
190
|
+
if (sql[i] === close) {
|
|
191
|
+
if (sql[i + 1] === close) {
|
|
192
|
+
i += 2;
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
return i + 1;
|
|
196
|
+
}
|
|
197
|
+
i++;
|
|
198
|
+
}
|
|
199
|
+
throw new SqlSyntaxError("Unterminated quoted identifier in SQL", { dialect });
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
module.exports = { parseSql };
|