aboutsummaryrefslogtreecommitdiff
path: root/common/inverse
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2010-11-22 14:20:22 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2010-11-22 14:20:22 +0200
commit73bcedf1a812136d338da7f274a37b02d0b30b0a (patch)
treed6b3c1f80839474c304b3b04980fd180022f9def /common/inverse
parentececaecd27d0a9f3d52a5886f68bc8eac4fc5227 (diff)
Test one-to-{one,many} inverse relationships
Diffstat (limited to 'common/inverse')
-rw-r--r--common/inverse/driver.cxx163
-rw-r--r--common/inverse/makefile106
-rw-r--r--common/inverse/test.hxx132
-rw-r--r--common/inverse/test.std0
-rw-r--r--common/inverse/tr1-memory.hxx44
5 files changed, 445 insertions, 0 deletions
diff --git a/common/inverse/driver.cxx b/common/inverse/driver.cxx
new file mode 100644
index 0000000..e3c8b22
--- /dev/null
+++ b/common/inverse/driver.cxx
@@ -0,0 +1,163 @@
+// file : common/inverse/driver.cxx
+// author : Boris Kolpackov <boris@codesynthesis.com>
+// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC
+// license : GNU GPL v2; see accompanying LICENSE file
+
+// Test bidirectional relationships with inverse sides.
+//
+
+#include <memory> // std::auto_ptr
+#include <cassert>
+#include <iostream>
+
+#include <odb/database.hxx>
+#include <odb/transaction.hxx>
+#include <odb/session.hxx>
+
+#include <common/common.hxx>
+
+#include "test.hxx"
+#include "test-odb.hxx"
+
+using namespace std;
+using namespace odb;
+
+int
+main (int argc, char* argv[])
+{
+ try
+ {
+ auto_ptr<database> db (create_database (argc, argv));
+
+ // Naked pointer version.
+ //
+ {
+ obj1_ptr o1 (new obj1);
+ obj2_ptr o2 (new obj2);
+ obj3_ptr o3_1 (new obj3);
+ obj3_ptr o3_2 (new obj3);
+
+ o1->id = "obj1";
+ o1->o2 = o2;
+ o1->o3.insert (o3_1);
+ o1->o3.insert (o3_2);
+
+ o2->str = "obj2";
+ o2->o1 = o1;
+
+ o3_1->str = "obj3 1";
+ o3_1->o1 = o1;
+
+ o3_2->str = "obj3 3";
+ o3_2->o1 = o1;
+
+ // persist
+ //
+ {
+ transaction t (db->begin ());
+
+ // obj2 and obj3 come first to get object id assigned.
+ //
+ db->persist (o3_1);
+ db->persist (o3_2);
+ db->persist (o2);
+ db->persist (o1);
+
+ t.commit ();
+ }
+
+ // load
+ //
+ {
+ session s;
+ transaction t (db->begin ());
+ obj2_ptr x2 (db->load<obj2> (o2->id));
+ obj3_ptr x3_1 (db->load<obj3> (o3_1->id));
+ obj3_ptr x3_2 (db->load<obj3> (o3_2->id));
+ t.commit ();
+
+ assert (x2->str == o2->str);
+ assert (x2->o1->id == o1->id);
+ assert (x2->o1->o2 == x2);
+
+ assert (x3_1->str == o3_1->str);
+ assert (x3_2->str == o3_2->str);
+ assert (x3_1->o1 == x3_2->o1);
+ assert (x3_1->o1->id == o1->id);
+ assert (x3_1->o1->o3.find (x3_1) != x3_1->o1->o3.end ());
+ assert (x3_1->o1->o3.find (x3_2) != x3_1->o1->o3.end ());
+
+ delete x2->o1;
+ }
+
+ delete o1;
+ }
+
+ // TR1 pointer version.
+ //
+#ifdef HAVE_TR1_MEMORY
+ {
+ tr1_obj1_ptr o1 (new tr1_obj1);
+ tr1_obj2_ptr o2 (new tr1_obj2);
+ tr1_obj3_ptr o3_1 (new tr1_obj3);
+ tr1_obj3_ptr o3_2 (new tr1_obj3);
+
+ o1->id = "obj1";
+ o1->o2 = o2;
+ o1->o3.push_back (o3_1);
+ o1->o3.push_back (o3_2);
+
+ o2->str = "obj2";
+ o2->o1 = o1;
+
+ o3_1->str = "obj3 1";
+ o3_1->o1 = o1;
+
+ o3_2->str = "obj3 3";
+ o3_2->o1 = o1;
+
+ // persist
+ //
+ {
+ transaction t (db->begin ());
+
+ // obj2 and obj3 come first to get object id assigned.
+ //
+ db->persist (o3_1);
+ db->persist (o3_2);
+ db->persist (o2);
+ db->persist (o1);
+
+ t.commit ();
+ }
+
+ // load
+ //
+ {
+ session s;
+ transaction t (db->begin ());
+ tr1_obj2_ptr x2 (db->load<tr1_obj2> (o2->id));
+ tr1_obj3_ptr x3_1 (db->load<tr1_obj3> (o3_1->id));
+ tr1_obj3_ptr x3_2 (db->load<tr1_obj3> (o3_2->id));
+ t.commit ();
+
+ assert (x2->str == o2->str);
+ assert (x2->o1.lock ()->id == o1->id);
+ assert (x2->o1.lock ()->o2 == x2);
+
+ assert (x3_1->str == o3_1->str);
+ assert (x3_2->str == o3_2->str);
+ assert (x3_1->o1.lock () == x3_2->o1.lock ());
+ assert (x3_1->o1.lock ()->id == o1->id);
+ assert (x3_1->o1.lock ()->o3[0] == x3_1);
+ assert (x3_1->o1.lock ()->o3[1] == x3_2);
+ }
+ }
+#endif
+ }
+ catch (const odb::exception& e)
+ {
+ cerr << e.what () << endl;
+ return 1;
+ }
+}
diff --git a/common/inverse/makefile b/common/inverse/makefile
new file mode 100644
index 0000000..0636dec
--- /dev/null
+++ b/common/inverse/makefile
@@ -0,0 +1,106 @@
+# file : common/inverse/makefile
+# author : Boris Kolpackov <boris@codesynthesis.com>
+# copyright : Copyright (c) 2009-2010 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)
+$(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$(out_base)
+$(gen): $(common.l.cpp-options)
+
+$(call include-dep,$(cxx_od),$(cxx_obj),$(gen))
+
+# Alias for default target.
+#
+$(out_base)/: $(driver)
+
+# Dist
+#
+name := $(notdir $(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 message,sql $$1,$(dcf_root)/db-driver $$1, $(src_base)/test.sql)
+ $(call message,test $<,$< --options-file $(dcf_root)/db.options \
+| diff -u $(src_base)/test.std -)
+
+# 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))
+
+# 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/inverse/test.hxx b/common/inverse/test.hxx
new file mode 100644
index 0000000..df93e5d
--- /dev/null
+++ b/common/inverse/test.hxx
@@ -0,0 +1,132 @@
+// file : common/inverse/test.hxx
+// author : Boris Kolpackov <boris@codesynthesis.com>
+// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC
+// license : GNU GPL v2; see accompanying LICENSE file
+
+#ifndef TEST_HXX
+#define TEST_HXX
+
+#include <set>
+#include <vector>
+#include <string>
+#include <memory>
+#include "tr1-memory.hxx"
+
+#include <odb/core.hxx>
+
+struct obj1;
+struct obj2;
+struct obj3;
+
+typedef obj1* obj1_ptr;
+typedef obj2* obj2_ptr;
+typedef obj3* obj3_ptr;
+
+typedef std::set<obj3_ptr> obj3_ptr_set;
+
+#pragma db object
+struct obj1
+{
+ obj1 (): o2 (0) {}
+ ~obj1 ();
+
+ #pragma db id
+ std::string id;
+
+ obj2_ptr o2;
+
+ #pragma db id_column("obj1_id") value_column("obj3_id")
+ obj3_ptr_set o3;
+};
+
+#pragma db object
+struct obj2
+{
+ #pragma db id auto
+ int id;
+
+ std::string str;
+
+ // one-to-one
+ //
+ #pragma db inverse(o2)
+ obj1_ptr o1;
+};
+
+#pragma db object
+struct obj3
+{
+ #pragma db id auto
+ int id;
+
+ std::string str;
+
+ // one-to-many
+ //
+ #pragma db inverse (o3)
+ obj1_ptr o1;
+};
+
+inline obj1::
+~obj1 ()
+{
+ delete o2;
+ for (obj3_ptr_set::iterator i (o3.begin ()); i != o3.end (); ++i)
+ delete *i;
+}
+
+// TR1 version
+//
+#ifdef HAVE_TR1_MEMORY
+struct tr1_obj1;
+struct tr1_obj2;
+struct tr1_obj3;
+
+typedef std::tr1::shared_ptr<tr1_obj1> tr1_obj1_ptr;
+typedef std::tr1::shared_ptr<tr1_obj2> tr1_obj2_ptr;
+typedef std::tr1::shared_ptr<tr1_obj3> tr1_obj3_ptr;
+
+typedef std::tr1::weak_ptr<tr1_obj1> tr1_obj1_wptr;
+
+typedef std::vector<tr1_obj3_ptr> tr1_obj3_ptr_vec;
+
+#pragma db object pointer(tr1_obj1_ptr)
+struct tr1_obj1
+{
+ #pragma db id
+ std::string id;
+
+ tr1_obj2_ptr o2;
+
+ #pragma db id_column("tr1_obj1_id") value_column("tr1_obj3_id")
+ tr1_obj3_ptr_vec o3;
+};
+
+#pragma db object pointer(tr1_obj2_ptr)
+struct tr1_obj2
+{
+ #pragma db id auto
+ int id;
+
+ std::string str;
+
+ #pragma db inverse(o2)
+ tr1_obj1_wptr o1;
+};
+
+#pragma db object pointer(tr1_obj3_ptr)
+struct tr1_obj3
+{
+ #pragma db id auto
+ int id;
+
+ std::string str;
+
+ // one-to-many
+ //
+ #pragma db inverse (o3)
+ tr1_obj1_wptr o1;
+};
+#endif
+
+#endif // TEST_HXX
diff --git a/common/inverse/test.std b/common/inverse/test.std
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/common/inverse/test.std
diff --git a/common/inverse/tr1-memory.hxx b/common/inverse/tr1-memory.hxx
new file mode 100644
index 0000000..76ac932
--- /dev/null
+++ b/common/inverse/tr1-memory.hxx
@@ -0,0 +1,44 @@
+// file : common/inverse/tr1-memory.hxx
+// author : Boris Kolpackov <boris@codesynthesis.com>
+// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC
+// license : GNU GPL v2; see accompanying LICENSE file
+
+#ifndef TR1_MEMORY_HXX
+#define TR1_MEMORY_HXX
+
+// Try to include TR1 <memory> in a compiler-specific manner. Define
+// HAVE_TR1_MEMORY if successfull. If the compiler does not provide
+// native TR1 support, fall-back on the Boost TR1 implementation if
+// HAVE_BOOST_TR1 is defined.
+//
+
+#include <cstddef> // __GLIBCXX__, _HAS_TR1
+
+// GNU C++ or Intel C++ using libstd++.
+//
+#if defined (__GNUC__) && __GNUC__ >= 4 && defined (__GLIBCXX__)
+# include <tr1/memory>
+# define HAVE_TR1_MEMORY 1
+//
+// IBM XL C++.
+//
+#elif defined (__xlC__) && __xlC__ >= 0x0900
+# define __IBMCPP_TR1__
+# include <memory>
+# define HAVE_TR1_MEMORY 1
+//
+// VC++ or Intel C++ using VC++ standard library.
+//
+#elif defined (_MSC_VER) && \
+ (_MSC_VER == 1500 && defined (_HAS_TR1) || _MSC_VER > 1500)
+# include <memory>
+# define HAVE_TR1_MEMORY 1
+//
+// Boost fall-back.
+//
+#elif defined (HAVE_BOOST_TR1)
+# include <boost/tr1/memory.hpp>
+# define HAVE_TR1_MEMORY 1
+#endif
+
+#endif // TR1_MEMORY_HXX