|
@@ -4,41 +4,41 @@ pub use crate::DB;
|
|
|
pub struct ID (i64);
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
-pub struct WithID<T: crate::model::Entity + crate::model::EntityColumn> {
|
|
|
+pub struct WithID<T: crate::model::Entity + crate::model::Entity> {
|
|
|
wrap: T,
|
|
|
id: ID
|
|
|
}
|
|
|
|
|
|
-impl<T: crate::model::Entity + crate::model::EntityColumn> WithID<T> {
|
|
|
+impl<T: crate::model::Entity + crate::model::Entity> WithID<T> {
|
|
|
fn wrap(what: T, raw_id: i64) -> Self {
|
|
|
Self { wrap: what, id: ID { 0: raw_id } }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-impl<T: crate::model::Entity + crate::model::EntityColumn> WithID<T> {
|
|
|
+impl<T: crate::model::Entity + crate::model::Entity> WithID<T> {
|
|
|
pub fn id(&self) -> ID { self.id }
|
|
|
}
|
|
|
|
|
|
-impl<T: crate::model::Entity + crate::model::EntityColumn> AsRef<T> for WithID<T> {
|
|
|
+impl<T: crate::model::Entity + crate::model::Entity> AsRef<T> for WithID<T> {
|
|
|
fn as_ref(&self) -> &T { return &self.wrap }
|
|
|
}
|
|
|
|
|
|
-impl<T: crate::model::Entity + crate::model::EntityColumn> std::ops::Deref for WithID<T> {
|
|
|
+impl<T: crate::model::Entity + crate::model::Entity> std::ops::Deref for WithID<T> {
|
|
|
type Target = T;
|
|
|
fn deref(&self) -> &Self::Target { &self.wrap }
|
|
|
}
|
|
|
|
|
|
-impl<T: crate::model::Entity + crate::model::EntityColumn> std::ops::DerefMut for WithID<T> {
|
|
|
+impl<T: crate::model::Entity + crate::model::Entity> std::ops::DerefMut for WithID<T> {
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target { &mut self.wrap }
|
|
|
}
|
|
|
|
|
|
|
|
|
/// Search for an entity by a property
|
|
|
-pub fn get_one_by<T: crate::model::Entity + crate::model::EntityColumn + for<'de> serde::Deserialize<'de>, V: rusqlite::ToSql>(
|
|
|
- db: &DB, c: <T as crate::model::EntityColumn>::Column, val: V) -> Option<WithID<T>> {
|
|
|
+pub fn get_one_by<T: crate::model::Entity, V: rusqlite::ToSql>(
|
|
|
+ db: &DB, c: <T as crate::model::Entity>::Column, val: V) -> Option<WithID<T>> {
|
|
|
|
|
|
let table_name = <T as crate::model::Entity>::table_name();
|
|
|
- let column_name = <T as crate::model::EntityColumn>::name(c);
|
|
|
+ let column_name = <T as crate::model::Entity>::name(c);
|
|
|
let mut prepared = db.conn.prepare(&format!("SELECT rowid, tbl.* FROM {} tbl WHERE {} = ?1", table_name, column_name)).ok()?;
|
|
|
|
|
|
let result = prepared.query_row([&val], |row| {
|
|
@@ -50,16 +50,16 @@ pub fn get_one_by<T: crate::model::Entity + crate::model::EntityColumn + for<'de
|
|
|
}
|
|
|
|
|
|
/// Add an entity to its table
|
|
|
-pub fn add<T: crate::model::Entity + crate::model::EntityColumn + serde::Serialize>(db: &DB, m: &T) -> Option<ID> {
|
|
|
+pub fn add<T: crate::model::Entity + serde::Serialize>(db: &DB, m: &T) -> Option<ID> {
|
|
|
let row = crate::model::store::serialize_as_row(m);
|
|
|
|
|
|
- let placeholders = (0..<T as crate::model::EntityColumn>::count()).map(|n| format!("?{}", n+1)).collect::<Vec<_>>().join(",");
|
|
|
+ let placeholders = (0..<T as crate::model::Entity>::column_count()).map(|n| format!("?{}", n+1)).collect::<Vec<_>>().join(",");
|
|
|
|
|
|
let res = db.conn.prepare(&format!("INSERT INTO {} VALUES ({})", <T as crate::model::Entity>::table_name(), placeholders));
|
|
|
let mut prepared = res.ok()?;
|
|
|
|
|
|
// make sure we bound enough things
|
|
|
- assert_eq!(row.len(), <T as crate::model::EntityColumn>::count());
|
|
|
+ assert_eq!(row.len(), <T as crate::model::Entity>::column_count());
|
|
|
|
|
|
let id = prepared.insert(rusqlite::params_from_iter(row)).ok()?;
|
|
|
Some(ID { 0: id })
|
|
@@ -70,8 +70,8 @@ pub struct Context<'a> {
|
|
|
}
|
|
|
|
|
|
impl<'a> Context<'a> {
|
|
|
- pub fn get_one_by<T: crate::model::Entity + crate::model::EntityColumn + for<'de> serde::Deserialize<'de>, V: rusqlite::ToSql>(
|
|
|
- &self, c: <T as crate::model::EntityColumn>::Column, val: V) -> Option<WithID<T>> {
|
|
|
+ pub fn get_one_by<T: crate::model::Entity + for<'de> serde::Deserialize<'de>, V: rusqlite::ToSql>(
|
|
|
+ &self, c: <T as crate::model::Entity>::Column, val: V) -> Option<WithID<T>> {
|
|
|
|
|
|
get_one_by(self.db, c, val)
|
|
|
}
|