Functional sqlite backend

..but not too efficient, creating a new connection pretty much per-query
This commit is contained in:
dbr 2021-05-28 12:09:36 +10:00
parent f91f797244
commit 98f2ab51cb

View file

@ -86,7 +86,7 @@ impl SqliteStorage {
let queries = vec![ let queries = vec![
"CREATE TABLE IF NOT EXISTS clients (client_key STRING PRIMARY KEY, latest_version_id STRING);", "CREATE TABLE IF NOT EXISTS clients (client_key STRING PRIMARY KEY, latest_version_id STRING);",
"CREATE TABLE IF NOT EXISTS versions (id STRING PRIMARY KEY, client_key STRING, parent STRING, history_segment STRING);", "CREATE TABLE IF NOT EXISTS versions (version_id STRING PRIMARY KEY, client_key STRING, parent_version_id STRING, history_segment STRING);",
]; ];
for q in queries { for q in queries {
txn.execute(q, []).context("Creating table")?; txn.execute(q, []).context("Creating table")?;
@ -145,6 +145,7 @@ impl<'t> StorageTxn for Txn<'t> {
params![&StoredUuid(client_key), &StoredUuid(latest_version_id)], params![&StoredUuid(client_key), &StoredUuid(latest_version_id)],
) )
.context("Create client query")?; .context("Create client query")?;
t.commit()?;
Ok(()) Ok(())
} }
@ -162,7 +163,24 @@ impl<'t> StorageTxn for Txn<'t> {
client_key: Uuid, client_key: Uuid,
parent_version_id: Uuid, parent_version_id: Uuid,
) -> anyhow::Result<Option<Version>> { ) -> anyhow::Result<Option<Version>> {
todo!() let t = self.get_txn()?;
let r = t.query_row(
"SELECT version_id, parent_version_id, history_segment FROM versions WHERE parent_version_id = ? AND client_key = ?",
params![&StoredUuid(parent_version_id), &StoredUuid(client_key)],
|r| {
let version_id: StoredUuid = r.get("version_id")?;
let parent_version_id: StoredUuid = r.get("parent_version_id")?;
Ok(Version{
version_id: version_id.0,
parent_version_id: parent_version_id.0,
history_segment: r.get("history_segment")?,
})}
)
.optional()
.context("Get version query")
?;
Ok(r)
} }
fn add_version( fn add_version(
@ -175,7 +193,7 @@ impl<'t> StorageTxn for Txn<'t> {
let t = self.get_txn()?; let t = self.get_txn()?;
t.execute( t.execute(
"INSERT INTO versions (id, client_key, parent, history_segment) VALUES(?, ?, ?, ?)", "INSERT INTO versions (version_id, client_key, parent_version_id, history_segment) VALUES(?, ?, ?, ?)",
params![ params![
StoredUuid(version_id), StoredUuid(version_id),
StoredUuid(client_key), StoredUuid(client_key),
@ -184,7 +202,7 @@ impl<'t> StorageTxn for Txn<'t> {
], ],
) )
.context("Add version query")?; .context("Add version query")?;
t.commit()?;
Ok(()) Ok(())
} }