diff --git a/backends/postgres/src/postgres_connection.cpp b/backends/postgres/src/postgres_connection.cpp index c8816d3..575f6e4 100644 --- a/backends/postgres/src/postgres_connection.cpp +++ b/backends/postgres/src/postgres_connection.cpp @@ -17,7 +17,7 @@ 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) { } utils::result postgres_connection::open() { @@ -93,16 +93,17 @@ utils::result, utils::error> postgres_co const int num_col = PQnfields(res); if (prototype.size() != static_cast(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) { - 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).change_type(type); + 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).change_type(type); } return utils::ok(std::make_unique(std::make_unique(res), prototype)); @@ -122,11 +123,11 @@ std::string postgres_connection::generate_statement_name(const sql::query_contex return name.str(); } -utils::result, utils::error> postgres_connection::prepare(const sql::query_context &context) { +utils::result, utils::error> postgres_connection::prepare( + const sql::query_context &context) { auto statement_name = generate_statement_name(context); - const PGresult *result = PQprepare(conn_, statement_name.c_str(), context.sql.c_str(), - static_cast(context.bind_vars.size()), nullptr); + const PGresult *result = PQprepare(conn_, statement_name.c_str(), context.sql.c_str(), static_cast(context.bind_vars.size()), nullptr); if (is_result_error(result)) { return utils::failure(make_error(sql::error_code::PREPARE_FAILED, result, conn_, "Failed to prepare", context.sql)); @@ -139,8 +140,7 @@ utils::result, utils::error> postgres_conne utils::result postgres_connection::execute(const std::string &stmt) { PGresult *res = PQexec(conn_, stmt.c_str()); - if (const auto status = PQresultStatus(res); status != PGRES_COMMAND_OK && - status != PGRES_TUPLES_OK) { + 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)); } @@ -152,64 +152,75 @@ utils::result postgres_connection::execute(const std::stri } utils::basic_type oid2type(const Oid oid) { - switch (oid) { - case 16: - return utils::basic_type::Boolean; - case 17: - return utils::basic_type::Blob; - case 18: - return utils::basic_type::Int8; - case 21: - return utils::basic_type::Int16; - case 23: - return utils::basic_type::Int32; - case 20: - return utils::basic_type::Int64; - case 25: - return utils::basic_type::Text; - case 1043: - return utils::basic_type::Varchar; - case 700: - return utils::basic_type::Float; - case 701: - return utils::basic_type::Double; - case 1082: - return utils::basic_type::Date; - case 1114: - return utils::basic_type::Time; - default: - return utils::basic_type::Null; - } + switch (oid) { + case 16: + return utils::basic_type::Boolean; + case 17: + return utils::basic_type::Blob; + case 18: + return utils::basic_type::Int8; + case 21: + return utils::basic_type::Int16; + case 23: + return utils::basic_type::Int32; + case 20: + return utils::basic_type::Int64; + case 25: + return utils::basic_type::Text; + case 1043: + return utils::basic_type::Varchar; + case 700: + return utils::basic_type::Float; + case 701: + return utils::basic_type::Double; + case 1082: + return utils::basic_type::Date; + case 1114: + return utils::basic_type::Time; + default: + return utils::basic_type::Null; + } } utils::basic_type string2type(const char *type) { if (strcmp(type, "int2") == 0) { 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, utils::error> postgres_connection::describe(const std::string &table) { @@ -251,8 +262,9 @@ utils::result, utils::error> postgres_connection: return utils::ok(prototype); } -utils::result postgres_connection::exists(const std::string &schema_name, const std::string &table_name) { - std::string stmt( "SELECT 1 FROM information_schema.tables WHERE table_name = '" + table_name + "'"); +utils::result postgres_connection::exists(const std::string &schema_name, + const std::string &table_name) { + std::string stmt("SELECT 1 FROM information_schema.tables WHERE table_name = '" + table_name + "'"); if (!schema_name.empty()) { stmt += " AND table_schema = '" + schema_name + "'"; } @@ -270,14 +282,12 @@ utils::result postgres_connection::exists(const std::string 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; unsigned char *escapedData = PQescapeByteaConn(conn_, value.data(), value.size(), &escapedDataLength); - return {reinterpret_cast(escapedData), escapedDataLength-1}; + return {reinterpret_cast(escapedData), escapedDataLength - 1}; } - } extern "C" {