Skip to content

Commit

Permalink
more lifetime
Browse files Browse the repository at this point in the history
  • Loading branch information
zorancv committed Nov 29, 2023
1 parent e29c0b1 commit ab692ae
Showing 1 changed file with 51 additions and 48 deletions.
99 changes: 51 additions & 48 deletions store/postgres/src/relational_queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,6 @@ struct QueryValue<'a>(&'a Value, &'a ColumnType);

impl<'a> QueryFragment<Pg> for QueryValue<'a> {
fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, Pg>) -> QueryResult<()> {
let out = &mut out;
out.unsafe_to_cache_prepared();
let column_type = self.1;

Expand Down Expand Up @@ -1105,12 +1104,12 @@ impl<'a> QueryFilter<'a> {
.expect("the constructor already checked that all attribute names are valid")
}

fn binary_op(
&self,
filters: &[EntityFilter],
fn binary_op<'b>(
&'b self,
filters: &'b [EntityFilter],
op: &str,
on_empty: &str,
mut out: AstPass<Pg>,
mut out: AstPass<'_, 'b, Pg>,
) -> QueryResult<()> {
if !filters.is_empty() {
out.push_sql("(");
Expand All @@ -1127,13 +1126,13 @@ impl<'a> QueryFilter<'a> {
Ok(())
}

fn contains(
&self,
fn contains<'b>(
&'b self,
attribute: &Attribute,
value: &Value,
value: &'b Value,
negated: bool,
strict: bool,
mut out: AstPass<Pg>,
mut out: AstPass<'_, 'b, Pg>,
) -> QueryResult<()> {
let column = self.column(attribute);
let operation = match (strict, negated) {
Expand Down Expand Up @@ -1199,12 +1198,12 @@ impl<'a> QueryFilter<'a> {
Ok(())
}

fn equals(
&self,
fn equals<'b>(
&'b self,
attribute: &Attribute,
value: &Value,
op: Comparison,
mut out: AstPass<Pg>,
mut out: AstPass<'_, 'b, Pg>,
) -> QueryResult<()> {
let column = self.column(attribute);

Expand Down Expand Up @@ -1480,14 +1479,14 @@ pub struct FindQuery<'a> {
}

impl<'a> QueryFragment<Pg> for FindQuery<'a> {
fn walk_ast(&self, mut out: AstPass<Pg>) -> QueryResult<()> {
fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, Pg>) -> QueryResult<()> {
out.unsafe_to_cache_prepared();

// Generate
// select '..' as entity, to_jsonb(e.*) as data
// from schema.table e where id = $1
out.push_sql("select ");
out.push_bind_param::<Text, _>(&self.table.object.as_str())?;
out.push_bind_param::<Text, _>(self.table.object.as_str())?;
out.push_sql(" as entity, to_jsonb(e.*) as data\n");
out.push_sql(" from ");
out.push_sql(self.table.qualified_name.as_str());
Expand Down Expand Up @@ -1534,7 +1533,7 @@ impl<'a> QueryFragment<Pg> for FindChangesQuery<'a> {
out.push_sql("\nunion all\n");
}
out.push_sql("select ");
out.push_bind_param::<Text, _>(&table.object.as_str())?;
out.push_bind_param::<Text, _>(table.object.as_str())?;
out.push_sql(" as entity, to_jsonb(e.*) as data\n");
out.push_sql(" from ");
out.push_sql(table.qualified_name.as_str());
Expand Down Expand Up @@ -1574,7 +1573,7 @@ pub struct FindPossibleDeletionsQuery<'a> {
}

impl<'a> QueryFragment<Pg> for FindPossibleDeletionsQuery<'a> {
fn walk_ast(&self, mut out: AstPass<Pg>) -> QueryResult<()> {
fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, Pg>) -> QueryResult<()> {
let out = &mut out;
out.unsafe_to_cache_prepared();

Expand All @@ -1583,7 +1582,7 @@ impl<'a> QueryFragment<Pg> for FindPossibleDeletionsQuery<'a> {
out.push_sql("\nunion all\n");
}
out.push_sql("select ");
out.push_bind_param::<Text, _>(&table.object.as_str())?;
out.push_bind_param::<Text, _>(table.object.as_str())?;
out.push_sql(" as entity, ");
if table.has_causality_region {
out.push_sql("causality_region, ");
Expand Down Expand Up @@ -1639,7 +1638,7 @@ impl<'a> QueryFragment<Pg> for FindManyQuery<'a> {
out.push_sql("\nunion all\n");
}
out.push_sql("select ");
out.push_bind_param::<Text, _>(&table.object.as_str())?;
out.push_bind_param::<Text, _>(table.object.as_str())?;
out.push_sql(" as entity, to_jsonb(e.*) as data\n");
out.push_sql(" from ");
out.push_sql(table.qualified_name.as_str());
Expand Down Expand Up @@ -1696,7 +1695,7 @@ impl<'a> QueryFragment<Pg> for FindDerivedQuery<'a> {
// select '..' as entity, to_jsonb(e.*) as data
// from schema.table e where field = $1
out.push_sql("select ");
out.push_bind_param::<Text, _>(&self.table.object.as_str())?;
out.push_bind_param::<Text, _>(self.table.object.as_str())?;
out.push_sql(" as entity, to_jsonb(e.*) as data\n");
out.push_sql(" from ");
out.push_sql(self.table.qualified_name.as_str());
Expand Down Expand Up @@ -1876,7 +1875,7 @@ impl<'a> InsertQuery<'a> {
}

impl<'a> QueryFragment<Pg> for InsertQuery<'a> {
fn walk_ast(&self, mut out: AstPass<Pg>) -> QueryResult<()> {
fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, Pg>) -> QueryResult<()> {
let out = &mut out;
out.unsafe_to_cache_prepared();

Expand Down Expand Up @@ -1984,7 +1983,7 @@ impl<'a> QueryFragment<Pg> for ConflictingEntityQuery<'a> {
out.push_sql("\nunion all\n");
}
out.push_sql("select ");
out.push_bind_param::<Text, _>(&table.object.as_str())?;
out.push_bind_param::<Text, _>(table.object.as_str())?;
out.push_sql(" as entity from ");
out.push_sql(table.qualified_name.as_str());
out.push_sql(" where id = ");
Expand Down Expand Up @@ -2073,17 +2072,17 @@ enum ParentLimit<'a> {
}

impl<'a> ParentLimit<'a> {
fn filter(&self, mut out: AstPass<'_, 'a, Pg>) {
fn filter(&self, out: &mut AstPass<'_, 'a, Pg>) {
match self {
ParentLimit::Outer => out.push_sql(" and q.id = p.id"),
ParentLimit::Ranked(_, _) => (),
}
}

fn restrict<'b>(&'b self, mut out: AstPass<'_, 'b, Pg>) -> QueryResult<()> {
fn restrict(&'a self, out: &mut AstPass<'_, 'a, Pg>) -> QueryResult<()> {
if let ParentLimit::Ranked(sort_key, range) = self {
out.push_sql(" ");
sort_key.order_by(&mut out, false)?;
sort_key.order_by(out, false)?;
range.walk_ast(out.reborrow())?;
}
Ok(())
Expand Down Expand Up @@ -2177,7 +2176,7 @@ impl<'a> FilterWindow<'a> {
fn children_type_a<'b>(
&'b self,
column: &Column,
limit: ParentLimit<'b>,
limit: &'b ParentLimit<'_>,
block: BlockNumber,
out: &mut AstPass<'_, 'b, Pg>,
) -> QueryResult<()> {
Expand All @@ -2202,20 +2201,20 @@ impl<'a> FilterWindow<'a> {
out.push_sql(self.table.qualified_name.as_str());
out.push_sql(" c where ");
BlockRangeColumn::new(self.table, "c.", block).contains(out, false)?;
limit.filter(out.reborrow());
limit.filter(out);
out.push_sql(" and p.id = any(c.");
out.push_identifier(column.name.as_str())?;
out.push_sql(")");
self.and_filter(out)?;
limit.restrict(out.reborrow())?;
limit.restrict(out)?;
out.push_sql(") c");
Ok(())
}

fn child_type_a<'b>(
&'b self,
column: &Column,
limit: ParentLimit<'b>,
limit: &'b ParentLimit<'_>,
block: BlockNumber,
out: &mut AstPass<'_, 'b, Pg>,
) -> QueryResult<()> {
Expand All @@ -2238,7 +2237,7 @@ impl<'a> FilterWindow<'a> {
out.push_sql(self.table.qualified_name.as_str());
out.push_sql(" c where ");
BlockRangeColumn::new(self.table, "c.", block).contains(out, false)?;
limit.filter(out.reborrow());
limit.filter(out);
out.push_sql(" and c.");
out.push_identifier(column.name.as_str())?;
out.push_sql(" @> array[p.id]");
Expand All @@ -2256,7 +2255,7 @@ impl<'a> FilterWindow<'a> {
fn children_type_b<'b>(
&'b self,
column: &Column,
limit: ParentLimit<'b>,
limit: &'b ParentLimit<'_>,
block: BlockNumber,
out: &mut AstPass<'_, 'b, Pg>,
) -> QueryResult<()> {
Expand All @@ -2281,19 +2280,19 @@ impl<'a> FilterWindow<'a> {
out.push_sql(self.table.qualified_name.as_str());
out.push_sql(" c where ");
BlockRangeColumn::new(self.table, "c.", block).contains(out, false)?;
limit.filter(out.reborrow());
limit.filter(out);
out.push_sql(" and p.id = c.");
out.push_identifier(column.name.as_str())?;
self.and_filter(out)?;
limit.restrict(out.reborrow())?;
limit.restrict(out)?;
out.push_sql(") c");
Ok(())
}

fn child_type_b<'b>(
&'b self,
column: &Column,
limit: ParentLimit<'b>,
limit: &'b ParentLimit<'_>,
block: BlockNumber,
out: &mut AstPass<'_, 'b, Pg>,
) -> QueryResult<()> {
Expand All @@ -2311,7 +2310,7 @@ impl<'a> FilterWindow<'a> {
out.push_sql(self.table.qualified_name.as_str());
out.push_sql(" c where ");
BlockRangeColumn::new(self.table, "c.", block).contains(out, false)?;
limit.filter(out.reborrow());
limit.filter(out);
out.push_sql(" and p.id = c.");
out.push_identifier(column.name.as_str())?;
self.and_filter(out)?;
Expand All @@ -2322,7 +2321,7 @@ impl<'a> FilterWindow<'a> {
fn children_type_c<'b>(
&'b self,
child_ids: &'b [IdList],
limit: ParentLimit<'b>,
limit: &'b ParentLimit<'_>,
block: BlockNumber,
out: &mut AstPass<'_, 'b, Pg>,
) -> QueryResult<()> {
Expand Down Expand Up @@ -2366,10 +2365,10 @@ impl<'a> FilterWindow<'a> {
out.push_sql(self.table.qualified_name.as_str());
out.push_sql(" c where ");
BlockRangeColumn::new(self.table, "c.", block).contains(out, true);
limit.filter(out.reborrow());
limit.filter(out);
out.push_sql(" and c.id = any(p.child_ids)");
self.and_filter(out)?;
limit.restrict(out.reborrow())?;
limit.restrict(out)?;
out.push_sql(") c");
} else {
// Generate
Expand All @@ -2390,7 +2389,7 @@ impl<'a> FilterWindow<'a> {
fn child_type_d<'b>(
&'b self,
child_ids: &'b IdList,
limit: ParentLimit<'b>,
limit: &'b ParentLimit<'_>,
block: BlockNumber,
out: &mut AstPass<'_, 'b, Pg>,
) -> QueryResult<()> {
Expand All @@ -2408,7 +2407,7 @@ impl<'a> FilterWindow<'a> {
out.push_sql(self.table.qualified_name.as_str());
out.push_sql(" c where ");
BlockRangeColumn::new(self.table, "c.", block).contains(out, true)?;
limit.filter(out.reborrow());
limit.filter(out);

// Include a constraint on the child IDs as a set if the size of the set
// is below the threshold set by environment variable. Set it to
Expand All @@ -2431,7 +2430,7 @@ impl<'a> FilterWindow<'a> {

fn children<'b>(
&'b self,
limit: ParentLimit<'b>,
limit: &'b ParentLimit<'_>,
block: BlockNumber,
out: &mut AstPass<'_, 'b, Pg>,
) -> QueryResult<()> {
Expand Down Expand Up @@ -2473,7 +2472,7 @@ impl<'a> FilterWindow<'a> {
out.push_sql("' as entity, c.id, c.vid, p.id::text as ");
out.push_sql(&*PARENT_ID);
sort_key.select(&mut out, SelectStatementLevel::InnerStatement)?;
self.children(ParentLimit::Outer, block, &mut out)
self.children(&ParentLimit::Outer, block, &mut out)
}

/// Collect all the parent id's from all windows
Expand Down Expand Up @@ -3646,14 +3645,18 @@ impl<'a> SortKey<'a> {
Ok(())
}

fn add_child(&self, block: BlockNumber, out: &mut AstPass<Pg>) -> QueryResult<()> {
fn add(
block: BlockNumber,
fn add_child<'b>(
&self,
block: &'b BlockNumber,
out: &mut AstPass<'_, 'b, Pg>,
) -> QueryResult<()> {
fn add<'b>(
block: &'b BlockNumber,
child_table: &Table,
child_column: &Column,
parent_column: &Column,
prefix: &str,
out: &mut AstPass<Pg>,
out: &mut AstPass<'_, 'b, Pg>,
) -> QueryResult<()> {
out.push_sql(" left join ");
out.push_sql(child_table.qualified_name.as_str());
Expand Down Expand Up @@ -3693,7 +3696,7 @@ impl<'a> SortKey<'a> {
out.push_sql(".");
out.push_identifier(BLOCK_RANGE_COLUMN)?;
out.push_sql(" @> ");
out.push_bind_param::<Integer, _>(&block)?;
out.push_bind_param::<Integer, _>(block)?;
out.push_sql(") ");

Ok(())
Expand Down Expand Up @@ -3855,7 +3858,7 @@ impl<'a> FilterQuery<'a> {
out.push_sql(table.qualified_name.as_str());
out.push_sql(" c");

self.sort_key.add_child(self.block, out)?;
self.sort_key.add_child(&self.block, out)?;

out.push_sql("\n where ");

Expand Down Expand Up @@ -3926,7 +3929,7 @@ impl<'a> FilterQuery<'a> {
out.push_sql("select c.*, p.id::text as ");
out.push_sql(&*PARENT_ID);
window.children(
ParentLimit::Ranked(&self.sort_key, &self.range),
&ParentLimit::Ranked(&self.sort_key, &self.range),
self.block,
&mut out,
)?;
Expand Down

0 comments on commit ab692ae

Please sign in to comment.