Browse Source

Renamed entity::EntityDatum to datum::Datum and removed ctx from table names.

Kestrel 1 year ago
parent
commit
524776d76e

+ 4 - 4
microrm-macros/src/entity.rs

@@ -149,13 +149,13 @@ pub fn derive(tokens: proc_macro::TokenStream) -> proc_macro::TokenStream {
             fn into_raw(self) -> i64 { self.0 }
             fn into_raw(self) -> i64 { self.0 }
         }
         }
 
 
-        impl ::microrm::entity::EntityDatum for #id_ident {
+        impl ::microrm::datum::Datum for #id_ident {
             fn sql_type() -> &'static str {
             fn sql_type() -> &'static str {
-                <i64 as ::microrm::entity::EntityDatum>::sql_type()
+                <i64 as ::microrm::datum::Datum>::sql_type()
             }
             }
 
 
             fn bind_to<'a>(&self, stmt: &mut ::microrm::sqlite::Statement<'a>, index: usize) {
             fn bind_to<'a>(&self, stmt: &mut ::microrm::sqlite::Statement<'a>, index: usize) {
-                <i64 as ::microrm::entity::EntityDatum>::bind_to(&self.0, stmt, index)
+                <i64 as ::microrm::datum::Datum>::bind_to(&self.0, stmt, index)
             }
             }
 
 
             fn build_from<'a>(
             fn build_from<'a>(
@@ -166,7 +166,7 @@ pub fn derive(tokens: proc_macro::TokenStream) -> proc_macro::TokenStream {
             where
             where
                 Self: Sized,
                 Self: Sized,
             {
             {
-                let raw = <i64 as ::microrm::entity::EntityDatum>::build_from(adata, stmt, index)?;
+                let raw = <i64 as ::microrm::datum::Datum>::build_from(adata, stmt, index)?;
                 Ok((Self(raw.0), raw.1))
                 Ok((Self(raw.0), raw.1))
             }
             }
         }
         }

+ 34 - 0
microrm/src/datum.rs

@@ -0,0 +1,34 @@
+use crate::{entity::EntityVisitor, schema::AssocData, DBResult};
+
+// ----------------------------------------------------------------------
+// Datum and related types
+// ----------------------------------------------------------------------
+
+/// Represents a data field in an Entity.
+pub trait Datum: 'static {
+    fn sql_type() -> &'static str;
+
+    fn bind_to<'a>(&self, _stmt: &mut sqlite::Statement<'a>, index: usize);
+    fn build_from<'a>(
+        adata: AssocData,
+        stmt: &mut sqlite::Statement<'a>,
+        index: usize,
+    ) -> DBResult<(Self, usize)>
+    where
+        Self: Sized;
+
+    fn accept_entity_visitor(_: &mut impl EntityVisitor) {}
+}
+
+/// A fixed-length list of EntityDatums, usually a tuple.
+pub trait DatumList {
+    fn accept(&self, visitor: &mut impl DatumVisitor);
+}
+
+/// A walker for a EntityDatumList instance.
+pub trait DatumVisitor {
+    fn visit<ED: Datum>(&mut self, datum: &ED);
+}
+
+// trait implementations for EntityDatumList
+mod datum_list;

+ 64 - 0
microrm/src/datum/datum_list.rs

