sea_query/backend/sqlite/
query.rs

1use super::*;
2use crate::extension::sqlite::SqliteBinOper;
3
4impl QueryBuilder for SqliteQueryBuilder {
5    fn prepare_select_lock(&self, _select_lock: &LockClause, _sql: &mut impl SqlWriter) {
6        // SQLite doesn't supports row locking
7    }
8
9    fn prepare_select_into(&self, _: &SelectInto, _: &mut impl SqlWriter) {
10        // SQLite doesn't support SELECT INOT table clause
11    }
12
13    fn prepare_sub_query_oper(&self, oper: &SubQueryOper, sql: &mut impl SqlWriter) {
14        sql.write_str(match oper {
15            SubQueryOper::Exists => "EXISTS",
16            SubQueryOper::Any => panic!("Operator 'ANY' doesnot support"),
17            SubQueryOper::Some => panic!("Operator 'SOME' doesnot support"),
18            SubQueryOper::All => panic!("Operator 'ALL' doesnot support"),
19        })
20        .unwrap();
21    }
22
23    fn prepare_bin_oper(&self, bin_oper: &BinOper, sql: &mut impl SqlWriter) {
24        match bin_oper {
25            BinOper::SqliteOperator(bin_oper) => sql
26                .write_str(match bin_oper {
27                    SqliteBinOper::Glob => "GLOB",
28                    SqliteBinOper::Match => "MATCH",
29                    SqliteBinOper::GetJsonField => "->",
30                    SqliteBinOper::CastJsonField => "->>",
31                })
32                .unwrap(),
33            _ => self.prepare_bin_oper_common(bin_oper, sql),
34        }
35    }
36
37    fn prepare_union_statement(
38        &self,
39        union_type: UnionType,
40        select_statement: &SelectStatement,
41        sql: &mut impl SqlWriter,
42    ) {
43        match union_type {
44            UnionType::Intersect => sql.write_str(" INTERSECT ").unwrap(),
45            UnionType::Distinct => sql.write_str(" UNION ").unwrap(),
46            UnionType::Except => sql.write_str(" EXCEPT ").unwrap(),
47            UnionType::All => sql.write_str(" UNION ALL ").unwrap(),
48        }
49        self.prepare_select_statement(select_statement, sql);
50    }
51
52    fn prepare_query_statement(&self, query: &SubQueryStatement, sql: &mut impl SqlWriter) {
53        query.prepare_statement(self, sql);
54    }
55
56    fn prepare_with_clause_recursive_options(&self, _: &WithClause, _: &mut impl SqlWriter) {
57        // Sqlite doesn't support sql recursive with query 'SEARCH' and 'CYCLE' options.
58    }
59
60    fn prepare_order_expr(&self, order_expr: &OrderExpr, sql: &mut impl SqlWriter) {
61        if !matches!(order_expr.order, Order::Field(_)) {
62            self.prepare_expr(&order_expr.expr, sql);
63        }
64        self.prepare_order(order_expr, sql);
65        match order_expr.nulls {
66            None => (),
67            Some(NullOrdering::Last) => sql.write_str(" NULLS LAST").unwrap(),
68            Some(NullOrdering::First) => sql.write_str(" NULLS FIRST").unwrap(),
69        }
70    }
71
72    fn prepare_value(&self, value: Value, sql: &mut impl SqlWriter) {
73        sql.push_param(value, self as _);
74    }
75
76    fn greatest_function(&self) -> &str {
77        "MAX"
78    }
79
80    fn least_function(&self) -> &str {
81        "MIN"
82    }
83
84    fn char_length_function(&self) -> &str {
85        "LENGTH"
86    }
87
88    fn insert_default_values(&self, _: u32, sql: &mut impl SqlWriter) {
89        // SQLite doesn't support inserting multiple rows with default values
90        sql.write_str("DEFAULT VALUES").unwrap()
91    }
92}