mirror of
https://github.com/KingDuckZ/dindexer.git
synced 2025-07-02 14:04:22 +00:00
Add support for std::vectors as arrays.
Nested vectors are not supported.
This commit is contained in:
parent
f82659e370
commit
2655ea5f5c
5 changed files with 157 additions and 14 deletions
|
@ -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();
|
||||
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue