aboutsummaryrefslogtreecommitdiff
path: root/sqlite/attach/driver.cxx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2021-06-14 10:01:20 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2021-06-14 10:01:20 +0200
commitd776aad4d4abb8eabd240621eb3d13b59a5b17d8 (patch)
treedc0ad5955eab72ba96452423aebfc6b7ad110540 /sqlite/attach/driver.cxx
parent1357765fd5d2bca38193f9be6f38618aef0b3ab0 (diff)
Add test for SQLite ATTACH DATABASE support
Diffstat (limited to 'sqlite/attach/driver.cxx')
-rw-r--r--sqlite/attach/driver.cxx107
1 files changed, 107 insertions, 0 deletions
diff --git a/sqlite/attach/driver.cxx b/sqlite/attach/driver.cxx
new file mode 100644
index 0000000..8d0672f
--- /dev/null
+++ b/sqlite/attach/driver.cxx
@@ -0,0 +1,107 @@
+// file : sqlite/attach/driver.cxx
+// license : GNU GPL v2; see accompanying LICENSE file
+
+// Test attached database support.
+//
+
+#include <memory> // std::auto_ptr
+#include <cassert>
+#include <iostream>
+
+#include <odb/schema-catalog.hxx>
+
+#include <odb/sqlite/database.hxx>
+#include <odb/sqlite/connection.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> mdb (create_specific_database<database> (argc, argv));
+
+ {
+ object o ("one");
+
+ connection_ptr c (mdb->connection ());
+
+ database adb (c, ":memory:", "adb");
+
+ // Create schema similar to create_database().
+ //
+ {
+ c->execute ("PRAGMA foreign_keys=OFF");
+
+ transaction t (c->begin ());
+ odb::schema_catalog::create_schema (adb);
+ t.commit ();
+
+ c->execute ("PRAGMA foreign_keys=ON");
+ }
+
+ {
+ transaction t (c->begin ());
+ mdb->persist (o);
+ adb.persist (o);
+ t.commit ();
+ }
+
+ {
+ transaction t (c->begin ());
+ auto_ptr<object> p (adb.load<object> (o.id));
+ t.commit ();
+
+ assert (p->str == o.str);
+ }
+
+ {
+ o.str = "two";
+
+ transaction t (c->begin ());
+ adb.update (o);
+ t.commit ();
+ }
+
+ {
+ typedef sqlite::query<object> query;
+
+ transaction t (c->begin ());
+ auto_ptr<object> p (adb.query_one<object> (query::str == "two"));
+ t.commit ();
+
+ assert (p->str == o.str);
+ }
+
+ {
+ transaction t (c->begin ());
+ adb.erase (o);
+ t.commit ();
+ }
+
+ {
+ transaction t (c->begin ());
+ auto_ptr<object> p (mdb->load<object> (o.id));
+ t.commit ();
+
+ assert (p.get () != 0);
+ }
+
+ adb.detach ();
+ }
+ }
+ catch (const odb::exception& e)
+ {
+ cerr << e.what () << endl;
+ return 1;
+ }
+}