Skip to main content

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_explain_statement(&self, explain: &ExplainStatement, sql: &mut impl SqlWriter) {
57        sql.write_str("EXPLAIN").unwrap();
58        explain.sqlite_opts.write_to(sql);
59        if let Some(statement) = &explain.statement {
60            sql.write_str(" ").unwrap();
61            statement.write_to(self, sql);
62        }
63    }
64
65    fn prepare_with_clause_recursive_options(&self, _: &WithClause, _: &mut impl SqlWriter) {
66        // Sqlite doesn't support sql recursive with query 'SEARCH' and 'CYCLE' options.
67    }
68
69    fn prepare_order_expr(&self, order_expr: &OrderExpr, sql: &mut impl SqlWriter) {
70        if !matches!(order_expr.order, Order::Field(_)) {
71            self.prepare_expr(&order_expr.expr, sql);
72        }
73        self.prepare_order(order_expr, sql);
74        match order_expr.nulls {
75            None => (),
76            Some(NullOrdering::Last) => sql.write_str(" NULLS LAST").unwrap(),
77            Some(NullOrdering::First) => sql.write_str(" NULLS FIRST").unwrap(),
78        }
79    }
80
81    fn prepare_value(&self, value: Value, sql: &mut impl SqlWriter) {
82        sql.push_param(value, self as _);
83    }
84
85    fn greatest_function(&self) -> &str {
86        "MAX"
87    }
88
89    fn least_function(&self) -> &str {
90        "MIN"
91    }
92
93    fn char_length_function(&self) -> &str {
94        "LENGTH"
95    }
96
97    fn insert_default_values(&self, _: u32, sql: &mut impl SqlWriter) {
98        // SQLite doesn't support inserting multiple rows with default values
99        sql.write_str("DEFAULT VALUES").unwrap()
100    }
101}