Skip to main content

sql_builder/backend/
query_builder.rs

1use std::{borrow::Cow, ops::Deref};
2
3use crate::*;
4
5const QUOTE: Quote = Quote(b'"', b'"');
6
7mod common;
8pub(crate) mod conflict;
9mod constants;
10pub(crate) mod expressions;
11pub(crate) mod functions;
12pub(crate) mod statements;
13pub(crate) mod values;
14pub(crate) use common::*;
15
16pub trait QueryBuilder:
17    QuotedBuilder + EscapeBuilder + TableRefBuilder + OperLeftAssocDecider + PrecedenceDecider + Sized
18{
19    /// The type of placeholder the builder uses for values, and whether it is numbered.
20    fn placeholder(&self) -> (&'static str, bool) {
21        statements::placeholder(self)
22    }
23
24    /// Whose lexical rules SQL text written for this builder follows: where
25    /// its strings, comments and markers begin and end.
26    fn lexer_dialect(&self) -> crate::lexer::Dialect;
27
28    /// Prefix for tuples in VALUES list (e.g. ROW for MySQL)
29    fn values_list_tuple_prefix(&self) -> &str {
30        statements::values_list_tuple_prefix(self)
31    }
32
33    fn prepare_union_statement(
34        &self,
35        union_type: UnionType,
36        select_statement: &SelectStatement,
37        sql: &mut impl SqlWriter,
38    ) -> Result<(), RenderError> {
39        statements::prepare_union_statement(self, union_type, select_statement, sql)
40    }
41
42    // Translate the LIMIT and OFFSET expression in [`SelectStatement`]
43    fn prepare_select_limit_offset(
44        &self,
45        select: &SelectStatement,
46        sql: &mut impl SqlWriter,
47    ) -> Result<(), RenderError> {
48        prepare_limit_offset(self, select, None, sql)
49    }
50
51    #[allow(unused_variables)]
52    fn prepare_update_join(
53        &self,
54        from: &[TableRef],
55        condition: &ConditionHolder,
56        sql: &mut impl SqlWriter,
57    ) -> Result<(), RenderError> {
58        // MySQL specific
59        Ok(())
60    }
61
62    fn prepare_update_from(
63        &self,
64        from: &[TableRef],
65        sql: &mut impl SqlWriter,
66    ) -> Result<(), RenderError> {
67        statements::prepare_update_from(self, from, sql)
68    }
69
70    fn prepare_update_column(
71        &self,
72        table: &Option<Box<TableRef>>,
73        from: &[TableRef],
74        column: &DynIden,
75        sql: &mut impl SqlWriter,
76    ) -> Result<(), RenderError> {
77        statements::prepare_update_column(self, table, from, column, sql)
78    }
79
80    fn prepare_update_condition(
81        &self,
82        from: &[TableRef],
83        condition: &ConditionHolder,
84        sql: &mut impl SqlWriter,
85    ) -> Result<(), RenderError> {
86        statements::prepare_update_condition(self, from, condition, sql)
87    }
88
89    /// Translate [`Expr`] into SQL statement.
90    fn prepare_expr(
91        &self,
92        simple_expr: &Expr,
93        sql: &mut impl SqlWriter,
94    ) -> Result<(), RenderError> {
95        expressions::prepare_expr(self, simple_expr, sql)
96    }
97
98    /// Translate [`SelectDistinct`] into SQL statement.
99    fn prepare_select_distinct(
100        &self,
101        select_distinct: &SelectDistinct,
102        sql: &mut impl SqlWriter,
103    ) -> Result<(), RenderError> {
104        expressions::prepare_select_distinct(self, select_distinct, sql)
105    }
106
107    /// Translate [`LockType`] into SQL statement.
108    fn prepare_select_lock(
109        &self,
110        lock: &LockClause,
111        sql: &mut impl SqlWriter,
112    ) -> Result<(), RenderError> {
113        expressions::prepare_select_lock(self, lock, sql)
114    }
115
116    /// Translate [`BinOper`] into SQL statement.
117    fn prepare_bin_oper(
118        &self,
119        bin_oper: &BinOper,
120        sql: &mut impl SqlWriter,
121    ) -> Result<(), RenderError> {
122        expressions::prepare_bin_oper(self, bin_oper, sql)
123    }
124
125    /// Translate [`SubQueryOper`] into SQL statement.
126    fn prepare_sub_query_oper(
127        &self,
128        oper: &SubQueryOper,
129        sql: &mut impl SqlWriter,
130    ) -> Result<(), RenderError> {
131        expressions::prepare_sub_query_oper(self, oper, sql);
132        Ok(())
133    }
134
135    /// Translate [`QueryStatement`] into SQL statement.
136    fn prepare_query_statement(
137        &self,
138        query: &SubQueryStatement,
139        sql: &mut impl SqlWriter,
140    ) -> Result<(), RenderError> {
141        query.prepare_statement(self, sql)
142    }
143
144    fn prepare_select_into(
145        &self,
146        into_table: &SelectInto,
147        sql: &mut impl SqlWriter,
148    ) -> Result<(), RenderError>;
149
150    fn prepare_with_query_clause_materialization(
151        &self,
152        cte: &CommonTableExpression,
153        sql: &mut impl SqlWriter,
154    ) -> Result<(), RenderError> {
155        functions::prepare_with_query_clause_materialization(self, cte, sql)
156    }
157
158    fn prepare_function_name(
159        &self,
160        function: &Func,
161        sql: &mut impl SqlWriter,
162    ) -> Result<(), RenderError> {
163        functions::prepare_function_name_common(self, function, sql)
164    }
165
166    /// Translate [`JoinType`] into SQL statement.
167    fn prepare_join_type(
168        &self,
169        join_type: &JoinType,
170        sql: &mut impl SqlWriter,
171    ) -> Result<(), RenderError> {
172        functions::prepare_join_type_common(self, join_type, sql)
173    }
174
175    /// Translate [`OrderExpr`] into SQL statement.
176    fn prepare_order_expr(
177        &self,
178        order_expr: &OrderExpr,
179        sql: &mut impl SqlWriter,
180    ) -> Result<(), RenderError> {
181        functions::prepare_order_expr(self, order_expr, sql)
182    }
183
184    /// Write [`Value`] into SQL statement as parameter.
185    fn prepare_value(&self, value: Value, sql: &mut impl SqlWriter) -> Result<(), RenderError> {
186        sql.push_param(value, self)
187    }
188
189    #[doc(hidden)]
190    fn write_value(&self, buf: &mut impl SqlWriter, value: &Value) -> Result<(), RenderError> {
191        values::write_value(self, buf, value)
192    }
193
194    #[doc(hidden)]
195    /// Write ON CONFLICT target
196    fn prepare_on_conflict_target(
197        &self,
198        on_conflict_targets: &OnConflictTarget,
199        sql: &mut impl SqlWriter,
200    ) -> Result<(), RenderError> {
201        conflict::prepare_on_conflict_target(self, on_conflict_targets, sql)
202    }
203
204    #[doc(hidden)]
205    /// Write ON CONFLICT action
206    fn prepare_on_conflict_action(
207        &self,
208        on_conflict_action: &Option<OnConflictAction>,
209        sql: &mut impl SqlWriter,
210    ) -> Result<(), RenderError> {
211        conflict::prepare_on_conflict_action(self, on_conflict_action, sql)
212    }
213
214    #[doc(hidden)]
215    /// Write ON CONFLICT keywords
216    fn prepare_on_conflict_keywords(&self, sql: &mut impl SqlWriter) -> Result<(), RenderError> {
217        conflict::prepare_on_conflict_keywords(self, sql);
218        Ok(())
219    }
220
221    #[doc(hidden)]
222    /// Write ON CONFLICT keywords
223    fn prepare_on_conflict_do_update_keywords(
224        &self,
225        sql: &mut impl SqlWriter,
226    ) -> Result<(), RenderError> {
227        conflict::prepare_on_conflict_do_update_keywords(self, sql);
228        Ok(())
229    }
230
231    #[doc(hidden)]
232    /// Write ON CONFLICT update action by retrieving value from the excluded table
233    fn prepare_on_conflict_excluded_table(
234        &self,
235        col: &DynIden,
236        sql: &mut impl SqlWriter,
237    ) -> Result<(), RenderError> {
238        conflict::prepare_on_conflict_excluded_table(self, col, sql)
239    }
240
241    #[doc(hidden)]
242    /// Write ON CONFLICT conditions
243    fn prepare_on_conflict_condition(
244        &self,
245        on_conflict_condition: &ConditionHolder,
246        sql: &mut impl SqlWriter,
247    ) -> Result<(), RenderError> {
248        conflict::prepare_on_conflict_condition(self, on_conflict_condition, sql)
249    }
250
251    #[doc(hidden)]
252    /// Write the WHERE of an ON CONFLICT action
253    fn prepare_on_conflict_action_condition(
254        &self,
255        condition: &ConditionHolder,
256        sql: &mut impl SqlWriter,
257    ) -> Result<(), RenderError> {
258        self.prepare_on_conflict_condition(condition, sql)
259    }
260
261    #[doc(hidden)]
262    /// Write `INSERT`, or `REPLACE` when `replace` is set
263    fn prepare_insert(&self, replace: bool, sql: &mut impl SqlWriter) -> Result<(), RenderError> {
264        functions::prepare_insert(self, replace, sql);
265        Ok(())
266    }
267
268    #[doc(hidden)]
269    /// Write the ORDER BY and LIMIT of an UPDATE or DELETE
270    fn prepare_mutation_order_limit(
271        &self,
272        orders: &[OrderExpr],
273        limit: &Option<Value>,
274        sql: &mut impl SqlWriter,
275    ) -> Result<(), RenderError> {
276        statements::prepare_mutation_order_limit(self, orders, limit, sql)
277    }
278
279    fn write_string_quoted(&self, string: &str, buffer: &mut impl Write) {
280        constants::write_string_quoted(self, string, buffer);
281    }
282
283    #[doc(hidden)]
284    /// Write bytes enclosed with engine specific byte syntax
285    fn write_bytes(&self, bytes: &[u8], buffer: &mut impl Write) {
286        constants::write_bytes(self, bytes, buffer);
287    }
288
289    #[doc(hidden)]
290    /// The name of the function that represents the "if null" condition.
291    fn if_null_function(&self) -> &str {
292        constants::if_null_function(self)
293    }
294
295    #[doc(hidden)]
296    /// The name of the function that represents the "greatest" function.
297    fn greatest_function(&self) -> &str {
298        constants::greatest_function(self)
299    }
300
301    #[doc(hidden)]
302    /// The name of the function that represents the "least" function.
303    fn least_function(&self) -> &str {
304        constants::least_function(self)
305    }
306
307    #[doc(hidden)]
308    /// The name of the function that returns the char length.
309    fn char_length_function(&self) -> &str {
310        constants::char_length_function(self)
311    }
312
313    #[doc(hidden)]
314    /// The name of the function that returns a random number
315    fn random_function(&self) -> &str {
316        constants::random_function(self)
317    }
318
319    #[doc(hidden)]
320    /// The name of the function that returns the lock phrase including the leading 'FOR'
321    fn lock_phrase(&self, lock_type: LockType) -> &'static str {
322        constants::lock_phrase(self, lock_type)
323    }
324
325    /// The keywords for insert default row.
326    fn insert_default_keyword(&self) -> &str {
327        constants::insert_default_keyword(self)
328    }
329
330    /// Write insert default rows expression.
331    fn insert_default_values(
332        &self,
333        num_rows: usize,
334        sql: &mut impl SqlWriter,
335    ) -> Result<(), RenderError> {
336        constants::insert_default_values(self, num_rows, sql)
337    }
338}
339
340#[cfg(test)]
341#[path = "../../tests/unit/query_builder_tests.rs"]
342mod tests;