Skip to content

Commit

Permalink
Fix most type-defs for #[auto_type] by cleaning up all the type defs
Browse files Browse the repository at this point in the history
  • Loading branch information
weiznich committed Dec 8, 2023
1 parent b51567f commit 32e1484
Show file tree
Hide file tree
Showing 22 changed files with 783 additions and 202 deletions.
2 changes: 1 addition & 1 deletion diesel/src/expression/case_when.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ use super::{AsExpression, TypedExpressionType};
/// assert_eq!(&[(1, Some(1)), (2, Some(2))], users_with_name.as_slice());
/// # }
/// ```
pub fn case_when<C, T, ST>(condition: C, if_true: T) -> helper_types::case_when<C, T, ST>
pub fn case_when<C, T, ST>(condition: C, if_true: T) -> helper_types::CaseWhen<C, T, ST>
where
C: Expression,
<C as Expression>::SqlType: BoolOrNullableBool,
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
48 changes: 42 additions & 6 deletions diesel/src/expression/functions/helper_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,55 @@ use crate::expression::grouped::Grouped;
use crate::expression::operators;

/// The return type of [`not(expr)`](crate::dsl::not())
pub type not<Expr> = operators::Not<Grouped<Expr>>;
pub type Not<Expr> = operators::Not<Grouped<Expr>>;

#[doc(hidden)]
// cannot put deprecated on this because rustc then
// also reports the function as deprecated
#[cfg(any(feature = "with-deprecated", not(feature = "without-deprecated")))]
pub type not<Expr> = Not<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::HelperType<SqlTypeOf<Expr>, Expr>;

#[doc(hidden)]
// cannot put deprecated on this because rustc then
// also reports the function as deprecated
#[cfg(any(feature = "with-deprecated", not(feature = "without-deprecated")))]
pub type max<Expr> = Max<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::HelperType<SqlTypeOf<Expr>, Expr>;

#[doc(hidden)]
// cannot put deprecated on this because rustc then
// also reports the function as deprecated
#[cfg(any(feature = "with-deprecated", not(feature = "without-deprecated")))]
pub type min<Expr> = Min<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::HelperType<SqlTypeOf<Expr>, Expr>;

#[doc(hidden)]
// cannot put deprecated on this because rustc then
// also reports the function as deprecated
#[cfg(any(feature = "with-deprecated", not(feature = "without-deprecated")))]
pub type sum<Expr> = Sum<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::HelperType<SqlTypeOf<Expr>, Expr>;

#[doc(hidden)]
// cannot put deprecated on this because rustc then
// also reports the function as deprecated
#[cfg(any(feature = "with-deprecated", not(feature = "without-deprecated")))]
pub type avg<Expr> = Avg<Expr>;

/// The return type of [`exists(expr)`](crate::dsl::exists())
pub type exists<Expr> = crate::expression::exists::Exists<Expr>;
pub type Exists<Expr> = crate::expression::exists::Exists<Expr>;

#[doc(hidden)]
// cannot put deprecated on this because rustc then
// also reports the function as deprecated
#[cfg(any(feature = "with-deprecated", not(feature = "without-deprecated")))]
pub type exists<Expr> = Exists<Expr>;
17 changes: 14 additions & 3 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,11 @@ 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)]
pub type case_when<C, T, ST = <T as Expression>::SqlType> = expression::case_when::CaseWhen<
pub type CaseWhen<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
38 changes: 33 additions & 5 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::{CaseWhen, Otherwise, When};
#[doc(inline)]
pub use super::not::not;
#[doc(inline)]
pub use super::sql_literal::sql;
Expand All @@ -73,21 +75,47 @@ 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::HelperType<SqlTypeOf<Expr>, Expr>;

#[doc(hidden)]
// cannot put deprecated on this because rustc then
// also reports the function as deprecated
#[cfg(any(feature = "with-deprecated", not(feature = "without-deprecated")))]
pub type count<Expr> = Count<Expr>;

