diesel/pg/expression/
array_comparison.rs

1use expression::subselect::Subselect;
2use expression::{AsExpression, Expression, NonAggregate};
3use pg::Pg;
4use query_builder::*;
5use result::QueryResult;
6use sql_types::Array;
7
8/// Creates a PostgreSQL `ANY` expression.
9///
10/// As with most bare functions, this is not exported by default. You can import
11/// it specifically from `diesel::expression::any`, or glob import
12/// `diesel::dsl::*`
13///
14/// # Example
15///
16/// ```rust
17/// # #[macro_use] extern crate diesel;
18/// # include!("../../doctest_setup.rs");
19/// # use diesel::dsl::*;
20/// #
21/// # fn main() {
22/// #     use schema::users::dsl::*;
23/// #     let connection = establish_connection();
24/// #     connection.execute("INSERT INTO users (name) VALUES ('Jim')").unwrap();
25/// let sean = (1, "Sean".to_string());
26/// let jim = (3, "Jim".to_string());
27/// let data = users.filter(name.eq(any(vec!["Sean", "Jim"])));
28/// assert_eq!(Ok(vec![sean, jim]), data.load(&connection));
29/// # }
30/// ```
31pub fn any<ST, T>(vals: T) -> Any<T::Expression>
32where
33    T: AsArrayExpression<ST>,
34{
35    Any::new(vals.as_expression())
36}
37
38/// Creates a PostgreSQL `ALL` expression.
39///
40/// As with most bare functions, this is not exported by default. You can import
41/// it specifically as `diesel::dsl::all`.
42///
43/// # Example
44///
45/// ```rust
46/// # #[macro_use] extern crate diesel;
47/// # include!("../../doctest_setup.rs");
48/// # use diesel::dsl::*;
49/// #
50/// # fn main() {
51/// #     use schema::users::dsl::*;
52/// #     let connection = establish_connection();
53/// #     connection.execute("INSERT INTO users (name) VALUES ('Jim')").unwrap();
54/// let tess = (2, "Tess".to_string());
55/// let data = users.filter(name.ne(all(vec!["Sean", "Jim"])));
56/// assert_eq!(Ok(vec![tess]), data.load(&connection));
57/// # }
58/// ```
59pub 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}