Small improvements & code cleanup

# Re-introduce the unique_ptr with custom deleter needed to avoid including sqlite.h in Backup.h
This commit is contained in:
Kacperos155 2022-07-23 16:07:53 +02:00 committed by Sébastien Rombauts
parent 36a2cb3153
commit cdafab0024
2 changed files with 21 additions and 18 deletions

View File

@ -96,9 +96,6 @@ public:
Backup(const Backup&) = delete;
Backup& operator=(const Backup&) = delete;
/// Release the SQLite Backup resource.
~Backup();
/**
* @brief Execute a step of backup with a given number of source pages to be copied
*
@ -121,7 +118,13 @@ public:
int getTotalPageCount() const;
private:
sqlite3_backup* mpSQLiteBackup = nullptr; ///< Pointer to SQLite Database Backup Handle
// Deleter functor to use with smart pointers to close the SQLite database backup in an RAII fashion.
struct Deleter
{
void operator()(sqlite3_backup* apBackup);
};
std::unique_ptr<sqlite3_backup, Deleter> mpSQLiteBackup{}; ///< Pointer to SQLite Database Backup Handle
};
} // namespace SQLite

View File

@ -24,10 +24,10 @@ Backup::Backup(Database& aDestDatabase,
Database& aSrcDatabase,
const char* apSrcDatabaseName)
{
mpSQLiteBackup = sqlite3_backup_init(aDestDatabase.getHandle(),
mpSQLiteBackup.reset(sqlite3_backup_init(aDestDatabase.getHandle(),
apDestDatabaseName,
aSrcDatabase.getHandle(),
apSrcDatabaseName);
apSrcDatabaseName));
if (nullptr == mpSQLiteBackup)
{
// If an error occurs, the error code and message are attached to the destination database connection.
@ -48,19 +48,10 @@ Backup::Backup(Database &aDestDatabase, Database &aSrcDatabase) :
{
}
// Release resource for SQLite database backup
Backup::~Backup()
{
if (mpSQLiteBackup)
{
sqlite3_backup_finish(mpSQLiteBackup);
}
}
// Execute backup step with a given number of source pages to be copied
int Backup::executeStep(const int aNumPage /* = -1 */)
{
const int res = sqlite3_backup_step(mpSQLiteBackup, aNumPage);
const int res = sqlite3_backup_step(mpSQLiteBackup.get(), aNumPage);
if (SQLITE_OK != res && SQLITE_DONE != res && SQLITE_BUSY != res && SQLITE_LOCKED != res)
{
throw SQLite::Exception(sqlite3_errstr(res), res);
@ -71,13 +62,22 @@ int Backup::executeStep(const int aNumPage /* = -1 */)
// Get the number of remaining source pages to be copied in this backup process
int Backup::getRemainingPageCount() const
{
return sqlite3_backup_remaining(mpSQLiteBackup);
return sqlite3_backup_remaining(mpSQLiteBackup.get());
}
// Get the number of total source pages to be copied in this backup process
int Backup::getTotalPageCount() const
{
return sqlite3_backup_pagecount(mpSQLiteBackup);
return sqlite3_backup_pagecount(mpSQLiteBackup.get());
}
// Release resource for SQLite database backup
void SQLite::Backup::Deleter::operator()(sqlite3_backup* apBackup)
{
if (apBackup)
{
sqlite3_backup_finish(apBackup);
}
}