From 87bc628b563f61185c4f9ec48627029b17d32bba Mon Sep 17 00:00:00 2001 From: =?utf8?q?Gr=C3=A9gory=20Burri?= Date: Thu, 29 Aug 2019 14:03:24 +0200 Subject: [PATCH] Beginning of data persistence module. --- .gitignore | 1 + backend/src/db.rs | 35 ++++++++++++++++++++++++++++++++++- backend/src/main.rs | 8 +++++++- 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 84a323e..ab8ddf5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ target **/*.rs.bk +backend/data/recipes.sqlite diff --git a/backend/src/db.rs b/backend/src/db.rs index 0002b41..f3356d2 100644 --- a/backend/src/db.rs +++ b/backend/src/db.rs @@ -1,6 +1,39 @@ +use std::path::Path; +use std::fs; +//use rusqlite::types::ToSql; +//use rusqlite::{Connection, Result, NO_PARAMS}; +const CURRENT_DB_VERSION: u32 = 1; -fn create_or_update() { +struct Connection { + pub sqlite_con : rusqlite::Connection +} +impl Connection { + fn new() -> Connection { + + // TODO: use a constant in consts module. + let data_dir = Path::new("data"); + + if !data_dir.exists() { + fs::DirBuilder::new().create(data_dir).unwrap(); + } + + Connection { sqlite_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(); } \ No newline at end of file diff --git a/backend/src/main.rs b/backend/src/main.rs index bc89ab8..fcb8e12 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -15,6 +15,7 @@ use std::{fs::File, env::args}; use itertools::Itertools; mod consts; +mod db; #[derive(Template)] #[template(path = "main.html")] @@ -61,6 +62,8 @@ fn main() -> std::io::Result<()> { println!("Configuration: {:?}", config); + let database_connection = db::create_or_update(); + let mut listenfd = ListenFd::from_env(); let mut server = HttpServer::new( @@ -86,7 +89,7 @@ fn main() -> std::io::Result<()> { fn process_args() -> bool { fn print_usage() { println!("Usage:"); - println!(" {} [--help]", get_exe_name()); + println!(" {} [--help] [--test]", get_exe_name()); } let args: Vec = args().collect(); @@ -94,6 +97,9 @@ fn process_args() -> bool { if args.iter().any(|arg| arg == "--help") { print_usage(); return true + } else if args.iter().any(|arg| arg == "--test") { + let database_connection = db::create_or_update(); + return true } false -- 2.45.2