added test for date and time, sql read and write functionality
This commit is contained in:
@@ -4,27 +4,23 @@
|
||||
#include "matador/utils/value.hpp"
|
||||
|
||||
namespace matador::backends::postgres {
|
||||
|
||||
postgres_result_reader::postgres_result_reader(PGresult *result)
|
||||
: result_(result)
|
||||
, row_count_(PQntuples(result_))
|
||||
, column_count_(PQnfields(result_))
|
||||
{}
|
||||
, row_count_(PQntuples(result_))
|
||||
, column_count_(PQnfields(result_)) {
|
||||
}
|
||||
|
||||
postgres_result_reader::~postgres_result_reader()
|
||||
{
|
||||
postgres_result_reader::~postgres_result_reader() {
|
||||
if (result_) {
|
||||
PQclear(result_);
|
||||
}
|
||||
}
|
||||
|
||||
size_t postgres_result_reader::column_count() const
|
||||
{
|
||||
size_t postgres_result_reader::column_count() const {
|
||||
return column_count_;
|
||||
}
|
||||
|
||||
const char *postgres_result_reader::column( const size_t index) const
|
||||
{
|
||||
const char *postgres_result_reader::column(const size_t index) const {
|
||||
return PQgetvalue(result_, static_cast<int>(row_index_), static_cast<int>(index));
|
||||
}
|
||||
|
||||
@@ -106,16 +102,21 @@ void postgres_result_reader::read_value(const char * /*id*/, const size_t index,
|
||||
}
|
||||
}
|
||||
|
||||
void postgres_result_reader::read_value(const char * /*id*/, const size_t /*index*/, time &/*value*/) {
|
||||
// if (const auto val = column(index); strlen(val) > 0) {
|
||||
void postgres_result_reader::read_value(const char * /*id*/, const size_t index, utils::date_type_t &value) {
|
||||
if (const auto val = column(index); strlen(val) > 0) {
|
||||
// value = time::parse(val, "%Y-%m-%d %T.%f");
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
void postgres_result_reader::read_value(const char * /*id*/, const size_t /*index*/, date &/*value*/) {
|
||||
// if (const auto val = column(index); strlen(val) > 0) {
|
||||
void postgres_result_reader::read_value(const char * /*id*/, const size_t index, utils::time_type_t &value) {
|
||||
if (const auto val = column(index); strlen(val) > 0) {
|
||||
// value.set(val, matador::utils::date_format::ISO8601);
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
void postgres_result_reader::read_value(const char * /*id*/, size_t index, utils::timestamp &value) {
|
||||
if (const auto val = column(index); strlen(val) > 0) {
|
||||
}
|
||||
}
|
||||
|
||||
void postgres_result_reader::read_value(const char * /*id*/, const size_t index, char *value, size_t size) {
|
||||
@@ -126,7 +127,7 @@ void postgres_result_reader::read_value(const char * /*id*/, const size_t index,
|
||||
#else
|
||||
strncpy(value, val, size);
|
||||
#endif
|
||||
value[size-1] = '\n';
|
||||
value[size - 1] = '\n';
|
||||
} else {
|
||||
#ifdef _MSC_VER
|
||||
strcpy_s(value, size, val);
|
||||
@@ -144,32 +145,30 @@ void postgres_result_reader::read_value(const char * /*id*/, const size_t index,
|
||||
value.assign(column(index));
|
||||
}
|
||||
|
||||
void postgres_result_reader::read_value( const char* /*id*/, const size_t index, utils::blob& value )
|
||||
{
|
||||
const auto *data = reinterpret_cast<const unsigned char*>(column(index));
|
||||
// auto length = PQgetlength(result_, row_index_, static_cast<int>(index));
|
||||
void postgres_result_reader::read_value(const char * /*id*/, const size_t index, utils::blob &value) {
|
||||
const auto *data = reinterpret_cast<const unsigned char *>(column(index));
|
||||
|
||||
size_t length;
|
||||
unsigned char* unescaped = PQunescapeBytea(data, &length);
|
||||
unsigned char *unescaped = PQunescapeBytea(data, &length);
|
||||
|
||||
value.assign(unescaped, unescaped+length);
|
||||
value.assign(unescaped, unescaped + length);
|
||||
|
||||
PQfreemem(unescaped);
|
||||
}
|
||||
|
||||
template <typename Type>
|
||||
void set_value(const char* str, utils::value& value) {
|
||||
template<typename Type>
|
||||
void set_value(const char *str, utils::value &value) {
|
||||
if (const auto res = utils::to<Type>(str); res.is_ok()) {
|
||||
value = res.value();
|
||||
}
|
||||
}
|
||||
|
||||
template <>
|
||||
void set_value<utils::blob>(const char* str, utils::value& value) {
|
||||
template<>
|
||||
void set_value<utils::blob>(const char *str, utils::value &value) {
|
||||
size_t length;
|
||||
unsigned char* unescaped = PQunescapeBytea(reinterpret_cast<const unsigned char*>(str), &length);
|
||||
unsigned char *unescaped = PQunescapeBytea(reinterpret_cast<const unsigned char *>(str), &length);
|
||||
|
||||
value = utils::blob(unescaped, unescaped+length);
|
||||
value = utils::blob(unescaped, unescaped + length);
|
||||
|
||||
PQfreemem(unescaped);
|
||||
}
|
||||
@@ -219,8 +218,13 @@ void postgres_result_reader::read_value(const char * /*id*/, const size_t index,
|
||||
break;
|
||||
}
|
||||
case utils::basic_type::Time:
|
||||
case utils::basic_type::Date: {
|
||||
val = std::string{column(index)};
|
||||
set_value<utils::time_type_t>(column(index), val);
|
||||
break;
|
||||
case utils::basic_type::Date:
|
||||
set_value<utils::date_type_t>(column(index), val);
|
||||
break;
|
||||
case utils::basic_type::DateTime: {
|
||||
set_value<utils::timestamp>(column(index), val);
|
||||
break;
|
||||
}
|
||||
case utils::basic_type::Null: {
|
||||
@@ -231,11 +235,12 @@ void postgres_result_reader::read_value(const char * /*id*/, const size_t index,
|
||||
set_value<utils::blob>(column(index), val);
|
||||
break;
|
||||
}
|
||||
case utils::basic_type::Unknown:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
utils::attribute_reader &postgres_result_reader::result_binder() {
|
||||
return empty_binder_;
|
||||
}
|
||||
|
||||
} // namespace matador::backends::postgres
|
||||
|
||||
Reference in New Issue
Block a user