sea_query/expr.rs
1//! Building blocks of SQL statements.
2//!
3//! [`Expr`] representing the primitive building block in the expressions.
4//!
5//! [`SimpleExpr`] is the expression common among select fields, where clauses and many other places.
6
7use crate::{func::*, query::*, types::*, value::*};
8
9/// Helper to build a [`SimpleExpr`].
10#[derive(Debug, Clone)]
11pub struct Expr {
12 pub(crate) left: SimpleExpr,
13 pub(crate) right: Option<SimpleExpr>,
14 pub(crate) uopr: Option<UnOper>,
15 pub(crate) bopr: Option<BinOper>,
16}
17
18/// Represents a Simple Expression in SQL.
19///
20/// [`SimpleExpr`] is a node in the expression tree and can represent identifiers, function calls,
21/// various operators and sub-queries.
22#[derive(Debug, Clone, PartialEq)]
23pub enum SimpleExpr {
24 Column(ColumnRef),
25 Tuple(Vec<SimpleExpr>),
26 Unary(UnOper, Box<SimpleExpr>),
27 FunctionCall(FunctionCall),
28 Binary(Box<SimpleExpr>, BinOper, Box<SimpleExpr>),
29 SubQuery(Option<SubQueryOper>, Box<SubQueryStatement>),
30 Value(Value),
31 Values(Vec<Value>),
32 Custom(String),
33 CustomWithExpr(String, Vec<SimpleExpr>),
34 Keyword(Keyword),
35 AsEnum(DynIden, Box<SimpleExpr>),
36 Case(Box<CaseStatement>),
37 Constant(Value),
38}
39
40/// "Operator" methods for building expressions.
41///
42/// Before `sea_query` 0.32.0 (`sea_orm` 1.1.1),
43/// these methods were awailable only on [`Expr`]/[`SimpleExpr`]
44/// and you needed to manually construct these types first.
45///
46/// Now, you can call them directly on any expression type:
47///
48/// ```no_run
49/// # use sea_query::*;
50/// #
51/// let expr = 1_i32.cast_as("REAL");
52///
53/// let expr = Func::char_length("abc").eq(3_i32);
54///
55/// let expr = Expr::current_date().cast_as("TEXT").like("2024%");
56/// ```
57pub trait ExprTrait: Sized {
58 /// Express an arithmetic addition operation.
59 ///
60 /// # Examples
61 ///
62 /// ```
63 /// use sea_query::{tests_cfg::*, *};
64 ///
65 /// let query = Query::select()
66 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
67 /// .from(Char::Table)
68 /// .and_where(1.add(1).eq(2))
69 /// .to_owned();
70 ///
71 /// assert_eq!(
72 /// query.to_string(MysqlQueryBuilder),
73 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE 1 + 1 = 2"#
74 /// );
75 /// assert_eq!(
76 /// query.to_string(PostgresQueryBuilder),
77 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 + 1 = 2"#
78 /// );
79 /// assert_eq!(
80 /// query.to_string(SqliteQueryBuilder),
81 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 + 1 = 2"#
82 /// );
83 /// ```
84 fn add<R>(self, right: R) -> SimpleExpr
85 where
86 R: Into<SimpleExpr>,
87 {
88 ExprTrait::binary(self, BinOper::Add, right)
89 }
90
91 /// Express a `AS enum` expression.
92 ///
93 /// # Examples
94 ///
95 /// ```
96 /// use sea_query::{tests_cfg::*, *};
97 ///
98 /// let query = Query::insert()
99 /// .into_table(Char::Table)
100 /// .columns([Char::FontSize])
101 /// .values_panic(["large".as_enum("FontSizeEnum")])
102 /// .to_owned();
103 ///
104 /// assert_eq!(
105 /// query.to_string(MysqlQueryBuilder),
106 /// r#"INSERT INTO `character` (`font_size`) VALUES ('large')"#
107 /// );
108 /// assert_eq!(
109 /// query.to_string(PostgresQueryBuilder),
110 /// r#"INSERT INTO "character" ("font_size") VALUES (CAST('large' AS "FontSizeEnum"))"#
111 /// );
112 /// assert_eq!(
113 /// query.to_string(SqliteQueryBuilder),
114 /// r#"INSERT INTO "character" ("font_size") VALUES ('large')"#
115 /// );
116 /// ```
117 #[allow(clippy::wrong_self_convention)]
118 fn as_enum<N>(self, type_name: N) -> SimpleExpr
119 where
120 N: IntoIden;
121
122 fn and<R>(self, right: R) -> SimpleExpr
123 where
124 R: Into<SimpleExpr>,
125 {
126 ExprTrait::binary(self, BinOper::And, right)
127 }
128
129 /// Express a `BETWEEN` expression.
130 ///
131 /// # Examples
132 ///
133 /// ```
134 /// use sea_query::{*, tests_cfg::*};
135 ///
136 /// let query = Query::select()
137 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
138 /// .from(Char::Table)
139 /// .and_where((Char::Table, Char::SizeW).into_column_ref().between(1, 10))
140 /// .to_owned();
141 ///
142 /// assert_eq!(
143 /// query.to_string(MysqlQueryBuilder),
144 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`size_w` BETWEEN 1 AND 10"#
145 /// );
146 /// assert_eq!(
147 /// query.to_string(PostgresQueryBuilder),
148 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" BETWEEN 1 AND 10"#
149 /// );
150 /// assert_eq!(
151 /// query.to_string(SqliteQueryBuilder),
152 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" BETWEEN 1 AND 10"#
153 /// );
154 /// ```
155 fn between<A, B>(self, a: A, b: B) -> SimpleExpr
156 where
157 A: Into<SimpleExpr>,
158 B: Into<SimpleExpr>,
159 {
160 self.binary(
161 BinOper::Between,
162 SimpleExpr::Binary(Box::new(a.into()), BinOper::And, Box::new(b.into())),
163 )
164 }
165
166 /// Create any binary operation
167 ///
168 /// # Examples
169 /// ```
170 /// use sea_query::{*, tests_cfg::*};
171 ///
172 /// let query = Query::select()
173 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
174 /// .from(Char::Table)
175 /// .cond_where(all![
176 /// Char::SizeW.into_column_ref().binary(BinOper::SmallerThan, 10),
177 /// Char::SizeW.into_column_ref().binary(BinOper::GreaterThan, Char::SizeH.into_column_ref())
178 /// ])
179 /// .to_owned();
180 /// assert_eq!(
181 /// query.to_string(MysqlQueryBuilder),
182 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `size_w` < 10 AND `size_w` > `size_h`"#
183 /// );
184 /// assert_eq!(
185 /// query.to_string(PostgresQueryBuilder),
186 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "size_w" < 10 AND "size_w" > "size_h""#
187 /// );
188 /// assert_eq!(
189 /// query.to_string(SqliteQueryBuilder),
190 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "size_w" < 10 AND "size_w" > "size_h""#
191 /// );
192 /// ```
193 fn binary<O, R>(self, op: O, right: R) -> SimpleExpr
194 where
195 O: Into<BinOper>,
196 R: Into<SimpleExpr>;
197
198 /// Express a `CAST AS` expression.
199 ///
200 /// # Examples
201 ///
202 /// ```
203 /// use sea_query::{tests_cfg::*, *};
204 ///
205 /// let query = Query::select().expr("1".cast_as("integer")).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 an expression to perform some operation on it later.
1622 ///
1623 /// Since `sea_query` 0.32.0 (`sea_orm` 1.1.1), **this is not necessary** in most cases!
1624 ///
1625 /// Some SQL operations used to be defined only as inherent methods on [`Expr`].
1626 /// Thus, to use them, you needed to manually convert from other types to [`Expr`].
1627 /// But now these operations are also defined as [`ExprTrait`] methods
1628 /// that can be called directly on any expression type,
1629 ///
1630 /// # Examples
1631 ///
1632 /// ```
1633 /// use sea_query::{tests_cfg::*, *};
1634 ///
1635 /// let query = Query::select()
1636 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
1637 /// .from(Char::Table)
1638 /// // This is the old style, when `Expr::expr` was necessary:
1639 /// .and_where(Expr::expr(Expr::col(Char::SizeW).if_null(0)).gt(2))
1640 /// .to_owned();
1641 ///
1642 /// // But since 0.32.0, this compiles too:
1643 /// let _ = Expr::col(Char::SizeW).if_null(0).gt(2);
1644 ///
1645 /// assert_eq!(
1646 /// query.to_string(MysqlQueryBuilder),
1647 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE IFNULL(`size_w`, 0) > 2"#
1648 /// );
1649 /// assert_eq!(
1650 /// query.to_string(PostgresQueryBuilder),
1651 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE COALESCE("size_w", 0) > 2"#
1652 /// );
1653 /// assert_eq!(
1654 /// query.to_string(SqliteQueryBuilder),
1655 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE IFNULL("size_w", 0) > 2"#
1656 /// );
1657 /// ```
1658 ///
1659 /// ```
1660 /// use sea_query::{tests_cfg::*, *};
1661 ///
1662 /// let query = Query::select()
1663 /// .column(Char::Character)
1664 /// .from(Char::Table)
1665 /// // This is the old style, when `Expr::expr` was necessary:
1666 /// .and_where(Expr::expr(Func::lower(Expr::col(Char::Character))).is_in(["a", "b"]))
1667 /// .to_owned();
1668 ///
1669 /// // But since 0.32.0, this compiles too:
1670 /// let _ = Func::lower(Expr::col(Char::Character)).is_in(["a", "b"]);
1671 ///
1672 /// assert_eq!(
1673 /// query.to_string(MysqlQueryBuilder),
1674 /// r#"SELECT `character` FROM `character` WHERE LOWER(`character`) IN ('a', 'b')"#
1675 /// );
1676 /// assert_eq!(
1677 /// query.to_string(PostgresQueryBuilder),
1678 /// r#"SELECT "character" FROM "character" WHERE LOWER("character") IN ('a', 'b')"#
1679 /// );
1680 /// assert_eq!(
1681 /// query.to_string(SqliteQueryBuilder),
1682 /// r#"SELECT "character" FROM "character" WHERE LOWER("character") IN ('a', 'b')"#
1683 /// );
1684 /// ```
1685 #[allow(clippy::self_named_constructors)]
1686 pub fn expr<T>(expr: T) -> Self
1687 where
1688 T: Into<SimpleExpr>,
1689 {
1690 Self::new_with_left(expr)
1691 }
1692
1693 /// Express a [`Value`], returning a [`SimpleExpr`].
1694 ///
1695 /// # Examples
1696 ///
1697 /// ```
1698 /// use sea_query::{tests_cfg::*, *};
1699 ///
1700 /// let query = Query::select()
1701 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
1702 /// .from(Char::Table)
1703 /// .and_where(Expr::value(1))
1704 /// .and_where(Expr::value(2.5))
1705 /// .and_where(Expr::value("3"))
1706 /// .to_owned();
1707 ///
1708 /// assert_eq!(
1709 /// query.to_string(MysqlQueryBuilder),
1710 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE 1 AND 2.5 AND '3'"#
1711 /// );
1712 /// assert_eq!(
1713 /// query.to_string(PostgresQueryBuilder),
1714 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 AND 2.5 AND '3'"#
1715 /// );
1716 /// assert_eq!(
1717 /// query.to_string(SqliteQueryBuilder),
1718 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 AND 2.5 AND '3'"#
1719 /// );
1720 /// ```
1721 pub fn value<V>(v: V) -> SimpleExpr
1722 where
1723 V: Into<SimpleExpr>,
1724 {
1725 v.into()
1726 }
1727
1728 /// Express any custom expression in [`&str`].
1729 ///
1730 /// # Examples
1731 ///
1732 /// ```
1733 /// use sea_query::{tests_cfg::*, *};
1734 ///
1735 /// let query = Query::select()
1736 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
1737 /// .from(Char::Table)
1738 /// .and_where(Expr::cust("1 = 1"))
1739 /// .to_owned();
1740 ///
1741 /// assert_eq!(
1742 /// query.to_string(MysqlQueryBuilder),
1743 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE 1 = 1"#
1744 /// );
1745 /// assert_eq!(
1746 /// query.to_string(PostgresQueryBuilder),
1747 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 = 1"#
1748 /// );
1749 /// assert_eq!(
1750 /// query.to_string(SqliteQueryBuilder),
1751 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 = 1"#
1752 /// );
1753 /// ```
1754 pub fn cust<T>(s: T) -> SimpleExpr
1755 where
1756 T: Into<String>,
1757 {
1758 SimpleExpr::Custom(s.into())
1759 }
1760
1761 /// Express any custom expression with [`Value`]. Use this if your expression needs variables.
1762 ///
1763 /// # Examples
1764 ///
1765 /// ```
1766 /// use sea_query::{tests_cfg::*, *};
1767 ///
1768 /// let query = Query::select()
1769 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
1770 /// .from(Char::Table)
1771 /// .and_where(Expr::col(Char::Id).eq(1))
1772 /// .and_where(Expr::cust_with_values("6 = ? * ?", [2, 3]))
1773 /// .to_owned();
1774 ///
1775 /// assert_eq!(
1776 /// query.to_string(MysqlQueryBuilder),
1777 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `id` = 1 AND (6 = 2 * 3)"#
1778 /// );
1779 /// assert_eq!(
1780 /// query.to_string(SqliteQueryBuilder),
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 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
1789 /// .from(Char::Table)
1790 /// .and_where(Expr::col(Char::Id).eq(1))
1791 /// .and_where(Expr::cust_with_values("6 = $2 * $1", [3, 2]))
1792 /// .to_owned();
1793 ///
1794 /// assert_eq!(
1795 /// query.to_string(PostgresQueryBuilder),
1796 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "id" = 1 AND (6 = 2 * 3)"#
1797 /// );
1798 /// ```
1799 /// ```
1800 /// use sea_query::{tests_cfg::*, *};
1801 ///
1802 /// let query = Query::select()
1803 /// .expr(Expr::cust_with_values("6 = ? * ?", [2, 3]))
1804 /// .to_owned();
1805 ///
1806 /// assert_eq!(query.to_string(MysqlQueryBuilder), r#"SELECT 6 = 2 * 3"#);
1807 /// assert_eq!(query.to_string(SqliteQueryBuilder), r#"SELECT 6 = 2 * 3"#);
1808 /// ```
1809 /// Postgres only: use `$$` to escape `$`
1810 /// ```
1811 /// use sea_query::{tests_cfg::*, *};
1812 ///
1813 /// let query = Query::select()
1814 /// .expr(Expr::cust_with_values("$1 $$ $2", ["a", "b"]))
1815 /// .to_owned();
1816 ///
1817 /// assert_eq!(query.to_string(PostgresQueryBuilder), r#"SELECT 'a' $ 'b'"#);
1818 /// ```
1819 /// ```
1820 /// use sea_query::{tests_cfg::*, *};
1821 ///
1822 /// let query = Query::select()
1823 /// .expr(Expr::cust_with_values("data @? ($1::JSONPATH)", ["hello"]))
1824 /// .to_owned();
1825 ///
1826 /// assert_eq!(
1827 /// query.to_string(PostgresQueryBuilder),
1828 /// r#"SELECT data @? ('hello'::JSONPATH)"#
1829 /// );
1830 /// ```
1831 pub fn cust_with_values<T, V, I>(s: T, v: I) -> SimpleExpr
1832 where
1833 T: Into<String>,
1834 V: Into<Value>,
1835 I: IntoIterator<Item = V>,
1836 {
1837 SimpleExpr::CustomWithExpr(
1838 s.into(),
1839 v.into_iter()
1840 .map(|v| Into::<Value>::into(v).into())
1841 .collect(),
1842 )
1843 }
1844
1845 /// Express any custom expression with [`SimpleExpr`]. Use this if your expression needs other expression.
1846 ///
1847 /// # Examples
1848 ///
1849 /// ```
1850 /// use sea_query::{tests_cfg::*, *};
1851 ///
1852 /// let query = Query::select()
1853 /// .expr(Expr::val(1).add(2))
1854 /// .expr(Expr::cust_with_expr("data @? ($1::JSONPATH)", "hello"))
1855 /// .to_owned();
1856 /// let (sql, values) = query.build(PostgresQueryBuilder);
1857 ///
1858 /// assert_eq!(sql, r#"SELECT $1 + $2, data @? ($3::JSONPATH)"#);
1859 /// assert_eq!(
1860 /// values,
1861 /// Values(vec![1i32.into(), 2i32.into(), "hello".into()])
1862 /// );
1863 /// ```
1864 /// ```
1865 /// use sea_query::{tests_cfg::*, *};
1866 ///
1867 /// let query = Query::select()
1868 /// .expr(Expr::cust_with_expr(
1869 /// "json_agg(DISTINCT $1)",
1870 /// Expr::col(Char::Character),
1871 /// ))
1872 /// .to_owned();
1873 ///
1874 /// assert_eq!(
1875 /// query.to_string(PostgresQueryBuilder),
1876 /// r#"SELECT json_agg(DISTINCT "character")"#
1877 /// );
1878 /// ```
1879 pub fn cust_with_expr<T, E>(s: T, expr: E) -> SimpleExpr
1880 where
1881 T: Into<String>,
1882 E: Into<SimpleExpr>,
1883 {
1884 SimpleExpr::CustomWithExpr(s.into(), vec![expr.into()])
1885 }
1886
1887 /// Express any custom expression with [`SimpleExpr`]. Use this if your expression needs other expressions.
1888 pub fn cust_with_exprs<T, I>(s: T, v: I) -> SimpleExpr
1889 where
1890 T: Into<String>,
1891 I: IntoIterator<Item = SimpleExpr>,
1892 {
1893 SimpleExpr::CustomWithExpr(s.into(), v.into_iter().collect())
1894 }
1895
1896 /// Express an equal (`=`) expression.
1897 ///
1898 /// This is equivalent to a newer [ExprTrait::eq] and may require more some wrapping beforehand.
1899 ///
1900 /// # Examples
1901 ///
1902 /// ```
1903 /// use sea_query::{*, tests_cfg::*};
1904 ///
1905 /// let query = Query::select()
1906 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
1907 /// .from(Char::Table)
1908 /// .and_where(Expr::val("What!").eq("Nothing"))
1909 /// .and_where(Expr::col(Char::Id).eq(1))
1910 /// .to_owned();
1911 ///
1912 /// assert_eq!(
1913 /// query.to_string(MysqlQueryBuilder),
1914 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE 'What!' = 'Nothing' AND `id` = 1"#
1915 /// );
1916 /// assert_eq!(
1917 /// query.to_string(PostgresQueryBuilder),
1918 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 'What!' = 'Nothing' AND "id" = 1"#
1919 /// );
1920 /// assert_eq!(
1921 /// query.to_string(SqliteQueryBuilder),
1922 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 'What!' = 'Nothing' AND "id" = 1"#
1923 /// );
1924 /// ```
1925 pub fn eq<V>(self, v: V) -> SimpleExpr
1926 where
1927 V: Into<SimpleExpr>,
1928 {
1929 ExprTrait::eq(self, v)
1930 }
1931
1932 /// Express a not equal (`<>`) expression.
1933 ///
1934 /// This is equivalent to a newer [ExprTrait::ne] and may require more some wrapping beforehand.
1935 ///
1936 /// # Examples
1937 ///
1938 /// ```
1939 /// use sea_query::{*, tests_cfg::*};
1940 ///
1941 /// let query = Query::select()
1942 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
1943 /// .from(Char::Table)
1944 /// .and_where(Expr::val("Morning").ne("Good"))
1945 /// .and_where(Expr::col(Char::Id).ne(1))
1946 /// .to_owned();
1947 ///
1948 /// assert_eq!(
1949 /// query.to_string(MysqlQueryBuilder),
1950 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE 'Morning' <> 'Good' AND `id` <> 1"#
1951 /// );
1952 /// assert_eq!(
1953 /// query.to_string(PostgresQueryBuilder),
1954 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 'Morning' <> 'Good' AND "id" <> 1"#
1955 /// );
1956 /// assert_eq!(
1957 /// query.to_string(SqliteQueryBuilder),
1958 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 'Morning' <> 'Good' AND "id" <> 1"#
1959 /// );
1960 /// ```
1961 pub fn ne<V>(self, v: V) -> SimpleExpr
1962 where
1963 V: Into<SimpleExpr>,
1964 {
1965 ExprTrait::ne(self, v)
1966 }
1967
1968 /// Express a equal expression between two table columns,
1969 /// you will mainly use this to relate identical value between two table columns.
1970 ///
1971 /// This is equivalent to a newer [ExprTrait::equals] and may require more some wrapping beforehand.
1972 ///
1973 /// # Examples
1974 ///
1975 /// ```
1976 /// use sea_query::{*, tests_cfg::*};
1977 ///
1978 /// let query = Query::select()
1979 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
1980 /// .from(Char::Table)
1981 /// .and_where(Expr::col((Char::Table, Char::FontId)).equals((Font::Table, Font::Id)))
1982 /// .to_owned();
1983 ///
1984 /// assert_eq!(
1985 /// query.to_string(MysqlQueryBuilder),
1986 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`font_id` = `font`.`id`"#
1987 /// );
1988 /// assert_eq!(
1989 /// query.to_string(PostgresQueryBuilder),
1990 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."font_id" = "font"."id""#
1991 /// );
1992 /// assert_eq!(
1993 /// query.to_string(SqliteQueryBuilder),
1994 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."font_id" = "font"."id""#
1995 /// );
1996 /// ```
1997 pub fn equals<C>(self, col: C) -> SimpleExpr
1998 where
1999 C: IntoColumnRef,
2000 {
2001 ExprTrait::equals(self, col)
2002 }
2003
2004 /// Express a not equal expression between two table columns,
2005 /// you will mainly use this to relate identical value between two table columns.
2006 ///
2007 /// This is equivalent to a newer [ExprTrait::not_equals] and may require more some wrapping beforehand.
2008 ///
2009 /// # Examples
2010 ///
2011 /// ```
2012 /// use sea_query::{*, tests_cfg::*};
2013 ///
2014 /// let query = Query::select()
2015 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
2016 /// .from(Char::Table)
2017 /// .and_where(Expr::col((Char::Table, Char::FontId)).not_equals((Font::Table, Font::Id)))
2018 /// .to_owned();
2019 ///
2020 /// assert_eq!(
2021 /// query.to_string(MysqlQueryBuilder),
2022 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`font_id` <> `font`.`id`"#
2023 /// );
2024 /// assert_eq!(
2025 /// query.to_string(PostgresQueryBuilder),
2026 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."font_id" <> "font"."id""#
2027 /// );
2028 /// assert_eq!(
2029 /// query.to_string(SqliteQueryBuilder),
2030 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."font_id" <> "font"."id""#
2031 /// );
2032 /// ```
2033 pub fn not_equals<C>(self, col: C) -> SimpleExpr
2034 where
2035 C: IntoColumnRef,
2036 {
2037 ExprTrait::not_equals(self, col)
2038 }
2039
2040 /// Express a greater than (`>`) expression.
2041 ///
2042 /// This is equivalent to a newer [ExprTrait::gt] and may require more some wrapping beforehand.
2043 ///
2044 /// # Examples
2045 ///
2046 /// ```
2047 /// use sea_query::{tests_cfg::*, *};
2048 ///
2049 /// let query = Query::select()
2050 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
2051 /// .from(Char::Table)
2052 /// .and_where(Expr::col((Char::Table, Char::SizeW)).gt(2))
2053 /// .to_owned();
2054 ///
2055 /// assert_eq!(
2056 /// query.to_string(MysqlQueryBuilder),
2057 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`size_w` > 2"#
2058 /// );
2059 /// assert_eq!(
2060 /// query.to_string(PostgresQueryBuilder),
2061 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" > 2"#
2062 /// );
2063 /// assert_eq!(
2064 /// query.to_string(SqliteQueryBuilder),
2065 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" > 2"#
2066 /// );
2067 /// ```
2068 pub fn gt<V>(self, v: V) -> SimpleExpr
2069 where
2070 V: Into<SimpleExpr>,
2071 {
2072 ExprTrait::gt(self, v)
2073 }
2074
2075 /// Express a greater than or equal (`>=`) expression.
2076 ///
2077 /// This is equivalent to a newer [ExprTrait::gte] and may require more some wrapping beforehand.
2078 ///
2079 /// # Examples
2080 ///
2081 /// ```
2082 /// use sea_query::{tests_cfg::*, *};
2083 ///
2084 /// let query = Query::select()
2085 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
2086 /// .from(Char::Table)
2087 /// .and_where(Expr::col((Char::Table, Char::SizeW)).gte(2))
2088 /// .to_owned();
2089 ///
2090 /// assert_eq!(
2091 /// query.to_string(MysqlQueryBuilder),
2092 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`size_w` >= 2"#
2093 /// );
2094 /// assert_eq!(
2095 /// query.to_string(PostgresQueryBuilder),
2096 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" >= 2"#
2097 /// );
2098 /// assert_eq!(
2099 /// query.to_string(SqliteQueryBuilder),
2100 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" >= 2"#
2101 /// );
2102 /// ```
2103 pub fn gte<V>(self, v: V) -> SimpleExpr
2104 where
2105 V: Into<SimpleExpr>,
2106 {
2107 ExprTrait::gte(self, v)
2108 }
2109
2110 /// Express a less than (`<`) expression.
2111 ///
2112 /// This is equivalent to a newer [ExprTrait::lt] and may require more some wrapping beforehand.
2113 ///
2114 /// # Examples
2115 ///
2116 /// ```
2117 /// use sea_query::{tests_cfg::*, *};
2118 ///
2119 /// let query = Query::select()
2120 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
2121 /// .from(Char::Table)
2122 /// .and_where(Expr::col((Char::Table, Char::SizeW)).lt(2))
2123 /// .to_owned();
2124 ///
2125 /// assert_eq!(
2126 /// query.to_string(MysqlQueryBuilder),
2127 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`size_w` < 2"#
2128 /// );
2129 /// assert_eq!(
2130 /// query.to_string(PostgresQueryBuilder),
2131 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" < 2"#
2132 /// );
2133 /// assert_eq!(
2134 /// query.to_string(SqliteQueryBuilder),
2135 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" < 2"#
2136 /// );
2137 /// ```
2138 pub fn lt<V>(self, v: V) -> SimpleExpr
2139 where
2140 V: Into<SimpleExpr>,
2141 {
2142 ExprTrait::lt(self, v)
2143 }
2144
2145 /// Express a less than or equal (`<=`) expression.
2146 ///
2147 /// This is equivalent to a newer [ExprTrait::lte] and may require more some wrapping beforehand.
2148 ///
2149 /// # Examples
2150 ///
2151 /// ```
2152 /// use sea_query::{tests_cfg::*, *};
2153 ///
2154 /// let query = Query::select()
2155 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
2156 /// .from(Char::Table)
2157 /// .and_where(Expr::col((Char::Table, Char::SizeW)).lte(2))
2158 /// .to_owned();
2159 ///
2160 /// assert_eq!(
2161 /// query.to_string(MysqlQueryBuilder),
2162 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`size_w` <= 2"#
2163 /// );
2164 /// assert_eq!(
2165 /// query.to_string(PostgresQueryBuilder),
2166 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" <= 2"#
2167 /// );
2168 /// assert_eq!(
2169 /// query.to_string(SqliteQueryBuilder),
2170 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" <= 2"#
2171 /// );
2172 /// ```
2173 pub fn lte<V>(self, v: V) -> SimpleExpr
2174 where
2175 V: Into<SimpleExpr>,
2176 {
2177 ExprTrait::lte(self, v)
2178 }
2179
2180 /// Express an arithmetic addition operation.
2181 ///
2182 /// This is equivalent to a newer [ExprTrait::add] and may require more some wrapping beforehand.
2183 ///
2184 /// # Examples
2185 ///
2186 /// ```
2187 /// use sea_query::{tests_cfg::*, *};
2188 ///
2189 /// let query = Query::select()
2190 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
2191 /// .from(Char::Table)
2192 /// .and_where(Expr::val(1).add(1).eq(2))
2193 /// .to_owned();
2194 ///
2195 /// assert_eq!(
2196 /// query.to_string(MysqlQueryBuilder),
2197 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE 1 + 1 = 2"#
2198 /// );
2199 /// assert_eq!(
2200 /// query.to_string(PostgresQueryBuilder),
2201 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 + 1 = 2"#
2202 /// );
2203 /// assert_eq!(
2204 /// query.to_string(SqliteQueryBuilder),
2205 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 + 1 = 2"#
2206 /// );
2207 /// ```
2208 #[allow(clippy::should_implement_trait)]
2209 pub fn add<V>(self, v: V) -> SimpleExpr
2210 where
2211 V: Into<SimpleExpr>,
2212 {
2213 ExprTrait::add(self, v)
2214 }
2215
2216 /// Express an arithmetic subtraction operation.
2217 ///
2218 /// This is equivalent to a newer [ExprTrait::sub] and may require more some wrapping beforehand.
2219 ///
2220 /// # Examples
2221 ///
2222 /// ```
2223 /// use sea_query::{tests_cfg::*, *};
2224 ///
2225 /// let query = Query::select()
2226 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
2227 /// .from(Char::Table)
2228 /// .and_where(Expr::val(1).sub(1).eq(2))
2229 /// .to_owned();
2230 ///
2231 /// assert_eq!(
2232 /// query.to_string(MysqlQueryBuilder),
2233 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE 1 - 1 = 2"#
2234 /// );
2235 /// assert_eq!(
2236 /// query.to_string(PostgresQueryBuilder),
2237 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 - 1 = 2"#
2238 /// );
2239 /// assert_eq!(
2240 /// query.to_string(SqliteQueryBuilder),
2241 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 - 1 = 2"#
2242 /// );
2243 /// ```
2244 #[allow(clippy::should_implement_trait)]
2245 pub fn sub<V>(self, v: V) -> SimpleExpr
2246 where
2247 V: Into<SimpleExpr>,
2248 {
2249 ExprTrait::sub(self, v)
2250 }
2251
2252 /// Express an arithmetic multiplication operation.
2253 ///
2254 /// This is equivalent to a newer [ExprTrait::mul] and may require more some wrapping beforehand.
2255 ///
2256 /// # Examples
2257 ///
2258 /// ```
2259 /// use sea_query::{tests_cfg::*, *};
2260 ///
2261 /// let query = Query::select()
2262 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
2263 /// .from(Char::Table)
2264 /// .and_where(Expr::val(1).mul(1).eq(2))
2265 /// .to_owned();
2266 ///
2267 /// assert_eq!(
2268 /// query.to_string(MysqlQueryBuilder),
2269 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE 1 * 1 = 2"#
2270 /// );
2271 /// assert_eq!(
2272 /// query.to_string(PostgresQueryBuilder),
2273 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 * 1 = 2"#
2274 /// );
2275 /// assert_eq!(
2276 /// query.to_string(SqliteQueryBuilder),
2277 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 * 1 = 2"#
2278 /// );
2279 /// ```
2280 #[allow(clippy::should_implement_trait)]
2281 pub fn mul<V>(self, v: V) -> SimpleExpr
2282 where
2283 V: Into<SimpleExpr>,
2284 {
2285 ExprTrait::mul(self, v)
2286 }
2287
2288 /// Express an arithmetic division operation.
2289 ///
2290 /// This is equivalent to a newer [ExprTrait::div] and may require more some wrapping beforehand.
2291 ///
2292 /// # Examples
2293 ///
2294 /// ```
2295 /// use sea_query::{tests_cfg::*, *};
2296 ///
2297 /// let query = Query::select()
2298 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
2299 /// .from(Char::Table)
2300 /// .and_where(Expr::val(1).div(1).eq(2))
2301 /// .to_owned();
2302 ///
2303 /// assert_eq!(
2304 /// query.to_string(MysqlQueryBuilder),
2305 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE 1 / 1 = 2"#
2306 /// );
2307 /// assert_eq!(
2308 /// query.to_string(PostgresQueryBuilder),
2309 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 / 1 = 2"#
2310 /// );
2311 /// assert_eq!(
2312 /// query.to_string(SqliteQueryBuilder),
2313 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 / 1 = 2"#
2314 /// );
2315 /// ```
2316 #[allow(clippy::should_implement_trait)]
2317 pub fn div<V>(self, v: V) -> SimpleExpr
2318 where
2319 V: Into<SimpleExpr>,
2320 {
2321 ExprTrait::div(self, v)
2322 }
2323
2324 /// Express an arithmetic modulo operation.
2325 ///
2326 /// This is equivalent to a newer [ExprTrait::modulo] and may require more some wrapping beforehand.
2327 ///
2328 /// # Examples
2329 ///
2330 /// ```
2331 /// use sea_query::{tests_cfg::*, *};
2332 ///
2333 /// let query = Query::select()
2334 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
2335 /// .from(Char::Table)
2336 /// .and_where(Expr::val(1).modulo(1).eq(2))
2337 /// .to_owned();
2338 ///
2339 /// assert_eq!(
2340 /// query.to_string(MysqlQueryBuilder),
2341 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE 1 % 1 = 2"#
2342 /// );
2343 /// assert_eq!(
2344 /// query.to_string(PostgresQueryBuilder),
2345 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 % 1 = 2"#
2346 /// );
2347 /// assert_eq!(
2348 /// query.to_string(SqliteQueryBuilder),
2349 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 % 1 = 2"#
2350 /// );
2351 /// ```
2352 #[allow(clippy::should_implement_trait)]
2353 pub fn modulo<V>(self, v: V) -> SimpleExpr
2354 where
2355 V: Into<SimpleExpr>,
2356 {
2357 ExprTrait::modulo(self, v)
2358 }
2359
2360 /// Express a bitwise left shift.
2361 ///
2362 /// This is equivalent to a newer [ExprTrait::left_shift] and may require more some wrapping beforehand.
2363 ///
2364 /// # Examples
2365 ///
2366 /// ```
2367 /// use sea_query::{tests_cfg::*, *};
2368 ///
2369 /// let query = Query::select()
2370 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
2371 /// .from(Char::Table)
2372 /// .and_where(Expr::val(1).left_shift(1).eq(2))
2373 /// .to_owned();
2374 ///
2375 /// assert_eq!(
2376 /// query.to_string(MysqlQueryBuilder),
2377 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE 1 << 1 = 2"#
2378 /// );
2379 /// assert_eq!(
2380 /// query.to_string(PostgresQueryBuilder),
2381 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 << 1 = 2"#
2382 /// );
2383 /// assert_eq!(
2384 /// query.to_string(SqliteQueryBuilder),
2385 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 << 1 = 2"#
2386 /// );
2387 /// ```
2388 #[allow(clippy::should_implement_trait)]
2389 pub fn left_shift<V>(self, v: V) -> SimpleExpr
2390 where
2391 V: Into<SimpleExpr>,
2392 {
2393 ExprTrait::left_shift(self, v)
2394 }
2395
2396 /// Express a bitwise right shift.
2397 ///
2398 /// This is equivalent to a newer [ExprTrait::right_shift] and may require more some wrapping beforehand.
2399 ///
2400 /// # Examples
2401 ///
2402 /// ```
2403 /// use sea_query::{tests_cfg::*, *};
2404 ///
2405 /// let query = Query::select()
2406 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
2407 /// .from(Char::Table)
2408 /// .and_where(Expr::val(1).right_shift(1).eq(2))
2409 /// .to_owned();
2410 ///
2411 /// assert_eq!(
2412 /// query.to_string(MysqlQueryBuilder),
2413 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE 1 >> 1 = 2"#
2414 /// );
2415 /// assert_eq!(
2416 /// query.to_string(PostgresQueryBuilder),
2417 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 >> 1 = 2"#
2418 /// );
2419 /// assert_eq!(
2420 /// query.to_string(SqliteQueryBuilder),
2421 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 >> 1 = 2"#
2422 /// );
2423 /// ```
2424 #[allow(clippy::should_implement_trait)]
2425 pub fn right_shift<V>(self, v: V) -> SimpleExpr
2426 where
2427 V: Into<SimpleExpr>,
2428 {
2429 ExprTrait::right_shift(self, v)
2430 }
2431
2432 /// Express a `BETWEEN` expression.
2433 ///
2434 /// This is equivalent to a newer [ExprTrait::between] and may require more some wrapping beforehand.
2435 ///
2436 /// # Examples
2437 ///
2438 /// ```
2439 /// use sea_query::{*, tests_cfg::*};
2440 ///
2441 /// let query = Query::select()
2442 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
2443 /// .from(Char::Table)
2444 /// .and_where(Expr::col((Char::Table, Char::SizeW)).between(1, 10))
2445 /// .to_owned();
2446 ///
2447 /// assert_eq!(
2448 /// query.to_string(MysqlQueryBuilder),
2449 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`size_w` BETWEEN 1 AND 10"#
2450 /// );
2451 /// assert_eq!(
2452 /// query.to_string(PostgresQueryBuilder),
2453 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" BETWEEN 1 AND 10"#
2454 /// );
2455 /// assert_eq!(
2456 /// query.to_string(SqliteQueryBuilder),
2457 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" BETWEEN 1 AND 10"#
2458 /// );
2459 /// ```
2460 pub fn between<V>(self, a: V, b: V) -> SimpleExpr
2461 where
2462 V: Into<SimpleExpr>,
2463 {
2464 ExprTrait::between(self, a, b)
2465 }
2466
2467 /// Express a `NOT BETWEEN` expression.
2468 ///
2469 /// This is equivalent to a newer [ExprTrait::not_between] and may require more some wrapping beforehand.
2470 ///
2471 /// # Examples
2472 ///
2473 /// ```
2474 /// use sea_query::{*, tests_cfg::*};
2475 ///
2476 /// let query = Query::select()
2477 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
2478 /// .from(Char::Table)
2479 /// .and_where(Expr::col((Char::Table, Char::SizeW)).not_between(1, 10))
2480 /// .to_owned();
2481 ///
2482 /// assert_eq!(
2483 /// query.to_string(MysqlQueryBuilder),
2484 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`size_w` NOT BETWEEN 1 AND 10"#
2485 /// );
2486 /// assert_eq!(
2487 /// query.to_string(PostgresQueryBuilder),
2488 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" NOT BETWEEN 1 AND 10"#
2489 /// );
2490 /// assert_eq!(
2491 /// query.to_string(SqliteQueryBuilder),
2492 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" NOT BETWEEN 1 AND 10"#
2493 /// );
2494 /// ```
2495 pub fn not_between<V>(self, a: V, b: V) -> SimpleExpr
2496 where
2497 V: Into<SimpleExpr>,
2498 {
2499 ExprTrait::not_between(self, a, b)
2500 }
2501
2502 /// Express a `LIKE` expression.
2503 ///
2504 /// This is equivalent to a newer [ExprTrait::like] and may require more some wrapping beforehand.
2505 ///
2506 /// # Examples
2507 ///
2508 /// ```
2509 /// use sea_query::{*, tests_cfg::*};
2510 ///
2511 /// let query = Query::select()
2512 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
2513 /// .from(Char::Table)
2514 /// .and_where(Expr::col((Char::Table, Char::Character)).like("Ours'%"))
2515 /// .to_owned();
2516 ///
2517 /// assert_eq!(
2518 /// query.to_string(MysqlQueryBuilder),
2519 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`character` LIKE 'Ours\'%'"#
2520 /// );
2521 /// assert_eq!(
2522 /// query.to_string(PostgresQueryBuilder),
2523 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."character" LIKE E'Ours\'%'"#
2524 /// );
2525 /// assert_eq!(
2526 /// query.to_string(SqliteQueryBuilder),
2527 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."character" LIKE 'Ours''%'"#
2528 /// );
2529 /// ```
2530 ///
2531 /// Like with ESCAPE
2532 ///
2533 /// ```
2534 /// use sea_query::{*, tests_cfg::*};
2535 ///
2536 /// let query = Query::select()
2537 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
2538 /// .from(Char::Table)
2539 /// .and_where(Expr::col((Char::Table, Char::Character)).like(LikeExpr::new(r"|_Our|_").escape('|')))
2540 /// .to_owned();
2541 ///
2542 /// assert_eq!(
2543 /// query.to_string(MysqlQueryBuilder),
2544 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`character` LIKE '|_Our|_' ESCAPE '|'"#
2545 /// );
2546 /// assert_eq!(
2547 /// query.to_string(PostgresQueryBuilder),
2548 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."character" LIKE '|_Our|_' ESCAPE '|'"#
2549 /// );
2550 /// assert_eq!(
2551 /// query.to_string(SqliteQueryBuilder),
2552 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."character" LIKE '|_Our|_' ESCAPE '|'"#
2553 /// );
2554 /// ```
2555 pub fn like<L: IntoLikeExpr>(self, like: L) -> SimpleExpr {
2556 ExprTrait::like(self, like)
2557 }
2558
2559 /// Express a `NOT LIKE` expression.
2560 ///
2561 /// This is equivalent to a newer [ExprTrait::not_like] and may require more some wrapping beforehand.
2562 pub fn not_like<L: IntoLikeExpr>(self, like: L) -> SimpleExpr {
2563 ExprTrait::not_like(self, like)
2564 }
2565
2566 /// Express a `IS NULL` expression.
2567 ///
2568 /// This is equivalent to a newer [ExprTrait::is_null] and may require more some wrapping beforehand.
2569 ///
2570 /// # Examples
2571 ///
2572 /// ```
2573 /// use sea_query::{*, tests_cfg::*};
2574 ///
2575 /// let query = Query::select()
2576 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
2577 /// .from(Char::Table)
2578 /// .and_where(Expr::col((Char::Table, Char::SizeW)).is_null())
2579 /// .to_owned();
2580 ///
2581 /// assert_eq!(
2582 /// query.to_string(MysqlQueryBuilder),
2583 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`size_w` IS NULL"#
2584 /// );
2585 /// assert_eq!(
2586 /// query.to_string(PostgresQueryBuilder),
2587 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" IS NULL"#
2588 /// );
2589 /// assert_eq!(
2590 /// query.to_string(SqliteQueryBuilder),
2591 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" IS NULL"#
2592 /// );
2593 /// ```
2594 #[allow(clippy::wrong_self_convention)]
2595 pub fn is_null(self) -> SimpleExpr {
2596 ExprTrait::is_null(self)
2597 }
2598
2599 /// Express a `IS` expression.
2600 ///
2601 /// This is equivalent to a newer [ExprTrait::is] and may require more some wrapping beforehand.
2602 ///
2603 /// # Examples
2604 ///
2605 /// ```
2606 /// use sea_query::{*, tests_cfg::*};
2607 ///
2608 /// let query = Query::select()
2609 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
2610 /// .from(Char::Table)
2611 /// .and_where(Expr::col((Char::Table, Char::Ascii)).is(true))
2612 /// .to_owned();
2613 ///
2614 /// assert_eq!(
2615 /// query.to_string(MysqlQueryBuilder),
2616 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`ascii` IS TRUE"#
2617 /// );
2618 /// assert_eq!(
2619 /// query.to_string(PostgresQueryBuilder),
2620 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."ascii" IS TRUE"#
2621 /// );
2622 /// assert_eq!(
2623 /// query.to_string(SqliteQueryBuilder),
2624 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."ascii" IS TRUE"#
2625 /// );
2626 /// ```
2627 pub fn is<V>(self, v: V) -> SimpleExpr
2628 where
2629 V: Into<SimpleExpr>,
2630 {
2631 ExprTrait::is(self, v)
2632 }
2633
2634 /// Express a `IS NOT NULL` expression.
2635 ///
2636 /// This is equivalent to a newer [ExprTrait::is_not_null] and may require more some wrapping beforehand.
2637 ///
2638 /// # Examples
2639 ///
2640 /// ```
2641 /// use sea_query::{*, tests_cfg::*};
2642 ///
2643 /// let query = Query::select()
2644 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
2645 /// .from(Char::Table)
2646 /// .and_where(Expr::col((Char::Table, Char::SizeW)).is_not_null())
2647 /// .to_owned();
2648 ///
2649 /// assert_eq!(
2650 /// query.to_string(MysqlQueryBuilder),
2651 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`size_w` IS NOT NULL"#
2652 /// );
2653 /// assert_eq!(
2654 /// query.to_string(PostgresQueryBuilder),
2655 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" IS NOT NULL"#
2656 /// );
2657 /// assert_eq!(
2658 /// query.to_string(SqliteQueryBuilder),
2659 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" IS NOT NULL"#
2660 /// );
2661 /// ```
2662 #[allow(clippy::wrong_self_convention)]
2663 pub fn is_not_null(self) -> SimpleExpr {
2664 ExprTrait::is_not_null(self)
2665 }
2666
2667 /// Express a `IS NOT` expression.
2668 ///
2669 /// This is equivalent to a newer [ExprTrait::is_not] and may require more some wrapping beforehand.
2670 ///
2671 /// # Examples
2672 ///
2673 /// ```
2674 /// use sea_query::{*, tests_cfg::*};
2675 ///
2676 /// let query = Query::select()
2677 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
2678 /// .from(Char::Table)
2679 /// .and_where(Expr::col((Char::Table, Char::Ascii)).is_not(true))
2680 /// .to_owned();
2681 ///
2682 /// assert_eq!(
2683 /// query.to_string(MysqlQueryBuilder),
2684 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`ascii` IS NOT TRUE"#
2685 /// );
2686 /// assert_eq!(
2687 /// query.to_string(PostgresQueryBuilder),
2688 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."ascii" IS NOT TRUE"#
2689 /// );
2690 /// assert_eq!(
2691 /// query.to_string(SqliteQueryBuilder),
2692 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."ascii" IS NOT TRUE"#
2693 /// );
2694 /// ```
2695 pub fn is_not<V>(self, v: V) -> SimpleExpr
2696 where
2697 V: Into<SimpleExpr>,
2698 {
2699 ExprTrait::is_not(self, v)
2700 }
2701
2702 /// Create any binary operation
2703 ///
2704 /// This is equivalent to a newer [ExprTrait::binary] and may require more some wrapping beforehand.
2705 ///
2706 /// # Examples
2707 /// ```
2708 /// use sea_query::{*, tests_cfg::*};
2709 ///
2710 /// let query = Query::select()
2711 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
2712 /// .from(Char::Table)
2713 /// .cond_where(all![
2714 /// Expr::col(Char::SizeW).binary(BinOper::SmallerThan, 10),
2715 /// Expr::col(Char::SizeW).binary(BinOper::GreaterThan, Expr::col(Char::SizeH))
2716 /// ])
2717 /// .to_owned();
2718 /// assert_eq!(
2719 /// query.to_string(MysqlQueryBuilder),
2720 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `size_w` < 10 AND `size_w` > `size_h`"#
2721 /// );
2722 /// assert_eq!(
2723 /// query.to_string(PostgresQueryBuilder),
2724 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "size_w" < 10 AND "size_w" > "size_h""#
2725 /// );
2726 /// assert_eq!(
2727 /// query.to_string(SqliteQueryBuilder),
2728 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "size_w" < 10 AND "size_w" > "size_h""#
2729 /// );
2730 /// ```
2731 pub fn binary<O, T>(self, op: O, right: T) -> SimpleExpr
2732 where
2733 O: Into<BinOper>,
2734 T: Into<SimpleExpr>,
2735 {
2736 ExprTrait::binary(self, op, right)
2737 }
2738
2739 /// Negates an expression with `NOT`.
2740 ///
2741 /// This is equivalent to a newer [ExprTrait::not] and may require more some wrapping beforehand.
2742 ///
2743 /// # Examples
2744 ///
2745 /// ```
2746 /// use sea_query::{*, tests_cfg::*};
2747 ///
2748 /// let query = Query::select()
2749 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
2750 /// .from(Char::Table)
2751 /// // Before 0.32.0, you had call `not` on an `Expr`, which had to be constructed using `Expr::expr`:
2752 /// .and_where(Expr::expr(Expr::col((Char::Table, Char::SizeW)).is_null()).not())
2753 /// .to_owned();
2754 ///
2755 /// // But since 0.32.0, this compiles too:
2756 /// let _ = Expr::col((Char::Table, Char::SizeW)).is_null().not();
2757 ///
2758 /// assert_eq!(
2759 /// query.to_string(MysqlQueryBuilder),
2760 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE NOT `character`.`size_w` IS NULL"#
2761 /// );
2762 /// assert_eq!(
2763 /// query.to_string(PostgresQueryBuilder),
2764 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE NOT "character"."size_w" IS NULL"#
2765 /// );
2766 /// assert_eq!(
2767 /// query.to_string(SqliteQueryBuilder),
2768 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE NOT "character"."size_w" IS NULL"#
2769 /// );
2770 /// ```
2771 #[allow(clippy::should_implement_trait)]
2772 pub fn not(self) -> SimpleExpr {
2773 ExprTrait::not(self)
2774 }
2775
2776 /// Express a `MAX` function.
2777 ///
2778 /// # Examples
2779 ///
2780 /// ```
2781 /// use sea_query::{tests_cfg::*, *};
2782 ///
2783 /// let query = Query::select()
2784 /// .expr(Expr::col((Char::Table, Char::SizeW)).max())
2785 /// .from(Char::Table)
2786 /// .to_owned();
2787 ///
2788 /// assert_eq!(
2789 /// query.to_string(MysqlQueryBuilder),
2790 /// r#"SELECT MAX(`character`.`size_w`) FROM `character`"#
2791 /// );
2792 /// assert_eq!(
2793 /// query.to_string(PostgresQueryBuilder),
2794 /// r#"SELECT MAX("character"."size_w") FROM "character""#
2795 /// );
2796 /// assert_eq!(
2797 /// query.to_string(SqliteQueryBuilder),
2798 /// r#"SELECT MAX("character"."size_w") FROM "character""#
2799 /// );
2800 /// ```
2801 pub fn max(self) -> SimpleExpr {
2802 Func::max(self.left).into()
2803 }
2804
2805 /// Express a `MIN` function.
2806 ///
2807 /// # Examples
2808 ///
2809 /// ```
2810 /// use sea_query::{tests_cfg::*, *};
2811 ///
2812 /// let query = Query::select()
2813 /// .expr(Expr::col((Char::Table, Char::SizeW)).min())
2814 /// .from(Char::Table)
2815 /// .to_owned();
2816 ///
2817 /// assert_eq!(
2818 /// query.to_string(MysqlQueryBuilder),
2819 /// r#"SELECT MIN(`character`.`size_w`) FROM `character`"#
2820 /// );
2821 /// assert_eq!(
2822 /// query.to_string(PostgresQueryBuilder),
2823 /// r#"SELECT MIN("character"."size_w") FROM "character""#
2824 /// );
2825 /// assert_eq!(
2826 /// query.to_string(SqliteQueryBuilder),
2827 /// r#"SELECT MIN("character"."size_w") FROM "character""#
2828 /// );
2829 /// ```
2830 pub fn min(self) -> SimpleExpr {
2831 Func::min(self.left).into()
2832 }
2833
2834 /// Express a `SUM` function.
2835 ///
2836 /// # Examples
2837 ///
2838 /// ```
2839 /// use sea_query::{tests_cfg::*, *};
2840 ///
2841 /// let query = Query::select()
2842 /// .expr(Expr::col((Char::Table, Char::SizeW)).sum())
2843 /// .from(Char::Table)
2844 /// .to_owned();
2845 ///
2846 /// assert_eq!(
2847 /// query.to_string(MysqlQueryBuilder),
2848 /// r#"SELECT SUM(`character`.`size_w`) FROM `character`"#
2849 /// );
2850 /// assert_eq!(
2851 /// query.to_string(PostgresQueryBuilder),
2852 /// r#"SELECT SUM("character"."size_w") FROM "character""#
2853 /// );
2854 /// assert_eq!(
2855 /// query.to_string(SqliteQueryBuilder),
2856 /// r#"SELECT SUM("character"."size_w") FROM "character""#
2857 /// );
2858 /// ```
2859 pub fn sum(self) -> SimpleExpr {
2860 Func::sum(self.left).into()
2861 }
2862
2863 /// Express a `COUNT` function.
2864 ///
2865 /// # Examples
2866 ///
2867 /// ```
2868 /// use sea_query::{tests_cfg::*, *};
2869 ///
2870 /// let query = Query::select()
2871 /// .expr(Expr::col((Char::Table, Char::SizeW)).count())
2872 /// .from(Char::Table)
2873 /// .to_owned();
2874 ///
2875 /// assert_eq!(
2876 /// query.to_string(MysqlQueryBuilder),
2877 /// r#"SELECT COUNT(`character`.`size_w`) FROM `character`"#
2878 /// );
2879 /// assert_eq!(
2880 /// query.to_string(PostgresQueryBuilder),
2881 /// r#"SELECT COUNT("character"."size_w") FROM "character""#
2882 /// );
2883 /// assert_eq!(
2884 /// query.to_string(SqliteQueryBuilder),
2885 /// r#"SELECT COUNT("character"."size_w") FROM "character""#
2886 /// );
2887 /// ```
2888 pub fn count(self) -> SimpleExpr {
2889 Func::count(self.left).into()
2890 }
2891
2892 /// Express a `COUNT` function with the DISTINCT modifier.
2893 ///
2894 /// # Examples
2895 ///
2896 /// ```
2897 /// use sea_query::{tests_cfg::*, *};
2898 ///
2899 /// let query = Query::select()
2900 /// .expr(Expr::col((Char::Table, Char::SizeW)).count_distinct())
2901 /// .from(Char::Table)
2902 /// .to_owned();
2903 ///
2904 /// assert_eq!(
2905 /// query.to_string(MysqlQueryBuilder),
2906 /// r#"SELECT COUNT(DISTINCT `character`.`size_w`) FROM `character`"#
2907 /// );
2908 /// assert_eq!(
2909 /// query.to_string(PostgresQueryBuilder),
2910 /// r#"SELECT COUNT(DISTINCT "character"."size_w") FROM "character""#
2911 /// );
2912 /// assert_eq!(
2913 /// query.to_string(SqliteQueryBuilder),
2914 /// r#"SELECT COUNT(DISTINCT "character"."size_w") FROM "character""#
2915 /// );
2916 /// ```
2917 pub fn count_distinct(self) -> SimpleExpr {
2918 Func::count_distinct(self.left).into()
2919 }
2920
2921 /// Express a `IF NULL` function.
2922 ///
2923 /// # Examples
2924 ///
2925 /// ```
2926 /// use sea_query::{tests_cfg::*, *};
2927 ///
2928 /// let query = Query::select()
2929 /// .expr(Expr::col((Char::Table, Char::SizeW)).if_null(0))
2930 /// .from(Char::Table)
2931 /// .to_owned();
2932 ///
2933 /// assert_eq!(
2934 /// query.to_string(MysqlQueryBuilder),
2935 /// r#"SELECT IFNULL(`character`.`size_w`, 0) FROM `character`"#
2936 /// );
2937 /// assert_eq!(
2938 /// query.to_string(PostgresQueryBuilder),
2939 /// r#"SELECT COALESCE("character"."size_w", 0) FROM "character""#
2940 /// );
2941 /// assert_eq!(
2942 /// query.to_string(SqliteQueryBuilder),
2943 /// r#"SELECT IFNULL("character"."size_w", 0) FROM "character""#
2944 /// );
2945 /// ```
2946 pub fn if_null<V>(self, v: V) -> SimpleExpr
2947 where
2948 V: Into<SimpleExpr>,
2949 {
2950 Func::if_null(self.left, v).into()
2951 }
2952
2953 /// Express a `IN` expression.
2954 ///
2955 /// This is equivalent to a newer [ExprTrait::is_in] and may require more some wrapping beforehand.
2956 ///
2957 /// # Examples
2958 ///
2959 /// ```
2960 /// use sea_query::{tests_cfg::*, *};
2961 ///
2962 /// let query = Query::select()
2963 /// .columns([Char::Id])
2964 /// .from(Char::Table)
2965 /// .and_where(Expr::col((Char::Table, Char::SizeW)).is_in([1, 2, 3]))
2966 /// .to_owned();
2967 ///
2968 /// assert_eq!(
2969 /// query.to_string(MysqlQueryBuilder),
2970 /// r#"SELECT `id` FROM `character` WHERE `character`.`size_w` IN (1, 2, 3)"#
2971 /// );
2972 /// assert_eq!(
2973 /// query.to_string(PostgresQueryBuilder),
2974 /// r#"SELECT "id" FROM "character" WHERE "character"."size_w" IN (1, 2, 3)"#
2975 /// );
2976 /// assert_eq!(
2977 /// query.to_string(SqliteQueryBuilder),
2978 /// r#"SELECT "id" FROM "character" WHERE "character"."size_w" IN (1, 2, 3)"#
2979 /// );
2980 /// ```
2981 /// Empty value list
2982 /// ```
2983 /// use sea_query::{tests_cfg::*, *};
2984 ///
2985 /// let query = Query::select()
2986 /// .columns([Char::Id])
2987 /// .from(Char::Table)
2988 /// .and_where(Expr::col((Char::Table, Char::SizeW)).is_in(Vec::<u8>::new()))
2989 /// .to_owned();
2990 ///
2991 /// assert_eq!(
2992 /// query.to_string(MysqlQueryBuilder),
2993 /// r#"SELECT `id` FROM `character` WHERE 1 = 2"#
2994 /// );
2995 /// assert_eq!(
2996 /// query.to_string(PostgresQueryBuilder),
2997 /// r#"SELECT "id" FROM "character" WHERE 1 = 2"#
2998 /// );
2999 /// assert_eq!(
3000 /// query.to_string(SqliteQueryBuilder),
3001 /// r#"SELECT "id" FROM "character" WHERE 1 = 2"#
3002 /// );
3003 /// ```
3004 #[allow(clippy::wrong_self_convention)]
3005 pub fn is_in<V, I>(self, v: I) -> SimpleExpr
3006 where
3007 V: Into<SimpleExpr>,
3008 I: IntoIterator<Item = V>,
3009 {
3010 ExprTrait::is_in(self, v)
3011 }
3012
3013 /// Express a `IN` sub expression.
3014 ///
3015 /// This is equivalent to a newer [ExprTrait::in_tuples] and may require more some wrapping beforehand.
3016 ///
3017 /// # Examples
3018 ///
3019 /// ```
3020 /// use sea_query::{*, tests_cfg::*};
3021 ///
3022 /// let query = Query::select()
3023 /// .columns([Char::Character, Char::FontId])
3024 /// .from(Char::Table)
3025 /// .and_where(
3026 /// Expr::tuple([
3027 /// Expr::col(Char::Character).into(),
3028 /// Expr::col(Char::FontId).into(),
3029 /// ])
3030 /// .in_tuples([(1, String::from("1")), (2, String::from("2"))])
3031 /// )
3032 /// .to_owned();
3033 ///
3034 /// assert_eq!(
3035 /// query.to_string(MysqlQueryBuilder),
3036 /// r#"SELECT `character`, `font_id` FROM `character` WHERE (`character`, `font_id`) IN ((1, '1'), (2, '2'))"#
3037 /// );
3038 ///
3039 /// assert_eq!(
3040 /// query.to_string(PostgresQueryBuilder),
3041 /// r#"SELECT "character", "font_id" FROM "character" WHERE ("character", "font_id") IN ((1, '1'), (2, '2'))"#
3042 /// );
3043 ///
3044 /// assert_eq!(
3045 /// query.to_string(SqliteQueryBuilder),
3046 /// r#"SELECT "character", "font_id" FROM "character" WHERE ("character", "font_id") IN ((1, '1'), (2, '2'))"#
3047 /// );
3048 /// ```
3049 #[allow(clippy::wrong_self_convention)]
3050 pub fn in_tuples<V, I>(self, v: I) -> SimpleExpr
3051 where
3052 V: IntoValueTuple,
3053 I: IntoIterator<Item = V>,
3054 {
3055 ExprTrait::in_tuples(self, v)
3056 }
3057
3058 /// Express a `NOT IN` expression.
3059 ///
3060 /// This is equivalent to a newer [ExprTrait::is_not_in] and may require more some wrapping beforehand.
3061 ///
3062 /// # Examples
3063 ///
3064 /// ```
3065 /// use sea_query::{tests_cfg::*, *};
3066 ///
3067 /// let query = Query::select()
3068 /// .columns([Char::Id])
3069 /// .from(Char::Table)
3070 /// .and_where(Expr::col((Char::Table, Char::SizeW)).is_not_in([1, 2, 3]))
3071 /// .to_owned();
3072 ///
3073 /// assert_eq!(
3074 /// query.to_string(MysqlQueryBuilder),
3075 /// r#"SELECT `id` FROM `character` WHERE `character`.`size_w` NOT IN (1, 2, 3)"#
3076 /// );
3077 /// assert_eq!(
3078 /// query.to_string(PostgresQueryBuilder),
3079 /// r#"SELECT "id" FROM "character" WHERE "character"."size_w" NOT IN (1, 2, 3)"#
3080 /// );
3081 /// assert_eq!(
3082 /// query.to_string(SqliteQueryBuilder),
3083 /// r#"SELECT "id" FROM "character" WHERE "character"."size_w" NOT IN (1, 2, 3)"#
3084 /// );
3085 /// ```
3086 /// Empty value list
3087 /// ```
3088 /// use sea_query::{tests_cfg::*, *};
3089 ///
3090 /// let query = Query::select()
3091 /// .columns([Char::Id])
3092 /// .from(Char::Table)
3093 /// .and_where(Expr::col((Char::Table, Char::SizeW)).is_not_in(Vec::<u8>::new()))
3094 /// .to_owned();
3095 ///
3096 /// assert_eq!(
3097 /// query.to_string(MysqlQueryBuilder),
3098 /// r#"SELECT `id` FROM `character` WHERE 1 = 1"#
3099 /// );
3100 /// assert_eq!(
3101 /// query.to_string(PostgresQueryBuilder),
3102 /// r#"SELECT "id" FROM "character" WHERE 1 = 1"#
3103 /// );
3104 /// assert_eq!(
3105 /// query.to_string(SqliteQueryBuilder),
3106 /// r#"SELECT "id" FROM "character" WHERE 1 = 1"#
3107 /// );
3108 /// ```
3109 #[allow(clippy::wrong_self_convention)]
3110 pub fn is_not_in<V, I>(self, v: I) -> SimpleExpr
3111 where
3112 V: Into<SimpleExpr>,
3113 I: IntoIterator<Item = V>,
3114 {
3115 ExprTrait::is_not_in(self, v)
3116 }
3117
3118 /// Express a `IN` sub-query expression.
3119 ///
3120 /// This is equivalent to a newer [ExprTrait::in_subquery] and may require more some wrapping beforehand.
3121 ///
3122 /// # Examples
3123 ///
3124 /// ```
3125 /// use sea_query::{*, tests_cfg::*};
3126 ///
3127 /// let query = Query::select()
3128 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
3129 /// .from(Char::Table)
3130 /// .and_where(Expr::col(Char::SizeW).in_subquery(
3131 /// Query::select()
3132 /// .expr(Expr::cust("3 + 2 * 2"))
3133 /// .take()
3134 /// ))
3135 /// .to_owned();
3136 ///
3137 /// assert_eq!(
3138 /// query.to_string(MysqlQueryBuilder),
3139 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `size_w` IN (SELECT 3 + 2 * 2)"#
3140 /// );
3141 /// assert_eq!(
3142 /// query.to_string(PostgresQueryBuilder),
3143 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "size_w" IN (SELECT 3 + 2 * 2)"#
3144 /// );
3145 /// assert_eq!(
3146 /// query.to_string(SqliteQueryBuilder),
3147 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "size_w" IN (SELECT 3 + 2 * 2)"#
3148 /// );
3149 /// ```
3150 #[allow(clippy::wrong_self_convention)]
3151 pub fn in_subquery(self, sel: SelectStatement) -> SimpleExpr {
3152 ExprTrait::in_subquery(self, sel)
3153 }
3154
3155 /// Express a `NOT IN` sub-query expression.
3156 ///
3157 /// This is equivalent to a newer [ExprTrait::not_in_subquery] and may require more some wrapping beforehand.
3158 ///
3159 /// # Examples
3160 ///
3161 /// ```
3162 /// use sea_query::{*, tests_cfg::*};
3163 ///
3164 /// let query = Query::select()
3165 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
3166 /// .from(Char::Table)
3167 /// .and_where(Expr::col(Char::SizeW).not_in_subquery(
3168 /// Query::select()
3169 /// .expr(Expr::cust("3 + 2 * 2"))
3170 /// .take()
3171 /// ))
3172 /// .to_owned();
3173 ///
3174 /// assert_eq!(
3175 /// query.to_string(MysqlQueryBuilder),
3176 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `size_w` NOT IN (SELECT 3 + 2 * 2)"#
3177 /// );
3178 /// assert_eq!(
3179 /// query.to_string(PostgresQueryBuilder),
3180 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "size_w" NOT IN (SELECT 3 + 2 * 2)"#
3181 /// );
3182 /// assert_eq!(
3183 /// query.to_string(SqliteQueryBuilder),
3184 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "size_w" NOT IN (SELECT 3 + 2 * 2)"#
3185 /// );
3186 /// ```
3187 #[allow(clippy::wrong_self_convention)]
3188 pub fn not_in_subquery(self, sel: SelectStatement) -> SimpleExpr {
3189 ExprTrait::not_in_subquery(self, sel)
3190 }
3191
3192 /// Express a `EXISTS` sub-query expression.
3193 ///
3194 /// # Examples
3195 ///
3196 /// ```
3197 /// use sea_query::{*, tests_cfg::*};
3198 ///
3199 /// let query = Query::select()
3200 /// .expr_as(Expr::exists(Query::select().column(Char::Id).from(Char::Table).take()), "character_exists")
3201 /// .expr_as(Expr::exists(Query::select().column(Glyph::Id).from(Glyph::Table).take()), "glyph_exists")
3202 /// .to_owned();
3203 ///
3204 /// assert_eq!(
3205 /// query.to_string(MysqlQueryBuilder),
3206 /// r#"SELECT EXISTS(SELECT `id` FROM `character`) AS `character_exists`, EXISTS(SELECT `id` FROM `glyph`) AS `glyph_exists`"#
3207 /// );
3208 /// assert_eq!(
3209 /// query.to_string(PostgresQueryBuilder),
3210 /// r#"SELECT EXISTS(SELECT "id" FROM "character") AS "character_exists", EXISTS(SELECT "id" FROM "glyph") AS "glyph_exists""#
3211 /// );
3212 /// assert_eq!(
3213 /// query.to_string(SqliteQueryBuilder),
3214 /// r#"SELECT EXISTS(SELECT "id" FROM "character") AS "character_exists", EXISTS(SELECT "id" FROM "glyph") AS "glyph_exists""#
3215 /// );
3216 /// ```
3217 pub fn exists(sel: SelectStatement) -> SimpleExpr {
3218 SimpleExpr::SubQuery(
3219 Some(SubQueryOper::Exists),
3220 Box::new(sel.into_sub_query_statement()),
3221 )
3222 }
3223
3224 /// Express a `ANY` sub-query expression.
3225 ///
3226 /// # Examples
3227 ///
3228 /// ```
3229 /// use sea_query::{tests_cfg::*, *};
3230 ///
3231 /// let query = Query::select()
3232 /// .column(Char::Id)
3233 /// .from(Char::Table)
3234 /// .and_where(Expr::col(Char::Id).eq(Expr::any(
3235 /// Query::select().column(Char::Id).from(Char::Table).take(),
3236 /// )))
3237 /// .to_owned();
3238 ///
3239 /// assert_eq!(
3240 /// query.to_string(MysqlQueryBuilder),
3241 /// r#"SELECT `id` FROM `character` WHERE `id` = ANY(SELECT `id` FROM `character`)"#
3242 /// );
3243 /// assert_eq!(
3244 /// query.to_string(PostgresQueryBuilder),
3245 /// r#"SELECT "id" FROM "character" WHERE "id" = ANY(SELECT "id" FROM "character")"#
3246 /// );
3247 /// ```
3248 pub fn any(sel: SelectStatement) -> SimpleExpr {
3249 SimpleExpr::SubQuery(
3250 Some(SubQueryOper::Any),
3251 Box::new(sel.into_sub_query_statement()),
3252 )
3253 }
3254
3255 /// Express a `SOME` sub-query expression.
3256 ///
3257 /// # Examples
3258 ///
3259 /// ```
3260 /// use sea_query::{tests_cfg::*, *};
3261 ///
3262 /// let query = Query::select()
3263 /// .column(Char::Id)
3264 /// .from(Char::Table)
3265 /// .and_where(Expr::col(Char::Id).ne(Expr::some(
3266 /// Query::select().column(Char::Id).from(Char::Table).take(),
3267 /// )))
3268 /// .to_owned();
3269 ///
3270 /// assert_eq!(
3271 /// query.to_string(MysqlQueryBuilder),
3272 /// r#"SELECT `id` FROM `character` WHERE `id` <> SOME(SELECT `id` FROM `character`)"#
3273 /// );
3274 /// assert_eq!(
3275 /// query.to_string(PostgresQueryBuilder),
3276 /// r#"SELECT "id" FROM "character" WHERE "id" <> SOME(SELECT "id" FROM "character")"#
3277 /// );
3278 /// ```
3279 pub fn some(sel: SelectStatement) -> SimpleExpr {
3280 SimpleExpr::SubQuery(
3281 Some(SubQueryOper::Some),
3282 Box::new(sel.into_sub_query_statement()),
3283 )
3284 }
3285
3286 /// Express a `ALL` sub-query expression.
3287 pub fn all(sel: SelectStatement) -> SimpleExpr {
3288 SimpleExpr::SubQuery(
3289 Some(SubQueryOper::All),
3290 Box::new(sel.into_sub_query_statement()),
3291 )
3292 }
3293
3294 /// Express a `AS enum` expression.
3295 ///
3296 /// This is equivalent to a newer [ExprTrait::as_enum] and may require more some wrapping beforehand.
3297 ///
3298 /// # Examples
3299 ///
3300 /// ```
3301 /// use sea_query::{tests_cfg::*, *};
3302 ///
3303 /// let query = Query::select()
3304 /// .expr(Expr::col(Char::FontSize).as_enum("text"))
3305 /// .from(Char::Table)
3306 /// .to_owned();
3307 ///
3308 /// assert_eq!(
3309 /// query.to_string(MysqlQueryBuilder),
3310 /// r#"SELECT `font_size` FROM `character`"#
3311 /// );
3312 /// assert_eq!(
3313 /// query.to_string(PostgresQueryBuilder),
3314 /// r#"SELECT CAST("font_size" AS "text") FROM "character""#
3315 /// );
3316 /// assert_eq!(
3317 /// query.to_string(SqliteQueryBuilder),
3318 /// r#"SELECT "font_size" FROM "character""#
3319 /// );
3320 ///
3321 /// struct TextArray;
3322 ///
3323 /// impl Iden for TextArray {
3324 /// fn unquoted(&self, s: &mut dyn std::fmt::Write) {
3325 /// write!(s, "text[]").unwrap();
3326 /// }
3327 /// }
3328 ///
3329 /// let query = Query::select()
3330 /// .expr(Expr::col(Char::FontSize).as_enum(TextArray))
3331 /// .from(Char::Table)
3332 /// .to_owned();
3333 ///
3334 /// assert_eq!(
3335 /// query.to_string(MysqlQueryBuilder),
3336 /// r#"SELECT `font_size` FROM `character`"#
3337 /// );
3338 /// assert_eq!(
3339 /// query.to_string(PostgresQueryBuilder),
3340 /// r#"SELECT CAST("font_size" AS "text"[]) FROM "character""#
3341 /// );
3342 /// assert_eq!(
3343 /// query.to_string(SqliteQueryBuilder),
3344 /// r#"SELECT "font_size" FROM "character""#
3345 /// );
3346 ///
3347 /// let query = Query::insert()
3348 /// .into_table(Char::Table)
3349 /// .columns([Char::FontSize])
3350 /// .values_panic([Expr::val("large").as_enum("FontSizeEnum")])
3351 /// .to_owned();
3352 ///
3353 /// assert_eq!(
3354 /// query.to_string(MysqlQueryBuilder),
3355 /// r#"INSERT INTO `character` (`font_size`) VALUES ('large')"#
3356 /// );
3357 /// assert_eq!(
3358 /// query.to_string(PostgresQueryBuilder),
3359 /// r#"INSERT INTO "character" ("font_size") VALUES (CAST('large' AS "FontSizeEnum"))"#
3360 /// );
3361 /// assert_eq!(
3362 /// query.to_string(SqliteQueryBuilder),
3363 /// r#"INSERT INTO "character" ("font_size") VALUES ('large')"#
3364 /// );
3365 /// ```
3366 pub fn as_enum<T>(self, type_name: T) -> SimpleExpr
3367 where
3368 T: IntoIden,
3369 {
3370 ExprTrait::as_enum(self, type_name)
3371 }
3372
3373 /// Adds new `CASE WHEN` to existing case statement.
3374 ///
3375 /// # Examples
3376 ///
3377 /// ```
3378 /// use sea_query::{*, tests_cfg::*};
3379 ///
3380 /// let query = Query::select()
3381 /// .expr_as(
3382 /// Expr::case(
3383 /// Expr::col((Glyph::Table, Glyph::Aspect)).is_in([2, 4]),
3384 /// true
3385 /// )
3386 /// .finally(false),
3387 /// "is_even"
3388 /// )
3389 /// .from(Glyph::Table)
3390 /// .to_owned();
3391 ///
3392 /// assert_eq!(
3393 /// query.to_string(PostgresQueryBuilder),
3394 /// r#"SELECT (CASE WHEN ("glyph"."aspect" IN (2, 4)) THEN TRUE ELSE FALSE END) AS "is_even" FROM "glyph""#
3395 /// );
3396 /// ```
3397 pub fn case<C, T>(cond: C, then: T) -> CaseStatement
3398 where
3399 C: IntoCondition,
3400 T: Into<SimpleExpr>,
3401 {
3402 CaseStatement::new().case(cond, then)
3403 }
3404
3405 /// Express a `CAST AS` expression.
3406 ///
3407 /// This is equivalent to a newer [ExprTrait::cast_as] and may require more some wrapping beforehand.
3408 ///
3409 /// # Examples
3410 ///
3411 /// ```
3412 /// use sea_query::{tests_cfg::*, *};
3413 ///
3414 /// let query = Query::select()
3415 /// .expr(Expr::val("1").cast_as("integer"))
3416 /// .to_owned();
3417 ///
3418 /// assert_eq!(
3419 /// query.to_string(MysqlQueryBuilder),
3420 /// r#"SELECT CAST('1' AS integer)"#
3421 /// );
3422 /// assert_eq!(
3423 /// query.to_string(PostgresQueryBuilder),
3424 /// r#"SELECT CAST('1' AS integer)"#
3425 /// );
3426 /// assert_eq!(
3427 /// query.to_string(SqliteQueryBuilder),
3428 /// r#"SELECT CAST('1' AS integer)"#
3429 /// );
3430 /// ```
3431 pub fn cast_as<T>(self, type_name: T) -> SimpleExpr
3432 where
3433 T: IntoIden,
3434 {
3435 ExprTrait::cast_as(self, type_name)
3436 }
3437
3438 /// Keyword `CURRENT_DATE`.
3439 ///
3440 /// # Examples
3441 ///
3442 /// ```
3443 /// use sea_query::*;
3444 ///
3445 /// let query = Query::select().expr(Expr::current_date()).to_owned();
3446 ///
3447 /// assert_eq!(query.to_string(MysqlQueryBuilder), r#"SELECT CURRENT_DATE"#);
3448 /// assert_eq!(
3449 /// query.to_string(PostgresQueryBuilder),
3450 /// r#"SELECT CURRENT_DATE"#
3451 /// );
3452 /// assert_eq!(
3453 /// query.to_string(SqliteQueryBuilder),
3454 /// r#"SELECT CURRENT_DATE"#
3455 /// );
3456 /// ```
3457 pub fn current_date() -> Expr {
3458 Expr::new_with_left(Keyword::CurrentDate)
3459 }
3460
3461 /// Keyword `CURRENT_TIMESTAMP`.
3462 ///
3463 /// # Examples
3464 ///
3465 /// ```
3466 /// use sea_query::*;
3467 ///
3468 /// let query = Query::select().expr(Expr::current_time()).to_owned();
3469 ///
3470 /// assert_eq!(query.to_string(MysqlQueryBuilder), r#"SELECT CURRENT_TIME"#);
3471 /// assert_eq!(
3472 /// query.to_string(PostgresQueryBuilder),
3473 /// r#"SELECT CURRENT_TIME"#
3474 /// );
3475 /// assert_eq!(
3476 /// query.to_string(SqliteQueryBuilder),
3477 /// r#"SELECT CURRENT_TIME"#
3478 /// );
3479 /// ```
3480 pub fn current_time() -> Expr {
3481 Expr::new_with_left(Keyword::CurrentTime)
3482 }
3483
3484 /// Keyword `CURRENT_TIMESTAMP`.
3485 ///
3486 /// # Examples
3487 ///
3488 /// ```
3489 /// use sea_query::{Expr, MysqlQueryBuilder, PostgresQueryBuilder, Query, SqliteQueryBuilder};
3490 ///
3491 /// let query = Query::select().expr(Expr::current_timestamp()).to_owned();
3492 ///
3493 /// assert_eq!(
3494 /// query.to_string(MysqlQueryBuilder),
3495 /// r#"SELECT CURRENT_TIMESTAMP"#
3496 /// );
3497 /// assert_eq!(
3498 /// query.to_string(PostgresQueryBuilder),
3499 /// r#"SELECT CURRENT_TIMESTAMP"#
3500 /// );
3501 /// assert_eq!(
3502 /// query.to_string(SqliteQueryBuilder),
3503 /// r#"SELECT CURRENT_TIMESTAMP"#
3504 /// );
3505 /// ```
3506 pub fn current_timestamp() -> Expr {
3507 Expr::new_with_left(Keyword::CurrentTimestamp)
3508 }
3509
3510 /// Custom keyword.
3511 ///
3512 /// # Examples
3513 ///
3514 /// ```
3515 /// use sea_query::*;
3516 ///
3517 /// let query = Query::select()
3518 /// .expr(Expr::custom_keyword("test"))
3519 /// .to_owned();
3520 ///
3521 /// assert_eq!(query.to_string(MysqlQueryBuilder), r#"SELECT test"#);
3522 /// assert_eq!(query.to_string(PostgresQueryBuilder), r#"SELECT test"#);
3523 /// assert_eq!(query.to_string(SqliteQueryBuilder), r#"SELECT test"#);
3524 /// ```
3525 pub fn custom_keyword<T>(i: T) -> Expr
3526 where
3527 T: IntoIden,
3528 {
3529 Expr::new_with_left(Keyword::Custom(i.into_iden()))
3530 }
3531}
3532
3533impl From<Expr> for SimpleExpr {
3534 /// Convert into SimpleExpr
3535 fn from(src: Expr) -> Self {
3536 if let Some(uopr) = src.uopr {
3537 SimpleExpr::Unary(uopr, Box::new(src.left))
3538 } else if let Some(bopr) = src.bopr {
3539 SimpleExpr::Binary(Box::new(src.left), bopr, Box::new(src.right.unwrap()))
3540 } else {
3541 src.left
3542 }
3543 }
3544}
3545
3546impl<T> From<T> for SimpleExpr
3547where
3548 T: Into<Value>,
3549{
3550 fn from(v: T) -> Self {
3551 SimpleExpr::Value(v.into())
3552 }
3553}
3554
3555impl From<FunctionCall> for SimpleExpr {
3556 fn from(func: FunctionCall) -> Self {
3557 SimpleExpr::FunctionCall(func)
3558 }
3559}
3560
3561impl From<ColumnRef> for SimpleExpr {
3562 fn from(col: ColumnRef) -> Self {
3563 SimpleExpr::Column(col)
3564 }
3565}
3566
3567impl From<Keyword> for SimpleExpr {
3568 fn from(k: Keyword) -> Self {
3569 SimpleExpr::Keyword(k)
3570 }
3571}
3572
3573impl From<LikeExpr> for SimpleExpr {
3574 fn from(like: LikeExpr) -> Self {
3575 match like.escape {
3576 Some(escape) => SimpleExpr::Binary(
3577 Box::new(like.pattern.into()),
3578 BinOper::Escape,
3579 Box::new(SimpleExpr::Constant(escape.into())),
3580 ),
3581 None => like.pattern.into(),
3582 }
3583 }
3584}
3585
3586impl SimpleExpr {
3587 /// Negates an expression with `NOT`.
3588 ///
3589 /// This is equivalent to a newer [ExprTrait::not] and may require more some wrapping beforehand.
3590 ///
3591 /// # Examples
3592 ///
3593 /// ```
3594 /// use sea_query::{tests_cfg::*, *};
3595 ///
3596 /// let query = Query::select()
3597 /// .column(Char::SizeW)
3598 /// .from(Char::Table)
3599 /// .and_where(Expr::col(Char::SizeW).eq(1).not())
3600 /// .to_owned();
3601 ///
3602 /// assert_eq!(
3603 /// query.to_string(MysqlQueryBuilder),
3604 /// r#"SELECT `size_w` FROM `character` WHERE NOT `size_w` = 1"#
3605 /// );
3606 /// assert_eq!(
3607 /// query.to_string(PostgresQueryBuilder),
3608 /// r#"SELECT "size_w" FROM "character" WHERE NOT "size_w" = 1"#
3609 /// );
3610 /// assert_eq!(
3611 /// query.to_string(SqliteQueryBuilder),
3612 /// r#"SELECT "size_w" FROM "character" WHERE NOT "size_w" = 1"#
3613 /// );
3614 /// ```
3615 #[allow(clippy::should_implement_trait)]
3616 pub fn not(self) -> SimpleExpr {
3617 ExprTrait::not(self)
3618 }
3619
3620 /// Express a logical `AND` operation.
3621 ///
3622 /// # Examples
3623 ///
3624 /// ```
3625 /// use sea_query::{*, tests_cfg::*};
3626 ///
3627 /// let query = Query::select()
3628 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
3629 /// .from(Char::Table)
3630 /// .cond_where(any![
3631 /// Expr::col(Char::SizeW).eq(1).and(Expr::col(Char::SizeH).eq(2)),
3632 /// Expr::col(Char::SizeW).eq(3).and(Expr::col(Char::SizeH).eq(4)),
3633 /// ])
3634 /// .to_owned();
3635 ///
3636 /// assert_eq!(
3637 /// query.to_string(MysqlQueryBuilder),
3638 /// 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)"#
3639 /// );
3640 /// assert_eq!(
3641 /// query.to_string(PostgresQueryBuilder),
3642 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE ("size_w" = 1 AND "size_h" = 2) OR ("size_w" = 3 AND "size_h" = 4)"#
3643 /// );
3644 /// assert_eq!(
3645 /// query.to_string(SqliteQueryBuilder),
3646 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE ("size_w" = 1 AND "size_h" = 2) OR ("size_w" = 3 AND "size_h" = 4)"#
3647 /// );
3648 /// ```
3649 pub fn and(self, right: SimpleExpr) -> Self {
3650 ExprTrait::and(self, right)
3651 }
3652
3653 /// Express a logical `OR` operation.
3654 ///
3655 /// This is equivalent to a newer [ExprTrait::or] and may require more some wrapping beforehand.
3656 ///
3657 /// # Examples
3658 ///
3659 /// ```
3660 /// use sea_query::{*, tests_cfg::*};
3661 ///
3662 /// let query = Query::select()
3663 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
3664 /// .from(Char::Table)
3665 /// .and_where(Expr::col(Char::SizeW).eq(1).or(Expr::col(Char::SizeH).eq(2)))
3666 /// .and_where(Expr::col(Char::SizeW).eq(3).or(Expr::col(Char::SizeH).eq(4)))
3667 /// .to_owned();
3668 ///
3669 /// assert_eq!(
3670 /// query.to_string(MysqlQueryBuilder),
3671 /// 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)"#
3672 /// );
3673 /// assert_eq!(
3674 /// query.to_string(PostgresQueryBuilder),
3675 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE ("size_w" = 1 OR "size_h" = 2) AND ("size_w" = 3 OR "size_h" = 4)"#
3676 /// );
3677 /// assert_eq!(
3678 /// query.to_string(SqliteQueryBuilder),
3679 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE ("size_w" = 1 OR "size_h" = 2) AND ("size_w" = 3 OR "size_h" = 4)"#
3680 /// );
3681 /// ```
3682 pub fn or(self, right: SimpleExpr) -> Self {
3683 ExprTrait::or(self, right)
3684 }
3685
3686 /// Express an equal (`=`) expression.
3687 ///
3688 /// This is equivalent to a newer [ExprTrait::eq] and may require more some wrapping beforehand.
3689 ///
3690 /// # Examples
3691 ///
3692 /// ```
3693 /// use sea_query::{tests_cfg::*, *};
3694 ///
3695 /// let query = Query::select()
3696 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
3697 /// .from(Char::Table)
3698 /// .and_where(Expr::value("What!").eq("Nothing"))
3699 /// .to_owned();
3700 ///
3701 /// assert_eq!(
3702 /// query.to_string(MysqlQueryBuilder),
3703 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE 'What!' = 'Nothing'"#
3704 /// );
3705 /// assert_eq!(
3706 /// query.to_string(PostgresQueryBuilder),
3707 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 'What!' = 'Nothing'"#
3708 /// );
3709 /// assert_eq!(
3710 /// query.to_string(SqliteQueryBuilder),
3711 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 'What!' = 'Nothing'"#
3712 /// );
3713 /// ```
3714 pub fn eq<V>(self, v: V) -> SimpleExpr
3715 where
3716 V: Into<SimpleExpr>,
3717 {
3718 ExprTrait::eq(self, v)
3719 }
3720
3721 /// Express a not equal (`<>`) expression.
3722 ///
3723 /// This is equivalent to a newer [ExprTrait::ne] and may require more some wrapping beforehand.
3724 ///
3725 /// # Examples
3726 ///
3727 /// ```
3728 /// use sea_query::{tests_cfg::*, *};
3729 ///
3730 /// let query = Query::select()
3731 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
3732 /// .from(Char::Table)
3733 /// .and_where(Expr::value("Morning").ne("Good"))
3734 /// .to_owned();
3735 ///
3736 /// assert_eq!(
3737 /// query.to_string(MysqlQueryBuilder),
3738 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE 'Morning' <> 'Good'"#
3739 /// );
3740 /// assert_eq!(
3741 /// query.to_string(PostgresQueryBuilder),
3742 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 'Morning' <> 'Good'"#
3743 /// );
3744 /// assert_eq!(
3745 /// query.to_string(SqliteQueryBuilder),
3746 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 'Morning' <> 'Good'"#
3747 /// );
3748 /// ```
3749 pub fn ne<V>(self, v: V) -> SimpleExpr
3750 where
3751 V: Into<SimpleExpr>,
3752 {
3753 ExprTrait::ne(self, v)
3754 }
3755
3756 /// Perform addition with another [`SimpleExpr`].
3757 ///
3758 /// This is equivalent to a newer [ExprTrait::add] and may require more some wrapping beforehand.
3759 ///
3760 /// # Examples
3761 ///
3762 /// ```
3763 /// use sea_query::{tests_cfg::*, *};
3764 ///
3765 /// let query = Query::select()
3766 /// .expr(
3767 /// Expr::col(Char::SizeW)
3768 /// .max()
3769 /// .add(Expr::col(Char::SizeH).max()),
3770 /// )
3771 /// .from(Char::Table)
3772 /// .to_owned();
3773 ///
3774 /// assert_eq!(
3775 /// query.to_string(MysqlQueryBuilder),
3776 /// r#"SELECT MAX(`size_w`) + MAX(`size_h`) FROM `character`"#
3777 /// );
3778 /// assert_eq!(
3779 /// query.to_string(PostgresQueryBuilder),
3780 /// r#"SELECT MAX("size_w") + MAX("size_h") FROM "character""#
3781 /// );
3782 /// assert_eq!(
3783 /// query.to_string(SqliteQueryBuilder),
3784 /// r#"SELECT MAX("size_w") + MAX("size_h") FROM "character""#
3785 /// );
3786 /// ```
3787 #[allow(clippy::should_implement_trait)]
3788 pub fn add<T>(self, right: T) -> Self
3789 where
3790 T: Into<SimpleExpr>,
3791 {
3792 ExprTrait::add(self, right)
3793 }
3794
3795 /// Perform multiplication with another [`SimpleExpr`].
3796 ///
3797 /// This is equivalent to a newer [ExprTrait::mul] and may require more some wrapping beforehand.
3798 ///
3799 /// # Examples
3800 ///
3801 /// ```
3802 /// use sea_query::{tests_cfg::*, *};
3803 ///
3804 /// let query = Query::select()
3805 /// .expr(
3806 /// Expr::col(Char::SizeW)
3807 /// .max()
3808 /// .mul(Expr::col(Char::SizeH).max()),
3809 /// )
3810 /// .from(Char::Table)
3811 /// .to_owned();
3812 ///
3813 /// assert_eq!(
3814 /// query.to_string(MysqlQueryBuilder),
3815 /// r#"SELECT MAX(`size_w`) * MAX(`size_h`) FROM `character`"#
3816 /// );
3817 /// assert_eq!(
3818 /// query.to_string(PostgresQueryBuilder),
3819 /// r#"SELECT MAX("size_w") * MAX("size_h") FROM "character""#
3820 /// );
3821 /// assert_eq!(
3822 /// query.to_string(SqliteQueryBuilder),
3823 /// r#"SELECT MAX("size_w") * MAX("size_h") FROM "character""#
3824 /// );
3825 /// ```
3826 #[allow(clippy::should_implement_trait)]
3827 pub fn mul<T>(self, right: T) -> Self
3828 where
3829 T: Into<SimpleExpr>,
3830 {
3831 ExprTrait::mul(self, right)
3832 }
3833
3834 /// Perform division with another [`SimpleExpr`].
3835 ///
3836 /// This is equivalent to a newer [ExprTrait::div] and may require more some wrapping beforehand.
3837 ///
3838 /// # Examples
3839 ///
3840 /// ```
3841 /// use sea_query::{tests_cfg::*, *};
3842 ///
3843 /// let query = Query::select()
3844 /// .expr(
3845 /// Expr::col(Char::SizeW)
3846 /// .max()
3847 /// .div(Expr::col(Char::SizeH).max()),
3848 /// )
3849 /// .from(Char::Table)
3850 /// .to_owned();
3851 ///
3852 /// assert_eq!(
3853 /// query.to_string(MysqlQueryBuilder),
3854 /// r#"SELECT MAX(`size_w`) / MAX(`size_h`) FROM `character`"#
3855 /// );
3856 /// assert_eq!(
3857 /// query.to_string(PostgresQueryBuilder),
3858 /// r#"SELECT MAX("size_w") / MAX("size_h") FROM "character""#
3859 /// );
3860 /// assert_eq!(
3861 /// query.to_string(SqliteQueryBuilder),
3862 /// r#"SELECT MAX("size_w") / MAX("size_h") FROM "character""#
3863 /// );
3864 /// ```
3865 #[allow(clippy::should_implement_trait)]
3866 pub fn div<T>(self, right: T) -> Self
3867 where
3868 T: Into<SimpleExpr>,
3869 {
3870 ExprTrait::div(self, right)
3871 }
3872
3873 /// Perform subtraction with another [`SimpleExpr`].
3874 ///
3875 /// This is equivalent to a newer [ExprTrait::sub] and may require more some wrapping beforehand.
3876 ///
3877 /// # Examples
3878 ///
3879 /// ```
3880 /// use sea_query::{tests_cfg::*, *};
3881 ///
3882 /// let query = Query::select()
3883 /// .expr(
3884 /// Expr::col(Char::SizeW)
3885 /// .max()
3886 /// .sub(Expr::col(Char::SizeW).min()),
3887 /// )
3888 /// .from(Char::Table)
3889 /// .to_owned();
3890 ///
3891 /// assert_eq!(
3892 /// query.to_string(MysqlQueryBuilder),
3893 /// r#"SELECT MAX(`size_w`) - MIN(`size_w`) FROM `character`"#
3894 /// );
3895 /// assert_eq!(
3896 /// query.to_string(PostgresQueryBuilder),
3897 /// r#"SELECT MAX("size_w") - MIN("size_w") FROM "character""#
3898 /// );
3899 /// assert_eq!(
3900 /// query.to_string(SqliteQueryBuilder),
3901 /// r#"SELECT MAX("size_w") - MIN("size_w") FROM "character""#
3902 /// );
3903 /// ```
3904 #[allow(clippy::should_implement_trait)]
3905 pub fn sub<T>(self, right: T) -> Self
3906 where
3907 T: Into<SimpleExpr>,
3908 {
3909 ExprTrait::sub(self, right)
3910 }
3911
3912 /// Express a `CAST AS` expression.
3913 ///
3914 /// This is equivalent to a newer [ExprTrait::cast_as] and may require more some wrapping beforehand.
3915 ///
3916 /// # Examples
3917 ///
3918 /// ```
3919 /// use sea_query::{tests_cfg::*, *};
3920 ///
3921 /// let query = Query::select()
3922 /// .expr(Expr::value("1").cast_as("integer"))
3923 /// .to_owned();
3924 ///
3925 /// assert_eq!(
3926 /// query.to_string(MysqlQueryBuilder),
3927 /// r#"SELECT CAST('1' AS integer)"#
3928 /// );
3929 /// assert_eq!(
3930 /// query.to_string(PostgresQueryBuilder),
3931 /// r#"SELECT CAST('1' AS integer)"#
3932 /// );
3933 /// assert_eq!(
3934 /// query.to_string(SqliteQueryBuilder),
3935 /// r#"SELECT CAST('1' AS integer)"#
3936 /// );
3937 /// ```
3938 pub fn cast_as<T>(self, type_name: T) -> Self
3939 where
3940 T: IntoIden,
3941 {
3942 ExprTrait::cast_as(self, type_name)
3943 }
3944
3945 /// Soft deprecated. This is not meant to be in the public API.
3946 #[doc(hidden)]
3947 pub fn cast_as_quoted<T>(self, type_name: T, q: Quote) -> Self
3948 where
3949 T: IntoIden,
3950 {
3951 let func = Func::cast_as_quoted(self, type_name, q);
3952 Self::FunctionCall(func)
3953 }
3954
3955 /// Create any binary operation
3956 ///
3957 /// This is equivalent to a newer [ExprTrait::add] and may require more some wrapping beforehand.
3958 ///
3959 /// # Examples
3960 /// ```
3961 /// use sea_query::{*, tests_cfg::*};
3962 ///
3963 /// let query = Query::select()
3964 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
3965 /// .from(Char::Table)
3966 /// .cond_where(all![
3967 /// Expr::value(10).binary(BinOper::SmallerThan, Expr::col(Char::SizeW)),
3968 /// Expr::value(20).binary(BinOper::GreaterThan, Expr::col(Char::SizeH))
3969 /// ])
3970 /// .to_owned();
3971 /// assert_eq!(
3972 /// query.to_string(MysqlQueryBuilder),
3973 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE 10 < `size_w` AND 20 > `size_h`"#
3974 /// );
3975 /// assert_eq!(
3976 /// query.to_string(PostgresQueryBuilder),
3977 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 10 < "size_w" AND 20 > "size_h""#
3978 /// );
3979 /// assert_eq!(
3980 /// query.to_string(SqliteQueryBuilder),
3981 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 10 < "size_w" AND 20 > "size_h""#
3982 /// );
3983 pub fn binary<O, T>(self, op: O, right: T) -> Self
3984 where
3985 O: Into<BinOper>,
3986 T: Into<SimpleExpr>,
3987 {
3988 ExprTrait::binary(self, op, right)
3989 }
3990
3991 /// Express a `LIKE` expression.
3992 ///
3993 /// This is equivalent to a newer [ExprTrait::like] and may require more some wrapping beforehand.
3994 ///
3995 /// # Examples
3996 ///
3997 /// ```
3998 /// use sea_query::{*, tests_cfg::*};
3999 ///
4000 /// let query = Query::select()
4001 /// .columns([Char::Character, Char::SizeW, Char::SizeH])
4002 /// .from(Char::Table)
4003 /// .and_where(Expr::col((Char::Table, Char::FontId)).cast_as("TEXT").like("a%"))
4004 /// .to_owned();
4005 ///
4006 /// assert_eq!(
4007 /// query.to_string(MysqlQueryBuilder),
4008 /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE CAST(`character`.`font_id` AS TEXT) LIKE 'a%'"#
4009 /// );
4010 /// assert_eq!(
4011 /// query.to_string(PostgresQueryBuilder),
4012 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE CAST("character"."font_id" AS TEXT) LIKE 'a%'"#
4013 /// );
4014 /// assert_eq!(
4015 /// query.to_string(SqliteQueryBuilder),
4016 /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE CAST("character"."font_id" AS TEXT) LIKE 'a%'"#
4017 /// );
4018 /// ```
4019 pub fn like<L: IntoLikeExpr>(self, like: L) -> Self {
4020 ExprTrait::like(self, like)
4021 }
4022
4023 /// Express a `NOT LIKE` expression.
4024 ///
4025 /// This is equivalent to a newer [ExprTrait::not_like] and may require more some wrapping beforehand.
4026 pub fn not_like<L: IntoLikeExpr>(self, like: L) -> Self {
4027 ExprTrait::not_like(self, like)
4028 }
4029
4030 pub(crate) fn is_binary(&self) -> bool {
4031 matches!(self, Self::Binary(_, _, _))
4032 }
4033
4034 pub(crate) fn get_bin_oper(&self) -> Option<&BinOper> {
4035 match self {
4036 Self::Binary(_, oper, _) => Some(oper),
4037 _ => None,
4038 }
4039 }
4040}