#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; /** \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()); } 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); // // }; }