|
@@ -47,12 +47,6 @@ impl<T: Entity> std::ops::DerefMut for WithID<T> {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-/*impl<T: Entity> std::convert::Into<T> for WithID<T> {
|
|
|
- fn into(self) -> T {
|
|
|
- self.wrap
|
|
|
- }
|
|
|
-}*/
|
|
|
-
|
|
|
/// Search for an entity by a property
|
|
|
pub fn get_one_by<T: Entity<Column = C>, C: EntityColumns<Entity = T>, V: rusqlite::ToSql>(
|
|
|
db: &DB,
|
|
@@ -61,10 +55,11 @@ pub fn get_one_by<T: Entity<Column = C>, C: EntityColumns<Entity = T>, V: rusqli
|
|
|
) -> Option<WithID<T>> {
|
|
|
let table_name = <T as Entity>::table_name();
|
|
|
let column_name = <T as Entity>::name(c);
|
|
|
+
|
|
|
let mut prepared = db
|
|
|
.conn
|
|
|
.prepare(&format!(
|
|
|
- "SELECT rowid, tbl.* FROM \"{}\" tbl WHERE \"{}\" = ?1",
|
|
|
+ "SELECT * FROM \"{}\" WHERE \"{}\" = ?1",
|
|
|
table_name, column_name
|
|
|
))
|
|
|
.ok()?;
|
|
@@ -73,7 +68,7 @@ pub fn get_one_by<T: Entity<Column = C>, C: EntityColumns<Entity = T>, V: rusqli
|
|
|
let mut deser = crate::model::load::RowDeserializer::from_row(row);
|
|
|
Ok(WithID::wrap(
|
|
|
T::deserialize(&mut deser).expect("deserialization works"),
|
|
|
- row.get(0).expect("can get rowid"),
|
|
|
+ row.get(0).expect("can get id"),
|
|
|
))
|
|
|
});
|
|
|
|
|
@@ -92,7 +87,7 @@ pub fn get_all_by<T: Entity<Column = C>, C: EntityColumns<Entity = T>, V: rusqli
|
|
|
let mut prepared = db
|
|
|
.conn
|
|
|
.prepare(&format!(
|
|
|
- "SELECT rowid, tbl.* FROM \"{}\" tbl WHERE \"{}\" = ?1",
|
|
|
+ "SELECT * FROM \"{}\" WHERE \"{}\" = ?1",
|
|
|
table_name, column_name
|
|
|
))
|
|
|
.ok()?;
|
|
@@ -116,7 +111,7 @@ pub fn get_one_by_id<T: Entity>(db: &DB, id: <T as Entity>::ID) -> Option<WithID
|
|
|
let mut prepared = db
|
|
|
.conn
|
|
|
.prepare(&format!(
|
|
|
- "SELECT rowid, tbl.* FROM \"{}\" tbl WHERE rowid = ?1",
|
|
|
+ "SELECT * FROM \"{}\" WHERE id = ?1",
|
|
|
table_name
|
|
|
))
|
|
|
.ok()?;
|
|
@@ -136,20 +131,20 @@ pub fn get_one_by_id<T: Entity>(db: &DB, id: <T as Entity>::ID) -> Option<WithID
|
|
|
pub fn add<T: Entity + serde::Serialize>(db: &DB, m: &T) -> Option<<T as Entity>::ID> {
|
|
|
let row = crate::model::store::serialize_as_row(m);
|
|
|
|
|
|
- let placeholders = (0..<T as Entity>::column_count())
|
|
|
+ let placeholders = (0..(<T as Entity>::column_count()-1))
|
|
|
.map(|n| format!("?{}", n + 1))
|
|
|
.collect::<Vec<_>>()
|
|
|
.join(",");
|
|
|
|
|
|
let res = db.conn.prepare(&format!(
|
|
|
- "INSERT INTO \"{}\" VALUES ({})",
|
|
|
+ "INSERT INTO \"{}\" VALUES (NULL, {})",
|
|
|
<T as Entity>::table_name(),
|
|
|
placeholders
|
|
|
));
|
|
|
let mut prepared = res.ok()?;
|
|
|
|
|
|
- // make sure we bound enough things
|
|
|
- assert_eq!(row.len(), <T as Entity>::column_count());
|
|
|
+ // make sure we bound enough things (not including ID column here)
|
|
|
+ assert_eq!(row.len(), <T as Entity>::column_count() - 1);
|
|
|
|
|
|
let id = prepared.insert(rusqlite::params_from_iter(row)).ok()?;
|
|
|
Some(<T as Entity>::ID::from_raw_id(id))
|