mirror of
https://github.com/YGGverse/Yoda.git
synced 2026-04-01 09:05:27 +00:00
implement sqlite transactions
This commit is contained in:
parent
d5101a6465
commit
271acd50ed
8 changed files with 213 additions and 119 deletions
|
|
@ -1,5 +1,4 @@
|
|||
use sqlite::{Connection, Error};
|
||||
use std::sync::Arc;
|
||||
use sqlite::{Error, Transaction};
|
||||
|
||||
pub struct Table {
|
||||
pub id: i64,
|
||||
|
|
@ -7,12 +6,12 @@ pub struct Table {
|
|||
}
|
||||
|
||||
pub struct Database {
|
||||
connection: Arc<Connection>,
|
||||
// nothing yet..
|
||||
}
|
||||
|
||||
impl Database {
|
||||
pub fn init(connection: Arc<Connection>) -> Result<Database, Error> {
|
||||
connection.execute(
|
||||
pub fn init(tx: &Transaction) -> Result<Database, Error> {
|
||||
tx.execute(
|
||||
"CREATE TABLE IF NOT EXISTS `app_browser`
|
||||
(
|
||||
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
|
|
@ -21,20 +20,17 @@ impl Database {
|
|||
[],
|
||||
)?;
|
||||
|
||||
Ok(Self { connection })
|
||||
Ok(Self {})
|
||||
}
|
||||
|
||||
pub fn add(&self, app_id: &i64) -> Result<usize, Error> {
|
||||
self.connection
|
||||
.execute("INSERT INTO `app_browser` (`app_id`) VALUES (?)", [app_id])
|
||||
pub fn add(&self, tx: &Transaction, app_id: &i64) -> Result<usize, Error> {
|
||||
tx.execute("INSERT INTO `app_browser` (`app_id`) VALUES (?)", [app_id])
|
||||
}
|
||||
|
||||
pub fn records(&self, app_id: &i64) -> Result<Vec<Table>, Error> {
|
||||
let mut statement = self
|
||||
.connection
|
||||
.prepare("SELECT `id`, `app_id` FROM `app_browser` WHERE `app_id` = ?")?;
|
||||
pub fn records(&self, tx: &Transaction, app_id: &i64) -> Result<Vec<Table>, Error> {
|
||||
let mut stmt = tx.prepare("SELECT `id`, `app_id` FROM `app_browser` WHERE `app_id` = ?")?;
|
||||
|
||||
let result = statement.query_map([app_id], |row| {
|
||||
let result = stmt.query_map([app_id], |row| {
|
||||
Ok(Table {
|
||||
id: row.get(0)?,
|
||||
// app_id: row.get(1)?, not in use
|
||||
|
|
@ -51,12 +47,11 @@ impl Database {
|
|||
Ok(records)
|
||||
}
|
||||
|
||||
pub fn delete(&self, id: &i64) -> Result<usize, Error> {
|
||||
self.connection
|
||||
.execute("DELETE FROM `app_browser` WHERE `id` = ?", [id])
|
||||
pub fn delete(&self, tx: &Transaction, id: &i64) -> Result<usize, Error> {
|
||||
tx.execute("DELETE FROM `app_browser` WHERE `id` = ?", [id])
|
||||
}
|
||||
|
||||
pub fn last_insert_id(&self) -> i64 {
|
||||
self.connection.last_insert_rowid()
|
||||
pub fn last_insert_id(&self, tx: &Transaction) -> i64 {
|
||||
tx.last_insert_rowid()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue