aboutsummaryrefslogtreecommitdiff
path: root/inverse
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 /inverse
parent5511613df7dce6142a84111488aaa25ff792d66b (diff)
Add composite, relationship, and inverse examples
All add the TR1 <memory> test for the latter two examples.
Diffstat (limited to 'inverse')
-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
5 files changed, 764 insertions, 0 deletions
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)
+