create identity table to normalize children relationships

This commit is contained in:
yggverse 2024-11-16 16:26:12 +02:00
parent 3348e30d80
commit 9530c37c59
4 changed files with 204 additions and 16 deletions

View file

@ -3,32 +3,32 @@ use std::{rc::Rc, sync::RwLock};
pub struct Table {
pub id: i64,
//pub profile_id: i64,
//pub profile_identity_id: i64,
pub pem: String,
}
/// Storage for Gemini auth certificates
pub struct Database {
connection: Rc<RwLock<Connection>>,
profile_id: Rc<i64>, // multi-profile relationship
profile_identity_id: Rc<i64>, // multi-profile relationship
}
impl Database {
// Constructors
/// Create new `Self`
pub fn new(connection: Rc<RwLock<Connection>>, profile_id: Rc<i64>) -> Self {
pub fn new(connection: Rc<RwLock<Connection>>, profile_identity_id: Rc<i64>) -> Self {
Self {
connection,
profile_id,
profile_identity_id,
}
}
/// Get all records match current `profile_id`
/// Get all records match current `profile_identity_id`
pub fn records(&self) -> Result<Vec<Table>, Error> {
let readable = self.connection.read().unwrap(); // @TODO
let tx = readable.unchecked_transaction()?;
select(&tx, *self.profile_id)
select(&tx, *self.profile_identity_id)
}
}
@ -38,11 +38,11 @@ pub fn init(tx: &Transaction) -> Result<usize, Error> {
tx.execute(
"CREATE TABLE IF NOT EXISTS `profile_identity_gemini`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`profile_id` INTEGER NOT NULL,
`pem` TEXT NOT NULL,
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`profile_identity_id` INTEGER NOT NULL,
`pem` TEXT NOT NULL,
FOREIGN KEY (`profile_id`) REFERENCES `profile`(`id`)
FOREIGN KEY (`profile_identity_id`) REFERENCES `profile_identity`(`id`)
)",
[],
)
@ -50,13 +50,17 @@ pub fn init(tx: &Transaction) -> Result<usize, Error> {
pub fn select(tx: &Transaction, profile_id: i64) -> Result<Vec<Table>, Error> {
let mut stmt = tx.prepare(
"SELECT `id`, `profile_id`, `pem` FROM `profile_identity_gemini` WHERE `profile_id` = ?",
"SELECT `id`,
`profile_identity_id`,
`pem`
FROM `profile_identity_gemini` WHERE `profile_identity_id` = ?",
)?;
let result = stmt.query_map([profile_id], |row| {
Ok(Table {
id: row.get(0)?,
//profile_id: row.get(1)?,
//profile_identity_id: row.get(1)?,
pem: row.get(2)?,
})
})?;