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 fc4af81 commit d62e09f
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 @@ -549,7 +549,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 @@ -1117,12 +1116,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 @@ -1139,13 +1138,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 @@ -1211,12 +1210,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 @@ -1492,14 +1491,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 @@ -1546,7 +1545,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 @@ -1586,7 +1585,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 @@ -1595,7 +1594,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 @@ -1651,7 +1650,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 @@ -1708,7 +1707,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 @@ -1888,7 +1887,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 @@ -1996,7 +1995,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 @@ -2085,17 +2084,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 @@ -2189,7 +2188,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 @@ -2214,20 +2213,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 @@ -2250,7 +2249,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 @@ -2268,7 +2267,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 @@ -2293,19 +2292,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 @@ -2323,7 +2322,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 @@ -2334,7 +2333,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 @@ -2378,10 +2377,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 @@ -2402,7 +2401,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 @@ -2420,7 +2419,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 @@ -2443,7 +2442,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 @@ -2485,7 +2484,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 @@ -3658,14 +3657,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 @@ -3705,7 +3708,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 @@ -3867,7 +3870,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 @@ -3938,7 +3941,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 d62e09f

Please sign in to comment.