added test for date and time, sql read and write functionality
This commit is contained in:
@@ -178,8 +178,10 @@ utils::basic_type oid2type(const Oid oid) {
|
||||
return utils::basic_type::Double;
|
||||
case 1082:
|
||||
return utils::basic_type::Date;
|
||||
case 1114:
|
||||
case 1083:
|
||||
return utils::basic_type::Time;
|
||||
case 1114:
|
||||
return utils::basic_type::DateTime;
|
||||
default:
|
||||
return utils::basic_type::Null;
|
||||
}
|
||||
@@ -201,9 +203,12 @@ utils::basic_type string2type(const char *type) {
|
||||
if (strcmp(type, "date") == 0) {
|
||||
return utils::basic_type::Date;
|
||||
}
|
||||
if (strcmp(type, "timestamp") == 0) {
|
||||
if (strcmp(type, "time") == 0) {
|
||||
return utils::basic_type::Time;
|
||||
}
|
||||
if (strcmp(type, "timestamp") == 0) {
|
||||
return utils::basic_type::DateTime;
|
||||
}
|
||||
if (strcmp(type, "float4") == 0) {
|
||||
return utils::basic_type::Float;
|
||||
}
|
||||
|
||||
@@ -20,7 +20,8 @@
|
||||
{matador::utils::basic_type::UInt8, "SMALLINT"},
|
||||
{matador::utils::basic_type::Float, "REAL"},
|
||||
{matador::utils::basic_type::Double, "DOUBLE PRECISION"},
|
||||
{matador::utils::basic_type::Time, "TIMESTAMP"},
|
||||
{matador::utils::basic_type::Time, "TIME(6)"},
|
||||
{matador::utils::basic_type::DateTime, "TIMESTAMPTZ(6)"},
|
||||
{matador::utils::basic_type::Blob, "BYTEA"}
|
||||
})
|
||||
.with_bool_strings("TRUE", "FALSE")
|
||||
|
||||
@@ -123,15 +123,23 @@ void postgres_parameter_binder::write_value(const size_t pos, const std::string
|
||||
write_value(pos, x);
|
||||
}
|
||||
|
||||
void postgres_parameter_binder::write_value(const size_t pos, const utils::date_type_t &/*x*/) {
|
||||
// bind_data_.strings[pos] = utils::to_string(x, utils::date_format::ISO8601);
|
||||
void postgres_parameter_binder::write_value(const size_t pos, const utils::date_type_t &x) {
|
||||
char buf[11]; // "YYYY-MM-DD" + '\0'
|
||||
|
||||
std::snprintf(buf, sizeof(buf), "%04d-%02u-%02u", x.year, static_cast<unsigned>(x.month), static_cast<unsigned>(x.day));
|
||||
bind_data_.strings[pos] = std::string{buf};
|
||||
bind_data_.values[pos] = bind_data_.strings[pos].data();
|
||||
bind_data_.lengths[pos] = static_cast<int>(bind_data_.strings[pos].size());
|
||||
bind_data_.formats[pos] = 0;
|
||||
}
|
||||
|
||||
void postgres_parameter_binder::write_value(const size_t pos, const utils::time_type_t &/*x*/) {
|
||||
// bind_data_.strings[pos] = utils::to_string(x, "%Y-%m-%d %T.%f");
|
||||
void postgres_parameter_binder::write_value(const size_t pos, const utils::time_type_t &x) {
|
||||
char buf[16]; // "HH:MM:SS.ffffff" + '\0'
|
||||
|
||||
std::snprintf(buf, sizeof(buf), "%02u:%02u:%02u.%06u",
|
||||
static_cast<unsigned>(x.hour), static_cast<unsigned>(x.minute), static_cast<unsigned>(x.second), x.microsecond);
|
||||
|
||||
bind_data_.strings[pos] = std::string{buf};
|
||||
bind_data_.values[pos] = bind_data_.strings[pos].data();
|
||||
bind_data_.lengths[pos] = static_cast<int>(bind_data_.strings[pos].size());
|
||||
bind_data_.formats[pos] = 0;
|
||||
|
||||
@@ -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