tideorm-sql-builder 0.2.0

SQL builder for TideORM
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
use std::{borrow::Cow, ops::Deref};

use crate::*;

const QUOTE: Quote = Quote(b'"', b'"');

mod common;
pub(crate) mod conflict;
mod constants;
pub(crate) mod expressions;
pub(crate) mod functions;
pub(crate) mod statements;
pub(crate) mod values;
pub(crate) use common::*;

pub trait QueryBuilder:
    QuotedBuilder + EscapeBuilder + TableRefBuilder + OperLeftAssocDecider + PrecedenceDecider + Sized
{
    /// The type of placeholder the builder uses for values, and whether it is numbered.
    fn placeholder(&self) -> (&'static str, bool) {
        statements::placeholder(self)
    }

    /// Whose lexical rules SQL text written for this builder follows: where
    /// its strings, comments and markers begin and end.
    fn lexer_dialect(&self) -> crate::lexer::Dialect;

    /// Prefix for tuples in VALUES list (e.g. ROW for MySQL)
    fn values_list_tuple_prefix(&self) -> &str {
        statements::values_list_tuple_prefix(self)
    }

    fn prepare_union_statement(
        &self,
        union_type: UnionType,
        select_statement: &SelectStatement,
        sql: &mut impl SqlWriter,
    ) -> Result<(), RenderError> {
        statements::prepare_union_statement(self, union_type, select_statement, sql)
    }

    // Translate the LIMIT and OFFSET expression in [`SelectStatement`]
    fn prepare_select_limit_offset(
        &self,
        select: &SelectStatement,
        sql: &mut impl SqlWriter,
    ) -> Result<(), RenderError> {
        prepare_limit_offset(self, select, None, sql)
    }

    #[allow(unused_variables)]
    fn prepare_update_join(
        &self,
        from: &[TableRef],
        condition: &ConditionHolder,
        sql: &mut impl SqlWriter,
    ) -> Result<(), RenderError> {
        // MySQL specific
        Ok(())
    }

    fn prepare_update_from(
        &self,
        from: &[TableRef],
        sql: &mut impl SqlWriter,
    ) -> Result<(), RenderError> {
        statements::prepare_update_from(self, from, sql)
    }

    fn prepare_update_column(
        &self,
        table: &Option<Box<TableRef>>,
        from: &[TableRef],
        column: &DynIden,
        sql: &mut impl SqlWriter,
    ) -> Result<(), RenderError> {
        statements::prepare_update_column(self, table, from, column, sql)
    }

    fn prepare_update_condition(
        &self,
        from: &[TableRef],
        condition: &ConditionHolder,
        sql: &mut impl SqlWriter,
    ) -> Result<(), RenderError> {
        statements::prepare_update_condition(self, from, condition, sql)
    }

    /// Translate [`Expr`] into SQL statement.
    fn prepare_expr(
        &self,
        simple_expr: &Expr,
        sql: &mut impl SqlWriter,
    ) -> Result<(), RenderError> {
        expressions::prepare_expr(self, simple_expr, sql)
    }

    /// Translate [`SelectDistinct`] into SQL statement.
    fn prepare_select_distinct(
        &self,
        select_distinct: &SelectDistinct,
        sql: &mut impl SqlWriter,
    ) -> Result<(), RenderError> {
        expressions::prepare_select_distinct(self, select_distinct, sql)
    }

    /// Translate [`LockType`] into SQL statement.
    fn prepare_select_lock(
        &self,
        lock: &LockClause,
        sql: &mut impl SqlWriter,
    ) -> Result<(), RenderError> {
        expressions::prepare_select_lock(self, lock, sql)
    }

    /// Translate [`BinOper`] into SQL statement.
    fn prepare_bin_oper(
        &self,
        bin_oper: &BinOper,
        sql: &mut impl SqlWriter,
    ) -> Result<(), RenderError> {
        expressions::prepare_bin_oper(self, bin_oper, sql)
    }

    /// Translate [`SubQueryOper`] into SQL statement.
    fn prepare_sub_query_oper(
        &self,
        oper: &SubQueryOper,
        sql: &mut impl SqlWriter,
    ) -> Result<(), RenderError> {
        expressions::prepare_sub_query_oper(self, oper, sql);
        Ok(())
    }

    /// Translate [`QueryStatement`] into SQL statement.
    fn prepare_query_statement(
        &self,
        query: &SubQueryStatement,
        sql: &mut impl SqlWriter,
    ) -> Result<(), RenderError> {
        query.prepare_statement(self, sql)
    }

    fn prepare_select_into(
        &self,
        into_table: &SelectInto,
        sql: &mut impl SqlWriter,
    ) -> Result<(), RenderError>;

    fn prepare_with_query_clause_materialization(
        &self,
        cte: &CommonTableExpression,
        sql: &mut impl SqlWriter,
    ) -> Result<(), RenderError> {
        functions::prepare_with_query_clause_materialization(self, cte, sql)
    }

    #[doc(hidden)]
    /// Write the body of a common table expression
    fn prepare_cte_query(
        &self,
        query: &SubQueryStatement,
        sql: &mut impl SqlWriter,
    ) -> Result<(), RenderError> {
        functions::prepare_cte_query(self, query, sql)
    }

    #[doc(hidden)]
    /// Write the WITH clause that precedes an INSERT
    fn prepare_insert_with(
        &self,
        with: &WithClause,
        sql: &mut impl SqlWriter,
    ) -> Result<(), RenderError> {
        functions::prepare_with_clause(self, with, sql)
    }

    fn prepare_function_name(
        &self,
        function: &Func,
        sql: &mut impl SqlWriter,
    ) -> Result<(), RenderError> {
        functions::prepare_function_name_common(self, function, sql)
    }

    /// Translate [`JoinType`] into SQL statement.
    fn prepare_join_type(
        &self,
        join_type: &JoinType,
        sql: &mut impl SqlWriter,
    ) -> Result<(), RenderError> {
        functions::prepare_join_type_common(self, join_type, sql)
    }

    /// Translate [`OrderExpr`] into SQL statement.
    fn prepare_order_expr(
        &self,
        order_expr: &OrderExpr,
        sql: &mut impl SqlWriter,
    ) -> Result<(), RenderError> {
        functions::prepare_order_expr(self, order_expr, sql)
    }

    /// Write [`Value`] into SQL statement as parameter.
    fn prepare_value(&self, value: Value, sql: &mut impl SqlWriter) -> Result<(), RenderError> {
        sql.push_param(value, self)
    }

    #[doc(hidden)]
    fn write_value(&self, buf: &mut impl SqlWriter, value: &Value) -> Result<(), RenderError> {
        values::write_value(self, buf, value)
    }

    #[doc(hidden)]
    /// Write ON CONFLICT target
    fn prepare_on_conflict_target(
        &self,
        on_conflict_targets: &OnConflictTarget,
        sql: &mut impl SqlWriter,
    ) -> Result<(), RenderError> {
        conflict::prepare_on_conflict_target(self, on_conflict_targets, sql)
    }

    #[doc(hidden)]
    /// Write ON CONFLICT action
    fn prepare_on_conflict_action(
        &self,
        on_conflict_action: &Option<OnConflictAction>,
        sql: &mut impl SqlWriter,
    ) -> Result<(), RenderError> {
        conflict::prepare_on_conflict_action(self, on_conflict_action, sql)
    }

    #[doc(hidden)]
    /// Write ON CONFLICT keywords
    fn prepare_on_conflict_keywords(&self, sql: &mut impl SqlWriter) -> Result<(), RenderError> {
        conflict::prepare_on_conflict_keywords(self, sql);
        Ok(())
    }

    #[doc(hidden)]
    /// Write ON CONFLICT keywords
    fn prepare_on_conflict_do_update_keywords(
        &self,
        sql: &mut impl SqlWriter,
    ) -> Result<(), RenderError> {
        conflict::prepare_on_conflict_do_update_keywords(self, sql);
        Ok(())
    }

    #[doc(hidden)]
    /// Write ON CONFLICT update action by retrieving value from the excluded table
    fn prepare_on_conflict_excluded_table(
        &self,
        col: &DynIden,
        sql: &mut impl SqlWriter,
    ) -> Result<(), RenderError> {
        conflict::prepare_on_conflict_excluded_table(self, col, sql)
    }

    #[doc(hidden)]
    /// Write ON CONFLICT conditions
    fn prepare_on_conflict_condition(
        &self,
        on_conflict_condition: &ConditionHolder,
        sql: &mut impl SqlWriter,
    ) -> Result<(), RenderError> {
        conflict::prepare_on_conflict_condition(self, on_conflict_condition, sql)
    }

    #[doc(hidden)]
    /// Write the WHERE of an ON CONFLICT action
    fn prepare_on_conflict_action_condition(
        &self,
        condition: &ConditionHolder,
        sql: &mut impl SqlWriter,
    ) -> Result<(), RenderError> {
        self.prepare_on_conflict_condition(condition, sql)
    }

    #[doc(hidden)]
    /// Write `INSERT`, or `REPLACE` when `replace` is set
    fn prepare_insert(&self, replace: bool, sql: &mut impl SqlWriter) -> Result<(), RenderError> {
        functions::prepare_insert(self, replace, sql);
        Ok(())
    }

    #[doc(hidden)]
    /// Write the ORDER BY and LIMIT of an UPDATE or DELETE
    fn prepare_mutation_order_limit(
        &self,
        orders: &[OrderExpr],
        limit: &Option<Value>,
        sql: &mut impl SqlWriter,
    ) -> Result<(), RenderError> {
        statements::prepare_mutation_order_limit(self, orders, limit, sql)
    }

    #[doc(hidden)]
    /// Write the ORDER BY and LIMIT of an UPDATE, which joins the `from` tables
    fn prepare_update_order_limit(
        &self,
        _from: &[TableRef],
        orders: &[OrderExpr],
        limit: &Option<Value>,
        sql: &mut impl SqlWriter,
    ) -> Result<(), RenderError> {
        self.prepare_mutation_order_limit(orders, limit, sql)
    }

    #[doc(hidden)]
    /// Write the RETURNING clause of an UPDATE, which joins the `from` tables
    fn prepare_update_returning(
        &self,
        _from: &[TableRef],
        returning: &Option<ReturningClause>,
        sql: &mut impl SqlWriter,
    ) -> Result<(), RenderError> {
        self.prepare_returning(returning, sql)
    }

    #[doc(hidden)]
    /// Write the `FILTER (WHERE ..)` of an aggregate call
    fn prepare_function_filter(
        &self,
        filter: &Condition,
        sql: &mut impl SqlWriter,
    ) -> Result<(), RenderError> {
        functions::prepare_function_filter(self, filter, sql)
    }

    #[doc(hidden)]
    /// Write the RETURNING clause of an INSERT, UPDATE or DELETE
    fn prepare_returning(
        &self,
        returning: &Option<ReturningClause>,
        sql: &mut impl SqlWriter,
    ) -> Result<(), RenderError> {
        conflict::prepare_returning(self, returning, sql)
    }

    fn write_string_quoted(&self, string: &str, buffer: &mut impl Write) {
        constants::write_string_quoted(self, string, buffer);
    }

    #[doc(hidden)]
    /// Write bytes enclosed with engine specific byte syntax
    fn write_bytes(&self, bytes: &[u8], buffer: &mut impl Write) {
        constants::write_bytes(self, bytes, buffer);
    }

    #[doc(hidden)]
    /// The name of the function that represents the "if null" condition.
    fn if_null_function(&self) -> &str {
        constants::if_null_function(self)
    }

    #[doc(hidden)]
    /// The name of the function that represents the "greatest" function.
    fn greatest_function(&self) -> &str {
        constants::greatest_function(self)
    }

    #[doc(hidden)]
    /// The name of the function that represents the "least" function.
    fn least_function(&self) -> &str {
        constants::least_function(self)
    }

    #[doc(hidden)]
    /// The name of the function that returns the char length.
    fn char_length_function(&self) -> &str {
        constants::char_length_function(self)
    }

    #[doc(hidden)]
    /// The name of the function that returns a random number
    fn random_function(&self) -> &str {
        constants::random_function(self)
    }

    #[doc(hidden)]
    /// The name of the function that returns the lock phrase including the leading 'FOR'
    fn lock_phrase(&self, lock_type: LockType) -> &'static str {
        constants::lock_phrase(self, lock_type)
    }

    /// The keywords for insert default row.
    fn insert_default_keyword(&self) -> &str {
        constants::insert_default_keyword(self)
    }

    /// Write insert default rows expression.
    fn insert_default_values(
        &self,
        num_rows: usize,
        sql: &mut impl SqlWriter,
    ) -> Result<(), RenderError> {
        constants::insert_default_values(self, num_rows, sql)
    }
}

#[cfg(test)]
#[path = "../../tests/unit/query_builder_tests.rs"]
mod tests;