From e84b9d4fb14e21f513494f270a27d2d3c72956d7 Mon Sep 17 00:00:00 2001 From: yggverse Date: Tue, 12 Nov 2024 12:30:12 +0200 Subject: [PATCH] draft profile db components --- src/profile/database.rs | 55 +++++++++++++++++++++++++++----- src/profile/database/bookmark.rs | 24 ++++++++++++++ src/profile/database/identity.rs | 26 +++++++++++++++ 3 files changed, 97 insertions(+), 8 deletions(-) create mode 100644 src/profile/database/bookmark.rs create mode 100644 src/profile/database/identity.rs diff --git a/src/profile/database.rs b/src/profile/database.rs index 9f4cbddc..d369d0ee 100644 --- a/src/profile/database.rs +++ b/src/profile/database.rs @@ -1,5 +1,15 @@ -use sqlite::Connection; -use std::{path::Path, rc::Rc, sync::RwLock}; +mod bookmark; +mod identity; + +use bookmark::Bookmark; +use identity::Identity; + +use sqlite::{Connection, Error}; +use std::{ + path::Path, + rc::Rc, + sync::{RwLock, RwLockWriteGuard}, +}; pub struct Database { connection: Rc>, @@ -8,13 +18,26 @@ pub struct Database { impl Database { // Constructors + /// Create new connected `Self` pub fn new(path: &Path) -> Self { - Self { - connection: match Connection::open(path) { - Ok(connection) => Rc::new(RwLock::new(connection)), - Err(reason) => panic!("{reason}"), - }, - } + // Init database connection + let connection = match Connection::open(path) { + Ok(connection) => Rc::new(RwLock::new(connection)), + Err(reason) => panic!("{reason}"), + }; + + // Init profile components + match connection.try_write() { + Ok(writable) => { + if let Err(reason) = init(writable) { + panic!("{reason}") + } + } + Err(reason) => panic!("{reason}"), + }; + + // Result + Self { connection } } // Getters @@ -23,3 +46,19 @@ impl Database { &self.connection } } + +// Tools + +fn init(mut connection: RwLockWriteGuard<'_, Connection>) -> Result<(), Error> { + // Create transaction + let transaction = connection.transaction()?; + + // Init profile components + Bookmark::init(&transaction)?; + Identity::init(&transaction)?; + + // Apply changes + transaction.commit()?; + + Ok(()) +} diff --git a/src/profile/database/bookmark.rs b/src/profile/database/bookmark.rs new file mode 100644 index 00000000..4f60e481 --- /dev/null +++ b/src/profile/database/bookmark.rs @@ -0,0 +1,24 @@ +use sqlite::{Error, Transaction}; + +pub struct Table { + pub id: i64, + // pub app_id: i64, not in use +} + +pub struct Bookmark { + // nothing yet.. +} + +impl Bookmark { + pub fn init(tx: &Transaction) -> Result { + tx.execute( + "CREATE TABLE IF NOT EXISTS `bookmark` + ( + `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, + `time` INTEGER NOT NULL, + `data` TEXT + )", + [], + ) + } +} diff --git a/src/profile/database/identity.rs b/src/profile/database/identity.rs new file mode 100644 index 00000000..1e9aee2a --- /dev/null +++ b/src/profile/database/identity.rs @@ -0,0 +1,26 @@ +use sqlite::{Error, Transaction}; + +pub struct Table { + pub id: i64, + // pub app_id: i64, not in use +} + +pub struct Identity { + // nothing yet.. +} + +impl Identity { + pub fn init(tx: &Transaction) -> Result { + tx.execute( + "CREATE TABLE IF NOT EXISTS `identity` + ( + `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, + `time` INTEGER NOT NULL, + `name` VARCHAR(255), + `crt` TEXT NOT NULL, + `key` TEXT NOT NULL + )", + [], + ) + } +}