| 1 |
use crate::expression::subselect::Subselect; |
| 2 |
use crate::expression::{AsExpression, Expression, TypedExpressionType, ValidGrouping}; |
| 3 |
use crate::pg::Pg; |
| 4 |
use crate::query_builder::*; |
| 5 |
use crate::result::QueryResult; |
| 6 |
use crate::sql_types::{Array, SqlType}; |
| 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::pg::expression::dsl::any`, or `diesel::dsl::any`. |
| 12 |
/// |
| 13 |
/// # Example |
| 14 |
/// |
| 15 |
/// ```rust |
| 16 |
/// # include!("../../doctest_setup.rs"); |
| 17 |
/// # use diesel::dsl::*; |
| 18 |
/// # |
| 19 |
/// # fn main() { |
| 20 |
/// # use schema::users::dsl::*; |
| 21 |
/// # let connection = &mut establish_connection(); |
| 22 |
/// # diesel::sql_query("INSERT INTO users (name) VALUES ('Jim')").execute(connection).unwrap(); |
| 23 |
/// let sean = (1, "Sean".to_string()); |
| 24 |
/// let jim = (3, "Jim".to_string()); |
| 25 |
/// let data = users.filter(name.eq(any(vec!["Sean", "Jim"]))); |
| 26 |
/// assert_eq!(Ok(vec![sean, jim]), data.load(connection)); |
| 27 |
/// # } |
| 28 |
/// ``` |
| 29 |
#[deprecated(since = "2.0.0", note = "Use `ExpressionMethods::eq_any` instead")] |
| 30 |
pub fn any<ST, T>(vals: T) -> Any<T::Expression> |
| 31 |
where |
| 32 |
T: AsArrayExpression<ST>, |
| 33 |
{ |
| 34 |
Any::new(vals.as_expression()) |
| 35 |
} |
| 36 |
|
| 37 |
/// Creates a PostgreSQL `ALL` expression. |
| 38 |
/// |
| 39 |
/// As with most bare functions, this is not exported by default. You can import |
| 40 |
/// it specifically as `diesel::pg::expression::dsl::all`, or `diesel::dsl::all`. |
| 41 |
/// |
| 42 |
/// # Example |
| 43 |
/// |
| 44 |
/// ```rust |
| 45 |
/// # include!("../../doctest_setup.rs"); |
| 46 |
/// # use diesel::dsl::*; |
| 47 |
/// # |
| 48 |
/// # fn main() { |
| 49 |
/// # use schema::users::dsl::*; |
| 50 |
/// # let connection = &mut establish_connection(); |
| 51 |
/// # diesel::sql_query("INSERT INTO users (name) VALUES ('Jim')").execute(connection).unwrap(); |
| 52 |
/// let tess = (2, "Tess".to_string()); |
| 53 |
/// let data = users.filter(name.ne(all(vec!["Sean", "Jim"]))); |
| 54 |
/// assert_eq!(Ok(vec![tess]), data.load(connection)); |
| 55 |
/// # } |
| 56 |
/// ``` |
| 57 |
#[deprecated(since = "2.0.0", note = "Use `ExpressionMethods::ne_all` instead")] |
| 58 |
pub fn all<ST, T>(vals: T) -> All<T::Expression> |
| 59 |
where |
| 60 |
T: AsArrayExpression<ST>, |
| 61 |
{ |
| 62 |
All::new(vals.as_expression()) |
| 63 |
} |
| 64 |
|
| 65 |
#[doc(hidden)] |
| 66 |
#[derive(Debug, Copy, Clone, QueryId, ValidGrouping)] |
| 67 |
pub struct Any<Expr> { |
| 68 |
expr: Expr, |
| 69 |
} |
| 70 |
|
| 71 |
impl<Expr> Any<Expr> { |
| 72 |
fn new(expr: Expr) -> Self { |
| 73 |
Any { expr: expr } |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
impl<Expr, ST> Expression for Any<Expr> |
| 78 |
where |
| 79 |
Expr: Expression<SqlType = Array<ST>>, |
| 80 |
ST: SqlType + TypedExpressionType, |
| 81 |
{ |
| 82 |
type SqlType = ST; |
| 83 |
} |
| 84 |
|
| 85 |
impl<Expr> QueryFragment<Pg> for Any<Expr> |
| 86 |
where |
| 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 |
|
| 97 |
impl_selectable_expression!(Any<Expr>); |
| 98 |
|
| 99 |
#[doc(hidden)] |
| 100 |
#[derive(Debug, Copy, Clone, QueryId, ValidGrouping)] |
| 101 |
pub struct All<Expr> { |
| 102 |
expr: Expr, |
| 103 |
} |
| 104 |
|
| 105 |
impl<Expr> All<Expr> { |
| 106 |
fn new(expr: Expr) -> Self { |
| 107 |
All { expr: expr } |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
impl<Expr, ST> Expression for All<Expr> |
| 112 |
where |
| 113 |
Expr: Expression<SqlType = Array<ST>>, |
| 114 |
ST: SqlType + TypedExpressionType, |
| 115 |
{ |
| 116 |
type SqlType = ST; |
| 117 |
} |
| 118 |
|
| 119 |
impl<Expr> QueryFragment<Pg> for All<Expr> |
| 120 |
where |
| 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 |
|
| 131 |
impl_selectable_expression!(All<Expr>); |
| 132 |
|
| 133 |
/// Deprecated trait used for implementing `any` and `all` (which are themselves deprecated). |
| 134 |
/// |
| 135 |
/// It has several quirks: |
| 136 |
/// - `All<Expr: Expression<SqlType = Array<ST>>>` pretends to be an expression of type `ST`, |
| 137 |
/// but it's in fact some custom that can really be only used in combination with the `=` |
| 138 |
/// operator in some select places. |
| 139 |
/// - Implementations that use Subelect below are lying: they pretend to be expressions of type |
| 140 |
/// `Array<ST>`, but they're actually subselects, which are processed differently by Postgres |
| 141 |
/// and may result in different query plans. |
| 142 |
/// The `IntoArrayExpression` trait represents this more accurately, actually building an |
| 143 |
/// actual expression of type array from a subselect (by wrapping it in `ARRAY(subselect)`). |
| 144 |
pub trait AsArrayExpression<ST: 'static> { |
| 145 |
type Expression: Expression<SqlType = Array<ST>>; |
| 146 |
|
| 147 |
// This method is part of the public API |
| 148 |
// we won't change it to appease a clippy lint |
| 149 |
#[allow(clippy::wrong_self_convention)] |
| 150 |
fn as_expression(self) -> Self::Expression; |
| 151 |
} |
| 152 |
|
| 153 |
impl<ST, T> AsArrayExpression<ST> for T |
| 154 |
where |
| 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 |
|
| 165 |
impl<ST, F, S, D, W, O, LOf, G, H, LC> AsArrayExpression<ST> |
| 166 |
for SelectStatement<F, S, D, W, O, LOf, G, H, LC> |
| 167 |
where |
| 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 |
|
| 178 |
impl<ST, QS, DB, GB> AsArrayExpression<ST> for BoxedSelectStatement<'_, ST, QS, DB, GB> |
| 179 |
where |
| 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 |
} |