1
0
Fork 0
mirror of https://github.com/KingDuckZ/dindexer.git synced 2025-08-20 15:40:50 +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
parent f82659e370
commit 2655ea5f5c
5 changed files with 157 additions and 14 deletions

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