Skip to content

Commit

Permalink
Update AST builder ExprNode to contain borrowed values, (#961)
Browse files Browse the repository at this point in the history
Change ExprNode to ExprNode<'a>.
Update ExprNode::Expr to contain Cow<..>.
Apply corresponding changes - mainly updating select nodes to have lifetimes
  • Loading branch information
panarch authored Oct 20, 2022
1 parent a7529f2 commit 3557d48
Show file tree
Hide file tree
Showing 37 changed files with 754 additions and 698 deletions.
10 changes: 5 additions & 5 deletions core/src/ast_builder/assignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ use crate::{
};

#[derive(Clone)]
pub enum AssignmentNode {
Expr(String, ExprNode),
pub enum AssignmentNode<'a> {
Expr(String, ExprNode<'a>),
Text(String),
}

impl From<&str> for AssignmentNode {
impl<'a> From<&str> for AssignmentNode<'a> {
fn from(expr: &str) -> Self {
Self::Text(expr.to_owned())
}
}

impl TryFrom<AssignmentNode> for Assignment {
impl<'a> TryFrom<AssignmentNode<'a>> for Assignment {
type Error = Error;

fn try_from(node: AssignmentNode) -> Result<Self> {
fn try_from(node: AssignmentNode<'a>) -> Result<Self> {
match node {
AssignmentNode::Text(expr) => {
let expr = parse_assignment(expr)
Expand Down
10 changes: 5 additions & 5 deletions core/src/ast_builder/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,27 @@ use {
};

#[derive(Clone)]
pub struct DeleteNode {
pub struct DeleteNode<'a> {
table_name: String,
filter_expr: Option<ExprNode>,
filter_expr: Option<ExprNode<'a>>,
}

impl DeleteNode {
impl<'a> DeleteNode<'a> {
pub fn new(table_name: String) -> Self {
Self {
table_name,
filter_expr: None,
}
}

pub fn filter<T: Into<ExprNode>>(mut self, expr: T) -> Self {
pub fn filter<T: Into<ExprNode<'a>>>(mut self, expr: T) -> Self {
self.filter_expr = Some(expr.into());

self
}
}

impl Build for DeleteNode {
impl<'a> Build for DeleteNode<'a> {
fn build(self) -> Result<Statement> {
let table_name = self.table_name;
let selection = self.filter_expr.map(Expr::try_from).transpose()?;
Expand Down
50 changes: 25 additions & 25 deletions core/src/ast_builder/expr/aggregate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,38 @@ use {
};

#[derive(Clone)]
pub enum AggregateNode {
Count(CountArgExprNode),
Sum(ExprNode),
Min(ExprNode),
Max(ExprNode),
Avg(ExprNode),
Variance(ExprNode),
Stdev(ExprNode),
pub enum AggregateNode<'a> {
Count(CountArgExprNode<'a>),
Sum(ExprNode<'a>),
Min(ExprNode<'a>),
Max(ExprNode<'a>),
Avg(ExprNode<'a>),
Variance(ExprNode<'a>),
Stdev(ExprNode<'a>),
}

#[derive(Clone)]
pub enum CountArgExprNode {
pub enum CountArgExprNode<'a> {
Text(String),
Expr(ExprNode),
Expr(ExprNode<'a>),
}

impl From<&str> for CountArgExprNode {
impl<'a> From<&'a str> for CountArgExprNode<'a> {
fn from(count_arg_str: &str) -> Self {
Self::Text(count_arg_str.to_owned())
}
}

impl From<ExprNode> for CountArgExprNode {
fn from(expr_node: ExprNode) -> Self {
impl<'a> From<ExprNode<'a>> for CountArgExprNode<'a> {
fn from(expr_node: ExprNode<'a>) -> Self {
Self::Expr(expr_node)
}
}

impl TryFrom<CountArgExprNode> for CountArgExpr {
impl<'a> TryFrom<CountArgExprNode<'a>> for CountArgExpr {
type Error = Error;

fn try_from(count_expr_node: CountArgExprNode) -> Result<Self> {
fn try_from(count_expr_node: CountArgExprNode<'a>) -> Result<Self> {
match count_expr_node {
CountArgExprNode::Text(s) if &s == "*" => Ok(CountArgExpr::Wildcard),
CountArgExprNode::Text(s) => {
Expand All @@ -53,10 +53,10 @@ impl TryFrom<CountArgExprNode> for CountArgExpr {
}
}

impl TryFrom<AggregateNode> for Aggregate {
impl<'a> TryFrom<AggregateNode<'a>> for Aggregate {
type Error = Error;

fn try_from(aggr_node: AggregateNode) -> Result<Self> {
fn try_from(aggr_node: AggregateNode<'a>) -> Result<Self> {
match aggr_node {
AggregateNode::Count(count_arg_expr_node) => {
count_arg_expr_node.try_into().map(Aggregate::Count)
Expand All @@ -71,7 +71,7 @@ impl TryFrom<AggregateNode> for Aggregate {
}
}

impl ExprNode {
impl<'a> ExprNode<'a> {
pub fn count(self) -> Self {
count(self)
}
Expand Down Expand Up @@ -101,31 +101,31 @@ impl ExprNode {
}
}

pub fn count<T: Into<CountArgExprNode>>(expr: T) -> ExprNode {
pub fn count<'a, T: Into<CountArgExprNode<'a>>>(expr: T) -> ExprNode<'a> {
ExprNode::Aggregate(Box::new(AggregateNode::Count(expr.into())))
}

pub fn sum<T: Into<ExprNode>>(expr: T) -> ExprNode {
pub fn sum<'a, T: Into<ExprNode<'a>>>(expr: T) -> ExprNode<'a> {
ExprNode::Aggregate(Box::new(AggregateNode::Sum(expr.into())))
}

pub fn min<T: Into<ExprNode>>(expr: T) -> ExprNode {
pub fn min<'a, T: Into<ExprNode<'a>>>(expr: T) -> ExprNode<'a> {
ExprNode::Aggregate(Box::new(AggregateNode::Min(expr.into())))
}

pub fn max<T: Into<ExprNode>>(expr: T) -> ExprNode {
pub fn max<'a, T: Into<ExprNode<'a>>>(expr: T) -> ExprNode<'a> {
ExprNode::Aggregate(Box::new(AggregateNode::Max(expr.into())))
}

pub fn avg<T: Into<ExprNode>>(expr: T) -> ExprNode {
pub fn avg<'a, T: Into<ExprNode<'a>>>(expr: T) -> ExprNode<'a> {
ExprNode::Aggregate(Box::new(AggregateNode::Avg(expr.into())))
}

pub fn variance<T: Into<ExprNode>>(expr: T) -> ExprNode {
pub fn variance<'a, T: Into<ExprNode<'a>>>(expr: T) -> ExprNode<'a> {
ExprNode::Aggregate(Box::new(AggregateNode::Variance(expr.into())))
}

pub fn stdev<T: Into<ExprNode>>(expr: T) -> ExprNode {
pub fn stdev<'a, T: Into<ExprNode<'a>>>(expr: T) -> ExprNode<'a> {
ExprNode::Aggregate(Box::new(AggregateNode::Stdev(expr.into())))
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/ast_builder/expr/between.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::ExprNode;

impl ExprNode {
impl<'a> ExprNode<'a> {
pub fn between<T: Into<Self>, U: Into<Self>>(self, low: T, high: U) -> Self {
Self::Between {
expr: Box::new(self),
Expand Down
2 changes: 1 addition & 1 deletion core/src/ast_builder/expr/binary_op.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use {super::ExprNode, crate::ast::BinaryOperator};

impl ExprNode {
impl<'a> ExprNode<'a> {
fn binary_op<T: Into<Self>>(self, op: BinaryOperator, other: T) -> Self {
Self::BinaryOp {
left: Box::new(self),
Expand Down
36 changes: 22 additions & 14 deletions core/src/ast_builder/expr/case.rs
Original file line number Diff line number Diff line change
@@ -1,50 +1,58 @@
use super::ExprNode;

impl ExprNode {
pub fn case(self) -> CaseNode {
impl<'a> ExprNode<'a> {
pub fn case(self) -> CaseNode<'a> {
CaseNode {
operand: Some(Box::new(self)),
}
}
}

pub fn case() -> CaseNode {
pub fn case() -> CaseNode<'static> {
CaseNode { operand: None }
}

pub struct CaseNode {
operand: Option<Box<ExprNode>>,
pub struct CaseNode<'a> {
operand: Option<Box<ExprNode<'a>>>,
}

impl CaseNode {
pub fn when_then<W: Into<ExprNode>, T: Into<ExprNode>>(self, when: W, then: T) -> WhenThenNode {
impl<'a> CaseNode<'a> {
pub fn when_then<W: Into<ExprNode<'a>>, T: Into<ExprNode<'a>>>(
self,
when: W,
then: T,
) -> WhenThenNode<'a> {
WhenThenNode {
prev_node: self,
when_then: vec![(when.into(), then.into())],
}
}
}

pub struct WhenThenNode {
prev_node: CaseNode,
when_then: Vec<(ExprNode, ExprNode)>,
pub struct WhenThenNode<'a> {
prev_node: CaseNode<'a>,
when_then: Vec<(ExprNode<'a>, ExprNode<'a>)>,
}

impl WhenThenNode {
pub fn when_then<W: Into<ExprNode>, T: Into<ExprNode>>(mut self, when: W, then: T) -> Self {
impl<'a> WhenThenNode<'a> {
pub fn when_then<W: Into<ExprNode<'a>>, T: Into<ExprNode<'a>>>(
mut self,
when: W,
then: T,
) -> Self {
self.when_then.push((when.into(), then.into()));
self
}

pub fn or_else<T: Into<ExprNode>>(self, else_result: T) -> ExprNode {
pub fn or_else<T: Into<ExprNode<'a>>>(self, else_result: T) -> ExprNode<'a> {
ExprNode::Case {
operand: self.prev_node.operand,
when_then: self.when_then,
else_result: Some(Box::new(else_result.into())),
}
}

pub fn end(self) -> ExprNode {
pub fn end(self) -> ExprNode<'a> {
ExprNode::Case {
operand: self.prev_node.operand,
when_then: self.when_then,
Expand Down
4 changes: 2 additions & 2 deletions core/src/ast_builder/expr/exists.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use {super::ExprNode, crate::ast_builder::QueryNode};

pub fn exists<T: Into<QueryNode>>(query: T) -> ExprNode {
pub fn exists<'a, T: Into<QueryNode<'a>>>(query: T) -> ExprNode<'a> {
ExprNode::Exists {
subquery: Box::new(query.into()),
negated: false,
}
}

pub fn not_exists<T: Into<QueryNode>>(query: T) -> ExprNode {
pub fn not_exists<'a, T: Into<QueryNode<'a>>>(query: T) -> ExprNode<'a> {
ExprNode::Exists {
subquery: Box::new(query.into()),
negated: true,
Expand Down
Loading

0 comments on commit 3557d48

Please sign in to comment.