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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user