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(
|
m_connection.setNoticeReceiver(
|
||||||
[this](const PGresult *result) { processNotice(result); });
|
[this](const PGresult *result) { processNotice(result); });
|
||||||
}
|
}
|
||||||
|
emit onStateChanged(state);
|
||||||
std::lock_guard<std::mutex> lg(m_stateCallback.m_mutex);
|
}
|
||||||
if (m_stateCallback.m_func) {
|
|
||||||
m_stateCallback.m_func(state);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void ASyncDBConnection::closeConnection()
|
void ASyncDBConnection::closeConnection()
|
||||||
|
|
@ -155,23 +152,8 @@ bool ASyncDBConnection::cancel()
|
||||||
return m_canceller.cancel(nullptr);
|
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)
|
void ASyncDBConnection::processNotice(const PGresult *result)
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lg(m_noticeCallback.m_mutex);
|
Pgsql::ErrorDetails details = Pgsql::ErrorDetails::createErrorDetailsFromPGresult(result);
|
||||||
if (m_noticeCallback.m_func) {
|
emit onNotice(details);
|
||||||
Pgsql::ErrorDetails details = Pgsql::ErrorDetails::createErrorDetailsFromPGresult(result);
|
|
||||||
m_noticeCallback.m_func(details);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,16 +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 "ConnectionConfig.h"
|
#include "ConnectionConfig.h"
|
||||||
#include <QElapsedTimer>
|
#include <QElapsedTimer>
|
||||||
// #include <functional>
|
|
||||||
// #include <condition_variable>
|
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
// #include <queue>
|
|
||||||
// #include <vector>
|
|
||||||
// #include <thread>
|
|
||||||
#include <boost/asio/ip/tcp.hpp>
|
#include <boost/asio/ip/tcp.hpp>
|
||||||
#include <boost/asio/io_service.hpp>
|
#include <boost/asio/io_service.hpp>
|
||||||
|
|
||||||
|
|
@ -19,7 +17,8 @@
|
||||||
* 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 on that routine is called.
|
* when the result is on that routine is called.
|
||||||
*/
|
*/
|
||||||
class ASyncDBConnection {
|
class ASyncDBConnection: public QObject {
|
||||||
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
enum class State {
|
enum class State {
|
||||||
NotConnected,
|
NotConnected,
|
||||||
|
|
@ -31,8 +30,6 @@ public:
|
||||||
};
|
};
|
||||||
|
|
||||||
using on_result_callback = std::function<void(std::shared_ptr<Pgsql::Result>, qint64)>;
|
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);
|
explicit ASyncDBConnection(boost::asio::io_service &ios);
|
||||||
~ASyncDBConnection();
|
~ASyncDBConnection();
|
||||||
|
|
@ -41,9 +38,6 @@ public:
|
||||||
void setupConnection(const ConnectionConfig &config);
|
void setupConnection(const ConnectionConfig &config);
|
||||||
void closeConnection();
|
void closeConnection();
|
||||||
|
|
||||||
void setStateCallback(on_state_callback state_callback);
|
|
||||||
void setNoticeCallback(on_notice_callback notice_callback);
|
|
||||||
|
|
||||||
/** Sends command to the server.
|
/** Sends command to the server.
|
||||||
|
|
||||||
When the result is in on_result will be called directly within the thread.
|
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 send(const std::string &command, Pgsql::Params params, on_result_callback on_result);
|
||||||
|
|
||||||
bool cancel();
|
bool cancel();
|
||||||
|
|
||||||
|
signals:
|
||||||
|
void onStateChanged(ASyncDBConnection::State state);
|
||||||
|
void onNotice(Pgsql::ErrorDetails notice);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Pgsql::Connection m_connection;
|
Pgsql::Connection m_connection;
|
||||||
|
|
@ -61,15 +59,7 @@ 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;
|
||||||
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;
|
QElapsedTimer m_timer;
|
||||||
|
|
||||||
void async_connect_handler(boost::system::error_code ec, std::size_t s);
|
void async_connect_handler(boost::system::error_code ec, std::size_t s);
|
||||||
|
|
@ -78,4 +68,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
|
||||||
|
|
|
||||||
|
|
@ -11,21 +11,13 @@ DatabaseWindow::DatabaseWindow(QWidget *parent) :
|
||||||
{
|
{
|
||||||
ui->setupUi(this);
|
ui->setupUi(this);
|
||||||
|
|
||||||
m_dbConnection.setStateCallback([this](ASyncDBConnection::State st)
|
connect(&m_dbConnection, &ASyncDBConnection::onStateChanged, this, &DatabaseWindow::connectionStateChanged);
|
||||||
{
|
connect(&m_dbConnection, &ASyncDBConnection::onNotice, this, &DatabaseWindow::receiveNotice);
|
||||||
QueueTask([this, st]() { connectionStateChanged(st); });
|
|
||||||
});
|
|
||||||
|
|
||||||
m_dbConnection.setNoticeCallback([this](Pgsql::ErrorDetails details)
|
|
||||||
{
|
|
||||||
QueueTask([this, details]() { receiveNotice(details); });
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
DatabaseWindow::~DatabaseWindow()
|
DatabaseWindow::~DatabaseWindow()
|
||||||
{
|
{
|
||||||
m_dbConnection.closeConnection();
|
m_dbConnection.closeConnection();
|
||||||
m_dbConnection.setStateCallback(nullptr);
|
|
||||||
delete ui;
|
delete ui;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,9 @@
|
||||||
#include "QueryTab.h"
|
#include "QueryTab.h"
|
||||||
#include "ui_QueryTab.h"
|
#include "ui_QueryTab.h"
|
||||||
|
|
||||||
#include "SqlSyntaxHighlighter.h"
|
#include "SqlSyntaxHighlighter.h"
|
||||||
|
|
||||||
#include <QStandardPaths>
|
#include <QStandardPaths>
|
||||||
#include <QPushButton>
|
#include <QPushButton>
|
||||||
|
|
||||||
#include <QFileDialog>
|
#include <QFileDialog>
|
||||||
#include <QMessageBox>
|
#include <QMessageBox>
|
||||||
#include <QTabWidget>
|
#include <QTabWidget>
|
||||||
|
|
@ -68,16 +67,9 @@ QueryTab::QueryTab(MainWindow *win, QWidget *parent) :
|
||||||
{
|
{
|
||||||
ui->setupUi(this);
|
ui->setupUi(this);
|
||||||
|
|
||||||
m_dbConnection.setStateCallback([this](ASyncDBConnection::State st)
|
connect(&m_dbConnection, &ASyncDBConnection::onStateChanged, this, &QueryTab::connectionStateChanged);
|
||||||
{
|
connect(&m_dbConnection, &ASyncDBConnection::onNotice, this, &QueryTab::receiveNotice);
|
||||||
m_win->QueueTask([this, st]() { connectionStateChanged(st); });
|
|
||||||
});
|
|
||||||
|
|
||||||
m_dbConnection.setNoticeCallback([this](Pgsql::ErrorDetails details)
|
|
||||||
{
|
|
||||||
m_win->QueueTask([this, details]() { receiveNotice(details); });
|
|
||||||
});
|
|
||||||
|
|
||||||
QFont font;
|
QFont font;
|
||||||
font.setFamily("Source Code Pro");
|
font.setFamily("Source Code Pro");
|
||||||
font.setFixedPitch(true);
|
font.setFixedPitch(true);
|
||||||
|
|
@ -103,7 +95,6 @@ QueryTab::QueryTab(MainWindow *win, QWidget *parent) :
|
||||||
QueryTab::~QueryTab()
|
QueryTab::~QueryTab()
|
||||||
{
|
{
|
||||||
m_dbConnection.closeConnection();
|
m_dbConnection.closeConnection();
|
||||||
m_dbConnection.setStateCallback(nullptr);
|
|
||||||
|
|
||||||
delete ui;
|
delete ui;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -73,14 +73,6 @@ public:
|
||||||
bool isNew() const { return m_new; }
|
bool isNew() const { return m_new; }
|
||||||
private:
|
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*>;
|
using ResultTabContainer = std::vector<TuplesResultWidget*>;
|
||||||
|
|
||||||
Ui::QueryTab *ui;
|
Ui::QueryTab *ui;
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,8 @@
|
||||||
#endif
|
#endif
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include "GlobalIoService.h"
|
#include "GlobalIoService.h"
|
||||||
|
#include "ASyncDBConnection.h"
|
||||||
|
#include "Pgsql_Result.h"
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
|
|
@ -22,6 +24,9 @@ int main(int argc, char *argv[])
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
qRegisterMetaType<ASyncDBConnection::State>();
|
||||||
|
qRegisterMetaType<Pgsql::ErrorDetails>();
|
||||||
|
|
||||||
QApplication a(argc, argv);
|
QApplication a(argc, argv);
|
||||||
|
|
||||||
QCoreApplication::setOrganizationName("pglab");
|
QCoreApplication::setOrganizationName("pglab");
|
||||||
|
|
|
||||||
|
|
@ -132,4 +132,5 @@ namespace Pgsql {
|
||||||
|
|
||||||
} // end namespace Pgsql
|
} // end namespace Pgsql
|
||||||
|
|
||||||
|
|
||||||
#endif // PGSQL_RESULT_H
|
#endif // PGSQL_RESULT_H
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue