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 ** Name of the URI option that causes RBU to take an exclusive lock as
115 ** part of the incremental checkpoint operation.
117 #define RBU_EXCLUSIVE_CHECKPOINT "rbu_exclusive_checkpoint"
121 ** The rbu_state table is used to save the state of a partially applied
122 ** update so that it can be resumed later. The table consists of integer
123 ** keys mapped to values as follows:
126 ** May be set to integer values 1, 2, 4 or 5. As follows:
127 ** 1: the *-rbu file is currently under construction.
128 ** 2: the *-rbu file has been constructed, but not yet moved
129 ** to the *-wal path.
130 ** 4: the checkpoint is underway.
131 ** 5: the rbu update has been checkpointed.
134 ** Only valid if STAGE==1. The target database name of the table
135 ** currently being written.
138 ** Only valid if STAGE==1. The target database name of the index
139 ** currently being written, or NULL if the main table is currently being
143 ** Only valid if STAGE==1. Number of rows already processed for the current
146 ** RBU_STATE_PROGRESS:
147 ** Trbul number of sqlite3rbu_step() calls made so far as part of this
151 ** Valid if STAGE==4. The 64-bit checksum associated with the wal-index
152 ** header created by recovering the *-wal file. This is used to detect
153 ** cases when another client appends frames to the *-wal file in the
154 ** middle of an incremental checkpoint (an incremental checkpoint cannot
155 ** be continued if this happens).
158 ** Valid if STAGE==1. The current change-counter cookie value in the
162 ** Valid if STAGE==1. The size in bytes of the *-oal file.
164 ** RBU_STATE_DATATBL:
165 ** Only valid if STAGE==1. The RBU database name of the table
166 ** currently being read.
168 #define RBU_STATE_STAGE 1
169 #define RBU_STATE_TBL 2
170 #define RBU_STATE_IDX 3
171 #define RBU_STATE_ROW 4
172 #define RBU_STATE_PROGRESS 5
173 #define RBU_STATE_CKPT 6
174 #define RBU_STATE_COOKIE 7
175 #define RBU_STATE_OALSZ 8
176 #define RBU_STATE_PHASEONESTEP 9
177 #define RBU_STATE_DATATBL 10
179 #define RBU_STAGE_OAL 1
180 #define RBU_STAGE_MOVE 2
181 #define RBU_STAGE_CAPTURE 3
182 #define RBU_STAGE_CKPT 4
183 #define RBU_STAGE_DONE 5
186 #define RBU_CREATE_STATE \
187 "CREATE TABLE IF NOT EXISTS %s.rbu_state(k INTEGER PRIMARY KEY, v)"
189 typedef struct RbuFrame RbuFrame
;
190 typedef struct RbuObjIter RbuObjIter
;
191 typedef struct RbuState RbuState
;
192 typedef struct RbuSpan RbuSpan
;
193 typedef struct rbu_vfs rbu_vfs
;
194 typedef struct rbu_file rbu_file
;
195 typedef struct RbuUpdateStmt RbuUpdateStmt
;
197 #if !defined(SQLITE_AMALGAMATION)
198 typedef unsigned int u32
;
199 typedef unsigned short u16
;
200 typedef unsigned char u8
;
201 typedef sqlite3_int64 i64
;
205 ** These values must match the values defined in wal.c for the equivalent
206 ** locks. These are not magic numbers as they are part of the SQLite file
209 #define WAL_LOCK_WRITE 0
210 #define WAL_LOCK_CKPT 1
211 #define WAL_LOCK_READ0 3
213 #define SQLITE_FCNTL_RBUCNT 5149216
216 ** A structure to store values read from the rbu_state table in memory.
231 struct RbuUpdateStmt
{
232 char *zMask
; /* Copy of update mask used with pUpdate */
233 sqlite3_stmt
*pUpdate
; /* Last update statement (or NULL) */
234 RbuUpdateStmt
*pNext
;
243 ** An iterator of this type is used to iterate through all objects in
244 ** the target database that require updating. For each such table, the
245 ** iterator visits, in order:
247 ** * the table itself,
248 ** * each index of the table (zero or more points to visit), and
249 ** * a special "cleanup table" state.
252 ** If the table has no indexes on it, abIndexed is set to NULL. Otherwise,
253 ** it points to an array of flags nTblCol elements in size. The flag is
254 ** set for each column that is either a part of the PK or a part of an
255 ** index. Or clear otherwise.
257 ** If there are one or more partial indexes on the table, all fields of
258 ** this array set set to 1. This is because in that case, the module has
259 ** no way to tell which fields will be required to add and remove entries
260 ** from the partial indexes.
264 sqlite3_stmt
*pTblIter
; /* Iterate through tables */
265 sqlite3_stmt
*pIdxIter
; /* Index iterator */
266 int nTblCol
; /* Size of azTblCol[] array */
267 char **azTblCol
; /* Array of unquoted target column names */
268 char **azTblType
; /* Array of target column types */
269 int *aiSrcOrder
; /* src table col -> target table col */
270 u8
*abTblPk
; /* Array of flags, set on target PK columns */
271 u8
*abNotNull
; /* Array of flags, set on NOT NULL columns */
272 u8
*abIndexed
; /* Array of flags, set on indexed & PK cols */
273 int eType
; /* Table type - an RBU_PK_XXX value */
275 /* Output variables. zTbl==0 implies EOF. */
276 int bCleanup
; /* True in "cleanup" state */
277 const char *zTbl
; /* Name of target db table */
278 const char *zDataTbl
; /* Name of rbu db table (or null) */
279 const char *zIdx
; /* Name of target db index (or null) */
280 int iTnum
; /* Root page of current object */
281 int iPkTnum
; /* If eType==EXTERNAL, root of PK index */
282 int bUnique
; /* Current index is unique */
283 int nIndex
; /* Number of aux. indexes on table zTbl */
285 /* Statements created by rbuObjIterPrepareAll() */
286 int nCol
; /* Number of columns in current object */
287 sqlite3_stmt
*pSelect
; /* Source data */
288 sqlite3_stmt
*pInsert
; /* Statement for INSERT operations */
289 sqlite3_stmt
*pDelete
; /* Statement for DELETE ops */
290 sqlite3_stmt
*pTmpInsert
; /* Insert into rbu_tmp_$zDataTbl */
295 /* Last UPDATE used (for PK b-tree updates only), or NULL. */
296 RbuUpdateStmt
*pRbuUpdate
;
300 ** Values for RbuObjIter.eType
302 ** 0: Table does not exist (error)
303 ** 1: Table has an implicit rowid.
304 ** 2: Table has an explicit IPK column.
305 ** 3: Table has an external PK index.
306 ** 4: Table is WITHOUT ROWID.
307 ** 5: Table is a virtual table.
309 #define RBU_PK_NOTABLE 0
310 #define RBU_PK_NONE 1
312 #define RBU_PK_EXTERNAL 3
313 #define RBU_PK_WITHOUT_ROWID 4
314 #define RBU_PK_VTAB 5
318 ** Within the RBU_STAGE_OAL stage, each call to sqlite3rbu_step() performs
319 ** one of the following operations.
321 #define RBU_INSERT 1 /* Insert on a main table b-tree */
322 #define RBU_DELETE 2 /* Delete a row from a main table b-tree */
323 #define RBU_REPLACE 3 /* Delete and then insert a row */
324 #define RBU_IDX_DELETE 4 /* Delete a row from an aux. index b-tree */
325 #define RBU_IDX_INSERT 5 /* Insert on an aux. index b-tree */
327 #define RBU_UPDATE 6 /* Update a row in a main table b-tree */
330 ** A single step of an incremental checkpoint - frame iWalFrame of the wal
331 ** file should be copied to page iDbPage of the database file.
342 ** If the RBU database contains an rbu_count table, this value is set to
343 ** a running estimate of the number of b-tree operations required to
344 ** finish populating the *-oal file. This allows the sqlite3_bp_progress()
345 ** API to calculate the permyriadage progress of populating the *-oal file
346 ** using the formula:
348 ** permyriadage = (10000 * nProgress) / nPhaseOneStep
350 ** nPhaseOneStep is initialized to the sum of:
352 ** nRow * (nIndex + 1)
354 ** for all source tables in the RBU database, where nRow is the number
355 ** of rows in the source table and nIndex the number of indexes on the
356 ** corresponding target database table.
358 ** This estimate is accurate if the RBU update consists entirely of
359 ** INSERT operations. However, it is inaccurate if:
361 ** * the RBU update contains any UPDATE operations. If the PK specified
362 ** for an UPDATE operation does not exist in the target table, then
363 ** no b-tree operations are required on index b-trees. Or if the
364 ** specified PK does exist, then (nIndex*2) such operations are
365 ** required (one delete and one insert on each index b-tree).
367 ** * the RBU update contains any DELETE operations for which the specified
368 ** PK does not exist. In this case no operations are required on index
371 ** * the RBU update contains REPLACE operations. These are similar to
372 ** UPDATE operations.
374 ** nPhaseOneStep is updated to account for the conditions above during the
375 ** first pass of each source table. The updated nPhaseOneStep value is
376 ** stored in the rbu_state table if the RBU update is suspended.
379 int eStage
; /* Value of RBU_STATE_STAGE field */
380 sqlite3
*dbMain
; /* target database handle */
381 sqlite3
*dbRbu
; /* rbu database handle */
382 char *zTarget
; /* Path to target db */
383 char *zRbu
; /* Path to rbu db */
384 char *zState
; /* Path to state db (or NULL if zRbu) */
385 char zStateDb
[5]; /* Db name for state ("stat" or "main") */
386 int rc
; /* Value returned by last rbu_step() call */
387 char *zErrmsg
; /* Error message if rc!=SQLITE_OK */
388 int nStep
; /* Rows processed for current object */
389 int nProgress
; /* Rows processed for all objects */
390 RbuObjIter objiter
; /* Iterator for skipping through tbl/idx */
391 const char *zVfsName
; /* Name of automatically created rbu vfs */
392 rbu_file
*pTargetFd
; /* File handle open on target db */
393 int nPagePerSector
; /* Pages per sector for pTargetFd */
397 int (*xRename
)(void*, const char*, const char*);
399 /* The following state variables are used as part of the incremental
400 ** checkpoint stage (eStage==RBU_STAGE_CKPT). See comments surrounding
401 ** function rbuSetupCheckpoint() for details. */
402 u32 iMaxFrame
; /* Largest iWalFrame value in aFrame[] */
404 int nFrame
; /* Entries in aFrame[] array */
405 int nFrameAlloc
; /* Allocated size of aFrame[] array */
410 i64 szTemp
; /* Current size of all temp files in use */
411 i64 szTempLimit
; /* Total size limit for temp files */
413 /* Used in RBU vacuum mode only */
414 int nRbu
; /* Number of RBU VFS in the stack */
415 rbu_file
*pRbuFd
; /* Fd for main db of dbRbu */
419 ** An rbu VFS is implemented using an instance of this structure.
421 ** Variable pRbu is only non-NULL for automatically created RBU VFS objects.
422 ** It is NULL for RBU VFS objects created explicitly using
423 ** sqlite3rbu_create_vfs(). It is used to track the total amount of temp
424 ** space used by the RBU handle.
427 sqlite3_vfs base
; /* rbu VFS shim methods */
428 sqlite3_vfs
*pRealVfs
; /* Underlying VFS */
429 sqlite3_mutex
*mutex
; /* Mutex to protect pMain */
430 sqlite3rbu
*pRbu
; /* Owner RBU object */
431 rbu_file
*pMain
; /* List of main db files */
432 rbu_file
*pMainRbu
; /* List of main db files with pRbu!=0 */
436 ** Each file opened by an rbu VFS is represented by an instance of
437 ** the following structure.
439 ** If this is a temporary file (pRbu!=0 && flags&DELETE_ON_CLOSE), variable
440 ** "sz" is set to the current size of the database file.
443 sqlite3_file base
; /* sqlite3_file methods */
444 sqlite3_file
*pReal
; /* Underlying file handle */
445 rbu_vfs
*pRbuVfs
; /* Pointer to the rbu_vfs object */
446 sqlite3rbu
*pRbu
; /* Pointer to rbu object (rbu target only) */
447 i64 sz
; /* Size of file in bytes (temp only) */
449 int openFlags
; /* Flags this file was opened with */
450 u32 iCookie
; /* Cookie value for main db files */
451 u8 iWriteVer
; /* "write-version" value for main db files */
452 u8 bNolock
; /* True to fail EXCLUSIVE locks */
454 int nShm
; /* Number of entries in apShm[] array */
455 char **apShm
; /* Array of mmap'd *-shm regions */
456 char *zDel
; /* Delete this when closing file */
458 const char *zWal
; /* Wal filename for this main db file */
459 rbu_file
*pWalFd
; /* Wal file descriptor for this main db */
460 rbu_file
*pMainNext
; /* Next MAIN_DB file */
461 rbu_file
*pMainRbuNext
; /* Next MAIN_DB file with pRbu!=0 */
465 ** True for an RBU vacuum handle, or false otherwise.
467 #define rbuIsVacuum(p) ((p)->zTarget==0)
470 /*************************************************************************
471 ** The following three functions, found below:
474 ** rbuDeltaChecksum()
477 ** are lifted from the fossil source code (http://fossil-scm.org). They
478 ** are used to implement the scalar SQL function rbu_fossil_delta().
482 ** Read bytes from *pz and convert them into a positive integer. When
483 ** finished, leave *pz pointing to the first character past the end of
484 ** the integer. The *pLen parameter holds the length of the string
485 ** in *pz and is decremented once for each character in the integer.
487 static unsigned int rbuDeltaGetInt(const char **pz
, int *pLen
){
488 static const signed char zValue
[] = {
489 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
490 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
491 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
492 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
493 -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
494 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, 36,
495 -1, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
496 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, -1, -1, -1, 63, -1,
500 unsigned char *z
= (unsigned char*)*pz
;
501 unsigned char *zStart
= z
;
502 while( (c
= zValue
[0x7f&*(z
++)])>=0 ){
511 #if RBU_ENABLE_DELTA_CKSUM
513 ** Compute a 32-bit checksum on the N-byte buffer. Return the result.
515 static unsigned int rbuDeltaChecksum(const char *zIn
, size_t N
){
516 const unsigned char *z
= (const unsigned char *)zIn
;
522 sum0
+= ((unsigned)z
[0] + z
[4] + z
[8] + z
[12]);
523 sum1
+= ((unsigned)z
[1] + z
[5] + z
[9] + z
[13]);
524 sum2
+= ((unsigned)z
[2] + z
[6] + z
[10]+ z
[14]);
525 sum3
+= ((unsigned)z
[3] + z
[7] + z
[11]+ z
[15]);
537 sum3
+= (sum2
<< 8) + (sum1
<< 16) + (sum0
<< 24);
539 case 3: sum3
+= (z
[2] << 8);
540 case 2: sum3
+= (z
[1] << 16);
541 case 1: sum3
+= (z
[0] << 24);
551 ** The output buffer should be big enough to hold the whole output
552 ** file and a NUL terminator at the end. The delta_output_size()
553 ** routine will determine this size for you.
555 ** The delta string should be null-terminated. But the delta string
556 ** may contain embedded NUL characters (if the input and output are
557 ** binary files) so we also have to pass in the length of the delta in
558 ** the lenDelta parameter.
560 ** This function returns the size of the output file in bytes (excluding
561 ** the final NUL terminator character). Except, if the delta string is
562 ** malformed or intended for use with a source file other than zSrc,
563 ** then this routine returns -1.
565 ** Refer to the delta_create() documentation above for a description
566 ** of the delta file format.
568 static int rbuDeltaApply(
569 const char *zSrc
, /* The source or pattern file */
570 int lenSrc
, /* Length of the source file */
571 const char *zDelta
, /* Delta to apply to the pattern */
572 int lenDelta
, /* Length of the delta */
573 char *zOut
/* Write the output into this preallocated buffer */
576 unsigned int total
= 0;
577 #if RBU_ENABLE_DELTA_CKSUM
578 char *zOrigOut
= zOut
;
581 limit
= rbuDeltaGetInt(&zDelta
, &lenDelta
);
583 /* ERROR: size integer not terminated by "\n" */
586 zDelta
++; lenDelta
--;
587 while( *zDelta
&& lenDelta
>0 ){
588 unsigned int cnt
, ofst
;
589 cnt
= rbuDeltaGetInt(&zDelta
, &lenDelta
);
592 zDelta
++; lenDelta
--;
593 ofst
= rbuDeltaGetInt(&zDelta
, &lenDelta
);
594 if( lenDelta
>0 && zDelta
[0]!=',' ){
595 /* ERROR: copy command not terminated by ',' */
598 zDelta
++; lenDelta
--;
601 /* ERROR: copy exceeds output file size */
604 if( (int)(ofst
+cnt
) > lenSrc
){
605 /* ERROR: copy extends past end of input */
608 memcpy(zOut
, &zSrc
[ofst
], cnt
);
613 zDelta
++; lenDelta
--;
616 /* ERROR: insert command gives an output larger than predicted */
619 if( (int)cnt
>lenDelta
){
620 /* ERROR: insert count exceeds size of delta */
623 memcpy(zOut
, zDelta
, cnt
);
630 zDelta
++; lenDelta
--;
632 #if RBU_ENABLE_DELTA_CKSUM
633 if( cnt
!=rbuDeltaChecksum(zOrigOut
, total
) ){
634 /* ERROR: bad checksum */
639 /* ERROR: generated size does not match predicted size */
645 /* ERROR: unknown delta operator */
650 /* ERROR: unterminated delta */
654 static int rbuDeltaOutputSize(const char *zDelta
, int lenDelta
){
656 size
= rbuDeltaGetInt(&zDelta
, &lenDelta
);
658 /* ERROR: size integer not terminated by "\n" */
665 ** End of code taken from fossil.
666 *************************************************************************/
669 ** Implementation of SQL scalar function rbu_fossil_delta().
671 ** This function applies a fossil delta patch to a blob. Exactly two
672 ** arguments must be passed to this function. The first is the blob to
673 ** patch and the second the patch to apply. If no error occurs, this
674 ** function returns the patched blob.
676 static void rbuFossilDeltaFunc(
677 sqlite3_context
*context
,
692 nOrig
= sqlite3_value_bytes(argv
[0]);
693 aOrig
= (const char*)sqlite3_value_blob(argv
[0]);
694 nDelta
= sqlite3_value_bytes(argv
[1]);
695 aDelta
= (const char*)sqlite3_value_blob(argv
[1]);
697 /* Figure out the size of the output */
698 nOut
= rbuDeltaOutputSize(aDelta
, nDelta
);
700 sqlite3_result_error(context
, "corrupt fossil delta", -1);
704 aOut
= sqlite3_malloc(nOut
+1);
706 sqlite3_result_error_nomem(context
);
708 nOut2
= rbuDeltaApply(aOrig
, nOrig
, aDelta
, nDelta
, aOut
);
711 sqlite3_result_error(context
, "corrupt fossil delta", -1);
713 sqlite3_result_blob(context
, aOut
, nOut
, sqlite3_free
);
720 ** Prepare the SQL statement in buffer zSql against database handle db.
721 ** If successful, set *ppStmt to point to the new statement and return
724 ** Otherwise, if an error does occur, set *ppStmt to NULL and return
725 ** an SQLite error code. Additionally, set output variable *pzErrmsg to
726 ** point to a buffer containing an error message. It is the responsibility
727 ** of the caller to (eventually) free this buffer using sqlite3_free().
729 static int prepareAndCollectError(
731 sqlite3_stmt
**ppStmt
,
735 int rc
= sqlite3_prepare_v2(db
, zSql
, -1, ppStmt
, 0);
737 *pzErrmsg
= sqlite3_mprintf("%s", sqlite3_errmsg(db
));
744 ** Reset the SQL statement passed as the first argument. Return a copy
745 ** of the value returned by sqlite3_reset().
747 ** If an error has occurred, then set *pzErrmsg to point to a buffer
748 ** containing an error message. It is the responsibility of the caller
749 ** to eventually free this buffer using sqlite3_free().
751 static int resetAndCollectError(sqlite3_stmt
*pStmt
, char **pzErrmsg
){
752 int rc
= sqlite3_reset(pStmt
);
754 *pzErrmsg
= sqlite3_mprintf("%s", sqlite3_errmsg(sqlite3_db_handle(pStmt
)));
760 ** Unless it is NULL, argument zSql points to a buffer allocated using
761 ** sqlite3_malloc containing an SQL statement. This function prepares the SQL
762 ** statement against database db and frees the buffer. If statement
763 ** compilation is successful, *ppStmt is set to point to the new statement
764 ** handle and SQLITE_OK is returned.
766 ** Otherwise, if an error occurs, *ppStmt is set to NULL and an error code
767 ** returned. In this case, *pzErrmsg may also be set to point to an error
768 ** message. It is the responsibility of the caller to free this error message
769 ** buffer using sqlite3_free().
771 ** If argument zSql is NULL, this function assumes that an OOM has occurred.
772 ** In this case SQLITE_NOMEM is returned and *ppStmt set to NULL.
774 static int prepareFreeAndCollectError(
776 sqlite3_stmt
**ppStmt
,
781 assert( *pzErrmsg
==0 );
786 rc
= prepareAndCollectError(db
, ppStmt
, pzErrmsg
, zSql
);
793 ** Free the RbuObjIter.azTblCol[] and RbuObjIter.abTblPk[] arrays allocated
794 ** by an earlier call to rbuObjIterCacheTableInfo().
796 static void rbuObjIterFreeCols(RbuObjIter
*pIter
){
798 for(i
=0; i
<pIter
->nTblCol
; i
++){
799 sqlite3_free(pIter
->azTblCol
[i
]);
800 sqlite3_free(pIter
->azTblType
[i
]);
802 sqlite3_free(pIter
->azTblCol
);
804 pIter
->azTblType
= 0;
805 pIter
->aiSrcOrder
= 0;
807 pIter
->abNotNull
= 0;
809 pIter
->eType
= 0; /* Invalid value */
813 ** Finalize all statements and free all allocations that are specific to
814 ** the current object (table/index pair).
816 static void rbuObjIterClearStatements(RbuObjIter
*pIter
){
819 sqlite3_finalize(pIter
->pSelect
);
820 sqlite3_finalize(pIter
->pInsert
);
821 sqlite3_finalize(pIter
->pDelete
);
822 sqlite3_finalize(pIter
->pTmpInsert
);
823 pUp
= pIter
->pRbuUpdate
;
825 RbuUpdateStmt
*pTmp
= pUp
->pNext
;
826 sqlite3_finalize(pUp
->pUpdate
);
830 sqlite3_free(pIter
->aIdxCol
);
831 sqlite3_free(pIter
->zIdxSql
);
836 pIter
->pRbuUpdate
= 0;
837 pIter
->pTmpInsert
= 0;
845 ** Clean up any resources allocated as part of the iterator object passed
846 ** as the only argument.
848 static void rbuObjIterFinalize(RbuObjIter
*pIter
){
849 rbuObjIterClearStatements(pIter
);
850 sqlite3_finalize(pIter
->pTblIter
);
851 sqlite3_finalize(pIter
->pIdxIter
);
852 rbuObjIterFreeCols(pIter
);
853 memset(pIter
, 0, sizeof(RbuObjIter
));
857 ** Advance the iterator to the next position.
859 ** If no error occurs, SQLITE_OK is returned and the iterator is left
860 ** pointing to the next entry. Otherwise, an error code and message is
861 ** left in the RBU handle passed as the first argument. A copy of the
862 ** error code is returned.
864 static int rbuObjIterNext(sqlite3rbu
*p
, RbuObjIter
*pIter
){
868 /* Free any SQLite statements used while processing the previous object */
869 rbuObjIterClearStatements(pIter
);
870 if( pIter
->zIdx
==0 ){
871 rc
= sqlite3_exec(p
->dbMain
,
872 "DROP TRIGGER IF EXISTS temp.rbu_insert_tr;"
873 "DROP TRIGGER IF EXISTS temp.rbu_update1_tr;"
874 "DROP TRIGGER IF EXISTS temp.rbu_update2_tr;"
875 "DROP TRIGGER IF EXISTS temp.rbu_delete_tr;"
881 if( pIter
->bCleanup
){
882 rbuObjIterFreeCols(pIter
);
884 rc
= sqlite3_step(pIter
->pTblIter
);
885 if( rc
!=SQLITE_ROW
){
886 rc
= resetAndCollectError(pIter
->pTblIter
, &p
->zErrmsg
);
889 pIter
->zTbl
= (const char*)sqlite3_column_text(pIter
->pTblIter
, 0);
890 pIter
->zDataTbl
= (const char*)sqlite3_column_text(pIter
->pTblIter
,1);
891 rc
= (pIter
->zDataTbl
&& pIter
->zTbl
) ? SQLITE_OK
: SQLITE_NOMEM
;
894 if( pIter
->zIdx
==0 ){
895 sqlite3_stmt
*pIdx
= pIter
->pIdxIter
;
896 rc
= sqlite3_bind_text(pIdx
, 1, pIter
->zTbl
, -1, SQLITE_STATIC
);
899 rc
= sqlite3_step(pIter
->pIdxIter
);
900 if( rc
!=SQLITE_ROW
){
901 rc
= resetAndCollectError(pIter
->pIdxIter
, &p
->zErrmsg
);
905 pIter
->zIdx
= (const char*)sqlite3_column_text(pIter
->pIdxIter
, 0);
906 pIter
->iTnum
= sqlite3_column_int(pIter
->pIdxIter
, 1);
907 pIter
->bUnique
= sqlite3_column_int(pIter
->pIdxIter
, 2);
908 rc
= pIter
->zIdx
? SQLITE_OK
: SQLITE_NOMEM
;
916 rbuObjIterFinalize(pIter
);
924 ** The implementation of the rbu_target_name() SQL function. This function
925 ** accepts one or two arguments. The first argument is the name of a table -
926 ** the name of a table in the RBU database. The second, if it is present, is 1
927 ** for a view or 0 for a table.
929 ** For a non-vacuum RBU handle, if the table name matches the pattern:
933 ** where <name> is any sequence of 1 or more characters, <name> is returned.
934 ** Otherwise, if the only argument does not match the above pattern, an SQL
938 ** "data0123_t2" -> "t2"
939 ** "dataAB_t3" -> NULL
941 ** For an rbu vacuum handle, a copy of the first argument is returned if
942 ** the second argument is either missing or 0 (not a view).
944 static void rbuTargetNameFunc(
945 sqlite3_context
*pCtx
,
949 sqlite3rbu
*p
= sqlite3_user_data(pCtx
);
951 assert( argc
==1 || argc
==2 );
953 zIn
= (const char*)sqlite3_value_text(argv
[0]);
955 if( rbuIsVacuum(p
) ){
956 assert( argc
==2 || argc
==1 );
957 if( argc
==1 || 0==sqlite3_value_int(argv
[1]) ){
958 sqlite3_result_text(pCtx
, zIn
, -1, SQLITE_STATIC
);
961 if( strlen(zIn
)>4 && memcmp("data", zIn
, 4)==0 ){
963 for(i
=4; zIn
[i
]>='0' && zIn
[i
]<='9'; i
++);
964 if( zIn
[i
]=='_' && zIn
[i
+1] ){
965 sqlite3_result_text(pCtx
, &zIn
[i
+1], -1, SQLITE_STATIC
);
973 ** Initialize the iterator structure passed as the second argument.
975 ** If no error occurs, SQLITE_OK is returned and the iterator is left
976 ** pointing to the first entry. Otherwise, an error code and message is
977 ** left in the RBU handle passed as the first argument. A copy of the
978 ** error code is returned.
980 static int rbuObjIterFirst(sqlite3rbu
*p
, RbuObjIter
*pIter
){
982 memset(pIter
, 0, sizeof(RbuObjIter
));
984 rc
= prepareFreeAndCollectError(p
->dbRbu
, &pIter
->pTblIter
, &p
->zErrmsg
,
986 "SELECT rbu_target_name(name, type='view') AS target, name "
987 "FROM sqlite_schema "
988 "WHERE type IN ('table', 'view') AND target IS NOT NULL "
991 , rbuIsVacuum(p
) ? "AND rootpage!=0 AND rootpage IS NOT NULL" : ""));
994 rc
= prepareAndCollectError(p
->dbMain
, &pIter
->pIdxIter
, &p
->zErrmsg
,
995 "SELECT name, rootpage, sql IS NULL OR substr(8, 6)=='UNIQUE' "
996 " FROM main.sqlite_schema "
997 " WHERE type='index' AND tbl_name = ?"
1001 pIter
->bCleanup
= 1;
1003 return rbuObjIterNext(p
, pIter
);
1007 ** This is a wrapper around "sqlite3_mprintf(zFmt, ...)". If an OOM occurs,
1008 ** an error code is stored in the RBU handle passed as the first argument.
1010 ** If an error has already occurred (p->rc is already set to something other
1011 ** than SQLITE_OK), then this function returns NULL without modifying the
1012 ** stored error code. In this case it still calls sqlite3_free() on any
1013 ** printf() parameters associated with %z conversions.
1015 static char *rbuMPrintf(sqlite3rbu
*p
, const char *zFmt
, ...){
1019 zSql
= sqlite3_vmprintf(zFmt
, ap
);
1020 if( p
->rc
==SQLITE_OK
){
1021 if( zSql
==0 ) p
->rc
= SQLITE_NOMEM
;
1031 ** Argument zFmt is a sqlite3_mprintf() style format string. The trailing
1032 ** arguments are the usual subsitution values. This function performs
1033 ** the printf() style substitutions and executes the result as an SQL
1034 ** statement on the RBU handles database.
1036 ** If an error occurs, an error code and error message is stored in the
1037 ** RBU handle. If an error has already occurred when this function is
1038 ** called, it is a no-op.
1040 static int rbuMPrintfExec(sqlite3rbu
*p
, sqlite3
*db
, const char *zFmt
, ...){
1044 zSql
= sqlite3_vmprintf(zFmt
, ap
);
1045 if( p
->rc
==SQLITE_OK
){
1047 p
->rc
= SQLITE_NOMEM
;
1049 p
->rc
= sqlite3_exec(db
, zSql
, 0, 0, &p
->zErrmsg
);
1058 ** Attempt to allocate and return a pointer to a zeroed block of nByte
1061 ** If an error (i.e. an OOM condition) occurs, return NULL and leave an
1062 ** error code in the rbu handle passed as the first argument. Or, if an
1063 ** error has already occurred when this function is called, return NULL
1064 ** immediately without attempting the allocation or modifying the stored
1067 static void *rbuMalloc(sqlite3rbu
*p
, sqlite3_int64 nByte
){
1069 if( p
->rc
==SQLITE_OK
){
1071 pRet
= sqlite3_malloc64(nByte
);
1073 p
->rc
= SQLITE_NOMEM
;
1075 memset(pRet
, 0, nByte
);
1083 ** Allocate and zero the pIter->azTblCol[] and abTblPk[] arrays so that
1084 ** there is room for at least nCol elements. If an OOM occurs, store an
1085 ** error code in the RBU handle passed as the first argument.
1087 static void rbuAllocateIterArrays(sqlite3rbu
*p
, RbuObjIter
*pIter
, int nCol
){
1088 sqlite3_int64 nByte
= (2*sizeof(char*) + sizeof(int) + 3*sizeof(u8
)) * nCol
;
1091 azNew
= (char**)rbuMalloc(p
, nByte
);
1093 pIter
->azTblCol
= azNew
;
1094 pIter
->azTblType
= &azNew
[nCol
];
1095 pIter
->aiSrcOrder
= (int*)&pIter
->azTblType
[nCol
];
1096 pIter
->abTblPk
= (u8
*)&pIter
->aiSrcOrder
[nCol
];
1097 pIter
->abNotNull
= (u8
*)&pIter
->abTblPk
[nCol
];
1098 pIter
->abIndexed
= (u8
*)&pIter
->abNotNull
[nCol
];
1103 ** The first argument must be a nul-terminated string. This function
1104 ** returns a copy of the string in memory obtained from sqlite3_malloc().
1105 ** It is the responsibility of the caller to eventually free this memory
1106 ** using sqlite3_free().
1108 ** If an OOM condition is encountered when attempting to allocate memory,
1109 ** output variable (*pRc) is set to SQLITE_NOMEM before returning. Otherwise,
1110 ** if the allocation succeeds, (*pRc) is left unchanged.
1112 static char *rbuStrndup(const char *zStr
, int *pRc
){
1115 if( *pRc
==SQLITE_OK
){
1117 size_t nCopy
= strlen(zStr
) + 1;
1118 zRet
= (char*)sqlite3_malloc64(nCopy
);
1120 memcpy(zRet
, zStr
, nCopy
);
1122 *pRc
= SQLITE_NOMEM
;
1131 ** Finalize the statement passed as the second argument.
1133 ** If the sqlite3_finalize() call indicates that an error occurs, and the
1134 ** rbu handle error code is not already set, set the error code and error
1135 ** message accordingly.
1137 static void rbuFinalize(sqlite3rbu
*p
, sqlite3_stmt
*pStmt
){
1138 sqlite3
*db
= sqlite3_db_handle(pStmt
);
1139 int rc
= sqlite3_finalize(pStmt
);
1140 if( p
->rc
==SQLITE_OK
&& rc
!=SQLITE_OK
){
1142 p
->zErrmsg
= sqlite3_mprintf("%s", sqlite3_errmsg(db
));
1146 /* Determine the type of a table.
1148 ** peType is of type (int*), a pointer to an output parameter of type
1149 ** (int). This call sets the output parameter as follows, depending
1150 ** on the type of the table specified by parameters dbName and zTbl.
1152 ** RBU_PK_NOTABLE: No such table.
1153 ** RBU_PK_NONE: Table has an implicit rowid.
1154 ** RBU_PK_IPK: Table has an explicit IPK column.
1155 ** RBU_PK_EXTERNAL: Table has an external PK index.
1156 ** RBU_PK_WITHOUT_ROWID: Table is WITHOUT ROWID.
1157 ** RBU_PK_VTAB: Table is a virtual table.
1159 ** Argument *piPk is also of type (int*), and also points to an output
1160 ** parameter. Unless the table has an external primary key index
1161 ** (i.e. unless *peType is set to 3), then *piPk is set to zero. Or,
1162 ** if the table does have an external primary key index, then *piPk
1163 ** is set to the root page number of the primary key index before
1168 ** if( no entry exists in sqlite_schema ){
1169 ** return RBU_PK_NOTABLE
1170 ** }else if( sql for the entry starts with "CREATE VIRTUAL" ){
1171 ** return RBU_PK_VTAB
1172 ** }else if( "PRAGMA index_list()" for the table contains a "pk" index ){
1173 ** if( the index that is the pk exists in sqlite_schema ){
1174 ** *piPK = rootpage of that index.
1175 ** return RBU_PK_EXTERNAL
1177 ** return RBU_PK_WITHOUT_ROWID
1179 ** }else if( "PRAGMA table_info()" lists one or more "pk" columns ){
1180 ** return RBU_PK_IPK
1182 ** return RBU_PK_NONE
1185 static void rbuTableType(
1193 ** 0) SELECT count(*) FROM sqlite_schema where name=%Q AND IsVirtual(%Q)
1194 ** 1) PRAGMA index_list = ?
1195 ** 2) SELECT count(*) FROM sqlite_schema where name=%Q
1196 ** 3) PRAGMA table_info = ?
1198 sqlite3_stmt
*aStmt
[4] = {0, 0, 0, 0};
1200 *peType
= RBU_PK_NOTABLE
;
1203 assert( p
->rc
==SQLITE_OK
);
1204 p
->rc
= prepareFreeAndCollectError(p
->dbMain
, &aStmt
[0], &p
->zErrmsg
,
1207 " (sql COLLATE nocase BETWEEN 'CREATE VIRTUAL' AND 'CREATE VIRTUAM'),"
1209 " FROM sqlite_schema"
1210 " WHERE name=%Q", zTab
1212 if( p
->rc
!=SQLITE_OK
|| sqlite3_step(aStmt
[0])!=SQLITE_ROW
){
1213 /* Either an error, or no such table. */
1214 goto rbuTableType_end
;
1216 if( sqlite3_column_int(aStmt
[0], 0) ){
1217 *peType
= RBU_PK_VTAB
; /* virtual table */
1218 goto rbuTableType_end
;
1220 *piTnum
= sqlite3_column_int(aStmt
[0], 1);
1222 p
->rc
= prepareFreeAndCollectError(p
->dbMain
, &aStmt
[1], &p
->zErrmsg
,
1223 sqlite3_mprintf("PRAGMA index_list=%Q",zTab
)
1225 if( p
->rc
) goto rbuTableType_end
;
1226 while( sqlite3_step(aStmt
[1])==SQLITE_ROW
){
1227 const u8
*zOrig
= sqlite3_column_text(aStmt
[1], 3);
1228 const u8
*zIdx
= sqlite3_column_text(aStmt
[1], 1);
1229 if( zOrig
&& zIdx
&& zOrig
[0]=='p' ){
1230 p
->rc
= prepareFreeAndCollectError(p
->dbMain
, &aStmt
[2], &p
->zErrmsg
,
1232 "SELECT rootpage FROM sqlite_schema WHERE name = %Q", zIdx
1234 if( p
->rc
==SQLITE_OK
){
1235 if( sqlite3_step(aStmt
[2])==SQLITE_ROW
){
1236 *piPk
= sqlite3_column_int(aStmt
[2], 0);
1237 *peType
= RBU_PK_EXTERNAL
;
1239 *peType
= RBU_PK_WITHOUT_ROWID
;
1242 goto rbuTableType_end
;
1246 p
->rc
= prepareFreeAndCollectError(p
->dbMain
, &aStmt
[3], &p
->zErrmsg
,
1247 sqlite3_mprintf("PRAGMA table_info=%Q",zTab
)
1249 if( p
->rc
==SQLITE_OK
){
1250 while( sqlite3_step(aStmt
[3])==SQLITE_ROW
){
1251 if( sqlite3_column_int(aStmt
[3],5)>0 ){
1252 *peType
= RBU_PK_IPK
; /* explicit IPK column */
1253 goto rbuTableType_end
;
1256 *peType
= RBU_PK_NONE
;
1261 for(i
=0; i
<sizeof(aStmt
)/sizeof(aStmt
[0]); i
++){
1262 rbuFinalize(p
, aStmt
[i
]);
1268 ** This is a helper function for rbuObjIterCacheTableInfo(). It populates
1269 ** the pIter->abIndexed[] array.
1271 static void rbuObjIterCacheIndexedCols(sqlite3rbu
*p
, RbuObjIter
*pIter
){
1272 sqlite3_stmt
*pList
= 0;
1275 if( p
->rc
==SQLITE_OK
){
1276 memcpy(pIter
->abIndexed
, pIter
->abTblPk
, sizeof(u8
)*pIter
->nTblCol
);
1277 p
->rc
= prepareFreeAndCollectError(p
->dbMain
, &pList
, &p
->zErrmsg
,
1278 sqlite3_mprintf("PRAGMA main.index_list = %Q", pIter
->zTbl
)
1283 while( p
->rc
==SQLITE_OK
&& SQLITE_ROW
==sqlite3_step(pList
) ){
1284 const char *zIdx
= (const char*)sqlite3_column_text(pList
, 1);
1285 int bPartial
= sqlite3_column_int(pList
, 4);
1286 sqlite3_stmt
*pXInfo
= 0;
1287 if( zIdx
==0 ) break;
1289 memset(pIter
->abIndexed
, 0x01, sizeof(u8
)*pIter
->nTblCol
);
1291 p
->rc
= prepareFreeAndCollectError(p
->dbMain
, &pXInfo
, &p
->zErrmsg
,
1292 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx
)
1294 while( p
->rc
==SQLITE_OK
&& SQLITE_ROW
==sqlite3_step(pXInfo
) ){
1295 int iCid
= sqlite3_column_int(pXInfo
, 1);
1296 if( iCid
>=0 ) pIter
->abIndexed
[iCid
] = 1;
1298 memset(pIter
->abIndexed
, 0x01, sizeof(u8
)*pIter
->nTblCol
);
1301 rbuFinalize(p
, pXInfo
);
1306 if( pIter
->eType
==RBU_PK_WITHOUT_ROWID
){
1307 /* "PRAGMA index_list" includes the main PK b-tree */
1311 rbuFinalize(p
, pList
);
1312 if( bIndex
==0 ) pIter
->abIndexed
= 0;
1317 ** If they are not already populated, populate the pIter->azTblCol[],
1318 ** pIter->abTblPk[], pIter->nTblCol and pIter->bRowid variables according to
1319 ** the table (not index) that the iterator currently points to.
1321 ** Return SQLITE_OK if successful, or an SQLite error code otherwise. If
1322 ** an error does occur, an error code and error message are also left in
1325 static int rbuObjIterCacheTableInfo(sqlite3rbu
*p
, RbuObjIter
*pIter
){
1326 if( pIter
->azTblCol
==0 ){
1327 sqlite3_stmt
*pStmt
= 0;
1329 int i
; /* for() loop iterator variable */
1330 int bRbuRowid
= 0; /* If input table has column "rbu_rowid" */
1334 /* Figure out the type of table this step will deal with. */
1335 assert( pIter
->eType
==0 );
1336 rbuTableType(p
, pIter
->zTbl
, &pIter
->eType
, &iTnum
, &pIter
->iPkTnum
);
1337 if( p
->rc
==SQLITE_OK
&& pIter
->eType
==RBU_PK_NOTABLE
){
1338 p
->rc
= SQLITE_ERROR
;
1339 p
->zErrmsg
= sqlite3_mprintf("no such table: %s", pIter
->zTbl
);
1341 if( p
->rc
) return p
->rc
;
1342 if( pIter
->zIdx
==0 ) pIter
->iTnum
= iTnum
;
1344 assert( pIter
->eType
==RBU_PK_NONE
|| pIter
->eType
==RBU_PK_IPK
1345 || pIter
->eType
==RBU_PK_EXTERNAL
|| pIter
->eType
==RBU_PK_WITHOUT_ROWID
1346 || pIter
->eType
==RBU_PK_VTAB
1349 /* Populate the azTblCol[] and nTblCol variables based on the columns
1350 ** of the input table. Ignore any input table columns that begin with
1352 p
->rc
= prepareFreeAndCollectError(p
->dbRbu
, &pStmt
, &p
->zErrmsg
,
1353 sqlite3_mprintf("SELECT * FROM '%q'", pIter
->zDataTbl
)
1355 if( p
->rc
==SQLITE_OK
){
1356 nCol
= sqlite3_column_count(pStmt
);
1357 rbuAllocateIterArrays(p
, pIter
, nCol
);
1359 for(i
=0; p
->rc
==SQLITE_OK
&& i
<nCol
; i
++){
1360 const char *zName
= (const char*)sqlite3_column_name(pStmt
, i
);
1361 if( sqlite3_strnicmp("rbu_", zName
, 4) ){
1362 char *zCopy
= rbuStrndup(zName
, &p
->rc
);
1363 pIter
->aiSrcOrder
[pIter
->nTblCol
] = pIter
->nTblCol
;
1364 pIter
->azTblCol
[pIter
->nTblCol
++] = zCopy
;
1366 else if( 0==sqlite3_stricmp("rbu_rowid", zName
) ){
1370 sqlite3_finalize(pStmt
);
1373 if( p
->rc
==SQLITE_OK
1374 && rbuIsVacuum(p
)==0
1375 && bRbuRowid
!=(pIter
->eType
==RBU_PK_VTAB
|| pIter
->eType
==RBU_PK_NONE
)
1377 p
->rc
= SQLITE_ERROR
;
1378 p
->zErrmsg
= sqlite3_mprintf(
1379 "table %q %s rbu_rowid column", pIter
->zDataTbl
,
1380 (bRbuRowid
? "may not have" : "requires")
1384 /* Check that all non-HIDDEN columns in the destination table are also
1385 ** present in the input table. Populate the abTblPk[], azTblType[] and
1386 ** aiTblOrder[] arrays at the same time. */
1387 if( p
->rc
==SQLITE_OK
){
1388 p
->rc
= prepareFreeAndCollectError(p
->dbMain
, &pStmt
, &p
->zErrmsg
,
1389 sqlite3_mprintf("PRAGMA table_info(%Q)", pIter
->zTbl
)
1392 while( p
->rc
==SQLITE_OK
&& SQLITE_ROW
==sqlite3_step(pStmt
) ){
1393 const char *zName
= (const char*)sqlite3_column_text(pStmt
, 1);
1394 if( zName
==0 ) break; /* An OOM - finalize() below returns S_NOMEM */
1395 for(i
=iOrder
; i
<pIter
->nTblCol
; i
++){
1396 if( 0==strcmp(zName
, pIter
->azTblCol
[i
]) ) break;
1398 if( i
==pIter
->nTblCol
){
1399 p
->rc
= SQLITE_ERROR
;
1400 p
->zErrmsg
= sqlite3_mprintf("column missing from %q: %s",
1401 pIter
->zDataTbl
, zName
1404 int iPk
= sqlite3_column_int(pStmt
, 5);
1405 int bNotNull
= sqlite3_column_int(pStmt
, 3);
1406 const char *zType
= (const char*)sqlite3_column_text(pStmt
, 2);
1409 SWAP(int, pIter
->aiSrcOrder
[i
], pIter
->aiSrcOrder
[iOrder
]);
1410 SWAP(char*, pIter
->azTblCol
[i
], pIter
->azTblCol
[iOrder
]);
1413 pIter
->azTblType
[iOrder
] = rbuStrndup(zType
, &p
->rc
);
1415 pIter
->abTblPk
[iOrder
] = (u8
)iPk
;
1416 pIter
->abNotNull
[iOrder
] = (u8
)bNotNull
|| (iPk
!=0);
1421 rbuFinalize(p
, pStmt
);
1422 rbuObjIterCacheIndexedCols(p
, pIter
);
1423 assert( pIter
->eType
!=RBU_PK_VTAB
|| pIter
->abIndexed
==0 );
1424 assert( pIter
->eType
!=RBU_PK_VTAB
|| pIter
->nIndex
==0 );
1431 ** This function constructs and returns a pointer to a nul-terminated
1432 ** string containing some SQL clause or list based on one or more of the
1433 ** column names currently stored in the pIter->azTblCol[] array.
1435 static char *rbuObjIterGetCollist(
1436 sqlite3rbu
*p
, /* RBU object */
1437 RbuObjIter
*pIter
/* Object iterator for column names */
1440 const char *zSep
= "";
1442 for(i
=0; i
<pIter
->nTblCol
; i
++){
1443 const char *z
= pIter
->azTblCol
[i
];
1444 zList
= rbuMPrintf(p
, "%z%s\"%w\"", zList
, zSep
, z
);
1451 ** Return a comma separated list of the quoted PRIMARY KEY column names,
1452 ** in order, for the current table. Before each column name, add the text
1453 ** zPre. After each column name, add the zPost text. Use zSeparator as
1454 ** the separator text (usually ", ").
1456 static char *rbuObjIterGetPkList(
1457 sqlite3rbu
*p
, /* RBU object */
1458 RbuObjIter
*pIter
, /* Object iterator for column names */
1459 const char *zPre
, /* Before each quoted column name */
1460 const char *zSeparator
, /* Separator to use between columns */
1461 const char *zPost
/* After each quoted column name */
1465 const char *zSep
= "";
1468 for(i
=0; i
<pIter
->nTblCol
; i
++){
1469 if( (int)pIter
->abTblPk
[i
]==iPk
){
1470 const char *zCol
= pIter
->azTblCol
[i
];
1471 zRet
= rbuMPrintf(p
, "%z%s%s\"%w\"%s", zRet
, zSep
, zPre
, zCol
, zPost
);
1476 if( i
==pIter
->nTblCol
) break;
1483 ** This function is called as part of restarting an RBU vacuum within
1484 ** stage 1 of the process (while the *-oal file is being built) while
1485 ** updating a table (not an index). The table may be a rowid table or
1486 ** a WITHOUT ROWID table. It queries the target database to find the
1487 ** largest key that has already been written to the target table and
1488 ** constructs a WHERE clause that can be used to extract the remaining
1489 ** rows from the source table. For a rowid table, the WHERE clause
1492 ** "WHERE _rowid_ > ?"
1494 ** and for WITHOUT ROWID tables:
1496 ** "WHERE (key1, key2) > (?, ?)"
1498 ** Instead of "?" placeholders, the actual WHERE clauses created by
1499 ** this function contain literal SQL values.
1501 static char *rbuVacuumTableStart(
1502 sqlite3rbu
*p
, /* RBU handle */
1503 RbuObjIter
*pIter
, /* RBU iterator object */
1504 int bRowid
, /* True for a rowid table */
1505 const char *zWrite
/* Target table name prefix */
1507 sqlite3_stmt
*pMax
= 0;
1510 p
->rc
= prepareFreeAndCollectError(p
->dbMain
, &pMax
, &p
->zErrmsg
,
1512 "SELECT max(_rowid_) FROM \"%s%w\"", zWrite
, pIter
->zTbl
1515 if( p
->rc
==SQLITE_OK
&& SQLITE_ROW
==sqlite3_step(pMax
) ){
1516 sqlite3_int64 iMax
= sqlite3_column_int64(pMax
, 0);
1517 zRet
= rbuMPrintf(p
, " WHERE _rowid_ > %lld ", iMax
);
1519 rbuFinalize(p
, pMax
);
1521 char *zOrder
= rbuObjIterGetPkList(p
, pIter
, "", ", ", " DESC");
1522 char *zSelect
= rbuObjIterGetPkList(p
, pIter
, "quote(", "||','||", ")");
1523 char *zList
= rbuObjIterGetPkList(p
, pIter
, "", ", ", "");
1525 if( p
->rc
==SQLITE_OK
){
1526 p
->rc
= prepareFreeAndCollectError(p
->dbMain
, &pMax
, &p
->zErrmsg
,
1528 "SELECT %s FROM \"%s%w\" ORDER BY %s LIMIT 1",
1529 zSelect
, zWrite
, pIter
->zTbl
, zOrder
1532 if( p
->rc
==SQLITE_OK
&& SQLITE_ROW
==sqlite3_step(pMax
) ){
1533 const char *zVal
= (const char*)sqlite3_column_text(pMax
, 0);
1534 zRet
= rbuMPrintf(p
, " WHERE (%s) > (%s) ", zList
, zVal
);
1536 rbuFinalize(p
, pMax
);
1539 sqlite3_free(zOrder
);
1540 sqlite3_free(zSelect
);
1541 sqlite3_free(zList
);
1547 ** This function is called as part of restating an RBU vacuum when the
1548 ** current operation is writing content to an index. If possible, it
1549 ** queries the target index b-tree for the largest key already written to
1550 ** it, then composes and returns an expression that can be used in a WHERE
1551 ** clause to select the remaining required rows from the source table.
1552 ** It is only possible to return such an expression if:
1554 ** * The index contains no DESC columns, and
1555 ** * The last key written to the index before the operation was
1556 ** suspended does not contain any NULL values.
1558 ** The expression is of the form:
1560 ** (index-field1, index-field2, ...) > (?, ?, ...)
1562 ** except that the "?" placeholders are replaced with literal values.
1564 ** If the expression cannot be created, NULL is returned. In this case,
1565 ** the caller has to use an OFFSET clause to extract only the required
1566 ** rows from the sourct table, just as it does for an RBU update operation.
1568 static char *rbuVacuumIndexStart(
1569 sqlite3rbu
*p
, /* RBU handle */
1570 RbuObjIter
*pIter
/* RBU iterator object */
1578 const char *zSep
= "";
1580 sqlite3_stmt
*pXInfo
= 0;
1582 p
->rc
= prepareFreeAndCollectError(p
->dbMain
, &pXInfo
, &p
->zErrmsg
,
1583 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", pIter
->zIdx
)
1585 while( p
->rc
==SQLITE_OK
&& SQLITE_ROW
==sqlite3_step(pXInfo
) ){
1586 int iCid
= sqlite3_column_int(pXInfo
, 1);
1587 const char *zCollate
= (const char*)sqlite3_column_text(pXInfo
, 4);
1589 if( sqlite3_column_int(pXInfo
, 3) ){
1595 if( pIter
->eType
==RBU_PK_IPK
){
1597 for(i
=0; pIter
->abTblPk
[i
]==0; i
++);
1598 assert( i
<pIter
->nTblCol
);
1599 zCol
= pIter
->azTblCol
[i
];
1604 zCol
= pIter
->azTblCol
[iCid
];
1607 zLhs
= rbuMPrintf(p
, "%z%s \"%w\" COLLATE %Q",
1608 zLhs
, zSep
, zCol
, zCollate
1610 zOrder
= rbuMPrintf(p
, "%z%s \"rbu_imp_%d%w\" COLLATE %Q DESC",
1611 zOrder
, zSep
, iCol
, zCol
, zCollate
1613 zSelect
= rbuMPrintf(p
, "%z%s quote(\"rbu_imp_%d%w\")",
1614 zSelect
, zSep
, iCol
, zCol
1619 rbuFinalize(p
, pXInfo
);
1620 if( bFailed
) goto index_start_out
;
1622 if( p
->rc
==SQLITE_OK
){
1623 sqlite3_stmt
*pSel
= 0;
1625 p
->rc
= prepareFreeAndCollectError(p
->dbMain
, &pSel
, &p
->zErrmsg
,
1626 sqlite3_mprintf("SELECT %s FROM \"rbu_imp_%w\" ORDER BY %s LIMIT 1",
1627 zSelect
, pIter
->zTbl
, zOrder
1630 if( p
->rc
==SQLITE_OK
&& SQLITE_ROW
==sqlite3_step(pSel
) ){
1632 for(iCol
=0; iCol
<pIter
->nCol
; iCol
++){
1633 const char *zQuoted
= (const char*)sqlite3_column_text(pSel
, iCol
);
1635 p
->rc
= SQLITE_NOMEM
;
1636 }else if( zQuoted
[0]=='N' ){
1640 zVector
= rbuMPrintf(p
, "%z%s%s", zVector
, zSep
, zQuoted
);
1645 zRet
= rbuMPrintf(p
, "(%s) > (%s)", zLhs
, zVector
);
1648 rbuFinalize(p
, pSel
);
1652 sqlite3_free(zOrder
);
1653 sqlite3_free(zSelect
);
1654 sqlite3_free(zVector
);
1660 ** This function is used to create a SELECT list (the list of SQL
1661 ** expressions that follows a SELECT keyword) for a SELECT statement
1662 ** used to read from an data_xxx or rbu_tmp_xxx table while updating the
1663 ** index object currently indicated by the iterator object passed as the
1664 ** second argument. A "PRAGMA index_xinfo = <idxname>" statement is used
1665 ** to obtain the required information.
1667 ** If the index is of the following form:
1669 ** CREATE INDEX i1 ON t1(c, b COLLATE nocase);
1671 ** and "t1" is a table with an explicit INTEGER PRIMARY KEY column
1672 ** "ipk", the returned string is:
1674 ** "`c` COLLATE 'BINARY', `b` COLLATE 'NOCASE', `ipk` COLLATE 'BINARY'"
1676 ** As well as the returned string, three other malloc'd strings are
1677 ** returned via output parameters. As follows:
1679 ** pzImposterCols: ...
1680 ** pzImposterPk: ...
1683 static char *rbuObjIterGetIndexCols(
1684 sqlite3rbu
*p
, /* RBU object */
1685 RbuObjIter
*pIter
, /* Object iterator for column names */
1686 char **pzImposterCols
, /* OUT: Columns for imposter table */
1687 char **pzImposterPk
, /* OUT: Imposter PK clause */
1688 char **pzWhere
, /* OUT: WHERE clause */
1689 int *pnBind
/* OUT: Trbul number of columns */
1691 int rc
= p
->rc
; /* Error code */
1692 int rc2
; /* sqlite3_finalize() return code */
1693 char *zRet
= 0; /* String to return */
1694 char *zImpCols
= 0; /* String to return via *pzImposterCols */
1695 char *zImpPK
= 0; /* String to return via *pzImposterPK */
1696 char *zWhere
= 0; /* String to return via *pzWhere */
1697 int nBind
= 0; /* Value to return via *pnBind */
1698 const char *zCom
= ""; /* Set to ", " later on */
1699 const char *zAnd
= ""; /* Set to " AND " later on */
1700 sqlite3_stmt
*pXInfo
= 0; /* PRAGMA index_xinfo = ? */
1702 if( rc
==SQLITE_OK
){
1703 assert( p
->zErrmsg
==0 );
1704 rc
= prepareFreeAndCollectError(p
->dbMain
, &pXInfo
, &p
->zErrmsg
,
1705 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", pIter
->zIdx
)
1709 while( rc
==SQLITE_OK
&& SQLITE_ROW
==sqlite3_step(pXInfo
) ){
1710 int iCid
= sqlite3_column_int(pXInfo
, 1);
1711 int bDesc
= sqlite3_column_int(pXInfo
, 3);
1712 const char *zCollate
= (const char*)sqlite3_column_text(pXInfo
, 4);
1713 const char *zCol
= 0;
1717 int iSeq
= sqlite3_column_int(pXInfo
, 0);
1718 zRet
= sqlite3_mprintf("%z%s(%.*s) COLLATE %Q", zRet
, zCom
,
1719 pIter
->aIdxCol
[iSeq
].nSpan
, pIter
->aIdxCol
[iSeq
].zSpan
, zCollate
1724 /* An integer primary key. If the table has an explicit IPK, use
1725 ** its name. Otherwise, use "rbu_rowid". */
1726 if( pIter
->eType
==RBU_PK_IPK
){
1728 for(i
=0; pIter
->abTblPk
[i
]==0; i
++);
1729 assert( i
<pIter
->nTblCol
);
1730 zCol
= pIter
->azTblCol
[i
];
1731 }else if( rbuIsVacuum(p
) ){
1738 zCol
= pIter
->azTblCol
[iCid
];
1739 zType
= pIter
->azTblType
[iCid
];
1741 zRet
= sqlite3_mprintf("%z%s\"%w\" COLLATE %Q", zRet
, zCom
,zCol
,zCollate
);
1744 if( pIter
->bUnique
==0 || sqlite3_column_int(pXInfo
, 5) ){
1745 const char *zOrder
= (bDesc
? " DESC" : "");
1746 zImpPK
= sqlite3_mprintf("%z%s\"rbu_imp_%d%w\"%s",
1747 zImpPK
, zCom
, nBind
, zCol
, zOrder
1750 zImpCols
= sqlite3_mprintf("%z%s\"rbu_imp_%d%w\" %s COLLATE %Q",
1751 zImpCols
, zCom
, nBind
, zCol
, zType
, zCollate
1753 zWhere
= sqlite3_mprintf(
1754 "%z%s\"rbu_imp_%d%w\" IS ?", zWhere
, zAnd
, nBind
, zCol
1756 if( zRet
==0 || zImpPK
==0 || zImpCols
==0 || zWhere
==0 ) rc
= SQLITE_NOMEM
;
1762 rc2
= sqlite3_finalize(pXInfo
);
1763 if( rc
==SQLITE_OK
) rc
= rc2
;
1765 if( rc
!=SQLITE_OK
){
1767 sqlite3_free(zImpCols
);
1768 sqlite3_free(zImpPK
);
1769 sqlite3_free(zWhere
);
1777 *pzImposterCols
= zImpCols
;
1778 *pzImposterPk
= zImpPK
;
1785 ** Assuming the current table columns are "a", "b" and "c", and the zObj
1786 ** paramter is passed "old", return a string of the form:
1788 ** "old.a, old.b, old.b"
1790 ** With the column names escaped.
1792 ** For tables with implicit rowids - RBU_PK_EXTERNAL and RBU_PK_NONE, append
1793 ** the text ", old._rowid_" to the returned value.
1795 static char *rbuObjIterGetOldlist(
1801 if( p
->rc
==SQLITE_OK
&& pIter
->abIndexed
){
1802 const char *zS
= "";
1804 for(i
=0; i
<pIter
->nTblCol
; i
++){
1805 if( pIter
->abIndexed
[i
] ){
1806 const char *zCol
= pIter
->azTblCol
[i
];
1807 zList
= sqlite3_mprintf("%z%s%s.\"%w\"", zList
, zS
, zObj
, zCol
);
1809 zList
= sqlite3_mprintf("%z%sNULL", zList
, zS
);
1813 p
->rc
= SQLITE_NOMEM
;
1818 /* For a table with implicit rowids, append "old._rowid_" to the list. */
1819 if( pIter
->eType
==RBU_PK_EXTERNAL
|| pIter
->eType
==RBU_PK_NONE
){
1820 zList
= rbuMPrintf(p
, "%z, %s._rowid_", zList
, zObj
);
1827 ** Return an expression that can be used in a WHERE clause to match the
1828 ** primary key of the current table. For example, if the table is:
1830 ** CREATE TABLE t1(a, b, c, PRIMARY KEY(b, c));
1832 ** Return the string:
1834 ** "b = ?1 AND c = ?2"
1836 static char *rbuObjIterGetWhere(
1841 if( pIter
->eType
==RBU_PK_VTAB
|| pIter
->eType
==RBU_PK_NONE
){
1842 zList
= rbuMPrintf(p
, "_rowid_ = ?%d", pIter
->nTblCol
+1);
1843 }else if( pIter
->eType
==RBU_PK_EXTERNAL
){
1844 const char *zSep
= "";
1846 for(i
=0; i
<pIter
->nTblCol
; i
++){
1847 if( pIter
->abTblPk
[i
] ){
1848 zList
= rbuMPrintf(p
, "%z%sc%d=?%d", zList
, zSep
, i
, i
+1);
1852 zList
= rbuMPrintf(p
,
1853 "_rowid_ = (SELECT id FROM rbu_imposter2 WHERE %z)", zList
1857 const char *zSep
= "";
1859 for(i
=0; i
<pIter
->nTblCol
; i
++){
1860 if( pIter
->abTblPk
[i
] ){
1861 const char *zCol
= pIter
->azTblCol
[i
];
1862 zList
= rbuMPrintf(p
, "%z%s\"%w\"=?%d", zList
, zSep
, zCol
, i
+1);
1871 ** The SELECT statement iterating through the keys for the current object
1872 ** (p->objiter.pSelect) currently points to a valid row. However, there
1873 ** is something wrong with the rbu_control value in the rbu_control value
1874 ** stored in the (p->nCol+1)'th column. Set the error code and error message
1875 ** of the RBU handle to something reflecting this.
1877 static void rbuBadControlError(sqlite3rbu
*p
){
1878 p
->rc
= SQLITE_ERROR
;
1879 p
->zErrmsg
= sqlite3_mprintf("invalid rbu_control value");
1884 ** Return a nul-terminated string containing the comma separated list of
1885 ** assignments that should be included following the "SET" keyword of
1886 ** an UPDATE statement used to update the table object that the iterator
1887 ** passed as the second argument currently points to if the rbu_control
1888 ** column of the data_xxx table entry is set to zMask.
1890 ** The memory for the returned string is obtained from sqlite3_malloc().
1891 ** It is the responsibility of the caller to eventually free it using
1894 ** If an OOM error is encountered when allocating space for the new
1895 ** string, an error code is left in the rbu handle passed as the first
1896 ** argument and NULL is returned. Or, if an error has already occurred
1897 ** when this function is called, NULL is returned immediately, without
1898 ** attempting the allocation or modifying the stored error code.
1900 static char *rbuObjIterGetSetlist(
1906 if( p
->rc
==SQLITE_OK
){
1909 if( (int)strlen(zMask
)!=pIter
->nTblCol
){
1910 rbuBadControlError(p
);
1912 const char *zSep
= "";
1913 for(i
=0; i
<pIter
->nTblCol
; i
++){
1914 char c
= zMask
[pIter
->aiSrcOrder
[i
]];
1916 zList
= rbuMPrintf(p
, "%z%s\"%w\"=?%d",
1917 zList
, zSep
, pIter
->azTblCol
[i
], i
+1
1922 zList
= rbuMPrintf(p
, "%z%s\"%w\"=rbu_delta(\"%w\", ?%d)",
1923 zList
, zSep
, pIter
->azTblCol
[i
], pIter
->azTblCol
[i
], i
+1
1928 zList
= rbuMPrintf(p
, "%z%s\"%w\"=rbu_fossil_delta(\"%w\", ?%d)",
1929 zList
, zSep
, pIter
->azTblCol
[i
], pIter
->azTblCol
[i
], i
+1
1940 ** Return a nul-terminated string consisting of nByte comma separated
1941 ** "?" expressions. For example, if nByte is 3, return a pointer to
1942 ** a buffer containing the string "?,?,?".
1944 ** The memory for the returned string is obtained from sqlite3_malloc().
1945 ** It is the responsibility of the caller to eventually free it using
1948 ** If an OOM error is encountered when allocating space for the new
1949 ** string, an error code is left in the rbu handle passed as the first
1950 ** argument and NULL is returned. Or, if an error has already occurred
1951 ** when this function is called, NULL is returned immediately, without
1952 ** attempting the allocation or modifying the stored error code.
1954 static char *rbuObjIterGetBindlist(sqlite3rbu
*p
, int nBind
){
1956 sqlite3_int64 nByte
= 2*(sqlite3_int64
)nBind
+ 1;
1958 zRet
= (char*)rbuMalloc(p
, nByte
);
1961 for(i
=0; i
<nBind
; i
++){
1963 zRet
[i
*2+1] = (i
+1==nBind
) ? '\0' : ',';
1970 ** The iterator currently points to a table (not index) of type
1971 ** RBU_PK_WITHOUT_ROWID. This function creates the PRIMARY KEY
1972 ** declaration for the corresponding imposter table. For example,
1973 ** if the iterator points to a table created as:
1975 ** CREATE TABLE t1(a, b, c, PRIMARY KEY(b, a DESC)) WITHOUT ROWID
1977 ** this function returns:
1979 ** PRIMARY KEY("b", "a" DESC)
1981 static char *rbuWithoutRowidPK(sqlite3rbu
*p
, RbuObjIter
*pIter
){
1983 assert( pIter
->zIdx
==0 );
1984 if( p
->rc
==SQLITE_OK
){
1985 const char *zSep
= "PRIMARY KEY(";
1986 sqlite3_stmt
*pXList
= 0; /* PRAGMA index_list = (pIter->zTbl) */
1987 sqlite3_stmt
*pXInfo
= 0; /* PRAGMA index_xinfo = <pk-index> */
1989 p
->rc
= prepareFreeAndCollectError(p
->dbMain
, &pXList
, &p
->zErrmsg
,
1990 sqlite3_mprintf("PRAGMA main.index_list = %Q", pIter
->zTbl
)
1992 while( p
->rc
==SQLITE_OK
&& SQLITE_ROW
==sqlite3_step(pXList
) ){
1993 const char *zOrig
= (const char*)sqlite3_column_text(pXList
,3);
1994 if( zOrig
&& strcmp(zOrig
, "pk")==0 ){
1995 const char *zIdx
= (const char*)sqlite3_column_text(pXList
,1);
1997 p
->rc
= prepareFreeAndCollectError(p
->dbMain
, &pXInfo
, &p
->zErrmsg
,
1998 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx
)
2004 rbuFinalize(p
, pXList
);
2006 while( p
->rc
==SQLITE_OK
&& SQLITE_ROW
==sqlite3_step(pXInfo
) ){
2007 if( sqlite3_column_int(pXInfo
, 5) ){
2008 /* int iCid = sqlite3_column_int(pXInfo, 0); */
2009 const char *zCol
= (const char*)sqlite3_column_text(pXInfo
, 2);
2010 const char *zDesc
= sqlite3_column_int(pXInfo
, 3) ? " DESC" : "";
2011 z
= rbuMPrintf(p
, "%z%s\"%w\"%s", z
, zSep
, zCol
, zDesc
);
2015 z
= rbuMPrintf(p
, "%z)", z
);
2016 rbuFinalize(p
, pXInfo
);
2022 ** This function creates the second imposter table used when writing to
2023 ** a table b-tree where the table has an external primary key. If the
2024 ** iterator passed as the second argument does not currently point to
2025 ** a table (not index) with an external primary key, this function is a
2028 ** Assuming the iterator does point to a table with an external PK, this
2029 ** function creates a WITHOUT ROWID imposter table named "rbu_imposter2"
2030 ** used to access that PK index. For example, if the target table is
2031 ** declared as follows:
2033 ** CREATE TABLE t1(a, b TEXT, c REAL, PRIMARY KEY(b, c));
2035 ** then the imposter table schema is:
2037 ** CREATE TABLE rbu_imposter2(c1 TEXT, c2 REAL, id INTEGER) WITHOUT ROWID;
2040 static void rbuCreateImposterTable2(sqlite3rbu
*p
, RbuObjIter
*pIter
){
2041 if( p
->rc
==SQLITE_OK
&& pIter
->eType
==RBU_PK_EXTERNAL
){
2042 int tnum
= pIter
->iPkTnum
; /* Root page of PK index */
2043 sqlite3_stmt
*pQuery
= 0; /* SELECT name ... WHERE rootpage = $tnum */
2044 const char *zIdx
= 0; /* Name of PK index */
2045 sqlite3_stmt
*pXInfo
= 0; /* PRAGMA main.index_xinfo = $zIdx */
2046 const char *zComma
= "";
2047 char *zCols
= 0; /* Used to build up list of table cols */
2048 char *zPk
= 0; /* Used to build up table PK declaration */
2050 /* Figure out the name of the primary key index for the current table.
2051 ** This is needed for the argument to "PRAGMA index_xinfo". Set
2052 ** zIdx to point to a nul-terminated string containing this name. */
2053 p
->rc
= prepareAndCollectError(p
->dbMain
, &pQuery
, &p
->zErrmsg
,
2054 "SELECT name FROM sqlite_schema WHERE rootpage = ?"
2056 if( p
->rc
==SQLITE_OK
){
2057 sqlite3_bind_int(pQuery
, 1, tnum
);
2058 if( SQLITE_ROW
==sqlite3_step(pQuery
) ){
2059 zIdx
= (const char*)sqlite3_column_text(pQuery
, 0);
2063 p
->rc
= prepareFreeAndCollectError(p
->dbMain
, &pXInfo
, &p
->zErrmsg
,
2064 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx
)
2067 rbuFinalize(p
, pQuery
);
2069 while( p
->rc
==SQLITE_OK
&& SQLITE_ROW
==sqlite3_step(pXInfo
) ){
2070 int bKey
= sqlite3_column_int(pXInfo
, 5);
2072 int iCid
= sqlite3_column_int(pXInfo
, 1);
2073 int bDesc
= sqlite3_column_int(pXInfo
, 3);
2074 const char *zCollate
= (const char*)sqlite3_column_text(pXInfo
, 4);
2075 zCols
= rbuMPrintf(p
, "%z%sc%d %s COLLATE %Q", zCols
, zComma
,
2076 iCid
, pIter
->azTblType
[iCid
], zCollate
2078 zPk
= rbuMPrintf(p
, "%z%sc%d%s", zPk
, zComma
, iCid
, bDesc
?" DESC":"");
2082 zCols
= rbuMPrintf(p
, "%z, id INTEGER", zCols
);
2083 rbuFinalize(p
, pXInfo
);
2085 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER
, p
->dbMain
, "main", 1, tnum
);
2086 rbuMPrintfExec(p
, p
->dbMain
,
2087 "CREATE TABLE rbu_imposter2(%z, PRIMARY KEY(%z)) WITHOUT ROWID",
2090 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER
, p
->dbMain
, "main", 0, 0);
2095 ** If an error has already occurred when this function is called, it
2096 ** immediately returns zero (without doing any work). Or, if an error
2097 ** occurs during the execution of this function, it sets the error code
2098 ** in the sqlite3rbu object indicated by the first argument and returns
2101 ** The iterator passed as the second argument is guaranteed to point to
2102 ** a table (not an index) when this function is called. This function
2103 ** attempts to create any imposter table required to write to the main
2104 ** table b-tree of the table before returning. Non-zero is returned if
2105 ** an imposter table are created, or zero otherwise.
2107 ** An imposter table is required in all cases except RBU_PK_VTAB. Only
2108 ** virtual tables are written to directly. The imposter table has the
2109 ** same schema as the actual target table (less any UNIQUE constraints).
2110 ** More precisely, the "same schema" means the same columns, types,
2111 ** collation sequences. For tables that do not have an external PRIMARY
2112 ** KEY, it also means the same PRIMARY KEY declaration.
2114 static void rbuCreateImposterTable(sqlite3rbu
*p
, RbuObjIter
*pIter
){
2115 if( p
->rc
==SQLITE_OK
&& pIter
->eType
!=RBU_PK_VTAB
){
2116 int tnum
= pIter
->iTnum
;
2117 const char *zComma
= "";
2120 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER
, p
->dbMain
, "main", 0, 1);
2122 for(iCol
=0; p
->rc
==SQLITE_OK
&& iCol
<pIter
->nTblCol
; iCol
++){
2123 const char *zPk
= "";
2124 const char *zCol
= pIter
->azTblCol
[iCol
];
2125 const char *zColl
= 0;
2127 p
->rc
= sqlite3_table_column_metadata(
2128 p
->dbMain
, "main", pIter
->zTbl
, zCol
, 0, &zColl
, 0, 0, 0
2131 if( pIter
->eType
==RBU_PK_IPK
&& pIter
->abTblPk
[iCol
] ){
2132 /* If the target table column is an "INTEGER PRIMARY KEY", add
2133 ** "PRIMARY KEY" to the imposter table column declaration. */
2134 zPk
= "PRIMARY KEY ";
2136 zSql
= rbuMPrintf(p
, "%z%s\"%w\" %s %sCOLLATE %Q%s",
2137 zSql
, zComma
, zCol
, pIter
->azTblType
[iCol
], zPk
, zColl
,
2138 (pIter
->abNotNull
[iCol
] ? " NOT NULL" : "")
2143 if( pIter
->eType
==RBU_PK_WITHOUT_ROWID
){
2144 char *zPk
= rbuWithoutRowidPK(p
, pIter
);
2146 zSql
= rbuMPrintf(p
, "%z, %z", zSql
, zPk
);
2150 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER
, p
->dbMain
, "main", 1, tnum
);
2151 rbuMPrintfExec(p
, p
->dbMain
, "CREATE TABLE \"rbu_imp_%w\"(%z)%s",
2153 (pIter
->eType
==RBU_PK_WITHOUT_ROWID
? " WITHOUT ROWID" : "")
2155 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER
, p
->dbMain
, "main", 0, 0);
2160 ** Prepare a statement used to insert rows into the "rbu_tmp_xxx" table.
2161 ** Specifically a statement of the form:
2163 ** INSERT INTO rbu_tmp_xxx VALUES(?, ?, ? ...);
2165 ** The number of bound variables is equal to the number of columns in
2166 ** the target table, plus one (for the rbu_control column), plus one more
2167 ** (for the rbu_rowid column) if the target table is an implicit IPK or
2170 static void rbuObjIterPrepareTmpInsert(
2173 const char *zCollist
,
2174 const char *zRbuRowid
2176 int bRbuRowid
= (pIter
->eType
==RBU_PK_EXTERNAL
|| pIter
->eType
==RBU_PK_NONE
);
2177 char *zBind
= rbuObjIterGetBindlist(p
, pIter
->nTblCol
+ 1 + bRbuRowid
);
2179 assert( pIter
->pTmpInsert
==0 );
2180 p
->rc
= prepareFreeAndCollectError(
2181 p
->dbRbu
, &pIter
->pTmpInsert
, &p
->zErrmsg
, sqlite3_mprintf(
2182 "INSERT INTO %s.'rbu_tmp_%q'(rbu_control,%s%s) VALUES(%z)",
2183 p
->zStateDb
, pIter
->zDataTbl
, zCollist
, zRbuRowid
, zBind
2188 static void rbuTmpInsertFunc(
2189 sqlite3_context
*pCtx
,
2191 sqlite3_value
**apVal
2193 sqlite3rbu
*p
= sqlite3_user_data(pCtx
);
2197 assert( sqlite3_value_int(apVal
[0])!=0
2198 || p
->objiter
.eType
==RBU_PK_EXTERNAL
2199 || p
->objiter
.eType
==RBU_PK_NONE
2201 if( sqlite3_value_int(apVal
[0])!=0 ){
2202 p
->nPhaseOneStep
+= p
->objiter
.nIndex
;
2205 for(i
=0; rc
==SQLITE_OK
&& i
<nVal
; i
++){
2206 rc
= sqlite3_bind_value(p
->objiter
.pTmpInsert
, i
+1, apVal
[i
]);
2208 if( rc
==SQLITE_OK
){
2209 sqlite3_step(p
->objiter
.pTmpInsert
);
2210 rc
= sqlite3_reset(p
->objiter
.pTmpInsert
);
2213 if( rc
!=SQLITE_OK
){
2214 sqlite3_result_error_code(pCtx
, rc
);
2218 static char *rbuObjIterGetIndexWhere(sqlite3rbu
*p
, RbuObjIter
*pIter
){
2219 sqlite3_stmt
*pStmt
= 0;
2223 assert( pIter
->zIdxSql
==0 && pIter
->nIdxCol
==0 && pIter
->aIdxCol
==0 );
2225 if( rc
==SQLITE_OK
){
2226 rc
= prepareAndCollectError(p
->dbMain
, &pStmt
, &p
->zErrmsg
,
2227 "SELECT trim(sql) FROM sqlite_schema WHERE type='index' AND name=?"
2230 if( rc
==SQLITE_OK
){
2232 rc
= sqlite3_bind_text(pStmt
, 1, pIter
->zIdx
, -1, SQLITE_STATIC
);
2233 if( rc
==SQLITE_OK
&& SQLITE_ROW
==sqlite3_step(pStmt
) ){
2234 char *zSql
= (char*)sqlite3_column_text(pStmt
, 0);
2236 pIter
->zIdxSql
= zSql
= rbuStrndup(zSql
, &rc
);
2239 int nParen
= 0; /* Number of open parenthesis */
2243 for(i
=0; zSql
[i
]; i
++){
2246 /* If necessary, grow the pIter->aIdxCol[] array */
2247 if( iIdxCol
==nIdxAlloc
){
2248 RbuSpan
*aIdxCol
= (RbuSpan
*)sqlite3_realloc(
2249 pIter
->aIdxCol
, (nIdxAlloc
+16)*sizeof(RbuSpan
)
2255 pIter
->aIdxCol
= aIdxCol
;
2261 assert( iIdxCol
==0 );
2262 pIter
->aIdxCol
[0].zSpan
= &zSql
[i
+1];
2269 int nSpan
= &zSql
[i
] - pIter
->aIdxCol
[iIdxCol
].zSpan
;
2270 pIter
->aIdxCol
[iIdxCol
++].nSpan
= nSpan
;
2274 }else if( c
==',' && nParen
==1 ){
2275 int nSpan
= &zSql
[i
] - pIter
->aIdxCol
[iIdxCol
].zSpan
;
2276 pIter
->aIdxCol
[iIdxCol
++].nSpan
= nSpan
;
2277 pIter
->aIdxCol
[iIdxCol
].zSpan
= &zSql
[i
+1];
2278 }else if( c
=='"' || c
=='\'' || c
=='`' ){
2281 if( zSql
[i
+1]!=c
) break;
2287 if( zSql
[i
]==']' ) break;
2289 }else if( c
=='-' && zSql
[i
+1]=='-' ){
2290 for(i
=i
+2; zSql
[i
] && zSql
[i
]!='\n'; i
++);
2291 if( zSql
[i
]=='\0' ) break;
2292 }else if( c
=='/' && zSql
[i
+1]=='*' ){
2293 for(i
=i
+2; zSql
[i
] && (zSql
[i
]!='*' || zSql
[i
+1]!='/'); i
++);
2294 if( zSql
[i
]=='\0' ) break;
2299 zRet
= rbuStrndup(&zSql
[i
], &rc
);
2301 pIter
->nIdxCol
= iIdxCol
;
2305 rc2
= sqlite3_finalize(pStmt
);
2306 if( rc
==SQLITE_OK
) rc
= rc2
;
2314 ** Ensure that the SQLite statement handles required to update the
2315 ** target database object currently indicated by the iterator passed
2316 ** as the second argument are available.
2318 static int rbuObjIterPrepareAll(
2321 int nOffset
/* Add "LIMIT -1 OFFSET $nOffset" to SELECT */
2323 assert( pIter
->bCleanup
==0 );
2324 if( pIter
->pSelect
==0 && rbuObjIterCacheTableInfo(p
, pIter
)==SQLITE_OK
){
2325 const int tnum
= pIter
->iTnum
;
2326 char *zCollist
= 0; /* List of indexed columns */
2327 char **pz
= &p
->zErrmsg
;
2328 const char *zIdx
= pIter
->zIdx
;
2332 zLimit
= sqlite3_mprintf(" LIMIT -1 OFFSET %d", nOffset
);
2333 if( !zLimit
) p
->rc
= SQLITE_NOMEM
;
2337 const char *zTbl
= pIter
->zTbl
;
2338 char *zImposterCols
= 0; /* Columns for imposter table */
2339 char *zImposterPK
= 0; /* Primary key declaration for imposter */
2340 char *zWhere
= 0; /* WHERE clause on PK columns */
2345 assert( pIter
->eType
!=RBU_PK_VTAB
);
2346 zPart
= rbuObjIterGetIndexWhere(p
, pIter
);
2347 zCollist
= rbuObjIterGetIndexCols(
2348 p
, pIter
, &zImposterCols
, &zImposterPK
, &zWhere
, &nBind
2350 zBind
= rbuObjIterGetBindlist(p
, nBind
);
2352 /* Create the imposter table used to write to this index. */
2353 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER
, p
->dbMain
, "main", 0, 1);
2354 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER
, p
->dbMain
, "main", 1,tnum
);
2355 rbuMPrintfExec(p
, p
->dbMain
,
2356 "CREATE TABLE \"rbu_imp_%w\"( %s, PRIMARY KEY( %s ) ) WITHOUT ROWID",
2357 zTbl
, zImposterCols
, zImposterPK
2359 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER
, p
->dbMain
, "main", 0, 0);
2361 /* Create the statement to insert index entries */
2362 pIter
->nCol
= nBind
;
2363 if( p
->rc
==SQLITE_OK
){
2364 p
->rc
= prepareFreeAndCollectError(
2365 p
->dbMain
, &pIter
->pInsert
, &p
->zErrmsg
,
2366 sqlite3_mprintf("INSERT INTO \"rbu_imp_%w\" VALUES(%s)", zTbl
, zBind
)
2370 /* And to delete index entries */
2371 if( rbuIsVacuum(p
)==0 && p
->rc
==SQLITE_OK
){
2372 p
->rc
= prepareFreeAndCollectError(
2373 p
->dbMain
, &pIter
->pDelete
, &p
->zErrmsg
,
2374 sqlite3_mprintf("DELETE FROM \"rbu_imp_%w\" WHERE %s", zTbl
, zWhere
)
2378 /* Create the SELECT statement to read keys in sorted order */
2379 if( p
->rc
==SQLITE_OK
){
2381 if( rbuIsVacuum(p
) ){
2384 zStart
= rbuVacuumIndexStart(p
, pIter
);
2386 sqlite3_free(zLimit
);
2391 zSql
= sqlite3_mprintf(
2392 "SELECT %s, 0 AS rbu_control FROM '%q' %s %s %s ORDER BY %s%s",
2396 (zStart
? (zPart
? "AND" : "WHERE") : ""), zStart
,
2399 sqlite3_free(zStart
);
2402 if( pIter
->eType
==RBU_PK_EXTERNAL
|| pIter
->eType
==RBU_PK_NONE
){
2403 zSql
= sqlite3_mprintf(
2404 "SELECT %s, rbu_control FROM %s.'rbu_tmp_%q' %s ORDER BY %s%s",
2405 zCollist
, p
->zStateDb
, pIter
->zDataTbl
,
2406 zPart
, zCollist
, zLimit
2409 zSql
= sqlite3_mprintf(
2410 "SELECT %s, rbu_control FROM %s.'rbu_tmp_%q' %s "
2412 "SELECT %s, rbu_control FROM '%q' "
2413 "%s %s typeof(rbu_control)='integer' AND rbu_control!=1 "
2415 zCollist
, p
->zStateDb
, pIter
->zDataTbl
, zPart
,
2416 zCollist
, pIter
->zDataTbl
,
2418 (zPart
? "AND" : "WHERE"),
2422 if( p
->rc
==SQLITE_OK
){
2423 p
->rc
= prepareFreeAndCollectError(p
->dbRbu
,&pIter
->pSelect
,pz
,zSql
);
2429 sqlite3_free(zImposterCols
);
2430 sqlite3_free(zImposterPK
);
2431 sqlite3_free(zWhere
);
2432 sqlite3_free(zBind
);
2433 sqlite3_free(zPart
);
2435 int bRbuRowid
= (pIter
->eType
==RBU_PK_VTAB
)
2436 ||(pIter
->eType
==RBU_PK_NONE
)
2437 ||(pIter
->eType
==RBU_PK_EXTERNAL
&& rbuIsVacuum(p
));
2438 const char *zTbl
= pIter
->zTbl
; /* Table this step applies to */
2439 const char *zWrite
; /* Imposter table name */
2441 char *zBindings
= rbuObjIterGetBindlist(p
, pIter
->nTblCol
+ bRbuRowid
);
2442 char *zWhere
= rbuObjIterGetWhere(p
, pIter
);
2443 char *zOldlist
= rbuObjIterGetOldlist(p
, pIter
, "old");
2444 char *zNewlist
= rbuObjIterGetOldlist(p
, pIter
, "new");
2446 zCollist
= rbuObjIterGetCollist(p
, pIter
);
2447 pIter
->nCol
= pIter
->nTblCol
;
2449 /* Create the imposter table or tables (if required). */
2450 rbuCreateImposterTable(p
, pIter
);
2451 rbuCreateImposterTable2(p
, pIter
);
2452 zWrite
= (pIter
->eType
==RBU_PK_VTAB
? "" : "rbu_imp_");
2454 /* Create the INSERT statement to write to the target PK b-tree */
2455 if( p
->rc
==SQLITE_OK
){
2456 p
->rc
= prepareFreeAndCollectError(p
->dbMain
, &pIter
->pInsert
, pz
,
2458 "INSERT INTO \"%s%w\"(%s%s) VALUES(%s)",
2459 zWrite
, zTbl
, zCollist
, (bRbuRowid
? ", _rowid_" : ""), zBindings
2464 /* Create the DELETE statement to write to the target PK b-tree.
2465 ** Because it only performs INSERT operations, this is not required for
2466 ** an rbu vacuum handle. */
2467 if( rbuIsVacuum(p
)==0 && p
->rc
==SQLITE_OK
){
2468 p
->rc
= prepareFreeAndCollectError(p
->dbMain
, &pIter
->pDelete
, pz
,
2470 "DELETE FROM \"%s%w\" WHERE %s", zWrite
, zTbl
, zWhere
2475 if( rbuIsVacuum(p
)==0 && pIter
->abIndexed
){
2476 const char *zRbuRowid
= "";
2477 if( pIter
->eType
==RBU_PK_EXTERNAL
|| pIter
->eType
==RBU_PK_NONE
){
2478 zRbuRowid
= ", rbu_rowid";
2481 /* Create the rbu_tmp_xxx table and the triggers to populate it. */
2482 rbuMPrintfExec(p
, p
->dbRbu
,
2483 "CREATE TABLE IF NOT EXISTS %s.'rbu_tmp_%q' AS "
2484 "SELECT *%s FROM '%q' WHERE 0;"
2485 , p
->zStateDb
, pIter
->zDataTbl
2486 , (pIter
->eType
==RBU_PK_EXTERNAL
? ", 0 AS rbu_rowid" : "")
2490 rbuMPrintfExec(p
, p
->dbMain
,
2491 "CREATE TEMP TRIGGER rbu_delete_tr BEFORE DELETE ON \"%s%w\" "
2493 " SELECT rbu_tmp_insert(3, %s);"
2496 "CREATE TEMP TRIGGER rbu_update1_tr BEFORE UPDATE ON \"%s%w\" "
2498 " SELECT rbu_tmp_insert(3, %s);"
2501 "CREATE TEMP TRIGGER rbu_update2_tr AFTER UPDATE ON \"%s%w\" "
2503 " SELECT rbu_tmp_insert(4, %s);"
2505 zWrite
, zTbl
, zOldlist
,
2506 zWrite
, zTbl
, zOldlist
,
2507 zWrite
, zTbl
, zNewlist
2510 if( pIter
->eType
==RBU_PK_EXTERNAL
|| pIter
->eType
==RBU_PK_NONE
){
2511 rbuMPrintfExec(p
, p
->dbMain
,
2512 "CREATE TEMP TRIGGER rbu_insert_tr AFTER INSERT ON \"%s%w\" "
2514 " SELECT rbu_tmp_insert(0, %s);"
2516 zWrite
, zTbl
, zNewlist
2520 rbuObjIterPrepareTmpInsert(p
, pIter
, zCollist
, zRbuRowid
);
2523 /* Create the SELECT statement to read keys from data_xxx */
2524 if( p
->rc
==SQLITE_OK
){
2525 const char *zRbuRowid
= "";
2529 zRbuRowid
= rbuIsVacuum(p
) ? ",_rowid_ " : ",rbu_rowid";
2532 if( rbuIsVacuum(p
) ){
2534 zStart
= rbuVacuumTableStart(p
, pIter
, bRbuRowid
, zWrite
);
2536 sqlite3_free(zLimit
);
2541 zOrder
= rbuMPrintf(p
, "_rowid_");
2543 zOrder
= rbuObjIterGetPkList(p
, pIter
, "", ", ", "");
2547 if( p
->rc
==SQLITE_OK
){
2548 p
->rc
= prepareFreeAndCollectError(p
->dbRbu
, &pIter
->pSelect
, pz
,
2550 "SELECT %s,%s rbu_control%s FROM '%q'%s %s %s %s",
2552 (rbuIsVacuum(p
) ? "0 AS " : ""),
2554 pIter
->zDataTbl
, (zStart
? zStart
: ""),
2555 (zOrder
? "ORDER BY" : ""), zOrder
,
2560 sqlite3_free(zStart
);
2561 sqlite3_free(zOrder
);
2564 sqlite3_free(zWhere
);
2565 sqlite3_free(zOldlist
);
2566 sqlite3_free(zNewlist
);
2567 sqlite3_free(zBindings
);
2569 sqlite3_free(zCollist
);
2570 sqlite3_free(zLimit
);
2577 ** Set output variable *ppStmt to point to an UPDATE statement that may
2578 ** be used to update the imposter table for the main table b-tree of the
2579 ** table object that pIter currently points to, assuming that the
2580 ** rbu_control column of the data_xyz table contains zMask.
2582 ** If the zMask string does not specify any columns to update, then this
2583 ** is not an error. Output variable *ppStmt is set to NULL in this case.
2585 static int rbuGetUpdateStmt(
2586 sqlite3rbu
*p
, /* RBU handle */
2587 RbuObjIter
*pIter
, /* Object iterator */
2588 const char *zMask
, /* rbu_control value ('x.x.') */
2589 sqlite3_stmt
**ppStmt
/* OUT: UPDATE statement handle */
2592 RbuUpdateStmt
*pUp
= 0;
2595 /* In case an error occurs */
2598 /* Search for an existing statement. If one is found, shift it to the front
2599 ** of the LRU queue and return immediately. Otherwise, leave nUp pointing
2600 ** to the number of statements currently in the cache and pUp to the
2601 ** last object in the list. */
2602 for(pp
=&pIter
->pRbuUpdate
; *pp
; pp
=&((*pp
)->pNext
)){
2604 if( strcmp(pUp
->zMask
, zMask
)==0 ){
2606 pUp
->pNext
= pIter
->pRbuUpdate
;
2607 pIter
->pRbuUpdate
= pUp
;
2608 *ppStmt
= pUp
->pUpdate
;
2613 assert( pUp
==0 || pUp
->pNext
==0 );
2615 if( nUp
>=SQLITE_RBU_UPDATE_CACHESIZE
){
2616 for(pp
=&pIter
->pRbuUpdate
; *pp
!=pUp
; pp
=&((*pp
)->pNext
));
2618 sqlite3_finalize(pUp
->pUpdate
);
2621 pUp
= (RbuUpdateStmt
*)rbuMalloc(p
, sizeof(RbuUpdateStmt
)+pIter
->nTblCol
+1);
2625 char *zWhere
= rbuObjIterGetWhere(p
, pIter
);
2626 char *zSet
= rbuObjIterGetSetlist(p
, pIter
, zMask
);
2629 pUp
->zMask
= (char*)&pUp
[1];
2630 memcpy(pUp
->zMask
, zMask
, pIter
->nTblCol
);
2631 pUp
->pNext
= pIter
->pRbuUpdate
;
2632 pIter
->pRbuUpdate
= pUp
;
2635 const char *zPrefix
= "";
2637 if( pIter
->eType
!=RBU_PK_VTAB
) zPrefix
= "rbu_imp_";
2638 zUpdate
= sqlite3_mprintf("UPDATE \"%s%w\" SET %s WHERE %s",
2639 zPrefix
, pIter
->zTbl
, zSet
, zWhere
2641 p
->rc
= prepareFreeAndCollectError(
2642 p
->dbMain
, &pUp
->pUpdate
, &p
->zErrmsg
, zUpdate
2644 *ppStmt
= pUp
->pUpdate
;
2646 sqlite3_free(zWhere
);
2653 static sqlite3
*rbuOpenDbhandle(
2659 if( p
->rc
==SQLITE_OK
){
2660 const int flags
= SQLITE_OPEN_READWRITE
|SQLITE_OPEN_CREATE
|SQLITE_OPEN_URI
;
2661 p
->rc
= sqlite3_open_v2(zName
, &db
, flags
, bUseVfs
? p
->zVfsName
: 0);
2663 p
->zErrmsg
= sqlite3_mprintf("%s", sqlite3_errmsg(db
));
2672 ** Free an RbuState object allocated by rbuLoadState().
2674 static void rbuFreeState(RbuState
*p
){
2676 sqlite3_free(p
->zTbl
);
2677 sqlite3_free(p
->zDataTbl
);
2678 sqlite3_free(p
->zIdx
);
2684 ** Allocate an RbuState object and load the contents of the rbu_state
2685 ** table into it. Return a pointer to the new object. It is the
2686 ** responsibility of the caller to eventually free the object using
2689 ** If an error occurs, leave an error code and message in the rbu handle
2692 static RbuState
*rbuLoadState(sqlite3rbu
*p
){
2694 sqlite3_stmt
*pStmt
= 0;
2698 pRet
= (RbuState
*)rbuMalloc(p
, sizeof(RbuState
));
2699 if( pRet
==0 ) return 0;
2701 rc
= prepareFreeAndCollectError(p
->dbRbu
, &pStmt
, &p
->zErrmsg
,
2702 sqlite3_mprintf("SELECT k, v FROM %s.rbu_state", p
->zStateDb
)
2704 while( rc
==SQLITE_OK
&& SQLITE_ROW
==sqlite3_step(pStmt
) ){
2705 switch( sqlite3_column_int(pStmt
, 0) ){
2706 case RBU_STATE_STAGE
:
2707 pRet
->eStage
= sqlite3_column_int(pStmt
, 1);
2708 if( pRet
->eStage
!=RBU_STAGE_OAL
2709 && pRet
->eStage
!=RBU_STAGE_MOVE
2710 && pRet
->eStage
!=RBU_STAGE_CKPT
2712 p
->rc
= SQLITE_CORRUPT
;
2717 pRet
->zTbl
= rbuStrndup((char*)sqlite3_column_text(pStmt
, 1), &rc
);
2721 pRet
->zIdx
= rbuStrndup((char*)sqlite3_column_text(pStmt
, 1), &rc
);
2725 pRet
->nRow
= sqlite3_column_int(pStmt
, 1);
2728 case RBU_STATE_PROGRESS
:
2729 pRet
->nProgress
= sqlite3_column_int64(pStmt
, 1);
2732 case RBU_STATE_CKPT
:
2733 pRet
->iWalCksum
= sqlite3_column_int64(pStmt
, 1);
2736 case RBU_STATE_COOKIE
:
2737 pRet
->iCookie
= (u32
)sqlite3_column_int64(pStmt
, 1);
2740 case RBU_STATE_OALSZ
:
2741 pRet
->iOalSz
= sqlite3_column_int64(pStmt
, 1);
2744 case RBU_STATE_PHASEONESTEP
:
2745 pRet
->nPhaseOneStep
= sqlite3_column_int64(pStmt
, 1);
2748 case RBU_STATE_DATATBL
:
2749 pRet
->zDataTbl
= rbuStrndup((char*)sqlite3_column_text(pStmt
, 1), &rc
);
2753 rc
= SQLITE_CORRUPT
;
2757 rc2
= sqlite3_finalize(pStmt
);
2758 if( rc
==SQLITE_OK
) rc
= rc2
;
2766 ** Open the database handle and attach the RBU database as "rbu". If an
2767 ** error occurs, leave an error code and message in the RBU handle.
2769 ** If argument dbMain is not NULL, then it is a database handle already
2770 ** open on the target database. Use this handle instead of opening a new
2773 static void rbuOpenDatabase(sqlite3rbu
*p
, sqlite3
*dbMain
, int *pbRetry
){
2774 assert( p
->rc
|| (p
->dbMain
==0 && p
->dbRbu
==0) );
2775 assert( p
->rc
|| rbuIsVacuum(p
) || p
->zTarget
!=0 );
2776 assert( dbMain
==0 || rbuIsVacuum(p
)==0 );
2778 /* Open the RBU database */
2779 p
->dbRbu
= rbuOpenDbhandle(p
, p
->zRbu
, 1);
2782 if( p
->rc
==SQLITE_OK
&& rbuIsVacuum(p
) ){
2783 sqlite3_file_control(p
->dbRbu
, "main", SQLITE_FCNTL_RBUCNT
, (void*)p
);
2785 const char *zFile
= sqlite3_db_filename(p
->dbRbu
, "main");
2786 p
->zState
= rbuMPrintf(p
, "file:///%s-vacuum?modeof=%s", zFile
, zFile
);
2790 /* If using separate RBU and state databases, attach the state database to
2791 ** the RBU db handle now. */
2793 rbuMPrintfExec(p
, p
->dbRbu
, "ATTACH %Q AS stat", p
->zState
);
2794 memcpy(p
->zStateDb
, "stat", 4);
2796 memcpy(p
->zStateDb
, "main", 4);
2800 if( p
->rc
==SQLITE_OK
&& rbuIsVacuum(p
) ){
2801 p
->rc
= sqlite3_exec(p
->dbRbu
, "BEGIN", 0, 0, 0);
2805 /* If it has not already been created, create the rbu_state table */
2806 rbuMPrintfExec(p
, p
->dbRbu
, RBU_CREATE_STATE
, p
->zStateDb
);
2809 if( rbuIsVacuum(p
) ){
2810 if( p
->rc
==SQLITE_OK
){
2813 sqlite3_stmt
*pCnt
= 0;
2814 p
->rc
= prepareAndCollectError(p
->dbRbu
, &pCnt
, &p
->zErrmsg
,
2815 "SELECT count(*) FROM stat.sqlite_schema"
2817 if( p
->rc
==SQLITE_OK
2818 && sqlite3_step(pCnt
)==SQLITE_ROW
2819 && 1==sqlite3_column_int(pCnt
, 0)
2823 rc2
= sqlite3_finalize(pCnt
);
2824 if( p
->rc
==SQLITE_OK
) p
->rc
= rc2
;
2826 if( p
->rc
==SQLITE_OK
&& bOk
==0 ){
2827 p
->rc
= SQLITE_ERROR
;
2828 p
->zErrmsg
= sqlite3_mprintf("invalid state database");
2831 if( p
->rc
==SQLITE_OK
){
2832 p
->rc
= sqlite3_exec(p
->dbRbu
, "COMMIT", 0, 0, 0);
2838 if( p
->rc
==SQLITE_OK
&& rbuIsVacuum(p
) ){
2843 rc
= sqlite3_file_control(p
->dbRbu
, "main", SQLITE_FCNTL_RBUCNT
, (void*)p
);
2844 if( rc
!=SQLITE_NOTFOUND
) p
->rc
= rc
;
2845 if( p
->eStage
>=RBU_STAGE_MOVE
){
2848 RbuState
*pState
= rbuLoadState(p
);
2850 bOpen
= (pState
->eStage
>=RBU_STAGE_MOVE
);
2851 rbuFreeState(pState
);
2854 if( bOpen
) p
->dbMain
= rbuOpenDbhandle(p
, p
->zRbu
, p
->nRbu
<=1);
2858 if( p
->rc
==SQLITE_OK
&& p
->dbMain
==0 ){
2859 if( !rbuIsVacuum(p
) ){
2860 p
->dbMain
= rbuOpenDbhandle(p
, p
->zTarget
, 1);
2861 }else if( p
->pRbuFd
->pWalFd
){
2863 p
->pRbuFd
->bNolock
= 0;
2864 sqlite3_close(p
->dbRbu
);
2865 sqlite3_close(p
->dbMain
);
2871 p
->rc
= SQLITE_ERROR
;
2872 p
->zErrmsg
= sqlite3_mprintf("cannot vacuum wal mode database");
2876 if( strlen(p
->zRbu
)>=5 && 0==memcmp("file:", p
->zRbu
, 5) ){
2877 zExtra
= &p
->zRbu
[5];
2879 if( *zExtra
++=='?' ) break;
2881 if( *zExtra
=='\0' ) zExtra
= 0;
2884 zTarget
= sqlite3_mprintf("file:%s-vactmp?rbu_memory=1%s%s",
2885 sqlite3_db_filename(p
->dbRbu
, "main"),
2886 (zExtra
==0 ? "" : "&"), (zExtra
==0 ? "" : zExtra
)
2890 p
->rc
= SQLITE_NOMEM
;
2893 p
->dbMain
= rbuOpenDbhandle(p
, zTarget
, p
->nRbu
<=1);
2894 sqlite3_free(zTarget
);
2898 if( p
->rc
==SQLITE_OK
){
2899 p
->rc
= sqlite3_create_function(p
->dbMain
,
2900 "rbu_tmp_insert", -1, SQLITE_UTF8
, (void*)p
, rbuTmpInsertFunc
, 0, 0
2904 if( p
->rc
==SQLITE_OK
){
2905 p
->rc
= sqlite3_create_function(p
->dbMain
,
2906 "rbu_fossil_delta", 2, SQLITE_UTF8
, 0, rbuFossilDeltaFunc
, 0, 0
2910 if( p
->rc
==SQLITE_OK
){
2911 p
->rc
= sqlite3_create_function(p
->dbRbu
,
2912 "rbu_target_name", -1, SQLITE_UTF8
, (void*)p
, rbuTargetNameFunc
, 0, 0
2916 if( p
->rc
==SQLITE_OK
){
2917 p
->rc
= sqlite3_file_control(p
->dbMain
, "main", SQLITE_FCNTL_RBU
, (void*)p
);
2919 rbuMPrintfExec(p
, p
->dbMain
, "SELECT * FROM sqlite_schema");
2921 /* Mark the database file just opened as an RBU target database. If
2922 ** this call returns SQLITE_NOTFOUND, then the RBU vfs is not in use.
2923 ** This is an error. */
2924 if( p
->rc
==SQLITE_OK
){
2925 p
->rc
= sqlite3_file_control(p
->dbMain
, "main", SQLITE_FCNTL_RBU
, (void*)p
);
2928 if( p
->rc
==SQLITE_NOTFOUND
){
2929 p
->rc
= SQLITE_ERROR
;
2930 p
->zErrmsg
= sqlite3_mprintf("rbu vfs not found");
2935 ** This routine is a copy of the sqlite3FileSuffix3() routine from the core.
2936 ** It is a no-op unless SQLITE_ENABLE_8_3_NAMES is defined.
2938 ** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
2939 ** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and
2940 ** if filename in z[] has a suffix (a.k.a. "extension") that is longer than
2941 ** three characters, then shorten the suffix on z[] to be the last three
2942 ** characters of the original suffix.
2944 ** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
2945 ** do the suffix shortening regardless of URI parameter.
2949 ** test.db-journal => test.nal
2950 ** test.db-wal => test.wal
2951 ** test.db-shm => test.shm
2952 ** test.db-mj7f3319fa => test.9fa
2954 static void rbuFileSuffix3(const char *zBase
, char *z
){
2955 #ifdef SQLITE_ENABLE_8_3_NAMES
2956 #if SQLITE_ENABLE_8_3_NAMES<2
2957 if( sqlite3_uri_boolean(zBase
, "8_3_names", 0) )
2961 sz
= (int)strlen(z
)&0xffffff;
2962 for(i
=sz
-1; i
>0 && z
[i
]!='/' && z
[i
]!='.'; i
--){}
2963 if( z
[i
]=='.' && sz
>i
+4 ) memmove(&z
[i
+1], &z
[sz
-3], 4);
2969 ** Return the current wal-index header checksum for the target database
2970 ** as a 64-bit integer.
2972 ** The checksum is store in the first page of xShmMap memory as an 8-byte
2973 ** blob starting at byte offset 40.
2975 static i64
rbuShmChecksum(sqlite3rbu
*p
){
2977 if( p
->rc
==SQLITE_OK
){
2978 sqlite3_file
*pDb
= p
->pTargetFd
->pReal
;
2980 p
->rc
= pDb
->pMethods
->xShmMap(pDb
, 0, 32*1024, 0, (void volatile**)&ptr
);
2981 if( p
->rc
==SQLITE_OK
){
2982 iRet
= ((i64
)ptr
[10] << 32) + ptr
[11];
2989 ** This function is called as part of initializing or reinitializing an
2990 ** incremental checkpoint.
2992 ** It populates the sqlite3rbu.aFrame[] array with the set of
2993 ** (wal frame -> db page) copy operations required to checkpoint the
2994 ** current wal file, and obtains the set of shm locks required to safely
2995 ** perform the copy operations directly on the file-system.
2997 ** If argument pState is not NULL, then the incremental checkpoint is
2998 ** being resumed. In this case, if the checksum of the wal-index-header
2999 ** following recovery is not the same as the checksum saved in the RbuState
3000 ** object, then the rbu handle is set to DONE state. This occurs if some
3001 ** other client appends a transaction to the wal file in the middle of
3002 ** an incremental checkpoint.
3004 static void rbuSetupCheckpoint(sqlite3rbu
*p
, RbuState
*pState
){
3006 /* If pState is NULL, then the wal file may not have been opened and
3007 ** recovered. Running a read-statement here to ensure that doing so
3008 ** does not interfere with the "capture" process below. */
3011 if( p
->rc
==SQLITE_OK
){
3012 p
->rc
= sqlite3_exec(p
->dbMain
, "SELECT * FROM sqlite_schema", 0, 0, 0);
3016 /* Assuming no error has occurred, run a "restart" checkpoint with the
3017 ** sqlite3rbu.eStage variable set to CAPTURE. This turns on the following
3018 ** special behaviour in the rbu VFS:
3020 ** * If the exclusive shm WRITER or READ0 lock cannot be obtained,
3021 ** the checkpoint fails with SQLITE_BUSY (normally SQLite would
3022 ** proceed with running a passive checkpoint instead of failing).
3024 ** * Attempts to read from the *-wal file or write to the database file
3025 ** do not perform any IO. Instead, the frame/page combinations that
3026 ** would be read/written are recorded in the sqlite3rbu.aFrame[]
3029 ** * Calls to xShmLock(UNLOCK) to release the exclusive shm WRITER,
3030 ** READ0 and CHECKPOINT locks taken as part of the checkpoint are
3031 ** no-ops. These locks will not be released until the connection
3034 ** * Attempting to xSync() the database file causes an SQLITE_NOTICE
3037 ** As a result, unless an error (i.e. OOM or SQLITE_BUSY) occurs, the
3038 ** checkpoint below fails with SQLITE_NOTICE, and leaves the aFrame[]
3039 ** array populated with a set of (frame -> page) mappings. Because the
3040 ** WRITER, CHECKPOINT and READ0 locks are still held, it is safe to copy
3041 ** data from the wal file into the database file according to the
3042 ** contents of aFrame[].
3044 if( p
->rc
==SQLITE_OK
){
3046 p
->eStage
= RBU_STAGE_CAPTURE
;
3047 rc2
= sqlite3_exec(p
->dbMain
, "PRAGMA main.wal_checkpoint=restart", 0, 0,0);
3048 if( rc2
!=SQLITE_NOTICE
) p
->rc
= rc2
;
3051 if( p
->rc
==SQLITE_OK
&& p
->nFrame
>0 ){
3052 p
->eStage
= RBU_STAGE_CKPT
;
3053 p
->nStep
= (pState
? pState
->nRow
: 0);
3054 p
->aBuf
= rbuMalloc(p
, p
->pgsz
);
3055 p
->iWalCksum
= rbuShmChecksum(p
);
3058 if( p
->rc
==SQLITE_OK
){
3059 if( p
->nFrame
==0 || (pState
&& pState
->iWalCksum
!=p
->iWalCksum
) ){
3060 p
->rc
= SQLITE_DONE
;
3061 p
->eStage
= RBU_STAGE_DONE
;
3064 sqlite3_file
*pDb
= p
->pTargetFd
->pReal
;
3065 sqlite3_file
*pWal
= p
->pTargetFd
->pWalFd
->pReal
;
3066 assert( p
->nPagePerSector
==0 );
3067 nSectorSize
= pDb
->pMethods
->xSectorSize(pDb
);
3068 if( nSectorSize
>p
->pgsz
){
3069 p
->nPagePerSector
= nSectorSize
/ p
->pgsz
;
3071 p
->nPagePerSector
= 1;
3074 /* Call xSync() on the wal file. This causes SQLite to sync the
3075 ** directory in which the target database and the wal file reside, in
3076 ** case it has not been synced since the rename() call in
3077 ** rbuMoveOalFile(). */
3078 p
->rc
= pWal
->pMethods
->xSync(pWal
, SQLITE_SYNC_NORMAL
);
3084 ** Called when iAmt bytes are read from offset iOff of the wal file while
3085 ** the rbu object is in capture mode. Record the frame number of the frame
3086 ** being read in the aFrame[] array.
3088 static int rbuCaptureWalRead(sqlite3rbu
*pRbu
, i64 iOff
, int iAmt
){
3089 const u32 mReq
= (1<<WAL_LOCK_WRITE
)|(1<<WAL_LOCK_CKPT
)|(1<<WAL_LOCK_READ0
);
3092 if( pRbu
->mLock
!=mReq
){
3093 pRbu
->rc
= SQLITE_BUSY
;
3094 return SQLITE_NOTICE_RBU
;
3098 if( pRbu
->nFrame
==pRbu
->nFrameAlloc
){
3099 int nNew
= (pRbu
->nFrameAlloc
? pRbu
->nFrameAlloc
: 64) * 2;
3101 aNew
= (RbuFrame
*)sqlite3_realloc64(pRbu
->aFrame
, nNew
* sizeof(RbuFrame
));
3102 if( aNew
==0 ) return SQLITE_NOMEM
;
3103 pRbu
->aFrame
= aNew
;
3104 pRbu
->nFrameAlloc
= nNew
;
3107 iFrame
= (u32
)((iOff
-32) / (i64
)(iAmt
+24)) + 1;
3108 if( pRbu
->iMaxFrame
<iFrame
) pRbu
->iMaxFrame
= iFrame
;
3109 pRbu
->aFrame
[pRbu
->nFrame
].iWalFrame
= iFrame
;
3110 pRbu
->aFrame
[pRbu
->nFrame
].iDbPage
= 0;
3116 ** Called when a page of data is written to offset iOff of the database
3117 ** file while the rbu handle is in capture mode. Record the page number
3118 ** of the page being written in the aFrame[] array.
3120 static int rbuCaptureDbWrite(sqlite3rbu
*pRbu
, i64 iOff
){
3121 pRbu
->aFrame
[pRbu
->nFrame
-1].iDbPage
= (u32
)(iOff
/ pRbu
->pgsz
) + 1;
3126 ** This is called as part of an incremental checkpoint operation. Copy
3127 ** a single frame of data from the wal file into the database file, as
3128 ** indicated by the RbuFrame object.
3130 static void rbuCheckpointFrame(sqlite3rbu
*p
, RbuFrame
*pFrame
){
3131 sqlite3_file
*pWal
= p
->pTargetFd
->pWalFd
->pReal
;
3132 sqlite3_file
*pDb
= p
->pTargetFd
->pReal
;
3135 assert( p
->rc
==SQLITE_OK
);
3136 iOff
= (i64
)(pFrame
->iWalFrame
-1) * (p
->pgsz
+ 24) + 32 + 24;
3137 p
->rc
= pWal
->pMethods
->xRead(pWal
, p
->aBuf
, p
->pgsz
, iOff
);
3140 iOff
= (i64
)(pFrame
->iDbPage
-1) * p
->pgsz
;
3141 p
->rc
= pDb
->pMethods
->xWrite(pDb
, p
->aBuf
, p
->pgsz
, iOff
);
3146 ** Take an EXCLUSIVE lock on the database file. Return SQLITE_OK if
3147 ** successful, or an SQLite error code otherwise.
3149 static int rbuLockDatabase(sqlite3
*db
){
3151 sqlite3_file
*fd
= 0;
3152 sqlite3_file_control(db
, "main", SQLITE_FCNTL_FILE_POINTER
, &fd
);
3155 rc
= fd
->pMethods
->xLock(fd
, SQLITE_LOCK_SHARED
);
3156 if( rc
==SQLITE_OK
){
3157 rc
= fd
->pMethods
->xLock(fd
, SQLITE_LOCK_EXCLUSIVE
);
3164 ** Return true if the database handle passed as the only argument
3165 ** was opened with the rbu_exclusive_checkpoint=1 URI parameter
3166 ** specified. Or false otherwise.
3168 static int rbuExclusiveCheckpoint(sqlite3
*db
){
3169 const char *zUri
= sqlite3_db_filename(db
, 0);
3170 return sqlite3_uri_boolean(zUri
, RBU_EXCLUSIVE_CHECKPOINT
, 0);
3173 #if defined(_WIN32_WCE)
3174 static LPWSTR
rbuWinUtf8ToUnicode(const char *zFilename
){
3176 LPWSTR zWideFilename
;
3178 nChar
= MultiByteToWideChar(CP_UTF8
, 0, zFilename
, -1, NULL
, 0);
3182 zWideFilename
= sqlite3_malloc64( nChar
*sizeof(zWideFilename
[0]) );
3183 if( zWideFilename
==0 ){
3186 memset(zWideFilename
, 0, nChar
*sizeof(zWideFilename
[0]));
3187 nChar
= MultiByteToWideChar(CP_UTF8
, 0, zFilename
, -1, zWideFilename
,
3190 sqlite3_free(zWideFilename
);
3193 return zWideFilename
;
3198 ** The RBU handle is currently in RBU_STAGE_OAL state, with a SHARED lock
3199 ** on the database file. This proc moves the *-oal file to the *-wal path,
3200 ** then reopens the database file (this time in vanilla, non-oal, WAL mode).
3201 ** If an error occurs, leave an error code and error message in the rbu
3204 static void rbuMoveOalFile(sqlite3rbu
*p
){
3205 const char *zBase
= sqlite3_db_filename(p
->dbMain
, "main");
3206 const char *zMove
= zBase
;
3210 if( rbuIsVacuum(p
) ){
3211 zMove
= sqlite3_db_filename(p
->dbRbu
, "main");
3213 zOal
= sqlite3_mprintf("%s-oal", zMove
);
3214 zWal
= sqlite3_mprintf("%s-wal", zMove
);
3216 assert( p
->eStage
==RBU_STAGE_MOVE
);
3217 assert( p
->rc
==SQLITE_OK
&& p
->zErrmsg
==0 );
3218 if( zWal
==0 || zOal
==0 ){
3219 p
->rc
= SQLITE_NOMEM
;
3221 /* Move the *-oal file to *-wal. At this point connection p->db is
3222 ** holding a SHARED lock on the target database file (because it is
3223 ** in WAL mode). So no other connection may be writing the db.
3225 ** In order to ensure that there are no database readers, an EXCLUSIVE
3226 ** lock is obtained here before the *-oal is moved to *-wal.
3228 sqlite3
*dbMain
= 0;
3229 rbuFileSuffix3(zBase
, zWal
);
3230 rbuFileSuffix3(zBase
, zOal
);
3232 /* Re-open the databases. */
3233 rbuObjIterFinalize(&p
->objiter
);
3234 sqlite3_close(p
->dbRbu
);
3235 sqlite3_close(p
->dbMain
);
3239 dbMain
= rbuOpenDbhandle(p
, p
->zTarget
, 1);
3241 assert( p
->rc
==SQLITE_OK
);
3242 p
->rc
= rbuLockDatabase(dbMain
);
3245 if( p
->rc
==SQLITE_OK
){
3246 p
->rc
= p
->xRename(p
->pRenameArg
, zOal
, zWal
);
3249 if( p
->rc
!=SQLITE_OK
3251 || rbuExclusiveCheckpoint(dbMain
)==0
3253 sqlite3_close(dbMain
);
3257 if( p
->rc
==SQLITE_OK
){
3258 rbuOpenDatabase(p
, dbMain
, 0);
3259 rbuSetupCheckpoint(p
, 0);
3268 ** The SELECT statement iterating through the keys for the current object
3269 ** (p->objiter.pSelect) currently points to a valid row. This function
3270 ** determines the type of operation requested by this row and returns
3271 ** one of the following values to indicate the result:
3278 ** If RBU_UPDATE is returned, then output variable *pzMask is set to
3279 ** point to the text value indicating the columns to update.
3281 ** If the rbu_control field contains an invalid value, an error code and
3282 ** message are left in the RBU handle and zero returned.
3284 static int rbuStepType(sqlite3rbu
*p
, const char **pzMask
){
3285 int iCol
= p
->objiter
.nCol
; /* Index of rbu_control column */
3286 int res
= 0; /* Return value */
3288 switch( sqlite3_column_type(p
->objiter
.pSelect
, iCol
) ){
3289 case SQLITE_INTEGER
: {
3290 int iVal
= sqlite3_column_int(p
->objiter
.pSelect
, iCol
);
3292 case 0: res
= RBU_INSERT
; break;
3293 case 1: res
= RBU_DELETE
; break;
3294 case 2: res
= RBU_REPLACE
; break;
3295 case 3: res
= RBU_IDX_DELETE
; break;
3296 case 4: res
= RBU_IDX_INSERT
; break;
3302 const unsigned char *z
= sqlite3_column_text(p
->objiter
.pSelect
, iCol
);
3304 p
->rc
= SQLITE_NOMEM
;
3306 *pzMask
= (const char*)z
;
3318 rbuBadControlError(p
);
3325 ** Assert that column iCol of statement pStmt is named zName.
3327 static void assertColumnName(sqlite3_stmt
*pStmt
, int iCol
, const char *zName
){
3328 const char *zCol
= sqlite3_column_name(pStmt
, iCol
);
3329 assert( 0==sqlite3_stricmp(zName
, zCol
) );
3332 # define assertColumnName(x,y,z)
3336 ** Argument eType must be one of RBU_INSERT, RBU_DELETE, RBU_IDX_INSERT or
3337 ** RBU_IDX_DELETE. This function performs the work of a single
3338 ** sqlite3rbu_step() call for the type of operation specified by eType.
3340 static void rbuStepOneOp(sqlite3rbu
*p
, int eType
){
3341 RbuObjIter
*pIter
= &p
->objiter
;
3342 sqlite3_value
*pVal
;
3343 sqlite3_stmt
*pWriter
;
3346 assert( p
->rc
==SQLITE_OK
);
3347 assert( eType
!=RBU_DELETE
|| pIter
->zIdx
==0 );
3348 assert( eType
==RBU_DELETE
|| eType
==RBU_IDX_DELETE
3349 || eType
==RBU_INSERT
|| eType
==RBU_IDX_INSERT
3352 /* If this is a delete, decrement nPhaseOneStep by nIndex. If the DELETE
3353 ** statement below does actually delete a row, nPhaseOneStep will be
3354 ** incremented by the same amount when SQL function rbu_tmp_insert()
3355 ** is invoked by the trigger. */
3356 if( eType
==RBU_DELETE
){
3357 p
->nPhaseOneStep
-= p
->objiter
.nIndex
;
3360 if( eType
==RBU_IDX_DELETE
|| eType
==RBU_DELETE
){
3361 pWriter
= pIter
->pDelete
;
3363 pWriter
= pIter
->pInsert
;
3366 for(i
=0; i
<pIter
->nCol
; i
++){
3367 /* If this is an INSERT into a table b-tree and the table has an
3368 ** explicit INTEGER PRIMARY KEY, check that this is not an attempt
3369 ** to write a NULL into the IPK column. That is not permitted. */
3370 if( eType
==RBU_INSERT
3371 && pIter
->zIdx
==0 && pIter
->eType
==RBU_PK_IPK
&& pIter
->abTblPk
[i
]
3372 && sqlite3_column_type(pIter
->pSelect
, i
)==SQLITE_NULL
3374 p
->rc
= SQLITE_MISMATCH
;
3375 p
->zErrmsg
= sqlite3_mprintf("datatype mismatch");
3379 if( eType
==RBU_DELETE
&& pIter
->abTblPk
[i
]==0 ){
3383 pVal
= sqlite3_column_value(pIter
->pSelect
, i
);
3384 p
->rc
= sqlite3_bind_value(pWriter
, i
+1, pVal
);
3387 if( pIter
->zIdx
==0 ){
3388 if( pIter
->eType
==RBU_PK_VTAB
3389 || pIter
->eType
==RBU_PK_NONE
3390 || (pIter
->eType
==RBU_PK_EXTERNAL
&& rbuIsVacuum(p
))
3392 /* For a virtual table, or a table with no primary key, the
3393 ** SELECT statement is:
3395 ** SELECT <cols>, rbu_control, rbu_rowid FROM ....
3397 ** Hence column_value(pIter->nCol+1).
3399 assertColumnName(pIter
->pSelect
, pIter
->nCol
+1,
3400 rbuIsVacuum(p
) ? "rowid" : "rbu_rowid"
3402 pVal
= sqlite3_column_value(pIter
->pSelect
, pIter
->nCol
+1);
3403 p
->rc
= sqlite3_bind_value(pWriter
, pIter
->nCol
+1, pVal
);
3406 if( p
->rc
==SQLITE_OK
){
3407 sqlite3_step(pWriter
);
3408 p
->rc
= resetAndCollectError(pWriter
, &p
->zErrmsg
);
3413 ** This function does the work for an sqlite3rbu_step() call.
3415 ** The object-iterator (p->objiter) currently points to a valid object,
3416 ** and the input cursor (p->objiter.pSelect) currently points to a valid
3417 ** input row. Perform whatever processing is required and return.
3419 ** If no error occurs, SQLITE_OK is returned. Otherwise, an error code
3420 ** and message is left in the RBU handle and a copy of the error code
3423 static int rbuStep(sqlite3rbu
*p
){
3424 RbuObjIter
*pIter
= &p
->objiter
;
3425 const char *zMask
= 0;
3426 int eType
= rbuStepType(p
, &zMask
);
3429 assert( eType
==RBU_INSERT
|| eType
==RBU_DELETE
3430 || eType
==RBU_REPLACE
|| eType
==RBU_IDX_DELETE
3431 || eType
==RBU_IDX_INSERT
|| eType
==RBU_UPDATE
3433 assert( eType
!=RBU_UPDATE
|| pIter
->zIdx
==0 );
3435 if( pIter
->zIdx
==0 && (eType
==RBU_IDX_DELETE
|| eType
==RBU_IDX_INSERT
) ){
3436 rbuBadControlError(p
);
3438 else if( eType
==RBU_REPLACE
){
3439 if( pIter
->zIdx
==0 ){
3440 p
->nPhaseOneStep
+= p
->objiter
.nIndex
;
3441 rbuStepOneOp(p
, RBU_DELETE
);
3443 if( p
->rc
==SQLITE_OK
) rbuStepOneOp(p
, RBU_INSERT
);
3445 else if( eType
!=RBU_UPDATE
){
3446 rbuStepOneOp(p
, eType
);
3449 sqlite3_value
*pVal
;
3450 sqlite3_stmt
*pUpdate
= 0;
3451 assert( eType
==RBU_UPDATE
);
3452 p
->nPhaseOneStep
-= p
->objiter
.nIndex
;
3453 rbuGetUpdateStmt(p
, pIter
, zMask
, &pUpdate
);
3456 for(i
=0; p
->rc
==SQLITE_OK
&& i
<pIter
->nCol
; i
++){
3457 char c
= zMask
[pIter
->aiSrcOrder
[i
]];
3458 pVal
= sqlite3_column_value(pIter
->pSelect
, i
);
3459 if( pIter
->abTblPk
[i
] || c
!='.' ){
3460 p
->rc
= sqlite3_bind_value(pUpdate
, i
+1, pVal
);
3463 if( p
->rc
==SQLITE_OK
3464 && (pIter
->eType
==RBU_PK_VTAB
|| pIter
->eType
==RBU_PK_NONE
)
3466 /* Bind the rbu_rowid value to column _rowid_ */
3467 assertColumnName(pIter
->pSelect
, pIter
->nCol
+1, "rbu_rowid");
3468 pVal
= sqlite3_column_value(pIter
->pSelect
, pIter
->nCol
+1);
3469 p
->rc
= sqlite3_bind_value(pUpdate
, pIter
->nCol
+1, pVal
);
3471 if( p
->rc
==SQLITE_OK
){
3472 sqlite3_step(pUpdate
);
3473 p
->rc
= resetAndCollectError(pUpdate
, &p
->zErrmsg
);
3482 ** Increment the schema cookie of the main database opened by p->dbMain.
3484 ** Or, if this is an RBU vacuum, set the schema cookie of the main db
3485 ** opened by p->dbMain to one more than the schema cookie of the main
3486 ** db opened by p->dbRbu.
3488 static void rbuIncrSchemaCookie(sqlite3rbu
*p
){
3489 if( p
->rc
==SQLITE_OK
){
3490 sqlite3
*dbread
= (rbuIsVacuum(p
) ? p
->dbRbu
: p
->dbMain
);
3491 int iCookie
= 1000000;
3492 sqlite3_stmt
*pStmt
;
3494 p
->rc
= prepareAndCollectError(dbread
, &pStmt
, &p
->zErrmsg
,
3495 "PRAGMA schema_version"
3497 if( p
->rc
==SQLITE_OK
){
3498 /* Coverage: it may be that this sqlite3_step() cannot fail. There
3499 ** is already a transaction open, so the prepared statement cannot
3500 ** throw an SQLITE_SCHEMA exception. The only database page the
3501 ** statement reads is page 1, which is guaranteed to be in the cache.
3502 ** And no memory allocations are required. */
3503 if( SQLITE_ROW
==sqlite3_step(pStmt
) ){
3504 iCookie
= sqlite3_column_int(pStmt
, 0);
3506 rbuFinalize(p
, pStmt
);
3508 if( p
->rc
==SQLITE_OK
){
3509 rbuMPrintfExec(p
, p
->dbMain
, "PRAGMA schema_version = %d", iCookie
+1);
3515 ** Update the contents of the rbu_state table within the rbu database. The
3516 ** value stored in the RBU_STATE_STAGE column is eStage. All other values
3517 ** are determined by inspecting the rbu handle passed as the first argument.
3519 static void rbuSaveState(sqlite3rbu
*p
, int eStage
){
3520 if( p
->rc
==SQLITE_OK
|| p
->rc
==SQLITE_DONE
){
3521 sqlite3_stmt
*pInsert
= 0;
3522 rbu_file
*pFd
= (rbuIsVacuum(p
) ? p
->pRbuFd
: p
->pTargetFd
);
3525 assert( p
->zErrmsg
==0 );
3526 rc
= prepareFreeAndCollectError(p
->dbRbu
, &pInsert
, &p
->zErrmsg
,
3528 "INSERT OR REPLACE INTO %s.rbu_state(k, v) VALUES "
3540 RBU_STATE_STAGE
, eStage
,
3541 RBU_STATE_TBL
, p
->objiter
.zTbl
,
3542 RBU_STATE_IDX
, p
->objiter
.zIdx
,
3543 RBU_STATE_ROW
, p
->nStep
,
3544 RBU_STATE_PROGRESS
, p
->nProgress
,
3545 RBU_STATE_CKPT
, p
->iWalCksum
,
3546 RBU_STATE_COOKIE
, (i64
)pFd
->iCookie
,
3547 RBU_STATE_OALSZ
, p
->iOalSz
,
3548 RBU_STATE_PHASEONESTEP
, p
->nPhaseOneStep
,
3549 RBU_STATE_DATATBL
, p
->objiter
.zDataTbl
3552 assert( pInsert
==0 || rc
==SQLITE_OK
);
3554 if( rc
==SQLITE_OK
){
3555 sqlite3_step(pInsert
);
3556 rc
= sqlite3_finalize(pInsert
);
3558 if( rc
!=SQLITE_OK
) p
->rc
= rc
;
3564 ** The second argument passed to this function is the name of a PRAGMA
3565 ** setting - "page_size", "auto_vacuum", "user_version" or "application_id".
3566 ** This function executes the following on sqlite3rbu.dbRbu:
3568 ** "PRAGMA main.$zPragma"
3570 ** where $zPragma is the string passed as the second argument, then
3571 ** on sqlite3rbu.dbMain:
3573 ** "PRAGMA main.$zPragma = $val"
3575 ** where $val is the value returned by the first PRAGMA invocation.
3577 ** In short, it copies the value of the specified PRAGMA setting from
3580 static void rbuCopyPragma(sqlite3rbu
*p
, const char *zPragma
){
3581 if( p
->rc
==SQLITE_OK
){
3582 sqlite3_stmt
*pPragma
= 0;
3583 p
->rc
= prepareFreeAndCollectError(p
->dbRbu
, &pPragma
, &p
->zErrmsg
,
3584 sqlite3_mprintf("PRAGMA main.%s", zPragma
)
3586 if( p
->rc
==SQLITE_OK
&& SQLITE_ROW
==sqlite3_step(pPragma
) ){
3587 p
->rc
= rbuMPrintfExec(p
, p
->dbMain
, "PRAGMA main.%s = %d",
3588 zPragma
, sqlite3_column_int(pPragma
, 0)
3591 rbuFinalize(p
, pPragma
);
3596 ** The RBU handle passed as the only argument has just been opened and
3597 ** the state database is empty. If this RBU handle was opened for an
3598 ** RBU vacuum operation, create the schema in the target db.
3600 static void rbuCreateTargetSchema(sqlite3rbu
*p
){
3601 sqlite3_stmt
*pSql
= 0;
3602 sqlite3_stmt
*pInsert
= 0;
3604 assert( rbuIsVacuum(p
) );
3605 p
->rc
= sqlite3_exec(p
->dbMain
, "PRAGMA writable_schema=1", 0,0, &p
->zErrmsg
);
3606 if( p
->rc
==SQLITE_OK
){
3607 p
->rc
= prepareAndCollectError(p
->dbRbu
, &pSql
, &p
->zErrmsg
,
3608 "SELECT sql FROM sqlite_schema WHERE sql!='' AND rootpage!=0"
3609 " AND name!='sqlite_sequence' "
3610 " ORDER BY type DESC"
3614 while( p
->rc
==SQLITE_OK
&& sqlite3_step(pSql
)==SQLITE_ROW
){
3615 const char *zSql
= (const char*)sqlite3_column_text(pSql
, 0);
3616 p
->rc
= sqlite3_exec(p
->dbMain
, zSql
, 0, 0, &p
->zErrmsg
);
3618 rbuFinalize(p
, pSql
);
3619 if( p
->rc
!=SQLITE_OK
) return;
3621 if( p
->rc
==SQLITE_OK
){
3622 p
->rc
= prepareAndCollectError(p
->dbRbu
, &pSql
, &p
->zErrmsg
,
3623 "SELECT * FROM sqlite_schema WHERE rootpage=0 OR rootpage IS NULL"
3627 if( p
->rc
==SQLITE_OK
){
3628 p
->rc
= prepareAndCollectError(p
->dbMain
, &pInsert
, &p
->zErrmsg
,
3629 "INSERT INTO sqlite_schema VALUES(?,?,?,?,?)"
3633 while( p
->rc
==SQLITE_OK
&& sqlite3_step(pSql
)==SQLITE_ROW
){
3636 sqlite3_bind_value(pInsert
, i
+1, sqlite3_column_value(pSql
, i
));
3638 sqlite3_step(pInsert
);
3639 p
->rc
= sqlite3_reset(pInsert
);
3641 if( p
->rc
==SQLITE_OK
){
3642 p
->rc
= sqlite3_exec(p
->dbMain
, "PRAGMA writable_schema=0",0,0,&p
->zErrmsg
);
3645 rbuFinalize(p
, pSql
);
3646 rbuFinalize(p
, pInsert
);
3650 ** Step the RBU object.
3652 int sqlite3rbu_step(sqlite3rbu
*p
){
3654 switch( p
->eStage
){
3655 case RBU_STAGE_OAL
: {
3656 RbuObjIter
*pIter
= &p
->objiter
;
3658 /* If this is an RBU vacuum operation and the state table was empty
3659 ** when this handle was opened, create the target database schema. */
3660 if( rbuIsVacuum(p
) && p
->nProgress
==0 && p
->rc
==SQLITE_OK
){
3661 rbuCreateTargetSchema(p
);
3662 rbuCopyPragma(p
, "user_version");
3663 rbuCopyPragma(p
, "application_id");
3666 while( p
->rc
==SQLITE_OK
&& pIter
->zTbl
){
3668 if( pIter
->bCleanup
){
3669 /* Clean up the rbu_tmp_xxx table for the previous table. It
3670 ** cannot be dropped as there are currently active SQL statements.
3671 ** But the contents can be deleted. */
3672 if( rbuIsVacuum(p
)==0 && pIter
->abIndexed
){
3673 rbuMPrintfExec(p
, p
->dbRbu
,
3674 "DELETE FROM %s.'rbu_tmp_%q'", p
->zStateDb
, pIter
->zDataTbl
3678 rbuObjIterPrepareAll(p
, pIter
, 0);
3680 /* Advance to the next row to process. */
3681 if( p
->rc
==SQLITE_OK
){
3682 int rc
= sqlite3_step(pIter
->pSelect
);
3683 if( rc
==SQLITE_ROW
){
3688 p
->rc
= sqlite3_reset(pIter
->pSelect
);
3693 rbuObjIterNext(p
, pIter
);
3696 if( p
->rc
==SQLITE_OK
){
3697 assert( pIter
->zTbl
==0 );
3698 rbuSaveState(p
, RBU_STAGE_MOVE
);
3699 rbuIncrSchemaCookie(p
);
3700 if( p
->rc
==SQLITE_OK
){
3701 p
->rc
= sqlite3_exec(p
->dbMain
, "COMMIT", 0, 0, &p
->zErrmsg
);
3703 if( p
->rc
==SQLITE_OK
){
3704 p
->rc
= sqlite3_exec(p
->dbRbu
, "COMMIT", 0, 0, &p
->zErrmsg
);
3706 p
->eStage
= RBU_STAGE_MOVE
;
3711 case RBU_STAGE_MOVE
: {
3712 if( p
->rc
==SQLITE_OK
){
3719 case RBU_STAGE_CKPT
: {
3720 if( p
->rc
==SQLITE_OK
){
3721 if( p
->nStep
>=p
->nFrame
){
3722 sqlite3_file
*pDb
= p
->pTargetFd
->pReal
;
3724 /* Sync the db file */
3725 p
->rc
= pDb
->pMethods
->xSync(pDb
, SQLITE_SYNC_NORMAL
);
3727 /* Update nBackfill */
3728 if( p
->rc
==SQLITE_OK
){
3730 p
->rc
= pDb
->pMethods
->xShmMap(pDb
, 0, 32*1024, 0, &ptr
);
3731 if( p
->rc
==SQLITE_OK
){
3732 ((u32
volatile*)ptr
)[24] = p
->iMaxFrame
;
3736 if( p
->rc
==SQLITE_OK
){
3737 p
->eStage
= RBU_STAGE_DONE
;
3738 p
->rc
= SQLITE_DONE
;
3741 /* At one point the following block copied a single frame from the
3742 ** wal file to the database file. So that one call to sqlite3rbu_step()
3743 ** checkpointed a single frame.
3745 ** However, if the sector-size is larger than the page-size, and the
3746 ** application calls sqlite3rbu_savestate() or close() immediately
3747 ** after this step, then rbu_step() again, then a power failure occurs,
3748 ** then the database page written here may be damaged. Work around
3749 ** this by checkpointing frames until the next page in the aFrame[]
3750 ** lies on a different disk sector to the current one. */
3753 RbuFrame
*pFrame
= &p
->aFrame
[p
->nStep
];
3754 iSector
= (pFrame
->iDbPage
-1) / p
->nPagePerSector
;
3755 rbuCheckpointFrame(p
, pFrame
);
3757 }while( p
->nStep
<p
->nFrame
3758 && iSector
==((p
->aFrame
[p
->nStep
].iDbPage
-1) / p
->nPagePerSector
)
3772 return SQLITE_NOMEM
;
3777 ** Compare strings z1 and z2, returning 0 if they are identical, or non-zero
3778 ** otherwise. Either or both argument may be NULL. Two NULL values are
3779 ** considered equal, and NULL is considered distinct from all other values.
3781 static int rbuStrCompare(const char *z1
, const char *z2
){
3782 if( z1
==0 && z2
==0 ) return 0;
3783 if( z1
==0 || z2
==0 ) return 1;
3784 return (sqlite3_stricmp(z1
, z2
)!=0);
3788 ** This function is called as part of sqlite3rbu_open() when initializing
3789 ** an rbu handle in OAL stage. If the rbu update has not started (i.e.
3790 ** the rbu_state table was empty) it is a no-op. Otherwise, it arranges
3791 ** things so that the next call to sqlite3rbu_step() continues on from
3792 ** where the previous rbu handle left off.
3794 ** If an error occurs, an error code and error message are left in the
3795 ** rbu handle passed as the first argument.
3797 static void rbuSetupOal(sqlite3rbu
*p
, RbuState
*pState
){
3798 assert( p
->rc
==SQLITE_OK
);
3800 RbuObjIter
*pIter
= &p
->objiter
;
3803 while( rc
==SQLITE_OK
&& pIter
->zTbl
&& (pIter
->bCleanup
3804 || rbuStrCompare(pIter
->zIdx
, pState
->zIdx
)
3805 || (pState
->zDataTbl
==0 && rbuStrCompare(pIter
->zTbl
, pState
->zTbl
))
3806 || (pState
->zDataTbl
&& rbuStrCompare(pIter
->zDataTbl
, pState
->zDataTbl
))
3808 rc
= rbuObjIterNext(p
, pIter
);
3811 if( rc
==SQLITE_OK
&& !pIter
->zTbl
){
3813 p
->zErrmsg
= sqlite3_mprintf("rbu_state mismatch error");
3816 if( rc
==SQLITE_OK
){
3817 p
->nStep
= pState
->nRow
;
3818 rc
= rbuObjIterPrepareAll(p
, &p
->objiter
, p
->nStep
);
3826 ** If there is a "*-oal" file in the file-system corresponding to the
3827 ** target database in the file-system, delete it. If an error occurs,
3828 ** leave an error code and error message in the rbu handle.
3830 static void rbuDeleteOalFile(sqlite3rbu
*p
){
3831 char *zOal
= rbuMPrintf(p
, "%s-oal", p
->zTarget
);
3833 sqlite3_vfs
*pVfs
= 0;
3834 sqlite3_file_control(p
->dbMain
, "main", SQLITE_FCNTL_VFS_POINTER
, &pVfs
);
3835 assert( pVfs
&& p
->rc
==SQLITE_OK
&& p
->zErrmsg
==0 );
3836 pVfs
->xDelete(pVfs
, zOal
, 0);
3842 ** Allocate a private rbu VFS for the rbu handle passed as the only
3843 ** argument. This VFS will be used unless the call to sqlite3rbu_open()
3844 ** specified a URI with a vfs=? option in place of a target database
3847 static void rbuCreateVfs(sqlite3rbu
*p
){
3851 assert( p
->rc
==SQLITE_OK
);
3852 sqlite3_randomness(sizeof(int), (void*)&rnd
);
3853 sqlite3_snprintf(sizeof(zRnd
), zRnd
, "rbu_vfs_%d", rnd
);
3854 p
->rc
= sqlite3rbu_create_vfs(zRnd
, 0);
3855 if( p
->rc
==SQLITE_OK
){
3856 sqlite3_vfs
*pVfs
= sqlite3_vfs_find(zRnd
);
3858 p
->zVfsName
= pVfs
->zName
;
3859 ((rbu_vfs
*)pVfs
)->pRbu
= p
;
3864 ** Destroy the private VFS created for the rbu handle passed as the only
3865 ** argument by an earlier call to rbuCreateVfs().
3867 static void rbuDeleteVfs(sqlite3rbu
*p
){
3869 sqlite3rbu_destroy_vfs(p
->zVfsName
);
3875 ** This user-defined SQL function is invoked with a single argument - the
3876 ** name of a table expected to appear in the target database. It returns
3877 ** the number of auxilliary indexes on the table.
3879 static void rbuIndexCntFunc(
3880 sqlite3_context
*pCtx
,
3882 sqlite3_value
**apVal
3884 sqlite3rbu
*p
= (sqlite3rbu
*)sqlite3_user_data(pCtx
);
3885 sqlite3_stmt
*pStmt
= 0;
3888 sqlite3
*db
= (rbuIsVacuum(p
) ? p
->dbRbu
: p
->dbMain
);
3892 rc
= prepareFreeAndCollectError(db
, &pStmt
, &zErrmsg
,
3893 sqlite3_mprintf("SELECT count(*) FROM sqlite_schema "
3894 "WHERE type='index' AND tbl_name = %Q", sqlite3_value_text(apVal
[0]))
3896 if( rc
!=SQLITE_OK
){
3897 sqlite3_result_error(pCtx
, zErrmsg
, -1);
3900 if( SQLITE_ROW
==sqlite3_step(pStmt
) ){
3901 nIndex
= sqlite3_column_int(pStmt
, 0);
3903 rc
= sqlite3_finalize(pStmt
);
3904 if( rc
==SQLITE_OK
){
3905 sqlite3_result_int(pCtx
, nIndex
);
3907 sqlite3_result_error(pCtx
, sqlite3_errmsg(db
), -1);
3911 sqlite3_free(zErrmsg
);
3915 ** If the RBU database contains the rbu_count table, use it to initialize
3916 ** the sqlite3rbu.nPhaseOneStep variable. The schema of the rbu_count table
3917 ** is assumed to contain the same columns as:
3919 ** CREATE TABLE rbu_count(tbl TEXT PRIMARY KEY, cnt INTEGER) WITHOUT ROWID;
3921 ** There should be one row in the table for each data_xxx table in the
3922 ** database. The 'tbl' column should contain the name of a data_xxx table,
3923 ** and the cnt column the number of rows it contains.
3925 ** sqlite3rbu.nPhaseOneStep is initialized to the sum of (1 + nIndex) * cnt
3926 ** for all rows in the rbu_count table, where nIndex is the number of
3927 ** indexes on the corresponding target database table.
3929 static void rbuInitPhaseOneSteps(sqlite3rbu
*p
){
3930 if( p
->rc
==SQLITE_OK
){
3931 sqlite3_stmt
*pStmt
= 0;
3932 int bExists
= 0; /* True if rbu_count exists */
3934 p
->nPhaseOneStep
= -1;
3936 p
->rc
= sqlite3_create_function(p
->dbRbu
,
3937 "rbu_index_cnt", 1, SQLITE_UTF8
, (void*)p
, rbuIndexCntFunc
, 0, 0
3940 /* Check for the rbu_count table. If it does not exist, or if an error
3941 ** occurs, nPhaseOneStep will be left set to -1. */
3942 if( p
->rc
==SQLITE_OK
){
3943 p
->rc
= prepareAndCollectError(p
->dbRbu
, &pStmt
, &p
->zErrmsg
,
3944 "SELECT 1 FROM sqlite_schema WHERE tbl_name = 'rbu_count'"
3947 if( p
->rc
==SQLITE_OK
){
3948 if( SQLITE_ROW
==sqlite3_step(pStmt
) ){
3951 p
->rc
= sqlite3_finalize(pStmt
);
3954 if( p
->rc
==SQLITE_OK
&& bExists
){
3955 p
->rc
= prepareAndCollectError(p
->dbRbu
, &pStmt
, &p
->zErrmsg
,
3956 "SELECT sum(cnt * (1 + rbu_index_cnt(rbu_target_name(tbl))))"
3959 if( p
->rc
==SQLITE_OK
){
3960 if( SQLITE_ROW
==sqlite3_step(pStmt
) ){
3961 p
->nPhaseOneStep
= sqlite3_column_int64(pStmt
, 0);
3963 p
->rc
= sqlite3_finalize(pStmt
);
3970 static sqlite3rbu
*openRbuHandle(
3971 const char *zTarget
,
3976 size_t nTarget
= zTarget
? strlen(zTarget
) : 0;
3977 size_t nRbu
= strlen(zRbu
);
3978 size_t nByte
= sizeof(sqlite3rbu
) + nTarget
+1 + nRbu
+1;
3980 p
= (sqlite3rbu
*)sqlite3_malloc64(nByte
);
3982 RbuState
*pState
= 0;
3984 /* Create the custom VFS. */
3985 memset(p
, 0, sizeof(sqlite3rbu
));
3986 sqlite3rbu_rename_handler(p
, 0, 0);
3989 /* Open the target, RBU and state databases */
3990 if( p
->rc
==SQLITE_OK
){
3991 char *pCsr
= (char*)&p
[1];
3995 memcpy(p
->zTarget
, zTarget
, nTarget
+1);
3999 memcpy(p
->zRbu
, zRbu
, nRbu
+1);
4002 p
->zState
= rbuMPrintf(p
, "%s", zState
);
4005 /* If the first attempt to open the database file fails and the bRetry
4006 ** flag it set, this means that the db was not opened because it seemed
4007 ** to be a wal-mode db. But, this may have happened due to an earlier
4008 ** RBU vacuum operation leaving an old wal file in the directory.
4009 ** If this is the case, it will have been checkpointed and deleted
4010 ** when the handle was closed and a second attempt to open the
4011 ** database may succeed. */
4012 rbuOpenDatabase(p
, 0, &bRetry
);
4014 rbuOpenDatabase(p
, 0, 0);
4018 if( p
->rc
==SQLITE_OK
){
4019 pState
= rbuLoadState(p
);
4020 assert( pState
|| p
->rc
!=SQLITE_OK
);
4021 if( p
->rc
==SQLITE_OK
){
4023 if( pState
->eStage
==0 ){
4024 rbuDeleteOalFile(p
);
4025 rbuInitPhaseOneSteps(p
);
4026 p
->eStage
= RBU_STAGE_OAL
;
4028 p
->eStage
= pState
->eStage
;
4029 p
->nPhaseOneStep
= pState
->nPhaseOneStep
;
4031 p
->nProgress
= pState
->nProgress
;
4032 p
->iOalSz
= pState
->iOalSz
;
4035 assert( p
->rc
!=SQLITE_OK
|| p
->eStage
!=0 );
4037 if( p
->rc
==SQLITE_OK
&& p
->pTargetFd
->pWalFd
){
4038 if( p
->eStage
==RBU_STAGE_OAL
){
4039 p
->rc
= SQLITE_ERROR
;
4040 p
->zErrmsg
= sqlite3_mprintf("cannot update wal mode database");
4041 }else if( p
->eStage
==RBU_STAGE_MOVE
){
4042 p
->eStage
= RBU_STAGE_CKPT
;
4047 if( p
->rc
==SQLITE_OK
4048 && (p
->eStage
==RBU_STAGE_OAL
|| p
->eStage
==RBU_STAGE_MOVE
)
4049 && pState
->eStage
!=0
4051 rbu_file
*pFd
= (rbuIsVacuum(p
) ? p
->pRbuFd
: p
->pTargetFd
);
4052 if( pFd
->iCookie
!=pState
->iCookie
){
4053 /* At this point (pTargetFd->iCookie) contains the value of the
4054 ** change-counter cookie (the thing that gets incremented when a
4055 ** transaction is committed in rollback mode) currently stored on
4056 ** page 1 of the database file. */
4057 p
->rc
= SQLITE_BUSY
;
4058 p
->zErrmsg
= sqlite3_mprintf("database modified during rbu %s",
4059 (rbuIsVacuum(p
) ? "vacuum" : "update")
4064 if( p
->rc
==SQLITE_OK
){
4065 if( p
->eStage
==RBU_STAGE_OAL
){
4066 sqlite3
*db
= p
->dbMain
;
4067 p
->rc
= sqlite3_exec(p
->dbRbu
, "BEGIN", 0, 0, &p
->zErrmsg
);
4069 /* Point the object iterator at the first object */
4070 if( p
->rc
==SQLITE_OK
){
4071 p
->rc
= rbuObjIterFirst(p
, &p
->objiter
);
4074 /* If the RBU database contains no data_xxx tables, declare the RBU
4075 ** update finished. */
4076 if( p
->rc
==SQLITE_OK
&& p
->objiter
.zTbl
==0 ){
4077 p
->rc
= SQLITE_DONE
;
4078 p
->eStage
= RBU_STAGE_DONE
;
4080 if( p
->rc
==SQLITE_OK
&& pState
->eStage
==0 && rbuIsVacuum(p
) ){
4081 rbuCopyPragma(p
, "page_size");
4082 rbuCopyPragma(p
, "auto_vacuum");
4085 /* Open transactions both databases. The *-oal file is opened or
4086 ** created at this point. */
4087 if( p
->rc
==SQLITE_OK
){
4088 p
->rc
= sqlite3_exec(db
, "BEGIN IMMEDIATE", 0, 0, &p
->zErrmsg
);
4091 /* Check if the main database is a zipvfs db. If it is, set the upper
4092 ** level pager to use "journal_mode=off". This prevents it from
4093 ** generating a large journal using a temp file. */
4094 if( p
->rc
==SQLITE_OK
){
4095 int frc
= sqlite3_file_control(db
, "main", SQLITE_FCNTL_ZIPVFS
, 0);
4096 if( frc
==SQLITE_OK
){
4097 p
->rc
= sqlite3_exec(
4098 db
, "PRAGMA journal_mode=off",0,0,&p
->zErrmsg
);
4102 if( p
->rc
==SQLITE_OK
){
4103 rbuSetupOal(p
, pState
);
4106 }else if( p
->eStage
==RBU_STAGE_MOVE
){
4108 }else if( p
->eStage
==RBU_STAGE_CKPT
){
4109 if( !rbuIsVacuum(p
) && rbuExclusiveCheckpoint(p
->dbMain
) ){
4110 /* If the rbu_exclusive_checkpoint=1 URI parameter was specified
4111 ** and an incremental checkpoint is being resumed, attempt an
4112 ** exclusive lock on the db file. If this fails, so be it. */
4113 p
->eStage
= RBU_STAGE_DONE
;
4114 rbuLockDatabase(p
->dbMain
);
4115 p
->eStage
= RBU_STAGE_CKPT
;
4117 rbuSetupCheckpoint(p
, pState
);
4118 }else if( p
->eStage
==RBU_STAGE_DONE
){
4119 p
->rc
= SQLITE_DONE
;
4121 p
->rc
= SQLITE_CORRUPT
;
4125 rbuFreeState(pState
);
4132 ** Allocate and return an RBU handle with all fields zeroed except for the
4133 ** error code, which is set to SQLITE_MISUSE.
4135 static sqlite3rbu
*rbuMisuseError(void){
4137 pRet
= sqlite3_malloc64(sizeof(sqlite3rbu
));
4139 memset(pRet
, 0, sizeof(sqlite3rbu
));
4140 pRet
->rc
= SQLITE_MISUSE
;
4146 ** Open and return a new RBU handle.
4148 sqlite3rbu
*sqlite3rbu_open(
4149 const char *zTarget
,
4153 if( zTarget
==0 || zRbu
==0 ){ return rbuMisuseError(); }
4154 return openRbuHandle(zTarget
, zRbu
, zState
);
4158 ** Open a handle to begin or resume an RBU VACUUM operation.
4160 sqlite3rbu
*sqlite3rbu_vacuum(
4161 const char *zTarget
,
4164 if( zTarget
==0 ){ return rbuMisuseError(); }
4166 int n
= strlen(zState
);
4167 if( n
>=7 && 0==memcmp("-vactmp", &zState
[n
-7], 7) ){
4168 return rbuMisuseError();
4171 /* TODO: Check that both arguments are non-NULL */
4172 return openRbuHandle(0, zTarget
, zState
);
4176 ** Return the database handle used by pRbu.
4178 sqlite3
*sqlite3rbu_db(sqlite3rbu
*pRbu
, int bRbu
){
4181 db
= (bRbu
? pRbu
->dbRbu
: pRbu
->dbMain
);
4188 ** If the error code currently stored in the RBU handle is SQLITE_CONSTRAINT,
4189 ** then edit any error message string so as to remove all occurrences of
4190 ** the pattern "rbu_imp_[0-9]*".
4192 static void rbuEditErrmsg(sqlite3rbu
*p
){
4193 if( p
->rc
==SQLITE_CONSTRAINT
&& p
->zErrmsg
){
4195 size_t nErrmsg
= strlen(p
->zErrmsg
);
4196 for(i
=0; i
<(nErrmsg
-8); i
++){
4197 if( memcmp(&p
->zErrmsg
[i
], "rbu_imp_", 8)==0 ){
4199 while( p
->zErrmsg
[i
+nDel
]>='0' && p
->zErrmsg
[i
+nDel
]<='9' ) nDel
++;
4200 memmove(&p
->zErrmsg
[i
], &p
->zErrmsg
[i
+nDel
], nErrmsg
+ 1 - i
- nDel
);
4208 ** Close the RBU handle.
4210 int sqlite3rbu_close(sqlite3rbu
*p
, char **pzErrmsg
){
4214 /* Commit the transaction to the *-oal file. */
4215 if( p
->rc
==SQLITE_OK
&& p
->eStage
==RBU_STAGE_OAL
){
4216 p
->rc
= sqlite3_exec(p
->dbMain
, "COMMIT", 0, 0, &p
->zErrmsg
);
4219 /* Sync the db file if currently doing an incremental checkpoint */
4220 if( p
->rc
==SQLITE_OK
&& p
->eStage
==RBU_STAGE_CKPT
){
4221 sqlite3_file
*pDb
= p
->pTargetFd
->pReal
;
4222 p
->rc
= pDb
->pMethods
->xSync(pDb
, SQLITE_SYNC_NORMAL
);
4225 rbuSaveState(p
, p
->eStage
);
4227 if( p
->rc
==SQLITE_OK
&& p
->eStage
==RBU_STAGE_OAL
){
4228 p
->rc
= sqlite3_exec(p
->dbRbu
, "COMMIT", 0, 0, &p
->zErrmsg
);
4231 /* Close any open statement handles. */
4232 rbuObjIterFinalize(&p
->objiter
);
4234 /* If this is an RBU vacuum handle and the vacuum has either finished
4235 ** successfully or encountered an error, delete the contents of the
4236 ** state table. This causes the next call to sqlite3rbu_vacuum()
4237 ** specifying the current target and state databases to start a new
4238 ** vacuum from scratch. */
4239 if( rbuIsVacuum(p
) && p
->rc
!=SQLITE_OK
&& p
->dbRbu
){
4240 int rc2
= sqlite3_exec(p
->dbRbu
, "DELETE FROM stat.rbu_state", 0, 0, 0);
4241 if( p
->rc
==SQLITE_DONE
&& rc2
!=SQLITE_OK
) p
->rc
= rc2
;
4244 /* Close the open database handle and VFS object. */
4245 sqlite3_close(p
->dbRbu
);
4246 sqlite3_close(p
->dbMain
);
4247 assert( p
->szTemp
==0 );
4249 sqlite3_free(p
->aBuf
);
4250 sqlite3_free(p
->aFrame
);
4255 *pzErrmsg
= p
->zErrmsg
;
4257 sqlite3_free(p
->zErrmsg
);
4259 sqlite3_free(p
->zState
);
4269 ** Return the total number of key-value operations (inserts, deletes or
4270 ** updates) that have been performed on the target database since the
4271 ** current RBU update was started.
4273 sqlite3_int64
sqlite3rbu_progress(sqlite3rbu
*pRbu
){
4274 return pRbu
->nProgress
;
4278 ** Return permyriadage progress indications for the two main stages of
4281 void sqlite3rbu_bp_progress(sqlite3rbu
*p
, int *pnOne
, int *pnTwo
){
4282 const int MAX_PROGRESS
= 10000;
4283 switch( p
->eStage
){
4285 if( p
->nPhaseOneStep
>0 ){
4286 *pnOne
= (int)(MAX_PROGRESS
* (i64
)p
->nProgress
/(i64
)p
->nPhaseOneStep
);
4293 case RBU_STAGE_MOVE
:
4294 *pnOne
= MAX_PROGRESS
;
4298 case RBU_STAGE_CKPT
:
4299 *pnOne
= MAX_PROGRESS
;
4300 *pnTwo
= (int)(MAX_PROGRESS
* (i64
)p
->nStep
/ (i64
)p
->nFrame
);
4303 case RBU_STAGE_DONE
:
4304 *pnOne
= MAX_PROGRESS
;
4305 *pnTwo
= MAX_PROGRESS
;
4314 ** Return the current state of the RBU vacuum or update operation.
4316 int sqlite3rbu_state(sqlite3rbu
*p
){
4318 0, SQLITE_RBU_STATE_OAL
, SQLITE_RBU_STATE_MOVE
,
4319 0, SQLITE_RBU_STATE_CHECKPOINT
, SQLITE_RBU_STATE_DONE
4322 assert( RBU_STAGE_OAL
==1 );
4323 assert( RBU_STAGE_MOVE
==2 );
4324 assert( RBU_STAGE_CKPT
==4 );
4325 assert( RBU_STAGE_DONE
==5 );
4326 assert( aRes
[RBU_STAGE_OAL
]==SQLITE_RBU_STATE_OAL
);
4327 assert( aRes
[RBU_STAGE_MOVE
]==SQLITE_RBU_STATE_MOVE
);
4328 assert( aRes
[RBU_STAGE_CKPT
]==SQLITE_RBU_STATE_CHECKPOINT
);
4329 assert( aRes
[RBU_STAGE_DONE
]==SQLITE_RBU_STATE_DONE
);
4331 if( p
->rc
!=SQLITE_OK
&& p
->rc
!=SQLITE_DONE
){
4332 return SQLITE_RBU_STATE_ERROR
;
4334 assert( p
->rc
!=SQLITE_DONE
|| p
->eStage
==RBU_STAGE_DONE
);
4335 assert( p
->eStage
==RBU_STAGE_OAL
4336 || p
->eStage
==RBU_STAGE_MOVE
4337 || p
->eStage
==RBU_STAGE_CKPT
4338 || p
->eStage
==RBU_STAGE_DONE
4340 return aRes
[p
->eStage
];
4344 int sqlite3rbu_savestate(sqlite3rbu
*p
){
4346 if( rc
==SQLITE_DONE
) return SQLITE_OK
;
4348 assert( p
->eStage
>=RBU_STAGE_OAL
&& p
->eStage
<=RBU_STAGE_DONE
);
4349 if( p
->eStage
==RBU_STAGE_OAL
){
4350 assert( rc
!=SQLITE_DONE
);
4351 if( rc
==SQLITE_OK
) rc
= sqlite3_exec(p
->dbMain
, "COMMIT", 0, 0, 0);
4354 /* Sync the db file */
4355 if( rc
==SQLITE_OK
&& p
->eStage
==RBU_STAGE_CKPT
){
4356 sqlite3_file
*pDb
= p
->pTargetFd
->pReal
;
4357 rc
= pDb
->pMethods
->xSync(pDb
, SQLITE_SYNC_NORMAL
);
4361 rbuSaveState(p
, p
->eStage
);
4364 if( p
->eStage
==RBU_STAGE_OAL
){
4365 assert( rc
!=SQLITE_DONE
);
4366 if( rc
==SQLITE_OK
) rc
= sqlite3_exec(p
->dbRbu
, "COMMIT", 0, 0, 0);
4367 if( rc
==SQLITE_OK
){
4368 const char *zBegin
= rbuIsVacuum(p
) ? "BEGIN" : "BEGIN IMMEDIATE";
4369 rc
= sqlite3_exec(p
->dbRbu
, zBegin
, 0, 0, 0);
4371 if( rc
==SQLITE_OK
) rc
= sqlite3_exec(p
->dbMain
, "BEGIN IMMEDIATE", 0, 0,0);
4379 ** Default xRename callback for RBU.
4381 static int xDefaultRename(void *pArg
, const char *zOld
, const char *zNew
){
4383 #if defined(_WIN32_WCE)
4388 zWideOld
= rbuWinUtf8ToUnicode(zOld
);
4390 zWideNew
= rbuWinUtf8ToUnicode(zNew
);
4392 if( MoveFileW(zWideOld
, zWideNew
) ){
4397 sqlite3_free(zWideNew
);
4399 rc
= SQLITE_IOERR_NOMEM
;
4401 sqlite3_free(zWideOld
);
4403 rc
= SQLITE_IOERR_NOMEM
;
4407 rc
= rename(zOld
, zNew
) ? SQLITE_IOERR
: SQLITE_OK
;
4412 void sqlite3rbu_rename_handler(
4415 int (*xRename
)(void *pArg
, const char *zOld
, const char *zNew
)
4418 pRbu
->xRename
= xRename
;
4419 pRbu
->pRenameArg
= pArg
;
4421 pRbu
->xRename
= xDefaultRename
;
4422 pRbu
->pRenameArg
= 0;
4426 /**************************************************************************
4427 ** Beginning of RBU VFS shim methods. The VFS shim modifies the behaviour
4428 ** of a standard VFS in the following ways:
4430 ** 1. Whenever the first page of a main database file is read or
4431 ** written, the value of the change-counter cookie is stored in
4432 ** rbu_file.iCookie. Similarly, the value of the "write-version"
4433 ** database header field is stored in rbu_file.iWriteVer. This ensures
4434 ** that the values are always trustworthy within an open transaction.
4436 ** 2. Whenever an SQLITE_OPEN_WAL file is opened, the (rbu_file.pWalFd)
4437 ** member variable of the associated database file descriptor is set
4438 ** to point to the new file. A mutex protected linked list of all main
4439 ** db fds opened using a particular RBU VFS is maintained at
4440 ** rbu_vfs.pMain to facilitate this.
4442 ** 3. Using a new file-control "SQLITE_FCNTL_RBU", a main db rbu_file
4443 ** object can be marked as the target database of an RBU update. This
4444 ** turns on the following extra special behaviour:
4446 ** 3a. If xAccess() is called to check if there exists a *-wal file
4447 ** associated with an RBU target database currently in RBU_STAGE_OAL
4448 ** stage (preparing the *-oal file), the following special handling
4451 ** * if the *-wal file does exist, return SQLITE_CANTOPEN. An RBU
4452 ** target database may not be in wal mode already.
4454 ** * if the *-wal file does not exist, set the output parameter to
4455 ** non-zero (to tell SQLite that it does exist) anyway.
4457 ** Then, when xOpen() is called to open the *-wal file associated with
4458 ** the RBU target in RBU_STAGE_OAL stage, instead of opening the *-wal
4459 ** file, the rbu vfs opens the corresponding *-oal file instead.
4461 ** 3b. The *-shm pages returned by xShmMap() for a target db file in
4462 ** RBU_STAGE_OAL mode are actually stored in heap memory. This is to
4463 ** avoid creating a *-shm file on disk. Additionally, xShmLock() calls
4464 ** are no-ops on target database files in RBU_STAGE_OAL mode. This is
4465 ** because assert() statements in some VFS implementations fail if
4466 ** xShmLock() is called before xShmMap().
4468 ** 3c. If an EXCLUSIVE lock is attempted on a target database file in any
4469 ** mode except RBU_STAGE_DONE (all work completed and checkpointed), it
4470 ** fails with an SQLITE_BUSY error. This is to stop RBU connections
4471 ** from automatically checkpointing a *-wal (or *-oal) file from within
4474 ** 3d. In RBU_STAGE_CAPTURE mode, all xRead() calls on the wal file, and
4475 ** all xWrite() calls on the target database file perform no IO.
4476 ** Instead the frame and page numbers that would be read and written
4477 ** are recorded. Additionally, successful attempts to obtain exclusive
4478 ** xShmLock() WRITER, CHECKPOINTER and READ0 locks on the target
4479 ** database file are recorded. xShmLock() calls to unlock the same
4480 ** locks are no-ops (so that once obtained, these locks are never
4481 ** relinquished). Finally, calls to xSync() on the target database
4482 ** file fail with SQLITE_NOTICE errors.
4485 static void rbuUnlockShm(rbu_file
*p
){
4486 assert( p
->openFlags
& SQLITE_OPEN_MAIN_DB
);
4488 int (*xShmLock
)(sqlite3_file
*,int,int,int) = p
->pReal
->pMethods
->xShmLock
;
4490 for(i
=0; i
<SQLITE_SHM_NLOCK
;i
++){
4491 if( (1<<i
) & p
->pRbu
->mLock
){
4492 xShmLock(p
->pReal
, i
, 1, SQLITE_SHM_UNLOCK
|SQLITE_SHM_EXCLUSIVE
);
4501 static int rbuUpdateTempSize(rbu_file
*pFd
, sqlite3_int64 nNew
){
4502 sqlite3rbu
*pRbu
= pFd
->pRbu
;
4503 i64 nDiff
= nNew
- pFd
->sz
;
4504 pRbu
->szTemp
+= nDiff
;
4506 assert( pRbu
->szTemp
>=0 );
4507 if( pRbu
->szTempLimit
&& pRbu
->szTemp
>pRbu
->szTempLimit
) return SQLITE_FULL
;
4512 ** Add an item to the main-db lists, if it is not already present.
4514 ** There are two main-db lists. One for all file descriptors, and one
4515 ** for all file descriptors with rbu_file.pDb!=0. If the argument has
4516 ** rbu_file.pDb!=0, then it is assumed to already be present on the
4517 ** main list and is only added to the pDb!=0 list.
4519 static void rbuMainlistAdd(rbu_file
*p
){
4520 rbu_vfs
*pRbuVfs
= p
->pRbuVfs
;
4522 assert( (p
->openFlags
& SQLITE_OPEN_MAIN_DB
) );
4523 sqlite3_mutex_enter(pRbuVfs
->mutex
);
4525 for(pIter
=pRbuVfs
->pMain
; pIter
; pIter
=pIter
->pMainNext
);
4526 p
->pMainNext
= pRbuVfs
->pMain
;
4529 for(pIter
=pRbuVfs
->pMainRbu
; pIter
&& pIter
!=p
; pIter
=pIter
->pMainRbuNext
){}
4531 p
->pMainRbuNext
= pRbuVfs
->pMainRbu
;
4532 pRbuVfs
->pMainRbu
= p
;
4535 sqlite3_mutex_leave(pRbuVfs
->mutex
);
4539 ** Remove an item from the main-db lists.
4541 static void rbuMainlistRemove(rbu_file
*p
){
4543 sqlite3_mutex_enter(p
->pRbuVfs
->mutex
);
4544 for(pp
=&p
->pRbuVfs
->pMain
; *pp
&& *pp
!=p
; pp
=&((*pp
)->pMainNext
)){}
4545 if( *pp
) *pp
= p
->pMainNext
;
4547 for(pp
=&p
->pRbuVfs
->pMainRbu
; *pp
&& *pp
!=p
; pp
=&((*pp
)->pMainRbuNext
)){}
4548 if( *pp
) *pp
= p
->pMainRbuNext
;
4549 p
->pMainRbuNext
= 0;
4550 sqlite3_mutex_leave(p
->pRbuVfs
->mutex
);
4554 ** Given that zWal points to a buffer containing a wal file name passed to
4555 ** either the xOpen() or xAccess() VFS method, search the main-db list for
4556 ** a file-handle opened by the same database connection on the corresponding
4559 ** If parameter bRbu is true, only search for file-descriptors with
4562 static rbu_file
*rbuFindMaindb(rbu_vfs
*pRbuVfs
, const char *zWal
, int bRbu
){
4564 sqlite3_mutex_enter(pRbuVfs
->mutex
);
4566 for(pDb
=pRbuVfs
->pMainRbu
; pDb
&& pDb
->zWal
!=zWal
; pDb
=pDb
->pMainRbuNext
){}
4568 for(pDb
=pRbuVfs
->pMain
; pDb
&& pDb
->zWal
!=zWal
; pDb
=pDb
->pMainNext
){}
4570 sqlite3_mutex_leave(pRbuVfs
->mutex
);
4575 ** Close an rbu file.
4577 static int rbuVfsClose(sqlite3_file
*pFile
){
4578 rbu_file
*p
= (rbu_file
*)pFile
;
4582 /* Free the contents of the apShm[] array. And the array itself. */
4583 for(i
=0; i
<p
->nShm
; i
++){
4584 sqlite3_free(p
->apShm
[i
]);
4586 sqlite3_free(p
->apShm
);
4588 sqlite3_free(p
->zDel
);
4590 if( p
->openFlags
& SQLITE_OPEN_MAIN_DB
){
4591 const sqlite3_io_methods
*pMeth
= p
->pReal
->pMethods
;
4592 rbuMainlistRemove(p
);
4594 if( pMeth
->iVersion
>1 && pMeth
->xShmUnmap
){
4595 pMeth
->xShmUnmap(p
->pReal
, 0);
4598 else if( (p
->openFlags
& SQLITE_OPEN_DELETEONCLOSE
) && p
->pRbu
){
4599 rbuUpdateTempSize(p
, 0);
4601 assert( p
->pMainNext
==0 && p
->pRbuVfs
->pMain
!=p
);
4603 /* Close the underlying file handle */
4604 rc
= p
->pReal
->pMethods
->xClose(p
->pReal
);
4610 ** Read and return an unsigned 32-bit big-endian integer from the buffer
4611 ** passed as the only argument.
4613 static u32
rbuGetU32(u8
*aBuf
){
4614 return ((u32
)aBuf
[0] << 24)
4615 + ((u32
)aBuf
[1] << 16)
4616 + ((u32
)aBuf
[2] << 8)
4621 ** Write an unsigned 32-bit value in big-endian format to the supplied
4624 static void rbuPutU32(u8
*aBuf
, u32 iVal
){
4625 aBuf
[0] = (iVal
>> 24) & 0xFF;
4626 aBuf
[1] = (iVal
>> 16) & 0xFF;
4627 aBuf
[2] = (iVal
>> 8) & 0xFF;
4628 aBuf
[3] = (iVal
>> 0) & 0xFF;
4631 static void rbuPutU16(u8
*aBuf
, u16 iVal
){
4632 aBuf
[0] = (iVal
>> 8) & 0xFF;
4633 aBuf
[1] = (iVal
>> 0) & 0xFF;
4637 ** Read data from an rbuVfs-file.
4639 static int rbuVfsRead(
4640 sqlite3_file
*pFile
,
4645 rbu_file
*p
= (rbu_file
*)pFile
;
4646 sqlite3rbu
*pRbu
= p
->pRbu
;
4649 if( pRbu
&& pRbu
->eStage
==RBU_STAGE_CAPTURE
){
4650 assert( p
->openFlags
& SQLITE_OPEN_WAL
);
4651 rc
= rbuCaptureWalRead(p
->pRbu
, iOfst
, iAmt
);
4653 if( pRbu
&& pRbu
->eStage
==RBU_STAGE_OAL
4654 && (p
->openFlags
& SQLITE_OPEN_WAL
)
4655 && iOfst
>=pRbu
->iOalSz
4658 memset(zBuf
, 0, iAmt
);
4660 rc
= p
->pReal
->pMethods
->xRead(p
->pReal
, zBuf
, iAmt
, iOfst
);
4662 /* If this is being called to read the first page of the target
4663 ** database as part of an rbu vacuum operation, synthesize the
4664 ** contents of the first page if it does not yet exist. Otherwise,
4665 ** SQLite will not check for a *-wal file. */
4666 if( pRbu
&& rbuIsVacuum(pRbu
)
4667 && rc
==SQLITE_IOERR_SHORT_READ
&& iOfst
==0
4668 && (p
->openFlags
& SQLITE_OPEN_MAIN_DB
)
4669 && pRbu
->rc
==SQLITE_OK
4671 sqlite3_file
*pFd
= (sqlite3_file
*)pRbu
->pRbuFd
;
4672 rc
= pFd
->pMethods
->xRead(pFd
, zBuf
, iAmt
, iOfst
);
4673 if( rc
==SQLITE_OK
){
4674 u8
*aBuf
= (u8
*)zBuf
;
4675 u32 iRoot
= rbuGetU32(&aBuf
[52]) ? 1 : 0;
4676 rbuPutU32(&aBuf
[52], iRoot
); /* largest root page number */
4677 rbuPutU32(&aBuf
[36], 0); /* number of free pages */
4678 rbuPutU32(&aBuf
[32], 0); /* first page on free list trunk */
4679 rbuPutU32(&aBuf
[28], 1); /* size of db file in pages */
4680 rbuPutU32(&aBuf
[24], pRbu
->pRbuFd
->iCookie
+1); /* Change counter */
4683 memset(&aBuf
[100], 0, iAmt
-100);
4684 rbuPutU16(&aBuf
[105], iAmt
& 0xFFFF);
4691 if( rc
==SQLITE_OK
&& iOfst
==0 && (p
->openFlags
& SQLITE_OPEN_MAIN_DB
) ){
4692 /* These look like magic numbers. But they are stable, as they are part
4693 ** of the definition of the SQLite file format, which may not change. */
4694 u8
*pBuf
= (u8
*)zBuf
;
4695 p
->iCookie
= rbuGetU32(&pBuf
[24]);
4696 p
->iWriteVer
= pBuf
[19];
4703 ** Write data to an rbuVfs-file.
4705 static int rbuVfsWrite(
4706 sqlite3_file
*pFile
,
4711 rbu_file
*p
= (rbu_file
*)pFile
;
4712 sqlite3rbu
*pRbu
= p
->pRbu
;
4715 if( pRbu
&& pRbu
->eStage
==RBU_STAGE_CAPTURE
){
4716 assert( p
->openFlags
& SQLITE_OPEN_MAIN_DB
);
4717 rc
= rbuCaptureDbWrite(p
->pRbu
, iOfst
);
4720 if( pRbu
->eStage
==RBU_STAGE_OAL
4721 && (p
->openFlags
& SQLITE_OPEN_WAL
)
4722 && iOfst
>=pRbu
->iOalSz
4724 pRbu
->iOalSz
= iAmt
+ iOfst
;
4725 }else if( p
->openFlags
& SQLITE_OPEN_DELETEONCLOSE
){
4726 i64 szNew
= iAmt
+iOfst
;
4728 rc
= rbuUpdateTempSize(p
, szNew
);
4729 if( rc
!=SQLITE_OK
) return rc
;
4733 rc
= p
->pReal
->pMethods
->xWrite(p
->pReal
, zBuf
, iAmt
, iOfst
);
4734 if( rc
==SQLITE_OK
&& iOfst
==0 && (p
->openFlags
& SQLITE_OPEN_MAIN_DB
) ){
4735 /* These look like magic numbers. But they are stable, as they are part
4736 ** of the definition of the SQLite file format, which may not change. */
4737 u8
*pBuf
= (u8
*)zBuf
;
4738 p
->iCookie
= rbuGetU32(&pBuf
[24]);
4739 p
->iWriteVer
= pBuf
[19];
4746 ** Truncate an rbuVfs-file.
4748 static int rbuVfsTruncate(sqlite3_file
*pFile
, sqlite_int64 size
){
4749 rbu_file
*p
= (rbu_file
*)pFile
;
4750 if( (p
->openFlags
& SQLITE_OPEN_DELETEONCLOSE
) && p
->pRbu
){
4751 int rc
= rbuUpdateTempSize(p
, size
);
4752 if( rc
!=SQLITE_OK
) return rc
;
4754 return p
->pReal
->pMethods
->xTruncate(p
->pReal
, size
);
4758 ** Sync an rbuVfs-file.
4760 static int rbuVfsSync(sqlite3_file
*pFile
, int flags
){
4761 rbu_file
*p
= (rbu_file
*)pFile
;
4762 if( p
->pRbu
&& p
->pRbu
->eStage
==RBU_STAGE_CAPTURE
){
4763 if( p
->openFlags
& SQLITE_OPEN_MAIN_DB
){
4764 return SQLITE_NOTICE_RBU
;
4768 return p
->pReal
->pMethods
->xSync(p
->pReal
, flags
);
4772 ** Return the current file-size of an rbuVfs-file.
4774 static int rbuVfsFileSize(sqlite3_file
*pFile
, sqlite_int64
*pSize
){
4775 rbu_file
*p
= (rbu_file
*)pFile
;
4777 rc
= p
->pReal
->pMethods
->xFileSize(p
->pReal
, pSize
);
4779 /* If this is an RBU vacuum operation and this is the target database,
4780 ** pretend that it has at least one page. Otherwise, SQLite will not
4781 ** check for the existance of a *-wal file. rbuVfsRead() contains
4782 ** similar logic. */
4783 if( rc
==SQLITE_OK
&& *pSize
==0
4784 && p
->pRbu
&& rbuIsVacuum(p
->pRbu
)
4785 && (p
->openFlags
& SQLITE_OPEN_MAIN_DB
)
4793 ** Lock an rbuVfs-file.
4795 static int rbuVfsLock(sqlite3_file
*pFile
, int eLock
){
4796 rbu_file
*p
= (rbu_file
*)pFile
;
4797 sqlite3rbu
*pRbu
= p
->pRbu
;
4800 assert( p
->openFlags
& (SQLITE_OPEN_MAIN_DB
|SQLITE_OPEN_TEMP_DB
) );
4801 if( eLock
==SQLITE_LOCK_EXCLUSIVE
4802 && (p
->bNolock
|| (pRbu
&& pRbu
->eStage
!=RBU_STAGE_DONE
))
4804 /* Do not allow EXCLUSIVE locks. Preventing SQLite from taking this
4805 ** prevents it from checkpointing the database from sqlite3_close(). */
4808 rc
= p
->pReal
->pMethods
->xLock(p
->pReal
, eLock
);
4815 ** Unlock an rbuVfs-file.
4817 static int rbuVfsUnlock(sqlite3_file
*pFile
, int eLock
){
4818 rbu_file
*p
= (rbu_file
*)pFile
;
4819 return p
->pReal
->pMethods
->xUnlock(p
->pReal
, eLock
);
4823 ** Check if another file-handle holds a RESERVED lock on an rbuVfs-file.
4825 static int rbuVfsCheckReservedLock(sqlite3_file
*pFile
, int *pResOut
){
4826 rbu_file
*p
= (rbu_file
*)pFile
;
4827 return p
->pReal
->pMethods
->xCheckReservedLock(p
->pReal
, pResOut
);
4831 ** File control method. For custom operations on an rbuVfs-file.
4833 static int rbuVfsFileControl(sqlite3_file
*pFile
, int op
, void *pArg
){
4834 rbu_file
*p
= (rbu_file
*)pFile
;
4835 int (*xControl
)(sqlite3_file
*,int,void*) = p
->pReal
->pMethods
->xFileControl
;
4838 assert( p
->openFlags
& (SQLITE_OPEN_MAIN_DB
|SQLITE_OPEN_TEMP_DB
)
4839 || p
->openFlags
& (SQLITE_OPEN_TRANSIENT_DB
|SQLITE_OPEN_TEMP_JOURNAL
)
4841 if( op
==SQLITE_FCNTL_RBU
){
4842 sqlite3rbu
*pRbu
= (sqlite3rbu
*)pArg
;
4844 /* First try to find another RBU vfs lower down in the vfs stack. If
4845 ** one is found, this vfs will operate in pass-through mode. The lower
4846 ** level vfs will do the special RBU handling. */
4847 rc
= xControl(p
->pReal
, op
, pArg
);
4849 if( rc
==SQLITE_NOTFOUND
){
4850 /* Now search for a zipvfs instance lower down in the VFS stack. If
4851 ** one is found, this is an error. */
4853 rc
= xControl(p
->pReal
, SQLITE_FCNTL_ZIPVFS
, &dummy
);
4854 if( rc
==SQLITE_OK
){
4856 pRbu
->zErrmsg
= sqlite3_mprintf("rbu/zipvfs setup error");
4857 }else if( rc
==SQLITE_NOTFOUND
){
4858 pRbu
->pTargetFd
= p
;
4861 if( p
->pWalFd
) p
->pWalFd
->pRbu
= pRbu
;
4867 else if( op
==SQLITE_FCNTL_RBUCNT
){
4868 sqlite3rbu
*pRbu
= (sqlite3rbu
*)pArg
;
4874 rc
= xControl(p
->pReal
, op
, pArg
);
4875 if( rc
==SQLITE_OK
&& op
==SQLITE_FCNTL_VFSNAME
){
4876 rbu_vfs
*pRbuVfs
= p
->pRbuVfs
;
4877 char *zIn
= *(char**)pArg
;
4878 char *zOut
= sqlite3_mprintf("rbu(%s)/%z", pRbuVfs
->base
.zName
, zIn
);
4879 *(char**)pArg
= zOut
;
4880 if( zOut
==0 ) rc
= SQLITE_NOMEM
;
4887 ** Return the sector-size in bytes for an rbuVfs-file.
4889 static int rbuVfsSectorSize(sqlite3_file
*pFile
){
4890 rbu_file
*p
= (rbu_file
*)pFile
;
4891 return p
->pReal
->pMethods
->xSectorSize(p
->pReal
);
4895 ** Return the device characteristic flags supported by an rbuVfs-file.
4897 static int rbuVfsDeviceCharacteristics(sqlite3_file
*pFile
){
4898 rbu_file
*p
= (rbu_file
*)pFile
;
4899 return p
->pReal
->pMethods
->xDeviceCharacteristics(p
->pReal
);
4903 ** Take or release a shared-memory lock.
4905 static int rbuVfsShmLock(sqlite3_file
*pFile
, int ofst
, int n
, int flags
){
4906 rbu_file
*p
= (rbu_file
*)pFile
;
4907 sqlite3rbu
*pRbu
= p
->pRbu
;
4910 #ifdef SQLITE_AMALGAMATION
4911 assert( WAL_CKPT_LOCK
==1 );
4914 assert( p
->openFlags
& (SQLITE_OPEN_MAIN_DB
|SQLITE_OPEN_TEMP_DB
) );
4916 pRbu
->eStage
==RBU_STAGE_OAL
4917 || pRbu
->eStage
==RBU_STAGE_MOVE
4918 || pRbu
->eStage
==RBU_STAGE_DONE
4920 /* Prevent SQLite from taking a shm-lock on the target file when it
4921 ** is supplying heap memory to the upper layer in place of *-shm
4923 if( ofst
==WAL_LOCK_CKPT
&& n
==1 ) rc
= SQLITE_BUSY
;
4926 if( pRbu
&& pRbu
->eStage
==RBU_STAGE_CAPTURE
){
4929 if( bCapture
==0 || 0==(flags
& SQLITE_SHM_UNLOCK
) ){
4930 rc
= p
->pReal
->pMethods
->xShmLock(p
->pReal
, ofst
, n
, flags
);
4931 if( bCapture
&& rc
==SQLITE_OK
){
4932 pRbu
->mLock
|= ((1<<n
) - 1) << ofst
;
4941 ** Obtain a pointer to a mapping of a single 32KiB page of the *-shm file.
4943 static int rbuVfsShmMap(
4944 sqlite3_file
*pFile
,
4950 rbu_file
*p
= (rbu_file
*)pFile
;
4952 int eStage
= (p
->pRbu
? p
->pRbu
->eStage
: 0);
4954 /* If not in RBU_STAGE_OAL, allow this call to pass through. Or, if this
4955 ** rbu is in the RBU_STAGE_OAL state, use heap memory for *-shm space
4956 ** instead of a file on disk. */
4957 assert( p
->openFlags
& (SQLITE_OPEN_MAIN_DB
|SQLITE_OPEN_TEMP_DB
) );
4958 if( eStage
==RBU_STAGE_OAL
){
4959 sqlite3_int64 nByte
= (iRegion
+1) * sizeof(char*);
4960 char **apNew
= (char**)sqlite3_realloc64(p
->apShm
, nByte
);
4962 /* This is an RBU connection that uses its own heap memory for the
4963 ** pages of the *-shm file. Since no other process can have run
4964 ** recovery, the connection must request *-shm pages in order
4965 ** from start to finish. */
4966 assert( iRegion
==p
->nShm
);
4970 memset(&apNew
[p
->nShm
], 0, sizeof(char*) * (1 + iRegion
- p
->nShm
));
4972 p
->nShm
= iRegion
+1;
4975 if( rc
==SQLITE_OK
){
4976 char *pNew
= (char*)sqlite3_malloc64(szRegion
);
4980 memset(pNew
, 0, szRegion
);
4981 p
->apShm
[iRegion
] = pNew
;
4985 if( rc
==SQLITE_OK
){
4986 *pp
= p
->apShm
[iRegion
];
4991 assert( p
->apShm
==0 );
4992 rc
= p
->pReal
->pMethods
->xShmMap(p
->pReal
, iRegion
, szRegion
, isWrite
, pp
);
5001 static void rbuVfsShmBarrier(sqlite3_file
*pFile
){
5002 rbu_file
*p
= (rbu_file
*)pFile
;
5003 p
->pReal
->pMethods
->xShmBarrier(p
->pReal
);
5007 ** The xShmUnmap method.
5009 static int rbuVfsShmUnmap(sqlite3_file
*pFile
, int delFlag
){
5010 rbu_file
*p
= (rbu_file
*)pFile
;
5012 int eStage
= (p
->pRbu
? p
->pRbu
->eStage
: 0);
5014 assert( p
->openFlags
& (SQLITE_OPEN_MAIN_DB
|SQLITE_OPEN_TEMP_DB
) );
5015 if( eStage
==RBU_STAGE_OAL
|| eStage
==RBU_STAGE_MOVE
){
5018 /* Release the checkpointer and writer locks */
5020 rc
= p
->pReal
->pMethods
->xShmUnmap(p
->pReal
, delFlag
);
5026 ** Open an rbu file handle.
5028 static int rbuVfsOpen(
5031 sqlite3_file
*pFile
,
5035 static sqlite3_io_methods rbuvfs_io_methods
= {
5037 rbuVfsClose
, /* xClose */
5038 rbuVfsRead
, /* xRead */
5039 rbuVfsWrite
, /* xWrite */
5040 rbuVfsTruncate
, /* xTruncate */
5041 rbuVfsSync
, /* xSync */
5042 rbuVfsFileSize
, /* xFileSize */
5043 rbuVfsLock
, /* xLock */
5044 rbuVfsUnlock
, /* xUnlock */
5045 rbuVfsCheckReservedLock
, /* xCheckReservedLock */
5046 rbuVfsFileControl
, /* xFileControl */
5047 rbuVfsSectorSize
, /* xSectorSize */
5048 rbuVfsDeviceCharacteristics
, /* xDeviceCharacteristics */
5049 rbuVfsShmMap
, /* xShmMap */
5050 rbuVfsShmLock
, /* xShmLock */
5051 rbuVfsShmBarrier
, /* xShmBarrier */
5052 rbuVfsShmUnmap
, /* xShmUnmap */
5053 0, 0 /* xFetch, xUnfetch */
5055 static sqlite3_io_methods rbuvfs_io_methods1
= {
5057 rbuVfsClose
, /* xClose */
5058 rbuVfsRead
, /* xRead */
5059 rbuVfsWrite
, /* xWrite */
5060 rbuVfsTruncate
, /* xTruncate */
5061 rbuVfsSync
, /* xSync */
5062 rbuVfsFileSize
, /* xFileSize */
5063 rbuVfsLock
, /* xLock */
5064 rbuVfsUnlock
, /* xUnlock */
5065 rbuVfsCheckReservedLock
, /* xCheckReservedLock */
5066 rbuVfsFileControl
, /* xFileControl */
5067 rbuVfsSectorSize
, /* xSectorSize */
5068 rbuVfsDeviceCharacteristics
, /* xDeviceCharacteristics */
5074 rbu_vfs
*pRbuVfs
= (rbu_vfs
*)pVfs
;
5075 sqlite3_vfs
*pRealVfs
= pRbuVfs
->pRealVfs
;
5076 rbu_file
*pFd
= (rbu_file
*)pFile
;
5078 const char *zOpen
= zName
;
5081 memset(pFd
, 0, sizeof(rbu_file
));
5082 pFd
->pReal
= (sqlite3_file
*)&pFd
[1];
5083 pFd
->pRbuVfs
= pRbuVfs
;
5084 pFd
->openFlags
= flags
;
5086 if( flags
& SQLITE_OPEN_MAIN_DB
){
5087 /* A main database has just been opened. The following block sets
5088 ** (pFd->zWal) to point to a buffer owned by SQLite that contains
5089 ** the name of the *-wal file this db connection will use. SQLite
5090 ** happens to pass a pointer to this buffer when using xAccess()
5091 ** or xOpen() to operate on the *-wal file. */
5092 pFd
->zWal
= sqlite3_filename_wal(zName
);
5094 else if( flags
& SQLITE_OPEN_WAL
){
5095 rbu_file
*pDb
= rbuFindMaindb(pRbuVfs
, zName
, 0);
5097 if( pDb
->pRbu
&& pDb
->pRbu
->eStage
==RBU_STAGE_OAL
){
5098 /* This call is to open a *-wal file. Intead, open the *-oal. */
5100 if( rbuIsVacuum(pDb
->pRbu
) ){
5101 zOpen
= sqlite3_db_filename(pDb
->pRbu
->dbRbu
, "main");
5102 zOpen
= sqlite3_filename_wal(zOpen
);
5104 nOpen
= strlen(zOpen
);
5105 ((char*)zOpen
)[nOpen
-3] = 'o';
5106 pFd
->pRbu
= pDb
->pRbu
;
5112 pFd
->pRbu
= pRbuVfs
->pRbu
;
5115 if( oflags
& SQLITE_OPEN_MAIN_DB
5116 && sqlite3_uri_boolean(zName
, "rbu_memory", 0)
5118 assert( oflags
& SQLITE_OPEN_MAIN_DB
);
5119 oflags
= SQLITE_OPEN_TEMP_DB
| SQLITE_OPEN_READWRITE
| SQLITE_OPEN_CREATE
|
5120 SQLITE_OPEN_EXCLUSIVE
| SQLITE_OPEN_DELETEONCLOSE
;
5124 if( rc
==SQLITE_OK
){
5125 rc
= pRealVfs
->xOpen(pRealVfs
, zOpen
, pFd
->pReal
, oflags
, pOutFlags
);
5127 if( pFd
->pReal
->pMethods
){
5128 const sqlite3_io_methods
*pMeth
= pFd
->pReal
->pMethods
;
5129 /* The xOpen() operation has succeeded. Set the sqlite3_file.pMethods
5130 ** pointer and, if the file is a main database file, link it into the
5131 ** mutex protected linked list of all such files. */
5132 if( pMeth
->iVersion
<2 || pMeth
->xShmLock
==0 ){
5133 pFile
->pMethods
= &rbuvfs_io_methods1
;
5135 pFile
->pMethods
= &rbuvfs_io_methods
;
5137 if( flags
& SQLITE_OPEN_MAIN_DB
){
5138 rbuMainlistAdd(pFd
);
5141 sqlite3_free(pFd
->zDel
);
5148 ** Delete the file located at zPath.
5150 static int rbuVfsDelete(sqlite3_vfs
*pVfs
, const char *zPath
, int dirSync
){
5151 sqlite3_vfs
*pRealVfs
= ((rbu_vfs
*)pVfs
)->pRealVfs
;
5152 return pRealVfs
->xDelete(pRealVfs
, zPath
, dirSync
);
5156 ** Test for access permissions. Return true if the requested permission
5157 ** is available, or false otherwise.
5159 static int rbuVfsAccess(
5165 rbu_vfs
*pRbuVfs
= (rbu_vfs
*)pVfs
;
5166 sqlite3_vfs
*pRealVfs
= pRbuVfs
->pRealVfs
;
5169 rc
= pRealVfs
->xAccess(pRealVfs
, zPath
, flags
, pResOut
);
5171 /* If this call is to check if a *-wal file associated with an RBU target
5172 ** database connection exists, and the RBU update is in RBU_STAGE_OAL,
5173 ** the following special handling is activated:
5175 ** a) if the *-wal file does exist, return SQLITE_CANTOPEN. This
5176 ** ensures that the RBU extension never tries to update a database
5177 ** in wal mode, even if the first page of the database file has
5180 ** b) if the *-wal file does not exist, claim that it does anyway,
5181 ** causing SQLite to call xOpen() to open it. This call will also
5182 ** be intercepted (see the rbuVfsOpen() function) and the *-oal
5183 ** file opened instead.
5185 if( rc
==SQLITE_OK
&& flags
==SQLITE_ACCESS_EXISTS
){
5186 rbu_file
*pDb
= rbuFindMaindb(pRbuVfs
, zPath
, 1);
5187 if( pDb
&& pDb
->pRbu
->eStage
==RBU_STAGE_OAL
){
5188 assert( pDb
->pRbu
);
5190 rc
= SQLITE_CANTOPEN
;
5192 sqlite3_int64 sz
= 0;
5193 rc
= rbuVfsFileSize(&pDb
->base
, &sz
);
5203 ** Populate buffer zOut with the full canonical pathname corresponding
5204 ** to the pathname in zPath. zOut is guaranteed to point to a buffer
5205 ** of at least (DEVSYM_MAX_PATHNAME+1) bytes.
5207 static int rbuVfsFullPathname(
5213 sqlite3_vfs
*pRealVfs
= ((rbu_vfs
*)pVfs
)->pRealVfs
;
5214 return pRealVfs
->xFullPathname(pRealVfs
, zPath
, nOut
, zOut
);
5217 #ifndef SQLITE_OMIT_LOAD_EXTENSION
5219 ** Open the dynamic library located at zPath and return a handle.
5221 static void *rbuVfsDlOpen(sqlite3_vfs
*pVfs
, const char *zPath
){
5222 sqlite3_vfs
*pRealVfs
= ((rbu_vfs
*)pVfs
)->pRealVfs
;
5223 return pRealVfs
->xDlOpen(pRealVfs
, zPath
);
5227 ** Populate the buffer zErrMsg (size nByte bytes) with a human readable
5228 ** utf-8 string describing the most recent error encountered associated
5229 ** with dynamic libraries.
5231 static void rbuVfsDlError(sqlite3_vfs
*pVfs
, int nByte
, char *zErrMsg
){
5232 sqlite3_vfs
*pRealVfs
= ((rbu_vfs
*)pVfs
)->pRealVfs
;
5233 pRealVfs
->xDlError(pRealVfs
, nByte
, zErrMsg
);
5237 ** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
5239 static void (*rbuVfsDlSym(
5244 sqlite3_vfs
*pRealVfs
= ((rbu_vfs
*)pVfs
)->pRealVfs
;
5245 return pRealVfs
->xDlSym(pRealVfs
, pArg
, zSym
);
5249 ** Close the dynamic library handle pHandle.
5251 static void rbuVfsDlClose(sqlite3_vfs
*pVfs
, void *pHandle
){
5252 sqlite3_vfs
*pRealVfs
= ((rbu_vfs
*)pVfs
)->pRealVfs
;
5253 pRealVfs
->xDlClose(pRealVfs
, pHandle
);
5255 #endif /* SQLITE_OMIT_LOAD_EXTENSION */
5258 ** Populate the buffer pointed to by zBufOut with nByte bytes of
5261 static int rbuVfsRandomness(sqlite3_vfs
*pVfs
, int nByte
, char *zBufOut
){
5262 sqlite3_vfs
*pRealVfs
= ((rbu_vfs
*)pVfs
)->pRealVfs
;
5263 return pRealVfs
->xRandomness(pRealVfs
, nByte
, zBufOut
);
5267 ** Sleep for nMicro microseconds. Return the number of microseconds
5270 static int rbuVfsSleep(sqlite3_vfs
*pVfs
, int nMicro
){
5271 sqlite3_vfs
*pRealVfs
= ((rbu_vfs
*)pVfs
)->pRealVfs
;
5272 return pRealVfs
->xSleep(pRealVfs
, nMicro
);
5276 ** Return the current time as a Julian Day number in *pTimeOut.
5278 static int rbuVfsCurrentTime(sqlite3_vfs
*pVfs
, double *pTimeOut
){
5279 sqlite3_vfs
*pRealVfs
= ((rbu_vfs
*)pVfs
)->pRealVfs
;
5280 return pRealVfs
->xCurrentTime(pRealVfs
, pTimeOut
);
5286 static int rbuVfsGetLastError(sqlite3_vfs
*pVfs
, int a
, char *b
){
5291 ** Deregister and destroy an RBU vfs created by an earlier call to
5292 ** sqlite3rbu_create_vfs().
5294 void sqlite3rbu_destroy_vfs(const char *zName
){
5295 sqlite3_vfs
*pVfs
= sqlite3_vfs_find(zName
);
5296 if( pVfs
&& pVfs
->xOpen
==rbuVfsOpen
){
5297 sqlite3_mutex_free(((rbu_vfs
*)pVfs
)->mutex
);
5298 sqlite3_vfs_unregister(pVfs
);
5304 ** Create an RBU VFS named zName that accesses the underlying file-system
5305 ** via existing VFS zParent. The new object is registered as a non-default
5306 ** VFS with SQLite before returning.
5308 int sqlite3rbu_create_vfs(const char *zName
, const char *zParent
){
5310 /* Template for VFS */
5311 static sqlite3_vfs vfs_template
= {
5318 rbuVfsOpen
, /* xOpen */
5319 rbuVfsDelete
, /* xDelete */
5320 rbuVfsAccess
, /* xAccess */
5321 rbuVfsFullPathname
, /* xFullPathname */
5323 #ifndef SQLITE_OMIT_LOAD_EXTENSION
5324 rbuVfsDlOpen
, /* xDlOpen */
5325 rbuVfsDlError
, /* xDlError */
5326 rbuVfsDlSym
, /* xDlSym */
5327 rbuVfsDlClose
, /* xDlClose */
5332 rbuVfsRandomness
, /* xRandomness */
5333 rbuVfsSleep
, /* xSleep */
5334 rbuVfsCurrentTime
, /* xCurrentTime */
5335 rbuVfsGetLastError
, /* xGetLastError */
5336 0, /* xCurrentTimeInt64 (version 2) */
5337 0, 0, 0 /* Unimplemented version 3 methods */
5340 rbu_vfs
*pNew
= 0; /* Newly allocated VFS */
5345 nName
= strlen(zName
);
5346 nByte
= sizeof(rbu_vfs
) + nName
+ 1;
5347 pNew
= (rbu_vfs
*)sqlite3_malloc64(nByte
);
5351 sqlite3_vfs
*pParent
; /* Parent VFS */
5352 memset(pNew
, 0, nByte
);
5353 pParent
= sqlite3_vfs_find(zParent
);
5355 rc
= SQLITE_NOTFOUND
;
5358 memcpy(&pNew
->base
, &vfs_template
, sizeof(sqlite3_vfs
));
5359 pNew
->base
.mxPathname
= pParent
->mxPathname
;
5360 pNew
->base
.szOsFile
= sizeof(rbu_file
) + pParent
->szOsFile
;
5361 pNew
->pRealVfs
= pParent
;
5362 pNew
->base
.zName
= (const char*)(zSpace
= (char*)&pNew
[1]);
5363 memcpy(zSpace
, zName
, nName
);
5365 /* Allocate the mutex and register the new VFS (not as the default) */
5366 pNew
->mutex
= sqlite3_mutex_alloc(SQLITE_MUTEX_RECURSIVE
);
5367 if( pNew
->mutex
==0 ){
5370 rc
= sqlite3_vfs_register(&pNew
->base
, 0);
5374 if( rc
!=SQLITE_OK
){
5375 sqlite3_mutex_free(pNew
->mutex
);
5384 ** Configure the aggregate temp file size limit for this RBU handle.
5386 sqlite3_int64
sqlite3rbu_temp_size_limit(sqlite3rbu
*pRbu
, sqlite3_int64 n
){
5388 pRbu
->szTempLimit
= n
;
5390 return pRbu
->szTempLimit
;
5393 sqlite3_int64
sqlite3rbu_temp_size(sqlite3rbu
*pRbu
){
5394 return pRbu
->szTemp
;
5398 /**************************************************************************/
5400 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RBU) */