-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
gen.rs
484 lines (447 loc) · 18.1 KB
/
gen.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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/*
* Copyright (c) 2017 Boucher, Antoni <bouanto@zoho.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
//! The PostgreSQL code generator.
use std::str::from_utf8;
use literalext::LiteralExt;
use syn::{ExprKind, Ident, LitKind};
use ast::{
Aggregate,
AggregateFilter,
AggregateFilterExpression,
AggregateFilters,
Assignment,
AssignementOperator,
Expression,
FieldList,
Filter,
Filters,
FilterExpression,
FilterValue,
Identifier,
Join,
Limit,
LogicalOperator,
MethodCall,
Order,
RelationalOperator,
Query,
TypedField,
};
use ast::Limit::{
EndRange,
Index,
LimitOffset,
NoLimit,
Range,
StartRange,
};
use sql::escape;
use state::get_primary_key_field_by_table_name;
/// Macro used to generate a ToSql implementation for a filter (for use in WHERE or HAVING).
macro_rules! filter_to_sql {
( $name:ident ) => {
impl ToSql for $name {
fn to_sql(&self) -> String {
self.operand1.to_sql() + " " +
&self.operator.to_sql() + " " +
&self.operand2.to_sql()
}
}
};
}
/// Macro used to generate a ToSql implementation for a filter expression.
macro_rules! filter_expression_to_sql {
( $name:ident ) => {
impl ToSql for $name {
fn to_sql(&self) -> String {
match *self {
$name::Filter(ref filter) => filter.to_sql(),
$name::Filters(ref filters) => filters.to_sql(),
$name::NegFilter(ref filter) =>
"NOT ".to_string() +
&filter.to_sql(),
$name::NoFilters => "".to_string(),
$name::ParenFilter(ref filter) =>
"(".to_string() +
&filter.to_sql() +
")",
$name::FilterValue(ref filter_value) => filter_value.node.to_sql(),
}
}
}
};
}
/// Macro used to generate a ToSql implementation for a slice.
macro_rules! slice_to_sql {
( $name:ty, $sep:expr ) => {
impl ToSql for [$name] {
fn to_sql(&self) -> String {
self.iter().map(ToSql::to_sql).collect::<Vec<_>>().join($sep)
}
}
};
}
/// A generic trait for converting a value to SQL.
pub trait ToSql {
fn to_sql(&self) -> String;
}
impl ToSql for Aggregate {
fn to_sql(&self) -> String {
// TODO: do not use CAST when this is in a HAVING clause.
// TODO: do not hard-code the type.
"CAST(".to_string() + &self.function.to_sql() + "(" + &self.field.expect("Aggregate field").to_sql()
+ ") AS INT)"
}
}
slice_to_sql!(Aggregate, ", ");
filter_to_sql!(AggregateFilter);
filter_expression_to_sql!(AggregateFilterExpression);
filter_to_sql!(AggregateFilters);
impl ToSql for Assignment {
fn to_sql(&self) -> String {
let identifier = self.identifier.expect("Assignment identifier").to_sql();
if let AssignementOperator::Equal = self.operator.node {
identifier + &self.operator.node.to_sql() + &self.value.to_sql()
}
else {
identifier.clone() +
&self.operator.node.to_sql().replace("{}", &identifier) +
&self.value.to_sql()
}
}
}
slice_to_sql!(Assignment, ", ");
impl ToSql for AssignementOperator {
fn to_sql(&self) -> String {
match *self {
AssignementOperator::Add => " = {} + ",
AssignementOperator::Divide => " = {} / ",
AssignementOperator::Equal => " = ",
AssignementOperator::Modulo => " = {} % ",
AssignementOperator::Mul => " = {} * ",
AssignementOperator::Sub => " = {} - ",
}.to_string()
}
}
/// Convert a literal expression to its SQL representation.
/// A non-literal is converted to ? for use with query parameters.
impl ToSql for Expression {
fn to_sql(&self) -> String {
match self.node {
ExprKind::Lit(ref literal) => {
match literal.value {
LitKind::Bool(boolean) => boolean.to_string().to_uppercase(),
LitKind::Other(ref literal) => {
if let Some(byte) = literal.parse_byte() {
"'".to_string() +
&escape((byte as char).to_string()) +
"'"
}
else if let Some(ref bytestring) = literal.parse_bytes() {
"'".to_string() +
// TODO: check if using unwrap() is secure here.
&escape(from_utf8(&bytestring[..]).unwrap().to_string()) +
"'"
}
else if let Some(character) = literal.parse_char() {
"'".to_string() +
&escape(character.to_string()) +
"'"
}
else if let Some(ref float) = literal.parse_float() {
match float.suffix() {
"f32" => float.as_f32().expect("f32").to_string(),
"f64" | "" => float.as_f64().expect("f64").to_string(),
suffix => panic!("Unexpected float suffix {}", suffix),
}
}
else if let Some(ref int) = literal.parse_int() {
match int.suffix() {
"isize" => int.as_i64().expect("isize").to_string(),
"i8" => int.as_i8().expect("i8").to_string(),
"i16" => int.as_i16().expect("i16").to_string(),
"i32" | "" => int.as_i32().expect("i32").to_string(),
"i64" => int.as_i64().expect("i64").to_string(),
"usize" => int.as_u64().expect("usize").to_string(),
"u8" => int.as_u8().expect("u8").to_string(),
"u16" => int.as_u16().expect("u16").to_string(),
"u32" => int.as_u32().expect("u32").to_string(),
"u64" => int.as_u64().expect("u64").to_string(),
suffix => panic!("Unexpected int suffix {}", suffix),
}
}
else if let Some(ref string) = literal.parse_string() {
"'".to_string() +
&escape(string.to_string()) +
"'"
}
else {
#[cfg(not(unstable))]
{
let string = literal.to_string();
if string == "true" || string == "false" {
return string.to_uppercase();
}
}
unreachable!("no other literal types");
}
}
}
},
_ => "?".to_string(),
}
}
}
slice_to_sql!(Expression, ", ");
impl ToSql for FieldList {
fn to_sql(&self) -> String {
self.join(", ")
}
}
filter_to_sql!(Filter);
filter_expression_to_sql!(FilterExpression);
filter_to_sql!(Filters);
impl ToSql for Ident {
fn to_sql(&self) -> String {
self.to_string()
}
}
impl ToSql for FilterValue {
fn to_sql(&self) -> String {
match *self {
FilterValue::Identifier(ref identifier) => identifier.to_sql(),
FilterValue::MethodCall(MethodCall { ref arguments, ref object_name, ref template, .. }) => {
// In the template, $0 represents the object identifier and $1, $2, ... the
// arguments.
let mut sql = template.replace("$0", &object_name.to_string());
let mut index = 1;
for argument in arguments {
sql = sql.replace(&format!("${}", index), &argument.to_sql());
index += 1;
}
sql
},
FilterValue::None => unreachable!("FilterValue::None in FilterValue::to_sql()"),
}
}
}
impl ToSql for Join {
fn to_sql(&self) -> String {
" INNER JOIN ".to_string() + &self.joined_table +
" ON " + &self.base_table + "." + &self.base_field + " = "
+ &self.joined_table + "." + &self.joined_field
}
}
slice_to_sql!(Join, " ");
impl ToSql for Identifier {
fn to_sql(&self) -> String {
self.clone()
}
}
impl ToSql for Limit {
fn to_sql(&self) -> String {
match *self {
EndRange(ref expression) => " LIMIT ".to_string() + &expression.to_sql(),
Index(ref expression) =>
" OFFSET ".to_string() + &expression.to_sql() +
" LIMIT 1",
LimitOffset(ref expression1, ref expression2) =>
" OFFSET ".to_string() + &expression2.to_sql() +
" LIMIT " + &expression1.to_sql(),
NoLimit => "".to_string(),
Range(ref expression1, ref expression2) =>
" OFFSET ".to_string() + &expression1.to_sql() +
" LIMIT " + &expression2.to_sql(),
StartRange(ref expression) => " OFFSET ".to_string() + &expression.to_sql(),
}
}
}
impl ToSql for LogicalOperator {
fn to_sql(&self) -> String {
match *self {
LogicalOperator::And => "AND",
LogicalOperator::Not => "NOT",
LogicalOperator::Or => "OR",
}.to_string()
}
}
impl ToSql for Order {
fn to_sql(&self) -> String {
match *self {
Order::Ascending(ref field) => field.to_sql(),
Order::Descending(ref field) => field.to_sql() + " DESC",
}
}
}
slice_to_sql!(Order, ", ");
/// Convert a whole `Query` to SQL.
impl ToSql for Query {
fn to_sql(&self) -> String {
match *self {
Query::Aggregate{ref aggregates, ref aggregate_filter, ref filter, ref groups, ref joins, ref table} => {
let where_clause = filter_to_where_clause(filter);
let group_clause =
if !groups.is_empty() {
" GROUP BY "
}
else {
""
};
let having_clause =
if let AggregateFilterExpression::NoFilters = *aggregate_filter {
""
}
else {
" HAVING "
};
replace_placeholder(format!("SELECT {aggregates} FROM {table_name}{joins}{where_clause}{filter}{group_clause}{groups}{having_clause}{aggregate_filter}",
aggregates = aggregates.to_sql(),
table_name = table,
joins = joins.to_sql(),
where_clause = where_clause,
filter = filter.to_sql(),
group_clause = group_clause,
groups = groups.to_sql(),
having_clause = having_clause,
aggregate_filter = aggregate_filter.to_sql())
)
},
Query::CreateTable { ref fields, ref table } => {
format!("CREATE TABLE {table} ({fields})",
table = table,
fields = fields.to_sql()
)
},
Query::Delete { ref filter, ref table } => {
let where_clause = filter_to_where_clause(filter);
replace_placeholder(format!("DELETE FROM {table}{where_clause}{filter}",
table = table,
where_clause = where_clause,
filter = filter.to_sql()
)
)
},
Query::Drop { ref table } => {
format!("DROP TABLE {table}", table = table)
},
Query::Insert { ref assignments, ref table } => {
let fields: Vec<_> = assignments.iter().map(|assign|
assign.identifier.expect("Assignment identifier").to_sql()).collect();
let values: Vec<_> = assignments.iter().map(|assign| assign.value.to_sql()).collect();
// Add the SQL code to get the inserted primary key.
// TODO: what to do when there is no primary key?
let return_value = get_primary_key_field_by_table_name(table)
.map_or("".to_string(), |primary_key| " RETURNING ".to_string() + &primary_key);
replace_placeholder(format!("INSERT INTO {table}({fields}) VALUES({values}){return_value}",
table = table,
fields = fields.to_sql(),
values = values.to_sql(),
return_value = return_value
))
},
Query::Select{ref fields, ref filter, ref joins, ref limit, ref order, ref table} => {
let where_clause = filter_to_where_clause(filter);
let order_clause =
if !order.is_empty() {
" ORDER BY "
}
else {
""
};
replace_placeholder(format!("SELECT {fields} FROM {table}{joins}{where_clause}{filter}{order_clause}{order}{limit}",
fields = fields.to_sql(),
table = table,
joins = joins.to_sql(),
where_clause = where_clause,
filter = filter.to_sql(),
order_clause = order_clause,
order = order.to_sql(),
limit = limit.to_sql()
)
)
},
Query::Update { ref assignments, ref filter, ref table } => {
let where_clause = filter_to_where_clause(filter);
replace_placeholder(format!("UPDATE {table} SET {assignments}{where_clause}{filter}",
table = table,
assignments = assignments.to_sql(),
where_clause = where_clause,
filter = filter.to_sql()
)
)
},
}
}
}
impl ToSql for RelationalOperator {
fn to_sql(&self) -> String {
match *self {
RelationalOperator::Equal => "=",
RelationalOperator::LesserThan => "<",
RelationalOperator::LesserThanEqual => "<=",
RelationalOperator::NotEqual => "<>",
RelationalOperator::GreaterThan => ">=",
RelationalOperator::GreaterThanEqual => ">",
}.to_string()
}
}
impl ToSql for TypedField {
fn to_sql(&self) -> String {
self.identifier.to_sql() + " " + &self.typ
}
}
slice_to_sql!(TypedField, ", ");
/// Convert a `FilterExpression` to either " WHERE " or the empty string if there are no filters.
fn filter_to_where_clause(filter: &FilterExpression) -> &str {
match *filter {
FilterExpression::Filter(_) | FilterExpression::Filters(_) | FilterExpression::NegFilter(_) | FilterExpression::ParenFilter(_) | FilterExpression::FilterValue(_) => " WHERE ",
FilterExpression::NoFilters => "",
}
}
// TODO: find a better way to write the symbols ($1, $2, …) in the query.
/// Replace the placeholders `{}` by $# by # where # is the index of the placeholder.
fn replace_placeholder(string: String) -> String {
let mut result = "".to_string();
let mut in_string = false;
let mut skip_next = false;
let mut index = 1;
for character in string.chars() {
if character == '?' && !in_string {
result.push('$');
result.push_str(&index.to_string());
index = index + 1;
}
else {
if character == '\\' {
skip_next = true;
}
else if character == '\'' && !skip_next {
skip_next = false;
in_string = !in_string;
}
else {
skip_next = false;
}
result.push(character);
}
}
result
}