sqlx_core_oldapi/
query_builder.rs

1//! Runtime query-builder API.
2
3use std::fmt::Display;
4use std::fmt::Write;
5use std::marker::PhantomData;
6
7use crate::arguments::Arguments;
8use crate::database::{Database, HasArguments};
9use crate::encode::Encode;
10use crate::from_row::FromRow;
11use crate::query::Query;
12use crate::query_as::QueryAs;
13use crate::types::Type;
14use crate::Either;
15
16/// A builder type for constructing queries at runtime.
17///
18/// See [`.push_values()`][Self::push_values] for an example of building a bulk `INSERT` statement.
19/// Note, however, that with Postgres you can get much better performance by using arrays
20/// and `UNNEST()`. [See our FAQ] for details.
21///
22/// [See our FAQ]: https://github.com/launchbadge/sqlx/blob/master/FAQ.md#how-can-i-bind-an-array-to-a-values-clause-how-can-i-do-bulk-inserts
23pub struct QueryBuilder<'args, DB>
24where
25    DB: Database,
26{
27    query: String,
28    init_len: usize,
29    arguments: Option<<DB as HasArguments<'args>>::Arguments>,
30}
31
32impl<'args, DB: Database> QueryBuilder<'args, DB>
33where
34    DB: Database,
35{
36    // `init` is provided because a query will almost always start with a constant fragment
37    // such as `INSERT INTO ...` or `SELECT ...`, etc.
38    /// Start building a query with an initial SQL fragment, which may be an empty string.
39    pub fn new(init: impl Into<String>) -> Self
40    where
41        <DB as HasArguments<'args>>::Arguments: Default,
42    {
43        let init = init.into();
44
45        QueryBuilder {
46            init_len: init.len(),
47            query: init,
48            arguments: Some(Default::default()),
49        }
50    }
51
52    #[inline]
53    fn sanity_check(&self) {
54        assert!(
55            self.arguments.is_some(),
56            "QueryBuilder must be reset before reuse after `.build()`"
57        );
58    }
59
60    /// Append a SQL fragment to the query.
61    ///
62    /// May be a string or anything that implements `Display`.
63    /// You can also use `format_args!()` here to push a formatted string without an intermediate
64    /// allocation.
65    ///
66    /// ### Warning: Beware SQL Injection Vulnerabilities and Untrusted Input!
67    /// You should *not* use this to insert input directly into the query from an untrusted user as
68    /// this can be used by an attacker to extract sensitive data or take over your database.
69    ///
70    /// Security breaches due to SQL injection can cost your organization a lot of money from
71    /// damage control and lost clients, betray the trust of your users in your system, and are just
72    /// plain embarrassing. If you are unfamiliar with the threat that SQL injection imposes, you
73    /// should take some time to learn more about it before proceeding:
74    ///
75    /// * [SQL Injection on OWASP.org](https://owasp.org/www-community/attacks/SQL_Injection)
76    /// * [SQL Injection on Wikipedia](https://en.wikipedia.org/wiki/SQL_injection)
77    ///     * See "Examples" for notable instances of security breaches due to SQL injection.
78    ///
79    /// This method does *not* perform sanitization. Instead, you should use
80    /// [`.push_bind()`][Self::push_bind] which inserts a placeholder into the query and then
81    /// sends the possibly untrustworthy value separately (called a "bind argument") so that it
82    /// cannot be misinterpreted by the database server.
83    ///
84    /// Note that you should still at least have some sort of sanity checks on the values you're
85    /// sending as that's just good practice and prevent other types of attacks against your system,
86    /// e.g. check that strings aren't too long, numbers are within expected ranges, etc.
87    pub fn push(&mut self, sql: impl Display) -> &mut Self {
88        self.sanity_check();
89
90        write!(self.query, "{}", sql).expect("error formatting `sql`");
91
92        self
93    }
94
95    /// Push a bind argument placeholder (`?` or `$N` for Postgres) and bind a value to it.
96    ///
97    /// ### Note: Database-specific Limits
98    /// Note that every database has a practical limit on the number of bind parameters
99    /// you can add to a single query. This varies by database.
100    ///
101    /// While you should consult the manual of your specific database version and/or current
102    /// configuration for the exact value as it may be different than listed here,
103    /// the defaults for supported databases as of writing are as follows:
104    ///
105    /// * Postgres and MySQL: 65535
106    ///     * You may find sources that state that Postgres has a limit of 32767,
107    ///       but that is a misinterpretation of the specification by the JDBC driver implementation
108    ///       as discussed in [this Github issue][postgres-limit-issue]. Postgres itself
109    ///       asserts that the number of parameters is in the range `[0, 65535)`.
110    /// * SQLite: 32766 (configurable by [`SQLITE_LIMIT_VARIABLE_NUMBER`])
111    ///     * SQLite prior to 3.32.0: 999
112    /// * MSSQL: 2100
113    ///
114    /// Exceeding these limits may panic (as a sanity check) or trigger a database error at runtime
115    /// depending on the implementation.
116    ///
117    /// [`SQLITE_LIMIT_VARIABLE_NUMBER`]: https://www.sqlite.org/limits.html#max_variable_number
118    /// [postgres-limit-issue]: https://github.com/launchbadge/sqlx/issues/671#issuecomment-687043510
119    pub fn push_bind<T>(&mut self, value: T) -> &mut Self
120    where
121        T: 'args + Encode<'args, DB> + Send + Type<DB>,
122    {
123        self.sanity_check();
124
125        let arguments = self
126            .arguments
127            .as_mut()
128            .expect("BUG: Arguments taken already");
129        arguments.add(value);
130
131        arguments
132            .format_placeholder(&mut self.query)
133            .expect("error in format_placeholder");
134
135        self
136    }
137
138    /// Start a list separated by `separator`.
139    ///
140    /// The returned type exposes identical [`.push()`][Separated::push] and
141    /// [`.push_bind()`][Separated::push_bind] methods which push `separator` to the query
142    /// before their normal behavior. [`.push_unseparated()`][Separated::push_unseparated] and [`.push_bind_unseparated()`][Separated::push_bind_unseparated] are also
143    /// provided to push a SQL fragment without the separator.
144    ///
145    /// ```rust
146    /// # #[cfg(feature = "mysql")] {
147    /// use sqlx::{Execute, MySql, QueryBuilder};
148    /// let foods = vec!["pizza".to_string(), "chips".to_string()];
149    /// let mut query_builder: QueryBuilder<MySql> = QueryBuilder::new(
150    ///     "SELECT * from food where name in ("
151    /// );
152    /// // One element vector is handled correctly but an empty vector
153    /// // would cause a sql syntax error
154    /// let mut separated = query_builder.separated(", ");
155    /// for value_type in foods.iter() {
156    ///   separated.push_bind(value_type);
157    /// }
158    /// separated.push_unseparated(") ");
159    ///
160    /// let mut query = query_builder.build();
161    /// let sql = query.sql();
162    /// assert!(sql.ends_with("in (?, ?) "));
163    /// # }
164    /// ```
165
166    pub fn separated<'qb, Sep>(&'qb mut self, separator: Sep) -> Separated<'qb, 'args, DB, Sep>
167    where
168        'args: 'qb,
169        Sep: Display,
170    {
171        self.sanity_check();
172
173        Separated {
174            query_builder: self,
175            separator,
176            push_separator: false,
177        }
178    }
179
180    // Most of the `QueryBuilder` API is purposefully very low-level but this was a commonly
181    // requested use-case so it made sense to support.
182    /// Push a `VALUES` clause where each item in `tuples` represents a tuple/row in the clause.
183    ///
184    /// This can be used to construct a bulk `INSERT` statement, although keep in mind that all
185    /// databases have some practical limit on the number of bind arguments in a single query.
186    /// See [`.push_bind()`][Self::push_bind] for details.
187    ///
188    /// To be safe, you can do `tuples.into_iter().take(N)` where `N` is the limit for your database
189    /// divided by the number of fields in each tuple; since integer division always rounds down,
190    /// this will ensure that you don't exceed the limit.
191    ///
192    /// ### Notes
193    ///
194    /// If `tuples` is empty, this will likely produce a syntactically invalid query as `VALUES`
195    /// generally expects to be followed by at least 1 tuple.
196    ///
197    /// If `tuples` can have many different lengths, you may want to call
198    /// [`.persistent(false)`][Query::persistent] after [`.build()`][Self::build] to avoid
199    /// filling up the connection's prepared statement cache.
200    ///
201    /// Because the `Arguments` API has a lifetime that must live longer than `Self`, you cannot
202    /// bind by-reference from an iterator unless that iterator yields references that live
203    /// longer than `Self`, even if the specific `Arguments` implementation doesn't actually
204    /// borrow the values (like `MySqlArguments` and `PgArguments` immediately encode the arguments
205    /// and don't borrow them past the `.add()` call).
206    ///
207    /// So basically, if you want to bind by-reference you need an iterator that yields references,
208    /// e.g. if you have values in a `Vec` you can do `.iter()` instead of `.into_iter()`. The
209    /// example below uses an iterator that creates values on the fly
210    /// and so cannot bind by-reference.
211    ///
212    /// ### Example (MySQL)
213    ///
214    /// ```rust
215    /// # #[cfg(feature = "mysql")]
216    /// # {
217    /// use sqlx::{Execute, MySql, QueryBuilder};
218    ///
219    /// struct User {
220    ///     id: i32,
221    ///     username: String,
222    ///     email: String,
223    ///     password: String,
224    /// }
225    ///
226    /// // The number of parameters in MySQL must fit in a `u16`.
227    /// const BIND_LIMIT: usize = 65535;
228    ///
229    /// // This would normally produce values forever!
230    /// let users = (0..).map(|i| User {
231    ///     id: i,
232    ///     username: format!("test_user_{}", i),
233    ///     email: format!("test-user-{}@example.com", i),
234    ///     password: format!("Test!User@Password#{}", i),
235    /// });
236    ///
237    /// let mut query_builder: QueryBuilder<MySql> = QueryBuilder::new(
238    ///     // Note the trailing space; most calls to `QueryBuilder` don't automatically insert
239    ///     // spaces as that might interfere with identifiers or quoted strings where exact
240    ///     // values may matter.
241    ///     "INSERT INTO users(id, username, email, password) "
242    /// );
243    ///
244    /// // Note that `.into_iter()` wasn't needed here since `users` is already an iterator.
245    /// query_builder.push_values(users.take(BIND_LIMIT / 4), |mut b, user| {
246    ///     // If you wanted to bind these by-reference instead of by-value,
247    ///     // you'd need an iterator that yields references that live as long as `query_builder`,
248    ///     // e.g. collect it to a `Vec` first.
249    ///     b.push_bind(user.id)
250    ///         .push_bind(user.username)
251    ///         .push_bind(user.email)
252    ///         .push_bind(user.password);
253    /// });
254    ///
255    /// let mut query = query_builder.build();
256    ///
257    /// // You can then call `query.execute()`, `.fetch_one()`, `.fetch_all()`, etc.
258    /// // For the sake of demonstration though, we're just going to assert the contents
259    /// // of the query.
260    ///
261    /// // These are methods of the `Execute` trait, not normally meant to be called in user code.
262    /// let sql = query.sql();
263    /// let arguments = query.take_arguments().unwrap();
264    ///
265    /// assert!(sql.starts_with(
266    ///     "INSERT INTO users(id, username, email, password) VALUES (?, ?, ?, ?), (?, ?, ?, ?)"
267    /// ));
268    ///
269    /// assert!(sql.ends_with("(?, ?, ?, ?)"));
270    ///
271    /// // Not a normally exposed function, only used for this doctest.
272    /// // 65535 / 4 = 16383 (rounded down)
273    /// // 16383 * 4 = 65532
274    /// assert_eq!(arguments.len(), 65532);
275    /// # }
276    /// ```
277    pub fn push_values<I, F>(&mut self, tuples: I, mut push_tuple: F) -> &mut Self
278    where
279        I: IntoIterator,
280        F: FnMut(Separated<'_, 'args, DB, &'static str>, I::Item),
281    {
282        self.sanity_check();
283
284        self.push("VALUES ");
285
286        let mut separated = self.separated(", ");
287
288        for tuple in tuples {
289            separated.push("(");
290
291            // use a `Separated` with a separate (hah) internal state
292            push_tuple(separated.query_builder.separated(", "), tuple);
293
294            separated.push_unseparated(")");
295        }
296
297        separated.query_builder
298    }
299
300    /// Creates `((a, b), (..)` statements, from `tuples`.
301    ///
302    /// This can be used to construct a bulk `SELECT` statement like this:
303    /// ```sql
304    /// SELECT * FROM users WHERE (id, username) IN ((1, "test_user_1"), (2, "test_user_2"))
305    /// ```
306    ///
307    /// Although keep in mind that all
308    /// databases have some practical limit on the number of bind arguments in a single query.
309    /// See [`.push_bind()`][Self::push_bind] for details.
310    ///
311    /// To be safe, you can do `tuples.into_iter().take(N)` where `N` is the limit for your database
312    /// divided by the number of fields in each tuple; since integer division always rounds down,
313    /// this will ensure that you don't exceed the limit.
314    ///
315    /// ### Notes
316    ///
317    /// If `tuples` is empty, this will likely produce a syntactically invalid query
318    ///
319    /// ### Example (MySQL)
320    ///
321    /// ```rust
322    /// # #[cfg(feature = "mysql")]
323    /// # {
324    /// use sqlx::{Execute, MySql, QueryBuilder};
325    ///
326    /// struct User {
327    ///     id: i32,
328    ///     username: String,
329    ///     email: String,
330    ///     password: String,
331    /// }
332    ///
333    /// // The number of parameters in MySQL must fit in a `u16`.
334    /// const BIND_LIMIT: usize = 65535;
335    ///
336    /// // This would normally produce values forever!
337    /// let users = (0..).map(|i| User {
338    ///     id: i,
339    ///     username: format!("test_user_{}", i),
340    ///     email: format!("test-user-{}@example.com", i),
341    ///     password: format!("Test!User@Password#{}", i),
342    /// });
343    ///
344    /// let mut query_builder: QueryBuilder<MySql> = QueryBuilder::new(
345    ///     // Note the trailing space; most calls to `QueryBuilder` don't automatically insert
346    ///     // spaces as that might interfere with identifiers or quoted strings where exact
347    ///     // values may matter.
348    ///     "SELECT * FROM users WHERE (id, username, email, password) in"
349    /// );
350    ///
351    /// // Note that `.into_iter()` wasn't needed here since `users` is already an iterator.
352    /// query_builder.push_tuples(users.take(BIND_LIMIT / 4), |mut b, user| {
353    ///     // If you wanted to bind these by-reference instead of by-value,
354    ///     // you'd need an iterator that yields references that live as long as `query_builder`,
355    ///     // e.g. collect it to a `Vec` first.
356    ///     b.push_bind(user.id)
357    ///         .push_bind(user.username)
358    ///         .push_bind(user.email)
359    ///         .push_bind(user.password);
360    /// });
361    ///
362    /// let mut query = query_builder.build();
363    ///
364    /// // You can then call `query.execute()`, `.fetch_one()`, `.fetch_all()`, etc.
365    /// // For the sake of demonstration though, we're just going to assert the contents
366    /// // of the query.
367    ///
368    /// // These are methods of the `Execute` trait, not normally meant to be called in user code.
369    /// let sql = query.sql();
370    /// let arguments = query.take_arguments().unwrap();
371    ///
372    /// assert!(sql.starts_with(
373    ///     "SELECT * FROM users WHERE (id, username, email, password) in ((?, ?, ?, ?), (?, ?, ?, ?), "
374    /// ));
375    ///
376    /// assert!(sql.ends_with("(?, ?, ?, ?)) "));
377    ///
378    /// // Not a normally exposed function, only used for this doctest.
379    /// // 65535 / 4 = 16383 (rounded down)
380    /// // 16383 * 4 = 65532
381    /// assert_eq!(arguments.len(), 65532);
382    /// }
383    /// ```
384    pub fn push_tuples<I, F>(&mut self, tuples: I, mut push_tuple: F) -> &mut Self
385    where
386        I: IntoIterator,
387        F: FnMut(Separated<'_, 'args, DB, &'static str>, I::Item),
388    {
389        self.sanity_check();
390
391        self.push(" (");
392
393        let mut separated = self.separated(", ");
394
395        for tuple in tuples {
396            separated.push("(");
397
398            push_tuple(separated.query_builder.separated(", "), tuple);
399
400            separated.push_unseparated(")");
401        }
402        separated.push_unseparated(") ");
403
404        separated.query_builder
405    }
406
407    /// Produce an executable query from this builder.
408    ///
409    /// ### Note: Query is not Checked
410    /// It is your responsibility to ensure that you produce a syntactically correct query here,
411    /// this API has no way to check it for you.
412    ///
413    /// ### Note: Reuse
414    /// You can reuse this builder afterwards to amortize the allocation overhead of the query
415    /// string, however you must call [`.reset()`][Self::reset] first, which returns `Self`
416    /// to the state it was in immediately after [`new()`][Self::new].
417    ///
418    /// Calling any other method but `.reset()` after `.build()` will panic for sanity reasons.
419    pub fn build(&mut self) -> Query<'_, DB, <DB as HasArguments<'args>>::Arguments> {
420        self.sanity_check();
421
422        Query {
423            statement: Either::Left(&self.query),
424            arguments: self.arguments.take(),
425            database: PhantomData,
426            persistent: true,
427        }
428    }
429
430    /// Produce an executable query from this builder.
431    ///
432    /// ### Note: Query is not Checked
433    /// It is your responsibility to ensure that you produce a syntactically correct query here,
434    /// this API has no way to check it for you.
435    ///
436    /// ### Note: Reuse
437    /// You can reuse this builder afterwards to amortize the allocation overhead of the query
438    /// string, however you must call [`.reset()`][Self::reset] first, which returns `Self`
439    /// to the state it was in immediately after [`new()`][Self::new].
440    ///
441    /// Calling any other method but `.reset()` after `.build()` will panic for sanity reasons.
442    pub fn build_query_as<'q, T: FromRow<'q, DB::Row>>(
443        &'q mut self,
444    ) -> QueryAs<'q, DB, T, <DB as HasArguments<'args>>::Arguments> {
445        QueryAs {
446            inner: self.build(),
447            output: PhantomData,
448        }
449    }
450
451    /// Reset this `QueryBuilder` back to its initial state.
452    ///
453    /// The query is truncated to the initial fragment provided to [`new()`][Self::new] and
454    /// the bind arguments are reset.
455    pub fn reset(&mut self) -> &mut Self {
456        self.query.truncate(self.init_len);
457        self.arguments = Some(Default::default());
458
459        self
460    }
461
462    /// Get the current build SQL; **note**: may not be syntactically correct.
463    pub fn sql(&self) -> &str {
464        &self.query
465    }
466
467    /// Deconstruct this `QueryBuilder`, returning the built SQL. May not be syntactically correct.
468    pub fn into_sql(self) -> String {
469        self.query
470    }
471}
472
473/// A wrapper around `QueryBuilder` for creating comma(or other token)-separated lists.
474///
475/// See [`QueryBuilder::separated()`] for details.
476#[allow(explicit_outlives_requirements)]
477pub struct Separated<'qb, 'args: 'qb, DB, Sep>
478where
479    DB: Database,
480{
481    query_builder: &'qb mut QueryBuilder<'args, DB>,
482    separator: Sep,
483    push_separator: bool,
484}
485
486impl<'qb, 'args: 'qb, DB, Sep> Separated<'qb, 'args, DB, Sep>
487where
488    DB: Database,
489    Sep: Display,
490{
491    /// Push the separator if applicable, and then the given SQL fragment.
492    ///
493    /// See [`QueryBuilder::push()`] for details.
494    pub fn push(&mut self, sql: impl Display) -> &mut Self {
495        if self.push_separator {
496            self.query_builder
497                .push(format_args!("{}{}", self.separator, sql));
498        } else {
499            self.query_builder.push(sql);
500            self.push_separator = true;
501        }
502
503        self
504    }
505
506    /// Push a SQL fragment without a separator.
507    ///
508    /// Simply calls [`QueryBuilder::push()`] directly.
509    pub fn push_unseparated(&mut self, sql: impl Display) -> &mut Self {
510        self.query_builder.push(sql);
511        self
512    }
513
514    /// Push the separator if applicable, then append a bind argument.
515    ///
516    /// See [`QueryBuilder::push_bind()`] for details.
517    pub fn push_bind<T>(&mut self, value: T) -> &mut Self
518    where
519        T: 'args + Encode<'args, DB> + Send + Type<DB>,
520    {
521        if self.push_separator {
522            self.query_builder.push(&self.separator);
523        }
524
525        self.query_builder.push_bind(value);
526        self.push_separator = true;
527
528        self
529    }
530
531    /// Push a bind argument placeholder (`?` or `$N` for Postgres) and bind a value to it
532    /// without a separator.
533    ///
534    /// Simply calls [`QueryBuilder::push_bind()`] directly.
535    pub fn push_bind_unseparated<T>(&mut self, value: T) -> &mut Self
536    where
537        T: 'args + Encode<'args, DB> + Send + Type<DB>,
538    {
539        self.query_builder.push_bind(value);
540        self
541    }
542}
543
544#[cfg(test)]
545mod test {
546    use crate::postgres::Postgres;
547
548    use super::*;
549
550    #[test]
551    fn test_new() {
552        let qb: QueryBuilder<'_, Postgres> = QueryBuilder::new("SELECT * FROM users");
553        assert_eq!(qb.query, "SELECT * FROM users");
554    }
555
556    #[test]
557    fn test_push() {
558        let mut qb: QueryBuilder<'_, Postgres> = QueryBuilder::new("SELECT * FROM users");
559        let second_line = " WHERE last_name LIKE '[A-N]%;";
560        qb.push(second_line);
561
562        assert_eq!(
563            qb.query,
564            "SELECT * FROM users WHERE last_name LIKE '[A-N]%;".to_string(),
565        );
566    }
567
568    #[test]
569    #[should_panic]
570    fn test_push_panics_when_no_arguments() {
571        let mut qb: QueryBuilder<'_, Postgres> = QueryBuilder::new("SELECT * FROM users;");
572        qb.arguments = None;
573
574        qb.push("SELECT * FROM users;");
575    }
576
577    #[test]
578    fn test_push_bind() {
579        let mut qb: QueryBuilder<'_, Postgres> =
580            QueryBuilder::new("SELECT * FROM users WHERE id = ");
581
582        qb.push_bind(42i32)
583            .push(" OR membership_level = ")
584            .push_bind(3i32);
585
586        assert_eq!(
587            qb.query,
588            "SELECT * FROM users WHERE id = $1 OR membership_level = $2"
589        );
590    }
591
592    #[test]
593    fn test_build() {
594        let mut qb: QueryBuilder<'_, Postgres> = QueryBuilder::new("SELECT * FROM users");
595
596        qb.push(" WHERE id = ").push_bind(42i32);
597        let query = qb.build();
598
599        assert_eq!(
600            query.statement.unwrap_left(),
601            "SELECT * FROM users WHERE id = $1"
602        );
603        assert_eq!(query.persistent, true);
604    }
605
606    #[test]
607    fn test_reset() {
608        let mut qb: QueryBuilder<'_, Postgres> = QueryBuilder::new("");
609
610        let _query = qb
611            .push("SELECT * FROM users WHERE id = ")
612            .push_bind(42i32)
613            .build();
614
615        qb.reset();
616
617        assert_eq!(qb.query, "");
618    }
619
620    #[test]
621    fn test_query_builder_reuse() {
622        let mut qb: QueryBuilder<'_, Postgres> = QueryBuilder::new("");
623
624        let _query = qb
625            .push("SELECT * FROM users WHERE id = ")
626            .push_bind(42i32)
627            .build();
628
629        qb.reset();
630
631        let query = qb.push("SELECT * FROM users WHERE id = 99").build();
632
633        assert_eq!(
634            query.statement.unwrap_left(),
635            "SELECT * FROM users WHERE id = 99"
636        );
637    }
638}