insert has one progress
This commit is contained in:
@@ -33,7 +33,7 @@ public:
|
||||
|
||||
utils::result<sql::execute_result, utils::error> execute(const sql::query_context &context) override;
|
||||
utils::result<std::unique_ptr<sql::statement_impl>, utils::error> prepare(const sql::query_context &context) override;
|
||||
utils::result<std::unique_ptr<sql::query_result_impl>, utils::error> fetch(const sql::query_context &context) override;
|
||||
utils::result<std::unique_ptr<sql::query_result_impl>, utils::error> fetch(const sql::query_context &ctx) override;
|
||||
|
||||
utils::result<std::vector<object::attribute>, utils::error> describe(const std::string& table) override;
|
||||
utils::result<bool, utils::error> exists(const std::string &schema_name, const std::string &table_name) override;
|
||||
@@ -43,16 +43,16 @@ public:
|
||||
[[nodiscard]] std::string to_escaped_string( const utils::blob_type_t& value ) const override;
|
||||
|
||||
private:
|
||||
[[nodiscard]] static std::string generate_statement_name(const sql::query_context &query) ;
|
||||
[[nodiscard]] static std::string generate_statement_name(const sql::query_context &ctx) ;
|
||||
|
||||
utils::result<sql::execute_result, utils::error> execute(const std::string &stmt) const;
|
||||
[[nodiscard]] utils::result<sql::execute_result, utils::error> execute(const std::string &stmt) const;
|
||||
|
||||
private:
|
||||
PGconn *conn_{nullptr};
|
||||
|
||||
using string_to_int_map = std::unordered_map<std::string, unsigned long>;
|
||||
using hash_to_string_map = std::unordered_map<size_t, std::string>;
|
||||
|
||||
static string_to_int_map statement_name_map_;
|
||||
static hash_to_string_map statement_name_map_;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -7,14 +7,15 @@
|
||||
|
||||
#include "matador/sql/error_code.hpp"
|
||||
#include "matador/sql/record.hpp"
|
||||
|
||||
#include "matador/sql/internal/query_result_impl.hpp"
|
||||
|
||||
#include "matador/utils/string.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <sstream>
|
||||
|
||||
namespace matador::backends::postgres {
|
||||
postgres_connection::string_to_int_map postgres_connection::statement_name_map_{};
|
||||
postgres_connection::hash_to_string_map postgres_connection::statement_name_map_{};
|
||||
|
||||
postgres_connection::postgres_connection(const sql::connection_info &info)
|
||||
: connection_impl(info) {
|
||||
@@ -82,20 +83,20 @@ utils::result<utils::version, utils::error> postgres_connection::server_version(
|
||||
|
||||
utils::basic_type oid2type(Oid oid);
|
||||
|
||||
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());
|
||||
utils::result<std::unique_ptr<sql::query_result_impl>, utils::error> postgres_connection::fetch(const sql::query_context &ctx) {
|
||||
PGresult *res = PQexec(conn_, ctx.sql.c_str());
|
||||
|
||||
if (is_result_error(res)) {
|
||||
const auto err = make_error(sql::error_code::FetchFailed, res, conn_, "Failed to fetch", context.sql);
|
||||
const auto err = make_error(sql::error_code::FetchFailed, res, conn_, "Failed to fetch", ctx.sql);
|
||||
PQclear(res);
|
||||
return utils::failure(err);
|
||||
}
|
||||
|
||||
std::vector<object::attribute> prototype = context.prototype;
|
||||
std::vector<object::attribute> prototype = ctx.prototype;
|
||||
|
||||
const int num_col = PQnfields(res);
|
||||
if (prototype.size() != static_cast<size_t>(num_col)) {
|
||||
const auto err = make_error(sql::error_code::FetchFailed, res, conn_, "Number of received columns doesn't match expected columns.", context.sql);
|
||||
const auto err = make_error(sql::error_code::FetchFailed, res, conn_, "Number of received columns doesn't match expected columns.", ctx.sql);
|
||||
PQclear(res);
|
||||
return utils::failure(err);
|
||||
}
|
||||
@@ -111,22 +112,18 @@ utils::result<std::unique_ptr<sql::query_result_impl>, utils::error> postgres_co
|
||||
|
||||
return utils::ok(std::make_unique<sql::query_result_impl>(std::make_unique<postgres_result_reader>(res),
|
||||
std::move(prototype),
|
||||
context.resolver,
|
||||
context.result_type));
|
||||
ctx.resolver,
|
||||
ctx.result_type));
|
||||
}
|
||||
|
||||
std::string postgres_connection::generate_statement_name(const sql::query_context &query) {
|
||||
std::stringstream name;
|
||||
name << query.table_name << "_" << query.command_name;
|
||||
auto result = statement_name_map_.find(name.str());
|
||||
std::string postgres_connection::generate_statement_name(const sql::query_context &ctx) {
|
||||
auto it = statement_name_map_.find(ctx.sql_hash);
|
||||
|
||||
if (result == statement_name_map_.end()) {
|
||||
result = statement_name_map_.insert(std::make_pair(name.str(), 0)).first;
|
||||
if (it == statement_name_map_.end()) {
|
||||
it = statement_name_map_.insert(std::make_pair(ctx.sql_hash, utils::to_hex_string(ctx.sql_hash))).first;
|
||||
}
|
||||
|
||||
name << "_" << ++result->second;
|
||||
|
||||
return name.str();
|
||||
return it->second;
|
||||
}
|
||||
|
||||
utils::result<sql::execute_result, utils::error> postgres_connection::execute(const std::string& stmt) const {
|
||||
|
||||
Reference in New Issue
Block a user