aboutsummaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2010-11-17 18:05:06 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2010-11-17 18:05:06 +0200
commitebb443f0f2d0cbcb7cc2bc0b48aeef9ee314f7bb (patch)
tree003ae9b2c7e9cb20bd7b54decff7e40d44528714 /common
parent57653f406505d00c3aaae2c3ba4462ca65637da3 (diff)
Add support for unidirectional object relationships
New test: common/relationship.
Diffstat (limited to 'common')
-rw-r--r--common/makefile7
-rw-r--r--common/relationship/driver.cxx129
-rw-r--r--common/relationship/makefile105
-rw-r--r--common/relationship/test.hxx250
-rw-r--r--common/relationship/test.std0
-rw-r--r--common/relationship/tr1-memory.hxx44
6 files changed, 532 insertions, 3 deletions
diff --git a/common/makefile b/common/makefile
index e6675bc..e7878e7 100644
--- a/common/makefile
+++ b/common/makefile
@@ -10,10 +10,11 @@ auto \
composite \
container \
ctor \
-schema \
-template \
lifecycle \
-query
+query \
+relationship \
+schema \
+template
thread_tests := threads
diff --git a/common/relationship/driver.cxx b/common/relationship/driver.cxx
new file mode 100644
index 0000000..421b909
--- /dev/null
+++ b/common/relationship/driver.cxx
@@ -0,0 +1,129 @@
+// file : common/relationship/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 object relationships.
+//
+
+#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;
+
+int
+main (int argc, char* argv[])
+{
+ try
+ {
+ auto_ptr<database> db (create_database (argc, argv));
+
+ aggr a ("aggr");
+ a.o1 = new obj1 ("o1", "obj1");
+ a.o2.reset (new obj2 ("obj2"));
+#ifdef HAVE_TR1_MEMORY
+ a.o3.reset (new obj3 ("obj3"));
+
+ a.c.num = 123;
+ a.c.o3.reset (new obj3 ("c"));
+
+ a.cv.push_back (comp (234, obj3_ptr (new obj3 ("cv 0"))));
+ a.cv.push_back (comp (235, obj3_ptr ()));
+ a.cv.push_back (comp (236, obj3_ptr (new obj3 ("cv 2"))));
+#endif
+
+ a.v1.push_back (new obj1 ("v1 0", "v1 0"));
+ a.v1.push_back (0);
+ a.v1.push_back (new obj1 ("v1 2", "v1 2"));
+
+ a.s1.insert (new obj1 ("s1 0", "s1 0"));
+ a.s1.insert (0);
+ a.s1.insert (new obj1 ("s1 2", "s1 2"));
+
+ a.m1[0] = new obj1 ("m1 0", "m1 0");
+ a.m1[1] = 0;
+ a.m1[2] = new obj1 ("m1 2", "m1 2");
+
+ // persist
+ //
+ {
+ transaction t (db->begin ());
+ db->persist (*a.o1);
+ db->persist (*a.o2);
+#ifdef HAVE_TR1_MEMORY
+ db->persist (*a.o3);
+
+ db->persist (*a.c.o3);
+
+ for (comp_vec::iterator i (a.cv.begin ()); i != a.cv.end (); ++i)
+ if (i->o3)
+ db->persist (*i->o3);
+#endif
+
+ for (obj1_vec::iterator i (a.v1.begin ()); i != a.v1.end (); ++i)
+ if (*i)
+ db->persist (**i);
+
+ for (obj1_set::iterator i (a.s1.begin ()); i != a.s1.end (); ++i)
+ if (*i)
+ db->persist (**i);
+
+ for (obj1_map::iterator i (a.m1.begin ()); i != a.m1.end (); ++i)
+ if (i->second)
+ db->persist (*i->second);
+
+ db->persist (a);
+ t.commit ();
+ }
+
+ // load & compare
+ //
+ {
+ transaction t (db->begin ());
+ auto_ptr<aggr> a1 (db->load<aggr> (a.id));
+ t.commit ();
+
+ assert (*a1 == a);
+ }
+
+ // test NULL pointer
+ //
+ delete a.o1;
+ a.o1 = 0;
+ a.o2.reset ();
+#ifdef HAVE_TR1_MEMORY
+ a.o3.reset ();
+#endif
+
+ {
+ transaction t (db->begin ());
+ db->update (a);
+ t.commit ();
+ }
+
+ // load & compare
+ //
+ {
+ transaction t (db->begin ());
+ auto_ptr<aggr> a1 (db->load<aggr> (a.id));
+ t.commit ();
+
+ assert (*a1 == a);
+ }
+ }
+ catch (const odb::exception& e)
+ {
+ cerr << e.what () << endl;
+ return 1;
+ }
+}
diff --git a/common/relationship/makefile b/common/relationship/makefile
new file mode 100644
index 0000000..82f2f28
--- /dev/null
+++ b/common/relationship/makefile
@@ -0,0 +1,105 @@
+# file : common/relationship/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
+$(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/relationship/test.hxx b/common/relationship/test.hxx
new file mode 100644
index 0000000..3f624e5
--- /dev/null
+++ b/common/relationship/test.hxx
@@ -0,0 +1,250 @@
+// file : common/relationship/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 <map>
+#include <vector>
+#include <string>
+#include <memory>
+#include "tr1-memory.hxx"
+
+#include <odb/core.hxx>
+
+// Naked pointer.
+//
+struct obj1;
+typedef obj1* obj1_ptr;
+
+#pragma db object pointer(obj1_ptr)
+struct obj1
+{
+ obj1 () {}
+ obj1 (const std::string& i, const std::string& s): id (i), str (s) {}
+
+ #pragma db id
+ std::string id;
+
+ std::string str;
+};
+
+inline bool
+operator== (const obj1& x, const obj1& y)
+{
+ return x.id == y.id && x.str == y.str;
+}
+
+// vector
+//
+typedef std::vector<obj1_ptr> obj1_vec;
+
+inline bool
+operator== (const obj1_vec& x, const obj1_vec& y)
+{
+ if (x.size () != y.size ())
+ return false;
+
+ for (obj1_vec::size_type i (0); i < x.size (); ++i)
+ if (!(x[i] ? (y[i] && *x[i] == *y[i]) : !y[i]))
+ return false;
+
+ return true;
+}
+
+// set
+//
+struct obj1_ptr_cmp
+{
+ bool
+ operator() (obj1_ptr x, obj1_ptr y) const
+ {
+ return (!x || !y) ? x < y : x->id < y->id;
+ }
+};
+
+typedef std::set<obj1_ptr, obj1_ptr_cmp> obj1_set;
+
+inline bool
+operator== (const obj1_set& x, const obj1_set& y)
+{
+ if (x.size () != y.size ())
+ return false;
+
+ for (obj1_set::const_iterator i (x.begin ()); i != x.end (); ++i)
+ {
+ obj1_set::const_iterator j (y.find (*i));
+
+ if (j == y.end ())
+ return false;
+
+ obj1_ptr x (*i), y (*j);
+
+ if (!(x ? (y && *x == *y) : !y))
+ return false;
+ }
+
+ return true;
+}
+
+// map
+//
+typedef std::map<int, obj1_ptr> obj1_map;
+
+inline bool
+operator== (const obj1_map& x, const obj1_map& y)
+{
+ if (x.size () != y.size ())
+ return false;
+
+ for (obj1_map::const_iterator i (x.begin ()); i != x.end (); ++i)
+ {
+ obj1_map::const_iterator j (y.find (i->first));
+
+ if (j == y.end ())
+ return false;
+
+ obj1_ptr x (i->second), y (j->second);
+
+ if (!(x ? (y && *x == *y) : !y))
+ return false;
+ }
+
+ return true;
+}
+
+// auto_ptr
+//
+struct obj2;
+typedef std::auto_ptr<obj2> obj2_ptr;
+
+#pragma db object pointer(obj2_ptr)
+struct obj2
+{
+ obj2 () {}
+ obj2 (const std::string& s): str (s) {}
+
+ #pragma db id auto
+ unsigned long id;
+
+ std::string str;
+};
+
+inline bool
+operator== (const obj2& x, const obj2& y)
+{
+ return x.id == y.id && x.str == y.str;
+}
+
+// tr1::shared_ptr
+//
+#ifdef HAVE_TR1_MEMORY
+struct obj3;
+typedef std::tr1::shared_ptr<obj3> obj3_ptr;
+
+#pragma db object pointer(obj3_ptr)
+struct obj3
+{
+ obj3 () {}
+ obj3 (const std::string& s): str (s) {}
+
+ #pragma db id auto
+ unsigned long id;
+
+ std::string str;
+};
+
+inline bool
+operator== (const obj3& x, const obj3& y)
+{
+ return x.id == y.id && x.str == y.str;
+}
+
+// composite
+//
+#pragma db value
+struct comp
+{
+ comp () {}
+ comp (int n, obj3_ptr o): num (n), o3 (o) {}
+
+ int num;
+ obj3_ptr o3;
+};
+
+inline bool
+operator== (const comp& x, const comp& y)
+{
+ return x.num == y.num &&
+ (x.o3 ? (y.o3 && *x.o3 == *y.o3) : !y.o3);
+}
+
+typedef std::vector<comp> comp_vec;
+#endif
+
+//
+//
+#pragma db object
+struct aggr
+{
+ aggr (): o1 (0) {}
+ aggr (const std::string& s): o1 (0), str (s) {}
+
+ ~aggr ()
+ {
+ delete o1;
+
+ for (obj1_vec::iterator i (v1.begin ()); i != v1.end (); ++i)
+ delete *i;
+
+ for (obj1_set::iterator i (s1.begin ()); i != s1.end (); ++i)
+ delete *i;
+
+ for (obj1_map::iterator i (m1.begin ()); i != m1.end (); ++i)
+ delete i->second;
+ }
+
+ #pragma db id auto
+ unsigned long id;
+
+ obj1_ptr o1;
+ obj2_ptr o2;
+#ifdef HAVE_TR1_MEMORY
+ obj3_ptr o3;
+ comp c;
+ comp_vec cv;
+#endif
+
+ obj1_vec v1;
+ obj1_set s1;
+ obj1_map m1;
+
+ std::string str;
+
+private:
+ aggr (const aggr&);
+ aggr& operator= (const aggr&);
+};
+
+inline bool
+operator== (const aggr& x, const aggr& y)
+{
+ return
+ x.id == y.id &&
+ (x.o1 ? (y.o1 && *x.o1 == *y.o1) : !y.o1) &&
+ (x.o2.get () ? (y.o2.get () && *x.o2 == *y.o2) : !y.o2.get ()) &&
+#ifdef HAVE_TR1_MEMORY
+ (x.o3.get () ? (y.o3.get () && *x.o3 == *y.o3) : !y.o3.get ()) &&
+ x.c == y.c &&
+ x.cv == y.cv &&
+#endif
+ x.v1 == y.v1 &&
+ x.s1 == y.s1 &&
+ x.m1 == y.m1 &&
+ x.str == y.str;
+}
+
+#endif // TEST_HXX
diff --git a/common/relationship/test.std b/common/relationship/test.std
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/common/relationship/test.std
diff --git a/common/relationship/tr1-memory.hxx b/common/relationship/tr1-memory.hxx
new file mode 100644
index 0000000..27e9da8
--- /dev/null
+++ b/common/relationship/tr1-memory.hxx
@@ -0,0 +1,44 @@
+// file : common/relationship/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