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;
9#[cfg(any(feature = "postgres_backend", feature = "sqlite"))]
10use crate::expression_methods::JsonIndex;
11use crate::expression_methods::PreferredBoolSqlType;
12use crate::sql_types;
13
14/// The SQL type of an expression
15pub type SqlTypeOf<Expr> = <Expr as Expression>::SqlType;
16
17/// The type of `Item` when converted to an expression with the same type as `TargetExpr`
18pub type AsExpr<Item, TargetExpr> = AsExprOf<Item, SqlTypeOf<TargetExpr>>;
19
20/// The type of `Item` when converted to an expression of `Type`
21pub type AsExprOf<Item, Type> = <Item as AsExpression<Type>>::Expression;
22
23/// The return type of
24/// [`lhs.eq(rhs)`](crate::expression_methods::ExpressionMethods::eq())
25pub type Eq<Lhs, Rhs> = Grouped<super::operators::Eq<Lhs, AsExpr<Rhs, Lhs>>>;
26
27/// The return type of
28/// [`lhs.ne(rhs)`](crate::expression_methods::ExpressionMethods::ne())
29pub type NotEq<Lhs, Rhs> = Grouped<super::operators::NotEq<Lhs, AsExpr<Rhs, Lhs>>>;
30
31#[doc(hidden)] // required for `#[auto_type]`
32pub type Ne<Lhs, Rhs> = NotEq<Lhs, Rhs>;
33
34/// The return type of
35/// [`lhs.eq_any(rhs)`](crate::expression_methods::ExpressionMethods::eq_any())
36pub type EqAny<Lhs, Rhs> = Grouped<In<Lhs, <Rhs as AsInExpression<SqlTypeOf<Lhs>>>::InExpression>>;
37
38/// The return type of
39/// [`lhs.ne_all(rhs)`](crate::expression_methods::ExpressionMethods::ne_all())
40pub type NeAny<Lhs, Rhs> =
41    Grouped<NotIn<Lhs, <Rhs as AsInExpression<SqlTypeOf<Lhs>>>::InExpression>>;
42
43#[doc(hidden)] // required for `#[auto_type]`
44pub type NeAll<Lhs, Rhs> = NeAny<Lhs, Rhs>;
45
46/// The return type of
47/// [`expr.is_null()`](crate::expression_methods::ExpressionMethods::is_null())
48pub type IsNull<Expr> = Grouped<super::operators::IsNull<Expr>>;
49
50/// The return type of
51/// [`expr.is_not_null()`](crate::expression_methods::ExpressionMethods::is_not_null())
52pub type IsNotNull<Expr> = Grouped<super::operators::IsNotNull<Expr>>;
53
54/// The return type of
55/// [`lhs.gt(rhs)`](crate::expression_methods::ExpressionMethods::gt())
56pub type Gt<Lhs, Rhs> = Grouped<super::operators::Gt<Lhs, AsExpr<Rhs, Lhs>>>;
57
58/// The return type of
59/// [`lhs.ge(rhs)`](crate::expression_methods::ExpressionMethods::ge())
60pub type GtEq<Lhs, Rhs> = Grouped<super::operators::GtEq<Lhs, AsExpr<Rhs, Lhs>>>;
61
62#[doc(hidden)] // required for `#[auto_type]`
63pub type Ge<Lhs, Rhs> = GtEq<Lhs, Rhs>;
64
65/// The return type of
66/// [`lhs.lt(rhs)`](crate::expression_methods::ExpressionMethods::lt())
67pub type Lt<Lhs, Rhs> = Grouped<super::operators::Lt<Lhs, AsExpr<Rhs, Lhs>>>;
68
69/// The return type of
70/// [`lhs.le(rhs)`](crate::expression_methods::ExpressionMethods::le())
71pub type LtEq<Lhs, Rhs> = Grouped<super::operators::LtEq<Lhs, AsExpr<Rhs, Lhs>>>;
72
73#[doc(hidden)] // required for `#[auto_type]`
74pub type Le<Lhs, Rhs> = LtEq<Lhs, Rhs>;
75
76/// The return type of
77/// [`lhs.between(lower, upper)`](crate::expression_methods::ExpressionMethods::between())
78pub type Between<Lhs, Lower, Upper> = Grouped<
79    super::operators::Between<Lhs, super::operators::And<AsExpr<Lower, Lhs>, AsExpr<Upper, Lhs>>>,
80>;
81
82/// The return type of
83/// [`lhs.not_between(lower, upper)`](crate::expression_methods::ExpressionMethods::not_between())
84pub type NotBetween<Lhs, Lower, Upper> = Grouped<
85    super::operators::NotBetween<
86        Lhs,
87        super::operators::And<AsExpr<Lower, Lhs>, AsExpr<Upper, Lhs>>,
88    >,
89>;
90
91/// The return type of
92/// [`lhs.concat(rhs)`](crate::expression_methods::TextExpressionMethods::concat())
93pub type Concat<Lhs, Rhs> = Grouped<super::operators::Concat<Lhs, AsExpr<Rhs, Lhs>>>;
94
95/// The return type of
96/// [`expr.cast<ST>()`](crate::expression_methods::ExpressionMethods::cast())
97pub type Cast<Expr, ST> = super::cast::Cast<Expr, ST>;
98
99/// The return type of
100/// [`expr.desc()`](crate::expression_methods::ExpressionMethods::desc())
101pub type Desc<Expr> = super::operators::Desc<Expr>;
102
103/// The return type of
104/// [`expr.asc()`](crate::expression_methods::ExpressionMethods::asc())
105pub type Asc<Expr> = super::operators::Asc<Expr>;
106
107/// The return type of
108/// [`expr.nullable()`](crate::expression_methods::NullableExpressionMethods::nullable())
109pub type Nullable<Expr> = super::nullable::Nullable<Expr>;
110
111/// The return type of
112/// [`expr.assume_not_null()`](crate::expression_methods::NullableExpressionMethods::assume_not_null())
113pub type AssumeNotNull<Expr> = super::assume_not_null::AssumeNotNull<Expr>;
114
115/// The return type of
116/// [`lhs.and(rhs)`](crate::expression_methods::BoolExpressionMethods::and())
117pub type And<Lhs, Rhs, ST = <Rhs as PreferredBoolSqlType>::PreferredSqlType> =
118    Grouped<super::operators::And<Lhs, AsExprOf<Rhs, ST>>>;
119
120/// The return type of
121/// [`lhs.or(rhs)`](crate::expression_methods::BoolExpressionMethods::or())
122pub type Or<Lhs, Rhs, ST = <Rhs as PreferredBoolSqlType>::PreferredSqlType> =
123    Grouped<super::operators::Or<Lhs, AsExprOf<Rhs, ST>>>;
124
125/// The return type of
126/// [`lhs.escape('x')`](crate::expression_methods::EscapeExpressionMethods::escape())
127pub type Escape<Lhs> = Grouped<
128    super::operators::Escape<
129        <Lhs as crate::expression_methods::EscapeExpressionMethods>::TextExpression,
130        AsExprOf<String, sql_types::VarChar>,
131    >,
132>;
133
134/// The return type of
135/// [`lhs.like(rhs)`](crate::expression_methods::TextExpressionMethods::like())
136pub type Like<Lhs, Rhs> = Grouped<super::operators::Like<Lhs, AsExprOf<Rhs, SqlTypeOf<Lhs>>>>;
137
138/// The return type of
139/// [`lhs.not_like(rhs)`](crate::expression_methods::TextExpressionMethods::not_like())
140pub type NotLike<Lhs, Rhs> = Grouped<super::operators::NotLike<Lhs, AsExprOf<Rhs, SqlTypeOf<Lhs>>>>;
141
142/// The return type of [`case_when()`](expression::case_when::case_when)
143#[allow(non_camel_case_types)] // required for `#[auto_type]`
144pub type case_when<C, T, ST = <T as Expression>::SqlType> = expression::case_when::CaseWhen<
145    expression::case_when::CaseWhenConditionsLeaf<Grouped<C>, Grouped<AsExprOf<T, ST>>>,
146    expression::case_when::NoElseExpression,
147>;
148/// The return type of [`case_when(...).when(...)`](expression::CaseWhen::when)
149pub type When<W, C, T> = expression::case_when::CaseWhen<
150    expression::case_when::CaseWhenConditionsIntermediateNode<
151        Grouped<C>,
152        Grouped<AsExprOf<T, <W as expression::case_when::CaseWhenTypesExtractor>::OutputExpressionSpecifiedSqlType>>,
153        <W as expression::case_when::CaseWhenTypesExtractor>::Whens,
154    >,
155    <W as expression::case_when::CaseWhenTypesExtractor>::Else,
156>;
157/// The return type of [`case_when(...).otherwise(...)`](expression::case_when::CaseWhen::otherwise)
158pub type Otherwise<W, E> = expression::case_when::CaseWhen<
159    <W as expression::case_when::CaseWhenTypesExtractor>::Whens,
160    expression::case_when::ElseExpression<Grouped<AsExprOf<E, <W as expression::case_when::CaseWhenTypesExtractor>::OutputExpressionSpecifiedSqlType>>>,
161>;
162
163/// Represents the return type of [`.as_select()`](crate::prelude::SelectableHelper::as_select)
164pub type AsSelect<Source, DB> = SelectBy<Source, DB>;
165
166/// Represents the return type of [`.into_sql()`](crate::expression::IntoSql::into_sql)
167pub type IntoSql<Item, SqlType> = AsExprOf<Item, SqlType>;
168
169/// The return type of [`alias.field(field)`](crate::query_source::Alias::field)
170pub type Field<Alias, Field> = Fields<Alias, Field>;
171
172/// The return type of [`alias.fields(fields)`](crate::query_source::Alias::fields)
173pub type Fields<Alias, Fields> = <Fields as crate::query_source::aliasing::FieldAliasMapper<
174    <Alias as crate::query_source::aliasing::GetAliasSourceFromAlias>::Source,
175>>::Out;
176
177/// The return type of
178/// [`l + r`](expression::ops::numeric::Add)
179pub type Add<L, R> = <L as ::core::ops::Add<R>>::Output;
180
181/// The return type of
182/// [`l - r`](expression::ops::numeric::Sub)
183pub type Sub<L, R> = <L as ::core::ops::Sub<R>>::Output;
184
185/// The return type of
186/// [`l * r`](expression::ops::numeric::Mul)
187pub type Mul<L, R> = <L as ::core::ops::Mul<R>>::Output;
188
189/// The return type of
190/// [`l / r`](expression::ops::numeric::Div)
191pub type Div<L, R> = <L as ::core::ops::Div<R>>::Output;
192
193// we allow unreachable_pub here
194// as rustc otherwise shows false positives
195// for every item in this module. We reexport
196// everything from `crate::helper_types::`
197#[doc(inline)]
198#[allow(unreachable_pub)]
199pub use super::functions::helper_types::*;
200
201#[doc(inline)]
202#[cfg(all(feature = "postgres_backend", not(feature = "sqlite")))]
203#[allow(unreachable_pub)]
204#[cfg_attr(
205    all(feature = "sqlite", feature = "postgres_backend"),
206    expect(
207        ambiguous_glob_reexports,
208        reason = "we cannot do much about this anymore"
209    )
210)]
211pub use crate::pg::expression::helper_types::*;
212
213#[doc(inline)]
214#[cfg(all(feature = "postgres_backend", feature = "sqlite"))]
215#[allow(unreachable_pub, ambiguous_glob_reexports)]
216pub use crate::pg::expression::helper_types::*;
217
218#[doc(inline)]
219#[cfg(all(feature = "sqlite", not(feature = "postgres_backend")))]
220#[allow(unreachable_pub)]
221pub use crate::sqlite::expression::helper_types::*;
222
223#[doc(inline)]
224#[cfg(all(feature = "sqlite", feature = "postgres_backend"))]
225#[allow(unreachable_pub, ambiguous_glob_reexports)]
226pub use crate::sqlite::expression::helper_types::*;
227
228/// The return type of [`lhs.retrieve_as_text(rhs)`](crate::expression_methods::AnyJsonExpressionMethods::retrieve_as_text)
229#[cfg(any(feature = "postgres_backend", feature = "sqlite"))]
230pub type RetrieveAsText<Lhs, Rhs> = Grouped<
231    crate::expression::operators::RetrieveAsTextJson<
232        Lhs,
233        AsExprOf<
234            <Rhs as JsonIndex>::Expression,
235            <<Rhs as JsonIndex>::Expression as Expression>::SqlType,
236        >,
237    >,
238>;