aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2010-11-30 18:13:45 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2010-11-30 18:13:45 +0200
commitfe69aa1a2b29e577b86026f7bb56a19f5b3bdfef (patch)
treeb2318cae2cabd5cd534fe351d850049e36bfa38d
parentb498f6525ac1953b5ef4fdf3f027138c00c1ea19 (diff)
Add container example
-rw-r--r--README3
-rw-r--r--container/README52
-rw-r--r--container/database.hxx46
-rw-r--r--container/driver.cxx120
-rw-r--r--container/makefile117
-rw-r--r--container/person.hxx108
-rw-r--r--makefile2
7 files changed, 447 insertions, 1 deletions
diff --git a/README b/README
index 9d7e2fa..139d31e 100644
--- a/README
+++ b/README
@@ -18,6 +18,9 @@ query
Shows how to use the ODB Query Language to search the database for
persistent objects matching certain criteria.
+container
+ Shows how to use containers as data members in persistent objects.
+
mapping
Shows how to customize the mapping between C++ value types and database
types.
diff --git a/container/README b/container/README
new file mode 100644
index 0000000..1af3f7b
--- /dev/null
+++ b/container/README
@@ -0,0 +1,52 @@
+This example shows how to use containers as data members in persistent objects.
+
+The example consists of the following files:
+
+person.hxx
+ Header file defining the 'person' persistent class. It contains a number of
+ data members of various container types.
+
+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 <database> --generate-schema person.hxx
+
+ Where <database> 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 back, and prints the
+ contents of its members. Finally, the driver modifies the object by adding,
+ removing, and updating elements in its container members, stores the changes
+ in the database, then re-loads and prints the object to verify that the
+ changes have been made persistent.
+
+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/container/database.hxx b/container/database.hxx
new file mode 100644
index 0000000..c72f320
--- /dev/null
+++ b/container/database.hxx
@@ -0,0 +1,46 @@
+// file : container/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;
+
+ 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<database> (new mysql::database (argc, argv));
+#endif
+}
+
+#endif // DATABASE_HXX
diff --git a/container/driver.cxx b/container/driver.cxx
new file mode 100644
index 0000000..07b3bd9
--- /dev/null
+++ b/container/driver.cxx
@@ -0,0 +1,120 @@
+// file : container/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/transaction.hxx>
+
+#include "database.hxx" // create_database
+
+#include "person.hxx"
+#include "person-odb.hxx"
+
+using namespace std;
+using namespace odb;
+
+void
+print (const person& p)
+{
+ cout << p.first () << " " << p.last () << endl;
+
+ // Print nicknames.
+ //
+ for (person::name_list::const_iterator i (p.nicknames ().begin ());
+ i != p.nicknames ().end (); ++i)
+ {
+ cout << " nickname: " << *i << endl;
+ }
+
+ // Print emails.
+ //
+ for (person::email_set::const_iterator i (p.emails ().begin ());
+ i != p.emails ().end (); ++i)
+ {
+ cout << " email: " << *i << endl;
+ }
+
+ // Print weights.
+ //
+ for (person::age_weight_map::const_iterator i (p.age_weight ().begin ());
+ i != p.age_weight ().end (); ++i)
+ {
+ cout << " weight at " << i->first << ": " << i->second << endl;
+ }
+}
+
+int
+main (int argc, char* argv[])
+{
+ try
+ {
+ auto_ptr<database> db (create_database (argc, argv));
+
+ unsigned long id;
+
+ // Create a persistent person objects.
+ //
+ {
+ person joe ("Joe", "Dirt");
+
+ joe.nicknames ().push_back ("JD");
+ joe.nicknames ().push_back ("Squeaky");
+
+ joe.emails ().insert ("joe@example.com");
+ joe.emails ().insert ("joe.dirt@example.com");
+
+ joe.age_weight ()[5] = 15.6F;
+ joe.age_weight ()[10] = 23.3F;
+ joe.age_weight ()[15] = 29.8F;
+
+ transaction t (db->begin ());
+ id = db->persist (joe);
+ t.commit ();
+ }
+
+ // Load the object and print what we've got. Then change some
+ // information and update it in the database.
+ //
+ {
+ transaction t1 (db->begin ());
+ auto_ptr<person> j (db->load<person> (id));
+ t1.commit ();
+
+ print (*j);
+
+ // Ann another nickname.
+ //
+ j->nicknames ().push_back ("Cleaner");
+
+ // The joe@example.com email is no longer working.
+ //
+ j->emails ().erase ("joe@example.com");
+
+ // Update a weight sample.
+ //
+ j->age_weight ()[15] = 28.8F;
+
+ transaction t2 (db->begin ());
+ db->update (*j);
+ t2.commit ();
+ }
+
+ // Load and print the updated object.
+ //
+ {
+ transaction t (db->begin ());
+ auto_ptr<person> j (db->load<person> (id));
+ t.commit ();
+
+ print (*j);
+ }
+ }
+ catch (const odb::exception& e)
+ {
+ cerr << e.what () << endl;
+ return 1;
+ }
+}
diff --git a/container/makefile b/container/makefile
new file mode 100644
index 0000000..50f02ff
--- /dev/null
+++ b/container/makefile
@@ -0,0 +1,117 @@
+# file : container/makefile
+# author : Boris Kolpackov <boris@codesynthesis.com>
+# copyright : Copyright (c) 2009-2010 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
+$(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/container/person.hxx b/container/person.hxx
new file mode 100644
index 0000000..a36c0e8
--- /dev/null
+++ b/container/person.hxx
@@ -0,0 +1,108 @@
+// file : container/person.hxx
+// author : Boris Kolpackov <boris@codesynthesis.com>
+// copyright : not copyrighted - public domain
+
+#ifndef PERSON_HXX
+#define PERSON_HXX
+
+#include <set>
+#include <map>
+#include <vector>
+#include <string>
+
+#include <odb/core.hxx>
+
+#pragma db object
+class person
+{
+public:
+ person (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_;
+ }
+
+ // Nicknames.
+ //
+ typedef std::vector<std::string> name_list;
+
+ const name_list&
+ nicknames () const
+ {
+ return nicknames_;
+ }
+
+ name_list&
+ nicknames ()
+ {
+ return nicknames_;
+ }
+
+ // Emails.
+ //
+ typedef std::set<std::string> email_set;
+
+ const email_set&
+ emails () const
+ {
+ return emails_;
+ }
+
+ email_set&
+ emails ()
+ {
+ return emails_;
+ }
+
+ // Age-to-weight map.
+ //
+ typedef std::map<unsigned short, float> age_weight_map;
+
+ const age_weight_map&
+ age_weight () const
+ {
+ return age_weight_;
+ }
+
+ age_weight_map&
+ age_weight ()
+ {
+ return age_weight_;
+ }
+
+public:
+ unsigned long
+ id () const
+ {
+ return id_;
+ }
+
+private:
+ friend class odb::access;
+
+ person () {}
+
+ #pragma db id auto
+ unsigned long id_;
+
+ std::string first_;
+ std::string last_;
+
+ name_list nicknames_;
+ email_set emails_;
+ age_weight_map age_weight_;
+};
+
+#endif // PERSON_HXX
diff --git a/makefile b/makefile
index a0000cd..d768ee8 100644
--- a/makefile
+++ b/makefile
@@ -5,7 +5,7 @@
include $(dir $(lastword $(MAKEFILE_LIST)))build/bootstrap.make
-dirs := hello query mapping template
+dirs := container hello query mapping template
dist_dirs := $(filter-out template,$(dirs))
default := $(out_base)/