diff options
Diffstat (limited to 'odb-examples/composite')
-rw-r--r-- | odb-examples/composite/README | 69 | ||||
-rw-r--r-- | odb-examples/composite/buildfile | 45 | ||||
-rw-r--r-- | odb-examples/composite/database.hxx | 95 | ||||
-rw-r--r-- | odb-examples/composite/driver.cxx | 108 | ||||
-rw-r--r-- | odb-examples/composite/person.hxx | 228 | ||||
-rw-r--r-- | odb-examples/composite/testscript | 13 |
6 files changed, 558 insertions, 0 deletions
diff --git a/odb-examples/composite/README b/odb-examples/composite/README new file mode 100644 index 0000000..39c227e --- /dev/null +++ b/odb-examples/composite/README @@ -0,0 +1,69 @@ +This example shows how to use composite value types as data members in objects +(including as object id members) and other value types, as element types in +containers, and as base types for other composite value types. It also shows +how to use composite value type data members in queries. + +The example consists of the following files: + +person.hxx + Header file defining the 'basic_name', 'name_extras', 'name', and + 'email_address' composite value types. It also defines the 'phone_numbers' + composite value type as an instantiation of the 'std::pair' class template + Finally it defines the 'person' persistent class which uses 'email_address' + as its object id as well as 'name' and 'phone_numbers' in its other data + members. + +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 --generate-query 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 and updates its + nickname and aliases which reside in a composite value type, then re-loads + the object and prints its name to verify that the changes have been made + persistent. Finally, the driver performs a database query which uses a + data member from the composite value type in its criterion. + +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/composite/buildfile b/odb-examples/composite/buildfile new file mode 100644 index 0000000..7d21db6 --- /dev/null +++ b/odb-examples/composite/buildfile @@ -0,0 +1,45 @@ +# file : composite/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 c_ \ + "-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/composite/database.hxx b/odb-examples/composite/database.hxx new file mode 100644 index 0000000..4520c32 --- /dev/null +++ b/odb-examples/composite/database.hxx @@ -0,0 +1,95 @@ +// file : composite/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/composite/driver.cxx b/odb-examples/composite/driver.cxx new file mode 100644 index 0000000..5cac553 --- /dev/null +++ b/odb-examples/composite/driver.cxx @@ -0,0 +1,108 @@ +// file : composite/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)); + + // Create a person object. + // + email_address id; + { + person p ("joe@example.com", + "Joe", + "Dirt", + "Mr", + phone_numbers ("555 5555", "666 6666")); + + transaction t (db->begin ()); + id = db->persist (p); + t.commit (); + } + + // Update the extra name information. + // + { + transaction t (db->begin ()); + + unique_ptr<person> joe (db->load<person> (id)); + name_extras& ne (joe->name ().extras ()); + ne.nickname ("Squeaky"); + ne.aliases ().push_back (basic_name ("Anthony", "Clean")); + + db->update (*joe); + + t.commit (); + } + + // Print the name and phone numbers. + // + { + transaction t (db->begin ()); + unique_ptr<person> joe (db->load<person> (id)); + t.commit (); + + name& n (joe->name ()); + + cout << n.title () << " " << n.first () << " " << n.last () << " " + << '<' << joe->email ().address () << '>' << endl; + + name_extras& ne (n.extras ()); + + if (!ne.nickname ().empty ()) + cout << " nickname: " << ne.nickname () << endl; + + for (basic_names::iterator i (ne.aliases ().begin ()); + i != ne.aliases ().end (); + ++i) + { + cout << " alias: " << i->first () << " " << i->last () << endl; + } + + cout << " phone 1: " << joe->phone ().first << endl; + cout << " phone 2: " << joe->phone ().second << endl; + } + + // Query the database for a person object. + // + { + typedef odb::query<person> query; + + transaction t (db->begin ()); + + unique_ptr<person> p ( + db->query_one<person> ( + query::name.extras.nickname == "Squeaky")); + + if (p.get () != 0) + { + name& n (p->name ()); + cout << n.title () << " " << n.first () << " " << n.last () << endl; + } + + t.commit (); + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/odb-examples/composite/person.hxx b/odb-examples/composite/person.hxx new file mode 100644 index 0000000..f0626e5 --- /dev/null +++ b/odb-examples/composite/person.hxx @@ -0,0 +1,228 @@ +// file : composite/person.hxx +// copyright : not copyrighted - public domain + +#ifndef PERSON_HXX +#define PERSON_HXX + +#include <vector> +#include <string> + +#include <odb/core.hxx> + +#pragma db value +class basic_name +{ +public: + basic_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; + + basic_name () {} // Needed for storing basic_name in containers. + + std::string first_; + std::string last_; +}; + +typedef std::vector<basic_name> basic_names; + + +#pragma db value +class name_extras +{ +public: + // Nickname. + // + const std::string& + nickname () const + { + return nickname_; + } + + void + nickname (const std::string& nickname) + { + nickname_ = nickname; + } + + // Aliases. + // + const basic_names& + aliases () const + { + return aliases_; + } + + basic_names& + aliases () + { + return aliases_; + } + +private: + friend class odb::access; + + std::string nickname_; + basic_names aliases_; +}; + + +#pragma db value +class name: public basic_name +{ +public: + name (const std::string& first, + const std::string& last, + const std::string& title) + : basic_name (first, last), title_ (title) + { + } + + // Title. + // + const std::string& + title () const + { + return title_; + } + + // Extras. + // + const name_extras& + extras () const + { + return extras_; + } + + name_extras& + extras () + { + return extras_; + } + +private: + friend class odb::access; + + std::string title_; + name_extras extras_; +}; + +// We can also define a composite value type as a class template +// instantiation. Here we use std::pair to store person's phone +// numbers, in the order of preference. +// +typedef std::pair<std::string, std::string> phone_numbers; +#pragma db value(phone_numbers) + +// We can also use a composite value type as an object id. +// +#pragma db value +class email_address +{ +public: + email_address () {} + email_address (const std::string& address) + { + std::string::size_type p (address.find ('@')); + recipient_.assign (address, 0, p); + domain_.assign (address, p + 1, std::string::npos); + } + + const std::string& + recipient () const + { + return recipient_; + } + + const std::string& + domain () const + { + return domain_; + } + + std::string + address () const + { + return recipient_ + '@' + domain_; + } + +private: + friend class odb::access; + + std::string recipient_; + std::string domain_; +}; + +#pragma db object +class person +{ +public: + person (const std::string& email, + const std::string& first, + const std::string& last, + const std::string& title, + const phone_numbers& phone) + : email_ (email), name_ (first, last, title), phone_ (phone) + { + } + + // Email address. + // + const email_address& + email () const + { + return email_; + } + + // Name. + // + typedef ::name name_type; + + const name_type& + name () const + { + return name_; + } + + name_type& + name () + { + return name_; + } + + // Phone. + // + const phone_numbers& + phone () const + { + return phone_; + } + +private: + friend class odb::access; + + person (): name_ ("", "", "") {} + + #pragma db id + email_address email_; + + name_type name_; + phone_numbers phone_; +}; + +#endif // PERSON_HXX diff --git a/odb-examples/composite/testscript b/odb-examples/composite/testscript new file mode 100644 index 0000000..d66ce0f --- /dev/null +++ b/odb-examples/composite/testscript @@ -0,0 +1,13 @@ +# file : composite/testscript +# license : GNU GPL v2; see accompanying LICENSE file + +.include ../database-options.testscript +.include ../$(database).testscript + ++if! $sqlite + $create_schema +end + +: basics +: +$* >| |