/ Check-in [27a5c09c]
Login
SQLite training in Houston TX on 2019-11-05 (details)
Part of the 2019 Tcl Conference

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:If the sqlite3_wal_checkpoint() API is passed a NULL pointer in place of a database name, attempt to checkpoint all attached databases.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | wal
Files: files | file ages | folders
SHA1: 27a5c09ce8a35039d844d08cfe5698e8b438abfe
User & Date: dan 2010-05-03 14:05:43
Context
2010-05-03
14:08
Merge the write-ahead-logging changes into the trunk. check-in: de9ae443 user: drh tags: trunk
14:05
If the sqlite3_wal_checkpoint() API is passed a NULL pointer in place of a database name, attempt to checkpoint all attached databases. Closed-Leaf check-in: 27a5c09c user: dan tags: wal
13:37
Make sure the mutex is held while calling sqlite3ApiExit() in sqlite3_wal_checkpoint(). Other cleanup of WAL logic. check-in: 11a85b82 user: drh tags: wal
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/main.c.

  1256   1256   ** Checkpoint database zDb. If zDb is NULL, the main database is checkpointed.
  1257   1257   */
  1258   1258   int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb){
  1259   1259   #ifdef SQLITE_OMIT_WAL
  1260   1260     return SQLITE_OK;
  1261   1261   #else
  1262   1262     int rc;                         /* Return code */
  1263         -  int iDb = 0;                    /* sqlite3.aDb[] index of db to checkpoint */
         1263  +  int iDb = SQLITE_MAX_ATTACHED;  /* sqlite3.aDb[] index of db to checkpoint */
  1264   1264   
  1265   1265     sqlite3_mutex_enter(db->mutex);
  1266   1266     if( zDb ){
  1267   1267       iDb = sqlite3FindDbName(db, zDb);
  1268   1268     }
  1269   1269     if( iDb<0 ){
  1270   1270       rc = SQLITE_ERROR;
................................................................................
  1280   1280   }
  1281   1281   
  1282   1282   #ifndef SQLITE_OMIT_WAL
  1283   1283   /*
  1284   1284   ** Run a checkpoint on database iDb. This is a no-op if database iDb is
  1285   1285   ** not currently open in WAL mode.
  1286   1286   **
  1287         -** If a transaction is open at either the database handle (db) or b-tree
  1288         -** level, this function returns SQLITE_LOCKED and a checkpoint is not
  1289         -** attempted. If an error occurs while running the checkpoint, an SQLite
  1290         -** error code is returned (i.e. SQLITE_IOERR). Otherwise, SQLITE_OK.
         1287  +** If a transaction is open on the database being checkpointed, this 
         1288  +** function returns SQLITE_LOCKED and a checkpoint is not attempted. If 
         1289  +** an error occurs while running the checkpoint, an SQLite error code is 
         1290  +** returned (i.e. SQLITE_IOERR). Otherwise, SQLITE_OK.
  1291   1291   **
  1292   1292   ** The mutex on database handle db should be held by the caller. The mutex
  1293   1293   ** associated with the specific b-tree being checkpointed is taken by
  1294   1294   ** this function while the checkpoint is running.
         1295  +**
         1296  +** If iDb is passed SQLITE_MAX_ATTACHED, then all attached databases are
         1297  +** checkpointed. If an error is encountered it is returned immediately -
         1298  +** no attempt is made to checkpoint any remaining databases.
  1295   1299   */
  1296   1300   int sqlite3Checkpoint(sqlite3 *db, int iDb){
  1297         -  Btree *pBt;                     /* Btree handle to checkpoint */
  1298         -  int rc;                         /* Return code */
         1301  +  int rc = SQLITE_OK;             /* Return code */
         1302  +  int i;                          /* Used to iterate through attached dbs */
  1299   1303   
  1300   1304     assert( sqlite3_mutex_held(db->mutex) );
  1301   1305   
  1302         -  pBt = db->aDb[iDb].pBt;
  1303         -  if( sqlite3BtreeIsInReadTrans(pBt) ){
  1304         -    rc = SQLITE_LOCKED;
  1305         -  }else{
  1306         -    sqlite3BtreeEnter(pBt);
  1307         -    rc = sqlite3PagerCheckpoint(sqlite3BtreePager(pBt));
  1308         -    sqlite3BtreeLeave(pBt);
         1306  +  for(i=0; i<db->nDb && rc==SQLITE_OK; i++){
         1307  +    if( i==iDb || iDb==SQLITE_MAX_ATTACHED ){
         1308  +      Btree *pBt = db->aDb[i].pBt;
         1309  +      if( pBt ){
         1310  +        if( sqlite3BtreeIsInReadTrans(pBt) ){
         1311  +          rc = SQLITE_LOCKED;
         1312  +        }else{
         1313  +          sqlite3BtreeEnter(pBt);
         1314  +          rc = sqlite3PagerCheckpoint(sqlite3BtreePager(pBt));
         1315  +          sqlite3BtreeLeave(pBt);
         1316  +        }
         1317  +      }
         1318  +    }
  1309   1319     }
  1310   1320   
  1311   1321     return rc;
  1312   1322   }
  1313   1323   #endif /* SQLITE_OMIT_WAL */
  1314   1324   
  1315   1325   /*