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