@powersync/service-sync-rules 0.17.10
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 +67 -0
- package/README.md +3 -0
- package/dist/DartSchemaGenerator.d.ts +12 -0
- package/dist/DartSchemaGenerator.js +39 -0
- package/dist/DartSchemaGenerator.js.map +1 -0
- package/dist/ExpressionType.d.ts +33 -0
- package/dist/ExpressionType.js +61 -0
- package/dist/ExpressionType.js.map +1 -0
- package/dist/IdSequence.d.ts +4 -0
- package/dist/IdSequence.js +9 -0
- package/dist/IdSequence.js.map +1 -0
- package/dist/JsSchemaGenerator.d.ts +12 -0
- package/dist/JsSchemaGenerator.js +42 -0
- package/dist/JsSchemaGenerator.js.map +1 -0
- package/dist/SchemaGenerator.d.ts +14 -0
- package/dist/SchemaGenerator.js +26 -0
- package/dist/SchemaGenerator.js.map +1 -0
- package/dist/SourceTableInterface.d.ts +5 -0
- package/dist/SourceTableInterface.js +2 -0
- package/dist/SourceTableInterface.js.map +1 -0
- package/dist/SqlBucketDescriptor.d.ts +37 -0
- package/dist/SqlBucketDescriptor.js +111 -0
- package/dist/SqlBucketDescriptor.js.map +1 -0
- package/dist/SqlDataQuery.d.ts +39 -0
- package/dist/SqlDataQuery.js +237 -0
- package/dist/SqlDataQuery.js.map +1 -0
- package/dist/SqlParameterQuery.d.ts +43 -0
- package/dist/SqlParameterQuery.js +238 -0
- package/dist/SqlParameterQuery.js.map +1 -0
- package/dist/SqlSyncRules.d.ts +52 -0
- package/dist/SqlSyncRules.js +247 -0
- package/dist/SqlSyncRules.js.map +1 -0
- package/dist/StaticSchema.d.ts +26 -0
- package/dist/StaticSchema.js +61 -0
- package/dist/StaticSchema.js.map +1 -0
- package/dist/StaticSqlParameterQuery.d.ts +24 -0
- package/dist/StaticSqlParameterQuery.js +66 -0
- package/dist/StaticSqlParameterQuery.js.map +1 -0
- package/dist/TablePattern.d.ts +17 -0
- package/dist/TablePattern.js +56 -0
- package/dist/TablePattern.js.map +1 -0
- package/dist/TableQuerySchema.d.ts +9 -0
- package/dist/TableQuerySchema.js +34 -0
- package/dist/TableQuerySchema.js.map +1 -0
- package/dist/errors.d.ts +22 -0
- package/dist/errors.js +58 -0
- package/dist/errors.js.map +1 -0
- package/dist/generators.d.ts +6 -0
- package/dist/generators.js +7 -0
- package/dist/generators.js.map +1 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +19 -0
- package/dist/index.js.map +1 -0
- package/dist/json_schema.d.ts +3 -0
- package/dist/json_schema.js +48 -0
- package/dist/json_schema.js.map +1 -0
- package/dist/sql_filters.d.ts +91 -0
- package/dist/sql_filters.js +506 -0
- package/dist/sql_filters.js.map +1 -0
- package/dist/sql_functions.d.ts +54 -0
- package/dist/sql_functions.js +773 -0
- package/dist/sql_functions.js.map +1 -0
- package/dist/sql_support.d.ts +22 -0
- package/dist/sql_support.js +213 -0
- package/dist/sql_support.js.map +1 -0
- package/dist/types.d.ts +174 -0
- package/dist/types.js +10 -0
- package/dist/types.js.map +1 -0
- package/dist/utils.d.ts +45 -0
- package/dist/utils.js +173 -0
- package/dist/utils.js.map +1 -0
- package/package.json +29 -0
|
@@ -0,0 +1,506 @@
|
|
|
1
|
+
import { SqlRuleError } from './errors.js';
|
|
2
|
+
import { BASIC_OPERATORS, cast, CAST_TYPES, jsonExtract, SQL_FUNCTIONS, sqliteTypeOf } from './sql_functions.js';
|
|
3
|
+
import { isJsonValue } from './utils.js';
|
|
4
|
+
import { andFilters, compileStaticOperator, isClauseError, isParameterMatchClause, isParameterValueClause, isStaticRowValueClause, orFilters, SQLITE_FALSE, SQLITE_TRUE, sqliteNot, toBooleanParameterSetClause } from './sql_support.js';
|
|
5
|
+
import { ExpressionType, TYPE_NONE } from './ExpressionType.js';
|
|
6
|
+
export const MATCH_CONST_FALSE = [];
|
|
7
|
+
export const MATCH_CONST_TRUE = [{}];
|
|
8
|
+
Object.freeze(MATCH_CONST_TRUE);
|
|
9
|
+
Object.freeze(MATCH_CONST_FALSE);
|
|
10
|
+
export class SqlTools {
|
|
11
|
+
constructor(options) {
|
|
12
|
+
this.errors = [];
|
|
13
|
+
this.default_table = options.table;
|
|
14
|
+
this.schema = options.schema;
|
|
15
|
+
if (options.value_tables) {
|
|
16
|
+
this.value_tables = options.value_tables;
|
|
17
|
+
}
|
|
18
|
+
else if (this.default_table) {
|
|
19
|
+
this.value_tables = [this.default_table];
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
this.value_tables = [];
|
|
23
|
+
}
|
|
24
|
+
this.parameter_tables = options.parameter_tables ?? [];
|
|
25
|
+
this.sql = options.sql;
|
|
26
|
+
this.supports_expanding_parameters = options.supports_expanding_parameters ?? false;
|
|
27
|
+
}
|
|
28
|
+
error(message, expr) {
|
|
29
|
+
this.errors.push(new SqlRuleError(message, this.sql, expr));
|
|
30
|
+
return { error: true };
|
|
31
|
+
}
|
|
32
|
+
warn(message, expr) {
|
|
33
|
+
const error = new SqlRuleError(message, this.sql, expr);
|
|
34
|
+
error.type = 'warning';
|
|
35
|
+
this.errors.push(error);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Compile the where clause into a ParameterMatchClause.
|
|
39
|
+
*
|
|
40
|
+
* A ParameterMatchClause takes a data row, and returns filter values that
|
|
41
|
+
* would make the expression true for the row.
|
|
42
|
+
*/
|
|
43
|
+
compileWhereClause(where) {
|
|
44
|
+
const base = this.compileClause(where);
|
|
45
|
+
return toBooleanParameterSetClause(base);
|
|
46
|
+
}
|
|
47
|
+
compileStaticExtractor(expr) {
|
|
48
|
+
const clause = this.compileClause(expr);
|
|
49
|
+
if (!isStaticRowValueClause(clause) && !isClauseError(clause)) {
|
|
50
|
+
throw new SqlRuleError('Bucket parameters are not allowed here', this.sql, expr ?? undefined);
|
|
51
|
+
}
|
|
52
|
+
return clause;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Given an expression, return a compiled clause.
|
|
56
|
+
*/
|
|
57
|
+
compileClause(expr) {
|
|
58
|
+
if (expr == null) {
|
|
59
|
+
return {
|
|
60
|
+
evaluate: () => SQLITE_TRUE,
|
|
61
|
+
getType() {
|
|
62
|
+
return ExpressionType.INTEGER;
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
else if (isStatic(expr)) {
|
|
67
|
+
const value = staticValue(expr);
|
|
68
|
+
return {
|
|
69
|
+
evaluate: () => value,
|
|
70
|
+
getType() {
|
|
71
|
+
return ExpressionType.fromTypeText(sqliteTypeOf(value));
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
else if (expr.type == 'ref') {
|
|
76
|
+
const column = expr.name;
|
|
77
|
+
if (column == '*') {
|
|
78
|
+
return this.error('* not supported here', expr);
|
|
79
|
+
}
|
|
80
|
+
if (this.refHasSchema(expr)) {
|
|
81
|
+
return this.error(`Schema is not supported in column references`, expr);
|
|
82
|
+
}
|
|
83
|
+
if (this.isParameterRef(expr)) {
|
|
84
|
+
const param = this.getParameterRef(expr);
|
|
85
|
+
return { bucketParameter: param };
|
|
86
|
+
}
|
|
87
|
+
else if (this.isTableRef(expr)) {
|
|
88
|
+
const table = this.getTableName(expr);
|
|
89
|
+
this.checkRef(table, expr);
|
|
90
|
+
return {
|
|
91
|
+
evaluate(tables) {
|
|
92
|
+
return tables[table]?.[column];
|
|
93
|
+
},
|
|
94
|
+
getType(schema) {
|
|
95
|
+
return schema.getType(table, column);
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
const ref = [expr.table?.schema, expr.table?.name, expr.name]
|
|
101
|
+
.filter((e) => e != null)
|
|
102
|
+
.join('.');
|
|
103
|
+
return this.error(`Undefined reference: ${ref}`, expr);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
else if (expr.type == 'binary') {
|
|
107
|
+
const { left, right, op } = expr;
|
|
108
|
+
const leftFilter = this.compileClause(left);
|
|
109
|
+
const rightFilter = this.compileClause(right);
|
|
110
|
+
if (isClauseError(leftFilter) || isClauseError(rightFilter)) {
|
|
111
|
+
return { error: true };
|
|
112
|
+
}
|
|
113
|
+
if (op == 'AND') {
|
|
114
|
+
try {
|
|
115
|
+
return andFilters(leftFilter, rightFilter);
|
|
116
|
+
}
|
|
117
|
+
catch (e) {
|
|
118
|
+
return this.error(e.message, expr);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
else if (op == 'OR') {
|
|
122
|
+
try {
|
|
123
|
+
return orFilters(leftFilter, rightFilter);
|
|
124
|
+
}
|
|
125
|
+
catch (e) {
|
|
126
|
+
return this.error(e.message, expr);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
else if (op == '=') {
|
|
130
|
+
// Options:
|
|
131
|
+
// 1. static, static
|
|
132
|
+
// 2. static, parameterValue
|
|
133
|
+
// 3. static true, parameterMatch - not supported yet
|
|
134
|
+
let staticFilter1;
|
|
135
|
+
let otherFilter1;
|
|
136
|
+
if (!isStaticRowValueClause(leftFilter) && !isStaticRowValueClause(rightFilter)) {
|
|
137
|
+
return this.error(`Cannot have bucket parameters on both sides of = operator`, expr);
|
|
138
|
+
}
|
|
139
|
+
else if (isStaticRowValueClause(leftFilter)) {
|
|
140
|
+
staticFilter1 = leftFilter;
|
|
141
|
+
otherFilter1 = rightFilter;
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
staticFilter1 = rightFilter;
|
|
145
|
+
otherFilter1 = leftFilter;
|
|
146
|
+
}
|
|
147
|
+
const staticFilter = staticFilter1;
|
|
148
|
+
const otherFilter = otherFilter1;
|
|
149
|
+
if (isStaticRowValueClause(otherFilter)) {
|
|
150
|
+
// 1. static, static
|
|
151
|
+
return compileStaticOperator(op, leftFilter, rightFilter);
|
|
152
|
+
}
|
|
153
|
+
else if (isParameterValueClause(otherFilter)) {
|
|
154
|
+
// 2. static, parameterValue
|
|
155
|
+
return {
|
|
156
|
+
error: false,
|
|
157
|
+
bucketParameters: [otherFilter.bucketParameter],
|
|
158
|
+
unbounded: false,
|
|
159
|
+
filter(tables) {
|
|
160
|
+
const value = staticFilter.evaluate(tables);
|
|
161
|
+
if (value == null) {
|
|
162
|
+
// null never matches on =
|
|
163
|
+
// Should technically return null, but "false" is sufficient here
|
|
164
|
+
return MATCH_CONST_FALSE;
|
|
165
|
+
}
|
|
166
|
+
if (!isJsonValue(value)) {
|
|
167
|
+
// Cannot persist this, e.g. BLOB
|
|
168
|
+
return MATCH_CONST_FALSE;
|
|
169
|
+
}
|
|
170
|
+
return [{ [otherFilter.bucketParameter]: value }];
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
else if (isParameterMatchClause(otherFilter)) {
|
|
175
|
+
// 3. static, parameterMatch
|
|
176
|
+
// (bucket.param = 'something') = staticValue
|
|
177
|
+
// To implement this, we need to ensure the static value here can only be true.
|
|
178
|
+
return this.error(`Bucket parameter clauses cannot currently be combined with other operators`, expr);
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
throw new Error('Unexpected');
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
else if (op == 'IN') {
|
|
185
|
+
// Options:
|
|
186
|
+
// static IN static
|
|
187
|
+
// parameterValue IN static
|
|
188
|
+
if (isStaticRowValueClause(leftFilter) && isStaticRowValueClause(rightFilter)) {
|
|
189
|
+
return compileStaticOperator(op, leftFilter, rightFilter);
|
|
190
|
+
}
|
|
191
|
+
else if (isParameterValueClause(leftFilter) && isStaticRowValueClause(rightFilter)) {
|
|
192
|
+
const param = leftFilter.bucketParameter;
|
|
193
|
+
return {
|
|
194
|
+
error: false,
|
|
195
|
+
bucketParameters: [param],
|
|
196
|
+
unbounded: true,
|
|
197
|
+
filter(tables) {
|
|
198
|
+
const aValue = rightFilter.evaluate(tables);
|
|
199
|
+
if (aValue == null) {
|
|
200
|
+
return MATCH_CONST_FALSE;
|
|
201
|
+
}
|
|
202
|
+
const values = JSON.parse(aValue);
|
|
203
|
+
if (!Array.isArray(values)) {
|
|
204
|
+
throw new Error('Not an array');
|
|
205
|
+
}
|
|
206
|
+
return values.map((value) => {
|
|
207
|
+
return { [param]: value };
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
else if (this.supports_expanding_parameters &&
|
|
213
|
+
isStaticRowValueClause(leftFilter) &&
|
|
214
|
+
isParameterValueClause(rightFilter)) {
|
|
215
|
+
const param = `${rightFilter.bucketParameter}[*]`;
|
|
216
|
+
return {
|
|
217
|
+
error: false,
|
|
218
|
+
bucketParameters: [param],
|
|
219
|
+
unbounded: false,
|
|
220
|
+
filter(tables) {
|
|
221
|
+
const value = leftFilter.evaluate(tables);
|
|
222
|
+
if (!isJsonValue(value)) {
|
|
223
|
+
// Cannot persist, e.g. BLOB
|
|
224
|
+
return MATCH_CONST_FALSE;
|
|
225
|
+
}
|
|
226
|
+
return [{ [param]: value }];
|
|
227
|
+
}
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
else {
|
|
231
|
+
return this.error(`Unsupported usage of IN operator`, expr);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
else if (BASIC_OPERATORS.has(op)) {
|
|
235
|
+
if (!isStaticRowValueClause(leftFilter) || !isStaticRowValueClause(rightFilter)) {
|
|
236
|
+
return this.error(`Operator ${op} is not supported on bucket parameters`, expr);
|
|
237
|
+
}
|
|
238
|
+
return compileStaticOperator(op, leftFilter, rightFilter);
|
|
239
|
+
}
|
|
240
|
+
else {
|
|
241
|
+
return this.error(`Operator not supported: ${op}`, expr);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
else if (expr.type == 'unary') {
|
|
245
|
+
if (expr.op == 'NOT') {
|
|
246
|
+
const filter = this.compileClause(expr.operand);
|
|
247
|
+
if (isClauseError(filter)) {
|
|
248
|
+
return filter;
|
|
249
|
+
}
|
|
250
|
+
else if (!isStaticRowValueClause(filter)) {
|
|
251
|
+
return this.error('Cannot use NOT on bucket parameter filters', expr);
|
|
252
|
+
}
|
|
253
|
+
return {
|
|
254
|
+
evaluate: (tables) => {
|
|
255
|
+
const value = filter.evaluate(tables);
|
|
256
|
+
return sqliteNot(value);
|
|
257
|
+
},
|
|
258
|
+
getType() {
|
|
259
|
+
return ExpressionType.INTEGER;
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
else if (expr.op == 'IS NULL') {
|
|
264
|
+
const leftFilter = this.compileClause(expr.operand);
|
|
265
|
+
if (isClauseError(leftFilter)) {
|
|
266
|
+
return leftFilter;
|
|
267
|
+
}
|
|
268
|
+
else if (isStaticRowValueClause(leftFilter)) {
|
|
269
|
+
// 1. static IS NULL
|
|
270
|
+
const nullValue = {
|
|
271
|
+
evaluate: () => null,
|
|
272
|
+
getType() {
|
|
273
|
+
return ExpressionType.INTEGER;
|
|
274
|
+
}
|
|
275
|
+
};
|
|
276
|
+
return compileStaticOperator('IS', leftFilter, nullValue);
|
|
277
|
+
}
|
|
278
|
+
else if (isParameterValueClause(leftFilter)) {
|
|
279
|
+
// 2. param IS NULL
|
|
280
|
+
return {
|
|
281
|
+
error: false,
|
|
282
|
+
bucketParameters: [leftFilter.bucketParameter],
|
|
283
|
+
unbounded: false,
|
|
284
|
+
filter(tables) {
|
|
285
|
+
return [{ [leftFilter.bucketParameter]: null }];
|
|
286
|
+
}
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
else {
|
|
290
|
+
return this.error(`Cannot use IS NULL here`, expr);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
else if (expr.op == 'IS NOT NULL') {
|
|
294
|
+
const leftFilter = this.compileClause(expr.operand);
|
|
295
|
+
if (isClauseError(leftFilter)) {
|
|
296
|
+
return leftFilter;
|
|
297
|
+
}
|
|
298
|
+
else if (isStaticRowValueClause(leftFilter)) {
|
|
299
|
+
// 1. static IS NULL
|
|
300
|
+
const nullValue = {
|
|
301
|
+
evaluate: () => null,
|
|
302
|
+
getType() {
|
|
303
|
+
return ExpressionType.INTEGER;
|
|
304
|
+
}
|
|
305
|
+
};
|
|
306
|
+
return compileStaticOperator('IS NOT', leftFilter, nullValue);
|
|
307
|
+
}
|
|
308
|
+
else {
|
|
309
|
+
return this.error(`Cannot use IS NOT NULL here`, expr);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
else {
|
|
313
|
+
return this.error(`Operator ${expr.op} is not supported`, expr);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
else if (expr.type == 'call' && expr.function?.name != null) {
|
|
317
|
+
const fn = expr.function.name;
|
|
318
|
+
const fnImpl = SQL_FUNCTIONS[fn];
|
|
319
|
+
if (fnImpl == null) {
|
|
320
|
+
return this.error(`Function '${fn}' is not defined`, expr);
|
|
321
|
+
}
|
|
322
|
+
let error = false;
|
|
323
|
+
const argExtractors = expr.args.map((arg) => {
|
|
324
|
+
const clause = this.compileClause(arg);
|
|
325
|
+
if (isClauseError(clause)) {
|
|
326
|
+
error = true;
|
|
327
|
+
}
|
|
328
|
+
else if (!isStaticRowValueClause(clause)) {
|
|
329
|
+
error = true;
|
|
330
|
+
return this.error(`Bucket parameters are not supported in function call arguments`, arg);
|
|
331
|
+
}
|
|
332
|
+
return clause;
|
|
333
|
+
});
|
|
334
|
+
if (error) {
|
|
335
|
+
return { error: true };
|
|
336
|
+
}
|
|
337
|
+
return {
|
|
338
|
+
evaluate: (tables) => {
|
|
339
|
+
const args = argExtractors.map((e) => e.evaluate(tables));
|
|
340
|
+
return fnImpl.call(...args);
|
|
341
|
+
},
|
|
342
|
+
getType(schema) {
|
|
343
|
+
const argTypes = argExtractors.map((e) => e.getType(schema));
|
|
344
|
+
return fnImpl.getReturnType(argTypes);
|
|
345
|
+
}
|
|
346
|
+
};
|
|
347
|
+
}
|
|
348
|
+
else if (expr.type == 'member') {
|
|
349
|
+
const operand = this.compileClause(expr.operand);
|
|
350
|
+
if (isClauseError(operand)) {
|
|
351
|
+
return operand;
|
|
352
|
+
}
|
|
353
|
+
else if (!isStaticRowValueClause(operand)) {
|
|
354
|
+
return this.error(`Bucket parameters are not supported in member lookups`, expr.operand);
|
|
355
|
+
}
|
|
356
|
+
if (typeof expr.member == 'string' && (expr.op == '->>' || expr.op == '->')) {
|
|
357
|
+
return {
|
|
358
|
+
evaluate: (tables) => {
|
|
359
|
+
const containerString = operand.evaluate(tables);
|
|
360
|
+
return jsonExtract(containerString, expr.member, expr.op);
|
|
361
|
+
},
|
|
362
|
+
getType() {
|
|
363
|
+
return ExpressionType.ANY_JSON;
|
|
364
|
+
}
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
else {
|
|
368
|
+
return this.error(`Unsupported member operation ${expr.op}`, expr);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
else if (expr.type == 'cast') {
|
|
372
|
+
const operand = this.compileClause(expr.operand);
|
|
373
|
+
if (isClauseError(operand)) {
|
|
374
|
+
return operand;
|
|
375
|
+
}
|
|
376
|
+
else if (!isStaticRowValueClause(operand)) {
|
|
377
|
+
return this.error(`Bucket parameters are not supported in cast expressions`, expr.operand);
|
|
378
|
+
}
|
|
379
|
+
const to = expr.to?.name?.toLowerCase();
|
|
380
|
+
if (CAST_TYPES.has(to)) {
|
|
381
|
+
return {
|
|
382
|
+
evaluate: (tables) => {
|
|
383
|
+
const value = operand.evaluate(tables);
|
|
384
|
+
if (value == null) {
|
|
385
|
+
return null;
|
|
386
|
+
}
|
|
387
|
+
return cast(value, to);
|
|
388
|
+
},
|
|
389
|
+
getType() {
|
|
390
|
+
return ExpressionType.fromTypeText(to);
|
|
391
|
+
}
|
|
392
|
+
};
|
|
393
|
+
}
|
|
394
|
+
else {
|
|
395
|
+
return this.error(`CAST not supported for '${to}'`, expr);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
else {
|
|
399
|
+
return this.error(`${expr.type} not supported here`, expr);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
/**
|
|
403
|
+
* "some_column" => "some_column"
|
|
404
|
+
* "table.some_column" => "some_column".
|
|
405
|
+
* "some_function() AS some_column" => "some_column"
|
|
406
|
+
* "some_function() some_column" => "some_column"
|
|
407
|
+
* "some_function()" => error
|
|
408
|
+
*/
|
|
409
|
+
getOutputName(column) {
|
|
410
|
+
let alias = column.alias?.name;
|
|
411
|
+
if (alias) {
|
|
412
|
+
return alias;
|
|
413
|
+
}
|
|
414
|
+
const expr = column.expr;
|
|
415
|
+
if (expr.type == 'ref') {
|
|
416
|
+
return expr.name;
|
|
417
|
+
}
|
|
418
|
+
throw new SqlRuleError(`alias is required`, this.sql, column.expr);
|
|
419
|
+
}
|
|
420
|
+
getSpecificOutputName(column) {
|
|
421
|
+
const name = this.getOutputName(column);
|
|
422
|
+
if (name == '*') {
|
|
423
|
+
throw new SqlRuleError('* is not supported here - use explicit columns', this.sql, column.expr);
|
|
424
|
+
}
|
|
425
|
+
return name;
|
|
426
|
+
}
|
|
427
|
+
/**
|
|
428
|
+
* Check if an expression is a parameter_table reference.
|
|
429
|
+
*/
|
|
430
|
+
isParameterRef(expr) {
|
|
431
|
+
if (expr.type != 'ref') {
|
|
432
|
+
return false;
|
|
433
|
+
}
|
|
434
|
+
return this.parameter_tables.includes(expr.table?.name ?? '');
|
|
435
|
+
}
|
|
436
|
+
/**
|
|
437
|
+
* Check if an expression is a value_tables reference.
|
|
438
|
+
*
|
|
439
|
+
* This means the expression can be evaluated directly on a value row.
|
|
440
|
+
*/
|
|
441
|
+
isTableRef(expr) {
|
|
442
|
+
if (expr.type != 'ref') {
|
|
443
|
+
return false;
|
|
444
|
+
}
|
|
445
|
+
try {
|
|
446
|
+
this.getTableName(expr);
|
|
447
|
+
return true;
|
|
448
|
+
}
|
|
449
|
+
catch (e) {
|
|
450
|
+
return false;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
checkRef(table, ref) {
|
|
454
|
+
if (this.schema) {
|
|
455
|
+
const type = this.schema.getType(table, ref.name);
|
|
456
|
+
if (type.typeFlags == TYPE_NONE) {
|
|
457
|
+
this.warn(`Column not found: ${ref.name}`, ref);
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
getParameterRef(expr) {
|
|
462
|
+
if (this.isParameterRef(expr)) {
|
|
463
|
+
return `${expr.table.name}.${expr.name}`;
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
refHasSchema(ref) {
|
|
467
|
+
return ref.table?.schema != null;
|
|
468
|
+
}
|
|
469
|
+
/**
|
|
470
|
+
* Get the table name from an expression.
|
|
471
|
+
*
|
|
472
|
+
* Only "value" tables are supported here, not parameter values.
|
|
473
|
+
*/
|
|
474
|
+
getTableName(ref) {
|
|
475
|
+
if (this.refHasSchema(ref)) {
|
|
476
|
+
throw new SqlRuleError(`Specifying schema in column references is not supported`, this.sql, ref);
|
|
477
|
+
}
|
|
478
|
+
if (ref.table?.name == null && this.default_table != null) {
|
|
479
|
+
return this.default_table;
|
|
480
|
+
}
|
|
481
|
+
else if (this.value_tables.includes(ref.table?.name ?? '')) {
|
|
482
|
+
return ref.table.name;
|
|
483
|
+
}
|
|
484
|
+
else if (ref.table?.name == null) {
|
|
485
|
+
throw new SqlRuleError(`Table name required`, this.sql, ref);
|
|
486
|
+
}
|
|
487
|
+
else {
|
|
488
|
+
throw new SqlRuleError(`Undefined table ${ref.table?.name}`, this.sql, ref);
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
function isStatic(expr) {
|
|
493
|
+
return ['integer', 'string', 'numeric', 'boolean', 'null'].includes(expr.type);
|
|
494
|
+
}
|
|
495
|
+
function staticValue(expr) {
|
|
496
|
+
if (expr.type == 'boolean') {
|
|
497
|
+
return expr.value ? SQLITE_TRUE : SQLITE_FALSE;
|
|
498
|
+
}
|
|
499
|
+
else if (expr.type == 'integer') {
|
|
500
|
+
return BigInt(expr.value);
|
|
501
|
+
}
|
|
502
|
+
else {
|
|
503
|
+
return expr.value;
|
|
504
|
+
}
|
|
505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I,CAAC,KAAK,CAAC,CAAC;KAC3B;SAAM;QACL,OAAQ,IAAY,CAAC,KAAK,CAAC;KAC5B;AACH,CAAC"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { SqliteValue } from './types.js';
|
|
2
|
+
import wkx from '@syncpoint/wkx';
|
|
3
|
+
import { ExpressionType } from './ExpressionType.js';
|
|
4
|
+
export declare const BASIC_OPERATORS: Set<string>;
|
|
5
|
+
export interface FunctionParameter {
|
|
6
|
+
name: string;
|
|
7
|
+
type: ExpressionType;
|
|
8
|
+
optional: boolean;
|
|
9
|
+
}
|
|
10
|
+
export interface SqlFunction {
|
|
11
|
+
parameters: FunctionParameter[];
|
|
12
|
+
call: (...args: SqliteValue[]) => SqliteValue;
|
|
13
|
+
getReturnType(args: ExpressionType[]): ExpressionType;
|
|
14
|
+
}
|
|
15
|
+
export declare const SQL_FUNCTIONS_NAMED: {
|
|
16
|
+
upper: SqlFunction;
|
|
17
|
+
lower: SqlFunction;
|
|
18
|
+
hex: SqlFunction;
|
|
19
|
+
length: SqlFunction;
|
|
20
|
+
base64: SqlFunction;
|
|
21
|
+
typeof: SqlFunction;
|
|
22
|
+
ifnull: SqlFunction;
|
|
23
|
+
json_extract: SqlFunction;
|
|
24
|
+
json_array_length: SqlFunction;
|
|
25
|
+
json_valid: SqlFunction;
|
|
26
|
+
unixepoch: SqlFunction;
|
|
27
|
+
datetime: SqlFunction;
|
|
28
|
+
st_asgeojson: SqlFunction;
|
|
29
|
+
st_astext: SqlFunction;
|
|
30
|
+
st_x: SqlFunction;
|
|
31
|
+
st_y: SqlFunction;
|
|
32
|
+
};
|
|
33
|
+
export declare const SQL_FUNCTIONS_CALL: Record<"base64" | "hex" | "json_extract" | "upper" | "lower" | "length" | "typeof" | "ifnull" | "json_array_length" | "json_valid" | "unixepoch" | "datetime" | "st_asgeojson" | "st_astext" | "st_x" | "st_y", (...args: SqliteValue[]) => SqliteValue>;
|
|
34
|
+
export declare const SQL_FUNCTIONS: Record<string, SqlFunction>;
|
|
35
|
+
export declare const CAST_TYPES: Set<String>;
|
|
36
|
+
export declare function castAsText(value: SqliteValue): string | null;
|
|
37
|
+
export declare function castAsBlob(value: SqliteValue): Uint8Array | null;
|
|
38
|
+
export declare function cast(value: SqliteValue, to: string): string | number | bigint | Uint8Array | null;
|
|
39
|
+
export declare function sqliteTypeOf(arg: SqliteValue): "null" | "blob" | "text" | "integer" | "real";
|
|
40
|
+
export declare function parseGeometry(value?: SqliteValue): wkx.Geometry | null;
|
|
41
|
+
export declare function evaluateOperator(op: string, a: SqliteValue, b: SqliteValue): SqliteValue;
|
|
42
|
+
export declare function getOperatorReturnType(op: string, left: ExpressionType, right: ExpressionType): ExpressionType;
|
|
43
|
+
export declare function jsonExtract(sourceValue: SqliteValue, path: SqliteValue, operator: string): SqliteValue;
|
|
44
|
+
export interface ParseDateFlags {
|
|
45
|
+
/**
|
|
46
|
+
* True if input is unixepoch instead of julien days
|
|
47
|
+
*/
|
|
48
|
+
unixepoch?: boolean;
|
|
49
|
+
/**
|
|
50
|
+
* True if output should include milliseconds
|
|
51
|
+
*/
|
|
52
|
+
subsecond?: boolean;
|
|
53
|
+
}
|
|
54
|
+
export declare function convertToDate(dateTime: SqliteValue, flags: ParseDateFlags): Date | null;
|