diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1530978 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.o \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index 859361c..94ddb0b 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,15 @@ { "files.associations": { "string": "cpp", - "vector": "cpp" + "vector": "cpp", + "*.tcc": "cpp", + "deque": "cpp", + "unordered_map": "cpp", + "system_error": "cpp", + "iostream": "cpp", + "type_traits": "cpp", + "tuple": "cpp", + "array": "cpp", + "utility": "cpp" } } \ No newline at end of file diff --git a/lib/sql3.hpp b/lib/sqlite3.hpp similarity index 59% rename from lib/sql3.hpp rename to lib/sqlite3.hpp index 738dc34..037fc52 100644 --- a/lib/sql3.hpp +++ b/lib/sqlite3.hpp @@ -1,5 +1,5 @@ -#ifndef _SQL3_ -#define _SQL3_ +#ifndef _MARCELB_SQLITE3_ +#define _MARCELB_SQLITE3_ #include #include @@ -8,35 +8,38 @@ #include #include #include -// #include #include #include - using namespace std; +namespace marcelb::SQlite3 { + static string responseDatabase; static int callback(void* data, int argc, char** argv, char** azColName); -class sql3 { - public: - +class SQLite3 { + mutex io; sqlite3* db; string path; bool keepOpen; - string Answer; - map> parsed; - mutex io; - - sql3(const string path, bool _keepOpen = false); + bool open(); bool close(); - void mapit(); + map> parse(const string& result); - map> query(const string sql_command); +public: + + // string Answer; + // map> parsed; - ~sql3(); + SQLite3(const string path, bool _keepOpen = false); + + map> query(const string sql_command); + + ~SQLite3(); }; +} #endif \ No newline at end of file diff --git a/src/sql3.cpp b/src/sqlite3.cpp similarity index 70% rename from src/sql3.cpp rename to src/sqlite3.cpp index 04f09ff..3cbf82e 100644 --- a/src/sql3.cpp +++ b/src/sqlite3.cpp @@ -1,6 +1,8 @@ -#include "../lib/sql3.hpp" +#include "../lib/sqlite3.hpp" -sql3::sql3(const string _path, bool _keepOpen) { +namespace marcelb::SQlite3 { + +SQLite3::SQLite3(const string _path, bool _keepOpen) { path = _path; keepOpen = _keepOpen; @@ -9,21 +11,20 @@ sql3::sql3(const string _path, bool _keepOpen) { throw string("[ERROR] Unable to open database "); } } - } -bool sql3::open() { +bool SQLite3::open() { uint operationStatus = sqlite3_open(path.c_str(), &db); return operationStatus != SQLITE_OK; } -bool sql3::close() { +bool SQLite3::close() { uint operationStatus = sqlite3_close(db); db = NULL; return operationStatus != SQLITE_OK; } -map> sql3::query(const string sql_command) { +map> SQLite3::query(const string sql_command) { io.lock(); if (!keepOpen) { if (open()) { @@ -34,15 +35,16 @@ map> sql3::query(const string sql_command) { bool r = true; char *messaggeError; - uint exec_stuts; - exec_stuts = sqlite3_exec(db, sql_command.c_str(), callback, NULL, &messaggeError); + uint exec_status; + exec_status = sqlite3_exec(db, sql_command.c_str(), callback, NULL, &messaggeError); - if (exec_stuts != SQLITE_OK) { + string result; + if (exec_status != SQLITE_OK) { sqlite3_free(messaggeError); r = false; } else { - Answer = responseDatabase; + result = responseDatabase; responseDatabase.clear(); } @@ -61,16 +63,16 @@ map> sql3::query(const string sql_command) { } else { - mapit(); - _parsed = parsed; - parsed.clear(); + _parsed = parse(result); } io.unlock(); return _parsed; } -void sql3::mapit() { - string a = Answer; + +map> SQLite3::parse(const string& result) { + string a = result; + map> parsed; while (a.find("\n") < a.length()) { uint lineend = a.find("\n"); @@ -81,27 +83,25 @@ void sql3::mapit() { string key = oneline.substr(0, sep); string value = oneline.substr(sep + 3, oneline.length()); value = value.substr(0, value.length()); - parsed[key].push_back(value); } - + return parsed; } -sql3::~sql3() { +SQLite3::~SQLite3() { if(close()) { - throw string("[ERROR] Unable to close database "); + // throw string("[ERROR] Unable to close database "); } } /** - * Callback funkcija za sqlite3 + * Callback funkcija za SQLite3 */ static int callback(void* data, int argc, char** argv, char** azColName) { int i; - // fprintf(stderr, "%s: ", (const char*)data); // ovo je ispisivalo Ĩudne stringove toplo se nadam da nam ne treba char res[1000]; for (i = 0; i < argc; i++) { sprintf(res, "%s = %s", azColName[i], argv[i] ? argv[i] : "NULL"); @@ -109,3 +109,6 @@ static int callback(void* data, int argc, char** argv, char** azColName) { } return 0; } + + +} \ No newline at end of file diff --git a/test/test.cpp b/test/test.cpp index d772a86..f1f4541 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -1,13 +1,14 @@ #include +using namespace std; -#include "../lib/sql3.hpp" -using namespace std; +#include "../lib/sqlite3.hpp" +using namespace marcelb::SQlite3; int main() { try { - sql3 mydb("../example/example.db", false); + SQLite3 mydb("../example/example.db", false); // cout << mydb.ask("Select * from Tab1"); // cout << mydb.ask("INSERT INTO Tab1 VALUES(3,'Pakora', 'marijanab@bitelex.ml');"); diff --git a/test/test.o b/test/test.o deleted file mode 100755 index 115037e..0000000 Binary files a/test/test.o and /dev/null differ