diesel/expression/
helper_types.rs

1//! The types in this module are all shorthand for `PredicateType<Lhs,
2//! AsExpr<Rhs, Lhs>>`. Since we often need to return concrete types, instead of
3//! a boxed trait object, these can be useful for writing concise return types.
4use super::array_comparison::{AsInExpression, In, NotIn};
5use super::grouped::Grouped;
6use super::select_by::SelectBy;
7use super::{AsExpression, Expression};
8use crate::expression;
9use crate::expression_methods::PreferredBoolSqlType;
10use crate::sql_types;
11
12/// The SQL type of an expression
13pub type SqlTypeOf<Expr> = <Expr as Expression>::SqlType;
14
15/// The type of `Item` when converted to an expression with the same type as `TargetExpr`
16pub type AsExpr<Item, TargetExpr> = AsExprOf<Item, SqlTypeOf<TargetExpr>>;
17
18/// The type of `Item` when converted to an expression of `Type`
19pub type AsExprOf<Item, Type> = <Item as AsExpression<Type>>::Expression;
20
21/// The return type of
22/// [`lhs.eq(rhs)`](crate::expression_methods::ExpressionMethods::eq())
23pub type Eq<Lhs, Rhs> = Grouped<super::operators::Eq<Lhs, AsExpr<Rhs, Lhs>>>;
24
25/// The return type of
26/// [`lhs.ne(rhs)`](crate::expression_methods::ExpressionMethods::ne())
27pub type NotEq<Lhs, Rhs> = Grouped<super::operators::NotEq<Lhs, AsExpr<Rhs, Lhs>>>;
28
29#[doc(hidden)] // required for `#[auto_type]`
30pub type Ne<Lhs, Rhs> = NotEq<Lhs, Rhs>;
31
32/// The return type of
33/// [`lhs.eq_any(rhs)`](crate::expression_methods::ExpressionMethods::eq_any())
34pub type EqAny<Lhs, Rhs> = Grouped<In<Lhs, <Rhs as AsInExpression<SqlTypeOf<Lhs>>>::InExpression>>;
35
36/// The return type of
37/// [`lhs.ne_all(rhs)`](crate::expression_methods::ExpressionMethods::ne_all())
38pub type NeAny<Lhs, Rhs> =
39    Grouped<NotIn<Lhs, <Rhs as AsInExpression<SqlTypeOf<Lhs>>>::InExpression>>;
40
41#[doc(hidden)] // required for `#[auto_type]`
42pub type NeAll<Lhs, Rhs> = NeAny<Lhs, Rhs>;
43
44/// The return type of
45/// [`expr.is_null()`](crate::expression_methods::ExpressionMethods::is_null())
46pub type IsNull<Expr> = Grouped<super::operators::IsNull<Expr>>;
47
48/// The return type of
49/// [`expr.is_not_null()`](crate::expression_methods::ExpressionMethods::is_not_null())
50pub type IsNotNull<Expr> = Grouped<super::operators::IsNotNull<Expr>>;
51
52/// The return type of
53/// [`lhs.gt(rhs)`](crate::expression_methods::ExpressionMethods::gt())
54pub type Gt<Lhs, Rhs> = Grouped<super::operators::Gt<Lhs, AsExpr<Rhs, Lhs>>>;
55
56/// The return type of
57/// [`lhs.ge(rhs)`](crate::expression_methods::ExpressionMethods::ge())
58pub type GtEq<Lhs, Rhs> = Grouped<super::operators::GtEq<Lhs, AsExpr<Rhs, Lhs>>>;
59
60#[doc(hidden)] // required for `#[auto_type]`
61pub type Ge<Lhs, Rhs> = GtEq<Lhs, Rhs>;
62
63/// The return type of
64/// [`lhs.lt(rhs)`](crate::expression_methods::ExpressionMethods::lt())
65pub type Lt<Lhs, Rhs> = Grouped<super::operators::Lt<Lhs, AsExpr<Rhs, Lhs>>>;
66
67/// The return type of
68/// [`lhs.le(rhs)`](crate::expression_methods::ExpressionMethods::le())
69pub type LtEq<Lhs, Rhs> = Grouped<super::operators::LtEq<Lhs, AsExpr<Rhs, Lhs>>>;
70
71#[doc(hidden)] // required for `#[auto_type]`
72pub type Le<Lhs, Rhs> = LtEq<Lhs, Rhs>;
73
74/// The return type of
75/// [`lhs.between(lower, upper)`](crate::expression_methods::ExpressionMethods::between())
76pub type Between<Lhs, Lower, Upper> = Grouped<
77    super::operators::Between<Lhs, super::operators::And<AsExpr<Lower, Lhs>, AsExpr<Upper, Lhs>>>,
78>;
79
80/// The return type of
81/// [`lhs.not_between(lower, upper)`](crate::expression_methods::ExpressionMethods::not_between())
82pub type NotBetween<Lhs, Lower, Upper> = Grouped<
83    super::operators::NotBetween<
84        Lhs,
85        super::operators::And<AsExpr<Lower, Lhs>, AsExpr<Upper, Lhs>>,
86    >,
87>;
88
89/// The return type of
90/// [`lhs.concat(rhs)`](crate::expression_methods::TextExpressionMethods::concat())
91pub type Concat<Lhs, Rhs> = Grouped<super::operators::Concat<Lhs, AsExpr<Rhs, Lhs>>>;
92
93/// The return type of
94/// [`expr.desc()`](crate::expression_methods::ExpressionMethods::desc())
95pub type Desc<Expr> = super::operators::Desc<Expr>;
96
97/// The return type of
98/// [`expr.asc()`](crate::expression_methods::ExpressionMethods::asc())
99pub type Asc<Expr> = super::operators::Asc<Expr>;
100
101/// The return type of
102/// [`expr.nullable()`](crate::expression_methods::NullableExpressionMethods::nullable())
103pub type Nullable<Expr> = super::nullable::Nullable<Expr>;
104
105/// The return type of
106/// [`expr.assume_not_null()`](crate::expression_methods::NullableExpressionMethods::assume_not_null())
107pub type AssumeNotNull<Expr> = super::assume_not_null::AssumeNotNull<Expr>;
108
109/// The return type of
110/// [`lhs.and(rhs)`](crate::expression_methods::BoolExpressionMethods::and())
111pub type And<Lhs, Rhs, ST = <Rhs as PreferredBoolSqlType>::PreferredSqlType> =
112    Grouped<super::operators::And<Lhs, AsExprOf<Rhs, ST>>>;
113
114/// The return type of
115/// [`lhs.or(rhs)`](crate::expression_methods::BoolExpressionMethods::or())
116pub type Or<Lhs, Rhs, ST = <Rhs as PreferredBoolSqlType>::PreferredSqlType> =
117    Grouped<super::operators::Or<Lhs, AsExprOf<Rhs, ST>>>;
118
119/// The return type of
120/// [`lhs.escape('x')`](crate::expression_methods::EscapeExpressionMethods::escape())
121pub type Escape<Lhs> = Grouped<
122    super::operators::Escape<
123        <Lhs as crate::expression_methods::EscapeExpressionMethods>::TextExpression,
124        AsExprOf<String, sql_types::VarChar>,
125    >,
126>;
127
128/// The return type of
129/// [`lhs.like(rhs)`](crate::expression_methods::TextExpressionMethods::like())
130pub type Like<Lhs, Rhs> = Grouped<super::operators::Like<Lhs, AsExprOf<Rhs, SqlTypeOf<Lhs>>>>;
131
132/// The return type of
133/// [`lhs.not_like(rhs)`](crate::expression_methods::TextExpressionMethods::not_like())
134pub type NotLike<Lhs, Rhs> = Grouped<super::operators::NotLike<Lhs, AsExprOf<Rhs, SqlTypeOf<Lhs>>>>;
135
136/// The return type of [`case_when()`](expression::case_when::case_when)
137#[allow(non_camel_case_types)] // required for `#[auto_type]`
138pub type case_when<C, T, ST = <T as Expression>::SqlType> = expression::case_when::CaseWhen<
139    expression::case_when::CaseWhenConditionsLeaf<Grouped<C>, Grouped<AsExprOf<T, ST>>>,
140    expression::case_when::NoElseExpression,
141>;
142/// The return type of [`case_when(...).when(...)`](expression::CaseWhen::when)
143pub type When<W, C, T> = expression::case_when::CaseWhen<
144    expression::case_when::CaseWhenConditionsIntermediateNode<
145        Grouped<C>,
146        Grouped<AsExprOf<T, <W as expression::case_when::CaseWhenTypesExtractor>::OutputExpressionSpecifiedSqlType>>,
147        <W as expression::case_when::CaseWhenTypesExtractor>::Whens,
148    >,
149    <W as expression::case_when::CaseWhenTypesExtractor>::Else,
150>;
151/// The return type of [`case_when(...).otherwise(...)`](expression::case_when::CaseWhen::otherwise)
152pub type Otherwise<W, E> = expression::case_when::CaseWhen<
153    <W as expression::case_when::CaseWhenTypesExtractor>::Whens,
154    expression::case_when::ElseExpression<Grouped<AsExprOf<E, <W as expression::case_when::CaseWhenTypesExtractor>::OutputExpressionSpecifiedSqlType>>>,
155>;
156
157/// Represents the return type of [`.as_select()`](crate::prelude::SelectableHelper::as_select)
158pub type AsSelect<Source, DB> = SelectBy<Source, DB>;
159
160/// Represents the return type of [`.into_sql()`](crate::expression::IntoSql::into_sql)
161pub type IntoSql<Item, SqlType> = AsExprOf<Item, SqlType>;
162
163/// The return type of [`alias.field(field)`](crate::query_source::Alias::field)
164pub type Field<Alias, Field> = Fields<Alias, Field>;
165
166/// The return type of [`alias.fields(fields)`](crate::query_source::Alias::fields)
167pub type Fields<Alias, Fields> = <Fields as crate::query_source::aliasing::FieldAliasMapper<
168    <Alias as crate::query_source::aliasing::GetAliasSourceFromAlias>::Source,
169>>::Out;
170
171// we allow unreachable_pub here
172// as rustc otherwise shows false positives
173// for every item in this module. We reexport
174// everything from `crate::helper_types::`
175#[doc(inline)]
176#[allow(unreachable_pub)]
177pub use super::functions::helper_types::*;
178
179#[doc(inline)]
180#[cfg(feature = "postgres_backend")]
181#[allow(unreachable_pub)]
182pub use crate::pg::expression::helper_types::*;
183
184#[doc(inline)]
185#[cfg(feature = "sqlite")]
186#[allow(unreachable_pub)]
187pub use crate::sqlite::expression::helper_types::*;