Fossil SCM

Update the built-in SQLite to the second 3.8.11 beta.

drh 2015-07-23 22:14 trunk
Commit eb423a74b5792807f164404ac8929d51778ff4c8
2 files changed +950 -944 +5 -5
+950 -944
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -325,11 +325,11 @@
325325
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
326326
** [sqlite_version()] and [sqlite_source_id()].
327327
*/
328328
#define SQLITE_VERSION "3.8.11"
329329
#define SQLITE_VERSION_NUMBER 3008011
330
-#define SQLITE_SOURCE_ID "2015-07-16 18:18:19 b79a4affe44bd0c8e155cae19f3f62c715684cd6"
330
+#define SQLITE_SOURCE_ID "2015-07-23 20:44:49 017c5019e1ce042025d4f327e50ec50af49f9fa4"
331331
332332
/*
333333
** CAPI3REF: Run-Time Library Version Numbers
334334
** KEYWORDS: sqlite3_version, sqlite3_sourceid
335335
**
@@ -1179,13 +1179,13 @@
11791179
**
11801180
** <li>[[SQLITE_FCNTL_ZIPVFS]]
11811181
** The [SQLITE_FCNTL_ZIPVFS] opcode is implemented by zipvfs only. All other
11821182
** VFS should return SQLITE_NOTFOUND for this opcode.
11831183
**
1184
-** <li>[[SQLITE_FCNTL_OTA]]
1185
-** The [SQLITE_FCNTL_OTA] opcode is implemented by the special VFS used by
1186
-** the OTA extension only. All other VFS should return SQLITE_NOTFOUND for
1184
+** <li>[[SQLITE_FCNTL_RBU]]
1185
+** The [SQLITE_FCNTL_RBU] opcode is implemented by the special VFS used by
1186
+** the RBU extension only. All other VFS should return SQLITE_NOTFOUND for
11871187
** this opcode.
11881188
** </ul>
11891189
*/
11901190
#define SQLITE_FCNTL_LOCKSTATE 1
11911191
#define SQLITE_FCNTL_GET_LOCKPROXYFILE 2
@@ -1209,11 +1209,11 @@
12091209
#define SQLITE_FCNTL_SYNC 21
12101210
#define SQLITE_FCNTL_COMMIT_PHASETWO 22
12111211
#define SQLITE_FCNTL_WIN32_SET_HANDLE 23
12121212
#define SQLITE_FCNTL_WAL_BLOCK 24
12131213
#define SQLITE_FCNTL_ZIPVFS 25
1214
-#define SQLITE_FCNTL_OTA 26
1214
+#define SQLITE_FCNTL_RBU 26
12151215
12161216
/* deprecated names */
12171217
#define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE
12181218
#define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE
12191219
#define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO
@@ -40711,22 +40711,22 @@
4071140711
/*
4071240712
** Try to initialize the pCache->pFree and pCache->pBulk fields. Return
4071340713
** true if pCache->pFree ends up containing one or more free pages.
4071440714
*/
4071540715
static int pcache1InitBulk(PCache1 *pCache){
40716
- int szBulk;
40716
+ i64 szBulk;
4071740717
char *zBulk;
4071840718
if( pcache1.nInitPage==0 ) return 0;
4071940719
/* Do not bother with a bulk allocation if the cache size very small */
4072040720
if( pCache->nMax<3 ) return 0;
4072140721
sqlite3BeginBenignMalloc();
4072240722
if( pcache1.nInitPage>0 ){
40723
- szBulk = pCache->szAlloc * pcache1.nInitPage;
40723
+ szBulk = pCache->szAlloc * (i64)pcache1.nInitPage;
4072440724
}else{
40725
- szBulk = -1024*pcache1.nInitPage;
40725
+ szBulk = -1024 * (i64)pcache1.nInitPage;
4072640726
}
40727
- if( szBulk > pCache->szAlloc*pCache->nMax ){
40727
+ if( szBulk > pCache->szAlloc*(i64)pCache->nMax ){
4072840728
szBulk = pCache->szAlloc*pCache->nMax;
4072940729
}
4073040730
zBulk = pCache->pBulk = sqlite3Malloc( szBulk );
4073140731
sqlite3EndBenignMalloc();
4073240732
if( zBulk ){
@@ -50252,13 +50252,13 @@
5025250252
5025350253
assert( pWal->writeLock );
5025450254
pWal->hdr.isInit = 1;
5025550255
pWal->hdr.iVersion = WALINDEX_MAX_VERSION;
5025650256
walChecksumBytes(1, (u8*)&pWal->hdr, nCksum, 0, pWal->hdr.aCksum);
50257
- memcpy((void *)&aHdr[1], (void *)&pWal->hdr, sizeof(WalIndexHdr));
50257
+ memcpy((void*)&aHdr[1], (const void*)&pWal->hdr, sizeof(WalIndexHdr));
5025850258
walShmBarrier(pWal);
50259
- memcpy((void *)&aHdr[0], (void *)&pWal->hdr, sizeof(WalIndexHdr));
50259
+ memcpy((void*)&aHdr[0], (const void*)&pWal->hdr, sizeof(WalIndexHdr));
5026050260
}
5026150261
5026250262
/*
5026350263
** This function encodes a single frame header and writes it to a buffer
5026450264
** supplied by the caller. A frame-header is made up of a series of
@@ -50556,17 +50556,17 @@
5055650556
#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
5055750557
/* Verify that the every entry in the mapping region is still reachable
5055850558
** via the hash table even after the cleanup.
5055950559
*/
5056050560
if( iLimit ){
50561
- int i; /* Loop counter */
50561
+ int j; /* Loop counter */
5056250562
int iKey; /* Hash key */
50563
- for(i=1; i<=iLimit; i++){
50564
- for(iKey=walHash(aPgno[i]); aHash[iKey]; iKey=walNextHash(iKey)){
50565
- if( aHash[iKey]==i ) break;
50563
+ for(j=1; j<=iLimit; j++){
50564
+ for(iKey=walHash(aPgno[j]); aHash[iKey]; iKey=walNextHash(iKey)){
50565
+ if( aHash[iKey]==j ) break;
5056650566
}
50567
- assert( aHash[iKey]==i );
50567
+ assert( aHash[iKey]==j );
5056850568
}
5056950569
}
5057050570
#endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
5057150571
}
5057250572
@@ -118653,11 +118653,15 @@
118653118653
118654118654
/* Read the PK into an array of temp registers. */
118655118655
r = sqlite3GetTempRange(pParse, nPk);
118656118656
for(iPk=0; iPk<nPk; iPk++){
118657118657
int iCol = pPk->aiColumn[iPk];
118658
- sqlite3ExprCodeGetColumn(pParse, pTab, iCol, iCur, r+iPk, 0);
118658
+ int rx;
118659
+ rx = sqlite3ExprCodeGetColumn(pParse, pTab, iCol, iCur,r+iPk,0);
118660
+ if( rx!=r+iPk ){
118661
+ sqlite3VdbeAddOp2(v, OP_SCopy, rx, r+iPk);
118662
+ }
118659118663
}
118660118664
118661118665
/* Check if the temp table already contains this key. If so,
118662118666
** the row has already been included in the result set and
118663118667
** can be ignored (by jumping past the Gosub below). Otherwise,
@@ -130036,10 +130040,11 @@
130036130040
** space for the lookaside memory is obtained from sqlite3_malloc().
130037130041
** If pStart is not NULL then it is sz*cnt bytes of memory to use for
130038130042
** the lookaside memory.
130039130043
*/
130040130044
static int setupLookaside(sqlite3 *db, void *pBuf, int sz, int cnt){
130045
+#ifndef SQLITE_OMIT_LOOKASIDE
130041130046
void *pStart;
130042130047
if( db->lookaside.nOut ){
130043130048
return SQLITE_BUSY;
130044130049
}
130045130050
/* Free any existing lookaside buffer for this handle before
@@ -130086,10 +130091,11 @@
130086130091
db->lookaside.pStart = db;
130087130092
db->lookaside.pEnd = db;
130088130093
db->lookaside.bEnabled = 0;
130089130094
db->lookaside.bMalloced = 0;
130090130095
}
130096
+#endif /* SQLITE_OMIT_LOOKASIDE */
130091130097
return SQLITE_OK;
130092130098
}
130093130099
130094130100
/*
130095130101
** Return the mutex associated with a database connection.
@@ -156830,11 +156836,11 @@
156830156836
156831156837
#endif /* defined(SQLITE_ENABLE_ICU) */
156832156838
#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
156833156839
156834156840
/************** End of fts3_icu.c ********************************************/
156835
-/************** Begin file sqlite3ota.c **************************************/
156841
+/************** Begin file sqlite3rbu.c **************************************/
156836156842
/*
156837156843
** 2014 August 30
156838156844
**
156839156845
** The author disclaims copyright to this source code. In place of
156840156846
** a legal notice, here is a blessing:
@@ -156846,15 +156852,15 @@
156846156852
*************************************************************************
156847156853
**
156848156854
**
156849156855
** OVERVIEW
156850156856
**
156851
-** The OTA extension requires that the OTA update be packaged as an
156857
+** The RBU extension requires that the RBU update be packaged as an
156852156858
** SQLite database. The tables it expects to find are described in
156853
-** sqlite3ota.h. Essentially, for each table xyz in the target database
156859
+** sqlite3rbu.h. Essentially, for each table xyz in the target database
156854156860
** that the user wishes to write to, a corresponding data_xyz table is
156855
-** created in the OTA database and populated with one row for each row to
156861
+** created in the RBU database and populated with one row for each row to
156856156862
** update, insert or delete from the target table.
156857156863
**
156858156864
** The update proceeds in three stages:
156859156865
**
156860156866
** 1) The database is updated. The modified database pages are written
@@ -156862,57 +156868,57 @@
156862156868
** that it is named "<database>-oal" instead of "<database>-wal".
156863156869
** Because regular SQLite clients do not look for file named
156864156870
** "<database>-oal", they go on using the original database in
156865156871
** rollback mode while the *-oal file is being generated.
156866156872
**
156867
-** During this stage OTA does not update the database by writing
156873
+** During this stage RBU does not update the database by writing
156868156874
** directly to the target tables. Instead it creates "imposter"
156869156875
** tables using the SQLITE_TESTCTRL_IMPOSTER interface that it uses
156870156876
** to update each b-tree individually. All updates required by each
156871156877
** b-tree are completed before moving on to the next, and all
156872156878
** updates are done in sorted key order.
156873156879
**
156874156880
** 2) The "<database>-oal" file is moved to the equivalent "<database>-wal"
156875
-** location using a call to rename(2). Before doing this the OTA
156881
+** location using a call to rename(2). Before doing this the RBU
156876156882
** module takes an EXCLUSIVE lock on the database file, ensuring
156877156883
** that there are no other active readers.
156878156884
**
156879156885
** Once the EXCLUSIVE lock is released, any other database readers
156880156886
** detect the new *-wal file and read the database in wal mode. At
156881156887
** this point they see the new version of the database - including
156882
-** the updates made as part of the OTA update.
156888
+** the updates made as part of the RBU update.
156883156889
**
156884156890
** 3) The new *-wal file is checkpointed. This proceeds in the same way
156885156891
** as a regular database checkpoint, except that a single frame is
156886
-** checkpointed each time sqlite3ota_step() is called. If the OTA
156892
+** checkpointed each time sqlite3rbu_step() is called. If the RBU
156887156893
** handle is closed before the entire *-wal file is checkpointed,
156888
-** the checkpoint progress is saved in the OTA database and the
156889
-** checkpoint can be resumed by another OTA client at some point in
156894
+** the checkpoint progress is saved in the RBU database and the
156895
+** checkpoint can be resumed by another RBU client at some point in
156890156896
** the future.
156891156897
**
156892156898
** POTENTIAL PROBLEMS
156893156899
**
156894
-** The rename() call might not be portable. And OTA is not currently
156900
+** The rename() call might not be portable. And RBU is not currently
156895156901
** syncing the directory after renaming the file.
156896156902
**
156897156903
** When state is saved, any commit to the *-oal file and the commit to
156898
-** the OTA update database are not atomic. So if the power fails at the
156904
+** the RBU update database are not atomic. So if the power fails at the
156899156905
** wrong moment they might get out of sync. As the main database will be
156900
-** committed before the OTA update database this will likely either just
156906
+** committed before the RBU update database this will likely either just
156901156907
** pass unnoticed, or result in SQLITE_CONSTRAINT errors (due to UNIQUE
156902156908
** constraint violations).
156903156909
**
156904
-** If some client does modify the target database mid OTA update, or some
156905
-** other error occurs, the OTA extension will keep throwing errors. It's
156910
+** If some client does modify the target database mid RBU update, or some
156911
+** other error occurs, the RBU extension will keep throwing errors. It's
156906156912
** not really clear how to get out of this state. The system could just
156907
-** by delete the OTA update database and *-oal file and have the device
156913
+** by delete the RBU update database and *-oal file and have the device
156908156914
** download the update again and start over.
156909156915
**
156910156916
** At present, for an UPDATE, both the new.* and old.* records are
156911
-** collected in the ota_xyz table. And for both UPDATEs and DELETEs all
156917
+** collected in the rbu_xyz table. And for both UPDATEs and DELETEs all
156912156918
** fields are collected. This means we're probably writing a lot more
156913
-** data to disk when saving the state of an ongoing update to the OTA
156919
+** data to disk when saving the state of an ongoing update to the RBU
156914156920
** update database than is strictly necessary.
156915156921
**
156916156922
*/
156917156923
156918156924
/* #include <assert.h> */
@@ -156920,13 +156926,13 @@
156920156926
/* #include <stdio.h> */
156921156927
/* #include <unistd.h> */
156922156928
156923156929
/* #include "sqlite3.h" */
156924156930
156925
-#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_OTA)
156926
-/************** Include sqlite3ota.h in the middle of sqlite3ota.c ***********/
156927
-/************** Begin file sqlite3ota.h **************************************/
156931
+#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RBU)
156932
+/************** Include sqlite3rbu.h in the middle of sqlite3rbu.c ***********/
156933
+/************** Begin file sqlite3rbu.h **************************************/
156928156934
/*
156929156935
** 2014 August 30
156930156936
**
156931156937
** The author disclaims copyright to this source code. In place of
156932156938
** a legal notice, here is a blessing:
@@ -156935,11 +156941,11 @@
156935156941
** May you find forgiveness for yourself and forgive others.
156936156942
** May you share freely, never taking more than you give.
156937156943
**
156938156944
*************************************************************************
156939156945
**
156940
-** This file contains the public interface for the OTA extension.
156946
+** This file contains the public interface for the RBU extension.
156941156947
*/
156942156948
156943156949
/*
156944156950
** SUMMARY
156945156951
**
@@ -156972,18 +156978,18 @@
156972156978
** mobile device that is frequently rebooted. Even after the writer process
156973156979
** has committed one or more sub-transactions, other database clients continue
156974156980
** to read from the original database snapshot. In other words, partially
156975156981
** applied transactions are not visible to other clients.
156976156982
**
156977
-** "OTA" stands for "Over The Air" update. As in a large database update
156983
+** "RBU" stands for "Over The Air" update. As in a large database update
156978156984
** transmitted via a wireless network to a mobile device. A transaction
156979
-** applied using this extension is hence refered to as an "OTA update".
156985
+** applied using this extension is hence refered to as an "RBU update".
156980156986
**
156981156987
**
156982156988
** LIMITATIONS
156983156989
**
156984
-** An "OTA update" transaction is subject to the following limitations:
156990
+** An "RBU update" transaction is subject to the following limitations:
156985156991
**
156986156992
** * The transaction must consist of INSERT, UPDATE and DELETE operations
156987156993
** only.
156988156994
**
156989156995
** * INSERT statements may not use any default values.
@@ -157004,430 +157010,430 @@
157004157010
** * No constraint handling mode except for "OR ROLLBACK" is supported.
157005157011
**
157006157012
**
157007157013
** PREPARATION
157008157014
**
157009
-** An "OTA update" is stored as a separate SQLite database. A database
157010
-** containing an OTA update is an "OTA database". For each table in the
157011
-** target database to be updated, the OTA database should contain a table
157015
+** An "RBU update" is stored as a separate SQLite database. A database
157016
+** containing an RBU update is an "RBU database". For each table in the
157017
+** target database to be updated, the RBU database should contain a table
157012157018
** named "data_<target name>" containing the same set of columns as the
157013
-** target table, and one more - "ota_control". The data_% table should
157019
+** target table, and one more - "rbu_control". The data_% table should
157014157020
** have no PRIMARY KEY or UNIQUE constraints, but each column should have
157015157021
** the same type as the corresponding column in the target database.
157016
-** The "ota_control" column should have no type at all. For example, if
157022
+** The "rbu_control" column should have no type at all. For example, if
157017157023
** the target database contains:
157018157024
**
157019157025
** CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT, c UNIQUE);
157020157026
**
157021
-** Then the OTA database should contain:
157027
+** Then the RBU database should contain:
157022157028
**
157023
-** CREATE TABLE data_t1(a INTEGER, b TEXT, c, ota_control);
157029
+** CREATE TABLE data_t1(a INTEGER, b TEXT, c, rbu_control);
157024157030
**
157025157031
** The order of the columns in the data_% table does not matter.
157026157032
**
157027157033
** If the target database table is a virtual table or a table that has no
157028157034
** PRIMARY KEY declaration, the data_% table must also contain a column
157029
-** named "ota_rowid". This column is mapped to the tables implicit primary
157035
+** named "rbu_rowid". This column is mapped to the tables implicit primary
157030157036
** key column - "rowid". Virtual tables for which the "rowid" column does
157031
-** not function like a primary key value cannot be updated using OTA. For
157037
+** not function like a primary key value cannot be updated using RBU. For
157032157038
** example, if the target db contains either of the following:
157033157039
**
157034157040
** CREATE VIRTUAL TABLE x1 USING fts3(a, b);
157035157041
** CREATE TABLE x1(a, b)
157036157042
**
157037
-** then the OTA database should contain:
157043
+** then the RBU database should contain:
157038157044
**
157039
-** CREATE TABLE data_x1(a, b, ota_rowid, ota_control);
157045
+** CREATE TABLE data_x1(a, b, rbu_rowid, rbu_control);
157040157046
**
157041157047
** All non-hidden columns (i.e. all columns matched by "SELECT *") of the
157042157048
** target table must be present in the input table. For virtual tables,
157043
-** hidden columns are optional - they are updated by OTA if present in
157049
+** hidden columns are optional - they are updated by RBU if present in
157044157050
** the input table, or not otherwise. For example, to write to an fts4
157045157051
** table with a hidden languageid column such as:
157046157052
**
157047157053
** CREATE VIRTUAL TABLE ft1 USING fts4(a, b, languageid='langid');
157048157054
**
157049157055
** Either of the following input table schemas may be used:
157050157056
**
157051
-** CREATE TABLE data_ft1(a, b, langid, ota_rowid, ota_control);
157052
-** CREATE TABLE data_ft1(a, b, ota_rowid, ota_control);
157057
+** CREATE TABLE data_ft1(a, b, langid, rbu_rowid, rbu_control);
157058
+** CREATE TABLE data_ft1(a, b, rbu_rowid, rbu_control);
157053157059
**
157054
-** For each row to INSERT into the target database as part of the OTA
157060
+** For each row to INSERT into the target database as part of the RBU
157055157061
** update, the corresponding data_% table should contain a single record
157056
-** with the "ota_control" column set to contain integer value 0. The
157062
+** with the "rbu_control" column set to contain integer value 0. The
157057157063
** other columns should be set to the values that make up the new record
157058157064
** to insert.
157059157065
**
157060157066
** If the target database table has an INTEGER PRIMARY KEY, it is not
157061157067
** possible to insert a NULL value into the IPK column. Attempting to
157062157068
** do so results in an SQLITE_MISMATCH error.
157063157069
**
157064
-** For each row to DELETE from the target database as part of the OTA
157070
+** For each row to DELETE from the target database as part of the RBU
157065157071
** update, the corresponding data_% table should contain a single record
157066
-** with the "ota_control" column set to contain integer value 1. The
157072
+** with the "rbu_control" column set to contain integer value 1. The
157067157073
** real primary key values of the row to delete should be stored in the
157068157074
** corresponding columns of the data_% table. The values stored in the
157069157075
** other columns are not used.
157070157076
**
157071
-** For each row to UPDATE from the target database as part of the OTA
157077
+** For each row to UPDATE from the target database as part of the RBU
157072157078
** update, the corresponding data_% table should contain a single record
157073
-** with the "ota_control" column set to contain a value of type text.
157079
+** with the "rbu_control" column set to contain a value of type text.
157074157080
** The real primary key values identifying the row to update should be
157075157081
** stored in the corresponding columns of the data_% table row, as should
157076157082
** the new values of all columns being update. The text value in the
157077
-** "ota_control" column must contain the same number of characters as
157083
+** "rbu_control" column must contain the same number of characters as
157078157084
** there are columns in the target database table, and must consist entirely
157079157085
** of 'x' and '.' characters (or in some special cases 'd' - see below). For
157080157086
** each column that is being updated, the corresponding character is set to
157081157087
** 'x'. For those that remain as they are, the corresponding character of the
157082
-** ota_control value should be set to '.'. For example, given the tables
157088
+** rbu_control value should be set to '.'. For example, given the tables
157083157089
** above, the update statement:
157084157090
**
157085157091
** UPDATE t1 SET c = 'usa' WHERE a = 4;
157086157092
**
157087157093
** is represented by the data_t1 row created by:
157088157094
**
157089
-** INSERT INTO data_t1(a, b, c, ota_control) VALUES(4, NULL, 'usa', '..x');
157095
+** INSERT INTO data_t1(a, b, c, rbu_control) VALUES(4, NULL, 'usa', '..x');
157090157096
**
157091
-** Instead of an 'x' character, characters of the ota_control value specified
157097
+** Instead of an 'x' character, characters of the rbu_control value specified
157092157098
** for UPDATEs may also be set to 'd'. In this case, instead of updating the
157093157099
** target table with the value stored in the corresponding data_% column, the
157094
-** user-defined SQL function "ota_delta()" is invoked and the result stored in
157095
-** the target table column. ota_delta() is invoked with two arguments - the
157100
+** user-defined SQL function "rbu_delta()" is invoked and the result stored in
157101
+** the target table column. rbu_delta() is invoked with two arguments - the
157096157102
** original value currently stored in the target table column and the
157097157103
** value specified in the data_xxx table.
157098157104
**
157099157105
** For example, this row:
157100157106
**
157101
-** INSERT INTO data_t1(a, b, c, ota_control) VALUES(4, NULL, 'usa', '..d');
157107
+** INSERT INTO data_t1(a, b, c, rbu_control) VALUES(4, NULL, 'usa', '..d');
157102157108
**
157103157109
** is similar to an UPDATE statement such as:
157104157110
**
157105
-** UPDATE t1 SET c = ota_delta(c, 'usa') WHERE a = 4;
157111
+** UPDATE t1 SET c = rbu_delta(c, 'usa') WHERE a = 4;
157106157112
**
157107157113
** If the target database table is a virtual table or a table with no PRIMARY
157108
-** KEY, the ota_control value should not include a character corresponding
157109
-** to the ota_rowid value. For example, this:
157114
+** KEY, the rbu_control value should not include a character corresponding
157115
+** to the rbu_rowid value. For example, this:
157110157116
**
157111
-** INSERT INTO data_ft1(a, b, ota_rowid, ota_control)
157117
+** INSERT INTO data_ft1(a, b, rbu_rowid, rbu_control)
157112157118
** VALUES(NULL, 'usa', 12, '.x');
157113157119
**
157114157120
** causes a result similar to:
157115157121
**
157116157122
** UPDATE ft1 SET b = 'usa' WHERE rowid = 12;
157117157123
**
157118157124
** The data_xxx tables themselves should have no PRIMARY KEY declarations.
157119
-** However, OTA is more efficient if reading the rows in from each data_xxx
157125
+** However, RBU is more efficient if reading the rows in from each data_xxx
157120157126
** table in "rowid" order is roughly the same as reading them sorted by
157121157127
** the PRIMARY KEY of the corresponding target database table. In other
157122157128
** words, rows should be sorted using the destination table PRIMARY KEY
157123157129
** fields before they are inserted into the data_xxx tables.
157124157130
**
157125157131
** USAGE
157126157132
**
157127
-** The API declared below allows an application to apply an OTA update
157133
+** The API declared below allows an application to apply an RBU update
157128157134
** stored on disk to an existing target database. Essentially, the
157129157135
** application:
157130157136
**
157131
-** 1) Opens an OTA handle using the sqlite3ota_open() function.
157137
+** 1) Opens an RBU handle using the sqlite3rbu_open() function.
157132157138
**
157133157139
** 2) Registers any required virtual table modules with the database
157134
-** handle returned by sqlite3ota_db(). Also, if required, register
157135
-** the ota_delta() implementation.
157140
+** handle returned by sqlite3rbu_db(). Also, if required, register
157141
+** the rbu_delta() implementation.
157136157142
**
157137
-** 3) Calls the sqlite3ota_step() function one or more times on
157138
-** the new handle. Each call to sqlite3ota_step() performs a single
157143
+** 3) Calls the sqlite3rbu_step() function one or more times on
157144
+** the new handle. Each call to sqlite3rbu_step() performs a single
157139157145
** b-tree operation, so thousands of calls may be required to apply
157140157146
** a complete update.
157141157147
**
157142
-** 4) Calls sqlite3ota_close() to close the OTA update handle. If
157143
-** sqlite3ota_step() has been called enough times to completely
157144
-** apply the update to the target database, then the OTA database
157145
-** is marked as fully applied. Otherwise, the state of the OTA
157146
-** update application is saved in the OTA database for later
157148
+** 4) Calls sqlite3rbu_close() to close the RBU update handle. If
157149
+** sqlite3rbu_step() has been called enough times to completely
157150
+** apply the update to the target database, then the RBU database
157151
+** is marked as fully applied. Otherwise, the state of the RBU
157152
+** update application is saved in the RBU database for later
157147157153
** resumption.
157148157154
**
157149157155
** See comments below for more detail on APIs.
157150157156
**
157151157157
** If an update is only partially applied to the target database by the
157152
-** time sqlite3ota_close() is called, various state information is saved
157153
-** within the OTA database. This allows subsequent processes to automatically
157154
-** resume the OTA update from where it left off.
157158
+** time sqlite3rbu_close() is called, various state information is saved
157159
+** within the RBU database. This allows subsequent processes to automatically
157160
+** resume the RBU update from where it left off.
157155157161
**
157156
-** To remove all OTA extension state information, returning an OTA database
157162
+** To remove all RBU extension state information, returning an RBU database
157157157163
** to its original contents, it is sufficient to drop all tables that begin
157158
-** with the prefix "ota_"
157164
+** with the prefix "rbu_"
157159157165
**
157160157166
** DATABASE LOCKING
157161157167
**
157162
-** An OTA update may not be applied to a database in WAL mode. Attempting
157168
+** An RBU update may not be applied to a database in WAL mode. Attempting
157163157169
** to do so is an error (SQLITE_ERROR).
157164157170
**
157165
-** While an OTA handle is open, a SHARED lock may be held on the target
157171
+** While an RBU handle is open, a SHARED lock may be held on the target
157166157172
** database file. This means it is possible for other clients to read the
157167157173
** database, but not to write it.
157168157174
**
157169
-** If an OTA update is started and then suspended before it is completed,
157175
+** If an RBU update is started and then suspended before it is completed,
157170157176
** then an external client writes to the database, then attempting to resume
157171
-** the suspended OTA update is also an error (SQLITE_BUSY).
157177
+** the suspended RBU update is also an error (SQLITE_BUSY).
157172157178
*/
157173157179
157174
-#ifndef _SQLITE3OTA_H
157175
-#define _SQLITE3OTA_H
157180
+#ifndef _SQLITE3RBU_H
157181
+#define _SQLITE3RBU_H
157176157182
157177157183
/* #include "sqlite3.h" ** Required for error code definitions ** */
157178157184
157179
-typedef struct sqlite3ota sqlite3ota;
157185
+typedef struct sqlite3rbu sqlite3rbu;
157180157186
157181157187
/*
157182
-** Open an OTA handle.
157188
+** Open an RBU handle.
157183157189
**
157184
-** Argument zTarget is the path to the target database. Argument zOta is
157185
-** the path to the OTA database. Each call to this function must be matched
157186
-** by a call to sqlite3ota_close(). When opening the databases, OTA passes
157190
+** Argument zTarget is the path to the target database. Argument zRbu is
157191
+** the path to the RBU database. Each call to this function must be matched
157192
+** by a call to sqlite3rbu_close(). When opening the databases, RBU passes
157187157193
** the SQLITE_CONFIG_URI flag to sqlite3_open_v2(). So if either zTarget
157188
-** or zOta begin with "file:", it will be interpreted as an SQLite
157194
+** or zRbu begin with "file:", it will be interpreted as an SQLite
157189157195
** database URI, not a regular file name.
157190157196
**
157191
-** If the zState argument is passed a NULL value, the OTA extension stores
157197
+** If the zState argument is passed a NULL value, the RBU extension stores
157192157198
** the current state of the update (how many rows have been updated, which
157193
-** indexes are yet to be updated etc.) within the OTA database itself. This
157194
-** can be convenient, as it means that the OTA application does not need to
157199
+** indexes are yet to be updated etc.) within the RBU database itself. This
157200
+** can be convenient, as it means that the RBU application does not need to
157195157201
** organize removing a separate state file after the update is concluded.
157196157202
** Or, if zState is non-NULL, it must be a path to a database file in which
157197
-** the OTA extension can store the state of the update.
157203
+** the RBU extension can store the state of the update.
157198157204
**
157199
-** When resuming an OTA update, the zState argument must be passed the same
157200
-** value as when the OTA update was started.
157205
+** When resuming an RBU update, the zState argument must be passed the same
157206
+** value as when the RBU update was started.
157201157207
**
157202
-** Once the OTA update is finished, the OTA extension does not
157208
+** Once the RBU update is finished, the RBU extension does not
157203157209
** automatically remove any zState database file, even if it created it.
157204157210
**
157205
-** By default, OTA uses the default VFS to access the files on disk. To
157211
+** By default, RBU uses the default VFS to access the files on disk. To
157206157212
** use a VFS other than the default, an SQLite "file:" URI containing a
157207157213
** "vfs=..." option may be passed as the zTarget option.
157208157214
**
157209
-** IMPORTANT NOTE FOR ZIPVFS USERS: The OTA extension works with all of
157215
+** IMPORTANT NOTE FOR ZIPVFS USERS: The RBU extension works with all of
157210157216
** SQLite's built-in VFSs, including the multiplexor VFS. However it does
157211157217
** not work out of the box with zipvfs. Refer to the comment describing
157212
-** the zipvfs_create_vfs() API below for details on using OTA with zipvfs.
157218
+** the zipvfs_create_vfs() API below for details on using RBU with zipvfs.
157213157219
*/
157214
-SQLITE_API sqlite3ota *SQLITE_STDCALL sqlite3ota_open(
157220
+SQLITE_API sqlite3rbu *SQLITE_STDCALL sqlite3rbu_open(
157215157221
const char *zTarget,
157216
- const char *zOta,
157222
+ const char *zRbu,
157217157223
const char *zState
157218157224
);
157219157225
157220157226
/*
157221
-** Internally, each OTA connection uses a separate SQLite database
157222
-** connection to access the target and ota update databases. This
157227
+** Internally, each RBU connection uses a separate SQLite database
157228
+** connection to access the target and rbu update databases. This
157223157229
** API allows the application direct access to these database handles.
157224157230
**
157225
-** The first argument passed to this function must be a valid, open, OTA
157231
+** The first argument passed to this function must be a valid, open, RBU
157226157232
** handle. The second argument should be passed zero to access the target
157227
-** database handle, or non-zero to access the ota update database handle.
157233
+** database handle, or non-zero to access the rbu update database handle.
157228157234
** Accessing the underlying database handles may be useful in the
157229157235
** following scenarios:
157230157236
**
157231157237
** * If any target tables are virtual tables, it may be necessary to
157232157238
** call sqlite3_create_module() on the target database handle to
157233157239
** register the required virtual table implementations.
157234157240
**
157235
-** * If the data_xxx tables in the OTA source database are virtual
157241
+** * If the data_xxx tables in the RBU source database are virtual
157236157242
** tables, the application may need to call sqlite3_create_module() on
157237
-** the ota update db handle to any required virtual table
157243
+** the rbu update db handle to any required virtual table
157238157244
** implementations.
157239157245
**
157240
-** * If the application uses the "ota_delta()" feature described above,
157246
+** * If the application uses the "rbu_delta()" feature described above,
157241157247
** it must use sqlite3_create_function() or similar to register the
157242
-** ota_delta() implementation with the target database handle.
157248
+** rbu_delta() implementation with the target database handle.
157243157249
**
157244
-** If an error has occurred, either while opening or stepping the OTA object,
157250
+** If an error has occurred, either while opening or stepping the RBU object,
157245157251
** this function may return NULL. The error code and message may be collected
157246
-** when sqlite3ota_close() is called.
157252
+** when sqlite3rbu_close() is called.
157247157253
*/
157248
-SQLITE_API sqlite3 *SQLITE_STDCALL sqlite3ota_db(sqlite3ota*, int bOta);
157254
+SQLITE_API sqlite3 *SQLITE_STDCALL sqlite3rbu_db(sqlite3rbu*, int bRbu);
157249157255
157250157256
/*
157251
-** Do some work towards applying the OTA update to the target db.
157257
+** Do some work towards applying the RBU update to the target db.
157252157258
**
157253157259
** Return SQLITE_DONE if the update has been completely applied, or
157254157260
** SQLITE_OK if no error occurs but there remains work to do to apply
157255
-** the OTA update. If an error does occur, some other error code is
157261
+** the RBU update. If an error does occur, some other error code is
157256157262
** returned.
157257157263
**
157258
-** Once a call to sqlite3ota_step() has returned a value other than
157259
-** SQLITE_OK, all subsequent calls on the same OTA handle are no-ops
157264
+** Once a call to sqlite3rbu_step() has returned a value other than
157265
+** SQLITE_OK, all subsequent calls on the same RBU handle are no-ops
157260157266
** that immediately return the same value.
157261157267
*/
157262
-SQLITE_API int SQLITE_STDCALL sqlite3ota_step(sqlite3ota *pOta);
157268
+SQLITE_API int SQLITE_STDCALL sqlite3rbu_step(sqlite3rbu *pRbu);
157263157269
157264157270
/*
157265
-** Close an OTA handle.
157271
+** Close an RBU handle.
157266157272
**
157267
-** If the OTA update has been completely applied, mark the OTA database
157273
+** If the RBU update has been completely applied, mark the RBU database
157268157274
** as fully applied. Otherwise, assuming no error has occurred, save the
157269
-** current state of the OTA update appliation to the OTA database.
157275
+** current state of the RBU update appliation to the RBU database.
157270157276
**
157271
-** If an error has already occurred as part of an sqlite3ota_step()
157272
-** or sqlite3ota_open() call, or if one occurs within this function, an
157277
+** If an error has already occurred as part of an sqlite3rbu_step()
157278
+** or sqlite3rbu_open() call, or if one occurs within this function, an
157273157279
** SQLite error code is returned. Additionally, *pzErrmsg may be set to
157274157280
** point to a buffer containing a utf-8 formatted English language error
157275157281
** message. It is the responsibility of the caller to eventually free any
157276157282
** such buffer using sqlite3_free().
157277157283
**
157278157284
** Otherwise, if no error occurs, this function returns SQLITE_OK if the
157279157285
** update has been partially applied, or SQLITE_DONE if it has been
157280157286
** completely applied.
157281157287
*/
157282
-SQLITE_API int SQLITE_STDCALL sqlite3ota_close(sqlite3ota *pOta, char **pzErrmsg);
157288
+SQLITE_API int SQLITE_STDCALL sqlite3rbu_close(sqlite3rbu *pRbu, char **pzErrmsg);
157283157289
157284157290
/*
157285157291
** Return the total number of key-value operations (inserts, deletes or
157286157292
** updates) that have been performed on the target database since the
157287
-** current OTA update was started.
157293
+** current RBU update was started.
157288157294
*/
157289
-SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3ota_progress(sqlite3ota *pOta);
157295
+SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3rbu_progress(sqlite3rbu *pRbu);
157290157296
157291157297
/*
157292
-** Create an OTA VFS named zName that accesses the underlying file-system
157298
+** Create an RBU VFS named zName that accesses the underlying file-system
157293157299
** via existing VFS zParent. Or, if the zParent parameter is passed NULL,
157294
-** then the new OTA VFS uses the default system VFS to access the file-system.
157300
+** then the new RBU VFS uses the default system VFS to access the file-system.
157295157301
** The new object is registered as a non-default VFS with SQLite before
157296157302
** returning.
157297157303
**
157298
-** Part of the OTA implementation uses a custom VFS object. Usually, this
157299
-** object is created and deleted automatically by OTA.
157304
+** Part of the RBU implementation uses a custom VFS object. Usually, this
157305
+** object is created and deleted automatically by RBU.
157300157306
**
157301157307
** The exception is for applications that also use zipvfs. In this case,
157302
-** the custom VFS must be explicitly created by the user before the OTA
157303
-** handle is opened. The OTA VFS should be installed so that the zipvfs
157304
-** VFS uses the OTA VFS, which in turn uses any other VFS layers in use
157308
+** the custom VFS must be explicitly created by the user before the RBU
157309
+** handle is opened. The RBU VFS should be installed so that the zipvfs
157310
+** VFS uses the RBU VFS, which in turn uses any other VFS layers in use
157305157311
** (for example multiplexor) to access the file-system. For example,
157306
-** to assemble an OTA enabled VFS stack that uses both zipvfs and
157312
+** to assemble an RBU enabled VFS stack that uses both zipvfs and
157307157313
** multiplexor (error checking omitted):
157308157314
**
157309157315
** // Create a VFS named "multiplex" (not the default).
157310157316
** sqlite3_multiplex_initialize(0, 0);
157311157317
**
157312
-** // Create an ota VFS named "ota" that uses multiplexor. If the
157313
-** // second argument were replaced with NULL, the "ota" VFS would
157318
+** // Create an rbu VFS named "rbu" that uses multiplexor. If the
157319
+** // second argument were replaced with NULL, the "rbu" VFS would
157314157320
** // access the file-system via the system default VFS, bypassing the
157315157321
** // multiplexor.
157316
-** sqlite3ota_create_vfs("ota", "multiplex");
157322
+** sqlite3rbu_create_vfs("rbu", "multiplex");
157317157323
**
157318
-** // Create a zipvfs VFS named "zipvfs" that uses ota.
157319
-** zipvfs_create_vfs_v3("zipvfs", "ota", 0, xCompressorAlgorithmDetector);
157324
+** // Create a zipvfs VFS named "zipvfs" that uses rbu.
157325
+** zipvfs_create_vfs_v3("zipvfs", "rbu", 0, xCompressorAlgorithmDetector);
157320157326
**
157321157327
** // Make zipvfs the default VFS.
157322157328
** sqlite3_vfs_register(sqlite3_vfs_find("zipvfs"), 1);
157323157329
**
157324
-** Because the default VFS created above includes a OTA functionality, it
157325
-** may be used by OTA clients. Attempting to use OTA with a zipvfs VFS stack
157326
-** that does not include the OTA layer results in an error.
157330
+** Because the default VFS created above includes a RBU functionality, it
157331
+** may be used by RBU clients. Attempting to use RBU with a zipvfs VFS stack
157332
+** that does not include the RBU layer results in an error.
157327157333
**
157328
-** The overhead of adding the "ota" VFS to the system is negligible for
157329
-** non-OTA users. There is no harm in an application accessing the
157330
-** file-system via "ota" all the time, even if it only uses OTA functionality
157334
+** The overhead of adding the "rbu" VFS to the system is negligible for
157335
+** non-RBU users. There is no harm in an application accessing the
157336
+** file-system via "rbu" all the time, even if it only uses RBU functionality
157331157337
** occasionally.
157332157338
*/
157333
-SQLITE_API int SQLITE_STDCALL sqlite3ota_create_vfs(const char *zName, const char *zParent);
157339
+SQLITE_API int SQLITE_STDCALL sqlite3rbu_create_vfs(const char *zName, const char *zParent);
157334157340
157335157341
/*
157336
-** Deregister and destroy an OTA vfs created by an earlier call to
157337
-** sqlite3ota_create_vfs().
157342
+** Deregister and destroy an RBU vfs created by an earlier call to
157343
+** sqlite3rbu_create_vfs().
157338157344
**
157339157345
** VFS objects are not reference counted. If a VFS object is destroyed
157340157346
** before all database handles that use it have been closed, the results
157341157347
** are undefined.
157342157348
*/
157343
-SQLITE_API void SQLITE_STDCALL sqlite3ota_destroy_vfs(const char *zName);
157349
+SQLITE_API void SQLITE_STDCALL sqlite3rbu_destroy_vfs(const char *zName);
157344157350
157345
-#endif /* _SQLITE3OTA_H */
157351
+#endif /* _SQLITE3RBU_H */
157346157352
157347
-/************** End of sqlite3ota.h ******************************************/
157348
-/************** Continuing where we left off in sqlite3ota.c *****************/
157353
+/************** End of sqlite3rbu.h ******************************************/
157354
+/************** Continuing where we left off in sqlite3rbu.c *****************/
157349157355
157350157356
/* Maximum number of prepared UPDATE statements held by this module */
157351
-#define SQLITE_OTA_UPDATE_CACHESIZE 16
157357
+#define SQLITE_RBU_UPDATE_CACHESIZE 16
157352157358
157353157359
/*
157354157360
** Swap two objects of type TYPE.
157355157361
*/
157356157362
#if !defined(SQLITE_AMALGAMATION)
157357157363
# define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
157358157364
#endif
157359157365
157360157366
/*
157361
-** The ota_state table is used to save the state of a partially applied
157367
+** The rbu_state table is used to save the state of a partially applied
157362157368
** update so that it can be resumed later. The table consists of integer
157363157369
** keys mapped to values as follows:
157364157370
**
157365
-** OTA_STATE_STAGE:
157371
+** RBU_STATE_STAGE:
157366157372
** May be set to integer values 1, 2, 4 or 5. As follows:
157367
-** 1: the *-ota file is currently under construction.
157368
-** 2: the *-ota file has been constructed, but not yet moved
157373
+** 1: the *-rbu file is currently under construction.
157374
+** 2: the *-rbu file has been constructed, but not yet moved
157369157375
** to the *-wal path.
157370157376
** 4: the checkpoint is underway.
157371
-** 5: the ota update has been checkpointed.
157377
+** 5: the rbu update has been checkpointed.
157372157378
**
157373
-** OTA_STATE_TBL:
157379
+** RBU_STATE_TBL:
157374157380
** Only valid if STAGE==1. The target database name of the table
157375157381
** currently being written.
157376157382
**
157377
-** OTA_STATE_IDX:
157383
+** RBU_STATE_IDX:
157378157384
** Only valid if STAGE==1. The target database name of the index
157379157385
** currently being written, or NULL if the main table is currently being
157380157386
** updated.
157381157387
**
157382
-** OTA_STATE_ROW:
157388
+** RBU_STATE_ROW:
157383157389
** Only valid if STAGE==1. Number of rows already processed for the current
157384157390
** table/index.
157385157391
**
157386
-** OTA_STATE_PROGRESS:
157387
-** Total number of sqlite3ota_step() calls made so far as part of this
157388
-** ota update.
157392
+** RBU_STATE_PROGRESS:
157393
+** Trbul number of sqlite3rbu_step() calls made so far as part of this
157394
+** rbu update.
157389157395
**
157390
-** OTA_STATE_CKPT:
157396
+** RBU_STATE_CKPT:
157391157397
** Valid if STAGE==4. The 64-bit checksum associated with the wal-index
157392157398
** header created by recovering the *-wal file. This is used to detect
157393157399
** cases when another client appends frames to the *-wal file in the
157394157400
** middle of an incremental checkpoint (an incremental checkpoint cannot
157395157401
** be continued if this happens).
157396157402
**
157397
-** OTA_STATE_COOKIE:
157403
+** RBU_STATE_COOKIE:
157398157404
** Valid if STAGE==1. The current change-counter cookie value in the
157399157405
** target db file.
157400157406
**
157401
-** OTA_STATE_OALSZ:
157407
+** RBU_STATE_OALSZ:
157402157408
** Valid if STAGE==1. The size in bytes of the *-oal file.
157403157409
*/
157404
-#define OTA_STATE_STAGE 1
157405
-#define OTA_STATE_TBL 2
157406
-#define OTA_STATE_IDX 3
157407
-#define OTA_STATE_ROW 4
157408
-#define OTA_STATE_PROGRESS 5
157409
-#define OTA_STATE_CKPT 6
157410
-#define OTA_STATE_COOKIE 7
157411
-#define OTA_STATE_OALSZ 8
157412
-
157413
-#define OTA_STAGE_OAL 1
157414
-#define OTA_STAGE_MOVE 2
157415
-#define OTA_STAGE_CAPTURE 3
157416
-#define OTA_STAGE_CKPT 4
157417
-#define OTA_STAGE_DONE 5
157418
-
157419
-
157420
-#define OTA_CREATE_STATE \
157421
- "CREATE TABLE IF NOT EXISTS %s.ota_state(k INTEGER PRIMARY KEY, v)"
157422
-
157423
-typedef struct OtaFrame OtaFrame;
157424
-typedef struct OtaObjIter OtaObjIter;
157425
-typedef struct OtaState OtaState;
157426
-typedef struct ota_vfs ota_vfs;
157427
-typedef struct ota_file ota_file;
157428
-typedef struct OtaUpdateStmt OtaUpdateStmt;
157410
+#define RBU_STATE_STAGE 1
157411
+#define RBU_STATE_TBL 2
157412
+#define RBU_STATE_IDX 3
157413
+#define RBU_STATE_ROW 4
157414
+#define RBU_STATE_PROGRESS 5
157415
+#define RBU_STATE_CKPT 6
157416
+#define RBU_STATE_COOKIE 7
157417
+#define RBU_STATE_OALSZ 8
157418
+
157419
+#define RBU_STAGE_OAL 1
157420
+#define RBU_STAGE_MOVE 2
157421
+#define RBU_STAGE_CAPTURE 3
157422
+#define RBU_STAGE_CKPT 4
157423
+#define RBU_STAGE_DONE 5
157424
+
157425
+
157426
+#define RBU_CREATE_STATE \
157427
+ "CREATE TABLE IF NOT EXISTS %s.rbu_state(k INTEGER PRIMARY KEY, v)"
157428
+
157429
+typedef struct RbuFrame RbuFrame;
157430
+typedef struct RbuObjIter RbuObjIter;
157431
+typedef struct RbuState RbuState;
157432
+typedef struct rbu_vfs rbu_vfs;
157433
+typedef struct rbu_file rbu_file;
157434
+typedef struct RbuUpdateStmt RbuUpdateStmt;
157429157435
157430157436
#if !defined(SQLITE_AMALGAMATION)
157431157437
typedef unsigned int u32;
157432157438
typedef unsigned char u8;
157433157439
typedef sqlite3_int64 i64;
@@ -157441,13 +157447,13 @@
157441157447
#define WAL_LOCK_WRITE 0
157442157448
#define WAL_LOCK_CKPT 1
157443157449
#define WAL_LOCK_READ0 3
157444157450
157445157451
/*
157446
-** A structure to store values read from the ota_state table in memory.
157452
+** A structure to store values read from the rbu_state table in memory.
157447157453
*/
157448
-struct OtaState {
157454
+struct RbuState {
157449157455
int eStage;
157450157456
char *zTbl;
157451157457
char *zIdx;
157452157458
i64 iWalCksum;
157453157459
int nRow;
@@ -157454,14 +157460,14 @@
157454157460
i64 nProgress;
157455157461
u32 iCookie;
157456157462
i64 iOalSz;
157457157463
};
157458157464
157459
-struct OtaUpdateStmt {
157465
+struct RbuUpdateStmt {
157460157466
char *zMask; /* Copy of update mask used with pUpdate */
157461157467
sqlite3_stmt *pUpdate; /* Last update statement (or NULL) */
157462
- OtaUpdateStmt *pNext;
157468
+ RbuUpdateStmt *pNext;
157463157469
};
157464157470
157465157471
/*
157466157472
** An iterator of this type is used to iterate through all objects in
157467157473
** the target database that require updating. For each such table, the
@@ -157476,131 +157482,131 @@
157476157482
** it points to an array of flags nTblCol elements in size. The flag is
157477157483
** set for each column that is either a part of the PK or a part of an
157478157484
** index. Or clear otherwise.
157479157485
**
157480157486
*/
157481
-struct OtaObjIter {
157487
+struct RbuObjIter {
157482157488
sqlite3_stmt *pTblIter; /* Iterate through tables */
157483157489
sqlite3_stmt *pIdxIter; /* Index iterator */
157484157490
int nTblCol; /* Size of azTblCol[] array */
157485157491
char **azTblCol; /* Array of unquoted target column names */
157486157492
char **azTblType; /* Array of target column types */
157487157493
int *aiSrcOrder; /* src table col -> target table col */
157488157494
u8 *abTblPk; /* Array of flags, set on target PK columns */
157489157495
u8 *abNotNull; /* Array of flags, set on NOT NULL columns */
157490157496
u8 *abIndexed; /* Array of flags, set on indexed & PK cols */
157491
- int eType; /* Table type - an OTA_PK_XXX value */
157497
+ int eType; /* Table type - an RBU_PK_XXX value */
157492157498
157493157499
/* Output variables. zTbl==0 implies EOF. */
157494157500
int bCleanup; /* True in "cleanup" state */
157495157501
const char *zTbl; /* Name of target db table */
157496157502
const char *zIdx; /* Name of target db index (or null) */
157497157503
int iTnum; /* Root page of current object */
157498157504
int iPkTnum; /* If eType==EXTERNAL, root of PK index */
157499157505
int bUnique; /* Current index is unique */
157500157506
157501
- /* Statements created by otaObjIterPrepareAll() */
157507
+ /* Statements created by rbuObjIterPrepareAll() */
157502157508
int nCol; /* Number of columns in current object */
157503157509
sqlite3_stmt *pSelect; /* Source data */
157504157510
sqlite3_stmt *pInsert; /* Statement for INSERT operations */
157505157511
sqlite3_stmt *pDelete; /* Statement for DELETE ops */
157506
- sqlite3_stmt *pTmpInsert; /* Insert into ota_tmp_$zTbl */
157512
+ sqlite3_stmt *pTmpInsert; /* Insert into rbu_tmp_$zTbl */
157507157513
157508157514
/* Last UPDATE used (for PK b-tree updates only), or NULL. */
157509
- OtaUpdateStmt *pOtaUpdate;
157515
+ RbuUpdateStmt *pRbuUpdate;
157510157516
};
157511157517
157512157518
/*
157513
-** Values for OtaObjIter.eType
157519
+** Values for RbuObjIter.eType
157514157520
**
157515157521
** 0: Table does not exist (error)
157516157522
** 1: Table has an implicit rowid.
157517157523
** 2: Table has an explicit IPK column.
157518157524
** 3: Table has an external PK index.
157519157525
** 4: Table is WITHOUT ROWID.
157520157526
** 5: Table is a virtual table.
157521157527
*/
157522
-#define OTA_PK_NOTABLE 0
157523
-#define OTA_PK_NONE 1
157524
-#define OTA_PK_IPK 2
157525
-#define OTA_PK_EXTERNAL 3
157526
-#define OTA_PK_WITHOUT_ROWID 4
157527
-#define OTA_PK_VTAB 5
157528
+#define RBU_PK_NOTABLE 0
157529
+#define RBU_PK_NONE 1
157530
+#define RBU_PK_IPK 2
157531
+#define RBU_PK_EXTERNAL 3
157532
+#define RBU_PK_WITHOUT_ROWID 4
157533
+#define RBU_PK_VTAB 5
157528157534
157529157535
157530157536
/*
157531
-** Within the OTA_STAGE_OAL stage, each call to sqlite3ota_step() performs
157537
+** Within the RBU_STAGE_OAL stage, each call to sqlite3rbu_step() performs
157532157538
** one of the following operations.
157533157539
*/
157534
-#define OTA_INSERT 1 /* Insert on a main table b-tree */
157535
-#define OTA_DELETE 2 /* Delete a row from a main table b-tree */
157536
-#define OTA_IDX_DELETE 3 /* Delete a row from an aux. index b-tree */
157537
-#define OTA_IDX_INSERT 4 /* Insert on an aux. index b-tree */
157538
-#define OTA_UPDATE 5 /* Update a row in a main table b-tree */
157540
+#define RBU_INSERT 1 /* Insert on a main table b-tree */
157541
+#define RBU_DELETE 2 /* Delete a row from a main table b-tree */
157542
+#define RBU_IDX_DELETE 3 /* Delete a row from an aux. index b-tree */
157543
+#define RBU_IDX_INSERT 4 /* Insert on an aux. index b-tree */
157544
+#define RBU_UPDATE 5 /* Update a row in a main table b-tree */
157539157545
157540157546
157541157547
/*
157542157548
** A single step of an incremental checkpoint - frame iWalFrame of the wal
157543157549
** file should be copied to page iDbPage of the database file.
157544157550
*/
157545
-struct OtaFrame {
157551
+struct RbuFrame {
157546157552
u32 iDbPage;
157547157553
u32 iWalFrame;
157548157554
};
157549157555
157550157556
/*
157551
-** OTA handle.
157557
+** RBU handle.
157552157558
*/
157553
-struct sqlite3ota {
157554
- int eStage; /* Value of OTA_STATE_STAGE field */
157559
+struct sqlite3rbu {
157560
+ int eStage; /* Value of RBU_STATE_STAGE field */
157555157561
sqlite3 *dbMain; /* target database handle */
157556
- sqlite3 *dbOta; /* ota database handle */
157562
+ sqlite3 *dbRbu; /* rbu database handle */
157557157563
char *zTarget; /* Path to target db */
157558
- char *zOta; /* Path to ota db */
157559
- char *zState; /* Path to state db (or NULL if zOta) */
157564
+ char *zRbu; /* Path to rbu db */
157565
+ char *zState; /* Path to state db (or NULL if zRbu) */
157560157566
char zStateDb[5]; /* Db name for state ("stat" or "main") */
157561
- int rc; /* Value returned by last ota_step() call */
157567
+ int rc; /* Value returned by last rbu_step() call */
157562157568
char *zErrmsg; /* Error message if rc!=SQLITE_OK */
157563157569
int nStep; /* Rows processed for current object */
157564157570
int nProgress; /* Rows processed for all objects */
157565
- OtaObjIter objiter; /* Iterator for skipping through tbl/idx */
157566
- const char *zVfsName; /* Name of automatically created ota vfs */
157567
- ota_file *pTargetFd; /* File handle open on target db */
157571
+ RbuObjIter objiter; /* Iterator for skipping through tbl/idx */
157572
+ const char *zVfsName; /* Name of automatically created rbu vfs */
157573
+ rbu_file *pTargetFd; /* File handle open on target db */
157568157574
i64 iOalSz;
157569157575
157570157576
/* The following state variables are used as part of the incremental
157571
- ** checkpoint stage (eStage==OTA_STAGE_CKPT). See comments surrounding
157572
- ** function otaSetupCheckpoint() for details. */
157577
+ ** checkpoint stage (eStage==RBU_STAGE_CKPT). See comments surrounding
157578
+ ** function rbuSetupCheckpoint() for details. */
157573157579
u32 iMaxFrame; /* Largest iWalFrame value in aFrame[] */
157574157580
u32 mLock;
157575157581
int nFrame; /* Entries in aFrame[] array */
157576157582
int nFrameAlloc; /* Allocated size of aFrame[] array */
157577
- OtaFrame *aFrame;
157583
+ RbuFrame *aFrame;
157578157584
int pgsz;
157579157585
u8 *aBuf;
157580157586
i64 iWalCksum;
157581157587
};
157582157588
157583157589
/*
157584
-** An ota VFS is implemented using an instance of this structure.
157590
+** An rbu VFS is implemented using an instance of this structure.
157585157591
*/
157586
-struct ota_vfs {
157587
- sqlite3_vfs base; /* ota VFS shim methods */
157592
+struct rbu_vfs {
157593
+ sqlite3_vfs base; /* rbu VFS shim methods */
157588157594
sqlite3_vfs *pRealVfs; /* Underlying VFS */
157589157595
sqlite3_mutex *mutex; /* Mutex to protect pMain */
157590
- ota_file *pMain; /* Linked list of main db files */
157596
+ rbu_file *pMain; /* Linked list of main db files */
157591157597
};
157592157598
157593157599
/*
157594
-** Each file opened by an ota VFS is represented by an instance of
157600
+** Each file opened by an rbu VFS is represented by an instance of
157595157601
** the following structure.
157596157602
*/
157597
-struct ota_file {
157603
+struct rbu_file {
157598157604
sqlite3_file base; /* sqlite3_file methods */
157599157605
sqlite3_file *pReal; /* Underlying file handle */
157600
- ota_vfs *pOtaVfs; /* Pointer to the ota_vfs object */
157601
- sqlite3ota *pOta; /* Pointer to ota object (ota target only) */
157606
+ rbu_vfs *pRbuVfs; /* Pointer to the rbu_vfs object */
157607
+ sqlite3rbu *pRbu; /* Pointer to rbu object (rbu target only) */
157602157608
157603157609
int openFlags; /* Flags this file was opened with */
157604157610
u32 iCookie; /* Cookie value for main db files */
157605157611
u8 iWriteVer; /* "write-version" value for main db files */
157606157612
@@ -157607,12 +157613,12 @@
157607157613
int nShm; /* Number of entries in apShm[] array */
157608157614
char **apShm; /* Array of mmap'd *-shm regions */
157609157615
char *zDel; /* Delete this when closing file */
157610157616
157611157617
const char *zWal; /* Wal filename for this main db file */
157612
- ota_file *pWalFd; /* Wal file descriptor for this main db */
157613
- ota_file *pMainNext; /* Next MAIN_DB file */
157618
+ rbu_file *pWalFd; /* Wal file descriptor for this main db */
157619
+ rbu_file *pMainNext; /* Next MAIN_DB file */
157614157620
};
157615157621
157616157622
157617157623
/*
157618157624
** Prepare the SQL statement in buffer zSql against database handle db.
@@ -157686,14 +157692,14 @@
157686157692
}
157687157693
return rc;
157688157694
}
157689157695
157690157696
/*
157691
-** Free the OtaObjIter.azTblCol[] and OtaObjIter.abTblPk[] arrays allocated
157692
-** by an earlier call to otaObjIterCacheTableInfo().
157697
+** Free the RbuObjIter.azTblCol[] and RbuObjIter.abTblPk[] arrays allocated
157698
+** by an earlier call to rbuObjIterCacheTableInfo().
157693157699
*/
157694
-static void otaObjIterFreeCols(OtaObjIter *pIter){
157700
+static void rbuObjIterFreeCols(RbuObjIter *pIter){
157695157701
int i;
157696157702
for(i=0; i<pIter->nTblCol; i++){
157697157703
sqlite3_free(pIter->azTblCol[i]);
157698157704
sqlite3_free(pIter->azTblType[i]);
157699157705
}
@@ -157709,72 +157715,72 @@
157709157715
157710157716
/*
157711157717
** Finalize all statements and free all allocations that are specific to
157712157718
** the current object (table/index pair).
157713157719
*/
157714
-static void otaObjIterClearStatements(OtaObjIter *pIter){
157715
- OtaUpdateStmt *pUp;
157720
+static void rbuObjIterClearStatements(RbuObjIter *pIter){
157721
+ RbuUpdateStmt *pUp;
157716157722
157717157723
sqlite3_finalize(pIter->pSelect);
157718157724
sqlite3_finalize(pIter->pInsert);
157719157725
sqlite3_finalize(pIter->pDelete);
157720157726
sqlite3_finalize(pIter->pTmpInsert);
157721
- pUp = pIter->pOtaUpdate;
157727
+ pUp = pIter->pRbuUpdate;
157722157728
while( pUp ){
157723
- OtaUpdateStmt *pTmp = pUp->pNext;
157729
+ RbuUpdateStmt *pTmp = pUp->pNext;
157724157730
sqlite3_finalize(pUp->pUpdate);
157725157731
sqlite3_free(pUp);
157726157732
pUp = pTmp;
157727157733
}
157728157734
157729157735
pIter->pSelect = 0;
157730157736
pIter->pInsert = 0;
157731157737
pIter->pDelete = 0;
157732
- pIter->pOtaUpdate = 0;
157738
+ pIter->pRbuUpdate = 0;
157733157739
pIter->pTmpInsert = 0;
157734157740
pIter->nCol = 0;
157735157741
}
157736157742
157737157743
/*
157738157744
** Clean up any resources allocated as part of the iterator object passed
157739157745
** as the only argument.
157740157746
*/
157741
-static void otaObjIterFinalize(OtaObjIter *pIter){
157742
- otaObjIterClearStatements(pIter);
157747
+static void rbuObjIterFinalize(RbuObjIter *pIter){
157748
+ rbuObjIterClearStatements(pIter);
157743157749
sqlite3_finalize(pIter->pTblIter);
157744157750
sqlite3_finalize(pIter->pIdxIter);
157745
- otaObjIterFreeCols(pIter);
157746
- memset(pIter, 0, sizeof(OtaObjIter));
157751
+ rbuObjIterFreeCols(pIter);
157752
+ memset(pIter, 0, sizeof(RbuObjIter));
157747157753
}
157748157754
157749157755
/*
157750157756
** Advance the iterator to the next position.
157751157757
**
157752157758
** If no error occurs, SQLITE_OK is returned and the iterator is left
157753157759
** pointing to the next entry. Otherwise, an error code and message is
157754
-** left in the OTA handle passed as the first argument. A copy of the
157760
+** left in the RBU handle passed as the first argument. A copy of the
157755157761
** error code is returned.
157756157762
*/
157757
-static int otaObjIterNext(sqlite3ota *p, OtaObjIter *pIter){
157763
+static int rbuObjIterNext(sqlite3rbu *p, RbuObjIter *pIter){
157758157764
int rc = p->rc;
157759157765
if( rc==SQLITE_OK ){
157760157766
157761157767
/* Free any SQLite statements used while processing the previous object */
157762
- otaObjIterClearStatements(pIter);
157768
+ rbuObjIterClearStatements(pIter);
157763157769
if( pIter->zIdx==0 ){
157764157770
rc = sqlite3_exec(p->dbMain,
157765
- "DROP TRIGGER IF EXISTS temp.ota_insert_tr;"
157766
- "DROP TRIGGER IF EXISTS temp.ota_update1_tr;"
157767
- "DROP TRIGGER IF EXISTS temp.ota_update2_tr;"
157768
- "DROP TRIGGER IF EXISTS temp.ota_delete_tr;"
157771
+ "DROP TRIGGER IF EXISTS temp.rbu_insert_tr;"
157772
+ "DROP TRIGGER IF EXISTS temp.rbu_update1_tr;"
157773
+ "DROP TRIGGER IF EXISTS temp.rbu_update2_tr;"
157774
+ "DROP TRIGGER IF EXISTS temp.rbu_delete_tr;"
157769157775
, 0, 0, &p->zErrmsg
157770157776
);
157771157777
}
157772157778
157773157779
if( rc==SQLITE_OK ){
157774157780
if( pIter->bCleanup ){
157775
- otaObjIterFreeCols(pIter);
157781
+ rbuObjIterFreeCols(pIter);
157776157782
pIter->bCleanup = 0;
157777157783
rc = sqlite3_step(pIter->pTblIter);
157778157784
if( rc!=SQLITE_ROW ){
157779157785
rc = resetAndCollectError(pIter->pTblIter, &p->zErrmsg);
157780157786
pIter->zTbl = 0;
@@ -157803,11 +157809,11 @@
157803157809
}
157804157810
}
157805157811
}
157806157812
157807157813
if( rc!=SQLITE_OK ){
157808
- otaObjIterFinalize(pIter);
157814
+ rbuObjIterFinalize(pIter);
157809157815
p->rc = rc;
157810157816
}
157811157817
return rc;
157812157818
}
157813157819
@@ -157814,18 +157820,18 @@
157814157820
/*
157815157821
** Initialize the iterator structure passed as the second argument.
157816157822
**
157817157823
** If no error occurs, SQLITE_OK is returned and the iterator is left
157818157824
** pointing to the first entry. Otherwise, an error code and message is
157819
-** left in the OTA handle passed as the first argument. A copy of the
157825
+** left in the RBU handle passed as the first argument. A copy of the
157820157826
** error code is returned.
157821157827
*/
157822
-static int otaObjIterFirst(sqlite3ota *p, OtaObjIter *pIter){
157828
+static int rbuObjIterFirst(sqlite3rbu *p, RbuObjIter *pIter){
157823157829
int rc;
157824
- memset(pIter, 0, sizeof(OtaObjIter));
157830
+ memset(pIter, 0, sizeof(RbuObjIter));
157825157831
157826
- rc = prepareAndCollectError(p->dbOta, &pIter->pTblIter, &p->zErrmsg,
157832
+ rc = prepareAndCollectError(p->dbRbu, &pIter->pTblIter, &p->zErrmsg,
157827157833
"SELECT substr(name, 6) FROM sqlite_master "
157828157834
"WHERE type='table' AND name LIKE 'data_%'"
157829157835
);
157830157836
157831157837
if( rc==SQLITE_OK ){
@@ -157836,23 +157842,23 @@
157836157842
);
157837157843
}
157838157844
157839157845
pIter->bCleanup = 1;
157840157846
p->rc = rc;
157841
- return otaObjIterNext(p, pIter);
157847
+ return rbuObjIterNext(p, pIter);
157842157848
}
157843157849
157844157850
/*
157845157851
** This is a wrapper around "sqlite3_mprintf(zFmt, ...)". If an OOM occurs,
157846
-** an error code is stored in the OTA handle passed as the first argument.
157852
+** an error code is stored in the RBU handle passed as the first argument.
157847157853
**
157848157854
** If an error has already occurred (p->rc is already set to something other
157849157855
** than SQLITE_OK), then this function returns NULL without modifying the
157850157856
** stored error code. In this case it still calls sqlite3_free() on any
157851157857
** printf() parameters associated with %z conversions.
157852157858
*/
157853
-static char *otaMPrintf(sqlite3ota *p, const char *zFmt, ...){
157859
+static char *rbuMPrintf(sqlite3rbu *p, const char *zFmt, ...){
157854157860
char *zSql = 0;
157855157861
va_list ap;
157856157862
va_start(ap, zFmt);
157857157863
zSql = sqlite3_vmprintf(zFmt, ap);
157858157864
if( p->rc==SQLITE_OK ){
@@ -157867,17 +157873,17 @@
157867157873
157868157874
/*
157869157875
** Argument zFmt is a sqlite3_mprintf() style format string. The trailing
157870157876
** arguments are the usual subsitution values. This function performs
157871157877
** the printf() style substitutions and executes the result as an SQL
157872
-** statement on the OTA handles database.
157878
+** statement on the RBU handles database.
157873157879
**
157874157880
** If an error occurs, an error code and error message is stored in the
157875
-** OTA handle. If an error has already occurred when this function is
157881
+** RBU handle. If an error has already occurred when this function is
157876157882
** called, it is a no-op.
157877157883
*/
157878
-static int otaMPrintfExec(sqlite3ota *p, sqlite3 *db, const char *zFmt, ...){
157884
+static int rbuMPrintfExec(sqlite3rbu *p, sqlite3 *db, const char *zFmt, ...){
157879157885
va_list ap;
157880157886
va_start(ap, zFmt);
157881157887
char *zSql = sqlite3_vmprintf(zFmt, ap);
157882157888
if( p->rc==SQLITE_OK ){
157883157889
if( zSql==0 ){
@@ -157894,16 +157900,16 @@
157894157900
/*
157895157901
** Attempt to allocate and return a pointer to a zeroed block of nByte
157896157902
** bytes.
157897157903
**
157898157904
** If an error (i.e. an OOM condition) occurs, return NULL and leave an
157899
-** error code in the ota handle passed as the first argument. Or, if an
157905
+** error code in the rbu handle passed as the first argument. Or, if an
157900157906
** error has already occurred when this function is called, return NULL
157901157907
** immediately without attempting the allocation or modifying the stored
157902157908
** error code.
157903157909
*/
157904
-static void *otaMalloc(sqlite3ota *p, int nByte){
157910
+static void *rbuMalloc(sqlite3rbu *p, int nByte){
157905157911
void *pRet = 0;
157906157912
if( p->rc==SQLITE_OK ){
157907157913
assert( nByte>0 );
157908157914
pRet = sqlite3_malloc(nByte);
157909157915
if( pRet==0 ){
@@ -157917,17 +157923,17 @@
157917157923
157918157924
157919157925
/*
157920157926
** Allocate and zero the pIter->azTblCol[] and abTblPk[] arrays so that
157921157927
** there is room for at least nCol elements. If an OOM occurs, store an
157922
-** error code in the OTA handle passed as the first argument.
157928
+** error code in the RBU handle passed as the first argument.
157923157929
*/
157924
-static void otaAllocateIterArrays(sqlite3ota *p, OtaObjIter *pIter, int nCol){
157930
+static void rbuAllocateIterArrays(sqlite3rbu *p, RbuObjIter *pIter, int nCol){
157925157931
int nByte = (2*sizeof(char*) + sizeof(int) + 3*sizeof(u8)) * nCol;
157926157932
char **azNew;
157927157933
157928
- azNew = (char**)otaMalloc(p, nByte);
157934
+ azNew = (char**)rbuMalloc(p, nByte);
157929157935
if( azNew ){
157930157936
pIter->azTblCol = azNew;
157931157937
pIter->azTblType = &azNew[nCol];
157932157938
pIter->aiSrcOrder = (int*)&pIter->azTblType[nCol];
157933157939
pIter->abTblPk = (u8*)&pIter->aiSrcOrder[nCol];
@@ -157944,11 +157950,11 @@
157944157950
**
157945157951
** If an OOM condition is encountered when attempting to allocate memory,
157946157952
** output variable (*pRc) is set to SQLITE_NOMEM before returning. Otherwise,
157947157953
** if the allocation succeeds, (*pRc) is left unchanged.
157948157954
*/
157949
-static char *otaStrndup(const char *zStr, int *pRc){
157955
+static char *rbuStrndup(const char *zStr, int *pRc){
157950157956
char *zRet = 0;
157951157957
157952157958
assert( *pRc==SQLITE_OK );
157953157959
if( zStr ){
157954157960
int nCopy = strlen(zStr) + 1;
@@ -157965,14 +157971,14 @@
157965157971
157966157972
/*
157967157973
** Finalize the statement passed as the second argument.
157968157974
**
157969157975
** If the sqlite3_finalize() call indicates that an error occurs, and the
157970
-** ota handle error code is not already set, set the error code and error
157976
+** rbu handle error code is not already set, set the error code and error
157971157977
** message accordingly.
157972157978
*/
157973
-static void otaFinalize(sqlite3ota *p, sqlite3_stmt *pStmt){
157979
+static void rbuFinalize(sqlite3rbu *p, sqlite3_stmt *pStmt){
157974157980
sqlite3 *db = sqlite3_db_handle(pStmt);
157975157981
int rc = sqlite3_finalize(pStmt);
157976157982
if( p->rc==SQLITE_OK && rc!=SQLITE_OK ){
157977157983
p->rc = rc;
157978157984
p->zErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
@@ -157983,16 +157989,16 @@
157983157989
**
157984157990
** peType is of type (int*), a pointer to an output parameter of type
157985157991
** (int). This call sets the output parameter as follows, depending
157986157992
** on the type of the table specified by parameters dbName and zTbl.
157987157993
**
157988
-** OTA_PK_NOTABLE: No such table.
157989
-** OTA_PK_NONE: Table has an implicit rowid.
157990
-** OTA_PK_IPK: Table has an explicit IPK column.
157991
-** OTA_PK_EXTERNAL: Table has an external PK index.
157992
-** OTA_PK_WITHOUT_ROWID: Table is WITHOUT ROWID.
157993
-** OTA_PK_VTAB: Table is a virtual table.
157994
+** RBU_PK_NOTABLE: No such table.
157995
+** RBU_PK_NONE: Table has an implicit rowid.
157996
+** RBU_PK_IPK: Table has an explicit IPK column.
157997
+** RBU_PK_EXTERNAL: Table has an external PK index.
157998
+** RBU_PK_WITHOUT_ROWID: Table is WITHOUT ROWID.
157999
+** RBU_PK_VTAB: Table is a virtual table.
157994158000
**
157995158001
** Argument *piPk is also of type (int*), and also points to an output
157996158002
** parameter. Unless the table has an external primary key index
157997158003
** (i.e. unless *peType is set to 3), then *piPk is set to zero. Or,
157998158004
** if the table does have an external primary key index, then *piPk
@@ -158000,28 +158006,28 @@
158000158006
** returning.
158001158007
**
158002158008
** ALGORITHM:
158003158009
**
158004158010
** if( no entry exists in sqlite_master ){
158005
-** return OTA_PK_NOTABLE
158011
+** return RBU_PK_NOTABLE
158006158012
** }else if( sql for the entry starts with "CREATE VIRTUAL" ){
158007
-** return OTA_PK_VTAB
158013
+** return RBU_PK_VTAB
158008158014
** }else if( "PRAGMA index_list()" for the table contains a "pk" index ){
158009158015
** if( the index that is the pk exists in sqlite_master ){
158010158016
** *piPK = rootpage of that index.
158011
-** return OTA_PK_EXTERNAL
158017
+** return RBU_PK_EXTERNAL
158012158018
** }else{
158013
-** return OTA_PK_WITHOUT_ROWID
158019
+** return RBU_PK_WITHOUT_ROWID
158014158020
** }
158015158021
** }else if( "PRAGMA table_info()" lists one or more "pk" columns ){
158016
-** return OTA_PK_IPK
158022
+** return RBU_PK_IPK
158017158023
** }else{
158018
-** return OTA_PK_NONE
158024
+** return RBU_PK_NONE
158019158025
** }
158020158026
*/
158021
-static void otaTableType(
158022
- sqlite3ota *p,
158027
+static void rbuTableType(
158028
+ sqlite3rbu *p,
158023158029
const char *zTab,
158024158030
int *peType,
158025158031
int *piTnum,
158026158032
int *piPk
158027158033
){
@@ -158031,11 +158037,11 @@
158031158037
** 2) SELECT count(*) FROM sqlite_master where name=%Q
158032158038
** 3) PRAGMA table_info = ?
158033158039
*/
158034158040
sqlite3_stmt *aStmt[4] = {0, 0, 0, 0};
158035158041
158036
- *peType = OTA_PK_NOTABLE;
158042
+ *peType = RBU_PK_NOTABLE;
158037158043
*piPk = 0;
158038158044
158039158045
assert( p->rc==SQLITE_OK );
158040158046
p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[0], &p->zErrmsg,
158041158047
sqlite3_mprintf(
@@ -158043,22 +158049,22 @@
158043158049
" FROM sqlite_master"
158044158050
" WHERE name=%Q", zTab
158045158051
));
158046158052
if( p->rc!=SQLITE_OK || sqlite3_step(aStmt[0])!=SQLITE_ROW ){
158047158053
/* Either an error, or no such table. */
158048
- goto otaTableType_end;
158054
+ goto rbuTableType_end;
158049158055
}
158050158056
if( sqlite3_column_int(aStmt[0], 0) ){
158051
- *peType = OTA_PK_VTAB; /* virtual table */
158052
- goto otaTableType_end;
158057
+ *peType = RBU_PK_VTAB; /* virtual table */
158058
+ goto rbuTableType_end;
158053158059
}
158054158060
*piTnum = sqlite3_column_int(aStmt[0], 1);
158055158061
158056158062
p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[1], &p->zErrmsg,
158057158063
sqlite3_mprintf("PRAGMA index_list=%Q",zTab)
158058158064
);
158059
- if( p->rc ) goto otaTableType_end;
158065
+ if( p->rc ) goto rbuTableType_end;
158060158066
while( sqlite3_step(aStmt[1])==SQLITE_ROW ){
158061158067
const u8 *zOrig = sqlite3_column_text(aStmt[1], 3);
158062158068
const u8 *zIdx = sqlite3_column_text(aStmt[1], 1);
158063158069
if( zOrig && zIdx && zOrig[0]=='p' ){
158064158070
p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[2], &p->zErrmsg,
@@ -158066,45 +158072,45 @@
158066158072
"SELECT rootpage FROM sqlite_master WHERE name = %Q", zIdx
158067158073
));
158068158074
if( p->rc==SQLITE_OK ){
158069158075
if( sqlite3_step(aStmt[2])==SQLITE_ROW ){
158070158076
*piPk = sqlite3_column_int(aStmt[2], 0);
158071
- *peType = OTA_PK_EXTERNAL;
158077
+ *peType = RBU_PK_EXTERNAL;
158072158078
}else{
158073
- *peType = OTA_PK_WITHOUT_ROWID;
158079
+ *peType = RBU_PK_WITHOUT_ROWID;
158074158080
}
158075158081
}
158076
- goto otaTableType_end;
158082
+ goto rbuTableType_end;
158077158083
}
158078158084
}
158079158085
158080158086
p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[3], &p->zErrmsg,
158081158087
sqlite3_mprintf("PRAGMA table_info=%Q",zTab)
158082158088
);
158083158089
if( p->rc==SQLITE_OK ){
158084158090
while( sqlite3_step(aStmt[3])==SQLITE_ROW ){
158085158091
if( sqlite3_column_int(aStmt[3],5)>0 ){
158086
- *peType = OTA_PK_IPK; /* explicit IPK column */
158087
- goto otaTableType_end;
158092
+ *peType = RBU_PK_IPK; /* explicit IPK column */
158093
+ goto rbuTableType_end;
158088158094
}
158089158095
}
158090
- *peType = OTA_PK_NONE;
158096
+ *peType = RBU_PK_NONE;
158091158097
}
158092158098
158093
-otaTableType_end: {
158099
+rbuTableType_end: {
158094158100
int i;
158095158101
for(i=0; i<sizeof(aStmt)/sizeof(aStmt[0]); i++){
158096
- otaFinalize(p, aStmt[i]);
158102
+ rbuFinalize(p, aStmt[i]);
158097158103
}
158098158104
}
158099158105
}
158100158106
158101158107
/*
158102
-** This is a helper function for otaObjIterCacheTableInfo(). It populates
158108
+** This is a helper function for rbuObjIterCacheTableInfo(). It populates
158103158109
** the pIter->abIndexed[] array.
158104158110
*/
158105
-static void otaObjIterCacheIndexedCols(sqlite3ota *p, OtaObjIter *pIter){
158111
+static void rbuObjIterCacheIndexedCols(sqlite3rbu *p, RbuObjIter *pIter){
158106158112
sqlite3_stmt *pList = 0;
158107158113
int bIndex = 0;
158108158114
158109158115
if( p->rc==SQLITE_OK ){
158110158116
memcpy(pIter->abIndexed, pIter->abTblPk, sizeof(u8)*pIter->nTblCol);
@@ -158122,15 +158128,15 @@
158122158128
);
158123158129
while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
158124158130
int iCid = sqlite3_column_int(pXInfo, 1);
158125158131
if( iCid>=0 ) pIter->abIndexed[iCid] = 1;
158126158132
}
158127
- otaFinalize(p, pXInfo);
158133
+ rbuFinalize(p, pXInfo);
158128158134
bIndex = 1;
158129158135
}
158130158136
158131
- otaFinalize(p, pList);
158137
+ rbuFinalize(p, pList);
158132158138
if( bIndex==0 ) pIter->abIndexed = 0;
158133158139
}
158134158140
158135158141
158136158142
/*
@@ -158138,67 +158144,67 @@
158138158144
** pIter->abTblPk[], pIter->nTblCol and pIter->bRowid variables according to
158139158145
** the table (not index) that the iterator currently points to.
158140158146
**
158141158147
** Return SQLITE_OK if successful, or an SQLite error code otherwise. If
158142158148
** an error does occur, an error code and error message are also left in
158143
-** the OTA handle.
158149
+** the RBU handle.
158144158150
*/
158145
-static int otaObjIterCacheTableInfo(sqlite3ota *p, OtaObjIter *pIter){
158151
+static int rbuObjIterCacheTableInfo(sqlite3rbu *p, RbuObjIter *pIter){
158146158152
if( pIter->azTblCol==0 ){
158147158153
sqlite3_stmt *pStmt = 0;
158148158154
int nCol = 0;
158149158155
int i; /* for() loop iterator variable */
158150
- int bOtaRowid = 0; /* If input table has column "ota_rowid" */
158156
+ int bRbuRowid = 0; /* If input table has column "rbu_rowid" */
158151158157
int iOrder = 0;
158152158158
int iTnum = 0;
158153158159
158154158160
/* Figure out the type of table this step will deal with. */
158155158161
assert( pIter->eType==0 );
158156
- otaTableType(p, pIter->zTbl, &pIter->eType, &iTnum, &pIter->iPkTnum);
158157
- if( p->rc==SQLITE_OK && pIter->eType==OTA_PK_NOTABLE ){
158162
+ rbuTableType(p, pIter->zTbl, &pIter->eType, &iTnum, &pIter->iPkTnum);
158163
+ if( p->rc==SQLITE_OK && pIter->eType==RBU_PK_NOTABLE ){
158158158164
p->rc = SQLITE_ERROR;
158159158165
p->zErrmsg = sqlite3_mprintf("no such table: %s", pIter->zTbl);
158160158166
}
158161158167
if( p->rc ) return p->rc;
158162158168
if( pIter->zIdx==0 ) pIter->iTnum = iTnum;
158163158169
158164
- assert( pIter->eType==OTA_PK_NONE || pIter->eType==OTA_PK_IPK
158165
- || pIter->eType==OTA_PK_EXTERNAL || pIter->eType==OTA_PK_WITHOUT_ROWID
158166
- || pIter->eType==OTA_PK_VTAB
158170
+ assert( pIter->eType==RBU_PK_NONE || pIter->eType==RBU_PK_IPK
158171
+ || pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_WITHOUT_ROWID
158172
+ || pIter->eType==RBU_PK_VTAB
158167158173
);
158168158174
158169158175
/* Populate the azTblCol[] and nTblCol variables based on the columns
158170158176
** of the input table. Ignore any input table columns that begin with
158171
- ** "ota_". */
158172
- p->rc = prepareFreeAndCollectError(p->dbOta, &pStmt, &p->zErrmsg,
158177
+ ** "rbu_". */
158178
+ p->rc = prepareFreeAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
158173158179
sqlite3_mprintf("SELECT * FROM 'data_%q'", pIter->zTbl)
158174158180
);
158175158181
if( p->rc==SQLITE_OK ){
158176158182
nCol = sqlite3_column_count(pStmt);
158177
- otaAllocateIterArrays(p, pIter, nCol);
158183
+ rbuAllocateIterArrays(p, pIter, nCol);
158178158184
}
158179158185
for(i=0; p->rc==SQLITE_OK && i<nCol; i++){
158180158186
const char *zName = (const char*)sqlite3_column_name(pStmt, i);
158181
- if( sqlite3_strnicmp("ota_", zName, 4) ){
158182
- char *zCopy = otaStrndup(zName, &p->rc);
158187
+ if( sqlite3_strnicmp("rbu_", zName, 4) ){
158188
+ char *zCopy = rbuStrndup(zName, &p->rc);
158183158189
pIter->aiSrcOrder[pIter->nTblCol] = pIter->nTblCol;
158184158190
pIter->azTblCol[pIter->nTblCol++] = zCopy;
158185158191
}
158186
- else if( 0==sqlite3_stricmp("ota_rowid", zName) ){
158187
- bOtaRowid = 1;
158192
+ else if( 0==sqlite3_stricmp("rbu_rowid", zName) ){
158193
+ bRbuRowid = 1;
158188158194
}
158189158195
}
158190158196
sqlite3_finalize(pStmt);
158191158197
pStmt = 0;
158192158198
158193158199
if( p->rc==SQLITE_OK
158194
- && bOtaRowid!=(pIter->eType==OTA_PK_VTAB || pIter->eType==OTA_PK_NONE)
158200
+ && bRbuRowid!=(pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE)
158195158201
){
158196158202
p->rc = SQLITE_ERROR;
158197158203
p->zErrmsg = sqlite3_mprintf(
158198
- "table data_%q %s ota_rowid column", pIter->zTbl,
158199
- (bOtaRowid ? "may not have" : "requires")
158204
+ "table data_%q %s rbu_rowid column", pIter->zTbl,
158205
+ (bRbuRowid ? "may not have" : "requires")
158200158206
);
158201158207
}
158202158208
158203158209
/* Check that all non-HIDDEN columns in the destination table are also
158204158210
** present in the input table. Populate the abTblPk[], azTblType[] and
@@ -158227,20 +158233,20 @@
158227158233
if( i!=iOrder ){
158228158234
SWAP(int, pIter->aiSrcOrder[i], pIter->aiSrcOrder[iOrder]);
158229158235
SWAP(char*, pIter->azTblCol[i], pIter->azTblCol[iOrder]);
158230158236
}
158231158237
158232
- pIter->azTblType[iOrder] = otaStrndup(zType, &p->rc);
158238
+ pIter->azTblType[iOrder] = rbuStrndup(zType, &p->rc);
158233158239
pIter->abTblPk[iOrder] = (iPk!=0);
158234158240
pIter->abNotNull[iOrder] = (u8)bNotNull || (iPk!=0);
158235158241
iOrder++;
158236158242
}
158237158243
}
158238158244
158239
- otaFinalize(p, pStmt);
158240
- otaObjIterCacheIndexedCols(p, pIter);
158241
- assert( pIter->eType!=OTA_PK_VTAB || pIter->abIndexed==0 );
158245
+ rbuFinalize(p, pStmt);
158246
+ rbuObjIterCacheIndexedCols(p, pIter);
158247
+ assert( pIter->eType!=RBU_PK_VTAB || pIter->abIndexed==0 );
158242158248
}
158243158249
158244158250
return p->rc;
158245158251
}
158246158252
@@ -158247,29 +158253,29 @@
158247158253
/*
158248158254
** This function constructs and returns a pointer to a nul-terminated
158249158255
** string containing some SQL clause or list based on one or more of the
158250158256
** column names currently stored in the pIter->azTblCol[] array.
158251158257
*/
158252
-static char *otaObjIterGetCollist(
158253
- sqlite3ota *p, /* OTA object */
158254
- OtaObjIter *pIter /* Object iterator for column names */
158258
+static char *rbuObjIterGetCollist(
158259
+ sqlite3rbu *p, /* RBU object */
158260
+ RbuObjIter *pIter /* Object iterator for column names */
158255158261
){
158256158262
char *zList = 0;
158257158263
const char *zSep = "";
158258158264
int i;
158259158265
for(i=0; i<pIter->nTblCol; i++){
158260158266
const char *z = pIter->azTblCol[i];
158261
- zList = otaMPrintf(p, "%z%s\"%w\"", zList, zSep, z);
158267
+ zList = rbuMPrintf(p, "%z%s\"%w\"", zList, zSep, z);
158262158268
zSep = ", ";
158263158269
}
158264158270
return zList;
158265158271
}
158266158272
158267158273
/*
158268158274
** This function is used to create a SELECT list (the list of SQL
158269158275
** expressions that follows a SELECT keyword) for a SELECT statement
158270
-** used to read from an data_xxx or ota_tmp_xxx table while updating the
158276
+** used to read from an data_xxx or rbu_tmp_xxx table while updating the
158271158277
** index object currently indicated by the iterator object passed as the
158272158278
** second argument. A "PRAGMA index_xinfo = <idxname>" statement is used
158273158279
** to obtain the required information.
158274158280
**
158275158281
** If the index is of the following form:
@@ -158286,17 +158292,17 @@
158286158292
**
158287158293
** pzImposterCols: ...
158288158294
** pzImposterPk: ...
158289158295
** pzWhere: ...
158290158296
*/
158291
-static char *otaObjIterGetIndexCols(
158292
- sqlite3ota *p, /* OTA object */
158293
- OtaObjIter *pIter, /* Object iterator for column names */
158297
+static char *rbuObjIterGetIndexCols(
158298
+ sqlite3rbu *p, /* RBU object */
158299
+ RbuObjIter *pIter, /* Object iterator for column names */
158294158300
char **pzImposterCols, /* OUT: Columns for imposter table */
158295158301
char **pzImposterPk, /* OUT: Imposter PK clause */
158296158302
char **pzWhere, /* OUT: WHERE clause */
158297
- int *pnBind /* OUT: Total number of columns */
158303
+ int *pnBind /* OUT: Trbul number of columns */
158298158304
){
158299158305
int rc = p->rc; /* Error code */
158300158306
int rc2; /* sqlite3_finalize() return code */
158301158307
char *zRet = 0; /* String to return */
158302158308
char *zImpCols = 0; /* String to return via *pzImposterCols */
@@ -158321,18 +158327,18 @@
158321158327
const char *zCol;
158322158328
const char *zType;
158323158329
158324158330
if( iCid<0 ){
158325158331
/* An integer primary key. If the table has an explicit IPK, use
158326
- ** its name. Otherwise, use "ota_rowid". */
158327
- if( pIter->eType==OTA_PK_IPK ){
158332
+ ** its name. Otherwise, use "rbu_rowid". */
158333
+ if( pIter->eType==RBU_PK_IPK ){
158328158334
int i;
158329158335
for(i=0; pIter->abTblPk[i]==0; i++);
158330158336
assert( i<pIter->nTblCol );
158331158337
zCol = pIter->azTblCol[i];
158332158338
}else{
158333
- zCol = "ota_rowid";
158339
+ zCol = "rbu_rowid";
158334158340
}
158335158341
zType = "INTEGER";
158336158342
}else{
158337158343
zCol = pIter->azTblCol[iCid];
158338158344
zType = pIter->azTblType[iCid];
@@ -158339,19 +158345,19 @@
158339158345
}
158340158346
158341158347
zRet = sqlite3_mprintf("%z%s\"%w\" COLLATE %Q", zRet, zCom, zCol, zCollate);
158342158348
if( pIter->bUnique==0 || sqlite3_column_int(pXInfo, 5) ){
158343158349
const char *zOrder = (bDesc ? " DESC" : "");
158344
- zImpPK = sqlite3_mprintf("%z%s\"ota_imp_%d%w\"%s",
158350
+ zImpPK = sqlite3_mprintf("%z%s\"rbu_imp_%d%w\"%s",
158345158351
zImpPK, zCom, nBind, zCol, zOrder
158346158352
);
158347158353
}
158348
- zImpCols = sqlite3_mprintf("%z%s\"ota_imp_%d%w\" %s COLLATE %Q",
158354
+ zImpCols = sqlite3_mprintf("%z%s\"rbu_imp_%d%w\" %s COLLATE %Q",
158349158355
zImpCols, zCom, nBind, zCol, zType, zCollate
158350158356
);
158351158357
zWhere = sqlite3_mprintf(
158352
- "%z%s\"ota_imp_%d%w\" IS ?", zWhere, zAnd, nBind, zCol
158358
+ "%z%s\"rbu_imp_%d%w\" IS ?", zWhere, zAnd, nBind, zCol
158353158359
);
158354158360
if( zRet==0 || zImpPK==0 || zImpCols==0 || zWhere==0 ) rc = SQLITE_NOMEM;
158355158361
zCom = ", ";
158356158362
zAnd = " AND ";
158357158363
nBind++;
@@ -158385,16 +158391,16 @@
158385158391
**
158386158392
** "old.a, old.b, old.b"
158387158393
**
158388158394
** With the column names escaped.
158389158395
**
158390
-** For tables with implicit rowids - OTA_PK_EXTERNAL and OTA_PK_NONE, append
158396
+** For tables with implicit rowids - RBU_PK_EXTERNAL and RBU_PK_NONE, append
158391158397
** the text ", old._rowid_" to the returned value.
158392158398
*/
158393
-static char *otaObjIterGetOldlist(
158394
- sqlite3ota *p,
158395
- OtaObjIter *pIter,
158399
+static char *rbuObjIterGetOldlist(
158400
+ sqlite3rbu *p,
158401
+ RbuObjIter *pIter,
158396158402
const char *zObj
158397158403
){
158398158404
char *zList = 0;
158399158405
if( p->rc==SQLITE_OK && pIter->abIndexed ){
158400158406
const char *zS = "";
@@ -158412,12 +158418,12 @@
158412158418
break;
158413158419
}
158414158420
}
158415158421
158416158422
/* For a table with implicit rowids, append "old._rowid_" to the list. */
158417
- if( pIter->eType==OTA_PK_EXTERNAL || pIter->eType==OTA_PK_NONE ){
158418
- zList = otaMPrintf(p, "%z, %s._rowid_", zList, zObj);
158423
+ if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
158424
+ zList = rbuMPrintf(p, "%z, %s._rowid_", zList, zObj);
158419158425
}
158420158426
}
158421158427
return zList;
158422158428
}
158423158429
@@ -158429,37 +158435,37 @@
158429158435
**
158430158436
** Return the string:
158431158437
**
158432158438
** "b = ?1 AND c = ?2"
158433158439
*/
158434
-static char *otaObjIterGetWhere(
158435
- sqlite3ota *p,
158436
- OtaObjIter *pIter
158440
+static char *rbuObjIterGetWhere(
158441
+ sqlite3rbu *p,
158442
+ RbuObjIter *pIter
158437158443
){
158438158444
char *zList = 0;
158439
- if( pIter->eType==OTA_PK_VTAB || pIter->eType==OTA_PK_NONE ){
158440
- zList = otaMPrintf(p, "_rowid_ = ?%d", pIter->nTblCol+1);
158441
- }else if( pIter->eType==OTA_PK_EXTERNAL ){
158445
+ if( pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE ){
158446
+ zList = rbuMPrintf(p, "_rowid_ = ?%d", pIter->nTblCol+1);
158447
+ }else if( pIter->eType==RBU_PK_EXTERNAL ){
158442158448
const char *zSep = "";
158443158449
int i;
158444158450
for(i=0; i<pIter->nTblCol; i++){
158445158451
if( pIter->abTblPk[i] ){
158446
- zList = otaMPrintf(p, "%z%sc%d=?%d", zList, zSep, i, i+1);
158452
+ zList = rbuMPrintf(p, "%z%sc%d=?%d", zList, zSep, i, i+1);
158447158453
zSep = " AND ";
158448158454
}
158449158455
}
158450
- zList = otaMPrintf(p,
158451
- "_rowid_ = (SELECT id FROM ota_imposter2 WHERE %z)", zList
158456
+ zList = rbuMPrintf(p,
158457
+ "_rowid_ = (SELECT id FROM rbu_imposter2 WHERE %z)", zList
158452158458
);
158453158459
158454158460
}else{
158455158461
const char *zSep = "";
158456158462
int i;
158457158463
for(i=0; i<pIter->nTblCol; i++){
158458158464
if( pIter->abTblPk[i] ){
158459158465
const char *zCol = pIter->azTblCol[i];
158460
- zList = otaMPrintf(p, "%z%s\"%w\"=?%d", zList, zSep, zCol, i+1);
158466
+ zList = rbuMPrintf(p, "%z%s\"%w\"=?%d", zList, zSep, zCol, i+1);
158461158467
zSep = " AND ";
158462158468
}
158463158469
}
158464158470
}
158465158471
return zList;
@@ -158466,60 +158472,60 @@
158466158472
}
158467158473
158468158474
/*
158469158475
** The SELECT statement iterating through the keys for the current object
158470158476
** (p->objiter.pSelect) currently points to a valid row. However, there
158471
-** is something wrong with the ota_control value in the ota_control value
158477
+** is something wrong with the rbu_control value in the rbu_control value
158472158478
** stored in the (p->nCol+1)'th column. Set the error code and error message
158473
-** of the OTA handle to something reflecting this.
158479
+** of the RBU handle to something reflecting this.
158474158480
*/
158475
-static void otaBadControlError(sqlite3ota *p){
158481
+static void rbuBadControlError(sqlite3rbu *p){
158476158482
p->rc = SQLITE_ERROR;
158477
- p->zErrmsg = sqlite3_mprintf("invalid ota_control value");
158483
+ p->zErrmsg = sqlite3_mprintf("invalid rbu_control value");
158478158484
}
158479158485
158480158486
158481158487
/*
158482158488
** Return a nul-terminated string containing the comma separated list of
158483158489
** assignments that should be included following the "SET" keyword of
158484158490
** an UPDATE statement used to update the table object that the iterator
158485
-** passed as the second argument currently points to if the ota_control
158491
+** passed as the second argument currently points to if the rbu_control
158486158492
** column of the data_xxx table entry is set to zMask.
158487158493
**
158488158494
** The memory for the returned string is obtained from sqlite3_malloc().
158489158495
** It is the responsibility of the caller to eventually free it using
158490158496
** sqlite3_free().
158491158497
**
158492158498
** If an OOM error is encountered when allocating space for the new
158493
-** string, an error code is left in the ota handle passed as the first
158499
+** string, an error code is left in the rbu handle passed as the first
158494158500
** argument and NULL is returned. Or, if an error has already occurred
158495158501
** when this function is called, NULL is returned immediately, without
158496158502
** attempting the allocation or modifying the stored error code.
158497158503
*/
158498
-static char *otaObjIterGetSetlist(
158499
- sqlite3ota *p,
158500
- OtaObjIter *pIter,
158504
+static char *rbuObjIterGetSetlist(
158505
+ sqlite3rbu *p,
158506
+ RbuObjIter *pIter,
158501158507
const char *zMask
158502158508
){
158503158509
char *zList = 0;
158504158510
if( p->rc==SQLITE_OK ){
158505158511
int i;
158506158512
158507158513
if( strlen(zMask)!=pIter->nTblCol ){
158508
- otaBadControlError(p);
158514
+ rbuBadControlError(p);
158509158515
}else{
158510158516
const char *zSep = "";
158511158517
for(i=0; i<pIter->nTblCol; i++){
158512158518
char c = zMask[pIter->aiSrcOrder[i]];
158513158519
if( c=='x' ){
158514
- zList = otaMPrintf(p, "%z%s\"%w\"=?%d",
158520
+ zList = rbuMPrintf(p, "%z%s\"%w\"=?%d",
158515158521
zList, zSep, pIter->azTblCol[i], i+1
158516158522
);
158517158523
zSep = ", ";
158518158524
}
158519158525
if( c=='d' ){
158520
- zList = otaMPrintf(p, "%z%s\"%w\"=ota_delta(\"%w\", ?%d)",
158526
+ zList = rbuMPrintf(p, "%z%s\"%w\"=rbu_delta(\"%w\", ?%d)",
158521158527
zList, zSep, pIter->azTblCol[i], pIter->azTblCol[i], i+1
158522158528
);
158523158529
zSep = ", ";
158524158530
}
158525158531
}
@@ -158536,20 +158542,20 @@
158536158542
** The memory for the returned string is obtained from sqlite3_malloc().
158537158543
** It is the responsibility of the caller to eventually free it using
158538158544
** sqlite3_free().
158539158545
**
158540158546
** If an OOM error is encountered when allocating space for the new
158541
-** string, an error code is left in the ota handle passed as the first
158547
+** string, an error code is left in the rbu handle passed as the first
158542158548
** argument and NULL is returned. Or, if an error has already occurred
158543158549
** when this function is called, NULL is returned immediately, without
158544158550
** attempting the allocation or modifying the stored error code.
158545158551
*/
158546
-static char *otaObjIterGetBindlist(sqlite3ota *p, int nBind){
158552
+static char *rbuObjIterGetBindlist(sqlite3rbu *p, int nBind){
158547158553
char *zRet = 0;
158548158554
int nByte = nBind*2 + 1;
158549158555
158550
- zRet = (char*)otaMalloc(p, nByte);
158556
+ zRet = (char*)rbuMalloc(p, nByte);
158551158557
if( zRet ){
158552158558
int i;
158553158559
for(i=0; i<nBind; i++){
158554158560
zRet[i*2] = '?';
158555158561
zRet[i*2+1] = (i+1==nBind) ? '\0' : ',';
@@ -158558,21 +158564,21 @@
158558158564
return zRet;
158559158565
}
158560158566
158561158567
/*
158562158568
** The iterator currently points to a table (not index) of type
158563
-** OTA_PK_WITHOUT_ROWID. This function creates the PRIMARY KEY
158569
+** RBU_PK_WITHOUT_ROWID. This function creates the PRIMARY KEY
158564158570
** declaration for the corresponding imposter table. For example,
158565158571
** if the iterator points to a table created as:
158566158572
**
158567158573
** CREATE TABLE t1(a, b, c, PRIMARY KEY(b, a DESC)) WITHOUT ROWID
158568158574
**
158569158575
** this function returns:
158570158576
**
158571158577
** PRIMARY KEY("b", "a" DESC)
158572158578
*/
158573
-static char *otaWithoutRowidPK(sqlite3ota *p, OtaObjIter *pIter){
158579
+static char *rbuWithoutRowidPK(sqlite3rbu *p, RbuObjIter *pIter){
158574158580
char *z = 0;
158575158581
assert( pIter->zIdx==0 );
158576158582
if( p->rc==SQLITE_OK ){
158577158583
const char *zSep = "PRIMARY KEY(";
158578158584
sqlite3_stmt *pXList = 0; /* PRAGMA index_list = (pIter->zTbl) */
@@ -158591,23 +158597,23 @@
158591158597
);
158592158598
}
158593158599
break;
158594158600
}
158595158601
}
158596
- otaFinalize(p, pXList);
158602
+ rbuFinalize(p, pXList);
158597158603
158598158604
while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
158599158605
if( sqlite3_column_int(pXInfo, 5) ){
158600158606
/* int iCid = sqlite3_column_int(pXInfo, 0); */
158601158607
const char *zCol = (const char*)sqlite3_column_text(pXInfo, 2);
158602158608
const char *zDesc = sqlite3_column_int(pXInfo, 3) ? " DESC" : "";
158603
- z = otaMPrintf(p, "%z%s\"%w\"%s", z, zSep, zCol, zDesc);
158609
+ z = rbuMPrintf(p, "%z%s\"%w\"%s", z, zSep, zCol, zDesc);
158604158610
zSep = ", ";
158605158611
}
158606158612
}
158607
- z = otaMPrintf(p, "%z)", z);
158608
- otaFinalize(p, pXInfo);
158613
+ z = rbuMPrintf(p, "%z)", z);
158614
+ rbuFinalize(p, pXInfo);
158609158615
}
158610158616
return z;
158611158617
}
158612158618
158613158619
/*
@@ -158616,23 +158622,23 @@
158616158622
** iterator passed as the second argument does not currently point to
158617158623
** a table (not index) with an external primary key, this function is a
158618158624
** no-op.
158619158625
**
158620158626
** Assuming the iterator does point to a table with an external PK, this
158621
-** function creates a WITHOUT ROWID imposter table named "ota_imposter2"
158627
+** function creates a WITHOUT ROWID imposter table named "rbu_imposter2"
158622158628
** used to access that PK index. For example, if the target table is
158623158629
** declared as follows:
158624158630
**
158625158631
** CREATE TABLE t1(a, b TEXT, c REAL, PRIMARY KEY(b, c));
158626158632
**
158627158633
** then the imposter table schema is:
158628158634
**
158629
-** CREATE TABLE ota_imposter2(c1 TEXT, c2 REAL, id INTEGER) WITHOUT ROWID;
158635
+** CREATE TABLE rbu_imposter2(c1 TEXT, c2 REAL, id INTEGER) WITHOUT ROWID;
158630158636
**
158631158637
*/
158632
-static void otaCreateImposterTable2(sqlite3ota *p, OtaObjIter *pIter){
158633
- if( p->rc==SQLITE_OK && pIter->eType==OTA_PK_EXTERNAL ){
158638
+static void rbuCreateImposterTable2(sqlite3rbu *p, RbuObjIter *pIter){
158639
+ if( p->rc==SQLITE_OK && pIter->eType==RBU_PK_EXTERNAL ){
158634158640
int tnum = pIter->iPkTnum; /* Root page of PK index */
158635158641
sqlite3_stmt *pQuery = 0; /* SELECT name ... WHERE rootpage = $tnum */
158636158642
const char *zIdx = 0; /* Name of PK index */
158637158643
sqlite3_stmt *pXInfo = 0; /* PRAGMA main.index_xinfo = $zIdx */
158638158644
const char *zComma = "";
@@ -158654,31 +158660,31 @@
158654158660
if( zIdx ){
158655158661
p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
158656158662
sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
158657158663
);
158658158664
}
158659
- otaFinalize(p, pQuery);
158665
+ rbuFinalize(p, pQuery);
158660158666
158661158667
while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
158662158668
int bKey = sqlite3_column_int(pXInfo, 5);
158663158669
if( bKey ){
158664158670
int iCid = sqlite3_column_int(pXInfo, 1);
158665158671
int bDesc = sqlite3_column_int(pXInfo, 3);
158666158672
const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
158667
- zCols = otaMPrintf(p, "%z%sc%d %s COLLATE %s", zCols, zComma,
158673
+ zCols = rbuMPrintf(p, "%z%sc%d %s COLLATE %s", zCols, zComma,
158668158674
iCid, pIter->azTblType[iCid], zCollate
158669158675
);
158670
- zPk = otaMPrintf(p, "%z%sc%d%s", zPk, zComma, iCid, bDesc?" DESC":"");
158676
+ zPk = rbuMPrintf(p, "%z%sc%d%s", zPk, zComma, iCid, bDesc?" DESC":"");
158671158677
zComma = ", ";
158672158678
}
158673158679
}
158674
- zCols = otaMPrintf(p, "%z, id INTEGER", zCols);
158675
- otaFinalize(p, pXInfo);
158680
+ zCols = rbuMPrintf(p, "%z, id INTEGER", zCols);
158681
+ rbuFinalize(p, pXInfo);
158676158682
158677158683
sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1, tnum);
158678
- otaMPrintfExec(p, p->dbMain,
158679
- "CREATE TABLE ota_imposter2(%z, PRIMARY KEY(%z)) WITHOUT ROWID",
158684
+ rbuMPrintfExec(p, p->dbMain,
158685
+ "CREATE TABLE rbu_imposter2(%z, PRIMARY KEY(%z)) WITHOUT ROWID",
158680158686
zCols, zPk
158681158687
);
158682158688
sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
158683158689
}
158684158690
}
@@ -158685,28 +158691,28 @@
158685158691
158686158692
/*
158687158693
** If an error has already occurred when this function is called, it
158688158694
** immediately returns zero (without doing any work). Or, if an error
158689158695
** occurs during the execution of this function, it sets the error code
158690
-** in the sqlite3ota object indicated by the first argument and returns
158696
+** in the sqlite3rbu object indicated by the first argument and returns
158691158697
** zero.
158692158698
**
158693158699
** The iterator passed as the second argument is guaranteed to point to
158694158700
** a table (not an index) when this function is called. This function
158695158701
** attempts to create any imposter table required to write to the main
158696158702
** table b-tree of the table before returning. Non-zero is returned if
158697158703
** an imposter table are created, or zero otherwise.
158698158704
**
158699
-** An imposter table is required in all cases except OTA_PK_VTAB. Only
158705
+** An imposter table is required in all cases except RBU_PK_VTAB. Only
158700158706
** virtual tables are written to directly. The imposter table has the
158701158707
** same schema as the actual target table (less any UNIQUE constraints).
158702158708
** More precisely, the "same schema" means the same columns, types,
158703158709
** collation sequences. For tables that do not have an external PRIMARY
158704158710
** KEY, it also means the same PRIMARY KEY declaration.
158705158711
*/
158706
-static void otaCreateImposterTable(sqlite3ota *p, OtaObjIter *pIter){
158707
- if( p->rc==SQLITE_OK && pIter->eType!=OTA_PK_VTAB ){
158712
+static void rbuCreateImposterTable(sqlite3rbu *p, RbuObjIter *pIter){
158713
+ if( p->rc==SQLITE_OK && pIter->eType!=RBU_PK_VTAB ){
158708158714
int tnum = pIter->iTnum;
158709158715
const char *zComma = "";
158710158716
char *zSql = 0;
158711158717
int iCol;
158712158718
sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 1);
@@ -158718,73 +158724,73 @@
158718158724
158719158725
p->rc = sqlite3_table_column_metadata(
158720158726
p->dbMain, "main", pIter->zTbl, zCol, 0, &zColl, 0, 0, 0
158721158727
);
158722158728
158723
- if( pIter->eType==OTA_PK_IPK && pIter->abTblPk[iCol] ){
158729
+ if( pIter->eType==RBU_PK_IPK && pIter->abTblPk[iCol] ){
158724158730
/* If the target table column is an "INTEGER PRIMARY KEY", add
158725158731
** "PRIMARY KEY" to the imposter table column declaration. */
158726158732
zPk = "PRIMARY KEY ";
158727158733
}
158728
- zSql = otaMPrintf(p, "%z%s\"%w\" %s %sCOLLATE %s%s",
158734
+ zSql = rbuMPrintf(p, "%z%s\"%w\" %s %sCOLLATE %s%s",
158729158735
zSql, zComma, zCol, pIter->azTblType[iCol], zPk, zColl,
158730158736
(pIter->abNotNull[iCol] ? " NOT NULL" : "")
158731158737
);
158732158738
zComma = ", ";
158733158739
}
158734158740
158735
- if( pIter->eType==OTA_PK_WITHOUT_ROWID ){
158736
- char *zPk = otaWithoutRowidPK(p, pIter);
158741
+ if( pIter->eType==RBU_PK_WITHOUT_ROWID ){
158742
+ char *zPk = rbuWithoutRowidPK(p, pIter);
158737158743
if( zPk ){
158738
- zSql = otaMPrintf(p, "%z, %z", zSql, zPk);
158744
+ zSql = rbuMPrintf(p, "%z, %z", zSql, zPk);
158739158745
}
158740158746
}
158741158747
158742158748
sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1, tnum);
158743
- otaMPrintfExec(p, p->dbMain, "CREATE TABLE \"ota_imp_%w\"(%z)%s",
158749
+ rbuMPrintfExec(p, p->dbMain, "CREATE TABLE \"rbu_imp_%w\"(%z)%s",
158744158750
pIter->zTbl, zSql,
158745
- (pIter->eType==OTA_PK_WITHOUT_ROWID ? " WITHOUT ROWID" : "")
158751
+ (pIter->eType==RBU_PK_WITHOUT_ROWID ? " WITHOUT ROWID" : "")
158746158752
);
158747158753
sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
158748158754
}
158749158755
}
158750158756
158751158757
/*
158752
-** Prepare a statement used to insert rows into the "ota_tmp_xxx" table.
158758
+** Prepare a statement used to insert rows into the "rbu_tmp_xxx" table.
158753158759
** Specifically a statement of the form:
158754158760
**
158755
-** INSERT INTO ota_tmp_xxx VALUES(?, ?, ? ...);
158761
+** INSERT INTO rbu_tmp_xxx VALUES(?, ?, ? ...);
158756158762
**
158757158763
** The number of bound variables is equal to the number of columns in
158758
-** the target table, plus one (for the ota_control column), plus one more
158759
-** (for the ota_rowid column) if the target table is an implicit IPK or
158764
+** the target table, plus one (for the rbu_control column), plus one more
158765
+** (for the rbu_rowid column) if the target table is an implicit IPK or
158760158766
** virtual table.
158761158767
*/
158762
-static void otaObjIterPrepareTmpInsert(
158763
- sqlite3ota *p,
158764
- OtaObjIter *pIter,
158768
+static void rbuObjIterPrepareTmpInsert(
158769
+ sqlite3rbu *p,
158770
+ RbuObjIter *pIter,
158765158771
const char *zCollist,
158766
- const char *zOtaRowid
158772
+ const char *zRbuRowid
158767158773
){
158768
- int bOtaRowid = (pIter->eType==OTA_PK_EXTERNAL || pIter->eType==OTA_PK_NONE);
158769
- char *zBind = otaObjIterGetBindlist(p, pIter->nTblCol + 1 + bOtaRowid);
158774
+ int bRbuRowid = (pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE);
158775
+ char *zBind = rbuObjIterGetBindlist(p, pIter->nTblCol + 1 + bRbuRowid);
158770158776
if( zBind ){
158771158777
assert( pIter->pTmpInsert==0 );
158772158778
p->rc = prepareFreeAndCollectError(
158773
- p->dbOta, &pIter->pTmpInsert, &p->zErrmsg, sqlite3_mprintf(
158774
- "INSERT INTO %s.'ota_tmp_%q'(ota_control,%s%s) VALUES(%z)",
158775
- p->zStateDb, pIter->zTbl, zCollist, zOtaRowid, zBind
158779
+ p->dbRbu, &pIter->pTmpInsert, &p->zErrmsg, sqlite3_mprintf(
158780
+ "INSERT INTO %s.'rbu_tmp_%q'(rbu_control,%s%s) VALUES(%z)",
158781
+ p->zStateDb, pIter->zTbl, zCollist, zRbuRowid, zBind
158776158782
));
158777158783
}
158778158784
}
158779158785
158780
-static void otaTmpInsertFunc(
158786
+static void rbuTmpInsertFunc(
158781158787
sqlite3_context *pCtx,
158782158788
int nVal,
158783158789
sqlite3_value **apVal
158784158790
){
158785
- sqlite3ota *p = sqlite3_user_data(pCtx);
158791
+ sqlite3rbu *p = sqlite3_user_data(pCtx);
158786158792
int rc = SQLITE_OK;
158787158793
int i;
158788158794
158789158795
for(i=0; rc==SQLITE_OK && i<nVal; i++){
158790158796
rc = sqlite3_bind_value(p->objiter.pTmpInsert, i+1, apVal[i]);
@@ -158802,17 +158808,17 @@
158802158808
/*
158803158809
** Ensure that the SQLite statement handles required to update the
158804158810
** target database object currently indicated by the iterator passed
158805158811
** as the second argument are available.
158806158812
*/
158807
-static int otaObjIterPrepareAll(
158808
- sqlite3ota *p,
158809
- OtaObjIter *pIter,
158813
+static int rbuObjIterPrepareAll(
158814
+ sqlite3rbu *p,
158815
+ RbuObjIter *pIter,
158810158816
int nOffset /* Add "LIMIT -1 OFFSET $nOffset" to SELECT */
158811158817
){
158812158818
assert( pIter->bCleanup==0 );
158813
- if( pIter->pSelect==0 && otaObjIterCacheTableInfo(p, pIter)==SQLITE_OK ){
158819
+ if( pIter->pSelect==0 && rbuObjIterCacheTableInfo(p, pIter)==SQLITE_OK ){
158814158820
const int tnum = pIter->iTnum;
158815158821
char *zCollist = 0; /* List of indexed columns */
158816158822
char **pz = &p->zErrmsg;
158817158823
const char *zIdx = pIter->zIdx;
158818158824
char *zLimit = 0;
@@ -158828,104 +158834,104 @@
158828158834
char *zImposterPK = 0; /* Primary key declaration for imposter */
158829158835
char *zWhere = 0; /* WHERE clause on PK columns */
158830158836
char *zBind = 0;
158831158837
int nBind = 0;
158832158838
158833
- assert( pIter->eType!=OTA_PK_VTAB );
158834
- zCollist = otaObjIterGetIndexCols(
158839
+ assert( pIter->eType!=RBU_PK_VTAB );
158840
+ zCollist = rbuObjIterGetIndexCols(
158835158841
p, pIter, &zImposterCols, &zImposterPK, &zWhere, &nBind
158836158842
);
158837
- zBind = otaObjIterGetBindlist(p, nBind);
158843
+ zBind = rbuObjIterGetBindlist(p, nBind);
158838158844
158839158845
/* Create the imposter table used to write to this index. */
158840158846
sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 1);
158841158847
sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1,tnum);
158842
- otaMPrintfExec(p, p->dbMain,
158843
- "CREATE TABLE \"ota_imp_%w\"( %s, PRIMARY KEY( %s ) ) WITHOUT ROWID",
158848
+ rbuMPrintfExec(p, p->dbMain,
158849
+ "CREATE TABLE \"rbu_imp_%w\"( %s, PRIMARY KEY( %s ) ) WITHOUT ROWID",
158844158850
zTbl, zImposterCols, zImposterPK
158845158851
);
158846158852
sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
158847158853
158848158854
/* Create the statement to insert index entries */
158849158855
pIter->nCol = nBind;
158850158856
if( p->rc==SQLITE_OK ){
158851158857
p->rc = prepareFreeAndCollectError(
158852158858
p->dbMain, &pIter->pInsert, &p->zErrmsg,
158853
- sqlite3_mprintf("INSERT INTO \"ota_imp_%w\" VALUES(%s)", zTbl, zBind)
158859
+ sqlite3_mprintf("INSERT INTO \"rbu_imp_%w\" VALUES(%s)", zTbl, zBind)
158854158860
);
158855158861
}
158856158862
158857158863
/* And to delete index entries */
158858158864
if( p->rc==SQLITE_OK ){
158859158865
p->rc = prepareFreeAndCollectError(
158860158866
p->dbMain, &pIter->pDelete, &p->zErrmsg,
158861
- sqlite3_mprintf("DELETE FROM \"ota_imp_%w\" WHERE %s", zTbl, zWhere)
158867
+ sqlite3_mprintf("DELETE FROM \"rbu_imp_%w\" WHERE %s", zTbl, zWhere)
158862158868
);
158863158869
}
158864158870
158865158871
/* Create the SELECT statement to read keys in sorted order */
158866158872
if( p->rc==SQLITE_OK ){
158867158873
char *zSql;
158868
- if( pIter->eType==OTA_PK_EXTERNAL || pIter->eType==OTA_PK_NONE ){
158874
+ if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
158869158875
zSql = sqlite3_mprintf(
158870
- "SELECT %s, ota_control FROM %s.'ota_tmp_%q' ORDER BY %s%s",
158876
+ "SELECT %s, rbu_control FROM %s.'rbu_tmp_%q' ORDER BY %s%s",
158871158877
zCollist, p->zStateDb, pIter->zTbl,
158872158878
zCollist, zLimit
158873158879
);
158874158880
}else{
158875158881
zSql = sqlite3_mprintf(
158876
- "SELECT %s, ota_control FROM 'data_%q' "
158877
- "WHERE typeof(ota_control)='integer' AND ota_control!=1 "
158882
+ "SELECT %s, rbu_control FROM 'data_%q' "
158883
+ "WHERE typeof(rbu_control)='integer' AND rbu_control!=1 "
158878158884
"UNION ALL "
158879
- "SELECT %s, ota_control FROM %s.'ota_tmp_%q' "
158885
+ "SELECT %s, rbu_control FROM %s.'rbu_tmp_%q' "
158880158886
"ORDER BY %s%s",
158881158887
zCollist, pIter->zTbl,
158882158888
zCollist, p->zStateDb, pIter->zTbl,
158883158889
zCollist, zLimit
158884158890
);
158885158891
}
158886
- p->rc = prepareFreeAndCollectError(p->dbOta, &pIter->pSelect, pz, zSql);
158892
+ p->rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pSelect, pz, zSql);
158887158893
}
158888158894
158889158895
sqlite3_free(zImposterCols);
158890158896
sqlite3_free(zImposterPK);
158891158897
sqlite3_free(zWhere);
158892158898
sqlite3_free(zBind);
158893158899
}else{
158894
- int bOtaRowid = (pIter->eType==OTA_PK_VTAB || pIter->eType==OTA_PK_NONE);
158900
+ int bRbuRowid = (pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE);
158895158901
const char *zTbl = pIter->zTbl; /* Table this step applies to */
158896158902
const char *zWrite; /* Imposter table name */
158897158903
158898
- char *zBindings = otaObjIterGetBindlist(p, pIter->nTblCol + bOtaRowid);
158899
- char *zWhere = otaObjIterGetWhere(p, pIter);
158900
- char *zOldlist = otaObjIterGetOldlist(p, pIter, "old");
158901
- char *zNewlist = otaObjIterGetOldlist(p, pIter, "new");
158904
+ char *zBindings = rbuObjIterGetBindlist(p, pIter->nTblCol + bRbuRowid);
158905
+ char *zWhere = rbuObjIterGetWhere(p, pIter);
158906
+ char *zOldlist = rbuObjIterGetOldlist(p, pIter, "old");
158907
+ char *zNewlist = rbuObjIterGetOldlist(p, pIter, "new");
158902158908
158903
- zCollist = otaObjIterGetCollist(p, pIter);
158909
+ zCollist = rbuObjIterGetCollist(p, pIter);
158904158910
pIter->nCol = pIter->nTblCol;
158905158911
158906158912
/* Create the SELECT statement to read keys from data_xxx */
158907158913
if( p->rc==SQLITE_OK ){
158908
- p->rc = prepareFreeAndCollectError(p->dbOta, &pIter->pSelect, pz,
158914
+ p->rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pSelect, pz,
158909158915
sqlite3_mprintf(
158910
- "SELECT %s, ota_control%s FROM 'data_%q'%s",
158911
- zCollist, (bOtaRowid ? ", ota_rowid" : ""), zTbl, zLimit
158916
+ "SELECT %s, rbu_control%s FROM 'data_%q'%s",
158917
+ zCollist, (bRbuRowid ? ", rbu_rowid" : ""), zTbl, zLimit
158912158918
)
158913158919
);
158914158920
}
158915158921
158916158922
/* Create the imposter table or tables (if required). */
158917
- otaCreateImposterTable(p, pIter);
158918
- otaCreateImposterTable2(p, pIter);
158919
- zWrite = (pIter->eType==OTA_PK_VTAB ? "" : "ota_imp_");
158923
+ rbuCreateImposterTable(p, pIter);
158924
+ rbuCreateImposterTable2(p, pIter);
158925
+ zWrite = (pIter->eType==RBU_PK_VTAB ? "" : "rbu_imp_");
158920158926
158921158927
/* Create the INSERT statement to write to the target PK b-tree */
158922158928
if( p->rc==SQLITE_OK ){
158923158929
p->rc = prepareFreeAndCollectError(p->dbMain, &pIter->pInsert, pz,
158924158930
sqlite3_mprintf(
158925158931
"INSERT INTO \"%s%w\"(%s%s) VALUES(%s)",
158926
- zWrite, zTbl, zCollist, (bOtaRowid ? ", _rowid_" : ""), zBindings
158932
+ zWrite, zTbl, zCollist, (bRbuRowid ? ", _rowid_" : ""), zBindings
158927158933
)
158928158934
);
158929158935
}
158930158936
158931158937
/* Create the DELETE statement to write to the target PK b-tree */
@@ -158936,55 +158942,55 @@
158936158942
)
158937158943
);
158938158944
}
158939158945
158940158946
if( pIter->abIndexed ){
158941
- const char *zOtaRowid = "";
158942
- if( pIter->eType==OTA_PK_EXTERNAL || pIter->eType==OTA_PK_NONE ){
158943
- zOtaRowid = ", ota_rowid";
158947
+ const char *zRbuRowid = "";
158948
+ if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
158949
+ zRbuRowid = ", rbu_rowid";
158944158950
}
158945158951
158946
- /* Create the ota_tmp_xxx table and the triggers to populate it. */
158947
- otaMPrintfExec(p, p->dbOta,
158948
- "CREATE TABLE IF NOT EXISTS %s.'ota_tmp_%q' AS "
158952
+ /* Create the rbu_tmp_xxx table and the triggers to populate it. */
158953
+ rbuMPrintfExec(p, p->dbRbu,
158954
+ "CREATE TABLE IF NOT EXISTS %s.'rbu_tmp_%q' AS "
158949158955
"SELECT *%s FROM 'data_%q' WHERE 0;"
158950158956
, p->zStateDb
158951
- , zTbl, (pIter->eType==OTA_PK_EXTERNAL ? ", 0 AS ota_rowid" : "")
158957
+ , zTbl, (pIter->eType==RBU_PK_EXTERNAL ? ", 0 AS rbu_rowid" : "")
158952158958
, zTbl
158953158959
);
158954158960
158955
- otaMPrintfExec(p, p->dbMain,
158956
- "CREATE TEMP TRIGGER ota_delete_tr BEFORE DELETE ON \"%s%w\" "
158961
+ rbuMPrintfExec(p, p->dbMain,
158962
+ "CREATE TEMP TRIGGER rbu_delete_tr BEFORE DELETE ON \"%s%w\" "
158957158963
"BEGIN "
158958
- " SELECT ota_tmp_insert(2, %s);"
158964
+ " SELECT rbu_tmp_insert(2, %s);"
158959158965
"END;"
158960158966
158961
- "CREATE TEMP TRIGGER ota_update1_tr BEFORE UPDATE ON \"%s%w\" "
158967
+ "CREATE TEMP TRIGGER rbu_update1_tr BEFORE UPDATE ON \"%s%w\" "
158962158968
"BEGIN "
158963
- " SELECT ota_tmp_insert(2, %s);"
158969
+ " SELECT rbu_tmp_insert(2, %s);"
158964158970
"END;"
158965158971
158966
- "CREATE TEMP TRIGGER ota_update2_tr AFTER UPDATE ON \"%s%w\" "
158972
+ "CREATE TEMP TRIGGER rbu_update2_tr AFTER UPDATE ON \"%s%w\" "
158967158973
"BEGIN "
158968
- " SELECT ota_tmp_insert(3, %s);"
158974
+ " SELECT rbu_tmp_insert(3, %s);"
158969158975
"END;",
158970158976
zWrite, zTbl, zOldlist,
158971158977
zWrite, zTbl, zOldlist,
158972158978
zWrite, zTbl, zNewlist
158973158979
);
158974158980
158975
- if( pIter->eType==OTA_PK_EXTERNAL || pIter->eType==OTA_PK_NONE ){
158976
- otaMPrintfExec(p, p->dbMain,
158977
- "CREATE TEMP TRIGGER ota_insert_tr AFTER INSERT ON \"%s%w\" "
158981
+ if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
158982
+ rbuMPrintfExec(p, p->dbMain,
158983
+ "CREATE TEMP TRIGGER rbu_insert_tr AFTER INSERT ON \"%s%w\" "
158978158984
"BEGIN "
158979
- " SELECT ota_tmp_insert(0, %s);"
158985
+ " SELECT rbu_tmp_insert(0, %s);"
158980158986
"END;",
158981158987
zWrite, zTbl, zNewlist
158982158988
);
158983158989
}
158984158990
158985
- otaObjIterPrepareTmpInsert(p, pIter, zCollist, zOtaRowid);
158991
+ rbuObjIterPrepareTmpInsert(p, pIter, zCollist, zRbuRowid);
158986158992
}
158987158993
158988158994
sqlite3_free(zWhere);
158989158995
sqlite3_free(zOldlist);
158990158996
sqlite3_free(zNewlist);
@@ -158999,68 +159005,68 @@
158999159005
159000159006
/*
159001159007
** Set output variable *ppStmt to point to an UPDATE statement that may
159002159008
** be used to update the imposter table for the main table b-tree of the
159003159009
** table object that pIter currently points to, assuming that the
159004
-** ota_control column of the data_xyz table contains zMask.
159010
+** rbu_control column of the data_xyz table contains zMask.
159005159011
**
159006159012
** If the zMask string does not specify any columns to update, then this
159007159013
** is not an error. Output variable *ppStmt is set to NULL in this case.
159008159014
*/
159009
-static int otaGetUpdateStmt(
159010
- sqlite3ota *p, /* OTA handle */
159011
- OtaObjIter *pIter, /* Object iterator */
159012
- const char *zMask, /* ota_control value ('x.x.') */
159015
+static int rbuGetUpdateStmt(
159016
+ sqlite3rbu *p, /* RBU handle */
159017
+ RbuObjIter *pIter, /* Object iterator */
159018
+ const char *zMask, /* rbu_control value ('x.x.') */
159013159019
sqlite3_stmt **ppStmt /* OUT: UPDATE statement handle */
159014159020
){
159015
- OtaUpdateStmt **pp;
159016
- OtaUpdateStmt *pUp = 0;
159021
+ RbuUpdateStmt **pp;
159022
+ RbuUpdateStmt *pUp = 0;
159017159023
int nUp = 0;
159018159024
159019159025
/* In case an error occurs */
159020159026
*ppStmt = 0;
159021159027
159022159028
/* Search for an existing statement. If one is found, shift it to the front
159023159029
** of the LRU queue and return immediately. Otherwise, leave nUp pointing
159024159030
** to the number of statements currently in the cache and pUp to the
159025159031
** last object in the list. */
159026
- for(pp=&pIter->pOtaUpdate; *pp; pp=&((*pp)->pNext)){
159032
+ for(pp=&pIter->pRbuUpdate; *pp; pp=&((*pp)->pNext)){
159027159033
pUp = *pp;
159028159034
if( strcmp(pUp->zMask, zMask)==0 ){
159029159035
*pp = pUp->pNext;
159030
- pUp->pNext = pIter->pOtaUpdate;
159031
- pIter->pOtaUpdate = pUp;
159036
+ pUp->pNext = pIter->pRbuUpdate;
159037
+ pIter->pRbuUpdate = pUp;
159032159038
*ppStmt = pUp->pUpdate;
159033159039
return SQLITE_OK;
159034159040
}
159035159041
nUp++;
159036159042
}
159037159043
assert( pUp==0 || pUp->pNext==0 );
159038159044
159039
- if( nUp>=SQLITE_OTA_UPDATE_CACHESIZE ){
159040
- for(pp=&pIter->pOtaUpdate; *pp!=pUp; pp=&((*pp)->pNext));
159045
+ if( nUp>=SQLITE_RBU_UPDATE_CACHESIZE ){
159046
+ for(pp=&pIter->pRbuUpdate; *pp!=pUp; pp=&((*pp)->pNext));
159041159047
*pp = 0;
159042159048
sqlite3_finalize(pUp->pUpdate);
159043159049
pUp->pUpdate = 0;
159044159050
}else{
159045
- pUp = (OtaUpdateStmt*)otaMalloc(p, sizeof(OtaUpdateStmt)+pIter->nTblCol+1);
159051
+ pUp = (RbuUpdateStmt*)rbuMalloc(p, sizeof(RbuUpdateStmt)+pIter->nTblCol+1);
159046159052
}
159047159053
159048159054
if( pUp ){
159049
- char *zWhere = otaObjIterGetWhere(p, pIter);
159050
- char *zSet = otaObjIterGetSetlist(p, pIter, zMask);
159055
+ char *zWhere = rbuObjIterGetWhere(p, pIter);
159056
+ char *zSet = rbuObjIterGetSetlist(p, pIter, zMask);
159051159057
char *zUpdate = 0;
159052159058
159053159059
pUp->zMask = (char*)&pUp[1];
159054159060
memcpy(pUp->zMask, zMask, pIter->nTblCol);
159055
- pUp->pNext = pIter->pOtaUpdate;
159056
- pIter->pOtaUpdate = pUp;
159061
+ pUp->pNext = pIter->pRbuUpdate;
159062
+ pIter->pRbuUpdate = pUp;
159057159063
159058159064
if( zSet ){
159059159065
const char *zPrefix = "";
159060159066
159061
- if( pIter->eType!=OTA_PK_VTAB ) zPrefix = "ota_imp_";
159067
+ if( pIter->eType!=RBU_PK_VTAB ) zPrefix = "rbu_imp_";
159062159068
zUpdate = sqlite3_mprintf("UPDATE \"%s%w\" SET %s WHERE %s",
159063159069
zPrefix, pIter->zTbl, zSet, zWhere
159064159070
);
159065159071
p->rc = prepareFreeAndCollectError(
159066159072
p->dbMain, &pUp->pUpdate, &p->zErrmsg, zUpdate
@@ -159072,11 +159078,11 @@
159072159078
}
159073159079
159074159080
return p->rc;
159075159081
}
159076159082
159077
-static sqlite3 *otaOpenDbhandle(sqlite3ota *p, const char *zName){
159083
+static sqlite3 *rbuOpenDbhandle(sqlite3rbu *p, const char *zName){
159078159084
sqlite3 *db = 0;
159079159085
if( p->rc==SQLITE_OK ){
159080159086
const int flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_URI;
159081159087
p->rc = sqlite3_open_v2(zName, &db, flags, p->zVfsName);
159082159088
if( p->rc ){
@@ -159087,51 +159093,51 @@
159087159093
}
159088159094
return db;
159089159095
}
159090159096
159091159097
/*
159092
-** Open the database handle and attach the OTA database as "ota". If an
159093
-** error occurs, leave an error code and message in the OTA handle.
159098
+** Open the database handle and attach the RBU database as "rbu". If an
159099
+** error occurs, leave an error code and message in the RBU handle.
159094159100
*/
159095
-static void otaOpenDatabase(sqlite3ota *p){
159101
+static void rbuOpenDatabase(sqlite3rbu *p){
159096159102
assert( p->rc==SQLITE_OK );
159097
- assert( p->dbMain==0 && p->dbOta==0 );
159103
+ assert( p->dbMain==0 && p->dbRbu==0 );
159098159104
159099159105
p->eStage = 0;
159100
- p->dbMain = otaOpenDbhandle(p, p->zTarget);
159101
- p->dbOta = otaOpenDbhandle(p, p->zOta);
159106
+ p->dbMain = rbuOpenDbhandle(p, p->zTarget);
159107
+ p->dbRbu = rbuOpenDbhandle(p, p->zRbu);
159102159108
159103
- /* If using separate OTA and state databases, attach the state database to
159104
- ** the OTA db handle now. */
159109
+ /* If using separate RBU and state databases, attach the state database to
159110
+ ** the RBU db handle now. */
159105159111
if( p->zState ){
159106
- otaMPrintfExec(p, p->dbOta, "ATTACH %Q AS stat", p->zState);
159112
+ rbuMPrintfExec(p, p->dbRbu, "ATTACH %Q AS stat", p->zState);
159107159113
memcpy(p->zStateDb, "stat", 4);
159108159114
}else{
159109159115
memcpy(p->zStateDb, "main", 4);
159110159116
}
159111159117
159112159118
if( p->rc==SQLITE_OK ){
159113159119
p->rc = sqlite3_create_function(p->dbMain,
159114
- "ota_tmp_insert", -1, SQLITE_UTF8, (void*)p, otaTmpInsertFunc, 0, 0
159120
+ "rbu_tmp_insert", -1, SQLITE_UTF8, (void*)p, rbuTmpInsertFunc, 0, 0
159115159121
);
159116159122
}
159117159123
159118159124
if( p->rc==SQLITE_OK ){
159119
- p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_OTA, (void*)p);
159125
+ p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_RBU, (void*)p);
159120159126
}
159121
- otaMPrintfExec(p, p->dbMain, "SELECT * FROM sqlite_master");
159127
+ rbuMPrintfExec(p, p->dbMain, "SELECT * FROM sqlite_master");
159122159128
159123
- /* Mark the database file just opened as an OTA target database. If
159124
- ** this call returns SQLITE_NOTFOUND, then the OTA vfs is not in use.
159129
+ /* Mark the database file just opened as an RBU target database. If
159130
+ ** this call returns SQLITE_NOTFOUND, then the RBU vfs is not in use.
159125159131
** This is an error. */
159126159132
if( p->rc==SQLITE_OK ){
159127
- p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_OTA, (void*)p);
159133
+ p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_RBU, (void*)p);
159128159134
}
159129159135
159130159136
if( p->rc==SQLITE_NOTFOUND ){
159131159137
p->rc = SQLITE_ERROR;
159132
- p->zErrmsg = sqlite3_mprintf("ota vfs not found");
159138
+ p->zErrmsg = sqlite3_mprintf("rbu vfs not found");
159133159139
}
159134159140
}
159135159141
159136159142
/*
159137159143
** This routine is a copy of the sqlite3FileSuffix3() routine from the core.
@@ -159151,11 +159157,11 @@
159151159157
** test.db-journal => test.nal
159152159158
** test.db-wal => test.wal
159153159159
** test.db-shm => test.shm
159154159160
** test.db-mj7f3319fa => test.9fa
159155159161
*/
159156
-static void otaFileSuffix3(const char *zBase, char *z){
159162
+static void rbuFileSuffix3(const char *zBase, char *z){
159157159163
#ifdef SQLITE_ENABLE_8_3_NAMES
159158159164
#if SQLITE_ENABLE_8_3_NAMES<2
159159159165
if( sqlite3_uri_boolean(zBase, "8_3_names", 0) )
159160159166
#endif
159161159167
{
@@ -159172,11 +159178,11 @@
159172159178
** as a 64-bit integer.
159173159179
**
159174159180
** The checksum is store in the first page of xShmMap memory as an 8-byte
159175159181
** blob starting at byte offset 40.
159176159182
*/
159177
-static i64 otaShmChecksum(sqlite3ota *p){
159183
+static i64 rbuShmChecksum(sqlite3rbu *p){
159178159184
i64 iRet = 0;
159179159185
if( p->rc==SQLITE_OK ){
159180159186
sqlite3_file *pDb = p->pTargetFd->pReal;
159181159187
u32 volatile *ptr;
159182159188
p->rc = pDb->pMethods->xShmMap(pDb, 0, 32*1024, 0, (void volatile**)&ptr);
@@ -159189,23 +159195,23 @@
159189159195
159190159196
/*
159191159197
** This function is called as part of initializing or reinitializing an
159192159198
** incremental checkpoint.
159193159199
**
159194
-** It populates the sqlite3ota.aFrame[] array with the set of
159200
+** It populates the sqlite3rbu.aFrame[] array with the set of
159195159201
** (wal frame -> db page) copy operations required to checkpoint the
159196159202
** current wal file, and obtains the set of shm locks required to safely
159197159203
** perform the copy operations directly on the file-system.
159198159204
**
159199159205
** If argument pState is not NULL, then the incremental checkpoint is
159200159206
** being resumed. In this case, if the checksum of the wal-index-header
159201
-** following recovery is not the same as the checksum saved in the OtaState
159202
-** object, then the ota handle is set to DONE state. This occurs if some
159207
+** following recovery is not the same as the checksum saved in the RbuState
159208
+** object, then the rbu handle is set to DONE state. This occurs if some
159203159209
** other client appends a transaction to the wal file in the middle of
159204159210
** an incremental checkpoint.
159205159211
*/
159206
-static void otaSetupCheckpoint(sqlite3ota *p, OtaState *pState){
159212
+static void rbuSetupCheckpoint(sqlite3rbu *p, RbuState *pState){
159207159213
159208159214
/* If pState is NULL, then the wal file may not have been opened and
159209159215
** recovered. Running a read-statement here to ensure that doing so
159210159216
** does not interfere with the "capture" process below. */
159211159217
if( pState==0 ){
@@ -159214,20 +159220,20 @@
159214159220
p->rc = sqlite3_exec(p->dbMain, "SELECT * FROM sqlite_master", 0, 0, 0);
159215159221
}
159216159222
}
159217159223
159218159224
/* Assuming no error has occurred, run a "restart" checkpoint with the
159219
- ** sqlite3ota.eStage variable set to CAPTURE. This turns on the following
159220
- ** special behaviour in the ota VFS:
159225
+ ** sqlite3rbu.eStage variable set to CAPTURE. This turns on the following
159226
+ ** special behaviour in the rbu VFS:
159221159227
**
159222159228
** * If the exclusive shm WRITER or READ0 lock cannot be obtained,
159223159229
** the checkpoint fails with SQLITE_BUSY (normally SQLite would
159224159230
** proceed with running a passive checkpoint instead of failing).
159225159231
**
159226159232
** * Attempts to read from the *-wal file or write to the database file
159227159233
** do not perform any IO. Instead, the frame/page combinations that
159228
- ** would be read/written are recorded in the sqlite3ota.aFrame[]
159234
+ ** would be read/written are recorded in the sqlite3rbu.aFrame[]
159229159235
** array.
159230159236
**
159231159237
** * Calls to xShmLock(UNLOCK) to release the exclusive shm WRITER,
159232159238
** READ0 and CHECKPOINT locks taken as part of the checkpoint are
159233159239
** no-ops. These locks will not be released until the connection
@@ -159243,76 +159249,76 @@
159243159249
** data from the wal file into the database file according to the
159244159250
** contents of aFrame[].
159245159251
*/
159246159252
if( p->rc==SQLITE_OK ){
159247159253
int rc2;
159248
- p->eStage = OTA_STAGE_CAPTURE;
159254
+ p->eStage = RBU_STAGE_CAPTURE;
159249159255
rc2 = sqlite3_exec(p->dbMain, "PRAGMA main.wal_checkpoint=restart", 0, 0,0);
159250159256
if( rc2!=SQLITE_INTERNAL ) p->rc = rc2;
159251159257
}
159252159258
159253159259
if( p->rc==SQLITE_OK ){
159254
- p->eStage = OTA_STAGE_CKPT;
159260
+ p->eStage = RBU_STAGE_CKPT;
159255159261
p->nStep = (pState ? pState->nRow : 0);
159256
- p->aBuf = otaMalloc(p, p->pgsz);
159257
- p->iWalCksum = otaShmChecksum(p);
159262
+ p->aBuf = rbuMalloc(p, p->pgsz);
159263
+ p->iWalCksum = rbuShmChecksum(p);
159258159264
}
159259159265
159260159266
if( p->rc==SQLITE_OK && pState && pState->iWalCksum!=p->iWalCksum ){
159261159267
p->rc = SQLITE_DONE;
159262
- p->eStage = OTA_STAGE_DONE;
159268
+ p->eStage = RBU_STAGE_DONE;
159263159269
}
159264159270
}
159265159271
159266159272
/*
159267159273
** Called when iAmt bytes are read from offset iOff of the wal file while
159268
-** the ota object is in capture mode. Record the frame number of the frame
159274
+** the rbu object is in capture mode. Record the frame number of the frame
159269159275
** being read in the aFrame[] array.
159270159276
*/
159271
-static int otaCaptureWalRead(sqlite3ota *pOta, i64 iOff, int iAmt){
159277
+static int rbuCaptureWalRead(sqlite3rbu *pRbu, i64 iOff, int iAmt){
159272159278
const u32 mReq = (1<<WAL_LOCK_WRITE)|(1<<WAL_LOCK_CKPT)|(1<<WAL_LOCK_READ0);
159273159279
u32 iFrame;
159274159280
159275
- if( pOta->mLock!=mReq ){
159276
- pOta->rc = SQLITE_BUSY;
159281
+ if( pRbu->mLock!=mReq ){
159282
+ pRbu->rc = SQLITE_BUSY;
159277159283
return SQLITE_INTERNAL;
159278159284
}
159279159285
159280
- pOta->pgsz = iAmt;
159281
- if( pOta->nFrame==pOta->nFrameAlloc ){
159282
- int nNew = (pOta->nFrameAlloc ? pOta->nFrameAlloc : 64) * 2;
159283
- OtaFrame *aNew;
159284
- aNew = (OtaFrame*)sqlite3_realloc(pOta->aFrame, nNew * sizeof(OtaFrame));
159286
+ pRbu->pgsz = iAmt;
159287
+ if( pRbu->nFrame==pRbu->nFrameAlloc ){
159288
+ int nNew = (pRbu->nFrameAlloc ? pRbu->nFrameAlloc : 64) * 2;
159289
+ RbuFrame *aNew;
159290
+ aNew = (RbuFrame*)sqlite3_realloc(pRbu->aFrame, nNew * sizeof(RbuFrame));
159285159291
if( aNew==0 ) return SQLITE_NOMEM;
159286
- pOta->aFrame = aNew;
159287
- pOta->nFrameAlloc = nNew;
159292
+ pRbu->aFrame = aNew;
159293
+ pRbu->nFrameAlloc = nNew;
159288159294
}
159289159295
159290159296
iFrame = (u32)((iOff-32) / (i64)(iAmt+24)) + 1;
159291
- if( pOta->iMaxFrame<iFrame ) pOta->iMaxFrame = iFrame;
159292
- pOta->aFrame[pOta->nFrame].iWalFrame = iFrame;
159293
- pOta->aFrame[pOta->nFrame].iDbPage = 0;
159294
- pOta->nFrame++;
159297
+ if( pRbu->iMaxFrame<iFrame ) pRbu->iMaxFrame = iFrame;
159298
+ pRbu->aFrame[pRbu->nFrame].iWalFrame = iFrame;
159299
+ pRbu->aFrame[pRbu->nFrame].iDbPage = 0;
159300
+ pRbu->nFrame++;
159295159301
return SQLITE_OK;
159296159302
}
159297159303
159298159304
/*
159299159305
** Called when a page of data is written to offset iOff of the database
159300
-** file while the ota handle is in capture mode. Record the page number
159306
+** file while the rbu handle is in capture mode. Record the page number
159301159307
** of the page being written in the aFrame[] array.
159302159308
*/
159303
-static int otaCaptureDbWrite(sqlite3ota *pOta, i64 iOff){
159304
- pOta->aFrame[pOta->nFrame-1].iDbPage = (u32)(iOff / pOta->pgsz) + 1;
159309
+static int rbuCaptureDbWrite(sqlite3rbu *pRbu, i64 iOff){
159310
+ pRbu->aFrame[pRbu->nFrame-1].iDbPage = (u32)(iOff / pRbu->pgsz) + 1;
159305159311
return SQLITE_OK;
159306159312
}
159307159313
159308159314
/*
159309159315
** This is called as part of an incremental checkpoint operation. Copy
159310159316
** a single frame of data from the wal file into the database file, as
159311
-** indicated by the OtaFrame object.
159317
+** indicated by the RbuFrame object.
159312159318
*/
159313
-static void otaCheckpointFrame(sqlite3ota *p, OtaFrame *pFrame){
159319
+static void rbuCheckpointFrame(sqlite3rbu *p, RbuFrame *pFrame){
159314159320
sqlite3_file *pWal = p->pTargetFd->pWalFd->pReal;
159315159321
sqlite3_file *pDb = p->pTargetFd->pReal;
159316159322
i64 iOff;
159317159323
159318159324
assert( p->rc==SQLITE_OK );
@@ -159326,33 +159332,33 @@
159326159332
159327159333
159328159334
/*
159329159335
** Take an EXCLUSIVE lock on the database file.
159330159336
*/
159331
-static void otaLockDatabase(sqlite3ota *p){
159337
+static void rbuLockDatabase(sqlite3rbu *p){
159332159338
sqlite3_file *pReal = p->pTargetFd->pReal;
159333159339
assert( p->rc==SQLITE_OK );
159334159340
p->rc = pReal->pMethods->xLock(pReal, SQLITE_LOCK_SHARED);
159335159341
if( p->rc==SQLITE_OK ){
159336159342
p->rc = pReal->pMethods->xLock(pReal, SQLITE_LOCK_EXCLUSIVE);
159337159343
}
159338159344
}
159339159345
159340159346
/*
159341
-** The OTA handle is currently in OTA_STAGE_OAL state, with a SHARED lock
159347
+** The RBU handle is currently in RBU_STAGE_OAL state, with a SHARED lock
159342159348
** on the database file. This proc moves the *-oal file to the *-wal path,
159343159349
** then reopens the database file (this time in vanilla, non-oal, WAL mode).
159344
-** If an error occurs, leave an error code and error message in the ota
159350
+** If an error occurs, leave an error code and error message in the rbu
159345159351
** handle.
159346159352
*/
159347
-static void otaMoveOalFile(sqlite3ota *p){
159353
+static void rbuMoveOalFile(sqlite3rbu *p){
159348159354
const char *zBase = sqlite3_db_filename(p->dbMain, "main");
159349159355
159350159356
char *zWal = sqlite3_mprintf("%s-wal", zBase);
159351159357
char *zOal = sqlite3_mprintf("%s-oal", zBase);
159352159358
159353
- assert( p->eStage==OTA_STAGE_MOVE );
159359
+ assert( p->eStage==RBU_STAGE_MOVE );
159354159360
assert( p->rc==SQLITE_OK && p->zErrmsg==0 );
159355159361
if( zWal==0 || zOal==0 ){
159356159362
p->rc = SQLITE_NOMEM;
159357159363
}else{
159358159364
/* Move the *-oal file to *-wal. At this point connection p->db is
@@ -159360,25 +159366,25 @@
159360159366
** in WAL mode). So no other connection may be writing the db.
159361159367
**
159362159368
** In order to ensure that there are no database readers, an EXCLUSIVE
159363159369
** lock is obtained here before the *-oal is moved to *-wal.
159364159370
*/
159365
- otaLockDatabase(p);
159371
+ rbuLockDatabase(p);
159366159372
if( p->rc==SQLITE_OK ){
159367
- otaFileSuffix3(zBase, zWal);
159368
- otaFileSuffix3(zBase, zOal);
159373
+ rbuFileSuffix3(zBase, zWal);
159374
+ rbuFileSuffix3(zBase, zOal);
159369159375
159370159376
/* Re-open the databases. */
159371
- otaObjIterFinalize(&p->objiter);
159377
+ rbuObjIterFinalize(&p->objiter);
159372159378
sqlite3_close(p->dbMain);
159373
- sqlite3_close(p->dbOta);
159379
+ sqlite3_close(p->dbRbu);
159374159380
p->rc = rename(zOal, zWal) ? SQLITE_IOERR : SQLITE_OK;
159375159381
if( p->rc==SQLITE_OK ){
159376159382
p->dbMain = 0;
159377
- p->dbOta = 0;
159378
- otaOpenDatabase(p);
159379
- otaSetupCheckpoint(p, 0);
159383
+ p->dbRbu = 0;
159384
+ rbuOpenDatabase(p);
159385
+ rbuSetupCheckpoint(p, 0);
159380159386
}
159381159387
}
159382159388
}
159383159389
159384159390
sqlite3_free(zWal);
@@ -159389,36 +159395,36 @@
159389159395
** The SELECT statement iterating through the keys for the current object
159390159396
** (p->objiter.pSelect) currently points to a valid row. This function
159391159397
** determines the type of operation requested by this row and returns
159392159398
** one of the following values to indicate the result:
159393159399
**
159394
-** * OTA_INSERT
159395
-** * OTA_DELETE
159396
-** * OTA_IDX_DELETE
159397
-** * OTA_UPDATE
159400
+** * RBU_INSERT
159401
+** * RBU_DELETE
159402
+** * RBU_IDX_DELETE
159403
+** * RBU_UPDATE
159398159404
**
159399
-** If OTA_UPDATE is returned, then output variable *pzMask is set to
159405
+** If RBU_UPDATE is returned, then output variable *pzMask is set to
159400159406
** point to the text value indicating the columns to update.
159401159407
**
159402
-** If the ota_control field contains an invalid value, an error code and
159403
-** message are left in the OTA handle and zero returned.
159408
+** If the rbu_control field contains an invalid value, an error code and
159409
+** message are left in the RBU handle and zero returned.
159404159410
*/
159405
-static int otaStepType(sqlite3ota *p, const char **pzMask){
159406
- int iCol = p->objiter.nCol; /* Index of ota_control column */
159411
+static int rbuStepType(sqlite3rbu *p, const char **pzMask){
159412
+ int iCol = p->objiter.nCol; /* Index of rbu_control column */
159407159413
int res = 0; /* Return value */
159408159414
159409159415
switch( sqlite3_column_type(p->objiter.pSelect, iCol) ){
159410159416
case SQLITE_INTEGER: {
159411159417
int iVal = sqlite3_column_int(p->objiter.pSelect, iCol);
159412159418
if( iVal==0 ){
159413
- res = OTA_INSERT;
159419
+ res = RBU_INSERT;
159414159420
}else if( iVal==1 ){
159415
- res = OTA_DELETE;
159421
+ res = RBU_DELETE;
159416159422
}else if( iVal==2 ){
159417
- res = OTA_IDX_DELETE;
159423
+ res = RBU_IDX_DELETE;
159418159424
}else if( iVal==3 ){
159419
- res = OTA_IDX_INSERT;
159425
+ res = RBU_IDX_INSERT;
159420159426
}
159421159427
break;
159422159428
}
159423159429
159424159430
case SQLITE_TEXT: {
@@ -159426,21 +159432,21 @@
159426159432
if( z==0 ){
159427159433
p->rc = SQLITE_NOMEM;
159428159434
}else{
159429159435
*pzMask = (const char*)z;
159430159436
}
159431
- res = OTA_UPDATE;
159437
+ res = RBU_UPDATE;
159432159438
159433159439
break;
159434159440
}
159435159441
159436159442
default:
159437159443
break;
159438159444
}
159439159445
159440159446
if( res==0 ){
159441
- otaBadControlError(p);
159447
+ rbuBadControlError(p);
159442159448
}
159443159449
return res;
159444159450
}
159445159451
159446159452
#ifdef SQLITE_DEBUG
@@ -159454,82 +159460,82 @@
159454159460
#else
159455159461
# define assertColumnName(x,y,z)
159456159462
#endif
159457159463
159458159464
/*
159459
-** This function does the work for an sqlite3ota_step() call.
159465
+** This function does the work for an sqlite3rbu_step() call.
159460159466
**
159461159467
** The object-iterator (p->objiter) currently points to a valid object,
159462159468
** and the input cursor (p->objiter.pSelect) currently points to a valid
159463159469
** input row. Perform whatever processing is required and return.
159464159470
**
159465159471
** If no error occurs, SQLITE_OK is returned. Otherwise, an error code
159466
-** and message is left in the OTA handle and a copy of the error code
159472
+** and message is left in the RBU handle and a copy of the error code
159467159473
** returned.
159468159474
*/
159469
-static int otaStep(sqlite3ota *p){
159470
- OtaObjIter *pIter = &p->objiter;
159475
+static int rbuStep(sqlite3rbu *p){
159476
+ RbuObjIter *pIter = &p->objiter;
159471159477
const char *zMask = 0;
159472159478
int i;
159473
- int eType = otaStepType(p, &zMask);
159479
+ int eType = rbuStepType(p, &zMask);
159474159480
159475159481
if( eType ){
159476
- assert( eType!=OTA_UPDATE || pIter->zIdx==0 );
159482
+ assert( eType!=RBU_UPDATE || pIter->zIdx==0 );
159477159483
159478
- if( pIter->zIdx==0 && eType==OTA_IDX_DELETE ){
159479
- otaBadControlError(p);
159484
+ if( pIter->zIdx==0 && eType==RBU_IDX_DELETE ){
159485
+ rbuBadControlError(p);
159480159486
}
159481159487
else if(
159482
- eType==OTA_INSERT
159483
- || eType==OTA_DELETE
159484
- || eType==OTA_IDX_DELETE
159485
- || eType==OTA_IDX_INSERT
159488
+ eType==RBU_INSERT
159489
+ || eType==RBU_DELETE
159490
+ || eType==RBU_IDX_DELETE
159491
+ || eType==RBU_IDX_INSERT
159486159492
){
159487159493
sqlite3_value *pVal;
159488159494
sqlite3_stmt *pWriter;
159489159495
159490
- assert( eType!=OTA_UPDATE );
159491
- assert( eType!=OTA_DELETE || pIter->zIdx==0 );
159496
+ assert( eType!=RBU_UPDATE );
159497
+ assert( eType!=RBU_DELETE || pIter->zIdx==0 );
159492159498
159493
- if( eType==OTA_IDX_DELETE || eType==OTA_DELETE ){
159499
+ if( eType==RBU_IDX_DELETE || eType==RBU_DELETE ){
159494159500
pWriter = pIter->pDelete;
159495159501
}else{
159496159502
pWriter = pIter->pInsert;
159497159503
}
159498159504
159499159505
for(i=0; i<pIter->nCol; i++){
159500159506
/* If this is an INSERT into a table b-tree and the table has an
159501159507
** explicit INTEGER PRIMARY KEY, check that this is not an attempt
159502159508
** to write a NULL into the IPK column. That is not permitted. */
159503
- if( eType==OTA_INSERT
159504
- && pIter->zIdx==0 && pIter->eType==OTA_PK_IPK && pIter->abTblPk[i]
159509
+ if( eType==RBU_INSERT
159510
+ && pIter->zIdx==0 && pIter->eType==RBU_PK_IPK && pIter->abTblPk[i]
159505159511
&& sqlite3_column_type(pIter->pSelect, i)==SQLITE_NULL
159506159512
){
159507159513
p->rc = SQLITE_MISMATCH;
159508159514
p->zErrmsg = sqlite3_mprintf("datatype mismatch");
159509159515
goto step_out;
159510159516
}
159511159517
159512
- if( eType==OTA_DELETE && pIter->abTblPk[i]==0 ){
159518
+ if( eType==RBU_DELETE && pIter->abTblPk[i]==0 ){
159513159519
continue;
159514159520
}
159515159521
159516159522
pVal = sqlite3_column_value(pIter->pSelect, i);
159517159523
p->rc = sqlite3_bind_value(pWriter, i+1, pVal);
159518159524
if( p->rc ) goto step_out;
159519159525
}
159520159526
if( pIter->zIdx==0
159521
- && (pIter->eType==OTA_PK_VTAB || pIter->eType==OTA_PK_NONE)
159527
+ && (pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE)
159522159528
){
159523159529
/* For a virtual table, or a table with no primary key, the
159524159530
** SELECT statement is:
159525159531
**
159526
- ** SELECT <cols>, ota_control, ota_rowid FROM ....
159532
+ ** SELECT <cols>, rbu_control, rbu_rowid FROM ....
159527159533
**
159528159534
** Hence column_value(pIter->nCol+1).
159529159535
*/
159530
- assertColumnName(pIter->pSelect, pIter->nCol+1, "ota_rowid");
159536
+ assertColumnName(pIter->pSelect, pIter->nCol+1, "rbu_rowid");
159531159537
pVal = sqlite3_column_value(pIter->pSelect, pIter->nCol+1);
159532159538
p->rc = sqlite3_bind_value(pWriter, pIter->nCol+1, pVal);
159533159539
}
159534159540
if( p->rc==SQLITE_OK ){
159535159541
sqlite3_step(pWriter);
@@ -159536,25 +159542,25 @@
159536159542
p->rc = resetAndCollectError(pWriter, &p->zErrmsg);
159537159543
}
159538159544
}else{
159539159545
sqlite3_value *pVal;
159540159546
sqlite3_stmt *pUpdate = 0;
159541
- assert( eType==OTA_UPDATE );
159542
- otaGetUpdateStmt(p, pIter, zMask, &pUpdate);
159547
+ assert( eType==RBU_UPDATE );
159548
+ rbuGetUpdateStmt(p, pIter, zMask, &pUpdate);
159543159549
if( pUpdate ){
159544159550
for(i=0; p->rc==SQLITE_OK && i<pIter->nCol; i++){
159545159551
char c = zMask[pIter->aiSrcOrder[i]];
159546159552
pVal = sqlite3_column_value(pIter->pSelect, i);
159547159553
if( pIter->abTblPk[i] || c=='x' || c=='d' ){
159548159554
p->rc = sqlite3_bind_value(pUpdate, i+1, pVal);
159549159555
}
159550159556
}
159551159557
if( p->rc==SQLITE_OK
159552
- && (pIter->eType==OTA_PK_VTAB || pIter->eType==OTA_PK_NONE)
159558
+ && (pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE)
159553159559
){
159554
- /* Bind the ota_rowid value to column _rowid_ */
159555
- assertColumnName(pIter->pSelect, pIter->nCol+1, "ota_rowid");
159560
+ /* Bind the rbu_rowid value to column _rowid_ */
159561
+ assertColumnName(pIter->pSelect, pIter->nCol+1, "rbu_rowid");
159556159562
pVal = sqlite3_column_value(pIter->pSelect, pIter->nCol+1);
159557159563
p->rc = sqlite3_bind_value(pUpdate, pIter->nCol+1, pVal);
159558159564
}
159559159565
if( p->rc==SQLITE_OK ){
159560159566
sqlite3_step(pUpdate);
@@ -159569,11 +159575,11 @@
159569159575
}
159570159576
159571159577
/*
159572159578
** Increment the schema cookie of the main database opened by p->dbMain.
159573159579
*/
159574
-static void otaIncrSchemaCookie(sqlite3ota *p){
159580
+static void rbuIncrSchemaCookie(sqlite3rbu *p){
159575159581
if( p->rc==SQLITE_OK ){
159576159582
int iCookie = 1000000;
159577159583
sqlite3_stmt *pStmt;
159578159584
159579159585
p->rc = prepareAndCollectError(p->dbMain, &pStmt, &p->zErrmsg,
@@ -159586,49 +159592,49 @@
159586159592
** statement reads is page 1, which is guaranteed to be in the cache.
159587159593
** And no memory allocations are required. */
159588159594
if( SQLITE_ROW==sqlite3_step(pStmt) ){
159589159595
iCookie = sqlite3_column_int(pStmt, 0);
159590159596
}
159591
- otaFinalize(p, pStmt);
159597
+ rbuFinalize(p, pStmt);
159592159598
}
159593159599
if( p->rc==SQLITE_OK ){
159594
- otaMPrintfExec(p, p->dbMain, "PRAGMA schema_version = %d", iCookie+1);
159600
+ rbuMPrintfExec(p, p->dbMain, "PRAGMA schema_version = %d", iCookie+1);
159595159601
}
159596159602
}
159597159603
}
159598159604
159599159605
/*
159600
-** Update the contents of the ota_state table within the ota database. The
159601
-** value stored in the OTA_STATE_STAGE column is eStage. All other values
159602
-** are determined by inspecting the ota handle passed as the first argument.
159606
+** Update the contents of the rbu_state table within the rbu database. The
159607
+** value stored in the RBU_STATE_STAGE column is eStage. All other values
159608
+** are determined by inspecting the rbu handle passed as the first argument.
159603159609
*/
159604
-static void otaSaveState(sqlite3ota *p, int eStage){
159610
+static void rbuSaveState(sqlite3rbu *p, int eStage){
159605159611
if( p->rc==SQLITE_OK || p->rc==SQLITE_DONE ){
159606159612
sqlite3_stmt *pInsert = 0;
159607159613
int rc;
159608159614
159609159615
assert( p->zErrmsg==0 );
159610
- rc = prepareFreeAndCollectError(p->dbOta, &pInsert, &p->zErrmsg,
159616
+ rc = prepareFreeAndCollectError(p->dbRbu, &pInsert, &p->zErrmsg,
159611159617
sqlite3_mprintf(
159612
- "INSERT OR REPLACE INTO %s.ota_state(k, v) VALUES "
159618
+ "INSERT OR REPLACE INTO %s.rbu_state(k, v) VALUES "
159613159619
"(%d, %d), "
159614159620
"(%d, %Q), "
159615159621
"(%d, %Q), "
159616159622
"(%d, %d), "
159617159623
"(%d, %d), "
159618159624
"(%d, %lld), "
159619159625
"(%d, %lld), "
159620159626
"(%d, %lld) ",
159621159627
p->zStateDb,
159622
- OTA_STATE_STAGE, eStage,
159623
- OTA_STATE_TBL, p->objiter.zTbl,
159624
- OTA_STATE_IDX, p->objiter.zIdx,
159625
- OTA_STATE_ROW, p->nStep,
159626
- OTA_STATE_PROGRESS, p->nProgress,
159627
- OTA_STATE_CKPT, p->iWalCksum,
159628
- OTA_STATE_COOKIE, (i64)p->pTargetFd->iCookie,
159629
- OTA_STATE_OALSZ, p->iOalSz
159628
+ RBU_STATE_STAGE, eStage,
159629
+ RBU_STATE_TBL, p->objiter.zTbl,
159630
+ RBU_STATE_IDX, p->objiter.zIdx,
159631
+ RBU_STATE_ROW, p->nStep,
159632
+ RBU_STATE_PROGRESS, p->nProgress,
159633
+ RBU_STATE_CKPT, p->iWalCksum,
159634
+ RBU_STATE_COOKIE, (i64)p->pTargetFd->iCookie,
159635
+ RBU_STATE_OALSZ, p->iOalSz
159630159636
)
159631159637
);
159632159638
assert( pInsert==0 || rc==SQLITE_OK );
159633159639
159634159640
if( rc==SQLITE_OK ){
@@ -159639,71 +159645,71 @@
159639159645
}
159640159646
}
159641159647
159642159648
159643159649
/*
159644
-** Step the OTA object.
159650
+** Step the RBU object.
159645159651
*/
159646
-SQLITE_API int SQLITE_STDCALL sqlite3ota_step(sqlite3ota *p){
159652
+SQLITE_API int SQLITE_STDCALL sqlite3rbu_step(sqlite3rbu *p){
159647159653
if( p ){
159648159654
switch( p->eStage ){
159649
- case OTA_STAGE_OAL: {
159650
- OtaObjIter *pIter = &p->objiter;
159655
+ case RBU_STAGE_OAL: {
159656
+ RbuObjIter *pIter = &p->objiter;
159651159657
while( p->rc==SQLITE_OK && pIter->zTbl ){
159652159658
159653159659
if( pIter->bCleanup ){
159654
- /* Clean up the ota_tmp_xxx table for the previous table. It
159660
+ /* Clean up the rbu_tmp_xxx table for the previous table. It
159655159661
** cannot be dropped as there are currently active SQL statements.
159656159662
** But the contents can be deleted. */
159657159663
if( pIter->abIndexed ){
159658
- otaMPrintfExec(p, p->dbOta,
159659
- "DELETE FROM %s.'ota_tmp_%q'", p->zStateDb, pIter->zTbl
159664
+ rbuMPrintfExec(p, p->dbRbu,
159665
+ "DELETE FROM %s.'rbu_tmp_%q'", p->zStateDb, pIter->zTbl
159660159666
);
159661159667
}
159662159668
}else{
159663
- otaObjIterPrepareAll(p, pIter, 0);
159669
+ rbuObjIterPrepareAll(p, pIter, 0);
159664159670
159665159671
/* Advance to the next row to process. */
159666159672
if( p->rc==SQLITE_OK ){
159667159673
int rc = sqlite3_step(pIter->pSelect);
159668159674
if( rc==SQLITE_ROW ){
159669159675
p->nProgress++;
159670159676
p->nStep++;
159671
- return otaStep(p);
159677
+ return rbuStep(p);
159672159678
}
159673159679
p->rc = sqlite3_reset(pIter->pSelect);
159674159680
p->nStep = 0;
159675159681
}
159676159682
}
159677159683
159678
- otaObjIterNext(p, pIter);
159684
+ rbuObjIterNext(p, pIter);
159679159685
}
159680159686
159681159687
if( p->rc==SQLITE_OK ){
159682159688
assert( pIter->zTbl==0 );
159683
- otaSaveState(p, OTA_STAGE_MOVE);
159684
- otaIncrSchemaCookie(p);
159689
+ rbuSaveState(p, RBU_STAGE_MOVE);
159690
+ rbuIncrSchemaCookie(p);
159685159691
if( p->rc==SQLITE_OK ){
159686159692
p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
159687159693
}
159688159694
if( p->rc==SQLITE_OK ){
159689
- p->rc = sqlite3_exec(p->dbOta, "COMMIT", 0, 0, &p->zErrmsg);
159690
- }
159691
- p->eStage = OTA_STAGE_MOVE;
159692
- }
159693
- break;
159694
- }
159695
-
159696
- case OTA_STAGE_MOVE: {
159695
+ p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, &p->zErrmsg);
159696
+ }
159697
+ p->eStage = RBU_STAGE_MOVE;
159698
+ }
159699
+ break;
159700
+ }
159701
+
159702
+ case RBU_STAGE_MOVE: {
159697159703
if( p->rc==SQLITE_OK ){
159698
- otaMoveOalFile(p);
159704
+ rbuMoveOalFile(p);
159699159705
p->nProgress++;
159700159706
}
159701159707
break;
159702159708
}
159703159709
159704
- case OTA_STAGE_CKPT: {
159710
+ case RBU_STAGE_CKPT: {
159705159711
if( p->rc==SQLITE_OK ){
159706159712
if( p->nStep>=p->nFrame ){
159707159713
sqlite3_file *pDb = p->pTargetFd->pReal;
159708159714
159709159715
/* Sync the db file */
@@ -159717,16 +159723,16 @@
159717159723
((u32 volatile*)ptr)[24] = p->iMaxFrame;
159718159724
}
159719159725
}
159720159726
159721159727
if( p->rc==SQLITE_OK ){
159722
- p->eStage = OTA_STAGE_DONE;
159728
+ p->eStage = RBU_STAGE_DONE;
159723159729
p->rc = SQLITE_DONE;
159724159730
}
159725159731
}else{
159726
- OtaFrame *pFrame = &p->aFrame[p->nStep];
159727
- otaCheckpointFrame(p, pFrame);
159732
+ RbuFrame *pFrame = &p->aFrame[p->nStep];
159733
+ rbuCheckpointFrame(p, pFrame);
159728159734
p->nStep++;
159729159735
}
159730159736
p->nProgress++;
159731159737
}
159732159738
break;
@@ -159740,78 +159746,78 @@
159740159746
return SQLITE_NOMEM;
159741159747
}
159742159748
}
159743159749
159744159750
/*
159745
-** Free an OtaState object allocated by otaLoadState().
159751
+** Free an RbuState object allocated by rbuLoadState().
159746159752
*/
159747
-static void otaFreeState(OtaState *p){
159753
+static void rbuFreeState(RbuState *p){
159748159754
if( p ){
159749159755
sqlite3_free(p->zTbl);
159750159756
sqlite3_free(p->zIdx);
159751159757
sqlite3_free(p);
159752159758
}
159753159759
}
159754159760
159755159761
/*
159756
-** Allocate an OtaState object and load the contents of the ota_state
159762
+** Allocate an RbuState object and load the contents of the rbu_state
159757159763
** table into it. Return a pointer to the new object. It is the
159758159764
** responsibility of the caller to eventually free the object using
159759159765
** sqlite3_free().
159760159766
**
159761
-** If an error occurs, leave an error code and message in the ota handle
159767
+** If an error occurs, leave an error code and message in the rbu handle
159762159768
** and return NULL.
159763159769
*/
159764
-static OtaState *otaLoadState(sqlite3ota *p){
159765
- OtaState *pRet = 0;
159770
+static RbuState *rbuLoadState(sqlite3rbu *p){
159771
+ RbuState *pRet = 0;
159766159772
sqlite3_stmt *pStmt = 0;
159767159773
int rc;
159768159774
int rc2;
159769159775
159770
- pRet = (OtaState*)otaMalloc(p, sizeof(OtaState));
159776
+ pRet = (RbuState*)rbuMalloc(p, sizeof(RbuState));
159771159777
if( pRet==0 ) return 0;
159772159778
159773
- rc = prepareFreeAndCollectError(p->dbOta, &pStmt, &p->zErrmsg,
159774
- sqlite3_mprintf("SELECT k, v FROM %s.ota_state", p->zStateDb)
159779
+ rc = prepareFreeAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
159780
+ sqlite3_mprintf("SELECT k, v FROM %s.rbu_state", p->zStateDb)
159775159781
);
159776159782
while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
159777159783
switch( sqlite3_column_int(pStmt, 0) ){
159778
- case OTA_STATE_STAGE:
159784
+ case RBU_STATE_STAGE:
159779159785
pRet->eStage = sqlite3_column_int(pStmt, 1);
159780
- if( pRet->eStage!=OTA_STAGE_OAL
159781
- && pRet->eStage!=OTA_STAGE_MOVE
159782
- && pRet->eStage!=OTA_STAGE_CKPT
159786
+ if( pRet->eStage!=RBU_STAGE_OAL
159787
+ && pRet->eStage!=RBU_STAGE_MOVE
159788
+ && pRet->eStage!=RBU_STAGE_CKPT
159783159789
){
159784159790
p->rc = SQLITE_CORRUPT;
159785159791
}
159786159792
break;
159787159793
159788
- case OTA_STATE_TBL:
159789
- pRet->zTbl = otaStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
159794
+ case RBU_STATE_TBL:
159795
+ pRet->zTbl = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
159790159796
break;
159791159797
159792
- case OTA_STATE_IDX:
159793
- pRet->zIdx = otaStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
159798
+ case RBU_STATE_IDX:
159799
+ pRet->zIdx = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
159794159800
break;
159795159801
159796
- case OTA_STATE_ROW:
159802
+ case RBU_STATE_ROW:
159797159803
pRet->nRow = sqlite3_column_int(pStmt, 1);
159798159804
break;
159799159805
159800
- case OTA_STATE_PROGRESS:
159806
+ case RBU_STATE_PROGRESS:
159801159807
pRet->nProgress = sqlite3_column_int64(pStmt, 1);
159802159808
break;
159803159809
159804
- case OTA_STATE_CKPT:
159810
+ case RBU_STATE_CKPT:
159805159811
pRet->iWalCksum = sqlite3_column_int64(pStmt, 1);
159806159812
break;
159807159813
159808
- case OTA_STATE_COOKIE:
159814
+ case RBU_STATE_COOKIE:
159809159815
pRet->iCookie = (u32)sqlite3_column_int64(pStmt, 1);
159810159816
break;
159811159817
159812
- case OTA_STATE_OALSZ:
159818
+ case RBU_STATE_OALSZ:
159813159819
pRet->iOalSz = (u32)sqlite3_column_int64(pStmt, 1);
159814159820
break;
159815159821
159816159822
default:
159817159823
rc = SQLITE_CORRUPT;
@@ -159828,142 +159834,142 @@
159828159834
/*
159829159835
** Compare strings z1 and z2, returning 0 if they are identical, or non-zero
159830159836
** otherwise. Either or both argument may be NULL. Two NULL values are
159831159837
** considered equal, and NULL is considered distinct from all other values.
159832159838
*/
159833
-static int otaStrCompare(const char *z1, const char *z2){
159839
+static int rbuStrCompare(const char *z1, const char *z2){
159834159840
if( z1==0 && z2==0 ) return 0;
159835159841
if( z1==0 || z2==0 ) return 1;
159836159842
return (sqlite3_stricmp(z1, z2)!=0);
159837159843
}
159838159844
159839159845
/*
159840
-** This function is called as part of sqlite3ota_open() when initializing
159841
-** an ota handle in OAL stage. If the ota update has not started (i.e.
159842
-** the ota_state table was empty) it is a no-op. Otherwise, it arranges
159843
-** things so that the next call to sqlite3ota_step() continues on from
159844
-** where the previous ota handle left off.
159846
+** This function is called as part of sqlite3rbu_open() when initializing
159847
+** an rbu handle in OAL stage. If the rbu update has not started (i.e.
159848
+** the rbu_state table was empty) it is a no-op. Otherwise, it arranges
159849
+** things so that the next call to sqlite3rbu_step() continues on from
159850
+** where the previous rbu handle left off.
159845159851
**
159846159852
** If an error occurs, an error code and error message are left in the
159847
-** ota handle passed as the first argument.
159853
+** rbu handle passed as the first argument.
159848159854
*/
159849
-static void otaSetupOal(sqlite3ota *p, OtaState *pState){
159855
+static void rbuSetupOal(sqlite3rbu *p, RbuState *pState){
159850159856
assert( p->rc==SQLITE_OK );
159851159857
if( pState->zTbl ){
159852
- OtaObjIter *pIter = &p->objiter;
159858
+ RbuObjIter *pIter = &p->objiter;
159853159859
int rc = SQLITE_OK;
159854159860
159855159861
while( rc==SQLITE_OK && pIter->zTbl && (pIter->bCleanup
159856
- || otaStrCompare(pIter->zIdx, pState->zIdx)
159857
- || otaStrCompare(pIter->zTbl, pState->zTbl)
159862
+ || rbuStrCompare(pIter->zIdx, pState->zIdx)
159863
+ || rbuStrCompare(pIter->zTbl, pState->zTbl)
159858159864
)){
159859
- rc = otaObjIterNext(p, pIter);
159865
+ rc = rbuObjIterNext(p, pIter);
159860159866
}
159861159867
159862159868
if( rc==SQLITE_OK && !pIter->zTbl ){
159863159869
rc = SQLITE_ERROR;
159864
- p->zErrmsg = sqlite3_mprintf("ota_state mismatch error");
159870
+ p->zErrmsg = sqlite3_mprintf("rbu_state mismatch error");
159865159871
}
159866159872
159867159873
if( rc==SQLITE_OK ){
159868159874
p->nStep = pState->nRow;
159869
- rc = otaObjIterPrepareAll(p, &p->objiter, p->nStep);
159875
+ rc = rbuObjIterPrepareAll(p, &p->objiter, p->nStep);
159870159876
}
159871159877
159872159878
p->rc = rc;
159873159879
}
159874159880
}
159875159881
159876159882
/*
159877159883
** If there is a "*-oal" file in the file-system corresponding to the
159878159884
** target database in the file-system, delete it. If an error occurs,
159879
-** leave an error code and error message in the ota handle.
159885
+** leave an error code and error message in the rbu handle.
159880159886
*/
159881
-static void otaDeleteOalFile(sqlite3ota *p){
159887
+static void rbuDeleteOalFile(sqlite3rbu *p){
159882159888
char *zOal = sqlite3_mprintf("%s-oal", p->zTarget);
159883159889
assert( p->rc==SQLITE_OK && p->zErrmsg==0 );
159884159890
unlink(zOal);
159885159891
sqlite3_free(zOal);
159886159892
}
159887159893
159888159894
/*
159889
-** Allocate a private ota VFS for the ota handle passed as the only
159890
-** argument. This VFS will be used unless the call to sqlite3ota_open()
159895
+** Allocate a private rbu VFS for the rbu handle passed as the only
159896
+** argument. This VFS will be used unless the call to sqlite3rbu_open()
159891159897
** specified a URI with a vfs=? option in place of a target database
159892159898
** file name.
159893159899
*/
159894
-static void otaCreateVfs(sqlite3ota *p){
159900
+static void rbuCreateVfs(sqlite3rbu *p){
159895159901
int rnd;
159896159902
char zRnd[64];
159897159903
159898159904
assert( p->rc==SQLITE_OK );
159899159905
sqlite3_randomness(sizeof(int), (void*)&rnd);
159900
- sprintf(zRnd, "ota_vfs_%d", rnd);
159901
- p->rc = sqlite3ota_create_vfs(zRnd, 0);
159906
+ sprintf(zRnd, "rbu_vfs_%d", rnd);
159907
+ p->rc = sqlite3rbu_create_vfs(zRnd, 0);
159902159908
if( p->rc==SQLITE_OK ){
159903159909
sqlite3_vfs *pVfs = sqlite3_vfs_find(zRnd);
159904159910
assert( pVfs );
159905159911
p->zVfsName = pVfs->zName;
159906159912
}
159907159913
}
159908159914
159909159915
/*
159910
-** Destroy the private VFS created for the ota handle passed as the only
159911
-** argument by an earlier call to otaCreateVfs().
159916
+** Destroy the private VFS created for the rbu handle passed as the only
159917
+** argument by an earlier call to rbuCreateVfs().
159912159918
*/
159913
-static void otaDeleteVfs(sqlite3ota *p){
159919
+static void rbuDeleteVfs(sqlite3rbu *p){
159914159920
if( p->zVfsName ){
159915
- sqlite3ota_destroy_vfs(p->zVfsName);
159921
+ sqlite3rbu_destroy_vfs(p->zVfsName);
159916159922
p->zVfsName = 0;
159917159923
}
159918159924
}
159919159925
159920159926
/*
159921
-** Open and return a new OTA handle.
159927
+** Open and return a new RBU handle.
159922159928
*/
159923
-SQLITE_API sqlite3ota *SQLITE_STDCALL sqlite3ota_open(
159929
+SQLITE_API sqlite3rbu *SQLITE_STDCALL sqlite3rbu_open(
159924159930
const char *zTarget,
159925
- const char *zOta,
159931
+ const char *zRbu,
159926159932
const char *zState
159927159933
){
159928
- sqlite3ota *p;
159934
+ sqlite3rbu *p;
159929159935
int nTarget = strlen(zTarget);
159930
- int nOta = strlen(zOta);
159936
+ int nRbu = strlen(zRbu);
159931159937
int nState = zState ? strlen(zState) : 0;
159932159938
159933
- p = (sqlite3ota*)sqlite3_malloc(sizeof(sqlite3ota)+nTarget+1+nOta+1+nState+1);
159939
+ p = (sqlite3rbu*)sqlite3_malloc(sizeof(sqlite3rbu)+nTarget+1+nRbu+1+nState+1);
159934159940
if( p ){
159935
- OtaState *pState = 0;
159941
+ RbuState *pState = 0;
159936159942
159937159943
/* Create the custom VFS. */
159938
- memset(p, 0, sizeof(sqlite3ota));
159939
- otaCreateVfs(p);
159944
+ memset(p, 0, sizeof(sqlite3rbu));
159945
+ rbuCreateVfs(p);
159940159946
159941159947
/* Open the target database */
159942159948
if( p->rc==SQLITE_OK ){
159943159949
p->zTarget = (char*)&p[1];
159944159950
memcpy(p->zTarget, zTarget, nTarget+1);
159945
- p->zOta = &p->zTarget[nTarget+1];
159946
- memcpy(p->zOta, zOta, nOta+1);
159951
+ p->zRbu = &p->zTarget[nTarget+1];
159952
+ memcpy(p->zRbu, zRbu, nRbu+1);
159947159953
if( zState ){
159948
- p->zState = &p->zOta[nOta+1];
159954
+ p->zState = &p->zRbu[nRbu+1];
159949159955
memcpy(p->zState, zState, nState+1);
159950159956
}
159951
- otaOpenDatabase(p);
159957
+ rbuOpenDatabase(p);
159952159958
}
159953159959
159954
- /* If it has not already been created, create the ota_state table */
159955
- otaMPrintfExec(p, p->dbOta, OTA_CREATE_STATE, p->zStateDb);
159960
+ /* If it has not already been created, create the rbu_state table */
159961
+ rbuMPrintfExec(p, p->dbRbu, RBU_CREATE_STATE, p->zStateDb);
159956159962
159957159963
if( p->rc==SQLITE_OK ){
159958
- pState = otaLoadState(p);
159964
+ pState = rbuLoadState(p);
159959159965
assert( pState || p->rc!=SQLITE_OK );
159960159966
if( p->rc==SQLITE_OK ){
159961159967
159962159968
if( pState->eStage==0 ){
159963
- otaDeleteOalFile(p);
159964
- p->eStage = OTA_STAGE_OAL;
159969
+ rbuDeleteOalFile(p);
159970
+ p->eStage = RBU_STAGE_OAL;
159965159971
}else{
159966159972
p->eStage = pState->eStage;
159967159973
}
159968159974
p->nProgress = pState->nProgress;
159969159975
p->iOalSz = pState->iOalSz;
@@ -159970,97 +159976,97 @@
159970159976
}
159971159977
}
159972159978
assert( p->rc!=SQLITE_OK || p->eStage!=0 );
159973159979
159974159980
if( p->rc==SQLITE_OK && p->pTargetFd->pWalFd ){
159975
- if( p->eStage==OTA_STAGE_OAL ){
159981
+ if( p->eStage==RBU_STAGE_OAL ){
159976159982
p->rc = SQLITE_ERROR;
159977159983
p->zErrmsg = sqlite3_mprintf("cannot update wal mode database");
159978
- }else if( p->eStage==OTA_STAGE_MOVE ){
159979
- p->eStage = OTA_STAGE_CKPT;
159984
+ }else if( p->eStage==RBU_STAGE_MOVE ){
159985
+ p->eStage = RBU_STAGE_CKPT;
159980159986
p->nStep = 0;
159981159987
}
159982159988
}
159983159989
159984159990
if( p->rc==SQLITE_OK
159985
- && (p->eStage==OTA_STAGE_OAL || p->eStage==OTA_STAGE_MOVE)
159991
+ && (p->eStage==RBU_STAGE_OAL || p->eStage==RBU_STAGE_MOVE)
159986159992
&& pState->eStage!=0 && p->pTargetFd->iCookie!=pState->iCookie
159987159993
){
159988159994
/* At this point (pTargetFd->iCookie) contains the value of the
159989159995
** change-counter cookie (the thing that gets incremented when a
159990159996
** transaction is committed in rollback mode) currently stored on
159991159997
** page 1 of the database file. */
159992159998
p->rc = SQLITE_BUSY;
159993
- p->zErrmsg = sqlite3_mprintf("database modified during ota update");
159999
+ p->zErrmsg = sqlite3_mprintf("database modified during rbu update");
159994160000
}
159995160001
159996160002
if( p->rc==SQLITE_OK ){
159997
- if( p->eStage==OTA_STAGE_OAL ){
160003
+ if( p->eStage==RBU_STAGE_OAL ){
159998160004
159999160005
/* Open transactions both databases. The *-oal file is opened or
160000160006
** created at this point. */
160001160007
p->rc = sqlite3_exec(p->dbMain, "BEGIN IMMEDIATE", 0, 0, &p->zErrmsg);
160002160008
if( p->rc==SQLITE_OK ){
160003
- p->rc = sqlite3_exec(p->dbOta, "BEGIN IMMEDIATE", 0, 0, &p->zErrmsg);
160009
+ p->rc = sqlite3_exec(p->dbRbu, "BEGIN IMMEDIATE", 0, 0, &p->zErrmsg);
160004160010
}
160005160011
160006160012
/* Point the object iterator at the first object */
160007160013
if( p->rc==SQLITE_OK ){
160008
- p->rc = otaObjIterFirst(p, &p->objiter);
160014
+ p->rc = rbuObjIterFirst(p, &p->objiter);
160009160015
}
160010160016
160011
- /* If the OTA database contains no data_xxx tables, declare the OTA
160017
+ /* If the RBU database contains no data_xxx tables, declare the RBU
160012160018
** update finished. */
160013160019
if( p->rc==SQLITE_OK && p->objiter.zTbl==0 ){
160014160020
p->rc = SQLITE_DONE;
160015160021
}
160016160022
160017160023
if( p->rc==SQLITE_OK ){
160018
- otaSetupOal(p, pState);
160024
+ rbuSetupOal(p, pState);
160019160025
}
160020160026
160021
- }else if( p->eStage==OTA_STAGE_MOVE ){
160027
+ }else if( p->eStage==RBU_STAGE_MOVE ){
160022160028
/* no-op */
160023
- }else if( p->eStage==OTA_STAGE_CKPT ){
160024
- otaSetupCheckpoint(p, pState);
160025
- }else if( p->eStage==OTA_STAGE_DONE ){
160029
+ }else if( p->eStage==RBU_STAGE_CKPT ){
160030
+ rbuSetupCheckpoint(p, pState);
160031
+ }else if( p->eStage==RBU_STAGE_DONE ){
160026160032
p->rc = SQLITE_DONE;
160027160033
}else{
160028160034
p->rc = SQLITE_CORRUPT;
160029160035
}
160030160036
}
160031160037
160032
- otaFreeState(pState);
160038
+ rbuFreeState(pState);
160033160039
}
160034160040
160035160041
return p;
160036160042
}
160037160043
160038160044
160039160045
/*
160040
-** Return the database handle used by pOta.
160046
+** Return the database handle used by pRbu.
160041160047
*/
160042
-SQLITE_API sqlite3 *SQLITE_STDCALL sqlite3ota_db(sqlite3ota *pOta, int bOta){
160048
+SQLITE_API sqlite3 *SQLITE_STDCALL sqlite3rbu_db(sqlite3rbu *pRbu, int bRbu){
160043160049
sqlite3 *db = 0;
160044
- if( pOta ){
160045
- db = (bOta ? pOta->dbOta : pOta->dbMain);
160050
+ if( pRbu ){
160051
+ db = (bRbu ? pRbu->dbRbu : pRbu->dbMain);
160046160052
}
160047160053
return db;
160048160054
}
160049160055
160050160056
160051160057
/*
160052
-** If the error code currently stored in the OTA handle is SQLITE_CONSTRAINT,
160058
+** If the error code currently stored in the RBU handle is SQLITE_CONSTRAINT,
160053160059
** then edit any error message string so as to remove all occurrences of
160054
-** the pattern "ota_imp_[0-9]*".
160060
+** the pattern "rbu_imp_[0-9]*".
160055160061
*/
160056
-static void otaEditErrmsg(sqlite3ota *p){
160062
+static void rbuEditErrmsg(sqlite3rbu *p){
160057160063
if( p->rc==SQLITE_CONSTRAINT && p->zErrmsg ){
160058160064
int i;
160059160065
int nErrmsg = strlen(p->zErrmsg);
160060160066
for(i=0; i<(nErrmsg-8); i++){
160061
- if( memcmp(&p->zErrmsg[i], "ota_imp_", 8)==0 ){
160067
+ if( memcmp(&p->zErrmsg[i], "rbu_imp_", 8)==0 ){
160062160068
int nDel = 8;
160063160069
while( p->zErrmsg[i+nDel]>='0' && p->zErrmsg[i+nDel]<='9' ) nDel++;
160064160070
memmove(&p->zErrmsg[i], &p->zErrmsg[i+nDel], nErrmsg + 1 - i - nDel);
160065160071
nErrmsg -= nDel;
160066160072
}
@@ -160067,38 +160073,38 @@
160067160073
}
160068160074
}
160069160075
}
160070160076
160071160077
/*
160072
-** Close the OTA handle.
160078
+** Close the RBU handle.
160073160079
*/
160074
-SQLITE_API int SQLITE_STDCALL sqlite3ota_close(sqlite3ota *p, char **pzErrmsg){
160080
+SQLITE_API int SQLITE_STDCALL sqlite3rbu_close(sqlite3rbu *p, char **pzErrmsg){
160075160081
int rc;
160076160082
if( p ){
160077160083
160078160084
/* Commit the transaction to the *-oal file. */
160079
- if( p->rc==SQLITE_OK && p->eStage==OTA_STAGE_OAL ){
160085
+ if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_OAL ){
160080160086
p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
160081160087
}
160082160088
160083
- otaSaveState(p, p->eStage);
160089
+ rbuSaveState(p, p->eStage);
160084160090
160085
- if( p->rc==SQLITE_OK && p->eStage==OTA_STAGE_OAL ){
160086
- p->rc = sqlite3_exec(p->dbOta, "COMMIT", 0, 0, &p->zErrmsg);
160091
+ if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_OAL ){
160092
+ p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, &p->zErrmsg);
160087160093
}
160088160094
160089160095
/* Close any open statement handles. */
160090
- otaObjIterFinalize(&p->objiter);
160096
+ rbuObjIterFinalize(&p->objiter);
160091160097
160092160098
/* Close the open database handle and VFS object. */
160093160099
sqlite3_close(p->dbMain);
160094
- sqlite3_close(p->dbOta);
160095
- otaDeleteVfs(p);
160100
+ sqlite3_close(p->dbRbu);
160101
+ rbuDeleteVfs(p);
160096160102
sqlite3_free(p->aBuf);
160097160103
sqlite3_free(p->aFrame);
160098160104
160099
- otaEditErrmsg(p);
160105
+ rbuEditErrmsg(p);
160100160106
rc = p->rc;
160101160107
*pzErrmsg = p->zErrmsg;
160102160108
sqlite3_free(p);
160103160109
}else{
160104160110
rc = SQLITE_NOMEM;
@@ -160108,65 +160114,65 @@
160108160114
}
160109160115
160110160116
/*
160111160117
** Return the total number of key-value operations (inserts, deletes or
160112160118
** updates) that have been performed on the target database since the
160113
-** current OTA update was started.
160119
+** current RBU update was started.
160114160120
*/
160115
-SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3ota_progress(sqlite3ota *pOta){
160116
- return pOta->nProgress;
160121
+SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3rbu_progress(sqlite3rbu *pRbu){
160122
+ return pRbu->nProgress;
160117160123
}
160118160124
160119160125
/**************************************************************************
160120
-** Beginning of OTA VFS shim methods. The VFS shim modifies the behaviour
160126
+** Beginning of RBU VFS shim methods. The VFS shim modifies the behaviour
160121160127
** of a standard VFS in the following ways:
160122160128
**
160123160129
** 1. Whenever the first page of a main database file is read or
160124160130
** written, the value of the change-counter cookie is stored in
160125
-** ota_file.iCookie. Similarly, the value of the "write-version"
160126
-** database header field is stored in ota_file.iWriteVer. This ensures
160131
+** rbu_file.iCookie. Similarly, the value of the "write-version"
160132
+** database header field is stored in rbu_file.iWriteVer. This ensures
160127160133
** that the values are always trustworthy within an open transaction.
160128160134
**
160129
-** 2. Whenever an SQLITE_OPEN_WAL file is opened, the (ota_file.pWalFd)
160135
+** 2. Whenever an SQLITE_OPEN_WAL file is opened, the (rbu_file.pWalFd)
160130160136
** member variable of the associated database file descriptor is set
160131160137
** to point to the new file. A mutex protected linked list of all main
160132
-** db fds opened using a particular OTA VFS is maintained at
160133
-** ota_vfs.pMain to facilitate this.
160138
+** db fds opened using a particular RBU VFS is maintained at
160139
+** rbu_vfs.pMain to facilitate this.
160134160140
**
160135
-** 3. Using a new file-control "SQLITE_FCNTL_OTA", a main db ota_file
160136
-** object can be marked as the target database of an OTA update. This
160141
+** 3. Using a new file-control "SQLITE_FCNTL_RBU", a main db rbu_file
160142
+** object can be marked as the target database of an RBU update. This
160137160143
** turns on the following extra special behaviour:
160138160144
**
160139160145
** 3a. If xAccess() is called to check if there exists a *-wal file
160140
-** associated with an OTA target database currently in OTA_STAGE_OAL
160146
+** associated with an RBU target database currently in RBU_STAGE_OAL
160141160147
** stage (preparing the *-oal file), the following special handling
160142160148
** applies:
160143160149
**
160144
-** * if the *-wal file does exist, return SQLITE_CANTOPEN. An OTA
160150
+** * if the *-wal file does exist, return SQLITE_CANTOPEN. An RBU
160145160151
** target database may not be in wal mode already.
160146160152
**
160147160153
** * if the *-wal file does not exist, set the output parameter to
160148160154
** non-zero (to tell SQLite that it does exist) anyway.
160149160155
**
160150160156
** Then, when xOpen() is called to open the *-wal file associated with
160151
-** the OTA target in OTA_STAGE_OAL stage, instead of opening the *-wal
160152
-** file, the ota vfs opens the corresponding *-oal file instead.
160157
+** the RBU target in RBU_STAGE_OAL stage, instead of opening the *-wal
160158
+** file, the rbu vfs opens the corresponding *-oal file instead.
160153160159
**
160154160160
** 3b. The *-shm pages returned by xShmMap() for a target db file in
160155
-** OTA_STAGE_OAL mode are actually stored in heap memory. This is to
160161
+** RBU_STAGE_OAL mode are actually stored in heap memory. This is to
160156160162
** avoid creating a *-shm file on disk. Additionally, xShmLock() calls
160157
-** are no-ops on target database files in OTA_STAGE_OAL mode. This is
160163
+** are no-ops on target database files in RBU_STAGE_OAL mode. This is
160158160164
** because assert() statements in some VFS implementations fail if
160159160165
** xShmLock() is called before xShmMap().
160160160166
**
160161160167
** 3c. If an EXCLUSIVE lock is attempted on a target database file in any
160162
-** mode except OTA_STAGE_DONE (all work completed and checkpointed), it
160163
-** fails with an SQLITE_BUSY error. This is to stop OTA connections
160168
+** mode except RBU_STAGE_DONE (all work completed and checkpointed), it
160169
+** fails with an SQLITE_BUSY error. This is to stop RBU connections
160164160170
** from automatically checkpointing a *-wal (or *-oal) file from within
160165160171
** sqlite3_close().
160166160172
**
160167
-** 3d. In OTA_STAGE_CAPTURE mode, all xRead() calls on the wal file, and
160173
+** 3d. In RBU_STAGE_CAPTURE mode, all xRead() calls on the wal file, and
160168160174
** all xWrite() calls on the target database file perform no IO.
160169160175
** Instead the frame and page numbers that would be read and written
160170160176
** are recorded. Additionally, successful attempts to obtain exclusive
160171160177
** xShmLock() WRITER, CHECKPOINTER and READ0 locks on the target
160172160178
** database file are recorded. xShmLock() calls to unlock the same
@@ -160173,28 +160179,28 @@
160173160179
** locks are no-ops (so that once obtained, these locks are never
160174160180
** relinquished). Finally, calls to xSync() on the target database
160175160181
** file fail with SQLITE_INTERNAL errors.
160176160182
*/
160177160183
160178
-static void otaUnlockShm(ota_file *p){
160179
- if( p->pOta ){
160184
+static void rbuUnlockShm(rbu_file *p){
160185
+ if( p->pRbu ){
160180160186
int (*xShmLock)(sqlite3_file*,int,int,int) = p->pReal->pMethods->xShmLock;
160181160187
int i;
160182160188
for(i=0; i<SQLITE_SHM_NLOCK;i++){
160183
- if( (1<<i) & p->pOta->mLock ){
160189
+ if( (1<<i) & p->pRbu->mLock ){
160184160190
xShmLock(p->pReal, i, 1, SQLITE_SHM_UNLOCK|SQLITE_SHM_EXCLUSIVE);
160185160191
}
160186160192
}
160187
- p->pOta->mLock = 0;
160193
+ p->pRbu->mLock = 0;
160188160194
}
160189160195
}
160190160196
160191160197
/*
160192
-** Close an ota file.
160198
+** Close an rbu file.
160193160199
*/
160194
-static int otaVfsClose(sqlite3_file *pFile){
160195
- ota_file *p = (ota_file*)pFile;
160200
+static int rbuVfsClose(sqlite3_file *pFile){
160201
+ rbu_file *p = (rbu_file*)pFile;
160196160202
int rc;
160197160203
int i;
160198160204
160199160205
/* Free the contents of the apShm[] array. And the array itself. */
160200160206
for(i=0; i<p->nShm; i++){
@@ -160203,16 +160209,16 @@
160203160209
sqlite3_free(p->apShm);
160204160210
p->apShm = 0;
160205160211
sqlite3_free(p->zDel);
160206160212
160207160213
if( p->openFlags & SQLITE_OPEN_MAIN_DB ){
160208
- ota_file **pp;
160209
- sqlite3_mutex_enter(p->pOtaVfs->mutex);
160210
- for(pp=&p->pOtaVfs->pMain; *pp!=p; pp=&((*pp)->pMainNext));
160214
+ rbu_file **pp;
160215
+ sqlite3_mutex_enter(p->pRbuVfs->mutex);
160216
+ for(pp=&p->pRbuVfs->pMain; *pp!=p; pp=&((*pp)->pMainNext));
160211160217
*pp = p->pMainNext;
160212
- sqlite3_mutex_leave(p->pOtaVfs->mutex);
160213
- otaUnlockShm(p);
160218
+ sqlite3_mutex_leave(p->pRbuVfs->mutex);
160219
+ rbuUnlockShm(p);
160214160220
p->pReal->pMethods->xShmUnmap(p->pReal, 0);
160215160221
}
160216160222
160217160223
/* Close the underlying file handle */
160218160224
rc = p->pReal->pMethods->xClose(p->pReal);
@@ -160222,37 +160228,37 @@
160222160228
160223160229
/*
160224160230
** Read and return an unsigned 32-bit big-endian integer from the buffer
160225160231
** passed as the only argument.
160226160232
*/
160227
-static u32 otaGetU32(u8 *aBuf){
160233
+static u32 rbuGetU32(u8 *aBuf){
160228160234
return ((u32)aBuf[0] << 24)
160229160235
+ ((u32)aBuf[1] << 16)
160230160236
+ ((u32)aBuf[2] << 8)
160231160237
+ ((u32)aBuf[3]);
160232160238
}
160233160239
160234160240
/*
160235
-** Read data from an otaVfs-file.
160241
+** Read data from an rbuVfs-file.
160236160242
*/
160237
-static int otaVfsRead(
160243
+static int rbuVfsRead(
160238160244
sqlite3_file *pFile,
160239160245
void *zBuf,
160240160246
int iAmt,
160241160247
sqlite_int64 iOfst
160242160248
){
160243
- ota_file *p = (ota_file*)pFile;
160244
- sqlite3ota *pOta = p->pOta;
160249
+ rbu_file *p = (rbu_file*)pFile;
160250
+ sqlite3rbu *pRbu = p->pRbu;
160245160251
int rc;
160246160252
160247
- if( pOta && pOta->eStage==OTA_STAGE_CAPTURE ){
160253
+ if( pRbu && pRbu->eStage==RBU_STAGE_CAPTURE ){
160248160254
assert( p->openFlags & SQLITE_OPEN_WAL );
160249
- rc = otaCaptureWalRead(p->pOta, iOfst, iAmt);
160255
+ rc = rbuCaptureWalRead(p->pRbu, iOfst, iAmt);
160250160256
}else{
160251
- if( pOta && pOta->eStage==OTA_STAGE_OAL
160257
+ if( pRbu && pRbu->eStage==RBU_STAGE_OAL
160252160258
&& (p->openFlags & SQLITE_OPEN_WAL)
160253
- && iOfst>=pOta->iOalSz
160259
+ && iOfst>=pRbu->iOalSz
160254160260
){
160255160261
rc = SQLITE_OK;
160256160262
memset(zBuf, 0, iAmt);
160257160263
}else{
160258160264
rc = p->pReal->pMethods->xRead(p->pReal, zBuf, iAmt, iOfst);
@@ -160259,92 +160265,92 @@
160259160265
}
160260160266
if( rc==SQLITE_OK && iOfst==0 && (p->openFlags & SQLITE_OPEN_MAIN_DB) ){
160261160267
/* These look like magic numbers. But they are stable, as they are part
160262160268
** of the definition of the SQLite file format, which may not change. */
160263160269
u8 *pBuf = (u8*)zBuf;
160264
- p->iCookie = otaGetU32(&pBuf[24]);
160270
+ p->iCookie = rbuGetU32(&pBuf[24]);
160265160271
p->iWriteVer = pBuf[19];
160266160272
}
160267160273
}
160268160274
return rc;
160269160275
}
160270160276
160271160277
/*
160272
-** Write data to an otaVfs-file.
160278
+** Write data to an rbuVfs-file.
160273160279
*/
160274
-static int otaVfsWrite(
160280
+static int rbuVfsWrite(
160275160281
sqlite3_file *pFile,
160276160282
const void *zBuf,
160277160283
int iAmt,
160278160284
sqlite_int64 iOfst
160279160285
){
160280
- ota_file *p = (ota_file*)pFile;
160281
- sqlite3ota *pOta = p->pOta;
160286
+ rbu_file *p = (rbu_file*)pFile;
160287
+ sqlite3rbu *pRbu = p->pRbu;
160282160288
int rc;
160283160289
160284
- if( pOta && pOta->eStage==OTA_STAGE_CAPTURE ){
160290
+ if( pRbu && pRbu->eStage==RBU_STAGE_CAPTURE ){
160285160291
assert( p->openFlags & SQLITE_OPEN_MAIN_DB );
160286
- rc = otaCaptureDbWrite(p->pOta, iOfst);
160292
+ rc = rbuCaptureDbWrite(p->pRbu, iOfst);
160287160293
}else{
160288
- if( pOta && pOta->eStage==OTA_STAGE_OAL
160294
+ if( pRbu && pRbu->eStage==RBU_STAGE_OAL
160289160295
&& (p->openFlags & SQLITE_OPEN_WAL)
160290
- && iOfst>=pOta->iOalSz
160296
+ && iOfst>=pRbu->iOalSz
160291160297
){
160292
- pOta->iOalSz = iAmt + iOfst;
160298
+ pRbu->iOalSz = iAmt + iOfst;
160293160299
}
160294160300
rc = p->pReal->pMethods->xWrite(p->pReal, zBuf, iAmt, iOfst);
160295160301
if( rc==SQLITE_OK && iOfst==0 && (p->openFlags & SQLITE_OPEN_MAIN_DB) ){
160296160302
/* These look like magic numbers. But they are stable, as they are part
160297160303
** of the definition of the SQLite file format, which may not change. */
160298160304
u8 *pBuf = (u8*)zBuf;
160299
- p->iCookie = otaGetU32(&pBuf[24]);
160305
+ p->iCookie = rbuGetU32(&pBuf[24]);
160300160306
p->iWriteVer = pBuf[19];
160301160307
}
160302160308
}
160303160309
return rc;
160304160310
}
160305160311
160306160312
/*
160307
-** Truncate an otaVfs-file.
160313
+** Truncate an rbuVfs-file.
160308160314
*/
160309
-static int otaVfsTruncate(sqlite3_file *pFile, sqlite_int64 size){
160310
- ota_file *p = (ota_file*)pFile;
160315
+static int rbuVfsTruncate(sqlite3_file *pFile, sqlite_int64 size){
160316
+ rbu_file *p = (rbu_file*)pFile;
160311160317
return p->pReal->pMethods->xTruncate(p->pReal, size);
160312160318
}
160313160319
160314160320
/*
160315
-** Sync an otaVfs-file.
160321
+** Sync an rbuVfs-file.
160316160322
*/
160317
-static int otaVfsSync(sqlite3_file *pFile, int flags){
160318
- ota_file *p = (ota_file *)pFile;
160319
- if( p->pOta && p->pOta->eStage==OTA_STAGE_CAPTURE ){
160323
+static int rbuVfsSync(sqlite3_file *pFile, int flags){
160324
+ rbu_file *p = (rbu_file *)pFile;
160325
+ if( p->pRbu && p->pRbu->eStage==RBU_STAGE_CAPTURE ){
160320160326
if( p->openFlags & SQLITE_OPEN_MAIN_DB ){
160321160327
return SQLITE_INTERNAL;
160322160328
}
160323160329
return SQLITE_OK;
160324160330
}
160325160331
return p->pReal->pMethods->xSync(p->pReal, flags);
160326160332
}
160327160333
160328160334
/*
160329
-** Return the current file-size of an otaVfs-file.
160335
+** Return the current file-size of an rbuVfs-file.
160330160336
*/
160331
-static int otaVfsFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
160332
- ota_file *p = (ota_file *)pFile;
160337
+static int rbuVfsFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
160338
+ rbu_file *p = (rbu_file *)pFile;
160333160339
return p->pReal->pMethods->xFileSize(p->pReal, pSize);
160334160340
}
160335160341
160336160342
/*
160337
-** Lock an otaVfs-file.
160343
+** Lock an rbuVfs-file.
160338160344
*/
160339
-static int otaVfsLock(sqlite3_file *pFile, int eLock){
160340
- ota_file *p = (ota_file*)pFile;
160341
- sqlite3ota *pOta = p->pOta;
160345
+static int rbuVfsLock(sqlite3_file *pFile, int eLock){
160346
+ rbu_file *p = (rbu_file*)pFile;
160347
+ sqlite3rbu *pRbu = p->pRbu;
160342160348
int rc = SQLITE_OK;
160343160349
160344160350
assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
160345
- if( pOta && eLock==SQLITE_LOCK_EXCLUSIVE && pOta->eStage!=OTA_STAGE_DONE ){
160351
+ if( pRbu && eLock==SQLITE_LOCK_EXCLUSIVE && pRbu->eStage!=RBU_STAGE_DONE ){
160346160352
/* Do not allow EXCLUSIVE locks. Preventing SQLite from taking this
160347160353
** prevents it from checkpointing the database from sqlite3_close(). */
160348160354
rc = SQLITE_BUSY;
160349160355
}else{
160350160356
rc = p->pReal->pMethods->xLock(p->pReal, eLock);
@@ -160352,122 +160358,122 @@
160352160358
160353160359
return rc;
160354160360
}
160355160361
160356160362
/*
160357
-** Unlock an otaVfs-file.
160363
+** Unlock an rbuVfs-file.
160358160364
*/
160359
-static int otaVfsUnlock(sqlite3_file *pFile, int eLock){
160360
- ota_file *p = (ota_file *)pFile;
160365
+static int rbuVfsUnlock(sqlite3_file *pFile, int eLock){
160366
+ rbu_file *p = (rbu_file *)pFile;
160361160367
return p->pReal->pMethods->xUnlock(p->pReal, eLock);
160362160368
}
160363160369
160364160370
/*
160365
-** Check if another file-handle holds a RESERVED lock on an otaVfs-file.
160371
+** Check if another file-handle holds a RESERVED lock on an rbuVfs-file.
160366160372
*/
160367
-static int otaVfsCheckReservedLock(sqlite3_file *pFile, int *pResOut){
160368
- ota_file *p = (ota_file *)pFile;
160373
+static int rbuVfsCheckReservedLock(sqlite3_file *pFile, int *pResOut){
160374
+ rbu_file *p = (rbu_file *)pFile;
160369160375
return p->pReal->pMethods->xCheckReservedLock(p->pReal, pResOut);
160370160376
}
160371160377
160372160378
/*
160373
-** File control method. For custom operations on an otaVfs-file.
160379
+** File control method. For custom operations on an rbuVfs-file.
160374160380
*/
160375
-static int otaVfsFileControl(sqlite3_file *pFile, int op, void *pArg){
160376
- ota_file *p = (ota_file *)pFile;
160381
+static int rbuVfsFileControl(sqlite3_file *pFile, int op, void *pArg){
160382
+ rbu_file *p = (rbu_file *)pFile;
160377160383
int (*xControl)(sqlite3_file*,int,void*) = p->pReal->pMethods->xFileControl;
160378160384
int rc;
160379160385
160380160386
assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB)
160381160387
|| p->openFlags & (SQLITE_OPEN_TRANSIENT_DB|SQLITE_OPEN_TEMP_JOURNAL)
160382160388
);
160383
- if( op==SQLITE_FCNTL_OTA ){
160384
- sqlite3ota *pOta = (sqlite3ota*)pArg;
160389
+ if( op==SQLITE_FCNTL_RBU ){
160390
+ sqlite3rbu *pRbu = (sqlite3rbu*)pArg;
160385160391
160386
- /* First try to find another OTA vfs lower down in the vfs stack. If
160392
+ /* First try to find another RBU vfs lower down in the vfs stack. If
160387160393
** one is found, this vfs will operate in pass-through mode. The lower
160388
- ** level vfs will do the special OTA handling. */
160394
+ ** level vfs will do the special RBU handling. */
160389160395
rc = xControl(p->pReal, op, pArg);
160390160396
160391160397
if( rc==SQLITE_NOTFOUND ){
160392160398
/* Now search for a zipvfs instance lower down in the VFS stack. If
160393160399
** one is found, this is an error. */
160394160400
void *dummy = 0;
160395160401
rc = xControl(p->pReal, SQLITE_FCNTL_ZIPVFS, &dummy);
160396160402
if( rc==SQLITE_OK ){
160397160403
rc = SQLITE_ERROR;
160398
- pOta->zErrmsg = sqlite3_mprintf("ota/zipvfs setup error");
160404
+ pRbu->zErrmsg = sqlite3_mprintf("rbu/zipvfs setup error");
160399160405
}else if( rc==SQLITE_NOTFOUND ){
160400
- pOta->pTargetFd = p;
160401
- p->pOta = pOta;
160402
- if( p->pWalFd ) p->pWalFd->pOta = pOta;
160406
+ pRbu->pTargetFd = p;
160407
+ p->pRbu = pRbu;
160408
+ if( p->pWalFd ) p->pWalFd->pRbu = pRbu;
160403160409
rc = SQLITE_OK;
160404160410
}
160405160411
}
160406160412
return rc;
160407160413
}
160408160414
160409160415
rc = xControl(p->pReal, op, pArg);
160410160416
if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){
160411
- ota_vfs *pOtaVfs = p->pOtaVfs;
160417
+ rbu_vfs *pRbuVfs = p->pRbuVfs;
160412160418
char *zIn = *(char**)pArg;
160413
- char *zOut = sqlite3_mprintf("ota(%s)/%z", pOtaVfs->base.zName, zIn);
160419
+ char *zOut = sqlite3_mprintf("rbu(%s)/%z", pRbuVfs->base.zName, zIn);
160414160420
*(char**)pArg = zOut;
160415160421
if( zOut==0 ) rc = SQLITE_NOMEM;
160416160422
}
160417160423
160418160424
return rc;
160419160425
}
160420160426
160421160427
/*
160422
-** Return the sector-size in bytes for an otaVfs-file.
160428
+** Return the sector-size in bytes for an rbuVfs-file.
160423160429
*/
160424
-static int otaVfsSectorSize(sqlite3_file *pFile){
160425
- ota_file *p = (ota_file *)pFile;
160430
+static int rbuVfsSectorSize(sqlite3_file *pFile){
160431
+ rbu_file *p = (rbu_file *)pFile;
160426160432
return p->pReal->pMethods->xSectorSize(p->pReal);
160427160433
}
160428160434
160429160435
/*
160430
-** Return the device characteristic flags supported by an otaVfs-file.
160436
+** Return the device characteristic flags supported by an rbuVfs-file.
160431160437
*/
160432
-static int otaVfsDeviceCharacteristics(sqlite3_file *pFile){
160433
- ota_file *p = (ota_file *)pFile;
160438
+static int rbuVfsDeviceCharacteristics(sqlite3_file *pFile){
160439
+ rbu_file *p = (rbu_file *)pFile;
160434160440
return p->pReal->pMethods->xDeviceCharacteristics(p->pReal);
160435160441
}
160436160442
160437160443
/*
160438160444
** Take or release a shared-memory lock.
160439160445
*/
160440
-static int otaVfsShmLock(sqlite3_file *pFile, int ofst, int n, int flags){
160441
- ota_file *p = (ota_file*)pFile;
160442
- sqlite3ota *pOta = p->pOta;
160446
+static int rbuVfsShmLock(sqlite3_file *pFile, int ofst, int n, int flags){
160447
+ rbu_file *p = (rbu_file*)pFile;
160448
+ sqlite3rbu *pRbu = p->pRbu;
160443160449
int rc = SQLITE_OK;
160444160450
160445160451
#ifdef SQLITE_AMALGAMATION
160446160452
assert( WAL_CKPT_LOCK==1 );
160447160453
#endif
160448160454
160449160455
assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
160450
- if( pOta && (pOta->eStage==OTA_STAGE_OAL || pOta->eStage==OTA_STAGE_MOVE) ){
160456
+ if( pRbu && (pRbu->eStage==RBU_STAGE_OAL || pRbu->eStage==RBU_STAGE_MOVE) ){
160451160457
/* Magic number 1 is the WAL_CKPT_LOCK lock. Preventing SQLite from
160452160458
** taking this lock also prevents any checkpoints from occurring.
160453160459
** todo: really, it's not clear why this might occur, as
160454160460
** wal_autocheckpoint ought to be turned off. */
160455160461
if( ofst==WAL_LOCK_CKPT && n==1 ) rc = SQLITE_BUSY;
160456160462
}else{
160457160463
int bCapture = 0;
160458160464
if( n==1 && (flags & SQLITE_SHM_EXCLUSIVE)
160459
- && pOta && pOta->eStage==OTA_STAGE_CAPTURE
160465
+ && pRbu && pRbu->eStage==RBU_STAGE_CAPTURE
160460160466
&& (ofst==WAL_LOCK_WRITE || ofst==WAL_LOCK_CKPT || ofst==WAL_LOCK_READ0)
160461160467
){
160462160468
bCapture = 1;
160463160469
}
160464160470
160465160471
if( bCapture==0 || 0==(flags & SQLITE_SHM_UNLOCK) ){
160466160472
rc = p->pReal->pMethods->xShmLock(p->pReal, ofst, n, flags);
160467160473
if( bCapture && rc==SQLITE_OK ){
160468
- pOta->mLock |= (1 << ofst);
160474
+ pRbu->mLock |= (1 << ofst);
160469160475
}
160470160476
}
160471160477
}
160472160478
160473160479
return rc;
@@ -160474,26 +160480,26 @@
160474160480
}
160475160481
160476160482
/*
160477160483
** Obtain a pointer to a mapping of a single 32KiB page of the *-shm file.
160478160484
*/
160479
-static int otaVfsShmMap(
160485
+static int rbuVfsShmMap(
160480160486
sqlite3_file *pFile,
160481160487
int iRegion,
160482160488
int szRegion,
160483160489
int isWrite,
160484160490
void volatile **pp
160485160491
){
160486
- ota_file *p = (ota_file*)pFile;
160492
+ rbu_file *p = (rbu_file*)pFile;
160487160493
int rc = SQLITE_OK;
160488
- int eStage = (p->pOta ? p->pOta->eStage : 0);
160494
+ int eStage = (p->pRbu ? p->pRbu->eStage : 0);
160489160495
160490
- /* If not in OTA_STAGE_OAL, allow this call to pass through. Or, if this
160491
- ** ota is in the OTA_STAGE_OAL state, use heap memory for *-shm space
160496
+ /* If not in RBU_STAGE_OAL, allow this call to pass through. Or, if this
160497
+ ** rbu is in the RBU_STAGE_OAL state, use heap memory for *-shm space
160492160498
** instead of a file on disk. */
160493160499
assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
160494
- if( eStage==OTA_STAGE_OAL || eStage==OTA_STAGE_MOVE ){
160500
+ if( eStage==RBU_STAGE_OAL || eStage==RBU_STAGE_MOVE ){
160495160501
if( iRegion<=p->nShm ){
160496160502
int nByte = (iRegion+1) * sizeof(char*);
160497160503
char **apNew = (char**)sqlite3_realloc(p->apShm, nByte);
160498160504
if( apNew==0 ){
160499160505
rc = SQLITE_NOMEM;
@@ -160528,29 +160534,29 @@
160528160534
}
160529160535
160530160536
/*
160531160537
** Memory barrier.
160532160538
*/
160533
-static void otaVfsShmBarrier(sqlite3_file *pFile){
160534
- ota_file *p = (ota_file *)pFile;
160539
+static void rbuVfsShmBarrier(sqlite3_file *pFile){
160540
+ rbu_file *p = (rbu_file *)pFile;
160535160541
p->pReal->pMethods->xShmBarrier(p->pReal);
160536160542
}
160537160543
160538160544
/*
160539160545
** The xShmUnmap method.
160540160546
*/
160541
-static int otaVfsShmUnmap(sqlite3_file *pFile, int delFlag){
160542
- ota_file *p = (ota_file*)pFile;
160547
+static int rbuVfsShmUnmap(sqlite3_file *pFile, int delFlag){
160548
+ rbu_file *p = (rbu_file*)pFile;
160543160549
int rc = SQLITE_OK;
160544
- int eStage = (p->pOta ? p->pOta->eStage : 0);
160550
+ int eStage = (p->pRbu ? p->pRbu->eStage : 0);
160545160551
160546160552
assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
160547
- if( eStage==OTA_STAGE_OAL || eStage==OTA_STAGE_MOVE ){
160553
+ if( eStage==RBU_STAGE_OAL || eStage==RBU_STAGE_MOVE ){
160548160554
/* no-op */
160549160555
}else{
160550160556
/* Release the checkpointer and writer locks */
160551
- otaUnlockShm(p);
160557
+ rbuUnlockShm(p);
160552160558
rc = p->pReal->pMethods->xShmUnmap(p->pReal, delFlag);
160553160559
}
160554160560
return rc;
160555160561
}
160556160562
@@ -160558,56 +160564,56 @@
160558160564
** Given that zWal points to a buffer containing a wal file name passed to
160559160565
** either the xOpen() or xAccess() VFS method, return a pointer to the
160560160566
** file-handle opened by the same database connection on the corresponding
160561160567
** database file.
160562160568
*/
160563
-static ota_file *otaFindMaindb(ota_vfs *pOtaVfs, const char *zWal){
160564
- ota_file *pDb;
160565
- sqlite3_mutex_enter(pOtaVfs->mutex);
160566
- for(pDb=pOtaVfs->pMain; pDb && pDb->zWal!=zWal; pDb=pDb->pMainNext);
160567
- sqlite3_mutex_leave(pOtaVfs->mutex);
160569
+static rbu_file *rbuFindMaindb(rbu_vfs *pRbuVfs, const char *zWal){
160570
+ rbu_file *pDb;
160571
+ sqlite3_mutex_enter(pRbuVfs->mutex);
160572
+ for(pDb=pRbuVfs->pMain; pDb && pDb->zWal!=zWal; pDb=pDb->pMainNext);
160573
+ sqlite3_mutex_leave(pRbuVfs->mutex);
160568160574
return pDb;
160569160575
}
160570160576
160571160577
/*
160572
-** Open an ota file handle.
160578
+** Open an rbu file handle.
160573160579
*/
160574
-static int otaVfsOpen(
160580
+static int rbuVfsOpen(
160575160581
sqlite3_vfs *pVfs,
160576160582
const char *zName,
160577160583
sqlite3_file *pFile,
160578160584
int flags,
160579160585
int *pOutFlags
160580160586
){
160581
- static sqlite3_io_methods otavfs_io_methods = {
160587
+ static sqlite3_io_methods rbuvfs_io_methods = {
160582160588
2, /* iVersion */
160583
- otaVfsClose, /* xClose */
160584
- otaVfsRead, /* xRead */
160585
- otaVfsWrite, /* xWrite */
160586
- otaVfsTruncate, /* xTruncate */
160587
- otaVfsSync, /* xSync */
160588
- otaVfsFileSize, /* xFileSize */
160589
- otaVfsLock, /* xLock */
160590
- otaVfsUnlock, /* xUnlock */
160591
- otaVfsCheckReservedLock, /* xCheckReservedLock */
160592
- otaVfsFileControl, /* xFileControl */
160593
- otaVfsSectorSize, /* xSectorSize */
160594
- otaVfsDeviceCharacteristics, /* xDeviceCharacteristics */
160595
- otaVfsShmMap, /* xShmMap */
160596
- otaVfsShmLock, /* xShmLock */
160597
- otaVfsShmBarrier, /* xShmBarrier */
160598
- otaVfsShmUnmap /* xShmUnmap */
160589
+ rbuVfsClose, /* xClose */
160590
+ rbuVfsRead, /* xRead */
160591
+ rbuVfsWrite, /* xWrite */
160592
+ rbuVfsTruncate, /* xTruncate */
160593
+ rbuVfsSync, /* xSync */
160594
+ rbuVfsFileSize, /* xFileSize */
160595
+ rbuVfsLock, /* xLock */
160596
+ rbuVfsUnlock, /* xUnlock */
160597
+ rbuVfsCheckReservedLock, /* xCheckReservedLock */
160598
+ rbuVfsFileControl, /* xFileControl */
160599
+ rbuVfsSectorSize, /* xSectorSize */
160600
+ rbuVfsDeviceCharacteristics, /* xDeviceCharacteristics */
160601
+ rbuVfsShmMap, /* xShmMap */
160602
+ rbuVfsShmLock, /* xShmLock */
160603
+ rbuVfsShmBarrier, /* xShmBarrier */
160604
+ rbuVfsShmUnmap /* xShmUnmap */
160599160605
};
160600
- ota_vfs *pOtaVfs = (ota_vfs*)pVfs;
160601
- sqlite3_vfs *pRealVfs = pOtaVfs->pRealVfs;
160602
- ota_file *pFd = (ota_file *)pFile;
160606
+ rbu_vfs *pRbuVfs = (rbu_vfs*)pVfs;
160607
+ sqlite3_vfs *pRealVfs = pRbuVfs->pRealVfs;
160608
+ rbu_file *pFd = (rbu_file *)pFile;
160603160609
int rc = SQLITE_OK;
160604160610
const char *zOpen = zName;
160605160611
160606
- memset(pFd, 0, sizeof(ota_file));
160612
+ memset(pFd, 0, sizeof(rbu_file));
160607160613
pFd->pReal = (sqlite3_file*)&pFd[1];
160608
- pFd->pOtaVfs = pOtaVfs;
160614
+ pFd->pRbuVfs = pRbuVfs;
160609160615
pFd->openFlags = flags;
160610160616
if( zName ){
160611160617
if( flags & SQLITE_OPEN_MAIN_DB ){
160612160618
/* A main database has just been opened. The following block sets
160613160619
** (pFd->zWal) to point to a buffer owned by SQLite that contains
@@ -160631,13 +160637,13 @@
160631160637
}
160632160638
z += (n + 8 + 1);
160633160639
pFd->zWal = z;
160634160640
}
160635160641
else if( flags & SQLITE_OPEN_WAL ){
160636
- ota_file *pDb = otaFindMaindb(pOtaVfs, zName);
160642
+ rbu_file *pDb = rbuFindMaindb(pRbuVfs, zName);
160637160643
if( pDb ){
160638
- if( pDb->pOta && pDb->pOta->eStage==OTA_STAGE_OAL ){
160644
+ if( pDb->pRbu && pDb->pRbu->eStage==RBU_STAGE_OAL ){
160639160645
/* This call is to open a *-wal file. Intead, open the *-oal. This
160640160646
** code ensures that the string passed to xOpen() is terminated by a
160641160647
** pair of '\0' bytes in case the VFS attempts to extract a URI
160642160648
** parameter from it. */
160643160649
int nCopy = strlen(zName);
@@ -160649,11 +160655,11 @@
160649160655
zCopy[nCopy+1] = '\0';
160650160656
zOpen = (const char*)(pFd->zDel = zCopy);
160651160657
}else{
160652160658
rc = SQLITE_NOMEM;
160653160659
}
160654
- pFd->pOta = pDb->pOta;
160660
+ pFd->pRbu = pDb->pRbu;
160655160661
}
160656160662
pDb->pWalFd = pFd;
160657160663
}
160658160664
}
160659160665
}
@@ -160663,16 +160669,16 @@
160663160669
}
160664160670
if( pFd->pReal->pMethods ){
160665160671
/* The xOpen() operation has succeeded. Set the sqlite3_file.pMethods
160666160672
** pointer and, if the file is a main database file, link it into the
160667160673
** mutex protected linked list of all such files. */
160668
- pFile->pMethods = &otavfs_io_methods;
160674
+ pFile->pMethods = &rbuvfs_io_methods;
160669160675
if( flags & SQLITE_OPEN_MAIN_DB ){
160670
- sqlite3_mutex_enter(pOtaVfs->mutex);
160671
- pFd->pMainNext = pOtaVfs->pMain;
160672
- pOtaVfs->pMain = pFd;
160673
- sqlite3_mutex_leave(pOtaVfs->mutex);
160676
+ sqlite3_mutex_enter(pRbuVfs->mutex);
160677
+ pFd->pMainNext = pRbuVfs->pMain;
160678
+ pRbuVfs->pMain = pFd;
160679
+ sqlite3_mutex_leave(pRbuVfs->mutex);
160674160680
}
160675160681
}else{
160676160682
sqlite3_free(pFd->zDel);
160677160683
}
160678160684
@@ -160680,48 +160686,48 @@
160680160686
}
160681160687
160682160688
/*
160683160689
** Delete the file located at zPath.
160684160690
*/
160685
-static int otaVfsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
160686
- sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
160691
+static int rbuVfsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
160692
+ sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
160687160693
return pRealVfs->xDelete(pRealVfs, zPath, dirSync);
160688160694
}
160689160695
160690160696
/*
160691160697
** Test for access permissions. Return true if the requested permission
160692160698
** is available, or false otherwise.
160693160699
*/
160694
-static int otaVfsAccess(
160700
+static int rbuVfsAccess(
160695160701
sqlite3_vfs *pVfs,
160696160702
const char *zPath,
160697160703
int flags,
160698160704
int *pResOut
160699160705
){
160700
- ota_vfs *pOtaVfs = (ota_vfs*)pVfs;
160701
- sqlite3_vfs *pRealVfs = pOtaVfs->pRealVfs;
160706
+ rbu_vfs *pRbuVfs = (rbu_vfs*)pVfs;
160707
+ sqlite3_vfs *pRealVfs = pRbuVfs->pRealVfs;
160702160708
int rc;
160703160709
160704160710
rc = pRealVfs->xAccess(pRealVfs, zPath, flags, pResOut);
160705160711
160706
- /* If this call is to check if a *-wal file associated with an OTA target
160707
- ** database connection exists, and the OTA update is in OTA_STAGE_OAL,
160712
+ /* If this call is to check if a *-wal file associated with an RBU target
160713
+ ** database connection exists, and the RBU update is in RBU_STAGE_OAL,
160708160714
** the following special handling is activated:
160709160715
**
160710160716
** a) if the *-wal file does exist, return SQLITE_CANTOPEN. This
160711
- ** ensures that the OTA extension never tries to update a database
160717
+ ** ensures that the RBU extension never tries to update a database
160712160718
** in wal mode, even if the first page of the database file has
160713160719
** been damaged.
160714160720
**
160715160721
** b) if the *-wal file does not exist, claim that it does anyway,
160716160722
** causing SQLite to call xOpen() to open it. This call will also
160717
- ** be intercepted (see the otaVfsOpen() function) and the *-oal
160723
+ ** be intercepted (see the rbuVfsOpen() function) and the *-oal
160718160724
** file opened instead.
160719160725
*/
160720160726
if( rc==SQLITE_OK && flags==SQLITE_ACCESS_EXISTS ){
160721
- ota_file *pDb = otaFindMaindb(pOtaVfs, zPath);
160722
- if( pDb && pDb->pOta && pDb->pOta->eStage==OTA_STAGE_OAL ){
160727
+ rbu_file *pDb = rbuFindMaindb(pRbuVfs, zPath);
160728
+ if( pDb && pDb->pRbu && pDb->pRbu->eStage==RBU_STAGE_OAL ){
160723160729
if( *pResOut ){
160724160730
rc = SQLITE_CANTOPEN;
160725160731
}else{
160726160732
*pResOut = 1;
160727160733
}
@@ -160734,151 +160740,151 @@
160734160740
/*
160735160741
** Populate buffer zOut with the full canonical pathname corresponding
160736160742
** to the pathname in zPath. zOut is guaranteed to point to a buffer
160737160743
** of at least (DEVSYM_MAX_PATHNAME+1) bytes.
160738160744
*/
160739
-static int otaVfsFullPathname(
160745
+static int rbuVfsFullPathname(
160740160746
sqlite3_vfs *pVfs,
160741160747
const char *zPath,
160742160748
int nOut,
160743160749
char *zOut
160744160750
){
160745
- sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
160751
+ sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
160746160752
return pRealVfs->xFullPathname(pRealVfs, zPath, nOut, zOut);
160747160753
}
160748160754
160749160755
#ifndef SQLITE_OMIT_LOAD_EXTENSION
160750160756
/*
160751160757
** Open the dynamic library located at zPath and return a handle.
160752160758
*/
160753
-static void *otaVfsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
160754
- sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
160759
+static void *rbuVfsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
160760
+ sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
160755160761
return pRealVfs->xDlOpen(pRealVfs, zPath);
160756160762
}
160757160763
160758160764
/*
160759160765
** Populate the buffer zErrMsg (size nByte bytes) with a human readable
160760160766
** utf-8 string describing the most recent error encountered associated
160761160767
** with dynamic libraries.
160762160768
*/
160763
-static void otaVfsDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
160764
- sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
160769
+static void rbuVfsDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
160770
+ sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
160765160771
pRealVfs->xDlError(pRealVfs, nByte, zErrMsg);
160766160772
}
160767160773
160768160774
/*
160769160775
** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
160770160776
*/
160771
-static void (*otaVfsDlSym(
160777
+static void (*rbuVfsDlSym(
160772160778
sqlite3_vfs *pVfs,
160773160779
void *pArg,
160774160780
const char *zSym
160775160781
))(void){
160776
- sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
160782
+ sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
160777160783
return pRealVfs->xDlSym(pRealVfs, pArg, zSym);
160778160784
}
160779160785
160780160786
/*
160781160787
** Close the dynamic library handle pHandle.
160782160788
*/
160783
-static void otaVfsDlClose(sqlite3_vfs *pVfs, void *pHandle){
160784
- sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
160789
+static void rbuVfsDlClose(sqlite3_vfs *pVfs, void *pHandle){
160790
+ sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
160785160791
return pRealVfs->xDlClose(pRealVfs, pHandle);
160786160792
}
160787160793
#endif /* SQLITE_OMIT_LOAD_EXTENSION */
160788160794
160789160795
/*
160790160796
** Populate the buffer pointed to by zBufOut with nByte bytes of
160791160797
** random data.
160792160798
*/
160793
-static int otaVfsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
160794
- sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
160799
+static int rbuVfsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
160800
+ sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
160795160801
return pRealVfs->xRandomness(pRealVfs, nByte, zBufOut);
160796160802
}
160797160803
160798160804
/*
160799160805
** Sleep for nMicro microseconds. Return the number of microseconds
160800160806
** actually slept.
160801160807
*/
160802
-static int otaVfsSleep(sqlite3_vfs *pVfs, int nMicro){
160803
- sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
160808
+static int rbuVfsSleep(sqlite3_vfs *pVfs, int nMicro){
160809
+ sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
160804160810
return pRealVfs->xSleep(pRealVfs, nMicro);
160805160811
}
160806160812
160807160813
/*
160808160814
** Return the current time as a Julian Day number in *pTimeOut.
160809160815
*/
160810
-static int otaVfsCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
160811
- sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
160816
+static int rbuVfsCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
160817
+ sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
160812160818
return pRealVfs->xCurrentTime(pRealVfs, pTimeOut);
160813160819
}
160814160820
160815160821
/*
160816160822
** No-op.
160817160823
*/
160818
-static int otaVfsGetLastError(sqlite3_vfs *pVfs, int a, char *b){
160824
+static int rbuVfsGetLastError(sqlite3_vfs *pVfs, int a, char *b){
160819160825
return 0;
160820160826
}
160821160827
160822160828
/*
160823
-** Deregister and destroy an OTA vfs created by an earlier call to
160824
-** sqlite3ota_create_vfs().
160829
+** Deregister and destroy an RBU vfs created by an earlier call to
160830
+** sqlite3rbu_create_vfs().
160825160831
*/
160826
-SQLITE_API void SQLITE_STDCALL sqlite3ota_destroy_vfs(const char *zName){
160832
+SQLITE_API void SQLITE_STDCALL sqlite3rbu_destroy_vfs(const char *zName){
160827160833
sqlite3_vfs *pVfs = sqlite3_vfs_find(zName);
160828
- if( pVfs && pVfs->xOpen==otaVfsOpen ){
160829
- sqlite3_mutex_free(((ota_vfs*)pVfs)->mutex);
160834
+ if( pVfs && pVfs->xOpen==rbuVfsOpen ){
160835
+ sqlite3_mutex_free(((rbu_vfs*)pVfs)->mutex);
160830160836
sqlite3_vfs_unregister(pVfs);
160831160837
sqlite3_free(pVfs);
160832160838
}
160833160839
}
160834160840
160835160841
/*
160836
-** Create an OTA VFS named zName that accesses the underlying file-system
160842
+** Create an RBU VFS named zName that accesses the underlying file-system
160837160843
** via existing VFS zParent. The new object is registered as a non-default
160838160844
** VFS with SQLite before returning.
160839160845
*/
160840
-SQLITE_API int SQLITE_STDCALL sqlite3ota_create_vfs(const char *zName, const char *zParent){
160846
+SQLITE_API int SQLITE_STDCALL sqlite3rbu_create_vfs(const char *zName, const char *zParent){
160841160847
160842160848
/* Template for VFS */
160843160849
static sqlite3_vfs vfs_template = {
160844160850
1, /* iVersion */
160845160851
0, /* szOsFile */
160846160852
0, /* mxPathname */
160847160853
0, /* pNext */
160848160854
0, /* zName */
160849160855
0, /* pAppData */
160850
- otaVfsOpen, /* xOpen */
160851
- otaVfsDelete, /* xDelete */
160852
- otaVfsAccess, /* xAccess */
160853
- otaVfsFullPathname, /* xFullPathname */
160856
+ rbuVfsOpen, /* xOpen */
160857
+ rbuVfsDelete, /* xDelete */
160858
+ rbuVfsAccess, /* xAccess */
160859
+ rbuVfsFullPathname, /* xFullPathname */
160854160860
160855160861
#ifndef SQLITE_OMIT_LOAD_EXTENSION
160856
- otaVfsDlOpen, /* xDlOpen */
160857
- otaVfsDlError, /* xDlError */
160858
- otaVfsDlSym, /* xDlSym */
160859
- otaVfsDlClose, /* xDlClose */
160862
+ rbuVfsDlOpen, /* xDlOpen */
160863
+ rbuVfsDlError, /* xDlError */
160864
+ rbuVfsDlSym, /* xDlSym */
160865
+ rbuVfsDlClose, /* xDlClose */
160860160866
#else
160861160867
0, 0, 0, 0,
160862160868
#endif
160863160869
160864
- otaVfsRandomness, /* xRandomness */
160865
- otaVfsSleep, /* xSleep */
160866
- otaVfsCurrentTime, /* xCurrentTime */
160867
- otaVfsGetLastError, /* xGetLastError */
160870
+ rbuVfsRandomness, /* xRandomness */
160871
+ rbuVfsSleep, /* xSleep */
160872
+ rbuVfsCurrentTime, /* xCurrentTime */
160873
+ rbuVfsGetLastError, /* xGetLastError */
160868160874
0, /* xCurrentTimeInt64 (version 2) */
160869160875
0, 0, 0 /* Unimplemented version 3 methods */
160870160876
};
160871160877
160872
- ota_vfs *pNew = 0; /* Newly allocated VFS */
160878
+ rbu_vfs *pNew = 0; /* Newly allocated VFS */
160873160879
int nName;
160874160880
int rc = SQLITE_OK;
160875160881
160876160882
int nByte;
160877160883
nName = strlen(zName);
160878
- nByte = sizeof(ota_vfs) + nName + 1;
160879
- pNew = (ota_vfs*)sqlite3_malloc(nByte);
160884
+ nByte = sizeof(rbu_vfs) + nName + 1;
160885
+ pNew = (rbu_vfs*)sqlite3_malloc(nByte);
160880160886
if( pNew==0 ){
160881160887
rc = SQLITE_NOMEM;
160882160888
}else{
160883160889
sqlite3_vfs *pParent; /* Parent VFS */
160884160890
memset(pNew, 0, nByte);
@@ -160887,11 +160893,11 @@
160887160893
rc = SQLITE_NOTFOUND;
160888160894
}else{
160889160895
char *zSpace;
160890160896
memcpy(&pNew->base, &vfs_template, sizeof(sqlite3_vfs));
160891160897
pNew->base.mxPathname = pParent->mxPathname;
160892
- pNew->base.szOsFile = sizeof(ota_file) + pParent->szOsFile;
160898
+ pNew->base.szOsFile = sizeof(rbu_file) + pParent->szOsFile;
160893160899
pNew->pRealVfs = pParent;
160894160900
pNew->base.zName = (const char*)(zSpace = (char*)&pNew[1]);
160895160901
memcpy(zSpace, zName, nName);
160896160902
160897160903
/* Allocate the mutex and register the new VFS (not as the default) */
@@ -160913,13 +160919,13 @@
160913160919
}
160914160920
160915160921
160916160922
/**************************************************************************/
160917160923
160918
-#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_OTA) */
160924
+#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RBU) */
160919160925
160920
-/************** End of sqlite3ota.c ******************************************/
160926
+/************** End of sqlite3rbu.c ******************************************/
160921160927
/************** Begin file dbstat.c ******************************************/
160922160928
/*
160923160929
** 2010 July 12
160924160930
**
160925160931
** The author disclaims copyright to this source code. In place of
160926160932
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -325,11 +325,11 @@
325 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
326 ** [sqlite_version()] and [sqlite_source_id()].
327 */
328 #define SQLITE_VERSION "3.8.11"
329 #define SQLITE_VERSION_NUMBER 3008011
330 #define SQLITE_SOURCE_ID "2015-07-16 18:18:19 b79a4affe44bd0c8e155cae19f3f62c715684cd6"
331
332 /*
333 ** CAPI3REF: Run-Time Library Version Numbers
334 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
335 **
@@ -1179,13 +1179,13 @@
1179 **
1180 ** <li>[[SQLITE_FCNTL_ZIPVFS]]
1181 ** The [SQLITE_FCNTL_ZIPVFS] opcode is implemented by zipvfs only. All other
1182 ** VFS should return SQLITE_NOTFOUND for this opcode.
1183 **
1184 ** <li>[[SQLITE_FCNTL_OTA]]
1185 ** The [SQLITE_FCNTL_OTA] opcode is implemented by the special VFS used by
1186 ** the OTA extension only. All other VFS should return SQLITE_NOTFOUND for
1187 ** this opcode.
1188 ** </ul>
1189 */
1190 #define SQLITE_FCNTL_LOCKSTATE 1
1191 #define SQLITE_FCNTL_GET_LOCKPROXYFILE 2
@@ -1209,11 +1209,11 @@
1209 #define SQLITE_FCNTL_SYNC 21
1210 #define SQLITE_FCNTL_COMMIT_PHASETWO 22
1211 #define SQLITE_FCNTL_WIN32_SET_HANDLE 23
1212 #define SQLITE_FCNTL_WAL_BLOCK 24
1213 #define SQLITE_FCNTL_ZIPVFS 25
1214 #define SQLITE_FCNTL_OTA 26
1215
1216 /* deprecated names */
1217 #define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE
1218 #define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE
1219 #define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO
@@ -40711,22 +40711,22 @@
40711 /*
40712 ** Try to initialize the pCache->pFree and pCache->pBulk fields. Return
40713 ** true if pCache->pFree ends up containing one or more free pages.
40714 */
40715 static int pcache1InitBulk(PCache1 *pCache){
40716 int szBulk;
40717 char *zBulk;
40718 if( pcache1.nInitPage==0 ) return 0;
40719 /* Do not bother with a bulk allocation if the cache size very small */
40720 if( pCache->nMax<3 ) return 0;
40721 sqlite3BeginBenignMalloc();
40722 if( pcache1.nInitPage>0 ){
40723 szBulk = pCache->szAlloc * pcache1.nInitPage;
40724 }else{
40725 szBulk = -1024*pcache1.nInitPage;
40726 }
40727 if( szBulk > pCache->szAlloc*pCache->nMax ){
40728 szBulk = pCache->szAlloc*pCache->nMax;
40729 }
40730 zBulk = pCache->pBulk = sqlite3Malloc( szBulk );
40731 sqlite3EndBenignMalloc();
40732 if( zBulk ){
@@ -50252,13 +50252,13 @@
50252
50253 assert( pWal->writeLock );
50254 pWal->hdr.isInit = 1;
50255 pWal->hdr.iVersion = WALINDEX_MAX_VERSION;
50256 walChecksumBytes(1, (u8*)&pWal->hdr, nCksum, 0, pWal->hdr.aCksum);
50257 memcpy((void *)&aHdr[1], (void *)&pWal->hdr, sizeof(WalIndexHdr));
50258 walShmBarrier(pWal);
50259 memcpy((void *)&aHdr[0], (void *)&pWal->hdr, sizeof(WalIndexHdr));
50260 }
50261
50262 /*
50263 ** This function encodes a single frame header and writes it to a buffer
50264 ** supplied by the caller. A frame-header is made up of a series of
@@ -50556,17 +50556,17 @@
50556 #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
50557 /* Verify that the every entry in the mapping region is still reachable
50558 ** via the hash table even after the cleanup.
50559 */
50560 if( iLimit ){
50561 int i; /* Loop counter */
50562 int iKey; /* Hash key */
50563 for(i=1; i<=iLimit; i++){
50564 for(iKey=walHash(aPgno[i]); aHash[iKey]; iKey=walNextHash(iKey)){
50565 if( aHash[iKey]==i ) break;
50566 }
50567 assert( aHash[iKey]==i );
50568 }
50569 }
50570 #endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
50571 }
50572
@@ -118653,11 +118653,15 @@
118653
118654 /* Read the PK into an array of temp registers. */
118655 r = sqlite3GetTempRange(pParse, nPk);
118656 for(iPk=0; iPk<nPk; iPk++){
118657 int iCol = pPk->aiColumn[iPk];
118658 sqlite3ExprCodeGetColumn(pParse, pTab, iCol, iCur, r+iPk, 0);
 
 
 
 
118659 }
118660
118661 /* Check if the temp table already contains this key. If so,
118662 ** the row has already been included in the result set and
118663 ** can be ignored (by jumping past the Gosub below). Otherwise,
@@ -130036,10 +130040,11 @@
130036 ** space for the lookaside memory is obtained from sqlite3_malloc().
130037 ** If pStart is not NULL then it is sz*cnt bytes of memory to use for
130038 ** the lookaside memory.
130039 */
130040 static int setupLookaside(sqlite3 *db, void *pBuf, int sz, int cnt){
 
130041 void *pStart;
130042 if( db->lookaside.nOut ){
130043 return SQLITE_BUSY;
130044 }
130045 /* Free any existing lookaside buffer for this handle before
@@ -130086,10 +130091,11 @@
130086 db->lookaside.pStart = db;
130087 db->lookaside.pEnd = db;
130088 db->lookaside.bEnabled = 0;
130089 db->lookaside.bMalloced = 0;
130090 }
 
130091 return SQLITE_OK;
130092 }
130093
130094 /*
130095 ** Return the mutex associated with a database connection.
@@ -156830,11 +156836,11 @@
156830
156831 #endif /* defined(SQLITE_ENABLE_ICU) */
156832 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
156833
156834 /************** End of fts3_icu.c ********************************************/
156835 /************** Begin file sqlite3ota.c **************************************/
156836 /*
156837 ** 2014 August 30
156838 **
156839 ** The author disclaims copyright to this source code. In place of
156840 ** a legal notice, here is a blessing:
@@ -156846,15 +156852,15 @@
156846 *************************************************************************
156847 **
156848 **
156849 ** OVERVIEW
156850 **
156851 ** The OTA extension requires that the OTA update be packaged as an
156852 ** SQLite database. The tables it expects to find are described in
156853 ** sqlite3ota.h. Essentially, for each table xyz in the target database
156854 ** that the user wishes to write to, a corresponding data_xyz table is
156855 ** created in the OTA database and populated with one row for each row to
156856 ** update, insert or delete from the target table.
156857 **
156858 ** The update proceeds in three stages:
156859 **
156860 ** 1) The database is updated. The modified database pages are written
@@ -156862,57 +156868,57 @@
156862 ** that it is named "<database>-oal" instead of "<database>-wal".
156863 ** Because regular SQLite clients do not look for file named
156864 ** "<database>-oal", they go on using the original database in
156865 ** rollback mode while the *-oal file is being generated.
156866 **
156867 ** During this stage OTA does not update the database by writing
156868 ** directly to the target tables. Instead it creates "imposter"
156869 ** tables using the SQLITE_TESTCTRL_IMPOSTER interface that it uses
156870 ** to update each b-tree individually. All updates required by each
156871 ** b-tree are completed before moving on to the next, and all
156872 ** updates are done in sorted key order.
156873 **
156874 ** 2) The "<database>-oal" file is moved to the equivalent "<database>-wal"
156875 ** location using a call to rename(2). Before doing this the OTA
156876 ** module takes an EXCLUSIVE lock on the database file, ensuring
156877 ** that there are no other active readers.
156878 **
156879 ** Once the EXCLUSIVE lock is released, any other database readers
156880 ** detect the new *-wal file and read the database in wal mode. At
156881 ** this point they see the new version of the database - including
156882 ** the updates made as part of the OTA update.
156883 **
156884 ** 3) The new *-wal file is checkpointed. This proceeds in the same way
156885 ** as a regular database checkpoint, except that a single frame is
156886 ** checkpointed each time sqlite3ota_step() is called. If the OTA
156887 ** handle is closed before the entire *-wal file is checkpointed,
156888 ** the checkpoint progress is saved in the OTA database and the
156889 ** checkpoint can be resumed by another OTA client at some point in
156890 ** the future.
156891 **
156892 ** POTENTIAL PROBLEMS
156893 **
156894 ** The rename() call might not be portable. And OTA is not currently
156895 ** syncing the directory after renaming the file.
156896 **
156897 ** When state is saved, any commit to the *-oal file and the commit to
156898 ** the OTA update database are not atomic. So if the power fails at the
156899 ** wrong moment they might get out of sync. As the main database will be
156900 ** committed before the OTA update database this will likely either just
156901 ** pass unnoticed, or result in SQLITE_CONSTRAINT errors (due to UNIQUE
156902 ** constraint violations).
156903 **
156904 ** If some client does modify the target database mid OTA update, or some
156905 ** other error occurs, the OTA extension will keep throwing errors. It's
156906 ** not really clear how to get out of this state. The system could just
156907 ** by delete the OTA update database and *-oal file and have the device
156908 ** download the update again and start over.
156909 **
156910 ** At present, for an UPDATE, both the new.* and old.* records are
156911 ** collected in the ota_xyz table. And for both UPDATEs and DELETEs all
156912 ** fields are collected. This means we're probably writing a lot more
156913 ** data to disk when saving the state of an ongoing update to the OTA
156914 ** update database than is strictly necessary.
156915 **
156916 */
156917
156918 /* #include <assert.h> */
@@ -156920,13 +156926,13 @@
156920 /* #include <stdio.h> */
156921 /* #include <unistd.h> */
156922
156923 /* #include "sqlite3.h" */
156924
156925 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_OTA)
156926 /************** Include sqlite3ota.h in the middle of sqlite3ota.c ***********/
156927 /************** Begin file sqlite3ota.h **************************************/
156928 /*
156929 ** 2014 August 30
156930 **
156931 ** The author disclaims copyright to this source code. In place of
156932 ** a legal notice, here is a blessing:
@@ -156935,11 +156941,11 @@
156935 ** May you find forgiveness for yourself and forgive others.
156936 ** May you share freely, never taking more than you give.
156937 **
156938 *************************************************************************
156939 **
156940 ** This file contains the public interface for the OTA extension.
156941 */
156942
156943 /*
156944 ** SUMMARY
156945 **
@@ -156972,18 +156978,18 @@
156972 ** mobile device that is frequently rebooted. Even after the writer process
156973 ** has committed one or more sub-transactions, other database clients continue
156974 ** to read from the original database snapshot. In other words, partially
156975 ** applied transactions are not visible to other clients.
156976 **
156977 ** "OTA" stands for "Over The Air" update. As in a large database update
156978 ** transmitted via a wireless network to a mobile device. A transaction
156979 ** applied using this extension is hence refered to as an "OTA update".
156980 **
156981 **
156982 ** LIMITATIONS
156983 **
156984 ** An "OTA update" transaction is subject to the following limitations:
156985 **
156986 ** * The transaction must consist of INSERT, UPDATE and DELETE operations
156987 ** only.
156988 **
156989 ** * INSERT statements may not use any default values.
@@ -157004,430 +157010,430 @@
157004 ** * No constraint handling mode except for "OR ROLLBACK" is supported.
157005 **
157006 **
157007 ** PREPARATION
157008 **
157009 ** An "OTA update" is stored as a separate SQLite database. A database
157010 ** containing an OTA update is an "OTA database". For each table in the
157011 ** target database to be updated, the OTA database should contain a table
157012 ** named "data_<target name>" containing the same set of columns as the
157013 ** target table, and one more - "ota_control". The data_% table should
157014 ** have no PRIMARY KEY or UNIQUE constraints, but each column should have
157015 ** the same type as the corresponding column in the target database.
157016 ** The "ota_control" column should have no type at all. For example, if
157017 ** the target database contains:
157018 **
157019 ** CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT, c UNIQUE);
157020 **
157021 ** Then the OTA database should contain:
157022 **
157023 ** CREATE TABLE data_t1(a INTEGER, b TEXT, c, ota_control);
157024 **
157025 ** The order of the columns in the data_% table does not matter.
157026 **
157027 ** If the target database table is a virtual table or a table that has no
157028 ** PRIMARY KEY declaration, the data_% table must also contain a column
157029 ** named "ota_rowid". This column is mapped to the tables implicit primary
157030 ** key column - "rowid". Virtual tables for which the "rowid" column does
157031 ** not function like a primary key value cannot be updated using OTA. For
157032 ** example, if the target db contains either of the following:
157033 **
157034 ** CREATE VIRTUAL TABLE x1 USING fts3(a, b);
157035 ** CREATE TABLE x1(a, b)
157036 **
157037 ** then the OTA database should contain:
157038 **
157039 ** CREATE TABLE data_x1(a, b, ota_rowid, ota_control);
157040 **
157041 ** All non-hidden columns (i.e. all columns matched by "SELECT *") of the
157042 ** target table must be present in the input table. For virtual tables,
157043 ** hidden columns are optional - they are updated by OTA if present in
157044 ** the input table, or not otherwise. For example, to write to an fts4
157045 ** table with a hidden languageid column such as:
157046 **
157047 ** CREATE VIRTUAL TABLE ft1 USING fts4(a, b, languageid='langid');
157048 **
157049 ** Either of the following input table schemas may be used:
157050 **
157051 ** CREATE TABLE data_ft1(a, b, langid, ota_rowid, ota_control);
157052 ** CREATE TABLE data_ft1(a, b, ota_rowid, ota_control);
157053 **
157054 ** For each row to INSERT into the target database as part of the OTA
157055 ** update, the corresponding data_% table should contain a single record
157056 ** with the "ota_control" column set to contain integer value 0. The
157057 ** other columns should be set to the values that make up the new record
157058 ** to insert.
157059 **
157060 ** If the target database table has an INTEGER PRIMARY KEY, it is not
157061 ** possible to insert a NULL value into the IPK column. Attempting to
157062 ** do so results in an SQLITE_MISMATCH error.
157063 **
157064 ** For each row to DELETE from the target database as part of the OTA
157065 ** update, the corresponding data_% table should contain a single record
157066 ** with the "ota_control" column set to contain integer value 1. The
157067 ** real primary key values of the row to delete should be stored in the
157068 ** corresponding columns of the data_% table. The values stored in the
157069 ** other columns are not used.
157070 **
157071 ** For each row to UPDATE from the target database as part of the OTA
157072 ** update, the corresponding data_% table should contain a single record
157073 ** with the "ota_control" column set to contain a value of type text.
157074 ** The real primary key values identifying the row to update should be
157075 ** stored in the corresponding columns of the data_% table row, as should
157076 ** the new values of all columns being update. The text value in the
157077 ** "ota_control" column must contain the same number of characters as
157078 ** there are columns in the target database table, and must consist entirely
157079 ** of 'x' and '.' characters (or in some special cases 'd' - see below). For
157080 ** each column that is being updated, the corresponding character is set to
157081 ** 'x'. For those that remain as they are, the corresponding character of the
157082 ** ota_control value should be set to '.'. For example, given the tables
157083 ** above, the update statement:
157084 **
157085 ** UPDATE t1 SET c = 'usa' WHERE a = 4;
157086 **
157087 ** is represented by the data_t1 row created by:
157088 **
157089 ** INSERT INTO data_t1(a, b, c, ota_control) VALUES(4, NULL, 'usa', '..x');
157090 **
157091 ** Instead of an 'x' character, characters of the ota_control value specified
157092 ** for UPDATEs may also be set to 'd'. In this case, instead of updating the
157093 ** target table with the value stored in the corresponding data_% column, the
157094 ** user-defined SQL function "ota_delta()" is invoked and the result stored in
157095 ** the target table column. ota_delta() is invoked with two arguments - the
157096 ** original value currently stored in the target table column and the
157097 ** value specified in the data_xxx table.
157098 **
157099 ** For example, this row:
157100 **
157101 ** INSERT INTO data_t1(a, b, c, ota_control) VALUES(4, NULL, 'usa', '..d');
157102 **
157103 ** is similar to an UPDATE statement such as:
157104 **
157105 ** UPDATE t1 SET c = ota_delta(c, 'usa') WHERE a = 4;
157106 **
157107 ** If the target database table is a virtual table or a table with no PRIMARY
157108 ** KEY, the ota_control value should not include a character corresponding
157109 ** to the ota_rowid value. For example, this:
157110 **
157111 ** INSERT INTO data_ft1(a, b, ota_rowid, ota_control)
157112 ** VALUES(NULL, 'usa', 12, '.x');
157113 **
157114 ** causes a result similar to:
157115 **
157116 ** UPDATE ft1 SET b = 'usa' WHERE rowid = 12;
157117 **
157118 ** The data_xxx tables themselves should have no PRIMARY KEY declarations.
157119 ** However, OTA is more efficient if reading the rows in from each data_xxx
157120 ** table in "rowid" order is roughly the same as reading them sorted by
157121 ** the PRIMARY KEY of the corresponding target database table. In other
157122 ** words, rows should be sorted using the destination table PRIMARY KEY
157123 ** fields before they are inserted into the data_xxx tables.
157124 **
157125 ** USAGE
157126 **
157127 ** The API declared below allows an application to apply an OTA update
157128 ** stored on disk to an existing target database. Essentially, the
157129 ** application:
157130 **
157131 ** 1) Opens an OTA handle using the sqlite3ota_open() function.
157132 **
157133 ** 2) Registers any required virtual table modules with the database
157134 ** handle returned by sqlite3ota_db(). Also, if required, register
157135 ** the ota_delta() implementation.
157136 **
157137 ** 3) Calls the sqlite3ota_step() function one or more times on
157138 ** the new handle. Each call to sqlite3ota_step() performs a single
157139 ** b-tree operation, so thousands of calls may be required to apply
157140 ** a complete update.
157141 **
157142 ** 4) Calls sqlite3ota_close() to close the OTA update handle. If
157143 ** sqlite3ota_step() has been called enough times to completely
157144 ** apply the update to the target database, then the OTA database
157145 ** is marked as fully applied. Otherwise, the state of the OTA
157146 ** update application is saved in the OTA database for later
157147 ** resumption.
157148 **
157149 ** See comments below for more detail on APIs.
157150 **
157151 ** If an update is only partially applied to the target database by the
157152 ** time sqlite3ota_close() is called, various state information is saved
157153 ** within the OTA database. This allows subsequent processes to automatically
157154 ** resume the OTA update from where it left off.
157155 **
157156 ** To remove all OTA extension state information, returning an OTA database
157157 ** to its original contents, it is sufficient to drop all tables that begin
157158 ** with the prefix "ota_"
157159 **
157160 ** DATABASE LOCKING
157161 **
157162 ** An OTA update may not be applied to a database in WAL mode. Attempting
157163 ** to do so is an error (SQLITE_ERROR).
157164 **
157165 ** While an OTA handle is open, a SHARED lock may be held on the target
157166 ** database file. This means it is possible for other clients to read the
157167 ** database, but not to write it.
157168 **
157169 ** If an OTA update is started and then suspended before it is completed,
157170 ** then an external client writes to the database, then attempting to resume
157171 ** the suspended OTA update is also an error (SQLITE_BUSY).
157172 */
157173
157174 #ifndef _SQLITE3OTA_H
157175 #define _SQLITE3OTA_H
157176
157177 /* #include "sqlite3.h" ** Required for error code definitions ** */
157178
157179 typedef struct sqlite3ota sqlite3ota;
157180
157181 /*
157182 ** Open an OTA handle.
157183 **
157184 ** Argument zTarget is the path to the target database. Argument zOta is
157185 ** the path to the OTA database. Each call to this function must be matched
157186 ** by a call to sqlite3ota_close(). When opening the databases, OTA passes
157187 ** the SQLITE_CONFIG_URI flag to sqlite3_open_v2(). So if either zTarget
157188 ** or zOta begin with "file:", it will be interpreted as an SQLite
157189 ** database URI, not a regular file name.
157190 **
157191 ** If the zState argument is passed a NULL value, the OTA extension stores
157192 ** the current state of the update (how many rows have been updated, which
157193 ** indexes are yet to be updated etc.) within the OTA database itself. This
157194 ** can be convenient, as it means that the OTA application does not need to
157195 ** organize removing a separate state file after the update is concluded.
157196 ** Or, if zState is non-NULL, it must be a path to a database file in which
157197 ** the OTA extension can store the state of the update.
157198 **
157199 ** When resuming an OTA update, the zState argument must be passed the same
157200 ** value as when the OTA update was started.
157201 **
157202 ** Once the OTA update is finished, the OTA extension does not
157203 ** automatically remove any zState database file, even if it created it.
157204 **
157205 ** By default, OTA uses the default VFS to access the files on disk. To
157206 ** use a VFS other than the default, an SQLite "file:" URI containing a
157207 ** "vfs=..." option may be passed as the zTarget option.
157208 **
157209 ** IMPORTANT NOTE FOR ZIPVFS USERS: The OTA extension works with all of
157210 ** SQLite's built-in VFSs, including the multiplexor VFS. However it does
157211 ** not work out of the box with zipvfs. Refer to the comment describing
157212 ** the zipvfs_create_vfs() API below for details on using OTA with zipvfs.
157213 */
157214 SQLITE_API sqlite3ota *SQLITE_STDCALL sqlite3ota_open(
157215 const char *zTarget,
157216 const char *zOta,
157217 const char *zState
157218 );
157219
157220 /*
157221 ** Internally, each OTA connection uses a separate SQLite database
157222 ** connection to access the target and ota update databases. This
157223 ** API allows the application direct access to these database handles.
157224 **
157225 ** The first argument passed to this function must be a valid, open, OTA
157226 ** handle. The second argument should be passed zero to access the target
157227 ** database handle, or non-zero to access the ota update database handle.
157228 ** Accessing the underlying database handles may be useful in the
157229 ** following scenarios:
157230 **
157231 ** * If any target tables are virtual tables, it may be necessary to
157232 ** call sqlite3_create_module() on the target database handle to
157233 ** register the required virtual table implementations.
157234 **
157235 ** * If the data_xxx tables in the OTA source database are virtual
157236 ** tables, the application may need to call sqlite3_create_module() on
157237 ** the ota update db handle to any required virtual table
157238 ** implementations.
157239 **
157240 ** * If the application uses the "ota_delta()" feature described above,
157241 ** it must use sqlite3_create_function() or similar to register the
157242 ** ota_delta() implementation with the target database handle.
157243 **
157244 ** If an error has occurred, either while opening or stepping the OTA object,
157245 ** this function may return NULL. The error code and message may be collected
157246 ** when sqlite3ota_close() is called.
157247 */
157248 SQLITE_API sqlite3 *SQLITE_STDCALL sqlite3ota_db(sqlite3ota*, int bOta);
157249
157250 /*
157251 ** Do some work towards applying the OTA update to the target db.
157252 **
157253 ** Return SQLITE_DONE if the update has been completely applied, or
157254 ** SQLITE_OK if no error occurs but there remains work to do to apply
157255 ** the OTA update. If an error does occur, some other error code is
157256 ** returned.
157257 **
157258 ** Once a call to sqlite3ota_step() has returned a value other than
157259 ** SQLITE_OK, all subsequent calls on the same OTA handle are no-ops
157260 ** that immediately return the same value.
157261 */
157262 SQLITE_API int SQLITE_STDCALL sqlite3ota_step(sqlite3ota *pOta);
157263
157264 /*
157265 ** Close an OTA handle.
157266 **
157267 ** If the OTA update has been completely applied, mark the OTA database
157268 ** as fully applied. Otherwise, assuming no error has occurred, save the
157269 ** current state of the OTA update appliation to the OTA database.
157270 **
157271 ** If an error has already occurred as part of an sqlite3ota_step()
157272 ** or sqlite3ota_open() call, or if one occurs within this function, an
157273 ** SQLite error code is returned. Additionally, *pzErrmsg may be set to
157274 ** point to a buffer containing a utf-8 formatted English language error
157275 ** message. It is the responsibility of the caller to eventually free any
157276 ** such buffer using sqlite3_free().
157277 **
157278 ** Otherwise, if no error occurs, this function returns SQLITE_OK if the
157279 ** update has been partially applied, or SQLITE_DONE if it has been
157280 ** completely applied.
157281 */
157282 SQLITE_API int SQLITE_STDCALL sqlite3ota_close(sqlite3ota *pOta, char **pzErrmsg);
157283
157284 /*
157285 ** Return the total number of key-value operations (inserts, deletes or
157286 ** updates) that have been performed on the target database since the
157287 ** current OTA update was started.
157288 */
157289 SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3ota_progress(sqlite3ota *pOta);
157290
157291 /*
157292 ** Create an OTA VFS named zName that accesses the underlying file-system
157293 ** via existing VFS zParent. Or, if the zParent parameter is passed NULL,
157294 ** then the new OTA VFS uses the default system VFS to access the file-system.
157295 ** The new object is registered as a non-default VFS with SQLite before
157296 ** returning.
157297 **
157298 ** Part of the OTA implementation uses a custom VFS object. Usually, this
157299 ** object is created and deleted automatically by OTA.
157300 **
157301 ** The exception is for applications that also use zipvfs. In this case,
157302 ** the custom VFS must be explicitly created by the user before the OTA
157303 ** handle is opened. The OTA VFS should be installed so that the zipvfs
157304 ** VFS uses the OTA VFS, which in turn uses any other VFS layers in use
157305 ** (for example multiplexor) to access the file-system. For example,
157306 ** to assemble an OTA enabled VFS stack that uses both zipvfs and
157307 ** multiplexor (error checking omitted):
157308 **
157309 ** // Create a VFS named "multiplex" (not the default).
157310 ** sqlite3_multiplex_initialize(0, 0);
157311 **
157312 ** // Create an ota VFS named "ota" that uses multiplexor. If the
157313 ** // second argument were replaced with NULL, the "ota" VFS would
157314 ** // access the file-system via the system default VFS, bypassing the
157315 ** // multiplexor.
157316 ** sqlite3ota_create_vfs("ota", "multiplex");
157317 **
157318 ** // Create a zipvfs VFS named "zipvfs" that uses ota.
157319 ** zipvfs_create_vfs_v3("zipvfs", "ota", 0, xCompressorAlgorithmDetector);
157320 **
157321 ** // Make zipvfs the default VFS.
157322 ** sqlite3_vfs_register(sqlite3_vfs_find("zipvfs"), 1);
157323 **
157324 ** Because the default VFS created above includes a OTA functionality, it
157325 ** may be used by OTA clients. Attempting to use OTA with a zipvfs VFS stack
157326 ** that does not include the OTA layer results in an error.
157327 **
157328 ** The overhead of adding the "ota" VFS to the system is negligible for
157329 ** non-OTA users. There is no harm in an application accessing the
157330 ** file-system via "ota" all the time, even if it only uses OTA functionality
157331 ** occasionally.
157332 */
157333 SQLITE_API int SQLITE_STDCALL sqlite3ota_create_vfs(const char *zName, const char *zParent);
157334
157335 /*
157336 ** Deregister and destroy an OTA vfs created by an earlier call to
157337 ** sqlite3ota_create_vfs().
157338 **
157339 ** VFS objects are not reference counted. If a VFS object is destroyed
157340 ** before all database handles that use it have been closed, the results
157341 ** are undefined.
157342 */
157343 SQLITE_API void SQLITE_STDCALL sqlite3ota_destroy_vfs(const char *zName);
157344
157345 #endif /* _SQLITE3OTA_H */
157346
157347 /************** End of sqlite3ota.h ******************************************/
157348 /************** Continuing where we left off in sqlite3ota.c *****************/
157349
157350 /* Maximum number of prepared UPDATE statements held by this module */
157351 #define SQLITE_OTA_UPDATE_CACHESIZE 16
157352
157353 /*
157354 ** Swap two objects of type TYPE.
157355 */
157356 #if !defined(SQLITE_AMALGAMATION)
157357 # define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
157358 #endif
157359
157360 /*
157361 ** The ota_state table is used to save the state of a partially applied
157362 ** update so that it can be resumed later. The table consists of integer
157363 ** keys mapped to values as follows:
157364 **
157365 ** OTA_STATE_STAGE:
157366 ** May be set to integer values 1, 2, 4 or 5. As follows:
157367 ** 1: the *-ota file is currently under construction.
157368 ** 2: the *-ota file has been constructed, but not yet moved
157369 ** to the *-wal path.
157370 ** 4: the checkpoint is underway.
157371 ** 5: the ota update has been checkpointed.
157372 **
157373 ** OTA_STATE_TBL:
157374 ** Only valid if STAGE==1. The target database name of the table
157375 ** currently being written.
157376 **
157377 ** OTA_STATE_IDX:
157378 ** Only valid if STAGE==1. The target database name of the index
157379 ** currently being written, or NULL if the main table is currently being
157380 ** updated.
157381 **
157382 ** OTA_STATE_ROW:
157383 ** Only valid if STAGE==1. Number of rows already processed for the current
157384 ** table/index.
157385 **
157386 ** OTA_STATE_PROGRESS:
157387 ** Total number of sqlite3ota_step() calls made so far as part of this
157388 ** ota update.
157389 **
157390 ** OTA_STATE_CKPT:
157391 ** Valid if STAGE==4. The 64-bit checksum associated with the wal-index
157392 ** header created by recovering the *-wal file. This is used to detect
157393 ** cases when another client appends frames to the *-wal file in the
157394 ** middle of an incremental checkpoint (an incremental checkpoint cannot
157395 ** be continued if this happens).
157396 **
157397 ** OTA_STATE_COOKIE:
157398 ** Valid if STAGE==1. The current change-counter cookie value in the
157399 ** target db file.
157400 **
157401 ** OTA_STATE_OALSZ:
157402 ** Valid if STAGE==1. The size in bytes of the *-oal file.
157403 */
157404 #define OTA_STATE_STAGE 1
157405 #define OTA_STATE_TBL 2
157406 #define OTA_STATE_IDX 3
157407 #define OTA_STATE_ROW 4
157408 #define OTA_STATE_PROGRESS 5
157409 #define OTA_STATE_CKPT 6
157410 #define OTA_STATE_COOKIE 7
157411 #define OTA_STATE_OALSZ 8
157412
157413 #define OTA_STAGE_OAL 1
157414 #define OTA_STAGE_MOVE 2
157415 #define OTA_STAGE_CAPTURE 3
157416 #define OTA_STAGE_CKPT 4
157417 #define OTA_STAGE_DONE 5
157418
157419
157420 #define OTA_CREATE_STATE \
157421 "CREATE TABLE IF NOT EXISTS %s.ota_state(k INTEGER PRIMARY KEY, v)"
157422
157423 typedef struct OtaFrame OtaFrame;
157424 typedef struct OtaObjIter OtaObjIter;
157425 typedef struct OtaState OtaState;
157426 typedef struct ota_vfs ota_vfs;
157427 typedef struct ota_file ota_file;
157428 typedef struct OtaUpdateStmt OtaUpdateStmt;
157429
157430 #if !defined(SQLITE_AMALGAMATION)
157431 typedef unsigned int u32;
157432 typedef unsigned char u8;
157433 typedef sqlite3_int64 i64;
@@ -157441,13 +157447,13 @@
157441 #define WAL_LOCK_WRITE 0
157442 #define WAL_LOCK_CKPT 1
157443 #define WAL_LOCK_READ0 3
157444
157445 /*
157446 ** A structure to store values read from the ota_state table in memory.
157447 */
157448 struct OtaState {
157449 int eStage;
157450 char *zTbl;
157451 char *zIdx;
157452 i64 iWalCksum;
157453 int nRow;
@@ -157454,14 +157460,14 @@
157454 i64 nProgress;
157455 u32 iCookie;
157456 i64 iOalSz;
157457 };
157458
157459 struct OtaUpdateStmt {
157460 char *zMask; /* Copy of update mask used with pUpdate */
157461 sqlite3_stmt *pUpdate; /* Last update statement (or NULL) */
157462 OtaUpdateStmt *pNext;
157463 };
157464
157465 /*
157466 ** An iterator of this type is used to iterate through all objects in
157467 ** the target database that require updating. For each such table, the
@@ -157476,131 +157482,131 @@
157476 ** it points to an array of flags nTblCol elements in size. The flag is
157477 ** set for each column that is either a part of the PK or a part of an
157478 ** index. Or clear otherwise.
157479 **
157480 */
157481 struct OtaObjIter {
157482 sqlite3_stmt *pTblIter; /* Iterate through tables */
157483 sqlite3_stmt *pIdxIter; /* Index iterator */
157484 int nTblCol; /* Size of azTblCol[] array */
157485 char **azTblCol; /* Array of unquoted target column names */
157486 char **azTblType; /* Array of target column types */
157487 int *aiSrcOrder; /* src table col -> target table col */
157488 u8 *abTblPk; /* Array of flags, set on target PK columns */
157489 u8 *abNotNull; /* Array of flags, set on NOT NULL columns */
157490 u8 *abIndexed; /* Array of flags, set on indexed & PK cols */
157491 int eType; /* Table type - an OTA_PK_XXX value */
157492
157493 /* Output variables. zTbl==0 implies EOF. */
157494 int bCleanup; /* True in "cleanup" state */
157495 const char *zTbl; /* Name of target db table */
157496 const char *zIdx; /* Name of target db index (or null) */
157497 int iTnum; /* Root page of current object */
157498 int iPkTnum; /* If eType==EXTERNAL, root of PK index */
157499 int bUnique; /* Current index is unique */
157500
157501 /* Statements created by otaObjIterPrepareAll() */
157502 int nCol; /* Number of columns in current object */
157503 sqlite3_stmt *pSelect; /* Source data */
157504 sqlite3_stmt *pInsert; /* Statement for INSERT operations */
157505 sqlite3_stmt *pDelete; /* Statement for DELETE ops */
157506 sqlite3_stmt *pTmpInsert; /* Insert into ota_tmp_$zTbl */
157507
157508 /* Last UPDATE used (for PK b-tree updates only), or NULL. */
157509 OtaUpdateStmt *pOtaUpdate;
157510 };
157511
157512 /*
157513 ** Values for OtaObjIter.eType
157514 **
157515 ** 0: Table does not exist (error)
157516 ** 1: Table has an implicit rowid.
157517 ** 2: Table has an explicit IPK column.
157518 ** 3: Table has an external PK index.
157519 ** 4: Table is WITHOUT ROWID.
157520 ** 5: Table is a virtual table.
157521 */
157522 #define OTA_PK_NOTABLE 0
157523 #define OTA_PK_NONE 1
157524 #define OTA_PK_IPK 2
157525 #define OTA_PK_EXTERNAL 3
157526 #define OTA_PK_WITHOUT_ROWID 4
157527 #define OTA_PK_VTAB 5
157528
157529
157530 /*
157531 ** Within the OTA_STAGE_OAL stage, each call to sqlite3ota_step() performs
157532 ** one of the following operations.
157533 */
157534 #define OTA_INSERT 1 /* Insert on a main table b-tree */
157535 #define OTA_DELETE 2 /* Delete a row from a main table b-tree */
157536 #define OTA_IDX_DELETE 3 /* Delete a row from an aux. index b-tree */
157537 #define OTA_IDX_INSERT 4 /* Insert on an aux. index b-tree */
157538 #define OTA_UPDATE 5 /* Update a row in a main table b-tree */
157539
157540
157541 /*
157542 ** A single step of an incremental checkpoint - frame iWalFrame of the wal
157543 ** file should be copied to page iDbPage of the database file.
157544 */
157545 struct OtaFrame {
157546 u32 iDbPage;
157547 u32 iWalFrame;
157548 };
157549
157550 /*
157551 ** OTA handle.
157552 */
157553 struct sqlite3ota {
157554 int eStage; /* Value of OTA_STATE_STAGE field */
157555 sqlite3 *dbMain; /* target database handle */
157556 sqlite3 *dbOta; /* ota database handle */
157557 char *zTarget; /* Path to target db */
157558 char *zOta; /* Path to ota db */
157559 char *zState; /* Path to state db (or NULL if zOta) */
157560 char zStateDb[5]; /* Db name for state ("stat" or "main") */
157561 int rc; /* Value returned by last ota_step() call */
157562 char *zErrmsg; /* Error message if rc!=SQLITE_OK */
157563 int nStep; /* Rows processed for current object */
157564 int nProgress; /* Rows processed for all objects */
157565 OtaObjIter objiter; /* Iterator for skipping through tbl/idx */
157566 const char *zVfsName; /* Name of automatically created ota vfs */
157567 ota_file *pTargetFd; /* File handle open on target db */
157568 i64 iOalSz;
157569
157570 /* The following state variables are used as part of the incremental
157571 ** checkpoint stage (eStage==OTA_STAGE_CKPT). See comments surrounding
157572 ** function otaSetupCheckpoint() for details. */
157573 u32 iMaxFrame; /* Largest iWalFrame value in aFrame[] */
157574 u32 mLock;
157575 int nFrame; /* Entries in aFrame[] array */
157576 int nFrameAlloc; /* Allocated size of aFrame[] array */
157577 OtaFrame *aFrame;
157578 int pgsz;
157579 u8 *aBuf;
157580 i64 iWalCksum;
157581 };
157582
157583 /*
157584 ** An ota VFS is implemented using an instance of this structure.
157585 */
157586 struct ota_vfs {
157587 sqlite3_vfs base; /* ota VFS shim methods */
157588 sqlite3_vfs *pRealVfs; /* Underlying VFS */
157589 sqlite3_mutex *mutex; /* Mutex to protect pMain */
157590 ota_file *pMain; /* Linked list of main db files */
157591 };
157592
157593 /*
157594 ** Each file opened by an ota VFS is represented by an instance of
157595 ** the following structure.
157596 */
157597 struct ota_file {
157598 sqlite3_file base; /* sqlite3_file methods */
157599 sqlite3_file *pReal; /* Underlying file handle */
157600 ota_vfs *pOtaVfs; /* Pointer to the ota_vfs object */
157601 sqlite3ota *pOta; /* Pointer to ota object (ota target only) */
157602
157603 int openFlags; /* Flags this file was opened with */
157604 u32 iCookie; /* Cookie value for main db files */
157605 u8 iWriteVer; /* "write-version" value for main db files */
157606
@@ -157607,12 +157613,12 @@
157607 int nShm; /* Number of entries in apShm[] array */
157608 char **apShm; /* Array of mmap'd *-shm regions */
157609 char *zDel; /* Delete this when closing file */
157610
157611 const char *zWal; /* Wal filename for this main db file */
157612 ota_file *pWalFd; /* Wal file descriptor for this main db */
157613 ota_file *pMainNext; /* Next MAIN_DB file */
157614 };
157615
157616
157617 /*
157618 ** Prepare the SQL statement in buffer zSql against database handle db.
@@ -157686,14 +157692,14 @@
157686 }
157687 return rc;
157688 }
157689
157690 /*
157691 ** Free the OtaObjIter.azTblCol[] and OtaObjIter.abTblPk[] arrays allocated
157692 ** by an earlier call to otaObjIterCacheTableInfo().
157693 */
157694 static void otaObjIterFreeCols(OtaObjIter *pIter){
157695 int i;
157696 for(i=0; i<pIter->nTblCol; i++){
157697 sqlite3_free(pIter->azTblCol[i]);
157698 sqlite3_free(pIter->azTblType[i]);
157699 }
@@ -157709,72 +157715,72 @@
157709
157710 /*
157711 ** Finalize all statements and free all allocations that are specific to
157712 ** the current object (table/index pair).
157713 */
157714 static void otaObjIterClearStatements(OtaObjIter *pIter){
157715 OtaUpdateStmt *pUp;
157716
157717 sqlite3_finalize(pIter->pSelect);
157718 sqlite3_finalize(pIter->pInsert);
157719 sqlite3_finalize(pIter->pDelete);
157720 sqlite3_finalize(pIter->pTmpInsert);
157721 pUp = pIter->pOtaUpdate;
157722 while( pUp ){
157723 OtaUpdateStmt *pTmp = pUp->pNext;
157724 sqlite3_finalize(pUp->pUpdate);
157725 sqlite3_free(pUp);
157726 pUp = pTmp;
157727 }
157728
157729 pIter->pSelect = 0;
157730 pIter->pInsert = 0;
157731 pIter->pDelete = 0;
157732 pIter->pOtaUpdate = 0;
157733 pIter->pTmpInsert = 0;
157734 pIter->nCol = 0;
157735 }
157736
157737 /*
157738 ** Clean up any resources allocated as part of the iterator object passed
157739 ** as the only argument.
157740 */
157741 static void otaObjIterFinalize(OtaObjIter *pIter){
157742 otaObjIterClearStatements(pIter);
157743 sqlite3_finalize(pIter->pTblIter);
157744 sqlite3_finalize(pIter->pIdxIter);
157745 otaObjIterFreeCols(pIter);
157746 memset(pIter, 0, sizeof(OtaObjIter));
157747 }
157748
157749 /*
157750 ** Advance the iterator to the next position.
157751 **
157752 ** If no error occurs, SQLITE_OK is returned and the iterator is left
157753 ** pointing to the next entry. Otherwise, an error code and message is
157754 ** left in the OTA handle passed as the first argument. A copy of the
157755 ** error code is returned.
157756 */
157757 static int otaObjIterNext(sqlite3ota *p, OtaObjIter *pIter){
157758 int rc = p->rc;
157759 if( rc==SQLITE_OK ){
157760
157761 /* Free any SQLite statements used while processing the previous object */
157762 otaObjIterClearStatements(pIter);
157763 if( pIter->zIdx==0 ){
157764 rc = sqlite3_exec(p->dbMain,
157765 "DROP TRIGGER IF EXISTS temp.ota_insert_tr;"
157766 "DROP TRIGGER IF EXISTS temp.ota_update1_tr;"
157767 "DROP TRIGGER IF EXISTS temp.ota_update2_tr;"
157768 "DROP TRIGGER IF EXISTS temp.ota_delete_tr;"
157769 , 0, 0, &p->zErrmsg
157770 );
157771 }
157772
157773 if( rc==SQLITE_OK ){
157774 if( pIter->bCleanup ){
157775 otaObjIterFreeCols(pIter);
157776 pIter->bCleanup = 0;
157777 rc = sqlite3_step(pIter->pTblIter);
157778 if( rc!=SQLITE_ROW ){
157779 rc = resetAndCollectError(pIter->pTblIter, &p->zErrmsg);
157780 pIter->zTbl = 0;
@@ -157803,11 +157809,11 @@
157803 }
157804 }
157805 }
157806
157807 if( rc!=SQLITE_OK ){
157808 otaObjIterFinalize(pIter);
157809 p->rc = rc;
157810 }
157811 return rc;
157812 }
157813
@@ -157814,18 +157820,18 @@
157814 /*
157815 ** Initialize the iterator structure passed as the second argument.
157816 **
157817 ** If no error occurs, SQLITE_OK is returned and the iterator is left
157818 ** pointing to the first entry. Otherwise, an error code and message is
157819 ** left in the OTA handle passed as the first argument. A copy of the
157820 ** error code is returned.
157821 */
157822 static int otaObjIterFirst(sqlite3ota *p, OtaObjIter *pIter){
157823 int rc;
157824 memset(pIter, 0, sizeof(OtaObjIter));
157825
157826 rc = prepareAndCollectError(p->dbOta, &pIter->pTblIter, &p->zErrmsg,
157827 "SELECT substr(name, 6) FROM sqlite_master "
157828 "WHERE type='table' AND name LIKE 'data_%'"
157829 );
157830
157831 if( rc==SQLITE_OK ){
@@ -157836,23 +157842,23 @@
157836 );
157837 }
157838
157839 pIter->bCleanup = 1;
157840 p->rc = rc;
157841 return otaObjIterNext(p, pIter);
157842 }
157843
157844 /*
157845 ** This is a wrapper around "sqlite3_mprintf(zFmt, ...)". If an OOM occurs,
157846 ** an error code is stored in the OTA handle passed as the first argument.
157847 **
157848 ** If an error has already occurred (p->rc is already set to something other
157849 ** than SQLITE_OK), then this function returns NULL without modifying the
157850 ** stored error code. In this case it still calls sqlite3_free() on any
157851 ** printf() parameters associated with %z conversions.
157852 */
157853 static char *otaMPrintf(sqlite3ota *p, const char *zFmt, ...){
157854 char *zSql = 0;
157855 va_list ap;
157856 va_start(ap, zFmt);
157857 zSql = sqlite3_vmprintf(zFmt, ap);
157858 if( p->rc==SQLITE_OK ){
@@ -157867,17 +157873,17 @@
157867
157868 /*
157869 ** Argument zFmt is a sqlite3_mprintf() style format string. The trailing
157870 ** arguments are the usual subsitution values. This function performs
157871 ** the printf() style substitutions and executes the result as an SQL
157872 ** statement on the OTA handles database.
157873 **
157874 ** If an error occurs, an error code and error message is stored in the
157875 ** OTA handle. If an error has already occurred when this function is
157876 ** called, it is a no-op.
157877 */
157878 static int otaMPrintfExec(sqlite3ota *p, sqlite3 *db, const char *zFmt, ...){
157879 va_list ap;
157880 va_start(ap, zFmt);
157881 char *zSql = sqlite3_vmprintf(zFmt, ap);
157882 if( p->rc==SQLITE_OK ){
157883 if( zSql==0 ){
@@ -157894,16 +157900,16 @@
157894 /*
157895 ** Attempt to allocate and return a pointer to a zeroed block of nByte
157896 ** bytes.
157897 **
157898 ** If an error (i.e. an OOM condition) occurs, return NULL and leave an
157899 ** error code in the ota handle passed as the first argument. Or, if an
157900 ** error has already occurred when this function is called, return NULL
157901 ** immediately without attempting the allocation or modifying the stored
157902 ** error code.
157903 */
157904 static void *otaMalloc(sqlite3ota *p, int nByte){
157905 void *pRet = 0;
157906 if( p->rc==SQLITE_OK ){
157907 assert( nByte>0 );
157908 pRet = sqlite3_malloc(nByte);
157909 if( pRet==0 ){
@@ -157917,17 +157923,17 @@
157917
157918
157919 /*
157920 ** Allocate and zero the pIter->azTblCol[] and abTblPk[] arrays so that
157921 ** there is room for at least nCol elements. If an OOM occurs, store an
157922 ** error code in the OTA handle passed as the first argument.
157923 */
157924 static void otaAllocateIterArrays(sqlite3ota *p, OtaObjIter *pIter, int nCol){
157925 int nByte = (2*sizeof(char*) + sizeof(int) + 3*sizeof(u8)) * nCol;
157926 char **azNew;
157927
157928 azNew = (char**)otaMalloc(p, nByte);
157929 if( azNew ){
157930 pIter->azTblCol = azNew;
157931 pIter->azTblType = &azNew[nCol];
157932 pIter->aiSrcOrder = (int*)&pIter->azTblType[nCol];
157933 pIter->abTblPk = (u8*)&pIter->aiSrcOrder[nCol];
@@ -157944,11 +157950,11 @@
157944 **
157945 ** If an OOM condition is encountered when attempting to allocate memory,
157946 ** output variable (*pRc) is set to SQLITE_NOMEM before returning. Otherwise,
157947 ** if the allocation succeeds, (*pRc) is left unchanged.
157948 */
157949 static char *otaStrndup(const char *zStr, int *pRc){
157950 char *zRet = 0;
157951
157952 assert( *pRc==SQLITE_OK );
157953 if( zStr ){
157954 int nCopy = strlen(zStr) + 1;
@@ -157965,14 +157971,14 @@
157965
157966 /*
157967 ** Finalize the statement passed as the second argument.
157968 **
157969 ** If the sqlite3_finalize() call indicates that an error occurs, and the
157970 ** ota handle error code is not already set, set the error code and error
157971 ** message accordingly.
157972 */
157973 static void otaFinalize(sqlite3ota *p, sqlite3_stmt *pStmt){
157974 sqlite3 *db = sqlite3_db_handle(pStmt);
157975 int rc = sqlite3_finalize(pStmt);
157976 if( p->rc==SQLITE_OK && rc!=SQLITE_OK ){
157977 p->rc = rc;
157978 p->zErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
@@ -157983,16 +157989,16 @@
157983 **
157984 ** peType is of type (int*), a pointer to an output parameter of type
157985 ** (int). This call sets the output parameter as follows, depending
157986 ** on the type of the table specified by parameters dbName and zTbl.
157987 **
157988 ** OTA_PK_NOTABLE: No such table.
157989 ** OTA_PK_NONE: Table has an implicit rowid.
157990 ** OTA_PK_IPK: Table has an explicit IPK column.
157991 ** OTA_PK_EXTERNAL: Table has an external PK index.
157992 ** OTA_PK_WITHOUT_ROWID: Table is WITHOUT ROWID.
157993 ** OTA_PK_VTAB: Table is a virtual table.
157994 **
157995 ** Argument *piPk is also of type (int*), and also points to an output
157996 ** parameter. Unless the table has an external primary key index
157997 ** (i.e. unless *peType is set to 3), then *piPk is set to zero. Or,
157998 ** if the table does have an external primary key index, then *piPk
@@ -158000,28 +158006,28 @@
158000 ** returning.
158001 **
158002 ** ALGORITHM:
158003 **
158004 ** if( no entry exists in sqlite_master ){
158005 ** return OTA_PK_NOTABLE
158006 ** }else if( sql for the entry starts with "CREATE VIRTUAL" ){
158007 ** return OTA_PK_VTAB
158008 ** }else if( "PRAGMA index_list()" for the table contains a "pk" index ){
158009 ** if( the index that is the pk exists in sqlite_master ){
158010 ** *piPK = rootpage of that index.
158011 ** return OTA_PK_EXTERNAL
158012 ** }else{
158013 ** return OTA_PK_WITHOUT_ROWID
158014 ** }
158015 ** }else if( "PRAGMA table_info()" lists one or more "pk" columns ){
158016 ** return OTA_PK_IPK
158017 ** }else{
158018 ** return OTA_PK_NONE
158019 ** }
158020 */
158021 static void otaTableType(
158022 sqlite3ota *p,
158023 const char *zTab,
158024 int *peType,
158025 int *piTnum,
158026 int *piPk
158027 ){
@@ -158031,11 +158037,11 @@
158031 ** 2) SELECT count(*) FROM sqlite_master where name=%Q
158032 ** 3) PRAGMA table_info = ?
158033 */
158034 sqlite3_stmt *aStmt[4] = {0, 0, 0, 0};
158035
158036 *peType = OTA_PK_NOTABLE;
158037 *piPk = 0;
158038
158039 assert( p->rc==SQLITE_OK );
158040 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[0], &p->zErrmsg,
158041 sqlite3_mprintf(
@@ -158043,22 +158049,22 @@
158043 " FROM sqlite_master"
158044 " WHERE name=%Q", zTab
158045 ));
158046 if( p->rc!=SQLITE_OK || sqlite3_step(aStmt[0])!=SQLITE_ROW ){
158047 /* Either an error, or no such table. */
158048 goto otaTableType_end;
158049 }
158050 if( sqlite3_column_int(aStmt[0], 0) ){
158051 *peType = OTA_PK_VTAB; /* virtual table */
158052 goto otaTableType_end;
158053 }
158054 *piTnum = sqlite3_column_int(aStmt[0], 1);
158055
158056 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[1], &p->zErrmsg,
158057 sqlite3_mprintf("PRAGMA index_list=%Q",zTab)
158058 );
158059 if( p->rc ) goto otaTableType_end;
158060 while( sqlite3_step(aStmt[1])==SQLITE_ROW ){
158061 const u8 *zOrig = sqlite3_column_text(aStmt[1], 3);
158062 const u8 *zIdx = sqlite3_column_text(aStmt[1], 1);
158063 if( zOrig && zIdx && zOrig[0]=='p' ){
158064 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[2], &p->zErrmsg,
@@ -158066,45 +158072,45 @@
158066 "SELECT rootpage FROM sqlite_master WHERE name = %Q", zIdx
158067 ));
158068 if( p->rc==SQLITE_OK ){
158069 if( sqlite3_step(aStmt[2])==SQLITE_ROW ){
158070 *piPk = sqlite3_column_int(aStmt[2], 0);
158071 *peType = OTA_PK_EXTERNAL;
158072 }else{
158073 *peType = OTA_PK_WITHOUT_ROWID;
158074 }
158075 }
158076 goto otaTableType_end;
158077 }
158078 }
158079
158080 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[3], &p->zErrmsg,
158081 sqlite3_mprintf("PRAGMA table_info=%Q",zTab)
158082 );
158083 if( p->rc==SQLITE_OK ){
158084 while( sqlite3_step(aStmt[3])==SQLITE_ROW ){
158085 if( sqlite3_column_int(aStmt[3],5)>0 ){
158086 *peType = OTA_PK_IPK; /* explicit IPK column */
158087 goto otaTableType_end;
158088 }
158089 }
158090 *peType = OTA_PK_NONE;
158091 }
158092
158093 otaTableType_end: {
158094 int i;
158095 for(i=0; i<sizeof(aStmt)/sizeof(aStmt[0]); i++){
158096 otaFinalize(p, aStmt[i]);
158097 }
158098 }
158099 }
158100
158101 /*
158102 ** This is a helper function for otaObjIterCacheTableInfo(). It populates
158103 ** the pIter->abIndexed[] array.
158104 */
158105 static void otaObjIterCacheIndexedCols(sqlite3ota *p, OtaObjIter *pIter){
158106 sqlite3_stmt *pList = 0;
158107 int bIndex = 0;
158108
158109 if( p->rc==SQLITE_OK ){
158110 memcpy(pIter->abIndexed, pIter->abTblPk, sizeof(u8)*pIter->nTblCol);
@@ -158122,15 +158128,15 @@
158122 );
158123 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
158124 int iCid = sqlite3_column_int(pXInfo, 1);
158125 if( iCid>=0 ) pIter->abIndexed[iCid] = 1;
158126 }
158127 otaFinalize(p, pXInfo);
158128 bIndex = 1;
158129 }
158130
158131 otaFinalize(p, pList);
158132 if( bIndex==0 ) pIter->abIndexed = 0;
158133 }
158134
158135
158136 /*
@@ -158138,67 +158144,67 @@
158138 ** pIter->abTblPk[], pIter->nTblCol and pIter->bRowid variables according to
158139 ** the table (not index) that the iterator currently points to.
158140 **
158141 ** Return SQLITE_OK if successful, or an SQLite error code otherwise. If
158142 ** an error does occur, an error code and error message are also left in
158143 ** the OTA handle.
158144 */
158145 static int otaObjIterCacheTableInfo(sqlite3ota *p, OtaObjIter *pIter){
158146 if( pIter->azTblCol==0 ){
158147 sqlite3_stmt *pStmt = 0;
158148 int nCol = 0;
158149 int i; /* for() loop iterator variable */
158150 int bOtaRowid = 0; /* If input table has column "ota_rowid" */
158151 int iOrder = 0;
158152 int iTnum = 0;
158153
158154 /* Figure out the type of table this step will deal with. */
158155 assert( pIter->eType==0 );
158156 otaTableType(p, pIter->zTbl, &pIter->eType, &iTnum, &pIter->iPkTnum);
158157 if( p->rc==SQLITE_OK && pIter->eType==OTA_PK_NOTABLE ){
158158 p->rc = SQLITE_ERROR;
158159 p->zErrmsg = sqlite3_mprintf("no such table: %s", pIter->zTbl);
158160 }
158161 if( p->rc ) return p->rc;
158162 if( pIter->zIdx==0 ) pIter->iTnum = iTnum;
158163
158164 assert( pIter->eType==OTA_PK_NONE || pIter->eType==OTA_PK_IPK
158165 || pIter->eType==OTA_PK_EXTERNAL || pIter->eType==OTA_PK_WITHOUT_ROWID
158166 || pIter->eType==OTA_PK_VTAB
158167 );
158168
158169 /* Populate the azTblCol[] and nTblCol variables based on the columns
158170 ** of the input table. Ignore any input table columns that begin with
158171 ** "ota_". */
158172 p->rc = prepareFreeAndCollectError(p->dbOta, &pStmt, &p->zErrmsg,
158173 sqlite3_mprintf("SELECT * FROM 'data_%q'", pIter->zTbl)
158174 );
158175 if( p->rc==SQLITE_OK ){
158176 nCol = sqlite3_column_count(pStmt);
158177 otaAllocateIterArrays(p, pIter, nCol);
158178 }
158179 for(i=0; p->rc==SQLITE_OK && i<nCol; i++){
158180 const char *zName = (const char*)sqlite3_column_name(pStmt, i);
158181 if( sqlite3_strnicmp("ota_", zName, 4) ){
158182 char *zCopy = otaStrndup(zName, &p->rc);
158183 pIter->aiSrcOrder[pIter->nTblCol] = pIter->nTblCol;
158184 pIter->azTblCol[pIter->nTblCol++] = zCopy;
158185 }
158186 else if( 0==sqlite3_stricmp("ota_rowid", zName) ){
158187 bOtaRowid = 1;
158188 }
158189 }
158190 sqlite3_finalize(pStmt);
158191 pStmt = 0;
158192
158193 if( p->rc==SQLITE_OK
158194 && bOtaRowid!=(pIter->eType==OTA_PK_VTAB || pIter->eType==OTA_PK_NONE)
158195 ){
158196 p->rc = SQLITE_ERROR;
158197 p->zErrmsg = sqlite3_mprintf(
158198 "table data_%q %s ota_rowid column", pIter->zTbl,
158199 (bOtaRowid ? "may not have" : "requires")
158200 );
158201 }
158202
158203 /* Check that all non-HIDDEN columns in the destination table are also
158204 ** present in the input table. Populate the abTblPk[], azTblType[] and
@@ -158227,20 +158233,20 @@
158227 if( i!=iOrder ){
158228 SWAP(int, pIter->aiSrcOrder[i], pIter->aiSrcOrder[iOrder]);
158229 SWAP(char*, pIter->azTblCol[i], pIter->azTblCol[iOrder]);
158230 }
158231
158232 pIter->azTblType[iOrder] = otaStrndup(zType, &p->rc);
158233 pIter->abTblPk[iOrder] = (iPk!=0);
158234 pIter->abNotNull[iOrder] = (u8)bNotNull || (iPk!=0);
158235 iOrder++;
158236 }
158237 }
158238
158239 otaFinalize(p, pStmt);
158240 otaObjIterCacheIndexedCols(p, pIter);
158241 assert( pIter->eType!=OTA_PK_VTAB || pIter->abIndexed==0 );
158242 }
158243
158244 return p->rc;
158245 }
158246
@@ -158247,29 +158253,29 @@
158247 /*
158248 ** This function constructs and returns a pointer to a nul-terminated
158249 ** string containing some SQL clause or list based on one or more of the
158250 ** column names currently stored in the pIter->azTblCol[] array.
158251 */
158252 static char *otaObjIterGetCollist(
158253 sqlite3ota *p, /* OTA object */
158254 OtaObjIter *pIter /* Object iterator for column names */
158255 ){
158256 char *zList = 0;
158257 const char *zSep = "";
158258 int i;
158259 for(i=0; i<pIter->nTblCol; i++){
158260 const char *z = pIter->azTblCol[i];
158261 zList = otaMPrintf(p, "%z%s\"%w\"", zList, zSep, z);
158262 zSep = ", ";
158263 }
158264 return zList;
158265 }
158266
158267 /*
158268 ** This function is used to create a SELECT list (the list of SQL
158269 ** expressions that follows a SELECT keyword) for a SELECT statement
158270 ** used to read from an data_xxx or ota_tmp_xxx table while updating the
158271 ** index object currently indicated by the iterator object passed as the
158272 ** second argument. A "PRAGMA index_xinfo = <idxname>" statement is used
158273 ** to obtain the required information.
158274 **
158275 ** If the index is of the following form:
@@ -158286,17 +158292,17 @@
158286 **
158287 ** pzImposterCols: ...
158288 ** pzImposterPk: ...
158289 ** pzWhere: ...
158290 */
158291 static char *otaObjIterGetIndexCols(
158292 sqlite3ota *p, /* OTA object */
158293 OtaObjIter *pIter, /* Object iterator for column names */
158294 char **pzImposterCols, /* OUT: Columns for imposter table */
158295 char **pzImposterPk, /* OUT: Imposter PK clause */
158296 char **pzWhere, /* OUT: WHERE clause */
158297 int *pnBind /* OUT: Total number of columns */
158298 ){
158299 int rc = p->rc; /* Error code */
158300 int rc2; /* sqlite3_finalize() return code */
158301 char *zRet = 0; /* String to return */
158302 char *zImpCols = 0; /* String to return via *pzImposterCols */
@@ -158321,18 +158327,18 @@
158321 const char *zCol;
158322 const char *zType;
158323
158324 if( iCid<0 ){
158325 /* An integer primary key. If the table has an explicit IPK, use
158326 ** its name. Otherwise, use "ota_rowid". */
158327 if( pIter->eType==OTA_PK_IPK ){
158328 int i;
158329 for(i=0; pIter->abTblPk[i]==0; i++);
158330 assert( i<pIter->nTblCol );
158331 zCol = pIter->azTblCol[i];
158332 }else{
158333 zCol = "ota_rowid";
158334 }
158335 zType = "INTEGER";
158336 }else{
158337 zCol = pIter->azTblCol[iCid];
158338 zType = pIter->azTblType[iCid];
@@ -158339,19 +158345,19 @@
158339 }
158340
158341 zRet = sqlite3_mprintf("%z%s\"%w\" COLLATE %Q", zRet, zCom, zCol, zCollate);
158342 if( pIter->bUnique==0 || sqlite3_column_int(pXInfo, 5) ){
158343 const char *zOrder = (bDesc ? " DESC" : "");
158344 zImpPK = sqlite3_mprintf("%z%s\"ota_imp_%d%w\"%s",
158345 zImpPK, zCom, nBind, zCol, zOrder
158346 );
158347 }
158348 zImpCols = sqlite3_mprintf("%z%s\"ota_imp_%d%w\" %s COLLATE %Q",
158349 zImpCols, zCom, nBind, zCol, zType, zCollate
158350 );
158351 zWhere = sqlite3_mprintf(
158352 "%z%s\"ota_imp_%d%w\" IS ?", zWhere, zAnd, nBind, zCol
158353 );
158354 if( zRet==0 || zImpPK==0 || zImpCols==0 || zWhere==0 ) rc = SQLITE_NOMEM;
158355 zCom = ", ";
158356 zAnd = " AND ";
158357 nBind++;
@@ -158385,16 +158391,16 @@
158385 **
158386 ** "old.a, old.b, old.b"
158387 **
158388 ** With the column names escaped.
158389 **
158390 ** For tables with implicit rowids - OTA_PK_EXTERNAL and OTA_PK_NONE, append
158391 ** the text ", old._rowid_" to the returned value.
158392 */
158393 static char *otaObjIterGetOldlist(
158394 sqlite3ota *p,
158395 OtaObjIter *pIter,
158396 const char *zObj
158397 ){
158398 char *zList = 0;
158399 if( p->rc==SQLITE_OK && pIter->abIndexed ){
158400 const char *zS = "";
@@ -158412,12 +158418,12 @@
158412 break;
158413 }
158414 }
158415
158416 /* For a table with implicit rowids, append "old._rowid_" to the list. */
158417 if( pIter->eType==OTA_PK_EXTERNAL || pIter->eType==OTA_PK_NONE ){
158418 zList = otaMPrintf(p, "%z, %s._rowid_", zList, zObj);
158419 }
158420 }
158421 return zList;
158422 }
158423
@@ -158429,37 +158435,37 @@
158429 **
158430 ** Return the string:
158431 **
158432 ** "b = ?1 AND c = ?2"
158433 */
158434 static char *otaObjIterGetWhere(
158435 sqlite3ota *p,
158436 OtaObjIter *pIter
158437 ){
158438 char *zList = 0;
158439 if( pIter->eType==OTA_PK_VTAB || pIter->eType==OTA_PK_NONE ){
158440 zList = otaMPrintf(p, "_rowid_ = ?%d", pIter->nTblCol+1);
158441 }else if( pIter->eType==OTA_PK_EXTERNAL ){
158442 const char *zSep = "";
158443 int i;
158444 for(i=0; i<pIter->nTblCol; i++){
158445 if( pIter->abTblPk[i] ){
158446 zList = otaMPrintf(p, "%z%sc%d=?%d", zList, zSep, i, i+1);
158447 zSep = " AND ";
158448 }
158449 }
158450 zList = otaMPrintf(p,
158451 "_rowid_ = (SELECT id FROM ota_imposter2 WHERE %z)", zList
158452 );
158453
158454 }else{
158455 const char *zSep = "";
158456 int i;
158457 for(i=0; i<pIter->nTblCol; i++){
158458 if( pIter->abTblPk[i] ){
158459 const char *zCol = pIter->azTblCol[i];
158460 zList = otaMPrintf(p, "%z%s\"%w\"=?%d", zList, zSep, zCol, i+1);
158461 zSep = " AND ";
158462 }
158463 }
158464 }
158465 return zList;
@@ -158466,60 +158472,60 @@
158466 }
158467
158468 /*
158469 ** The SELECT statement iterating through the keys for the current object
158470 ** (p->objiter.pSelect) currently points to a valid row. However, there
158471 ** is something wrong with the ota_control value in the ota_control value
158472 ** stored in the (p->nCol+1)'th column. Set the error code and error message
158473 ** of the OTA handle to something reflecting this.
158474 */
158475 static void otaBadControlError(sqlite3ota *p){
158476 p->rc = SQLITE_ERROR;
158477 p->zErrmsg = sqlite3_mprintf("invalid ota_control value");
158478 }
158479
158480
158481 /*
158482 ** Return a nul-terminated string containing the comma separated list of
158483 ** assignments that should be included following the "SET" keyword of
158484 ** an UPDATE statement used to update the table object that the iterator
158485 ** passed as the second argument currently points to if the ota_control
158486 ** column of the data_xxx table entry is set to zMask.
158487 **
158488 ** The memory for the returned string is obtained from sqlite3_malloc().
158489 ** It is the responsibility of the caller to eventually free it using
158490 ** sqlite3_free().
158491 **
158492 ** If an OOM error is encountered when allocating space for the new
158493 ** string, an error code is left in the ota handle passed as the first
158494 ** argument and NULL is returned. Or, if an error has already occurred
158495 ** when this function is called, NULL is returned immediately, without
158496 ** attempting the allocation or modifying the stored error code.
158497 */
158498 static char *otaObjIterGetSetlist(
158499 sqlite3ota *p,
158500 OtaObjIter *pIter,
158501 const char *zMask
158502 ){
158503 char *zList = 0;
158504 if( p->rc==SQLITE_OK ){
158505 int i;
158506
158507 if( strlen(zMask)!=pIter->nTblCol ){
158508 otaBadControlError(p);
158509 }else{
158510 const char *zSep = "";
158511 for(i=0; i<pIter->nTblCol; i++){
158512 char c = zMask[pIter->aiSrcOrder[i]];
158513 if( c=='x' ){
158514 zList = otaMPrintf(p, "%z%s\"%w\"=?%d",
158515 zList, zSep, pIter->azTblCol[i], i+1
158516 );
158517 zSep = ", ";
158518 }
158519 if( c=='d' ){
158520 zList = otaMPrintf(p, "%z%s\"%w\"=ota_delta(\"%w\", ?%d)",
158521 zList, zSep, pIter->azTblCol[i], pIter->azTblCol[i], i+1
158522 );
158523 zSep = ", ";
158524 }
158525 }
@@ -158536,20 +158542,20 @@
158536 ** The memory for the returned string is obtained from sqlite3_malloc().
158537 ** It is the responsibility of the caller to eventually free it using
158538 ** sqlite3_free().
158539 **
158540 ** If an OOM error is encountered when allocating space for the new
158541 ** string, an error code is left in the ota handle passed as the first
158542 ** argument and NULL is returned. Or, if an error has already occurred
158543 ** when this function is called, NULL is returned immediately, without
158544 ** attempting the allocation or modifying the stored error code.
158545 */
158546 static char *otaObjIterGetBindlist(sqlite3ota *p, int nBind){
158547 char *zRet = 0;
158548 int nByte = nBind*2 + 1;
158549
158550 zRet = (char*)otaMalloc(p, nByte);
158551 if( zRet ){
158552 int i;
158553 for(i=0; i<nBind; i++){
158554 zRet[i*2] = '?';
158555 zRet[i*2+1] = (i+1==nBind) ? '\0' : ',';
@@ -158558,21 +158564,21 @@
158558 return zRet;
158559 }
158560
158561 /*
158562 ** The iterator currently points to a table (not index) of type
158563 ** OTA_PK_WITHOUT_ROWID. This function creates the PRIMARY KEY
158564 ** declaration for the corresponding imposter table. For example,
158565 ** if the iterator points to a table created as:
158566 **
158567 ** CREATE TABLE t1(a, b, c, PRIMARY KEY(b, a DESC)) WITHOUT ROWID
158568 **
158569 ** this function returns:
158570 **
158571 ** PRIMARY KEY("b", "a" DESC)
158572 */
158573 static char *otaWithoutRowidPK(sqlite3ota *p, OtaObjIter *pIter){
158574 char *z = 0;
158575 assert( pIter->zIdx==0 );
158576 if( p->rc==SQLITE_OK ){
158577 const char *zSep = "PRIMARY KEY(";
158578 sqlite3_stmt *pXList = 0; /* PRAGMA index_list = (pIter->zTbl) */
@@ -158591,23 +158597,23 @@
158591 );
158592 }
158593 break;
158594 }
158595 }
158596 otaFinalize(p, pXList);
158597
158598 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
158599 if( sqlite3_column_int(pXInfo, 5) ){
158600 /* int iCid = sqlite3_column_int(pXInfo, 0); */
158601 const char *zCol = (const char*)sqlite3_column_text(pXInfo, 2);
158602 const char *zDesc = sqlite3_column_int(pXInfo, 3) ? " DESC" : "";
158603 z = otaMPrintf(p, "%z%s\"%w\"%s", z, zSep, zCol, zDesc);
158604 zSep = ", ";
158605 }
158606 }
158607 z = otaMPrintf(p, "%z)", z);
158608 otaFinalize(p, pXInfo);
158609 }
158610 return z;
158611 }
158612
158613 /*
@@ -158616,23 +158622,23 @@
158616 ** iterator passed as the second argument does not currently point to
158617 ** a table (not index) with an external primary key, this function is a
158618 ** no-op.
158619 **
158620 ** Assuming the iterator does point to a table with an external PK, this
158621 ** function creates a WITHOUT ROWID imposter table named "ota_imposter2"
158622 ** used to access that PK index. For example, if the target table is
158623 ** declared as follows:
158624 **
158625 ** CREATE TABLE t1(a, b TEXT, c REAL, PRIMARY KEY(b, c));
158626 **
158627 ** then the imposter table schema is:
158628 **
158629 ** CREATE TABLE ota_imposter2(c1 TEXT, c2 REAL, id INTEGER) WITHOUT ROWID;
158630 **
158631 */
158632 static void otaCreateImposterTable2(sqlite3ota *p, OtaObjIter *pIter){
158633 if( p->rc==SQLITE_OK && pIter->eType==OTA_PK_EXTERNAL ){
158634 int tnum = pIter->iPkTnum; /* Root page of PK index */
158635 sqlite3_stmt *pQuery = 0; /* SELECT name ... WHERE rootpage = $tnum */
158636 const char *zIdx = 0; /* Name of PK index */
158637 sqlite3_stmt *pXInfo = 0; /* PRAGMA main.index_xinfo = $zIdx */
158638 const char *zComma = "";
@@ -158654,31 +158660,31 @@
158654 if( zIdx ){
158655 p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
158656 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
158657 );
158658 }
158659 otaFinalize(p, pQuery);
158660
158661 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
158662 int bKey = sqlite3_column_int(pXInfo, 5);
158663 if( bKey ){
158664 int iCid = sqlite3_column_int(pXInfo, 1);
158665 int bDesc = sqlite3_column_int(pXInfo, 3);
158666 const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
158667 zCols = otaMPrintf(p, "%z%sc%d %s COLLATE %s", zCols, zComma,
158668 iCid, pIter->azTblType[iCid], zCollate
158669 );
158670 zPk = otaMPrintf(p, "%z%sc%d%s", zPk, zComma, iCid, bDesc?" DESC":"");
158671 zComma = ", ";
158672 }
158673 }
158674 zCols = otaMPrintf(p, "%z, id INTEGER", zCols);
158675 otaFinalize(p, pXInfo);
158676
158677 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1, tnum);
158678 otaMPrintfExec(p, p->dbMain,
158679 "CREATE TABLE ota_imposter2(%z, PRIMARY KEY(%z)) WITHOUT ROWID",
158680 zCols, zPk
158681 );
158682 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
158683 }
158684 }
@@ -158685,28 +158691,28 @@
158685
158686 /*
158687 ** If an error has already occurred when this function is called, it
158688 ** immediately returns zero (without doing any work). Or, if an error
158689 ** occurs during the execution of this function, it sets the error code
158690 ** in the sqlite3ota object indicated by the first argument and returns
158691 ** zero.
158692 **
158693 ** The iterator passed as the second argument is guaranteed to point to
158694 ** a table (not an index) when this function is called. This function
158695 ** attempts to create any imposter table required to write to the main
158696 ** table b-tree of the table before returning. Non-zero is returned if
158697 ** an imposter table are created, or zero otherwise.
158698 **
158699 ** An imposter table is required in all cases except OTA_PK_VTAB. Only
158700 ** virtual tables are written to directly. The imposter table has the
158701 ** same schema as the actual target table (less any UNIQUE constraints).
158702 ** More precisely, the "same schema" means the same columns, types,
158703 ** collation sequences. For tables that do not have an external PRIMARY
158704 ** KEY, it also means the same PRIMARY KEY declaration.
158705 */
158706 static void otaCreateImposterTable(sqlite3ota *p, OtaObjIter *pIter){
158707 if( p->rc==SQLITE_OK && pIter->eType!=OTA_PK_VTAB ){
158708 int tnum = pIter->iTnum;
158709 const char *zComma = "";
158710 char *zSql = 0;
158711 int iCol;
158712 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 1);
@@ -158718,73 +158724,73 @@
158718
158719 p->rc = sqlite3_table_column_metadata(
158720 p->dbMain, "main", pIter->zTbl, zCol, 0, &zColl, 0, 0, 0
158721 );
158722
158723 if( pIter->eType==OTA_PK_IPK && pIter->abTblPk[iCol] ){
158724 /* If the target table column is an "INTEGER PRIMARY KEY", add
158725 ** "PRIMARY KEY" to the imposter table column declaration. */
158726 zPk = "PRIMARY KEY ";
158727 }
158728 zSql = otaMPrintf(p, "%z%s\"%w\" %s %sCOLLATE %s%s",
158729 zSql, zComma, zCol, pIter->azTblType[iCol], zPk, zColl,
158730 (pIter->abNotNull[iCol] ? " NOT NULL" : "")
158731 );
158732 zComma = ", ";
158733 }
158734
158735 if( pIter->eType==OTA_PK_WITHOUT_ROWID ){
158736 char *zPk = otaWithoutRowidPK(p, pIter);
158737 if( zPk ){
158738 zSql = otaMPrintf(p, "%z, %z", zSql, zPk);
158739 }
158740 }
158741
158742 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1, tnum);
158743 otaMPrintfExec(p, p->dbMain, "CREATE TABLE \"ota_imp_%w\"(%z)%s",
158744 pIter->zTbl, zSql,
158745 (pIter->eType==OTA_PK_WITHOUT_ROWID ? " WITHOUT ROWID" : "")
158746 );
158747 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
158748 }
158749 }
158750
158751 /*
158752 ** Prepare a statement used to insert rows into the "ota_tmp_xxx" table.
158753 ** Specifically a statement of the form:
158754 **
158755 ** INSERT INTO ota_tmp_xxx VALUES(?, ?, ? ...);
158756 **
158757 ** The number of bound variables is equal to the number of columns in
158758 ** the target table, plus one (for the ota_control column), plus one more
158759 ** (for the ota_rowid column) if the target table is an implicit IPK or
158760 ** virtual table.
158761 */
158762 static void otaObjIterPrepareTmpInsert(
158763 sqlite3ota *p,
158764 OtaObjIter *pIter,
158765 const char *zCollist,
158766 const char *zOtaRowid
158767 ){
158768 int bOtaRowid = (pIter->eType==OTA_PK_EXTERNAL || pIter->eType==OTA_PK_NONE);
158769 char *zBind = otaObjIterGetBindlist(p, pIter->nTblCol + 1 + bOtaRowid);
158770 if( zBind ){
158771 assert( pIter->pTmpInsert==0 );
158772 p->rc = prepareFreeAndCollectError(
158773 p->dbOta, &pIter->pTmpInsert, &p->zErrmsg, sqlite3_mprintf(
158774 "INSERT INTO %s.'ota_tmp_%q'(ota_control,%s%s) VALUES(%z)",
158775 p->zStateDb, pIter->zTbl, zCollist, zOtaRowid, zBind
158776 ));
158777 }
158778 }
158779
158780 static void otaTmpInsertFunc(
158781 sqlite3_context *pCtx,
158782 int nVal,
158783 sqlite3_value **apVal
158784 ){
158785 sqlite3ota *p = sqlite3_user_data(pCtx);
158786 int rc = SQLITE_OK;
158787 int i;
158788
158789 for(i=0; rc==SQLITE_OK && i<nVal; i++){
158790 rc = sqlite3_bind_value(p->objiter.pTmpInsert, i+1, apVal[i]);
@@ -158802,17 +158808,17 @@
158802 /*
158803 ** Ensure that the SQLite statement handles required to update the
158804 ** target database object currently indicated by the iterator passed
158805 ** as the second argument are available.
158806 */
158807 static int otaObjIterPrepareAll(
158808 sqlite3ota *p,
158809 OtaObjIter *pIter,
158810 int nOffset /* Add "LIMIT -1 OFFSET $nOffset" to SELECT */
158811 ){
158812 assert( pIter->bCleanup==0 );
158813 if( pIter->pSelect==0 && otaObjIterCacheTableInfo(p, pIter)==SQLITE_OK ){
158814 const int tnum = pIter->iTnum;
158815 char *zCollist = 0; /* List of indexed columns */
158816 char **pz = &p->zErrmsg;
158817 const char *zIdx = pIter->zIdx;
158818 char *zLimit = 0;
@@ -158828,104 +158834,104 @@
158828 char *zImposterPK = 0; /* Primary key declaration for imposter */
158829 char *zWhere = 0; /* WHERE clause on PK columns */
158830 char *zBind = 0;
158831 int nBind = 0;
158832
158833 assert( pIter->eType!=OTA_PK_VTAB );
158834 zCollist = otaObjIterGetIndexCols(
158835 p, pIter, &zImposterCols, &zImposterPK, &zWhere, &nBind
158836 );
158837 zBind = otaObjIterGetBindlist(p, nBind);
158838
158839 /* Create the imposter table used to write to this index. */
158840 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 1);
158841 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1,tnum);
158842 otaMPrintfExec(p, p->dbMain,
158843 "CREATE TABLE \"ota_imp_%w\"( %s, PRIMARY KEY( %s ) ) WITHOUT ROWID",
158844 zTbl, zImposterCols, zImposterPK
158845 );
158846 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
158847
158848 /* Create the statement to insert index entries */
158849 pIter->nCol = nBind;
158850 if( p->rc==SQLITE_OK ){
158851 p->rc = prepareFreeAndCollectError(
158852 p->dbMain, &pIter->pInsert, &p->zErrmsg,
158853 sqlite3_mprintf("INSERT INTO \"ota_imp_%w\" VALUES(%s)", zTbl, zBind)
158854 );
158855 }
158856
158857 /* And to delete index entries */
158858 if( p->rc==SQLITE_OK ){
158859 p->rc = prepareFreeAndCollectError(
158860 p->dbMain, &pIter->pDelete, &p->zErrmsg,
158861 sqlite3_mprintf("DELETE FROM \"ota_imp_%w\" WHERE %s", zTbl, zWhere)
158862 );
158863 }
158864
158865 /* Create the SELECT statement to read keys in sorted order */
158866 if( p->rc==SQLITE_OK ){
158867 char *zSql;
158868 if( pIter->eType==OTA_PK_EXTERNAL || pIter->eType==OTA_PK_NONE ){
158869 zSql = sqlite3_mprintf(
158870 "SELECT %s, ota_control FROM %s.'ota_tmp_%q' ORDER BY %s%s",
158871 zCollist, p->zStateDb, pIter->zTbl,
158872 zCollist, zLimit
158873 );
158874 }else{
158875 zSql = sqlite3_mprintf(
158876 "SELECT %s, ota_control FROM 'data_%q' "
158877 "WHERE typeof(ota_control)='integer' AND ota_control!=1 "
158878 "UNION ALL "
158879 "SELECT %s, ota_control FROM %s.'ota_tmp_%q' "
158880 "ORDER BY %s%s",
158881 zCollist, pIter->zTbl,
158882 zCollist, p->zStateDb, pIter->zTbl,
158883 zCollist, zLimit
158884 );
158885 }
158886 p->rc = prepareFreeAndCollectError(p->dbOta, &pIter->pSelect, pz, zSql);
158887 }
158888
158889 sqlite3_free(zImposterCols);
158890 sqlite3_free(zImposterPK);
158891 sqlite3_free(zWhere);
158892 sqlite3_free(zBind);
158893 }else{
158894 int bOtaRowid = (pIter->eType==OTA_PK_VTAB || pIter->eType==OTA_PK_NONE);
158895 const char *zTbl = pIter->zTbl; /* Table this step applies to */
158896 const char *zWrite; /* Imposter table name */
158897
158898 char *zBindings = otaObjIterGetBindlist(p, pIter->nTblCol + bOtaRowid);
158899 char *zWhere = otaObjIterGetWhere(p, pIter);
158900 char *zOldlist = otaObjIterGetOldlist(p, pIter, "old");
158901 char *zNewlist = otaObjIterGetOldlist(p, pIter, "new");
158902
158903 zCollist = otaObjIterGetCollist(p, pIter);
158904 pIter->nCol = pIter->nTblCol;
158905
158906 /* Create the SELECT statement to read keys from data_xxx */
158907 if( p->rc==SQLITE_OK ){
158908 p->rc = prepareFreeAndCollectError(p->dbOta, &pIter->pSelect, pz,
158909 sqlite3_mprintf(
158910 "SELECT %s, ota_control%s FROM 'data_%q'%s",
158911 zCollist, (bOtaRowid ? ", ota_rowid" : ""), zTbl, zLimit
158912 )
158913 );
158914 }
158915
158916 /* Create the imposter table or tables (if required). */
158917 otaCreateImposterTable(p, pIter);
158918 otaCreateImposterTable2(p, pIter);
158919 zWrite = (pIter->eType==OTA_PK_VTAB ? "" : "ota_imp_");
158920
158921 /* Create the INSERT statement to write to the target PK b-tree */
158922 if( p->rc==SQLITE_OK ){
158923 p->rc = prepareFreeAndCollectError(p->dbMain, &pIter->pInsert, pz,
158924 sqlite3_mprintf(
158925 "INSERT INTO \"%s%w\"(%s%s) VALUES(%s)",
158926 zWrite, zTbl, zCollist, (bOtaRowid ? ", _rowid_" : ""), zBindings
158927 )
158928 );
158929 }
158930
158931 /* Create the DELETE statement to write to the target PK b-tree */
@@ -158936,55 +158942,55 @@
158936 )
158937 );
158938 }
158939
158940 if( pIter->abIndexed ){
158941 const char *zOtaRowid = "";
158942 if( pIter->eType==OTA_PK_EXTERNAL || pIter->eType==OTA_PK_NONE ){
158943 zOtaRowid = ", ota_rowid";
158944 }
158945
158946 /* Create the ota_tmp_xxx table and the triggers to populate it. */
158947 otaMPrintfExec(p, p->dbOta,
158948 "CREATE TABLE IF NOT EXISTS %s.'ota_tmp_%q' AS "
158949 "SELECT *%s FROM 'data_%q' WHERE 0;"
158950 , p->zStateDb
158951 , zTbl, (pIter->eType==OTA_PK_EXTERNAL ? ", 0 AS ota_rowid" : "")
158952 , zTbl
158953 );
158954
158955 otaMPrintfExec(p, p->dbMain,
158956 "CREATE TEMP TRIGGER ota_delete_tr BEFORE DELETE ON \"%s%w\" "
158957 "BEGIN "
158958 " SELECT ota_tmp_insert(2, %s);"
158959 "END;"
158960
158961 "CREATE TEMP TRIGGER ota_update1_tr BEFORE UPDATE ON \"%s%w\" "
158962 "BEGIN "
158963 " SELECT ota_tmp_insert(2, %s);"
158964 "END;"
158965
158966 "CREATE TEMP TRIGGER ota_update2_tr AFTER UPDATE ON \"%s%w\" "
158967 "BEGIN "
158968 " SELECT ota_tmp_insert(3, %s);"
158969 "END;",
158970 zWrite, zTbl, zOldlist,
158971 zWrite, zTbl, zOldlist,
158972 zWrite, zTbl, zNewlist
158973 );
158974
158975 if( pIter->eType==OTA_PK_EXTERNAL || pIter->eType==OTA_PK_NONE ){
158976 otaMPrintfExec(p, p->dbMain,
158977 "CREATE TEMP TRIGGER ota_insert_tr AFTER INSERT ON \"%s%w\" "
158978 "BEGIN "
158979 " SELECT ota_tmp_insert(0, %s);"
158980 "END;",
158981 zWrite, zTbl, zNewlist
158982 );
158983 }
158984
158985 otaObjIterPrepareTmpInsert(p, pIter, zCollist, zOtaRowid);
158986 }
158987
158988 sqlite3_free(zWhere);
158989 sqlite3_free(zOldlist);
158990 sqlite3_free(zNewlist);
@@ -158999,68 +159005,68 @@
158999
159000 /*
159001 ** Set output variable *ppStmt to point to an UPDATE statement that may
159002 ** be used to update the imposter table for the main table b-tree of the
159003 ** table object that pIter currently points to, assuming that the
159004 ** ota_control column of the data_xyz table contains zMask.
159005 **
159006 ** If the zMask string does not specify any columns to update, then this
159007 ** is not an error. Output variable *ppStmt is set to NULL in this case.
159008 */
159009 static int otaGetUpdateStmt(
159010 sqlite3ota *p, /* OTA handle */
159011 OtaObjIter *pIter, /* Object iterator */
159012 const char *zMask, /* ota_control value ('x.x.') */
159013 sqlite3_stmt **ppStmt /* OUT: UPDATE statement handle */
159014 ){
159015 OtaUpdateStmt **pp;
159016 OtaUpdateStmt *pUp = 0;
159017 int nUp = 0;
159018
159019 /* In case an error occurs */
159020 *ppStmt = 0;
159021
159022 /* Search for an existing statement. If one is found, shift it to the front
159023 ** of the LRU queue and return immediately. Otherwise, leave nUp pointing
159024 ** to the number of statements currently in the cache and pUp to the
159025 ** last object in the list. */
159026 for(pp=&pIter->pOtaUpdate; *pp; pp=&((*pp)->pNext)){
159027 pUp = *pp;
159028 if( strcmp(pUp->zMask, zMask)==0 ){
159029 *pp = pUp->pNext;
159030 pUp->pNext = pIter->pOtaUpdate;
159031 pIter->pOtaUpdate = pUp;
159032 *ppStmt = pUp->pUpdate;
159033 return SQLITE_OK;
159034 }
159035 nUp++;
159036 }
159037 assert( pUp==0 || pUp->pNext==0 );
159038
159039 if( nUp>=SQLITE_OTA_UPDATE_CACHESIZE ){
159040 for(pp=&pIter->pOtaUpdate; *pp!=pUp; pp=&((*pp)->pNext));
159041 *pp = 0;
159042 sqlite3_finalize(pUp->pUpdate);
159043 pUp->pUpdate = 0;
159044 }else{
159045 pUp = (OtaUpdateStmt*)otaMalloc(p, sizeof(OtaUpdateStmt)+pIter->nTblCol+1);
159046 }
159047
159048 if( pUp ){
159049 char *zWhere = otaObjIterGetWhere(p, pIter);
159050 char *zSet = otaObjIterGetSetlist(p, pIter, zMask);
159051 char *zUpdate = 0;
159052
159053 pUp->zMask = (char*)&pUp[1];
159054 memcpy(pUp->zMask, zMask, pIter->nTblCol);
159055 pUp->pNext = pIter->pOtaUpdate;
159056 pIter->pOtaUpdate = pUp;
159057
159058 if( zSet ){
159059 const char *zPrefix = "";
159060
159061 if( pIter->eType!=OTA_PK_VTAB ) zPrefix = "ota_imp_";
159062 zUpdate = sqlite3_mprintf("UPDATE \"%s%w\" SET %s WHERE %s",
159063 zPrefix, pIter->zTbl, zSet, zWhere
159064 );
159065 p->rc = prepareFreeAndCollectError(
159066 p->dbMain, &pUp->pUpdate, &p->zErrmsg, zUpdate
@@ -159072,11 +159078,11 @@
159072 }
159073
159074 return p->rc;
159075 }
159076
159077 static sqlite3 *otaOpenDbhandle(sqlite3ota *p, const char *zName){
159078 sqlite3 *db = 0;
159079 if( p->rc==SQLITE_OK ){
159080 const int flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_URI;
159081 p->rc = sqlite3_open_v2(zName, &db, flags, p->zVfsName);
159082 if( p->rc ){
@@ -159087,51 +159093,51 @@
159087 }
159088 return db;
159089 }
159090
159091 /*
159092 ** Open the database handle and attach the OTA database as "ota". If an
159093 ** error occurs, leave an error code and message in the OTA handle.
159094 */
159095 static void otaOpenDatabase(sqlite3ota *p){
159096 assert( p->rc==SQLITE_OK );
159097 assert( p->dbMain==0 && p->dbOta==0 );
159098
159099 p->eStage = 0;
159100 p->dbMain = otaOpenDbhandle(p, p->zTarget);
159101 p->dbOta = otaOpenDbhandle(p, p->zOta);
159102
159103 /* If using separate OTA and state databases, attach the state database to
159104 ** the OTA db handle now. */
159105 if( p->zState ){
159106 otaMPrintfExec(p, p->dbOta, "ATTACH %Q AS stat", p->zState);
159107 memcpy(p->zStateDb, "stat", 4);
159108 }else{
159109 memcpy(p->zStateDb, "main", 4);
159110 }
159111
159112 if( p->rc==SQLITE_OK ){
159113 p->rc = sqlite3_create_function(p->dbMain,
159114 "ota_tmp_insert", -1, SQLITE_UTF8, (void*)p, otaTmpInsertFunc, 0, 0
159115 );
159116 }
159117
159118 if( p->rc==SQLITE_OK ){
159119 p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_OTA, (void*)p);
159120 }
159121 otaMPrintfExec(p, p->dbMain, "SELECT * FROM sqlite_master");
159122
159123 /* Mark the database file just opened as an OTA target database. If
159124 ** this call returns SQLITE_NOTFOUND, then the OTA vfs is not in use.
159125 ** This is an error. */
159126 if( p->rc==SQLITE_OK ){
159127 p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_OTA, (void*)p);
159128 }
159129
159130 if( p->rc==SQLITE_NOTFOUND ){
159131 p->rc = SQLITE_ERROR;
159132 p->zErrmsg = sqlite3_mprintf("ota vfs not found");
159133 }
159134 }
159135
159136 /*
159137 ** This routine is a copy of the sqlite3FileSuffix3() routine from the core.
@@ -159151,11 +159157,11 @@
159151 ** test.db-journal => test.nal
159152 ** test.db-wal => test.wal
159153 ** test.db-shm => test.shm
159154 ** test.db-mj7f3319fa => test.9fa
159155 */
159156 static void otaFileSuffix3(const char *zBase, char *z){
159157 #ifdef SQLITE_ENABLE_8_3_NAMES
159158 #if SQLITE_ENABLE_8_3_NAMES<2
159159 if( sqlite3_uri_boolean(zBase, "8_3_names", 0) )
159160 #endif
159161 {
@@ -159172,11 +159178,11 @@
159172 ** as a 64-bit integer.
159173 **
159174 ** The checksum is store in the first page of xShmMap memory as an 8-byte
159175 ** blob starting at byte offset 40.
159176 */
159177 static i64 otaShmChecksum(sqlite3ota *p){
159178 i64 iRet = 0;
159179 if( p->rc==SQLITE_OK ){
159180 sqlite3_file *pDb = p->pTargetFd->pReal;
159181 u32 volatile *ptr;
159182 p->rc = pDb->pMethods->xShmMap(pDb, 0, 32*1024, 0, (void volatile**)&ptr);
@@ -159189,23 +159195,23 @@
159189
159190 /*
159191 ** This function is called as part of initializing or reinitializing an
159192 ** incremental checkpoint.
159193 **
159194 ** It populates the sqlite3ota.aFrame[] array with the set of
159195 ** (wal frame -> db page) copy operations required to checkpoint the
159196 ** current wal file, and obtains the set of shm locks required to safely
159197 ** perform the copy operations directly on the file-system.
159198 **
159199 ** If argument pState is not NULL, then the incremental checkpoint is
159200 ** being resumed. In this case, if the checksum of the wal-index-header
159201 ** following recovery is not the same as the checksum saved in the OtaState
159202 ** object, then the ota handle is set to DONE state. This occurs if some
159203 ** other client appends a transaction to the wal file in the middle of
159204 ** an incremental checkpoint.
159205 */
159206 static void otaSetupCheckpoint(sqlite3ota *p, OtaState *pState){
159207
159208 /* If pState is NULL, then the wal file may not have been opened and
159209 ** recovered. Running a read-statement here to ensure that doing so
159210 ** does not interfere with the "capture" process below. */
159211 if( pState==0 ){
@@ -159214,20 +159220,20 @@
159214 p->rc = sqlite3_exec(p->dbMain, "SELECT * FROM sqlite_master", 0, 0, 0);
159215 }
159216 }
159217
159218 /* Assuming no error has occurred, run a "restart" checkpoint with the
159219 ** sqlite3ota.eStage variable set to CAPTURE. This turns on the following
159220 ** special behaviour in the ota VFS:
159221 **
159222 ** * If the exclusive shm WRITER or READ0 lock cannot be obtained,
159223 ** the checkpoint fails with SQLITE_BUSY (normally SQLite would
159224 ** proceed with running a passive checkpoint instead of failing).
159225 **
159226 ** * Attempts to read from the *-wal file or write to the database file
159227 ** do not perform any IO. Instead, the frame/page combinations that
159228 ** would be read/written are recorded in the sqlite3ota.aFrame[]
159229 ** array.
159230 **
159231 ** * Calls to xShmLock(UNLOCK) to release the exclusive shm WRITER,
159232 ** READ0 and CHECKPOINT locks taken as part of the checkpoint are
159233 ** no-ops. These locks will not be released until the connection
@@ -159243,76 +159249,76 @@
159243 ** data from the wal file into the database file according to the
159244 ** contents of aFrame[].
159245 */
159246 if( p->rc==SQLITE_OK ){
159247 int rc2;
159248 p->eStage = OTA_STAGE_CAPTURE;
159249 rc2 = sqlite3_exec(p->dbMain, "PRAGMA main.wal_checkpoint=restart", 0, 0,0);
159250 if( rc2!=SQLITE_INTERNAL ) p->rc = rc2;
159251 }
159252
159253 if( p->rc==SQLITE_OK ){
159254 p->eStage = OTA_STAGE_CKPT;
159255 p->nStep = (pState ? pState->nRow : 0);
159256 p->aBuf = otaMalloc(p, p->pgsz);
159257 p->iWalCksum = otaShmChecksum(p);
159258 }
159259
159260 if( p->rc==SQLITE_OK && pState && pState->iWalCksum!=p->iWalCksum ){
159261 p->rc = SQLITE_DONE;
159262 p->eStage = OTA_STAGE_DONE;
159263 }
159264 }
159265
159266 /*
159267 ** Called when iAmt bytes are read from offset iOff of the wal file while
159268 ** the ota object is in capture mode. Record the frame number of the frame
159269 ** being read in the aFrame[] array.
159270 */
159271 static int otaCaptureWalRead(sqlite3ota *pOta, i64 iOff, int iAmt){
159272 const u32 mReq = (1<<WAL_LOCK_WRITE)|(1<<WAL_LOCK_CKPT)|(1<<WAL_LOCK_READ0);
159273 u32 iFrame;
159274
159275 if( pOta->mLock!=mReq ){
159276 pOta->rc = SQLITE_BUSY;
159277 return SQLITE_INTERNAL;
159278 }
159279
159280 pOta->pgsz = iAmt;
159281 if( pOta->nFrame==pOta->nFrameAlloc ){
159282 int nNew = (pOta->nFrameAlloc ? pOta->nFrameAlloc : 64) * 2;
159283 OtaFrame *aNew;
159284 aNew = (OtaFrame*)sqlite3_realloc(pOta->aFrame, nNew * sizeof(OtaFrame));
159285 if( aNew==0 ) return SQLITE_NOMEM;
159286 pOta->aFrame = aNew;
159287 pOta->nFrameAlloc = nNew;
159288 }
159289
159290 iFrame = (u32)((iOff-32) / (i64)(iAmt+24)) + 1;
159291 if( pOta->iMaxFrame<iFrame ) pOta->iMaxFrame = iFrame;
159292 pOta->aFrame[pOta->nFrame].iWalFrame = iFrame;
159293 pOta->aFrame[pOta->nFrame].iDbPage = 0;
159294 pOta->nFrame++;
159295 return SQLITE_OK;
159296 }
159297
159298 /*
159299 ** Called when a page of data is written to offset iOff of the database
159300 ** file while the ota handle is in capture mode. Record the page number
159301 ** of the page being written in the aFrame[] array.
159302 */
159303 static int otaCaptureDbWrite(sqlite3ota *pOta, i64 iOff){
159304 pOta->aFrame[pOta->nFrame-1].iDbPage = (u32)(iOff / pOta->pgsz) + 1;
159305 return SQLITE_OK;
159306 }
159307
159308 /*
159309 ** This is called as part of an incremental checkpoint operation. Copy
159310 ** a single frame of data from the wal file into the database file, as
159311 ** indicated by the OtaFrame object.
159312 */
159313 static void otaCheckpointFrame(sqlite3ota *p, OtaFrame *pFrame){
159314 sqlite3_file *pWal = p->pTargetFd->pWalFd->pReal;
159315 sqlite3_file *pDb = p->pTargetFd->pReal;
159316 i64 iOff;
159317
159318 assert( p->rc==SQLITE_OK );
@@ -159326,33 +159332,33 @@
159326
159327
159328 /*
159329 ** Take an EXCLUSIVE lock on the database file.
159330 */
159331 static void otaLockDatabase(sqlite3ota *p){
159332 sqlite3_file *pReal = p->pTargetFd->pReal;
159333 assert( p->rc==SQLITE_OK );
159334 p->rc = pReal->pMethods->xLock(pReal, SQLITE_LOCK_SHARED);
159335 if( p->rc==SQLITE_OK ){
159336 p->rc = pReal->pMethods->xLock(pReal, SQLITE_LOCK_EXCLUSIVE);
159337 }
159338 }
159339
159340 /*
159341 ** The OTA handle is currently in OTA_STAGE_OAL state, with a SHARED lock
159342 ** on the database file. This proc moves the *-oal file to the *-wal path,
159343 ** then reopens the database file (this time in vanilla, non-oal, WAL mode).
159344 ** If an error occurs, leave an error code and error message in the ota
159345 ** handle.
159346 */
159347 static void otaMoveOalFile(sqlite3ota *p){
159348 const char *zBase = sqlite3_db_filename(p->dbMain, "main");
159349
159350 char *zWal = sqlite3_mprintf("%s-wal", zBase);
159351 char *zOal = sqlite3_mprintf("%s-oal", zBase);
159352
159353 assert( p->eStage==OTA_STAGE_MOVE );
159354 assert( p->rc==SQLITE_OK && p->zErrmsg==0 );
159355 if( zWal==0 || zOal==0 ){
159356 p->rc = SQLITE_NOMEM;
159357 }else{
159358 /* Move the *-oal file to *-wal. At this point connection p->db is
@@ -159360,25 +159366,25 @@
159360 ** in WAL mode). So no other connection may be writing the db.
159361 **
159362 ** In order to ensure that there are no database readers, an EXCLUSIVE
159363 ** lock is obtained here before the *-oal is moved to *-wal.
159364 */
159365 otaLockDatabase(p);
159366 if( p->rc==SQLITE_OK ){
159367 otaFileSuffix3(zBase, zWal);
159368 otaFileSuffix3(zBase, zOal);
159369
159370 /* Re-open the databases. */
159371 otaObjIterFinalize(&p->objiter);
159372 sqlite3_close(p->dbMain);
159373 sqlite3_close(p->dbOta);
159374 p->rc = rename(zOal, zWal) ? SQLITE_IOERR : SQLITE_OK;
159375 if( p->rc==SQLITE_OK ){
159376 p->dbMain = 0;
159377 p->dbOta = 0;
159378 otaOpenDatabase(p);
159379 otaSetupCheckpoint(p, 0);
159380 }
159381 }
159382 }
159383
159384 sqlite3_free(zWal);
@@ -159389,36 +159395,36 @@
159389 ** The SELECT statement iterating through the keys for the current object
159390 ** (p->objiter.pSelect) currently points to a valid row. This function
159391 ** determines the type of operation requested by this row and returns
159392 ** one of the following values to indicate the result:
159393 **
159394 ** * OTA_INSERT
159395 ** * OTA_DELETE
159396 ** * OTA_IDX_DELETE
159397 ** * OTA_UPDATE
159398 **
159399 ** If OTA_UPDATE is returned, then output variable *pzMask is set to
159400 ** point to the text value indicating the columns to update.
159401 **
159402 ** If the ota_control field contains an invalid value, an error code and
159403 ** message are left in the OTA handle and zero returned.
159404 */
159405 static int otaStepType(sqlite3ota *p, const char **pzMask){
159406 int iCol = p->objiter.nCol; /* Index of ota_control column */
159407 int res = 0; /* Return value */
159408
159409 switch( sqlite3_column_type(p->objiter.pSelect, iCol) ){
159410 case SQLITE_INTEGER: {
159411 int iVal = sqlite3_column_int(p->objiter.pSelect, iCol);
159412 if( iVal==0 ){
159413 res = OTA_INSERT;
159414 }else if( iVal==1 ){
159415 res = OTA_DELETE;
159416 }else if( iVal==2 ){
159417 res = OTA_IDX_DELETE;
159418 }else if( iVal==3 ){
159419 res = OTA_IDX_INSERT;
159420 }
159421 break;
159422 }
159423
159424 case SQLITE_TEXT: {
@@ -159426,21 +159432,21 @@
159426 if( z==0 ){
159427 p->rc = SQLITE_NOMEM;
159428 }else{
159429 *pzMask = (const char*)z;
159430 }
159431 res = OTA_UPDATE;
159432
159433 break;
159434 }
159435
159436 default:
159437 break;
159438 }
159439
159440 if( res==0 ){
159441 otaBadControlError(p);
159442 }
159443 return res;
159444 }
159445
159446 #ifdef SQLITE_DEBUG
@@ -159454,82 +159460,82 @@
159454 #else
159455 # define assertColumnName(x,y,z)
159456 #endif
159457
159458 /*
159459 ** This function does the work for an sqlite3ota_step() call.
159460 **
159461 ** The object-iterator (p->objiter) currently points to a valid object,
159462 ** and the input cursor (p->objiter.pSelect) currently points to a valid
159463 ** input row. Perform whatever processing is required and return.
159464 **
159465 ** If no error occurs, SQLITE_OK is returned. Otherwise, an error code
159466 ** and message is left in the OTA handle and a copy of the error code
159467 ** returned.
159468 */
159469 static int otaStep(sqlite3ota *p){
159470 OtaObjIter *pIter = &p->objiter;
159471 const char *zMask = 0;
159472 int i;
159473 int eType = otaStepType(p, &zMask);
159474
159475 if( eType ){
159476 assert( eType!=OTA_UPDATE || pIter->zIdx==0 );
159477
159478 if( pIter->zIdx==0 && eType==OTA_IDX_DELETE ){
159479 otaBadControlError(p);
159480 }
159481 else if(
159482 eType==OTA_INSERT
159483 || eType==OTA_DELETE
159484 || eType==OTA_IDX_DELETE
159485 || eType==OTA_IDX_INSERT
159486 ){
159487 sqlite3_value *pVal;
159488 sqlite3_stmt *pWriter;
159489
159490 assert( eType!=OTA_UPDATE );
159491 assert( eType!=OTA_DELETE || pIter->zIdx==0 );
159492
159493 if( eType==OTA_IDX_DELETE || eType==OTA_DELETE ){
159494 pWriter = pIter->pDelete;
159495 }else{
159496 pWriter = pIter->pInsert;
159497 }
159498
159499 for(i=0; i<pIter->nCol; i++){
159500 /* If this is an INSERT into a table b-tree and the table has an
159501 ** explicit INTEGER PRIMARY KEY, check that this is not an attempt
159502 ** to write a NULL into the IPK column. That is not permitted. */
159503 if( eType==OTA_INSERT
159504 && pIter->zIdx==0 && pIter->eType==OTA_PK_IPK && pIter->abTblPk[i]
159505 && sqlite3_column_type(pIter->pSelect, i)==SQLITE_NULL
159506 ){
159507 p->rc = SQLITE_MISMATCH;
159508 p->zErrmsg = sqlite3_mprintf("datatype mismatch");
159509 goto step_out;
159510 }
159511
159512 if( eType==OTA_DELETE && pIter->abTblPk[i]==0 ){
159513 continue;
159514 }
159515
159516 pVal = sqlite3_column_value(pIter->pSelect, i);
159517 p->rc = sqlite3_bind_value(pWriter, i+1, pVal);
159518 if( p->rc ) goto step_out;
159519 }
159520 if( pIter->zIdx==0
159521 && (pIter->eType==OTA_PK_VTAB || pIter->eType==OTA_PK_NONE)
159522 ){
159523 /* For a virtual table, or a table with no primary key, the
159524 ** SELECT statement is:
159525 **
159526 ** SELECT <cols>, ota_control, ota_rowid FROM ....
159527 **
159528 ** Hence column_value(pIter->nCol+1).
159529 */
159530 assertColumnName(pIter->pSelect, pIter->nCol+1, "ota_rowid");
159531 pVal = sqlite3_column_value(pIter->pSelect, pIter->nCol+1);
159532 p->rc = sqlite3_bind_value(pWriter, pIter->nCol+1, pVal);
159533 }
159534 if( p->rc==SQLITE_OK ){
159535 sqlite3_step(pWriter);
@@ -159536,25 +159542,25 @@
159536 p->rc = resetAndCollectError(pWriter, &p->zErrmsg);
159537 }
159538 }else{
159539 sqlite3_value *pVal;
159540 sqlite3_stmt *pUpdate = 0;
159541 assert( eType==OTA_UPDATE );
159542 otaGetUpdateStmt(p, pIter, zMask, &pUpdate);
159543 if( pUpdate ){
159544 for(i=0; p->rc==SQLITE_OK && i<pIter->nCol; i++){
159545 char c = zMask[pIter->aiSrcOrder[i]];
159546 pVal = sqlite3_column_value(pIter->pSelect, i);
159547 if( pIter->abTblPk[i] || c=='x' || c=='d' ){
159548 p->rc = sqlite3_bind_value(pUpdate, i+1, pVal);
159549 }
159550 }
159551 if( p->rc==SQLITE_OK
159552 && (pIter->eType==OTA_PK_VTAB || pIter->eType==OTA_PK_NONE)
159553 ){
159554 /* Bind the ota_rowid value to column _rowid_ */
159555 assertColumnName(pIter->pSelect, pIter->nCol+1, "ota_rowid");
159556 pVal = sqlite3_column_value(pIter->pSelect, pIter->nCol+1);
159557 p->rc = sqlite3_bind_value(pUpdate, pIter->nCol+1, pVal);
159558 }
159559 if( p->rc==SQLITE_OK ){
159560 sqlite3_step(pUpdate);
@@ -159569,11 +159575,11 @@
159569 }
159570
159571 /*
159572 ** Increment the schema cookie of the main database opened by p->dbMain.
159573 */
159574 static void otaIncrSchemaCookie(sqlite3ota *p){
159575 if( p->rc==SQLITE_OK ){
159576 int iCookie = 1000000;
159577 sqlite3_stmt *pStmt;
159578
159579 p->rc = prepareAndCollectError(p->dbMain, &pStmt, &p->zErrmsg,
@@ -159586,49 +159592,49 @@
159586 ** statement reads is page 1, which is guaranteed to be in the cache.
159587 ** And no memory allocations are required. */
159588 if( SQLITE_ROW==sqlite3_step(pStmt) ){
159589 iCookie = sqlite3_column_int(pStmt, 0);
159590 }
159591 otaFinalize(p, pStmt);
159592 }
159593 if( p->rc==SQLITE_OK ){
159594 otaMPrintfExec(p, p->dbMain, "PRAGMA schema_version = %d", iCookie+1);
159595 }
159596 }
159597 }
159598
159599 /*
159600 ** Update the contents of the ota_state table within the ota database. The
159601 ** value stored in the OTA_STATE_STAGE column is eStage. All other values
159602 ** are determined by inspecting the ota handle passed as the first argument.
159603 */
159604 static void otaSaveState(sqlite3ota *p, int eStage){
159605 if( p->rc==SQLITE_OK || p->rc==SQLITE_DONE ){
159606 sqlite3_stmt *pInsert = 0;
159607 int rc;
159608
159609 assert( p->zErrmsg==0 );
159610 rc = prepareFreeAndCollectError(p->dbOta, &pInsert, &p->zErrmsg,
159611 sqlite3_mprintf(
159612 "INSERT OR REPLACE INTO %s.ota_state(k, v) VALUES "
159613 "(%d, %d), "
159614 "(%d, %Q), "
159615 "(%d, %Q), "
159616 "(%d, %d), "
159617 "(%d, %d), "
159618 "(%d, %lld), "
159619 "(%d, %lld), "
159620 "(%d, %lld) ",
159621 p->zStateDb,
159622 OTA_STATE_STAGE, eStage,
159623 OTA_STATE_TBL, p->objiter.zTbl,
159624 OTA_STATE_IDX, p->objiter.zIdx,
159625 OTA_STATE_ROW, p->nStep,
159626 OTA_STATE_PROGRESS, p->nProgress,
159627 OTA_STATE_CKPT, p->iWalCksum,
159628 OTA_STATE_COOKIE, (i64)p->pTargetFd->iCookie,
159629 OTA_STATE_OALSZ, p->iOalSz
159630 )
159631 );
159632 assert( pInsert==0 || rc==SQLITE_OK );
159633
159634 if( rc==SQLITE_OK ){
@@ -159639,71 +159645,71 @@
159639 }
159640 }
159641
159642
159643 /*
159644 ** Step the OTA object.
159645 */
159646 SQLITE_API int SQLITE_STDCALL sqlite3ota_step(sqlite3ota *p){
159647 if( p ){
159648 switch( p->eStage ){
159649 case OTA_STAGE_OAL: {
159650 OtaObjIter *pIter = &p->objiter;
159651 while( p->rc==SQLITE_OK && pIter->zTbl ){
159652
159653 if( pIter->bCleanup ){
159654 /* Clean up the ota_tmp_xxx table for the previous table. It
159655 ** cannot be dropped as there are currently active SQL statements.
159656 ** But the contents can be deleted. */
159657 if( pIter->abIndexed ){
159658 otaMPrintfExec(p, p->dbOta,
159659 "DELETE FROM %s.'ota_tmp_%q'", p->zStateDb, pIter->zTbl
159660 );
159661 }
159662 }else{
159663 otaObjIterPrepareAll(p, pIter, 0);
159664
159665 /* Advance to the next row to process. */
159666 if( p->rc==SQLITE_OK ){
159667 int rc = sqlite3_step(pIter->pSelect);
159668 if( rc==SQLITE_ROW ){
159669 p->nProgress++;
159670 p->nStep++;
159671 return otaStep(p);
159672 }
159673 p->rc = sqlite3_reset(pIter->pSelect);
159674 p->nStep = 0;
159675 }
159676 }
159677
159678 otaObjIterNext(p, pIter);
159679 }
159680
159681 if( p->rc==SQLITE_OK ){
159682 assert( pIter->zTbl==0 );
159683 otaSaveState(p, OTA_STAGE_MOVE);
159684 otaIncrSchemaCookie(p);
159685 if( p->rc==SQLITE_OK ){
159686 p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
159687 }
159688 if( p->rc==SQLITE_OK ){
159689 p->rc = sqlite3_exec(p->dbOta, "COMMIT", 0, 0, &p->zErrmsg);
159690 }
159691 p->eStage = OTA_STAGE_MOVE;
159692 }
159693 break;
159694 }
159695
159696 case OTA_STAGE_MOVE: {
159697 if( p->rc==SQLITE_OK ){
159698 otaMoveOalFile(p);
159699 p->nProgress++;
159700 }
159701 break;
159702 }
159703
159704 case OTA_STAGE_CKPT: {
159705 if( p->rc==SQLITE_OK ){
159706 if( p->nStep>=p->nFrame ){
159707 sqlite3_file *pDb = p->pTargetFd->pReal;
159708
159709 /* Sync the db file */
@@ -159717,16 +159723,16 @@
159717 ((u32 volatile*)ptr)[24] = p->iMaxFrame;
159718 }
159719 }
159720
159721 if( p->rc==SQLITE_OK ){
159722 p->eStage = OTA_STAGE_DONE;
159723 p->rc = SQLITE_DONE;
159724 }
159725 }else{
159726 OtaFrame *pFrame = &p->aFrame[p->nStep];
159727 otaCheckpointFrame(p, pFrame);
159728 p->nStep++;
159729 }
159730 p->nProgress++;
159731 }
159732 break;
@@ -159740,78 +159746,78 @@
159740 return SQLITE_NOMEM;
159741 }
159742 }
159743
159744 /*
159745 ** Free an OtaState object allocated by otaLoadState().
159746 */
159747 static void otaFreeState(OtaState *p){
159748 if( p ){
159749 sqlite3_free(p->zTbl);
159750 sqlite3_free(p->zIdx);
159751 sqlite3_free(p);
159752 }
159753 }
159754
159755 /*
159756 ** Allocate an OtaState object and load the contents of the ota_state
159757 ** table into it. Return a pointer to the new object. It is the
159758 ** responsibility of the caller to eventually free the object using
159759 ** sqlite3_free().
159760 **
159761 ** If an error occurs, leave an error code and message in the ota handle
159762 ** and return NULL.
159763 */
159764 static OtaState *otaLoadState(sqlite3ota *p){
159765 OtaState *pRet = 0;
159766 sqlite3_stmt *pStmt = 0;
159767 int rc;
159768 int rc2;
159769
159770 pRet = (OtaState*)otaMalloc(p, sizeof(OtaState));
159771 if( pRet==0 ) return 0;
159772
159773 rc = prepareFreeAndCollectError(p->dbOta, &pStmt, &p->zErrmsg,
159774 sqlite3_mprintf("SELECT k, v FROM %s.ota_state", p->zStateDb)
159775 );
159776 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
159777 switch( sqlite3_column_int(pStmt, 0) ){
159778 case OTA_STATE_STAGE:
159779 pRet->eStage = sqlite3_column_int(pStmt, 1);
159780 if( pRet->eStage!=OTA_STAGE_OAL
159781 && pRet->eStage!=OTA_STAGE_MOVE
159782 && pRet->eStage!=OTA_STAGE_CKPT
159783 ){
159784 p->rc = SQLITE_CORRUPT;
159785 }
159786 break;
159787
159788 case OTA_STATE_TBL:
159789 pRet->zTbl = otaStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
159790 break;
159791
159792 case OTA_STATE_IDX:
159793 pRet->zIdx = otaStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
159794 break;
159795
159796 case OTA_STATE_ROW:
159797 pRet->nRow = sqlite3_column_int(pStmt, 1);
159798 break;
159799
159800 case OTA_STATE_PROGRESS:
159801 pRet->nProgress = sqlite3_column_int64(pStmt, 1);
159802 break;
159803
159804 case OTA_STATE_CKPT:
159805 pRet->iWalCksum = sqlite3_column_int64(pStmt, 1);
159806 break;
159807
159808 case OTA_STATE_COOKIE:
159809 pRet->iCookie = (u32)sqlite3_column_int64(pStmt, 1);
159810 break;
159811
159812 case OTA_STATE_OALSZ:
159813 pRet->iOalSz = (u32)sqlite3_column_int64(pStmt, 1);
159814 break;
159815
159816 default:
159817 rc = SQLITE_CORRUPT;
@@ -159828,142 +159834,142 @@
159828 /*
159829 ** Compare strings z1 and z2, returning 0 if they are identical, or non-zero
159830 ** otherwise. Either or both argument may be NULL. Two NULL values are
159831 ** considered equal, and NULL is considered distinct from all other values.
159832 */
159833 static int otaStrCompare(const char *z1, const char *z2){
159834 if( z1==0 && z2==0 ) return 0;
159835 if( z1==0 || z2==0 ) return 1;
159836 return (sqlite3_stricmp(z1, z2)!=0);
159837 }
159838
159839 /*
159840 ** This function is called as part of sqlite3ota_open() when initializing
159841 ** an ota handle in OAL stage. If the ota update has not started (i.e.
159842 ** the ota_state table was empty) it is a no-op. Otherwise, it arranges
159843 ** things so that the next call to sqlite3ota_step() continues on from
159844 ** where the previous ota handle left off.
159845 **
159846 ** If an error occurs, an error code and error message are left in the
159847 ** ota handle passed as the first argument.
159848 */
159849 static void otaSetupOal(sqlite3ota *p, OtaState *pState){
159850 assert( p->rc==SQLITE_OK );
159851 if( pState->zTbl ){
159852 OtaObjIter *pIter = &p->objiter;
159853 int rc = SQLITE_OK;
159854
159855 while( rc==SQLITE_OK && pIter->zTbl && (pIter->bCleanup
159856 || otaStrCompare(pIter->zIdx, pState->zIdx)
159857 || otaStrCompare(pIter->zTbl, pState->zTbl)
159858 )){
159859 rc = otaObjIterNext(p, pIter);
159860 }
159861
159862 if( rc==SQLITE_OK && !pIter->zTbl ){
159863 rc = SQLITE_ERROR;
159864 p->zErrmsg = sqlite3_mprintf("ota_state mismatch error");
159865 }
159866
159867 if( rc==SQLITE_OK ){
159868 p->nStep = pState->nRow;
159869 rc = otaObjIterPrepareAll(p, &p->objiter, p->nStep);
159870 }
159871
159872 p->rc = rc;
159873 }
159874 }
159875
159876 /*
159877 ** If there is a "*-oal" file in the file-system corresponding to the
159878 ** target database in the file-system, delete it. If an error occurs,
159879 ** leave an error code and error message in the ota handle.
159880 */
159881 static void otaDeleteOalFile(sqlite3ota *p){
159882 char *zOal = sqlite3_mprintf("%s-oal", p->zTarget);
159883 assert( p->rc==SQLITE_OK && p->zErrmsg==0 );
159884 unlink(zOal);
159885 sqlite3_free(zOal);
159886 }
159887
159888 /*
159889 ** Allocate a private ota VFS for the ota handle passed as the only
159890 ** argument. This VFS will be used unless the call to sqlite3ota_open()
159891 ** specified a URI with a vfs=? option in place of a target database
159892 ** file name.
159893 */
159894 static void otaCreateVfs(sqlite3ota *p){
159895 int rnd;
159896 char zRnd[64];
159897
159898 assert( p->rc==SQLITE_OK );
159899 sqlite3_randomness(sizeof(int), (void*)&rnd);
159900 sprintf(zRnd, "ota_vfs_%d", rnd);
159901 p->rc = sqlite3ota_create_vfs(zRnd, 0);
159902 if( p->rc==SQLITE_OK ){
159903 sqlite3_vfs *pVfs = sqlite3_vfs_find(zRnd);
159904 assert( pVfs );
159905 p->zVfsName = pVfs->zName;
159906 }
159907 }
159908
159909 /*
159910 ** Destroy the private VFS created for the ota handle passed as the only
159911 ** argument by an earlier call to otaCreateVfs().
159912 */
159913 static void otaDeleteVfs(sqlite3ota *p){
159914 if( p->zVfsName ){
159915 sqlite3ota_destroy_vfs(p->zVfsName);
159916 p->zVfsName = 0;
159917 }
159918 }
159919
159920 /*
159921 ** Open and return a new OTA handle.
159922 */
159923 SQLITE_API sqlite3ota *SQLITE_STDCALL sqlite3ota_open(
159924 const char *zTarget,
159925 const char *zOta,
159926 const char *zState
159927 ){
159928 sqlite3ota *p;
159929 int nTarget = strlen(zTarget);
159930 int nOta = strlen(zOta);
159931 int nState = zState ? strlen(zState) : 0;
159932
159933 p = (sqlite3ota*)sqlite3_malloc(sizeof(sqlite3ota)+nTarget+1+nOta+1+nState+1);
159934 if( p ){
159935 OtaState *pState = 0;
159936
159937 /* Create the custom VFS. */
159938 memset(p, 0, sizeof(sqlite3ota));
159939 otaCreateVfs(p);
159940
159941 /* Open the target database */
159942 if( p->rc==SQLITE_OK ){
159943 p->zTarget = (char*)&p[1];
159944 memcpy(p->zTarget, zTarget, nTarget+1);
159945 p->zOta = &p->zTarget[nTarget+1];
159946 memcpy(p->zOta, zOta, nOta+1);
159947 if( zState ){
159948 p->zState = &p->zOta[nOta+1];
159949 memcpy(p->zState, zState, nState+1);
159950 }
159951 otaOpenDatabase(p);
159952 }
159953
159954 /* If it has not already been created, create the ota_state table */
159955 otaMPrintfExec(p, p->dbOta, OTA_CREATE_STATE, p->zStateDb);
159956
159957 if( p->rc==SQLITE_OK ){
159958 pState = otaLoadState(p);
159959 assert( pState || p->rc!=SQLITE_OK );
159960 if( p->rc==SQLITE_OK ){
159961
159962 if( pState->eStage==0 ){
159963 otaDeleteOalFile(p);
159964 p->eStage = OTA_STAGE_OAL;
159965 }else{
159966 p->eStage = pState->eStage;
159967 }
159968 p->nProgress = pState->nProgress;
159969 p->iOalSz = pState->iOalSz;
@@ -159970,97 +159976,97 @@
159970 }
159971 }
159972 assert( p->rc!=SQLITE_OK || p->eStage!=0 );
159973
159974 if( p->rc==SQLITE_OK && p->pTargetFd->pWalFd ){
159975 if( p->eStage==OTA_STAGE_OAL ){
159976 p->rc = SQLITE_ERROR;
159977 p->zErrmsg = sqlite3_mprintf("cannot update wal mode database");
159978 }else if( p->eStage==OTA_STAGE_MOVE ){
159979 p->eStage = OTA_STAGE_CKPT;
159980 p->nStep = 0;
159981 }
159982 }
159983
159984 if( p->rc==SQLITE_OK
159985 && (p->eStage==OTA_STAGE_OAL || p->eStage==OTA_STAGE_MOVE)
159986 && pState->eStage!=0 && p->pTargetFd->iCookie!=pState->iCookie
159987 ){
159988 /* At this point (pTargetFd->iCookie) contains the value of the
159989 ** change-counter cookie (the thing that gets incremented when a
159990 ** transaction is committed in rollback mode) currently stored on
159991 ** page 1 of the database file. */
159992 p->rc = SQLITE_BUSY;
159993 p->zErrmsg = sqlite3_mprintf("database modified during ota update");
159994 }
159995
159996 if( p->rc==SQLITE_OK ){
159997 if( p->eStage==OTA_STAGE_OAL ){
159998
159999 /* Open transactions both databases. The *-oal file is opened or
160000 ** created at this point. */
160001 p->rc = sqlite3_exec(p->dbMain, "BEGIN IMMEDIATE", 0, 0, &p->zErrmsg);
160002 if( p->rc==SQLITE_OK ){
160003 p->rc = sqlite3_exec(p->dbOta, "BEGIN IMMEDIATE", 0, 0, &p->zErrmsg);
160004 }
160005
160006 /* Point the object iterator at the first object */
160007 if( p->rc==SQLITE_OK ){
160008 p->rc = otaObjIterFirst(p, &p->objiter);
160009 }
160010
160011 /* If the OTA database contains no data_xxx tables, declare the OTA
160012 ** update finished. */
160013 if( p->rc==SQLITE_OK && p->objiter.zTbl==0 ){
160014 p->rc = SQLITE_DONE;
160015 }
160016
160017 if( p->rc==SQLITE_OK ){
160018 otaSetupOal(p, pState);
160019 }
160020
160021 }else if( p->eStage==OTA_STAGE_MOVE ){
160022 /* no-op */
160023 }else if( p->eStage==OTA_STAGE_CKPT ){
160024 otaSetupCheckpoint(p, pState);
160025 }else if( p->eStage==OTA_STAGE_DONE ){
160026 p->rc = SQLITE_DONE;
160027 }else{
160028 p->rc = SQLITE_CORRUPT;
160029 }
160030 }
160031
160032 otaFreeState(pState);
160033 }
160034
160035 return p;
160036 }
160037
160038
160039 /*
160040 ** Return the database handle used by pOta.
160041 */
160042 SQLITE_API sqlite3 *SQLITE_STDCALL sqlite3ota_db(sqlite3ota *pOta, int bOta){
160043 sqlite3 *db = 0;
160044 if( pOta ){
160045 db = (bOta ? pOta->dbOta : pOta->dbMain);
160046 }
160047 return db;
160048 }
160049
160050
160051 /*
160052 ** If the error code currently stored in the OTA handle is SQLITE_CONSTRAINT,
160053 ** then edit any error message string so as to remove all occurrences of
160054 ** the pattern "ota_imp_[0-9]*".
160055 */
160056 static void otaEditErrmsg(sqlite3ota *p){
160057 if( p->rc==SQLITE_CONSTRAINT && p->zErrmsg ){
160058 int i;
160059 int nErrmsg = strlen(p->zErrmsg);
160060 for(i=0; i<(nErrmsg-8); i++){
160061 if( memcmp(&p->zErrmsg[i], "ota_imp_", 8)==0 ){
160062 int nDel = 8;
160063 while( p->zErrmsg[i+nDel]>='0' && p->zErrmsg[i+nDel]<='9' ) nDel++;
160064 memmove(&p->zErrmsg[i], &p->zErrmsg[i+nDel], nErrmsg + 1 - i - nDel);
160065 nErrmsg -= nDel;
160066 }
@@ -160067,38 +160073,38 @@
160067 }
160068 }
160069 }
160070
160071 /*
160072 ** Close the OTA handle.
160073 */
160074 SQLITE_API int SQLITE_STDCALL sqlite3ota_close(sqlite3ota *p, char **pzErrmsg){
160075 int rc;
160076 if( p ){
160077
160078 /* Commit the transaction to the *-oal file. */
160079 if( p->rc==SQLITE_OK && p->eStage==OTA_STAGE_OAL ){
160080 p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
160081 }
160082
160083 otaSaveState(p, p->eStage);
160084
160085 if( p->rc==SQLITE_OK && p->eStage==OTA_STAGE_OAL ){
160086 p->rc = sqlite3_exec(p->dbOta, "COMMIT", 0, 0, &p->zErrmsg);
160087 }
160088
160089 /* Close any open statement handles. */
160090 otaObjIterFinalize(&p->objiter);
160091
160092 /* Close the open database handle and VFS object. */
160093 sqlite3_close(p->dbMain);
160094 sqlite3_close(p->dbOta);
160095 otaDeleteVfs(p);
160096 sqlite3_free(p->aBuf);
160097 sqlite3_free(p->aFrame);
160098
160099 otaEditErrmsg(p);
160100 rc = p->rc;
160101 *pzErrmsg = p->zErrmsg;
160102 sqlite3_free(p);
160103 }else{
160104 rc = SQLITE_NOMEM;
@@ -160108,65 +160114,65 @@
160108 }
160109
160110 /*
160111 ** Return the total number of key-value operations (inserts, deletes or
160112 ** updates) that have been performed on the target database since the
160113 ** current OTA update was started.
160114 */
160115 SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3ota_progress(sqlite3ota *pOta){
160116 return pOta->nProgress;
160117 }
160118
160119 /**************************************************************************
160120 ** Beginning of OTA VFS shim methods. The VFS shim modifies the behaviour
160121 ** of a standard VFS in the following ways:
160122 **
160123 ** 1. Whenever the first page of a main database file is read or
160124 ** written, the value of the change-counter cookie is stored in
160125 ** ota_file.iCookie. Similarly, the value of the "write-version"
160126 ** database header field is stored in ota_file.iWriteVer. This ensures
160127 ** that the values are always trustworthy within an open transaction.
160128 **
160129 ** 2. Whenever an SQLITE_OPEN_WAL file is opened, the (ota_file.pWalFd)
160130 ** member variable of the associated database file descriptor is set
160131 ** to point to the new file. A mutex protected linked list of all main
160132 ** db fds opened using a particular OTA VFS is maintained at
160133 ** ota_vfs.pMain to facilitate this.
160134 **
160135 ** 3. Using a new file-control "SQLITE_FCNTL_OTA", a main db ota_file
160136 ** object can be marked as the target database of an OTA update. This
160137 ** turns on the following extra special behaviour:
160138 **
160139 ** 3a. If xAccess() is called to check if there exists a *-wal file
160140 ** associated with an OTA target database currently in OTA_STAGE_OAL
160141 ** stage (preparing the *-oal file), the following special handling
160142 ** applies:
160143 **
160144 ** * if the *-wal file does exist, return SQLITE_CANTOPEN. An OTA
160145 ** target database may not be in wal mode already.
160146 **
160147 ** * if the *-wal file does not exist, set the output parameter to
160148 ** non-zero (to tell SQLite that it does exist) anyway.
160149 **
160150 ** Then, when xOpen() is called to open the *-wal file associated with
160151 ** the OTA target in OTA_STAGE_OAL stage, instead of opening the *-wal
160152 ** file, the ota vfs opens the corresponding *-oal file instead.
160153 **
160154 ** 3b. The *-shm pages returned by xShmMap() for a target db file in
160155 ** OTA_STAGE_OAL mode are actually stored in heap memory. This is to
160156 ** avoid creating a *-shm file on disk. Additionally, xShmLock() calls
160157 ** are no-ops on target database files in OTA_STAGE_OAL mode. This is
160158 ** because assert() statements in some VFS implementations fail if
160159 ** xShmLock() is called before xShmMap().
160160 **
160161 ** 3c. If an EXCLUSIVE lock is attempted on a target database file in any
160162 ** mode except OTA_STAGE_DONE (all work completed and checkpointed), it
160163 ** fails with an SQLITE_BUSY error. This is to stop OTA connections
160164 ** from automatically checkpointing a *-wal (or *-oal) file from within
160165 ** sqlite3_close().
160166 **
160167 ** 3d. In OTA_STAGE_CAPTURE mode, all xRead() calls on the wal file, and
160168 ** all xWrite() calls on the target database file perform no IO.
160169 ** Instead the frame and page numbers that would be read and written
160170 ** are recorded. Additionally, successful attempts to obtain exclusive
160171 ** xShmLock() WRITER, CHECKPOINTER and READ0 locks on the target
160172 ** database file are recorded. xShmLock() calls to unlock the same
@@ -160173,28 +160179,28 @@
160173 ** locks are no-ops (so that once obtained, these locks are never
160174 ** relinquished). Finally, calls to xSync() on the target database
160175 ** file fail with SQLITE_INTERNAL errors.
160176 */
160177
160178 static void otaUnlockShm(ota_file *p){
160179 if( p->pOta ){
160180 int (*xShmLock)(sqlite3_file*,int,int,int) = p->pReal->pMethods->xShmLock;
160181 int i;
160182 for(i=0; i<SQLITE_SHM_NLOCK;i++){
160183 if( (1<<i) & p->pOta->mLock ){
160184 xShmLock(p->pReal, i, 1, SQLITE_SHM_UNLOCK|SQLITE_SHM_EXCLUSIVE);
160185 }
160186 }
160187 p->pOta->mLock = 0;
160188 }
160189 }
160190
160191 /*
160192 ** Close an ota file.
160193 */
160194 static int otaVfsClose(sqlite3_file *pFile){
160195 ota_file *p = (ota_file*)pFile;
160196 int rc;
160197 int i;
160198
160199 /* Free the contents of the apShm[] array. And the array itself. */
160200 for(i=0; i<p->nShm; i++){
@@ -160203,16 +160209,16 @@
160203 sqlite3_free(p->apShm);
160204 p->apShm = 0;
160205 sqlite3_free(p->zDel);
160206
160207 if( p->openFlags & SQLITE_OPEN_MAIN_DB ){
160208 ota_file **pp;
160209 sqlite3_mutex_enter(p->pOtaVfs->mutex);
160210 for(pp=&p->pOtaVfs->pMain; *pp!=p; pp=&((*pp)->pMainNext));
160211 *pp = p->pMainNext;
160212 sqlite3_mutex_leave(p->pOtaVfs->mutex);
160213 otaUnlockShm(p);
160214 p->pReal->pMethods->xShmUnmap(p->pReal, 0);
160215 }
160216
160217 /* Close the underlying file handle */
160218 rc = p->pReal->pMethods->xClose(p->pReal);
@@ -160222,37 +160228,37 @@
160222
160223 /*
160224 ** Read and return an unsigned 32-bit big-endian integer from the buffer
160225 ** passed as the only argument.
160226 */
160227 static u32 otaGetU32(u8 *aBuf){
160228 return ((u32)aBuf[0] << 24)
160229 + ((u32)aBuf[1] << 16)
160230 + ((u32)aBuf[2] << 8)
160231 + ((u32)aBuf[3]);
160232 }
160233
160234 /*
160235 ** Read data from an otaVfs-file.
160236 */
160237 static int otaVfsRead(
160238 sqlite3_file *pFile,
160239 void *zBuf,
160240 int iAmt,
160241 sqlite_int64 iOfst
160242 ){
160243 ota_file *p = (ota_file*)pFile;
160244 sqlite3ota *pOta = p->pOta;
160245 int rc;
160246
160247 if( pOta && pOta->eStage==OTA_STAGE_CAPTURE ){
160248 assert( p->openFlags & SQLITE_OPEN_WAL );
160249 rc = otaCaptureWalRead(p->pOta, iOfst, iAmt);
160250 }else{
160251 if( pOta && pOta->eStage==OTA_STAGE_OAL
160252 && (p->openFlags & SQLITE_OPEN_WAL)
160253 && iOfst>=pOta->iOalSz
160254 ){
160255 rc = SQLITE_OK;
160256 memset(zBuf, 0, iAmt);
160257 }else{
160258 rc = p->pReal->pMethods->xRead(p->pReal, zBuf, iAmt, iOfst);
@@ -160259,92 +160265,92 @@
160259 }
160260 if( rc==SQLITE_OK && iOfst==0 && (p->openFlags & SQLITE_OPEN_MAIN_DB) ){
160261 /* These look like magic numbers. But they are stable, as they are part
160262 ** of the definition of the SQLite file format, which may not change. */
160263 u8 *pBuf = (u8*)zBuf;
160264 p->iCookie = otaGetU32(&pBuf[24]);
160265 p->iWriteVer = pBuf[19];
160266 }
160267 }
160268 return rc;
160269 }
160270
160271 /*
160272 ** Write data to an otaVfs-file.
160273 */
160274 static int otaVfsWrite(
160275 sqlite3_file *pFile,
160276 const void *zBuf,
160277 int iAmt,
160278 sqlite_int64 iOfst
160279 ){
160280 ota_file *p = (ota_file*)pFile;
160281 sqlite3ota *pOta = p->pOta;
160282 int rc;
160283
160284 if( pOta && pOta->eStage==OTA_STAGE_CAPTURE ){
160285 assert( p->openFlags & SQLITE_OPEN_MAIN_DB );
160286 rc = otaCaptureDbWrite(p->pOta, iOfst);
160287 }else{
160288 if( pOta && pOta->eStage==OTA_STAGE_OAL
160289 && (p->openFlags & SQLITE_OPEN_WAL)
160290 && iOfst>=pOta->iOalSz
160291 ){
160292 pOta->iOalSz = iAmt + iOfst;
160293 }
160294 rc = p->pReal->pMethods->xWrite(p->pReal, zBuf, iAmt, iOfst);
160295 if( rc==SQLITE_OK && iOfst==0 && (p->openFlags & SQLITE_OPEN_MAIN_DB) ){
160296 /* These look like magic numbers. But they are stable, as they are part
160297 ** of the definition of the SQLite file format, which may not change. */
160298 u8 *pBuf = (u8*)zBuf;
160299 p->iCookie = otaGetU32(&pBuf[24]);
160300 p->iWriteVer = pBuf[19];
160301 }
160302 }
160303 return rc;
160304 }
160305
160306 /*
160307 ** Truncate an otaVfs-file.
160308 */
160309 static int otaVfsTruncate(sqlite3_file *pFile, sqlite_int64 size){
160310 ota_file *p = (ota_file*)pFile;
160311 return p->pReal->pMethods->xTruncate(p->pReal, size);
160312 }
160313
160314 /*
160315 ** Sync an otaVfs-file.
160316 */
160317 static int otaVfsSync(sqlite3_file *pFile, int flags){
160318 ota_file *p = (ota_file *)pFile;
160319 if( p->pOta && p->pOta->eStage==OTA_STAGE_CAPTURE ){
160320 if( p->openFlags & SQLITE_OPEN_MAIN_DB ){
160321 return SQLITE_INTERNAL;
160322 }
160323 return SQLITE_OK;
160324 }
160325 return p->pReal->pMethods->xSync(p->pReal, flags);
160326 }
160327
160328 /*
160329 ** Return the current file-size of an otaVfs-file.
160330 */
160331 static int otaVfsFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
160332 ota_file *p = (ota_file *)pFile;
160333 return p->pReal->pMethods->xFileSize(p->pReal, pSize);
160334 }
160335
160336 /*
160337 ** Lock an otaVfs-file.
160338 */
160339 static int otaVfsLock(sqlite3_file *pFile, int eLock){
160340 ota_file *p = (ota_file*)pFile;
160341 sqlite3ota *pOta = p->pOta;
160342 int rc = SQLITE_OK;
160343
160344 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
160345 if( pOta && eLock==SQLITE_LOCK_EXCLUSIVE && pOta->eStage!=OTA_STAGE_DONE ){
160346 /* Do not allow EXCLUSIVE locks. Preventing SQLite from taking this
160347 ** prevents it from checkpointing the database from sqlite3_close(). */
160348 rc = SQLITE_BUSY;
160349 }else{
160350 rc = p->pReal->pMethods->xLock(p->pReal, eLock);
@@ -160352,122 +160358,122 @@
160352
160353 return rc;
160354 }
160355
160356 /*
160357 ** Unlock an otaVfs-file.
160358 */
160359 static int otaVfsUnlock(sqlite3_file *pFile, int eLock){
160360 ota_file *p = (ota_file *)pFile;
160361 return p->pReal->pMethods->xUnlock(p->pReal, eLock);
160362 }
160363
160364 /*
160365 ** Check if another file-handle holds a RESERVED lock on an otaVfs-file.
160366 */
160367 static int otaVfsCheckReservedLock(sqlite3_file *pFile, int *pResOut){
160368 ota_file *p = (ota_file *)pFile;
160369 return p->pReal->pMethods->xCheckReservedLock(p->pReal, pResOut);
160370 }
160371
160372 /*
160373 ** File control method. For custom operations on an otaVfs-file.
160374 */
160375 static int otaVfsFileControl(sqlite3_file *pFile, int op, void *pArg){
160376 ota_file *p = (ota_file *)pFile;
160377 int (*xControl)(sqlite3_file*,int,void*) = p->pReal->pMethods->xFileControl;
160378 int rc;
160379
160380 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB)
160381 || p->openFlags & (SQLITE_OPEN_TRANSIENT_DB|SQLITE_OPEN_TEMP_JOURNAL)
160382 );
160383 if( op==SQLITE_FCNTL_OTA ){
160384 sqlite3ota *pOta = (sqlite3ota*)pArg;
160385
160386 /* First try to find another OTA vfs lower down in the vfs stack. If
160387 ** one is found, this vfs will operate in pass-through mode. The lower
160388 ** level vfs will do the special OTA handling. */
160389 rc = xControl(p->pReal, op, pArg);
160390
160391 if( rc==SQLITE_NOTFOUND ){
160392 /* Now search for a zipvfs instance lower down in the VFS stack. If
160393 ** one is found, this is an error. */
160394 void *dummy = 0;
160395 rc = xControl(p->pReal, SQLITE_FCNTL_ZIPVFS, &dummy);
160396 if( rc==SQLITE_OK ){
160397 rc = SQLITE_ERROR;
160398 pOta->zErrmsg = sqlite3_mprintf("ota/zipvfs setup error");
160399 }else if( rc==SQLITE_NOTFOUND ){
160400 pOta->pTargetFd = p;
160401 p->pOta = pOta;
160402 if( p->pWalFd ) p->pWalFd->pOta = pOta;
160403 rc = SQLITE_OK;
160404 }
160405 }
160406 return rc;
160407 }
160408
160409 rc = xControl(p->pReal, op, pArg);
160410 if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){
160411 ota_vfs *pOtaVfs = p->pOtaVfs;
160412 char *zIn = *(char**)pArg;
160413 char *zOut = sqlite3_mprintf("ota(%s)/%z", pOtaVfs->base.zName, zIn);
160414 *(char**)pArg = zOut;
160415 if( zOut==0 ) rc = SQLITE_NOMEM;
160416 }
160417
160418 return rc;
160419 }
160420
160421 /*
160422 ** Return the sector-size in bytes for an otaVfs-file.
160423 */
160424 static int otaVfsSectorSize(sqlite3_file *pFile){
160425 ota_file *p = (ota_file *)pFile;
160426 return p->pReal->pMethods->xSectorSize(p->pReal);
160427 }
160428
160429 /*
160430 ** Return the device characteristic flags supported by an otaVfs-file.
160431 */
160432 static int otaVfsDeviceCharacteristics(sqlite3_file *pFile){
160433 ota_file *p = (ota_file *)pFile;
160434 return p->pReal->pMethods->xDeviceCharacteristics(p->pReal);
160435 }
160436
160437 /*
160438 ** Take or release a shared-memory lock.
160439 */
160440 static int otaVfsShmLock(sqlite3_file *pFile, int ofst, int n, int flags){
160441 ota_file *p = (ota_file*)pFile;
160442 sqlite3ota *pOta = p->pOta;
160443 int rc = SQLITE_OK;
160444
160445 #ifdef SQLITE_AMALGAMATION
160446 assert( WAL_CKPT_LOCK==1 );
160447 #endif
160448
160449 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
160450 if( pOta && (pOta->eStage==OTA_STAGE_OAL || pOta->eStage==OTA_STAGE_MOVE) ){
160451 /* Magic number 1 is the WAL_CKPT_LOCK lock. Preventing SQLite from
160452 ** taking this lock also prevents any checkpoints from occurring.
160453 ** todo: really, it's not clear why this might occur, as
160454 ** wal_autocheckpoint ought to be turned off. */
160455 if( ofst==WAL_LOCK_CKPT && n==1 ) rc = SQLITE_BUSY;
160456 }else{
160457 int bCapture = 0;
160458 if( n==1 && (flags & SQLITE_SHM_EXCLUSIVE)
160459 && pOta && pOta->eStage==OTA_STAGE_CAPTURE
160460 && (ofst==WAL_LOCK_WRITE || ofst==WAL_LOCK_CKPT || ofst==WAL_LOCK_READ0)
160461 ){
160462 bCapture = 1;
160463 }
160464
160465 if( bCapture==0 || 0==(flags & SQLITE_SHM_UNLOCK) ){
160466 rc = p->pReal->pMethods->xShmLock(p->pReal, ofst, n, flags);
160467 if( bCapture && rc==SQLITE_OK ){
160468 pOta->mLock |= (1 << ofst);
160469 }
160470 }
160471 }
160472
160473 return rc;
@@ -160474,26 +160480,26 @@
160474 }
160475
160476 /*
160477 ** Obtain a pointer to a mapping of a single 32KiB page of the *-shm file.
160478 */
160479 static int otaVfsShmMap(
160480 sqlite3_file *pFile,
160481 int iRegion,
160482 int szRegion,
160483 int isWrite,
160484 void volatile **pp
160485 ){
160486 ota_file *p = (ota_file*)pFile;
160487 int rc = SQLITE_OK;
160488 int eStage = (p->pOta ? p->pOta->eStage : 0);
160489
160490 /* If not in OTA_STAGE_OAL, allow this call to pass through. Or, if this
160491 ** ota is in the OTA_STAGE_OAL state, use heap memory for *-shm space
160492 ** instead of a file on disk. */
160493 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
160494 if( eStage==OTA_STAGE_OAL || eStage==OTA_STAGE_MOVE ){
160495 if( iRegion<=p->nShm ){
160496 int nByte = (iRegion+1) * sizeof(char*);
160497 char **apNew = (char**)sqlite3_realloc(p->apShm, nByte);
160498 if( apNew==0 ){
160499 rc = SQLITE_NOMEM;
@@ -160528,29 +160534,29 @@
160528 }
160529
160530 /*
160531 ** Memory barrier.
160532 */
160533 static void otaVfsShmBarrier(sqlite3_file *pFile){
160534 ota_file *p = (ota_file *)pFile;
160535 p->pReal->pMethods->xShmBarrier(p->pReal);
160536 }
160537
160538 /*
160539 ** The xShmUnmap method.
160540 */
160541 static int otaVfsShmUnmap(sqlite3_file *pFile, int delFlag){
160542 ota_file *p = (ota_file*)pFile;
160543 int rc = SQLITE_OK;
160544 int eStage = (p->pOta ? p->pOta->eStage : 0);
160545
160546 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
160547 if( eStage==OTA_STAGE_OAL || eStage==OTA_STAGE_MOVE ){
160548 /* no-op */
160549 }else{
160550 /* Release the checkpointer and writer locks */
160551 otaUnlockShm(p);
160552 rc = p->pReal->pMethods->xShmUnmap(p->pReal, delFlag);
160553 }
160554 return rc;
160555 }
160556
@@ -160558,56 +160564,56 @@
160558 ** Given that zWal points to a buffer containing a wal file name passed to
160559 ** either the xOpen() or xAccess() VFS method, return a pointer to the
160560 ** file-handle opened by the same database connection on the corresponding
160561 ** database file.
160562 */
160563 static ota_file *otaFindMaindb(ota_vfs *pOtaVfs, const char *zWal){
160564 ota_file *pDb;
160565 sqlite3_mutex_enter(pOtaVfs->mutex);
160566 for(pDb=pOtaVfs->pMain; pDb && pDb->zWal!=zWal; pDb=pDb->pMainNext);
160567 sqlite3_mutex_leave(pOtaVfs->mutex);
160568 return pDb;
160569 }
160570
160571 /*
160572 ** Open an ota file handle.
160573 */
160574 static int otaVfsOpen(
160575 sqlite3_vfs *pVfs,
160576 const char *zName,
160577 sqlite3_file *pFile,
160578 int flags,
160579 int *pOutFlags
160580 ){
160581 static sqlite3_io_methods otavfs_io_methods = {
160582 2, /* iVersion */
160583 otaVfsClose, /* xClose */
160584 otaVfsRead, /* xRead */
160585 otaVfsWrite, /* xWrite */
160586 otaVfsTruncate, /* xTruncate */
160587 otaVfsSync, /* xSync */
160588 otaVfsFileSize, /* xFileSize */
160589 otaVfsLock, /* xLock */
160590 otaVfsUnlock, /* xUnlock */
160591 otaVfsCheckReservedLock, /* xCheckReservedLock */
160592 otaVfsFileControl, /* xFileControl */
160593 otaVfsSectorSize, /* xSectorSize */
160594 otaVfsDeviceCharacteristics, /* xDeviceCharacteristics */
160595 otaVfsShmMap, /* xShmMap */
160596 otaVfsShmLock, /* xShmLock */
160597 otaVfsShmBarrier, /* xShmBarrier */
160598 otaVfsShmUnmap /* xShmUnmap */
160599 };
160600 ota_vfs *pOtaVfs = (ota_vfs*)pVfs;
160601 sqlite3_vfs *pRealVfs = pOtaVfs->pRealVfs;
160602 ota_file *pFd = (ota_file *)pFile;
160603 int rc = SQLITE_OK;
160604 const char *zOpen = zName;
160605
160606 memset(pFd, 0, sizeof(ota_file));
160607 pFd->pReal = (sqlite3_file*)&pFd[1];
160608 pFd->pOtaVfs = pOtaVfs;
160609 pFd->openFlags = flags;
160610 if( zName ){
160611 if( flags & SQLITE_OPEN_MAIN_DB ){
160612 /* A main database has just been opened. The following block sets
160613 ** (pFd->zWal) to point to a buffer owned by SQLite that contains
@@ -160631,13 +160637,13 @@
160631 }
160632 z += (n + 8 + 1);
160633 pFd->zWal = z;
160634 }
160635 else if( flags & SQLITE_OPEN_WAL ){
160636 ota_file *pDb = otaFindMaindb(pOtaVfs, zName);
160637 if( pDb ){
160638 if( pDb->pOta && pDb->pOta->eStage==OTA_STAGE_OAL ){
160639 /* This call is to open a *-wal file. Intead, open the *-oal. This
160640 ** code ensures that the string passed to xOpen() is terminated by a
160641 ** pair of '\0' bytes in case the VFS attempts to extract a URI
160642 ** parameter from it. */
160643 int nCopy = strlen(zName);
@@ -160649,11 +160655,11 @@
160649 zCopy[nCopy+1] = '\0';
160650 zOpen = (const char*)(pFd->zDel = zCopy);
160651 }else{
160652 rc = SQLITE_NOMEM;
160653 }
160654 pFd->pOta = pDb->pOta;
160655 }
160656 pDb->pWalFd = pFd;
160657 }
160658 }
160659 }
@@ -160663,16 +160669,16 @@
160663 }
160664 if( pFd->pReal->pMethods ){
160665 /* The xOpen() operation has succeeded. Set the sqlite3_file.pMethods
160666 ** pointer and, if the file is a main database file, link it into the
160667 ** mutex protected linked list of all such files. */
160668 pFile->pMethods = &otavfs_io_methods;
160669 if( flags & SQLITE_OPEN_MAIN_DB ){
160670 sqlite3_mutex_enter(pOtaVfs->mutex);
160671 pFd->pMainNext = pOtaVfs->pMain;
160672 pOtaVfs->pMain = pFd;
160673 sqlite3_mutex_leave(pOtaVfs->mutex);
160674 }
160675 }else{
160676 sqlite3_free(pFd->zDel);
160677 }
160678
@@ -160680,48 +160686,48 @@
160680 }
160681
160682 /*
160683 ** Delete the file located at zPath.
160684 */
160685 static int otaVfsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
160686 sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
160687 return pRealVfs->xDelete(pRealVfs, zPath, dirSync);
160688 }
160689
160690 /*
160691 ** Test for access permissions. Return true if the requested permission
160692 ** is available, or false otherwise.
160693 */
160694 static int otaVfsAccess(
160695 sqlite3_vfs *pVfs,
160696 const char *zPath,
160697 int flags,
160698 int *pResOut
160699 ){
160700 ota_vfs *pOtaVfs = (ota_vfs*)pVfs;
160701 sqlite3_vfs *pRealVfs = pOtaVfs->pRealVfs;
160702 int rc;
160703
160704 rc = pRealVfs->xAccess(pRealVfs, zPath, flags, pResOut);
160705
160706 /* If this call is to check if a *-wal file associated with an OTA target
160707 ** database connection exists, and the OTA update is in OTA_STAGE_OAL,
160708 ** the following special handling is activated:
160709 **
160710 ** a) if the *-wal file does exist, return SQLITE_CANTOPEN. This
160711 ** ensures that the OTA extension never tries to update a database
160712 ** in wal mode, even if the first page of the database file has
160713 ** been damaged.
160714 **
160715 ** b) if the *-wal file does not exist, claim that it does anyway,
160716 ** causing SQLite to call xOpen() to open it. This call will also
160717 ** be intercepted (see the otaVfsOpen() function) and the *-oal
160718 ** file opened instead.
160719 */
160720 if( rc==SQLITE_OK && flags==SQLITE_ACCESS_EXISTS ){
160721 ota_file *pDb = otaFindMaindb(pOtaVfs, zPath);
160722 if( pDb && pDb->pOta && pDb->pOta->eStage==OTA_STAGE_OAL ){
160723 if( *pResOut ){
160724 rc = SQLITE_CANTOPEN;
160725 }else{
160726 *pResOut = 1;
160727 }
@@ -160734,151 +160740,151 @@
160734 /*
160735 ** Populate buffer zOut with the full canonical pathname corresponding
160736 ** to the pathname in zPath. zOut is guaranteed to point to a buffer
160737 ** of at least (DEVSYM_MAX_PATHNAME+1) bytes.
160738 */
160739 static int otaVfsFullPathname(
160740 sqlite3_vfs *pVfs,
160741 const char *zPath,
160742 int nOut,
160743 char *zOut
160744 ){
160745 sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
160746 return pRealVfs->xFullPathname(pRealVfs, zPath, nOut, zOut);
160747 }
160748
160749 #ifndef SQLITE_OMIT_LOAD_EXTENSION
160750 /*
160751 ** Open the dynamic library located at zPath and return a handle.
160752 */
160753 static void *otaVfsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
160754 sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
160755 return pRealVfs->xDlOpen(pRealVfs, zPath);
160756 }
160757
160758 /*
160759 ** Populate the buffer zErrMsg (size nByte bytes) with a human readable
160760 ** utf-8 string describing the most recent error encountered associated
160761 ** with dynamic libraries.
160762 */
160763 static void otaVfsDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
160764 sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
160765 pRealVfs->xDlError(pRealVfs, nByte, zErrMsg);
160766 }
160767
160768 /*
160769 ** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
160770 */
160771 static void (*otaVfsDlSym(
160772 sqlite3_vfs *pVfs,
160773 void *pArg,
160774 const char *zSym
160775 ))(void){
160776 sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
160777 return pRealVfs->xDlSym(pRealVfs, pArg, zSym);
160778 }
160779
160780 /*
160781 ** Close the dynamic library handle pHandle.
160782 */
160783 static void otaVfsDlClose(sqlite3_vfs *pVfs, void *pHandle){
160784 sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
160785 return pRealVfs->xDlClose(pRealVfs, pHandle);
160786 }
160787 #endif /* SQLITE_OMIT_LOAD_EXTENSION */
160788
160789 /*
160790 ** Populate the buffer pointed to by zBufOut with nByte bytes of
160791 ** random data.
160792 */
160793 static int otaVfsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
160794 sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
160795 return pRealVfs->xRandomness(pRealVfs, nByte, zBufOut);
160796 }
160797
160798 /*
160799 ** Sleep for nMicro microseconds. Return the number of microseconds
160800 ** actually slept.
160801 */
160802 static int otaVfsSleep(sqlite3_vfs *pVfs, int nMicro){
160803 sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
160804 return pRealVfs->xSleep(pRealVfs, nMicro);
160805 }
160806
160807 /*
160808 ** Return the current time as a Julian Day number in *pTimeOut.
160809 */
160810 static int otaVfsCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
160811 sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
160812 return pRealVfs->xCurrentTime(pRealVfs, pTimeOut);
160813 }
160814
160815 /*
160816 ** No-op.
160817 */
160818 static int otaVfsGetLastError(sqlite3_vfs *pVfs, int a, char *b){
160819 return 0;
160820 }
160821
160822 /*
160823 ** Deregister and destroy an OTA vfs created by an earlier call to
160824 ** sqlite3ota_create_vfs().
160825 */
160826 SQLITE_API void SQLITE_STDCALL sqlite3ota_destroy_vfs(const char *zName){
160827 sqlite3_vfs *pVfs = sqlite3_vfs_find(zName);
160828 if( pVfs && pVfs->xOpen==otaVfsOpen ){
160829 sqlite3_mutex_free(((ota_vfs*)pVfs)->mutex);
160830 sqlite3_vfs_unregister(pVfs);
160831 sqlite3_free(pVfs);
160832 }
160833 }
160834
160835 /*
160836 ** Create an OTA VFS named zName that accesses the underlying file-system
160837 ** via existing VFS zParent. The new object is registered as a non-default
160838 ** VFS with SQLite before returning.
160839 */
160840 SQLITE_API int SQLITE_STDCALL sqlite3ota_create_vfs(const char *zName, const char *zParent){
160841
160842 /* Template for VFS */
160843 static sqlite3_vfs vfs_template = {
160844 1, /* iVersion */
160845 0, /* szOsFile */
160846 0, /* mxPathname */
160847 0, /* pNext */
160848 0, /* zName */
160849 0, /* pAppData */
160850 otaVfsOpen, /* xOpen */
160851 otaVfsDelete, /* xDelete */
160852 otaVfsAccess, /* xAccess */
160853 otaVfsFullPathname, /* xFullPathname */
160854
160855 #ifndef SQLITE_OMIT_LOAD_EXTENSION
160856 otaVfsDlOpen, /* xDlOpen */
160857 otaVfsDlError, /* xDlError */
160858 otaVfsDlSym, /* xDlSym */
160859 otaVfsDlClose, /* xDlClose */
160860 #else
160861 0, 0, 0, 0,
160862 #endif
160863
160864 otaVfsRandomness, /* xRandomness */
160865 otaVfsSleep, /* xSleep */
160866 otaVfsCurrentTime, /* xCurrentTime */
160867 otaVfsGetLastError, /* xGetLastError */
160868 0, /* xCurrentTimeInt64 (version 2) */
160869 0, 0, 0 /* Unimplemented version 3 methods */
160870 };
160871
160872 ota_vfs *pNew = 0; /* Newly allocated VFS */
160873 int nName;
160874 int rc = SQLITE_OK;
160875
160876 int nByte;
160877 nName = strlen(zName);
160878 nByte = sizeof(ota_vfs) + nName + 1;
160879 pNew = (ota_vfs*)sqlite3_malloc(nByte);
160880 if( pNew==0 ){
160881 rc = SQLITE_NOMEM;
160882 }else{
160883 sqlite3_vfs *pParent; /* Parent VFS */
160884 memset(pNew, 0, nByte);
@@ -160887,11 +160893,11 @@
160887 rc = SQLITE_NOTFOUND;
160888 }else{
160889 char *zSpace;
160890 memcpy(&pNew->base, &vfs_template, sizeof(sqlite3_vfs));
160891 pNew->base.mxPathname = pParent->mxPathname;
160892 pNew->base.szOsFile = sizeof(ota_file) + pParent->szOsFile;
160893 pNew->pRealVfs = pParent;
160894 pNew->base.zName = (const char*)(zSpace = (char*)&pNew[1]);
160895 memcpy(zSpace, zName, nName);
160896
160897 /* Allocate the mutex and register the new VFS (not as the default) */
@@ -160913,13 +160919,13 @@
160913 }
160914
160915
160916 /**************************************************************************/
160917
160918 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_OTA) */
160919
160920 /************** End of sqlite3ota.c ******************************************/
160921 /************** Begin file dbstat.c ******************************************/
160922 /*
160923 ** 2010 July 12
160924 **
160925 ** The author disclaims copyright to this source code. In place of
160926
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -325,11 +325,11 @@
325 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
326 ** [sqlite_version()] and [sqlite_source_id()].
327 */
328 #define SQLITE_VERSION "3.8.11"
329 #define SQLITE_VERSION_NUMBER 3008011
330 #define SQLITE_SOURCE_ID "2015-07-23 20:44:49 017c5019e1ce042025d4f327e50ec50af49f9fa4"
331
332 /*
333 ** CAPI3REF: Run-Time Library Version Numbers
334 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
335 **
@@ -1179,13 +1179,13 @@
1179 **
1180 ** <li>[[SQLITE_FCNTL_ZIPVFS]]
1181 ** The [SQLITE_FCNTL_ZIPVFS] opcode is implemented by zipvfs only. All other
1182 ** VFS should return SQLITE_NOTFOUND for this opcode.
1183 **
1184 ** <li>[[SQLITE_FCNTL_RBU]]
1185 ** The [SQLITE_FCNTL_RBU] opcode is implemented by the special VFS used by
1186 ** the RBU extension only. All other VFS should return SQLITE_NOTFOUND for
1187 ** this opcode.
1188 ** </ul>
1189 */
1190 #define SQLITE_FCNTL_LOCKSTATE 1
1191 #define SQLITE_FCNTL_GET_LOCKPROXYFILE 2
@@ -1209,11 +1209,11 @@
1209 #define SQLITE_FCNTL_SYNC 21
1210 #define SQLITE_FCNTL_COMMIT_PHASETWO 22
1211 #define SQLITE_FCNTL_WIN32_SET_HANDLE 23
1212 #define SQLITE_FCNTL_WAL_BLOCK 24
1213 #define SQLITE_FCNTL_ZIPVFS 25
1214 #define SQLITE_FCNTL_RBU 26
1215
1216 /* deprecated names */
1217 #define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE
1218 #define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE
1219 #define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO
@@ -40711,22 +40711,22 @@
40711 /*
40712 ** Try to initialize the pCache->pFree and pCache->pBulk fields. Return
40713 ** true if pCache->pFree ends up containing one or more free pages.
40714 */
40715 static int pcache1InitBulk(PCache1 *pCache){
40716 i64 szBulk;
40717 char *zBulk;
40718 if( pcache1.nInitPage==0 ) return 0;
40719 /* Do not bother with a bulk allocation if the cache size very small */
40720 if( pCache->nMax<3 ) return 0;
40721 sqlite3BeginBenignMalloc();
40722 if( pcache1.nInitPage>0 ){
40723 szBulk = pCache->szAlloc * (i64)pcache1.nInitPage;
40724 }else{
40725 szBulk = -1024 * (i64)pcache1.nInitPage;
40726 }
40727 if( szBulk > pCache->szAlloc*(i64)pCache->nMax ){
40728 szBulk = pCache->szAlloc*pCache->nMax;
40729 }
40730 zBulk = pCache->pBulk = sqlite3Malloc( szBulk );
40731 sqlite3EndBenignMalloc();
40732 if( zBulk ){
@@ -50252,13 +50252,13 @@
50252
50253 assert( pWal->writeLock );
50254 pWal->hdr.isInit = 1;
50255 pWal->hdr.iVersion = WALINDEX_MAX_VERSION;
50256 walChecksumBytes(1, (u8*)&pWal->hdr, nCksum, 0, pWal->hdr.aCksum);
50257 memcpy((void*)&aHdr[1], (const void*)&pWal->hdr, sizeof(WalIndexHdr));
50258 walShmBarrier(pWal);
50259 memcpy((void*)&aHdr[0], (const void*)&pWal->hdr, sizeof(WalIndexHdr));
50260 }
50261
50262 /*
50263 ** This function encodes a single frame header and writes it to a buffer
50264 ** supplied by the caller. A frame-header is made up of a series of
@@ -50556,17 +50556,17 @@
50556 #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
50557 /* Verify that the every entry in the mapping region is still reachable
50558 ** via the hash table even after the cleanup.
50559 */
50560 if( iLimit ){
50561 int j; /* Loop counter */
50562 int iKey; /* Hash key */
50563 for(j=1; j<=iLimit; j++){
50564 for(iKey=walHash(aPgno[j]); aHash[iKey]; iKey=walNextHash(iKey)){
50565 if( aHash[iKey]==j ) break;
50566 }
50567 assert( aHash[iKey]==j );
50568 }
50569 }
50570 #endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
50571 }
50572
@@ -118653,11 +118653,15 @@
118653
118654 /* Read the PK into an array of temp registers. */
118655 r = sqlite3GetTempRange(pParse, nPk);
118656 for(iPk=0; iPk<nPk; iPk++){
118657 int iCol = pPk->aiColumn[iPk];
118658 int rx;
118659 rx = sqlite3ExprCodeGetColumn(pParse, pTab, iCol, iCur,r+iPk,0);
118660 if( rx!=r+iPk ){
118661 sqlite3VdbeAddOp2(v, OP_SCopy, rx, r+iPk);
118662 }
118663 }
118664
118665 /* Check if the temp table already contains this key. If so,
118666 ** the row has already been included in the result set and
118667 ** can be ignored (by jumping past the Gosub below). Otherwise,
@@ -130036,10 +130040,11 @@
130040 ** space for the lookaside memory is obtained from sqlite3_malloc().
130041 ** If pStart is not NULL then it is sz*cnt bytes of memory to use for
130042 ** the lookaside memory.
130043 */
130044 static int setupLookaside(sqlite3 *db, void *pBuf, int sz, int cnt){
130045 #ifndef SQLITE_OMIT_LOOKASIDE
130046 void *pStart;
130047 if( db->lookaside.nOut ){
130048 return SQLITE_BUSY;
130049 }
130050 /* Free any existing lookaside buffer for this handle before
@@ -130086,10 +130091,11 @@
130091 db->lookaside.pStart = db;
130092 db->lookaside.pEnd = db;
130093 db->lookaside.bEnabled = 0;
130094 db->lookaside.bMalloced = 0;
130095 }
130096 #endif /* SQLITE_OMIT_LOOKASIDE */
130097 return SQLITE_OK;
130098 }
130099
130100 /*
130101 ** Return the mutex associated with a database connection.
@@ -156830,11 +156836,11 @@
156836
156837 #endif /* defined(SQLITE_ENABLE_ICU) */
156838 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
156839
156840 /************** End of fts3_icu.c ********************************************/
156841 /************** Begin file sqlite3rbu.c **************************************/
156842 /*
156843 ** 2014 August 30
156844 **
156845 ** The author disclaims copyright to this source code. In place of
156846 ** a legal notice, here is a blessing:
@@ -156846,15 +156852,15 @@
156852 *************************************************************************
156853 **
156854 **
156855 ** OVERVIEW
156856 **
156857 ** The RBU extension requires that the RBU update be packaged as an
156858 ** SQLite database. The tables it expects to find are described in
156859 ** sqlite3rbu.h. Essentially, for each table xyz in the target database
156860 ** that the user wishes to write to, a corresponding data_xyz table is
156861 ** created in the RBU database and populated with one row for each row to
156862 ** update, insert or delete from the target table.
156863 **
156864 ** The update proceeds in three stages:
156865 **
156866 ** 1) The database is updated. The modified database pages are written
@@ -156862,57 +156868,57 @@
156868 ** that it is named "<database>-oal" instead of "<database>-wal".
156869 ** Because regular SQLite clients do not look for file named
156870 ** "<database>-oal", they go on using the original database in
156871 ** rollback mode while the *-oal file is being generated.
156872 **
156873 ** During this stage RBU does not update the database by writing
156874 ** directly to the target tables. Instead it creates "imposter"
156875 ** tables using the SQLITE_TESTCTRL_IMPOSTER interface that it uses
156876 ** to update each b-tree individually. All updates required by each
156877 ** b-tree are completed before moving on to the next, and all
156878 ** updates are done in sorted key order.
156879 **
156880 ** 2) The "<database>-oal" file is moved to the equivalent "<database>-wal"
156881 ** location using a call to rename(2). Before doing this the RBU
156882 ** module takes an EXCLUSIVE lock on the database file, ensuring
156883 ** that there are no other active readers.
156884 **
156885 ** Once the EXCLUSIVE lock is released, any other database readers
156886 ** detect the new *-wal file and read the database in wal mode. At
156887 ** this point they see the new version of the database - including
156888 ** the updates made as part of the RBU update.
156889 **
156890 ** 3) The new *-wal file is checkpointed. This proceeds in the same way
156891 ** as a regular database checkpoint, except that a single frame is
156892 ** checkpointed each time sqlite3rbu_step() is called. If the RBU
156893 ** handle is closed before the entire *-wal file is checkpointed,
156894 ** the checkpoint progress is saved in the RBU database and the
156895 ** checkpoint can be resumed by another RBU client at some point in
156896 ** the future.
156897 **
156898 ** POTENTIAL PROBLEMS
156899 **
156900 ** The rename() call might not be portable. And RBU is not currently
156901 ** syncing the directory after renaming the file.
156902 **
156903 ** When state is saved, any commit to the *-oal file and the commit to
156904 ** the RBU update database are not atomic. So if the power fails at the
156905 ** wrong moment they might get out of sync. As the main database will be
156906 ** committed before the RBU update database this will likely either just
156907 ** pass unnoticed, or result in SQLITE_CONSTRAINT errors (due to UNIQUE
156908 ** constraint violations).
156909 **
156910 ** If some client does modify the target database mid RBU update, or some
156911 ** other error occurs, the RBU extension will keep throwing errors. It's
156912 ** not really clear how to get out of this state. The system could just
156913 ** by delete the RBU update database and *-oal file and have the device
156914 ** download the update again and start over.
156915 **
156916 ** At present, for an UPDATE, both the new.* and old.* records are
156917 ** collected in the rbu_xyz table. And for both UPDATEs and DELETEs all
156918 ** fields are collected. This means we're probably writing a lot more
156919 ** data to disk when saving the state of an ongoing update to the RBU
156920 ** update database than is strictly necessary.
156921 **
156922 */
156923
156924 /* #include <assert.h> */
@@ -156920,13 +156926,13 @@
156926 /* #include <stdio.h> */
156927 /* #include <unistd.h> */
156928
156929 /* #include "sqlite3.h" */
156930
156931 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RBU)
156932 /************** Include sqlite3rbu.h in the middle of sqlite3rbu.c ***********/
156933 /************** Begin file sqlite3rbu.h **************************************/
156934 /*
156935 ** 2014 August 30
156936 **
156937 ** The author disclaims copyright to this source code. In place of
156938 ** a legal notice, here is a blessing:
@@ -156935,11 +156941,11 @@
156941 ** May you find forgiveness for yourself and forgive others.
156942 ** May you share freely, never taking more than you give.
156943 **
156944 *************************************************************************
156945 **
156946 ** This file contains the public interface for the RBU extension.
156947 */
156948
156949 /*
156950 ** SUMMARY
156951 **
@@ -156972,18 +156978,18 @@
156978 ** mobile device that is frequently rebooted. Even after the writer process
156979 ** has committed one or more sub-transactions, other database clients continue
156980 ** to read from the original database snapshot. In other words, partially
156981 ** applied transactions are not visible to other clients.
156982 **
156983 ** "RBU" stands for "Over The Air" update. As in a large database update
156984 ** transmitted via a wireless network to a mobile device. A transaction
156985 ** applied using this extension is hence refered to as an "RBU update".
156986 **
156987 **
156988 ** LIMITATIONS
156989 **
156990 ** An "RBU update" transaction is subject to the following limitations:
156991 **
156992 ** * The transaction must consist of INSERT, UPDATE and DELETE operations
156993 ** only.
156994 **
156995 ** * INSERT statements may not use any default values.
@@ -157004,430 +157010,430 @@
157010 ** * No constraint handling mode except for "OR ROLLBACK" is supported.
157011 **
157012 **
157013 ** PREPARATION
157014 **
157015 ** An "RBU update" is stored as a separate SQLite database. A database
157016 ** containing an RBU update is an "RBU database". For each table in the
157017 ** target database to be updated, the RBU database should contain a table
157018 ** named "data_<target name>" containing the same set of columns as the
157019 ** target table, and one more - "rbu_control". The data_% table should
157020 ** have no PRIMARY KEY or UNIQUE constraints, but each column should have
157021 ** the same type as the corresponding column in the target database.
157022 ** The "rbu_control" column should have no type at all. For example, if
157023 ** the target database contains:
157024 **
157025 ** CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT, c UNIQUE);
157026 **
157027 ** Then the RBU database should contain:
157028 **
157029 ** CREATE TABLE data_t1(a INTEGER, b TEXT, c, rbu_control);
157030 **
157031 ** The order of the columns in the data_% table does not matter.
157032 **
157033 ** If the target database table is a virtual table or a table that has no
157034 ** PRIMARY KEY declaration, the data_% table must also contain a column
157035 ** named "rbu_rowid". This column is mapped to the tables implicit primary
157036 ** key column - "rowid". Virtual tables for which the "rowid" column does
157037 ** not function like a primary key value cannot be updated using RBU. For
157038 ** example, if the target db contains either of the following:
157039 **
157040 ** CREATE VIRTUAL TABLE x1 USING fts3(a, b);
157041 ** CREATE TABLE x1(a, b)
157042 **
157043 ** then the RBU database should contain:
157044 **
157045 ** CREATE TABLE data_x1(a, b, rbu_rowid, rbu_control);
157046 **
157047 ** All non-hidden columns (i.e. all columns matched by "SELECT *") of the
157048 ** target table must be present in the input table. For virtual tables,
157049 ** hidden columns are optional - they are updated by RBU if present in
157050 ** the input table, or not otherwise. For example, to write to an fts4
157051 ** table with a hidden languageid column such as:
157052 **
157053 ** CREATE VIRTUAL TABLE ft1 USING fts4(a, b, languageid='langid');
157054 **
157055 ** Either of the following input table schemas may be used:
157056 **
157057 ** CREATE TABLE data_ft1(a, b, langid, rbu_rowid, rbu_control);
157058 ** CREATE TABLE data_ft1(a, b, rbu_rowid, rbu_control);
157059 **
157060 ** For each row to INSERT into the target database as part of the RBU
157061 ** update, the corresponding data_% table should contain a single record
157062 ** with the "rbu_control" column set to contain integer value 0. The
157063 ** other columns should be set to the values that make up the new record
157064 ** to insert.
157065 **
157066 ** If the target database table has an INTEGER PRIMARY KEY, it is not
157067 ** possible to insert a NULL value into the IPK column. Attempting to
157068 ** do so results in an SQLITE_MISMATCH error.
157069 **
157070 ** For each row to DELETE from the target database as part of the RBU
157071 ** update, the corresponding data_% table should contain a single record
157072 ** with the "rbu_control" column set to contain integer value 1. The
157073 ** real primary key values of the row to delete should be stored in the
157074 ** corresponding columns of the data_% table. The values stored in the
157075 ** other columns are not used.
157076 **
157077 ** For each row to UPDATE from the target database as part of the RBU
157078 ** update, the corresponding data_% table should contain a single record
157079 ** with the "rbu_control" column set to contain a value of type text.
157080 ** The real primary key values identifying the row to update should be
157081 ** stored in the corresponding columns of the data_% table row, as should
157082 ** the new values of all columns being update. The text value in the
157083 ** "rbu_control" column must contain the same number of characters as
157084 ** there are columns in the target database table, and must consist entirely
157085 ** of 'x' and '.' characters (or in some special cases 'd' - see below). For
157086 ** each column that is being updated, the corresponding character is set to
157087 ** 'x'. For those that remain as they are, the corresponding character of the
157088 ** rbu_control value should be set to '.'. For example, given the tables
157089 ** above, the update statement:
157090 **
157091 ** UPDATE t1 SET c = 'usa' WHERE a = 4;
157092 **
157093 ** is represented by the data_t1 row created by:
157094 **
157095 ** INSERT INTO data_t1(a, b, c, rbu_control) VALUES(4, NULL, 'usa', '..x');
157096 **
157097 ** Instead of an 'x' character, characters of the rbu_control value specified
157098 ** for UPDATEs may also be set to 'd'. In this case, instead of updating the
157099 ** target table with the value stored in the corresponding data_% column, the
157100 ** user-defined SQL function "rbu_delta()" is invoked and the result stored in
157101 ** the target table column. rbu_delta() is invoked with two arguments - the
157102 ** original value currently stored in the target table column and the
157103 ** value specified in the data_xxx table.
157104 **
157105 ** For example, this row:
157106 **
157107 ** INSERT INTO data_t1(a, b, c, rbu_control) VALUES(4, NULL, 'usa', '..d');
157108 **
157109 ** is similar to an UPDATE statement such as:
157110 **
157111 ** UPDATE t1 SET c = rbu_delta(c, 'usa') WHERE a = 4;
157112 **
157113 ** If the target database table is a virtual table or a table with no PRIMARY
157114 ** KEY, the rbu_control value should not include a character corresponding
157115 ** to the rbu_rowid value. For example, this:
157116 **
157117 ** INSERT INTO data_ft1(a, b, rbu_rowid, rbu_control)
157118 ** VALUES(NULL, 'usa', 12, '.x');
157119 **
157120 ** causes a result similar to:
157121 **
157122 ** UPDATE ft1 SET b = 'usa' WHERE rowid = 12;
157123 **
157124 ** The data_xxx tables themselves should have no PRIMARY KEY declarations.
157125 ** However, RBU is more efficient if reading the rows in from each data_xxx
157126 ** table in "rowid" order is roughly the same as reading them sorted by
157127 ** the PRIMARY KEY of the corresponding target database table. In other
157128 ** words, rows should be sorted using the destination table PRIMARY KEY
157129 ** fields before they are inserted into the data_xxx tables.
157130 **
157131 ** USAGE
157132 **
157133 ** The API declared below allows an application to apply an RBU update
157134 ** stored on disk to an existing target database. Essentially, the
157135 ** application:
157136 **
157137 ** 1) Opens an RBU handle using the sqlite3rbu_open() function.
157138 **
157139 ** 2) Registers any required virtual table modules with the database
157140 ** handle returned by sqlite3rbu_db(). Also, if required, register
157141 ** the rbu_delta() implementation.
157142 **
157143 ** 3) Calls the sqlite3rbu_step() function one or more times on
157144 ** the new handle. Each call to sqlite3rbu_step() performs a single
157145 ** b-tree operation, so thousands of calls may be required to apply
157146 ** a complete update.
157147 **
157148 ** 4) Calls sqlite3rbu_close() to close the RBU update handle. If
157149 ** sqlite3rbu_step() has been called enough times to completely
157150 ** apply the update to the target database, then the RBU database
157151 ** is marked as fully applied. Otherwise, the state of the RBU
157152 ** update application is saved in the RBU database for later
157153 ** resumption.
157154 **
157155 ** See comments below for more detail on APIs.
157156 **
157157 ** If an update is only partially applied to the target database by the
157158 ** time sqlite3rbu_close() is called, various state information is saved
157159 ** within the RBU database. This allows subsequent processes to automatically
157160 ** resume the RBU update from where it left off.
157161 **
157162 ** To remove all RBU extension state information, returning an RBU database
157163 ** to its original contents, it is sufficient to drop all tables that begin
157164 ** with the prefix "rbu_"
157165 **
157166 ** DATABASE LOCKING
157167 **
157168 ** An RBU update may not be applied to a database in WAL mode. Attempting
157169 ** to do so is an error (SQLITE_ERROR).
157170 **
157171 ** While an RBU handle is open, a SHARED lock may be held on the target
157172 ** database file. This means it is possible for other clients to read the
157173 ** database, but not to write it.
157174 **
157175 ** If an RBU update is started and then suspended before it is completed,
157176 ** then an external client writes to the database, then attempting to resume
157177 ** the suspended RBU update is also an error (SQLITE_BUSY).
157178 */
157179
157180 #ifndef _SQLITE3RBU_H
157181 #define _SQLITE3RBU_H
157182
157183 /* #include "sqlite3.h" ** Required for error code definitions ** */
157184
157185 typedef struct sqlite3rbu sqlite3rbu;
157186
157187 /*
157188 ** Open an RBU handle.
157189 **
157190 ** Argument zTarget is the path to the target database. Argument zRbu is
157191 ** the path to the RBU database. Each call to this function must be matched
157192 ** by a call to sqlite3rbu_close(). When opening the databases, RBU passes
157193 ** the SQLITE_CONFIG_URI flag to sqlite3_open_v2(). So if either zTarget
157194 ** or zRbu begin with "file:", it will be interpreted as an SQLite
157195 ** database URI, not a regular file name.
157196 **
157197 ** If the zState argument is passed a NULL value, the RBU extension stores
157198 ** the current state of the update (how many rows have been updated, which
157199 ** indexes are yet to be updated etc.) within the RBU database itself. This
157200 ** can be convenient, as it means that the RBU application does not need to
157201 ** organize removing a separate state file after the update is concluded.
157202 ** Or, if zState is non-NULL, it must be a path to a database file in which
157203 ** the RBU extension can store the state of the update.
157204 **
157205 ** When resuming an RBU update, the zState argument must be passed the same
157206 ** value as when the RBU update was started.
157207 **
157208 ** Once the RBU update is finished, the RBU extension does not
157209 ** automatically remove any zState database file, even if it created it.
157210 **
157211 ** By default, RBU uses the default VFS to access the files on disk. To
157212 ** use a VFS other than the default, an SQLite "file:" URI containing a
157213 ** "vfs=..." option may be passed as the zTarget option.
157214 **
157215 ** IMPORTANT NOTE FOR ZIPVFS USERS: The RBU extension works with all of
157216 ** SQLite's built-in VFSs, including the multiplexor VFS. However it does
157217 ** not work out of the box with zipvfs. Refer to the comment describing
157218 ** the zipvfs_create_vfs() API below for details on using RBU with zipvfs.
157219 */
157220 SQLITE_API sqlite3rbu *SQLITE_STDCALL sqlite3rbu_open(
157221 const char *zTarget,
157222 const char *zRbu,
157223 const char *zState
157224 );
157225
157226 /*
157227 ** Internally, each RBU connection uses a separate SQLite database
157228 ** connection to access the target and rbu update databases. This
157229 ** API allows the application direct access to these database handles.
157230 **
157231 ** The first argument passed to this function must be a valid, open, RBU
157232 ** handle. The second argument should be passed zero to access the target
157233 ** database handle, or non-zero to access the rbu update database handle.
157234 ** Accessing the underlying database handles may be useful in the
157235 ** following scenarios:
157236 **
157237 ** * If any target tables are virtual tables, it may be necessary to
157238 ** call sqlite3_create_module() on the target database handle to
157239 ** register the required virtual table implementations.
157240 **
157241 ** * If the data_xxx tables in the RBU source database are virtual
157242 ** tables, the application may need to call sqlite3_create_module() on
157243 ** the rbu update db handle to any required virtual table
157244 ** implementations.
157245 **
157246 ** * If the application uses the "rbu_delta()" feature described above,
157247 ** it must use sqlite3_create_function() or similar to register the
157248 ** rbu_delta() implementation with the target database handle.
157249 **
157250 ** If an error has occurred, either while opening or stepping the RBU object,
157251 ** this function may return NULL. The error code and message may be collected
157252 ** when sqlite3rbu_close() is called.
157253 */
157254 SQLITE_API sqlite3 *SQLITE_STDCALL sqlite3rbu_db(sqlite3rbu*, int bRbu);
157255
157256 /*
157257 ** Do some work towards applying the RBU update to the target db.
157258 **
157259 ** Return SQLITE_DONE if the update has been completely applied, or
157260 ** SQLITE_OK if no error occurs but there remains work to do to apply
157261 ** the RBU update. If an error does occur, some other error code is
157262 ** returned.
157263 **
157264 ** Once a call to sqlite3rbu_step() has returned a value other than
157265 ** SQLITE_OK, all subsequent calls on the same RBU handle are no-ops
157266 ** that immediately return the same value.
157267 */
157268 SQLITE_API int SQLITE_STDCALL sqlite3rbu_step(sqlite3rbu *pRbu);
157269
157270 /*
157271 ** Close an RBU handle.
157272 **
157273 ** If the RBU update has been completely applied, mark the RBU database
157274 ** as fully applied. Otherwise, assuming no error has occurred, save the
157275 ** current state of the RBU update appliation to the RBU database.
157276 **
157277 ** If an error has already occurred as part of an sqlite3rbu_step()
157278 ** or sqlite3rbu_open() call, or if one occurs within this function, an
157279 ** SQLite error code is returned. Additionally, *pzErrmsg may be set to
157280 ** point to a buffer containing a utf-8 formatted English language error
157281 ** message. It is the responsibility of the caller to eventually free any
157282 ** such buffer using sqlite3_free().
157283 **
157284 ** Otherwise, if no error occurs, this function returns SQLITE_OK if the
157285 ** update has been partially applied, or SQLITE_DONE if it has been
157286 ** completely applied.
157287 */
157288 SQLITE_API int SQLITE_STDCALL sqlite3rbu_close(sqlite3rbu *pRbu, char **pzErrmsg);
157289
157290 /*
157291 ** Return the total number of key-value operations (inserts, deletes or
157292 ** updates) that have been performed on the target database since the
157293 ** current RBU update was started.
157294 */
157295 SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3rbu_progress(sqlite3rbu *pRbu);
157296
157297 /*
157298 ** Create an RBU VFS named zName that accesses the underlying file-system
157299 ** via existing VFS zParent. Or, if the zParent parameter is passed NULL,
157300 ** then the new RBU VFS uses the default system VFS to access the file-system.
157301 ** The new object is registered as a non-default VFS with SQLite before
157302 ** returning.
157303 **
157304 ** Part of the RBU implementation uses a custom VFS object. Usually, this
157305 ** object is created and deleted automatically by RBU.
157306 **
157307 ** The exception is for applications that also use zipvfs. In this case,
157308 ** the custom VFS must be explicitly created by the user before the RBU
157309 ** handle is opened. The RBU VFS should be installed so that the zipvfs
157310 ** VFS uses the RBU VFS, which in turn uses any other VFS layers in use
157311 ** (for example multiplexor) to access the file-system. For example,
157312 ** to assemble an RBU enabled VFS stack that uses both zipvfs and
157313 ** multiplexor (error checking omitted):
157314 **
157315 ** // Create a VFS named "multiplex" (not the default).
157316 ** sqlite3_multiplex_initialize(0, 0);
157317 **
157318 ** // Create an rbu VFS named "rbu" that uses multiplexor. If the
157319 ** // second argument were replaced with NULL, the "rbu" VFS would
157320 ** // access the file-system via the system default VFS, bypassing the
157321 ** // multiplexor.
157322 ** sqlite3rbu_create_vfs("rbu", "multiplex");
157323 **
157324 ** // Create a zipvfs VFS named "zipvfs" that uses rbu.
157325 ** zipvfs_create_vfs_v3("zipvfs", "rbu", 0, xCompressorAlgorithmDetector);
157326 **
157327 ** // Make zipvfs the default VFS.
157328 ** sqlite3_vfs_register(sqlite3_vfs_find("zipvfs"), 1);
157329 **
157330 ** Because the default VFS created above includes a RBU functionality, it
157331 ** may be used by RBU clients. Attempting to use RBU with a zipvfs VFS stack
157332 ** that does not include the RBU layer results in an error.
157333 **
157334 ** The overhead of adding the "rbu" VFS to the system is negligible for
157335 ** non-RBU users. There is no harm in an application accessing the
157336 ** file-system via "rbu" all the time, even if it only uses RBU functionality
157337 ** occasionally.
157338 */
157339 SQLITE_API int SQLITE_STDCALL sqlite3rbu_create_vfs(const char *zName, const char *zParent);
157340
157341 /*
157342 ** Deregister and destroy an RBU vfs created by an earlier call to
157343 ** sqlite3rbu_create_vfs().
157344 **
157345 ** VFS objects are not reference counted. If a VFS object is destroyed
157346 ** before all database handles that use it have been closed, the results
157347 ** are undefined.
157348 */
157349 SQLITE_API void SQLITE_STDCALL sqlite3rbu_destroy_vfs(const char *zName);
157350
157351 #endif /* _SQLITE3RBU_H */
157352
157353 /************** End of sqlite3rbu.h ******************************************/
157354 /************** Continuing where we left off in sqlite3rbu.c *****************/
157355
157356 /* Maximum number of prepared UPDATE statements held by this module */
157357 #define SQLITE_RBU_UPDATE_CACHESIZE 16
157358
157359 /*
157360 ** Swap two objects of type TYPE.
157361 */
157362 #if !defined(SQLITE_AMALGAMATION)
157363 # define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
157364 #endif
157365
157366 /*
157367 ** The rbu_state table is used to save the state of a partially applied
157368 ** update so that it can be resumed later. The table consists of integer
157369 ** keys mapped to values as follows:
157370 **
157371 ** RBU_STATE_STAGE:
157372 ** May be set to integer values 1, 2, 4 or 5. As follows:
157373 ** 1: the *-rbu file is currently under construction.
157374 ** 2: the *-rbu file has been constructed, but not yet moved
157375 ** to the *-wal path.
157376 ** 4: the checkpoint is underway.
157377 ** 5: the rbu update has been checkpointed.
157378 **
157379 ** RBU_STATE_TBL:
157380 ** Only valid if STAGE==1. The target database name of the table
157381 ** currently being written.
157382 **
157383 ** RBU_STATE_IDX:
157384 ** Only valid if STAGE==1. The target database name of the index
157385 ** currently being written, or NULL if the main table is currently being
157386 ** updated.
157387 **
157388 ** RBU_STATE_ROW:
157389 ** Only valid if STAGE==1. Number of rows already processed for the current
157390 ** table/index.
157391 **
157392 ** RBU_STATE_PROGRESS:
157393 ** Trbul number of sqlite3rbu_step() calls made so far as part of this
157394 ** rbu update.
157395 **
157396 ** RBU_STATE_CKPT:
157397 ** Valid if STAGE==4. The 64-bit checksum associated with the wal-index
157398 ** header created by recovering the *-wal file. This is used to detect
157399 ** cases when another client appends frames to the *-wal file in the
157400 ** middle of an incremental checkpoint (an incremental checkpoint cannot
157401 ** be continued if this happens).
157402 **
157403 ** RBU_STATE_COOKIE:
157404 ** Valid if STAGE==1. The current change-counter cookie value in the
157405 ** target db file.
157406 **
157407 ** RBU_STATE_OALSZ:
157408 ** Valid if STAGE==1. The size in bytes of the *-oal file.
157409 */
157410 #define RBU_STATE_STAGE 1
157411 #define RBU_STATE_TBL 2
157412 #define RBU_STATE_IDX 3
157413 #define RBU_STATE_ROW 4
157414 #define RBU_STATE_PROGRESS 5
157415 #define RBU_STATE_CKPT 6
157416 #define RBU_STATE_COOKIE 7
157417 #define RBU_STATE_OALSZ 8
157418
157419 #define RBU_STAGE_OAL 1
157420 #define RBU_STAGE_MOVE 2
157421 #define RBU_STAGE_CAPTURE 3
157422 #define RBU_STAGE_CKPT 4
157423 #define RBU_STAGE_DONE 5
157424
157425
157426 #define RBU_CREATE_STATE \
157427 "CREATE TABLE IF NOT EXISTS %s.rbu_state(k INTEGER PRIMARY KEY, v)"
157428
157429 typedef struct RbuFrame RbuFrame;
157430 typedef struct RbuObjIter RbuObjIter;
157431 typedef struct RbuState RbuState;
157432 typedef struct rbu_vfs rbu_vfs;
157433 typedef struct rbu_file rbu_file;
157434 typedef struct RbuUpdateStmt RbuUpdateStmt;
157435
157436 #if !defined(SQLITE_AMALGAMATION)
157437 typedef unsigned int u32;
157438 typedef unsigned char u8;
157439 typedef sqlite3_int64 i64;
@@ -157441,13 +157447,13 @@
157447 #define WAL_LOCK_WRITE 0
157448 #define WAL_LOCK_CKPT 1
157449 #define WAL_LOCK_READ0 3
157450
157451 /*
157452 ** A structure to store values read from the rbu_state table in memory.
157453 */
157454 struct RbuState {
157455 int eStage;
157456 char *zTbl;
157457 char *zIdx;
157458 i64 iWalCksum;
157459 int nRow;
@@ -157454,14 +157460,14 @@
157460 i64 nProgress;
157461 u32 iCookie;
157462 i64 iOalSz;
157463 };
157464
157465 struct RbuUpdateStmt {
157466 char *zMask; /* Copy of update mask used with pUpdate */
157467 sqlite3_stmt *pUpdate; /* Last update statement (or NULL) */
157468 RbuUpdateStmt *pNext;
157469 };
157470
157471 /*
157472 ** An iterator of this type is used to iterate through all objects in
157473 ** the target database that require updating. For each such table, the
@@ -157476,131 +157482,131 @@
157482 ** it points to an array of flags nTblCol elements in size. The flag is
157483 ** set for each column that is either a part of the PK or a part of an
157484 ** index. Or clear otherwise.
157485 **
157486 */
157487 struct RbuObjIter {
157488 sqlite3_stmt *pTblIter; /* Iterate through tables */
157489 sqlite3_stmt *pIdxIter; /* Index iterator */
157490 int nTblCol; /* Size of azTblCol[] array */
157491 char **azTblCol; /* Array of unquoted target column names */
157492 char **azTblType; /* Array of target column types */
157493 int *aiSrcOrder; /* src table col -> target table col */
157494 u8 *abTblPk; /* Array of flags, set on target PK columns */
157495 u8 *abNotNull; /* Array of flags, set on NOT NULL columns */
157496 u8 *abIndexed; /* Array of flags, set on indexed & PK cols */
157497 int eType; /* Table type - an RBU_PK_XXX value */
157498
157499 /* Output variables. zTbl==0 implies EOF. */
157500 int bCleanup; /* True in "cleanup" state */
157501 const char *zTbl; /* Name of target db table */
157502 const char *zIdx; /* Name of target db index (or null) */
157503 int iTnum; /* Root page of current object */
157504 int iPkTnum; /* If eType==EXTERNAL, root of PK index */
157505 int bUnique; /* Current index is unique */
157506
157507 /* Statements created by rbuObjIterPrepareAll() */
157508 int nCol; /* Number of columns in current object */
157509 sqlite3_stmt *pSelect; /* Source data */
157510 sqlite3_stmt *pInsert; /* Statement for INSERT operations */
157511 sqlite3_stmt *pDelete; /* Statement for DELETE ops */
157512 sqlite3_stmt *pTmpInsert; /* Insert into rbu_tmp_$zTbl */
157513
157514 /* Last UPDATE used (for PK b-tree updates only), or NULL. */
157515 RbuUpdateStmt *pRbuUpdate;
157516 };
157517
157518 /*
157519 ** Values for RbuObjIter.eType
157520 **
157521 ** 0: Table does not exist (error)
157522 ** 1: Table has an implicit rowid.
157523 ** 2: Table has an explicit IPK column.
157524 ** 3: Table has an external PK index.
157525 ** 4: Table is WITHOUT ROWID.
157526 ** 5: Table is a virtual table.
157527 */
157528 #define RBU_PK_NOTABLE 0
157529 #define RBU_PK_NONE 1
157530 #define RBU_PK_IPK 2
157531 #define RBU_PK_EXTERNAL 3
157532 #define RBU_PK_WITHOUT_ROWID 4
157533 #define RBU_PK_VTAB 5
157534
157535
157536 /*
157537 ** Within the RBU_STAGE_OAL stage, each call to sqlite3rbu_step() performs
157538 ** one of the following operations.
157539 */
157540 #define RBU_INSERT 1 /* Insert on a main table b-tree */
157541 #define RBU_DELETE 2 /* Delete a row from a main table b-tree */
157542 #define RBU_IDX_DELETE 3 /* Delete a row from an aux. index b-tree */
157543 #define RBU_IDX_INSERT 4 /* Insert on an aux. index b-tree */
157544 #define RBU_UPDATE 5 /* Update a row in a main table b-tree */
157545
157546
157547 /*
157548 ** A single step of an incremental checkpoint - frame iWalFrame of the wal
157549 ** file should be copied to page iDbPage of the database file.
157550 */
157551 struct RbuFrame {
157552 u32 iDbPage;
157553 u32 iWalFrame;
157554 };
157555
157556 /*
157557 ** RBU handle.
157558 */
157559 struct sqlite3rbu {
157560 int eStage; /* Value of RBU_STATE_STAGE field */
157561 sqlite3 *dbMain; /* target database handle */
157562 sqlite3 *dbRbu; /* rbu database handle */
157563 char *zTarget; /* Path to target db */
157564 char *zRbu; /* Path to rbu db */
157565 char *zState; /* Path to state db (or NULL if zRbu) */
157566 char zStateDb[5]; /* Db name for state ("stat" or "main") */
157567 int rc; /* Value returned by last rbu_step() call */
157568 char *zErrmsg; /* Error message if rc!=SQLITE_OK */
157569 int nStep; /* Rows processed for current object */
157570 int nProgress; /* Rows processed for all objects */
157571 RbuObjIter objiter; /* Iterator for skipping through tbl/idx */
157572 const char *zVfsName; /* Name of automatically created rbu vfs */
157573 rbu_file *pTargetFd; /* File handle open on target db */
157574 i64 iOalSz;
157575
157576 /* The following state variables are used as part of the incremental
157577 ** checkpoint stage (eStage==RBU_STAGE_CKPT). See comments surrounding
157578 ** function rbuSetupCheckpoint() for details. */
157579 u32 iMaxFrame; /* Largest iWalFrame value in aFrame[] */
157580 u32 mLock;
157581 int nFrame; /* Entries in aFrame[] array */
157582 int nFrameAlloc; /* Allocated size of aFrame[] array */
157583 RbuFrame *aFrame;
157584 int pgsz;
157585 u8 *aBuf;
157586 i64 iWalCksum;
157587 };
157588
157589 /*
157590 ** An rbu VFS is implemented using an instance of this structure.
157591 */
157592 struct rbu_vfs {
157593 sqlite3_vfs base; /* rbu VFS shim methods */
157594 sqlite3_vfs *pRealVfs; /* Underlying VFS */
157595 sqlite3_mutex *mutex; /* Mutex to protect pMain */
157596 rbu_file *pMain; /* Linked list of main db files */
157597 };
157598
157599 /*
157600 ** Each file opened by an rbu VFS is represented by an instance of
157601 ** the following structure.
157602 */
157603 struct rbu_file {
157604 sqlite3_file base; /* sqlite3_file methods */
157605 sqlite3_file *pReal; /* Underlying file handle */
157606 rbu_vfs *pRbuVfs; /* Pointer to the rbu_vfs object */
157607 sqlite3rbu *pRbu; /* Pointer to rbu object (rbu target only) */
157608
157609 int openFlags; /* Flags this file was opened with */
157610 u32 iCookie; /* Cookie value for main db files */
157611 u8 iWriteVer; /* "write-version" value for main db files */
157612
@@ -157607,12 +157613,12 @@
157613 int nShm; /* Number of entries in apShm[] array */
157614 char **apShm; /* Array of mmap'd *-shm regions */
157615 char *zDel; /* Delete this when closing file */
157616
157617 const char *zWal; /* Wal filename for this main db file */
157618 rbu_file *pWalFd; /* Wal file descriptor for this main db */
157619 rbu_file *pMainNext; /* Next MAIN_DB file */
157620 };
157621
157622
157623 /*
157624 ** Prepare the SQL statement in buffer zSql against database handle db.
@@ -157686,14 +157692,14 @@
157692 }
157693 return rc;
157694 }
157695
157696 /*
157697 ** Free the RbuObjIter.azTblCol[] and RbuObjIter.abTblPk[] arrays allocated
157698 ** by an earlier call to rbuObjIterCacheTableInfo().
157699 */
157700 static void rbuObjIterFreeCols(RbuObjIter *pIter){
157701 int i;
157702 for(i=0; i<pIter->nTblCol; i++){
157703 sqlite3_free(pIter->azTblCol[i]);
157704 sqlite3_free(pIter->azTblType[i]);
157705 }
@@ -157709,72 +157715,72 @@
157715
157716 /*
157717 ** Finalize all statements and free all allocations that are specific to
157718 ** the current object (table/index pair).
157719 */
157720 static void rbuObjIterClearStatements(RbuObjIter *pIter){
157721 RbuUpdateStmt *pUp;
157722
157723 sqlite3_finalize(pIter->pSelect);
157724 sqlite3_finalize(pIter->pInsert);
157725 sqlite3_finalize(pIter->pDelete);
157726 sqlite3_finalize(pIter->pTmpInsert);
157727 pUp = pIter->pRbuUpdate;
157728 while( pUp ){
157729 RbuUpdateStmt *pTmp = pUp->pNext;
157730 sqlite3_finalize(pUp->pUpdate);
157731 sqlite3_free(pUp);
157732 pUp = pTmp;
157733 }
157734
157735 pIter->pSelect = 0;
157736 pIter->pInsert = 0;
157737 pIter->pDelete = 0;
157738 pIter->pRbuUpdate = 0;
157739 pIter->pTmpInsert = 0;
157740 pIter->nCol = 0;
157741 }
157742
157743 /*
157744 ** Clean up any resources allocated as part of the iterator object passed
157745 ** as the only argument.
157746 */
157747 static void rbuObjIterFinalize(RbuObjIter *pIter){
157748 rbuObjIterClearStatements(pIter);
157749 sqlite3_finalize(pIter->pTblIter);
157750 sqlite3_finalize(pIter->pIdxIter);
157751 rbuObjIterFreeCols(pIter);
157752 memset(pIter, 0, sizeof(RbuObjIter));
157753 }
157754
157755 /*
157756 ** Advance the iterator to the next position.
157757 **
157758 ** If no error occurs, SQLITE_OK is returned and the iterator is left
157759 ** pointing to the next entry. Otherwise, an error code and message is
157760 ** left in the RBU handle passed as the first argument. A copy of the
157761 ** error code is returned.
157762 */
157763 static int rbuObjIterNext(sqlite3rbu *p, RbuObjIter *pIter){
157764 int rc = p->rc;
157765 if( rc==SQLITE_OK ){
157766
157767 /* Free any SQLite statements used while processing the previous object */
157768 rbuObjIterClearStatements(pIter);
157769 if( pIter->zIdx==0 ){
157770 rc = sqlite3_exec(p->dbMain,
157771 "DROP TRIGGER IF EXISTS temp.rbu_insert_tr;"
157772 "DROP TRIGGER IF EXISTS temp.rbu_update1_tr;"
157773 "DROP TRIGGER IF EXISTS temp.rbu_update2_tr;"
157774 "DROP TRIGGER IF EXISTS temp.rbu_delete_tr;"
157775 , 0, 0, &p->zErrmsg
157776 );
157777 }
157778
157779 if( rc==SQLITE_OK ){
157780 if( pIter->bCleanup ){
157781 rbuObjIterFreeCols(pIter);
157782 pIter->bCleanup = 0;
157783 rc = sqlite3_step(pIter->pTblIter);
157784 if( rc!=SQLITE_ROW ){
157785 rc = resetAndCollectError(pIter->pTblIter, &p->zErrmsg);
157786 pIter->zTbl = 0;
@@ -157803,11 +157809,11 @@
157809 }
157810 }
157811 }
157812
157813 if( rc!=SQLITE_OK ){
157814 rbuObjIterFinalize(pIter);
157815 p->rc = rc;
157816 }
157817 return rc;
157818 }
157819
@@ -157814,18 +157820,18 @@
157820 /*
157821 ** Initialize the iterator structure passed as the second argument.
157822 **
157823 ** If no error occurs, SQLITE_OK is returned and the iterator is left
157824 ** pointing to the first entry. Otherwise, an error code and message is
157825 ** left in the RBU handle passed as the first argument. A copy of the
157826 ** error code is returned.
157827 */
157828 static int rbuObjIterFirst(sqlite3rbu *p, RbuObjIter *pIter){
157829 int rc;
157830 memset(pIter, 0, sizeof(RbuObjIter));
157831
157832 rc = prepareAndCollectError(p->dbRbu, &pIter->pTblIter, &p->zErrmsg,
157833 "SELECT substr(name, 6) FROM sqlite_master "
157834 "WHERE type='table' AND name LIKE 'data_%'"
157835 );
157836
157837 if( rc==SQLITE_OK ){
@@ -157836,23 +157842,23 @@
157842 );
157843 }
157844
157845 pIter->bCleanup = 1;
157846 p->rc = rc;
157847 return rbuObjIterNext(p, pIter);
157848 }
157849
157850 /*
157851 ** This is a wrapper around "sqlite3_mprintf(zFmt, ...)". If an OOM occurs,
157852 ** an error code is stored in the RBU handle passed as the first argument.
157853 **
157854 ** If an error has already occurred (p->rc is already set to something other
157855 ** than SQLITE_OK), then this function returns NULL without modifying the
157856 ** stored error code. In this case it still calls sqlite3_free() on any
157857 ** printf() parameters associated with %z conversions.
157858 */
157859 static char *rbuMPrintf(sqlite3rbu *p, const char *zFmt, ...){
157860 char *zSql = 0;
157861 va_list ap;
157862 va_start(ap, zFmt);
157863 zSql = sqlite3_vmprintf(zFmt, ap);
157864 if( p->rc==SQLITE_OK ){
@@ -157867,17 +157873,17 @@
157873
157874 /*
157875 ** Argument zFmt is a sqlite3_mprintf() style format string. The trailing
157876 ** arguments are the usual subsitution values. This function performs
157877 ** the printf() style substitutions and executes the result as an SQL
157878 ** statement on the RBU handles database.
157879 **
157880 ** If an error occurs, an error code and error message is stored in the
157881 ** RBU handle. If an error has already occurred when this function is
157882 ** called, it is a no-op.
157883 */
157884 static int rbuMPrintfExec(sqlite3rbu *p, sqlite3 *db, const char *zFmt, ...){
157885 va_list ap;
157886 va_start(ap, zFmt);
157887 char *zSql = sqlite3_vmprintf(zFmt, ap);
157888 if( p->rc==SQLITE_OK ){
157889 if( zSql==0 ){
@@ -157894,16 +157900,16 @@
157900 /*
157901 ** Attempt to allocate and return a pointer to a zeroed block of nByte
157902 ** bytes.
157903 **
157904 ** If an error (i.e. an OOM condition) occurs, return NULL and leave an
157905 ** error code in the rbu handle passed as the first argument. Or, if an
157906 ** error has already occurred when this function is called, return NULL
157907 ** immediately without attempting the allocation or modifying the stored
157908 ** error code.
157909 */
157910 static void *rbuMalloc(sqlite3rbu *p, int nByte){
157911 void *pRet = 0;
157912 if( p->rc==SQLITE_OK ){
157913 assert( nByte>0 );
157914 pRet = sqlite3_malloc(nByte);
157915 if( pRet==0 ){
@@ -157917,17 +157923,17 @@
157923
157924
157925 /*
157926 ** Allocate and zero the pIter->azTblCol[] and abTblPk[] arrays so that
157927 ** there is room for at least nCol elements. If an OOM occurs, store an
157928 ** error code in the RBU handle passed as the first argument.
157929 */
157930 static void rbuAllocateIterArrays(sqlite3rbu *p, RbuObjIter *pIter, int nCol){
157931 int nByte = (2*sizeof(char*) + sizeof(int) + 3*sizeof(u8)) * nCol;
157932 char **azNew;
157933
157934 azNew = (char**)rbuMalloc(p, nByte);
157935 if( azNew ){
157936 pIter->azTblCol = azNew;
157937 pIter->azTblType = &azNew[nCol];
157938 pIter->aiSrcOrder = (int*)&pIter->azTblType[nCol];
157939 pIter->abTblPk = (u8*)&pIter->aiSrcOrder[nCol];
@@ -157944,11 +157950,11 @@
157950 **
157951 ** If an OOM condition is encountered when attempting to allocate memory,
157952 ** output variable (*pRc) is set to SQLITE_NOMEM before returning. Otherwise,
157953 ** if the allocation succeeds, (*pRc) is left unchanged.
157954 */
157955 static char *rbuStrndup(const char *zStr, int *pRc){
157956 char *zRet = 0;
157957
157958 assert( *pRc==SQLITE_OK );
157959 if( zStr ){
157960 int nCopy = strlen(zStr) + 1;
@@ -157965,14 +157971,14 @@
157971
157972 /*
157973 ** Finalize the statement passed as the second argument.
157974 **
157975 ** If the sqlite3_finalize() call indicates that an error occurs, and the
157976 ** rbu handle error code is not already set, set the error code and error
157977 ** message accordingly.
157978 */
157979 static void rbuFinalize(sqlite3rbu *p, sqlite3_stmt *pStmt){
157980 sqlite3 *db = sqlite3_db_handle(pStmt);
157981 int rc = sqlite3_finalize(pStmt);
157982 if( p->rc==SQLITE_OK && rc!=SQLITE_OK ){
157983 p->rc = rc;
157984 p->zErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
@@ -157983,16 +157989,16 @@
157989 **
157990 ** peType is of type (int*), a pointer to an output parameter of type
157991 ** (int). This call sets the output parameter as follows, depending
157992 ** on the type of the table specified by parameters dbName and zTbl.
157993 **
157994 ** RBU_PK_NOTABLE: No such table.
157995 ** RBU_PK_NONE: Table has an implicit rowid.
157996 ** RBU_PK_IPK: Table has an explicit IPK column.
157997 ** RBU_PK_EXTERNAL: Table has an external PK index.
157998 ** RBU_PK_WITHOUT_ROWID: Table is WITHOUT ROWID.
157999 ** RBU_PK_VTAB: Table is a virtual table.
158000 **
158001 ** Argument *piPk is also of type (int*), and also points to an output
158002 ** parameter. Unless the table has an external primary key index
158003 ** (i.e. unless *peType is set to 3), then *piPk is set to zero. Or,
158004 ** if the table does have an external primary key index, then *piPk
@@ -158000,28 +158006,28 @@
158006 ** returning.
158007 **
158008 ** ALGORITHM:
158009 **
158010 ** if( no entry exists in sqlite_master ){
158011 ** return RBU_PK_NOTABLE
158012 ** }else if( sql for the entry starts with "CREATE VIRTUAL" ){
158013 ** return RBU_PK_VTAB
158014 ** }else if( "PRAGMA index_list()" for the table contains a "pk" index ){
158015 ** if( the index that is the pk exists in sqlite_master ){
158016 ** *piPK = rootpage of that index.
158017 ** return RBU_PK_EXTERNAL
158018 ** }else{
158019 ** return RBU_PK_WITHOUT_ROWID
158020 ** }
158021 ** }else if( "PRAGMA table_info()" lists one or more "pk" columns ){
158022 ** return RBU_PK_IPK
158023 ** }else{
158024 ** return RBU_PK_NONE
158025 ** }
158026 */
158027 static void rbuTableType(
158028 sqlite3rbu *p,
158029 const char *zTab,
158030 int *peType,
158031 int *piTnum,
158032 int *piPk
158033 ){
@@ -158031,11 +158037,11 @@
158037 ** 2) SELECT count(*) FROM sqlite_master where name=%Q
158038 ** 3) PRAGMA table_info = ?
158039 */
158040 sqlite3_stmt *aStmt[4] = {0, 0, 0, 0};
158041
158042 *peType = RBU_PK_NOTABLE;
158043 *piPk = 0;
158044
158045 assert( p->rc==SQLITE_OK );
158046 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[0], &p->zErrmsg,
158047 sqlite3_mprintf(
@@ -158043,22 +158049,22 @@
158049 " FROM sqlite_master"
158050 " WHERE name=%Q", zTab
158051 ));
158052 if( p->rc!=SQLITE_OK || sqlite3_step(aStmt[0])!=SQLITE_ROW ){
158053 /* Either an error, or no such table. */
158054 goto rbuTableType_end;
158055 }
158056 if( sqlite3_column_int(aStmt[0], 0) ){
158057 *peType = RBU_PK_VTAB; /* virtual table */
158058 goto rbuTableType_end;
158059 }
158060 *piTnum = sqlite3_column_int(aStmt[0], 1);
158061
158062 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[1], &p->zErrmsg,
158063 sqlite3_mprintf("PRAGMA index_list=%Q",zTab)
158064 );
158065 if( p->rc ) goto rbuTableType_end;
158066 while( sqlite3_step(aStmt[1])==SQLITE_ROW ){
158067 const u8 *zOrig = sqlite3_column_text(aStmt[1], 3);
158068 const u8 *zIdx = sqlite3_column_text(aStmt[1], 1);
158069 if( zOrig && zIdx && zOrig[0]=='p' ){
158070 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[2], &p->zErrmsg,
@@ -158066,45 +158072,45 @@
158072 "SELECT rootpage FROM sqlite_master WHERE name = %Q", zIdx
158073 ));
158074 if( p->rc==SQLITE_OK ){
158075 if( sqlite3_step(aStmt[2])==SQLITE_ROW ){
158076 *piPk = sqlite3_column_int(aStmt[2], 0);
158077 *peType = RBU_PK_EXTERNAL;
158078 }else{
158079 *peType = RBU_PK_WITHOUT_ROWID;
158080 }
158081 }
158082 goto rbuTableType_end;
158083 }
158084 }
158085
158086 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[3], &p->zErrmsg,
158087 sqlite3_mprintf("PRAGMA table_info=%Q",zTab)
158088 );
158089 if( p->rc==SQLITE_OK ){
158090 while( sqlite3_step(aStmt[3])==SQLITE_ROW ){
158091 if( sqlite3_column_int(aStmt[3],5)>0 ){
158092 *peType = RBU_PK_IPK; /* explicit IPK column */
158093 goto rbuTableType_end;
158094 }
158095 }
158096 *peType = RBU_PK_NONE;
158097 }
158098
158099 rbuTableType_end: {
158100 int i;
158101 for(i=0; i<sizeof(aStmt)/sizeof(aStmt[0]); i++){
158102 rbuFinalize(p, aStmt[i]);
158103 }
158104 }
158105 }
158106
158107 /*
158108 ** This is a helper function for rbuObjIterCacheTableInfo(). It populates
158109 ** the pIter->abIndexed[] array.
158110 */
158111 static void rbuObjIterCacheIndexedCols(sqlite3rbu *p, RbuObjIter *pIter){
158112 sqlite3_stmt *pList = 0;
158113 int bIndex = 0;
158114
158115 if( p->rc==SQLITE_OK ){
158116 memcpy(pIter->abIndexed, pIter->abTblPk, sizeof(u8)*pIter->nTblCol);
@@ -158122,15 +158128,15 @@
158128 );
158129 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
158130 int iCid = sqlite3_column_int(pXInfo, 1);
158131 if( iCid>=0 ) pIter->abIndexed[iCid] = 1;
158132 }
158133 rbuFinalize(p, pXInfo);
158134 bIndex = 1;
158135 }
158136
158137 rbuFinalize(p, pList);
158138 if( bIndex==0 ) pIter->abIndexed = 0;
158139 }
158140
158141
158142 /*
@@ -158138,67 +158144,67 @@
158144 ** pIter->abTblPk[], pIter->nTblCol and pIter->bRowid variables according to
158145 ** the table (not index) that the iterator currently points to.
158146 **
158147 ** Return SQLITE_OK if successful, or an SQLite error code otherwise. If
158148 ** an error does occur, an error code and error message are also left in
158149 ** the RBU handle.
158150 */
158151 static int rbuObjIterCacheTableInfo(sqlite3rbu *p, RbuObjIter *pIter){
158152 if( pIter->azTblCol==0 ){
158153 sqlite3_stmt *pStmt = 0;
158154 int nCol = 0;
158155 int i; /* for() loop iterator variable */
158156 int bRbuRowid = 0; /* If input table has column "rbu_rowid" */
158157 int iOrder = 0;
158158 int iTnum = 0;
158159
158160 /* Figure out the type of table this step will deal with. */
158161 assert( pIter->eType==0 );
158162 rbuTableType(p, pIter->zTbl, &pIter->eType, &iTnum, &pIter->iPkTnum);
158163 if( p->rc==SQLITE_OK && pIter->eType==RBU_PK_NOTABLE ){
158164 p->rc = SQLITE_ERROR;
158165 p->zErrmsg = sqlite3_mprintf("no such table: %s", pIter->zTbl);
158166 }
158167 if( p->rc ) return p->rc;
158168 if( pIter->zIdx==0 ) pIter->iTnum = iTnum;
158169
158170 assert( pIter->eType==RBU_PK_NONE || pIter->eType==RBU_PK_IPK
158171 || pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_WITHOUT_ROWID
158172 || pIter->eType==RBU_PK_VTAB
158173 );
158174
158175 /* Populate the azTblCol[] and nTblCol variables based on the columns
158176 ** of the input table. Ignore any input table columns that begin with
158177 ** "rbu_". */
158178 p->rc = prepareFreeAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
158179 sqlite3_mprintf("SELECT * FROM 'data_%q'", pIter->zTbl)
158180 );
158181 if( p->rc==SQLITE_OK ){
158182 nCol = sqlite3_column_count(pStmt);
158183 rbuAllocateIterArrays(p, pIter, nCol);
158184 }
158185 for(i=0; p->rc==SQLITE_OK && i<nCol; i++){
158186 const char *zName = (const char*)sqlite3_column_name(pStmt, i);
158187 if( sqlite3_strnicmp("rbu_", zName, 4) ){
158188 char *zCopy = rbuStrndup(zName, &p->rc);
158189 pIter->aiSrcOrder[pIter->nTblCol] = pIter->nTblCol;
158190 pIter->azTblCol[pIter->nTblCol++] = zCopy;
158191 }
158192 else if( 0==sqlite3_stricmp("rbu_rowid", zName) ){
158193 bRbuRowid = 1;
158194 }
158195 }
158196 sqlite3_finalize(pStmt);
158197 pStmt = 0;
158198
158199 if( p->rc==SQLITE_OK
158200 && bRbuRowid!=(pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE)
158201 ){
158202 p->rc = SQLITE_ERROR;
158203 p->zErrmsg = sqlite3_mprintf(
158204 "table data_%q %s rbu_rowid column", pIter->zTbl,
158205 (bRbuRowid ? "may not have" : "requires")
158206 );
158207 }
158208
158209 /* Check that all non-HIDDEN columns in the destination table are also
158210 ** present in the input table. Populate the abTblPk[], azTblType[] and
@@ -158227,20 +158233,20 @@
158233 if( i!=iOrder ){
158234 SWAP(int, pIter->aiSrcOrder[i], pIter->aiSrcOrder[iOrder]);
158235 SWAP(char*, pIter->azTblCol[i], pIter->azTblCol[iOrder]);
158236 }
158237
158238 pIter->azTblType[iOrder] = rbuStrndup(zType, &p->rc);
158239 pIter->abTblPk[iOrder] = (iPk!=0);
158240 pIter->abNotNull[iOrder] = (u8)bNotNull || (iPk!=0);
158241 iOrder++;
158242 }
158243 }
158244
158245 rbuFinalize(p, pStmt);
158246 rbuObjIterCacheIndexedCols(p, pIter);
158247 assert( pIter->eType!=RBU_PK_VTAB || pIter->abIndexed==0 );
158248 }
158249
158250 return p->rc;
158251 }
158252
@@ -158247,29 +158253,29 @@
158253 /*
158254 ** This function constructs and returns a pointer to a nul-terminated
158255 ** string containing some SQL clause or list based on one or more of the
158256 ** column names currently stored in the pIter->azTblCol[] array.
158257 */
158258 static char *rbuObjIterGetCollist(
158259 sqlite3rbu *p, /* RBU object */
158260 RbuObjIter *pIter /* Object iterator for column names */
158261 ){
158262 char *zList = 0;
158263 const char *zSep = "";
158264 int i;
158265 for(i=0; i<pIter->nTblCol; i++){
158266 const char *z = pIter->azTblCol[i];
158267 zList = rbuMPrintf(p, "%z%s\"%w\"", zList, zSep, z);
158268 zSep = ", ";
158269 }
158270 return zList;
158271 }
158272
158273 /*
158274 ** This function is used to create a SELECT list (the list of SQL
158275 ** expressions that follows a SELECT keyword) for a SELECT statement
158276 ** used to read from an data_xxx or rbu_tmp_xxx table while updating the
158277 ** index object currently indicated by the iterator object passed as the
158278 ** second argument. A "PRAGMA index_xinfo = <idxname>" statement is used
158279 ** to obtain the required information.
158280 **
158281 ** If the index is of the following form:
@@ -158286,17 +158292,17 @@
158292 **
158293 ** pzImposterCols: ...
158294 ** pzImposterPk: ...
158295 ** pzWhere: ...
158296 */
158297 static char *rbuObjIterGetIndexCols(
158298 sqlite3rbu *p, /* RBU object */
158299 RbuObjIter *pIter, /* Object iterator for column names */
158300 char **pzImposterCols, /* OUT: Columns for imposter table */
158301 char **pzImposterPk, /* OUT: Imposter PK clause */
158302 char **pzWhere, /* OUT: WHERE clause */
158303 int *pnBind /* OUT: Trbul number of columns */
158304 ){
158305 int rc = p->rc; /* Error code */
158306 int rc2; /* sqlite3_finalize() return code */
158307 char *zRet = 0; /* String to return */
158308 char *zImpCols = 0; /* String to return via *pzImposterCols */
@@ -158321,18 +158327,18 @@
158327 const char *zCol;
158328 const char *zType;
158329
158330 if( iCid<0 ){
158331 /* An integer primary key. If the table has an explicit IPK, use
158332 ** its name. Otherwise, use "rbu_rowid". */
158333 if( pIter->eType==RBU_PK_IPK ){
158334 int i;
158335 for(i=0; pIter->abTblPk[i]==0; i++);
158336 assert( i<pIter->nTblCol );
158337 zCol = pIter->azTblCol[i];
158338 }else{
158339 zCol = "rbu_rowid";
158340 }
158341 zType = "INTEGER";
158342 }else{
158343 zCol = pIter->azTblCol[iCid];
158344 zType = pIter->azTblType[iCid];
@@ -158339,19 +158345,19 @@
158345 }
158346
158347 zRet = sqlite3_mprintf("%z%s\"%w\" COLLATE %Q", zRet, zCom, zCol, zCollate);
158348 if( pIter->bUnique==0 || sqlite3_column_int(pXInfo, 5) ){
158349 const char *zOrder = (bDesc ? " DESC" : "");
158350 zImpPK = sqlite3_mprintf("%z%s\"rbu_imp_%d%w\"%s",
158351 zImpPK, zCom, nBind, zCol, zOrder
158352 );
158353 }
158354 zImpCols = sqlite3_mprintf("%z%s\"rbu_imp_%d%w\" %s COLLATE %Q",
158355 zImpCols, zCom, nBind, zCol, zType, zCollate
158356 );
158357 zWhere = sqlite3_mprintf(
158358 "%z%s\"rbu_imp_%d%w\" IS ?", zWhere, zAnd, nBind, zCol
158359 );
158360 if( zRet==0 || zImpPK==0 || zImpCols==0 || zWhere==0 ) rc = SQLITE_NOMEM;
158361 zCom = ", ";
158362 zAnd = " AND ";
158363 nBind++;
@@ -158385,16 +158391,16 @@
158391 **
158392 ** "old.a, old.b, old.b"
158393 **
158394 ** With the column names escaped.
158395 **
158396 ** For tables with implicit rowids - RBU_PK_EXTERNAL and RBU_PK_NONE, append
158397 ** the text ", old._rowid_" to the returned value.
158398 */
158399 static char *rbuObjIterGetOldlist(
158400 sqlite3rbu *p,
158401 RbuObjIter *pIter,
158402 const char *zObj
158403 ){
158404 char *zList = 0;
158405 if( p->rc==SQLITE_OK && pIter->abIndexed ){
158406 const char *zS = "";
@@ -158412,12 +158418,12 @@
158418 break;
158419 }
158420 }
158421
158422 /* For a table with implicit rowids, append "old._rowid_" to the list. */
158423 if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
158424 zList = rbuMPrintf(p, "%z, %s._rowid_", zList, zObj);
158425 }
158426 }
158427 return zList;
158428 }
158429
@@ -158429,37 +158435,37 @@
158435 **
158436 ** Return the string:
158437 **
158438 ** "b = ?1 AND c = ?2"
158439 */
158440 static char *rbuObjIterGetWhere(
158441 sqlite3rbu *p,
158442 RbuObjIter *pIter
158443 ){
158444 char *zList = 0;
158445 if( pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE ){
158446 zList = rbuMPrintf(p, "_rowid_ = ?%d", pIter->nTblCol+1);
158447 }else if( pIter->eType==RBU_PK_EXTERNAL ){
158448 const char *zSep = "";
158449 int i;
158450 for(i=0; i<pIter->nTblCol; i++){
158451 if( pIter->abTblPk[i] ){
158452 zList = rbuMPrintf(p, "%z%sc%d=?%d", zList, zSep, i, i+1);
158453 zSep = " AND ";
158454 }
158455 }
158456 zList = rbuMPrintf(p,
158457 "_rowid_ = (SELECT id FROM rbu_imposter2 WHERE %z)", zList
158458 );
158459
158460 }else{
158461 const char *zSep = "";
158462 int i;
158463 for(i=0; i<pIter->nTblCol; i++){
158464 if( pIter->abTblPk[i] ){
158465 const char *zCol = pIter->azTblCol[i];
158466 zList = rbuMPrintf(p, "%z%s\"%w\"=?%d", zList, zSep, zCol, i+1);
158467 zSep = " AND ";
158468 }
158469 }
158470 }
158471 return zList;
@@ -158466,60 +158472,60 @@
158472 }
158473
158474 /*
158475 ** The SELECT statement iterating through the keys for the current object
158476 ** (p->objiter.pSelect) currently points to a valid row. However, there
158477 ** is something wrong with the rbu_control value in the rbu_control value
158478 ** stored in the (p->nCol+1)'th column. Set the error code and error message
158479 ** of the RBU handle to something reflecting this.
158480 */
158481 static void rbuBadControlError(sqlite3rbu *p){
158482 p->rc = SQLITE_ERROR;
158483 p->zErrmsg = sqlite3_mprintf("invalid rbu_control value");
158484 }
158485
158486
158487 /*
158488 ** Return a nul-terminated string containing the comma separated list of
158489 ** assignments that should be included following the "SET" keyword of
158490 ** an UPDATE statement used to update the table object that the iterator
158491 ** passed as the second argument currently points to if the rbu_control
158492 ** column of the data_xxx table entry is set to zMask.
158493 **
158494 ** The memory for the returned string is obtained from sqlite3_malloc().
158495 ** It is the responsibility of the caller to eventually free it using
158496 ** sqlite3_free().
158497 **
158498 ** If an OOM error is encountered when allocating space for the new
158499 ** string, an error code is left in the rbu handle passed as the first
158500 ** argument and NULL is returned. Or, if an error has already occurred
158501 ** when this function is called, NULL is returned immediately, without
158502 ** attempting the allocation or modifying the stored error code.
158503 */
158504 static char *rbuObjIterGetSetlist(
158505 sqlite3rbu *p,
158506 RbuObjIter *pIter,
158507 const char *zMask
158508 ){
158509 char *zList = 0;
158510 if( p->rc==SQLITE_OK ){
158511 int i;
158512
158513 if( strlen(zMask)!=pIter->nTblCol ){
158514 rbuBadControlError(p);
158515 }else{
158516 const char *zSep = "";
158517 for(i=0; i<pIter->nTblCol; i++){
158518 char c = zMask[pIter->aiSrcOrder[i]];
158519 if( c=='x' ){
158520 zList = rbuMPrintf(p, "%z%s\"%w\"=?%d",
158521 zList, zSep, pIter->azTblCol[i], i+1
158522 );
158523 zSep = ", ";
158524 }
158525 if( c=='d' ){
158526 zList = rbuMPrintf(p, "%z%s\"%w\"=rbu_delta(\"%w\", ?%d)",
158527 zList, zSep, pIter->azTblCol[i], pIter->azTblCol[i], i+1
158528 );
158529 zSep = ", ";
158530 }
158531 }
@@ -158536,20 +158542,20 @@
158542 ** The memory for the returned string is obtained from sqlite3_malloc().
158543 ** It is the responsibility of the caller to eventually free it using
158544 ** sqlite3_free().
158545 **
158546 ** If an OOM error is encountered when allocating space for the new
158547 ** string, an error code is left in the rbu handle passed as the first
158548 ** argument and NULL is returned. Or, if an error has already occurred
158549 ** when this function is called, NULL is returned immediately, without
158550 ** attempting the allocation or modifying the stored error code.
158551 */
158552 static char *rbuObjIterGetBindlist(sqlite3rbu *p, int nBind){
158553 char *zRet = 0;
158554 int nByte = nBind*2 + 1;
158555
158556 zRet = (char*)rbuMalloc(p, nByte);
158557 if( zRet ){
158558 int i;
158559 for(i=0; i<nBind; i++){
158560 zRet[i*2] = '?';
158561 zRet[i*2+1] = (i+1==nBind) ? '\0' : ',';
@@ -158558,21 +158564,21 @@
158564 return zRet;
158565 }
158566
158567 /*
158568 ** The iterator currently points to a table (not index) of type
158569 ** RBU_PK_WITHOUT_ROWID. This function creates the PRIMARY KEY
158570 ** declaration for the corresponding imposter table. For example,
158571 ** if the iterator points to a table created as:
158572 **
158573 ** CREATE TABLE t1(a, b, c, PRIMARY KEY(b, a DESC)) WITHOUT ROWID
158574 **
158575 ** this function returns:
158576 **
158577 ** PRIMARY KEY("b", "a" DESC)
158578 */
158579 static char *rbuWithoutRowidPK(sqlite3rbu *p, RbuObjIter *pIter){
158580 char *z = 0;
158581 assert( pIter->zIdx==0 );
158582 if( p->rc==SQLITE_OK ){
158583 const char *zSep = "PRIMARY KEY(";
158584 sqlite3_stmt *pXList = 0; /* PRAGMA index_list = (pIter->zTbl) */
@@ -158591,23 +158597,23 @@
158597 );
158598 }
158599 break;
158600 }
158601 }
158602 rbuFinalize(p, pXList);
158603
158604 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
158605 if( sqlite3_column_int(pXInfo, 5) ){
158606 /* int iCid = sqlite3_column_int(pXInfo, 0); */
158607 const char *zCol = (const char*)sqlite3_column_text(pXInfo, 2);
158608 const char *zDesc = sqlite3_column_int(pXInfo, 3) ? " DESC" : "";
158609 z = rbuMPrintf(p, "%z%s\"%w\"%s", z, zSep, zCol, zDesc);
158610 zSep = ", ";
158611 }
158612 }
158613 z = rbuMPrintf(p, "%z)", z);
158614 rbuFinalize(p, pXInfo);
158615 }
158616 return z;
158617 }
158618
158619 /*
@@ -158616,23 +158622,23 @@
158622 ** iterator passed as the second argument does not currently point to
158623 ** a table (not index) with an external primary key, this function is a
158624 ** no-op.
158625 **
158626 ** Assuming the iterator does point to a table with an external PK, this
158627 ** function creates a WITHOUT ROWID imposter table named "rbu_imposter2"
158628 ** used to access that PK index. For example, if the target table is
158629 ** declared as follows:
158630 **
158631 ** CREATE TABLE t1(a, b TEXT, c REAL, PRIMARY KEY(b, c));
158632 **
158633 ** then the imposter table schema is:
158634 **
158635 ** CREATE TABLE rbu_imposter2(c1 TEXT, c2 REAL, id INTEGER) WITHOUT ROWID;
158636 **
158637 */
158638 static void rbuCreateImposterTable2(sqlite3rbu *p, RbuObjIter *pIter){
158639 if( p->rc==SQLITE_OK && pIter->eType==RBU_PK_EXTERNAL ){
158640 int tnum = pIter->iPkTnum; /* Root page of PK index */
158641 sqlite3_stmt *pQuery = 0; /* SELECT name ... WHERE rootpage = $tnum */
158642 const char *zIdx = 0; /* Name of PK index */
158643 sqlite3_stmt *pXInfo = 0; /* PRAGMA main.index_xinfo = $zIdx */
158644 const char *zComma = "";
@@ -158654,31 +158660,31 @@
158660 if( zIdx ){
158661 p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
158662 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
158663 );
158664 }
158665 rbuFinalize(p, pQuery);
158666
158667 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
158668 int bKey = sqlite3_column_int(pXInfo, 5);
158669 if( bKey ){
158670 int iCid = sqlite3_column_int(pXInfo, 1);
158671 int bDesc = sqlite3_column_int(pXInfo, 3);
158672 const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
158673 zCols = rbuMPrintf(p, "%z%sc%d %s COLLATE %s", zCols, zComma,
158674 iCid, pIter->azTblType[iCid], zCollate
158675 );
158676 zPk = rbuMPrintf(p, "%z%sc%d%s", zPk, zComma, iCid, bDesc?" DESC":"");
158677 zComma = ", ";
158678 }
158679 }
158680 zCols = rbuMPrintf(p, "%z, id INTEGER", zCols);
158681 rbuFinalize(p, pXInfo);
158682
158683 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1, tnum);
158684 rbuMPrintfExec(p, p->dbMain,
158685 "CREATE TABLE rbu_imposter2(%z, PRIMARY KEY(%z)) WITHOUT ROWID",
158686 zCols, zPk
158687 );
158688 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
158689 }
158690 }
@@ -158685,28 +158691,28 @@
158691
158692 /*
158693 ** If an error has already occurred when this function is called, it
158694 ** immediately returns zero (without doing any work). Or, if an error
158695 ** occurs during the execution of this function, it sets the error code
158696 ** in the sqlite3rbu object indicated by the first argument and returns
158697 ** zero.
158698 **
158699 ** The iterator passed as the second argument is guaranteed to point to
158700 ** a table (not an index) when this function is called. This function
158701 ** attempts to create any imposter table required to write to the main
158702 ** table b-tree of the table before returning. Non-zero is returned if
158703 ** an imposter table are created, or zero otherwise.
158704 **
158705 ** An imposter table is required in all cases except RBU_PK_VTAB. Only
158706 ** virtual tables are written to directly. The imposter table has the
158707 ** same schema as the actual target table (less any UNIQUE constraints).
158708 ** More precisely, the "same schema" means the same columns, types,
158709 ** collation sequences. For tables that do not have an external PRIMARY
158710 ** KEY, it also means the same PRIMARY KEY declaration.
158711 */
158712 static void rbuCreateImposterTable(sqlite3rbu *p, RbuObjIter *pIter){
158713 if( p->rc==SQLITE_OK && pIter->eType!=RBU_PK_VTAB ){
158714 int tnum = pIter->iTnum;
158715 const char *zComma = "";
158716 char *zSql = 0;
158717 int iCol;
158718 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 1);
@@ -158718,73 +158724,73 @@
158724
158725 p->rc = sqlite3_table_column_metadata(
158726 p->dbMain, "main", pIter->zTbl, zCol, 0, &zColl, 0, 0, 0
158727 );
158728
158729 if( pIter->eType==RBU_PK_IPK && pIter->abTblPk[iCol] ){
158730 /* If the target table column is an "INTEGER PRIMARY KEY", add
158731 ** "PRIMARY KEY" to the imposter table column declaration. */
158732 zPk = "PRIMARY KEY ";
158733 }
158734 zSql = rbuMPrintf(p, "%z%s\"%w\" %s %sCOLLATE %s%s",
158735 zSql, zComma, zCol, pIter->azTblType[iCol], zPk, zColl,
158736 (pIter->abNotNull[iCol] ? " NOT NULL" : "")
158737 );
158738 zComma = ", ";
158739 }
158740
158741 if( pIter->eType==RBU_PK_WITHOUT_ROWID ){
158742 char *zPk = rbuWithoutRowidPK(p, pIter);
158743 if( zPk ){
158744 zSql = rbuMPrintf(p, "%z, %z", zSql, zPk);
158745 }
158746 }
158747
158748 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1, tnum);
158749 rbuMPrintfExec(p, p->dbMain, "CREATE TABLE \"rbu_imp_%w\"(%z)%s",
158750 pIter->zTbl, zSql,
158751 (pIter->eType==RBU_PK_WITHOUT_ROWID ? " WITHOUT ROWID" : "")
158752 );
158753 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
158754 }
158755 }
158756
158757 /*
158758 ** Prepare a statement used to insert rows into the "rbu_tmp_xxx" table.
158759 ** Specifically a statement of the form:
158760 **
158761 ** INSERT INTO rbu_tmp_xxx VALUES(?, ?, ? ...);
158762 **
158763 ** The number of bound variables is equal to the number of columns in
158764 ** the target table, plus one (for the rbu_control column), plus one more
158765 ** (for the rbu_rowid column) if the target table is an implicit IPK or
158766 ** virtual table.
158767 */
158768 static void rbuObjIterPrepareTmpInsert(
158769 sqlite3rbu *p,
158770 RbuObjIter *pIter,
158771 const char *zCollist,
158772 const char *zRbuRowid
158773 ){
158774 int bRbuRowid = (pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE);
158775 char *zBind = rbuObjIterGetBindlist(p, pIter->nTblCol + 1 + bRbuRowid);
158776 if( zBind ){
158777 assert( pIter->pTmpInsert==0 );
158778 p->rc = prepareFreeAndCollectError(
158779 p->dbRbu, &pIter->pTmpInsert, &p->zErrmsg, sqlite3_mprintf(
158780 "INSERT INTO %s.'rbu_tmp_%q'(rbu_control,%s%s) VALUES(%z)",
158781 p->zStateDb, pIter->zTbl, zCollist, zRbuRowid, zBind
158782 ));
158783 }
158784 }
158785
158786 static void rbuTmpInsertFunc(
158787 sqlite3_context *pCtx,
158788 int nVal,
158789 sqlite3_value **apVal
158790 ){
158791 sqlite3rbu *p = sqlite3_user_data(pCtx);
158792 int rc = SQLITE_OK;
158793 int i;
158794
158795 for(i=0; rc==SQLITE_OK && i<nVal; i++){
158796 rc = sqlite3_bind_value(p->objiter.pTmpInsert, i+1, apVal[i]);
@@ -158802,17 +158808,17 @@
158808 /*
158809 ** Ensure that the SQLite statement handles required to update the
158810 ** target database object currently indicated by the iterator passed
158811 ** as the second argument are available.
158812 */
158813 static int rbuObjIterPrepareAll(
158814 sqlite3rbu *p,
158815 RbuObjIter *pIter,
158816 int nOffset /* Add "LIMIT -1 OFFSET $nOffset" to SELECT */
158817 ){
158818 assert( pIter->bCleanup==0 );
158819 if( pIter->pSelect==0 && rbuObjIterCacheTableInfo(p, pIter)==SQLITE_OK ){
158820 const int tnum = pIter->iTnum;
158821 char *zCollist = 0; /* List of indexed columns */
158822 char **pz = &p->zErrmsg;
158823 const char *zIdx = pIter->zIdx;
158824 char *zLimit = 0;
@@ -158828,104 +158834,104 @@
158834 char *zImposterPK = 0; /* Primary key declaration for imposter */
158835 char *zWhere = 0; /* WHERE clause on PK columns */
158836 char *zBind = 0;
158837 int nBind = 0;
158838
158839 assert( pIter->eType!=RBU_PK_VTAB );
158840 zCollist = rbuObjIterGetIndexCols(
158841 p, pIter, &zImposterCols, &zImposterPK, &zWhere, &nBind
158842 );
158843 zBind = rbuObjIterGetBindlist(p, nBind);
158844
158845 /* Create the imposter table used to write to this index. */
158846 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 1);
158847 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1,tnum);
158848 rbuMPrintfExec(p, p->dbMain,
158849 "CREATE TABLE \"rbu_imp_%w\"( %s, PRIMARY KEY( %s ) ) WITHOUT ROWID",
158850 zTbl, zImposterCols, zImposterPK
158851 );
158852 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
158853
158854 /* Create the statement to insert index entries */
158855 pIter->nCol = nBind;
158856 if( p->rc==SQLITE_OK ){
158857 p->rc = prepareFreeAndCollectError(
158858 p->dbMain, &pIter->pInsert, &p->zErrmsg,
158859 sqlite3_mprintf("INSERT INTO \"rbu_imp_%w\" VALUES(%s)", zTbl, zBind)
158860 );
158861 }
158862
158863 /* And to delete index entries */
158864 if( p->rc==SQLITE_OK ){
158865 p->rc = prepareFreeAndCollectError(
158866 p->dbMain, &pIter->pDelete, &p->zErrmsg,
158867 sqlite3_mprintf("DELETE FROM \"rbu_imp_%w\" WHERE %s", zTbl, zWhere)
158868 );
158869 }
158870
158871 /* Create the SELECT statement to read keys in sorted order */
158872 if( p->rc==SQLITE_OK ){
158873 char *zSql;
158874 if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
158875 zSql = sqlite3_mprintf(
158876 "SELECT %s, rbu_control FROM %s.'rbu_tmp_%q' ORDER BY %s%s",
158877 zCollist, p->zStateDb, pIter->zTbl,
158878 zCollist, zLimit
158879 );
158880 }else{
158881 zSql = sqlite3_mprintf(
158882 "SELECT %s, rbu_control FROM 'data_%q' "
158883 "WHERE typeof(rbu_control)='integer' AND rbu_control!=1 "
158884 "UNION ALL "
158885 "SELECT %s, rbu_control FROM %s.'rbu_tmp_%q' "
158886 "ORDER BY %s%s",
158887 zCollist, pIter->zTbl,
158888 zCollist, p->zStateDb, pIter->zTbl,
158889 zCollist, zLimit
158890 );
158891 }
158892 p->rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pSelect, pz, zSql);
158893 }
158894
158895 sqlite3_free(zImposterCols);
158896 sqlite3_free(zImposterPK);
158897 sqlite3_free(zWhere);
158898 sqlite3_free(zBind);
158899 }else{
158900 int bRbuRowid = (pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE);
158901 const char *zTbl = pIter->zTbl; /* Table this step applies to */
158902 const char *zWrite; /* Imposter table name */
158903
158904 char *zBindings = rbuObjIterGetBindlist(p, pIter->nTblCol + bRbuRowid);
158905 char *zWhere = rbuObjIterGetWhere(p, pIter);
158906 char *zOldlist = rbuObjIterGetOldlist(p, pIter, "old");
158907 char *zNewlist = rbuObjIterGetOldlist(p, pIter, "new");
158908
158909 zCollist = rbuObjIterGetCollist(p, pIter);
158910 pIter->nCol = pIter->nTblCol;
158911
158912 /* Create the SELECT statement to read keys from data_xxx */
158913 if( p->rc==SQLITE_OK ){
158914 p->rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pSelect, pz,
158915 sqlite3_mprintf(
158916 "SELECT %s, rbu_control%s FROM 'data_%q'%s",
158917 zCollist, (bRbuRowid ? ", rbu_rowid" : ""), zTbl, zLimit
158918 )
158919 );
158920 }
158921
158922 /* Create the imposter table or tables (if required). */
158923 rbuCreateImposterTable(p, pIter);
158924 rbuCreateImposterTable2(p, pIter);
158925 zWrite = (pIter->eType==RBU_PK_VTAB ? "" : "rbu_imp_");
158926
158927 /* Create the INSERT statement to write to the target PK b-tree */
158928 if( p->rc==SQLITE_OK ){
158929 p->rc = prepareFreeAndCollectError(p->dbMain, &pIter->pInsert, pz,
158930 sqlite3_mprintf(
158931 "INSERT INTO \"%s%w\"(%s%s) VALUES(%s)",
158932 zWrite, zTbl, zCollist, (bRbuRowid ? ", _rowid_" : ""), zBindings
158933 )
158934 );
158935 }
158936
158937 /* Create the DELETE statement to write to the target PK b-tree */
@@ -158936,55 +158942,55 @@
158942 )
158943 );
158944 }
158945
158946 if( pIter->abIndexed ){
158947 const char *zRbuRowid = "";
158948 if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
158949 zRbuRowid = ", rbu_rowid";
158950 }
158951
158952 /* Create the rbu_tmp_xxx table and the triggers to populate it. */
158953 rbuMPrintfExec(p, p->dbRbu,
158954 "CREATE TABLE IF NOT EXISTS %s.'rbu_tmp_%q' AS "
158955 "SELECT *%s FROM 'data_%q' WHERE 0;"
158956 , p->zStateDb
158957 , zTbl, (pIter->eType==RBU_PK_EXTERNAL ? ", 0 AS rbu_rowid" : "")
158958 , zTbl
158959 );
158960
158961 rbuMPrintfExec(p, p->dbMain,
158962 "CREATE TEMP TRIGGER rbu_delete_tr BEFORE DELETE ON \"%s%w\" "
158963 "BEGIN "
158964 " SELECT rbu_tmp_insert(2, %s);"
158965 "END;"
158966
158967 "CREATE TEMP TRIGGER rbu_update1_tr BEFORE UPDATE ON \"%s%w\" "
158968 "BEGIN "
158969 " SELECT rbu_tmp_insert(2, %s);"
158970 "END;"
158971
158972 "CREATE TEMP TRIGGER rbu_update2_tr AFTER UPDATE ON \"%s%w\" "
158973 "BEGIN "
158974 " SELECT rbu_tmp_insert(3, %s);"
158975 "END;",
158976 zWrite, zTbl, zOldlist,
158977 zWrite, zTbl, zOldlist,
158978 zWrite, zTbl, zNewlist
158979 );
158980
158981 if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
158982 rbuMPrintfExec(p, p->dbMain,
158983 "CREATE TEMP TRIGGER rbu_insert_tr AFTER INSERT ON \"%s%w\" "
158984 "BEGIN "
158985 " SELECT rbu_tmp_insert(0, %s);"
158986 "END;",
158987 zWrite, zTbl, zNewlist
158988 );
158989 }
158990
158991 rbuObjIterPrepareTmpInsert(p, pIter, zCollist, zRbuRowid);
158992 }
158993
158994 sqlite3_free(zWhere);
158995 sqlite3_free(zOldlist);
158996 sqlite3_free(zNewlist);
@@ -158999,68 +159005,68 @@
159005
159006 /*
159007 ** Set output variable *ppStmt to point to an UPDATE statement that may
159008 ** be used to update the imposter table for the main table b-tree of the
159009 ** table object that pIter currently points to, assuming that the
159010 ** rbu_control column of the data_xyz table contains zMask.
159011 **
159012 ** If the zMask string does not specify any columns to update, then this
159013 ** is not an error. Output variable *ppStmt is set to NULL in this case.
159014 */
159015 static int rbuGetUpdateStmt(
159016 sqlite3rbu *p, /* RBU handle */
159017 RbuObjIter *pIter, /* Object iterator */
159018 const char *zMask, /* rbu_control value ('x.x.') */
159019 sqlite3_stmt **ppStmt /* OUT: UPDATE statement handle */
159020 ){
159021 RbuUpdateStmt **pp;
159022 RbuUpdateStmt *pUp = 0;
159023 int nUp = 0;
159024
159025 /* In case an error occurs */
159026 *ppStmt = 0;
159027
159028 /* Search for an existing statement. If one is found, shift it to the front
159029 ** of the LRU queue and return immediately. Otherwise, leave nUp pointing
159030 ** to the number of statements currently in the cache and pUp to the
159031 ** last object in the list. */
159032 for(pp=&pIter->pRbuUpdate; *pp; pp=&((*pp)->pNext)){
159033 pUp = *pp;
159034 if( strcmp(pUp->zMask, zMask)==0 ){
159035 *pp = pUp->pNext;
159036 pUp->pNext = pIter->pRbuUpdate;
159037 pIter->pRbuUpdate = pUp;
159038 *ppStmt = pUp->pUpdate;
159039 return SQLITE_OK;
159040 }
159041 nUp++;
159042 }
159043 assert( pUp==0 || pUp->pNext==0 );
159044
159045 if( nUp>=SQLITE_RBU_UPDATE_CACHESIZE ){
159046 for(pp=&pIter->pRbuUpdate; *pp!=pUp; pp=&((*pp)->pNext));
159047 *pp = 0;
159048 sqlite3_finalize(pUp->pUpdate);
159049 pUp->pUpdate = 0;
159050 }else{
159051 pUp = (RbuUpdateStmt*)rbuMalloc(p, sizeof(RbuUpdateStmt)+pIter->nTblCol+1);
159052 }
159053
159054 if( pUp ){
159055 char *zWhere = rbuObjIterGetWhere(p, pIter);
159056 char *zSet = rbuObjIterGetSetlist(p, pIter, zMask);
159057 char *zUpdate = 0;
159058
159059 pUp->zMask = (char*)&pUp[1];
159060 memcpy(pUp->zMask, zMask, pIter->nTblCol);
159061 pUp->pNext = pIter->pRbuUpdate;
159062 pIter->pRbuUpdate = pUp;
159063
159064 if( zSet ){
159065 const char *zPrefix = "";
159066
159067 if( pIter->eType!=RBU_PK_VTAB ) zPrefix = "rbu_imp_";
159068 zUpdate = sqlite3_mprintf("UPDATE \"%s%w\" SET %s WHERE %s",
159069 zPrefix, pIter->zTbl, zSet, zWhere
159070 );
159071 p->rc = prepareFreeAndCollectError(
159072 p->dbMain, &pUp->pUpdate, &p->zErrmsg, zUpdate
@@ -159072,11 +159078,11 @@
159078 }
159079
159080 return p->rc;
159081 }
159082
159083 static sqlite3 *rbuOpenDbhandle(sqlite3rbu *p, const char *zName){
159084 sqlite3 *db = 0;
159085 if( p->rc==SQLITE_OK ){
159086 const int flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_URI;
159087 p->rc = sqlite3_open_v2(zName, &db, flags, p->zVfsName);
159088 if( p->rc ){
@@ -159087,51 +159093,51 @@
159093 }
159094 return db;
159095 }
159096
159097 /*
159098 ** Open the database handle and attach the RBU database as "rbu". If an
159099 ** error occurs, leave an error code and message in the RBU handle.
159100 */
159101 static void rbuOpenDatabase(sqlite3rbu *p){
159102 assert( p->rc==SQLITE_OK );
159103 assert( p->dbMain==0 && p->dbRbu==0 );
159104
159105 p->eStage = 0;
159106 p->dbMain = rbuOpenDbhandle(p, p->zTarget);
159107 p->dbRbu = rbuOpenDbhandle(p, p->zRbu);
159108
159109 /* If using separate RBU and state databases, attach the state database to
159110 ** the RBU db handle now. */
159111 if( p->zState ){
159112 rbuMPrintfExec(p, p->dbRbu, "ATTACH %Q AS stat", p->zState);
159113 memcpy(p->zStateDb, "stat", 4);
159114 }else{
159115 memcpy(p->zStateDb, "main", 4);
159116 }
159117
159118 if( p->rc==SQLITE_OK ){
159119 p->rc = sqlite3_create_function(p->dbMain,
159120 "rbu_tmp_insert", -1, SQLITE_UTF8, (void*)p, rbuTmpInsertFunc, 0, 0
159121 );
159122 }
159123
159124 if( p->rc==SQLITE_OK ){
159125 p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_RBU, (void*)p);
159126 }
159127 rbuMPrintfExec(p, p->dbMain, "SELECT * FROM sqlite_master");
159128
159129 /* Mark the database file just opened as an RBU target database. If
159130 ** this call returns SQLITE_NOTFOUND, then the RBU vfs is not in use.
159131 ** This is an error. */
159132 if( p->rc==SQLITE_OK ){
159133 p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_RBU, (void*)p);
159134 }
159135
159136 if( p->rc==SQLITE_NOTFOUND ){
159137 p->rc = SQLITE_ERROR;
159138 p->zErrmsg = sqlite3_mprintf("rbu vfs not found");
159139 }
159140 }
159141
159142 /*
159143 ** This routine is a copy of the sqlite3FileSuffix3() routine from the core.
@@ -159151,11 +159157,11 @@
159157 ** test.db-journal => test.nal
159158 ** test.db-wal => test.wal
159159 ** test.db-shm => test.shm
159160 ** test.db-mj7f3319fa => test.9fa
159161 */
159162 static void rbuFileSuffix3(const char *zBase, char *z){
159163 #ifdef SQLITE_ENABLE_8_3_NAMES
159164 #if SQLITE_ENABLE_8_3_NAMES<2
159165 if( sqlite3_uri_boolean(zBase, "8_3_names", 0) )
159166 #endif
159167 {
@@ -159172,11 +159178,11 @@
159178 ** as a 64-bit integer.
159179 **
159180 ** The checksum is store in the first page of xShmMap memory as an 8-byte
159181 ** blob starting at byte offset 40.
159182 */
159183 static i64 rbuShmChecksum(sqlite3rbu *p){
159184 i64 iRet = 0;
159185 if( p->rc==SQLITE_OK ){
159186 sqlite3_file *pDb = p->pTargetFd->pReal;
159187 u32 volatile *ptr;
159188 p->rc = pDb->pMethods->xShmMap(pDb, 0, 32*1024, 0, (void volatile**)&ptr);
@@ -159189,23 +159195,23 @@
159195
159196 /*
159197 ** This function is called as part of initializing or reinitializing an
159198 ** incremental checkpoint.
159199 **
159200 ** It populates the sqlite3rbu.aFrame[] array with the set of
159201 ** (wal frame -> db page) copy operations required to checkpoint the
159202 ** current wal file, and obtains the set of shm locks required to safely
159203 ** perform the copy operations directly on the file-system.
159204 **
159205 ** If argument pState is not NULL, then the incremental checkpoint is
159206 ** being resumed. In this case, if the checksum of the wal-index-header
159207 ** following recovery is not the same as the checksum saved in the RbuState
159208 ** object, then the rbu handle is set to DONE state. This occurs if some
159209 ** other client appends a transaction to the wal file in the middle of
159210 ** an incremental checkpoint.
159211 */
159212 static void rbuSetupCheckpoint(sqlite3rbu *p, RbuState *pState){
159213
159214 /* If pState is NULL, then the wal file may not have been opened and
159215 ** recovered. Running a read-statement here to ensure that doing so
159216 ** does not interfere with the "capture" process below. */
159217 if( pState==0 ){
@@ -159214,20 +159220,20 @@
159220 p->rc = sqlite3_exec(p->dbMain, "SELECT * FROM sqlite_master", 0, 0, 0);
159221 }
159222 }
159223
159224 /* Assuming no error has occurred, run a "restart" checkpoint with the
159225 ** sqlite3rbu.eStage variable set to CAPTURE. This turns on the following
159226 ** special behaviour in the rbu VFS:
159227 **
159228 ** * If the exclusive shm WRITER or READ0 lock cannot be obtained,
159229 ** the checkpoint fails with SQLITE_BUSY (normally SQLite would
159230 ** proceed with running a passive checkpoint instead of failing).
159231 **
159232 ** * Attempts to read from the *-wal file or write to the database file
159233 ** do not perform any IO. Instead, the frame/page combinations that
159234 ** would be read/written are recorded in the sqlite3rbu.aFrame[]
159235 ** array.
159236 **
159237 ** * Calls to xShmLock(UNLOCK) to release the exclusive shm WRITER,
159238 ** READ0 and CHECKPOINT locks taken as part of the checkpoint are
159239 ** no-ops. These locks will not be released until the connection
@@ -159243,76 +159249,76 @@
159249 ** data from the wal file into the database file according to the
159250 ** contents of aFrame[].
159251 */
159252 if( p->rc==SQLITE_OK ){
159253 int rc2;
159254 p->eStage = RBU_STAGE_CAPTURE;
159255 rc2 = sqlite3_exec(p->dbMain, "PRAGMA main.wal_checkpoint=restart", 0, 0,0);
159256 if( rc2!=SQLITE_INTERNAL ) p->rc = rc2;
159257 }
159258
159259 if( p->rc==SQLITE_OK ){
159260 p->eStage = RBU_STAGE_CKPT;
159261 p->nStep = (pState ? pState->nRow : 0);
159262 p->aBuf = rbuMalloc(p, p->pgsz);
159263 p->iWalCksum = rbuShmChecksum(p);
159264 }
159265
159266 if( p->rc==SQLITE_OK && pState && pState->iWalCksum!=p->iWalCksum ){
159267 p->rc = SQLITE_DONE;
159268 p->eStage = RBU_STAGE_DONE;
159269 }
159270 }
159271
159272 /*
159273 ** Called when iAmt bytes are read from offset iOff of the wal file while
159274 ** the rbu object is in capture mode. Record the frame number of the frame
159275 ** being read in the aFrame[] array.
159276 */
159277 static int rbuCaptureWalRead(sqlite3rbu *pRbu, i64 iOff, int iAmt){
159278 const u32 mReq = (1<<WAL_LOCK_WRITE)|(1<<WAL_LOCK_CKPT)|(1<<WAL_LOCK_READ0);
159279 u32 iFrame;
159280
159281 if( pRbu->mLock!=mReq ){
159282 pRbu->rc = SQLITE_BUSY;
159283 return SQLITE_INTERNAL;
159284 }
159285
159286 pRbu->pgsz = iAmt;
159287 if( pRbu->nFrame==pRbu->nFrameAlloc ){
159288 int nNew = (pRbu->nFrameAlloc ? pRbu->nFrameAlloc : 64) * 2;
159289 RbuFrame *aNew;
159290 aNew = (RbuFrame*)sqlite3_realloc(pRbu->aFrame, nNew * sizeof(RbuFrame));
159291 if( aNew==0 ) return SQLITE_NOMEM;
159292 pRbu->aFrame = aNew;
159293 pRbu->nFrameAlloc = nNew;
159294 }
159295
159296 iFrame = (u32)((iOff-32) / (i64)(iAmt+24)) + 1;
159297 if( pRbu->iMaxFrame<iFrame ) pRbu->iMaxFrame = iFrame;
159298 pRbu->aFrame[pRbu->nFrame].iWalFrame = iFrame;
159299 pRbu->aFrame[pRbu->nFrame].iDbPage = 0;
159300 pRbu->nFrame++;
159301 return SQLITE_OK;
159302 }
159303
159304 /*
159305 ** Called when a page of data is written to offset iOff of the database
159306 ** file while the rbu handle is in capture mode. Record the page number
159307 ** of the page being written in the aFrame[] array.
159308 */
159309 static int rbuCaptureDbWrite(sqlite3rbu *pRbu, i64 iOff){
159310 pRbu->aFrame[pRbu->nFrame-1].iDbPage = (u32)(iOff / pRbu->pgsz) + 1;
159311 return SQLITE_OK;
159312 }
159313
159314 /*
159315 ** This is called as part of an incremental checkpoint operation. Copy
159316 ** a single frame of data from the wal file into the database file, as
159317 ** indicated by the RbuFrame object.
159318 */
159319 static void rbuCheckpointFrame(sqlite3rbu *p, RbuFrame *pFrame){
159320 sqlite3_file *pWal = p->pTargetFd->pWalFd->pReal;
159321 sqlite3_file *pDb = p->pTargetFd->pReal;
159322 i64 iOff;
159323
159324 assert( p->rc==SQLITE_OK );
@@ -159326,33 +159332,33 @@
159332
159333
159334 /*
159335 ** Take an EXCLUSIVE lock on the database file.
159336 */
159337 static void rbuLockDatabase(sqlite3rbu *p){
159338 sqlite3_file *pReal = p->pTargetFd->pReal;
159339 assert( p->rc==SQLITE_OK );
159340 p->rc = pReal->pMethods->xLock(pReal, SQLITE_LOCK_SHARED);
159341 if( p->rc==SQLITE_OK ){
159342 p->rc = pReal->pMethods->xLock(pReal, SQLITE_LOCK_EXCLUSIVE);
159343 }
159344 }
159345
159346 /*
159347 ** The RBU handle is currently in RBU_STAGE_OAL state, with a SHARED lock
159348 ** on the database file. This proc moves the *-oal file to the *-wal path,
159349 ** then reopens the database file (this time in vanilla, non-oal, WAL mode).
159350 ** If an error occurs, leave an error code and error message in the rbu
159351 ** handle.
159352 */
159353 static void rbuMoveOalFile(sqlite3rbu *p){
159354 const char *zBase = sqlite3_db_filename(p->dbMain, "main");
159355
159356 char *zWal = sqlite3_mprintf("%s-wal", zBase);
159357 char *zOal = sqlite3_mprintf("%s-oal", zBase);
159358
159359 assert( p->eStage==RBU_STAGE_MOVE );
159360 assert( p->rc==SQLITE_OK && p->zErrmsg==0 );
159361 if( zWal==0 || zOal==0 ){
159362 p->rc = SQLITE_NOMEM;
159363 }else{
159364 /* Move the *-oal file to *-wal. At this point connection p->db is
@@ -159360,25 +159366,25 @@
159366 ** in WAL mode). So no other connection may be writing the db.
159367 **
159368 ** In order to ensure that there are no database readers, an EXCLUSIVE
159369 ** lock is obtained here before the *-oal is moved to *-wal.
159370 */
159371 rbuLockDatabase(p);
159372 if( p->rc==SQLITE_OK ){
159373 rbuFileSuffix3(zBase, zWal);
159374 rbuFileSuffix3(zBase, zOal);
159375
159376 /* Re-open the databases. */
159377 rbuObjIterFinalize(&p->objiter);
159378 sqlite3_close(p->dbMain);
159379 sqlite3_close(p->dbRbu);
159380 p->rc = rename(zOal, zWal) ? SQLITE_IOERR : SQLITE_OK;
159381 if( p->rc==SQLITE_OK ){
159382 p->dbMain = 0;
159383 p->dbRbu = 0;
159384 rbuOpenDatabase(p);
159385 rbuSetupCheckpoint(p, 0);
159386 }
159387 }
159388 }
159389
159390 sqlite3_free(zWal);
@@ -159389,36 +159395,36 @@
159395 ** The SELECT statement iterating through the keys for the current object
159396 ** (p->objiter.pSelect) currently points to a valid row. This function
159397 ** determines the type of operation requested by this row and returns
159398 ** one of the following values to indicate the result:
159399 **
159400 ** * RBU_INSERT
159401 ** * RBU_DELETE
159402 ** * RBU_IDX_DELETE
159403 ** * RBU_UPDATE
159404 **
159405 ** If RBU_UPDATE is returned, then output variable *pzMask is set to
159406 ** point to the text value indicating the columns to update.
159407 **
159408 ** If the rbu_control field contains an invalid value, an error code and
159409 ** message are left in the RBU handle and zero returned.
159410 */
159411 static int rbuStepType(sqlite3rbu *p, const char **pzMask){
159412 int iCol = p->objiter.nCol; /* Index of rbu_control column */
159413 int res = 0; /* Return value */
159414
159415 switch( sqlite3_column_type(p->objiter.pSelect, iCol) ){
159416 case SQLITE_INTEGER: {
159417 int iVal = sqlite3_column_int(p->objiter.pSelect, iCol);
159418 if( iVal==0 ){
159419 res = RBU_INSERT;
159420 }else if( iVal==1 ){
159421 res = RBU_DELETE;
159422 }else if( iVal==2 ){
159423 res = RBU_IDX_DELETE;
159424 }else if( iVal==3 ){
159425 res = RBU_IDX_INSERT;
159426 }
159427 break;
159428 }
159429
159430 case SQLITE_TEXT: {
@@ -159426,21 +159432,21 @@
159432 if( z==0 ){
159433 p->rc = SQLITE_NOMEM;
159434 }else{
159435 *pzMask = (const char*)z;
159436 }
159437 res = RBU_UPDATE;
159438
159439 break;
159440 }
159441
159442 default:
159443 break;
159444 }
159445
159446 if( res==0 ){
159447 rbuBadControlError(p);
159448 }
159449 return res;
159450 }
159451
159452 #ifdef SQLITE_DEBUG
@@ -159454,82 +159460,82 @@
159460 #else
159461 # define assertColumnName(x,y,z)
159462 #endif
159463
159464 /*
159465 ** This function does the work for an sqlite3rbu_step() call.
159466 **
159467 ** The object-iterator (p->objiter) currently points to a valid object,
159468 ** and the input cursor (p->objiter.pSelect) currently points to a valid
159469 ** input row. Perform whatever processing is required and return.
159470 **
159471 ** If no error occurs, SQLITE_OK is returned. Otherwise, an error code
159472 ** and message is left in the RBU handle and a copy of the error code
159473 ** returned.
159474 */
159475 static int rbuStep(sqlite3rbu *p){
159476 RbuObjIter *pIter = &p->objiter;
159477 const char *zMask = 0;
159478 int i;
159479 int eType = rbuStepType(p, &zMask);
159480
159481 if( eType ){
159482 assert( eType!=RBU_UPDATE || pIter->zIdx==0 );
159483
159484 if( pIter->zIdx==0 && eType==RBU_IDX_DELETE ){
159485 rbuBadControlError(p);
159486 }
159487 else if(
159488 eType==RBU_INSERT
159489 || eType==RBU_DELETE
159490 || eType==RBU_IDX_DELETE
159491 || eType==RBU_IDX_INSERT
159492 ){
159493 sqlite3_value *pVal;
159494 sqlite3_stmt *pWriter;
159495
159496 assert( eType!=RBU_UPDATE );
159497 assert( eType!=RBU_DELETE || pIter->zIdx==0 );
159498
159499 if( eType==RBU_IDX_DELETE || eType==RBU_DELETE ){
159500 pWriter = pIter->pDelete;
159501 }else{
159502 pWriter = pIter->pInsert;
159503 }
159504
159505 for(i=0; i<pIter->nCol; i++){
159506 /* If this is an INSERT into a table b-tree and the table has an
159507 ** explicit INTEGER PRIMARY KEY, check that this is not an attempt
159508 ** to write a NULL into the IPK column. That is not permitted. */
159509 if( eType==RBU_INSERT
159510 && pIter->zIdx==0 && pIter->eType==RBU_PK_IPK && pIter->abTblPk[i]
159511 && sqlite3_column_type(pIter->pSelect, i)==SQLITE_NULL
159512 ){
159513 p->rc = SQLITE_MISMATCH;
159514 p->zErrmsg = sqlite3_mprintf("datatype mismatch");
159515 goto step_out;
159516 }
159517
159518 if( eType==RBU_DELETE && pIter->abTblPk[i]==0 ){
159519 continue;
159520 }
159521
159522 pVal = sqlite3_column_value(pIter->pSelect, i);
159523 p->rc = sqlite3_bind_value(pWriter, i+1, pVal);
159524 if( p->rc ) goto step_out;
159525 }
159526 if( pIter->zIdx==0
159527 && (pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE)
159528 ){
159529 /* For a virtual table, or a table with no primary key, the
159530 ** SELECT statement is:
159531 **
159532 ** SELECT <cols>, rbu_control, rbu_rowid FROM ....
159533 **
159534 ** Hence column_value(pIter->nCol+1).
159535 */
159536 assertColumnName(pIter->pSelect, pIter->nCol+1, "rbu_rowid");
159537 pVal = sqlite3_column_value(pIter->pSelect, pIter->nCol+1);
159538 p->rc = sqlite3_bind_value(pWriter, pIter->nCol+1, pVal);
159539 }
159540 if( p->rc==SQLITE_OK ){
159541 sqlite3_step(pWriter);
@@ -159536,25 +159542,25 @@
159542 p->rc = resetAndCollectError(pWriter, &p->zErrmsg);
159543 }
159544 }else{
159545 sqlite3_value *pVal;
159546 sqlite3_stmt *pUpdate = 0;
159547 assert( eType==RBU_UPDATE );
159548 rbuGetUpdateStmt(p, pIter, zMask, &pUpdate);
159549 if( pUpdate ){
159550 for(i=0; p->rc==SQLITE_OK && i<pIter->nCol; i++){
159551 char c = zMask[pIter->aiSrcOrder[i]];
159552 pVal = sqlite3_column_value(pIter->pSelect, i);
159553 if( pIter->abTblPk[i] || c=='x' || c=='d' ){
159554 p->rc = sqlite3_bind_value(pUpdate, i+1, pVal);
159555 }
159556 }
159557 if( p->rc==SQLITE_OK
159558 && (pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE)
159559 ){
159560 /* Bind the rbu_rowid value to column _rowid_ */
159561 assertColumnName(pIter->pSelect, pIter->nCol+1, "rbu_rowid");
159562 pVal = sqlite3_column_value(pIter->pSelect, pIter->nCol+1);
159563 p->rc = sqlite3_bind_value(pUpdate, pIter->nCol+1, pVal);
159564 }
159565 if( p->rc==SQLITE_OK ){
159566 sqlite3_step(pUpdate);
@@ -159569,11 +159575,11 @@
159575 }
159576
159577 /*
159578 ** Increment the schema cookie of the main database opened by p->dbMain.
159579 */
159580 static void rbuIncrSchemaCookie(sqlite3rbu *p){
159581 if( p->rc==SQLITE_OK ){
159582 int iCookie = 1000000;
159583 sqlite3_stmt *pStmt;
159584
159585 p->rc = prepareAndCollectError(p->dbMain, &pStmt, &p->zErrmsg,
@@ -159586,49 +159592,49 @@
159592 ** statement reads is page 1, which is guaranteed to be in the cache.
159593 ** And no memory allocations are required. */
159594 if( SQLITE_ROW==sqlite3_step(pStmt) ){
159595 iCookie = sqlite3_column_int(pStmt, 0);
159596 }
159597 rbuFinalize(p, pStmt);
159598 }
159599 if( p->rc==SQLITE_OK ){
159600 rbuMPrintfExec(p, p->dbMain, "PRAGMA schema_version = %d", iCookie+1);
159601 }
159602 }
159603 }
159604
159605 /*
159606 ** Update the contents of the rbu_state table within the rbu database. The
159607 ** value stored in the RBU_STATE_STAGE column is eStage. All other values
159608 ** are determined by inspecting the rbu handle passed as the first argument.
159609 */
159610 static void rbuSaveState(sqlite3rbu *p, int eStage){
159611 if( p->rc==SQLITE_OK || p->rc==SQLITE_DONE ){
159612 sqlite3_stmt *pInsert = 0;
159613 int rc;
159614
159615 assert( p->zErrmsg==0 );
159616 rc = prepareFreeAndCollectError(p->dbRbu, &pInsert, &p->zErrmsg,
159617 sqlite3_mprintf(
159618 "INSERT OR REPLACE INTO %s.rbu_state(k, v) VALUES "
159619 "(%d, %d), "
159620 "(%d, %Q), "
159621 "(%d, %Q), "
159622 "(%d, %d), "
159623 "(%d, %d), "
159624 "(%d, %lld), "
159625 "(%d, %lld), "
159626 "(%d, %lld) ",
159627 p->zStateDb,
159628 RBU_STATE_STAGE, eStage,
159629 RBU_STATE_TBL, p->objiter.zTbl,
159630 RBU_STATE_IDX, p->objiter.zIdx,
159631 RBU_STATE_ROW, p->nStep,
159632 RBU_STATE_PROGRESS, p->nProgress,
159633 RBU_STATE_CKPT, p->iWalCksum,
159634 RBU_STATE_COOKIE, (i64)p->pTargetFd->iCookie,
159635 RBU_STATE_OALSZ, p->iOalSz
159636 )
159637 );
159638 assert( pInsert==0 || rc==SQLITE_OK );
159639
159640 if( rc==SQLITE_OK ){
@@ -159639,71 +159645,71 @@
159645 }
159646 }
159647
159648
159649 /*
159650 ** Step the RBU object.
159651 */
159652 SQLITE_API int SQLITE_STDCALL sqlite3rbu_step(sqlite3rbu *p){
159653 if( p ){
159654 switch( p->eStage ){
159655 case RBU_STAGE_OAL: {
159656 RbuObjIter *pIter = &p->objiter;
159657 while( p->rc==SQLITE_OK && pIter->zTbl ){
159658
159659 if( pIter->bCleanup ){
159660 /* Clean up the rbu_tmp_xxx table for the previous table. It
159661 ** cannot be dropped as there are currently active SQL statements.
159662 ** But the contents can be deleted. */
159663 if( pIter->abIndexed ){
159664 rbuMPrintfExec(p, p->dbRbu,
159665 "DELETE FROM %s.'rbu_tmp_%q'", p->zStateDb, pIter->zTbl
159666 );
159667 }
159668 }else{
159669 rbuObjIterPrepareAll(p, pIter, 0);
159670
159671 /* Advance to the next row to process. */
159672 if( p->rc==SQLITE_OK ){
159673 int rc = sqlite3_step(pIter->pSelect);
159674 if( rc==SQLITE_ROW ){
159675 p->nProgress++;
159676 p->nStep++;
159677 return rbuStep(p);
159678 }
159679 p->rc = sqlite3_reset(pIter->pSelect);
159680 p->nStep = 0;
159681 }
159682 }
159683
159684 rbuObjIterNext(p, pIter);
159685 }
159686
159687 if( p->rc==SQLITE_OK ){
159688 assert( pIter->zTbl==0 );
159689 rbuSaveState(p, RBU_STAGE_MOVE);
159690 rbuIncrSchemaCookie(p);
159691 if( p->rc==SQLITE_OK ){
159692 p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
159693 }
159694 if( p->rc==SQLITE_OK ){
159695 p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, &p->zErrmsg);
159696 }
159697 p->eStage = RBU_STAGE_MOVE;
159698 }
159699 break;
159700 }
159701
159702 case RBU_STAGE_MOVE: {
159703 if( p->rc==SQLITE_OK ){
159704 rbuMoveOalFile(p);
159705 p->nProgress++;
159706 }
159707 break;
159708 }
159709
159710 case RBU_STAGE_CKPT: {
159711 if( p->rc==SQLITE_OK ){
159712 if( p->nStep>=p->nFrame ){
159713 sqlite3_file *pDb = p->pTargetFd->pReal;
159714
159715 /* Sync the db file */
@@ -159717,16 +159723,16 @@
159723 ((u32 volatile*)ptr)[24] = p->iMaxFrame;
159724 }
159725 }
159726
159727 if( p->rc==SQLITE_OK ){
159728 p->eStage = RBU_STAGE_DONE;
159729 p->rc = SQLITE_DONE;
159730 }
159731 }else{
159732 RbuFrame *pFrame = &p->aFrame[p->nStep];
159733 rbuCheckpointFrame(p, pFrame);
159734 p->nStep++;
159735 }
159736 p->nProgress++;
159737 }
159738 break;
@@ -159740,78 +159746,78 @@
159746 return SQLITE_NOMEM;
159747 }
159748 }
159749
159750 /*
159751 ** Free an RbuState object allocated by rbuLoadState().
159752 */
159753 static void rbuFreeState(RbuState *p){
159754 if( p ){
159755 sqlite3_free(p->zTbl);
159756 sqlite3_free(p->zIdx);
159757 sqlite3_free(p);
159758 }
159759 }
159760
159761 /*
159762 ** Allocate an RbuState object and load the contents of the rbu_state
159763 ** table into it. Return a pointer to the new object. It is the
159764 ** responsibility of the caller to eventually free the object using
159765 ** sqlite3_free().
159766 **
159767 ** If an error occurs, leave an error code and message in the rbu handle
159768 ** and return NULL.
159769 */
159770 static RbuState *rbuLoadState(sqlite3rbu *p){
159771 RbuState *pRet = 0;
159772 sqlite3_stmt *pStmt = 0;
159773 int rc;
159774 int rc2;
159775
159776 pRet = (RbuState*)rbuMalloc(p, sizeof(RbuState));
159777 if( pRet==0 ) return 0;
159778
159779 rc = prepareFreeAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
159780 sqlite3_mprintf("SELECT k, v FROM %s.rbu_state", p->zStateDb)
159781 );
159782 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
159783 switch( sqlite3_column_int(pStmt, 0) ){
159784 case RBU_STATE_STAGE:
159785 pRet->eStage = sqlite3_column_int(pStmt, 1);
159786 if( pRet->eStage!=RBU_STAGE_OAL
159787 && pRet->eStage!=RBU_STAGE_MOVE
159788 && pRet->eStage!=RBU_STAGE_CKPT
159789 ){
159790 p->rc = SQLITE_CORRUPT;
159791 }
159792 break;
159793
159794 case RBU_STATE_TBL:
159795 pRet->zTbl = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
159796 break;
159797
159798 case RBU_STATE_IDX:
159799 pRet->zIdx = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
159800 break;
159801
159802 case RBU_STATE_ROW:
159803 pRet->nRow = sqlite3_column_int(pStmt, 1);
159804 break;
159805
159806 case RBU_STATE_PROGRESS:
159807 pRet->nProgress = sqlite3_column_int64(pStmt, 1);
159808 break;
159809
159810 case RBU_STATE_CKPT:
159811 pRet->iWalCksum = sqlite3_column_int64(pStmt, 1);
159812 break;
159813
159814 case RBU_STATE_COOKIE:
159815 pRet->iCookie = (u32)sqlite3_column_int64(pStmt, 1);
159816 break;
159817
159818 case RBU_STATE_OALSZ:
159819 pRet->iOalSz = (u32)sqlite3_column_int64(pStmt, 1);
159820 break;
159821
159822 default:
159823 rc = SQLITE_CORRUPT;
@@ -159828,142 +159834,142 @@
159834 /*
159835 ** Compare strings z1 and z2, returning 0 if they are identical, or non-zero
159836 ** otherwise. Either or both argument may be NULL. Two NULL values are
159837 ** considered equal, and NULL is considered distinct from all other values.
159838 */
159839 static int rbuStrCompare(const char *z1, const char *z2){
159840 if( z1==0 && z2==0 ) return 0;
159841 if( z1==0 || z2==0 ) return 1;
159842 return (sqlite3_stricmp(z1, z2)!=0);
159843 }
159844
159845 /*
159846 ** This function is called as part of sqlite3rbu_open() when initializing
159847 ** an rbu handle in OAL stage. If the rbu update has not started (i.e.
159848 ** the rbu_state table was empty) it is a no-op. Otherwise, it arranges
159849 ** things so that the next call to sqlite3rbu_step() continues on from
159850 ** where the previous rbu handle left off.
159851 **
159852 ** If an error occurs, an error code and error message are left in the
159853 ** rbu handle passed as the first argument.
159854 */
159855 static void rbuSetupOal(sqlite3rbu *p, RbuState *pState){
159856 assert( p->rc==SQLITE_OK );
159857 if( pState->zTbl ){
159858 RbuObjIter *pIter = &p->objiter;
159859 int rc = SQLITE_OK;
159860
159861 while( rc==SQLITE_OK && pIter->zTbl && (pIter->bCleanup
159862 || rbuStrCompare(pIter->zIdx, pState->zIdx)
159863 || rbuStrCompare(pIter->zTbl, pState->zTbl)
159864 )){
159865 rc = rbuObjIterNext(p, pIter);
159866 }
159867
159868 if( rc==SQLITE_OK && !pIter->zTbl ){
159869 rc = SQLITE_ERROR;
159870 p->zErrmsg = sqlite3_mprintf("rbu_state mismatch error");
159871 }
159872
159873 if( rc==SQLITE_OK ){
159874 p->nStep = pState->nRow;
159875 rc = rbuObjIterPrepareAll(p, &p->objiter, p->nStep);
159876 }
159877
159878 p->rc = rc;
159879 }
159880 }
159881
159882 /*
159883 ** If there is a "*-oal" file in the file-system corresponding to the
159884 ** target database in the file-system, delete it. If an error occurs,
159885 ** leave an error code and error message in the rbu handle.
159886 */
159887 static void rbuDeleteOalFile(sqlite3rbu *p){
159888 char *zOal = sqlite3_mprintf("%s-oal", p->zTarget);
159889 assert( p->rc==SQLITE_OK && p->zErrmsg==0 );
159890 unlink(zOal);
159891 sqlite3_free(zOal);
159892 }
159893
159894 /*
159895 ** Allocate a private rbu VFS for the rbu handle passed as the only
159896 ** argument. This VFS will be used unless the call to sqlite3rbu_open()
159897 ** specified a URI with a vfs=? option in place of a target database
159898 ** file name.
159899 */
159900 static void rbuCreateVfs(sqlite3rbu *p){
159901 int rnd;
159902 char zRnd[64];
159903
159904 assert( p->rc==SQLITE_OK );
159905 sqlite3_randomness(sizeof(int), (void*)&rnd);
159906 sprintf(zRnd, "rbu_vfs_%d", rnd);
159907 p->rc = sqlite3rbu_create_vfs(zRnd, 0);
159908 if( p->rc==SQLITE_OK ){
159909 sqlite3_vfs *pVfs = sqlite3_vfs_find(zRnd);
159910 assert( pVfs );
159911 p->zVfsName = pVfs->zName;
159912 }
159913 }
159914
159915 /*
159916 ** Destroy the private VFS created for the rbu handle passed as the only
159917 ** argument by an earlier call to rbuCreateVfs().
159918 */
159919 static void rbuDeleteVfs(sqlite3rbu *p){
159920 if( p->zVfsName ){
159921 sqlite3rbu_destroy_vfs(p->zVfsName);
159922 p->zVfsName = 0;
159923 }
159924 }
159925
159926 /*
159927 ** Open and return a new RBU handle.
159928 */
159929 SQLITE_API sqlite3rbu *SQLITE_STDCALL sqlite3rbu_open(
159930 const char *zTarget,
159931 const char *zRbu,
159932 const char *zState
159933 ){
159934 sqlite3rbu *p;
159935 int nTarget = strlen(zTarget);
159936 int nRbu = strlen(zRbu);
159937 int nState = zState ? strlen(zState) : 0;
159938
159939 p = (sqlite3rbu*)sqlite3_malloc(sizeof(sqlite3rbu)+nTarget+1+nRbu+1+nState+1);
159940 if( p ){
159941 RbuState *pState = 0;
159942
159943 /* Create the custom VFS. */
159944 memset(p, 0, sizeof(sqlite3rbu));
159945 rbuCreateVfs(p);
159946
159947 /* Open the target database */
159948 if( p->rc==SQLITE_OK ){
159949 p->zTarget = (char*)&p[1];
159950 memcpy(p->zTarget, zTarget, nTarget+1);
159951 p->zRbu = &p->zTarget[nTarget+1];
159952 memcpy(p->zRbu, zRbu, nRbu+1);
159953 if( zState ){
159954 p->zState = &p->zRbu[nRbu+1];
159955 memcpy(p->zState, zState, nState+1);
159956 }
159957 rbuOpenDatabase(p);
159958 }
159959
159960 /* If it has not already been created, create the rbu_state table */
159961 rbuMPrintfExec(p, p->dbRbu, RBU_CREATE_STATE, p->zStateDb);
159962
159963 if( p->rc==SQLITE_OK ){
159964 pState = rbuLoadState(p);
159965 assert( pState || p->rc!=SQLITE_OK );
159966 if( p->rc==SQLITE_OK ){
159967
159968 if( pState->eStage==0 ){
159969 rbuDeleteOalFile(p);
159970 p->eStage = RBU_STAGE_OAL;
159971 }else{
159972 p->eStage = pState->eStage;
159973 }
159974 p->nProgress = pState->nProgress;
159975 p->iOalSz = pState->iOalSz;
@@ -159970,97 +159976,97 @@
159976 }
159977 }
159978 assert( p->rc!=SQLITE_OK || p->eStage!=0 );
159979
159980 if( p->rc==SQLITE_OK && p->pTargetFd->pWalFd ){
159981 if( p->eStage==RBU_STAGE_OAL ){
159982 p->rc = SQLITE_ERROR;
159983 p->zErrmsg = sqlite3_mprintf("cannot update wal mode database");
159984 }else if( p->eStage==RBU_STAGE_MOVE ){
159985 p->eStage = RBU_STAGE_CKPT;
159986 p->nStep = 0;
159987 }
159988 }
159989
159990 if( p->rc==SQLITE_OK
159991 && (p->eStage==RBU_STAGE_OAL || p->eStage==RBU_STAGE_MOVE)
159992 && pState->eStage!=0 && p->pTargetFd->iCookie!=pState->iCookie
159993 ){
159994 /* At this point (pTargetFd->iCookie) contains the value of the
159995 ** change-counter cookie (the thing that gets incremented when a
159996 ** transaction is committed in rollback mode) currently stored on
159997 ** page 1 of the database file. */
159998 p->rc = SQLITE_BUSY;
159999 p->zErrmsg = sqlite3_mprintf("database modified during rbu update");
160000 }
160001
160002 if( p->rc==SQLITE_OK ){
160003 if( p->eStage==RBU_STAGE_OAL ){
160004
160005 /* Open transactions both databases. The *-oal file is opened or
160006 ** created at this point. */
160007 p->rc = sqlite3_exec(p->dbMain, "BEGIN IMMEDIATE", 0, 0, &p->zErrmsg);
160008 if( p->rc==SQLITE_OK ){
160009 p->rc = sqlite3_exec(p->dbRbu, "BEGIN IMMEDIATE", 0, 0, &p->zErrmsg);
160010 }
160011
160012 /* Point the object iterator at the first object */
160013 if( p->rc==SQLITE_OK ){
160014 p->rc = rbuObjIterFirst(p, &p->objiter);
160015 }
160016
160017 /* If the RBU database contains no data_xxx tables, declare the RBU
160018 ** update finished. */
160019 if( p->rc==SQLITE_OK && p->objiter.zTbl==0 ){
160020 p->rc = SQLITE_DONE;
160021 }
160022
160023 if( p->rc==SQLITE_OK ){
160024 rbuSetupOal(p, pState);
160025 }
160026
160027 }else if( p->eStage==RBU_STAGE_MOVE ){
160028 /* no-op */
160029 }else if( p->eStage==RBU_STAGE_CKPT ){
160030 rbuSetupCheckpoint(p, pState);
160031 }else if( p->eStage==RBU_STAGE_DONE ){
160032 p->rc = SQLITE_DONE;
160033 }else{
160034 p->rc = SQLITE_CORRUPT;
160035 }
160036 }
160037
160038 rbuFreeState(pState);
160039 }
160040
160041 return p;
160042 }
160043
160044
160045 /*
160046 ** Return the database handle used by pRbu.
160047 */
160048 SQLITE_API sqlite3 *SQLITE_STDCALL sqlite3rbu_db(sqlite3rbu *pRbu, int bRbu){
160049 sqlite3 *db = 0;
160050 if( pRbu ){
160051 db = (bRbu ? pRbu->dbRbu : pRbu->dbMain);
160052 }
160053 return db;
160054 }
160055
160056
160057 /*
160058 ** If the error code currently stored in the RBU handle is SQLITE_CONSTRAINT,
160059 ** then edit any error message string so as to remove all occurrences of
160060 ** the pattern "rbu_imp_[0-9]*".
160061 */
160062 static void rbuEditErrmsg(sqlite3rbu *p){
160063 if( p->rc==SQLITE_CONSTRAINT && p->zErrmsg ){
160064 int i;
160065 int nErrmsg = strlen(p->zErrmsg);
160066 for(i=0; i<(nErrmsg-8); i++){
160067 if( memcmp(&p->zErrmsg[i], "rbu_imp_", 8)==0 ){
160068 int nDel = 8;
160069 while( p->zErrmsg[i+nDel]>='0' && p->zErrmsg[i+nDel]<='9' ) nDel++;
160070 memmove(&p->zErrmsg[i], &p->zErrmsg[i+nDel], nErrmsg + 1 - i - nDel);
160071 nErrmsg -= nDel;
160072 }
@@ -160067,38 +160073,38 @@
160073 }
160074 }
160075 }
160076
160077 /*
160078 ** Close the RBU handle.
160079 */
160080 SQLITE_API int SQLITE_STDCALL sqlite3rbu_close(sqlite3rbu *p, char **pzErrmsg){
160081 int rc;
160082 if( p ){
160083
160084 /* Commit the transaction to the *-oal file. */
160085 if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_OAL ){
160086 p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
160087 }
160088
160089 rbuSaveState(p, p->eStage);
160090
160091 if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_OAL ){
160092 p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, &p->zErrmsg);
160093 }
160094
160095 /* Close any open statement handles. */
160096 rbuObjIterFinalize(&p->objiter);
160097
160098 /* Close the open database handle and VFS object. */
160099 sqlite3_close(p->dbMain);
160100 sqlite3_close(p->dbRbu);
160101 rbuDeleteVfs(p);
160102 sqlite3_free(p->aBuf);
160103 sqlite3_free(p->aFrame);
160104
160105 rbuEditErrmsg(p);
160106 rc = p->rc;
160107 *pzErrmsg = p->zErrmsg;
160108 sqlite3_free(p);
160109 }else{
160110 rc = SQLITE_NOMEM;
@@ -160108,65 +160114,65 @@
160114 }
160115
160116 /*
160117 ** Return the total number of key-value operations (inserts, deletes or
160118 ** updates) that have been performed on the target database since the
160119 ** current RBU update was started.
160120 */
160121 SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3rbu_progress(sqlite3rbu *pRbu){
160122 return pRbu->nProgress;
160123 }
160124
160125 /**************************************************************************
160126 ** Beginning of RBU VFS shim methods. The VFS shim modifies the behaviour
160127 ** of a standard VFS in the following ways:
160128 **
160129 ** 1. Whenever the first page of a main database file is read or
160130 ** written, the value of the change-counter cookie is stored in
160131 ** rbu_file.iCookie. Similarly, the value of the "write-version"
160132 ** database header field is stored in rbu_file.iWriteVer. This ensures
160133 ** that the values are always trustworthy within an open transaction.
160134 **
160135 ** 2. Whenever an SQLITE_OPEN_WAL file is opened, the (rbu_file.pWalFd)
160136 ** member variable of the associated database file descriptor is set
160137 ** to point to the new file. A mutex protected linked list of all main
160138 ** db fds opened using a particular RBU VFS is maintained at
160139 ** rbu_vfs.pMain to facilitate this.
160140 **
160141 ** 3. Using a new file-control "SQLITE_FCNTL_RBU", a main db rbu_file
160142 ** object can be marked as the target database of an RBU update. This
160143 ** turns on the following extra special behaviour:
160144 **
160145 ** 3a. If xAccess() is called to check if there exists a *-wal file
160146 ** associated with an RBU target database currently in RBU_STAGE_OAL
160147 ** stage (preparing the *-oal file), the following special handling
160148 ** applies:
160149 **
160150 ** * if the *-wal file does exist, return SQLITE_CANTOPEN. An RBU
160151 ** target database may not be in wal mode already.
160152 **
160153 ** * if the *-wal file does not exist, set the output parameter to
160154 ** non-zero (to tell SQLite that it does exist) anyway.
160155 **
160156 ** Then, when xOpen() is called to open the *-wal file associated with
160157 ** the RBU target in RBU_STAGE_OAL stage, instead of opening the *-wal
160158 ** file, the rbu vfs opens the corresponding *-oal file instead.
160159 **
160160 ** 3b. The *-shm pages returned by xShmMap() for a target db file in
160161 ** RBU_STAGE_OAL mode are actually stored in heap memory. This is to
160162 ** avoid creating a *-shm file on disk. Additionally, xShmLock() calls
160163 ** are no-ops on target database files in RBU_STAGE_OAL mode. This is
160164 ** because assert() statements in some VFS implementations fail if
160165 ** xShmLock() is called before xShmMap().
160166 **
160167 ** 3c. If an EXCLUSIVE lock is attempted on a target database file in any
160168 ** mode except RBU_STAGE_DONE (all work completed and checkpointed), it
160169 ** fails with an SQLITE_BUSY error. This is to stop RBU connections
160170 ** from automatically checkpointing a *-wal (or *-oal) file from within
160171 ** sqlite3_close().
160172 **
160173 ** 3d. In RBU_STAGE_CAPTURE mode, all xRead() calls on the wal file, and
160174 ** all xWrite() calls on the target database file perform no IO.
160175 ** Instead the frame and page numbers that would be read and written
160176 ** are recorded. Additionally, successful attempts to obtain exclusive
160177 ** xShmLock() WRITER, CHECKPOINTER and READ0 locks on the target
160178 ** database file are recorded. xShmLock() calls to unlock the same
@@ -160173,28 +160179,28 @@
160179 ** locks are no-ops (so that once obtained, these locks are never
160180 ** relinquished). Finally, calls to xSync() on the target database
160181 ** file fail with SQLITE_INTERNAL errors.
160182 */
160183
160184 static void rbuUnlockShm(rbu_file *p){
160185 if( p->pRbu ){
160186 int (*xShmLock)(sqlite3_file*,int,int,int) = p->pReal->pMethods->xShmLock;
160187 int i;
160188 for(i=0; i<SQLITE_SHM_NLOCK;i++){
160189 if( (1<<i) & p->pRbu->mLock ){
160190 xShmLock(p->pReal, i, 1, SQLITE_SHM_UNLOCK|SQLITE_SHM_EXCLUSIVE);
160191 }
160192 }
160193 p->pRbu->mLock = 0;
160194 }
160195 }
160196
160197 /*
160198 ** Close an rbu file.
160199 */
160200 static int rbuVfsClose(sqlite3_file *pFile){
160201 rbu_file *p = (rbu_file*)pFile;
160202 int rc;
160203 int i;
160204
160205 /* Free the contents of the apShm[] array. And the array itself. */
160206 for(i=0; i<p->nShm; i++){
@@ -160203,16 +160209,16 @@
160209 sqlite3_free(p->apShm);
160210 p->apShm = 0;
160211 sqlite3_free(p->zDel);
160212
160213 if( p->openFlags & SQLITE_OPEN_MAIN_DB ){
160214 rbu_file **pp;
160215 sqlite3_mutex_enter(p->pRbuVfs->mutex);
160216 for(pp=&p->pRbuVfs->pMain; *pp!=p; pp=&((*pp)->pMainNext));
160217 *pp = p->pMainNext;
160218 sqlite3_mutex_leave(p->pRbuVfs->mutex);
160219 rbuUnlockShm(p);
160220 p->pReal->pMethods->xShmUnmap(p->pReal, 0);
160221 }
160222
160223 /* Close the underlying file handle */
160224 rc = p->pReal->pMethods->xClose(p->pReal);
@@ -160222,37 +160228,37 @@
160228
160229 /*
160230 ** Read and return an unsigned 32-bit big-endian integer from the buffer
160231 ** passed as the only argument.
160232 */
160233 static u32 rbuGetU32(u8 *aBuf){
160234 return ((u32)aBuf[0] << 24)
160235 + ((u32)aBuf[1] << 16)
160236 + ((u32)aBuf[2] << 8)
160237 + ((u32)aBuf[3]);
160238 }
160239
160240 /*
160241 ** Read data from an rbuVfs-file.
160242 */
160243 static int rbuVfsRead(
160244 sqlite3_file *pFile,
160245 void *zBuf,
160246 int iAmt,
160247 sqlite_int64 iOfst
160248 ){
160249 rbu_file *p = (rbu_file*)pFile;
160250 sqlite3rbu *pRbu = p->pRbu;
160251 int rc;
160252
160253 if( pRbu && pRbu->eStage==RBU_STAGE_CAPTURE ){
160254 assert( p->openFlags & SQLITE_OPEN_WAL );
160255 rc = rbuCaptureWalRead(p->pRbu, iOfst, iAmt);
160256 }else{
160257 if( pRbu && pRbu->eStage==RBU_STAGE_OAL
160258 && (p->openFlags & SQLITE_OPEN_WAL)
160259 && iOfst>=pRbu->iOalSz
160260 ){
160261 rc = SQLITE_OK;
160262 memset(zBuf, 0, iAmt);
160263 }else{
160264 rc = p->pReal->pMethods->xRead(p->pReal, zBuf, iAmt, iOfst);
@@ -160259,92 +160265,92 @@
160265 }
160266 if( rc==SQLITE_OK && iOfst==0 && (p->openFlags & SQLITE_OPEN_MAIN_DB) ){
160267 /* These look like magic numbers. But they are stable, as they are part
160268 ** of the definition of the SQLite file format, which may not change. */
160269 u8 *pBuf = (u8*)zBuf;
160270 p->iCookie = rbuGetU32(&pBuf[24]);
160271 p->iWriteVer = pBuf[19];
160272 }
160273 }
160274 return rc;
160275 }
160276
160277 /*
160278 ** Write data to an rbuVfs-file.
160279 */
160280 static int rbuVfsWrite(
160281 sqlite3_file *pFile,
160282 const void *zBuf,
160283 int iAmt,
160284 sqlite_int64 iOfst
160285 ){
160286 rbu_file *p = (rbu_file*)pFile;
160287 sqlite3rbu *pRbu = p->pRbu;
160288 int rc;
160289
160290 if( pRbu && pRbu->eStage==RBU_STAGE_CAPTURE ){
160291 assert( p->openFlags & SQLITE_OPEN_MAIN_DB );
160292 rc = rbuCaptureDbWrite(p->pRbu, iOfst);
160293 }else{
160294 if( pRbu && pRbu->eStage==RBU_STAGE_OAL
160295 && (p->openFlags & SQLITE_OPEN_WAL)
160296 && iOfst>=pRbu->iOalSz
160297 ){
160298 pRbu->iOalSz = iAmt + iOfst;
160299 }
160300 rc = p->pReal->pMethods->xWrite(p->pReal, zBuf, iAmt, iOfst);
160301 if( rc==SQLITE_OK && iOfst==0 && (p->openFlags & SQLITE_OPEN_MAIN_DB) ){
160302 /* These look like magic numbers. But they are stable, as they are part
160303 ** of the definition of the SQLite file format, which may not change. */
160304 u8 *pBuf = (u8*)zBuf;
160305 p->iCookie = rbuGetU32(&pBuf[24]);
160306 p->iWriteVer = pBuf[19];
160307 }
160308 }
160309 return rc;
160310 }
160311
160312 /*
160313 ** Truncate an rbuVfs-file.
160314 */
160315 static int rbuVfsTruncate(sqlite3_file *pFile, sqlite_int64 size){
160316 rbu_file *p = (rbu_file*)pFile;
160317 return p->pReal->pMethods->xTruncate(p->pReal, size);
160318 }
160319
160320 /*
160321 ** Sync an rbuVfs-file.
160322 */
160323 static int rbuVfsSync(sqlite3_file *pFile, int flags){
160324 rbu_file *p = (rbu_file *)pFile;
160325 if( p->pRbu && p->pRbu->eStage==RBU_STAGE_CAPTURE ){
160326 if( p->openFlags & SQLITE_OPEN_MAIN_DB ){
160327 return SQLITE_INTERNAL;
160328 }
160329 return SQLITE_OK;
160330 }
160331 return p->pReal->pMethods->xSync(p->pReal, flags);
160332 }
160333
160334 /*
160335 ** Return the current file-size of an rbuVfs-file.
160336 */
160337 static int rbuVfsFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
160338 rbu_file *p = (rbu_file *)pFile;
160339 return p->pReal->pMethods->xFileSize(p->pReal, pSize);
160340 }
160341
160342 /*
160343 ** Lock an rbuVfs-file.
160344 */
160345 static int rbuVfsLock(sqlite3_file *pFile, int eLock){
160346 rbu_file *p = (rbu_file*)pFile;
160347 sqlite3rbu *pRbu = p->pRbu;
160348 int rc = SQLITE_OK;
160349
160350 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
160351 if( pRbu && eLock==SQLITE_LOCK_EXCLUSIVE && pRbu->eStage!=RBU_STAGE_DONE ){
160352 /* Do not allow EXCLUSIVE locks. Preventing SQLite from taking this
160353 ** prevents it from checkpointing the database from sqlite3_close(). */
160354 rc = SQLITE_BUSY;
160355 }else{
160356 rc = p->pReal->pMethods->xLock(p->pReal, eLock);
@@ -160352,122 +160358,122 @@
160358
160359 return rc;
160360 }
160361
160362 /*
160363 ** Unlock an rbuVfs-file.
160364 */
160365 static int rbuVfsUnlock(sqlite3_file *pFile, int eLock){
160366 rbu_file *p = (rbu_file *)pFile;
160367 return p->pReal->pMethods->xUnlock(p->pReal, eLock);
160368 }
160369
160370 /*
160371 ** Check if another file-handle holds a RESERVED lock on an rbuVfs-file.
160372 */
160373 static int rbuVfsCheckReservedLock(sqlite3_file *pFile, int *pResOut){
160374 rbu_file *p = (rbu_file *)pFile;
160375 return p->pReal->pMethods->xCheckReservedLock(p->pReal, pResOut);
160376 }
160377
160378 /*
160379 ** File control method. For custom operations on an rbuVfs-file.
160380 */
160381 static int rbuVfsFileControl(sqlite3_file *pFile, int op, void *pArg){
160382 rbu_file *p = (rbu_file *)pFile;
160383 int (*xControl)(sqlite3_file*,int,void*) = p->pReal->pMethods->xFileControl;
160384 int rc;
160385
160386 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB)
160387 || p->openFlags & (SQLITE_OPEN_TRANSIENT_DB|SQLITE_OPEN_TEMP_JOURNAL)
160388 );
160389 if( op==SQLITE_FCNTL_RBU ){
160390 sqlite3rbu *pRbu = (sqlite3rbu*)pArg;
160391
160392 /* First try to find another RBU vfs lower down in the vfs stack. If
160393 ** one is found, this vfs will operate in pass-through mode. The lower
160394 ** level vfs will do the special RBU handling. */
160395 rc = xControl(p->pReal, op, pArg);
160396
160397 if( rc==SQLITE_NOTFOUND ){
160398 /* Now search for a zipvfs instance lower down in the VFS stack. If
160399 ** one is found, this is an error. */
160400 void *dummy = 0;
160401 rc = xControl(p->pReal, SQLITE_FCNTL_ZIPVFS, &dummy);
160402 if( rc==SQLITE_OK ){
160403 rc = SQLITE_ERROR;
160404 pRbu->zErrmsg = sqlite3_mprintf("rbu/zipvfs setup error");
160405 }else if( rc==SQLITE_NOTFOUND ){
160406 pRbu->pTargetFd = p;
160407 p->pRbu = pRbu;
160408 if( p->pWalFd ) p->pWalFd->pRbu = pRbu;
160409 rc = SQLITE_OK;
160410 }
160411 }
160412 return rc;
160413 }
160414
160415 rc = xControl(p->pReal, op, pArg);
160416 if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){
160417 rbu_vfs *pRbuVfs = p->pRbuVfs;
160418 char *zIn = *(char**)pArg;
160419 char *zOut = sqlite3_mprintf("rbu(%s)/%z", pRbuVfs->base.zName, zIn);
160420 *(char**)pArg = zOut;
160421 if( zOut==0 ) rc = SQLITE_NOMEM;
160422 }
160423
160424 return rc;
160425 }
160426
160427 /*
160428 ** Return the sector-size in bytes for an rbuVfs-file.
160429 */
160430 static int rbuVfsSectorSize(sqlite3_file *pFile){
160431 rbu_file *p = (rbu_file *)pFile;
160432 return p->pReal->pMethods->xSectorSize(p->pReal);
160433 }
160434
160435 /*
160436 ** Return the device characteristic flags supported by an rbuVfs-file.
160437 */
160438 static int rbuVfsDeviceCharacteristics(sqlite3_file *pFile){
160439 rbu_file *p = (rbu_file *)pFile;
160440 return p->pReal->pMethods->xDeviceCharacteristics(p->pReal);
160441 }
160442
160443 /*
160444 ** Take or release a shared-memory lock.
160445 */
160446 static int rbuVfsShmLock(sqlite3_file *pFile, int ofst, int n, int flags){
160447 rbu_file *p = (rbu_file*)pFile;
160448 sqlite3rbu *pRbu = p->pRbu;
160449 int rc = SQLITE_OK;
160450
160451 #ifdef SQLITE_AMALGAMATION
160452 assert( WAL_CKPT_LOCK==1 );
160453 #endif
160454
160455 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
160456 if( pRbu && (pRbu->eStage==RBU_STAGE_OAL || pRbu->eStage==RBU_STAGE_MOVE) ){
160457 /* Magic number 1 is the WAL_CKPT_LOCK lock. Preventing SQLite from
160458 ** taking this lock also prevents any checkpoints from occurring.
160459 ** todo: really, it's not clear why this might occur, as
160460 ** wal_autocheckpoint ought to be turned off. */
160461 if( ofst==WAL_LOCK_CKPT && n==1 ) rc = SQLITE_BUSY;
160462 }else{
160463 int bCapture = 0;
160464 if( n==1 && (flags & SQLITE_SHM_EXCLUSIVE)
160465 && pRbu && pRbu->eStage==RBU_STAGE_CAPTURE
160466 && (ofst==WAL_LOCK_WRITE || ofst==WAL_LOCK_CKPT || ofst==WAL_LOCK_READ0)
160467 ){
160468 bCapture = 1;
160469 }
160470
160471 if( bCapture==0 || 0==(flags & SQLITE_SHM_UNLOCK) ){
160472 rc = p->pReal->pMethods->xShmLock(p->pReal, ofst, n, flags);
160473 if( bCapture && rc==SQLITE_OK ){
160474 pRbu->mLock |= (1 << ofst);
160475 }
160476 }
160477 }
160478
160479 return rc;
@@ -160474,26 +160480,26 @@
160480 }
160481
160482 /*
160483 ** Obtain a pointer to a mapping of a single 32KiB page of the *-shm file.
160484 */
160485 static int rbuVfsShmMap(
160486 sqlite3_file *pFile,
160487 int iRegion,
160488 int szRegion,
160489 int isWrite,
160490 void volatile **pp
160491 ){
160492 rbu_file *p = (rbu_file*)pFile;
160493 int rc = SQLITE_OK;
160494 int eStage = (p->pRbu ? p->pRbu->eStage : 0);
160495
160496 /* If not in RBU_STAGE_OAL, allow this call to pass through. Or, if this
160497 ** rbu is in the RBU_STAGE_OAL state, use heap memory for *-shm space
160498 ** instead of a file on disk. */
160499 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
160500 if( eStage==RBU_STAGE_OAL || eStage==RBU_STAGE_MOVE ){
160501 if( iRegion<=p->nShm ){
160502 int nByte = (iRegion+1) * sizeof(char*);
160503 char **apNew = (char**)sqlite3_realloc(p->apShm, nByte);
160504 if( apNew==0 ){
160505 rc = SQLITE_NOMEM;
@@ -160528,29 +160534,29 @@
160534 }
160535
160536 /*
160537 ** Memory barrier.
160538 */
160539 static void rbuVfsShmBarrier(sqlite3_file *pFile){
160540 rbu_file *p = (rbu_file *)pFile;
160541 p->pReal->pMethods->xShmBarrier(p->pReal);
160542 }
160543
160544 /*
160545 ** The xShmUnmap method.
160546 */
160547 static int rbuVfsShmUnmap(sqlite3_file *pFile, int delFlag){
160548 rbu_file *p = (rbu_file*)pFile;
160549 int rc = SQLITE_OK;
160550 int eStage = (p->pRbu ? p->pRbu->eStage : 0);
160551
160552 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
160553 if( eStage==RBU_STAGE_OAL || eStage==RBU_STAGE_MOVE ){
160554 /* no-op */
160555 }else{
160556 /* Release the checkpointer and writer locks */
160557 rbuUnlockShm(p);
160558 rc = p->pReal->pMethods->xShmUnmap(p->pReal, delFlag);
160559 }
160560 return rc;
160561 }
160562
@@ -160558,56 +160564,56 @@
160564 ** Given that zWal points to a buffer containing a wal file name passed to
160565 ** either the xOpen() or xAccess() VFS method, return a pointer to the
160566 ** file-handle opened by the same database connection on the corresponding
160567 ** database file.
160568 */
160569 static rbu_file *rbuFindMaindb(rbu_vfs *pRbuVfs, const char *zWal){
160570 rbu_file *pDb;
160571 sqlite3_mutex_enter(pRbuVfs->mutex);
160572 for(pDb=pRbuVfs->pMain; pDb && pDb->zWal!=zWal; pDb=pDb->pMainNext);
160573 sqlite3_mutex_leave(pRbuVfs->mutex);
160574 return pDb;
160575 }
160576
160577 /*
160578 ** Open an rbu file handle.
160579 */
160580 static int rbuVfsOpen(
160581 sqlite3_vfs *pVfs,
160582 const char *zName,
160583 sqlite3_file *pFile,
160584 int flags,
160585 int *pOutFlags
160586 ){
160587 static sqlite3_io_methods rbuvfs_io_methods = {
160588 2, /* iVersion */
160589 rbuVfsClose, /* xClose */
160590 rbuVfsRead, /* xRead */
160591 rbuVfsWrite, /* xWrite */
160592 rbuVfsTruncate, /* xTruncate */
160593 rbuVfsSync, /* xSync */
160594 rbuVfsFileSize, /* xFileSize */
160595 rbuVfsLock, /* xLock */
160596 rbuVfsUnlock, /* xUnlock */
160597 rbuVfsCheckReservedLock, /* xCheckReservedLock */
160598 rbuVfsFileControl, /* xFileControl */
160599 rbuVfsSectorSize, /* xSectorSize */
160600 rbuVfsDeviceCharacteristics, /* xDeviceCharacteristics */
160601 rbuVfsShmMap, /* xShmMap */
160602 rbuVfsShmLock, /* xShmLock */
160603 rbuVfsShmBarrier, /* xShmBarrier */
160604 rbuVfsShmUnmap /* xShmUnmap */
160605 };
160606 rbu_vfs *pRbuVfs = (rbu_vfs*)pVfs;
160607 sqlite3_vfs *pRealVfs = pRbuVfs->pRealVfs;
160608 rbu_file *pFd = (rbu_file *)pFile;
160609 int rc = SQLITE_OK;
160610 const char *zOpen = zName;
160611
160612 memset(pFd, 0, sizeof(rbu_file));
160613 pFd->pReal = (sqlite3_file*)&pFd[1];
160614 pFd->pRbuVfs = pRbuVfs;
160615 pFd->openFlags = flags;
160616 if( zName ){
160617 if( flags & SQLITE_OPEN_MAIN_DB ){
160618 /* A main database has just been opened. The following block sets
160619 ** (pFd->zWal) to point to a buffer owned by SQLite that contains
@@ -160631,13 +160637,13 @@
160637 }
160638 z += (n + 8 + 1);
160639 pFd->zWal = z;
160640 }
160641 else if( flags & SQLITE_OPEN_WAL ){
160642 rbu_file *pDb = rbuFindMaindb(pRbuVfs, zName);
160643 if( pDb ){
160644 if( pDb->pRbu && pDb->pRbu->eStage==RBU_STAGE_OAL ){
160645 /* This call is to open a *-wal file. Intead, open the *-oal. This
160646 ** code ensures that the string passed to xOpen() is terminated by a
160647 ** pair of '\0' bytes in case the VFS attempts to extract a URI
160648 ** parameter from it. */
160649 int nCopy = strlen(zName);
@@ -160649,11 +160655,11 @@
160655 zCopy[nCopy+1] = '\0';
160656 zOpen = (const char*)(pFd->zDel = zCopy);
160657 }else{
160658 rc = SQLITE_NOMEM;
160659 }
160660 pFd->pRbu = pDb->pRbu;
160661 }
160662 pDb->pWalFd = pFd;
160663 }
160664 }
160665 }
@@ -160663,16 +160669,16 @@
160669 }
160670 if( pFd->pReal->pMethods ){
160671 /* The xOpen() operation has succeeded. Set the sqlite3_file.pMethods
160672 ** pointer and, if the file is a main database file, link it into the
160673 ** mutex protected linked list of all such files. */
160674 pFile->pMethods = &rbuvfs_io_methods;
160675 if( flags & SQLITE_OPEN_MAIN_DB ){
160676 sqlite3_mutex_enter(pRbuVfs->mutex);
160677 pFd->pMainNext = pRbuVfs->pMain;
160678 pRbuVfs->pMain = pFd;
160679 sqlite3_mutex_leave(pRbuVfs->mutex);
160680 }
160681 }else{
160682 sqlite3_free(pFd->zDel);
160683 }
160684
@@ -160680,48 +160686,48 @@
160686 }
160687
160688 /*
160689 ** Delete the file located at zPath.
160690 */
160691 static int rbuVfsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
160692 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
160693 return pRealVfs->xDelete(pRealVfs, zPath, dirSync);
160694 }
160695
160696 /*
160697 ** Test for access permissions. Return true if the requested permission
160698 ** is available, or false otherwise.
160699 */
160700 static int rbuVfsAccess(
160701 sqlite3_vfs *pVfs,
160702 const char *zPath,
160703 int flags,
160704 int *pResOut
160705 ){
160706 rbu_vfs *pRbuVfs = (rbu_vfs*)pVfs;
160707 sqlite3_vfs *pRealVfs = pRbuVfs->pRealVfs;
160708 int rc;
160709
160710 rc = pRealVfs->xAccess(pRealVfs, zPath, flags, pResOut);
160711
160712 /* If this call is to check if a *-wal file associated with an RBU target
160713 ** database connection exists, and the RBU update is in RBU_STAGE_OAL,
160714 ** the following special handling is activated:
160715 **
160716 ** a) if the *-wal file does exist, return SQLITE_CANTOPEN. This
160717 ** ensures that the RBU extension never tries to update a database
160718 ** in wal mode, even if the first page of the database file has
160719 ** been damaged.
160720 **
160721 ** b) if the *-wal file does not exist, claim that it does anyway,
160722 ** causing SQLite to call xOpen() to open it. This call will also
160723 ** be intercepted (see the rbuVfsOpen() function) and the *-oal
160724 ** file opened instead.
160725 */
160726 if( rc==SQLITE_OK && flags==SQLITE_ACCESS_EXISTS ){
160727 rbu_file *pDb = rbuFindMaindb(pRbuVfs, zPath);
160728 if( pDb && pDb->pRbu && pDb->pRbu->eStage==RBU_STAGE_OAL ){
160729 if( *pResOut ){
160730 rc = SQLITE_CANTOPEN;
160731 }else{
160732 *pResOut = 1;
160733 }
@@ -160734,151 +160740,151 @@
160740 /*
160741 ** Populate buffer zOut with the full canonical pathname corresponding
160742 ** to the pathname in zPath. zOut is guaranteed to point to a buffer
160743 ** of at least (DEVSYM_MAX_PATHNAME+1) bytes.
160744 */
160745 static int rbuVfsFullPathname(
160746 sqlite3_vfs *pVfs,
160747 const char *zPath,
160748 int nOut,
160749 char *zOut
160750 ){
160751 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
160752 return pRealVfs->xFullPathname(pRealVfs, zPath, nOut, zOut);
160753 }
160754
160755 #ifndef SQLITE_OMIT_LOAD_EXTENSION
160756 /*
160757 ** Open the dynamic library located at zPath and return a handle.
160758 */
160759 static void *rbuVfsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
160760 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
160761 return pRealVfs->xDlOpen(pRealVfs, zPath);
160762 }
160763
160764 /*
160765 ** Populate the buffer zErrMsg (size nByte bytes) with a human readable
160766 ** utf-8 string describing the most recent error encountered associated
160767 ** with dynamic libraries.
160768 */
160769 static void rbuVfsDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
160770 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
160771 pRealVfs->xDlError(pRealVfs, nByte, zErrMsg);
160772 }
160773
160774 /*
160775 ** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
160776 */
160777 static void (*rbuVfsDlSym(
160778 sqlite3_vfs *pVfs,
160779 void *pArg,
160780 const char *zSym
160781 ))(void){
160782 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
160783 return pRealVfs->xDlSym(pRealVfs, pArg, zSym);
160784 }
160785
160786 /*
160787 ** Close the dynamic library handle pHandle.
160788 */
160789 static void rbuVfsDlClose(sqlite3_vfs *pVfs, void *pHandle){
160790 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
160791 return pRealVfs->xDlClose(pRealVfs, pHandle);
160792 }
160793 #endif /* SQLITE_OMIT_LOAD_EXTENSION */
160794
160795 /*
160796 ** Populate the buffer pointed to by zBufOut with nByte bytes of
160797 ** random data.
160798 */
160799 static int rbuVfsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
160800 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
160801 return pRealVfs->xRandomness(pRealVfs, nByte, zBufOut);
160802 }
160803
160804 /*
160805 ** Sleep for nMicro microseconds. Return the number of microseconds
160806 ** actually slept.
160807 */
160808 static int rbuVfsSleep(sqlite3_vfs *pVfs, int nMicro){
160809 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
160810 return pRealVfs->xSleep(pRealVfs, nMicro);
160811 }
160812
160813 /*
160814 ** Return the current time as a Julian Day number in *pTimeOut.
160815 */
160816 static int rbuVfsCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
160817 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
160818 return pRealVfs->xCurrentTime(pRealVfs, pTimeOut);
160819 }
160820
160821 /*
160822 ** No-op.
160823 */
160824 static int rbuVfsGetLastError(sqlite3_vfs *pVfs, int a, char *b){
160825 return 0;
160826 }
160827
160828 /*
160829 ** Deregister and destroy an RBU vfs created by an earlier call to
160830 ** sqlite3rbu_create_vfs().
160831 */
160832 SQLITE_API void SQLITE_STDCALL sqlite3rbu_destroy_vfs(const char *zName){
160833 sqlite3_vfs *pVfs = sqlite3_vfs_find(zName);
160834 if( pVfs && pVfs->xOpen==rbuVfsOpen ){
160835 sqlite3_mutex_free(((rbu_vfs*)pVfs)->mutex);
160836 sqlite3_vfs_unregister(pVfs);
160837 sqlite3_free(pVfs);
160838 }
160839 }
160840
160841 /*
160842 ** Create an RBU VFS named zName that accesses the underlying file-system
160843 ** via existing VFS zParent. The new object is registered as a non-default
160844 ** VFS with SQLite before returning.
160845 */
160846 SQLITE_API int SQLITE_STDCALL sqlite3rbu_create_vfs(const char *zName, const char *zParent){
160847
160848 /* Template for VFS */
160849 static sqlite3_vfs vfs_template = {
160850 1, /* iVersion */
160851 0, /* szOsFile */
160852 0, /* mxPathname */
160853 0, /* pNext */
160854 0, /* zName */
160855 0, /* pAppData */
160856 rbuVfsOpen, /* xOpen */
160857 rbuVfsDelete, /* xDelete */
160858 rbuVfsAccess, /* xAccess */
160859 rbuVfsFullPathname, /* xFullPathname */
160860
160861 #ifndef SQLITE_OMIT_LOAD_EXTENSION
160862 rbuVfsDlOpen, /* xDlOpen */
160863 rbuVfsDlError, /* xDlError */
160864 rbuVfsDlSym, /* xDlSym */
160865 rbuVfsDlClose, /* xDlClose */
160866 #else
160867 0, 0, 0, 0,
160868 #endif
160869
160870 rbuVfsRandomness, /* xRandomness */
160871 rbuVfsSleep, /* xSleep */
160872 rbuVfsCurrentTime, /* xCurrentTime */
160873 rbuVfsGetLastError, /* xGetLastError */
160874 0, /* xCurrentTimeInt64 (version 2) */
160875 0, 0, 0 /* Unimplemented version 3 methods */
160876 };
160877
160878 rbu_vfs *pNew = 0; /* Newly allocated VFS */
160879 int nName;
160880 int rc = SQLITE_OK;
160881
160882 int nByte;
160883 nName = strlen(zName);
160884 nByte = sizeof(rbu_vfs) + nName + 1;
160885 pNew = (rbu_vfs*)sqlite3_malloc(nByte);
160886 if( pNew==0 ){
160887 rc = SQLITE_NOMEM;
160888 }else{
160889 sqlite3_vfs *pParent; /* Parent VFS */
160890 memset(pNew, 0, nByte);
@@ -160887,11 +160893,11 @@
160893 rc = SQLITE_NOTFOUND;
160894 }else{
160895 char *zSpace;
160896 memcpy(&pNew->base, &vfs_template, sizeof(sqlite3_vfs));
160897 pNew->base.mxPathname = pParent->mxPathname;
160898 pNew->base.szOsFile = sizeof(rbu_file) + pParent->szOsFile;
160899 pNew->pRealVfs = pParent;
160900 pNew->base.zName = (const char*)(zSpace = (char*)&pNew[1]);
160901 memcpy(zSpace, zName, nName);
160902
160903 /* Allocate the mutex and register the new VFS (not as the default) */
@@ -160913,13 +160919,13 @@
160919 }
160920
160921
160922 /**************************************************************************/
160923
160924 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RBU) */
160925
160926 /************** End of sqlite3rbu.c ******************************************/
160927 /************** Begin file dbstat.c ******************************************/
160928 /*
160929 ** 2010 July 12
160930 **
160931 ** The author disclaims copyright to this source code. In place of
160932
+5 -5
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -111,11 +111,11 @@
111111
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
112112
** [sqlite_version()] and [sqlite_source_id()].
113113
*/
114114
#define SQLITE_VERSION "3.8.11"
115115
#define SQLITE_VERSION_NUMBER 3008011
116
-#define SQLITE_SOURCE_ID "2015-07-16 18:18:19 b79a4affe44bd0c8e155cae19f3f62c715684cd6"
116
+#define SQLITE_SOURCE_ID "2015-07-23 20:44:49 017c5019e1ce042025d4f327e50ec50af49f9fa4"
117117
118118
/*
119119
** CAPI3REF: Run-Time Library Version Numbers
120120
** KEYWORDS: sqlite3_version, sqlite3_sourceid
121121
**
@@ -965,13 +965,13 @@
965965
**
966966
** <li>[[SQLITE_FCNTL_ZIPVFS]]
967967
** The [SQLITE_FCNTL_ZIPVFS] opcode is implemented by zipvfs only. All other
968968
** VFS should return SQLITE_NOTFOUND for this opcode.
969969
**
970
-** <li>[[SQLITE_FCNTL_OTA]]
971
-** The [SQLITE_FCNTL_OTA] opcode is implemented by the special VFS used by
972
-** the OTA extension only. All other VFS should return SQLITE_NOTFOUND for
970
+** <li>[[SQLITE_FCNTL_RBU]]
971
+** The [SQLITE_FCNTL_RBU] opcode is implemented by the special VFS used by
972
+** the RBU extension only. All other VFS should return SQLITE_NOTFOUND for
973973
** this opcode.
974974
** </ul>
975975
*/
976976
#define SQLITE_FCNTL_LOCKSTATE 1
977977
#define SQLITE_FCNTL_GET_LOCKPROXYFILE 2
@@ -995,11 +995,11 @@
995995
#define SQLITE_FCNTL_SYNC 21
996996
#define SQLITE_FCNTL_COMMIT_PHASETWO 22
997997
#define SQLITE_FCNTL_WIN32_SET_HANDLE 23
998998
#define SQLITE_FCNTL_WAL_BLOCK 24
999999
#define SQLITE_FCNTL_ZIPVFS 25
1000
-#define SQLITE_FCNTL_OTA 26
1000
+#define SQLITE_FCNTL_RBU 26
10011001
10021002
/* deprecated names */
10031003
#define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE
10041004
#define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE
10051005
#define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO
10061006
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -111,11 +111,11 @@
111 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
112 ** [sqlite_version()] and [sqlite_source_id()].
113 */
114 #define SQLITE_VERSION "3.8.11"
115 #define SQLITE_VERSION_NUMBER 3008011
116 #define SQLITE_SOURCE_ID "2015-07-16 18:18:19 b79a4affe44bd0c8e155cae19f3f62c715684cd6"
117
118 /*
119 ** CAPI3REF: Run-Time Library Version Numbers
120 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
121 **
@@ -965,13 +965,13 @@
965 **
966 ** <li>[[SQLITE_FCNTL_ZIPVFS]]
967 ** The [SQLITE_FCNTL_ZIPVFS] opcode is implemented by zipvfs only. All other
968 ** VFS should return SQLITE_NOTFOUND for this opcode.
969 **
970 ** <li>[[SQLITE_FCNTL_OTA]]
971 ** The [SQLITE_FCNTL_OTA] opcode is implemented by the special VFS used by
972 ** the OTA extension only. All other VFS should return SQLITE_NOTFOUND for
973 ** this opcode.
974 ** </ul>
975 */
976 #define SQLITE_FCNTL_LOCKSTATE 1
977 #define SQLITE_FCNTL_GET_LOCKPROXYFILE 2
@@ -995,11 +995,11 @@
995 #define SQLITE_FCNTL_SYNC 21
996 #define SQLITE_FCNTL_COMMIT_PHASETWO 22
997 #define SQLITE_FCNTL_WIN32_SET_HANDLE 23
998 #define SQLITE_FCNTL_WAL_BLOCK 24
999 #define SQLITE_FCNTL_ZIPVFS 25
1000 #define SQLITE_FCNTL_OTA 26
1001
1002 /* deprecated names */
1003 #define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE
1004 #define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE
1005 #define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO
1006
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -111,11 +111,11 @@
111 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
112 ** [sqlite_version()] and [sqlite_source_id()].
113 */
114 #define SQLITE_VERSION "3.8.11"
115 #define SQLITE_VERSION_NUMBER 3008011
116 #define SQLITE_SOURCE_ID "2015-07-23 20:44:49 017c5019e1ce042025d4f327e50ec50af49f9fa4"
117
118 /*
119 ** CAPI3REF: Run-Time Library Version Numbers
120 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
121 **
@@ -965,13 +965,13 @@
965 **
966 ** <li>[[SQLITE_FCNTL_ZIPVFS]]
967 ** The [SQLITE_FCNTL_ZIPVFS] opcode is implemented by zipvfs only. All other
968 ** VFS should return SQLITE_NOTFOUND for this opcode.
969 **
970 ** <li>[[SQLITE_FCNTL_RBU]]
971 ** The [SQLITE_FCNTL_RBU] opcode is implemented by the special VFS used by
972 ** the RBU extension only. All other VFS should return SQLITE_NOTFOUND for
973 ** this opcode.
974 ** </ul>
975 */
976 #define SQLITE_FCNTL_LOCKSTATE 1
977 #define SQLITE_FCNTL_GET_LOCKPROXYFILE 2
@@ -995,11 +995,11 @@
995 #define SQLITE_FCNTL_SYNC 21
996 #define SQLITE_FCNTL_COMMIT_PHASETWO 22
997 #define SQLITE_FCNTL_WIN32_SET_HANDLE 23
998 #define SQLITE_FCNTL_WAL_BLOCK 24
999 #define SQLITE_FCNTL_ZIPVFS 25
1000 #define SQLITE_FCNTL_RBU 26
1001
1002 /* deprecated names */
1003 #define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE
1004 #define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE
1005 #define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO
1006

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button