diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2013-09-02 14:40:50 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2013-09-02 14:40:50 +0200 |
commit | e8b23f12981a754757c49471f4e4a2376368909b (patch) | |
tree | 4977861a21b253b4fe7de43ba76e5d8f270997df /sqlite/transaction | |
parent | dcb6d84266a0b291781855cf623aa4d4c395f14d (diff) |
Handle SQLite commit failures that don't automatically rollback transaction
Diffstat (limited to 'sqlite/transaction')
-rw-r--r-- | sqlite/transaction/driver.cxx | 60 | ||||
-rw-r--r-- | sqlite/transaction/makefile | 105 | ||||
-rw-r--r-- | sqlite/transaction/test.hxx | 27 | ||||
-rw-r--r-- | sqlite/transaction/test.std | 0 |
4 files changed, 192 insertions, 0 deletions
diff --git a/sqlite/transaction/driver.cxx b/sqlite/transaction/driver.cxx new file mode 100644 index 0000000..a70e1fd --- /dev/null +++ b/sqlite/transaction/driver.cxx @@ -0,0 +1,60 @@ +// file : sqlite/transaction/driver.cxx +// copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +// Test esoteric SQLite transaction semantics aspects. +// + +#include <memory> // std::auto_ptr +#include <cassert> +#include <iostream> + +#include <odb/sqlite/database.hxx> +#include <odb/sqlite/transaction.hxx> + +#include <common/common.hxx> + +#include "test.hxx" +#include "test-odb.hxx" + +using namespace std; +namespace sqlite = odb::sqlite; +using namespace sqlite; + +int +main (int argc, char* argv[]) +{ + try + { + auto_ptr<database> db (create_specific_database<database> (argc, argv)); + + // In SQLite, when a commit fails because of the deferred foreign + // key constraint violation, the transaction is not automatically + // rolled back. Make sure we compensate for that. + // + try + { + object o; + o.p = odb::lazy_ptr<object1> (*db, 0); + + transaction t (db->begin ()); + db->persist(o); + t.commit (); + } + catch (const odb::exception&) + { + } + + // Make sure we can start a new transaction. + // + { + transaction t (db->begin ()); + t.commit (); + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/sqlite/transaction/makefile b/sqlite/transaction/makefile new file mode 100644 index 0000000..21d1c7d --- /dev/null +++ b/sqlite/transaction/makefile @@ -0,0 +1,105 @@ +# file : sqlite/transaction/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 := test.hxx +genf := $(call odb-gen,$(odb_hdr)) +gen := $(addprefix $(out_base)/,$(genf)) +cxx_obj := $(addprefix $(out_base)/,$(cxx_tun:.cxx=.o)) $(filter %.o,$(gen:.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 + +# 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) + +$(gen): $(odb) +$(gen): odb := $(odb) +$(gen) $(dist): export odb_options += --database sqlite --default-database \ +common --generate-schema --table-prefix sqlitex_transaction_ +$(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 +# +$(dist): sources := $(cxx_tun) +$(dist): headers := $(odb_hdr) +$(dist): data_dist := test.std +$(dist): export name := $(subst /,-,$(subst $(src_root)/sqlite/,,$(src_base))) +$(dist): export extra_dist := $(data_dist) \ +$(name)-vc8.vcproj $(name)-vc9.vcproj \ +$(name)-vc10.vcxproj $(name)-vc10.vcxproj.filters \ +$(name)-vc11.vcxproj $(name)-vc11.vcxproj.filters +$(dist): + $(call dist-data,$(sources) $(headers) $(data_dist)) + $(call meta-automake,../template/Makefile.am) + $(call meta-vc8proj,../template/template-vc8.vcproj,$(name)-vc8.vcproj) + $(call meta-vc9proj,../template/template-vc9.vcproj,$(name)-vc9.vcproj) + $(call meta-vc10proj,../template/template-vc10.vcxproj,$(name)-vc10.vcxproj) + $(call meta-vc11proj,../template/template-vc11.vcxproj,$(name)-vc11.vcxproj) + +# Test. +# +$(eval $(call test-schemaless-rule)) + +# Clean. +# +$(clean): \ + $(driver).o.clean \ + $(addsuffix .cxx.clean,$(cxx_obj)) \ + $(addsuffix .cxx.clean,$(cxx_od)) \ + $(addsuffix .hxx.clean,$(filter %.cxx,$(gen))) + $(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/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) + +# Dependencies. +# +$(call import,$(src_root)/libcommon/makefile) diff --git a/sqlite/transaction/test.hxx b/sqlite/transaction/test.hxx new file mode 100644 index 0000000..ae47107 --- /dev/null +++ b/sqlite/transaction/test.hxx @@ -0,0 +1,27 @@ +// file : sqlite/transaction/test.hxx +// copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include <odb/core.hxx> +#include <odb/lazy-ptr.hxx> + +#pragma db object +struct object1 +{ + #pragma db id + unsigned long id_; +}; + +#pragma db object +struct object +{ + #pragma db id auto + unsigned long id_; + + odb::lazy_ptr<object1> p; +}; + +#endif // TEST_HXX diff --git a/sqlite/transaction/test.std b/sqlite/transaction/test.std new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/sqlite/transaction/test.std |