diff --git a/example/example copy.db b/example/example copy.db deleted file mode 100644 index 15a87b0..0000000 Binary files a/example/example copy.db and /dev/null differ diff --git a/lib/sql3.hpp b/lib/sql3.hpp index 37762d4..738dc34 100644 --- a/lib/sql3.hpp +++ b/lib/sql3.hpp @@ -10,6 +10,7 @@ #include // #include #include +#include using namespace std; @@ -24,15 +25,13 @@ class sql3 { bool keepOpen; string Answer; map> parsed; + mutex io; sql3(const string path, bool _keepOpen = false); bool open(); bool close(); - bool run(const string sql_command); - string answer(); void mapit(); - string ask(const string sql_command); map> query(const string sql_command); ~sql3(); diff --git a/src/sql3.cpp b/src/sql3.cpp index d1dd704..04f09ff 100644 --- a/src/sql3.cpp +++ b/src/sql3.cpp @@ -23,10 +23,11 @@ bool sql3::close() { return operationStatus != SQLITE_OK; } -bool sql3::run(const string sql_command) { - +map> sql3::query(const string sql_command) { + io.lock(); if (!keepOpen) { if (open()) { + io.unlock(); throw string("[ERROR] Unable to open database "); } } @@ -37,7 +38,6 @@ bool sql3::run(const string sql_command) { exec_stuts = sqlite3_exec(db, sql_command.c_str(), callback, NULL, &messaggeError); if (exec_stuts != SQLITE_OK) { - // printf("[ERROR] Ne mogu Ĩitati bazu podataka!"); sqlite3_free(messaggeError); r = false; } @@ -48,40 +48,25 @@ bool sql3::run(const string sql_command) { if (!keepOpen) { if(close()) { + io.unlock(); throw string("[ERROR] Unable to close database "); } } - return r; -} - -string sql3::answer() { - string _Answer = Answer; - Answer.clear(); - return _Answer; -} + map> _parsed; -string sql3::ask(const string sql_command) { - if (!run(sql_command)) { + if (!r) { + io.unlock(); throw string("[ERROR] Unable to read database "); - //return {}; } - else { - return answer(); - } -} -map> sql3::query(const string sql_command) { - if (!run(sql_command)) { - throw string("[ERROR] Unable to read database "); - //return {}; - } else { mapit(); - map> _parsed = parsed; + _parsed = parsed; parsed.clear(); - return _parsed; } + io.unlock(); + return _parsed; } void sql3::mapit() { diff --git a/test/test.cpp b/test/test.cpp index 85c864a..b97e378 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -5,21 +5,27 @@ using namespace std; int main() { + try { - sql3 mydb("../example/example.db", false); + sql3 mydb("../example/example_copy.db", false); // cout << mydb.ask("Select * from Tab1"); // cout << mydb.ask("INSERT INTO Tab1 VALUES(3,'Pakora', 'marijanab@bitelex.ml');"); - // auto res = mydb.query("Select * from Tab1"); - auto res = mydb.query("INSERT INTO Tab1 VALUES(4,'Mare', 'marijanab@bitelex.co');"); + auto res = mydb.query("Select * from Tab1"); + // cout << endl << res["NAME"][1]; - // for (auto i : res) - // for (auto j: i.second) - // cout << i.first << " : " << j << endl; - + // cout << endl << res["MAIL"][1]; - // cout << endl << res["NAME"][1]; + for (auto i : res) { + for (auto j: i.second) { + cout << i.first << " : " << j << endl; + } + } + + } catch (const string err) { + cout << err << endl; + } return 0; } \ No newline at end of file