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 Sep 14, 2023
1 parent ac0b075 commit 9a04494
Show file tree
Hide file tree
Showing 19 changed files with 727 additions and 182 deletions.
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 deperecated
#[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 deperecated
#[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 deperecated
#[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 deperecated
#[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 deperecated
#[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 deperecated
#[cfg(any(feature = "with-deprecated", not(feature = "without-deprecated")))]
pub type exists<Expr> = Exists<Expr>;
12 changes: 12 additions & 0 deletions diesel/src/expression/helper_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,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)]
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 @@ -34,6 +37,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 @@ -50,6 +56,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 @@ -58,6 +67,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
32 changes: 28 additions & 4 deletions diesel/src/expression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,40 @@ 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 deperecated
#[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 deperecated
#[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 deperecated
#[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 deperecated
#[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;
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
102 changes: 101 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,104 @@ where
Ok(())
}
}

// need an explict 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 {}
54 changes: 51 additions & 3 deletions diesel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ pub use diesel_derives::{

pub use diesel_derives::MultiConnection;

#[allow(unknown_lints, ambiguous_glob_reexports)]
#[allow(unknown_lints, ambiguous_glob_reexports, missing_docs)]
pub mod dsl {
//! Includes various helper types and bare functions which are named too
//! generically to be included in prelude, but are often used when using Diesel.
Expand All @@ -335,6 +335,7 @@ pub mod dsl {
pub use diesel_derives::auto_type;
}

#[allow(missing_docs)]
pub mod helper_types {
//! Provide helper types for concisely writing the return type of functions.
//! As with iterators, it is unfortunately difficult to return a partially
Expand Down Expand Up @@ -407,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 @@ -622,6 +623,53 @@ 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>;

/// todo
pub type Delete<T> = crate::query_builder::DeleteStatement<
<T as HasTable>::Table,
<T as IntoUpdateTarget>::WhereClause,
>;

pub type InsertInto<T> = crate::query_builder::IncompleteInsertStatement<T>;

pub type InsertOrIgnoreInto<T> = crate::query_builder::IncompleteInsertOrIgnoreStatement<T>;
pub type ReplaceInto<T> = crate::query_builder::IncompleteReplaceStatement<T>;

pub trait InsertAutoTypeHelper {
type Table;
type Op;
}

impl<T, Op> InsertAutoTypeHelper for crate::query_builder::IncompleteInsertStatement<T, Op> {
type Table = T;
type Op = Op;
}

pub type Values<I, U> = crate::query_builder::InsertStatement<
<I as InsertAutoTypeHelper>::Table,
<U as crate::Insertable<<I as InsertAutoTypeHelper>::Table>>::Values,
<I as InsertAutoTypeHelper>::Op,
>;

pub trait UpdateAutoTypeHelper {
type Table;
type Where;
}

impl<T, W> UpdateAutoTypeHelper for crate::query_builder::UpdateStatement<T, W>
where
T: crate::QuerySource,
{
type Table = T;

type Where = W;
}

pub type Set<U, V> = crate::query_builder::UpdateStatement<
<U as UpdateAutoTypeHelper>::Table,
<U as UpdateAutoTypeHelper>::Where,
<V as crate::AsChangeset>::Changeset,
>;
}

pub mod prelude {
Expand Down
3 changes: 3 additions & 0 deletions diesel/src/pg/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use super::query_builder::PgQueryBuilder;
use super::{PgMetadataLookup, PgValue};
use crate::backend::*;
use crate::deserialize::Queryable;
use crate::expression::operators::LikeIsAllowedForType;
use crate::pg::metadata_lookup::PgMetadataCacheKey;
use crate::query_builder::bind_collector::RawBytesBindCollector;
use crate::sql_types::TypeMetadata;
Expand Down Expand Up @@ -154,3 +155,5 @@ impl sql_dialect::on_conflict_clause::PgLikeOnConflictClause for PgOnConflictCla

#[derive(Debug, Copy, Clone)]
pub struct PgStyleArrayComparison;

impl LikeIsAllowedForType<crate::sql_types::Binary> for Pg {}
Loading

0 comments on commit 9a04494

Please sign in to comment.