iridium_core 0.1.9

SQL Server-compatible Rust engine core for Iridium SQL
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
use super::super::common::DataType;
use super::super::common::TableRef;
use super::super::expressions::Expr;
use super::query::{JoinClause, SelectStmt};
use serde::{Deserialize, Serialize};

#[allow(clippy::large_enum_variant)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Statement {
    Dml(DmlStatement),
    Ddl(DdlStatement),
    Procedural(ProceduralStatement),
    Transaction(TransactionStatement),
    Cursor(CursorStatement),
    Session(SessionStatement),
    WithCte {
        ctes: Vec<CteDef>,
        body: Box<Statement>,
    },
}

#[allow(clippy::large_enum_variant)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum DmlStatement {
    Select(Box<SelectStmt>),
    Insert(Box<InsertStmt>),
    Update(Box<UpdateStmt>),
    Delete(Box<DeleteStmt>),
    Merge(Box<MergeStmt>),
    BulkInsert(Box<BulkInsertStmt>),
    InsertBulk(Box<InsertBulkStmt>),
    SelectAssign {
        assignments: Vec<SelectAssignTarget>,
        from: Option<TableRef>,
        selection: Option<Expr>,
    },
}

#[allow(clippy::large_enum_variant)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum DdlStatement {
    Create(Box<CreateStmt>),
    AlterTable {
        table: Vec<String>,
        action: AlterTableAction,
    },
    TruncateTable(Vec<String>),
    DropTable(Vec<String>),
    DropView(Vec<String>),
    DropProcedure(Vec<String>),
    DropFunction(Vec<String>),
    DropTrigger(Vec<String>),
    DropIndex {
        name: Vec<String>,
        table: Vec<String>,
    },
    DropType(Vec<String>),
    DropSchema(String),
    CreateSynonym {
        name: Vec<String>,
        base_object: Vec<String>,
    },
    DropSynonym(Vec<String>),
    CreateSequence {
        name: Vec<String>,
        data_type: Option<DataType>,
        start_with: Option<i64>,
        increment_by: Option<i64>,
        min_value: Option<i64>,
        max_value: Option<i64>,
        cycle: bool,
    },
    DropSequence(Vec<String>),
    CreateIndex(Box<CreateIndexStmt>),
    CreateType {
        name: Vec<String>,
        columns: Vec<ColumnDef>,
    },
    CreateSchema(String),
}

