Added support for SQLite's IS and IS NOT

This commit is contained in:
hi-rustin 2021-04-21 17:13:30 +08:00
parent 4272ba05e4
commit 11f4369ca5
8 changed files with 107 additions and 0 deletions

View File

@ -58,6 +58,8 @@ for Rust libraries in [RFC #1105](https://github.com/rust-lang/rfcs/blob/master/
* Added ability to create custom collation functions in SQLite.
* Added support for SQLite's `IS` and `IS NOT`.
### Removed
* All previously deprecated items have been removed.

View File

@ -119,3 +119,7 @@ pub use super::functions::helper_types::*;
#[doc(inline)]
#[cfg(feature = "postgres")]
pub use crate::pg::expression::helper_types::*;
#[doc(inline)]
#[cfg(feature = "sqlite")]
pub use crate::sqlite::expression::helper_types::*;

View File

@ -24,3 +24,7 @@ pub use self::text_expression_methods::TextExpressionMethods;
#[cfg(feature = "postgres")]
#[doc(inline)]
pub use crate::pg::expression::expression_methods::*;
#[cfg(feature = "sqlite")]
#[doc(inline)]
pub use crate::sqlite::expression::expression_methods::*;

View File

@ -0,0 +1,72 @@
//! Sqlite specific expression methods.
use super::operators::*;
use crate::dsl;
use crate::expression::grouped::Grouped;
use crate::expression::{AsExpression, Expression};
use crate::sql_types::SqlType;
/// Sqlite specific methods which are present on all expressions.
pub trait SqliteExpressionMethods: Expression + Sized {
/// Creates a Sqlite `IS` expression.
///
/// # Example
///
/// ```rust
/// # include!("../../doctest_setup.rs");
/// #
/// # fn main() {
/// # run_test().unwrap();
/// # }
/// #
/// # fn run_test() -> QueryResult<()> {
/// # use schema::animals::dsl::*;
/// # let connection = establish_connection();
/// let jack_is_a_dog = animals
/// .select(name)
/// .filter(species.is("dog"))
/// .get_results::<Option<String>>(&connection)?;
/// assert_eq!(vec![Some("Jack".to_string())], jack_is_a_dog);
/// # Ok(())
/// # }
/// ```
fn is<T>(self, other: T) -> dsl::Is<Self, T>
where
Self::SqlType: SqlType,
T: AsExpression<Self::SqlType>,
{
Grouped(Is::new(self, other.as_expression()))
}
/// Creates a Sqlite `IS NOT` expression.
///
/// # Example
///
/// ```rust
/// # include!("../../doctest_setup.rs");
/// #
/// # fn main() {
/// # run_test().unwrap();
/// # }
/// #
/// # fn run_test() -> QueryResult<()> {
/// # use schema::animals::dsl::*;
/// # let connection = establish_connection();
/// let jack_is_not_a_spider = animals
/// .select(name)
/// .filter(species.is_not("spider"))
/// .get_results::<Option<String>>(&connection)?;
/// assert_eq!(vec![Some("Jack".to_string())], jack_is_not_a_spider);
/// # Ok(())
/// # }
/// ```
fn is_not<T>(self, other: T) -> dsl::IsNot<Self, T>
where
Self::SqlType: SqlType,
T: AsExpression<Self::SqlType>,
{
Grouped(IsNot::new(self, other.as_expression()))
}
}
impl<T: Expression> SqliteExpressionMethods for T {}

View File

@ -0,0 +1,8 @@
use crate::dsl::AsExpr;
use crate::expression::grouped::Grouped;
/// The return type of `lhs.is(rhs)`.
pub type Is<Lhs, Rhs> = Grouped<super::operators::Is<Lhs, AsExpr<Rhs, Lhs>>>;
/// The return type of `lhs.is_not(rhs)`.
pub type IsNot<Lhs, Rhs> = Grouped<super::operators::IsNot<Lhs, AsExpr<Rhs, Lhs>>>;

View File

@ -0,0 +1,11 @@
//! Sqlite related query builder extensions.
//!
//! Everything in this module is re-exported from database agnostic locations.
//! You should rely on the re-exports rather than this module directly. It is
//! kept separate purely for documentation purposes.
pub(crate) mod expression_methods;
#[doc(hidden)]
pub mod helper_types;
#[doc(hidden)]
pub mod operators;

View File

@ -0,0 +1,5 @@
use crate::sql_types::Bool;
use crate::sqlite::Sqlite;
__diesel_infix_operator!(Is, " IS ", ConstantNullability Bool, backend: Sqlite);
__diesel_infix_operator!(IsNot, " IS NOT ", ConstantNullability Bool, backend: Sqlite);

View File

@ -6,6 +6,7 @@
mod backend;
mod connection;
pub(crate) mod expression;
mod types;
pub mod query_builder;