diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2011-04-26 09:14:56 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2011-04-26 09:14:56 +0200 |
commit | e159da71e25aa50fc73479f9aa7aef7185c28a1c (patch) | |
tree | bf4fae4b9fd76983c69b67a76dc3be8974ce943a | |
parent | ba7a7b6ed8b81df116b1c2352665b03192386793 (diff) |
Add inheritance example
-rw-r--r-- | README | 3 | ||||
-rw-r--r-- | inheritance/README | 55 | ||||
-rw-r--r-- | inheritance/database.hxx | 66 | ||||
-rw-r--r-- | inheritance/driver.cxx | 82 | ||||
-rw-r--r-- | inheritance/employee.hxx | 144 | ||||
-rw-r--r-- | inheritance/makefile | 114 | ||||
-rw-r--r-- | makefile | 10 |
7 files changed, 473 insertions, 1 deletions
@@ -32,6 +32,9 @@ inverse Shows how to declare and use bidirectional one-to-one, one-to-many, and many-to-many relationships. +inheritance + Shows how to use persistent class inheritance. + boost Shows how to persist objects that use Boost smart pointers, containers, and value types with the help of the Boost profile library (libodb-boost). diff --git a/inheritance/README b/inheritance/README new file mode 100644 index 0000000..e1565b8 --- /dev/null +++ b/inheritance/README @@ -0,0 +1,55 @@ +This example shows how to use persistent class inheritance. + +The example consists of the following files: + +employee.hxx + Header file defining the 'person' and 'employee' abstract persistent + classes as well as the 'permanent_employee', 'temporary_employee', and + 'contractor' concrete persistent classes. + +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 employee.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 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 persists a number of permanent and temporary employee + objects as well as a number of contractor objects. The next transaction + looks up a contractor based on the email address. Finally, the driver + performs a database query which uses a data member from the base class + in its criterion. + +To run the example we may first need to create the database schema (for some +database systems, such as SQLite, the schema is embedded into the generated +code which makes this step unnecessary). 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/inheritance/database.hxx b/inheritance/database.hxx new file mode 100644 index 0000000..6d1fa66 --- /dev/null +++ b/inheritance/database.hxx @@ -0,0 +1,66 @@ +// file : inheritance/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> +#elif defined(DATABASE_SQLITE) +# include <odb/transaction.hxx> +# include <odb/schema-catalog.hxx> +# include <odb/sqlite/database.hxx> +#endif + +inline std::auto_ptr<odb::database> +create_database (int& argc, char* argv[]) +{ + using namespace std; + using namespace odb::core; + + if (argc > 1 && argv[1] == string ("--help")) + { + cerr << "Usage: " << argv[0] << " [options]" << endl + << "Options:" << endl; + +#if defined(DATABASE_MYSQL) + odb::mysql::database::print_usage (cerr); +#elif defined(DATABASE_SQLITE) + odb::sqlite::database::print_usage (cerr); +#endif + + exit (0); + } + +#if defined(DATABASE_MYSQL) + auto_ptr<database> db (new odb::mysql::database (argc, argv)); +#elif defined(DATABASE_SQLITE) + auto_ptr<database> db ( + new odb::sqlite::database ( + argc, argv, false, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE)); + + // Create the database schema. + // + { + transaction t (db->begin ()); + schema_catalog::create_schema (*db); + t.commit (); + } +#endif + + return db; +} + +#endif // DATABASE_HXX diff --git a/inheritance/driver.cxx b/inheritance/driver.cxx new file mode 100644 index 0000000..44fd721 --- /dev/null +++ b/inheritance/driver.cxx @@ -0,0 +1,82 @@ +// file : inheritance/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::core; + +int +main (int argc, char* argv[]) +{ + try + { + auto_ptr<database> db (create_database (argc, argv)); + + // Add a few employees and contractors to the database. + // + { + permanent_employee p1 ("John", "Doe"); + permanent_employee p2 ("Jane", "Doe"); + + temporary_employee t1 ("John", "Smith", 6); + temporary_employee t2 ("Jane", "Smith", 12); + + contractor c1 ("Joe", "Doe", "j.doe@example.com"); + contractor c2 ("Joe", "Smith", "j.smith@example.com"); + + transaction t (db->begin ()); + db->persist (p1); + db->persist (p2); + db->persist (t1); + db->persist (t2); + db->persist (c1); + db->persist (c2); + t.commit (); + } + + // Lookup a contractor based on the email address. + // + { + transaction t (db->begin ()); + auto_ptr<contractor> c (db->load<contractor> ("j.smith@example.com")); + t.commit (); + + cout << c->first () << " " << c->last () << " " << c->email () << endl; + } + + // Query for temporary employees that have John as first name. + // + { + typedef odb::query<temporary_employee> query; + typedef odb::result<temporary_employee> result; + + transaction t (db->begin ()); + + result r (db->query<temporary_employee> (query::first == "John")); + + for (result::iterator i (r.begin ()); i != r.end (); ++i) + { + cout << i->first () << " " << i->last () << " " + << i->duration () << " months" << endl; + } + + t.commit (); + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/inheritance/employee.hxx b/inheritance/employee.hxx new file mode 100644 index 0000000..7b68b52 --- /dev/null +++ b/inheritance/employee.hxx @@ -0,0 +1,144 @@ +// file : inheritance/employee.hxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : not copyrighted - public domain + +#ifndef EMPLOYEE_HXX +#define EMPLOYEE_HXX + +#include <string> + +#include <odb/core.hxx> + +// Abstract person class. Note that it does not declare the object id. +// +#pragma db object abstract +class person +{ +public: + person (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_; + } + +protected: + friend class odb::access; + person () {} + +private: + std::string first_; + std::string last_; +}; + +// Abstract employee class. It derives from the person class and declares +// the object id for all the concrete employee types. +// +#pragma db object abstract +class employee: public person +{ +public: + employee (const std::string& first, const std::string& last) + : person (first, last) + { + } + + unsigned long + number () const + { + return id_; + } + +protected: + friend class odb::access; + employee () {} + +private: + #pragma db id auto + unsigned long id_; +}; + +// Concrete permanent_employee class. Note that it doesn't define any +// data members of its own. +// +#pragma db object +class permanent_employee: public employee +{ +public: + permanent_employee (const std::string& first, const std::string& last) + : employee (first, last) + { + } + +private: + friend class odb::access; + permanent_employee () {} +}; + +// Concrete temporary_employee class. It adds the employment duration in +// months. +// +#pragma db object +class temporary_employee: public employee +{ +public: + temporary_employee (const std::string& first, + const std::string& last, + unsigned long duration) + : employee (first, last), duration_ (duration) + { + } + + unsigned long + duration () const + { + return duration_; + } + +private: + friend class odb::access; + temporary_employee () {} + + unsigned long duration_; +}; + +// Concrete contractor class. It derives from the person class (and not +// employee; an independent contractor is not considered an employee). +// We use the contractor's external email address as the object id. +// +#pragma db object +class contractor: public person +{ +public: + contractor (const std::string& first, + const std::string& last, + const std::string& email) + : person (first, last), email_ (email) + { + } + + const std::string& + email () const + { + return email_; + } + +private: + friend class odb::access; + contractor () {} + + #pragma db id + std::string email_; +}; + +#endif // EMPLOYEE_HXX diff --git a/inheritance/makefile b/inheritance/makefile new file mode 100644 index 0000000..8c3aedc --- /dev/null +++ b/inheritance/makefile @@ -0,0 +1,114 @@ +# file : inheritance/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) -I$(src_base) -D$(db_macro) +$(cxx_obj) $(cxx_od): $(odb.l.cpp-options) $(odb_db.l.cpp-options) + +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$(src_base) +$(gen): $(odb.l.cpp-options) + +$(call include-dep,$(cxx_od),$(cxx_obj),$(gen)) + +# Alias for default target. +# +$(out_base)/: $(driver) + +# Dist +# +name := $(subst /,-,$(subst $(src_root)/,,$(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): header := $(odb_hdr) +$(test): $(driver) + $(call 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) + @@ -5,7 +5,15 @@ include $(dir $(lastword $(MAKEFILE_LIST)))build/bootstrap.make -dirs := composite container hello query mapping schema/embedded +dirs := \ +composite \ +container \ +hello \ +inheritance \ +query \ +mapping \ +schema/embedded + tr1_dirs := relationship inverse schema/custom boost_dirs := boost qt_dirs := qt |