ASyncDBConnection uses Qt signals now for reporting state changes and notices

This commit is contained in:
Eelke Klein 2017-09-03 10:06:32 +02:00
parent 90d0a14b63
commit 3befef2464
7 changed files with 30 additions and 73 deletions

View file

@ -1,16 +1,14 @@
#ifndef ASYNCDBCONNECTION_H
#define ASYNCDBCONNECTION_H
#include <QObject>
#include "Pgsql_Connection.h"
#include "Pgsql_Params.h"
#include "Pgsql_Result.h"
#include "ConnectionConfig.h"
#include <QElapsedTimer>
// #include <functional>
// #include <condition_variable>
#include <mutex>
// #include <queue>
// #include <vector>
// #include <thread>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/io_service.hpp>
@ -19,7 +17,8 @@
* Queries are passed to this class with a routine to call on completion
* when the result is on that routine is called.
*/
class ASyncDBConnection {
class ASyncDBConnection: public QObject {
Q_OBJECT
public:
enum class State {
NotConnected,
@ -31,8 +30,6 @@ public:
};
using on_result_callback = std::function<void(std::shared_ptr<Pgsql::Result>, qint64)>;
using on_state_callback = std::function<void(State)>;
using on_notice_callback = std::function<void(Pgsql::ErrorDetails)>;
explicit ASyncDBConnection(boost::asio::io_service &ios);
~ASyncDBConnection();
@ -41,9 +38,6 @@ public:
void setupConnection(const ConnectionConfig &config);
void closeConnection();
void setStateCallback(on_state_callback state_callback);
void setNoticeCallback(on_notice_callback notice_callback);
/** Sends command to the server.
When the result is in on_result will be called directly within the thread.
@ -54,6 +48,10 @@ public:
bool send(const std::string &command, Pgsql::Params params, on_result_callback on_result);
bool cancel();
signals:
void onStateChanged(ASyncDBConnection::State state);
void onNotice(Pgsql::ErrorDetails notice);
private:
Pgsql::Connection m_connection;
@ -61,15 +59,7 @@ private:
ConnectionConfig m_config;
State m_state = State::NotConnected;
Pgsql::Canceller m_canceller;
struct {
std::mutex m_mutex;
on_state_callback m_func;
} m_stateCallback;
struct {
std::mutex m_mutex;
on_notice_callback m_func;
} m_noticeCallback;
QElapsedTimer m_timer;
void async_connect_handler(boost::system::error_code ec, std::size_t s);
@ -78,4 +68,8 @@ private:
void processNotice(const PGresult *result);
};
Q_DECLARE_METATYPE(ASyncDBConnection::State);
Q_DECLARE_METATYPE(Pgsql::ErrorDetails);
#endif // ASYNCDBCONNECTION_H