summaryrefslogtreecommitdiff
path: root/odb-tests/sqlite/custom
diff options
context:
space:
mode:
Diffstat (limited to 'odb-tests/sqlite/custom')
-rw-r--r--odb-tests/sqlite/custom/buildfile34
-rw-r--r--odb-tests/sqlite/custom/driver.cxx78
-rw-r--r--odb-tests/sqlite/custom/test.hxx39
-rw-r--r--odb-tests/sqlite/custom/testscript9
4 files changed, 160 insertions, 0 deletions
diff --git a/odb-tests/sqlite/custom/buildfile b/odb-tests/sqlite/custom/buildfile
new file mode 100644
index 0000000..de199b4
--- /dev/null
+++ b/odb-tests/sqlite/custom/buildfile
@@ -0,0 +1,34 @@
+# file : sqlite/custom/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_custom_ \
+ --generate-schema \
+ --default-database common \
+ --generate-query
+
+cxx.poptions =+ "-I$out_base" "-I$src_base"
diff --git a/odb-tests/sqlite/custom/driver.cxx b/odb-tests/sqlite/custom/driver.cxx
new file mode 100644
index 0000000..0627708
--- /dev/null
+++ b/odb-tests/sqlite/custom/driver.cxx
@@ -0,0 +1,78 @@
+// file : sqlite/custom/driver.cxx
+// license : GNU GPL v2; see accompanying LICENSE file
+
+// Test custom database type mapping in SQLite.
+//
+
+#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));
+
+ object o (1);
+ o.nv.push_back ("123"); // INTEGER
+ o.nv.push_back ("1.23"); // REAL
+ o.nv.push_back ("abc"); // TEXT
+
+ // Persist.
+ //
+ {
+ transaction t (db->begin ());
+ db->persist (o);
+ t.commit ();
+ }
+
+ // Load.
+ //
+ {
+ transaction t (db->begin ());
+ unique_ptr<object> o1 (db->load<object> (1));
+ t.commit ();
+
+ assert (o == *o1);
+ }
+
+ // Update.
+ //
+ o.nv[1] += "4";
+
+ {
+ transaction t (db->begin ());
+ db->update (o);
+ t.commit ();
+ }
+
+ {
+ transaction t (db->begin ());
+ unique_ptr<object> o1 (db->load<object> (1));
+ t.commit ();
+
+ assert (o == *o1);
+ }
+ }
+ catch (const odb::exception& e)
+ {
+ cerr << e.what () << endl;
+ return 1;
+ }
+}
diff --git a/odb-tests/sqlite/custom/test.hxx b/odb-tests/sqlite/custom/test.hxx
new file mode 100644
index 0000000..54a2ba3
--- /dev/null
+++ b/odb-tests/sqlite/custom/test.hxx
@@ -0,0 +1,39 @@
+// file : sqlite/custom/test.hxx
+// license : GNU GPL v2; see accompanying LICENSE file
+
+#ifndef TEST_HXX
+#define TEST_HXX
+
+#include <string>
+#include <vector>
+
+#include <odb/core.hxx>
+
+// Map NUMERIC SQLite type to std::string (or any other type that
+// provides the value_traits<?, id_string> specialization). By
+// default ODB treats NUMERIC as REAL. Note also that we don't
+// need to specify to/from conversions since SQLite will convert
+// implicitly.
+//
+#pragma db map type("NUMERIC") as("TEXT")
+
+#pragma db object
+struct object
+{
+ object () {}
+ object (unsigned long id_): id (id_) {}
+
+ #pragma db id
+ unsigned long id;
+
+ #pragma db value_type("NUMERIC")
+ std::vector<std::string> nv;
+
+ bool
+ operator== (const object& y) const
+ {
+ return id == y.id && nv == y.nv;
+ }
+};
+
+#endif // TEST_HXX
diff --git a/odb-tests/sqlite/custom/testscript b/odb-tests/sqlite/custom/testscript
new file mode 100644
index 0000000..53561ae
--- /dev/null
+++ b/odb-tests/sqlite/custom/testscript
@@ -0,0 +1,9 @@
+# file : sqlite/custom/testscript
+# license : GNU GPL v2; see accompanying LICENSE file
+
+.include ../../database-options.testscript
+.include ../../sqlite.testscript
+
+: basics
+:
+$*