From e1cfca20b9978de3bc14602a16a46a6660eeee7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Norbert=20K=C3=A9ri?= Date: Wed, 14 May 2025 22:01:27 +0200 Subject: [PATCH 1/2] make the structs sync + send regardless of features enabled --- src/concat/non_standard.rs | 2 +- src/delete/delete.rs | 2 +- src/insert/insert.rs | 2 +- src/select/select.rs | 2 +- src/structure.rs | 8 ++++---- src/update/update.rs | 2 +- tests/clauses_sync_send.rs | 18 ++++++++++++++++++ 7 files changed, 27 insertions(+), 9 deletions(-) create mode 100644 tests/clauses_sync_send.rs diff --git a/src/concat/non_standard.rs b/src/concat/non_standard.rs index bf02340..828b58d 100644 --- a/src/concat/non_standard.rs +++ b/src/concat/non_standard.rs @@ -60,7 +60,7 @@ pub(crate) trait ConcatWith { query: String, fmts: &fmt::Formatter, clause: Clause, - items: &Vec<(String, std::sync::Arc)>, + items: &Vec<(String, std::sync::Arc)>, ) -> String { let fmt::Formatter { comma, diff --git a/src/delete/delete.rs b/src/delete/delete.rs index 5544797..7915e61 100644 --- a/src/delete/delete.rs +++ b/src/delete/delete.rs @@ -344,7 +344,7 @@ impl Delete { /// WHERE id in (select * from deactivated_users) /// ``` #[cfg(any(feature = "postgresql", feature = "sqlite"))] - pub fn with(mut self, name: &str, query: impl WithQuery + 'static) -> Self { + pub fn with(mut self, name: &str, query: impl WithQuery + 'static + Send + Sync) -> Self { self._with.push((name.trim().to_string(), std::sync::Arc::new(query))); self } diff --git a/src/insert/insert.rs b/src/insert/insert.rs index 6dccca6..e009f0c 100644 --- a/src/insert/insert.rs +++ b/src/insert/insert.rs @@ -408,7 +408,7 @@ impl Insert { /// FROM active_users /// ``` #[cfg(any(feature = "postgresql", feature = "sqlite"))] - pub fn with(mut self, name: &str, query: impl WithQuery + 'static) -> Self { + pub fn with(mut self, name: &str, query: impl WithQuery + 'static + Send + Sync) -> Self { self._with.push((name.trim().to_string(), std::sync::Arc::new(query))); self } diff --git a/src/select/select.rs b/src/select/select.rs index d2eba65..7127608 100644 --- a/src/select/select.rs +++ b/src/select/select.rs @@ -793,7 +793,7 @@ impl Select { /// -- ------------------------------------------------------------------------------ /// ``` #[cfg(any(feature = "postgresql", feature = "sqlite"))] - pub fn with(mut self, name: &str, query: impl WithQuery + 'static) -> Self { + pub fn with(mut self, name: &str, query: impl WithQuery + Send + Sync + 'static) -> Self { self._with.push((name.trim().to_string(), std::sync::Arc::new(query))); self } diff --git a/src/structure.rs b/src/structure.rs index 33db72e..2b0d0ce 100644 --- a/src/structure.rs +++ b/src/structure.rs @@ -324,7 +324,7 @@ pub struct Delete { pub(crate) _returning: Vec, #[cfg(any(feature = "postgresql", feature = "sqlite"))] - pub(crate) _with: Vec<(String, std::sync::Arc)>, + pub(crate) _with: Vec<(String, std::sync::Arc)>, } /// All available clauses to be used in [Delete::raw_before] and [Delete::raw_after] methods on [Delete] builder @@ -381,7 +381,7 @@ pub struct Insert { pub(crate) _returning: Vec, #[cfg(any(feature = "postgresql", feature = "sqlite"))] - pub(crate) _with: Vec<(String, std::sync::Arc)>, + pub(crate) _with: Vec<(String, std::sync::Arc)>, #[cfg(not(feature = "sqlite"))] pub(crate) _insert_into: String, @@ -508,7 +508,7 @@ pub struct Select { pub(crate) _union: Vec, #[cfg(any(feature = "postgresql", feature = "sqlite"))] - pub(crate) _with: Vec<(String, Arc)>, + pub(crate) _with: Vec<(String, Arc)>, } /// All available clauses to be used in [Select::raw_before] and [Select::raw_after] methods on [Select] builder @@ -671,7 +671,7 @@ pub struct Update { pub(crate) _returning: Vec, #[cfg(any(feature = "postgresql", feature = "sqlite"))] - pub(crate) _with: Vec<(String, std::sync::Arc)>, + pub(crate) _with: Vec<(String, std::sync::Arc)>, #[cfg(not(feature = "sqlite"))] pub(crate) _update: String, diff --git a/src/update/update.rs b/src/update/update.rs index 356e9ea..2fae6cf 100644 --- a/src/update/update.rs +++ b/src/update/update.rs @@ -418,7 +418,7 @@ impl Update { /// -- ------------------------------------------------------------------------------ /// ``` #[cfg(any(feature = "postgresql", feature = "sqlite"))] - pub fn with(mut self, name: &str, query: impl WithQuery + 'static) -> Self { + pub fn with(mut self, name: &str, query: impl WithQuery + 'static + Send + Sync) -> Self { self._with.push((name.trim().to_string(), std::sync::Arc::new(query))); self } diff --git a/tests/clauses_sync_send.rs b/tests/clauses_sync_send.rs new file mode 100644 index 0000000..f178042 --- /dev/null +++ b/tests/clauses_sync_send.rs @@ -0,0 +1,18 @@ +use sql_query_builder::{Delete, Insert, Select, Update}; + +/* If any of these tests fail to compile with a message similar to: +* +* `(dyn sql_query_builder::behavior::WithQuery + 'static)` cannot be sent between threads safely +* +* Then it means that an introduced change caused a query type to lose its Send/Sync marker. +*/ + +#[test] +fn queries_must_be_sync_send() { + must_be_sync_send(Select::new()); + must_be_sync_send(Delete::new()); + must_be_sync_send(Update::new()); + must_be_sync_send(Insert::new()); +} + +fn must_be_sync_send(_clause: impl Sync + Send) {} From 6b8cacea66fb7331e8f37f1e21c68a6882721941 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Norbert=20K=C3=A9ri?= Date: Fri, 16 May 2025 23:58:52 +0200 Subject: [PATCH 2/2] pr fixes --- tests/clauses_sync_send.rs | 18 ------------------ tests/command_delete_spec.rs | 9 +++++++++ tests/command_insert_spec.rs | 9 +++++++++ tests/command_select_spec.rs | 9 +++++++++ tests/command_update_spec.rs | 9 +++++++++ 5 files changed, 36 insertions(+), 18 deletions(-) delete mode 100644 tests/clauses_sync_send.rs diff --git a/tests/clauses_sync_send.rs b/tests/clauses_sync_send.rs deleted file mode 100644 index f178042..0000000 --- a/tests/clauses_sync_send.rs +++ /dev/null @@ -1,18 +0,0 @@ -use sql_query_builder::{Delete, Insert, Select, Update}; - -/* If any of these tests fail to compile with a message similar to: -* -* `(dyn sql_query_builder::behavior::WithQuery + 'static)` cannot be sent between threads safely -* -* Then it means that an introduced change caused a query type to lose its Send/Sync marker. -*/ - -#[test] -fn queries_must_be_sync_send() { - must_be_sync_send(Select::new()); - must_be_sync_send(Delete::new()); - must_be_sync_send(Update::new()); - must_be_sync_send(Insert::new()); -} - -fn must_be_sync_send(_clause: impl Sync + Send) {} diff --git a/tests/command_delete_spec.rs b/tests/command_delete_spec.rs index 768817b..802c968 100644 --- a/tests/command_delete_spec.rs +++ b/tests/command_delete_spec.rs @@ -124,6 +124,15 @@ mod builder_features { assert_eq!(query, expected_query); } + + /** This test can fail only at compile time + * [More context](https://github.com/belchior/sql_query_builder/pull/53) + */ + #[test] + fn select_builder_should_impl_send_and_sync() { + fn assert_impl_sync_send(_builder: impl Sync + Send) {} + assert_impl_sync_send(sql::Delete::new()); + } } mod builder_methods { diff --git a/tests/command_insert_spec.rs b/tests/command_insert_spec.rs index 7d8875f..639df28 100644 --- a/tests/command_insert_spec.rs +++ b/tests/command_insert_spec.rs @@ -124,6 +124,15 @@ mod builder_features { assert_eq!(query, expected_query); } + + /** This test can fail only at compile time + * [More context](https://github.com/belchior/sql_query_builder/pull/53) + */ + #[test] + fn select_builder_should_impl_send_and_sync() { + fn assert_impl_sync_send(_builder: impl Sync + Send) {} + assert_impl_sync_send(sql::Insert::new()); + } } mod builder_methods { diff --git a/tests/command_select_spec.rs b/tests/command_select_spec.rs index 85c79f5..3a2678d 100644 --- a/tests/command_select_spec.rs +++ b/tests/command_select_spec.rs @@ -144,6 +144,15 @@ mod builder_features { assert_eq!(query, expected_query); } + + /** This test can fail only at compile time + * [More context](https://github.com/belchior/sql_query_builder/pull/53) + */ + #[test] + fn select_builder_should_impl_send_and_sync() { + fn assert_impl_sync_send(_builder: impl Sync + Send) {} + assert_impl_sync_send(sql::Select::new()); + } } mod builder_methods { diff --git a/tests/command_update_spec.rs b/tests/command_update_spec.rs index 0832d44..1a3834b 100644 --- a/tests/command_update_spec.rs +++ b/tests/command_update_spec.rs @@ -133,6 +133,15 @@ mod builder_features { assert_eq!(query, expected_query); } + + /** This test can fail only at compile time + * [More context](https://github.com/belchior/sql_query_builder/pull/53) + */ + #[test] + fn select_builder_should_impl_send_and_sync() { + fn assert_impl_sync_send(_builder: impl Sync + Send) {} + assert_impl_sync_send(sql::Update::new()); + } } mod builder_methods {