-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathSqlBucketDescriptor.ts
171 lines (146 loc) · 4.94 KB
/
SqlBucketDescriptor.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import { BucketDescription } from './BucketDescription.js';
import { BucketParameterQuerier, mergeBucketParameterQueriers } from './BucketParameterQuerier.js';
import { IdSequence } from './IdSequence.js';
import { SourceTableInterface } from './SourceTableInterface.js';
import { SqlDataQuery } from './SqlDataQuery.js';
import { SqlParameterQuery } from './SqlParameterQuery.js';
import { SyncRulesOptions } from './SqlSyncRules.js';
import { StaticSqlParameterQuery } from './StaticSqlParameterQuery.js';
import { TablePattern } from './TablePattern.js';
import { SqlRuleError } from './errors.js';
import {
EvaluatedParametersResult,
EvaluateRowOptions,
EvaluationResult,
QueryParseOptions,
RequestParameters,
SqliteRow
} from './types.js';
export interface QueryParseResult {
/**
* True if parsed in some form, even if there are errors.
*/
parsed: boolean;
errors: SqlRuleError[];
}
export class SqlBucketDescriptor {
name: string;
bucket_parameters?: string[];
constructor(
name: string,
public idSequence: IdSequence
) {
this.name = name;
}
/**
* source table -> queries
*/
data_queries: SqlDataQuery[] = [];
parameter_queries: SqlParameterQuery[] = [];
global_parameter_queries: StaticSqlParameterQuery[] = [];
parameterIdSequence = new IdSequence();
addDataQuery(sql: string, options: SyncRulesOptions): QueryParseResult {
if (this.bucket_parameters == null) {
throw new Error('Bucket parameters must be defined');
}
const dataRows = SqlDataQuery.fromSql(this.name, this.bucket_parameters, sql, options);
dataRows.ruleId = this.idSequence.nextId();
this.data_queries.push(dataRows);
return {
parsed: true,
errors: dataRows.errors
};
}
addParameterQuery(sql: string, options: QueryParseOptions): QueryParseResult {
const parameterQuery = SqlParameterQuery.fromSql(this.name, sql, options);
if (this.bucket_parameters == null) {
this.bucket_parameters = parameterQuery.bucket_parameters;
} else {
if (
new Set([...parameterQuery.bucket_parameters!, ...this.bucket_parameters]).size != this.bucket_parameters.length
) {
throw new Error('Bucket parameters must match for each parameter query within a bucket');
}
}
parameterQuery.id = this.parameterIdSequence.nextId();
if (parameterQuery instanceof SqlParameterQuery) {
this.parameter_queries.push(parameterQuery);
} else {
this.global_parameter_queries.push(parameterQuery);
}
return {
parsed: true,
errors: parameterQuery.errors
};
}
evaluateRow(options: EvaluateRowOptions): EvaluationResult[] {
let results: EvaluationResult[] = [];
for (let query of this.data_queries) {
if (!query.applies(options.sourceTable)) {
continue;
}
results.push(...query.evaluateRow(options.sourceTable, options.record));
}
return results;
}
evaluateParameterRow(sourceTable: SourceTableInterface, row: SqliteRow): EvaluatedParametersResult[] {
let results: EvaluatedParametersResult[] = [];
for (let query of this.parameter_queries) {
if (query.applies(sourceTable)) {
results.push(...query.evaluateParameterRow(row));
}
}
return results;
}
getBucketParameterQuerier(parameters: RequestParameters): BucketParameterQuerier {
const staticBuckets = this.getStaticBucketDescriptions(parameters);
const staticQuerier = {
staticBuckets,
hasDynamicBuckets: false,
parameterQueryLookups: [],
queryDynamicBucketDescriptions: async () => []
} satisfies BucketParameterQuerier;
if (this.parameter_queries.length == 0) {
return staticQuerier;
}
const dynamicQueriers = this.parameter_queries.map((query) => query.getBucketParameterQuerier(parameters));
return mergeBucketParameterQueriers([staticQuerier, ...dynamicQueriers]);
}
getStaticBucketDescriptions(parameters: RequestParameters): BucketDescription[] {
let results: BucketDescription[] = [];
for (let query of this.global_parameter_queries) {
results.push(...query.getStaticBucketDescriptions(parameters));
}
return results;
}
hasDynamicBucketQueries(): boolean {
return this.parameter_queries.length > 0;
}
getSourceTables(): Set<TablePattern> {
let result = new Set<TablePattern>();
for (let query of this.parameter_queries) {
result.add(query.sourceTable!);
}
for (let query of this.data_queries) {
result.add(query.sourceTable!);
}
// Note: No physical tables for global_parameter_queries
return result;
}
tableSyncsData(table: SourceTableInterface): boolean {
for (let query of this.data_queries) {
if (query.applies(table)) {
return true;
}
}
return false;
}
tableSyncsParameters(table: SourceTableInterface): boolean {
for (let query of this.parameter_queries) {
if (query.applies(table)) {
return true;
}
}
return false;
}
}