use crate::sql::token::Param;
use alloc::boxed::Box;
use alloc::string::String;
use alloc::vec::Vec;
#[allow(clippy::large_enum_variant)]
#[derive(Debug, Clone, PartialEq)]
pub enum Statement {
Select(Select),
Insert(Insert),
Update(Update),
Delete(Delete),
CreateTable(CreateTable),
CreateIndex(CreateIndex),
CreateView(CreateView),
CreateTrigger(CreateTrigger),
Drop(Drop),
Alter(Alter),
Begin,
Commit,
Rollback,
Pragma(Pragma),
Vacuum,
Explain {
query_plan: bool,
stmt: Box<Statement>,
},
}
#[derive(Debug, Clone, PartialEq)]
pub struct Cte {
pub name: String,
pub columns: Vec<String>,
pub select: Box<Select>,
}
#[derive(Debug, Clone, PartialEq, Default)]
pub struct WindowSpec {
pub partition_by: Vec<Expr>,
pub order_by: Vec<OrderTerm>,
pub frame: Option<WindowFrame>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct WindowFrame {
pub mode: FrameMode,
pub start: FrameBound,
pub end: FrameBound,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FrameMode {
Rows,
Range,
Groups,
}
#[derive(Debug, Clone, PartialEq)]
pub enum FrameBound {
UnboundedPreceding,
Preceding(i64),
CurrentRow,
Following(i64),
UnboundedFollowing,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CompoundOp {
Union,
UnionAll,
Intersect,
Except,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Select {
pub ctes: Vec<Cte>,
pub compound: Vec<(CompoundOp, Select)>,
pub distinct: bool,
pub columns: Vec<ResultColumn>,
pub from: Option<FromClause>,
pub where_clause: Option<Expr>,
pub group_by: Vec<Expr>,
pub having: Option<Expr>,
pub order_by: Vec<OrderTerm>,
pub limit: Option<Expr>,
pub offset: Option<Expr>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum ResultColumn {
Wildcard,
TableWildcard(String),
Expr {
expr: Expr,
alias: Option<String>,
},
}
#[derive(Debug, Clone, PartialEq)]
pub struct FromClause {
pub first: TableRef,
pub joins: Vec<Join>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct TableRef {
pub name: String,
pub alias: Option<String>,
pub subquery: Option<Box<Select>>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum JoinKind {
Inner,
Left,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Join {
pub kind: JoinKind,
pub table: TableRef,
pub on: Option<Expr>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct OrderTerm {
pub expr: Expr,
pub descending: bool,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Insert {
pub table: String,
pub columns: Vec<String>,
pub source: InsertSource,
pub on_conflict: OnConflict,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OnConflict {
Abort,
Ignore,
Replace,
}
#[derive(Debug, Clone, PartialEq)]
pub enum InsertSource {
Values(Vec<Vec<Expr>>),
Select(Box<Select>),
DefaultValues,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Update {
pub table: String,
pub assignments: Vec<(String, Expr)>,
pub where_clause: Option<Expr>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Delete {
pub table: String,
pub where_clause: Option<Expr>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct CreateTable {
pub if_not_exists: bool,
pub name: String,
pub columns: Vec<ColumnDef>,
pub constraints: Vec<TableConstraint>,
pub without_rowid: bool,
}
#[derive(Debug, Clone, PartialEq)]
pub struct ColumnDef {
pub name: String,
pub type_name: Option<String>,
pub constraints: Vec<ColumnConstraint>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum FkAction {
#[default]
NoAction,
Restrict,
Cascade,
SetNull,
SetDefault,
}
#[derive(Debug, Clone, PartialEq)]
pub struct ForeignKey {
pub columns: Vec<String>,
pub ref_table: String,
pub ref_columns: Vec<String>,
pub on_delete: FkAction,
pub on_update: FkAction,
}
#[derive(Debug, Clone, PartialEq)]
pub enum ColumnConstraint {
PrimaryKey {
descending: bool,
},
NotNull,
Unique,
Default(Expr),
Collate(String),
Check(Expr),
References(ForeignKey),
}
#[derive(Debug, Clone, PartialEq)]
pub enum TableConstraint {
PrimaryKey(Vec<String>),
Unique(Vec<String>),
Check(Expr),
ForeignKey(ForeignKey),
}
#[derive(Debug, Clone, PartialEq)]
pub struct CreateIndex {
pub unique: bool,
pub if_not_exists: bool,
pub name: String,
pub table: String,
pub columns: Vec<OrderTerm>,
pub where_clause: Option<Expr>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct CreateView {
pub if_not_exists: bool,
pub name: String,
pub columns: Vec<String>,
pub select: Box<Select>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TriggerTiming {
Before,
After,
InsteadOf,
}
#[derive(Debug, Clone, PartialEq)]
pub enum TriggerEvent {
Insert,
Update(Vec<String>),
Delete,
}
#[derive(Debug, Clone, PartialEq)]
pub struct CreateTrigger {
pub if_not_exists: bool,
pub name: String,
pub timing: TriggerTiming,
pub event: TriggerEvent,
pub table: String,
pub when: Option<Expr>,
pub body: Vec<Statement>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DropKind {
Table,
Index,
View,
Trigger,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Drop {
pub kind: DropKind,
pub if_exists: bool,
pub name: String,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Alter {
pub table: String,
pub action: AlterAction,
}
#[derive(Debug, Clone, PartialEq)]
pub enum AlterAction {
RenameTable(String),
RenameColumn {
old: String,
new: String,
},
AddColumn(ColumnDef),
}
#[derive(Debug, Clone, PartialEq)]
pub struct Pragma {
pub name: String,
pub value: Option<Expr>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum Expr {
Literal(Literal),
Parameter(Param),
Column {
table: Option<String>,
column: String,
},
Unary {
op: UnaryOp,
expr: Box<Expr>,
},
Binary {
op: BinaryOp,
left: Box<Expr>,
right: Box<Expr>,
},
Function {
name: String,
distinct: bool,
args: Vec<Expr>,
star: bool,
over: Option<WindowSpec>,
},
IsNull {
expr: Box<Expr>,
negated: bool,
},
InList {
expr: Box<Expr>,
list: Vec<Expr>,
negated: bool,
},
Between {
expr: Box<Expr>,
low: Box<Expr>,
high: Box<Expr>,
negated: bool,
},
Case {
operand: Option<Box<Expr>>,
when_then: Vec<(Expr, Expr)>,
else_result: Option<Box<Expr>>,
},
Cast {
expr: Box<Expr>,
type_name: String,
},
Paren(Box<Expr>),
Subquery(Box<Select>),
Exists {
select: Box<Select>,
negated: bool,
},
InSelect {
expr: Box<Expr>,
select: Box<Select>,
negated: bool,
},
}
#[derive(Debug, Clone, PartialEq)]
pub enum Literal {
Null,
Integer(i64),
Real(f64),
Str(String),
Blob(Vec<u8>),
Boolean(bool),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UnaryOp {
Negate,
Identity,
Not,
BitNot,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BinaryOp {
Or,
And,
Eq,
NotEq,
Lt,
LtEq,
Gt,
GtEq,
Is,
IsNot,
Like,
Glob,
Add,
Sub,
Mul,
Div,
Mod,
Concat,
BitAnd,
BitOr,
LShift,
RShift,
}