use serde::Serialize;
use text_size::TextRange;
#[derive(Debug, Clone, Serialize)]
pub struct Statement {
pub range: TextRange,
pub kind: StatementKind,
}
#[derive(Debug, Clone, Serialize)]
pub enum StatementKind {
Select(SelectStmt),
Insert(InsertStmt),
Update(UpdateStmt),
Delete(DeleteStmt),
CreateTable(CreateTableStmt),
AlterTable(AlterTableStmt),
DropTable(DropTableStmt),
Unknown {
text: String,
},
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct SelectStmt {
pub projections: Vec<Projection>,
pub from: Vec<TableRef>,
pub joins: Vec<JoinClause>,
pub where_clause: Option<Expr>,
#[serde(default)]
pub cte_names: Vec<String>,
}
#[derive(Debug, Clone, Serialize)]
pub enum Projection {
Star,
QualifiedStar(String),
Expr { expr: Expr, alias: Option<String> },
}
#[derive(Debug, Clone, Serialize, Default)]
pub struct TableRef {
pub schema: Option<String>,
pub name: String,
pub alias: Option<String>,
pub range: TextRange,
}
#[derive(Debug, Clone, Serialize)]
pub struct JoinClause {
pub kind: JoinKind,
pub table: TableRef,
pub on: Option<Expr>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub enum JoinKind {
Inner,
Left,
Right,
Full,
Cross,
}
#[derive(Debug, Clone, Serialize)]
pub enum Expr {
Column {
qualifier: Option<String>,
name: String,
range: TextRange,
},
Literal(String),
BinaryOp {
op: String,
left: Box<Expr>,
right: Box<Expr>,
},
Call {
name: String,
args: Vec<Expr>,
},
Other(String),
List(Vec<Expr>),
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct InsertStmt {
pub table: TableRef,
pub columns: Vec<String>,
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct UpdateStmt {
pub table: TableRef,
pub assignments: Vec<(String, Expr)>,
pub where_clause: Option<Expr>,
pub from_tables: Vec<TableRef>,
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct DeleteStmt {
pub table: TableRef,
pub where_clause: Option<Expr>,
pub using_tables: Vec<TableRef>,
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct CreateTableStmt {
pub table: TableRef,
pub if_not_exists: bool,
pub columns: Vec<ColumnDef>,
}
#[derive(Debug, Clone, Serialize)]
pub struct ColumnDef {
pub name: String,
pub type_name: String,
pub nullable: bool,
pub default: Option<String>,
pub range: TextRange,
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct AlterTableStmt {
pub table: TableRef,
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct DropTableStmt {
pub tables: Vec<TableRef>,
pub if_exists: bool,
}