diff options
| author | salmonik <raisersoft@proton.me> | 2026-02-28 17:35:40 +0100 |
|---|---|---|
| committer | salmonik <raisersoft@proton.me> | 2026-02-28 17:35:40 +0100 |
| commit | 28382b249535e964ce983fe5a0821a898a077775 (patch) | |
| tree | 3b68dd13d9fec27ab3163f09285c718874540c65 /src/bin/client/database.rs | |
| parent | init version (diff) | |
| download | zagan-bet-28382b249535e964ce983fe5a0821a898a077775.tar.gz zagan-bet-28382b249535e964ce983fe5a0821a898a077775.zip | |
multi file for both sides
Diffstat (limited to '')
| -rw-r--r-- | src/bin/client/database.rs | 30 |
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 @@ | |||
| 1 | use rusqlite::Connection; | ||
| 2 | |||
| 3 | pub 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 | |||
| 15 | pub 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 | |||
| 25 | pub 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 | } | ||
