diesel/expression/functions/
mod.rs

1//! Helper macros to define custom sql functions
2
3#[doc(inline)]
4pub use diesel_derives::declare_sql_function;
5
6#[doc(inline)]
7pub use diesel_derives::define_sql_function;
8
9#[doc(inline)]
10#[cfg(all(feature = "with-deprecated", not(feature = "without-deprecated")))]
11pub 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")))]
16macro_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")))]
38macro_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!(now, sql_types::Timestamp, "Represents the SQL NOW() function");
76/// # fn main() {}
77/// ```
78///
79/// You can optionally pass the name of a trait, as a constraint for backends which support the
80/// function.
81#[deprecated(
82    since = "2.0.0",
83    note = "Use `define_sql_function!` instead. See `CHANGELOG.md` for migration instructions"
84)]
85#[cfg(all(feature = "with-deprecated", not(feature = "without-deprecated")))]
86macro_rules! no_arg_sql_function {
87    ($type_name:ident, $return_type:ty) => {
88        no_arg_sql_function!($type_name, $return_type, "");
89    };
90
91    ($type_name:ident, $return_type:ty, $docs:expr) => {
92        no_arg_sql_function_body!($type_name, $return_type, $docs);
93    };
94
95    ($type_name:ident, $return_type:ty, $docs:expr, $($constraint:ident)::+) => {
96        no_arg_sql_function_body!($type_name, $return_type, $docs, $($constraint)::+);
97    };
98}
99
100pub(crate) mod aggregate_folding;
101pub(crate) mod aggregate_ordering;
102pub(crate) mod date_and_time;
103pub(crate) mod helper_types;