@@ -0,0 +1,64 @@
+use super::{Datum, DatumList, DatumVisitor};
+
+impl DatumList for () {
+    fn accept(&self, _: &mut impl DatumVisitor) {}
+}
+
+impl<T: Datum> DatumList for T {
+    fn accept(&self, visitor: &mut impl DatumVisitor) {
+        visitor.visit(self);
+    }
+}
+
+impl<T0: Datum> DatumList for (T0,) {
+    fn accept(&self, visitor: &mut impl DatumVisitor) {
+        visitor.visit(&self.0);
+    }
+}
+
+impl<T0: Datum, T1: Datum> DatumList for (T0, T1) {
+    fn accept(&self, visitor: &mut impl DatumVisitor) {
+        visitor.visit(&self.0);
+        visitor.visit(&self.1);
+    }
+}
+
+impl<T0: Datum, T1: Datum, T2: Datum> DatumList for (T0, T1, T2) {
+    fn accept(&self, visitor: &mut impl DatumVisitor) {
+        visitor.visit(&self.0);
+        visitor.visit(&self.1);
+        visitor.visit(&self.2);
+    }
+}
+
+impl<T0: Datum, T1: Datum, T2: Datum, T3: Datum> DatumList for (T0, T1, T2, T3) {
+    fn accept(&self, visitor: &mut impl DatumVisitor) {
+        visitor.visit(&self.0);
+        visitor.visit(&self.1);
+        visitor.visit(&self.2);
+        visitor.visit(&self.3);
+    }
+}
+
+impl<T0: Datum, T1: Datum, T2: Datum, T3: Datum, T4: Datum> DatumList for (T0, T1, T2, T3, T4) {
+    fn accept(&self, visitor: &mut impl DatumVisitor) {
+        visitor.visit(&self.0);
+        visitor.visit(&self.1);
+        visitor.visit(&self.2);
+        visitor.visit(&self.3);
+        visitor.visit(&self.4);
+    }
+}
+
+impl<T0: Datum, T1: Datum, T2: Datum, T3: Datum, T4: Datum, T5: Datum> DatumList
+    for (T0, T1, T2, T3, T4, T5)
+{
+    fn accept(&self, visitor: &mut impl DatumVisitor) {
+        visitor.visit(&self.0);
+        visitor.visit(&self.1);
+        visitor.visit(&self.2);
+        visitor.visit(&self.3);
+        visitor.visit(&self.4);
+        visitor.visit(&self.5);
+    }
+}

+ 7 - 36
microrm/src/entity.rs

@@ -1,6 +1,10 @@
 use std::{fmt::Debug, hash::Hash};
 use std::{fmt::Debug, hash::Hash};
 
 
-use crate::{db::DBConnection, schema::AssocData, DBResult};
+use crate::{
+    datum::{Datum, DatumList},
+    db::DBConnection,
+    DBResult,
+};
 
 
 mod datum;
 mod datum;
 pub(crate) mod helpers;
 pub(crate) mod helpers;
@@ -16,46 +20,13 @@ pub trait EntityID: 'static + PartialEq + Hash + PartialOrd + Debug + Copy {
     fn into_raw(self) -> i64;
     fn into_raw(self) -> i64;
 }
 }
 
 
-// ----------------------------------------------------------------------
-// EntityDatum and related types
-// ----------------------------------------------------------------------
-
-/// Represents a data field in an Entity.
-pub trait EntityDatum: 'static {
-    fn sql_type() -> &'static str;
-
-    fn bind_to<'a>(&self, _stmt: &mut sqlite::Statement<'a>, index: usize);
-    fn build_from<'a>(
-        adata: AssocData,
-        stmt: &mut sqlite::Statement<'a>,
-        index: usize,
-    ) -> DBResult<(Self, usize)>
-    where
-        Self: Sized;
-
-    fn accept_entity_visitor(_: &mut impl EntityVisitor) {}
-}
-
-/// A fixed-length list of EntityDatums, usually a tuple.
-pub trait EntityDatumList {
-    fn accept(&self, visitor: &mut impl EntityDatumVisitor);
-}
-
-/// A walker for a EntityDatumList instance.
-pub trait EntityDatumVisitor {
-    fn visit<ED: EntityDatum>(&mut self, datum: &ED);
-}
-
-// trait implementations for EntityDatumList
-mod datum_list;
-
 // ----------------------------------------------------------------------
 // ----------------------------------------------------------------------
 // EntityPart and related types
 // EntityPart and related types
 // ----------------------------------------------------------------------
 // ----------------------------------------------------------------------
 
 
 /// A single data field in an Entity, automatically declared and derived as part of `#[derive(Entity)]`.
 /// A single data field in an Entity, automatically declared and derived as part of `#[derive(Entity)]`.
 pub trait EntityPart: 'static {
 pub trait EntityPart: 'static {
