Skip to main content

vs_store/store/
annotations.rs

1//! `annotations` table CRUD.
2
3use rusqlite::params;
4
5use super::{epoch_secs, Store};
6use crate::error::Result;
7use crate::types::{Annotation, AnnotationTarget};
8
9impl Store {
10    pub fn add_annotation(
11        &mut self,
12        id: &str,
13        target: &AnnotationTarget,
14        key: &str,
15        value: Option<&str>,
16    ) -> Result<Annotation> {
17        let now = epoch_secs();
18        let kind = target.kind();
19        let target_id = target.id();
20        self.conn().execute(
21            "INSERT INTO annotations(id, target_kind, target_id, key, value, created_at)
22             VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
23            params![id, kind, target_id, key, value, now],
24        )?;
25        Ok(Annotation {
26            id: id.to_string(),
27            target: target.clone(),
28            key: key.to_string(),
29            value: value.map(str::to_string),
30            created_at: now,
31        })
32    }
33
34    pub fn list_annotations(&self, target: &AnnotationTarget) -> Result<Vec<Annotation>> {
35        let kind = target.kind();
36        let target_id = target.id();
37        let mut stmt = self.conn().prepare(
38            "SELECT * FROM annotations
39              WHERE target_kind=?1 AND target_id=?2
40              ORDER BY created_at ASC",
41        )?;
42        let rows = stmt.query_map([kind, target_id.as_str()], Annotation::from_row)?;
43        Ok(rows.collect::<rusqlite::Result<Vec<_>>>()?)
44    }
45}