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