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