summaryrefslogtreecommitdiff
path: root/odb-tests/oracle/custom
diff options
context:
space:
mode:
Diffstat (limited to 'odb-tests/oracle/custom')
-rw-r--r--odb-tests/oracle/custom/buildfile36
-rw-r--r--odb-tests/oracle/custom/custom.sql53
-rw-r--r--odb-tests/oracle/custom/driver.cxx79
-rw-r--r--odb-tests/oracle/custom/test.hxx40
-rw-r--r--odb-tests/oracle/custom/testscript11
-rw-r--r--odb-tests/oracle/custom/traits.hxx76
6 files changed, 295 insertions, 0 deletions
diff --git a/odb-tests/oracle/custom/buildfile b/odb-tests/oracle/custom/buildfile
new file mode 100644
index 0000000..db901c3
--- /dev/null
+++ b/odb-tests/oracle/custom/buildfile
@@ -0,0 +1,36 @@
+# file : oracle/custom/buildfile
+# license : GNU GPL v2; see accompanying LICENSE file
+
+if ($build.meta_operation != 'dist')
+{
+ assert ($oracle) "oracle 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-oracle%lib{odb-oracle}
+import libs += lib{common}
+
+exe{driver}: {hxx cxx}{* -*-odb} {hxx ixx cxx}{test-odb} sql{custom} 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 oracle_custom_ \
+ --generate-schema \
+ --default-database common \
+ --generate-query \
+ --hxx-prologue '#include "traits.hxx"' \
+ --sql-interlude "@ $src_base/custom.sql"
+
+cxx.poptions =+ "-I$out_base" "-I$src_base"
diff --git a/odb-tests/oracle/custom/custom.sql b/odb-tests/oracle/custom/custom.sql
new file mode 100644
index 0000000..6e22903
--- /dev/null
+++ b/odb-tests/oracle/custom/custom.sql
@@ -0,0 +1,53 @@
+/* This file contains custom type definitions and helper functions.
+ */
+
+/* For some reason CREATE OR REPLACE TYPE does not work on Oracle 10.2. */
+BEGIN
+ BEGIN
+ EXECUTE IMMEDIATE 'DROP TYPE Numbers';
+ EXCEPTION
+ WHEN OTHERS THEN
+ IF SQLCODE != -4043 THEN RAISE; END IF;
+ END;
+END;
+/
+
+CREATE TYPE Numbers AS VARRAY(100) OF NUMBER(10);
+/
+
+CREATE OR REPLACE FUNCTION string_to_numbers(in_str IN VARCHAR2) RETURN Numbers
+IS
+ ret Numbers := Numbers();
+ s_pos NUMBER := 1;
+ e_pos NUMBER := 0;
+BEGIN
+ IF in_str IS NOT NULL THEN
+ LOOP
+ e_pos := INSTR(in_str, ',', s_pos);
+ EXIT WHEN e_pos = 0;
+ ret.extend;
+ ret(ret.COUNT) := CAST(SUBSTR(in_str, s_pos, e_pos - s_pos) AS NUMBER);
+ s_pos := e_pos + 1;
+ END LOOP;
+ ret.extend;
+ ret(ret.COUNT) := CAST(SUBSTR(in_str, s_pos) AS NUMBER);
+ END IF;
+ RETURN ret;
+END;
+/
+
+CREATE OR REPLACE FUNCTION numbers_to_string(in_nums IN Numbers) RETURN VARCHAR2
+IS
+ ret VARCHAR2(1500);
+BEGIN
+ IF in_nums.COUNT != 0 THEN
+ FOR i IN in_nums.FIRST .. in_nums.LAST LOOP
+ IF i != in_nums.FIRST THEN
+ ret := ret || ',';
+ END IF;
+ ret := ret || in_nums(i);
+ END LOOP;
+ END IF;
+ RETURN ret;
+END;
+/
diff --git a/odb-tests/oracle/custom/driver.cxx b/odb-tests/oracle/custom/driver.cxx
new file mode 100644
index 0000000..16439d1
--- /dev/null
+++ b/odb-tests/oracle/custom/driver.cxx
@@ -0,0 +1,79 @@
+// file : oracle/custom/driver.cxx
+// license : GNU GPL v2; see accompanying LICENSE file
+
+// Test custom database type mapping in Oracle.
+//
+
+#include <memory> // std::unique_ptr
+#include <iostream>
+
+#include <odb/oracle/database.hxx>
+#include <odb/oracle/transaction.hxx>
+
+#include <libcommon/common.hxx>
+
+#include "test.hxx"
+#include "test-odb.hxx"
+
+#undef NDEBUG
+#include <cassert>
+
+using namespace std;
+namespace oracle = odb::oracle;
+using namespace oracle;
+
+int
+main (int argc, char* argv[])
+{
+ try
+ {
+ unique_ptr<database> db (create_specific_database<database> (argc, argv));
+
+ object o (1);
+ o.iv.push_back (123);
+ o.iv.push_back (234);
+ o.iv.push_back (-345);
+
+ // 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.iv[0]++;
+ o.iv.pop_back ();
+
+ {
+ 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/oracle/custom/test.hxx b/odb-tests/oracle/custom/test.hxx
new file mode 100644
index 0000000..523d50b
--- /dev/null
+++ b/odb-tests/oracle/custom/test.hxx
@@ -0,0 +1,40 @@
+// file : oracle/custom/test.hxx
+// license : GNU GPL v2; see accompanying LICENSE file
+
+#ifndef TEST_HXX
+#define TEST_HXX
+
+#include <vector>
+
+#include <odb/core.hxx>
+
+// Map Numbers VARRAY Oracle type to std::vector<int>. This type is defined
+// in the custom.sql file along with two helper functions that convert
+// between Numbers and its string representation. The other half of this
+// mapping is in traits.hxx (value_traits<std::vector<int>, id_string>).
+//
+#pragma db map type("Numbers") \
+ as("VARCHAR2(1500)") \
+ to("CAST(string_to_numbers((?)) AS Numbers)") \
+ from("numbers_to_string((?))")
+
+#pragma db object
+struct object
+{
+ object () {}
+ object (unsigned long id_) : id (id_) {}
+
+ #pragma db id
+ unsigned long id;
+
+ #pragma db type("Numbers")
+ std::vector<int> iv;
+
+ bool
+ operator== (const object& y) const
+ {
+ return id == y.id && iv == y.iv;
+ }
+};
+
+#endif // TEST_HXX
diff --git a/odb-tests/oracle/custom/testscript b/odb-tests/oracle/custom/testscript
new file mode 100644
index 0000000..e39b328
--- /dev/null
+++ b/odb-tests/oracle/custom/testscript
@@ -0,0 +1,11 @@
+# file : oracle/custom/testscript
+# license : GNU GPL v2; see accompanying LICENSE file
+
+.include ../../database-options.testscript
+.include ../../oracle.testscript
+
++$create_schema
+
+: basics
+:
+$*
diff --git a/odb-tests/oracle/custom/traits.hxx b/odb-tests/oracle/custom/traits.hxx
new file mode 100644
index 0000000..8df2f91
--- /dev/null
+++ b/odb-tests/oracle/custom/traits.hxx
@@ -0,0 +1,76 @@
+// file : oracle/types/traits.hxx
+// license : GNU GPL v2; see accompanying LICENSE file
+
+#ifndef TRAITS_HXX
+#define TRAITS_HXX
+
+#include <vector>
+#include <sstream>
+#include <cstring> // std::memcpy
+#include <cassert> // std::memcpy
+
+#include <odb/oracle/traits.hxx>
+
+namespace odb
+{
+ namespace oracle
+ {
+
+ template <>
+ class value_traits<std::vector<int>, id_string>
+ {
+ public:
+ typedef std::vector<int> value_type;
+ typedef value_type query_type;
+ typedef details::buffer image_type;
+
+ static void
+ set_value (value_type& v,
+ const char* b,
+ std::size_t n,
+ bool is_null)
+ {
+ v.clear ();
+
+ if (!is_null)
+ {
+ // Array format is "n1,n2,n3...".
+ //
+ std::istringstream is (std::string (b, n));
+
+ for (char c; !is.eof (); is >> c)
+ {
+ v.push_back (int ());
+ is >> v.back ();
+ }
+ }
+ }
+
+ static void
+ set_image (char* b,
+ std::size_t c,
+ std::size_t& n,
+ bool& is_null,
+ const value_type& v)
+ {
+ is_null = false;
+ std::ostringstream os;
+
+ for (value_type::const_iterator i (v.begin ()), e (v.end ()); i != e;)
+ {
+ os << *i;
+
+ if (++i != e)
+ os << ',';
+ }
+
+ const std::string& s (os.str ());
+ n = s.size ();
+ assert (n <= c);
+ std::memcpy (b, s.c_str (), n);
+ }
+ };
+ }
+}
+
+#endif // TRAITS_HXX