@@ -132,6 +132,66 @@ impl<'a> QueryFragment<Pg> for BlockRangeUpperBoundClause<'a> {
132132 }
133133}
134134
135+ /// Helper for generating SQL fragments for selecting entities in a specific block range
136+ #[ derive( Debug , Clone , Copy ) ]
137+ pub enum EntityBlockRange {
138+ Mutable ( BlockRange ) , // TODO: check if this is a proper type here (maybe Range<BlockNumber>?)
139+ Immutable ( BlockRange ) ,
140+ }
141+
142+ impl EntityBlockRange {
143+ pub fn new ( immutable : bool , block_range : std:: ops:: Range < u32 > ) -> Self {
144+ let st: Bound < BlockNumber > = Bound :: Included ( block_range. start . try_into ( ) . unwrap ( ) ) ;
145+ let en: Bound < BlockNumber > = Bound :: Excluded ( block_range. end . try_into ( ) . unwrap ( ) ) ;
146+ let block_range: BlockRange = BlockRange ( st, en) ;
147+ if immutable {
148+ Self :: Immutable ( block_range)
149+ } else {
150+ Self :: Mutable ( block_range)
151+ }
152+ }
153+
154+ /// Output SQL that matches only rows whose block range contains `block`.
155+ pub fn contains < ' b > ( & ' b self , out : & mut AstPass < ' _ , ' b , Pg > ) -> QueryResult < ( ) > {
156+ out. unsafe_to_cache_prepared ( ) ;
157+ let block_range = match self {
158+ EntityBlockRange :: Mutable ( br) => br,
159+ EntityBlockRange :: Immutable ( br) => br,
160+ } ;
161+ let BlockRange ( start, finish) = block_range;
162+
163+ self . compare_column ( out) ;
164+ out. push_sql ( " >= " ) ;
165+ match start {
166+ Bound :: Included ( block) => out. push_bind_param :: < Integer , _ > ( block) ?,
167+ Bound :: Excluded ( block) => {
168+ out. push_bind_param :: < Integer , _ > ( block) ?;
169+ out. push_sql ( "+1" ) ;
170+ }
171+ Bound :: Unbounded => unimplemented ! ( ) ,
172+ } ;
173+ out. push_sql ( " AND " ) ;
174+ self . compare_column ( out) ;
175+ out. push_sql ( " <= " ) ;
176+ match finish {
177+ Bound :: Included ( block) => {
178+ out. push_bind_param :: < Integer , _ > ( block) ?;
179+ out. push_sql ( "+1" ) ;
180+ }
181+ Bound :: Excluded ( block) => out. push_bind_param :: < Integer , _ > ( block) ?,
182+ Bound :: Unbounded => unimplemented ! ( ) ,
183+ } ;
184+ Ok ( ( ) )
185+ }
186+
187+ pub fn compare_column ( & self , out : & mut AstPass < Pg > ) {
188+ match self {
189+ EntityBlockRange :: Mutable ( _) => out. push_sql ( " lower(block_range) " ) ,
190+ EntityBlockRange :: Immutable ( _) => out. push_sql ( " block$ " ) ,
191+ }
192+ }
193+ }
194+
135195/// Helper for generating various SQL fragments for handling the block range
136196/// of entity versions
137197#[ allow( unused) ]
@@ -147,16 +207,6 @@ pub enum BlockRangeColumn<'a> {
147207 table_prefix : & ' a str ,
148208 block : BlockNumber ,
149209 } ,
150- MutableRange {
151- table : & ' a Table ,
152- table_prefix : & ' a str ,
153- block_range : BlockRange , // TODO: check if this is a proper type here (maybe Range<BlockNumber>?)
154- } ,
155- ImmutableRange {
156- table : & ' a Table ,
157- table_prefix : & ' a str ,
158- block_range : BlockRange ,
159- } ,
160210}
161211
162212impl < ' a > BlockRangeColumn < ' a > {
@@ -176,35 +226,10 @@ impl<'a> BlockRangeColumn<'a> {
176226 }
177227 }
178228
179- // TODO: refactor new and new2 into one. use enum of both BlockNumber and range
180- pub fn new2 (
181- table : & ' a Table ,
182- table_prefix : & ' a str ,
183- block_range : std:: ops:: Range < u32 > ,
184- ) -> Self {
185- let st: Bound < BlockNumber > = Bound :: Included ( block_range. start . try_into ( ) . unwrap ( ) ) ;
186- let en: Bound < BlockNumber > = Bound :: Excluded ( block_range. end . try_into ( ) . unwrap ( ) ) ;
187- let block_range: BlockRange = BlockRange ( st, en) ;
188- if table. immutable {
189- Self :: ImmutableRange {
190- table,
191- table_prefix,
192- block_range,
193- }
194- } else {
195- Self :: MutableRange {
196- table,
197- table_prefix,
198- block_range,
199- }
200- }
201- }
202-
203229 pub fn block ( & self ) -> BlockNumber {
204230 match self {
205231 BlockRangeColumn :: Mutable { block, .. } => * block,
206232 BlockRangeColumn :: Immutable { block, .. } => * block,
207- _ => todo ! ( ) ,
208233 }
209234 }
210235}
@@ -259,64 +284,13 @@ impl<'a> BlockRangeColumn<'a> {
259284 out. push_bind_param :: < Integer , _ > ( block)
260285 }
261286 }
262- BlockRangeColumn :: MutableRange {
263- table : _,
264- table_prefix : _,
265- block_range : BlockRange ( start, finish) ,
266- } => {
267- out. push_sql ( "lower(block_range) >= " ) ;
268- match start {
269- Bound :: Included ( block) => out. push_bind_param :: < Integer , _ > ( block) ?,
270- Bound :: Excluded ( block) => {
271- out. push_bind_param :: < Integer , _ > ( block) ?;
272- out. push_sql ( "+1" ) ;
273- }
274- Bound :: Unbounded => todo ! ( ) ,
275- } ;
276- out. push_sql ( " AND lower(block_range) <= " ) ;
277- match finish {
278- Bound :: Included ( block) => {
279- out. push_bind_param :: < Integer , _ > ( block) ?;
280- out. push_sql ( "+1" ) ;
281- }
282- Bound :: Excluded ( block) => out. push_bind_param :: < Integer , _ > ( block) ?,
283- Bound :: Unbounded => todo ! ( ) ,
284- } ;
285- Ok ( ( ) )
286- }
287- BlockRangeColumn :: ImmutableRange {
288- table : _,
289- table_prefix : _,
290- block_range : BlockRange ( start, finish) ,
291- } => {
292- out. push_sql ( "block$ >= " ) ;
293- match start {
294- Bound :: Included ( block) => out. push_bind_param :: < Integer , _ > ( block) ?,
295- Bound :: Excluded ( block) => {
296- out. push_bind_param :: < Integer , _ > ( block) ?;
297- out. push_sql ( "+1" ) ;
298- }
299- Bound :: Unbounded => todo ! ( ) ,
300- } ;
301- out. push_sql ( " AND block$ <= " ) ;
302- match finish {
303- Bound :: Included ( block) => {
304- out. push_bind_param :: < Integer , _ > ( block) ?;
305- out. push_sql ( "+1" ) ;
306- }
307- Bound :: Excluded ( block) => out. push_bind_param :: < Integer , _ > ( block) ?,
308- Bound :: Unbounded => todo ! ( ) ,
309- } ;
310- Ok ( ( ) )
311- }
312287 }
313288 }
314289
315290 pub fn column_name ( & self ) -> & str {
316291 match self {
317292 BlockRangeColumn :: Mutable { .. } => BLOCK_RANGE_COLUMN ,
318293 BlockRangeColumn :: Immutable { .. } => BLOCK_COLUMN ,
319- _ => todo ! ( ) ,
320294 }
321295 }
322296
@@ -331,7 +305,6 @@ impl<'a> BlockRangeColumn<'a> {
331305 out. push_sql ( table_prefix) ;
332306 out. push_sql ( BLOCK_COLUMN ) ;
333307 }
334- _ => todo ! ( ) ,
335308 }
336309 }
337310
@@ -341,7 +314,6 @@ impl<'a> BlockRangeColumn<'a> {
341314 match self {
342315 BlockRangeColumn :: Mutable { .. } => out. push_sql ( BLOCK_RANGE_CURRENT ) ,
343316 BlockRangeColumn :: Immutable { .. } => out. push_sql ( "true" ) ,
344- _ => todo ! ( ) ,
345317 }
346318 }
347319
@@ -365,7 +337,6 @@ impl<'a> BlockRangeColumn<'a> {
365337 BlockRangeColumn :: Immutable { .. } => {
366338 unreachable ! ( "immutable entities can not be updated or deleted" )
367339 }
368- _ => todo ! ( ) ,
369340 }
370341 }
371342
@@ -374,7 +345,6 @@ impl<'a> BlockRangeColumn<'a> {
374345 match self {
375346 BlockRangeColumn :: Mutable { .. } => out. push_sql ( BLOCK_RANGE_COLUMN ) ,
376347 BlockRangeColumn :: Immutable { .. } => out. push_sql ( BLOCK_COLUMN ) ,
377- _ => todo ! ( ) ,
378348 }
379349 }
380350
@@ -393,7 +363,6 @@ impl<'a> BlockRangeColumn<'a> {
393363 out. push_sql ( " >= " ) ;
394364 out. push_bind_param :: < Integer , _ > ( block)
395365 }
396- _ => todo ! ( ) ,
397366 }
398367 }
399368}
0 commit comments