Bladeren bron

Minor cleanups.

Kestrel 9 maanden geleden
bovenliggende
commit
2c7cd772f3
4 gewijzigde bestanden met toevoegingen van 67 en 155 verwijderingen
  1. 17 0
      microrm/src/db.rs
  2. 1 0
      microrm/src/schema/build.rs
  3. 7 0
      microrm/src/schema/datum/datum_list.rs
  4. 42 155
      microrm/src/schema/entity/part_list.rs

+ 17 - 0
microrm/src/db.rs

@@ -25,6 +25,15 @@ struct ConnectionData {
     stmts: HashMap<u64, Statement>,
 }
 
+impl Drop for ConnectionData {
+    fn drop(&mut self) {
+        self.stmts.clear();
+        unsafe {
+            sq::sqlite3_close(self.sqlite);
+        }
+    }
+}
+
 unsafe impl Send for ConnectionData {}
 
 pub(crate) trait PreparedKey {
@@ -179,6 +188,14 @@ impl Statement {
     }
 }
 
+impl Drop for Statement {
+    fn drop(&mut self) {
+        unsafe {
+            sq::sqlite3_finalize(self.stmt);
+        }
+    }
+}
+
 #[cfg(test)]
 mod test {
     use super::Connection;

+ 1 - 0
microrm/src/schema/build.rs

@@ -85,6 +85,7 @@ impl DatabaseSchema {
 
     pub fn create(&self, db: Connection) -> DBResult<()> {
         for query in self.queries.iter() {
+            println!("Running {query}");
             db.execute_raw_sql(query)?;
         }
 

+ 7 - 0
microrm/src/schema/datum/datum_list.rs

@@ -62,3 +62,10 @@ datum_list!(T0: 0, T1: 1, T2: 2, T3: 3, T4: 4, T5: 5);
 datum_list!(T0: 0, T1: 1, T2: 2, T3: 3, T4: 4, T5: 5, T6: 6);
 datum_list!(T0: 0, T1: 1, T2: 2, T3: 3, T4: 4, T5: 5, T6: 6, T7: 7);
 datum_list!(T0: 0, T1: 1, T2: 2, T3: 3, T4: 4, T5: 5, T6: 6, T7: 7, T8: 8);
+datum_list!(T0: 0, T1: 1, T2: 2, T3: 3, T4: 4, T5: 5, T6: 6, T7: 7, T8: 8, T9: 9);
+datum_list!(T0: 0, T1: 1, T2: 2, T3: 3, T4: 4, T5: 5, T6: 6, T7: 7, T8: 8, T9: 9, T10: 10);
+datum_list!(T0: 0, T1: 1, T2: 2, T3: 3, T4: 4, T5: 5, T6: 6, T7: 7, T8: 8, T9: 9, T10: 10, T11: 11);
+datum_list!(T0: 0, T1: 1, T2: 2, T3: 3, T4: 4, T5: 5, T6: 6, T7: 7, T8: 8, T9: 9, T10: 10, T11: 11, T12: 12);
+datum_list!(T0: 0, T1: 1, T2: 2, T3: 3, T4: 4, T5: 5, T6: 6, T7: 7, T8: 8, T9: 9, T10: 10, T11: 11, T12: 12, T13: 13);
+datum_list!(T0: 0, T1: 1, T2: 2, T3: 3, T4: 4, T5: 5, T6: 6, T7: 7, T8: 8, T9: 9, T10: 10, T11: 11, T12: 12, T13: 13, T14: 14);
+datum_list!(T0: 0, T1: 1, T2: 2, T3: 3, T4: 4, T5: 5, T6: 6, T7: 7, T8: 8, T9: 9, T10: 10, T11: 11, T12: 12, T13: 13, T14: 14, T15: 15);

+ 42 - 155
microrm/src/schema/entity/part_list.rs

@@ -66,161 +66,48 @@ impl<P0: EntityPart> EntityPartList for (P0,) {
     }
 }
 
-impl<P0: EntityPart, P1: EntityPart> EntityPartList for (P0, P1) {
-    type DatumList = (P0::Datum, P1::Datum);
-
-    fn build_datum_list(conn: &Connection, stmt: &mut StatementRow) -> DBResult<Self::DatumList> {
-        let local_id: i64 = stmt.read(0)?;
-        let mut idx = 1; // starting index is 1 since index 0 is the ID
-        build_datum!(conn, local_id, stmt, idx, d0, P0);
-        build_datum!(conn, local_id, stmt, idx, d1, P1);
-
-        Ok((d0, d1))
-    }
-
-    fn accept_part_visitor(v: &mut impl EntityPartVisitor) {
-        v.visit::<P0>();
-        v.visit::<P1>();
-    }
-    fn accept_part_visitor_ref(datum_list: &Self::DatumList, v: &mut impl EntityPartVisitor) {
-        v.visit_datum::<P0>(&datum_list.0);
-        v.visit_datum::<P1>(&datum_list.1);
-    }
-}
-
-impl<P0: EntityPart, P1: EntityPart, P2: EntityPart> EntityPartList for (P0, P1, P2) {
-    type DatumList = (P0::Datum, P1::Datum, P2::Datum);
-
-    fn build_datum_list(conn: &Connection, stmt: &mut StatementRow) -> DBResult<Self::DatumList> {
-        let local_id: i64 = stmt.read(0)?;
-        let mut idx = 1; // starting index is 1 since index 0 is the ID
-        build_datum!(conn, local_id, stmt, idx, d0, P0);
-        build_datum!(conn, local_id, stmt, idx, d1, P1);
-        build_datum!(conn, local_id, stmt, idx, d2, P2);
-
-        Ok((d0, d1, d2))
-    }
-
-    fn accept_part_visitor(v: &mut impl EntityPartVisitor) {
-        v.visit::<P0>();
-        v.visit::<P1>();
-        v.visit::<P2>();
-    }
-    fn accept_part_visitor_ref(datum_list: &Self::DatumList, v: &mut impl EntityPartVisitor) {
-        v.visit_datum::<P0>(&datum_list.0);
-        v.visit_datum::<P1>(&datum_list.1);
-        v.visit_datum::<P2>(&datum_list.2);
+macro_rules! part_list_impl {
+    ($($p:ident : $d:ident : $n:tt),+) => {
+        impl< $( $p : EntityPart ),* > EntityPartList for ( $($p),* ) {
+            type DatumList = ( $( $p :: Datum ),* );
+
+            fn build_datum_list(conn: &Connection, stmt: &mut StatementRow) -> DBResult<Self::DatumList> {
+                let local_id: i64 = stmt.read(0)?;
+                let mut idx = 1;
+                $(
+                    build_datum!(conn, local_id, stmt, idx, $d, $p);
+                )*
+
+                Ok(( $( $d ),* ))
+            }
+
+            fn accept_part_visitor(v: &mut impl EntityPartVisitor) {
+                $(
+                    v.visit::< $p >();
+                )*
+            }
+
+            fn accept_part_visitor_ref(datum_list: &Self::DatumList, v: &mut impl EntityPartVisitor) {
+                $(
+                    v.visit_datum::< $p >(&datum_list . $n);
+                )*
+            }
+        }
     }
 }
 
-impl<P0: EntityPart, P1: EntityPart, P2: EntityPart, P3: EntityPart> EntityPartList
-    for (P0, P1, P2, P3)
-{
-    type DatumList = (P0::Datum, P1::Datum, P2::Datum, P3::Datum);
-
-    fn build_datum_list(conn: &Connection, stmt: &mut StatementRow) -> DBResult<Self::DatumList> {
-        let local_id: i64 = stmt.read(0)?;
-        let mut idx = 1; // starting index is 1 since index 0 is the ID
-        build_datum!(conn, local_id, stmt, idx, d0, P0);
-        build_datum!(conn, local_id, stmt, idx, d1, P1);
-        build_datum!(conn, local_id, stmt, idx, d2, P2);
-        build_datum!(conn, local_id, stmt, idx, d3, P3);
-
-        Ok((d0, d1, d2, d3))
-    }
-
-    fn accept_part_visitor(v: &mut impl EntityPartVisitor) {
-        v.visit::<P0>();
-        v.visit::<P1>();
-        v.visit::<P2>();
-        v.visit::<P3>();
-    }
-    fn accept_part_visitor_ref(datum_list: &Self::DatumList, v: &mut impl EntityPartVisitor) {
-        v.visit_datum::<P0>(&datum_list.0);
-        v.visit_datum::<P1>(&datum_list.1);
-        v.visit_datum::<P2>(&datum_list.2);
-        v.visit_datum::<P3>(&datum_list.3);
-    }
-}
-
-impl<P0: EntityPart, P1: EntityPart, P2: EntityPart, P3: EntityPart, P4: EntityPart> EntityPartList
-    for (P0, P1, P2, P3, P4)
-{
-    type DatumList = (P0::Datum, P1::Datum, P2::Datum, P3::Datum, P4::Datum);
-
-    fn build_datum_list(conn: &Connection, stmt: &mut StatementRow) -> DBResult<Self::DatumList> {
-        let local_id: i64 = stmt.read(0)?;
-        let mut idx = 1; // starting index is 1 since index 0 is the ID
-        build_datum!(conn, local_id, stmt, idx, d0, P0);
-        build_datum!(conn, local_id, stmt, idx, d1, P1);
-        build_datum!(conn, local_id, stmt, idx, d2, P2);
-        build_datum!(conn, local_id, stmt, idx, d3, P3);
-        build_datum!(conn, local_id, stmt, idx, d4, P4);
-
-        Ok((d0, d1, d2, d3, d4))
-    }
-
-    fn accept_part_visitor(v: &mut impl EntityPartVisitor) {
-        v.visit::<P0>();
-        v.visit::<P1>();
-        v.visit::<P2>();
-        v.visit::<P3>();
-        v.visit::<P4>();
-    }
-    fn accept_part_visitor_ref(datum_list: &Self::DatumList, v: &mut impl EntityPartVisitor) {
-        v.visit_datum::<P0>(&datum_list.0);
-        v.visit_datum::<P1>(&datum_list.1);
-        v.visit_datum::<P2>(&datum_list.2);
-        v.visit_datum::<P3>(&datum_list.3);
-        v.visit_datum::<P4>(&datum_list.4);
-    }
-}
-
-impl<
-        P0: EntityPart,
-        P1: EntityPart,
-        P2: EntityPart,
-        P3: EntityPart,
-        P4: EntityPart,
-        P5: EntityPart,
-    > EntityPartList for (P0, P1, P2, P3, P4, P5)
-{
-    type DatumList = (
-        P0::Datum,
-        P1::Datum,
-        P2::Datum,
-        P3::Datum,
-        P4::Datum,
-        P5::Datum,
-    );
-
-    fn build_datum_list(conn: &Connection, stmt: &mut StatementRow) -> DBResult<Self::DatumList> {
-        let local_id: i64 = stmt.read(0)?;
-        let mut idx = 1; // starting index is 1 since index 0 is the ID
-        build_datum!(conn, local_id, stmt, idx, d0, P0);
-        build_datum!(conn, local_id, stmt, idx, d1, P1);
-        build_datum!(conn, local_id, stmt, idx, d2, P2);
-        build_datum!(conn, local_id, stmt, idx, d3, P3);
-        build_datum!(conn, local_id, stmt, idx, d4, P4);
-        build_datum!(conn, local_id, stmt, idx, d5, P5);
-
-        Ok((d0, d1, d2, d3, d4, d5))
-    }
-
-    fn accept_part_visitor(v: &mut impl EntityPartVisitor) {
-        v.visit::<P0>();
-        v.visit::<P1>();
-        v.visit::<P2>();
-        v.visit::<P3>();
-        v.visit::<P4>();
-        v.visit::<P5>();
-    }
-    fn accept_part_visitor_ref(datum_list: &Self::DatumList, v: &mut impl EntityPartVisitor) {
-        v.visit_datum::<P0>(&datum_list.0);
-        v.visit_datum::<P1>(&datum_list.1);
-        v.visit_datum::<P2>(&datum_list.2);
-        v.visit_datum::<P3>(&datum_list.3);
-        v.visit_datum::<P4>(&datum_list.4);
-        v.visit_datum::<P5>(&datum_list.5);
-    }
-}
+part_list_impl!(P0:d0:0, P1:d1:1);
+part_list_impl!(P0:d0:0, P1:d1:1, P2:d2:2);
+part_list_impl!(P0:d0:0, P1:d1:1, P2:d2:2, P3:d3:3);
+part_list_impl!(P0:d0:0, P1:d1:1, P2:d2:2, P3:d3:3, P4:d4:4);
+part_list_impl!(P0:d0:0, P1:d1:1, P2:d2:2, P3:d3:3, P4:d4:4, P5:d5:5);
+part_list_impl!(P0:d0:0, P1:d1:1, P2:d2:2, P3:d3:3, P4:d4:4, P5:d5:5, P6:d6:6);
+part_list_impl!(P0:d0:0, P1:d1:1, P2:d2:2, P3:d3:3, P4:d4:4, P5:d5:5, P6:d6:6, P7:d7:7);
+part_list_impl!(P0:d0:0, P1:d1:1, P2:d2:2, P3:d3:3, P4:d4:4, P5:d5:5, P6:d6:6, P7:d7:7, P8:d8:8);
+part_list_impl!(P0:d0:0, P1:d1:1, P2:d2:2, P3:d3:3, P4:d4:4, P5:d5:5, P6:d6:6, P7:d7:7, P8:d8:8, P9:d9:9);
+part_list_impl!(P0:d0:0, P1:d1:1, P2:d2:2, P3:d3:3, P4:d4:4, P5:d5:5, P6:d6:6, P7:d7:7, P8:d8:8, P9:d9:9, P10:d10:10);
+part_list_impl!(P0:d0:0, P1:d1:1, P2:d2:2, P3:d3:3, P4:d4:4, P5:d5:5, P6:d6:6, P7:d7:7, P8:d8:8, P9:d9:9, P10:d10:10, P11:d11:11);
+part_list_impl!(P0:d0:0, P1:d1:1, P2:d2:2, P3:d3:3, P4:d4:4, P5:d5:5, P6:d6:6, P7:d7:7, P8:d8:8, P9:d9:9, P10:d10:10, P11:d11:11, P12:d12:12);
+part_list_impl!(P0:d0:0, P1:d1:1, P2:d2:2, P3:d3:3, P4:d4:4, P5:d5:5, P6:d6:6, P7:d7:7, P8:d8:8, P9:d9:9, P10:d10:10, P11:d11:11, P12:d12:12, P13:d13:13);
+part_list_impl!(P0:d0:0, P1:d1:1, P2:d2:2, P3:d3:3, P4:d4:4, P5:d5:5, P6:d6:6, P7:d7:7, P8:d8:8, P9:d9:9, P10:d10:10, P11:d11:11, P12:d12:12, P13:d13:13, P14:d14:14);
+part_list_impl!(P0:d0:0, P1:d1:1, P2:d2:2, P3:d3:3, P4:d4:4, P5:d5:5, P6:d6:6, P7:d7:7, P8:d8:8, P9:d9:9, P10:d10:10, P11:d11:11, P12:d12:12, P13:d13:13, P14:d14:14, P15:d15:15);