Yoda/src/app/database.rs

40 lines
873 B
Rust
Raw Normal View History

use sqlite::Connection;
2024-10-02 15:22:50 +03:00
use std::sync::Arc;
2024-10-02 23:28:57 +03:00
enum Table {
2024-10-02 20:02:48 +03:00
Id,
Time,
}
2024-10-02 15:22:50 +03:00
pub struct Database {
connection: Arc<sqlite::Connection>,
}
impl Database {
// Construct new application DB
pub fn init(connection: Arc<Connection>) -> Database {
// Init app table
if let Err(error) = connection.execute(
"CREATE TABLE IF NOT EXISTS `app`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`time` INTEGER NOT NULL DEFAULT CURRENT_TIMESTAMP
)",
[],
) {
panic!("{error}");
}
2024-10-02 15:22:50 +03:00
// Return struct
Self { connection }
2024-10-02 15:22:50 +03:00
}
pub fn add(&self) -> i64 {
if let Err(error) = self.connection.execute("INSERT INTO `app`", []) {
panic!("{error}");
}
2024-10-02 15:22:50 +03:00
self.connection.last_insert_rowid()
2024-10-02 15:22:50 +03:00
}
}