mirror of
https://github.com/YGGverse/Yoda.git
synced 2026-04-02 01:25:27 +00:00
use internal address HashMap registry for the TOFU validation
This commit is contained in:
parent
6d6b1bc8c8
commit
a7230fd329
4 changed files with 86 additions and 55 deletions
|
|
@ -34,10 +34,10 @@ impl Database {
|
|||
|
||||
/// Create new record in database
|
||||
/// * return last insert ID on success
|
||||
pub fn add(&self, time: &DateTime, pem: &str) -> Result<i64> {
|
||||
pub fn add(&self, address: String, time: &DateTime, pem: &str) -> Result<i64> {
|
||||
let mut connection = self.pool.get()?;
|
||||
let tx = connection.transaction()?;
|
||||
let id = insert(&tx, self.profile_id, time, pem)?;
|
||||
let id = insert(&tx, self.profile_id, address, time, pem)?;
|
||||
tx.commit()?;
|
||||
Ok(id)
|
||||
}
|
||||
|
|
@ -52,37 +52,49 @@ pub fn init(tx: &Transaction) -> Result<usize> {
|
|||
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
`profile_id` INTEGER NOT NULL,
|
||||
`time` INTEGER NOT NULL,
|
||||
`address` VARCHAR(255) NOT NULL,
|
||||
`pem` TEXT NOT NULL,
|
||||
|
||||
FOREIGN KEY (`profile_id`) REFERENCES `profile` (`id`)
|
||||
FOREIGN KEY (`profile_id`) REFERENCES `profile` (`id`),
|
||||
UNIQUE (`address`)
|
||||
)",
|
||||
[],
|
||||
)?)
|
||||
}
|
||||
|
||||
pub fn insert(tx: &Transaction, profile_id: i64, time: &DateTime, pem: &str) -> Result<i64> {
|
||||
pub fn insert(
|
||||
tx: &Transaction,
|
||||
profile_id: i64,
|
||||
address: String,
|
||||
time: &DateTime,
|
||||
pem: &str,
|
||||
) -> Result<i64> {
|
||||
tx.execute(
|
||||
"INSERT INTO `profile_tofu` (
|
||||
`profile_id`,
|
||||
`time`,
|
||||
`address`,
|
||||
`pem`
|
||||
) VALUES (?, ?, ?)",
|
||||
(profile_id, time.to_unix(), pem),
|
||||
) VALUES (?, ?, ?, ?) ON CONFLICT (`address`)
|
||||
DO UPDATE SET `time` = `excluded`.`time`,
|
||||
`pem` = `excluded`.`pem`",
|
||||
(profile_id, time.to_unix(), address, pem),
|
||||
)?;
|
||||
Ok(tx.last_insert_rowid())
|
||||
}
|
||||
|
||||
pub fn select(tx: &Transaction, profile_id: i64) -> Result<Vec<Row>> {
|
||||
let mut stmt = tx.prepare(
|
||||
"SELECT `id`, `profile_id`, `time`, `pem` FROM `profile_tofu` WHERE `profile_id` = ?",
|
||||
"SELECT `id`, `profile_id`, `address`, `time`, `pem` FROM `profile_tofu` WHERE `profile_id` = ?",
|
||||
)?;
|
||||
|
||||
let result = stmt.query_map([profile_id], |row| {
|
||||
Ok(Row {
|
||||
id: row.get(0)?,
|
||||
//profile_id: row.get(1)?,
|
||||
time: DateTime::from_unix_local(row.get(2)?).unwrap(),
|
||||
pem: row.get(3)?,
|
||||
address: row.get(2)?,
|
||||
time: DateTime::from_unix_local(row.get(3)?).unwrap(),
|
||||
pem: row.get(4)?,
|
||||
})
|
||||
})?;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
pub struct Row {
|
||||
pub address: String,
|
||||
pub id: i64,
|
||||
pub pem: String,
|
||||
pub time: gtk::glib::DateTime,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue