From 683eccc6887a7fa34d58e873e19b48405ebf420a Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 4 Jul 2011 17:53:47 +0200 Subject: Implement support for database operations callbacks New object pragma: callback. New test: common/callback. New manual section: 10.1.4, "callback". --- common/callback/driver.cxx | 160 +++++++++++++++++++++++++++++++++++++++++++++ common/callback/makefile | 109 ++++++++++++++++++++++++++++++ common/callback/test.hxx | 32 +++++++++ common/callback/test.std | 52 +++++++++++++++ common/makefile | 1 + 5 files changed, 354 insertions(+) create mode 100644 common/callback/driver.cxx create mode 100644 common/callback/makefile create mode 100644 common/callback/test.hxx create mode 100644 common/callback/test.std diff --git a/common/callback/driver.cxx b/common/callback/driver.cxx new file mode 100644 index 0000000..2934737 --- /dev/null +++ b/common/callback/driver.cxx @@ -0,0 +1,160 @@ +// file : common/callback/driver.cxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +// Test database operation callbacks. +// + +#include // std::auto_ptr +#include +#include + +#include +#include + +#include + +#include "test.hxx" +#include "test-odb.hxx" + +using namespace std; +using namespace odb::core; + +const char* events[] = +{ + "pre_persist", + "post_persist", + "pre_load", + "post_load", + "pre_update", + "post_update", + "pre_erase", + "post_erase" +}; + +void object:: +db_callback (callback_event e, database&) +{ + cout << " " << events[e] << " " << id_ << endl; +} + +void object:: +db_callback (callback_event e, database&) const +{ + cout << " " << events[e] << " " << id_ << " const" << endl; +} + +int +main (int argc, char* argv[]) +{ + try + { + auto_ptr db (create_database (argc, argv)); + + // Persist. + // + cout << "persist" << endl; + { + object o1 (1, 1); + object const o2 (2, 2); + transaction t (db->begin ()); + db->persist (o1); + db->persist (&o2); + t.commit (); + } + cout << "***" << endl; + + // Load. + // + cout << "load" << endl; + { + transaction t (db->begin ()); + auto_ptr o1 (db->load (1)); + object o2; + db->load (2, o2); + t.commit (); + } + cout << "***" << endl; + + // Query. + // + cout << "query" << endl; + { + typedef odb::query query; + typedef odb::result result; + + transaction t (db->begin ()); + result r (db->query (query::id < 3)); + + for (result::iterator i (r.begin ()); i != r.end (); ++i) + { + if (i->id_ > 3) // Load. + break; + } + + t.commit (); + } + cout << "***" << endl; + + // Update. + // + cout << "update" << endl; + { + transaction t (db->begin ()); + auto_ptr o1 (db->load (1)); + auto_ptr o2 (db->load (2)); + o1->data++; + o2->data++; + db->update (o1.get ()); + db->update (static_cast (*o2)); + t.commit (); + } + cout << "***" << endl; + + // Erase. + // + cout << "erase" << endl; + { + transaction t (db->begin ()); + auto_ptr o1 (db->load (1)); + auto_ptr o2 (db->load (2)); + db->erase (static_cast (o1.get ())); + db->erase (*o2); + t.commit (); + } + cout << "***" << endl; + + // Delayed (recursive) load. + // + cout << "delayed load" << endl; + { + { + object o1 (1, 1); + object o2 (2, 2); + object o3 (3, 2); + + o1.pobj = &o2; + o2.pobj = &o3; + + transaction t (db->begin ()); + db->persist (o1); + db->persist (o2); + db->persist (o3); + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr o1 (db->load (1)); + t.commit (); + } + } + cout << "***" << endl; + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/common/callback/makefile b/common/callback/makefile new file mode 100644 index 0000000..640e148 --- /dev/null +++ b/common/callback/makefile @@ -0,0 +1,109 @@ +# file : common/callback/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 := test.hxx +cxx_obj := $(addprefix $(out_base)/,$(cxx_tun:.cxx=.o) $(odb_hdr:.hxx=-odb.o)) +cxx_od := $(cxx_obj:.o=.o.d) + +common.l := $(out_root)/libcommon/common/common.l +common.l.cpp-options := $(out_root)/libcommon/common/common.l.cpp-options + +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) + +# Build. +# +$(driver): $(cxx_obj) $(common.l) +$(cxx_obj) $(cxx_od): cpp_options := -I$(out_base) -I$(src_base) +$(cxx_obj) $(cxx_od): $(common.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 += --database $(db_id) --generate-schema \ +--generate-query +$(gen): cpp_options := -I$(src_base) +$(gen): $(common.l.cpp-options) + +$(call include-dep,$(cxx_od),$(cxx_obj),$(gen)) + +# Alias for default target. +# +$(out_base)/: $(driver) + +# Dist +# +name := $(subst /,-,$(subst $(src_root)/common/,,$(src_base))) + +$(dist): db_id := @database@ +$(dist): sources := $(cxx_tun) +$(dist): headers := $(odb_hdr) +$(dist): data_dist := test.std +$(dist): export name := $(name) +$(dist): export extra_dist := $(data_dist) $(call vc9projs,$(name)) \ +$(call vc10projs,$(name)) +$(dist): + $(call dist-data,$(sources) $(headers) $(data_dist)) + $(call meta-automake,../template/Makefile.am) + $(call meta-vc9projs,../template/template,$(name)) + $(call meta-vc10projs,../template/template,$(name)) + +# Test. +# +$(test): $(driver) $(src_base)/test.std + $(call schema) + $(call message,test $<,$< --options-file $(dcf_root)/db.options \ +>$(out_base)/test.out) + $(call message,,diff -u $(src_base)/test.std $(out_base)/test.out) + $(call message,,rm -f $(out_base)/test.out) + +# 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)) + $(call message,,rm -f $(out_base)/test.out) + +# 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) + +# Dependencies. +# +$(call import,$(src_root)/libcommon/makefile) diff --git a/common/callback/test.hxx b/common/callback/test.hxx new file mode 100644 index 0000000..274e2b5 --- /dev/null +++ b/common/callback/test.hxx @@ -0,0 +1,32 @@ +// file : common/callback/test.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include +#include + +#pragma db object callback(db_callback) +struct object +{ + object (unsigned long id, unsigned long d): id_ (id), data (d), pobj (0) {} + object (): id_ (0) {}; + + #pragma db id + unsigned long id_; + + unsigned long data; + + object* pobj; + + void + db_callback (odb::callback_event, odb::database&); + + void + db_callback (odb::callback_event, odb::database&) const; +}; + +#endif // TEST_HXX diff --git a/common/callback/test.std b/common/callback/test.std new file mode 100644 index 0000000..05ae286 --- /dev/null +++ b/common/callback/test.std @@ -0,0 +1,52 @@ +persist + pre_persist 1 + post_persist 1 + pre_persist 2 const + post_persist 2 const +*** +load + pre_load 0 + post_load 1 + pre_load 0 + post_load 2 +*** +query + pre_load 0 + post_load 1 + pre_load 0 + post_load 2 +*** +update + pre_load 0 + post_load 1 + pre_load 0 + post_load 2 + pre_update 1 + post_update 1 + pre_update 2 const + post_update 2 const +*** +erase + pre_load 0 + post_load 1 + pre_load 0 + post_load 2 + pre_erase 1 const + post_erase 1 const + pre_erase 2 + post_erase 2 +*** +delayed load + pre_persist 1 + post_persist 1 + pre_persist 2 + post_persist 2 + pre_persist 3 + post_persist 3 + pre_load 0 + pre_load 0 + pre_load 0 + post_load 3 + post_load 2 + post_load 1 +*** diff --git a/common/makefile b/common/makefile index 394f261..d2e8b0f 100644 --- a/common/makefile +++ b/common/makefile @@ -7,6 +7,7 @@ include $(dir $(lastword $(MAKEFILE_LIST)))../build/bootstrap.make tests := \ auto \ +callback \ composite \ const \ container \ -- cgit v1.1