use super::{
DataClause, Expr, GroupClause, Idiom, OrderClause, PartialNode, Projection, ReturnMode,
Spanned, TypeExpr,
};
use crate::span::ByteRange;
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Script {
pub statements: Vec<Spanned<Statement>>,
}
#[allow(clippy::large_enum_variant)]
#[derive(Clone, Debug, PartialEq)]
pub enum Statement {
Select(SelectStmt),
Create(CreateStmt),
Update(UpdateStmt),
Upsert(UpsertStmt),
Delete(DeleteStmt),
Insert(InsertStmt),
Relate(RelateStmt),
Define(DefineStmt),
Remove(RemoveStmt),
Alter(AlterStmt),
Let(LetStmt),
Return(ReturnStmt),
IfElse(IfElseStmt),
For(ForStmt),
Block(Block),
LiveSelect(LiveSelectStmt),
Kill(KillStmt),
Use(UseStmt),
Info(InfoStmt),
Show(ShowStmt),
Rebuild(RebuildStmt),
Throw(ThrowStmt),
Break(BreakStmt),
Continue(ContinueStmt),
Begin(BeginStmt),
Cancel(CancelStmt),
Commit(CommitStmt),
Sleep(SleepStmt),
Option(OptionStmt),
Expr(Spanned<Expr>),
Partial(PartialNode),
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Block {
pub statements: Vec<Spanned<Statement>>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct SelectStmt {
pub only: bool,
pub value: bool,
pub projections: Vec<Projection>,
pub from: Vec<Spanned<Expr>>,
pub omit: Vec<Spanned<Idiom>>,
pub fetch: Vec<Spanned<Idiom>>,
pub split: Vec<Spanned<Idiom>>,
pub where_clause: Option<Spanned<Expr>>,
pub group: Option<GroupClause>,
pub order: Option<OrderClause>,
pub limit: Option<Spanned<Expr>>,
pub start: Option<Spanned<Expr>>,
pub explain: Option<ByteRange>,
pub timeout: Option<Spanned<Expr>>,
pub parallel: Option<ByteRange>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct CreateStmt {
pub only: bool,
pub targets: Vec<Spanned<Expr>>,
pub data: Option<DataClause>,
pub ret: Option<Spanned<ReturnMode>>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct UpdateStmt {
pub only: bool,
pub targets: Vec<Spanned<Expr>>,
pub data: Option<DataClause>,
pub where_clause: Option<Spanned<Expr>>,
pub ret: Option<Spanned<ReturnMode>>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct UpsertStmt {
pub only: bool,
pub targets: Vec<Spanned<Expr>>,
pub data: Option<DataClause>,
pub where_clause: Option<Spanned<Expr>>,
pub ret: Option<Spanned<ReturnMode>>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct DeleteStmt {
pub only: bool,
pub targets: Vec<Spanned<Expr>>,
pub where_clause: Option<Spanned<Expr>>,
pub ret: Option<Spanned<ReturnMode>>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct InsertStmt {
pub ignore: bool,
pub relation: bool,
pub target: Option<Spanned<Expr>>,
pub data: InsertData,
pub ret: Option<Spanned<ReturnMode>>,
}
#[derive(Clone, Debug, PartialEq)]
pub enum InsertData {
Values(Vec<Spanned<Expr>>),
Rows {
rows: Vec<Vec<(Spanned<Idiom>, Spanned<Expr>)>>,
misaligned: Option<Spanned<(usize, usize)>>,
},
Assignments(Vec<(Spanned<Idiom>, Spanned<Expr>)>),
Partial(PartialNode),
}
#[derive(Clone, Debug, PartialEq)]
pub struct RelateStmt {
pub only: bool,
pub from: Option<Spanned<Expr>>,
pub edge: Option<Spanned<Expr>>,
pub to: Option<Spanned<Expr>>,
pub data: Option<DataClause>,
pub ret: Option<Spanned<ReturnMode>>,
}
#[derive(Clone, Debug, PartialEq)]
pub enum DefineStmt {
Table(DefineTable),
Field(DefineField),
Index(DefineIndex),
Event(DefineEvent),
Param(DefineParam),
Function(DefineFunction),
Analyzer(DefineAnalyzer),
Other(PartialNode),
}
#[derive(Clone, Debug, PartialEq)]
pub struct DefineTable {
pub name: Spanned<String>,
pub overwrite: bool,
pub schemafull: bool,
pub relation: Option<RelationDef>,
pub drop: bool,
pub changefeed: bool,
pub permissions: Vec<Spanned<Expr>>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct RelationDef {
pub in_tables: Vec<Spanned<String>>,
pub out_tables: Vec<Spanned<String>>,
pub span: ByteRange,
}
#[derive(Clone, Debug, PartialEq)]
pub struct DefineField {
pub path: Spanned<Idiom>,
pub table: Spanned<String>,
pub ty: Option<Spanned<TypeExpr>>,
pub overwrite: bool,
pub default: Option<Spanned<Expr>>,
pub value: Option<Spanned<Expr>>,
pub computed: Option<Spanned<Expr>>,
pub reference: bool,
pub assert: Option<Spanned<Expr>>,
pub readonly: bool,
pub permissions: Vec<Spanned<Expr>>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct DefineIndex {
pub name: Spanned<String>,
pub table: Spanned<String>,
pub fields: Vec<Spanned<Idiom>>,
pub kind: IndexKind,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum IndexKind {
Normal,
Unique,
Search,
Vector,
}
#[derive(Clone, Debug, PartialEq)]
pub struct DefineEvent {
pub name: Spanned<String>,
pub table: Spanned<String>,
pub when: Option<Spanned<Expr>>,
pub then: Option<Spanned<Expr>>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct DefineParam {
pub name: Spanned<String>,
pub value: Option<Spanned<Expr>>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct DefineFunction {
pub name: Spanned<String>,
pub params: Vec<(Spanned<String>, Option<Spanned<TypeExpr>>)>,
pub body: Option<Block>,
pub return_ty: Option<Spanned<TypeExpr>>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct DefineAnalyzer {
pub name: Spanned<String>,
pub tokenizers: Vec<Spanned<String>>,
pub filters: Vec<Spanned<String>>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct RemoveStmt {
pub target: RemoveTarget,
}
#[derive(Clone, Debug, PartialEq)]
pub enum RemoveTarget {
Table(Spanned<String>),
Field {
field: Spanned<Idiom>,
table: Spanned<String>,
},
Index {
index: Spanned<String>,
table: Spanned<String>,
},
Other(PartialNode),
}
#[derive(Clone, Debug, PartialEq)]
pub struct AlterStmt {
pub table: Option<Spanned<String>>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct LetStmt {
pub name: Spanned<String>,
pub value: Spanned<Expr>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct ReturnStmt {
pub value: Option<Spanned<Expr>>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct IfElseStmt {
pub branches: Vec<IfBranch>,
pub else_branch: Option<Block>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct IfBranch {
pub condition: Spanned<Expr>,
pub body: Block,
}
#[derive(Clone, Debug, PartialEq)]
pub struct ForStmt {
pub binding: Spanned<String>,
pub iterable: Spanned<Expr>,
pub body: Block,
}
#[derive(Clone, Debug, PartialEq)]
pub struct LiveSelectStmt {
pub table: Option<Spanned<String>>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct KillStmt {
pub id: Option<Spanned<Expr>>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct UseStmt {
pub namespace: Option<Spanned<String>>,
pub database: Option<Spanned<String>>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct InfoStmt {
pub table: Option<Spanned<String>>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct ShowStmt {
pub table: Option<Spanned<String>>,
pub since: Option<Spanned<Expr>>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct RebuildStmt {
pub index: Option<Spanned<String>>,
pub table: Option<Spanned<String>>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct ThrowStmt {
pub value: Option<Spanned<Expr>>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct SleepStmt {
pub duration: Option<Spanned<Expr>>,
}
macro_rules! unit_statements {
($($(#[$doc:meta])* $name:ident),+ $(,)?) => {
$(
$(#[$doc])*
#[derive(Clone, Debug, Default, PartialEq)]
pub struct $name {}
)+
};
}
unit_statements! {
BreakStmt,
ContinueStmt,
BeginStmt,
CancelStmt,
CommitStmt,
OptionStmt,
}