2017-01-03 07:22:36 +01:00
|
|
|
|
#ifndef ASYNCDBCONNECTION_H
|
|
|
|
|
|
#define ASYNCDBCONNECTION_H
|
|
|
|
|
|
|
|
|
|
|
|
#include "PgsqlConn.h"
|
2017-01-06 07:23:40 +01:00
|
|
|
|
#include "win32event.h"
|
2017-01-15 21:01:40 +01:00
|
|
|
|
#include "connectionconfig.h"
|
2017-01-03 07:22:36 +01:00
|
|
|
|
#include <functional>
|
2017-01-06 07:23:40 +01:00
|
|
|
|
#include <mutex>
|
2017-01-08 09:58:34 +01:00
|
|
|
|
#include <queue>
|
2017-01-03 07:22:36 +01:00
|
|
|
|
#include <vector>
|
2017-01-06 07:23:40 +01:00
|
|
|
|
#include <thread>
|
2017-01-03 07:22:36 +01:00
|
|
|
|
|
|
|
|
|
|
class ASyncDBConnection {
|
|
|
|
|
|
public:
|
|
|
|
|
|
enum class State {
|
|
|
|
|
|
NotConnected,
|
|
|
|
|
|
Connecting,
|
|
|
|
|
|
Connected,
|
|
|
|
|
|
QuerySend,
|
2017-01-08 09:58:34 +01:00
|
|
|
|
CancelSend,
|
|
|
|
|
|
Terminating
|
2017-01-03 07:22:36 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
2017-01-08 09:58:34 +01:00
|
|
|
|
using on_result_callback = std::function<void(std::shared_ptr<Pgsql::Result>)>;
|
2017-01-03 07:22:36 +01:00
|
|
|
|
using on_state_callback = std::function<void(State)>;
|
2017-01-08 10:29:21 +01:00
|
|
|
|
using on_notice_callback = std::function<void(Pgsql::ErrorDetails)>;
|
2017-01-03 07:22:36 +01:00
|
|
|
|
|
|
|
|
|
|
ASyncDBConnection();
|
|
|
|
|
|
|
2017-01-15 21:01:40 +01:00
|
|
|
|
// void setupConnection(const std::string &connstring);
|
|
|
|
|
|
void setupConnection(const ConnectionConfig &config);
|
2017-01-03 07:22:36 +01:00
|
|
|
|
void closeConnection();
|
|
|
|
|
|
|
|
|
|
|
|
void setStateCallback(on_state_callback state_callback);
|
2017-01-08 10:29:21 +01:00
|
|
|
|
void setNoticeCallback(on_notice_callback notice_callback);
|
2017-01-03 07:22:36 +01:00
|
|
|
|
|
|
|
|
|
|
/** Sends command to the server.
|
|
|
|
|
|
|
2017-01-08 09:58:34 +01:00
|
|
|
|
When the result is in on_result will be called directly within the thread.
|
2017-01-03 07:22:36 +01:00
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
2017-01-08 09:58:34 +01:00
|
|
|
|
bool cancel();
|
|
|
|
|
|
|
2017-01-03 07:22:36 +01:00
|
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
|
|
class Command {
|
|
|
|
|
|
public:
|
|
|
|
|
|
std::string command;
|
|
|
|
|
|
on_result_callback on_result;
|
2017-01-08 09:58:34 +01:00
|
|
|
|
|
|
|
|
|
|
Command() = default;
|
|
|
|
|
|
Command(const std::string &cmd, on_result_callback cb)
|
|
|
|
|
|
: command(cmd), on_result(cb)
|
|
|
|
|
|
{}
|
2017-01-03 07:22:36 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/// Contains all the members accessed by the thread
|
|
|
|
|
|
class Thread {
|
|
|
|
|
|
public:
|
2017-01-08 09:58:34 +01:00
|
|
|
|
using t_CommandQueue = std::queue<Command>;
|
|
|
|
|
|
struct {
|
|
|
|
|
|
std::mutex m_mutex;
|
|
|
|
|
|
on_state_callback m_func;
|
|
|
|
|
|
} m_stateCallback;
|
2017-01-08 10:29:21 +01:00
|
|
|
|
struct {
|
|
|
|
|
|
std::mutex m_mutex;
|
|
|
|
|
|
on_notice_callback m_func;
|
|
|
|
|
|
} m_noticeCallback;
|
2017-01-08 09:58:34 +01:00
|
|
|
|
|
|
|
|
|
|
struct t_Command {
|
|
|
|
|
|
std::mutex m_mutex;
|
|
|
|
|
|
t_CommandQueue m_queue;
|
|
|
|
|
|
Win32Event m_newEvent;
|
|
|
|
|
|
t_Command()
|
|
|
|
|
|
: m_newEvent(Win32Event::Reset::Auto, Win32Event::Initial::Clear)
|
|
|
|
|
|
{}
|
|
|
|
|
|
} m_commandQueue;
|
2017-01-06 07:23:40 +01:00
|
|
|
|
|
2017-01-15 21:01:40 +01:00
|
|
|
|
// std::string m_initString;
|
|
|
|
|
|
ConnectionConfig m_config;
|
2017-01-06 07:23:40 +01:00
|
|
|
|
|
|
|
|
|
|
Thread();
|
2017-01-03 07:22:36 +01:00
|
|
|
|
|
|
|
|
|
|
/// Is started as a seperate thread by ASyncDBConnection
|
|
|
|
|
|
void run();
|
|
|
|
|
|
|
|
|
|
|
|
/// Sends a cancel request to the DB server
|
2017-01-08 09:58:34 +01:00
|
|
|
|
bool cancel();
|
2017-01-06 07:23:40 +01:00
|
|
|
|
|
|
|
|
|
|
void stop();
|
|
|
|
|
|
|
2017-01-03 07:22:36 +01:00
|
|
|
|
private:
|
|
|
|
|
|
|
2017-01-08 09:58:34 +01:00
|
|
|
|
State m_state = State::NotConnected;
|
|
|
|
|
|
|
2017-01-06 07:23:40 +01:00
|
|
|
|
Win32Event m_stopEvent;
|
2017-01-03 07:22:36 +01:00
|
|
|
|
Pgsql::Connection m_connection;
|
|
|
|
|
|
bool terminateRequested = false; ///< is set when the thread should stop
|
2017-01-08 09:58:34 +01:00
|
|
|
|
bool m_terminated = true;
|
|
|
|
|
|
Pgsql::Canceller m_canceller;
|
2017-01-03 07:22:36 +01:00
|
|
|
|
|
2017-01-06 07:23:40 +01:00
|
|
|
|
|
2017-01-03 07:22:36 +01:00
|
|
|
|
bool makeConnection();
|
|
|
|
|
|
void communicate();
|
2017-01-06 07:23:40 +01:00
|
|
|
|
|
|
|
|
|
|
void doStateCallback(State state);
|
2017-01-08 09:58:34 +01:00
|
|
|
|
/// Wait's for a command to come in and send's it to the server
|
|
|
|
|
|
void waitForAndSendCommand();
|
|
|
|
|
|
void doNewCommand();
|
|
|
|
|
|
void waitForResult();
|
2017-01-08 10:29:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void processNotice(const PGresult *result);
|
2017-01-03 07:22:36 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
2017-01-06 07:23:40 +01:00
|
|
|
|
Thread m_threadData;
|
|
|
|
|
|
std::thread m_thread;
|
2017-01-03 07:22:36 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif // ASYNCDBCONNECTION_H
|