mirror of
https://github.com/YGGverse/Yoda.git
synced 2025-01-15 17:20:08 +00:00
draft profile db components
This commit is contained in:
parent
0dee053e36
commit
e84b9d4fb1
@ -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<RwLock<Connection>>,
|
||||
@ -8,14 +18,27 @@ pub struct Database {
|
||||
impl Database {
|
||||
// Constructors
|
||||
|
||||
/// Create new connected `Self`
|
||||
pub fn new(path: &Path) -> Self {
|
||||
Self {
|
||||
connection: match Connection::open(path) {
|
||||
// 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(())
|
||||
}
|
||||
|
24
src/profile/database/bookmark.rs
Normal file
24
src/profile/database/bookmark.rs
Normal file
@ -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<usize, Error> {
|
||||
tx.execute(
|
||||
"CREATE TABLE IF NOT EXISTS `bookmark`
|
||||
(
|
||||
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
`time` INTEGER NOT NULL,
|
||||
`data` TEXT
|
||||
)",
|
||||
[],
|
||||
)
|
||||
}
|
||||
}
|
26
src/profile/database/identity.rs
Normal file
26
src/profile/database/identity.rs
Normal file
@ -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<usize, Error> {
|
||||
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
|
||||
)",
|
||||
[],
|
||||
)
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user