diesel/expression/
exists.rs1use backend::Backend;
2use expression::subselect::Subselect;
3use expression::{AppearsOnTable, Expression, NonAggregate, SelectableExpression};
4use query_builder::*;
5use result::QueryResult;
6use sql_types::Bool;
7
8pub fn exists<T>(query: T) -> Exists<T> {
33 Exists(Subselect::new(query))
34}
35
36#[derive(Debug, Clone, Copy, QueryId)]
37pub struct Exists<T>(Subselect<T, ()>);
38
39impl<T> Expression for Exists<T>
40where
41 Subselect<T, ()>: Expression,
42{
43 type SqlType = Bool;
44}
45
46impl<T> NonAggregate for Exists<T> {}
47
48impl<T, DB> QueryFragment<DB> for Exists<T>
49where
50 DB: Backend,
51 T: QueryFragment<DB>,
52{
53 fn walk_ast(&self, mut out: AstPass<DB>) -> QueryResult<()> {
54 out.push_sql("EXISTS (");
55 self.0.walk_ast(out.reborrow())?;
56 out.push_sql(")");
57 Ok(())
58 }
59}
60
61impl<T, QS> SelectableExpression<QS> for Exists<T>
62where
63 Self: AppearsOnTable<QS>,
64 Subselect<T, ()>: SelectableExpression<QS>,
65{
66}
67
68impl<T, QS> AppearsOnTable<QS> for Exists<T>
69where
70 Self: Expression,
71 Subselect<T, ()>: AppearsOnTable<QS>,
72{
73}