X-Git-Url: http://git.euphorik.ch/?a=blobdiff_plain;f=backend%2Fsrc%2Fdb.rs;h=6458c7eb7308d384a695f7af38893d0c0e2fb821;hb=HEAD;hp=ac97f1b14639db17377415e1e736c773810a4788;hpb=a080d19cb9076780db9e86325b2ab617886e2a5f;p=recipes.git diff --git a/backend/src/db.rs b/backend/src/db.rs deleted file mode 100644 index ac97f1b..0000000 --- a/backend/src/db.rs +++ /dev/null @@ -1,142 +0,0 @@ -use std::path::Path; -use std::fs; - -//use rusqlite::types::ToSql; -//use rusqlite::{Connection, Result, NO_PARAMS}; - -//extern crate r2d2; -//extern crate r2d2_sqlite; -//extern crate rusqlite; - -use r2d2_sqlite::SqliteConnectionManager; -use r2d2::Pool; - -#[derive(Debug)] -pub enum DbError { - SqliteError(rusqlite::Error), - R2d2Error(r2d2::Error), - UnsupportedVersion(i32), -} - -use super::consts; - -const CURRENT_DB_VERSION: u32 = 1; - -pub struct Connection { - //con: rusqlite::Connection - pool: Pool -} - -pub struct Recipe { - pub title: String, - pub id: i32, -} - -impl std::convert::From for DbError { - fn from(error: rusqlite::Error) -> Self { - DbError::SqliteError(error) - } -} - -impl std::convert::From for DbError { - fn from(error: r2d2::Error) -> Self { - DbError::R2d2Error(error) - } -} - -impl Connection { - pub fn new() -> Result { - - let data_dir = Path::new(consts::DB_DIRECTORY); - - if !data_dir.exists() { - fs::DirBuilder::new().create(data_dir).unwrap(); - } - - let manager = SqliteConnectionManager::file("file.db"); - let pool = r2d2::Pool::new(manager).unwrap(); - - let connection = Connection { pool }; - connection.create_or_update()?; - Ok(connection) - } - - /* - * Called after the connection has been established for creating or updating the database. - * The 'Version' table tracks the current state of the database. - */ - fn create_or_update(self: &Self) -> Result<(), DbError> { - // let connection = Connection::new(); - // let mut stmt = connection.sqlite_con.prepare("SELECT * FROM versions ORDER BY date").unwrap(); - // let mut stmt = connection.sqlite_con.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='versions'").unwrap(); - - // Check the Database version. - let mut con = self.pool.get()?; - let tx = con.transaction()?; - - let mut version = { - match tx.query_row( - "SELECT [name] FROM [sqlite_master] WHERE [type] = 'table' AND [name] = 'Version'", - rusqlite::NO_PARAMS, - |row| row.get::(0) - ) { - Ok(_) => tx.query_row("SELECT [version] FROM [Version]", rusqlite::NO_PARAMS, |row| row.get(0)).unwrap_or_default(), - Err(_) => 0 - } - }; - - while Connection::update_to_next_version(version, &tx)? { - version += 1; - } - - tx.commit()?; - - Ok(()) - } - - fn update_to_next_version(version: i32, tx: &rusqlite::Transaction) -> Result { - match version { - 0 => { - println!("Update to version 1..."); - - // Initial structure. - tx.execute_batch( - " - CREATE TABLE [Version] ( - [id] INTEGER PRIMARY KEY, - [version] INTEGER NOT NULL UNIQUE, - [datetime] INTEGER DATETIME - ); - - CREATE TABLE [Recipe] ( - [id] INTEGER PRIMARY KEY, - [title] INTEGER NOT NULL, - [description] INTEGER DATETIME - ); - " - )?; - - /* - tx.execute( - " - INSERT INTO Version - ", - rusqlite::NO_PARAMS - );*/ - - Ok(true) - } - - // Current version. - 1 => - Ok(false), - - v => - Err(DbError::UnsupportedVersion(v)), - } - } - - pub fn get_all_recipes() { - - } -} \ No newline at end of file