Skip to content

Commit 7dd1dfa

Browse files
committed
fix: solved the update entity bug
1 parent 463eab8 commit 7dd1dfa

4 files changed

Lines changed: 46 additions & 18 deletions

File tree

canyon_macros/src/canyon_mapper_macro.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ mod __details {
288288
let set_pk_val_method = if let Some(pk_ident) = pk_ident_ts {
289289
quote! {
290290
self.#pk_ident = value.into();
291-
Ok(())
291+
Ok(())
292292
}
293293
} else {
294294
quote! {

canyon_macros/src/query_operations/update.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,20 +192,26 @@ mod __details {
192192

193193
fn generate_update_entity_pk_body_logic(table_schema_data: &str) -> TokenStream {
194194
quote! {
195-
let pk_actual_value = &entity.primary_key_actual_value();
195+
let pk_actual_value = entity.primary_key_actual_value();
196196
let update_columns = entity.fields_names();
197-
let update_values = entity.fields_actual_values();
197+
let update_values_pk_parsed = entity.fields_actual_values();
198198

199199
let mut vec_columns_values: Vec<String> = Vec::new();
200200
for (i, column_name) in update_columns.to_vec().iter().enumerate() {
201201
let column_equal_value = format!("{} = ${}", column_name, i + 2);
202202
vec_columns_values.push(column_equal_value)
203203
}
204-
let str_columns_values = vec_columns_values.join(", ");
204+
let col_vals_placeholders = vec_columns_values.join(", ");
205+
206+
// Efficiently build argument list: pk first, then values
207+
let mut update_values: Vec<&dyn canyon_sql::query::QueryParameter> =
208+
Vec::with_capacity(1 + update_values_pk_parsed.len());
209+
update_values.push(pk_actual_value);
210+
update_values.extend(update_values_pk_parsed);
205211

206212
let stmt = format!(
207-
"UPDATE {} SET {} WHERE {} = ${:?}",
208-
#table_schema_data, str_columns_values, primary_key, pk_actual_value
213+
"UPDATE {} SET {} WHERE {:?} = $1",
214+
#table_schema_data, col_vals_placeholders, primary_key
209215
);
210216
}
211217
}

tests/crud/hex_arch_example.rs

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,42 @@ fn test_hex_arch_ops() {
3333
.unwrap() as usize,
3434
find_all_result.len()
3535
);
36-
// assert_eq!(LeagueHexRepositoryAdapter::<DatabaseConnection>::count_with(binding.deref_mut()).await.unwrap() as usize, find_all_result.len());
37-
// The line above works, because we're using binding, but in a better ideal world, our repository would hold an Arc<Mutex<...>> with the connection,
38-
// so the user acquire the lock on every query, just cloning the Arc, which if you remember, just increases in one unit the number of active
39-
// references pointing to the resource behind the atomic smart pointer
4036
}
4137

4238
#[cfg(feature = "postgres")]
4339
#[canyon_sql::macros::canyon_tokio_test]
44-
fn test_hex_arch_find_insert_ops() {
40+
fn test_hex_arch_insert_entity_ops() {
41+
let default_db_conn = Canyon::instance()
42+
.unwrap()
43+
.get_default_connection()
44+
.unwrap();
45+
let league_service = LeagueHexServiceAdapter {
46+
league_repository: LeagueHexRepositoryAdapter {
47+
db_conn: default_db_conn,
48+
},
49+
};
50+
51+
let mut other_league: LeagueHex = LeagueHex {
52+
id: Default::default(),
53+
ext_id: Default::default(),
54+
slug: "leaguehex-slug".to_string(),
55+
name: "Test LeagueHex on layered".to_string(),
56+
region: "LeagueHex Region".to_string(),
57+
image_url: "http://example.com/image.png".to_string(),
58+
};
59+
league_service.create(&mut other_league).await.unwrap();
60+
61+
let find_new_league = league_service.get(&other_league.id).await.unwrap();
62+
assert!(find_new_league.is_some());
63+
assert_eq!(
64+
find_new_league.as_ref().unwrap().name,
65+
String::from("Test LeagueHex on layered")
66+
);
67+
}
68+
69+
#[cfg(feature = "postgres")]
70+
#[canyon_sql::macros::canyon_tokio_test]
71+
fn test_hex_arch_update_entity_ops() {
4572
let default_db_conn = Canyon::instance()
4673
.unwrap()
4774
.get_default_connection()
@@ -61,7 +88,6 @@ fn test_hex_arch_find_insert_ops() {
6188
image_url: "http://example.com/image.png".to_string(),
6289
};
6390
league_service.create(&mut other_league).await.unwrap();
64-
println!("New league inserted with: {:#?}", other_league.id);
6591

6692
let find_new_league = league_service.get(&other_league.id).await.unwrap();
6793
assert!(find_new_league.is_some());
@@ -76,7 +102,7 @@ fn test_hex_arch_find_insert_ops() {
76102
assert!(r.is_ok());
77103

78104
let updated = league_service.get(&other_league.id).await.unwrap();
79-
assert_eq!(updated.unwrap().ext_id, 5)
105+
assert_eq!(updated.unwrap().ext_id, 5);
80106
}
81107

82108
#[derive(CanyonMapper, Debug)]
@@ -167,8 +193,6 @@ impl<T: DbConnection + Send + Sync> LeagueHexRepository for LeagueHexRepositoryA
167193
&self,
168194
id: &'a Pk,
169195
) -> Result<Option<LeagueHex>, Box<dyn Error + Send + Sync + 'a>> {
170-
let r = Self::find_by_pk(id).await;
171-
println!("FIND BY PK ON GET err: {:?}", r);
172-
r
196+
Self::find_by_pk(id).await
173197
}
174198
}

tests/crud/querybuilder_operations.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ use crate::constants::MYSQL_DS;
44
use crate::constants::SQL_SERVER_DS;
55
use canyon_sql::connection::DatabaseType;
66

7-
use canyon_sql::query::querybuilder::DeleteQueryBuilder;
8-
97
/// Tests for the QueryBuilder available operations within Canyon.
108
///
119
/// QueryBuilder are the way of obtain more flexibility that with

0 commit comments

Comments
 (0)