#[allow(clippy::large_enum_variant)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum ProceduralStatement {
    Declare(Vec<DeclareVar>),
    DeclareTableVar {
        name: String,
        columns: Vec<ColumnDef>,
        constraints: Vec<TableConstraint>,
    },
    Synonym {
        name: Vec<String>,
        base_object: Vec<String>,
    },
    Sequence {
        name: Vec<String>,
        data_type: Option<DataType>,
        start_with: Option<i64>,
        increment_by: Option<i64>,
        min_value: Option<i64>,
        max_value: Option<i64>,
        cycle: bool,
    },
    DeclareCursor {
        name: String,
        query: SelectStmt,
    },
    Set {
        variable: String,
        expr: Expr,
    },
    If {
        condition: Expr,
        then_stmt: Box<Statement>,
        else_stmt: Option<Box<Statement>>,
    },
    BeginEnd(Vec<Statement>),
    While {
        condition: Expr,
        stmt: Box<Statement>,
    },
    Break,
    Continue,
    Return(Option<Expr>),
    Print(Expr),
    Raiserror {
        message: Expr,
        severity: Expr,
        state: Expr,
    },
    Throw {
        error_number: Option<Expr>,
        message: Option<Expr>,
        state: Option<Expr>,
    },
    TryCatch {
        try_body: Vec<Statement>,
        catch_body: Vec<Statement>,
    },
    ExecDynamic {
        sql_expr: Expr,
    },
    ExecProcedure {
        return_variable: Option<String>,
        name: Vec<String>,
        args: Vec<ExecArg>,
    },
    SpExecuteSql {
        sql_expr: Expr,
        params_def: Option<Expr>,
        args: Vec<ExecArg>,
    },
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum TransactionStatement {
    Begin(Option<String>),
    Commit(Option<String>),
    Rollback(Option<String>),
    Save(String),
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum CursorStatement {
    Open(String),
    Fetch {
        name: String,
        direction: FetchDirection,
        into_vars: Option<Vec<String>>,
    },
    Close(String),
    Deallocate(String),
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum SessionStatement {
    SetTransactionIsolationLevel(IsolationLevel),
    UseDatabase(String),
    SetOption {
        option: SessionOption,
        value: SessionOptionValue,
    },
    SetIdentityInsert {
        table: Vec<String>,
        on: bool,
    },
    SetContextInfo(Expr),
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct MergeStmt {
    pub target: TableRef,
    pub source: TableRef,
    pub on_condition: Expr,
    pub when_clauses: Vec<MergeWhenClause>,
    pub output: Option<Vec<OutputColumn>>,
    pub output_into: Option<Vec<String>>,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct MergeWhenClause {
    pub when: MergeWhen,
    pub condition: Option<Expr>,
    pub action: MergeAction,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum MergeWhen {
    Matched,
    NotMatched,
    NotMatchedBySource,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum MergeAction {
    Update {
        assignments: Vec<UpdateAssignment>,
    },
    Insert {
        columns: Vec<String>,
        values: Vec<Expr>,
    },
    Delete,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct CteDef {
    pub name: String,
    pub columns: Vec<String>,
    pub query: SelectStmt,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct InsertStmt {
    pub table: Vec<String>,
    pub columns: Vec<String>,
    pub source: InsertSource,
    pub output: Option<Vec<OutputColumn>>,
    pub output_into: Option<Vec<String>>,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum InsertSource {
    Values(Vec<Vec<Expr>>),
    Select(Box<SelectStmt>),
    Exec {
        procedure: Vec<String>,
        args: Vec<Expr>,
    },
    DefaultValues,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct BulkInsertStmt {
    pub table: Vec<String>,
    pub from: String,
    pub options: Vec<BulkInsertOption>,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum BulkInsertOption {
    CheckConstraints,
    FireTriggers,
    KeepIdentity,
    KeepNulls,
    TabLock,
    Format(String),
    DataFiletype(String),
    FieldTerminator(String),
    RowTerminator(String),
    FirstRow(i64),
    LastRow(i64),
    ErrorFile(String),
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct InsertBulkStmt {
    pub table: Vec<String>,
    pub columns: Vec<ColumnDef>,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct UpdateStmt {
    pub table: TableRef,
    pub assignments: Vec<UpdateAssignment>,
    pub top: Option<Expr>,
    pub from: Option<Vec<TableRef>>,
    pub joins: Vec<JoinClause>,
    pub selection: Option<Expr>,
    pub output: Option<Vec<OutputColumn>>,
    pub output_into: Option<Vec<String>>,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct UpdateAssignment {
    pub column: String,
    pub expr: Expr,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct DeleteStmt {
    pub table: Vec<String>,
    pub top: Option<Expr>,
    pub from: Vec<TableRef>,
    pub joins: Vec<JoinClause>,
    pub selection: Option<Expr>,
    pub output: Option<Vec<OutputColumn>>,
    pub output_into: Option<Vec<String>>,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct DeclareVar {
    pub name: String,
    pub data_type: DataType,
    pub initial_value: Option<Expr>,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum CreateStmt {
    Table {
        name: Vec<String>,
        columns: Vec<ColumnDef>,
        constraints: Vec<TableConstraint>,
    },
    View {
        name: Vec<String>,
        query: SelectStmt,
    },
    Procedure {
        name: Vec<String>,
        params: Vec<RoutineParam>,
        body: Vec<Statement>,
    },
    Function {
        name: Vec<String>,
        params: Vec<RoutineParam>,
        returns: Option<DataType>,
        body: FunctionBody,
    },
    Trigger {
        name: Vec<String>,
        table: Vec<String>,
        events: Vec<TriggerEvent>,
        is_instead_of: bool,
        body: Vec<Statement>,
    },
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct RoutineParam {
    pub name: String,
    pub data_type: DataType,
    pub is_output: bool,
    pub is_readonly: bool,
    pub default: Option<Expr>,
}

#[allow(clippy::large_enum_variant)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum FunctionBody {
    ScalarReturn(Expr),
    Block(Vec<Statement>),
    Table(SelectStmt),
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct ColumnDef {
    pub name: String,
    pub data_type: DataType,
    pub is_nullable: Option<bool>,
    pub is_identity: bool,
    pub identity_spec: Option<(i64, i64)>,
    pub is_primary_key: bool,
    pub is_unique: bool,
    pub default_expr: Option<Expr>,
    pub default_constraint_name: Option<String>,
    pub check_expr: Option<Expr>,
    pub check_constraint_name: Option<String>,
    pub computed_expr: Option<Expr>,
    pub foreign_key: Option<ForeignKeyRef>,
    pub collation: Option<String>,
    pub is_clustered: bool,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct ForeignKeyRef {
    pub ref_table: Vec<String>,
    pub ref_columns: Vec<String>,
    pub on_delete: Option<ReferentialAction>,
    pub on_update: Option<ReferentialAction>,
}

#[allow(clippy::large_enum_variant)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum AlterTableAction {
    AddColumn(ColumnDef),
    DropColumn(String),
    AlterColumn {
        name: String,
        data_type: DataType,
        nullable: Option<bool>,
    },
    AddConstraint(TableConstraint),
    DropConstraint(String),
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum TableConstraint {
    PrimaryKey {
        name: Option<String>,
        columns: Vec<IndexColumn>,
        is_clustered: bool,
    },
    Unique {
        name: Option<String>,
        columns: Vec<IndexColumn>,
        is_clustered: bool,
    },
    ForeignKey {
        name: Option<String>,
        columns: Vec<String>,
        ref_table: Vec<String>,
        ref_columns: Vec<String>,
        on_delete: Option<ReferentialAction>,
        on_update: Option<ReferentialAction>,
    },
    Check {
        name: Option<String>,
        expr: Expr,
    },
    Default {
        name: Option<String>,
        column: String,
        expr: Expr,
    },
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum ReferentialAction {
    NoAction,
    Cascade,
    SetNull,
    SetDefault,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum FetchDirection {
    Next,
    Prior,
    First,
    Last,
    Absolute(Expr),
    Relative(Expr),
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct IndexColumn {
    pub name: String,
    pub is_desc: bool,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum IndexOption {
    FillFactor(u8),
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct CreateIndexStmt {
    pub name: Vec<String>,
    pub table: Vec<String>,
    pub is_unique: bool,
    pub is_clustered: bool,
    pub columns: Vec<IndexColumn>,
    pub options: Vec<IndexOption>,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct SelectAssignTarget {
    pub variable: String,
    pub expr: Expr,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct ExecArg {
    pub name: Option<String>,
    pub expr: Expr,
    pub is_output: bool,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct OutputColumn {
    pub source: OutputSource,
    pub column: String,
    pub alias: Option<String>,
    pub is_wildcard: bool,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum OutputSource {
    Inserted,
    Deleted,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum IsolationLevel {
    ReadUncommitted,
    ReadCommitted,
    RepeatableRead,
    Serializable,
    Snapshot,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum SessionOption {
    AnsiNulls,
    QuotedIdentifier,
    NoCount,
    XactAbort,
    FmtOnly,
    NoExec,
    DateFirst,
    Language,
    DateFormat,
    LockTimeout,
    RowCount,
    TextSize,
    ConcatNullYieldsNull,
    ArithAbort,
    QueryGovernorCostLimit,
    DeadlockPriority,
    AnsiNullDfltOn,
    AnsiPadding,
    AnsiWarnings,
    CursorCloseOnCommit,
    ImplicitTransactions,
    StatisticsIo,
    StatisticsTime,
    ShowplanAll,
    AnsiDefaults,
    Unsupported(String),
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum SessionOptionValue {
    Bool(bool),
    Int(i64),
    Text(String),
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum RoutineParamType {
    Scalar(super::super::data_types::DataTypeSpec),
    TableType(super::super::common::ObjectName),
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum TriggerEvent {
    Insert,
    Update,
    Delete,
}