@loaders.gl/sql 5.0.0-alpha.1 → 5.0.0-alpha.11
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 +14 -3
- package/README.md +107 -0
- package/dist/arrow-query.cjs +1404 -0
- package/dist/arrow-query.cjs.map +6 -0
- package/dist/arrow-query.d.ts +5 -0
- package/dist/arrow-query.d.ts.map +1 -0
- package/dist/arrow-query.js +6 -0
- package/dist/arrow-query.js.map +1 -0
- package/dist/arrow-table-source.d.ts +27 -0
- package/dist/arrow-table-source.d.ts.map +1 -0
- package/dist/arrow-table-source.js +79 -0
- package/dist/arrow-table-source.js.map +1 -0
- package/dist/bind-sql-predicate.d.ts +4 -0
- package/dist/bind-sql-predicate.d.ts.map +1 -0
- package/dist/bind-sql-predicate.js +11 -0
- package/dist/bind-sql-predicate.js.map +1 -0
- package/dist/compile-table-query.d.ts +55 -0
- package/dist/compile-table-query.d.ts.map +1 -0
- package/dist/compile-table-query.js +255 -0
- package/dist/compile-table-query.js.map +1 -0
- package/dist/duckdb-sql-source.d.ts +51 -3
- package/dist/duckdb-sql-source.d.ts.map +1 -1
- package/dist/duckdb-sql-source.js +21 -7
- package/dist/duckdb-sql-source.js.map +1 -1
- package/dist/index.cjs +3352 -37
- package/dist/index.cjs.map +3 -4
- package/dist/index.d.ts +16 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -1
- package/dist/parse-sql-predicate.d.ts +10 -0
- package/dist/parse-sql-predicate.d.ts.map +1 -0
- package/dist/parse-sql-predicate.js +322 -0
- package/dist/parse-sql-predicate.js.map +1 -0
- package/dist/query-arrow-table.d.ts +83 -0
- package/dist/query-arrow-table.d.ts.map +1 -0
- package/dist/query-arrow-table.js +663 -0
- package/dist/query-arrow-table.js.map +1 -0
- package/dist/snowflake-sql-source.d.ts +51 -3
- package/dist/snowflake-sql-source.d.ts.map +1 -1
- package/dist/snowflake-sql-source.js +9 -6
- package/dist/snowflake-sql-source.js.map +1 -1
- package/dist/sql-predicate-schema.d.ts +156 -0
- package/dist/sql-predicate-schema.d.ts.map +1 -0
- package/dist/sql-predicate-schema.js +226 -0
- package/dist/sql-predicate-schema.js.map +1 -0
- package/dist/sql-predicate-types.d.ts +45 -0
- package/dist/sql-predicate-types.d.ts.map +1 -0
- package/dist/sql-predicate-types.js +9 -0
- package/dist/sql-predicate-types.js.map +1 -0
- package/dist/sql-predicate-zod.cjs +54 -0
- package/dist/sql-predicate-zod.cjs.map +6 -0
- package/dist/sql-predicate-zod.d.ts +11 -0
- package/dist/sql-predicate-zod.d.ts.map +1 -0
- package/dist/sql-predicate-zod.js +45 -0
- package/dist/sql-predicate-zod.js.map +1 -0
- package/dist/sql-predicate.cjs +607 -0
- package/dist/sql-predicate.cjs.map +6 -0
- package/dist/sql-predicate.d.ts +6 -0
- package/dist/sql-predicate.d.ts.map +1 -0
- package/dist/sql-predicate.js +8 -0
- package/dist/sql-predicate.js.map +1 -0
- package/dist/sql-source.d.ts +16 -3
- package/dist/sql-source.d.ts.map +1 -1
- package/dist/sql-source.js +34 -5
- package/dist/sql-source.js.map +1 -1
- package/dist/sql-types.d.ts +3 -0
- package/dist/sql-types.d.ts.map +1 -1
- package/dist/table-query-capabilities.d.ts +6 -0
- package/dist/table-query-capabilities.d.ts.map +1 -0
- package/dist/table-query-capabilities.js +26 -0
- package/dist/table-query-capabilities.js.map +1 -0
- package/dist/table-query.cjs +286 -0
- package/dist/table-query.cjs.map +6 -0
- package/dist/table-query.d.ts +29 -0
- package/dist/table-query.d.ts.map +1 -0
- package/dist/table-query.js +30 -0
- package/dist/table-query.js.map +1 -0
- package/package.json +45 -8
- package/src/arrow-query.ts +17 -0
- package/src/arrow-table-source.ts +111 -0
- package/src/bind-sql-predicate.ts +21 -0
- package/src/compile-table-query.ts +354 -0
- package/src/duckdb-sql-source.ts +30 -6
- package/src/index.ts +57 -0
- package/src/parse-sql-predicate.ts +381 -0
- package/src/query-arrow-table.ts +846 -0
- package/src/snowflake-sql-source.ts +8 -5
- package/src/sql-predicate-schema.ts +251 -0
- package/src/sql-predicate-types.ts +87 -0
- package/src/sql-predicate-zod.ts +52 -0
- package/src/sql-predicate.ts +28 -0
- package/src/sql-source.ts +40 -5
- package/src/sql-types.ts +4 -0
- package/src/table-query-capabilities.ts +29 -0
- package/src/table-query.ts +78 -0
package/src/index.ts
CHANGED
|
@@ -3,8 +3,41 @@
|
|
|
3
3
|
// Copyright (c) vis.gl contributors
|
|
4
4
|
|
|
5
5
|
export {SQLDataSource, registerSQLAdapter, getSQLAdapterFactory} from './sql-source';
|
|
6
|
+
export {ArrowTableSource} from './arrow-table-source';
|
|
6
7
|
export {DuckDBSQLSource, DuckDBSQLDataSource} from './duckdb-sql-source';
|
|
7
8
|
export {SnowflakeSQLSource, SnowflakeSQLDataSource} from './snowflake-sql-source';
|
|
9
|
+
export {
|
|
10
|
+
ARROW_TABLE_QUERY_CAPABILITIES,
|
|
11
|
+
explainArrowTableQuery,
|
|
12
|
+
queryArrowTable
|
|
13
|
+
} from './query-arrow-table';
|
|
14
|
+
export {parseSQLPredicate} from './parse-sql-predicate';
|
|
15
|
+
export {bindSQLPredicate} from './bind-sql-predicate';
|
|
16
|
+
export {compileSQLTableQuery} from './compile-table-query';
|
|
17
|
+
export {explainTableQuery, getSQLPredicateColumnNames, planTableQuery} from './table-query';
|
|
18
|
+
export {SQL_DATA_SOURCE_TABLE_QUERY_CAPABILITIES} from './table-query-capabilities';
|
|
19
|
+
export {isSQLPredicateParameter} from './sql-predicate-types';
|
|
20
|
+
export {
|
|
21
|
+
isSQLPredicate,
|
|
22
|
+
SQL_PREDICATE_JSON_SCHEMA,
|
|
23
|
+
validateSQLPredicate
|
|
24
|
+
} from './sql-predicate-schema';
|
|
25
|
+
|
|
26
|
+
export type {
|
|
27
|
+
BoundSQLPredicate,
|
|
28
|
+
SQLComparisonPredicate,
|
|
29
|
+
SQLInPredicate,
|
|
30
|
+
SQLLogicalPredicate,
|
|
31
|
+
SQLNotPredicate,
|
|
32
|
+
SQLNullPredicate,
|
|
33
|
+
SQLPredicate,
|
|
34
|
+
SQLPredicateParameter,
|
|
35
|
+
SQLPredicateParameterValues,
|
|
36
|
+
SQLPredicateParserOptions,
|
|
37
|
+
SQLPredicateProperty,
|
|
38
|
+
SQLPredicateScalar,
|
|
39
|
+
SQLPredicateValue
|
|
40
|
+
} from './sql-predicate-types';
|
|
8
41
|
|
|
9
42
|
export type {
|
|
10
43
|
SQLAdapter,
|
|
@@ -15,8 +48,32 @@ export type {
|
|
|
15
48
|
SQLColumnInfo,
|
|
16
49
|
SQLMetadata,
|
|
17
50
|
SQLParameterValues,
|
|
51
|
+
SQLQuery,
|
|
18
52
|
SQLQueryOptions,
|
|
19
53
|
SQLSchemaInfo,
|
|
20
54
|
SQLSourceOptions,
|
|
21
55
|
SQLTableInfo
|
|
22
56
|
} from './sql-types';
|
|
57
|
+
|
|
58
|
+
export type {ArrowQueryOptions} from './query-arrow-table';
|
|
59
|
+
export type {ArrowTableSourceOptions} from './arrow-table-source';
|
|
60
|
+
export type {
|
|
61
|
+
CompiledSQLTableQuery,
|
|
62
|
+
SQLTableQuery,
|
|
63
|
+
SQLTableQueryCompilerOptions,
|
|
64
|
+
SQLTableQueryDialect
|
|
65
|
+
} from './compile-table-query';
|
|
66
|
+
export type {
|
|
67
|
+
RelationalAggregate,
|
|
68
|
+
RelationalExpression,
|
|
69
|
+
RelationalOrderKey
|
|
70
|
+
} from '@loaders.gl/loader-utils';
|
|
71
|
+
export type {
|
|
72
|
+
TableQueryFilterStep,
|
|
73
|
+
TableQueryLimitStep,
|
|
74
|
+
TableQueryOptions,
|
|
75
|
+
TableQueryPlan,
|
|
76
|
+
TableQueryPlanStep,
|
|
77
|
+
TableQueryProjectStep,
|
|
78
|
+
TableQueryScanStep
|
|
79
|
+
} from './table-query';
|
|
@@ -0,0 +1,381 @@
|
|
|
1
|
+
// loaders.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import type {
|
|
6
|
+
SQLLogicalPredicate,
|
|
7
|
+
SQLPredicate,
|
|
8
|
+
SQLPredicateParameterValues,
|
|
9
|
+
SQLPredicateParserOptions,
|
|
10
|
+
SQLPredicateProperty,
|
|
11
|
+
SQLPredicateValue
|
|
12
|
+
} from './sql-predicate-types';
|
|
13
|
+
import {validateSQLPredicate} from './sql-predicate-schema';
|
|
14
|
+
|
|
15
|
+
const MAXIMUM_SQL_LENGTH = 65_536;
|
|
16
|
+
const MAXIMUM_SQL_TOKENS = 4096;
|
|
17
|
+
const MAXIMUM_EXPRESSION_DEPTH = 64;
|
|
18
|
+
|
|
19
|
+
type SQLPredicateToken = Readonly<{
|
|
20
|
+
kind: 'identifier' | 'number' | 'parameter' | 'string' | 'symbol';
|
|
21
|
+
value: string;
|
|
22
|
+
quoted?: boolean;
|
|
23
|
+
}>;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Parses a small SQL `WHERE` expression into a portable CQL2-shaped predicate AST.
|
|
27
|
+
*
|
|
28
|
+
* Supported syntax is limited to comparisons, `IN`, `IS [NOT] NULL`, `AND`, `OR`, `NOT`, named
|
|
29
|
+
* parameters, scalar literals, and parentheses. The input is the expression after `WHERE`, not a
|
|
30
|
+
* complete `SELECT` statement.
|
|
31
|
+
*/
|
|
32
|
+
export function parseSQLPredicate(
|
|
33
|
+
source: string,
|
|
34
|
+
options: SQLPredicateParserOptions = {}
|
|
35
|
+
): SQLPredicate {
|
|
36
|
+
const parser = new SQLPredicateParser(
|
|
37
|
+
source,
|
|
38
|
+
options.parameters ?? {},
|
|
39
|
+
options.preserveParameters === true
|
|
40
|
+
);
|
|
41
|
+
const predicate = parser.parse();
|
|
42
|
+
validateSQLPredicate(predicate);
|
|
43
|
+
return predicate;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Dependency-free recursive-descent parser for one SQL predicate expression. */
|
|
47
|
+
class SQLPredicateParser {
|
|
48
|
+
/** Token stream parsed by this parser instance. */
|
|
49
|
+
private readonly tokens: readonly SQLPredicateToken[];
|
|
50
|
+
/** Named scalar parameters available to the expression. */
|
|
51
|
+
private readonly parameters: SQLPredicateParameterValues;
|
|
52
|
+
/** Whether named parameters remain references in the parsed AST. */
|
|
53
|
+
private readonly preserveParameters: boolean;
|
|
54
|
+
/** Current index in the token stream. */
|
|
55
|
+
private position = 0;
|
|
56
|
+
|
|
57
|
+
constructor(
|
|
58
|
+
source: string,
|
|
59
|
+
parameters: SQLPredicateParameterValues,
|
|
60
|
+
preserveParameters: boolean
|
|
61
|
+
) {
|
|
62
|
+
this.tokens = tokenizeSQLPredicate(source);
|
|
63
|
+
this.parameters = parameters;
|
|
64
|
+
this.preserveParameters = preserveParameters;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Parses the complete token stream. */
|
|
68
|
+
parse(): SQLPredicate {
|
|
69
|
+
const predicate = this.parseOr(0);
|
|
70
|
+
this.takeSymbol(';');
|
|
71
|
+
const token = this.peek();
|
|
72
|
+
if (token) {
|
|
73
|
+
throw new Error(`SQL predicate contains unexpected token ${JSON.stringify(token.value)}`);
|
|
74
|
+
}
|
|
75
|
+
return predicate;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Parses left-associative disjunction. */
|
|
79
|
+
private parseOr(depth: number): SQLPredicate {
|
|
80
|
+
let predicate = this.parseAnd(depth + 1);
|
|
81
|
+
while (this.takeKeyword('OR')) {
|
|
82
|
+
predicate = combineLogicalPredicate('or', predicate, this.parseAnd(depth + 1));
|
|
83
|
+
}
|
|
84
|
+
return predicate;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Parses left-associative conjunction. */
|
|
88
|
+
private parseAnd(depth: number): SQLPredicate {
|
|
89
|
+
let predicate = this.parseNot(depth + 1);
|
|
90
|
+
while (this.takeKeyword('AND')) {
|
|
91
|
+
predicate = combineLogicalPredicate('and', predicate, this.parseNot(depth + 1));
|
|
92
|
+
}
|
|
93
|
+
return predicate;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Parses boolean negation and parenthesized predicates. */
|
|
97
|
+
private parseNot(depth: number): SQLPredicate {
|
|
98
|
+
this.assertDepth(depth);
|
|
99
|
+
if (this.takeKeyword('NOT')) {
|
|
100
|
+
return {op: 'not', args: [this.parseNot(depth + 1)]};
|
|
101
|
+
}
|
|
102
|
+
if (this.takeSymbol('(')) {
|
|
103
|
+
const predicate = this.parseOr(depth + 1);
|
|
104
|
+
this.expectSymbol(')');
|
|
105
|
+
return predicate;
|
|
106
|
+
}
|
|
107
|
+
return this.parseLeaf();
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Parses one comparison, membership, or null predicate. */
|
|
111
|
+
private parseLeaf(): SQLPredicate {
|
|
112
|
+
const property = this.parseProperty();
|
|
113
|
+
if (this.takeKeyword('IS')) {
|
|
114
|
+
const negated = this.takeKeyword('NOT');
|
|
115
|
+
this.expectKeyword('NULL');
|
|
116
|
+
const predicate: SQLPredicate = {op: 'isNull', args: [property]};
|
|
117
|
+
return negated ? {op: 'not', args: [predicate]} : predicate;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const negatedMembership = this.takeKeyword('NOT');
|
|
121
|
+
if (negatedMembership || this.peekKeyword('IN')) {
|
|
122
|
+
this.expectKeyword('IN');
|
|
123
|
+
this.expectSymbol('(');
|
|
124
|
+
if (this.peek()?.kind === 'symbol' && this.peek()?.value === ')') {
|
|
125
|
+
throw new Error('SQL predicate IN requires at least one scalar value');
|
|
126
|
+
}
|
|
127
|
+
const values: SQLPredicateValue[] = [this.parseValue()];
|
|
128
|
+
while (this.takeSymbol(',')) {
|
|
129
|
+
values.push(this.parseValue());
|
|
130
|
+
}
|
|
131
|
+
this.expectSymbol(')');
|
|
132
|
+
const predicate: SQLPredicate = {op: 'in', args: [property, values]};
|
|
133
|
+
return negatedMembership ? {op: 'not', args: [predicate]} : predicate;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const token = this.take();
|
|
137
|
+
if (
|
|
138
|
+
token?.kind !== 'symbol' ||
|
|
139
|
+
!['=', '!=', '<>', '<', '<=', '>', '>='].includes(token.value)
|
|
140
|
+
) {
|
|
141
|
+
throw new Error('SQL predicate expected a comparison, IN, or IS NULL operator');
|
|
142
|
+
}
|
|
143
|
+
const operator = token.value === '!=' ? '<>' : token.value;
|
|
144
|
+
return {
|
|
145
|
+
op: operator as '=' | '<>' | '<' | '<=' | '>' | '>=',
|
|
146
|
+
args: [property, this.parseValue()]
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** Parses an identifier into a property reference. */
|
|
151
|
+
private parseProperty(): SQLPredicateProperty {
|
|
152
|
+
const token = this.take();
|
|
153
|
+
if (token?.kind !== 'identifier') {
|
|
154
|
+
throw new Error('SQL predicate expected a column identifier');
|
|
155
|
+
}
|
|
156
|
+
return token.quoted ? {property: token.value, quoted: true} : {property: token.value};
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** Parses one scalar literal or named parameter. */
|
|
160
|
+
private parseValue(): SQLPredicateValue {
|
|
161
|
+
const negative = this.takeSymbol('-');
|
|
162
|
+
const token = this.take();
|
|
163
|
+
if (!token) {
|
|
164
|
+
throw new Error('SQL predicate expected a scalar value');
|
|
165
|
+
}
|
|
166
|
+
if (negative && token.kind !== 'number') {
|
|
167
|
+
throw new Error('SQL predicate only permits unary minus on numeric literals');
|
|
168
|
+
}
|
|
169
|
+
if (token.kind === 'number') {
|
|
170
|
+
return parseNumericLiteral(negative ? `-${token.value}` : token.value);
|
|
171
|
+
}
|
|
172
|
+
if (negative) {
|
|
173
|
+
throw new Error('SQL predicate expected a numeric literal after unary minus');
|
|
174
|
+
}
|
|
175
|
+
if (token.kind === 'string') {
|
|
176
|
+
return token.value;
|
|
177
|
+
}
|
|
178
|
+
if (token.kind === 'parameter') {
|
|
179
|
+
if (this.preserveParameters) {
|
|
180
|
+
return {parameter: token.value};
|
|
181
|
+
}
|
|
182
|
+
if (!Object.hasOwn(this.parameters, token.value)) {
|
|
183
|
+
throw new Error(`SQL predicate parameter ":${token.value}" requires a value`);
|
|
184
|
+
}
|
|
185
|
+
const value = this.parameters[token.value];
|
|
186
|
+
validateParameterValue(value, token.value);
|
|
187
|
+
return value;
|
|
188
|
+
}
|
|
189
|
+
if (token.kind === 'identifier' && /^(TRUE|FALSE)$/i.test(token.value)) {
|
|
190
|
+
return /^TRUE$/i.test(token.value);
|
|
191
|
+
}
|
|
192
|
+
if (token.kind === 'identifier' && /^NULL$/i.test(token.value)) {
|
|
193
|
+
throw new Error('SQL predicate comparisons with NULL must use IS NULL or IS NOT NULL');
|
|
194
|
+
}
|
|
195
|
+
throw new Error(`SQL predicate does not support scalar token ${JSON.stringify(token.value)}`);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** Rejects excessively nested inputs before recursion can exhaust the stack. */
|
|
199
|
+
private assertDepth(depth: number): void {
|
|
200
|
+
if (depth > MAXIMUM_EXPRESSION_DEPTH) {
|
|
201
|
+
throw new Error('SQL predicate nesting exceeds its safe limit');
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** Consumes one required keyword. */
|
|
206
|
+
private expectKeyword(keyword: string): void {
|
|
207
|
+
if (!this.takeKeyword(keyword)) {
|
|
208
|
+
throw new Error(`SQL predicate expected ${keyword}`);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/** Returns whether the next token is a keyword without consuming it. */
|
|
213
|
+
private peekKeyword(keyword: string): boolean {
|
|
214
|
+
const token = this.peek();
|
|
215
|
+
return token?.kind === 'identifier' && token.value.toUpperCase() === keyword;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/** Consumes one optional keyword. */
|
|
219
|
+
private takeKeyword(keyword: string): boolean {
|
|
220
|
+
if (!this.peekKeyword(keyword)) {
|
|
221
|
+
return false;
|
|
222
|
+
}
|
|
223
|
+
this.position++;
|
|
224
|
+
return true;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** Consumes one required symbol. */
|
|
228
|
+
private expectSymbol(symbol: string): void {
|
|
229
|
+
if (!this.takeSymbol(symbol)) {
|
|
230
|
+
throw new Error(`SQL predicate expected ${JSON.stringify(symbol)}`);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/** Consumes one optional symbol. */
|
|
235
|
+
private takeSymbol(symbol: string): boolean {
|
|
236
|
+
const token = this.peek();
|
|
237
|
+
if (token?.kind !== 'symbol' || token.value !== symbol) {
|
|
238
|
+
return false;
|
|
239
|
+
}
|
|
240
|
+
this.position++;
|
|
241
|
+
return true;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/** Consumes and returns the next token. */
|
|
245
|
+
private take(): SQLPredicateToken | undefined {
|
|
246
|
+
const token = this.tokens[this.position];
|
|
247
|
+
if (token) {
|
|
248
|
+
this.position++;
|
|
249
|
+
}
|
|
250
|
+
return token;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/** Returns the next token without consuming it. */
|
|
254
|
+
private peek(): SQLPredicateToken | undefined {
|
|
255
|
+
return this.tokens[this.position];
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/** Flattens adjacent equal logical operators into one CQL2-shaped node. */
|
|
260
|
+
function combineLogicalPredicate(
|
|
261
|
+
operator: 'and' | 'or',
|
|
262
|
+
left: SQLPredicate,
|
|
263
|
+
right: SQLPredicate
|
|
264
|
+
): SQLLogicalPredicate {
|
|
265
|
+
return left.op === operator
|
|
266
|
+
? {op: operator, args: [...(left as SQLLogicalPredicate).args, right]}
|
|
267
|
+
: {op: operator, args: [left, right]};
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/** Converts a bounded SQL numeric token to a number or exact bigint. */
|
|
271
|
+
function parseNumericLiteral(source: string): number | bigint {
|
|
272
|
+
if (/^-?\d+$/.test(source)) {
|
|
273
|
+
const bigint = BigInt(source);
|
|
274
|
+
if (bigint > BigInt(Number.MAX_SAFE_INTEGER) || bigint < BigInt(Number.MIN_SAFE_INTEGER)) {
|
|
275
|
+
return bigint;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
const number = Number(source);
|
|
279
|
+
if (!Number.isFinite(number)) {
|
|
280
|
+
throw new Error('SQL predicate numeric literals must be finite');
|
|
281
|
+
}
|
|
282
|
+
return number;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/** Validates one named parameter at its parser boundary. */
|
|
286
|
+
function validateParameterValue(value: unknown, name: string): asserts value is SQLPredicateValue {
|
|
287
|
+
const probe = {op: '=', args: [{property: '_'}, value]} as unknown;
|
|
288
|
+
try {
|
|
289
|
+
validateSQLPredicate(probe);
|
|
290
|
+
} catch {
|
|
291
|
+
throw new Error(`SQL predicate parameter ":${name}" has an unsupported value`);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/** Tokenizes a safely bounded SQL predicate expression. */
|
|
296
|
+
function tokenizeSQLPredicate(source: string): SQLPredicateToken[] {
|
|
297
|
+
if (
|
|
298
|
+
typeof source !== 'string' ||
|
|
299
|
+
source.trim().length === 0 ||
|
|
300
|
+
source.length > MAXIMUM_SQL_LENGTH
|
|
301
|
+
) {
|
|
302
|
+
throw new Error('SQL predicate requires a non-empty, safely bounded expression');
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
const tokens: SQLPredicateToken[] = [];
|
|
306
|
+
let position = 0;
|
|
307
|
+
while (position < source.length) {
|
|
308
|
+
const character = source[position];
|
|
309
|
+
if (/\s/.test(character)) {
|
|
310
|
+
position++;
|
|
311
|
+
continue;
|
|
312
|
+
}
|
|
313
|
+
if (character === "'") {
|
|
314
|
+
const result = readQuotedToken(source, position, "'", "'");
|
|
315
|
+
tokens.push({kind: 'string', value: result.value});
|
|
316
|
+
position = result.position;
|
|
317
|
+
} else if (character === '"') {
|
|
318
|
+
const result = readQuotedToken(source, position, '"', '"');
|
|
319
|
+
tokens.push({kind: 'identifier', value: result.value, quoted: true});
|
|
320
|
+
position = result.position;
|
|
321
|
+
} else if (character === ':') {
|
|
322
|
+
const match = /^[A-Za-z_][A-Za-z0-9_]*/.exec(source.slice(position + 1));
|
|
323
|
+
if (!match) {
|
|
324
|
+
throw new Error(`SQL predicate contains an invalid parameter at character ${position}`);
|
|
325
|
+
}
|
|
326
|
+
tokens.push({kind: 'parameter', value: match[0]});
|
|
327
|
+
position += match[0].length + 1;
|
|
328
|
+
} else {
|
|
329
|
+
const numberMatch = /^(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?/.exec(source.slice(position));
|
|
330
|
+
const identifierMatch = /^[A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z_][A-Za-z0-9_]*)*/.exec(
|
|
331
|
+
source.slice(position)
|
|
332
|
+
);
|
|
333
|
+
if (numberMatch) {
|
|
334
|
+
tokens.push({kind: 'number', value: numberMatch[0]});
|
|
335
|
+
position += numberMatch[0].length;
|
|
336
|
+
} else if (identifierMatch) {
|
|
337
|
+
tokens.push({kind: 'identifier', value: identifierMatch[0]});
|
|
338
|
+
position += identifierMatch[0].length;
|
|
339
|
+
} else {
|
|
340
|
+
const twoCharacters = source.slice(position, position + 2);
|
|
341
|
+
if (['<=', '>=', '<>', '!='].includes(twoCharacters)) {
|
|
342
|
+
tokens.push({kind: 'symbol', value: twoCharacters});
|
|
343
|
+
position += 2;
|
|
344
|
+
} else if ('(),;-=<>'.includes(character)) {
|
|
345
|
+
tokens.push({kind: 'symbol', value: character});
|
|
346
|
+
position++;
|
|
347
|
+
} else {
|
|
348
|
+
throw new Error(`SQL predicate contains an unsupported token at character ${position}`);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
if (tokens.length > MAXIMUM_SQL_TOKENS) {
|
|
353
|
+
throw new Error('SQL predicate exceeds its safe token limit');
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
return tokens;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/** Reads one SQL string or quoted identifier with doubled-quote escaping. */
|
|
360
|
+
function readQuotedToken(
|
|
361
|
+
source: string,
|
|
362
|
+
start: number,
|
|
363
|
+
quote: string,
|
|
364
|
+
escapedQuote: string
|
|
365
|
+
): {value: string; position: number} {
|
|
366
|
+
let value = '';
|
|
367
|
+
let position = start + 1;
|
|
368
|
+
while (position < source.length) {
|
|
369
|
+
if (source[position] !== quote) {
|
|
370
|
+
value += source[position++];
|
|
371
|
+
continue;
|
|
372
|
+
}
|
|
373
|
+
if (source[position + 1] === escapedQuote) {
|
|
374
|
+
value += quote;
|
|
375
|
+
position += 2;
|
|
376
|
+
continue;
|
|
377
|
+
}
|
|
378
|
+
return {value, position: position + 1};
|
|
379
|
+
}
|
|
380
|
+
throw new Error(`SQL predicate contains an unterminated quoted value at character ${start}`);
|
|
381
|
+
}
|