mirror of
https://github.com/YGGverse/Yoda.git
synced 2026-04-01 00:55:28 +00:00
move profile components to separated namespace
This commit is contained in:
parent
61010e0a42
commit
b12f5e8b8f
13 changed files with 332 additions and 214 deletions
18
src/profile/bookmark.rs
Normal file
18
src/profile/bookmark.rs
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
mod database;
|
||||
|
||||
use sqlite::Transaction;
|
||||
|
||||
// Tools
|
||||
|
||||
pub fn migrate(tx: &Transaction) -> Result<(), String> {
|
||||
// Migrate self components
|
||||
if let Err(e) = database::init(tx) {
|
||||
return Err(e.to_string());
|
||||
}
|
||||
|
||||
// Delegate migration to childs
|
||||
// nothing yet..
|
||||
|
||||
// Success
|
||||
Ok(())
|
||||
}
|
||||
81
src/profile/bookmark/database.rs
Normal file
81
src/profile/bookmark/database.rs
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
use gtk::glib::DateTime;
|
||||
use sqlite::{Error, Transaction};
|
||||
|
||||
pub struct Table {
|
||||
pub id: i64,
|
||||
pub profile_id: i64,
|
||||
pub time: DateTime,
|
||||
pub request: String,
|
||||
}
|
||||
|
||||
pub struct Bookmark {
|
||||
// nothing yet..
|
||||
}
|
||||
|
||||
pub fn init(tx: &Transaction) -> Result<usize, Error> {
|
||||
tx.execute(
|
||||
"CREATE TABLE IF NOT EXISTS `profile_bookmark`
|
||||
(
|
||||
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
`profile_id` INTEGER NOT NULL,
|
||||
`time` INTEGER NOT NULL,
|
||||
`request` TEXT NOT NULL
|
||||
)",
|
||||
[],
|
||||
)
|
||||
}
|
||||
|
||||
pub fn add(
|
||||
tx: &Transaction,
|
||||
profile_id: &i64,
|
||||
time: &DateTime,
|
||||
request: &str,
|
||||
) -> Result<usize, Error> {
|
||||
tx.execute(
|
||||
"INSERT INTO `profile_bookmark` (
|
||||
`profile_id`,
|
||||
`time`,
|
||||
`request`
|
||||
) VALUES (?, ?, ?)",
|
||||
(profile_id, time.to_unix(), request),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn records(
|
||||
tx: &Transaction,
|
||||
profile_id: &i64,
|
||||
request: Option<&str>,
|
||||
) -> Result<Vec<Table>, Error> {
|
||||
let mut stmt = tx.prepare(
|
||||
"SELECT `id`, `profile_id`, `time`, `request`
|
||||
FROM `profile_bookmark`
|
||||
WHERE `profile_id` = ? AND `request` LIKE ?",
|
||||
)?;
|
||||
|
||||
let filter = match request {
|
||||
Some(value) => value,
|
||||
None => "%",
|
||||
};
|
||||
|
||||
let result = stmt.query_map((profile_id, filter), |row| {
|
||||
Ok(Table {
|
||||
id: row.get(0)?,
|
||||
profile_id: row.get(1)?,
|
||||
time: DateTime::from_unix_local(row.get(2)?).unwrap(),
|
||||
request: row.get(3)?,
|
||||
})
|
||||
})?;
|
||||
|
||||
let mut records = Vec::new();
|
||||
|
||||
for record in result {
|
||||
let table = record?;
|
||||
records.push(table);
|
||||
}
|
||||
|
||||
Ok(records)
|
||||
}
|
||||
|
||||
pub fn delete(tx: &Transaction, id: &i64) -> Result<usize, Error> {
|
||||
tx.execute("DELETE FROM `profile_bookmark` WHERE `id` = ?", [id])
|
||||
}
|
||||
|
|
@ -1,63 +1,41 @@
|
|||
mod bookmark;
|
||||
mod history;
|
||||
mod identity;
|
||||
use sqlite::{Error, Transaction};
|
||||
|
||||
use sqlite::{Connection, Error};
|
||||
use std::{
|
||||
path::Path,
|
||||
rc::Rc,
|
||||
sync::{RwLock, RwLockWriteGuard},
|
||||
};
|
||||
|
||||
pub struct Database {
|
||||
connection: Rc<RwLock<Connection>>,
|
||||
pub struct Table {
|
||||
pub id: i64,
|
||||
}
|
||||
|
||||
impl Database {
|
||||
// Constructors
|
||||
pub fn init(tx: &Transaction) -> Result<usize, Error> {
|
||||
tx.execute(
|
||||
"CREATE TABLE IF NOT EXISTS `profile`
|
||||
(
|
||||
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL
|
||||
)",
|
||||
[],
|
||||
)
|
||||
}
|
||||
|
||||
/// Create new connected `Self`
|
||||
pub fn new(path: &Path) -> Self {
|
||||
// Init database connection
|
||||
let connection = match Connection::open(path) {
|
||||
Ok(connection) => Rc::new(RwLock::new(connection)),
|
||||
Err(reason) => panic!("{reason}"),
|
||||
};
|
||||
pub fn add(tx: &Transaction) -> Result<usize, Error> {
|
||||
tx.execute("INSERT INTO `profile` DEFAULT VALUES", [])
|
||||
}
|
||||
|
||||
// Init profile components
|
||||
match connection.write() {
|
||||
Ok(writable) => {
|
||||
if let Err(reason) = init(writable) {
|
||||
panic!("{reason}")
|
||||
}
|
||||
}
|
||||
Err(reason) => panic!("{reason}"),
|
||||
};
|
||||
pub fn records(tx: &Transaction) -> Result<Vec<Table>, Error> {
|
||||
let mut stmt = tx.prepare("SELECT `profile_id` FROM `profile`")?;
|
||||
let result = stmt.query_map([], |row| Ok(Table { id: row.get(0)? }))?;
|
||||
|
||||
// Result
|
||||
Self { connection }
|
||||
let mut records = Vec::new();
|
||||
|
||||
for record in result {
|
||||
let table = record?;
|
||||
records.push(table);
|
||||
}
|
||||
|
||||
// Getters
|
||||
|
||||
pub fn connection(&self) -> &Rc<RwLock<Connection>> {
|
||||
&self.connection
|
||||
}
|
||||
Ok(records)
|
||||
}
|
||||
|
||||
// Tools
|
||||
|
||||
fn init(mut connection: RwLockWriteGuard<'_, Connection>) -> Result<(), Error> {
|
||||
// Begin transaction
|
||||
let transaction = connection.transaction()?;
|
||||
|
||||
// Init profile components
|
||||
bookmark::init(&transaction)?;
|
||||
history::init(&transaction)?;
|
||||
identity::init(&transaction)?;
|
||||
|
||||
// Apply changes
|
||||
transaction.commit()?;
|
||||
|
||||
Ok(())
|
||||
pub fn delete(tx: &Transaction, id: &i64) -> Result<usize, Error> {
|
||||
tx.execute("DELETE FROM `profile` WHERE `id` = ?", [id])
|
||||
}
|
||||
|
||||
pub fn last_insert_id(tx: &Transaction) -> i64 {
|
||||
tx.last_insert_rowid()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,65 +0,0 @@
|
|||
use gtk::glib::DateTime;
|
||||
use sqlite::{Error, Transaction};
|
||||
|
||||
pub struct Table {
|
||||
pub id: i64,
|
||||
pub time: DateTime,
|
||||
pub request: String,
|
||||
}
|
||||
|
||||
pub struct Bookmark {
|
||||
// nothing yet..
|
||||
}
|
||||
|
||||
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,
|
||||
`request` TEXT NOT NULL
|
||||
)",
|
||||
[],
|
||||
)
|
||||
}
|
||||
|
||||
pub fn add(tx: &Transaction, time: &DateTime, request: &str) -> Result<usize, Error> {
|
||||
tx.execute(
|
||||
"INSERT INTO `bookmark` (
|
||||
`time`,
|
||||
`request`
|
||||
) VALUES (?, ?)",
|
||||
(time.to_unix(), request),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn records(tx: &Transaction, request: Option<&str>) -> Result<Vec<Table>, Error> {
|
||||
let mut stmt =
|
||||
tx.prepare("SELECT `id`, `time`, `request` FROM `bookmark` WHERE `request` LIKE ?")?;
|
||||
|
||||
let filter = match request {
|
||||
Some(value) => value,
|
||||
None => "%",
|
||||
};
|
||||
|
||||
let result = stmt.query_map([filter], |row| {
|
||||
Ok(Table {
|
||||
id: row.get(0)?,
|
||||
time: DateTime::from_unix_local(row.get(1)?).unwrap(),
|
||||
request: row.get(2)?,
|
||||
})
|
||||
})?;
|
||||
|
||||
let mut records = Vec::new();
|
||||
|
||||
for record in result {
|
||||
let table = record?;
|
||||
records.push(table);
|
||||
}
|
||||
|
||||
Ok(records)
|
||||
}
|
||||
|
||||
pub fn delete(tx: &Transaction, id: &i64) -> Result<usize, Error> {
|
||||
tx.execute("DELETE FROM `bookmark` WHERE `id` = ?", [id])
|
||||
}
|
||||
|
|
@ -1,65 +0,0 @@
|
|||
use gtk::glib::DateTime;
|
||||
use sqlite::{Error, Transaction};
|
||||
|
||||
pub struct Table {
|
||||
pub id: i64,
|
||||
pub time: DateTime,
|
||||
pub request: String,
|
||||
}
|
||||
|
||||
pub struct History {
|
||||
// nothing yet..
|
||||
}
|
||||
|
||||
pub fn init(tx: &Transaction) -> Result<usize, Error> {
|
||||
tx.execute(
|
||||
"CREATE TABLE IF NOT EXISTS `history`
|
||||
(
|
||||
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
`time` INTEGER NOT NULL,
|
||||
`request` TEXT NOT NULL
|
||||
)",
|
||||
[],
|
||||
)
|
||||
}
|
||||
|
||||
pub fn add(tx: &Transaction, time: &DateTime, request: &str) -> Result<usize, Error> {
|
||||
tx.execute(
|
||||
"INSERT INTO `history` (
|
||||
`time`,
|
||||
`request`
|
||||
) VALUES (?, ?)",
|
||||
(time.to_unix(), request),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn records(tx: &Transaction, request: Option<&str>) -> Result<Vec<Table>, Error> {
|
||||
let mut stmt =
|
||||
tx.prepare("SELECT `id`, `time`, `request` FROM `history` WHERE `request` LIKE ?")?;
|
||||
|
||||
let filter = match request {
|
||||
Some(value) => value,
|
||||
None => "%",
|
||||
};
|
||||
|
||||
let result = stmt.query_map([filter], |row| {
|
||||
Ok(Table {
|
||||
id: row.get(0)?,
|
||||
time: DateTime::from_unix_local(row.get(1)?).unwrap(),
|
||||
request: row.get(2)?,
|
||||
})
|
||||
})?;
|
||||
|
||||
let mut records = Vec::new();
|
||||
|
||||
for record in result {
|
||||
let table = record?;
|
||||
records.push(table);
|
||||
}
|
||||
|
||||
Ok(records)
|
||||
}
|
||||
|
||||
pub fn delete(tx: &Transaction, id: &i64) -> Result<usize, Error> {
|
||||
tx.execute("DELETE FROM `history` WHERE `id` = ?", [id])
|
||||
}
|
||||
|
|
@ -1,20 +0,0 @@
|
|||
use sqlite::{Error, Transaction};
|
||||
|
||||
pub struct Table {
|
||||
pub id: i64,
|
||||
// pub app_id: i64,
|
||||
}
|
||||
|
||||
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
|
||||
)",
|
||||
[],
|
||||
)
|
||||
}
|
||||
18
src/profile/history.rs
Normal file
18
src/profile/history.rs
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
mod database;
|
||||
|
||||
use sqlite::Transaction;
|
||||
|
||||
// Tools
|
||||
|
||||
pub fn migrate(tx: &Transaction) -> Result<(), String> {
|
||||
// Migrate self components
|
||||
if let Err(e) = database::init(tx) {
|
||||
return Err(e.to_string());
|
||||
}
|
||||
|
||||
// Delegate migration to childs
|
||||
// nothing yet..
|
||||
|
||||
// Success
|
||||
Ok(())
|
||||
}
|
||||
81
src/profile/history/database.rs
Normal file
81
src/profile/history/database.rs
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
use gtk::glib::DateTime;
|
||||
use sqlite::{Error, Transaction};
|
||||
|
||||
pub struct Table {
|
||||
pub id: i64,
|
||||
pub profile_id: i64,
|
||||
pub time: DateTime,
|
||||
pub request: String,
|
||||
}
|
||||
|
||||
pub struct History {
|
||||
// nothing yet..
|
||||
}
|
||||
|
||||
pub fn init(tx: &Transaction) -> Result<usize, Error> {
|
||||
tx.execute(
|
||||
"CREATE TABLE IF NOT EXISTS `profile_history`
|
||||
(
|
||||
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
`profile_id` INTEGER NOT NULL,
|
||||
`time` INTEGER NOT NULL,
|
||||
`request` TEXT NOT NULL
|
||||
)",
|
||||
[],
|
||||
)
|
||||
}
|
||||
|
||||
pub fn add(
|
||||
tx: &Transaction,
|
||||
profile_id: &i64,
|
||||
time: &DateTime,
|
||||
request: &str,
|
||||
) -> Result<usize, Error> {
|
||||
tx.execute(
|
||||
"INSERT INTO `history` (
|
||||
`profile_id`,
|
||||
`time`,
|
||||
`request`
|
||||
) VALUES (?, ?, ?)",
|
||||
(profile_id, time.to_unix(), request),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn records(
|
||||
tx: &Transaction,
|
||||
profile_id: &i64,
|
||||
request: Option<&str>,
|
||||
) -> Result<Vec<Table>, Error> {
|
||||
let mut stmt = tx.prepare(
|
||||
"SELECT `id`, `profile_id`, `time`, `request`
|
||||
FROM `profile_history`
|
||||
WHERE `profile_id` = ? AND `request` LIKE ?",
|
||||
)?;
|
||||
|
||||
let filter = match request {
|
||||
Some(value) => value,
|
||||
None => "%",
|
||||
};
|
||||
|
||||
let result = stmt.query_map((profile_id, filter), |row| {
|
||||
Ok(Table {
|
||||
id: row.get(0)?,
|
||||
profile_id: row.get(1)?,
|
||||
time: DateTime::from_unix_local(row.get(2)?).unwrap(),
|
||||
request: row.get(3)?,
|
||||
})
|
||||
})?;
|
||||
|
||||
let mut records = Vec::new();
|
||||
|
||||
for record in result {
|
||||
let table = record?;
|
||||
records.push(table);
|
||||
}
|
||||
|
||||
Ok(records)
|
||||
}
|
||||
|
||||
pub fn delete(tx: &Transaction, id: &i64) -> Result<usize, Error> {
|
||||
tx.execute("DELETE FROM `profile_history` WHERE `id` = ?", [id])
|
||||
}
|
||||
18
src/profile/identity.rs
Normal file
18
src/profile/identity.rs
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
mod database;
|
||||
|
||||
use sqlite::Transaction;
|
||||
|
||||
// Tools
|
||||
|
||||
pub fn migrate(tx: &Transaction) -> Result<(), String> {
|
||||
// Migrate self components
|
||||
if let Err(e) = database::init(tx) {
|
||||
return Err(e.to_string());
|
||||
}
|
||||
|
||||
// Delegate migration to childs
|
||||
// nothing yet..
|
||||
|
||||
// Success
|
||||
Ok(())
|
||||
}
|
||||
21
src/profile/identity/database.rs
Normal file
21
src/profile/identity/database.rs
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
use sqlite::{Error, Transaction};
|
||||
|
||||
pub struct Table {
|
||||
pub id: i64,
|
||||
//pub profile_id: i64,
|
||||
}
|
||||
|
||||
pub fn init(tx: &Transaction) -> Result<usize, Error> {
|
||||
tx.execute(
|
||||
"CREATE TABLE IF NOT EXISTS `profile_identity`
|
||||
(
|
||||
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
`profile_id` INTEGER NOT NULL,
|
||||
`time` INTEGER NOT NULL,
|
||||
`name` VARCHAR(255),
|
||||
`certificate` TEXT NOT NULL,
|
||||
`key` TEXT NOT NULL
|
||||
)",
|
||||
[],
|
||||
)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue