diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2011-01-13 11:31:14 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2011-01-13 11:31:14 +0200 |
commit | ab51fa65f9e8cad4ef5a1db85029dfe6404e9a1f (patch) | |
tree | 36c78dd50d88e4797f2bbb886c41399006111fea /relationship | |
parent | 5511613df7dce6142a84111488aaa25ff792d66b (diff) |
Add composite, relationship, and inverse examples
All add the TR1 <memory> test for the latter two examples.
Diffstat (limited to 'relationship')
-rw-r--r-- | relationship/README | 63 | ||||
-rw-r--r-- | relationship/database.hxx | 46 | ||||
-rw-r--r-- | relationship/driver.cxx | 168 | ||||
-rw-r--r-- | relationship/employee.hxx | 161 | ||||
-rw-r--r-- | relationship/makefile | 118 |
5 files changed, 556 insertions, 0 deletions
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) + |