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-25 06:38:00 +0200
commit6323bca38688f0f5be72481fb146313d2fc5fe78 (patch)
tree608bd4a9b9a7159353818697964bffa1be4c911a
parent1ca674f0dda6ea6f1204f0c06e22e165655927c3 (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: