Skip to content

Commit 59c9117

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) * The new attribute macro
1 parent 874b97b commit 59c9117

38 files changed

+928
-714
lines changed

CHANGELOG.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,18 @@ Increasing the minimal supported Rust version will always be coupled at least wi
1717
* Added embedded struct support for `AsChangeset` via `#[diesel(embed)]`
1818
* Added a `#[diesel(skip_update)]` attribute for the `AsChangeset` derive to skip updating a field present in the struct
1919
* Support for libsqlite3-sys 0.31.0
20-
* Add support for built-in PostgreSQL range operators and functions
20+
* Add support for built-in PostgreSQL range operators and functions
2121
* Support for postgres multirange type
2222
* Added `diesel::r2d2::TestCustomizer`, which allows users to customize their `diesel::r2d2::Pool`s
2323
in a way that makes the pools suitable for use in parallel tests.
24+
* Added support for built-in PostgreSQL range operators and functions
25+
* Added support for various built-in PostgreSQL array functions
2426
* Added `Json` and `Jsonb` support for the SQLite backend.
27+
* Added a `#[diesel::declare_sql_function]` attribute macro to easily define support for
28+
multiple sql functions at once via an `extern "SQL"` block
29+
30+
### Fixed
31+
2532
* Fixed diesel thinking `a.eq_any(b)` was non-nullable even if `a` and `b` were nullable.
2633
* Generate `InstrumentationEvent::BeginTransaction` for immediate and exclusive transactions in SQLite
2734
* Added `wasm32-unknown-unknown` target support for sqlite backend.

diesel/src/connection/statement_cache/strategy.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,6 @@ mod testing_utils {
149149
mod tests_pg {
150150
use crate::connection::CacheSize;
151151
use crate::dsl::sql;
152-
use crate::expression::functions::define_sql_function;
153152
use crate::insertable::Insertable;
154153
use crate::pg::Pg;
155154
use crate::sql_types::{Integer, VarChar};
@@ -159,6 +158,11 @@ mod tests_pg {
159158

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

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

213-
define_sql_function!(fn lower(x: VarChar) -> VarChar);
214-
215217
#[diesel_test_helper::test]
216218
fn queries_with_identical_types_and_binds_but_different_sql_are_cached_separately() {
217219
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
@@ -735,6 +735,8 @@ pub mod prelude {
735735
pub use crate::expression::IntoSql as _;
736736

737737
#[doc(inline)]
738+
pub use crate::expression::functions::declare_sql_function;
739+
#[cfg(all(feature = "with-deprecated", not(feature = "without-deprecated")))]
738740
pub use crate::expression::functions::define_sql_function;
739741
#[cfg(all(feature = "with-deprecated", not(feature = "without-deprecated")))]
740742
pub use crate::expression::functions::sql_function;

0 commit comments

Comments
 (0)