2017-01-03 07:22:36 +01:00
|
|
|
|
#ifndef ASYNCDBCONNECTION_H
|
|
|
|
|
|
#define ASYNCDBCONNECTION_H
|
|
|
|
|
|
|
2017-08-26 11:45:50 +02:00
|
|
|
|
#include "Pgsql_Connection.h"
|
2017-02-19 11:12:43 +01:00
|
|
|
|
#include "Pgsql_Params.h"
|
2017-09-03 10:06:32 +02:00
|
|
|
|
#include "Pgsql_Result.h"
|
2017-09-10 10:11:58 +02:00
|
|
|
|
#include "Expected.h"
|
2017-08-23 08:10:01 +02:00
|
|
|
|
#include "ConnectionConfig.h"
|
2017-01-25 06:54:21 +01:00
|
|
|
|
#include <QElapsedTimer>
|
2017-12-28 07:28:21 +01:00
|
|
|
|
#include <functional>
|
2017-01-06 07:23:40 +01:00
|
|
|
|
#include <mutex>
|
2017-08-24 21:12:32 +02:00
|
|
|
|
#include <boost/asio/ip/tcp.hpp>
|
|
|
|
|
|
#include <boost/asio/io_service.hpp>
|
2017-01-03 07:22:36 +01:00
|
|
|
|
|
2017-02-02 07:18:44 +01:00
|
|
|
|
/** \brief Class that handles asynchronous execution of queries.
|
|
|
|
|
|
*
|
|
|
|
|
|
* Queries are passed to this class with a routine to call on completion
|
2017-12-28 07:28:21 +01:00
|
|
|
|
* when the result is received that routine is called. The asynchronous
|
|
|
|
|
|
* processing is done using boost::asio.
|
|
|
|
|
|
*
|
|
|
|
|
|
* You can cancel a running query but even then you have to wait for the result.
|
2017-02-02 07:18:44 +01:00
|
|
|
|
*/
|
2017-12-28 07:28:21 +01:00
|
|
|
|
class ASyncDBConnection {
|
2017-01-03 07:22:36 +01:00
|
|
|
|
public:
|
|
|
|
|
|
enum class State {
|
|
|
|
|
|
NotConnected,
|
|
|
|
|
|
Connecting,
|
2017-08-25 08:37:18 +02:00
|
|
|
|
Connected, ///< connected and idle
|
|
|
|
|
|
QuerySend, ///< connected query send expecting result
|
|
|
|
|
|
CancelSend, ///< cancel send expecting result
|
|
|
|
|
|
Terminating ///< shutting down
|
2017-01-03 07:22:36 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
2017-09-10 10:11:58 +02:00
|
|
|
|
using on_result_callback = std::function<void(Expected<std::shared_ptr<Pgsql::Result>>, qint64)>;
|
2017-01-03 07:22:36 +01:00
|
|
|
|
|
2017-08-25 08:37:18 +02:00
|
|
|
|
explicit ASyncDBConnection(boost::asio::io_service &ios);
|
|
|
|
|
|
~ASyncDBConnection();
|
2017-01-03 07:22:36 +01:00
|
|
|
|
|
2017-01-15 21:38:07 +01:00
|
|
|
|
State state() const;
|
2017-01-15 21:01:40 +01:00
|
|
|
|
void setupConnection(const ConnectionConfig &config);
|
2017-01-03 07:22:36 +01:00
|
|
|
|
void closeConnection();
|
|
|
|
|
|
|
2017-12-28 07:28:21 +01:00
|
|
|
|
/** \defgroup send These function send queries to the server.
|
|
|
|
|
|
*
|
|
|
|
|
|
* The query string can actually contain multiple queries.
|
|
|
|
|
|
* These functions report their results through a callback function.
|
|
|
|
|
|
* If the command gives multiple results on_result will be called for each result.
|
|
|
|
|
|
*
|
|
|
|
|
|
* \note when the io_service that was passed to the constructor is running
|
|
|
|
|
|
* on a different thread the callback also will be done on that thread. Forward
|
|
|
|
|
|
* the call to your own thread if needed.
|
|
|
|
|
|
*
|
|
|
|
|
|
* \note while you can pass multiple queries to a single send call it is not allowed to
|
|
|
|
|
|
* send new queries while not all results have been received.
|
|
|
|
|
|
*/
|
2017-01-03 07:22:36 +01:00
|
|
|
|
|
2017-12-28 07:28:21 +01:00
|
|
|
|
/** \ingroup send
|
|
|
|
|
|
* \brief Function to send a command without parameters to the server.
|
|
|
|
|
|
*/
|
2017-01-03 07:22:36 +01:00
|
|
|
|
bool send(const std::string &command, on_result_callback on_result);
|
2017-12-28 07:28:21 +01:00
|
|
|
|
/** \ingroup send
|
|
|
|
|
|
* \brief Function to send a a command with parameters to the server.
|
|
|
|
|
|
*/
|
2017-02-19 11:12:43 +01:00
|
|
|
|
bool send(const std::string &command, Pgsql::Params params, on_result_callback on_result);
|
2017-01-03 07:22:36 +01:00
|
|
|
|
|
2017-01-08 09:58:34 +01:00
|
|
|
|
bool cancel();
|
2017-09-03 10:06:32 +02:00
|
|
|
|
|
2017-12-28 07:28:21 +01:00
|
|
|
|
using StateChangeReceiver = std::function<void(ASyncDBConnection::State)>;
|
|
|
|
|
|
void setStateChangeReceiver(StateChangeReceiver state_change_receiver);
|
|
|
|
|
|
|
|
|
|
|
|
using NoticeReceiver = std::function<void(Pgsql::ErrorDetails)>;
|
|
|
|
|
|
void setNoticeReceiver(NoticeReceiver notice_receiver);
|
2017-01-08 09:58:34 +01:00
|
|
|
|
|
2017-01-03 07:22:36 +01:00
|
|
|
|
private:
|
2017-08-24 21:12:32 +02:00
|
|
|
|
Pgsql::Connection m_connection;
|
|
|
|
|
|
boost::asio::ip::tcp::socket m_asioSock;
|
|
|
|
|
|
ConnectionConfig m_config;
|
|
|
|
|
|
State m_state = State::NotConnected;
|
2017-08-25 08:37:18 +02:00
|
|
|
|
Pgsql::Canceller m_canceller;
|
2017-12-28 07:28:21 +01:00
|
|
|
|
StateChangeReceiver m_stateChangeReceiver;
|
|
|
|
|
|
NoticeReceiver m_noticeReceiver;
|
|
|
|
|
|
|
2017-08-24 21:12:32 +02:00
|
|
|
|
QElapsedTimer m_timer;
|
|
|
|
|
|
|
|
|
|
|
|
void async_connect_handler(boost::system::error_code ec, std::size_t s);
|
|
|
|
|
|
void async_query_handler(boost::system::error_code ec, std::size_t s, on_result_callback on_result);
|
|
|
|
|
|
void doStateCallback(State state);
|
|
|
|
|
|
void processNotice(const PGresult *result);
|
2017-01-03 07:22:36 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif // ASYNCDBCONNECTION_H
|