summaryrefslogtreecommitdiff
path: root/odb-tests/pgsql/types
diff options
context:
space:
mode:
Diffstat (limited to 'odb-tests/pgsql/types')
-rw-r--r--odb-tests/pgsql/types/buildfile36
-rw-r--r--odb-tests/pgsql/types/driver.cxx165
-rw-r--r--odb-tests/pgsql/types/test.hxx220
-rw-r--r--odb-tests/pgsql/types/testscript11
-rw-r--r--odb-tests/pgsql/types/traits.hxx171
5 files changed, 603 insertions, 0 deletions
diff --git a/odb-tests/pgsql/types/buildfile b/odb-tests/pgsql/types/buildfile
new file mode 100644
index 0000000..64f0c02
--- /dev/null
+++ b/odb-tests/pgsql/types/buildfile
@@ -0,0 +1,36 @@
+# file : pgsql/types/buildfile
+# license : GNU GPL v2; see accompanying LICENSE file
+
+assert ($pgsql && !$multi || $build.meta_operation == 'dist') \
+"pgsql should be configured via config.odb_tests.database variable as a single database"
+
+import libodb = libodb%lib{odb}
+import libcommon = lib{common}
+import libs = libodb-pgsql%lib{odb-pgsql}
+
+exe{driver}: {hxx cxx}{* -*-odb} {hxx ixx cxx}{test-odb} testscript
+
+# Introduce the metadata library target to make sure the libodb and libcommon
+# libraries are resolved for the odb_compile ad hoc rule (see build/root.build
+# for details).
+#
+libue{test-meta}: $libodb $libcommon
+
+<{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 pgsql_types_ \
+ --generate-schema \
+ --default-database common \
+ --generate-query \
+ --cxx-prologue '#include "traits.hxx"'
+
+cxx.poptions =+ "-I$out_base" "-I$src_base"
+
+# Testscript's run-time prerequisites.
+#
+exe{driver}: ../../alias{pgsql-client}: include = adhoc
diff --git a/odb-tests/pgsql/types/driver.cxx b/odb-tests/pgsql/types/driver.cxx
new file mode 100644
index 0000000..710f601
--- /dev/null
+++ b/odb-tests/pgsql/types/driver.cxx
@@ -0,0 +1,165 @@
+// file : pgsql/types/driver.cxx
+// license : GNU GPL v2; see accompanying LICENSE file
+
+// Test PostgreSQL type conversion.
+//
+
+#include <memory> // std::unique_ptr
+#include <iostream>
+
+#include <odb/pgsql/database.hxx>
+#include <odb/pgsql/transaction.hxx>
+
+#include <libcommon/common.hxx>
+
+#include "test.hxx"
+#include "test-odb.hxx"
+
+#undef NDEBUG
+#include <cassert>
+
+using namespace std;
+namespace pgsql = odb::pgsql;
+using namespace pgsql;
+
+int
+main (int argc, char* argv[])
+{
+ try
+ {
+ unique_ptr<database> db (create_specific_database<database> (argc, argv));
+
+ object o (1);
+
+ o.bool_ = true;
+ o.short_ = 12345;
+ o.int_ = -123456;
+ o.long_long_ = 123456;
+
+ o.float_ = 1.123F;
+ o.float8_ = 1.123;
+ o.double_ = 1.123;
+
+ o.date_ = 4015;
+ o.time_ = 48180000000LL;
+ o.timestamp_ = 346896000LL;
+
+ string short_str (128, 's');
+ string medium_str (250, 'm');
+ string long_str (2040, 'l');
+
+ o.char_ = short_str;
+ o.varchar_ = medium_str;
+ o.text_ = long_str;
+
+ o.bytea_.assign (long_str.c_str (), long_str.c_str () + long_str.size ());
+
+ unsigned char varbit_buf[8] = {1, 3, 1, 3, 1, 3, 1, 3};
+ o.varbit_.size = 52;
+ o.varbit_.ubuffer_ = ubuffer (varbit_buf, 8);
+
+ o.bit_.a = 0;
+ o.bit_.b = 1;
+ o.bit_.c = 0;
+ o.bit_.d = 1;
+
+ // 6F846D41-C89A-4E4D-B22F-56443CFA543F
+ memcpy (o.uuid_, "\x6F\x84\x6D\x41\xC8\x9A\x4E\x4D\xB2\x2F"
+ "\x56\x44\x3C\xFA\x54\x3F", 16);
+
+ o.enum_ = green;
+
+ // 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);
+ }
+
+ typedef pgsql::query<object> query;
+ typedef odb::result<object> result;
+
+ // Test UUID in queries.
+ //
+ {
+ char uuid[16];
+ memcpy (uuid, o.uuid_, 16);
+
+ transaction t (db->begin ());
+
+ {
+ result r (db->query<object> (query::uuid == uuid));
+ assert (size (r) == 1);
+ }
+
+ {
+ result r (db->query<object> (query::uuid == query::_val (uuid)));
+ assert (size (r) == 1);
+ }
+
+ {
+ result r (db->query<object> (query::uuid == query::_ref (uuid)));
+ assert (size (r) == 1);
+ }
+
+ {
+ const char* d (uuid);
+ result r (db->query<object> (query::uuid == d));
+ assert (size (r) == 1);
+ }
+
+ 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 ();
+ }
+
+ // PostgreSQL returns padded values for CHAR(N).
+ //
+ memcpy (o1.s2, " ", 16);
+ o1.s3[0] = o1.c1 = ' ';
+ memcpy (o2.s2, "1234567890 ", 16);
+
+ {
+ transaction t (db->begin ());
+ unique_ptr<char_array> p1 (db->load<char_array> (1));
+ unique_ptr<char_array> p2 (db->load<char_array> (2));
+ unique_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)
+ {
+ cerr << e.what () << endl;
+ return 1;
+ }
+}
diff --git a/odb-tests/pgsql/types/test.hxx b/odb-tests/pgsql/types/test.hxx
new file mode 100644
index 0000000..462ebad
--- /dev/null
+++ b/odb-tests/pgsql/types/test.hxx
@@ -0,0 +1,220 @@
+// file : pgsql/types/test.hxx
+// license : GNU GPL v2; see accompanying LICENSE file
+
+#ifndef TEST_HXX
+#define TEST_HXX
+
+#include <set>
+#include <string>
+#include <vector>
+#include <memory> // std::unique_ptr
+#include <cstring> // std::memcmp, std::memcpy, std::str[n]cmp, std::strlen
+#include <cstddef> // std::size_t
+
+#include <odb/core.hxx>
+
+#include <libcommon/buffer.hxx>
+
+struct bitfield
+{
+ unsigned int a: 1;
+ unsigned int b: 1;
+ unsigned int c: 1;
+ unsigned int d: 1;
+};
+
+inline bool
+operator== (bitfield x, bitfield y)
+{
+ return
+ x.a == y.a &&
+ x.b == y.b &&
+ x.c == y.c &&
+ x.d == y.d;
+}
+
+struct varbit
+{
+ std::size_t size;
+ ubuffer ubuffer_;
+
+ bool
+ compare (const varbit& x) const
+ {
+ if (size != x.size)
+ return false;
+
+ std::size_t byte_len = size / 8;
+
+ if (std::memcmp (ubuffer_.data (), x.ubuffer_.data (), byte_len) != 0)
+ return false;
+
+ std::size_t trailing_bits = size % 8;
+
+ if (trailing_bits != 0)
+ {
+ unsigned char mask (0xFFU << (8 - trailing_bits));
+
+ return (ubuffer_.data ()[byte_len] & mask) ==
+ (x.ubuffer_.data ()[byte_len] & mask);
+ }
+
+ return true;
+ }
+};
+
+inline bool
+operator== (const varbit& x, const varbit& y)
+{
+ return x.compare (y);
+}
+
+#pragma db value(bitfield) type ("BIT(4)")
+
+typedef std::unique_ptr<std::string> string_ptr;
+
+enum color {red, green, blue};
+
+#pragma db object
+struct object
+{
+ object () {}
+ object (unsigned long id): id_ (id) {}
+
+ #pragma db id
+ unsigned long id_;
+
+ // Integral types.
+ //
+ #pragma db type ("BOOL")
+ bool bool_;
+
+ #pragma db type ("SMALLINT")
+ short short_;
+
+ #pragma db type ("INT")
+ int int_;
+
+ #pragma db type ("BIGINT")
+ long long long_long_;
+
+ // Float types.
+ //
+ #pragma db type ("REAL")
+ float float_;
+
+ #pragma db type ("FLOAT(32)")
+ double float8_;
+
+ #pragma db type ("DOUBLE PRECISION")
+ double double_;
+
+ // Data-time types.
+ //
+ #pragma db type ("DATE")
+ int date_;
+
+ #pragma db type ("TIME")
+ long long time_;
+
+ #pragma db type ("TIMESTAMP")
+ long long timestamp_;
+
+ // String and binary types.
+ //
+ #pragma db type ("CHAR(128)")
+ std::string char_;
+
+ #pragma db type ("VARCHAR(256)")
+ std::string varchar_;
+
+ #pragma db type ("TEXT")
+ std::string text_;
+
+ #pragma db type ("BYTEA")
+ std::vector<char> bytea_;
+
+ #pragma db type ("VARBIT(1024)")
+ varbit varbit_;
+
+ // #pragma db type ("BIT(4)") - assigned by #pragma db value
+ bitfield bit_;
+
+ // Other types.
+ //
+ #pragma db type ("UUID")
+ char uuid_[16];
+
+ // Test ENUM representation.
+ //
+ color enum_;
+
+ // Test NULL value.
+ //
+ #pragma db type ("TEXT") null
+ string_ptr null_;
+
+ bool
+ operator== (const object& y) const
+ {
+ return
+ id_ == y.id_ &&
+ bool_ == y.bool_ &&
+ short_ == y.short_ &&
+ int_ == y.int_ &&
+ long_long_ == y.long_long_ &&
+ float_ == y.float_ &&
+ float8_ == y.float8_ &&
+ double_ == y.double_ &&
+ date_ == y.date_ &&
+ time_ == y.time_ &&
+ timestamp_ == y.timestamp_ &&
+ char_ == y.char_ &&
+ varchar_ == y.varchar_ &&
+ text_ == y.text_ &&
+ bytea_ == y.bytea_ &&
+ bit_ == y.bit_ &&
+ varbit_ == y.varbit_ &&
+ memcmp (uuid_, y.uuid_, 16) == 0 &&
+ enum_ == y.enum_ &&
+ ((null_.get () == 0 && y.null_.get () == 0) || *null_ == *y.null_);
+ }
+};
+
+// Test char array.
+//
+#pragma db object
+struct char_array
+{
+ char_array () {}
+ char_array (unsigned long id, const char* s)
+ : id_ (id)
+ {
+ std::memcpy (s1, s, std::strlen (s) + 1); // VC++ strncpy deprecation.
+ std::memcpy (s2, s, std::strlen (s) + 1);
+ s3[0] = c1 = *s;
+ }
+
+ #pragma db id
+ unsigned long id_;
+
+ char s1[17];
+
+ #pragma db type("CHAR(16)")
+ char s2[16];
+
+ char s3[1];
+ 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
diff --git a/odb-tests/pgsql/types/testscript b/odb-tests/pgsql/types/testscript
new file mode 100644
index 0000000..57218e1
--- /dev/null
+++ b/odb-tests/pgsql/types/testscript
@@ -0,0 +1,11 @@
+# file : pgsql/types/testscript
+# license : GNU GPL v2; see accompanying LICENSE file
+
+.include ../../database-options.testscript
+.include ../../pgsql.testscript
+
++$create_schema
+
+: basics
+:
+$*
diff --git a/odb-tests/pgsql/types/traits.hxx b/odb-tests/pgsql/types/traits.hxx
new file mode 100644
index 0000000..a1c8fbe
--- /dev/null
+++ b/odb-tests/pgsql/types/traits.hxx
@@ -0,0 +1,171 @@
+// file : pgsql/types/traits.hxx
+// license : GNU GPL v2; see accompanying LICENSE file
+
+#ifndef TRAITS_HXX
+#define TRAITS_HXX
+
+#include <cassert>
+#include <cstring> // std::memcpy, std::memset
+
+#include <odb/pgsql/traits.hxx>
+#include <odb/pgsql/details/endian-traits.hxx>
+
+#include "test.hxx" // varbit, ubuffer, string_ptr
+
+namespace odb
+{
+ namespace pgsql
+ {
+ // The first 4 bytes of the image is a signed int specifying the
+ // number of significant bits contained by the BIT. The following
+ // bytes contain the bit data.
+ //
+ template <>
+ class value_traits<bitfield, id_bit>
+ {
+ public:
+ typedef bitfield value_type;
+ typedef bitfield query_type;
+ typedef unsigned char* image_type;
+
+ static void
+ set_value (bitfield& v,
+ const unsigned char* i,
+ std::size_t,
+ bool is_null)
+ {
+ if (!is_null)
+ {
+ assert (
+ details::endian_traits::ntoh (
+ *reinterpret_cast<const int*> (i)) == 4);
+
+ const unsigned char* d (i + 4);
+
+ v.a = *d >> 4 & 1;
+ v.b = (*d >> 5) & 1;
+ v.c = (*d >> 6) & 1;
+ v.d = (*d >> 7) & 1;
+ }
+ else
+ v.a = v.b = v.c = v.d = 0;
+ }
+
+ static void
+ set_image (unsigned char* i,
+ std::size_t,
+ std::size_t& n,
+ bool& is_null,
+ bitfield v)
+ {
+ is_null = false;
+ n = 5;
+
+ *reinterpret_cast<int*> (i) = details::endian_traits::hton (4);
+ *(i + 4) = v.a << 4 | (v.b << 5) | (v.c << 6) | (v.d << 7);
+ }
+ };
+
+ // The first 4 bytes of the image is a signed int specifying the
+ // number of significant bits contained by the VARBIT. The following
+ // bytes contain the VARBIT data.
+ //
+ template <>
+ class value_traits<varbit, id_varbit>
+ {
+ public:
+ typedef varbit value_type;
+ typedef varbit query_type;
+ typedef details::ubuffer image_type;
+
+ static void
+ set_value (varbit& v,
+ const details::ubuffer& b,
+ std::size_t n,
+ bool is_null)
+ {
+ if (!is_null)
+ {
+ v.size = static_cast<std::size_t> (
+ details::endian_traits::ntoh (
+ *reinterpret_cast<const int*> (b.data ())));
+
+ std::size_t byte_len (v.size / 8 + (v.size % 8 > 0 ? 1 : 0));
+ assert (n >= byte_len + 4);
+
+ v.ubuffer_.assign (b.data () + 4, byte_len);
+ }
+
+ else
+ {
+ v.size = 0;
+ v.ubuffer_.assign (0, 0);
+ }
+ }
+
+ static void
+ set_image (details::ubuffer& b,
+ std::size_t& n,
+ bool& is_null,
+ const varbit& v)
+ {
+ is_null = false;
+ n = 4 + v.size / 8 + (v.size % 8 > 0 ? 1 : 0);
+
+ if (n > b.capacity ())
+ b.capacity (n);
+
+ // PostgreSQL requires all trailing bits of a VARBIT image
+ // to be zero.
+ //
+ std::memset (b.data (), 0, b.capacity ());
+
+ *reinterpret_cast<int*> (b.data ()) =
+ details::endian_traits::hton (static_cast<int> (v.size));
+
+ if (v.size != 0)
+ std::memcpy (b.data () + 4, v.ubuffer_.data (), n - 4);
+ }
+ };
+
+ template <>
+ class value_traits<string_ptr, id_string>
+ {
+ public:
+ typedef string_ptr value_type;
+ typedef std::string query_type;
+ typedef details::buffer image_type;
+
+ static void
+ set_value (string_ptr& v,
+ const details::buffer& b,
+ std::size_t n,
+ bool is_null)
+ {
+ v.reset (is_null ? 0 : new std::string (b.data (), n));
+ }
+
+ static void
+ set_image (details::buffer& b,
+ std::size_t& n,
+ bool& is_null,
+ const string_ptr& v)
+ {
+ is_null = v.get () == 0;
+
+ if (!is_null)
+ {
+ n = v->size ();
+
+ if (n > b.capacity ())
+ b.capacity (n);
+
+ if (n != 0)
+ std::memcpy (b.data (), v->c_str (), n);
+ }
+ }
+ };
+ }
+}
+
+#endif // TRAITS_HXX