coven_database/
local_state.rs1use super::*;
2
3pub(crate) fn get_protocol_state_on(
5 conn: &Connection,
6 key: &str,
7) -> Result<Option<String>, DbError> {
8 conn.query_row(
9 "SELECT value FROM protocol_state WHERE key = ?1",
10 [key],
11 |row| row.get(0),
12 )
13 .optional()
14 .map_err(DbError::from)
15}
16
17pub(crate) fn required_protocol_state_on(conn: &Connection, key: &str) -> Result<String, DbError> {
20 get_protocol_state_on(conn, key)?
21 .ok_or_else(|| DbError::Message(format!("protocol_state key {key:?} is absent")))
22}
23
24pub(crate) fn set_protocol_state_on(
26 conn: &Connection,
27 key: &str,
28 value: &str,
29) -> Result<(), DbError> {
30 conn.execute(
31 "INSERT INTO protocol_state (key, value) VALUES (?1, ?2) \
32 ON CONFLICT(key) DO UPDATE SET value = excluded.value",
33 (key, value),
34 )
35 .map(|_| ())
36 .map_err(DbError::from)
37}
38
39pub(crate) fn delete_protocol_state_on(conn: &Connection, key: &str) -> Result<usize, DbError> {
42 conn.execute("DELETE FROM protocol_state WHERE key = ?1", [key])
43 .map_err(DbError::from)
44}
45
46#[cfg(any(test, feature = "test-utils"))]
47impl Database {
48 pub async fn get_protocol_state(&self, key: &str) -> Result<Option<String>, DbError> {
49 let key = key.to_string();
50 self.call_database(move |session| session.protocol_state(&key))
51 .await
52 }
53
54 pub async fn set_protocol_state(&self, key: &str, value: &str) -> Result<(), DbError> {
55 let (key, value) = (key.to_string(), value.to_string());
56 self.call_database(move |session| session.set_protocol_state(&key, &value))
57 .await
58 }
59
60 pub async fn delete_protocol_state(&self, key: &str) -> Result<(), DbError> {
61 let key = key.to_string();
62 self.call_database(move |session| session.delete_protocol_state(&key))
63 .await
64 }
65}