Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add in an example of how to use separated #1902

Merged
merged 5 commits into from
Jul 15, 2022
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions sqlx-core/src/query_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
sbeckeriv marked this conversation as resolved.
Show resolved Hide resolved
/// # #[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 filtered_types.iter() {
sbeckeriv marked this conversation as resolved.
Show resolved Hide resolved
sbeckeriv marked this conversation as resolved.
Show resolved Hide resolved
/// separated.push_bind(value_type);
/// }
/// separated.push_unseparated(") ");
///
/// let mut query = query_builder.build();
/// let sql = query.sql();
/// assert!(sql.ends_with("in (?, ?) "));
sbeckeriv marked this conversation as resolved.
Show resolved Hide resolved
/// # }
/// ```

sbeckeriv marked this conversation as resolved.
Show resolved Hide resolved
pub fn separated<'qb, Sep>(&'qb mut self, separator: Sep) -> Separated<'qb, 'args, DB, Sep>
where
'args: 'qb,
Expand Down Expand Up @@ -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,
Expand Down