From 73157d74c1aba8717ed65d2035cee65c4d3eaffa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BE=8D=E6=99=A8?= Date: Mon, 8 Aug 2022 15:31:36 +0800 Subject: [PATCH 1/6] feat(develop): Upgrade version 0.4.0 --- src/segment.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/segment.rs b/src/segment.rs index 82c46c2..3ab7647 100644 --- a/src/segment.rs +++ b/src/segment.rs @@ -261,7 +261,7 @@ impl ToSegment for str if self.is_empty() { return Segment::Nil } - Segment::Extenssion(format!("'{}'", self.replace(SINGLE_QUOTE, EMPTY))) + Segment::Extenssion(format!("{}", self.replace(SINGLE_QUOTE, EMPTY))) } } @@ -271,7 +271,7 @@ impl ToSegment for &str if self.is_empty() { return Segment::Nil } - Segment::Extenssion(format!("'{}'", self.replace(SINGLE_QUOTE, EMPTY))) + Segment::Extenssion(format!("{}", self.replace(SINGLE_QUOTE, EMPTY))) } } @@ -288,7 +288,7 @@ impl ToSegment for String if self.is_empty() { return Segment::Nil } - Segment::Extenssion(format!("'{}'", self.replace(SINGLE_QUOTE, EMPTY))) + Segment::Extenssion(format!("{}", self.replace(SINGLE_QUOTE, EMPTY))) } } From 686bb14e26ea720d9a40de13f0ffa9148b85a254 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BE=8D=E6=99=A8?= Date: Mon, 8 Aug 2022 16:15:57 +0800 Subject: [PATCH 2/6] feat(develop): Fixed the update condition --- src/manager.rs | 5 ++++- src/segment.rs | 4 ++-- src/wrapper.rs | 25 ++++++------------------- 3 files changed, 12 insertions(+), 22 deletions(-) diff --git a/src/manager.rs b/src/manager.rs index 3f4b640..bb4a7e4 100644 --- a/src/manager.rs +++ b/src/manager.rs @@ -308,10 +308,13 @@ pub fn build_update_clause(platform: &DatabasePlatform, _entity: &T, wrapper: { let table = T::table_name(); let columns = T::fields(); + let update_sql = wrapper.get_update_sql(&table.complete_name()).unwrap_or_default(); + if !update_sql.is_empty() { + return update_sql; + } let set_fields = &mut wrapper.fields_set; let mut sql = String::new(); sql += &format!("update {} ", table.complete_name()); - if set_fields.is_empty() { sql += &format!( "set {}", diff --git a/src/segment.rs b/src/segment.rs index 3ab7647..765c087 100644 --- a/src/segment.rs +++ b/src/segment.rs @@ -271,7 +271,7 @@ impl ToSegment for &str if self.is_empty() { return Segment::Nil } - Segment::Extenssion(format!("{}", self.replace(SINGLE_QUOTE, EMPTY))) + Segment::Extenssion(format!("'{}'", self.replace(SINGLE_QUOTE, EMPTY))) } } @@ -288,7 +288,7 @@ impl ToSegment for String if self.is_empty() { return Segment::Nil } - Segment::Extenssion(format!("{}", self.replace(SINGLE_QUOTE, EMPTY))) + Segment::Extenssion(format!("'{}'", self.replace(SINGLE_QUOTE, EMPTY))) } } diff --git a/src/wrapper.rs b/src/wrapper.rs index cd514b4..54a7c69 100644 --- a/src/wrapper.rs +++ b/src/wrapper.rs @@ -64,19 +64,6 @@ impl Wrapper{ self.set_condition(true, column, val) } - pub fn sets, U: ISegment>(self, column: S, val: U) -> Self { - self.set_conditions(true, column, val) - } - - pub fn set_conditions, U: ISegment>(mut self,condition: bool, column: S, mut val: U) -> Self { - if condition { - let col: String = column.into(); - self.sql_set.push(col.to_owned() + EQUALS + val.get_sql_segment().as_str()); - // self.fields_set.push((col.to_owned(), val.to_segment())); - } - self - } - pub fn set_condition, U: ToSegment>(mut self,condition: bool, column: S, val: U) -> Self { if condition { let col: String = column.into(); @@ -86,9 +73,9 @@ impl Wrapper{ self } - pub fn set_sql>(mut self, condition: bool, sql: S) -> Self { + pub fn set_sql>(mut self, sql: S) -> Self { let sql: String = sql.into(); - if condition && !sql.is_empty() { + if !sql.is_empty() { self.sql_set.push(sql); } self @@ -108,7 +95,7 @@ impl Wrapper{ self.sql_set.clear(); } - pub fn get_update_sql(&mut self, table_name: &'static str) -> Result { + pub fn get_update_sql(&mut self, table_name: &str) -> Result { let set_fields = if let Some(set) = self.get_set_sql() { set.to_owned() } else { @@ -127,7 +114,7 @@ impl Wrapper{ } } - pub fn get_query_sql(mut self, table_name: &'static str) -> Result { + pub fn get_query_sql(mut self, table_name: &str) -> Result { let select_fields = self.get_select_sql(); if table_name.is_empty() { Err("table name is empty!!!") @@ -255,7 +242,7 @@ impl Wrapper{ fn basic_test() { let s : Option = Some("ffffa".to_string()); let d: Option = None; - let mut wrapper = Wrapper::new().last("limit 1"); + let mut wrapper = Wrapper::new().set_sql("a='b'").eq("a", "bn").last("limit 1"); //.not_in("vecs", vec!["a","f","g"]); - println!("{}", wrapper.get_sql_segment()); + println!("{}", wrapper.get_set_sql().unwrap_or_default()); } \ No newline at end of file From e4ef817f1c8eba0c95e3e5747e5e9181523098f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BE=8D=E6=99=A8?= Date: Mon, 8 Aug 2022 18:18:35 +0800 Subject: [PATCH 3/6] feat(develop): Fixed the update condition --- src/akita.rs | 7 +++++-- src/manager.rs | 4 ---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/akita.rs b/src/akita.rs index f4e565e..ecc85cc 100644 --- a/src/akita.rs +++ b/src/akita.rs @@ -378,10 +378,13 @@ impl AkitaMapper for Akita { } let mut conn = self.acquire()?; let columns = T::fields(); - let sql = build_update_clause(&conn, entity, &mut wrapper); + let mut sql = build_update_clause(&conn, entity, &mut wrapper); let update_fields = wrapper.fields_set; - let _bvalues: Vec<&Value> = Vec::new(); if update_fields.is_empty() { + sql = wrapper.get_update_sql(&table.complete_name()).unwrap_or_default(); + } + let _bvalues: Vec<&Value> = Vec::new(); + if update_fields.is_empty() && update_sql.is_empty() { let data = entity.to_value(); let mut values: Vec = Vec::with_capacity(columns.len()); for col in columns.iter() { diff --git a/src/manager.rs b/src/manager.rs index bb4a7e4..be8c913 100644 --- a/src/manager.rs +++ b/src/manager.rs @@ -308,10 +308,6 @@ pub fn build_update_clause(platform: &DatabasePlatform, _entity: &T, wrapper: { let table = T::table_name(); let columns = T::fields(); - let update_sql = wrapper.get_update_sql(&table.complete_name()).unwrap_or_default(); - if !update_sql.is_empty() { - return update_sql; - } let set_fields = &mut wrapper.fields_set; let mut sql = String::new(); sql += &format!("update {} ", table.complete_name()); From 2fed35a098864be75e991d41821ebd4884265911 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BE=8D=E6=99=A8?= Date: Mon, 8 Aug 2022 18:28:32 +0800 Subject: [PATCH 4/6] feat(develop): Fixed the update condition --- src/akita.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/akita.rs b/src/akita.rs index ecc85cc..563282c 100644 --- a/src/akita.rs +++ b/src/akita.rs @@ -379,12 +379,13 @@ impl AkitaMapper for Akita { let mut conn = self.acquire()?; let columns = T::fields(); let mut sql = build_update_clause(&conn, entity, &mut wrapper); - let update_fields = wrapper.fields_set; - if update_fields.is_empty() { + let update_fields = wrapper.fields_set.to_owned(); + let is_set = wrapper.get_set_sql().is_none(); + if update_fields.is_empty() && !is_set { sql = wrapper.get_update_sql(&table.complete_name()).unwrap_or_default(); } let _bvalues: Vec<&Value> = Vec::new(); - if update_fields.is_empty() && update_sql.is_empty() { + if update_fields.is_empty() && is_set { let data = entity.to_value(); let mut values: Vec = Vec::with_capacity(columns.len()); for col in columns.iter() { From dff91fa360cd1bd7792bf4be05cd773374ad8235 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BE=8D=E6=99=A8?= Date: Tue, 16 Aug 2022 17:29:46 +0800 Subject: [PATCH 5/6] feat(develop): Remove affect rows & last insert id --- src/akita.rs | 20 +------------------- src/manager.rs | 27 ++++++++++++++------------- src/platform/mysql.rs | 4 ++-- 3 files changed, 17 insertions(+), 34 deletions(-) diff --git a/src/akita.rs b/src/akita.rs index 563282c..5df02f2 100644 --- a/src/akita.rs +++ b/src/akita.rs @@ -104,25 +104,6 @@ impl Akita { pub fn new_wrapper(&self) -> Wrapper { Wrapper::new() } - - pub fn affected_rows(&self) -> u64 { - match self.acquire() { - Ok(conn) => { - conn.affected_rows() - } - Err(_) => 0 - } - } - - pub fn last_insert_id(&self) -> u64 { - match self.acquire() { - Ok(conn) => { - conn.last_insert_id() - } - Err(_) => 0 - } - - } } #[allow(unused)] @@ -601,6 +582,7 @@ impl AkitaMapper for Akita { let rows = conn.execute_result(&sql.into(), params.into())?; Ok(rows) } + } #[allow(unused)] diff --git a/src/manager.rs b/src/manager.rs index be8c913..ba3fedb 100644 --- a/src/manager.rs +++ b/src/manager.rs @@ -16,15 +16,17 @@ pub struct AkitaTransaction<'a> { #[allow(unused)] impl AkitaTransaction <'_> { pub fn commit(mut self) -> Result<(), AkitaError> { - let mut conn = self.conn.acquire()?; - conn.commit_transaction()?; + // let mut conn = self.conn.acquire()?; + // let conn = self.conn.conn.get().unwrap(); + // conn.commit_transaction()?; self.committed = true; Ok(()) } pub fn rollback(mut self) -> Result<(), AkitaError> { - let mut conn = self.conn.acquire()?; - conn.rollback_transaction()?; + // let mut conn = self.conn.acquire()?; + // let conn = self.conn.conn.get().unwrap(); + // conn.rollback_transaction()?; self.rolled_back = true; Ok(()) } @@ -34,15 +36,14 @@ impl<'a> Drop for AkitaTransaction<'a> { /// Will rollback transaction. fn drop(&mut self) { if !self.committed && !self.rolled_back { - match self.conn.acquire() { - Ok(mut conn) => { - let _ = conn.rollback_transaction().unwrap_or_default(); - } - Err(_err) => { - // todo: Error to rollback - } - } - + // match self.conn.acquire() { + // Ok(mut conn) => { + // let _ = conn.rollback_transaction().unwrap_or_default(); + // } + // Err(_err) => { + // // todo: Error to rollback + // } + // } } } } diff --git a/src/platform/mysql.rs b/src/platform/mysql.rs index e6a0e1e..a853e0a 100644 --- a/src/platform/mysql.rs +++ b/src/platform/mysql.rs @@ -96,7 +96,7 @@ impl Database for MysqlDatabase { .query_iter(&sql) .map_err(|e| AkitaError::ExcuteSqlError(e.to_string(), sql.to_string()))?; let rows = collect(rows)?; - self.log(format!("AffectRows: {}", self.0.affected_rows())); + self.log(format!("AffectRows: {}", self.affected_rows())); Ok(rows) }, Params::Vector(param) => { @@ -112,7 +112,7 @@ impl Database for MysqlDatabase { .into(); let rows = self.0.exec_iter(stmt, ¶ms).map_err(|e| AkitaError::ExcuteSqlError(e.to_string(), sql.to_string()))?; let rows = collect(rows)?; - self.log(format!("AffectRows: {} records: {:?}", self.0.affected_rows(), rows)); + self.log(format!("AffectRows: {} records: {:?}", self.affected_rows(), rows)); Ok(rows) }, Params::Custom(param) => { From 9a921b123aac8f843b7264e0df573e33972c81c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BE=8D=E6=99=A8?= Date: Tue, 16 Aug 2022 17:31:45 +0800 Subject: [PATCH 6/6] feat(develop): Upgrade version --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index a7211e0..67be2cb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "akita" -version = "0.4.0" +version = "0.4.1" authors = ["mrpan <1049058427@qq.com>"] edition = "2018" description = "Akita - Mini orm for rust."