Skip to content

Commit

Permalink
Merge pull request #36 from iwongu/backup
Browse files Browse the repository at this point in the history
Supports backup methods in database class.
  • Loading branch information
iwongu authored Jun 15, 2016
2 parents 9a594a6 + 22ad8b6 commit b284519
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 0 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,26 @@ sqlite3pp::query qry(
"SELECT epi.* FROM episodes epi, test.contacts con WHERE epi.id = con.id");
```
## backup
```cpp
sqlite3pp::database db("test.db");
sqlite3pp::database backupdb("backup.db");
db.backup(backupdb);
```

```cpp
db.backup(
backupdb,
[](int pagecount, int remaining, int rc) {
cout << pagecount << "/" << remaining << endl;
if (rc == SQLITE_OK || rc == SQLITE_BUSY || rc == SQLITE_LOCKED) {
// sleep or do nothing.
}
});
```
## callback
```cpp
Expand Down
4 changes: 4 additions & 0 deletions headeronly_src/sqlite3pp.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ namespace sqlite3pp
using rollback_handler = std::function<void ()>;
using update_handler = std::function<void (int, char const*, char const*, long long int)>;
using authorize_handler = std::function<int (int, char const*, char const*, char const*, char const*)>;
using backup_handler = std::function<void (int, int, int)>;

explicit database(char const* dbname = nullptr, int flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, const char* vfs = nullptr);

Expand All @@ -92,6 +93,9 @@ namespace sqlite3pp
int attach(char const* dbname, char const* name);
int detach(char const* name);

int backup(database& destdb, backup_handler h = {});
int backup(char const* dbname, database& destdb, char const* destdbname, backup_handler h, int step_page = 5);

long long int last_insert_rowid() const;

int enable_foreign_keys(bool enable = true);
Expand Down
22 changes: 22 additions & 0 deletions headeronly_src/sqlite3pp.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,28 @@ namespace sqlite3pp
return executef("DETACH '%q'", name);
}

inline int database::backup(database& destdb, backup_handler h)
{
return backup("main", destdb, "main", h);
}

inline int database::backup(char const* dbname, database& destdb, char const* destdbname, backup_handler h, int step_page)
{
sqlite3_backup* bkup = sqlite3_backup_init(destdb.db_, destdbname, db_, dbname);
if (!bkup) {
return error_code();
}
auto rc = SQLITE_OK;
do {
rc = sqlite3_backup_step(bkup, step_page);
if (h) {
h(sqlite3_backup_remaining(bkup), sqlite3_backup_pagecount(bkup), rc);
}
} while (rc == SQLITE_OK || rc == SQLITE_BUSY || rc == SQLITE_LOCKED);
sqlite3_backup_finish(bkup);
return rc;
}

inline void database::set_busy_handler(busy_handler h)
{
bh_ = h;
Expand Down
22 changes: 22 additions & 0 deletions src/sqlite3pp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,28 @@ namespace sqlite3pp
return executef("DETACH '%q'", name);
}

int database::backup(database& destdb, backup_handler h)
{
return backup("main", destdb, "main", h);
}

int database::backup(char const* dbname, database& destdb, char const* destdbname, backup_handler h, int step_page)
{
sqlite3_backup* bkup = sqlite3_backup_init(destdb.db_, destdbname, db_, dbname);
if (!bkup) {
return error_code();
}
auto rc = SQLITE_OK;
do {
rc = sqlite3_backup_step(bkup, step_page);
if (h) {
h(sqlite3_backup_remaining(bkup), sqlite3_backup_pagecount(bkup), rc);
}
} while (rc == SQLITE_OK || rc == SQLITE_BUSY || rc == SQLITE_LOCKED);
sqlite3_backup_finish(bkup);
return rc;
}

void database::set_busy_handler(busy_handler h)
{
bh_ = h;
Expand Down
4 changes: 4 additions & 0 deletions src/sqlite3pp.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ namespace sqlite3pp
using rollback_handler = std::function<void ()>;
using update_handler = std::function<void (int, char const*, char const*, long long int)>;
using authorize_handler = std::function<int (int, char const*, char const*, char const*, char const*)>;
using backup_handler = std::function<void (int, int, int)>;

explicit database(char const* dbname = nullptr, int flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, const char* vfs = nullptr);

Expand All @@ -93,6 +94,9 @@ namespace sqlite3pp
int attach(char const* dbname, char const* name);
int detach(char const* name);

int backup(database& destdb, backup_handler h = {});
int backup(char const* dbname, database& destdb, char const* destdbname, backup_handler h, int step_page = 5);

long long int last_insert_rowid() const;

int enable_foreign_keys(bool enable = true);
Expand Down
25 changes: 25 additions & 0 deletions test/testbackup.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <iostream>
#include "sqlite3pp.h"

using namespace std;

int main()
{
try {
sqlite3pp::database db("test.db");
sqlite3pp::database backupdb("backup.db");

db.backup(
backupdb,
[](int pagecount, int remaining, int rc) {
cout << pagecount << "/" << remaining << endl;
if (rc == SQLITE_OK || rc == SQLITE_BUSY || rc == SQLITE_LOCKED) {
// sleep(250);
}
});
}
catch (exception& ex) {
cout << ex.what() << endl;
}

}

0 comments on commit b284519

Please sign in to comment.