|
|
@ -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; |
|
|
|
path = _path; |
|
|
|
keepOpen = _keepOpen; |
|
|
|
keepOpen = _keepOpen; |
|
|
|
|
|
|
|
|
|
|
@ -9,21 +11,20 @@ sql3::sql3(const string _path, bool _keepOpen) { |
|
|
|
throw string("[ERROR] Unable to open database "); |
|
|
|
throw string("[ERROR] Unable to open database "); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool sql3::open() { |
|
|
|
bool SQLite3::open() { |
|
|
|
uint operationStatus = sqlite3_open(path.c_str(), &db); |
|
|
|
uint operationStatus = sqlite3_open(path.c_str(), &db); |
|
|
|
return operationStatus != SQLITE_OK; |
|
|
|
return operationStatus != SQLITE_OK; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool sql3::close() { |
|
|
|
bool SQLite3::close() { |
|
|
|
uint operationStatus = sqlite3_close(db); |
|
|
|
uint operationStatus = sqlite3_close(db); |
|
|
|
db = NULL; |
|
|
|
db = NULL; |
|
|
|
return operationStatus != SQLITE_OK; |
|
|
|
return operationStatus != SQLITE_OK; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
map<string, vector<string>> sql3::query(const string sql_command) { |
|
|
|
map<string, vector<string>> SQLite3::query(const string sql_command) { |
|
|
|
io.lock(); |
|
|
|
io.lock(); |
|
|
|
if (!keepOpen) { |
|
|
|
if (!keepOpen) { |
|
|
|
if (open()) { |
|
|
|
if (open()) { |
|
|
@ -34,15 +35,16 @@ map<string, vector<string>> sql3::query(const string sql_command) { |
|
|
|
|
|
|
|
|
|
|
|
bool r = true; |
|
|
|
bool r = true; |
|
|
|
char *messaggeError; |
|
|
|
char *messaggeError; |
|
|
|
uint exec_stuts; |
|
|
|
uint exec_status; |
|
|
|
exec_stuts = sqlite3_exec(db, sql_command.c_str(), callback, NULL, &messaggeError); |
|
|
|
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); |
|
|
|
sqlite3_free(messaggeError); |
|
|
|
r = false; |
|
|
|
r = false; |
|
|
|
} |
|
|
|
} |
|
|
|
else { |
|
|
|
else { |
|
|
|
Answer = responseDatabase; |
|
|
|
result = responseDatabase; |
|
|
|
responseDatabase.clear(); |
|
|
|
responseDatabase.clear(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -61,16 +63,16 @@ map<string, vector<string>> sql3::query(const string sql_command) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
else { |
|
|
|
else { |
|
|
|
mapit(); |
|
|
|
_parsed = parse(result); |
|
|
|
_parsed = parsed; |
|
|
|
|
|
|
|
parsed.clear(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
io.unlock(); |
|
|
|
io.unlock(); |
|
|
|
return _parsed; |
|
|
|
return _parsed; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void sql3::mapit() { |
|
|
|
|
|
|
|
string a = Answer; |
|
|
|
map<string, vector<string>> SQLite3::parse(const string& result) { |
|
|
|
|
|
|
|
string a = result; |
|
|
|
|
|
|
|
map<string, vector<string>> parsed; |
|
|
|
|
|
|
|
|
|
|
|
while (a.find("\n") < a.length()) { |
|
|
|
while (a.find("\n") < a.length()) { |
|
|
|
uint lineend = a.find("\n"); |
|
|
|
uint lineend = a.find("\n"); |
|
|
@ -81,27 +83,25 @@ void sql3::mapit() { |
|
|
|
string key = oneline.substr(0, sep); |
|
|
|
string key = oneline.substr(0, sep); |
|
|
|
string value = oneline.substr(sep + 3, oneline.length()); |
|
|
|
string value = oneline.substr(sep + 3, oneline.length()); |
|
|
|
value = value.substr(0, value.length()); |
|
|
|
value = value.substr(0, value.length()); |
|
|
|
|
|
|
|
|
|
|
|
parsed[key].push_back(value); |
|
|
|
parsed[key].push_back(value); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return parsed; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
sql3::~sql3() { |
|
|
|
SQLite3::~SQLite3() { |
|
|
|
if(close()) { |
|
|
|
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) { |
|
|
|
static int callback(void* data, int argc, char** argv, char** azColName) { |
|
|
|
int i; |
|
|
|
int i; |
|
|
|
// fprintf(stderr, "%s: ", (const char*)data); // ovo je ispisivalo čudne stringove toplo se nadam da nam ne treba
|
|
|
|
|
|
|
|
char res[1000]; |
|
|
|
char res[1000]; |
|
|
|
for (i = 0; i < argc; i++) { |
|
|
|
for (i = 0; i < argc; i++) { |
|
|
|
sprintf(res, "%s = %s", azColName[i], argv[i] ? argv[i] : "NULL"); |
|
|
|
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; |
|
|
|
return 0; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |