use crate::expression::subselect::Subselect; use crate::expression::{AsExpression, Expression, TypedExpressionType, ValidGrouping}; use crate::pg::Pg; use crate::query_builder::*; use crate::result::QueryResult; use crate::sql_types::{Array, SqlType}; /// Creates a PostgreSQL `ANY` expression. /// /// As with most bare functions, this is not exported by default. You can import /// it specifically from `diesel::pg::expression::dsl::any`, or `diesel::dsl::any`. /// /// # Example /// /// ```rust /// # include!("../../doctest_setup.rs"); /// # use diesel::dsl::*; /// # /// # fn main() { /// # use schema::users::dsl::*; /// # let connection = &mut establish_connection(); /// # diesel::sql_query("INSERT INTO users (name) VALUES ('Jim')").execute(connection).unwrap(); /// let sean = (1, "Sean".to_string()); /// let jim = (3, "Jim".to_string()); /// let data = users.filter(name.eq(any(vec!["Sean", "Jim"]))); /// assert_eq!(Ok(vec![sean, jim]), data.load(connection)); /// # } /// ``` #[deprecated(since = "2.0.0", note = "Use `ExpressionMethods::eq_any` instead")] pub fn any(vals: T) -> Any where T: AsArrayExpression, { Any::new(vals.as_expression()) } /// Creates a PostgreSQL `ALL` expression. /// /// As with most bare functions, this is not exported by default. You can import /// it specifically as `diesel::pg::expression::dsl::all`, or `diesel::dsl::all`. /// /// # Example /// /// ```rust /// # include!("../../doctest_setup.rs"); /// # use diesel::dsl::*; /// # /// # fn main() { /// # use schema::users::dsl::*; /// # let connection = &mut establish_connection(); /// # diesel::sql_query("INSERT INTO users (name) VALUES ('Jim')").execute(connection).unwrap(); /// let tess = (2, "Tess".to_string()); /// let data = users.filter(name.ne(all(vec!["Sean", "Jim"]))); /// assert_eq!(Ok(vec![tess]), data.load(connection)); /// # } /// ``` #[deprecated(since = "2.0.0", note = "Use `ExpressionMethods::ne_all` instead")] pub fn all(vals: T) -> All where T: AsArrayExpression, { All::new(vals.as_expression()) } #[doc(hidden)] #[derive(Debug, Copy, Clone, QueryId, ValidGrouping)] pub struct Any { expr: Expr, } impl Any { fn new(expr: Expr) -> Self { Any { expr: expr } } } impl Expression for Any where Expr: Expression>, ST: SqlType + TypedExpressionType, { type SqlType = ST; } impl QueryFragment for Any where Expr: QueryFragment, { fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, Pg>) -> QueryResult<()> { out.push_sql("ANY("); self.expr.walk_ast(out.reborrow())?; out.push_sql(")"); Ok(()) } } impl_selectable_expression!(Any); #[doc(hidden)] #[derive(Debug, Copy, Clone, QueryId, ValidGrouping)] pub struct All { expr: Expr, } impl All { fn new(expr: Expr) -> Self { All { expr: expr } } } impl Expression for All where Expr: Expression>, ST: SqlType + TypedExpressionType, { type SqlType = ST; } impl QueryFragment for All where Expr: QueryFragment, { fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, Pg>) -> QueryResult<()> { out.push_sql("ALL("); self.expr.walk_ast(out.reborrow())?; out.push_sql(")"); Ok(()) } } impl_selectable_expression!(All); /// Deprecated trait used for implementing `any` and `all` (which are themselves deprecated). /// /// It has several quirks: /// - `All>>` pretends to be an expression of type `ST`, /// but it's in fact some custom that can really be only used in combination with the `=` /// operator in some select places. /// - Implementations that use Subelect below are lying: they pretend to be expressions of type /// `Array`, but they're actually subselects, which are processed differently by Postgres /// and may result in different query plans. /// The `IntoArrayExpression` trait represents this more accurately, actually building an /// actual expression of type array from a subselect (by wrapping it in `ARRAY(subselect)`). pub trait AsArrayExpression { type Expression: Expression>; // This method is part of the public API // we won't change it to appease a clippy lint #[allow(clippy::wrong_self_convention)] fn as_expression(self) -> Self::Expression; } impl AsArrayExpression for T where ST: 'static, T: AsExpression>, { type Expression = >>::Expression; fn as_expression(self) -> Self::Expression { >>::as_expression(self) } } impl AsArrayExpression for SelectStatement where ST: 'static, Self: SelectQuery, { type Expression = Subselect>; fn as_expression(self) -> Self::Expression { Subselect::new(self) } } impl AsArrayExpression for BoxedSelectStatement<'_, ST, QS, DB, GB> where ST: 'static, Self: SelectQuery, { type Expression = Subselect>; fn as_expression(self) -> Self::Expression { Subselect::new(self) } }