-    type Datum: EntityDatum;
+    type Datum: Datum;
     type Entity: Entity;
     type Entity: Entity;
 
 
     fn part_name() -> &'static str;
     fn part_name() -> &'static str;
@@ -70,7 +41,7 @@ pub trait EntityPartVisitor {
 
 
 /// List of EntityParts.
 /// List of EntityParts.
 pub trait EntityPartList: 'static {
 pub trait EntityPartList: 'static {
-    type DatumList: EntityDatumList;
+    type DatumList: DatumList;
 
 
     fn build_datum_list(
     fn build_datum_list(
         conn: &DBConnection,
         conn: &DBConnection,

+ 9 - 9
microrm/src/entity/datum.rs

@@ -1,8 +1,8 @@
 use crate::{schema::AssocData, DBResult};
 use crate::{schema::AssocData, DBResult};
 
 
-use super::EntityDatum;
+use super::Datum;
 
 
-impl EntityDatum for String {
+impl Datum for String {
     fn sql_type() -> &'static str {
     fn sql_type() -> &'static str {
         "text"
         "text"
     }
     }
@@ -24,7 +24,7 @@ impl EntityDatum for String {
     }
     }
 }
 }
 
 
-impl EntityDatum for usize {
+impl Datum for usize {
     fn sql_type() -> &'static str {
     fn sql_type() -> &'static str {
         "int"
         "int"
     }
     }
@@ -46,7 +46,7 @@ impl EntityDatum for usize {
     }
     }
 }
 }
 
 
-impl EntityDatum for isize {
+impl Datum for isize {
     fn sql_type() -> &'static str {
     fn sql_type() -> &'static str {
         "int"
         "int"
     }
     }
@@ -67,7 +67,7 @@ impl EntityDatum for isize {
     }
     }
 }
 }
 
 
-impl EntityDatum for u64 {
+impl Datum for u64 {
     fn sql_type() -> &'static str {
     fn sql_type() -> &'static str {
         "int"
         "int"
     }
     }
@@ -88,7 +88,7 @@ impl EntityDatum for u64 {
     }
     }
 }
 }
 
 
-impl EntityDatum for i64 {
+impl Datum for i64 {
     fn sql_type() -> &'static str {
     fn sql_type() -> &'static str {
         "int"
         "int"
     }
     }
@@ -109,7 +109,7 @@ impl EntityDatum for i64 {
     }
     }
 }
 }
 
 
-impl<T: EntityDatum> EntityDatum for Option<T> {
+impl<T: Datum> Datum for Option<T> {
     fn sql_type() -> &'static str {
     fn sql_type() -> &'static str {
         T::sql_type()
         T::sql_type()
     }
     }
@@ -131,7 +131,7 @@ impl<T: EntityDatum> EntityDatum for Option<T> {
     }
     }
 }
 }
 
 
-impl EntityDatum for bool {
+impl Datum for bool {
     fn sql_type() -> &'static str {
     fn sql_type() -> &'static str {
         "int"
         "int"
     }
     }
@@ -153,7 +153,7 @@ impl EntityDatum for bool {
     }
     }
 }
 }
 
 
-impl EntityDatum for Vec<u8> {
+impl Datum for Vec<u8> {
     fn sql_type() -> &'static str {
     fn sql_type() -> &'static str {
         "blob"
         "blob"
     }
     }

+ 0 - 68
microrm/src/entity/datum_list.rs

