From e8b23f12981a754757c49471f4e4a2376368909b Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 2 Sep 2013 14:40:50 +0200 Subject: Handle SQLite commit failures that don't automatically rollback transaction --- sqlite/makefile | 1 + sqlite/transaction/driver.cxx | 60 ++++++++++++++++++++++++ sqlite/transaction/makefile | 105 ++++++++++++++++++++++++++++++++++++++++++ sqlite/transaction/test.hxx | 27 +++++++++++ sqlite/transaction/test.std | 0 5 files changed, 193 insertions(+) create mode 100644 sqlite/transaction/driver.cxx create mode 100644 sqlite/transaction/makefile create mode 100644 sqlite/transaction/test.hxx create mode 100644 sqlite/transaction/test.std (limited to 'sqlite') diff --git a/sqlite/makefile b/sqlite/makefile index 7348436..ebc9cde 100644 --- a/sqlite/makefile +++ b/sqlite/makefile @@ -9,6 +9,7 @@ template \ custom \ database \ native \ +transaction \ truncation \ types 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 // std::auto_ptr +#include +#include + +#include +#include + +#include + +#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 db (create_specific_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 (*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 +#include + +#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 p; +}; + +#endif // TEST_HXX diff --git a/sqlite/transaction/test.std b/sqlite/transaction/test.std new file mode 100644 index 0000000..e69de29 -- cgit v1.1