Restore Backup destructor & small style changes

This commit is contained in:
Kacperos155 2022-07-24 18:54:00 +02:00
parent 11f33af197
commit accceeecb9
8 changed files with 96 additions and 30 deletions

View File

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

View File

@ -56,10 +56,6 @@ public:
*/ */
explicit Column(const Statement::TStatementPtr& aStmtPtr, int aIndex); explicit Column(const Statement::TStatementPtr& aStmtPtr, int aIndex);
// default destructor: the finalization will be done by the destructor of the last shared pointer
// default copy constructor and assignment operator are perfectly suited :
// they copy the Statement::Ptr which in turn increments the reference counter.
/** /**
* @brief Return a pointer to the named assigned to this result column (potentially aliased) * @brief Return a pointer to the named assigned to this result column (potentially aliased)
* *

47
include/SQLiteCpp/Row.h Normal file
View File

@ -0,0 +1,47 @@
/**
* @file Row.h
* @ingroup SQLiteCpp
* @brief TODO:
*
* Copyright (c) 2015 Shibao HONG (shibaohong@outlook.com)
* Copyright (c) 2015-2021 Sebastien Rombauts (sebastien.rombauts@gmail.com)
*
* Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
* or copy at http://opensource.org/licenses/MIT)
*/
#pragma once
#include <SQLiteCpp/RowExecutor.h>
#include <string>
// Forward declaration to avoid inclusion of <sqlite3.h> in a header
struct sqlite3_stmt;
class Row;
namespace SQLite
{
class Row
{
public:
Row(RowExecutor::TRowPtr apRow, std::size_t aID);
/**
* @brief Test if the column value is NULL
*
* @param[in] aIndex Index of the column, starting at 0
*
* @return true if the column value is NULL
*
* Throw an exception if the specified index is out of the [0, getColumnCount()) range.
*/
bool isColumnNull(const int aIndex) const;
private:
RowExecutor::TRowWeakPtr mpRow;
std::size_t ID;
};
} // namespace SQLite

View File

@ -90,6 +90,6 @@ class Savepoint {
private: private:
Database& mDatabase; ///< Reference to the SQLite Database Connection Database& mDatabase; ///< Reference to the SQLite Database Connection
std::string msName; ///< Name of the Savepoint std::string msName; ///< Name of the Savepoint
bool mbReleased{ false }; ///< True when release has been called bool mbReleased = false; ///< True when release has been called
}; };
} // namespace SQLite } // namespace SQLite

View File

@ -698,12 +698,12 @@ private:
std::string mQuery; //!< UTF-8 SQL Query std::string mQuery; //!< UTF-8 SQL Query
sqlite3* mpSQLite; //!< Pointer to SQLite Database Connection Handle sqlite3* mpSQLite; //!< Pointer to SQLite Database Connection Handle
TStatementPtr mpPreparedStatement; //!< Shared Pointer to the prepared SQLite Statement Object TStatementPtr mpPreparedStatement; //!< Shared Pointer to the prepared SQLite Statement Object
int mColumnCount{0}; //!< Number of columns in the result of the prepared statement int mColumnCount = 0; //!< Number of columns in the result of the prepared statement
bool mbHasRow{false}; //!< true when a row has been fetched with executeStep() bool mbHasRow = false; //!< true when a row has been fetched with executeStep()
bool mbDone{false}; //!< true when the last executeStep() had no more row to fetch bool mbDone = false; //!< true when the last executeStep() had no more row to fetch
/// Map of columns index by name (mutable so getColumnIndex can be const) /// Map of columns index by name (mutable so getColumnIndex can be const)
mutable std::map<std::string, int, std::less<>> mColumnNames{}; mutable std::map<std::string, int, std::less<>> mColumnNames;
}; };

View File

@ -88,7 +88,7 @@ public:
private: private:
Database& mDatabase; ///< Reference to the SQLite Database Connection Database& mDatabase; ///< Reference to the SQLite Database Connection
bool mbCommited{ false }; ///< True when commit has been called bool mbCommited = false; ///< True when commit has been called
}; };

View File

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

25
src/Row.cpp Normal file
View File

@ -0,0 +1,25 @@
/**
* @file Row.cpp
* @ingroup SQLiteCpp
* @brief TODO:
*
* Copyright (c) 2015 Shibao HONG (shibaohong@outlook.com)
* Copyright (c) 2015-2021 Sebastien Rombauts (sebastien.rombauts@gmail.com)
*
* Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
* or copy at http://opensource.org/licenses/MIT)
*/
#include <SQLiteCpp/Row.h>
#include <SQLiteCpp/Exception.h>
#include <sqlite3.h>
namespace SQLite
{
;
} // namespace SQLite