X-Git-Url: http://git.euphorik.ch/?p=recipes.git;a=blobdiff_plain;f=backend%2Fsrc%2Fdb.rs;h=6458c7eb7308d384a695f7af38893d0c0e2fb821;hp=f3356d206eb8330027e7d8ab6271f370a3e7b52c;hb=eab43f8995eff5b8a4f6c4ded6a655866feddedb;hpb=240996a313ae1f81fa315693ac010dc4ddacc58b diff --git a/backend/src/db.rs b/backend/src/db.rs index f3356d2..6458c7e 100644 --- a/backend/src/db.rs +++ b/backend/src/db.rs @@ -6,12 +6,12 @@ use std::fs; const CURRENT_DB_VERSION: u32 = 1; -struct Connection { - pub sqlite_con : rusqlite::Connection +pub struct Connection { + con: rusqlite::Connection } impl Connection { - fn new() -> Connection { + pub fn new() -> Connection { // TODO: use a constant in consts module. let data_dir = Path::new("data"); @@ -20,20 +20,29 @@ impl Connection { fs::DirBuilder::new().create(data_dir).unwrap(); } - Connection { sqlite_con : rusqlite::Connection::open(data_dir.join("recipes.sqlite")).unwrap() } + Connection { con: rusqlite::Connection::open(data_dir.join("recipes.sqlite")).unwrap() } } -} - -pub fn create_or_update() { - 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(); - connection.sqlite_con.query_row( - "SELECT name FROM sqlite_master WHERE type='table' AND name='versions'", - rusqlite::NO_PARAMS, - |row| Ok(dbg!("test")) - ) - .unwrap(); + pub fn create_or_update(self: &Self) -> rusqlite::Result<&str> { + //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 version = { + let stmt_version_table = self.con.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='versions'")?; + /*if stmt_version_table.query(rusqlite::NO_PARAMS)?.count() == 0 { + 0 + } else { + 1 // let stmt_versions = self.con.prepare("SELECT number FROM [") + }*/ + 0 + }; + + self.con.query_row( + "SELECT name FROM sqlite_master WHERE type='table' AND name='versions'", + rusqlite::NO_PARAMS, + |row| Ok(dbg!("test")) + ) + } } \ No newline at end of file