diff options
author | Alex Auvolat <alex@adnab.me> | 2022-06-07 18:26:28 +0200 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2022-06-07 18:26:28 +0200 |
commit | 48928d2d70ac34b0ea639d199a3e5c71322a5fd8 (patch) | |
tree | a5127757b4833aeb784f72acb35b0189e154e32f | |
parent | cc4f1aca978d9d4c71d89553e3bcf990c8efab18 (diff) | |
download | garage-48928d2d70ac34b0ea639d199a3e5c71322a5fd8.tar.gz garage-48928d2d70ac34b0ea639d199a3e5c71322a5fd8.zip |
Simplify sqlite
-rw-r--r-- | src/db/sqlite_adapter.rs | 44 |
1 files changed, 12 insertions, 32 deletions
diff --git a/src/db/sqlite_adapter.rs b/src/db/sqlite_adapter.rs index e11a3e60..14bf35ff 100644 --- a/src/db/sqlite_adapter.rs +++ b/src/db/sqlite_adapter.rs @@ -154,22 +154,12 @@ impl IDb for SqliteDb { let tree = this.get_tree(tree)?; let old_val = this.internal_get(tree, key)?; - match &old_val { - Some(_) => { - let n = this.db.execute( - &format!("UPDATE {} SET v = ?2 WHERE k = ?1", tree), - params![key, value], - )?; - 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); - } - } + let sql = match &old_val { + Some(_) => format!("UPDATE {} SET v = ?2 WHERE k = ?1", tree), + None => format!("INSERT INTO {} (k, v) VALUES (?1, ?2)", tree), + }; + let n = this.db.execute(&sql, params![key, value])?; + assert_eq!(n, 1); Ok(old_val) } @@ -343,22 +333,12 @@ impl<'a> ITx for SqliteTx<'a> { let tree = self.get_tree(tree)?; let old_val = self.internal_get(tree, key)?; - match &old_val { - Some(_) => { - let n = self.tx.execute( - &format!("UPDATE {} SET v = ?2 WHERE k = ?1", tree), - params![key, value], - )?; - 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); - } - } + let sql = match &old_val { + Some(_) => format!("UPDATE {} SET v = ?2 WHERE k = ?1", tree), + None => format!("INSERT INTO {} (k, v) VALUES (?1, ?2)", tree), + }; + let n = self.tx.execute(&sql, params![key, value])?; + assert_eq!(n, 1); Ok(old_val) } |