SQLiteXX  0.1.0
 All Classes Namespaces Files Functions Enumerations Enumerator
Backup.cpp
1 #include "Backup.h"
2 #include "Exception.h"
3 
4 
5 namespace sqlite
6 {
7  void save(const dbconnection& source, const std::string& filename)
8  {
9  dbconnection destination(filename);
10  backup backup(source, destination);
11  backup.step();
12  }
13 
15  dbconnection const & source,
16  dbconnection const & destination,
17  const std::string &sourceName,
18  const std::string &destinationName) :
19  m_handle(
20  sqlite3_backup_init(
21  destination.handle(),
22  destinationName.c_str(),
23  source.handle(),
24  sourceName.c_str()),
25  sqlite3_backup_finish),
26  m_destination(&destination)
27  {
28  if (!m_handle)
29  {
30  throw_error_code(destination.handle());
31  }
32  }
33 
34  bool backup::step(const int pages)
35  {
36  int const result = sqlite3_backup_step(m_handle.get(), pages);
37 
38  if (result == SQLITE_OK) return true;
39  if (result == SQLITE_DONE) return false;
40 
41  m_handle.reset();
42  throw_error_code(m_destination->handle());
43  return false;
44  }
45 
46  int backup::total_page_count() noexcept
47  {
48  return sqlite3_backup_pagecount(m_handle.get());
49  }
50 
52  {
53  return sqlite3_backup_remaining(m_handle.get());
54  }
55 
56  sqlite3_backup* backup::handle() noexcept
57  {
58  return m_handle.get();
59  }
60 }
sqlite3 * handle() const noexcept
Returns pointer to the underlying "sqlite3" object.
Used to aid in the process of backing up a database.
Definition: Backup.h:20
backup(const dbconnection &source, const dbconnection &destination, const std::string &sourceName="main", const std::string &destinationName="main")
Construct a backup object.
Definition: Backup.cpp:14
int remaining_page_count() noexcept
Returns the number of pages still to be backed up.
Definition: Backup.cpp:51
int total_page_count() noexcept
Returns the total number of pages in the source database.
Definition: Backup.cpp:46
bool step(const int pages=-1)
Will copy a specified number of pages to the destination database.
Definition: Backup.cpp:34
sqlite3_backup * handle() noexcept
Returns the pointer to the underlying sqlite3_backup object.
Definition: Backup.cpp:56
Class that represents a connection to a database.
Definition: DBConnection.h:24