69 lines
1.4 KiB
C
69 lines
1.4 KiB
C
|
|
#ifndef ASYNCDBCONNECTION_H
|
|||
|
|
#define ASYNCDBCONNECTION_H
|
|||
|
|
|
|||
|
|
#include "PgsqlConn.h"
|
|||
|
|
#include <functional>
|
|||
|
|
#include <vector>
|
|||
|
|
|
|||
|
|
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:
|
|||
|
|
on_state_callback m_state_callback;
|
|||
|
|
std::string m_init_string;
|
|||
|
|
|
|||
|
|
/// Is started as a seperate thread by ASyncDBConnection
|
|||
|
|
void run();
|
|||
|
|
|
|||
|
|
/// Sends a cancel request to the DB server
|
|||
|
|
void cancel();
|
|||
|
|
private:
|
|||
|
|
|
|||
|
|
Pgsql::Connection m_connection;
|
|||
|
|
bool terminateRequested = false; ///< is set when the thread should stop
|
|||
|
|
|
|||
|
|
bool makeConnection();
|
|||
|
|
void communicate();
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
Thread thread;
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
#endif // ASYNCDBCONNECTION_H
|