1
- import { BucketDescription , BucketInclusionReason , ResolvedBucket } from './BucketDescription.js' ;
1
+ import { BucketInclusionReason , ResolvedBucket } from './BucketDescription.js' ;
2
2
import { BucketParameterQuerier , mergeBucketParameterQueriers } from './BucketParameterQuerier.js' ;
3
+ import { BucketSource , BucketSourceType , ResultSetDescription } from './BucketSource.js' ;
4
+ import { ColumnDefinition } from './ExpressionType.js' ;
3
5
import { IdSequence } from './IdSequence.js' ;
4
6
import { SourceTableInterface } from './SourceTableInterface.js' ;
5
7
import { SqlDataQuery } from './SqlDataQuery.js' ;
6
8
import { SqlParameterQuery } from './SqlParameterQuery.js' ;
7
9
import { GetQuerierOptions , SyncRulesOptions } from './SqlSyncRules.js' ;
8
10
import { StaticSqlParameterQuery } from './StaticSqlParameterQuery.js' ;
9
- import { StreamQuery } from './StreamQuery.js' ;
10
11
import { TablePattern } from './TablePattern.js' ;
11
12
import { TableValuedFunctionSqlParameterQuery } from './TableValuedFunctionSqlParameterQuery.js' ;
12
13
import { SqlRuleError } from './errors.js' ;
@@ -16,8 +17,8 @@ import {
16
17
EvaluationResult ,
17
18
QueryParseOptions ,
18
19
RequestParameters ,
19
- SqliteRow ,
20
- StreamParseOptions
20
+ SourceSchema ,
21
+ SqliteRow
21
22
} from './types.js' ;
22
23
23
24
export interface QueryParseResult {
@@ -29,23 +30,20 @@ export interface QueryParseResult {
29
30
errors : SqlRuleError [ ] ;
30
31
}
31
32
32
- export enum SqlBucketDescriptorType {
33
- SYNC_RULE ,
34
- STREAM
35
- }
36
-
37
- export class SqlBucketDescriptor {
33
+ export class SqlBucketDescriptor implements BucketSource {
38
34
name : string ;
39
35
bucketParameters ?: string [ ] ;
40
- type : SqlBucketDescriptorType ;
41
- subscribedToByDefault : boolean ;
42
36
43
- constructor ( name : string , type : SqlBucketDescriptorType ) {
37
+ constructor ( name : string ) {
44
38
this . name = name ;
45
- this . type = type ;
39
+ }
46
40
47
- // Sync-rule style buckets are subscribed to by default, streams are opt-in unless their definition says otherwise.
48
- this . subscribedToByDefault = type == SqlBucketDescriptorType . SYNC_RULE ;
41
+ get type ( ) : BucketSourceType {
42
+ return BucketSourceType . SYNC_RULE ;
43
+ }
44
+
45
+ public get subscribedToByDefault ( ) : boolean {
46
+ return true ;
49
47
}
50
48
51
49
/**
@@ -94,24 +92,6 @@ export class SqlBucketDescriptor {
94
92
} ;
95
93
}
96
94
97
- addUnifiedStreamQuery ( sql : string , options : StreamParseOptions ) : QueryParseResult {
98
- const [ query , errors ] = StreamQuery . fromSql ( this . name , sql , options ) ;
99
- for ( const parameterQuery of query . inferredParameters ) {
100
- if ( parameterQuery instanceof StaticSqlParameterQuery ) {
101
- this . globalParameterQueries . push ( parameterQuery ) ;
102
- } else {
103
- this . parameterQueries . push ( parameterQuery ) ;
104
- }
105
- }
106
- this . dataQueries . push ( query . data ) ;
107
- this . subscribedToByDefault = options . default ?? false ;
108
-
109
- return {
110
- parsed : true ,
111
- errors
112
- } ;
113
- }
114
-
115
95
evaluateRow ( options : EvaluateRowOptions ) : EvaluationResult [ ] {
116
96
let results : EvaluationResult [ ] = [ ] ;
117
97
for ( let query of this . dataQueries ) {
@@ -137,20 +117,16 @@ export class SqlBucketDescriptor {
137
117
/**
138
118
* @deprecated Use `pushBucketParameterQueriers` instead and merge at the top-level.
139
119
*/
140
- getBucketParameterQuerier ( options : GetQuerierOptions , parameters : RequestParameters ) : BucketParameterQuerier {
120
+ getBucketParameterQuerier ( options : GetQuerierOptions ) : BucketParameterQuerier {
141
121
const queriers : BucketParameterQuerier [ ] = [ ] ;
142
- this . pushBucketParameterQueriers ( queriers , options , parameters ) ;
122
+ this . pushBucketParameterQueriers ( queriers , options ) ;
143
123
144
124
return mergeBucketParameterQueriers ( queriers ) ;
145
125
}
146
126
147
- pushBucketParameterQueriers (
148
- result : BucketParameterQuerier [ ] ,
149
- options : GetQuerierOptions ,
150
- parameters : RequestParameters
151
- ) {
152
- const reasons = [ this . bucketInclusionReason ( options ) ] ;
153
- const staticBuckets = this . getStaticBucketDescriptions ( parameters , reasons ) ;
127
+ pushBucketParameterQueriers ( result : BucketParameterQuerier [ ] , options : GetQuerierOptions ) {
128
+ const reasons = [ this . bucketInclusionReason ( ) ] ;
129
+ const staticBuckets = this . getStaticBucketDescriptions ( options . globalParameters , reasons ) ;
154
130
const staticQuerier = {
155
131
staticBuckets,
156
132
hasDynamicBuckets : false ,
@@ -163,7 +139,9 @@ export class SqlBucketDescriptor {
163
139
return ;
164
140
}
165
141
166
- const dynamicQueriers = this . parameterQueries . map ( ( query ) => query . getBucketParameterQuerier ( parameters , reasons ) ) ;
142
+ const dynamicQueriers = this . parameterQueries . map ( ( query ) =>
143
+ query . getBucketParameterQuerier ( options . globalParameters , reasons )
144
+ ) ;
167
145
result . push ( ...dynamicQueriers ) ;
168
146
}
169
147
@@ -198,12 +176,8 @@ export class SqlBucketDescriptor {
198
176
return result ;
199
177
}
200
178
201
- private bucketInclusionReason ( parameters : GetQuerierOptions ) : BucketInclusionReason {
202
- if ( this . type == SqlBucketDescriptorType . STREAM && ! this . subscribedToByDefault ) {
203
- return { subscription : this . name } ;
204
- } else {
205
- return 'default' ;
206
- }
179
+ private bucketInclusionReason ( ) : BucketInclusionReason {
180
+ return 'default' ;
207
181
}
208
182
209
183
tableSyncsData ( table : SourceTableInterface ) : boolean {
@@ -223,4 +197,58 @@ export class SqlBucketDescriptor {
223
197
}
224
198
return false ;
225
199
}
200
+
201
+ resolveResultSets ( schema : SourceSchema , tables : Record < string , Record < string , ColumnDefinition > > ) {
202
+ for ( let query of this . dataQueries ) {
203
+ const outTables = query . getColumnOutputs ( schema ) ;
204
+ for ( let table of outTables ) {
205
+ tables [ table . name ] ??= { } ;
206
+ for ( let column of table . columns ) {
207
+ if ( column . name != 'id' ) {
208
+ tables [ table . name ] [ column . name ] ??= column ;
209
+ }
210
+ }
211
+ }
212
+ }
213
+ }
214
+
215
+ debugWriteOutputTables ( result : Record < string , { query : string } [ ] > ) : void {
216
+ for ( let q of this . dataQueries ) {
217
+ result [ q . table ! ] ??= [ ] ;
218
+ const r = {
219
+ query : q . sql
220
+ } ;
221
+
222
+ result [ q . table ! ] . push ( r ) ;
223
+ }
224
+ }
225
+
226
+ debugRepresentation ( ) {
227
+ let all_parameter_queries = [ ...this . parameterQueries . values ( ) ] . flat ( ) ;
228
+ let all_data_queries = [ ...this . dataQueries . values ( ) ] . flat ( ) ;
229
+ return {
230
+ name : this . name ,
231
+ type : this . type . toString ( ) ,
232
+ bucket_parameters : this . bucketParameters ,
233
+ global_parameter_queries : this . globalParameterQueries . map ( ( q ) => {
234
+ return {
235
+ sql : q . sql
236
+ } ;
237
+ } ) ,
238
+ parameter_queries : all_parameter_queries . map ( ( q ) => {
239
+ return {
240
+ sql : q . sql ,
241
+ table : q . sourceTable ,
242
+ input_parameters : q . inputParameters
243
+ } ;
244
+ } ) ,
245
+ data_queries : all_data_queries . map ( ( q ) => {
246
+ return {
247
+ sql : q . sql ,
248
+ table : q . sourceTable ,
249
+ columns : q . columnOutputNames ( )
250
+ } ;
251
+ } )
252
+ } ;
253
+ }
226
254
}
0 commit comments