From 8548eb9d770a744926c0a5a9c384a21e069cced1 Mon Sep 17 00:00:00 2001 From: heck Date: Sun, 18 Apr 2021 22:19:24 +0200 Subject: [PATCH] pEpSQLite - bugfixes add is_open() idempotent close() close db before deleting add to_string(RSRecord) add to_string(ResultSet) --- src/pEpSQLite.cc | 77 ++++++++++++++++++++++++++++++++---------------- src/pEpSQLite.hh | 8 +++-- 2 files changed, 58 insertions(+), 27 deletions(-) diff --git a/src/pEpSQLite.cc b/src/pEpSQLite.cc index a6ac759..e07f9b5 100644 --- a/src/pEpSQLite.cc +++ b/src/pEpSQLite.cc @@ -11,18 +11,18 @@ namespace pEp { 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() { pEpLogClass("called"); - int rc{::sqlite3_open(db_path.c_str(), &db)}; + 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))}; - throw (e); + runtime_error e{ string("Can't open database (" + db_path + "): " + ::sqlite3_errmsg(db)) }; + close_db(); + throw(e); } } @@ -35,16 +35,30 @@ namespace pEp { void pEpSQLite::close_db() { 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() { 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))}; - throw (e); + runtime_error e{ string("could not delete db (" + db_path + "): " + strerror(errno)) }; + throw(e); } } @@ -52,43 +66,56 @@ namespace pEp { { RSRecord record; 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 - const string val = string{argv[col] ? argv[col] : "NULL"}; - record.insert({key, val}); + const string val = string{ argv[col] ? argv[col] : "NULL" }; + record.insert({ key, val }); } (static_cast(obj))->resultset.push_back(record); return 0; } - ResultSet pEpSQLite::execute(const string& stmt) + ResultSet pEpSQLite::execute(const string &stmt) { - if(db == nullptr) { - runtime_error e{string("execute(): - Error: db is not open")}; - throw (e); + 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); + throw(e); } } return resultset; } - string pEpSQLite::resultset_to_string(const ResultSet& rs) + string pEpSQLite::to_string(const RSRecord &rec) { - pEpLogClass("called"); 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; - for (const RSRecord& rec : rs) { - for (const auto& item : rec) { - ss << "RESULTSET[" << i << "][" << item.first << "] = " << item.second << "\"" << endl; - } + for (const RSRecord &rec : rs) { + ss << "ROW[" << i << "]" << endl << to_string(rec); i++; } return ss.str(); @@ -99,4 +126,4 @@ namespace pEp { pEpLogClass("called"); close_db(); } -} +} // namespace pEp diff --git a/src/pEpSQLite.hh b/src/pEpSQLite.hh index ebf7b52..00135e5 100644 --- a/src/pEpSQLite.hh +++ b/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();