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
use std::rc::Rc;
use crate::{ColumnDef, backend::TableBuilder, foreign_key::*, types::*, prepare::*};
#[derive(Debug, Clone)]
pub struct TableCreateStatement {
pub(crate) table: Option<Rc<dyn Iden>>,
pub(crate) columns: Vec<ColumnDef>,
pub(crate) options: Vec<TableOpt>,
pub(crate) partitions: Vec<TablePartition>,
pub(crate) foreign_keys: Vec<ForeignKeyCreateStatement>,
pub(crate) if_not_exists: bool,
}
#[derive(Debug, Clone)]
pub enum TableOpt {
Engine(String),
Collate(String),
CharacterSet(String),
}
#[derive(Debug, Clone)]
pub enum TablePartition {
}
impl Default for TableCreateStatement {
fn default() -> Self {
Self::new()
}
}
impl TableCreateStatement {
pub fn new() -> Self {
Self {
table: None,
columns: Vec::new(),
options: Vec::new(),
partitions: Vec::new(),
foreign_keys: Vec::new(),
if_not_exists: false,
}
}
#[deprecated(
since = "0.9.6",
note = "Please use the [`TableCreateStatement::if_not_exists`]"
)]
pub fn create_if_not_exists(&mut self) -> &mut Self {
self.if_not_exists = true;
self
}
pub fn if_not_exists(&mut self) -> &mut Self {
self.if_not_exists = true;
self
}
pub fn table<T: 'static>(&mut self, table: T) -> &mut Self
where T: Iden {
self.table = Some(Rc::new(table));
self
}
pub fn col(&mut self, column: ColumnDef) -> &mut Self {
let mut column = column;
column.table = self.table.clone();
self.columns.push(column);
self
}
pub fn foreign_key(&mut self, foreign_key: ForeignKeyCreateStatement) -> &mut Self {
let mut foreign_key = foreign_key;
foreign_key.inside_table_creation = true;
self.foreign_keys.push(foreign_key);
self
}
pub fn engine(&mut self, string: &str) -> &mut Self {
self.opt(TableOpt::Engine(string.into()));
self
}
pub fn collate(&mut self, string: &str) -> &mut Self {
self.opt(TableOpt::Collate(string.into()));
self
}
pub fn character_set(&mut self, string: &str) -> &mut Self {
self.opt(TableOpt::CharacterSet(string.into()));
self
}
fn opt(&mut self, option: TableOpt) -> &mut Self {
self.options.push(option);
self
}
#[allow(dead_code)]
fn partition(&mut self, partition: TablePartition) -> &mut Self {
self.partitions.push(partition);
self
}
pub fn build<T: TableBuilder>(&self, table_builder: T) -> String {
let mut sql = SqlWriter::new();
table_builder.prepare_table_create_statement(self, &mut sql);
sql.result()
}
pub fn build_any(&self, table_builder: &dyn TableBuilder) -> String {
let mut sql = SqlWriter::new();
table_builder.prepare_table_create_statement(self, &mut sql);
sql.result()
}
pub fn to_string<T: TableBuilder>(&self, table_builder: T) -> String {
self.build(table_builder)
}
}