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.execute( "CREATE TABLE IF NOT EXISTS credentials ( host TEXT PRIMARY KEY, username TEXT NOT NULL, password TEXT NOT NULL )", [], ).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(); } pub fn save_credentials(conn: &Connection, host: &str, username: &str, password: &str) -> Result<(), Box> { conn.execute( "INSERT OR REPLACE INTO credentials (host, username, password) VALUES (?1, ?2, ?3)", (host, username, password), )?; Ok(()) } pub fn get_credentials(conn: &Connection, host: &str) -> Option<(String, String)> { let mut stmt = conn.prepare("SELECT username, password FROM credentials WHERE host = ?1").unwrap(); let mut rows = stmt.query([host]).unwrap(); if let Ok(Some(row)) = rows.next() { let username: String = row.get(0).unwrap(); let password: String = row.get(1).unwrap(); Some((username, password)) } else { None } }