1- import { BucketDescription , BucketInclusionReason , ResolvedBucket } from './BucketDescription.js' ;
1+ import { BucketInclusionReason , ResolvedBucket } from './BucketDescription.js' ;
22import { BucketParameterQuerier , mergeBucketParameterQueriers } from './BucketParameterQuerier.js' ;
3+ import { BucketSource , BucketSourceType , ResultSetDescription } from './BucketSource.js' ;
4+ import { ColumnDefinition } from './ExpressionType.js' ;
35import { IdSequence } from './IdSequence.js' ;
46import { SourceTableInterface } from './SourceTableInterface.js' ;
57import { SqlDataQuery } from './SqlDataQuery.js' ;
68import { SqlParameterQuery } from './SqlParameterQuery.js' ;
79import { GetQuerierOptions , SyncRulesOptions } from './SqlSyncRules.js' ;
810import { StaticSqlParameterQuery } from './StaticSqlParameterQuery.js' ;
9- import { StreamQuery } from './StreamQuery.js' ;
1011import { TablePattern } from './TablePattern.js' ;
1112import { TableValuedFunctionSqlParameterQuery } from './TableValuedFunctionSqlParameterQuery.js' ;
1213import { SqlRuleError } from './errors.js' ;
@@ -16,8 +17,8 @@ import {
1617 EvaluationResult ,
1718 QueryParseOptions ,
1819 RequestParameters ,
19- SqliteRow ,
20- StreamParseOptions
20+ SourceSchema ,
21+ SqliteRow
2122} from './types.js' ;
2223
2324export interface QueryParseResult {
@@ -29,23 +30,20 @@ export interface QueryParseResult {
2930 errors : SqlRuleError [ ] ;
3031}
3132
32- export enum SqlBucketDescriptorType {
33- SYNC_RULE ,
34- STREAM
35- }
36-
37- export class SqlBucketDescriptor {
33+ export class SqlBucketDescriptor implements BucketSource {
3834 name : string ;
3935 bucketParameters ?: string [ ] ;
40- type : SqlBucketDescriptorType ;
41- subscribedToByDefault : boolean ;
4236
43- constructor ( name : string , type : SqlBucketDescriptorType ) {
37+ constructor ( name : string ) {
4438 this . name = name ;
45- this . type = type ;
39+ }
4640
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 ;
4947 }
5048
5149 /**
@@ -94,24 +92,6 @@ export class SqlBucketDescriptor {
9492 } ;
9593 }
9694
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-
11595 evaluateRow ( options : EvaluateRowOptions ) : EvaluationResult [ ] {
11696 let results : EvaluationResult [ ] = [ ] ;
11797 for ( let query of this . dataQueries ) {
@@ -137,20 +117,16 @@ export class SqlBucketDescriptor {
137117 /**
138118 * @deprecated Use `pushBucketParameterQueriers` instead and merge at the top-level.
139119 */
140- getBucketParameterQuerier ( options : GetQuerierOptions , parameters : RequestParameters ) : BucketParameterQuerier {
120+ getBucketParameterQuerier ( options : GetQuerierOptions ) : BucketParameterQuerier {
141121 const queriers : BucketParameterQuerier [ ] = [ ] ;
142- this . pushBucketParameterQueriers ( queriers , options , parameters ) ;
122+ this . pushBucketParameterQueriers ( queriers , options ) ;
143123
144124 return mergeBucketParameterQueriers ( queriers ) ;
145125 }
146126
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 ) ;
154130 const staticQuerier = {
155131 staticBuckets,
156132 hasDynamicBuckets : false ,
@@ -163,7 +139,9 @@ export class SqlBucketDescriptor {
163139 return ;
164140 }
165141
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+ ) ;
167145 result . push ( ...dynamicQueriers ) ;
168146 }
169147
@@ -198,12 +176,8 @@ export class SqlBucketDescriptor {
198176 return result ;
199177 }
200178
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' ;
207181 }
208182
209183 tableSyncsData ( table : SourceTableInterface ) : boolean {
@@ -223,4 +197,58 @@ export class SqlBucketDescriptor {
223197 }
224198 return false ;
225199 }
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+ }
226254}
0 commit comments