summaryrefslogtreecommitdiff
path: root/odb-examples/container
diff options
context:
space:
mode:
Diffstat (limited to 'odb-examples/container')
-rw-r--r--odb-examples/container/README62
-rw-r--r--odb-examples/container/buildfile44
-rw-r--r--odb-examples/container/database.hxx95
-rw-r--r--odb-examples/container/driver.cxx119
-rw-r--r--odb-examples/container/person.hxx99
-rw-r--r--odb-examples/container/testscript13
6 files changed, 432 insertions, 0 deletions
diff --git a/odb-examples/container/README b/odb-examples/container/README
new file mode 100644
index 0000000..d11d676
--- /dev/null
+++ b/odb-examples/container/README
@@ -0,0 +1,62 @@
+This example shows how to use containers as data members in persistent objects.
+
+The example consists of the following files:
+
+person.hxx
+ Header file defining the 'person' persistent class. It contains a number of
+ data members of various container types.
+
+person-odb.hxx
+person-odb.ixx
+person-odb.cxx
+person.sql
+ The first three files contain the database support code and the last file
+ contains the database schema for the person.hxx header.
+
+ These files are generated by the ODB compiler from person.hxx using the
+ following command line:
+
+ odb --std c++11 -d <database> --generate-schema person.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 person.hxx and person-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 'person' object, loads it back, and prints the
+ contents of its members. Finally, the driver modifies the object by adding,
+ removing, and updating elements in its container members, stores the changes
+ in the database, then re-loads and prints the object to verify that the
+ changes have been made persistent.
+
+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 person-odb.cxx
+c++ -DDATABASE_PGSQL -c driver.cxx
+c++ -o driver driver.o person-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 person.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/container/buildfile b/odb-examples/container/buildfile
new file mode 100644
index 0000000..a045a3b
--- /dev/null
+++ b/odb-examples/container/buildfile
@@ -0,0 +1,44 @@
+# file : container/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}{* -person-odb} {hxx ixx cxx}{person-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{person-meta}: $libodb
+
+exe{driver}: libue{person-meta} $libs
+
+<{hxx ixx cxx}{person-odb}>: hxx{person} libue{person-meta} $odb
+{{
+ pops = $cxx.lib_poptions($<[1])
+ depdb hash $pops
+
+ depdb dyndep --dyn-target --target-what 'generated schema' --format lines \
+ -- echo ($sqlite ? '' : "$out_base/person.sql")
+
+ $odb --std c++11 \
+ --database $database \
+ --generate-schema \
+ --output-dir $out_base \
+ --table-prefix container_ \
+ "-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 = person
diff --git a/odb-examples/container/database.hxx b/odb-examples/container/database.hxx
new file mode 100644
index 0000000..3d23d86
--- /dev/null
+++ b/odb-examples/container/database.hxx
@@ -0,0 +1,95 @@
+// file : container/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/container/driver.cxx b/odb-examples/container/driver.cxx
new file mode 100644
index 0000000..b3d50c6
--- /dev/null
+++ b/odb-examples/container/driver.cxx
@@ -0,0 +1,119 @@
+// file : container/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 "person.hxx"
+#include "person-odb.hxx"
+
+using namespace std;
+using namespace odb::core;
+
+void
+print (const person& p)
+{
+ cout << p.first () << " " << p.last () << endl;
+
+ // Print nicknames.
+ //
+ for (names::const_iterator i (p.nicknames ().begin ());
+ i != p.nicknames ().end (); ++i)
+ {
+ cout << " nickname: " << *i << endl;
+ }
+
+ // Print emails.
+ //
+ for (emails::const_iterator i (p.emails ().begin ());
+ i != p.emails ().end (); ++i)
+ {
+ cout << " email: " << *i << endl;
+ }
+
+ // Print weights.
+ //
+ for (age_weight_map::const_iterator i (p.age_weight ().begin ());
+ i != p.age_weight ().end (); ++i)
+ {
+ cout << " weight at " << i->first << ": " << i->second << endl;
+ }
+}
+
+int
+main (int argc, char* argv[])
+{
+ try
+ {
+ unique_ptr<database> db (create_database (argc, argv));
+
+ unsigned long id;
+
+ // Create a persistent person object.
+ //
+ {
+ person joe ("Joe", "Dirt");
+
+ joe.nicknames ().push_back ("JD");
+ joe.nicknames ().push_back ("Squeaky");
+
+ joe.emails ().insert ("joe@example.com");
+ joe.emails ().insert ("joe.dirt@example.com");
+
+ joe.age_weight ()[5] = 15.6F;
+ joe.age_weight ()[10] = 23.3F;
+ joe.age_weight ()[15] = 29.8F;
+
+ transaction t (db->begin ());
+ id = db->persist (joe);
+ t.commit ();
+ }
+
+ // Load the object and print what we've got. Then change some
+ // information and update it in the database.
+ //
+ {
+ transaction t1 (db->begin ());
+ unique_ptr<person> j (db->load<person> (id));
+ t1.commit ();
+
+ print (*j);
+
+ // Ann another nickname.
+ //
+ j->nicknames ().push_back ("Cleaner");
+
+ // The joe@example.com email is no longer working.
+ //
+ j->emails ().erase ("joe@example.com");
+
+ // Update a weight sample.
+ //
+ j->age_weight ()[15] = 28.8F;
+
+ transaction t2 (db->begin ());
+ db->update (*j);
+ t2.commit ();
+ }
+
+ // Load and print the updated object.
+ //
+ {
+ transaction t (db->begin ());
+ unique_ptr<person> j (db->load<person> (id));
+ t.commit ();
+
+ print (*j);
+ }
+ }
+ catch (const odb::exception& e)
+ {
+ cerr << e.what () << endl;
+ return 1;
+ }
+}
diff --git a/odb-examples/container/person.hxx b/odb-examples/container/person.hxx
new file mode 100644
index 0000000..31b90a5
--- /dev/null
+++ b/odb-examples/container/person.hxx
@@ -0,0 +1,99 @@
+// file : container/person.hxx
+// copyright : not copyrighted - public domain
+
+#ifndef PERSON_HXX
+#define PERSON_HXX
+
+#include <set>
+#include <map>
+#include <vector>
+#include <string>
+
+#include <odb/core.hxx>
+
+typedef std::vector<std::string> names;
+typedef std::set<std::string> emails;
+typedef std::map<unsigned short, float> age_weight_map;
+
+#pragma db object
+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_;
+ }
+
+ // Nicknames.
+ //
+ const names&
+ nicknames () const
+ {
+ return nicknames_;
+ }
+
+ names&
+ nicknames ()
+ {
+ return nicknames_;
+ }
+
+ // Emails.
+ //
+ typedef ::emails emails_type;
+
+ const emails_type&
+ emails () const
+ {
+ return emails_;
+ }
+
+ emails_type&
+ emails ()
+ {
+ return emails_;
+ }
+
+ // Age-to-weight map.
+ //
+ const age_weight_map&
+ age_weight () const
+ {
+ return age_weight_;
+ }
+
+ age_weight_map&
+ age_weight ()
+ {
+ return age_weight_;
+ }
+
+private:
+ friend class odb::access;
+
+ person () {}
+
+ #pragma db id auto
+ unsigned long id_;
+
+ std::string first_;
+ std::string last_;
+
+ names nicknames_;
+ emails_type emails_;
+ age_weight_map age_weight_;
+};
+
+#endif // PERSON_HXX
diff --git a/odb-examples/container/testscript b/odb-examples/container/testscript
new file mode 100644
index 0000000..3068677
--- /dev/null
+++ b/odb-examples/container/testscript
@@ -0,0 +1,13 @@
+# file : container/testscript
+# license : GNU GPL v2; see accompanying LICENSE file
+
+.include ../database-options.testscript
+.include ../$(database).testscript
+
++if! $sqlite
+ $create_schema
+end
+
+: basics
+:
+$* >|