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
use super::common::{lower_data_type, lower_expr, lower_object_name};
use super::dml::lower_select;
use super::lower_statement;
use crate::ast as executor_ast;
use crate::error::DbError;
use crate::parser::ast;

pub fn lower_ddl(ddl: ast::DdlStatement) -> Result<executor_ast::Statement, DbError> {
    match ddl {
        ast::DdlStatement::Create(s) => lower_create(*s),
        ast::DdlStatement::AlterTable { table, action } => Ok(executor_ast::Statement::Ddl(
            executor_ast::statements::DdlStatement::AlterTable(
                executor_ast::statements::ddl::AlterTableStmt {
                    table: lower_object_name(table),
                    action: lower_alter_action(action)?,
                },
            ),
        )),
        ast::DdlStatement::TruncateTable(table) => Ok(executor_ast::Statement::Ddl(
            executor_ast::statements::DdlStatement::TruncateTable(
                executor_ast::statements::ddl::TruncateTableStmt {
                    name: lower_object_name(table),
                },
            ),
        )),
        ast::DdlStatement::DropTable(table) => Ok(executor_ast::Statement::Ddl(
            executor_ast::statements::DdlStatement::DropTable(
                executor_ast::statements::ddl::DropTableStmt {
                    name: lower_object_name(table),
                },
            ),
        )),
        ast::DdlStatement::DropView(name) => Ok(executor_ast::Statement::Ddl(
            executor_ast::statements::DdlStatement::DropView(
                executor_ast::statements::ddl::DropViewStmt {
                    name: lower_object_name(name),
                },
            ),
        )),
        ast::DdlStatement::DropProcedure(name) => Ok(executor_ast::Statement::Ddl(
            executor_ast::statements::DdlStatement::DropProcedure(
                executor_ast::statements::procedural::DropProcedureStmt {
                    name: lower_object_name(name),
                },
            ),
        )),
        ast::DdlStatement::DropFunction(name) => Ok(executor_ast::Statement::Ddl(
            executor_ast::statements::DdlStatement::DropFunction(
                executor_ast::statements::procedural::DropFunctionStmt {
                    name: lower_object_name(name),
                },
            ),
        )),
        ast::DdlStatement::DropTrigger(name) => Ok(executor_ast::Statement::Ddl(
            executor_ast::statements::DdlStatement::DropTrigger(
                executor_ast::statements::procedural::DropTriggerStmt {
                    name: lower_object_name(name),
                },
            ),
        )),
        ast::DdlStatement::DropIndex { name, table } => Ok(executor_ast::Statement::Ddl(
            executor_ast::statements::DdlStatement::DropIndex(
                executor_ast::statements::ddl::DropIndexStmt {
                    name: lower_object_name(name),
                    table: lower_object_name(table),
                },
            ),
        )),
        ast::DdlStatement::DropType(name) => Ok(executor_ast::Statement::Ddl(
            executor_ast::statements::DdlStatement::DropType(
                executor_ast::statements::ddl::DropTypeStmt {
                    name: lower_object_name(name),
                },
            ),
        )),
        ast::DdlStatement::DropSchema(name) => Ok(executor_ast::Statement::Ddl(
            executor_ast::statements::DdlStatement::DropSchema(
                executor_ast::statements::ddl::DropSchemaStmt { name },
            ),
        )),
        ast::DdlStatement::CreateIndex(stmt) => Ok(executor_ast::Statement::Ddl(
            executor_ast::statements::DdlStatement::CreateIndex(
                executor_ast::statements::ddl::CreateIndexStmt {
                    name: lower_object_name(stmt.name),
                    table: lower_object_name(stmt.table),
                    columns: stmt
                        .columns
                        .into_iter()
                        .map(|c| executor_ast::statements::ddl::IndexColumnSpec {
                            name: c.name,
                            is_desc: c.is_desc,
                        })
                        .collect(),
                    is_unique: stmt.is_unique,
                    is_clustered: stmt.is_clustered,
                    options: stmt
                        .options
                        .into_iter()
                        .map(|o| match o {
                            ast::IndexOption::FillFactor(f) => {
                                executor_ast::statements::ddl::IndexOptionSpec::FillFactor(f)
                            }
                        })
                        .collect(),
                },
            ),
        )),
        ast::DdlStatement::CreateType { name, columns } => Ok(executor_ast::Statement::Ddl(
            executor_ast::statements::DdlStatement::CreateType(
                executor_ast::statements::ddl::CreateTypeStmt {
                    name: lower_object_name(name),
                    columns: columns
                        .into_iter()
                        .map(lower_column_def)
                        .collect::<Result<Vec<_>, _>>()?,
                    table_constraints: Vec::new(),
                },
            ),
        )),
        ast::DdlStatement::CreateSchema(name) => Ok(executor_ast::Statement::Ddl(
            executor_ast::statements::DdlStatement::CreateSchema(
                executor_ast::statements::ddl::CreateSchemaStmt { name },
            ),
        )),
        ast::DdlStatement::CreateSynonym { name, base_object } => Ok(executor_ast::Statement::Ddl(
            executor_ast::statements::DdlStatement::CreateSynonym(
                executor_ast::statements::ddl::CreateSynonymStmt {
                    name: lower_object_name(name),
                    base_object: lower_object_name(base_object),
                },
            ),
        )),
        ast::DdlStatement::DropSynonym(name) => Ok(executor_ast::Statement::Ddl(
            executor_ast::statements::DdlStatement::DropSynonym(
                executor_ast::statements::ddl::DropSynonymStmt {
                    name: lower_object_name(name),
                },
            ),
        )),
        ast::DdlStatement::CreateSequence {
            name,
            data_type,
            start_with,
            increment_by,
            min_value,
            max_value,
            cycle,
        } => Ok(executor_ast::Statement::Ddl(
            executor_ast::statements::DdlStatement::CreateSequence(
                executor_ast::statements::ddl::CreateSequenceStmt {
                    name: lower_object_name(name),
                    data_type: data_type
                        .map(lower_data_type)
                        .transpose()?
                        .unwrap_or(executor_ast::data_types::DataTypeSpec::BigInt),
                    start_value: start_with.unwrap_or(1),
                    increment: increment_by.unwrap_or(1),
                    minimum_value: min_value.unwrap_or(i64::MIN),
                    maximum_value: max_value.unwrap_or(i64::MAX),
                    is_cycling: cycle,
                },
            ),
        )),
        ast::DdlStatement::DropSequence(name) => Ok(executor_ast::Statement::Ddl(
            executor_ast::statements::DdlStatement::DropSequence(
                executor_ast::statements::ddl::DropSequenceStmt {
                    name: lower_object_name(name),
                },
            ),
        )),
    }
}

pub fn lower_create(s: ast::CreateStmt) -> Result<executor_ast::Statement, DbError> {
    match s {
        ast::CreateStmt::Table {
            name,
            columns,
            constraints,
        } => Ok(executor_ast::Statement::Ddl(
            executor_ast::statements::DdlStatement::CreateTable(
                executor_ast::statements::ddl::CreateTableStmt {
                    name: lower_object_name(name),
                    columns: columns
                        .into_iter()
                        .map(lower_column_def)
                        .collect::<Result<Vec<_>, _>>()?,
                    table_constraints: constraints
                        .into_iter()
                        .map(lower_table_constraint)
                        .collect::<Result<Vec<_>, _>>()?,
                },
            ),
        )),
        ast::CreateStmt::View { name, query } => Ok(executor_ast::Statement::Procedural(
            executor_ast::statements::ProceduralStatement::CreateView(
                executor_ast::statements::ddl::CreateViewStmt {
                    name: lower_object_name(name),
                    query: lower_select(query)?,
                },
            ),
        )),
        ast::CreateStmt::Procedure { name, params, body } => {
            Ok(executor_ast::Statement::Procedural(
                executor_ast::statements::ProceduralStatement::CreateProcedure(
                    executor_ast::statements::procedural::CreateProcedureStmt {
                        name: lower_object_name(name),
                        params: params
                            .into_iter()
                            .map(lower_routine_param)
                            .collect::<Result<Vec<_>, _>>()?,
                        body: body
                            .into_iter()
                            .map(lower_statement)
                            .collect::<Result<Vec<_>, _>>()?,
                    },
                ),
            ))
        }
        ast::CreateStmt::Function {
            name,
            params,
            returns,
            body,
        } => Ok(executor_ast::Statement::Procedural(
            executor_ast::statements::ProceduralStatement::CreateFunction(
                executor_ast::statements::procedural::CreateFunctionStmt {
                    name: lower_object_name(name),
                    params: params
                        .into_iter()
                        .map(lower_routine_param)
                        .collect::<Result<Vec<_>, _>>()?,
                    returns: returns.map(lower_data_type).transpose()?,
                    body: match body {
                        ast::FunctionBody::ScalarReturn(e) => {
                            executor_ast::statements::procedural::FunctionBody::ScalarReturn(
                                lower_expr(e)?,
                            )
                        }
                        ast::FunctionBody::Block(stmts) => {
                            executor_ast::statements::procedural::FunctionBody::Scalar(
                                stmts
                                    .into_iter()
                                    .map(lower_statement)
                                    .collect::<Result<Vec<_>, _>>()?,
                            )
                        }
                        ast::FunctionBody::Table(sel) => {
                            executor_ast::statements::procedural::FunctionBody::InlineTable(
                                lower_select(sel)?,
                            )
                        }
                    },
                },
            ),
        )),
        ast::CreateStmt::Trigger {
            name,
            table,
            events,
            is_instead_of,
            body,
        } => Ok(executor_ast::Statement::Procedural(
            executor_ast::statements::ProceduralStatement::CreateTrigger(
                executor_ast::statements::procedural::CreateTriggerStmt {
                    name: lower_object_name(name),
                    table: lower_object_name(table),
                    events: events
                        .into_iter()
                        .map(|e| match e {
                            ast::TriggerEvent::Insert => executor_ast::TriggerEvent::Insert,
                            ast::TriggerEvent::Update => executor_ast::TriggerEvent::Update,
                            ast::TriggerEvent::Delete => executor_ast::TriggerEvent::Delete,
                        })
                        .collect(),
                    is_instead_of,
                    body: body
                        .into_iter()
                        .map(lower_statement)
                        .collect::<Result<Vec<_>, _>>()?,
                },
            ),
        )),
    }
}

