From 73bcedf1a812136d338da7f274a37b02d0b30b0a Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 22 Nov 2010 14:20:22 +0200 Subject: Test one-to-{one,many} inverse relationships --- common/inverse/driver.cxx | 163 ++++++++++++++++++++++++++++++++++++++++++ common/inverse/makefile | 106 +++++++++++++++++++++++++++ common/inverse/test.hxx | 132 ++++++++++++++++++++++++++++++++++ common/inverse/test.std | 0 common/inverse/tr1-memory.hxx | 44 ++++++++++++ 5 files changed, 445 insertions(+) create mode 100644 common/inverse/driver.cxx create mode 100644 common/inverse/makefile create mode 100644 common/inverse/test.hxx create mode 100644 common/inverse/test.std create mode 100644 common/inverse/tr1-memory.hxx (limited to 'common/inverse') 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 +// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +// Test bidirectional relationships with inverse sides. +// + +#include // std::auto_ptr +#include +#include + +#include +#include +#include + +#include + +#include "test.hxx" +#include "test-odb.hxx" + +using namespace std; +using namespace odb; + +int +main (int argc, char* argv[]) +{ + try + { + auto_ptr 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 (o2->id)); + obj3_ptr x3_1 (db->load (o3_1->id)); + obj3_ptr x3_2 (db->load (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 (o2->id)); + tr1_obj3_ptr x3_1 (db->load (o3_1->id)); + tr1_obj3_ptr x3_2 (db->load (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 +# 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 +// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include +#include +#include +#include +#include "tr1-memory.hxx" + +#include + +struct obj1; +struct obj2; +struct obj3; + +typedef obj1* obj1_ptr; +typedef obj2* obj2_ptr; +typedef obj3* obj3_ptr; + +typedef std::set 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_ptr; +typedef std::tr1::shared_ptr tr1_obj2_ptr; +typedef std::tr1::shared_ptr tr1_obj3_ptr; + +typedef std::tr1::weak_ptr tr1_obj1_wptr; + +typedef std::vector 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 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 +// 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 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 // __GLIBCXX__, _HAS_TR1 + +// GNU C++ or Intel C++ using libstd++. +// +#if defined (__GNUC__) && __GNUC__ >= 4 && defined (__GLIBCXX__) +# include +# define HAVE_TR1_MEMORY 1 +// +// IBM XL C++. +// +#elif defined (__xlC__) && __xlC__ >= 0x0900 +# define __IBMCPP_TR1__ +# include +# 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 +# define HAVE_TR1_MEMORY 1 +// +// Boost fall-back. +// +#elif defined (HAVE_BOOST_TR1) +# include +# define HAVE_TR1_MEMORY 1 +#endif + +#endif // TR1_MEMORY_HXX -- cgit v1.1