commit 4c171a5c99dcfb2548bc7a59b34a995e0bc07323 Author: marcelb Date: Sun May 21 09:12:35 2023 +0200 Tested diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..859361c --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "files.associations": { + "string": "cpp", + "vector": "cpp" + } +} \ No newline at end of file diff --git a/example/example copy.db b/example/example copy.db new file mode 100644 index 0000000..15a87b0 Binary files /dev/null and b/example/example copy.db differ diff --git a/example/example.db b/example/example.db new file mode 100644 index 0000000..bf69448 Binary files /dev/null and b/example/example.db differ diff --git a/lib/sql3.hpp b/lib/sql3.hpp new file mode 100644 index 0000000..9f83063 --- /dev/null +++ b/lib/sql3.hpp @@ -0,0 +1,44 @@ +#ifndef _SQL3_ +#define _SQL3_ + +#include +#include +#include + +#include +#include +#include +// #include +#include + +using namespace std; + +static string responseDatabase; +static int callback(void* data, int argc, char** argv, char** azColName); + +class sql3 { + public: + + sqlite3* db; + string path; + bool keepOpen; + // map> model; + string Answer; + map> parsed; + + 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(); + +}; + + +#endif \ No newline at end of file diff --git a/src/sql3.cpp b/src/sql3.cpp new file mode 100644 index 0000000..3724657 --- /dev/null +++ b/src/sql3.cpp @@ -0,0 +1,113 @@ +#include "../lib/sql3.hpp" + +sql3::sql3(const string _path, bool _keepOpen) { + path = _path; + keepOpen = _keepOpen; + + if (keepOpen) { + if (open()) { + printf("[ERROR] Ne mogu otvoriti bazu podataka!"); + } + } + +} + +bool sql3::open() { + uint operationStatus = sqlite3_open(path.c_str(), &db); + return operationStatus != SQLITE_OK; +} + +bool sql3::close() { + uint operationStatus = sqlite3_close(db); + db = NULL; + return operationStatus != SQLITE_OK; +} + +bool sql3::run(const string sql_command) { + bool r = true; + char *messaggeError; + uint exec_stuts; + 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; + } + else { + Answer = responseDatabase; + } + + return r; +} + +string sql3::answer() { + string _Answer = Answer; + Answer.clear(); + return _Answer; +} + +string sql3::ask(const string sql_command) { + if (!run(sql_command)) { + printf("[ERROR] Ne mogu čitati bazu podataka!"); + return {}; + } + else { + return answer(); + } +} + +map> sql3::query(const string sql_command) { + if (!run(sql_command)) { + printf("[ERROR] Ne mogu čitati bazu podataka!"); + return {}; + } + else { + mapit(); + map> _parsed = parsed; + parsed.clear(); + return _parsed; + } +} + +void sql3::mapit() { + string a = Answer; + + while (a.find("\n") < a.length()) { + uint lineend = a.find("\n"); + string oneline = a.substr(0, lineend); + a.erase(0, lineend+1); + + size_t sep = oneline.find(" = "); + 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); + } + + +} + +sql3::~sql3() { + if(close()) { + printf ("ERROR Zatvaranja baze podataka!"); + } +} + + +/** + * Callback funkcija za sqlite3 +*/ + +static int callback(void* data, int argc, char** argv, char** azColName) { + int i; + fprintf(stderr, "%s: ", (const char*)data); + char res[1000]; + for (i = 0; i < argc; i++) { + sprintf(res, "%s = %s", azColName[i], argv[i] ? argv[i] : "NULL"); + responseDatabase += res; + responseDatabase += '\n'; + } + return 0; +} diff --git a/test/compile.sh b/test/compile.sh new file mode 100644 index 0000000..d78b604 --- /dev/null +++ b/test/compile.sh @@ -0,0 +1 @@ +g++ test.cpp ../src/* -o test.o -l sqlite3 \ No newline at end of file diff --git a/test/test.cpp b/test/test.cpp new file mode 100644 index 0000000..9b8cf32 --- /dev/null +++ b/test/test.cpp @@ -0,0 +1,19 @@ +#include + +#include "../lib/sql3.hpp" + +using namespace std; + +int main() { + + sql3 mydb("../example/example.db", true); + + //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"); + + cout << endl << res["NAME"][0]; + + return 0; +} \ No newline at end of file diff --git a/test/test.o b/test/test.o new file mode 100755 index 0000000..a601bab Binary files /dev/null and b/test/test.o differ