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. 47
      src/pEpSQLite.cc
  2. 8
      src/pEpSQLite.hh

47
src/pEpSQLite.cc

@ -20,8 +20,8 @@ namespace pEp {
int rc{ ::sqlite3_open(db_path.c_str(), &db) };
if (rc) {
::sqlite3_close(db);
runtime_error e{ string("Can't open database (" + db_path + "): " + ::sqlite3_errmsg(db)) };
close_db();
throw(e);
}
}
@ -35,12 +35,26 @@ namespace pEp {
void pEpSQLite::close_db()
{
pEpLogClass("called");
if (db != nullptr) {
::sqlite3_close(db);
db = nullptr;
}
}
bool pEpSQLite::is_open()
{
if (db == nullptr) {
return false;
} else {
return true;
}
}
void pEpSQLite::delete_db()
{
pEpLogClass("called");
close_db();
int status = remove(db_path.c_str());
if (status) {
runtime_error e{ string("could not delete db (" + db_path + "): " + strerror(errno)) };
@ -63,16 +77,22 @@ namespace pEp {
ResultSet pEpSQLite::execute(const string &stmt)
{
if(db == nullptr) {
if (!is_open()) {
runtime_error e{ string("execute(): - Error: db is not open") };
throw(e);
} else {
pEpLogClass("called");
this->resultset.clear();
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) {
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);
throw(e);
}
@ -80,15 +100,22 @@ namespace pEp {
return resultset;
}
string pEpSQLite::resultset_to_string(const ResultSet& rs)
string pEpSQLite::to_string(const RSRecord &rec)
{
stringstream ss;
for (const auto &col : rec) {
ss << "[\"" << col.first << "\"] = \"" << col.second << "\"" << endl;
}
return ss.str();
}
string pEpSQLite::to_string(const ResultSet &rs)
{
pEpLogClass("called");
stringstream ss;
ss << "ROWCOUNT: " << rs.size() << endl;
int i = 0;
for (const RSRecord &rec : rs) {
for (const auto& item : rec) {
ss << "RESULTSET[" << i << "][" << item.first << "] = " << item.second << "\"" << endl;
}
ss << "ROW[" << i << "]" << endl << to_string(rec);
i++;
}
return ss.str();
@ -99,4 +126,4 @@ namespace pEp {
pEpLogClass("called");
close_db();
}
}
} // namespace pEp

8
src/pEpSQLite.hh

@ -26,13 +26,17 @@ namespace pEp {
// Will not create any dirs
void create_or_open_db();
void close_db();
bool is_open();
// Delete the database file
void delete_db();
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;
Adapter::pEpLog::pEpLogger logger{"pEpSQLite", log_enabled};
~pEpSQLite();

Loading…
Cancel
Save