1
0
Fork 0
mirror of https://github.com/KingDuckZ/dindexer.git synced 2024-11-25 00:53:43 +00:00

Move libpqtypes template helpers out to a new header.

This commit is contained in:
King_DuckZ 2015-12-31 01:20:56 +00:00
parent ed0fbac5e1
commit c4a2433a04
3 changed files with 85 additions and 73 deletions

View file

@ -19,12 +19,11 @@
#define id68BF7AAC5BD54AF2BBD6EC1318B6687E
#include "pq/resultset.hpp"
#include "pq/implem/pq_type_helpers.hpp"
#include <string>
#include <cstdint>
#include <memory>
#include <boost/utility/string_ref.hpp>
#include <chrono>
#include <type_traits>
struct pg_param;
typedef pg_param PGparam;
@ -62,50 +61,6 @@ namespace pq {
std::unique_ptr<LocalData> m_localData;
};
namespace implem {
template <typename T>
const char* type_to_pqtypes_name ( void );
template <typename T>
struct get_pqlib_c_type_struct {
using type = T;
static type conv ( T parParam ) { return parParam; }
};
template <>
struct get_pqlib_c_type_struct<std::string> {
using type = const char*;
static type conv ( const std::string& parParam ) { return parParam.c_str(); }
};
template <>
struct get_pqlib_c_type_struct<boost::string_ref> {
using type = const char*;
static type conv ( const boost::string_ref& parParam ) { return parParam.data(); }
};
template <>
struct get_pqlib_c_type_struct<bool> {
using type = int;
static type conv ( bool parParam ) { return (parParam ? 1 : 0); }
};
template <>
struct get_pqlib_c_type_struct<std::chrono::system_clock::time_point> {
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;
storage m_storage;
public:
using type = const storage*;
type conv ( const std::chrono::system_clock::time_point& parParam );
~get_pqlib_c_type_struct ( void ) noexcept;
};
template <typename T>
inline typename get_pqlib_c_type_struct<T>::type get_pqlib_c_type (const T& parParam) {
return get_pqlib_c_type_struct<T>::conv(parParam);
}
} //namespace implem
template <typename... Args>
ResultSet Connection::query (const std::string& parQuery, Args&&... parArgs) {
using std::remove_cv;

View file

@ -0,0 +1,84 @@
/* Copyright 2015, Michele Santullo
* This file is part of "dindexer".
*
* "dindexer" is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* "dindexer" is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with "dindexer". If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef idEC73C3E4D64D44ABA0DB7D41FA8A7EB7
#define idEC73C3E4D64D44ABA0DB7D41FA8A7EB7
#include <chrono>
#include <type_traits>
#include <boost/utility/string_ref.hpp>
namespace pq {
namespace implem {
template <typename T>
const char* type_to_pqtypes_name ( void );
template <typename T>
struct get_pqlib_c_type_struct {
using type = T;
static type conv ( T parParam ) { return parParam; }
};
template <>
struct get_pqlib_c_type_struct<std::string> {
using type = const char*;
static type conv ( const std::string& parParam ) { return parParam.c_str(); }
};
template <>
struct get_pqlib_c_type_struct<boost::string_ref> {
using type = const char*;
static type conv ( const boost::string_ref& parParam ) { return parParam.data(); }
};
template <>
struct get_pqlib_c_type_struct<bool> {
using type = int;
static type conv ( bool parParam ) { return (parParam ? 1 : 0); }
};
template <>
struct get_pqlib_c_type_struct<std::chrono::system_clock::time_point> {
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;
storage m_storage;
public:
using type = const storage*;
type conv ( const std::chrono::system_clock::time_point& parParam );
~get_pqlib_c_type_struct ( void ) noexcept;
};
template <typename T>
inline typename get_pqlib_c_type_struct<T>::type get_pqlib_c_type (const T& parParam) {
return get_pqlib_c_type_struct<T>::conv(parParam);
}
template <> inline const char* type_to_pqtypes_name<std::string>() { return "%text"; }
template <> inline const char* type_to_pqtypes_name<boost::string_ref>() { return "%text"; }
template <> inline const char* type_to_pqtypes_name<bool>() { return "%bool"; }
template <> inline const char* type_to_pqtypes_name<float>() { return "%float4"; }
template <> inline const char* type_to_pqtypes_name<double>() { return "%float8"; }
template <> inline const char* type_to_pqtypes_name<int16_t>() { return "%int2"; }
template <> inline const char* type_to_pqtypes_name<int32_t>() { return "%int4"; }
template <> inline const char* type_to_pqtypes_name<int64_t>() { return "%int8"; }
template <> inline const char* type_to_pqtypes_name<uint16_t>() { return "%int2"; }
template <> inline const char* type_to_pqtypes_name<uint32_t>() { return "%int4"; }
template <> inline const char* type_to_pqtypes_name<uint64_t>() { return "%int8"; }
template <> inline const char* type_to_pqtypes_name<std::chrono::system_clock::time_point>() { return "%timestamptz"; }
} //namespace implem
} //namespace pq
#endif

View file

@ -30,35 +30,8 @@
#include <ctime>
#include <cassert>
using sc = std::chrono::system_clock;
namespace pq {
namespace implem {
template <> const char* type_to_pqtypes_name<std::string>() { return "%text"; }
template <> const char* type_to_pqtypes_name<boost::string_ref>() { return "%text"; }
template <> const char* type_to_pqtypes_name<bool>() { return "%bool"; }
template <> const char* type_to_pqtypes_name<float>() { return "%float4"; }
template <> const char* type_to_pqtypes_name<double>() { return "%float8"; }
template <> const char* type_to_pqtypes_name<int16_t>() { return "%int2"; }
template <> const char* type_to_pqtypes_name<int32_t>() { return "%int4"; }
template <> const char* type_to_pqtypes_name<int64_t>() { return "%int8"; }
template <> const char* type_to_pqtypes_name<uint16_t>() { return "%int2"; }
template <> const char* type_to_pqtypes_name<uint32_t>() { return "%int4"; }
template <> const char* type_to_pqtypes_name<uint64_t>() { return "%int8"; }
template <> const char* type_to_pqtypes_name<sc::time_point>() { return "%timestamptz"; }
template const char* type_to_pqtypes_name<std::string> ( void );
template const char* type_to_pqtypes_name<boost::string_ref> ( void );
template const char* type_to_pqtypes_name<bool> ( void );
template const char* type_to_pqtypes_name<float> ( void );
template const char* type_to_pqtypes_name<double> ( void );
template const char* type_to_pqtypes_name<int16_t> ( void );
template const char* type_to_pqtypes_name<int32_t> ( void );
template const char* type_to_pqtypes_name<int64_t> ( void );
template const char* type_to_pqtypes_name<uint16_t> ( void );
template const char* type_to_pqtypes_name<uint32_t> ( void );
template const char* type_to_pqtypes_name<uint64_t> ( void );
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");