Debian Codegraph @

src/expression/functions/mod.rs

raw
1 //! Helper macros to define custom sql functions
2
3 #[doc(inline)]
4 pub use diesel_derives::declare_sql_function;
5
6 #[doc(inline)]
7 pub use diesel_derives::define_sql_function;
8
9 #[doc(inline)]
10 #[cfg(all(feature = "with-deprecated", not(feature = "without-deprecated")))]
11 pub use diesel_derives::sql_function_proc as sql_function;
12
13 #[macro_export]
14 #[doc(hidden)]
15 #[cfg(all(feature = "with-deprecated", not(feature = "without-deprecated")))]
16 macro_rules! no_arg_sql_function_body_except_to_sql {
17 ($type_name:ident, $return_type:ty, $docs:expr) => {
18 #[allow(non_camel_case_types)]
19 #[doc=$docs]
20 #[derive(
21 Debug, Clone, Copy, $crate::query_builder::QueryId, $crate::expression::ValidGrouping,
22 )]
23 pub struct $type_name;
24
25 impl $crate::expression::Expression for $type_name {
26 type SqlType = $return_type;
27 }
28
29 impl<QS> $crate::expression::SelectableExpression<QS> for $type_name {}
30
31 impl<QS> $crate::expression::AppearsOnTable<QS> for $type_name {}
32 };
33 }
34
35 #[macro_export]
36 #[doc(hidden)]
37 #[cfg(all(feature = "with-deprecated", not(feature = "without-deprecated")))]
38 macro_rules! no_arg_sql_function_body {
39 ($type_name:ident, $return_type:ty, $docs:expr, $($constraint:ident)::+) => {
40 no_arg_sql_function_body_except_to_sql!($type_name, $return_type, $docs);
41
42 impl<DB> $crate::query_builder::QueryFragment<DB> for $type_name where
43 DB: $crate::backend::Backend + $($constraint)::+,
44 {
45 fn walk_ast<'b>(&'b self, mut out: $crate::query_builder::AstPass<'_, 'b, DB>) -> $crate::result::QueryResult<()>
46 {
47 out.push_sql(concat!(stringify!($type_name), "()"));
48 Ok(())
49 }
50 }
51 };
52
53 ($type_name:ident, $return_type:ty, $docs:expr) => {
54 no_arg_sql_function_body_except_to_sql!($type_name, $return_type, $docs);
55
56 impl<DB> $crate::query_builder::QueryFragment<DB> for $type_name where
57 DB: $crate::backend::Backend,
58 {
59 fn walk_ast<'b>(&'b self, mut out: $crate::query_builder::AstPass<'_, 'b, DB>) -> $crate::result::QueryResult<()> {
60 out.push_sql(concat!(stringify!($type_name), "()"));
61 Ok(())
62 }
63 }
64 };
65 }
66
67 #[macro_export]
68 /// Declare a 0 argument SQL function for use in your code. This will generate a
69 /// unit struct, which is an expression representing calling this function. See
70 /// [`now`](crate::expression::dsl::now) for example output. `now` was
71 /// generated using:
72 ///
73 /// ```no_run
74 /// # pub use diesel::*;
75 /// no_arg_sql_function!(
76 /// now,
77 /// sql_types::Timestamp,
78 /// "Represents the SQL NOW() function"
79 /// );
80 /// # fn main() {}
81 /// ```
82 ///
83 /// You can optionally pass the name of a trait, as a constraint for backends which support the
84 /// function.
85 #[deprecated(
86 since = "2.0.0",
87 note = "Use `define_sql_function!` instead. See `CHANGELOG.md` for migration instructions"
88 )]
89 #[cfg(all(feature = "with-deprecated", not(feature = "without-deprecated")))]
90 macro_rules! no_arg_sql_function {
91 ($type_name:ident, $return_type:ty) => {
92 no_arg_sql_function!($type_name, $return_type, "");
93 };
94
95 ($type_name:ident, $return_type:ty, $docs:expr) => {
96 no_arg_sql_function_body!($type_name, $return_type, $docs);
97 };
98
99 ($type_name:ident, $return_type:ty, $docs:expr, $($constraint:ident)::+) => {
100 no_arg_sql_function_body!($type_name, $return_type, $docs, $($constraint)::+);
101 };
102 }
103
104 pub(crate) mod aggregate_expressions;
105 pub(crate) mod aggregate_folding;
106 pub(crate) mod aggregate_ordering;
107 pub(crate) mod date_and_time;
108 pub(crate) mod helper_types;
109 pub(crate) mod window_functions;