diff options
-rw-r--r-- | libpqpp/pq-binarycolumn.cpp | 19 | ||||
-rw-r--r-- | libpqpp/pq-binarycolumn.h | 3 | ||||
-rw-r--r-- | libpqpp/pq-column.h | 9 |
3 files changed, 19 insertions, 12 deletions
diff --git a/libpqpp/pq-binarycolumn.cpp b/libpqpp/pq-binarycolumn.cpp index 514a145..761bf0b 100644 --- a/libpqpp/pq-binarycolumn.cpp +++ b/libpqpp/pq-binarycolumn.cpp @@ -2,6 +2,7 @@ #include "column.h" #include "dbTypes.h" #include "pq-column.h" +#include <bit> #include <cstdint> #include <endian.h> #include <error.h> @@ -9,6 +10,18 @@ PQ::BinaryColumn::BinaryColumn(const PQ::SelectBase * s, unsigned int f) : PQ::Column(s, f) { } +template<std::integral T> +inline T +PQ::BinaryColumn::valueAs() const +{ + T v {}; + std::memcpy(&v, value(), sizeof(T)); + if constexpr (std::endian::native != std::endian::big && sizeof(T) > 1) { + return std::byteswap(v); + } + return v; +} + void PQ::BinaryColumn::apply(DB::HandleField & h) const { @@ -27,13 +40,13 @@ PQ::BinaryColumn::apply(DB::HandleField & h) const h.boolean(valueAs<bool>()); break; case INT2OID: - h.integer(static_cast<int64_t>(be16toh(valueAs<uint16_t>()))); + h.integer(valueAs<int16_t>()); break; case INT4OID: - h.integer(static_cast<int64_t>(be32toh(valueAs<uint32_t>()))); + h.integer(valueAs<int32_t>()); break; case INT8OID: - h.integer(static_cast<int64_t>(be64toh(valueAs<uint64_t>()))); + h.integer(valueAs<int64_t>()); break; case BYTEAOID: h.blob(DB::Blob(value(), length())); diff --git a/libpqpp/pq-binarycolumn.h b/libpqpp/pq-binarycolumn.h index e2a8044..e44d7e2 100644 --- a/libpqpp/pq-binarycolumn.h +++ b/libpqpp/pq-binarycolumn.h @@ -15,6 +15,9 @@ namespace PQ { BinaryColumn(const SelectBase *, unsigned int field); void apply(DB::HandleField &) const override; + + private: + template<std::integral T> [[nodiscard]] inline T valueAs() const; }; } diff --git a/libpqpp/pq-column.h b/libpqpp/pq-column.h index 6bf2669..fa4a78a 100644 --- a/libpqpp/pq-column.h +++ b/libpqpp/pq-column.h @@ -17,15 +17,6 @@ namespace PQ { void apply(DB::HandleField &) const override; protected: - template<typename T> - inline T - valueAs() const - { - T v {}; - std::memcpy(&v, value(), sizeof(T)); - return v; - } - const char * value() const; std::size_t length() const; |