summaryrefslogtreecommitdiff
path: root/odb-examples/schema/embedded
diff options
context:
space:
mode:
Diffstat (limited to 'odb-examples/schema/embedded')
-rw-r--r--odb-examples/schema/embedded/README63
-rw-r--r--odb-examples/schema/embedded/buildfile45
-rw-r--r--odb-examples/schema/embedded/database.hxx76
-rw-r--r--odb-examples/schema/embedded/driver.cxx96
-rw-r--r--odb-examples/schema/embedded/person.hxx59
-rw-r--r--odb-examples/schema/embedded/testscript14
6 files changed, 353 insertions, 0 deletions
diff --git a/odb-examples/schema/embedded/README b/odb-examples/schema/embedded/README
new file mode 100644
index 0000000..50c3f7e
--- /dev/null
+++ b/odb-examples/schema/embedded/README
@@ -0,0 +1,63 @@
+This example shows how to generate and use a database schema that is embedded
+into the application rather than stored as a separate SQL file.
+
+The example consists of the following files:
+
+person.hxx
+ Header file defining the 'person' persistent class.
+
+person-odb.hxx
+person-odb.ixx
+person-odb.cxx
+ These files contain the database support code as well as the embedded
+ database schema for the person.hxx header. They are generated by the ODB
+ compiler from person.hxx using the following command line:
+
+ odb --std c++11 -d <database> --generate-schema --schema-format embedded \
+ --generate-query person.hxx
+
+ Where <database> stands for the database system we are using, for example,
+ 'pgsql'.
+
+ The --generate-schema option requests the generation of the database schema.
+ The --schema-format option is used to instruct the ODB compiler to embed the
+ schema into the generated C++ files. It is also possible to generate the
+ schema creation code into a separate source file by passing the 'separate'
+ value instead of 'embedded' to the --schema-format option. This is primarily
+ useful if you want to place the schema creation functionality into a
+ separate program or library.
+
+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 uses the ODB schema catalog to create the database
+ schema. During this step the generated code issues a number of SQL
+ statements that drop and create necessary database tables, etc.
+
+ After the database schema is ready, the driver persists a number of 'person'
+ objects, performs a database query, and prints the information about the
+ returned 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 driver, using PostgreSQL as an example, we can execute the
+following command:
+
+./driver --user odb_test --database odb_test
+
+Here we use 'odb_test' as the database login and also 'odb_test' as the
+database name.
diff --git a/odb-examples/schema/embedded/buildfile b/odb-examples/schema/embedded/buildfile
new file mode 100644
index 0000000..79a45a0
--- /dev/null
+++ b/odb-examples/schema/embedded/buildfile
@@ -0,0 +1,45 @@
+# file : schema/embedded/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
+
+ $odb --std c++11 \
+ --database $database \
+ --generate-schema \
+ --schema-format embedded \
+ --generate-query \
+ --output-dir $out_base \
+ --table-prefix schema_embedded_ \
+ "-I$src_base" $pops \
+ $path($<[0])
+}}
+
+cxx.poptions =+ "-I$out_base" "-I$src_base" -DDATABASE_$ucase($database)
+
+# Testscript's run-time prerequisites.
+#
+# Note that while we don't create the schema using the database client
+# utility, we still add this prerequisite since database-options.testscript
+# requires that.
+#
+# @@ 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
diff --git a/odb-examples/schema/embedded/database.hxx b/odb-examples/schema/embedded/database.hxx
new file mode 100644
index 0000000..922db8b
--- /dev/null
+++ b/odb-examples/schema/embedded/database.hxx
@@ -0,0 +1,76 @@
+// file : schema/embedded/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/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));
+#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/schema/embedded/driver.cxx b/odb-examples/schema/embedded/driver.cxx
new file mode 100644
index 0000000..3aadef0
--- /dev/null
+++ b/odb-examples/schema/embedded/driver.cxx
@@ -0,0 +1,96 @@
+// file : schema/embedded/driver.cxx
+// copyright : not copyrighted - public domain
+
+#include <memory> // std::unique_ptr
+#include <iostream>
+
+#include <odb/database.hxx>
+#include <odb/transaction.hxx>
+#include <odb/schema-catalog.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
+ {
+ typedef odb::query<person> query;
+ typedef odb::result<person> result;
+
+ unique_ptr<database> db (create_database (argc, argv));
+
+ // Create the database schema.
+ //
+ {
+ transaction t (db->begin ());
+ schema_catalog::create_schema (*db);
+ t.commit ();
+ }
+
+ // The following alternative version only creates the schema if it
+ // hasn't already been created. To detect the existence of the schema
+ // this version tries to query the database for a person object. If
+ // the corresponding table does not exist, then an exceptions will be
+ // thrown in which case we proceed to creating the schema.
+ //
+ /*
+ {
+ transaction t (db->begin ());
+
+ try
+ {
+ db->query<person> (false);
+ }
+ catch (const odb::exception& e)
+ {
+ schema_catalog::create_schema (*db);
+ }
+
+ t.commit ();
+ }
+ */
+
+ // Create a few persistent person objects.
+ //
+ {
+ person john ("John", "Doe", 33);
+ person jane ("Jane", "Doe", 32);
+ person joe ("Joe", "Dirt", 30);
+
+ transaction t (db->begin ());
+
+ db->persist (john);
+ db->persist (jane);
+ db->persist (joe);
+
+ t.commit ();
+ }
+
+ // Print those over 30.
+ //
+ {
+ transaction t (db->begin ());
+
+ result r (db->query<person> (query::age > 30));
+
+ 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/schema/embedded/person.hxx b/odb-examples/schema/embedded/person.hxx
new file mode 100644
index 0000000..e73ddcf
--- /dev/null
+++ b/odb-examples/schema/embedded/person.hxx
@@ -0,0 +1,59 @@
+// file : schema/embedded/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 (const std::string& first,
+ const std::string& last,
+ unsigned short age)
+ : first_ (first), last_ (last), age_ (age)
+ {
+ }
+
+ const std::string&
+ first () const
+ {
+ return first_;
+ }
+
+ const std::string&
+ last () const
+ {
+ return last_;
+ }
+
+ unsigned short
+ age () const
+ {
+ return age_;
+ }
+
+ void
+ age (unsigned short age)
+ {
+ age_ = age;
+ }
+
+private:
+ friend class odb::access;
+
+ person () {}
+
+ #pragma db id auto
+ unsigned long id_;
+
+ std::string first_;
+ std::string last_;
+ unsigned short age_;
+};
+
+#endif // PERSON_HXX
diff --git a/odb-examples/schema/embedded/testscript b/odb-examples/schema/embedded/testscript
new file mode 100644
index 0000000..1310f1b
--- /dev/null
+++ b/odb-examples/schema/embedded/testscript
@@ -0,0 +1,14 @@
+# file : schema/embedded/testscript
+# license : GNU GPL v2; see accompanying LICENSE file
+
+.include ../../database-options.testscript
+
+test.arguments += $($(database)_options)
+
++if $sqlite
+ test.cleanups += &odb-test.db
+end
+
+: basics
+:
+$* >|