From ab51fa65f9e8cad4ef5a1db85029dfe6404e9a1f Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 13 Jan 2011 11:31:14 +0200 Subject: Add composite, relationship, and inverse examples All add the TR1 test for the latter two examples. --- inverse/README | 67 +++++++++++++ inverse/database.hxx | 46 +++++++++ inverse/driver.cxx | 258 +++++++++++++++++++++++++++++++++++++++++++++++ inverse/employee.hxx | 275 +++++++++++++++++++++++++++++++++++++++++++++++++++ inverse/makefile | 118 ++++++++++++++++++++++ 5 files changed, 764 insertions(+) create mode 100644 inverse/README create mode 100644 inverse/database.hxx create mode 100644 inverse/driver.cxx create mode 100644 inverse/employee.hxx create mode 100644 inverse/makefile (limited to 'inverse') 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 --generate-schema --generate-query \ + --default-pointer std::tr1::shared_ptr employee.hxx + + Where 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 +// copyright : not copyrighted - public domain + +// +// Create concrete database instance based on the DATABASE_* macros. +// + +#ifndef DATABASE_HXX +#define DATABASE_HXX + +#include +#include // std::auto_ptr +#include // std::exit +#include + +#include + +#if defined(DATABASE_MYSQL) +# include +#endif + +inline std::auto_ptr +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 (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 +// copyright : not copyrighted - public domain + +#include // std::auto_ptr +#include + +#include +#include + +#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& p (*i); + p.load (); + + cout << " project: " << p->name () << endl; + } + + cout << endl; +} + +int +main (int argc, char* argv[]) +{ + try + { + auto_ptr db (create_database (argc, argv)); + + // Create a few persistent objects. + // + { + // Simple Tech Ltd. + // + { + shared_ptr er (new employer ("Simple Tech Ltd")); + + shared_ptr he (new position ("Hardware Engineer")); + shared_ptr se (new position ("Software Engineer")); + + shared_ptr sh (new project ("Simple Hardware")); + shared_ptr ss (new project ("Simple Software")); + + shared_ptr john (new employee ("John", "Doe", er, he)); + shared_ptr 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 er (new employer ("Complex Systems Inc")); + + shared_ptr he (new position ("Hardware Engineer")); + shared_ptr se (new position ("Software Engineer")); + + shared_ptr ch (new project ("Complex Hardware")); + shared_ptr cs (new project ("Complex Software")); + + shared_ptr john (new employee ("John", "Smith", er, se)); + shared_ptr 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 stl (db->load ("Simple Tech Ltd")); + + employees& es (stl->employees ()); + + for (employees::iterator i (es.begin ()); i != es.end (); ++i) + { + lazy_weak_ptr& lwp (*i); + shared_ptr p (lwp.load ()); // Load and lock. + print (*p); + } + + t.commit (); + } + + // Find all Software Engineers. + // + { + typedef odb::query query; + typedef odb::result result; + + session s; + transaction t (db->begin ()); + + result r (db->query (query::title == "Software Engineer")); + + for (result::iterator i (r.begin ()); i != r.end (); ++i) + { + const lazy_weak_ptr& lwp (i->employee ()); + shared_ptr 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 query; + typedef odb::result result; + + session s; + transaction t (db->begin ()); + + // Create "unloaded" pointers to the employer and project objects. + // + lazy_shared_ptr csi (*db, std::string ("Complex Systems Inc")); + lazy_shared_ptr cs (*db, std::string ("Complex Software")); + + // Create a new Software Engineer position. + // + shared_ptr se (new position ("Software Engineer")); + + result r (db->query (query::first == "John" && + query::last == "Doe")); + + shared_ptr 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 query; + typedef odb::result result; + + session s; + transaction t (db->begin ()); + + result r (db->query ( + 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 +// copyright : not copyrighted - public domain + +#ifndef EMPLOYEE_HXX +#define EMPLOYEE_HXX + +#include +#include + +#include + +// Include TR1 header in a compiler-specific fashion. Fall back +// on the Boost implementation if the compiler does not support TR1. +// +#include + +#include + +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 > projects; +typedef std::vector > 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 () const + { + return employee_; + } + + void + employee (lazy_weak_ptr 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_; +}; + +#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, + lazy_shared_ptr 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 () const + { + return employer_; + } + + void + employer (lazy_shared_ptr employer) + { + employer_ = employer; + } + + // Position. + // + const lazy_shared_ptr& + position () const + { + return position_; + } + + void + position (lazy_shared_ptr 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_; + + #pragma db not_null + lazy_shared_ptr 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 +# 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) + -- cgit v1.1