Skip to content

Commit

Permalink
Small improvements & code cleanup
Browse files Browse the repository at this point in the history
# Re-introduce the unique_ptr with custom deleter needed to avoid including sqlite.h in Backup.h
  • Loading branch information
Kacperos155 authored and SRombauts committed Sep 18, 2022
1 parent 36a2cb3 commit cdafab0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
11 changes: 7 additions & 4 deletions include/SQLiteCpp/Backup.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,6 @@ class Backup
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
*
Expand All @@ -121,7 +118,13 @@ class Backup
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
28 changes: 14 additions & 14 deletions src/Backup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
Expand All @@ -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);
}
}


Expand Down

0 comments on commit cdafab0

Please sign in to comment.