fixed backend tests (progress)
This commit is contained in:
@@ -10,6 +10,7 @@
|
||||
|
||||
#include "matador/sql/internal/query_result_impl.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <sstream>
|
||||
|
||||
namespace matador::backends::postgres {
|
||||
@@ -79,6 +80,8 @@ utils::result<utils::version, utils::error> postgres_connection::server_version(
|
||||
});
|
||||
}
|
||||
|
||||
utils::basic_type oid2type(Oid oid);
|
||||
|
||||
utils::result<std::unique_ptr<sql::query_result_impl>, utils::error> postgres_connection::fetch(const sql::query_context &context) {
|
||||
PGresult *res = PQexec(conn_, context.sql.c_str());
|
||||
|
||||
@@ -86,16 +89,28 @@ utils::result<std::unique_ptr<sql::query_result_impl>, utils::error> postgres_co
|
||||
return utils::failure(make_error(sql::error_code::FETCH_FAILED, res, conn_, "Failed to fetch", context.sql));
|
||||
}
|
||||
|
||||
// std::vector<object::column_definition> prototype;
|
||||
// const auto num_col = PQnfields(res);
|
||||
// for (int i = 0; i < num_col; ++i) {
|
||||
// const char *col_name = PQfname(res, i);
|
||||
// auto type = PQftype(res, i);
|
||||
// auto size = PQfmod(res, i);
|
||||
// prototype.emplace_back(col_name);
|
||||
// }
|
||||
// const auto is_unknown = std::all_of( context.prototype.begin(), context.prototype.end(), [](const object::attribute_definition &a) {return a.is_null();} );
|
||||
// if (!is_unknown) {
|
||||
// return utils::ok(std::make_unique<sql::query_result_impl>(std::make_unique<postgres_result_reader>(res), context.prototype));
|
||||
// }
|
||||
|
||||
return utils::ok(std::make_unique<sql::query_result_impl>(std::make_unique<postgres_result_reader>(res), context.prototype));
|
||||
std::vector<object::attribute_definition> prototype = context.prototype;
|
||||
|
||||
const auto num_col = PQnfields(res);
|
||||
if (prototype.size() != num_col) {
|
||||
return utils::failure(make_error(sql::error_code::FETCH_FAILED, res, conn_, "Number of received columns doesn't match expected columns.", context.sql));
|
||||
}
|
||||
for (int i = 0; i < num_col; ++i) {
|
||||
if (!prototype.at(i).is_null()) {
|
||||
continue;
|
||||
}
|
||||
const auto type = oid2type(PQftype(res, i));
|
||||
// const char *col_name = PQfname(res, i);
|
||||
// const auto size = PQfmod(res, i);
|
||||
prototype.at(i).type(type);
|
||||
}
|
||||
|
||||
return utils::ok(std::make_unique<sql::query_result_impl>(std::make_unique<postgres_result_reader>(res), prototype));
|
||||
}
|
||||
|
||||
std::string postgres_connection::generate_statement_name(const sql::query_context &query) {
|
||||
@@ -141,6 +156,37 @@ utils::result<size_t, utils::error> postgres_connection::execute(const std::stri
|
||||
return utils::ok(static_cast<size_t>(affected_rows));
|
||||
}
|
||||
|
||||
utils::basic_type oid2type(const Oid oid) {
|
||||
switch (oid) {
|
||||
case 16:
|
||||
return utils::basic_type::type_bool;
|
||||
case 17:
|
||||
return utils::basic_type::type_blob;
|
||||
case 18:
|
||||
return utils::basic_type::type_int8;
|
||||
case 21:
|
||||
return utils::basic_type::type_int16;
|
||||
case 23:
|
||||
return utils::basic_type::type_int32;
|
||||
case 20:
|
||||
return utils::basic_type::type_int64;
|
||||
case 24:
|
||||
return utils::basic_type::type_text;
|
||||
case 1043:
|
||||
return utils::basic_type::type_varchar;
|
||||
case 700:
|
||||
return utils::basic_type::type_float;
|
||||
case 701:
|
||||
return utils::basic_type::type_double;
|
||||
case 1082:
|
||||
return utils::basic_type::type_date;
|
||||
case 1114:
|
||||
return utils::basic_type::type_time;
|
||||
default:
|
||||
return utils::basic_type::type_null;
|
||||
}
|
||||
}
|
||||
|
||||
utils::basic_type string2type(const char *type) {
|
||||
if (strcmp(type, "int2") == 0) {
|
||||
return utils::basic_type::type_int16;
|
||||
|
||||
Reference in New Issue
Block a user