int sqlite3_backup_pagecount_idr(void *backup){ DBbackup* dbi=(DBbackup*) backup; int res =sqlite3_backup_pagecount(dbi-> backup); return res; }
void Database::BackupFile(const QString& filename) { qLog(Debug) << "Starting database backup"; QString dest_filename = QString("%1.bak").arg(filename); const int task_id = app_->task_manager()->StartTask(tr("Backing up database")); sqlite3* source_connection = nullptr; sqlite3* dest_connection = nullptr; BOOST_SCOPE_EXIT((source_connection)(dest_connection)(task_id)(app_)) { // Harmless to call sqlite3_close() with a nullptr pointer. sqlite3_close(source_connection); sqlite3_close(dest_connection); app_->task_manager()->SetTaskFinished(task_id); } BOOST_SCOPE_EXIT_END bool success = OpenDatabase(filename, &source_connection); if (!success) { return; } success = OpenDatabase(dest_filename, &dest_connection); if (!success) { return; } sqlite3_backup* backup = sqlite3_backup_init(dest_connection, "main", source_connection, "main"); if (!backup) { const char* error_message = sqlite3_errmsg(dest_connection); qLog(Error) << "Failed to start database backup:" << error_message; return; } int ret = SQLITE_OK; do { ret = sqlite3_backup_step(backup, 16); const int page_count = sqlite3_backup_pagecount(backup); app_->task_manager()->SetTaskProgress( task_id, page_count - sqlite3_backup_remaining(backup), page_count); } while (ret == SQLITE_OK); if (ret != SQLITE_DONE) { qLog(Error) << "Database backup failed"; } sqlite3_backup_finish(backup); }
Int32 CBackup::NativePagecount() { Int32 result = 0; #if HAVE_SQLITE3 && HAVE_SQLITE3_BACKUPAPI hbk *bk = mHandle; if (bk) { if (bk->bkup) { result = sqlite3_backup_pagecount(bk->bkup); } } #else return E_SQL_SQLITE_THROWEX_EXCEPTION; #endif return result; }
/** * @brief Perform an online backup of our database db to the database file named * by zFilename. Used to store our memory table in between map changes to disk * * This function copies 5 database pages from pDb to * zFilename, then unlocks pDb and sleeps for 250 ms, then repeats the * process until the entire database is backed up. * * The third argument passed to this function must be a pointer to a progress * function. After each set of 5 pages is backed up, the progress function * is invoked with two integer parameters: the number of pages left to * copy, and the total number of pages in the source file. This information * may be used, for example, to update a GUI progress bar. * * While this function is running, another thread may use the database pDb, or * another process may access the underlying database file via a separate * connection. * * If the backup process is successfully completed, SQLITE_OK is returned. * Otherwise, if an error occurs, an SQLite error code is returned. * * @param zFilename * * @return * */ int DB_BackupDB(const char *zFilename, void (*xProgress)(int, int)) // Progress function to invoke { int rc; // Function return code sqlite3 *pFile; // Database connection opened on zFilename sqlite3_backup *pBackup; // Backup handle used to copy data // Open the database file identified by zFilename. rc = sqlite3_open(zFilename, &pFile); if (rc == SQLITE_OK) { // Open the sqlite3_backup object used to accomplish the transfer pBackup = sqlite3_backup_init(pFile, "main", db, "main"); if (pBackup) { // Each iteration of this loop copies 5 database pages from database // pDb to the backup database. If the return value of backup_step() // indicates that there are still further pages to copy, sleep for // 250 ms before repeating. do { rc = sqlite3_backup_step(pBackup, 5); xProgress(sqlite3_backup_remaining(pBackup), sqlite3_backup_pagecount(pBackup)); if (rc == SQLITE_OK || rc == SQLITE_BUSY || rc == SQLITE_LOCKED) { sqlite3_sleep(250); } } while (rc == SQLITE_OK || rc == SQLITE_BUSY || rc == SQLITE_LOCKED); // Release resources allocated by backup_init(). (void) sqlite3_backup_finish(pBackup); } rc = sqlite3_errcode(pFile); } // Close the database connection opened on database file zFilename and return the result of this function. (void) sqlite3_close(pFile); return rc; }
int backup::pagecount() { return sqlite3_backup_pagecount(handle); }
static int copy_database(sqlite3 *src, sqlite3 *dst, const char *name) { sqlite3_backup *b; char *errmsg; off_t total; off_t done; off_t page_size; time_t start; time_t elapsed; int ret; assert(src != NULL); assert(dst != NULL); /* Do not remove until gcc has gone from FreeBSD base */ done = total = 0; ret = sqlite3_exec(dst, "PRAGMA main.locking_mode=EXCLUSIVE;" "BEGIN IMMEDIATE;COMMIT;", NULL, NULL, &errmsg); if (ret != SQLITE_OK) { pkg_emit_error("sqlite error -- %s", errmsg); sqlite3_free(errmsg); return (EPKG_FATAL); } ret = sqlite3_exec(dst, "PRAGMA page_size", ps_cb, &page_size, &errmsg); if (ret != SQLITE_OK) { pkg_emit_error("sqlite error -- %s", errmsg); sqlite3_free(errmsg); return (EPKG_FATAL); } b = sqlite3_backup_init(dst, "main", src, "main"); elapsed = -1; done = total = 0; start = time(NULL); do { ret = sqlite3_backup_step(b, NPAGES); if (ret != SQLITE_OK && ret != SQLITE_DONE ) { if (ret == SQLITE_BUSY) { sqlite3_sleep(250); } else { ERROR_SQLITE(dst); break; } } total = sqlite3_backup_pagecount(b) * page_size; done = total - sqlite3_backup_remaining(b) * page_size; /* Callout no more than once a second */ if (elapsed < time(NULL) - start) { elapsed = time(NULL) - start; pkg_emit_fetching(name, total, done, elapsed); } } while(done < total); ret = sqlite3_backup_finish(b); pkg_emit_fetching(name, total, done, time(NULL) - start); sqlite3_exec(dst, "PRAGMA main.locking_mode=NORMAL;" "BEGIN IMMEDIATE;COMMIT;", NULL, NULL, &errmsg); if (ret != SQLITE_OK) { pkg_emit_error("sqlite error -- %s", errmsg); sqlite3_free(errmsg); return (EPKG_FATAL); } return ret; }
static int backupTestCmd( ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const*objv ){ enum BackupSubCommandEnum { BACKUP_STEP, BACKUP_FINISH, BACKUP_REMAINING, BACKUP_PAGECOUNT }; struct BackupSubCommand { const char *zCmd; enum BackupSubCommandEnum eCmd; int nArg; const char *zArg; } aSub[] = { {"step", BACKUP_STEP , 1, "npage" }, {"finish", BACKUP_FINISH , 0, "" }, {"remaining", BACKUP_REMAINING , 0, "" }, {"pagecount", BACKUP_PAGECOUNT , 0, "" }, {0, 0, 0, 0} }; sqlite3_backup *p = (sqlite3_backup *)clientData; int iCmd; int rc; rc = Tcl_GetIndexFromObjStruct( interp, objv[1], aSub, sizeof(aSub[0]), "option", 0, &iCmd ); if( rc!=TCL_OK ){ return rc; } if( objc!=(2 + aSub[iCmd].nArg) ){ Tcl_WrongNumArgs(interp, 2, objv, aSub[iCmd].zArg); return TCL_ERROR; } switch( aSub[iCmd].eCmd ){ case BACKUP_FINISH: { const char *zCmdName; Tcl_CmdInfo cmdInfo; zCmdName = Tcl_GetString(objv[0]); Tcl_GetCommandInfo(interp, zCmdName, &cmdInfo); cmdInfo.deleteProc = 0; Tcl_SetCommandInfo(interp, zCmdName, &cmdInfo); Tcl_DeleteCommand(interp, zCmdName); rc = sqlite3_backup_finish(p); Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC); break; } case BACKUP_STEP: { int nPage; if( TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &nPage) ){ return TCL_ERROR; } rc = sqlite3_backup_step(p, nPage); Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC); break; } case BACKUP_REMAINING: Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_backup_remaining(p))); break; case BACKUP_PAGECOUNT: Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_backup_pagecount(p))); break; } return TCL_OK; }
// Get the number of total source pages to be copied in this backup process int Backup::getTotalPageCount() { return sqlite3_backup_pagecount(mpSQLiteBackup); }
static int copy_database(sqlite3 *src, sqlite3 *dst, const char *name) { sqlite3_backup *b; char *errmsg; off_t total; off_t done; off_t page_size; int ret; assert(src != NULL); assert(dst != NULL); ret = sqlite3_exec(dst, "PRAGMA main.locking_mode=EXCLUSIVE;" "BEGIN IMMEDIATE;COMMIT;", NULL, NULL, &errmsg); if (ret != SQLITE_OK) { pkg_emit_error("sqlite error -- %s", errmsg); sqlite3_free(errmsg); return (EPKG_FATAL); } ret = sqlite3_exec(dst, "PRAGMA page_size", ps_cb, &page_size, &errmsg); if (ret != SQLITE_OK) { pkg_emit_error("sqlite error -- %s", errmsg); sqlite3_free(errmsg); return (EPKG_FATAL); } b = sqlite3_backup_init(dst, "main", src, "main"); done = total = 0; pkg_emit_progress_start(NULL); do { ret = sqlite3_backup_step(b, NPAGES); total = sqlite3_backup_pagecount(b); done = total - sqlite3_backup_remaining(b); pkg_emit_progress_tick(done, total); if (ret != SQLITE_OK && ret != SQLITE_DONE ) { if (ret == SQLITE_BUSY) { sqlite3_sleep(250); } else { ERROR_SQLITE(dst, "backup step"); break; } } } while(done < total); ret = sqlite3_backup_finish(b); pkg_emit_progress_tick(total, total); sqlite3_exec(dst, "PRAGMA main.locking_mode=NORMAL;" "BEGIN IMMEDIATE;COMMIT;", NULL, NULL, &errmsg); if (ret != SQLITE_OK) { pkg_emit_error("sqlite error -- %s", errmsg); sqlite3_free(errmsg); return (EPKG_FATAL); } return ret; }
void Database::sqliteDBMemFile(bool save) { if (save) qWarning() << "sqliteDBMemFile(): from memory to file..."; else qWarning() << "sqliteDBMemFile(): from file to memory..."; int rc = -1; /* Function return code */ QVariant v = QSqlDatabase::database().driver()->handle(); if (v.isValid() && qstrcmp(v.typeName(),"sqlite3*") == 0) { // v.data() returns a pointer to the handle sqlite3 *handle = *static_cast<sqlite3 **>(v.data()); if (handle != 0) { // check that it is not NULL sqlite3 *pInMemory = handle; sqlite3 *pFile; /* Database connection opened on zFilename */ sqlite3_backup *pBackup; /* Backup object used to copy data */ sqlite3 *pTo; /* Database to copy to (pFile or pInMemory) */ sqlite3 *pFrom; /* Database to copy from (pFile or pInMemory) */ /* Open the database file identified by zFilename. Exit early if this fails ** for any reason. */ rc = sqlite3_open(mainApp->dbFileName().toUtf8().data(), &pFile); if (rc == SQLITE_OK) { /* If this is a 'load' operation (isSave==0), then data is copied ** from the database file just opened to database pInMemory. ** Otherwise, if this is a 'save' operation (isSave==1), then data ** is copied from pInMemory to pFile. Set the variables pFrom and ** pTo accordingly. */ pFrom = (save ? pInMemory : pFile); pTo = (save ? pFile : pInMemory); /* Set up the backup procedure to copy from the "main" database of ** connection pFile to the main database of connection pInMemory. ** If something goes wrong, pBackup will be set to NULL and an error ** code and message left in connection pTo. ** ** If the backup object is successfully created, call backup_step() ** to copy data from pFile to pInMemory. Then call backup_finish() ** to release resources associated with the pBackup object. If an ** error occurred, then an error code and message will be left in ** connection pTo. If no error occurred, then the error code belonging ** to pTo is set to SQLITE_OK. */ pBackup = sqlite3_backup_init(pTo, "main", pFrom, "main"); /* Each iteration of this loop copies 5 database pages from database ** pDb to the backup database. If the return value of backup_step() ** indicates that there are still further pages to copy, sleep for ** 250 ms before repeating. */ do { rc = sqlite3_backup_step(pBackup, 10000); if (!mainApp->isNoDebugOutput()) { int remaining = sqlite3_backup_remaining(pBackup); int pagecount = sqlite3_backup_pagecount(pBackup); qDebug() << rc << "backup" << pagecount << "remain" << remaining; } if((rc == SQLITE_OK) || (rc == SQLITE_BUSY) || (rc == SQLITE_LOCKED)) sqlite3_sleep(100); } while((rc == SQLITE_OK) || (rc == SQLITE_BUSY) || (rc == SQLITE_LOCKED)); /* Release resources allocated by backup_init(). */ (void)sqlite3_backup_finish(pBackup); if (rc != SQLITE_DONE) qCritical() << "sqliteDBMemFile(): return code =" << rc; } else { qCritical() << "sqliteDBMemFile(): error open =" << rc; } /* Close the database connection opened on database file zFilename ** and return the result of this function. */ (void)sqlite3_close(pFile); } } qWarning() << "sqliteDBMemFile(): finished!"; }