2024-11-13 03:40:29 +02:00
|
|
|
use sqlite::{Error, Transaction};
|
2024-11-08 07:46:25 +02:00
|
|
|
|
2024-11-13 03:40:29 +02:00
|
|
|
pub struct Table {
|
|
|
|
pub id: i64,
|
|
|
|
}
|
2024-11-08 07:46:25 +02:00
|
|
|
|
2024-11-13 03:40:29 +02:00
|
|
|
pub fn init(tx: &Transaction) -> Result<usize, Error> {
|
|
|
|
tx.execute(
|
|
|
|
"CREATE TABLE IF NOT EXISTS `profile`
|
|
|
|
(
|
|
|
|
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL
|
|
|
|
)",
|
|
|
|
[],
|
|
|
|
)
|
|
|
|
}
|
2024-11-08 07:46:25 +02:00
|
|
|
|
2024-11-13 03:40:29 +02:00
|
|
|
pub fn add(tx: &Transaction) -> Result<usize, Error> {
|
|
|
|
tx.execute("INSERT INTO `profile` DEFAULT VALUES", [])
|
2024-11-08 07:46:25 +02:00
|
|
|
}
|
2024-11-12 12:30:12 +02:00
|
|
|
|
2024-11-13 03:40:29 +02:00
|
|
|
pub fn records(tx: &Transaction) -> Result<Vec<Table>, Error> {
|
|
|
|
let mut stmt = tx.prepare("SELECT `profile_id` FROM `profile`")?;
|
|
|
|
let result = stmt.query_map([], |row| Ok(Table { id: row.get(0)? }))?;
|
2024-11-12 12:30:12 +02:00
|
|
|
|
2024-11-13 03:40:29 +02:00
|
|
|
let mut records = Vec::new();
|
2024-11-12 12:30:12 +02:00
|
|
|
|
2024-11-13 03:40:29 +02:00
|
|
|
for record in result {
|
|
|
|
let table = record?;
|
|
|
|
records.push(table);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(records)
|
|
|
|
}
|
2024-11-12 12:30:12 +02:00
|
|
|
|
2024-11-13 03:40:29 +02:00
|
|
|
pub fn delete(tx: &Transaction, id: &i64) -> Result<usize, Error> {
|
|
|
|
tx.execute("DELETE FROM `profile` WHERE `id` = ?", [id])
|
|
|
|
}
|
2024-11-12 12:30:12 +02:00
|
|
|
|
2024-11-13 03:40:29 +02:00
|
|
|
pub fn last_insert_id(tx: &Transaction) -> i64 {
|
|
|
|
tx.last_insert_rowid()
|
2024-11-12 12:30:12 +02:00
|
|
|
}
|