From ab994fdada3eebc794d6b1686f55a35420e4d758 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Wed, 25 Apr 2012 10:45:32 +0200 Subject: New example, inheritance/polymorphism Also move the inheritance example to inheritance/reuse. --- inheritance/README | 55 ------------- inheritance/database.hxx | 92 ---------------------- inheritance/driver.cxx | 81 ------------------- inheritance/employee.hxx | 143 ---------------------------------- inheritance/makefile | 118 ---------------------------- inheritance/polymorphism/README | 69 ++++++++++++++++ inheritance/polymorphism/database.hxx | 92 ++++++++++++++++++++++ inheritance/polymorphism/driver.cxx | 102 ++++++++++++++++++++++++ inheritance/polymorphism/employee.cxx | 27 +++++++ inheritance/polymorphism/employee.hxx | 109 ++++++++++++++++++++++++++ inheritance/polymorphism/makefile | 118 ++++++++++++++++++++++++++++ inheritance/reuse/README | 61 +++++++++++++++ inheritance/reuse/database.hxx | 92 ++++++++++++++++++++++ inheritance/reuse/driver.cxx | 81 +++++++++++++++++++ inheritance/reuse/employee.hxx | 143 ++++++++++++++++++++++++++++++++++ inheritance/reuse/makefile | 118 ++++++++++++++++++++++++++++ 16 files changed, 1012 insertions(+), 489 deletions(-) delete mode 100644 inheritance/README delete mode 100644 inheritance/database.hxx delete mode 100644 inheritance/driver.cxx delete mode 100644 inheritance/employee.hxx delete mode 100644 inheritance/makefile create mode 100644 inheritance/polymorphism/README create mode 100644 inheritance/polymorphism/database.hxx create mode 100644 inheritance/polymorphism/driver.cxx create mode 100644 inheritance/polymorphism/employee.cxx create mode 100644 inheritance/polymorphism/employee.hxx create mode 100644 inheritance/polymorphism/makefile create mode 100644 inheritance/reuse/README create mode 100644 inheritance/reuse/database.hxx create mode 100644 inheritance/reuse/driver.cxx create mode 100644 inheritance/reuse/employee.hxx create mode 100644 inheritance/reuse/makefile (limited to 'inheritance') diff --git a/inheritance/README b/inheritance/README deleted file mode 100644 index e1565b8..0000000 --- a/inheritance/README +++ /dev/null @@ -1,55 +0,0 @@ -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 --generate-schema --generate-query employee.hxx - - Where 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 deleted file mode 100644 index af048c4..0000000 --- a/inheritance/database.hxx +++ /dev/null @@ -1,92 +0,0 @@ -// file : inheritance/database.hxx -// 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 -#elif defined(DATABASE_SQLITE) -# include -# include -# include -# include -#elif defined(DATABASE_PGSQL) -# include -#elif defined(DATABASE_ORACLE) -# include -#elif defined(DATABASE_MSSQL) -# include -#endif - -inline std::auto_ptr -create_database (int& argc, char* argv[]) -{ - using namespace std; - using namespace odb::core; - - if (argc > 1 && argv[1] == string ("--help")) - { - cout << "Usage: " << argv[0] << " [options]" << endl - << "Options:" << endl; - -#if defined(DATABASE_MYSQL) - odb::mysql::database::print_usage (cout); -#elif defined(DATABASE_SQLITE) - odb::sqlite::database::print_usage (cout); -#elif defined(DATABASE_PGSQL) - odb::pgsql::database::print_usage (cout); -#elif defined(DATABASE_ORACLE) - odb::oracle::database::print_usage (cout); -#elif defined(DATABASE_MSSQL) - odb::mssql::database::print_usage (cout); -#endif - - exit (0); - } - -#if defined(DATABASE_MYSQL) - auto_ptr db (new odb::mysql::database (argc, argv)); -#elif defined(DATABASE_SQLITE) - auto_ptr db ( - new odb::sqlite::database ( - argc, argv, false, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE)); - - // Create the database schema. Due to bugs in SQLite foreign key - // support for DDL statements, we need to temporarily disable - // foreign keys. - // - { - connection_ptr c (db->connection ()); - - c->execute ("PRAGMA foreign_keys=OFF"); - - transaction t (c->begin ()); - schema_catalog::create_schema (*db); - t.commit (); - - c->execute ("PRAGMA foreign_keys=ON"); - } -#elif defined(DATABASE_PGSQL) - auto_ptr db (new odb::pgsql::database (argc, argv)); -#elif defined(DATABASE_ORACLE) - auto_ptr db (new odb::oracle::database (argc, argv)); -#elif defined(DATABASE_MSSQL) - auto_ptr db (new odb::mssql::database (argc, argv)); -#endif - - return db; -} - -#endif // DATABASE_HXX diff --git a/inheritance/driver.cxx b/inheritance/driver.cxx deleted file mode 100644 index 38964af..0000000 --- a/inheritance/driver.cxx +++ /dev/null @@ -1,81 +0,0 @@ -// file : inheritance/driver.cxx -// 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::core; - -int -main (int argc, char* argv[]) -{ - try - { - auto_ptr 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 c (db->load ("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 query; - typedef odb::result result; - - transaction t (db->begin ()); - - result r (db->query (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 deleted file mode 100644 index 62b1330..0000000 --- a/inheritance/employee.hxx +++ /dev/null @@ -1,143 +0,0 @@ -// file : inheritance/employee.hxx -// copyright : not copyrighted - public domain - -#ifndef EMPLOYEE_HXX -#define EMPLOYEE_HXX - -#include - -#include - -// 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 deleted file mode 100644 index 7f66d28..0000000 --- a/inheritance/makefile +++ /dev/null @@ -1,118 +0,0 @@ -# file : inheritance/makefile -# copyright : Copyright (c) 2009-2012 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 --table-prefix inherit_ -$(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,$(bld_root)/cxx/standard.make) # cxx_standard -ifdef cxx_standard -$(gen): odb_options += --std $(cxx_standard) -$(call include,$(odb_rules)) -endif - -$(call include,$(bld_root)/cxx/cxx-d.make) -$(call include,$(bld_root)/cxx/cxx-o.make) -$(call include,$(bld_root)/cxx/o-e.make) - diff --git a/inheritance/polymorphism/README b/inheritance/polymorphism/README new file mode 100644 index 0000000..4fb3e15 --- /dev/null +++ b/inheritance/polymorphism/README @@ -0,0 +1,69 @@ +This example shows how to use polymorphism inheritance with ODB. This +inheritance style is normally used to provide polymorphic behavior through +a common interface. The base class defines a number of virtual functions and, +normally, a virtual destructor while the derived classes provide specific +implementations of these virtual functions. + +The other commonly used inheritance style is reuse inheritance. Refer to the +inheritance/reuse example for more information on this style. + +The example consists of the following files: + +employee.hxx +employee.cxx + Header and source files defining the 'person' abstract polymorphic + persistent class as well as the '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 --generate-schema --generate-query employee.hxx + + Where 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 employee and contractor objects + via their common base (person). The next transaction loads these objects, + also via their common base. Once loaded, the driver calls the print() + virtual function on each of them. Next, the driver changes an employee + from temporary to permanent and updates its state in the database, again + using the base class interface. The driver then queries the database for + all the person objects that have Doe as the last name. The result set of + this query contains a mix of employee and contractor objects. The driver + iterates over this result set and calls the print() virtual function for + each object. Finally, the driver erases the state of the persistent + objects from the database, again using the base class interface. + +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/polymorphism/database.hxx b/inheritance/polymorphism/database.hxx new file mode 100644 index 0000000..403b170 --- /dev/null +++ b/inheritance/polymorphism/database.hxx @@ -0,0 +1,92 @@ +// file : inheritance/polymorphism/database.hxx +// 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 +#elif defined(DATABASE_SQLITE) +# include +# include +# include +# include +#elif defined(DATABASE_PGSQL) +# include +#elif defined(DATABASE_ORACLE) +# include +#elif defined(DATABASE_MSSQL) +# include +#endif + +inline std::auto_ptr +create_database (int& argc, char* argv[]) +{ + using namespace std; + using namespace odb::core; + + if (argc > 1 && argv[1] == string ("--help")) + { + cout << "Usage: " << argv[0] << " [options]" << endl + << "Options:" << endl; + +#if defined(DATABASE_MYSQL) + odb::mysql::database::print_usage (cout); +#elif defined(DATABASE_SQLITE) + odb::sqlite::database::print_usage (cout); +#elif defined(DATABASE_PGSQL) + odb::pgsql::database::print_usage (cout); +#elif defined(DATABASE_ORACLE) + odb::oracle::database::print_usage (cout); +#elif defined(DATABASE_MSSQL) + odb::mssql::database::print_usage (cout); +#endif + + exit (0); + } + +#if defined(DATABASE_MYSQL) + auto_ptr db (new odb::mysql::database (argc, argv)); +#elif defined(DATABASE_SQLITE) + auto_ptr db ( + new odb::sqlite::database ( + argc, argv, false, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE)); + + // Create the database schema. Due to bugs in SQLite foreign key + // support for DDL statements, we need to temporarily disable + // foreign keys. + // + { + connection_ptr c (db->connection ()); + + c->execute ("PRAGMA foreign_keys=OFF"); + + transaction t (c->begin ()); + schema_catalog::create_schema (*db); + t.commit (); + + c->execute ("PRAGMA foreign_keys=ON"); + } +#elif defined(DATABASE_PGSQL) + auto_ptr db (new odb::pgsql::database (argc, argv)); +#elif defined(DATABASE_ORACLE) + auto_ptr db (new odb::oracle::database (argc, argv)); +#elif defined(DATABASE_MSSQL) + auto_ptr db (new odb::mssql::database (argc, argv)); +#endif + + return db; +} + +#endif // DATABASE_HXX diff --git a/inheritance/polymorphism/driver.cxx b/inheritance/polymorphism/driver.cxx new file mode 100644 index 0000000..e0c9952 --- /dev/null +++ b/inheritance/polymorphism/driver.cxx @@ -0,0 +1,102 @@ +// file : inheritance/polymorphism/driver.cxx +// 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::core; + +int +main (int argc, char* argv[]) +{ + try + { + auto_ptr db (create_database (argc, argv)); + + unsigned long id1, id2; + + // Add a few employee and contractor objects to the database. + // + { + auto_ptr p1 (new employee ("John", "Doe", true)); + auto_ptr p2 (new contractor ("Jane", "Doe", "jane@doe.com")); + + transaction t (db->begin ()); + id1 = db->persist (*p1); // Stores employee. + id2 = db->persist (*p2); // Stores contractor. + t.commit (); + } + + // Load polymorphic objects given their object ids. + // + { + transaction t (db->begin ()); + auto_ptr p1 (db->load (id1)); // Loads employee. + auto_ptr p2 (db->load (id2)); // Loads contractor. + t.commit (); + + p1->print (); + p2->print (); + } + + // Make John Doe a permanent employee. + // + { + transaction t (db->begin ()); + + auto_ptr e (db->load (id1)); + e->temporary (false); + person& p (*e); + db->update (p); // Updates employee. + + t.commit (); + } + + // Query all the person objects that have Doe as the last name. + // + { + typedef odb::query query; + typedef odb::result result; + + transaction t (db->begin ()); + + result r (db->query (query::last == "Doe")); + + for (result::iterator i (r.begin ()); i != r.end (); ++i) + { + // We can check the discriminator before loading the object. + // + string d (i.discriminator ()); + cout << "discriminator: " << d << endl; + + i->print (); // Can be employee or contractor. + } + + t.commit (); + } + + // Erase the objects from the database. + // + { + transaction t (db->begin ()); + auto_ptr p (db->load (id1)); // Loads employee. + db->erase (*p); // Erases employee. + db->erase (id2); // Erases contractor. + t.commit (); + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/inheritance/polymorphism/employee.cxx b/inheritance/polymorphism/employee.cxx new file mode 100644 index 0000000..83a51e6 --- /dev/null +++ b/inheritance/polymorphism/employee.cxx @@ -0,0 +1,27 @@ +// file : inheritance/polymorphism/employee.cxx +// copyright : not copyrighted - public domain + +#include + +#include "employee.hxx" + +using namespace std; + +person:: +~person () +{ +} + +void employee:: +print () +{ + cout << first_ << ' ' << last_ + << (temporary_ ? " temporary " : " permanent ") + << "employee" << endl; +} + +void contractor:: +print () +{ + cout << first_ << ' ' << last_ << ' ' << email_ << " contractor" << endl; +} diff --git a/inheritance/polymorphism/employee.hxx b/inheritance/polymorphism/employee.hxx new file mode 100644 index 0000000..efb2c1c --- /dev/null +++ b/inheritance/polymorphism/employee.hxx @@ -0,0 +1,109 @@ +// file : inheritance/polymorphism/employee.hxx +// copyright : not copyrighted - public domain + +#ifndef EMPLOYEE_HXX +#define EMPLOYEE_HXX + +#include + +#include + +#pragma db object polymorphic +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_; + } + + virtual + ~person () = 0; + + virtual void + print () = 0; + +protected: + friend class odb::access; + person () {} + + #pragma db id auto + unsigned long id_; + + std::string first_; + std::string last_; +}; + +#pragma db object +class employee: public person +{ +public: + employee (const std::string& first, + const std::string& last, + bool temporary) + : person (first, last), temporary_ (temporary) + { + } + + bool + temporary () const + { + return temporary_; + } + + void + temporary (bool t) + { + temporary_ = t; + } + + virtual void + print (); + +private: + friend class odb::access; + employee () {} + + bool temporary_; +}; + +#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_; + } + + virtual void + print (); + +private: + friend class odb::access; + contractor () {} + + std::string email_; +}; + +#endif // EMPLOYEE_HXX diff --git a/inheritance/polymorphism/makefile b/inheritance/polymorphism/makefile new file mode 100644 index 0000000..f3538ae --- /dev/null +++ b/inheritance/polymorphism/makefile @@ -0,0 +1,118 @@ +# file : inheritance/polymorphism/makefile +# copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +# license : GNU GPL v2; see accompanying LICENSE file + +include $(dir $(lastword $(MAKEFILE_LIST)))../../build/bootstrap.make + +cxx_tun := driver.cxx employee.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 --table-prefix inh_poly_ +$(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,$(bld_root)/cxx/standard.make) # cxx_standard +ifdef cxx_standard +$(gen): odb_options += --std $(cxx_standard) +$(call include,$(odb_rules)) +endif + +$(call include,$(bld_root)/cxx/cxx-d.make) +$(call include,$(bld_root)/cxx/cxx-o.make) +$(call include,$(bld_root)/cxx/o-e.make) + diff --git a/inheritance/reuse/README b/inheritance/reuse/README new file mode 100644 index 0000000..8cba573 --- /dev/null +++ b/inheritance/reuse/README @@ -0,0 +1,61 @@ +This example shows how to use reuse inheritance with ODB. This inheritance +style normally lacks virtual functions and a virtual destructor in the base +class. The application code is normally written in terms of the derived +classes instead of the base. + +The other commonly used inheritance style is polymorphism inheritance. Refer +to the inheritance/polymorphism example for more information on this style. + +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 --generate-schema --generate-query employee.hxx + + Where 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/reuse/database.hxx b/inheritance/reuse/database.hxx new file mode 100644 index 0000000..605c07f --- /dev/null +++ b/inheritance/reuse/database.hxx @@ -0,0 +1,92 @@ +// file : inheritance/reuse/database.hxx +// 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 +#elif defined(DATABASE_SQLITE) +# include +# include +# include +# include +#elif defined(DATABASE_PGSQL) +# include +#elif defined(DATABASE_ORACLE) +# include +#elif defined(DATABASE_MSSQL) +# include +#endif + +inline std::auto_ptr +create_database (int& argc, char* argv[]) +{ + using namespace std; + using namespace odb::core; + + if (argc > 1 && argv[1] == string ("--help")) + { + cout << "Usage: " << argv[0] << " [options]" << endl + << "Options:" << endl; + +#if defined(DATABASE_MYSQL) + odb::mysql::database::print_usage (cout); +#elif defined(DATABASE_SQLITE) + odb::sqlite::database::print_usage (cout); +#elif defined(DATABASE_PGSQL) + odb::pgsql::database::print_usage (cout); +#elif defined(DATABASE_ORACLE) + odb::oracle::database::print_usage (cout); +#elif defined(DATABASE_MSSQL) + odb::mssql::database::print_usage (cout); +#endif + + exit (0); + } + +#if defined(DATABASE_MYSQL) + auto_ptr db (new odb::mysql::database (argc, argv)); +#elif defined(DATABASE_SQLITE) + auto_ptr db ( + new odb::sqlite::database ( + argc, argv, false, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE)); + + // Create the database schema. Due to bugs in SQLite foreign key + // support for DDL statements, we need to temporarily disable + // foreign keys. + // + { + connection_ptr c (db->connection ()); + + c->execute ("PRAGMA foreign_keys=OFF"); + + transaction t (c->begin ()); + schema_catalog::create_schema (*db); + t.commit (); + + c->execute ("PRAGMA foreign_keys=ON"); + } +#elif defined(DATABASE_PGSQL) + auto_ptr db (new odb::pgsql::database (argc, argv)); +#elif defined(DATABASE_ORACLE) + auto_ptr db (new odb::oracle::database (argc, argv)); +#elif defined(DATABASE_MSSQL) + auto_ptr db (new odb::mssql::database (argc, argv)); +#endif + + return db; +} + +#endif // DATABASE_HXX diff --git a/inheritance/reuse/driver.cxx b/inheritance/reuse/driver.cxx new file mode 100644 index 0000000..b9a22da --- /dev/null +++ b/inheritance/reuse/driver.cxx @@ -0,0 +1,81 @@ +// file : inheritance/reuse/driver.cxx +// 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::core; + +int +main (int argc, char* argv[]) +{ + try + { + auto_ptr 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 c (db->load ("j.smith@example.com")); + t.commit (); + + cout << c->first () << " " << c->last () << " " << c->email () << endl; + } + + // Query for temporary employees that have John as the first name. + // + { + typedef odb::query query; + typedef odb::result result; + + transaction t (db->begin ()); + + result r (db->query (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/reuse/employee.hxx b/inheritance/reuse/employee.hxx new file mode 100644 index 0000000..7de3989 --- /dev/null +++ b/inheritance/reuse/employee.hxx @@ -0,0 +1,143 @@ +// file : inheritance/reuse/employee.hxx +// copyright : not copyrighted - public domain + +#ifndef EMPLOYEE_HXX +#define EMPLOYEE_HXX + +#include + +#include + +// 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/reuse/makefile b/inheritance/reuse/makefile new file mode 100644 index 0000000..32af647 --- /dev/null +++ b/inheritance/reuse/makefile @@ -0,0 +1,118 @@ +# file : inheritance/reuse/makefile +# copyright : Copyright (c) 2009-2012 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 --table-prefix inh_reuse_ +$(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,$(bld_root)/cxx/standard.make) # cxx_standard +ifdef cxx_standard +$(gen): odb_options += --std $(cxx_standard) +$(call include,$(odb_rules)) +endif + +$(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