Initial commit. Contains a simple query tool.
This commit is contained in:
commit
edc6df25da
19 changed files with 1245 additions and 0 deletions
139
PgsqlConn.cpp
Normal file
139
PgsqlConn.cpp
Normal file
|
|
@ -0,0 +1,139 @@
|
|||
#include "PgsqlConn.h"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
|
||||
using namespace Pgsql;
|
||||
|
||||
|
||||
Result::Result(PGresult *res)
|
||||
: result(res)
|
||||
{
|
||||
if (res == nullptr) {
|
||||
throw std::runtime_error("Passing nullptr to Result::Result is not allowed");
|
||||
}
|
||||
}
|
||||
|
||||
Result::~Result()
|
||||
{
|
||||
PQclear(result);
|
||||
}
|
||||
|
||||
|
||||
Result::Result(Result &&rhs)
|
||||
: result(rhs.result)
|
||||
{
|
||||
rhs.result = nullptr;
|
||||
}
|
||||
|
||||
Result& Result::operator=(Result &&rhs)
|
||||
{
|
||||
if (result) {
|
||||
PQclear(result);
|
||||
}
|
||||
result = rhs.result;
|
||||
rhs.result = nullptr;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Result::operator bool() const
|
||||
{
|
||||
return result != nullptr;
|
||||
}
|
||||
|
||||
ExecStatusType Result::getResultStatus()
|
||||
{
|
||||
return PQresultStatus(result);
|
||||
}
|
||||
|
||||
std::string Result::getResStatus()
|
||||
{
|
||||
// return PQresStatus(result);
|
||||
return "";
|
||||
}
|
||||
|
||||
int Result::getRows() const
|
||||
{
|
||||
return PQntuples(result);
|
||||
}
|
||||
|
||||
int Result::getCols() const
|
||||
{
|
||||
return PQnfields(result);
|
||||
}
|
||||
|
||||
const char* const Result::getColName(int idx) const
|
||||
{
|
||||
return PQfname(result, idx);
|
||||
}
|
||||
|
||||
const char * Result::getVal(int col, int row) const
|
||||
{
|
||||
return PQgetvalue(result, row, col);
|
||||
}
|
||||
|
||||
|
||||
Connection::Connection() = default;
|
||||
|
||||
Connection::~Connection()
|
||||
{
|
||||
close();
|
||||
}
|
||||
|
||||
Connection::Connection(Connection &&rhs)
|
||||
: conn(rhs.conn)
|
||||
{
|
||||
rhs.conn = nullptr;
|
||||
}
|
||||
|
||||
Connection& Connection::operator=(Connection &&rhs)
|
||||
{
|
||||
close();
|
||||
conn = rhs.conn;
|
||||
rhs.conn = nullptr;
|
||||
return *this;
|
||||
}
|
||||
|
||||
void Connection::close()
|
||||
{
|
||||
if (conn) {
|
||||
PQfinish(conn);
|
||||
conn = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool Connection::connect(const char *params)
|
||||
{
|
||||
bool result = false;
|
||||
conn = PQconnectdb(params);
|
||||
if (conn) {
|
||||
ConnStatusType status = PQstatus(conn);
|
||||
result = (status == CONNECTION_OK);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
std::string Connection::getErrorMessage() const
|
||||
{
|
||||
std::string result;
|
||||
if (conn) {
|
||||
result = PQerrorMessage(conn);
|
||||
}
|
||||
else {
|
||||
result = "no connection";
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
Result Connection::Query(const char * query)
|
||||
{
|
||||
PGresult *result = PQexec(conn, query);
|
||||
if (result) {
|
||||
return Result(result);
|
||||
}
|
||||
else {
|
||||
throw std::runtime_error("Failed to allocate result object");
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue