summaryrefslogtreecommitdiff
path: root/odb-examples/schema
diff options
context:
space:
mode:
Diffstat (limited to 'odb-examples/schema')
-rw-r--r--odb-examples/schema/custom/README61
-rw-r--r--odb-examples/schema/custom/buildfile44
-rw-r--r--odb-examples/schema/custom/database.hxx76
-rw-r--r--odb-examples/schema/custom/driver.cxx238
-rw-r--r--odb-examples/schema/custom/employee.hxx143
-rw-r--r--odb-examples/schema/custom/testscript14
-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
12 files changed, 929 insertions, 0 deletions
diff --git a/odb-examples/schema/custom/README b/odb-examples/schema/custom/README
new file mode 100644
index 0000000..48699d0
--- /dev/null
+++ b/odb-examples/schema/custom/README
@@ -0,0 +1,61 @@
+This example shows how to map persistent C++ classes to a custom database
+schema. In particular, it shows how to map all the commonly-used constructs,
+including containers, object relationships, and composite value types.
+
+The example consists of the following files:
+
+employee.hxx
+ Header file defining the 'employee' and 'employer' persistent classes
+ as well as the 'name' composite value type. ODB pragmas are used to
+ assign custom database tables to persistent classes as well as custom
+ database types and columns to data members.
+
+employee-odb.hxx
+employee-odb.ixx
+employee-odb.cxx
+ These files contain the database support code for the employee.hxx header
+ and are generated by the ODB compiler from employee.hxx using the following
+ command line:
+
+ odb --std c++11 -d <database> --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 programmatically creates the database schema by executing
+ a series of SQL statements. After that the driver creates a number of
+ 'employee' and 'employer' objects, sets the relationships between them,
+ and persists them in the database. Finally, the driver 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 employee-odb.cxx
+c++ -DDATABASE_PGSQL -c driver.cxx
+c++ -o driver driver.o employee-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/custom/buildfile b/odb-examples/schema/custom/buildfile
new file mode 100644
index 0000000..349da2c
--- /dev/null
+++ b/odb-examples/schema/custom/buildfile
@@ -0,0 +1,44 @@
+# file : schema/custom/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
+
+ $odb --std c++11 \
+ --database $database \
+ --generate-query \
+ --generate-session \
+ --default-pointer std::shared_ptr \
+ --output-dir $out_base \
+ "-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/custom/database.hxx b/odb-examples/schema/custom/database.hxx
new file mode 100644
index 0000000..db6543c
--- /dev/null
+++ b/odb-examples/schema/custom/database.hxx
@@ -0,0 +1,76 @@
+// file : schema/custom/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/custom/driver.cxx b/odb-examples/schema/custom/driver.cxx
new file mode 100644
index 0000000..f9d40e2
--- /dev/null
+++ b/odb-examples/schema/custom/driver.cxx
@@ -0,0 +1,238 @@
+// file : schema/custom/driver.cxx
+// copyright : not copyrighted - public domain
+
+#include <memory> // std::unique_ptr
+#include <iostream>
+
+#include <odb/database.hxx>
+#include <odb/connection.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;
+
+int
+main (int argc, char* argv[])
+{
+ try
+ {
+ unique_ptr<database> db (create_database (argc, argv));
+
+ // Create the database schema.
+ //
+#if defined(DATABASE_MYSQL) || \
+ defined(DATABASE_SQLITE) || \
+ defined(DATABASE_MSSQL)
+ {
+
+ // Due to bugs in SQLite foreign key support for DDL statements,
+ // we need to temporarily disable foreign keys.
+ //
+ connection_ptr c (db->connection ());
+
+#ifdef DATABASE_SQLITE
+ c->execute ("PRAGMA foreign_keys=OFF");
+#endif
+
+ transaction t (c->begin ());
+
+ // Try to drop the tables if they exist and ignore the error
+ // if they don't.
+ //
+ try
+ {
+ db->execute ("DROP TABLE EmployeeDegree");
+ db->execute ("DROP TABLE Employee");
+ db->execute ("DROP TABLE Employer");
+ }
+ catch (const odb::exception&)
+ {
+ }
+
+ db->execute (
+ "CREATE TABLE Employer ("
+ "name VARCHAR (255) NOT NULL PRIMARY KEY)");
+
+ db->execute (
+ "CREATE TABLE Employee ("
+ "ssn INTEGER NOT NULL PRIMARY KEY,"
+ "first_name VARCHAR (255) NOT NULL,"
+ "last_name VARCHAR (255) NOT NULL,"
+ "employer VARCHAR (255) NOT NULL REFERENCES Employer (name))");
+
+ db->execute (
+ "CREATE TABLE EmployeeDegree ("
+ "ssn INTEGER NOT NULL REFERENCES Employee (ssn),"
+ "degree VARCHAR (255) NOT NULL)");
+
+ t.commit ();
+
+#ifdef DATABASE_SQLITE
+ c->execute ("PRAGMA foreign_keys=ON");
+#endif
+ }
+#elif defined(DATABASE_PGSQL)
+ {
+ // PostgreSQL-specific SQL.
+ //
+ transaction t (db->begin ());
+
+ db->execute ("DROP TABLE IF EXISTS \"Employer\" CASCADE");
+ db->execute ("DROP TABLE IF EXISTS \"Employee\" CASCADE");
+ db->execute ("DROP TABLE IF EXISTS \"EmployeeDegree\" CASCADE");
+
+ db->execute (
+ "CREATE TABLE \"Employer\" ("
+ "name VARCHAR (255) NOT NULL PRIMARY KEY)");
+
+ db->execute (
+ "CREATE TABLE \"Employee\" ("
+ "ssn INTEGER NOT NULL PRIMARY KEY,"
+ "first_name VARCHAR (255) NOT NULL,"
+ "last_name VARCHAR (255) NOT NULL,"
+ "employer VARCHAR (255) NOT NULL)");
+
+ db->execute (
+ "CREATE TABLE \"EmployeeDegree\" ("
+ "ssn INTEGER NOT NULL,"
+ "degree VARCHAR (255) NOT NULL)");
+
+ db->execute (
+ "ALTER TABLE \"Employee\" "
+ "ADD FOREIGN KEY (employer) "
+ "REFERENCES \"Employer\" "
+ "INITIALLY DEFERRED");
+
+ db->execute (
+ "ALTER TABLE \"EmployeeDegree\" "
+ "ADD FOREIGN KEY (ssn) "
+ "REFERENCES \"Employee\" "
+ "INITIALLY DEFERRED");
+
+ t.commit ();
+ }
+#elif defined(DATABASE_ORACLE)
+ {
+ // Oracle-specific PL/SQL.
+ //
+ transaction t (db->begin ());
+
+ db->execute ("BEGIN "
+ " EXECUTE IMMEDIATE "
+ " 'DROP TABLE \"Employer\" CASCADE CONSTRAINTS';"
+ " EXCEPTION "
+ " WHEN OTHERS THEN "
+ " IF SQLCODE != -942 THEN RAISE; END IF;"
+ "END;");
+
+ db->execute ("BEGIN "
+ " EXECUTE IMMEDIATE "
+ " 'DROP TABLE \"Employee\" CASCADE CONSTRAINTS';"
+ " EXCEPTION "
+ " WHEN OTHERS THEN "
+ " IF SQLCODE != -942 THEN RAISE; END IF;"
+ "END;");
+
+ db->execute ("BEGIN "
+ " EXECUTE IMMEDIATE 'DROP TABLE \"EmployeeDegree\"';"
+ " EXCEPTION "
+ " WHEN OTHERS THEN "
+ " IF SQLCODE != -942 THEN RAISE; END IF;"
+ "END;");
+
+ db->execute (
+ "CREATE TABLE \"Employer\" ("
+ "\"name\" VARCHAR (255) PRIMARY KEY)");
+
+ db->execute (
+ "CREATE TABLE \"Employee\" ("
+ "\"ssn\" NUMBER(10) PRIMARY KEY,"
+ "\"first_name\" VARCHAR (255) NOT NULL,"
+ "\"last_name\" VARCHAR (255) NOT NULL,"
+ "\"employer\" VARCHAR (255) NOT NULL)");
+
+ db->execute (
+ "CREATE TABLE \"EmployeeDegree\" ("
+ "\"ssn\" NUMBER(10) NOT NULL,"
+ "\"degree\" VARCHAR (255) NOT NULL)");
+
+ db->execute (
+ "ALTER TABLE \"Employee\" "
+ "ADD FOREIGN KEY (\"employer\") "
+ "REFERENCES \"Employer\" "
+ "INITIALLY DEFERRED");
+
+ db->execute (
+ "ALTER TABLE \"EmployeeDegree\" "
+ "ADD FOREIGN KEY (\"ssn\") "
+ "REFERENCES \"Employee\" "
+ "INITIALLY DEFERRED");
+
+ t.commit ();
+ }
+#else
+# error unknown database
+#endif
+
+ // Create a few persistent objects.
+ //
+ {
+ shared_ptr<employer> st (new employer ("Simple Tech Ltd"));
+
+ shared_ptr<employee> john (new employee (1, "John", "Doe", st));
+ shared_ptr<employee> jane (new employee (2, "Jane", "Doe", st));
+
+ john->degrees ().push_back ("BA");
+ john->degrees ().push_back ("MSc");
+ jane->degrees ().push_back ("Ph.D.");
+
+ transaction t (db->begin ());
+
+ db->persist (st);
+ db->persist (john);
+ db->persist (jane);
+
+ t.commit ();
+ }
+
+ // Load employees with "Doe" as the last name and print what we've got.
+ //
+ {
+ typedef odb::query<employee> query;
+ typedef odb::result<employee> result;
+
+ session s;
+ transaction t (db->begin ());
+
+ result r (db->query<employee> (query::name.last == "Doe"));
+
+ for (result::iterator i (r.begin ()); i != r.end (); ++i)
+ {
+ cout << i->name ().first () << " " << i->name ().last () << endl
+ << " employer: " << i->employer ()->name () << endl;
+
+ for (degrees::iterator j (i->degrees ().begin ());
+ j != i->degrees ().end ();
+ ++j)
+ {
+ cout << " degree: " << *j << endl;
+ }
+
+ cout << endl;
+ }
+
+ t.commit ();
+ }
+ }
+ catch (const odb::exception& e)
+ {
+ cerr << e.what () << endl;
+ return 1;
+ }
+}
diff --git a/odb-examples/schema/custom/employee.hxx b/odb-examples/schema/custom/employee.hxx
new file mode 100644
index 0000000..200017d
--- /dev/null
+++ b/odb-examples/schema/custom/employee.hxx
@@ -0,0 +1,143 @@
+// file : schema/custom/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>
+
+typedef std::vector<std::string> degrees;
+
+#pragma db value
+class name
+{
+public:
+ name (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_;
+ }
+
+private:
+ friend class odb::access;
+
+ #pragma db type("VARCHAR(255)") column("first_name")
+ std::string first_;
+
+ #pragma db type("VARCHAR(255)") column("last_name")
+ std::string last_;
+};
+
+#pragma db object table("Employer")
+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 type("VARCHAR(255)") column("name")
+ std::string name_;
+};
+
+#pragma db object table("Employee")
+class employee
+{
+public:
+ typedef ::employer employer_type;
+
+ employee (unsigned long id,
+ const std::string& first,
+ const std::string& last,
+ std::shared_ptr<employer_type> employer)
+ : id_ (id), name_ (first, last), employer_ (employer)
+ {
+ }
+
+ // Name.
+ //
+ typedef ::name name_type;
+
+ const name_type&
+ name () const
+ {
+ return name_;
+ }
+
+ // Degrees.
+ //
+ typedef ::degrees degrees_type;
+
+ const degrees_type&
+ degrees () const
+ {
+ return degrees_;
+ }
+
+ degrees_type&
+ degrees ()
+ {
+ return degrees_;
+ }
+
+ // Employer.
+ //
+ std::shared_ptr<employer_type>
+ employer () const
+ {
+ return employer_;
+ }
+
+ void
+ employer (std::shared_ptr<employer_type> employer)
+ {
+ employer_ = employer;
+ }
+
+private:
+ friend class odb::access;
+
+ employee (): name_ ("", "") {}
+
+ #pragma db id type("INTEGER") column("ssn")
+ unsigned long id_;
+
+ #pragma db column("") // No column prefix.
+ name_type name_;
+
+ #pragma db unordered table("EmployeeDegree") id_column("ssn") \
+ value_type("VARCHAR(255)") value_column("degree")
+ degrees_type degrees_;
+
+ #pragma db not_null column("employer")
+ std::shared_ptr<employer_type> employer_;
+};
+
+#endif // EMPLOYEE_HXX
diff --git a/odb-examples/schema/custom/testscript b/odb-examples/schema/custom/testscript
new file mode 100644
index 0000000..a482025
--- /dev/null
+++ b/odb-examples/schema/custom/testscript
@@ -0,0 +1,14 @@
+# file : schema/custom/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
+:
+$* >|
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
+:
+$* >|