remove profile_id relationship as profile_identity_gemini_id already has it

This commit is contained in:
yggverse 2024-11-16 15:51:55 +02:00
parent d9a6c88c1c
commit 3348e30d80
3 changed files with 11 additions and 21 deletions

View File

@ -21,7 +21,7 @@ impl Gemini {
/// Create new `Self` /// Create new `Self`
pub fn new(connection: Rc<RwLock<Connection>>, profile_id: Rc<i64>) -> Self { pub fn new(connection: Rc<RwLock<Connection>>, profile_id: Rc<i64>) -> Self {
Self { Self {
auth: Rc::new(Auth::new(connection.clone(), profile_id.clone())), auth: Rc::new(Auth::new(connection.clone())),
database: Rc::new(Database::new(connection, profile_id)), database: Rc::new(Database::new(connection, profile_id)),
} }
} }

View File

@ -5,7 +5,7 @@ use database::Database;
use sqlite::{Connection, Transaction}; use sqlite::{Connection, Transaction};
use std::{rc::Rc, sync::RwLock}; use std::{rc::Rc, sync::RwLock};
/// API for `gemini_id` + `request` auth pairs operations /// API for `profile_identity_gemini_id` + `url` auth pairs operations
pub struct Auth { pub struct Auth {
pub database: Rc<Database>, pub database: Rc<Database>,
} }
@ -14,9 +14,9 @@ impl Auth {
// Constructors // Constructors
/// Create new `Self` /// Create new `Self`
pub fn new(connection: Rc<RwLock<Connection>>, profile_id: Rc<i64>) -> Self { pub fn new(connection: Rc<RwLock<Connection>>) -> Self {
Self { Self {
database: Rc::new(Database::new(connection, profile_id)), database: Rc::new(Database::new(connection)),
} }
} }
} }

View File

@ -4,25 +4,20 @@ use sqlite::{Connection, Error, Transaction};
pub struct Table { pub struct Table {
//pub id: i64, //pub id: i64,
//pub profile_id: i64,
pub profile_identity_gemini_id: i64, pub profile_identity_gemini_id: i64,
} }
/// Storage for `profile_identity_gemini_id` + `url` auth pairs /// Storage for `profile_identity_gemini_id` + `url` auth pairs
pub struct Database { pub struct Database {
connection: Rc<RwLock<Connection>>, connection: Rc<RwLock<Connection>>,
profile_id: Rc<i64>, // multi-profile relationship
} }
impl Database { impl Database {
// Constructors // Constructors
/// Create new `Self` /// Create new `Self`
pub fn new(connection: Rc<RwLock<Connection>>, profile_id: Rc<i64>) -> Self { pub fn new(connection: Rc<RwLock<Connection>>) -> Self {
Self { Self { connection }
connection,
profile_id,
}
} }
// Getters // Getters
@ -31,7 +26,7 @@ impl Database {
pub fn records(&self, url: Option<&str>) -> Result<Vec<Table>, Error> { pub fn records(&self, url: Option<&str>) -> Result<Vec<Table>, Error> {
let readable = self.connection.read().unwrap(); // @TODO let readable = self.connection.read().unwrap(); // @TODO
let tx = readable.unchecked_transaction()?; let tx = readable.unchecked_transaction()?;
select(&tx, *self.profile_id, url) select(&tx, url)
} }
} }
@ -42,16 +37,13 @@ pub fn init(tx: &Transaction) -> Result<usize, Error> {
"CREATE TABLE IF NOT EXISTS `profile_identity_gemini_auth` "CREATE TABLE IF NOT EXISTS `profile_identity_gemini_auth`
( (
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`profile_id` INTEGER NOT NULL,
`profile_identity_gemini_id` INTEGER NOT NULL, `profile_identity_gemini_id` INTEGER NOT NULL,
`is_active` INTEGER NOT NULL, `is_active` INTEGER NOT NULL,
`url` VARCHAR(1024) NOT NULL, `url` VARCHAR(1024) NOT NULL,
FOREIGN KEY (`profile_id`) REFERENCES `profile`(`id`),
FOREIGN KEY (`profile_identity_gemini_id`) REFERENCES `profile_identity_gemini`(`id`), FOREIGN KEY (`profile_identity_gemini_id`) REFERENCES `profile_identity_gemini`(`id`),
UNIQUE ( UNIQUE (
`profile_id`,
`profile_identity_gemini_id`, `profile_identity_gemini_id`,
`is_active`, `is_active`,
`url` `url`
@ -61,21 +53,19 @@ pub fn init(tx: &Transaction) -> Result<usize, Error> {
) )
} }
pub fn select(tx: &Transaction, profile_id: i64, url: Option<&str>) -> Result<Vec<Table>, Error> { pub fn select(tx: &Transaction, url: Option<&str>) -> Result<Vec<Table>, Error> {
let mut stmt = tx.prepare( let mut stmt = tx.prepare(
"SELECT `id`, "SELECT `id`,
`profile_id`,
`profile_identity_gemini_id` `profile_identity_gemini_id`
FROM `profile_identity_gemini_auth` FROM `profile_identity_gemini_auth`
WHERE `profile_id` = ? AND `url` LIKE ?", WHERE `url` LIKE ?",
)?; )?;
let result = stmt.query_map((profile_id, url.unwrap_or("%")), |row| { let result = stmt.query_map([url.unwrap_or("%")], |row| {
Ok(Table { Ok(Table {
//id: row.get(0)?, //id: row.get(0)?,
//profile_id: row.get(1)?, profile_identity_gemini_id: row.get(1)?,
profile_identity_gemini_id: row.get(2)?,
}) })
})?; })?;