From cbaa5532f056d0ad20300abbba253b065982187a Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 8 Nov 2011 17:06:00 +0200 Subject: Add common/transaction test (port from tracer) --- common/makefile | 1 + common/transaction/driver.cxx | 117 ++++++++++++++++++++++++++++++++++++++++++ common/transaction/makefile | 88 +++++++++++++++++++++++++++++++ common/transaction/test.std | 21 ++++++++ libcommon/common/common.hxx | 2 +- libcommon/common/concrete.hxx | 43 ++++++++++++++++ 6 files changed, 271 insertions(+), 1 deletion(-) create mode 100644 common/transaction/driver.cxx create mode 100644 common/transaction/makefile create mode 100644 common/transaction/test.std create mode 100644 libcommon/common/concrete.hxx diff --git a/common/makefile b/common/makefile index c75ea56..81481b6 100644 --- a/common/makefile +++ b/common/makefile @@ -31,6 +31,7 @@ relationship \ relationship-query \ schema \ template \ +transaction \ types \ view \ wrapper diff --git a/common/transaction/driver.cxx b/common/transaction/driver.cxx new file mode 100644 index 0000000..d868782 --- /dev/null +++ b/common/transaction/driver.cxx @@ -0,0 +1,117 @@ +// file : common/transaction/driver.cxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +// Test transaction operations. +// + +#include +#include +#include + +#include +#include +#include +#include + +#include +#include + +using namespace std; +using namespace odb::core; + +struct transaction_tracer: odb::tracer +{ + virtual void + execute (connection&, const char* s) + { + string str (s); + + if (str == "BEGIN") + cout << "begin transaction" << endl; + else if (str == "COMMIT") + cout << "commit transaction" << endl; + else if (str == "ROLLBACK") + cout << "rollback transaction" << endl; + } +}; + +int +main (int argc, char* argv[]) +{ + transaction_tracer tracer; + auto_ptr db (create_database (argc, argv, false)); + db->tracer (tracer); + + assert (!transaction::has_current ()); + + // Current and db accessors. + // + cout << "test 001" << endl; + { + transaction t (db->begin ()); + assert (&t.database () == db.get ()); + assert (transaction::has_current ()); + assert (&transaction::current () == &t); + + transaction::reset_current (); + assert (!transaction::has_current ()); + + transaction t2 (db->begin (), false); + assert (!transaction::has_current ()); + + transaction::current (t2); + assert (&transaction::current () == &t2); + } + + // Commit. + // + cout << "test 002" << endl; + { + transaction t (db->begin ()); + t.commit (); + } + + // Rollback. + // + cout << "test 003" << endl; + { + transaction t (db->begin ()); + t.rollback (); + } + + // Auto rollback. + // + cout << "test 004" << endl; + { + transaction t (db->begin ()); + } + + // Nested transaction. + // + cout << "test 005" << endl; + { + transaction t (db->begin ()); + + try + { + transaction n (db->begin ()); + } + catch (const already_in_transaction&) + { + cout << "already_in_transaction" << endl; + } + } + + // Concrete transaction type. + // + cout << "test 006" << endl; + { + assert (sizeof (odb_db::transaction) == sizeof (transaction)); + + odb_db::transaction t (static_cast (*db).begin ()); + odb_db::transaction& r (odb_db::transaction::current ()); + assert (&t == &r); + } +} diff --git a/common/transaction/makefile b/common/transaction/makefile new file mode 100644 index 0000000..7c0889b --- /dev/null +++ b/common/transaction/makefile @@ -0,0 +1,88 @@ +# file : common/transaction/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 +cxx_obj := $(addprefix $(out_base)/,$(cxx_tun:.cxx=.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 + +# 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) + +$(call include-dep,$(cxx_od)) + +# 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): 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 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)) + $(call message,,rm -f $(out_base)/test.out) + +# Generated .gitignore. +# +ifeq ($(out_base),$(src_base)) +$(driver): | $(out_base)/.gitignore + +$(out_base)/.gitignore: files := driver +$(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,$(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/transaction/test.std b/common/transaction/test.std new file mode 100644 index 0000000..792010c --- /dev/null +++ b/common/transaction/test.std @@ -0,0 +1,21 @@ +test 001 +begin transaction +begin transaction +rollback transaction +rollback transaction +test 002 +begin transaction +commit transaction +test 003 +begin transaction +rollback transaction +test 004 +begin transaction +rollback transaction +test 005 +begin transaction +already_in_transaction +rollback transaction +test 006 +begin transaction +rollback transaction diff --git a/libcommon/common/common.hxx b/libcommon/common/common.hxx index b3ab40c..ead8888 100644 --- a/libcommon/common/common.hxx +++ b/libcommon/common/common.hxx @@ -15,7 +15,7 @@ #include #include -// Make sure assert() is not diabled. +// Make sure assert() is not disabled. // #ifdef NDEBUG # error ODB tests require enabled assert(); un-define the NDEBUG macro diff --git a/libcommon/common/concrete.hxx b/libcommon/common/concrete.hxx new file mode 100644 index 0000000..d3c8cc4 --- /dev/null +++ b/libcommon/common/concrete.hxx @@ -0,0 +1,43 @@ +// file : libcommon/common/concrete.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef LIBCOMMON_COMMON_CONCRETE_HXX +#define LIBCOMMON_COMMON_CONCRETE_HXX + +#include + +// Namespace alias for the concrete database namespace. +// +#if defined(DATABASE_MYSQL) + +#include +#include + +namespace odb_db = odb::mysql; + +#elif defined(DATABASE_SQLITE) + +#include +#include + +namespace odb_db = odb::sqlite; + +#elif defined(DATABASE_PGSQL) + +#include +#include + +namespace odb_db = odb::pgsql; + +#elif defined(DATABASE_ORACLE) + +#include +#include + +namespace odb_db = odb::oracle; + +#endif + +#endif // LIBCOMMON_COMMON_CONCRETE_HXX -- cgit v1.1