toasty_sql/stmt/
create_table.rs

1use super::{ColumnDef, Statement};
2
3use toasty_core::{
4    driver::Capability,
5    schema::db::{Table, TableId},
6    stmt,
7};
8
9#[derive(Debug, Clone)]
10pub struct CreateTable {
11    /// Name of the table
12    pub table: TableId,
13
14    /// Column definitions
15    pub columns: Vec<ColumnDef>,
16
17    /// Primary key clause
18    pub primary_key: Option<Box<stmt::Expr>>,
19}
20
21impl Statement {
22    pub fn create_table(table: &Table, capability: &Capability) -> Self {
23        CreateTable {
24            table: table.id,
25            columns: table
26                .columns
27                .iter()
28                .map(|column| ColumnDef::from_schema(column, &capability.storage_types))
29                .collect(),
30            primary_key: Some(Box::new(stmt::Expr::record(
31                table
32                    .primary_key
33                    .columns
34                    .iter()
35                    .map(|col| stmt::Expr::column(*col)),
36            ))),
37        }
38        .into()
39    }
40}
41
42impl From<CreateTable> for Statement {
43    fn from(value: CreateTable) -> Self {
44        Self::CreateTable(value)
45    }
46}