Beginning of data persistence module.
[recipes.git] / backend / src / db.rs
index 0002b41..f3356d2 100644 (file)
@@ -1,6 +1,39 @@
+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
+struct Connection {\r
+    pub sqlite_con : rusqlite::Connection\r
+}\r
 \r
+impl Connection {\r
+    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 { sqlite_con : rusqlite::Connection::open(data_dir.join("recipes.sqlite")).unwrap() }\r
+    }\r
+}\r
+\r
+pub fn create_or_update() {\r
+    let connection = Connection::new();\r
+\r
+    // let mut stmt = connection.sqlite_con.prepare("SELECT * FROM versions ORDER BY date").unwrap();\r
+\r
+    //let mut stmt = connection.sqlite_con..prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='versions'").unwrap();\r
+    connection.sqlite_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
+    .unwrap();\r
 }
\ No newline at end of file