Skip to content

Commit ac06652

Browse files
committed
Implement #[sql_name] for sql_function!
The docs for this have already been written, it just hadn't been implemented. The primary use case is to support variadic functions. Example: ```rust sql_function! { #[sql_name = "COALESCE"] fn coalesce2<T>(x: T, y: T) -> T; } sql_function! { #[sql_name = "COALESCE"] fn coalesce3<T>(x: T, y: T, Z: T) -> T; } sql_function! { #[sql_name = "COALESCE"] fn or_default<T: NotNull>(x: Nullable<T>, y: T) -> T; } ```
1 parent d75847e commit ac06652

3 files changed

Lines changed: 62 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ for Rust libraries in [RFC #1105](https://github.com/rust-lang/rfcs/blob/master/
2020
`NonAggregate` for your function. See [the documentation for
2121
`sql_function!`][sql-function-1-3-0] for more details.
2222

23+
* `sql_function!` now supports renaming the function by annotating it with
24+
`#[sql_name = "SOME_FUNCTION"]`. This can be used to support functions with
25+
multiple signatures such as coalesce, by defining multiple rust functions
26+
(with different names) that have the same `#[sql_name]`.
27+
2328
* Added `sqlite-bundled` feature to `diesel_cli` to make installing on
2429
some platforms easier.
2530

diesel/src/expression/functions/mod.rs

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,16 @@ macro_rules! __diesel_sql_function_body {
125125
}
126126
};
127127

128-
// Searching for `#[aggregate]`, found it.
128+
// Searching for `#[aggregate]`, found it. Search for `sql_name`.
129129
(
130130
unchecked_meta = (#[aggregate] $($unchecked:tt)*),
131131
meta = ($($meta:tt)*),
132132
$($rest:tt)*
133133
) => {
134134
__diesel_sql_function_body! {
135135
aggregate = yes,
136-
meta = ($($meta)* $($unchecked)*),
136+
unchecked_meta = ($($meta)* $($unchecked)*),
137+
meta = (),
137138
$($rest)*
138139
}
139140
};
@@ -151,21 +152,70 @@ macro_rules! __diesel_sql_function_body {
151152
}
152153
};
153154

154-
// Done searching for `#[aggregate]`.
155+
// Done searching for `#[aggregate]`. Search for `sql_name`
155156
(
156157
unchecked_meta = (),
157158
meta = $meta:tt,
158159
$($rest:tt)*
159160
) => {
160161
__diesel_sql_function_body! {
161162
aggregate = no,
163+
unchecked_meta = $meta,
164+
meta = (),
165+
$($rest)*
166+
}
167+
};
168+
169+
// Searching for `#[sql_name]`, found it.
170+
(
171+
aggregate = $aggregate:tt,
172+
unchecked_meta = (#[sql_name = $sql_name:expr] $($unchecked:tt)*),
173+
meta = ($($meta:tt)*),
174+
$($rest:tt)*
175+
) => {
176+
__diesel_sql_function_body! {
177+
aggregate = $aggregate,
178+
sql_name = $sql_name,
179+
meta = ($($meta)* $($unchecked)*),
180+
$($rest)*
181+
}
182+
};
183+
184+
// Searching for `#[sql_name]`. Didn't find it.
185+
(
186+
aggregate = $aggregate:tt,
187+
unchecked_meta = (#$checked:tt $($unchecked:tt)*),
188+
meta = ($($meta:tt)*),
189+
$($rest:tt)*
190+
) => {
191+
__diesel_sql_function_body! {
192+
aggregate = $aggregate,
193+
unchecked_meta = ($($unchecked)*),
194+
meta = ($($meta)* #$checked),
195+
$($rest)*
196+
}
197+
};
198+
199+
// Done searching for `#[sql_name]`.
200+
(
201+
aggregate = $aggregate:tt,
202+
unchecked_meta = (),
203+
meta = $meta:tt,
204+
fn_name = $fn_name:ident,
205+
$($rest:tt)*
206+
) => {
207+
__diesel_sql_function_body! {
208+
aggregate = $aggregate,
209+
sql_name = stringify!($fn_name),
162210
meta = $meta,
211+
fn_name = $fn_name,
163212
$($rest)*
164213
}
165214
};
166215

167216
(
168217
aggregate = $aggregate:tt,
218+
sql_name = $sql_name:expr,
169219
meta = ($($meta:tt)*),
170220
fn_name = $fn_name:ident,
171221
type_args = ($($type_args:ident,)*),
@@ -222,7 +272,7 @@ macro_rules! __diesel_sql_function_body {
222272
for<'a> ($(&'a $arg_name),*): $crate::query_builder::QueryFragment<DB>,
223273
{
224274
fn walk_ast(&self, mut out: $crate::query_builder::AstPass<DB>) -> $crate::result::QueryResult<()> {
225-
out.push_sql(concat!(stringify!($fn_name), "("));
275+
out.push_sql(concat!($sql_name, "("));
226276
$crate::query_builder::QueryFragment::walk_ast(
227277
&($(&self.$arg_name),*), out.reborrow())?;
228278
out.push_sql(")");
@@ -460,6 +510,7 @@ macro_rules! __diesel_sqlite_register_fn {
460510
///
461511
/// sql_function! {
462512
/// #[aggregate]
513+
/// #[sql_name = "SUM"]
463514
/// fn sum<ST: Foldable>(expr: ST) -> ST::Sum;
464515
/// }
465516
///

diesel/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@
125125
used_underscore_binding))]
126126
#![cfg_attr(all(test, feature = "clippy"), allow(option_unwrap_used, result_unwrap_used))]
127127

128+
#![recursion_limit = "256"]
129+
128130
#[cfg(feature = "postgres")]
129131
#[macro_use]
130132
extern crate bitflags;

0 commit comments

Comments
 (0)