diesel/expression/
exists.rs1use crate::backend::{sql_dialect, Backend, SqlDialect};
5use crate::expression::subselect::Subselect;
6use crate::expression::{AppearsOnTable, Expression, SelectableExpression, ValidGrouping};
7use crate::helper_types;
8use crate::query_builder::*;
9use crate::result::QueryResult;
10use crate::sql_types::Bool;
11
12pub fn exists<T>(query: T) -> helper_types::exists<T> {
36 Exists {
37 subselect: Subselect::new(query),
38 }
39}
40
41#[derive(Clone, Copy, QueryId, Debug)]
49#[non_exhaustive]
50pub struct Exists<T> {
51 pub subselect: Subselect<T, Bool>,
53}
54
55impl<T> Expression for Exists<T>
56where
57 Subselect<T, Bool>: Expression,
58{
59 type SqlType = Bool;
60}
61
62impl<T, GB> ValidGrouping<GB> for Exists<T>
63where
64 Subselect<T, Bool>: ValidGrouping<GB>,
65{
66 type IsAggregate = <Subselect<T, Bool> as ValidGrouping<GB>>::IsAggregate;
67}
68
69impl<T, DB> QueryFragment<DB> for Exists<T>
70where
71 DB: Backend,
72 Self: QueryFragment<DB, DB::ExistsSyntax>,
73{
74 fn walk_ast<'b>(&'b self, pass: AstPass<'_, 'b, DB>) -> QueryResult<()> {
75 <Self as QueryFragment<DB, DB::ExistsSyntax>>::walk_ast(self, pass)
76 }
77}
78
79impl<T, DB> QueryFragment<DB, sql_dialect::exists_syntax::AnsiSqlExistsSyntax> for Exists<T>
80where
81 DB: Backend + SqlDialect<ExistsSyntax = sql_dialect::exists_syntax::AnsiSqlExistsSyntax>,
82 T: QueryFragment<DB>,
83{
84 fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, DB>) -> QueryResult<()> {
85 out.push_sql("EXISTS (");
86 self.subselect.walk_ast(out.reborrow())?;
87 out.push_sql(")");
88 Ok(())
89 }
90}
91
92impl<T, QS> SelectableExpression<QS> for Exists<T>
93where
94 Self: AppearsOnTable<QS>,
95 Subselect<T, Bool>: SelectableExpression<QS>,
96{
97}
98
99impl<T, QS> AppearsOnTable<QS> for Exists<T>
100where
101 Self: Expression,
102 Subselect<T, Bool>: AppearsOnTable<QS>,
103{
104}