aboutsummaryrefslogtreecommitdiff
path: root/section
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2013-08-14 15:16:09 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2013-08-14 15:16:09 +0200
commit22cb56f4a1ad5d2c6d581bab5e528a0cf0d13b17 (patch)
tree7d16b824f639b5133a480abb094c350eedbb1d2e /section
parentbb02fdbce23477d931a1572f8b2ffe52c80edf32 (diff)
Add support for object sections
Sections are an optimization mechanism that allows the partitioning of data members of a persistent class into groups that can be separately loaded and/or updated.
Diffstat (limited to 'section')
-rw-r--r--section/README63
-rw-r--r--section/database.hxx94
-rw-r--r--section/driver.cxx133
-rw-r--r--section/makefile122
-rw-r--r--section/person.hxx105
5 files changed, 517 insertions, 0 deletions
diff --git a/section/README b/section/README
new file mode 100644
index 0000000..34be589
--- /dev/null
+++ b/section/README
@@ -0,0 +1,63 @@
+This example shows how to use object sections to implement lazy-loading
+and change-updating of a subset of data members in a persistent class.
+
+The example consists of the following files:
+
+person.hxx
+ Header file defining the 'person' persistent class. It contains a number of
+ data members some of which are made to belong to a section.
+
+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 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 back, including
+ the section, and prints the contents of its members. Finally, the driver
+ shows how to update the state of the section data members in various ways,
+ then re-loads and prints the object to verify that the changes have been
+ made persistent.
+
+To compile and link the example manually from the command line we can use
+the following commands (using MySQL as an example; replace 'c++' with your
+C++ compiler name):
+
+c++ -c person-odb.cxx
+c++ -DDATABASE_MYSQL -c driver.cxx
+c++ -o driver driver.o person-odb.o -lodb-mysql -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 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/section/database.hxx b/section/database.hxx
new file mode 100644
index 0000000..4a1a350
--- /dev/null
+++ b/section/database.hxx
@@ -0,0 +1,94 @@
+// file : section/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::auto_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::auto_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)
+ auto_ptr<database> db (new odb::mysql::database (argc, argv));
+#elif defined(DATABASE_SQLITE)
+ auto_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)
+ auto_ptr<database> db (new odb::pgsql::database (argc, argv));
+#elif defined(DATABASE_ORACLE)
+ auto_ptr<database> db (new odb::oracle::database (argc, argv));
+#elif defined(DATABASE_MSSQL)
+ auto_ptr<database> db (new odb::mssql::database (argc, argv));
+#endif
+
+ return db;
+}
+
+#endif // DATABASE_HXX
diff --git a/section/driver.cxx b/section/driver.cxx
new file mode 100644
index 0000000..b01454e
--- /dev/null
+++ b/section/driver.cxx
@@ -0,0 +1,133 @@
+// file : section/driver.cxx
+// 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::core;
+
+int
+main (int argc, char* argv[])
+{
+ try
+ {
+ auto_ptr<database> db (create_database (argc, argv));
+
+ unsigned long id;
+
+ // Create a persistent person object.
+ //
+ {
+ person joe ("Joe", "Dirt");
+ joe.bio ("Joe Dirt is a very strange man indeed...");
+ joe.nicknames ().push_back ("JD");
+
+ // At this point the state of a section is undefined since the
+ // object is transient.
+
+ transaction t (db->begin ());
+ id = db->persist (joe);
+ t.commit ();
+
+ // Now, since the object is persistent, the state of the section
+ // is loaded and unchanged.
+ }
+
+ // Load the object and print what we've got. Then update the bio.
+ //
+ {
+ transaction t (db->begin ());
+ auto_ptr<person> p (db->load<person> (id));
+
+ // Now, while the object is loaded, the section is not. If (and
+ // when) we need to access the section data members, we have to
+ // load them explicitly.
+
+ cout << p->first () << " " << p->last () << endl;
+
+ db->load (*p, p->extras_section ());
+
+ // Now the section is loaded and unchanged.
+
+ cout << " " << p->bio () << endl
+ << " " << p->nicknames ().size () << " nickname(s)" << endl;
+
+ // Update the bio.
+ //
+ p->bio ("Joe Dirt is a very clean man indeed...");
+
+ // Now, the section is marked changed by the bio() modifier.
+ // Because of that, the following object update will also
+ // update the section.
+ //
+ db->update (*p);
+
+ t.commit ();
+ }
+
+ // Add a new nickname.
+ //
+ {
+ transaction t (db->begin ());
+
+ auto_ptr<person> p (db->load<person> (id));
+ db->load (*p, p->extras_section ());
+
+ // Add a new nickname. The nicknames container is now marked
+ // changed.
+ //
+ p->nicknames ().push_back ("Squeaky Clean");
+
+ // While the section hasn't been marked as changed, a change-
+ // tracking container that belongs to it has been. As a result,
+ // the following object update call will automatically mark the
+ // section as changed and update its state in the database.
+ //
+ db->update (*p);
+
+ t.commit ();
+ }
+
+ // We can also update just the section if we know the rest of
+ // the object hasn't changed.
+ //
+ {
+ transaction t (db->begin ());
+
+ auto_ptr<person> p (db->load<person> (id));
+ db->load (*p, p->extras_section ());
+
+ p->nicknames ().push_back ("Dirty Joe");
+ db->update (*p, p->extras_section ());
+
+ t.commit ();
+ }
+
+ // Load and print the updated object.
+ //
+ {
+ transaction t (db->begin ());
+ auto_ptr<person> p (db->load<person> (id));
+ db->load (*p, p->extras_section ());
+ t.commit ();
+
+ cout << p->first () << " " << p->last () << endl
+ << " " << p->bio () << endl
+ << " " << p->nicknames ().size () << " nickname(s)" << endl;
+ }
+ }
+ catch (const odb::exception& e)
+ {
+ cerr << e.what () << endl;
+ return 1;
+ }
+}
diff --git a/section/makefile b/section/makefile
new file mode 100644
index 0000000..abbb9c9
--- /dev/null
+++ b/section/makefile
@@ -0,0 +1,122 @@
+# file : section/makefile
+# copyright : Copyright (c) 2009-2013 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) -I$(src_base) -D$(db_macro)
+$(cxx_obj) $(cxx_od): $(odb.l.cpp-options) $(odb_db.l.cpp-options)
+
+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 += --generate-schema --table-prefix section_
+$(gen): cpp_options := -I$(src_base)
+$(gen): $(odb.l.cpp-options)
+
+$(gen): odb_options += --database $(db_id)
+
+$(call include-dep,$(cxx_od),$(cxx_obj),$(gen))
+
+# Alias for default target.
+#
+$(out_base)/: $(driver)
+
+# Dist
+#
+name := $(subst /,-,$(subst $(src_root)/,,$(src_base)))
+
+$(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 vc8projs,$(name)) \
+$(call vc9projs,$(name)) $(call vc10projs,$(name)) $(call vc11projs,$(name))
+$(dist):
+ $(call dist-data,$(sources) $(headers) README database.hxx)
+ $(call meta-automake,../template/Makefile.am)
+ $(call meta-vc8projs,../template/template,$(name))
+ $(call meta-vc9projs,../template/template,$(name))
+ $(call meta-vc10projs,../template/template,$(name))
+ $(call meta-vc11projs,../template/template,$(name))
+
+# Test.
+#
+$(test): header := $(odb_hdr)
+$(test): $(driver)
+ $(call schema)
+ $(call message,test $<,$< --options-file $(dcf_root)/$(db_id).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/vc8proj.make)
+$(call include,$(bld_root)/meta/vc9proj.make)
+$(call include,$(bld_root)/meta/vc10proj.make)
+$(call include,$(bld_root)/meta/vc11proj.make)
+$(call include,$(bld_root)/meta/automake.make)
+
+$(call include,$(bld_root)/cxx/standard.make) # cxx_standard
+ifdef cxx_standard
+$(gen): odb_options += --std $(cxx_standard)
+$(call include,$(odb_rules))
+endif
+
+$(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/section/person.hxx b/section/person.hxx
new file mode 100644
index 0000000..996fc19
--- /dev/null
+++ b/section/person.hxx
@@ -0,0 +1,105 @@
+// file : section/person.hxx
+// copyright : not copyrighted - public domain
+
+#ifndef PERSON_HXX
+#define PERSON_HXX
+
+#include <string>
+
+#include <odb/core.hxx>
+#include <odb/vector.hxx>
+#include <odb/section.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_;
+ }
+
+ // Biography.
+ //
+ const std::string&
+ bio () const
+ {
+ return bio_;
+ }
+
+ void
+ bio (const std::string& bio)
+ {
+ bio_ = bio;
+ extras_.change (); // Mark the section as changed.
+ }
+
+ // Nicknames.
+ //
+ typedef odb::vector<std::string> names;
+
+ const names&
+ nicknames () const
+ {
+ return nicknames_;
+ }
+
+ names&
+ nicknames ()
+ {
+ // Because we are using a change-tracking container (odb::vector),
+ // ODB will automatically mark the section as changed if we update
+ // the nicknames.
+ //
+ return nicknames_;
+ }
+
+ // The extras section accessor and modifier. Notice that they are
+ // by-reference.
+ //
+ const odb::section&
+ extras_section () const
+ {
+ return extras_;
+ }
+
+ odb::section&
+ extras_section ()
+ {
+ return extras_;
+ }
+
+private:
+ friend class odb::access;
+
+ person () {}
+
+ #pragma db id auto
+ unsigned long id_;
+
+ std::string first_;
+ std::string last_;
+
+ #pragma db section(extras_)
+ std::string bio_;
+
+ #pragma db section(extras_)
+ names nicknames_;
+
+ #pragma db load(lazy) update(change)
+ odb::section extras_;
+};
+
+#endif // PERSON_HXX