reformatted postgres_connection.cpp

This commit is contained in:
Sascha Kühl 2026-01-01 17:11:25 +01:00
parent a79d50e6e8
commit f07a360da2
1 changed files with 82 additions and 72 deletions

View File

@ -17,7 +17,7 @@ namespace matador::backends::postgres {
postgres_connection::string_to_int_map postgres_connection::statement_name_map_{}; postgres_connection::string_to_int_map postgres_connection::statement_name_map_{};
postgres_connection::postgres_connection(const sql::connection_info &info) postgres_connection::postgres_connection(const sql::connection_info &info)
: connection_impl(info) { : connection_impl(info) {
} }
utils::result<void, utils::error> postgres_connection::open() { utils::result<void, utils::error> postgres_connection::open() {
@ -93,7 +93,8 @@ utils::result<std::unique_ptr<sql::query_result_impl>, utils::error> postgres_co
const int num_col = PQnfields(res); const int num_col = PQnfields(res);
if (prototype.size() != static_cast<size_t>(num_col)) { if (prototype.size() != static_cast<size_t>(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)); 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) { for (int i = 0; i < num_col; ++i) {
if (!prototype.at(i).is_null()) { if (!prototype.at(i).is_null()) {
@ -122,11 +123,11 @@ std::string postgres_connection::generate_statement_name(const sql::query_contex
return name.str(); return name.str();
} }
utils::result<std::unique_ptr<sql::statement_impl>, utils::error> postgres_connection::prepare(const 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 = generate_statement_name(context); auto statement_name = generate_statement_name(context);
const PGresult *result = PQprepare(conn_, statement_name.c_str(), context.sql.c_str(), const PGresult *result = PQprepare(conn_, statement_name.c_str(), context.sql.c_str(), static_cast<int>(context.bind_vars.size()), nullptr);
static_cast<int>(context.bind_vars.size()), nullptr);
if (is_result_error(result)) { if (is_result_error(result)) {
return utils::failure(make_error(sql::error_code::PREPARE_FAILED, result, conn_, "Failed to prepare", context.sql)); return utils::failure(make_error(sql::error_code::PREPARE_FAILED, result, conn_, "Failed to prepare", context.sql));
@ -139,8 +140,7 @@ utils::result<std::unique_ptr<sql::statement_impl>, utils::error> postgres_conne
utils::result<size_t, utils::error> 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()); PGresult *res = PQexec(conn_, stmt.c_str());
if (const auto status = PQresultStatus(res); status != PGRES_COMMAND_OK && if (const auto status = PQresultStatus(res); status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK) {
status != PGRES_TUPLES_OK) {
return utils::failure(make_error(sql::error_code::FAILURE, res, conn_, "Failed to execute", stmt)); return utils::failure(make_error(sql::error_code::FAILURE, res, conn_, "Failed to execute", stmt));
} }
@ -185,31 +185,42 @@ utils::basic_type oid2type(const Oid oid) {
utils::basic_type string2type(const char *type) { utils::basic_type string2type(const char *type) {
if (strcmp(type, "int2") == 0) { if (strcmp(type, "int2") == 0) {
return utils::basic_type::Int16; return utils::basic_type::Int16;
} else if (strcmp(type, "int4") == 0) {
return utils::basic_type::Int32;
} else if (strcmp(type, "int8") == 0) {
return utils::basic_type::Int64;
} else if (strcmp(type, "bool") == 0) {
return utils::basic_type::Boolean;
} else if (strcmp(type, "date") == 0) {
return utils::basic_type::Date;
} else if (strcmp(type, "timestamp") == 0) {
return utils::basic_type::Time;
} else if (strcmp(type, "float4") == 0) {
return utils::basic_type::Float;
} else if (strcmp(type, "float8") == 0) {
return utils::basic_type::Double;
} else if (strncmp(type, "varchar", 7) == 0) {
return utils::basic_type::Varchar;
} else if (strcmp(type, "character varying") == 0) {
return utils::basic_type::Varchar;
} else if (strcmp(type, "text") == 0) {
return utils::basic_type::Text;
} else if (strcmp(type, "bytea") == 0) {
return utils::basic_type::Blob;
} else {
return utils::basic_type::Null;
} }
if (strcmp(type, "int4") == 0) {
return utils::basic_type::Int32;
}
if (strcmp(type, "int8") == 0) {
return utils::basic_type::Int64;
}
if (strcmp(type, "bool") == 0) {
return utils::basic_type::Boolean;
}
if (strcmp(type, "date") == 0) {
return utils::basic_type::Date;
}
if (strcmp(type, "timestamp") == 0) {
return utils::basic_type::Time;
}
if (strcmp(type, "float4") == 0) {
return utils::basic_type::Float;
}
if (strcmp(type, "float8") == 0) {
return utils::basic_type::Double;
}
if (strncmp(type, "varchar", 7) == 0) {
return utils::basic_type::Varchar;
}
if (strcmp(type, "character varying") == 0) {
return utils::basic_type::Varchar;
}
if (strcmp(type, "text") == 0) {
return utils::basic_type::Text;
}
if (strcmp(type, "bytea") == 0) {
return utils::basic_type::Blob;
}
return utils::basic_type::Null;
} }
utils::result<std::vector<object::attribute>, utils::error> postgres_connection::describe(const std::string &table) { utils::result<std::vector<object::attribute>, utils::error> postgres_connection::describe(const std::string &table) {
@ -251,8 +262,9 @@ utils::result<std::vector<object::attribute>, utils::error> postgres_connection:
return utils::ok(prototype); return utils::ok(prototype);
} }
utils::result<bool, utils::error> postgres_connection::exists(const std::string &schema_name, const std::string &table_name) { utils::result<bool, utils::error> postgres_connection::exists(const std::string &schema_name,
std::string stmt( "SELECT 1 FROM information_schema.tables WHERE table_name = '" + table_name + "'"); const std::string &table_name) {
std::string stmt("SELECT 1 FROM information_schema.tables WHERE table_name = '" + table_name + "'");
if (!schema_name.empty()) { if (!schema_name.empty()) {
stmt += " AND table_schema = '" + schema_name + "'"; stmt += " AND table_schema = '" + schema_name + "'";
} }
@ -270,14 +282,12 @@ utils::result<bool, utils::error> postgres_connection::exists(const std::string
return utils::ok(*result == 1); return utils::ok(*result == 1);
} }
std::string postgres_connection::to_escaped_string(const utils::blob& value) const std::string postgres_connection::to_escaped_string(const utils::blob &value) const {
{
size_t escapedDataLength; size_t escapedDataLength;
unsigned char *escapedData = PQescapeByteaConn(conn_, value.data(), value.size(), &escapedDataLength); unsigned char *escapedData = PQescapeByteaConn(conn_, value.data(), value.size(), &escapedDataLength);
return {reinterpret_cast<char*>(escapedData), escapedDataLength-1}; return {reinterpret_cast<char *>(escapedData), escapedDataLength - 1};
} }
} }
extern "C" { extern "C" {