refactor(backend): implement Storable trait and refactor database operations

This commit introduces the `Storable` trait to standardize the conversion of database rows into application data structures. It refactors existing manager implementations (`AiUsageManager`, `FrecencyManager`, `QuicklinkManager`, `SnippetManager`) to utilize the new `query` and `execute` methods in the `Store` struct.
This commit is contained in:
ByteAtATime 2025-06-29 16:11:34 -07:00
parent 881fb9f53d
commit cfbd31f51b
No known key found for this signature in database
5 changed files with 141 additions and 152 deletions

View file

@ -1,5 +1,5 @@
use crate::error::AppError;
use crate::store::Store;
use crate::store::{Storable, Store};
use futures_util::StreamExt;
use once_cell::sync::Lazy;
use rusqlite::{params, Result as RusqliteResult};
@ -61,6 +61,21 @@ pub struct GenerationData {
pub total_cost: f64,
}
impl Storable for GenerationData {
fn from_row(row: &rusqlite::Row) -> RusqliteResult<Self> {
Ok(GenerationData {
id: row.get(0)?,
created: row.get(1)?,
model: row.get(2)?,
tokens_prompt: row.get(3)?,
tokens_completion: row.get(4)?,
native_tokens_prompt: row.get(5)?,
native_tokens_completion: row.get(6)?,
total_cost: row.get(7)?,
})
}
}
static DEFAULT_AI_MODELS: Lazy<HashMap<&'static str, &'static str>> = Lazy::new(|| {
let mut m = HashMap::new();
// OpenAI
@ -244,8 +259,7 @@ impl AiUsageManager {
}
pub fn log_generation(&self, data: &GenerationData) -> Result<(), AppError> {
let db = self.store.conn();
db.execute(
self.store.execute(
"INSERT OR REPLACE INTO ai_generations (id, created, model, tokens_prompt, tokens_completion, native_tokens_prompt, native_tokens_completion, total_cost)
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)",
params![
@ -263,25 +277,10 @@ impl AiUsageManager {
}
pub fn get_history(&self, limit: u32, offset: u32) -> Result<Vec<GenerationData>, AppError> {
let db = self.store.conn();
let mut stmt = db.prepare(
self.store.query(
"SELECT id, created, model, tokens_prompt, tokens_completion, native_tokens_prompt, native_tokens_completion, total_cost FROM ai_generations ORDER BY created DESC LIMIT ?1 OFFSET ?2",
)?;
let iter = stmt.query_map(params![limit, offset], |row| {
Ok(GenerationData {
id: row.get(0)?,
created: row.get(1)?,
model: row.get(2)?,
tokens_prompt: row.get(3)?,
tokens_completion: row.get(4)?,
native_tokens_prompt: row.get(5)?,
native_tokens_completion: row.get(6)?,
total_cost: row.get(7)?,
})
})?;
iter.collect::<RusqliteResult<Vec<_>>>()
.map_err(|e| e.into())
params![limit, offset],
)
}
}

View file

@ -1,5 +1,5 @@
use crate::error::AppError;
use crate::store::Store;
use crate::store::{Storable, Store};
use chrono::Utc;
use rusqlite::{params, Result as RusqliteResult};
use serde::Serialize;
@ -21,6 +21,16 @@ pub struct FrecencyData {
pub last_used_at: i64,
}
impl Storable for FrecencyData {
fn from_row(row: &rusqlite::Row) -> RusqliteResult<Self> {
Ok(FrecencyData {
item_id: row.get(0)?,
use_count: row.get(1)?,
last_used_at: row.get(2)?,
})
}
}
pub struct FrecencyManager {
store: Store,
}
@ -34,9 +44,8 @@ impl FrecencyManager {
}
pub fn record_usage(&self, item_id: String) -> Result<(), AppError> {
let db = self.store.conn();
let now = Utc::now().timestamp();
db.execute(
self.store.execute(
"INSERT INTO frecency (item_id, use_count, last_used_at) VALUES (?, 1, ?)
ON CONFLICT(item_id) DO UPDATE SET
use_count = use_count + 1,
@ -47,30 +56,18 @@ impl FrecencyManager {
}
pub fn get_frecency_data(&self) -> Result<Vec<FrecencyData>, AppError> {
let db = self.store.conn();
let mut stmt = db.prepare("SELECT item_id, use_count, last_used_at FROM frecency")?;
let data_iter = stmt.query_map([], |row| {
Ok(FrecencyData {
item_id: row.get(0)?,
use_count: row.get(1)?,
last_used_at: row.get(2)?,
})
})?;
data_iter
.collect::<RusqliteResult<Vec<_>>>()
.map_err(|e| e.into())
self.store
.query("SELECT item_id, use_count, last_used_at FROM frecency", [])
}
pub fn delete_frecency_entry(&self, item_id: String) -> Result<(), AppError> {
let db = self.store.conn();
db.execute("DELETE FROM frecency WHERE item_id = ?", params![item_id])?;
self.store
.execute("DELETE FROM frecency WHERE item_id = ?", params![item_id])?;
Ok(())
}
pub fn hide_item(&self, item_id: String) -> Result<(), AppError> {
let db = self.store.conn();
db.execute(
self.store.execute(
"INSERT OR IGNORE INTO hidden_items (item_id) VALUES (?)",
params![item_id],
)?;

View file

@ -1,5 +1,5 @@
use crate::error::AppError;
use crate::store::Store;
use crate::store::{Storable, Store};
use chrono::{DateTime, Utc};
use rusqlite::{params, Result as RusqliteResult};
use serde::Serialize;
@ -28,6 +28,22 @@ pub struct Quicklink {
updated_at: DateTime<Utc>,
}
impl Storable for Quicklink {
fn from_row(row: &rusqlite::Row) -> RusqliteResult<Self> {
let created_at_ts: i64 = row.get(5)?;
let updated_at_ts: i64 = row.get(6)?;
Ok(Quicklink {
id: row.get(0)?,
name: row.get(1)?,
link: row.get(2)?,
application: row.get(3)?,
icon: row.get(4)?,
created_at: DateTime::from_timestamp(created_at_ts, 0).unwrap_or_default(),
updated_at: DateTime::from_timestamp(updated_at_ts, 0).unwrap_or_default(),
})
}
}
pub struct QuicklinkManager {
store: Store,
}
@ -46,36 +62,20 @@ impl QuicklinkManager {
application: Option<String>,
icon: Option<String>,
) -> Result<i64, AppError> {
let db = self.store.conn();
let now = Utc::now().timestamp();
db.execute(
self.store.execute(
"INSERT INTO quicklinks (name, link, application, icon, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?)",
params![name, link, application, icon, now, now],
)?;
Ok(db.last_insert_rowid())
Ok(self.store.last_insert_rowid())
}
fn list_quicklinks(&self) -> Result<Vec<Quicklink>, AppError> {
let db = self.store.conn();
let mut stmt = db.prepare("SELECT id, name, link, application, icon, created_at, updated_at FROM quicklinks ORDER BY name ASC")?;
let quicklinks_iter = stmt.query_map([], |row| {
let created_at_ts: i64 = row.get(5)?;
let updated_at_ts: i64 = row.get(6)?;
Ok(Quicklink {
id: row.get(0)?,
name: row.get(1)?,
link: row.get(2)?,
application: row.get(3)?,
icon: row.get(4)?,
created_at: DateTime::from_timestamp(created_at_ts, 0).unwrap_or_default(),
updated_at: DateTime::from_timestamp(updated_at_ts, 0).unwrap_or_default(),
})
})?;
quicklinks_iter
.collect::<RusqliteResult<Vec<_>>>()
.map_err(|e| e.into())
self.store.query(
"SELECT id, name, link, application, icon, created_at, updated_at FROM quicklinks ORDER BY name ASC",
[],
)
}
fn update_quicklink(
@ -86,9 +86,8 @@ impl QuicklinkManager {
application: Option<String>,
icon: Option<String>,
) -> Result<(), AppError> {
let db = self.store.conn();
let now = Utc::now().timestamp();
db.execute(
self.store.execute(
"UPDATE quicklinks SET name = ?, link = ?, application = ?, icon = ?, updated_at = ?
WHERE id = ?",
params![name, link, application, icon, now, id],
@ -97,8 +96,8 @@ impl QuicklinkManager {
}
fn delete_quicklink(&self, id: i64) -> Result<(), AppError> {
let db = self.store.conn();
db.execute("DELETE FROM quicklinks WHERE id = ?", params![id])?;
self.store
.execute("DELETE FROM quicklinks WHERE id = ?", params![id])?;
Ok(())
}
}

View file

@ -1,6 +1,6 @@
use crate::error::AppError;
use crate::snippets::types::Snippet;
use crate::store::Store;
use crate::store::{Storable, Store};
use chrono::{DateTime, Utc};
use rusqlite::params;
use std::sync::Arc;
@ -20,6 +20,24 @@ pub struct SnippetManager {
store: Arc<Store>,
}
impl Storable for Snippet {
fn from_row(row: &rusqlite::Row) -> rusqlite::Result<Self> {
let created_at_ts: i64 = row.get(4)?;
let updated_at_ts: i64 = row.get(5)?;
let last_used_at_ts: i64 = row.get(7)?;
Ok(Snippet {
id: row.get(0)?,
name: row.get(1)?,
keyword: row.get(2)?,
content: row.get(3)?,
created_at: DateTime::from_timestamp(created_at_ts, 0).unwrap_or_default(),
updated_at: DateTime::from_timestamp(updated_at_ts, 0).unwrap_or_default(),
times_used: row.get(6)?,
last_used_at: DateTime::from_timestamp(last_used_at_ts, 0).unwrap_or_default(),
})
}
}
impl SnippetManager {
pub fn new(app_handle: &AppHandle) -> Result<Self, AppError> {
let store = Store::new(app_handle, "snippets.sqlite")?;
@ -58,52 +76,29 @@ impl SnippetManager {
keyword: String,
content: String,
) -> Result<i64, AppError> {
let db = self.store.conn();
let now = Utc::now().timestamp();
db.execute(
self.store.execute(
"INSERT INTO snippets (name, keyword, content, created_at, updated_at, times_used, last_used_at)
VALUES (?1, ?2, ?3, ?4, ?4, 0, 0)",
params![name, keyword, content, now],
)?;
Ok(db.last_insert_rowid())
Ok(self.store.last_insert_rowid())
}
pub fn list_snippets(&self, search_term: Option<String>) -> Result<Vec<Snippet>, AppError> {
let db = self.store.conn();
let mut query = "SELECT id, name, keyword, content, created_at, updated_at, times_used, last_used_at FROM snippets".to_string();
let mut params_vec: Vec<Box<dyn rusqlite::ToSql>> = vec![];
if let Some(term) = search_term {
if !term.is_empty() {
query.push_str(" WHERE name LIKE ?1 OR keyword LIKE ?1 OR content LIKE ?1");
params_vec.push(Box::new(format!("%{}%", term)));
query.push_str(" ORDER BY updated_at DESC");
let search_param = format!("%{}%", term);
return self.store.query(&query, params![search_param]);
}
}
query.push_str(" ORDER BY updated_at DESC");
let params_ref: Vec<&dyn rusqlite::ToSql> = params_vec.iter().map(|b| b.as_ref()).collect();
let mut stmt = db.prepare(&query)?;
let snippets_iter = stmt.query_map(&params_ref[..], |row| {
let created_at_ts: i64 = row.get(4)?;
let updated_at_ts: i64 = row.get(5)?;
let last_used_at_ts: i64 = row.get(7)?;
Ok(Snippet {
id: row.get(0)?,
name: row.get(1)?,
keyword: row.get(2)?,
content: row.get(3)?,
created_at: DateTime::from_timestamp(created_at_ts, 0).unwrap_or_default(),
updated_at: DateTime::from_timestamp(updated_at_ts, 0).unwrap_or_default(),
times_used: row.get(6)?,
last_used_at: DateTime::from_timestamp(last_used_at_ts, 0).unwrap_or_default(),
})
})?;
snippets_iter
.collect::<Result<Vec<_>, _>>()
.map_err(|e| e.into())
self.store.query(&query, [])
}
pub fn update_snippet(
@ -113,9 +108,8 @@ impl SnippetManager {
keyword: String,
content: String,
) -> Result<(), AppError> {
let db = self.store.conn();
let now = Utc::now().timestamp();
db.execute(
self.store.execute(
"UPDATE snippets SET name = ?1, keyword = ?2, content = ?3, updated_at = ?4 WHERE id = ?5",
params![name, keyword, content, now, id],
)?;
@ -123,15 +117,14 @@ impl SnippetManager {
}
pub fn delete_snippet(&self, id: i64) -> Result<(), AppError> {
let db = self.store.conn();
db.execute("DELETE FROM snippets WHERE id = ?1", params![id])?;
self.store
.execute("DELETE FROM snippets WHERE id = ?1", params![id])?;
Ok(())
}
pub fn snippet_was_used(&self, id: i64) -> Result<(), AppError> {
let db = self.store.conn();
let now = Utc::now().timestamp();
db.execute(
self.store.execute(
"UPDATE snippets SET times_used = times_used + 1, last_used_at = ?1 WHERE id = ?2",
params![now, id],
)?;
@ -139,54 +132,16 @@ impl SnippetManager {
}
pub fn find_snippet_by_keyword(&self, keyword: &str) -> Result<Option<Snippet>, AppError> {
let db = self.store.conn();
let mut stmt = db.prepare("SELECT id, name, keyword, content, created_at, updated_at, times_used, last_used_at FROM snippets WHERE keyword = ?1")?;
let mut rows = stmt.query_map(params![keyword], |row| {
let created_at_ts: i64 = row.get(4)?;
let updated_at_ts: i64 = row.get(5)?;
let last_used_at_ts: i64 = row.get(7)?;
Ok(Snippet {
id: row.get(0)?,
name: row.get(1)?,
keyword: row.get(2)?,
content: row.get(3)?,
created_at: DateTime::from_timestamp(created_at_ts, 0).unwrap_or_default(),
updated_at: DateTime::from_timestamp(updated_at_ts, 0).unwrap_or_default(),
times_used: row.get(6)?,
last_used_at: DateTime::from_timestamp(last_used_at_ts, 0).unwrap_or_default(),
})
})?;
if let Some(row) = rows.next() {
Ok(Some(row?))
} else {
Ok(None)
}
self.store.query_row(
"SELECT id, name, keyword, content, created_at, updated_at, times_used, last_used_at FROM snippets WHERE keyword = ?1",
params![keyword],
)
}
pub fn find_snippet_by_name(&self, name: &str) -> Result<Option<Snippet>, AppError> {
let db = self.store.conn();
let mut stmt = db.prepare("SELECT id, name, keyword, content, created_at, updated_at, times_used, last_used_at FROM snippets WHERE name = ?1 ORDER BY updated_at DESC LIMIT 1")?;
let mut rows = stmt.query_map(params![name], |row| {
let created_at_ts: i64 = row.get(4)?;
let updated_at_ts: i64 = row.get(5)?;
let last_used_at_ts: i64 = row.get(7)?;
Ok(Snippet {
id: row.get(0)?,
name: row.get(1)?,
keyword: row.get(2)?,
content: row.get(3)?,
created_at: DateTime::from_timestamp(created_at_ts, 0).unwrap_or_default(),
updated_at: DateTime::from_timestamp(updated_at_ts, 0).unwrap_or_default(),
times_used: row.get(6)?,
last_used_at: DateTime::from_timestamp(last_used_at_ts, 0).unwrap_or_default(),
})
})?;
if let Some(row) = rows.next() {
Ok(Some(row?))
} else {
Ok(None)
}
self.store.query_row(
"SELECT id, name, keyword, content, created_at, updated_at, times_used, last_used_at FROM snippets WHERE name = ?1 ORDER BY updated_at DESC LIMIT 1",
params![name],
)
}
}

View file

@ -1,8 +1,12 @@
use crate::error::AppError;
use rusqlite::Connection;
use rusqlite::{Connection, Result as RusqliteResult, Row, ToSql};
use std::sync::{Mutex, MutexGuard};
use tauri::{AppHandle, Manager};
pub trait Storable: Sized {
fn from_row(row: &Row) -> RusqliteResult<Self>;
}
pub struct Store {
db: Mutex<Connection>,
}
@ -23,11 +27,46 @@ impl Store {
}
pub fn init_table(&self, schema: &str) -> Result<(), AppError> {
self.db.lock().unwrap().execute(schema, [])?;
self.conn().execute(schema, [])?;
Ok(())
}
pub fn conn(&self) -> MutexGuard<Connection> {
self.db.lock().unwrap()
}
pub fn query<T: Storable, P: rusqlite::Params>(
&self,
sql: &str,
params: P,
) -> Result<Vec<T>, AppError> {
let db = self.conn();
let mut stmt = db.prepare(sql)?;
let iter = stmt.query_map(params, T::from_row)?;
iter.collect::<RusqliteResult<Vec<_>>>().map_err(|e| e.into())
}
pub fn query_row<T: Storable, P: rusqlite::Params>(
&self,
sql: &str,
params: P,
) -> Result<Option<T>, AppError> {
let db = self.conn();
let mut stmt = db.prepare(sql)?;
let mut iter = stmt.query_map(params, T::from_row)?;
if let Some(row) = iter.next() {
Ok(Some(row?))
} else {
Ok(None)
}
}
pub fn execute<P: rusqlite::Params>(&self, sql: &str, params: P) -> Result<usize, AppError> {
self.conn().execute(sql, params).map_err(|e| e.into())
}
pub fn last_insert_rowid(&self) -> i64 {
self.conn().last_insert_rowid()
}
}