diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2011-02-25 12:07:33 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2011-02-25 12:07:33 +0200 |
commit | 2ef165437ce47f50110a9b230f302c5cd5fde1d4 (patch) | |
tree | b1b7c14b9b264e8571ec37c3098c8763ef655ca2 /schema/custom | |
parent | 10d9fed7123cdc7fd9287d2cf6fea8c40c2402a0 (diff) |
Add support for examples in subdirectories
Move the schema example to schema/custom.
Diffstat (limited to 'schema/custom')
-rw-r--r-- | schema/custom/README | 56 | ||||
-rw-r--r-- | schema/custom/database.hxx | 46 | ||||
-rw-r--r-- | schema/custom/driver.cxx | 119 | ||||
-rw-r--r-- | schema/custom/employee.hxx | 150 | ||||
-rw-r--r-- | schema/custom/makefile | 116 |
5 files changed, 487 insertions, 0 deletions
diff --git a/schema/custom/README b/schema/custom/README new file mode 100644 index 0000000..373169f --- /dev/null +++ b/schema/custom/README @@ -0,0 +1,56 @@ +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 uses the shared_ptr smart pointer from TR1 and requires a C++ +compiler with TR1 support or an external TR1 implementation, such as the +one provided by Boost. + +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 -d <database> --generate-query --default-pointer std::tr1::shared_ptr \ + employee.hxx + + Where <database> stands for the database system we are using, for example, + 'mysql'. + + The --default-pointer option is used to make TR1 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 run the driver, using MySQL 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/schema/custom/database.hxx b/schema/custom/database.hxx new file mode 100644 index 0000000..88536ab --- /dev/null +++ b/schema/custom/database.hxx @@ -0,0 +1,46 @@ +// file : schema/custom/database.hxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// 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::auto_ptr +#include <cstdlib> // std::exit +#include <iostream> + +#include <odb/database.hxx> + +#if defined(DATABASE_MYSQL) +# include <odb/mysql/database.hxx> +#endif + +inline std::auto_ptr<odb::database> +create_database (int& argc, char* argv[]) +{ + using namespace std; + using namespace odb::core; + + if (argc > 1 && argv[1] == string ("--help")) + { + cerr << "Usage: " << argv[0] << " [options]" << endl + << "Options:" << endl; + +#if defined(DATABASE_MYSQL) + odb::mysql::database::print_usage (cerr); +#endif + + exit (0); + } + +#if defined(DATABASE_MYSQL) + return auto_ptr<database> (new odb::mysql::database (argc, argv)); +#endif +} + +#endif // DATABASE_HXX diff --git a/schema/custom/driver.cxx b/schema/custom/driver.cxx new file mode 100644 index 0000000..3e49c72 --- /dev/null +++ b/schema/custom/driver.cxx @@ -0,0 +1,119 @@ +// file : schema/custom/driver.cxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : not copyrighted - public domain + +#include <memory> // std::auto_ptr +#include <iostream> + +#include <odb/database.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 + { + auto_ptr<database> db (create_database (argc, argv)); + + // Create the database schema. + // + { + transaction t (db->begin ()); + + // Try to drop the tables if they exist and ignore the error + // if they don't. + // + try + { + db->execute ("DROP TABLE Employer"); + db->execute ("DROP TABLE Employee"); + db->execute ("DROP TABLE EmployeeDegree"); + } + catch (const odb::exception&) + { + } + + db->execute ( + "CREATE TABLE Employer (" + "name VARCHAR (255) NOT NULL PRIMARY KEY)"); + + db->execute ( + "CREATE TABLE Employee (" + "ssn INTEGER UNSIGNED 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 UNSIGNED NOT NULL REFERENCES Employee (ssn)," + "degree VARCHAR (255) NOT NULL)"); + + t.commit (); + } + + // 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/schema/custom/employee.hxx b/schema/custom/employee.hxx new file mode 100644 index 0000000..742ad42 --- /dev/null +++ b/schema/custom/employee.hxx @@ -0,0 +1,150 @@ +// file : schema/custom/employee.hxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : not copyrighted - public domain + +#ifndef EMPLOYEE_HXX +#define EMPLOYEE_HXX + +#include <vector> +#include <string> + +#include <odb/core.hxx> + +// Include TR1 <memory> header in a compiler-specific fashion. Fall back +// on the Boost implementation if the compiler does not support TR1. +// +#include <odb/tr1/memory.hxx> + +using std::tr1::shared_ptr; + +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) NOT NULL") column("first_name") + std::string first_; + + #pragma db type("VARCHAR(255) NOT NULL") 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) NOT NULL") 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, + 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. + // + shared_ptr<employer_type> + employer () const + { + return employer_; + } + + void + employer (shared_ptr<employer_type> employer) + { + employer_ = employer; + } + +private: + friend class odb::access; + + employee (): name_ ("", "") {} + + #pragma db id type("INTEGER UNSIGNED NOT NULL") 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) NOT NULL") value_column("degree") + degrees_type degrees_; + + #pragma db not_null column("employer") + shared_ptr<employer_type> employer_; +}; + +#endif // EMPLOYEE_HXX diff --git a/schema/custom/makefile b/schema/custom/makefile new file mode 100644 index 0000000..e03c31c --- /dev/null +++ b/schema/custom/makefile @@ -0,0 +1,116 @@ +# file : schema/custom/makefile +# author : Boris Kolpackov <boris@codesynthesis.com> +# copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +# license : GNU GPL v2; see accompanying LICENSE file + +include $(dir $(lastword $(MAKEFILE_LIST)))../../build/bootstrap.make + +cxx_tun := driver.cxx +odb_hdr := employee.hxx +cxx_obj := $(addprefix $(out_base)/,$(cxx_tun:.cxx=.o) $(odb_hdr:.hxx=-odb.o)) +cxx_od := $(cxx_obj:.o=.o.d) + +driver := $(out_base)/driver +dist := $(out_base)/.dist +test := $(out_base)/.test +clean := $(out_base)/.clean + +# Import. +# +$(call import,\ + $(scf_root)/import/odb/stub.make,\ + odb: odb,odb-rules: odb_rules) + +$(call import,\ + $(scf_root)/import/libodb/stub.make,\ + l: odb.l,cpp-options: odb.l.cpp-options) + +ifdef db_id +$(call import,\ + $(scf_root)/import/libodb-$(db_id)/stub.make,\ + l: odb_db.l,cpp-options: odb_db.l.cpp-options) +endif + +ifeq ($(odb_db.l.cpp-options),) +odb_db.l.cpp-options := $(out_base)/.unbuildable +endif + +# Build. +# +$(driver): $(cxx_obj) $(odb_db.l) $(odb.l) +$(cxx_obj) $(cxx_od): cpp_options := -I$(out_base) +$(cxx_obj) $(cxx_od): $(odb.l.cpp-options) $(odb_db.l.cpp-options) + +ifeq ($(db_id),mysql) +$(cxx_obj) $(cxx_od): cpp_options += -DDATABASE_MYSQL +endif + +genf := $(addprefix $(odb_hdr:.hxx=-odb),.hxx .ixx .cxx) +gen := $(addprefix $(out_base)/,$(genf)) + +$(gen): $(odb) +$(gen): odb := $(odb) +$(gen) $(dist): export odb_options += --database $(db_id) --generate-query \ +--default-pointer std::tr1::shared_ptr +$(gen): cpp_options := -I$(out_base) +$(gen): $(odb.l.cpp-options) + +$(call include-dep,$(cxx_od),$(cxx_obj),$(gen)) + +# Alias for default target. +# +$(out_base)/: $(driver) + +# Dist +# +name := $(subst /,-,$(subst $(src_root)/,,$(src_base))) + +$(dist): db_id := @database@ +$(dist): sources := $(cxx_tun) +$(dist): headers := $(odb_hdr) +$(dist): export name := $(name) +$(dist): export odb_header_stem := $(basename $(odb_hdr)) +$(dist): export extra_dist := README $(call vc9projs,$(name)) \ +$(call vc10projs,$(name)) +$(dist): + $(call dist-data,$(sources) $(headers) README database.hxx) + $(call meta-automake,../../template/Makefile.am) + $(call meta-vc9projs,../../template/template,$(name)) + $(call meta-vc10projs,../../template/template,$(name)) + +# Test. +# +$(test): $(driver) + $(call message,test $<,$< --options-file $(dcf_root)/db.options) + +# Clean. +# +$(clean): \ + $(driver).o.clean \ + $(addsuffix .cxx.clean,$(cxx_obj)) \ + $(addsuffix .cxx.clean,$(cxx_od)) \ + $(addprefix $(out_base)/,$(odb_hdr:.hxx=-odb.cxx.hxx.clean)) + +# Generated .gitignore. +# +ifeq ($(out_base),$(src_base)) +$(driver): | $(out_base)/.gitignore + +$(out_base)/.gitignore: files := driver $(genf) +$(clean): $(out_base)/.gitignore.clean + +$(call include,$(bld_root)/git/gitignore.make) +endif + +# How to. +# +$(call include,$(bld_root)/dist.make) +$(call include,$(bld_root)/meta/vc9proj.make) +$(call include,$(bld_root)/meta/vc10proj.make) +$(call include,$(bld_root)/meta/automake.make) + +$(call include,$(odb_rules)) +$(call include,$(bld_root)/cxx/cxx-d.make) +$(call include,$(bld_root)/cxx/cxx-o.make) +$(call include,$(bld_root)/cxx/o-e.make) + |