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> {
34 Exists {
35 subselect: Subselect::new(query),
36 }
37}
38
39#[derive(Clone, Copy, QueryId, Debug)]
47#[non_exhaustive]
48pub struct Exists<T> {
49 pub subselect: Subselect<T, Bool>,
51}
52
53impl<T> Expression for Exists<T>
54where
55 Subselect<T, Bool>: Expression,
56{
57 type SqlType = Bool;
58}
59
60impl<T, GB> ValidGrouping<GB> for Exists<T>
61where
62 Subselect<T, Bool>: ValidGrouping<GB>,
63{
64 type IsAggregate = <Subselect<T, Bool> as ValidGrouping<GB>>::IsAggregate;
65}
66
67impl<T, DB> QueryFragment<DB> for Exists<T>
68where
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
77impl<T, DB> QueryFragment<DB, sql_dialect::exists_syntax::AnsiSqlExistsSyntax> for Exists<T>
78where
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
90impl<T, QS> SelectableExpression<QS> for Exists<T>
91where
92 Self: AppearsOnTable<QS>,
93 Subselect<T, Bool>: SelectableExpression<QS>,
94{
95}
96
97impl<T, QS> AppearsOnTable<QS> for Exists<T>
98where
99 Self: Expression,
100 Subselect<T, Bool>: AppearsOnTable<QS>,
101{
102}