diff options
Diffstat (limited to 'odb-examples/schema/custom')
-rw-r--r-- | odb-examples/schema/custom/README | 61 | ||||
-rw-r--r-- | odb-examples/schema/custom/buildfile | 44 | ||||
-rw-r--r-- | odb-examples/schema/custom/database.hxx | 76 | ||||
-rw-r--r-- | odb-examples/schema/custom/driver.cxx | 238 | ||||
-rw-r--r-- | odb-examples/schema/custom/employee.hxx | 143 | ||||
-rw-r--r-- | odb-examples/schema/custom/testscript | 14 |
6 files changed, 576 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 +: +$* >| |