diff --git a/.vscode/settings.json b/.vscode/settings.json index e375c2a..5ecea1e 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -3,6 +3,7 @@ "string": "cpp", "vector": "cpp", "deque": "cpp", - "ostream": "cpp" + "ostream": "cpp", + "future": "cpp" } } \ No newline at end of file diff --git a/lib/mysql.hpp b/lib/mysql.hpp index 584ed64..9b3ca03 100644 --- a/lib/mysql.hpp +++ b/lib/mysql.hpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -16,7 +17,8 @@ #include #include -#define CONNECT_TRY_LIMIT 3 +#define unlimited 0 +#define reconnectSleep 100000 // in us using namespace std; using namespace sql; @@ -59,18 +61,50 @@ class mySQL { public: mutex io; MySQL_Driver *drv; - Connection *con; + // Connection *con; + vector con; string path, username, password, db; bool isPersistent; + uint numOfCon; + uint reconTrys = 3; - mySQL(const string _path, const string _username, const string _password, const string _db, bool _isPersistent = false); - bool open(const string _db = ""); - bool connect(); + mySQL(const string _path, const string _username, const string _password, const string _db, const bool _isPersistent = false, const uint _numOfCon = 1); + bool open(const string _db = "", const int con_idx = -1); + bool connect(const int con_idx = -1); bool disconnect(); + void reconnectTrys(const uint _trys); void exec(sqlQA &sql_qa); void getColumns(const string _table, vector &_columns); ~mySQL(); }; +// class mySQLPool { +// public: + +// struct Drop { +// mySQL* instance; +// bool used = false; +// }; + +// struct Swimmer { +// thread instance; +// bool used = false; +// }; + +// mutex io; +// uint maxpools = 0; +// vector droplets; +// bool fixServer = false; +// bool fixScheme = false; +// vector swimmers; + +// mySQLPool(const uint _maxpools); +// mySQLPool(const uint _maxpools, const string _path, const string _username, const string _password, const string _db); + +// void exec(sqlQA &sql_qa, const string _db = ""); + + +// }; + #endif \ No newline at end of file diff --git a/src/mysql.cpp b/src/mysql.cpp index 04d4e19..7ba1e99 100644 --- a/src/mysql.cpp +++ b/src/mysql.cpp @@ -74,12 +74,15 @@ void sqlQA::parse_columns(const string _columns) { } } -mySQL::mySQL(const string _path, const string _username, const string _password, const string _db, bool _isPersistent) { +mySQL::mySQL(const string _path, const string _username, const string _password, const string _db, const bool _isPersistent, const uint _numOfCon) { path = _path; username = _username; password = _password; db = _db; - isPersistent = _isPersistent; + isPersistent = _numOfCon > 1 ? true : _isPersistent; + numOfCon = _numOfCon; + + drv = get_mysql_driver_instance(); if (isPersistent) { if (connect()) { @@ -95,39 +98,52 @@ mySQL::mySQL(const string _path, const string _username, const string _password, } -bool mySQL::open(const string _db) { +bool mySQL::open(const string _db, const int con_idx) { io.lock(); db = _db.empty() ? db : _db; - bool status = true; + bool status = true; // ako true greška je - try { - con->setSchema(db); - status = false; - io.unlock(); + for (uint i=0; isetSchema(db); + status = false; + io.unlock(); + } + catch (const SQLException &error) { + cout << error.what() << endl; + io.unlock(); + } + con_idx != -1 ? i = con.size() : i; } - catch (const SQLException &error) { - cout << error.what() << endl; - io.unlock(); - } + return status; } -bool mySQL::connect() { +bool mySQL::connect(const int con_idx) { io.lock(); uint trys = 0; bool status = true; - while (trys < CONNECT_TRY_LIMIT && status) { - try { - drv = get_mysql_driver_instance(); - con = drv->connect(path, username, password); - status = false; - io.unlock(); - } - catch (const SQLException &error) { - cout << error.what() << endl; - usleep(10000*trys++); - io.unlock(); + for (uint i=0; iconnect(path, username, password)); + } + else { + con[i] = drv->connect(path, username, password); + } + status = false; + io.unlock(); + } + catch (const SQLException &error) { + cout << error.what() << endl; + usleep(reconnectSleep); + reconTrys == 0 ? trys : trys++; + io.unlock(); + } } } @@ -138,38 +154,54 @@ bool mySQL::disconnect() { io.lock(); bool status = true; - if (con->isValid() && !con->isClosed()) { - try { - con->close(); - status = false; - io.unlock(); - } - catch (const SQLException &error) { - cout << error.what() << endl; - status = true; - io.unlock(); + for (uint i=0; iisValid() && !con[i]->isClosed()) { + try { + con[i]->close(); + status = false; + io.unlock(); + } + catch (const SQLException &error) { + cout << error.what() << endl; + status = true; + io.unlock(); + } } - } - else { - status = false; + else { + status = false; // već je zatvorena + } } return status; } +/** + * Broj pokušaja usljed povezivanja s bazom od 1 do unlimited; +*/ + +void mySQL::reconnectTrys(const uint _trys) { + io.lock(); + reconTrys = _trys; + io.unlock(); +} + void mySQL::exec(sqlQA &sql_qa) { - if (!isPersistent || !con->isValid() || con->isClosed()) { - if (connect()) { - throw string("[ERROR] Unable to connect database "); - } + for (uint i=0; iisValid() || con[i]->isClosed()) { + if (connect(i)) { + throw string("[ERROR] Unable to connect database "); + } - if (open()) { - throw string("[ERROR] Unable to open database " + db); + if (open(i)) { + throw string("[ERROR] Unable to open database " + db); + } } } + // find free connection + io.lock(); /**/ try { @@ -249,3 +281,50 @@ mySQL::~mySQL() { throw string("[ERROR] Unable to close database "); } } + +// mySQLPool::mySQLPool(const uint _maxpools) { +// maxpools = _maxpools; +// fixServer = false; +// fixScheme = true; +// } + +// mySQLPool::mySQLPool(const uint _maxpools, const string _path, const string _username, const string _password, const string _db) { +// maxpools = _maxpools; +// fixServer = true; +// fixScheme = !_db.empty(); +// for (uint i=0; iexec(sql_qa); +// }); +// swimmers[i].instance.join(); +// swimmers[i].used = false; +// isRunned = true; +// } +// } +// } +// droplets[i].used = false; +// } +// } + +// }