-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a function for SQL EXISTS
expressions.
#534
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
use backend::Backend; | ||
use expression::{Expression, SelectableExpression, NonAggregate}; | ||
use query_builder::*; | ||
use result::QueryResult; | ||
use types::Bool; | ||
|
||
/// Creates a SQL `EXISTS` expression. | ||
/// | ||
/// The argument must be a complete SQL query. The result of this could in | ||
/// theory be passed to `.filter`, but since the query cannot reference columns | ||
/// from the outer query, this is of limited usefulness. | ||
/// | ||
/// # Example | ||
/// | ||
/// ```rust | ||
/// # #[macro_use] extern crate diesel; | ||
/// # include!("src/doctest_setup.rs"); | ||
/// # | ||
/// # table! { | ||
/// # users { | ||
/// # id -> Integer, | ||
/// # name -> VarChar, | ||
/// # } | ||
/// # } | ||
/// # | ||
/// # fn main() { | ||
/// # use self::users::dsl::*; | ||
/// # use diesel::select; | ||
/// # use diesel::expression::dsl::exists; | ||
/// # let connection = establish_connection(); | ||
/// let sean_exists = select(exists(users.filter(name.eq("Sean")))) | ||
/// .get_result(&connection); | ||
/// let jim_exists = select(exists(users.filter(name.eq("Jim")))) | ||
/// .get_result(&connection); | ||
/// assert_eq!(Ok(true), sean_exists); | ||
/// assert_eq!(Ok(false), jim_exists); | ||
/// # } | ||
/// ``` | ||
pub fn exists<T: AsQuery>(query: T) -> Exists<T::Query> { | ||
Exists(query.as_query()) | ||
} | ||
|
||
#[derive(Debug, Clone, Copy)] | ||
pub struct Exists<T>(T); | ||
|
||
impl<T> Expression for Exists<T> where | ||
T: Query, | ||
{ | ||
type SqlType = Bool; | ||
} | ||
|
||
impl<T, QS> SelectableExpression<QS> for Exists<T> where | ||
Exists<T>: Expression, | ||
{ | ||
} | ||
|
||
impl<T> NonAggregate for Exists<T> { | ||
} | ||
|
||
impl<T, DB> QueryFragment<DB> for Exists<T> where | ||
DB: Backend, | ||
T: QueryFragment<DB>, | ||
{ | ||
fn to_sql(&self, out: &mut DB::QueryBuilder) -> BuildQueryResult { | ||
out.push_sql("EXISTS ("); | ||
try!(self.0.to_sql(out)); | ||
out.push_sql(")"); | ||
Ok(()) | ||
} | ||
|
||
fn collect_binds(&self, out: &mut DB::BindCollector) -> QueryResult<()> { | ||
try!(self.0.collect_binds(out)); | ||
Ok(()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't the inner There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, but I quite frequently end up adding fields to these things, so I've been consistently writing it like this (and never relying on the return value of the last delegating line) to make it easier to add more fields in the future with less diff noise There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point. I appreciate les diff noise :) |
||
} | ||
|
||
fn is_safe_to_cache_prepared(&self) -> bool { | ||
self.0.is_safe_to_cache_prepared() | ||
} | ||
} | ||
|
||
impl_query_id!(Exists<T>); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add newline after headline