@@ -1,68 +0,0 @@
-use super::{EntityDatum, EntityDatumList, EntityDatumVisitor};
-
-impl EntityDatumList for () {
-    fn accept(&self, _: &mut impl EntityDatumVisitor) {}
-}
-
-impl<T: EntityDatum> EntityDatumList for T {
-    fn accept(&self, visitor: &mut impl EntityDatumVisitor) {
-        visitor.visit(self);
-    }
-}
-
-impl<T0: EntityDatum> EntityDatumList for (T0,) {
-    fn accept(&self, visitor: &mut impl EntityDatumVisitor) {
-        visitor.visit(&self.0);
-    }
-}
-
-impl<T0: EntityDatum, T1: EntityDatum> EntityDatumList for (T0, T1) {
-    fn accept(&self, visitor: &mut impl EntityDatumVisitor) {
-        visitor.visit(&self.0);
-        visitor.visit(&self.1);
-    }
-}
-
-impl<T0: EntityDatum, T1: EntityDatum, T2: EntityDatum> EntityDatumList for (T0, T1, T2) {
-    fn accept(&self, visitor: &mut impl EntityDatumVisitor) {
-        visitor.visit(&self.0);
-        visitor.visit(&self.1);
-        visitor.visit(&self.2);
-    }
-}
-
-impl<T0: EntityDatum, T1: EntityDatum, T2: EntityDatum, T3: EntityDatum> EntityDatumList
-    for (T0, T1, T2, T3)
-{
-    fn accept(&self, visitor: &mut impl EntityDatumVisitor) {
-        visitor.visit(&self.0);
-        visitor.visit(&self.1);
-        visitor.visit(&self.2);
-        visitor.visit(&self.3);
-    }
-}
-
-impl<T0: EntityDatum, T1: EntityDatum, T2: EntityDatum, T3: EntityDatum, T4: EntityDatum> EntityDatumList
-    for (T0, T1, T2, T3, T4)
-{
-    fn accept(&self, visitor: &mut impl EntityDatumVisitor) {
-        visitor.visit(&self.0);
-        visitor.visit(&self.1);
-        visitor.visit(&self.2);
-        visitor.visit(&self.3);
-        visitor.visit(&self.4);
-    }
-}
-
-impl<T0: EntityDatum, T1: EntityDatum, T2: EntityDatum, T3: EntityDatum, T4: EntityDatum, T5: EntityDatum> EntityDatumList
-    for (T0, T1, T2, T3, T4, T5)
-{
-    fn accept(&self, visitor: &mut impl EntityDatumVisitor) {
-        visitor.visit(&self.0);
-        visitor.visit(&self.1);
-        visitor.visit(&self.2);
-        visitor.visit(&self.3);
-        visitor.visit(&self.4);
-        visitor.visit(&self.5);
-    }
-}

+ 2 - 2
microrm/src/entity/helpers.rs

@@ -1,4 +1,4 @@
-use crate::entity::{Entity, EntityDatum, EntityVisitor};
+use crate::entity::{Datum, Entity, EntityVisitor};
 
 
 use super::EntityPart;
 use super::EntityPart;
 
 
@@ -10,6 +10,6 @@ pub fn check_assoc<EP: EntityPart>() -> bool {
         }
         }
     }
     }
     let mut checker = Checker(false);
     let mut checker = Checker(false);
-    <EP::Datum as EntityDatum>::accept_entity_visitor(&mut checker);
+    <EP::Datum as Datum>::accept_entity_visitor(&mut checker);
     checker.0
     checker.0
 }
 }

+ 18 - 5
microrm/src/entity/part_list.rs

@@ -1,10 +1,10 @@
 use crate::{db::DBConnection, schema::AssocData, DBResult};
 use crate::{db::DBConnection, schema::AssocData, DBResult};
 
 
