mirror of
https://github.com/launchbadge/sqlx.git
synced 2025-10-02 15:25:32 +00:00
Add in an example of how to use separated (#1902)
* Add in an example of how to use separated Dearest Maintainer, Thank you for your work on this project. I started using query builder today and I have enjoyed it. I did have a hard time figuring out how best to use separated to generate the values for an IN statement. It is my hope that adding an example will save someone else a few minutes of code reading or compile time. I wrote the example in the github text editor but It looks correct. Thanks again for your work on this. Becker * end ``` * Apply cfg and end ``` * remove dup * Update sqlx-core/src/query_builder.rs
This commit is contained in:
parent
28e22e1259
commit
dbb1feebda
@ -139,6 +139,28 @@ where
|
||||
/// [`.push_bind()`][Separated::push_bind] methods which push `separator` to the query
|
||||
/// before their normal behavior. [`.push_unseparated()`][Separated::push_unseparated] is also
|
||||
/// provided to push a SQL fragment without the separator.
|
||||
///
|
||||
/// ```rust
|
||||
/// # #[cfg(feature = "mysql")] {
|
||||
/// use sqlx::{Execute, MySql, QueryBuilder};
|
||||
/// let foods = vec!["pizza".to_string(), "chips".to_string()];
|
||||
/// let mut query_builder: QueryBuilder<MySql> = QueryBuilder::new(
|
||||
/// "SELECT * from food where name in ("
|
||||
/// );
|
||||
/// // One element vector is handled correctly but an empty vector
|
||||
/// // would cause a sql syntax error
|
||||
/// let mut separated = query_builder.separated(", ");
|
||||
/// for value_type in foods.iter() {
|
||||
/// separated.push_bind(value_type);
|
||||
/// }
|
||||
/// separated.push_unseparated(") ");
|
||||
///
|
||||
/// let mut query = query_builder.build();
|
||||
/// let sql = query.sql();
|
||||
/// assert!(sql.ends_with("in (?, ?) "));
|
||||
/// # }
|
||||
/// ```
|
||||
|
||||
pub fn separated<'qb, Sep>(&'qb mut self, separator: Sep) -> Separated<'qb, 'args, DB, Sep>
|
||||
where
|
||||
'args: 'qb,
|
||||
@ -249,6 +271,7 @@ where
|
||||
/// // 16383 * 4 = 65532
|
||||
/// assert_eq!(arguments.len(), 65532);
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn push_values<I, F>(&mut self, tuples: I, mut push_tuple: F) -> &mut Self
|
||||
where
|
||||
I: IntoIterator,
|
||||
|
Loading…
x
Reference in New Issue
Block a user