reformatted postgres_connection.cpp
This commit is contained in:
parent
a79d50e6e8
commit
f07a360da2
|
|
@ -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,16 +93,17 @@ 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()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
const auto type = oid2type(PQftype(res, i));
|
const auto type = oid2type(PQftype(res, i));
|
||||||
// const char *col_name = PQfname(res, i);
|
// const char *col_name = PQfname(res, i);
|
||||||
// const auto size = PQfmod(res, i);
|
// const auto size = PQfmod(res, i);
|
||||||
prototype.at(i).change_type(type);
|
prototype.at(i).change_type(type);
|
||||||
}
|
}
|
||||||
|
|
||||||
return utils::ok(std::make_unique<sql::query_result_impl>(std::make_unique<postgres_result_reader>(res), prototype));
|
return utils::ok(std::make_unique<sql::query_result_impl>(std::make_unique<postgres_result_reader>(res), prototype));
|
||||||
|
|
@ -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));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -152,64 +152,75 @@ utils::result<size_t, utils::error> postgres_connection::execute(const std::stri
|
||||||
}
|
}
|
||||||
|
|
||||||
utils::basic_type oid2type(const Oid oid) {
|
utils::basic_type oid2type(const Oid oid) {
|
||||||
switch (oid) {
|
switch (oid) {
|
||||||
case 16:
|
case 16:
|
||||||
return utils::basic_type::Boolean;
|
return utils::basic_type::Boolean;
|
||||||
case 17:
|
case 17:
|
||||||
return utils::basic_type::Blob;
|
return utils::basic_type::Blob;
|
||||||
case 18:
|
case 18:
|
||||||
return utils::basic_type::Int8;
|
return utils::basic_type::Int8;
|
||||||
case 21:
|
case 21:
|
||||||
return utils::basic_type::Int16;
|
return utils::basic_type::Int16;
|
||||||
case 23:
|
case 23:
|
||||||
return utils::basic_type::Int32;
|
return utils::basic_type::Int32;
|
||||||
case 20:
|
case 20:
|
||||||
return utils::basic_type::Int64;
|
return utils::basic_type::Int64;
|
||||||
case 25:
|
case 25:
|
||||||
return utils::basic_type::Text;
|
return utils::basic_type::Text;
|
||||||
case 1043:
|
case 1043:
|
||||||
return utils::basic_type::Varchar;
|
return utils::basic_type::Varchar;
|
||||||
case 700:
|
case 700:
|
||||||
return utils::basic_type::Float;
|
return utils::basic_type::Float;
|
||||||
case 701:
|
case 701:
|
||||||
return utils::basic_type::Double;
|
return utils::basic_type::Double;
|
||||||
case 1082:
|
case 1082:
|
||||||
return utils::basic_type::Date;
|
return utils::basic_type::Date;
|
||||||
case 1114:
|
case 1114:
|
||||||
return utils::basic_type::Time;
|
return utils::basic_type::Time;
|
||||||
default:
|
default:
|
||||||
return utils::basic_type::Null;
|
return utils::basic_type::Null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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" {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue