diff options
Diffstat (limited to 'odb-tests/sqlite/attach')
-rw-r--r-- | odb-tests/sqlite/attach/buildfile | 35 | ||||
-rw-r--r-- | odb-tests/sqlite/attach/driver.cxx | 109 | ||||
-rw-r--r-- | odb-tests/sqlite/attach/test.hxx | 32 | ||||
-rw-r--r-- | odb-tests/sqlite/attach/testscript | 9 |
4 files changed, 185 insertions, 0 deletions
diff --git a/odb-tests/sqlite/attach/buildfile b/odb-tests/sqlite/attach/buildfile new file mode 100644 index 0000000..bff5435 --- /dev/null +++ b/odb-tests/sqlite/attach/buildfile @@ -0,0 +1,35 @@ +# file : sqlite/attach/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_attach_ \ + --generate-schema \ + --default-database common \ + --generate-query \ + --schema main + +cxx.poptions =+ "-I$out_base" "-I$src_base" diff --git a/odb-tests/sqlite/attach/driver.cxx b/odb-tests/sqlite/attach/driver.cxx new file mode 100644 index 0000000..25d279f --- /dev/null +++ b/odb-tests/sqlite/attach/driver.cxx @@ -0,0 +1,109 @@ +// file : sqlite/attach/driver.cxx +// license : GNU GPL v2; see accompanying LICENSE file + +// Test attached database support. +// + +#include <memory> // std::unique_ptr +#include <iostream> + +#include <odb/schema-catalog.hxx> + +#include <odb/sqlite/database.hxx> +#include <odb/sqlite/connection.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> 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 ()); + unique_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 ()); + unique_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 ()); + unique_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; + } +} diff --git a/odb-tests/sqlite/attach/test.hxx b/odb-tests/sqlite/attach/test.hxx new file mode 100644 index 0000000..b3b8f63 --- /dev/null +++ b/odb-tests/sqlite/attach/test.hxx @@ -0,0 +1,32 @@ +// file : sqlite/attach/test.hxx +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include <odb/core.hxx> +#include <odb/nullable.hxx> + +#include <string> + +#include <odb/core.hxx> + +#pragma db object +struct object +{ + explicit + object (const std::string& s): str (s) {} + + #pragma db id auto + unsigned long id; + + #pragma db unique + std::string str; + +private: + object () {} + + friend class odb::access; +}; + +#endif // TEST_HXX diff --git a/odb-tests/sqlite/attach/testscript b/odb-tests/sqlite/attach/testscript new file mode 100644 index 0000000..db5089d --- /dev/null +++ b/odb-tests/sqlite/attach/testscript @@ -0,0 +1,9 @@ +# file : sqlite/attach/testscript +# license : GNU GPL v2; see accompanying LICENSE file + +.include ../../database-options.testscript +.include ../../sqlite.testscript + +: basics +: +$* |