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
use std::fmt::Write;

use crate::create_column::{CreateColumn, CreateColumnImpl};
use crate::error::Error;
use crate::Value;

/**
The trait representing a create table builder
*/
pub trait CreateTable<'until_build, 'post_build> {
    /**
    Add a column to the table.
     */
    fn add_column(self, column: CreateColumnImpl<'until_build, 'post_build>) -> Self;

    /**
    Sets the IF NOT EXISTS trait on the table
     */
    fn if_not_exists(self) -> Self;

    /**
    This method is used to convert the current state for the given dialect in a
    list of tuples.

    Each tuple consists of the query string and the corresponding bind parameters.
     */
    fn build(self) -> Result<Vec<(String, Vec<Value<'post_build>>)>, Error>;
}

/**
The representation of an create table operation.
*/
pub struct CreateTableData<'until_build, 'post_build> {
    pub(crate) name: &'until_build str,
    pub(crate) columns: Vec<CreateColumnImpl<'until_build, 'post_build>>,
    pub(crate) if_not_exists: bool,
    pub(crate) lookup: Vec<Value<'post_build>>,
    pub(crate) pre_statements: Vec<(String, Vec<Value<'post_build>>)>,
    pub(crate) statements: Vec<(String, Vec<Value<'post_build>>)>,
}

/**
The implementation of the [CreateTable] trait for different database dialects.

This should only be constructed via [crate::DBImpl::create_table].
*/
pub enum CreateTableImpl<'until_build, 'post_build> {
    /**
    SQLite representation of the CREATE TABLE operation.
     */
    #[cfg(feature = "sqlite")]
    SQLite(CreateTableData<'until_build, 'post_build>),
    /**
    MySQL representation of the CREATE TABLE operation.
     */
    #[cfg(feature = "mysql")]
    MySQL(CreateTableData<'until_build, 'post_build>),
    /**
    Postgres representation of the CREATE TABLE operation.
     */
    #[cfg(feature = "postgres")]
    Postgres(CreateTableData<'until_build, 'post_build>),
}

impl<'until_build, 'post_build> CreateTable<'until_build, 'post_build>
    for CreateTableImpl<'until_build, 'post_build>
{
    fn add_column(mut self, column: CreateColumnImpl<'until_build, 'post_build>) -> Self {
        match self {
            #[cfg(feature = "sqlite")]
            CreateTableImpl::SQLite(ref mut d) => d.columns.push(column),
            #[cfg(feature = "mysql")]
            CreateTableImpl::MySQL(ref mut d) => d.columns.push(column),
            #[cfg(feature = "postgres")]
            CreateTableImpl::Postgres(ref mut d) => d.columns.push(column),
        }
        self
    }

    fn if_not_exists(mut self) -> Self {
        match self {
            #[cfg(feature = "sqlite")]
            CreateTableImpl::SQLite(ref mut d) => d.if_not_exists = true,
            #[cfg(feature = "mysql")]
            CreateTableImpl::MySQL(ref mut d) => d.if_not_exists = true,
            #[cfg(feature = "postgres")]
            CreateTableImpl::Postgres(ref mut d) => d.if_not_exists = true,
        }
        self
    }

    fn build(self) -> Result<Vec<(String, Vec<Value<'post_build>>)>, Error> {
        match self {
            #[cfg(feature = "sqlite")]
            CreateTableImpl::SQLite(mut d) => {
                let mut s = format!(
                    "CREATE TABLE{} \"{}\" (",
                    if d.if_not_exists {
                        " IF NOT EXISTS"
                    } else {
                        ""
                    },
                    d.name
                );

                let columns_len = d.columns.len() - 1;
                for (idx, mut x) in d.columns.into_iter().enumerate() {
                    #[cfg(any(feature = "mysql", feature = "postgres"))]
                    if let CreateColumnImpl::SQLite(ref mut cci) = x {
                        cci.statements = Some(&mut d.statements)
                    }
                    #[cfg(not(any(feature = "mysql", feature = "postgres")))]
                    {
                        let CreateColumnImpl::SQLite(ref mut cci) = x;
                        cci.statements = Some(&mut d.statements);
                    }

                    x.build(&mut s)?;

                    if idx != columns_len {
                        write!(s, ", ").unwrap();
                    }
                }

                write!(s, ") STRICT; ").unwrap();

                let mut statements = vec![(s, d.lookup)];
                statements.extend(d.statements);

                Ok(statements)
            }
            #[cfg(feature = "mysql")]
            CreateTableImpl::MySQL(mut d) => {
                let mut s = format!(
                    "CREATE TABLE{} `{}` (",
                    if d.if_not_exists {
                        " IF NOT EXISTS"
                    } else {
                        ""
                    },
                    d.name
                );

                let columns_len = d.columns.len() - 1;
                for (idx, mut x) in d.columns.into_iter().enumerate() {
                    #[cfg(any(feature = "postgres", feature = "sqlite"))]
                    if let CreateColumnImpl::MySQL(ref mut cci) = x {
                        cci.statements = Some(&mut d.statements);
                    }
                    #[cfg(not(any(feature = "postgres", feature = "sqlite")))]
                    {
                        let CreateColumnImpl::MySQL(ref mut cci) = x;
                        cci.statements = Some(&mut d.statements);
                    }

                    x.build(&mut s)?;

                    if idx != columns_len {
                        write!(s, ", ").unwrap();
                    }
                }

                write!(s, "); ").unwrap();

                let mut statements = vec![(s, d.lookup)];
                statements.extend(d.statements);

                Ok(statements)
            }
            #[cfg(feature = "postgres")]
            CreateTableImpl::Postgres(mut d) => {
                let mut s = format!(
                    "CREATE TABLE{} \"{}\" (",
                    if d.if_not_exists {
                        " IF NOT EXISTS"
                    } else {
                        ""
                    },
                    d.name
                );

                let columns_len = d.columns.len() - 1;
                for (idx, mut x) in d.columns.into_iter().enumerate() {
                    #[cfg(any(feature = "sqlite", feature = "mysql"))]
                    if let CreateColumnImpl::Postgres(ref mut cci) = x {
                        cci.pre_statements = Some(&mut d.pre_statements);
                        cci.statements = Some(&mut d.statements);
                    }
                    #[cfg(not(any(feature = "sqlite", feature = "mysql")))]
                    {
                        let CreateColumnImpl::Postgres(ref mut cci) = x;
                        cci.pre_statements = Some(&mut d.pre_statements);
                        cci.statements = Some(&mut d.statements);
                    }

                    x.build(&mut s)?;

                    if idx != columns_len {
                        write!(s, ", ").unwrap();
                    }
                }

                write!(s, "); ").unwrap();

                let mut statements = d.pre_statements;
                statements.push((s, d.lookup));
                statements.extend(d.statements);

                Ok(statements)
            }
        }
    }
}