diff --git a/.vscode/settings.json b/.vscode/settings.json index 8044b1c..e375c2a 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -2,6 +2,7 @@ "files.associations": { "string": "cpp", "vector": "cpp", - "deque": "cpp" + "deque": "cpp", + "ostream": "cpp" } } \ No newline at end of file diff --git a/lib/mysql.hpp b/lib/mysql.hpp index 26b2674..762007d 100644 --- a/lib/mysql.hpp +++ b/lib/mysql.hpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -22,7 +23,7 @@ using namespace mysql; class mySQL { public: - + mutex io; MySQL_Driver *drv; Connection *con; string path, username, password, db; diff --git a/src/mysql.cpp b/src/mysql.cpp index 01b1243..80f7da3 100644 --- a/src/mysql.cpp +++ b/src/mysql.cpp @@ -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> mySQL::query(const string sql_command) { } } + io.lock(); /**/ map> maped; @@ -115,12 +124,15 @@ map> 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(); } /**/ diff --git a/test/test.cpp b/test/test.cpp index 77c162d..70bec39 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -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) { diff --git a/test/test.o b/test/test.o index 6958657..81818ce 100755 Binary files a/test/test.o and b/test/test.o differ