From c8458a266eb258cf61b1e66066c72a2dc0f0ad8b Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 27 Apr 2012 11:36:43 +0200 Subject: Add support for NULL pointers to objects with composite object ids --- common/composite-id/driver.cxx | 71 ++++++++++++++++++++++++++++++++++++++++++ common/composite-id/test.hxx | 54 +++++++++++++++++++++++++++++++- 2 files changed, 124 insertions(+), 1 deletion(-) diff --git a/common/composite-id/driver.cxx b/common/composite-id/driver.cxx index 2066335..8a230eb 100644 --- a/common/composite-id/driver.cxx +++ b/common/composite-id/driver.cxx @@ -624,6 +624,77 @@ main (int argc, char* argv[]) } } + // Test 8. + // + { + using namespace test8; + + object2 o2a, o2b; + object3 o3; + + o2b.o1 = new object1 (scomp ("222", "aaa", "bbb"), 123); + o3.o1.push_back (0); + o3.o1.push_back (new object1 (scomp ("333", "aaa", "bbb"), 234)); + + // Persist. + // + { + transaction t (db->begin ()); + db->persist (o2a); + db->persist (o2b); + db->persist (o2b.o1); + db->persist (o3); + db->persist (o3.o1[1]); + t.commit (); + } + + // Load. + // + { + transaction t (db->begin ()); + auto_ptr p2a (db->load (o2a.id)); + auto_ptr p2b (db->load (o2b.id)); + auto_ptr p3 (db->load (o3.id)); + t.commit (); + + assert (p2a->o1 == 0); + assert (p2b->o1 != 0 && *p2b->o1 == *o2b.o1); + assert (p3->o1[0] == 0); + assert (p3->o1[1] != 0 && *p3->o1[1] == *o3.o1[1]); + } + + // Update. + // + { + object1* o1 (o3.o1[1]); + + o3.o1.clear (); + o3.o1.push_back (o2b.o1); + o3.o1.push_back (0); + + o2a.o1 = o1; + o2b.o1 = 0; + + transaction t (db->begin ()); + db->update (o2a); + db->update (o2b); + db->update (o3); + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr p2a (db->load (o2a.id)); + auto_ptr p2b (db->load (o2b.id)); + auto_ptr p3 (db->load (o3.id)); + t.commit (); + + assert (p2a->o1 != 0 && *p2a->o1 == *o2a.o1); + assert (p2b->o1 == 0); + assert (p3->o1[0] != 0 && *p3->o1[0] == *o3.o1[0]); + assert (p3->o1[1] == 0); + } + } } catch (const odb::exception& e) { diff --git a/common/composite-id/test.hxx b/common/composite-id/test.hxx index 512c980..9c2b2b3 100644 --- a/common/composite-id/test.hxx +++ b/common/composite-id/test.hxx @@ -414,8 +414,60 @@ namespace test7 inline bool operator== (const object& x, const object& y) { - return x.id == y.id && x.ver == y.ver && x.num == y.num ; + return x.id == y.id && x.ver == y.ver && x.num == y.num; } } +// Test composite NULL pointers. +// +#pragma db namespace table("t8_") +namespace test8 +{ + #pragma db object + struct object1 + { + object1 () {} + object1 (scomp const& i, unsigned long n): id (i), num (n) {} + + #pragma db id + scomp id; + + unsigned long num; + }; + + inline bool + operator== (const object1& x, const object1& y) + { + return x.id == y.id && x.num == y.num; + } + + #pragma db object + struct object2 + { + object2 (): o1 (0) {} + ~object2 () {delete o1;} + + #pragma db id auto + unsigned long id; + + object1* o1; + }; + + #pragma db object + struct object3 + { + ~object3 () + { + for (std::vector::iterator i (o1.begin ()); + i != o1.end (); ++i) + delete *i; + } + + #pragma db id auto + unsigned long id; + + std::vector o1; + }; +} + #endif // TEST_HXX -- cgit v1.1