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
//! SQL expression than can be resolved into raw SQL.
pub mod resolver;
pub mod resolver_error;
use crate::sql_arg::SqlArg;
use std::fmt;
/// Hold information about a predicate column.
/// Used by the [SqlExprToken::Predicate]
#[derive(Debug, Clone, PartialEq)]
pub enum PredicateColumn {
/// The column name is self aliased.
SelfAliased(String),
/// The column name is other aliased.
OtherAliased(String),
/// The column name is not aliased.
Literal(String),
/// The column name is already aliased.
/// Tuple with (Alias name, column).
Aliased(String, String),
}
/// Token that makes up [SqlExpr].
/// The expression is a string of tokens.
/// To turn the expression into SQL, each
// token must be fully resolved. This is done by the [Resolver].
#[derive(Debug, Clone)]
pub enum SqlExprToken {
/// Self alias in expression
SelfAlias,
/// Other alias in expression
OtherAlias,
/// Aux param in expression
AuxParam(String),
/// Unresolved arguent in expression
UnresolvedArg,
/// Literal raw SQL in expression
Literal(String),
/// Argument in expression
Arg(SqlArg),
/// Canonical alias in expression
Alias(String),
/// Predicate in expression
/// A predicate token has been introduced to
/// improve SQL formatting.
/// Depending on the number of columns and arguments
/// this translates different SQL.
Predicate {
columns: Vec<PredicateColumn>,
args: Vec<SqlArg>,
},
}
/// A SQL expression is a list of tokens that can be resolved into SQL.
///
/// Library users are advised to not build it programmatically,
/// but to use the `sql_expr!` macro.
/// This macro provides compile time safety and convenience.
///
/// However it's also possible to build it programmatically:
///
/// ### Example
///
/// ```rust
/// use toql_core::sql_expr::SqlExpr;
///
/// let mut e = SqlExpr::literal("SELECT ");
/// e.push_self_alias();
/// e.push_literal("id FROM User ");
/// e.push_self_alias();
///
/// assert_eq!("SELECT ..id FROM User ..", e.to_string());
/// ```
/// The resolver will replace the self aliases into real aliases and build proper SQL.
#[derive(Debug, Clone)]
pub struct SqlExpr {
tokens: Vec<SqlExprToken>,
/// Hint to speed up function first_aux_param
maybe_aux_params: bool,
}
impl SqlExpr {
/// Create SQL expression from token list.
pub fn from(tokens: Vec<SqlExprToken>) -> Self {
let maybe_aux_params = tokens
.iter()
.any(|t| matches!(t, SqlExprToken::AuxParam(_)));
SqlExpr {
tokens,
maybe_aux_params,
}
}
/// Create new empty SQL expression.
pub fn new() -> Self {
SqlExpr {
tokens: Vec::new(),
maybe_aux_params: false,
}
}
/// Create SQL expression from literal.
pub fn literal(lit: impl Into<String>) -> Self {
SqlExpr {
tokens: vec![SqlExprToken::Literal(lit.into())],
maybe_aux_params: false,
}
}
/// Create SQL expression from alias.
pub fn alias(lit: impl Into<String>) -> Self {
SqlExpr {
tokens: vec![SqlExprToken::Alias(lit.into())],
maybe_aux_params: false,
}
}
/// Create SQL expression from self alias.
pub fn self_alias() -> Self {
SqlExpr {
tokens: vec![SqlExprToken::SelfAlias],
maybe_aux_params: false,
}
}
/// Create SQL expression from other alias.
pub fn other_alias() -> Self {
SqlExpr {
tokens: vec![SqlExprToken::OtherAlias],
maybe_aux_params: false,
}
}
/// Create SQL expression from unresolved argument.
pub fn unresolved_arg() -> Self {
SqlExpr {
tokens: vec![SqlExprToken::UnresolvedArg],
maybe_aux_params: false,
}
}
/// Create SQL expression from argument.
pub fn arg(a: SqlArg) -> Self {
SqlExpr {
tokens: vec![SqlExprToken::Arg(a)],
maybe_aux_params: false,
}
}
/// Create SQL expression from aliased column.
pub fn aliased_column(column_name: impl Into<String>) -> Self {
SqlExpr {
tokens: vec![
SqlExprToken::SelfAlias,
SqlExprToken::Literal(".".to_string()),
SqlExprToken::Literal(column_name.into()),
],
maybe_aux_params: false,
}
}
/// Add literal at the end of token list.
pub fn push_literal(&mut self, lit: impl Into<String>) -> &mut Self {
self.tokens.push(SqlExprToken::Literal(lit.into()));
self
}
/// Remove a number of characters -or less- from the end of the list.
/// This affects only the last (literal) token.
pub fn pop_literals(&mut self, count: usize) -> &mut Self {
if let Some(SqlExprToken::Literal(l)) = self.tokens.last_mut() {
for _ in 0..count {
l.pop();
}
}
self
}
/// Return true if last literal token ends with `lit`.
pub fn ends_with_literal(&self, lit: &str) -> bool {
if let Some(SqlExprToken::Literal(l)) = self.tokens.last() {
l.ends_with(lit)
} else {
false
}
}
/// Remove last token from list.
pub fn pop(&mut self) -> &mut Self {
self.tokens.pop();
self
}
/// Add self alias to the end of the list.
pub fn push_self_alias(&mut self) -> &mut Self {
self.tokens.push(SqlExprToken::SelfAlias);
self
}
/// Add other alias to the end of the list.
pub fn push_other_alias(&mut self) -> &mut Self {
self.tokens.push(SqlExprToken::OtherAlias);
self
}
/// Add custom alias to the end of the list.
pub fn push_alias(&mut self, alias: impl Into<String>) -> &mut Self {
self.tokens.push(SqlExprToken::Alias(alias.into()));
self
}
/// Add argument to the end of the list.
pub fn push_arg(&mut self, arg: SqlArg) -> &mut Self {
self.tokens.push(SqlExprToken::Arg(arg));
self
}
/// Add unresolved argument to the end of the list.
pub fn push_unresolved_arg(&mut self) -> &mut Self {
self.tokens.push(SqlExprToken::UnresolvedArg);
self
}
/// Returns true, if list is empty.
pub fn is_empty(&self) -> bool {
self.tokens.is_empty()
}
/// Returns first auxiliary parameter, if any.
pub fn first_aux_param(&self) -> Option<&String> {
if self.maybe_aux_params {
for t in self.tokens() {
if let SqlExprToken::AuxParam(p) = t {
return Some(p);
}
}
}
None
}
/// Add a predicate to the end of the list.
pub fn push_predicate(
&mut self,
columns: Vec<PredicateColumn>,
args: Vec<SqlArg>,
) -> &mut Self {
// Append args to last predicate if they have the same columns
if let Some(SqlExprToken::Predicate {
columns: c,
args: a,
}) = self.tokens.last_mut()
{
if c.iter().eq(&columns) {
a.extend(args);
} else {
self.tokens.push(SqlExprToken::Predicate { columns, args });
}
} else {
self.tokens.push(SqlExprToken::Predicate { columns, args });
}
self
}
/// Add another SQL expression to the end of the list.
pub fn extend(&mut self, expr: impl Into<SqlExpr>) -> &mut Self {
let tokens = expr.into().tokens;
let maybe_aux_params = tokens
.iter()
.any(|t| matches!(t, SqlExprToken::AuxParam(_)));
self.maybe_aux_params |= maybe_aux_params;
self.tokens.extend(tokens);
self
}
pub fn tokens(&self) -> &[SqlExprToken] {
&self.tokens
}
}
impl fmt::Display for SqlExpr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for t in &self.tokens {
write!(f, "{}", t)?;
}
Ok(())
}
}
impl fmt::Display for SqlExprToken {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
SqlExprToken::SelfAlias => write!(f, ".."),
SqlExprToken::OtherAlias => write!(f, "..."),
SqlExprToken::AuxParam(name) => write!(f, "<{}>", name),
SqlExprToken::UnresolvedArg => write!(f, "?"),
SqlExprToken::Literal(l) => write!(f, "{}", l),
SqlExprToken::Arg(a) => write!(f, "{}", a.to_sql_string()),
SqlExprToken::Alias(a) => write!(f, "{}", a),
SqlExprToken::Predicate {
columns: _,
args: _,
} => write!(f, "<Predicate>"),
}
}
}
impl Default for SqlExpr {
fn default() -> Self {
Self::new()
}
}
// Convenience trait that allow
// sql_expr!("SELECT {}", "hkjh")
impl From<&str> for SqlExpr {
fn from(s: &str) -> Self {
SqlExpr::literal(s.to_string())
}
}
impl From<String> for SqlExpr {
fn from(s: String) -> Self {
SqlExpr::literal(s)
}
}
impl From<&String> for SqlExpr {
fn from(s: &String) -> Self {
SqlExpr::literal(s)
}
}
#[cfg(test)]
mod test {
use super::SqlExpr;
use crate::sql_arg::SqlArg;
#[test]
fn create() {
assert_eq!(format!("{}", SqlExpr::default()), "");
assert_eq!(format!("{}", SqlExpr::new()), "");
assert_eq!(format!("{}", SqlExpr::literal("lit")), "lit");
assert_eq!(format!("{}", SqlExpr::alias("alias")), "alias");
assert_eq!(format!("{}", SqlExpr::self_alias()), "..");
assert_eq!(format!("{}", SqlExpr::other_alias()), "...");
assert_eq!(format!("{}", SqlExpr::unresolved_arg()), "?");
assert_eq!(format!("{}", SqlExpr::arg(SqlArg::from("arg"))), "'arg'");
assert_eq!(format!("{}", SqlExpr::aliased_column("col")), "...col");
assert_eq!(format!("{}", <SqlExpr as From<&str>>::from("lit")), "lit");
assert_eq!(
format!("{}", <SqlExpr as From<String>>::from("lit".to_string())),
"lit"
);
assert_eq!(
format!("{}", <SqlExpr as From<&String>>::from(&"lit".to_string())),
"lit"
);
}
}