-
Notifications
You must be signed in to change notification settings - Fork 0
/
codegen.rs
167 lines (136 loc) · 4.72 KB
/
codegen.rs
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
use super::*;
pub fn from_row(derivee: &Derivee) -> QuoteStream {
let field_idents = derivee.field_idents();
let col_names = derivee.col_names().map(|s| Literal::string(&s));
let getters = derivee
.fields
.iter()
.zip(col_names)
.map(|(field, name)| {
let ty = &field.ty;
if let Some(extr) = util::get_extr_path(field) {
quote! { #extr(row.get_ref(#name)?)? }
}
else {
quote! { row.get::<_, #ty>(#name)? }
}
});
quote! {
#[inline]
fn from_row(row: &::rusqlite::Row) -> ::rusqlite::Result<Self> {
Ok(Self {
#(#field_idents : #getters),*
})
}
}
}
pub fn inserts(derivee: &Derivee) -> (QuoteStream, QuoteStream) {
let col_names = derivee
.col_names()
.map(|mut str| {
str.insert(0, ':');
Literal::string(&str)
});
let field_idents = derivee
.field_idents()
.zip(&derivee.fields)
.map(|(ident, field)| {
if let Some(bind) = util::get_bind_path(field) {
quote! { &#bind(&self.#ident)? }
}
else {
quote! { &self.#ident }
}
});
let abort_sql = derivee.gen_query(None);
let fail_sql = derivee.gen_query(Some("FAIL"));
let ignore_sql = derivee.gen_query(Some("IGNORE"));
let replace_sql = derivee.gen_query(Some("REPLACE"));
let rollback_sql = derivee.gen_query(Some("ROLLBACK"));
let insert = quote! {
#[inline]
fn insert<C>(&self, conn: C) -> ::rusqlite::Result<()>
where
C: ::std::ops::Deref<Target = ::rusqlite::Connection>
{
self.insert_or(conn, ::exemplar::OnConflict::Abort)
}
};
let insert_or = quote! {
#[inline]
fn insert_or<C>(&self, conn: C, strategy: ::exemplar::OnConflict) -> ::rusqlite::Result<()>
where
C: ::std::ops::Deref<Target = ::rusqlite::Connection>
{
use ::exemplar::OnConflict::*;
let exec = |sql: &str| -> ::rusqlite::Result<()> {
let mut stmt = conn.prepare_cached(sql)?;
let params = [
#((#col_names, #field_idents as &dyn ::rusqlite::ToSql)),*
];
stmt.execute(¶ms)?;
Ok(())
};
match strategy {
Abort => exec(#abort_sql),
Fail => exec(#fail_sql),
Ignore => exec(#ignore_sql),
Replace => exec(#replace_sql),
Rollback => exec(#rollback_sql),
}
}
};
(insert, insert_or)
}
pub fn as_params(derivee: &Derivee) -> QuoteStream {
let col_names = derivee
.col_names()
.map(|mut str| {
str.insert(0, ':');
Literal::string(&str)
});
let field_idents = derivee.field_idents();
quote! {
#[inline]
fn as_params(&self) -> ::std::boxed::Box<[(&str, &dyn ::rusqlite::ToSql)]> {
let params = [
#((#col_names, &self.#field_idents as &dyn ::rusqlite::ToSql)),*
];
::std::boxed::Box::new(params)
}
}
}
pub fn check_test(derivee: &Derivee) -> QuoteStream {
let Some(path) = &derivee.schema else {
return QuoteStream::new()
};
let module = derivee.name.to_string().to_lowercase();
let module = format_ident!("{}_exemplar_check", module);
let table = &derivee.table;
let columns = derivee.col_names();
quote! {
// Hack to prevent clippy::items_after_test_module from firing
#[cfg(not(not(test)))]
#[allow(clippy::items_after_test_module)]
mod #module {
use ::rusqlite::Connection;
#[test]
fn schema_matches() {
let schema = include_str!(#path);
let conn = Connection::open_in_memory()
.expect("In-memory DB connection should open successfully.");
conn.execute_batch(schema)
.expect("Failed to apply provided schema to check DB.");
let mut names = String::new();
conn.pragma(None, "table_info", #table, |row| {
let name = row.get::<_, String>("name")
.expect("Failed to get name for table row.");
names += &name;
names += "\n";
Ok(())
}).expect("Failed to query table_info pragma.");
#(assert!(names.contains(#columns)));*
}
}
}
}