Skip to content

Commit 32e1484

Browse files
committed
Fix most type-defs for #[auto_type] by cleaning up all the type defs
1 parent b51567f commit 32e1484

22 files changed

+783
-202
lines changed

diesel/src/expression/case_when.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ use super::{AsExpression, TypedExpressionType};
6767
/// assert_eq!(&[(1, Some(1)), (2, Some(2))], users_with_name.as_slice());
6868
/// # }
6969
/// ```
70-
pub fn case_when<C, T, ST>(condition: C, if_true: T) -> helper_types::case_when<C, T, ST>
70+
pub fn case_when<C, T, ST>(condition: C, if_true: T) -> helper_types::CaseWhen<C, T, ST>
7171
where
7272
C: Expression,
7373
<C as Expression>::SqlType: BoolOrNullableBool,

diesel/src/expression/exists.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use crate::backend::{sql_dialect, Backend, SqlDialect};
55
use crate::expression::subselect::Subselect;
66
use crate::expression::{AppearsOnTable, Expression, SelectableExpression, ValidGrouping};
7-
use crate::helper_types::exists;
7+
use crate::helper_types;
88
use crate::query_builder::*;
99
use crate::result::QueryResult;
1010
use crate::sql_types::Bool;
@@ -32,7 +32,7 @@ use crate::sql_types::Bool;
3232
/// assert_eq!(Ok(false), jim_exists);
3333
/// # }
3434
/// ```
35-
pub fn exists<T>(query: T) -> exists<T> {
35+
pub fn exists<T>(query: T) -> helper_types::Exists<T> {
3636
Exists {
3737
subselect: Subselect::new(query),
3838
}

diesel/src/expression/functions/helper_types.rs

+42-6
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,55 @@ use crate::expression::grouped::Grouped;
55
use crate::expression::operators;
66

77
/// The return type of [`not(expr)`](crate::dsl::not())
8-
pub type not<Expr> = operators::Not<Grouped<Expr>>;
8+
pub type Not<Expr> = operators::Not<Grouped<Expr>>;
9+
10+
#[doc(hidden)]
11+
// cannot put deprecated on this because rustc then
12+
// also reports the function as deprecated
13+
#[cfg(any(feature = "with-deprecated", not(feature = "without-deprecated")))]
14+
pub type not<Expr> = Not<Expr>;
915

1016
/// The return type of [`max(expr)`](crate::dsl::max())
11-
pub type max<Expr> = super::aggregate_ordering::max::HelperType<SqlTypeOf<Expr>, Expr>;
17+
pub type Max<Expr> = super::aggregate_ordering::max::HelperType<SqlTypeOf<Expr>, Expr>;
18+
19+
#[doc(hidden)]
20+
// cannot put deprecated on this because rustc then
21+
// also reports the function as deprecated
22+
#[cfg(any(feature = "with-deprecated", not(feature = "without-deprecated")))]
23+
pub type max<Expr> = Max<Expr>;
1224

1325
/// The return type of [`min(expr)`](crate::dsl::min())
14-
pub type min<Expr> = super::aggregate_ordering::min::HelperType<SqlTypeOf<Expr>, Expr>;
26+
pub type Min<Expr> = super::aggregate_ordering::min::HelperType<SqlTypeOf<Expr>, Expr>;
27+
28+
#[doc(hidden)]
29+
// cannot put deprecated on this because rustc then
30+
// also reports the function as deprecated
31+
#[cfg(any(feature = "with-deprecated", not(feature = "without-deprecated")))]
32+
pub type min<Expr> = Min<Expr>;
1533

1634
/// The return type of [`sum(expr)`](crate::dsl::sum())
17-
pub type sum<Expr> = super::aggregate_folding::sum::HelperType<SqlTypeOf<Expr>, Expr>;
35+
pub type Sum<Expr> = super::aggregate_folding::sum::HelperType<SqlTypeOf<Expr>, Expr>;
36+
37+
#[doc(hidden)]
38+
// cannot put deprecated on this because rustc then
39+
// also reports the function as deprecated
40+
#[cfg(any(feature = "with-deprecated", not(feature = "without-deprecated")))]
41+
pub type sum<Expr> = Sum<Expr>;
1842

1943
/// The return type of [`avg(expr)`](crate::dsl::avg())
20-
pub type avg<Expr> = super::aggregate_folding::avg::HelperType<SqlTypeOf<Expr>, Expr>;
44+
pub type Avg<Expr> = super::aggregate_folding::avg::HelperType<SqlTypeOf<Expr>, Expr>;
45+
46+
#[doc(hidden)]
47+
// cannot put deprecated on this because rustc then
48+
// also reports the function as deprecated
49+
#[cfg(any(feature = "with-deprecated", not(feature = "without-deprecated")))]
50+
pub type avg<Expr> = Avg<Expr>;
2151

2252
/// The return type of [`exists(expr)`](crate::dsl::exists())
23-
pub type exists<Expr> = crate::expression::exists::Exists<Expr>;
53+
pub type Exists<Expr> = crate::expression::exists::Exists<Expr>;
54+
55+
#[doc(hidden)]
56+
// cannot put deprecated on this because rustc then
57+
// also reports the function as deprecated
58+
#[cfg(any(feature = "with-deprecated", not(feature = "without-deprecated")))]
59+
pub type exists<Expr> = Exists<Expr>;

diesel/src/expression/helper_types.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ pub type Eq<Lhs, Rhs> = Grouped<super::operators::Eq<Lhs, AsExpr<Rhs, Lhs>>>;
2626
/// [`lhs.ne(rhs)`](crate::expression_methods::ExpressionMethods::ne())
2727
pub type NotEq<Lhs, Rhs> = Grouped<super::operators::NotEq<Lhs, AsExpr<Rhs, Lhs>>>;
2828

29+
#[doc(hidden)] // required for `#[auto_type]`
30+
pub type Ne<Lhs, Rhs> = NotEq<Lhs, Rhs>;
31+
2932
/// The return type of
3033
/// [`lhs.eq_any(rhs)`](crate::expression_methods::ExpressionMethods::eq_any())
3134
pub type EqAny<Lhs, Rhs> = Grouped<In<Lhs, <Rhs as AsInExpression<SqlTypeOf<Lhs>>>::InExpression>>;
@@ -35,6 +38,9 @@ pub type EqAny<Lhs, Rhs> = Grouped<In<Lhs, <Rhs as AsInExpression<SqlTypeOf<Lhs>
3538
pub type NeAny<Lhs, Rhs> =
3639
Grouped<NotIn<Lhs, <Rhs as AsInExpression<SqlTypeOf<Lhs>>>::InExpression>>;
3740

41+
#[doc(hidden)] // required for `#[auto_type]`
42+
pub type NeAll<Lhs, Rhs> = NeAny<Lhs, Rhs>;
43+
3844
/// The return type of
3945
/// [`expr.is_null()`](crate::expression_methods::ExpressionMethods::is_null())
4046
pub type IsNull<Expr> = Grouped<super::operators::IsNull<Expr>>;
@@ -51,6 +57,9 @@ pub type Gt<Lhs, Rhs> = Grouped<super::operators::Gt<Lhs, AsExpr<Rhs, Lhs>>>;
5157
/// [`lhs.ge(rhs)`](crate::expression_methods::ExpressionMethods::ge())
5258
pub type GtEq<Lhs, Rhs> = Grouped<super::operators::GtEq<Lhs, AsExpr<Rhs, Lhs>>>;
5359

60+
#[doc(hidden)] // required for `#[auto_type]`
61+
pub type Ge<Lhs, Rhs> = GtEq<Lhs, Rhs>;
62+
5463
/// The return type of
5564
/// [`lhs.lt(rhs)`](crate::expression_methods::ExpressionMethods::lt())
5665
pub type Lt<Lhs, Rhs> = Grouped<super::operators::Lt<Lhs, AsExpr<Rhs, Lhs>>>;
@@ -59,6 +68,9 @@ pub type Lt<Lhs, Rhs> = Grouped<super::operators::Lt<Lhs, AsExpr<Rhs, Lhs>>>;
5968
/// [`lhs.le(rhs)`](crate::expression_methods::ExpressionMethods::le())
6069
pub type LtEq<Lhs, Rhs> = Grouped<super::operators::LtEq<Lhs, AsExpr<Rhs, Lhs>>>;
6170

71+
#[doc(hidden)] // required for `#[auto_type]`
72+
pub type Le<Lhs, Rhs> = LtEq<Lhs, Rhs>;
73+
6274
/// The return type of
6375
/// [`lhs.between(lower, upper)`](crate::expression_methods::ExpressionMethods::between())
6476
pub type Between<Lhs, Lower, Upper> = Grouped<
@@ -122,12 +134,11 @@ pub type Like<Lhs, Rhs> = Grouped<super::operators::Like<Lhs, AsExprOf<Rhs, SqlT
122134
pub type NotLike<Lhs, Rhs> = Grouped<super::operators::NotLike<Lhs, AsExprOf<Rhs, SqlTypeOf<Lhs>>>>;
123135

124136
/// The return type of [`case_when()`](expression::case_when::case_when)
125-
#[allow(non_camel_case_types)]
126-
pub type case_when<C, T, ST = <T as Expression>::SqlType> = expression::case_when::CaseWhen<
137+
pub type CaseWhen<C, T, ST = <T as Expression>::SqlType> = expression::case_when::CaseWhen<
127138
expression::case_when::CaseWhenConditionsLeaf<Grouped<C>, Grouped<AsExprOf<T, ST>>>,
128139
expression::case_when::NoElseExpression,
129140
>;
130-
/// The return type of [`case_when(...).when(...)`](expression::case_when::CaseWhen::when)
141+
/// The return type of [`case_when(...).when(...)`](expression::CaseWhen::when)
131142
pub type When<W, C, T> = expression::case_when::CaseWhen<
132143
expression::case_when::CaseWhenConditionsIntermediateNode<
133144
Grouped<C>,

diesel/src/expression/mod.rs

+33-5
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub(crate) mod dsl {
5353
use crate::dsl::SqlTypeOf;
5454

5555
#[doc(inline)]
56-
pub use super::case_when::*;
56+
pub use super::case_when::case_when;
5757
#[doc(inline)]
5858
pub use super::count::*;
5959
#[doc(inline)]
@@ -65,6 +65,8 @@ pub(crate) mod dsl {
6565
#[doc(inline)]
6666
pub use super::functions::date_and_time::*;
6767
#[doc(inline)]
68+
pub use super::helper_types::{CaseWhen, Otherwise, When};
69+
#[doc(inline)]
6870
pub use super::not::not;
6971
#[doc(inline)]
7072
pub use super::sql_literal::sql;
@@ -73,21 +75,47 @@ pub(crate) mod dsl {
7375
pub use crate::pg::expression::dsl::*;
7476

7577
/// The return type of [`count(expr)`](crate::dsl::count())
76-
pub type count<Expr> = super::count::count::HelperType<SqlTypeOf<Expr>, Expr>;
78+
pub type Count<Expr> = super::count::count::HelperType<SqlTypeOf<Expr>, Expr>;
79+
80+
#[doc(hidden)]
81+
// cannot put deprecated on this because rustc then
82+
// also reports the function as deprecated
83+
#[cfg(any(feature = "with-deprecated", not(feature = "without-deprecated")))]
84+
pub type count<Expr> = Count<Expr>;
7785

7886
/// The return type of [`count_star()`](crate::dsl::count_star())
79-
pub type count_star = super::count::CountStar;
87+
pub type CountStar = super::count::CountStar;
88+
89+
#[doc(hidden)]
90+
// cannot put deprecated on this because rustc then
91+
// also reports the function as deprecated
92+
#[cfg(any(feature = "with-deprecated", not(feature = "without-deprecated")))]
93+
pub type count_star = CountStar;
8094

8195
/// The return type of [`count_distinct()`](crate::dsl::count_distinct())
82-
pub type count_distinct<Expr> = super::count::CountDistinct<SqlTypeOf<Expr>, Expr>;
96+
pub type CountDistinct<Expr> = super::count::CountDistinct<SqlTypeOf<Expr>, Expr>;
97+
98+
#[doc(hidden)]
99+
// cannot put deprecated on this because rustc then
100+
// also reports the function as deprecated
101+
#[cfg(any(feature = "with-deprecated", not(feature = "without-deprecated")))]
102+
pub type count_distinct<Expr> = CountDistinct<Expr>;
83103

84104
/// The return type of [`date(expr)`](crate::dsl::date())
85-
pub type date<Expr> = super::functions::date_and_time::date::HelperType<Expr>;
105+
pub type Date<Expr> = super::functions::date_and_time::date::HelperType<Expr>;
106+
107+
#[doc(hidden)]
108+
// cannot put deprecated on this because rustc then
109+
// also reports the function as deprecated
110+
#[cfg(any(feature = "with-deprecated", not(feature = "without-deprecated")))]
111+
pub type date<Expr> = Date<Expr>;
86112

87113
#[cfg(feature = "mysql_backend")]
88114
pub use crate::mysql::query_builder::DuplicatedKeys;
89115
}
90116

117+
#[doc(inline)]
118+
pub use self::case_when::CaseWhen;
91119
#[doc(inline)]
92120
pub use self::sql_literal::{SqlLiteral, UncheckedBind};
93121

diesel/src/expression/not.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::expression::grouped::Grouped;
22
use crate::expression::Expression;
3-
use crate::helper_types::not;
3+
use crate::helper_types;
44
use crate::sql_types::BoolOrNullableBool;
55

66
/// Creates a SQL `NOT` expression
@@ -23,7 +23,7 @@ use crate::sql_types::BoolOrNullableBool;
2323
/// assert_eq!(Ok(2), users_not_with_name.first(connection));
2424
/// # }
2525
/// ```
26-
pub fn not<T>(expr: T) -> not<T>
26+
pub fn not<T>(expr: T) -> helper_types::Not<T>
2727
where
2828
T: Expression,
2929
<T as Expression>::SqlType: BoolOrNullableBool,

diesel/src/expression/operators.rs

+103-1
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,6 @@ infix_operator!(Escape, " ESCAPE ");
529529
infix_operator!(Eq, " = ");
530530
infix_operator!(Gt, " > ");
531531
infix_operator!(GtEq, " >= ");
532-
infix_operator!(Like, " LIKE ");
533532
infix_operator!(Lt, " < ");
534533
infix_operator!(LtEq, " <= ");
535534
infix_operator!(NotEq, " != ");
@@ -645,3 +644,106 @@ where
645644
Ok(())
646645
}
647646
}
647+
648+
// need an explicit impl here to control which types are allowed
649+
#[derive(
650+
Debug,
651+
Clone,
652+
Copy,
653+
crate::query_builder::QueryId,
654+
crate::sql_types::DieselNumericOps,
655+
crate::expression::ValidGrouping,
656+
)]
657+
#[doc(hidden)]
658+
pub struct Like<T, U> {
659+
pub(crate) left: T,
660+
pub(crate) right: U,
661+
}
662+
663+
impl<T, U> Like<T, U> {
664+
pub(crate) fn new(left: T, right: U) -> Self {
665+
Like { left, right }
666+
}
667+
}
668+
669+
impl<T, U, QS> crate::expression::SelectableExpression<QS> for Like<T, U>
670+
where
671+
Like<T, U>: crate::expression::AppearsOnTable<QS>,
672+
T: crate::expression::SelectableExpression<QS>,
673+
U: crate::expression::SelectableExpression<QS>,
674+
{
675+
}
676+
677+
impl<T, U, QS> crate::expression::AppearsOnTable<QS> for Like<T, U>
678+
where
679+
Like<T, U>: crate::expression::Expression,
680+
T: crate::expression::AppearsOnTable<QS>,
681+
U: crate::expression::AppearsOnTable<QS>,
682+
{
683+
}
684+
685+
impl<T, U> crate::expression::Expression for Like<T, U>
686+
where
687+
T: crate::expression::Expression,
688+
U: crate::expression::Expression,
689+
<T as crate::expression::Expression>::SqlType: crate::sql_types::SqlType,
690+
<U as crate::expression::Expression>::SqlType: crate::sql_types::SqlType,
691+
crate::sql_types::is_nullable::IsSqlTypeNullable<<T as crate::expression::Expression>::SqlType>:
692+
crate::sql_types::OneIsNullable<
693+
crate::sql_types::is_nullable::IsSqlTypeNullable<
694+
<U as crate::expression::Expression>::SqlType,
695+
>,
696+
>,
697+
crate::sql_types::is_nullable::IsOneNullable<
698+
<T as crate::expression::Expression>::SqlType,
699+
<U as crate::expression::Expression>::SqlType,
700+
>: crate::sql_types::MaybeNullableType<crate::sql_types::Bool>,
701+
{
702+
type SqlType = crate::sql_types::is_nullable::MaybeNullable<
703+
crate::sql_types::is_nullable::IsOneNullable<
704+
<T as crate::expression::Expression>::SqlType,
705+
<U as crate::expression::Expression>::SqlType,
706+
>,
707+
crate::sql_types::Bool,
708+
>;
709+
}
710+
711+
impl<T, U, DB> crate::query_builder::QueryFragment<DB> for Like<T, U>
712+
where
713+
T: crate::query_builder::QueryFragment<DB> + crate::Expression,
714+
U: crate::query_builder::QueryFragment<DB>,
715+
DB: crate::backend::Backend,
716+
DB: LikeIsAllowedForType<T::SqlType>,
717+
{
718+
fn walk_ast<'b>(
719+
&'b self,
720+
mut out: crate::query_builder::AstPass<'_, 'b, DB>,
721+
) -> crate::result::QueryResult<()> {
722+
(self.left.walk_ast(out.reborrow())?);
723+
(out.push_sql(" LIKE "));
724+
(self.right.walk_ast(out.reborrow())?);
725+
Ok(())
726+
}
727+
}
728+
729+
impl<S, T, U> crate::internal::operators_macro::FieldAliasMapper<S> for Like<T, U>
730+
where
731+
S: crate::query_source::AliasSource,
732+
T: crate::internal::operators_macro::FieldAliasMapper<S>,
733+
U: crate::internal::operators_macro::FieldAliasMapper<S>,
734+
{
735+
type Out = Like<
736+
<T as crate::internal::operators_macro::FieldAliasMapper<S>>::Out,
737+
<U as crate::internal::operators_macro::FieldAliasMapper<S>>::Out,
738+
>;
739+
fn map(self, alias: &crate::query_source::Alias<S>) -> Self::Out {
740+
Like {
741+
left: self.left.map(alias),
742+
right: self.right.map(alias),
743+
}
744+
}
745+
}
746+
747+
pub trait LikeIsAllowedForType<ST>: Backend {}
748+
749+
impl<DB> LikeIsAllowedForType<crate::sql_types::Text> for DB where DB: Backend {}

diesel/src/lib.rs

+35-2
Original file line numberDiff line numberDiff line change
@@ -408,10 +408,10 @@ pub mod helper_types {
408408
pub type ThenOrderBy<Source, Ordering> = <Source as ThenOrderDsl<Ordering>>::Output;
409409

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

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

416416
/// Represents the return type of [`.inner_join(rhs)`](crate::prelude::QueryDsl::inner_join)
417417
pub type InnerJoin<Source, Rhs> =
@@ -623,6 +623,39 @@ pub mod helper_types {
623623
#[deprecated(note = "Use `LoadQuery::RowIter` directly")]
624624
pub type LoadIter<'conn, 'query, Q, Conn, U, B = crate::connection::DefaultLoadingMode> =
625625
<Q as load_dsl::LoadQuery<'query, Conn, U, B>>::RowIter<'conn>;
626+
627+
/// Represents the return type of [`diesel::delete`]
628+
pub type Delete<T> = crate::query_builder::DeleteStatement<
629+
<T as HasTable>::Table,
630+
<T as IntoUpdateTarget>::WhereClause,
631+
>;
632+
633+
/// Represents the return type of [`diesel::insert_into`]
634+
pub type InsertInto<T> = crate::query_builder::IncompleteInsertStatement<T>;
635+
636+
/// Represents the return type of [`diesel::insert_or_ignore_into`]
637+
pub type InsertOrIgnoreInto<T> = crate::query_builder::IncompleteInsertOrIgnoreStatement<T>;
638+
639+
/// Represents the return type of [`diesel::replace_into`]
640+
pub type ReplaceInto<T> = crate::query_builder::IncompleteReplaceStatement<T>;
641+
642+
/// Represents the return type of
643+
/// [`IncompleteInsertStatement::values()`](crate::query_builder::IncompleteInsertStatement::values)
644+
pub type Values<I, U> = crate::query_builder::InsertStatement<
645+
<I as crate::query_builder::insert_statement::InsertAutoTypeHelper>::Table,
646+
<U as crate::Insertable<
647+
<I as crate::query_builder::insert_statement::InsertAutoTypeHelper>::Table,
648+
>>::Values,
649+
<I as crate::query_builder::insert_statement::InsertAutoTypeHelper>::Op,
650+
>;
651+
652+
/// Represents the return type of
653+
/// [`UpdateStatement::set()`](crate::query_builder::UpdateStatement::set)
654+
pub type Set<U, V> = crate::query_builder::UpdateStatement<
655+
<U as crate::query_builder::update_statement::UpdateAutoTypeHelper>::Table,
656+
<U as crate::query_builder::update_statement::UpdateAutoTypeHelper>::Where,
657+
<V as crate::AsChangeset>::Changeset,
658+
>;
626659
}
627660

628661
pub mod prelude {

0 commit comments

Comments
 (0)