Skip to main content

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