implement existing certificate selection

This commit is contained in:
yggverse 2024-11-19 13:46:55 +02:00
parent 10cf4fc6a1
commit 728a9c06d5
9 changed files with 189 additions and 41 deletions

View file

@ -2,9 +2,8 @@ use sqlite::{Connection, Error, Transaction};
use std::{rc::Rc, sync::RwLock};
pub struct Table {
//pub id: i64,
pub id: i64,
pub profile_identity_gemini_id: i64,
pub is_active: bool,
pub url: String,
}
@ -21,6 +20,43 @@ impl Database {
Self { connection }
}
// Actions
/// Create new record in database
pub fn add(&self, profile_identity_gemini_id: i64, url: &str) -> Result<i64, Error> {
// Begin new transaction
let mut writable = self.connection.write().unwrap(); // @TODO
let tx = writable.transaction()?;
// Create new record
insert(&tx, profile_identity_gemini_id, url)?;
// Hold insert ID for result
let id = last_insert_id(&tx);
// Done
match tx.commit() {
Ok(_) => Ok(id),
Err(reason) => Err(reason),
}
}
/// Delete record with given `id` from database
pub fn delete(&self, id: i64) -> Result<(), Error> {
// Begin new transaction
let mut writable = self.connection.write().unwrap(); // @TODO
let tx = writable.transaction()?;
// Create new record
delete(&tx, id)?;
// Done
match tx.commit() {
Ok(_) => Ok(()),
Err(reason) => Err(reason),
}
}
// Getters
/// Get records from database match current `profile_id` optionally filtered by `url`
@ -39,14 +75,12 @@ pub fn init(tx: &Transaction) -> Result<usize, Error> {
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`profile_identity_gemini_id` INTEGER NOT NULL,
`is_active` INTEGER NOT NULL,
`url` VARCHAR(1024) NOT NULL,
FOREIGN KEY (`profile_identity_gemini_id`) REFERENCES `profile_identity_gemini`(`id`),
UNIQUE (
`profile_identity_gemini_id`,
`is_active`,
`url`
)
)",
@ -54,11 +88,31 @@ pub fn init(tx: &Transaction) -> Result<usize, Error> {
)
}
pub fn insert(
tx: &Transaction,
profile_identity_gemini_id: i64,
url: &str,
) -> Result<usize, Error> {
tx.execute(
"INSERT INTO `profile_identity_gemini_auth` (
`profile_identity_gemini_id`,
`url`
) VALUES (?, ?)",
(profile_identity_gemini_id, url),
)
}
pub fn delete(tx: &Transaction, id: i64) -> Result<usize, Error> {
tx.execute(
"DELETE FROM `profile_identity_gemini_auth` WHERE `id` = ?",
[id],
)
}
pub fn select(tx: &Transaction, url: Option<&str>) -> Result<Vec<Table>, Error> {
let mut stmt = tx.prepare(
"SELECT `id`,
`profile_identity_gemini_id`,
`is_active`,
`url`
FROM `profile_identity_gemini_auth`
@ -67,10 +121,9 @@ pub fn select(tx: &Transaction, url: Option<&str>) -> Result<Vec<Table>, Error>
let result = stmt.query_map([url.unwrap_or("%")], |row| {
Ok(Table {
//id: row.get(0)?,
id: row.get(0)?,
profile_identity_gemini_id: row.get(1)?,
is_active: row.get(2)?,
url: row.get(3)?,
url: row.get(2)?,
})
})?;
@ -83,3 +136,7 @@ pub fn select(tx: &Transaction, url: Option<&str>) -> Result<Vec<Table>, Error>
Ok(records)
}
pub fn last_insert_id(tx: &Transaction) -> i64 {
tx.last_insert_rowid()
}