Mutex secure

queue v0.1_beta
marcelb 1 year ago
parent c348921cf8
commit 04f8489cf3
  1. 3
      .vscode/settings.json
  2. 3
      lib/mysql.hpp
  3. 19
      src/mysql.cpp
  4. 8
      test/test.cpp
  5. BIN
      test/test.o

@ -2,6 +2,7 @@
"files.associations": {
"string": "cpp",
"vector": "cpp",
"deque": "cpp"
"deque": "cpp",
"ostream": "cpp"
}
}

@ -5,6 +5,7 @@
#include <vector>
#include <map>
#include <iostream>
#include <mutex>
#include <mysql_driver.h>
#include <mysql_connection.h>
@ -22,7 +23,7 @@ using namespace mysql;
class mySQL {
public:
mutex io;
MySQL_Driver *drv;
Connection *con;
string path, username, password, db;

@ -22,21 +22,24 @@ mySQL::mySQL(const string _path, const string _username, const string _password,
}
bool mySQL::open(const string _db) {
io.lock();
db = _db.empty() ? db : _db;
bool status = true;
try {
con->setSchema(db);
status = false;
io.unlock();
}
catch (const SQLException &error) {
cout << error.what() << endl;
io.unlock();
}
return status;
}
bool mySQL::connect() {
io.lock();
uint trys = 0;
bool status = true;
@ -45,10 +48,12 @@ bool mySQL::connect() {
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();
}
}
@ -56,15 +61,18 @@ bool mySQL::connect() {
}
bool mySQL::close() {
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;
io.unlock();
}
}
@ -83,6 +91,7 @@ map<string, vector<string>> mySQL::query(const string sql_command) {
}
}
io.lock();
/**/
map<string, vector<string>> maped;
@ -115,12 +124,15 @@ map<string, vector<string>> mySQL::query(const string sql_command) {
delete res;
delete stmt;
io.unlock();
}
catch (const SQLException &error) {
cout << error.what() << endl;
io.unlock();
}
catch (const string error) {
throw error;
io.unlock();
}
/**/
@ -146,6 +158,7 @@ bool mySQL::change(const string sql_command) {
}
}
io.lock();
/**/
bool status = false;
@ -157,13 +170,15 @@ bool mySQL::change(const string sql_command) {
status = (bool)changeCatch;
delete stmt;
io.unlock();
}
catch (const SQLException &error) {
cout << error.what() << endl;
io.unlock();
}
catch (const string error) {
throw error;
io.unlock();
}
/**/

@ -10,7 +10,7 @@ int main() {
try {
auto res = mydb.query("SELECT * FROM users");
auto res = mydb.query("SELECT * FROM records");
for (auto i : res) {
for (auto j: i.second) {
@ -18,9 +18,9 @@ int main() {
}
}
if (mydb.change("UPDATE records SET enabled = 0 WHERE domain = 'bitelex.ml'")) {
cout << "Update sucessfuly" << endl;
}
// if (mydb.change("UPDATE records SET enabled = 0 WHERE domain = 'bitelex.ml'")) {
// cout << "Update sucessfuly" << endl;
// }
}
catch (const SQLException error) {

Binary file not shown.
Loading…
Cancel
Save