Skip to main content

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