-use super::{Entity, EntityDatum, EntityPart, EntityPartList, EntityPartVisitor};
+use super::{Datum, Entity, EntityPart, EntityPartList, EntityPartVisitor};
 
 
 macro_rules! build_datum {
 macro_rules! build_datum {
     ($conn:ident, $ctx:ident, $base_rowid:ident,$stmt:ident,$idx:ident,$d:ident,$t:ident) => {
     ($conn:ident, $ctx:ident, $base_rowid:ident,$stmt:ident,$idx:ident,$d:ident,$t:ident) => {
-        let ($d, $idx) = <$t::Datum as EntityDatum>::build_from(
+        let ($d, $idx) = <$t::Datum as Datum>::build_from(
             AssocData {
             AssocData {
                 conn: $conn.clone(),
                 conn: $conn.clone(),
                 ctx: $ctx,
                 ctx: $ctx,
@@ -206,10 +206,23 @@ impl<P0: EntityPart, P1: EntityPart, P2: EntityPart, P3: EntityPart, P4: EntityP
     }
     }
 }
 }
 
 
-impl<P0: EntityPart, P1: EntityPart, P2: EntityPart, P3: EntityPart, P4: EntityPart, P5: EntityPart> EntityPartList
-    for (P0, P1, P2, P3, P4, P5)
+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);
+    type DatumList = (
+        P0::Datum,
+        P1::Datum,
+        P2::Datum,
+        P3::Datum,
+        P4::Datum,
+        P5::Datum,
+    );
 
 
     fn build_datum_list(
     fn build_datum_list(
         conn: &DBConnection,
         conn: &DBConnection,

+ 3 - 2
microrm/src/lib.rs

@@ -1,6 +1,7 @@
 // to make the proc_macros work inside the microrm crate; needed for tests and the metaschema.
 // to make the proc_macros work inside the microrm crate; needed for tests and the metaschema.
 extern crate self as microrm;
 extern crate self as microrm;
 
 
+pub mod datum;
 pub mod db;
 pub mod db;
 pub mod entity;
 pub mod entity;
 mod query;
 mod query;
@@ -10,9 +11,9 @@ pub mod schema;
 pub use sqlite;
 pub use sqlite;
 
 
 pub mod prelude {
 pub mod prelude {
-    pub use microrm_macros::{Database, Entity};
     pub use crate::entity::Entity;
     pub use crate::entity::Entity;
-    pub use crate::schema::Database;
+    pub use crate::schema::{AssocMap, Database, IDMap};
+    pub use microrm_macros::{Database, Entity};
 }
 }
 
 
 // ----------------------------------------------------------------------
 // ----------------------------------------------------------------------

+ 14 - 11
microrm/src/query.rs

@@ -1,10 +1,8 @@
 use crate::entity::helpers::check_assoc;
 use crate::entity::helpers::check_assoc;
 use crate::schema::{AssocMap, IDWrap};
 use crate::schema::{AssocMap, IDWrap};
 use crate::{
 use crate::{
-    entity::{
-        Entity, EntityDatum, EntityDatumList, EntityDatumVisitor, EntityID, EntityPart,
-        EntityPartList, EntityPartVisitor,
-    },
+    datum::{Datum, DatumList, DatumVisitor},
+    entity::{Entity, EntityID, EntityPart, EntityPartList, EntityPartVisitor},
     schema::{EntityMap, IDMap},
     schema::{EntityMap, IDMap},
 };
 };
 use crate::{DBError, DBResult};
 use crate::{DBError, DBResult};
@@ -113,8 +111,8 @@ pub(crate) fn select_by<E: Entity, PL: EntityPartList>(
     by: &PL::DatumList,
     by: &PL::DatumList,
 ) -> DBResult<Vec<IDWrap<E>>> {
 ) -> DBResult<Vec<IDWrap<E>>> {
     struct HashDatumListTypes(std::collections::hash_map::DefaultHasher);
     struct HashDatumListTypes(std::collections::hash_map::DefaultHasher);
-    impl EntityDatumVisitor for HashDatumListTypes {
-        fn visit<ED: EntityDatum>(&mut self, _: &ED) {
+    impl DatumVisitor for HashDatumListTypes {
+        fn visit<ED: Datum>(&mut self, _: &ED) {
             std::any::TypeId::of::<ED>().hash(&mut self.0);
             std::any::TypeId::of::<ED>().hash(&mut self.0);
         }
         }
     }
     }
@@ -139,13 +137,14 @@ pub(crate) fn select_by<E: Entity, PL: EntityPartList>(
             }
             }
             PL::accept_part_visitor(&mut BuildConditions(&mut conditions));
             PL::accept_part_visitor(&mut BuildConditions(&mut conditions));
 
 
-            let table_name = format!("{}_{}", map.ctx(), E::entity_name());
+            // CTX let table_name = format!("{}_{}", map.ctx(), E::entity_name());
+            let table_name = format!("{}", E::entity_name());
             format!("select rowid, * from `{}` where {}", table_name, conditions)
             format!("select rowid, * from `{}` where {}", table_name, conditions)
         },
         },
         |stmt| {
         |stmt| {
             struct BindDatum<'a, 'b>(&'a mut sqlite::Statement<'b>, usize);
             struct BindDatum<'a, 'b>(&'a mut sqlite::Statement<'b>, usize);
-            impl<'a, 'b> EntityDatumVisitor for BindDatum<'a, 'b> {
-                fn visit<ED: EntityDatum>(&mut self, datum: &ED) {
+            impl<'a, 'b> DatumVisitor for BindDatum<'a, 'b> {
+                fn visit<ED: Datum>(&mut self, datum: &ED) {
                     datum.bind_to(self.0, self.1);
                     datum.bind_to(self.0, self.1);
                     self.1 += 1
                     self.1 += 1
                 }
                 }
@@ -157,7 +156,10 @@ pub(crate) fn select_by<E: Entity, PL: EntityPartList>(
             let mut rows = vec![];
             let mut rows = vec![];
             while stmt.next()? == sqlite::State::Row {
             while stmt.next()? == sqlite::State::Row {
                 let datum_list = <E::Parts>::build_datum_list(&map.conn(), map.ctx(), stmt)?;
                 let datum_list = <E::Parts>::build_datum_list(&map.conn(), map.ctx(), stmt)?;
-                rows.push(IDWrap::new(<E::ID>::from_raw(stmt.read::<i64, _>(0)?), E::build(datum_list)));
+                rows.push(IDWrap::new(
+                    <E::ID>::from_raw(stmt.read::<i64, _>(0)?),
+                    E::build(datum_list),
+                ));
             }
             }
 
 
             stmt.reset()?;
             stmt.reset()?;
@@ -174,7 +176,8 @@ pub(crate) fn insert<E: Entity>(
     map.conn().with_prepared(
     map.conn().with_prepared(
         query_hash::<E>(map.ctx(), QueryType::Insert),
         query_hash::<E>(map.ctx(), QueryType::Insert),
         || {
         || {
-            let table_name = format!("{}_{}", map.ctx(), E::entity_name());
+            // CTX let table_name = format!("{}_{}", map.ctx(), E::entity_name());
+            let table_name = format!("{}", E::entity_name());
 
 
             let mut part_names = String::new();
             let mut part_names = String::new();
             let mut placeholders = String::new();
             let mut placeholders = String::new();

+ 14 - 11
microrm/src/schema.rs

@@ -1,6 +1,7 @@
 use crate::{
 use crate::{
+    datum::Datum,
     db::{Connection, DBConnection},
     db::{Connection, DBConnection},
-    entity::{Entity, EntityDatum, EntityPartList, EntityVisitor},
+    entity::{Entity, EntityPartList, EntityVisitor},
     query,
     query,
 };
 };
 use crate::{DBError, DBResult};
 use crate::{DBError, DBResult};
@@ -116,7 +117,7 @@ impl<T: Entity> IDMap<T> {
     }
     }
 }
 }
 
 
-pub struct Index<T: Entity, Key: EntityDatum> {
+pub struct Index<T: Entity, Key: Datum> {
     _conn: DBConnection,
     _conn: DBConnection,
     _ghost: std::marker::PhantomData<(T, Key)>,
     _ghost: std::marker::PhantomData<(T, Key)>,
 }
 }
@@ -172,7 +173,7 @@ impl<T: Entity> EntityMap for AssocMap<T> {
     }
     }
 }
 }
 
 
-impl<T: Entity> EntityDatum for AssocMap<T> {
+impl<T: Entity> Datum for AssocMap<T> {
     fn sql_type() -> &'static str {
     fn sql_type() -> &'static str {
         unreachable!()
         unreachable!()
     }
     }
@@ -216,7 +217,9 @@ impl<T: serde::Serialize + serde::de::DeserializeOwned + Default> Default for Se
     }
     }
 }
 }
 
 
-impl<T: serde::Serialize + serde::de::DeserializeOwned + std::fmt::Debug> std::fmt::Debug for Serialized<T> {
+impl<T: serde::Serialize + serde::de::DeserializeOwned + std::fmt::Debug> std::fmt::Debug
+    for Serialized<T>
+{
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
         <T as std::fmt::Debug>::fmt(&self.wrapped, f)
         <T as std::fmt::Debug>::fmt(&self.wrapped, f)
     }
     }
@@ -234,16 +237,16 @@ impl<T: serde::Serialize + serde::de::DeserializeOwned> AsRef<T> for Serialized<
     }
     }
 }
 }
 
 
