Skip to content
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

Fix most type-defs for #[auto_type] by cleaning up all the type defs #3783

Merged
merged 15 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from 14 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Increasing the minimal supported Rust version will always be coupled at least wi
### Changed

* The minimal officially supported rustc version is now 1.70.0
* Deprecated `sql_function!` in favour of `define_sql_function!` which provides compatibility with `#[dsl::auto_type]`

## [2.1.0] 2023-05-26

Expand Down
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::define_sql_function;
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! {
define_sql_function! {
/// Creates a SQL `COUNT` expression
///
/// As with most bare functions, this is not exported by default. You can import
Expand Down
4 changes: 2 additions & 2 deletions diesel/src/expression/exists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use crate::backend::{sql_dialect, Backend, SqlDialect};
use crate::expression::subselect::Subselect;
use crate::expression::{AppearsOnTable, Expression, SelectableExpression, ValidGrouping};
use crate::helper_types::exists;
use crate::helper_types;
use crate::query_builder::*;
use crate::result::QueryResult;
use crate::sql_types::Bool;
Expand Down Expand Up @@ -32,7 +32,7 @@ use crate::sql_types::Bool;
/// assert_eq!(Ok(false), jim_exists);
/// # }
/// ```
pub fn exists<T>(query: T) -> exists<T> {
pub fn exists<T>(query: T) -> helper_types::exists<T> {
Exists {
subselect: Subselect::new(query),
}
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::define_sql_function;
use crate::sql_types::Foldable;

sql_function! {
define_sql_function! {
/// 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! {
define_sql_function! {
/// 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::define_sql_function;

sql_function! {
define_sql_function! {
/// 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! {
define_sql_function! {
/// 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::define_sql_function;
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! {
define_sql_function! {
/// 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>;
6 changes: 5 additions & 1 deletion diesel/src/expression/functions/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
//! Helper macros to define custom sql functions

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

#[doc(inline)]
#[cfg(all(feature = "with-deprecated", not(feature = "without-deprecated")))]
pub use diesel_derives::sql_function_proc as sql_function;

#[macro_export]
Expand Down Expand Up @@ -73,7 +77,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 `define_sql_function!` instead. See `CHANGELOG.md` for migration instructions"
)]
#[cfg(all(feature = "with-deprecated", not(feature = "without-deprecated")))]
macro_rules! no_arg_sql_function {
Expand Down
16 changes: 14 additions & 2 deletions diesel/src/expression/helper_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ pub type Eq<Lhs, Rhs> = Grouped<super::operators::Eq<Lhs, AsExpr<Rhs, Lhs>>>;
/// [`lhs.ne(rhs)`](crate::expression_methods::ExpressionMethods::ne())
pub type NotEq<Lhs, Rhs> = Grouped<super::operators::NotEq<Lhs, AsExpr<Rhs, Lhs>>>;

#[doc(hidden)] // required for `#[auto_type]`
pub type Ne<Lhs, Rhs> = NotEq<Lhs, Rhs>;

/// The return type of
/// [`lhs.eq_any(rhs)`](crate::expression_methods::ExpressionMethods::eq_any())
pub type EqAny<Lhs, Rhs> = Grouped<In<Lhs, <Rhs as AsInExpression<SqlTypeOf<Lhs>>>::InExpression>>;
Expand All @@ -35,6 +38,9 @@ pub type EqAny<Lhs, Rhs> = Grouped<In<Lhs, <Rhs as AsInExpression<SqlTypeOf<Lhs>
pub type NeAny<Lhs, Rhs> =
Grouped<NotIn<Lhs, <Rhs as AsInExpression<SqlTypeOf<Lhs>>>::InExpression>>;

#[doc(hidden)] // required for `#[auto_type]`
pub type NeAll<Lhs, Rhs> = NeAny<Lhs, Rhs>;

/// The return type of
/// [`expr.is_null()`](crate::expression_methods::ExpressionMethods::is_null())
pub type IsNull<Expr> = Grouped<super::operators::IsNull<Expr>>;
Expand All @@ -51,6 +57,9 @@ pub type Gt<Lhs, Rhs> = Grouped<super::operators::Gt<Lhs, AsExpr<Rhs, Lhs>>>;
/// [`lhs.ge(rhs)`](crate::expression_methods::ExpressionMethods::ge())
pub type GtEq<Lhs, Rhs> = Grouped<super::operators::GtEq<Lhs, AsExpr<Rhs, Lhs>>>;

#[doc(hidden)] // required for `#[auto_type]`
pub type Ge<Lhs, Rhs> = GtEq<Lhs, Rhs>;

/// The return type of
/// [`lhs.lt(rhs)`](crate::expression_methods::ExpressionMethods::lt())
pub type Lt<Lhs, Rhs> = Grouped<super::operators::Lt<Lhs, AsExpr<Rhs, Lhs>>>;
Expand All @@ -59,6 +68,9 @@ pub type Lt<Lhs, Rhs> = Grouped<super::operators::Lt<Lhs, AsExpr<Rhs, Lhs>>>;
/// [`lhs.le(rhs)`](crate::expression_methods::ExpressionMethods::le())
pub type LtEq<Lhs, Rhs> = Grouped<super::operators::LtEq<Lhs, AsExpr<Rhs, Lhs>>>;

#[doc(hidden)] // required for `#[auto_type]`
pub type Le<Lhs, Rhs> = LtEq<Lhs, Rhs>;

/// The return type of
/// [`lhs.between(lower, upper)`](crate::expression_methods::ExpressionMethods::between())
pub type Between<Lhs, Lower, Upper> = Grouped<
Expand Down Expand Up @@ -122,12 +134,12 @@ pub type Like<Lhs, Rhs> = Grouped<super::operators::Like<Lhs, AsExprOf<Rhs, SqlT
pub type NotLike<Lhs, Rhs> = Grouped<super::operators::NotLike<Lhs, AsExprOf<Rhs, SqlTypeOf<Lhs>>>>;

/// The return type of [`case_when()`](expression::case_when::case_when)
#[allow(non_camel_case_types)]
#[allow(non_camel_case_types)] // required for `#[auto_type]`
pub type case_when<C, T, ST = <T as Expression>::SqlType> = expression::case_when::CaseWhen<
expression::case_when::CaseWhenConditionsLeaf<Grouped<C>, Grouped<AsExprOf<T, ST>>>,
expression::case_when::NoElseExpression,
>;
/// The return type of [`case_when(...).when(...)`](expression::case_when::CaseWhen::when)
/// The return type of [`case_when(...).when(...)`](expression::CaseWhen::when)
pub type When<W, C, T> = expression::case_when::CaseWhen<
expression::case_when::CaseWhenConditionsIntermediateNode<
Grouped<C>,
Expand Down
10 changes: 7 additions & 3 deletions diesel/src/expression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub(crate) mod dsl {
use crate::dsl::SqlTypeOf;

#[doc(inline)]
pub use super::case_when::*;
pub use super::case_when::case_when;
#[doc(inline)]
pub use super::count::*;
#[doc(inline)]
Expand All @@ -65,6 +65,8 @@ pub(crate) mod dsl {
#[doc(inline)]
pub use super::functions::date_and_time::*;
#[doc(inline)]
pub use super::helper_types::{case_when, Otherwise, When};
#[doc(inline)]
pub use super::not::not;
#[doc(inline)]
pub use super::sql_literal::sql;
Expand All @@ -73,7 +75,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,12 +84,14 @@ 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;
}

#[doc(inline)]
pub use self::case_when::CaseWhen;
#[doc(inline)]
pub use self::sql_literal::{SqlLiteral, UncheckedBind};

Expand Down
4 changes: 2 additions & 2 deletions diesel/src/expression/not.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::expression::grouped::Grouped;
use crate::expression::Expression;
use crate::helper_types::not;
use crate::helper_types;
use crate::sql_types::BoolOrNullableBool;

/// Creates a SQL `NOT` expression
Expand All @@ -23,7 +23,7 @@ use crate::sql_types::BoolOrNullableBool;
/// assert_eq!(Ok(2), users_not_with_name.first(connection));
/// # }
/// ```
pub fn not<T>(expr: T) -> not<T>
pub fn not<T>(expr: T) -> helper_types::not<T>
where
T: Expression,
<T as Expression>::SqlType: BoolOrNullableBool,
Expand Down
104 changes: 103 additions & 1 deletion diesel/src/expression/operators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,6 @@ infix_operator!(Escape, " ESCAPE ");
infix_operator!(Eq, " = ");
infix_operator!(Gt, " > ");
infix_operator!(GtEq, " >= ");
infix_operator!(Like, " LIKE ");
infix_operator!(Lt, " < ");
infix_operator!(LtEq, " <= ");
infix_operator!(NotEq, " != ");
Expand Down Expand Up @@ -645,3 +644,106 @@ where
Ok(())
}
}

// need an explicit impl here to control which types are allowed
#[derive(
Debug,
Clone,
Copy,
crate::query_builder::QueryId,
crate::sql_types::DieselNumericOps,
crate::expression::ValidGrouping,
)]
#[doc(hidden)]
pub struct Like<T, U> {
pub(crate) left: T,
pub(crate) right: U,
}

impl<T, U> Like<T, U> {
pub(crate) fn new(left: T, right: U) -> Self {
Like { left, right }
}
}

impl<T, U, QS> crate::expression::SelectableExpression<QS> for Like<T, U>
where
Like<T, U>: crate::expression::AppearsOnTable<QS>,
T: crate::expression::SelectableExpression<QS>,
U: crate::expression::SelectableExpression<QS>,
{
}

impl<T, U, QS> crate::expression::AppearsOnTable<QS> for Like<T, U>
where
Like<T, U>: crate::expression::Expression,
T: crate::expression::AppearsOnTable<QS>,
U: crate::expression::AppearsOnTable<QS>,
{
}

impl<T, U> crate::expression::Expression for Like<T, U>
where
T: crate::expression::Expression,
U: crate::expression::Expression,
<T as crate::expression::Expression>::SqlType: crate::sql_types::SqlType,
<U as crate::expression::Expression>::SqlType: crate::sql_types::SqlType,
crate::sql_types::is_nullable::IsSqlTypeNullable<<T as crate::expression::Expression>::SqlType>:
crate::sql_types::OneIsNullable<
crate::sql_types::is_nullable::IsSqlTypeNullable<
<U as crate::expression::Expression>::SqlType,
>,
>,
crate::sql_types::is_nullable::IsOneNullable<
<T as crate::expression::Expression>::SqlType,
<U as crate::expression::Expression>::SqlType,
>: crate::sql_types::MaybeNullableType<crate::sql_types::Bool>,
{
type SqlType = crate::sql_types::is_nullable::MaybeNullable<
crate::sql_types::is_nullable::IsOneNullable<
<T as crate::expression::Expression>::SqlType,
<U as crate::expression::Expression>::SqlType,
>,
crate::sql_types::Bool,
>;
}

impl<T, U, DB> crate::query_builder::QueryFragment<DB> for Like<T, U>
where
T: crate::query_builder::QueryFragment<DB> + crate::Expression,
U: crate::query_builder::QueryFragment<DB>,
DB: crate::backend::Backend,
DB: LikeIsAllowedForType<T::SqlType>,
{
fn walk_ast<'b>(
&'b self,
mut out: crate::query_builder::AstPass<'_, 'b, DB>,
) -> crate::result::QueryResult<()> {
(self.left.walk_ast(out.reborrow())?);
(out.push_sql(" LIKE "));
(self.right.walk_ast(out.reborrow())?);
Ok(())
}
}

impl<S, T, U> crate::internal::operators_macro::FieldAliasMapper<S> for Like<T, U>
where
S: crate::query_source::AliasSource,
T: crate::internal::operators_macro::FieldAliasMapper<S>,
U: crate::internal::operators_macro::FieldAliasMapper<S>,
{
type Out = Like<
<T as crate::internal::operators_macro::FieldAliasMapper<S>>::Out,
<U as crate::internal::operators_macro::FieldAliasMapper<S>>::Out,
>;
fn map(self, alias: &crate::query_source::Alias<S>) -> Self::Out {
Like {
left: self.left.map(alias),
right: self.right.map(alias),
}
}
}

pub trait LikeIsAllowedForType<ST>: Backend {}

impl<DB> LikeIsAllowedForType<crate::sql_types::Text> for DB where DB: Backend {}
Loading
Loading