aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2011-01-13 11:31:14 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2011-01-13 11:31:14 +0200
commitab51fa65f9e8cad4ef5a1db85029dfe6404e9a1f (patch)
tree36c78dd50d88e4797f2bbb886c41399006111fea
parent5511613df7dce6142a84111488aaa25ff792d66b (diff)
Add composite, relationship, and inverse examples
All add the TR1 <memory> test for the latter two examples.
-rw-r--r--Makefile.am4
-rw-r--r--README11
-rw-r--r--composite/README56
-rw-r--r--composite/database.hxx46
-rw-r--r--composite/driver.cxx101
-rw-r--r--composite/makefile118
-rw-r--r--composite/person.hxx165
-rw-r--r--configure.ac4
-rw-r--r--inverse/README67
-rw-r--r--inverse/database.hxx46
-rw-r--r--inverse/driver.cxx258
-rw-r--r--inverse/employee.hxx275
-rw-r--r--inverse/makefile118
-rw-r--r--m4/tr1-memory.m440
-rw-r--r--makefile19
-rw-r--r--relationship/README63
-rw-r--r--relationship/database.hxx46
-rw-r--r--relationship/driver.cxx168
-rw-r--r--relationship/employee.hxx161
-rw-r--r--relationship/makefile118
20 files changed, 1876 insertions, 8 deletions
diff --git a/Makefile.am b/Makefile.am
index 8035971..32f52bd 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -5,5 +5,9 @@
SUBDIRS = __path__(dirs)
+if HAVE_TR1_MEMORY
+SUBDIRS += __path__(tr1_dirs)
+endif
+
EXTRA_DIST = __file__(extra_dist)
ACLOCAL_AMFLAGS = -I m4
diff --git a/README b/README
index 139d31e..18c14de 100644
--- a/README
+++ b/README
@@ -18,9 +18,20 @@ query
Shows how to use the ODB Query Language to search the database for
persistent objects matching certain criteria.
+composite
+ Shows how to declare and use composite value types.
+
container
Shows how to use containers as data members in persistent objects.
+relationship
+ Shows how to declare and use unidirectional to-one and to-many
+ relationships.
+
+inverse
+ Shows how to declare and use bidirectional one-to-one, one-to-many, and
+ many-to-many relationships.
+
mapping
Shows how to customize the mapping between C++ value types and database
types.
diff --git a/composite/README b/composite/README
new file mode 100644
index 0000000..fc508bb
--- /dev/null
+++ b/composite/README
@@ -0,0 +1,56 @@
+This example shows how to use composite value types as data members in objects
+and other value types, as element types in containers, and as base types for
+other composite value types. It also shows how to use composite value type
+data members in queries.
+
+The example consists of the following files:
+
+person.hxx
+ Header file defining the 'basic_name', 'name_extras', and 'name' composite
+ value types. It also defines the 'person' persistent class which use the
+ 'name' value type in one of its data members.
+
+person-odb.hxx
+person-odb.ixx
+person-odb.cxx
+person.sql
+ The first three files contain the database support code and the last file
+ contains the database schema for the person.hxx header.
+
+ These files are generated by the ODB compiler from person.hxx using the
+ following command line:
+
+ odb -d <database> --generate-schema --generate-query person.hxx
+
+ Where <database> stands for the database system we are using, for example,
+ 'mysql'.
+
+database.hxx
+ Contains the create_database() function which instantiates the concrete
+ database class corresponding to the database system we are using.
+
+driver.cxx
+ Driver for the example. It includes the person.hxx and person-odb.hxx
+ headers to gain access to the 'person' class and the database support
+ code for this class. It also includes database.hxx for the
+ create_database() function declaration.
+
+ In main() the driver first calls create_database() to obtain the database
+ instance. It then persists a 'person' object, loads it and updates its
+ nickname and aliases which reside in a composite value type, then re-loads
+ the object and prints its name to verify that the changes have been made
+ persistent. Finally, the driver performs a database query which uses a
+ data member from the composite value type in its criterion.
+
+To run the example we first need to create the database schema. Using MySQL
+as an example, this can be achieved with the following command:
+
+mysql --user=odb_test --database=odb_test < person.sql
+
+Here we use 'odb_test' as the database login and also 'odb_test' as the
+database name.
+
+Once the database schema is ready, we can run the example (using MySQL as
+the database):
+
+./driver --user odb_test --database odb_test
diff --git a/composite/database.hxx b/composite/database.hxx
new file mode 100644
index 0000000..c21c9fc
--- /dev/null
+++ b/composite/database.hxx
@@ -0,0 +1,46 @@
+// file : composite/database.hxx
+// author : Boris Kolpackov <boris@codesynthesis.com>
+// copyright : not copyrighted - public domain
+
+//
+// Create concrete database instance based on the DATABASE_* macros.
+//
+
+#ifndef DATABASE_HXX
+#define DATABASE_HXX
+
+#include <string>
+#include <memory> // std::auto_ptr
+#include <cstdlib> // std::exit
+#include <iostream>
+
+#include <odb/database.hxx>
+
+#if defined(DATABASE_MYSQL)
+# include <odb/mysql/database.hxx>
+#endif
+
+inline std::auto_ptr<odb::database>
+create_database (int& argc, char* argv[])
+{
+ using namespace std;
+ using namespace odb;
+
+ if (argc > 1 && argv[1] == string ("--help"))
+ {
+ cerr << "Usage: " << argv[0] << " [options]" << endl
+ << "Options:" << endl;
+
+#if defined(DATABASE_MYSQL)
+ mysql::database::print_usage (cerr);
+#endif
+
+ exit (0);
+ }
+
+#if defined(DATABASE_MYSQL)
+ return auto_ptr<database> (new mysql::database (argc, argv));
+#endif
+}
+
+#endif // DATABASE_HXX
diff --git a/composite/driver.cxx b/composite/driver.cxx
new file mode 100644
index 0000000..f8feb2f
--- /dev/null
+++ b/composite/driver.cxx
@@ -0,0 +1,101 @@
+// file : composite/driver.cxx
+// author : Boris Kolpackov <boris@codesynthesis.com>
+// copyright : not copyrighted - public domain
+
+#include <memory> // std::auto_ptr
+#include <iostream>
+
+#include <odb/database.hxx>
+#include <odb/transaction.hxx>
+
+#include "database.hxx" // create_database
+
+#include "person.hxx"
+#include "person-odb.hxx"
+
+using namespace std;
+using namespace odb;
+
+int
+main (int argc, char* argv[])
+{
+ try
+ {
+ auto_ptr<database> db (create_database (argc, argv));
+
+ // Create a person object.
+ //
+ unsigned int id;
+ {
+ person p ("Joe", "Dirt", "Mr");
+
+ transaction t (db->begin ());
+ id = db->persist (p);
+ t.commit ();
+ }
+
+ // Update the extra name information.
+ //
+ {
+ transaction t (db->begin ());
+
+ auto_ptr<person> joe (db->load<person> (id));
+ name_extras& ne (joe->name ().extras ());
+ ne.nickname ("Squeaky");
+ ne.aliases ().push_back (basic_name ("Anthony", "Clean"));
+
+ db->update (*joe);
+
+ t.commit ();
+ }
+
+ // Print the name information.
+ //
+ {
+ transaction t (db->begin ());
+ auto_ptr<person> joe (db->load<person> (id));
+ t.commit ();
+
+ name& n (joe->name ());
+
+ cout << n.title () << " " << n.first () << " " << n.last () << endl;
+
+ name_extras& ne (n.extras ());
+
+ if (!ne.nickname ().empty ())
+ cout << " nickname: " << ne.nickname () << endl;
+
+ for (basic_names::iterator i (ne.aliases ().begin ());
+ i != ne.aliases ().end ();
+ ++i)
+ {
+ cout << " alias: " << i->first () << " " << i->last () << endl;
+ }
+ }
+
+ // Query the database for a person object.
+ //
+ {
+ typedef odb::query<person> query;
+ typedef odb::result<person> result;
+
+ transaction t (db->begin ());
+
+ result r (db->query<person> (
+ query::name::extras::nickname == "Squeaky"));
+
+ if (!r.empty ())
+ {
+ name& n (r.begin ()->name ());
+ cout << n.title () << " " << n.first () << " " << n.last () << endl;
+ }
+
+ t.commit ();
+ }
+ }
+ catch (const odb::exception& e)
+ {
+ cerr << e.what () << endl;
+ return 1;
+ }
+}
diff --git a/composite/makefile b/composite/makefile
new file mode 100644
index 0000000..dcaeb82
--- /dev/null
+++ b/composite/makefile
@@ -0,0 +1,118 @@
+# file : composite/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 := person.hxx
+cxx_obj := $(addprefix $(out_base)/,$(cxx_tun:.cxx=.o) $(odb_hdr:.hxx=-odb.o))
+cxx_od := $(cxx_obj:.o=.o.d)
+
+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)
+
+$(call import,\
+ $(scf_root)/import/libodb/stub.make,\
+ l: odb.l,cpp-options: odb.l.cpp-options)
+
+ifdef db_id
+$(call import,\
+ $(scf_root)/import/libodb-$(db_id)/stub.make,\
+ l: odb_db.l,cpp-options: odb_db.l.cpp-options)
+endif
+
+ifeq ($(odb_db.l.cpp-options),)
+odb_db.l.cpp-options := $(out_base)/.unbuildable
+endif
+
+# Build.
+#
+$(driver): $(cxx_obj) $(odb_db.l) $(odb.l)
+$(cxx_obj) $(cxx_od): cpp_options := -I$(out_base)
+$(cxx_obj) $(cxx_od): $(odb.l.cpp-options) $(odb_db.l.cpp-options)
+
+ifeq ($(db_id),mysql)
+$(cxx_obj) $(cxx_od): cpp_options += -DDATABASE_MYSQL
+endif
+
+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): $(odb.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): export name := $(name)
+$(dist): export odb_header_stem := $(basename $(odb_hdr))
+$(dist): export extra_dist := README $(call vc9projs,$(name)) \
+$(call vc10projs,$(name))
+$(dist):
+ $(call dist-data,$(sources) $(headers) README database.hxx)
+ $(call meta-automake,../template/Makefile.am)
+ $(call meta-vc9projs,../template/template,$(name))
+ $(call meta-vc10projs,../template/template,$(name))
+
+# Test.
+#
+$(test): schema := $(src_base)/$(basename $(odb_hdr)).sql
+$(test): $(driver)
+ $(call message,sql $$1,$(dcf_root)/db-driver $$1,$(schema))
+ $(call message,test $<,$< --options-file $(dcf_root)/db.options)
+
+# 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)
+
diff --git a/composite/person.hxx b/composite/person.hxx
new file mode 100644
index 0000000..dfc44e6
--- /dev/null
+++ b/composite/person.hxx
@@ -0,0 +1,165 @@
+// file : composite/person.hxx
+// author : Boris Kolpackov <boris@codesynthesis.com>
+// copyright : not copyrighted - public domain
+
+#ifndef PERSON_HXX
+#define PERSON_HXX
+
+#include <vector>
+#include <string>
+
+#include <odb/core.hxx>
+
+#pragma db value
+class basic_name
+{
+public:
+ basic_name (const std::string& first, const std::string& last)
+ : first_ (first), last_ (last)
+ {
+ }
+
+ const std::string&
+ first () const
+ {
+ return first_;
+ }
+
+ const std::string&
+ last () const
+ {
+ return last_;
+ }
+
+private:
+ friend class odb::access;
+
+ basic_name () {} // Needed for storing basic_name in containers.
+
+ std::string first_;
+ std::string last_;
+};
+
+typedef std::vector<basic_name> basic_names;
+
+
+#pragma db value
+class name_extras
+{
+public:
+ // Nickname.
+ //
+ const std::string&
+ nickname () const
+ {
+ return nickname_;
+ }
+
+ void
+ nickname (const std::string& nickname)
+ {
+ nickname_ = nickname;
+ }
+
+ // Aliases.
+ //
+ const basic_names&
+ aliases () const
+ {
+ return aliases_;
+ }
+
+ basic_names&
+ aliases ()
+ {
+ return aliases_;
+ }
+
+private:
+ friend class odb::access;
+
+ std::string nickname_;
+ basic_names aliases_;
+};
+
+
+#pragma db value
+class name: public basic_name
+{
+public:
+ name (const std::string& first,
+ const std::string& last,
+ const std::string& title)
+ : basic_name (first, last), title_ (title)
+ {
+ }
+
+ // Title.
+ //
+ const std::string&
+ title () const
+ {
+ return title_;
+ }
+
+ // Extras.
+ //
+ const name_extras&
+ extras () const
+ {
+ return extras_;
+ }
+
+ name_extras&
+ extras ()
+ {
+ return extras_;
+ }
+
+private:
+ friend class odb::access;
+
+ std::string title_;
+ name_extras extras_;
+};
+
+
+#pragma db object
+class person
+{
+public:
+ person (const std::string& first,
+ const std::string& last,
+ const std::string& title)
+ : name_ (first, last, title)
+ {
+ }
+
+ // Name.
+ //
+ typedef ::name name_type;
+
+ const name_type&
+ name () const
+ {
+ return name_;
+ }
+
+ name_type&
+ name ()
+ {
+ return name_;
+ }
+
+private:
+ friend class odb::access;
+
+ person (): name_ ("", "", "") {}
+
+ #pragma db id auto
+ unsigned long id_;
+
+ name_type name_;
+};
+
+#endif // PERSON_HXX
diff --git a/configure.ac b/configure.ac
index e31a1cc..530b490 100644
--- a/configure.ac
+++ b/configure.ac
@@ -34,6 +34,10 @@ AM_CONDITIONAL([ODB_EXAMPLES_THREADS], [test x$threads != xnone])
#
LIBODB([], [AC_MSG_ERROR([libodb is not found; consider using --with-libodb=DIR])])
+# Check for TR1 <memory> availability.
+#
+TR1_MEMORY
+
# Check which database we are using.
#
DATABASE
diff --git a/inverse/README b/inverse/README
new file mode 100644
index 0000000..2c89c62
--- /dev/null
+++ b/inverse/README
@@ -0,0 +1,67 @@
+This example shows how to declare and use bidirectional one-to-one, one-to-
+many, and many-to-many relationships between persistent objects. It also
+shows how to work with lazy pointers. All the relationships presented in
+this example declare one side as inverse in order to produce canonical
+database schema.
+
+The example uses the shared_ptr and weak_ptr smart pointers from TR1 and
+requires a C++ compiler with TR1 support or an external TR1 implementation,
+such as the one provided by Boost.
+
+The example consists of the following files:
+
+employee.hxx
+ Header file defining the 'employee', 'employer', 'position', and 'project'
+ persistent classes as well as the employer-employee (one-to-many),
+ employee-position (one-to-one), and employee-project (many-to-many)
+ bidirectional relationships between them.
+
+employee-odb.hxx
+employee-odb.ixx
+employee-odb.cxx
+employee.sql
+ The first three files contain the database support code and the last file
+ contains the database schema for the employee.hxx header.
+
+ These files are generated by the ODB compiler from employee.hxx using the
+ following command line:
+
+ odb -d <database> --generate-schema --generate-query \
+ --default-pointer std::tr1::shared_ptr employee.hxx
+
+ Where <database> stands for the database system we are using, for example,
+ 'mysql'.
+
+ The --default-pointer option is used to make TR1 shared_ptr the default
+ object pointer.
+
+database.hxx
+ Contains the create_database() function which instantiates the concrete
+ database class corresponding to the database system we are using.
+
+driver.cxx
+ Driver for the example. It includes the employee.hxx and employee-odb.hxx
+ headers to gain access to the 'employee' class and the database support
+ code for this class. It also includes database.hxx for the
+ create_database() function declaration.
+
+ In main() the driver first calls create_database() to obtain the database
+ instance. It then creates a number of 'employee', 'employer', 'position',
+ and 'project' objects, sets the relationships between them, and persists
+ them in the database. In the next few transactions the driver loads various
+ objects, then accesses and modifies the relationships between them. Finally,
+ the driver performs a database query which uses a data member from a related
+ object in its criterion.
+
+To run the example we first need to create the database schema. Using MySQL
+as an example, this can be achieved with the following command:
+
+mysql --user=odb_test --database=odb_test < employee.sql
+
+Here we use 'odb_test' as the database login and also 'odb_test' as the
+database name.
+
+Once the database schema is ready, we can run the example (using MySQL as
+the database):
+
+./driver --user odb_test --database odb_test
diff --git a/inverse/database.hxx b/inverse/database.hxx
new file mode 100644
index 0000000..964381b
--- /dev/null
+++ b/inverse/database.hxx
@@ -0,0 +1,46 @@
+// file : inverse/database.hxx
+// author : Boris Kolpackov <boris@codesynthesis.com>
+// copyright : not copyrighted - public domain
+
+//
+// Create concrete database instance based on the DATABASE_* macros.
+//
+
+#ifndef DATABASE_HXX
+#define DATABASE_HXX
+
+#include <string>
+#include <memory> // std::auto_ptr
+#include <cstdlib> // std::exit
+#include <iostream>
+
+#include <odb/database.hxx>
+
+#if defined(DATABASE_MYSQL)
+# include <odb/mysql/database.hxx>
+#endif
+
+inline std::auto_ptr<odb::database>
+create_database (int& argc, char* argv[])
+{
+ using namespace std;
+ using namespace odb;
+
+ if (argc > 1 && argv[1] == string ("--help"))
+ {
+ cerr << "Usage: " << argv[0] << " [options]" << endl
+ << "Options:" << endl;
+
+#if defined(DATABASE_MYSQL)
+ mysql::database::print_usage (cerr);
+#endif
+
+ exit (0);
+ }
+
+#if defined(DATABASE_MYSQL)
+ return auto_ptr<database> (new mysql::database (argc, argv));
+#endif
+}
+
+#endif // DATABASE_HXX
diff --git a/inverse/driver.cxx b/inverse/driver.cxx
new file mode 100644
index 0000000..bffbf65
--- /dev/null
+++ b/inverse/driver.cxx
@@ -0,0 +1,258 @@
+// file : inverse/driver.cxx
+// author : Boris Kolpackov <boris@codesynthesis.com>
+// copyright : not copyrighted - public domain
+
+#include <memory> // std::auto_ptr
+#include <iostream>
+
+#include <odb/database.hxx>
+#include <odb/transaction.hxx>
+
+#include "database.hxx" // create_database
+
+#include "employee.hxx"
+#include "employee-odb.hxx"
+
+using namespace std;
+using namespace odb;
+
+void
+print (const employee& e)
+{
+ cout << e.first () << " " << e.last () << endl
+ << " employer: " << e.employer ().load ()->name () << endl
+ << " position: " << e.position ().load ()->title () << endl;
+
+ const projects& ps (e.projects ());
+
+ for (projects::const_iterator i (ps.begin ()); i != ps.end (); ++i)
+ {
+ const lazy_shared_ptr<project>& p (*i);
+ p.load ();
+
+ cout << " project: " << p->name () << endl;
+ }
+
+ cout << endl;
+}
+
+int
+main (int argc, char* argv[])
+{
+ try
+ {
+ auto_ptr<database> db (create_database (argc, argv));
+
+ // Create a few persistent objects.
+ //
+ {
+ // Simple Tech Ltd.
+ //
+ {
+ shared_ptr<employer> er (new employer ("Simple Tech Ltd"));
+
+ shared_ptr<position> he (new position ("Hardware Engineer"));
+ shared_ptr<position> se (new position ("Software Engineer"));
+
+ shared_ptr<project> sh (new project ("Simple Hardware"));
+ shared_ptr<project> ss (new project ("Simple Software"));
+
+ shared_ptr<employee> john (new employee ("John", "Doe", er, he));
+ shared_ptr<employee> jane (new employee ("Jane", "Doe", er, se));
+
+ // Set the inverse side of the employee-employer relationship.
+ //
+ er->employees ().push_back (john);
+ er->employees ().push_back (jane);
+
+ // Set the inverse side of the employee-position relationship.
+ //
+ he->employee (john);
+ se->employee (jane);
+
+ // Set the employee-project relationship (both directions).
+ //
+ john->projects ().push_back (sh);
+ john->projects ().push_back (ss);
+ jane->projects ().push_back (ss);
+
+ sh->employees ().push_back (john);
+ ss->employees ().push_back (john);
+ ss->employees ().push_back (jane);
+
+ transaction t (db->begin ());
+
+ db->persist (er);
+
+ db->persist (he);
+ db->persist (se);
+
+ db->persist (sh);
+ db->persist (ss);
+
+ db->persist (john);
+ db->persist (jane);
+
+ t.commit ();
+ }
+
+ // Complex Systems Inc.
+ //
+ {
+ shared_ptr<employer> er (new employer ("Complex Systems Inc"));
+
+ shared_ptr<position> he (new position ("Hardware Engineer"));
+ shared_ptr<position> se (new position ("Software Engineer"));
+
+ shared_ptr<project> ch (new project ("Complex Hardware"));
+ shared_ptr<project> cs (new project ("Complex Software"));
+
+ shared_ptr<employee> john (new employee ("John", "Smith", er, se));
+ shared_ptr<employee> jane (new employee ("Jane", "Smith", er, he));
+
+ // Set the inverse side of the employee-employer relationship.
+ //
+ er->employees ().push_back (john);
+ er->employees ().push_back (jane);
+
+ // Set the inverse side of the employee-position relationship.
+ //
+ he->employee (john);
+ se->employee (jane);
+
+ // Set the employee-project relationship (both directions).
+ //
+ john->projects ().push_back (cs);
+ jane->projects ().push_back (ch);
+ jane->projects ().push_back (cs);
+
+ ch->employees ().push_back (jane);
+ cs->employees ().push_back (john);
+ cs->employees ().push_back (jane);
+
+ transaction t (db->begin ());
+
+ db->persist (er);
+
+ db->persist (he);
+ db->persist (se);
+
+ db->persist (ch);
+ db->persist (cs);
+
+ db->persist (john);
+ db->persist (jane);
+
+ t.commit ();
+ }
+ }
+
+ // Load Simple Tech Ltd and print its employees. We use a session in this
+ // and subsequent transactions to make sure that a single instance of any
+ // particular object (e.g., employer) is shared among all objects (e.g.,
+ // employee) that relate to it.
+ //
+ {
+ session s;
+ transaction t (db->begin ());
+
+ shared_ptr<employer> stl (db->load<employer> ("Simple Tech Ltd"));
+
+ employees& es (stl->employees ());
+
+ for (employees::iterator i (es.begin ()); i != es.end (); ++i)
+ {
+ lazy_weak_ptr<employee>& lwp (*i);
+ shared_ptr<employee> p (lwp.load ()); // Load and lock.
+ print (*p);
+ }
+
+ t.commit ();
+ }
+
+ // Find all Software Engineers.
+ //
+ {
+ typedef odb::query<position> query;
+ typedef odb::result<position> result;
+
+ session s;
+ transaction t (db->begin ());
+
+ result r (db->query<position> (query::title == "Software Engineer"));
+
+ for (result::iterator i (r.begin ()); i != r.end (); ++i)
+ {
+ const lazy_weak_ptr<employee>& lwp (i->employee ());
+ shared_ptr<employee> p (lwp.load ()); // Load and lock.
+
+ // Employee can be NULL if the position is vacant.
+ //
+ if (p)
+ print (*p);
+ }
+
+ t.commit ();
+ }
+
+ // John Doe has moved to Complex Systems Inc and is now working as
+ // a Software Engineer on Complex Software.
+ //
+ {
+ typedef odb::query<employee> query;
+ typedef odb::result<employee> result;
+
+ session s;
+ transaction t (db->begin ());
+
+ // Create "unloaded" pointers to the employer and project objects.
+ //
+ lazy_shared_ptr<employer> csi (*db, std::string ("Complex Systems Inc"));
+ lazy_shared_ptr<project> cs (*db, std::string ("Complex Software"));
+
+ // Create a new Software Engineer position.
+ //
+ shared_ptr<position> se (new position ("Software Engineer"));
+
+ result r (db->query<employee> (query::first == "John" &&
+ query::last == "Doe"));
+
+ shared_ptr<employee> john (r.begin ().load ());
+
+ john->employer (csi);
+ john->position (se);
+ john->projects ().clear ();
+ john->projects ().push_back (cs);
+
+ db->persist (se);
+ db->update (john);
+
+ t.commit ();
+ }
+
+ // Print Complex Systems Inc's employees. This time, instead of loading
+ // the employer object, we use a query which shows how we can use members
+ // of the pointed-to objects in the queries.
+ //
+ {
+ typedef odb::query<employee> query;
+ typedef odb::result<employee> result;
+
+ session s;
+ transaction t (db->begin ());
+
+ result r (db->query<employee> (
+ query::employer::name == "Complex Systems Inc"));
+
+ for (result::iterator i (r.begin ()); i != r.end (); ++i)
+ print (*i);
+
+ t.commit ();
+ }
+ }
+ catch (const odb::exception& e)
+ {
+ cerr << e.what () << endl;
+ return 1;
+ }
+}
diff --git a/inverse/employee.hxx b/inverse/employee.hxx
new file mode 100644
index 0000000..915b561
--- /dev/null
+++ b/inverse/employee.hxx
@@ -0,0 +1,275 @@
+// file : inverse/employee.hxx
+// author : Boris Kolpackov <boris@codesynthesis.com>
+// copyright : not copyrighted - public domain
+
+#ifndef EMPLOYEE_HXX
+#define EMPLOYEE_HXX
+
+#include <vector>
+#include <string>
+
+#include <odb/core.hxx>
+
+// Include TR1 <memory> header in a compiler-specific fashion. Fall back
+// on the Boost implementation if the compiler does not support TR1.
+//
+#include <odb/tr1/memory.hxx>
+
+#include <odb/tr1/lazy-ptr.hxx>
+
+using std::tr1::shared_ptr;
+
+using odb::tr1::lazy_shared_ptr;
+using odb::tr1::lazy_weak_ptr;
+
+// The "pointer architecture" in this object model is as follows: All
+// object pointers are lazy. The employee class holds shared pointers
+// to employer, position, and projects. All other objects hold weak
+// pointers back to the employee object. The weak sides are also the
+// ones that are made inverse.
+//
+// The following bidirectional relationships are used:
+//
+// many-to-one : employee <--> employer
+// one-to-one : employee <--> position
+// many-to-many : employee <--> project
+//
+
+// Forward declarations.
+//
+class employer;
+class position;
+class project;
+class employee;
+
+typedef std::vector<lazy_shared_ptr<project> > projects;
+typedef std::vector<lazy_weak_ptr<employee> > employees;
+
+#pragma db object
+class employer
+{
+public:
+ employer (const std::string& name)
+ : name_ (name)
+ {
+ }
+
+ const std::string&
+ name () const
+ {
+ return name_;
+ }
+
+ // Employees of this employer.
+ //
+ typedef ::employees employees_type;
+
+ const employees_type&
+ employees () const
+ {
+ return employees_;
+ }
+
+ employees_type&
+ employees ()
+ {
+ return employees_;
+ }
+
+private:
+ friend class odb::access;
+
+ employer () {}
+
+ #pragma db id
+ std::string name_;
+
+ #pragma db not_null inverse(employer_)
+ employees_type employees_;
+};
+
+#pragma db object
+class position
+{
+public:
+ position (const std::string& title)
+ : title_ (title)
+ {
+ }
+
+ const std::string&
+ title () const
+ {
+ return title_;
+ }
+
+ // Employee that fills this position. NULL if the position is vacant.
+ //
+ typedef ::employee employee_type;
+
+ const lazy_weak_ptr<employee_type>&
+ employee () const
+ {
+ return employee_;
+ }
+
+ void
+ employee (lazy_weak_ptr<employee_type> employee)
+ {
+ employee_ = employee;
+ }
+
+private:
+ friend class odb::access;
+
+ position () {}
+
+ #pragma db id auto
+ unsigned long id_;
+
+ std::string title_;
+
+ #pragma db inverse(position_)
+ lazy_weak_ptr<employee_type> employee_;
+};
+
+#pragma db object
+class project
+{
+public:
+ project (const std::string& name)
+ : name_ (name)
+ {
+ }
+
+ const std::string&
+ name () const
+ {
+ return name_;
+ }
+
+ // Employees working on this project.
+ //
+ typedef ::employees employees_type;
+
+ const employees_type&
+ employees () const
+ {
+ return employees_;
+ }
+
+ employees_type&
+ employees ()
+ {
+ return employees_;
+ }
+
+private:
+ friend class odb::access;
+
+ project () {}
+
+ #pragma db id
+ std::string name_;
+
+ #pragma db not_null inverse(projects_)
+ employees_type employees_;
+};
+
+#pragma db object
+class employee
+{
+public:
+ typedef ::employer employer_type;
+ typedef ::position position_type;
+
+ employee (const std::string& first,
+ const std::string& last,
+ lazy_shared_ptr<employer_type> employer,
+ lazy_shared_ptr<position_type> position)
+ : first_ (first), last_ (last),
+ employer_ (employer),
+ position_ (position)
+ {
+ }
+
+ // Name.
+ //
+ const std::string&
+ first () const
+ {
+ return first_;
+ }
+
+ const std::string&
+ last () const
+ {
+ return last_;
+ }
+
+ // Employer.
+ //
+ const lazy_shared_ptr<employer_type>&
+ employer () const
+ {
+ return employer_;
+ }
+
+ void
+ employer (lazy_shared_ptr<employer_type> employer)
+ {
+ employer_ = employer;
+ }
+
+ // Position.
+ //
+ const lazy_shared_ptr<position_type>&
+ position () const
+ {
+ return position_;
+ }
+
+ void
+ position (lazy_shared_ptr<position_type> position)
+ {
+ position_ = position;
+ }
+
+ // Projects.
+ //
+ typedef ::projects projects_type;
+
+ const projects_type&
+ projects () const
+ {
+ return projects_;
+ }
+
+ projects_type&
+ projects ()
+ {
+ return projects_;
+ }
+
+private:
+ friend class odb::access;
+
+ employee () {}
+
+ #pragma db id auto
+ unsigned long id_;
+
+ std::string first_;
+ std::string last_;
+
+ #pragma db not_null
+ lazy_shared_ptr<employer_type> employer_;
+
+ #pragma db not_null
+ lazy_shared_ptr<position_type> position_;
+
+ #pragma db not_null unordered
+ projects_type projects_;
+};
+
+#endif // EMPLOYEE_HXX
diff --git a/inverse/makefile b/inverse/makefile
new file mode 100644
index 0000000..b56754e
--- /dev/null
+++ b/inverse/makefile
@@ -0,0 +1,118 @@
+# file : inverse/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 := employee.hxx
+cxx_obj := $(addprefix $(out_base)/,$(cxx_tun:.cxx=.o) $(odb_hdr:.hxx=-odb.o))
+cxx_od := $(cxx_obj:.o=.o.d)
+
+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)
+
+$(call import,\
+ $(scf_root)/import/libodb/stub.make,\
+ l: odb.l,cpp-options: odb.l.cpp-options)
+
+ifdef db_id
+$(call import,\
+ $(scf_root)/import/libodb-$(db_id)/stub.make,\
+ l: odb_db.l,cpp-options: odb_db.l.cpp-options)
+endif
+
+ifeq ($(odb_db.l.cpp-options),)
+odb_db.l.cpp-options := $(out_base)/.unbuildable
+endif
+
+# Build.
+#
+$(driver): $(cxx_obj) $(odb_db.l) $(odb.l)
+$(cxx_obj) $(cxx_od): cpp_options := -I$(out_base)
+$(cxx_obj) $(cxx_od): $(odb.l.cpp-options) $(odb_db.l.cpp-options)
+
+ifeq ($(db_id),mysql)
+$(cxx_obj) $(cxx_od): cpp_options += -DDATABASE_MYSQL
+endif
+
+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-query \
+--generate-schema --default-pointer std::tr1::shared_ptr
+$(gen): cpp_options := -I$(out_base)
+$(gen): $(odb.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): export name := $(name)
+$(dist): export odb_header_stem := $(basename $(odb_hdr))
+$(dist): export extra_dist := README $(call vc9projs,$(name)) \
+$(call vc10projs,$(name))
+$(dist):
+ $(call dist-data,$(sources) $(headers) README database.hxx)
+ $(call meta-automake,../template/Makefile.am)
+ $(call meta-vc9projs,../template/template,$(name))
+ $(call meta-vc10projs,../template/template,$(name))
+
+# Test.
+#
+$(test): schema := $(src_base)/$(basename $(odb_hdr)).sql
+$(test): $(driver)
+ $(call message,sql $$1,$(dcf_root)/db-driver $$1,$(schema))
+ $(call message,test $<,$< --options-file $(dcf_root)/db.options)
+
+# 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)
+
diff --git a/m4/tr1-memory.m4 b/m4/tr1-memory.m4
new file mode 100644
index 0000000..29966b3
--- /dev/null
+++ b/m4/tr1-memory.m4
@@ -0,0 +1,40 @@
+dnl file : m4/tr1-memory.m4
+dnl author : Boris Kolpackov <boris@codesynthesis.com>
+dnl copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC
+dnl license : GNU GPL v2; see accompanying LICENSE file
+dnl
+dnl TR1_MEMORY
+dnl
+dnl Check for TR1 <memory> availability. If successful, define HAVE_TR1_MEMORY
+dnl as both a macro and conditional as well as set the tr1_memory variable
+dnl to 'yes'.
+dnl
+AC_DEFUN([TR1_MEMORY],
+[
+tr1_memory=no
+
+AC_MSG_CHECKING([for TR1 <memory>])
+
+CXX_LIBTOOL_LINK_IFELSE(
+AC_LANG_SOURCE([[
+#include <odb/tr1/memory.hxx>
+
+int
+main ()
+{
+ std::tr1::shared_ptr<int> p (new int (10));
+ *p = 11;
+}
+]]),
+[tr1_memory=yes])
+
+if test x"$tr1_memory" = xyes; then
+ AC_MSG_RESULT([yes])
+ AC_DEFINE([HAVE_TR1_MEMORY], [1], [Have TR1 <memory>.])
+else
+ AC_MSG_RESULT([no])
+fi
+
+AM_CONDITIONAL([HAVE_TR1_MEMORY], [test x$tr1_memory = xyes])
+
+])dnl
diff --git a/makefile b/makefile
index eab1c3f..833654c 100644
--- a/makefile
+++ b/makefile
@@ -5,18 +5,22 @@
include $(dir $(lastword $(MAKEFILE_LIST)))build/bootstrap.make
-dirs := container hello query mapping template
-dist_dirs := $(filter-out template,$(dirs))
+dirs := composite container hello query mapping
+tr1_dirs := relationship inverse
+
+dist_dirs := $(dirs) $(tr1_dirs)
+all_dirs := $(dirs) $(tr1_dirs) template
default := $(out_base)/
dist := $(out_base)/.dist
test := $(out_base)/.test
clean := $(out_base)/.clean
-$(default): $(addprefix $(out_base)/,$(addsuffix /,$(dirs)))
+$(default): $(addprefix $(out_base)/,$(addsuffix /,$(all_dirs)))
$(dist): name := examples
-$(dist): export dirs := $(dist_dirs)
+$(dist): export dirs := $(dirs)
+$(dist): export tr1_dirs := $(tr1_dirs)
$(dist): data_dist := GPLv2 LICENSE README NEWS INSTALL version tester.bat \
mysql-driver.bat mysql.options
$(dist): exec_dist := bootstrap tester
@@ -34,8 +38,8 @@ $(dist): $(addprefix $(out_base)/,$(addsuffix /.dist,$(dist_dirs)))
$(call meta-vc10slns,$(name))
$(call meta-vctest,$(name)-mysql-vc10.sln,test.bat)
-$(test): $(addprefix $(out_base)/,$(addsuffix /.test,$(dirs)))
-$(clean): $(addprefix $(out_base)/,$(addsuffix /.clean,$(dirs)))
+$(test): $(addprefix $(out_base)/,$(addsuffix /.test,$(all_dirs)))
+$(clean): $(addprefix $(out_base)/,$(addsuffix /.clean,$(all_dirs)))
$(call include,$(bld_root)/dist.make)
$(call include,$(bld_root)/meta/vc9sln.make)
@@ -44,5 +48,4 @@ $(call include,$(bld_root)/meta/vctest.make)
$(call include,$(bld_root)/meta/automake.make)
$(call include,$(bld_root)/meta/autoconf.make)
-$(foreach d,$(dirs),$(call import,$(src_base)/$d/makefile))
-
+$(foreach d,$(all_dirs),$(call import,$(src_base)/$d/makefile))
diff --git a/relationship/README b/relationship/README
new file mode 100644
index 0000000..c7eacf3
--- /dev/null
+++ b/relationship/README
@@ -0,0 +1,63 @@
+This example shows how to declare and use unidirectional to-one and to-many
+relationships between persistent objects.
+
+The example uses the shared_ptr smart pointer from TR1 and requires a C++
+compiler with TR1 support or an external TR1 implementation, such as the
+one provided by Boost.
+
+The example consists of the following files:
+
+employee.hxx
+ Header file defining the 'employee', 'employer', and 'project' persistent
+ classes as well as the employee-employer (to-one) and employee-project (to-
+ many) unidirectional relationships between them.
+
+employee-odb.hxx
+employee-odb.ixx
+employee-odb.cxx
+employee.sql
+ The first three files contain the database support code and the last file
+ contains the database schema for the employee.hxx header.
+
+ These files are generated by the ODB compiler from employee.hxx using the
+ following command line:
+
+ odb -d <database> --generate-schema --generate-query \
+ --default-pointer std::tr1::shared_ptr employee.hxx
+
+ Where <database> stands for the database system we are using, for example,
+ 'mysql'.
+
+ The --default-pointer option is used to make TR1 shared_ptr the default
+ object pointer.
+
+database.hxx
+ Contains the create_database() function which instantiates the concrete
+ database class corresponding to the database system we are using.
+
+driver.cxx
+ Driver for the example. It includes the employee.hxx and employee-odb.hxx
+ headers to gain access to the 'employee' class and the database support
+ code for this class. It also includes database.hxx for the
+ create_database() function declaration.
+
+ In main() the driver first calls create_database() to obtain the database
+ instance. It then creates a number of 'employee', 'employer', and 'project'
+ objects, sets the relationships between them, and persists them in the
+ database. In the next few transactions the driver loads various objects,
+ then accesses and modifies the relationships between them. Finally, the
+ driver performs a database query which uses a data member from a related
+ object in its criterion.
+
+To run the example we first need to create the database schema. Using MySQL
+as an example, this can be achieved with the following command:
+
+mysql --user=odb_test --database=odb_test < employee.sql
+
+Here we use 'odb_test' as the database login and also 'odb_test' as the
+database name.
+
+Once the database schema is ready, we can run the example (using MySQL as
+the database):
+
+./driver --user odb_test --database odb_test
diff --git a/relationship/database.hxx b/relationship/database.hxx
new file mode 100644
index 0000000..f9cd50c
--- /dev/null
+++ b/relationship/database.hxx
@@ -0,0 +1,46 @@
+// file : relationship/database.hxx
+// author : Boris Kolpackov <boris@codesynthesis.com>
+// copyright : not copyrighted - public domain
+
+//
+// Create concrete database instance based on the DATABASE_* macros.
+//
+
+#ifndef DATABASE_HXX
+#define DATABASE_HXX
+
+#include <string>
+#include <memory> // std::auto_ptr
+#include <cstdlib> // std::exit
+#include <iostream>
+
+#include <odb/database.hxx>
+
+#if defined(DATABASE_MYSQL)
+# include <odb/mysql/database.hxx>
+#endif
+
+inline std::auto_ptr<odb::database>
+create_database (int& argc, char* argv[])
+{
+ using namespace std;
+ using namespace odb;
+
+ if (argc > 1 && argv[1] == string ("--help"))
+ {
+ cerr << "Usage: " << argv[0] << " [options]" << endl
+ << "Options:" << endl;
+
+#if defined(DATABASE_MYSQL)
+ mysql::database::print_usage (cerr);
+#endif
+
+ exit (0);
+ }
+
+#if defined(DATABASE_MYSQL)
+ return auto_ptr<database> (new mysql::database (argc, argv));
+#endif
+}
+
+#endif // DATABASE_HXX
diff --git a/relationship/driver.cxx b/relationship/driver.cxx
new file mode 100644
index 0000000..9d09913
--- /dev/null
+++ b/relationship/driver.cxx
@@ -0,0 +1,168 @@
+// file : relationship/driver.cxx
+// author : Boris Kolpackov <boris@codesynthesis.com>
+// copyright : not copyrighted - public domain
+
+#include <memory> // std::auto_ptr
+#include <iostream>
+
+#include <odb/database.hxx>
+#include <odb/transaction.hxx>
+
+#include "database.hxx" // create_database
+
+#include "employee.hxx"
+#include "employee-odb.hxx"
+
+using namespace std;
+using namespace odb;
+
+void
+print (const employee& e)
+{
+ cout << e.first () << " " << e.last () << endl
+ << " employer: " << e.employer ()->name () << endl;
+
+ const projects& ps (e.projects ());
+
+ for (projects::const_iterator i (ps.begin ()); i != ps.end (); ++i)
+ {
+ shared_ptr<project> p (*i);
+ cout << " project: " << p->name () << endl;
+ }
+
+ cout << endl;
+}
+
+int
+main (int argc, char* argv[])
+{
+ try
+ {
+ auto_ptr<database> db (create_database (argc, argv));
+
+ // Create a few persistent objects.
+ //
+ {
+ // Simple Tech Ltd.
+ //
+ {
+ shared_ptr<employer> er (new employer ("Simple Tech Ltd"));
+
+ shared_ptr<project> sh (new project ("Simple Hardware"));
+ shared_ptr<project> ss (new project ("Simple Software"));
+
+ shared_ptr<employee> john (new employee ("John", "Doe", er));
+ shared_ptr<employee> jane (new employee ("Jane", "Doe", er));
+
+ john->projects ().push_back (sh);
+ john->projects ().push_back (ss);
+ jane->projects ().push_back (ss);
+
+ transaction t (db->begin ());
+
+ db->persist (er);
+
+ db->persist (sh);
+ db->persist (ss);
+
+ db->persist (john);
+ db->persist (jane);
+
+ t.commit ();
+ }
+
+ // Complex Systems Inc.
+ //
+ {
+ shared_ptr<employer> er (new employer ("Complex Systems Inc"));
+
+ shared_ptr<project> ch (new project ("Complex Hardware"));
+ shared_ptr<project> cs (new project ("Complex Software"));
+
+ shared_ptr<employee> john (new employee ("John", "Smith", er));
+ shared_ptr<employee> jane (new employee ("Jane", "Smith", er));
+
+ john->projects ().push_back (cs);
+ jane->projects ().push_back (ch);
+ jane->projects ().push_back (cs);
+
+ transaction t (db->begin ());
+
+ db->persist (er);
+
+ db->persist (ch);
+ db->persist (cs);
+
+ db->persist (john);
+ db->persist (jane);
+
+ t.commit ();
+ }
+ }
+
+ typedef odb::query<employee> query;
+ typedef odb::result<employee> result;
+
+ // Load employees with "Doe" as the last name and print what we've got.
+ // We use a session in this and subsequent transactions to make sure
+ // that a single instance of any particular object (e.g., employer) is
+ // shared among all objects (e.g., employee) that relate to it.
+ //
+ {
+ session s;
+ transaction t (db->begin ());
+
+ result r (db->query<employee> (query::last == "Doe"));
+
+ for (result::iterator i (r.begin ()); i != r.end (); ++i)
+ print (*i);
+
+ t.commit ();
+ }
+
+ // John Doe has moved to Complex Systems Inc and is now working on
+ // Complex Hardware.
+ //
+ {
+ session s;
+ transaction t (db->begin ());
+
+ shared_ptr<employer> csi (db->load<employer> ("Complex Systems Inc"));
+ shared_ptr<project> ch (db->load<project> ("Complex Hardware"));
+
+ result r (db->query<employee> (query::first == "John" &&
+ query::last == "Doe"));
+
+ shared_ptr<employee> john (r.begin ().load ());
+
+ john->employer (csi);
+ john->projects ().clear ();
+ john->projects ().push_back (ch);
+
+ db->update (john);
+
+ t.commit ();
+ }
+
+ // We can also use members of the pointed-to objects in the queries. The
+ // following transaction prints all the employees of Complex Systems Inc.
+ //
+ {
+ session s;
+ transaction t (db->begin ());
+
+ result r (db->query<employee> (
+ query::employer::name == "Complex Systems Inc"));
+
+ for (result::iterator i (r.begin ()); i != r.end (); ++i)
+ print (*i);
+
+ t.commit ();
+ }
+ }
+ catch (const odb::exception& e)
+ {
+ cerr << e.what () << endl;
+ return 1;
+ }
+}
diff --git a/relationship/employee.hxx b/relationship/employee.hxx
new file mode 100644
index 0000000..e89e704
--- /dev/null
+++ b/relationship/employee.hxx
@@ -0,0 +1,161 @@
+// file : relationship/employee.hxx
+// author : Boris Kolpackov <boris@codesynthesis.com>
+// copyright : not copyrighted - public domain
+
+#ifndef EMPLOYEE_HXX
+#define EMPLOYEE_HXX
+
+#include <vector>
+#include <string>
+
+#include <odb/core.hxx>
+
+// Include TR1 <memory> header in a compiler-specific fashion. Fall back
+// on the Boost implementation if the compiler does not support TR1.
+//
+#include <odb/tr1/memory.hxx>
+
+using std::tr1::shared_ptr;
+
+// The "pointer architecture" in this object model is as follows: All
+// object pointers are eager. The employee class holds shared pointers
+// to employer and projects.
+//
+// The following unidirectional relationships are used:
+//
+// to-one : employee -> employer
+// to-many : employee -> project
+//
+
+// Forward declarations.
+//
+class employer;
+class project;
+class employee;
+
+typedef std::vector<shared_ptr<project> > projects;
+
+#pragma db object
+class employer
+{
+public:
+ employer (const std::string& name)
+ : name_ (name)
+ {
+ }
+
+ const std::string&
+ name () const
+ {
+ return name_;
+ }
+
+private:
+ friend class odb::access;
+
+ employer () {}
+
+ #pragma db id
+ std::string name_;
+};
+
+#pragma db object
+class project
+{
+public:
+ project (const std::string& name)
+ : name_ (name)
+ {
+ }
+
+ const std::string&
+ name () const
+ {
+ return name_;
+ }
+
+private:
+ friend class odb::access;
+
+ project () {}
+
+ #pragma db id
+ std::string name_;
+};
+
+#pragma db object
+class employee
+{
+public:
+ typedef ::employer employer_type;
+
+ employee (const std::string& first,
+ const std::string& last,
+ shared_ptr<employer_type> employer)
+ : first_ (first), last_ (last), employer_ (employer)
+ {
+ }
+
+ // Name.
+ //
+ const std::string&
+ first () const
+ {
+ return first_;
+ }
+
+ const std::string&
+ last () const
+ {
+ return last_;
+ }
+
+ // Employer.
+ //
+ shared_ptr<employer_type>
+ employer () const
+ {
+ return employer_;
+ }
+
+ void
+ employer (shared_ptr<employer_type> employer)
+ {
+ employer_ = employer;
+ }
+
+ // Projects.
+ //
+ typedef ::projects projects_type;
+
+ const projects_type&
+ projects () const
+ {
+ return projects_;
+ }
+
+ projects_type&
+ projects ()
+ {
+ return projects_;
+ }
+
+private:
+ friend class odb::access;
+
+ employee () {}
+
+ #pragma db id auto
+ unsigned long id_;
+
+ std::string first_;
+ std::string last_;
+
+ #pragma db not_null
+ shared_ptr<employer_type> employer_;
+
+ #pragma db not_null unordered
+ projects_type projects_;
+};
+
+#endif // EMPLOYEE_HXX
diff --git a/relationship/makefile b/relationship/makefile
new file mode 100644
index 0000000..56a7734
--- /dev/null
+++ b/relationship/makefile
@@ -0,0 +1,118 @@
+# file : relationship/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 := employee.hxx
+cxx_obj := $(addprefix $(out_base)/,$(cxx_tun:.cxx=.o) $(odb_hdr:.hxx=-odb.o))
+cxx_od := $(cxx_obj:.o=.o.d)
+
+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)
+
+$(call import,\
+ $(scf_root)/import/libodb/stub.make,\
+ l: odb.l,cpp-options: odb.l.cpp-options)
+
+ifdef db_id
+$(call import,\
+ $(scf_root)/import/libodb-$(db_id)/stub.make,\
+ l: odb_db.l,cpp-options: odb_db.l.cpp-options)
+endif
+
+ifeq ($(odb_db.l.cpp-options),)
+odb_db.l.cpp-options := $(out_base)/.unbuildable
+endif
+
+# Build.
+#
+$(driver): $(cxx_obj) $(odb_db.l) $(odb.l)
+$(cxx_obj) $(cxx_od): cpp_options := -I$(out_base)
+$(cxx_obj) $(cxx_od): $(odb.l.cpp-options) $(odb_db.l.cpp-options)
+
+ifeq ($(db_id),mysql)
+$(cxx_obj) $(cxx_od): cpp_options += -DDATABASE_MYSQL
+endif
+
+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-query \
+--generate-schema --default-pointer std::tr1::shared_ptr
+$(gen): cpp_options := -I$(out_base)
+$(gen): $(odb.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): export name := $(name)
+$(dist): export odb_header_stem := $(basename $(odb_hdr))
+$(dist): export extra_dist := README $(call vc9projs,$(name)) \
+$(call vc10projs,$(name))
+$(dist):
+ $(call dist-data,$(sources) $(headers) README database.hxx)
+ $(call meta-automake,../template/Makefile.am)
+ $(call meta-vc9projs,../template/template,$(name))
+ $(call meta-vc10projs,../template/template,$(name))
+
+# Test.
+#
+$(test): schema := $(src_base)/$(basename $(odb_hdr)).sql
+$(test): $(driver)
+ $(call message,sql $$1,$(dcf_root)/db-driver $$1,$(schema))
+ $(call message,test $<,$< --options-file $(dcf_root)/db.options)
+
+# 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)
+