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." diff --git a/src/akita.rs b/src/akita.rs index f4e565e..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)] @@ -378,10 +359,14 @@ impl AkitaMapper for Akita { } let mut conn = self.acquire()?; let columns = T::fields(); - let sql = build_update_clause(&conn, entity, &mut wrapper); - let update_fields = wrapper.fields_set; + let mut sql = build_update_clause(&conn, entity, &mut wrapper); + 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() { + 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() { @@ -597,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 3f4b640..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 + // } + // } } } } @@ -311,7 +312,6 @@ pub fn build_update_clause(platform: &DatabasePlatform, _entity: &T, wrapper: 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/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) => { diff --git a/src/segment.rs b/src/segment.rs index 82c46c2..765c087 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))) } } 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