Compare commits

..

1 Commits
dev ... thread

  1. 1
      .gitignore
  2. 11
      .vscode/settings.json
  3. BIN
      example/example copy.db
  4. 32
      lib/sql3.hpp
  5. 76
      src/sql3.cpp
  6. 23
      test/test.cpp
  7. BIN
      test/test.o

1
.gitignore vendored

@ -1 +0,0 @@
*.o

@ -1,15 +1,6 @@
{ {
"files.associations": { "files.associations": {
"string": "cpp", "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"
} }
} }

Binary file not shown.

@ -1,5 +1,5 @@
#ifndef _MARCELB_SQLITE3_ #ifndef _SQL3_
#define _MARCELB_SQLITE3_ #define _SQL3_
#include <stdio.h> #include <stdio.h>
#include <sqlite3.h> #include <sqlite3.h>
@ -8,38 +8,36 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <map> #include <map>
// #include <algorithm>
#include <iostream> #include <iostream>
#include <mutex>
using namespace std;
namespace marcelb::SQlite3 { using namespace std;
static string responseDatabase; static string responseDatabase;
static int callback(void* data, int argc, char** argv, char** azColName); static int callback(void* data, int argc, char** argv, char** azColName);
class SQLite3 { class sql3 {
mutex io; public:
sqlite3* db; sqlite3* db;
string path; string path;
bool keepOpen; bool keepOpen;
string Answer;
map<string, vector<string>> parsed;
sql3(const string path, bool _keepOpen = false);
bool open(); bool open();
bool close(); bool close();
map<string, vector<string>> parse(const string& result); bool run(const string sql_command);
string answer();
public: void mapit();
// string Answer;
// map<string, vector<string>> parsed;
SQLite3(const string path, bool _keepOpen = false);
string ask(const string sql_command);
map<string, vector<string>> query(const string sql_command); map<string, vector<string>> query(const string sql_command);
~SQLite3(); ~sql3();
}; };
}
#endif #endif

@ -1,8 +1,6 @@
#include "../lib/sqlite3.hpp" #include "../lib/sql3.hpp"
namespace marcelb::SQlite3 { sql3::sql3(const string _path, bool _keepOpen) {
SQLite3::SQLite3(const string _path, bool _keepOpen) {
path = _path; path = _path;
keepOpen = _keepOpen; keepOpen = _keepOpen;
@ -11,68 +9,83 @@ SQLite3::SQLite3(const string _path, bool _keepOpen) {
throw string("[ERROR] Unable to open database "); throw string("[ERROR] Unable to open database ");
} }
} }
} }
bool SQLite3::open() { bool sql3::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 SQLite3::close() { bool sql3::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>> SQLite3::query(const string sql_command) { bool sql3::run(const string sql_command) {
io.lock();
if (!keepOpen) { if (!keepOpen) {
if (open()) { if (open()) {
io.unlock();
throw string("[ERROR] Unable to open database "); throw string("[ERROR] Unable to open database ");
} }
} }
bool r = true; bool r = true;
char *messaggeError; char *messaggeError;
uint exec_status; uint exec_stuts;
exec_status = sqlite3_exec(db, sql_command.c_str(), callback, NULL, &messaggeError); exec_stuts = sqlite3_exec(db, sql_command.c_str(), callback, NULL, &messaggeError);
string result; if (exec_stuts != SQLITE_OK) {
if (exec_status != SQLITE_OK) { // printf("[ERROR] Ne mogu čitati bazu podataka!");
sqlite3_free(messaggeError); sqlite3_free(messaggeError);
r = false; r = false;
} }
else { else {
result = responseDatabase; Answer = responseDatabase;
responseDatabase.clear(); responseDatabase.clear();
} }
if (!keepOpen) { if (!keepOpen) {
if(close()) { if(close()) {
io.unlock();
throw string("[ERROR] Unable to close database "); throw string("[ERROR] Unable to close database ");
} }
} }
map<string, vector<string>> _parsed; return r;
}
if (!r) { string sql3::answer() {
io.unlock(); string _Answer = Answer;
throw string("[ERROR] Unable to read database "); Answer.clear();
return _Answer;
} }
string sql3::ask(const string sql_command) {
if (!run(sql_command)) {
throw string("[ERROR] Unable to read database ");
//return {};
}
else { else {
_parsed = parse(result); return answer();
} }
io.unlock();
return _parsed;
} }
map<string, vector<string>> sql3::query(const string sql_command) {
if (!run(sql_command)) {
throw string("[ERROR] Unable to read database ");
//return {};
}
else {
mapit();
map<string, vector<string>> _parsed = parsed;
parsed.clear();
return _parsed;
}
}
map<string, vector<string>> SQLite3::parse(const string& result) { void sql3::mapit() {
string a = result; string a = Answer;
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");
@ -83,25 +96,27 @@ map<string, vector<string>> SQLite3::parse(const string& result) {
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;
} }
SQLite3::~SQLite3() { sql3::~sql3() {
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,6 +124,3 @@ static int callback(void* data, int argc, char** argv, char** azColName) {
} }
return 0; return 0;
} }
}

@ -1,32 +1,25 @@
#include <iostream> #include <iostream>
using namespace std;
#include "../lib/sql3.hpp"
#include "../lib/sqlite3.hpp" using namespace std;
using namespace marcelb::SQlite3;
int main() { int main() {
try {
SQLite3 mydb("../example/example.db", false); sql3 mydb("../example/example.db", false);
// cout << mydb.ask("Select * from Tab1"); // cout << mydb.ask("Select * from Tab1");
// cout << mydb.ask("INSERT INTO Tab1 VALUES(3,'Pakora', 'marijanab@bitelex.ml');"); // cout << mydb.ask("INSERT INTO Tab1 VALUES(3,'Pakora', 'marijanab@bitelex.ml');");
auto res = mydb.query("Select * from Tab1"); auto res = mydb.query("Select * from Tab1");
// cout << endl << res["NAME"][1]; // auto res = mydb.query("INSERT INTO Tab1 VALUES(4,'Mare', 'marijanab@bitelex.co');");
// cout << endl << res["MAIL"][1]; for (auto i : res)
for (auto j: i.second)
for (auto i : res) {
for (auto j: i.second) {
cout << i.first << " : " << j << endl; cout << i.first << " : " << j << endl;
}
}
} catch (const string err) {
cout << err << endl; // cout << endl << res["NAME"][1];
}
return 0; return 0;
} }

Binary file not shown.
Loading…
Cancel
Save