diesel/pg/expression/
array_comparison.rs1use expression::subselect::Subselect;
2use expression::{AsExpression, Expression, NonAggregate};
3use pg::Pg;
4use query_builder::*;
5use result::QueryResult;
6use sql_types::Array;
7
8pub fn any<ST, T>(vals: T) -> Any<T::Expression>
32where
33 T: AsArrayExpression<ST>,
34{
35 Any::new(vals.as_expression())
36}
37
38pub fn all<ST, T>(vals: T) -> All<T::Expression>
60where
61 T: AsArrayExpression<ST>,
62{
63 All::new(vals.as_expression())
64}
65
66#[doc(hidden)]
67#[derive(Debug, Copy, Clone, QueryId)]
68pub struct Any<Expr> {
69 expr: Expr,
70}
71
72impl<Expr> Any<Expr> {
73 fn new(expr: Expr) -> Self {
74 Any { expr: expr }
75 }
76}
77
78impl<Expr, ST> Expression for Any<Expr>
79where
80 Expr: Expression<SqlType = Array<ST>>,
81{
82 type SqlType = ST;
83}
84
85impl<Expr> QueryFragment<Pg> for Any<Expr>
86where
87 Expr: QueryFragment<Pg>,
88{
89 fn walk_ast(&self, mut out: AstPass<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
99impl<Expr> NonAggregate for Any<Expr> where Expr: NonAggregate {}
100
101#[doc(hidden)]
102#[derive(Debug, Copy, Clone, QueryId)]
103pub struct All<Expr> {
104 expr: Expr,
105}
106
107impl<Expr> All<Expr> {
108 fn new(expr: Expr) -> Self {
109 All { expr: expr }
110 }
111}
112
113impl<Expr, ST> Expression for All<Expr>
114where
115 Expr: Expression<SqlType = Array<ST>>,
116{
117 type SqlType = ST;
118}
119
120impl<Expr> QueryFragment<Pg> for All<Expr>
121where
122 Expr: QueryFragment<Pg>,
123{
124 fn walk_ast(&self, mut out: AstPass<Pg>) -> QueryResult<()> {
125 out.push_sql("ALL(");
126 self.expr.walk_ast(out.reborrow())?;
127 out.push_sql(")");
128 Ok(())
129 }
130}
131
132impl_selectable_expression!(All<Expr>);
133
134impl<Expr> NonAggregate for All<Expr> where Expr: NonAggregate {}
135
136pub trait AsArrayExpression<ST> {
137 type Expression: Expression<SqlType = Array<ST>>;
138
139 fn as_expression(self) -> Self::Expression;
140}
141
142impl<ST, T> AsArrayExpression<ST> for T
143where
144 T: AsExpression<Array<ST>>,
145{
146 type Expression = <T as AsExpression<Array<ST>>>::Expression;
147
148 fn as_expression(self) -> Self::Expression {
149 AsExpression::as_expression(self)
150 }
151}
152
153impl<ST, S, F, W, O, L, Of, G, FU> AsArrayExpression<ST>
154 for SelectStatement<S, F, W, O, L, Of, G, FU>
155where
156 Self: SelectQuery<SqlType = ST>,
157{
158 type Expression = Subselect<Self, Array<ST>>;
159
160 fn as_expression(self) -> Self::Expression {
161 Subselect::new(self)
162 }
163}
164
165impl<'a, ST, QS, DB> AsArrayExpression<ST> for BoxedSelectStatement<'a, ST, QS, DB>
166where
167 Self: SelectQuery<SqlType = ST>,
168{
169 type Expression = Subselect<Self, Array<ST>>;
170
171 fn as_expression(self) -> Self::Expression {
172 Subselect::new(self)
173 }
174}