diesel/sqlite/expression/
expression_methods.rs

1//! Sqlite specific expression methods.
2
3use super::operators::*;
4use crate::dsl;
5use crate::expression::grouped::Grouped;
6use crate::expression::{AsExpression, Expression};
7use crate::sql_types::SqlType;
8
9/// Sqlite specific methods which are present on all expressions.
10#[cfg(feature = "sqlite")]
11pub trait SqliteExpressionMethods: Expression + Sized {
12    /// Creates a Sqlite `IS` expression.
13    ///
14    /// The `IS` operator work like = except when one or both of the operands are NULL.
15    /// In this case, if both operands are NULL, then the `IS` operator evaluates to true.
16    /// If one operand is NULL and the other is not, then the `IS` operator evaluates to false.
17    /// It is not possible for an `IS` expression to evaluate to NULL.
18    ///
19    /// # Example
20    ///
21    /// ```rust
22    /// # include!("../../doctest_setup.rs");
23    /// #
24    /// # fn main() {
25    /// #     run_test().unwrap();
26    /// # }
27    /// #
28    /// # fn run_test() -> QueryResult<()> {
29    /// #     use schema::animals::dsl::*;
30    /// #     let connection = &mut establish_connection();
31    /// let jack_is_a_dog = animals
32    ///     .select(name)
33    ///     .filter(species.is("dog"))
34    ///     .get_results::<Option<String>>(connection)?;
35    /// assert_eq!(vec![Some("Jack".to_string())], jack_is_a_dog);
36    /// #     Ok(())
37    /// # }
38    /// ```
39    fn is<T>(self, other: T) -> dsl::Is<Self, T>
40    where
41        Self::SqlType: SqlType,
42        T: AsExpression<Self::SqlType>,
43    {
44        Grouped(Is::new(self, other.as_expression()))
45    }
46
47    /// Creates a Sqlite `IS NOT` expression.
48    ///
49    /// The `IS NOT` operator work like != except when one or both of the operands are NULL.
50    /// In this case, if both operands are NULL, then the `IS NOT` operator evaluates to false.
51    /// If one operand is NULL and the other is not, then the `IS NOT` operator is true.
52    /// It is not possible for an `IS NOT` expression to evaluate to NULL.
53    ///
54    /// # Example
55    ///
56    /// ```rust
57    /// # include!("../../doctest_setup.rs");
58    /// #
59    /// # fn main() {
60    /// #     run_test().unwrap();
61    /// # }
62    /// #
63    /// # fn run_test() -> QueryResult<()> {
64    /// #     use schema::animals::dsl::*;
65    /// #     let connection = &mut establish_connection();
66    /// let jack_is_not_a_spider = animals
67    ///     .select(name)
68    ///     .filter(species.is_not("spider"))
69    ///     .get_results::<Option<String>>(connection)?;
70    /// assert_eq!(vec![Some("Jack".to_string())], jack_is_not_a_spider);
71    /// #     Ok(())
72    /// # }
73    /// ```
74    #[allow(clippy::wrong_self_convention)] // This is named after the sql operator
75    fn is_not<T>(self, other: T) -> dsl::IsNot<Self, T>
76    where
77        Self::SqlType: SqlType,
78        T: AsExpression<Self::SqlType>,
79    {
80        Grouped(IsNot::new(self, other.as_expression()))
81    }
82}
83
84impl<T: Expression> SqliteExpressionMethods for T {}