#ifndef _MYSQL_ #define _MYSQL_ #include #include #include #include // #include #include #include #include #include #include #include #include #include #include #include #define unlimited 0 #define reconnectSleep 10000 // in us using namespace std; using namespace sql; using namespace mysql; namespace marcelb { /** * A class for creating sql responses */ template class MySQL_Res : public vector> { public: bool have_result = false; uint16_t affected = 0; uint16_t rows = 0; uint16_t columns = (int)tuple_size>::value; }; /** * Type conversion functions */ template T getValue(ResultSet* res, int column); template<> inline int getValue(ResultSet* res, int column) { return res->getInt(column); } template<> inline uint getValue(ResultSet* res, int column) { return res->getUInt(column); } template<> inline int64_t getValue(ResultSet* res, int column) { return res->getInt64(column); } template<> inline uint64_t getValue(ResultSet* res, int column) { return res->getUInt64(column); } template<> inline float getValue(ResultSet* res, int column) { return res->getDouble(column); } template<> inline double getValue(ResultSet* res, int column) { return res->getDouble(column); } template<> inline string getValue(ResultSet* res, int column) { return res->getString(column); } template<> inline bool getValue(ResultSet* res, int column) { return res->getBoolean(column); } // implementiraj neku c++ kompatibilnu pretvorbu za timestampe, time, date datetime itd. class MySQL { mutex io; MySQL_Driver *drv; deque con; string path, username, password, db; uint available; uint reconTrys = 3; bool runBot = true; future bot; /** * Open one database */ bool open_one(Connection* con_ptr); /** * Open one database server connection */ Connection* create_con(); /** * Close one database connection */ bool disconnect_one(Connection* con_ptr); /** * Take an available database connection */ Connection* shift_con(); /** * Function parses a parameterized row */ template static tuple getRow(sql::ResultSet* res, index_sequence) { return make_tuple(getValue(res, Is + 1)...); } public: /** * MySQL constructor, * receive the path to the mysql server, * username, password, database name, * and number of active connections (optional) */ MySQL(const string _path, const string _username, const string _password, const string _db, const uint _available = 1); /** * Disconnect all connections to server */ bool disconnect(); /** * Define the maximum number of attempts to * reconnect to the server */ void reconnectTrys(const uint _trys); /** * Execute the SQL statement */ template MySQL_Res exec(const string& sql_q) { Connection* con_ptr = shift_con(); MySQL_Res result; try { Statement *stmt; stmt = con_ptr->createStatement(); result.have_result = stmt->execute(sql_q); if (result.have_result) { ResultSet* res = stmt->getResultSet(); result.rows = res->rowsCount(); while (res->next()) { result.push_back(MySQL::getRow(res, std::make_index_sequence{})); } res->close(); delete res; } else { result.affected = stmt->getUpdateCount(); } stmt->close(); delete stmt; disconnect_one(con_ptr); } catch (sql::SQLException& e) { throw runtime_error(e.what()); } return result; } /** * Destruktor * close all connections */ ~MySQL(); }; } #endif