feat: add read/write
This commit is contained in:
12
src/db.rs
12
src/db.rs
@@ -8,6 +8,10 @@ pub struct Key {
|
||||
pub encrypted_key: String,
|
||||
}
|
||||
|
||||
pub fn make_db_key_name(name: &str) -> String {
|
||||
format!("value:{}", name)
|
||||
}
|
||||
|
||||
pub fn open_db(db: &str) -> XResult<Connection> {
|
||||
let con = opt_result!(Connection::open(db), "Open sqlite db: {}, failed: {}", db);
|
||||
debugging!("Db auto commit: {}", con.is_autocommit());
|
||||
@@ -41,6 +45,14 @@ pub fn insert_key(conn: &Connection, key: &Key) -> XResult<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn update_key(conn: &Connection, key: &Key) -> XResult<()> {
|
||||
let _ = conn.execute(
|
||||
"UPDATE keys SET value = ?1 WHERE name = ?2",
|
||||
(&key.encrypted_key, &key.name),
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn find_key(conn: &Connection, name: &str) -> XResult<Option<Key>> {
|
||||
let mut stmt = conn.prepare("SELECT id, name, value FROM keys WHERE name = ?1")?;
|
||||
let mut key_iter = stmt.query_map(params![name], |row| {
|
||||
|
||||
Reference in New Issue
Block a user