A library for MySQL that implements a simpler framework for MySQL Connector++
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mySQL/lib/mysql.hpp

191 lines
4.3 KiB

1 year ago
#ifndef _MYSQL_
#define _MYSQL_
#include <deque>
1 year ago
#include <mutex>
#include <thread>
#include <future>
// #include <iostream>
#include <string>
#include <vector>
#include <tuple>
1 year ago
#include <mysql_driver.h>
#include <mysql_connection.h>
#include <cppconn/driver.h>
#include <cppconn/connection.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>
#include <cppconn/resultset.h>
#define unlimited 0
#define reconnectSleep 10000 // in us
1 year ago
using namespace std;
using namespace sql;
using namespace mysql;
namespace marcelb {
/**
* A class for creating sql responses
*/
template<typename... Types>
class MySQL_Res : public vector<tuple<Types...>> {
public:
bool have_result = false;
uint16_t affected = 0;
uint16_t rows = 0;
uint16_t columns = (int)tuple_size<tuple<Types...>>::value;
};
/**
* Type conversion functions
*/
template<typename T>
T getValue(ResultSet* res, int column);
template<>
inline int getValue<int>(ResultSet* res, int column) {
return res->getInt(column);
}
template<>
inline uint getValue<uint>(ResultSet* res, int column) {
return res->getUInt(column);
}
template<>
inline int64_t getValue<int64_t>(ResultSet* res, int column) {
return res->getInt64(column);
}
template<>
inline uint64_t getValue<uint64_t>(ResultSet* res, int column) {
return res->getUInt64(column);
}
template<>
inline float getValue<float>(ResultSet* res, int column) {
return res->getDouble(column);
}
template<>
inline double getValue<double>(ResultSet* res, int column) {
return res->getDouble(column);
}
template<>
inline string getValue<string>(ResultSet* res, int column) {
return res->getString(column);
}
template<>
inline bool getValue<bool>(ResultSet* res, int column) {
return res->getBoolean(column);
}
// implementiraj neku c++ kompatibilnu pretvorbu za timestampe, time, date datetime itd.
class MySQL {
1 year ago
mutex io;
1 year ago
MySQL_Driver *drv;
deque<Connection*> con;
1 year ago
string path, username, password, db;
uint available;
uint reconTrys = 3;
bool runBot = true;
future<void> bot;
1 year ago
/**
* 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<typename... Types, size_t... Is>
static tuple<Types...> getRow(sql::ResultSet* res, index_sequence<Is...>) {
return make_tuple(getValue<Types>(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<typename... Types>
MySQL_Res<Types...> exec(const string& sql_q) {
Connection* con_ptr = shift_con();
MySQL_Res<Types...> 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<Types...>(res, std::make_index_sequence<sizeof...(Types)>{}));
}
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();
};
1 year ago
}
1 year ago
#endif