Add much stuff for rewrite
This commit is contained in:
parent
4261141c3a
commit
bd44dc3183
37 changed files with 1356 additions and 2551 deletions
|
@ -11,6 +11,7 @@ pub enum DatabaseError {
|
|||
NotFound,
|
||||
Protected,
|
||||
UpdateError(diesel::result::Error),
|
||||
Unknown,
|
||||
}
|
||||
|
||||
impl DatabaseError {
|
||||
|
@ -47,6 +48,7 @@ impl From<&DatabaseError> for String {
|
|||
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::Unknown => String::from("unknown error"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,16 +1,14 @@
|
|||
use crate::db::models::Periods;
|
||||
use chrono::{NaiveTime, Timelike};
|
||||
use diesel::backend::Backend;
|
||||
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 std::io::Write;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, AsExpression, FromSqlRow, PartialEq, Clone)]
|
||||
#[sql_type = "Binary"]
|
||||
#[diesel(sql_type = Binary)]
|
||||
pub struct Period {
|
||||
#[serde(with = "period_format")]
|
||||
pub start: NaiveTime,
|
||||
|
@ -41,23 +39,51 @@ mod period_format {
|
|||
}
|
||||
}
|
||||
|
||||
impl ToSql<Binary, Sqlite> for Periods {
|
||||
fn to_sql<W: Write>(&self, out: &mut Output<W, Sqlite>) -> serialize::Result {
|
||||
for period in self.0.iter() {
|
||||
out.write_all(&[
|
||||
period.start.hour() as u8,
|
||||
period.start.minute() as u8,
|
||||
period.end.hour() as u8,
|
||||
period.end.minute() as u8,
|
||||
])?;
|
||||
impl Period {
|
||||
pub fn new(start: NaiveTime, end: NaiveTime) -> Self {
|
||||
Period { start, end }
|
||||
}
|
||||
|
||||
pub fn new_on() -> Self {
|
||||
Period {
|
||||
start: NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
|
||||
end: NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 FromSql<Binary, Sqlite> for Periods {
|
||||
fn from_sql(bytes: Option<&<Sqlite as Backend>::RawValue>) -> deserialize::Result<Self> {
|
||||
let blob = bytes.unwrap().read_blob();
|
||||
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) {
|
||||
|
@ -66,8 +92,8 @@ impl FromSql<Binary, Sqlite> for Periods {
|
|||
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(start_val_h, start_val_m, 0),
|
||||
end: NaiveTime::from_hms(end_val_h, end_val_m, 0),
|
||||
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))
|
||||
|
|
|
@ -23,7 +23,7 @@ pub struct Schedule {
|
|||
}
|
||||
|
||||
#[derive(Insertable)]
|
||||
#[table_name = "schedules"]
|
||||
#[diesel(table_name = crate::db::schema::schedules)]
|
||||
pub struct NewSchedule<'a> {
|
||||
pub uid: &'a EmgauwaUid,
|
||||
pub name: &'a str,
|
||||
|
@ -31,26 +31,27 @@ pub struct NewSchedule<'a> {
|
|||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, AsExpression, FromSqlRow, PartialEq, Clone)]
|
||||
#[sql_type = "Binary"]
|
||||
pub struct Periods(pub(crate) Vec<Period>);
|
||||
#[diesel(sql_type = Binary)]
|
||||
pub struct Periods(pub Vec<Period>);
|
||||
|
||||
#[derive(Debug, Serialize, Identifiable, Queryable, Clone)]
|
||||
#[diesel(table_name = crate::db::schema::tags)]
|
||||
pub struct Tag {
|
||||
pub id: i32,
|
||||
pub tag: String,
|
||||
}
|
||||
|
||||
#[derive(Insertable)]
|
||||
#[table_name = "tags"]
|
||||
#[diesel(table_name = crate::db::schema::tags)]
|
||||
pub struct NewTag<'a> {
|
||||
pub tag: &'a str,
|
||||
}
|
||||
|
||||
#[derive(Queryable, Associations, Identifiable)]
|
||||
#[belongs_to(Relay)]
|
||||
#[belongs_to(Schedule)]
|
||||
#[belongs_to(Tag)]
|
||||
#[table_name = "junction_tag"]
|
||||
#[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,
|
||||
|
@ -59,7 +60,7 @@ pub struct JunctionTag {
|
|||
}
|
||||
|
||||
#[derive(Insertable)]
|
||||
#[table_name = "junction_tag"]
|
||||
#[diesel(table_name = crate::db::schema::junction_tag)]
|
||||
pub struct NewJunctionTag {
|
||||
pub tag_id: i32,
|
||||
pub relay_id: Option<i32>,
|
||||
|
|
|
@ -13,37 +13,37 @@ use crate::db::tag::{create_junction_tag, create_tag};
|
|||
use crate::db::{get_connection, schema};
|
||||
|
||||
pub fn get_schedule_tags(schedule: &Schedule) -> Vec<String> {
|
||||
let connection = get_connection();
|
||||
let mut connection = get_connection();
|
||||
JunctionTag::belonging_to(schedule)
|
||||
.inner_join(schema::tags::dsl::tags)
|
||||
.select(schema::tags::tag)
|
||||
.load::<String>(&connection)
|
||||
.load::<String>(&mut connection)
|
||||
.expect("Error loading tags")
|
||||
}
|
||||
|
||||
pub fn get_schedules() -> Vec<Schedule> {
|
||||
let connection = get_connection();
|
||||
let mut connection = get_connection();
|
||||
schedules
|
||||
.load::<Schedule>(&connection)
|
||||
.load::<Schedule>(&mut connection)
|
||||
.expect("Error loading schedules")
|
||||
}
|
||||
|
||||
pub fn get_schedule_by_uid(filter_uid: EmgauwaUid) -> Result<Schedule, DatabaseError> {
|
||||
let connection = get_connection();
|
||||
let mut connection = get_connection();
|
||||
let result = schedules
|
||||
.filter(schema::schedules::uid.eq(filter_uid))
|
||||
.first::<Schedule>(&connection)
|
||||
.first::<Schedule>(&mut connection)
|
||||
.or(Err(DatabaseError::NotFound))?;
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub fn get_schedules_by_tag(tag: &Tag) -> Vec<Schedule> {
|
||||
let connection = get_connection();
|
||||
let mut connection = get_connection();
|
||||
JunctionTag::belonging_to(tag)
|
||||
.inner_join(schedules)
|
||||
.select(schema::schedules::all_columns)
|
||||
.load::<Schedule>(&connection)
|
||||
.load::<Schedule>(&mut connection)
|
||||
.expect("Error loading tags")
|
||||
}
|
||||
|
||||
|
@ -54,9 +54,9 @@ pub fn delete_schedule_by_uid(filter_uid: EmgauwaUid) -> Result<(), DatabaseErro
|
|||
EmgauwaUid::Any(_) => Ok(filter_uid),
|
||||
}?;
|
||||
|
||||
let connection = get_connection();
|
||||
let mut connection = get_connection();
|
||||
match diesel::delete(schedules.filter(schema::schedules::uid.eq(filter_uid)))
|
||||
.execute(&connection)
|
||||
.execute(&mut connection)
|
||||
{
|
||||
Ok(rows) => {
|
||||
if rows != 0 {
|
||||
|
@ -70,7 +70,7 @@ pub fn delete_schedule_by_uid(filter_uid: EmgauwaUid) -> Result<(), DatabaseErro
|
|||
}
|
||||
|
||||
pub fn create_schedule(new_name: &str, new_periods: &Periods) -> Result<Schedule, DatabaseError> {
|
||||
let connection = get_connection();
|
||||
let mut connection = get_connection();
|
||||
|
||||
let new_schedule = NewSchedule {
|
||||
uid: &EmgauwaUid::default(),
|
||||
|
@ -80,12 +80,12 @@ pub fn create_schedule(new_name: &str, new_periods: &Periods) -> Result<Schedule
|
|||
|
||||
diesel::insert_into(schedules)
|
||||
.values(&new_schedule)
|
||||
.execute(&connection)
|
||||
.execute(&mut connection)
|
||||
.map_err(DatabaseError::InsertError)?;
|
||||
|
||||
let result = schedules
|
||||
.find(sql("last_insert_rowid()"))
|
||||
.get_result::<Schedule>(&connection)
|
||||
.get_result::<Schedule>(&mut connection)
|
||||
.or(Err(DatabaseError::InsertGetError))?;
|
||||
|
||||
Ok(result)
|
||||
|
@ -96,7 +96,7 @@ pub fn update_schedule(
|
|||
new_name: &str,
|
||||
new_periods: &Periods,
|
||||
) -> Result<Schedule, DatabaseError> {
|
||||
let connection = get_connection();
|
||||
let mut connection = get_connection();
|
||||
|
||||
let new_periods = match schedule.uid {
|
||||
EmgauwaUid::Off | EmgauwaUid::On => schedule.periods.borrow(),
|
||||
|
@ -108,21 +108,21 @@ pub fn update_schedule(
|
|||
schema::schedules::name.eq(new_name),
|
||||
schema::schedules::periods.eq(new_periods),
|
||||
))
|
||||
.execute(&connection)
|
||||
.execute(&mut connection)
|
||||
.map_err(DatabaseError::UpdateError)?;
|
||||
|
||||
get_schedule_by_uid(schedule.uid.clone())
|
||||
}
|
||||
|
||||
pub fn set_schedule_tags(schedule: &Schedule, new_tags: &[String]) -> Result<(), DatabaseError> {
|
||||
let connection = get_connection();
|
||||
let mut connection = get_connection();
|
||||
diesel::delete(junction_tag.filter(schema::junction_tag::schedule_id.eq(schedule.id)))
|
||||
.execute(&connection)
|
||||
.execute(&mut connection)
|
||||
.or(Err(DatabaseError::DeleteError))?;
|
||||
|
||||
let mut database_tags: Vec<Tag> = tags
|
||||
.filter(schema::tags::tag.eq_any(new_tags))
|
||||
.load::<Tag>(&connection)
|
||||
.load::<Tag>(&mut connection)
|
||||
.expect("Error loading tags");
|
||||
|
||||
// create missing tags
|
||||
|
|
|
@ -8,29 +8,29 @@ use crate::db::schema::tags::dsl::tags;
|
|||
use crate::db::{get_connection, schema};
|
||||
|
||||
pub fn create_tag(new_tag: &str) -> Result<Tag, DatabaseError> {
|
||||
let connection = get_connection();
|
||||
let mut connection = get_connection();
|
||||
|
||||
let new_tag = NewTag { tag: new_tag };
|
||||
|
||||
diesel::insert_into(tags)
|
||||
.values(&new_tag)
|
||||
.execute(&connection)
|
||||
.execute(&mut connection)
|
||||
.map_err(DatabaseError::InsertError)?;
|
||||
|
||||
let result = tags
|
||||
.find(sql("last_insert_rowid()"))
|
||||
.get_result::<Tag>(&connection)
|
||||
.get_result::<Tag>(&mut connection)
|
||||
.or(Err(DatabaseError::InsertGetError))?;
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub fn get_tag(target_tag: &str) -> Result<Tag, DatabaseError> {
|
||||
let connection = get_connection();
|
||||
let mut connection = get_connection();
|
||||
|
||||
let result = tags
|
||||
.filter(schema::tags::tag.eq(target_tag))
|
||||
.first::<Tag>(&connection)
|
||||
.first::<Tag>(&mut connection)
|
||||
.or(Err(DatabaseError::NotFound))?;
|
||||
|
||||
Ok(result)
|
||||
|
@ -41,7 +41,7 @@ pub fn create_junction_tag(
|
|||
target_relay: Option<&Relay>,
|
||||
target_schedule: Option<&Schedule>,
|
||||
) -> Result<JunctionTag, DatabaseError> {
|
||||
let connection = get_connection();
|
||||
let mut connection = get_connection();
|
||||
|
||||
let new_junction_tag = NewJunctionTag {
|
||||
relay_id: target_relay.map(|r| r.id),
|
||||
|
@ -51,12 +51,12 @@ pub fn create_junction_tag(
|
|||
|
||||
diesel::insert_into(junction_tag)
|
||||
.values(&new_junction_tag)
|
||||
.execute(&connection)
|
||||
.execute(&mut connection)
|
||||
.map_err(DatabaseError::InsertError)?;
|
||||
|
||||
let result = junction_tag
|
||||
.find(sql("last_insert_rowid()"))
|
||||
.get_result::<JunctionTag>(&connection)
|
||||
.get_result::<JunctionTag>(&mut connection)
|
||||
.or(Err(DatabaseError::InsertGetError))?;
|
||||
|
||||
Ok(result)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue