aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--common/makefile39
-rw-r--r--common/relationship-query/driver.cxx175
-rw-r--r--common/relationship-query/makefile109
-rw-r--r--common/relationship-query/test.hxx140
-rw-r--r--common/relationship-query/test.std0
5 files changed, 444 insertions, 19 deletions
diff --git a/common/makefile b/common/makefile
index a2f4962..371ad13 100644
--- a/common/makefile
+++ b/common/makefile
@@ -5,25 +5,26 @@
include $(dir $(lastword $(MAKEFILE_LIST)))../build/bootstrap.make
-tests := \
-auto \
-callback \
-composite \
-const \
-container \
-ctor \
-default \
-enum \
-erase-query \
-inheritance \
-inverse \
-lazy-ptr \
-lifecycle \
-query \
-relationship \
-schema \
-template \
-view \
+tests := \
+auto \
+callback \
+composite \
+const \
+container \
+ctor \
+default \
+enum \
+erase-query \
+inheritance \
+inverse \
+lazy-ptr \
+lifecycle \
+query \
+relationship \
+relationship-query \
+schema \
+template \
+view \
wrapper
thread_tests := threads
diff --git a/common/relationship-query/driver.cxx b/common/relationship-query/driver.cxx
new file mode 100644
index 0000000..57a6215
--- /dev/null
+++ b/common/relationship-query/driver.cxx
@@ -0,0 +1,175 @@
+// file : common/relationship-query/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 relationship queries.
+//
+
+#include <memory> // std::auto_ptr
+#include <cassert>
+#include <iostream>
+
+#include <odb/session.hxx>
+#include <odb/database.hxx>
+#include <odb/transaction.hxx>
+
+#include <common/config.hxx> // HAVE_TR1_MEMORY
+#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));
+
+#ifdef HAVE_TR1_MEMORY
+
+ using std::tr1::shared_ptr;
+
+ //
+ //
+ {
+ shared_ptr<country> ca (new country ("CA", "Canada"));
+ shared_ptr<country> za (new country ("ZA", "South Africa"));
+ shared_ptr<country> us (new country ("US", "United States"));
+ shared_ptr<country> se (new country ("SE", "Sweden"));
+
+ shared_ptr<employer> st (new employer ("Simple Tech, Inc"));
+ shared_ptr<employer> ct (new employer ("Complex Tech, Inc"));
+
+ // person
+ //
+ shared_ptr<person> p1 (
+ new person (1, "John", "Doe", 30, ca, true, za));
+
+ shared_ptr<person> p2 (
+ new person (2, "Jane", "Doe", 29, za, false, us));
+ p2->husband = p1;
+
+ shared_ptr<person> p3 (
+ new person (3, "Joe", "Dirt", 31, us, true, us));
+
+ shared_ptr<person> p4 (
+ new person (4, "Johan", "Johansen", 32, se, false, ca));
+
+ // employee
+ //
+ shared_ptr<employee> e1 (
+ new employee (1, "John", "Doe", 30, ca, true, za, st));
+
+ shared_ptr<employee> e2 (
+ new employee (2, "Jane", "Doe", 29, za, false, us, ct));
+ e2->husband = p1;
+
+ shared_ptr<employee> e3 (
+ new employee (3, "Joe", "Dirt", 31, us, true, us, st));
+
+ shared_ptr<employee> e4 (
+ new employee (4, "Johan", "Johansen", 32, se, false, ca, ct));
+
+ transaction t (db->begin ());
+ db->persist (ca);
+ db->persist (za);
+ db->persist (us);
+ db->persist (se);
+
+ db->persist (st);
+ db->persist (ct);
+
+ db->persist (p1);
+ db->persist (p2);
+ db->persist (p3);
+ db->persist (p4);
+
+ db->persist (e1);
+ db->persist (e2);
+ db->persist (e3);
+ db->persist (e4);
+ t.commit ();
+ }
+
+ typedef odb::query<person> p_query;
+ typedef odb::result<person> p_result;
+
+ typedef odb::query<employee> e_query;
+ typedef odb::result<employee> e_result;
+
+ // Make sure we have an independent JOIN for each relationship.
+ //
+ {
+ session s;
+ transaction t (db->begin ());
+
+ p_result pr (db->query<person> (
+ p_query::residence::location::code == "ZA"));
+ assert (size (pr) == 1);
+
+ e_result er (db->query<employee> (
+ e_query::residence::location::code == "ZA"));
+ assert (size (er) == 1);
+
+ t.commit ();
+ }
+
+ // Test Self-JOIN.
+ //
+ {
+ session s;
+ transaction t (db->begin ());
+
+ p_result pr (db->query<person> (p_query::husband::last_name == "Doe"));
+ assert (size (pr) == 1);
+
+ e_result er (db->query<employee> (e_query::husband::last_name == "Doe"));
+ assert (size (er) == 1);
+
+ t.commit ();
+ }
+
+ // Test query conditions from both base and derived.
+ //
+ {
+ session s;
+ transaction t (db->begin ());
+
+ e_result r (
+ db->query<employee> (
+ e_query::employed_by::name == "Simple Tech, Inc" &&
+ e_query::nationality::code == "US"));
+
+ assert (size (r) == 1);
+
+ t.commit ();
+ }
+
+ // Test second-level pointers.
+ //
+ {
+ session s;
+ transaction t (db->begin ());
+
+ p_result r (
+ db->query<person> (
+ p_query::husband::residence::location == "CA"));
+
+ assert (size (r) == 1);
+
+ t.commit ();
+ }
+
+#endif
+ }
+ catch (const odb::exception& e)
+ {
+ cerr << e.what () << endl;
+ return 1;
+ }
+}
diff --git a/common/relationship-query/makefile b/common/relationship-query/makefile
new file mode 100644
index 0000000..f422a8a
--- /dev/null
+++ b/common/relationship-query/makefile
@@ -0,0 +1,109 @@
+# file : common/relationship-query/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 --table-prefix common_relationship_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/relationship-query/test.hxx b/common/relationship-query/test.hxx
new file mode 100644
index 0000000..c832405
--- /dev/null
+++ b/common/relationship-query/test.hxx
@@ -0,0 +1,140 @@
+// file : common/relationship-query/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 <common/config.hxx> // HAVE_TR1_MEMORY
+
+#ifdef HAVE_TR1_MEMORY
+
+#include <string>
+
+#include <odb/core.hxx>
+#include <odb/tr1/memory.hxx>
+
+struct country;
+
+#pragma db value
+struct residence_info
+{
+ residence_info (bool p, std::tr1::shared_ptr<country> l)
+ : permanent (p), location (l)
+ {
+ }
+
+ residence_info ()
+ {
+ }
+
+ bool permanent;
+
+ #pragma db not_null
+ std::tr1::shared_ptr<country> location;
+};
+
+#pragma db object pointer(std::tr1::shared_ptr)
+struct person
+{
+ person (unsigned long i,
+ const std::string& fn,
+ const std::string& ln,
+ unsigned short a,
+ std::tr1::shared_ptr<country> r,
+ bool p,
+ std::tr1::shared_ptr<country> n)
+ : id (i),
+ first_name (fn),
+ last_name (ln),
+ age (a),
+ residence (p, r),
+ nationality (n)
+ {
+ }
+
+ person ()
+ {
+ }
+
+ #pragma db id
+ unsigned long id;
+
+ #pragma db column ("first")
+ std::string first_name;
+
+ #pragma db column ("last")
+ std::string last_name;
+
+ unsigned short age;
+
+ residence_info residence;
+
+ #pragma db not_null
+ std::tr1::shared_ptr<country> nationality;
+
+ std::tr1::shared_ptr<person> husband; // Self-join.
+};
+
+struct employer;
+
+#pragma db object pointer(std::tr1::shared_ptr)
+struct employee: person
+{
+ employee (unsigned long i,
+ const std::string& fn,
+ const std::string& ln,
+ unsigned short a,
+ std::tr1::shared_ptr<country> r,
+ bool p,
+ std::tr1::shared_ptr<country> n,
+ std::tr1::shared_ptr<employer> e)
+ : person (i, fn, ln, a, r, p, n),
+ employed_by (e)
+ {
+ }
+
+ employee ()
+ {
+ }
+
+ std::tr1::shared_ptr<employer> employed_by;
+};
+
+#pragma db object pointer(std::tr1::shared_ptr)
+struct employer
+{
+ employer (const std::string& n)
+ : name (n)
+ {
+ }
+
+ employer ()
+ {
+ }
+
+ #pragma db id
+ std::string name;
+};
+
+#pragma db object pointer(std::tr1::shared_ptr)
+struct country
+{
+ country (const std::string& c, std::string const& n)
+ : code (c), name (n)
+ {
+ }
+
+ country ()
+ {
+ }
+
+ #pragma db id
+ std::string code; // ISO 2-letter country code.
+
+ std::string name;
+};
+
+#endif // HAVE_TR1_MEMORY
+#endif // TEST_HXX
diff --git a/common/relationship-query/test.std b/common/relationship-query/test.std
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/common/relationship-query/test.std