sea_query/func.rs
1//! For calling built-in SQL functions.
2
3use crate::{expr::*, types::*};
4
5#[cfg(feature = "backend-postgres")]
6pub use crate::extension::postgres::{PgFunc, PgFunction};
7
8/// Functions
9#[derive(Debug, Clone, PartialEq)]
10pub enum Function {
11 Max,
12 Min,
13 Sum,
14 Avg,
15 Abs,
16 Count,
17 IfNull,
18 Greatest,
19 Least,
20 CharLength,
21 Cast,
22 Custom(DynIden),
23 Coalesce,
24 Lower,
25 Upper,
26 BitAnd,
27 BitOr,
28 Random,
29 Round,
30 Md5,
31 #[cfg(feature = "backend-postgres")]
32 PgFunction(PgFunction),
33}
34
35/// Function call.
36#[derive(Debug, Clone, PartialEq)]
37pub struct FunctionCall {
38 pub(crate) func: Function,
39 pub(crate) args: Vec<SimpleExpr>,
40 pub(crate) mods: Vec<FuncArgMod>,
41}
42
43#[derive(Debug, Default, Copy, Clone, PartialEq)]
44pub struct FuncArgMod {
45 pub distinct: bool,
46}
47
48impl FunctionCall {
49 pub(crate) fn new(func: Function) -> Self {
50 Self {
51 func,
52 args: Vec::new(),
53 mods: Vec::new(),
54 }
55 }
56
57 /// Append an argument to the function call
58 pub fn arg<T>(self, arg: T) -> Self
59 where
60 T: Into<SimpleExpr>,
61 {
62 self.arg_with(arg, Default::default())
63 }
64
65 pub(crate) fn arg_with<T>(mut self, arg: T, mod_: FuncArgMod) -> Self
66 where
67 T: Into<SimpleExpr>,
68 {
69 self.args.push(arg.into());
70 self.mods.push(mod_);
71 self
72 }
73
74 /// Replace the arguments of the function call
75 pub fn args<I>(mut self, args: I) -> Self
76 where
77 I: IntoIterator<Item = SimpleExpr>,
78 {
79 self.args = args.into_iter().collect();
80 self.mods = vec![Default::default(); self.args.len()];
81 self
82 }
83
84 pub fn get_func(&self) -> &Function {
85 &self.func
86 }
87
88 pub fn get_args(&self) -> &[SimpleExpr] {
89 &self.args
90 }
91
92 pub fn get_mods(&self) -> &[FuncArgMod] {
93 &self.mods
94 }
95}
96
97/// Function call helper.
98#[derive(Debug, Clone)]
99pub struct Func;
100
101impl Func {
102 /// Call a custom function.
103 ///
104 /// # Examples
105 ///
106 /// ```
107 /// use sea_query::{tests_cfg::*, *};
108 ///
109 /// struct MyFunction;
110 ///
111 /// impl Iden for MyFunction {
112 /// fn unquoted(&self, s: &mut dyn Write) {
113 /// write!(s, "MY_FUNCTION").unwrap();
114 /// }
115 /// }
116 ///
117 /// let query = Query::select()
118 /// .expr(Func::cust(MyFunction).arg("hello"))
119 /// .to_owned();
120 ///
121 /// assert_eq!(
122 /// query.to_string(MysqlQueryBuilder),
123 /// r#"SELECT MY_FUNCTION('hello')"#
124 /// );
125 /// assert_eq!(
126 /// query.to_string(PostgresQueryBuilder),
127 /// r#"SELECT MY_FUNCTION('hello')"#
128 /// );
129 /// assert_eq!(
130 /// query.to_string(SqliteQueryBuilder),
131 /// r#"SELECT MY_FUNCTION('hello')"#
132 /// );
133 /// ```
134 pub fn cust<T>(func: T) -> FunctionCall
135 where
136 T: IntoIden,
137 {
138 FunctionCall::new(Function::Custom(func.into_iden()))
139 }
140
141 /// Call `MAX` function.
142 ///
143 /// # Examples
144 ///
145 /// ```
146 /// use sea_query::{tests_cfg::*, *};
147 ///
148 /// let query = Query::select()
149 /// .expr(Func::max(Expr::col((Char::Table, Char::SizeW))))
150 /// .from(Char::Table)
151 /// .to_owned();
152 ///
153 /// assert_eq!(
154 /// query.to_string(MysqlQueryBuilder),
155 /// r#"SELECT MAX(`character`.`size_w`) FROM `character`"#
156 /// );
157 /// assert_eq!(
158 /// query.to_string(PostgresQueryBuilder),
159 /// r#"SELECT MAX("character"."size_w") FROM "character""#
160 /// );
161 /// assert_eq!(
162 /// query.to_string(SqliteQueryBuilder),
163 /// r#"SELECT MAX("character"."size_w") FROM "character""#
164 /// );
165 /// ```
166 pub fn max<T>(expr: T) -> FunctionCall
167 where
168 T: Into<SimpleExpr>,
169 {
170 FunctionCall::new(Function::Max).arg(expr)
171 }
172
173 /// Call `MIN` function.
174 ///
175 /// # Examples
176 ///
177 /// ```
178 /// use sea_query::{tests_cfg::*, *};
179 ///
180 /// let query = Query::select()
181 /// .expr(Func::min(Expr::col((Char::Table, Char::SizeH))))
182 /// .from(Char::Table)
183 /// .to_owned();
184 ///
185 /// assert_eq!(
186 /// query.to_string(MysqlQueryBuilder),
187 /// r#"SELECT MIN(`character`.`size_h`) FROM `character`"#
188 /// );
189 /// assert_eq!(
190 /// query.to_string(PostgresQueryBuilder),
191 /// r#"SELECT MIN("character"."size_h") FROM "character""#
192 /// );
193 /// assert_eq!(
194 /// query.to_string(SqliteQueryBuilder),
195 /// r#"SELECT MIN("character"."size_h") FROM "character""#
196 /// );
197 /// ```
198 pub fn min<T>(expr: T) -> FunctionCall
199 where
200 T: Into<SimpleExpr>,
201 {
202 FunctionCall::new(Function::Min).arg(expr)
203 }
204
205 /// Call `SUM` function.
206 ///
207 /// # Examples
208 ///
209 /// ```
210 /// use sea_query::{tests_cfg::*, *};
211 ///
212 /// let query = Query::select()
213 /// .expr(Func::sum(Expr::col((Char::Table, Char::SizeH))))
214 /// .from(Char::Table)
215 /// .to_owned();
216 ///
217 /// assert_eq!(
218 /// query.to_string(MysqlQueryBuilder),
219 /// r#"SELECT SUM(`character`.`size_h`) FROM `character`"#
220 /// );
221 /// assert_eq!(
222 /// query.to_string(PostgresQueryBuilder),
223 /// r#"SELECT SUM("character"."size_h") FROM "character""#
224 /// );
225 /// assert_eq!(
226 /// query.to_string(SqliteQueryBuilder),
227 /// r#"SELECT SUM("character"."size_h") FROM "character""#
228 /// );
229 /// ```
230 pub fn sum<T>(expr: T) -> FunctionCall
231 where
232 T: Into<SimpleExpr>,
233 {
234 FunctionCall::new(Function::Sum).arg(expr)
235 }
236
237 /// Call `AVG` function.
238 ///
239 /// # Examples
240 ///
241 /// ```
242 /// use sea_query::{tests_cfg::*, *};
243 ///
244 /// let query = Query::select()
245 /// .expr(Func::avg(Expr::col((Char::Table, Char::SizeH))))
246 /// .from(Char::Table)
247 /// .to_owned();
248 ///
249 /// assert_eq!(
250 /// query.to_string(MysqlQueryBuilder),
251 /// r#"SELECT AVG(`character`.`size_h`) FROM `character`"#
252 /// );
253 /// assert_eq!(
254 /// query.to_string(PostgresQueryBuilder),
255 /// r#"SELECT AVG("character"."size_h") FROM "character""#
256 /// );
257 /// assert_eq!(
258 /// query.to_string(SqliteQueryBuilder),
259 /// r#"SELECT AVG("character"."size_h") FROM "character""#
260 /// );
261 /// ```
262 pub fn avg<T>(expr: T) -> FunctionCall
263 where
264 T: Into<SimpleExpr>,
265 {
266 FunctionCall::new(Function::Avg).arg(expr)
267 }
268
269 /// Call `ABS` function.
270 ///
271 /// # Examples
272 ///
273 /// ```
274 /// use sea_query::{tests_cfg::*, *};
275 ///
276 /// let query = Query::select()
277 /// .expr(Func::abs(Expr::col((Char::Table, Char::SizeH))))
278 /// .from(Char::Table)
279 /// .to_owned();
280 ///
281 /// assert_eq!(
282 /// query.to_string(MysqlQueryBuilder),
283 /// r#"SELECT ABS(`character`.`size_h`) FROM `character`"#
284 /// );
285 /// assert_eq!(
286 /// query.to_string(PostgresQueryBuilder),
287 /// r#"SELECT ABS("character"."size_h") FROM "character""#
288 /// );
289 /// assert_eq!(
290 /// query.to_string(SqliteQueryBuilder),
291 /// r#"SELECT ABS("character"."size_h") FROM "character""#
292 /// );
293 /// ```
294 pub fn abs<T>(expr: T) -> FunctionCall
295 where
296 T: Into<SimpleExpr>,
297 {
298 FunctionCall::new(Function::Abs).arg(expr)
299 }
300
301 /// Call `COUNT` function.
302 ///
303 /// # Examples
304 ///
305 /// ```
306 /// use sea_query::{tests_cfg::*, *};
307 ///
308 /// let query = Query::select()
309 /// .expr(Func::count(Expr::col((Char::Table, Char::Id))))
310 /// .from(Char::Table)
311 /// .to_owned();
312 ///
313 /// assert_eq!(
314 /// query.to_string(MysqlQueryBuilder),
315 /// r#"SELECT COUNT(`character`.`id`) FROM `character`"#
316 /// );
317 /// assert_eq!(
318 /// query.to_string(PostgresQueryBuilder),
319 /// r#"SELECT COUNT("character"."id") FROM "character""#
320 /// );
321 /// assert_eq!(
322 /// query.to_string(SqliteQueryBuilder),
323 /// r#"SELECT COUNT("character"."id") FROM "character""#
324 /// );
325 /// ```
326 pub fn count<T>(expr: T) -> FunctionCall
327 where
328 T: Into<SimpleExpr>,
329 {
330 FunctionCall::new(Function::Count).arg(expr)
331 }
332
333 /// Call `COUNT` function with the `DISTINCT` modifier.
334 ///
335 /// # Examples
336 ///
337 /// ```
338 /// use sea_query::{tests_cfg::*, *};
339 ///
340 /// let query = Query::select()
341 /// .expr(Func::count_distinct(Expr::col((Char::Table, Char::Id))))
342 /// .from(Char::Table)
343 /// .to_owned();
344 ///
345 /// assert_eq!(
346 /// query.to_string(MysqlQueryBuilder),
347 /// r#"SELECT COUNT(DISTINCT `character`.`id`) FROM `character`"#
348 /// );
349 /// assert_eq!(
350 /// query.to_string(PostgresQueryBuilder),
351 /// r#"SELECT COUNT(DISTINCT "character"."id") FROM "character""#
352 /// );
353 /// assert_eq!(
354 /// query.to_string(SqliteQueryBuilder),
355 /// r#"SELECT COUNT(DISTINCT "character"."id") FROM "character""#
356 /// );
357 /// ```
358 pub fn count_distinct<T>(expr: T) -> FunctionCall
359 where
360 T: Into<SimpleExpr>,
361 {
362 FunctionCall::new(Function::Count).arg_with(expr, FuncArgMod { distinct: true })
363 }
364
365 /// Call `CHAR_LENGTH` function.
366 ///
367 /// # Examples
368 ///
369 /// ```
370 /// use sea_query::{tests_cfg::*, *};
371 ///
372 /// let query = Query::select()
373 /// .expr(Func::char_length(Expr::col((Char::Table, Char::Character))))
374 /// .from(Char::Table)
375 /// .to_owned();
376 ///
377 /// assert_eq!(
378 /// query.to_string(MysqlQueryBuilder),
379 /// r#"SELECT CHAR_LENGTH(`character`.`character`) FROM `character`"#
380 /// );
381 /// assert_eq!(
382 /// query.to_string(PostgresQueryBuilder),
383 /// r#"SELECT CHAR_LENGTH("character"."character") FROM "character""#
384 /// );
385 /// assert_eq!(
386 /// query.to_string(SqliteQueryBuilder),
387 /// r#"SELECT LENGTH("character"."character") FROM "character""#
388 /// );
389 /// ```
390 pub fn char_length<T>(expr: T) -> FunctionCall
391 where
392 T: Into<SimpleExpr>,
393 {
394 FunctionCall::new(Function::CharLength).arg(expr)
395 }
396
397 /// Call `GREATEST` function.
398 ///
399 /// # Examples
400 ///
401 /// ```
402 /// use sea_query::{tests_cfg::*, *};
403 ///
404 /// let query = Query::select()
405 /// .expr(Func::greatest([
406 /// Expr::col(Char::SizeW).into(),
407 /// Expr::col(Char::SizeH).into(),
408 /// ]))
409 /// .from(Char::Table)
410 /// .to_owned();
411 ///
412 /// assert_eq!(
413 /// query.to_string(MysqlQueryBuilder),
414 /// r#"SELECT GREATEST(`size_w`, `size_h`) FROM `character`"#
415 /// );
416 /// assert_eq!(
417 /// query.to_string(PostgresQueryBuilder),
418 /// r#"SELECT GREATEST("size_w", "size_h") FROM "character""#
419 /// );
420 /// assert_eq!(
421 /// query.to_string(SqliteQueryBuilder),
422 /// r#"SELECT MAX("size_w", "size_h") FROM "character""#
423 /// );
424 /// ```
425 pub fn greatest<I>(args: I) -> FunctionCall
426 where
427 I: IntoIterator<Item = SimpleExpr>,
428 {
429 FunctionCall::new(Function::Greatest).args(args)
430 }
431
432 /// Call `LEAST` function.
433 ///
434 /// # Examples
435 ///
436 /// ```
437 /// use sea_query::{tests_cfg::*, *};
438 ///
439 /// let query = Query::select()
440 /// .expr(Func::least([
441 /// Expr::col(Char::SizeW).into(),
442 /// Expr::col(Char::SizeH).into(),
443 /// ]))
444 /// .from(Char::Table)
445 /// .to_owned();
446 ///
447 /// assert_eq!(
448 /// query.to_string(MysqlQueryBuilder),
449 /// r#"SELECT LEAST(`size_w`, `size_h`) FROM `character`"#
450 /// );
451 /// assert_eq!(
452 /// query.to_string(PostgresQueryBuilder),
453 /// r#"SELECT LEAST("size_w", "size_h") FROM "character""#
454 /// );
455 /// assert_eq!(
456 /// query.to_string(SqliteQueryBuilder),
457 /// r#"SELECT MIN("size_w", "size_h") FROM "character""#
458 /// );
459 /// ```
460 pub fn least<I>(args: I) -> FunctionCall
461 where
462 I: IntoIterator<Item = SimpleExpr>,
463 {
464 FunctionCall::new(Function::Least).args(args)
465 }
466
467 /// Call `IF NULL` function.
468 ///
469 /// # Examples
470 ///
471 /// ```
472 /// use sea_query::{tests_cfg::*, *};
473 ///
474 /// let query = Query::select()
475 /// .expr(Func::if_null(
476 /// Expr::col(Char::SizeW),
477 /// Expr::col(Char::SizeH),
478 /// ))
479 /// .from(Char::Table)
480 /// .to_owned();
481 ///
482 /// assert_eq!(
483 /// query.to_string(MysqlQueryBuilder),
484 /// r#"SELECT IFNULL(`size_w`, `size_h`) FROM `character`"#
485 /// );
486 /// assert_eq!(
487 /// query.to_string(PostgresQueryBuilder),
488 /// r#"SELECT COALESCE("size_w", "size_h") FROM "character""#
489 /// );
490 /// assert_eq!(
491 /// query.to_string(SqliteQueryBuilder),
492 /// r#"SELECT IFNULL("size_w", "size_h") FROM "character""#
493 /// );
494 /// ```
495 pub fn if_null<A, B>(a: A, b: B) -> FunctionCall
496 where
497 A: Into<SimpleExpr>,
498 B: Into<SimpleExpr>,
499 {
500 FunctionCall::new(Function::IfNull).args([a.into(), b.into()])
501 }
502
503 /// Call `CAST` function with a custom type.
504 ///
505 /// # Examples
506 ///
507 /// ```
508 /// use sea_query::{tests_cfg::*, *};
509 ///
510 /// let query = Query::select()
511 /// .expr(Func::cast_as("hello", Alias::new("MyType")))
512 /// .to_owned();
513 ///
514 /// assert_eq!(
515 /// query.to_string(MysqlQueryBuilder),
516 /// r#"SELECT CAST('hello' AS MyType)"#
517 /// );
518 /// assert_eq!(
519 /// query.to_string(PostgresQueryBuilder),
520 /// r#"SELECT CAST('hello' AS MyType)"#
521 /// );
522 /// assert_eq!(
523 /// query.to_string(SqliteQueryBuilder),
524 /// r#"SELECT CAST('hello' AS MyType)"#
525 /// );
526 /// ```
527 pub fn cast_as<V, I>(expr: V, iden: I) -> FunctionCall
528 where
529 V: Into<SimpleExpr>,
530 I: IntoIden,
531 {
532 let expr: SimpleExpr = expr.into();
533 FunctionCall::new(Function::Cast).arg(expr.binary(
534 BinOper::As,
535 Expr::cust(iden.into_iden().to_string().as_str()),
536 ))
537 }
538
539 /// Call `CAST` function with a case-sensitive custom type.
540 ///
541 /// # Examples
542 ///
543 /// ```
544 /// use sea_query::{tests_cfg::*, *};
545 ///
546 /// let query = Query::select()
547 /// .expr(Func::cast_as_quoted(
548 /// "hello",
549 /// Alias::new("MyType"),
550 /// '"'.into(),
551 /// ))
552 /// .to_owned();
553 ///
554 /// assert_eq!(
555 /// query.to_string(MysqlQueryBuilder),
556 /// r#"SELECT CAST('hello' AS "MyType")"#
557 /// );
558 /// assert_eq!(
559 /// query.to_string(PostgresQueryBuilder),
560 /// r#"SELECT CAST('hello' AS "MyType")"#
561 /// );
562 /// assert_eq!(
563 /// query.to_string(SqliteQueryBuilder),
564 /// r#"SELECT CAST('hello' AS "MyType")"#
565 /// );
566 /// ```
567 pub fn cast_as_quoted<V, I>(expr: V, iden: I, q: Quote) -> FunctionCall
568 where
569 V: Into<SimpleExpr>,
570 I: IntoIden,
571 {
572 let expr: SimpleExpr = expr.into();
573 let mut quoted_type = String::new();
574 iden.into_iden().prepare(&mut quoted_type, q);
575 FunctionCall::new(Function::Cast)
576 .arg(expr.binary(BinOper::As, Expr::cust(quoted_type.as_str())))
577 }
578
579 /// Call `COALESCE` function.
580 ///
581 /// # Examples
582 ///
583 /// ```
584 /// use sea_query::{tests_cfg::*, *};
585 ///
586 /// let query = Query::select()
587 /// .expr(Func::coalesce([
588 /// Expr::col(Char::SizeW).into(),
589 /// Expr::col(Char::SizeH).into(),
590 /// Expr::val(12).into(),
591 /// ]))
592 /// .from(Char::Table)
593 /// .to_owned();
594 ///
595 /// assert_eq!(
596 /// query.to_string(MysqlQueryBuilder),
597 /// r#"SELECT COALESCE(`size_w`, `size_h`, 12) FROM `character`"#
598 /// );
599 /// assert_eq!(
600 /// query.to_string(PostgresQueryBuilder),
601 /// r#"SELECT COALESCE("size_w", "size_h", 12) FROM "character""#
602 /// );
603 /// assert_eq!(
604 /// query.to_string(SqliteQueryBuilder),
605 /// r#"SELECT COALESCE("size_w", "size_h", 12) FROM "character""#
606 /// );
607 /// ```
608 pub fn coalesce<I>(args: I) -> FunctionCall
609 where
610 I: IntoIterator<Item = SimpleExpr>,
611 {
612 FunctionCall::new(Function::Coalesce).args(args)
613 }
614
615 /// Call `LOWER` function.
616 ///
617 /// # Examples
618 ///
619 /// ```
620 /// use sea_query::{tests_cfg::*, *};
621 ///
622 /// let query = Query::select()
623 /// .expr(Func::lower(Expr::col(Char::Character)))
624 /// .from(Char::Table)
625 /// .to_owned();
626 ///
627 /// assert_eq!(
628 /// query.to_string(MysqlQueryBuilder),
629 /// r#"SELECT LOWER(`character`) FROM `character`"#
630 /// );
631 /// assert_eq!(
632 /// query.to_string(PostgresQueryBuilder),
633 /// r#"SELECT LOWER("character") FROM "character""#
634 /// );
635 /// assert_eq!(
636 /// query.to_string(SqliteQueryBuilder),
637 /// r#"SELECT LOWER("character") FROM "character""#
638 /// );
639 /// ```
640 ///
641 /// ```
642 /// use sea_query::{tests_cfg::*, *};
643 ///
644 /// let query = Query::select()
645 /// .column(Font::Id)
646 /// .from(Font::Table)
647 /// .and_where(Expr::expr(Func::lower(Expr::col(Font::Name))).eq("abc".trim().to_lowercase()))
648 /// .take();
649 ///
650 /// assert_eq!(
651 /// query.to_string(MysqlQueryBuilder),
652 /// "SELECT `id` FROM `font` WHERE LOWER(`name`) = 'abc'"
653 /// );
654 /// assert_eq!(
655 /// query.to_string(PostgresQueryBuilder),
656 /// r#"SELECT "id" FROM "font" WHERE LOWER("name") = 'abc'"#
657 /// );
658 /// assert_eq!(
659 /// query.to_string(SqliteQueryBuilder),
660 /// r#"SELECT "id" FROM "font" WHERE LOWER("name") = 'abc'"#
661 /// );
662 /// ```
663 pub fn lower<T>(expr: T) -> FunctionCall
664 where
665 T: Into<SimpleExpr>,
666 {
667 FunctionCall::new(Function::Lower).arg(expr)
668 }
669
670 /// Call `UPPER` function.
671 ///
672 /// # Examples
673 ///
674 /// ```
675 /// use sea_query::{tests_cfg::*, *};
676 ///
677 /// let query = Query::select()
678 /// .expr(Func::upper(Expr::col(Char::Character)))
679 /// .from(Char::Table)
680 /// .to_owned();
681 ///
682 /// assert_eq!(
683 /// query.to_string(MysqlQueryBuilder),
684 /// r#"SELECT UPPER(`character`) FROM `character`"#
685 /// );
686 /// assert_eq!(
687 /// query.to_string(PostgresQueryBuilder),
688 /// r#"SELECT UPPER("character") FROM "character""#
689 /// );
690 /// assert_eq!(
691 /// query.to_string(SqliteQueryBuilder),
692 /// r#"SELECT UPPER("character") FROM "character""#
693 /// );
694 /// ```
695 pub fn upper<T>(expr: T) -> FunctionCall
696 where
697 T: Into<SimpleExpr>,
698 {
699 FunctionCall::new(Function::Upper).arg(expr)
700 }
701
702 /// Call `BIT_AND` function, this is not supported on SQLite.
703 ///
704 /// # Examples
705 ///
706 /// ```
707 /// use sea_query::{tests_cfg::*, *};
708 ///
709 /// let query = Query::select()
710 /// .expr(Func::bit_and(Expr::col(Char::FontSize)))
711 /// .from(Char::Table)
712 /// .to_owned();
713 ///
714 /// assert_eq!(
715 /// query.to_string(MysqlQueryBuilder),
716 /// r#"SELECT BIT_AND(`font_size`) FROM `character`"#
717 /// );
718 /// assert_eq!(
719 /// query.to_string(PostgresQueryBuilder),
720 /// r#"SELECT BIT_AND("font_size") FROM "character""#
721 /// );
722 /// ```
723 pub fn bit_and<T>(expr: T) -> FunctionCall
724 where
725 T: Into<SimpleExpr>,
726 {
727 FunctionCall::new(Function::BitAnd).arg(expr)
728 }
729
730 /// Call `BIT_OR` function, this is not supported on SQLite.
731 ///
732 /// # Examples
733 ///
734 /// ```
735 /// use sea_query::{tests_cfg::*, *};
736 ///
737 /// let query = Query::select()
738 /// .expr(Func::bit_or(Expr::col(Char::FontSize)))
739 /// .from(Char::Table)
740 /// .to_owned();
741 ///
742 /// assert_eq!(
743 /// query.to_string(MysqlQueryBuilder),
744 /// r#"SELECT BIT_OR(`font_size`) FROM `character`"#
745 /// );
746 /// assert_eq!(
747 /// query.to_string(PostgresQueryBuilder),
748 /// r#"SELECT BIT_OR("font_size") FROM "character""#
749 /// );
750 /// ```
751 pub fn bit_or<T>(expr: T) -> FunctionCall
752 where
753 T: Into<SimpleExpr>,
754 {
755 FunctionCall::new(Function::BitOr).arg(expr)
756 }
757
758 /// Call `ROUND` function.
759 ///
760 /// # Examples
761 ///
762 /// ```
763 /// use sea_query::tests_cfg::Character::Character;
764 /// use sea_query::{tests_cfg::*, *};
765 ///
766 /// let query = Query::select().expr(Func::round(5.654)).to_owned();
767 ///
768 /// assert_eq!(query.to_string(MysqlQueryBuilder), r#"SELECT ROUND(5.654)"#);
769 ///
770 /// assert_eq!(
771 /// query.to_string(PostgresQueryBuilder),
772 /// r#"SELECT ROUND(5.654)"#
773 /// );
774 ///
775 /// assert_eq!(
776 /// query.to_string(SqliteQueryBuilder),
777 /// r#"SELECT ROUND(5.654)"#
778 /// );
779 /// ```
780 pub fn round<A>(expr: A) -> FunctionCall
781 where
782 A: Into<SimpleExpr>,
783 {
784 FunctionCall::new(Function::Round).arg(expr)
785 }
786
787 /// Call `ROUND` function with the precision.
788 ///
789 /// # Examples
790 ///
791 /// ```
792 /// use sea_query::tests_cfg::Character::Character;
793 /// use sea_query::{tests_cfg::*, *};
794 ///
795 /// let query = Query::select()
796 /// .expr(Func::round_with_precision(5.654, 2))
797 /// .to_owned();
798 ///
799 /// assert_eq!(
800 /// query.to_string(MysqlQueryBuilder),
801 /// r#"SELECT ROUND(5.654, 2)"#
802 /// );
803 ///
804 /// assert_eq!(
805 /// query.to_string(PostgresQueryBuilder),
806 /// r#"SELECT ROUND(5.654, 2)"#
807 /// );
808 ///
809 /// assert_eq!(
810 /// query.to_string(SqliteQueryBuilder),
811 /// r#"SELECT ROUND(5.654, 2)"#
812 /// );
813 /// ```
814 pub fn round_with_precision<A, B>(a: A, b: B) -> FunctionCall
815 where
816 A: Into<SimpleExpr>,
817 B: Into<SimpleExpr>,
818 {
819 FunctionCall::new(Function::Round).args([a.into(), b.into()])
820 }
821
822 /// Call `RANDOM` function.
823 ///
824 /// # Examples
825 ///
826 /// ```
827 /// use sea_query::tests_cfg::Character::Character;
828 /// use sea_query::{tests_cfg::*, *};
829 ///
830 /// let query = Query::select().expr(Func::random()).to_owned();
831 ///
832 /// assert_eq!(query.to_string(MysqlQueryBuilder), r#"SELECT RAND()"#);
833 ///
834 /// assert_eq!(query.to_string(PostgresQueryBuilder), r#"SELECT RANDOM()"#);
835 ///
836 /// assert_eq!(query.to_string(SqliteQueryBuilder), r#"SELECT RANDOM()"#);
837 /// ```
838 pub fn random() -> FunctionCall {
839 FunctionCall::new(Function::Random)
840 }
841
842 /// Call `MD5` function, this is only available in Postgres and MySQL.
843 ///
844 /// # Examples
845 ///
846 /// ```
847 /// use sea_query::{tests_cfg::*, *};
848 ///
849 /// let query = Query::select()
850 /// .expr(Func::md5(Expr::col((Char::Table, Char::Character))))
851 /// .from(Char::Table)
852 /// .to_owned();
853 ///
854 /// assert_eq!(
855 /// query.to_string(MysqlQueryBuilder),
856 /// r#"SELECT MD5(`character`.`character`) FROM `character`"#
857 /// );
858 ///
859 /// assert_eq!(
860 /// query.to_string(PostgresQueryBuilder),
861 /// r#"SELECT MD5("character"."character") FROM "character""#
862 /// );
863 /// ```
864 pub fn md5<T>(expr: T) -> FunctionCall
865 where
866 T: Into<SimpleExpr>,
867 {
868 FunctionCall::new(Function::Md5).arg(expr)
869 }
870}