Abstract database behind generic interface and implement alternative drivers #322
1 changed files with 12 additions and 32 deletions
|
@ -154,22 +154,12 @@ impl IDb for SqliteDb {
|
||||||
let tree = this.get_tree(tree)?;
|
let tree = this.get_tree(tree)?;
|
||||||
let old_val = this.internal_get(tree, key)?;
|
let old_val = this.internal_get(tree, key)?;
|
||||||
|
|
||||||
match &old_val {
|
let sql = match &old_val {
|
||||||
Some(_) => {
|
Some(_) => format!("UPDATE {} SET v = ?2 WHERE k = ?1", tree),
|
||||||
let n = this.db.execute(
|
None => format!("INSERT INTO {} (k, v) VALUES (?1, ?2)", tree),
|
||||||
&format!("UPDATE {} SET v = ?2 WHERE k = ?1", tree),
|
};
|
||||||
params![key, value],
|
let n = this.db.execute(&sql, params![key, value])?;
|
||||||
)?;
|
|
||||||
assert_eq!(n, 1);
|
assert_eq!(n, 1);
|
||||||
}
|
|
||||||
None => {
|
|
||||||
let n = this.db.execute(
|
|
||||||
&format!("INSERT INTO {} (k, v) VALUES (?1, ?2)", tree),
|
|
||||||
params![key, value],
|
|
||||||
)?;
|
|
||||||
assert_eq!(n, 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(old_val)
|
Ok(old_val)
|
||||||
}
|
}
|
||||||
|
@ -343,22 +333,12 @@ impl<'a> ITx for SqliteTx<'a> {
|
||||||
let tree = self.get_tree(tree)?;
|
let tree = self.get_tree(tree)?;
|
||||||
let old_val = self.internal_get(tree, key)?;
|
let old_val = self.internal_get(tree, key)?;
|
||||||
|
|
||||||
match &old_val {
|
let sql = match &old_val {
|
||||||
Some(_) => {
|
Some(_) => format!("UPDATE {} SET v = ?2 WHERE k = ?1", tree),
|
||||||
let n = self.tx.execute(
|
None => format!("INSERT INTO {} (k, v) VALUES (?1, ?2)", tree),
|
||||||
&format!("UPDATE {} SET v = ?2 WHERE k = ?1", tree),
|
};
|
||||||
params![key, value],
|
let n = self.tx.execute(&sql, params![key, value])?;
|
||||||
)?;
|
|
||||||
assert_eq!(n, 1);
|
assert_eq!(n, 1);
|
||||||
}
|
|
||||||
None => {
|
|
||||||
let n = self.tx.execute(
|
|
||||||
&format!("INSERT INTO {} (k, v) VALUES (?1, ?2)", tree),
|
|
||||||
params![key, value],
|
|
||||||
)?;
|
|
||||||
assert_eq!(n, 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(old_val)
|
Ok(old_val)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue