sqlite: Rename utils::ConnectionExt to SqliteKeyValueStoreConnExt

Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
This commit is contained in:
Kévin Commaille
2024-08-26 15:01:06 +02:00
committed by Benjamin Bouvier
parent 6e369aecc9
commit 6e62f8b269
2 changed files with 27 additions and 15 deletions

View File

@@ -45,7 +45,7 @@ use crate::{
error::{Error, Result},
get_or_create_store_cipher,
utils::{
load_db_version, repeat_vars, Key, SqliteAsyncConnExt, SqliteConnectionExt as _,
load_db_version, repeat_vars, Key, SqliteAsyncConnExt, SqliteKeyValueStoreConnExt,
SqliteObjectStoreExt as _,
},
OpenStoreError,

View File

@@ -185,20 +185,6 @@ impl SqliteAsyncConnExt for SqliteAsyncConn {
}
}
pub(crate) trait SqliteConnectionExt {
fn set_kv(&self, key: &str, value: &[u8]) -> rusqlite::Result<()>;
}
impl SqliteConnectionExt for rusqlite::Connection {
fn set_kv(&self, key: &str, value: &[u8]) -> rusqlite::Result<()> {
self.execute(
"INSERT INTO kv VALUES (?1, ?2) ON CONFLICT (key) DO UPDATE SET value = ?2",
(key, value),
)?;
Ok(())
}
}
pub(crate) trait SqliteTransactionExt {
fn chunk_large_query_over<Query, Res>(
&self,
@@ -255,6 +241,32 @@ impl<'a> SqliteTransactionExt for Transaction<'a> {
}
}
/// Extension trait for a [`rusqlite::Connection`] that contains a key-value
/// table named `kv`.
///
/// The table should be created like this:
///
/// ```sql
/// CREATE TABLE "kv" (
/// "key" TEXT PRIMARY KEY NOT NULL,
/// "value" BLOB NOT NULL
/// );
/// ```
pub(crate) trait SqliteKeyValueStoreConnExt {
/// Store the given value for the given key.
fn set_kv(&self, key: &str, value: &[u8]) -> rusqlite::Result<()>;
}
impl SqliteKeyValueStoreConnExt for rusqlite::Connection {
fn set_kv(&self, key: &str, value: &[u8]) -> rusqlite::Result<()> {
self.execute(
"INSERT INTO kv VALUES (?1, ?2) ON CONFLICT (key) DO UPDATE SET value = ?2",
(key, value),
)?;
Ok(())
}
}
#[async_trait]
pub(crate) trait SqliteObjectStoreExt: SqliteAsyncConnExt {
async fn get_kv(&self, key: &str) -> rusqlite::Result<Option<Vec<u8>>> {