aboutsummaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2011-04-22 14:07:33 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2011-04-22 14:07:33 +0200
commitd0f0e168cac3d117f64fddcca638a18fc4309ba1 (patch)
tree90a23287b37065aeb99a597c5bc6273b9efbbf07 /common
parent6aca63b74d4fff54b89112bda3ec3ba258a073c1 (diff)
Initial support for non-polymorphic inheritance
Every class gets a separate table. New test: common/inheritance.
Diffstat (limited to 'common')
-rw-r--r--common/inheritance/driver.cxx136
-rw-r--r--common/inheritance/makefile109
-rw-r--r--common/inheritance/test.hxx88
-rw-r--r--common/inheritance/test.std0
-rw-r--r--common/makefile1
5 files changed, 334 insertions, 0 deletions
diff --git a/common/inheritance/driver.cxx b/common/inheritance/driver.cxx
new file mode 100644
index 0000000..203e912
--- /dev/null
+++ b/common/inheritance/driver.cxx
@@ -0,0 +1,136 @@
+// file : common/inheritance/driver.cxx
+// author : Boris Kolpackov <boris@codesynthesis.com>
+// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC
+// license : GNU GPL v2; see accompanying LICENSE file
+
+// Test 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_.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_.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_.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");
+
+ reference r;
+ r.o1_ = &o1;
+
+ // persist
+ //
+ {
+ transaction t (db->begin ());
+ db->persist (b);
+ db->persist (o1);
+ db->persist (o2);
+ db->persist (r);
+ 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<reference> lr (db->load<reference> (r.id_));
+ t.commit ();
+
+ assert (b == *lb);
+ assert (o1 == *lo1);
+ assert (o2 == *lo2);
+ assert (lr->o1_->id_ == r.o1_->id_);
+
+ delete lr->o1_;
+ }
+
+ // query
+ //
+ {
+ typedef odb::query<base> b_query;
+ typedef odb::result<base> b_result;
+
+ typedef odb::query<object1> o1_query;
+ typedef odb::result<object1> o1_result;
+
+ typedef odb::query<object2> o2_query;
+ typedef odb::result<object2> o2_result;
+
+ 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> (b_query::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 ();
+ }
+ }
+ catch (const odb::exception& e)
+ {
+ cerr << e.what () << endl;
+ return 1;
+ }
+}
diff --git a/common/inheritance/makefile b/common/inheritance/makefile
new file mode 100644
index 0000000..b9715e3
--- /dev/null
+++ b/common/inheritance/makefile
@@ -0,0 +1,109 @@
+# file : common/inheritance/makefile
+# author : Boris Kolpackov <boris@codesynthesis.com>
+# copyright : Copyright (c) 2009-2011 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
+$(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))
+$(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))
+
+# 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/automake.make)
+
+$(call include,$(odb_rules))
+$(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/test.hxx b/common/inheritance/test.hxx
new file mode 100644
index 0000000..e27ff7a
--- /dev/null
+++ b/common/inheritance/test.hxx
@@ -0,0 +1,88 @@
+// file : common/inheritance/test.hxx
+// author : Boris Kolpackov <boris@codesynthesis.com>
+// copyright : Copyright (c) 2009-2011 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
+{
+ unsigned int num;
+ std::string str;
+
+ std::vector<unsigned int> nums;
+
+ bool
+ operator== (const comp& y) const
+ {
+ return num == y.num && str == y.str && nums == y.nums;
+ }
+};
+
+#pragma db object
+struct base
+{
+ #pragma db id auto
+ unsigned long id_;
+
+ comp comp_;
+
+ unsigned int num_;
+ std::string str_;
+
+ std::vector<std::string> strs_;
+
+ bool
+ operator== (const base& y) const
+ {
+ return
+ id_ == y.id_ &&
+ comp_ == y.comp_ &&
+ num_ == y.num_ &&
+ str_ == y.str_ &&
+ strs_ == y.strs_;
+ }
+};
+
+#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_;
+ }
+};
+
+#pragma db object
+struct reference
+{
+ #pragma db id auto
+ unsigned long id_;
+
+ object1* o1_;
+};
+
+#endif // TEST_HXX
diff --git a/common/inheritance/test.std b/common/inheritance/test.std
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/common/inheritance/test.std
diff --git a/common/makefile b/common/makefile
index 317c49e..394f261 100644
--- a/common/makefile
+++ b/common/makefile
@@ -12,6 +12,7 @@ const \
container \
ctor \
enum \
+inheritance \
inverse \
lazy-ptr \
lifecycle \