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": { "files.associations": {
"string": "cpp", "string": "cpp",
"vector": "cpp", "vector": "cpp",
"deque": "cpp" "deque": "cpp",
"ostream": "cpp"
} }
} }

@ -5,6 +5,7 @@
#include <vector> #include <vector>
#include <map> #include <map>
#include <iostream> #include <iostream>
#include <mutex>
#include <mysql_driver.h> #include <mysql_driver.h>
#include <mysql_connection.h> #include <mysql_connection.h>
@ -22,7 +23,7 @@ using namespace mysql;
class mySQL { class mySQL {
public: public:
mutex io;
MySQL_Driver *drv; MySQL_Driver *drv;
Connection *con; Connection *con;
string path, username, password, db; 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) { bool mySQL::open(const string _db) {
io.lock();
db = _db.empty() ? db : _db; db = _db.empty() ? db : _db;
bool status = true; bool status = true;
try { try {
con->setSchema(db); con->setSchema(db);
status = false; status = false;
io.unlock();
} }
catch (const SQLException &error) { catch (const SQLException &error) {
cout << error.what() << endl; cout << error.what() << endl;
io.unlock();
} }
return status; return status;
} }
bool mySQL::connect() { bool mySQL::connect() {
io.lock();
uint trys = 0; uint trys = 0;
bool status = true; bool status = true;
@ -45,10 +48,12 @@ bool mySQL::connect() {
drv = get_mysql_driver_instance(); drv = get_mysql_driver_instance();
con = drv->connect(path, username, password); con = drv->connect(path, username, password);
status = false; status = false;
io.unlock();
} }
catch (const SQLException &error) { catch (const SQLException &error) {
cout << error.what() << endl; cout << error.what() << endl;
usleep(10000*trys++); usleep(10000*trys++);
io.unlock();
} }
} }
@ -56,15 +61,18 @@ bool mySQL::connect() {
} }
bool mySQL::close() { bool mySQL::close() {
io.lock();
bool status = true; bool status = true;
if (con->isValid() && !con->isClosed()) { if (con->isValid() && !con->isClosed()) {
try { try {
con->close(); con->close();
status = false; status = false;
io.unlock();
} }
catch (const SQLException &error) { catch (const SQLException &error) {
cout << error.what() << endl; 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; map<string, vector<string>> maped;
@ -115,12 +124,15 @@ map<string, vector<string>> mySQL::query(const string sql_command) {
delete res; delete res;
delete stmt; delete stmt;
io.unlock();
} }
catch (const SQLException &error) { catch (const SQLException &error) {
cout << error.what() << endl; cout << error.what() << endl;
io.unlock();
} }
catch (const string error) { catch (const string error) {
throw error; throw error;
io.unlock();
} }
/**/ /**/
@ -146,6 +158,7 @@ bool mySQL::change(const string sql_command) {
} }
} }
io.lock();
/**/ /**/
bool status = false; bool status = false;
@ -157,13 +170,15 @@ bool mySQL::change(const string sql_command) {
status = (bool)changeCatch; status = (bool)changeCatch;
delete stmt; delete stmt;
io.unlock();
} }
catch (const SQLException &error) { catch (const SQLException &error) {
cout << error.what() << endl; cout << error.what() << endl;
io.unlock();
} }
catch (const string error) { catch (const string error) {
throw error; throw error;
io.unlock();
} }
/**/ /**/

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

Binary file not shown.
Loading…
Cancel
Save