Browse Source

Properly implement QueryInterface::get_all_by().

Kestrel 2 years ago
parent
commit
1540557f38
1 changed files with 12 additions and 32 deletions
  1. 12 32
      microrm/src/query.rs

+ 12 - 32
microrm/src/query.rs

@@ -230,42 +230,21 @@ impl<'l> QueryInterface<'l> {
             &mut |stmt| {
             &mut |stmt| {
                 val.bind_to(stmt, 1).ok()?;
                 val.bind_to(stmt, 1).ok()?;
 
 
-                todo!()
+                let mut res = Vec::new();
+                loop {
+                    let state = stmt.next().ok()?;
+                    if state == sqlite::State::Done {
+                        break;
+                    }
 
 
-                /*self.expect_one_result(stmt, &mut |stmt| {
                     let id: i64 = stmt.read(0).ok()?;
                     let id: i64 = stmt.read(0).ok()?;
                     let mut rd = crate::model::load::RowDeserializer::from_row(stmt);
                     let mut rd = crate::model::load::RowDeserializer::from_row(stmt);
-                    Some(WithID::wrap(T::deserialize(&mut rd).ok()?, id))
-                })*/
+                    res.push(WithID::wrap(T::deserialize(&mut rd).ok()?, id));
+                }
+
+                Some(res)
             },
             },
         )
         )
-
-        /*
-        let mut prepared = self
-            .db
-            .conn
-            .prepare(&format!(
-                "SELECT * FROM \"{}\" WHERE \"{}\" = ?",
-                table_name, column_name
-            ))
-            .ok()?;
-
-        val.bind_to(&mut prepared, 1).ok()?;
-
-        todo!();
-        */
-
-        /*let rows = prepared
-            .query_map([&val], |row| {
-                let mut deser = crate::model::load::RowDeserializer::from_row(row);
-                Ok(WithID::wrap(
-                    T::deserialize(&mut deser)?,
-                    row.get(0).expect("can get rowid"),
-                ))
-            })
-            .ok()?;
-
-        Some(rows.map(|x| x.unwrap()).collect())*/
     }
     }
 
 
     /// Add an entity to its table
     /// Add an entity to its table
@@ -283,7 +262,8 @@ impl<'l> QueryInterface<'l> {
                     .conn
                     .conn
                     .prepare(&format!(
                     .prepare(&format!(
                         "INSERT INTO \"{}\" VALUES (NULL, {}) RETURNING \"id\"",
                         "INSERT INTO \"{}\" VALUES (NULL, {}) RETURNING \"id\"",
-                        <T as Entity>::table_name(), placeholders
+                        <T as Entity>::table_name(),
+                        placeholders
                     ))
                     ))
                     .expect("")
                     .expect("")
             },
             },