Skip to main content

vibesql/ast/
stmt.rs

1//! Statement AST definitions.
2//!
3//! This module defines the statement types for SQL DDL and DML.
4
5use super::{
6    Alias, ColumnDef, DataTypeSpec, Expr, FromClause, Ident, ObjectName, Query, SelectItem,
7    SortKey, SqlOption, TableConstraint, TableRef,
8};
9use crate::error::Span;
10
11/// A complete SQL statement.
12#[derive(Debug, Clone, PartialEq)]
13pub struct Statement {
14    pub kind: StatementKind,
15    pub span: Span,
16}
17
18impl Statement {
19    pub fn new(kind: StatementKind, span: Span) -> Self {
20        Self { kind, span }
21    }
22}
23
24/// Statement kind.
25#[derive(Debug, Clone, PartialEq)]
26pub enum StatementKind {
27    // Query
28    Query(Box<Query>),
29
30    // DML
31    Insert(InsertStatement),
32    Update(UpdateStatement),
33    Delete(DeleteStatement),
34    Merge(MergeStatement),
35
36    // DDL
37    CreateDatabase(CreateDatabaseStatement),
38    CreateTable(CreateTableStatement),
39    CreateView(CreateViewStatement),
40    CreateIndex(CreateIndexStatement),
41    CreateFunction(CreateFunctionStatement),
42    CreateProcedure(CreateProcedureStatement),
43
44    AlterTable(AlterTableStatement),
45    AlterView(AlterViewStatement),
46
47    Drop(DropStatement),
48    Truncate(TruncateStatement),
49
50    // Transaction control
51    Begin(BeginStatement),
52    Commit,
53    Rollback(RollbackStatement),
54
55    // Utility
56    Explain(ExplainStatement),
57    Describe(DescribeStatement),
58    Show(ShowStatement),
59    Set(SetStatement),
60
61    // Empty statement (just a semicolon)
62    Empty,
63}
64
65// ============================================================================
66// DML Statements
67// ============================================================================
68
69/// INSERT statement.
70#[derive(Debug, Clone, PartialEq)]
71pub struct InsertStatement {
72    pub or_action: Option<InsertOrAction>,
73    pub table: ObjectName,
74    pub columns: Vec<Ident>,
75    pub source: InsertSource,
76    pub returning: Option<ReturningClause>,
77}
78
79/// INSERT OR action.
80#[derive(Debug, Clone, Copy, PartialEq, Eq)]
81pub enum InsertOrAction {
82    Ignore,
83    Replace,
84    Update,
85}
86
87/// INSERT source (VALUES or SELECT).
88#[derive(Debug, Clone, PartialEq)]
89pub enum InsertSource {
90    Values(Vec<Vec<Box<Expr>>>),
91    Query(Box<Query>),
92    DefaultValues,
93}
94
95/// UPDATE statement.
96#[derive(Debug, Clone, PartialEq)]
97pub struct UpdateStatement {
98    pub table: TableRef,
99    pub assignments: Vec<Assignment>,
100    pub from: Option<FromClause>,
101    pub where_clause: Option<Box<Expr>>,
102    pub returning: Option<ReturningClause>,
103}
104
105/// Assignment in UPDATE SET clause.
106#[derive(Debug, Clone, PartialEq)]
107pub struct Assignment {
108    pub target: AssignmentTarget,
109    pub value: Box<Expr>,
110}
111
112/// Assignment target (column or path).
113#[derive(Debug, Clone, PartialEq)]
114pub enum AssignmentTarget {
115    Column(Ident),
116    Path(Vec<Ident>),
117}
118
119/// DELETE statement.
120#[derive(Debug, Clone, PartialEq)]
121pub struct DeleteStatement {
122    pub table: ObjectName,
123    pub alias: Option<Alias>,
124    pub where_clause: Option<Box<Expr>>,
125    pub returning: Option<ReturningClause>,
126}
127
128/// MERGE statement.
129#[derive(Debug, Clone, PartialEq)]
130pub struct MergeStatement {
131    pub target: TableRef,
132    pub source: TableRef,
133    pub on: Box<Expr>,
134    pub clauses: Vec<MergeClause>,
135}
136
137/// MERGE clause.
138#[derive(Debug, Clone, PartialEq)]
139pub enum MergeClause {
140    Matched {
141        condition: Option<Box<Expr>>,
142        action: MergeMatchedAction,
143    },
144    NotMatched {
145        condition: Option<Box<Expr>>,
146        action: MergeNotMatchedAction,
147    },
148    NotMatchedBySource {
149        condition: Option<Box<Expr>>,
150        action: MergeMatchedAction,
151    },
152}
153
154/// Action for WHEN MATCHED.
155#[derive(Debug, Clone, PartialEq)]
156pub enum MergeMatchedAction {
157    Update { assignments: Vec<Assignment> },
158    Delete,
159}
160
161/// Action for WHEN NOT MATCHED.
162#[derive(Debug, Clone, PartialEq)]
163pub struct MergeNotMatchedAction {
164    pub columns: Vec<Ident>,
165    pub values: Vec<Box<Expr>>,
166}
167
168/// RETURNING clause.
169#[derive(Debug, Clone, PartialEq)]
170pub struct ReturningClause {
171    pub items: Vec<SelectItem>,
172    pub with_action: bool,
173}
174
175// ============================================================================
176// DDL Statements
177// ============================================================================
178
179/// CREATE DATABASE statement.
180#[derive(Debug, Clone, PartialEq)]
181pub struct CreateDatabaseStatement {
182    pub name: Ident,
183    pub if_not_exists: bool,
184    pub options: Vec<SqlOption>,
185}
186
187/// CREATE TABLE statement.
188#[derive(Debug, Clone, PartialEq)]
189pub struct CreateTableStatement {
190    pub or_replace: bool,
191    pub temporary: bool,
192    pub if_not_exists: bool,
193    pub name: ObjectName,
194    pub columns: Vec<ColumnDef>,
195    pub constraints: Vec<TableConstraint>,
196    pub partition_by: Vec<Box<Expr>>,
197    pub cluster_by: Vec<Box<Expr>>,
198    pub options: Vec<SqlOption>,
199    pub as_query: Option<Box<Query>>,
200    pub like: Option<ObjectName>,
201    pub clone: Option<ObjectName>,
202}
203
204/// CREATE VIEW statement.
205#[derive(Debug, Clone, PartialEq)]
206pub struct CreateViewStatement {
207    pub or_replace: bool,
208    pub materialized: bool,
209    pub if_not_exists: bool,
210    pub name: ObjectName,
211    pub columns: Vec<Ident>,
212    pub query: Box<Query>,
213    pub options: Vec<SqlOption>,
214}
215
216/// CREATE INDEX statement.
217#[derive(Debug, Clone, PartialEq)]
218pub struct CreateIndexStatement {
219    pub unique: bool,
220    pub if_not_exists: bool,
221    pub name: Option<Ident>,
222    pub table: ObjectName,
223    pub columns: Vec<SortKey>,
224    pub options: Vec<SqlOption>,
225}
226
227/// CREATE FUNCTION statement.
228#[derive(Debug, Clone, PartialEq)]
229pub struct CreateFunctionStatement {
230    pub or_replace: bool,
231    pub temporary: bool,
232    pub if_not_exists: bool,
233    pub name: ObjectName,
234    pub params: Vec<FunctionParam>,
235    pub returns: Option<DataTypeSpec>,
236    pub language: Option<String>,
237    pub body: FunctionBody,
238    pub options: Vec<SqlOption>,
239}
240
241/// Function parameter.
242#[derive(Debug, Clone, PartialEq)]
243pub struct FunctionParam {
244    pub name: Option<Ident>,
245    pub data_type: DataTypeSpec,
246    pub default: Option<Box<Expr>>,
247}
248
249/// Function body.
250#[derive(Debug, Clone, PartialEq)]
251pub enum FunctionBody {
252    Expr(Box<Expr>),
253    Statements(Vec<Statement>),
254    External(String),
255}
256
257/// CREATE PROCEDURE statement.
258#[derive(Debug, Clone, PartialEq)]
259pub struct CreateProcedureStatement {
260    pub or_replace: bool,
261    pub if_not_exists: bool,
262    pub name: ObjectName,
263    pub params: Vec<ProcedureParam>,
264    pub body: Vec<Statement>,
265    pub options: Vec<SqlOption>,
266}
267
268/// Procedure parameter.
269#[derive(Debug, Clone, PartialEq)]
270pub struct ProcedureParam {
271    pub mode: ParamMode,
272    pub name: Option<Ident>,
273    pub data_type: DataTypeSpec,
274}
275
276/// Parameter mode.
277#[derive(Debug, Clone, Copy, PartialEq, Eq)]
278pub enum ParamMode {
279    In,
280    Out,
281    InOut,
282}
283
284/// ALTER TABLE statement.
285#[derive(Debug, Clone, PartialEq)]
286pub struct AlterTableStatement {
287    pub if_exists: bool,
288    pub name: ObjectName,
289    pub action: AlterTableAction,
290}
291
292/// ALTER TABLE action.
293#[derive(Debug, Clone, PartialEq)]
294pub enum AlterTableAction {
295    AddColumn {
296        if_not_exists: bool,
297        column: ColumnDef,
298    },
299    DropColumn {
300        if_exists: bool,
301        column: Ident,
302    },
303    AlterColumn {
304        column: Ident,
305        action: AlterColumnAction,
306    },
307    AddConstraint(TableConstraint),
308    DropConstraint {
309        if_exists: bool,
310        name: Ident,
311    },
312    RenameColumn {
313        from: Ident,
314        to: Ident,
315    },
316    RenameTable(ObjectName),
317    SetOptions(Vec<SqlOption>),
318}
319
320/// ALTER COLUMN action.
321#[derive(Debug, Clone, PartialEq)]
322pub enum AlterColumnAction {
323    SetDataType(DataTypeSpec),
324    SetDefault(Box<Expr>),
325    DropDefault,
326    SetNotNull,
327    DropNotNull,
328    SetOptions(Vec<SqlOption>),
329}
330
331/// ALTER VIEW statement.
332#[derive(Debug, Clone, PartialEq)]
333pub struct AlterViewStatement {
334    pub if_exists: bool,
335    pub name: ObjectName,
336    pub action: AlterViewAction,
337}
338
339/// ALTER VIEW action.
340#[derive(Debug, Clone, PartialEq)]
341pub enum AlterViewAction {
342    SetOptions(Vec<SqlOption>),
343    SetQuery(Box<Query>),
344}
345
346/// DROP statement.
347#[derive(Debug, Clone, PartialEq)]
348pub struct DropStatement {
349    pub object_type: ObjectType,
350    pub if_exists: bool,
351    pub names: Vec<ObjectName>,
352    pub cascade: bool,
353}
354
355/// Object type for DROP.
356#[derive(Debug, Clone, Copy, PartialEq, Eq)]
357pub enum ObjectType {
358    Table,
359    View,
360    MaterializedView,
361    Index,
362    Function,
363    Procedure,
364    Database,
365    Schema,
366}
367
368/// TRUNCATE statement.
369#[derive(Debug, Clone, PartialEq)]
370pub struct TruncateStatement {
371    pub table: ObjectName,
372}
373
374// ============================================================================
375// Transaction Control
376// ============================================================================
377
378/// BEGIN statement.
379#[derive(Debug, Clone, PartialEq)]
380pub struct BeginStatement {
381    pub mode: Option<TransactionMode>,
382}
383
384/// Transaction mode.
385#[derive(Debug, Clone, Copy, PartialEq, Eq)]
386pub enum TransactionMode {
387    ReadOnly,
388    ReadWrite,
389}
390
391/// ROLLBACK statement.
392#[derive(Debug, Clone, PartialEq)]
393pub struct RollbackStatement {
394    pub savepoint: Option<Ident>,
395}
396
397// ============================================================================
398// Utility Statements
399// ============================================================================
400
401/// EXPLAIN statement.
402#[derive(Debug, Clone, PartialEq)]
403pub struct ExplainStatement {
404    pub analyze: bool,
405    pub format: Option<ExplainFormat>,
406    pub statement: Box<Statement>,
407}
408
409/// EXPLAIN format.
410#[derive(Debug, Clone, Copy, PartialEq, Eq)]
411pub enum ExplainFormat {
412    Text,
413    Json,
414}
415
416/// DESCRIBE statement.
417#[derive(Debug, Clone, PartialEq)]
418pub struct DescribeStatement {
419    pub object: ObjectName,
420}
421
422/// SHOW statement.
423#[derive(Debug, Clone, PartialEq)]
424pub struct ShowStatement {
425    pub object_type: ShowObjectType,
426    pub filter: Option<ShowFilter>,
427}
428
429/// SHOW object type.
430#[derive(Debug, Clone, PartialEq)]
431pub enum ShowObjectType {
432    Tables { from: Option<Ident> },
433    Databases,
434    Schemas { from: Option<Ident> },
435    Columns { from: ObjectName },
436    Functions { from: Option<Ident> },
437    Variables,
438}
439
440/// SHOW filter.
441#[derive(Debug, Clone, PartialEq)]
442pub enum ShowFilter {
443    Like(String),
444    Where(Box<Expr>),
445}
446
447/// SET statement.
448#[derive(Debug, Clone, PartialEq)]
449pub struct SetStatement {
450    pub variable: Ident,
451    pub value: SetValue,
452}
453
454/// SET value.
455#[derive(Debug, Clone, PartialEq)]
456pub enum SetValue {
457    Expr(Box<Expr>),
458    Default,
459}
460
461// ============================================================================
462// Procedural Language
463// ============================================================================
464
465/// Procedural statement (for stored procedures).
466#[derive(Debug, Clone, PartialEq)]
467pub enum ProceduralStatement {
468    Declare(DeclareStatement),
469    Set(SetVariableStatement),
470    If(IfStatement),
471    Loop(LoopStatement),
472    While(WhileStatement),
473    For(ForStatement),
474    Break,
475    Continue,
476    Return(Option<Box<Expr>>),
477    Raise(RaiseStatement),
478    Call(CallStatement),
479    Begin(BeginEndStatement),
480}
481
482/// DECLARE statement.
483#[derive(Debug, Clone, PartialEq)]
484pub struct DeclareStatement {
485    pub names: Vec<Ident>,
486    pub data_type: Option<DataTypeSpec>,
487    pub default: Option<Box<Expr>>,
488}
489
490/// SET variable statement.
491#[derive(Debug, Clone, PartialEq)]
492pub struct SetVariableStatement {
493    pub names: Vec<Ident>,
494    pub value: Box<Expr>,
495}
496
497/// IF statement.
498#[derive(Debug, Clone, PartialEq)]
499pub struct IfStatement {
500    pub condition: Box<Expr>,
501    pub then_body: Vec<Statement>,
502    pub elseif_clauses: Vec<(Box<Expr>, Vec<Statement>)>,
503    pub else_body: Option<Vec<Statement>>,
504}
505
506/// LOOP statement.
507#[derive(Debug, Clone, PartialEq)]
508pub struct LoopStatement {
509    pub body: Vec<Statement>,
510}
511
512/// WHILE statement.
513#[derive(Debug, Clone, PartialEq)]
514pub struct WhileStatement {
515    pub condition: Box<Expr>,
516    pub body: Vec<Statement>,
517}
518
519/// FOR statement.
520#[derive(Debug, Clone, PartialEq)]
521pub struct ForStatement {
522    pub variable: Ident,
523    pub query: Box<Query>,
524    pub body: Vec<Statement>,
525}
526
527/// RAISE statement.
528#[derive(Debug, Clone, PartialEq)]
529pub struct RaiseStatement {
530    pub message: Option<Box<Expr>>,
531}
532
533/// CALL statement.
534#[derive(Debug, Clone, PartialEq)]
535pub struct CallStatement {
536    pub name: ObjectName,
537    pub args: Vec<Box<Expr>>,
538}
539
540/// BEGIN...END block.
541#[derive(Debug, Clone, PartialEq)]
542pub struct BeginEndStatement {
543    pub statements: Vec<Statement>,
544    pub exception_handlers: Vec<ExceptionHandler>,
545}
546
547/// Exception handler.
548#[derive(Debug, Clone, PartialEq)]
549pub struct ExceptionHandler {
550    pub when: ExceptionWhen,
551    pub body: Vec<Statement>,
552}
553
554/// Exception condition.
555#[derive(Debug, Clone, PartialEq)]
556pub enum ExceptionWhen {
557    Error,
558    Named(String),
559}