move DateTime type out of DB row member

This commit is contained in:
yggverse 2025-07-26 05:25:15 +03:00
parent a7c6e79aac
commit b56cd19164
3 changed files with 13 additions and 10 deletions

View File

@ -6,7 +6,7 @@ use certificate::Certificate;
use database::Database; use database::Database;
use gtk::{ use gtk::{
gio::TlsCertificate, gio::TlsCertificate,
glib::{GString, Uri}, glib::{DateTime, GString, Uri},
}; };
use r2d2::Pool; use r2d2::Pool;
use r2d2_sqlite::SqliteConnectionManager; use r2d2_sqlite::SqliteConnectionManager;
@ -36,7 +36,7 @@ impl Tofu {
for r in records { for r in records {
if m.insert( if m.insert(
(r.host.into(), r.port), (r.host.into(), r.port),
Certificate::from_db(Some(r.id), &r.pem, r.time)?, Certificate::from_db(Some(r.id), &r.pem, DateTime::from_unix_local(r.time)?)?,
) )
.is_some() .is_some()
{ {
@ -82,8 +82,12 @@ impl Tofu {
pub fn save(&self) -> Result<()> { pub fn save(&self) -> Result<()> {
for ((host, port), certificate) in self.memory.borrow_mut().drain() { for ((host, port), certificate) in self.memory.borrow_mut().drain() {
if certificate.id().is_none() { if certificate.id().is_none() {
self.database self.database.add(
.add(host.into(), port, certificate.time(), &certificate.pem())?; host.into(),
port,
certificate.time().to_unix(),
&certificate.pem(),
)?;
} }
} }
Ok(()) Ok(())

View File

@ -1,7 +1,6 @@
mod row; mod row;
use anyhow::Result; use anyhow::Result;
use gtk::glib::DateTime;
use r2d2::Pool; use r2d2::Pool;
use r2d2_sqlite::SqliteConnectionManager; use r2d2_sqlite::SqliteConnectionManager;
use row::Row; use row::Row;
@ -34,7 +33,7 @@ impl Database {
/// Create new record in database /// Create new record in database
/// * return last insert ID on success /// * return last insert ID on success
pub fn add(&self, host: String, port: i32, time: &DateTime, pem: &str) -> Result<i64> { pub fn add(&self, host: String, port: i32, time: i64, pem: &str) -> Result<i64> {
let mut connection = self.pool.get()?; let mut connection = self.pool.get()?;
let tx = connection.transaction()?; let tx = connection.transaction()?;
let id = insert(&tx, self.profile_id, host, port, time, pem)?; let id = insert(&tx, self.profile_id, host, port, time, pem)?;
@ -68,7 +67,7 @@ pub fn insert(
profile_id: i64, profile_id: i64,
host: String, host: String,
port: i32, port: i32,
time: &DateTime, time: i64,
pem: &str, pem: &str,
) -> Result<i64> { ) -> Result<i64> {
tx.execute( tx.execute(
@ -81,7 +80,7 @@ pub fn insert(
) VALUES (?, ?, ?, ?, ?) ON CONFLICT (`host`, `port`) ) VALUES (?, ?, ?, ?, ?) ON CONFLICT (`host`, `port`)
DO UPDATE SET `time` = `excluded`.`time`, DO UPDATE SET `time` = `excluded`.`time`,
`pem` = `excluded`.`pem`", `pem` = `excluded`.`pem`",
(profile_id, time.to_unix(), host, port, pem), (profile_id, time, host, port, pem),
)?; )?;
Ok(tx.last_insert_rowid()) Ok(tx.last_insert_rowid())
} }
@ -102,7 +101,7 @@ pub fn select(tx: &Transaction, profile_id: i64) -> Result<Vec<Row>> {
//profile_id: row.get(1)?, //profile_id: row.get(1)?,
host: row.get(2)?, host: row.get(2)?,
port: row.get(3)?, port: row.get(3)?,
time: DateTime::from_unix_local(row.get(4)?).unwrap(), time: row.get(4)?,
pem: row.get(5)?, pem: row.get(5)?,
}) })
})?; })?;

View File

@ -3,5 +3,5 @@ pub struct Row {
pub id: i64, pub id: i64,
pub pem: String, pub pem: String,
pub port: i32, pub port: i32,
pub time: gtk::glib::DateTime, pub time: i64,
} }