summaryrefslogtreecommitdiff
path: root/odb-examples/boost
diff options
context:
space:
mode:
Diffstat (limited to 'odb-examples/boost')
-rw-r--r--odb-examples/boost/README79
-rw-r--r--odb-examples/boost/buildfile59
-rw-r--r--odb-examples/boost/database.hxx95
-rw-r--r--odb-examples/boost/driver.cxx176
-rw-r--r--odb-examples/boost/employee.hxx196
-rw-r--r--odb-examples/boost/testscript12
6 files changed, 617 insertions, 0 deletions
diff --git a/odb-examples/boost/README b/odb-examples/boost/README
new file mode 100644
index 0000000..dabacfc
--- /dev/null
+++ b/odb-examples/boost/README
@@ -0,0 +1,79 @@
+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
+ and the boost::unordered_set container to keep track of the employee's
+ email addresses. The employee's object id is boost::uuids::uuid. Finally,
+ we use boost::optional for the optional middle name. If the middle name
+ is not present, it will be represented in the database as a NULL value.
+
+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> --profile boost --generate-schema \
+ --generate-query --generate-session employee.hxx
+
+ Where <database> stands for the database system we are using, for example,
+ 'pgsql'.
+
+ The --profile option is used to instruct the ODB compiler to load the Boost
+ profile. The --generate-session option is used to enable session support
+ for all the persistent classes in employee.hxx.
+
+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 few database queries which use data members of the
+ various Boost value types in their 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-boost -lodb-pgsql -lodb
+
+Note that libodb-boost doesn't link any Boost libraries and it is the user's
+responsibility to link the necessary ones.
+
+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/boost/buildfile b/odb-examples/boost/buildfile
new file mode 100644
index 0000000..32d5d8b
--- /dev/null
+++ b/odb-examples/boost/buildfile
@@ -0,0 +1,59 @@
+# file : boost/buildfile
+# license : not copyrighted - public domain
+
+import meta_libs = libodb%lib{odb}
+import meta_libs += libodb-boost%lib{odb-boost}
+import meta_libs += libboost-multi-index%lib{boost_multi_index}
+import meta_libs += libboost-optional%lib{boost_optional}
+import meta_libs += libboost-smart-ptr%lib{boost_smart_ptr}
+import meta_libs += libboost-unordered%lib{boost_unordered}
+import meta_libs += libboost-uuid%lib{boost_uuid}
+import meta_libs += libboost-date-time%lib{boost_date_time}
+
+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}: $meta_libs
+
+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 \
+ --profile boost \
+ --generate-schema \
+ --generate-query \
+ --generate-session \
+ --output-dir $out_base \
+ --table-prefix boost_ \
+ "-I$src_base" $pops \
+ $path($<[0])
+}}
+
+cxx.poptions =+ "-I$out_base" "-I$src_base" -DDATABASE_$ucase($database)
+
+# @@ TMP Until https://github.com/build2-packaging/boost/issues/2 is fixed.
+#
+if ($cxx.target.class == 'windows')
+ cxx.libs += ($cxx.target.system == 'mingw32' ? -lbcrypt : bcrypt.lib)
+
+# 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/boost/database.hxx b/odb-examples/boost/database.hxx
new file mode 100644
index 0000000..96cfa68
--- /dev/null
+++ b/odb-examples/boost/database.hxx
@@ -0,0 +1,95 @@
+// file : boost/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/boost/driver.cxx b/odb-examples/boost/driver.cxx
new file mode 100644
index 0000000..badaeb9
--- /dev/null
+++ b/odb-examples/boost/driver.cxx
@@ -0,0 +1,176 @@
+// file : boost/driver.cxx
+// copyright : not copyrighted - public domain
+
+#include <memory> // std::unique_ptr
+#include <iostream>
+
+#include <boost/uuid/uuid_io.hpp>
+
+#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 boost::gregorian;
+using namespace odb::core;
+
+int
+main (int argc, char* argv[])
+{
+ using boost::shared_ptr;
+
+ 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<employee> john (
+ new employee ("John", "Doe", date (1975, Jan, 1), er));
+
+ shared_ptr<employee> jane (
+ new employee ("Jane", "Q", "Doe", date (1976, Feb, 2), er));
+
+ john->emails ().insert ("john_d@example.com");
+ john->emails ().insert ("john.doe@example.com");
+ jane->emails ().insert ("jane_d@example.com");
+ jane->emails ().insert ("jane.doe@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<employer> er (new employer ("Complex Systems Inc"));
+
+ shared_ptr<employee> john (
+ new employee ("John", "Z", "Smith", date (1977, Mar, 3), er));
+
+ shared_ptr<employee> 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<employer> stl (db->load<employer> ("Simple Tech Ltd"));
+
+ employees& es (stl->employees ());
+
+ for (employees::iterator i (es.begin ()); i != es.end (); ++i)
+ {
+ lazy_weak_ptr<employee>& lwp (*i);
+ shared_ptr<employee> p (lwp.load ()); // Load and lock.
+
+ cout << p->first () << " ";
+
+ if (p->middle ())
+ cout << *p->middle () << " ";
+
+ cout << p->last () << endl;
+
+ cout << " 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 << " id: {" << p->id () << '}' << endl
+ << endl;
+ }
+
+ t.commit ();
+ }
+
+ using query = odb::query<employee>;
+ using result = odb::result<employee>;
+
+ // Search for Complex Systems Inc employees that were born before
+ // April 1978.
+ //
+ {
+ session s;
+ transaction t (db->begin ());
+
+ result r (db->query<employee> (
+ 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;
+
+ cout << endl;
+ t.commit ();
+ }
+
+ // Search for all the employees that don't have a middle name.
+ //
+ {
+ session s;
+ transaction t (db->begin ());
+
+ result r (db->query<employee> (query::middle.is_null ()));
+
+ for (result::iterator i (r.begin ()); i != r.end (); ++i)
+ cout << i->first () << " " << i->last () << endl;
+
+ t.commit ();
+ }
+ }
+ catch (const odb::exception& e)
+ {
+ cerr << e.what () << endl;
+ return 1;
+ }
+}
diff --git a/odb-examples/boost/employee.hxx b/odb-examples/boost/employee.hxx
new file mode 100644
index 0000000..571f514
--- /dev/null
+++ b/odb-examples/boost/employee.hxx
@@ -0,0 +1,196 @@
+// file : boost/employee.hxx
+// copyright : not copyrighted - public domain
+
+#ifndef EMPLOYEE_HXX
+#define EMPLOYEE_HXX
+
+#include <vector>
+#include <string>
+
+#include <boost/shared_ptr.hpp>
+#include <boost/weak_ptr.hpp>
+#include <boost/optional.hpp>
+#include <boost/unordered_set.hpp>
+#include <boost/uuid/uuid.hpp>
+#include <boost/uuid/uuid_generators.hpp>
+#include <boost/date_time/gregorian/gregorian.hpp>
+
+#include <odb/core.hxx>
+
+#include <odb/boost/lazy-ptr.hxx>
+
+using boost::shared_ptr;
+using boost::weak_ptr;
+
+using odb::boost::lazy_shared_ptr;
+using odb::boost::lazy_weak_ptr;
+
+using boost::uuids::uuid;
+using boost::gregorian::date;
+
+// Forward declarations.
+//
+class employer;
+class employee;
+
+using emails = boost::unordered_set<std::string>;
+using employees = std::vector<lazy_weak_ptr<employee>>;
+
+#pragma db object
+class employer
+{
+public:
+ employer (const std::string& name)
+ : name_ (name)
+ {
+ }
+
+ const std::string&
+ name () const
+ {
+ return name_;
+ }
+
+ // Employees of this employer.
+ //
+ using employees_type = ::employees;
+
+ 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 value_not_null inverse(employer_)
+ employees_type employees_;
+};
+
+#pragma db object
+class employee
+{
+public:
+ using employer_type = ::employer;
+
+ employee (const std::string& first,
+ const std::string& last,
+ const date& born,
+ shared_ptr<employer_type> employer)
+ : id_ (boost::uuids::random_generator () ()),
+ first_ (first), last_ (last),
+ born_ (born),
+ employer_ (employer)
+ {
+ }
+
+ employee (const std::string& first,
+ const std::string& middle,
+ const std::string& last,
+ const date& born,
+ shared_ptr<employer_type> employer)
+ : id_ (boost::uuids::random_generator () ()),
+ first_ (first), middle_ (middle), last_ (last),
+ born_ (born),
+ employer_ (employer)
+ {
+ }
+
+ // Id.
+ //
+ const uuid&
+ id () const
+ {
+ return id_;
+ }
+
+ // Name.
+ //
+ const std::string&
+ first () const
+ {
+ return first_;
+ }
+
+ const boost::optional<std::string>&
+ middle () const
+ {
+ return middle_;
+ }
+
+ const std::string&
+ last () const
+ {
+ return last_;
+ }
+
+ // Date of birth.
+ //
+ const date&
+ born () const
+ {
+ return born_;
+ }
+
+ // Emails.
+ //
+ using emails_type = ::emails;
+
+ const emails_type&
+ emails () const
+ {
+ return emails_;
+ }
+
+ emails_type&
+ emails ()
+ {
+ return emails_;
+ }
+
+ // Employer.
+ //
+ shared_ptr<employer_type>
+ employer () const
+ {
+ return employer_;
+ }
+
+ void
+ employer (shared_ptr<employer_type> employer)
+ {
+ employer_ = employer;
+ }
+
+private:
+ friend class odb::access;
+
+ employee () {}
+
+ #pragma db id
+ uuid id_;
+
+ std::string first_;
+ boost::optional<std::string> middle_;
+ std::string last_;
+
+ date born_;
+ emails_type emails_;
+
+ #pragma db not_null
+ shared_ptr<employer_type> employer_;
+};
+
+#endif // EMPLOYEE_HXX
diff --git a/odb-examples/boost/testscript b/odb-examples/boost/testscript
new file mode 100644
index 0000000..9fb5698
--- /dev/null
+++ b/odb-examples/boost/testscript
@@ -0,0 +1,12 @@
+# file : boost/testscript
+# license : not copyrighted - public domain
+
+.include ../database-options.testscript
+.include ../$(database).testscript
+
++if! $sqlite
+ $create_schema
+
+: basics
+:
+$* >|