vibesql_ast/ddl/
advanced.rs

1//! Advanced SQL:1999 DDL objects
2//!
3//! This module contains AST nodes for advanced SQL:1999 features:
4//! - DOMAIN
5//! - SEQUENCE
6//! - TYPE (distinct, structured)
7//! - COLLATION
8//! - CHARACTER SET
9//! - TRANSLATION
10//! - ASSERTION
11
12use vibesql_types::DataType;
13
14use crate::Expression;
15
16// ============================================================================
17// DOMAIN
18// ============================================================================
19
20/// CREATE DOMAIN statement
21#[derive(Debug, Clone, PartialEq)]
22pub struct CreateDomainStmt {
23    pub domain_name: String,
24    pub data_type: DataType,
25    pub default: Option<Box<Expression>>,
26    pub constraints: Vec<DomainConstraint>,
27}
28
29/// Domain constraint (CHECK constraint on domain values)
30#[derive(Debug, Clone, PartialEq)]
31pub struct DomainConstraint {
32    pub name: Option<String>,
33    pub check: Box<Expression>,
34}
35
36/// DROP DOMAIN statement
37#[derive(Debug, Clone, PartialEq)]
38pub struct DropDomainStmt {
39    pub domain_name: String,
40    pub cascade: bool, // true for CASCADE, false for RESTRICT
41}
42
43// ============================================================================
44// SEQUENCE
45// ============================================================================
46
47/// CREATE SEQUENCE statement
48#[derive(Debug, Clone, PartialEq)]
49pub struct CreateSequenceStmt {
50    pub sequence_name: String,
51    pub start_with: Option<i64>,
52    pub increment_by: i64, // default: 1
53    pub min_value: Option<i64>,
54    pub max_value: Option<i64>,
55    pub cycle: bool, // default: false
56}
57
58/// DROP SEQUENCE statement
59#[derive(Debug, Clone, PartialEq)]
60pub struct DropSequenceStmt {
61    pub sequence_name: String,
62    pub cascade: bool, // true for CASCADE, false for RESTRICT
63}
64
65/// ALTER SEQUENCE statement
66#[derive(Debug, Clone, PartialEq)]
67pub struct AlterSequenceStmt {
68    pub sequence_name: String,
69    pub restart_with: Option<i64>,
70    pub increment_by: Option<i64>,
71    pub min_value: Option<Option<i64>>, /* None = no change, Some(None) = NO MINVALUE,
72                                         * Some(Some(n)) = MINVALUE n */
73    pub max_value: Option<Option<i64>>, /* None = no change, Some(None) = NO MAXVALUE,
74                                         * Some(Some(n)) = MAXVALUE n */
75    pub cycle: Option<bool>,
76}
77
78// ============================================================================
79// TYPE
80// ============================================================================
81
82/// CREATE TYPE statement
83#[derive(Debug, Clone, PartialEq)]
84pub struct CreateTypeStmt {
85    pub type_name: String,
86    pub definition: TypeDefinition,
87}
88
89/// Type definition (distinct, structured, or forward)
90#[derive(Debug, Clone, PartialEq)]
91pub enum TypeDefinition {
92    Distinct { base_type: DataType },
93    Structured { attributes: Vec<TypeAttribute> },
94    Forward, // Forward declaration without definition
95}
96
97/// Attribute in a structured type
98#[derive(Debug, Clone, PartialEq)]
99pub struct TypeAttribute {
100    pub name: String,
101    pub data_type: DataType,
102}
103
104/// DROP TYPE statement
105#[derive(Debug, Clone, PartialEq)]
106pub struct DropTypeStmt {
107    pub type_name: String,
108    pub behavior: DropBehavior,
109}
110
111/// Drop behavior for CASCADE/RESTRICT
112#[derive(Debug, Clone, PartialEq)]
113pub enum DropBehavior {
114    Cascade,
115    Restrict,
116}
117
118// ============================================================================
119// COLLATION
120// ============================================================================
121
122/// CREATE COLLATION statement
123///
124/// SQL:1999 Syntax:
125///   CREATE COLLATION collation_name
126///     [FOR character_set]
127///     [FROM source_collation]
128///     [PAD SPACE | NO PAD]
129#[derive(Debug, Clone, PartialEq)]
130pub struct CreateCollationStmt {
131    pub collation_name: String,
132    pub character_set: Option<String>,    // FOR character_set
133    pub source_collation: Option<String>, // FROM source_collation
134    pub pad_space: Option<bool>,          // PAD SPACE (true) | NO PAD (false)
135}
136
137/// DROP COLLATION statement
138#[derive(Debug, Clone, PartialEq)]
139pub struct DropCollationStmt {
140    pub collation_name: String,
141}
142
143// ============================================================================
144// CHARACTER SET
145// ============================================================================
146
147/// CREATE CHARACTER SET statement
148///
149/// SQL:1999 Syntax:
150///   CREATE CHARACTER SET charset_name [AS]
151///     [GET source]
152///     [COLLATE FROM collation]
153#[derive(Debug, Clone, PartialEq)]
154pub struct CreateCharacterSetStmt {
155    pub charset_name: String,
156    pub source: Option<String>,    // GET source
157    pub collation: Option<String>, // COLLATE FROM collation
158}
159
160/// DROP CHARACTER SET statement
161#[derive(Debug, Clone, PartialEq)]
162pub struct DropCharacterSetStmt {
163    pub charset_name: String,
164}
165
166// ============================================================================
167// TRANSLATION
168// ============================================================================
169
170/// CREATE TRANSLATION statement
171///
172/// SQL:1999 Syntax:
173///   CREATE TRANSLATION translation_name
174///     [FOR source_charset TO target_charset]
175///     [FROM translation_source]
176#[derive(Debug, Clone, PartialEq)]
177pub struct CreateTranslationStmt {
178    pub translation_name: String,
179    pub source_charset: Option<String>,     // FOR source_charset
180    pub target_charset: Option<String>,     // TO target_charset
181    pub translation_source: Option<String>, // FROM translation_source
182}
183
184/// DROP TRANSLATION statement
185#[derive(Debug, Clone, PartialEq)]
186pub struct DropTranslationStmt {
187    pub translation_name: String,
188}
189
190// ============================================================================
191// ASSERTION
192// ============================================================================
193
194/// CREATE ASSERTION statement
195#[derive(Debug, Clone, PartialEq)]
196pub struct CreateAssertionStmt {
197    pub assertion_name: String,
198    pub check_condition: Box<Expression>,
199}
200
201/// DROP ASSERTION statement
202#[derive(Debug, Clone, PartialEq)]
203pub struct DropAssertionStmt {
204    pub assertion_name: String,
205    pub cascade: bool, // true for CASCADE, false for RESTRICT
206}
207
208// ============================================================================
209// STORED PROCEDURES AND FUNCTIONS
210// ============================================================================
211
212/// CREATE PROCEDURE statement
213#[derive(Debug, Clone, PartialEq)]
214pub struct CreateProcedureStmt {
215    pub procedure_name: String,
216    pub parameters: Vec<ProcedureParameter>,
217    pub body: ProcedureBody,
218    // Characteristics (Phase 6)
219    pub sql_security: Option<SqlSecurity>,
220    pub comment: Option<String>,
221    pub language: Option<String>,
222}
223
224/// CREATE FUNCTION statement
225#[derive(Debug, Clone, PartialEq)]
226pub struct CreateFunctionStmt {
227    pub function_name: String,
228    pub parameters: Vec<FunctionParameter>,
229    pub return_type: vibesql_types::DataType,
230    pub body: ProcedureBody,
231    // Characteristics (Phase 6)
232    pub deterministic: Option<bool>,
233    pub sql_security: Option<SqlSecurity>,
234    pub comment: Option<String>,
235    pub language: Option<String>,
236}
237
238/// SQL SECURITY characteristic for procedures and functions
239#[derive(Debug, Clone, PartialEq)]
240pub enum SqlSecurity {
241    Definer,
242    Invoker,
243}
244
245/// Parameter in a procedure definition (MySQL-style)
246#[derive(Debug, Clone, PartialEq)]
247pub struct ProcedureParameter {
248    pub mode: ParameterMode,
249    pub name: String,
250    pub data_type: vibesql_types::DataType,
251}
252
253/// Parameter mode: IN, OUT, or INOUT
254#[derive(Debug, Clone, PartialEq)]
255pub enum ParameterMode {
256    In,
257    Out,
258    InOut,
259}
260
261/// Parameter in a function definition (functions typically only have IN parameters)
262#[derive(Debug, Clone, PartialEq)]
263pub struct FunctionParameter {
264    pub name: String,
265    pub data_type: vibesql_types::DataType,
266}
267
268/// Body of a procedure or function
269#[derive(Debug, Clone, PartialEq)]
270pub enum ProcedureBody {
271    /// SQL procedural block: BEGIN ... END
272    BeginEnd(Vec<ProceduralStatement>),
273    /// Raw SQL for initial implementation
274    RawSql(String),
275}
276
277/// A statement within a procedural block
278#[derive(Debug, Clone, PartialEq)]
279pub enum ProceduralStatement {
280    /// SQL statement (SELECT, INSERT, UPDATE, DELETE, etc.)
281    Sql(Box<crate::Statement>),
282    /// Variable declaration: DECLARE var_name data_type
283    Declare {
284        name: String,
285        data_type: vibesql_types::DataType,
286        default_value: Option<Box<Expression>>,
287    },
288    /// Variable assignment: SET var_name = expr
289    Set { name: String, value: Box<Expression> },
290    /// IF statement: IF condition THEN ... ELSE ... END IF
291    If {
292        condition: Box<Expression>,
293        then_statements: Vec<ProceduralStatement>,
294        else_statements: Option<Vec<ProceduralStatement>>,
295    },
296    /// WHILE loop: WHILE condition DO ... END WHILE
297    While { condition: Box<Expression>, statements: Vec<ProceduralStatement> },
298    /// LOOP statement: LOOP ... END LOOP (infinite loop with LEAVE to break)
299    Loop { statements: Vec<ProceduralStatement> },
300    /// REPEAT UNTIL: REPEAT ... UNTIL condition END REPEAT
301    Repeat { statements: Vec<ProceduralStatement>, condition: Box<Expression> },
302    /// RETURN statement (for functions)
303    Return(Box<Expression>),
304    /// LEAVE statement (break out of loops)
305    Leave(String), // Label to leave
306    /// ITERATE statement (continue loop)
307    Iterate(String), // Label to iterate
308}
309
310/// DROP PROCEDURE statement
311#[derive(Debug, Clone, PartialEq)]
312pub struct DropProcedureStmt {
313    pub procedure_name: String,
314    pub if_exists: bool,
315}
316
317/// DROP FUNCTION statement
318#[derive(Debug, Clone, PartialEq)]
319pub struct DropFunctionStmt {
320    pub function_name: String,
321    pub if_exists: bool,
322}
323
324/// CALL statement (execute a procedure)
325#[derive(Debug, Clone, PartialEq)]
326pub struct CallStmt {
327    pub procedure_name: String,
328    pub arguments: Vec<Expression>,
329}