From 490338930de2c559cdf3d84f4c690013a8499f79 Mon Sep 17 00:00:00 2001 From: yggverse Date: Wed, 2 Oct 2024 20:00:08 +0300 Subject: [PATCH] change sqlite wrapper to rusqlite, update database api --- Cargo.toml | 5 +++-- src/app.rs | 4 ++-- src/app/database.rs | 25 +++++++++++-------------- src/main.rs | 5 +++-- 4 files changed, 19 insertions(+), 20 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e7c288cc..de85fbe6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,8 +10,9 @@ categories = ["network-programming", "gui"] repository = "https://github.com/YGGverse/Yoda/tree/Rust-GTK4" # homepage = "https://yggverse.github.io" -[dependencies] -sqlite = "0.36.1" +[dependencies.sqlite] +package = "rusqlite" +version = "0.32.1" [dependencies.gtk] package = "gtk4" diff --git a/src/app.rs b/src/app.rs index c312fdda..3a2335fd 100644 --- a/src/app.rs +++ b/src/app.rs @@ -75,10 +75,10 @@ impl App { // Init events app.connect_activate({ - let database = database.clone(); + // let database = database.clone(); move |application| { // Restore previous session - database.restore(); + // @TODO // Init components let browser = Arc::new(Browser::new( diff --git a/src/app/database.rs b/src/app/database.rs index 366fa240..435aa229 100644 --- a/src/app/database.rs +++ b/src/app/database.rs @@ -10,13 +10,12 @@ impl Database { pub fn init(connection: Arc) -> Database { // Init app table if let Err(error) = connection.execute( - r" - CREATE TABLE IF NOT EXISTS `app` - ( - `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, - `time` INTEGER NOT NULL DEFAULT CURRENT_TIMESTAMP - ) - ", + "CREATE TABLE IF NOT EXISTS `app` + ( + `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, + `time` INTEGER NOT NULL DEFAULT CURRENT_TIMESTAMP + )", + [], ) { panic!("{error}"); } @@ -25,13 +24,11 @@ impl Database { Self { connection } } - // Restore previous browser session from DB - pub fn restore(&self) { - // @TODO migration test - } + pub fn add(&self) -> i64 { + if let Err(error) = self.connection.execute("INSERT INTO `app`", []) { + panic!("{error}"); + } - // Save browser session to DB - pub fn save(&self) { - // @TODO migration test + self.connection.last_insert_rowid() } } diff --git a/src/main.rs b/src/main.rs index 5fa594ee..1b42af0c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ mod app; use app::App; use gtk::glib::{user_config_dir, ExitCode}; +use sqlite::Connection; use std::{fs::create_dir_all, sync::Arc}; fn main() -> ExitCode { @@ -20,8 +21,8 @@ fn main() -> ExitCode { profile_database_path.push("database.sqlite3"); // Init database connection - let profile_database_connection = match sqlite::open(profile_database_path) { - Ok(profile_database_connection) => Arc::new(profile_database_connection), + let profile_database_connection = match Connection::open(profile_database_path) { + Ok(connection) => Arc::new(connection), Err(error) => panic!("Failed to connect profile database: {error}"), };