// simple hand-built database example #![allow(unused)] use microrm::db::{ConnectionPool, StatementContext, StatementRow}; use microrm::schema::datum::{ConcreteDatum, Datum}; use microrm::schema::entity::{Entity, EntityID, EntityPart, EntityPartVisitor, EntityVisitor}; use microrm::schema::{DatabaseItem, DatabaseItemVisitor, IDMap, Schema}; use test_log::test; #[derive(Debug)] struct SimpleEntity { name: String, } #[derive(Clone, Copy, Default, PartialEq, PartialOrd, Hash, Debug)] struct SimpleEntityID(i64); impl Datum for SimpleEntityID { fn sql_type() -> &'static str { "int" } fn accept_entity_visitor(_: &mut impl EntityVisitor) {} fn accept_discriminator(d: &mut impl microrm::schema::datum::DatumDiscriminator) where Self: Sized, { d.visit_entity_id::<::Entity>(); } fn bind_to<'a>(&self, _stmt: &mut StatementContext<'a>, _index: i32) { todo!() } fn build_from<'a>( _rdata: microrm::schema::relation::RelationData, _stmt: &mut StatementRow, _index: &mut i32, ) -> microrm::DBResult where Self: Sized, { todo!() } } impl ConcreteDatum for SimpleEntityID {} impl EntityID for SimpleEntityID { type Entity = SimpleEntity; fn from_raw(id: i64) -> Self { Self(id) } fn into_raw(self) -> i64 { self.0 } } #[derive(Clone, Copy, Default)] struct SimpleEntityIDPart; impl EntityPart for SimpleEntityIDPart { type Datum = SimpleEntityID; type Entity = SimpleEntity; fn unique() -> bool { true } fn part_name() -> &'static str { "id" } fn desc() -> Option<&'static str> { None } fn get_datum(_from: &Self::Entity) -> &Self::Datum { unreachable!() } } #[derive(Clone, Default)] struct SimpleEntityName; impl EntityPart for SimpleEntityName { type Datum = String; type Entity = SimpleEntity; fn part_name() -> &'static str { "name" } fn unique() -> bool { true } fn desc() -> Option<&'static str> { None } fn get_datum(from: &Self::Entity) -> &Self::Datum { &from.name } } impl Entity for SimpleEntity { type Parts = SimpleEntityName; type Keys = SimpleEntityName; type ID = SimpleEntityID; type IDPart = SimpleEntityIDPart; fn build(name: String) -> Self { Self { name } } fn entity_name() -> &'static str { "simple_entity" } fn accept_part_visitor(visitor: &mut impl EntityPartVisitor) { visitor.visit::(); } fn accept_part_visitor_ref(&self, visitor: &mut impl EntityPartVisitor) { visitor.visit_datum::(&self.name); } fn accept_part_visitor_mut(&mut self, visitor: &mut impl EntityPartVisitor) { visitor.visit_datum_mut::(&mut self.name); } } #[derive(Default)] struct SimpleDatabase { strings: IDMap, } impl Schema for SimpleDatabase { fn accept_item_visitor(&self, visitor: &mut impl DatabaseItemVisitor) where Self: Sized, { as DatabaseItem>::accept_item_visitor(&IDMap::default(), visitor); } } #[test] fn part_visitor() { struct V { v: Vec, _ghost: std::marker::PhantomData, } impl EntityPartVisitor for V { type Entity = E; fn visit(&mut self) { self.v.push(std::any::TypeId::of::()); } } let mut vis = V { v: vec![], _ghost: Default::default(), }; SimpleEntity::accept_part_visitor(&mut vis); assert_eq!( vis.v.as_slice(), &[std::any::TypeId::of::()] ); }