added postgres backend
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
#include "postgres_connection.hpp"
|
||||
#include "postgres_error.hpp"
|
||||
|
||||
namespace matador::backends::postgres {
|
||||
|
||||
postgres_connection::postgres_connection(const sql::connection_info &info)
|
||||
: connection_impl(info) {}
|
||||
|
||||
void postgres_connection::open()
|
||||
{
|
||||
if (is_open()) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::string connection("user=" + info().user + " password=" + info().password + " host=" + info().hostname + " dbname=" + info().database + " port=" + std::to_string(info().port));
|
||||
|
||||
conn_ = PQconnectdb(connection.c_str());
|
||||
if (PQstatus(conn_) == CONNECTION_BAD) {
|
||||
const auto msg = PQerrorMessage(conn_);
|
||||
PQfinish(conn_);
|
||||
throw_postgres_error(msg, "postgres");
|
||||
}
|
||||
}
|
||||
|
||||
void postgres_connection::close()
|
||||
{
|
||||
if (conn_) {
|
||||
PQfinish(conn_);
|
||||
conn_ = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
bool postgres_connection::is_open()
|
||||
{
|
||||
return conn_ != nullptr;
|
||||
}
|
||||
|
||||
std::unique_ptr<sql::query_result_impl> postgres_connection::fetch(const std::string &stmt)
|
||||
{
|
||||
PGresult *res = PQexec(conn_, stmt.c_str());
|
||||
|
||||
throw_postgres_error(res, conn_, "postgres", stmt);
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
void postgres_connection::prepare(const std::string &stmt)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
size_t postgres_connection::execute(const std::string &stmt)
|
||||
{
|
||||
PGresult *res = PQexec(conn_, stmt.c_str());
|
||||
|
||||
throw_postgres_error(res, conn_, "postgres", stmt);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
sql::record postgres_connection::describe(const std::string &table)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
bool postgres_connection::exists(const std::string &table_name)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
extern "C"
|
||||
{
|
||||
MATADOR_POSTGRES_API matador::sql::connection_impl *create_database(const matador::sql::connection_info &info)
|
||||
{
|
||||
return new matador::backends::postgres::postgres_connection(info);
|
||||
}
|
||||
|
||||
MATADOR_POSTGRES_API void destroy_database(matador::sql::connection_impl *db)
|
||||
{
|
||||
delete db;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
#include "postgres_dialect.hpp"
|
||||
|
||||
[[maybe_unused]] const matador::sql::dialect* get_dialect() {
|
||||
using namespace matador::sql;
|
||||
const static dialect d{};
|
||||
return &d;
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
#include "postgres_error.hpp"
|
||||
|
||||
#include <sstream>
|
||||
|
||||
#include <libpq-fe.h>
|
||||
|
||||
namespace matador::backends::postgres {
|
||||
|
||||
void throw_postgres_error(const char *what, const std::string &source)
|
||||
{
|
||||
std::stringstream msg;
|
||||
msg << "postgres error (" << source << "): " << what;
|
||||
throw std::logic_error(msg.str());
|
||||
}
|
||||
|
||||
void throw_postgres_error(PGconn *db, const std::string &source)
|
||||
{
|
||||
if (PQstatus(db) == CONNECTION_BAD) {
|
||||
throw_postgres_error(PQerrorMessage(db), source);
|
||||
}
|
||||
}
|
||||
|
||||
void throw_postgres_error(PGresult *res, PGconn *db, const std::string &source, const std::string &sql)
|
||||
{
|
||||
if (res == nullptr ||
|
||||
(PQresultStatus(res) != PGRES_COMMAND_OK &&
|
||||
PQresultStatus(res) != PGRES_TUPLES_OK)) {
|
||||
std::stringstream msg;
|
||||
msg << "postgres error (" << source << ", " << PQresultErrorField(res, PG_DIAG_SQLSTATE) << ") " << PQerrorMessage(db) << ": " << sql;
|
||||
throw std::logic_error(msg.str());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
#include "postgres_result_reader.hpp"
|
||||
|
||||
#include "matador/sql/to_value.hpp"
|
||||
|
||||
namespace matador::backends::postgres {
|
||||
|
||||
postgres_result_reader::postgres_result_reader(PGresult *result)
|
||||
: result_(result)
|
||||
, row_count_(PQntuples(result_))
|
||||
, column_count_(PQnfields(result_))
|
||||
{}
|
||||
|
||||
postgres_result_reader::~postgres_result_reader()
|
||||
{
|
||||
if (result_) {
|
||||
PQclear(result_);
|
||||
}
|
||||
}
|
||||
|
||||
size_t postgres_result_reader::column_count() const
|
||||
{
|
||||
return column_count_;
|
||||
}
|
||||
|
||||
const char *postgres_result_reader::column(size_t index) const
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool postgres_result_reader::fetch()
|
||||
{
|
||||
return ++row_index_ < row_count_;
|
||||
}
|
||||
|
||||
void postgres_result_reader::read_value(const char *id, size_t index, char &value)
|
||||
{
|
||||
sql::to_value(value, PQgetvalue(result_, static_cast<int>(row_index_), static_cast<int>(index)));
|
||||
}
|
||||
|
||||
void postgres_result_reader::read_value(const char *id, size_t index, short &value)
|
||||
{
|
||||
sql::to_value(value, PQgetvalue(result_, static_cast<int>(row_index_), static_cast<int>(index)));
|
||||
}
|
||||
|
||||
void postgres_result_reader::read_value(const char *id, size_t index, int &value)
|
||||
{
|
||||
sql::to_value(value, PQgetvalue(result_, static_cast<int>(row_index_), static_cast<int>(index)));
|
||||
}
|
||||
|
||||
void postgres_result_reader::read_value(const char *id, size_t index, long &value)
|
||||
{
|
||||
sql::to_value(value, PQgetvalue(result_, static_cast<int>(row_index_), static_cast<int>(index)));
|
||||
}
|
||||
|
||||
void postgres_result_reader::read_value(const char *id, size_t index, long long int &value)
|
||||
{
|
||||
sql::to_value(value, PQgetvalue(result_, static_cast<int>(row_index_), static_cast<int>(index)));
|
||||
}
|
||||
|
||||
void postgres_result_reader::read_value(const char *id, size_t index, unsigned char &value)
|
||||
{
|
||||
sql::to_value(value, PQgetvalue(result_, static_cast<int>(row_index_), static_cast<int>(index)));
|
||||
}
|
||||
|
||||
void postgres_result_reader::read_value(const char *id, size_t index, unsigned short &value)
|
||||
{
|
||||
sql::to_value(value, PQgetvalue(result_, static_cast<int>(row_index_), static_cast<int>(index)));
|
||||
}
|
||||
|
||||
void postgres_result_reader::read_value(const char *id, size_t index, unsigned int &value)
|
||||
{
|
||||
sql::to_value(value, PQgetvalue(result_, static_cast<int>(row_index_), static_cast<int>(index)));
|
||||
}
|
||||
|
||||
void postgres_result_reader::read_value(const char *id, size_t index, unsigned long &value)
|
||||
{
|
||||
sql::to_value(value, PQgetvalue(result_, static_cast<int>(row_index_), static_cast<int>(index)));
|
||||
}
|
||||
|
||||
void postgres_result_reader::read_value(const char *id, size_t index, unsigned long long int &value)
|
||||
{
|
||||
sql::to_value(value, PQgetvalue(result_, static_cast<int>(row_index_), static_cast<int>(index)));
|
||||
}
|
||||
|
||||
void postgres_result_reader::read_value(const char *id, size_t index, bool &value)
|
||||
{
|
||||
sql::to_value(value, PQgetvalue(result_, static_cast<int>(row_index_), static_cast<int>(index)));
|
||||
}
|
||||
|
||||
void postgres_result_reader::read_value(const char *id, size_t index, float &value)
|
||||
{
|
||||
sql::to_value(value, PQgetvalue(result_, static_cast<int>(row_index_), static_cast<int>(index)));
|
||||
}
|
||||
|
||||
void postgres_result_reader::read_value(const char *id, size_t index, double &value)
|
||||
{
|
||||
sql::to_value(value, PQgetvalue(result_, static_cast<int>(row_index_), static_cast<int>(index)));
|
||||
}
|
||||
|
||||
void postgres_result_reader::read_value(const char *id, size_t index, char *value, size_t s)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void postgres_result_reader::read_value(const char *id, size_t index, std::string &value)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void postgres_result_reader::read_value(const char *id, size_t index, std::string &value, size_t s)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void postgres_result_reader::read_value(const char *id, size_t index, sql::any_type &value, sql::data_type_t type,
|
||||
size_t size)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user