summaryrefslogtreecommitdiff
path: root/odb-examples/inheritance/reuse
diff options
context:
space:
mode:
Diffstat (limited to 'odb-examples/inheritance/reuse')
-rw-r--r--odb-examples/inheritance/reuse/README69
-rw-r--r--odb-examples/inheritance/reuse/buildfile45
-rw-r--r--odb-examples/inheritance/reuse/database.hxx95
-rw-r--r--odb-examples/inheritance/reuse/driver.cxx81
-rw-r--r--odb-examples/inheritance/reuse/employee.hxx143
-rw-r--r--odb-examples/inheritance/reuse/testscript13
6 files changed, 446 insertions, 0 deletions
diff --git a/odb-examples/inheritance/reuse/README b/odb-examples/inheritance/reuse/README
new file mode 100644
index 0000000..bb1f69a
--- /dev/null
+++ b/odb-examples/inheritance/reuse/README
@@ -0,0 +1,69 @@
+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 --std c++11 -d <database> --generate-schema --generate-query employee.hxx
+
+ Where <database> stands for the database system we are using, for example,
+ 'pgsql'.
+
+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 compile and link the example manually from the command line we can use the
+following commands (using PostgreSQL as an example; replace 'c++' with your
+C++ compiler name):
+
+c++ -c employee-odb.cxx
+c++ -DDATABASE_PGSQL -c driver.cxx
+c++ -o driver driver.o employee-odb.o -lodb-pgsql -lodb
+
+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 PostgreSQL as an example, this
+can be achieved with the following command:
+
+psql --username=odb_test --dbname=odb_test -f 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 PostgreSQL as
+the database):
+
+./driver --user odb_test --database odb_test
diff --git a/odb-examples/inheritance/reuse/buildfile b/odb-examples/inheritance/reuse/buildfile
new file mode 100644
index 0000000..7190d39
--- /dev/null
+++ b/odb-examples/inheritance/reuse/buildfile
@@ -0,0 +1,45 @@
+# file : inheritance/reuse/buildfile
+# license : GNU GPL v2; see accompanying LICENSE file
+
+import libodb = libodb%lib{odb}
+
+import libs = libodb-$database%lib{odb-$database}
+
+exe{driver}: {hxx cxx}{* -employee-odb} {hxx ixx cxx}{employee-odb} testscript
+
+# The metadata library target which we use to extract the poptions variable
+# value for specifying the preprocessor options (-I, etc) on the ODB compiler
+# command line.
+#
+libue{employee-meta}: $libodb
+
+exe{driver}: libue{employee-meta} $libs
+
+<{hxx ixx cxx}{employee-odb}>: hxx{employee} libue{employee-meta} $odb
+{{
+ pops = $cxx.lib_poptions($<[1])
+ depdb hash $pops
+
+ depdb dyndep --dyn-target --target-what 'generated schema' --format lines \
+ -- echo ($sqlite ? '' : "$out_base/employee.sql")
+
+ $odb --std c++11 \
+ --database $database \
+ --generate-schema \
+ --generate-query \
+ --output-dir $out_base \
+ --table-prefix inh_reuse_ \
+ "-I$src_base" $pops \
+ $path($<[0])
+}}
+
+cxx.poptions =+ "-I$out_base" "-I$src_base" -DDATABASE_$ucase($database)
+
+# Testscript's run-time prerequisites.
+#
+# @@ BUILD2: Eventually we should be able to mark it as test.input once
+# this is supported for testscript tests.
+#
+exe{driver}: ../../alias{database-client}: include = adhoc
+
+testscript@./: schema = employee
diff --git a/odb-examples/inheritance/reuse/database.hxx b/odb-examples/inheritance/reuse/database.hxx
new file mode 100644
index 0000000..f9d3a61
--- /dev/null
+++ b/odb-examples/inheritance/reuse/database.hxx
@@ -0,0 +1,95 @@
+// 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 <string>
+#include <memory> // std::unique_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/connection.hxx>
+# include <odb/transaction.hxx>
+# include <odb/schema-catalog.hxx>
+# include <odb/sqlite/database.hxx>
+#elif defined(DATABASE_PGSQL)
+# include <odb/pgsql/database.hxx>
+#elif defined(DATABASE_ORACLE)
+# include <odb/oracle/database.hxx>
+#elif defined(DATABASE_MSSQL)
+# include <odb/mssql/database.hxx>
+#else
+# error unknown database; did you forget to define the DATABASE_* macros?
+#endif
+
+inline std::unique_ptr<odb::database>
+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)
+ unique_ptr<database> db (new odb::mysql::database (argc, argv));
+#elif defined(DATABASE_SQLITE)
+ unique_ptr<database> 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)
+ unique_ptr<database> db (new odb::pgsql::database (argc, argv));
+#elif defined(DATABASE_ORACLE)
+ unique_ptr<database> db (new odb::oracle::database (argc, argv));
+#elif defined(DATABASE_MSSQL)
+ unique_ptr<database> db (
+ new odb::mssql::database (argc, argv, false, "TrustServerCertificate=yes"));
+#endif
+
+ return db;
+}
+
+#endif // DATABASE_HXX
diff --git a/odb-examples/inheritance/reuse/driver.cxx b/odb-examples/inheritance/reuse/driver.cxx
new file mode 100644
index 0000000..b56a368
--- /dev/null
+++ b/odb-examples/inheritance/reuse/driver.cxx
@@ -0,0 +1,81 @@
+// file : inheritance/reuse/driver.cxx
+// copyright : not copyrighted - public domain
+
+#include <memory> // std::unique_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
+ {
+ unique_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 ());
+ unique_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 the 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/odb-examples/inheritance/reuse/employee.hxx b/odb-examples/inheritance/reuse/employee.hxx
new file mode 100644
index 0000000..7de3989
--- /dev/null
+++ b/odb-examples/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 <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/odb-examples/inheritance/reuse/testscript b/odb-examples/inheritance/reuse/testscript
new file mode 100644
index 0000000..9b24b63
--- /dev/null
+++ b/odb-examples/inheritance/reuse/testscript
@@ -0,0 +1,13 @@
+# file : inheritance/reuse/testscript
+# license : GNU GPL v2; see accompanying LICENSE file
+
+.include ../../database-options.testscript
+.include ../../$(database).testscript
+
++if! $sqlite
+ $create_schema
+end
+
+: basics
+:
+$* >|