/// The return type of [`count_star()`](crate::dsl::count_star())
pub type count_star = super::count::CountStar;
pub type CountStar = super::count::CountStar;

#[doc(hidden)]
// cannot put deprecated on this because rustc then
// also reports the function as deprecated
#[cfg(any(feature = "with-deprecated", not(feature = "without-deprecated")))]
pub type count_star = CountStar;

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

#[doc(hidden)]
// cannot put deprecated on this because rustc then
// also reports the function as deprecated
#[cfg(any(feature = "with-deprecated", not(feature = "without-deprecated")))]
pub type count_distinct<Expr> = CountDistinct<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::HelperType<Expr>;

#[doc(hidden)]
// cannot put deprecated on this because rustc then
// also reports the function as deprecated
#[cfg(any(feature = "with-deprecated", not(feature = "without-deprecated")))]
pub type date<Expr> = 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 {}
37 changes: 35 additions & 2 deletions diesel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,10 +408,10 @@ pub mod helper_types {
pub type ThenOrderBy<Source, Ordering> = <Source as ThenOrderDsl<Ordering>>::Output;

/// Represents the return type of [`.limit()`](crate::prelude::QueryDsl::limit)
pub type Limit<Source> = <Source as LimitDsl>::Output;
pub type Limit<Source, DummyArg = i64> = <Source as LimitDsl<DummyArg>>::Output;

/// Represents the return type of [`.offset()`](crate::prelude::QueryDsl::offset)
pub type Offset<Source> = <Source as OffsetDsl>::Output;
pub type Offset<Source, DummyArg = i64> = <Source as OffsetDsl<DummyArg>>::Output;

/// Represents the return type of [`.inner_join(rhs)`](crate::prelude::QueryDsl::inner_join)
pub type InnerJoin<Source, Rhs> =
Expand Down Expand Up @@ -623,6 +623,39 @@ pub mod helper_types {
#[deprecated(note = "Use `LoadQuery::RowIter` directly")]
pub type LoadIter<'conn, 'query, Q, Conn, U, B = crate::connection::DefaultLoadingMode> =
<Q as load_dsl::LoadQuery<'query, Conn, U, B>>::RowIter<'conn>;

/// Represents the return type of [`diesel::delete`]
pub type Delete<T> = crate::query_builder::DeleteStatement<
<T as HasTable>::Table,
<T as IntoUpdateTarget>::WhereClause,
>;

/// Represents the return type of [`diesel::insert_into`]
pub type InsertInto<T> = crate::query_builder::IncompleteInsertStatement<T>;

/// Represents the return type of [`diesel::insert_or_ignore_into`]
pub type InsertOrIgnoreInto<T> = crate::query_builder::IncompleteInsertOrIgnoreStatement<T>;

/// Represents the return type of [`diesel::replace_into`]
pub type ReplaceInto<T> = crate::query_builder::IncompleteReplaceStatement<T>;

/// Represents the return type of
/// [`IncompleteInsertStatement::values()`](crate::query_builder::IncompleteInsertStatement::values)
pub type Values<I, U> = crate::query_builder::InsertStatement<
<I as crate::query_builder::insert_statement::InsertAutoTypeHelper>::Table,
<U as crate::Insertable<
<I as crate::query_builder::insert_statement::InsertAutoTypeHelper>::Table,
>>::Values,
<I as crate::query_builder::insert_statement::InsertAutoTypeHelper>::Op,
>;

/// Represents the return type of
/// [`UpdateStatement::set()`](crate::query_builder::UpdateStatement::set)
pub type Set<U, V> = crate::query_builder::UpdateStatement<
<U as crate::query_builder::update_statement::UpdateAutoTypeHelper>::Table,
<U as crate::query_builder::update_statement::UpdateAutoTypeHelper>::Where,
<V as crate::AsChangeset>::Changeset,
>;
}

pub mod prelude {
Expand Down
Loading

0 comments on commit 32e1484

Please sign in to comment.