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