2bae4fd59c0599fc166ee6182171325ed7c96c18
4 use r2d2_sqlite
::SqliteConnectionManager
;
9 const CURRENT_DB_VERSION
: u32 = 1;
11 pub struct Connection
{
12 //con: rusqlite::Connection
13 pool
: Pool
<SqliteConnectionManager
>
22 pub fn new() -> Connection
{
24 let data_dir
= Path
::new(consts
::DB_DIRECTORY
);
26 if !data_dir
.exists() {
27 fs
::DirBuilder
::new().create(data_dir
).unwrap();
30 let manager
= SqliteConnectionManager
::file(consts
::DB_FILENAME
);
31 let pool
= r2d2
::Pool
::new(manager
).unwrap();
33 let connection
= Connection
{ pool
};
34 connection
.create_or_update();
38 fn create_or_update(self: &Self) {
39 // let connection = Connection::new();
40 // let mut stmt = connection.sqlite_con.prepare("SELECT * FROM versions ORDER BY date").unwrap();
41 // let mut stmt = connection.sqlite_con..prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='versions'").unwrap();
43 // Check the Database version.
44 let con
= self.pool
.get().unwrap();
49 "SELECT [name] FROM [sqlite_master] WHERE [type] = 'table' AND [name] = 'Version'",
51 |row
| row
.get
::<usize, String
>(0)
53 Ok(_
) => con
.query_row("SELECT [version] FROM [Version]", rusqlite
::NO_PARAMS
, |row
| row
.get(0)).unwrap_or_default(),
60 println!("Update to version 1...");
63 CREATE TABLE [Version] (
64 [id] INTEGER PRIMARY KEY,
65 [version] INTEGER NOT NULL,
66 [datetime] INTEGER DATETIME
73 CREATE TABLE [SetLetterCommand] (
74 [id] INTEGER PRIMARY KEY,
75 [pos_i] INTEGER NOT NULL,
76 [pos_j] INTEGER NOT NULL,
78 [letter] INTEGER NOT NULL,
86 panic!("Unsupported database version: {}", v
)
90 pub fn insert_set_letter_command() {