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/reuse/driver.cxx | 182 ++++++++++++++++++++++++++++++++++++ common/inheritance/reuse/makefile | 115 +++++++++++++++++++++++ common/inheritance/reuse/test.hxx | 158 +++++++++++++++++++++++++++++++ common/inheritance/reuse/test.std | 0 4 files changed, 455 insertions(+) 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 (limited to 'common/inheritance/reuse') 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 -- cgit v1.1