Browse Source

pEpSQLite - bugfixes

add is_open()
idempotent close()
close db before deleting
add to_string(RSRecord)
add to_string(ResultSet)
LIB-12
heck 4 years ago
parent
commit
8548eb9d77
  1. 77
      src/pEpSQLite.cc
  2. 8
      src/pEpSQLite.hh

77
src/pEpSQLite.cc

@ -11,18 +11,18 @@ namespace pEp {
pEpSQLite::pEpSQLite(std::string db_path) : db_path(db_path) pEpSQLite::pEpSQLite(std::string db_path) : db_path(db_path)
{ {
pEpLogClass("called with: db_path = "+ db_path +""); pEpLogClass("called with: db_path = " + db_path + "");
} }
void pEpSQLite::create_or_open_db() void pEpSQLite::create_or_open_db()
{ {
pEpLogClass("called"); pEpLogClass("called");
int rc{::sqlite3_open(db_path.c_str(), &db)}; int rc{ ::sqlite3_open(db_path.c_str(), &db) };
if (rc) { if (rc) {
::sqlite3_close(db); runtime_error e{ string("Can't open database (" + db_path + "): " + ::sqlite3_errmsg(db)) };
runtime_error e{string("Can't open database (" + db_path + "): " + ::sqlite3_errmsg(db))}; close_db();
throw (e); throw(e);
} }
} }
@ -35,16 +35,30 @@ namespace pEp {
void pEpSQLite::close_db() void pEpSQLite::close_db()
{ {
pEpLogClass("called"); pEpLogClass("called");
::sqlite3_close(db); if (db != nullptr) {
::sqlite3_close(db);
db = nullptr;
}
} }
bool pEpSQLite::is_open()
{
if (db == nullptr) {
return false;
} else {
return true;
}
}
void pEpSQLite::delete_db() void pEpSQLite::delete_db()
{ {
pEpLogClass("called"); pEpLogClass("called");
close_db();
int status = remove(db_path.c_str()); int status = remove(db_path.c_str());
if (status) { if (status) {
runtime_error e{string("could not delete db (" + db_path + "): " + strerror(errno))}; runtime_error e{ string("could not delete db (" + db_path + "): " + strerror(errno)) };
throw (e); throw(e);
} }
} }
@ -52,43 +66,56 @@ namespace pEp {
{ {
RSRecord record; RSRecord record;
for (int col = 0; col < argc; col++) { for (int col = 0; col < argc; col++) {
const string key = string{azColName[col]}; const string key = string{ azColName[col] };
// TODO: NULL is not correct, could be a valid value // TODO: NULL is not correct, could be a valid value
const string val = string{argv[col] ? argv[col] : "NULL"}; const string val = string{ argv[col] ? argv[col] : "NULL" };
record.insert({key, val}); record.insert({ key, val });
} }
(static_cast<pEpSQLite *>(obj))->resultset.push_back(record); (static_cast<pEpSQLite *>(obj))->resultset.push_back(record);
return 0; return 0;
} }
ResultSet pEpSQLite::execute(const string& stmt) ResultSet pEpSQLite::execute(const string &stmt)
{ {
if(db == nullptr) { if (!is_open()) {
runtime_error e{string("execute(): - Error: db is not open")}; runtime_error e{ string("execute(): - Error: db is not open") };
throw (e); throw(e);
} else { } else {
pEpLogClass("called"); pEpLogClass("called");
this->resultset.clear(); this->resultset.clear();
char *zErrMsg = nullptr; char *zErrMsg = nullptr;
int rc = ::sqlite3_exec(db, stmt.c_str(), (int (*)(void *, int, char **, char **)) &callback, this, &zErrMsg); int rc = ::sqlite3_exec(
db,
stmt.c_str(),
(int (*)(void *, int, char **, char **)) & callback,
this,
&zErrMsg);
if (rc != SQLITE_OK) { if (rc != SQLITE_OK) {
runtime_error e{string("execute: " + string(::sqlite3_errmsg(db)) + ":" + string(zErrMsg))}; runtime_error e{ string(
"execute: " + string(::sqlite3_errmsg(db)) + ":" + string(zErrMsg)) };
::sqlite3_free(zErrMsg); ::sqlite3_free(zErrMsg);
throw (e); throw(e);
} }
} }
return resultset; return resultset;
} }
string pEpSQLite::resultset_to_string(const ResultSet& rs) string pEpSQLite::to_string(const RSRecord &rec)
{ {
pEpLogClass("called");
stringstream ss; stringstream ss;
for (const auto &col : rec) {
ss << "[\"" << col.first << "\"] = \"" << col.second << "\"" << endl;
}
return ss.str();
}
string pEpSQLite::to_string(const ResultSet &rs)
{
stringstream ss;
ss << "ROWCOUNT: " << rs.size() << endl;
int i = 0; int i = 0;
for (const RSRecord& rec : rs) { for (const RSRecord &rec : rs) {
for (const auto& item : rec) { ss << "ROW[" << i << "]" << endl << to_string(rec);
ss << "RESULTSET[" << i << "][" << item.first << "] = " << item.second << "\"" << endl;
}
i++; i++;
} }
return ss.str(); return ss.str();
@ -99,4 +126,4 @@ namespace pEp {
pEpLogClass("called"); pEpLogClass("called");
close_db(); close_db();
} }
} } // namespace pEp

8
src/pEpSQLite.hh

@ -26,13 +26,17 @@ namespace pEp {
// Will not create any dirs // Will not create any dirs
void create_or_open_db(); void create_or_open_db();
void close_db(); void close_db();
bool is_open();
// Delete the database file // Delete the database file
void delete_db(); void delete_db();
ResultSet execute(const std::string& stmt); ResultSet execute(const std::string& stmt);
std::string resultset_to_string(const ResultSet& rs);
// logging // Utils
static std::string to_string(const RSRecord& rec);
static std::string to_string(const ResultSet& rs);
// Logging
static bool log_enabled; static bool log_enabled;
Adapter::pEpLog::pEpLogger logger{"pEpSQLite", log_enabled}; Adapter::pEpLog::pEpLogger logger{"pEpSQLite", log_enabled};
~pEpSQLite(); ~pEpSQLite();

Loading…
Cancel
Save