Migrate to sqlx
This commit is contained in:
parent
bd44dc3183
commit
f3d08aab80
19 changed files with 1488 additions and 588 deletions
|
@ -2,15 +2,16 @@ use actix_web::http::StatusCode;
|
|||
use actix_web::HttpResponse;
|
||||
use serde::ser::SerializeStruct;
|
||||
use serde::{Serialize, Serializer};
|
||||
use sqlx::Error;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum DatabaseError {
|
||||
DeleteError,
|
||||
InsertError(diesel::result::Error),
|
||||
InsertError,
|
||||
InsertGetError,
|
||||
NotFound,
|
||||
Protected,
|
||||
UpdateError(diesel::result::Error),
|
||||
UpdateError,
|
||||
Unknown,
|
||||
}
|
||||
|
||||
|
@ -40,14 +41,14 @@ impl Serialize for DatabaseError {
|
|||
impl From<&DatabaseError> for String {
|
||||
fn from(err: &DatabaseError) -> Self {
|
||||
match err {
|
||||
DatabaseError::InsertError(_) => String::from("error on inserting into database"),
|
||||
DatabaseError::InsertError => String::from("error on inserting into database"),
|
||||
DatabaseError::InsertGetError => {
|
||||
String::from("error on retrieving new entry from database (your entry was saved)")
|
||||
}
|
||||
DatabaseError::NotFound => String::from("model was not found in database"),
|
||||
DatabaseError::DeleteError => String::from("error on deleting from database"),
|
||||
DatabaseError::Protected => String::from("model is protected"),
|
||||
DatabaseError::UpdateError(_) => String::from("error on updating the model"),
|
||||
DatabaseError::UpdateError => String::from("error on updating the model"),
|
||||
DatabaseError::Unknown => String::from("unknown error"),
|
||||
}
|
||||
}
|
||||
|
@ -58,3 +59,12 @@ impl From<DatabaseError> for HttpResponse {
|
|||
HttpResponse::build(err.get_code()).json(err)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Error> for DatabaseError {
|
||||
fn from(value: Error) -> Self {
|
||||
match value {
|
||||
Error::RowNotFound => DatabaseError::NotFound,
|
||||
_ => DatabaseError::Unknown,
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,14 +1,13 @@
|
|||
use crate::db::models::Periods;
|
||||
use chrono::{NaiveTime, Timelike};
|
||||
use diesel::deserialize::FromSql;
|
||||
use diesel::serialize::{IsNull, Output, ToSql};
|
||||
use diesel::sql_types::Binary;
|
||||
use diesel::sqlite::Sqlite;
|
||||
use diesel::{deserialize, serialize};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sqlx::{Decode, Encode, Sqlite, Type};
|
||||
use sqlx::database::HasArguments;
|
||||
use sqlx::encode::IsNull;
|
||||
use sqlx::error::BoxDynError;
|
||||
use sqlx::sqlite::{SqliteTypeInfo, SqliteValueRef};
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, AsExpression, FromSqlRow, PartialEq, Clone)]
|
||||
#[diesel(sql_type = Binary)]
|
||||
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
|
||||
pub struct Period {
|
||||
#[serde(with = "period_format")]
|
||||
pub start: NaiveTime,
|
||||
|
@ -52,13 +51,81 @@ impl Period {
|
|||
}
|
||||
}
|
||||
|
||||
impl ToSql<Binary, Sqlite> for Periods
|
||||
where
|
||||
Vec<u8>: ToSql<Binary, Sqlite>,
|
||||
{
|
||||
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Sqlite>) -> serialize::Result {
|
||||
let periods_u8: Vec<u8> = self
|
||||
.0
|
||||
//impl ToSql<Binary, Sqlite> for Periods
|
||||
//where
|
||||
// Vec<u8>: ToSql<Binary, Sqlite>,
|
||||
//{
|
||||
// fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Sqlite>) -> serialize::Result {
|
||||
// let periods_u8: Vec<u8> = self
|
||||
// .0
|
||||
// .iter()
|
||||
// .flat_map(|period| {
|
||||
// let vec = vec![
|
||||
// period.start.hour() as u8,
|
||||
// period.start.minute() as u8,
|
||||
// period.end.hour() as u8,
|
||||
// period.end.minute() as u8,
|
||||
// ];
|
||||
// vec
|
||||
// })
|
||||
// .collect();
|
||||
//
|
||||
// out.set_value(periods_u8);
|
||||
//
|
||||
// Ok(IsNull::No)
|
||||
// }
|
||||
//}
|
||||
//
|
||||
//impl<DB> FromSql<Binary, DB> for Periods
|
||||
//where
|
||||
// DB: diesel::backend::Backend,
|
||||
// Vec<u8>: FromSql<Binary, DB>,
|
||||
//{
|
||||
// fn from_sql(bytes: DB::RawValue<'_>) -> deserialize::Result<Self> {
|
||||
// let blob: Vec<u8> = Vec::from_sql(bytes).unwrap();
|
||||
//
|
||||
// let mut vec = Vec::new();
|
||||
// for i in (3..blob.len()).step_by(4) {
|
||||
// let start_val_h: u32 = blob[i - 3] as u32;
|
||||
// let start_val_m: u32 = blob[i - 2] as u32;
|
||||
// let end_val_h: u32 = blob[i - 1] as u32;
|
||||
// let end_val_m: u32 = blob[i] as u32;
|
||||
// vec.push(Period {
|
||||
// start: NaiveTime::from_hms_opt(start_val_h, start_val_m, 0).unwrap(),
|
||||
// end: NaiveTime::from_hms_opt(end_val_h, end_val_m, 0).unwrap(),
|
||||
// });
|
||||
// }
|
||||
// Ok(Periods(vec))
|
||||
// }
|
||||
//}
|
||||
|
||||
impl Type<Sqlite> for Periods {
|
||||
fn type_info() -> SqliteTypeInfo {
|
||||
<&[u8] as Type<Sqlite>>::type_info()
|
||||
}
|
||||
|
||||
fn compatible(ty: &SqliteTypeInfo) -> bool {
|
||||
<&[u8] as Type<Sqlite>>::compatible(ty)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'q> Encode<'q, Sqlite> for Periods {
|
||||
//noinspection DuplicatedCode
|
||||
fn encode_by_ref(&self, buf: &mut <Sqlite as HasArguments<'q>>::ArgumentBuffer) -> IsNull {
|
||||
<&Vec<u8> as Encode<Sqlite>>::encode(&Vec::from(self), buf)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'r> Decode<'r, Sqlite> for Periods {
|
||||
fn decode(value: SqliteValueRef<'r>) -> Result<Self, BoxDynError> {
|
||||
let blob = <&[u8] as Decode<Sqlite>>::decode(value)?;
|
||||
Ok(Periods::from(Vec::from(blob)))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&Periods> for Vec<u8> {
|
||||
fn from(periods: &Periods) -> Vec<u8> {
|
||||
periods.0
|
||||
.iter()
|
||||
.flat_map(|period| {
|
||||
let vec = vec![
|
||||
|
@ -69,33 +136,23 @@ where
|
|||
];
|
||||
vec
|
||||
})
|
||||
.collect();
|
||||
|
||||
out.set_value(periods_u8);
|
||||
|
||||
Ok(IsNull::No)
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
impl<DB> FromSql<Binary, DB> for Periods
|
||||
where
|
||||
DB: diesel::backend::Backend,
|
||||
Vec<u8>: FromSql<Binary, DB>,
|
||||
{
|
||||
fn from_sql(bytes: DB::RawValue<'_>) -> deserialize::Result<Self> {
|
||||
let blob: Vec<u8> = Vec::from_sql(bytes).unwrap();
|
||||
|
||||
impl From<Vec<u8>> for Periods {
|
||||
fn from(value: Vec<u8>) -> Self {
|
||||
let mut vec = Vec::new();
|
||||
for i in (3..blob.len()).step_by(4) {
|
||||
let start_val_h: u32 = blob[i - 3] as u32;
|
||||
let start_val_m: u32 = blob[i - 2] as u32;
|
||||
let end_val_h: u32 = blob[i - 1] as u32;
|
||||
let end_val_m: u32 = blob[i] as u32;
|
||||
for i in (3..value.len()).step_by(4) {
|
||||
let start_val_h: u32 = value[i - 3] as u32;
|
||||
let start_val_m: u32 = value[i - 2] as u32;
|
||||
let end_val_h: u32 = value[i - 1] as u32;
|
||||
let end_val_m: u32 = value[i] as u32;
|
||||
vec.push(Period {
|
||||
start: NaiveTime::from_hms_opt(start_val_h, start_val_m, 0).unwrap(),
|
||||
end: NaiveTime::from_hms_opt(end_val_h, end_val_m, 0).unwrap(),
|
||||
});
|
||||
}
|
||||
Ok(Periods(vec))
|
||||
Periods(vec)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,68 +1,37 @@
|
|||
use crate::db::model_utils::Period;
|
||||
use diesel::sql_types::Binary;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use super::schema::*;
|
||||
use crate::types::EmgauwaUid;
|
||||
|
||||
#[derive(Debug, Serialize, Identifiable, Queryable)]
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct Relay {
|
||||
#[serde(skip)]
|
||||
pub id: i32,
|
||||
pub id: i64,
|
||||
// TODO
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Identifiable, Queryable, Clone)]
|
||||
#[derive(Debug, Serialize, Clone)]
|
||||
pub struct Schedule {
|
||||
#[serde(skip)]
|
||||
pub id: i32,
|
||||
pub id: i64,
|
||||
#[serde(rename(serialize = "id"))]
|
||||
pub uid: EmgauwaUid,
|
||||
pub name: String,
|
||||
pub periods: Periods,
|
||||
}
|
||||
|
||||
#[derive(Insertable)]
|
||||
#[diesel(table_name = crate::db::schema::schedules)]
|
||||
pub struct NewSchedule<'a> {
|
||||
pub uid: &'a EmgauwaUid,
|
||||
pub name: &'a str,
|
||||
pub periods: &'a Periods,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, AsExpression, FromSqlRow, PartialEq, Clone)]
|
||||
#[diesel(sql_type = Binary)]
|
||||
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
|
||||
pub struct Periods(pub Vec<Period>);
|
||||
|
||||
#[derive(Debug, Serialize, Identifiable, Queryable, Clone)]
|
||||
#[diesel(table_name = crate::db::schema::tags)]
|
||||
#[derive(Debug, Serialize, Clone)]
|
||||
pub struct Tag {
|
||||
pub id: i32,
|
||||
pub id: i64,
|
||||
pub tag: String,
|
||||
}
|
||||
|
||||
#[derive(Insertable)]
|
||||
#[diesel(table_name = crate::db::schema::tags)]
|
||||
pub struct NewTag<'a> {
|
||||
pub tag: &'a str,
|
||||
}
|
||||
|
||||
#[derive(Queryable, Associations, Identifiable)]
|
||||
#[diesel(belongs_to(Relay))]
|
||||
#[diesel(belongs_to(Schedule))]
|
||||
#[diesel(belongs_to(Tag))]
|
||||
#[diesel(table_name = crate::db::schema::junction_tag)]
|
||||
pub struct JunctionTag {
|
||||
pub id: i32,
|
||||
pub tag_id: i32,
|
||||
pub relay_id: Option<i32>,
|
||||
pub schedule_id: Option<i32>,
|
||||
}
|
||||
|
||||
#[derive(Insertable)]
|
||||
#[diesel(table_name = crate::db::schema::junction_tag)]
|
||||
pub struct NewJunctionTag {
|
||||
pub tag_id: i32,
|
||||
pub relay_id: Option<i32>,
|
||||
pub schedule_id: Option<i32>,
|
||||
pub id: i64,
|
||||
pub tag_id: i64,
|
||||
pub relay_id: Option<i64>,
|
||||
pub schedule_id: Option<i64>,
|
||||
}
|
||||
|
|
|
@ -1,141 +1,106 @@
|
|||
use diesel::dsl::sql;
|
||||
use diesel::prelude::*;
|
||||
use std::borrow::Borrow;
|
||||
use sqlx::{Pool, Sqlite};
|
||||
|
||||
use crate::types::EmgauwaUid;
|
||||
|
||||
use crate::db::errors::DatabaseError;
|
||||
use crate::db::models::*;
|
||||
use crate::db::schema::junction_tag::dsl::junction_tag;
|
||||
use crate::db::schema::schedules::dsl::schedules;
|
||||
use crate::db::schema::tags::dsl::tags;
|
||||
use crate::db::tag::{create_junction_tag, create_tag};
|
||||
use crate::db::{get_connection, schema};
|
||||
use crate::db::tag::{create_junction_tag_schedule, create_tag};
|
||||
|
||||
pub fn get_schedule_tags(schedule: &Schedule) -> Vec<String> {
|
||||
let mut connection = get_connection();
|
||||
JunctionTag::belonging_to(schedule)
|
||||
.inner_join(schema::tags::dsl::tags)
|
||||
.select(schema::tags::tag)
|
||||
.load::<String>(&mut connection)
|
||||
.expect("Error loading tags")
|
||||
pub async fn get_schedule_tags(pool: &Pool<Sqlite>, schedule: &Schedule) -> 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 = ?", schedule.id)
|
||||
.fetch_all(pool)
|
||||
.await?)
|
||||
}
|
||||
|
||||
pub fn get_schedules() -> Vec<Schedule> {
|
||||
let mut connection = get_connection();
|
||||
schedules
|
||||
.load::<Schedule>(&mut connection)
|
||||
.expect("Error loading schedules")
|
||||
pub async fn get_schedules(pool: &Pool<Sqlite>) -> Result<Vec<Schedule>, DatabaseError> {
|
||||
Ok(sqlx::query_as!(Schedule, "SELECT * FROM schedules")
|
||||
.fetch_all(pool)
|
||||
.await?)
|
||||
}
|
||||
|
||||
pub fn get_schedule_by_uid(filter_uid: EmgauwaUid) -> Result<Schedule, DatabaseError> {
|
||||
let mut connection = get_connection();
|
||||
let result = schedules
|
||||
.filter(schema::schedules::uid.eq(filter_uid))
|
||||
.first::<Schedule>(&mut connection)
|
||||
.or(Err(DatabaseError::NotFound))?;
|
||||
|
||||
Ok(result)
|
||||
pub async fn get_schedule_by_uid(pool: &Pool<Sqlite>, filter_uid: &EmgauwaUid) -> Result<Schedule, DatabaseError> {
|
||||
sqlx::query_as!(Schedule, "SELECT * FROM schedules WHERE uid = ?", filter_uid)
|
||||
.fetch_optional(pool)
|
||||
.await
|
||||
.map(|s| s.ok_or(DatabaseError::NotFound))?
|
||||
}
|
||||
|
||||
pub fn get_schedules_by_tag(tag: &Tag) -> Vec<Schedule> {
|
||||
let mut connection = get_connection();
|
||||
JunctionTag::belonging_to(tag)
|
||||
.inner_join(schedules)
|
||||
.select(schema::schedules::all_columns)
|
||||
.load::<Schedule>(&mut connection)
|
||||
.expect("Error loading tags")
|
||||
pub async fn get_schedules_by_tag(pool: &Pool<Sqlite>, tag: &Tag) -> Result<Vec<Schedule>, DatabaseError> {
|
||||
Ok(sqlx::query_as!(Schedule, "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(pool)
|
||||
.await?)
|
||||
}
|
||||
|
||||
pub fn delete_schedule_by_uid(filter_uid: EmgauwaUid) -> Result<(), DatabaseError> {
|
||||
pub async fn delete_schedule_by_uid(pool: &Pool<Sqlite>, filter_uid: EmgauwaUid) -> Result<(), DatabaseError> {
|
||||
let filter_uid = match filter_uid {
|
||||
EmgauwaUid::Off => Err(DatabaseError::Protected),
|
||||
EmgauwaUid::On => Err(DatabaseError::Protected),
|
||||
EmgauwaUid::Any(_) => Ok(filter_uid),
|
||||
}?;
|
||||
|
||||
let mut connection = get_connection();
|
||||
match diesel::delete(schedules.filter(schema::schedules::uid.eq(filter_uid)))
|
||||
.execute(&mut connection)
|
||||
{
|
||||
Ok(rows) => {
|
||||
if rows != 0 {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(DatabaseError::DeleteError)
|
||||
}
|
||||
}
|
||||
Err(_) => Err(DatabaseError::DeleteError),
|
||||
}
|
||||
sqlx::query!("DELETE FROM schedules WHERE uid = ?", filter_uid)
|
||||
.execute(pool)
|
||||
.await
|
||||
.map(|res| match res.rows_affected() {
|
||||
0 => Err(DatabaseError::DeleteError),
|
||||
_ => Ok(()),
|
||||
})?
|
||||
}
|
||||
|
||||
pub fn create_schedule(new_name: &str, new_periods: &Periods) -> Result<Schedule, DatabaseError> {
|
||||
let mut connection = get_connection();
|
||||
|
||||
let new_schedule = NewSchedule {
|
||||
uid: &EmgauwaUid::default(),
|
||||
name: new_name,
|
||||
periods: new_periods,
|
||||
};
|
||||
|
||||
diesel::insert_into(schedules)
|
||||
.values(&new_schedule)
|
||||
.execute(&mut connection)
|
||||
.map_err(DatabaseError::InsertError)?;
|
||||
|
||||
let result = schedules
|
||||
.find(sql("last_insert_rowid()"))
|
||||
.get_result::<Schedule>(&mut connection)
|
||||
.or(Err(DatabaseError::InsertGetError))?;
|
||||
|
||||
Ok(result)
|
||||
pub async fn create_schedule(pool: &Pool<Sqlite>, new_name: &str, new_periods: &Periods) -> Result<Schedule, DatabaseError> {
|
||||
let uid = EmgauwaUid::default();
|
||||
sqlx::query_as!(Schedule, "INSERT INTO schedules (uid, name, periods) VALUES (?, ?, ?) RETURNING *",
|
||||
uid,
|
||||
new_name,
|
||||
new_periods,
|
||||
)
|
||||
.fetch_optional(pool)
|
||||
.await?
|
||||
.ok_or(DatabaseError::InsertGetError)
|
||||
}
|
||||
|
||||
pub fn update_schedule(
|
||||
pub async fn update_schedule(
|
||||
pool: &Pool<Sqlite>,
|
||||
schedule: &Schedule,
|
||||
new_name: &str,
|
||||
new_periods: &Periods,
|
||||
) -> Result<Schedule, DatabaseError> {
|
||||
let mut connection = get_connection();
|
||||
|
||||
// overwrite periods on protected schedules
|
||||
let new_periods = match schedule.uid {
|
||||
EmgauwaUid::Off | EmgauwaUid::On => schedule.periods.borrow(),
|
||||
EmgauwaUid::Any(_) => new_periods,
|
||||
};
|
||||
|
||||
diesel::update(schedule)
|
||||
.set((
|
||||
schema::schedules::name.eq(new_name),
|
||||
schema::schedules::periods.eq(new_periods),
|
||||
))
|
||||
.execute(&mut connection)
|
||||
.map_err(DatabaseError::UpdateError)?;
|
||||
sqlx::query!("UPDATE schedules SET name = ?, periods = ? WHERE id = ?",
|
||||
new_name,
|
||||
new_periods,
|
||||
schedule.id,
|
||||
)
|
||||
.execute(pool)
|
||||
.await?;
|
||||
|
||||
get_schedule_by_uid(schedule.uid.clone())
|
||||
get_schedule_by_uid(pool, &schedule.uid).await
|
||||
}
|
||||
|
||||
pub fn set_schedule_tags(schedule: &Schedule, new_tags: &[String]) -> Result<(), DatabaseError> {
|
||||
let mut connection = get_connection();
|
||||
diesel::delete(junction_tag.filter(schema::junction_tag::schedule_id.eq(schedule.id)))
|
||||
.execute(&mut connection)
|
||||
.or(Err(DatabaseError::DeleteError))?;
|
||||
pub async fn set_schedule_tags(pool: &Pool<Sqlite>, schedule: &Schedule, new_tags: &[String]) -> Result<(), DatabaseError> {
|
||||
sqlx::query!("DELETE FROM junction_tag WHERE schedule_id = ?", schedule.id)
|
||||
.execute(pool)
|
||||
.await?;
|
||||
|
||||
let mut database_tags: Vec<Tag> = tags
|
||||
.filter(schema::tags::tag.eq_any(new_tags))
|
||||
.load::<Tag>(&mut connection)
|
||||
.expect("Error loading tags");
|
||||
|
||||
// create missing tags
|
||||
for new_tag in new_tags {
|
||||
if !database_tags.iter().any(|tab_db| tab_db.tag.eq(new_tag)) {
|
||||
database_tags.push(create_tag(new_tag).expect("Error inserting tag"));
|
||||
}
|
||||
}
|
||||
let tag: Option<Tag> = sqlx::query_as!(Tag, "SELECT * FROM tags WHERE tag = ?", new_tag)
|
||||
.fetch_optional(pool)
|
||||
.await?;
|
||||
|
||||
for database_tag in database_tags {
|
||||
create_junction_tag(database_tag, None, Some(schedule))
|
||||
.expect("Error saving junction between tag and schedule");
|
||||
}
|
||||
let tag = match tag {
|
||||
Some(id) => id,
|
||||
None => {
|
||||
create_tag(pool, new_tag).await?
|
||||
}
|
||||
};
|
||||
|
||||
create_junction_tag_schedule(pool, tag, schedule).await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -1,93 +0,0 @@
|
|||
table! {
|
||||
controllers (id) {
|
||||
id -> Integer,
|
||||
uid -> Text,
|
||||
name -> Text,
|
||||
ip -> Nullable<Text>,
|
||||
port -> Nullable<Integer>,
|
||||
relay_count -> Nullable<Integer>,
|
||||
active -> Bool,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
junction_relay_schedule (id) {
|
||||
id -> Integer,
|
||||
weekday -> SmallInt,
|
||||
relay_id -> Nullable<Integer>,
|
||||
schedule_id -> Nullable<Integer>,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
junction_tag (id) {
|
||||
id -> Integer,
|
||||
tag_id -> Integer,
|
||||
relay_id -> Nullable<Integer>,
|
||||
schedule_id -> Nullable<Integer>,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
macro_actions (id) {
|
||||
id -> Integer,
|
||||
macro_id -> Integer,
|
||||
relay_id -> Integer,
|
||||
schedule_id -> Integer,
|
||||
weekday -> SmallInt,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
macros (id) {
|
||||
id -> Integer,
|
||||
uid -> Text,
|
||||
name -> Text,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
relays (id) {
|
||||
id -> Integer,
|
||||
name -> Text,
|
||||
number -> Integer,
|
||||
controller_id -> Integer,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
schedules (id) {
|
||||
id -> Integer,
|
||||
uid -> Binary,
|
||||
name -> Text,
|
||||
periods -> Binary,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
tags (id) {
|
||||
id -> Integer,
|
||||
tag -> Text,
|
||||
}
|
||||
}
|
||||
|
||||
joinable!(junction_relay_schedule -> relays (relay_id));
|
||||
joinable!(junction_relay_schedule -> schedules (schedule_id));
|
||||
joinable!(junction_tag -> relays (relay_id));
|
||||
joinable!(junction_tag -> schedules (schedule_id));
|
||||
joinable!(junction_tag -> tags (tag_id));
|
||||
joinable!(macro_actions -> macros (macro_id));
|
||||
joinable!(macro_actions -> relays (relay_id));
|
||||
joinable!(macro_actions -> schedules (schedule_id));
|
||||
joinable!(relays -> controllers (controller_id));
|
||||
|
||||
allow_tables_to_appear_in_same_query!(
|
||||
controllers,
|
||||
junction_relay_schedule,
|
||||
junction_tag,
|
||||
macro_actions,
|
||||
macros,
|
||||
relays,
|
||||
schedules,
|
||||
tags,
|
||||
);
|
|
@ -1,63 +1,41 @@
|
|||
use diesel::dsl::sql;
|
||||
use diesel::prelude::*;
|
||||
|
||||
use sqlx::{Pool, Sqlite};
|
||||
use crate::db::errors::DatabaseError;
|
||||
use crate::db::models::*;
|
||||
use crate::db::schema::junction_tag::dsl::junction_tag;
|
||||
use crate::db::schema::tags::dsl::tags;
|
||||
use crate::db::{get_connection, schema};
|
||||
|
||||
pub fn create_tag(new_tag: &str) -> Result<Tag, DatabaseError> {
|
||||
let mut connection = get_connection();
|
||||
|
||||
let new_tag = NewTag { tag: new_tag };
|
||||
|
||||
diesel::insert_into(tags)
|
||||
.values(&new_tag)
|
||||
.execute(&mut connection)
|
||||
.map_err(DatabaseError::InsertError)?;
|
||||
|
||||
let result = tags
|
||||
.find(sql("last_insert_rowid()"))
|
||||
.get_result::<Tag>(&mut connection)
|
||||
.or(Err(DatabaseError::InsertGetError))?;
|
||||
|
||||
Ok(result)
|
||||
pub async fn create_tag(pool: &Pool<Sqlite>, new_tag: &str) -> Result<Tag, DatabaseError> {
|
||||
sqlx::query_as!(Tag, "INSERT INTO tags (tag) VALUES (?) RETURNING *", new_tag)
|
||||
.fetch_optional(pool)
|
||||
.await?
|
||||
.ok_or(DatabaseError::InsertGetError)
|
||||
}
|
||||
|
||||
pub fn get_tag(target_tag: &str) -> Result<Tag, DatabaseError> {
|
||||
let mut connection = get_connection();
|
||||
|
||||
let result = tags
|
||||
.filter(schema::tags::tag.eq(target_tag))
|
||||
.first::<Tag>(&mut connection)
|
||||
.or(Err(DatabaseError::NotFound))?;
|
||||
|
||||
Ok(result)
|
||||
pub async fn get_tag(pool: &Pool<Sqlite>, target_tag: &str) -> Result<Tag, DatabaseError> {
|
||||
sqlx::query_as!(Tag, "SELECT * FROM tags WHERE tag = ?", target_tag)
|
||||
.fetch_optional(pool)
|
||||
.await
|
||||
.map(|t| t.ok_or(DatabaseError::NotFound))?
|
||||
}
|
||||
|
||||
pub fn create_junction_tag(
|
||||
#[allow(dead_code)]
|
||||
pub async fn create_junction_tag_relay(
|
||||
pool: &Pool<Sqlite>,
|
||||
target_tag: Tag,
|
||||
target_relay: Option<&Relay>,
|
||||
target_schedule: Option<&Schedule>,
|
||||
target_relay: &Relay,
|
||||
) -> Result<JunctionTag, DatabaseError> {
|
||||
let mut connection = get_connection();
|
||||
|
||||
let new_junction_tag = NewJunctionTag {
|
||||
relay_id: target_relay.map(|r| r.id),
|
||||
schedule_id: target_schedule.map(|s| s.id),
|
||||
tag_id: target_tag.id,
|
||||
};
|
||||
|
||||
diesel::insert_into(junction_tag)
|
||||
.values(&new_junction_tag)
|
||||
.execute(&mut connection)
|
||||
.map_err(DatabaseError::InsertError)?;
|
||||
|
||||
let result = junction_tag
|
||||
.find(sql("last_insert_rowid()"))
|
||||
.get_result::<JunctionTag>(&mut connection)
|
||||
.or(Err(DatabaseError::InsertGetError))?;
|
||||
|
||||
Ok(result)
|
||||
sqlx::query_as!(JunctionTag, "INSERT INTO junction_tag (tag_id, relay_id) VALUES (?, ?) RETURNING *", target_tag.id, target_relay.id)
|
||||
.fetch_optional(pool)
|
||||
.await?
|
||||
.ok_or(DatabaseError::InsertGetError)
|
||||
}
|
||||
|
||||
pub async fn create_junction_tag_schedule(
|
||||
pool: &Pool<Sqlite>,
|
||||
target_tag: Tag,
|
||||
target_schedule: &Schedule,
|
||||
) -> Result<JunctionTag, DatabaseError> {
|
||||
sqlx::query_as!(JunctionTag, "INSERT INTO junction_tag (tag_id, schedule_id) VALUES (?, ?) RETURNING *", target_tag.id, target_schedule.id)
|
||||
.fetch_optional(pool)
|
||||
.await?
|
||||
.ok_or(DatabaseError::InsertGetError)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue