A mobile app for listening to radio stations from around the globe
git clone git://soophie.de/verbal
blob: d9425ca15ce20956f814953f4cf871fcf7fba3f4 (1375 bytes)[doc][blame][raw]
1
use serde::{Deserialize, Serialize};2
use sqlx::FromRow;4
pub trait Query {5
fn into_keys(&self) -> String;6
fn into_values(&self) -> String;7
fn into_update(&self) -> String;8
}10
#[derive(Debug, Deserialize, Serialize, FromRow)]11
pub struct Model<T> {12
pub id: i32,13
pub inner: T,14
}16
impl<T> Model<T> where T: Query {17
pub fn new(inner: T) -> Self {18
Self { id: 0, inner }19
}20
pub fn from(id: i32, inner: T) -> Self {21
Self { id, inner }22
}23
pub fn into_insert(&self, table: &str) -> String {24
format!(25
"26
INSERT INTO {}27
(28
{}29
created_at,30
updated_at31
)32
VALUES33
(34
{}35
CURRENT_TIMESTAMP,36
CURRENT_TIMESTAMP37
)38
",39
&table,40
&self.inner.into_keys(),41
&self.inner.into_values(),42
)43
}44
pub fn into_update(&self, table: &str) -> String {45
format!(46
"47
UPDATE {}48
SET49
{}50
updated_at = CURRENT_TIMESTAMP51
WHERE52
id = {}53
",54
&table,55
&self.inner.into_update(),56
&self.id,57
)58
}59
}