Minor cleanup on throwing exception in Database

This commit is contained in:
Sébastien Rombauts 2019-12-29 22:24:07 +01:00
parent dc3f1ac271
commit ae01dfb895

View File

@ -216,7 +216,7 @@ void Database::loadExtension(const char* apExtensionName, const char *apEntryPoi
(void)apExtensionName; (void)apExtensionName;
(void)apEntryPointName; (void)apEntryPointName;
throw std::runtime_error("sqlite extensions are disabled"); throw SQLite::Exception("sqlite extensions are disabled");
#else #else
#ifdef SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION // Since SQLite 3.13 (2016-05-18): #ifdef SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION // Since SQLite 3.13 (2016-05-18):
// Security warning: // Security warning:
@ -248,8 +248,7 @@ void Database::key(const std::string& aKey) const
#else // SQLITE_HAS_CODEC #else // SQLITE_HAS_CODEC
if (passLen > 0) if (passLen > 0)
{ {
const SQLite::Exception exception("No encryption support, recompile with SQLITE_HAS_CODEC to enable."); throw SQLite::Exception("No encryption support, recompile with SQLITE_HAS_CODEC to enable.");
throw exception;
} }
#endif // SQLITE_HAS_CODEC #endif // SQLITE_HAS_CODEC
} }
@ -271,33 +270,32 @@ void Database::rekey(const std::string& aNewKey) const
} }
#else // SQLITE_HAS_CODEC #else // SQLITE_HAS_CODEC
static_cast<void>(aNewKey); // silence unused parameter warning static_cast<void>(aNewKey); // silence unused parameter warning
const SQLite::Exception exception("No encryption support, recompile with SQLITE_HAS_CODEC to enable."); throw SQLite::Exception("No encryption support, recompile with SQLITE_HAS_CODEC to enable.");
throw exception;
#endif // SQLITE_HAS_CODEC #endif // SQLITE_HAS_CODEC
} }
// Test if a file contains an unencrypted database. // Test if a file contains an unencrypted database.
bool Database::isUnencrypted(const std::string& aFilename) bool Database::isUnencrypted(const std::string& aFilename)
{ {
if (aFilename.length() > 0) if (aFilename.empty())
{ {
std::ifstream fileBuffer(aFilename.c_str(), std::ios::in | std::ios::binary); throw SQLite::Exception("Could not open database, the aFilename parameter was empty.");
char header[16];
if (fileBuffer.is_open())
{
fileBuffer.seekg(0, std::ios::beg);
fileBuffer.getline(header, 16);
fileBuffer.close();
}
else
{
const SQLite::Exception exception("Error opening file: " + aFilename);
throw exception;
}
return strncmp(header, "SQLite format 3\000", 16) == 0;
} }
const SQLite::Exception exception("Could not open database, the aFilename parameter was empty.");
throw exception; std::ifstream fileBuffer(aFilename.c_str(), std::ios::in | std::ios::binary);
char header[16];
if (fileBuffer.is_open())
{
fileBuffer.seekg(0, std::ios::beg);
fileBuffer.getline(header, 16);
fileBuffer.close();
}
else
{
throw SQLite::Exception("Error opening file: " + aFilename);
}
return strncmp(header, "SQLite format 3\000", 16) == 0;
} }
// This is a reference implementation of live backup taken from the official sit: // This is a reference implementation of live backup taken from the official sit: