4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give.
11 *************************************************************************
16 ** The RBU extension requires that the RBU update be packaged as an
17 ** SQLite database. The tables it expects to find are described in
18 ** sqlite3rbu.h. Essentially, for each table xyz in the target database
19 ** that the user wishes to write to, a corresponding data_xyz table is
20 ** created in the RBU database and populated with one row for each row to
21 ** update, insert or delete from the target table.
23 ** The update proceeds in three stages:
25 ** 1) The database is updated. The modified database pages are written
26 ** to a *-oal file. A *-oal file is just like a *-wal file, except
27 ** that it is named "<database>-oal" instead of "<database>-wal".
28 ** Because regular SQLite clients do not look for file named
29 ** "<database>-oal", they go on using the original database in
30 ** rollback mode while the *-oal file is being generated.
32 ** During this stage RBU does not update the database by writing
33 ** directly to the target tables. Instead it creates "imposter"
34 ** tables using the SQLITE_TESTCTRL_IMPOSTER interface that it uses
35 ** to update each b-tree individually. All updates required by each
36 ** b-tree are completed before moving on to the next, and all
37 ** updates are done in sorted key order.
39 ** 2) The "<database>-oal" file is moved to the equivalent "<database>-wal"
40 ** location using a call to rename(2). Before doing this the RBU
41 ** module takes an EXCLUSIVE lock on the database file, ensuring
42 ** that there are no other active readers.
44 ** Once the EXCLUSIVE lock is released, any other database readers
45 ** detect the new *-wal file and read the database in wal mode. At
46 ** this point they see the new version of the database - including
47 ** the updates made as part of the RBU update.
49 ** 3) The new *-wal file is checkpointed. This proceeds in the same way
50 ** as a regular database checkpoint, except that a single frame is
51 ** checkpointed each time sqlite3rbu_step() is called. If the RBU
52 ** handle is closed before the entire *-wal file is checkpointed,
53 ** the checkpoint progress is saved in the RBU database and the
54 ** checkpoint can be resumed by another RBU client at some point in
59 ** The rename() call might not be portable. And RBU is not currently
60 ** syncing the directory after renaming the file.
62 ** When state is saved, any commit to the *-oal file and the commit to
63 ** the RBU update database are not atomic. So if the power fails at the
64 ** wrong moment they might get out of sync. As the main database will be
65 ** committed before the RBU update database this will likely either just
66 ** pass unnoticed, or result in SQLITE_CONSTRAINT errors (due to UNIQUE
67 ** constraint violations).
69 ** If some client does modify the target database mid RBU update, or some
70 ** other error occurs, the RBU extension will keep throwing errors. It's
71 ** not really clear how to get out of this state. The system could just
72 ** by delete the RBU update database and *-oal file and have the device
73 ** download the update again and start over.
75 ** At present, for an UPDATE, both the new.* and old.* records are
76 ** collected in the rbu_xyz table. And for both UPDATEs and DELETEs all
77 ** fields are collected. This means we're probably writing a lot more
78 ** data to disk when saving the state of an ongoing update to the RBU
79 ** update database than is strictly necessary.
89 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RBU)
90 #include "sqlite3rbu.h"
92 #if defined(_WIN32_WCE)
96 /* Maximum number of prepared UPDATE statements held by this module */
97 #define SQLITE_RBU_UPDATE_CACHESIZE 16
99 /* Delta checksums disabled by default. Compile with -DRBU_ENABLE_DELTA_CKSUM
100 ** to enable checksum verification.
102 #ifndef RBU_ENABLE_DELTA_CKSUM
103 # define RBU_ENABLE_DELTA_CKSUM 0
107 ** Swap two objects of type TYPE.
109 #if !defined(SQLITE_AMALGAMATION)
110 # define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
114 ** The rbu_state table is used to save the state of a partially applied
115 ** update so that it can be resumed later. The table consists of integer
116 ** keys mapped to values as follows:
119 ** May be set to integer values 1, 2, 4 or 5. As follows:
120 ** 1: the *-rbu file is currently under construction.
121 ** 2: the *-rbu file has been constructed, but not yet moved
122 ** to the *-wal path.
123 ** 4: the checkpoint is underway.
124 ** 5: the rbu update has been checkpointed.
127 ** Only valid if STAGE==1. The target database name of the table
128 ** currently being written.
131 ** Only valid if STAGE==1. The target database name of the index
132 ** currently being written, or NULL if the main table is currently being
136 ** Only valid if STAGE==1. Number of rows already processed for the current
139 ** RBU_STATE_PROGRESS:
140 ** Trbul number of sqlite3rbu_step() calls made so far as part of this
144 ** Valid if STAGE==4. The 64-bit checksum associated with the wal-index
145 ** header created by recovering the *-wal file. This is used to detect
146 ** cases when another client appends frames to the *-wal file in the
147 ** middle of an incremental checkpoint (an incremental checkpoint cannot
148 ** be continued if this happens).
151 ** Valid if STAGE==1. The current change-counter cookie value in the
155 ** Valid if STAGE==1. The size in bytes of the *-oal file.
157 ** RBU_STATE_DATATBL:
158 ** Only valid if STAGE==1. The RBU database name of the table
159 ** currently being read.
161 #define RBU_STATE_STAGE 1
162 #define RBU_STATE_TBL 2
163 #define RBU_STATE_IDX 3
164 #define RBU_STATE_ROW 4
165 #define RBU_STATE_PROGRESS 5
166 #define RBU_STATE_CKPT 6
167 #define RBU_STATE_COOKIE 7
168 #define RBU_STATE_OALSZ 8
169 #define RBU_STATE_PHASEONESTEP 9
170 #define RBU_STATE_DATATBL 10
172 #define RBU_STAGE_OAL 1
173 #define RBU_STAGE_MOVE 2
174 #define RBU_STAGE_CAPTURE 3
175 #define RBU_STAGE_CKPT 4
176 #define RBU_STAGE_DONE 5
179 #define RBU_CREATE_STATE \
180 "CREATE TABLE IF NOT EXISTS %s.rbu_state(k INTEGER PRIMARY KEY, v)"
182 typedef struct RbuFrame RbuFrame
;
183 typedef struct RbuObjIter RbuObjIter
;
184 typedef struct RbuState RbuState
;
185 typedef struct rbu_vfs rbu_vfs
;
186 typedef struct rbu_file rbu_file
;
187 typedef struct RbuUpdateStmt RbuUpdateStmt
;
189 #if !defined(SQLITE_AMALGAMATION)
190 typedef unsigned int u32
;
191 typedef unsigned short u16
;
192 typedef unsigned char u8
;
193 typedef sqlite3_int64 i64
;
197 ** These values must match the values defined in wal.c for the equivalent
198 ** locks. These are not magic numbers as they are part of the SQLite file
201 #define WAL_LOCK_WRITE 0
202 #define WAL_LOCK_CKPT 1
203 #define WAL_LOCK_READ0 3
205 #define SQLITE_FCNTL_RBUCNT 5149216
208 ** A structure to store values read from the rbu_state table in memory.
223 struct RbuUpdateStmt
{
224 char *zMask
; /* Copy of update mask used with pUpdate */
225 sqlite3_stmt
*pUpdate
; /* Last update statement (or NULL) */
226 RbuUpdateStmt
*pNext
;
230 ** An iterator of this type is used to iterate through all objects in
231 ** the target database that require updating. For each such table, the
232 ** iterator visits, in order:
234 ** * the table itself,
235 ** * each index of the table (zero or more points to visit), and
236 ** * a special "cleanup table" state.
239 ** If the table has no indexes on it, abIndexed is set to NULL. Otherwise,
240 ** it points to an array of flags nTblCol elements in size. The flag is
241 ** set for each column that is either a part of the PK or a part of an
242 ** index. Or clear otherwise.
246 sqlite3_stmt
*pTblIter
; /* Iterate through tables */
247 sqlite3_stmt
*pIdxIter
; /* Index iterator */
248 int nTblCol
; /* Size of azTblCol[] array */
249 char **azTblCol
; /* Array of unquoted target column names */
250 char **azTblType
; /* Array of target column types */
251 int *aiSrcOrder
; /* src table col -> target table col */
252 u8
*abTblPk
; /* Array of flags, set on target PK columns */
253 u8
*abNotNull
; /* Array of flags, set on NOT NULL columns */
254 u8
*abIndexed
; /* Array of flags, set on indexed & PK cols */
255 int eType
; /* Table type - an RBU_PK_XXX value */
257 /* Output variables. zTbl==0 implies EOF. */
258 int bCleanup
; /* True in "cleanup" state */
259 const char *zTbl
; /* Name of target db table */
260 const char *zDataTbl
; /* Name of rbu db table (or null) */
261 const char *zIdx
; /* Name of target db index (or null) */
262 int iTnum
; /* Root page of current object */
263 int iPkTnum
; /* If eType==EXTERNAL, root of PK index */
264 int bUnique
; /* Current index is unique */
265 int nIndex
; /* Number of aux. indexes on table zTbl */
267 /* Statements created by rbuObjIterPrepareAll() */
268 int nCol
; /* Number of columns in current object */
269 sqlite3_stmt
*pSelect
; /* Source data */
270 sqlite3_stmt
*pInsert
; /* Statement for INSERT operations */
271 sqlite3_stmt
*pDelete
; /* Statement for DELETE ops */
272 sqlite3_stmt
*pTmpInsert
; /* Insert into rbu_tmp_$zDataTbl */
274 /* Last UPDATE used (for PK b-tree updates only), or NULL. */
275 RbuUpdateStmt
*pRbuUpdate
;
279 ** Values for RbuObjIter.eType
281 ** 0: Table does not exist (error)
282 ** 1: Table has an implicit rowid.
283 ** 2: Table has an explicit IPK column.
284 ** 3: Table has an external PK index.
285 ** 4: Table is WITHOUT ROWID.
286 ** 5: Table is a virtual table.
288 #define RBU_PK_NOTABLE 0
289 #define RBU_PK_NONE 1
291 #define RBU_PK_EXTERNAL 3
292 #define RBU_PK_WITHOUT_ROWID 4
293 #define RBU_PK_VTAB 5
297 ** Within the RBU_STAGE_OAL stage, each call to sqlite3rbu_step() performs
298 ** one of the following operations.
300 #define RBU_INSERT 1 /* Insert on a main table b-tree */
301 #define RBU_DELETE 2 /* Delete a row from a main table b-tree */
302 #define RBU_REPLACE 3 /* Delete and then insert a row */
303 #define RBU_IDX_DELETE 4 /* Delete a row from an aux. index b-tree */
304 #define RBU_IDX_INSERT 5 /* Insert on an aux. index b-tree */
306 #define RBU_UPDATE 6 /* Update a row in a main table b-tree */
309 ** A single step of an incremental checkpoint - frame iWalFrame of the wal
310 ** file should be copied to page iDbPage of the database file.
321 ** If the RBU database contains an rbu_count table, this value is set to
322 ** a running estimate of the number of b-tree operations required to
323 ** finish populating the *-oal file. This allows the sqlite3_bp_progress()
324 ** API to calculate the permyriadage progress of populating the *-oal file
325 ** using the formula:
327 ** permyriadage = (10000 * nProgress) / nPhaseOneStep
329 ** nPhaseOneStep is initialized to the sum of:
331 ** nRow * (nIndex + 1)
333 ** for all source tables in the RBU database, where nRow is the number
334 ** of rows in the source table and nIndex the number of indexes on the
335 ** corresponding target database table.
337 ** This estimate is accurate if the RBU update consists entirely of
338 ** INSERT operations. However, it is inaccurate if:
340 ** * the RBU update contains any UPDATE operations. If the PK specified
341 ** for an UPDATE operation does not exist in the target table, then
342 ** no b-tree operations are required on index b-trees. Or if the
343 ** specified PK does exist, then (nIndex*2) such operations are
344 ** required (one delete and one insert on each index b-tree).
346 ** * the RBU update contains any DELETE operations for which the specified
347 ** PK does not exist. In this case no operations are required on index
350 ** * the RBU update contains REPLACE operations. These are similar to
351 ** UPDATE operations.
353 ** nPhaseOneStep is updated to account for the conditions above during the
354 ** first pass of each source table. The updated nPhaseOneStep value is
355 ** stored in the rbu_state table if the RBU update is suspended.
358 int eStage
; /* Value of RBU_STATE_STAGE field */
359 sqlite3
*dbMain
; /* target database handle */
360 sqlite3
*dbRbu
; /* rbu database handle */
361 char *zTarget
; /* Path to target db */
362 char *zRbu
; /* Path to rbu db */
363 char *zState
; /* Path to state db (or NULL if zRbu) */
364 char zStateDb
[5]; /* Db name for state ("stat" or "main") */
365 int rc
; /* Value returned by last rbu_step() call */
366 char *zErrmsg
; /* Error message if rc!=SQLITE_OK */
367 int nStep
; /* Rows processed for current object */
368 int nProgress
; /* Rows processed for all objects */
369 RbuObjIter objiter
; /* Iterator for skipping through tbl/idx */
370 const char *zVfsName
; /* Name of automatically created rbu vfs */
371 rbu_file
*pTargetFd
; /* File handle open on target db */
372 int nPagePerSector
; /* Pages per sector for pTargetFd */
376 /* The following state variables are used as part of the incremental
377 ** checkpoint stage (eStage==RBU_STAGE_CKPT). See comments surrounding
378 ** function rbuSetupCheckpoint() for details. */
379 u32 iMaxFrame
; /* Largest iWalFrame value in aFrame[] */
381 int nFrame
; /* Entries in aFrame[] array */
382 int nFrameAlloc
; /* Allocated size of aFrame[] array */
387 i64 szTemp
; /* Current size of all temp files in use */
388 i64 szTempLimit
; /* Total size limit for temp files */
390 /* Used in RBU vacuum mode only */
391 int nRbu
; /* Number of RBU VFS in the stack */
392 rbu_file
*pRbuFd
; /* Fd for main db of dbRbu */
396 ** An rbu VFS is implemented using an instance of this structure.
398 ** Variable pRbu is only non-NULL for automatically created RBU VFS objects.
399 ** It is NULL for RBU VFS objects created explicitly using
400 ** sqlite3rbu_create_vfs(). It is used to track the total amount of temp
401 ** space used by the RBU handle.
404 sqlite3_vfs base
; /* rbu VFS shim methods */
405 sqlite3_vfs
*pRealVfs
; /* Underlying VFS */
406 sqlite3_mutex
*mutex
; /* Mutex to protect pMain */
407 sqlite3rbu
*pRbu
; /* Owner RBU object */
408 rbu_file
*pMain
; /* Linked list of main db files */
412 ** Each file opened by an rbu VFS is represented by an instance of
413 ** the following structure.
415 ** If this is a temporary file (pRbu!=0 && flags&DELETE_ON_CLOSE), variable
416 ** "sz" is set to the current size of the database file.
419 sqlite3_file base
; /* sqlite3_file methods */
420 sqlite3_file
*pReal
; /* Underlying file handle */
421 rbu_vfs
*pRbuVfs
; /* Pointer to the rbu_vfs object */
422 sqlite3rbu
*pRbu
; /* Pointer to rbu object (rbu target only) */
423 i64 sz
; /* Size of file in bytes (temp only) */
425 int openFlags
; /* Flags this file was opened with */
426 u32 iCookie
; /* Cookie value for main db files */
427 u8 iWriteVer
; /* "write-version" value for main db files */
428 u8 bNolock
; /* True to fail EXCLUSIVE locks */
430 int nShm
; /* Number of entries in apShm[] array */
431 char **apShm
; /* Array of mmap'd *-shm regions */
432 char *zDel
; /* Delete this when closing file */
434 const char *zWal
; /* Wal filename for this main db file */
435 rbu_file
*pWalFd
; /* Wal file descriptor for this main db */
436 rbu_file
*pMainNext
; /* Next MAIN_DB file */
440 ** True for an RBU vacuum handle, or false otherwise.
442 #define rbuIsVacuum(p) ((p)->zTarget==0)
445 /*************************************************************************
446 ** The following three functions, found below:
449 ** rbuDeltaChecksum()
452 ** are lifted from the fossil source code (http://fossil-scm.org). They
453 ** are used to implement the scalar SQL function rbu_fossil_delta().
457 ** Read bytes from *pz and convert them into a positive integer. When
458 ** finished, leave *pz pointing to the first character past the end of
459 ** the integer. The *pLen parameter holds the length of the string
460 ** in *pz and is decremented once for each character in the integer.
462 static unsigned int rbuDeltaGetInt(const char **pz
, int *pLen
){
463 static const signed char zValue
[] = {
464 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
465 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
466 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
467 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
468 -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
469 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, 36,
470 -1, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
471 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, -1, -1, -1, 63, -1,
475 unsigned char *z
= (unsigned char*)*pz
;
476 unsigned char *zStart
= z
;
477 while( (c
= zValue
[0x7f&*(z
++)])>=0 ){
486 #if RBU_ENABLE_DELTA_CKSUM
488 ** Compute a 32-bit checksum on the N-byte buffer. Return the result.
490 static unsigned int rbuDeltaChecksum(const char *zIn
, size_t N
){
491 const unsigned char *z
= (const unsigned char *)zIn
;
497 sum0
+= ((unsigned)z
[0] + z
[4] + z
[8] + z
[12]);
498 sum1
+= ((unsigned)z
[1] + z
[5] + z
[9] + z
[13]);
499 sum2
+= ((unsigned)z
[2] + z
[6] + z
[10]+ z
[14]);
500 sum3
+= ((unsigned)z
[3] + z
[7] + z
[11]+ z
[15]);
512 sum3
+= (sum2
<< 8) + (sum1
<< 16) + (sum0
<< 24);
514 case 3: sum3
+= (z
[2] << 8);
515 case 2: sum3
+= (z
[1] << 16);
516 case 1: sum3
+= (z
[0] << 24);
526 ** The output buffer should be big enough to hold the whole output
527 ** file and a NUL terminator at the end. The delta_output_size()
528 ** routine will determine this size for you.
530 ** The delta string should be null-terminated. But the delta string
531 ** may contain embedded NUL characters (if the input and output are
532 ** binary files) so we also have to pass in the length of the delta in
533 ** the lenDelta parameter.
535 ** This function returns the size of the output file in bytes (excluding
536 ** the final NUL terminator character). Except, if the delta string is
537 ** malformed or intended for use with a source file other than zSrc,
538 ** then this routine returns -1.
540 ** Refer to the delta_create() documentation above for a description
541 ** of the delta file format.
543 static int rbuDeltaApply(
544 const char *zSrc
, /* The source or pattern file */
545 int lenSrc
, /* Length of the source file */
546 const char *zDelta
, /* Delta to apply to the pattern */
547 int lenDelta
, /* Length of the delta */
548 char *zOut
/* Write the output into this preallocated buffer */
551 unsigned int total
= 0;
552 #if RBU_ENABLE_DELTA_CKSUM
553 char *zOrigOut
= zOut
;
556 limit
= rbuDeltaGetInt(&zDelta
, &lenDelta
);
558 /* ERROR: size integer not terminated by "\n" */
561 zDelta
++; lenDelta
--;
562 while( *zDelta
&& lenDelta
>0 ){
563 unsigned int cnt
, ofst
;
564 cnt
= rbuDeltaGetInt(&zDelta
, &lenDelta
);
567 zDelta
++; lenDelta
--;
568 ofst
= rbuDeltaGetInt(&zDelta
, &lenDelta
);
569 if( lenDelta
>0 && zDelta
[0]!=',' ){
570 /* ERROR: copy command not terminated by ',' */
573 zDelta
++; lenDelta
--;
576 /* ERROR: copy exceeds output file size */
579 if( (int)(ofst
+cnt
) > lenSrc
){
580 /* ERROR: copy extends past end of input */
583 memcpy(zOut
, &zSrc
[ofst
], cnt
);
588 zDelta
++; lenDelta
--;
591 /* ERROR: insert command gives an output larger than predicted */
594 if( (int)cnt
>lenDelta
){
595 /* ERROR: insert count exceeds size of delta */
598 memcpy(zOut
, zDelta
, cnt
);
605 zDelta
++; lenDelta
--;
607 #if RBU_ENABLE_DELTA_CKSUM
608 if( cnt
!=rbuDeltaChecksum(zOrigOut
, total
) ){
609 /* ERROR: bad checksum */
614 /* ERROR: generated size does not match predicted size */
620 /* ERROR: unknown delta operator */
625 /* ERROR: unterminated delta */
629 static int rbuDeltaOutputSize(const char *zDelta
, int lenDelta
){
631 size
= rbuDeltaGetInt(&zDelta
, &lenDelta
);
633 /* ERROR: size integer not terminated by "\n" */
640 ** End of code taken from fossil.
641 *************************************************************************/
644 ** Implementation of SQL scalar function rbu_fossil_delta().
646 ** This function applies a fossil delta patch to a blob. Exactly two
647 ** arguments must be passed to this function. The first is the blob to
648 ** patch and the second the patch to apply. If no error occurs, this
649 ** function returns the patched blob.
651 static void rbuFossilDeltaFunc(
652 sqlite3_context
*context
,
667 nOrig
= sqlite3_value_bytes(argv
[0]);
668 aOrig
= (const char*)sqlite3_value_blob(argv
[0]);
669 nDelta
= sqlite3_value_bytes(argv
[1]);
670 aDelta
= (const char*)sqlite3_value_blob(argv
[1]);
672 /* Figure out the size of the output */
673 nOut
= rbuDeltaOutputSize(aDelta
, nDelta
);
675 sqlite3_result_error(context
, "corrupt fossil delta", -1);
679 aOut
= sqlite3_malloc(nOut
+1);
681 sqlite3_result_error_nomem(context
);
683 nOut2
= rbuDeltaApply(aOrig
, nOrig
, aDelta
, nDelta
, aOut
);
685 sqlite3_result_error(context
, "corrupt fossil delta", -1);
687 sqlite3_result_blob(context
, aOut
, nOut
, sqlite3_free
);
694 ** Prepare the SQL statement in buffer zSql against database handle db.
695 ** If successful, set *ppStmt to point to the new statement and return
698 ** Otherwise, if an error does occur, set *ppStmt to NULL and return
699 ** an SQLite error code. Additionally, set output variable *pzErrmsg to
700 ** point to a buffer containing an error message. It is the responsibility
701 ** of the caller to (eventually) free this buffer using sqlite3_free().
703 static int prepareAndCollectError(
705 sqlite3_stmt
**ppStmt
,
709 int rc
= sqlite3_prepare_v2(db
, zSql
, -1, ppStmt
, 0);
711 *pzErrmsg
= sqlite3_mprintf("%s", sqlite3_errmsg(db
));
718 ** Reset the SQL statement passed as the first argument. Return a copy
719 ** of the value returned by sqlite3_reset().
721 ** If an error has occurred, then set *pzErrmsg to point to a buffer
722 ** containing an error message. It is the responsibility of the caller
723 ** to eventually free this buffer using sqlite3_free().
725 static int resetAndCollectError(sqlite3_stmt
*pStmt
, char **pzErrmsg
){
726 int rc
= sqlite3_reset(pStmt
);
728 *pzErrmsg
= sqlite3_mprintf("%s", sqlite3_errmsg(sqlite3_db_handle(pStmt
)));
734 ** Unless it is NULL, argument zSql points to a buffer allocated using
735 ** sqlite3_malloc containing an SQL statement. This function prepares the SQL
736 ** statement against database db and frees the buffer. If statement
737 ** compilation is successful, *ppStmt is set to point to the new statement
738 ** handle and SQLITE_OK is returned.
740 ** Otherwise, if an error occurs, *ppStmt is set to NULL and an error code
741 ** returned. In this case, *pzErrmsg may also be set to point to an error
742 ** message. It is the responsibility of the caller to free this error message
743 ** buffer using sqlite3_free().
745 ** If argument zSql is NULL, this function assumes that an OOM has occurred.
746 ** In this case SQLITE_NOMEM is returned and *ppStmt set to NULL.
748 static int prepareFreeAndCollectError(
750 sqlite3_stmt
**ppStmt
,
755 assert( *pzErrmsg
==0 );
760 rc
= prepareAndCollectError(db
, ppStmt
, pzErrmsg
, zSql
);
767 ** Free the RbuObjIter.azTblCol[] and RbuObjIter.abTblPk[] arrays allocated
768 ** by an earlier call to rbuObjIterCacheTableInfo().
770 static void rbuObjIterFreeCols(RbuObjIter
*pIter
){
772 for(i
=0; i
<pIter
->nTblCol
; i
++){
773 sqlite3_free(pIter
->azTblCol
[i
]);
774 sqlite3_free(pIter
->azTblType
[i
]);
776 sqlite3_free(pIter
->azTblCol
);
778 pIter
->azTblType
= 0;
779 pIter
->aiSrcOrder
= 0;
781 pIter
->abNotNull
= 0;
783 pIter
->eType
= 0; /* Invalid value */
787 ** Finalize all statements and free all allocations that are specific to
788 ** the current object (table/index pair).
790 static void rbuObjIterClearStatements(RbuObjIter
*pIter
){
793 sqlite3_finalize(pIter
->pSelect
);
794 sqlite3_finalize(pIter
->pInsert
);
795 sqlite3_finalize(pIter
->pDelete
);
796 sqlite3_finalize(pIter
->pTmpInsert
);
797 pUp
= pIter
->pRbuUpdate
;
799 RbuUpdateStmt
*pTmp
= pUp
->pNext
;
800 sqlite3_finalize(pUp
->pUpdate
);
808 pIter
->pRbuUpdate
= 0;
809 pIter
->pTmpInsert
= 0;
814 ** Clean up any resources allocated as part of the iterator object passed
815 ** as the only argument.
817 static void rbuObjIterFinalize(RbuObjIter
*pIter
){
818 rbuObjIterClearStatements(pIter
);
819 sqlite3_finalize(pIter
->pTblIter
);
820 sqlite3_finalize(pIter
->pIdxIter
);
821 rbuObjIterFreeCols(pIter
);
822 memset(pIter
, 0, sizeof(RbuObjIter
));
826 ** Advance the iterator to the next position.
828 ** If no error occurs, SQLITE_OK is returned and the iterator is left
829 ** pointing to the next entry. Otherwise, an error code and message is
830 ** left in the RBU handle passed as the first argument. A copy of the
831 ** error code is returned.
833 static int rbuObjIterNext(sqlite3rbu
*p
, RbuObjIter
*pIter
){
837 /* Free any SQLite statements used while processing the previous object */
838 rbuObjIterClearStatements(pIter
);
839 if( pIter
->zIdx
==0 ){
840 rc
= sqlite3_exec(p
->dbMain
,
841 "DROP TRIGGER IF EXISTS temp.rbu_insert_tr;"
842 "DROP TRIGGER IF EXISTS temp.rbu_update1_tr;"
843 "DROP TRIGGER IF EXISTS temp.rbu_update2_tr;"
844 "DROP TRIGGER IF EXISTS temp.rbu_delete_tr;"
850 if( pIter
->bCleanup
){
851 rbuObjIterFreeCols(pIter
);
853 rc
= sqlite3_step(pIter
->pTblIter
);
854 if( rc
!=SQLITE_ROW
){
855 rc
= resetAndCollectError(pIter
->pTblIter
, &p
->zErrmsg
);
858 pIter
->zTbl
= (const char*)sqlite3_column_text(pIter
->pTblIter
, 0);
859 pIter
->zDataTbl
= (const char*)sqlite3_column_text(pIter
->pTblIter
,1);
860 rc
= (pIter
->zDataTbl
&& pIter
->zTbl
) ? SQLITE_OK
: SQLITE_NOMEM
;
863 if( pIter
->zIdx
==0 ){
864 sqlite3_stmt
*pIdx
= pIter
->pIdxIter
;
865 rc
= sqlite3_bind_text(pIdx
, 1, pIter
->zTbl
, -1, SQLITE_STATIC
);
868 rc
= sqlite3_step(pIter
->pIdxIter
);
869 if( rc
!=SQLITE_ROW
){
870 rc
= resetAndCollectError(pIter
->pIdxIter
, &p
->zErrmsg
);
874 pIter
->zIdx
= (const char*)sqlite3_column_text(pIter
->pIdxIter
, 0);
875 pIter
->iTnum
= sqlite3_column_int(pIter
->pIdxIter
, 1);
876 pIter
->bUnique
= sqlite3_column_int(pIter
->pIdxIter
, 2);
877 rc
= pIter
->zIdx
? SQLITE_OK
: SQLITE_NOMEM
;
885 rbuObjIterFinalize(pIter
);
893 ** The implementation of the rbu_target_name() SQL function. This function
894 ** accepts one or two arguments. The first argument is the name of a table -
895 ** the name of a table in the RBU database. The second, if it is present, is 1
896 ** for a view or 0 for a table.
898 ** For a non-vacuum RBU handle, if the table name matches the pattern:
902 ** where <name> is any sequence of 1 or more characters, <name> is returned.
903 ** Otherwise, if the only argument does not match the above pattern, an SQL
907 ** "data0123_t2" -> "t2"
908 ** "dataAB_t3" -> NULL
910 ** For an rbu vacuum handle, a copy of the first argument is returned if
911 ** the second argument is either missing or 0 (not a view).
913 static void rbuTargetNameFunc(
914 sqlite3_context
*pCtx
,
918 sqlite3rbu
*p
= sqlite3_user_data(pCtx
);
920 assert( argc
==1 || argc
==2 );
922 zIn
= (const char*)sqlite3_value_text(argv
[0]);
924 if( rbuIsVacuum(p
) ){
925 if( argc
==1 || 0==sqlite3_value_int(argv
[1]) ){
926 sqlite3_result_text(pCtx
, zIn
, -1, SQLITE_STATIC
);
929 if( strlen(zIn
)>4 && memcmp("data", zIn
, 4)==0 ){
931 for(i
=4; zIn
[i
]>='0' && zIn
[i
]<='9'; i
++);
932 if( zIn
[i
]=='_' && zIn
[i
+1] ){
933 sqlite3_result_text(pCtx
, &zIn
[i
+1], -1, SQLITE_STATIC
);
941 ** Initialize the iterator structure passed as the second argument.
943 ** If no error occurs, SQLITE_OK is returned and the iterator is left
944 ** pointing to the first entry. Otherwise, an error code and message is
945 ** left in the RBU handle passed as the first argument. A copy of the
946 ** error code is returned.
948 static int rbuObjIterFirst(sqlite3rbu
*p
, RbuObjIter
*pIter
){
950 memset(pIter
, 0, sizeof(RbuObjIter
));
952 rc
= prepareFreeAndCollectError(p
->dbRbu
, &pIter
->pTblIter
, &p
->zErrmsg
,
954 "SELECT rbu_target_name(name, type='view') AS target, name "
955 "FROM sqlite_master "
956 "WHERE type IN ('table', 'view') AND target IS NOT NULL "
959 , rbuIsVacuum(p
) ? "AND rootpage!=0 AND rootpage IS NOT NULL" : ""));
962 rc
= prepareAndCollectError(p
->dbMain
, &pIter
->pIdxIter
, &p
->zErrmsg
,
963 "SELECT name, rootpage, sql IS NULL OR substr(8, 6)=='UNIQUE' "
964 " FROM main.sqlite_master "
965 " WHERE type='index' AND tbl_name = ?"
971 return rbuObjIterNext(p
, pIter
);
975 ** This is a wrapper around "sqlite3_mprintf(zFmt, ...)". If an OOM occurs,
976 ** an error code is stored in the RBU handle passed as the first argument.
978 ** If an error has already occurred (p->rc is already set to something other
979 ** than SQLITE_OK), then this function returns NULL without modifying the
980 ** stored error code. In this case it still calls sqlite3_free() on any
981 ** printf() parameters associated with %z conversions.
983 static char *rbuMPrintf(sqlite3rbu
*p
, const char *zFmt
, ...){
987 zSql
= sqlite3_vmprintf(zFmt
, ap
);
988 if( p
->rc
==SQLITE_OK
){
989 if( zSql
==0 ) p
->rc
= SQLITE_NOMEM
;
999 ** Argument zFmt is a sqlite3_mprintf() style format string. The trailing
1000 ** arguments are the usual subsitution values. This function performs
1001 ** the printf() style substitutions and executes the result as an SQL
1002 ** statement on the RBU handles database.
1004 ** If an error occurs, an error code and error message is stored in the
1005 ** RBU handle. If an error has already occurred when this function is
1006 ** called, it is a no-op.
1008 static int rbuMPrintfExec(sqlite3rbu
*p
, sqlite3
*db
, const char *zFmt
, ...){
1012 zSql
= sqlite3_vmprintf(zFmt
, ap
);
1013 if( p
->rc
==SQLITE_OK
){
1015 p
->rc
= SQLITE_NOMEM
;
1017 p
->rc
= sqlite3_exec(db
, zSql
, 0, 0, &p
->zErrmsg
);
1026 ** Attempt to allocate and return a pointer to a zeroed block of nByte
1029 ** If an error (i.e. an OOM condition) occurs, return NULL and leave an
1030 ** error code in the rbu handle passed as the first argument. Or, if an
1031 ** error has already occurred when this function is called, return NULL
1032 ** immediately without attempting the allocation or modifying the stored
1035 static void *rbuMalloc(sqlite3rbu
*p
, int nByte
){
1037 if( p
->rc
==SQLITE_OK
){
1039 pRet
= sqlite3_malloc64(nByte
);
1041 p
->rc
= SQLITE_NOMEM
;
1043 memset(pRet
, 0, nByte
);
1051 ** Allocate and zero the pIter->azTblCol[] and abTblPk[] arrays so that
1052 ** there is room for at least nCol elements. If an OOM occurs, store an
1053 ** error code in the RBU handle passed as the first argument.
1055 static void rbuAllocateIterArrays(sqlite3rbu
*p
, RbuObjIter
*pIter
, int nCol
){
1056 int nByte
= (2*sizeof(char*) + sizeof(int) + 3*sizeof(u8
)) * nCol
;
1059 azNew
= (char**)rbuMalloc(p
, nByte
);
1061 pIter
->azTblCol
= azNew
;
1062 pIter
->azTblType
= &azNew
[nCol
];
1063 pIter
->aiSrcOrder
= (int*)&pIter
->azTblType
[nCol
];
1064 pIter
->abTblPk
= (u8
*)&pIter
->aiSrcOrder
[nCol
];
1065 pIter
->abNotNull
= (u8
*)&pIter
->abTblPk
[nCol
];
1066 pIter
->abIndexed
= (u8
*)&pIter
->abNotNull
[nCol
];
1071 ** The first argument must be a nul-terminated string. This function
1072 ** returns a copy of the string in memory obtained from sqlite3_malloc().
1073 ** It is the responsibility of the caller to eventually free this memory
1074 ** using sqlite3_free().
1076 ** If an OOM condition is encountered when attempting to allocate memory,
1077 ** output variable (*pRc) is set to SQLITE_NOMEM before returning. Otherwise,
1078 ** if the allocation succeeds, (*pRc) is left unchanged.
1080 static char *rbuStrndup(const char *zStr
, int *pRc
){
1083 assert( *pRc
==SQLITE_OK
);
1085 size_t nCopy
= strlen(zStr
) + 1;
1086 zRet
= (char*)sqlite3_malloc64(nCopy
);
1088 memcpy(zRet
, zStr
, nCopy
);
1090 *pRc
= SQLITE_NOMEM
;
1098 ** Finalize the statement passed as the second argument.
1100 ** If the sqlite3_finalize() call indicates that an error occurs, and the
1101 ** rbu handle error code is not already set, set the error code and error
1102 ** message accordingly.
1104 static void rbuFinalize(sqlite3rbu
*p
, sqlite3_stmt
*pStmt
){
1105 sqlite3
*db
= sqlite3_db_handle(pStmt
);
1106 int rc
= sqlite3_finalize(pStmt
);
1107 if( p
->rc
==SQLITE_OK
&& rc
!=SQLITE_OK
){
1109 p
->zErrmsg
= sqlite3_mprintf("%s", sqlite3_errmsg(db
));
1113 /* Determine the type of a table.
1115 ** peType is of type (int*), a pointer to an output parameter of type
1116 ** (int). This call sets the output parameter as follows, depending
1117 ** on the type of the table specified by parameters dbName and zTbl.
1119 ** RBU_PK_NOTABLE: No such table.
1120 ** RBU_PK_NONE: Table has an implicit rowid.
1121 ** RBU_PK_IPK: Table has an explicit IPK column.
1122 ** RBU_PK_EXTERNAL: Table has an external PK index.
1123 ** RBU_PK_WITHOUT_ROWID: Table is WITHOUT ROWID.
1124 ** RBU_PK_VTAB: Table is a virtual table.
1126 ** Argument *piPk is also of type (int*), and also points to an output
1127 ** parameter. Unless the table has an external primary key index
1128 ** (i.e. unless *peType is set to 3), then *piPk is set to zero. Or,
1129 ** if the table does have an external primary key index, then *piPk
1130 ** is set to the root page number of the primary key index before
1135 ** if( no entry exists in sqlite_master ){
1136 ** return RBU_PK_NOTABLE
1137 ** }else if( sql for the entry starts with "CREATE VIRTUAL" ){
1138 ** return RBU_PK_VTAB
1139 ** }else if( "PRAGMA index_list()" for the table contains a "pk" index ){
1140 ** if( the index that is the pk exists in sqlite_master ){
1141 ** *piPK = rootpage of that index.
1142 ** return RBU_PK_EXTERNAL
1144 ** return RBU_PK_WITHOUT_ROWID
1146 ** }else if( "PRAGMA table_info()" lists one or more "pk" columns ){
1147 ** return RBU_PK_IPK
1149 ** return RBU_PK_NONE
1152 static void rbuTableType(
1160 ** 0) SELECT count(*) FROM sqlite_master where name=%Q AND IsVirtual(%Q)
1161 ** 1) PRAGMA index_list = ?
1162 ** 2) SELECT count(*) FROM sqlite_master where name=%Q
1163 ** 3) PRAGMA table_info = ?
1165 sqlite3_stmt
*aStmt
[4] = {0, 0, 0, 0};
1167 *peType
= RBU_PK_NOTABLE
;
1170 assert( p
->rc
==SQLITE_OK
);
1171 p
->rc
= prepareFreeAndCollectError(p
->dbMain
, &aStmt
[0], &p
->zErrmsg
,
1173 "SELECT (sql LIKE 'create virtual%%'), rootpage"
1174 " FROM sqlite_master"
1175 " WHERE name=%Q", zTab
1177 if( p
->rc
!=SQLITE_OK
|| sqlite3_step(aStmt
[0])!=SQLITE_ROW
){
1178 /* Either an error, or no such table. */
1179 goto rbuTableType_end
;
1181 if( sqlite3_column_int(aStmt
[0], 0) ){
1182 *peType
= RBU_PK_VTAB
; /* virtual table */
1183 goto rbuTableType_end
;
1185 *piTnum
= sqlite3_column_int(aStmt
[0], 1);
1187 p
->rc
= prepareFreeAndCollectError(p
->dbMain
, &aStmt
[1], &p
->zErrmsg
,
1188 sqlite3_mprintf("PRAGMA index_list=%Q",zTab
)
1190 if( p
->rc
) goto rbuTableType_end
;
1191 while( sqlite3_step(aStmt
[1])==SQLITE_ROW
){
1192 const u8
*zOrig
= sqlite3_column_text(aStmt
[1], 3);
1193 const u8
*zIdx
= sqlite3_column_text(aStmt
[1], 1);
1194 if( zOrig
&& zIdx
&& zOrig
[0]=='p' ){
1195 p
->rc
= prepareFreeAndCollectError(p
->dbMain
, &aStmt
[2], &p
->zErrmsg
,
1197 "SELECT rootpage FROM sqlite_master WHERE name = %Q", zIdx
1199 if( p
->rc
==SQLITE_OK
){
1200 if( sqlite3_step(aStmt
[2])==SQLITE_ROW
){
1201 *piPk
= sqlite3_column_int(aStmt
[2], 0);
1202 *peType
= RBU_PK_EXTERNAL
;
1204 *peType
= RBU_PK_WITHOUT_ROWID
;
1207 goto rbuTableType_end
;
1211 p
->rc
= prepareFreeAndCollectError(p
->dbMain
, &aStmt
[3], &p
->zErrmsg
,
1212 sqlite3_mprintf("PRAGMA table_info=%Q",zTab
)
1214 if( p
->rc
==SQLITE_OK
){
1215 while( sqlite3_step(aStmt
[3])==SQLITE_ROW
){
1216 if( sqlite3_column_int(aStmt
[3],5)>0 ){
1217 *peType
= RBU_PK_IPK
; /* explicit IPK column */
1218 goto rbuTableType_end
;
1221 *peType
= RBU_PK_NONE
;
1226 for(i
=0; i
<sizeof(aStmt
)/sizeof(aStmt
[0]); i
++){
1227 rbuFinalize(p
, aStmt
[i
]);
1233 ** This is a helper function for rbuObjIterCacheTableInfo(). It populates
1234 ** the pIter->abIndexed[] array.
1236 static void rbuObjIterCacheIndexedCols(sqlite3rbu
*p
, RbuObjIter
*pIter
){
1237 sqlite3_stmt
*pList
= 0;
1240 if( p
->rc
==SQLITE_OK
){
1241 memcpy(pIter
->abIndexed
, pIter
->abTblPk
, sizeof(u8
)*pIter
->nTblCol
);
1242 p
->rc
= prepareFreeAndCollectError(p
->dbMain
, &pList
, &p
->zErrmsg
,
1243 sqlite3_mprintf("PRAGMA main.index_list = %Q", pIter
->zTbl
)
1248 while( p
->rc
==SQLITE_OK
&& SQLITE_ROW
==sqlite3_step(pList
) ){
1249 const char *zIdx
= (const char*)sqlite3_column_text(pList
, 1);
1250 sqlite3_stmt
*pXInfo
= 0;
1251 if( zIdx
==0 ) break;
1252 p
->rc
= prepareFreeAndCollectError(p
->dbMain
, &pXInfo
, &p
->zErrmsg
,
1253 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx
)
1255 while( p
->rc
==SQLITE_OK
&& SQLITE_ROW
==sqlite3_step(pXInfo
) ){
1256 int iCid
= sqlite3_column_int(pXInfo
, 1);
1257 if( iCid
>=0 ) pIter
->abIndexed
[iCid
] = 1;
1259 rbuFinalize(p
, pXInfo
);
1264 if( pIter
->eType
==RBU_PK_WITHOUT_ROWID
){
1265 /* "PRAGMA index_list" includes the main PK b-tree */
1269 rbuFinalize(p
, pList
);
1270 if( bIndex
==0 ) pIter
->abIndexed
= 0;
1275 ** If they are not already populated, populate the pIter->azTblCol[],
1276 ** pIter->abTblPk[], pIter->nTblCol and pIter->bRowid variables according to
1277 ** the table (not index) that the iterator currently points to.
1279 ** Return SQLITE_OK if successful, or an SQLite error code otherwise. If
1280 ** an error does occur, an error code and error message are also left in
1283 static int rbuObjIterCacheTableInfo(sqlite3rbu
*p
, RbuObjIter
*pIter
){
1284 if( pIter
->azTblCol
==0 ){
1285 sqlite3_stmt
*pStmt
= 0;
1287 int i
; /* for() loop iterator variable */
1288 int bRbuRowid
= 0; /* If input table has column "rbu_rowid" */
1292 /* Figure out the type of table this step will deal with. */
1293 assert( pIter
->eType
==0 );
1294 rbuTableType(p
, pIter
->zTbl
, &pIter
->eType
, &iTnum
, &pIter
->iPkTnum
);
1295 if( p
->rc
==SQLITE_OK
&& pIter
->eType
==RBU_PK_NOTABLE
){
1296 p
->rc
= SQLITE_ERROR
;
1297 p
->zErrmsg
= sqlite3_mprintf("no such table: %s", pIter
->zTbl
);
1299 if( p
->rc
) return p
->rc
;
1300 if( pIter
->zIdx
==0 ) pIter
->iTnum
= iTnum
;
1302 assert( pIter
->eType
==RBU_PK_NONE
|| pIter
->eType
==RBU_PK_IPK
1303 || pIter
->eType
==RBU_PK_EXTERNAL
|| pIter
->eType
==RBU_PK_WITHOUT_ROWID
1304 || pIter
->eType
==RBU_PK_VTAB
1307 /* Populate the azTblCol[] and nTblCol variables based on the columns
1308 ** of the input table. Ignore any input table columns that begin with
1310 p
->rc
= prepareFreeAndCollectError(p
->dbRbu
, &pStmt
, &p
->zErrmsg
,
1311 sqlite3_mprintf("SELECT * FROM '%q'", pIter
->zDataTbl
)
1313 if( p
->rc
==SQLITE_OK
){
1314 nCol
= sqlite3_column_count(pStmt
);
1315 rbuAllocateIterArrays(p
, pIter
, nCol
);
1317 for(i
=0; p
->rc
==SQLITE_OK
&& i
<nCol
; i
++){
1318 const char *zName
= (const char*)sqlite3_column_name(pStmt
, i
);
1319 if( sqlite3_strnicmp("rbu_", zName
, 4) ){
1320 char *zCopy
= rbuStrndup(zName
, &p
->rc
);
1321 pIter
->aiSrcOrder
[pIter
->nTblCol
] = pIter
->nTblCol
;
1322 pIter
->azTblCol
[pIter
->nTblCol
++] = zCopy
;
1324 else if( 0==sqlite3_stricmp("rbu_rowid", zName
) ){
1328 sqlite3_finalize(pStmt
);
1331 if( p
->rc
==SQLITE_OK
1332 && rbuIsVacuum(p
)==0
1333 && bRbuRowid
!=(pIter
->eType
==RBU_PK_VTAB
|| pIter
->eType
==RBU_PK_NONE
)
1335 p
->rc
= SQLITE_ERROR
;
1336 p
->zErrmsg
= sqlite3_mprintf(
1337 "table %q %s rbu_rowid column", pIter
->zDataTbl
,
1338 (bRbuRowid
? "may not have" : "requires")
1342 /* Check that all non-HIDDEN columns in the destination table are also
1343 ** present in the input table. Populate the abTblPk[], azTblType[] and
1344 ** aiTblOrder[] arrays at the same time. */
1345 if( p
->rc
==SQLITE_OK
){
1346 p
->rc
= prepareFreeAndCollectError(p
->dbMain
, &pStmt
, &p
->zErrmsg
,
1347 sqlite3_mprintf("PRAGMA table_info(%Q)", pIter
->zTbl
)
1350 while( p
->rc
==SQLITE_OK
&& SQLITE_ROW
==sqlite3_step(pStmt
) ){
1351 const char *zName
= (const char*)sqlite3_column_text(pStmt
, 1);
1352 if( zName
==0 ) break; /* An OOM - finalize() below returns S_NOMEM */
1353 for(i
=iOrder
; i
<pIter
->nTblCol
; i
++){
1354 if( 0==strcmp(zName
, pIter
->azTblCol
[i
]) ) break;
1356 if( i
==pIter
->nTblCol
){
1357 p
->rc
= SQLITE_ERROR
;
1358 p
->zErrmsg
= sqlite3_mprintf("column missing from %q: %s",
1359 pIter
->zDataTbl
, zName
1362 int iPk
= sqlite3_column_int(pStmt
, 5);
1363 int bNotNull
= sqlite3_column_int(pStmt
, 3);
1364 const char *zType
= (const char*)sqlite3_column_text(pStmt
, 2);
1367 SWAP(int, pIter
->aiSrcOrder
[i
], pIter
->aiSrcOrder
[iOrder
]);
1368 SWAP(char*, pIter
->azTblCol
[i
], pIter
->azTblCol
[iOrder
]);
1371 pIter
->azTblType
[iOrder
] = rbuStrndup(zType
, &p
->rc
);
1372 pIter
->abTblPk
[iOrder
] = (iPk
!=0);
1373 pIter
->abNotNull
[iOrder
] = (u8
)bNotNull
|| (iPk
!=0);
1378 rbuFinalize(p
, pStmt
);
1379 rbuObjIterCacheIndexedCols(p
, pIter
);
1380 assert( pIter
->eType
!=RBU_PK_VTAB
|| pIter
->abIndexed
==0 );
1381 assert( pIter
->eType
!=RBU_PK_VTAB
|| pIter
->nIndex
==0 );
1388 ** This function constructs and returns a pointer to a nul-terminated
1389 ** string containing some SQL clause or list based on one or more of the
1390 ** column names currently stored in the pIter->azTblCol[] array.
1392 static char *rbuObjIterGetCollist(
1393 sqlite3rbu
*p
, /* RBU object */
1394 RbuObjIter
*pIter
/* Object iterator for column names */
1397 const char *zSep
= "";
1399 for(i
=0; i
<pIter
->nTblCol
; i
++){
1400 const char *z
= pIter
->azTblCol
[i
];
1401 zList
= rbuMPrintf(p
, "%z%s\"%w\"", zList
, zSep
, z
);
1408 ** This function is used to create a SELECT list (the list of SQL
1409 ** expressions that follows a SELECT keyword) for a SELECT statement
1410 ** used to read from an data_xxx or rbu_tmp_xxx table while updating the
1411 ** index object currently indicated by the iterator object passed as the
1412 ** second argument. A "PRAGMA index_xinfo = <idxname>" statement is used
1413 ** to obtain the required information.
1415 ** If the index is of the following form:
1417 ** CREATE INDEX i1 ON t1(c, b COLLATE nocase);
1419 ** and "t1" is a table with an explicit INTEGER PRIMARY KEY column
1420 ** "ipk", the returned string is:
1422 ** "`c` COLLATE 'BINARY', `b` COLLATE 'NOCASE', `ipk` COLLATE 'BINARY'"
1424 ** As well as the returned string, three other malloc'd strings are
1425 ** returned via output parameters. As follows:
1427 ** pzImposterCols: ...
1428 ** pzImposterPk: ...
1431 static char *rbuObjIterGetIndexCols(
1432 sqlite3rbu
*p
, /* RBU object */
1433 RbuObjIter
*pIter
, /* Object iterator for column names */
1434 char **pzImposterCols
, /* OUT: Columns for imposter table */
1435 char **pzImposterPk
, /* OUT: Imposter PK clause */
1436 char **pzWhere
, /* OUT: WHERE clause */
1437 int *pnBind
/* OUT: Trbul number of columns */
1439 int rc
= p
->rc
; /* Error code */
1440 int rc2
; /* sqlite3_finalize() return code */
1441 char *zRet
= 0; /* String to return */
1442 char *zImpCols
= 0; /* String to return via *pzImposterCols */
1443 char *zImpPK
= 0; /* String to return via *pzImposterPK */
1444 char *zWhere
= 0; /* String to return via *pzWhere */
1445 int nBind
= 0; /* Value to return via *pnBind */
1446 const char *zCom
= ""; /* Set to ", " later on */
1447 const char *zAnd
= ""; /* Set to " AND " later on */
1448 sqlite3_stmt
*pXInfo
= 0; /* PRAGMA index_xinfo = ? */
1450 if( rc
==SQLITE_OK
){
1451 assert( p
->zErrmsg
==0 );
1452 rc
= prepareFreeAndCollectError(p
->dbMain
, &pXInfo
, &p
->zErrmsg
,
1453 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", pIter
->zIdx
)
1457 while( rc
==SQLITE_OK
&& SQLITE_ROW
==sqlite3_step(pXInfo
) ){
1458 int iCid
= sqlite3_column_int(pXInfo
, 1);
1459 int bDesc
= sqlite3_column_int(pXInfo
, 3);
1460 const char *zCollate
= (const char*)sqlite3_column_text(pXInfo
, 4);
1465 /* An integer primary key. If the table has an explicit IPK, use
1466 ** its name. Otherwise, use "rbu_rowid". */
1467 if( pIter
->eType
==RBU_PK_IPK
){
1469 for(i
=0; pIter
->abTblPk
[i
]==0; i
++);
1470 assert( i
<pIter
->nTblCol
);
1471 zCol
= pIter
->azTblCol
[i
];
1472 }else if( rbuIsVacuum(p
) ){
1479 zCol
= pIter
->azTblCol
[iCid
];
1480 zType
= pIter
->azTblType
[iCid
];
1483 zRet
= sqlite3_mprintf("%z%s\"%w\" COLLATE %Q", zRet
, zCom
, zCol
, zCollate
);
1484 if( pIter
->bUnique
==0 || sqlite3_column_int(pXInfo
, 5) ){
1485 const char *zOrder
= (bDesc
? " DESC" : "");
1486 zImpPK
= sqlite3_mprintf("%z%s\"rbu_imp_%d%w\"%s",
1487 zImpPK
, zCom
, nBind
, zCol
, zOrder
1490 zImpCols
= sqlite3_mprintf("%z%s\"rbu_imp_%d%w\" %s COLLATE %Q",
1491 zImpCols
, zCom
, nBind
, zCol
, zType
, zCollate
1493 zWhere
= sqlite3_mprintf(
1494 "%z%s\"rbu_imp_%d%w\" IS ?", zWhere
, zAnd
, nBind
, zCol
1496 if( zRet
==0 || zImpPK
==0 || zImpCols
==0 || zWhere
==0 ) rc
= SQLITE_NOMEM
;
1502 rc2
= sqlite3_finalize(pXInfo
);
1503 if( rc
==SQLITE_OK
) rc
= rc2
;
1505 if( rc
!=SQLITE_OK
){
1507 sqlite3_free(zImpCols
);
1508 sqlite3_free(zImpPK
);
1509 sqlite3_free(zWhere
);
1517 *pzImposterCols
= zImpCols
;
1518 *pzImposterPk
= zImpPK
;
1525 ** Assuming the current table columns are "a", "b" and "c", and the zObj
1526 ** paramter is passed "old", return a string of the form:
1528 ** "old.a, old.b, old.b"
1530 ** With the column names escaped.
1532 ** For tables with implicit rowids - RBU_PK_EXTERNAL and RBU_PK_NONE, append
1533 ** the text ", old._rowid_" to the returned value.
1535 static char *rbuObjIterGetOldlist(
1541 if( p
->rc
==SQLITE_OK
&& pIter
->abIndexed
){
1542 const char *zS
= "";
1544 for(i
=0; i
<pIter
->nTblCol
; i
++){
1545 if( pIter
->abIndexed
[i
] ){
1546 const char *zCol
= pIter
->azTblCol
[i
];
1547 zList
= sqlite3_mprintf("%z%s%s.\"%w\"", zList
, zS
, zObj
, zCol
);
1549 zList
= sqlite3_mprintf("%z%sNULL", zList
, zS
);
1553 p
->rc
= SQLITE_NOMEM
;
1558 /* For a table with implicit rowids, append "old._rowid_" to the list. */
1559 if( pIter
->eType
==RBU_PK_EXTERNAL
|| pIter
->eType
==RBU_PK_NONE
){
1560 zList
= rbuMPrintf(p
, "%z, %s._rowid_", zList
, zObj
);
1567 ** Return an expression that can be used in a WHERE clause to match the
1568 ** primary key of the current table. For example, if the table is:
1570 ** CREATE TABLE t1(a, b, c, PRIMARY KEY(b, c));
1572 ** Return the string:
1574 ** "b = ?1 AND c = ?2"
1576 static char *rbuObjIterGetWhere(
1581 if( pIter
->eType
==RBU_PK_VTAB
|| pIter
->eType
==RBU_PK_NONE
){
1582 zList
= rbuMPrintf(p
, "_rowid_ = ?%d", pIter
->nTblCol
+1);
1583 }else if( pIter
->eType
==RBU_PK_EXTERNAL
){
1584 const char *zSep
= "";
1586 for(i
=0; i
<pIter
->nTblCol
; i
++){
1587 if( pIter
->abTblPk
[i
] ){
1588 zList
= rbuMPrintf(p
, "%z%sc%d=?%d", zList
, zSep
, i
, i
+1);
1592 zList
= rbuMPrintf(p
,
1593 "_rowid_ = (SELECT id FROM rbu_imposter2 WHERE %z)", zList
1597 const char *zSep
= "";
1599 for(i
=0; i
<pIter
->nTblCol
; i
++){
1600 if( pIter
->abTblPk
[i
] ){
1601 const char *zCol
= pIter
->azTblCol
[i
];
1602 zList
= rbuMPrintf(p
, "%z%s\"%w\"=?%d", zList
, zSep
, zCol
, i
+1);
1611 ** The SELECT statement iterating through the keys for the current object
1612 ** (p->objiter.pSelect) currently points to a valid row. However, there
1613 ** is something wrong with the rbu_control value in the rbu_control value
1614 ** stored in the (p->nCol+1)'th column. Set the error code and error message
1615 ** of the RBU handle to something reflecting this.
1617 static void rbuBadControlError(sqlite3rbu
*p
){
1618 p
->rc
= SQLITE_ERROR
;
1619 p
->zErrmsg
= sqlite3_mprintf("invalid rbu_control value");
1624 ** Return a nul-terminated string containing the comma separated list of
1625 ** assignments that should be included following the "SET" keyword of
1626 ** an UPDATE statement used to update the table object that the iterator
1627 ** passed as the second argument currently points to if the rbu_control
1628 ** column of the data_xxx table entry is set to zMask.
1630 ** The memory for the returned string is obtained from sqlite3_malloc().
1631 ** It is the responsibility of the caller to eventually free it using
1634 ** If an OOM error is encountered when allocating space for the new
1635 ** string, an error code is left in the rbu handle passed as the first
1636 ** argument and NULL is returned. Or, if an error has already occurred
1637 ** when this function is called, NULL is returned immediately, without
1638 ** attempting the allocation or modifying the stored error code.
1640 static char *rbuObjIterGetSetlist(
1646 if( p
->rc
==SQLITE_OK
){
1649 if( (int)strlen(zMask
)!=pIter
->nTblCol
){
1650 rbuBadControlError(p
);
1652 const char *zSep
= "";
1653 for(i
=0; i
<pIter
->nTblCol
; i
++){
1654 char c
= zMask
[pIter
->aiSrcOrder
[i
]];
1656 zList
= rbuMPrintf(p
, "%z%s\"%w\"=?%d",
1657 zList
, zSep
, pIter
->azTblCol
[i
], i
+1
1662 zList
= rbuMPrintf(p
, "%z%s\"%w\"=rbu_delta(\"%w\", ?%d)",
1663 zList
, zSep
, pIter
->azTblCol
[i
], pIter
->azTblCol
[i
], i
+1
1668 zList
= rbuMPrintf(p
, "%z%s\"%w\"=rbu_fossil_delta(\"%w\", ?%d)",
1669 zList
, zSep
, pIter
->azTblCol
[i
], pIter
->azTblCol
[i
], i
+1
1680 ** Return a nul-terminated string consisting of nByte comma separated
1681 ** "?" expressions. For example, if nByte is 3, return a pointer to
1682 ** a buffer containing the string "?,?,?".
1684 ** The memory for the returned string is obtained from sqlite3_malloc().
1685 ** It is the responsibility of the caller to eventually free it using
1688 ** If an OOM error is encountered when allocating space for the new
1689 ** string, an error code is left in the rbu handle passed as the first
1690 ** argument and NULL is returned. Or, if an error has already occurred
1691 ** when this function is called, NULL is returned immediately, without
1692 ** attempting the allocation or modifying the stored error code.
1694 static char *rbuObjIterGetBindlist(sqlite3rbu
*p
, int nBind
){
1696 int nByte
= nBind
*2 + 1;
1698 zRet
= (char*)rbuMalloc(p
, nByte
);
1701 for(i
=0; i
<nBind
; i
++){
1703 zRet
[i
*2+1] = (i
+1==nBind
) ? '\0' : ',';
1710 ** The iterator currently points to a table (not index) of type
1711 ** RBU_PK_WITHOUT_ROWID. This function creates the PRIMARY KEY
1712 ** declaration for the corresponding imposter table. For example,
1713 ** if the iterator points to a table created as:
1715 ** CREATE TABLE t1(a, b, c, PRIMARY KEY(b, a DESC)) WITHOUT ROWID
1717 ** this function returns:
1719 ** PRIMARY KEY("b", "a" DESC)
1721 static char *rbuWithoutRowidPK(sqlite3rbu
*p
, RbuObjIter
*pIter
){
1723 assert( pIter
->zIdx
==0 );
1724 if( p
->rc
==SQLITE_OK
){
1725 const char *zSep
= "PRIMARY KEY(";
1726 sqlite3_stmt
*pXList
= 0; /* PRAGMA index_list = (pIter->zTbl) */
1727 sqlite3_stmt
*pXInfo
= 0; /* PRAGMA index_xinfo = <pk-index> */
1729 p
->rc
= prepareFreeAndCollectError(p
->dbMain
, &pXList
, &p
->zErrmsg
,
1730 sqlite3_mprintf("PRAGMA main.index_list = %Q", pIter
->zTbl
)
1732 while( p
->rc
==SQLITE_OK
&& SQLITE_ROW
==sqlite3_step(pXList
) ){
1733 const char *zOrig
= (const char*)sqlite3_column_text(pXList
,3);
1734 if( zOrig
&& strcmp(zOrig
, "pk")==0 ){
1735 const char *zIdx
= (const char*)sqlite3_column_text(pXList
,1);
1737 p
->rc
= prepareFreeAndCollectError(p
->dbMain
, &pXInfo
, &p
->zErrmsg
,
1738 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx
)
1744 rbuFinalize(p
, pXList
);
1746 while( p
->rc
==SQLITE_OK
&& SQLITE_ROW
==sqlite3_step(pXInfo
) ){
1747 if( sqlite3_column_int(pXInfo
, 5) ){
1748 /* int iCid = sqlite3_column_int(pXInfo, 0); */
1749 const char *zCol
= (const char*)sqlite3_column_text(pXInfo
, 2);
1750 const char *zDesc
= sqlite3_column_int(pXInfo
, 3) ? " DESC" : "";
1751 z
= rbuMPrintf(p
, "%z%s\"%w\"%s", z
, zSep
, zCol
, zDesc
);
1755 z
= rbuMPrintf(p
, "%z)", z
);
1756 rbuFinalize(p
, pXInfo
);
1762 ** This function creates the second imposter table used when writing to
1763 ** a table b-tree where the table has an external primary key. If the
1764 ** iterator passed as the second argument does not currently point to
1765 ** a table (not index) with an external primary key, this function is a
1768 ** Assuming the iterator does point to a table with an external PK, this
1769 ** function creates a WITHOUT ROWID imposter table named "rbu_imposter2"
1770 ** used to access that PK index. For example, if the target table is
1771 ** declared as follows:
1773 ** CREATE TABLE t1(a, b TEXT, c REAL, PRIMARY KEY(b, c));
1775 ** then the imposter table schema is:
1777 ** CREATE TABLE rbu_imposter2(c1 TEXT, c2 REAL, id INTEGER) WITHOUT ROWID;
1780 static void rbuCreateImposterTable2(sqlite3rbu
*p
, RbuObjIter
*pIter
){
1781 if( p
->rc
==SQLITE_OK
&& pIter
->eType
==RBU_PK_EXTERNAL
){
1782 int tnum
= pIter
->iPkTnum
; /* Root page of PK index */
1783 sqlite3_stmt
*pQuery
= 0; /* SELECT name ... WHERE rootpage = $tnum */
1784 const char *zIdx
= 0; /* Name of PK index */
1785 sqlite3_stmt
*pXInfo
= 0; /* PRAGMA main.index_xinfo = $zIdx */
1786 const char *zComma
= "";
1787 char *zCols
= 0; /* Used to build up list of table cols */
1788 char *zPk
= 0; /* Used to build up table PK declaration */
1790 /* Figure out the name of the primary key index for the current table.
1791 ** This is needed for the argument to "PRAGMA index_xinfo". Set
1792 ** zIdx to point to a nul-terminated string containing this name. */
1793 p
->rc
= prepareAndCollectError(p
->dbMain
, &pQuery
, &p
->zErrmsg
,
1794 "SELECT name FROM sqlite_master WHERE rootpage = ?"
1796 if( p
->rc
==SQLITE_OK
){
1797 sqlite3_bind_int(pQuery
, 1, tnum
);
1798 if( SQLITE_ROW
==sqlite3_step(pQuery
) ){
1799 zIdx
= (const char*)sqlite3_column_text(pQuery
, 0);
1803 p
->rc
= prepareFreeAndCollectError(p
->dbMain
, &pXInfo
, &p
->zErrmsg
,
1804 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx
)
1807 rbuFinalize(p
, pQuery
);
1809 while( p
->rc
==SQLITE_OK
&& SQLITE_ROW
==sqlite3_step(pXInfo
) ){
1810 int bKey
= sqlite3_column_int(pXInfo
, 5);
1812 int iCid
= sqlite3_column_int(pXInfo
, 1);
1813 int bDesc
= sqlite3_column_int(pXInfo
, 3);
1814 const char *zCollate
= (const char*)sqlite3_column_text(pXInfo
, 4);
1815 zCols
= rbuMPrintf(p
, "%z%sc%d %s COLLATE %Q", zCols
, zComma
,
1816 iCid
, pIter
->azTblType
[iCid
], zCollate
1818 zPk
= rbuMPrintf(p
, "%z%sc%d%s", zPk
, zComma
, iCid
, bDesc
?" DESC":"");
1822 zCols
= rbuMPrintf(p
, "%z, id INTEGER", zCols
);
1823 rbuFinalize(p
, pXInfo
);
1825 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER
, p
->dbMain
, "main", 1, tnum
);
1826 rbuMPrintfExec(p
, p
->dbMain
,
1827 "CREATE TABLE rbu_imposter2(%z, PRIMARY KEY(%z)) WITHOUT ROWID",
1830 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER
, p
->dbMain
, "main", 0, 0);
1835 ** If an error has already occurred when this function is called, it
1836 ** immediately returns zero (without doing any work). Or, if an error
1837 ** occurs during the execution of this function, it sets the error code
1838 ** in the sqlite3rbu object indicated by the first argument and returns
1841 ** The iterator passed as the second argument is guaranteed to point to
1842 ** a table (not an index) when this function is called. This function
1843 ** attempts to create any imposter table required to write to the main
1844 ** table b-tree of the table before returning. Non-zero is returned if
1845 ** an imposter table are created, or zero otherwise.
1847 ** An imposter table is required in all cases except RBU_PK_VTAB. Only
1848 ** virtual tables are written to directly. The imposter table has the
1849 ** same schema as the actual target table (less any UNIQUE constraints).
1850 ** More precisely, the "same schema" means the same columns, types,
1851 ** collation sequences. For tables that do not have an external PRIMARY
1852 ** KEY, it also means the same PRIMARY KEY declaration.
1854 static void rbuCreateImposterTable(sqlite3rbu
*p
, RbuObjIter
*pIter
){
1855 if( p
->rc
==SQLITE_OK
&& pIter
->eType
!=RBU_PK_VTAB
){
1856 int tnum
= pIter
->iTnum
;
1857 const char *zComma
= "";
1860 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER
, p
->dbMain
, "main", 0, 1);
1862 for(iCol
=0; p
->rc
==SQLITE_OK
&& iCol
<pIter
->nTblCol
; iCol
++){
1863 const char *zPk
= "";
1864 const char *zCol
= pIter
->azTblCol
[iCol
];
1865 const char *zColl
= 0;
1867 p
->rc
= sqlite3_table_column_metadata(
1868 p
->dbMain
, "main", pIter
->zTbl
, zCol
, 0, &zColl
, 0, 0, 0
1871 if( pIter
->eType
==RBU_PK_IPK
&& pIter
->abTblPk
[iCol
] ){
1872 /* If the target table column is an "INTEGER PRIMARY KEY", add
1873 ** "PRIMARY KEY" to the imposter table column declaration. */
1874 zPk
= "PRIMARY KEY ";
1876 zSql
= rbuMPrintf(p
, "%z%s\"%w\" %s %sCOLLATE %Q%s",
1877 zSql
, zComma
, zCol
, pIter
->azTblType
[iCol
], zPk
, zColl
,
1878 (pIter
->abNotNull
[iCol
] ? " NOT NULL" : "")
1883 if( pIter
->eType
==RBU_PK_WITHOUT_ROWID
){
1884 char *zPk
= rbuWithoutRowidPK(p
, pIter
);
1886 zSql
= rbuMPrintf(p
, "%z, %z", zSql
, zPk
);
1890 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER
, p
->dbMain
, "main", 1, tnum
);
1891 rbuMPrintfExec(p
, p
->dbMain
, "CREATE TABLE \"rbu_imp_%w\"(%z)%s",
1893 (pIter
->eType
==RBU_PK_WITHOUT_ROWID
? " WITHOUT ROWID" : "")
1895 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER
, p
->dbMain
, "main", 0, 0);
1900 ** Prepare a statement used to insert rows into the "rbu_tmp_xxx" table.
1901 ** Specifically a statement of the form:
1903 ** INSERT INTO rbu_tmp_xxx VALUES(?, ?, ? ...);
1905 ** The number of bound variables is equal to the number of columns in
1906 ** the target table, plus one (for the rbu_control column), plus one more
1907 ** (for the rbu_rowid column) if the target table is an implicit IPK or
1910 static void rbuObjIterPrepareTmpInsert(
1913 const char *zCollist
,
1914 const char *zRbuRowid
1916 int bRbuRowid
= (pIter
->eType
==RBU_PK_EXTERNAL
|| pIter
->eType
==RBU_PK_NONE
);
1917 char *zBind
= rbuObjIterGetBindlist(p
, pIter
->nTblCol
+ 1 + bRbuRowid
);
1919 assert( pIter
->pTmpInsert
==0 );
1920 p
->rc
= prepareFreeAndCollectError(
1921 p
->dbRbu
, &pIter
->pTmpInsert
, &p
->zErrmsg
, sqlite3_mprintf(
1922 "INSERT INTO %s.'rbu_tmp_%q'(rbu_control,%s%s) VALUES(%z)",
1923 p
->zStateDb
, pIter
->zDataTbl
, zCollist
, zRbuRowid
, zBind
1928 static void rbuTmpInsertFunc(
1929 sqlite3_context
*pCtx
,
1931 sqlite3_value
**apVal
1933 sqlite3rbu
*p
= sqlite3_user_data(pCtx
);
1937 assert( sqlite3_value_int(apVal
[0])!=0
1938 || p
->objiter
.eType
==RBU_PK_EXTERNAL
1939 || p
->objiter
.eType
==RBU_PK_NONE
1941 if( sqlite3_value_int(apVal
[0])!=0 ){
1942 p
->nPhaseOneStep
+= p
->objiter
.nIndex
;
1945 for(i
=0; rc
==SQLITE_OK
&& i
<nVal
; i
++){
1946 rc
= sqlite3_bind_value(p
->objiter
.pTmpInsert
, i
+1, apVal
[i
]);
1948 if( rc
==SQLITE_OK
){
1949 sqlite3_step(p
->objiter
.pTmpInsert
);
1950 rc
= sqlite3_reset(p
->objiter
.pTmpInsert
);
1953 if( rc
!=SQLITE_OK
){
1954 sqlite3_result_error_code(pCtx
, rc
);
1959 ** Ensure that the SQLite statement handles required to update the
1960 ** target database object currently indicated by the iterator passed
1961 ** as the second argument are available.
1963 static int rbuObjIterPrepareAll(
1966 int nOffset
/* Add "LIMIT -1 OFFSET $nOffset" to SELECT */
1968 assert( pIter
->bCleanup
==0 );
1969 if( pIter
->pSelect
==0 && rbuObjIterCacheTableInfo(p
, pIter
)==SQLITE_OK
){
1970 const int tnum
= pIter
->iTnum
;
1971 char *zCollist
= 0; /* List of indexed columns */
1972 char **pz
= &p
->zErrmsg
;
1973 const char *zIdx
= pIter
->zIdx
;
1977 zLimit
= sqlite3_mprintf(" LIMIT -1 OFFSET %d", nOffset
);
1978 if( !zLimit
) p
->rc
= SQLITE_NOMEM
;
1982 const char *zTbl
= pIter
->zTbl
;
1983 char *zImposterCols
= 0; /* Columns for imposter table */
1984 char *zImposterPK
= 0; /* Primary key declaration for imposter */
1985 char *zWhere
= 0; /* WHERE clause on PK columns */
1989 assert( pIter
->eType
!=RBU_PK_VTAB
);
1990 zCollist
= rbuObjIterGetIndexCols(
1991 p
, pIter
, &zImposterCols
, &zImposterPK
, &zWhere
, &nBind
1993 zBind
= rbuObjIterGetBindlist(p
, nBind
);
1995 /* Create the imposter table used to write to this index. */
1996 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER
, p
->dbMain
, "main", 0, 1);
1997 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER
, p
->dbMain
, "main", 1,tnum
);
1998 rbuMPrintfExec(p
, p
->dbMain
,
1999 "CREATE TABLE \"rbu_imp_%w\"( %s, PRIMARY KEY( %s ) ) WITHOUT ROWID",
2000 zTbl
, zImposterCols
, zImposterPK
2002 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER
, p
->dbMain
, "main", 0, 0);
2004 /* Create the statement to insert index entries */
2005 pIter
->nCol
= nBind
;
2006 if( p
->rc
==SQLITE_OK
){
2007 p
->rc
= prepareFreeAndCollectError(
2008 p
->dbMain
, &pIter
->pInsert
, &p
->zErrmsg
,
2009 sqlite3_mprintf("INSERT INTO \"rbu_imp_%w\" VALUES(%s)", zTbl
, zBind
)
2013 /* And to delete index entries */
2014 if( rbuIsVacuum(p
)==0 && p
->rc
==SQLITE_OK
){
2015 p
->rc
= prepareFreeAndCollectError(
2016 p
->dbMain
, &pIter
->pDelete
, &p
->zErrmsg
,
2017 sqlite3_mprintf("DELETE FROM \"rbu_imp_%w\" WHERE %s", zTbl
, zWhere
)
2021 /* Create the SELECT statement to read keys in sorted order */
2022 if( p
->rc
==SQLITE_OK
){
2024 if( rbuIsVacuum(p
) ){
2025 zSql
= sqlite3_mprintf(
2026 "SELECT %s, 0 AS rbu_control FROM '%q' ORDER BY %s%s",
2033 if( pIter
->eType
==RBU_PK_EXTERNAL
|| pIter
->eType
==RBU_PK_NONE
){
2034 zSql
= sqlite3_mprintf(
2035 "SELECT %s, rbu_control FROM %s.'rbu_tmp_%q' ORDER BY %s%s",
2036 zCollist
, p
->zStateDb
, pIter
->zDataTbl
,
2040 zSql
= sqlite3_mprintf(
2041 "SELECT %s, rbu_control FROM %s.'rbu_tmp_%q' "
2043 "SELECT %s, rbu_control FROM '%q' "
2044 "WHERE typeof(rbu_control)='integer' AND rbu_control!=1 "
2046 zCollist
, p
->zStateDb
, pIter
->zDataTbl
,
2047 zCollist
, pIter
->zDataTbl
,
2051 p
->rc
= prepareFreeAndCollectError(p
->dbRbu
, &pIter
->pSelect
, pz
, zSql
);
2054 sqlite3_free(zImposterCols
);
2055 sqlite3_free(zImposterPK
);
2056 sqlite3_free(zWhere
);
2057 sqlite3_free(zBind
);
2059 int bRbuRowid
= (pIter
->eType
==RBU_PK_VTAB
)
2060 ||(pIter
->eType
==RBU_PK_NONE
)
2061 ||(pIter
->eType
==RBU_PK_EXTERNAL
&& rbuIsVacuum(p
));
2062 const char *zTbl
= pIter
->zTbl
; /* Table this step applies to */
2063 const char *zWrite
; /* Imposter table name */
2065 char *zBindings
= rbuObjIterGetBindlist(p
, pIter
->nTblCol
+ bRbuRowid
);
2066 char *zWhere
= rbuObjIterGetWhere(p
, pIter
);
2067 char *zOldlist
= rbuObjIterGetOldlist(p
, pIter
, "old");
2068 char *zNewlist
= rbuObjIterGetOldlist(p
, pIter
, "new");
2070 zCollist
= rbuObjIterGetCollist(p
, pIter
);
2071 pIter
->nCol
= pIter
->nTblCol
;
2073 /* Create the imposter table or tables (if required). */
2074 rbuCreateImposterTable(p
, pIter
);
2075 rbuCreateImposterTable2(p
, pIter
);
2076 zWrite
= (pIter
->eType
==RBU_PK_VTAB
? "" : "rbu_imp_");
2078 /* Create the INSERT statement to write to the target PK b-tree */
2079 if( p
->rc
==SQLITE_OK
){
2080 p
->rc
= prepareFreeAndCollectError(p
->dbMain
, &pIter
->pInsert
, pz
,
2082 "INSERT INTO \"%s%w\"(%s%s) VALUES(%s)",
2083 zWrite
, zTbl
, zCollist
, (bRbuRowid
? ", _rowid_" : ""), zBindings
2088 /* Create the DELETE statement to write to the target PK b-tree.
2089 ** Because it only performs INSERT operations, this is not required for
2090 ** an rbu vacuum handle. */
2091 if( rbuIsVacuum(p
)==0 && p
->rc
==SQLITE_OK
){
2092 p
->rc
= prepareFreeAndCollectError(p
->dbMain
, &pIter
->pDelete
, pz
,
2094 "DELETE FROM \"%s%w\" WHERE %s", zWrite
, zTbl
, zWhere
2099 if( rbuIsVacuum(p
)==0 && pIter
->abIndexed
){
2100 const char *zRbuRowid
= "";
2101 if( pIter
->eType
==RBU_PK_EXTERNAL
|| pIter
->eType
==RBU_PK_NONE
){
2102 zRbuRowid
= ", rbu_rowid";
2105 /* Create the rbu_tmp_xxx table and the triggers to populate it. */
2106 rbuMPrintfExec(p
, p
->dbRbu
,
2107 "CREATE TABLE IF NOT EXISTS %s.'rbu_tmp_%q' AS "
2108 "SELECT *%s FROM '%q' WHERE 0;"
2109 , p
->zStateDb
, pIter
->zDataTbl
2110 , (pIter
->eType
==RBU_PK_EXTERNAL
? ", 0 AS rbu_rowid" : "")
2114 rbuMPrintfExec(p
, p
->dbMain
,
2115 "CREATE TEMP TRIGGER rbu_delete_tr BEFORE DELETE ON \"%s%w\" "
2117 " SELECT rbu_tmp_insert(3, %s);"
2120 "CREATE TEMP TRIGGER rbu_update1_tr BEFORE UPDATE ON \"%s%w\" "
2122 " SELECT rbu_tmp_insert(3, %s);"
2125 "CREATE TEMP TRIGGER rbu_update2_tr AFTER UPDATE ON \"%s%w\" "
2127 " SELECT rbu_tmp_insert(4, %s);"
2129 zWrite
, zTbl
, zOldlist
,
2130 zWrite
, zTbl
, zOldlist
,
2131 zWrite
, zTbl
, zNewlist
2134 if( pIter
->eType
==RBU_PK_EXTERNAL
|| pIter
->eType
==RBU_PK_NONE
){
2135 rbuMPrintfExec(p
, p
->dbMain
,
2136 "CREATE TEMP TRIGGER rbu_insert_tr AFTER INSERT ON \"%s%w\" "
2138 " SELECT rbu_tmp_insert(0, %s);"
2140 zWrite
, zTbl
, zNewlist
2144 rbuObjIterPrepareTmpInsert(p
, pIter
, zCollist
, zRbuRowid
);
2147 /* Create the SELECT statement to read keys from data_xxx */
2148 if( p
->rc
==SQLITE_OK
){
2149 const char *zRbuRowid
= "";
2151 zRbuRowid
= rbuIsVacuum(p
) ? ",_rowid_ " : ",rbu_rowid";
2153 p
->rc
= prepareFreeAndCollectError(p
->dbRbu
, &pIter
->pSelect
, pz
,
2155 "SELECT %s,%s rbu_control%s FROM '%q'%s",
2157 (rbuIsVacuum(p
) ? "0 AS " : ""),
2159 pIter
->zDataTbl
, zLimit
2164 sqlite3_free(zWhere
);
2165 sqlite3_free(zOldlist
);
2166 sqlite3_free(zNewlist
);
2167 sqlite3_free(zBindings
);
2169 sqlite3_free(zCollist
);
2170 sqlite3_free(zLimit
);
2177 ** Set output variable *ppStmt to point to an UPDATE statement that may
2178 ** be used to update the imposter table for the main table b-tree of the
2179 ** table object that pIter currently points to, assuming that the
2180 ** rbu_control column of the data_xyz table contains zMask.
2182 ** If the zMask string does not specify any columns to update, then this
2183 ** is not an error. Output variable *ppStmt is set to NULL in this case.
2185 static int rbuGetUpdateStmt(
2186 sqlite3rbu
*p
, /* RBU handle */
2187 RbuObjIter
*pIter
, /* Object iterator */
2188 const char *zMask
, /* rbu_control value ('x.x.') */
2189 sqlite3_stmt
**ppStmt
/* OUT: UPDATE statement handle */
2192 RbuUpdateStmt
*pUp
= 0;
2195 /* In case an error occurs */
2198 /* Search for an existing statement. If one is found, shift it to the front
2199 ** of the LRU queue and return immediately. Otherwise, leave nUp pointing
2200 ** to the number of statements currently in the cache and pUp to the
2201 ** last object in the list. */
2202 for(pp
=&pIter
->pRbuUpdate
; *pp
; pp
=&((*pp
)->pNext
)){
2204 if( strcmp(pUp
->zMask
, zMask
)==0 ){
2206 pUp
->pNext
= pIter
->pRbuUpdate
;
2207 pIter
->pRbuUpdate
= pUp
;
2208 *ppStmt
= pUp
->pUpdate
;
2213 assert( pUp
==0 || pUp
->pNext
==0 );
2215 if( nUp
>=SQLITE_RBU_UPDATE_CACHESIZE
){
2216 for(pp
=&pIter
->pRbuUpdate
; *pp
!=pUp
; pp
=&((*pp
)->pNext
));
2218 sqlite3_finalize(pUp
->pUpdate
);
2221 pUp
= (RbuUpdateStmt
*)rbuMalloc(p
, sizeof(RbuUpdateStmt
)+pIter
->nTblCol
+1);
2225 char *zWhere
= rbuObjIterGetWhere(p
, pIter
);
2226 char *zSet
= rbuObjIterGetSetlist(p
, pIter
, zMask
);
2229 pUp
->zMask
= (char*)&pUp
[1];
2230 memcpy(pUp
->zMask
, zMask
, pIter
->nTblCol
);
2231 pUp
->pNext
= pIter
->pRbuUpdate
;
2232 pIter
->pRbuUpdate
= pUp
;
2235 const char *zPrefix
= "";
2237 if( pIter
->eType
!=RBU_PK_VTAB
) zPrefix
= "rbu_imp_";
2238 zUpdate
= sqlite3_mprintf("UPDATE \"%s%w\" SET %s WHERE %s",
2239 zPrefix
, pIter
->zTbl
, zSet
, zWhere
2241 p
->rc
= prepareFreeAndCollectError(
2242 p
->dbMain
, &pUp
->pUpdate
, &p
->zErrmsg
, zUpdate
2244 *ppStmt
= pUp
->pUpdate
;
2246 sqlite3_free(zWhere
);
2253 static sqlite3
*rbuOpenDbhandle(
2259 if( p
->rc
==SQLITE_OK
){
2260 const int flags
= SQLITE_OPEN_READWRITE
|SQLITE_OPEN_CREATE
|SQLITE_OPEN_URI
;
2261 p
->rc
= sqlite3_open_v2(zName
, &db
, flags
, bUseVfs
? p
->zVfsName
: 0);
2263 p
->zErrmsg
= sqlite3_mprintf("%s", sqlite3_errmsg(db
));
2272 ** Free an RbuState object allocated by rbuLoadState().
2274 static void rbuFreeState(RbuState
*p
){
2276 sqlite3_free(p
->zTbl
);
2277 sqlite3_free(p
->zDataTbl
);
2278 sqlite3_free(p
->zIdx
);
2284 ** Allocate an RbuState object and load the contents of the rbu_state
2285 ** table into it. Return a pointer to the new object. It is the
2286 ** responsibility of the caller to eventually free the object using
2289 ** If an error occurs, leave an error code and message in the rbu handle
2292 static RbuState
*rbuLoadState(sqlite3rbu
*p
){
2294 sqlite3_stmt
*pStmt
= 0;
2298 pRet
= (RbuState
*)rbuMalloc(p
, sizeof(RbuState
));
2299 if( pRet
==0 ) return 0;
2301 rc
= prepareFreeAndCollectError(p
->dbRbu
, &pStmt
, &p
->zErrmsg
,
2302 sqlite3_mprintf("SELECT k, v FROM %s.rbu_state", p
->zStateDb
)
2304 while( rc
==SQLITE_OK
&& SQLITE_ROW
==sqlite3_step(pStmt
) ){
2305 switch( sqlite3_column_int(pStmt
, 0) ){
2306 case RBU_STATE_STAGE
:
2307 pRet
->eStage
= sqlite3_column_int(pStmt
, 1);
2308 if( pRet
->eStage
!=RBU_STAGE_OAL
2309 && pRet
->eStage
!=RBU_STAGE_MOVE
2310 && pRet
->eStage
!=RBU_STAGE_CKPT
2312 p
->rc
= SQLITE_CORRUPT
;
2317 pRet
->zTbl
= rbuStrndup((char*)sqlite3_column_text(pStmt
, 1), &rc
);
2321 pRet
->zIdx
= rbuStrndup((char*)sqlite3_column_text(pStmt
, 1), &rc
);
2325 pRet
->nRow
= sqlite3_column_int(pStmt
, 1);
2328 case RBU_STATE_PROGRESS
:
2329 pRet
->nProgress
= sqlite3_column_int64(pStmt
, 1);
2332 case RBU_STATE_CKPT
:
2333 pRet
->iWalCksum
= sqlite3_column_int64(pStmt
, 1);
2336 case RBU_STATE_COOKIE
:
2337 pRet
->iCookie
= (u32
)sqlite3_column_int64(pStmt
, 1);
2340 case RBU_STATE_OALSZ
:
2341 pRet
->iOalSz
= (u32
)sqlite3_column_int64(pStmt
, 1);
2344 case RBU_STATE_PHASEONESTEP
:
2345 pRet
->nPhaseOneStep
= sqlite3_column_int64(pStmt
, 1);
2348 case RBU_STATE_DATATBL
:
2349 pRet
->zDataTbl
= rbuStrndup((char*)sqlite3_column_text(pStmt
, 1), &rc
);
2353 rc
= SQLITE_CORRUPT
;
2357 rc2
= sqlite3_finalize(pStmt
);
2358 if( rc
==SQLITE_OK
) rc
= rc2
;
2366 ** Open the database handle and attach the RBU database as "rbu". If an
2367 ** error occurs, leave an error code and message in the RBU handle.
2369 static void rbuOpenDatabase(sqlite3rbu
*p
, int *pbRetry
){
2370 assert( p
->rc
|| (p
->dbMain
==0 && p
->dbRbu
==0) );
2371 assert( p
->rc
|| rbuIsVacuum(p
) || p
->zTarget
!=0 );
2373 /* Open the RBU database */
2374 p
->dbRbu
= rbuOpenDbhandle(p
, p
->zRbu
, 1);
2376 if( p
->rc
==SQLITE_OK
&& rbuIsVacuum(p
) ){
2377 sqlite3_file_control(p
->dbRbu
, "main", SQLITE_FCNTL_RBUCNT
, (void*)p
);
2379 const char *zFile
= sqlite3_db_filename(p
->dbRbu
, "main");
2380 p
->zState
= rbuMPrintf(p
, "file://%s-vacuum?modeof=%s", zFile
, zFile
);
2384 /* If using separate RBU and state databases, attach the state database to
2385 ** the RBU db handle now. */
2387 rbuMPrintfExec(p
, p
->dbRbu
, "ATTACH %Q AS stat", p
->zState
);
2388 memcpy(p
->zStateDb
, "stat", 4);
2390 memcpy(p
->zStateDb
, "main", 4);
2394 if( p
->rc
==SQLITE_OK
&& rbuIsVacuum(p
) ){
2395 p
->rc
= sqlite3_exec(p
->dbRbu
, "BEGIN", 0, 0, 0);
2399 /* If it has not already been created, create the rbu_state table */
2400 rbuMPrintfExec(p
, p
->dbRbu
, RBU_CREATE_STATE
, p
->zStateDb
);
2403 if( rbuIsVacuum(p
) ){
2404 if( p
->rc
==SQLITE_OK
){
2407 sqlite3_stmt
*pCnt
= 0;
2408 p
->rc
= prepareAndCollectError(p
->dbRbu
, &pCnt
, &p
->zErrmsg
,
2409 "SELECT count(*) FROM stat.sqlite_master"
2411 if( p
->rc
==SQLITE_OK
2412 && sqlite3_step(pCnt
)==SQLITE_ROW
2413 && 1==sqlite3_column_int(pCnt
, 0)
2417 rc2
= sqlite3_finalize(pCnt
);
2418 if( p
->rc
==SQLITE_OK
) p
->rc
= rc2
;
2420 if( p
->rc
==SQLITE_OK
&& bOk
==0 ){
2421 p
->rc
= SQLITE_ERROR
;
2422 p
->zErrmsg
= sqlite3_mprintf("invalid state database");
2425 if( p
->rc
==SQLITE_OK
){
2426 p
->rc
= sqlite3_exec(p
->dbRbu
, "COMMIT", 0, 0, 0);
2432 if( p
->rc
==SQLITE_OK
&& rbuIsVacuum(p
) ){
2437 rc
= sqlite3_file_control(p
->dbRbu
, "main", SQLITE_FCNTL_RBUCNT
, (void*)p
);
2438 if( rc
!=SQLITE_NOTFOUND
) p
->rc
= rc
;
2439 if( p
->eStage
>=RBU_STAGE_MOVE
){
2442 RbuState
*pState
= rbuLoadState(p
);
2444 bOpen
= (pState
->eStage
>=RBU_STAGE_MOVE
);
2445 rbuFreeState(pState
);
2448 if( bOpen
) p
->dbMain
= rbuOpenDbhandle(p
, p
->zRbu
, p
->nRbu
<=1);
2452 if( p
->rc
==SQLITE_OK
&& p
->dbMain
==0 ){
2453 if( !rbuIsVacuum(p
) ){
2454 p
->dbMain
= rbuOpenDbhandle(p
, p
->zTarget
, 1);
2455 }else if( p
->pRbuFd
->pWalFd
){
2457 p
->pRbuFd
->bNolock
= 0;
2458 sqlite3_close(p
->dbRbu
);
2459 sqlite3_close(p
->dbMain
);
2465 p
->rc
= SQLITE_ERROR
;
2466 p
->zErrmsg
= sqlite3_mprintf("cannot vacuum wal mode database");
2470 if( strlen(p
->zRbu
)>=5 && 0==memcmp("file:", p
->zRbu
, 5) ){
2471 zExtra
= &p
->zRbu
[5];
2473 if( *zExtra
++=='?' ) break;
2475 if( *zExtra
=='\0' ) zExtra
= 0;
2478 zTarget
= sqlite3_mprintf("file:%s-vacuum?rbu_memory=1%s%s",
2479 sqlite3_db_filename(p
->dbRbu
, "main"),
2480 (zExtra
==0 ? "" : "&"), (zExtra
==0 ? "" : zExtra
)
2484 p
->rc
= SQLITE_NOMEM
;
2487 p
->dbMain
= rbuOpenDbhandle(p
, zTarget
, p
->nRbu
<=1);
2488 sqlite3_free(zTarget
);
2492 if( p
->rc
==SQLITE_OK
){
2493 p
->rc
= sqlite3_create_function(p
->dbMain
,
2494 "rbu_tmp_insert", -1, SQLITE_UTF8
, (void*)p
, rbuTmpInsertFunc
, 0, 0
2498 if( p
->rc
==SQLITE_OK
){
2499 p
->rc
= sqlite3_create_function(p
->dbMain
,
2500 "rbu_fossil_delta", 2, SQLITE_UTF8
, 0, rbuFossilDeltaFunc
, 0, 0
2504 if( p
->rc
==SQLITE_OK
){
2505 p
->rc
= sqlite3_create_function(p
->dbRbu
,
2506 "rbu_target_name", -1, SQLITE_UTF8
, (void*)p
, rbuTargetNameFunc
, 0, 0
2510 if( p
->rc
==SQLITE_OK
){
2511 p
->rc
= sqlite3_file_control(p
->dbMain
, "main", SQLITE_FCNTL_RBU
, (void*)p
);
2513 rbuMPrintfExec(p
, p
->dbMain
, "SELECT * FROM sqlite_master");
2515 /* Mark the database file just opened as an RBU target database. If
2516 ** this call returns SQLITE_NOTFOUND, then the RBU vfs is not in use.
2517 ** This is an error. */
2518 if( p
->rc
==SQLITE_OK
){
2519 p
->rc
= sqlite3_file_control(p
->dbMain
, "main", SQLITE_FCNTL_RBU
, (void*)p
);
2522 if( p
->rc
==SQLITE_NOTFOUND
){
2523 p
->rc
= SQLITE_ERROR
;
2524 p
->zErrmsg
= sqlite3_mprintf("rbu vfs not found");
2529 ** This routine is a copy of the sqlite3FileSuffix3() routine from the core.
2530 ** It is a no-op unless SQLITE_ENABLE_8_3_NAMES is defined.
2532 ** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
2533 ** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and
2534 ** if filename in z[] has a suffix (a.k.a. "extension") that is longer than
2535 ** three characters, then shorten the suffix on z[] to be the last three
2536 ** characters of the original suffix.
2538 ** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
2539 ** do the suffix shortening regardless of URI parameter.
2543 ** test.db-journal => test.nal
2544 ** test.db-wal => test.wal
2545 ** test.db-shm => test.shm
2546 ** test.db-mj7f3319fa => test.9fa
2548 static void rbuFileSuffix3(const char *zBase
, char *z
){
2549 #ifdef SQLITE_ENABLE_8_3_NAMES
2550 #if SQLITE_ENABLE_8_3_NAMES<2
2551 if( sqlite3_uri_boolean(zBase
, "8_3_names", 0) )
2555 sz
= (int)strlen(z
)&0xffffff;
2556 for(i
=sz
-1; i
>0 && z
[i
]!='/' && z
[i
]!='.'; i
--){}
2557 if( z
[i
]=='.' && sz
>i
+4 ) memmove(&z
[i
+1], &z
[sz
-3], 4);
2563 ** Return the current wal-index header checksum for the target database
2564 ** as a 64-bit integer.
2566 ** The checksum is store in the first page of xShmMap memory as an 8-byte
2567 ** blob starting at byte offset 40.
2569 static i64
rbuShmChecksum(sqlite3rbu
*p
){
2571 if( p
->rc
==SQLITE_OK
){
2572 sqlite3_file
*pDb
= p
->pTargetFd
->pReal
;
2574 p
->rc
= pDb
->pMethods
->xShmMap(pDb
, 0, 32*1024, 0, (void volatile**)&ptr
);
2575 if( p
->rc
==SQLITE_OK
){
2576 iRet
= ((i64
)ptr
[10] << 32) + ptr
[11];
2583 ** This function is called as part of initializing or reinitializing an
2584 ** incremental checkpoint.
2586 ** It populates the sqlite3rbu.aFrame[] array with the set of
2587 ** (wal frame -> db page) copy operations required to checkpoint the
2588 ** current wal file, and obtains the set of shm locks required to safely
2589 ** perform the copy operations directly on the file-system.
2591 ** If argument pState is not NULL, then the incremental checkpoint is
2592 ** being resumed. In this case, if the checksum of the wal-index-header
2593 ** following recovery is not the same as the checksum saved in the RbuState
2594 ** object, then the rbu handle is set to DONE state. This occurs if some
2595 ** other client appends a transaction to the wal file in the middle of
2596 ** an incremental checkpoint.
2598 static void rbuSetupCheckpoint(sqlite3rbu
*p
, RbuState
*pState
){
2600 /* If pState is NULL, then the wal file may not have been opened and
2601 ** recovered. Running a read-statement here to ensure that doing so
2602 ** does not interfere with the "capture" process below. */
2605 if( p
->rc
==SQLITE_OK
){
2606 p
->rc
= sqlite3_exec(p
->dbMain
, "SELECT * FROM sqlite_master", 0, 0, 0);
2610 /* Assuming no error has occurred, run a "restart" checkpoint with the
2611 ** sqlite3rbu.eStage variable set to CAPTURE. This turns on the following
2612 ** special behaviour in the rbu VFS:
2614 ** * If the exclusive shm WRITER or READ0 lock cannot be obtained,
2615 ** the checkpoint fails with SQLITE_BUSY (normally SQLite would
2616 ** proceed with running a passive checkpoint instead of failing).
2618 ** * Attempts to read from the *-wal file or write to the database file
2619 ** do not perform any IO. Instead, the frame/page combinations that
2620 ** would be read/written are recorded in the sqlite3rbu.aFrame[]
2623 ** * Calls to xShmLock(UNLOCK) to release the exclusive shm WRITER,
2624 ** READ0 and CHECKPOINT locks taken as part of the checkpoint are
2625 ** no-ops. These locks will not be released until the connection
2628 ** * Attempting to xSync() the database file causes an SQLITE_INTERNAL
2631 ** As a result, unless an error (i.e. OOM or SQLITE_BUSY) occurs, the
2632 ** checkpoint below fails with SQLITE_INTERNAL, and leaves the aFrame[]
2633 ** array populated with a set of (frame -> page) mappings. Because the
2634 ** WRITER, CHECKPOINT and READ0 locks are still held, it is safe to copy
2635 ** data from the wal file into the database file according to the
2636 ** contents of aFrame[].
2638 if( p
->rc
==SQLITE_OK
){
2640 p
->eStage
= RBU_STAGE_CAPTURE
;
2641 rc2
= sqlite3_exec(p
->dbMain
, "PRAGMA main.wal_checkpoint=restart", 0, 0,0);
2642 if( rc2
!=SQLITE_INTERNAL
) p
->rc
= rc2
;
2645 if( p
->rc
==SQLITE_OK
&& p
->nFrame
>0 ){
2646 p
->eStage
= RBU_STAGE_CKPT
;
2647 p
->nStep
= (pState
? pState
->nRow
: 0);
2648 p
->aBuf
= rbuMalloc(p
, p
->pgsz
);
2649 p
->iWalCksum
= rbuShmChecksum(p
);
2652 if( p
->rc
==SQLITE_OK
){
2653 if( p
->nFrame
==0 || (pState
&& pState
->iWalCksum
!=p
->iWalCksum
) ){
2654 p
->rc
= SQLITE_DONE
;
2655 p
->eStage
= RBU_STAGE_DONE
;
2658 sqlite3_file
*pDb
= p
->pTargetFd
->pReal
;
2659 sqlite3_file
*pWal
= p
->pTargetFd
->pWalFd
->pReal
;
2660 assert( p
->nPagePerSector
==0 );
2661 nSectorSize
= pDb
->pMethods
->xSectorSize(pDb
);
2662 if( nSectorSize
>p
->pgsz
){
2663 p
->nPagePerSector
= nSectorSize
/ p
->pgsz
;
2665 p
->nPagePerSector
= 1;
2668 /* Call xSync() on the wal file. This causes SQLite to sync the
2669 ** directory in which the target database and the wal file reside, in
2670 ** case it has not been synced since the rename() call in
2671 ** rbuMoveOalFile(). */
2672 p
->rc
= pWal
->pMethods
->xSync(pWal
, SQLITE_SYNC_NORMAL
);
2678 ** Called when iAmt bytes are read from offset iOff of the wal file while
2679 ** the rbu object is in capture mode. Record the frame number of the frame
2680 ** being read in the aFrame[] array.
2682 static int rbuCaptureWalRead(sqlite3rbu
*pRbu
, i64 iOff
, int iAmt
){
2683 const u32 mReq
= (1<<WAL_LOCK_WRITE
)|(1<<WAL_LOCK_CKPT
)|(1<<WAL_LOCK_READ0
);
2686 if( pRbu
->mLock
!=mReq
){
2687 pRbu
->rc
= SQLITE_BUSY
;
2688 return SQLITE_INTERNAL
;
2692 if( pRbu
->nFrame
==pRbu
->nFrameAlloc
){
2693 int nNew
= (pRbu
->nFrameAlloc
? pRbu
->nFrameAlloc
: 64) * 2;
2695 aNew
= (RbuFrame
*)sqlite3_realloc64(pRbu
->aFrame
, nNew
* sizeof(RbuFrame
));
2696 if( aNew
==0 ) return SQLITE_NOMEM
;
2697 pRbu
->aFrame
= aNew
;
2698 pRbu
->nFrameAlloc
= nNew
;
2701 iFrame
= (u32
)((iOff
-32) / (i64
)(iAmt
+24)) + 1;
2702 if( pRbu
->iMaxFrame
<iFrame
) pRbu
->iMaxFrame
= iFrame
;
2703 pRbu
->aFrame
[pRbu
->nFrame
].iWalFrame
= iFrame
;
2704 pRbu
->aFrame
[pRbu
->nFrame
].iDbPage
= 0;
2710 ** Called when a page of data is written to offset iOff of the database
2711 ** file while the rbu handle is in capture mode. Record the page number
2712 ** of the page being written in the aFrame[] array.
2714 static int rbuCaptureDbWrite(sqlite3rbu
*pRbu
, i64 iOff
){
2715 pRbu
->aFrame
[pRbu
->nFrame
-1].iDbPage
= (u32
)(iOff
/ pRbu
->pgsz
) + 1;
2720 ** This is called as part of an incremental checkpoint operation. Copy
2721 ** a single frame of data from the wal file into the database file, as
2722 ** indicated by the RbuFrame object.
2724 static void rbuCheckpointFrame(sqlite3rbu
*p
, RbuFrame
*pFrame
){
2725 sqlite3_file
*pWal
= p
->pTargetFd
->pWalFd
->pReal
;
2726 sqlite3_file
*pDb
= p
->pTargetFd
->pReal
;
2729 assert( p
->rc
==SQLITE_OK
);
2730 iOff
= (i64
)(pFrame
->iWalFrame
-1) * (p
->pgsz
+ 24) + 32 + 24;
2731 p
->rc
= pWal
->pMethods
->xRead(pWal
, p
->aBuf
, p
->pgsz
, iOff
);
2734 iOff
= (i64
)(pFrame
->iDbPage
-1) * p
->pgsz
;
2735 p
->rc
= pDb
->pMethods
->xWrite(pDb
, p
->aBuf
, p
->pgsz
, iOff
);
2740 ** Take an EXCLUSIVE lock on the database file.
2742 static void rbuLockDatabase(sqlite3rbu
*p
){
2743 sqlite3_file
*pReal
= p
->pTargetFd
->pReal
;
2744 assert( p
->rc
==SQLITE_OK
);
2745 p
->rc
= pReal
->pMethods
->xLock(pReal
, SQLITE_LOCK_SHARED
);
2746 if( p
->rc
==SQLITE_OK
){
2747 p
->rc
= pReal
->pMethods
->xLock(pReal
, SQLITE_LOCK_EXCLUSIVE
);
2751 #if defined(_WIN32_WCE)
2752 static LPWSTR
rbuWinUtf8ToUnicode(const char *zFilename
){
2754 LPWSTR zWideFilename
;
2756 nChar
= MultiByteToWideChar(CP_UTF8
, 0, zFilename
, -1, NULL
, 0);
2760 zWideFilename
= sqlite3_malloc64( nChar
*sizeof(zWideFilename
[0]) );
2761 if( zWideFilename
==0 ){
2764 memset(zWideFilename
, 0, nChar
*sizeof(zWideFilename
[0]));
2765 nChar
= MultiByteToWideChar(CP_UTF8
, 0, zFilename
, -1, zWideFilename
,
2768 sqlite3_free(zWideFilename
);
2771 return zWideFilename
;
2776 ** The RBU handle is currently in RBU_STAGE_OAL state, with a SHARED lock
2777 ** on the database file. This proc moves the *-oal file to the *-wal path,
2778 ** then reopens the database file (this time in vanilla, non-oal, WAL mode).
2779 ** If an error occurs, leave an error code and error message in the rbu
2782 static void rbuMoveOalFile(sqlite3rbu
*p
){
2783 const char *zBase
= sqlite3_db_filename(p
->dbMain
, "main");
2784 const char *zMove
= zBase
;
2788 if( rbuIsVacuum(p
) ){
2789 zMove
= sqlite3_db_filename(p
->dbRbu
, "main");
2791 zOal
= sqlite3_mprintf("%s-oal", zMove
);
2792 zWal
= sqlite3_mprintf("%s-wal", zMove
);
2794 assert( p
->eStage
==RBU_STAGE_MOVE
);
2795 assert( p
->rc
==SQLITE_OK
&& p
->zErrmsg
==0 );
2796 if( zWal
==0 || zOal
==0 ){
2797 p
->rc
= SQLITE_NOMEM
;
2799 /* Move the *-oal file to *-wal. At this point connection p->db is
2800 ** holding a SHARED lock on the target database file (because it is
2801 ** in WAL mode). So no other connection may be writing the db.
2803 ** In order to ensure that there are no database readers, an EXCLUSIVE
2804 ** lock is obtained here before the *-oal is moved to *-wal.
2807 if( p
->rc
==SQLITE_OK
){
2808 rbuFileSuffix3(zBase
, zWal
);
2809 rbuFileSuffix3(zBase
, zOal
);
2811 /* Re-open the databases. */
2812 rbuObjIterFinalize(&p
->objiter
);
2813 sqlite3_close(p
->dbRbu
);
2814 sqlite3_close(p
->dbMain
);
2818 #if defined(_WIN32_WCE)
2823 zWideOal
= rbuWinUtf8ToUnicode(zOal
);
2825 zWideWal
= rbuWinUtf8ToUnicode(zWal
);
2827 if( MoveFileW(zWideOal
, zWideWal
) ){
2830 p
->rc
= SQLITE_IOERR
;
2832 sqlite3_free(zWideWal
);
2834 p
->rc
= SQLITE_IOERR_NOMEM
;
2836 sqlite3_free(zWideOal
);
2838 p
->rc
= SQLITE_IOERR_NOMEM
;
2842 p
->rc
= rename(zOal
, zWal
) ? SQLITE_IOERR
: SQLITE_OK
;
2845 if( p
->rc
==SQLITE_OK
){
2846 rbuOpenDatabase(p
, 0);
2847 rbuSetupCheckpoint(p
, 0);
2857 ** The SELECT statement iterating through the keys for the current object
2858 ** (p->objiter.pSelect) currently points to a valid row. This function
2859 ** determines the type of operation requested by this row and returns
2860 ** one of the following values to indicate the result:
2867 ** If RBU_UPDATE is returned, then output variable *pzMask is set to
2868 ** point to the text value indicating the columns to update.
2870 ** If the rbu_control field contains an invalid value, an error code and
2871 ** message are left in the RBU handle and zero returned.
2873 static int rbuStepType(sqlite3rbu
*p
, const char **pzMask
){
2874 int iCol
= p
->objiter
.nCol
; /* Index of rbu_control column */
2875 int res
= 0; /* Return value */
2877 switch( sqlite3_column_type(p
->objiter
.pSelect
, iCol
) ){
2878 case SQLITE_INTEGER
: {
2879 int iVal
= sqlite3_column_int(p
->objiter
.pSelect
, iCol
);
2881 case 0: res
= RBU_INSERT
; break;
2882 case 1: res
= RBU_DELETE
; break;
2883 case 2: res
= RBU_REPLACE
; break;
2884 case 3: res
= RBU_IDX_DELETE
; break;
2885 case 4: res
= RBU_IDX_INSERT
; break;
2891 const unsigned char *z
= sqlite3_column_text(p
->objiter
.pSelect
, iCol
);
2893 p
->rc
= SQLITE_NOMEM
;
2895 *pzMask
= (const char*)z
;
2907 rbuBadControlError(p
);
2914 ** Assert that column iCol of statement pStmt is named zName.
2916 static void assertColumnName(sqlite3_stmt
*pStmt
, int iCol
, const char *zName
){
2917 const char *zCol
= sqlite3_column_name(pStmt
, iCol
);
2918 assert( 0==sqlite3_stricmp(zName
, zCol
) );
2921 # define assertColumnName(x,y,z)
2925 ** Argument eType must be one of RBU_INSERT, RBU_DELETE, RBU_IDX_INSERT or
2926 ** RBU_IDX_DELETE. This function performs the work of a single
2927 ** sqlite3rbu_step() call for the type of operation specified by eType.
2929 static void rbuStepOneOp(sqlite3rbu
*p
, int eType
){
2930 RbuObjIter
*pIter
= &p
->objiter
;
2931 sqlite3_value
*pVal
;
2932 sqlite3_stmt
*pWriter
;
2935 assert( p
->rc
==SQLITE_OK
);
2936 assert( eType
!=RBU_DELETE
|| pIter
->zIdx
==0 );
2937 assert( eType
==RBU_DELETE
|| eType
==RBU_IDX_DELETE
2938 || eType
==RBU_INSERT
|| eType
==RBU_IDX_INSERT
2941 /* If this is a delete, decrement nPhaseOneStep by nIndex. If the DELETE
2942 ** statement below does actually delete a row, nPhaseOneStep will be
2943 ** incremented by the same amount when SQL function rbu_tmp_insert()
2944 ** is invoked by the trigger. */
2945 if( eType
==RBU_DELETE
){
2946 p
->nPhaseOneStep
-= p
->objiter
.nIndex
;
2949 if( eType
==RBU_IDX_DELETE
|| eType
==RBU_DELETE
){
2950 pWriter
= pIter
->pDelete
;
2952 pWriter
= pIter
->pInsert
;
2955 for(i
=0; i
<pIter
->nCol
; i
++){
2956 /* If this is an INSERT into a table b-tree and the table has an
2957 ** explicit INTEGER PRIMARY KEY, check that this is not an attempt
2958 ** to write a NULL into the IPK column. That is not permitted. */
2959 if( eType
==RBU_INSERT
2960 && pIter
->zIdx
==0 && pIter
->eType
==RBU_PK_IPK
&& pIter
->abTblPk
[i
]
2961 && sqlite3_column_type(pIter
->pSelect
, i
)==SQLITE_NULL
2963 p
->rc
= SQLITE_MISMATCH
;
2964 p
->zErrmsg
= sqlite3_mprintf("datatype mismatch");
2968 if( eType
==RBU_DELETE
&& pIter
->abTblPk
[i
]==0 ){
2972 pVal
= sqlite3_column_value(pIter
->pSelect
, i
);
2973 p
->rc
= sqlite3_bind_value(pWriter
, i
+1, pVal
);
2976 if( pIter
->zIdx
==0 ){
2977 if( pIter
->eType
==RBU_PK_VTAB
2978 || pIter
->eType
==RBU_PK_NONE
2979 || (pIter
->eType
==RBU_PK_EXTERNAL
&& rbuIsVacuum(p
))
2981 /* For a virtual table, or a table with no primary key, the
2982 ** SELECT statement is:
2984 ** SELECT <cols>, rbu_control, rbu_rowid FROM ....
2986 ** Hence column_value(pIter->nCol+1).
2988 assertColumnName(pIter
->pSelect
, pIter
->nCol
+1,
2989 rbuIsVacuum(p
) ? "rowid" : "rbu_rowid"
2991 pVal
= sqlite3_column_value(pIter
->pSelect
, pIter
->nCol
+1);
2992 p
->rc
= sqlite3_bind_value(pWriter
, pIter
->nCol
+1, pVal
);
2995 if( p
->rc
==SQLITE_OK
){
2996 sqlite3_step(pWriter
);
2997 p
->rc
= resetAndCollectError(pWriter
, &p
->zErrmsg
);
3002 ** This function does the work for an sqlite3rbu_step() call.
3004 ** The object-iterator (p->objiter) currently points to a valid object,
3005 ** and the input cursor (p->objiter.pSelect) currently points to a valid
3006 ** input row. Perform whatever processing is required and return.
3008 ** If no error occurs, SQLITE_OK is returned. Otherwise, an error code
3009 ** and message is left in the RBU handle and a copy of the error code
3012 static int rbuStep(sqlite3rbu
*p
){
3013 RbuObjIter
*pIter
= &p
->objiter
;
3014 const char *zMask
= 0;
3015 int eType
= rbuStepType(p
, &zMask
);
3018 assert( eType
==RBU_INSERT
|| eType
==RBU_DELETE
3019 || eType
==RBU_REPLACE
|| eType
==RBU_IDX_DELETE
3020 || eType
==RBU_IDX_INSERT
|| eType
==RBU_UPDATE
3022 assert( eType
!=RBU_UPDATE
|| pIter
->zIdx
==0 );
3024 if( pIter
->zIdx
==0 && (eType
==RBU_IDX_DELETE
|| eType
==RBU_IDX_INSERT
) ){
3025 rbuBadControlError(p
);
3027 else if( eType
==RBU_REPLACE
){
3028 if( pIter
->zIdx
==0 ){
3029 p
->nPhaseOneStep
+= p
->objiter
.nIndex
;
3030 rbuStepOneOp(p
, RBU_DELETE
);
3032 if( p
->rc
==SQLITE_OK
) rbuStepOneOp(p
, RBU_INSERT
);
3034 else if( eType
!=RBU_UPDATE
){
3035 rbuStepOneOp(p
, eType
);
3038 sqlite3_value
*pVal
;
3039 sqlite3_stmt
*pUpdate
= 0;
3040 assert( eType
==RBU_UPDATE
);
3041 p
->nPhaseOneStep
-= p
->objiter
.nIndex
;
3042 rbuGetUpdateStmt(p
, pIter
, zMask
, &pUpdate
);
3045 for(i
=0; p
->rc
==SQLITE_OK
&& i
<pIter
->nCol
; i
++){
3046 char c
= zMask
[pIter
->aiSrcOrder
[i
]];
3047 pVal
= sqlite3_column_value(pIter
->pSelect
, i
);
3048 if( pIter
->abTblPk
[i
] || c
!='.' ){
3049 p
->rc
= sqlite3_bind_value(pUpdate
, i
+1, pVal
);
3052 if( p
->rc
==SQLITE_OK
3053 && (pIter
->eType
==RBU_PK_VTAB
|| pIter
->eType
==RBU_PK_NONE
)
3055 /* Bind the rbu_rowid value to column _rowid_ */
3056 assertColumnName(pIter
->pSelect
, pIter
->nCol
+1, "rbu_rowid");
3057 pVal
= sqlite3_column_value(pIter
->pSelect
, pIter
->nCol
+1);
3058 p
->rc
= sqlite3_bind_value(pUpdate
, pIter
->nCol
+1, pVal
);
3060 if( p
->rc
==SQLITE_OK
){
3061 sqlite3_step(pUpdate
);
3062 p
->rc
= resetAndCollectError(pUpdate
, &p
->zErrmsg
);
3071 ** Increment the schema cookie of the main database opened by p->dbMain.
3073 ** Or, if this is an RBU vacuum, set the schema cookie of the main db
3074 ** opened by p->dbMain to one more than the schema cookie of the main
3075 ** db opened by p->dbRbu.
3077 static void rbuIncrSchemaCookie(sqlite3rbu
*p
){
3078 if( p
->rc
==SQLITE_OK
){
3079 sqlite3
*dbread
= (rbuIsVacuum(p
) ? p
->dbRbu
: p
->dbMain
);
3080 int iCookie
= 1000000;
3081 sqlite3_stmt
*pStmt
;
3083 p
->rc
= prepareAndCollectError(dbread
, &pStmt
, &p
->zErrmsg
,
3084 "PRAGMA schema_version"
3086 if( p
->rc
==SQLITE_OK
){
3087 /* Coverage: it may be that this sqlite3_step() cannot fail. There
3088 ** is already a transaction open, so the prepared statement cannot
3089 ** throw an SQLITE_SCHEMA exception. The only database page the
3090 ** statement reads is page 1, which is guaranteed to be in the cache.
3091 ** And no memory allocations are required. */
3092 if( SQLITE_ROW
==sqlite3_step(pStmt
) ){
3093 iCookie
= sqlite3_column_int(pStmt
, 0);
3095 rbuFinalize(p
, pStmt
);
3097 if( p
->rc
==SQLITE_OK
){
3098 rbuMPrintfExec(p
, p
->dbMain
, "PRAGMA schema_version = %d", iCookie
+1);
3104 ** Update the contents of the rbu_state table within the rbu database. The
3105 ** value stored in the RBU_STATE_STAGE column is eStage. All other values
3106 ** are determined by inspecting the rbu handle passed as the first argument.
3108 static void rbuSaveState(sqlite3rbu
*p
, int eStage
){
3109 if( p
->rc
==SQLITE_OK
|| p
->rc
==SQLITE_DONE
){
3110 sqlite3_stmt
*pInsert
= 0;
3111 rbu_file
*pFd
= (rbuIsVacuum(p
) ? p
->pRbuFd
: p
->pTargetFd
);
3114 assert( p
->zErrmsg
==0 );
3115 rc
= prepareFreeAndCollectError(p
->dbRbu
, &pInsert
, &p
->zErrmsg
,
3117 "INSERT OR REPLACE INTO %s.rbu_state(k, v) VALUES "
3129 RBU_STATE_STAGE
, eStage
,
3130 RBU_STATE_TBL
, p
->objiter
.zTbl
,
3131 RBU_STATE_IDX
, p
->objiter
.zIdx
,
3132 RBU_STATE_ROW
, p
->nStep
,
3133 RBU_STATE_PROGRESS
, p
->nProgress
,
3134 RBU_STATE_CKPT
, p
->iWalCksum
,
3135 RBU_STATE_COOKIE
, (i64
)pFd
->iCookie
,
3136 RBU_STATE_OALSZ
, p
->iOalSz
,
3137 RBU_STATE_PHASEONESTEP
, p
->nPhaseOneStep
,
3138 RBU_STATE_DATATBL
, p
->objiter
.zDataTbl
3141 assert( pInsert
==0 || rc
==SQLITE_OK
);
3143 if( rc
==SQLITE_OK
){
3144 sqlite3_step(pInsert
);
3145 rc
= sqlite3_finalize(pInsert
);
3147 if( rc
!=SQLITE_OK
) p
->rc
= rc
;
3153 ** The second argument passed to this function is the name of a PRAGMA
3154 ** setting - "page_size", "auto_vacuum", "user_version" or "application_id".
3155 ** This function executes the following on sqlite3rbu.dbRbu:
3157 ** "PRAGMA main.$zPragma"
3159 ** where $zPragma is the string passed as the second argument, then
3160 ** on sqlite3rbu.dbMain:
3162 ** "PRAGMA main.$zPragma = $val"
3164 ** where $val is the value returned by the first PRAGMA invocation.
3166 ** In short, it copies the value of the specified PRAGMA setting from
3169 static void rbuCopyPragma(sqlite3rbu
*p
, const char *zPragma
){
3170 if( p
->rc
==SQLITE_OK
){
3171 sqlite3_stmt
*pPragma
= 0;
3172 p
->rc
= prepareFreeAndCollectError(p
->dbRbu
, &pPragma
, &p
->zErrmsg
,
3173 sqlite3_mprintf("PRAGMA main.%s", zPragma
)
3175 if( p
->rc
==SQLITE_OK
&& SQLITE_ROW
==sqlite3_step(pPragma
) ){
3176 p
->rc
= rbuMPrintfExec(p
, p
->dbMain
, "PRAGMA main.%s = %d",
3177 zPragma
, sqlite3_column_int(pPragma
, 0)
3180 rbuFinalize(p
, pPragma
);
3185 ** The RBU handle passed as the only argument has just been opened and
3186 ** the state database is empty. If this RBU handle was opened for an
3187 ** RBU vacuum operation, create the schema in the target db.
3189 static void rbuCreateTargetSchema(sqlite3rbu
*p
){
3190 sqlite3_stmt
*pSql
= 0;
3191 sqlite3_stmt
*pInsert
= 0;
3193 assert( rbuIsVacuum(p
) );
3194 p
->rc
= sqlite3_exec(p
->dbMain
, "PRAGMA writable_schema=1", 0,0, &p
->zErrmsg
);
3195 if( p
->rc
==SQLITE_OK
){
3196 p
->rc
= prepareAndCollectError(p
->dbRbu
, &pSql
, &p
->zErrmsg
,
3197 "SELECT sql FROM sqlite_master WHERE sql!='' AND rootpage!=0"
3198 " AND name!='sqlite_sequence' "
3199 " ORDER BY type DESC"
3203 while( p
->rc
==SQLITE_OK
&& sqlite3_step(pSql
)==SQLITE_ROW
){
3204 const char *zSql
= (const char*)sqlite3_column_text(pSql
, 0);
3205 p
->rc
= sqlite3_exec(p
->dbMain
, zSql
, 0, 0, &p
->zErrmsg
);
3207 rbuFinalize(p
, pSql
);
3208 if( p
->rc
!=SQLITE_OK
) return;
3210 if( p
->rc
==SQLITE_OK
){
3211 p
->rc
= prepareAndCollectError(p
->dbRbu
, &pSql
, &p
->zErrmsg
,
3212 "SELECT * FROM sqlite_master WHERE rootpage=0 OR rootpage IS NULL"
3216 if( p
->rc
==SQLITE_OK
){
3217 p
->rc
= prepareAndCollectError(p
->dbMain
, &pInsert
, &p
->zErrmsg
,
3218 "INSERT INTO sqlite_master VALUES(?,?,?,?,?)"
3222 while( p
->rc
==SQLITE_OK
&& sqlite3_step(pSql
)==SQLITE_ROW
){
3225 sqlite3_bind_value(pInsert
, i
+1, sqlite3_column_value(pSql
, i
));
3227 sqlite3_step(pInsert
);
3228 p
->rc
= sqlite3_reset(pInsert
);
3230 if( p
->rc
==SQLITE_OK
){
3231 p
->rc
= sqlite3_exec(p
->dbMain
, "PRAGMA writable_schema=0",0,0,&p
->zErrmsg
);
3234 rbuFinalize(p
, pSql
);
3235 rbuFinalize(p
, pInsert
);
3239 ** Step the RBU object.
3241 int sqlite3rbu_step(sqlite3rbu
*p
){
3243 switch( p
->eStage
){
3244 case RBU_STAGE_OAL
: {
3245 RbuObjIter
*pIter
= &p
->objiter
;
3247 /* If this is an RBU vacuum operation and the state table was empty
3248 ** when this handle was opened, create the target database schema. */
3249 if( rbuIsVacuum(p
) && p
->nProgress
==0 && p
->rc
==SQLITE_OK
){
3250 rbuCreateTargetSchema(p
);
3251 rbuCopyPragma(p
, "user_version");
3252 rbuCopyPragma(p
, "application_id");
3255 while( p
->rc
==SQLITE_OK
&& pIter
->zTbl
){
3257 if( pIter
->bCleanup
){
3258 /* Clean up the rbu_tmp_xxx table for the previous table. It
3259 ** cannot be dropped as there are currently active SQL statements.
3260 ** But the contents can be deleted. */
3261 if( rbuIsVacuum(p
)==0 && pIter
->abIndexed
){
3262 rbuMPrintfExec(p
, p
->dbRbu
,
3263 "DELETE FROM %s.'rbu_tmp_%q'", p
->zStateDb
, pIter
->zDataTbl
3267 rbuObjIterPrepareAll(p
, pIter
, 0);
3269 /* Advance to the next row to process. */
3270 if( p
->rc
==SQLITE_OK
){
3271 int rc
= sqlite3_step(pIter
->pSelect
);
3272 if( rc
==SQLITE_ROW
){
3277 p
->rc
= sqlite3_reset(pIter
->pSelect
);
3282 rbuObjIterNext(p
, pIter
);
3285 if( p
->rc
==SQLITE_OK
){
3286 assert( pIter
->zTbl
==0 );
3287 rbuSaveState(p
, RBU_STAGE_MOVE
);
3288 rbuIncrSchemaCookie(p
);
3289 if( p
->rc
==SQLITE_OK
){
3290 p
->rc
= sqlite3_exec(p
->dbMain
, "COMMIT", 0, 0, &p
->zErrmsg
);
3292 if( p
->rc
==SQLITE_OK
){
3293 p
->rc
= sqlite3_exec(p
->dbRbu
, "COMMIT", 0, 0, &p
->zErrmsg
);
3295 p
->eStage
= RBU_STAGE_MOVE
;
3300 case RBU_STAGE_MOVE
: {
3301 if( p
->rc
==SQLITE_OK
){
3308 case RBU_STAGE_CKPT
: {
3309 if( p
->rc
==SQLITE_OK
){
3310 if( p
->nStep
>=p
->nFrame
){
3311 sqlite3_file
*pDb
= p
->pTargetFd
->pReal
;
3313 /* Sync the db file */
3314 p
->rc
= pDb
->pMethods
->xSync(pDb
, SQLITE_SYNC_NORMAL
);
3316 /* Update nBackfill */
3317 if( p
->rc
==SQLITE_OK
){
3319 p
->rc
= pDb
->pMethods
->xShmMap(pDb
, 0, 32*1024, 0, &ptr
);
3320 if( p
->rc
==SQLITE_OK
){
3321 ((u32
volatile*)ptr
)[24] = p
->iMaxFrame
;
3325 if( p
->rc
==SQLITE_OK
){
3326 p
->eStage
= RBU_STAGE_DONE
;
3327 p
->rc
= SQLITE_DONE
;
3330 /* At one point the following block copied a single frame from the
3331 ** wal file to the database file. So that one call to sqlite3rbu_step()
3332 ** checkpointed a single frame.
3334 ** However, if the sector-size is larger than the page-size, and the
3335 ** application calls sqlite3rbu_savestate() or close() immediately
3336 ** after this step, then rbu_step() again, then a power failure occurs,
3337 ** then the database page written here may be damaged. Work around
3338 ** this by checkpointing frames until the next page in the aFrame[]
3339 ** lies on a different disk sector to the current one. */
3342 RbuFrame
*pFrame
= &p
->aFrame
[p
->nStep
];
3343 iSector
= (pFrame
->iDbPage
-1) / p
->nPagePerSector
;
3344 rbuCheckpointFrame(p
, pFrame
);
3346 }while( p
->nStep
<p
->nFrame
3347 && iSector
==((p
->aFrame
[p
->nStep
].iDbPage
-1) / p
->nPagePerSector
)
3361 return SQLITE_NOMEM
;
3366 ** Compare strings z1 and z2, returning 0 if they are identical, or non-zero
3367 ** otherwise. Either or both argument may be NULL. Two NULL values are
3368 ** considered equal, and NULL is considered distinct from all other values.
3370 static int rbuStrCompare(const char *z1
, const char *z2
){
3371 if( z1
==0 && z2
==0 ) return 0;
3372 if( z1
==0 || z2
==0 ) return 1;
3373 return (sqlite3_stricmp(z1
, z2
)!=0);
3377 ** This function is called as part of sqlite3rbu_open() when initializing
3378 ** an rbu handle in OAL stage. If the rbu update has not started (i.e.
3379 ** the rbu_state table was empty) it is a no-op. Otherwise, it arranges
3380 ** things so that the next call to sqlite3rbu_step() continues on from
3381 ** where the previous rbu handle left off.
3383 ** If an error occurs, an error code and error message are left in the
3384 ** rbu handle passed as the first argument.
3386 static void rbuSetupOal(sqlite3rbu
*p
, RbuState
*pState
){
3387 assert( p
->rc
==SQLITE_OK
);
3389 RbuObjIter
*pIter
= &p
->objiter
;
3392 while( rc
==SQLITE_OK
&& pIter
->zTbl
&& (pIter
->bCleanup
3393 || rbuStrCompare(pIter
->zIdx
, pState
->zIdx
)
3394 || (pState
->zDataTbl
==0 && rbuStrCompare(pIter
->zTbl
, pState
->zTbl
))
3395 || (pState
->zDataTbl
&& rbuStrCompare(pIter
->zDataTbl
, pState
->zDataTbl
))
3397 rc
= rbuObjIterNext(p
, pIter
);
3400 if( rc
==SQLITE_OK
&& !pIter
->zTbl
){
3402 p
->zErrmsg
= sqlite3_mprintf("rbu_state mismatch error");
3405 if( rc
==SQLITE_OK
){
3406 p
->nStep
= pState
->nRow
;
3407 rc
= rbuObjIterPrepareAll(p
, &p
->objiter
, p
->nStep
);
3415 ** If there is a "*-oal" file in the file-system corresponding to the
3416 ** target database in the file-system, delete it. If an error occurs,
3417 ** leave an error code and error message in the rbu handle.
3419 static void rbuDeleteOalFile(sqlite3rbu
*p
){
3420 char *zOal
= rbuMPrintf(p
, "%s-oal", p
->zTarget
);
3422 sqlite3_vfs
*pVfs
= sqlite3_vfs_find(0);
3423 assert( pVfs
&& p
->rc
==SQLITE_OK
&& p
->zErrmsg
==0 );
3424 pVfs
->xDelete(pVfs
, zOal
, 0);
3430 ** Allocate a private rbu VFS for the rbu handle passed as the only
3431 ** argument. This VFS will be used unless the call to sqlite3rbu_open()
3432 ** specified a URI with a vfs=? option in place of a target database
3435 static void rbuCreateVfs(sqlite3rbu
*p
){
3439 assert( p
->rc
==SQLITE_OK
);
3440 sqlite3_randomness(sizeof(int), (void*)&rnd
);
3441 sqlite3_snprintf(sizeof(zRnd
), zRnd
, "rbu_vfs_%d", rnd
);
3442 p
->rc
= sqlite3rbu_create_vfs(zRnd
, 0);
3443 if( p
->rc
==SQLITE_OK
){
3444 sqlite3_vfs
*pVfs
= sqlite3_vfs_find(zRnd
);
3446 p
->zVfsName
= pVfs
->zName
;
3447 ((rbu_vfs
*)pVfs
)->pRbu
= p
;
3452 ** Destroy the private VFS created for the rbu handle passed as the only
3453 ** argument by an earlier call to rbuCreateVfs().
3455 static void rbuDeleteVfs(sqlite3rbu
*p
){
3457 sqlite3rbu_destroy_vfs(p
->zVfsName
);
3463 ** This user-defined SQL function is invoked with a single argument - the
3464 ** name of a table expected to appear in the target database. It returns
3465 ** the number of auxilliary indexes on the table.
3467 static void rbuIndexCntFunc(
3468 sqlite3_context
*pCtx
,
3470 sqlite3_value
**apVal
3472 sqlite3rbu
*p
= (sqlite3rbu
*)sqlite3_user_data(pCtx
);
3473 sqlite3_stmt
*pStmt
= 0;
3479 rc
= prepareFreeAndCollectError(p
->dbMain
, &pStmt
, &zErrmsg
,
3480 sqlite3_mprintf("SELECT count(*) FROM sqlite_master "
3481 "WHERE type='index' AND tbl_name = %Q", sqlite3_value_text(apVal
[0]))
3483 if( rc
!=SQLITE_OK
){
3484 sqlite3_result_error(pCtx
, zErrmsg
, -1);
3487 if( SQLITE_ROW
==sqlite3_step(pStmt
) ){
3488 nIndex
= sqlite3_column_int(pStmt
, 0);
3490 rc
= sqlite3_finalize(pStmt
);
3491 if( rc
==SQLITE_OK
){
3492 sqlite3_result_int(pCtx
, nIndex
);
3494 sqlite3_result_error(pCtx
, sqlite3_errmsg(p
->dbMain
), -1);
3498 sqlite3_free(zErrmsg
);
3502 ** If the RBU database contains the rbu_count table, use it to initialize
3503 ** the sqlite3rbu.nPhaseOneStep variable. The schema of the rbu_count table
3504 ** is assumed to contain the same columns as:
3506 ** CREATE TABLE rbu_count(tbl TEXT PRIMARY KEY, cnt INTEGER) WITHOUT ROWID;
3508 ** There should be one row in the table for each data_xxx table in the
3509 ** database. The 'tbl' column should contain the name of a data_xxx table,
3510 ** and the cnt column the number of rows it contains.
3512 ** sqlite3rbu.nPhaseOneStep is initialized to the sum of (1 + nIndex) * cnt
3513 ** for all rows in the rbu_count table, where nIndex is the number of
3514 ** indexes on the corresponding target database table.
3516 static void rbuInitPhaseOneSteps(sqlite3rbu
*p
){
3517 if( p
->rc
==SQLITE_OK
){
3518 sqlite3_stmt
*pStmt
= 0;
3519 int bExists
= 0; /* True if rbu_count exists */
3521 p
->nPhaseOneStep
= -1;
3523 p
->rc
= sqlite3_create_function(p
->dbRbu
,
3524 "rbu_index_cnt", 1, SQLITE_UTF8
, (void*)p
, rbuIndexCntFunc
, 0, 0
3527 /* Check for the rbu_count table. If it does not exist, or if an error
3528 ** occurs, nPhaseOneStep will be left set to -1. */
3529 if( p
->rc
==SQLITE_OK
){
3530 p
->rc
= prepareAndCollectError(p
->dbRbu
, &pStmt
, &p
->zErrmsg
,
3531 "SELECT 1 FROM sqlite_master WHERE tbl_name = 'rbu_count'"
3534 if( p
->rc
==SQLITE_OK
){
3535 if( SQLITE_ROW
==sqlite3_step(pStmt
) ){
3538 p
->rc
= sqlite3_finalize(pStmt
);
3541 if( p
->rc
==SQLITE_OK
&& bExists
){
3542 p
->rc
= prepareAndCollectError(p
->dbRbu
, &pStmt
, &p
->zErrmsg
,
3543 "SELECT sum(cnt * (1 + rbu_index_cnt(rbu_target_name(tbl))))"
3546 if( p
->rc
==SQLITE_OK
){
3547 if( SQLITE_ROW
==sqlite3_step(pStmt
) ){
3548 p
->nPhaseOneStep
= sqlite3_column_int64(pStmt
, 0);
3550 p
->rc
= sqlite3_finalize(pStmt
);
3557 static sqlite3rbu
*openRbuHandle(
3558 const char *zTarget
,
3563 size_t nTarget
= zTarget
? strlen(zTarget
) : 0;
3564 size_t nRbu
= strlen(zRbu
);
3565 size_t nByte
= sizeof(sqlite3rbu
) + nTarget
+1 + nRbu
+1;
3567 p
= (sqlite3rbu
*)sqlite3_malloc64(nByte
);
3569 RbuState
*pState
= 0;
3571 /* Create the custom VFS. */
3572 memset(p
, 0, sizeof(sqlite3rbu
));
3575 /* Open the target, RBU and state databases */
3576 if( p
->rc
==SQLITE_OK
){
3577 char *pCsr
= (char*)&p
[1];
3581 memcpy(p
->zTarget
, zTarget
, nTarget
+1);
3585 memcpy(p
->zRbu
, zRbu
, nRbu
+1);
3588 p
->zState
= rbuMPrintf(p
, "%s", zState
);
3591 /* If the first attempt to open the database file fails and the bRetry
3592 ** flag it set, this means that the db was not opened because it seemed
3593 ** to be a wal-mode db. But, this may have happened due to an earlier
3594 ** RBU vacuum operation leaving an old wal file in the directory.
3595 ** If this is the case, it will have been checkpointed and deleted
3596 ** when the handle was closed and a second attempt to open the
3597 ** database may succeed. */
3598 rbuOpenDatabase(p
, &bRetry
);
3600 rbuOpenDatabase(p
, 0);
3604 if( p
->rc
==SQLITE_OK
){
3605 pState
= rbuLoadState(p
);
3606 assert( pState
|| p
->rc
!=SQLITE_OK
);
3607 if( p
->rc
==SQLITE_OK
){
3609 if( pState
->eStage
==0 ){
3610 rbuDeleteOalFile(p
);
3611 rbuInitPhaseOneSteps(p
);
3612 p
->eStage
= RBU_STAGE_OAL
;
3614 p
->eStage
= pState
->eStage
;
3615 p
->nPhaseOneStep
= pState
->nPhaseOneStep
;
3617 p
->nProgress
= pState
->nProgress
;
3618 p
->iOalSz
= pState
->iOalSz
;
3621 assert( p
->rc
!=SQLITE_OK
|| p
->eStage
!=0 );
3623 if( p
->rc
==SQLITE_OK
&& p
->pTargetFd
->pWalFd
){
3624 if( p
->eStage
==RBU_STAGE_OAL
){
3625 p
->rc
= SQLITE_ERROR
;
3626 p
->zErrmsg
= sqlite3_mprintf("cannot update wal mode database");
3627 }else if( p
->eStage
==RBU_STAGE_MOVE
){
3628 p
->eStage
= RBU_STAGE_CKPT
;
3633 if( p
->rc
==SQLITE_OK
3634 && (p
->eStage
==RBU_STAGE_OAL
|| p
->eStage
==RBU_STAGE_MOVE
)
3635 && pState
->eStage
!=0
3637 rbu_file
*pFd
= (rbuIsVacuum(p
) ? p
->pRbuFd
: p
->pTargetFd
);
3638 if( pFd
->iCookie
!=pState
->iCookie
){
3639 /* At this point (pTargetFd->iCookie) contains the value of the
3640 ** change-counter cookie (the thing that gets incremented when a
3641 ** transaction is committed in rollback mode) currently stored on
3642 ** page 1 of the database file. */
3643 p
->rc
= SQLITE_BUSY
;
3644 p
->zErrmsg
= sqlite3_mprintf("database modified during rbu %s",
3645 (rbuIsVacuum(p
) ? "vacuum" : "update")
3650 if( p
->rc
==SQLITE_OK
){
3651 if( p
->eStage
==RBU_STAGE_OAL
){
3652 sqlite3
*db
= p
->dbMain
;
3653 p
->rc
= sqlite3_exec(p
->dbRbu
, "BEGIN", 0, 0, &p
->zErrmsg
);
3655 /* Point the object iterator at the first object */
3656 if( p
->rc
==SQLITE_OK
){
3657 p
->rc
= rbuObjIterFirst(p
, &p
->objiter
);
3660 /* If the RBU database contains no data_xxx tables, declare the RBU
3661 ** update finished. */
3662 if( p
->rc
==SQLITE_OK
&& p
->objiter
.zTbl
==0 ){
3663 p
->rc
= SQLITE_DONE
;
3664 p
->eStage
= RBU_STAGE_DONE
;
3666 if( p
->rc
==SQLITE_OK
&& pState
->eStage
==0 && rbuIsVacuum(p
) ){
3667 rbuCopyPragma(p
, "page_size");
3668 rbuCopyPragma(p
, "auto_vacuum");
3671 /* Open transactions both databases. The *-oal file is opened or
3672 ** created at this point. */
3673 if( p
->rc
==SQLITE_OK
){
3674 p
->rc
= sqlite3_exec(db
, "BEGIN IMMEDIATE", 0, 0, &p
->zErrmsg
);
3677 /* Check if the main database is a zipvfs db. If it is, set the upper
3678 ** level pager to use "journal_mode=off". This prevents it from
3679 ** generating a large journal using a temp file. */
3680 if( p
->rc
==SQLITE_OK
){
3681 int frc
= sqlite3_file_control(db
, "main", SQLITE_FCNTL_ZIPVFS
, 0);
3682 if( frc
==SQLITE_OK
){
3683 p
->rc
= sqlite3_exec(
3684 db
, "PRAGMA journal_mode=off",0,0,&p
->zErrmsg
);
3688 if( p
->rc
==SQLITE_OK
){
3689 rbuSetupOal(p
, pState
);
3692 }else if( p
->eStage
==RBU_STAGE_MOVE
){
3694 }else if( p
->eStage
==RBU_STAGE_CKPT
){
3695 rbuSetupCheckpoint(p
, pState
);
3696 }else if( p
->eStage
==RBU_STAGE_DONE
){
3697 p
->rc
= SQLITE_DONE
;
3699 p
->rc
= SQLITE_CORRUPT
;
3703 rbuFreeState(pState
);
3710 ** Allocate and return an RBU handle with all fields zeroed except for the
3711 ** error code, which is set to SQLITE_MISUSE.
3713 static sqlite3rbu
*rbuMisuseError(void){
3715 pRet
= sqlite3_malloc64(sizeof(sqlite3rbu
));
3717 memset(pRet
, 0, sizeof(sqlite3rbu
));
3718 pRet
->rc
= SQLITE_MISUSE
;
3724 ** Open and return a new RBU handle.
3726 sqlite3rbu
*sqlite3rbu_open(
3727 const char *zTarget
,
3731 if( zTarget
==0 || zRbu
==0 ){ return rbuMisuseError(); }
3732 /* TODO: Check that zTarget and zRbu are non-NULL */
3733 return openRbuHandle(zTarget
, zRbu
, zState
);
3737 ** Open a handle to begin or resume an RBU VACUUM operation.
3739 sqlite3rbu
*sqlite3rbu_vacuum(
3740 const char *zTarget
,
3743 if( zTarget
==0 ){ return rbuMisuseError(); }
3744 /* TODO: Check that both arguments are non-NULL */
3745 return openRbuHandle(0, zTarget
, zState
);
3749 ** Return the database handle used by pRbu.
3751 sqlite3
*sqlite3rbu_db(sqlite3rbu
*pRbu
, int bRbu
){
3754 db
= (bRbu
? pRbu
->dbRbu
: pRbu
->dbMain
);
3761 ** If the error code currently stored in the RBU handle is SQLITE_CONSTRAINT,
3762 ** then edit any error message string so as to remove all occurrences of
3763 ** the pattern "rbu_imp_[0-9]*".
3765 static void rbuEditErrmsg(sqlite3rbu
*p
){
3766 if( p
->rc
==SQLITE_CONSTRAINT
&& p
->zErrmsg
){
3768 size_t nErrmsg
= strlen(p
->zErrmsg
);
3769 for(i
=0; i
<(nErrmsg
-8); i
++){
3770 if( memcmp(&p
->zErrmsg
[i
], "rbu_imp_", 8)==0 ){
3772 while( p
->zErrmsg
[i
+nDel
]>='0' && p
->zErrmsg
[i
+nDel
]<='9' ) nDel
++;
3773 memmove(&p
->zErrmsg
[i
], &p
->zErrmsg
[i
+nDel
], nErrmsg
+ 1 - i
- nDel
);
3781 ** Close the RBU handle.
3783 int sqlite3rbu_close(sqlite3rbu
*p
, char **pzErrmsg
){
3787 /* Commit the transaction to the *-oal file. */
3788 if( p
->rc
==SQLITE_OK
&& p
->eStage
==RBU_STAGE_OAL
){
3789 p
->rc
= sqlite3_exec(p
->dbMain
, "COMMIT", 0, 0, &p
->zErrmsg
);
3792 /* Sync the db file if currently doing an incremental checkpoint */
3793 if( p
->rc
==SQLITE_OK
&& p
->eStage
==RBU_STAGE_CKPT
){
3794 sqlite3_file
*pDb
= p
->pTargetFd
->pReal
;
3795 p
->rc
= pDb
->pMethods
->xSync(pDb
, SQLITE_SYNC_NORMAL
);
3798 rbuSaveState(p
, p
->eStage
);
3800 if( p
->rc
==SQLITE_OK
&& p
->eStage
==RBU_STAGE_OAL
){
3801 p
->rc
= sqlite3_exec(p
->dbRbu
, "COMMIT", 0, 0, &p
->zErrmsg
);
3804 /* Close any open statement handles. */
3805 rbuObjIterFinalize(&p
->objiter
);
3807 /* If this is an RBU vacuum handle and the vacuum has either finished
3808 ** successfully or encountered an error, delete the contents of the
3809 ** state table. This causes the next call to sqlite3rbu_vacuum()
3810 ** specifying the current target and state databases to start a new
3811 ** vacuum from scratch. */
3812 if( rbuIsVacuum(p
) && p
->rc
!=SQLITE_OK
&& p
->dbRbu
){
3813 int rc2
= sqlite3_exec(p
->dbRbu
, "DELETE FROM stat.rbu_state", 0, 0, 0);
3814 if( p
->rc
==SQLITE_DONE
&& rc2
!=SQLITE_OK
) p
->rc
= rc2
;
3817 /* Close the open database handle and VFS object. */
3818 sqlite3_close(p
->dbRbu
);
3819 sqlite3_close(p
->dbMain
);
3820 assert( p
->szTemp
==0 );
3822 sqlite3_free(p
->aBuf
);
3823 sqlite3_free(p
->aFrame
);
3828 *pzErrmsg
= p
->zErrmsg
;
3830 sqlite3_free(p
->zErrmsg
);
3832 sqlite3_free(p
->zState
);
3842 ** Return the total number of key-value operations (inserts, deletes or
3843 ** updates) that have been performed on the target database since the
3844 ** current RBU update was started.
3846 sqlite3_int64
sqlite3rbu_progress(sqlite3rbu
*pRbu
){
3847 return pRbu
->nProgress
;
3851 ** Return permyriadage progress indications for the two main stages of
3854 void sqlite3rbu_bp_progress(sqlite3rbu
*p
, int *pnOne
, int *pnTwo
){
3855 const int MAX_PROGRESS
= 10000;
3856 switch( p
->eStage
){
3858 if( p
->nPhaseOneStep
>0 ){
3859 *pnOne
= (int)(MAX_PROGRESS
* (i64
)p
->nProgress
/(i64
)p
->nPhaseOneStep
);
3866 case RBU_STAGE_MOVE
:
3867 *pnOne
= MAX_PROGRESS
;
3871 case RBU_STAGE_CKPT
:
3872 *pnOne
= MAX_PROGRESS
;
3873 *pnTwo
= (int)(MAX_PROGRESS
* (i64
)p
->nStep
/ (i64
)p
->nFrame
);
3876 case RBU_STAGE_DONE
:
3877 *pnOne
= MAX_PROGRESS
;
3878 *pnTwo
= MAX_PROGRESS
;
3887 ** Return the current state of the RBU vacuum or update operation.
3889 int sqlite3rbu_state(sqlite3rbu
*p
){
3891 0, SQLITE_RBU_STATE_OAL
, SQLITE_RBU_STATE_MOVE
,
3892 0, SQLITE_RBU_STATE_CHECKPOINT
, SQLITE_RBU_STATE_DONE
3895 assert( RBU_STAGE_OAL
==1 );
3896 assert( RBU_STAGE_MOVE
==2 );
3897 assert( RBU_STAGE_CKPT
==4 );
3898 assert( RBU_STAGE_DONE
==5 );
3899 assert( aRes
[RBU_STAGE_OAL
]==SQLITE_RBU_STATE_OAL
);
3900 assert( aRes
[RBU_STAGE_MOVE
]==SQLITE_RBU_STATE_MOVE
);
3901 assert( aRes
[RBU_STAGE_CKPT
]==SQLITE_RBU_STATE_CHECKPOINT
);
3902 assert( aRes
[RBU_STAGE_DONE
]==SQLITE_RBU_STATE_DONE
);
3904 if( p
->rc
!=SQLITE_OK
&& p
->rc
!=SQLITE_DONE
){
3905 return SQLITE_RBU_STATE_ERROR
;
3907 assert( p
->rc
!=SQLITE_DONE
|| p
->eStage
==RBU_STAGE_DONE
);
3908 assert( p
->eStage
==RBU_STAGE_OAL
3909 || p
->eStage
==RBU_STAGE_MOVE
3910 || p
->eStage
==RBU_STAGE_CKPT
3911 || p
->eStage
==RBU_STAGE_DONE
3913 return aRes
[p
->eStage
];
3917 int sqlite3rbu_savestate(sqlite3rbu
*p
){
3919 if( rc
==SQLITE_DONE
) return SQLITE_OK
;
3921 assert( p
->eStage
>=RBU_STAGE_OAL
&& p
->eStage
<=RBU_STAGE_DONE
);
3922 if( p
->eStage
==RBU_STAGE_OAL
){
3923 assert( rc
!=SQLITE_DONE
);
3924 if( rc
==SQLITE_OK
) rc
= sqlite3_exec(p
->dbMain
, "COMMIT", 0, 0, 0);
3927 /* Sync the db file */
3928 if( rc
==SQLITE_OK
&& p
->eStage
==RBU_STAGE_CKPT
){
3929 sqlite3_file
*pDb
= p
->pTargetFd
->pReal
;
3930 rc
= pDb
->pMethods
->xSync(pDb
, SQLITE_SYNC_NORMAL
);
3934 rbuSaveState(p
, p
->eStage
);
3937 if( p
->eStage
==RBU_STAGE_OAL
){
3938 assert( rc
!=SQLITE_DONE
);
3939 if( rc
==SQLITE_OK
) rc
= sqlite3_exec(p
->dbRbu
, "COMMIT", 0, 0, 0);
3940 if( rc
==SQLITE_OK
) rc
= sqlite3_exec(p
->dbRbu
, "BEGIN IMMEDIATE", 0, 0, 0);
3941 if( rc
==SQLITE_OK
) rc
= sqlite3_exec(p
->dbMain
, "BEGIN IMMEDIATE", 0, 0,0);
3948 /**************************************************************************
3949 ** Beginning of RBU VFS shim methods. The VFS shim modifies the behaviour
3950 ** of a standard VFS in the following ways:
3952 ** 1. Whenever the first page of a main database file is read or
3953 ** written, the value of the change-counter cookie is stored in
3954 ** rbu_file.iCookie. Similarly, the value of the "write-version"
3955 ** database header field is stored in rbu_file.iWriteVer. This ensures
3956 ** that the values are always trustworthy within an open transaction.
3958 ** 2. Whenever an SQLITE_OPEN_WAL file is opened, the (rbu_file.pWalFd)
3959 ** member variable of the associated database file descriptor is set
3960 ** to point to the new file. A mutex protected linked list of all main
3961 ** db fds opened using a particular RBU VFS is maintained at
3962 ** rbu_vfs.pMain to facilitate this.
3964 ** 3. Using a new file-control "SQLITE_FCNTL_RBU", a main db rbu_file
3965 ** object can be marked as the target database of an RBU update. This
3966 ** turns on the following extra special behaviour:
3968 ** 3a. If xAccess() is called to check if there exists a *-wal file
3969 ** associated with an RBU target database currently in RBU_STAGE_OAL
3970 ** stage (preparing the *-oal file), the following special handling
3973 ** * if the *-wal file does exist, return SQLITE_CANTOPEN. An RBU
3974 ** target database may not be in wal mode already.
3976 ** * if the *-wal file does not exist, set the output parameter to
3977 ** non-zero (to tell SQLite that it does exist) anyway.
3979 ** Then, when xOpen() is called to open the *-wal file associated with
3980 ** the RBU target in RBU_STAGE_OAL stage, instead of opening the *-wal
3981 ** file, the rbu vfs opens the corresponding *-oal file instead.
3983 ** 3b. The *-shm pages returned by xShmMap() for a target db file in
3984 ** RBU_STAGE_OAL mode are actually stored in heap memory. This is to
3985 ** avoid creating a *-shm file on disk. Additionally, xShmLock() calls
3986 ** are no-ops on target database files in RBU_STAGE_OAL mode. This is
3987 ** because assert() statements in some VFS implementations fail if
3988 ** xShmLock() is called before xShmMap().
3990 ** 3c. If an EXCLUSIVE lock is attempted on a target database file in any
3991 ** mode except RBU_STAGE_DONE (all work completed and checkpointed), it
3992 ** fails with an SQLITE_BUSY error. This is to stop RBU connections
3993 ** from automatically checkpointing a *-wal (or *-oal) file from within
3996 ** 3d. In RBU_STAGE_CAPTURE mode, all xRead() calls on the wal file, and
3997 ** all xWrite() calls on the target database file perform no IO.
3998 ** Instead the frame and page numbers that would be read and written
3999 ** are recorded. Additionally, successful attempts to obtain exclusive
4000 ** xShmLock() WRITER, CHECKPOINTER and READ0 locks on the target
4001 ** database file are recorded. xShmLock() calls to unlock the same
4002 ** locks are no-ops (so that once obtained, these locks are never
4003 ** relinquished). Finally, calls to xSync() on the target database
4004 ** file fail with SQLITE_INTERNAL errors.
4007 static void rbuUnlockShm(rbu_file
*p
){
4008 assert( p
->openFlags
& SQLITE_OPEN_MAIN_DB
);
4010 int (*xShmLock
)(sqlite3_file
*,int,int,int) = p
->pReal
->pMethods
->xShmLock
;
4012 for(i
=0; i
<SQLITE_SHM_NLOCK
;i
++){
4013 if( (1<<i
) & p
->pRbu
->mLock
){
4014 xShmLock(p
->pReal
, i
, 1, SQLITE_SHM_UNLOCK
|SQLITE_SHM_EXCLUSIVE
);
4023 static int rbuUpdateTempSize(rbu_file
*pFd
, sqlite3_int64 nNew
){
4024 sqlite3rbu
*pRbu
= pFd
->pRbu
;
4025 i64 nDiff
= nNew
- pFd
->sz
;
4026 pRbu
->szTemp
+= nDiff
;
4028 assert( pRbu
->szTemp
>=0 );
4029 if( pRbu
->szTempLimit
&& pRbu
->szTemp
>pRbu
->szTempLimit
) return SQLITE_FULL
;
4034 ** Close an rbu file.
4036 static int rbuVfsClose(sqlite3_file
*pFile
){
4037 rbu_file
*p
= (rbu_file
*)pFile
;
4041 /* Free the contents of the apShm[] array. And the array itself. */
4042 for(i
=0; i
<p
->nShm
; i
++){
4043 sqlite3_free(p
->apShm
[i
]);
4045 sqlite3_free(p
->apShm
);
4047 sqlite3_free(p
->zDel
);
4049 if( p
->openFlags
& SQLITE_OPEN_MAIN_DB
){
4051 sqlite3_mutex_enter(p
->pRbuVfs
->mutex
);
4052 for(pp
=&p
->pRbuVfs
->pMain
; *pp
!=p
; pp
=&((*pp
)->pMainNext
));
4054 sqlite3_mutex_leave(p
->pRbuVfs
->mutex
);
4056 p
->pReal
->pMethods
->xShmUnmap(p
->pReal
, 0);
4058 else if( (p
->openFlags
& SQLITE_OPEN_DELETEONCLOSE
) && p
->pRbu
){
4059 rbuUpdateTempSize(p
, 0);
4062 /* Close the underlying file handle */
4063 rc
= p
->pReal
->pMethods
->xClose(p
->pReal
);
4069 ** Read and return an unsigned 32-bit big-endian integer from the buffer
4070 ** passed as the only argument.
4072 static u32
rbuGetU32(u8
*aBuf
){
4073 return ((u32
)aBuf
[0] << 24)
4074 + ((u32
)aBuf
[1] << 16)
4075 + ((u32
)aBuf
[2] << 8)
4080 ** Write an unsigned 32-bit value in big-endian format to the supplied
4083 static void rbuPutU32(u8
*aBuf
, u32 iVal
){
4084 aBuf
[0] = (iVal
>> 24) & 0xFF;
4085 aBuf
[1] = (iVal
>> 16) & 0xFF;
4086 aBuf
[2] = (iVal
>> 8) & 0xFF;
4087 aBuf
[3] = (iVal
>> 0) & 0xFF;
4090 static void rbuPutU16(u8
*aBuf
, u16 iVal
){
4091 aBuf
[0] = (iVal
>> 8) & 0xFF;
4092 aBuf
[1] = (iVal
>> 0) & 0xFF;
4096 ** Read data from an rbuVfs-file.
4098 static int rbuVfsRead(
4099 sqlite3_file
*pFile
,
4104 rbu_file
*p
= (rbu_file
*)pFile
;
4105 sqlite3rbu
*pRbu
= p
->pRbu
;
4108 if( pRbu
&& pRbu
->eStage
==RBU_STAGE_CAPTURE
){
4109 assert( p
->openFlags
& SQLITE_OPEN_WAL
);
4110 rc
= rbuCaptureWalRead(p
->pRbu
, iOfst
, iAmt
);
4112 if( pRbu
&& pRbu
->eStage
==RBU_STAGE_OAL
4113 && (p
->openFlags
& SQLITE_OPEN_WAL
)
4114 && iOfst
>=pRbu
->iOalSz
4117 memset(zBuf
, 0, iAmt
);
4119 rc
= p
->pReal
->pMethods
->xRead(p
->pReal
, zBuf
, iAmt
, iOfst
);
4121 /* If this is being called to read the first page of the target
4122 ** database as part of an rbu vacuum operation, synthesize the
4123 ** contents of the first page if it does not yet exist. Otherwise,
4124 ** SQLite will not check for a *-wal file. */
4125 if( pRbu
&& rbuIsVacuum(pRbu
)
4126 && rc
==SQLITE_IOERR_SHORT_READ
&& iOfst
==0
4127 && (p
->openFlags
& SQLITE_OPEN_MAIN_DB
)
4128 && pRbu
->rc
==SQLITE_OK
4130 sqlite3_file
*pFd
= (sqlite3_file
*)pRbu
->pRbuFd
;
4131 rc
= pFd
->pMethods
->xRead(pFd
, zBuf
, iAmt
, iOfst
);
4132 if( rc
==SQLITE_OK
){
4133 u8
*aBuf
= (u8
*)zBuf
;
4134 u32 iRoot
= rbuGetU32(&aBuf
[52]) ? 1 : 0;
4135 rbuPutU32(&aBuf
[52], iRoot
); /* largest root page number */
4136 rbuPutU32(&aBuf
[36], 0); /* number of free pages */
4137 rbuPutU32(&aBuf
[32], 0); /* first page on free list trunk */
4138 rbuPutU32(&aBuf
[28], 1); /* size of db file in pages */
4139 rbuPutU32(&aBuf
[24], pRbu
->pRbuFd
->iCookie
+1); /* Change counter */
4142 memset(&aBuf
[100], 0, iAmt
-100);
4143 rbuPutU16(&aBuf
[105], iAmt
& 0xFFFF);
4150 if( rc
==SQLITE_OK
&& iOfst
==0 && (p
->openFlags
& SQLITE_OPEN_MAIN_DB
) ){
4151 /* These look like magic numbers. But they are stable, as they are part
4152 ** of the definition of the SQLite file format, which may not change. */
4153 u8
*pBuf
= (u8
*)zBuf
;
4154 p
->iCookie
= rbuGetU32(&pBuf
[24]);
4155 p
->iWriteVer
= pBuf
[19];
4162 ** Write data to an rbuVfs-file.
4164 static int rbuVfsWrite(
4165 sqlite3_file
*pFile
,
4170 rbu_file
*p
= (rbu_file
*)pFile
;
4171 sqlite3rbu
*pRbu
= p
->pRbu
;
4174 if( pRbu
&& pRbu
->eStage
==RBU_STAGE_CAPTURE
){
4175 assert( p
->openFlags
& SQLITE_OPEN_MAIN_DB
);
4176 rc
= rbuCaptureDbWrite(p
->pRbu
, iOfst
);
4179 if( pRbu
->eStage
==RBU_STAGE_OAL
4180 && (p
->openFlags
& SQLITE_OPEN_WAL
)
4181 && iOfst
>=pRbu
->iOalSz
4183 pRbu
->iOalSz
= iAmt
+ iOfst
;
4184 }else if( p
->openFlags
& SQLITE_OPEN_DELETEONCLOSE
){
4185 i64 szNew
= iAmt
+iOfst
;
4187 rc
= rbuUpdateTempSize(p
, szNew
);
4188 if( rc
!=SQLITE_OK
) return rc
;
4192 rc
= p
->pReal
->pMethods
->xWrite(p
->pReal
, zBuf
, iAmt
, iOfst
);
4193 if( rc
==SQLITE_OK
&& iOfst
==0 && (p
->openFlags
& SQLITE_OPEN_MAIN_DB
) ){
4194 /* These look like magic numbers. But they are stable, as they are part
4195 ** of the definition of the SQLite file format, which may not change. */
4196 u8
*pBuf
= (u8
*)zBuf
;
4197 p
->iCookie
= rbuGetU32(&pBuf
[24]);
4198 p
->iWriteVer
= pBuf
[19];
4205 ** Truncate an rbuVfs-file.
4207 static int rbuVfsTruncate(sqlite3_file
*pFile
, sqlite_int64 size
){
4208 rbu_file
*p
= (rbu_file
*)pFile
;
4209 if( (p
->openFlags
& SQLITE_OPEN_DELETEONCLOSE
) && p
->pRbu
){
4210 int rc
= rbuUpdateTempSize(p
, size
);
4211 if( rc
!=SQLITE_OK
) return rc
;
4213 return p
->pReal
->pMethods
->xTruncate(p
->pReal
, size
);
4217 ** Sync an rbuVfs-file.
4219 static int rbuVfsSync(sqlite3_file
*pFile
, int flags
){
4220 rbu_file
*p
= (rbu_file
*)pFile
;
4221 if( p
->pRbu
&& p
->pRbu
->eStage
==RBU_STAGE_CAPTURE
){
4222 if( p
->openFlags
& SQLITE_OPEN_MAIN_DB
){
4223 return SQLITE_INTERNAL
;
4227 return p
->pReal
->pMethods
->xSync(p
->pReal
, flags
);
4231 ** Return the current file-size of an rbuVfs-file.
4233 static int rbuVfsFileSize(sqlite3_file
*pFile
, sqlite_int64
*pSize
){
4234 rbu_file
*p
= (rbu_file
*)pFile
;
4236 rc
= p
->pReal
->pMethods
->xFileSize(p
->pReal
, pSize
);
4238 /* If this is an RBU vacuum operation and this is the target database,
4239 ** pretend that it has at least one page. Otherwise, SQLite will not
4240 ** check for the existance of a *-wal file. rbuVfsRead() contains
4241 ** similar logic. */
4242 if( rc
==SQLITE_OK
&& *pSize
==0
4243 && p
->pRbu
&& rbuIsVacuum(p
->pRbu
)
4244 && (p
->openFlags
& SQLITE_OPEN_MAIN_DB
)
4252 ** Lock an rbuVfs-file.
4254 static int rbuVfsLock(sqlite3_file
*pFile
, int eLock
){
4255 rbu_file
*p
= (rbu_file
*)pFile
;
4256 sqlite3rbu
*pRbu
= p
->pRbu
;
4259 assert( p
->openFlags
& (SQLITE_OPEN_MAIN_DB
|SQLITE_OPEN_TEMP_DB
) );
4260 if( eLock
==SQLITE_LOCK_EXCLUSIVE
4261 && (p
->bNolock
|| (pRbu
&& pRbu
->eStage
!=RBU_STAGE_DONE
))
4263 /* Do not allow EXCLUSIVE locks. Preventing SQLite from taking this
4264 ** prevents it from checkpointing the database from sqlite3_close(). */
4267 rc
= p
->pReal
->pMethods
->xLock(p
->pReal
, eLock
);
4274 ** Unlock an rbuVfs-file.
4276 static int rbuVfsUnlock(sqlite3_file
*pFile
, int eLock
){
4277 rbu_file
*p
= (rbu_file
*)pFile
;
4278 return p
->pReal
->pMethods
->xUnlock(p
->pReal
, eLock
);
4282 ** Check if another file-handle holds a RESERVED lock on an rbuVfs-file.
4284 static int rbuVfsCheckReservedLock(sqlite3_file
*pFile
, int *pResOut
){
4285 rbu_file
*p
= (rbu_file
*)pFile
;
4286 return p
->pReal
->pMethods
->xCheckReservedLock(p
->pReal
, pResOut
);
4290 ** File control method. For custom operations on an rbuVfs-file.
4292 static int rbuVfsFileControl(sqlite3_file
*pFile
, int op
, void *pArg
){
4293 rbu_file
*p
= (rbu_file
*)pFile
;
4294 int (*xControl
)(sqlite3_file
*,int,void*) = p
->pReal
->pMethods
->xFileControl
;
4297 assert( p
->openFlags
& (SQLITE_OPEN_MAIN_DB
|SQLITE_OPEN_TEMP_DB
)
4298 || p
->openFlags
& (SQLITE_OPEN_TRANSIENT_DB
|SQLITE_OPEN_TEMP_JOURNAL
)
4300 if( op
==SQLITE_FCNTL_RBU
){
4301 sqlite3rbu
*pRbu
= (sqlite3rbu
*)pArg
;
4303 /* First try to find another RBU vfs lower down in the vfs stack. If
4304 ** one is found, this vfs will operate in pass-through mode. The lower
4305 ** level vfs will do the special RBU handling. */
4306 rc
= xControl(p
->pReal
, op
, pArg
);
4308 if( rc
==SQLITE_NOTFOUND
){
4309 /* Now search for a zipvfs instance lower down in the VFS stack. If
4310 ** one is found, this is an error. */
4312 rc
= xControl(p
->pReal
, SQLITE_FCNTL_ZIPVFS
, &dummy
);
4313 if( rc
==SQLITE_OK
){
4315 pRbu
->zErrmsg
= sqlite3_mprintf("rbu/zipvfs setup error");
4316 }else if( rc
==SQLITE_NOTFOUND
){
4317 pRbu
->pTargetFd
= p
;
4319 if( p
->pWalFd
) p
->pWalFd
->pRbu
= pRbu
;
4325 else if( op
==SQLITE_FCNTL_RBUCNT
){
4326 sqlite3rbu
*pRbu
= (sqlite3rbu
*)pArg
;
4332 rc
= xControl(p
->pReal
, op
, pArg
);
4333 if( rc
==SQLITE_OK
&& op
==SQLITE_FCNTL_VFSNAME
){
4334 rbu_vfs
*pRbuVfs
= p
->pRbuVfs
;
4335 char *zIn
= *(char**)pArg
;
4336 char *zOut
= sqlite3_mprintf("rbu(%s)/%z", pRbuVfs
->base
.zName
, zIn
);
4337 *(char**)pArg
= zOut
;
4338 if( zOut
==0 ) rc
= SQLITE_NOMEM
;
4345 ** Return the sector-size in bytes for an rbuVfs-file.
4347 static int rbuVfsSectorSize(sqlite3_file
*pFile
){
4348 rbu_file
*p
= (rbu_file
*)pFile
;
4349 return p
->pReal
->pMethods
->xSectorSize(p
->pReal
);
4353 ** Return the device characteristic flags supported by an rbuVfs-file.
4355 static int rbuVfsDeviceCharacteristics(sqlite3_file
*pFile
){
4356 rbu_file
*p
= (rbu_file
*)pFile
;
4357 return p
->pReal
->pMethods
->xDeviceCharacteristics(p
->pReal
);
4361 ** Take or release a shared-memory lock.
4363 static int rbuVfsShmLock(sqlite3_file
*pFile
, int ofst
, int n
, int flags
){
4364 rbu_file
*p
= (rbu_file
*)pFile
;
4365 sqlite3rbu
*pRbu
= p
->pRbu
;
4368 #ifdef SQLITE_AMALGAMATION
4369 assert( WAL_CKPT_LOCK
==1 );
4372 assert( p
->openFlags
& (SQLITE_OPEN_MAIN_DB
|SQLITE_OPEN_TEMP_DB
) );
4373 if( pRbu
&& (pRbu
->eStage
==RBU_STAGE_OAL
|| pRbu
->eStage
==RBU_STAGE_MOVE
) ){
4374 /* Magic number 1 is the WAL_CKPT_LOCK lock. Preventing SQLite from
4375 ** taking this lock also prevents any checkpoints from occurring.
4376 ** todo: really, it's not clear why this might occur, as
4377 ** wal_autocheckpoint ought to be turned off. */
4378 if( ofst
==WAL_LOCK_CKPT
&& n
==1 ) rc
= SQLITE_BUSY
;
4381 if( n
==1 && (flags
& SQLITE_SHM_EXCLUSIVE
)
4382 && pRbu
&& pRbu
->eStage
==RBU_STAGE_CAPTURE
4383 && (ofst
==WAL_LOCK_WRITE
|| ofst
==WAL_LOCK_CKPT
|| ofst
==WAL_LOCK_READ0
)
4388 if( bCapture
==0 || 0==(flags
& SQLITE_SHM_UNLOCK
) ){
4389 rc
= p
->pReal
->pMethods
->xShmLock(p
->pReal
, ofst
, n
, flags
);
4390 if( bCapture
&& rc
==SQLITE_OK
){
4391 pRbu
->mLock
|= (1 << ofst
);
4400 ** Obtain a pointer to a mapping of a single 32KiB page of the *-shm file.
4402 static int rbuVfsShmMap(
4403 sqlite3_file
*pFile
,
4409 rbu_file
*p
= (rbu_file
*)pFile
;
4411 int eStage
= (p
->pRbu
? p
->pRbu
->eStage
: 0);
4413 /* If not in RBU_STAGE_OAL, allow this call to pass through. Or, if this
4414 ** rbu is in the RBU_STAGE_OAL state, use heap memory for *-shm space
4415 ** instead of a file on disk. */
4416 assert( p
->openFlags
& (SQLITE_OPEN_MAIN_DB
|SQLITE_OPEN_TEMP_DB
) );
4417 if( eStage
==RBU_STAGE_OAL
|| eStage
==RBU_STAGE_MOVE
){
4418 if( iRegion
<=p
->nShm
){
4419 int nByte
= (iRegion
+1) * sizeof(char*);
4420 char **apNew
= (char**)sqlite3_realloc64(p
->apShm
, nByte
);
4424 memset(&apNew
[p
->nShm
], 0, sizeof(char*) * (1 + iRegion
- p
->nShm
));
4426 p
->nShm
= iRegion
+1;
4430 if( rc
==SQLITE_OK
&& p
->apShm
[iRegion
]==0 ){
4431 char *pNew
= (char*)sqlite3_malloc64(szRegion
);
4435 memset(pNew
, 0, szRegion
);
4436 p
->apShm
[iRegion
] = pNew
;
4440 if( rc
==SQLITE_OK
){
4441 *pp
= p
->apShm
[iRegion
];
4446 assert( p
->apShm
==0 );
4447 rc
= p
->pReal
->pMethods
->xShmMap(p
->pReal
, iRegion
, szRegion
, isWrite
, pp
);
4456 static void rbuVfsShmBarrier(sqlite3_file
*pFile
){
4457 rbu_file
*p
= (rbu_file
*)pFile
;
4458 p
->pReal
->pMethods
->xShmBarrier(p
->pReal
);
4462 ** The xShmUnmap method.
4464 static int rbuVfsShmUnmap(sqlite3_file
*pFile
, int delFlag
){
4465 rbu_file
*p
= (rbu_file
*)pFile
;
4467 int eStage
= (p
->pRbu
? p
->pRbu
->eStage
: 0);
4469 assert( p
->openFlags
& (SQLITE_OPEN_MAIN_DB
|SQLITE_OPEN_TEMP_DB
) );
4470 if( eStage
==RBU_STAGE_OAL
|| eStage
==RBU_STAGE_MOVE
){
4473 /* Release the checkpointer and writer locks */
4475 rc
= p
->pReal
->pMethods
->xShmUnmap(p
->pReal
, delFlag
);
4481 ** Given that zWal points to a buffer containing a wal file name passed to
4482 ** either the xOpen() or xAccess() VFS method, return a pointer to the
4483 ** file-handle opened by the same database connection on the corresponding
4486 static rbu_file
*rbuFindMaindb(rbu_vfs
*pRbuVfs
, const char *zWal
){
4488 sqlite3_mutex_enter(pRbuVfs
->mutex
);
4489 for(pDb
=pRbuVfs
->pMain
; pDb
&& pDb
->zWal
!=zWal
; pDb
=pDb
->pMainNext
){}
4490 sqlite3_mutex_leave(pRbuVfs
->mutex
);
4495 ** A main database named zName has just been opened. The following
4496 ** function returns a pointer to a buffer owned by SQLite that contains
4497 ** the name of the *-wal file this db connection will use. SQLite
4498 ** happens to pass a pointer to this buffer when using xAccess()
4499 ** or xOpen() to operate on the *-wal file.
4501 static const char *rbuMainToWal(const char *zName
, int flags
){
4502 int n
= (int)strlen(zName
);
4503 const char *z
= &zName
[n
];
4504 if( flags
& SQLITE_OPEN_URI
){
4509 if( odd
&& z
[1]==0 ) break;
4522 ** Open an rbu file handle.
4524 static int rbuVfsOpen(
4527 sqlite3_file
*pFile
,
4531 static sqlite3_io_methods rbuvfs_io_methods
= {
4533 rbuVfsClose
, /* xClose */
4534 rbuVfsRead
, /* xRead */
4535 rbuVfsWrite
, /* xWrite */
4536 rbuVfsTruncate
, /* xTruncate */
4537 rbuVfsSync
, /* xSync */
4538 rbuVfsFileSize
, /* xFileSize */
4539 rbuVfsLock
, /* xLock */
4540 rbuVfsUnlock
, /* xUnlock */
4541 rbuVfsCheckReservedLock
, /* xCheckReservedLock */
4542 rbuVfsFileControl
, /* xFileControl */
4543 rbuVfsSectorSize
, /* xSectorSize */
4544 rbuVfsDeviceCharacteristics
, /* xDeviceCharacteristics */
4545 rbuVfsShmMap
, /* xShmMap */
4546 rbuVfsShmLock
, /* xShmLock */
4547 rbuVfsShmBarrier
, /* xShmBarrier */
4548 rbuVfsShmUnmap
, /* xShmUnmap */
4549 0, 0 /* xFetch, xUnfetch */
4551 rbu_vfs
*pRbuVfs
= (rbu_vfs
*)pVfs
;
4552 sqlite3_vfs
*pRealVfs
= pRbuVfs
->pRealVfs
;
4553 rbu_file
*pFd
= (rbu_file
*)pFile
;
4555 const char *zOpen
= zName
;
4558 memset(pFd
, 0, sizeof(rbu_file
));
4559 pFd
->pReal
= (sqlite3_file
*)&pFd
[1];
4560 pFd
->pRbuVfs
= pRbuVfs
;
4561 pFd
->openFlags
= flags
;
4563 if( flags
& SQLITE_OPEN_MAIN_DB
){
4564 /* A main database has just been opened. The following block sets
4565 ** (pFd->zWal) to point to a buffer owned by SQLite that contains
4566 ** the name of the *-wal file this db connection will use. SQLite
4567 ** happens to pass a pointer to this buffer when using xAccess()
4568 ** or xOpen() to operate on the *-wal file. */
4569 pFd
->zWal
= rbuMainToWal(zName
, flags
);
4571 else if( flags
& SQLITE_OPEN_WAL
){
4572 rbu_file
*pDb
= rbuFindMaindb(pRbuVfs
, zName
);
4574 if( pDb
->pRbu
&& pDb
->pRbu
->eStage
==RBU_STAGE_OAL
){
4575 /* This call is to open a *-wal file. Intead, open the *-oal. This
4576 ** code ensures that the string passed to xOpen() is terminated by a
4577 ** pair of '\0' bytes in case the VFS attempts to extract a URI
4578 ** parameter from it. */
4579 const char *zBase
= zName
;
4582 if( rbuIsVacuum(pDb
->pRbu
) ){
4583 zBase
= sqlite3_db_filename(pDb
->pRbu
->dbRbu
, "main");
4584 zBase
= rbuMainToWal(zBase
, SQLITE_OPEN_URI
);
4586 nCopy
= strlen(zBase
);
4587 zCopy
= sqlite3_malloc64(nCopy
+2);
4589 memcpy(zCopy
, zBase
, nCopy
);
4590 zCopy
[nCopy
-3] = 'o';
4591 zCopy
[nCopy
] = '\0';
4592 zCopy
[nCopy
+1] = '\0';
4593 zOpen
= (const char*)(pFd
->zDel
= zCopy
);
4597 pFd
->pRbu
= pDb
->pRbu
;
4603 pFd
->pRbu
= pRbuVfs
->pRbu
;
4606 if( oflags
& SQLITE_OPEN_MAIN_DB
4607 && sqlite3_uri_boolean(zName
, "rbu_memory", 0)
4609 assert( oflags
& SQLITE_OPEN_MAIN_DB
);
4610 oflags
= SQLITE_OPEN_TEMP_DB
| SQLITE_OPEN_READWRITE
| SQLITE_OPEN_CREATE
|
4611 SQLITE_OPEN_EXCLUSIVE
| SQLITE_OPEN_DELETEONCLOSE
;
4615 if( rc
==SQLITE_OK
){
4616 rc
= pRealVfs
->xOpen(pRealVfs
, zOpen
, pFd
->pReal
, oflags
, pOutFlags
);
4618 if( pFd
->pReal
->pMethods
){
4619 /* The xOpen() operation has succeeded. Set the sqlite3_file.pMethods
4620 ** pointer and, if the file is a main database file, link it into the
4621 ** mutex protected linked list of all such files. */
4622 pFile
->pMethods
= &rbuvfs_io_methods
;
4623 if( flags
& SQLITE_OPEN_MAIN_DB
){
4624 sqlite3_mutex_enter(pRbuVfs
->mutex
);
4625 pFd
->pMainNext
= pRbuVfs
->pMain
;
4626 pRbuVfs
->pMain
= pFd
;
4627 sqlite3_mutex_leave(pRbuVfs
->mutex
);
4630 sqlite3_free(pFd
->zDel
);
4637 ** Delete the file located at zPath.
4639 static int rbuVfsDelete(sqlite3_vfs
*pVfs
, const char *zPath
, int dirSync
){
4640 sqlite3_vfs
*pRealVfs
= ((rbu_vfs
*)pVfs
)->pRealVfs
;
4641 return pRealVfs
->xDelete(pRealVfs
, zPath
, dirSync
);
4645 ** Test for access permissions. Return true if the requested permission
4646 ** is available, or false otherwise.
4648 static int rbuVfsAccess(
4654 rbu_vfs
*pRbuVfs
= (rbu_vfs
*)pVfs
;
4655 sqlite3_vfs
*pRealVfs
= pRbuVfs
->pRealVfs
;
4658 rc
= pRealVfs
->xAccess(pRealVfs
, zPath
, flags
, pResOut
);
4660 /* If this call is to check if a *-wal file associated with an RBU target
4661 ** database connection exists, and the RBU update is in RBU_STAGE_OAL,
4662 ** the following special handling is activated:
4664 ** a) if the *-wal file does exist, return SQLITE_CANTOPEN. This
4665 ** ensures that the RBU extension never tries to update a database
4666 ** in wal mode, even if the first page of the database file has
4669 ** b) if the *-wal file does not exist, claim that it does anyway,
4670 ** causing SQLite to call xOpen() to open it. This call will also
4671 ** be intercepted (see the rbuVfsOpen() function) and the *-oal
4672 ** file opened instead.
4674 if( rc
==SQLITE_OK
&& flags
==SQLITE_ACCESS_EXISTS
){
4675 rbu_file
*pDb
= rbuFindMaindb(pRbuVfs
, zPath
);
4676 if( pDb
&& pDb
->pRbu
&& pDb
->pRbu
->eStage
==RBU_STAGE_OAL
){
4678 rc
= SQLITE_CANTOPEN
;
4680 sqlite3_int64 sz
= 0;
4681 rc
= rbuVfsFileSize(&pDb
->base
, &sz
);
4691 ** Populate buffer zOut with the full canonical pathname corresponding
4692 ** to the pathname in zPath. zOut is guaranteed to point to a buffer
4693 ** of at least (DEVSYM_MAX_PATHNAME+1) bytes.
4695 static int rbuVfsFullPathname(
4701 sqlite3_vfs
*pRealVfs
= ((rbu_vfs
*)pVfs
)->pRealVfs
;
4702 return pRealVfs
->xFullPathname(pRealVfs
, zPath
, nOut
, zOut
);
4705 #ifndef SQLITE_OMIT_LOAD_EXTENSION
4707 ** Open the dynamic library located at zPath and return a handle.
4709 static void *rbuVfsDlOpen(sqlite3_vfs
*pVfs
, const char *zPath
){
4710 sqlite3_vfs
*pRealVfs
= ((rbu_vfs
*)pVfs
)->pRealVfs
;
4711 return pRealVfs
->xDlOpen(pRealVfs
, zPath
);
4715 ** Populate the buffer zErrMsg (size nByte bytes) with a human readable
4716 ** utf-8 string describing the most recent error encountered associated
4717 ** with dynamic libraries.
4719 static void rbuVfsDlError(sqlite3_vfs
*pVfs
, int nByte
, char *zErrMsg
){
4720 sqlite3_vfs
*pRealVfs
= ((rbu_vfs
*)pVfs
)->pRealVfs
;
4721 pRealVfs
->xDlError(pRealVfs
, nByte
, zErrMsg
);
4725 ** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
4727 static void (*rbuVfsDlSym(
4732 sqlite3_vfs
*pRealVfs
= ((rbu_vfs
*)pVfs
)->pRealVfs
;
4733 return pRealVfs
->xDlSym(pRealVfs
, pArg
, zSym
);
4737 ** Close the dynamic library handle pHandle.
4739 static void rbuVfsDlClose(sqlite3_vfs
*pVfs
, void *pHandle
){
4740 sqlite3_vfs
*pRealVfs
= ((rbu_vfs
*)pVfs
)->pRealVfs
;
4741 pRealVfs
->xDlClose(pRealVfs
, pHandle
);
4743 #endif /* SQLITE_OMIT_LOAD_EXTENSION */
4746 ** Populate the buffer pointed to by zBufOut with nByte bytes of
4749 static int rbuVfsRandomness(sqlite3_vfs
*pVfs
, int nByte
, char *zBufOut
){
4750 sqlite3_vfs
*pRealVfs
= ((rbu_vfs
*)pVfs
)->pRealVfs
;
4751 return pRealVfs
->xRandomness(pRealVfs
, nByte
, zBufOut
);
4755 ** Sleep for nMicro microseconds. Return the number of microseconds
4758 static int rbuVfsSleep(sqlite3_vfs
*pVfs
, int nMicro
){
4759 sqlite3_vfs
*pRealVfs
= ((rbu_vfs
*)pVfs
)->pRealVfs
;
4760 return pRealVfs
->xSleep(pRealVfs
, nMicro
);
4764 ** Return the current time as a Julian Day number in *pTimeOut.
4766 static int rbuVfsCurrentTime(sqlite3_vfs
*pVfs
, double *pTimeOut
){
4767 sqlite3_vfs
*pRealVfs
= ((rbu_vfs
*)pVfs
)->pRealVfs
;
4768 return pRealVfs
->xCurrentTime(pRealVfs
, pTimeOut
);
4774 static int rbuVfsGetLastError(sqlite3_vfs
*pVfs
, int a
, char *b
){
4779 ** Deregister and destroy an RBU vfs created by an earlier call to
4780 ** sqlite3rbu_create_vfs().
4782 void sqlite3rbu_destroy_vfs(const char *zName
){
4783 sqlite3_vfs
*pVfs
= sqlite3_vfs_find(zName
);
4784 if( pVfs
&& pVfs
->xOpen
==rbuVfsOpen
){
4785 sqlite3_mutex_free(((rbu_vfs
*)pVfs
)->mutex
);
4786 sqlite3_vfs_unregister(pVfs
);
4792 ** Create an RBU VFS named zName that accesses the underlying file-system
4793 ** via existing VFS zParent. The new object is registered as a non-default
4794 ** VFS with SQLite before returning.
4796 int sqlite3rbu_create_vfs(const char *zName
, const char *zParent
){
4798 /* Template for VFS */
4799 static sqlite3_vfs vfs_template
= {
4806 rbuVfsOpen
, /* xOpen */
4807 rbuVfsDelete
, /* xDelete */
4808 rbuVfsAccess
, /* xAccess */
4809 rbuVfsFullPathname
, /* xFullPathname */
4811 #ifndef SQLITE_OMIT_LOAD_EXTENSION
4812 rbuVfsDlOpen
, /* xDlOpen */
4813 rbuVfsDlError
, /* xDlError */
4814 rbuVfsDlSym
, /* xDlSym */
4815 rbuVfsDlClose
, /* xDlClose */
4820 rbuVfsRandomness
, /* xRandomness */
4821 rbuVfsSleep
, /* xSleep */
4822 rbuVfsCurrentTime
, /* xCurrentTime */
4823 rbuVfsGetLastError
, /* xGetLastError */
4824 0, /* xCurrentTimeInt64 (version 2) */
4825 0, 0, 0 /* Unimplemented version 3 methods */
4828 rbu_vfs
*pNew
= 0; /* Newly allocated VFS */
4833 nName
= strlen(zName
);
4834 nByte
= sizeof(rbu_vfs
) + nName
+ 1;
4835 pNew
= (rbu_vfs
*)sqlite3_malloc64(nByte
);
4839 sqlite3_vfs
*pParent
; /* Parent VFS */
4840 memset(pNew
, 0, nByte
);
4841 pParent
= sqlite3_vfs_find(zParent
);
4843 rc
= SQLITE_NOTFOUND
;
4846 memcpy(&pNew
->base
, &vfs_template
, sizeof(sqlite3_vfs
));
4847 pNew
->base
.mxPathname
= pParent
->mxPathname
;
4848 pNew
->base
.szOsFile
= sizeof(rbu_file
) + pParent
->szOsFile
;
4849 pNew
->pRealVfs
= pParent
;
4850 pNew
->base
.zName
= (const char*)(zSpace
= (char*)&pNew
[1]);
4851 memcpy(zSpace
, zName
, nName
);
4853 /* Allocate the mutex and register the new VFS (not as the default) */
4854 pNew
->mutex
= sqlite3_mutex_alloc(SQLITE_MUTEX_RECURSIVE
);
4855 if( pNew
->mutex
==0 ){
4858 rc
= sqlite3_vfs_register(&pNew
->base
, 0);
4862 if( rc
!=SQLITE_OK
){
4863 sqlite3_mutex_free(pNew
->mutex
);
4872 ** Configure the aggregate temp file size limit for this RBU handle.
4874 sqlite3_int64
sqlite3rbu_temp_size_limit(sqlite3rbu
*pRbu
, sqlite3_int64 n
){
4876 pRbu
->szTempLimit
= n
;
4878 return pRbu
->szTempLimit
;
4881 sqlite3_int64
sqlite3rbu_temp_size(sqlite3rbu
*pRbu
){
4882 return pRbu
->szTemp
;
4886 /**************************************************************************/
4888 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RBU) */