sql_builder/backend/sqlite/
query.rs1use super::*;
2
3impl QueryBuilder for SqliteQueryBuilder {
4 fn lexer_dialect(&self) -> crate::lexer::Dialect {
5 crate::lexer::Dialect::Sqlite
6 }
7
8 fn prepare_expr(
9 &self,
10 simple_expr: &Expr,
11 sql: &mut impl SqlWriter,
12 ) -> Result<(), RenderError> {
13 if matches!(simple_expr, Expr::Keyword(Keyword::Default)) {
14 return Err(RenderError::Unsupported(
15 "SQLite has no DEFAULT value; leave the column out of the INSERT".into(),
16 ));
17 }
18 expressions::prepare_expr_common(self, simple_expr, sql)
19 }
20
21 fn prepare_select_lock(
22 &self,
23 _select_lock: &LockClause,
24 _sql: &mut impl SqlWriter,
25 ) -> Result<(), RenderError> {
26 Ok(())
28 }
29
30 fn prepare_select_into(
31 &self,
32 _: &SelectInto,
33 _: &mut impl SqlWriter,
34 ) -> Result<(), RenderError> {
35 Err(RenderError::Unsupported(
36 "SQLite has no SELECT INTO a table".into(),
37 ))
38 }
39
40 fn prepare_on_conflict_target(
41 &self,
42 targets: &OnConflictTarget,
43 sql: &mut impl SqlWriter,
44 ) -> Result<(), RenderError> {
45 if let OnConflictTarget::Constraint(_) = targets {
46 return Err(RenderError::Unsupported(
47 "SQLite has no ON CONFLICT ON CONSTRAINT; name the columns".into(),
48 ));
49 }
50 conflict::prepare_on_conflict_target(self, targets, sql)
51 }
52
53 fn prepare_sub_query_oper(
54 &self,
55 oper: &SubQueryOper,
56 sql: &mut impl SqlWriter,
57 ) -> Result<(), RenderError> {
58 sql.write_str(match oper {
59 SubQueryOper::Exists => "EXISTS",
60 SubQueryOper::Any => {
61 return Err(RenderError::Unsupported(
62 "SQLite has no ANY subquery operator".into(),
63 ));
64 }
65 SubQueryOper::Some => {
66 return Err(RenderError::Unsupported(
67 "SQLite has no SOME subquery operator".into(),
68 ));
69 }
70 SubQueryOper::All => {
71 return Err(RenderError::Unsupported(
72 "SQLite has no ALL subquery operator".into(),
73 ));
74 }
75 })
76 .unwrap();
77 Ok(())
78 }
79
80 fn prepare_bin_oper(
81 &self,
82 bin_oper: &BinOper,
83 sql: &mut impl SqlWriter,
84 ) -> Result<(), RenderError> {
85 match bin_oper {
86 BinOper::SqliteOperator(oper) => sql.write_str(oper.sql()).unwrap(),
87 _ => expressions::prepare_bin_oper_common(self, bin_oper, sql)?,
88 }
89 Ok(())
90 }
91
92 fn prepare_union_statement(
93 &self,
94 union_type: UnionType,
95 select_statement: &SelectStatement,
96 sql: &mut impl SqlWriter,
97 ) -> Result<(), RenderError> {
98 match union_type {
99 UnionType::Intersect => sql.write_str(" INTERSECT ").unwrap(),
100 UnionType::Distinct => sql.write_str(" UNION ").unwrap(),
101 UnionType::Except => sql.write_str(" EXCEPT ").unwrap(),
102 UnionType::All => sql.write_str(" UNION ALL ").unwrap(),
103 }
104 if select_statement.orders.is_empty()
107 && select_statement.limit.is_none()
108 && select_statement.offset.is_none()
109 && select_statement.unions.is_empty()
110 && select_statement.with.is_none()
111 {
112 return statements::prepare_select_statement(self, select_statement, sql);
113 }
114 let mut derived = SelectStatement::new();
115 derived
116 .column(Asterisk)
117 .from_subquery(select_statement.clone(), "tideorm_union_operand");
118 statements::prepare_select_statement(self, &derived, sql)
119 }
120
121 fn prepare_select_limit_offset(
122 &self,
123 select: &SelectStatement,
124 sql: &mut impl SqlWriter,
125 ) -> Result<(), RenderError> {
126 prepare_limit_offset(self, select, SqlDialect::open_limit(self), sql)
127 }
128
129 fn greatest_function(&self) -> &str {
130 "MAX"
131 }
132
133 fn least_function(&self) -> &str {
134 "MIN"
135 }
136
137 fn char_length_function(&self) -> &str {
138 "LENGTH"
139 }
140
141 fn insert_default_values(
142 &self,
143 num_rows: usize,
144 sql: &mut impl SqlWriter,
145 ) -> Result<(), RenderError> {
146 if num_rows <= 1 {
150 sql.write_str("DEFAULT VALUES").unwrap();
151 return Ok(());
152 }
153 sql.write_str("(rowid) VALUES (NULL)").unwrap();
154 for _ in 1..num_rows {
155 sql.write_str(", (NULL)").unwrap();
156 }
157 Ok(())
158 }
159}