sea_query/expr.rs
1//! Building blocks of SQL statements.
2//!
3//! [`Expr`] representing the primitive building block in the expressions.
4//!
5//! [`SimpleExpr`] is the expression common among select fields, where clauses and many other places.
6
7use crate::{func::*, query::*, types::*, value::*};
8
9/// Helper to build a [`SimpleExpr`].
10#[derive(Debug, Clone)]
11pub struct Expr {
12 pub(crate) left: SimpleExpr,
13 pub(crate) right: Option<SimpleExpr>,
14 pub(crate) uopr: Option<UnOper>,
15 pub(crate) bopr: Option<BinOper>,
16}
17
18/// Represents a Simple Expression in SQL.
19///
20/// [`SimpleExpr`] is a node in the expression tree and can represent identifiers, function calls,
21/// various operators and sub-queries.
22#[derive(Debug, Clone, PartialEq)]
23pub enum SimpleExpr {
24 Column(ColumnRef),
25 Tuple(Vec<SimpleExpr>),
26 Unary(UnOper, Box<SimpleExpr>),
27 FunctionCall(FunctionCall),
28 Binary(Box<SimpleExpr>, BinOper, Box<SimpleExpr>),
29 SubQuery(Option<SubQueryOper>, Box<SubQueryStatement>),
30 Value(Value),
31 Values(Vec<Value>),
32 Custom(String),
33 CustomWithExpr(String, Vec<SimpleExpr>),
34 Keyword(Keyword),
35 AsEnum(DynIden, Box<SimpleExpr>),
36 Case(Box<CaseStatement>),
37 Constant(Value),
38}
39
40/// "Operator" methods for building expressions.
41///
42/// Before `sea_query` 0.32.0 (`sea_orm` 1.1.1),
43/// these methods were awailable only on [`Expr`]/[`SimpleExpr`]
44/// and you needed to manually construct these types first.
45///
46/// Now, you can call them directly on any expression type:
47///
48/// ```no_run
49/// # use sea_query::*;
50/// #
51/// let expr = 1_i32.cast_as(Alias::new("REAL"));
52///
53/// let expr = Func::char_length("abc").eq(3_i32);
54///
55/// let expr = Expr::current_date()
56/// .cast_as(Alias::new("TEXT"))
57/// .like("2024%");
58/// ```
59pub trait ExprTrait: Sized {
60 /// Express an arithmetic addition operation.
61 ///
62 /// # Examples
63 ///
64 /// ```
65 /// use sea_query::{tests_cfg::*, *};
66 ///
67 /// let query = Query::select()
68 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
69 /// .from(Char::Table)
70 /// .and_where(1.add(1).eq(2))
71 /// .to_owned();
72 ///
73 /// assert_eq!(
74 /// query.to_string(MysqlQueryBuilder),
75 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE 1 + 1 = 2"#
76 /// );
77 /// assert_eq!(
78 /// query.to_string(PostgresQueryBuilder),
79 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 + 1 = 2"#
80 /// );
81 /// assert_eq!(
82 /// query.to_string(SqliteQueryBuilder),
83 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 + 1 = 2"#
84 /// );
85 /// ```
86 fn add<R>(self, right: R) -> SimpleExpr
87 where
88 R: Into<SimpleExpr>,
89 {
90 ExprTrait::binary(self, BinOper::Add, right)
91 }
92
93 /// Express a `AS enum` expression.
94 ///
95 /// # Examples
96 ///
97 /// ```
98 /// use sea_query::{tests_cfg::*, *};
99 ///
100 /// let query = Query::insert()
101 /// .into_table(Char::Table)
102 /// .columns([Char::FontSize])
103 /// .values_panic(["large".as_enum(Alias::new("FontSizeEnum"))])
104 /// .to_owned();
105 ///
106 /// assert_eq!(
107 /// query.to_string(MysqlQueryBuilder),
108 /// r#"INSERT INTO `character` (`font_size`) VALUES ('large')"#
109 /// );
110 /// assert_eq!(
111 /// query.to_string(PostgresQueryBuilder),
112 /// r#"INSERT INTO "character" ("font_size") VALUES (CAST('large' AS "FontSizeEnum"))"#
113 /// );
114 /// assert_eq!(
115 /// query.to_string(SqliteQueryBuilder),
116 /// r#"INSERT INTO "character" ("font_size") VALUES ('large')"#
117 /// );
118 /// ```
119 #[allow(clippy::wrong_self_convention)]
120 fn as_enum<N>(self, type_name: N) -> SimpleExpr
121 where
122 N: IntoIden;
123
124 fn and<R>(self, right: R) -> SimpleExpr
125 where
126 R: Into<SimpleExpr>,
127 {
128 ExprTrait::binary(self, BinOper::And, right)
129 }
130
131 /// Express a `BETWEEN` expression.
132 ///
133 /// # Examples
134 ///
135 /// ```
136 /// use sea_query::{*, tests_cfg::*};
137 ///
138 /// let query = Query::select()
139 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
140 /// .from(Char::Table)
141 /// .and_where((Char::Table, Char::SizeW).into_column_ref().between(1, 10))
142 /// .to_owned();
143 ///
144 /// assert_eq!(
145 /// query.to_string(MysqlQueryBuilder),
146 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`size_w` BETWEEN 1 AND 10"#
147 /// );
148 /// assert_eq!(
149 /// query.to_string(PostgresQueryBuilder),
150 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" BETWEEN 1 AND 10"#
151 /// );
152 /// assert_eq!(
153 /// query.to_string(SqliteQueryBuilder),
154 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" BETWEEN 1 AND 10"#
155 /// );
156 /// ```
157 fn between<A, B>(self, a: A, b: B) -> SimpleExpr
158 where
159 A: Into<SimpleExpr>,
160 B: Into<SimpleExpr>,
161 {
162 self.binary(
163 BinOper::Between,
164 SimpleExpr::Binary(Box::new(a.into()), BinOper::And, Box::new(b.into())),
165 )
166 }
167
168 /// Create any binary operation
169 ///
170 /// # Examples
171 /// ```
172 /// use sea_query::{*, tests_cfg::*};
173 ///
174 /// let query = Query::select()
175 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
176 /// .from(Char::Table)
177 /// .cond_where(all![
178 /// Char::SizeW.into_column_ref().binary(BinOper::SmallerThan, 10),
179 /// Char::SizeW.into_column_ref().binary(BinOper::GreaterThan, Char::SizeH.into_column_ref())
180 /// ])
181 /// .to_owned();
182 /// assert_eq!(
183 /// query.to_string(MysqlQueryBuilder),
184 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `size_w` < 10 AND `size_w` > `size_h`"#
185 /// );
186 /// assert_eq!(
187 /// query.to_string(PostgresQueryBuilder),
188 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "size_w" < 10 AND "size_w" > "size_h""#
189 /// );
190 /// assert_eq!(
191 /// query.to_string(SqliteQueryBuilder),
192 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "size_w" < 10 AND "size_w" > "size_h""#
193 /// );
194 /// ```
195 fn binary<O, R>(self, op: O, right: R) -> SimpleExpr
196 where
197 O: Into<BinOper>,
198 R: Into<SimpleExpr>;
199
200 /// Express a `CAST AS` expression.
201 ///
202 /// # Examples
203 ///
204 /// ```
205 /// use sea_query::{tests_cfg::*, *};
206 ///
207 /// let query = Query::select()
208 /// .expr("1".cast_as(Alias::new("integer")))
209 /// .to_owned();
210 ///
211 /// assert_eq!(
212 /// query.to_string(MysqlQueryBuilder),
213 /// r#"SELECT CAST('1' AS integer)"#
214 /// );
215 /// assert_eq!(
216 /// query.to_string(PostgresQueryBuilder),
217 /// r#"SELECT CAST('1' AS integer)"#
218 /// );
219 /// assert_eq!(
220 /// query.to_string(SqliteQueryBuilder),
221 /// r#"SELECT CAST('1' AS integer)"#
222 /// );
223 /// ```
224 fn cast_as<N>(self, type_name: N) -> SimpleExpr
225 where
226 N: IntoIden;
227
228 /// Express an arithmetic division operation.
229 ///
230 /// # Examples
231 ///
232 /// ```
233 /// use sea_query::{tests_cfg::*, *};
234 ///
235 /// let query = Query::select()
236 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
237 /// .from(Char::Table)
238 /// .and_where(1.div(1).eq(2))
239 /// .to_owned();
240 ///
241 /// assert_eq!(
242 /// query.to_string(MysqlQueryBuilder),
243 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE 1 / 1 = 2"#
244 /// );
245 /// assert_eq!(
246 /// query.to_string(PostgresQueryBuilder),
247 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 / 1 = 2"#
248 /// );
249 /// assert_eq!(
250 /// query.to_string(SqliteQueryBuilder),
251 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 / 1 = 2"#
252 /// );
253 /// ```
254 fn div<R>(self, right: R) -> SimpleExpr
255 where
256 R: Into<SimpleExpr>,
257 {
258 ExprTrait::binary(self, BinOper::Div, right)
259 }
260
261 /// Express an equal (`=`) expression.
262 ///
263 /// # Examples
264 ///
265 /// ```
266 /// use sea_query::{*, tests_cfg::*};
267 ///
268 /// let query = Query::select()
269 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
270 /// .from(Char::Table)
271 /// // Sometimes, you'll have to qualify the call because of conflicting std traits.
272 /// .and_where(ExprTrait::eq("What!", "Nothing"))
273 /// .and_where(Char::Id.into_column_ref().eq(1))
274 /// .to_owned();
275 ///
276 /// assert_eq!(
277 /// query.to_string(MysqlQueryBuilder),
278 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE 'What!' = 'Nothing' AND `id` = 1"#
279 /// );
280 /// assert_eq!(
281 /// query.to_string(PostgresQueryBuilder),
282 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 'What!' = 'Nothing' AND "id" = 1"#
283 /// );
284 /// assert_eq!(
285 /// query.to_string(SqliteQueryBuilder),
286 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 'What!' = 'Nothing' AND "id" = 1"#
287 /// );
288 /// ```
289 fn eq<R>(self, right: R) -> SimpleExpr
290 where
291 R: Into<SimpleExpr>,
292 {
293 ExprTrait::binary(self, BinOper::Equal, right)
294 }
295
296 /// Express a equal expression between two table columns,
297 /// you will mainly use this to relate identical value between two table columns.
298 ///
299 /// # Examples
300 ///
301 /// ```
302 /// use sea_query::{*, tests_cfg::*};
303 ///
304 /// let query = Query::select()
305 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
306 /// .from(Char::Table)
307 /// .and_where((Char::Table, Char::FontId).into_column_ref().equals((Font::Table, Font::Id)))
308 /// .to_owned();
309 ///
310 /// assert_eq!(
311 /// query.to_string(MysqlQueryBuilder),
312 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`font_id` = `font`.`id`"#
313 /// );
314 /// assert_eq!(
315 /// query.to_string(PostgresQueryBuilder),
316 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."font_id" = "font"."id""#
317 /// );
318 /// assert_eq!(
319 /// query.to_string(SqliteQueryBuilder),
320 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."font_id" = "font"."id""#
321 /// );
322 /// ```
323 fn equals<C>(self, col: C) -> SimpleExpr
324 where
325 C: IntoColumnRef,
326 {
327 self.binary(BinOper::Equal, col.into_column_ref())
328 }
329
330 /// Express a greater than (`>`) expression.
331 ///
332 /// # Examples
333 ///
334 /// ```
335 /// use sea_query::{tests_cfg::*, *};
336 ///
337 /// let query = Query::select()
338 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
339 /// .from(Char::Table)
340 /// .and_where((Char::Table, Char::SizeW).into_column_ref().gt(2))
341 /// .to_owned();
342 ///
343 /// assert_eq!(
344 /// query.to_string(MysqlQueryBuilder),
345 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`size_w` > 2"#
346 /// );
347 /// assert_eq!(
348 /// query.to_string(PostgresQueryBuilder),
349 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" > 2"#
350 /// );
351 /// assert_eq!(
352 /// query.to_string(SqliteQueryBuilder),
353 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" > 2"#
354 /// );
355 /// ```
356 fn gt<R>(self, right: R) -> SimpleExpr
357 where
358 R: Into<SimpleExpr>,
359 {
360 ExprTrait::binary(self, BinOper::GreaterThan, right)
361 }
362
363 /// Express a greater than or equal (`>=`) expression.
364 ///
365 /// # Examples
366 ///
367 /// ```
368 /// use sea_query::{tests_cfg::*, *};
369 ///
370 /// let query = Query::select()
371 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
372 /// .from(Char::Table)
373 /// .and_where((Char::Table, Char::SizeW).into_column_ref().gte(2))
374 /// .to_owned();
375 ///
376 /// assert_eq!(
377 /// query.to_string(MysqlQueryBuilder),
378 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`size_w` >= 2"#
379 /// );
380 /// assert_eq!(
381 /// query.to_string(PostgresQueryBuilder),
382 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" >= 2"#
383 /// );
384 /// assert_eq!(
385 /// query.to_string(SqliteQueryBuilder),
386 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" >= 2"#
387 /// );
388 /// ```
389 fn gte<R>(self, right: R) -> SimpleExpr
390 where
391 R: Into<SimpleExpr>,
392 {
393 ExprTrait::binary(self, BinOper::GreaterThanOrEqual, right)
394 }
395
396 /// Express a `IN` sub-query expression.
397 ///
398 /// # Examples
399 ///
400 /// ```
401 /// use sea_query::{*, tests_cfg::*};
402 ///
403 /// let query = Query::select()
404 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
405 /// .from(Char::Table)
406 /// .and_where(Char::SizeW.into_column_ref().in_subquery(
407 /// Query::select()
408 /// .expr(Expr::cust("3 + 2 * 2"))
409 /// .take()
410 /// ))
411 /// .to_owned();
412 ///
413 /// assert_eq!(
414 /// query.to_string(MysqlQueryBuilder),
415 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `size_w` IN (SELECT 3 + 2 * 2)"#
416 /// );
417 /// assert_eq!(
418 /// query.to_string(PostgresQueryBuilder),
419 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "size_w" IN (SELECT 3 + 2 * 2)"#
420 /// );
421 /// assert_eq!(
422 /// query.to_string(SqliteQueryBuilder),
423 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "size_w" IN (SELECT 3 + 2 * 2)"#
424 /// );
425 /// ```
426 fn in_subquery(self, sel: SelectStatement) -> SimpleExpr {
427 self.binary(
428 BinOper::In,
429 SimpleExpr::SubQuery(None, Box::new(sel.into_sub_query_statement())),
430 )
431 }
432
433 /// Express a `IN` sub expression.
434 ///
435 /// # Examples
436 ///
437 /// ```
438 /// use sea_query::{*, tests_cfg::*};
439 ///
440 /// let query = Query::select()
441 /// .columns([Char::Character, Char::FontId])
442 /// .from(Char::Table)
443 /// .and_where(
444 /// ExprTrait::in_tuples(
445 /// Expr::tuple([
446 /// Expr::col(Char::Character).into(),
447 /// Expr::col(Char::FontId).into(),
448 /// ]),
449 /// [(1, String::from("1")), (2, String::from("2"))]
450 /// )
451 /// )
452 /// .to_owned();
453 ///
454 /// assert_eq!(
455 /// query.to_string(MysqlQueryBuilder),
456 /// r#"SELECT `character`, `font_id` FROM `character` WHERE (`character`, `font_id`) IN ((1, '1'), (2, '2'))"#
457 /// );
458 ///
459 /// assert_eq!(
460 /// query.to_string(PostgresQueryBuilder),
461 /// r#"SELECT "character", "font_id" FROM "character" WHERE ("character", "font_id") IN ((1, '1'), (2, '2'))"#
462 /// );
463 ///
464 /// assert_eq!(
465 /// query.to_string(SqliteQueryBuilder),
466 /// r#"SELECT "character", "font_id" FROM "character" WHERE ("character", "font_id") IN ((1, '1'), (2, '2'))"#
467 /// );
468 /// ```
469 fn in_tuples<V, I>(self, v: I) -> SimpleExpr
470 where
471 V: IntoValueTuple,
472 I: IntoIterator<Item = V>,
473 {
474 self.binary(
475 BinOper::In,
476 SimpleExpr::Tuple(
477 v.into_iter()
478 .map(|m| SimpleExpr::Values(m.into_value_tuple().into_iter().collect()))
479 .collect(),
480 ),
481 )
482 }
483
484 /// Express a `IS` expression.
485 ///
486 /// # Examples
487 ///
488 /// ```
489 /// use sea_query::{*, tests_cfg::*};
490 ///
491 /// let query = Query::select()
492 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
493 /// .from(Char::Table)
494 /// .and_where((Char::Table, Char::Ascii).into_column_ref().is(true))
495 /// .to_owned();
496 ///
497 /// assert_eq!(
498 /// query.to_string(MysqlQueryBuilder),
499 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`ascii` IS TRUE"#
500 /// );
501 /// assert_eq!(
502 /// query.to_string(PostgresQueryBuilder),
503 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."ascii" IS TRUE"#
504 /// );
505 /// assert_eq!(
506 /// query.to_string(SqliteQueryBuilder),
507 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."ascii" IS TRUE"#
508 /// );
509 /// ```
510 fn is<R>(self, right: R) -> SimpleExpr
511 where
512 R: Into<SimpleExpr>,
513 {
514 self.binary(BinOper::Is, right)
515 }
516
517 /// Express a `IN` expression.
518 ///
519 /// # Examples
520 ///
521 /// ```
522 /// use sea_query::{tests_cfg::*, *};
523 ///
524 /// let query = Query::select()
525 /// .columns([Char::Id])
526 /// .from(Char::Table)
527 /// .and_where(
528 /// (Char::Table, Char::SizeW)
529 /// .into_column_ref()
530 /// .is_in([1, 2, 3]),
531 /// )
532 /// .to_owned();
533 ///
534 /// assert_eq!(
535 /// query.to_string(MysqlQueryBuilder),
536 /// r#"SELECT `id` FROM `character` WHERE `character`.`size_w` IN (1, 2, 3)"#
537 /// );
538 /// assert_eq!(
539 /// query.to_string(PostgresQueryBuilder),
540 /// r#"SELECT "id" FROM "character" WHERE "character"."size_w" IN (1, 2, 3)"#
541 /// );
542 /// assert_eq!(
543 /// query.to_string(SqliteQueryBuilder),
544 /// r#"SELECT "id" FROM "character" WHERE "character"."size_w" IN (1, 2, 3)"#
545 /// );
546 /// ```
547 /// Empty value list
548 /// ```
549 /// use sea_query::{tests_cfg::*, *};
550 ///
551 /// let query = Query::select()
552 /// .columns([Char::Id])
553 /// .from(Char::Table)
554 /// .and_where(
555 /// (Char::Table, Char::SizeW)
556 /// .into_column_ref()
557 /// .is_in(Vec::<u8>::new()),
558 /// )
559 /// .to_owned();
560 ///
561 /// assert_eq!(
562 /// query.to_string(MysqlQueryBuilder),
563 /// r#"SELECT `id` FROM `character` WHERE 1 = 2"#
564 /// );
565 /// assert_eq!(
566 /// query.to_string(PostgresQueryBuilder),
567 /// r#"SELECT "id" FROM "character" WHERE 1 = 2"#
568 /// );
569 /// assert_eq!(
570 /// query.to_string(SqliteQueryBuilder),
571 /// r#"SELECT "id" FROM "character" WHERE 1 = 2"#
572 /// );
573 /// ```
574 #[allow(clippy::wrong_self_convention)]
575 fn is_in<V, I>(self, v: I) -> SimpleExpr
576 where
577 V: Into<SimpleExpr>,
578 I: IntoIterator<Item = V>,
579 {
580 self.binary(
581 BinOper::In,
582 SimpleExpr::Tuple(v.into_iter().map(|v| v.into()).collect()),
583 )
584 }
585
586 /// Express a `IS NOT` expression.
587 ///
588 /// # Examples
589 ///
590 /// ```
591 /// use sea_query::{*, tests_cfg::*};
592 ///
593 /// let query = Query::select()
594 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
595 /// .from(Char::Table)
596 /// .and_where((Char::Table, Char::Ascii).into_column_ref().is_not(true))
597 /// .to_owned();
598 ///
599 /// assert_eq!(
600 /// query.to_string(MysqlQueryBuilder),
601 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`ascii` IS NOT TRUE"#
602 /// );
603 /// assert_eq!(
604 /// query.to_string(PostgresQueryBuilder),
605 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."ascii" IS NOT TRUE"#
606 /// );
607 /// assert_eq!(
608 /// query.to_string(SqliteQueryBuilder),
609 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."ascii" IS NOT TRUE"#
610 /// );
611 /// ```
612 #[allow(clippy::wrong_self_convention)]
613 fn is_not<R>(self, right: R) -> SimpleExpr
614 where
615 R: Into<SimpleExpr>,
616 {
617 self.binary(BinOper::IsNot, right)
618 }
619
620 /// Express a `NOT IN` expression.
621 ///
622 /// # Examples
623 ///
624 /// ```
625 /// use sea_query::{tests_cfg::*, *};
626 ///
627 /// let query = Query::select()
628 /// .columns([Char::Id])
629 /// .from(Char::Table)
630 /// .and_where(
631 /// (Char::Table, Char::SizeW)
632 /// .into_column_ref()
633 /// .is_not_in([1, 2, 3]),
634 /// )
635 /// .to_owned();
636 ///
637 /// assert_eq!(
638 /// query.to_string(MysqlQueryBuilder),
639 /// r#"SELECT `id` FROM `character` WHERE `character`.`size_w` NOT IN (1, 2, 3)"#
640 /// );
641 /// assert_eq!(
642 /// query.to_string(PostgresQueryBuilder),
643 /// r#"SELECT "id" FROM "character" WHERE "character"."size_w" NOT IN (1, 2, 3)"#
644 /// );
645 /// assert_eq!(
646 /// query.to_string(SqliteQueryBuilder),
647 /// r#"SELECT "id" FROM "character" WHERE "character"."size_w" NOT IN (1, 2, 3)"#
648 /// );
649 /// ```
650 /// Empty value list
651 /// ```
652 /// use sea_query::{tests_cfg::*, *};
653 ///
654 /// let query = Query::select()
655 /// .columns([Char::Id])
656 /// .from(Char::Table)
657 /// .and_where(
658 /// (Char::Table, Char::SizeW)
659 /// .into_column_ref()
660 /// .is_not_in(Vec::<u8>::new()),
661 /// )
662 /// .to_owned();
663 ///
664 /// assert_eq!(
665 /// query.to_string(MysqlQueryBuilder),
666 /// r#"SELECT `id` FROM `character` WHERE 1 = 1"#
667 /// );
668 /// assert_eq!(
669 /// query.to_string(PostgresQueryBuilder),
670 /// r#"SELECT "id" FROM "character" WHERE 1 = 1"#
671 /// );
672 /// assert_eq!(
673 /// query.to_string(SqliteQueryBuilder),
674 /// r#"SELECT "id" FROM "character" WHERE 1 = 1"#
675 /// );
676 /// ```
677 #[allow(clippy::wrong_self_convention)]
678 fn is_not_in<V, I>(self, v: I) -> SimpleExpr
679 where
680 V: Into<SimpleExpr>,
681 I: IntoIterator<Item = V>,
682 {
683 self.binary(
684 BinOper::NotIn,
685 SimpleExpr::Tuple(v.into_iter().map(|v| v.into()).collect()),
686 )
687 }
688
689 /// Express a `IS NOT NULL` expression.
690 ///
691 /// # Examples
692 ///
693 /// ```
694 /// use sea_query::{*, tests_cfg::*};
695 ///
696 /// let query = Query::select()
697 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
698 /// .from(Char::Table)
699 /// .and_where((Char::Table, Char::SizeW).into_column_ref().is_not_null())
700 /// .to_owned();
701 ///
702 /// assert_eq!(
703 /// query.to_string(MysqlQueryBuilder),
704 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`size_w` IS NOT NULL"#
705 /// );
706 /// assert_eq!(
707 /// query.to_string(PostgresQueryBuilder),
708 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" IS NOT NULL"#
709 /// );
710 /// assert_eq!(
711 /// query.to_string(SqliteQueryBuilder),
712 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" IS NOT NULL"#
713 /// );
714 /// ```
715 #[allow(clippy::wrong_self_convention)]
716 fn is_not_null(self) -> SimpleExpr {
717 self.binary(BinOper::IsNot, Keyword::Null)
718 }
719
720 /// Express a `IS NULL` expression.
721 ///
722 /// # Examples
723 ///
724 /// ```
725 /// use sea_query::{*, tests_cfg::*};
726 ///
727 /// let query = Query::select()
728 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
729 /// .from(Char::Table)
730 /// .and_where((Char::Table, Char::SizeW).into_column_ref().is_null())
731 /// .to_owned();
732 ///
733 /// assert_eq!(
734 /// query.to_string(MysqlQueryBuilder),
735 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`size_w` IS NULL"#
736 /// );
737 /// assert_eq!(
738 /// query.to_string(PostgresQueryBuilder),
739 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" IS NULL"#
740 /// );
741 /// assert_eq!(
742 /// query.to_string(SqliteQueryBuilder),
743 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" IS NULL"#
744 /// );
745 /// ```
746 #[allow(clippy::wrong_self_convention)]
747 fn is_null(self) -> SimpleExpr {
748 self.binary(BinOper::Is, Keyword::Null)
749 }
750
751 /// Express a bitwise left shift.
752 ///
753 /// # Examples
754 ///
755 /// ```
756 /// use sea_query::{tests_cfg::*, *};
757 ///
758 /// let query = Query::select()
759 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
760 /// .from(Char::Table)
761 /// .and_where(1.left_shift(1).eq(2))
762 /// .to_owned();
763 ///
764 /// assert_eq!(
765 /// query.to_string(MysqlQueryBuilder),
766 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE 1 << 1 = 2"#
767 /// );
768 /// assert_eq!(
769 /// query.to_string(PostgresQueryBuilder),
770 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 << 1 = 2"#
771 /// );
772 /// assert_eq!(
773 /// query.to_string(SqliteQueryBuilder),
774 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 << 1 = 2"#
775 /// );
776 /// ```
777 fn left_shift<R>(self, right: R) -> SimpleExpr
778 where
779 R: Into<SimpleExpr>,
780 {
781 self.binary(BinOper::LShift, right)
782 }
783
784 /// Express a `LIKE` expression.
785 ///
786 /// # Examples
787 ///
788 /// ```
789 /// use sea_query::{*, tests_cfg::*};
790 ///
791 /// let query = Query::select()
792 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
793 /// .from(Char::Table)
794 /// .and_where((Char::Table, Char::Character).into_column_ref().like("Ours'%"))
795 /// .to_owned();
796 ///
797 /// assert_eq!(
798 /// query.to_string(MysqlQueryBuilder),
799 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`character` LIKE 'Ours\'%'"#
800 /// );
801 /// assert_eq!(
802 /// query.to_string(PostgresQueryBuilder),
803 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."character" LIKE E'Ours\'%'"#
804 /// );
805 /// assert_eq!(
806 /// query.to_string(SqliteQueryBuilder),
807 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."character" LIKE 'Ours''%'"#
808 /// );
809 /// ```
810 ///
811 /// Like with ESCAPE
812 ///
813 /// ```
814 /// use sea_query::{*, tests_cfg::*};
815 ///
816 /// let query = Query::select()
817 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
818 /// .from(Char::Table)
819 /// .and_where((Char::Table, Char::Character).into_column_ref().like(LikeExpr::new(r"|_Our|_").escape('|')))
820 /// .to_owned();
821 ///
822 /// assert_eq!(
823 /// query.to_string(MysqlQueryBuilder),
824 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`character` LIKE '|_Our|_' ESCAPE '|'"#
825 /// );
826 /// assert_eq!(
827 /// query.to_string(PostgresQueryBuilder),
828 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."character" LIKE '|_Our|_' ESCAPE '|'"#
829 /// );
830 /// assert_eq!(
831 /// query.to_string(SqliteQueryBuilder),
832 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."character" LIKE '|_Our|_' ESCAPE '|'"#
833 /// );
834 /// ```
835 fn like<L>(self, like: L) -> SimpleExpr
836 where
837 L: IntoLikeExpr,
838 {
839 ExprTrait::binary(self, BinOper::Like, like.into_like_expr())
840 }
841
842 /// Express a less than (`<`) expression.
843 ///
844 /// # Examples
845 ///
846 /// ```
847 /// use sea_query::{tests_cfg::*, *};
848 ///
849 /// let query = Query::select()
850 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
851 /// .from(Char::Table)
852 /// .and_where((Char::Table, Char::SizeW).into_column_ref().lt(2))
853 /// .to_owned();
854 ///
855 /// assert_eq!(
856 /// query.to_string(MysqlQueryBuilder),
857 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`size_w` < 2"#
858 /// );
859 /// assert_eq!(
860 /// query.to_string(PostgresQueryBuilder),
861 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" < 2"#
862 /// );
863 /// assert_eq!(
864 /// query.to_string(SqliteQueryBuilder),
865 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" < 2"#
866 /// );
867 /// ```
868 fn lt<R>(self, right: R) -> SimpleExpr
869 where
870 R: Into<SimpleExpr>,
871 {
872 ExprTrait::binary(self, BinOper::SmallerThan, right)
873 }
874
875 /// Express a less than or equal (`<=`) expression.
876 ///
877 /// # Examples
878 ///
879 /// ```
880 /// use sea_query::{tests_cfg::*, *};
881 ///
882 /// let query = Query::select()
883 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
884 /// .from(Char::Table)
885 /// .and_where((Char::Table, Char::SizeW).into_column_ref().lte(2))
886 /// .to_owned();
887 ///
888 /// assert_eq!(
889 /// query.to_string(MysqlQueryBuilder),
890 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`size_w` <= 2"#
891 /// );
892 /// assert_eq!(
893 /// query.to_string(PostgresQueryBuilder),
894 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" <= 2"#
895 /// );
896 /// assert_eq!(
897 /// query.to_string(SqliteQueryBuilder),
898 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" <= 2"#
899 /// );
900 /// ```
901 fn lte<R>(self, right: R) -> SimpleExpr
902 where
903 R: Into<SimpleExpr>,
904 {
905 ExprTrait::binary(self, BinOper::SmallerThanOrEqual, right)
906 }
907
908 /// Express an arithmetic modulo operation.
909 ///
910 /// # Examples
911 ///
912 /// ```
913 /// use sea_query::{tests_cfg::*, *};
914 ///
915 /// let query = Query::select()
916 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
917 /// .from(Char::Table)
918 /// .and_where(1.modulo(1).eq(2))
919 /// .to_owned();
920 ///
921 /// assert_eq!(
922 /// query.to_string(MysqlQueryBuilder),
923 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE 1 % 1 = 2"#
924 /// );
925 /// assert_eq!(
926 /// query.to_string(PostgresQueryBuilder),
927 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 % 1 = 2"#
928 /// );
929 /// assert_eq!(
930 /// query.to_string(SqliteQueryBuilder),
931 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 % 1 = 2"#
932 /// );
933 /// ```
934 fn modulo<R>(self, right: R) -> SimpleExpr
935 where
936 R: Into<SimpleExpr>,
937 {
938 self.binary(BinOper::Mod, right)
939 }
940
941 /// Express an arithmetic multiplication operation.
942 ///
943 /// # Examples
944 ///
945 /// ```
946 /// use sea_query::{tests_cfg::*, *};
947 ///
948 /// let query = Query::select()
949 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
950 /// .from(Char::Table)
951 /// .and_where(1.mul(1).eq(2))
952 /// .to_owned();
953 ///
954 /// assert_eq!(
955 /// query.to_string(MysqlQueryBuilder),
956 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE 1 * 1 = 2"#
957 /// );
958 /// assert_eq!(
959 /// query.to_string(PostgresQueryBuilder),
960 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 * 1 = 2"#
961 /// );
962 /// assert_eq!(
963 /// query.to_string(SqliteQueryBuilder),
964 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 * 1 = 2"#
965 /// );
966 /// ```
967 fn mul<R>(self, right: R) -> SimpleExpr
968 where
969 R: Into<SimpleExpr>,
970 {
971 ExprTrait::binary(self, BinOper::Mul, right)
972 }
973
974 /// Express a not equal (`<>`) expression.
975 ///
976 /// # Examples
977 ///
978 /// ```
979 /// use sea_query::{*, tests_cfg::*};
980 ///
981 /// let query = Query::select()
982 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
983 /// .from(Char::Table)
984 /// // Sometimes, you'll have to qualify the call because of conflicting std traits.
985 /// .and_where(ExprTrait::ne("Morning", "Good"))
986 /// .and_where(Char::Id.into_column_ref().ne(1))
987 /// .to_owned();
988 ///
989 /// assert_eq!(
990 /// query.to_string(MysqlQueryBuilder),
991 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE 'Morning' <> 'Good' AND `id` <> 1"#
992 /// );
993 /// assert_eq!(
994 /// query.to_string(PostgresQueryBuilder),
995 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 'Morning' <> 'Good' AND "id" <> 1"#
996 /// );
997 /// assert_eq!(
998 /// query.to_string(SqliteQueryBuilder),
999 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 'Morning' <> 'Good' AND "id" <> 1"#
1000 /// );
1001 /// ```
1002 fn ne<R>(self, right: R) -> SimpleExpr
1003 where
1004 R: Into<SimpleExpr>,
1005 {
1006 ExprTrait::binary(self, BinOper::NotEqual, right)
1007 }
1008
1009 /// Negates an expression with `NOT`.
1010 ///
1011 /// # Examples
1012 ///
1013 /// ```
1014 /// use sea_query::{*, tests_cfg::*};
1015 ///
1016 /// let query = Query::select()
1017 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
1018 /// .from(Char::Table)
1019 /// .and_where(ExprTrait::not(Expr::col((Char::Table, Char::SizeW)).is_null()))
1020 /// .to_owned();
1021 ///
1022 /// assert_eq!(
1023 /// query.to_string(MysqlQueryBuilder),
1024 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE NOT `character`.`size_w` IS NULL"#
1025 /// );
1026 /// assert_eq!(
1027 /// query.to_string(PostgresQueryBuilder),
1028 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE NOT "character"."size_w" IS NULL"#
1029 /// );
1030 /// assert_eq!(
1031 /// query.to_string(SqliteQueryBuilder),
1032 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE NOT "character"."size_w" IS NULL"#
1033 /// );
1034 /// ```
1035 fn not(self) -> SimpleExpr {
1036 self.unary(UnOper::Not)
1037 }
1038
1039 /// Express a `NOT BETWEEN` expression.
1040 ///
1041 /// # Examples
1042 ///
1043 /// ```
1044 /// use sea_query::{*, tests_cfg::*};
1045 ///
1046 /// let query = Query::select()
1047 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
1048 /// .from(Char::Table)
1049 /// .and_where((Char::Table, Char::SizeW).into_column_ref().not_between(1, 10))
1050 /// .to_owned();
1051 ///
1052 /// assert_eq!(
1053 /// query.to_string(MysqlQueryBuilder),
1054 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`size_w` NOT BETWEEN 1 AND 10"#
1055 /// );
1056 /// assert_eq!(
1057 /// query.to_string(PostgresQueryBuilder),
1058 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" NOT BETWEEN 1 AND 10"#
1059 /// );
1060 /// assert_eq!(
1061 /// query.to_string(SqliteQueryBuilder),
1062 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" NOT BETWEEN 1 AND 10"#
1063 /// );
1064 /// ```
1065 fn not_between<A, B>(self, a: A, b: B) -> SimpleExpr
1066 where
1067 A: Into<SimpleExpr>,
1068 B: Into<SimpleExpr>,
1069 {
1070 self.binary(
1071 BinOper::NotBetween,
1072 SimpleExpr::Binary(Box::new(a.into()), BinOper::And, Box::new(b.into())),
1073 )
1074 }
1075
1076 /// Express a not equal expression between two table columns,
1077 /// you will mainly use this to relate identical value between two table columns.
1078 ///
1079 /// # Examples
1080 ///
1081 /// ```
1082 /// use sea_query::{*, tests_cfg::*};
1083 ///
1084 /// let query = Query::select()
1085 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
1086 /// .from(Char::Table)
1087 /// .and_where((Char::Table, Char::FontId).into_column_ref().not_equals((Font::Table, Font::Id)))
1088 /// .to_owned();
1089 ///
1090 /// assert_eq!(
1091 /// query.to_string(MysqlQueryBuilder),
1092 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`font_id` <> `font`.`id`"#
1093 /// );
1094 /// assert_eq!(
1095 /// query.to_string(PostgresQueryBuilder),
1096 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."font_id" <> "font"."id""#
1097 /// );
1098 /// assert_eq!(
1099 /// query.to_string(SqliteQueryBuilder),
1100 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."font_id" <> "font"."id""#
1101 /// );
1102 /// ```
1103 fn not_equals<C>(self, col: C) -> SimpleExpr
1104 where
1105 C: IntoColumnRef,
1106 {
1107 self.binary(BinOper::NotEqual, col.into_column_ref())
1108 }
1109
1110 /// Express a `NOT IN` sub-query expression.
1111 ///
1112 /// # Examples
1113 ///
1114 /// ```
1115 /// use sea_query::{*, tests_cfg::*};
1116 ///
1117 /// let query = Query::select()
1118 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
1119 /// .from(Char::Table)
1120 /// .and_where(Char::SizeW.into_column_ref().not_in_subquery(
1121 /// Query::select()
1122 /// .expr(Expr::cust("3 + 2 * 2"))
1123 /// .take()
1124 /// ))
1125 /// .to_owned();
1126 ///
1127 /// assert_eq!(
1128 /// query.to_string(MysqlQueryBuilder),
1129 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `size_w` NOT IN (SELECT 3 + 2 * 2)"#
1130 /// );
1131 /// assert_eq!(
1132 /// query.to_string(PostgresQueryBuilder),
1133 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "size_w" NOT IN (SELECT 3 + 2 * 2)"#
1134 /// );
1135 /// assert_eq!(
1136 /// query.to_string(SqliteQueryBuilder),
1137 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "size_w" NOT IN (SELECT 3 + 2 * 2)"#
1138 /// );
1139 /// ```
1140 fn not_in_subquery(self, sel: SelectStatement) -> SimpleExpr {
1141 self.binary(
1142 BinOper::NotIn,
1143 SimpleExpr::SubQuery(None, Box::new(sel.into_sub_query_statement())),
1144 )
1145 }
1146
1147 /// Express a `NOT LIKE` expression.
1148 ///
1149 /// # Examples
1150 ///
1151 /// ```
1152 /// use sea_query::{*, tests_cfg::*};
1153 ///
1154 /// let query = Query::select()
1155 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
1156 /// .from(Char::Table)
1157 /// .and_where((Char::Table, Char::Character).into_column_ref().not_like("Ours'%"))
1158 /// .to_owned();
1159 ///
1160 /// assert_eq!(
1161 /// query.to_string(MysqlQueryBuilder),
1162 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`character` NOT LIKE 'Ours\'%'"#
1163 /// );
1164 /// assert_eq!(
1165 /// query.to_string(PostgresQueryBuilder),
1166 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."character" NOT LIKE E'Ours\'%'"#
1167 /// );
1168 /// assert_eq!(
1169 /// query.to_string(SqliteQueryBuilder),
1170 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."character" NOT LIKE 'Ours''%'"#
1171 /// );
1172 /// ```
1173 fn not_like<L>(self, like: L) -> SimpleExpr
1174 where
1175 L: IntoLikeExpr,
1176 {
1177 ExprTrait::binary(self, BinOper::NotLike, like.into_like_expr())
1178 }
1179
1180 /// Express a logical `OR` operation.
1181 ///
1182 /// # Examples
1183 ///
1184 /// ```
1185 /// use sea_query::{tests_cfg::*, *};
1186 ///
1187 /// let query = Query::select()
1188 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
1189 /// .from(Char::Table)
1190 /// .and_where(false.or(true))
1191 /// .to_owned();
1192 ///
1193 /// assert_eq!(
1194 /// query.to_string(MysqlQueryBuilder),
1195 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE FALSE OR TRUE"#
1196 /// );
1197 /// assert_eq!(
1198 /// query.to_string(PostgresQueryBuilder),
1199 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE FALSE OR TRUE"#
1200 /// );
1201 /// assert_eq!(
1202 /// query.to_string(SqliteQueryBuilder),
1203 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE FALSE OR TRUE"#
1204 /// );
1205 /// ```
1206 fn or<R>(self, right: R) -> SimpleExpr
1207 where
1208 R: Into<SimpleExpr>,
1209 {
1210 ExprTrait::binary(self, BinOper::Or, right)
1211 }
1212
1213 /// Express a bitwise right shift.
1214 ///
1215 /// # Examples
1216 ///
1217 /// ```
1218 /// use sea_query::{tests_cfg::*, *};
1219 ///
1220 /// let query = Query::select()
1221 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
1222 /// .from(Char::Table)
1223 /// .and_where(1.right_shift(1).eq(2))
1224 /// .to_owned();
1225 ///
1226 /// assert_eq!(
1227 /// query.to_string(MysqlQueryBuilder),
1228 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE 1 >> 1 = 2"#
1229 /// );
1230 /// assert_eq!(
1231 /// query.to_string(PostgresQueryBuilder),
1232 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 >> 1 = 2"#
1233 /// );
1234 /// assert_eq!(
1235 /// query.to_string(SqliteQueryBuilder),
1236 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 >> 1 = 2"#
1237 /// );
1238 /// ```
1239 fn right_shift<R>(self, right: R) -> SimpleExpr
1240 where
1241 R: Into<SimpleExpr>,
1242 {
1243 self.binary(BinOper::RShift, right)
1244 }
1245
1246 /// Express an arithmetic subtraction operation.
1247 ///
1248 /// # Examples
1249 ///
1250 /// ```
1251 /// use sea_query::{tests_cfg::*, *};
1252 ///
1253 /// let query = Query::select()
1254 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
1255 /// .from(Char::Table)
1256 /// .and_where(1.sub(1).eq(2))
1257 /// .to_owned();
1258 ///
1259 /// assert_eq!(
1260 /// query.to_string(MysqlQueryBuilder),
1261 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE 1 - 1 = 2"#
1262 /// );
1263 /// assert_eq!(
1264 /// query.to_string(PostgresQueryBuilder),
1265 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 - 1 = 2"#
1266 /// );
1267 /// assert_eq!(
1268 /// query.to_string(SqliteQueryBuilder),
1269 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 - 1 = 2"#
1270 /// );
1271 /// ```
1272 fn sub<R>(self, right: R) -> SimpleExpr
1273 where
1274 R: Into<SimpleExpr>,
1275 {
1276 ExprTrait::binary(self, BinOper::Sub, right)
1277 }
1278
1279 /// Apply any unary operator to the expression.
1280 ///
1281 /// # Examples
1282 ///
1283 /// ```
1284 /// use sea_query::{*, tests_cfg::*};
1285 ///
1286 /// let query = Query::select()
1287 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
1288 /// .from(Char::Table)
1289 /// .and_where(Expr::col((Char::Table, Char::SizeW)).is_null().unary(UnOper::Not))
1290 /// .to_owned();
1291 ///
1292 /// assert_eq!(
1293 /// query.to_string(MysqlQueryBuilder),
1294 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE NOT `character`.`size_w` IS NULL"#
1295 /// );
1296 /// assert_eq!(
1297 /// query.to_string(PostgresQueryBuilder),
1298 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE NOT "character"."size_w" IS NULL"#
1299 /// );
1300 /// assert_eq!(
1301 /// query.to_string(SqliteQueryBuilder),
1302 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE NOT "character"."size_w" IS NULL"#
1303 /// );
1304 /// ```
1305 fn unary(self, o: UnOper) -> SimpleExpr;
1306
1307 /// Express a bitwise AND operation.
1308 ///
1309 /// # Examples
1310 ///
1311 /// ```
1312 /// use sea_query::{tests_cfg::*, *};
1313 ///
1314 /// let query = Query::select().expr(1.bit_and(2).eq(3)).to_owned();
1315 ///
1316 /// assert_eq!(
1317 /// query.to_string(PostgresQueryBuilder),
1318 /// r#"SELECT (1 & 2) = 3"#
1319 /// );
1320 ///
1321 /// let query = Query::select()
1322 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
1323 /// .from(Char::Table)
1324 /// .and_where(1.bit_and(1).eq(1))
1325 /// .to_owned();
1326 ///
1327 /// assert_eq!(
1328 /// query.to_string(MysqlQueryBuilder),
1329 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE (1 & 1) = 1"#
1330 /// );
1331 /// assert_eq!(
1332 /// query.to_string(PostgresQueryBuilder),
1333 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE (1 & 1) = 1"#
1334 /// );
1335 /// assert_eq!(
1336 /// query.to_string(SqliteQueryBuilder),
1337 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE (1 & 1) = 1"#
1338 /// );
1339 /// ```
1340 fn bit_and<R>(self, right: R) -> SimpleExpr
1341 where
1342 R: Into<SimpleExpr>,
1343 {
1344 ExprTrait::binary(self, BinOper::BitAnd, right)
1345 }
1346
1347 /// Express a bitwise OR operation.
1348 ///
1349 /// # Examples
1350 ///
1351 /// ```
1352 /// use sea_query::{tests_cfg::*, *};
1353 ///
1354 /// let query = Query::select()
1355 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
1356 /// .from(Char::Table)
1357 /// .and_where(1.bit_or(1).eq(1))
1358 /// .to_owned();
1359 ///
1360 /// assert_eq!(
1361 /// query.to_string(MysqlQueryBuilder),
1362 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE (1 | 1) = 1"#
1363 /// );
1364 /// assert_eq!(
1365 /// query.to_string(PostgresQueryBuilder),
1366 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE (1 | 1) = 1"#
1367 /// );
1368 /// assert_eq!(
1369 /// query.to_string(SqliteQueryBuilder),
1370 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE (1 | 1) = 1"#
1371 /// );
1372 /// ```
1373 fn bit_or<R>(self, right: R) -> SimpleExpr
1374 where
1375 R: Into<SimpleExpr>,
1376 {
1377 ExprTrait::binary(self, BinOper::BitOr, right)
1378 }
1379}
1380
1381/// This generic implementation covers all expression types,
1382/// including [ColumnRef], [Value], [FunctionCall], [SimpleExpr]...
1383impl<T> ExprTrait for T
1384where
1385 T: Into<SimpleExpr>,
1386{
1387 fn as_enum<N>(self, type_name: N) -> SimpleExpr
1388 where
1389 N: IntoIden,
1390 {
1391 SimpleExpr::AsEnum(type_name.into_iden(), Box::new(self.into()))
1392 }
1393
1394 fn binary<O, R>(self, op: O, right: R) -> SimpleExpr
1395 where
1396 O: Into<BinOper>,
1397 R: Into<SimpleExpr>,
1398 {
1399 SimpleExpr::Binary(Box::new(self.into()), op.into(), Box::new(right.into()))
1400 }
1401
1402 fn cast_as<N>(self, type_name: N) -> SimpleExpr
1403 where
1404 N: IntoIden,
1405 {
1406 SimpleExpr::FunctionCall(Func::cast_as(self, type_name))
1407 }
1408
1409 fn unary(self, op: UnOper) -> SimpleExpr {
1410 SimpleExpr::Unary(op, Box::new(self.into()))
1411 }
1412}
1413
1414impl Expr {
1415 fn new_with_left<T>(left: T) -> Self
1416 where
1417 T: Into<SimpleExpr>,
1418 {
1419 let left = left.into();
1420 Self {
1421 left,
1422 right: None,
1423 uopr: None,
1424 bopr: None,
1425 }
1426 }
1427
1428 #[deprecated(since = "0.29.0", note = "Please use the [`Asterisk`]")]
1429 pub fn asterisk() -> Self {
1430 Self::col(Asterisk)
1431 }
1432
1433 /// Express the target column without table prefix.
1434 ///
1435 /// # Examples
1436 ///
1437 /// ```
1438 /// use sea_query::{tests_cfg::*, *};
1439 ///
1440 /// let query = Query::select()
1441 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
1442 /// .from(Char::Table)
1443 /// .and_where(Expr::col(Char::SizeW).eq(1))
1444 /// .to_owned();
1445 ///
1446 /// assert_eq!(
1447 /// query.to_string(MysqlQueryBuilder),
1448 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `size_w` = 1"#
1449 /// );
1450 /// assert_eq!(
1451 /// query.to_string(PostgresQueryBuilder),
1452 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "size_w" = 1"#
1453 /// );
1454 /// assert_eq!(
1455 /// query.to_string(SqliteQueryBuilder),
1456 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "size_w" = 1"#
1457 /// );
1458 /// ```
1459 ///
1460 /// ```
1461 /// use sea_query::{tests_cfg::*, *};
1462 ///
1463 /// let query = Query::select()
1464 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
1465 /// .from(Char::Table)
1466 /// .and_where(Expr::col((Char::Table, Char::SizeW)).eq(1))
1467 /// .to_owned();
1468 ///
1469 /// assert_eq!(
1470 /// query.to_string(MysqlQueryBuilder),
1471 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`size_w` = 1"#
1472 /// );
1473 /// assert_eq!(
1474 /// query.to_string(PostgresQueryBuilder),
1475 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" = 1"#
1476 /// );
1477 /// assert_eq!(
1478 /// query.to_string(SqliteQueryBuilder),
1479 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" = 1"#
1480 /// );
1481 /// ```
1482 pub fn col<T>(n: T) -> Self
1483 where
1484 T: IntoColumnRef,
1485 {
1486 Self::new_with_left(n.into_column_ref())
1487 }
1488
1489 /// Express the target column without table prefix, returning a [`SimpleExpr`].
1490 ///
1491 /// # Examples
1492 ///
1493 /// ```
1494 /// use sea_query::{tests_cfg::*, *};
1495 ///
1496 /// let query = Query::select()
1497 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
1498 /// .from(Char::Table)
1499 /// .and_where(Expr::column(Char::SizeW).eq(1))
1500 /// .to_owned();
1501 ///
1502 /// assert_eq!(
1503 /// query.to_string(MysqlQueryBuilder),
1504 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `size_w` = 1"#
1505 /// );
1506 /// assert_eq!(
1507 /// query.to_string(PostgresQueryBuilder),
1508 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "size_w" = 1"#
1509 /// );
1510 /// assert_eq!(
1511 /// query.to_string(SqliteQueryBuilder),
1512 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "size_w" = 1"#
1513 /// );
1514 /// ```
1515 ///
1516 /// ```
1517 /// use sea_query::{tests_cfg::*, *};
1518 ///
1519 /// let query = Query::select()
1520 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
1521 /// .from(Char::Table)
1522 /// .and_where(Expr::column((Char::Table, Char::SizeW)).eq(1))
1523 /// .to_owned();
1524 ///
1525 /// assert_eq!(
1526 /// query.to_string(MysqlQueryBuilder),
1527 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`size_w` = 1"#
1528 /// );
1529 /// assert_eq!(
1530 /// query.to_string(PostgresQueryBuilder),
1531 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" = 1"#
1532 /// );
1533 /// assert_eq!(
1534 /// query.to_string(SqliteQueryBuilder),
1535 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" = 1"#
1536 /// );
1537 /// ```
1538 pub fn column<T>(n: T) -> SimpleExpr
1539 where
1540 T: IntoColumnRef,
1541 {
1542 SimpleExpr::Column(n.into_column_ref())
1543 }
1544
1545 /// Wraps tuple of `SimpleExpr`, can be used for tuple comparison
1546 ///
1547 /// # Examples
1548 ///
1549 /// ```
1550 /// use sea_query::{tests_cfg::*, *};
1551 ///
1552 /// let query = Query::select()
1553 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
1554 /// .from(Char::Table)
1555 /// .and_where(
1556 /// Expr::tuple([Expr::col(Char::SizeW).into(), Expr::value(100)])
1557 /// .lt(Expr::tuple([Expr::value(500), Expr::value(100)])))
1558 /// .to_owned();
1559 ///
1560 /// assert_eq!(
1561 /// query.to_string(MysqlQueryBuilder),
1562 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE (`size_w`, 100) < (500, 100)"#
1563 /// );
1564 /// assert_eq!(
1565 /// query.to_string(PostgresQueryBuilder),
1566 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE ("size_w", 100) < (500, 100)"#
1567 /// );
1568 /// assert_eq!(
1569 /// query.to_string(SqliteQueryBuilder),
1570 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE ("size_w", 100) < (500, 100)"#
1571 /// );
1572 /// ```
1573 pub fn tuple<I>(n: I) -> Self
1574 where
1575 I: IntoIterator<Item = SimpleExpr>,
1576 {
1577 Expr::expr(SimpleExpr::Tuple(
1578 n.into_iter().collect::<Vec<SimpleExpr>>(),
1579 ))
1580 }
1581
1582 #[deprecated(since = "0.29.0", note = "Please use the [`Asterisk`]")]
1583 pub fn table_asterisk<T>(t: T) -> Self
1584 where
1585 T: IntoIden,
1586 {
1587 Self::col((t.into_iden(), Asterisk))
1588 }
1589
1590 /// Express a [`Value`], returning a [`Expr`].
1591 ///
1592 /// # Examples
1593 ///
1594 /// ```
1595 /// use sea_query::{tests_cfg::*, *};
1596 ///
1597 /// let query = Query::select()
1598 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
1599 /// .from(Char::Table)
1600 /// .and_where(Expr::val(1).into())
1601 /// .and_where(Expr::val(2.5).into())
1602 /// .and_where(Expr::val("3").into())
1603 /// .to_owned();
1604 ///
1605 /// assert_eq!(
1606 /// query.to_string(MysqlQueryBuilder),
1607 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE 1 AND 2.5 AND '3'"#
1608 /// );
1609 /// assert_eq!(
1610 /// query.to_string(PostgresQueryBuilder),
1611 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 AND 2.5 AND '3'"#
1612 /// );
1613 /// assert_eq!(
1614 /// query.to_string(SqliteQueryBuilder),
1615 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 AND 2.5 AND '3'"#
1616 /// );
1617 /// ```
1618 pub fn val<V>(v: V) -> Self
1619 where
1620 V: Into<Value>,
1621 {
1622 Self::new_with_left(v)
1623 }
1624
1625 /// Wrap an expression to perform some operation on it later.
1626 ///
1627 /// Since `sea_query` 0.32.0 (`sea_orm` 1.1.1), **this is not necessary** in most cases!
1628 ///
1629 /// Some SQL operations used to be defined only as inherent methods on [`Expr`].
1630 /// Thus, to use them, you needed to manually convert from other types to [`Expr`].
1631 /// But now these operations are also defined as [`ExprTrait`] methods
1632 /// that can be called directly on any expression type,
1633 ///
1634 /// # Examples
1635 ///
1636 /// ```
1637 /// use sea_query::{tests_cfg::*, *};
1638 ///
1639 /// let query = Query::select()
1640 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
1641 /// .from(Char::Table)
1642 /// // This is the old style, when `Expr::expr` was necessary:
1643 /// .and_where(Expr::expr(Expr::col(Char::SizeW).if_null(0)).gt(2))
1644 /// .to_owned();
1645 ///
1646 /// // But since 0.32.0, this compiles too:
1647 /// let _ = Expr::col(Char::SizeW).if_null(0).gt(2);
1648 ///
1649 /// assert_eq!(
1650 /// query.to_string(MysqlQueryBuilder),
1651 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE IFNULL(`size_w`, 0) > 2"#
1652 /// );
1653 /// assert_eq!(
1654 /// query.to_string(PostgresQueryBuilder),
1655 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE COALESCE("size_w", 0) > 2"#
1656 /// );
1657 /// assert_eq!(
1658 /// query.to_string(SqliteQueryBuilder),
1659 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE IFNULL("size_w", 0) > 2"#
1660 /// );
1661 /// ```
1662 ///
1663 /// ```
1664 /// use sea_query::{tests_cfg::*, *};
1665 ///
1666 /// let query = Query::select()
1667 /// .column(Char::Character)
1668 /// .from(Char::Table)
1669 /// // This is the old style, when `Expr::expr` was necessary:
1670 /// .and_where(Expr::expr(Func::lower(Expr::col(Char::Character))).is_in(["a", "b"]))
1671 /// .to_owned();
1672 ///
1673 /// // But since 0.32.0, this compiles too:
1674 /// let _ = Func::lower(Expr::col(Char::Character)).is_in(["a", "b"]);
1675 ///
1676 /// assert_eq!(
1677 /// query.to_string(MysqlQueryBuilder),
1678 /// r#"SELECT `character` FROM `character` WHERE LOWER(`character`) IN ('a', 'b')"#
1679 /// );
1680 /// assert_eq!(
1681 /// query.to_string(PostgresQueryBuilder),
1682 /// r#"SELECT "character" FROM "character" WHERE LOWER("character") IN ('a', 'b')"#
1683 /// );
1684 /// assert_eq!(
1685 /// query.to_string(SqliteQueryBuilder),
1686 /// r#"SELECT "character" FROM "character" WHERE LOWER("character") IN ('a', 'b')"#
1687 /// );
1688 /// ```
1689 #[allow(clippy::self_named_constructors)]
1690 pub fn expr<T>(expr: T) -> Self
1691 where
1692 T: Into<SimpleExpr>,
1693 {
1694 Self::new_with_left(expr)
1695 }
1696
1697 /// Express a [`Value`], returning a [`SimpleExpr`].
1698 ///
1699 /// # Examples
1700 ///
1701 /// ```
1702 /// use sea_query::{tests_cfg::*, *};
1703 ///
1704 /// let query = Query::select()
1705 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
1706 /// .from(Char::Table)
1707 /// .and_where(Expr::value(1))
1708 /// .and_where(Expr::value(2.5))
1709 /// .and_where(Expr::value("3"))
1710 /// .to_owned();
1711 ///
1712 /// assert_eq!(
1713 /// query.to_string(MysqlQueryBuilder),
1714 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE 1 AND 2.5 AND '3'"#
1715 /// );
1716 /// assert_eq!(
1717 /// query.to_string(PostgresQueryBuilder),
1718 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 AND 2.5 AND '3'"#
1719 /// );
1720 /// assert_eq!(
1721 /// query.to_string(SqliteQueryBuilder),
1722 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 AND 2.5 AND '3'"#
1723 /// );
1724 /// ```
1725 pub fn value<V>(v: V) -> SimpleExpr
1726 where
1727 V: Into<SimpleExpr>,
1728 {
1729 v.into()
1730 }
1731
1732 /// Express any custom expression in [`&str`].
1733 ///
1734 /// # Examples
1735 ///
1736 /// ```
1737 /// use sea_query::{tests_cfg::*, *};
1738 ///
1739 /// let query = Query::select()
1740 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
1741 /// .from(Char::Table)
1742 /// .and_where(Expr::cust("1 = 1"))
1743 /// .to_owned();
1744 ///
1745 /// assert_eq!(
1746 /// query.to_string(MysqlQueryBuilder),
1747 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE 1 = 1"#
1748 /// );
1749 /// assert_eq!(
1750 /// query.to_string(PostgresQueryBuilder),
1751 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 = 1"#
1752 /// );
1753 /// assert_eq!(
1754 /// query.to_string(SqliteQueryBuilder),
1755 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 = 1"#
1756 /// );
1757 /// ```
1758 pub fn cust<T>(s: T) -> SimpleExpr
1759 where
1760 T: Into<String>,
1761 {
1762 SimpleExpr::Custom(s.into())
1763 }
1764
1765 /// Express any custom expression with [`Value`]. Use this if your expression needs variables.
1766 ///
1767 /// # Examples
1768 ///
1769 /// ```
1770 /// use sea_query::{tests_cfg::*, *};
1771 ///
1772 /// let query = Query::select()
1773 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
1774 /// .from(Char::Table)
1775 /// .and_where(Expr::col(Char::Id).eq(1))
1776 /// .and_where(Expr::cust_with_values("6 = ? * ?", [2, 3]))
1777 /// .to_owned();
1778 ///
1779 /// assert_eq!(
1780 /// query.to_string(MysqlQueryBuilder),
1781 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `id` = 1 AND (6 = 2 * 3)"#
1782 /// );
1783 /// assert_eq!(
1784 /// query.to_string(SqliteQueryBuilder),
1785 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "id" = 1 AND (6 = 2 * 3)"#
1786 /// );
1787 /// ```
1788 /// ```
1789 /// use sea_query::{tests_cfg::*, *};
1790 ///
1791 /// let query = Query::select()
1792 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
1793 /// .from(Char::Table)
1794 /// .and_where(Expr::col(Char::Id).eq(1))
1795 /// .and_where(Expr::cust_with_values("6 = $2 * $1", [3, 2]))
1796 /// .to_owned();
1797 ///
1798 /// assert_eq!(
1799 /// query.to_string(PostgresQueryBuilder),
1800 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "id" = 1 AND (6 = 2 * 3)"#
1801 /// );
1802 /// ```
1803 /// ```
1804 /// use sea_query::{tests_cfg::*, *};
1805 ///
1806 /// let query = Query::select()
1807 /// .expr(Expr::cust_with_values("6 = ? * ?", [2, 3]))
1808 /// .to_owned();
1809 ///
1810 /// assert_eq!(query.to_string(MysqlQueryBuilder), r#"SELECT 6 = 2 * 3"#);
1811 /// assert_eq!(query.to_string(SqliteQueryBuilder), r#"SELECT 6 = 2 * 3"#);
1812 /// ```
1813 /// Postgres only: use `$$` to escape `$`
1814 /// ```
1815 /// use sea_query::{tests_cfg::*, *};
1816 ///
1817 /// let query = Query::select()
1818 /// .expr(Expr::cust_with_values("$1 $$ $2", ["a", "b"]))
1819 /// .to_owned();
1820 ///
1821 /// assert_eq!(query.to_string(PostgresQueryBuilder), r#"SELECT 'a' $ 'b'"#);
1822 /// ```
1823 /// ```
1824 /// use sea_query::{tests_cfg::*, *};
1825 ///
1826 /// let query = Query::select()
1827 /// .expr(Expr::cust_with_values("data @? ($1::JSONPATH)", ["hello"]))
1828 /// .to_owned();
1829 ///
1830 /// assert_eq!(
1831 /// query.to_string(PostgresQueryBuilder),
1832 /// r#"SELECT data @? ('hello'::JSONPATH)"#
1833 /// );
1834 /// ```
1835 pub fn cust_with_values<T, V, I>(s: T, v: I) -> SimpleExpr
1836 where
1837 T: Into<String>,
1838 V: Into<Value>,
1839 I: IntoIterator<Item = V>,
1840 {
1841 SimpleExpr::CustomWithExpr(
1842 s.into(),
1843 v.into_iter()
1844 .map(|v| Into::<Value>::into(v).into())
1845 .collect(),
1846 )
1847 }
1848
1849 /// Express any custom expression with [`SimpleExpr`]. Use this if your expression needs other expression.
1850 ///
1851 /// # Examples
1852 ///
1853 /// ```
1854 /// use sea_query::{tests_cfg::*, *};
1855 ///
1856 /// let query = Query::select()
1857 /// .expr(Expr::val(1).add(2))
1858 /// .expr(Expr::cust_with_expr("data @? ($1::JSONPATH)", "hello"))
1859 /// .to_owned();
1860 /// let (sql, values) = query.build(PostgresQueryBuilder);
1861 ///
1862 /// assert_eq!(sql, r#"SELECT $1 + $2, data @? ($3::JSONPATH)"#);
1863 /// assert_eq!(
1864 /// values,
1865 /// Values(vec![1i32.into(), 2i32.into(), "hello".into()])
1866 /// );
1867 /// ```
1868 /// ```
1869 /// use sea_query::{tests_cfg::*, *};
1870 ///
1871 /// let query = Query::select()
1872 /// .expr(Expr::cust_with_expr(
1873 /// "json_agg(DISTINCT $1)",
1874 /// Expr::col(Char::Character),
1875 /// ))
1876 /// .to_owned();
1877 ///
1878 /// assert_eq!(
1879 /// query.to_string(PostgresQueryBuilder),
1880 /// r#"SELECT json_agg(DISTINCT "character")"#
1881 /// );
1882 /// ```
1883 pub fn cust_with_expr<T, E>(s: T, expr: E) -> SimpleExpr
1884 where
1885 T: Into<String>,
1886 E: Into<SimpleExpr>,
1887 {
1888 SimpleExpr::CustomWithExpr(s.into(), vec![expr.into()])
1889 }
1890
1891 /// Express any custom expression with [`SimpleExpr`]. Use this if your expression needs other expressions.
1892 pub fn cust_with_exprs<T, I>(s: T, v: I) -> SimpleExpr
1893 where
1894 T: Into<String>,
1895 I: IntoIterator<Item = SimpleExpr>,
1896 {
1897 SimpleExpr::CustomWithExpr(s.into(), v.into_iter().collect())
1898 }
1899
1900 /// Express an equal (`=`) expression.
1901 ///
1902 /// This is equivalent to a newer [ExprTrait::eq] and may require more some wrapping beforehand.
1903 ///
1904 /// # Examples
1905 ///
1906 /// ```
1907 /// use sea_query::{*, tests_cfg::*};
1908 ///
1909 /// let query = Query::select()
1910 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
1911 /// .from(Char::Table)
1912 /// .and_where(Expr::val("What!").eq("Nothing"))
1913 /// .and_where(Expr::col(Char::Id).eq(1))
1914 /// .to_owned();
1915 ///
1916 /// assert_eq!(
1917 /// query.to_string(MysqlQueryBuilder),
1918 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE 'What!' = 'Nothing' AND `id` = 1"#
1919 /// );
1920 /// assert_eq!(
1921 /// query.to_string(PostgresQueryBuilder),
1922 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 'What!' = 'Nothing' AND "id" = 1"#
1923 /// );
1924 /// assert_eq!(
1925 /// query.to_string(SqliteQueryBuilder),
1926 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 'What!' = 'Nothing' AND "id" = 1"#
1927 /// );
1928 /// ```
1929 pub fn eq<V>(self, v: V) -> SimpleExpr
1930 where
1931 V: Into<SimpleExpr>,
1932 {
1933 ExprTrait::eq(self, v)
1934 }
1935
1936 /// Express a not equal (`<>`) expression.
1937 ///
1938 /// This is equivalent to a newer [ExprTrait::ne] and may require more some wrapping beforehand.
1939 ///
1940 /// # Examples
1941 ///
1942 /// ```
1943 /// use sea_query::{*, tests_cfg::*};
1944 ///
1945 /// let query = Query::select()
1946 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
1947 /// .from(Char::Table)
1948 /// .and_where(Expr::val("Morning").ne("Good"))
1949 /// .and_where(Expr::col(Char::Id).ne(1))
1950 /// .to_owned();
1951 ///
1952 /// assert_eq!(
1953 /// query.to_string(MysqlQueryBuilder),
1954 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE 'Morning' <> 'Good' AND `id` <> 1"#
1955 /// );
1956 /// assert_eq!(
1957 /// query.to_string(PostgresQueryBuilder),
1958 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 'Morning' <> 'Good' AND "id" <> 1"#
1959 /// );
1960 /// assert_eq!(
1961 /// query.to_string(SqliteQueryBuilder),
1962 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 'Morning' <> 'Good' AND "id" <> 1"#
1963 /// );
1964 /// ```
1965 pub fn ne<V>(self, v: V) -> SimpleExpr
1966 where
1967 V: Into<SimpleExpr>,
1968 {
1969 ExprTrait::ne(self, v)
1970 }
1971
1972 /// Express a equal expression between two table columns,
1973 /// you will mainly use this to relate identical value between two table columns.
1974 ///
1975 /// This is equivalent to a newer [ExprTrait::equals] and may require more some wrapping beforehand.
1976 ///
1977 /// # Examples
1978 ///
1979 /// ```
1980 /// use sea_query::{*, tests_cfg::*};
1981 ///
1982 /// let query = Query::select()
1983 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
1984 /// .from(Char::Table)
1985 /// .and_where(Expr::col((Char::Table, Char::FontId)).equals((Font::Table, Font::Id)))
1986 /// .to_owned();
1987 ///
1988 /// assert_eq!(
1989 /// query.to_string(MysqlQueryBuilder),
1990 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`font_id` = `font`.`id`"#
1991 /// );
1992 /// assert_eq!(
1993 /// query.to_string(PostgresQueryBuilder),
1994 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."font_id" = "font"."id""#
1995 /// );
1996 /// assert_eq!(
1997 /// query.to_string(SqliteQueryBuilder),
1998 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."font_id" = "font"."id""#
1999 /// );
2000 /// ```
2001 pub fn equals<C>(self, col: C) -> SimpleExpr
2002 where
2003 C: IntoColumnRef,
2004 {
2005 ExprTrait::equals(self, col)
2006 }
2007
2008 /// Express a not equal expression between two table columns,
2009 /// you will mainly use this to relate identical value between two table columns.
2010 ///
2011 /// This is equivalent to a newer [ExprTrait::not_equals] and may require more some wrapping beforehand.
2012 ///
2013 /// # Examples
2014 ///
2015 /// ```
2016 /// use sea_query::{*, tests_cfg::*};
2017 ///
2018 /// let query = Query::select()
2019 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
2020 /// .from(Char::Table)
2021 /// .and_where(Expr::col((Char::Table, Char::FontId)).not_equals((Font::Table, Font::Id)))
2022 /// .to_owned();
2023 ///
2024 /// assert_eq!(
2025 /// query.to_string(MysqlQueryBuilder),
2026 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`font_id` <> `font`.`id`"#
2027 /// );
2028 /// assert_eq!(
2029 /// query.to_string(PostgresQueryBuilder),
2030 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."font_id" <> "font"."id""#
2031 /// );
2032 /// assert_eq!(
2033 /// query.to_string(SqliteQueryBuilder),
2034 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."font_id" <> "font"."id""#
2035 /// );
2036 /// ```
2037 pub fn not_equals<C>(self, col: C) -> SimpleExpr
2038 where
2039 C: IntoColumnRef,
2040 {
2041 ExprTrait::not_equals(self, col)
2042 }
2043
2044 /// Express a greater than (`>`) expression.
2045 ///
2046 /// This is equivalent to a newer [ExprTrait::gt] and may require more some wrapping beforehand.
2047 ///
2048 /// # Examples
2049 ///
2050 /// ```
2051 /// use sea_query::{tests_cfg::*, *};
2052 ///
2053 /// let query = Query::select()
2054 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
2055 /// .from(Char::Table)
2056 /// .and_where(Expr::col((Char::Table, Char::SizeW)).gt(2))
2057 /// .to_owned();
2058 ///
2059 /// assert_eq!(
2060 /// query.to_string(MysqlQueryBuilder),
2061 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`size_w` > 2"#
2062 /// );
2063 /// assert_eq!(
2064 /// query.to_string(PostgresQueryBuilder),
2065 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" > 2"#
2066 /// );
2067 /// assert_eq!(
2068 /// query.to_string(SqliteQueryBuilder),
2069 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" > 2"#
2070 /// );
2071 /// ```
2072 pub fn gt<V>(self, v: V) -> SimpleExpr
2073 where
2074 V: Into<SimpleExpr>,
2075 {
2076 ExprTrait::gt(self, v)
2077 }
2078
2079 /// Express a greater than or equal (`>=`) expression.
2080 ///
2081 /// This is equivalent to a newer [ExprTrait::gte] and may require more some wrapping beforehand.
2082 ///
2083 /// # Examples
2084 ///
2085 /// ```
2086 /// use sea_query::{tests_cfg::*, *};
2087 ///
2088 /// let query = Query::select()
2089 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
2090 /// .from(Char::Table)
2091 /// .and_where(Expr::col((Char::Table, Char::SizeW)).gte(2))
2092 /// .to_owned();
2093 ///
2094 /// assert_eq!(
2095 /// query.to_string(MysqlQueryBuilder),
2096 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`size_w` >= 2"#
2097 /// );
2098 /// assert_eq!(
2099 /// query.to_string(PostgresQueryBuilder),
2100 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" >= 2"#
2101 /// );
2102 /// assert_eq!(
2103 /// query.to_string(SqliteQueryBuilder),
2104 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" >= 2"#
2105 /// );
2106 /// ```
2107 pub fn gte<V>(self, v: V) -> SimpleExpr
2108 where
2109 V: Into<SimpleExpr>,
2110 {
2111 ExprTrait::gte(self, v)
2112 }
2113
2114 /// Express a less than (`<`) expression.
2115 ///
2116 /// This is equivalent to a newer [ExprTrait::lt] and may require more some wrapping beforehand.
2117 ///
2118 /// # Examples
2119 ///
2120 /// ```
2121 /// use sea_query::{tests_cfg::*, *};
2122 ///
2123 /// let query = Query::select()
2124 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
2125 /// .from(Char::Table)
2126 /// .and_where(Expr::col((Char::Table, Char::SizeW)).lt(2))
2127 /// .to_owned();
2128 ///
2129 /// assert_eq!(
2130 /// query.to_string(MysqlQueryBuilder),
2131 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`size_w` < 2"#
2132 /// );
2133 /// assert_eq!(
2134 /// query.to_string(PostgresQueryBuilder),
2135 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" < 2"#
2136 /// );
2137 /// assert_eq!(
2138 /// query.to_string(SqliteQueryBuilder),
2139 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" < 2"#
2140 /// );
2141 /// ```
2142 pub fn lt<V>(self, v: V) -> SimpleExpr
2143 where
2144 V: Into<SimpleExpr>,
2145 {
2146 ExprTrait::lt(self, v)
2147 }
2148
2149 /// Express a less than or equal (`<=`) expression.
2150 ///
2151 /// This is equivalent to a newer [ExprTrait::lte] and may require more some wrapping beforehand.
2152 ///
2153 /// # Examples
2154 ///
2155 /// ```
2156 /// use sea_query::{tests_cfg::*, *};
2157 ///
2158 /// let query = Query::select()
2159 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
2160 /// .from(Char::Table)
2161 /// .and_where(Expr::col((Char::Table, Char::SizeW)).lte(2))
2162 /// .to_owned();
2163 ///
2164 /// assert_eq!(
2165 /// query.to_string(MysqlQueryBuilder),
2166 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`size_w` <= 2"#
2167 /// );
2168 /// assert_eq!(
2169 /// query.to_string(PostgresQueryBuilder),
2170 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" <= 2"#
2171 /// );
2172 /// assert_eq!(
2173 /// query.to_string(SqliteQueryBuilder),
2174 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" <= 2"#
2175 /// );
2176 /// ```
2177 pub fn lte<V>(self, v: V) -> SimpleExpr
2178 where
2179 V: Into<SimpleExpr>,
2180 {
2181 ExprTrait::lte(self, v)
2182 }
2183
2184 /// Express an arithmetic addition operation.
2185 ///
2186 /// This is equivalent to a newer [ExprTrait::add] and may require more some wrapping beforehand.
2187 ///
2188 /// # Examples
2189 ///
2190 /// ```
2191 /// use sea_query::{tests_cfg::*, *};
2192 ///
2193 /// let query = Query::select()
2194 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
2195 /// .from(Char::Table)
2196 /// .and_where(Expr::val(1).add(1).eq(2))
2197 /// .to_owned();
2198 ///
2199 /// assert_eq!(
2200 /// query.to_string(MysqlQueryBuilder),
2201 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE 1 + 1 = 2"#
2202 /// );
2203 /// assert_eq!(
2204 /// query.to_string(PostgresQueryBuilder),
2205 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 + 1 = 2"#
2206 /// );
2207 /// assert_eq!(
2208 /// query.to_string(SqliteQueryBuilder),
2209 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 + 1 = 2"#
2210 /// );
2211 /// ```
2212 #[allow(clippy::should_implement_trait)]
2213 pub fn add<V>(self, v: V) -> SimpleExpr
2214 where
2215 V: Into<SimpleExpr>,
2216 {
2217 ExprTrait::add(self, v)
2218 }
2219
2220 /// Express an arithmetic subtraction operation.
2221 ///
2222 /// This is equivalent to a newer [ExprTrait::sub] and may require more some wrapping beforehand.
2223 ///
2224 /// # Examples
2225 ///
2226 /// ```
2227 /// use sea_query::{tests_cfg::*, *};
2228 ///
2229 /// let query = Query::select()
2230 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
2231 /// .from(Char::Table)
2232 /// .and_where(Expr::val(1).sub(1).eq(2))
2233 /// .to_owned();
2234 ///
2235 /// assert_eq!(
2236 /// query.to_string(MysqlQueryBuilder),
2237 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE 1 - 1 = 2"#
2238 /// );
2239 /// assert_eq!(
2240 /// query.to_string(PostgresQueryBuilder),
2241 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 - 1 = 2"#
2242 /// );
2243 /// assert_eq!(
2244 /// query.to_string(SqliteQueryBuilder),
2245 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 - 1 = 2"#
2246 /// );
2247 /// ```
2248 #[allow(clippy::should_implement_trait)]
2249 pub fn sub<V>(self, v: V) -> SimpleExpr
2250 where
2251 V: Into<SimpleExpr>,
2252 {
2253 ExprTrait::sub(self, v)
2254 }
2255
2256 /// Express an arithmetic multiplication operation.
2257 ///
2258 /// This is equivalent to a newer [ExprTrait::mul] and may require more some wrapping beforehand.
2259 ///
2260 /// # Examples
2261 ///
2262 /// ```
2263 /// use sea_query::{tests_cfg::*, *};
2264 ///
2265 /// let query = Query::select()
2266 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
2267 /// .from(Char::Table)
2268 /// .and_where(Expr::val(1).mul(1).eq(2))
2269 /// .to_owned();
2270 ///
2271 /// assert_eq!(
2272 /// query.to_string(MysqlQueryBuilder),
2273 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE 1 * 1 = 2"#
2274 /// );
2275 /// assert_eq!(
2276 /// query.to_string(PostgresQueryBuilder),
2277 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 * 1 = 2"#
2278 /// );
2279 /// assert_eq!(
2280 /// query.to_string(SqliteQueryBuilder),
2281 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 * 1 = 2"#
2282 /// );
2283 /// ```
2284 #[allow(clippy::should_implement_trait)]
2285 pub fn mul<V>(self, v: V) -> SimpleExpr
2286 where
2287 V: Into<SimpleExpr>,
2288 {
2289 ExprTrait::mul(self, v)
2290 }
2291
2292 /// Express an arithmetic division operation.
2293 ///
2294 /// This is equivalent to a newer [ExprTrait::div] and may require more some wrapping beforehand.
2295 ///
2296 /// # Examples
2297 ///
2298 /// ```
2299 /// use sea_query::{tests_cfg::*, *};
2300 ///
2301 /// let query = Query::select()
2302 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
2303 /// .from(Char::Table)
2304 /// .and_where(Expr::val(1).div(1).eq(2))
2305 /// .to_owned();
2306 ///
2307 /// assert_eq!(
2308 /// query.to_string(MysqlQueryBuilder),
2309 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE 1 / 1 = 2"#
2310 /// );
2311 /// assert_eq!(
2312 /// query.to_string(PostgresQueryBuilder),
2313 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 / 1 = 2"#
2314 /// );
2315 /// assert_eq!(
2316 /// query.to_string(SqliteQueryBuilder),
2317 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 / 1 = 2"#
2318 /// );
2319 /// ```
2320 #[allow(clippy::should_implement_trait)]
2321 pub fn div<V>(self, v: V) -> SimpleExpr
2322 where
2323 V: Into<SimpleExpr>,
2324 {
2325 ExprTrait::div(self, v)
2326 }
2327
2328 /// Express an arithmetic modulo operation.
2329 ///
2330 /// This is equivalent to a newer [ExprTrait::modulo] and may require more some wrapping beforehand.
2331 ///
2332 /// # Examples
2333 ///
2334 /// ```
2335 /// use sea_query::{tests_cfg::*, *};
2336 ///
2337 /// let query = Query::select()
2338 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
2339 /// .from(Char::Table)
2340 /// .and_where(Expr::val(1).modulo(1).eq(2))
2341 /// .to_owned();
2342 ///
2343 /// assert_eq!(
2344 /// query.to_string(MysqlQueryBuilder),
2345 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE 1 % 1 = 2"#
2346 /// );
2347 /// assert_eq!(
2348 /// query.to_string(PostgresQueryBuilder),
2349 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 % 1 = 2"#
2350 /// );
2351 /// assert_eq!(
2352 /// query.to_string(SqliteQueryBuilder),
2353 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 % 1 = 2"#
2354 /// );
2355 /// ```
2356 #[allow(clippy::should_implement_trait)]
2357 pub fn modulo<V>(self, v: V) -> SimpleExpr
2358 where
2359 V: Into<SimpleExpr>,
2360 {
2361 ExprTrait::modulo(self, v)
2362 }
2363
2364 /// Express a bitwise left shift.
2365 ///
2366 /// This is equivalent to a newer [ExprTrait::left_shift] and may require more some wrapping beforehand.
2367 ///
2368 /// # Examples
2369 ///
2370 /// ```
2371 /// use sea_query::{tests_cfg::*, *};
2372 ///
2373 /// let query = Query::select()
2374 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
2375 /// .from(Char::Table)
2376 /// .and_where(Expr::val(1).left_shift(1).eq(2))
2377 /// .to_owned();
2378 ///
2379 /// assert_eq!(
2380 /// query.to_string(MysqlQueryBuilder),
2381 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE 1 << 1 = 2"#
2382 /// );
2383 /// assert_eq!(
2384 /// query.to_string(PostgresQueryBuilder),
2385 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 << 1 = 2"#
2386 /// );
2387 /// assert_eq!(
2388 /// query.to_string(SqliteQueryBuilder),
2389 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 << 1 = 2"#
2390 /// );
2391 /// ```
2392 #[allow(clippy::should_implement_trait)]
2393 pub fn left_shift<V>(self, v: V) -> SimpleExpr
2394 where
2395 V: Into<SimpleExpr>,
2396 {
2397 ExprTrait::left_shift(self, v)
2398 }
2399
2400 /// Express a bitwise right shift.
2401 ///
2402 /// This is equivalent to a newer [ExprTrait::right_shift] and may require more some wrapping beforehand.
2403 ///
2404 /// # Examples
2405 ///
2406 /// ```
2407 /// use sea_query::{tests_cfg::*, *};
2408 ///
2409 /// let query = Query::select()
2410 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
2411 /// .from(Char::Table)
2412 /// .and_where(Expr::val(1).right_shift(1).eq(2))
2413 /// .to_owned();
2414 ///
2415 /// assert_eq!(
2416 /// query.to_string(MysqlQueryBuilder),
2417 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE 1 >> 1 = 2"#
2418 /// );
2419 /// assert_eq!(
2420 /// query.to_string(PostgresQueryBuilder),
2421 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 >> 1 = 2"#
2422 /// );
2423 /// assert_eq!(
2424 /// query.to_string(SqliteQueryBuilder),
2425 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 >> 1 = 2"#
2426 /// );
2427 /// ```
2428 #[allow(clippy::should_implement_trait)]
2429 pub fn right_shift<V>(self, v: V) -> SimpleExpr
2430 where
2431 V: Into<SimpleExpr>,
2432 {
2433 ExprTrait::right_shift(self, v)
2434 }
2435
2436 /// Express a `BETWEEN` expression.
2437 ///
2438 /// This is equivalent to a newer [ExprTrait::between] and may require more some wrapping beforehand.
2439 ///
2440 /// # Examples
2441 ///
2442 /// ```
2443 /// use sea_query::{*, tests_cfg::*};
2444 ///
2445 /// let query = Query::select()
2446 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
2447 /// .from(Char::Table)
2448 /// .and_where(Expr::col((Char::Table, Char::SizeW)).between(1, 10))
2449 /// .to_owned();
2450 ///
2451 /// assert_eq!(
2452 /// query.to_string(MysqlQueryBuilder),
2453 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`size_w` BETWEEN 1 AND 10"#
2454 /// );
2455 /// assert_eq!(
2456 /// query.to_string(PostgresQueryBuilder),
2457 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" BETWEEN 1 AND 10"#
2458 /// );
2459 /// assert_eq!(
2460 /// query.to_string(SqliteQueryBuilder),
2461 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" BETWEEN 1 AND 10"#
2462 /// );
2463 /// ```
2464 pub fn between<V>(self, a: V, b: V) -> SimpleExpr
2465 where
2466 V: Into<SimpleExpr>,
2467 {
2468 ExprTrait::between(self, a, b)
2469 }
2470
2471 /// Express a `NOT BETWEEN` expression.
2472 ///
2473 /// This is equivalent to a newer [ExprTrait::not_between] and may require more some wrapping beforehand.
2474 ///
2475 /// # Examples
2476 ///
2477 /// ```
2478 /// use sea_query::{*, tests_cfg::*};
2479 ///
2480 /// let query = Query::select()
2481 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
2482 /// .from(Char::Table)
2483 /// .and_where(Expr::col((Char::Table, Char::SizeW)).not_between(1, 10))
2484 /// .to_owned();
2485 ///
2486 /// assert_eq!(
2487 /// query.to_string(MysqlQueryBuilder),
2488 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`size_w` NOT BETWEEN 1 AND 10"#
2489 /// );
2490 /// assert_eq!(
2491 /// query.to_string(PostgresQueryBuilder),
2492 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" NOT BETWEEN 1 AND 10"#
2493 /// );
2494 /// assert_eq!(
2495 /// query.to_string(SqliteQueryBuilder),
2496 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" NOT BETWEEN 1 AND 10"#
2497 /// );
2498 /// ```
2499 pub fn not_between<V>(self, a: V, b: V) -> SimpleExpr
2500 where
2501 V: Into<SimpleExpr>,
2502 {
2503 ExprTrait::not_between(self, a, b)
2504 }
2505
2506 /// Express a `LIKE` expression.
2507 ///
2508 /// This is equivalent to a newer [ExprTrait::like] and may require more some wrapping beforehand.
2509 ///
2510 /// # Examples
2511 ///
2512 /// ```
2513 /// use sea_query::{*, tests_cfg::*};
2514 ///
2515 /// let query = Query::select()
2516 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
2517 /// .from(Char::Table)
2518 /// .and_where(Expr::col((Char::Table, Char::Character)).like("Ours'%"))
2519 /// .to_owned();
2520 ///
2521 /// assert_eq!(
2522 /// query.to_string(MysqlQueryBuilder),
2523 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`character` LIKE 'Ours\'%'"#
2524 /// );
2525 /// assert_eq!(
2526 /// query.to_string(PostgresQueryBuilder),
2527 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."character" LIKE E'Ours\'%'"#
2528 /// );
2529 /// assert_eq!(
2530 /// query.to_string(SqliteQueryBuilder),
2531 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."character" LIKE 'Ours''%'"#
2532 /// );
2533 /// ```
2534 ///
2535 /// Like with ESCAPE
2536 ///
2537 /// ```
2538 /// use sea_query::{*, tests_cfg::*};
2539 ///
2540 /// let query = Query::select()
2541 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
2542 /// .from(Char::Table)
2543 /// .and_where(Expr::col((Char::Table, Char::Character)).like(LikeExpr::new(r"|_Our|_").escape('|')))
2544 /// .to_owned();
2545 ///
2546 /// assert_eq!(
2547 /// query.to_string(MysqlQueryBuilder),
2548 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`character` LIKE '|_Our|_' ESCAPE '|'"#
2549 /// );
2550 /// assert_eq!(
2551 /// query.to_string(PostgresQueryBuilder),
2552 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."character" LIKE '|_Our|_' ESCAPE '|'"#
2553 /// );
2554 /// assert_eq!(
2555 /// query.to_string(SqliteQueryBuilder),
2556 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."character" LIKE '|_Our|_' ESCAPE '|'"#
2557 /// );
2558 /// ```
2559 pub fn like<L: IntoLikeExpr>(self, like: L) -> SimpleExpr {
2560 ExprTrait::like(self, like)
2561 }
2562
2563 /// Express a `NOT LIKE` expression.
2564 ///
2565 /// This is equivalent to a newer [ExprTrait::not_like] and may require more some wrapping beforehand.
2566 pub fn not_like<L: IntoLikeExpr>(self, like: L) -> SimpleExpr {
2567 ExprTrait::not_like(self, like)
2568 }
2569
2570 /// Express a `IS NULL` expression.
2571 ///
2572 /// This is equivalent to a newer [ExprTrait::is_null] and may require more some wrapping beforehand.
2573 ///
2574 /// # Examples
2575 ///
2576 /// ```
2577 /// use sea_query::{*, tests_cfg::*};
2578 ///
2579 /// let query = Query::select()
2580 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
2581 /// .from(Char::Table)
2582 /// .and_where(Expr::col((Char::Table, Char::SizeW)).is_null())
2583 /// .to_owned();
2584 ///
2585 /// assert_eq!(
2586 /// query.to_string(MysqlQueryBuilder),
2587 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`size_w` IS NULL"#
2588 /// );
2589 /// assert_eq!(
2590 /// query.to_string(PostgresQueryBuilder),
2591 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" IS NULL"#
2592 /// );
2593 /// assert_eq!(
2594 /// query.to_string(SqliteQueryBuilder),
2595 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" IS NULL"#
2596 /// );
2597 /// ```
2598 #[allow(clippy::wrong_self_convention)]
2599 pub fn is_null(self) -> SimpleExpr {
2600 ExprTrait::is_null(self)
2601 }
2602
2603 /// Express a `IS` expression.
2604 ///
2605 /// This is equivalent to a newer [ExprTrait::is] and may require more some wrapping beforehand.
2606 ///
2607 /// # Examples
2608 ///
2609 /// ```
2610 /// use sea_query::{*, tests_cfg::*};
2611 ///
2612 /// let query = Query::select()
2613 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
2614 /// .from(Char::Table)
2615 /// .and_where(Expr::col((Char::Table, Char::Ascii)).is(true))
2616 /// .to_owned();
2617 ///
2618 /// assert_eq!(
2619 /// query.to_string(MysqlQueryBuilder),
2620 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`ascii` IS TRUE"#
2621 /// );
2622 /// assert_eq!(
2623 /// query.to_string(PostgresQueryBuilder),
2624 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."ascii" IS TRUE"#
2625 /// );
2626 /// assert_eq!(
2627 /// query.to_string(SqliteQueryBuilder),
2628 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."ascii" IS TRUE"#
2629 /// );
2630 /// ```
2631 pub fn is<V>(self, v: V) -> SimpleExpr
2632 where
2633 V: Into<SimpleExpr>,
2634 {
2635 ExprTrait::is(self, v)
2636 }
2637
2638 /// Express a `IS NOT NULL` expression.
2639 ///
2640 /// This is equivalent to a newer [ExprTrait::is_not_null] and may require more some wrapping beforehand.
2641 ///
2642 /// # Examples
2643 ///
2644 /// ```
2645 /// use sea_query::{*, tests_cfg::*};
2646 ///
2647 /// let query = Query::select()
2648 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
2649 /// .from(Char::Table)
2650 /// .and_where(Expr::col((Char::Table, Char::SizeW)).is_not_null())
2651 /// .to_owned();
2652 ///
2653 /// assert_eq!(
2654 /// query.to_string(MysqlQueryBuilder),
2655 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`size_w` IS NOT NULL"#
2656 /// );
2657 /// assert_eq!(
2658 /// query.to_string(PostgresQueryBuilder),
2659 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" IS NOT NULL"#
2660 /// );
2661 /// assert_eq!(
2662 /// query.to_string(SqliteQueryBuilder),
2663 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" IS NOT NULL"#
2664 /// );
2665 /// ```
2666 #[allow(clippy::wrong_self_convention)]
2667 pub fn is_not_null(self) -> SimpleExpr {
2668 ExprTrait::is_not_null(self)
2669 }
2670
2671 /// Express a `IS NOT` expression.
2672 ///
2673 /// This is equivalent to a newer [ExprTrait::is_not] and may require more some wrapping beforehand.
2674 ///
2675 /// # Examples
2676 ///
2677 /// ```
2678 /// use sea_query::{*, tests_cfg::*};
2679 ///
2680 /// let query = Query::select()
2681 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
2682 /// .from(Char::Table)
2683 /// .and_where(Expr::col((Char::Table, Char::Ascii)).is_not(true))
2684 /// .to_owned();
2685 ///
2686 /// assert_eq!(
2687 /// query.to_string(MysqlQueryBuilder),
2688 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`ascii` IS NOT TRUE"#
2689 /// );
2690 /// assert_eq!(
2691 /// query.to_string(PostgresQueryBuilder),
2692 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."ascii" IS NOT TRUE"#
2693 /// );
2694 /// assert_eq!(
2695 /// query.to_string(SqliteQueryBuilder),
2696 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."ascii" IS NOT TRUE"#
2697 /// );
2698 /// ```
2699 pub fn is_not<V>(self, v: V) -> SimpleExpr
2700 where
2701 V: Into<SimpleExpr>,
2702 {
2703 ExprTrait::is_not(self, v)
2704 }
2705
2706 /// Create any binary operation
2707 ///
2708 /// This is equivalent to a newer [ExprTrait::binary] and may require more some wrapping beforehand.
2709 ///
2710 /// # Examples
2711 /// ```
2712 /// use sea_query::{*, tests_cfg::*};
2713 ///
2714 /// let query = Query::select()
2715 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
2716 /// .from(Char::Table)
2717 /// .cond_where(all![
2718 /// Expr::col(Char::SizeW).binary(BinOper::SmallerThan, 10),
2719 /// Expr::col(Char::SizeW).binary(BinOper::GreaterThan, Expr::col(Char::SizeH))
2720 /// ])
2721 /// .to_owned();
2722 /// assert_eq!(
2723 /// query.to_string(MysqlQueryBuilder),
2724 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `size_w` < 10 AND `size_w` > `size_h`"#
2725 /// );
2726 /// assert_eq!(
2727 /// query.to_string(PostgresQueryBuilder),
2728 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "size_w" < 10 AND "size_w" > "size_h""#
2729 /// );
2730 /// assert_eq!(
2731 /// query.to_string(SqliteQueryBuilder),
2732 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "size_w" < 10 AND "size_w" > "size_h""#
2733 /// );
2734 /// ```
2735 pub fn binary<O, T>(self, op: O, right: T) -> SimpleExpr
2736 where
2737 O: Into<BinOper>,
2738 T: Into<SimpleExpr>,
2739 {
2740 ExprTrait::binary(self, op, right)
2741 }
2742
2743 /// Negates an expression with `NOT`.
2744 ///
2745 /// This is equivalent to a newer [ExprTrait::not] and may require more some wrapping beforehand.
2746 ///
2747 /// # Examples
2748 ///
2749 /// ```
2750 /// use sea_query::{*, tests_cfg::*};
2751 ///
2752 /// let query = Query::select()
2753 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
2754 /// .from(Char::Table)
2755 /// // Before 0.32.0, you had call `not` on an `Expr`, which had to be constructed using `Expr::expr`:
2756 /// .and_where(Expr::expr(Expr::col((Char::Table, Char::SizeW)).is_null()).not())
2757 /// .to_owned();
2758 ///
2759 /// // But since 0.32.0, this compiles too:
2760 /// let _ = Expr::col((Char::Table, Char::SizeW)).is_null().not();
2761 ///
2762 /// assert_eq!(
2763 /// query.to_string(MysqlQueryBuilder),
2764 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE NOT `character`.`size_w` IS NULL"#
2765 /// );
2766 /// assert_eq!(
2767 /// query.to_string(PostgresQueryBuilder),
2768 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE NOT "character"."size_w" IS NULL"#
2769 /// );
2770 /// assert_eq!(
2771 /// query.to_string(SqliteQueryBuilder),
2772 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE NOT "character"."size_w" IS NULL"#
2773 /// );
2774 /// ```
2775 #[allow(clippy::should_implement_trait)]
2776 pub fn not(self) -> SimpleExpr {
2777 ExprTrait::not(self)
2778 }
2779
2780 /// Express a `MAX` function.
2781 ///
2782 /// # Examples
2783 ///
2784 /// ```
2785 /// use sea_query::{tests_cfg::*, *};
2786 ///
2787 /// let query = Query::select()
2788 /// .expr(Expr::col((Char::Table, Char::SizeW)).max())
2789 /// .from(Char::Table)
2790 /// .to_owned();
2791 ///
2792 /// assert_eq!(
2793 /// query.to_string(MysqlQueryBuilder),
2794 /// r#"SELECT MAX(`character`.`size_w`) FROM `character`"#
2795 /// );
2796 /// assert_eq!(
2797 /// query.to_string(PostgresQueryBuilder),
2798 /// r#"SELECT MAX("character"."size_w") FROM "character""#
2799 /// );
2800 /// assert_eq!(
2801 /// query.to_string(SqliteQueryBuilder),
2802 /// r#"SELECT MAX("character"."size_w") FROM "character""#
2803 /// );
2804 /// ```
2805 pub fn max(self) -> SimpleExpr {
2806 Func::max(self.left).into()
2807 }
2808
2809 /// Express a `MIN` function.
2810 ///
2811 /// # Examples
2812 ///
2813 /// ```
2814 /// use sea_query::{tests_cfg::*, *};
2815 ///
2816 /// let query = Query::select()
2817 /// .expr(Expr::col((Char::Table, Char::SizeW)).min())
2818 /// .from(Char::Table)
2819 /// .to_owned();
2820 ///
2821 /// assert_eq!(
2822 /// query.to_string(MysqlQueryBuilder),
2823 /// r#"SELECT MIN(`character`.`size_w`) FROM `character`"#
2824 /// );
2825 /// assert_eq!(
2826 /// query.to_string(PostgresQueryBuilder),
2827 /// r#"SELECT MIN("character"."size_w") FROM "character""#
2828 /// );
2829 /// assert_eq!(
2830 /// query.to_string(SqliteQueryBuilder),
2831 /// r#"SELECT MIN("character"."size_w") FROM "character""#
2832 /// );
2833 /// ```
2834 pub fn min(self) -> SimpleExpr {
2835 Func::min(self.left).into()
2836 }
2837
2838 /// Express a `SUM` function.
2839 ///
2840 /// # Examples
2841 ///
2842 /// ```
2843 /// use sea_query::{tests_cfg::*, *};
2844 ///
2845 /// let query = Query::select()
2846 /// .expr(Expr::col((Char::Table, Char::SizeW)).sum())
2847 /// .from(Char::Table)
2848 /// .to_owned();
2849 ///
2850 /// assert_eq!(
2851 /// query.to_string(MysqlQueryBuilder),
2852 /// r#"SELECT SUM(`character`.`size_w`) FROM `character`"#
2853 /// );
2854 /// assert_eq!(
2855 /// query.to_string(PostgresQueryBuilder),
2856 /// r#"SELECT SUM("character"."size_w") FROM "character""#
2857 /// );
2858 /// assert_eq!(
2859 /// query.to_string(SqliteQueryBuilder),
2860 /// r#"SELECT SUM("character"."size_w") FROM "character""#
2861 /// );
2862 /// ```
2863 pub fn sum(self) -> SimpleExpr {
2864 Func::sum(self.left).into()
2865 }
2866
2867 /// Express a `COUNT` function.
2868 ///
2869 /// # Examples
2870 ///
2871 /// ```
2872 /// use sea_query::{tests_cfg::*, *};
2873 ///
2874 /// let query = Query::select()
2875 /// .expr(Expr::col((Char::Table, Char::SizeW)).count())
2876 /// .from(Char::Table)
2877 /// .to_owned();
2878 ///
2879 /// assert_eq!(
2880 /// query.to_string(MysqlQueryBuilder),
2881 /// r#"SELECT COUNT(`character`.`size_w`) FROM `character`"#
2882 /// );
2883 /// assert_eq!(
2884 /// query.to_string(PostgresQueryBuilder),
2885 /// r#"SELECT COUNT("character"."size_w") FROM "character""#
2886 /// );
2887 /// assert_eq!(
2888 /// query.to_string(SqliteQueryBuilder),
2889 /// r#"SELECT COUNT("character"."size_w") FROM "character""#
2890 /// );
2891 /// ```
2892 pub fn count(self) -> SimpleExpr {
2893 Func::count(self.left).into()
2894 }
2895
2896 /// Express a `COUNT` function with the DISTINCT modifier.
2897 ///
2898 /// # Examples
2899 ///
2900 /// ```
2901 /// use sea_query::{tests_cfg::*, *};
2902 ///
2903 /// let query = Query::select()
2904 /// .expr(Expr::col((Char::Table, Char::SizeW)).count_distinct())
2905 /// .from(Char::Table)
2906 /// .to_owned();
2907 ///
2908 /// assert_eq!(
2909 /// query.to_string(MysqlQueryBuilder),
2910 /// r#"SELECT COUNT(DISTINCT `character`.`size_w`) FROM `character`"#
2911 /// );
2912 /// assert_eq!(
2913 /// query.to_string(PostgresQueryBuilder),
2914 /// r#"SELECT COUNT(DISTINCT "character"."size_w") FROM "character""#
2915 /// );
2916 /// assert_eq!(
2917 /// query.to_string(SqliteQueryBuilder),
2918 /// r#"SELECT COUNT(DISTINCT "character"."size_w") FROM "character""#
2919 /// );
2920 /// ```
2921 pub fn count_distinct(self) -> SimpleExpr {
2922 Func::count_distinct(self.left).into()
2923 }
2924
2925 /// Express a `IF NULL` function.
2926 ///
2927 /// # Examples
2928 ///
2929 /// ```
2930 /// use sea_query::{tests_cfg::*, *};
2931 ///
2932 /// let query = Query::select()
2933 /// .expr(Expr::col((Char::Table, Char::SizeW)).if_null(0))
2934 /// .from(Char::Table)
2935 /// .to_owned();
2936 ///
2937 /// assert_eq!(
2938 /// query.to_string(MysqlQueryBuilder),
2939 /// r#"SELECT IFNULL(`character`.`size_w`, 0) FROM `character`"#
2940 /// );
2941 /// assert_eq!(
2942 /// query.to_string(PostgresQueryBuilder),
2943 /// r#"SELECT COALESCE("character"."size_w", 0) FROM "character""#
2944 /// );
2945 /// assert_eq!(
2946 /// query.to_string(SqliteQueryBuilder),
2947 /// r#"SELECT IFNULL("character"."size_w", 0) FROM "character""#
2948 /// );
2949 /// ```
2950 pub fn if_null<V>(self, v: V) -> SimpleExpr
2951 where
2952 V: Into<SimpleExpr>,
2953 {
2954 Func::if_null(self.left, v).into()
2955 }
2956
2957 /// Express a `IN` expression.
2958 ///
2959 /// This is equivalent to a newer [ExprTrait::is_in] and may require more some wrapping beforehand.
2960 ///
2961 /// # Examples
2962 ///
2963 /// ```
2964 /// use sea_query::{tests_cfg::*, *};
2965 ///
2966 /// let query = Query::select()
2967 /// .columns([Char::Id])
2968 /// .from(Char::Table)
2969 /// .and_where(Expr::col((Char::Table, Char::SizeW)).is_in([1, 2, 3]))
2970 /// .to_owned();
2971 ///
2972 /// assert_eq!(
2973 /// query.to_string(MysqlQueryBuilder),
2974 /// r#"SELECT `id` FROM `character` WHERE `character`.`size_w` IN (1, 2, 3)"#
2975 /// );
2976 /// assert_eq!(
2977 /// query.to_string(PostgresQueryBuilder),
2978 /// r#"SELECT "id" FROM "character" WHERE "character"."size_w" IN (1, 2, 3)"#
2979 /// );
2980 /// assert_eq!(
2981 /// query.to_string(SqliteQueryBuilder),
2982 /// r#"SELECT "id" FROM "character" WHERE "character"."size_w" IN (1, 2, 3)"#
2983 /// );
2984 /// ```
2985 /// Empty value list
2986 /// ```
2987 /// use sea_query::{tests_cfg::*, *};
2988 ///
2989 /// let query = Query::select()
2990 /// .columns([Char::Id])
2991 /// .from(Char::Table)
2992 /// .and_where(Expr::col((Char::Table, Char::SizeW)).is_in(Vec::<u8>::new()))
2993 /// .to_owned();
2994 ///
2995 /// assert_eq!(
2996 /// query.to_string(MysqlQueryBuilder),
2997 /// r#"SELECT `id` FROM `character` WHERE 1 = 2"#
2998 /// );
2999 /// assert_eq!(
3000 /// query.to_string(PostgresQueryBuilder),
3001 /// r#"SELECT "id" FROM "character" WHERE 1 = 2"#
3002 /// );
3003 /// assert_eq!(
3004 /// query.to_string(SqliteQueryBuilder),
3005 /// r#"SELECT "id" FROM "character" WHERE 1 = 2"#
3006 /// );
3007 /// ```
3008 #[allow(clippy::wrong_self_convention)]
3009 pub fn is_in<V, I>(self, v: I) -> SimpleExpr
3010 where
3011 V: Into<SimpleExpr>,
3012 I: IntoIterator<Item = V>,
3013 {
3014 ExprTrait::is_in(self, v)
3015 }
3016
3017 /// Express a `IN` sub expression.
3018 ///
3019 /// This is equivalent to a newer [ExprTrait::in_tuples] and may require more some wrapping beforehand.
3020 ///
3021 /// # Examples
3022 ///
3023 /// ```
3024 /// use sea_query::{*, tests_cfg::*};
3025 ///
3026 /// let query = Query::select()
3027 /// .columns([Char::Character, Char::FontId])
3028 /// .from(Char::Table)
3029 /// .and_where(
3030 /// Expr::tuple([
3031 /// Expr::col(Char::Character).into(),
3032 /// Expr::col(Char::FontId).into(),
3033 /// ])
3034 /// .in_tuples([(1, String::from("1")), (2, String::from("2"))])
3035 /// )
3036 /// .to_owned();
3037 ///
3038 /// assert_eq!(
3039 /// query.to_string(MysqlQueryBuilder),
3040 /// r#"SELECT `character`, `font_id` FROM `character` WHERE (`character`, `font_id`) IN ((1, '1'), (2, '2'))"#
3041 /// );
3042 ///
3043 /// assert_eq!(
3044 /// query.to_string(PostgresQueryBuilder),
3045 /// r#"SELECT "character", "font_id" FROM "character" WHERE ("character", "font_id") IN ((1, '1'), (2, '2'))"#
3046 /// );
3047 ///
3048 /// assert_eq!(
3049 /// query.to_string(SqliteQueryBuilder),
3050 /// r#"SELECT "character", "font_id" FROM "character" WHERE ("character", "font_id") IN ((1, '1'), (2, '2'))"#
3051 /// );
3052 /// ```
3053 #[allow(clippy::wrong_self_convention)]
3054 pub fn in_tuples<V, I>(self, v: I) -> SimpleExpr
3055 where
3056 V: IntoValueTuple,
3057 I: IntoIterator<Item = V>,
3058 {
3059 ExprTrait::in_tuples(self, v)
3060 }
3061
3062 /// Express a `NOT IN` expression.
3063 ///
3064 /// This is equivalent to a newer [ExprTrait::is_not_in] and may require more some wrapping beforehand.
3065 ///
3066 /// # Examples
3067 ///
3068 /// ```
3069 /// use sea_query::{tests_cfg::*, *};
3070 ///
3071 /// let query = Query::select()
3072 /// .columns([Char::Id])
3073 /// .from(Char::Table)
3074 /// .and_where(Expr::col((Char::Table, Char::SizeW)).is_not_in([1, 2, 3]))
3075 /// .to_owned();
3076 ///
3077 /// assert_eq!(
3078 /// query.to_string(MysqlQueryBuilder),
3079 /// r#"SELECT `id` FROM `character` WHERE `character`.`size_w` NOT IN (1, 2, 3)"#
3080 /// );
3081 /// assert_eq!(
3082 /// query.to_string(PostgresQueryBuilder),
3083 /// r#"SELECT "id" FROM "character" WHERE "character"."size_w" NOT IN (1, 2, 3)"#
3084 /// );
3085 /// assert_eq!(
3086 /// query.to_string(SqliteQueryBuilder),
3087 /// r#"SELECT "id" FROM "character" WHERE "character"."size_w" NOT IN (1, 2, 3)"#
3088 /// );
3089 /// ```
3090 /// Empty value list
3091 /// ```
3092 /// use sea_query::{tests_cfg::*, *};
3093 ///
3094 /// let query = Query::select()
3095 /// .columns([Char::Id])
3096 /// .from(Char::Table)
3097 /// .and_where(Expr::col((Char::Table, Char::SizeW)).is_not_in(Vec::<u8>::new()))
3098 /// .to_owned();
3099 ///
3100 /// assert_eq!(
3101 /// query.to_string(MysqlQueryBuilder),
3102 /// r#"SELECT `id` FROM `character` WHERE 1 = 1"#
3103 /// );
3104 /// assert_eq!(
3105 /// query.to_string(PostgresQueryBuilder),
3106 /// r#"SELECT "id" FROM "character" WHERE 1 = 1"#
3107 /// );
3108 /// assert_eq!(
3109 /// query.to_string(SqliteQueryBuilder),
3110 /// r#"SELECT "id" FROM "character" WHERE 1 = 1"#
3111 /// );
3112 /// ```
3113 #[allow(clippy::wrong_self_convention)]
3114 pub fn is_not_in<V, I>(self, v: I) -> SimpleExpr
3115 where
3116 V: Into<SimpleExpr>,
3117 I: IntoIterator<Item = V>,
3118 {
3119 ExprTrait::is_not_in(self, v)
3120 }
3121
3122 /// Express a `IN` sub-query expression.
3123 ///
3124 /// This is equivalent to a newer [ExprTrait::in_subquery] and may require more some wrapping beforehand.
3125 ///
3126 /// # Examples
3127 ///
3128 /// ```
3129 /// use sea_query::{*, tests_cfg::*};
3130 ///
3131 /// let query = Query::select()
3132 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
3133 /// .from(Char::Table)
3134 /// .and_where(Expr::col(Char::SizeW).in_subquery(
3135 /// Query::select()
3136 /// .expr(Expr::cust("3 + 2 * 2"))
3137 /// .take()
3138 /// ))
3139 /// .to_owned();
3140 ///
3141 /// assert_eq!(
3142 /// query.to_string(MysqlQueryBuilder),
3143 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `size_w` IN (SELECT 3 + 2 * 2)"#
3144 /// );
3145 /// assert_eq!(
3146 /// query.to_string(PostgresQueryBuilder),
3147 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "size_w" IN (SELECT 3 + 2 * 2)"#
3148 /// );
3149 /// assert_eq!(
3150 /// query.to_string(SqliteQueryBuilder),
3151 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "size_w" IN (SELECT 3 + 2 * 2)"#
3152 /// );
3153 /// ```
3154 #[allow(clippy::wrong_self_convention)]
3155 pub fn in_subquery(self, sel: SelectStatement) -> SimpleExpr {
3156 ExprTrait::in_subquery(self, sel)
3157 }
3158
3159 /// Express a `NOT IN` sub-query expression.
3160 ///
3161 /// This is equivalent to a newer [ExprTrait::not_in_subquery] and may require more some wrapping beforehand.
3162 ///
3163 /// # Examples
3164 ///
3165 /// ```
3166 /// use sea_query::{*, tests_cfg::*};
3167 ///
3168 /// let query = Query::select()
3169 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
3170 /// .from(Char::Table)
3171 /// .and_where(Expr::col(Char::SizeW).not_in_subquery(
3172 /// Query::select()
3173 /// .expr(Expr::cust("3 + 2 * 2"))
3174 /// .take()
3175 /// ))
3176 /// .to_owned();
3177 ///
3178 /// assert_eq!(
3179 /// query.to_string(MysqlQueryBuilder),
3180 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `size_w` NOT IN (SELECT 3 + 2 * 2)"#
3181 /// );
3182 /// assert_eq!(
3183 /// query.to_string(PostgresQueryBuilder),
3184 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "size_w" NOT IN (SELECT 3 + 2 * 2)"#
3185 /// );
3186 /// assert_eq!(
3187 /// query.to_string(SqliteQueryBuilder),
3188 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "size_w" NOT IN (SELECT 3 + 2 * 2)"#
3189 /// );
3190 /// ```
3191 #[allow(clippy::wrong_self_convention)]
3192 pub fn not_in_subquery(self, sel: SelectStatement) -> SimpleExpr {
3193 ExprTrait::not_in_subquery(self, sel)
3194 }
3195
3196 /// Express a `EXISTS` sub-query expression.
3197 ///
3198 /// # Examples
3199 ///
3200 /// ```
3201 /// use sea_query::{*, tests_cfg::*};
3202 ///
3203 /// let query = Query::select()
3204 /// .expr_as(Expr::exists(Query::select().column(Char::Id).from(Char::Table).take()), Alias::new("character_exists"))
3205 /// .expr_as(Expr::exists(Query::select().column(Glyph::Id).from(Glyph::Table).take()), Alias::new("glyph_exists"))
3206 /// .to_owned();
3207 ///
3208 /// assert_eq!(
3209 /// query.to_string(MysqlQueryBuilder),
3210 /// r#"SELECT EXISTS(SELECT `id` FROM `character`) AS `character_exists`, EXISTS(SELECT `id` FROM `glyph`) AS `glyph_exists`"#
3211 /// );
3212 /// assert_eq!(
3213 /// query.to_string(PostgresQueryBuilder),
3214 /// r#"SELECT EXISTS(SELECT "id" FROM "character") AS "character_exists", EXISTS(SELECT "id" FROM "glyph") AS "glyph_exists""#
3215 /// );
3216 /// assert_eq!(
3217 /// query.to_string(SqliteQueryBuilder),
3218 /// r#"SELECT EXISTS(SELECT "id" FROM "character") AS "character_exists", EXISTS(SELECT "id" FROM "glyph") AS "glyph_exists""#
3219 /// );
3220 /// ```
3221 pub fn exists(sel: SelectStatement) -> SimpleExpr {
3222 SimpleExpr::SubQuery(
3223 Some(SubQueryOper::Exists),
3224 Box::new(sel.into_sub_query_statement()),
3225 )
3226 }
3227
3228 /// Express a `ANY` sub-query expression.
3229 ///
3230 /// # Examples
3231 ///
3232 /// ```
3233 /// use sea_query::{tests_cfg::*, *};
3234 ///
3235 /// let query = Query::select()
3236 /// .column(Char::Id)
3237 /// .from(Char::Table)
3238 /// .and_where(Expr::col(Char::Id).eq(Expr::any(
3239 /// Query::select().column(Char::Id).from(Char::Table).take(),
3240 /// )))
3241 /// .to_owned();
3242 ///
3243 /// assert_eq!(
3244 /// query.to_string(MysqlQueryBuilder),
3245 /// r#"SELECT `id` FROM `character` WHERE `id` = ANY(SELECT `id` FROM `character`)"#
3246 /// );
3247 /// assert_eq!(
3248 /// query.to_string(PostgresQueryBuilder),
3249 /// r#"SELECT "id" FROM "character" WHERE "id" = ANY(SELECT "id" FROM "character")"#
3250 /// );
3251 /// ```
3252 pub fn any(sel: SelectStatement) -> SimpleExpr {
3253 SimpleExpr::SubQuery(
3254 Some(SubQueryOper::Any),
3255 Box::new(sel.into_sub_query_statement()),
3256 )
3257 }
3258
3259 /// Express a `SOME` sub-query expression.
3260 ///
3261 /// # Examples
3262 ///
3263 /// ```
3264 /// use sea_query::{tests_cfg::*, *};
3265 ///
3266 /// let query = Query::select()
3267 /// .column(Char::Id)
3268 /// .from(Char::Table)
3269 /// .and_where(Expr::col(Char::Id).ne(Expr::some(
3270 /// Query::select().column(Char::Id).from(Char::Table).take(),
3271 /// )))
3272 /// .to_owned();
3273 ///
3274 /// assert_eq!(
3275 /// query.to_string(MysqlQueryBuilder),
3276 /// r#"SELECT `id` FROM `character` WHERE `id` <> SOME(SELECT `id` FROM `character`)"#
3277 /// );
3278 /// assert_eq!(
3279 /// query.to_string(PostgresQueryBuilder),
3280 /// r#"SELECT "id" FROM "character" WHERE "id" <> SOME(SELECT "id" FROM "character")"#
3281 /// );
3282 /// ```
3283 pub fn some(sel: SelectStatement) -> SimpleExpr {
3284 SimpleExpr::SubQuery(
3285 Some(SubQueryOper::Some),
3286 Box::new(sel.into_sub_query_statement()),
3287 )
3288 }
3289
3290 /// Express a `ALL` sub-query expression.
3291 pub fn all(sel: SelectStatement) -> SimpleExpr {
3292 SimpleExpr::SubQuery(
3293 Some(SubQueryOper::All),
3294 Box::new(sel.into_sub_query_statement()),
3295 )
3296 }
3297
3298 /// Express a `AS enum` expression.
3299 ///
3300 /// This is equivalent to a newer [ExprTrait::as_enum] and may require more some wrapping beforehand.
3301 ///
3302 /// # Examples
3303 ///
3304 /// ```
3305 /// use sea_query::{tests_cfg::*, *};
3306 ///
3307 /// let query = Query::select()
3308 /// .expr(Expr::col(Char::FontSize).as_enum(Alias::new("text")))
3309 /// .from(Char::Table)
3310 /// .to_owned();
3311 ///
3312 /// assert_eq!(
3313 /// query.to_string(MysqlQueryBuilder),
3314 /// r#"SELECT `font_size` FROM `character`"#
3315 /// );
3316 /// assert_eq!(
3317 /// query.to_string(PostgresQueryBuilder),
3318 /// r#"SELECT CAST("font_size" AS "text") FROM "character""#
3319 /// );
3320 /// assert_eq!(
3321 /// query.to_string(SqliteQueryBuilder),
3322 /// r#"SELECT "font_size" FROM "character""#
3323 /// );
3324 ///
3325 /// struct TextArray;
3326 ///
3327 /// impl Iden for TextArray {
3328 /// fn unquoted(&self, s: &mut dyn std::fmt::Write) {
3329 /// write!(s, "text[]").unwrap();
3330 /// }
3331 /// }
3332 ///
3333 /// let query = Query::select()
3334 /// .expr(Expr::col(Char::FontSize).as_enum(TextArray))
3335 /// .from(Char::Table)
3336 /// .to_owned();
3337 ///
3338 /// assert_eq!(
3339 /// query.to_string(MysqlQueryBuilder),
3340 /// r#"SELECT `font_size` FROM `character`"#
3341 /// );
3342 /// assert_eq!(
3343 /// query.to_string(PostgresQueryBuilder),
3344 /// r#"SELECT CAST("font_size" AS "text"[]) FROM "character""#
3345 /// );
3346 /// assert_eq!(
3347 /// query.to_string(SqliteQueryBuilder),
3348 /// r#"SELECT "font_size" FROM "character""#
3349 /// );
3350 ///
3351 /// let query = Query::insert()
3352 /// .into_table(Char::Table)
3353 /// .columns([Char::FontSize])
3354 /// .values_panic([Expr::val("large").as_enum(Alias::new("FontSizeEnum"))])
3355 /// .to_owned();
3356 ///
3357 /// assert_eq!(
3358 /// query.to_string(MysqlQueryBuilder),
3359 /// r#"INSERT INTO `character` (`font_size`) VALUES ('large')"#
3360 /// );
3361 /// assert_eq!(
3362 /// query.to_string(PostgresQueryBuilder),
3363 /// r#"INSERT INTO "character" ("font_size") VALUES (CAST('large' AS "FontSizeEnum"))"#
3364 /// );
3365 /// assert_eq!(
3366 /// query.to_string(SqliteQueryBuilder),
3367 /// r#"INSERT INTO "character" ("font_size") VALUES ('large')"#
3368 /// );
3369 /// ```
3370 pub fn as_enum<T>(self, type_name: T) -> SimpleExpr
3371 where
3372 T: IntoIden,
3373 {
3374 ExprTrait::as_enum(self, type_name)
3375 }
3376
3377 /// Adds new `CASE WHEN` to existing case statement.
3378 ///
3379 /// # Examples
3380 ///
3381 /// ```
3382 /// use sea_query::{*, tests_cfg::*};
3383 ///
3384 /// let query = Query::select()
3385 /// .expr_as(
3386 /// Expr::case(
3387 /// Expr::col((Glyph::Table, Glyph::Aspect)).is_in([2, 4]),
3388 /// true
3389 /// )
3390 /// .finally(false),
3391 /// Alias::new("is_even")
3392 /// )
3393 /// .from(Glyph::Table)
3394 /// .to_owned();
3395 ///
3396 /// assert_eq!(
3397 /// query.to_string(PostgresQueryBuilder),
3398 /// r#"SELECT (CASE WHEN ("glyph"."aspect" IN (2, 4)) THEN TRUE ELSE FALSE END) AS "is_even" FROM "glyph""#
3399 /// );
3400 /// ```
3401 pub fn case<C, T>(cond: C, then: T) -> CaseStatement
3402 where
3403 C: IntoCondition,
3404 T: Into<SimpleExpr>,
3405 {
3406 CaseStatement::new().case(cond, then)
3407 }
3408
3409 /// Express a `CAST AS` expression.
3410 ///
3411 /// This is equivalent to a newer [ExprTrait::cast_as] and may require more some wrapping beforehand.
3412 ///
3413 /// # Examples
3414 ///
3415 /// ```
3416 /// use sea_query::{tests_cfg::*, *};
3417 ///
3418 /// let query = Query::select()
3419 /// .expr(Expr::val("1").cast_as(Alias::new("integer")))
3420 /// .to_owned();
3421 ///
3422 /// assert_eq!(
3423 /// query.to_string(MysqlQueryBuilder),
3424 /// r#"SELECT CAST('1' AS integer)"#
3425 /// );
3426 /// assert_eq!(
3427 /// query.to_string(PostgresQueryBuilder),
3428 /// r#"SELECT CAST('1' AS integer)"#
3429 /// );
3430 /// assert_eq!(
3431 /// query.to_string(SqliteQueryBuilder),
3432 /// r#"SELECT CAST('1' AS integer)"#
3433 /// );
3434 /// ```
3435 pub fn cast_as<T>(self, type_name: T) -> SimpleExpr
3436 where
3437 T: IntoIden,
3438 {
3439 ExprTrait::cast_as(self, type_name)
3440 }
3441
3442 /// Keyword `CURRENT_DATE`.
3443 ///
3444 /// # Examples
3445 ///
3446 /// ```
3447 /// use sea_query::*;
3448 ///
3449 /// let query = Query::select().expr(Expr::current_date()).to_owned();
3450 ///
3451 /// assert_eq!(query.to_string(MysqlQueryBuilder), r#"SELECT CURRENT_DATE"#);
3452 /// assert_eq!(
3453 /// query.to_string(PostgresQueryBuilder),
3454 /// r#"SELECT CURRENT_DATE"#
3455 /// );
3456 /// assert_eq!(
3457 /// query.to_string(SqliteQueryBuilder),
3458 /// r#"SELECT CURRENT_DATE"#
3459 /// );
3460 /// ```
3461 pub fn current_date() -> Expr {
3462 Expr::new_with_left(Keyword::CurrentDate)
3463 }
3464
3465 /// Keyword `CURRENT_TIMESTAMP`.
3466 ///
3467 /// # Examples
3468 ///
3469 /// ```
3470 /// use sea_query::*;
3471 ///
3472 /// let query = Query::select().expr(Expr::current_time()).to_owned();
3473 ///
3474 /// assert_eq!(query.to_string(MysqlQueryBuilder), r#"SELECT CURRENT_TIME"#);
3475 /// assert_eq!(
3476 /// query.to_string(PostgresQueryBuilder),
3477 /// r#"SELECT CURRENT_TIME"#
3478 /// );
3479 /// assert_eq!(
3480 /// query.to_string(SqliteQueryBuilder),
3481 /// r#"SELECT CURRENT_TIME"#
3482 /// );
3483 /// ```
3484 pub fn current_time() -> Expr {
3485 Expr::new_with_left(Keyword::CurrentTime)
3486 }
3487
3488 /// Keyword `CURRENT_TIMESTAMP`.
3489 ///
3490 /// # Examples
3491 ///
3492 /// ```
3493 /// use sea_query::{Expr, MysqlQueryBuilder, PostgresQueryBuilder, Query, SqliteQueryBuilder};
3494 ///
3495 /// let query = Query::select().expr(Expr::current_timestamp()).to_owned();
3496 ///
3497 /// assert_eq!(
3498 /// query.to_string(MysqlQueryBuilder),
3499 /// r#"SELECT CURRENT_TIMESTAMP"#
3500 /// );
3501 /// assert_eq!(
3502 /// query.to_string(PostgresQueryBuilder),
3503 /// r#"SELECT CURRENT_TIMESTAMP"#
3504 /// );
3505 /// assert_eq!(
3506 /// query.to_string(SqliteQueryBuilder),
3507 /// r#"SELECT CURRENT_TIMESTAMP"#
3508 /// );
3509 /// ```
3510 pub fn current_timestamp() -> Expr {
3511 Expr::new_with_left(Keyword::CurrentTimestamp)
3512 }
3513
3514 /// Custom keyword.
3515 ///
3516 /// # Examples
3517 ///
3518 /// ```
3519 /// use sea_query::*;
3520 ///
3521 /// let query = Query::select()
3522 /// .expr(Expr::custom_keyword(Alias::new("test")))
3523 /// .to_owned();
3524 ///
3525 /// assert_eq!(query.to_string(MysqlQueryBuilder), r#"SELECT test"#);
3526 /// assert_eq!(query.to_string(PostgresQueryBuilder), r#"SELECT test"#);
3527 /// assert_eq!(query.to_string(SqliteQueryBuilder), r#"SELECT test"#);
3528 /// ```
3529 pub fn custom_keyword<T>(i: T) -> Expr
3530 where
3531 T: IntoIden,
3532 {
3533 Expr::new_with_left(Keyword::Custom(i.into_iden()))
3534 }
3535}
3536
3537impl From<Expr> for SimpleExpr {
3538 /// Convert into SimpleExpr
3539 fn from(src: Expr) -> Self {
3540 if let Some(uopr) = src.uopr {
3541 SimpleExpr::Unary(uopr, Box::new(src.left))
3542 } else if let Some(bopr) = src.bopr {
3543 SimpleExpr::Binary(Box::new(src.left), bopr, Box::new(src.right.unwrap()))
3544 } else {
3545 src.left
3546 }
3547 }
3548}
3549
3550impl<T> From<T> for SimpleExpr
3551where
3552 T: Into<Value>,
3553{
3554 fn from(v: T) -> Self {
3555 SimpleExpr::Value(v.into())
3556 }
3557}
3558
3559impl From<FunctionCall> for SimpleExpr {
3560 fn from(func: FunctionCall) -> Self {
3561 SimpleExpr::FunctionCall(func)
3562 }
3563}
3564
3565impl From<ColumnRef> for SimpleExpr {
3566 fn from(col: ColumnRef) -> Self {
3567 SimpleExpr::Column(col)
3568 }
3569}
3570
3571impl From<Keyword> for SimpleExpr {
3572 fn from(k: Keyword) -> Self {
3573 SimpleExpr::Keyword(k)
3574 }
3575}
3576
3577impl From<LikeExpr> for SimpleExpr {
3578 fn from(like: LikeExpr) -> Self {
3579 match like.escape {
3580 Some(escape) => SimpleExpr::Binary(
3581 Box::new(like.pattern.into()),
3582 BinOper::Escape,
3583 Box::new(SimpleExpr::Constant(escape.into())),
3584 ),
3585 None => like.pattern.into(),
3586 }
3587 }
3588}
3589
3590impl SimpleExpr {
3591 /// Negates an expression with `NOT`.
3592 ///
3593 /// This is equivalent to a newer [ExprTrait::not] and may require more some wrapping beforehand.
3594 ///
3595 /// # Examples
3596 ///
3597 /// ```
3598 /// use sea_query::{tests_cfg::*, *};
3599 ///
3600 /// let query = Query::select()
3601 /// .column(Char::SizeW)
3602 /// .from(Char::Table)
3603 /// .and_where(Expr::col(Char::SizeW).eq(1).not())
3604 /// .to_owned();
3605 ///
3606 /// assert_eq!(
3607 /// query.to_string(MysqlQueryBuilder),
3608 /// r#"SELECT `size_w` FROM `character` WHERE NOT `size_w` = 1"#
3609 /// );
3610 /// assert_eq!(
3611 /// query.to_string(PostgresQueryBuilder),
3612 /// r#"SELECT "size_w" FROM "character" WHERE NOT "size_w" = 1"#
3613 /// );
3614 /// assert_eq!(
3615 /// query.to_string(SqliteQueryBuilder),
3616 /// r#"SELECT "size_w" FROM "character" WHERE NOT "size_w" = 1"#
3617 /// );
3618 /// ```
3619 #[allow(clippy::should_implement_trait)]
3620 pub fn not(self) -> SimpleExpr {
3621 ExprTrait::not(self)
3622 }
3623
3624 /// Express a logical `AND` operation.
3625 ///
3626 /// # Examples
3627 ///
3628 /// ```
3629 /// use sea_query::{*, tests_cfg::*};
3630 ///
3631 /// let query = Query::select()
3632 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
3633 /// .from(Char::Table)
3634 /// .cond_where(any![
3635 /// Expr::col(Char::SizeW).eq(1).and(Expr::col(Char::SizeH).eq(2)),
3636 /// Expr::col(Char::SizeW).eq(3).and(Expr::col(Char::SizeH).eq(4)),
3637 /// ])
3638 /// .to_owned();
3639 ///
3640 /// assert_eq!(
3641 /// query.to_string(MysqlQueryBuilder),
3642 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE (`size_w` = 1 AND `size_h` = 2) OR (`size_w` = 3 AND `size_h` = 4)"#
3643 /// );
3644 /// assert_eq!(
3645 /// query.to_string(PostgresQueryBuilder),
3646 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE ("size_w" = 1 AND "size_h" = 2) OR ("size_w" = 3 AND "size_h" = 4)"#
3647 /// );
3648 /// assert_eq!(
3649 /// query.to_string(SqliteQueryBuilder),
3650 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE ("size_w" = 1 AND "size_h" = 2) OR ("size_w" = 3 AND "size_h" = 4)"#
3651 /// );
3652 /// ```
3653 pub fn and(self, right: SimpleExpr) -> Self {
3654 ExprTrait::and(self, right)
3655 }
3656
3657 /// Express a logical `OR` operation.
3658 ///
3659 /// This is equivalent to a newer [ExprTrait::or] and may require more some wrapping beforehand.
3660 ///
3661 /// # Examples
3662 ///
3663 /// ```
3664 /// use sea_query::{*, tests_cfg::*};
3665 ///
3666 /// let query = Query::select()
3667 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
3668 /// .from(Char::Table)
3669 /// .and_where(Expr::col(Char::SizeW).eq(1).or(Expr::col(Char::SizeH).eq(2)))
3670 /// .and_where(Expr::col(Char::SizeW).eq(3).or(Expr::col(Char::SizeH).eq(4)))
3671 /// .to_owned();
3672 ///
3673 /// assert_eq!(
3674 /// query.to_string(MysqlQueryBuilder),
3675 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE (`size_w` = 1 OR `size_h` = 2) AND (`size_w` = 3 OR `size_h` = 4)"#
3676 /// );
3677 /// assert_eq!(
3678 /// query.to_string(PostgresQueryBuilder),
3679 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE ("size_w" = 1 OR "size_h" = 2) AND ("size_w" = 3 OR "size_h" = 4)"#
3680 /// );
3681 /// assert_eq!(
3682 /// query.to_string(SqliteQueryBuilder),
3683 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE ("size_w" = 1 OR "size_h" = 2) AND ("size_w" = 3 OR "size_h" = 4)"#
3684 /// );
3685 /// ```
3686 pub fn or(self, right: SimpleExpr) -> Self {
3687 ExprTrait::or(self, right)
3688 }
3689
3690 /// Express an equal (`=`) expression.
3691 ///
3692 /// This is equivalent to a newer [ExprTrait::eq] and may require more some wrapping beforehand.
3693 ///
3694 /// # Examples
3695 ///
3696 /// ```
3697 /// use sea_query::{tests_cfg::*, *};
3698 ///
3699 /// let query = Query::select()
3700 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
3701 /// .from(Char::Table)
3702 /// .and_where(Expr::value("What!").eq("Nothing"))
3703 /// .to_owned();
3704 ///
3705 /// assert_eq!(
3706 /// query.to_string(MysqlQueryBuilder),
3707 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE 'What!' = 'Nothing'"#
3708 /// );
3709 /// assert_eq!(
3710 /// query.to_string(PostgresQueryBuilder),
3711 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 'What!' = 'Nothing'"#
3712 /// );
3713 /// assert_eq!(
3714 /// query.to_string(SqliteQueryBuilder),
3715 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 'What!' = 'Nothing'"#
3716 /// );
3717 /// ```
3718 pub fn eq<V>(self, v: V) -> SimpleExpr
3719 where
3720 V: Into<SimpleExpr>,
3721 {
3722 ExprTrait::eq(self, v)
3723 }
3724
3725 /// Express a not equal (`<>`) expression.
3726 ///
3727 /// This is equivalent to a newer [ExprTrait::ne] and may require more some wrapping beforehand.
3728 ///
3729 /// # Examples
3730 ///
3731 /// ```
3732 /// use sea_query::{tests_cfg::*, *};
3733 ///
3734 /// let query = Query::select()
3735 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
3736 /// .from(Char::Table)
3737 /// .and_where(Expr::value("Morning").ne("Good"))
3738 /// .to_owned();
3739 ///
3740 /// assert_eq!(
3741 /// query.to_string(MysqlQueryBuilder),
3742 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE 'Morning' <> 'Good'"#
3743 /// );
3744 /// assert_eq!(
3745 /// query.to_string(PostgresQueryBuilder),
3746 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 'Morning' <> 'Good'"#
3747 /// );
3748 /// assert_eq!(
3749 /// query.to_string(SqliteQueryBuilder),
3750 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 'Morning' <> 'Good'"#
3751 /// );
3752 /// ```
3753 pub fn ne<V>(self, v: V) -> SimpleExpr
3754 where
3755 V: Into<SimpleExpr>,
3756 {
3757 ExprTrait::ne(self, v)
3758 }
3759
3760 /// Perform addition with another [`SimpleExpr`].
3761 ///
3762 /// This is equivalent to a newer [ExprTrait::add] and may require more some wrapping beforehand.
3763 ///
3764 /// # Examples
3765 ///
3766 /// ```
3767 /// use sea_query::{tests_cfg::*, *};
3768 ///
3769 /// let query = Query::select()
3770 /// .expr(
3771 /// Expr::col(Char::SizeW)
3772 /// .max()
3773 /// .add(Expr::col(Char::SizeH).max()),
3774 /// )
3775 /// .from(Char::Table)
3776 /// .to_owned();
3777 ///
3778 /// assert_eq!(
3779 /// query.to_string(MysqlQueryBuilder),
3780 /// r#"SELECT MAX(`size_w`) + MAX(`size_h`) FROM `character`"#
3781 /// );
3782 /// assert_eq!(
3783 /// query.to_string(PostgresQueryBuilder),
3784 /// r#"SELECT MAX("size_w") + MAX("size_h") FROM "character""#
3785 /// );
3786 /// assert_eq!(
3787 /// query.to_string(SqliteQueryBuilder),
3788 /// r#"SELECT MAX("size_w") + MAX("size_h") FROM "character""#
3789 /// );
3790 /// ```
3791 #[allow(clippy::should_implement_trait)]
3792 pub fn add<T>(self, right: T) -> Self
3793 where
3794 T: Into<SimpleExpr>,
3795 {
3796 ExprTrait::add(self, right)
3797 }
3798
3799 /// Perform multiplication with another [`SimpleExpr`].
3800 ///
3801 /// This is equivalent to a newer [ExprTrait::mul] and may require more some wrapping beforehand.
3802 ///
3803 /// # Examples
3804 ///
3805 /// ```
3806 /// use sea_query::{tests_cfg::*, *};
3807 ///
3808 /// let query = Query::select()
3809 /// .expr(
3810 /// Expr::col(Char::SizeW)
3811 /// .max()
3812 /// .mul(Expr::col(Char::SizeH).max()),
3813 /// )
3814 /// .from(Char::Table)
3815 /// .to_owned();
3816 ///
3817 /// assert_eq!(
3818 /// query.to_string(MysqlQueryBuilder),
3819 /// r#"SELECT MAX(`size_w`) * MAX(`size_h`) FROM `character`"#
3820 /// );
3821 /// assert_eq!(
3822 /// query.to_string(PostgresQueryBuilder),
3823 /// r#"SELECT MAX("size_w") * MAX("size_h") FROM "character""#
3824 /// );
3825 /// assert_eq!(
3826 /// query.to_string(SqliteQueryBuilder),
3827 /// r#"SELECT MAX("size_w") * MAX("size_h") FROM "character""#
3828 /// );
3829 /// ```
3830 #[allow(clippy::should_implement_trait)]
3831 pub fn mul<T>(self, right: T) -> Self
3832 where
3833 T: Into<SimpleExpr>,
3834 {
3835 ExprTrait::mul(self, right)
3836 }
3837
3838 /// Perform division with another [`SimpleExpr`].
3839 ///
3840 /// This is equivalent to a newer [ExprTrait::div] and may require more some wrapping beforehand.
3841 ///
3842 /// # Examples
3843 ///
3844 /// ```
3845 /// use sea_query::{tests_cfg::*, *};
3846 ///
3847 /// let query = Query::select()
3848 /// .expr(
3849 /// Expr::col(Char::SizeW)
3850 /// .max()
3851 /// .div(Expr::col(Char::SizeH).max()),
3852 /// )
3853 /// .from(Char::Table)
3854 /// .to_owned();
3855 ///
3856 /// assert_eq!(
3857 /// query.to_string(MysqlQueryBuilder),
3858 /// r#"SELECT MAX(`size_w`) / MAX(`size_h`) FROM `character`"#
3859 /// );
3860 /// assert_eq!(
3861 /// query.to_string(PostgresQueryBuilder),
3862 /// r#"SELECT MAX("size_w") / MAX("size_h") FROM "character""#
3863 /// );
3864 /// assert_eq!(
3865 /// query.to_string(SqliteQueryBuilder),
3866 /// r#"SELECT MAX("size_w") / MAX("size_h") FROM "character""#
3867 /// );
3868 /// ```
3869 #[allow(clippy::should_implement_trait)]
3870 pub fn div<T>(self, right: T) -> Self
3871 where
3872 T: Into<SimpleExpr>,
3873 {
3874 ExprTrait::div(self, right)
3875 }
3876
3877 /// Perform subtraction with another [`SimpleExpr`].
3878 ///
3879 /// This is equivalent to a newer [ExprTrait::sub] and may require more some wrapping beforehand.
3880 ///
3881 /// # Examples
3882 ///
3883 /// ```
3884 /// use sea_query::{tests_cfg::*, *};
3885 ///
3886 /// let query = Query::select()
3887 /// .expr(
3888 /// Expr::col(Char::SizeW)
3889 /// .max()
3890 /// .sub(Expr::col(Char::SizeW).min()),
3891 /// )
3892 /// .from(Char::Table)
3893 /// .to_owned();
3894 ///
3895 /// assert_eq!(
3896 /// query.to_string(MysqlQueryBuilder),
3897 /// r#"SELECT MAX(`size_w`) - MIN(`size_w`) FROM `character`"#
3898 /// );
3899 /// assert_eq!(
3900 /// query.to_string(PostgresQueryBuilder),
3901 /// r#"SELECT MAX("size_w") - MIN("size_w") FROM "character""#
3902 /// );
3903 /// assert_eq!(
3904 /// query.to_string(SqliteQueryBuilder),
3905 /// r#"SELECT MAX("size_w") - MIN("size_w") FROM "character""#
3906 /// );
3907 /// ```
3908 #[allow(clippy::should_implement_trait)]
3909 pub fn sub<T>(self, right: T) -> Self
3910 where
3911 T: Into<SimpleExpr>,
3912 {
3913 ExprTrait::sub(self, right)
3914 }
3915
3916 /// Express a `CAST AS` expression.
3917 ///
3918 /// This is equivalent to a newer [ExprTrait::cast_as] and may require more some wrapping beforehand.
3919 ///
3920 /// # Examples
3921 ///
3922 /// ```
3923 /// use sea_query::{tests_cfg::*, *};
3924 ///
3925 /// let query = Query::select()
3926 /// .expr(Expr::value("1").cast_as(Alias::new("integer")))
3927 /// .to_owned();
3928 ///
3929 /// assert_eq!(
3930 /// query.to_string(MysqlQueryBuilder),
3931 /// r#"SELECT CAST('1' AS integer)"#
3932 /// );
3933 /// assert_eq!(
3934 /// query.to_string(PostgresQueryBuilder),
3935 /// r#"SELECT CAST('1' AS integer)"#
3936 /// );
3937 /// assert_eq!(
3938 /// query.to_string(SqliteQueryBuilder),
3939 /// r#"SELECT CAST('1' AS integer)"#
3940 /// );
3941 /// ```
3942 pub fn cast_as<T>(self, type_name: T) -> Self
3943 where
3944 T: IntoIden,
3945 {
3946 ExprTrait::cast_as(self, type_name)
3947 }
3948
3949 /// Soft deprecated. This is not meant to be in the public API.
3950 #[doc(hidden)]
3951 pub fn cast_as_quoted<T>(self, type_name: T, q: Quote) -> Self
3952 where
3953 T: IntoIden,
3954 {
3955 let func = Func::cast_as_quoted(self, type_name, q);
3956 Self::FunctionCall(func)
3957 }
3958
3959 /// Create any binary operation
3960 ///
3961 /// This is equivalent to a newer [ExprTrait::add] and may require more some wrapping beforehand.
3962 ///
3963 /// # Examples
3964 /// ```
3965 /// use sea_query::{*, tests_cfg::*};
3966 ///
3967 /// let query = Query::select()
3968 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
3969 /// .from(Char::Table)
3970 /// .cond_where(all![
3971 /// Expr::value(10).binary(BinOper::SmallerThan, Expr::col(Char::SizeW)),
3972 /// Expr::value(20).binary(BinOper::GreaterThan, Expr::col(Char::SizeH))
3973 /// ])
3974 /// .to_owned();
3975 /// assert_eq!(
3976 /// query.to_string(MysqlQueryBuilder),
3977 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE 10 < `size_w` AND 20 > `size_h`"#
3978 /// );
3979 /// assert_eq!(
3980 /// query.to_string(PostgresQueryBuilder),
3981 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 10 < "size_w" AND 20 > "size_h""#
3982 /// );
3983 /// assert_eq!(
3984 /// query.to_string(SqliteQueryBuilder),
3985 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 10 < "size_w" AND 20 > "size_h""#
3986 /// );
3987 pub fn binary<O, T>(self, op: O, right: T) -> Self
3988 where
3989 O: Into<BinOper>,
3990 T: Into<SimpleExpr>,
3991 {
3992 ExprTrait::binary(self, op, right)
3993 }
3994
3995 /// Express a `LIKE` expression.
3996 ///
3997 /// This is equivalent to a newer [ExprTrait::like] and may require more some wrapping beforehand.
3998 ///
3999 /// # Examples
4000 ///
4001 /// ```
4002 /// use sea_query::{*, tests_cfg::*};
4003 ///
4004 /// let query = Query::select()
4005 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
4006 /// .from(Char::Table)
4007 /// .and_where(Expr::col((Char::Table, Char::FontId)).cast_as(Alias::new("TEXT")).like("a%"))
4008 /// .to_owned();
4009 ///
4010 /// assert_eq!(
4011 /// query.to_string(MysqlQueryBuilder),
4012 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE CAST(`character`.`font_id` AS TEXT) LIKE 'a%'"#
4013 /// );
4014 /// assert_eq!(
4015 /// query.to_string(PostgresQueryBuilder),
4016 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE CAST("character"."font_id" AS TEXT) LIKE 'a%'"#
4017 /// );
4018 /// assert_eq!(
4019 /// query.to_string(SqliteQueryBuilder),
4020 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE CAST("character"."font_id" AS TEXT) LIKE 'a%'"#
4021 /// );
4022 /// ```
4023 pub fn like<L: IntoLikeExpr>(self, like: L) -> Self {
4024 ExprTrait::like(self, like)
4025 }
4026
4027 /// Express a `NOT LIKE` expression.
4028 ///
4029 /// This is equivalent to a newer [ExprTrait::not_like] and may require more some wrapping beforehand.
4030 pub fn not_like<L: IntoLikeExpr>(self, like: L) -> Self {
4031 ExprTrait::not_like(self, like)
4032 }
4033
4034 pub(crate) fn is_binary(&self) -> bool {
4035 matches!(self, Self::Binary(_, _, _))
4036 }
4037
4038 pub(crate) fn get_bin_oper(&self) -> Option<&BinOper> {
4039 match self {
4040 Self::Binary(_, oper, _) => Some(oper),
4041 _ => None,
4042 }
4043 }
4044}