From ab51fa65f9e8cad4ef5a1db85029dfe6404e9a1f Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 13 Jan 2011 11:31:14 +0200 Subject: Add composite, relationship, and inverse examples All add the TR1 test for the latter two examples. --- composite/README | 56 +++++++++++++++++ composite/database.hxx | 46 ++++++++++++++ composite/driver.cxx | 101 ++++++++++++++++++++++++++++++ composite/makefile | 118 +++++++++++++++++++++++++++++++++++ composite/person.hxx | 165 +++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 486 insertions(+) create mode 100644 composite/README create mode 100644 composite/database.hxx create mode 100644 composite/driver.cxx create mode 100644 composite/makefile create mode 100644 composite/person.hxx (limited to 'composite') diff --git a/composite/README b/composite/README new file mode 100644 index 0000000..fc508bb --- /dev/null +++ b/composite/README @@ -0,0 +1,56 @@ +This example shows how to use composite value types as data members in objects +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', and 'name' composite + value types. It also defines the 'person' persistent class which use the + 'name' value type in one of its 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 -d --generate-schema --generate-query person.hxx + + Where stands for the database system we are using, for example, + 'mysql'. + +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 'person' class and the database support + code for this class. 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 run the example we first need to create the database schema. Using MySQL +as an example, this can be achieved with the following command: + +mysql --user=odb_test --database=odb_test < 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 MySQL as +the database): + +./driver --user odb_test --database odb_test diff --git a/composite/database.hxx b/composite/database.hxx new file mode 100644 index 0000000..c21c9fc --- /dev/null +++ b/composite/database.hxx @@ -0,0 +1,46 @@ +// file : composite/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/composite/driver.cxx b/composite/driver.cxx new file mode 100644 index 0000000..f8feb2f --- /dev/null +++ b/composite/driver.cxx @@ -0,0 +1,101 @@ +// file : composite/driver.cxx +// author : Boris Kolpackov +// copyright : not copyrighted - public domain + +#include // std::auto_ptr +#include + +#include +#include + +#include "database.hxx" // create_database + +#include "person.hxx" +#include "person-odb.hxx" + +using namespace std; +using namespace odb; + +int +main (int argc, char* argv[]) +{ + try + { + auto_ptr db (create_database (argc, argv)); + + // Create a person object. + // + unsigned int id; + { + person p ("Joe", "Dirt", "Mr"); + + transaction t (db->begin ()); + id = db->persist (p); + t.commit (); + } + + // Update the extra name information. + // + { + transaction t (db->begin ()); + + auto_ptr joe (db->load (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 information. + // + { + transaction t (db->begin ()); + auto_ptr joe (db->load (id)); + t.commit (); + + name& n (joe->name ()); + + cout << n.title () << " " << n.first () << " " << n.last () << 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; + } + } + + // Query the database for a person object. + // + { + typedef odb::query query; + typedef odb::result result; + + transaction t (db->begin ()); + + result r (db->query ( + query::name::extras::nickname == "Squeaky")); + + if (!r.empty ()) + { + name& n (r.begin ()->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/composite/makefile b/composite/makefile new file mode 100644 index 0000000..dcaeb82 --- /dev/null +++ b/composite/makefile @@ -0,0 +1,118 @@ +# file : composite/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 := person.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) $(odb_hdr:.hxx=.sql) +gen := $(addprefix $(out_base)/,$(genf)) + +$(gen): $(odb) +$(gen): odb := $(odb) +$(gen) $(dist): export odb_options += --database $(db_id) --generate-schema \ +--generate-query +$(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,sql $$1,$(dcf_root)/db-driver $$1,$(schema)) + $(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) + diff --git a/composite/person.hxx b/composite/person.hxx new file mode 100644 index 0000000..dfc44e6 --- /dev/null +++ b/composite/person.hxx @@ -0,0 +1,165 @@ +// file : composite/person.hxx +// author : Boris Kolpackov +// copyright : not copyrighted - public domain + +#ifndef PERSON_HXX +#define PERSON_HXX + +#include +#include + +#include + +#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_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_; +}; + + +#pragma db object +class person +{ +public: + person (const std::string& first, + const std::string& last, + const std::string& title) + : name_ (first, last, title) + { + } + + // Name. + // + typedef ::name name_type; + + const name_type& + name () const + { + return name_; + } + + name_type& + name () + { + return name_; + } + +private: + friend class odb::access; + + person (): name_ ("", "", "") {} + + #pragma db id auto + unsigned long id_; + + name_type name_; +}; + +#endif // PERSON_HXX -- cgit v1.1