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.cast<ST>()`](crate::expression_methods::ExpressionMethods::cast())
95pub type Cast<Expr, ST> = super::cast::Cast<Expr, ST>;
96
97/// The return type of
98/// [`expr.desc()`](crate::expression_methods::ExpressionMethods::desc())
99pub type Desc<Expr> = super::operators::Desc<Expr>;
100
101/// The return type of
102/// [`expr.asc()`](crate::expression_methods::ExpressionMethods::asc())
103pub type Asc<Expr> = super::operators::Asc<Expr>;
104
105/// The return type of
106/// [`expr.nullable()`](crate::expression_methods::NullableExpressionMethods::nullable())
107pub type Nullable<Expr> = super::nullable::Nullable<Expr>;
108
109/// The return type of
110/// [`expr.assume_not_null()`](crate::expression_methods::NullableExpressionMethods::assume_not_null())
111pub type AssumeNotNull<Expr> = super::assume_not_null::AssumeNotNull<Expr>;
112
113/// The return type of
114/// [`lhs.and(rhs)`](crate::expression_methods::BoolExpressionMethods::and())
115pub type And<Lhs, Rhs, ST = <Rhs as PreferredBoolSqlType>::PreferredSqlType> =
116    Grouped<super::operators::And<Lhs, AsExprOf<Rhs, ST>>>;
117
118/// The return type of
119/// [`lhs.or(rhs)`](crate::expression_methods::BoolExpressionMethods::or())
120pub type Or<Lhs, Rhs, ST = <Rhs as PreferredBoolSqlType>::PreferredSqlType> =
121    Grouped<super::operators::Or<Lhs, AsExprOf<Rhs, ST>>>;
122
123/// The return type of
124/// [`lhs.escape('x')`](crate::expression_methods::EscapeExpressionMethods::escape())
125pub type Escape<Lhs> = Grouped<
126    super::operators::Escape<
127        <Lhs as crate::expression_methods::EscapeExpressionMethods>::TextExpression,
128        AsExprOf<String, sql_types::VarChar>,
129    >,
130>;
131
132/// The return type of
133/// [`lhs.like(rhs)`](crate::expression_methods::TextExpressionMethods::like())
134pub type Like<Lhs, Rhs> = Grouped<super::operators::Like<Lhs, AsExprOf<Rhs, SqlTypeOf<Lhs>>>>;
135
136/// The return type of
137/// [`lhs.not_like(rhs)`](crate::expression_methods::TextExpressionMethods::not_like())
138pub type NotLike<Lhs, Rhs> = Grouped<super::operators::NotLike<Lhs, AsExprOf<Rhs, SqlTypeOf<Lhs>>>>;
139
140/// The return type of [`case_when()`](expression::case_when::case_when)
141#[allow(non_camel_case_types)] // required for `#[auto_type]`
142pub type case_when<C, T, ST = <T as Expression>::SqlType> = expression::case_when::CaseWhen<
143    expression::case_when::CaseWhenConditionsLeaf<Grouped<C>, Grouped<AsExprOf<T, ST>>>,
144    expression::case_when::NoElseExpression,
145>;
146/// The return type of [`case_when(...).when(...)`](expression::CaseWhen::when)
147pub type When<W, C, T> = expression::case_when::CaseWhen<
148    expression::case_when::CaseWhenConditionsIntermediateNode<
149        Grouped<C>,
150        Grouped<AsExprOf<T, <W as expression::case_when::CaseWhenTypesExtractor>::OutputExpressionSpecifiedSqlType>>,
151        <W as expression::case_when::CaseWhenTypesExtractor>::Whens,
152    >,
153    <W as expression::case_when::CaseWhenTypesExtractor>::Else,
154>;
155/// The return type of [`case_when(...).otherwise(...)`](expression::case_when::CaseWhen::otherwise)
156pub type Otherwise<W, E> = expression::case_when::CaseWhen<
157    <W as expression::case_when::CaseWhenTypesExtractor>::Whens,
158    expression::case_when::ElseExpression<Grouped<AsExprOf<E, <W as expression::case_when::CaseWhenTypesExtractor>::OutputExpressionSpecifiedSqlType>>>,
159>;
160
161/// Represents the return type of [`.as_select()`](crate::prelude::SelectableHelper::as_select)
162pub type AsSelect<Source, DB> = SelectBy<Source, DB>;
163
164/// Represents the return type of [`.into_sql()`](crate::expression::IntoSql::into_sql)
165pub type IntoSql<Item, SqlType> = AsExprOf<Item, SqlType>;
166
167/// The return type of [`alias.field(field)`](crate::query_source::Alias::field)
168pub type Field<Alias, Field> = Fields<Alias, Field>;
169
170/// The return type of [`alias.fields(fields)`](crate::query_source::Alias::fields)
171pub type Fields<Alias, Fields> = <Fields as crate::query_source::aliasing::FieldAliasMapper<
172    <Alias as crate::query_source::aliasing::GetAliasSourceFromAlias>::Source,
173>>::Out;
174
175/// The return type of
176/// [`l + r`](expression::ops::numeric::Add)
177pub type Add<L, R> = <L as ::core::ops::Add<R>>::Output;
178
179/// The return type of
180/// [`l - r`](expression::ops::numeric::Sub)
181pub type Sub<L, R> = <L as ::core::ops::Sub<R>>::Output;
182
183/// The return type of
184/// [`l * r`](expression::ops::numeric::Mul)
185pub type Mul<L, R> = <L as ::core::ops::Mul<R>>::Output;
186
187/// The return type of
188/// [`l / r`](expression::ops::numeric::Div)
189pub type Div<L, R> = <L as ::core::ops::Div<R>>::Output;
190
191// we allow unreachable_pub here
192// as rustc otherwise shows false positives
193// for every item in this module. We reexport
194// everything from `crate::helper_types::`
195#[doc(inline)]
196#[allow(unreachable_pub)]
197pub use super::functions::helper_types::*;
198
199#[doc(inline)]
200#[cfg(feature = "postgres_backend")]
201#[allow(unreachable_pub)]
202pub use crate::pg::expression::helper_types::*;
203
204#[doc(inline)]
205#[cfg(feature = "sqlite")]
206#[allow(unreachable_pub)]
207pub use crate::sqlite::expression::helper_types::*;