@@ -2008,37 +2008,64 @@ impl<'a, Conn> RunQueryDsl<Conn> for FindQuery<'a> {}
20082008
20092009#[ derive( Debug , Clone ) ]
20102010pub struct FindRangeQuery < ' a > {
2011- table : & ' a Table ,
2012- eb_range : EntityBlockRange ,
2011+ tables : & ' a Vec < & ' a Table > ,
2012+ imm_range : EntityBlockRange ,
2013+ mut_range : EntityBlockRange ,
20132014}
20142015
20152016impl < ' a > FindRangeQuery < ' a > {
2016- pub fn new ( table : & ' a Table , block_range : Range < BlockNumber > ) -> Self {
2017- let eb_range = EntityBlockRange :: new ( table. immutable , block_range) ;
2018- Self { table, eb_range }
2017+ pub fn new ( tables : & ' a Vec < & Table > , block_range : Range < BlockNumber > ) -> Self {
2018+ let imm_range = EntityBlockRange :: new ( true , block_range. clone ( ) ) ;
2019+ let mut_range = EntityBlockRange :: new ( false , block_range) ;
2020+ Self {
2021+ tables,
2022+ imm_range,
2023+ mut_range,
2024+ }
20192025 }
20202026}
20212027
20222028impl < ' a > QueryFragment < Pg > for FindRangeQuery < ' a > {
20232029 fn walk_ast < ' b > ( & ' b self , mut out : AstPass < ' _ , ' b , Pg > ) -> QueryResult < ( ) > {
20242030 out. unsafe_to_cache_prepared ( ) ;
20252031
2026- // Generate
2027- // select '..' as entity, to_jsonb(e.*) as data
2028- // from schema.table e where id = $1
2029- out. push_sql ( "select " ) ;
2030- out. push_bind_param :: < Text , _ > ( self . table . object . as_str ( ) ) ?;
2031- out. push_sql ( " as entity, to_jsonb(e.*) as data\n " ) ;
2032- out. push_sql ( " from " ) ;
2033- out. push_sql ( self . table . qualified_name . as_str ( ) ) ;
2034- out. push_sql ( " e\n where " ) ;
2035- // TODO: do we need to care about it?
2036- // if self.table.has_causality_region {
2037- // out.push_sql("causality_region = ");
2038- // out.push_bind_param::<Integer, _>(&self.key.causality_region)?;
2039- // out.push_sql(" and ");
2040- // }
2041- self . eb_range . contains ( & mut out)
2032+ let mut iter = self . tables . iter ( ) . peekable ( ) ;
2033+ while let Some ( table) = iter. next ( ) {
2034+ // Generate
2035+ // select '..' as entity, to_jsonb(e.*) as data, block$ as block_number
2036+ // from schema.table e where id = $1
2037+ out. push_sql ( "select " ) ;
2038+ out. push_bind_param :: < Text , _ > ( table. object . as_str ( ) ) ?;
2039+ out. push_sql ( " as entity, to_jsonb(e.*) as data," ) ;
2040+ if table. immutable {
2041+ self . imm_range . compare_column ( & mut out)
2042+ } else {
2043+ self . mut_range . compare_column ( & mut out)
2044+ }
2045+ out. push_sql ( "as block_number\n " ) ;
2046+ out. push_sql ( " from " ) ;
2047+ out. push_sql ( table. qualified_name . as_str ( ) ) ;
2048+ out. push_sql ( " e\n where" ) ;
2049+ // TODO: do we need to care about it?
2050+ // if self.table.has_causality_region {
2051+ // out.push_sql("causality_region = ");
2052+ // out.push_bind_param::<Integer, _>(&self.key.causality_region)?;
2053+ // out.push_sql(" and ");
2054+ // }
2055+ if table. immutable {
2056+ self . imm_range . contains ( & mut out) ?;
2057+ } else {
2058+ self . mut_range . contains ( & mut out) ?;
2059+ }
2060+ // more elements left?
2061+ if iter. peek ( ) . is_some ( ) {
2062+ out. push_sql ( "\n union all\n " ) ; // with the next
2063+ } else {
2064+ out. push_sql ( "\n order by block_number" ) ; // on the last
2065+ }
2066+ }
2067+
2068+ Ok ( ( ) )
20422069 }
20432070}
20442071
0 commit comments