Skip to main content

chainlink/db/
labels.rs

1use anyhow::Result;
2use rusqlite::params;
3
4use super::{Database, MAX_LABEL_LEN};
5
6impl Database {
7    pub fn add_label(&self, issue_id: i64, label: &str) -> Result<bool> {
8        if label.len() > MAX_LABEL_LEN {
9            anyhow::bail!(
10                "Label exceeds maximum length of {} characters",
11                MAX_LABEL_LEN
12            );
13        }
14        let result = self.conn.execute(
15            "INSERT OR IGNORE INTO labels (issue_id, label) VALUES (?1, ?2)",
16            params![issue_id, label],
17        )?;
18        Ok(result > 0)
19    }
20
21    pub fn remove_label(&self, issue_id: i64, label: &str) -> Result<bool> {
22        let rows = self.conn.execute(
23            "DELETE FROM labels WHERE issue_id = ?1 AND label = ?2",
24            params![issue_id, label],
25        )?;
26        Ok(rows > 0)
27    }
28
29    pub fn get_labels(&self, issue_id: i64) -> Result<Vec<String>> {
30        let mut stmt = self
31            .conn
32            .prepare("SELECT label FROM labels WHERE issue_id = ?1 ORDER BY label")?;
33        let labels = stmt
34            .query_map([issue_id], |row| row.get(0))?
35            .collect::<std::result::Result<Vec<String>, _>>()?;
36        Ok(labels)
37    }
38}