From 6b76715e63d2c265a4c51c73f9019bc578f874cb Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 8 Oct 2012 14:47:57 +0200 Subject: Fix bug in transient inheritance handling Also test that we can handle it for objects, composite values, and views. --- common/inheritance/driver.cxx | 182 --- common/inheritance/makefile | 115 -- common/inheritance/polymorphism/driver.cxx | 1880 ++++++++++++++++++++++++++++ common/inheritance/polymorphism/makefile | 118 ++ common/inheritance/polymorphism/test.std | 36 + common/inheritance/polymorphism/test1.hxx | 116 ++ common/inheritance/polymorphism/test10.hxx | 79 ++ common/inheritance/polymorphism/test11.hxx | 79 ++ common/inheritance/polymorphism/test12.hxx | 80 ++ common/inheritance/polymorphism/test2.hxx | 106 ++ common/inheritance/polymorphism/test3.hxx | 147 +++ common/inheritance/polymorphism/test4.hxx | 76 ++ common/inheritance/polymorphism/test5.hxx | 93 ++ common/inheritance/polymorphism/test6.hxx | 65 + common/inheritance/polymorphism/test7.hxx | 67 + common/inheritance/polymorphism/test8.hxx | 130 ++ common/inheritance/polymorphism/test9.hxx | 162 +++ common/inheritance/reuse/driver.cxx | 182 +++ common/inheritance/reuse/makefile | 115 ++ common/inheritance/reuse/test.hxx | 158 +++ common/inheritance/reuse/test.std | 0 common/inheritance/test.hxx | 158 --- common/inheritance/test.std | 0 common/inheritance/transient/driver.cxx | 79 ++ common/inheritance/transient/makefile | 115 ++ common/inheritance/transient/test.hxx | 61 + common/inheritance/transient/test.std | 0 common/makefile | 77 +- common/polymorphism/driver.cxx | 1880 ---------------------------- common/polymorphism/makefile | 118 -- common/polymorphism/test.std | 36 - common/polymorphism/test1.hxx | 116 -- common/polymorphism/test10.hxx | 79 -- common/polymorphism/test11.hxx | 79 -- common/polymorphism/test12.hxx | 80 -- common/polymorphism/test2.hxx | 106 -- common/polymorphism/test3.hxx | 147 --- common/polymorphism/test4.hxx | 76 -- common/polymorphism/test5.hxx | 93 -- common/polymorphism/test6.hxx | 65 - common/polymorphism/test7.hxx | 67 - common/polymorphism/test8.hxx | 130 -- common/polymorphism/test9.hxx | 162 --- 43 files changed, 3983 insertions(+), 3727 deletions(-) delete mode 100644 common/inheritance/driver.cxx delete mode 100644 common/inheritance/makefile create mode 100644 common/inheritance/polymorphism/driver.cxx create mode 100644 common/inheritance/polymorphism/makefile create mode 100644 common/inheritance/polymorphism/test.std create mode 100644 common/inheritance/polymorphism/test1.hxx create mode 100644 common/inheritance/polymorphism/test10.hxx create mode 100644 common/inheritance/polymorphism/test11.hxx create mode 100644 common/inheritance/polymorphism/test12.hxx create mode 100644 common/inheritance/polymorphism/test2.hxx create mode 100644 common/inheritance/polymorphism/test3.hxx create mode 100644 common/inheritance/polymorphism/test4.hxx create mode 100644 common/inheritance/polymorphism/test5.hxx create mode 100644 common/inheritance/polymorphism/test6.hxx create mode 100644 common/inheritance/polymorphism/test7.hxx create mode 100644 common/inheritance/polymorphism/test8.hxx create mode 100644 common/inheritance/polymorphism/test9.hxx create mode 100644 common/inheritance/reuse/driver.cxx create mode 100644 common/inheritance/reuse/makefile create mode 100644 common/inheritance/reuse/test.hxx create mode 100644 common/inheritance/reuse/test.std delete mode 100644 common/inheritance/test.hxx delete mode 100644 common/inheritance/test.std create mode 100644 common/inheritance/transient/driver.cxx create mode 100644 common/inheritance/transient/makefile create mode 100644 common/inheritance/transient/test.hxx create mode 100644 common/inheritance/transient/test.std delete mode 100644 common/polymorphism/driver.cxx delete mode 100644 common/polymorphism/makefile delete mode 100644 common/polymorphism/test.std delete mode 100644 common/polymorphism/test1.hxx delete mode 100644 common/polymorphism/test10.hxx delete mode 100644 common/polymorphism/test11.hxx delete mode 100644 common/polymorphism/test12.hxx delete mode 100644 common/polymorphism/test2.hxx delete mode 100644 common/polymorphism/test3.hxx delete mode 100644 common/polymorphism/test4.hxx delete mode 100644 common/polymorphism/test5.hxx delete mode 100644 common/polymorphism/test6.hxx delete mode 100644 common/polymorphism/test7.hxx delete mode 100644 common/polymorphism/test8.hxx delete mode 100644 common/polymorphism/test9.hxx (limited to 'common') diff --git a/common/inheritance/driver.cxx b/common/inheritance/driver.cxx deleted file mode 100644 index ba907ce..0000000 --- a/common/inheritance/driver.cxx +++ /dev/null @@ -1,182 +0,0 @@ -// file : common/inheritance/driver.cxx -// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC -// license : GNU GPL v2; see accompanying LICENSE file - -// Test 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; - -int -main (int argc, char* argv[]) -{ - try - { - auto_ptr db (create_database (argc, argv)); - - base b; - b.comp_.bools.push_back (true); - b.comp_.num = 10; - b.comp_.str = "comp bbb"; - b.comp_.nums.push_back (101); - b.comp_.nums.push_back (102); - b.num_ = 0; - b.str_ = "bbb"; - b.strs_.push_back ("bbb one"); - b.strs_.push_back ("bbb two"); - - object1 o1; - o1.comp_.bools.push_back (false); - o1.comp_.num = 11; - o1.comp_.str = "comp o1o1o1"; - o1.comp_.nums.push_back (111); - o1.comp_.nums.push_back (112); - static_cast (o1).num_ = 1; - o1.num1_ = 21; - o1.str_ = "base o1o1o1"; - o1.strs_.push_back ("base o1o1o1 one"); - o1.strs_.push_back ("base o1o1o1 two"); - - object2 o2; - o2.comp_.bools.push_back (true); - o2.comp_.bools.push_back (false); - o2.comp_.num = 12; - o2.comp_.str = "comp o2o2o2"; - o2.comp_.nums.push_back (121); - o2.comp_.nums.push_back (122); - o2.num_ = 2; - static_cast (o2).str_ = "base o2o2o2"; - o2.str_ = "o2o2o2"; - o2.strs_.push_back ("base o2o2o2 one"); - o2.strs_.push_back ("base o2o2o2 two"); - - object3 o3; - o3.comp_.bools.push_back (false); - o3.comp_.bools.push_back (false); - o3.comp_.num = 13; - o3.comp_.str = "comp o3o3o3"; - o3.comp_.nums.push_back (131); - o3.comp_.nums.push_back (132); - o3.num_ = 3; - o3.str_ = "base o3o3o3"; - o3.strs_.push_back ("base o3o3o3 one"); - o3.strs_.push_back ("base o3o3o3 two"); - - reference r; - r.o1_ = &o1; - - empty e; - e.comp_.bools.push_back (true); - e.comp_.bools.push_back (true); - e.comp_.num = 14; - e.comp_.str = "comp eee"; - e.comp_.nums.push_back (141); - e.comp_.nums.push_back (142); - e.num_ = 4; - e.str_ = "base eee"; - e.strs_.push_back ("base eee one"); - e.strs_.push_back ("base eee two"); - - // persist - // - { - transaction t (db->begin ()); - db->persist (b); - db->persist (o1); - db->persist (o2); - db->persist (o3); - db->persist (r); - db->persist (e); - t.commit (); - } - - // load & check - // - { - transaction t (db->begin ()); - auto_ptr lb (db->load (b.id_)); - auto_ptr lo1 (db->load (o1.id_)); - auto_ptr lo2 (db->load (o2.id_)); - auto_ptr lo3 (db->load (o3.id_)); - auto_ptr le (db->load (e.id_)); - auto_ptr lr (db->load (r.id_)); - t.commit (); - - assert (b == *lb); - assert (o1 == *lo1); - assert (o2 == *lo2); - assert (o3 == *lo3); - assert (lr->o1_->id_ == r.o1_->id_); - assert (e == *le); - - delete lr->o1_; - } - - // query - // - { - typedef odb::query b_query; - typedef odb::query o1_query; - typedef odb::query o2_query; - typedef odb::query r_query; - - typedef odb::result r_result; - - transaction t (db->begin ()); - - assert (!db->query (b_query::comp.num == 10).empty ()); - assert (!db->query (o1_query::num1 == 21).empty ()); - assert (!db->query (o2_query::num == 2).empty ()); - - // Query condition with hidden members. - // - assert ( - !db->query (o2_query::base::str == "base o2o2o2").empty ()); - - // Query condition with referenced composite member in base class. - // - { - r_result r (db->query (r_query::o1->comp.num == 11)); - assert (!r.empty ()); - delete r.begin ()->o1_; - } - - 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) - { - cerr << e.what () << endl; - return 1; - } -} diff --git a/common/inheritance/makefile b/common/inheritance/makefile deleted file mode 100644 index df31c4f..0000000 --- a/common/inheritance/makefile +++ /dev/null @@ -1,115 +0,0 @@ -# file : common/inheritance/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 inherit_ -$(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)) $(call vc11projs,$(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)) - $(call meta-vc11projs,../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/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/inheritance/polymorphism/driver.cxx b/common/inheritance/polymorphism/driver.cxx new file mode 100644 index 0000000..20007e4 --- /dev/null +++ b/common/inheritance/polymorphism/driver.cxx @@ -0,0 +1,1880 @@ +// file : common/inheritance/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 + +#include "test1.hxx" +#include "test2.hxx" +#include "test3.hxx" +#include "test4.hxx" +#include "test5.hxx" +#include "test6.hxx" +#include "test7.hxx" +#include "test8.hxx" +#include "test9.hxx" +#include "test10.hxx" +#include "test11.hxx" +#include "test12.hxx" + +#include "test1-odb.hxx" +#include "test2-odb.hxx" +#include "test3-odb.hxx" +#include "test4-odb.hxx" +#include "test5-odb.hxx" +#include "test6-odb.hxx" +#include "test7-odb.hxx" +#include "test8-odb.hxx" +#include "test9-odb.hxx" +#include "test10-odb.hxx" +#include "test11-odb.hxx" +#include "test12-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::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::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::result root_result; + 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::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 (); + } + } + + // Test 12: polymorphic objects with auto id. + // + { + using namespace test12; + + base b (1); + derived d (2); + + unsigned long id1, id2; + + // Persist. + // + { + transaction t (db->begin ()); + id1 = db->persist (b); + id2 = db->persist (static_cast (d)); + t.commit (); + } + + // Load. + // + { + transaction t (db->begin ()); + auto_ptr pb (db->load (id1)); + auto_ptr pd (db->load (id2)); + t.commit (); + + assert (*pb == b); + assert (*pd == d); + } + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/common/inheritance/polymorphism/makefile b/common/inheritance/polymorphism/makefile new file mode 100644 index 0000000..5796d49 --- /dev/null +++ b/common/inheritance/polymorphism/makefile @@ -0,0 +1,118 @@ +# file : common/inheritance/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 := test1.hxx test2.hxx test3.hxx test4.hxx test5.hxx test6.hxx \ +test7.hxx test8.hxx test9.hxx test10.hxx test11.hxx test12.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 := $(foreach f,$(odb_hdr:.hxx=),$(addprefix $f,-odb.hxx -odb.ixx -odb.cxx .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 inherit_p_ +$(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 odb_header_stem := $(basename $(odb_hdr)) +$(dist): export extra_dist := $(data_dist) $(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-vc9projs,../template/template,$(name)) + $(call meta-vc10projs,../template/template,$(name)) + $(call meta-vc11projs,../template/template,$(name)) + +# Test. +# +$(test): schemas := $(addprefix $(out_base)/,$(odb_hdr:.hxx=.sql)) +$(test): $(driver) $(src_base)/test.std + $(call schema,$(schemas)) + $(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/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/inheritance/polymorphism/test.std b/common/inheritance/polymorphism/test.std new file mode 100644 index 0000000..5c18191 --- /dev/null +++ b/common/inheritance/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/inheritance/polymorphism/test1.hxx b/common/inheritance/polymorphism/test1.hxx new file mode 100644 index 0000000..f3015cd --- /dev/null +++ b/common/inheritance/polymorphism/test1.hxx @@ -0,0 +1,116 @@ +// file : common/inheritance/polymorphism/test1.hxx +// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST1_HXX +#define TEST1_HXX + +#include +#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; + }; +} + +#endif // TEST1_HXX diff --git a/common/inheritance/polymorphism/test10.hxx b/common/inheritance/polymorphism/test10.hxx new file mode 100644 index 0000000..d534498 --- /dev/null +++ b/common/inheritance/polymorphism/test10.hxx @@ -0,0 +1,79 @@ +// file : common/inheritance/polymorphism/test10.hxx +// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST10_HXX +#define TEST10_HXX + +#include + +#include + +// 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); + } + }; +} + +#endif // TEST10_HXX diff --git a/common/inheritance/polymorphism/test11.hxx b/common/inheritance/polymorphism/test11.hxx new file mode 100644 index 0000000..0622c37 --- /dev/null +++ b/common/inheritance/polymorphism/test11.hxx @@ -0,0 +1,79 @@ +// file : common/inheritance/polymorphism/test11.hxx +// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST11_HXX +#define TEST11_HXX + +#include +#include +#include + +#include + +// 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 // TEST11_HXX diff --git a/common/inheritance/polymorphism/test12.hxx b/common/inheritance/polymorphism/test12.hxx new file mode 100644 index 0000000..94054ce --- /dev/null +++ b/common/inheritance/polymorphism/test12.hxx @@ -0,0 +1,80 @@ +// file : common/inheritance/polymorphism/test12.hxx +// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST12_HXX +#define TEST12_HXX + +#include + +#include + +// Test polymorphic classes with private auto id. +// +#pragma db namespace table("t12_") +namespace test12 +{ + #pragma db object polymorphic + struct root + { + virtual ~root () = 0; // Auto-abstract. + + virtual bool + compare (const root& r, bool tc = true) const + { + if (tc && typeid (r) != typeid (root)) + return false; + + return id_ == r.id_; + } + + unsigned long id () const {return id_;} + void id (unsigned long id) {id_ = id;} + private: + #pragma db id auto access(id) + unsigned long 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 n): 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 n): base (n) {} + + virtual bool + compare (const root& r, bool tc = true) const + { + if (tc && typeid (r) != typeid (derived)) + return false; + + return base::compare (r, false); + } + }; +} + +#endif // TEST12_HXX diff --git a/common/inheritance/polymorphism/test2.hxx b/common/inheritance/polymorphism/test2.hxx new file mode 100644 index 0000000..a238190 --- /dev/null +++ b/common/inheritance/polymorphism/test2.hxx @@ -0,0 +1,106 @@ +// file : common/inheritance/polymorphism/test2.hxx +// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST2_HXX +#define TEST2_HXX + +#include +#include + +#include +#include + +// 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; + }; +} + +#endif // TEST2_HXX diff --git a/common/inheritance/polymorphism/test3.hxx b/common/inheritance/polymorphism/test3.hxx new file mode 100644 index 0000000..c219bfc --- /dev/null +++ b/common/inheritance/polymorphism/test3.hxx @@ -0,0 +1,147 @@ +// file : common/inheritance/polymorphism/test3.hxx +// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST3_HXX +#define TEST3_HXX + +#include +#include + +#include + +// 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; + }; +} + +#endif // TEST3_HXX diff --git a/common/inheritance/polymorphism/test4.hxx b/common/inheritance/polymorphism/test4.hxx new file mode 100644 index 0000000..b268548 --- /dev/null +++ b/common/inheritance/polymorphism/test4.hxx @@ -0,0 +1,76 @@ +// file : common/inheritance/polymorphism/test4.hxx +// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST4_HXX +#define TEST4_HXX + +#include + +#include + +// 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; + }; +} + +#endif // TEST4_HXX diff --git a/common/inheritance/polymorphism/test5.hxx b/common/inheritance/polymorphism/test5.hxx new file mode 100644 index 0000000..ecc92ad --- /dev/null +++ b/common/inheritance/polymorphism/test5.hxx @@ -0,0 +1,93 @@ +// file : common/inheritance/polymorphism/test5.hxx +// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST5_HXX +#define TEST5_HXX + +#include +#include +#include +#include + +#include + +// 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; + } + }; +} + +#endif // TEST5_HXX diff --git a/common/inheritance/polymorphism/test6.hxx b/common/inheritance/polymorphism/test6.hxx new file mode 100644 index 0000000..a40d8c1 --- /dev/null +++ b/common/inheritance/polymorphism/test6.hxx @@ -0,0 +1,65 @@ +// file : common/inheritance/polymorphism/test6.hxx +// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST6_HXX +#define TEST6_HXX + +#include +#include + +#include +#include + +// 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; + }; +} + +#endif // TEST6_HXX diff --git a/common/inheritance/polymorphism/test7.hxx b/common/inheritance/polymorphism/test7.hxx new file mode 100644 index 0000000..10f604f --- /dev/null +++ b/common/inheritance/polymorphism/test7.hxx @@ -0,0 +1,67 @@ +// file : common/inheritance/polymorphism/test7.hxx +// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST7_HXX +#define TEST7_HXX + +#include // HAVE_CXX11, HAVE_TR1_MEMORY + +#include +#include + +#if !defined(HAVE_CXX11) && defined(HAVE_TR1_MEMORY) +# include +#endif + +#include + +// 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) session + 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 + +#endif // TEST7_HXX diff --git a/common/inheritance/polymorphism/test8.hxx b/common/inheritance/polymorphism/test8.hxx new file mode 100644 index 0000000..ee5829a --- /dev/null +++ b/common/inheritance/polymorphism/test8.hxx @@ -0,0 +1,130 @@ +// file : common/inheritance/polymorphism/test8.hxx +// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST8_HXX +#define TEST8_HXX + +#include +#include +#include + +#include + +// 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; + } + }; +} + +#endif // TEST8_HXX diff --git a/common/inheritance/polymorphism/test9.hxx b/common/inheritance/polymorphism/test9.hxx new file mode 100644 index 0000000..b059464 --- /dev/null +++ b/common/inheritance/polymorphism/test9.hxx @@ -0,0 +1,162 @@ +// file : common/inheritance/polymorphism/test9.hxx +// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST9_HXX +#define TEST9_HXX + +#include +#include +#include + +#include + +// 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; + } + }; +} + +#endif // TEST9_HXX diff --git a/common/inheritance/reuse/driver.cxx b/common/inheritance/reuse/driver.cxx new file mode 100644 index 0000000..78c2703 --- /dev/null +++ b/common/inheritance/reuse/driver.cxx @@ -0,0 +1,182 @@ +// file : common/inheritance/reuse/driver.cxx +// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +// Test reuse 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; + +int +main (int argc, char* argv[]) +{ + try + { + auto_ptr db (create_database (argc, argv)); + + base b; + b.comp_.bools.push_back (true); + b.comp_.num = 10; + b.comp_.str = "comp bbb"; + b.comp_.nums.push_back (101); + b.comp_.nums.push_back (102); + b.num_ = 0; + b.str_ = "bbb"; + b.strs_.push_back ("bbb one"); + b.strs_.push_back ("bbb two"); + + object1 o1; + o1.comp_.bools.push_back (false); + o1.comp_.num = 11; + o1.comp_.str = "comp o1o1o1"; + o1.comp_.nums.push_back (111); + o1.comp_.nums.push_back (112); + static_cast (o1).num_ = 1; + o1.num1_ = 21; + o1.str_ = "base o1o1o1"; + o1.strs_.push_back ("base o1o1o1 one"); + o1.strs_.push_back ("base o1o1o1 two"); + + object2 o2; + o2.comp_.bools.push_back (true); + o2.comp_.bools.push_back (false); + o2.comp_.num = 12; + o2.comp_.str = "comp o2o2o2"; + o2.comp_.nums.push_back (121); + o2.comp_.nums.push_back (122); + o2.num_ = 2; + static_cast (o2).str_ = "base o2o2o2"; + o2.str_ = "o2o2o2"; + o2.strs_.push_back ("base o2o2o2 one"); + o2.strs_.push_back ("base o2o2o2 two"); + + object3 o3; + o3.comp_.bools.push_back (false); + o3.comp_.bools.push_back (false); + o3.comp_.num = 13; + o3.comp_.str = "comp o3o3o3"; + o3.comp_.nums.push_back (131); + o3.comp_.nums.push_back (132); + o3.num_ = 3; + o3.str_ = "base o3o3o3"; + o3.strs_.push_back ("base o3o3o3 one"); + o3.strs_.push_back ("base o3o3o3 two"); + + reference r; + r.o1_ = &o1; + + empty e; + e.comp_.bools.push_back (true); + e.comp_.bools.push_back (true); + e.comp_.num = 14; + e.comp_.str = "comp eee"; + e.comp_.nums.push_back (141); + e.comp_.nums.push_back (142); + e.num_ = 4; + e.str_ = "base eee"; + e.strs_.push_back ("base eee one"); + e.strs_.push_back ("base eee two"); + + // persist + // + { + transaction t (db->begin ()); + db->persist (b); + db->persist (o1); + db->persist (o2); + db->persist (o3); + db->persist (r); + db->persist (e); + t.commit (); + } + + // load & check + // + { + transaction t (db->begin ()); + auto_ptr lb (db->load (b.id_)); + auto_ptr lo1 (db->load (o1.id_)); + auto_ptr lo2 (db->load (o2.id_)); + auto_ptr lo3 (db->load (o3.id_)); + auto_ptr le (db->load (e.id_)); + auto_ptr lr (db->load (r.id_)); + t.commit (); + + assert (b == *lb); + assert (o1 == *lo1); + assert (o2 == *lo2); + assert (o3 == *lo3); + assert (lr->o1_->id_ == r.o1_->id_); + assert (e == *le); + + delete lr->o1_; + } + + // query + // + { + typedef odb::query b_query; + typedef odb::query o1_query; + typedef odb::query o2_query; + typedef odb::query r_query; + + typedef odb::result r_result; + + transaction t (db->begin ()); + + assert (!db->query (b_query::comp.num == 10).empty ()); + assert (!db->query (o1_query::num1 == 21).empty ()); + assert (!db->query (o2_query::num == 2).empty ()); + + // Query condition with hidden members. + // + assert ( + !db->query (o2_query::base::str == "base o2o2o2").empty ()); + + // Query condition with referenced composite member in base class. + // + { + r_result r (db->query (r_query::o1->comp.num == 11)); + assert (!r.empty ()); + delete r.begin ()->o1_; + } + + 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) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/common/inheritance/reuse/makefile b/common/inheritance/reuse/makefile new file mode 100644 index 0000000..1fc1142 --- /dev/null +++ b/common/inheritance/reuse/makefile @@ -0,0 +1,115 @@ +# file : common/inheritance/reuse/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 inherit_r_ +$(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)) $(call vc11projs,$(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)) + $(call meta-vc11projs,../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/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/inheritance/reuse/test.hxx b/common/inheritance/reuse/test.hxx new file mode 100644 index 0000000..8222f97 --- /dev/null +++ b/common/inheritance/reuse/test.hxx @@ -0,0 +1,158 @@ +// file : common/inheritance/reuse/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 +#include + +#include + +#pragma db value +struct comp_base +{ + std::vector bools; + + bool + operator== (const comp_base& y) const + { + return bools == y.bools; + } +}; + +#pragma db value +struct comp: comp_base +{ + unsigned int num; + std::string str; + + std::vector nums; + + bool + operator== (const comp& y) const + { + return + static_cast (*this) == y && + num == y.num && + str == y.str && + nums == y.nums; + } +}; + +#pragma db object abstract +struct abstract_base +{ + comp comp_; + + unsigned int num_; + std::string str_; + + std::vector strs_; + + bool + operator== (const abstract_base& y) const + { + return + comp_ == y.comp_ && + num_ == y.num_ && + str_ == y.str_ && + strs_ == y.strs_; + } +}; + +#pragma db object +struct base: abstract_base +{ + #pragma db id auto + unsigned long id_; + + bool + operator== (const base& y) const + { + return id_ == y.id_ && static_cast (*this) == y; + } +}; + +#pragma db object +struct object1: base +{ + unsigned int num1_; + + bool + operator== (const object1& y) const + { + return static_cast (*this) == y && num1_ == y.num1_; + } +}; + +#pragma db object +struct object2: base +{ + #pragma db column("derived_str") + std::string str_; + + bool + operator== (const object2& y) const + { + return static_cast (*this) == y && str_ == y.str_; + } +}; + +// Reference to derived object. +// +#pragma db object +struct reference +{ + #pragma db id auto + unsigned long id_; + + object1* o1_; +}; + +// Multiple inheritance. +// +#pragma db object abstract +struct id_base +{ + #pragma db id auto + unsigned long id_; + + bool + operator== (const id_base& y) const + { + return id_ == y.id_; + } +}; + +#pragma db object +struct object3: abstract_base, id_base +{ + bool + operator== (const object3& y) const + { + return + static_cast (*this) == y && + static_cast (*this) == y; + } +}; + +// Empty derived object. +// +#pragma db object +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/inheritance/reuse/test.std b/common/inheritance/reuse/test.std new file mode 100644 index 0000000..e69de29 diff --git a/common/inheritance/test.hxx b/common/inheritance/test.hxx deleted file mode 100644 index 6059337..0000000 --- a/common/inheritance/test.hxx +++ /dev/null @@ -1,158 +0,0 @@ -// file : common/inheritance/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 -#include - -#include - -#pragma db value -struct comp_base -{ - std::vector bools; - - bool - operator== (const comp_base& y) const - { - return bools == y.bools; - } -}; - -#pragma db value -struct comp: comp_base -{ - unsigned int num; - std::string str; - - std::vector nums; - - bool - operator== (const comp& y) const - { - return - static_cast (*this) == y && - num == y.num && - str == y.str && - nums == y.nums; - } -}; - -#pragma db object abstract -struct abstract_base -{ - comp comp_; - - unsigned int num_; - std::string str_; - - std::vector strs_; - - bool - operator== (const abstract_base& y) const - { - return - comp_ == y.comp_ && - num_ == y.num_ && - str_ == y.str_ && - strs_ == y.strs_; - } -}; - -#pragma db object -struct base: abstract_base -{ - #pragma db id auto - unsigned long id_; - - bool - operator== (const base& y) const - { - return id_ == y.id_ && static_cast (*this) == y; - } -}; - -#pragma db object -struct object1: base -{ - unsigned int num1_; - - bool - operator== (const object1& y) const - { - return static_cast (*this) == y && num1_ == y.num1_; - } -}; - -#pragma db object -struct object2: base -{ - #pragma db column("derived_str") - std::string str_; - - bool - operator== (const object2& y) const - { - return static_cast (*this) == y && str_ == y.str_; - } -}; - -// Reference to derived object. -// -#pragma db object -struct reference -{ - #pragma db id auto - unsigned long id_; - - object1* o1_; -}; - -// Multiple inheritance. -// -#pragma db object abstract -struct id_base -{ - #pragma db id auto - unsigned long id_; - - bool - operator== (const id_base& y) const - { - return id_ == y.id_; - } -}; - -#pragma db object -struct object3: abstract_base, id_base -{ - bool - operator== (const object3& y) const - { - return - static_cast (*this) == y && - static_cast (*this) == y; - } -}; - -// Empty derived object. -// -#pragma db object -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/inheritance/test.std b/common/inheritance/test.std deleted file mode 100644 index e69de29..0000000 diff --git a/common/inheritance/transient/driver.cxx b/common/inheritance/transient/driver.cxx new file mode 100644 index 0000000..099506b --- /dev/null +++ b/common/inheritance/transient/driver.cxx @@ -0,0 +1,79 @@ +// file : common/inheritance/transient/driver.cxx +// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +// Test transient inheritance of objects, composite value types, and views. +// + +#include // std::auto_ptr +#include +#include + +#include +#include + +#include + +#include "test.hxx" +#include "test-odb.hxx" + +using namespace std; +using namespace odb::core; + +int +main (int argc, char* argv[]) +{ + try + { + auto_ptr db (create_database (argc, argv)); + + object o; + o.num = 1; + o.str = "abc"; + o.strs.push_back ("abc 1"); + o.strs.push_back ("abc 2"); + o.c.num = 11; + o.c.str = "comp abc"; + o.c.nums.push_back (111); + o.c.nums.push_back (112); + + // persist + // + { + transaction t (db->begin ()); + db->persist (o); + t.commit (); + } + + // load & check + // + { + transaction t (db->begin ()); + auto_ptr p (db->load (o.id_)); + t.commit (); + + assert (*p == o); + } + + // view + // + { + typedef odb::query query; + typedef odb::result result; + + transaction t (db->begin ()); + + result r (db->query (query::id == o.id_)); + result::iterator i (r.begin ()); + assert (i != r.end () && i->num == o.num && i->str == o.str); + assert (++i == r.end ()); + + t.commit (); + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/common/inheritance/transient/makefile b/common/inheritance/transient/makefile new file mode 100644 index 0000000..ab5936a --- /dev/null +++ b/common/inheritance/transient/makefile @@ -0,0 +1,115 @@ +# file : common/inheritance/transient/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 inherit_t_ +$(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)) $(call vc11projs,$(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)) + $(call meta-vc11projs,../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/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/inheritance/transient/test.hxx b/common/inheritance/transient/test.hxx new file mode 100644 index 0000000..df5d0c1 --- /dev/null +++ b/common/inheritance/transient/test.hxx @@ -0,0 +1,61 @@ +// file : common/inheritance/transient/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 +#include + +#include + +struct object; + +struct base +{ + int n; + std::vector v; + object* p; +}; + +#pragma db value +struct comp: base +{ + unsigned int num; + std::string str; + std::vector nums; + + bool + operator== (const comp& y) const + { + return num == y.num && str == y.str && nums == y.nums; + } +}; + +#pragma db object +struct object: base +{ + #pragma db id auto + unsigned int id_; + + unsigned int num; + std::string str; + std::vector strs; + comp c; + + bool + operator== (const object& y) const + { + return num == y.num && str == y.str && strs == y.strs && c == y.c; + } +}; + +#pragma db view object(object) +struct view: base +{ + unsigned int num; + std::string str; +}; + +#endif // TEST_HXX diff --git a/common/inheritance/transient/test.std b/common/inheritance/transient/test.std new file mode 100644 index 0000000..e69de29 diff --git a/common/makefile b/common/makefile index 3988260..6f187fb 100644 --- a/common/makefile +++ b/common/makefile @@ -4,44 +4,45 @@ include $(dir $(lastword $(MAKEFILE_LIST)))../build/bootstrap.make -tests := \ -access \ -auto \ -blob \ -callback \ -circular/single \ -circular/multiple \ -composite \ -composite-id \ -const-object \ -const-member \ -container \ -ctor \ -default \ -definition \ -enum \ -erase-query \ -include \ -index \ -inheritance \ -inverse \ -lazy-ptr \ -lifecycle \ -no-id \ -optimistic \ -polymorphism \ -pragma \ -query \ -readonly \ -relationship \ -relationship-query \ -schema \ -session \ -template \ -transaction \ -types \ -view \ -virtual \ +tests := \ +access \ +auto \ +blob \ +callback \ +circular/single \ +circular/multiple \ +composite \ +composite-id \ +const-object \ +const-member \ +container \ +ctor \ +default \ +definition \ +enum \ +erase-query \ +include \ +index \ +inheritance/polymorphism \ +inheritance/reuse \ +inheritance/transient \ +inverse \ +lazy-ptr \ +lifecycle \ +no-id \ +optimistic \ +pragma \ +query \ +readonly \ +relationship \ +relationship-query \ +schema \ +session \ +template \ +transaction \ +types \ +view \ +virtual \ wrapper thread_tests := threads diff --git a/common/polymorphism/driver.cxx b/common/polymorphism/driver.cxx deleted file mode 100644 index 1bd8a2f..0000000 --- a/common/polymorphism/driver.cxx +++ /dev/null @@ -1,1880 +0,0 @@ -// 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 - -#include "test1.hxx" -#include "test2.hxx" -#include "test3.hxx" -#include "test4.hxx" -#include "test5.hxx" -#include "test6.hxx" -#include "test7.hxx" -#include "test8.hxx" -#include "test9.hxx" -#include "test10.hxx" -#include "test11.hxx" -#include "test12.hxx" - -#include "test1-odb.hxx" -#include "test2-odb.hxx" -#include "test3-odb.hxx" -#include "test4-odb.hxx" -#include "test5-odb.hxx" -#include "test6-odb.hxx" -#include "test7-odb.hxx" -#include "test8-odb.hxx" -#include "test9-odb.hxx" -#include "test10-odb.hxx" -#include "test11-odb.hxx" -#include "test12-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::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::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::result root_result; - 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::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 (); - } - } - - // Test 12: polymorphic objects with auto id. - // - { - using namespace test12; - - base b (1); - derived d (2); - - unsigned long id1, id2; - - // Persist. - // - { - transaction t (db->begin ()); - id1 = db->persist (b); - id2 = db->persist (static_cast (d)); - t.commit (); - } - - // Load. - // - { - transaction t (db->begin ()); - auto_ptr pb (db->load (id1)); - auto_ptr pd (db->load (id2)); - t.commit (); - - assert (*pb == b); - assert (*pd == d); - } - } - } - catch (const odb::exception& e) - { - cerr << e.what () << endl; - return 1; - } -} diff --git a/common/polymorphism/makefile b/common/polymorphism/makefile deleted file mode 100644 index 6a8b72a..0000000 --- a/common/polymorphism/makefile +++ /dev/null @@ -1,118 +0,0 @@ -# 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 := test1.hxx test2.hxx test3.hxx test4.hxx test5.hxx test6.hxx \ -test7.hxx test8.hxx test9.hxx test10.hxx test11.hxx test12.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 := $(foreach f,$(odb_hdr:.hxx=),$(addprefix $f,-odb.hxx -odb.ixx -odb.cxx .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 odb_header_stem := $(basename $(odb_hdr)) -$(dist): export extra_dist := $(data_dist) $(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-vc9projs,../template/template,$(name)) - $(call meta-vc10projs,../template/template,$(name)) - $(call meta-vc11projs,../template/template,$(name)) - -# Test. -# -$(test): schemas := $(addprefix $(out_base)/,$(odb_hdr:.hxx=.sql)) -$(test): $(driver) $(src_base)/test.std - $(call schema,$(schemas)) - $(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/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/polymorphism/test.std b/common/polymorphism/test.std deleted file mode 100644 index 5c18191..0000000 --- a/common/polymorphism/test.std +++ /dev/null @@ -1,36 +0,0 @@ -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/polymorphism/test1.hxx b/common/polymorphism/test1.hxx deleted file mode 100644 index e6db3f4..0000000 --- a/common/polymorphism/test1.hxx +++ /dev/null @@ -1,116 +0,0 @@ -// file : common/polymorphism/test1.hxx -// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC -// license : GNU GPL v2; see accompanying LICENSE file - -#ifndef TEST1_HXX -#define TEST1_HXX - -#include -#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; - }; -} - -#endif // TEST1_HXX diff --git a/common/polymorphism/test10.hxx b/common/polymorphism/test10.hxx deleted file mode 100644 index be7d3c0..0000000 --- a/common/polymorphism/test10.hxx +++ /dev/null @@ -1,79 +0,0 @@ -// file : common/polymorphism/test10.hxx -// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC -// license : GNU GPL v2; see accompanying LICENSE file - -#ifndef TEST10_HXX -#define TEST10_HXX - -#include - -#include - -// 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); - } - }; -} - -#endif // TEST10_HXX diff --git a/common/polymorphism/test11.hxx b/common/polymorphism/test11.hxx deleted file mode 100644 index 58a4ff4..0000000 --- a/common/polymorphism/test11.hxx +++ /dev/null @@ -1,79 +0,0 @@ -// file : common/polymorphism/test11.hxx -// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC -// license : GNU GPL v2; see accompanying LICENSE file - -#ifndef TEST11_HXX -#define TEST11_HXX - -#include -#include -#include - -#include - -// 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 // TEST11_HXX diff --git a/common/polymorphism/test12.hxx b/common/polymorphism/test12.hxx deleted file mode 100644 index 5feb2bb..0000000 --- a/common/polymorphism/test12.hxx +++ /dev/null @@ -1,80 +0,0 @@ -// file : common/polymorphism/test12.hxx -// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC -// license : GNU GPL v2; see accompanying LICENSE file - -#ifndef TEST12_HXX -#define TEST12_HXX - -#include - -#include - -// Test polymorphic classes with private auto id. -// -#pragma db namespace table("t12_") -namespace test12 -{ - #pragma db object polymorphic - struct root - { - virtual ~root () = 0; // Auto-abstract. - - virtual bool - compare (const root& r, bool tc = true) const - { - if (tc && typeid (r) != typeid (root)) - return false; - - return id_ == r.id_; - } - - unsigned long id () const {return id_;} - void id (unsigned long id) {id_ = id;} - private: - #pragma db id auto access(id) - unsigned long 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 n): 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 n): base (n) {} - - virtual bool - compare (const root& r, bool tc = true) const - { - if (tc && typeid (r) != typeid (derived)) - return false; - - return base::compare (r, false); - } - }; -} - -#endif // TEST12_HXX diff --git a/common/polymorphism/test2.hxx b/common/polymorphism/test2.hxx deleted file mode 100644 index 1b81f41..0000000 --- a/common/polymorphism/test2.hxx +++ /dev/null @@ -1,106 +0,0 @@ -// file : common/polymorphism/test2.hxx -// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC -// license : GNU GPL v2; see accompanying LICENSE file - -#ifndef TEST2_HXX -#define TEST2_HXX - -#include -#include - -#include -#include - -// 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; - }; -} - -#endif // TEST2_HXX diff --git a/common/polymorphism/test3.hxx b/common/polymorphism/test3.hxx deleted file mode 100644 index 9a9a269..0000000 --- a/common/polymorphism/test3.hxx +++ /dev/null @@ -1,147 +0,0 @@ -// file : common/polymorphism/test3.hxx -// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC -// license : GNU GPL v2; see accompanying LICENSE file - -#ifndef TEST3_HXX -#define TEST3_HXX - -#include -#include - -#include - -// 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; - }; -} - -#endif // TEST3_HXX diff --git a/common/polymorphism/test4.hxx b/common/polymorphism/test4.hxx deleted file mode 100644 index d9eba86..0000000 --- a/common/polymorphism/test4.hxx +++ /dev/null @@ -1,76 +0,0 @@ -// file : common/polymorphism/test4.hxx -// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC -// license : GNU GPL v2; see accompanying LICENSE file - -#ifndef TEST4_HXX -#define TEST4_HXX - -#include - -#include - -// 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; - }; -} - -#endif // TEST4_HXX diff --git a/common/polymorphism/test5.hxx b/common/polymorphism/test5.hxx deleted file mode 100644 index ede75a0..0000000 --- a/common/polymorphism/test5.hxx +++ /dev/null @@ -1,93 +0,0 @@ -// file : common/polymorphism/test5.hxx -// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC -// license : GNU GPL v2; see accompanying LICENSE file - -#ifndef TEST5_HXX -#define TEST5_HXX - -#include -#include -#include -#include - -#include - -// 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; - } - }; -} - -#endif // TEST5_HXX diff --git a/common/polymorphism/test6.hxx b/common/polymorphism/test6.hxx deleted file mode 100644 index 9ecef40..0000000 --- a/common/polymorphism/test6.hxx +++ /dev/null @@ -1,65 +0,0 @@ -// file : common/polymorphism/test6.hxx -// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC -// license : GNU GPL v2; see accompanying LICENSE file - -#ifndef TEST6_HXX -#define TEST6_HXX - -#include -#include - -#include -#include - -// 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; - }; -} - -#endif // TEST6_HXX diff --git a/common/polymorphism/test7.hxx b/common/polymorphism/test7.hxx deleted file mode 100644 index 1d3a807..0000000 --- a/common/polymorphism/test7.hxx +++ /dev/null @@ -1,67 +0,0 @@ -// file : common/polymorphism/test7.hxx -// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC -// license : GNU GPL v2; see accompanying LICENSE file - -#ifndef TEST7_HXX -#define TEST7_HXX - -#include // HAVE_CXX11, HAVE_TR1_MEMORY - -#include -#include - -#if !defined(HAVE_CXX11) && defined(HAVE_TR1_MEMORY) -# include -#endif - -#include - -// 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) session - 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 - -#endif // TEST7_HXX diff --git a/common/polymorphism/test8.hxx b/common/polymorphism/test8.hxx deleted file mode 100644 index adaafc2..0000000 --- a/common/polymorphism/test8.hxx +++ /dev/null @@ -1,130 +0,0 @@ -// file : common/polymorphism/test8.hxx -// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC -// license : GNU GPL v2; see accompanying LICENSE file - -#ifndef TEST8_HXX -#define TEST8_HXX - -#include -#include -#include - -#include - -// 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; - } - }; -} - -#endif // TEST8_HXX diff --git a/common/polymorphism/test9.hxx b/common/polymorphism/test9.hxx deleted file mode 100644 index d1496c1..0000000 --- a/common/polymorphism/test9.hxx +++ /dev/null @@ -1,162 +0,0 @@ -// file : common/polymorphism/test9.hxx -// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC -// license : GNU GPL v2; see accompanying LICENSE file - -#ifndef TEST9_HXX -#define TEST9_HXX - -#include -#include -#include - -#include - -// 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; - } - }; -} - -#endif // TEST9_HXX -- cgit v1.1