Skip to content

Fix support of sql functions with #[auto_type] #3773

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions diesel/src/expression/count.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::marker::PhantomData;

use super::functions::sql_function;
use super::functions::sql_function_v2;
use super::{is_aggregate, AsExpression};
use super::{Expression, ValidGrouping};
use crate::backend::Backend;
Expand All @@ -9,7 +9,7 @@ use crate::result::QueryResult;
use crate::sql_types::{BigInt, DieselNumericOps, SingleValue, SqlType};
use crate::{AppearsOnTable, SelectableExpression};

sql_function! {
sql_function_v2! {
/// Creates a SQL `COUNT` expression
///
/// As with most bare functions, this is not exported by default. You can import
Expand Down
6 changes: 3 additions & 3 deletions diesel/src/expression/functions/aggregate_folding.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::expression::functions::sql_function;
use crate::expression::functions::sql_function_v2;
use crate::sql_types::Foldable;

sql_function! {
sql_function_v2! {
/// Represents a SQL `SUM` function. This function can only take types which are
/// Foldable.
///
Expand All @@ -21,7 +21,7 @@ sql_function! {
fn sum<ST: Foldable>(expr: ST) -> ST::Sum;
}

sql_function! {
sql_function_v2! {
/// Represents a SQL `AVG` function. This function can only take types which are
/// Foldable.
///
Expand Down
6 changes: 3 additions & 3 deletions diesel/src/expression/functions/aggregate_ordering.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use self::private::SqlOrdAggregate;
use crate::expression::functions::sql_function;
use crate::expression::functions::sql_function_v2;

sql_function! {
sql_function_v2! {
/// Represents a SQL `MAX` function. This function can only take types which are
/// ordered.
///
Expand All @@ -20,7 +20,7 @@ sql_function! {
fn max<ST: SqlOrdAggregate>(expr: ST) -> ST::Ret;
}

sql_function! {
sql_function_v2! {
/// Represents a SQL `MIN` function. This function can only take types which are
/// ordered.
///
Expand Down
4 changes: 2 additions & 2 deletions diesel/src/expression/functions/date_and_time.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::backend::Backend;
use crate::expression::coerce::Coerce;
use crate::expression::functions::sql_function;
use crate::expression::functions::sql_function_v2;
use crate::expression::{AsExpression, Expression, ValidGrouping};
use crate::query_builder::*;
use crate::result::QueryResult;
Expand All @@ -27,7 +27,7 @@ impl_selectable_expression!(now);

operator_allowed!(now, Add, add);
operator_allowed!(now, Sub, sub);
sql_function! {
sql_function_v2! {
/// Represents the SQL `DATE` function. The argument should be a Timestamp
/// expression, and the return value will be an expression of type Date.
///
Expand Down
8 changes: 4 additions & 4 deletions diesel/src/expression/functions/helper_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ use crate::expression::operators;
pub type not<Expr> = operators::Not<Grouped<Expr>>;

/// The return type of [`max(expr)`](crate::dsl::max())
pub type max<Expr> = super::aggregate_ordering::max::HelperType<SqlTypeOf<Expr>, Expr>;
pub type max<Expr> = super::aggregate_ordering::max<SqlTypeOf<Expr>, Expr>;

/// The return type of [`min(expr)`](crate::dsl::min())
pub type min<Expr> = super::aggregate_ordering::min::HelperType<SqlTypeOf<Expr>, Expr>;
pub type min<Expr> = super::aggregate_ordering::min<SqlTypeOf<Expr>, Expr>;

/// The return type of [`sum(expr)`](crate::dsl::sum())
pub type sum<Expr> = super::aggregate_folding::sum::HelperType<SqlTypeOf<Expr>, Expr>;
pub type sum<Expr> = super::aggregate_folding::sum<SqlTypeOf<Expr>, Expr>;

/// The return type of [`avg(expr)`](crate::dsl::avg())
pub type avg<Expr> = super::aggregate_folding::avg::HelperType<SqlTypeOf<Expr>, Expr>;
pub type avg<Expr> = super::aggregate_folding::avg<SqlTypeOf<Expr>, Expr>;

/// The return type of [`exists(expr)`](crate::dsl::exists())
pub type exists<Expr> = crate::expression::exists::Exists<Expr>;
4 changes: 2 additions & 2 deletions diesel/src/expression/functions/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Helper macros to define custom sql functions

#[doc(inline)]
pub use diesel_derives::sql_function_proc as sql_function;
pub use diesel_derives::{sql_function_proc as sql_function, sql_function_v2};

#[macro_export]
#[doc(hidden)]
Expand Down Expand Up @@ -73,7 +73,7 @@ macro_rules! no_arg_sql_function_body {
/// function.
#[deprecated(
since = "2.0.0",
note = "Use `sql_function!` instead. See `CHANGELOG.md` for migration instructions"
note = "Use `sql_function_v2!` instead. See `CHANGELOG.md` for migration instructions"
)]
#[cfg(all(feature = "with-deprecated", not(feature = "without-deprecated")))]
macro_rules! no_arg_sql_function {
Expand Down
4 changes: 2 additions & 2 deletions diesel/src/expression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub(crate) mod dsl {
pub use crate::pg::expression::dsl::*;

/// The return type of [`count(expr)`](crate::dsl::count())
pub type count<Expr> = super::count::count::HelperType<SqlTypeOf<Expr>, Expr>;
pub type count<Expr> = super::count::count<SqlTypeOf<Expr>, Expr>;

/// The return type of [`count_star()`](crate::dsl::count_star())
pub type count_star = super::count::CountStar;
Expand All @@ -82,7 +82,7 @@ pub(crate) mod dsl {
pub type count_distinct<Expr> = super::count::CountDistinct<SqlTypeOf<Expr>, Expr>;

/// The return type of [`date(expr)`](crate::dsl::date())
pub type date<Expr> = super::functions::date_and_time::date::HelperType<Expr>;
pub type date<Expr> = super::functions::date_and_time::date<Expr>;

#[cfg(feature = "mysql_backend")]
pub use crate::mysql::query_builder::DuplicatedKeys;
Expand Down
4 changes: 2 additions & 2 deletions diesel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
//! They live in [the `dsl` module](dsl).
//! Diesel only supports a very small number of these functions.
//! You can declare additional functions you want to use
//! with [the `sql_function!` macro][`sql_function!`].
//! with [the `sql_function_v2!` macro][`sql_function_v2!`].
//!
//! [`std::ops`]: //doc.rust-lang.org/stable/std/ops/index.html
//!
Expand Down Expand Up @@ -640,7 +640,7 @@ pub mod prelude {
};

#[doc(inline)]
pub use crate::expression::functions::sql_function;
pub use crate::expression::functions::{sql_function, sql_function_v2};

#[doc(inline)]
pub use crate::expression::SelectableHelper;
Expand Down
2 changes: 1 addition & 1 deletion diesel/src/pg/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ mod tests {
assert_eq!(2, connection.statement_cache.len());
}

sql_function!(fn lower(x: VarChar) -> VarChar);
sql_function_v2!(fn lower(x: VarChar) -> VarChar);

#[test]
fn queries_with_identical_types_and_binds_but_different_sql_are_cached_separately() {
Expand Down
24 changes: 12 additions & 12 deletions diesel/src/pg/expression/functions.rs
Original file line number Diff line number Diff line change
@@ -1,61 +1,61 @@
//! PostgreSQL specific functions

use super::expression_methods::InetOrCidr;
use crate::expression::functions::sql_function;
use crate::expression::functions::sql_function_v2;
use crate::sql_types::*;

sql_function! {
sql_function_v2! {
/// Creates an abbreviated display format as text.
#[cfg(feature = "postgres_backend")]
fn abbrev<T: InetOrCidr + SingleValue>(addr: T) -> Text;
}
sql_function! {
sql_function_v2! {
/// Computes the broadcast address for the address's network.
#[cfg(feature = "postgres_backend")]
fn broadcast<T: InetOrCidr + SingleValue>(addr: T) -> Inet;
}
sql_function! {
sql_function_v2! {
/// Returns the address's family: 4 for IPv4, 6 for IPv6.
#[cfg(feature = "postgres_backend")]
fn family<T: InetOrCidr + SingleValue>(addr: T) -> Integer;
}
sql_function! {
sql_function_v2! {
/// Returns the IP address as text, ignoring the netmask.
#[cfg(feature = "postgres_backend")]
fn host<T: InetOrCidr + SingleValue>(addr: T) -> Text;
}
sql_function! {
sql_function_v2! {
/// Computes the host mask for the address's network.
#[cfg(feature = "postgres_backend")]
fn hostmask<T: InetOrCidr + SingleValue>(addr: T) -> Inet;
}
sql_function! {
sql_function_v2! {
/// Computes the smallest network that includes both of the given networks.
#[cfg(feature = "postgres_backend")]
fn inet_merge<T: InetOrCidr + SingleValue, U: InetOrCidr + SingleValue>(a: T, b: U) -> Cidr;
}
sql_function! {
sql_function_v2! {
/// Tests whether the addresses belong to the same IP family.
#[cfg(feature = "postgres_backend")]
fn inet_same_family<T: InetOrCidr + SingleValue, U: InetOrCidr + SingleValue>(a: T, b: U) -> Bool;
}
sql_function! {
sql_function_v2! {
/// Returns the netmask length in bits.
#[cfg(feature = "postgres_backend")]
fn masklen<T: InetOrCidr + SingleValue>(addr: T) -> Integer;
}
sql_function! {
sql_function_v2! {
/// Computes the network mask for the address's network.
#[cfg(feature = "postgres_backend")]
fn netmask<T: InetOrCidr + SingleValue>(addr: T) -> Inet;
}
sql_function! {
sql_function_v2! {
/// Returns the network part of the address, zeroing out whatever is to the right of the
/// netmask. (This is equivalent to casting the value to cidr.)
#[cfg(feature = "postgres_backend")]
fn network<T: InetOrCidr + SingleValue>(addr: T) -> Cidr;
}
sql_function! {
sql_function_v2! {
/// Sets the netmask length for an inet or cidr value.
/// For inet, the address part does not changes. For cidr, address bits to the right of the new
/// netmask are set to zero.
Expand Down
2 changes: 1 addition & 1 deletion diesel/src/pg/metadata_lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,4 @@ table! {
joinable!(pg_type -> pg_namespace(typnamespace));
allow_tables_to_appear_in_same_query!(pg_type, pg_namespace);

sql_function! { fn pg_my_temp_schema() -> Oid; }
sql_function_v2! { fn pg_my_temp_schema() -> Oid; }
28 changes: 14 additions & 14 deletions diesel/src/sqlite/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -634,12 +634,12 @@ mod tests {
}

use crate::sql_types::Text;
sql_function!(fn fun_case(x: Text) -> Text);
sql_function_v2!(fn fun_case(x: Text) -> Text);

#[test]
fn register_custom_function() {
let connection = &mut SqliteConnection::establish(":memory:").unwrap();
fun_case::register_impl(connection, |x: String| {
fun_case_internals::register_impl(connection, |x: String| {
x.chars()
.enumerate()
.map(|(i, c)| {
Expand All @@ -659,23 +659,23 @@ mod tests {
assert_eq!("fOoBaR", mapped_string);
}

sql_function!(fn my_add(x: Integer, y: Integer) -> Integer);
sql_function_v2!(fn my_add(x: Integer, y: Integer) -> Integer);

#[test]
fn register_multiarg_function() {
let connection = &mut SqliteConnection::establish(":memory:").unwrap();
my_add::register_impl(connection, |x: i32, y: i32| x + y).unwrap();
my_add_internals::register_impl(connection, |x: i32, y: i32| x + y).unwrap();

let added = crate::select(my_add(1, 2)).get_result::<i32>(connection);
assert_eq!(Ok(3), added);
}

sql_function!(fn answer() -> Integer);
sql_function_v2!(fn answer() -> Integer);

#[test]
fn register_noarg_function() {
let connection = &mut SqliteConnection::establish(":memory:").unwrap();
answer::register_impl(connection, || 42).unwrap();
answer_internals::register_impl(connection, || 42).unwrap();

let answer = crate::select(answer()).get_result::<i32>(connection);
assert_eq!(Ok(42), answer);
Expand All @@ -684,19 +684,19 @@ mod tests {
#[test]
fn register_nondeterministic_noarg_function() {
let connection = &mut SqliteConnection::establish(":memory:").unwrap();
answer::register_nondeterministic_impl(connection, || 42).unwrap();
answer_internals::register_nondeterministic_impl(connection, || 42).unwrap();

let answer = crate::select(answer()).get_result::<i32>(connection);
assert_eq!(Ok(42), answer);
}

sql_function!(fn add_counter(x: Integer) -> Integer);
sql_function_v2!(fn add_counter(x: Integer) -> Integer);

#[test]
fn register_nondeterministic_function() {
let connection = &mut SqliteConnection::establish(":memory:").unwrap();
let mut y = 0;
add_counter::register_nondeterministic_impl(connection, move |x: i32| {
add_counter_internals::register_nondeterministic_impl(connection, move |x: i32| {
y += 1;
x + y
})
Expand All @@ -709,7 +709,7 @@ mod tests {

use crate::sqlite::SqliteAggregateFunction;

sql_function! {
sql_function_v2! {
#[aggregate]
fn my_sum(expr: Integer) -> Integer;
}
Expand Down Expand Up @@ -752,7 +752,7 @@ mod tests {
.execute(connection)
.unwrap();

my_sum::register_impl::<MySum, _>(connection).unwrap();
my_sum_internals::register_impl::<MySum, _>(connection).unwrap();

let result = my_sum_example
.select(my_sum(value))
Expand All @@ -771,15 +771,15 @@ mod tests {
.execute(connection)
.unwrap();

my_sum::register_impl::<MySum, _>(connection).unwrap();
my_sum_internals::register_impl::<MySum, _>(connection).unwrap();

let result = my_sum_example
.select(my_sum(value))
.get_result::<i32>(connection);
assert_eq!(Ok(0), result);
}

sql_function! {
sql_function_v2! {
#[aggregate]
fn range_max(expr1: Integer, expr2: Integer, expr3: Integer) -> Nullable<Integer>;
}
Expand Down Expand Up @@ -843,7 +843,7 @@ mod tests {
.execute(connection)
.unwrap();

range_max::register_impl::<RangeMax<i32>, _, _, _>(connection).unwrap();
range_max_internals::register_impl::<RangeMax<i32>, _, _, _>(connection).unwrap();
let result = range_max_example
.select(range_max(value1, value2, value3))
.get_result::<Option<i32>>(connection)
Expand Down
4 changes: 2 additions & 2 deletions diesel/src/sqlite/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ pub use self::query_builder::SqliteQueryBuilder;

/// Trait for the implementation of a SQLite aggregate function
///
/// This trait is to be used in conjunction with the `sql_function!`
/// This trait is to be used in conjunction with the `sql_function_v2!`
/// macro for defining a custom SQLite aggregate function. See
/// the documentation [there](super::prelude::sql_function!) for details.
/// the documentation [there](super::prelude::sql_function_v2!) for details.
pub trait SqliteAggregateFunction<Args>: Default {
/// The result type of the SQLite aggregate function
type Output;
Expand Down
6 changes: 3 additions & 3 deletions diesel/src/sqlite/types/date_and_time/chrono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,9 @@ mod tests {
use crate::sql_types::{Text, Time, Timestamp, TimestamptzSqlite};
use crate::test_helpers::connection;

sql_function!(fn datetime(x: Text) -> Timestamp);
sql_function!(fn time(x: Text) -> Time);
sql_function!(fn date(x: Text) -> Date);
sql_function_v2!(fn datetime(x: Text) -> Timestamp);
sql_function_v2!(fn time(x: Text) -> Time);
sql_function_v2!(fn date(x: Text) -> Date);

#[test]
fn unix_epoch_encodes_correctly() {
Expand Down
6 changes: 3 additions & 3 deletions diesel/src/sqlite/types/date_and_time/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,9 @@ mod tests {
use crate::sql_types::{Text, Time, Timestamp, TimestamptzSqlite};
use crate::test_helpers::connection;

sql_function!(fn datetime(x: Text) -> Timestamp);
sql_function!(fn time(x: Text) -> Time);
sql_function!(fn date(x: Text) -> Date);
sql_function_v2!(fn datetime(x: Text) -> Timestamp);
sql_function_v2!(fn time(x: Text) -> Time);
sql_function_v2!(fn date(x: Text) -> Date);

#[test]
fn unix_epoch_encodes_correctly() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl DefaultSchema for Pg {
}

#[cfg(feature = "mysql")]
sql_function!(fn database() -> VarChar);
sql_function_v2!(fn database() -> VarChar);

#[cfg(feature = "mysql")]
impl DefaultSchema for Mysql {
Expand Down
Loading