Debian Codegraph @

src/expression/exists.rs

raw
1 //! This module contains the query dsl node definition
2 //! for `EXISTS` expressions
3
4 use crate::backend::{sql_dialect, Backend, SqlDialect};
5 use crate::expression::subselect::Subselect;
6 use crate::expression::{AppearsOnTable, Expression, SelectableExpression, ValidGrouping};
7 use crate::helper_types;
8 use crate::query_builder::*;
9 use crate::result::QueryResult;
10 use crate::sql_types::Bool;
11
12 /// Creates a SQL `EXISTS` expression.
13 ///
14 /// The argument must be a complete SQL query. The query may reference columns
15 /// from the outer table.
16 ///
17 /// # Example
18 ///
19 /// ```rust
20 /// # include!("../doctest_setup.rs");
21 /// #
22 /// # fn main() {
23 /// # use schema::users::dsl::*;
24 /// # use diesel::select;
25 /// # use diesel::dsl::exists;
26 /// # let connection = &mut establish_connection();
27 /// let sean_exists = select(exists(users.filter(name.eq("Sean")))).get_result(connection);
28 /// let jim_exists = select(exists(users.filter(name.eq("Jim")))).get_result(connection);
29 /// assert_eq!(Ok(true), sean_exists);
30 /// assert_eq!(Ok(false), jim_exists);
31 /// # }
32 /// ```
33 pub fn exists<T>(query: T) -> helper_types::exists<T> {
34 Exists {
35 subselect: Subselect::new(query),
36 }
37 }
38
39 /// The query dsl node that represents a SQL `EXISTS (subselect)` expression.
40 ///
41 /// Third party backend can customize the [`QueryFragment`]
42 /// implementation of this query dsl node via
43 /// [`SqlDialect::ExistsSyntax`]. A customized implementation
44 /// is expected to provide the same semantics as an ANSI SQL
45 /// `EXIST (subselect)` expression.
46 #[derive(Clone, Copy, QueryId, Debug)]
47 #[non_exhaustive]
48 pub struct Exists<T> {
49 /// The inner subselect
50 pub subselect: Subselect<T, Bool>,
51 }
52
53 impl<T> Expression for Exists<T>
54 where
55 Subselect<T, Bool>: Expression,
56 {
57 type SqlType = Bool;
58 }
59
60 impl<T, GB> ValidGrouping<GB> for Exists<T>
61 where
62 Subselect<T, Bool>: ValidGrouping<GB>,
63 {
64 type IsAggregate = <Subselect<T, Bool> as ValidGrouping<GB>>::IsAggregate;
65 }
66
67 impl<T, DB> QueryFragment<DB> for Exists<T>
68 where
69 DB: Backend,
70 Self: QueryFragment<DB, DB::ExistsSyntax>,
71 {
72 fn walk_ast<'b>(&'b self, pass: AstPass<'_, 'b, DB>) -> QueryResult<()> {
73 <Self as QueryFragment<DB, DB::ExistsSyntax>>::walk_ast(self, pass)
74 }
75 }
76
77 impl<T, DB> QueryFragment<DB, sql_dialect::exists_syntax::AnsiSqlExistsSyntax> for Exists<T>
78 where
79 DB: Backend + SqlDialect<ExistsSyntax = sql_dialect::exists_syntax::AnsiSqlExistsSyntax>,
80 T: QueryFragment<DB>,
81 {
82 fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, DB>) -> QueryResult<()> {
83 out.push_sql("EXISTS (");
84 self.subselect.walk_ast(out.reborrow())?;
85 out.push_sql(")");
86 Ok(())
87 }
88 }
89
90 impl<T, QS> SelectableExpression<QS> for Exists<T>
91 where
92 Self: AppearsOnTable<QS>,
93 Subselect<T, Bool>: SelectableExpression<QS>,
94 {
95 }
96
97 impl<T, QS> AppearsOnTable<QS> for Exists<T>
98 where
99 Self: Expression,
100 Subselect<T, Bool>: AppearsOnTable<QS>,
101 {
102 }