diesel/pg/expression/
array_comparison.rs
1use crate::expression::subselect::Subselect;
2use crate::expression::{AsExpression, Expression, TypedExpressionType, ValidGrouping};
3use crate::pg::Pg;
4use crate::query_builder::*;
5use crate::result::QueryResult;
6use crate::sql_types::{Array, SqlType};
7
8#[deprecated(since = "2.0.0", note = "Use `ExpressionMethods::eq_any` instead")]
30pub fn any<ST, T>(vals: T) -> Any<T::Expression>
31where
32 T: AsArrayExpression<ST>,
33{
34 Any::new(vals.as_expression())
35}
36
37#[deprecated(since = "2.0.0", note = "Use `ExpressionMethods::ne_all` instead")]
58pub fn all<ST, T>(vals: T) -> All<T::Expression>
59where
60 T: AsArrayExpression<ST>,
61{
62 All::new(vals.as_expression())
63}
64
65#[doc(hidden)]
66#[derive(Debug, Copy, Clone, QueryId, ValidGrouping)]
67pub struct Any<Expr> {
68 expr: Expr,
69}
70
71impl<Expr> Any<Expr> {
72 fn new(expr: Expr) -> Self {
73 Any { expr: expr }
74 }
75}
76
77impl<Expr, ST> Expression for Any<Expr>
78where
79 Expr: Expression<SqlType = Array<ST>>,
80 ST: SqlType + TypedExpressionType,
81{
82 type SqlType = ST;
83}
84
85impl<Expr> QueryFragment<Pg> for Any<Expr>
86where
87 Expr: QueryFragment<Pg>,
88{
89 fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, Pg>) -> QueryResult<()> {
90 out.push_sql("ANY(");
91 self.expr.walk_ast(out.reborrow())?;
92 out.push_sql(")");
93 Ok(())
94 }
95}
96
97impl_selectable_expression!(Any<Expr>);
98
99#[doc(hidden)]
100#[derive(Debug, Copy, Clone, QueryId, ValidGrouping)]
101pub struct All<Expr> {
102 expr: Expr,
103}
104
105impl<Expr> All<Expr> {
106 fn new(expr: Expr) -> Self {
107 All { expr: expr }
108 }
109}
110
111impl<Expr, ST> Expression for All<Expr>
112where
113 Expr: Expression<SqlType = Array<ST>>,
114 ST: SqlType + TypedExpressionType,
115{
116 type SqlType = ST;
117}
118
119impl<Expr> QueryFragment<Pg> for All<Expr>
120where
121 Expr: QueryFragment<Pg>,
122{
123 fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, Pg>) -> QueryResult<()> {
124 out.push_sql("ALL(");
125 self.expr.walk_ast(out.reborrow())?;
126 out.push_sql(")");
127 Ok(())
128 }
129}
130
131impl_selectable_expression!(All<Expr>);
132
133pub trait AsArrayExpression<ST: 'static> {
145 type Expression: Expression<SqlType = Array<ST>>;
146
147 #[allow(clippy::wrong_self_convention)]
150 fn as_expression(self) -> Self::Expression;
151}
152
153impl<ST, T> AsArrayExpression<ST> for T
154where
155 ST: 'static,
156 T: AsExpression<Array<ST>>,
157{
158 type Expression = <T as AsExpression<Array<ST>>>::Expression;
159
160 fn as_expression(self) -> Self::Expression {
161 <T as AsExpression<Array<ST>>>::as_expression(self)
162 }
163}
164
165impl<ST, F, S, D, W, O, LOf, G, H, LC> AsArrayExpression<ST>
166 for SelectStatement<F, S, D, W, O, LOf, G, H, LC>
167where
168 ST: 'static,
169 Self: SelectQuery<SqlType = ST>,
170{
171 type Expression = Subselect<Self, Array<ST>>;
172
173 fn as_expression(self) -> Self::Expression {
174 Subselect::new(self)
175 }
176}
177
178impl<ST, QS, DB, GB> AsArrayExpression<ST> for BoxedSelectStatement<'_, ST, QS, DB, GB>
179where
180 ST: 'static,
181 Self: SelectQuery<SqlType = ST>,
182{
183 type Expression = Subselect<Self, Array<ST>>;
184
185 fn as_expression(self) -> Self::Expression {
186 Subselect::new(self)
187 }
188}