Skip to content

Commit

Permalink
update uses inside Diesel to sql_function_v2
Browse files Browse the repository at this point in the history
that should be almost always backwards compatible because the module
was only pub(crate) anyway.
It is not strictly backwards compatible because diesel-rs#3745 (comment)
  • Loading branch information
Ten0 committed Sep 1, 2023
1 parent 5ece64e commit 6c635de
Show file tree
Hide file tree
Showing 33 changed files with 103 additions and 95 deletions.
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 @@ -70,7 +70,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 @@ -79,7 +79,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 @@ -639,7 +639,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 @@ -489,7 +489,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; }
12 changes: 6 additions & 6 deletions diesel/src/sqlite/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ 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() {
Expand All @@ -511,7 +511,7 @@ 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() {
Expand All @@ -522,7 +522,7 @@ mod tests {
assert_eq!(Ok(3), added);
}

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

#[test]
fn register_noarg_function() {
Expand All @@ -542,7 +542,7 @@ mod tests {
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() {
Expand All @@ -561,7 +561,7 @@ mod tests {

use crate::sqlite::SqliteAggregateFunction;

sql_function! {
sql_function_v2! {
#[aggregate]
fn my_sum(expr: Integer) -> Integer;
}
Expand Down Expand Up @@ -631,7 +631,7 @@ mod tests {
assert_eq!(Ok(0), result);
}

sql_function! {
sql_function_v2! {
#[aggregate]
fn range_max(expr1: Integer, expr2: Integer, expr3: Integer) -> Nullable<Integer>;
}
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 @@ -20,9 +20,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 @@ -32,7 +32,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
2 changes: 1 addition & 1 deletion diesel_cli/src/infer_schema_internals/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use super::information_schema::DefaultSchema;
use super::table_data::TableName;
use crate::print_schema::ColumnSorting;

diesel::sql_function! {
diesel::sql_function_v2! {
#[sql_name = "NULLIF"]
fn null_if_text(lhs: sql_types::Text, rhs: sql_types::Text) -> sql_types::Nullable<sql_types::Text>
}
Expand Down
6 changes: 3 additions & 3 deletions diesel_cli/src/infer_schema_internals/pg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ fn regclass(table: &TableName) -> Regclass<AsExprOf<String, sql_types::Text>> {
))
}

diesel::sql_function!(fn col_description(table: sql_types::Oid, column_number: sql_types::BigInt) -> sql_types::Nullable<sql_types::Text>);
diesel::sql_function_v2!(fn col_description(table: sql_types::Oid, column_number: sql_types::BigInt) -> sql_types::Nullable<sql_types::Text>);

pub fn get_table_data(
conn: &mut PgConnection,
Expand Down Expand Up @@ -140,7 +140,7 @@ where
}
}

sql_function!(fn obj_description(oid: sql_types::Oid, catalog: sql_types::Text) -> Nullable<Text>);
sql_function_v2!(fn obj_description(oid: sql_types::Oid, catalog: sql_types::Text) -> Nullable<Text>);

pub fn get_table_comment(
conn: &mut PgConnection,
Expand All @@ -167,7 +167,7 @@ mod information_schema {
}
}

sql_function! {
sql_function_v2! {
#[aggregate]
fn array_agg(input: diesel::sql_types::Text) -> diesel::sql_types::Array<diesel::sql_types::Text>;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ table! {
}
}

sql_function!(fn f(x: Nullable<Integer>, y: Nullable<Integer>) -> Nullable<Integer>);
sql_function_v2!(fn f(x: Nullable<Integer>, y: Nullable<Integer>) -> Nullable<Integer>);

fn main() {
use self::users::dsl::*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ error[E0277]: the trait bound `diesel::expression::is_aggregate::No: MixedAggreg
note: required for `__Derived<nullable_int_col, max<Nullable<Integer>, nullable_int_col>>` to implement `ValidGrouping<()>`
--> tests/fail/cannot_mix_aggregate_and_non_aggregate_selects.rs:14:1
|
14 | sql_function!(fn f(x: Nullable<Integer>, y: Nullable<Integer>) -> Nullable<Integer>);
14 | sql_function_v2!(fn f(x: Nullable<Integer>, y: Nullable<Integer>) -> Nullable<Integer>);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unsatisfied trait bound introduced in this `derive` macro
= note: 1 redundant requirement hidden
= note: required for `f<nullable_int_col, max<Nullable<Integer>, nullable_int_col>>` to implement `ValidGrouping<()>`
= note: required for `SelectStatement<FromClause<users::table>>` to implement `SelectDsl<f::f<columns::nullable_int_col, diesel::expression::functions::aggregate_ordering::max::max<diesel::sql_types::Nullable<diesel::sql_types::Integer>, columns::nullable_int_col>>>`
= note: this error originates in the macro `sql_function` which comes from the expansion of the derive macro `ValidGrouping` (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this error originates in the macro `sql_function_v2` which comes from the expansion of the derive macro `ValidGrouping` (in Nightly builds, run with -Z macro-backtrace for more info)
Loading

0 comments on commit 6c635de

Please sign in to comment.