|
|
@ -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; |
|
|
|
path = _path; |
|
|
|
username = _username; |
|
|
|
username = _username; |
|
|
|
password = _password; |
|
|
|
password = _password; |
|
|
|
db = _db; |
|
|
|
db = _db; |
|
|
|
isPersistent = _isPersistent; |
|
|
|
isPersistent = _numOfCon > 1 ? true : _isPersistent; |
|
|
|
|
|
|
|
numOfCon = _numOfCon; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
drv = get_mysql_driver_instance(); |
|
|
|
|
|
|
|
|
|
|
|
if (isPersistent) { |
|
|
|
if (isPersistent) { |
|
|
|
if (connect()) { |
|
|
|
if (connect()) { |
|
|
@ -95,13 +98,15 @@ 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(); |
|
|
|
io.lock(); |
|
|
|
db = _db.empty() ? db : _db; |
|
|
|
db = _db.empty() ? db : _db; |
|
|
|
bool status = true; |
|
|
|
bool status = true; // ako true greška je
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (uint i=0; i<con.size(); i++) { |
|
|
|
|
|
|
|
con_idx != -1 ? i = con_idx : i; |
|
|
|
try { |
|
|
|
try { |
|
|
|
con->setSchema(db); |
|
|
|
con[i]->setSchema(db); |
|
|
|
status = false; |
|
|
|
status = false; |
|
|
|
io.unlock(); |
|
|
|
io.unlock(); |
|
|
|
} |
|
|
|
} |
|
|
@ -109,27 +114,38 @@ bool mySQL::open(const string _db) { |
|
|
|
cout << error.what() << endl; |
|
|
|
cout << error.what() << endl; |
|
|
|
io.unlock(); |
|
|
|
io.unlock(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
con_idx != -1 ? i = con.size() : i; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return status; |
|
|
|
return status; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool mySQL::connect() { |
|
|
|
bool mySQL::connect(const int con_idx) { |
|
|
|
io.lock(); |
|
|
|
io.lock(); |
|
|
|
uint trys = 0; |
|
|
|
uint trys = 0; |
|
|
|
bool status = true; |
|
|
|
bool status = true; |
|
|
|
|
|
|
|
|
|
|
|
while (trys < CONNECT_TRY_LIMIT && status) { |
|
|
|
for (uint i=0; i<numOfCon; i++) { |
|
|
|
|
|
|
|
con_idx != -1 ? i = con_idx : i; |
|
|
|
|
|
|
|
while (reconTrys == 0 ? status : (trys < reconTrys && status)) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
drv = get_mysql_driver_instance(); |
|
|
|
if (con_idx == -1) { |
|
|
|
con = drv->connect(path, username, password); |
|
|
|
con.push_back(drv->connect(path, username, password)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else { |
|
|
|
|
|
|
|
con[i] = drv->connect(path, username, password); |
|
|
|
|
|
|
|
} |
|
|
|
status = false; |
|
|
|
status = false; |
|
|
|
io.unlock(); |
|
|
|
io.unlock(); |
|
|
|
} |
|
|
|
} |
|
|
|
catch (const SQLException &error) { |
|
|
|
catch (const SQLException &error) { |
|
|
|
cout << error.what() << endl; |
|
|
|
cout << error.what() << endl; |
|
|
|
usleep(10000*trys++); |
|
|
|
usleep(reconnectSleep); |
|
|
|
|
|
|
|
reconTrys == 0 ? trys : trys++; |
|
|
|
io.unlock(); |
|
|
|
io.unlock(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return status; |
|
|
|
return status; |
|
|
|
} |
|
|
|
} |
|
|
@ -138,9 +154,10 @@ bool mySQL::disconnect() { |
|
|
|
io.lock(); |
|
|
|
io.lock(); |
|
|
|
bool status = true; |
|
|
|
bool status = true; |
|
|
|
|
|
|
|
|
|
|
|
if (con->isValid() && !con->isClosed()) { |
|
|
|
for (uint i=0; i<con.size(); i++) { |
|
|
|
|
|
|
|
if (con[i]->isValid() && !con[i]->isClosed()) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
con->close(); |
|
|
|
con[i]->close(); |
|
|
|
status = false; |
|
|
|
status = false; |
|
|
|
io.unlock(); |
|
|
|
io.unlock(); |
|
|
|
}
|
|
|
|
}
|
|
|
@ -152,23 +169,38 @@ bool mySQL::disconnect() { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
else { |
|
|
|
else { |
|
|
|
status = false; |
|
|
|
status = false; // već je zatvorena
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return status; |
|
|
|
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) { |
|
|
|
void mySQL::exec(sqlQA &sql_qa) { |
|
|
|
|
|
|
|
|
|
|
|
if (!isPersistent || !con->isValid() || con->isClosed()) { |
|
|
|
for (uint i=0; i<con.size(); i++) { |
|
|
|
if (connect()) { |
|
|
|
if (!isPersistent || !con[i]->isValid() || con[i]->isClosed()) { |
|
|
|
|
|
|
|
if (connect(i)) { |
|
|
|
throw string("[ERROR] Unable to connect database "); |
|
|
|
throw string("[ERROR] Unable to connect database "); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (open()) { |
|
|
|
if (open(i)) { |
|
|
|
throw string("[ERROR] Unable to open database " + db); |
|
|
|
throw string("[ERROR] Unable to open database " + db); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// find free connection
|
|
|
|
|
|
|
|
|
|
|
|
io.lock(); |
|
|
|
io.lock(); |
|
|
|
/**/ |
|
|
|
/**/ |
|
|
@ -249,3 +281,50 @@ mySQL::~mySQL() { |
|
|
|
throw string("[ERROR] Unable to close database "); |
|
|
|
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; i<maxpools; i++) {
|
|
|
|
|
|
|
|
// mySQL tmpmysql(_path, _username, _password, _db);
|
|
|
|
|
|
|
|
// // struct Drop tmpdrop(new mySQL(_path, _username, _password, _db), false);
|
|
|
|
|
|
|
|
// droplets.push_back({mySQL(_path, _username, _password, _db, true), false});
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// void mySQLPool::exec(sqlQA &sql_qa, const string _db) {
|
|
|
|
|
|
|
|
// if (!fixScheme && _db.empty()) {
|
|
|
|
|
|
|
|
// throw string("[ERROR] Database is not selected! ");
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// for (uint i=0; i<droplets.size(); i++) {
|
|
|
|
|
|
|
|
// if (droplets[i].used == false) {
|
|
|
|
|
|
|
|
// droplets[i].used = true;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// bool isRunned = false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// while (!isRunned) {
|
|
|
|
|
|
|
|
// for (uint i=0; i<swimmers.size(); i++) {
|
|
|
|
|
|
|
|
// if (swimmers[i].used == false) {
|
|
|
|
|
|
|
|
// swimmers[i].used = true;
|
|
|
|
|
|
|
|
// swimmers[i].instance = thread([&]() {
|
|
|
|
|
|
|
|
// droplets[i].instance->exec(sql_qa);
|
|
|
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
// swimmers[i].instance.join();
|
|
|
|
|
|
|
|
// swimmers[i].used = false;
|
|
|
|
|
|
|
|
// isRunned = true;
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// droplets[i].used = false;
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// }
|
|
|
|