Skip to content

Commit 05d304b

Browse files
committed
add Backup class
1 parent 7fa11f6 commit 05d304b

File tree

3 files changed

+161
-0
lines changed

3 files changed

+161
-0
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ set(SQLITECPP_SRC
9090
${PROJECT_SOURCE_DIR}/src/Database.cpp
9191
${PROJECT_SOURCE_DIR}/src/Statement.cpp
9292
${PROJECT_SOURCE_DIR}/src/Transaction.cpp
93+
${PROJECT_SOURCE_DIR}/src/Backup.cpp
9394
)
9495
source_group(src FILES ${SQLITECPP_SRC})
9596

@@ -102,6 +103,7 @@ set(SQLITECPP_INC
102103
${PROJECT_SOURCE_DIR}/include/SQLiteCpp/Exception.h
103104
${PROJECT_SOURCE_DIR}/include/SQLiteCpp/Statement.h
104105
${PROJECT_SOURCE_DIR}/include/SQLiteCpp/Transaction.h
106+
${PROJECT_SOURCE_DIR}/include/SQLiteCpp/Backup.h
105107
)
106108
source_group(inc FILES ${SQLITECPP_INC})
107109

include/SQLiteCpp/Backup.h

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* @file Backup.h
3+
* @ingroup SQLiteCpp
4+
* @brief Management of a SQLite Database Backup.
5+
*
6+
* Copyright (c) 2015 Shibao HONG ([email protected])
7+
*
8+
* Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
9+
* or copy at http://opensource.org/licenses/MIT)
10+
*/
11+
#pragma once
12+
13+
#include <sqlite3.h>
14+
15+
#include <SQLiteCpp/Database.h>
16+
17+
#include <string>
18+
19+
namespace SQLite
20+
{
21+
22+
class Backup
23+
{
24+
public:
25+
Backup(Database& aDestDatabase,
26+
const char* apDestDatabaseName,
27+
Database& aSrcDatabase,
28+
const char* apSrcDatabaseName);
29+
30+
Backup(Database& aDestDatabase,
31+
const std::string& aDestDatabaseName,
32+
Database& aSrcDatabase,
33+
const std::string& aSrcDatabaseName);
34+
35+
Backup(Database& aDestDatabase,
36+
Database& aSrcDatabase);
37+
38+
virtual ~Backup() noexcept;
39+
40+
int executeStep(const int aNumPage = -1);
41+
42+
int remainingPageCount();
43+
44+
int totalPageCount();
45+
46+
/**
47+
* @brief Return raw pointer to SQLite Database Backup Handle.
48+
*
49+
* This is often needed to mix this wrapper with other libraries or for advance usage not supported by SQLiteCpp.
50+
*/
51+
inline sqlite3_backup* getHandle() const noexcept // nothrow
52+
{
53+
return mpSQLiteBackup;
54+
}
55+
56+
private:
57+
/// @{ Backup must be non-copyable
58+
Backup(const Backup&);
59+
Backup& operator=(const Backup&);
60+
/// @}
61+
62+
private:
63+
sqlite3_backup* mpSQLiteBackup;
64+
};
65+
66+
} // namespace SQLite

src/Backup.cpp

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/**
2+
* @file Backup.cpp
3+
* @ingroup SQLiteCpp
4+
* @brief Management of a SQLite Database Backup.
5+
*
6+
* Copyright (c) 2015 Shibao HONG ([email protected])
7+
*
8+
* Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
9+
* or copy at http://opensource.org/licenses/MIT)
10+
*/
11+
12+
#include <SQLiteCpp/Backup.h>
13+
14+
#include <SQLiteCpp/Exception.h>
15+
16+
#include <string>
17+
18+
namespace SQLite
19+
{
20+
21+
Backup::Backup(Database& aDestDatabase,
22+
const char *apDestDatabaseName,
23+
Database& aSrcDatabase,
24+
const char *apSrcDatabaseName) :
25+
mpSQLiteBackup(NULL)
26+
{
27+
mpSQLiteBackup = sqlite3_backup_init(aDestDatabase.getHandle(),
28+
apDestDatabaseName,
29+
aSrcDatabase.getHandle(),
30+
apSrcDatabaseName);
31+
if (NULL == mpSQLiteBackup)
32+
{
33+
std::string strerr = sqlite3_errmsg(aDestDatabase.getHandle());
34+
throw SQLite::Exception(strerr);
35+
}
36+
}
37+
38+
Backup::Backup(Database &aDestDatabase,
39+
const std::string &aDestDatabaseName,
40+
Database &aSrcDatabase,
41+
const std::string &aSrcDatabaseName) :
42+
mpSQLiteBackup(NULL)
43+
{
44+
mpSQLiteBackup = sqlite3_backup_init(aDestDatabase.getHandle(),
45+
aDestDatabaseName.c_str(),
46+
aSrcDatabase.getHandle(),
47+
aSrcDatabaseName.c_str());
48+
if (NULL == mpSQLiteBackup)
49+
{
50+
std::string strerr = sqlite3_errmsg(aDestDatabase.getHandle());
51+
throw SQLite::Exception(strerr);
52+
}
53+
}
54+
55+
Backup::Backup(Database &aDestDatabase, Database &aSrcDatabase) :
56+
mpSQLiteBackup(NULL)
57+
{
58+
mpSQLiteBackup = sqlite3_backup_init(aDestDatabase.getHandle(),
59+
"main",
60+
aSrcDatabase.getHandle(),
61+
"main");
62+
if (NULL == mpSQLiteBackup)
63+
{
64+
std::string strerr = sqlite3_errmsg(aDestDatabase.getHandle());
65+
throw SQLite::Exception(strerr);
66+
}
67+
}
68+
69+
Backup::~Backup() noexcept
70+
{
71+
if (NULL != mpSQLiteBackup)
72+
{
73+
sqlite3_backup_finish(mpSQLiteBackup);
74+
}
75+
}
76+
77+
int Backup::executeStep(const int aNumPage)
78+
{
79+
const int res = sqlite3_backup_step(mpSQLiteBackup, aNumPage);
80+
return res;
81+
}
82+
83+
int Backup::remainingPageCount()
84+
{
85+
return sqlite3_backup_remaining(mpSQLiteBackup);
86+
}
87+
88+
int Backup::totalPageCount()
89+
{
90+
return sqlite3_backup_pagecount(mpSQLiteBackup);
91+
}
92+
93+
} // namespace SQLite

0 commit comments

Comments
 (0)