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-03 07:22:36 +01:00
|
|
|
|
#include <functional>
|
2017-01-06 07:23:40 +01:00
|
|
|
|
#include <mutex>
|
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,
|
|
|
|
|
|
CancelSend
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
using on_result_callback = std::function<void(Pgsql::Result)>;
|
|
|
|
|
|
using on_state_callback = std::function<void(State)>;
|
|
|
|
|
|
|
|
|
|
|
|
ASyncDBConnection();
|
|
|
|
|
|
|
|
|
|
|
|
void setupConnection(const std::string &connstring);
|
|
|
|
|
|
void closeConnection();
|
|
|
|
|
|
|
|
|
|
|
|
void setStateCallback(on_state_callback state_callback);
|
|
|
|
|
|
|
|
|
|
|
|
/** Sends command to the server.
|
|
|
|
|
|
|
|
|
|
|
|
When the result is in and no result_task_queue was passed then on_result will be
|
|
|
|
|
|
called directly within the thread.
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
|
|
class Command {
|
|
|
|
|
|
public:
|
|
|
|
|
|
std::string command;
|
|
|
|
|
|
on_result_callback on_result;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/// Contains all the members accessed by the thread
|
|
|
|
|
|
class Thread {
|
|
|
|
|
|
public:
|
2017-01-06 07:23:40 +01:00
|
|
|
|
on_state_callback m_stateCallback;
|
|
|
|
|
|
std::mutex m_stateCallbackMutex;
|
|
|
|
|
|
|
|
|
|
|
|
std::string m_initString;
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
void cancel();
|
2017-01-06 07:23:40 +01:00
|
|
|
|
|
|
|
|
|
|
void stop();
|
|
|
|
|
|
|
2017-01-03 07:22:36 +01:00
|
|
|
|
private:
|
|
|
|
|
|
|
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-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-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
|