summaryrefslogtreecommitdiffstats
path: root/src/bin/client/database.rs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/bin/client/database.rs30
1 files changed, 30 insertions, 0 deletions
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 @@
1use rusqlite::Connection;
2
3pub fn init_db() -> Connection {
4 let conn = Connection::open("client.db").unwrap();
5 conn.execute(
6 "CREATE TABLE IF NOT EXISTS hosts (
7 host TEXT PRIMARY KEY,
8 last_used DATETIME DEFAULT CURRENT_TIMESTAMP
9 )",
10 [],
11 ).unwrap();
12 conn
13}
14
15pub fn get_last_host(conn: &Connection) -> Option<String> {
16 let mut stmt = conn.prepare("SELECT host FROM hosts ORDER BY last_used DESC LIMIT 1").unwrap();
17 let mut rows = stmt.query([]).unwrap();
18 if let Ok(Some(row)) = rows.next() {
19 row.get(0).ok()
20 } else {
21 None
22 }
23}
24
25pub fn save_host(conn: &Connection, host: &str) {
26 conn.execute(
27 "INSERT OR REPLACE INTO hosts (host, last_used) VALUES (?1, CURRENT_TIMESTAMP)",
28 [host],
29 ).unwrap();
30}