#ifndef ASYNCDBCONNECTION_H #define ASYNCDBCONNECTION_H #include "PgsqlConn.h" #include "win32event.h" #include #include #include #include class ASyncDBConnection { public: enum class State { NotConnected, Connecting, Connected, QuerySend, CancelSend }; using on_result_callback = std::function; using on_state_callback = std::function; 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_stateCallback; std::mutex m_stateCallbackMutex; std::string m_initString; Thread(); /// Is started as a seperate thread by ASyncDBConnection void run(); /// Sends a cancel request to the DB server void cancel(); void stop(); private: Win32Event m_stopEvent; Pgsql::Connection m_connection; bool terminateRequested = false; ///< is set when the thread should stop bool makeConnection(); void communicate(); void doStateCallback(State state); }; Thread m_threadData; std::thread m_thread; }; #endif // ASYNCDBCONNECTION_H