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),
Drop(Drop),
Begin,
Commit,
Rollback,
Pragma(Pragma),
}
#[derive(Debug, Clone, PartialEq)]
pub struct 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>,
}
#[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,
}
#[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, PartialEq)]
pub enum ColumnConstraint {
PrimaryKey {
descending: bool,
},
NotNull,
Unique,
Default(Expr),
Collate(String),
}
#[derive(Debug, Clone, PartialEq)]
pub enum TableConstraint {
PrimaryKey(Vec<String>),
Unique(Vec<String>),
}
#[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, 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 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,
},
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>),
}
#[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,
}