sea_query/extension/sqlite/
expr.rs

1use crate::{ColumnRef, Expr, ExprTrait, FunctionCall, Keyword, LikeExpr, SimpleExpr, Value};
2
3use super::SqliteBinOper;
4
5pub trait SqliteExpr: ExprTrait {
6    /// Express an sqlite `GLOB` operator.
7    ///
8    /// # Examples
9    ///
10    /// ```
11    /// use sea_query::{extension::sqlite::SqliteExpr, tests_cfg::*, *};
12    ///
13    /// let query = Query::select()
14    ///     .column(Font::Name)
15    ///     .from(Font::Table)
16    ///     .and_where(Expr::col(Font::Name).glob("a"))
17    ///     .to_owned();
18    ///
19    /// assert_eq!(
20    ///     query.to_string(SqliteQueryBuilder),
21    ///     r#"SELECT "name" FROM "font" WHERE "name" GLOB 'a'"#
22    /// );
23    /// ```
24    fn glob<T>(self, right: T) -> SimpleExpr
25    where
26        T: Into<SimpleExpr>,
27    {
28        self.binary(SqliteBinOper::Glob, right)
29    }
30
31    /// Express an sqlite `MATCH` operator.
32    ///
33    /// # Examples
34    ///
35    /// ```
36    /// use sea_query::{extension::sqlite::SqliteExpr, tests_cfg::*, *};
37    ///
38    /// let query = Query::select()
39    ///     .column(Font::Name)
40    ///     .from(Font::Table)
41    ///     .and_where(Expr::col(Font::Name).matches("a"))
42    ///     .to_owned();
43    ///
44    /// assert_eq!(
45    ///     query.to_string(SqliteQueryBuilder),
46    ///     r#"SELECT "name" FROM "font" WHERE "name" MATCH 'a'"#
47    /// );
48    /// ```
49    fn matches<T>(self, right: T) -> SimpleExpr
50    where
51        T: Into<SimpleExpr>,
52    {
53        self.binary(SqliteBinOper::Match, right)
54    }
55
56    /// Express an sqlite retrieves JSON field as JSON value (`->`).
57    ///
58    /// # Examples
59    ///
60    /// ```
61    /// use sea_query::{extension::sqlite::SqliteExpr, tests_cfg::*, *};
62    ///
63    /// let query = Query::select()
64    ///     .column(Font::Variant)
65    ///     .from(Font::Table)
66    ///     .and_where(Expr::col(Font::Variant).get_json_field("a"))
67    ///     .to_owned();
68    ///
69    /// assert_eq!(
70    ///     query.to_string(SqliteQueryBuilder),
71    ///     r#"SELECT "variant" FROM "font" WHERE "variant" -> 'a'"#
72    /// );
73    /// ```
74    fn get_json_field<T>(self, right: T) -> SimpleExpr
75    where
76        T: Into<SimpleExpr>,
77    {
78        self.binary(SqliteBinOper::GetJsonField, right)
79    }
80
81    /// Express an sqlite retrieves JSON field and casts it to an appropriate SQL type (`->>`).
82    ///
83    /// # Examples
84    ///
85    /// ```
86    /// use sea_query::{extension::sqlite::SqliteExpr, tests_cfg::*, *};
87    ///
88    /// let query = Query::select()
89    ///     .column(Font::Variant)
90    ///     .from(Font::Table)
91    ///     .and_where(Expr::col(Font::Variant).cast_json_field("a"))
92    ///     .to_owned();
93    ///
94    /// assert_eq!(
95    ///     query.to_string(SqliteQueryBuilder),
96    ///     r#"SELECT "variant" FROM "font" WHERE "variant" ->> 'a'"#
97    /// );
98    /// ```
99    fn cast_json_field<T>(self, right: T) -> SimpleExpr
100    where
101        T: Into<SimpleExpr>,
102    {
103        self.binary(SqliteBinOper::CastJsonField, right)
104    }
105}
106
107// TODO: https://github.com/SeaQL/sea-query/discussions/795:
108// replace all of this with `impl<T> PgExpr for T where T: ExprTrait {}`
109impl SqliteExpr for Expr {}
110impl SqliteExpr for SimpleExpr {}
111impl SqliteExpr for FunctionCall {}
112impl SqliteExpr for ColumnRef {}
113impl SqliteExpr for Keyword {}
114impl SqliteExpr for LikeExpr {}
115impl SqliteExpr for Value {}