2024-10-06 00:43:35 +03:00
|
|
|
use sqlite::{Error, Transaction};
|
2024-10-02 15:22:50 +03:00
|
|
|
|
2024-10-04 02:22:37 +03:00
|
|
|
pub struct Table {
|
|
|
|
pub id: i64,
|
2024-10-02 20:02:48 +03:00
|
|
|
}
|
|
|
|
|
2024-10-02 15:22:50 +03:00
|
|
|
pub struct Database {
|
2024-10-06 00:43:35 +03:00
|
|
|
// nothing yet..
|
2024-10-02 15:22:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Database {
|
2024-10-06 00:43:35 +03:00
|
|
|
pub fn init(tx: &Transaction) -> Result<Database, Error> {
|
|
|
|
tx.execute(
|
2024-10-02 20:00:08 +03:00
|
|
|
"CREATE TABLE IF NOT EXISTS `app`
|
|
|
|
(
|
2024-10-06 00:43:35 +03:00
|
|
|
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL
|
2024-10-02 20:00:08 +03:00
|
|
|
)",
|
|
|
|
[],
|
2024-10-04 02:57:50 +03:00
|
|
|
)?;
|
2024-10-02 15:22:50 +03:00
|
|
|
|
2024-10-06 00:43:35 +03:00
|
|
|
Ok(Self {})
|
2024-10-02 15:22:50 +03:00
|
|
|
}
|
|
|
|
|
2024-10-06 00:43:35 +03:00
|
|
|
pub fn add(&self, tx: &Transaction) -> Result<usize, Error> {
|
|
|
|
tx.execute("INSERT INTO `app` DEFAULT VALUES", [])
|
2024-10-03 00:31:41 +03:00
|
|
|
}
|
|
|
|
|
2024-10-06 00:43:35 +03:00
|
|
|
pub fn records(&self, tx: &Transaction) -> Result<Vec<Table>, Error> {
|
|
|
|
let mut stmt = tx.prepare("SELECT `id` FROM `app`")?;
|
|
|
|
let result = stmt.query_map([], |row| Ok(Table { id: row.get(0)? }))?;
|
2024-10-04 18:59:41 +03:00
|
|
|
|
2024-10-04 19:00:47 +03:00
|
|
|
let mut records = Vec::new();
|
2024-10-04 18:59:41 +03:00
|
|
|
|
|
|
|
for record in result {
|
|
|
|
let table = record?;
|
|
|
|
records.push(table);
|
|
|
|
}
|
2024-10-04 02:22:37 +03:00
|
|
|
|
|
|
|
Ok(records)
|
|
|
|
}
|
|
|
|
|
2024-10-06 00:43:35 +03:00
|
|
|
pub fn delete(&self, tx: &Transaction, id: &i64) -> Result<usize, Error> {
|
|
|
|
tx.execute("DELETE FROM `app` WHERE `id` = ?", [id])
|
2024-10-03 00:31:41 +03:00
|
|
|
}
|
2024-10-02 15:22:50 +03:00
|
|
|
|
2024-10-06 00:43:35 +03:00
|
|
|
pub fn last_insert_id(&self, tx: &Transaction) -> i64 {
|
|
|
|
tx.last_insert_rowid()
|
2024-10-02 15:22:50 +03:00
|
|
|
}
|
|
|
|
}
|