summaryrefslogtreecommitdiff
path: root/odb-tests/sqlite/transaction
diff options
context:
space:
mode:
Diffstat (limited to 'odb-tests/sqlite/transaction')
-rw-r--r--odb-tests/sqlite/transaction/buildfile33
-rw-r--r--odb-tests/sqlite/transaction/driver.cxx61
-rw-r--r--odb-tests/sqlite/transaction/test.hxx26
-rw-r--r--odb-tests/sqlite/transaction/testscript9
4 files changed, 129 insertions, 0 deletions
diff --git a/odb-tests/sqlite/transaction/buildfile b/odb-tests/sqlite/transaction/buildfile
new file mode 100644
index 0000000..1653c29
--- /dev/null
+++ b/odb-tests/sqlite/transaction/buildfile
@@ -0,0 +1,33 @@
+# file : sqlite/transaction/buildfile
+# license : GNU GPL v2; see accompanying LICENSE file
+
+if ($build.meta_operation != 'dist')
+{
+ assert ($sqlite) "sqlite should be configured for this test"
+ assert (!$multi) "multi-database mode is not supported by this test"
+}
+
+import libodb = libodb%lib{odb}
+
+import libs = libodb-sqlite%lib{odb-sqlite}
+import libs += lib{common}
+
+exe{driver}: {hxx cxx}{* -*-odb} {hxx ixx cxx}{test-odb} testscript
+
+# Introduce the metadata library target to make sure the libodb library is
+# resolved for the odb_compile ad hoc rule (see build/root.build for details).
+#
+libue{test-meta}: $libodb
+
+<{hxx ixx cxx}{test-odb}>: hxx{test} libue{test-meta}
+
+exe{driver}: libue{test-meta} $libs
+
+# Specify the ODB custom options to be used by the odb_compile ad hoc rule
+# (see build/root.build for details).
+#
+odb_options = --table-prefix sqlitex_transaction_ \
+ --generate-schema \
+ --default-database common
+
+cxx.poptions =+ "-I$out_base" "-I$src_base"
diff --git a/odb-tests/sqlite/transaction/driver.cxx b/odb-tests/sqlite/transaction/driver.cxx
new file mode 100644
index 0000000..80a0804
--- /dev/null
+++ b/odb-tests/sqlite/transaction/driver.cxx
@@ -0,0 +1,61 @@
+// file : sqlite/transaction/driver.cxx
+// license : GNU GPL v2; see accompanying LICENSE file
+
+// Test esoteric SQLite transaction semantics aspects.
+//
+
+#include <memory> // std::unique_ptr
+#include <iostream>
+
+#include <odb/sqlite/database.hxx>
+#include <odb/sqlite/transaction.hxx>
+
+#include <libcommon/common.hxx>
+
+#include "test.hxx"
+#include "test-odb.hxx"
+
+#undef NDEBUG
+#include <cassert>
+
+using namespace std;
+namespace sqlite = odb::sqlite;
+using namespace sqlite;
+
+int
+main (int argc, char* argv[])
+{
+ try
+ {
+ unique_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/odb-tests/sqlite/transaction/test.hxx b/odb-tests/sqlite/transaction/test.hxx
new file mode 100644
index 0000000..bfb981b
--- /dev/null
+++ b/odb-tests/sqlite/transaction/test.hxx
@@ -0,0 +1,26 @@
+// file : sqlite/transaction/test.hxx
+// 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/odb-tests/sqlite/transaction/testscript b/odb-tests/sqlite/transaction/testscript
new file mode 100644
index 0000000..c3388cb
--- /dev/null
+++ b/odb-tests/sqlite/transaction/testscript
@@ -0,0 +1,9 @@
+# file : sqlite/transaction/testscript
+# license : GNU GPL v2; see accompanying LICENSE file
+
+.include ../../database-options.testscript
+.include ../../sqlite.testscript
+
+: basics
+:
+$*