1
0
Fork 0
mirror of https://github.com/KingDuckZ/dindexer.git synced 2025-10-17 16:49:26 +00:00

Add support for std::vectors as arrays.

Nested vectors are not supported.
This commit is contained in:
King_DuckZ 2016-01-07 12:57:06 +00:00
commit 2655ea5f5c
5 changed files with 157 additions and 14 deletions

View file

@ -1,4 +1,4 @@
/* Copyright 2015, Michele Santullo
/* Copyright 2016, Michele Santullo
* This file is part of "dindexer".
*
* "dindexer" is free software: you can redistribute it and/or modify
@ -30,6 +30,7 @@ typedef pg_param PGparam;
namespace pq {
class Connection {
friend struct implem::get_pqlib_c_type_struct_arr;
public:
Connection ( std::string&& parUsername, std::string&& parPasswd, std::string&& parDatabase, std::string&& parAddress, uint16_t parPort );
Connection ( Connection&& );
@ -57,6 +58,7 @@ namespace pq {
ResultSet query_params ( const std::string& parQuery, PGParams& parParams );
PGParams make_params ( const std::string* parTypes, ... );
PGParams make_empty_params ( void ) const;
std::unique_ptr<LocalData> m_localData;
std::string m_username;
@ -78,7 +80,7 @@ namespace pq {
auto types_bt = concat_strings(make_pqtypes_name<typename remove_cv<typename remove_reference<Args>::type>::type>()...);
static_assert(types_bt.size() > 0, "Invalid empty types string (function called with no arguments?)");
const std::string types(types_bt.data(), types_bt.size());
return this->make_params(&types, implem::get_pqlib_c_type_struct<typename remove_cv<typename remove_reference<Args>::type>::type>().conv(parArgs)...);
return this->make_params(&types, implem::get_pqlib_c_type_struct<typename remove_cv<typename remove_reference<Args>::type>::type>(*this).conv(parArgs)...);
};
PGParams pgparams = make_pgparams();

View file

@ -1,4 +1,4 @@
/* Copyright 2015, Michele Santullo
/* Copyright 2016, Michele Santullo
* This file is part of "dindexer".
*
* "dindexer" is free software: you can redistribute it and/or modify
@ -22,8 +22,15 @@
#include <chrono>
#include <type_traits>
#include <boost/utility/string_ref.hpp>
#include <vector>
#include <memory>
struct pg_param;
typedef pg_param PGparam;
namespace pq {
class Connection;
namespace implem {
template <typename T>
struct type_to_pqtypes_name;
@ -31,25 +38,30 @@ namespace pq {
template <typename T>
struct get_pqlib_c_type_struct {
using type = T;
explicit get_pqlib_c_type_struct ( const Connection& ) { }
static type conv ( T parParam ) { return parParam; }
};
template <>
struct get_pqlib_c_type_struct<std::string> {
using type = const char*;
explicit get_pqlib_c_type_struct ( const Connection& ) { }
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*;
explicit get_pqlib_c_type_struct ( const Connection& ) { }
static type conv ( const boost::string_ref& parParam ) { return parParam.data(); }
};
template <>
struct get_pqlib_c_type_struct<bool> {
using type = int;
explicit get_pqlib_c_type_struct ( const Connection& ) { }
static type conv ( bool parParam ) { return (parParam ? 1 : 0); }
};
template <>
struct get_pqlib_c_type_struct<std::chrono::system_clock::time_point> {
//Hack to make some sort of "static pimpl"
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;
@ -58,6 +70,7 @@ namespace pq {
public:
using type = const storage*;
explicit get_pqlib_c_type_struct ( const Connection& ) { }
type conv ( const std::chrono::system_clock::time_point& parParam );
~get_pqlib_c_type_struct ( void ) noexcept;
};
@ -67,6 +80,19 @@ namespace pq {
return get_pqlib_c_type_struct<T>::conv(parParam);
}
constexpr inline std::size_t strlen (const char* parStr) {
return (*parStr ? 1 + strlen(parStr + 1) : 0);
}
template <std::size_t S>
constexpr inline std::size_t strlen (bt::string<S> parStr) {
return parStr.size();
}
template <typename T>
struct type_to_pqtypes_name_impl : public type_to_pqtypes_name<T> {
constexpr static const std::size_t size = strlen(type_to_pqtypes_name<T>::name) + 1;
};
template <>
struct type_to_pqtypes_name<std::string> {
constexpr static const char* name = "text";
@ -115,19 +141,56 @@ namespace pq {
struct type_to_pqtypes_name<std::chrono::system_clock::time_point> {
constexpr static const char* name = "timestamptz";
};
constexpr inline std::size_t strlen (const char* parStr) {
return (*parStr ? 1 + strlen(parStr + 1) : 0);
}
template <typename T>
struct type_to_pqtypes_name_impl : public type_to_pqtypes_name<T> {
constexpr static const std::size_t size = strlen(type_to_pqtypes_name<T>::name) + 1;
template <typename VT, typename VU>
struct type_to_pqtypes_name<std::vector<VT, VU>> {
private:
constexpr static const std::size_t sub_size = strlen(type_to_pqtypes_name<VT>::name) + 1;
constexpr static const bt::string<sub_size> sub_name_str = type_to_pqtypes_name<VT>::name;
public:
constexpr static bt::string<3 + sub_size - 1> name = sub_name_str + bt::make_string("[]");
};
template <typename T>
constexpr bt::string<type_to_pqtypes_name_impl<T>::size + 1> make_pqtypes_name ( const char parPrefix='%' );
//Helper class to build a parameter of array type
struct get_pqlib_c_type_struct_arr {
private:
using PGParams = std::unique_ptr<::PGparam, void(*)(::PGparam*)>;
//Hack to make some sort of "static pimpl"
struct StorageStruct { int a; int b[2 * 6]; void* c[2]; };
static constexpr std::size_t DATA_SIZE = sizeof(StorageStruct);
using storage = std::aligned_storage<DATA_SIZE, alignof(uint64_t)>::type;
void push_param ( const char* parFormat, ... );
storage m_storage;
PGParams m_par;
protected:
explicit get_pqlib_c_type_struct_arr ( const Connection& parConn );
~get_pqlib_c_type_struct_arr ( void ) noexcept;
void par_reset ( void );
const void* get_return_ptr ( void );
template <typename T>
void push_param ( const T& parParam ) {
static_assert(std::is_fundamental<T>::value or std::is_same<std::string, T>::value or std::is_same<boost::string_ref, T>::value or std::is_same<std::chrono::system_clock::time_point, T>::value, "Unsupported type in array");
this->push_param(make_pqtypes_name<T>().data(), get_pqlib_c_type_struct<T>::conv(parParam));
}
};
template <typename VT, typename VU>
struct get_pqlib_c_type_struct<std::vector<VT, VU>> : private get_pqlib_c_type_struct_arr {
using type = const void*;
explicit get_pqlib_c_type_struct ( const Connection& parConn ) : get_pqlib_c_type_struct_arr(parConn) { }
type conv ( const std::vector<VT, VU>& parParam) {
this->par_reset();
for (const auto& i : parParam) {
this->push_param(i);
}
return get_return_ptr();
}
};
template <typename T>
inline constexpr
bt::string<type_to_pqtypes_name_impl<T>::size + 1> make_pqtypes_name (const char parPrefix) {

View file

@ -4,6 +4,7 @@ add_library(${PROJECT_NAME} STATIC
connection.cpp
databaseexception.cpp
resultset.cpp
pq_type_helpers.cpp
)
target_include_directories(${PROJECT_NAME}

View file

@ -1,4 +1,4 @@
/* Copyright 2015, Michele Santullo
/* Copyright 2016, Michele Santullo
* This file is part of "dindexer".
*
* "dindexer" is free software: you can redistribute it and/or modify
@ -99,7 +99,9 @@ namespace pq {
}
Connection::~Connection() noexcept {
disconnect();
if (m_localData) {
disconnect();
}
}
Connection& Connection::operator= (Connection&& parOther) {
@ -214,7 +216,7 @@ namespace pq {
}
auto Connection::make_params (const std::string* parTypes, ...) -> PGParams {
PGParams retval(PQparamCreate(m_localData->connection), &PQparamClear);
PGParams retval = make_empty_params();
va_list argp;
va_start(argp, parTypes);
@ -223,4 +225,11 @@ namespace pq {
return std::move(retval);
}
auto Connection::make_empty_params() const -> PGParams {
assert(is_connected());
auto ret = PGParams(PQparamCreate(m_localData->connection), &PQparamClear);
assert(ret.get());
return std::move(ret);
}
} //namespace pq

View file

@ -0,0 +1,68 @@
/* Copyright 2016, 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/>.
*/
#include "pq/implem/pq_type_helpers.hpp"
#include "pq/connection.hpp"
#include <algorithm>
#include <cstdarg>
#include "libpqtypes.h"
#include <cassert>
#if !defined(NDEBUG)
# include <cstring>
#endif
namespace pq {
namespace implem {
get_pqlib_c_type_struct_arr::get_pqlib_c_type_struct_arr (const Connection& parConn) :
m_par(parConn.make_empty_params())
{
static_assert(sizeof(storage) == sizeof(PGarray), "Wrong size for PGarray's static memory");
static_assert(alignof(storage) == alignof(PGarray), "Wrong alignment for PGarray's static memory");
PGarray arr;
std::fill(reinterpret_cast<char*>(&arr), reinterpret_cast<char*>(&arr) + sizeof(PGarray), '\0');
arr.param = m_par.get();
std::copy(reinterpret_cast<const char*>(&arr), reinterpret_cast<const char*>(&arr) + sizeof(PGarray), reinterpret_cast<char*>(&m_storage));
assert(not std::memcmp(&arr, &m_storage, sizeof(PGarray)));
assert(not arr.ndims);
assert(not arr.dims[0]);
assert(arr.param);
}
get_pqlib_c_type_struct_arr::~get_pqlib_c_type_struct_arr() noexcept {
}
void get_pqlib_c_type_struct_arr::par_reset() {
PQparamReset(m_par.get());
}
const void* get_pqlib_c_type_struct_arr::get_return_ptr() {
assert(m_par);
return reinterpret_cast<const void*>(&m_storage);
}
void get_pqlib_c_type_struct_arr::push_param (const char* parFormat, ...) {
//Only one argument is expected, but type and size on the stack is unknown :/
va_list argp;
va_start(argp, parFormat);
PQputvf(m_par.get(), nullptr, 0, parFormat, argp);
va_end(argp);
}
} //namespace implem
} //namespace pq