From acba5c43f189dfddef4992371fadaa6f019a8027 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 4 Feb 2011 14:02:12 +0200 Subject: Add Boost profile example --- boost/README | 62 +++++++ boost/boost-vc10.sln | 15 ++ boost/boost-vc10.vcxproj | 174 +++++++++++++++++++ boost/boost-vc10.vcxproj.filters | 25 +++ boost/boost-vc9.sln | 15 ++ boost/boost-vc9.vcproj | 357 +++++++++++++++++++++++++++++++++++++++ boost/database.hxx | 46 +++++ boost/driver.cxx | 151 +++++++++++++++++ boost/employee.hxx | 163 ++++++++++++++++++ boost/makefile | 132 +++++++++++++++ 10 files changed, 1140 insertions(+) create mode 100644 boost/README create mode 100644 boost/boost-vc10.sln create mode 100644 boost/boost-vc10.vcxproj create mode 100644 boost/boost-vc10.vcxproj.filters create mode 100644 boost/boost-vc9.sln create mode 100644 boost/boost-vc9.vcproj create mode 100644 boost/database.hxx create mode 100644 boost/driver.cxx create mode 100644 boost/employee.hxx create mode 100644 boost/makefile (limited to 'boost') diff --git a/boost/README b/boost/README new file mode 100644 index 0000000..c731bad --- /dev/null +++ b/boost/README @@ -0,0 +1,62 @@ +This example shows how to persist objects that use Boost smart pointers, +containers, and value types with the help of the Boost profile library +(libodb-boost). + +The example consists of the following files: + +employee.hxx + Header file defining the 'employee' and 'employer' persistent classes. + We use shared_ptr/weak_ptr smart pointers provided by Boost (as well + as their lazy versions provided by the Boost profile library) to + establish a bidirectional employee-employer relationship. We also use + the boost::gregorian::date type to store the employee's date of birth. + Finally, we use the boost::unordered_set container to keep track of + the employee's email addresses. + +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 -p boost --generate-schema --generate-query employee.hxx + + Where stands for the database system we are using, for example, + 'mysql'. + + The -p option is used to instruct the ODB compiler to load the Boost + profile. + +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 persistent classes and their database support + code. 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' and 'employer' objects and + persists them in the database. The next transaction loads all the employees + of a particular employer using the employee-employer relationship. Finally, + the driver performs a database query which uses a data member of the Boost + gregorian::date 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 < 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/boost/boost-vc10.sln b/boost/boost-vc10.sln new file mode 100644 index 0000000..9a5dc32 --- /dev/null +++ b/boost/boost-vc10.sln @@ -0,0 +1,15 @@ + +Microsoft Visual Studio Solution File, Format Version 11.00 +# Visual Studio 2010 +__projects__ +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution +__solution_configurations__ + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution +__project_configurations__ + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/boost/boost-vc10.vcxproj b/boost/boost-vc10.vcxproj new file mode 100644 index 0000000..f8b144a --- /dev/null +++ b/boost/boost-vc10.vcxproj @@ -0,0 +1,174 @@ + + + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + {__uuid__()} + Win32Proj + __value__(name) + + + + Application + true + Unicode + + + Application + true + Unicode + + + Application + false + true + Unicode + + + Application + false + true + Unicode + + + + + + + + + + + + + + + + + + + true + $(Configuration)\ + driver + + + true + $(Platform)\$(Configuration)\ + driver + + + false + $(Configuration)\ + driver + + + false + $(Platform)\$(Configuration)\ + driver + + + + + + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;__upcase__(database_)__upcase__(__value__(database));%(PreprocessorDefinitions) + 4068;4355;4800;4290;%(DisableSpecificWarnings) + + + odb-__value__(database)-d.lib;odb-boost-d.lib;odb-d.lib;%(AdditionalDependencies) + Console + true + + + + + + + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;__upcase__(database_)__upcase__(__value__(database));%(PreprocessorDefinitions) + 4068;4355;4800;4290;%(DisableSpecificWarnings) + + + odb-__value__(database)-d.lib;odb-boost-d.lib;odb-d.lib;%(AdditionalDependencies) + Console + true + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;__upcase__(database_)__upcase__(__value__(database));%(PreprocessorDefinitions) + 4068;4355;4800;4290;%(DisableSpecificWarnings) + + + odb-__value__(database).lib;odb-boost.lib;odb.lib;%(AdditionalDependencies) + Console + true + true + true + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;__upcase__(database_)__upcase__(__value__(database));%(PreprocessorDefinitions) + 4068;4355;4800;4290;%(DisableSpecificWarnings) + + + odb-__value__(database).lib;odb-boost.lib;odb.lib;%(AdditionalDependencies) + Console + true + true + true + + + +__custom_build_entry__( +__path__(odb_header_stem).hxx, +odb __path__(odb_header_stem).hxx, +odb.exe __xml__(__shell_quotes__(m4_patsubst(__value__(odb_options), @database@, __value__(database)))) __path__(odb_header_stem).hxx, +__path__(odb_header_stem)-odb.hxx;__path__(odb_header_stem)-odb.ixx;__path__(odb_header_stem)-odb.cxx) + + +__header_entry__(__path__(odb_header_stem)-odb.hxx) +__header_entry__(__path__(odb_header_stem)-odb.ixx) +__header_entries__(database.hxx) +__header_entries__(extra_headers) + + +__source_entry__(driver.cxx) +__source_entry__(__path__(odb_header_stem)-odb.cxx) +__source_entries__(extra_sources) + + + + + diff --git a/boost/boost-vc10.vcxproj.filters b/boost/boost-vc10.vcxproj.filters new file mode 100644 index 0000000..f754d41 --- /dev/null +++ b/boost/boost-vc10.vcxproj.filters @@ -0,0 +1,25 @@ + + + + + {__uuid__()} + cxx + + + {__uuid__()} + h;hxx;ixx;txx + + + +__header_filter_entry__(__path__(odb_header_stem).hxx) +__header_filter_entry__(__path__(odb_header_stem)-odb.hxx) +__header_filter_entry__(__path__(odb_header_stem)-odb.ixx) +__header_filter_entries__(database.hxx) +__header_filter_entries__(extra_headers) + + +__source_filter_entry__(driver.cxx) +__source_filter_entry__(__path__(odb_header_stem)-odb.cxx) +__source_filter_entries__(extra_sources) + + diff --git a/boost/boost-vc9.sln b/boost/boost-vc9.sln new file mode 100644 index 0000000..2ec9432 --- /dev/null +++ b/boost/boost-vc9.sln @@ -0,0 +1,15 @@ + +Microsoft Visual Studio Solution File, Format Version 10.00 +# Visual Studio 2008 +__projects__ +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution +__solution_configurations__ + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution +__project_configurations__ + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/boost/boost-vc9.vcproj b/boost/boost-vc9.vcproj new file mode 100644 index 0000000..14524eb --- /dev/null +++ b/boost/boost-vc9.vcproj @@ -0,0 +1,357 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +__source_entry__(driver.cxx) +__source_entry__(__path__(odb_header_stem)-odb.cxx) +__source_entries__(extra_sources) + + +__file_entry_custom_build__( +__path__(odb_header_stem).hxx, +odb __path__(odb_header_stem).hxx, +odb.exe __xml__(__shell_quotes__(m4_patsubst(__value__(odb_options), @database@, __value__(database)))) __path__(odb_header_stem).hxx, +__path__(odb_header_stem)-odb.hxx;__path__(odb_header_stem)-odb.ixx;__path__(odb_header_stem)-odb.cxx) +__file_entry__(__path__(odb_header_stem)-odb.hxx) +__file_entry__(__path__(odb_header_stem)-odb.ixx) +__file_entries__(database.hxx) +__file_entries__(extra_headers) + + + + + diff --git a/boost/database.hxx b/boost/database.hxx new file mode 100644 index 0000000..9fd9478 --- /dev/null +++ b/boost/database.hxx @@ -0,0 +1,46 @@ +// file : boost/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/boost/driver.cxx b/boost/driver.cxx new file mode 100644 index 0000000..5136312 --- /dev/null +++ b/boost/driver.cxx @@ -0,0 +1,151 @@ +// file : boost/driver.cxx +// author : Boris Kolpackov +// copyright : not copyrighted - public domain + +#include // std::auto_ptr +#include + +#include +#include +#include + +#include "database.hxx" // create_database + +#include "employee.hxx" +#include "employee-odb.hxx" + +using namespace std; +using namespace boost::gregorian; +using namespace odb; + +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 john ( + new employee ("John", "Doe", date (1975, Jan, 1), er)); + + shared_ptr jane ( + new employee ("Jane", "Doe", date (1976, Feb, 2), er)); + + john->emails ().insert ("john_d@example.com"); + john->emails ().insert ("john.doe@example.com"); + jane->emails ().insert ("jane_s@example.com"); + jane->emails ().insert ("jane.smith@example.com"); + + // Set the inverse side of the employee-employer relationship. + // + er->employees ().push_back (john); + er->employees ().push_back (jane); + + transaction t (db->begin ()); + + db->persist (er); + + db->persist (john); + db->persist (jane); + + t.commit (); + } + + // Complex Systems Inc. + // + { + shared_ptr er (new employer ("Complex Systems Inc")); + + shared_ptr john ( + new employee ("John", "Smith", date (1977, Mar, 3), er)); + + shared_ptr jane ( + new employee ("Jane", "Smith", date (1978, Apr, 4), er)); + + john->emails ().insert ("john_s@example.com"); + john->emails ().insert ("john.smith@example.com"); + jane->emails ().insert ("jane_s@example.com"); + jane->emails ().insert ("jane.smith@example.com"); + + // Set the inverse side of the employee-employer relationship. + // + er->employees ().push_back (john); + er->employees ().push_back (jane); + + transaction t (db->begin ()); + + db->persist (er); + + db->persist (john); + db->persist (jane); + + t.commit (); + } + } + + + // Load Simple Tech Ltd and print its employees. + // + { + 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. + + cout << p->first () << " " << p->last () << endl + << " born: " << p->born () << endl + << " employer: " << p->employer ()->name () << endl; + + for (emails::const_iterator j (p->emails ().begin ()); + j != p->emails ().end (); ++j) + { + cout << " email: " << *j << endl; + } + + cout << endl; + } + + t.commit (); + } + + // Search for Complex Systems Inc employees that were born before + // April 1978. + // + { + typedef odb::query query; + typedef odb::result result; + + session s; + transaction t (db->begin ()); + + result r (db->query ( + query::employer::name == "Complex Systems Inc" && + query::born < date (1978, Apr, 1))); + + for (result::iterator i (r.begin ()); i != r.end (); ++i) + cout << i->first () << " " << i->last () << " " << i->born () << endl; + + t.commit (); + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/boost/employee.hxx b/boost/employee.hxx new file mode 100644 index 0000000..7540c44 --- /dev/null +++ b/boost/employee.hxx @@ -0,0 +1,163 @@ +// file : boost/employee.hxx +// author : Boris Kolpackov +// copyright : not copyrighted - public domain + +#ifndef EMPLOYEE_HXX +#define EMPLOYEE_HXX + +#include +#include + +#include +#include +#include +#include + +#include + +#include + +using boost::shared_ptr; +using boost::weak_ptr; + +using odb::boost::lazy_shared_ptr; +using odb::boost::lazy_weak_ptr; + +using boost::gregorian::date; + +// Forward declarations. +// +class employer; +class employee; + +typedef boost::unordered_set emails; +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 employee +{ +public: + typedef ::employer employer_type; + + employee (const std::string& first, + const std::string& last, + const date& born, + shared_ptr employer) + : first_ (first), last_ (last), born_ (born), employer_ (employer) + { + } + + // Name. + // + const std::string& + first () const + { + return first_; + } + + const std::string& + last () const + { + return last_; + } + + // Date of birth. + // + const date& + born () const + { + return born_; + } + + // Emails. + // + typedef ::emails emails_type; + + const emails_type& + emails () const + { + return emails_; + } + + emails_type& + emails () + { + return emails_; + } + + // Employer. + // + shared_ptr + employer () const + { + return employer_; + } + + void + employer (shared_ptr employer) + { + employer_ = employer; + } + +private: + friend class odb::access; + + employee () {} + + #pragma db id auto + unsigned long id_; + + std::string first_; + std::string last_; + + date born_; + emails_type emails_; + + #pragma db not_null + shared_ptr employer_; +}; + +#endif // EMPLOYEE_HXX diff --git a/boost/makefile b/boost/makefile new file mode 100644 index 0000000..1444e46 --- /dev/null +++ b/boost/makefile @@ -0,0 +1,132 @@ +# file : boost/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) + +$(call import,\ + $(scf_root)/import/libodb-boost/stub.make,\ + l: odb_boost.l,cpp-options: odb_boost.l.cpp-options) + +$(call import,\ + $(scf_root)/import/libboost/header-only/stub.make,\ + cpp-options: boost.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_boost.l) $(odb.l) +$(cxx_obj) $(cxx_od): cpp_options := -I$(out_base) +$(cxx_obj) $(cxx_od): $(odb.l.cpp-options) $(odb_boost.l.cpp-options) \ +$(odb_db.l.cpp-options) $(boost.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) --profile boost \ +--generate-query --generate-schema +$(gen): cpp_options := -I$(out_base) +$(gen): $(odb.l.cpp-options) $(odb_boost.l.cpp-options) $(boost.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)) $(call vc9slns,$(name)) $(call vc10slns,$(name)) +$(dist): + $(call dist-data,$(sources) $(headers) README database.hxx) + $(call meta-automake,../template/Makefile.am) + $(call meta-vc9projs,$(name),$(name)) + $(call meta-vc10projs,$(name),$(name)) + $(call meta-vc9slns,$(name)) + $(call meta-vc10slns,$(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/vc9sln.make) +$(call include,$(bld_root)/meta/vc10sln.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