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>
|
2019-11-06 20:03:27 +01:00
|
|
|
|
#include <memory>
|
2017-01-03 07:22:36 +01:00
|
|
|
|
|
2019-11-06 20:03:27 +01:00
|
|
|
|
class ASyncDBConnectionThread;
|
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
|
|
|
|
|
2019-11-06 20:03:27 +01:00
|
|
|
|
explicit ASyncDBConnection();
|
2017-08-25 08:37:18 +02:00
|
|
|
|
~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
|
|
|
|
|
2018-01-09 20:39:43 +01:00
|
|
|
|
/** This version of send uses the signal onQueryResult and onQueryError to report back
|
|
|
|
|
|
* the completion of the query.
|
|
|
|
|
|
*/
|
2021-07-04 16:45:50 +02:00
|
|
|
|
// bool send(const std::string &command, Pgsql::Params params = Pgsql::Params())
|
|
|
|
|
|
// {
|
|
|
|
|
|
// return send(command, params, [this] (Expected<std::shared_ptr<Pgsql::Result>> res, qint64) {
|
|
|
|
|
|
// if (res.valid()) {
|
|
|
|
|
|
// emit onQueryResult(res.get());
|
|
|
|
|
|
// }
|
|
|
|
|
|
// else {
|
|
|
|
|
|
// emit onQueryError();
|
|
|
|
|
|
// }
|
|
|
|
|
|
// });
|
|
|
|
|
|
// }
|
2018-01-09 20:39:43 +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);
|
2021-07-04 16:45:50 +02:00
|
|
|
|
// void onQueryResult(std::shared_ptr<Pgsql::Result> result);
|
|
|
|
|
|
// void onQueryError();
|
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;
|
2019-11-06 20:03:27 +01:00
|
|
|
|
std::unique_ptr<ASyncDBConnectionThread> m_threadData;
|
|
|
|
|
|
std::thread m_thread;
|
2017-08-24 21:12:32 +02:00
|
|
|
|
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 doStateCallback(State state);
|
|
|
|
|
|
void processNotice(const PGresult *result);
|
2019-11-06 20:03:27 +01:00
|
|
|
|
|
|
|
|
|
|
friend class ASyncDBConnectionThread;
|
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);
|
2018-01-09 20:39:43 +01:00
|
|
|
|
Q_DECLARE_METATYPE(std::shared_ptr<Pgsql::Result>);
|
2018-01-08 20:54:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
2017-01-03 07:22:36 +01:00
|
|
|
|
#endif // ASYNCDBCONNECTION_H
|