mirror of
https://github.com/cuberite/SQLiteCpp.git
synced 2025-08-04 17:56:13 -04:00
add Backup class
This commit is contained in:
parent
7fa11f679e
commit
05d304b7c9
@ -90,6 +90,7 @@ set(SQLITECPP_SRC
|
|||||||
${PROJECT_SOURCE_DIR}/src/Database.cpp
|
${PROJECT_SOURCE_DIR}/src/Database.cpp
|
||||||
${PROJECT_SOURCE_DIR}/src/Statement.cpp
|
${PROJECT_SOURCE_DIR}/src/Statement.cpp
|
||||||
${PROJECT_SOURCE_DIR}/src/Transaction.cpp
|
${PROJECT_SOURCE_DIR}/src/Transaction.cpp
|
||||||
|
${PROJECT_SOURCE_DIR}/src/Backup.cpp
|
||||||
)
|
)
|
||||||
source_group(src FILES ${SQLITECPP_SRC})
|
source_group(src FILES ${SQLITECPP_SRC})
|
||||||
|
|
||||||
@ -102,6 +103,7 @@ set(SQLITECPP_INC
|
|||||||
${PROJECT_SOURCE_DIR}/include/SQLiteCpp/Exception.h
|
${PROJECT_SOURCE_DIR}/include/SQLiteCpp/Exception.h
|
||||||
${PROJECT_SOURCE_DIR}/include/SQLiteCpp/Statement.h
|
${PROJECT_SOURCE_DIR}/include/SQLiteCpp/Statement.h
|
||||||
${PROJECT_SOURCE_DIR}/include/SQLiteCpp/Transaction.h
|
${PROJECT_SOURCE_DIR}/include/SQLiteCpp/Transaction.h
|
||||||
|
${PROJECT_SOURCE_DIR}/include/SQLiteCpp/Backup.h
|
||||||
)
|
)
|
||||||
source_group(inc FILES ${SQLITECPP_INC})
|
source_group(inc FILES ${SQLITECPP_INC})
|
||||||
|
|
||||||
|
66
include/SQLiteCpp/Backup.h
Normal file
66
include/SQLiteCpp/Backup.h
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
/**
|
||||||
|
* @file Backup.h
|
||||||
|
* @ingroup SQLiteCpp
|
||||||
|
* @brief Management of a SQLite Database Backup.
|
||||||
|
*
|
||||||
|
* Copyright (c) 2015 Shibao HONG (shibaohong@outlook.com)
|
||||||
|
*
|
||||||
|
* Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
|
||||||
|
* or copy at http://opensource.org/licenses/MIT)
|
||||||
|
*/
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <sqlite3.h>
|
||||||
|
|
||||||
|
#include <SQLiteCpp/Database.h>
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace SQLite
|
||||||
|
{
|
||||||
|
|
||||||
|
class Backup
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Backup(Database& aDestDatabase,
|
||||||
|
const char* apDestDatabaseName,
|
||||||
|
Database& aSrcDatabase,
|
||||||
|
const char* apSrcDatabaseName);
|
||||||
|
|
||||||
|
Backup(Database& aDestDatabase,
|
||||||
|
const std::string& aDestDatabaseName,
|
||||||
|
Database& aSrcDatabase,
|
||||||
|
const std::string& aSrcDatabaseName);
|
||||||
|
|
||||||
|
Backup(Database& aDestDatabase,
|
||||||
|
Database& aSrcDatabase);
|
||||||
|
|
||||||
|
virtual ~Backup() noexcept;
|
||||||
|
|
||||||
|
int executeStep(const int aNumPage = -1);
|
||||||
|
|
||||||
|
int remainingPageCount();
|
||||||
|
|
||||||
|
int totalPageCount();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Return raw pointer to SQLite Database Backup Handle.
|
||||||
|
*
|
||||||
|
* This is often needed to mix this wrapper with other libraries or for advance usage not supported by SQLiteCpp.
|
||||||
|
*/
|
||||||
|
inline sqlite3_backup* getHandle() const noexcept // nothrow
|
||||||
|
{
|
||||||
|
return mpSQLiteBackup;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
/// @{ Backup must be non-copyable
|
||||||
|
Backup(const Backup&);
|
||||||
|
Backup& operator=(const Backup&);
|
||||||
|
/// @}
|
||||||
|
|
||||||
|
private:
|
||||||
|
sqlite3_backup* mpSQLiteBackup;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace SQLite
|
93
src/Backup.cpp
Normal file
93
src/Backup.cpp
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
/**
|
||||||
|
* @file Backup.cpp
|
||||||
|
* @ingroup SQLiteCpp
|
||||||
|
* @brief Management of a SQLite Database Backup.
|
||||||
|
*
|
||||||
|
* Copyright (c) 2015 Shibao HONG (shibaohong@outlook.com)
|
||||||
|
*
|
||||||
|
* Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
|
||||||
|
* or copy at http://opensource.org/licenses/MIT)
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <SQLiteCpp/Backup.h>
|
||||||
|
|
||||||
|
#include <SQLiteCpp/Exception.h>
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace SQLite
|
||||||
|
{
|
||||||
|
|
||||||
|
Backup::Backup(Database& aDestDatabase,
|
||||||
|
const char *apDestDatabaseName,
|
||||||
|
Database& aSrcDatabase,
|
||||||
|
const char *apSrcDatabaseName) :
|
||||||
|
mpSQLiteBackup(NULL)
|
||||||
|
{
|
||||||
|
mpSQLiteBackup = sqlite3_backup_init(aDestDatabase.getHandle(),
|
||||||
|
apDestDatabaseName,
|
||||||
|
aSrcDatabase.getHandle(),
|
||||||
|
apSrcDatabaseName);
|
||||||
|
if (NULL == mpSQLiteBackup)
|
||||||
|
{
|
||||||
|
std::string strerr = sqlite3_errmsg(aDestDatabase.getHandle());
|
||||||
|
throw SQLite::Exception(strerr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Backup::Backup(Database &aDestDatabase,
|
||||||
|
const std::string &aDestDatabaseName,
|
||||||
|
Database &aSrcDatabase,
|
||||||
|
const std::string &aSrcDatabaseName) :
|
||||||
|
mpSQLiteBackup(NULL)
|
||||||
|
{
|
||||||
|
mpSQLiteBackup = sqlite3_backup_init(aDestDatabase.getHandle(),
|
||||||
|
aDestDatabaseName.c_str(),
|
||||||
|
aSrcDatabase.getHandle(),
|
||||||
|
aSrcDatabaseName.c_str());
|
||||||
|
if (NULL == mpSQLiteBackup)
|
||||||
|
{
|
||||||
|
std::string strerr = sqlite3_errmsg(aDestDatabase.getHandle());
|
||||||
|
throw SQLite::Exception(strerr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Backup::Backup(Database &aDestDatabase, Database &aSrcDatabase) :
|
||||||
|
mpSQLiteBackup(NULL)
|
||||||
|
{
|
||||||
|
mpSQLiteBackup = sqlite3_backup_init(aDestDatabase.getHandle(),
|
||||||
|
"main",
|
||||||
|
aSrcDatabase.getHandle(),
|
||||||
|
"main");
|
||||||
|
if (NULL == mpSQLiteBackup)
|
||||||
|
{
|
||||||
|
std::string strerr = sqlite3_errmsg(aDestDatabase.getHandle());
|
||||||
|
throw SQLite::Exception(strerr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Backup::~Backup() noexcept
|
||||||
|
{
|
||||||
|
if (NULL != mpSQLiteBackup)
|
||||||
|
{
|
||||||
|
sqlite3_backup_finish(mpSQLiteBackup);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int Backup::executeStep(const int aNumPage)
|
||||||
|
{
|
||||||
|
const int res = sqlite3_backup_step(mpSQLiteBackup, aNumPage);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Backup::remainingPageCount()
|
||||||
|
{
|
||||||
|
return sqlite3_backup_remaining(mpSQLiteBackup);
|
||||||
|
}
|
||||||
|
|
||||||
|
int Backup::totalPageCount()
|
||||||
|
{
|
||||||
|
return sqlite3_backup_pagecount(mpSQLiteBackup);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace SQLite
|
Loading…
x
Reference in New Issue
Block a user