From 17d96bd351d0db6760706f2620b25353f1883ddd Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Wed, 19 Jan 2011 12:04:47 +0200 Subject: Add schema example It shows how to map persistent classes to a custom database schema. --- schema/README | 56 ++++++++++++++++++++ schema/database.hxx | 46 ++++++++++++++++ schema/driver.cxx | 119 +++++++++++++++++++++++++++++++++++++++++ schema/employee.hxx | 150 ++++++++++++++++++++++++++++++++++++++++++++++++++++ schema/makefile | 117 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 488 insertions(+) create mode 100644 schema/README create mode 100644 schema/database.hxx create mode 100644 schema/driver.cxx create mode 100644 schema/employee.hxx create mode 100644 schema/makefile (limited to 'schema') diff --git a/schema/README b/schema/README new file mode 100644 index 0000000..373169f --- /dev/null +++ b/schema/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 --generate-query --default-pointer std::tr1::shared_ptr \ + employee.hxx + + Where 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/database.hxx b/schema/database.hxx new file mode 100644 index 0000000..c163925 --- /dev/null +++ b/schema/database.hxx @@ -0,0 +1,46 @@ +// file : schema/database.hxx +// author : Boris Kolpackov +// copyright : not copyrighted - public domain + +// +// Create concrete database instance based on the DATABASE_* macros. +// + +#ifndef DATABASE_HXX +#define DATABASE_HXX + +#include +#include // std::auto_ptr +#include // std::exit +#include + +#include + +#if defined(DATABASE_MYSQL) +# include +#endif + +inline std::auto_ptr +create_database (int& argc, char* argv[]) +{ + using namespace std; + using namespace odb; + + if (argc > 1 && argv[1] == string ("--help")) + { + cerr << "Usage: " << argv[0] << " [options]" << endl + << "Options:" << endl; + +#if defined(DATABASE_MYSQL) + mysql::database::print_usage (cerr); +#endif + + exit (0); + } + +#if defined(DATABASE_MYSQL) + return auto_ptr (new mysql::database (argc, argv)); +#endif +} + +#endif // DATABASE_HXX diff --git a/schema/driver.cxx b/schema/driver.cxx new file mode 100644 index 0000000..5ba5113 --- /dev/null +++ b/schema/driver.cxx @@ -0,0 +1,119 @@ +// file : schema/driver.cxx +// author : Boris Kolpackov +// copyright : not copyrighted - public domain + +#include // std::auto_ptr +#include + +#include +#include +#include + +#include "database.hxx" // create_database + +#include "employee.hxx" +#include "employee-odb.hxx" + +using namespace std; +using namespace odb; + +int +main (int argc, char* argv[]) +{ + try + { + auto_ptr 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 st (new employer ("Simple Tech Ltd")); + + shared_ptr john (new employee (1, "John", "Doe", st)); + shared_ptr 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 query; + typedef odb::result result; + + session s; + transaction t (db->begin ()); + + result r (db->query (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/employee.hxx b/schema/employee.hxx new file mode 100644 index 0000000..25f8d8f --- /dev/null +++ b/schema/employee.hxx @@ -0,0 +1,150 @@ +// file : schema/employee.hxx +// author : Boris Kolpackov +// copyright : not copyrighted - public domain + +#ifndef EMPLOYEE_HXX +#define EMPLOYEE_HXX + +#include +#include + +#include + +// Include TR1 header in a compiler-specific fashion. Fall back +// on the Boost implementation if the compiler does not support TR1. +// +#include + +using std::tr1::shared_ptr; + +typedef std::vector 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) + : 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 () const + { + return employer_; + } + + void + employer (shared_ptr 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_; +}; + +#endif // EMPLOYEE_HXX diff --git a/schema/makefile b/schema/makefile new file mode 100644 index 0000000..ed5114e --- /dev/null +++ b/schema/makefile @@ -0,0 +1,117 @@ +# file : schema/makefile +# author : Boris Kolpackov +# 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 := $(notdir $(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): schema := $(src_base)/$(basename $(odb_hdr)).sql +$(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) + -- cgit v1.1