From 3dcf1d592133e485e10bf2465868d645321a8404 Mon Sep 17 00:00:00 2001 From: jakka Date: Wed, 2 Jul 2025 14:00:12 +0300 Subject: supposedly finished implementing everything --- src/db.rs | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) (limited to 'src/db.rs') diff --git a/src/db.rs b/src/db.rs index fba81c5..be1b827 100644 --- a/src/db.rs +++ b/src/db.rs @@ -1,14 +1,14 @@ use anyhow::{Result, anyhow}; use directories::BaseDirs; +use libsql::{Builder, Connection, params}; use std::{ - path::Path, + path::{Path, PathBuf}, time::{Duration, UNIX_EPOCH}, }; -use turso::{Builder, Connection, Rows, params}; use crate::flac::{CURRENT_VENDOR, get_vendor}; -const TABLE_CREATE: &str = "CREATE TABLE IF NOT EXISTS flacs (path TEXT PRIMARY KEY, toencode BOOLEAN NOT NULL, modtime INTEGER)"; +const TABLE_CREATE: &str = "CREATE TABLE IF NOT EXISTS flacs (path TEXT PRIMARY KEY UNIQUE, toencode BOOLEAN NOT NULL, modtime INTEGER)"; const ADD_NEW_ITEM: &str = "INSERT INTO flacs (path, toencode, modtime) VALUES (?1, ?2, ?3)"; const REPLACE_ITEM: &str = "REPLACE INTO flacs (path, toencode, modtime) VALUES (?1, ?2, ?3)"; const TOENCODE_QUERY: &str = "SELECT path FROM flacs WHERE toencode"; @@ -80,7 +80,7 @@ impl Database { .next() .await? { - Ok(matches!(row.get_value(0)?, turso::Value::Integer(1))) + Ok(matches!(row.get_value(0)?, libsql::Value::Integer(1))) } else { Err(anyhow!("database error")) } @@ -104,9 +104,14 @@ impl Database { } } - pub async fn init_clean_files(&self) -> Result { + pub async fn init_clean_files(&self) -> Result, libsql::Error> { self.0.execute(DEDUPE_DB, ()).await?; - self.0.query(FETCH_FILES, ()).await + let mut rows = self.0.query(FETCH_FILES, ()).await?; + let mut files = Vec::new(); + while let Ok(Some(row)) = rows.next().await { + files.push(PathBuf::from(row.get_value(0)?.as_text().unwrap())) + } + Ok(files) } pub async fn remove_file(&self, filename: impl AsRef) -> Result<()> { @@ -116,8 +121,13 @@ impl Database { Ok(()) } - pub async fn get_toencode_files(&self) -> Result { - self.0.query(TOENCODE_QUERY, ()).await + pub async fn get_toencode_files(&self) -> Result, libsql::Error> { + let mut rows = self.0.query(TOENCODE_QUERY, ()).await?; + let mut files = Vec::new(); + while let Ok(Some(row)) = rows.next().await { + files.push(PathBuf::from(row.get_value(0)?.as_text().unwrap())) + } + Ok(files) } pub async fn get_toencode_number(&self) -> Result { -- cgit v1.3.1