summaryrefslogtreecommitdiff
path: root/odb-examples/access
diff options
context:
space:
mode:
Diffstat (limited to 'odb-examples/access')
-rw-r--r--odb-examples/access/README63
-rw-r--r--odb-examples/access/buildfile45
-rw-r--r--odb-examples/access/database.hxx95
-rw-r--r--odb-examples/access/driver.cxx56
-rw-r--r--odb-examples/access/person.hxx125
-rw-r--r--odb-examples/access/testscript13
6 files changed, 397 insertions, 0 deletions
diff --git a/odb-examples/access/README b/odb-examples/access/README
new file mode 100644
index 0000000..aeb0830
--- /dev/null
+++ b/odb-examples/access/README
@@ -0,0 +1,63 @@
+This example shows various approaches used by ODB to access data members
+that cannot be accessed directly. Approaches that are illustrated by this
+example include the automatic discovery of suitable accessor/modifier
+functions, explicit specification of the accessor/modifier functions,
+explicit specification of more complex accessor/modifier expressions, and
+use of virtual data members.
+
+The example consists of the following files:
+
+person.hxx
+ Header file implementing the 'person' persistent class.
+
+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-query --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 class and its 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. Then it executes a number of database transactions on the 'person'
+ objects.
+
+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/access/buildfile b/odb-examples/access/buildfile
new file mode 100644
index 0000000..5001f97
--- /dev/null
+++ b/odb-examples/access/buildfile
@@ -0,0 +1,45 @@
+# file : access/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 \
+ --generate-query \
+ --output-dir $out_base \
+ --table-prefix access_ \
+ "-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/access/database.hxx b/odb-examples/access/database.hxx
new file mode 100644
index 0000000..2180735
--- /dev/null
+++ b/odb-examples/access/database.hxx
@@ -0,0 +1,95 @@
+// file : access/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/access/driver.cxx b/odb-examples/access/driver.cxx
new file mode 100644
index 0000000..b477d6f
--- /dev/null
+++ b/odb-examples/access/driver.cxx
@@ -0,0 +1,56 @@
+// file : access/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;
+
+int
+main (int argc, char* argv[])
+{
+ try
+ {
+ unique_ptr<database> db (create_database (argc, argv));
+
+ {
+ person john ("john@doe.com", "John", "X", "Doe", 31);
+ person jane ("jane@doe.com", "Jane", "Y", "Doe", 29);
+
+ transaction t (db->begin ());
+ db->persist (john);
+ db->persist (jane);
+ t.commit ();
+ }
+
+ {
+ typedef odb::result<person> result;
+
+ transaction t (db->begin ());
+ result r (db->query<person> ());
+
+ for (result::iterator i (r.begin ()); i != r.end (); ++i)
+ cout << i->getFirst () << ' '
+ << i->g_middle () << ' '
+ << i->last () << ' '
+ << i->email () << ' '
+ << i->age () << endl;
+
+ t.commit ();
+ }
+ }
+ catch (const odb::exception& e)
+ {
+ cerr << e.what () << endl;
+ return 1;
+ }
+}
diff --git a/odb-examples/access/person.hxx b/odb-examples/access/person.hxx
new file mode 100644
index 0000000..a0e37ea
--- /dev/null
+++ b/odb-examples/access/person.hxx
@@ -0,0 +1,125 @@
+// file : access/person.hxx
+// copyright : not copyrighted - public domain
+
+#ifndef PERSON_HXX
+#define PERSON_HXX
+
+#include <string>
+
+#include <odb/core.hxx>
+
+#pragma db object
+class person
+{
+public:
+ person () {}
+ person (const std::string& email,
+ const std::string& first,
+ const std::string& middle,
+ const std::string& last,
+ unsigned short age)
+ : email_ (email), first_ (first), middle_ (middle), last_ (last)
+ {
+ data_.age = age;
+ }
+
+ // Standard accessor/modifier names. Auto-discovered by ODB.
+ //
+ const std::string&
+ email () const
+ {
+ return email_;
+ }
+
+ void
+ email (const std::string& email)
+ {
+ email_ = email;
+ }
+
+ // Get/set-style accessor/modifier names. Also auto-discovered
+ // by ODB.
+ //
+ const std::string&
+ getFirst () const
+ {
+ return first_;
+ }
+
+ std::string&
+ setFirst ()
+ {
+ return first_;
+ }
+
+ // Unconventional accessor/modifier names which ODB is unable to
+ // auto-discover (but see also the --{accessor,modifier}-regex
+ // options). We have to specify these names explicitly (see below).
+ //
+ const std::string&
+ g_middle () const
+ {
+ return middle_;
+ }
+
+ void
+ s_middle (const std::string& middle)
+ {
+ middle_ = middle;
+ }
+
+ // Accessor/modifier types do not match data member type. Again,
+ // we have to specify accessor/modifier expressions that perform
+ // the necessary conversions (see below).
+ //
+ const char*
+ last () const
+ {
+ return last_.c_str ();
+ }
+
+ void
+ last (const char* last)
+ {
+ last_ = last;
+ }
+
+ // Accessor/modifier for a data member that is wrapped in an
+ // anonymous struct. We use a virtual data member to handle
+ // this case.
+ //
+ unsigned short
+ age () const
+ {
+ return data_.age;
+ }
+
+ void
+ age (unsigned short age)
+ {
+ data_.age = age;
+ }
+
+private:
+ #pragma db id
+ std::string email_; // Accessor and modifier are auto-discovered.
+
+ std::string first_; // Accessor and modifier are auto-discovered.
+
+ #pragma db get(g_middle) set(s_middle)
+ std::string middle_;
+
+ #pragma db get(std::string (this.last ())) set(last ((?).c_str ()))
+ std::string last_;
+
+ #pragma db transient
+ struct
+ {
+ unsigned short age;
+ } data_;
+
+ #pragma db member(age) virtual(unsigned short) // Accessor and modifier
+ // are auto-discovered.
+};
+
+#endif // PERSON_HXX
diff --git a/odb-examples/access/testscript b/odb-examples/access/testscript
new file mode 100644
index 0000000..33a0392
--- /dev/null
+++ b/odb-examples/access/testscript
@@ -0,0 +1,13 @@
+# file : access/testscript
+# license : GNU GPL v2; see accompanying LICENSE file
+
+.include ../database-options.testscript
+.include ../$(database).testscript
+
++if! $sqlite
+ $create_schema
+end
+
+: basics
+:
+$* >|