ASyncDBConnection uses Qt signals now for reporting state changes and notices
This commit is contained in:
parent
90d0a14b63
commit
3befef2464
7 changed files with 30 additions and 73 deletions
|
|
@ -72,13 +72,10 @@ void ASyncDBConnection::doStateCallback(State state)
|
|||
m_connection.setNoticeReceiver(
|
||||
[this](const PGresult *result) { processNotice(result); });
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lg(m_stateCallback.m_mutex);
|
||||
if (m_stateCallback.m_func) {
|
||||
m_stateCallback.m_func(state);
|
||||
}
|
||||
emit onStateChanged(state);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
void ASyncDBConnection::closeConnection()
|
||||
|
|
@ -155,23 +152,8 @@ bool ASyncDBConnection::cancel()
|
|||
return m_canceller.cancel(nullptr);
|
||||
}
|
||||
|
||||
void ASyncDBConnection::setStateCallback(on_state_callback state_callback)
|
||||
{
|
||||
std::lock_guard<std::mutex> lg(m_stateCallback.m_mutex);
|
||||
m_stateCallback.m_func = state_callback;
|
||||
}
|
||||
|
||||
void ASyncDBConnection::setNoticeCallback(on_notice_callback notice_callback)
|
||||
{
|
||||
std::lock_guard<std::mutex> lg(m_noticeCallback.m_mutex);
|
||||
m_noticeCallback.m_func = notice_callback;
|
||||
}
|
||||
|
||||
void ASyncDBConnection::processNotice(const PGresult *result)
|
||||
{
|
||||
std::lock_guard<std::mutex> lg(m_noticeCallback.m_mutex);
|
||||
if (m_noticeCallback.m_func) {
|
||||
Pgsql::ErrorDetails details = Pgsql::ErrorDetails::createErrorDetailsFromPGresult(result);
|
||||
m_noticeCallback.m_func(details);
|
||||
}
|
||||
Pgsql::ErrorDetails details = Pgsql::ErrorDetails::createErrorDetailsFromPGresult(result);
|
||||
emit onNotice(details);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -11,21 +11,13 @@ DatabaseWindow::DatabaseWindow(QWidget *parent) :
|
|||
{
|
||||
ui->setupUi(this);
|
||||
|
||||
m_dbConnection.setStateCallback([this](ASyncDBConnection::State st)
|
||||
{
|
||||
QueueTask([this, st]() { connectionStateChanged(st); });
|
||||
});
|
||||
|
||||
m_dbConnection.setNoticeCallback([this](Pgsql::ErrorDetails details)
|
||||
{
|
||||
QueueTask([this, details]() { receiveNotice(details); });
|
||||
});
|
||||
connect(&m_dbConnection, &ASyncDBConnection::onStateChanged, this, &DatabaseWindow::connectionStateChanged);
|
||||
connect(&m_dbConnection, &ASyncDBConnection::onNotice, this, &DatabaseWindow::receiveNotice);
|
||||
}
|
||||
|
||||
DatabaseWindow::~DatabaseWindow()
|
||||
{
|
||||
m_dbConnection.closeConnection();
|
||||
m_dbConnection.setStateCallback(nullptr);
|
||||
delete ui;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,10 +1,9 @@
|
|||
#include "QueryTab.h"
|
||||
#include "ui_QueryTab.h"
|
||||
|
||||
#include "SqlSyntaxHighlighter.h"
|
||||
|
||||
#include <QStandardPaths>
|
||||
#include <QPushButton>
|
||||
|
||||
#include <QFileDialog>
|
||||
#include <QMessageBox>
|
||||
#include <QTabWidget>
|
||||
|
|
@ -68,16 +67,9 @@ QueryTab::QueryTab(MainWindow *win, QWidget *parent) :
|
|||
{
|
||||
ui->setupUi(this);
|
||||
|
||||
m_dbConnection.setStateCallback([this](ASyncDBConnection::State st)
|
||||
{
|
||||
m_win->QueueTask([this, st]() { connectionStateChanged(st); });
|
||||
});
|
||||
|
||||
m_dbConnection.setNoticeCallback([this](Pgsql::ErrorDetails details)
|
||||
{
|
||||
m_win->QueueTask([this, details]() { receiveNotice(details); });
|
||||
});
|
||||
|
||||
connect(&m_dbConnection, &ASyncDBConnection::onStateChanged, this, &QueryTab::connectionStateChanged);
|
||||
connect(&m_dbConnection, &ASyncDBConnection::onNotice, this, &QueryTab::receiveNotice);
|
||||
|
||||
QFont font;
|
||||
font.setFamily("Source Code Pro");
|
||||
font.setFixedPitch(true);
|
||||
|
|
@ -103,7 +95,6 @@ QueryTab::QueryTab(MainWindow *win, QWidget *parent) :
|
|||
QueryTab::~QueryTab()
|
||||
{
|
||||
m_dbConnection.closeConnection();
|
||||
m_dbConnection.setStateCallback(nullptr);
|
||||
|
||||
delete ui;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -73,14 +73,6 @@ public:
|
|||
bool isNew() const { return m_new; }
|
||||
private:
|
||||
|
||||
// struct ResultTab {
|
||||
// public:
|
||||
// std::shared_ptr<QueryResultModel> resultModel;
|
||||
// std::shared_ptr<TuplesResultWidget> tuplesResult;
|
||||
//// ResultTab(std::shared_ptr<QueryResultModel> rm, Ui::TuplesResult *tr)
|
||||
//// : resultModel(rm), tuplesResult(tr)
|
||||
//// {}
|
||||
// };
|
||||
using ResultTabContainer = std::vector<TuplesResultWidget*>;
|
||||
|
||||
Ui::QueryTab *ui;
|
||||
|
|
|
|||
|
|
@ -5,6 +5,8 @@
|
|||
#endif
|
||||
#include <memory>
|
||||
#include "GlobalIoService.h"
|
||||
#include "ASyncDBConnection.h"
|
||||
#include "Pgsql_Result.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
|
|
@ -22,6 +24,9 @@ int main(int argc, char *argv[])
|
|||
}
|
||||
#endif
|
||||
|
||||
qRegisterMetaType<ASyncDBConnection::State>();
|
||||
qRegisterMetaType<Pgsql::ErrorDetails>();
|
||||
|
||||
QApplication a(argc, argv);
|
||||
|
||||
QCoreApplication::setOrganizationName("pglab");
|
||||
|
|
|
|||
|
|
@ -132,4 +132,5 @@ namespace Pgsql {
|
|||
|
||||
} // end namespace Pgsql
|
||||
|
||||
|
||||
#endif // PGSQL_RESULT_H
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue