Decided to use signal slot mechanism
The automatic cross thread activation is probably worth it.
This commit is contained in:
parent
14ab400ccb
commit
abd4020ddf
3 changed files with 34 additions and 63 deletions
|
|
@ -32,10 +32,8 @@ QueryTab::QueryTab(MainWindow *win, QWidget *parent) :
|
||||||
{
|
{
|
||||||
ui->setupUi(this);
|
ui->setupUi(this);
|
||||||
|
|
||||||
m_dbConnection.setStateChangeReceiver(
|
connect(&m_dbConnection, &ASyncDBConnection::onStateChanged, this, &QueryTab::connectionStateChanged);
|
||||||
[this, win](auto s) { win->QueueTask([this, s]() { connectionStateChanged(s); }); });
|
connect(&m_dbConnection, &ASyncDBConnection::onNotice, this, &QueryTab::receiveNotice);
|
||||||
m_dbConnection.setNoticeReceiver(
|
|
||||||
[this, win](auto n) { win->QueueTask([this, n]() { receiveNotice(n); }); });
|
|
||||||
|
|
||||||
QFont font;
|
QFont font;
|
||||||
font.setFamily("Source Code Pro");
|
font.setFamily("Source Code Pro");
|
||||||
|
|
|
||||||
|
|
@ -4,19 +4,19 @@
|
||||||
|
|
||||||
using namespace boost::asio;
|
using namespace boost::asio;
|
||||||
|
|
||||||
//namespace {
|
namespace {
|
||||||
|
|
||||||
// class registerMetaTypes {
|
class registerMetaTypes {
|
||||||
// public:
|
public:
|
||||||
// registerMetaTypes()
|
registerMetaTypes()
|
||||||
// {
|
{
|
||||||
// qRegisterMetaType<ASyncDBConnection::State>();
|
qRegisterMetaType<ASyncDBConnection::State>();
|
||||||
// qRegisterMetaType<Pgsql::ErrorDetails>();
|
qRegisterMetaType<Pgsql::ErrorDetails>();
|
||||||
// }
|
}
|
||||||
// } registerMetaTypes_instance;
|
} registerMetaTypes_instance;
|
||||||
|
|
||||||
|
|
||||||
//}
|
}
|
||||||
|
|
||||||
ASyncDBConnection::ASyncDBConnection(boost::asio::io_service &ios)
|
ASyncDBConnection::ASyncDBConnection(boost::asio::io_service &ios)
|
||||||
: m_asioSock(ios)
|
: m_asioSock(ios)
|
||||||
|
|
@ -87,8 +87,7 @@ void ASyncDBConnection::doStateCallback(State state)
|
||||||
m_connection.setNoticeReceiver(
|
m_connection.setNoticeReceiver(
|
||||||
[this](const PGresult *result) { processNotice(result); });
|
[this](const PGresult *result) { processNotice(result); });
|
||||||
}
|
}
|
||||||
if (m_stateChangeReceiver)
|
emit onStateChanged(state);
|
||||||
m_stateChangeReceiver(state);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -137,7 +136,7 @@ void ASyncDBConnection::async_query_handler(boost::system::error_code ec, std::s
|
||||||
while ( ! finished && ! m_connection.isBusy()) {
|
while ( ! finished && ! m_connection.isBusy()) {
|
||||||
auto res = m_connection.getResult();
|
auto res = m_connection.getResult();
|
||||||
qint64 ms = m_timer.restart();
|
qint64 ms = m_timer.restart();
|
||||||
on_result(res, ms); // do we really want to send null result to???
|
on_result(res, ms);
|
||||||
if (res == nullptr) {
|
if (res == nullptr) {
|
||||||
m_timer.invalidate();
|
m_timer.invalidate();
|
||||||
doStateCallback(State::Connected);
|
doStateCallback(State::Connected);
|
||||||
|
|
@ -170,16 +169,5 @@ bool ASyncDBConnection::cancel()
|
||||||
void ASyncDBConnection::processNotice(const PGresult *result)
|
void ASyncDBConnection::processNotice(const PGresult *result)
|
||||||
{
|
{
|
||||||
Pgsql::ErrorDetails details = Pgsql::ErrorDetails::createErrorDetailsFromPGresult(result);
|
Pgsql::ErrorDetails details = Pgsql::ErrorDetails::createErrorDetailsFromPGresult(result);
|
||||||
if (m_noticeReceiver)
|
emit onNotice(details);
|
||||||
m_noticeReceiver(details);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ASyncDBConnection::setStateChangeReceiver(StateChangeReceiver state_change_receiver)
|
|
||||||
{
|
|
||||||
m_stateChangeReceiver = state_change_receiver;
|
|
||||||
}
|
|
||||||
|
|
||||||
void ASyncDBConnection::setNoticeReceiver(NoticeReceiver notice_receiver)
|
|
||||||
{
|
|
||||||
m_noticeReceiver = notice_receiver;
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,14 @@
|
||||||
#ifndef ASYNCDBCONNECTION_H
|
#ifndef ASYNCDBCONNECTION_H
|
||||||
#define ASYNCDBCONNECTION_H
|
#define ASYNCDBCONNECTION_H
|
||||||
|
|
||||||
|
#include <QObject>
|
||||||
|
|
||||||
#include "Pgsql_Connection.h"
|
#include "Pgsql_Connection.h"
|
||||||
#include "Pgsql_Params.h"
|
#include "Pgsql_Params.h"
|
||||||
#include "Pgsql_Result.h"
|
#include "Pgsql_Result.h"
|
||||||
#include "Expected.h"
|
#include "Expected.h"
|
||||||
#include "ConnectionConfig.h"
|
#include "ConnectionConfig.h"
|
||||||
#include <QElapsedTimer>
|
#include <QElapsedTimer>
|
||||||
#include <functional>
|
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <boost/asio/ip/tcp.hpp>
|
#include <boost/asio/ip/tcp.hpp>
|
||||||
#include <boost/asio/io_service.hpp>
|
#include <boost/asio/io_service.hpp>
|
||||||
|
|
@ -15,12 +16,10 @@
|
||||||
/** \brief Class that handles asynchronous execution of queries.
|
/** \brief Class that handles asynchronous execution of queries.
|
||||||
*
|
*
|
||||||
* Queries are passed to this class with a routine to call on completion
|
* Queries are passed to this class with a routine to call on completion
|
||||||
* when the result is received that routine is called. The asynchronous
|
* when the result is on that routine is called.
|
||||||
* processing is done using boost::asio.
|
|
||||||
*
|
|
||||||
* You can cancel a running query but even then you have to wait for the result.
|
|
||||||
*/
|
*/
|
||||||
class ASyncDBConnection {
|
class ASyncDBConnection: public QObject {
|
||||||
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
enum class State {
|
enum class State {
|
||||||
NotConnected,
|
NotConnected,
|
||||||
|
|
@ -40,36 +39,20 @@ public:
|
||||||
void setupConnection(const ConnectionConfig &config);
|
void setupConnection(const ConnectionConfig &config);
|
||||||
void closeConnection();
|
void closeConnection();
|
||||||
|
|
||||||
/** \defgroup send These function send queries to the server.
|
/** Sends command 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.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/** \ingroup send
|
When the result is in on_result will be called directly within the thread.
|
||||||
* \brief Function to send a command without parameters to the server.
|
|
||||||
*/
|
If the command gives multiple results on_result will be called for each result.
|
||||||
|
*/
|
||||||
bool send(const std::string &command, on_result_callback on_result);
|
bool send(const std::string &command, on_result_callback on_result);
|
||||||
/** \ingroup send
|
|
||||||
* \brief Function to send a a command with parameters to the server.
|
|
||||||
*/
|
|
||||||
bool send(const std::string &command, Pgsql::Params params, on_result_callback on_result);
|
bool send(const std::string &command, Pgsql::Params params, on_result_callback on_result);
|
||||||
|
|
||||||
bool cancel();
|
bool cancel();
|
||||||
|
|
||||||
using StateChangeReceiver = std::function<void(ASyncDBConnection::State)>;
|
signals:
|
||||||
void setStateChangeReceiver(StateChangeReceiver state_change_receiver);
|
void onStateChanged(ASyncDBConnection::State state);
|
||||||
|
void onNotice(Pgsql::ErrorDetails notice);
|
||||||
using NoticeReceiver = std::function<void(Pgsql::ErrorDetails)>;
|
|
||||||
void setNoticeReceiver(NoticeReceiver notice_receiver);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Pgsql::Connection m_connection;
|
Pgsql::Connection m_connection;
|
||||||
|
|
@ -77,8 +60,6 @@ private:
|
||||||
ConnectionConfig m_config;
|
ConnectionConfig m_config;
|
||||||
State m_state = State::NotConnected;
|
State m_state = State::NotConnected;
|
||||||
Pgsql::Canceller m_canceller;
|
Pgsql::Canceller m_canceller;
|
||||||
StateChangeReceiver m_stateChangeReceiver;
|
|
||||||
NoticeReceiver m_noticeReceiver;
|
|
||||||
|
|
||||||
QElapsedTimer m_timer;
|
QElapsedTimer m_timer;
|
||||||
|
|
||||||
|
|
@ -88,4 +69,8 @@ private:
|
||||||
void processNotice(const PGresult *result);
|
void processNotice(const PGresult *result);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Q_DECLARE_METATYPE(ASyncDBConnection::State);
|
||||||
|
Q_DECLARE_METATYPE(Pgsql::ErrorDetails);
|
||||||
|
|
||||||
|
|
||||||
#endif // ASYNCDBCONNECTION_H
|
#endif // ASYNCDBCONNECTION_H
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue