diff options
| author | salmonik <salmonik@tuta.io> | 2026-08-24 14:48:12 +0200 |
|---|---|---|
| committer | salmonik <salmonik@tuta.io> | 2026-08-24 14:48:12 +0200 |
| commit | 5d6b5049c2a34a3f8abe725d4a871ba84a0dd714 (patch) | |
| tree | 57cb03bf59b3358a96742f455d38c21f4f645396 /src/bin/client/database.rs | |
| parent | stable and working, both platforms (diff) | |
| download | zagan-bet-5d6b5049c2a34a3f8abe725d4a871ba84a0dd714.tar.gz zagan-bet-5d6b5049c2a34a3f8abe725d4a871ba84a0dd714.zip | |
TLS transport and games framework on client and server
Diffstat (limited to '')
| -rw-r--r-- | src/bin/client/database.rs | 26 |
1 files changed, 19 insertions, 7 deletions
diff --git a/src/bin/client/database.rs b/src/bin/client/database.rs index da069a7..a274f31 100644 --- a/src/bin/client/database.rs +++ b/src/bin/client/database.rs | |||
| @@ -8,7 +8,8 @@ pub fn init_db() -> Connection { | |||
| 8 | last_used DATETIME DEFAULT CURRENT_TIMESTAMP | 8 | last_used DATETIME DEFAULT CURRENT_TIMESTAMP |
| 9 | )", | 9 | )", |
| 10 | [], | 10 | [], |
| 11 | ).unwrap(); | 11 | ) |
| 12 | .unwrap(); | ||
| 12 | conn.execute( | 13 | conn.execute( |
| 13 | "CREATE TABLE IF NOT EXISTS credentials ( | 14 | "CREATE TABLE IF NOT EXISTS credentials ( |
| 14 | host TEXT PRIMARY KEY, | 15 | host TEXT PRIMARY KEY, |
| @@ -16,12 +17,15 @@ pub fn init_db() -> Connection { | |||
| 16 | password TEXT NOT NULL | 17 | password TEXT NOT NULL |
| 17 | )", | 18 | )", |
| 18 | [], | 19 | [], |
| 19 | ).unwrap(); | 20 | ) |
| 21 | .unwrap(); | ||
| 20 | conn | 22 | conn |
| 21 | } | 23 | } |
| 22 | 24 | ||
| 23 | pub fn get_last_host(conn: &Connection) -> Option<String> { | 25 | pub fn get_last_host(conn: &Connection) -> Option<String> { |
| 24 | let mut stmt = conn.prepare("SELECT host FROM hosts ORDER BY last_used DESC LIMIT 1").unwrap(); | 26 | let mut stmt = conn |
| 27 | .prepare("SELECT host FROM hosts ORDER BY last_used DESC LIMIT 1") | ||
| 28 | .unwrap(); | ||
| 25 | let mut rows = stmt.query([]).unwrap(); | 29 | let mut rows = stmt.query([]).unwrap(); |
| 26 | if let Ok(Some(row)) = rows.next() { | 30 | if let Ok(Some(row)) = rows.next() { |
| 27 | row.get(0).ok() | 31 | row.get(0).ok() |
| @@ -34,10 +38,16 @@ pub fn save_host(conn: &Connection, host: &str) { | |||
| 34 | conn.execute( | 38 | conn.execute( |
| 35 | "INSERT OR REPLACE INTO hosts (host, last_used) VALUES (?1, CURRENT_TIMESTAMP)", | 39 | "INSERT OR REPLACE INTO hosts (host, last_used) VALUES (?1, CURRENT_TIMESTAMP)", |
| 36 | [host], | 40 | [host], |
| 37 | ).unwrap(); | 41 | ) |
| 42 | .unwrap(); | ||
| 38 | } | 43 | } |
| 39 | 44 | ||
| 40 | pub fn save_credentials(conn: &Connection, host: &str, username: &str, password: &str) -> Result<(), Box<dyn std::error::Error>> { | 45 | pub fn save_credentials( |
| 46 | conn: &Connection, | ||
| 47 | host: &str, | ||
| 48 | username: &str, | ||
| 49 | password: &str, | ||
| 50 | ) -> Result<(), Box<dyn std::error::Error>> { | ||
| 41 | conn.execute( | 51 | conn.execute( |
| 42 | "INSERT OR REPLACE INTO credentials (host, username, password) VALUES (?1, ?2, ?3)", | 52 | "INSERT OR REPLACE INTO credentials (host, username, password) VALUES (?1, ?2, ?3)", |
| 43 | (host, username, password), | 53 | (host, username, password), |
| @@ -46,7 +56,9 @@ pub fn save_credentials(conn: &Connection, host: &str, username: &str, password: | |||
| 46 | } | 56 | } |
| 47 | 57 | ||
| 48 | pub fn get_credentials(conn: &Connection, host: &str) -> Option<(String, String)> { | 58 | pub fn get_credentials(conn: &Connection, host: &str) -> Option<(String, String)> { |
| 49 | let mut stmt = conn.prepare("SELECT username, password FROM credentials WHERE host = ?1").unwrap(); | 59 | let mut stmt = conn |
| 60 | .prepare("SELECT username, password FROM credentials WHERE host = ?1") | ||
| 61 | .unwrap(); | ||
| 50 | let mut rows = stmt.query([host]).unwrap(); | 62 | let mut rows = stmt.query([host]).unwrap(); |
| 51 | if let Ok(Some(row)) = rows.next() { | 63 | if let Ok(Some(row)) = rows.next() { |
| 52 | let username: String = row.get(0).unwrap(); | 64 | let username: String = row.get(0).unwrap(); |
| @@ -55,4 +67,4 @@ pub fn get_credentials(conn: &Connection, host: &str) -> Option<(String, String) | |||
| 55 | } else { | 67 | } else { |
| 56 | None | 68 | None |
| 57 | } | 69 | } |
| 58 | } \ No newline at end of file | 70 | } |
