#[derive(Debug, Clone, PartialEq)]
pub enum Statement {
Query(QueryExpr),
Insert(InsertExpr),
UpdateQuery(UpdateExpr),
DeleteQuery(DeleteExpr),
CreateType(CreateTypeExpr),
AlterTable(AlterTableExpr),
DropTable(DropTableExpr),
CreateView(CreateViewExpr),
RefreshView(RefreshViewExpr),
DropView(DropViewExpr),
Union(UnionExpr),
Upsert(UpsertExpr),
Explain(Box<Statement>),
Begin,
Commit,
Rollback,
}
#[derive(Debug, Clone, PartialEq)]
pub struct AlterTableExpr {
pub table: String,
pub action: AlterAction,
}
#[derive(Debug, Clone, PartialEq)]
pub enum AlterAction {
AddColumn {
name: String,
type_name: String,
required: bool,
},
DropColumn {
name: String,
},
AddIndex {
column: String,
},
}
#[derive(Debug, Clone, PartialEq)]
pub struct DropTableExpr {
pub table: String,
}
#[derive(Debug, Clone, PartialEq)]
pub struct CreateViewExpr {
pub name: String,
pub query: QueryExpr,
pub query_text: String,
}
#[derive(Debug, Clone, PartialEq)]
pub struct RefreshViewExpr {
pub name: String,
}
#[derive(Debug, Clone, PartialEq)]
pub struct DropViewExpr {
pub name: String,
}
#[derive(Debug, Clone, PartialEq)]
pub struct UnionExpr {
pub left: Box<Statement>,
pub right: Box<Statement>,
pub all: bool,
}
#[derive(Debug, Clone, PartialEq)]
pub struct QueryExpr {
pub source: String,
pub alias: Option<String>,
pub joins: Vec<JoinClause>,
pub filter: Option<Expr>,
pub order: Option<OrderClause>,
pub limit: Option<Expr>,
pub offset: Option<Expr>,
pub projection: Option<Vec<ProjectionField>>,
pub aggregation: Option<AggregateExpr>,
pub distinct: bool,
pub group_by: Option<GroupByClause>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct GroupByClause {
pub keys: Vec<String>,
pub having: Option<Expr>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct JoinClause {
pub kind: JoinKind,
pub source: String,
pub alias: Option<String>,
pub on: Option<Expr>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum JoinKind {
Inner,
LeftOuter,
RightOuter,
Cross,
}
#[derive(Debug, Clone, PartialEq)]
pub struct ProjectionField {
pub alias: Option<String>,
pub expr: Expr,
}
#[derive(Debug, Clone, PartialEq)]
pub struct OrderClause {
pub keys: Vec<OrderKey>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct OrderKey {
pub field: String,
pub descending: bool,
}
#[derive(Debug, Clone, PartialEq)]
pub struct InsertExpr {
pub target: String,
pub rows: Vec<Vec<Assignment>>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct UpdateExpr {
pub source: String,
pub filter: Option<Expr>,
pub assignments: Vec<Assignment>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct DeleteExpr {
pub source: String,
pub filter: Option<Expr>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Assignment {
pub field: String,
pub value: Expr,
}
#[derive(Debug, Clone, PartialEq)]
pub struct UpsertExpr {
pub target: String,
pub key_column: String,
pub assignments: Vec<Assignment>,
pub on_conflict: Vec<Assignment>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct CreateTypeExpr {
pub name: String,
pub fields: Vec<FieldDef>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct FieldDef {
pub name: String,
pub type_name: String,
pub required: bool,
}
#[derive(Debug, Clone, PartialEq)]
pub struct AggregateExpr {
pub function: AggFunc,
pub field: Option<String>,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum AggFunc {
Count,
CountDistinct,
Avg,
Sum,
Min,
Max,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum WindowFunc {
RowNumber,
Rank,
DenseRank,
Sum,
Avg,
Count,
Min,
Max,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ScalarFn {
Upper,
Lower,
Length,
Trim,
Substring, Concat, Abs,
Round, Ceil,
Floor,
Sqrt,
Pow, Now, Extract, DateAdd, DateDiff, }
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum CastType {
Int,
Float,
Str,
Bool,
DateTime,
}
#[derive(Debug, Clone, PartialEq)]
pub enum Expr {
Field(String),
QualifiedField {
qualifier: String,
field: String,
},
Literal(Literal),
Param(String),
BinaryOp(Box<Expr>, BinOp, Box<Expr>),
UnaryOp(UnaryOp, Box<Expr>),
FunctionCall(AggFunc, Box<Expr>),
ScalarFunc(ScalarFn, Vec<Expr>),
Coalesce(Box<Expr>, Box<Expr>),
InList {
expr: Box<Expr>,
list: Vec<Expr>,
negated: bool,
},
InSubquery {
expr: Box<Expr>,
subquery: Box<QueryExpr>,
negated: bool,
},
ExistsSubquery {
subquery: Box<QueryExpr>,
negated: bool,
},
Case {
whens: Vec<(Box<Expr>, Box<Expr>)>,
else_expr: Option<Box<Expr>>,
},
Window {
function: WindowFunc,
args: Vec<Expr>,
partition_by: Vec<String>,
order_by: Vec<OrderKey>,
},
Cast(Box<Expr>, CastType),
Null,
}
#[derive(Debug, Clone, PartialEq)]
pub enum Literal {
Int(i64),
Float(f64),
String(String),
Bool(bool),
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum BinOp {
Eq,
Neq,
Lt,
Gt,
Lte,
Gte,
And,
Or,
Add,
Sub,
Mul,
Div,
Like,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum UnaryOp {
Not,
Exists,
NotExists,
IsNull,
IsNotNull,
}