From 635d11d74cd873e6f53d05b1d9f091ae112402a5 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 23 Apr 2012 16:48:03 +0200 Subject: Polymorphic inheritance support --- common/callback/test.std | 28 +- common/inheritance/driver.cxx | 17 + common/inheritance/test.hxx | 12 +- common/makefile | 1 + common/polymorphism/driver.cxx | 1830 ++++++++++++++++++++++++++++++++++ common/polymorphism/makefile | 113 +++ common/polymorphism/test.hxx | 987 ++++++++++++++++++ common/polymorphism/test.std | 36 + common/query/driver.cxx | 14 +- common/readonly/driver.cxx | 12 +- common/readonly/test.hxx | 12 + common/relationship-query/driver.cxx | 4 +- common/relationship-query/test.hxx | 9 +- 13 files changed, 3052 insertions(+), 23 deletions(-) create mode 100644 common/polymorphism/driver.cxx create mode 100644 common/polymorphism/makefile create mode 100644 common/polymorphism/test.hxx create mode 100644 common/polymorphism/test.std diff --git a/common/callback/test.std b/common/callback/test.std index c1afa36..b34a8de 100644 --- a/common/callback/test.std +++ b/common/callback/test.std @@ -1,6 +1,6 @@ persist - pre_persist 1 - post_persist 1 + pre_persist 1 const + post_persist 1 const pre_persist 2 const post_persist 2 const *** @@ -21,8 +21,8 @@ update post_load 1 pre_load 0 post_load 2 - pre_update 1 - post_update 1 + pre_update 1 const + post_update 1 const pre_update 2 const post_update 2 const *** @@ -33,18 +33,18 @@ erase post_load 2 pre_erase 1 const post_erase 1 const - pre_erase 2 - post_erase 2 + pre_erase 2 const + post_erase 2 const *** delayed load - pre_persist 1 - post_persist 1 - pre_persist 2 - post_persist 2 - pre_persist 3 - post_persist 3 - pre_persist 4 - post_persist 4 + pre_persist 1 const + post_persist 1 const + pre_persist 2 const + post_persist 2 const + pre_persist 3 const + post_persist 3 const + pre_persist 4 const + post_persist 4 const pre_load 0 pre_load 0 pre_load 0 diff --git a/common/inheritance/driver.cxx b/common/inheritance/driver.cxx index a0e3725..466ed33 100644 --- a/common/inheritance/driver.cxx +++ b/common/inheritance/driver.cxx @@ -161,6 +161,23 @@ main (int argc, char* argv[]) t.commit (); } + + // views + // + { + typedef odb::query query; + typedef odb::result result; + + transaction t (db->begin ()); + + result r (db->query (query::num == o2.num_)); + result::iterator i (r.begin ()); + assert (i != r.end () && + i->num == o2.num_ && i->id == o2.id_ && i->str == o2.str_); + assert (++i == r.end ()); + + t.commit (); + } } catch (const odb::exception& e) { diff --git a/common/inheritance/test.hxx b/common/inheritance/test.hxx index d679c46..6059337 100644 --- a/common/inheritance/test.hxx +++ b/common/inheritance/test.hxx @@ -90,7 +90,7 @@ struct object1: base #pragma db object struct object2: base { - #pragma db column ("derived_str") + #pragma db column("derived_str") std::string str_; bool @@ -145,4 +145,14 @@ struct empty: base { }; +// View based on the derived object. +// +#pragma db view object(object2) +struct object2_view +{ + unsigned int num; // from abstract_base + unsigned long id; // from base + std::string str; // from object2, hides one from abstract_base +}; + #endif // TEST_HXX diff --git a/common/makefile b/common/makefile index 886e4fa..3ec0aa2 100644 --- a/common/makefile +++ b/common/makefile @@ -24,6 +24,7 @@ lazy-ptr \ lifecycle \ no-id \ optimistic \ +polymorphism \ pragma \ query \ readonly \ diff --git a/common/polymorphism/driver.cxx b/common/polymorphism/driver.cxx new file mode 100644 index 0000000..efda095 --- /dev/null +++ b/common/polymorphism/driver.cxx @@ -0,0 +1,1830 @@ +// file : common/polymorphism/driver.cxx +// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +// Test polymorphic object inheritance. +// + +#include // std::auto_ptr +#include +#include + +#include +#include + +#include + +#include "test.hxx" +#include "test-odb.hxx" + +using namespace std; +using namespace odb::core; + +const char* events[] = +{ + "pre_persist", + "post_persist", + "pre_load", + "post_load", + "pre_update", + "post_update", + "pre_erase", + "post_erase" +}; + +namespace test6 +{ + void base:: + db_callback (callback_event e, database&) + { + cout << "base " << events[e] << " " << id << endl; + } + + void base:: + db_callback (callback_event e, database&) const + { + cout << "base " << events[e] << " " << id << " const" << endl; + } + + void derived:: + db_callback (callback_event e, database&) const + { + cout << "derived " << events[e] << " " << id << " const" << endl; + } +} + +int +main (int argc, char* argv[]) +{ + try + { + auto_ptr db (create_database (argc, argv)); + + // Test 1: basic polymorphism functionality. + // + { + using namespace test1; + + root r (1, 1); + base b (2, 2, "bbb"); + derived d (3, 3, "ddd"); + + r.strs.push_back ("a"); + r.strs.push_back ("aa"); + r.strs.push_back ("aaa"); + + b.nums.push_back (21); + b.nums.push_back (22); + b.nums.push_back (23); + b.strs.push_back ("b"); + b.strs.push_back ("bb"); + b.strs.push_back ("bbb"); + + d.nums.push_back (31); + d.nums.push_back (32); + d.nums.push_back (33); + d.strs.push_back ("d"); + d.strs.push_back ("dd"); + d.strs.push_back ("ddd"); + + { + transaction t (db->begin ()); + + // Static persist. + // + db->persist (r); + db->persist (b); + + // Dynamic persist. + // + root& r (d); + db->persist (r); + + t.commit (); + } + + // Static load. + // + { + transaction t (db->begin ()); + auto_ptr pr (db->load (r.id)); + auto_ptr pb (db->load (b.id)); + auto_ptr pd (db->load (d.id)); + t.commit (); + + assert (*pr == r); + assert (*pb == b); + assert (*pd == d); + } + + // Dynamic load. + // + { + transaction t (db->begin ()); + auto_ptr pb (db->load (b.id)); + auto_ptr pd1 (db->load (d.id)); + auto_ptr pd2 (db->load (d.id)); + t.commit (); + + assert (*pb == b); + assert (*pd1 == d); + assert (*pd2 == d); + } + + // Invalid load. + // + { + transaction t (db->begin ()); + + try + { + auto_ptr p (db->load (r.id)); + assert (false); + } + catch (const object_not_persistent&) {} + + try + { + auto_ptr p (db->load (b.id)); + assert (false); + } + catch (const object_not_persistent&) {} + + t.commit (); + } + + // Static load into existing instance. + // + { + transaction t (db->begin ()); + root r1; + db->load (r.id, r1); + base b1; + db->load (b.id, b1); + derived d1; + db->load (d.id, d1); + t.commit (); + + assert (r1 == r); + assert (b1 == b); + assert (d1 == d); + } + + // Dynamic load into existing instance. + // + { + transaction t (db->begin ()); + base b1; + db->load (b.id, static_cast (b1)); + derived d1; + db->load (d.id, static_cast (d1)); + t.commit (); + + assert (b1 == b); + assert (d1 == d); + } + + // Invalid load into existing instance. + // + { + transaction t (db->begin ()); + + try + { + base b; + db->load (r.id, static_cast (b)); + assert (false); + } + catch (const object_not_persistent&) {} + + try + { + derived d; + db->load (b.id, static_cast (d)); + assert (false); + } + catch (const object_not_persistent&) {} + + t.commit (); + } + + // Slicing load. + // + { + transaction t (db->begin ()); + root b1; + db->load (b.id, b1); + base d1; + db->load (d.id, d1); + t.commit (); + + assert (b1 == static_cast (b)); + assert (d1 == static_cast (d)); + } + + // Static reload. + // + { + transaction t (db->begin ()); + root r1; + r1.id = r.id; + db->reload (r1); + base b1; + b1.id = b.id; + db->reload (b1); + derived d1; + d1.id = d.id; + db->reload (d1); + t.commit (); + + assert (r1 == r); + assert (b1 == b); + assert (d1 == d); + } + + // Dynamic reload. + // + { + transaction t (db->begin ()); + base b1; + b1.id = b.id; + db->reload (static_cast (b1)); + derived d1; + d1.id = d.id; + db->reload (static_cast (d1)); + t.commit (); + + assert (b1 == b); + assert (d1 == d); + } + + // Invalid reload. + // + { + transaction t (db->begin ()); + + try + { + base b; + b.id = r.id; + db->reload (static_cast (b)); + assert (false); + } + catch (const object_not_persistent&) {} + + try + { + derived d; + d.id = b.id; + db->reload (static_cast (d)); + assert (false); + } + catch (const object_not_persistent&) {} + + t.commit (); + } + + // Slicing reload. + // + { + transaction t (db->begin ()); + root b1; + b1.id = b.id; + db->reload (b1); + base d1; + d1.id = d.id; + db->reload (d1); + t.commit (); + + assert (b1 == static_cast (b)); + assert (d1 == static_cast (d)); + } + + // Query. + // + { + typedef odb::query root_query; + typedef odb::result root_result; + + typedef odb::query base_query; + typedef odb::result base_result; + + typedef odb::query derived_query; + typedef odb::result derived_result; + + transaction t (db->begin ()); + + // Test loading via root. + // + { + root_result qr (db->query ("ORDER BY" + root_query::id)); + root_result::iterator i (qr.begin ()), e (qr.end ()); + + assert (i != e && *i == r); + assert (++i != e && *i == b); + assert (++i != e && *i == d); + assert (++i == e); + } + + // Test loading via base. + // + { + base_result qr (db->query ("ORDER BY" + base_query::id)); + base_result::iterator i (qr.begin ()), e (qr.end ()); + + assert (i != e && *i == b); + assert (++i != e && *i == d); + assert (++i == e); + } + + // Test loading via derived. + // + { + derived_result qr (db->query ()); + derived_result::iterator i (qr.begin ()), e (qr.end ()); + + assert (i != e && *i == d); + assert (++i == e); + } + + // Test loading into an existing instance. + // + { + root_result qr (db->query ()); + + unsigned short mask (0); + + for (root_result::iterator i (qr.begin ()); i != qr.end (); ++i) + { + string ds (i.discriminator ()); + + if (ds == "test1::root") + { + root r1; + i.load (r1); + assert (r1 == r); + mask |= 1; + } + else if (ds == "test1::base") + { + base b1; + i.load (b1); + assert (b1 == b); + mask |= 2; + } + else if (ds == "test1::derived") + { + derived d1; + i.load (d1); + assert (d1 == d); + mask |= 4; + } + else + assert (false); + } + + assert (mask == 7); + } + + // Test query conditions with columns from multiple tables. + // + { + base_result qr ( + db->query ( + base_query::num == 3 && base_query::str == "ddd")); + + base_result::iterator i (qr.begin ()), e (qr.end ()); + + assert (i != e && *i == d); + assert (++i == e); + } + + // Test discriminator access. + // + { + base_result qr (db->query (base_query::id == 3)); + base_result::iterator i (qr.begin ()), e (qr.end ()); + + assert (i != e && i.discriminator () == "test1::derived"); + assert (++i == e); + } + + // Test loading of an object from the same hierarchy during + // query result iteration (tests image copying via change + // callbacks in some databases). + // + { + base_result qr (db->query ()); + + unsigned short mask (0); + + for (base_result::iterator i (qr.begin ()); i != qr.end (); ++i) + { + string ds (i.discriminator ()); + + if (ds == "test1::base") + { + auto_ptr d1 (db->load (d.id)); + assert (*d1 == d); + assert (*i == b); + mask |= 1; + } + else if (ds == "test1::derived") + { + auto_ptr b1 (db->load (b.id)); + assert (*b1 == b); + assert (*i == d); + mask |= 2; + } + } + + assert (mask == 3); + } + + t.commit (); + } + + // Views. + // + { + typedef odb::query root_query; + typedef odb::result root_result; + + typedef odb::query base_query; + typedef odb::result base_result; + + typedef odb::query derived_query; + typedef odb::result derived_result; + + transaction t (db->begin ()); + + // root + // + { + root_result qr (db->query ("ORDER BY" + root_query::id)); + root_result::iterator i (qr.begin ()), e (qr.end ()); + + assert (i != e && i->typeid_ == "test1::root" && i->num == r.num); + assert (++i != e && i->typeid_ == "test1::base" && i->num == b.num); + assert (++i != e && i->typeid_ == "test1::derived" && i->num == d.num); + assert (++i == e); + } + + // base + // + { + base_result qr (db->query ("ORDER BY" + base_query::id)); + base_result::iterator i (qr.begin ()), e (qr.end ()); + + assert (i != e && + i->id == b.id && i->num == b.num && i->str == b.str); + assert (++i != e && + i->id == d.id && i->num == d.num && i->str == d.str); + assert (++i == e); + } + + // derived + // + { + derived_result qr (db->query ()); + derived_result::iterator i (qr.begin ()), e (qr.end ()); + + assert (i != e && + i->num == d.num && i->str == d.str && + i->dnum == d.dnum && i->dstr == d.dstr); + assert (++i == e); + } + + t.commit (); + } + + // Update. + // + r.num++; + r.strs.push_back ("aaaa"); + + b.num++; + b.str += "b"; + b.nums.push_back (24); + b.strs.push_back ("bbbb"); + + d.num++; + d.str += "d"; + d.dnum++; + d.dstr += "d"; + d.nums.push_back (34); + d.strs.push_back ("dddd"); + + { + transaction t (db->begin ()); + + // Static update. + // + db->update (r); + db->update (b); + + // Dynamic update. + // + root& r (d); + db->update (r); + + t.commit (); + } + + // Verify update. + // + { + transaction t (db->begin ()); + auto_ptr pr (db->load (r.id)); + auto_ptr pb (db->load (b.id)); + auto_ptr pd (db->load (d.id)); + t.commit (); + + assert (*pr == r); + assert (*pb == b); + assert (*pd == d); + } + + // Invalid erase via id. + // + { + transaction t (db->begin ()); + + try + { + db->erase (r.id); + assert (false); + } + catch (const object_not_persistent&) {} + + try + { + db->erase (b.id); + assert (false); + } + catch (const object_not_persistent&) {} + + t.commit (); + } + + // Static erase via id. + // + { + transaction t (db->begin ()); + db->erase (r.id); + db->erase (b.id); + t.commit (); + } + + // Dynamic erase via id. + // + { + transaction t (db->begin ()); + db->erase (d.id); + t.commit (); + } + + { + transaction t (db->begin ()); + db->persist (r); + db->persist (b); + db->persist (d); + t.commit (); + } + + // Static erase via object. + // + { + transaction t (db->begin ()); + db->erase (r); + db->erase (b); + t.commit (); + } + + // Dynamic erase via object. + // + { + const root& r (d); + transaction t (db->begin ()); + db->erase (r); + t.commit (); + } + } + + // Test 2: inverse object pointers in polymorhic bases. + // + { + using namespace test2; + + derived d (1, "d", 1); + root_pointer rp (&d); + base_pointer bp (&d); + + { + transaction t (db->begin ()); + db->persist (rp); + db->persist (bp); + + d.rp.reset (*db, &rp); + d.bp.reset (*db, &bp); + + db->persist (d); + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr pd (db->load (d.id)); + auto_ptr pb (db->load (d.id)); + auto_ptr pr (db->load (d.id)); + t.commit (); + + assert (pd->rp.object_id () == rp.id && + pd->bp.object_id () == bp.id); + + derived* p (dynamic_cast (pb.get ())); + assert (p != 0 && + p->rp.object_id () == rp.id && + p->bp.object_id () == bp.id); + + p = dynamic_cast (pr.get ()); + assert (p != 0 && + p->rp.object_id () == rp.id && + p->bp.object_id () == bp.id); + } + + // Query. + // + { + typedef odb::query base_query; + typedef odb::result base_result; + + transaction t (db->begin ()); + + // Test query conditions with columns in pointed-to objects from + // multiple tables. + // + { + base_result qr ( + db->query ( + base_query::rp->id == rp.id && + base_query::bp->id == bp.id)); + + base_result::iterator i (qr.begin ()), e (qr.end ()); + + assert (i != e && i.discriminator () == "test2::derived"); + assert (++i == e); + } + + t.commit (); + } + + // Views. + // + { + typedef odb::query root_query; + typedef odb::result root_result; + + typedef odb::query base_query; + typedef odb::result base_result; + + transaction t (db->begin ()); + + // root + // + { + root_result qr (db->query ()); + root_result::iterator i (qr.begin ()), e (qr.end ()); + + assert (i != e && i->rp_id == rp.id && i->r_id == d.id); + assert (++i == e); + } + + // base + // + { + base_result qr (db->query ()); + base_result::iterator i (qr.begin ()), e (qr.end ()); + + assert (i != e && + i->bp_id == bp.id && i->b_id == d.id && i->str == d.str); + assert (++i == e); + } + + t.commit (); + } + } + + // Test 3: delayed loading. + // + { + using namespace test3; + + base b1 (21, 21); + base b2 (22, 22); + base b3 (23, 23); + + derived d1 (31, 31, "d"); + derived d2 (32, 32, "dd"); + derived d3 (33, 33, "ddd"); + + b1.rptr = new root (1); + b2.rptr = new base (2, 2); + b3.rptr = new derived (3, 3, "b3"); + + d1.rptr = new root (4); + d2.rptr = new base (5, 5); + d3.rptr = new derived (6, 6, "d3"); + + d2.bptr = new base (7, 7); + d3.bptr = new derived (8, 8, "d3b"); + + { + transaction t (db->begin ()); + db->persist (b1); + db->persist (b2); + db->persist (b3); + + db->persist (d1); + db->persist (d2); + db->persist (d3); + + db->persist (b1.rptr); + db->persist (b2.rptr); + db->persist (b3.rptr); + + db->persist (d1.rptr); + db->persist (d2.rptr); + db->persist (d3.rptr); + + db->persist (d2.bptr); + db->persist (d3.bptr); + + t.commit (); + } + + { + transaction t (db->begin ()); + + { + auto_ptr p1 (db->load (b1.id)); + auto_ptr p2 (db->load (b2.id)); + auto_ptr p3 (db->load (b3.id)); + assert (*p1 == b1); + assert (*p2 == b2); + assert (*p3 == b3); + } + + { + auto_ptr p1 (db->load (d1.id)); + auto_ptr p2 (db->load (d2.id)); + auto_ptr p3 (db->load (d3.id)); + assert (*p1 == d1); + assert (*p2 == d2); + assert (*p3 == d3); + } + + t.commit (); + } + + // Query. + // + { + typedef odb::query derived_query; + typedef odb::result derived_result; + + transaction t (db->begin ()); + + // Test query conditions with columns in pointed-to objects from + // multiple tables. + // + { + derived_result qr ( + db->query ( + derived_query::rptr->id == 6 && + derived_query::bptr->id == 8 && + derived_query::bptr->num == 8)); + + derived_result::iterator i (qr.begin ()), e (qr.end ()); + + assert (i != e && *i == d3); + assert (++i == e); + } + + t.commit (); + } + + // Views. + // + { + typedef odb::query base_query; + typedef odb::result base_result; + + typedef odb::query derived_query; + typedef odb::result derived_result; + + typedef odb::query root_query; + typedef odb::result root_result; + + transaction t (db->begin ()); + + // base + // + { + base_result qr ( + db->query ( + base_query::base::num == b2.num && + base_query::base::id == b2.id && + base_query::r::id == b2.rptr->id)); + + base_result::iterator i (qr.begin ()), e (qr.end ()); + + assert (i != e && + i->b_id == b2.id && + i->r_id == b2.rptr->id && + i->num == b2.num); + assert (++i == e); + } + + // derived + // + { + derived_result qr ( + db->query ( + derived_query::d::str == d3.str && + derived_query::d::num == d3.num && + derived_query::b::num == d3.bptr->num && + derived_query::d::id == d3.id && + derived_query::b::id == d3.bptr->id && + derived_query::r::id == d3.rptr->id)); + + derived_result::iterator i (qr.begin ()), e (qr.end ()); + + assert (i != e && + i->d_id == d3.id && + i->b_id == d3.bptr->id && + i->r_id == d3.rptr->id && + i->d_num == d3.num && + i->b_num == d3.bptr->num && + i->str == d3.str); + assert (++i == e); + } + + // root + // + { + root_result qr ( + db->query ( + root_query::r::id.in (b2.rptr->id, d2.rptr->id))); + + root_result::iterator i (qr.begin ()), e (qr.end ()); + + assert (i != e && + i->r_id == d2.rptr->id && + i->d_id == d2.id && + i->str == d2.str); + assert (++i == e); + } + + t.commit (); + } + } + + // Test 4: views. + // + { + using namespace test4; + + base1 b1 (21, 1); + + root2 r2 (11, 0); + base2 b2 (21, 1, "abc"); + + { + transaction t (db->begin ()); + db->persist (b1); + db->persist (r2); + db->persist (b2); + t.commit (); + } + + { + typedef odb::query query; + typedef odb::result result; + + transaction t (db->begin ()); + + { + result qr ( + db->query ( + query::base1::num == b1.num)); + + result::iterator i (qr.begin ()), e (qr.end ()); + + assert (i != e && i->str == "abc"); + assert (++i == e); + } + + t.commit (); + } + + { + typedef odb::query query; + typedef odb::result result; + + transaction t (db->begin ()); + + { + result qr (db->query ()); + result::iterator i (qr.begin ()), e (qr.end ()); + + assert (i != e && i->min_num == 1); + assert (++i == e); + } + + t.commit (); + } + } + + // Test 5: polymorphism and optimistic concurrency. + // + { + using namespace test5; + + root r (1, 1); + base b (2, 2, "bbb"); + derived d (3, 3, "ddd"); + + r.strs.push_back ("a"); + r.strs.push_back ("aa"); + r.strs.push_back ("aaa"); + + b.nums.push_back (21); + b.nums.push_back (22); + b.nums.push_back (23); + b.strs.push_back ("b"); + b.strs.push_back ("bb"); + b.strs.push_back ("bbb"); + + d.nums.push_back (31); + d.nums.push_back (32); + d.nums.push_back (33); + d.strs.push_back ("d"); + d.strs.push_back ("dd"); + d.strs.push_back ("ddd"); + + { + transaction t (db->begin ()); + db->persist (r); + db->persist (b); + db->persist (d); + t.commit (); + } + + // Update. + // + { + transaction t (db->begin ()); + + // Root. + // + { + auto_ptr p (db->load (r.id)); + + r.num++; + r.strs.push_back ("aaaa"); + db->update (r); + + p->num--; + p->strs.pop_back (); + try + { + db->update (p); + assert (false); + } + catch (const odb::object_changed&) {} + + // Make sure the object is intact. + // + db->reload (p); + assert (r == *p); + } + + // Base. + // + { + auto_ptr p (db->load (b.id)); + + b.num++; + b.str += "b"; + b.strs.push_back ("bbbb"); + b.nums.push_back (24); + db->update (b); + + p->num--; + p->str += "B"; + p->strs.pop_back (); + p->nums.pop_back (); + try + { + db->update (p); + assert (false); + } + catch (const odb::object_changed&) {} + + // Make sure the object is intact. + // + db->reload (p); + assert (b == *p); + } + + // Derived. + // + { + auto_ptr p (db->load (d.id)); // Via root. + + d.num++; + d.str += "d"; + d.strs.push_back ("dddd"); + d.nums.push_back (24); + d.dnum++; + d.dstr += "d"; + db->update (d); + + derived& d1 (static_cast (*p)); + d1.num--; + d1.str += "D"; + d1.strs.pop_back (); + d1.nums.pop_back (); + d1.dnum--; + d1.dstr += "D"; + try + { + db->update (p); + assert (false); + } + catch (const odb::object_changed&) {} + + // Make sure the object is intact. + // + db->reload (p); + assert (d == *p); + } + + t.commit (); + } + + // Reload. + // + { + transaction t (db->begin ()); + + // Make sure reload doesn't modify the object if the versions + // match. + // + derived d1 (d); + d1.num++; + d1.str += "d"; + d1.strs.push_back ("dddd"); + d1.nums.push_back (24); + d1.dnum++; + d1.dstr += "d"; + derived d2 (d1); + + db->reload (d1); + assert (d1 == d2); + + t.commit (); + } + + // Erase. + // + { + transaction t (db->begin ()); + + // Root. + // + { + auto_ptr p (db->load (r.id)); + + r.num++; + r.strs.push_back ("aaaaa"); + db->update (r); + + try + { + db->erase (p); + assert (false); + } + catch (const odb::object_changed&) {} + + db->reload (p); + db->erase (p); + } + + // Base. + // + { + auto_ptr p (db->load (b.id)); + + b.num++; + b.str += "b"; + b.strs.push_back ("bbbb"); + b.nums.push_back (24); + db->update (b); + + try + { + db->erase (p); + assert (false); + } + catch (const odb::object_changed&) {} + + db->reload (p); + db->erase (p); + } + + // Derived. + // + { + auto_ptr p (db->load (d.id)); // Via root. + + d.num++; + d.str += "d"; + d.strs.push_back ("dddd"); + d.nums.push_back (24); + d.dnum++; + d.dstr += "d"; + db->update (d); + + try + { + db->erase (p); + assert (false); + } + catch (const odb::object_changed&) {} + + db->reload (p); + db->erase (p); + } + + // Try to update non-existent object. + // + { + try + { + db->update (d); + assert (false); + } + catch (const odb::object_changed&) {} + } + + // Try to erase non-existent object. + // + { + try + { + db->erase (d); + assert (false); + } + catch (const odb::object_changed&) {} + } + + t.commit (); + } + } + + // Test 6: polymorphism and callbacks. + // + { + using namespace test6; + + base b (1, 1, "bbb"); + auto_ptr d (new derived (2, 2, "ddd")); + + // Persist. + // + { + transaction t (db->begin ()); + db->persist (b); + db->persist (d); + t.commit (); + } + + // Load. + // + { + transaction t (db->begin ()); + + auto_ptr pb (db->load (b.id)); + auto_ptr pd (db->load (d->id)); + + db->load (b.id, *pb); + db->load (d->id, *pd); + + db->reload (*pb); + db->reload (*pd); + + t.commit (); + } + + // Update. + // + { + b.num++; + d->num++; + + transaction t (db->begin ()); + db->update (b); + db->update (d); + t.commit (); + } + + // Query. + // + { + typedef odb::query query; + typedef odb::result result; + + transaction t (db->begin ()); + + result r (db->query ("ORDER BY" + query::id)); + for (result::iterator i (r.begin ()); i != r.end (); ++i) + *i; + + t.commit (); + } + + // Erase. + // + { + transaction t (db->begin ()); + db->erase (b); + db->erase (d); + t.commit (); + } + + // Recursive (delayed) loading. + // + { + derived d (3, 3, "dddd"); + d.ptr.reset (new derived (4, 4, "ddddd")); + + { + transaction t (db->begin ()); + db->persist (d); + db->persist (d.ptr); + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr p (db->load (d.id)); + t.commit (); + } + } + } + + // Test 7: polymorphism and object cache (session). + // +#if defined(HAVE_CXX11) || defined(HAVE_TR1_MEMORY) + { + using namespace test7; + + shared_ptr r (new root (1, 1)); + shared_ptr b (new base (2, 2, "b")); + shared_ptr d (new derived (3, 3, "d")); + + // Persist. + // + { + session s; + + { + transaction t (db->begin ()); + db->persist (r); + db->persist (b); + db->persist (d); + t.commit (); + } + + assert (db->load (r->id) == r); + assert (db->load (b->id) == b); + assert (db->load (d->id) == d); + } + + // Load. + // + { + session s; + + transaction t (db->begin ()); + shared_ptr r1 (db->load (r->id)); + shared_ptr b1 (db->load (b->id)); + shared_ptr d1 (db->load (d->id)); + t.commit (); + + assert (db->load (r->id) == r1); + assert (db->load (b->id) == b1); + assert (db->load (d->id) == d1); + + assert (!db->find (b->id)); + } + + // Query. + // + { + typedef odb::query query; + typedef odb::result result; + + session s; + + transaction t (db->begin ()); + shared_ptr r1 (db->load (r->id)); + shared_ptr b1 (db->load (b->id)); + shared_ptr d1 (db->load (d->id)); + t.commit (); + + { + transaction t (db->begin ()); + + result r (db->query ("ORDER BY" + query::id)); + result::iterator i (r.begin ()), e (r.end ()); + + assert (i != e && i.load () == r1); + assert (++i != e && i.load () == b1); + assert (++i != e && i.load () == d1); + assert (++i == e); + + t.commit (); + } + } + + // Erase. + // + { + session s; + + { + transaction t (db->begin ()); + db->load (r->id); + db->load (b->id); + db->load (d->id); + t.commit (); + } + + { + transaction t (db->begin ()); + db->erase (r); + db->erase (b); + db->erase (d); + t.commit (); + } + + { + transaction t (db->begin ()); + assert (!db->find (r->id)); + assert (!db->find (b->id)); + assert (!db->find (d->id)); + t.commit (); + } + } + } +#endif + + // Test 8: polymorphism and abstract bases. + // + { + using namespace test8; + + base b (1, 1, "b"); + interm i (2, 2, "i", true); + derived1 d1 (3, 3, "d1", true); + derived2 d2 (4, 4, "d2", false); + + // Persist. + // + { + transaction t (db->begin ()); + db->persist (b); + db->persist (static_cast (d1)); + db->persist (static_cast (d2)); + + try + { + db->persist (i); + assert (false); + } + catch (const odb::abstract_class&) {} + + try + { + db->persist (static_cast (i)); + assert (false); + } + catch (const odb::no_type_info&) {} + + t.commit (); + } + + // Load. + // + { + base vb; + interm vi; + derived1 vd1; + derived2 vd2; + + transaction t (db->begin ()); + + // load (id) + // + auto_ptr pb (db->load (b.id)); + auto_ptr pd1 (db->load (d1.id)); + auto_ptr pd2 (db->load (d2.id)); + + assert (*pb == b); + assert (*pd1 == d1); + assert (*pd2 == d2); + + // load (id, obj) + // + db->load (b.id, static_cast (vb)); + db->load (d1.id, static_cast (vd1)); + db->load (d2.id, static_cast (vd2)); + + assert (vb == b); + assert (vd1 == d1); + assert (vd2 == d2); + + try + { + db->load (i.id, static_cast (vi)); + assert (false); + } + catch (const odb::no_type_info&) {} + + // reload (obj) + // + vb.num = 0; + vd1.num = 0; + vd2.num = 0; + + db->reload (static_cast (vb)); + db->reload (static_cast (vd1)); + db->reload (static_cast (vd2)); + + assert (vb == b); + assert (vd1 == d1); + assert (vd2 == d2); + + try + { + db->reload (static_cast (vi)); + assert (false); + } + catch (const odb::no_type_info&) {} + + t.commit (); + } + + // Update. + // + { + b.num++; + b.str += 'b'; + d1.num++; + d1.str += "d1"; + d2.num++; + d2.str += "d1"; + + { + transaction t (db->begin ()); + db->update (static_cast (b)); + db->update (d1); + db->update (static_cast (d2)); + + try + { + db->update (i); + assert (false); + } + catch (const odb::abstract_class&) {} + + try + { + db->update (static_cast (i)); + assert (false); + } + catch (const odb::no_type_info&) {} + + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr pb (db->load (b.id)); + auto_ptr pd1 (db->load (d1.id)); + auto_ptr pd2 (db->load (d2.id)); + t.commit (); + + assert (*pb == b); + assert (*pd1 == d1); + assert (*pd2 == d2); + } + } + + // Erase. + // + { + transaction t (db->begin ()); + db->erase (b); + db->erase (d1.id); + db->erase (static_cast (d2)); + + try + { + db->erase (i); + assert (false); + } + catch (const odb::abstract_class&) {} + + try + { + db->erase (static_cast (i)); + assert (false); + } + catch (const odb::no_type_info&) {} + + t.commit (); + } + } + + // Test 9: polymorphism and readonly classes. + // + { + using namespace test9; + + ro_root ro_r (1, 1); + rw_base rw_b (2, 2, "b"); + ro_derived ro_d (3, 3, "d"); + + rw_root rw_r (1, 1); + ro_base ro_b (2, 2, "b"); + rw_derived rw_d (3, 3, "d"); + + // Persist. + // + { + transaction t (db->begin ()); + db->persist (ro_r); + db->persist (rw_b); + db->persist (ro_d); + + db->persist (rw_r); + db->persist (ro_b); + db->persist (rw_d); + t.commit (); + } + + // Update. + // + { + ro_root ro_r1 (ro_r); + rw_base rw_b1 (rw_b); + ro_derived ro_d1 (ro_d); + + ro_base ro_b1 (ro_b); + rw_derived rw_d1 (rw_d); + + ro_r1.num++; + ro_r1.strs.push_back ("b"); + + rw_b1.num++; + rw_b1.strs.push_back ("b"); + rw_b1.str += "b"; + rw_b1.nums.push_back (2); + rw_b.str += "b"; + rw_b.nums.push_back (2); + + ro_d1.num++; + ro_d1.strs.push_back ("d"); + ro_d1.str += "d"; + ro_d1.nums.push_back (3); + ro_d1.dnum++; + ro_d1.dstr += "d"; + + rw_r.num++; + rw_r.strs.push_back ("b"); + + ro_b1.num++; + ro_b1.strs.push_back ("b"); + ro_b1.str += "b"; + ro_b1.nums.push_back (2); + + rw_d1.num++; + rw_d1.strs.push_back ("d"); + rw_d1.str += "d"; + rw_d1.nums.push_back (3); + rw_d1.dnum++; + rw_d1.dstr += "d"; + rw_d.dnum++; + rw_d.dstr += "d"; + + { + // These should be no-ops. + // + db->update (ro_r1); + db->update (static_cast (ro_d1)); + db->update (ro_b1); + + transaction t (db->begin ()); + db->update (static_cast (rw_b1)); + db->update (rw_r); + db->update (static_cast (rw_d1)); + t.commit (); + } + } + + // Load. + // + { + transaction t (db->begin ()); + auto_ptr p_ro_r (db->load (ro_r.id)); + auto_ptr p_rw_b (db->load (rw_b.id)); + auto_ptr p_ro_d (db->load (ro_d.id)); + + auto_ptr p_rw_r (db->load (rw_r.id)); + auto_ptr p_ro_b (db->load (ro_b.id)); + auto_ptr p_rw_d (db->load (rw_d.id)); + t.commit (); + + assert (*p_ro_r == ro_r); + assert (*p_rw_b == rw_b); + assert (*p_ro_d == ro_d); + + assert (*p_rw_r == rw_r); + assert (*p_ro_b == ro_b); + assert (*p_rw_d == rw_d); + } + } + + // Test 10: empty polymorphic classes. + // + { + using namespace test10; + + base b (1, 1); + derived d (2, 2); + + // Persist. + // + { + transaction t (db->begin ()); + db->persist (b); + db->persist (static_cast (d)); + t.commit (); + } + + // Load. + // + { + transaction t (db->begin ()); + auto_ptr pb (db->load (b.id)); + auto_ptr pd (db->load (d.id)); + t.commit (); + + assert (*pb == b); + assert (*pd == d); + } + + // Update. + // + { + b.num++; + d.num++; + + transaction t (db->begin ()); + db->update (static_cast (b)); + db->update (d); + t.commit (); + } + + // Load. + // + { + transaction t (db->begin ()); + auto_ptr pb (db->load (b.id)); + auto_ptr pd (db->load (d.id)); + t.commit (); + + assert (*pb == b); + assert (*pd == d); + } + } + + // Test 11: reuse and polymorphic inheritance. + // + { + using namespace test11; + + base b (1, 1, "b"); + derived d (2, 2, "d"); + + b.strs.push_back ("b"); + b.nums.push_back (1); + + d.strs.push_back ("d"); + d.nums.push_back (1); + + // Persist. + // + { + transaction t (db->begin ()); + db->persist (b); + db->persist (static_cast (d)); + t.commit (); + } + + // Load. + // + { + transaction t (db->begin ()); + auto_ptr pb (db->load (b.id)); + auto_ptr pd (db->load (d.id)); + t.commit (); + + assert (*pb == b); + assert (*pd == d); + } + + // Update. + // + { + b.num++; + b.str += "b"; + b.strs.push_back ("bb"); + b.nums.push_back (2); + + d.num++; + d.str += "d"; + d.strs.push_back ("dd"); + d.nums.push_back (2); + d.dnum++; + d.dstr += "d"; + + transaction t (db->begin ()); + db->update (b); + db->update (d); + t.commit (); + } + + // Load. + // + { + transaction t (db->begin ()); + auto_ptr pb (db->load (b.id)); + auto_ptr pd (db->load (d.id)); + t.commit (); + + assert (*pb == b); + assert (*pd == d); + } + + // Query. + // + { + typedef odb::query base_query; + typedef odb::result base_result; + + typedef odb::query derived_query; + typedef odb::result derived_result; + + transaction t (db->begin ()); + + { + base_result qr (db->query (base_query::num == 2)); + base_result::iterator i (qr.begin ()), e (qr.end ()); + + assert (i != e && *i == b); + assert (++i == e); + } + + { + derived_result qr (db->query (derived_query::num == 3)); + derived_result::iterator i (qr.begin ()), e (qr.end ()); + + assert (i != e && *i == d); + assert (++i == e); + } + + t.commit (); + } + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/common/polymorphism/makefile b/common/polymorphism/makefile new file mode 100644 index 0000000..9b4385e --- /dev/null +++ b/common/polymorphism/makefile @@ -0,0 +1,113 @@ +# file : common/polymorphism/makefile +# copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +# license : GNU GPL v2; see accompanying LICENSE file + +include $(dir $(lastword $(MAKEFILE_LIST)))../../build/bootstrap.make + +cxx_tun := driver.cxx +odb_hdr := test.hxx +cxx_obj := $(addprefix $(out_base)/,$(cxx_tun:.cxx=.o) $(odb_hdr:.hxx=-odb.o)) +cxx_od := $(cxx_obj:.o=.o.d) + +common.l := $(out_root)/libcommon/common/common.l +common.l.cpp-options := $(out_root)/libcommon/common/common.l.cpp-options + +driver := $(out_base)/driver +dist := $(out_base)/.dist +test := $(out_base)/.test +clean := $(out_base)/.clean + +# Import. +# +$(call import,\ + $(scf_root)/import/odb/stub.make,\ + odb: odb,odb-rules: odb_rules) + +# Build. +# +$(driver): $(cxx_obj) $(common.l) +$(cxx_obj) $(cxx_od): cpp_options := -I$(out_base) -I$(src_base) +$(cxx_obj) $(cxx_od): $(common.l.cpp-options) + +genf := $(addprefix $(odb_hdr:.hxx=-odb),.hxx .ixx .cxx) $(odb_hdr:.hxx=.sql) +gen := $(addprefix $(out_base)/,$(genf)) + +$(gen): $(odb) +$(gen): odb := $(odb) +$(gen) $(dist): export odb_options += --database $(db_id) --generate-schema \ +--generate-query --table-prefix poly_ +$(gen): cpp_options := -I$(src_base) +$(gen): $(common.l.cpp-options) + +$(call include-dep,$(cxx_od),$(cxx_obj),$(gen)) + +# Alias for default target. +# +$(out_base)/: $(driver) + +# Dist +# +name := $(subst /,-,$(subst $(src_root)/common/,,$(src_base))) + +$(dist): db_id := @database@ +$(dist): sources := $(cxx_tun) +$(dist): headers := $(odb_hdr) +$(dist): data_dist := test.std +$(dist): export name := $(name) +$(dist): export extra_dist := $(data_dist) $(call vc9projs,$(name)) \ +$(call vc10projs,$(name)) +$(dist): + $(call dist-data,$(sources) $(headers) $(data_dist)) + $(call meta-automake,../template/Makefile.am) + $(call meta-vc9projs,../template/template,$(name)) + $(call meta-vc10projs,../template/template,$(name)) + +# Test. +# +$(test): $(driver) $(src_base)/test.std + $(call schema) + $(call message,test $<,$< --options-file $(dcf_root)/db.options \ +>$(out_base)/test.out) + $(call message,,diff -u $(src_base)/test.std $(out_base)/test.out) + $(call message,,rm -f $(out_base)/test.out) + +# Clean. +# +$(clean): \ + $(driver).o.clean \ + $(addsuffix .cxx.clean,$(cxx_obj)) \ + $(addsuffix .cxx.clean,$(cxx_od)) \ + $(addprefix $(out_base)/,$(odb_hdr:.hxx=-odb.cxx.hxx.clean)) + $(call message,,rm -f $(out_base)/test.out) + +# Generated .gitignore. +# +ifeq ($(out_base),$(src_base)) +$(driver): | $(out_base)/.gitignore + +$(out_base)/.gitignore: files := driver $(genf) +$(clean): $(out_base)/.gitignore.clean + +$(call include,$(bld_root)/git/gitignore.make) +endif + +# How to. +# +$(call include,$(bld_root)/dist.make) +$(call include,$(bld_root)/meta/vc9proj.make) +$(call include,$(bld_root)/meta/vc10proj.make) +$(call include,$(bld_root)/meta/automake.make) + +$(call include,$(bld_root)/cxx/standard.make) # cxx_standard +ifdef cxx_standard +$(gen): odb_options += --std $(cxx_standard) +$(call include,$(odb_rules)) +endif + +$(call include,$(bld_root)/cxx/cxx-d.make) +$(call include,$(bld_root)/cxx/cxx-o.make) +$(call include,$(bld_root)/cxx/o-e.make) + +# Dependencies. +# +$(call import,$(src_root)/libcommon/makefile) diff --git a/common/polymorphism/test.hxx b/common/polymorphism/test.hxx new file mode 100644 index 0000000..5e72b77 --- /dev/null +++ b/common/polymorphism/test.hxx @@ -0,0 +1,987 @@ +// file : common/polymorphism/test.hxx +// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include // HAVE_CXX11, HAVE_TR1_MEMORY + +#include +#include +#include + +#if !defined(HAVE_CXX11) && defined(HAVE_TR1_MEMORY) +# include +#endif + +#include +#include +#include + +// Test basic polymorphism functionality. +// +#pragma db namespace table("t1_") +namespace test1 +{ + #pragma db object polymorphic + struct root + { + virtual ~root () {} + root () {} + root (unsigned long i, unsigned long n): id (i), num (n) {} + + #pragma db id + unsigned long id; + + unsigned long num; + std::vector strs; + + virtual bool + compare (const root& r, bool tc = true) const + { + if (tc && typeid (r) != typeid (root)) + return false; + + return id == r.id && num == r.num && strs == r.strs; + } + }; + + inline bool + operator== (const root& x, const root& y) {return x.compare (y);} + + #pragma db object + struct base: root + { + base () {} + base (unsigned long i, unsigned long n, const std::string& s) + : root (i, n), str (s) {} + + std::string str; + std::vector nums; + + virtual bool + compare (const root& r, bool tc = true) const + { + if (tc && typeid (r) != typeid (base)) + return false; + + const base& b (static_cast (r)); + return root::compare (r, false) && str == b.str && nums == b.nums; + } + }; + + #pragma db object + struct derived: base + { + derived () {} + derived (unsigned long i, unsigned long n, const std::string& s) + : base (i, n, s), dnum (n + 1), dstr (s + 'd') {} + + unsigned long dnum; + std::string dstr; + + virtual bool + compare (const root& r, bool tc = true) const + { + if (tc && typeid (r) != typeid (derived)) + return false; + + const derived& d (static_cast (r)); + return base::compare (r, false) && dnum == d.dnum && dstr == d.dstr; + } + }; + + // Views. + // + #pragma db view object(root) + struct root_view + { + //#pragma db column(root::typeid_) + std::string typeid_; // @@ tmp + + unsigned long num; + }; + + #pragma db view object(base = b) + struct base_view + { + unsigned long id; + unsigned long num; + std::string str; + }; + + #pragma db view object(derived) + struct derived_view + { + unsigned long num; + std::string str; + unsigned long dnum; + std::string dstr; + }; +} + +// Test inverse object pointers in polymorhic bases. +// +#pragma db namespace table("t2_") +namespace test2 +{ + struct root; + + #pragma db object + struct root_pointer + { + root_pointer (root* r = 0): p (r) {} + + #pragma db id auto + unsigned long id; + + root* p; + }; + + #pragma db object polymorphic + struct root + { + virtual ~root () {} + root () {} + root (unsigned long i): id (i) {} + + #pragma db id + unsigned long id; + + #pragma db inverse(p) + odb::lazy_ptr rp; + }; + + struct base; + + #pragma db object + struct base_pointer + { + base_pointer (base* b = 0) {if (b != 0) vp.push_back (b);} + + #pragma db id auto + unsigned long id; + + std::vector vp; + }; + + #pragma db object + struct base: root + { + base () {} + base (unsigned long i, const std::string& s): root (i), str (s) {} + + std::string str; + + #pragma db inverse(vp) + odb::lazy_ptr bp; + }; + + #pragma db object + struct derived: base + { + derived () {} + derived (unsigned long i, const std::string& s, unsigned long n) + : base (i, s), num (n) {} + + unsigned long num; + }; + + // Views. + // + #pragma db view object(root_pointer = rp) object(root) + struct root_view + { + #pragma db column(rp::id) + unsigned long rp_id; + + #pragma db column(root::id) + unsigned long r_id; + }; + + #pragma db view object(base_pointer) object(base = b) + struct base_view + { + #pragma db column(base_pointer::id) + unsigned long bp_id; + + #pragma db column(b::id) + unsigned long b_id; + + std::string str; + }; +} + +// Test delayed loading. +// +#pragma db namespace table("t3_") +namespace test3 +{ + #pragma db object polymorphic + struct root + { + virtual ~root () {} + root () {} + root (unsigned long i): id (i) {} + + #pragma db id + unsigned long id; + + virtual bool + compare (const root& r, bool tc = true) const + { + if (tc && typeid (r) != typeid (root)) + return false; + + return id == r.id; + } + }; + + inline bool + operator== (const root& x, const root& y) {return x.compare (y);} + + #pragma db object + struct base: root + { + virtual ~base () {delete rptr;} + base (): rptr (0) {} + base (unsigned long i, unsigned long n): root (i), num (n), rptr (0) {} + + unsigned long num; + root* rptr; + + virtual bool + compare (const root& r, bool tc = true) const + { + if (tc && typeid (r) != typeid (base)) + return false; + + const base& b (static_cast (r)); + return + root::compare (r, false) && + num == b.num && + ((rptr == 0 && b.rptr == 0) || rptr->compare (*b.rptr)); + } + }; + + #pragma db object + struct derived: base + { + virtual ~derived () {delete bptr;} + derived (): bptr (0) {} + derived (unsigned long i, unsigned long n, const std::string& s) + : base (i, n), str (s), bptr (0) {} + + std::string str; + base* bptr; + + virtual bool + compare (const root& r, bool tc = true) const + { + if (tc && typeid (r) != typeid (derived)) + return false; + + const derived& d (static_cast (r)); + return + base::compare (r, false) && + str == d.str && + ((bptr == 0 && d.bptr == 0) || bptr->compare (*d.bptr)); + } + }; + + // Views. + // + #pragma db view object(base) object(root = r) + struct base_view + { + #pragma db column(base::id) + unsigned long b_id; + + #pragma db column(r::id) + unsigned long r_id; + + unsigned long num; + }; + + #pragma db view \ + object(derived = d) \ + object(base = b) \ + object(root = r: d::rptr) + struct derived_view + { + #pragma db column(d::id) + unsigned long d_id; + + #pragma db column(b::id) + unsigned long b_id; + + #pragma db column(r::id) + unsigned long r_id; + + #pragma db column(d::num) + unsigned long d_num; + + #pragma db column(b::num) + unsigned long b_num; + + std::string str; + }; + + // This is an example of a pathological case, where the right-hand-side + // of the join condition comes from one of the bases. As a result, we + // join the base table first, which means we will get both bases and + // derived objects instead of just derived. + // + //#pragma db view object(root = r) object(derived = d) + #pragma db view object(derived = d) object(root = r) + struct root_view + { + #pragma db column(r::id) + unsigned long r_id; + + #pragma db column(d::id) + unsigned long d_id; + + std::string str; + }; +} + +// Test views. +// +#pragma db namespace table("t4_") +namespace test4 +{ + #pragma db object polymorphic + struct root1 + { + virtual ~root1 () {} + root1 () {} + root1 (unsigned long i): id (i) {} + + #pragma db id + unsigned long id; + }; + + #pragma db object + struct base1: root1 + { + base1 () {} + base1 (unsigned long i, unsigned long n): root1 (i), num (n) {} + + unsigned long num; + }; + + #pragma db object polymorphic + struct root2 + { + virtual ~root2 () {} + root2 () {} + root2 (unsigned long i, unsigned long n): id (i), num (n) {} + + #pragma db id + unsigned long id; + + unsigned long num; + }; + + #pragma db object + struct base2: root2 + { + base2 () {} + base2 (unsigned long i, unsigned long n, const std::string& s) + : root2 (i, n), str (s) {} + + std::string str; + }; + + // Test custom join condition. + // + #pragma db view object(base2) object(base1: base2::num == base1::num) + struct view1 + { + std::string str; + }; + + #pragma db view object(base2) + struct view2 + { + #pragma db column("min(" + base2::num + ")") + std::size_t min_num; + }; +} + +// Test polymorphism and optimistic concurrency. +// +#pragma db namespace table("t5_") +namespace test5 +{ + #pragma db object polymorphic optimistic pointer(std::auto_ptr) + struct root + { + virtual ~root () {} + root () {} + root (unsigned long i, unsigned long n): id (i), num (n) {} + + #pragma db id + unsigned long id; + + #pragma db version + unsigned long version; + + unsigned long num; + std::vector strs; + + virtual bool + compare (const root& r, bool tc = true) const + { + if (tc && typeid (r) != typeid (root)) + return false; + + return id == r.id && version == r.version && + num == r.num && strs == r.strs; + } + }; + + inline bool + operator== (const root& x, const root& y) {return x.compare (y);} + + #pragma db object + struct base: root + { + base () {} + base (unsigned long i, unsigned long n, const std::string& s) + : root (i, n), str (s) {} + + std::string str; + std::vector nums; + + virtual bool + compare (const root& r, bool tc = true) const + { + if (tc && typeid (r) != typeid (base)) + return false; + + const base& b (static_cast (r)); + return root::compare (r, false) && str == b.str && nums == b.nums; + } + }; + + #pragma db object + struct derived: base + { + derived () {} + derived (unsigned long i, unsigned long n, const std::string& s) + : base (i, n, s), dnum (n + 1), dstr (s + 'd') {} + + unsigned long dnum; + std::string dstr; + + virtual bool + compare (const root& r, bool tc = true) const + { + if (tc && typeid (r) != typeid (derived)) + return false; + + const derived& d (static_cast (r)); + return base::compare (r, false) && dnum == d.dnum && dstr == d.dstr; + } + }; +} + +// Test polymorphism and callbacks. +// +#pragma db namespace table("t6_") +namespace test6 +{ + #pragma db object polymorphic pointer(std::auto_ptr) + struct root + { + virtual ~root () {} + root (): id (0) {} + root (unsigned long i, unsigned long n): id (i), num (n) {} + + #pragma db id + unsigned long id; + + unsigned long num; + }; + + #pragma db object callback(db_callback) + struct base: root + { + base () {} + base (unsigned long i, unsigned long n, const std::string& s) + : root (i, n), str (s) {} + + std::string str; + + void + db_callback (odb::callback_event, odb::database&); + + void + db_callback (odb::callback_event, odb::database&) const; + }; + + #pragma db object callback(db_callback) + struct derived: base + { + derived () {} + derived (unsigned long i, unsigned long n, const std::string& s) + : base (i, n, s), dnum (n + 1), dstr (s + 'd') {} + + unsigned long dnum; + std::string dstr; + + std::auto_ptr ptr; + + void + db_callback (odb::callback_event, odb::database&) const; + }; +} + +// Test polymorphism and object cache (session). +// +#if defined(HAVE_CXX11) || defined(HAVE_TR1_MEMORY) +#pragma db namespace table("t7_") +namespace test7 +{ +#ifdef HAVE_CXX11 + using std::shared_ptr; +#else + using std::tr1::shared_ptr; +#endif + + #pragma db object polymorphic pointer(shared_ptr) + struct root + { + virtual ~root () {} + root (): id (0) {} + root (unsigned long i, unsigned long n): id (i), num (n) {} + + #pragma db id + unsigned long id; + + unsigned long num; + }; + + #pragma db object + struct base: root + { + base () {} + base (unsigned long i, unsigned long n, const std::string& s) + : root (i, n), str (s) {} + + std::string str; + }; + + #pragma db object + struct derived: base + { + derived () {} + derived (unsigned long i, unsigned long n, const std::string& s) + : base (i, n, s), dnum (n + 1), dstr (s + 'd') {} + + unsigned long dnum; + std::string dstr; + }; +} +#endif + +// Test polymorphism and abstract bases. +// +#pragma db namespace table("t8_") +namespace test8 +{ + #pragma db object polymorphic + struct root + { + virtual ~root () = 0; // Auto-abstract. + root () {} + root (unsigned long i, unsigned long n): id (i), num (n) {} + + #pragma db id + unsigned long id; + + unsigned long num; + std::vector strs; + + virtual bool + compare (const root& r, bool tc = true) const + { + if (tc && typeid (r) != typeid (root)) + return false; + + return id == r.id && num == r.num && strs == r.strs; + } + }; + + inline root:: + ~root () {} + + inline bool + operator== (const root& x, const root& y) {return x.compare (y);} + + #pragma db object + struct base: root + { + base () {} + base (unsigned long i, unsigned long n, const std::string& s) + : root (i, n), str (s) {} + + std::string str; + std::vector nums; + + virtual bool + compare (const root& r, bool tc = true) const + { + if (tc && typeid (r) != typeid (base)) + return false; + + const base& b (static_cast (r)); + return root::compare (r, false) && str == b.str && nums == b.nums; + } + }; + + #pragma db object abstract + struct interm: base + { + interm () {} + interm (unsigned long i, unsigned long n, const std::string& s, bool b) + : base (i, n, s), bln (b) {} + + bool bln; + + virtual bool + compare (const root& r, bool tc = true) const + { + if (tc && typeid (r) != typeid (interm)) + return false; + + const interm& i (static_cast (r)); + return base::compare (r, false) && bln == i.bln; + } + }; + + #pragma db object + struct derived1: interm + { + derived1 () {} + derived1 (unsigned long i, unsigned long n, const std::string& s, bool b) + : interm (i, n, s, b), dnum (n + 1) {} + + unsigned long dnum; + + virtual bool + compare (const root& r, bool tc = true) const + { + if (tc && typeid (r) != typeid (derived1)) + return false; + + const derived1& d (static_cast (r)); + return interm::compare (r, false) && dnum == d.dnum; + } + }; + + #pragma db object + struct derived2: interm + { + derived2 () {} + derived2 (unsigned long i, unsigned long n, const std::string& s, bool b) + : interm (i, n, s, b), dstr (s + 'd') {} + + std::string dstr; + + virtual bool + compare (const root& r, bool tc = true) const + { + if (tc && typeid (r) != typeid (derived2)) + return false; + + const derived2& d (static_cast (r)); + return interm::compare (r, false) && dstr == d.dstr; + } + }; +} + +// Test polymorphism and readonly classes. +// +#pragma db namespace table("t9_") +namespace test9 +{ + // + // ro_root, rw_base, ro_derived + // + #pragma db object polymorphic readonly + struct ro_root + { + virtual ~ro_root () {} + ro_root () {} + ro_root (unsigned long i, unsigned long n): id (i), num (n) {} + + #pragma db id + unsigned long id; + + unsigned long num; + std::vector strs; + + virtual bool + compare (const ro_root& r, bool tc = true) const + { + if (tc && typeid (r) != typeid (ro_root)) + return false; + + return id == r.id && num == r.num && strs == r.strs; + } + }; + + inline bool + operator== (const ro_root& x, const ro_root& y) {return x.compare (y);} + + #pragma db object + struct rw_base: ro_root + { + rw_base () {} + rw_base (unsigned long i, unsigned long n, const std::string& s) + : ro_root (i, n), str (s) {} + + std::string str; + std::vector nums; + + virtual bool + compare (const ro_root& r, bool tc = true) const + { + if (tc && typeid (r) != typeid (rw_base)) + return false; + + const rw_base& b (static_cast (r)); + return ro_root::compare (r, false) && str == b.str && nums == b.nums; + } + }; + + #pragma db object readonly + struct ro_derived: rw_base + { + ro_derived () {} + ro_derived (unsigned long i, unsigned long n, const std::string& s) + : rw_base (i, n, s), dnum (n + 1), dstr (s + 'd') {} + + unsigned long dnum; + std::string dstr; + + virtual bool + compare (const ro_root& r, bool tc = true) const + { + if (tc && typeid (r) != typeid (ro_derived)) + return false; + + const ro_derived& d (static_cast (r)); + return rw_base::compare (r, false) && dnum == d.dnum && dstr == d.dstr; + } + }; + + // + // rw_root, ro_base, rw_derived + // + #pragma db object polymorphic + struct rw_root + { + virtual ~rw_root () {} + rw_root () {} + rw_root (unsigned long i, unsigned long n): id (i), num (n) {} + + #pragma db id + unsigned long id; + + unsigned long num; + std::vector strs; + + virtual bool + compare (const rw_root& r, bool tc = true) const + { + if (tc && typeid (r) != typeid (rw_root)) + return false; + + return id == r.id && num == r.num && strs == r.strs; + } + }; + + inline bool + operator== (const rw_root& x, const rw_root& y) {return x.compare (y);} + + #pragma db object readonly + struct ro_base: rw_root + { + ro_base () {} + ro_base (unsigned long i, unsigned long n, const std::string& s) + : rw_root (i, n), str (s) {} + + std::string str; + std::vector nums; + + virtual bool + compare (const rw_root& r, bool tc = true) const + { + if (tc && typeid (r) != typeid (ro_base)) + return false; + + const ro_base& b (static_cast (r)); + return rw_root::compare (r, false) && str == b.str && nums == b.nums; + } + }; + + #pragma db object + struct rw_derived: ro_base + { + rw_derived () {} + rw_derived (unsigned long i, unsigned long n, const std::string& s) + : ro_base (i, n, s), dnum (n + 1), dstr (s + 'd') {} + + unsigned long dnum; + std::string dstr; + + virtual bool + compare (const rw_root& r, bool tc = true) const + { + if (tc && typeid (r) != typeid (rw_derived)) + return false; + + const rw_derived& d (static_cast (r)); + return ro_base::compare (r, false) && dnum == d.dnum && dstr == d.dstr; + } + }; +} + +// Test empty polymorphic classes. +// +#pragma db namespace table("t10_") +namespace test10 +{ + #pragma db object polymorphic + struct root + { + virtual ~root () = 0; // Auto-abstract. + root () {} + root (unsigned long i): id (i) {} + + #pragma db id + unsigned long id; + + virtual bool + compare (const root& r, bool tc = true) const + { + if (tc && typeid (r) != typeid (root)) + return false; + + return id == r.id; + } + }; + + inline root:: + ~root () {} + + inline bool + operator== (const root& x, const root& y) {return x.compare (y);} + + #pragma db object + struct base: root + { + base () {} + base (unsigned long i, unsigned long n) : root (i), num (n) {} + + unsigned long num; + + virtual bool + compare (const root& r, bool tc = true) const + { + if (tc && typeid (r) != typeid (base)) + return false; + + const base& b (static_cast (r)); + return root::compare (r, false) && num == b.num; + } + }; + + #pragma db object + struct derived: base + { + derived () {} + derived (unsigned long i, unsigned long n): base (i, n) {} + + virtual bool + compare (const root& r, bool tc = true) const + { + if (tc && typeid (r) != typeid (derived)) + return false; + + return base::compare (r, false); + } + }; +} + +// Test mixing reuse and polymorphic inheritance. +// +#pragma db namespace table("t11_") +namespace test11 +{ + #pragma db object abstract + struct root + { + root () {} + root (unsigned long i, unsigned long n): id (i), num (n) {} + + #pragma db id + unsigned long id; + + unsigned long num; + std::vector strs; + }; + + #pragma db object polymorphic + struct base: root + { + virtual ~base () {} + base () {} + base (unsigned long i, unsigned long n, const std::string& s) + : root (i, n), str (s) {} + + std::string str; + std::vector nums; + + virtual bool + compare (const base& b, bool tc = true) const + { + if (tc && typeid (b) != typeid (base)) + return false; + + return id == b.id && num == b.num && strs == b.strs && + str == b.str && nums == b.nums; + } + }; + + inline bool + operator== (const base& x, const base& y) {return x.compare (y);} + + #pragma db object + struct derived: base + { + derived () {} + derived (unsigned long i, unsigned long n, const std::string& s) + : base (i, n, s), dnum (n + 1), dstr (s + 'd') {} + + unsigned long dnum; + std::string dstr; + + virtual bool + compare (const base& b, bool tc = true) const + { + if (tc && typeid (b) != typeid (derived)) + return false; + + const derived& d (static_cast (b)); + return base::compare (b, false) && dnum == d.dnum && dstr == d.dstr; + } + }; +} + +#endif // TEST_HXX diff --git a/common/polymorphism/test.std b/common/polymorphism/test.std new file mode 100644 index 0000000..5c18191 --- /dev/null +++ b/common/polymorphism/test.std @@ -0,0 +1,36 @@ +base pre_persist 1 const +base post_persist 1 const +derived pre_persist 2 const +derived post_persist 2 const +base pre_load 0 +base post_load 1 +derived pre_load 0 const +derived post_load 2 const +base pre_load 1 +base post_load 1 +derived pre_load 2 const +derived post_load 2 const +base pre_load 1 +base post_load 1 +derived pre_load 2 const +derived post_load 2 const +base pre_update 1 const +base post_update 1 const +derived pre_update 2 const +derived post_update 2 const +base pre_load 0 +base post_load 1 +derived pre_load 0 const +derived post_load 2 const +base pre_erase 1 const +base post_erase 1 const +derived pre_erase 2 const +derived post_erase 2 const +derived pre_persist 3 const +derived post_persist 3 const +derived pre_persist 4 const +derived post_persist 4 const +derived pre_load 0 const +derived pre_load 0 const +derived post_load 4 const +derived post_load 3 const diff --git a/common/query/driver.cxx b/common/query/driver.cxx index c207955..cc92f41 100644 --- a/common/query/driver.cxx +++ b/common/query/driver.cxx @@ -421,7 +421,15 @@ main (int argc, char* argv[]) ++i; assert (i != r.end ()); - auto_ptr joe (db->load (3)); + { + auto_ptr joe (db->load (3)); + } + + { + person p (5, "Peter", "Peterson", 70, false, key3); + db->persist (p); + db->erase (p); + } // SQL Server does not support re-loading of an object with long data // from a query result. @@ -430,8 +438,10 @@ main (int argc, char* argv[]) assert (i->last_name_ == "Doe"); // Actual load. #endif + // Overwrite object image again. + // + auto_ptr joe (db->load (3)); person p; - joe.reset (db->load (3)); // Overwrite object image again. i.load (p); assert (p.last_name_ == "Doe"); diff --git a/common/readonly/driver.cxx b/common/readonly/driver.cxx index c543f7a..b39ab38 100644 --- a/common/readonly/driver.cxx +++ b/common/readonly/driver.cxx @@ -204,15 +204,20 @@ main (int argc, char* argv[]) // Readonly object. // { + typedef odb::object_traits so_traits; typedef odb::object_traits ro_traits; typedef odb::object_traits rw_traits; + assert (so_traits::column_count == + so_traits::id_column_count + so_traits::readonly_column_count); + assert (ro_traits::column_count == ro_traits::id_column_count + ro_traits::readonly_column_count); assert (rw_traits::column_count != rw_traits::id_column_count + rw_traits::readonly_column_count); + simple_object so (1, 1); ro_object ro_o (1, 1); rw_object rw_o (1, 1); @@ -224,6 +229,7 @@ main (int argc, char* argv[]) { transaction t (db->begin ()); + db->persist (so); db->persist (ro_o); db->persist (rw_o); t.commit (); @@ -234,6 +240,7 @@ main (int argc, char* argv[]) { transaction t (db->begin ()); + //db->update (so); // Compile error. //db->update (ro_o); // Compile error. db->update (rw_o); t.commit (); @@ -241,8 +248,9 @@ main (int argc, char* argv[]) { transaction t (db->begin ()); - db->load (1, ro_o); - db->load (1, rw_o); + db->load (1, so); + db->load (1, ro_o); + db->load (1, rw_o); t.commit (); } diff --git a/common/readonly/test.hxx b/common/readonly/test.hxx index 04250fe..a29a2bd 100644 --- a/common/readonly/test.hxx +++ b/common/readonly/test.hxx @@ -132,6 +132,18 @@ struct container // Readonly object. // +#pragma db object readonly +struct simple_object +{ + simple_object (unsigned long i, unsigned long x): id (i), sv (x) {} + simple_object () {} + + #pragma db id + unsigned long id; + + unsigned long sv; +}; + #pragma db object struct object { diff --git a/common/relationship-query/driver.cxx b/common/relationship-query/driver.cxx index 58bbef5..ed96223 100644 --- a/common/relationship-query/driver.cxx +++ b/common/relationship-query/driver.cxx @@ -39,8 +39,8 @@ main (int argc, char* argv[]) shared_ptr us (new country ("US", "United States")); shared_ptr se (new country ("SE", "Sweden")); - shared_ptr st (new employer ("Simple Tech, Inc")); - shared_ptr ct (new employer ("Complex Tech, Inc")); + shared_ptr st (new employer ("Simple Tech, Inc", ca)); + shared_ptr ct (new employer ("Complex Tech, Inc", us)); // person // diff --git a/common/relationship-query/test.hxx b/common/relationship-query/test.hxx index c2e167c..671c849 100644 --- a/common/relationship-query/test.hxx +++ b/common/relationship-query/test.hxx @@ -111,8 +111,8 @@ struct employee: person #pragma db object pointer(shared_ptr) struct employer { - employer (const std::string& n) - : name (n) + employer (const std::string& n, shared_ptr nat) + : name (n), nationality (nat) { } @@ -122,6 +122,11 @@ struct employer #pragma db id std::string name; + + // The same member name and type as in person (test JOIN alias). + // + #pragma db not_null + shared_ptr nationality; }; #pragma db object pointer(shared_ptr) -- cgit v1.1