initial matador ng commit
This commit is contained in:
@@ -3,67 +3,101 @@
|
||||
#include "postgres_result_reader.hpp"
|
||||
#include "postgres_statement.hpp"
|
||||
|
||||
#include "matador/sql/error_code.hpp"
|
||||
#include "matador/sql/record.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include "matador/sql/internal/query_result_impl.hpp"
|
||||
|
||||
|
||||
#include <sstream>
|
||||
|
||||
namespace matador::backends::postgres {
|
||||
|
||||
postgres_connection::string_to_int_map postgres_connection::statement_name_map_{};
|
||||
|
||||
postgres_connection::postgres_connection(const sql::connection_info &info)
|
||||
: connection_impl(info) {}
|
||||
: connection_impl(info) {
|
||||
}
|
||||
|
||||
void postgres_connection::open()
|
||||
{
|
||||
utils::result<void, utils::error> postgres_connection::open() {
|
||||
if (is_open()) {
|
||||
return;
|
||||
return utils::ok<void>();
|
||||
}
|
||||
|
||||
std::string connection("user=" + info().user + " password=" + info().password + " host=" + info().hostname + " dbname=" + info().database + " port=" + std::to_string(info().port));
|
||||
const std::string connection(
|
||||
"user=" + info().user + " password=" + info().password + " host=" + info().hostname + " dbname=" + info().database +
|
||||
" port=" + std::to_string(info().port));
|
||||
|
||||
conn_ = PQconnectdb(connection.c_str());
|
||||
if (PQstatus(conn_) == CONNECTION_BAD) {
|
||||
const std::string msg = PQerrorMessage(conn_);
|
||||
PQfinish(conn_);
|
||||
conn_ = nullptr;
|
||||
throw_postgres_error(msg.c_str(), "postgres");
|
||||
return utils::failure(make_error(sql::error_code::OPEN_ERROR, nullptr, conn_, "Failed to connect"));
|
||||
}
|
||||
|
||||
return utils::ok<void>();
|
||||
}
|
||||
|
||||
void postgres_connection::close()
|
||||
{
|
||||
utils::result<void, utils::error> postgres_connection::close() {
|
||||
if (conn_) {
|
||||
PQfinish(conn_);
|
||||
conn_ = nullptr;
|
||||
}
|
||||
|
||||
return utils::ok<void>();
|
||||
}
|
||||
|
||||
bool postgres_connection::is_open()
|
||||
{
|
||||
return conn_ != nullptr;
|
||||
utils::result<bool, utils::error> postgres_connection::is_open() const {
|
||||
return utils::ok(conn_ != nullptr);
|
||||
}
|
||||
|
||||
std::unique_ptr<sql::query_result_impl> postgres_connection::fetch(const std::string &stmt)
|
||||
{
|
||||
PGresult *res = PQexec(conn_, stmt.c_str());
|
||||
utils::result<bool, utils::error> postgres_connection::is_valid() const {
|
||||
return utils::ok(PQstatus(conn_) == CONNECTION_OK);
|
||||
}
|
||||
|
||||
throw_postgres_error(res, conn_, "postgres", stmt);
|
||||
utils::result<utils::version, utils::error> postgres_connection::client_version() const {
|
||||
const auto client_version = PQlibVersion();
|
||||
return utils::ok(utils::version{
|
||||
static_cast<unsigned int>(client_version / 10000),
|
||||
static_cast<unsigned int>((client_version % 10000) / 100),
|
||||
static_cast<unsigned int>(client_version % 100)
|
||||
});
|
||||
}
|
||||
|
||||
std::vector<sql::column_definition> prototype;
|
||||
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);
|
||||
utils::result<utils::version, utils::error> postgres_connection::server_version() const {
|
||||
const auto server_version = PQserverVersion(conn_);
|
||||
|
||||
if (server_version == 0) {
|
||||
return utils::failure(make_error(sql::error_code::FAILURE, nullptr, conn_, "Failed to get server version"));
|
||||
}
|
||||
return std::move(std::make_unique<sql::query_result_impl>(std::make_unique<postgres_result_reader>(res), std::move(prototype)));
|
||||
|
||||
return utils::ok(utils::version{
|
||||
static_cast<unsigned int>(server_version / 10000),
|
||||
static_cast<unsigned int>((server_version % 10000) / 100),
|
||||
static_cast<unsigned int>(server_version % 100)
|
||||
});
|
||||
}
|
||||
|
||||
std::string postgres_connection::generate_statement_name(const sql::query_context &query)
|
||||
{
|
||||
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());
|
||||
|
||||
if (is_result_error(res)) {
|
||||
return utils::failure(make_error(sql::error_code::FETCH_FAILED, res, conn_, "Failed to fetch", context.sql));
|
||||
}
|
||||
|
||||
// std::vector<sql::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);
|
||||
// }
|
||||
|
||||
return utils::ok(std::make_unique<sql::query_result_impl>(std::make_unique<postgres_result_reader>(res), context.prototype));
|
||||
}
|
||||
|
||||
std::string postgres_connection::generate_statement_name(const sql::query_context &query) {
|
||||
std::stringstream name;
|
||||
name << query.table.name << "_" << query.command_name;
|
||||
auto result = postgres_connection::statement_name_map_.find(name.str());
|
||||
@@ -77,71 +111,85 @@ std::string postgres_connection::generate_statement_name(const sql::query_contex
|
||||
return name.str();
|
||||
}
|
||||
|
||||
std::unique_ptr<sql::statement_impl> postgres_connection::prepare(sql::query_context context)
|
||||
{
|
||||
utils::result<std::unique_ptr<sql::statement_impl>, utils::error> postgres_connection::prepare(const sql::query_context &context) {
|
||||
auto statement_name = postgres_connection::generate_statement_name(context);
|
||||
|
||||
PGresult *result = PQprepare(conn_, statement_name.c_str(), context.sql.c_str(), static_cast<int>(context.bind_vars.size()), nullptr);
|
||||
PGresult *result = PQprepare(conn_, statement_name.c_str(), context.sql.c_str(),
|
||||
static_cast<int>(context.bind_vars.size()), nullptr);
|
||||
|
||||
throw_postgres_error(result, conn_, "postgres", context.sql);
|
||||
if (is_result_error(result)) {
|
||||
return utils::failure(make_error(sql::error_code::PREPARE_FAILED, result, conn_, "Failed to prepare", context.sql));
|
||||
}
|
||||
|
||||
return std::make_unique<postgres_statement>(conn_, result, statement_name, std::move(context));
|
||||
std::unique_ptr<sql::statement_impl> s(std::make_unique<postgres_statement>(conn_, result, statement_name, context));
|
||||
return utils::ok(std::move(s));
|
||||
}
|
||||
|
||||
size_t postgres_connection::execute(const std::string &stmt)
|
||||
{
|
||||
utils::result<size_t, utils::error> postgres_connection::execute(const std::string &stmt) {
|
||||
PGresult *res = PQexec(conn_, stmt.c_str());
|
||||
|
||||
throw_postgres_error(res, conn_, "postgres", stmt);
|
||||
if (const auto status = PQresultStatus(res); status != PGRES_COMMAND_OK &&
|
||||
status != PGRES_TUPLES_OK) {
|
||||
return utils::failure(make_error(sql::error_code::FAILURE, res, conn_, "Failed to execute", stmt));
|
||||
}
|
||||
|
||||
const auto affected_rows = sql::to_long_long(PQcmdTuples(res));
|
||||
const auto affected_rows = utils::to<size_t>(PQcmdTuples(res));
|
||||
|
||||
PQclear(res);
|
||||
|
||||
return affected_rows;
|
||||
return utils::ok(static_cast<size_t>(affected_rows));
|
||||
}
|
||||
|
||||
sql::data_type_t string2type(const char *type)
|
||||
{
|
||||
utils::basic_type string2type(const char *type) {
|
||||
if (strcmp(type, "int2") == 0) {
|
||||
return sql::data_type_t::type_short;
|
||||
return utils::basic_type::type_int16;
|
||||
} else if (strcmp(type, "int4") == 0) {
|
||||
return sql::data_type_t::type_int;
|
||||
return utils::basic_type::type_int32;
|
||||
} 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;
|
||||
return utils::basic_type::type_int64;
|
||||
} else if (strcmp(type, "bool") == 0) {
|
||||
return utils::basic_type::type_bool;
|
||||
} 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;
|
||||
return utils::basic_type::type_date;
|
||||
} else if (strcmp(type, "timestamp") == 0) {
|
||||
return utils::basic_type::type_time;
|
||||
} else if (strcmp(type, "float4") == 0) {
|
||||
return sql::data_type_t::type_float;
|
||||
return utils::basic_type::type_float;
|
||||
} else if (strcmp(type, "float8") == 0) {
|
||||
return sql::data_type_t::type_double;
|
||||
return utils::basic_type::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;
|
||||
return utils::basic_type::type_varchar;
|
||||
} else if (strcmp(type, "character varying") == 0) {
|
||||
return utils::basic_type::type_varchar;
|
||||
} else if (strcmp(type, "text") == 0) {
|
||||
return utils::basic_type::type_text;
|
||||
} else if (strcmp(type, "bytea") == 0) {
|
||||
return utils::basic_type::type_blob;
|
||||
} else {
|
||||
return sql::data_type_t::type_unknown;
|
||||
return utils::basic_type::type_null;
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<sql::column_definition> postgres_connection::describe(const std::string &table)
|
||||
{
|
||||
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 + "'");
|
||||
utils::result<std::vector<sql::column_definition>, utils::error> postgres_connection::describe(const std::string &table) {
|
||||
const 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);
|
||||
if (is_result_error(res)) {
|
||||
return utils::failure(make_error(sql::error_code::DESCRIBE_FAILED, res, conn_, "Failed to describe", stmt));
|
||||
}
|
||||
|
||||
postgres_result_reader reader(res);
|
||||
std::vector<sql::column_definition> prototype;
|
||||
while (reader.fetch()) {
|
||||
while (auto fetched = reader.fetch()) {
|
||||
if (!fetched.is_ok()) {
|
||||
return utils::failure(fetched.release_error());
|
||||
}
|
||||
if (!*fetched) {
|
||||
break;
|
||||
}
|
||||
char *end = nullptr;
|
||||
// Todo: Handle error
|
||||
auto index = strtoul(reader.column(0), &end, 10) - 1;
|
||||
@@ -158,31 +206,39 @@ std::vector<sql::column_definition> postgres_connection::describe(const std::str
|
||||
prototype.emplace_back(name, type, utils::null_attributes, null_opt, index);
|
||||
}
|
||||
|
||||
return std::move(prototype);
|
||||
return utils::ok(prototype);
|
||||
}
|
||||
|
||||
bool postgres_connection::exists(const std::string &schema_name, const std::string &table_name)
|
||||
{
|
||||
std::string stmt("SELECT 1 FROM information_schema.tables WHERE table_schema = '" + schema_name + "' AND table_name = '" + table_name + "'");
|
||||
utils::result<bool, utils::error> postgres_connection::exists(const std::string &schema_name, const std::string &table_name) {
|
||||
const std::string stmt(
|
||||
"SELECT 1 FROM information_schema.tables WHERE table_schema = '" + schema_name + "' AND table_name = '" + table_name
|
||||
+ "'");
|
||||
|
||||
PGresult *res = PQexec(conn_, stmt.c_str());
|
||||
|
||||
throw_postgres_error(res, conn_, "postgres", stmt);
|
||||
if (is_result_error(res)) {
|
||||
return utils::failure(make_error(sql::error_code::TABLE_EXISTS_FAILED, res, conn_, "Failed check if table exists", stmt));
|
||||
}
|
||||
|
||||
return sql::to_long_long(PQcmdTuples(res)) == 1;
|
||||
return utils::ok(utils::to<size_t>(PQcmdTuples(res)) == 1);
|
||||
}
|
||||
|
||||
std::string postgres_connection::to_escaped_string(const utils::blob& value) const
|
||||
{
|
||||
size_t escapedDataLength;
|
||||
unsigned char *escapedData = PQescapeByteaConn(conn_, value.data(), value.size(), &escapedDataLength);
|
||||
|
||||
return {reinterpret_cast<char*>(escapedData), escapedDataLength-1};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
extern "C"
|
||||
{
|
||||
MATADOR_POSTGRES_API matador::sql::connection_impl *create_database(const matador::sql::connection_info &info)
|
||||
{
|
||||
extern "C" {
|
||||
MATADOR_POSTGRES_API matador::sql::connection_impl *create_database(const matador::sql::connection_info &info) {
|
||||
return new matador::backends::postgres::postgres_connection(info);
|
||||
}
|
||||
|
||||
MATADOR_POSTGRES_API void destroy_database(matador::sql::connection_impl *db)
|
||||
{
|
||||
MATADOR_POSTGRES_API void destroy_database(matador::sql::connection_impl *db) {
|
||||
delete db;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user