summaryrefslogtreecommitdiff
path: root/odb-examples/relationship
diff options
context:
space:
mode:
Diffstat (limited to 'odb-examples/relationship')
-rw-r--r--odb-examples/relationship/README70
-rw-r--r--odb-examples/relationship/buildfile47
-rw-r--r--odb-examples/relationship/database.hxx95
-rw-r--r--odb-examples/relationship/driver.cxx167
-rw-r--r--odb-examples/relationship/employee.hxx154
-rw-r--r--odb-examples/relationship/testscript13
6 files changed, 546 insertions, 0 deletions
diff --git a/odb-examples/relationship/README b/odb-examples/relationship/README
new file mode 100644
index 0000000..fd4e3cd
--- /dev/null
+++ b/odb-examples/relationship/README
@@ -0,0 +1,70 @@
+This example shows how to declare and use unidirectional to-one and to-many
+relationships between persistent objects.
+
+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 --std c++11 -d <database> --generate-schema --generate-query \
+ --generate-session --default-pointer std::shared_ptr employee.hxx
+
+ Where <database> stands for the database system we are using, for example,
+ 'pgsql'.
+
+ The --generate-session option is used to enable session support for all
+ the persistent classes in employee.hxx. The --default-pointer option is
+ used to make std::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 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', '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 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/relationship/buildfile b/odb-examples/relationship/buildfile
new file mode 100644
index 0000000..2b4ffed
--- /dev/null
+++ b/odb-examples/relationship/buildfile
@@ -0,0 +1,47 @@
+# file : relationship/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 \
+ --generate-session \
+ --default-pointer std::shared_ptr \
+ --output-dir $out_base \
+ --table-prefix relation_ \
+ "-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/relationship/database.hxx b/odb-examples/relationship/database.hxx
new file mode 100644
index 0000000..17d8393
--- /dev/null
+++ b/odb-examples/relationship/database.hxx
@@ -0,0 +1,95 @@
+// file : relationship/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/relationship/driver.cxx b/odb-examples/relationship/driver.cxx
new file mode 100644
index 0000000..1f74824
--- /dev/null
+++ b/odb-examples/relationship/driver.cxx
@@ -0,0 +1,167 @@
+// file : relationship/driver.cxx
+// copyright : not copyrighted - public domain
+
+#include <memory> // std::unique_ptr, std::shared_ptr
+#include <iostream>
+
+#include <odb/database.hxx>
+#include <odb/session.hxx>
+#include <odb/transaction.hxx>
+
+#include "database.hxx" // create_database
+
+#include "employee.hxx"
+#include "employee-odb.hxx"
+
+using namespace std;
+using namespace odb::core;
+
+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
+ {
+ unique_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"));
+
+ shared_ptr<employee> john (
+ db->query_one<employee> (query::first == "John" &&
+ query::last == "Doe"));
+
+ 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/odb-examples/relationship/employee.hxx b/odb-examples/relationship/employee.hxx
new file mode 100644
index 0000000..4133c05
--- /dev/null
+++ b/odb-examples/relationship/employee.hxx
@@ -0,0 +1,154 @@
+// file : relationship/employee.hxx
+// copyright : not copyrighted - public domain
+
+#ifndef EMPLOYEE_HXX
+#define EMPLOYEE_HXX
+
+#include <vector>
+#include <string>
+#include <memory> // std::shared_ptr
+
+#include <odb/core.hxx>
+
+// 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<std::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,
+ std::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.
+ //
+ std::shared_ptr<employer_type>
+ employer () const
+ {
+ return employer_;
+ }
+
+ void
+ employer (std::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
+ std::shared_ptr<employer_type> employer_;
+
+ #pragma db value_not_null unordered
+ projects_type projects_;
+};
+
+#endif // EMPLOYEE_HXX
diff --git a/odb-examples/relationship/testscript b/odb-examples/relationship/testscript
new file mode 100644
index 0000000..d23eb2d
--- /dev/null
+++ b/odb-examples/relationship/testscript
@@ -0,0 +1,13 @@
+# file : relationship/testscript
+# license : GNU GPL v2; see accompanying LICENSE file
+
+.include ../database-options.testscript
+.include ../$(database).testscript
+
++if! $sqlite
+ $create_schema
+end
+
+: basics
+:
+$* >|