aboutsummaryrefslogtreecommitdiff
path: root/common/inheritance/reuse
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2012-10-08 14:47:57 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2012-10-08 14:47:57 +0200
commit6b76715e63d2c265a4c51c73f9019bc578f874cb (patch)
treec0b5f6a944abf6fe015742bbc08e5a393249cf6d /common/inheritance/reuse
parentdcee8530bb5c58710a22f50bb92ac7c3b19ddf84 (diff)
Fix bug in transient inheritance handling
Also test that we can handle it for objects, composite values, and views.
Diffstat (limited to 'common/inheritance/reuse')
-rw-r--r--common/inheritance/reuse/driver.cxx182
-rw-r--r--common/inheritance/reuse/makefile115
-rw-r--r--common/inheritance/reuse/test.hxx158
-rw-r--r--common/inheritance/reuse/test.std0
4 files changed, 455 insertions, 0 deletions
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 <memory> // std::auto_ptr
+#include <cassert>
+#include <iostream>
+
+#include <odb/database.hxx>
+#include <odb/transaction.hxx>
+
+#include <common/common.hxx>
+
+#include "test.hxx"
+#include "test-odb.hxx"
+
+using namespace std;
+using namespace odb::core;
+
+int
+main (int argc, char* argv[])
+{
+ try
+ {
+ auto_ptr<database> 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<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");
+
+ 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<base&> (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<base> lb (db->load<base> (b.id_));
+ auto_ptr<object1> lo1 (db->load<object1> (o1.id_));
+ auto_ptr<object2> lo2 (db->load<object2> (o2.id_));
+ auto_ptr<object3> lo3 (db->load<object3> (o3.id_));
+ auto_ptr<empty> le (db->load<empty> (e.id_));
+ auto_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_;
+ }
+
+ // 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 ();
+ }
+ }
+ 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 <string>
+#include <vector>
+
+#include <odb/core.hxx>
+
+#pragma db value
+struct comp_base
+{
+ std::vector<unsigned char> 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<unsigned int> nums;
+
+ bool
+ operator== (const comp& y) const
+ {
+ return
+ static_cast<const comp_base&> (*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<std::string> 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<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: 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
--- /dev/null
+++ b/common/inheritance/reuse/test.std