2017-01-03 07:22:36 +01:00
|
|
|
|
#ifndef ASYNCDBCONNECTION_H
|
|
|
|
|
|
#define ASYNCDBCONNECTION_H
|
|
|
|
|
|
|
2018-01-08 20:54:03 +01:00
|
|
|
|
#include <QObject>
|
|
|
|
|
|
|
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-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
|
2018-01-08 20:54:03 +01:00
|
|
|
|
* when the result is on that routine is called.
|
2017-02-02 07:18:44 +01:00
|
|
|
|
*/
|
2018-01-08 20:54:03 +01:00
|
|
|
|
class ASyncDBConnection: public QObject {
|
|
|
|
|
|
Q_OBJECT
|
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();
|
|
|
|
|
|
|
2018-01-08 20:54:03 +01:00
|
|
|
|
/** Sends command to the server.
|
2017-01-03 07:22:36 +01:00
|
|
|
|
|
2018-01-08 20:54:03 +01:00
|
|
|
|
When the result is in on_result will be called directly within the thread.
|
|
|
|
|
|
|
|
|
|
|
|
If the command gives multiple results on_result will be called for each result.
|
|
|
|
|
|
*/
|
2017-01-03 07:22:36 +01:00
|
|
|
|
bool send(const std::string &command, on_result_callback on_result);
|
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
|
|
|
|
|
2018-01-08 20:54:03 +01:00
|
|
|
|
signals:
|
|
|
|
|
|
void onStateChanged(ASyncDBConnection::State state);
|
|
|
|
|
|
void onNotice(Pgsql::ErrorDetails notice);
|
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;
|
2018-01-08 20:54:03 +01:00
|
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
|
|
2018-01-08 20:54:03 +01:00
|
|
|
|
Q_DECLARE_METATYPE(ASyncDBConnection::State);
|
|
|
|
|
|
Q_DECLARE_METATYPE(Pgsql::ErrorDetails);
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-01-03 07:22:36 +01:00
|
|
|
|
#endif // ASYNCDBCONNECTION_H
|