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", "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("hello", "MyType", '"'.into()))
550 /// .to_owned();
551 ///
552 /// assert_eq!(
553 /// query.to_string(MysqlQueryBuilder),
554 /// r#"SELECT CAST('hello' AS "MyType")"#
555 /// );
556 /// assert_eq!(
557 /// query.to_string(PostgresQueryBuilder),
558 /// r#"SELECT CAST('hello' AS "MyType")"#
559 /// );
560 /// assert_eq!(
561 /// query.to_string(SqliteQueryBuilder),
562 /// r#"SELECT CAST('hello' AS "MyType")"#
563 /// );
564 /// ```
565 pub fn cast_as_quoted<V, I>(expr: V, iden: I, q: Quote) -> FunctionCall
566 where
567 V: Into<SimpleExpr>,
568 I: IntoIden,
569 {
570 let expr: SimpleExpr = expr.into();
571 let mut quoted_type = String::new();
572 iden.into_iden().prepare(&mut quoted_type, q);
573 FunctionCall::new(Function::Cast)
574 .arg(expr.binary(BinOper::As, Expr::cust(quoted_type.as_str())))
575 }
576
577 /// Call `COALESCE` function.
578 ///
579 /// # Examples
580 ///
581 /// ```
582 /// use sea_query::{tests_cfg::*, *};
583 ///
584 /// let query = Query::select()
585 /// .expr(Func::coalesce([
586 /// Expr::col(Char::SizeW).into(),
587 /// Expr::col(Char::SizeH).into(),
588 /// Expr::val(12).into(),
589 /// ]))
590 /// .from(Char::Table)
591 /// .to_owned();
592 ///
593 /// assert_eq!(
594 /// query.to_string(MysqlQueryBuilder),
595 /// r#"SELECT COALESCE(`size_w`, `size_h`, 12) FROM `character`"#
596 /// );
597 /// assert_eq!(
598 /// query.to_string(PostgresQueryBuilder),
599 /// r#"SELECT COALESCE("size_w", "size_h", 12) FROM "character""#
600 /// );
601 /// assert_eq!(
602 /// query.to_string(SqliteQueryBuilder),
603 /// r#"SELECT COALESCE("size_w", "size_h", 12) FROM "character""#
604 /// );
605 /// ```
606 pub fn coalesce<I>(args: I) -> FunctionCall
607 where
608 I: IntoIterator<Item = SimpleExpr>,
609 {
610 FunctionCall::new(Function::Coalesce).args(args)
611 }
612
613 /// Call `LOWER` function.
614 ///
615 /// # Examples
616 ///
617 /// ```
618 /// use sea_query::{tests_cfg::*, *};
619 ///
620 /// let query = Query::select()
621 /// .expr(Func::lower(Expr::col(Char::Character)))
622 /// .from(Char::Table)
623 /// .to_owned();
624 ///
625 /// assert_eq!(
626 /// query.to_string(MysqlQueryBuilder),
627 /// r#"SELECT LOWER(`character`) FROM `character`"#
628 /// );
629 /// assert_eq!(
630 /// query.to_string(PostgresQueryBuilder),
631 /// r#"SELECT LOWER("character") FROM "character""#
632 /// );
633 /// assert_eq!(
634 /// query.to_string(SqliteQueryBuilder),
635 /// r#"SELECT LOWER("character") FROM "character""#
636 /// );
637 /// ```
638 ///
639 /// ```
640 /// use sea_query::{tests_cfg::*, *};
641 ///
642 /// let query = Query::select()
643 /// .column(Font::Id)
644 /// .from(Font::Table)
645 /// .and_where(Func::lower(Expr::col(Font::Name)).eq("abc".trim().to_lowercase()))
646 /// .take();
647 ///
648 /// assert_eq!(
649 /// query.to_string(MysqlQueryBuilder),
650 /// "SELECT `id` FROM `font` WHERE LOWER(`name`) = 'abc'"
651 /// );
652 /// assert_eq!(
653 /// query.to_string(PostgresQueryBuilder),
654 /// r#"SELECT "id" FROM "font" WHERE LOWER("name") = 'abc'"#
655 /// );
656 /// assert_eq!(
657 /// query.to_string(SqliteQueryBuilder),
658 /// r#"SELECT "id" FROM "font" WHERE LOWER("name") = 'abc'"#
659 /// );
660 /// ```
661 pub fn lower<T>(expr: T) -> FunctionCall
662 where
663 T: Into<SimpleExpr>,
664 {
665 FunctionCall::new(Function::Lower).arg(expr)
666 }
667
668 /// Call `UPPER` function.
669 ///
670 /// # Examples
671 ///
672 /// ```
673 /// use sea_query::{tests_cfg::*, *};
674 ///
675 /// let query = Query::select()
676 /// .expr(Func::upper(Expr::col(Char::Character)))
677 /// .from(Char::Table)
678 /// .to_owned();
679 ///
680 /// assert_eq!(
681 /// query.to_string(MysqlQueryBuilder),
682 /// r#"SELECT UPPER(`character`) FROM `character`"#
683 /// );
684 /// assert_eq!(
685 /// query.to_string(PostgresQueryBuilder),
686 /// r#"SELECT UPPER("character") FROM "character""#
687 /// );
688 /// assert_eq!(
689 /// query.to_string(SqliteQueryBuilder),
690 /// r#"SELECT UPPER("character") FROM "character""#
691 /// );
692 /// ```
693 pub fn upper<T>(expr: T) -> FunctionCall
694 where
695 T: Into<SimpleExpr>,
696 {
697 FunctionCall::new(Function::Upper).arg(expr)
698 }
699
700 /// Call `BIT_AND` function, this is not supported on SQLite.
701 ///
702 /// # Examples
703 ///
704 /// ```
705 /// use sea_query::{tests_cfg::*, *};
706 ///
707 /// let query = Query::select()
708 /// .expr(Func::bit_and(Expr::col(Char::FontSize)))
709 /// .from(Char::Table)
710 /// .to_owned();
711 ///
712 /// assert_eq!(
713 /// query.to_string(MysqlQueryBuilder),
714 /// r#"SELECT BIT_AND(`font_size`) FROM `character`"#
715 /// );
716 /// assert_eq!(
717 /// query.to_string(PostgresQueryBuilder),
718 /// r#"SELECT BIT_AND("font_size") FROM "character""#
719 /// );
720 /// ```
721 pub fn bit_and<T>(expr: T) -> FunctionCall
722 where
723 T: Into<SimpleExpr>,
724 {
725 FunctionCall::new(Function::BitAnd).arg(expr)
726 }
727
728 /// Call `BIT_OR` function, this is not supported on SQLite.
729 ///
730 /// # Examples
731 ///
732 /// ```
733 /// use sea_query::{tests_cfg::*, *};
734 ///
735 /// let query = Query::select()
736 /// .expr(Func::bit_or(Expr::col(Char::FontSize)))
737 /// .from(Char::Table)
738 /// .to_owned();
739 ///
740 /// assert_eq!(
741 /// query.to_string(MysqlQueryBuilder),
742 /// r#"SELECT BIT_OR(`font_size`) FROM `character`"#
743 /// );
744 /// assert_eq!(
745 /// query.to_string(PostgresQueryBuilder),
746 /// r#"SELECT BIT_OR("font_size") FROM "character""#
747 /// );
748 /// ```
749 pub fn bit_or<T>(expr: T) -> FunctionCall
750 where
751 T: Into<SimpleExpr>,
752 {
753 FunctionCall::new(Function::BitOr).arg(expr)
754 }
755
756 /// Call `ROUND` function.
757 ///
758 /// # Examples
759 ///
760 /// ```
761 /// use sea_query::tests_cfg::Character::Character;
762 /// use sea_query::{tests_cfg::*, *};
763 ///
764 /// let query = Query::select().expr(Func::round(5.654)).to_owned();
765 ///
766 /// assert_eq!(query.to_string(MysqlQueryBuilder), r#"SELECT ROUND(5.654)"#);
767 ///
768 /// assert_eq!(
769 /// query.to_string(PostgresQueryBuilder),
770 /// r#"SELECT ROUND(5.654)"#
771 /// );
772 ///
773 /// assert_eq!(
774 /// query.to_string(SqliteQueryBuilder),
775 /// r#"SELECT ROUND(5.654)"#
776 /// );
777 /// ```
778 pub fn round<A>(expr: A) -> FunctionCall
779 where
780 A: Into<SimpleExpr>,
781 {
782 FunctionCall::new(Function::Round).arg(expr)
783 }
784
785 /// Call `ROUND` function with the precision.
786 ///
787 /// # Examples
788 ///
789 /// ```
790 /// use sea_query::tests_cfg::Character::Character;
791 /// use sea_query::{tests_cfg::*, *};
792 ///
793 /// let query = Query::select()
794 /// .expr(Func::round_with_precision(5.654, 2))
795 /// .to_owned();
796 ///
797 /// assert_eq!(
798 /// query.to_string(MysqlQueryBuilder),
799 /// r#"SELECT ROUND(5.654, 2)"#
800 /// );
801 ///
802 /// assert_eq!(
803 /// query.to_string(PostgresQueryBuilder),
804 /// r#"SELECT ROUND(5.654, 2)"#
805 /// );
806 ///
807 /// assert_eq!(
808 /// query.to_string(SqliteQueryBuilder),
809 /// r#"SELECT ROUND(5.654, 2)"#
810 /// );
811 /// ```
812 pub fn round_with_precision<A, B>(a: A, b: B) -> FunctionCall
813 where
814 A: Into<SimpleExpr>,
815 B: Into<SimpleExpr>,
816 {
817 FunctionCall::new(Function::Round).args([a.into(), b.into()])
818 }
819
820 /// Call `RANDOM` function.
821 ///
822 /// # Examples
823 ///
824 /// ```
825 /// use sea_query::tests_cfg::Character::Character;
826 /// use sea_query::{tests_cfg::*, *};
827 ///
828 /// let query = Query::select().expr(Func::random()).to_owned();
829 ///
830 /// assert_eq!(query.to_string(MysqlQueryBuilder), r#"SELECT RAND()"#);
831 ///
832 /// assert_eq!(query.to_string(PostgresQueryBuilder), r#"SELECT RANDOM()"#);
833 ///
834 /// assert_eq!(query.to_string(SqliteQueryBuilder), r#"SELECT RANDOM()"#);
835 /// ```
836 pub fn random() -> FunctionCall {
837 FunctionCall::new(Function::Random)
838 }
839
840 /// Call `MD5` function, this is only available in Postgres and MySQL.
841 ///
842 /// # Examples
843 ///
844 /// ```
845 /// use sea_query::{tests_cfg::*, *};
846 ///
847 /// let query = Query::select()
848 /// .expr(Func::md5(Expr::col((Char::Table, Char::Character))))
849 /// .from(Char::Table)
850 /// .to_owned();
851 ///
852 /// assert_eq!(
853 /// query.to_string(MysqlQueryBuilder),
854 /// r#"SELECT MD5(`character`.`character`) FROM `character`"#
855 /// );
856 ///
857 /// assert_eq!(
858 /// query.to_string(PostgresQueryBuilder),
859 /// r#"SELECT MD5("character"."character") FROM "character""#
860 /// );
861 /// ```
862 pub fn md5<T>(expr: T) -> FunctionCall
863 where
864 T: Into<SimpleExpr>,
865 {
866 FunctionCall::new(Function::Md5).arg(expr)
867 }
868}