Yoda/src/app/database.rs

42 lines
956 B
Rust
Raw Normal View History

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
}
pub fn init(tx: &Transaction) -> Result<usize, Error> {
tx.execute(
"CREATE TABLE IF NOT EXISTS `app`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL
)",
[],
)
2024-10-02 15:22:50 +03:00
}
2024-11-13 11:45:33 +02:00
pub fn insert(tx: &Transaction) -> Result<usize, Error> {
tx.execute("INSERT INTO `app` DEFAULT VALUES", [])
}
2024-10-04 18:59:41 +03:00
2024-11-13 11:45:33 +02:00
pub fn select(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
let mut records = Vec::new();
2024-10-04 02:22:37 +03:00
for record in result {
let table = record?;
records.push(table);
2024-10-04 02:22:37 +03:00
}
Ok(records)
}
2024-10-02 15:22:50 +03:00
pub fn delete(tx: &Transaction, id: &i64) -> Result<usize, Error> {
tx.execute("DELETE FROM `app` WHERE `id` = ?", [id])
}
pub fn last_insert_id(tx: &Transaction) -> i64 {
tx.last_insert_rowid()
2024-10-02 15:22:50 +03:00
}