use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Statement {
Select(SelectStatement),
Insert(InsertStatement),
Update(UpdateStatement),
Delete(DeleteStatement),
Upsert(UpsertStatement),
CreateTable(CreateTableStatement),
DropTable(DropTableStatement),
Use(UseStatement),
Show(ShowStatement),
Describe(String), Set(SetStatement),
Begin,
Commit,
Rollback,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SelectStatement {
pub distinct: bool,
pub columns: Vec<SelectColumn>,
pub from: Option<String>,
pub joins: Vec<JoinClause>,
pub where_clause: Option<Expression>,
pub group_by: Vec<String>,
pub having: Option<Expression>,
pub order_by: Vec<OrderByColumn>,
pub limit: Option<Limit>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct JoinClause {
pub join_type: JoinType,
pub table: String,
pub on_condition: Expression,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum JoinType {
Inner,
Left,
Right,
Full,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum SelectColumn {
Wildcard,
Column { name: String, alias: Option<String> },
Function { name: String, args: Vec<Expression>, alias: Option<String> },
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct OrderByColumn {
pub column: String,
pub direction: OrderDirection,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum OrderDirection {
Asc,
Desc,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Limit {
pub offset: Option<usize>,
pub count: usize,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct InsertStatement {
pub table: String,
pub columns: Vec<String>,
pub values: Vec<Vec<Expression>>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct UpsertStatement {
pub table: String,
pub columns: Vec<String>,
pub values: Vec<Vec<Expression>>,
pub key_column: String, }
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct UpdateStatement {
pub table: String,
pub assignments: Vec<(String, Expression)>,
pub where_clause: Option<Expression>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct DeleteStatement {
pub table: String,
pub where_clause: Option<Expression>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CreateTableStatement {
pub name: String,
pub columns: Vec<ColumnDefinition>,
pub file_path: String,
pub delimiter: Option<char>,
pub data_starts_on: Option<usize>,
pub comment_char: Option<char>,
pub quote_char: Option<char>,
pub if_not_exists: bool,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ColumnDefinition {
pub name: String,
pub data_type: ColumnType,
pub nullable: bool,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum ColumnType {
Integer,
Float,
String,
Boolean,
Date,
DateTime,
Time,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct DropTableStatement {
pub name: String,
pub if_exists: bool,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct UseStatement {
pub database: String,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum ShowStatement {
Tables,
Columns(String),
Variables,
OutputModules,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SetStatement {
pub variable: String,
pub value: Expression,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Expression {
Literal(Literal),
Column(String),
SystemVariable(String), Function { name: String, args: Vec<Expression> },
BinaryOp { left: Box<Expression>, op: BinaryOperator, right: Box<Expression> },
UnaryOp { op: UnaryOperator, operand: Box<Expression> },
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Literal {
String(String),
Number(f64),
Integer(i64),
Boolean(bool),
Null,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum BinaryOperator {
Equal,
NotEqual,
GreaterThan,
GreaterEqual,
LessThan,
LessEqual,
Like,
And,
Or,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum UnaryOperator {
Not,
IsNull,
}