aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2014-11-17 09:05:00 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2014-11-17 09:05:00 +0200
commit04372ef4d8ef500a4d5b83ee1e6e19d19820c6b8 (patch)
treee2158f3df389e6361a8acc3c38692104552cd7a8
parent521596e38974a68a60dbc8eb083cc881bb11914f (diff)
Handle conversion between different integer widths in result extraction
This will help with custom schemas where the integer types in the database may not necessarily match the ODB's default mappings.
-rw-r--r--odb/pgsql/statement.cxx66
1 files changed, 55 insertions, 11 deletions
diff --git a/odb/pgsql/statement.cxx b/odb/pgsql/statement.cxx
index cf3e181..895cff5 100644
--- a/odb/pgsql/statement.cxx
+++ b/odb/pgsql/statement.cxx
@@ -360,20 +360,64 @@ namespace odb
break;
}
case bind::smallint:
- {
- *static_cast<short*> (b.buffer) =
- *reinterpret_cast<const short*> (v);
- break;
- }
case bind::integer:
- {
- *static_cast<int*> (b.buffer) = *reinterpret_cast<const int*> (v);
- break;
- }
case bind::bigint:
{
- *static_cast<long long*> (b.buffer) =
- *reinterpret_cast<const long long*> (v);
+ // If we are dealing with a custom schema, we may have a
+ // difference in the integer widths. Note also that we have
+ // to go to our endianness and back in order for casts to
+ // work properly.
+ //
+ long long i;
+
+ switch (PQftype (result, c))
+ {
+ case int2_oid:
+ {
+ i = endian_traits::ntoh (*reinterpret_cast<const short*> (v));
+ break;
+ }
+ case int4_oid:
+ {
+ i = endian_traits::ntoh (*reinterpret_cast<const int*> (v));
+ break;
+ }
+ case int8_oid:
+ {
+ i = endian_traits::ntoh (
+ *reinterpret_cast<const long long*> (v));
+ break;
+ }
+ default:
+ {
+ assert (false); // Column in the database is not an integer.
+ break;
+ }
+ }
+
+ switch (b.type)
+ {
+ case bind::smallint:
+ {
+ *static_cast<short*> (b.buffer) =
+ endian_traits::hton (static_cast<short> (i));
+ break;
+ }
+ case bind::integer:
+ {
+ *static_cast<int*> (b.buffer) =
+ endian_traits::hton (static_cast<int> (i));
+ break;
+ }
+ case bind::bigint:
+ {
+ *static_cast<long long*> (b.buffer) = endian_traits::hton (i);
+ break;
+ }
+ default:
+ break;
+ }
+
break;
}
case bind::real: