4 //use rusqlite::types::ToSql;
5 //use rusqlite::{Connection, Result, NO_PARAMS};
8 //extern crate r2d2_sqlite;
9 //extern crate rusqlite;
11 use r2d2_sqlite
::SqliteConnectionManager
;
17 const CURRENT_DB_VERSION
: u32 = 1;
19 pub struct Connection
{
20 //con: rusqlite::Connection
21 pool
: Pool
<SqliteConnectionManager
>
30 pub fn new() -> Connection
{
32 let data_dir
= Path
::new(consts
::DB_DIRECTORY
);
34 if !data_dir
.exists() {
35 fs
::DirBuilder
::new().create(data_dir
).unwrap();
38 let manager
= SqliteConnectionManager
::file("file.db");
39 let pool
= r2d2
::Pool
::new(manager
).unwrap();
41 let connection
= Connection
{ pool
};
42 connection
.create_or_update();
46 fn create_or_update(self: &Self) {
47 // let connection = Connection::new();
48 // let mut stmt = connection.sqlite_con.prepare("SELECT * FROM versions ORDER BY date").unwrap();
49 // let mut stmt = connection.sqlite_con..prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='versions'").unwrap();
51 // Check the Database version.
52 let con
= self.pool
.get().unwrap();
57 "SELECT [name] FROM [sqlite_master] WHERE [type] = 'table' AND [name] = 'Version'",
59 |row
| row
.get
::<usize, String
>(0)
62 Ok(_
) => con
.query_row("SELECT [version] FROM [Version]", rusqlite
::NO_PARAMS
, |row
| row
.get(0)).unwrap_or_default(),
69 println!("Update to version 1...");
72 CREATE TABLE [Version] (
73 [id] INTEGER PRIMARY KEY,
74 [version] INTEGER NOT NULL,
75 [datetime] INTEGER DATETIME
82 CREATE TABLE [Recipe] (
83 [id] INTEGER PRIMARY KEY,
84 [title] INTEGER NOT NULL,
85 [description] INTEGER DATETIME
93 panic!("Unsupported database version: {}", v
)
97 pub fn get_all_recipes() {