Add sql transactions
This commit is contained in:
		
							parent
							
								
									f539c47e3b
								
							
						
					
					
						commit
						19e2ea003b
					
				
					 17 changed files with 242 additions and 240 deletions
				
			
		| 
						 | 
				
			
			@ -1,7 +1,7 @@
 | 
			
		|||
use std::ops::DerefMut;
 | 
			
		||||
 | 
			
		||||
use serde_derive::{Deserialize, Serialize};
 | 
			
		||||
use sqlx::pool::PoolConnection;
 | 
			
		||||
use sqlx::Transaction;
 | 
			
		||||
use sqlx::Sqlite;
 | 
			
		||||
 | 
			
		||||
use crate::db::{DbRelay, DbTag};
 | 
			
		||||
| 
						 | 
				
			
			@ -21,26 +21,26 @@ pub struct DbController {
 | 
			
		|||
 | 
			
		||||
impl DbController {
 | 
			
		||||
	pub async fn get_all(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
	) -> Result<Vec<DbController>, DatabaseError> {
 | 
			
		||||
		sqlx::query_as!(DbController, "SELECT * FROM controllers")
 | 
			
		||||
			.fetch_all(conn.deref_mut())
 | 
			
		||||
			.fetch_all(tx.deref_mut())
 | 
			
		||||
			.await
 | 
			
		||||
			.map_err(DatabaseError::from)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		id: i64,
 | 
			
		||||
	) -> Result<Option<DbController>, DatabaseError> {
 | 
			
		||||
		sqlx::query_as!(DbController, "SELECT * FROM controllers WHERE id = ?", id)
 | 
			
		||||
			.fetch_optional(conn.deref_mut())
 | 
			
		||||
			.fetch_optional(tx.deref_mut())
 | 
			
		||||
			.await
 | 
			
		||||
			.map_err(DatabaseError::from)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get_by_uid(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		filter_uid: &EmgauwaUid,
 | 
			
		||||
	) -> Result<Option<DbController>, DatabaseError> {
 | 
			
		||||
		sqlx::query_as!(
 | 
			
		||||
| 
						 | 
				
			
			@ -48,39 +48,39 @@ impl DbController {
 | 
			
		|||
			"SELECT * FROM controllers WHERE uid = ?",
 | 
			
		||||
			filter_uid
 | 
			
		||||
		)
 | 
			
		||||
		.fetch_optional(conn.deref_mut())
 | 
			
		||||
		.fetch_optional(tx.deref_mut())
 | 
			
		||||
		.await
 | 
			
		||||
		.map_err(DatabaseError::from)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get_by_uid_or_create(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		uid: &EmgauwaUid,
 | 
			
		||||
		new_name: &str,
 | 
			
		||||
		new_relay_count: i64,
 | 
			
		||||
	) -> Result<DbController, DatabaseError> {
 | 
			
		||||
		match DbController::get_by_uid(conn, uid).await? {
 | 
			
		||||
		match DbController::get_by_uid(tx, uid).await? {
 | 
			
		||||
			Some(tag) => Ok(tag),
 | 
			
		||||
			None => DbController::create(conn, uid, new_name, new_relay_count).await,
 | 
			
		||||
			None => DbController::create(tx, uid, new_name, new_relay_count).await,
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get_by_tag(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		tag: &DbTag,
 | 
			
		||||
	) -> Result<Vec<DbController>, DatabaseError> {
 | 
			
		||||
		sqlx::query_as!(DbController, "SELECT schedule.* FROM controllers AS schedule INNER JOIN junction_tag ON junction_tag.schedule_id = schedule.id WHERE junction_tag.tag_id = ?", tag.id)
 | 
			
		||||
			.fetch_all(conn.deref_mut())
 | 
			
		||||
			.fetch_all(tx.deref_mut())
 | 
			
		||||
			.await
 | 
			
		||||
			.map_err(DatabaseError::from)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn delete_by_uid(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		filter_uid: EmgauwaUid,
 | 
			
		||||
	) -> Result<(), DatabaseError> {
 | 
			
		||||
		if sqlx::query_scalar!("SELECT 1 FROM controllers WHERE uid = ?", filter_uid)
 | 
			
		||||
			.fetch_optional(conn.deref_mut())
 | 
			
		||||
			.fetch_optional(tx.deref_mut())
 | 
			
		||||
			.await?
 | 
			
		||||
			.is_none()
 | 
			
		||||
		{
 | 
			
		||||
| 
						 | 
				
			
			@ -88,7 +88,7 @@ impl DbController {
 | 
			
		|||
		}
 | 
			
		||||
 | 
			
		||||
		sqlx::query!("DELETE FROM controllers WHERE uid = ?", filter_uid)
 | 
			
		||||
			.execute(conn.deref_mut())
 | 
			
		||||
			.execute(tx.deref_mut())
 | 
			
		||||
			.await
 | 
			
		||||
			.map(|res| match res.rows_affected() {
 | 
			
		||||
				0 => Err(DatabaseError::DeleteError),
 | 
			
		||||
| 
						 | 
				
			
			@ -97,7 +97,7 @@ impl DbController {
 | 
			
		|||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn create(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		new_uid: &EmgauwaUid,
 | 
			
		||||
		new_name: &str,
 | 
			
		||||
		new_relay_count: i64,
 | 
			
		||||
| 
						 | 
				
			
			@ -109,14 +109,14 @@ impl DbController {
 | 
			
		|||
			new_name,
 | 
			
		||||
			new_relay_count,
 | 
			
		||||
		)
 | 
			
		||||
		.fetch_optional(conn.deref_mut())
 | 
			
		||||
		.fetch_optional(tx.deref_mut())
 | 
			
		||||
		.await?
 | 
			
		||||
		.ok_or(DatabaseError::InsertGetError)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn update(
 | 
			
		||||
		&self,
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		new_name: &str,
 | 
			
		||||
		new_relay_count: i64,
 | 
			
		||||
	) -> Result<DbController, DatabaseError> {
 | 
			
		||||
| 
						 | 
				
			
			@ -126,17 +126,17 @@ impl DbController {
 | 
			
		|||
			new_relay_count,
 | 
			
		||||
			self.id,
 | 
			
		||||
		)
 | 
			
		||||
		.execute(conn.deref_mut())
 | 
			
		||||
		.execute(tx.deref_mut())
 | 
			
		||||
		.await?;
 | 
			
		||||
 | 
			
		||||
		Self::get(conn, self.id)
 | 
			
		||||
		Self::get(tx, self.id)
 | 
			
		||||
			.await?
 | 
			
		||||
			.ok_or(DatabaseError::UpdateGetError)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn update_active(
 | 
			
		||||
		&self,
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		new_active: bool,
 | 
			
		||||
	) -> Result<DbController, DatabaseError> {
 | 
			
		||||
		sqlx::query!(
 | 
			
		||||
| 
						 | 
				
			
			@ -144,40 +144,40 @@ impl DbController {
 | 
			
		|||
			new_active,
 | 
			
		||||
			self.id,
 | 
			
		||||
		)
 | 
			
		||||
		.execute(conn.deref_mut())
 | 
			
		||||
		.execute(tx.deref_mut())
 | 
			
		||||
		.await?;
 | 
			
		||||
 | 
			
		||||
		Self::get(conn, self.id)
 | 
			
		||||
		Self::get(tx, self.id)
 | 
			
		||||
			.await?
 | 
			
		||||
			.ok_or(DatabaseError::UpdateGetError)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get_relays(
 | 
			
		||||
		&self,
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
	) -> Result<Vec<DbRelay>, DatabaseError> {
 | 
			
		||||
		sqlx::query_as!(
 | 
			
		||||
			DbRelay,
 | 
			
		||||
			"SELECT * FROM relays WHERE controller_id = ?",
 | 
			
		||||
			self.id
 | 
			
		||||
		)
 | 
			
		||||
		.fetch_all(conn.deref_mut())
 | 
			
		||||
		.fetch_all(tx.deref_mut())
 | 
			
		||||
		.await
 | 
			
		||||
		.map_err(DatabaseError::from)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn all_inactive(conn: &mut PoolConnection<Sqlite>) -> Result<(), DatabaseError> {
 | 
			
		||||
	pub async fn all_inactive(tx: &mut Transaction<'_, Sqlite>) -> Result<(), DatabaseError> {
 | 
			
		||||
		sqlx::query!("UPDATE controllers SET active = 0")
 | 
			
		||||
			.execute(conn.deref_mut())
 | 
			
		||||
			.execute(tx.deref_mut())
 | 
			
		||||
			.await?;
 | 
			
		||||
		Ok(())
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn reload(
 | 
			
		||||
		&self,
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
	) -> Result<DbController, DatabaseError> {
 | 
			
		||||
		Self::get(conn, self.id)
 | 
			
		||||
		Self::get(tx, self.id)
 | 
			
		||||
			.await?
 | 
			
		||||
			.ok_or(DatabaseError::NotFound)
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,6 +1,6 @@
 | 
			
		|||
use std::ops::DerefMut;
 | 
			
		||||
 | 
			
		||||
use sqlx::pool::PoolConnection;
 | 
			
		||||
use sqlx::Transaction;
 | 
			
		||||
use sqlx::Sqlite;
 | 
			
		||||
 | 
			
		||||
use crate::db::{DbRelay, DbSchedule};
 | 
			
		||||
| 
						 | 
				
			
			@ -16,7 +16,7 @@ pub struct DbJunctionRelaySchedule {
 | 
			
		|||
 | 
			
		||||
impl DbJunctionRelaySchedule {
 | 
			
		||||
	pub async fn get(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		id: i64,
 | 
			
		||||
	) -> Result<Option<DbJunctionRelaySchedule>, DatabaseError> {
 | 
			
		||||
		sqlx::query_as!(
 | 
			
		||||
| 
						 | 
				
			
			@ -24,13 +24,13 @@ impl DbJunctionRelaySchedule {
 | 
			
		|||
			"SELECT * FROM junction_relay_schedule WHERE id = ?",
 | 
			
		||||
			id
 | 
			
		||||
		)
 | 
			
		||||
		.fetch_optional(conn.deref_mut())
 | 
			
		||||
		.fetch_optional(tx.deref_mut())
 | 
			
		||||
		.await
 | 
			
		||||
		.map_err(DatabaseError::from)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get_junction_by_relay_and_weekday(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		relay: &DbRelay,
 | 
			
		||||
		weekday: Weekday,
 | 
			
		||||
	) -> Result<Option<DbJunctionRelaySchedule>, DatabaseError> {
 | 
			
		||||
| 
						 | 
				
			
			@ -40,13 +40,13 @@ impl DbJunctionRelaySchedule {
 | 
			
		|||
			relay.id,
 | 
			
		||||
			weekday
 | 
			
		||||
		)
 | 
			
		||||
		.fetch_optional(conn.deref_mut())
 | 
			
		||||
		.fetch_optional(tx.deref_mut())
 | 
			
		||||
		.await
 | 
			
		||||
		.map_err(DatabaseError::from)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get_relays(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		schedule: &DbSchedule,
 | 
			
		||||
	) -> Result<Vec<DbRelay>, DatabaseError> {
 | 
			
		||||
		sqlx::query_as!(
 | 
			
		||||
| 
						 | 
				
			
			@ -57,13 +57,13 @@ impl DbJunctionRelaySchedule {
 | 
			
		|||
			ORDER BY junction_relay_schedule.weekday"#,
 | 
			
		||||
			schedule.id
 | 
			
		||||
		)
 | 
			
		||||
		.fetch_all(conn.deref_mut())
 | 
			
		||||
		.fetch_all(tx.deref_mut())
 | 
			
		||||
		.await
 | 
			
		||||
		.map_err(DatabaseError::from)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get_schedule(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		relay: &DbRelay,
 | 
			
		||||
		weekday: Weekday,
 | 
			
		||||
	) -> Result<Option<DbSchedule>, DatabaseError> {
 | 
			
		||||
| 
						 | 
				
			
			@ -75,13 +75,13 @@ impl DbJunctionRelaySchedule {
 | 
			
		|||
			relay.id,
 | 
			
		||||
			weekday
 | 
			
		||||
		)
 | 
			
		||||
		.fetch_optional(conn.deref_mut())
 | 
			
		||||
		.fetch_optional(tx.deref_mut())
 | 
			
		||||
		.await
 | 
			
		||||
		.map_err(DatabaseError::from)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get_schedules(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		relay: &DbRelay,
 | 
			
		||||
	) -> Result<Vec<DbSchedule>, DatabaseError> {
 | 
			
		||||
		sqlx::query_as!(
 | 
			
		||||
| 
						 | 
				
			
			@ -92,18 +92,18 @@ impl DbJunctionRelaySchedule {
 | 
			
		|||
			ORDER BY junction_relay_schedule.weekday"#,
 | 
			
		||||
			relay.id
 | 
			
		||||
		)
 | 
			
		||||
		.fetch_all(conn.deref_mut())
 | 
			
		||||
		.fetch_all(tx.deref_mut())
 | 
			
		||||
		.await
 | 
			
		||||
		.map_err(DatabaseError::from)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn set_schedule(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		relay: &DbRelay,
 | 
			
		||||
		schedule: &DbSchedule,
 | 
			
		||||
		weekday: Weekday,
 | 
			
		||||
	) -> Result<DbJunctionRelaySchedule, DatabaseError> {
 | 
			
		||||
		match Self::get_junction_by_relay_and_weekday(conn, relay, weekday).await? {
 | 
			
		||||
		match Self::get_junction_by_relay_and_weekday(tx, relay, weekday).await? {
 | 
			
		||||
			None => sqlx::query_as!(
 | 
			
		||||
				DbJunctionRelaySchedule,
 | 
			
		||||
				"INSERT INTO junction_relay_schedule (weekday, relay_id, schedule_id) VALUES (?, ?, ?) RETURNING *",
 | 
			
		||||
| 
						 | 
				
			
			@ -111,7 +111,7 @@ impl DbJunctionRelaySchedule {
 | 
			
		|||
				relay.id,
 | 
			
		||||
				schedule.id
 | 
			
		||||
			)
 | 
			
		||||
			.fetch_optional(conn.deref_mut())
 | 
			
		||||
			.fetch_optional(tx.deref_mut())
 | 
			
		||||
			.await?
 | 
			
		||||
			.ok_or(DatabaseError::InsertGetError),
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -123,10 +123,10 @@ impl DbJunctionRelaySchedule {
 | 
			
		|||
                    schedule.id,
 | 
			
		||||
					junction.id
 | 
			
		||||
                )
 | 
			
		||||
					.execute(conn.deref_mut())
 | 
			
		||||
					.execute(tx.deref_mut())
 | 
			
		||||
					.await?;
 | 
			
		||||
 | 
			
		||||
				Self::get(conn, junction.id)
 | 
			
		||||
				Self::get(tx, junction.id)
 | 
			
		||||
					.await?
 | 
			
		||||
					.ok_or(DatabaseError::UpdateGetError)
 | 
			
		||||
			}
 | 
			
		||||
| 
						 | 
				
			
			@ -134,12 +134,12 @@ impl DbJunctionRelaySchedule {
 | 
			
		|||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn set_schedules(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		relay: &DbRelay,
 | 
			
		||||
		schedules: Vec<&DbSchedule>,
 | 
			
		||||
	) -> Result<(), DatabaseError> {
 | 
			
		||||
		for (weekday, schedule) in schedules.iter().enumerate() {
 | 
			
		||||
			Self::set_schedule(conn, relay, schedule, weekday as Weekday).await?;
 | 
			
		||||
			Self::set_schedule(tx, relay, schedule, weekday as Weekday).await?;
 | 
			
		||||
		}
 | 
			
		||||
		Ok(())
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,6 +1,6 @@
 | 
			
		|||
use std::ops::DerefMut;
 | 
			
		||||
 | 
			
		||||
use sqlx::pool::PoolConnection;
 | 
			
		||||
use sqlx::Transaction;
 | 
			
		||||
use sqlx::Sqlite;
 | 
			
		||||
 | 
			
		||||
use crate::db::{DbRelay, DbSchedule, DbTag};
 | 
			
		||||
| 
						 | 
				
			
			@ -15,7 +15,7 @@ pub struct DbJunctionTag {
 | 
			
		|||
 | 
			
		||||
impl DbJunctionTag {
 | 
			
		||||
	pub async fn link_relay(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		tag: &DbTag,
 | 
			
		||||
		relay: &DbRelay,
 | 
			
		||||
	) -> Result<DbJunctionTag, DatabaseError> {
 | 
			
		||||
| 
						 | 
				
			
			@ -25,13 +25,13 @@ impl DbJunctionTag {
 | 
			
		|||
			tag.id,
 | 
			
		||||
			relay.id
 | 
			
		||||
		)
 | 
			
		||||
		.fetch_optional(conn.deref_mut())
 | 
			
		||||
		.fetch_optional(tx.deref_mut())
 | 
			
		||||
		.await?
 | 
			
		||||
		.ok_or(DatabaseError::InsertGetError)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn link_schedule(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		tag: &DbTag,
 | 
			
		||||
		schedule: &DbSchedule,
 | 
			
		||||
	) -> Result<DbJunctionTag, DatabaseError> {
 | 
			
		||||
| 
						 | 
				
			
			@ -41,7 +41,7 @@ impl DbJunctionTag {
 | 
			
		|||
			tag.id,
 | 
			
		||||
			schedule.id
 | 
			
		||||
		)
 | 
			
		||||
		.fetch_optional(conn.deref_mut())
 | 
			
		||||
		.fetch_optional(tx.deref_mut())
 | 
			
		||||
		.await?
 | 
			
		||||
		.ok_or(DatabaseError::InsertGetError)
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,7 +1,7 @@
 | 
			
		|||
use std::ops::DerefMut;
 | 
			
		||||
 | 
			
		||||
use serde_derive::{Deserialize, Serialize};
 | 
			
		||||
use sqlx::pool::PoolConnection;
 | 
			
		||||
use sqlx::Transaction;
 | 
			
		||||
use sqlx::Sqlite;
 | 
			
		||||
 | 
			
		||||
use crate::db::{DbController, DbMacroAction, DbRelay, DbSchedule};
 | 
			
		||||
| 
						 | 
				
			
			@ -18,35 +18,35 @@ pub struct DbMacro {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
impl DbMacro {
 | 
			
		||||
	pub async fn get_all(conn: &mut PoolConnection<Sqlite>) -> Result<Vec<DbMacro>, DatabaseError> {
 | 
			
		||||
	pub async fn get_all(tx: &mut Transaction<'_, Sqlite>) -> Result<Vec<DbMacro>, DatabaseError> {
 | 
			
		||||
		sqlx::query_as!(DbMacro, "SELECT * FROM macros")
 | 
			
		||||
			.fetch_all(conn.deref_mut())
 | 
			
		||||
			.fetch_all(tx.deref_mut())
 | 
			
		||||
			.await
 | 
			
		||||
			.map_err(DatabaseError::from)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		id: i64,
 | 
			
		||||
	) -> Result<Option<DbMacro>, DatabaseError> {
 | 
			
		||||
		sqlx::query_as!(DbMacro, "SELECT * FROM macros WHERE id = ?", id)
 | 
			
		||||
			.fetch_optional(conn.deref_mut())
 | 
			
		||||
			.fetch_optional(tx.deref_mut())
 | 
			
		||||
			.await
 | 
			
		||||
			.map_err(DatabaseError::from)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get_by_uid(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		filter_uid: &EmgauwaUid,
 | 
			
		||||
	) -> Result<Option<DbMacro>, DatabaseError> {
 | 
			
		||||
		sqlx::query_as!(DbMacro, "SELECT * FROM macros WHERE uid = ?", filter_uid)
 | 
			
		||||
			.fetch_optional(conn.deref_mut())
 | 
			
		||||
			.fetch_optional(tx.deref_mut())
 | 
			
		||||
			.await
 | 
			
		||||
			.map_err(DatabaseError::from)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn create(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		new_uid: EmgauwaUid,
 | 
			
		||||
		new_name: &str,
 | 
			
		||||
	) -> Result<DbMacro, DatabaseError> {
 | 
			
		||||
| 
						 | 
				
			
			@ -56,14 +56,14 @@ impl DbMacro {
 | 
			
		|||
			new_uid,
 | 
			
		||||
			new_name
 | 
			
		||||
		)
 | 
			
		||||
		.fetch_optional(conn.deref_mut())
 | 
			
		||||
		.fetch_optional(tx.deref_mut())
 | 
			
		||||
		.await?
 | 
			
		||||
		.ok_or(DatabaseError::InsertGetError)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn delete(&self, conn: &mut PoolConnection<Sqlite>) -> Result<(), DatabaseError> {
 | 
			
		||||
	pub async fn delete(&self, tx: &mut Transaction<'_, Sqlite>) -> Result<(), DatabaseError> {
 | 
			
		||||
		sqlx::query!("DELETE FROM macros WHERE id = ?", self.id)
 | 
			
		||||
			.execute(conn.deref_mut())
 | 
			
		||||
			.execute(tx.deref_mut())
 | 
			
		||||
			.await
 | 
			
		||||
			.map(|res| match res.rows_affected() {
 | 
			
		||||
				0 => Err(DatabaseError::DeleteError),
 | 
			
		||||
| 
						 | 
				
			
			@ -72,11 +72,11 @@ impl DbMacro {
 | 
			
		|||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn delete_by_uid(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		filter_uid: EmgauwaUid,
 | 
			
		||||
	) -> Result<(), DatabaseError> {
 | 
			
		||||
		if sqlx::query_scalar!("SELECT 1 FROM macros WHERE uid = ?", filter_uid)
 | 
			
		||||
			.fetch_optional(conn.deref_mut())
 | 
			
		||||
			.fetch_optional(tx.deref_mut())
 | 
			
		||||
			.await?
 | 
			
		||||
			.is_none()
 | 
			
		||||
		{
 | 
			
		||||
| 
						 | 
				
			
			@ -84,7 +84,7 @@ impl DbMacro {
 | 
			
		|||
		}
 | 
			
		||||
 | 
			
		||||
		sqlx::query!("DELETE FROM macros WHERE uid = ?", filter_uid)
 | 
			
		||||
			.execute(conn.deref_mut())
 | 
			
		||||
			.execute(tx.deref_mut())
 | 
			
		||||
			.await
 | 
			
		||||
			.map(|res| match res.rows_affected() {
 | 
			
		||||
				0 => Err(DatabaseError::DeleteError),
 | 
			
		||||
| 
						 | 
				
			
			@ -94,62 +94,62 @@ impl DbMacro {
 | 
			
		|||
 | 
			
		||||
	pub async fn update(
 | 
			
		||||
		&self,
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		new_name: &str,
 | 
			
		||||
	) -> Result<DbMacro, DatabaseError> {
 | 
			
		||||
		sqlx::query!("UPDATE relays SET name = ? WHERE id = ?", new_name, self.id,)
 | 
			
		||||
			.execute(conn.deref_mut())
 | 
			
		||||
			.execute(tx.deref_mut())
 | 
			
		||||
			.await?;
 | 
			
		||||
 | 
			
		||||
		DbMacro::get(conn, self.id)
 | 
			
		||||
		DbMacro::get(tx, self.id)
 | 
			
		||||
			.await?
 | 
			
		||||
			.ok_or(DatabaseError::UpdateGetError)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn set_actions(
 | 
			
		||||
		&self,
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		new_actions: &[RequestMacroAction],
 | 
			
		||||
	) -> Result<(), DatabaseError> {
 | 
			
		||||
		sqlx::query!("DELETE FROM macro_actions WHERE macro_id = ?", self.id)
 | 
			
		||||
			.execute(conn.deref_mut())
 | 
			
		||||
			.execute(tx.deref_mut())
 | 
			
		||||
			.await?;
 | 
			
		||||
 | 
			
		||||
		for new_action in new_actions {
 | 
			
		||||
			let controller = DbController::get_by_uid(conn, &new_action.relay.controller_id)
 | 
			
		||||
			let controller = DbController::get_by_uid(tx, &new_action.relay.controller_id)
 | 
			
		||||
				.await?
 | 
			
		||||
				.ok_or(DatabaseError::NotFound)?;
 | 
			
		||||
			let relay =
 | 
			
		||||
				DbRelay::get_by_controller_and_num(conn, &controller, new_action.relay.number)
 | 
			
		||||
				DbRelay::get_by_controller_and_num(tx, &controller, new_action.relay.number)
 | 
			
		||||
					.await?
 | 
			
		||||
					.ok_or(DatabaseError::NotFound)?;
 | 
			
		||||
 | 
			
		||||
			let schedule = DbSchedule::get_by_uid(conn, &new_action.schedule.id)
 | 
			
		||||
			let schedule = DbSchedule::get_by_uid(tx, &new_action.schedule.id)
 | 
			
		||||
				.await?
 | 
			
		||||
				.ok_or(DatabaseError::NotFound)?;
 | 
			
		||||
 | 
			
		||||
			DbMacroAction::create(conn, self, &relay, &schedule, new_action.weekday).await?;
 | 
			
		||||
			DbMacroAction::create(tx, self, &relay, &schedule, new_action.weekday).await?;
 | 
			
		||||
		}
 | 
			
		||||
		Ok(())
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get_actions(
 | 
			
		||||
		&self,
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
	) -> Result<Vec<DbMacroAction>, DatabaseError> {
 | 
			
		||||
		sqlx::query_as!(
 | 
			
		||||
			DbMacroAction,
 | 
			
		||||
			"SELECT * FROM macro_actions WHERE macro_id = ?",
 | 
			
		||||
			self.id
 | 
			
		||||
		)
 | 
			
		||||
		.fetch_all(conn.deref_mut())
 | 
			
		||||
		.fetch_all(tx.deref_mut())
 | 
			
		||||
		.await
 | 
			
		||||
		.map_err(DatabaseError::from)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get_actions_weekday(
 | 
			
		||||
		&self,
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		weekday: i64,
 | 
			
		||||
	) -> Result<Vec<DbMacroAction>, DatabaseError> {
 | 
			
		||||
		sqlx::query_as!(
 | 
			
		||||
| 
						 | 
				
			
			@ -158,7 +158,7 @@ impl DbMacro {
 | 
			
		|||
			self.id,
 | 
			
		||||
			weekday
 | 
			
		||||
		)
 | 
			
		||||
		.fetch_all(conn.deref_mut())
 | 
			
		||||
		.fetch_all(tx.deref_mut())
 | 
			
		||||
		.await
 | 
			
		||||
		.map_err(DatabaseError::from)
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,6 +1,6 @@
 | 
			
		|||
use std::ops::DerefMut;
 | 
			
		||||
 | 
			
		||||
use sqlx::pool::PoolConnection;
 | 
			
		||||
use sqlx::Transaction;
 | 
			
		||||
use sqlx::Sqlite;
 | 
			
		||||
 | 
			
		||||
use crate::db::{DbMacro, DbRelay, DbSchedule};
 | 
			
		||||
| 
						 | 
				
			
			@ -17,16 +17,16 @@ pub struct DbMacroAction {
 | 
			
		|||
 | 
			
		||||
impl DbMacroAction {
 | 
			
		||||
	pub async fn get_all(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
	) -> Result<Vec<DbMacroAction>, DatabaseError> {
 | 
			
		||||
		sqlx::query_as!(DbMacroAction, "SELECT * FROM macro_actions")
 | 
			
		||||
			.fetch_all(conn.deref_mut())
 | 
			
		||||
			.fetch_all(tx.deref_mut())
 | 
			
		||||
			.await
 | 
			
		||||
			.map_err(DatabaseError::from)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		id: i64,
 | 
			
		||||
	) -> Result<Option<DbMacroAction>, DatabaseError> {
 | 
			
		||||
		sqlx::query_as!(
 | 
			
		||||
| 
						 | 
				
			
			@ -34,13 +34,13 @@ impl DbMacroAction {
 | 
			
		|||
			"SELECT * FROM macro_actions WHERE id = ?",
 | 
			
		||||
			id
 | 
			
		||||
		)
 | 
			
		||||
		.fetch_optional(conn.deref_mut())
 | 
			
		||||
		.fetch_optional(tx.deref_mut())
 | 
			
		||||
		.await
 | 
			
		||||
		.map_err(DatabaseError::from)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn create(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		new_macro: &DbMacro,
 | 
			
		||||
		new_relay: &DbRelay,
 | 
			
		||||
		new_schedule: &DbSchedule,
 | 
			
		||||
| 
						 | 
				
			
			@ -54,14 +54,14 @@ impl DbMacroAction {
 | 
			
		|||
			new_schedule.id,
 | 
			
		||||
			new_weekday
 | 
			
		||||
		)
 | 
			
		||||
		.fetch_optional(conn.deref_mut())
 | 
			
		||||
		.fetch_optional(tx.deref_mut())
 | 
			
		||||
		.await?
 | 
			
		||||
		.ok_or(DatabaseError::InsertGetError)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn delete(&self, conn: &mut PoolConnection<Sqlite>) -> Result<(), DatabaseError> {
 | 
			
		||||
	pub async fn delete(&self, tx: &mut Transaction<'_, Sqlite>) -> Result<(), DatabaseError> {
 | 
			
		||||
		sqlx::query!("DELETE FROM macro_actions WHERE id = ?", self.id)
 | 
			
		||||
			.execute(conn.deref_mut())
 | 
			
		||||
			.execute(tx.deref_mut())
 | 
			
		||||
			.await
 | 
			
		||||
			.map(|res| match res.rows_affected() {
 | 
			
		||||
				0 => Err(DatabaseError::DeleteError),
 | 
			
		||||
| 
						 | 
				
			
			@ -71,27 +71,27 @@ impl DbMacroAction {
 | 
			
		|||
 | 
			
		||||
	pub async fn get_relay(
 | 
			
		||||
		&self,
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
	) -> Result<DbRelay, DatabaseError> {
 | 
			
		||||
		DbRelay::get(conn, self.relay_id)
 | 
			
		||||
		DbRelay::get(tx, self.relay_id)
 | 
			
		||||
			.await?
 | 
			
		||||
			.ok_or(DatabaseError::NotFound)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get_schedule(
 | 
			
		||||
		&self,
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
	) -> Result<DbSchedule, DatabaseError> {
 | 
			
		||||
		DbSchedule::get(conn, self.schedule_id)
 | 
			
		||||
		DbSchedule::get(tx, self.schedule_id)
 | 
			
		||||
			.await?
 | 
			
		||||
			.ok_or(DatabaseError::NotFound)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get_macro(
 | 
			
		||||
		&self,
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
	) -> Result<DbMacro, DatabaseError> {
 | 
			
		||||
		DbMacro::get(conn, self.macro_id)
 | 
			
		||||
		DbMacro::get(tx, self.macro_id)
 | 
			
		||||
			.await?
 | 
			
		||||
			.ok_or(DatabaseError::NotFound)
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -46,10 +46,12 @@ pub async fn init(db: &str) -> Result<Pool<Sqlite>, EmgauwaError> {
 | 
			
		|||
 | 
			
		||||
	run_migrations(&pool).await?;
 | 
			
		||||
 | 
			
		||||
	let mut pool_conn = pool.acquire().await?;
 | 
			
		||||
	let mut tx = pool.begin().await?;
 | 
			
		||||
 | 
			
		||||
	DbSchedule::get_on(&mut pool_conn).await?;
 | 
			
		||||
	DbSchedule::get_off(&mut pool_conn).await?;
 | 
			
		||||
	DbSchedule::get_on(&mut tx).await?;
 | 
			
		||||
	DbSchedule::get_off(&mut tx).await?;
 | 
			
		||||
 | 
			
		||||
	tx.commit().await?;
 | 
			
		||||
 | 
			
		||||
	Ok(pool)
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,7 +1,7 @@
 | 
			
		|||
use std::ops::DerefMut;
 | 
			
		||||
 | 
			
		||||
use serde_derive::{Deserialize, Serialize};
 | 
			
		||||
use sqlx::pool::PoolConnection;
 | 
			
		||||
use sqlx::Transaction;
 | 
			
		||||
use sqlx::Sqlite;
 | 
			
		||||
 | 
			
		||||
use crate::db::{DbController, DbJunctionRelaySchedule, DbJunctionTag, DbSchedule, DbTag};
 | 
			
		||||
| 
						 | 
				
			
			@ -20,25 +20,25 @@ pub struct DbRelay {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
impl DbRelay {
 | 
			
		||||
	pub async fn get_all(conn: &mut PoolConnection<Sqlite>) -> Result<Vec<DbRelay>, DatabaseError> {
 | 
			
		||||
	pub async fn get_all(tx: &mut Transaction<'_, Sqlite>) -> Result<Vec<DbRelay>, DatabaseError> {
 | 
			
		||||
		sqlx::query_as!(DbRelay, "SELECT * FROM relays")
 | 
			
		||||
			.fetch_all(conn.deref_mut())
 | 
			
		||||
			.fetch_all(tx.deref_mut())
 | 
			
		||||
			.await
 | 
			
		||||
			.map_err(DatabaseError::from)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		id: i64,
 | 
			
		||||
	) -> Result<Option<DbRelay>, DatabaseError> {
 | 
			
		||||
		sqlx::query_as!(DbRelay, "SELECT * FROM relays WHERE id = ?", id)
 | 
			
		||||
			.fetch_optional(conn.deref_mut())
 | 
			
		||||
			.fetch_optional(tx.deref_mut())
 | 
			
		||||
			.await
 | 
			
		||||
			.map_err(DatabaseError::from)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get_by_controller_and_num(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		controller: &DbController,
 | 
			
		||||
		number: i64,
 | 
			
		||||
	) -> Result<Option<DbRelay>, DatabaseError> {
 | 
			
		||||
| 
						 | 
				
			
			@ -48,38 +48,38 @@ impl DbRelay {
 | 
			
		|||
			controller.id,
 | 
			
		||||
			number
 | 
			
		||||
		)
 | 
			
		||||
		.fetch_optional(conn.deref_mut())
 | 
			
		||||
		.fetch_optional(tx.deref_mut())
 | 
			
		||||
		.await
 | 
			
		||||
		.map_err(DatabaseError::from)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get_by_controller_and_num_or_create(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		controller: &DbController,
 | 
			
		||||
		number: i64,
 | 
			
		||||
		new_name: &str,
 | 
			
		||||
	) -> Result<(DbRelay, bool), DatabaseError> {
 | 
			
		||||
		match DbRelay::get_by_controller_and_num(conn, controller, number).await? {
 | 
			
		||||
		match DbRelay::get_by_controller_and_num(tx, controller, number).await? {
 | 
			
		||||
			Some(relay) => Ok((relay, false)),
 | 
			
		||||
			None => {
 | 
			
		||||
				let relay = DbRelay::create(conn, new_name, number, controller).await?;
 | 
			
		||||
				let relay = DbRelay::create(tx, new_name, number, controller).await?;
 | 
			
		||||
				Ok((relay, true))
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get_by_tag(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		tag: &DbTag,
 | 
			
		||||
	) -> Result<Vec<DbRelay>, DatabaseError> {
 | 
			
		||||
		sqlx::query_as!(DbRelay, "SELECT relay.* FROM relays AS relay INNER JOIN junction_tag ON junction_tag.relay_id = relay.id WHERE junction_tag.tag_id = ?", tag.id)
 | 
			
		||||
			.fetch_all(conn.deref_mut())
 | 
			
		||||
			.fetch_all(tx.deref_mut())
 | 
			
		||||
			.await
 | 
			
		||||
			.map_err(DatabaseError::from)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn create(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		new_name: &str,
 | 
			
		||||
		new_number: i64,
 | 
			
		||||
		new_controller: &DbController,
 | 
			
		||||
| 
						 | 
				
			
			@ -91,14 +91,14 @@ impl DbRelay {
 | 
			
		|||
			new_number,
 | 
			
		||||
			new_controller.id,
 | 
			
		||||
		)
 | 
			
		||||
		.fetch_optional(conn.deref_mut())
 | 
			
		||||
		.fetch_optional(tx.deref_mut())
 | 
			
		||||
		.await?
 | 
			
		||||
		.ok_or(DatabaseError::InsertGetError)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn delete(&self, conn: &mut PoolConnection<Sqlite>) -> Result<(), DatabaseError> {
 | 
			
		||||
	pub async fn delete(&self, tx: &mut Transaction<'_, Sqlite>) -> Result<(), DatabaseError> {
 | 
			
		||||
		sqlx::query!("DELETE FROM relays WHERE id = ?", self.id)
 | 
			
		||||
			.execute(conn.deref_mut())
 | 
			
		||||
			.execute(tx.deref_mut())
 | 
			
		||||
			.await
 | 
			
		||||
			.map(|res| match res.rows_affected() {
 | 
			
		||||
				0 => Err(DatabaseError::DeleteError),
 | 
			
		||||
| 
						 | 
				
			
			@ -108,68 +108,68 @@ impl DbRelay {
 | 
			
		|||
 | 
			
		||||
	pub async fn update(
 | 
			
		||||
		&self,
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		new_name: &str,
 | 
			
		||||
	) -> Result<DbRelay, DatabaseError> {
 | 
			
		||||
		sqlx::query!("UPDATE relays SET name = ? WHERE id = ?", new_name, self.id,)
 | 
			
		||||
			.execute(conn.deref_mut())
 | 
			
		||||
			.execute(tx.deref_mut())
 | 
			
		||||
			.await?;
 | 
			
		||||
 | 
			
		||||
		DbRelay::get(conn, self.id)
 | 
			
		||||
		DbRelay::get(tx, self.id)
 | 
			
		||||
			.await?
 | 
			
		||||
			.ok_or(DatabaseError::UpdateGetError)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get_controller(
 | 
			
		||||
		&self,
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
	) -> Result<DbController, DatabaseError> {
 | 
			
		||||
		DbController::get(conn, self.controller_id)
 | 
			
		||||
		DbController::get(tx, self.controller_id)
 | 
			
		||||
			.await?
 | 
			
		||||
			.ok_or(DatabaseError::NotFound)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get_tags(
 | 
			
		||||
		&self,
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
	) -> Result<Vec<String>, DatabaseError> {
 | 
			
		||||
		sqlx::query_scalar!("SELECT tag FROM tags INNER JOIN junction_tag ON junction_tag.tag_id = tags.id WHERE junction_tag.relay_id = ?", self.id)
 | 
			
		||||
			.fetch_all(conn.deref_mut())
 | 
			
		||||
			.fetch_all(tx.deref_mut())
 | 
			
		||||
			.await
 | 
			
		||||
			.map_err(DatabaseError::from)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn set_tags(
 | 
			
		||||
		&self,
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		new_tags: &[String],
 | 
			
		||||
	) -> Result<(), DatabaseError> {
 | 
			
		||||
		sqlx::query!("DELETE FROM junction_tag WHERE relay_id = ?", self.id)
 | 
			
		||||
			.execute(conn.deref_mut())
 | 
			
		||||
			.execute(tx.deref_mut())
 | 
			
		||||
			.await?;
 | 
			
		||||
 | 
			
		||||
		for new_tag in new_tags {
 | 
			
		||||
			let tag: DbTag = DbTag::get_by_tag_or_create(conn, new_tag).await?;
 | 
			
		||||
			DbJunctionTag::link_relay(conn, &tag, self).await?;
 | 
			
		||||
			let tag: DbTag = DbTag::get_by_tag_or_create(tx, new_tag).await?;
 | 
			
		||||
			DbJunctionTag::link_relay(tx, &tag, self).await?;
 | 
			
		||||
		}
 | 
			
		||||
		Ok(())
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn reload(
 | 
			
		||||
		&self,
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
	) -> Result<DbRelay, DatabaseError> {
 | 
			
		||||
		Self::get(conn, self.id)
 | 
			
		||||
		Self::get(tx, self.id)
 | 
			
		||||
			.await?
 | 
			
		||||
			.ok_or(DatabaseError::NotFound)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get_active_schedule(
 | 
			
		||||
		&self,
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
	) -> Result<DbSchedule, DatabaseError> {
 | 
			
		||||
		let weekday = utils::get_weekday();
 | 
			
		||||
		DbJunctionRelaySchedule::get_schedule(conn, self, weekday as Weekday)
 | 
			
		||||
		DbJunctionRelaySchedule::get_schedule(tx, self, weekday as Weekday)
 | 
			
		||||
			.await?
 | 
			
		||||
			.ok_or(DatabaseError::NotFound)
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -3,7 +3,7 @@ use std::ops::DerefMut;
 | 
			
		|||
 | 
			
		||||
use chrono::NaiveTime;
 | 
			
		||||
use serde_derive::{Deserialize, Serialize};
 | 
			
		||||
use sqlx::pool::PoolConnection;
 | 
			
		||||
use sqlx::Transaction;
 | 
			
		||||
use sqlx::Sqlite;
 | 
			
		||||
 | 
			
		||||
use crate::db::model_utils::Period;
 | 
			
		||||
| 
						 | 
				
			
			@ -26,26 +26,26 @@ pub struct DbPeriods(pub Vec<Period>);
 | 
			
		|||
 | 
			
		||||
impl DbSchedule {
 | 
			
		||||
	pub async fn get_all(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
	) -> Result<Vec<DbSchedule>, DatabaseError> {
 | 
			
		||||
		sqlx::query_as!(DbSchedule, "SELECT * FROM schedules")
 | 
			
		||||
			.fetch_all(conn.deref_mut())
 | 
			
		||||
			.fetch_all(tx.deref_mut())
 | 
			
		||||
			.await
 | 
			
		||||
			.map_err(DatabaseError::from)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		id: i64,
 | 
			
		||||
	) -> Result<Option<DbSchedule>, DatabaseError> {
 | 
			
		||||
		sqlx::query_as!(DbSchedule, "SELECT * FROM schedules WHERE id = ?", id)
 | 
			
		||||
			.fetch_optional(conn.deref_mut())
 | 
			
		||||
			.fetch_optional(tx.deref_mut())
 | 
			
		||||
			.await
 | 
			
		||||
			.map_err(DatabaseError::from)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get_by_uid(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		filter_uid: &ScheduleUid,
 | 
			
		||||
	) -> Result<Option<DbSchedule>, DatabaseError> {
 | 
			
		||||
		sqlx::query_as!(
 | 
			
		||||
| 
						 | 
				
			
			@ -53,23 +53,23 @@ impl DbSchedule {
 | 
			
		|||
			"SELECT * FROM schedules WHERE uid = ?",
 | 
			
		||||
			filter_uid
 | 
			
		||||
		)
 | 
			
		||||
		.fetch_optional(conn.deref_mut())
 | 
			
		||||
		.fetch_optional(tx.deref_mut())
 | 
			
		||||
		.await
 | 
			
		||||
		.map_err(DatabaseError::from)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get_by_tag(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		tag: &DbTag,
 | 
			
		||||
	) -> Result<Vec<DbSchedule>, DatabaseError> {
 | 
			
		||||
		sqlx::query_as!(DbSchedule, "SELECT schedule.* FROM schedules AS schedule INNER JOIN junction_tag ON junction_tag.schedule_id = schedule.id WHERE junction_tag.tag_id = ?", tag.id)
 | 
			
		||||
			.fetch_all(conn.deref_mut())
 | 
			
		||||
			.fetch_all(tx.deref_mut())
 | 
			
		||||
			.await
 | 
			
		||||
			.map_err(DatabaseError::from)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn delete_by_uid(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		filter_uid: ScheduleUid,
 | 
			
		||||
	) -> Result<(), DatabaseError> {
 | 
			
		||||
		let filter_uid = match filter_uid {
 | 
			
		||||
| 
						 | 
				
			
			@ -79,7 +79,7 @@ impl DbSchedule {
 | 
			
		|||
		}?;
 | 
			
		||||
 | 
			
		||||
		if sqlx::query_scalar!("SELECT 1 FROM schedules WHERE uid = ?", filter_uid)
 | 
			
		||||
			.fetch_optional(conn.deref_mut())
 | 
			
		||||
			.fetch_optional(tx.deref_mut())
 | 
			
		||||
			.await?
 | 
			
		||||
			.is_none()
 | 
			
		||||
		{
 | 
			
		||||
| 
						 | 
				
			
			@ -87,7 +87,7 @@ impl DbSchedule {
 | 
			
		|||
		}
 | 
			
		||||
 | 
			
		||||
		sqlx::query!("DELETE FROM schedules WHERE uid = ?", filter_uid)
 | 
			
		||||
			.execute(conn.deref_mut())
 | 
			
		||||
			.execute(tx.deref_mut())
 | 
			
		||||
			.await
 | 
			
		||||
			.map(|res| match res.rows_affected() {
 | 
			
		||||
				0 => Err(DatabaseError::DeleteError),
 | 
			
		||||
| 
						 | 
				
			
			@ -96,7 +96,7 @@ impl DbSchedule {
 | 
			
		|||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn create(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		new_uid: ScheduleUid,
 | 
			
		||||
		new_name: &str,
 | 
			
		||||
		new_periods: &DbPeriods,
 | 
			
		||||
| 
						 | 
				
			
			@ -108,45 +108,45 @@ impl DbSchedule {
 | 
			
		|||
			new_name,
 | 
			
		||||
			new_periods,
 | 
			
		||||
		)
 | 
			
		||||
		.fetch_optional(conn.deref_mut())
 | 
			
		||||
		.fetch_optional(tx.deref_mut())
 | 
			
		||||
		.await?
 | 
			
		||||
		.ok_or(DatabaseError::InsertGetError)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get_by_uid_or_create(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		uid: ScheduleUid,
 | 
			
		||||
		name: &str,
 | 
			
		||||
		periods: &DbPeriods,
 | 
			
		||||
	) -> Result<(DbSchedule, bool), DatabaseError> {
 | 
			
		||||
		match DbSchedule::get_by_uid(conn, &uid).await? {
 | 
			
		||||
		match DbSchedule::get_by_uid(tx, &uid).await? {
 | 
			
		||||
			Some(schedule) => Ok((schedule, false)),
 | 
			
		||||
			None => {
 | 
			
		||||
				let schedule = DbSchedule::create(conn, uid, name, periods).await?;
 | 
			
		||||
				let schedule = DbSchedule::create(tx, uid, name, periods).await?;
 | 
			
		||||
				Ok((schedule, true))
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get_on(conn: &mut PoolConnection<Sqlite>) -> Result<DbSchedule, DatabaseError> {
 | 
			
		||||
		if let Some(schedule) = DbSchedule::get_by_uid(conn, &ScheduleUid::On).await? {
 | 
			
		||||
	pub async fn get_on(tx: &mut Transaction<'_, Sqlite>) -> Result<DbSchedule, DatabaseError> {
 | 
			
		||||
		if let Some(schedule) = DbSchedule::get_by_uid(tx, &ScheduleUid::On).await? {
 | 
			
		||||
			return Ok(schedule);
 | 
			
		||||
		}
 | 
			
		||||
		let periods = DbPeriods(vec![Period::new_on()]);
 | 
			
		||||
		Self::create(conn, ScheduleUid::On, "On", &periods).await
 | 
			
		||||
		Self::create(tx, ScheduleUid::On, "On", &periods).await
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get_off(conn: &mut PoolConnection<Sqlite>) -> Result<DbSchedule, DatabaseError> {
 | 
			
		||||
		if let Some(schedule) = DbSchedule::get_by_uid(conn, &ScheduleUid::Off).await? {
 | 
			
		||||
	pub async fn get_off(tx: &mut Transaction<'_, Sqlite>) -> Result<DbSchedule, DatabaseError> {
 | 
			
		||||
		if let Some(schedule) = DbSchedule::get_by_uid(tx, &ScheduleUid::Off).await? {
 | 
			
		||||
			return Ok(schedule);
 | 
			
		||||
		}
 | 
			
		||||
		let periods = DbPeriods(vec![]);
 | 
			
		||||
		Self::create(conn, ScheduleUid::Off, "Off", &periods).await
 | 
			
		||||
		Self::create(tx, ScheduleUid::Off, "Off", &periods).await
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn update(
 | 
			
		||||
		&self,
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		new_name: &str,
 | 
			
		||||
		new_periods: &DbPeriods,
 | 
			
		||||
	) -> Result<DbSchedule, DatabaseError> {
 | 
			
		||||
| 
						 | 
				
			
			@ -162,35 +162,35 @@ impl DbSchedule {
 | 
			
		|||
			new_periods,
 | 
			
		||||
			self.id,
 | 
			
		||||
		)
 | 
			
		||||
		.execute(conn.deref_mut())
 | 
			
		||||
		.execute(tx.deref_mut())
 | 
			
		||||
		.await?;
 | 
			
		||||
 | 
			
		||||
		DbSchedule::get(conn, self.id)
 | 
			
		||||
		DbSchedule::get(tx, self.id)
 | 
			
		||||
			.await?
 | 
			
		||||
			.ok_or(DatabaseError::UpdateGetError)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get_tags(
 | 
			
		||||
		&self,
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
	) -> Result<Vec<String>, DatabaseError> {
 | 
			
		||||
		Ok(sqlx::query_scalar!("SELECT tag FROM tags INNER JOIN junction_tag ON junction_tag.tag_id = tags.id WHERE junction_tag.schedule_id = ?", self.id)
 | 
			
		||||
			.fetch_all(conn.deref_mut())
 | 
			
		||||
			.fetch_all(tx.deref_mut())
 | 
			
		||||
			.await?)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn set_tags(
 | 
			
		||||
		&self,
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		new_tags: &[String],
 | 
			
		||||
	) -> Result<(), DatabaseError> {
 | 
			
		||||
		sqlx::query!("DELETE FROM junction_tag WHERE schedule_id = ?", self.id)
 | 
			
		||||
			.execute(conn.deref_mut())
 | 
			
		||||
			.execute(tx.deref_mut())
 | 
			
		||||
			.await?;
 | 
			
		||||
 | 
			
		||||
		for new_tag in new_tags {
 | 
			
		||||
			let tag: DbTag = DbTag::get_by_tag_or_create(conn, new_tag).await?;
 | 
			
		||||
			DbJunctionTag::link_schedule(conn, &tag, self).await?;
 | 
			
		||||
			let tag: DbTag = DbTag::get_by_tag_or_create(tx, new_tag).await?;
 | 
			
		||||
			DbJunctionTag::link_schedule(tx, &tag, self).await?;
 | 
			
		||||
		}
 | 
			
		||||
		Ok(())
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,7 +1,7 @@
 | 
			
		|||
use std::ops::DerefMut;
 | 
			
		||||
 | 
			
		||||
use serde_derive::Serialize;
 | 
			
		||||
use sqlx::pool::PoolConnection;
 | 
			
		||||
use sqlx::Transaction;
 | 
			
		||||
use sqlx::Sqlite;
 | 
			
		||||
 | 
			
		||||
use crate::errors::DatabaseError;
 | 
			
		||||
| 
						 | 
				
			
			@ -14,7 +14,7 @@ pub struct DbTag {
 | 
			
		|||
 | 
			
		||||
impl DbTag {
 | 
			
		||||
	pub async fn create(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		new_tag: &str,
 | 
			
		||||
	) -> Result<DbTag, DatabaseError> {
 | 
			
		||||
		if new_tag.is_empty() {
 | 
			
		||||
| 
						 | 
				
			
			@ -26,54 +26,54 @@ impl DbTag {
 | 
			
		|||
			"INSERT INTO tags (tag) VALUES (?) RETURNING *",
 | 
			
		||||
			new_tag
 | 
			
		||||
		)
 | 
			
		||||
		.fetch_optional(conn.deref_mut())
 | 
			
		||||
		.fetch_optional(tx.deref_mut())
 | 
			
		||||
		.await?
 | 
			
		||||
		.ok_or(DatabaseError::InsertGetError)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get_all(conn: &mut PoolConnection<Sqlite>) -> Result<Vec<DbTag>, DatabaseError> {
 | 
			
		||||
	pub async fn get_all(tx: &mut Transaction<'_, Sqlite>) -> Result<Vec<DbTag>, DatabaseError> {
 | 
			
		||||
		sqlx::query_as!(DbTag, "SELECT * FROM tags")
 | 
			
		||||
			.fetch_all(conn.deref_mut())
 | 
			
		||||
			.fetch_all(tx.deref_mut())
 | 
			
		||||
			.await
 | 
			
		||||
			.map_err(DatabaseError::from)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		id: i64,
 | 
			
		||||
	) -> Result<Option<DbTag>, DatabaseError> {
 | 
			
		||||
		sqlx::query_as!(DbTag, "SELECT * FROM tags WHERE id = ?", id)
 | 
			
		||||
			.fetch_optional(conn.deref_mut())
 | 
			
		||||
			.fetch_optional(tx.deref_mut())
 | 
			
		||||
			.await
 | 
			
		||||
			.map_err(DatabaseError::from)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get_by_tag_or_create(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		target_tag: &str,
 | 
			
		||||
	) -> Result<DbTag, DatabaseError> {
 | 
			
		||||
		match DbTag::get_by_tag(conn, target_tag).await? {
 | 
			
		||||
		match DbTag::get_by_tag(tx, target_tag).await? {
 | 
			
		||||
			Some(tag) => Ok(tag),
 | 
			
		||||
			None => DbTag::create(conn, target_tag).await,
 | 
			
		||||
			None => DbTag::create(tx, target_tag).await,
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get_by_tag(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		target_tag: &str,
 | 
			
		||||
	) -> Result<Option<DbTag>, DatabaseError> {
 | 
			
		||||
		sqlx::query_as!(DbTag, "SELECT * FROM tags WHERE tag = ?", target_tag)
 | 
			
		||||
			.fetch_optional(conn.deref_mut())
 | 
			
		||||
			.fetch_optional(tx.deref_mut())
 | 
			
		||||
			.await
 | 
			
		||||
			.map_err(DatabaseError::from)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn delete_by_tag(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		filter_tag: &str,
 | 
			
		||||
	) -> Result<(), DatabaseError> {
 | 
			
		||||
		if sqlx::query_scalar!("SELECT 1 FROM tags WHERE tag = ?", filter_tag)
 | 
			
		||||
			.fetch_optional(conn.deref_mut())
 | 
			
		||||
			.fetch_optional(tx.deref_mut())
 | 
			
		||||
			.await?
 | 
			
		||||
			.is_none()
 | 
			
		||||
		{
 | 
			
		||||
| 
						 | 
				
			
			@ -81,7 +81,7 @@ impl DbTag {
 | 
			
		|||
		}
 | 
			
		||||
 | 
			
		||||
		sqlx::query!("DELETE FROM tags WHERE tag = ?", filter_tag)
 | 
			
		||||
			.execute(conn.deref_mut())
 | 
			
		||||
			.execute(tx.deref_mut())
 | 
			
		||||
			.await
 | 
			
		||||
			.map(|res| match res.rows_affected() {
 | 
			
		||||
				0 => Err(DatabaseError::DeleteError),
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -4,7 +4,7 @@ use actix::MessageResponse;
 | 
			
		|||
use chrono::NaiveTime;
 | 
			
		||||
use futures::executor::block_on;
 | 
			
		||||
use serde_derive::{Deserialize, Serialize};
 | 
			
		||||
use sqlx::pool::PoolConnection;
 | 
			
		||||
use sqlx::Transaction;
 | 
			
		||||
use sqlx::Sqlite;
 | 
			
		||||
 | 
			
		||||
use crate::db::DbController;
 | 
			
		||||
| 
						 | 
				
			
			@ -24,16 +24,16 @@ impl FromDbModel for Controller {
 | 
			
		|||
	type DbModelCache = Vec<Relay>;
 | 
			
		||||
 | 
			
		||||
	fn from_db_model(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		db_model: Self::DbModel,
 | 
			
		||||
	) -> Result<Self, DatabaseError> {
 | 
			
		||||
		let relays_db = block_on(db_model.get_relays(conn))?;
 | 
			
		||||
		let cache = convert_db_list_cache(conn, relays_db, db_model.clone())?;
 | 
			
		||||
		Self::from_db_model_cache(conn, db_model, cache)
 | 
			
		||||
		let relays_db = block_on(db_model.get_relays(tx))?;
 | 
			
		||||
		let cache = convert_db_list_cache(tx, relays_db, db_model.clone())?;
 | 
			
		||||
		Self::from_db_model_cache(tx, db_model, cache)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	fn from_db_model_cache(
 | 
			
		||||
		_conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		_tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		db_model: Self::DbModel,
 | 
			
		||||
		cache: Self::DbModelCache,
 | 
			
		||||
	) -> Result<Self, DatabaseError> {
 | 
			
		||||
| 
						 | 
				
			
			@ -45,10 +45,10 @@ impl FromDbModel for Controller {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
impl Controller {
 | 
			
		||||
	pub fn reload(&mut self, conn: &mut PoolConnection<Sqlite>) -> Result<(), EmgauwaError> {
 | 
			
		||||
		self.c = block_on(self.c.reload(conn))?;
 | 
			
		||||
	pub fn reload(&mut self, tx: &mut Transaction<'_, Sqlite>) -> Result<(), EmgauwaError> {
 | 
			
		||||
		self.c = block_on(self.c.reload(tx))?;
 | 
			
		||||
		for relay in &mut self.relays {
 | 
			
		||||
			relay.reload(conn)?;
 | 
			
		||||
			relay.reload(tx)?;
 | 
			
		||||
		}
 | 
			
		||||
		Ok(())
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,6 +1,6 @@
 | 
			
		|||
use futures::executor::block_on;
 | 
			
		||||
use serde_derive::{Deserialize, Serialize};
 | 
			
		||||
use sqlx::pool::PoolConnection;
 | 
			
		||||
use sqlx::Transaction;
 | 
			
		||||
use sqlx::Sqlite;
 | 
			
		||||
 | 
			
		||||
use crate::db::DbMacro;
 | 
			
		||||
| 
						 | 
				
			
			@ -19,19 +19,19 @@ impl FromDbModel for Macro {
 | 
			
		|||
	type DbModelCache = ();
 | 
			
		||||
 | 
			
		||||
	fn from_db_model(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		db_model: Self::DbModel,
 | 
			
		||||
	) -> Result<Self, DatabaseError> {
 | 
			
		||||
		Self::from_db_model_cache(conn, db_model, ())
 | 
			
		||||
		Self::from_db_model_cache(tx, db_model, ())
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	fn from_db_model_cache(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		db_model: Self::DbModel,
 | 
			
		||||
		_cache: Self::DbModelCache,
 | 
			
		||||
	) -> Result<Self, DatabaseError> {
 | 
			
		||||
		let actions_db = block_on(db_model.get_actions(conn))?;
 | 
			
		||||
		let actions: Vec<MacroAction> = convert_db_list(conn, actions_db)?;
 | 
			
		||||
		let actions_db = block_on(db_model.get_actions(tx))?;
 | 
			
		||||
		let actions: Vec<MacroAction> = convert_db_list(tx, actions_db)?;
 | 
			
		||||
 | 
			
		||||
		Ok(Macro {
 | 
			
		||||
			m: db_model,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,6 +1,6 @@
 | 
			
		|||
use futures::executor::block_on;
 | 
			
		||||
use serde_derive::{Deserialize, Serialize};
 | 
			
		||||
use sqlx::pool::PoolConnection;
 | 
			
		||||
use sqlx::Transaction;
 | 
			
		||||
use sqlx::Sqlite;
 | 
			
		||||
 | 
			
		||||
use crate::db::{DbJunctionRelaySchedule, DbMacroAction};
 | 
			
		||||
| 
						 | 
				
			
			@ -19,22 +19,22 @@ impl FromDbModel for MacroAction {
 | 
			
		|||
	type DbModelCache = ();
 | 
			
		||||
 | 
			
		||||
	fn from_db_model(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		db_model: Self::DbModel,
 | 
			
		||||
	) -> Result<Self, DatabaseError> {
 | 
			
		||||
		Self::from_db_model_cache(conn, db_model, ())
 | 
			
		||||
		Self::from_db_model_cache(tx, db_model, ())
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	fn from_db_model_cache(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		db_model: Self::DbModel,
 | 
			
		||||
		_cache: Self::DbModelCache,
 | 
			
		||||
	) -> Result<Self, DatabaseError> {
 | 
			
		||||
		let schedule_db = block_on(db_model.get_schedule(conn))?;
 | 
			
		||||
		let schedule = Schedule::from_db_model(conn, schedule_db)?;
 | 
			
		||||
		let schedule_db = block_on(db_model.get_schedule(tx))?;
 | 
			
		||||
		let schedule = Schedule::from_db_model(tx, schedule_db)?;
 | 
			
		||||
 | 
			
		||||
		let relay_db = block_on(db_model.get_relay(conn))?;
 | 
			
		||||
		let relay = Relay::from_db_model(conn, relay_db)?;
 | 
			
		||||
		let relay_db = block_on(db_model.get_relay(tx))?;
 | 
			
		||||
		let relay = Relay::from_db_model(tx, relay_db)?;
 | 
			
		||||
 | 
			
		||||
		let weekday = db_model.weekday;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -47,8 +47,8 @@ impl FromDbModel for MacroAction {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
impl MacroAction {
 | 
			
		||||
	pub async fn execute(&self, conn: &mut PoolConnection<Sqlite>) -> Result<(), EmgauwaError> {
 | 
			
		||||
		DbJunctionRelaySchedule::set_schedule(conn, &self.relay.r, &self.schedule.s, self.weekday)
 | 
			
		||||
	pub async fn execute(&self, tx: &mut Transaction<'_, Sqlite>) -> Result<(), EmgauwaError> {
 | 
			
		||||
		DbJunctionRelaySchedule::set_schedule(tx, &self.relay.r, &self.schedule.s, self.weekday)
 | 
			
		||||
			.await?;
 | 
			
		||||
		Ok(())
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -10,7 +10,7 @@ pub use macro_action::MacroAction;
 | 
			
		|||
pub use r#macro::Macro;
 | 
			
		||||
pub use relay::Relay;
 | 
			
		||||
pub use schedule::Schedule;
 | 
			
		||||
use sqlx::pool::PoolConnection;
 | 
			
		||||
use sqlx::Transaction;
 | 
			
		||||
use sqlx::Sqlite;
 | 
			
		||||
pub use tag::Tag;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -21,14 +21,14 @@ pub trait FromDbModel {
 | 
			
		|||
	type DbModelCache: Clone;
 | 
			
		||||
 | 
			
		||||
	fn from_db_model(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		db_model: Self::DbModel,
 | 
			
		||||
	) -> Result<Self, DatabaseError>
 | 
			
		||||
	where
 | 
			
		||||
		Self: Sized;
 | 
			
		||||
 | 
			
		||||
	fn from_db_model_cache(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		db_model: Self::DbModel,
 | 
			
		||||
		cache: Self::DbModelCache,
 | 
			
		||||
	) -> Result<Self, DatabaseError>
 | 
			
		||||
| 
						 | 
				
			
			@ -37,15 +37,15 @@ pub trait FromDbModel {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
fn convert_db_list_generic<T: FromDbModel>(
 | 
			
		||||
	conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
	tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
	db_models: Vec<T::DbModel>,
 | 
			
		||||
	cache: Option<T::DbModelCache>,
 | 
			
		||||
) -> Result<Vec<T>, DatabaseError> {
 | 
			
		||||
	let mut result: Vec<T> = Vec::new();
 | 
			
		||||
	for db_model in db_models {
 | 
			
		||||
		let new = match &cache {
 | 
			
		||||
			Some(c) => T::from_db_model_cache(conn, db_model, c.clone()),
 | 
			
		||||
			None => T::from_db_model(conn, db_model),
 | 
			
		||||
			Some(c) => T::from_db_model_cache(tx, db_model, c.clone()),
 | 
			
		||||
			None => T::from_db_model(tx, db_model),
 | 
			
		||||
		}?;
 | 
			
		||||
		result.push(new);
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			@ -53,16 +53,16 @@ fn convert_db_list_generic<T: FromDbModel>(
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
pub fn convert_db_list<T: FromDbModel>(
 | 
			
		||||
	conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
	tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
	db_models: Vec<T::DbModel>,
 | 
			
		||||
) -> Result<Vec<T>, DatabaseError> {
 | 
			
		||||
	convert_db_list_generic(conn, db_models, None)
 | 
			
		||||
	convert_db_list_generic(tx, db_models, None)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub fn convert_db_list_cache<T: FromDbModel>(
 | 
			
		||||
	conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
	tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
	db_models: Vec<T::DbModel>,
 | 
			
		||||
	cache: T::DbModelCache,
 | 
			
		||||
) -> Result<Vec<T>, DatabaseError> {
 | 
			
		||||
	convert_db_list_generic(conn, db_models, Some(cache))
 | 
			
		||||
	convert_db_list_generic(tx, db_models, Some(cache))
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -3,7 +3,7 @@ use std::time::Instant;
 | 
			
		|||
use chrono::NaiveTime;
 | 
			
		||||
use futures::executor::block_on;
 | 
			
		||||
use serde_derive::{Deserialize, Serialize};
 | 
			
		||||
use sqlx::pool::PoolConnection;
 | 
			
		||||
use sqlx::Transaction;
 | 
			
		||||
use sqlx::Sqlite;
 | 
			
		||||
 | 
			
		||||
use crate::db::{DbController, DbJunctionRelaySchedule, DbRelay, DbSchedule};
 | 
			
		||||
| 
						 | 
				
			
			@ -32,23 +32,23 @@ impl FromDbModel for Relay {
 | 
			
		|||
	type DbModelCache = DbController;
 | 
			
		||||
 | 
			
		||||
	fn from_db_model(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		db_model: Self::DbModel,
 | 
			
		||||
	) -> Result<Self, DatabaseError> {
 | 
			
		||||
		let cache = block_on(db_model.get_controller(conn))?;
 | 
			
		||||
		Self::from_db_model_cache(conn, db_model, cache)
 | 
			
		||||
		let cache = block_on(db_model.get_controller(tx))?;
 | 
			
		||||
		Self::from_db_model_cache(tx, db_model, cache)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	fn from_db_model_cache(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		db_model: Self::DbModel,
 | 
			
		||||
		cache: Self::DbModelCache,
 | 
			
		||||
	) -> Result<Self, DatabaseError> {
 | 
			
		||||
		let tags = block_on(db_model.get_tags(conn))?;
 | 
			
		||||
		let tags = block_on(db_model.get_tags(tx))?;
 | 
			
		||||
		let controller_id = cache.uid.clone();
 | 
			
		||||
 | 
			
		||||
		let schedules = block_on(DbJunctionRelaySchedule::get_schedules(conn, &db_model))?;
 | 
			
		||||
		let active_schedule = block_on(db_model.get_active_schedule(conn))?;
 | 
			
		||||
		let schedules = block_on(DbJunctionRelaySchedule::get_schedules(tx, &db_model))?;
 | 
			
		||||
		let active_schedule = block_on(db_model.get_active_schedule(tx))?;
 | 
			
		||||
 | 
			
		||||
		let is_on = None;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -66,19 +66,19 @@ impl FromDbModel for Relay {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
impl Relay {
 | 
			
		||||
	pub fn reload(&mut self, conn: &mut PoolConnection<Sqlite>) -> Result<(), DatabaseError> {
 | 
			
		||||
		self.r = block_on(self.r.reload(conn))?;
 | 
			
		||||
		self.schedules = block_on(DbJunctionRelaySchedule::get_schedules(conn, &self.r))?;
 | 
			
		||||
		self.reload_active_schedule(conn)?;
 | 
			
		||||
	pub fn reload(&mut self, tx: &mut Transaction<'_, Sqlite>) -> Result<(), DatabaseError> {
 | 
			
		||||
		self.r = block_on(self.r.reload(tx))?;
 | 
			
		||||
		self.schedules = block_on(DbJunctionRelaySchedule::get_schedules(tx, &self.r))?;
 | 
			
		||||
		self.reload_active_schedule(tx)?;
 | 
			
		||||
 | 
			
		||||
		Ok(())
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub fn reload_active_schedule(
 | 
			
		||||
		&mut self,
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
	) -> Result<(), DatabaseError> {
 | 
			
		||||
		self.active_schedule = block_on(self.r.get_active_schedule(conn))?;
 | 
			
		||||
		self.active_schedule = block_on(self.r.get_active_schedule(tx))?;
 | 
			
		||||
		Ok(())
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,6 +1,6 @@
 | 
			
		|||
use futures::executor::block_on;
 | 
			
		||||
use serde_derive::{Deserialize, Serialize};
 | 
			
		||||
use sqlx::pool::PoolConnection;
 | 
			
		||||
use sqlx::Transaction;
 | 
			
		||||
use sqlx::Sqlite;
 | 
			
		||||
 | 
			
		||||
use crate::db::DbSchedule;
 | 
			
		||||
| 
						 | 
				
			
			@ -19,15 +19,15 @@ impl FromDbModel for Schedule {
 | 
			
		|||
	type DbModelCache = Vec<String>;
 | 
			
		||||
 | 
			
		||||
	fn from_db_model(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		db_model: Self::DbModel,
 | 
			
		||||
	) -> Result<Self, DatabaseError> {
 | 
			
		||||
		let cache = block_on(db_model.get_tags(conn))?;
 | 
			
		||||
		Self::from_db_model_cache(conn, db_model, cache)
 | 
			
		||||
		let cache = block_on(db_model.get_tags(tx))?;
 | 
			
		||||
		Self::from_db_model_cache(tx, db_model, cache)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	fn from_db_model_cache(
 | 
			
		||||
		_conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		_tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		db_model: Self::DbModel,
 | 
			
		||||
		cache: Self::DbModelCache,
 | 
			
		||||
	) -> Result<Self, DatabaseError> {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,7 +1,7 @@
 | 
			
		|||
use actix::MessageResponse;
 | 
			
		||||
use futures::executor::block_on;
 | 
			
		||||
use serde_derive::{Deserialize, Serialize};
 | 
			
		||||
use sqlx::pool::PoolConnection;
 | 
			
		||||
use sqlx::Transaction;
 | 
			
		||||
use sqlx::Sqlite;
 | 
			
		||||
 | 
			
		||||
use crate::db::{DbRelay, DbSchedule, DbTag};
 | 
			
		||||
| 
						 | 
				
			
			@ -20,21 +20,21 @@ impl FromDbModel for Tag {
 | 
			
		|||
	type DbModelCache = (Vec<Relay>, Vec<Schedule>);
 | 
			
		||||
 | 
			
		||||
	fn from_db_model(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		db_model: Self::DbModel,
 | 
			
		||||
	) -> Result<Self, DatabaseError> {
 | 
			
		||||
		let db_schedules = block_on(DbSchedule::get_by_tag(conn, &db_model))?;
 | 
			
		||||
		let schedules: Vec<Schedule> = convert_db_list(conn, db_schedules)?;
 | 
			
		||||
		let db_schedules = block_on(DbSchedule::get_by_tag(tx, &db_model))?;
 | 
			
		||||
		let schedules: Vec<Schedule> = convert_db_list(tx, db_schedules)?;
 | 
			
		||||
 | 
			
		||||
		let db_relays = block_on(DbRelay::get_by_tag(conn, &db_model))?;
 | 
			
		||||
		let relays: Vec<Relay> = convert_db_list(conn, db_relays)?;
 | 
			
		||||
		let db_relays = block_on(DbRelay::get_by_tag(tx, &db_model))?;
 | 
			
		||||
		let relays: Vec<Relay> = convert_db_list(tx, db_relays)?;
 | 
			
		||||
 | 
			
		||||
		let cache = (relays, schedules);
 | 
			
		||||
		Self::from_db_model_cache(conn, db_model, cache)
 | 
			
		||||
		Self::from_db_model_cache(tx, db_model, cache)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	fn from_db_model_cache(
 | 
			
		||||
		_conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		_tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
		db_model: Self::DbModel,
 | 
			
		||||
		cache: Self::DbModelCache,
 | 
			
		||||
	) -> Result<Self, DatabaseError> {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,5 +1,5 @@
 | 
			
		|||
use serde_derive::{Deserialize, Serialize};
 | 
			
		||||
use sqlx::pool::PoolConnection;
 | 
			
		||||
use sqlx::Transaction;
 | 
			
		||||
use sqlx::Sqlite;
 | 
			
		||||
 | 
			
		||||
use crate::db::{DbPeriods, DbSchedule};
 | 
			
		||||
| 
						 | 
				
			
			@ -86,9 +86,9 @@ pub struct RequestMacroExecute {
 | 
			
		|||
impl RequestScheduleId {
 | 
			
		||||
	pub async fn get_schedule(
 | 
			
		||||
		&self,
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tx: &mut Transaction<'_, Sqlite>,
 | 
			
		||||
	) -> Result<DbSchedule, DatabaseError> {
 | 
			
		||||
		DbSchedule::get_by_uid(conn, &self.id)
 | 
			
		||||
		DbSchedule::get_by_uid(tx, &self.id)
 | 
			
		||||
			.await?
 | 
			
		||||
			.ok_or(DatabaseError::NotFound)
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue