Skip to content

Commit

Permalink
Introduce a #[declare_sql_function] attribute macro
Browse files Browse the repository at this point in the history
This commit introduces a new `#[declare_sql_function]` attribute macro
that can be applied to `extern "SQL"` blocks. This is essentially the
same as the existing `define_sql_function!` function like macro in terms
of functionality.

I see the following advantages of using an attribute macro + an `extern
"SQL"` block instead:

* This is closer to rust syntax, so rustfmt will understand that and
work correctly inside these blocks
* This allows to put several functions into the same block
* Maybe in the future this also allows to apply attributes to the whole
block instead of to each item

The downside of this change is that we then have three variants to
declare sql functions:

* `sql_function!()` (deprectated)
* `define_sql_function!()` (introduced in 2.2, we might want to
deprecate that as well?)
* The new attribute macro
  • Loading branch information
weiznich committed Nov 26, 2024
1 parent a55f0fe commit 599c2bb
Show file tree
Hide file tree
Showing 4 changed files with 395 additions and 4 deletions.
7 changes: 3 additions & 4 deletions diesel/src/expression/functions/aggregate_folding.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::expression::functions::define_sql_function;
use crate::expression::functions::declare_sql_function;
use crate::sql_types::Foldable;

define_sql_function! {
#[declare_sql_function]
extern "SQL" {
/// Represents a SQL `SUM` function. This function can only take types which are
/// Foldable.
///
Expand All @@ -19,9 +20,7 @@ define_sql_function! {
/// ```
#[aggregate]
fn sum<ST: Foldable>(expr: ST) -> ST::Sum;
}

define_sql_function! {
/// Represents a SQL `AVG` function. This function can only take types which are
/// Foldable.
///
Expand Down
2 changes: 2 additions & 0 deletions diesel/src/expression/functions/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Helper macros to define custom sql functions

#[doc(inline)]
pub use diesel_derives::declare_sql_function;
#[doc(inline)]
pub use diesel_derives::define_sql_function;

Expand Down
Loading

0 comments on commit 599c2bb

Please sign in to comment.