From ae6efed243e2ba2a611014707368de6a86d74940 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 15 Jun 2015 13:22:36 +0200 Subject: Support for inverse on points_to --- common/inverse/driver.cxx | 79 +++++++++++++++++++++++++++++++++++++++++++++++ common/inverse/test.hxx | 79 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 158 insertions(+) (limited to 'common') diff --git a/common/inverse/driver.cxx b/common/inverse/driver.cxx index a4a0fc8..ed02509 100644 --- a/common/inverse/driver.cxx +++ b/common/inverse/driver.cxx @@ -325,6 +325,85 @@ main (int argc, char* argv[]) } } #endif + + // Test inverse based on points_to. + // + { + using namespace test3; + + { + obj1 o1 (1, 2); + o1.o2 = new obj2; + + { + transaction t (db->begin ()); + + o1.o2->o1 = db->persist (o1); + db->persist (o1.o2); + + t.commit (); + } + + { + transaction t (db->begin ()); + + auto_ptr p (db->load (o1.id)); + assert (p->o2->id == o1.o2->id); + + t.commit (); + } + + { + typedef odb::query query; + + transaction t (db->begin ()); + + auto_ptr p (db->query_one (query::o2->o1.i == o1.id.i && + query::o2->o1.j == o1.id.j)); + assert (p->o2->id == o1.o2->id); + + t.commit (); + } + } + + { + obj3 o3; + o3.o4.push_back (new obj4); + o3.o4.push_back (new obj4); + + { + transaction t (db->begin ()); + + o3.o4[0]->o3 = o3.o4[1]->o3 = db->persist (o3); + db->persist (o3.o4[0]); + db->persist (o3.o4[1]); + + t.commit (); + } + + { + transaction t (db->begin ()); + + auto_ptr p (db->load (o3.id)); + assert (p->o4[0]->id == o3.o4[0]->id); + assert (p->o4[1]->id == o3.o4[1]->id); + + t.commit (); + } + + { + typedef odb::query query; + + transaction t (db->begin ()); + + auto_ptr p (db->query_one (query::id == o3.id)); + assert (p->o4[0]->id == o3.o4[0]->id); + assert (p->o4[1]->id == o3.o4[1]->id); + + t.commit (); + } + } + } } catch (const odb::exception& e) { diff --git a/common/inverse/test.hxx b/common/inverse/test.hxx index a245084..52a525d 100644 --- a/common/inverse/test.hxx +++ b/common/inverse/test.hxx @@ -232,4 +232,83 @@ namespace test2 } #endif +// Test inverse based on points_to. +// +#pragma db namespace table("t3_") +namespace test3 +{ + // Inverse pointer. + // + #pragma db value + struct comp + { + int i; + int j; + }; + + inline bool + operator< (comp x, comp y) {return x.i < y.i || (x.i == y.i && x.j < y.j);} + + struct obj2; + + #pragma db object + struct obj1 + { + #pragma db id + comp id; + + #pragma db inverse(o1) + obj2* o2; + + obj1 (int i = 0, int j = 0): o2 (0) {id.i = i; id.j = j;} + ~obj1 (); + }; + + #pragma db object + struct obj2 + { + #pragma db id auto + int id; + + #pragma db points_to(obj1) on_delete(cascade) + comp o1; + }; + + inline obj1:: + ~obj1 () {delete o2;} + + // Inverse container of pointers. + // + struct obj4; + + #pragma db object + struct obj3 + { + #pragma db id auto + int id; + + #pragma db inverse(o3) + std::vector o4; + + ~obj3 (); + }; + + #pragma db object + struct obj4 + { + #pragma db id auto + int id; + + #pragma db points_to(obj3) + int o3; + }; + + inline obj3:: + ~obj3 () + { + for (std::vector::iterator i (o4.begin ()); i != o4.end (); ++i) + delete *i; + } +}; + #endif // TEST_HXX -- cgit v1.1