summaryrefslogtreecommitdiffstats
path: root/src/local/database.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/local/database.rs')
-rw-r--r--src/local/database.rs23
1 files changed, 23 insertions, 0 deletions
diff --git a/src/local/database.rs b/src/local/database.rs
index 5b98bb36..3133578b 100644
--- a/src/local/database.rs
+++ b/src/local/database.rs
@@ -8,6 +8,10 @@ use rusqlite::{Transaction, NO_PARAMS};
use super::history::History;
+pub enum QueryParam {
+ Text(String),
+}
+
pub trait Database {
fn save(&mut self, h: &History) -> Result<()>;
fn save_bulk(&mut self, h: &[History]) -> Result<()>;
@@ -16,6 +20,7 @@ pub trait Database {
fn range(&self, from: chrono::DateTime<Utc>, to: chrono::DateTime<Utc>)
-> Result<Vec<History>>;
fn update(&self, h: &History) -> Result<()>;
+ fn query(&self, query: &str, params: &[QueryParam]) -> Result<Vec<History>>;
}
// Intended for use on a developer machine and not a sync server.
@@ -95,6 +100,16 @@ impl Sqlite {
}
}
+impl rusqlite::ToSql for QueryParam {
+ fn to_sql(&self) -> Result<rusqlite::types::ToSqlOutput<'_>, rusqlite::Error> {
+ use rusqlite::types::{ToSqlOutput, Value};
+
+ match self {
+ QueryParam::Text(s) => Ok(ToSqlOutput::Owned(Value::Text(s.clone()))),
+ }
+ }
+}
+
impl Database for Sqlite {
fn save(&mut self, h: &History) -> Result<()> {
debug!("saving history to sqlite");
@@ -176,6 +191,14 @@ impl Database for Sqlite {
Ok(history_iter.filter_map(Result::ok).collect())
}
+
+ fn query(&self, query: &str, params: &[QueryParam]) -> Result<Vec<History>> {
+ let mut stmt = self.conn.prepare(query)?;
+
+ let history_iter = stmt.query_map(params, |row| history_from_sqlite_row(None, row))?;
+
+ Ok(history_iter.filter_map(Result::ok).collect())
+ }
}
fn history_from_sqlite_row(