Skip to content

Commit 6ef23bd

Browse files
authored
Merge pull request #3783 from weiznich/fix/auto_type_defs
Fix most type-defs for `#[auto_type]` by cleaning up all the type defs
2 parents e5fc2a8 + 94d1a5e commit 6ef23bd

File tree

63 files changed

+1019
-441
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+1019
-441
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Increasing the minimal supported Rust version will always be coupled at least wi
2525
### Changed
2626

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

2930
## [2.1.0] 2023-05-26
3031

diesel/src/expression/count.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::marker::PhantomData;
22

3-
use super::functions::sql_function;
3+
use super::functions::define_sql_function;
44
use super::{is_aggregate, AsExpression};
55
use super::{Expression, ValidGrouping};
66
use crate::backend::Backend;
@@ -9,7 +9,7 @@ use crate::result::QueryResult;
99
use crate::sql_types::{BigInt, DieselNumericOps, SingleValue, SqlType};
1010
use crate::{AppearsOnTable, SelectableExpression};
1111

12-
sql_function! {
12+
define_sql_function! {
1313
/// Creates a SQL `COUNT` expression
1414
///
1515
/// As with most bare functions, this is not exported by default. You can import

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/aggregate_folding.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use crate::expression::functions::sql_function;
1+
use crate::expression::functions::define_sql_function;
22
use crate::sql_types::Foldable;
33

4-
sql_function! {
4+
define_sql_function! {
55
/// Represents a SQL `SUM` function. This function can only take types which are
66
/// Foldable.
77
///
@@ -21,7 +21,7 @@ sql_function! {
2121
fn sum<ST: Foldable>(expr: ST) -> ST::Sum;
2222
}
2323

24-
sql_function! {
24+
define_sql_function! {
2525
/// Represents a SQL `AVG` function. This function can only take types which are
2626
/// Foldable.
2727
///

diesel/src/expression/functions/aggregate_ordering.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use self::private::SqlOrdAggregate;
2-
use crate::expression::functions::sql_function;
2+
use crate::expression::functions::define_sql_function;
33

4-
sql_function! {
4+
define_sql_function! {
55
/// Represents a SQL `MAX` function. This function can only take types which are
66
/// ordered.
77
///
@@ -20,7 +20,7 @@ sql_function! {
2020
fn max<ST: SqlOrdAggregate>(expr: ST) -> ST::Ret;
2121
}
2222

23-
sql_function! {
23+
define_sql_function! {
2424
/// Represents a SQL `MIN` function. This function can only take types which are
2525
/// ordered.
2626
///

diesel/src/expression/functions/date_and_time.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::backend::Backend;
22
use crate::expression::coerce::Coerce;
3-
use crate::expression::functions::sql_function;
3+
use crate::expression::functions::define_sql_function;
44
use crate::expression::{AsExpression, Expression, ValidGrouping};
55
use crate::query_builder::*;
66
use crate::result::QueryResult;
@@ -27,7 +27,7 @@ impl_selectable_expression!(now);
2727

2828
operator_allowed!(now, Add, add);
2929
operator_allowed!(now, Sub, sub);
30-
sql_function! {
30+
define_sql_function! {
3131
/// Represents the SQL `DATE` function. The argument should be a Timestamp
3232
/// expression, and the return value will be an expression of type Date.
3333
///

diesel/src/expression/functions/helper_types.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ use crate::expression::operators;
88
pub type not<Expr> = operators::Not<Grouped<Expr>>;
99

1010
/// The return type of [`max(expr)`](crate::dsl::max())
11-
pub type max<Expr> = super::aggregate_ordering::max::HelperType<SqlTypeOf<Expr>, Expr>;
11+
pub type max<Expr> = super::aggregate_ordering::max<SqlTypeOf<Expr>, Expr>;
1212

1313
/// The return type of [`min(expr)`](crate::dsl::min())
14-
pub type min<Expr> = super::aggregate_ordering::min::HelperType<SqlTypeOf<Expr>, Expr>;
14+
pub type min<Expr> = super::aggregate_ordering::min<SqlTypeOf<Expr>, Expr>;
1515

1616
/// The return type of [`sum(expr)`](crate::dsl::sum())
17-
pub type sum<Expr> = super::aggregate_folding::sum::HelperType<SqlTypeOf<Expr>, Expr>;
17+
pub type sum<Expr> = super::aggregate_folding::sum<SqlTypeOf<Expr>, Expr>;
1818

1919
/// The return type of [`avg(expr)`](crate::dsl::avg())
20-
pub type avg<Expr> = super::aggregate_folding::avg::HelperType<SqlTypeOf<Expr>, Expr>;
20+
pub type avg<Expr> = super::aggregate_folding::avg<SqlTypeOf<Expr>, Expr>;
2121

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

diesel/src/expression/functions/mod.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
//! Helper macros to define custom sql functions
22
33
#[doc(inline)]
4+
pub use diesel_derives::define_sql_function;
5+
6+
#[doc(inline)]
7+
#[cfg(all(feature = "with-deprecated", not(feature = "without-deprecated")))]
48
pub use diesel_derives::sql_function_proc as sql_function;
59

610
#[macro_export]
@@ -73,7 +77,7 @@ macro_rules! no_arg_sql_function_body {
7377
/// function.
7478
#[deprecated(
7579
since = "2.0.0",
76-
note = "Use `sql_function!` instead. See `CHANGELOG.md` for migration instructions"
80+
note = "Use `define_sql_function!` instead. See `CHANGELOG.md` for migration instructions"
7781
)]
7882
#[cfg(all(feature = "with-deprecated", not(feature = "without-deprecated")))]
7983
macro_rules! no_arg_sql_function {

diesel/src/expression/helper_types.rs

+14-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)] // 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,12 @@ 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)]
137+
#[allow(non_camel_case_types)] // required for `#[auto_type]`
126138
pub type case_when<C, T, ST = <T as Expression>::SqlType> = expression::case_when::CaseWhen<
127139
expression::case_when::CaseWhenConditionsLeaf<Grouped<C>, Grouped<AsExprOf<T, ST>>>,
128140
expression::case_when::NoElseExpression,
129141
>;
130-
/// The return type of [`case_when(...).when(...)`](expression::case_when::CaseWhen::when)
142+
/// The return type of [`case_when(...).when(...)`](expression::CaseWhen::when)
131143
pub type When<W, C, T> = expression::case_when::CaseWhen<
132144
expression::case_when::CaseWhenConditionsIntermediateNode<
133145
Grouped<C>,

diesel/src/expression/mod.rs

+7-3
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::{case_when, Otherwise, When};
69+
#[doc(inline)]
6870
pub use super::not::not;
6971
#[doc(inline)]
7072
pub use super::sql_literal::sql;
@@ -73,7 +75,7 @@ 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<SqlTypeOf<Expr>, Expr>;
7779

7880
/// The return type of [`count_star()`](crate::dsl::count_star())
7981
pub type count_star = super::count::CountStar;
@@ -82,12 +84,14 @@ pub(crate) mod dsl {
8284
pub type count_distinct<Expr> = super::count::CountDistinct<SqlTypeOf<Expr>, Expr>;
8385

8486
/// The return type of [`date(expr)`](crate::dsl::date())
85-
pub type date<Expr> = super::functions::date_and_time::date::HelperType<Expr>;
87+
pub type date<Expr> = super::functions::date_and_time::date<Expr>;
8688

8789
#[cfg(feature = "mysql_backend")]
8890
pub use crate::mysql::query_builder::DuplicatedKeys;
8991
}
9092

93+
#[doc(inline)]
94+
pub use self::case_when::CaseWhen;
9195
#[doc(inline)]
9296
pub use self::sql_literal::{SqlLiteral, UncheckedBind};
9397

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 {}

0 commit comments

Comments
 (0)