91 lines
3.0 KiB
C++
91 lines
3.0 KiB
C++
#pragma once
|
|
|
|
#if __cplusplus >= 201703L
|
|
/* MySQL override. This needed to be inclided before cppconn/exception.h to define them */
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <memory>
|
|
|
|
/* Now remove the trow */
|
|
#define throw(...)
|
|
#include <cppconn/exception.h>
|
|
#undef throw /* reset */
|
|
#endif
|
|
|
|
#include <condition_variable>
|
|
#include "sql/SqlQuery.h"
|
|
#include <cppconn/prepared_statement.h>
|
|
#include <cppconn/driver.h>
|
|
|
|
#define ERROR_MYSQL_MISSING_DRIVER -1
|
|
#define ERROR_MYSQL_INVLID_CONNECT -2
|
|
#define ERROR_MYSQL_INVLID_PROPERTIES -3
|
|
#define ERROR_MYSQL_INVLID_URL -4
|
|
|
|
namespace sql {
|
|
namespace mysql {
|
|
class MySQLManager;
|
|
struct LocalConnection;
|
|
|
|
bool evaluate_sql_query(std::string& sql, const std::vector<variable>& vars, std::vector<variable>& result);
|
|
|
|
class MySQLCommand : public CommandData { };
|
|
|
|
struct ConnectionEntry {
|
|
friend class MySQLManager;
|
|
friend class LocalConnection;
|
|
public:
|
|
typedef std::function<void()> DisconnectListener;
|
|
|
|
private:
|
|
ConnectionEntry(std::unique_ptr<Connection>&& connection, bool used) : connection(std::move(connection)), used(used) {}
|
|
|
|
std::auto_ptr<sql::Savepoint> save_point;
|
|
std::unique_ptr<sql::Connection> connection;
|
|
bool used = false;
|
|
};
|
|
struct LocalConnection {
|
|
LocalConnection(MySQLManager* mgr, const std::shared_ptr<ConnectionEntry>& entry);
|
|
~LocalConnection();
|
|
|
|
MySQLManager* _mgr;
|
|
std::shared_ptr<ConnectionEntry> _connection;
|
|
};
|
|
|
|
class MySQLManager : public SqlManager {
|
|
friend class LocalConnection;
|
|
public:
|
|
typedef std::function<void(const std::shared_ptr<ConnectionEntry>&)> ListenerConnectionDisconnect;
|
|
typedef std::function<void(const std::shared_ptr<ConnectionEntry>&)> ListenerConnectionCreated;
|
|
|
|
typedef std::function<void()> ListenerConnected;
|
|
typedef std::function<void(bool /* wanted */)> ListenerDisconnected;
|
|
|
|
MySQLManager();
|
|
virtual ~MySQLManager();
|
|
|
|
result connect(const std::string &string) override;
|
|
bool connected() override;
|
|
result disconnect() override;
|
|
|
|
ListenerDisconnected listener_disconnected;
|
|
|
|
result execute_raw(const std::string& /* command */);
|
|
protected:
|
|
std::shared_ptr<CommandData> copyCommandData(std::shared_ptr<CommandData> ptr) override;
|
|
std::shared_ptr<CommandData> allocateCommandData() override;
|
|
result executeCommand(std::shared_ptr<CommandData> ptr) override;
|
|
result queryCommand(std::shared_ptr<CommandData> ptr, const QueryCallback &fn) override;
|
|
|
|
public:
|
|
inline std::unique_ptr<LocalConnection> next_connection();
|
|
|
|
std::mutex connections_lock;
|
|
std::condition_variable connections_condition;
|
|
std::deque<std::shared_ptr<ConnectionEntry>> connections;
|
|
|
|
sql::Driver* driver = nullptr;
|
|
bool disconnecting = false;
|
|
};
|
|
}
|
|
} |