Skip to content

Commit

Permalink
Merge pull request #36 from wslongchen/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
wslongchen authored Aug 16, 2022
2 parents c59c393 + 9a921b1 commit dced6e6
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 59 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "akita"
version = "0.4.0"
version = "0.4.1"
authors = ["mrpan <[email protected]>"]
edition = "2018"
description = "Akita - Mini orm for rust."
Expand Down
30 changes: 8 additions & 22 deletions src/akita.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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<Value> = Vec::with_capacity(columns.len());
for col in columns.iter() {
Expand Down Expand Up @@ -597,6 +582,7 @@ impl AkitaMapper for Akita {
let rows = conn.execute_result(&sql.into(), params.into())?;
Ok(rows)
}

}

#[allow(unused)]
Expand Down
28 changes: 14 additions & 14 deletions src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
Expand All @@ -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
// }
// }
}
}
}
Expand Down Expand Up @@ -311,7 +312,6 @@ pub fn build_update_clause<T>(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 {}",
Expand Down
4 changes: 2 additions & 2 deletions src/platform/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -112,7 +112,7 @@ impl Database for MysqlDatabase {
.into();
let rows = self.0.exec_iter(stmt, &params).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) => {
Expand Down
2 changes: 1 addition & 1 deletion src/segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
}
}

Expand Down
25 changes: 6 additions & 19 deletions src/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,6 @@ impl Wrapper{
self.set_condition(true, column, val)
}

pub fn sets<S: Into<String>, U: ISegment>(self, column: S, val: U) -> Self {
self.set_conditions(true, column, val)
}

pub fn set_conditions<S: Into<String>, 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<S: Into<String>, U: ToSegment>(mut self,condition: bool, column: S, val: U) -> Self {
if condition {
let col: String = column.into();
Expand All @@ -86,9 +73,9 @@ impl Wrapper{
self
}

pub fn set_sql<S: Into<String>>(mut self, condition: bool, sql: S) -> Self {
pub fn set_sql<S: Into<String>>(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
Expand All @@ -108,7 +95,7 @@ impl Wrapper{
self.sql_set.clear();
}

pub fn get_update_sql(&mut self, table_name: &'static str) -> Result<String, &str> {
pub fn get_update_sql(&mut self, table_name: &str) -> Result<String, &str> {
let set_fields = if let Some(set) = self.get_set_sql() {
set.to_owned()
} else {
Expand All @@ -127,7 +114,7 @@ impl Wrapper{
}
}

pub fn get_query_sql(mut self, table_name: &'static str) -> Result<String, &str> {
pub fn get_query_sql(mut self, table_name: &str) -> Result<String, &str> {
let select_fields = self.get_select_sql();
if table_name.is_empty() {
Err("table name is empty!!!")
Expand Down Expand Up @@ -255,7 +242,7 @@ impl Wrapper{
fn basic_test() {
let s : Option<String> = Some("ffffa".to_string());
let d: Option<i32> = 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());
}

0 comments on commit dced6e6

Please sign in to comment.