pub fn lower_column_def(
    c: ast::ColumnDef,
) -> Result<executor_ast::statements::ddl::ColumnSpec, DbError> {
    let nullable_explicit = c.is_nullable.is_some();
    Ok(executor_ast::statements::ddl::ColumnSpec {
        name: c.name,
        data_type: lower_data_type(c.data_type)?,
        nullable: c.is_nullable.unwrap_or(true),
        nullable_explicit,
        identity: c.identity_spec,
        primary_key: c.is_primary_key,
        unique: c.is_unique,
        default: c.default_expr.map(lower_expr).transpose()?,
        default_constraint_name: c.default_constraint_name,
        check: c.check_expr.map(lower_expr).transpose()?,
        check_constraint_name: c.check_constraint_name,
        computed_expr: c.computed_expr.map(lower_expr).transpose()?,
        foreign_key: c
            .foreign_key
            .map(|fk| executor_ast::statements::ddl::ForeignKeyRef {
                referenced_table: lower_object_name(fk.ref_table),
                referenced_columns: fk.ref_columns,
                on_delete: fk.on_delete.map(lower_referential_action),
                on_update: fk.on_update.map(lower_referential_action),
            }),
        collation: c.collation,
        is_clustered: c.is_clustered,
        ansi_padding_on: true,
    })
}

pub fn lower_index_column(
    c: ast::IndexColumn,
) -> executor_ast::statements::ddl::IndexColumnSpec {
    executor_ast::statements::ddl::IndexColumnSpec {
        name: c.name,
        is_desc: c.is_desc,
    }
}

