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
use super::*;
impl Expr {
/// Express a `EXISTS` sub-query expression.
///
/// # Examples
///
/// ```
/// use sql_builder::*;
///
/// let query = Query::select()
/// .expr_as(Expr::exists(Query::select().column("id").from("character").take()), "character_exists")
/// .expr_as(Expr::exists(Query::select().column("id").from("glyph").take()), "glyph_exists")
/// .to_owned();
///
/// assert_eq!(
/// query.to_string(MysqlQueryBuilder).unwrap(),
/// r#"SELECT EXISTS(SELECT `id` FROM `character`) AS `character_exists`, EXISTS(SELECT `id` FROM `glyph`) AS `glyph_exists`"#
/// );
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder).unwrap(),
/// r#"SELECT EXISTS(SELECT "id" FROM "character") AS "character_exists", EXISTS(SELECT "id" FROM "glyph") AS "glyph_exists""#
/// );
/// assert_eq!(
/// query.to_string(SqliteQueryBuilder).unwrap(),
/// r#"SELECT EXISTS(SELECT "id" FROM "character") AS "character_exists", EXISTS(SELECT "id" FROM "glyph") AS "glyph_exists""#
/// );
/// ```
pub fn exists(sel: SelectStatement) -> Self {
Self::SubQuery(Some(SubQueryOper::Exists), Box::new(sel.into()))
}
/// Express a `NOT EXISTS` sub-query expression.
/// ```
/// use sql_builder::*;
///
/// let query = Query::select()
/// .expr_as(Expr::not_exists(Query::select().column("id").from("character").take()), "character_exists")
/// .expr_as(Expr::not_exists(Query::select().column("id").from("glyph").take()), "glyph_exists")
/// .to_owned();
///
/// assert_eq!(
/// query.to_string(MysqlQueryBuilder).unwrap(),
/// r#"SELECT NOT EXISTS(SELECT `id` FROM `character`) AS `character_exists`, NOT EXISTS(SELECT `id` FROM `glyph`) AS `glyph_exists`"#
/// );
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder).unwrap(),
/// r#"SELECT NOT EXISTS(SELECT "id" FROM "character") AS "character_exists", NOT EXISTS(SELECT "id" FROM "glyph") AS "glyph_exists""#
/// );
/// assert_eq!(
/// query.to_string(SqliteQueryBuilder).unwrap(),
/// r#"SELECT NOT EXISTS(SELECT "id" FROM "character") AS "character_exists", NOT EXISTS(SELECT "id" FROM "glyph") AS "glyph_exists""#
/// );
/// ```
pub fn not_exists(sel: SelectStatement) -> Self {
Self::exists(sel).not()
}
/// Express a `ANY` sub-query expression.
///
/// # Examples
///
/// ```
/// use sql_builder::*;
///
/// let query = Query::select()
/// .column("id")
/// .from("character")
/// .and_where(Expr::col("id").eq(Expr::any(
/// Query::select().column("id").from("character").take(),
/// )))
/// .to_owned();
///
/// assert_eq!(
/// query.to_string(MysqlQueryBuilder).unwrap(),
/// r#"SELECT `id` FROM `character` WHERE `id` = ANY(SELECT `id` FROM `character`)"#
/// );
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder).unwrap(),
/// r#"SELECT "id" FROM "character" WHERE "id" = ANY(SELECT "id" FROM "character")"#
/// );
/// ```
pub fn any(sel: SelectStatement) -> Self {
Self::SubQuery(Some(SubQueryOper::Any), Box::new(sel.into()))
}
/// Express a `SOME` sub-query expression.
///
/// # Examples
///
/// ```
/// use sql_builder::*;
///
/// let query = Query::select()
/// .column("id")
/// .from("character")
/// .and_where(Expr::col("id").ne(Expr::some(
/// Query::select().column("id").from("character").take(),
/// )))
/// .to_owned();
///
/// assert_eq!(
/// query.to_string(MysqlQueryBuilder).unwrap(),
/// r#"SELECT `id` FROM `character` WHERE `id` <> SOME(SELECT `id` FROM `character`)"#
/// );
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder).unwrap(),
/// r#"SELECT "id" FROM "character" WHERE "id" <> SOME(SELECT "id" FROM "character")"#
/// );
/// ```
pub fn some(sel: SelectStatement) -> Self {
Self::SubQuery(Some(SubQueryOper::Some), Box::new(sel.into()))
}
/// Express a `ALL` sub-query expression.
pub fn all(sel: SelectStatement) -> Self {
Self::SubQuery(Some(SubQueryOper::All), Box::new(sel.into()))
}
/// Adds new `CASE WHEN` to existing case statement.
///
/// # Examples
///
/// ```
/// use sql_builder::*;
///
/// let query = Query::select()
/// .expr_as(
/// Expr::case(
/// Expr::col(("glyph", "aspect")).is_in([2, 4]),
/// true
/// )
/// .finally(false),
/// "is_even"
/// )
/// .from("glyph")
/// .to_owned();
///
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder).unwrap(),
/// r#"SELECT (CASE WHEN ("glyph"."aspect" IN (2, 4)) THEN TRUE ELSE FALSE END) AS "is_even" FROM "glyph""#
/// );
/// ```
pub fn case<C, T>(cond: C, then: T) -> CaseStatement
where
C: IntoCondition,
T: Into<Self>,
{
CaseStatement::new().case(cond, then)
}
/// Keyword `NULL`.
///
/// # Examples
///
/// ```
/// use sql_builder::*;
///
/// let query = Query::select().expr(Expr::null()).to_owned();
///
/// assert_eq!(query.to_string(MysqlQueryBuilder).unwrap(), r#"SELECT NULL"#);
/// assert_eq!(query.to_string(PostgresQueryBuilder).unwrap(), r#"SELECT NULL"#);
/// assert_eq!(query.to_string(SqliteQueryBuilder).unwrap(), r#"SELECT NULL"#);
/// ```
pub fn null() -> Self {
Self::Keyword(Keyword::Null)
}
/// Keyword `CURRENT_DATE`.
///
/// # Examples
///
/// ```
/// use sql_builder::*;
///
/// let query = Query::select().expr(Expr::current_date()).to_owned();
///
/// assert_eq!(query.to_string(MysqlQueryBuilder).unwrap(), r#"SELECT CURRENT_DATE"#);
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder).unwrap(),
/// r#"SELECT CURRENT_DATE"#
/// );
/// assert_eq!(
/// query.to_string(SqliteQueryBuilder).unwrap(),
/// r#"SELECT CURRENT_DATE"#
/// );
/// ```
pub fn current_date() -> Self {
Self::Keyword(Keyword::CurrentDate)
}
/// Keyword `CURRENT_TIMESTAMP`.
///
/// # Examples
///
/// ```
/// use sql_builder::*;
///
/// let query = Query::select().expr(Expr::current_time()).to_owned();
///
/// assert_eq!(query.to_string(MysqlQueryBuilder).unwrap(), r#"SELECT CURRENT_TIME"#);
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder).unwrap(),
/// r#"SELECT CURRENT_TIME"#
/// );
/// assert_eq!(
/// query.to_string(SqliteQueryBuilder).unwrap(),
/// r#"SELECT CURRENT_TIME"#
/// );
/// ```
pub fn current_time() -> Self {
Self::Keyword(Keyword::CurrentTime)
}
/// Keyword `CURRENT_TIMESTAMP`.
///
/// # Examples
///
/// ```
/// use sql_builder::{Expr, MysqlQueryBuilder, PostgresQueryBuilder, Query, SqliteQueryBuilder};
///
/// let query = Query::select().expr(Expr::current_timestamp()).to_owned();
///
/// assert_eq!(
/// query.to_string(MysqlQueryBuilder).unwrap(),
/// r#"SELECT CURRENT_TIMESTAMP"#
/// );
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder).unwrap(),
/// r#"SELECT CURRENT_TIMESTAMP"#
/// );
/// assert_eq!(
/// query.to_string(SqliteQueryBuilder).unwrap(),
/// r#"SELECT CURRENT_TIMESTAMP"#
/// );
/// ```
pub fn current_timestamp() -> Self {
Self::Keyword(Keyword::CurrentTimestamp)
}
/// Keyword `DEFAULT`.
///
/// SQLite has no `DEFAULT` value and refuses to render it.
///
/// # Examples
///
/// ```
/// use sql_builder::{
/// Expr, MysqlQueryBuilder, PostgresQueryBuilder, Query, SqliteQueryBuilder,
/// };
///
/// let query = Query::insert()
/// .columns(["id"])
/// .values_panic([Expr::keyword_default()])
/// .to_owned();
///
/// assert_eq!(
/// query.to_string(MysqlQueryBuilder).unwrap(),
/// r#"INSERT (`id`) VALUES (DEFAULT)"#
/// );
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder).unwrap(),
/// r#"INSERT ("id") VALUES (DEFAULT)"#
/// );
/// assert!(query.to_string(SqliteQueryBuilder).is_err());
/// ```
pub fn keyword_default() -> Self {
Self::Keyword(Keyword::Default)
}
/// Custom keyword.
///
/// # Examples
///
/// ```
/// use sql_builder::*;
///
/// let query = Query::select()
/// .expr(Expr::custom_keyword("test"))
/// .to_owned();
///
/// assert_eq!(query.to_string(MysqlQueryBuilder).unwrap(), r#"SELECT test"#);
/// assert_eq!(query.to_string(PostgresQueryBuilder).unwrap(), r#"SELECT test"#);
/// assert_eq!(query.to_string(SqliteQueryBuilder).unwrap(), r#"SELECT test"#);
/// ```
pub fn custom_keyword<T>(i: T) -> Self
where
T: IntoIden,
{
Self::Keyword(Keyword::Custom(i.into_iden()))
}
pub(crate) fn is_binary(&self) -> bool {
matches!(self, Self::Binary(_, _, _))
}
pub(crate) fn get_bin_oper(&self) -> Option<&BinOper> {
match self {
Self::Binary(_, oper, _) => Some(oper),
_ => None,
}
}
}