mirror of
https://github.com/KingDuckZ/dindexer.git
synced 2025-08-08 13:49:47 +00:00
Now there is a cpp for pq_type_helpers.hpp so move code there.
This commit is contained in:
parent
2655ea5f5c
commit
2593d46ed7
3 changed files with 38 additions and 36 deletions
|
@ -62,6 +62,10 @@ namespace pq {
|
||||||
template <>
|
template <>
|
||||||
struct get_pqlib_c_type_struct<std::chrono::system_clock::time_point> {
|
struct get_pqlib_c_type_struct<std::chrono::system_clock::time_point> {
|
||||||
//Hack to make some sort of "static pimpl"
|
//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]; };
|
struct StorageStruct { uint64_t epoch; int a[14]; char tzabbr[16]; };
|
||||||
static constexpr std::size_t DATA_SIZE = sizeof(StorageStruct);
|
static constexpr std::size_t DATA_SIZE = sizeof(StorageStruct);
|
||||||
using storage = std::aligned_storage<DATA_SIZE, alignof(uint64_t)>::type;
|
using storage = std::aligned_storage<DATA_SIZE, alignof(uint64_t)>::type;
|
||||||
|
|
|
@ -31,42 +31,6 @@
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
|
||||||
namespace pq {
|
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 {
|
namespace {
|
||||||
int call_PQputf (PGparam* parParam, const std::string* parTypes, va_list parArgp) {
|
int call_PQputf (PGparam* parParam, const std::string* parTypes, va_list parArgp) {
|
||||||
return PQputvf(parParam, nullptr, 0, parTypes->c_str(), parArgp);
|
return PQputvf(parParam, nullptr, 0, parTypes->c_str(), parArgp);
|
||||||
|
|
|
@ -27,6 +27,40 @@
|
||||||
|
|
||||||
namespace pq {
|
namespace pq {
|
||||||
namespace implem {
|
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) :
|
get_pqlib_c_type_struct_arr::get_pqlib_c_type_struct_arr (const Connection& parConn) :
|
||||||
m_par(parConn.make_empty_params())
|
m_par(parConn.make_empty_params())
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue