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
use crate::entity::{Entity, Index};
pub fn sql_for_table<T: Entity>() -> (String, String) {
let all_columns = T::columns();
let mut columns = vec!["id integer primary key".to_owned()];
for col in all_columns.iter().skip(1) {
columns.push(format!(
"\"{}\" {}{}",
col.name(),
col.sql_type(),
if col.fk_table_name().is_some() {
format!(
" REFERENCES \"{}\"(\"{}\") ON DELETE CASCADE",
col.fk_table_name().unwrap(),
col.fk_column_name().unwrap()
)
} else {
"".to_owned()
}
));
}
(
format!("DROP TABLE IF EXISTS \"{}\"", <T as Entity>::table_name()),
format!(
"CREATE TABLE IF NOT EXISTS \"{}\" ({})",
<T as Entity>::table_name(),
columns.join(",")
),
)
}
pub fn sql_for_index<I: Index>() -> (String, String) {
(
format!("DROP INDEX IF EXISTS \"{}\"", I::index_name()),
format!(
"CREATE {}INDEX \"{}\" ON \"{}\" ({})",
if I::unique() { "UNIQUE " } else { "" },
I::index_name(),
I::table_name(),
I::column_names()
.iter()
.map(|x| format!("\"{}\"", x))
.collect::<Vec<_>>()
.join(",")
),
)
}
#[cfg(test)]
mod test {
#[derive(serde::Serialize, serde::Deserialize, crate::Entity)]
#[microrm_internal]
pub struct Empty {}
#[derive(serde::Serialize, serde::Deserialize, crate::Entity)]
#[microrm_internal]
pub struct Single {
e: i32,
}
#[derive(serde::Serialize, serde::Deserialize, crate::Entity)]
#[microrm_internal]
pub struct Reference {
e: SingleID,
}
#[test]
fn example_sql_for() {
assert_eq!(
super::sql_for_table::<Empty>(),
(
r#"DROP TABLE IF EXISTS "empty""#.to_owned(),
r#"CREATE TABLE IF NOT EXISTS "empty" (id integer primary key)"#.to_owned()
)
);
assert_eq!(
super::sql_for_table::<Single>(),
(
r#"DROP TABLE IF EXISTS "single""#.to_owned(),
r#"CREATE TABLE IF NOT EXISTS "single" (id integer primary key,"e" integer)"#
.to_owned()
)
);
assert_eq!(
super::sql_for_table::<Reference>(),
(
r#"DROP TABLE IF EXISTS "reference""#.to_owned(),
r#"CREATE TABLE IF NOT EXISTS "reference" (id integer primary key,"e" integer)"#
.to_owned()
)
);
}
#[derive(serde::Serialize, serde::Deserialize, crate::Modelable)]
#[microrm_internal]
pub struct Unit(u8);
#[derive(serde::Serialize, serde::Deserialize, crate::Entity)]
#[microrm_internal]
pub struct UnitNewtype {
newtype: Unit,
}
#[test]
fn unit_newtype_struct() {
assert_eq!(
super::sql_for_table::<UnitNewtype>(),
(
r#"DROP TABLE IF EXISTS "unit_newtype""#.to_owned(),
r#"CREATE TABLE IF NOT EXISTS "unit_newtype" (id integer primary key,"newtype" integer)"#
.to_owned()
)
);
}
#[derive(serde::Serialize, serde::Deserialize, crate::Modelable)]
#[microrm_internal]
pub struct NonUnit(u8, u8);
#[derive(serde::Serialize, serde::Deserialize, crate::Entity)]
#[microrm_internal]
pub struct NonUnitNewtype {
newtype: NonUnit,
}
#[test]
fn nonunit_newtype_struct() {
assert_eq!(
super::sql_for_table::<NonUnitNewtype>(),
(
r#"DROP TABLE IF EXISTS "non_unit_newtype""#.to_owned(),
r#"CREATE TABLE IF NOT EXISTS "non_unit_newtype" (id integer primary key,"newtype" blob)"#.to_owned()
)
)
}
#[derive(serde::Serialize, serde::Deserialize, crate::Entity)]
#[microrm_internal]
pub struct Child {
#[microrm_foreign]
parent_id: SingleID,
}
#[test]
fn test_foreign_key() {
assert_eq!(
super::sql_for_table::<Child>(),
(
r#"DROP TABLE IF EXISTS "child""#.to_owned(),
r#"CREATE TABLE IF NOT EXISTS "child" (id integer primary key,"parent_id" integer REFERENCES "single"("id") ON DELETE CASCADE)"#.to_owned()
)
);
}
#[derive(serde::Serialize, serde::Deserialize, crate::Entity)]
#[microrm_internal]
pub struct KeyValue {
key: String,
value: String,
}
microrm_macros::make_index_internal!(ValueIndex, KeyValue::Value);
microrm_macros::make_index_internal!(!UniqueValueIndex, KeyValue::Value);
#[test]
fn test_indexes() {
assert_eq!(
super::sql_for_index::<ValueIndex>(),
(
r#"DROP INDEX IF EXISTS "value_index""#.to_owned(),
r#"CREATE INDEX "value_index" ON "key_value" ("value")"#.to_owned()
)
);
assert_eq!(
super::sql_for_index::<UniqueValueIndex>(),
(
r#"DROP INDEX IF EXISTS "unique_value_index""#.to_owned(),
r#"CREATE UNIQUE INDEX "unique_value_index" ON "key_value" ("value")"#.to_owned()
)
)
}
#[derive(serde::Serialize, serde::Deserialize, crate::Entity)]
#[microrm_internal]
pub struct VecTest {
e: u64,
test: Vec<usize>,
}
#[test]
fn test_vec() {
assert_eq!(
super::sql_for_table::<VecTest>().1,
r#"CREATE TABLE IF NOT EXISTS "vec_test" (id integer primary key,"e" integer,"test" blob)"#.to_owned()
);
}
#[derive(crate::Modelable, serde::Deserialize, serde::Serialize)]
#[microrm_internal]
pub enum TestEnum {
A,
B,
C,
}
#[derive(serde::Serialize, serde::Deserialize, crate::Entity)]
#[microrm_internal]
pub struct EnumContainer {
before: usize,
e: TestEnum,
after: usize,
}
#[test]
fn test_enum() {
assert_eq!(
super::sql_for_table::<EnumContainer>().1,
r#"CREATE TABLE IF NOT EXISTS "enum_container" (id integer primary key,"before" integer,"e" text,"after" integer)"#.to_owned()
)
}
}