Update dependencies (actix-web 2)
[recipes.git] / backend / src / db.rs
index 0002b41..6458c7e 100644 (file)
@@ -1,6 +1,48 @@
+use std::path::Path;\r
+use std::fs;\r
 \r
+//use rusqlite::types::ToSql;\r
+//use rusqlite::{Connection, Result, NO_PARAMS};\r
 \r
+const CURRENT_DB_VERSION: u32 = 1;\r
 \r
-fn create_or_update() {\r
+pub struct Connection {\r
+    con: rusqlite::Connection\r
+}\r
 \r
+impl Connection {\r
+    pub fn new() -> Connection {\r
+\r
+        // TODO: use a constant in consts module.\r
+        let data_dir = Path::new("data");\r
+\r
+        if !data_dir.exists() {\r
+            fs::DirBuilder::new().create(data_dir).unwrap();\r
+        }\r
+\r
+        Connection { con: rusqlite::Connection::open(data_dir.join("recipes.sqlite")).unwrap() }\r
+    }\r
+\r
+    pub fn create_or_update(self: &Self) -> rusqlite::Result<&str> {\r
+        //let connection = Connection::new();\r
+        // let mut stmt = connection.sqlite_con.prepare("SELECT * FROM versions ORDER BY date").unwrap();\r
+        // let mut stmt = connection.sqlite_con..prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='versions'").unwrap();\r
+\r
+        // Check the Database version.\r
+        let version = {\r
+            let stmt_version_table = self.con.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='versions'")?;\r
+            /*if stmt_version_table.query(rusqlite::NO_PARAMS)?.count() == 0 {\r
+                0\r
+            } else {\r
+                1 // let stmt_versions = self.con.prepare("SELECT number FROM [")\r
+            }*/\r
+            0\r
+        };\r
+\r
+        self.con.query_row(\r
+            "SELECT name FROM sqlite_master WHERE type='table' AND name='versions'",\r
+            rusqlite::NO_PARAMS,\r
+            |row| Ok(dbg!("test"))\r
+        )\r
+    }\r
 }
\ No newline at end of file