implemented prepared statement for postgres and sqlite

This commit is contained in:
2023-12-09 11:08:03 +01:00
parent 8ba70cc79e
commit da423ea8bb
59 changed files with 1769 additions and 314 deletions
+29 -4
View File
@@ -1,13 +1,17 @@
#include "postgres_connection.hpp"
#include "postgres_error.hpp"
#include "postgres_result_reader.hpp"
#include "postgres_statement.hpp"
#include "matador/sql/record.hpp"
#include <iostream>
#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) {}
@@ -47,20 +51,41 @@ std::unique_ptr<sql::query_result_impl> postgres_connection::fetch(const std::st
throw_postgres_error(res, conn_, "postgres", stmt);
sql::record prototype;
auto ncol = PQnfields(res);
for (int i = 0; i < ncol; ++i) {
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);
std::cout << "column " << col_name << ", type " << type << " (size: " << size << ")\n";
// std::cout << "column " << col_name << ", type " << type << " (size: " << size << ")\n";
prototype.append({col_name});
}
return std::move(std::make_unique<sql::query_result_impl>(std::make_unique<postgres_result_reader>(res), std::move(prototype)));
}
void postgres_connection::prepare(const std::string &stmt)
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());
if (result == postgres_connection::statement_name_map_.end()) {
result = postgres_connection::statement_name_map_.insert(std::make_pair(name.str(), 0)).first;
}
name << "_" << ++result->second;
return name.str();
}
std::unique_ptr<sql::statement_impl> postgres_connection::prepare(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);
throw_postgres_error(result, conn_, "postgres", context.sql);
return std::make_unique<postgres_statement>(conn_, result, statement_name, std::move(context));
}
size_t postgres_connection::execute(const std::string &stmt)