BlueGit

ict rusty-teams

Files

rusty-teams / crates / store / src / meta.rs

meta.rs

meta.rs 1.4 KiB Download Raw
1use chrono::{DateTime, Utc};
2use rusqlite::{OptionalExtension, params};
3
4use crate::error::Result;
5use crate::store::Store;
6use crate::time::{from_millis, to_millis};
7
8impl Store {
9 pub fn meta(&self, key: &str) -> Result<Option<String>> {
10 let connection = self.read()?;
11 Ok(connection
12 .query_row("SELECT value FROM meta WHERE key = ?1", [key], |row| {
13 row.get(0)
14 })
15 .optional()?)
16 }
17
18 pub fn set_meta(&self, key: &str, value: &str) -> Result<()> {
19 let connection = self.lock()?;
20 connection.execute(
21 "INSERT INTO meta (key, value) VALUES (?1, ?2) ON CONFLICT (key) DO UPDATE SET value = excluded.value",
22 params![key, value],
23 )?;
24 Ok(())
25 }
26
27 pub fn set_meta_if_absent(&self, key: &str, value: &str) -> Result<()> {
28 let connection = self.lock()?;
29 connection.execute(
30 "INSERT INTO meta (key, value) VALUES (?1, ?2) ON CONFLICT (key) DO NOTHING",
31 params![key, value],
32 )?;
33 Ok(())
34 }
35
36 pub fn meta_time(&self, key: &str) -> Result<Option<DateTime<Utc>>> {
37 Ok(self
38 .meta(key)?
39 .and_then(|value| value.parse().ok())
40 .map(from_millis))
41 }
42
43 pub fn set_meta_time(&self, key: &str, time: DateTime<Utc>) -> Result<()> {
44 self.set_meta(key, &to_millis(time).to_string())
45 }
46}