diff --git a/lib/mysql.hpp b/lib/mysql.hpp index 7d94b5f..b86a496 100644 --- a/lib/mysql.hpp +++ b/lib/mysql.hpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -68,6 +69,8 @@ class mySQL { bool isPersistent; uint numOfCon; uint reconTrys = 3; + bool runBot = true; + future bot; void getColumns(const string _table, vector &_columns, Connection *ptr_con); // privatno bool open_one(const uint idx); diff --git a/src/mysql.cpp b/src/mysql.cpp index fbb9c86..95b1f28 100644 --- a/src/mysql.cpp +++ b/src/mysql.cpp @@ -114,6 +114,34 @@ mySQL::mySQL(const string _path, const string _username, const string _password, throw string("[ERROR] Unable to open database " + db); } } + + bot = async(launch::async, [&]{ + while (runBot) { + try { + for (uint i=0; itry_lock()) { + if (!con[i].second->isValid()) { + if (connect_one(i)) { + throw string("[ERROR] Unable to connect database "); + } + + if (!db.empty()) { + if (open_one(i)) { + throw string("[ERROR] Unable to open database " + db); + } + } + } + con[i].first->unlock(); + } + } + } catch (const SQLException except) { + cout << except.what() << endl; + } catch (const string except) { + cout << except << endl; + } + } + return; + }); } } @@ -184,6 +212,7 @@ bool mySQL::disconnect_one(const uint idx) { status = false; // već je zatvorena } + delete con[idx].second; return status; } @@ -236,9 +265,36 @@ void mySQL::reconnectTrys(const uint _trys) { } void mySQL::exec(sqlQA &sql_qa) { + + if (!isPersistent) { + if (connect()) { + throw string("[ERROR] Unable to connect database "); + } + + if (!db.empty()) { + if (open()) { + throw string("[ERROR] Unable to open database " + db); + } + } + } + const uint idx = findFreeCon(); - if (!isPersistent || !con[idx].second->isValid() || con[idx].second->isClosed()) { + if (!isPersistent || con[idx].second->isClosed()) { + if (connect_one(idx)) { + throw string("[ERROR] Unable to connect database "); + } + + if (open_one(idx)) { + throw string("[ERROR] Unable to open database " + db); + } + } + + else if (!con[idx].second->isValid()) { + if(disconnect_one(idx)) { + throw string("[ERROR] Unable to close database "); + } + if (connect_one(idx)) { throw string("[ERROR] Unable to connect database "); } @@ -325,14 +381,18 @@ uint mySQL::findFreeCon() { lock_guard master(io); while (true) { for (uint i=0; itry_lock()) { - return i; - } + // if (con[i].second->isValid()) { + if (con[i].first->try_lock()) { + return i; + } + // } } } } mySQL::~mySQL() { + runBot = false; + bot.get(); if(disconnect()) { throw string("[ERROR] Unable to close database "); } diff --git a/test/test.cpp b/test/test.cpp index 30d9714..0a8b6f7 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -18,40 +18,75 @@ int main() { sqlQA test_qa; test_qa.select().from("records").where("enabled = 1"); mydb.exec(test_qa); - test_qa.print(true); - } catch (const string err) { - cout << err << endl; - } - }); - - thread t2([&](){ - try { - sqlQA test_qa; - test_qa.select().from("zones"); - mydb.exec(test_qa); - test_qa.print(true); - } catch (const string err) { - cout << err << endl; - } - }); - - thread t3([&](){ - try { - sqlQA test_qa; - test_qa.select().from("users"); - mydb.exec(test_qa); - test_qa.print(true); + //test_qa.print(true); } catch (const string err) { cout << err << endl; } }); + // thread t2([&](){ + // try { + // sqlQA test_qa; + // test_qa.select().from("zones"); + // mydb.exec(test_qa); + // //test_qa.print(true); + // } catch (const string err) { + // cout << err << endl; + // } + // }); + + // thread t3([&](){ + // try { + // sqlQA test_qa; + // test_qa.select().from("users"); + // mydb.exec(test_qa); + // //test_qa.print(true); + // } catch (const string err) { + // cout << err << endl; + // } + // }); + + // thread t4([&](){ + // try { + // sqlQA test_qa; + // test_qa.select().from("records").where("enabled = 1"); + // mydb.exec(test_qa); + // //test_qa.print(true); + // } catch (const string err) { + // cout << err << endl; + // } + // }); + + // thread t5([&](){ + // try { + // sqlQA test_qa; + // test_qa.select().from("zones"); + // mydb.exec(test_qa); + // //test_qa.print(true); + // } catch (const string err) { + // cout << err << endl; + // } + // }); + + // thread t6([&](){ + // try { + // sqlQA test_qa; + // test_qa.select().from("users"); + // mydb.exec(test_qa); + // // test_qa.print(true); + // } catch (const string err) { + // cout << err << endl; + // } + // }); t1.join(); - t2.join(); - t3.join(); + // t2.join(); + // t3.join(); + // t4.join(); + // t5.join(); + // t6.join(); - // one by one +// one by one // try { // sqlQA test_qa; // test_qa.select().from("records").where("enabled = 1"); @@ -94,6 +129,8 @@ int main() { cout << "Jebi ga" << endl; } + sleep(100); + return 0; } \ No newline at end of file diff --git a/test/test.o b/test/test.o index 0bc3010..af687d8 100755 Binary files a/test/test.o and b/test/test.o differ