pub fn lower_routine_param(
    p: ast::RoutineParam,
) -> Result<executor_ast::statements::RoutineParam, DbError> {
    let (param_type, is_readonly) = match p.data_type {
        ast::DataType::Custom(name) => {
            if !p.is_readonly {
                return Err(DbError::Parse(format!(
                    "table-valued parameter '{}' must be READONLY",
                    p.name
                )));
            }
            let name_str = name.as_str();
            let (schema, type_name) = match name_str.rsplit_once('.') {
                Some((schema, ty)) => (Some(schema.to_string()), ty.to_string()),
                None => (None, name_str.to_string()),
            };
            (
                executor_ast::statements::RoutineParamType::TableType(executor_ast::ObjectName {
                    database: None,
                    schema,
                    name: type_name,
                }),
                true,
            )
        }
        other => (
            executor_ast::statements::RoutineParamType::Scalar(lower_data_type(other)?),
            p.is_readonly,
        ),
    };
    Ok(executor_ast::statements::RoutineParam {
        name: p.name,
        param_type,
        is_output: p.is_output,
        is_readonly,
        default: p.default.map(lower_expr).transpose()?,
    })
}

pub fn lower_alter_action(
    a: ast::AlterTableAction,
) -> Result<executor_ast::statements::ddl::AlterTableAction, DbError> {
    match a {
        ast::AlterTableAction::AddColumn(c) => Ok(
            executor_ast::statements::ddl::AlterTableAction::AddColumn(lower_column_def(c)?),
        ),
        ast::AlterTableAction::DropColumn(c) => Ok(
            executor_ast::statements::ddl::AlterTableAction::DropColumn(c),
        ),
        ast::AlterTableAction::AlterColumn {
            name,
            data_type,
            nullable,
        } => Ok(
            executor_ast::statements::ddl::AlterTableAction::AlterColumn {
                name,
                data_type: lower_data_type(data_type)?,
                nullable,
            },
        ),
        ast::AlterTableAction::AddConstraint(c) => Ok(
            executor_ast::statements::ddl::AlterTableAction::AddConstraint(lower_table_constraint(
                c,
            )?),
        ),
        ast::AlterTableAction::DropConstraint(c) => {
            Ok(executor_ast::statements::ddl::AlterTableAction::DropConstraint(c))
        }
    }
}

pub fn lower_table_constraint(
    c: ast::TableConstraint,
) -> Result<executor_ast::statements::ddl::TableConstraintSpec, DbError> {
    match c {
        ast::TableConstraint::PrimaryKey {
            name,
            columns,
            is_clustered,
        } => Ok(
            executor_ast::statements::ddl::TableConstraintSpec::PrimaryKey {
                name: name.unwrap_or_default(),
                columns: columns.into_iter().map(lower_index_column).collect(),
                is_clustered,
            },
        ),
        ast::TableConstraint::Unique {
            name,
            columns,
            is_clustered,
        } => Ok(
            executor_ast::statements::ddl::TableConstraintSpec::Unique {
                name: name.unwrap_or_default(),
                columns: columns.into_iter().map(lower_index_column).collect(),
                is_clustered,
            },
        ),
        ast::TableConstraint::ForeignKey {
            name,
            columns,
            ref_table,
            ref_columns,
            on_delete,
            on_update,
        } => Ok(
            executor_ast::statements::ddl::TableConstraintSpec::ForeignKey {
                name: name.unwrap_or_default(),
                columns,
                referenced_table: lower_object_name(ref_table),
                referenced_columns: ref_columns,
                on_delete: on_delete.map(lower_referential_action),
                on_update: on_update.map(lower_referential_action),
            },
        ),
        ast::TableConstraint::Check { name, expr } => {
            Ok(executor_ast::statements::ddl::TableConstraintSpec::Check {
                name: name.unwrap_or_default(),
                expr: lower_expr(expr)?,
            })
        }
        ast::TableConstraint::Default { name, column, expr } => Ok(
            executor_ast::statements::ddl::TableConstraintSpec::Default {
                name: name.unwrap_or_default(),
                column,
                expr: lower_expr(expr)?,
            },
        ),
    }
}

pub fn lower_referential_action(
    a: ast::ReferentialAction,
) -> executor_ast::statements::ddl::ReferentialAction {
    match a {
        ast::ReferentialAction::NoAction => {
            executor_ast::statements::ddl::ReferentialAction::NoAction
        }
        ast::ReferentialAction::Cascade => {
            executor_ast::statements::ddl::ReferentialAction::Cascade
        }
        ast::ReferentialAction::SetNull => {
            executor_ast::statements::ddl::ReferentialAction::SetNull
        }
        ast::ReferentialAction::SetDefault => {
            executor_ast::statements::ddl::ReferentialAction::SetDefault
        }
    }
}