diff options
Diffstat (limited to 'common')
-rw-r--r-- | common/makefile | 2 | ||||
-rw-r--r-- | common/section/basics/driver.cxx | 1689 | ||||
-rw-r--r-- | common/section/basics/makefile | 115 | ||||
-rw-r--r-- | common/section/basics/test.hxx | 600 | ||||
-rw-r--r-- | common/section/basics/test.std | 0 | ||||
-rw-r--r-- | common/section/polymorphism/driver.cxx | 1763 | ||||
-rw-r--r-- | common/section/polymorphism/makefile | 115 | ||||
-rw-r--r-- | common/section/polymorphism/test.hxx | 495 | ||||
-rw-r--r-- | common/section/polymorphism/test.std | 0 |
9 files changed, 4779 insertions, 0 deletions
diff --git a/common/makefile b/common/makefile index 82743a1..3263410 100644 --- a/common/makefile +++ b/common/makefile @@ -43,6 +43,8 @@ relationship-query \ schema/namespace \ schema/embedded/basics \ schema/embedded/order \ +section/basics \ +section/polymorphism \ session/cache \ template \ transaction/basics \ diff --git a/common/section/basics/driver.cxx b/common/section/basics/driver.cxx new file mode 100644 index 0000000..67e09ef --- /dev/null +++ b/common/section/basics/driver.cxx @@ -0,0 +1,1689 @@ +// file : common/section/basics/driver.cxx +// copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +// Test object section basics. +// + +#include <memory> // std::auto_ptr +#include <cassert> +#include <iostream> + +#include <odb/session.hxx> +#include <odb/database.hxx> +#include <odb/transaction.hxx> + +#include <common/common.hxx> + +#include "test.hxx" +#include "test-odb.hxx" + +using namespace std; +using namespace odb::core; + +struct failed {}; + +int +main (int argc, char* argv[]) +{ + try + { + auto_ptr<database> db (create_database (argc, argv)); + + // Test lazy-loaded, always updating section. + // + { + using namespace test1; + + object o (123, "abc"); + + { + transaction t (db->begin ()); + db->persist (o); + t.commit (); + + assert (o.s.loaded ()); + } + + { + transaction t (db->begin ()); + auto_ptr<object> p (db->load<object> (o.id)); + + assert (!p->s.loaded ()); + assert (p->n == o.n && + p->sn != o.sn && p->ss != o.ss && p->sv != o.sv); + + db->load (*p, p->s); + + assert (p->s.loaded ()); + assert (p->n == o.n && + p->sn == o.sn && p->ss == o.ss && p->sv == o.sv); + + t.commit (); + } + + // Update. + // + o.n++; + o.sn++; + o.ss += 'd'; + o.sv[0]++; + + { + transaction t (db->begin ()); + db->update (o); + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr<object> p (db->load<object> (o.id)); + db->load (*p, p->s); + assert (p->n == o.n && + p->sn == o.sn && p->ss == o.ss && p->sv == o.sv); + t.commit (); + } + + // We can also update just the section. + // + o.n++; + o.sn++; + o.ss += 'd'; + o.sv[0]++; + + { + transaction t (db->begin ()); + db->update (o, o.s); + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr<object> p (db->load<object> (o.id)); + db->load (*p, p->s); + assert (p->n != o.n && + p->sn == o.sn && p->ss == o.ss && p->sv == o.sv); + t.commit (); + } + + // Test updating unloaded section. + // + o.n++; + o.sn++; + o.ss += 'd'; + o.sv[0]++; + o.s.unload (); + + { + transaction t (db->begin ()); + db->update (o); + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr<object> p (db->load<object> (o.id)); + db->load (*p, p->s); + assert (p->n == o.n && + p->sn != o.sn && p->ss != o.ss && p->sv != o.sv); + t.commit (); + } + + // Test reloading of loaded/unloaded sections. + // + o.n++; + o.sn++; + o.ss += 'd'; + o.sv[0]++; + o.s.unload (); + + { + transaction t (db->begin ()); + auto_ptr<object> p (db->load<object> (o.id)); + db->load (*p, p->s); + db->reload (o); + assert (p->n == o.n && + p->sn != o.sn && p->ss != o.ss && p->sv != o.sv); + db->load (o, o.s); + t.commit (); + } + + o.n++; + o.sn++; + o.ss += 'd'; + o.sv[0]++; + + { + transaction t (db->begin ()); + auto_ptr<object> p (db->load<object> (o.id)); + db->load (*p, p->s); + db->reload (o); + assert (p->n == o.n && + p->sn == o.sn && p->ss == o.ss && p->sv == o.sv); + t.commit (); + } + + typedef odb::query<object> query; + typedef odb::result<object> result; + + // Make sure we can access section members in queries. + // + { + transaction t (db->begin ()); + + result r (db->query<object> (query::ss == o.ss)); + result::iterator i (r.begin ()); + + assert (i != r.end () && !i->s.loaded ()); + + db->load (*i, i->s); + assert (i->n == o.n && + i->sn == o.sn && i->ss == o.ss && i->sv == o.sv); + + assert (++i == r.end ()); + + t.commit (); + } + + // Make sure we can load/update sections without messing up the + // loaded object's image. + // + { + transaction t (db->begin ()); + + result r (db->query<object> (query::ss == o.ss)); + result::iterator i (r.begin ()); + + assert (i != r.end ()); + + object o1; + i.load (o1); + db->load (o1, o1.s); + assert (o1.n == o.n && + o1.sn == o.sn && o1.ss == o.ss && o1.sv == o.sv); + + o.sn++; + o.ss += 'd'; + o.sv[0]++; + db->update (o, o.s); + + object o2; + i.load (o2); + db->load (o2, o2.s); + assert (o2.n == o1.n && + o2.sn == o.sn && o2.ss == o.ss && o2.sv == o.sv); + + assert (++i == r.end ()); + + t.commit (); + } + } + + // Test lazy-loaded, change-updated section. + // + { + using namespace test2; + + object o (123, "abc"); + + { + transaction t (db->begin ()); + db->persist (o); + t.commit (); + + assert (o.s.loaded ()); + } + + { + transaction t (db->begin ()); + auto_ptr<object> p (db->load<object> (o.id)); + + assert (!p->s.loaded ()); + assert (p->n == o.n && + p->sn != o.sn && p->ss != o.ss && p->sv != o.sv); + + db->load (*p, p->s); + + assert (p->s.loaded ()); + assert (p->n == o.n && + p->sn == o.sn && p->ss == o.ss && p->sv == o.sv); + + t.commit (); + } + + // Update but don't mark as changed. + // + o.n++; + o.sn++; + o.ss += 'd'; + o.sv[0]++; + + { + transaction t (db->begin ()); + db->update (o); + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr<object> p (db->load<object> (o.id)); + db->load (*p, p->s); + assert (p->n == o.n && + p->sn != o.sn && p->ss != o.ss && p->sv != o.sv); + t.commit (); + } + + // Mark as changed. + // + o.s.change (); + + { + transaction t (db->begin ()); + db->update (o); + assert (!o.s.changed ()); + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr<object> p (db->load<object> (o.id)); + db->load (*p, p->s); + assert (p->n == o.n && + p->sn == o.sn && p->ss == o.ss && p->sv == o.sv); + t.commit (); + } + + // We can also update just the section manually. + // + o.n++; + o.sn++; + o.ss += 'd'; + o.sv[0]++; + o.s.change (); + + { + transaction t (db->begin ()); + db->update (o, o.s); + assert (!o.s.changed ()); + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr<object> p (db->load<object> (o.id)); + db->load (*p, p->s); + assert (p->n != o.n && + p->sn == o.sn && p->ss == o.ss && p->sv == o.sv); + t.commit (); + } + } + + // Test lazy-loaded, manually-updated section. + // + { + using namespace test3; + + object o (123, "abc"); + + { + transaction t (db->begin ()); + db->persist (o); + t.commit (); + + assert (o.s.loaded ()); + } + + { + transaction t (db->begin ()); + auto_ptr<object> p (db->load<object> (o.id)); + + assert (!p->s.loaded ()); + assert (p->n == o.n && + p->sn != o.sn && p->ss != o.ss && p->sv != o.sv); + + db->load (*p, p->s); + + assert (p->s.loaded ()); + assert (p->n == o.n && + p->sn == o.sn && p->ss == o.ss && p->sv == o.sv); + + t.commit (); + } + + // Update the object only. + // + o.n++; + o.sn++; + o.ss += 'd'; + o.sv[0]++; + + { + transaction t (db->begin ()); + db->update (o); + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr<object> p (db->load<object> (o.id)); + db->load (*p, p->s); + assert (p->n == o.n && + p->sn != o.sn && p->ss != o.ss && p->sv != o.sv); + t.commit (); + } + + // Update both the object and section. + // + o.n++; + + { + transaction t (db->begin ()); + db->update (o); + db->update (o, o.s); + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr<object> p (db->load<object> (o.id)); + db->load (*p, p->s); + assert (p->n == o.n && + p->sn == o.sn && p->ss == o.ss && p->sv == o.sv); + t.commit (); + } + + // We can also update just the section. + // + o.n++; + o.sn++; + o.ss += 'd'; + o.sv[0]++; + + { + transaction t (db->begin ()); + db->update (o, o.s); + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr<object> p (db->load<object> (o.id)); + db->load (*p, p->s); + assert (p->n != o.n && + p->sn == o.sn && p->ss == o.ss && p->sv == o.sv); + t.commit (); + } + + // Test detection of unloaded section update. + // + try + { + transaction t (db->begin ()); + auto_ptr<object> p (db->load<object> (o.id)); + db->update (*p, p->s); + assert (false); + } + catch (const section_not_loaded&) + { + } + } + + // Test eager-loaded, change-updated section. + // + { + using namespace test4; + + object o (123, "abc"); + + { + transaction t (db->begin ()); + db->persist (o); + t.commit (); + + assert (o.s.loaded ()); + } + + { + transaction t (db->begin ()); + auto_ptr<object> p (db->load<object> (o.id)); + + assert (p->s.loaded ()); + assert (p->n == o.n && + p->sn == o.sn && p->ss == o.ss && p->sv == o.sv); + + t.commit (); + } + + // Update but don't mark as changed. + // + o.n++; + o.sn++; + o.ss += 'd'; + o.sv[0]++; + + { + transaction t (db->begin ()); + db->update (o); + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr<object> p (db->load<object> (o.id)); + assert (p->n == o.n && + p->sn != o.sn && p->ss != o.ss && p->sv != o.sv); + t.commit (); + } + + // Mark as changed. + // + o.s.change (); + + { + transaction t (db->begin ()); + db->update (o); + assert (!o.s.changed ()); + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr<object> p (db->load<object> (o.id)); + assert (p->n == o.n && + p->sn == o.sn && p->ss == o.ss && p->sv == o.sv); + t.commit (); + } + } + + // Test eager-loaded, manually-updated section. + // + { + using namespace test5; + + object o (123, "abc"); + + { + transaction t (db->begin ()); + db->persist (o); + t.commit (); + + assert (o.s.loaded ()); + } + + { + transaction t (db->begin ()); + auto_ptr<object> p (db->load<object> (o.id)); + + assert (p->s.loaded ()); + assert (p->n == o.n && + p->sn == o.sn && p->ss == o.ss && p->sv == o.sv); + + t.commit (); + } + + // Update the object only. + // + o.n++; + o.sn++; + o.ss += 'd'; + o.sv[0]++; + + { + transaction t (db->begin ()); + db->update (o); + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr<object> p (db->load<object> (o.id)); + assert (p->n == o.n && + p->sn != o.sn && p->ss != o.ss && p->sv != o.sv); + t.commit (); + } + + // Update both the object and section. + // + o.n++; + + { + transaction t (db->begin ()); + db->update (o); + db->update (o, o.s); + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr<object> p (db->load<object> (o.id)); + assert (p->n == o.n && + p->sn == o.sn && p->ss == o.ss && p->sv == o.sv); + t.commit (); + } + + // We can also update just the section. + // + o.n++; + o.sn++; + o.ss += 'd'; + o.sv[0]++; + + { + transaction t (db->begin ()); + db->update (o, o.s); + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr<object> p (db->load<object> (o.id)); + assert (p->n != o.n && + p->sn == o.sn && p->ss == o.ss && p->sv == o.sv); + t.commit (); + } + } + + // Test value-only and container-only section. Also multiple sections + // in an object. + // + { + using namespace test6; + + object o (123, "abc"); + + { + transaction t (db->begin ()); + db->persist (o); + t.commit (); + + assert (o.s1.loaded ()); + assert (o.s2.loaded ()); + } + + { + transaction t (db->begin ()); + auto_ptr<object> p (db->load<object> (o.id)); + + assert (!p->s1.loaded ()); + assert (!p->s2.loaded ()); + assert (p->n == o.n && + p->sn != o.sn && p->ss != o.ss && p->sv != o.sv); + + db->load (*p, p->s1); + assert (p->s1.loaded ()); + assert (p->n == o.n && + p->sn == o.sn && p->ss == o.ss && p->sv != o.sv); + + db->load (*p, p->s2); + assert (p->s2.loaded ()); + assert (p->n == o.n && + p->sn == o.sn && p->ss == o.ss && p->sv == o.sv); + + t.commit (); + } + + // Update. + // + o.n++; + o.sn++; + o.ss += 'd'; + o.sv[0]++; + + { + transaction t (db->begin ()); + db->update (o); + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr<object> p (db->load<object> (o.id)); + db->load (*p, p->s1); + db->load (*p, p->s2); + assert (p->n == o.n && + p->sn == o.sn && p->ss == o.ss && p->sv == o.sv); + t.commit (); + } + + // We can also update just the section. + // + o.n++; + o.sn++; + o.ss += 'd'; + o.sv[0]++; + + { + transaction t (db->begin ()); + db->update (o, o.s1); + db->update (o, o.s2); + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr<object> p (db->load<object> (o.id)); + db->load (*p, p->s1); + db->load (*p, p->s2); + assert (p->n != o.n && + p->sn == o.sn && p->ss == o.ss && p->sv == o.sv); + t.commit (); + } + } + + // Test value-only and container-only section. Also multiple sections + // in an object. + // + { + using namespace test7; + + object o (123, "abc", true); + + { + transaction t (db->begin ()); + db->persist (o); + t.commit (); + + assert (o.s1.loaded ()); + assert (o.s2.loaded ()); + } + + { + transaction t (db->begin ()); + auto_ptr<object> p (db->load<object> (o.id)); + + assert (!p->s1.loaded ()); + assert (!p->s2.loaded ()); + assert (p->sn1 != o.sn1 && p->ss1 != o.ss1 && + p->sn2 != o.sn2 && p->ss2 != o.ss2 && p->sb2 != o.sb2); + + db->load (*p, p->s1); + db->load (*p, p->s2); + assert (p->s1.loaded ()); + assert (p->s2.loaded ()); + assert (p->sn1 == o.sn1 && p->ss1 == o.ss1 && + p->sn2 == o.sn2 && p->ss2 == o.ss2 && p->sb2 == o.sb2); + + + t.commit (); + } + + // Update. + // + o.sn1++; + o.sn2++; + o.ss1 += 'd'; + o.ss2 += 'd'; + o.sb2 = !o.sb2; + + { + transaction t (db->begin ()); + db->update (o); + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr<object> p (db->load<object> (o.id)); + db->load (*p, p->s1); + db->load (*p, p->s2); + assert (p->sn1 == o.sn1 && p->ss1 == o.ss1 && + p->sn2 == o.sn2 && p->ss2 == o.ss2 && p->sb2 == o.sb2); + t.commit (); + } + + // Manual update of just the section. + // + o.sn1++; + o.sn2++; + o.ss1 += 'd'; + o.ss2 += 'd'; + o.sb2 = !o.sb2; + + { + transaction t (db->begin ()); + db->update (o, o.s2); + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr<object> p (db->load<object> (o.id)); + db->load (*p, p->s1); + db->load (*p, p->s2); + assert (p->sn1 != o.sn1 && p->ss1 != o.ss1 && + p->sn2 == o.sn2 && p->ss2 == o.ss2 && p->sb2 == o.sb2); + t.commit (); + } + } + + // Test readonly and inverse section members. + // + { + using namespace test8; + + object1 o1 (new object (123, "abc")); + object& o (*o1.p); + o.sp = &o1; + + { + transaction t (db->begin ()); + db->persist (o); + db->persist (o1); + t.commit (); + + assert (o.s.loaded ()); + } + + { + session s; + + transaction t (db->begin ()); + auto_ptr<object1> p1 (db->load<object1> (o1.id)); + object* p (p1->p); + + assert (!p->s.loaded ()); + assert (p->n == o.n && + p->sn != o.sn && p->ss != o.ss && p->sp == 0); + + db->load (*p, p->s); + + assert (p->s.loaded ()); + assert (p->n == o.n && + p->sn == o.sn && p->ss == o.ss && p->sp->id == o.sp->id); + + t.commit (); + } + + // Update. + // + o.n++; + o.sn++; + o.ss += 'd'; + + { + transaction t (db->begin ()); + db->update (o); + t.commit (); + } + + { + session s; + + transaction t (db->begin ()); + auto_ptr<object1> p1 (db->load<object1> (o1.id)); + object* p (p1->p); + + db->load (*p, p->s); + assert (p->n == o.n && + p->sn != o.sn && p->ss == o.ss && p->sp->id == o.sp->id); + + t.commit (); + } + } + + // Test object without any columns to load or update. + // + { + using namespace test9; + + object o (123, "abc"); + + { + transaction t (db->begin ()); + db->persist (o); + t.commit (); + + assert (o.s.loaded ()); + } + + { + transaction t (db->begin ()); + auto_ptr<object> p (db->load<object> (o.id)); + + assert (!p->s.loaded ()); + assert (o.id == o.id && + p->sn != o.sn && p->ss != o.ss); + + db->load (*p, p->s); + + assert (p->s.loaded ()); + assert (o.id == o.id && + p->sn == o.sn && p->ss == o.ss); + + t.commit (); + } + + // Update object. + // + o.sn++; + o.ss += 'd'; + + { + transaction t (db->begin ()); + db->update (o); // No-op. + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr<object> p (db->load<object> (o.id)); + db->load (*p, p->s); + assert (o.id == o.id && + p->sn != o.sn && p->ss != o.ss); + t.commit (); + } + + // Update section. + // + { + transaction t (db->begin ()); + db->update (o, o.s); + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr<object> p (db->load<object> (o.id)); + db->load (*p, p->s); + assert (o.id == o.id && + p->sn == o.sn && p->ss == o.ss); + t.commit (); + } + } + + // Test section without any columns or containers to update. + // + { + using namespace test10; + + object o (123); + + { + transaction t (db->begin ()); + db->persist (o); + t.commit (); + + assert (o.s.loaded ()); + } + + { + transaction t (db->begin ()); + auto_ptr<object> p (db->load<object> (o.id)); + + assert (!p->s.loaded ()); + assert (p->n == o.n && p->sn != o.sn); + + db->load (*p, p->s); + + assert (p->s.loaded ()); + assert (p->n == o.n && p->sn == o.sn); + + t.commit (); + } + + // Update. + // + o.n++; + o.sn++; + + { + transaction t (db->begin ()); + db->update (o); + //db->update (o, o.s); // Error. + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr<object> p (db->load<object> (o.id)); + db->load (*p, p->s); + assert (p->n == o.n && p->sn != o.sn); + t.commit (); + } + } + + // Test section with composite member. + // + { + using namespace test11; + + object o (123, "abc"); + + { + transaction t (db->begin ()); + db->persist (o); + t.commit (); + + assert (o.s.loaded ()); + } + + { + transaction t (db->begin ()); + auto_ptr<object> p (db->load<object> (o.id)); + + assert (!p->s.loaded ()); + assert (p->n == o.n && + p->sn != o.sn && p->sc.s != o.sc.s && p->sc.v != o.sc.v); + + db->load (*p, p->s); + + assert (p->s.loaded ()); + assert (p->n == o.n && + p->sn == o.sn && p->sc.s == o.sc.s && p->sc.v == o.sc.v); + + t.commit (); + } + + // Update. + // + o.n++; + o.sn++; + o.sc.s += 'd'; + o.sc.v[0]++; + + { + transaction t (db->begin ()); + db->update (o); + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr<object> p (db->load<object> (o.id)); + db->load (*p, p->s); + assert (p->n == o.n && + p->sn == o.sn && p->sc.s == o.sc.s && p->sc.v == o.sc.v); + t.commit (); + } + } + + // Test change state restoration on transaction rollback. + // + { + using namespace test12; + + object o (123, "abc"); + + { + transaction t (db->begin ()); + db->persist (o); + t.commit (); + + assert (o.s.loaded ()); + } + + // Update. + // + o.n++; + o.sn++; + o.ss += 'd'; + o.s.change (); + + try + { + transaction t (db->begin ()); + db->update (o); + assert (!o.s.changed ()); + throw failed (); + } + catch (const failed&) + { + assert (o.s.changed ()); + } + + // Retry. Also test the object destruction before transaction + // termination case. + // + { + transaction t (db->begin ()); + { + object c (o); + db->update (c); + assert (!c.s.changed ()); + } + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr<object> p (db->load<object> (o.id)); + db->load (*p, p->s); + assert (p->n == o.n && p->sn == o.sn && p->ss == o.ss); + t.commit (); + } + } + + // Test section accessor/modifier. + // + { + using namespace test13; + + object o (123, "abc"); + + { + transaction t (db->begin ()); + db->persist (o); + t.commit (); + + assert (o.s ().loaded ()); + } + + // Load. + // + { + transaction t (db->begin ()); + auto_ptr<object> p (db->load<object> (o.id)); + + assert (!p->s ().loaded ()); + assert (p->n == o.n && p->sn != o.sn && p->ss != o.ss); + + db->load (*p, p->rw_s ()); + + assert (p->s ().loaded ()); + assert (p->n == o.n && p->sn == o.sn && p->ss == o.ss); + + t.commit (); + } + + // Update. + // + o.n++; + o.sn++; + o.ss += 'd'; + + { + transaction t (db->begin ()); + db->update (o); + db->update (o, o.s ()); + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr<object> p (db->load<object> (o.id)); + db->load (*p, p->rw_s ()); + assert (p->n == o.n && p->sn == o.sn && p->ss == o.ss); + t.commit (); + } + + // Test detection of section copy. + // + try + { + transaction t (db->begin ()); + section c (o.s ()); + db->update (o, c); + assert (false); + } + catch (const section_not_in_object&) + { + } + } + + // Test LOB in section streaming, column re-ordering. + // + { + using namespace test14; + + object o (123, "\x01\x02\x03\x04\x05"); + + { + transaction t (db->begin ()); + db->persist (o); + t.commit (); + + assert (o.s.loaded ()); + } + + { + transaction t (db->begin ()); + auto_ptr<object> p (db->load<object> (o.id)); + + assert (!p->s.loaded ()); + assert (p->n == o.n && p->sn != o.sn && p->sb != o.sb); + + db->load (*p, p->s); + + assert (p->s.loaded ()); + assert (p->n == o.n && p->sn == o.sn && p->sb == o.sb); + + t.commit (); + } + + // Update. + // + o.n++; + o.sn++; + o.sb.push_back ('\x06'); + + { + transaction t (db->begin ()); + db->update (o); + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr<object> p (db->load<object> (o.id)); + db->load (*p, p->s); + assert (p->n == o.n && p->sn == o.sn && p->sb == o.sb); + t.commit (); + } + + // We can also update just the section. + // + o.n++; + o.sn++; + o.sb.push_back ('\x07'); + + { + transaction t (db->begin ()); + db->update (o, o.s); + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr<object> p (db->load<object> (o.id)); + db->load (*p, p->s); + assert (p->n != o.n && p->sn == o.sn && p->sb == o.sb); + t.commit (); + } + } + + // Test sections and optimistic concurrency. + // + { + using namespace test15; + + object o (123, "abc"); + + { + transaction t (db->begin ()); + db->persist (o); + t.commit (); + + assert (o.s.loaded ()); + } + + { + transaction t (db->begin ()); + auto_ptr<object> p (db->load<object> (o.id)); + + assert (!p->s.loaded ()); + assert (p->n == o.n && + p->sn != o.sn && p->ss != o.ss && p->sv != o.sv); + + db->load (*p, p->s); + + assert (p->s.loaded ()); + assert (p->n == o.n && + p->sn == o.sn && p->ss == o.ss && p->sv == o.sv); + + t.commit (); + } + + // Update object. + // + object o1 (o); + o1.n++; + o1.sn++; + o1.ss += 'd'; + o1.sv[0]++; + + { + transaction t (db->begin ()); + auto_ptr<object> p (db->load<object> (o.id)); + + db->update (o1); + assert (o.v != o1.v); + + try + { + db->load (*p, p->s); + assert (false); + } + catch (const object_changed&) + { + db->reload (*p); + assert (!p->s.loaded ()); + db->load (*p, p->s); + + assert (p->n == o1.n && + p->sn == o1.sn && p->ss == o1.ss && p->sv == o1.sv); + } + + db->reload (o); + assert (o.v == o1.v); + assert (o.n == o1.n && + o.sn == o1.sn && o.ss == o1.ss && o.sv == o1.sv); + t.commit (); + } + + // Update section. + // + o1.sn++; + o1.ss += 'd'; + o1.sv[0]++; + + { + transaction t (db->begin ()); + auto_ptr<object> p (db->load<object> (o.id)); + + db->update (o1, o1.s); + assert (o.v != o1.v); + + try + { + db->load (*p, p->s); + assert (false); + } + catch (const object_changed&) + { + db->reload (*p); + assert (!p->s.loaded ()); + db->load (*p, p->s); + + assert (p->n == o1.n && + p->sn == o1.sn && p->ss == o1.ss && p->sv == o1.sv); + } + + db->reload (o); + assert (o.v == o1.v); + assert (o.n == o1.n && + o.sn == o1.sn && o.ss == o1.ss && o.sv == o1.sv); + t.commit (); + } + + // Update changed section. + // + o1.sn++; + o1.ss += 'd'; + o1.sv[0]++; + + { + transaction t (db->begin ()); + db->update (o1, o1.s); + + try + { + db->update (o, o.s); + assert (false); + } + catch (const object_changed&) + { + db->reload (o); + db->update (o, o.s); + } + + t.commit (); + } + } + + // Test container-only sections and optimistic concurrency. + // + { + using namespace test16; + + object o (123); + + { + transaction t (db->begin ()); + db->persist (o); + t.commit (); + + assert (o.s.loaded ()); + } + + { + transaction t (db->begin ()); + auto_ptr<object> p (db->load<object> (o.id)); + + assert (!p->s.loaded ()); + assert (p->sv != o.sv); + + db->load (*p, p->s); + + assert (p->s.loaded ()); + assert (p->sv == o.sv); + + t.commit (); + } + + // Update object. + // + object o1 (o); + o1.sv[0]++; + + { + transaction t (db->begin ()); + auto_ptr<object> p (db->load<object> (o.id)); + + db->update (o1); + assert (o.v != o1.v); + + try + { + db->load (*p, p->s); + assert (false); + } + catch (const object_changed&) + { + db->reload (*p); + assert (!p->s.loaded ()); + db->load (*p, p->s); + + assert (p->sv == o1.sv); + } + + db->reload (o); + assert (o.v == o1.v); + assert (o.sv == o1.sv); + t.commit (); + } + + // Update section. + // + o1.sv[0]++; + + { + transaction t (db->begin ()); + auto_ptr<object> p (db->load<object> (o.id)); + + db->update (o1, o1.s); + assert (o.v != o1.v); + + try + { + db->load (*p, p->s); + assert (false); + } + catch (const object_changed&) + { + db->reload (*p); + assert (!p->s.loaded ()); + db->load (*p, p->s); + + assert (p->sv == o1.sv); + } + + db->reload (o); + assert (o.v == o1.v); + assert (o.sv == o1.sv); + t.commit (); + } + + // Update changed section. + // + o1.sv[0]++; + + { + transaction t (db->begin ()); + db->update (o1, o1.s); + + try + { + db->update (o, o.s); + assert (false); + } + catch (const object_changed&) + { + db->reload (o); + db->update (o, o.s); + } + + t.commit (); + } + } + + // Test reuse-inheritance, sections, and optimistic concurrency. + // + { + using namespace test17; + + object o (123); + + { + transaction t (db->begin ()); + db->persist (o); + t.commit (); + + assert (o.s1.loaded ()); + assert (o.s2.loaded ()); + } + + { + transaction t (db->begin ()); + auto_ptr<object> p (db->load<object> (o.id)); + + assert (!p->s1.loaded ()); + assert (!p->s2.loaded ()); + assert (p->s1n != o.s1n && p->s2v != o.s2v); + + db->load (*p, p->s1); + db->load (*p, p->s2); + + assert (p->s1.loaded ()); + assert (p->s2.loaded ()); + assert (p->s1n == o.s1n && p->s2v == o.s2v); + + t.commit (); + } + + object o1 (o); + + // Update object. + // + for (unsigned short s (1); s < 3; ++s) + { + o1.s1n++; + o1.s2v[0]++; + + transaction t (db->begin ()); + auto_ptr<object> p (db->load<object> (o.id)); + + db->update (o1); + assert (o.v != o1.v); + + try + { + switch (s) + { + case 1: db->load (*p, p->s1); break; + case 2: db->load (*p, p->s2); break; + default: break; + } + assert (false); + } + catch (const object_changed&) + { + db->reload (*p); + + assert (!p->s1.loaded ()); + assert (!p->s2.loaded ()); + + db->load (*p, p->s1); + db->load (*p, p->s2); + + assert (p->s1n == o1.s1n && p->s2v == o1.s2v); + } + + db->reload (o); + assert (o.v == o1.v); + assert (o.s1n == o1.s1n && o.s2v == o1.s2v); + t.commit (); + } + + // Update section. + // + for (unsigned short s (1); s < 3; ++s) + { + transaction t (db->begin ()); + auto_ptr<object> p (db->load<object> (o.id)); + + switch (s) + { + case 1: + o1.s1n++; + db->update (o1, o1.s1); + assert (o.v != o1.v); + break; + case 2: + o1.s2v[0]++; + db->update (o1, o1.s2); + assert (o.v != o1.v); + break; + default: break; + } + + try + { + switch (s) + { + case 1: db->load (*p, p->s1); break; + case 2: db->load (*p, p->s2); break; + default: break; + } + assert (false); + } + catch (const object_changed&) + { + db->reload (*p); + + assert (!p->s1.loaded ()); + assert (!p->s2.loaded ()); + + db->load (*p, p->s1); + db->load (*p, p->s2); + + assert (p->s1n == o1.s1n && p->s2v == o1.s2v); + } + + db->reload (o); + assert (o.v == o1.v); + assert (o.s1n == o1.s1n && o.s2v == o1.s2v); + t.commit (); + } + + // Update changed section. + // + for (unsigned short s (1); s < 3; ++s) + { + transaction t (db->begin ()); + + switch (s) + { + case 1: + o1.s1n++; + db->update (o1, o1.s1); + break; + case 2: + o1.s2v[0]++; + db->update (o1, o1.s2); + break; + default: break; + } + + try + { + switch (s) + { + case 1: db->update (o, o.s1); break; + case 2: db->update (o, o.s2); break; + default: break; + } + assert (false); + } + catch (const object_changed&) + { + db->reload (o); + + switch (s) + { + case 1: db->update (o, o.s1); break; + case 2: db->update (o, o.s2); break; + default: break; + } + } + + db->reload (o1); + + t.commit (); + } + } + + // Test change-updated section and change-tracking container. + // + { + using namespace test18; + + object o (123); + + { + transaction t (db->begin ()); + db->persist (o); + t.commit (); + + assert (o.s.loaded ()); + } + + { + transaction t (db->begin ()); + auto_ptr<object> p (db->load<object> (o.id)); + + assert (!p->s.loaded ()); + assert (p->sn != o.sn && p->sv != o.sv); + + db->load (*p, p->s); + + assert (p->s.loaded ()); + assert (p->sn == o.sn && p->sv == o.sv); + + t.commit (); + } + + // Update but don't mark as changed. + // + o.sn++; + o.sv.modify (0)++; + + { + transaction t (db->begin ()); + db->update (o); // Automatically marked as changed. + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr<object> p (db->load<object> (o.id)); + db->load (*p, p->s); + assert (p->sn == o.sn && p->sv == o.sv); + t.commit (); + } + + // Test updating just the section manually. + // + o.sn++; + o.sv.modify (0)++; + + { + transaction t (db->begin ()); + db->update (o, o.s); + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr<object> p (db->load<object> (o.id)); + db->load (*p, p->s); + assert (p->sn == o.sn && p->sv == o.sv); + t.commit (); + } + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/common/section/basics/makefile b/common/section/basics/makefile new file mode 100644 index 0000000..f0484e8 --- /dev/null +++ b/common/section/basics/makefile @@ -0,0 +1,115 @@ +# file : common/section/basics/makefile +# copyright : Copyright (c) 2009-2013 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 +genf := $(call odb-gen,$(odb_hdr)) +gen := $(addprefix $(out_base)/,$(genf)) +cxx_obj := $(addprefix $(out_base)/,$(cxx_tun:.cxx=.o)) $(filter %.o,$(gen:.cxx=.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 + +# 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) + +$(gen): $(odb) +$(gen): odb := $(odb) +$(gen) $(dist): export odb_options += --generate-schema --generate-query \ +--table-prefix t_section_b_ +$(gen): cpp_options := -I$(src_base) +$(gen): $(common.l.cpp-options) + +ifneq ($(db_id),common) +$(gen): odb_options += --database $(db_id) +else +$(gen): odb_options += --multi-database dynamic +endif + +$(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): sources := $(cxx_tun) +$(dist): headers := $(odb_hdr) +$(dist): data_dist := test.std +$(dist): export name := $(name) +$(dist): export extra_dist := $(data_dist) $(call vc8projs,$(name)) \ +$(call vc9projs,$(name)) $(call vc10projs,$(name)) $(call vc11projs,$(name)) +$(dist): + $(call dist-data,$(sources) $(headers) $(data_dist)) + $(call meta-automake,../../template/Makefile.am) + $(call meta-vc8projs,../../template/template,$(name)) + $(call meta-vc9projs,../../template/template,$(name)) + $(call meta-vc10projs,../../template/template,$(name)) + $(call meta-vc11projs,../../template/template,$(name)) + +# Test. +# +ifneq ($(db_id),common) +$(eval $(call test-rule)) +else +$(foreach d,$(databases),$(eval $(call test-rule,$d))) +endif + +# Clean. +# +$(clean): \ + $(driver).o.clean \ + $(addsuffix .cxx.clean,$(cxx_obj)) \ + $(addsuffix .cxx.clean,$(cxx_od)) \ + $(addsuffix .hxx.clean,$(filter %.cxx,$(gen))) + $(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/vc8proj.make) +$(call include,$(bld_root)/meta/vc9proj.make) +$(call include,$(bld_root)/meta/vc10proj.make) +$(call include,$(bld_root)/meta/vc11proj.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/section/basics/test.hxx b/common/section/basics/test.hxx new file mode 100644 index 0000000..c129f3e --- /dev/null +++ b/common/section/basics/test.hxx @@ -0,0 +1,600 @@ +// file : common/section/basics/test.hxx +// copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include <string> +#include <vector> + +#include <odb/core.hxx> +#include <odb/section.hxx> +#include <odb/vector.hxx> + +#ifdef ODB_COMPILER +# if defined(ODB_DATABASE_PGSQL) +# define BLOB_TYPE "BYTEA" +# elif defined(ODB_DATABASE_MSSQL) +# define BLOB_TYPE "VARBINARY(max)" +# else +# define BLOB_TYPE "BLOB" +# endif +#endif + +// Test lazy-loaded, always-updated section. +// +#pragma db namespace table("t1_") +namespace test1 +{ + #pragma db object + struct object + { + object (int n_ = 999, const std::string& s_ = "xxx") + : sn (n_), n (n_), ss (s_) {sv.push_back (n_);} + + #pragma db id auto + unsigned long id; + + #pragma db load(lazy) + odb::section s; + + #pragma db section(s) + int sn; + + int n; + + #pragma db section(s) + std::string ss; + + #pragma db section(s) + std::vector<int> sv; + }; +} + +// Test lazy-loaded, change-updated section. +// +#pragma db namespace table("t2_") +namespace test2 +{ + #pragma db object + struct object + { + object (int n_ = 999, const std::string& s_ = "xxx") + : sn (n_), n (n_), ss (s_) {sv.push_back (n_);} + + #pragma db id auto + unsigned long id; + + #pragma db load(lazy) update(change) + odb::section s; + + #pragma db section(s) + int sn; + + int n; + + #pragma db section(s) + std::string ss; + + #pragma db section(s) + std::vector<int> sv; + }; +} + +// Test lazy-loaded, manually-updated section. +// +#pragma db namespace table("t3_") +namespace test3 +{ + #pragma db object + struct object + { + object (int n_ = 999, const std::string& s_ = "xxx") + : sn (n_), n (n_), ss (s_) {sv.push_back (n_);} + + #pragma db id auto + unsigned long id; + + #pragma db load(lazy) update(manual) + odb::section s; + + #pragma db section(s) + int sn; + + int n; + + #pragma db section(s) + std::string ss; + + #pragma db section(s) + std::vector<int> sv; + }; +} + +// Test eager-loaded, change-updated section. +// +#pragma db namespace table("t4_") +namespace test4 +{ + #pragma db object + struct object + { + object (int n_ = 999, const std::string& s_ = "xxx") + : sn (n_), n (n_), ss (s_) {sv.push_back (n_);} + + #pragma db id auto + unsigned long id; + + #pragma db section(s) + int sn; + + #pragma db update(change) + odb::section s; + + int n; + + #pragma db section(s) + std::string ss; + + #pragma db section(s) + std::vector<int> sv; + }; +} + +// Test eager-loaded, manually-updated section. +// +#pragma db namespace table("t5_") +namespace test5 +{ + #pragma db object + struct object + { + object (int n_ = 999, const std::string& s_ = "xxx") + : sn (n_), n (n_), ss (s_) {sv.push_back (n_);} + + #pragma db id auto + unsigned long id; + + #pragma db section(s) + int sn; + + #pragma db update(manual) + odb::section s; + + int n; + + #pragma db section(s) + std::string ss; + + #pragma db section(s) + std::vector<int> sv; + }; +} + +// Test value-only and container-only section. Also multiple sections +// in an object. +// +#pragma db namespace table("t6_") +namespace test6 +{ + #pragma db object + struct object + { + object (int n_ = 999, const std::string& s_ = "xxx") + : n (n_), sn (n_), ss (s_) {sv.push_back (n_);} + + #pragma db id auto + unsigned long id; + + int n; + + #pragma db load(lazy) + odb::section s1; + + #pragma db load(lazy) + odb::section s2; + + #pragma db section(s1) + int sn; + + #pragma db section(s1) + std::string ss; + + #pragma db section(s2) + std::vector<int> sv; + }; +} + +// Test sections and reuse inheritance. +// +#pragma db namespace table("t7_") +namespace test7 +{ + #pragma db object abstract + struct base + { + #pragma db id auto + unsigned long id; + + #pragma db load(lazy) + odb::section s1; // Empty section. + + #pragma db load(lazy) + odb::section s2; + + #pragma db section(s2) + int sn2; + }; + + #pragma db object abstract + struct interm: base + { + // Section s1 is still empty. + + #pragma db section(s2) + std::string ss2; + }; + + #pragma db object + struct derived: interm + { + #pragma db section(s1) + int sn1; + }; + + #pragma db object + struct object: derived + { + object (int n = 999, const std::string& s = "xxx", bool b = false) + { + sn1 = sn2 = n; + ss1 = ss2 = s; + sb2 = b; + } + + #pragma db section(s1) + std::string ss1; + + #pragma db section(s2) + bool sb2; + }; +} + +// Test readonly and inverse section members. +// +#pragma db namespace table("t8_") +namespace test8 +{ + struct object1; + + #pragma db object session + struct object + { + object (int n_ = 999, const std::string& s_ = "xxx", object1* p_ = 0) + : n (n_), sn (n_), ss (s_), sp (p_) {} + + #pragma db id auto + unsigned long id; + + int n; + + #pragma db load(lazy) + odb::section s; + + #pragma db section(s) readonly + int sn; + + #pragma db section(s) + std::string ss; + + #pragma db inverse(p) section(s) + object1* sp; + }; + + #pragma db object session + struct object1 + { + object1 (object* p_ = 0): p (p_) {} + ~object1 () {delete p;} + + #pragma db id auto + unsigned long id; + + object* p; + }; +} + +// Test object without any columns to load or update. +// +#pragma db namespace table("t9_") +namespace test9 +{ + #pragma db object + struct object + { + object (int n_ = 999, const std::string& s_ = "xxx"): sn (n_), ss (s_) {} + + #pragma db id auto + unsigned long id; + + #pragma db load(lazy) update(manual) + odb::section s; + + #pragma db section(s) + int sn; + + #pragma db section(s) + std::string ss; + }; +} + +// Test section without any columns or containers to update. +// +#pragma db namespace table("t10_") +namespace test10 +{ + #pragma db object + struct object + { + object (int n_ = 999): n (n_), sn (n_) {} + + #pragma db id auto + unsigned long id; + + int n; + + #pragma db load(lazy) + odb::section s; + + #pragma db section(s) readonly + int sn; + }; +} + +// Test section with composite member. +// +#pragma db namespace table("t11_") +namespace test11 +{ + #pragma db value + struct comp + { + comp (int n_, const std::string& s_): s (s_) {v.push_back (n_);} + + std::string s; + std::vector<int> v; + }; + + #pragma db object + struct object + { + object (int n_ = 999, const std::string& s_ = "xxx") + : n (n_), sn (n_), sc (n_, s_) {} + + #pragma db id auto + unsigned long id; + + int n; + + #pragma db load(lazy) + odb::section s; + + #pragma db section(s) + int sn; + + #pragma db section(s) + comp sc; + }; +} + +// Test change state restoration on transaction rollback. +// +#pragma db namespace table("t12_") +namespace test12 +{ + #pragma db object + struct object + { + object (int n_ = 999, const std::string& s_ = "xxx") + : sn (n_), n (n_), ss (s_) {} + + #pragma db id auto + unsigned long id; + + #pragma db load(lazy) update(change) + odb::section s; + + #pragma db section(s) + int sn; + + int n; + + #pragma db section(s) + std::string ss; + }; +} + +// Test section accessor/modifier. +// +#pragma db namespace table("t13_") +namespace test13 +{ + #pragma db object + struct object + { + object (int n_ = 999, const std::string& s_ = "xxx") + : n (n_), sn (n_), ss (s_) {} + + #pragma db id auto + unsigned long id; + + int n; + + #pragma db section(s_) + int sn; + + #pragma db section(s_) + std::string ss; + + public: + const odb::section& + s () const {return s_;} + + odb::section& + rw_s () {return s_;} + + private: + #pragma db load(lazy) update(manual) + odb::section s_; + }; +} + +// Test LOB in section streaming, column re-ordering. +// +#pragma db namespace table("t14_") +namespace test14 +{ + #pragma db object + struct object + { + object (int n_ = 999, const std::string& s_ = "xxx") + : sb (s_.begin (), s_.end ()), sn (n_), n (n_) {} + + #pragma db id auto + unsigned long id; + + #pragma db load(lazy) + odb::section s; + + #pragma db section(s) type(BLOB_TYPE) + std::vector<char> sb; // Comes before sn. + + #pragma db section(s) + int sn; + + int n; + }; +} + +// Test sections and optimistic concurrency. +// +#pragma db namespace table("t15_") +namespace test15 +{ + #pragma db object optimistic + struct object + { + object (int n_ = 999, const std::string& s_ = "xxx") + : sn (n_), n (n_), ss (s_) {sv.push_back (n_);} + + #pragma db id auto + unsigned long id; + + #pragma db version mssql:type("ROWVERSION") + unsigned long long v; + + #pragma db load(lazy) + odb::section s; + + #pragma db section(s) + int sn; + + int n; + + #pragma db section(s) + std::string ss; + + #pragma db section(s) + std::vector<int> sv; + }; +} + +// Test container-only sections and optimistic concurrency. +// +#pragma db namespace table("t16_") +namespace test16 +{ + #pragma db object optimistic + struct object + { + object (int n = 999) {sv.push_back (n);} + + #pragma db id auto + unsigned long id; + + #pragma db version // mssql:type("ROWVERSION") + unsigned long long v; + + #pragma db load(lazy) + odb::section s; + + #pragma db section(s) + std::vector<int> sv; + }; +} + +// Test reuse-inheritance, sections, and optimistic concurrency. +// +#pragma db namespace table("t17_") +namespace test17 +{ + #pragma db object optimistic sectionable abstract + struct root + { + #pragma db id auto + unsigned long id; + + #pragma db version + unsigned long long v; + }; + + #pragma db object + struct base: root + { + }; + + #pragma db object + struct object: base + { + object (int n = 999): s1n (n) {s2v.push_back (n);} + + #pragma db load(lazy) + odb::section s1; + + #pragma db section(s1) + int s1n; + + #pragma db load(lazy) + odb::section s2; + + #pragma db section(s2) + std::vector<int> s2v; + }; +} + +// Test change-updated section and change-tracking container. +// +#pragma db namespace table("t18_") +namespace test18 +{ + #pragma db object + struct object + { + object (int n = 999): sn (n) {sv.push_back (n);} + + #pragma db id auto + unsigned long id; + + #pragma db load(lazy) update(change) + odb::section s; + + #pragma db section(s) + int sn; + + #pragma db section(s) + odb::vector<int> sv; + }; +} + +#endif // TEST_HXX diff --git a/common/section/basics/test.std b/common/section/basics/test.std new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/common/section/basics/test.std diff --git a/common/section/polymorphism/driver.cxx b/common/section/polymorphism/driver.cxx new file mode 100644 index 0000000..8c38035 --- /dev/null +++ b/common/section/polymorphism/driver.cxx @@ -0,0 +1,1763 @@ +// file : common/section/polymorphism/driver.cxx +// copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +// Test sections in polymorphic objects. +// + +#include <memory> // std::auto_ptr +#include <cassert> +#include <iostream> + +#include <odb/session.hxx> +#include <odb/database.hxx> +#include <odb/transaction.hxx> + +#include <common/common.hxx> + +#include "test.hxx" +#include "test-odb.hxx" + +using namespace std; +using namespace odb::core; + +struct failed {}; + +int +main (int argc, char* argv[]) +{ + try + { + auto_ptr<database> db (create_database (argc, argv)); + + // Test basic polymorphic section functionality. + // + { + using namespace test1; + + base b (123, "abc"); + derived d (234, "bcd", true); + + { + transaction t (db->begin ()); + db->persist (b); + db->persist (d); + t.commit (); + + assert (b.rs1.loaded ()); + assert (b.rs2.loaded ()); + assert (b.rs3.loaded ()); + assert (b.rs4.loaded ()); + assert (b.bs1.loaded ()); + + assert (d.rs1.loaded ()); + assert (d.rs2.loaded ()); + assert (d.rs3.loaded ()); + assert (d.rs4.loaded ()); + assert (d.bs1.loaded ()); + assert (d.ds1.loaded ()); + } + + { + transaction t (db->begin ()); + auto_ptr<base> pb (db->load<base> (b.id)); + auto_ptr<derived> pd (db->load<derived> (d.id)); + + assert (!pb->rs1.loaded ()); + assert (!pb->rs2.loaded ()); + assert (!pb->rs3.loaded ()); + assert (!pb->rs4.loaded ()); + assert (!pb->bs1.loaded ()); + + assert (!pd->rs1.loaded ()); + assert (!pd->rs2.loaded ()); + assert (!pd->rs3.loaded ()); + assert (!pd->rs4.loaded ()); + assert (!pd->bs1.loaded ()); + assert (!pd->ds1.loaded ()); + + assert (pb->rs1n != b.rs1n && pb->rs1s != b.rs1s && + pb->rs2n != b.rs2n && pb->rs2v != b.rs2v && + pb->rs3v != b.rs3v && + pb->rs4n != b.rs4n && + pb->bs1n != b.bs1n); + + assert (pd->rs1n != d.rs1n && pd->rs1s != d.rs1s && + pd->rs1b != d.rs1b && pd->rs1v != d.rs1v && + pd->rs2n != d.rs2n && pd->rs2v != d.rs2v && + pd->rs3v != d.rs3v && pd->rs3n != d.rs3n && + pd->rs4n != d.rs4n && pd->rs4s != d.rs4s && + pd->bs1n != d.bs1n && pd->bs1s != d.bs1s && + pd->ds1n != d.ds1n); + + db->load (*pb, pb->rs1); + db->load (*pb, pb->rs2); + db->load (*pb, pb->rs3); + db->load (*pb, pb->rs4); + db->load (*pb, pb->bs1); + + root* pr (pd.get ()); + db->load (*pr, pr->rs1); // Via base. + db->load (*pd, pd->rs2); + db->load (*pr, pr->rs3); // Via base. + db->load (*pd, pd->rs4); + db->load (*pd, pd->bs1); + db->load (*pd, pd->ds1); + + try + { + db->load (*pr, pd->bs1); // Object-section association is static. + assert (false); + } + catch (const section_not_in_object&) {} + + assert (pb->rs1.loaded ()); + assert (pb->rs2.loaded ()); + assert (pb->rs3.loaded ()); + assert (pb->rs4.loaded ()); + assert (pb->bs1.loaded ()); + + assert (pd->rs1.loaded ()); + assert (pd->rs2.loaded ()); + assert (pd->rs3.loaded ()); + assert (pd->rs4.loaded ()); + assert (pd->bs1.loaded ()); + assert (pd->ds1.loaded ()); + + assert (pb->rs1n == b.rs1n && pb->rs1s == b.rs1s && + pb->rs2n == b.rs2n && pb->rs2v == b.rs2v && + pb->rs3v == b.rs3v && + pb->rs4n == b.rs4n && + pb->bs1n == b.bs1n); + + assert (pd->rs1n == d.rs1n && pd->rs1s == d.rs1s && + pd->rs1b == d.rs1b && pd->rs1v == d.rs1v && + pd->rs2n == d.rs2n && pd->rs2v == d.rs2v && + pd->rs3v == d.rs3v && pd->rs3n == d.rs3n && + pd->rs4n == d.rs4n && pd->rs4s == d.rs4s && + pd->bs1n == d.bs1n && pd->bs1s == d.bs1s && + pd->ds1n == d.ds1n); + t.commit (); + } + + // Update object. + // + b.rs1n++; + b.rs1s += 'd'; + b.rs1.change (); + b.rs2n++; + b.rs2v[0]++; + b.rs3v[0]++; + b.rs4n++; + b.bs1n++; + + d.rs1n++; + d.rs1s += 'e'; + d.rs1b = !d.rs1b; + d.rs1v[0]++; + d.rs1.change (); + d.rs2n++; + d.rs2v[0]++; + d.rs3v[0]++; + d.rs3n++; + d.rs4n++; + d.rs4s += 'e'; + d.bs1n++; + d.bs1s += 'e'; + d.ds1n++; + + { + transaction t (db->begin ()); + db->update (b); + db->update (d); + t.commit (); + + assert (!b.rs1.changed ()); + assert (!d.rs1.changed ()); + } + + { + transaction t (db->begin ()); + auto_ptr<base> pb (db->load<base> (b.id)); + auto_ptr<derived> pd (db->load<derived> (d.id)); + + db->load (*pb, pb->rs1); + db->load (*pb, pb->rs2); + db->load (*pb, pb->rs3); + db->load (*pb, pb->rs4); + db->load (*pb, pb->bs1); + + db->load (*pd, pd->rs1); + db->load (*pd, pd->rs2); + db->load (*pd, pd->rs3); + db->load (*pd, pd->rs4); + db->load (*pd, pd->bs1); + db->load (*pd, pd->ds1); + + assert (pb->rs1n == b.rs1n && pb->rs1s == b.rs1s && + pb->rs2n == b.rs2n && pb->rs2v == b.rs2v && + pb->rs3v == b.rs3v && + pb->rs4n == b.rs4n && + pb->bs1n == b.bs1n); + + assert (pd->rs1n == d.rs1n && pd->rs1s == d.rs1s && + pd->rs1b == d.rs1b && pd->rs1v == d.rs1v && + pd->rs2n == d.rs2n && pd->rs2v == d.rs2v && + pd->rs3v == d.rs3v && pd->rs3n == d.rs3n && + pd->rs4n == d.rs4n && pd->rs4s == d.rs4s && + pd->bs1n == d.bs1n && pd->bs1s == d.bs1s && + pd->ds1n == d.ds1n); + t.commit (); + } + + // Update section. + // + b.rs1n++; + b.rs1s += 'd'; + b.rs2n++; + b.rs2v[0]++; + b.rs3v[0]++; + b.rs4n++; + b.bs1n++; + + d.rs1n++; + d.rs1s += 'e'; + d.rs1b = !d.rs1b; + d.rs1v[0]++; + d.rs2n++; + d.rs2v[0]++; + d.rs3v[0]++; + d.rs3n++; + d.rs4n++; + d.rs4s += 'e'; + d.bs1n++; + d.bs1s += 'e'; + d.ds1n++; + + { + transaction t (db->begin ()); + db->update (b, b.rs1); + db->update (b, b.rs2); + db->update (b, b.rs3); + db->update (b, b.rs4); + db->update (b, b.bs1); + + db->update (d, d.rs1); + db->update (d, d.rs2); + db->update (d, d.rs3); + db->update (d, d.rs4); + db->update (d, d.bs1); + db->update (d, d.ds1); + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr<base> pb (db->load<base> (b.id)); + auto_ptr<derived> pd (db->load<derived> (d.id)); + + db->load (*pb, pb->rs1); + db->load (*pb, pb->rs2); + db->load (*pb, pb->rs3); + db->load (*pb, pb->rs4); + db->load (*pb, pb->bs1); + + db->load (*pd, pd->rs1); + db->load (*pd, pd->rs2); + db->load (*pd, pd->rs3); + db->load (*pd, pd->rs4); + db->load (*pd, pd->bs1); + db->load (*pd, pd->ds1); + + assert (pb->rs1n == b.rs1n && pb->rs1s == b.rs1s && + pb->rs2n == b.rs2n && pb->rs2v == b.rs2v && + pb->rs3v == b.rs3v && + pb->rs4n == b.rs4n && + pb->bs1n == b.bs1n); + + assert (pd->rs1n == d.rs1n && pd->rs1s == d.rs1s && + pd->rs1b == d.rs1b && pd->rs1v == d.rs1v && + pd->rs2n == d.rs2n && pd->rs2v == d.rs2v && + pd->rs3v == d.rs3v && pd->rs3n == d.rs3n && + pd->rs4n == d.rs4n && pd->rs4s == d.rs4s && + pd->bs1n == d.bs1n && pd->bs1s == d.bs1s && + pd->ds1n == d.ds1n); + t.commit (); + } + + // Reload. + // + b.rs1n++; + b.rs1s += 'd'; + b.rs1.change (); + b.rs2n++; + b.rs2v[0]++; + b.rs3v[0]++; + b.rs4n++; + b.bs1n++; + + d.rs1n++; + d.rs1s += 'e'; + d.rs1b = !d.rs1b; + d.rs1v[0]++; + d.rs1.change (); + d.rs2n++; + d.rs2v[0]++; + d.rs3v[0]++; + d.rs3n++; + d.rs4n++; + d.rs4s += 'e'; + d.bs1n++; + d.bs1s += 'e'; + d.ds1n++; + + { + transaction t (db->begin ()); + auto_ptr<base> pb (db->load<base> (b.id)); + auto_ptr<derived> pd (db->load<derived> (d.id)); + + db->load (*pb, pb->rs1); + db->load (*pb, pb->rs2); + db->load (*pb, pb->rs3); + db->load (*pb, pb->rs4); + db->load (*pb, pb->bs1); + + db->load (*pd, pd->rs1); + db->load (*pd, pd->rs2); + db->load (*pd, pd->rs3); + db->load (*pd, pd->rs4); + db->load (*pd, pd->bs1); + db->load (*pd, pd->ds1); + + db->update (b); + db->update (d); + + db->reload (*pb); + db->reload (*pd); + + assert (pb->rs1n == b.rs1n && pb->rs1s == b.rs1s && + pb->rs2n == b.rs2n && pb->rs2v == b.rs2v && + pb->rs3v == b.rs3v && + pb->rs4n == b.rs4n && + pb->bs1n == b.bs1n); + + assert (pd->rs1n == d.rs1n && pd->rs1s == d.rs1s && + pd->rs1b == d.rs1b && pd->rs1v == d.rs1v && + pd->rs2n == d.rs2n && pd->rs2v == d.rs2v && + pd->rs3v == d.rs3v && pd->rs3n == d.rs3n && + pd->rs4n == d.rs4n && pd->rs4s == d.rs4s && + pd->bs1n == d.bs1n && pd->bs1s == d.bs1s && + pd->ds1n == d.ds1n); + + t.commit (); + } + } + + // Test empty section and override "gap". + // + { + using namespace test2; + + derived d (234); + + { + transaction t (db->begin ()); + db->persist (d); + t.commit (); + + assert (d.s.loaded ()); + } + + { + transaction t (db->begin ()); + auto_ptr<derived> pd (db->load<derived> (d.id)); + + assert (!pd->s.loaded ()); + assert (pd->sn != d.sn && pd->sv != d.sv); + + root* pr (pd.get ()); + db->load (*pr, pr->s); // Via root. + + assert (pd->s.loaded ()); + assert (pd->sn == d.sn && pd->sv == d.sv); + t.commit (); + } + + // Update object. + // + d.sn++; + d.sv[0]++; + + { + transaction t (db->begin ()); + root* pr (&d); + db->update (pr); // Via root. + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr<derived> pd (db->load<derived> (d.id)); + + base* pb (pd.get ()); + db->load (*pb, pb->s); // Via base. + + assert (pd->sn == d.sn && pd->sv == d.sv); + t.commit (); + } + + // Update section. + // + d.sn++; + d.sv[0]++; + + { + transaction t (db->begin ()); + root* pr (&d); + db->update (*pr, pr->s); // Via root. + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr<derived> pd (db->load<derived> (d.id)); + + db->load (*pd, pd->s); + + assert (pd->sn == d.sn && pd->sv == d.sv); + t.commit (); + } + + // Reload. + // + d.sn++; + d.sv[0]++; + + { + transaction t (db->begin ()); + auto_ptr<derived> pd (db->load<derived> (d.id)); + + db->load (*pd, pd->s); + + db->update (d); + + root* pr (pd.get ()); + db->reload (*pr); + + assert (pd->sn == d.sn && pd->sv == d.sv); + t.commit (); + } + } + + // Test value-only/container-only base/override combinations. + // + { + using namespace test3; + + root r (123); + base b (234); + derived d (345, "abc"); + + { + transaction t (db->begin ()); + db->persist (r); + db->persist (b); + db->persist (d); + t.commit (); + + assert (r.s1.loaded ()); + assert (r.s2.loaded ()); + assert (r.s3.loaded ()); + assert (r.s4.loaded ()); + + assert (b.s1.loaded ()); + assert (b.s2.loaded ()); + assert (b.s3.loaded ()); + assert (b.s4.loaded ()); + + assert (d.s1.loaded ()); + assert (d.s2.loaded ()); + assert (d.s3.loaded ()); + assert (d.s4.loaded ()); + } + + { + transaction t (db->begin ()); + auto_ptr<root> pr (db->load<root> (r.id)); + auto_ptr<base> pb (db->load<base> (b.id)); + auto_ptr<derived> pd (db->load<derived> (d.id)); + + assert (!pr->s1.loaded ()); + assert (!pr->s2.loaded ()); + assert (!pr->s3.loaded ()); + assert (!pr->s4.loaded ()); + + assert (!pb->s1.loaded ()); + assert (!pb->s2.loaded ()); + assert (!pb->s3.loaded ()); + assert (!pb->s4.loaded ()); + + assert (!pd->s1.loaded ()); + assert (!pd->s2.loaded ()); + assert (!pd->s3.loaded ()); + assert (!pd->s4.loaded ()); + + assert (pr->s1n != r.s1n && + pr->s2n != r.s2n && + pr->s3v != r.s3v && + pr->s4nv != r.s4nv); + + assert (pb->s1n != b.s1n && + pb->s2n != b.s2n && + pb->s3v != b.s3v && + pb->s4nv != b.s4nv); + + assert (pd->s1n != d.s1n && pd->s1s != d.s1s && + pd->s2n != d.s2n && pd->s2v != d.s2v && + pd->s3v != d.s3v && pd->s3n != d.s3n && + pd->s4nv != d.s4nv && pd->s4sv != d.s4sv); + + db->load (*pr, pr->s1); + db->load (*pr, pr->s2); + db->load (*pr, pr->s3); + db->load (*pr, pr->s4); + + db->load (*pb, pb->s1); + db->load (*pb, pb->s2); + db->load (*pb, pb->s3); + db->load (*pb, pb->s4); + + root* pdr (pd.get ()); + db->load (*pdr, pdr->s1); + db->load (*pdr, pdr->s2); + db->load (*pdr, pdr->s3); + db->load (*pdr, pdr->s4); + + assert (pr->s1.loaded ()); + assert (pr->s2.loaded ()); + assert (pr->s3.loaded ()); + assert (pr->s4.loaded ()); + + assert (pb->s1.loaded ()); + assert (pb->s2.loaded ()); + assert (pb->s3.loaded ()); + assert (pb->s4.loaded ()); + + assert (pd->s1.loaded ()); + assert (pd->s2.loaded ()); + assert (pd->s3.loaded ()); + assert (pd->s4.loaded ()); + + assert (pr->s1n == r.s1n && + pr->s2n == r.s2n && + pr->s3v == r.s3v && + pr->s4nv == r.s4nv); + + assert (pb->s1n == b.s1n && + pb->s2n == b.s2n && + pb->s3v == b.s3v && + pb->s4nv == b.s4nv); + + assert (pd->s1n == d.s1n && pd->s1s == d.s1s && + pd->s2n == d.s2n && pd->s2v == d.s2v && + pd->s3v == d.s3v && pd->s3n == d.s3n && + pd->s4nv == d.s4nv && pd->s4sv == d.s4sv); + t.commit (); + } + + // Update object. + // + r.s1n++; + r.s2n++; + r.s3v[0]++; + r.s4nv[0]++; + + b.s1n++; + b.s2n++; + b.s3v[0]++; + b.s4nv[0]++; + + d.s1n++; + d.s1s += 'd'; + d.s2n++; + d.s2v[0]++; + d.s3v[0]++; + d.s3n++; + d.s4nv[0]++; + d.s4sv[0] += 'd'; + + { + transaction t (db->begin ()); + db->update (r); + db->update (b); + db->update (d); + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr<root> pr (db->load<root> (r.id)); + auto_ptr<base> pb (db->load<base> (b.id)); + auto_ptr<derived> pd (db->load<derived> (d.id)); + + db->load (*pr, pr->s1); + db->load (*pr, pr->s2); + db->load (*pr, pr->s3); + db->load (*pr, pr->s4); + + db->load (*pb, pb->s1); + db->load (*pb, pb->s2); + db->load (*pb, pb->s3); + db->load (*pb, pb->s4); + + db->load (*pd, pd->s1); + db->load (*pd, pd->s2); + db->load (*pd, pd->s3); + db->load (*pd, pd->s4); + + assert (pr->s1n == r.s1n && + pr->s2n == r.s2n && + pr->s3v == r.s3v && + pr->s4nv == r.s4nv); + + assert (pb->s1n == b.s1n && + pb->s2n == b.s2n && + pb->s3v == b.s3v && + pb->s4nv == b.s4nv); + + assert (pd->s1n == d.s1n && pd->s1s == d.s1s && + pd->s2n == d.s2n && pd->s2v == d.s2v && + pd->s3v == d.s3v && pd->s3n == d.s3n && + pd->s4nv == d.s4nv && pd->s4sv == d.s4sv); + t.commit (); + } + + // Update section. + // + r.s1n++; + r.s2n++; + r.s3v[0]++; + r.s4nv[0]++; + + b.s1n++; + b.s2n++; + b.s3v[0]++; + b.s4nv[0]++; + + d.s1n++; + d.s1s += 'd'; + d.s2n++; + d.s2v[0]++; + d.s3v[0]++; + d.s3n++; + d.s4nv[0]++; + d.s4sv[0] += 'd'; + + { + transaction t (db->begin ()); + db->update (r, r.s1); + db->update (r, r.s2); + db->update (r, r.s3); + db->update (r, r.s4); + + db->update (b, b.s1); + db->update (b, b.s2); + db->update (b, b.s3); + db->update (b, b.s4); + + root& rr (d); + db->update (rr, rr.s1); + db->update (rr, rr.s2); + db->update (rr, rr.s3); + db->update (rr, rr.s4); + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr<root> pr (db->load<root> (r.id)); + auto_ptr<base> pb (db->load<base> (b.id)); + auto_ptr<derived> pd (db->load<derived> (d.id)); + + db->load (*pr, pr->s1); + db->load (*pr, pr->s2); + db->load (*pr, pr->s3); + db->load (*pr, pr->s4); + + db->load (*pb, pb->s1); + db->load (*pb, pb->s2); + db->load (*pb, pb->s3); + db->load (*pb, pb->s4); + + db->load (*pd, pd->s1); + db->load (*pd, pd->s2); + db->load (*pd, pd->s3); + db->load (*pd, pd->s4); + + assert (pr->s1n == r.s1n && + pr->s2n == r.s2n && + pr->s3v == r.s3v && + pr->s4nv == r.s4nv); + + assert (pb->s1n == b.s1n && + pb->s2n == b.s2n && + pb->s3v == b.s3v && + pb->s4nv == b.s4nv); + + assert (pd->s1n == d.s1n && pd->s1s == d.s1s && + pd->s2n == d.s2n && pd->s2v == d.s2v && + pd->s3v == d.s3v && pd->s3n == d.s3n && + pd->s4nv == d.s4nv && pd->s4sv == d.s4sv); + t.commit (); + } + + // Reload. + // + r.s1n++; + r.s2n++; + r.s3v[0]++; + r.s4nv[0]++; + + b.s1n++; + b.s2n++; + b.s3v[0]++; + b.s4nv[0]++; + + d.s1n++; + d.s1s += 'd'; + d.s2n++; + d.s2v[0]++; + d.s3v[0]++; + d.s3n++; + d.s4nv[0]++; + d.s4sv[0] += 'd'; + + { + transaction t (db->begin ()); + auto_ptr<root> pr (db->load<root> (r.id)); + auto_ptr<base> pb (db->load<base> (b.id)); + auto_ptr<derived> pd (db->load<derived> (d.id)); + + db->load (*pr, pr->s1); + db->load (*pr, pr->s2); + db->load (*pr, pr->s3); + db->load (*pr, pr->s4); + + db->load (*pb, pb->s1); + db->load (*pb, pb->s2); + db->load (*pb, pb->s3); + db->load (*pb, pb->s4); + + db->load (*pd, pd->s1); + db->load (*pd, pd->s2); + db->load (*pd, pd->s3); + db->load (*pd, pd->s4); + + db->update (r); + db->update (b); + db->update (d); + + db->reload (*pr); + db->reload (*pb); + db->reload (*pd); + + assert (pr->s1n == r.s1n && + pr->s2n == r.s2n && + pr->s3v == r.s3v && + pr->s4nv == r.s4nv); + + assert (pb->s1n == b.s1n && + pb->s2n == b.s2n && + pb->s3v == b.s3v && + pb->s4nv == b.s4nv); + + assert (pd->s1n == d.s1n && pd->s1s == d.s1s && + pd->s2n == d.s2n && pd->s2v == d.s2v && + pd->s3v == d.s3v && pd->s3n == d.s3n && + pd->s4nv == d.s4nv && pd->s4sv == d.s4sv); + t.commit (); + } + } + + // Test basic polymorphic optimistic section functionality. + // + { + using namespace test4; + + base b (123, "abc"); + derived d (234, "bcd", true); + + { + transaction t (db->begin ()); + db->persist (b); + db->persist (d); + t.commit (); + + assert (b.rs1.loaded ()); + assert (b.rs2.loaded ()); + assert (b.rs3.loaded ()); + assert (b.rs4.loaded ()); + assert (b.bs1.loaded ()); + + assert (d.rs1.loaded ()); + assert (d.rs2.loaded ()); + assert (d.rs3.loaded ()); + assert (d.rs4.loaded ()); + assert (d.bs1.loaded ()); + assert (d.ds1.loaded ()); + } + + { + transaction t (db->begin ()); + auto_ptr<base> pb (db->load<base> (b.id)); + auto_ptr<derived> pd (db->load<derived> (d.id)); + + assert (!pb->rs1.loaded ()); + assert (!pb->rs2.loaded ()); + assert (!pb->rs3.loaded ()); + assert (!pb->rs4.loaded ()); + assert (!pb->bs1.loaded ()); + + assert (!pd->rs1.loaded ()); + assert (!pd->rs2.loaded ()); + assert (!pd->rs3.loaded ()); + assert (!pd->rs4.loaded ()); + assert (!pd->bs1.loaded ()); + assert (!pd->ds1.loaded ()); + + assert (pb->rs1n != b.rs1n && pb->rs1s != b.rs1s && + pb->rs2n != b.rs2n && + pb->rs3n != b.rs3n && + pb->rs4n != b.rs4n && pb->rs4s != b.rs4s); + + assert (pd->rs1n != d.rs1n && pd->rs1s != d.rs1s && + pd->rs1b != d.rs1b && pd->rs1v != d.rs1v && + pd->rs2n != d.rs2n && + pd->rs3n != d.rs3n && pd->rs3s != d.rs3s && + pd->rs4n != d.rs4n && pd->rs4s != d.rs4s && + pd->rs4v != d.rs4v && + pd->bs1n != d.bs1n && + pd->ds1v != d.ds1v); + + db->load (*pb, pb->rs1); + db->load (*pb, pb->rs2); + db->load (*pb, pb->rs3); + db->load (*pb, pb->rs4); + db->load (*pb, pb->bs1); // No-op. + + root* pr (pd.get ()); + db->load (*pr, pr->rs1); // Via base. + db->load (*pd, pd->rs2); + db->load (*pd, pd->rs3); + db->load (*pr, pr->rs4); // Via base. + db->load (*pd, pd->bs1); + db->load (*pd, pd->ds1); + + assert (pb->rs1.loaded ()); + assert (pb->rs2.loaded ()); + assert (pb->rs3.loaded ()); + assert (pb->rs4.loaded ()); + assert (pb->bs1.loaded ()); + + assert (pd->rs1.loaded ()); + assert (pd->rs2.loaded ()); + assert (pd->rs3.loaded ()); + assert (pd->rs4.loaded ()); + assert (pd->bs1.loaded ()); + assert (pd->ds1.loaded ()); + + assert (pb->rs1n == b.rs1n && pb->rs1s == b.rs1s && + pb->rs2n == b.rs2n && + pb->rs3n == b.rs3n && + pb->rs4n == b.rs4n && pb->rs4s == b.rs4s); + + assert (pd->rs1n == d.rs1n && pd->rs1s == d.rs1s && + pd->rs1b == d.rs1b && pd->rs1v == d.rs1v && + pd->rs2n == d.rs2n && + pd->rs3n == d.rs3n && pd->rs3s == d.rs3s && + pd->rs4n == d.rs4n && pd->rs4s == d.rs4s && + pd->rs4v == d.rs4v && + pd->bs1n == d.bs1n && + pd->ds1v == d.ds1v); + t.commit (); + } + + base b1 (b); + derived d1 (d); + + // Update object. + // + for (unsigned short s (1); s < 7; ++s) + { + b1.rs1n++; + b1.rs1s += 'd'; + b1.rs1.change (); + b1.rs4s += 'd'; + + d1.rs1n++; + d1.rs1s += 'e'; + d1.rs1b = !d.rs1b; + d1.rs1v[0]++; + d1.rs1.change (); + d1.rs4s += 'e'; + d1.rs4v[0]++; + d1.bs1n++; + d1.ds1v[0]++; + + transaction t (db->begin ()); + auto_ptr<base> pb (db->load<base> (b.id)); + auto_ptr<derived> pd (db->load<derived> (d.id)); + + db->update (b1); + db->update (d1); + + assert (!b1.rs1.changed ()); + assert (!d1.rs1.changed ()); + + assert (b.v != b1.v); + assert (d.v != d1.v); + + try + { + bool a (false); + switch (s) + { + case 1: db->load (*pb, pb->rs1); break; + case 2: db->load (*pb, pb->rs2); break; + case 3: db->load (*pb, pb->rs3); break; + case 4: db->load (*pb, pb->rs4); break; + case 5: + case 6: a = true; break; // No-op. + default: break; + } + assert (a); + } + catch (const object_changed&) + { + db->reload (*pb); + + assert (!pb->rs1.loaded ()); + assert (!pb->rs2.loaded ()); + assert (!pb->rs3.loaded ()); + assert (!pb->rs3.loaded ()); + assert (!pb->bs1.loaded ()); + + db->load (*pb, pb->rs1); + db->load (*pb, pb->rs2); + db->load (*pb, pb->rs3); + db->load (*pb, pb->rs4); + db->load (*pb, pb->bs1); // No-op. + + assert (pb->rs1n == b1.rs1n && pb->rs1s == b1.rs1s && + pb->rs2n == b1.rs2n && + pb->rs3n == b1.rs3n && + pb->rs4n == b1.rs4n && pb->rs4s == b1.rs4s); + } + + try + { + switch (s) + { + case 1: db->load (*pd, pd->rs1); break; + case 2: db->load (*pd, pd->rs2); break; + case 3: db->load (*pd, pd->rs3); break; + case 4: db->load (*pd, pd->rs4); break; + case 5: db->load (*pd, pd->bs1); break; + case 6: db->load (*pd, pd->ds1); break; + default: break; + } + assert (false); + } + catch (const object_changed&) + { + db->reload (*pd); + + assert (!pd->rs1.loaded ()); + assert (!pd->rs2.loaded ()); + assert (!pd->rs3.loaded ()); + assert (!pd->rs4.loaded ()); + assert (!pd->bs1.loaded ()); + assert (!pd->ds1.loaded ()); + + db->load (*pd, pd->rs1); + db->load (*pd, pd->rs2); + db->load (*pd, pd->rs3); + db->load (*pd, pd->rs4); + db->load (*pd, pd->bs1); + db->load (*pd, pd->ds1); + + assert (pd->rs1n == d1.rs1n && pd->rs1s == d1.rs1s && + pd->rs1b == d1.rs1b && pd->rs1v == d1.rs1v && + pd->rs2n == d1.rs2n && + pd->rs3n == d1.rs3n && pd->rs3s == d1.rs3s && + pd->rs4n == d1.rs4n && pd->rs4s == d1.rs4s && + pd->rs4v == d1.rs4v && + pd->bs1n == d1.bs1n && + pd->ds1v == d1.ds1v); + } + + db->reload (b); + db->reload (d); + + assert (b.v == b1.v); + assert (d.v == d1.v); + + assert (b.rs1n == b1.rs1n && b.rs1s == b1.rs1s && + b.rs2n == b1.rs2n && + b.rs3n == b1.rs3n && + b.rs4n == b1.rs4n && b.rs4s == b1.rs4s); + + assert (d.rs1n == d1.rs1n && d.rs1s == d1.rs1s && + d.rs1b == d1.rs1b && d.rs1v == d1.rs1v && + d.rs2n == d1.rs2n && + d.rs3n == d1.rs3n && d.rs3s == d1.rs3s && + d.rs4n == d1.rs4n && d.rs4s == d1.rs4s && + d.rs4v == d1.rs4v && + d.bs1n == d1.bs1n && + d.ds1v == d1.ds1v); + + t.commit (); + } + + // Update section. + // + for (unsigned short s (1); s < 7; ++s) + { + transaction t (db->begin ()); + auto_ptr<base> pb (db->load<base> (b.id)); + auto_ptr<derived> pd (db->load<derived> (d.id)); + + switch (s) + { + case 1: + b1.rs1n++; + b1.rs1s += 'd'; + + d1.rs1n++; + d1.rs1s += 'e'; + d1.rs1b = !d.rs1b; + d1.rs1v[0]++; + + db->update (b1, b1.rs1); + db->update (d1, d1.rs1); + + assert (b.v != b1.v); + assert (d.v != d1.v); + break; + case 2: + db->update (b1, b1.rs2); // No-op. + db->update (d1, d1.rs2); // No-op. + + assert (b.v == b1.v); + assert (d.v == d1.v); + continue; // Object hasn't changed. + case 3: + db->update (b1, b1.rs3); // No-op. + db->update (d1, d1.rs3); // No-op. + + assert (b.v == b1.v); + assert (d.v == d1.v); + continue; // Object hasn't changed. + case 4: + b1.rs4s += 'd'; + + d1.rs4s += 'e'; + d1.rs4v[0]++; + + db->update (b1, b1.rs4); + db->update (d1, d1.rs4); + + assert (b.v != b1.v); + assert (d.v != d1.v); + break; + case 5: + d1.bs1n++; + + db->update (b1, b1.bs1); // No-op. + db->update (d1, d1.bs1); + + assert (b.v == b1.v); + assert (d.v != d1.v); + break; + case 6: + d1.ds1v[0]++; + + db->update (d1, d1.ds1); + + assert (d.v != d1.v); + break; + default: break; + } + + try + { + bool a (false); + switch (s) + { + case 1: db->load (*pb, pb->rs1); break; + case 4: db->load (*pb, pb->rs4); break; + case 5: + case 6: a = true; break; // No-op. + default: break; + } + assert (a); + } + catch (const object_changed&) + { + db->reload (*pb); + + assert (!pb->rs1.loaded ()); + assert (!pb->rs2.loaded ()); + assert (!pb->rs3.loaded ()); + assert (!pb->rs4.loaded ()); + assert (!pb->bs1.loaded ()); + + db->load (*pb, pb->rs1); + db->load (*pb, pb->rs2); + db->load (*pb, pb->rs3); + db->load (*pb, pb->rs4); + db->load (*pb, pb->bs1); // No-op. + + assert (pb->rs1n == b1.rs1n && pb->rs1s == b1.rs1s && + pb->rs2n == b1.rs2n && + pb->rs3n == b1.rs3n && + pb->rs4n == b1.rs4n && pb->rs4s == b1.rs4s); + } + + try + { + switch (s) + { + case 1: db->load (*pd, pd->rs1); break; + case 4: db->load (*pd, pd->rs4); break; + case 5: db->load (*pd, pd->bs1); break; + case 6: db->load (*pd, pd->ds1); break; + default: break; + } + assert (false); + } + catch (const object_changed&) + { + db->reload (*pd); + + assert (!pd->rs1.loaded ()); + assert (!pd->rs2.loaded ()); + assert (!pd->rs3.loaded ()); + assert (!pd->rs4.loaded ()); + assert (!pd->bs1.loaded ()); + assert (!pd->ds1.loaded ()); + + db->load (*pd, pd->rs1); + db->load (*pd, pd->rs2); + db->load (*pd, pd->rs3); + db->load (*pd, pd->rs4); + db->load (*pd, pd->bs1); + db->load (*pd, pd->ds1); + + assert (pd->rs1n == d1.rs1n && pd->rs1s == d1.rs1s && + pd->rs1b == d1.rs1b && pd->rs1v == d1.rs1v && + pd->rs2n == d1.rs2n && + pd->rs3n == d1.rs3n && pd->rs3s == d1.rs3s && + pd->rs4n == d1.rs4n && pd->rs4s == d1.rs4s && + pd->rs4v == d1.rs4v && + pd->bs1n == d1.bs1n && + pd->ds1v == d1.ds1v); + } + + db->reload (b); + db->reload (d); + + assert (b.v == b1.v); + assert (d.v == d1.v); + + assert (b.rs1n == b1.rs1n && b.rs1s == b1.rs1s && + b.rs2n == b1.rs2n && + b.rs3n == b1.rs3n && + b.rs4n == b1.rs4n && b.rs4s == b1.rs4s); + + assert (d.rs1n == d1.rs1n && d.rs1s == d1.rs1s && + d.rs1b == d1.rs1b && d.rs1v == d1.rs1v && + d.rs2n == d1.rs2n && + d.rs3n == d1.rs3n && d.rs3s == d1.rs3s && + d.rs4n == d1.rs4n && d.rs4s == d1.rs4s && + d.rs4v == d1.rs4v && + d.bs1n == d1.bs1n && + d.ds1v == d1.ds1v); + + t.commit (); + } + + // Update changed section. + // + for (unsigned short s (1); s < 7; ++s) + { + if (s == 2 || s == 3) // Readonly sections. + continue; + + transaction t (db->begin ()); + + switch (s) + { + case 1: + b1.rs1n++; + b1.rs1s += 'd'; + + d1.rs1n++; + d1.rs1s += 'e'; + d1.rs1b = !d.rs1b; + d1.rs1v[0]++; + + db->update (b1, b1.rs1); + db->update (d1, d1.rs1); + break; + case 4: + b1.rs4s += 'd'; + + d1.rs4s += 'e'; + d1.rs4v[0]++; + + db->update (b1, b1.rs4); + db->update (d1, d1.rs4); + break; + case 5: + d1.bs1n++; + + db->update (b1, b1.bs1); // No-op. + db->update (d1, d1.bs1); + break; + case 6: + d1.ds1v[0]++; + + db->update (d1, d1.bs1); + break; + default: break; + } + + try + { + bool a (false); + switch (s) + { + case 1: db->update (b, b.rs1); break; + case 4: db->update (b, b.rs4); break; + case 5: + case 6: a = true; break; // No-op. + default: break; + } + assert (a); + } + catch (const object_changed&) + { + db->reload (b); + + switch (s) + { + case 1: db->update (b, b.rs1); break; + case 4: db->update (b, b.rs4); break; + default: break; + } + } + + try + { + switch (s) + { + case 1: db->update (d, d.rs1); break; + case 4: db->update (d, d.rs4); break; + case 5: db->update (d, d.bs1); break; + case 6: db->update (d, d.ds1); break; + default: break; + } + assert (false); + } + catch (const object_changed&) + { + db->reload (d); + + switch (s) + { + case 1: db->update (d, d.rs1); break; + case 4: db->update (d, d.rs4); break; + case 5: db->update (d, d.bs1); break; + case 6: db->update (d, d.ds1); break; + default: break; + } + } + + db->reload (b1); + db->reload (d1); + + t.commit (); + } + } + + // Test polymorphic optimistic readonly/empty to readwrite section + // override. + // + { + using namespace test5; + + base b; + derived d (123); + + { + transaction t (db->begin ()); + db->persist (b); + db->persist (d); + t.commit (); + + assert (b.s.loaded ()); + assert (d.s.loaded ()); + } + + { + transaction t (db->begin ()); + auto_ptr<base> pb (db->load<base> (b.id)); + auto_ptr<derived> pd (db->load<derived> (d.id)); + + assert (!pb->s.loaded ()); + assert (!pd->s.loaded ()); + assert (pd->sn != d.sn); + + db->load (*pb, pb->s); // No-op. + db->load (*pd, pd->s); + + assert (pb->s.loaded ()); + assert (pd->s.loaded ()); + + assert (pd->sn == d.sn); + + t.commit (); + } + + // Update object. + // + base b1 (b); + derived d1 (d); + d1.sn++; + d1.s.change (); + + { + transaction t (db->begin ()); + auto_ptr<derived> pd (db->load<derived> (d.id)); + + db->update (d1); + + assert (!d1.s.changed ()); + assert (d.v != d1.v); + + try + { + db->load (*pd, pd->s); + assert (false); + } + catch (const object_changed&) + { + db->reload (*pd); + assert (!pd->s.loaded ()); + db->load (*pd, pd->s); + assert (pd->sn == d1.sn); + } + + db->reload (d); + assert (d.v == d1.v); + assert (d.sn == d1.sn); + t.commit (); + } + + // Update section. + // + d1.sn++; + + { + transaction t (db->begin ()); + auto_ptr<derived> pd (db->load<derived> (d.id)); + + db->update (b1, b1.s); // No-op. + db->update (d1, d1.s); + + assert (b.v == b1.v); + assert (d.v != d1.v); + + try + { + db->load (*pd, pd->s); + assert (false); + } + catch (const object_changed&) + { + db->reload (*pd); + assert (!pd->s.loaded ()); + db->load (*pd, pd->s); + + assert (pd->sn == d1.sn); + } + + db->reload (d); + assert (d.v == d1.v); + assert (d.sn == d1.sn); + t.commit (); + } + + // Update changed section. + // + d1.sn++; + + { + transaction t (db->begin ()); + db->update (d1, d1.s); + + try + { + db->update (d, d.s); + assert (false); + } + catch (const object_changed&) + { + db->reload (d); + db->update (d, d.s); + } + + t.commit (); + } + } + + // Test polymorphic optimistic readonly/empty to readwrite section + // override, eager-loaded case. + // + { + using namespace test6; + + derived d (123); + + { + transaction t (db->begin ()); + db->persist (d); + t.commit (); + + assert (d.s.loaded ()); + } + + { + transaction t (db->begin ()); + auto_ptr<derived> pd (db->load<derived> (d.id)); + + assert (pd->s.loaded ()); + assert (pd->sn == d.sn); + + t.commit (); + } + + // Update object. + // + derived d1 (d); + d1.sn++; + d1.s.change (); + + { + transaction t (db->begin ()); + + db->update (d1); + + assert (!d1.s.changed ()); + assert (d.v != d1.v); + + db->reload (d); + assert (d.v == d1.v); + assert (d.sn == d1.sn); + t.commit (); + } + + // Update section. + // + d1.sn++; + + { + transaction t (db->begin ()); + + db->update (d1, d1.s); + assert (d.v != d1.v); + + db->reload (d); + assert (d.v == d1.v); + assert (d.sn == d1.sn); + t.commit (); + } + + // Update changed section. + // + d1.sn++; + + { + transaction t (db->begin ()); + db->update (d1, d1.s); + + try + { + db->update (d, d.s); + assert (false); + } + catch (const object_changed&) + { + db->reload (d); + db->update (d, d.s); + } + + t.commit (); + } + } + + // Test polymorphic optimistic section added in derived. + // + { + using namespace test7; + + base b; + derived d (123); + + { + transaction t (db->begin ()); + db->persist (b); + db->persist (d); + t.commit (); + + assert (b.s.loaded ()); + assert (d.s.loaded ()); + } + + { + transaction t (db->begin ()); + auto_ptr<base> pb (db->load<base> (b.id)); + auto_ptr<derived> pd (db->load<derived> (d.id)); + + assert (!pb->s.loaded ()); + assert (!pd->s.loaded ()); + assert (pd->sn != d.sn); + + db->load (*pb, pb->s); // No-op. + db->load (*pd, pd->s); + + assert (pb->s.loaded ()); + assert (pd->s.loaded ()); + + assert (pd->sn == d.sn); + + t.commit (); + } + + // Update object. + // + base b1 (b); + derived d1 (d); + d1.sn++; + d1.s.change (); + + { + transaction t (db->begin ()); + auto_ptr<derived> pd (db->load<derived> (d.id)); + + db->update (d1); + + assert (!d1.s.changed ()); + assert (d.v != d1.v); + + try + { + db->load (*pd, pd->s); + assert (false); + } + catch (const object_changed&) + { + db->reload (*pd); + assert (!pd->s.loaded ()); + db->load (*pd, pd->s); + assert (pd->sn == d1.sn); + } + + db->reload (d); + assert (d.v == d1.v); + assert (d.sn == d1.sn); + t.commit (); + } + + // Update section. + // + d1.sn++; + + { + transaction t (db->begin ()); + auto_ptr<derived> pd (db->load<derived> (d.id)); + + db->update (b1, b1.s); // No-op. + db->update (d1, d1.s); + + assert (b.v == b1.v); + assert (d.v != d1.v); + + try + { + db->load (*pd, pd->s); + assert (false); + } + catch (const object_changed&) + { + db->reload (*pd); + assert (!pd->s.loaded ()); + db->load (*pd, pd->s); + + assert (pd->sn == d1.sn); + } + + db->reload (d); + assert (d.v == d1.v); + assert (d.sn == d1.sn); + t.commit (); + } + + // Update changed section. + // + d1.sn++; + + { + transaction t (db->begin ()); + db->update (d1, d1.s); + + try + { + db->update (d, d.s); + assert (false); + } + catch (const object_changed&) + { + db->reload (d); + db->update (d, d.s); + } + + t.commit (); + } + } + + // Test reuse/polymorphic inheritance and optimistic mix. + // + { + using namespace test8; + + derived d (123); + + { + transaction t (db->begin ()); + db->persist (d); + t.commit (); + + assert (d.s.loaded ()); + } + + { + transaction t (db->begin ()); + auto_ptr<derived> pd (db->load<derived> (d.id)); + + assert (!pd->s.loaded ()); + assert (pd->sn != d.sn); + + db->load (*pd, pd->s); + + assert (pd->s.loaded ()); + assert (pd->sn == d.sn); + + t.commit (); + } + + // Update object. + // + derived d1 (d); + d1.sn++; + d1.s.change (); + + { + transaction t (db->begin ()); + auto_ptr<derived> pd (db->load<derived> (d.id)); + + db->update (d1); + + assert (!d1.s.changed ()); + assert (d.v != d1.v); + + try + { + db->load (*pd, pd->s); + assert (false); + } + catch (const object_changed&) + { + db->reload (*pd); + assert (!pd->s.loaded ()); + db->load (*pd, pd->s); + assert (pd->sn == d1.sn); + } + + db->reload (d); + assert (d.v == d1.v); + assert (d.sn == d1.sn); + t.commit (); + } + + // Update section. + // + d1.sn++; + + { + transaction t (db->begin ()); + auto_ptr<derived> pd (db->load<derived> (d.id)); + + db->update (d1, d1.s); + assert (d.v != d1.v); + + try + { + db->load (*pd, pd->s); + assert (false); + } + catch (const object_changed&) + { + db->reload (*pd); + assert (!pd->s.loaded ()); + db->load (*pd, pd->s); + + assert (pd->sn == d1.sn); + } + + db->reload (d); + assert (d.v == d1.v); + assert (d.sn == d1.sn); + t.commit (); + } + + // Update changed section. + // + d1.sn++; + + { + transaction t (db->begin ()); + db->update (d1, d1.s); + + try + { + db->update (d, d.s); + assert (false); + } + catch (const object_changed&) + { + db->reload (d); + db->update (d, d.s); + } + + t.commit (); + } + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/common/section/polymorphism/makefile b/common/section/polymorphism/makefile new file mode 100644 index 0000000..deb13dc --- /dev/null +++ b/common/section/polymorphism/makefile @@ -0,0 +1,115 @@ +# file : common/section/polymorphism/makefile +# copyright : Copyright (c) 2009-2013 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 +genf := $(call odb-gen,$(odb_hdr)) +gen := $(addprefix $(out_base)/,$(genf)) +cxx_obj := $(addprefix $(out_base)/,$(cxx_tun:.cxx=.o)) $(filter %.o,$(gen:.cxx=.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 + +# 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) + +$(gen): $(odb) +$(gen): odb := $(odb) +$(gen) $(dist): export odb_options += --generate-schema --generate-query \ +--table-prefix t_section_p_ +$(gen): cpp_options := -I$(src_base) +$(gen): $(common.l.cpp-options) + +ifneq ($(db_id),common) +$(gen): odb_options += --database $(db_id) +else +$(gen): odb_options += --multi-database dynamic +endif + +$(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): sources := $(cxx_tun) +$(dist): headers := $(odb_hdr) +$(dist): data_dist := test.std +$(dist): export name := $(name) +$(dist): export extra_dist := $(data_dist) $(call vc8projs,$(name)) \ +$(call vc9projs,$(name)) $(call vc10projs,$(name)) $(call vc11projs,$(name)) +$(dist): + $(call dist-data,$(sources) $(headers) $(data_dist)) + $(call meta-automake,../../template/Makefile.am) + $(call meta-vc8projs,../../template/template,$(name)) + $(call meta-vc9projs,../../template/template,$(name)) + $(call meta-vc10projs,../../template/template,$(name)) + $(call meta-vc11projs,../../template/template,$(name)) + +# Test. +# +ifneq ($(db_id),common) +$(eval $(call test-rule)) +else +$(foreach d,$(databases),$(eval $(call test-rule,$d))) +endif + +# Clean. +# +$(clean): \ + $(driver).o.clean \ + $(addsuffix .cxx.clean,$(cxx_obj)) \ + $(addsuffix .cxx.clean,$(cxx_od)) \ + $(addsuffix .hxx.clean,$(filter %.cxx,$(gen))) + $(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/vc8proj.make) +$(call include,$(bld_root)/meta/vc9proj.make) +$(call include,$(bld_root)/meta/vc10proj.make) +$(call include,$(bld_root)/meta/vc11proj.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/section/polymorphism/test.hxx b/common/section/polymorphism/test.hxx new file mode 100644 index 0000000..f2081c2 --- /dev/null +++ b/common/section/polymorphism/test.hxx @@ -0,0 +1,495 @@ +// file : common/section/polymorphism/test.hxx +// copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include <string> +#include <vector> + +#include <odb/core.hxx> +#include <odb/section.hxx> + +// Test basic polymorphic section functionality. +// +#pragma db namespace table("t1_") +namespace test1 +{ + #pragma db object polymorphic abstract + struct root + { + root (int n): rs1n (n), rs2n (n), rs4n (n) {rs2v.push_back (n);} + virtual ~root () {} + + #pragma db id auto + unsigned long id; + + // rs1: override in base and derived + // + #pragma db load(lazy) update(change) + odb::section rs1; + + #pragma db section(rs1) + int rs1n; + + // rs2: no override + // + #pragma db load(lazy) + odb::section rs2; + + #pragma db section(rs2) + int rs2n; + + #pragma db section(rs2) + std::vector<int> rs2v; + + // rs3: empty + // + #pragma db load(lazy) + odb::section rs3; + + // rs4: override "gap" + // + #pragma db load(lazy) + odb::section rs4; + + #pragma db section(rs4) + int rs4n; + }; + + #pragma db object + struct base: root + { + base (int n = 999, const std::string& s = "xxx") + : root (n), rs1s (s), bs1n (n) {rs3v.push_back (n);} + + // rs1 + // + #pragma db section(rs1) + std::string rs1s; + + // rs3 + // + #pragma db section(rs3) + std::vector<int> rs3v; + + // bs1: override in derived + // + #pragma db load(lazy) + odb::section bs1; + + #pragma db section(bs1) + int bs1n; + }; + + #pragma db object + struct derived: base + { + derived (int n = 999, const std::string& s = "xxx", bool b = false) + : base (n, s), rs1b (b), rs3n (n), rs4s (s), bs1s (s), ds1n (n) + {rs1v.push_back (n);} + + // rs1 + // + #pragma db section(rs1) + bool rs1b; + + #pragma db section(rs1) + std::vector<int> rs1v; + + // rs3 + // + #pragma db section(rs3) + int rs3n; + + // rs4 + // + #pragma db section(rs4) + std::string rs4s; + + // bs1 + // + #pragma db section(bs1) + std::string bs1s; + + // ds1: no override + // + #pragma db load(lazy) + odb::section ds1; + + #pragma db section(ds1) + int ds1n; + }; +} + +// Test empty section and override "gap". +// +#pragma db namespace table("t2_") +namespace test2 +{ + #pragma db object polymorphic abstract + struct root + { + virtual ~root () {} + + #pragma db id auto + unsigned long id; + + #pragma db load(lazy) + odb::section s; + }; + + #pragma db object abstract + struct base: root + { + // The "gap". + }; + + #pragma db object + struct derived: base + { + derived (int n = 999): sn (n) {sv.push_back (n);} + + #pragma db section(s) + int sn; + + #pragma db section(s) + std::vector<int> sv; + }; +} + +// Test value-only/container-only base/override combinations. +// +#pragma db namespace table("t3_") +namespace test3 +{ + #pragma db object polymorphic + struct root + { + root (int n = 999) + : s1n (n), s2n (n) {s3v.push_back (n); s4nv.push_back (n);} + virtual ~root () {} + + #pragma db id auto + unsigned long id; + + // value/value + // + #pragma db load(lazy) + odb::section s1; + + #pragma db section(s1) + int s1n; + + // value/container + // + #pragma db load(lazy) + odb::section s2; + + #pragma db section(s2) + int s2n; + + // container/value + // + #pragma db load(lazy) + odb::section s3; + + #pragma db section(s3) + std::vector<int> s3v; + + // container/container + // + #pragma db load(lazy) + odb::section s4; + + #pragma db section(s4) + std::vector<int> s4nv; + }; + + #pragma db object + struct base: root + { + base (int n = 999): root (n) {} + + // The "gap". + }; + + #pragma db object + struct derived: base + { + derived (int n = 999, const std::string& s = "xxx") + : base (n), s1s (s), s3n (n) {s2v.push_back (n); s4sv.push_back (s);} + + #pragma db section(s1) + std::string s1s; + + #pragma db section(s2) + std::vector<int> s2v; + + #pragma db section(s3) + int s3n; + + #pragma db section(s4) + std::vector<std::string> s4sv; + }; +} + +// Test basic polymorphic optimistic section functionality. +// +#pragma db namespace table("t4_") +namespace test4 +{ + #pragma db object polymorphic optimistic abstract sectionable + struct root + { + root (int n): rs1n (n), rs2n (n), rs3n (n), rs4n (n) {} + virtual ~root () {} + + #pragma db id auto + unsigned long id; + + #pragma db version + unsigned long long v; + + // rs1: readwrite, override + // + #pragma db load(lazy) update(change) + odb::section rs1; + + #pragma db section(rs1) + int rs1n; + + // rs2: readonly, no override + // + #pragma db load(lazy) + odb::section rs2; + + #pragma db section(rs2) + const int rs2n; + + // rs3: readonly, readonly override + // + #pragma db load(lazy) + odb::section rs3; + + #pragma db section(rs3) + const int rs3n; + + // rs4: readonly, readwrite override + // + #pragma db load(lazy) + odb::section rs4; + + #pragma db section(rs4) + const int rs4n; + }; + + #pragma db object + struct base: root + { + base (int n = 999, const std::string& s = "xxx") + : root (n), rs1s (s), rs4s (s) {} + + // rs1 + // + #pragma db section(rs1) + std::string rs1s; + + // rs4 + // + #pragma db section(rs4) + std::string rs4s; + + // bs2: empty, readwrite override + // + #pragma db load(lazy) + odb::section bs1; + }; + + #pragma db object + struct derived: base + { + derived (int n = 999, const std::string& s = "xxx", bool b = false) + : base (n, s), rs1b (b), rs3s (s), bs1n (n) + { + rs1v.push_back (n); + rs4v.push_back (n); + ds1v.push_back (n); + } + + // rs1 + // + #pragma db section(rs1) + bool rs1b; + + #pragma db section(rs1) + std::vector<int> rs1v; + + // rs3 + // + #pragma db section(rs3) + const std::string rs3s; + + // rs4 + // + #pragma db section(rs4) + std::vector<int> rs4v; + + // bs1 + // + #pragma db section(bs1) + int bs1n; + + // ds1: readwrite + // + #pragma db load(lazy) + odb::section ds1; + + #pragma db section(ds1) + std::vector<int> ds1v; + }; +} + +// Test polymorphic optimistic readonly/empty to readwrite section override. +// +#pragma db namespace table("t5_") +namespace test5 +{ + #pragma db object polymorphic optimistic abstract + struct root + { + virtual ~root () {} + + #pragma db id auto + unsigned long id; + + #pragma db version + unsigned long long v; + + #pragma db load(lazy) update(change) + odb::section s; + }; + + #pragma db object + struct base: root + { + // The "gap". + }; + + #pragma db object + struct derived: base + { + derived (int n = 999): sn (n) {} + + #pragma db section(s) + int sn; + }; +} + +// Test polymorphic optimistic readonly/empty to readwrite section override, +// eager-loaded case. +// +#pragma db namespace table("t6_") +namespace test6 +{ + #pragma db object polymorphic optimistic abstract + struct root + { + virtual ~root () {} + + #pragma db id auto + unsigned long id; + + #pragma db version + unsigned long long v; + + #pragma db update(change) + odb::section s; + }; + + #pragma db object abstract + struct base: root + { + // The "gap". + }; + + #pragma db object + struct derived: base + { + derived (int n = 999): sn (n) {} + + #pragma db section(s) + int sn; + }; +} + +// Test polymorphic optimistic section added in derived. +// +#pragma db namespace table("t7_") +namespace test7 +{ + #pragma db object polymorphic optimistic sectionable + struct root + { + virtual ~root () {} + + #pragma db id auto + unsigned long id; + + #pragma db version + unsigned long long v; + }; + + #pragma db object + struct base: root + { + #pragma db load(lazy) update(change) + odb::section s; + }; + + #pragma db object + struct derived: base + { + derived (int n = 999): sn (n) {} + + #pragma db section(s) + int sn; + }; +} + +// Test reuse/polymorphic inheritance and optimistic mix. +// +#pragma db namespace table("t8_") +namespace test8 +{ + #pragma db object optimistic sectionable abstract + struct root + { + #pragma db id auto + unsigned long id; + + #pragma db version + unsigned long long v; + }; + + #pragma db object polymorphic sectionable + struct base: root + { + virtual ~base () {} + }; + + #pragma db object + struct derived: base + { + derived (int n = 999): sn (n) {} + + #pragma db load(lazy) update(change) + odb::section s; + + #pragma db section(s) + int sn; + }; +} + +#endif // TEST_HXX diff --git a/common/section/polymorphism/test.std b/common/section/polymorphism/test.std new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/common/section/polymorphism/test.std |