diff options
Diffstat (limited to 'odb-tests/common/inheritance/reuse')
-rw-r--r-- | odb-tests/common/inheritance/reuse/buildfile | 53 | ||||
-rw-r--r-- | odb-tests/common/inheritance/reuse/driver.cxx | 237 | ||||
-rw-r--r-- | odb-tests/common/inheritance/reuse/test.hxx | 163 | ||||
-rw-r--r-- | odb-tests/common/inheritance/reuse/testscript | 53 |
4 files changed, 506 insertions, 0 deletions
diff --git a/odb-tests/common/inheritance/reuse/buildfile b/odb-tests/common/inheritance/reuse/buildfile new file mode 100644 index 0000000..0b7f944 --- /dev/null +++ b/odb-tests/common/inheritance/reuse/buildfile @@ -0,0 +1,53 @@ +# file : common/inheritance/reuse/buildfile +# license : GNU GPL v2; see accompanying LICENSE file + +import libodb = libodb%lib{odb} + +libs = + +for db: $databases + import libs += libodb-$db%lib{odb-$db} + +import libs += lib{common} + +exe{driver}: {hxx cxx}{* -*-odb -*-odb-*} {hxx ixx cxx}{test-odb} testscript + +# Introduce the metadata library target to make sure the libodb library is +# resolved for the odb_compile ad hoc rule (see build/root.build for details). +# +libue{test-meta}: $libodb + +<{hxx ixx cxx}{test-odb}>: hxx{test} libue{test-meta} + +for db: $databases +{ + exe{driver}: {hxx ixx cxx}{test-odb-$db}: include = $multi + <{hxx ixx cxx}{test-odb-$db}>: hxx{test} libue{test-meta} +} + +exe{driver}: libue{test-meta} $libs + +# Specify the ODB custom options to be used by the odb_compile ad hoc rule +# (see build/root.build for details). +# +# Note that the table prefix inhrt_r_, which we would normally use for this +# test, doesn't work well for Oracle. In this case ODB ends up with the +# following error: +# +# test.hxx:16:30: error: index name 'inhrt_r_empty_object_comp_bool' conflicts with an already defined index name +# test.hxx:16:30: info: index name 'inhrt_r_empty_object_comp_bool' is truncated 'inhrt_r_empty_object_comp_bools_index_i' +# test.hxx:16:30: info: conflicting index is defined here +# test.hxx:16:30: info: conflicting index name 'inhrt_r_empty_object_comp_bool' is truncated 'inhrt_r_empty_object_comp_bools_object_id_i' +# test.hxx:16:30: info: use #pragma db index to change one of the names +# +# Thus, we shorten the prefix to just i_r_. +# +odb_options = --table-prefix i_r_ \ + --generate-schema \ + --generate-query + +cxx.poptions =+ "-I$out_base" "-I$src_base" + +# Testscript's run-time prerequisites. +# +exe{driver}: ../../../alias{database-client}: include = adhoc diff --git a/odb-tests/common/inheritance/reuse/driver.cxx b/odb-tests/common/inheritance/reuse/driver.cxx new file mode 100644 index 0000000..e6122bb --- /dev/null +++ b/odb-tests/common/inheritance/reuse/driver.cxx @@ -0,0 +1,237 @@ +// file : common/inheritance/reuse/driver.cxx +// license : GNU GPL v2; see accompanying LICENSE file + +// Test reuse object inheritance. +// + +#include <memory> // std::unique_ptr +#include <iostream> + +#include <odb/database.hxx> +#include <odb/transaction.hxx> + +#include <libcommon/common.hxx> + +#include "test.hxx" +#include "test-odb.hxx" + +#undef NDEBUG +#include <cassert> + +using namespace std; +using namespace odb::core; + +int +main (int argc, char* argv[]) +{ + try + { + unique_ptr<database> db (create_database (argc, argv)); + + base b; + b.comp_.bools.push_back (true); + b.comp_.obools.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.comp_.onums.push_back (101); + b.comp_.onums.push_back (102); + b.num_ = 0; + b.str_ = "bbb"; + b.strs_.push_back ("bbb one"); + b.strs_.push_back ("bbb two"); + b.ostrs_.push_back ("bbb one"); + b.ostrs_.push_back ("bbb two"); + + object1 o1; + o1.comp_.bools.push_back (false); + o1.comp_.obools.push_back (false); + o1.comp_.num = 11; + o1.comp_.str = "comp o1o1o1"; + o1.comp_.nums.push_back (111); + o1.comp_.nums.push_back (112); + o1.comp_.onums.push_back (111); + o1.comp_.onums.push_back (112); + static_cast<base&> (o1).num_ = 1; + o1.num1_ = 21; + o1.str_ = "base o1o1o1"; + o1.strs_.push_back ("base o1o1o1 one"); + o1.strs_.push_back ("base o1o1o1 two"); + o1.ostrs_.push_back ("base o1o1o1 one"); + o1.ostrs_.push_back ("base o1o1o1 two"); + + object2 o2; + o2.comp_.bools.push_back (true); + o2.comp_.bools.push_back (false); + o2.comp_.obools.push_back (true); + o2.comp_.obools.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.comp_.onums.push_back (121); + o2.comp_.onums.push_back (122); + o2.num_ = 2; + static_cast<base&> (o2).str_ = "base o2o2o2"; + o2.str_ = "o2o2o2"; + o2.strs_.push_back ("base o2o2o2 one"); + o2.strs_.push_back ("base o2o2o2 two"); + o2.ostrs_.push_back ("base o2o2o2 one"); + o2.ostrs_.push_back ("base o2o2o2 two"); + + object3 o3; + o3.comp_.bools.push_back (false); + o3.comp_.bools.push_back (false); + o3.comp_.obools.push_back (false); + o3.comp_.obools.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.comp_.onums.push_back (131); + o3.comp_.onums.push_back (132); + o3.num_ = 3; + o3.str_ = "base o3o3o3"; + o3.strs_.push_back ("base o3o3o3 one"); + o3.strs_.push_back ("base o3o3o3 two"); + o3.ostrs_.push_back ("base o3o3o3 one"); + o3.ostrs_.push_back ("base o3o3o3 two"); + + reference r; + r.o1_ = &o1; + + empty_object e; + e.comp_.bools.push_back (true); + e.comp_.bools.push_back (true); + e.comp_.obools.push_back (true); + e.comp_.obools.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.comp_.onums.push_back (141); + e.comp_.onums.push_back (142); + e.num_ = 4; + e.str_ = "base eee"; + e.strs_.push_back ("base eee one"); + e.strs_.push_back ("base eee two"); + e.ostrs_.push_back ("base eee one"); + e.ostrs_.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 ()); + unique_ptr<base> lb (db->load<base> (b.id_)); + unique_ptr<object1> lo1 (db->load<object1> (o1.id_)); + unique_ptr<object2> lo2 (db->load<object2> (o2.id_)); + unique_ptr<object3> lo3 (db->load<object3> (o3.id_)); + unique_ptr<empty_object> le (db->load<empty_object> (e.id_)); + unique_ptr<reference> lr (db->load<reference> (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_; + } + + // update + // + { + transaction t (db->begin ()); + db->update (b); + db->update (o1); + db->update (o2); + db->update (o3); + db->update (r); + db->update (e); + t.commit (); + } + + // query + // + { + typedef odb::query<base> b_query; + typedef odb::query<object1> o1_query; + typedef odb::query<object2> o2_query; + typedef odb::query<reference> r_query; + + typedef odb::result<reference> r_result; + + transaction t (db->begin ()); + + assert (!db->query<base> (b_query::comp.num == 10).empty ()); + assert (!db->query<object1> (o1_query::num1 == 21).empty ()); + assert (!db->query<object2> (o2_query::num == 2).empty ()); + + // Query condition with hidden members. + // + assert ( + !db->query<object2> (o2_query::base::str == "base o2o2o2").empty ()); + + // Query condition with referenced composite member in base class. + // + { + r_result r (db->query<reference> (r_query::o1->comp.num == 11)); + assert (!r.empty ()); + delete r.begin ()->o1_; + } + + t.commit (); + } + + // views + // + { + typedef odb::query<object2_view> query; + typedef odb::result<object2_view> result; + + transaction t (db->begin ()); + + result r (db->query<object2_view> (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 (); + } + + // erase + // + { + transaction t (db->begin ()); + db->erase (b); + db->erase (o1); + db->erase (o2); + db->erase (o3); + db->erase (r); + db->erase (e); + t.commit (); + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/odb-tests/common/inheritance/reuse/test.hxx b/odb-tests/common/inheritance/reuse/test.hxx new file mode 100644 index 0000000..48f474f --- /dev/null +++ b/odb-tests/common/inheritance/reuse/test.hxx @@ -0,0 +1,163 @@ +// file : common/inheritance/reuse/test.hxx +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include <string> +#include <vector> + +#include <odb/core.hxx> +#include <odb/vector.hxx> + +#pragma db value +struct comp_base +{ + std::vector<unsigned char> bools; + odb::vector<unsigned char> obools; + + bool + operator== (const comp_base& y) const + { + return bools == y.bools && obools == y.obools; + } +}; + +#pragma db value +struct comp: comp_base +{ + unsigned int num; + std::string str; + + std::vector<unsigned int> nums; + odb::vector<unsigned int> onums; + + bool + operator== (const comp& y) const + { + return + static_cast<const comp_base&> (*this) == y && + num == y.num && + str == y.str && + nums == y.nums && + onums == y.onums; + } +}; + +#pragma db object abstract +struct abstract_base +{ + comp comp_; + + unsigned int num_; + std::string str_; + + std::vector<std::string> strs_; + odb::vector<std::string> ostrs_; + + bool + operator== (const abstract_base& y) const + { + return + comp_ == y.comp_ && + num_ == y.num_ && + str_ == y.str_ && + strs_ == y.strs_ && + ostrs_ == y.ostrs_; + } +}; + +#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<const abstract_base&> (*this) == y; + } +}; + +#pragma db object +struct object1: base +{ + unsigned int num1_; + + bool + operator== (const object1& y) const + { + return static_cast<const base&> (*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<const base&> (*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<const abstract_base&> (*this) == y && + static_cast<const id_base&> (*this) == y; + } +}; + +// Empty derived object. +// +#pragma db object +struct empty_object: 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/odb-tests/common/inheritance/reuse/testscript b/odb-tests/common/inheritance/reuse/testscript new file mode 100644 index 0000000..1bc20ea --- /dev/null +++ b/odb-tests/common/inheritance/reuse/testscript @@ -0,0 +1,53 @@ +# file : common/inheritance/reuse/testscript +# license : GNU GPL v2; see accompanying LICENSE file + +.include ../../../database-options.testscript + +: mysql +: +if $mysql +{ + .include ../../../mysql.testscript + + $create_schema; + $* +} + +: sqlite +: +if $sqlite +{ + .include ../../../sqlite.testscript + + $* +} + +: pgsql +: +if $pgsql +{ + .include ../../../pgsql.testscript + + $create_schema; + $* +} + +: oracle +: +if $oracle +{ + .include ../../../oracle.testscript + + $create_schema; + $* +} + +: mssql +: +if $mssql +{ + .include ../../../mssql.testscript + + $create_schema; + $* +} |