diff --git a/CMakeLists.txt b/CMakeLists.txt index 0aff6ba..044da69 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -94,7 +94,7 @@ add_executable(pglab pglab/PgDatabaseContainer.cpp pglab/PgDatabase.cpp pglab/PgNamespace.cpp - pglab/PgsqlConn.cpp + pglab/Pgsql_Connection.cpp pglab/Pgsql_Params.cpp pglab/Pgsql_Result.cpp pglab/Pgsql_Row.cpp diff --git a/pglab/ASyncDBConnection.h b/pglab/ASyncDBConnection.h index 4d16f54..6ddb358 100644 --- a/pglab/ASyncDBConnection.h +++ b/pglab/ASyncDBConnection.h @@ -1,7 +1,7 @@ #ifndef ASYNCDBCONNECTION_H #define ASYNCDBCONNECTION_H -#include "PgsqlConn.h" +#include "Pgsql_Connection.h" #include "Pgsql_Params.h" #include "ConnectionConfig.h" #include diff --git a/pglab/MainWindow.h b/pglab/MainWindow.h index b1fa2cb..438b66f 100644 --- a/pglab/MainWindow.h +++ b/pglab/MainWindow.h @@ -9,7 +9,7 @@ #include #include #include -#include "PgsqlConn.h" +#include "Pgsql_Connection.h" #include #include #include diff --git a/pglab/OpenDatabase.cpp b/pglab/OpenDatabase.cpp index 027b33b..f00cf5d 100644 --- a/pglab/OpenDatabase.cpp +++ b/pglab/OpenDatabase.cpp @@ -1,6 +1,6 @@ #include "OpenDatabase.h" #include "PgDatabaseCatalogue.h" -#include "PgsqlConn.h" +#include "Pgsql_Connection.h" #include "TypeSelectionItemModel.h" Expected OpenDatabase::createOpenDatabase(const ConnectionConfig &cfg) diff --git a/pglab/PgAuthIdContainer.cpp b/pglab/PgAuthIdContainer.cpp index f39dac3..6c3a4a7 100644 --- a/pglab/PgAuthIdContainer.cpp +++ b/pglab/PgAuthIdContainer.cpp @@ -1,5 +1,5 @@ #include "PgAuthIdContainer.h" -#include "PgsqlConn.h" +#include "Pgsql_Connection.h" #include "PgDatabaseCatalogue.h" PgAuthIdContainer::PgAuthIdContainer(PgDatabaseCatalogue *cat) diff --git a/pglab/PgDatabaseCatalogue.cpp b/pglab/PgDatabaseCatalogue.cpp index 5f00aa7..ca53c0c 100644 --- a/pglab/PgDatabaseCatalogue.cpp +++ b/pglab/PgDatabaseCatalogue.cpp @@ -2,7 +2,7 @@ #include "PgTypeContainer.h" #include "PgDatabaseContainer.h" #include "PgAuthIdContainer.h" -#include "PgsqlConn.h" +#include "Pgsql_Connection.h" QString getRoleNameFromOid(const PgDatabaseCatalogue *cat, Oid oid) diff --git a/pglab/PgDatabaseContainer.cpp b/pglab/PgDatabaseContainer.cpp index e0a5977..ba5b729 100644 --- a/pglab/PgDatabaseContainer.cpp +++ b/pglab/PgDatabaseContainer.cpp @@ -1,5 +1,5 @@ #include "PgDatabaseContainer.h" -#include "PgsqlConn.h" +#include "Pgsql_Connection.h" PgDatabaseContainer::PgDatabaseContainer(PgDatabaseCatalogue *cat) : PgContainer(cat) diff --git a/pglab/PgType.cpp b/pglab/PgType.cpp index 7b19a7a..ba81860 100644 --- a/pglab/PgType.cpp +++ b/pglab/PgType.cpp @@ -1,5 +1,5 @@ #include "PgType.h" -#include "PgsqlConn.h" +#include "Pgsql_Connection.h" PgType::PgType() = default; diff --git a/pglab/PgTypeContainer.cpp b/pglab/PgTypeContainer.cpp index fc10f7a..311e112 100644 --- a/pglab/PgTypeContainer.cpp +++ b/pglab/PgTypeContainer.cpp @@ -1,5 +1,5 @@ #include "PgTypeContainer.h" -#include "PgsqlConn.h" +#include "Pgsql_Connection.h" #include PgTypeContainer::PgTypeContainer(PgDatabaseCatalogue *cat) diff --git a/pglab/PgsqlConn.cpp b/pglab/PgsqlConn.cpp deleted file mode 100644 index 78b37d5..0000000 --- a/pglab/PgsqlConn.cpp +++ /dev/null @@ -1,206 +0,0 @@ -#include "PgsqlConn.h" -#include "Pgsql_declare.h" -#include "Pgsql_Params.h" -#include - - -using namespace Pgsql; - - - - -Canceller::Canceller(PGcancel *c) - : m_cancel(c) -{} - -Canceller::Canceller(Canceller&& rhs) - : m_cancel(rhs.m_cancel) -{ - rhs.m_cancel = nullptr; -} - -Canceller& Canceller::operator=(Canceller&& rhs) -{ - if (m_cancel) { - PQfreeCancel(m_cancel); - } - m_cancel = rhs.m_cancel; - rhs.m_cancel = nullptr; - return *this; -} - -Canceller::~Canceller() -{ - if (m_cancel) { - PQfreeCancel(m_cancel); - } -} - -bool Canceller::cancel(std::string *error) -{ - const int errbuf_size = 256; - char errbuf[errbuf_size]; - bool res = PQcancel(m_cancel, errbuf, errbuf_size); - if (!res && error) { - *error = errbuf; - } - return res; -} - - - -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; - } -} - -Canceller Connection::getCancel() -{ - Canceller c(PQgetCancel(conn)); - return c; -} - -bool Connection::connect(const char *params) -{ - bool result = false; - conn = PQconnectdb(params); - if (conn) { - ConnStatusType status = PQstatus(conn); - result = (status == CONNECTION_OK); - } - return result; -} - -bool Connection::connect(const char *const * keywords, const char* const * values, int expand_dbname) -{ - bool result = false; - conn = PQconnectdbParams(keywords, values, expand_dbname); - if (conn) { - ConnStatusType status = PQstatus(conn); - result = (status == CONNECTION_OK); - } - return result; -} - -bool Connection::connectStart(const char* params) -{ - conn = PQconnectStart(params); - return conn != nullptr; -} - -bool Connection::connectStart(const char * const *keywords, - const char * const *values) -{ - conn = PQconnectStartParams(keywords, values, 0); - return conn != nullptr; -} - -PostgresPollingStatusType Connection::connectPoll() -{ - return PQconnectPoll(conn); -} - -ConnStatusType Connection::status() -{ - return PQstatus(conn); -} - -int Connection::socket() -{ - return PQsocket(conn); -} - -std::string Connection::getErrorMessage() const -{ - std::string result; - if (conn) { - result = PQerrorMessage(conn); - } - else { - result = "no connection"; - } - return result; -} - -Result Connection::query(const char * command) -{ - PGresult *result = PQexec(conn, command); - return Result(result); -} - - -bool Connection::sendQuery(const char *query) -{ - int res = PQsendQuery(conn, query); - return res == 1; -} - -bool Connection::sendQueryParams(const char * command, const Params ¶ms) -{ - int res = PQsendQueryParams(conn, command, params.size(), params.types(), - params.values(), params.lengths(), params.formats(), - 0); // text format - - return res == 1; -} - -std::shared_ptr Connection::getResult() -{ - PGresult *r = PQgetResult(conn); - if (r) { - return std::make_shared(r); - } - else { - return nullptr; - } -} - -bool Connection::consumeInput() -{ - int res = PQconsumeInput(conn); - return res == 1; -} - -bool Connection::isBusy() -{ - int res = PQisBusy(conn); - return res == 1; -} - -void Connection::setNoticeReceiver(std::function callback) -{ - notifyReceiver = callback; - PQsetNoticeReceiver(conn, - Connection::notifyReceiveFunc - , reinterpret_cast(this)); -} - -void Connection::notifyReceiveFunc(void *arg, const PGresult *result) -{ - Connection *c = reinterpret_cast(arg); - c->notifyReceiver(result); -} diff --git a/pglab/PgsqlConn.h b/pglab/PgsqlConn.h deleted file mode 100644 index 74dbf1c..0000000 --- a/pglab/PgsqlConn.h +++ /dev/null @@ -1,217 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include - -#include -#include -#include - -#include - -#include "Pgsql_Result.h" - -namespace Pgsql { - - class Connection; - /* - This library has multiple layers. - - Layer 1 delivers lowlevel C++ wrappers for the basic libpq functionality. Adding - automatic resource management. - - */ - -// class ConnectionParams { -// public: -// std::string host; -// std::string hostaddr; -// unsigned short port = 5432; -// std::string dbname; -// std::string user; -// std::string password; -// int connect_timeout = -1; ///< -1 omit (ie uses default) -// std::string application_name; - -// }; - - - - class Result; - class Params; - - - - - - /** \brief Wrapper for a cancel object from libpq. - */ - class Canceller { - public: - Canceller() = default; - Canceller(PGcancel *c); - Canceller(const Canceller&) = delete; - Canceller& operator=(const Canceller&) = delete; - Canceller(Canceller&& rhs); - Canceller& operator=(Canceller&& rhs); - ~Canceller(); - - bool cancel(std::string *error); - private: - PGcancel *m_cancel = nullptr; - }; - - /** \brief Class for connecting to the database. - * - * The class isolates the programmer from the worst C style parts - * of the libpq API but is mostly a very thin wrapper. - */ - class Connection { - public: - Connection(); - ~Connection(); - - Connection(const Connection &rhs) = delete; - Connection& operator=(const Connection &rhs) = delete; - - Connection(Connection &&rhs); - Connection& operator=(Connection &&rhs); - -// void connect(const ConnectionParams ¶ms); - bool connect(const char *params); - bool connect(const QString ¶ms) - { - return connect(params.toUtf8().data()); - } - bool connect(const char *const * keywords, const char* const * values, int expand_dbname); - - bool connectStart(const char *params); - - bool connectStart(const std::string ¶ms) - { - return connectStart(params.c_str()); - } - - bool connectStart(const QString ¶ms) - { - return connectStart(params.toUtf8().data()); - } - bool connectStart(const char * const *keywords, - const char * const *values); - - PostgresPollingStatusType connectPoll(); - ConnStatusType status(); - int socket(); - - void close(); - Canceller getCancel(); - - std::string getErrorMessage() const; - - Result query(const char * command); - Result query(const QString &command) - { - return query(command.toUtf8().data()); - } - - bool sendQuery(const char * query); - bool sendQuery(const std::string &command) - { - return sendQuery(command.c_str()); - } - bool sendQuery(const QString &command) - { - return sendQuery(command.toUtf8().data()); - } - - bool sendQueryParams(const char * command, const Params ¶ms); - - std::shared_ptr getResult(); - - bool consumeInput(); - bool isBusy(); - - void setNoticeReceiver(std::function callback); - private: - PGconn *conn = nullptr; - std::function notifyReceiver; - - static void notifyReceiveFunc(void *arg, const PGresult *result); - }; - - - - -// class Field { -// public: - -// std::string getName() const; - -// private: -// //Tuples tuples; -// int field; -// // - -// }; - -// class Tuples { -// public: -// int getRowCount() const; -// int getColCount() const; - -// Field getField(const std::string &fname); -// Field getField(const int idx); - -// class const_iterator { -// public: -// const_iterator& operator++(); -// }; - -// void rewind(); - - -// }; - -// class OkResult: public Result { -// public: -// /** If the result is a data result then returns an interface for processing this data. - -// The returned interface remains valid as long as this OkResult exists. -// */ -// Tuples* hasTuples(); -// }; - -// class ErrorResult: public Result { -// -// }; - -// class ITransaction { -// public: -// -// Canceller Query(std::string query, -// std::function on_success, -// std::function on_cancelled, -// std::function on_error); -// -// Canceller Query(std::string query, -// std::function on_success, -// std::function on_cancelled, -// std::function on_error); -// -// -// void Rollback( -// std::function on_success, -// std::function on_error); -// void Commit( -// std::function on_success, -// std::function on_cancelled, -// std::function on_error); -// -// }; -} - - - diff --git a/pglab/Pgsql_Connection.cpp b/pglab/Pgsql_Connection.cpp index 78b37d5..9555434 100644 --- a/pglab/Pgsql_Connection.cpp +++ b/pglab/Pgsql_Connection.cpp @@ -1,4 +1,4 @@ -#include "PgsqlConn.h" +#include "Pgsql_Connection.h" #include "Pgsql_declare.h" #include "Pgsql_Params.h" #include diff --git a/pglab/QueryResultModel.h b/pglab/QueryResultModel.h index f137293..ed8f21a 100644 --- a/pglab/QueryResultModel.h +++ b/pglab/QueryResultModel.h @@ -2,7 +2,7 @@ #define QUERYRESULTMODEL_H #include -#include "PgsqlConn.h" +#include "Pgsql_Connection.h" class QueryResultModel : public QAbstractTableModel {