Skip to content

Commit 4402f6d

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

21 files changed

+763
-199
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

+13-2
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)]
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,8 +134,7 @@ 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
>;

diesel/src/expression/mod.rs

+31-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,16 +75,40 @@ 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;

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

+101-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,104 @@ 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+
impl<T, U> Like<T, U> {
663+
pub(crate) fn new(left: T, right: U) -> Self {
664+
Like { left, right }
665+
}
666+
}
667+
impl<T, U, QS> crate::expression::SelectableExpression<QS> for Like<T, U>
668+
where
669+
Like<T, U>: crate::expression::AppearsOnTable<QS>,
670+
T: crate::expression::SelectableExpression<QS>,
671+
U: crate::expression::SelectableExpression<QS>,
672+
{
673+
}
674+
675+
impl<T, U, QS> crate::expression::AppearsOnTable<QS> for Like<T, U>
676+
where
677+
Like<T, U>: crate::expression::Expression,
678+
T: crate::expression::AppearsOnTable<QS>,
679+
U: crate::expression::AppearsOnTable<QS>,
680+
{
681+
}
682+
683+
impl<T, U> crate::expression::Expression for Like<T, U>
684+
where
685+
T: crate::expression::Expression,
686+
U: crate::expression::Expression,
687+
<T as crate::expression::Expression>::SqlType: crate::sql_types::SqlType,
688+
<U as crate::expression::Expression>::SqlType: crate::sql_types::SqlType,
689+
crate::sql_types::is_nullable::IsSqlTypeNullable<<T as crate::expression::Expression>::SqlType>:
690+
crate::sql_types::OneIsNullable<
691+
crate::sql_types::is_nullable::IsSqlTypeNullable<
692+
<U as crate::expression::Expression>::SqlType,
693+
>,
694+
>,
695+
crate::sql_types::is_nullable::IsOneNullable<
696+
<T as crate::expression::Expression>::SqlType,
697+
<U as crate::expression::Expression>::SqlType,
698+
>: crate::sql_types::MaybeNullableType<crate::sql_types::Bool>,
699+
{
700+
type SqlType = crate::sql_types::is_nullable::MaybeNullable<
701+
crate::sql_types::is_nullable::IsOneNullable<
702+
<T as crate::expression::Expression>::SqlType,
703+
<U as crate::expression::Expression>::SqlType,
704+
>,
705+
crate::sql_types::Bool,
706+
>;
707+
}
708+
709+
impl<T, U, DB> crate::query_builder::QueryFragment<DB> for Like<T, U>
710+
where
711+
T: crate::query_builder::QueryFragment<DB> + crate::Expression,
712+
U: crate::query_builder::QueryFragment<DB>,
713+
DB: crate::backend::Backend,
714+
DB: LikeIsAllowedForType<T::SqlType>,
715+
{
716+
fn walk_ast<'b>(
717+
&'b self,
718+
mut out: crate::query_builder::AstPass<'_, 'b, DB>,
719+
) -> crate::result::QueryResult<()> {
720+
(self.left.walk_ast(out.reborrow())?);
721+
(out.push_sql(" LIKE "));
722+
(self.right.walk_ast(out.reborrow())?);
723+
Ok(())
724+
}
725+
}
726+
727+
impl<S, T, U> crate::internal::operators_macro::FieldAliasMapper<S> for Like<T, U>
728+
where
729+
S: crate::query_source::AliasSource,
730+
T: crate::internal::operators_macro::FieldAliasMapper<S>,
731+
U: crate::internal::operators_macro::FieldAliasMapper<S>,
732+
{
733+
type Out = Like<
734+
<T as crate::internal::operators_macro::FieldAliasMapper<S>>::Out,
735+
<U as crate::internal::operators_macro::FieldAliasMapper<S>>::Out,
736+
>;
737+
fn map(self, alias: &crate::query_source::Alias<S>) -> Self::Out {
738+
Like {
739+
left: self.left.map(alias),
740+
right: self.right.map(alias),
741+
}
742+
}
743+
}
744+
745+
pub trait LikeIsAllowedForType<ST>: Backend {}
746+
747+
impl<DB> LikeIsAllowedForType<crate::sql_types::Text> for DB where DB: Backend {}

0 commit comments

Comments
 (0)