diff --git a/src/db/sqlite/db.rs b/src/db/sqlite/db.rs index 4444d88..09c6e51 100644 --- a/src/db/sqlite/db.rs +++ b/src/db/sqlite/db.rs @@ -2,30 +2,56 @@ use super::super::{ PixivDownloaderDb, PixivDownloaderDbConfig, PixivDownloaderDbError, PixivDownloaderSqliteConfig, }; use super::SqliteError; -use rusqlite::{Connection, OpenFlags}; -use std::sync::Mutex; +use futures_util::lock::Mutex; +use rusqlite::{Connection, OpenFlags, OptionalExtension}; +use std::collections::HashMap; + +const TAGS_TABLE: &'static str = "CREATE TABLE tags ( +id INT, +name TEXT, +PRIMARY KEY (id) +);"; +const VERSION_TABLE: &'static str = "CREATE TABLE version ( +id TEXT, +v1 INT, +v2 INT, +v3 INT, +v4 INT, +PRIMARY KEY (id) +);"; +const VERSION: [u8; 4] = [1, 0, 0, 0]; pub struct PixivDownloaderSqlite { db: Mutex, } impl PixivDownloaderSqlite { + /// Get all exists tables + async fn _get_exists_table(&self) -> Result, SqliteError> { + let con = self.db.lock().await; + let mut tables = HashMap::new(); + let mut stmt = con.prepare("SELECT name FROM main.sqlite_master WHERE type='table';")?; + let mut rows = stmt.query([])?; + while let Some(row) = rows.next()? { + tables.insert(row.get(0)?, ()); + } + Ok(tables) + } + fn _new(cfg: &PixivDownloaderSqliteConfig) -> Result { - let con = Connection::open_with_flags( + let db = Connection::open_with_flags( &cfg.path, OpenFlags::SQLITE_OPEN_READ_WRITE | OpenFlags::SQLITE_OPEN_FULL_MUTEX | OpenFlags::SQLITE_OPEN_CREATE | OpenFlags::SQLITE_OPEN_URI, )?; - Ok(Self { - db: Mutex::new(con), - }) + Ok(Self { db: Mutex::new(db) }) } } +#[async_trait] impl PixivDownloaderDb for PixivDownloaderSqlite { - #[allow(unreachable_patterns)] fn new + ?Sized>( cfg: &R, ) -> Result { @@ -37,4 +63,8 @@ impl PixivDownloaderDb for PixivDownloaderSqlite { _ => panic!("Config mismatched."), } } + + async fn init(&self) -> Result<(), PixivDownloaderDbError> { + Ok(()) + } } diff --git a/src/db/traits.rs b/src/db/traits.rs index 29136c8..07e8a07 100644 --- a/src/db/traits.rs +++ b/src/db/traits.rs @@ -1,6 +1,7 @@ use super::PixivDownloaderDbConfig; use super::PixivDownloaderDbError; +#[async_trait] pub trait PixivDownloaderDb { /// Create a new instance of database fn new + ?Sized>( @@ -8,4 +9,6 @@ pub trait PixivDownloaderDb { ) -> Result where Self: Sized + Send + Sync; + /// Initialize the database (create tables, migrate data, etc.) + async fn init(&self) -> Result<(), PixivDownloaderDbError>; }