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