aboutsummaryrefslogtreecommitdiff
path: root/oracle
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2013-01-24 15:10:22 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2013-01-24 15:10:22 +0200
commit89de275db2b77d0abf9fa1ec066ef11e262c88af (patch)
tree06cb2272c5e791946dc58421ed3f2d03b7c8122e /oracle
parent539c92147e8d58c49f350c4070051a6ddf6b2354 (diff)
Add support for mapping char[N] to CHAR/VARCHAR database types
Also improve query support for arrays (decaying).
Diffstat (limited to 'oracle')
-rw-r--r--oracle/types/driver.cxx33
-rw-r--r--oracle/types/test.hxx54
2 files changed, 77 insertions, 10 deletions
diff --git a/oracle/types/driver.cxx b/oracle/types/driver.cxx
index be6251c..6370e44 100644
--- a/oracle/types/driver.cxx
+++ b/oracle/types/driver.cxx
@@ -272,6 +272,39 @@ main (int argc, char* argv[])
t.commit ();
}
+
+ // Test char array.
+ //
+ {
+ char_array o1 (1, "");
+ char_array o2 (2, "1234567890");
+ char_array o3 (3, "1234567890123456");
+
+ {
+ transaction t (db->begin ());
+ db->persist (o1);
+ db->persist (o2);
+ db->persist (o3);
+ t.commit ();
+ }
+
+ // Oracle returns padded values for CHAR(N) unless they are
+ // empty (represented as NULL).
+ //
+ memcpy (o2.s2, "1234567890 ", 16);
+
+ {
+ transaction t (db->begin ());
+ auto_ptr<char_array> p1 (db->load<char_array> (1));
+ auto_ptr<char_array> p2 (db->load<char_array> (2));
+ auto_ptr<char_array> p3 (db->load<char_array> (3));
+ t.commit ();
+
+ assert (o1 == *p1);
+ assert (o2 == *p2);
+ assert (o3 == *p3);
+ }
+ }
}
catch (const odb::exception& e)
{
diff --git a/oracle/types/test.hxx b/oracle/types/test.hxx
index 0543598..51665c9 100644
--- a/oracle/types/test.hxx
+++ b/oracle/types/test.hxx
@@ -8,6 +8,7 @@
#include <string>
#include <vector>
#include <memory> // std::auto_ptr
+#include <cstring> // std::strncpy, std::str[n]cmp
#include <odb/core.hxx>
@@ -101,14 +102,8 @@ typedef std::vector<std::string> strings;
#pragma db object
struct object
{
- object (unsigned int id)
- : id_ (id)
- {
- }
-
- object ()
- {
- }
+ object () {}
+ object (unsigned long id): id_ (id) {}
#pragma db id
unsigned int id_;
@@ -166,13 +161,13 @@ struct object
#pragma db type ("CHAR(13)")
std::string char_;
- #pragma db type ("VARCHAR2(512)")
+ #pragma db type ("VARCHAR2(512)") null
std::string varchar2_;
#pragma db type ("NCHAR(8)")
std::string nchar_;
- #pragma db type ("NVARCHAR2(512)")
+ #pragma db type ("NVARCHAR2(512)") null
std::string nvarchar2_;
// Oracle treats empty and NULL VARCHAR2 the same. Test that we
@@ -302,4 +297,43 @@ struct blob
}
};
+// Test char array.
+//
+#pragma db object
+struct char_array
+{
+ char_array () {}
+ char_array (unsigned long id, const char* s)
+ : id_ (id)
+ {
+ std::strncpy (s1, s, sizeof (s1));
+ std::strncpy (s2, s, sizeof (s2));
+ s3[0] = c1 = *s;
+ }
+
+ #pragma db id
+ unsigned long id_;
+
+ char s1[17];
+
+ #pragma db type("CHAR(16)") null
+ char s2[16];
+
+ #pragma db null
+ char s3[1];
+
+ #pragma db null
+ char c1;
+
+ bool
+ operator== (const char_array& y) const
+ {
+ return id_ == y.id_ &&
+ std::strcmp (s1, y.s1) == 0 &&
+ std::strncmp (s2, y.s2, sizeof (s2)) == 0 &&
+ s3[0] == y.s3[0] &&
+ c1 == y.c1;
+ }
+};
+
#endif // TEST_HXX