finished postgres fetch
This commit is contained in:
@@ -1,5 +1,10 @@
|
||||
#include "postgres_connection.hpp"
|
||||
#include "postgres_error.hpp"
|
||||
#include "postgres_result_reader.hpp"
|
||||
|
||||
#include "matador/sql/record.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
namespace matador::backends::postgres {
|
||||
|
||||
@@ -41,7 +46,16 @@ std::unique_ptr<sql::query_result_impl> postgres_connection::fetch(const std::st
|
||||
|
||||
throw_postgres_error(res, conn_, "postgres", stmt);
|
||||
|
||||
return {};
|
||||
sql::record prototype;
|
||||
auto ncol = PQnfields(res);
|
||||
for (int i = 0; i < ncol; ++i) {
|
||||
const char *col_name = PQfname(res, i);
|
||||
auto type = PQftype(res, i);
|
||||
auto size = PQfmod(res, i);
|
||||
std::cout << "column " << col_name << ", type " << type << " (size: " << size << ")\n";
|
||||
prototype.append({col_name});
|
||||
}
|
||||
return std::move(std::make_unique<sql::query_result_impl>(std::make_unique<postgres_result_reader>(res), std::move(prototype)));
|
||||
}
|
||||
|
||||
void postgres_connection::prepare(const std::string &stmt)
|
||||
@@ -55,12 +69,67 @@ size_t postgres_connection::execute(const std::string &stmt)
|
||||
|
||||
throw_postgres_error(res, conn_, "postgres", stmt);
|
||||
|
||||
return 0;
|
||||
return sql::to_long_long(PQcmdTuples(res));
|
||||
}
|
||||
|
||||
sql::data_type_t string2type(const char *type)
|
||||
{
|
||||
if (strcmp(type, "int2") == 0) {
|
||||
return sql::data_type_t::type_short;
|
||||
} else if (strcmp(type, "int4") == 0) {
|
||||
return sql::data_type_t::type_int;
|
||||
} else if (strcmp(type, "int8") == 0) {
|
||||
return sql::data_type_t::type_long_long;
|
||||
} else if (strncmp(type, "int8", 6) == 0) {
|
||||
return sql::data_type_t::type_long_long;
|
||||
} else if (strcmp(type, "date") == 0) {
|
||||
return sql::data_type_t::type_date;
|
||||
} else if (strncmp(type, "timestamp", 8) == 0) {
|
||||
return sql::data_type_t::type_time;
|
||||
} else if (strcmp(type, "float4") == 0) {
|
||||
return sql::data_type_t::type_float;
|
||||
} else if (strcmp(type, "float8") == 0) {
|
||||
return sql::data_type_t::type_double;
|
||||
} else if (strncmp(type, "varchar", 7) == 0) {
|
||||
return sql::data_type_t::type_varchar;
|
||||
} else if (strncmp(type, "character varying", 7) == 0) {
|
||||
return sql::data_type_t::type_varchar;
|
||||
} else if (strncmp(type, "text", 0) == 0) {
|
||||
return sql::data_type_t::type_text;
|
||||
} else {
|
||||
return sql::data_type_t::type_unknown;
|
||||
}
|
||||
}
|
||||
|
||||
sql::record postgres_connection::describe(const std::string &table)
|
||||
{
|
||||
return {};
|
||||
std::string stmt(
|
||||
"SELECT ordinal_position, column_name, udt_name, data_type, is_nullable, column_default FROM information_schema.columns WHERE table_schema='public' AND table_name='" + table + "'");
|
||||
|
||||
PGresult *res = PQexec(conn_, stmt.c_str());
|
||||
|
||||
throw_postgres_error(res, conn_, "postgres", stmt);
|
||||
|
||||
postgres_result_reader reader(res);
|
||||
sql::record prototype;
|
||||
while (reader.fetch()) {
|
||||
char *end = nullptr;
|
||||
// Todo: Handle error
|
||||
auto index = strtoul(reader.column(0), &end, 10);
|
||||
std::string name = reader.column(1);
|
||||
|
||||
// Todo: extract size
|
||||
auto type = (string2type(reader.column(2)));
|
||||
end = nullptr;
|
||||
utils::constraints options{};
|
||||
if (strtoul(reader.column(4), &end, 10) == 0) {
|
||||
options = utils::constraints::NOT_NULL;
|
||||
}
|
||||
// f.default_value(res->column(4));
|
||||
prototype.append({name, type, {options}});
|
||||
}
|
||||
|
||||
return std::move(prototype);
|
||||
}
|
||||
|
||||
bool postgres_connection::exists(const std::string &table_name)
|
||||
|
||||
Reference in New Issue
Block a user