From 337e7231a12cdbd1b8fae0ff0b5b8de25acee3ab Mon Sep 17 00:00:00 2001 From: Stephen Becker IV Date: Tue, 14 Jun 2022 04:10:16 +0000 Subject: [PATCH 1/5] 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 --- sqlx-core/src/query_builder.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/sqlx-core/src/query_builder.rs b/sqlx-core/src/query_builder.rs index 761903f0e8..0c9dccd7cd 100644 --- a/sqlx-core/src/query_builder.rs +++ b/sqlx-core/src/query_builder.rs @@ -139,6 +139,25 @@ 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 + /// use sqlx::{Execute, MySql, QueryBuilder}; + /// let foods = vec!["pizza".to_string(), "chips".to_string()]; + /// let mut query_builder: QueryBuilder = 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() { + /// 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, From dc9a5ac0a73738e6f86e222732d2539c33289c3f Mon Sep 17 00:00:00 2001 From: Stephen Becker IV Date: Thu, 14 Jul 2022 21:27:32 +0000 Subject: [PATCH 2/5] end ``` --- sqlx-core/src/query_builder.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sqlx-core/src/query_builder.rs b/sqlx-core/src/query_builder.rs index 0c9dccd7cd..daea9877ed 100644 --- a/sqlx-core/src/query_builder.rs +++ b/sqlx-core/src/query_builder.rs @@ -157,6 +157,7 @@ where /// 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 @@ -268,6 +269,7 @@ where /// // 16383 * 4 = 65532 /// assert_eq!(arguments.len(), 65532); /// # } + /// ``` pub fn push_values(&mut self, tuples: I, mut push_tuple: F) -> &mut Self where I: IntoIterator, From 2aaf36e1fe64364a7730c2b0461bedb50943cab9 Mon Sep 17 00:00:00 2001 From: Stephen Becker IV Date: Thu, 14 Jul 2022 21:28:14 +0000 Subject: [PATCH 3/5] Apply cfg and end ``` --- sqlx-core/src/query_builder.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/sqlx-core/src/query_builder.rs b/sqlx-core/src/query_builder.rs index daea9877ed..383c0de731 100644 --- a/sqlx-core/src/query_builder.rs +++ b/sqlx-core/src/query_builder.rs @@ -141,6 +141,7 @@ where /// 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 = QueryBuilder::new( @@ -157,6 +158,8 @@ where /// 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> From 96c530540ccff89f249c1241253312ae1ed43434 Mon Sep 17 00:00:00 2001 From: Stephen Becker IV Date: Thu, 14 Jul 2022 21:42:33 +0000 Subject: [PATCH 4/5] remove dup --- sqlx-core/src/query_builder.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/sqlx-core/src/query_builder.rs b/sqlx-core/src/query_builder.rs index 383c0de731..3ec219594e 100644 --- a/sqlx-core/src/query_builder.rs +++ b/sqlx-core/src/query_builder.rs @@ -160,7 +160,6 @@ where /// assert!(sql.ends_with("in (?, ?) ")); /// # } /// ``` - /// ``` pub fn separated<'qb, Sep>(&'qb mut self, separator: Sep) -> Separated<'qb, 'args, DB, Sep> where From 66267caf9ae27c157e470fac7af3d1405df81684 Mon Sep 17 00:00:00 2001 From: Stephen Becker IV Date: Fri, 15 Jul 2022 16:41:46 +0000 Subject: [PATCH 5/5] Update sqlx-core/src/query_builder.rs --- sqlx-core/src/query_builder.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sqlx-core/src/query_builder.rs b/sqlx-core/src/query_builder.rs index 3ec219594e..69f7b7f927 100644 --- a/sqlx-core/src/query_builder.rs +++ b/sqlx-core/src/query_builder.rs @@ -150,7 +150,7 @@ where /// // 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() { + /// for value_type in foods.iter() { /// separated.push_bind(value_type); /// } /// separated.push_unseparated(") ");