1
0
Fork 0
mirror of https://github.com/KingDuckZ/dindexer.git synced 2024-11-29 01:33:46 +00:00

Now there is a cpp for pq_type_helpers.hpp so move code there.

This commit is contained in:
King_DuckZ 2016-01-07 14:52:32 +00:00
parent 2655ea5f5c
commit 2593d46ed7
3 changed files with 38 additions and 36 deletions

View file

@ -62,6 +62,10 @@ namespace pq {
template <>
struct get_pqlib_c_type_struct<std::chrono::system_clock::time_point> {
//Hack to make some sort of "static pimpl"
//I don't want to include libpqtypes here since this is a public
//header, so the following should mimic the real struct in that they
//should have the same size and alignment. This is asserted in the
//cpp.
struct StorageStruct { uint64_t epoch; int a[14]; char tzabbr[16]; };
static constexpr std::size_t DATA_SIZE = sizeof(StorageStruct);
using storage = std::aligned_storage<DATA_SIZE, alignof(uint64_t)>::type;

View file

@ -31,42 +31,6 @@
#include <cassert>
namespace pq {
namespace implem {
auto get_pqlib_c_type_struct<std::chrono::system_clock::time_point>::conv (const std::chrono::system_clock::time_point& parParam) -> type {
static_assert(sizeof(storage) == sizeof(PGtimestamp), "Wrong size for timestamp, please update DATA_SIZE");
static_assert(alignof(storage) == alignof(PGtimestamp), "Wrong alignment for timestamp, please update type");
using std::chrono::system_clock;
PGtimestamp ts;
std::memset(&ts, 0, sizeof(PGtimestamp));
auto t = system_clock::to_time_t(parParam);
ts.epoch = t;
auto tm = std::localtime(&t);
ts.time.hour = tm->tm_hour;
ts.time.min = tm->tm_min;
ts.time.sec = tm->tm_sec;
ts.time.usec = 0;
ts.time.withtz = 1;
ts.date.isbc = 0;
ts.date.year = tm->tm_year + 1900;
ts.date.mon = tm->tm_mon;
ts.date.mday = tm->tm_mday;
char* tzn;
PQlocalTZInfo(&t, &ts.time.gmtoff, &ts.time.isdst, &tzn);
std::strcpy(ts.time.tzabbr, tzn);
std::copy(reinterpret_cast<const char*>(&ts), reinterpret_cast<const char*>(&ts) + sizeof(ts), reinterpret_cast<char*>(&m_storage));
return &m_storage;
}
get_pqlib_c_type_struct<std::chrono::system_clock::time_point>::~get_pqlib_c_type_struct ( void ) noexcept {
return;
}
} //namespace implem
namespace {
int call_PQputf (PGparam* parParam, const std::string* parTypes, va_list parArgp) {
return PQputvf(parParam, nullptr, 0, parTypes->c_str(), parArgp);

View file

@ -27,6 +27,40 @@
namespace pq {
namespace implem {
auto get_pqlib_c_type_struct<std::chrono::system_clock::time_point>::conv (const std::chrono::system_clock::time_point& parParam) -> type {
static_assert(sizeof(storage) == sizeof(PGtimestamp), "Wrong size for timestamp, please update DATA_SIZE");
static_assert(alignof(storage) == alignof(PGtimestamp), "Wrong alignment for timestamp, please update type");
using std::chrono::system_clock;
PGtimestamp ts;
std::memset(&ts, 0, sizeof(PGtimestamp));
auto t = system_clock::to_time_t(parParam);
ts.epoch = t;
auto tm = std::localtime(&t);
ts.time.hour = tm->tm_hour;
ts.time.min = tm->tm_min;
ts.time.sec = tm->tm_sec;
ts.time.usec = 0;
ts.time.withtz = 1;
ts.date.isbc = 0;
ts.date.year = tm->tm_year + 1900;
ts.date.mon = tm->tm_mon;
ts.date.mday = tm->tm_mday;
char* tzn;
PQlocalTZInfo(&t, &ts.time.gmtoff, &ts.time.isdst, &tzn);
std::strcpy(ts.time.tzabbr, tzn);
std::copy(reinterpret_cast<const char*>(&ts), reinterpret_cast<const char*>(&ts) + sizeof(ts), reinterpret_cast<char*>(&m_storage));
return &m_storage;
}
get_pqlib_c_type_struct<std::chrono::system_clock::time_point>::~get_pqlib_c_type_struct ( void ) noexcept {
return;
}
get_pqlib_c_type_struct_arr::get_pqlib_c_type_struct_arr (const Connection& parConn) :
m_par(parConn.make_empty_params())
{