//! This module contains the query dsl node definitions //! for array comparison operations like `IN` and `NOT IN` use super::expression_types::NotSelectable; use crate::backend::{sql_dialect, Backend, SqlDialect}; use crate::expression::subselect::Subselect; use crate::expression::{ AppearsOnTable, AsExpression, Expression, SelectableExpression, TypedExpressionType, ValidGrouping, }; use crate::query_builder::combination_clause::CombinationClause; use crate::query_builder::{ AstPass, BoxedSelectStatement, QueryFragment, QueryId, SelectQuery, SelectStatement, }; use crate::result::QueryResult; use crate::serialize::ToSql; use crate::sql_types::{self, HasSqlType, SingleValue, SqlType}; use std::marker::PhantomData; /// Query dsl node that represents a `left IN (values)` /// expression /// /// Third party backend can customize the [`QueryFragment`] /// implementation of this query dsl node via /// [`SqlDialect::ArrayComparison`]. A customized implementation /// is expected to provide the same semantics as an ANSI SQL /// `IN` expression. /// /// The postgres backend provided a specialized implementation /// by using `left = ANY(values)` as optimized variant instead /// if this is possible. For cases where this is not possible /// like for example if values is a vector of arrays we /// generate an ordinary `IN` expression instead. #[derive(Debug, Copy, Clone, QueryId, ValidGrouping)] #[non_exhaustive] pub struct In { /// The expression on the left side of the `IN` keyword pub left: T, /// The values clause of the `IN` expression pub values: U, } /// Query dsl node that represents a `left NOT IN (values)` /// expression /// /// Third party backend can customize the [`QueryFragment`] /// implementation of this query dsl node via /// [`SqlDialect::ArrayComparison`]. A customized implementation /// is expected to provide the same semantics as an ANSI SQL /// `NOT IN` expression.0 /// /// The postgres backend provided a specialized implementation /// by using `left != ALL(values)` as optimized variant instead /// if this is possible. For cases where this is not possible /// like for example if values is a vector of arrays we /// generate a ordinary `NOT IN` expression instead #[derive(Debug, Copy, Clone, QueryId, ValidGrouping)] #[non_exhaustive] pub struct NotIn { /// The expression on the left side of the `NOT IN` keyword pub left: T, /// The values clause of the `NOT IN` expression pub values: U, } impl In { pub(crate) fn new(left: T, values: U) -> Self { In { left, values } } pub(crate) fn walk_ansi_ast<'b, DB>(&'b self, mut out: AstPass<'_, 'b, DB>) -> QueryResult<()> where DB: Backend, T: QueryFragment, U: QueryFragment + InExpression, { if self.values.is_empty() { out.push_sql("1=0"); } else { self.left.walk_ast(out.reborrow())?; out.push_sql(" IN ("); self.values.walk_ast(out.reborrow())?; out.push_sql(")"); } Ok(()) } } impl NotIn { pub(crate) fn new(left: T, values: U) -> Self { NotIn { left, values } } pub(crate) fn walk_ansi_ast<'b, DB>(&'b self, mut out: AstPass<'_, 'b, DB>) -> QueryResult<()> where DB: Backend, T: QueryFragment, U: QueryFragment + InExpression, { if self.values.is_empty() { out.push_sql("1=1"); } else { self.left.walk_ast(out.reborrow())?; out.push_sql(" NOT IN ("); self.values.walk_ast(out.reborrow())?; out.push_sql(")"); } Ok(()) } } impl Expression for In where T: Expression, U: InExpression, T::SqlType: SqlType, sql_types::is_nullable::IsSqlTypeNullable: sql_types::MaybeNullableType, { type SqlType = sql_types::is_nullable::MaybeNullable< sql_types::is_nullable::IsSqlTypeNullable, sql_types::Bool, >; } impl Expression for NotIn where T: Expression, U: InExpression, T::SqlType: SqlType, sql_types::is_nullable::IsSqlTypeNullable: sql_types::MaybeNullableType, { type SqlType = sql_types::is_nullable::MaybeNullable< sql_types::is_nullable::IsSqlTypeNullable, sql_types::Bool, >; } impl QueryFragment for In where DB: Backend, Self: QueryFragment, { fn walk_ast<'b>(&'b self, pass: AstPass<'_, 'b, DB>) -> QueryResult<()> { >::walk_ast(self, pass) } } impl QueryFragment for In where DB: Backend + SqlDialect, T: QueryFragment, U: QueryFragment + InExpression, { fn walk_ast<'b>(&'b self, out: AstPass<'_, 'b, DB>) -> QueryResult<()> { self.walk_ansi_ast(out) } } impl QueryFragment for NotIn where DB: Backend, Self: QueryFragment, { fn walk_ast<'b>(&'b self, pass: AstPass<'_, 'b, DB>) -> QueryResult<()> { >::walk_ast(self, pass) } } impl QueryFragment for NotIn where DB: Backend + SqlDialect, T: QueryFragment, U: QueryFragment + InExpression, { fn walk_ast<'b>(&'b self, out: AstPass<'_, 'b, DB>) -> QueryResult<()> { self.walk_ansi_ast(out) } } impl_selectable_expression!(In); impl_selectable_expression!(NotIn); /// This trait describes how a type is transformed to the /// `IN (values)` value expression /// /// Diesel provided several implementations here: /// /// - An implementation for any [`Iterator`] over values /// that implement [`AsExpression`] for the corresponding /// sql type ST. The corresponding values clause will contain /// bind statements for each individual value. /// - An implementation for select statements, that returns /// a single field. The corresponding values clause will contain /// the sub query. /// /// This trait is exposed for custom third party backends so /// that they can restrict the [`QueryFragment`] implementations /// for [`In`] and [`NotIn`]. pub trait AsInExpression { /// Type of the expression returned by [AsInExpression::as_in_expression] type InExpression: InExpression; /// Construct the diesel query dsl representation of /// the `IN (values)` clause for the given type #[allow(clippy::wrong_self_convention)] // That's a public api, we cannot just change it to // appease clippy fn as_in_expression(self) -> Self::InExpression; } impl AsInExpression for I where I: IntoIterator, T: AsExpression, ST: SqlType + TypedExpressionType, { type InExpression = Many; fn as_in_expression(self) -> Self::InExpression { Many { values: self.into_iter().collect(), p: PhantomData, } } } /// A marker trait that identifies query fragments that can be used in `IN(...)` and `NOT IN(...)` /// clauses, (or `= ANY (...)` clauses on the Postgres backend) /// /// These can be wrapped in [`In`] or [`NotIn`] query dsl nodes pub trait InExpression { /// The SQL type of the inner values, which should be the same as the left of the `IN` or /// `NOT IN` clause type SqlType: SqlType; /// Returns `true` if self represents an empty collection /// Otherwise `false` is returned. fn is_empty(&self) -> bool; /// Returns `true` if the values clause represents /// bind values and each bind value is a postgres array type fn is_array(&self) -> bool; } impl AsInExpression for SelectStatement where ST: SqlType, Subselect: Expression, Self: SelectQuery, { type InExpression = Subselect; fn as_in_expression(self) -> Self::InExpression { Subselect::new(self) } } impl<'a, ST, QS, DB, GB> AsInExpression for BoxedSelectStatement<'a, ST, QS, DB, GB> where ST: SqlType, Subselect, ST>: Expression, { type InExpression = Subselect; fn as_in_expression(self) -> Self::InExpression { Subselect::new(self) } } impl AsInExpression for CombinationClause where ST: SqlType, Self: SelectQuery, Subselect: Expression, { type InExpression = Subselect; fn as_in_expression(self) -> Self::InExpression { Subselect::new(self) } } /// Query dsl node for the `values` part of an `IN (values)` clause /// containing a variable number of bind values. /// /// Third party backend can customize the [`QueryFragment`] /// implementation of this query dsl node via /// [`SqlDialect::ArrayComparison`]. The default /// implementation does generate one bind per value /// in the `values` field. /// /// Diesel provides an optimized implementation for Postgresql /// like database systems that bind all values with one /// bind value of the type `Array` instead. #[derive(Debug, Clone)] pub struct Many { /// The values contained in the `IN (values)` clause pub values: Vec, p: PhantomData, } impl ValidGrouping for Many where ST: SingleValue, I: AsExpression, I::Expression: ValidGrouping, { type IsAggregate = >::IsAggregate; } impl Expression for Many where ST: TypedExpressionType, { // Comma-ed fake expressions are not usable directly in SQL // This is only implemented so that we can use the usual SelectableExpression & co traits // as constraints for the same implementations on [`In`] and [`NotIn`] type SqlType = NotSelectable; } impl InExpression for Many where ST: SqlType, { type SqlType = ST; fn is_empty(&self) -> bool { self.values.is_empty() } fn is_array(&self) -> bool { ST::IS_ARRAY } } impl SelectableExpression for Many where Many: AppearsOnTable, ST: SingleValue, I: AsExpression, >::Expression: SelectableExpression, { } impl AppearsOnTable for Many where Many: Expression, I: AsExpression, ST: SingleValue, >::Expression: SelectableExpression, { } impl QueryFragment for Many where Self: QueryFragment, DB: Backend, { fn walk_ast<'b>(&'b self, pass: AstPass<'_, 'b, DB>) -> QueryResult<()> { >::walk_ast(self, pass) } } impl QueryFragment for Many where DB: Backend + HasSqlType + SqlDialect, ST: SingleValue, I: ToSql, { fn walk_ast<'b>(&'b self, out: AstPass<'_, 'b, DB>) -> QueryResult<()> { self.walk_ansi_ast(out) } } impl Many { pub(crate) fn walk_ansi_ast<'b, DB>(&'b self, mut out: AstPass<'_, 'b, DB>) -> QueryResult<()> where DB: Backend + HasSqlType, ST: SingleValue, I: ToSql, { out.unsafe_to_cache_prepared(); let mut first = true; for value in &self.values { if first { first = false; } else { out.push_sql(", "); } out.push_bind_param(value)?; } Ok(()) } } impl QueryId for Many { type QueryId = (); const HAS_STATIC_QUERY_ID: bool = false; }