-impl<T: 'static + serde::Serialize + serde::de::DeserializeOwned> EntityDatum for Serialized<T> {
+impl<T: 'static + serde::Serialize + serde::de::DeserializeOwned> Datum for Serialized<T> {
     fn sql_type() -> &'static str {
     fn sql_type() -> &'static str {
         "text"
         "text"
     }
     }
 
 
     fn bind_to<'a>(&self, stmt: &mut sqlite::Statement<'a>, index: usize) {
     fn bind_to<'a>(&self, stmt: &mut sqlite::Statement<'a>, index: usize) {
-        <String as EntityDatum>::bind_to(
+        <String as Datum>::bind_to(
             &serde_json::to_string(&self.wrapped).expect("couldn't serialize object into JSON"),
             &serde_json::to_string(&self.wrapped).expect("couldn't serialize object into JSON"),
             stmt,
             stmt,
-            index
+            index,
         )
         )
     }
     }
 
 
@@ -253,9 +256,9 @@ impl<T: 'static + serde::Serialize + serde::de::DeserializeOwned> EntityDatum fo
         index: usize,
         index: usize,
     ) -> DBResult<(Self, usize)>
     ) -> DBResult<(Self, usize)>
     where
     where
-        Self: Sized {
-
-        let (s, idx) = <String as EntityDatum>::build_from(adata, stmt, index)?;
+        Self: Sized,
+    {
+        let (s, idx) = <String as Datum>::build_from(adata, stmt, index)?;
 
 
         let d = serde_json::from_str::<T>(s.as_str()).map_err(|e| DBError::JSON(e))?;
         let d = serde_json::from_str::<T>(s.as_str()).map_err(|e| DBError::JSON(e))?;
 
 
@@ -305,7 +308,7 @@ pub trait Database {
         let schema = build::collect_from_database::<Self>();
         let schema = build::collect_from_database::<Self>();
         match schema.check(conn.clone()) {
         match schema.check(conn.clone()) {
             // schema checks out
             // schema checks out
-            Some(true) => {},
+            Some(true) => {}
             // schema doesn't match
             // schema doesn't match
             Some(false) => Err(DBError::IncompatibleSchema)?,
             Some(false) => Err(DBError::IncompatibleSchema)?,
             // no schema found
             // no schema found

+ 16 - 15
microrm/src/schema/build.rs

@@ -1,7 +1,10 @@
-use crate::{schema::{
-    entity::{EntityStateContainer, PartType},
-    meta, DBConnection, Database, DatabaseItem, DatabaseItemVisitor,
-}, DBResult};
+use crate::{
+    schema::{
+        entity::{EntityStateContainer, PartType},
+        meta, DBConnection, Database, DatabaseItem, DatabaseItemVisitor,
+    },
+    DBResult,
+};
 
 
 #[derive(Debug)]
 #[derive(Debug)]
 struct ColumnInfo {
 struct ColumnInfo {
@@ -91,12 +94,10 @@ impl DatabaseSchema {
         }
         }
 
 
         // store signature
         // store signature
-        metadb
-            .kv_metastore
-            .insert(meta::KV {
-                key: Self::SCHEMA_SIGNATURE_KEY.into(),
-                value: format!("{}", self.signature),
-            })?;
+        metadb.kv_metastore.insert(meta::KV {
+            key: Self::SCHEMA_SIGNATURE_KEY.into(),
+            value: format!("{}", self.signature),
+        })?;
         Ok(())
         Ok(())
     }
     }
 }
 }
@@ -121,7 +122,8 @@ pub(crate) fn collect_from_database<DB: Database>() -> DatabaseSchema {
     let mut tables = std::collections::HashMap::new();
     let mut tables = std::collections::HashMap::new();
 
 
     for state in iv.0.iter_states() {
     for state in iv.0.iter_states() {
-        let table_name = format!("{}_{}", state.context, state.name);
+        // CTX let table_name = format!("{}_{}", state.context, state.name);
+        let table_name = state.name.to_string();
         // we may end up visiting duplicate entities; skip them if so
         // we may end up visiting duplicate entities; skip them if so
         if tables.contains_key(&table_name) {
         if tables.contains_key(&table_name) {
             continue;
             continue;
@@ -144,9 +146,7 @@ pub(crate) fn collect_from_database<DB: Database>() -> DatabaseSchema {
                     );
                     );
                     let mut assoc_table = TableInfo::new(assoc_table_name.clone());
                     let mut assoc_table = TableInfo::new(assoc_table_name.clone());
                     assoc_table.dependencies.push(table_name.clone());
                     assoc_table.dependencies.push(table_name.clone());
-                    assoc_table
-                        .dependencies
-                        .push(format!("{}_{}", state.context, assoc_name));
+                    assoc_table.dependencies.push(format!("{}", assoc_name));
 
 
                     assoc_table.columns.push(ColumnInfo {
                     assoc_table.columns.push(ColumnInfo {
                         name: "base".into(),
                         name: "base".into(),
@@ -158,7 +158,8 @@ pub(crate) fn collect_from_database<DB: Database>() -> DatabaseSchema {
                     assoc_table.columns.push(ColumnInfo {
                     assoc_table.columns.push(ColumnInfo {
                         name: "target".into(),
                         name: "target".into(),
                         ty: "int".into(),
                         ty: "int".into(),
-                        fkey: Some(format!("{}_{}(`id`)", state.context, assoc_name)),
+                        // CTX fkey: Some(format!("{}_{}(`id`)", state.context, assoc_name)),
+                        fkey: Some(format!("{}(`id`)", assoc_name)),
                         unique: false,
                         unique: false,
                     });
                     });
 
 

+ 8 - 3
microrm/src/schema/entity.rs

@@ -1,6 +1,7 @@
 use std::collections::HashMap;
 use std::collections::HashMap;
 
 
-use crate::entity::{Entity, EntityDatum, EntityPart, EntityPartVisitor, EntityVisitor};
+use crate::datum::Datum;
+use crate::entity::{Entity, EntityPart, EntityPartVisitor, EntityVisitor};
 
 
 #[derive(Debug)]
 #[derive(Debug)]
 pub enum PartType {
 pub enum PartType {
@@ -97,8 +98,12 @@ impl<'a> EntityVisitor for EntityContext<'a> {
         // 2. we've seen this entity in this context before
         // 2. we've seen this entity in this context before
         // 3. we haven't seen this entity in this context before, but we've seen one with an identical name in the same context
         // 3. we haven't seen this entity in this context before, but we've seen one with an identical name in the same context
 
 
-        if self.container.states.contains_key(&(self.context, E::entity_name())) {
-            return
+        if self
+            .container
+            .states
+            .contains_key(&(self.context, E::entity_name()))
+        {
+            return;
         }
         }
 
 
         let entry = self
         let entry = self

+ 2 - 3
microrm/src/schema/tests.rs

@@ -1,6 +1,7 @@
+#![allow(unused)]
+
 mod manual_test_db {
 mod manual_test_db {
     // simple hand-built database example
     // simple hand-built database example
-    #![allow(unused)]
 
 
     use crate::db::DBConnection;
     use crate::db::DBConnection;
     use crate::entity::{
     use crate::entity::{
@@ -229,8 +230,6 @@ mod derive_tests {
 }
 }
 
 
 mod mutual_relationship {
 mod mutual_relationship {
-    #![allow(unused)]
-
     use crate::schema::{AssocMap, Database, IDMap};
     use crate::schema::{AssocMap, Database, IDMap};
     use microrm_macros::{Database, Entity};
     use microrm_macros::{Database, Entity};