Skip to content

Commit 15d9201

Browse files
committed
Introduce a #[declare_sql_function] attribute macro
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
1 parent 233f814 commit 15d9201

39 files changed

+1343
-1181
lines changed

CHANGELOG.md

+12-1
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,24 @@ Increasing the minimal supported Rust version will always be coupled at least wi
1616
* Fixed `#[derive(Identifiable)]` ignoring attribute `#[diesel(serialize_as)]` on primary keys
1717
* Added embedded struct support for `AsChangeset` via `#[diesel(embed)]`
1818
* Support for libsqlite3-sys 0.30.0
19-
* Add support for built-in PostgreSQL range operators and functions
19+
* Added support for built-in PostgreSQL range operators and functions
20+
* Added support for various built-in PostgreSQL array functions
2021
* Support for postgres multirange type
2122
* Added `diesel::r2d2::TestCustomizer`, which allows users to customize their `diesel::r2d2::Pool`s
2223
in a way that makes the pools suitable for use in parallel tests.
2324
* Added `Json` and `Jsonb` support for the SQLite backend.
25+
* Added a `#[diesel::declare_sql_function]` attribute macro to easily define support for
26+
multiple sql functions at once via an `extern "SQL"` block
27+
28+
### Fixed
29+
2430
* Fixed diesel thinking `a.eq_any(b)` was non-nullable even if `a` and `b` were nullable.
2531

32+
### Deprecated
33+
34+
* The `diesel::define_sql_function!` is now deprecated in favour of the `#[diesel::declare_sql_function]`
35+
attribute macro.
36+
2637
## [2.2.2] 2024-07-19
2738

2839
### Fixed

diesel/src/connection/statement_cache/strategy.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ mod testing_utils {
148148
mod tests_pg {
149149
use crate::connection::CacheSize;
150150
use crate::dsl::sql;
151-
use crate::expression::functions::define_sql_function;
152151
use crate::insertable::Insertable;
153152
use crate::pg::Pg;
154153
use crate::sql_types::{Integer, VarChar};
@@ -158,6 +157,11 @@ mod tests_pg {
158157

159158
use super::testing_utils::{count_cache_calls, RecordCacheEvents};
160159

160+
#[crate::declare_sql_function]
161+
extern "SQL" {
162+
fn lower(x: VarChar) -> VarChar;
163+
}
164+
161165
table! {
162166
users {
163167
id -> Integer,
@@ -209,8 +213,6 @@ mod tests_pg {
209213
assert_eq!(2, count_cache_calls(connection));
210214
}
211215

212-
define_sql_function!(fn lower(x: VarChar) -> VarChar);
213-
214216
#[test]
215217
fn queries_with_identical_types_and_binds_but_different_sql_are_cached_separately() {
216218
let connection = &mut connection();

diesel/src/expression/count.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::marker::PhantomData;
22

3-
use super::functions::define_sql_function;
3+
use super::functions::declare_sql_function;
44
use super::{is_aggregate, AsExpression};
55
use super::{Expression, ValidGrouping};
66
use crate::backend::Backend;
@@ -9,7 +9,8 @@ use crate::result::QueryResult;
99
use crate::sql_types::{BigInt, DieselNumericOps, SingleValue, SqlType};
1010
use crate::{AppearsOnTable, SelectableExpression};
1111

12-
define_sql_function! {
12+
#[declare_sql_function]
13+
extern "SQL" {
1314
/// Creates a SQL `COUNT` expression
1415
///
1516
/// As with most bare functions, this is not exported by default. You can import

diesel/src/expression/functions/aggregate_folding.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
use crate::expression::functions::define_sql_function;
1+
use crate::expression::functions::declare_sql_function;
22
use crate::sql_types::Foldable;
33

4-
define_sql_function! {
4+
#[declare_sql_function]
5+
extern "SQL" {
56
/// Represents a SQL `SUM` function. This function can only take types which are
67
/// Foldable.
78
///
@@ -19,9 +20,7 @@ define_sql_function! {
1920
/// ```
2021
#[aggregate]
2122
fn sum<ST: Foldable>(expr: ST) -> ST::Sum;
22-
}
2323

24-
define_sql_function! {
2524
/// Represents a SQL `AVG` function. This function can only take types which are
2625
/// Foldable.
2726
///

diesel/src/expression/functions/aggregate_ordering.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use self::private::SqlOrdAggregate;
2-
use crate::expression::functions::define_sql_function;
2+
use crate::expression::functions::declare_sql_function;
33

4-
define_sql_function! {
4+
#[declare_sql_function]
5+
extern "SQL" {
56
/// Represents a SQL `MAX` function. This function can only take types which are
67
/// ordered.
78
///
@@ -18,9 +19,7 @@ define_sql_function! {
1819
/// # }
1920
#[aggregate]
2021
fn max<ST: SqlOrdAggregate>(expr: ST) -> ST::Ret;
21-
}
2222

23-
define_sql_function! {
2423
/// Represents a SQL `MIN` function. This function can only take types which are
2524
/// ordered.
2625
///

diesel/src/expression/functions/date_and_time.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::backend::Backend;
22
use crate::expression::coerce::Coerce;
3-
use crate::expression::functions::define_sql_function;
3+
use crate::expression::functions::declare_sql_function;
44
use crate::expression::{AsExpression, Expression, ValidGrouping};
55
use crate::query_builder::*;
66
use crate::result::QueryResult;
@@ -27,7 +27,9 @@ impl_selectable_expression!(now);
2727

2828
operator_allowed!(now, Add, add);
2929
operator_allowed!(now, Sub, sub);
30-
define_sql_function! {
30+
31+
#[declare_sql_function]
32+
extern "SQL" {
3133
/// Represents the SQL `DATE` function. The argument should be a Timestamp
3234
/// expression, and the return value will be an expression of type Date.
3335
///

diesel/src/expression/functions/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
//! Helper macros to define custom sql functions
22
3+
#[doc(inline)]
4+
pub use diesel_derives::declare_sql_function;
5+
6+
#[cfg(all(feature = "with-deprecated", not(feature = "without-deprecated")))]
37
#[doc(inline)]
48
pub use diesel_derives::define_sql_function;
59

diesel/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,8 @@ pub mod prelude {
725725
pub use crate::expression::IntoSql as _;
726726

727727
#[doc(inline)]
728+
pub use crate::expression::functions::declare_sql_function;
729+
#[cfg(all(feature = "with-deprecated", not(feature = "without-deprecated")))]
728730
pub use crate::expression::functions::define_sql_function;
729731
#[cfg(all(feature = "with-deprecated", not(feature = "without-deprecated")))]
730732
pub use crate::expression::functions::sql_function;

0 commit comments

Comments
 (0)