Lot of thing

This commit is contained in:
Greg Burri 2020-05-20 17:25:56 +02:00
parent 3ebbe8172b
commit e2e54b8f43
12 changed files with 421 additions and 227 deletions

View file

@ -4,46 +4,94 @@ 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;
use super::consts;
const CURRENT_DB_VERSION: u32 = 1;
pub struct Connection {
con: rusqlite::Connection
//con: rusqlite::Connection
pool: Pool<SqliteConnectionManager>
}
pub struct Recipe {
pub title: String,
pub id: i32,
}
impl Connection {
pub fn new() -> Connection {
// TODO: use a constant in consts module.
let data_dir = Path::new("data");
let data_dir = Path::new(consts::DB_DIRECTORY);
if !data_dir.exists() {
fs::DirBuilder::new().create(data_dir).unwrap();
}
Connection { con: rusqlite::Connection::open(data_dir.join("recipes.sqlite")).unwrap() }
let manager = SqliteConnectionManager::file("file.db");
let pool = r2d2::Pool::new(manager).unwrap();
let connection = Connection { pool };
connection.create_or_update();
connection
}
pub fn create_or_update(self: &Self) -> rusqlite::Result<&str> {
//let connection = Connection::new();
fn create_or_update(self: &Self) {
// 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 con = self.pool.get().unwrap();
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
match
con.query_row(
"SELECT [name] FROM [sqlite_master] WHERE [type] = 'table' AND [name] = 'Version'",
rusqlite::NO_PARAMS,
|row| row.get::<usize, String>(0)
) {
Ok(_) => con.query_row("SELECT [version] FROM [Version]", rusqlite::NO_PARAMS, |row| row.get(0)).unwrap_or_default(),
Err(_) => 0
}
};
self.con.query_row(
"SELECT name FROM sqlite_master WHERE type='table' AND name='versions'",
rusqlite::NO_PARAMS,
|row| Ok(dbg!("test"))
)
match version {
0 => {
println!("Update to version 1...");
con.execute(
"
CREATE TABLE [Version] (
[id] INTEGER PRIMARY KEY,
[version] INTEGER NOT NULL,
[datetime] INTEGER DATETIME
)
",
rusqlite::NO_PARAMS
);
con.execute(
"
CREATE TABLE [Recipe] (
[id] INTEGER PRIMARY KEY,
[title] INTEGER NOT NULL,
[description] INTEGER DATETIME
)
",
rusqlite::NO_PARAMS
);
()
}
v =>
panic!("Unsupported database version: {}", v)
};
}
pub fn get_all_recipes() {