From 28382b249535e964ce983fe5a0821a898a077775 Mon Sep 17 00:00:00 2001 From: salmonik Date: Sat, 28 Feb 2026 17:35:40 +0100 Subject: multi file for both sides --- src/bin/client/database.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/bin/client/database.rs (limited to 'src/bin/client/database.rs') diff --git a/src/bin/client/database.rs b/src/bin/client/database.rs new file mode 100644 index 0000000..bc8f6f8 --- /dev/null +++ b/src/bin/client/database.rs @@ -0,0 +1,30 @@ +use rusqlite::Connection; + +pub fn init_db() -> Connection { + let conn = Connection::open("client.db").unwrap(); + conn.execute( + "CREATE TABLE IF NOT EXISTS hosts ( + host TEXT PRIMARY KEY, + last_used DATETIME DEFAULT CURRENT_TIMESTAMP + )", + [], + ).unwrap(); + conn +} + +pub fn get_last_host(conn: &Connection) -> Option { + let mut stmt = conn.prepare("SELECT host FROM hosts ORDER BY last_used DESC LIMIT 1").unwrap(); + let mut rows = stmt.query([]).unwrap(); + if let Ok(Some(row)) = rows.next() { + row.get(0).ok() + } else { + None + } +} + +pub fn save_host(conn: &Connection, host: &str) { + conn.execute( + "INSERT OR REPLACE INTO hosts (host, last_used) VALUES (?1, CURRENT_TIMESTAMP)", + [host], + ).unwrap(); +} -- cgit v1.2.3