Fossil SCM

Merge trunk into wcag-2.1 for cleaner diff comparison.

stephan 2022-11-13 17:18 wcag-2.1 merge
Commit e1a9fdd768fe00052aeccc9fe1f526ed501faffcb27bb77198728eb9e4f7baad
+3308 -701
--- extsrc/shell.c
+++ extsrc/shell.c
@@ -32,10 +32,12 @@
3232
*/
3333
#if (defined(_WIN32) || defined(WIN32)) && !defined(_CRT_SECURE_NO_WARNINGS)
3434
/* This needs to come before any includes for MSVC compiler */
3535
#define _CRT_SECURE_NO_WARNINGS
3636
#endif
37
+typedef unsigned int u32;
38
+typedef unsigned short int u16;
3739
3840
/*
3941
** Optionally #include a user-defined header, whereby compilation options
4042
** may be set prior to where they take effect, but after platform setup.
4143
** If SQLITE_CUSTOM_INCLUDE=? is defined, its value names the #include
@@ -565,10 +567,11 @@
565567
*/
566568
static void utf8_width_print(FILE *pOut, int w, const char *zUtf){
567569
int i;
568570
int n;
569571
int aw = w<0 ? -w : w;
572
+ if( zUtf==0 ) zUtf = "";
570573
for(i=n=0; zUtf[i]; i++){
571574
if( (zUtf[i]&0xc0)!=0x80 ){
572575
n++;
573576
if( n==aw ){
574577
do{ i++; }while( (zUtf[i]&0xc0)==0x80 );
@@ -6890,12 +6893,12 @@
68906893
# define UINT16_TYPE unsigned short int
68916894
# endif
68926895
#endif
68936896
/* typedef sqlite3_int64 i64; */
68946897
/* typedef unsigned char u8; */
6895
-typedef UINT32_TYPE u32; /* 4-byte unsigned integer */
6896
-typedef UINT16_TYPE u16; /* 2-byte unsigned integer */
6898
+/* typedef UINT32_TYPE u32; // 4-byte unsigned integer // */
6899
+/* typedef UINT16_TYPE u16; // 2-byte unsigned integer // */
68976900
#define MIN(a,b) ((a)<(b) ? (a) : (b))
68986901
68996902
#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
69006903
# define SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS 1
69016904
#endif
@@ -11402,11 +11405,16 @@
1140211405
#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
1140311406
1140411407
/************************* End ../ext/expert/sqlite3expert.c ********************/
1140511408
1140611409
#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
11407
-/************************* Begin ../ext/misc/dbdata.c ******************/
11410
+#define SQLITE_SHELL_HAVE_RECOVER 1
11411
+#else
11412
+#define SQLITE_SHELL_HAVE_RECOVER 0
11413
+#endif
11414
+#if SQLITE_SHELL_HAVE_RECOVER
11415
+/************************* Begin ../ext/recover/dbdata.c ******************/
1140811416
/*
1140911417
** 2019-04-17
1141011418
**
1141111419
** The author disclaims copyright to this source code. In place of
1141211420
** a legal notice, here is a blessing:
@@ -11476,19 +11484,23 @@
1147611484
** );
1147711485
**
1147811486
** It contains one entry for each b-tree pointer between a parent and
1147911487
** child page in the database.
1148011488
*/
11489
+
1148111490
#if !defined(SQLITEINT_H)
1148211491
/* #include "sqlite3ext.h" */
1148311492
1148411493
/* typedef unsigned char u8; */
11494
+/* typedef unsigned int u32; */
1148511495
1148611496
#endif
1148711497
SQLITE_EXTENSION_INIT1
1148811498
#include <string.h>
1148911499
#include <assert.h>
11500
+
11501
+#ifndef SQLITE_OMIT_VIRTUALTABLE
1149011502
1149111503
#define DBDATA_PADDING_BYTES 100
1149211504
1149311505
typedef struct DbdataTable DbdataTable;
1149411506
typedef struct DbdataCursor DbdataCursor;
@@ -11507,15 +11519,16 @@
1150711519
int szDb;
1150811520
sqlite3_int64 iRowid;
1150911521
1151011522
/* Only for the sqlite_dbdata table */
1151111523
u8 *pRec; /* Buffer containing current record */
11512
- int nRec; /* Size of pRec[] in bytes */
11513
- int nHdr; /* Size of header in bytes */
11524
+ sqlite3_int64 nRec; /* Size of pRec[] in bytes */
11525
+ sqlite3_int64 nHdr; /* Size of header in bytes */
1151411526
int iField; /* Current field number */
1151511527
u8 *pHdrPtr;
1151611528
u8 *pPtr;
11529
+ u32 enc; /* Text encoding */
1151711530
1151811531
sqlite3_int64 iIntkey; /* Integer key value */
1151911532
};
1152011533
1152111534
/* Table object */
@@ -11704,18 +11717,18 @@
1170411717
}
1170511718
1170611719
/*
1170711720
** Utility methods to decode 16 and 32-bit big-endian unsigned integers.
1170811721
*/
11709
-static unsigned int get_uint16(unsigned char *a){
11722
+static u32 get_uint16(unsigned char *a){
1171011723
return (a[0]<<8)|a[1];
1171111724
}
11712
-static unsigned int get_uint32(unsigned char *a){
11713
- return ((unsigned int)a[0]<<24)
11714
- | ((unsigned int)a[1]<<16)
11715
- | ((unsigned int)a[2]<<8)
11716
- | ((unsigned int)a[3]);
11725
+static u32 get_uint32(unsigned char *a){
11726
+ return ((u32)a[0]<<24)
11727
+ | ((u32)a[1]<<16)
11728
+ | ((u32)a[2]<<8)
11729
+ | ((u32)a[3]);
1171711730
}
1171811731
1171911732
/*
1172011733
** Load page pgno from the database via the sqlite_dbpage virtual table.
1172111734
** If successful, set (*ppPage) to point to a buffer containing the page
@@ -11726,57 +11739,72 @@
1172611739
** Or, if an error occurs, set both (*ppPage) and (*pnPage) to 0 and
1172711740
** return an SQLite error code.
1172811741
*/
1172911742
static int dbdataLoadPage(
1173011743
DbdataCursor *pCsr, /* Cursor object */
11731
- unsigned int pgno, /* Page number of page to load */
11744
+ u32 pgno, /* Page number of page to load */
1173211745
u8 **ppPage, /* OUT: pointer to page buffer */
1173311746
int *pnPage /* OUT: Size of (*ppPage) in bytes */
1173411747
){
1173511748
int rc2;
1173611749
int rc = SQLITE_OK;
1173711750
sqlite3_stmt *pStmt = pCsr->pStmt;
1173811751
1173911752
*ppPage = 0;
1174011753
*pnPage = 0;
11741
- sqlite3_bind_int64(pStmt, 2, pgno);
11742
- if( SQLITE_ROW==sqlite3_step(pStmt) ){
11743
- int nCopy = sqlite3_column_bytes(pStmt, 0);
11744
- if( nCopy>0 ){
11745
- u8 *pPage;
11746
- pPage = (u8*)sqlite3_malloc64(nCopy + DBDATA_PADDING_BYTES);
11747
- if( pPage==0 ){
11748
- rc = SQLITE_NOMEM;
11749
- }else{
11750
- const u8 *pCopy = sqlite3_column_blob(pStmt, 0);
11751
- memcpy(pPage, pCopy, nCopy);
11752
- memset(&pPage[nCopy], 0, DBDATA_PADDING_BYTES);
11753
- }
11754
- *ppPage = pPage;
11755
- *pnPage = nCopy;
11756
- }
11757
- }
11758
- rc2 = sqlite3_reset(pStmt);
11759
- if( rc==SQLITE_OK ) rc = rc2;
11754
+ if( pgno>0 ){
11755
+ sqlite3_bind_int64(pStmt, 2, pgno);
11756
+ if( SQLITE_ROW==sqlite3_step(pStmt) ){
11757
+ int nCopy = sqlite3_column_bytes(pStmt, 0);
11758
+ if( nCopy>0 ){
11759
+ u8 *pPage;
11760
+ pPage = (u8*)sqlite3_malloc64(nCopy + DBDATA_PADDING_BYTES);
11761
+ if( pPage==0 ){
11762
+ rc = SQLITE_NOMEM;
11763
+ }else{
11764
+ const u8 *pCopy = sqlite3_column_blob(pStmt, 0);
11765
+ memcpy(pPage, pCopy, nCopy);
11766
+ memset(&pPage[nCopy], 0, DBDATA_PADDING_BYTES);
11767
+ }
11768
+ *ppPage = pPage;
11769
+ *pnPage = nCopy;
11770
+ }
11771
+ }
11772
+ rc2 = sqlite3_reset(pStmt);
11773
+ if( rc==SQLITE_OK ) rc = rc2;
11774
+ }
1176011775
1176111776
return rc;
1176211777
}
1176311778
1176411779
/*
1176511780
** Read a varint. Put the value in *pVal and return the number of bytes.
1176611781
*/
1176711782
static int dbdataGetVarint(const u8 *z, sqlite3_int64 *pVal){
11768
- sqlite3_int64 v = 0;
11783
+ sqlite3_uint64 u = 0;
1176911784
int i;
1177011785
for(i=0; i<8; i++){
11771
- v = (v<<7) + (z[i]&0x7f);
11772
- if( (z[i]&0x80)==0 ){ *pVal = v; return i+1; }
11786
+ u = (u<<7) + (z[i]&0x7f);
11787
+ if( (z[i]&0x80)==0 ){ *pVal = (sqlite3_int64)u; return i+1; }
1177311788
}
11774
- v = (v<<8) + (z[i]&0xff);
11775
- *pVal = v;
11789
+ u = (u<<8) + (z[i]&0xff);
11790
+ *pVal = (sqlite3_int64)u;
1177611791
return 9;
1177711792
}
11793
+
11794
+/*
11795
+** Like dbdataGetVarint(), but set the output to 0 if it is less than 0
11796
+** or greater than 0xFFFFFFFF. This can be used for all varints in an
11797
+** SQLite database except for key values in intkey tables.
11798
+*/
11799
+static int dbdataGetVarintU32(const u8 *z, sqlite3_int64 *pVal){
11800
+ sqlite3_int64 val;
11801
+ int nRet = dbdataGetVarint(z, &val);
11802
+ if( val<0 || val>0xFFFFFFFF ) val = 0;
11803
+ *pVal = val;
11804
+ return nRet;
11805
+}
1177811806
1177911807
/*
1178011808
** Return the number of bytes of space used by an SQLite value of type
1178111809
** eType.
1178211810
*/
@@ -11810,10 +11838,11 @@
1181011838
** Load a value of type eType from buffer pData and use it to set the
1181111839
** result of context object pCtx.
1181211840
*/
1181311841
static void dbdataValue(
1181411842
sqlite3_context *pCtx,
11843
+ u32 enc,
1181511844
int eType,
1181611845
u8 *pData,
1181711846
int nData
1181811847
){
1181911848
if( eType>=0 && dbdataValueBytes(eType)<=nData ){
@@ -11854,11 +11883,23 @@
1185411883
}
1185511884
1185611885
default: {
1185711886
int n = ((eType-12) / 2);
1185811887
if( eType % 2 ){
11859
- sqlite3_result_text(pCtx, (const char*)pData, n, SQLITE_TRANSIENT);
11888
+ switch( enc ){
11889
+#ifndef SQLITE_OMIT_UTF16
11890
+ case SQLITE_UTF16BE:
11891
+ sqlite3_result_text16be(pCtx, (void*)pData, n, SQLITE_TRANSIENT);
11892
+ break;
11893
+ case SQLITE_UTF16LE:
11894
+ sqlite3_result_text16le(pCtx, (void*)pData, n, SQLITE_TRANSIENT);
11895
+ break;
11896
+#endif
11897
+ default:
11898
+ sqlite3_result_text(pCtx, (char*)pData, n, SQLITE_TRANSIENT);
11899
+ break;
11900
+ }
1186011901
}else{
1186111902
sqlite3_result_blob(pCtx, pData, n, SQLITE_TRANSIENT);
1186211903
}
1186311904
}
1186411905
}
@@ -11882,10 +11923,11 @@
1188211923
while( 1 ){
1188311924
if( pCsr->bOnePage==0 && pCsr->iPgno>pCsr->szDb ) return SQLITE_OK;
1188411925
rc = dbdataLoadPage(pCsr, pCsr->iPgno, &pCsr->aPage, &pCsr->nPage);
1188511926
if( rc!=SQLITE_OK ) return rc;
1188611927
if( pCsr->aPage ) break;
11928
+ if( pCsr->bOnePage ) return SQLITE_OK;
1188711929
pCsr->iPgno++;
1188811930
}
1188911931
pCsr->iCell = pTab->bPtr ? -2 : 0;
1189011932
pCsr->nCell = get_uint16(&pCsr->aPage[iOff+3]);
1189111933
}
@@ -11945,11 +11987,11 @@
1194511987
1194611988
/* Load the "byte of payload including overflow" field */
1194711989
if( bNextPage || iOff>pCsr->nPage ){
1194811990
bNextPage = 1;
1194911991
}else{
11950
- iOff += dbdataGetVarint(&pCsr->aPage[iOff], &nPayload);
11992
+ iOff += dbdataGetVarintU32(&pCsr->aPage[iOff], &nPayload);
1195111993
}
1195211994
1195311995
/* If this is a leaf intkey cell, load the rowid */
1195411996
if( bHasRowid && !bNextPage && iOff<pCsr->nPage ){
1195511997
iOff += dbdataGetVarint(&pCsr->aPage[iOff], &pCsr->iIntkey);
@@ -11992,11 +12034,11 @@
1199212034
iOff += nLocal;
1199312035
1199412036
/* Load content from overflow pages */
1199512037
if( nPayload>nLocal ){
1199612038
sqlite3_int64 nRem = nPayload - nLocal;
11997
- unsigned int pgnoOvfl = get_uint32(&pCsr->aPage[iOff]);
12039
+ u32 pgnoOvfl = get_uint32(&pCsr->aPage[iOff]);
1199812040
while( nRem>0 ){
1199912041
u8 *aOvfl = 0;
1200012042
int nOvfl = 0;
1200112043
int nCopy;
1200212044
rc = dbdataLoadPage(pCsr, pgnoOvfl, &aOvfl, &nOvfl);
@@ -12012,11 +12054,12 @@
1201212054
pgnoOvfl = get_uint32(aOvfl);
1201312055
sqlite3_free(aOvfl);
1201412056
}
1201512057
}
1201612058
12017
- iHdr = dbdataGetVarint(pCsr->pRec, &nHdr);
12059
+ iHdr = dbdataGetVarintU32(pCsr->pRec, &nHdr);
12060
+ if( nHdr>nPayload ) nHdr = 0;
1201812061
pCsr->nHdr = nHdr;
1201912062
pCsr->pHdrPtr = &pCsr->pRec[iHdr];
1202012063
pCsr->pPtr = &pCsr->pRec[pCsr->nHdr];
1202112064
pCsr->iField = (bHasRowid ? -1 : 0);
1202212065
}
@@ -12026,11 +12069,11 @@
1202612069
if( pCsr->iField>0 ){
1202712070
sqlite3_int64 iType;
1202812071
if( pCsr->pHdrPtr>&pCsr->pRec[pCsr->nRec] ){
1202912072
bNextPage = 1;
1203012073
}else{
12031
- pCsr->pHdrPtr += dbdataGetVarint(pCsr->pHdrPtr, &iType);
12074
+ pCsr->pHdrPtr += dbdataGetVarintU32(pCsr->pHdrPtr, &iType);
1203212075
pCsr->pPtr += dbdataValueBytes(iType);
1203312076
}
1203412077
}
1203512078
}
1203612079
@@ -12064,10 +12107,22 @@
1206412107
*/
1206512108
static int dbdataEof(sqlite3_vtab_cursor *pCursor){
1206612109
DbdataCursor *pCsr = (DbdataCursor*)pCursor;
1206712110
return pCsr->aPage==0;
1206812111
}
12112
+
12113
+/*
12114
+** Return true if nul-terminated string zSchema ends in "()". Or false
12115
+** otherwise.
12116
+*/
12117
+static int dbdataIsFunction(const char *zSchema){
12118
+ size_t n = strlen(zSchema);
12119
+ if( n>2 && zSchema[n-2]=='(' && zSchema[n-1]==')' ){
12120
+ return (int)n-2;
12121
+ }
12122
+ return 0;
12123
+}
1206912124
1207012125
/*
1207112126
** Determine the size in pages of database zSchema (where zSchema is
1207212127
** "main", "temp" or the name of an attached database) and set
1207312128
** pCsr->szDb accordingly. If successful, return SQLITE_OK. Otherwise,
@@ -12075,23 +12130,48 @@
1207512130
*/
1207612131
static int dbdataDbsize(DbdataCursor *pCsr, const char *zSchema){
1207712132
DbdataTable *pTab = (DbdataTable*)pCsr->base.pVtab;
1207812133
char *zSql = 0;
1207912134
int rc, rc2;
12135
+ int nFunc = 0;
1208012136
sqlite3_stmt *pStmt = 0;
1208112137
12082
- zSql = sqlite3_mprintf("PRAGMA %Q.page_count", zSchema);
12138
+ if( (nFunc = dbdataIsFunction(zSchema))>0 ){
12139
+ zSql = sqlite3_mprintf("SELECT %.*s(0)", nFunc, zSchema);
12140
+ }else{
12141
+ zSql = sqlite3_mprintf("PRAGMA %Q.page_count", zSchema);
12142
+ }
1208312143
if( zSql==0 ) return SQLITE_NOMEM;
12144
+
1208412145
rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pStmt, 0);
1208512146
sqlite3_free(zSql);
1208612147
if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
1208712148
pCsr->szDb = sqlite3_column_int(pStmt, 0);
1208812149
}
1208912150
rc2 = sqlite3_finalize(pStmt);
1209012151
if( rc==SQLITE_OK ) rc = rc2;
1209112152
return rc;
1209212153
}
12154
+
12155
+/*
12156
+** Attempt to figure out the encoding of the database by retrieving page 1
12157
+** and inspecting the header field. If successful, set the pCsr->enc variable
12158
+** and return SQLITE_OK. Otherwise, return an SQLite error code.
12159
+*/
12160
+static int dbdataGetEncoding(DbdataCursor *pCsr){
12161
+ int rc = SQLITE_OK;
12162
+ int nPg1 = 0;
12163
+ u8 *aPg1 = 0;
12164
+ rc = dbdataLoadPage(pCsr, 1, &aPg1, &nPg1);
12165
+ assert( rc!=SQLITE_OK || nPg1==0 || nPg1>=512 );
12166
+ if( rc==SQLITE_OK && nPg1>0 ){
12167
+ pCsr->enc = get_uint32(&aPg1[56]);
12168
+ }
12169
+ sqlite3_free(aPg1);
12170
+ return rc;
12171
+}
12172
+
1209312173
1209412174
/*
1209512175
** xFilter method for sqlite_dbdata and sqlite_dbptr.
1209612176
*/
1209712177
static int dbdataFilter(
@@ -12106,23 +12186,32 @@
1210612186
1210712187
dbdataResetCursor(pCsr);
1210812188
assert( pCsr->iPgno==1 );
1210912189
if( idxNum & 0x01 ){
1211012190
zSchema = (const char*)sqlite3_value_text(argv[0]);
12191
+ if( zSchema==0 ) zSchema = "";
1211112192
}
1211212193
if( idxNum & 0x02 ){
1211312194
pCsr->iPgno = sqlite3_value_int(argv[(idxNum & 0x01)]);
1211412195
pCsr->bOnePage = 1;
1211512196
}else{
12116
- pCsr->nPage = dbdataDbsize(pCsr, zSchema);
1211712197
rc = dbdataDbsize(pCsr, zSchema);
1211812198
}
1211912199
1212012200
if( rc==SQLITE_OK ){
12201
+ int nFunc = 0;
1212112202
if( pTab->pStmt ){
1212212203
pCsr->pStmt = pTab->pStmt;
1212312204
pTab->pStmt = 0;
12205
+ }else if( (nFunc = dbdataIsFunction(zSchema))>0 ){
12206
+ char *zSql = sqlite3_mprintf("SELECT %.*s(?2)", nFunc, zSchema);
12207
+ if( zSql==0 ){
12208
+ rc = SQLITE_NOMEM;
12209
+ }else{
12210
+ rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pStmt, 0);
12211
+ sqlite3_free(zSql);
12212
+ }
1212412213
}else{
1212512214
rc = sqlite3_prepare_v2(pTab->db,
1212612215
"SELECT data FROM sqlite_dbpage(?) WHERE pgno=?", -1,
1212712216
&pCsr->pStmt, 0
1212812217
);
@@ -12131,17 +12220,24 @@
1213112220
if( rc==SQLITE_OK ){
1213212221
rc = sqlite3_bind_text(pCsr->pStmt, 1, zSchema, -1, SQLITE_TRANSIENT);
1213312222
}else{
1213412223
pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
1213512224
}
12225
+
12226
+ /* Try to determine the encoding of the db by inspecting the header
12227
+ ** field on page 1. */
12228
+ if( rc==SQLITE_OK ){
12229
+ rc = dbdataGetEncoding(pCsr);
12230
+ }
12231
+
1213612232
if( rc==SQLITE_OK ){
1213712233
rc = dbdataNext(pCursor);
1213812234
}
1213912235
return rc;
1214012236
}
1214112237
12142
-/*
12238
+/*
1214312239
** Return a column for the sqlite_dbdata or sqlite_dbptr table.
1214412240
*/
1214512241
static int dbdataColumn(
1214612242
sqlite3_vtab_cursor *pCursor,
1214712243
sqlite3_context *ctx,
@@ -12183,13 +12279,14 @@
1218312279
case DBDATA_COLUMN_VALUE: {
1218412280
if( pCsr->iField<0 ){
1218512281
sqlite3_result_int64(ctx, pCsr->iIntkey);
1218612282
}else{
1218712283
sqlite3_int64 iType;
12188
- dbdataGetVarint(pCsr->pHdrPtr, &iType);
12284
+ dbdataGetVarintU32(pCsr->pHdrPtr, &iType);
1218912285
dbdataValue(
12190
- ctx, iType, pCsr->pPtr, &pCsr->pRec[pCsr->nRec] - pCsr->pPtr
12286
+ ctx, pCsr->enc, iType, pCsr->pPtr,
12287
+ &pCsr->pRec[pCsr->nRec] - pCsr->pPtr
1219112288
);
1219212289
}
1219312290
break;
1219412291
}
1219512292
}
@@ -12255,11 +12352,3125 @@
1225512352
){
1225612353
SQLITE_EXTENSION_INIT2(pApi);
1225712354
return sqlite3DbdataRegister(db);
1225812355
}
1225912356
12260
-/************************* End ../ext/misc/dbdata.c ********************/
12357
+#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
12358
+
12359
+/************************* End ../ext/recover/dbdata.c ********************/
12360
+/************************* Begin ../ext/recover/sqlite3recover.h ******************/
12361
+/*
12362
+** 2022-08-27
12363
+**
12364
+** The author disclaims copyright to this source code. In place of
12365
+** a legal notice, here is a blessing:
12366
+**
12367
+** May you do good and not evil.
12368
+** May you find forgiveness for yourself and forgive others.
12369
+** May you share freely, never taking more than you give.
12370
+**
12371
+*************************************************************************
12372
+**
12373
+** This file contains the public interface to the "recover" extension -
12374
+** an SQLite extension designed to recover data from corrupted database
12375
+** files.
12376
+*/
12377
+
12378
+/*
12379
+** OVERVIEW:
12380
+**
12381
+** To use the API to recover data from a corrupted database, an
12382
+** application:
12383
+**
12384
+** 1) Creates an sqlite3_recover handle by calling either
12385
+** sqlite3_recover_init() or sqlite3_recover_init_sql().
12386
+**
12387
+** 2) Configures the new handle using one or more calls to
12388
+** sqlite3_recover_config().
12389
+**
12390
+** 3) Executes the recovery by repeatedly calling sqlite3_recover_step() on
12391
+** the handle until it returns something other than SQLITE_OK. If it
12392
+** returns SQLITE_DONE, then the recovery operation completed without
12393
+** error. If it returns some other non-SQLITE_OK value, then an error
12394
+** has occurred.
12395
+**
12396
+** 4) Retrieves any error code and English language error message using the
12397
+** sqlite3_recover_errcode() and sqlite3_recover_errmsg() APIs,
12398
+** respectively.
12399
+**
12400
+** 5) Destroys the sqlite3_recover handle and frees all resources
12401
+** using sqlite3_recover_finish().
12402
+**
12403
+** The application may abandon the recovery operation at any point
12404
+** before it is finished by passing the sqlite3_recover handle to
12405
+** sqlite3_recover_finish(). This is not an error, but the final state
12406
+** of the output database, or the results of running the partial script
12407
+** delivered to the SQL callback, are undefined.
12408
+*/
12409
+
12410
+#ifndef _SQLITE_RECOVER_H
12411
+#define _SQLITE_RECOVER_H
12412
+
12413
+/* #include "sqlite3.h" */
12414
+
12415
+#ifdef __cplusplus
12416
+extern "C" {
12417
+#endif
12418
+
12419
+/*
12420
+** An instance of the sqlite3_recover object represents a recovery
12421
+** operation in progress.
12422
+**
12423
+** Constructors:
12424
+**
12425
+** sqlite3_recover_init()
12426
+** sqlite3_recover_init_sql()
12427
+**
12428
+** Destructor:
12429
+**
12430
+** sqlite3_recover_finish()
12431
+**
12432
+** Methods:
12433
+**
12434
+** sqlite3_recover_config()
12435
+** sqlite3_recover_errcode()
12436
+** sqlite3_recover_errmsg()
12437
+** sqlite3_recover_run()
12438
+** sqlite3_recover_step()
12439
+*/
12440
+typedef struct sqlite3_recover sqlite3_recover;
12441
+
12442
+/*
12443
+** These two APIs attempt to create and return a new sqlite3_recover object.
12444
+** In both cases the first two arguments identify the (possibly
12445
+** corrupt) database to recover data from. The first argument is an open
12446
+** database handle and the second the name of a database attached to that
12447
+** handle (i.e. "main", "temp" or the name of an attached database).
12448
+**
12449
+** If sqlite3_recover_init() is used to create the new sqlite3_recover
12450
+** handle, then data is recovered into a new database, identified by
12451
+** string parameter zUri. zUri may be an absolute or relative file path,
12452
+** or may be an SQLite URI. If the identified database file already exists,
12453
+** it is overwritten.
12454
+**
12455
+** If sqlite3_recover_init_sql() is invoked, then any recovered data will
12456
+** be returned to the user as a series of SQL statements. Executing these
12457
+** SQL statements results in the same database as would have been created
12458
+** had sqlite3_recover_init() been used. For each SQL statement in the
12459
+** output, the callback function passed as the third argument (xSql) is
12460
+** invoked once. The first parameter is a passed a copy of the fourth argument
12461
+** to this function (pCtx) as its first parameter, and a pointer to a
12462
+** nul-terminated buffer containing the SQL statement formated as UTF-8 as
12463
+** the second. If the xSql callback returns any value other than SQLITE_OK,
12464
+** then processing is immediately abandoned and the value returned used as
12465
+** the recover handle error code (see below).
12466
+**
12467
+** If an out-of-memory error occurs, NULL may be returned instead of
12468
+** a valid handle. In all other cases, it is the responsibility of the
12469
+** application to avoid resource leaks by ensuring that
12470
+** sqlite3_recover_finish() is called on all allocated handles.
12471
+*/
12472
+sqlite3_recover *sqlite3_recover_init(
12473
+ sqlite3* db,
12474
+ const char *zDb,
12475
+ const char *zUri
12476
+);
12477
+sqlite3_recover *sqlite3_recover_init_sql(
12478
+ sqlite3* db,
12479
+ const char *zDb,
12480
+ int (*xSql)(void*, const char*),
12481
+ void *pCtx
12482
+);
12483
+
12484
+/*
12485
+** Configure an sqlite3_recover object that has just been created using
12486
+** sqlite3_recover_init() or sqlite3_recover_init_sql(). This function
12487
+** may only be called before the first call to sqlite3_recover_step()
12488
+** or sqlite3_recover_run() on the object.
12489
+**
12490
+** The second argument passed to this function must be one of the
12491
+** SQLITE_RECOVER_* symbols defined below. Valid values for the third argument
12492
+** depend on the specific SQLITE_RECOVER_* symbol in use.
12493
+**
12494
+** SQLITE_OK is returned if the configuration operation was successful,
12495
+** or an SQLite error code otherwise.
12496
+*/
12497
+int sqlite3_recover_config(sqlite3_recover*, int op, void *pArg);
12498
+
12499
+/*
12500
+** SQLITE_RECOVER_LOST_AND_FOUND:
12501
+** The pArg argument points to a string buffer containing the name
12502
+** of a "lost-and-found" table in the output database, or NULL. If
12503
+** the argument is non-NULL and the database contains seemingly
12504
+** valid pages that cannot be associated with any table in the
12505
+** recovered part of the schema, data is extracted from these
12506
+** pages to add to the lost-and-found table.
12507
+**
12508
+** SQLITE_RECOVER_FREELIST_CORRUPT:
12509
+** The pArg value must actually be a pointer to a value of type
12510
+** int containing value 0 or 1 cast as a (void*). If this option is set
12511
+** (argument is 1) and a lost-and-found table has been configured using
12512
+** SQLITE_RECOVER_LOST_AND_FOUND, then is assumed that the freelist is
12513
+** corrupt and an attempt is made to recover records from pages that
12514
+** appear to be linked into the freelist. Otherwise, pages on the freelist
12515
+** are ignored. Setting this option can recover more data from the
12516
+** database, but often ends up "recovering" deleted records. The default
12517
+** value is 0 (clear).
12518
+**
12519
+** SQLITE_RECOVER_ROWIDS:
12520
+** The pArg value must actually be a pointer to a value of type
12521
+** int containing value 0 or 1 cast as a (void*). If this option is set
12522
+** (argument is 1), then an attempt is made to recover rowid values
12523
+** that are not also INTEGER PRIMARY KEY values. If this option is
12524
+** clear, then new rowids are assigned to all recovered rows. The
12525
+** default value is 1 (set).
12526
+**
12527
+** SQLITE_RECOVER_SLOWINDEXES:
12528
+** The pArg value must actually be a pointer to a value of type
12529
+** int containing value 0 or 1 cast as a (void*). If this option is clear
12530
+** (argument is 0), then when creating an output database, the recover
12531
+** module creates and populates non-UNIQUE indexes right at the end of the
12532
+** recovery operation - after all recoverable data has been inserted
12533
+** into the new database. This is faster overall, but means that the
12534
+** final call to sqlite3_recover_step() for a recovery operation may
12535
+** be need to create a large number of indexes, which may be very slow.
12536
+**
12537
+** Or, if this option is set (argument is 1), then non-UNIQUE indexes
12538
+** are created in the output database before it is populated with
12539
+** recovered data. This is slower overall, but avoids the slow call
12540
+** to sqlite3_recover_step() at the end of the recovery operation.
12541
+**
12542
+** The default option value is 0.
12543
+*/
12544
+#define SQLITE_RECOVER_LOST_AND_FOUND 1
12545
+#define SQLITE_RECOVER_FREELIST_CORRUPT 2
12546
+#define SQLITE_RECOVER_ROWIDS 3
12547
+#define SQLITE_RECOVER_SLOWINDEXES 4
12548
+
12549
+/*
12550
+** Perform a unit of work towards the recovery operation. This function
12551
+** must normally be called multiple times to complete database recovery.
12552
+**
12553
+** If no error occurs but the recovery operation is not completed, this
12554
+** function returns SQLITE_OK. If recovery has been completed successfully
12555
+** then SQLITE_DONE is returned. If an error has occurred, then an SQLite
12556
+** error code (e.g. SQLITE_IOERR or SQLITE_NOMEM) is returned. It is not
12557
+** considered an error if some or all of the data cannot be recovered
12558
+** due to database corruption.
12559
+**
12560
+** Once sqlite3_recover_step() has returned a value other than SQLITE_OK,
12561
+** all further such calls on the same recover handle are no-ops that return
12562
+** the same non-SQLITE_OK value.
12563
+*/
12564
+int sqlite3_recover_step(sqlite3_recover*);
12565
+
12566
+/*
12567
+** Run the recovery operation to completion. Return SQLITE_OK if successful,
12568
+** or an SQLite error code otherwise. Calling this function is the same
12569
+** as executing:
12570
+**
12571
+** while( SQLITE_OK==sqlite3_recover_step(p) );
12572
+** return sqlite3_recover_errcode(p);
12573
+*/
12574
+int sqlite3_recover_run(sqlite3_recover*);
12575
+
12576
+/*
12577
+** If an error has been encountered during a prior call to
12578
+** sqlite3_recover_step(), then this function attempts to return a
12579
+** pointer to a buffer containing an English language explanation of
12580
+** the error. If no error message is available, or if an out-of memory
12581
+** error occurs while attempting to allocate a buffer in which to format
12582
+** the error message, NULL is returned.
12583
+**
12584
+** The returned buffer remains valid until the sqlite3_recover handle is
12585
+** destroyed using sqlite3_recover_finish().
12586
+*/
12587
+const char *sqlite3_recover_errmsg(sqlite3_recover*);
12588
+
12589
+/*
12590
+** If this function is called on an sqlite3_recover handle after
12591
+** an error occurs, an SQLite error code is returned. Otherwise, SQLITE_OK.
12592
+*/
12593
+int sqlite3_recover_errcode(sqlite3_recover*);
12594
+
12595
+/*
12596
+** Clean up a recovery object created by a call to sqlite3_recover_init().
12597
+** The results of using a recovery object with any API after it has been
12598
+** passed to this function are undefined.
12599
+**
12600
+** This function returns the same value as sqlite3_recover_errcode().
12601
+*/
12602
+int sqlite3_recover_finish(sqlite3_recover*);
12603
+
12604
+
12605
+#ifdef __cplusplus
12606
+} /* end of the 'extern "C"' block */
12607
+#endif
12608
+
12609
+#endif /* ifndef _SQLITE_RECOVER_H */
12610
+
12611
+/************************* End ../ext/recover/sqlite3recover.h ********************/
12612
+/************************* Begin ../ext/recover/sqlite3recover.c ******************/
12613
+/*
12614
+** 2022-08-27
12615
+**
12616
+** The author disclaims copyright to this source code. In place of
12617
+** a legal notice, here is a blessing:
12618
+**
12619
+** May you do good and not evil.
12620
+** May you find forgiveness for yourself and forgive others.
12621
+** May you share freely, never taking more than you give.
12622
+**
12623
+*************************************************************************
12624
+**
12625
+*/
12626
+
12627
+
12628
+/* #include "sqlite3recover.h" */
12629
+#include <assert.h>
12630
+#include <string.h>
12631
+
12632
+#ifndef SQLITE_OMIT_VIRTUALTABLE
12633
+
12634
+/*
12635
+** Declaration for public API function in file dbdata.c. This may be called
12636
+** with NULL as the final two arguments to register the sqlite_dbptr and
12637
+** sqlite_dbdata virtual tables with a database handle.
12638
+*/
12639
+#ifdef _WIN32
12640
+
12641
+#endif
12642
+int sqlite3_dbdata_init(sqlite3*, char**, const sqlite3_api_routines*);
12643
+
12644
+/* typedef unsigned int u32; */
12645
+/* typedef unsigned char u8; */
12646
+/* typedef sqlite3_int64 i64; */
12647
+
12648
+typedef struct RecoverTable RecoverTable;
12649
+typedef struct RecoverColumn RecoverColumn;
12650
+
12651
+/*
12652
+** When recovering rows of data that can be associated with table
12653
+** definitions recovered from the sqlite_schema table, each table is
12654
+** represented by an instance of the following object.
12655
+**
12656
+** iRoot:
12657
+** The root page in the original database. Not necessarily (and usually
12658
+** not) the same in the recovered database.
12659
+**
12660
+** zTab:
12661
+** Name of the table.
12662
+**
12663
+** nCol/aCol[]:
12664
+** aCol[] is an array of nCol columns. In the order in which they appear
12665
+** in the table.
12666
+**
12667
+** bIntkey:
12668
+** Set to true for intkey tables, false for WITHOUT ROWID.
12669
+**
12670
+** iRowidBind:
12671
+** Each column in the aCol[] array has associated with it the index of
12672
+** the bind parameter its values will be bound to in the INSERT statement
12673
+** used to construct the output database. If the table does has a rowid
12674
+** but not an INTEGER PRIMARY KEY column, then iRowidBind contains the
12675
+** index of the bind paramater to which the rowid value should be bound.
12676
+** Otherwise, it contains -1. If the table does contain an INTEGER PRIMARY
12677
+** KEY column, then the rowid value should be bound to the index associated
12678
+** with the column.
12679
+**
12680
+** pNext:
12681
+** All RecoverTable objects used by the recovery operation are allocated
12682
+** and populated as part of creating the recovered database schema in
12683
+** the output database, before any non-schema data are recovered. They
12684
+** are then stored in a singly-linked list linked by this variable beginning
12685
+** at sqlite3_recover.pTblList.
12686
+*/
12687
+struct RecoverTable {
12688
+ u32 iRoot; /* Root page in original database */
12689
+ char *zTab; /* Name of table */
12690
+ int nCol; /* Number of columns in table */
12691
+ RecoverColumn *aCol; /* Array of columns */
12692
+ int bIntkey; /* True for intkey, false for without rowid */
12693
+ int iRowidBind; /* If >0, bind rowid to INSERT here */
12694
+ RecoverTable *pNext;
12695
+};
12696
+
12697
+/*
12698
+** Each database column is represented by an instance of the following object
12699
+** stored in the RecoverTable.aCol[] array of the associated table.
12700
+**
12701
+** iField:
12702
+** The index of the associated field within database records. Or -1 if
12703
+** there is no associated field (e.g. for virtual generated columns).
12704
+**
12705
+** iBind:
12706
+** The bind index of the INSERT statement to bind this columns values
12707
+** to. Or 0 if there is no such index (iff (iField<0)).
12708
+**
12709
+** bIPK:
12710
+** True if this is the INTEGER PRIMARY KEY column.
12711
+**
12712
+** zCol:
12713
+** Name of column.
12714
+**
12715
+** eHidden:
12716
+** A RECOVER_EHIDDEN_* constant value (see below for interpretation of each).
12717
+*/
12718
+struct RecoverColumn {
12719
+ int iField; /* Field in record on disk */
12720
+ int iBind; /* Binding to use in INSERT */
12721
+ int bIPK; /* True for IPK column */
12722
+ char *zCol;
12723
+ int eHidden;
12724
+};
12725
+
12726
+#define RECOVER_EHIDDEN_NONE 0 /* Normal database column */
12727
+#define RECOVER_EHIDDEN_HIDDEN 1 /* Column is __HIDDEN__ */
12728
+#define RECOVER_EHIDDEN_VIRTUAL 2 /* Virtual generated column */
12729
+#define RECOVER_EHIDDEN_STORED 3 /* Stored generated column */
12730
+
12731
+/*
12732
+** Bitmap object used to track pages in the input database. Allocated
12733
+** and manipulated only by the following functions:
12734
+**
12735
+** recoverBitmapAlloc()
12736
+** recoverBitmapFree()
12737
+** recoverBitmapSet()
12738
+** recoverBitmapQuery()
12739
+**
12740
+** nPg:
12741
+** Largest page number that may be stored in the bitmap. The range
12742
+** of valid keys is 1 to nPg, inclusive.
12743
+**
12744
+** aElem[]:
12745
+** Array large enough to contain a bit for each key. For key value
12746
+** iKey, the associated bit is the bit (iKey%32) of aElem[iKey/32].
12747
+** In other words, the following is true if bit iKey is set, or
12748
+** false if it is clear:
12749
+**
12750
+** (aElem[iKey/32] & (1 << (iKey%32))) ? 1 : 0
12751
+*/
12752
+typedef struct RecoverBitmap RecoverBitmap;
12753
+struct RecoverBitmap {
12754
+ i64 nPg; /* Size of bitmap */
12755
+ u32 aElem[1]; /* Array of 32-bit bitmasks */
12756
+};
12757
+
12758
+/*
12759
+** State variables (part of the sqlite3_recover structure) used while
12760
+** recovering data for tables identified in the recovered schema (state
12761
+** RECOVER_STATE_WRITING).
12762
+*/
12763
+typedef struct RecoverStateW1 RecoverStateW1;
12764
+struct RecoverStateW1 {
12765
+ sqlite3_stmt *pTbls;
12766
+ sqlite3_stmt *pSel;
12767
+ sqlite3_stmt *pInsert;
12768
+ int nInsert;
12769
+
12770
+ RecoverTable *pTab; /* Table currently being written */
12771
+ int nMax; /* Max column count in any schema table */
12772
+ sqlite3_value **apVal; /* Array of nMax values */
12773
+ int nVal; /* Number of valid entries in apVal[] */
12774
+ int bHaveRowid;
12775
+ i64 iRowid;
12776
+ i64 iPrevPage;
12777
+ int iPrevCell;
12778
+};
12779
+
12780
+/*
12781
+** State variables (part of the sqlite3_recover structure) used while
12782
+** recovering data destined for the lost and found table (states
12783
+** RECOVER_STATE_LOSTANDFOUND[123]).
12784
+*/
12785
+typedef struct RecoverStateLAF RecoverStateLAF;
12786
+struct RecoverStateLAF {
12787
+ RecoverBitmap *pUsed;
12788
+ i64 nPg; /* Size of db in pages */
12789
+ sqlite3_stmt *pAllAndParent;
12790
+ sqlite3_stmt *pMapInsert;
12791
+ sqlite3_stmt *pMaxField;
12792
+ sqlite3_stmt *pUsedPages;
12793
+ sqlite3_stmt *pFindRoot;
12794
+ sqlite3_stmt *pInsert; /* INSERT INTO lost_and_found ... */
12795
+ sqlite3_stmt *pAllPage;
12796
+ sqlite3_stmt *pPageData;
12797
+ sqlite3_value **apVal;
12798
+ int nMaxField;
12799
+};
12800
+
12801
+/*
12802
+** Main recover handle structure.
12803
+*/
12804
+struct sqlite3_recover {
12805
+ /* Copies of sqlite3_recover_init[_sql]() parameters */
12806
+ sqlite3 *dbIn; /* Input database */
12807
+ char *zDb; /* Name of input db ("main" etc.) */
12808
+ char *zUri; /* URI for output database */
12809
+ void *pSqlCtx; /* SQL callback context */
12810
+ int (*xSql)(void*,const char*); /* Pointer to SQL callback function */
12811
+
12812
+ /* Values configured by sqlite3_recover_config() */
12813
+ char *zStateDb; /* State database to use (or NULL) */
12814
+ char *zLostAndFound; /* Name of lost-and-found table (or NULL) */
12815
+ int bFreelistCorrupt; /* SQLITE_RECOVER_FREELIST_CORRUPT setting */
12816
+ int bRecoverRowid; /* SQLITE_RECOVER_ROWIDS setting */
12817
+ int bSlowIndexes; /* SQLITE_RECOVER_SLOWINDEXES setting */
12818
+
12819
+ int pgsz;
12820
+ int detected_pgsz;
12821
+ int nReserve;
12822
+ u8 *pPage1Disk;
12823
+ u8 *pPage1Cache;
12824
+
12825
+ /* Error code and error message */
12826
+ int errCode; /* For sqlite3_recover_errcode() */
12827
+ char *zErrMsg; /* For sqlite3_recover_errmsg() */
12828
+
12829
+ int eState;
12830
+ int bCloseTransaction;
12831
+
12832
+ /* Variables used with eState==RECOVER_STATE_WRITING */
12833
+ RecoverStateW1 w1;
12834
+
12835
+ /* Variables used with states RECOVER_STATE_LOSTANDFOUND[123] */
12836
+ RecoverStateLAF laf;
12837
+
12838
+ /* Fields used within sqlite3_recover_run() */
12839
+ sqlite3 *dbOut; /* Output database */
12840
+ sqlite3_stmt *pGetPage; /* SELECT against input db sqlite_dbdata */
12841
+ RecoverTable *pTblList; /* List of tables recovered from schema */
12842
+};
12843
+
12844
+/*
12845
+** The various states in which an sqlite3_recover object may exist:
12846
+**
12847
+** RECOVER_STATE_INIT:
12848
+** The object is initially created in this state. sqlite3_recover_step()
12849
+** has yet to be called. This is the only state in which it is permitted
12850
+** to call sqlite3_recover_config().
12851
+**
12852
+** RECOVER_STATE_WRITING:
12853
+**
12854
+** RECOVER_STATE_LOSTANDFOUND1:
12855
+** State to populate the bitmap of pages used by other tables or the
12856
+** database freelist.
12857
+**
12858
+** RECOVER_STATE_LOSTANDFOUND2:
12859
+** Populate the recovery.map table - used to figure out a "root" page
12860
+** for each lost page from in the database from which records are
12861
+** extracted.
12862
+**
12863
+** RECOVER_STATE_LOSTANDFOUND3:
12864
+** Populate the lost-and-found table itself.
12865
+*/
12866
+#define RECOVER_STATE_INIT 0
12867
+#define RECOVER_STATE_WRITING 1
12868
+#define RECOVER_STATE_LOSTANDFOUND1 2
12869
+#define RECOVER_STATE_LOSTANDFOUND2 3
12870
+#define RECOVER_STATE_LOSTANDFOUND3 4
12871
+#define RECOVER_STATE_SCHEMA2 5
12872
+#define RECOVER_STATE_DONE 6
12873
+
12874
+
12875
+/*
12876
+** Global variables used by this extension.
12877
+*/
12878
+typedef struct RecoverGlobal RecoverGlobal;
12879
+struct RecoverGlobal {
12880
+ const sqlite3_io_methods *pMethods;
12881
+ sqlite3_recover *p;
12882
+};
12883
+static RecoverGlobal recover_g;
12884
+
12885
+/*
12886
+** Use this static SQLite mutex to protect the globals during the
12887
+** first call to sqlite3_recover_step().
12888
+*/
12889
+#define RECOVER_MUTEX_ID SQLITE_MUTEX_STATIC_APP2
12890
+
12891
+
12892
+/*
12893
+** Default value for SQLITE_RECOVER_ROWIDS (sqlite3_recover.bRecoverRowid).
12894
+*/
12895
+#define RECOVER_ROWID_DEFAULT 1
12896
+
12897
+/*
12898
+** Mutex handling:
12899
+**
12900
+** recoverEnterMutex() - Enter the recovery mutex
12901
+** recoverLeaveMutex() - Leave the recovery mutex
12902
+** recoverAssertMutexHeld() - Assert that the recovery mutex is held
12903
+*/
12904
+#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE==0
12905
+# define recoverEnterMutex()
12906
+# define recoverLeaveMutex()
12907
+#else
12908
+static void recoverEnterMutex(void){
12909
+ sqlite3_mutex_enter(sqlite3_mutex_alloc(RECOVER_MUTEX_ID));
12910
+}
12911
+static void recoverLeaveMutex(void){
12912
+ sqlite3_mutex_leave(sqlite3_mutex_alloc(RECOVER_MUTEX_ID));
12913
+}
12914
+#endif
12915
+#if SQLITE_THREADSAFE+0>=1 && defined(SQLITE_DEBUG)
12916
+static void recoverAssertMutexHeld(void){
12917
+ assert( sqlite3_mutex_held(sqlite3_mutex_alloc(RECOVER_MUTEX_ID)) );
12918
+}
12919
+#else
12920
+# define recoverAssertMutexHeld()
12921
+#endif
12922
+
12923
+
12924
+/*
12925
+** Like strlen(). But handles NULL pointer arguments.
12926
+*/
12927
+static int recoverStrlen(const char *zStr){
12928
+ if( zStr==0 ) return 0;
12929
+ return (int)(strlen(zStr)&0x7fffffff);
12930
+}
12931
+
12932
+/*
12933
+** This function is a no-op if the recover handle passed as the first
12934
+** argument already contains an error (if p->errCode!=SQLITE_OK).
12935
+**
12936
+** Otherwise, an attempt is made to allocate, zero and return a buffer nByte
12937
+** bytes in size. If successful, a pointer to the new buffer is returned. Or,
12938
+** if an OOM error occurs, NULL is returned and the handle error code
12939
+** (p->errCode) set to SQLITE_NOMEM.
12940
+*/
12941
+static void *recoverMalloc(sqlite3_recover *p, i64 nByte){
12942
+ void *pRet = 0;
12943
+ assert( nByte>0 );
12944
+ if( p->errCode==SQLITE_OK ){
12945
+ pRet = sqlite3_malloc64(nByte);
12946
+ if( pRet ){
12947
+ memset(pRet, 0, nByte);
12948
+ }else{
12949
+ p->errCode = SQLITE_NOMEM;
12950
+ }
12951
+ }
12952
+ return pRet;
12953
+}
12954
+
12955
+/*
12956
+** Set the error code and error message for the recover handle passed as
12957
+** the first argument. The error code is set to the value of parameter
12958
+** errCode.
12959
+**
12960
+** Parameter zFmt must be a printf() style formatting string. The handle
12961
+** error message is set to the result of using any trailing arguments for
12962
+** parameter substitutions in the formatting string.
12963
+**
12964
+** For example:
12965
+**
12966
+** recoverError(p, SQLITE_ERROR, "no such table: %s", zTablename);
12967
+*/
12968
+static int recoverError(
12969
+ sqlite3_recover *p,
12970
+ int errCode,
12971
+ const char *zFmt, ...
12972
+){
12973
+ char *z = 0;
12974
+ va_list ap;
12975
+ va_start(ap, zFmt);
12976
+ if( zFmt ){
12977
+ z = sqlite3_vmprintf(zFmt, ap);
12978
+ va_end(ap);
12979
+ }
12980
+ sqlite3_free(p->zErrMsg);
12981
+ p->zErrMsg = z;
12982
+ p->errCode = errCode;
12983
+ return errCode;
12984
+}
12985
+
12986
+
12987
+/*
12988
+** This function is a no-op if p->errCode is initially other than SQLITE_OK.
12989
+** In this case it returns NULL.
12990
+**
12991
+** Otherwise, an attempt is made to allocate and return a bitmap object
12992
+** large enough to store a bit for all page numbers between 1 and nPg,
12993
+** inclusive. The bitmap is initially zeroed.
12994
+*/
12995
+static RecoverBitmap *recoverBitmapAlloc(sqlite3_recover *p, i64 nPg){
12996
+ int nElem = (nPg+1+31) / 32;
12997
+ int nByte = sizeof(RecoverBitmap) + nElem*sizeof(u32);
12998
+ RecoverBitmap *pRet = (RecoverBitmap*)recoverMalloc(p, nByte);
12999
+
13000
+ if( pRet ){
13001
+ pRet->nPg = nPg;
13002
+ }
13003
+ return pRet;
13004
+}
13005
+
13006
+/*
13007
+** Free a bitmap object allocated by recoverBitmapAlloc().
13008
+*/
13009
+static void recoverBitmapFree(RecoverBitmap *pMap){
13010
+ sqlite3_free(pMap);
13011
+}
13012
+
13013
+/*
13014
+** Set the bit associated with page iPg in bitvec pMap.
13015
+*/
13016
+static void recoverBitmapSet(RecoverBitmap *pMap, i64 iPg){
13017
+ if( iPg<=pMap->nPg ){
13018
+ int iElem = (iPg / 32);
13019
+ int iBit = (iPg % 32);
13020
+ pMap->aElem[iElem] |= (((u32)1) << iBit);
13021
+ }
13022
+}
13023
+
13024
+/*
13025
+** Query bitmap object pMap for the state of the bit associated with page
13026
+** iPg. Return 1 if it is set, or 0 otherwise.
13027
+*/
13028
+static int recoverBitmapQuery(RecoverBitmap *pMap, i64 iPg){
13029
+ int ret = 1;
13030
+ if( iPg<=pMap->nPg && iPg>0 ){
13031
+ int iElem = (iPg / 32);
13032
+ int iBit = (iPg % 32);
13033
+ ret = (pMap->aElem[iElem] & (((u32)1) << iBit)) ? 1 : 0;
13034
+ }
13035
+ return ret;
13036
+}
13037
+
13038
+/*
13039
+** Set the recover handle error to the error code and message returned by
13040
+** calling sqlite3_errcode() and sqlite3_errmsg(), respectively, on database
13041
+** handle db.
13042
+*/
13043
+static int recoverDbError(sqlite3_recover *p, sqlite3 *db){
13044
+ return recoverError(p, sqlite3_errcode(db), "%s", sqlite3_errmsg(db));
13045
+}
13046
+
13047
+/*
13048
+** This function is a no-op if recover handle p already contains an error
13049
+** (if p->errCode!=SQLITE_OK).
13050
+**
13051
+** Otherwise, it attempts to prepare the SQL statement in zSql against
13052
+** database handle db. If successful, the statement handle is returned.
13053
+** Or, if an error occurs, NULL is returned and an error left in the
13054
+** recover handle.
13055
+*/
13056
+static sqlite3_stmt *recoverPrepare(
13057
+ sqlite3_recover *p,
13058
+ sqlite3 *db,
13059
+ const char *zSql
13060
+){
13061
+ sqlite3_stmt *pStmt = 0;
13062
+ if( p->errCode==SQLITE_OK ){
13063
+ if( sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0) ){
13064
+ recoverDbError(p, db);
13065
+ }
13066
+ }
13067
+ return pStmt;
13068
+}
13069
+
13070
+/*
13071
+** This function is a no-op if recover handle p already contains an error
13072
+** (if p->errCode!=SQLITE_OK).
13073
+**
13074
+** Otherwise, argument zFmt is used as a printf() style format string,
13075
+** along with any trailing arguments, to create an SQL statement. This
13076
+** SQL statement is prepared against database handle db and, if successful,
13077
+** the statment handle returned. Or, if an error occurs - either during
13078
+** the printf() formatting or when preparing the resulting SQL - an
13079
+** error code and message are left in the recover handle.
13080
+*/
13081
+static sqlite3_stmt *recoverPreparePrintf(
13082
+ sqlite3_recover *p,
13083
+ sqlite3 *db,
13084
+ const char *zFmt, ...
13085
+){
13086
+ sqlite3_stmt *pStmt = 0;
13087
+ if( p->errCode==SQLITE_OK ){
13088
+ va_list ap;
13089
+ char *z;
13090
+ va_start(ap, zFmt);
13091
+ z = sqlite3_vmprintf(zFmt, ap);
13092
+ va_end(ap);
13093
+ if( z==0 ){
13094
+ p->errCode = SQLITE_NOMEM;
13095
+ }else{
13096
+ pStmt = recoverPrepare(p, db, z);
13097
+ sqlite3_free(z);
13098
+ }
13099
+ }
13100
+ return pStmt;
13101
+}
13102
+
13103
+/*
13104
+** Reset SQLite statement handle pStmt. If the call to sqlite3_reset()
13105
+** indicates that an error occurred, and there is not already an error
13106
+** in the recover handle passed as the first argument, set the error
13107
+** code and error message appropriately.
13108
+**
13109
+** This function returns a copy of the statement handle pointer passed
13110
+** as the second argument.
13111
+*/
13112
+static sqlite3_stmt *recoverReset(sqlite3_recover *p, sqlite3_stmt *pStmt){
13113
+ int rc = sqlite3_reset(pStmt);
13114
+ if( rc!=SQLITE_OK && rc!=SQLITE_CONSTRAINT && p->errCode==SQLITE_OK ){
13115
+ recoverDbError(p, sqlite3_db_handle(pStmt));
13116
+ }
13117
+ return pStmt;
13118
+}
13119
+
13120
+/*
13121
+** Finalize SQLite statement handle pStmt. If the call to sqlite3_reset()
13122
+** indicates that an error occurred, and there is not already an error
13123
+** in the recover handle passed as the first argument, set the error
13124
+** code and error message appropriately.
13125
+*/
13126
+static void recoverFinalize(sqlite3_recover *p, sqlite3_stmt *pStmt){
13127
+ sqlite3 *db = sqlite3_db_handle(pStmt);
13128
+ int rc = sqlite3_finalize(pStmt);
13129
+ if( rc!=SQLITE_OK && p->errCode==SQLITE_OK ){
13130
+ recoverDbError(p, db);
13131
+ }
13132
+}
13133
+
13134
+/*
13135
+** This function is a no-op if recover handle p already contains an error
13136
+** (if p->errCode!=SQLITE_OK). A copy of p->errCode is returned in this
13137
+** case.
13138
+**
13139
+** Otherwise, execute SQL script zSql. If successful, return SQLITE_OK.
13140
+** Or, if an error occurs, leave an error code and message in the recover
13141
+** handle and return a copy of the error code.
13142
+*/
13143
+static int recoverExec(sqlite3_recover *p, sqlite3 *db, const char *zSql){
13144
+ if( p->errCode==SQLITE_OK ){
13145
+ int rc = sqlite3_exec(db, zSql, 0, 0, 0);
13146
+ if( rc ){
13147
+ recoverDbError(p, db);
13148
+ }
13149
+ }
13150
+ return p->errCode;
13151
+}
13152
+
13153
+/*
13154
+** Bind the value pVal to parameter iBind of statement pStmt. Leave an
13155
+** error in the recover handle passed as the first argument if an error
13156
+** (e.g. an OOM) occurs.
13157
+*/
13158
+static void recoverBindValue(
13159
+ sqlite3_recover *p,
13160
+ sqlite3_stmt *pStmt,
13161
+ int iBind,
13162
+ sqlite3_value *pVal
13163
+){
13164
+ if( p->errCode==SQLITE_OK ){
13165
+ int rc = sqlite3_bind_value(pStmt, iBind, pVal);
13166
+ if( rc ) recoverError(p, rc, 0);
13167
+ }
13168
+}
13169
+
13170
+/*
13171
+** This function is a no-op if recover handle p already contains an error
13172
+** (if p->errCode!=SQLITE_OK). NULL is returned in this case.
13173
+**
13174
+** Otherwise, an attempt is made to interpret zFmt as a printf() style
13175
+** formatting string and the result of using the trailing arguments for
13176
+** parameter substitution with it written into a buffer obtained from
13177
+** sqlite3_malloc(). If successful, a pointer to the buffer is returned.
13178
+** It is the responsibility of the caller to eventually free the buffer
13179
+** using sqlite3_free().
13180
+**
13181
+** Or, if an error occurs, an error code and message is left in the recover
13182
+** handle and NULL returned.
13183
+*/
13184
+static char *recoverMPrintf(sqlite3_recover *p, const char *zFmt, ...){
13185
+ va_list ap;
13186
+ char *z;
13187
+ va_start(ap, zFmt);
13188
+ z = sqlite3_vmprintf(zFmt, ap);
13189
+ va_end(ap);
13190
+ if( p->errCode==SQLITE_OK ){
13191
+ if( z==0 ) p->errCode = SQLITE_NOMEM;
13192
+ }else{
13193
+ sqlite3_free(z);
13194
+ z = 0;
13195
+ }
13196
+ return z;
13197
+}
13198
+
13199
+/*
13200
+** This function is a no-op if recover handle p already contains an error
13201
+** (if p->errCode!=SQLITE_OK). Zero is returned in this case.
13202
+**
13203
+** Otherwise, execute "PRAGMA page_count" against the input database. If
13204
+** successful, return the integer result. Or, if an error occurs, leave an
13205
+** error code and error message in the sqlite3_recover handle and return
13206
+** zero.
13207
+*/
13208
+static i64 recoverPageCount(sqlite3_recover *p){
13209
+ i64 nPg = 0;
13210
+ if( p->errCode==SQLITE_OK ){
13211
+ sqlite3_stmt *pStmt = 0;
13212
+ pStmt = recoverPreparePrintf(p, p->dbIn, "PRAGMA %Q.page_count", p->zDb);
13213
+ if( pStmt ){
13214
+ sqlite3_step(pStmt);
13215
+ nPg = sqlite3_column_int64(pStmt, 0);
13216
+ }
13217
+ recoverFinalize(p, pStmt);
13218
+ }
13219
+ return nPg;
13220
+}
13221
+
13222
+/*
13223
+** Implementation of SQL scalar function "read_i32". The first argument to
13224
+** this function must be a blob. The second a non-negative integer. This
13225
+** function reads and returns a 32-bit big-endian integer from byte
13226
+** offset (4*<arg2>) of the blob.
13227
+**
13228
+** SELECT read_i32(<blob>, <idx>)
13229
+*/
13230
+static void recoverReadI32(
13231
+ sqlite3_context *context,
13232
+ int argc,
13233
+ sqlite3_value **argv
13234
+){
13235
+ const unsigned char *pBlob;
13236
+ int nBlob;
13237
+ int iInt;
13238
+
13239
+ assert( argc==2 );
13240
+ nBlob = sqlite3_value_bytes(argv[0]);
13241
+ pBlob = (const unsigned char*)sqlite3_value_blob(argv[0]);
13242
+ iInt = sqlite3_value_int(argv[1]) & 0xFFFF;
13243
+
13244
+ if( (iInt+1)*4<=nBlob ){
13245
+ const unsigned char *a = &pBlob[iInt*4];
13246
+ i64 iVal = ((i64)a[0]<<24)
13247
+ + ((i64)a[1]<<16)
13248
+ + ((i64)a[2]<< 8)
13249
+ + ((i64)a[3]<< 0);
13250
+ sqlite3_result_int64(context, iVal);
13251
+ }
13252
+}
13253
+
13254
+/*
13255
+** Implementation of SQL scalar function "page_is_used". This function
13256
+** is used as part of the procedure for locating orphan rows for the
13257
+** lost-and-found table, and it depends on those routines having populated
13258
+** the sqlite3_recover.laf.pUsed variable.
13259
+**
13260
+** The only argument to this function is a page-number. It returns true
13261
+** if the page has already been used somehow during data recovery, or false
13262
+** otherwise.
13263
+**
13264
+** SELECT page_is_used(<pgno>);
13265
+*/
13266
+static void recoverPageIsUsed(
13267
+ sqlite3_context *pCtx,
13268
+ int nArg,
13269
+ sqlite3_value **apArg
13270
+){
13271
+ sqlite3_recover *p = (sqlite3_recover*)sqlite3_user_data(pCtx);
13272
+ i64 pgno = sqlite3_value_int64(apArg[0]);
13273
+ assert( nArg==1 );
13274
+ sqlite3_result_int(pCtx, recoverBitmapQuery(p->laf.pUsed, pgno));
13275
+}
13276
+
13277
+/*
13278
+** The implementation of a user-defined SQL function invoked by the
13279
+** sqlite_dbdata and sqlite_dbptr virtual table modules to access pages
13280
+** of the database being recovered.
13281
+**
13282
+** This function always takes a single integer argument. If the argument
13283
+** is zero, then the value returned is the number of pages in the db being
13284
+** recovered. If the argument is greater than zero, it is a page number.
13285
+** The value returned in this case is an SQL blob containing the data for
13286
+** the identified page of the db being recovered. e.g.
13287
+**
13288
+** SELECT getpage(0); -- return number of pages in db
13289
+** SELECT getpage(4); -- return page 4 of db as a blob of data
13290
+*/
13291
+static void recoverGetPage(
13292
+ sqlite3_context *pCtx,
13293
+ int nArg,
13294
+ sqlite3_value **apArg
13295
+){
13296
+ sqlite3_recover *p = (sqlite3_recover*)sqlite3_user_data(pCtx);
13297
+ i64 pgno = sqlite3_value_int64(apArg[0]);
13298
+ sqlite3_stmt *pStmt = 0;
13299
+
13300
+ assert( nArg==1 );
13301
+ if( pgno==0 ){
13302
+ i64 nPg = recoverPageCount(p);
13303
+ sqlite3_result_int64(pCtx, nPg);
13304
+ return;
13305
+ }else{
13306
+ if( p->pGetPage==0 ){
13307
+ pStmt = p->pGetPage = recoverPreparePrintf(
13308
+ p, p->dbIn, "SELECT data FROM sqlite_dbpage(%Q) WHERE pgno=?", p->zDb
13309
+ );
13310
+ }else if( p->errCode==SQLITE_OK ){
13311
+ pStmt = p->pGetPage;
13312
+ }
13313
+
13314
+ if( pStmt ){
13315
+ sqlite3_bind_int64(pStmt, 1, pgno);
13316
+ if( SQLITE_ROW==sqlite3_step(pStmt) ){
13317
+ const u8 *aPg;
13318
+ int nPg;
13319
+ assert( p->errCode==SQLITE_OK );
13320
+ aPg = sqlite3_column_blob(pStmt, 0);
13321
+ nPg = sqlite3_column_bytes(pStmt, 0);
13322
+ if( pgno==1 && nPg==p->pgsz && 0==memcmp(p->pPage1Cache, aPg, nPg) ){
13323
+ aPg = p->pPage1Disk;
13324
+ }
13325
+ sqlite3_result_blob(pCtx, aPg, nPg-p->nReserve, SQLITE_TRANSIENT);
13326
+ }
13327
+ recoverReset(p, pStmt);
13328
+ }
13329
+ }
13330
+
13331
+ if( p->errCode ){
13332
+ if( p->zErrMsg ) sqlite3_result_error(pCtx, p->zErrMsg, -1);
13333
+ sqlite3_result_error_code(pCtx, p->errCode);
13334
+ }
13335
+}
13336
+
13337
+/*
13338
+** Find a string that is not found anywhere in z[]. Return a pointer
13339
+** to that string.
13340
+**
13341
+** Try to use zA and zB first. If both of those are already found in z[]
13342
+** then make up some string and store it in the buffer zBuf.
13343
+*/
13344
+static const char *recoverUnusedString(
13345
+ const char *z, /* Result must not appear anywhere in z */
13346
+ const char *zA, const char *zB, /* Try these first */
13347
+ char *zBuf /* Space to store a generated string */
13348
+){
13349
+ unsigned i = 0;
13350
+ if( strstr(z, zA)==0 ) return zA;
13351
+ if( strstr(z, zB)==0 ) return zB;
13352
+ do{
13353
+ sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
13354
+ }while( strstr(z,zBuf)!=0 );
13355
+ return zBuf;
13356
+}
13357
+
13358
+/*
13359
+** Implementation of scalar SQL function "escape_crnl". The argument passed to
13360
+** this function is the output of built-in function quote(). If the first
13361
+** character of the input is "'", indicating that the value passed to quote()
13362
+** was a text value, then this function searches the input for "\n" and "\r"
13363
+** characters and adds a wrapper similar to the following:
13364
+**
13365
+** replace(replace(<input>, '\n', char(10), '\r', char(13));
13366
+**
13367
+** Or, if the first character of the input is not "'", then a copy of the input
13368
+** is returned.
13369
+*/
13370
+static void recoverEscapeCrnl(
13371
+ sqlite3_context *context,
13372
+ int argc,
13373
+ sqlite3_value **argv
13374
+){
13375
+ const char *zText = (const char*)sqlite3_value_text(argv[0]);
13376
+ if( zText && zText[0]=='\'' ){
13377
+ int nText = sqlite3_value_bytes(argv[0]);
13378
+ int i;
13379
+ char zBuf1[20];
13380
+ char zBuf2[20];
13381
+ const char *zNL = 0;
13382
+ const char *zCR = 0;
13383
+ int nCR = 0;
13384
+ int nNL = 0;
13385
+
13386
+ for(i=0; zText[i]; i++){
13387
+ if( zNL==0 && zText[i]=='\n' ){
13388
+ zNL = recoverUnusedString(zText, "\\n", "\\012", zBuf1);
13389
+ nNL = (int)strlen(zNL);
13390
+ }
13391
+ if( zCR==0 && zText[i]=='\r' ){
13392
+ zCR = recoverUnusedString(zText, "\\r", "\\015", zBuf2);
13393
+ nCR = (int)strlen(zCR);
13394
+ }
13395
+ }
13396
+
13397
+ if( zNL || zCR ){
13398
+ int iOut = 0;
13399
+ i64 nMax = (nNL > nCR) ? nNL : nCR;
13400
+ i64 nAlloc = nMax * nText + (nMax+64)*2;
13401
+ char *zOut = (char*)sqlite3_malloc64(nAlloc);
13402
+ if( zOut==0 ){
13403
+ sqlite3_result_error_nomem(context);
13404
+ return;
13405
+ }
13406
+
13407
+ if( zNL && zCR ){
13408
+ memcpy(&zOut[iOut], "replace(replace(", 16);
13409
+ iOut += 16;
13410
+ }else{
13411
+ memcpy(&zOut[iOut], "replace(", 8);
13412
+ iOut += 8;
13413
+ }
13414
+ for(i=0; zText[i]; i++){
13415
+ if( zText[i]=='\n' ){
13416
+ memcpy(&zOut[iOut], zNL, nNL);
13417
+ iOut += nNL;
13418
+ }else if( zText[i]=='\r' ){
13419
+ memcpy(&zOut[iOut], zCR, nCR);
13420
+ iOut += nCR;
13421
+ }else{
13422
+ zOut[iOut] = zText[i];
13423
+ iOut++;
13424
+ }
13425
+ }
13426
+
13427
+ if( zNL ){
13428
+ memcpy(&zOut[iOut], ",'", 2); iOut += 2;
13429
+ memcpy(&zOut[iOut], zNL, nNL); iOut += nNL;
13430
+ memcpy(&zOut[iOut], "', char(10))", 12); iOut += 12;
13431
+ }
13432
+ if( zCR ){
13433
+ memcpy(&zOut[iOut], ",'", 2); iOut += 2;
13434
+ memcpy(&zOut[iOut], zCR, nCR); iOut += nCR;
13435
+ memcpy(&zOut[iOut], "', char(13))", 12); iOut += 12;
13436
+ }
13437
+
13438
+ sqlite3_result_text(context, zOut, iOut, SQLITE_TRANSIENT);
13439
+ sqlite3_free(zOut);
13440
+ return;
13441
+ }
13442
+ }
13443
+
13444
+ sqlite3_result_value(context, argv[0]);
13445
+}
13446
+
13447
+/*
13448
+** This function is a no-op if recover handle p already contains an error
13449
+** (if p->errCode!=SQLITE_OK). A copy of the error code is returned in
13450
+** this case.
13451
+**
13452
+** Otherwise, attempt to populate temporary table "recovery.schema" with the
13453
+** parts of the database schema that can be extracted from the input database.
13454
+**
13455
+** If no error occurs, SQLITE_OK is returned. Otherwise, an error code
13456
+** and error message are left in the recover handle and a copy of the
13457
+** error code returned. It is not considered an error if part of all of
13458
+** the database schema cannot be recovered due to corruption.
13459
+*/
13460
+static int recoverCacheSchema(sqlite3_recover *p){
13461
+ return recoverExec(p, p->dbOut,
13462
+ "WITH RECURSIVE pages(p) AS ("
13463
+ " SELECT 1"
13464
+ " UNION"
13465
+ " SELECT child FROM sqlite_dbptr('getpage()'), pages WHERE pgno=p"
13466
+ ")"
13467
+ "INSERT INTO recovery.schema SELECT"
13468
+ " max(CASE WHEN field=0 THEN value ELSE NULL END),"
13469
+ " max(CASE WHEN field=1 THEN value ELSE NULL END),"
13470
+ " max(CASE WHEN field=2 THEN value ELSE NULL END),"
13471
+ " max(CASE WHEN field=3 THEN value ELSE NULL END),"
13472
+ " max(CASE WHEN field=4 THEN value ELSE NULL END)"
13473
+ "FROM sqlite_dbdata('getpage()') WHERE pgno IN ("
13474
+ " SELECT p FROM pages"
13475
+ ") GROUP BY pgno, cell"
13476
+ );
13477
+}
13478
+
13479
+/*
13480
+** If this recover handle is not in SQL callback mode (i.e. was not created
13481
+** using sqlite3_recover_init_sql()) of if an error has already occurred,
13482
+** this function is a no-op. Otherwise, issue a callback with SQL statement
13483
+** zSql as the parameter.
13484
+**
13485
+** If the callback returns non-zero, set the recover handle error code to
13486
+** the value returned (so that the caller will abandon processing).
13487
+*/
13488
+static void recoverSqlCallback(sqlite3_recover *p, const char *zSql){
13489
+ if( p->errCode==SQLITE_OK && p->xSql ){
13490
+ int res = p->xSql(p->pSqlCtx, zSql);
13491
+ if( res ){
13492
+ recoverError(p, SQLITE_ERROR, "callback returned an error - %d", res);
13493
+ }
13494
+ }
13495
+}
13496
+
13497
+/*
13498
+** Transfer the following settings from the input database to the output
13499
+** database:
13500
+**
13501
+** + page-size,
13502
+** + auto-vacuum settings,
13503
+** + database encoding,
13504
+** + user-version (PRAGMA user_version), and
13505
+** + application-id (PRAGMA application_id), and
13506
+*/
13507
+static void recoverTransferSettings(sqlite3_recover *p){
13508
+ const char *aPragma[] = {
13509
+ "encoding",
13510
+ "page_size",
13511
+ "auto_vacuum",
13512
+ "user_version",
13513
+ "application_id"
13514
+ };
13515
+ int ii;
13516
+
13517
+ /* Truncate the output database to 0 pages in size. This is done by
13518
+ ** opening a new, empty, temp db, then using the backup API to clobber
13519
+ ** any existing output db with a copy of it. */
13520
+ if( p->errCode==SQLITE_OK ){
13521
+ sqlite3 *db2 = 0;
13522
+ int rc = sqlite3_open("", &db2);
13523
+ if( rc!=SQLITE_OK ){
13524
+ recoverDbError(p, db2);
13525
+ return;
13526
+ }
13527
+
13528
+ for(ii=0; ii<sizeof(aPragma)/sizeof(aPragma[0]); ii++){
13529
+ const char *zPrag = aPragma[ii];
13530
+ sqlite3_stmt *p1 = 0;
13531
+ p1 = recoverPreparePrintf(p, p->dbIn, "PRAGMA %Q.%s", p->zDb, zPrag);
13532
+ if( p->errCode==SQLITE_OK && sqlite3_step(p1)==SQLITE_ROW ){
13533
+ const char *zArg = (const char*)sqlite3_column_text(p1, 0);
13534
+ char *z2 = recoverMPrintf(p, "PRAGMA %s = %Q", zPrag, zArg);
13535
+ recoverSqlCallback(p, z2);
13536
+ recoverExec(p, db2, z2);
13537
+ sqlite3_free(z2);
13538
+ if( zArg==0 ){
13539
+ recoverError(p, SQLITE_NOMEM, 0);
13540
+ }
13541
+ }
13542
+ recoverFinalize(p, p1);
13543
+ }
13544
+ recoverExec(p, db2, "CREATE TABLE t1(a); DROP TABLE t1;");
13545
+
13546
+ if( p->errCode==SQLITE_OK ){
13547
+ sqlite3 *db = p->dbOut;
13548
+ sqlite3_backup *pBackup = sqlite3_backup_init(db, "main", db2, "main");
13549
+ if( pBackup ){
13550
+ sqlite3_backup_step(pBackup, -1);
13551
+ p->errCode = sqlite3_backup_finish(pBackup);
13552
+ }else{
13553
+ recoverDbError(p, db);
13554
+ }
13555
+ }
13556
+
13557
+ sqlite3_close(db2);
13558
+ }
13559
+}
13560
+
13561
+/*
13562
+** This function is a no-op if recover handle p already contains an error
13563
+** (if p->errCode!=SQLITE_OK). A copy of the error code is returned in
13564
+** this case.
13565
+**
13566
+** Otherwise, an attempt is made to open the output database, attach
13567
+** and create the schema of the temporary database used to store
13568
+** intermediate data, and to register all required user functions and
13569
+** virtual table modules with the output handle.
13570
+**
13571
+** If no error occurs, SQLITE_OK is returned. Otherwise, an error code
13572
+** and error message are left in the recover handle and a copy of the
13573
+** error code returned.
13574
+*/
13575
+static int recoverOpenOutput(sqlite3_recover *p){
13576
+ struct Func {
13577
+ const char *zName;
13578
+ int nArg;
13579
+ void (*xFunc)(sqlite3_context*,int,sqlite3_value **);
13580
+ } aFunc[] = {
13581
+ { "getpage", 1, recoverGetPage },
13582
+ { "page_is_used", 1, recoverPageIsUsed },
13583
+ { "read_i32", 2, recoverReadI32 },
13584
+ { "escape_crnl", 1, recoverEscapeCrnl },
13585
+ };
13586
+
13587
+ const int flags = SQLITE_OPEN_URI|SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE;
13588
+ sqlite3 *db = 0; /* New database handle */
13589
+ int ii; /* For iterating through aFunc[] */
13590
+
13591
+ assert( p->dbOut==0 );
13592
+
13593
+ if( sqlite3_open_v2(p->zUri, &db, flags, 0) ){
13594
+ recoverDbError(p, db);
13595
+ }
13596
+
13597
+ /* Register the sqlite_dbdata and sqlite_dbptr virtual table modules.
13598
+ ** These two are registered with the output database handle - this
13599
+ ** module depends on the input handle supporting the sqlite_dbpage
13600
+ ** virtual table only. */
13601
+ if( p->errCode==SQLITE_OK ){
13602
+ p->errCode = sqlite3_dbdata_init(db, 0, 0);
13603
+ }
13604
+
13605
+ /* Register the custom user-functions with the output handle. */
13606
+ for(ii=0; p->errCode==SQLITE_OK && ii<sizeof(aFunc)/sizeof(aFunc[0]); ii++){
13607
+ p->errCode = sqlite3_create_function(db, aFunc[ii].zName,
13608
+ aFunc[ii].nArg, SQLITE_UTF8, (void*)p, aFunc[ii].xFunc, 0, 0
13609
+ );
13610
+ }
13611
+
13612
+ p->dbOut = db;
13613
+ return p->errCode;
13614
+}
13615
+
13616
+/*
13617
+** Attach the auxiliary database 'recovery' to the output database handle.
13618
+** This temporary database is used during the recovery process and then
13619
+** discarded.
13620
+*/
13621
+static void recoverOpenRecovery(sqlite3_recover *p){
13622
+ char *zSql = recoverMPrintf(p, "ATTACH %Q AS recovery;", p->zStateDb);
13623
+ recoverExec(p, p->dbOut, zSql);
13624
+ recoverExec(p, p->dbOut,
13625
+ "PRAGMA writable_schema = 1;"
13626
+ "CREATE TABLE recovery.map(pgno INTEGER PRIMARY KEY, parent INT);"
13627
+ "CREATE TABLE recovery.schema(type, name, tbl_name, rootpage, sql);"
13628
+ );
13629
+ sqlite3_free(zSql);
13630
+}
13631
+
13632
+
13633
+/*
13634
+** This function is a no-op if recover handle p already contains an error
13635
+** (if p->errCode!=SQLITE_OK).
13636
+**
13637
+** Otherwise, argument zName must be the name of a table that has just been
13638
+** created in the output database. This function queries the output db
13639
+** for the schema of said table, and creates a RecoverTable object to
13640
+** store the schema in memory. The new RecoverTable object is linked into
13641
+** the list at sqlite3_recover.pTblList.
13642
+**
13643
+** Parameter iRoot must be the root page of table zName in the INPUT
13644
+** database.
13645
+*/
13646
+static void recoverAddTable(
13647
+ sqlite3_recover *p,
13648
+ const char *zName, /* Name of table created in output db */
13649
+ i64 iRoot /* Root page of same table in INPUT db */
13650
+){
13651
+ sqlite3_stmt *pStmt = recoverPreparePrintf(p, p->dbOut,
13652
+ "PRAGMA table_xinfo(%Q)", zName
13653
+ );
13654
+
13655
+ if( pStmt ){
13656
+ int iPk = -1;
13657
+ int iBind = 1;
13658
+ RecoverTable *pNew = 0;
13659
+ int nCol = 0;
13660
+ int nName = recoverStrlen(zName);
13661
+ int nByte = 0;
13662
+ while( sqlite3_step(pStmt)==SQLITE_ROW ){
13663
+ nCol++;
13664
+ nByte += (sqlite3_column_bytes(pStmt, 1)+1);
13665
+ }
13666
+ nByte += sizeof(RecoverTable) + nCol*sizeof(RecoverColumn) + nName+1;
13667
+ recoverReset(p, pStmt);
13668
+
13669
+ pNew = recoverMalloc(p, nByte);
13670
+ if( pNew ){
13671
+ int i = 0;
13672
+ int iField = 0;
13673
+ char *csr = 0;
13674
+ pNew->aCol = (RecoverColumn*)&pNew[1];
13675
+ pNew->zTab = csr = (char*)&pNew->aCol[nCol];
13676
+ pNew->nCol = nCol;
13677
+ pNew->iRoot = iRoot;
13678
+ memcpy(csr, zName, nName);
13679
+ csr += nName+1;
13680
+
13681
+ for(i=0; sqlite3_step(pStmt)==SQLITE_ROW; i++){
13682
+ int iPKF = sqlite3_column_int(pStmt, 5);
13683
+ int n = sqlite3_column_bytes(pStmt, 1);
13684
+ const char *z = (const char*)sqlite3_column_text(pStmt, 1);
13685
+ const char *zType = (const char*)sqlite3_column_text(pStmt, 2);
13686
+ int eHidden = sqlite3_column_int(pStmt, 6);
13687
+
13688
+ if( iPk==-1 && iPKF==1 && !sqlite3_stricmp("integer", zType) ) iPk = i;
13689
+ if( iPKF>1 ) iPk = -2;
13690
+ pNew->aCol[i].zCol = csr;
13691
+ pNew->aCol[i].eHidden = eHidden;
13692
+ if( eHidden==RECOVER_EHIDDEN_VIRTUAL ){
13693
+ pNew->aCol[i].iField = -1;
13694
+ }else{
13695
+ pNew->aCol[i].iField = iField++;
13696
+ }
13697
+ if( eHidden!=RECOVER_EHIDDEN_VIRTUAL
13698
+ && eHidden!=RECOVER_EHIDDEN_STORED
13699
+ ){
13700
+ pNew->aCol[i].iBind = iBind++;
13701
+ }
13702
+ memcpy(csr, z, n);
13703
+ csr += (n+1);
13704
+ }
13705
+
13706
+ pNew->pNext = p->pTblList;
13707
+ p->pTblList = pNew;
13708
+ pNew->bIntkey = 1;
13709
+ }
13710
+
13711
+ recoverFinalize(p, pStmt);
13712
+
13713
+ pStmt = recoverPreparePrintf(p, p->dbOut, "PRAGMA index_xinfo(%Q)", zName);
13714
+ while( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
13715
+ int iField = sqlite3_column_int(pStmt, 0);
13716
+ int iCol = sqlite3_column_int(pStmt, 1);
13717
+
13718
+ assert( iField<pNew->nCol && iCol<pNew->nCol );
13719
+ pNew->aCol[iCol].iField = iField;
13720
+
13721
+ pNew->bIntkey = 0;
13722
+ iPk = -2;
13723
+ }
13724
+ recoverFinalize(p, pStmt);
13725
+
13726
+ if( p->errCode==SQLITE_OK ){
13727
+ if( iPk>=0 ){
13728
+ pNew->aCol[iPk].bIPK = 1;
13729
+ }else if( pNew->bIntkey ){
13730
+ pNew->iRowidBind = iBind++;
13731
+ }
13732
+ }
13733
+ }
13734
+}
13735
+
13736
+/*
13737
+** This function is called after recoverCacheSchema() has cached those parts
13738
+** of the input database schema that could be recovered in temporary table
13739
+** "recovery.schema". This function creates in the output database copies
13740
+** of all parts of that schema that must be created before the tables can
13741
+** be populated. Specifically, this means:
13742
+**
13743
+** * all tables that are not VIRTUAL, and
13744
+** * UNIQUE indexes.
13745
+**
13746
+** If the recovery handle uses SQL callbacks, then callbacks containing
13747
+** the associated "CREATE TABLE" and "CREATE INDEX" statements are made.
13748
+**
13749
+** Additionally, records are added to the sqlite_schema table of the
13750
+** output database for any VIRTUAL tables. The CREATE VIRTUAL TABLE
13751
+** records are written directly to sqlite_schema, not actually executed.
13752
+** If the handle is in SQL callback mode, then callbacks are invoked
13753
+** with equivalent SQL statements.
13754
+*/
13755
+static int recoverWriteSchema1(sqlite3_recover *p){
13756
+ sqlite3_stmt *pSelect = 0;
13757
+ sqlite3_stmt *pTblname = 0;
13758
+
13759
+ pSelect = recoverPrepare(p, p->dbOut,
13760
+ "WITH dbschema(rootpage, name, sql, tbl, isVirtual, isIndex) AS ("
13761
+ " SELECT rootpage, name, sql, "
13762
+ " type='table', "
13763
+ " sql LIKE 'create virtual%',"
13764
+ " (type='index' AND (sql LIKE '%unique%' OR ?1))"
13765
+ " FROM recovery.schema"
13766
+ ")"
13767
+ "SELECT rootpage, tbl, isVirtual, name, sql"
13768
+ " FROM dbschema "
13769
+ " WHERE tbl OR isIndex"
13770
+ " ORDER BY tbl DESC, name=='sqlite_sequence' DESC"
13771
+ );
13772
+
13773
+ pTblname = recoverPrepare(p, p->dbOut,
13774
+ "SELECT name FROM sqlite_schema "
13775
+ "WHERE type='table' ORDER BY rowid DESC LIMIT 1"
13776
+ );
13777
+
13778
+ if( pSelect ){
13779
+ sqlite3_bind_int(pSelect, 1, p->bSlowIndexes);
13780
+ while( sqlite3_step(pSelect)==SQLITE_ROW ){
13781
+ i64 iRoot = sqlite3_column_int64(pSelect, 0);
13782
+ int bTable = sqlite3_column_int(pSelect, 1);
13783
+ int bVirtual = sqlite3_column_int(pSelect, 2);
13784
+ const char *zName = (const char*)sqlite3_column_text(pSelect, 3);
13785
+ const char *zSql = (const char*)sqlite3_column_text(pSelect, 4);
13786
+ char *zFree = 0;
13787
+ int rc = SQLITE_OK;
13788
+
13789
+ if( bVirtual ){
13790
+ zSql = (const char*)(zFree = recoverMPrintf(p,
13791
+ "INSERT INTO sqlite_schema VALUES('table', %Q, %Q, 0, %Q)",
13792
+ zName, zName, zSql
13793
+ ));
13794
+ }
13795
+ rc = sqlite3_exec(p->dbOut, zSql, 0, 0, 0);
13796
+ if( rc==SQLITE_OK ){
13797
+ recoverSqlCallback(p, zSql);
13798
+ if( bTable && !bVirtual ){
13799
+ if( SQLITE_ROW==sqlite3_step(pTblname) ){
13800
+ const char *zTbl = (const char*)sqlite3_column_text(pTblname, 0);
13801
+ recoverAddTable(p, zTbl, iRoot);
13802
+ }
13803
+ recoverReset(p, pTblname);
13804
+ }
13805
+ }else if( rc!=SQLITE_ERROR ){
13806
+ recoverDbError(p, p->dbOut);
13807
+ }
13808
+ sqlite3_free(zFree);
13809
+ }
13810
+ }
13811
+ recoverFinalize(p, pSelect);
13812
+ recoverFinalize(p, pTblname);
13813
+
13814
+ return p->errCode;
13815
+}
13816
+
13817
+/*
13818
+** This function is called after the output database has been populated. It
13819
+** adds all recovered schema elements that were not created in the output
13820
+** database by recoverWriteSchema1() - everything except for tables and
13821
+** UNIQUE indexes. Specifically:
13822
+**
13823
+** * views,
13824
+** * triggers,
13825
+** * non-UNIQUE indexes.
13826
+**
13827
+** If the recover handle is in SQL callback mode, then equivalent callbacks
13828
+** are issued to create the schema elements.
13829
+*/
13830
+static int recoverWriteSchema2(sqlite3_recover *p){
13831
+ sqlite3_stmt *pSelect = 0;
13832
+
13833
+ pSelect = recoverPrepare(p, p->dbOut,
13834
+ p->bSlowIndexes ?
13835
+ "SELECT rootpage, sql FROM recovery.schema "
13836
+ " WHERE type!='table' AND type!='index'"
13837
+ :
13838
+ "SELECT rootpage, sql FROM recovery.schema "
13839
+ " WHERE type!='table' AND (type!='index' OR sql NOT LIKE '%unique%')"
13840
+ );
13841
+
13842
+ if( pSelect ){
13843
+ while( sqlite3_step(pSelect)==SQLITE_ROW ){
13844
+ const char *zSql = (const char*)sqlite3_column_text(pSelect, 1);
13845
+ int rc = sqlite3_exec(p->dbOut, zSql, 0, 0, 0);
13846
+ if( rc==SQLITE_OK ){
13847
+ recoverSqlCallback(p, zSql);
13848
+ }else if( rc!=SQLITE_ERROR ){
13849
+ recoverDbError(p, p->dbOut);
13850
+ }
13851
+ }
13852
+ }
13853
+ recoverFinalize(p, pSelect);
13854
+
13855
+ return p->errCode;
13856
+}
13857
+
13858
+/*
13859
+** This function is a no-op if recover handle p already contains an error
13860
+** (if p->errCode!=SQLITE_OK). In this case it returns NULL.
13861
+**
13862
+** Otherwise, if the recover handle is configured to create an output
13863
+** database (was created by sqlite3_recover_init()), then this function
13864
+** prepares and returns an SQL statement to INSERT a new record into table
13865
+** pTab, assuming the first nField fields of a record extracted from disk
13866
+** are valid.
13867
+**
13868
+** For example, if table pTab is:
13869
+**
13870
+** CREATE TABLE name(a, b GENERATED ALWAYS AS (a+1) STORED, c, d, e);
13871
+**
13872
+** And nField is 4, then the SQL statement prepared and returned is:
13873
+**
13874
+** INSERT INTO (a, c, d) VALUES (?1, ?2, ?3);
13875
+**
13876
+** In this case even though 4 values were extracted from the input db,
13877
+** only 3 are written to the output, as the generated STORED column
13878
+** cannot be written.
13879
+**
13880
+** If the recover handle is in SQL callback mode, then the SQL statement
13881
+** prepared is such that evaluating it returns a single row containing
13882
+** a single text value - itself an SQL statement similar to the above,
13883
+** except with SQL literals in place of the variables. For example:
13884
+**
13885
+** SELECT 'INSERT INTO (a, c, d) VALUES ('
13886
+** || quote(?1) || ', '
13887
+** || quote(?2) || ', '
13888
+** || quote(?3) || ')';
13889
+**
13890
+** In either case, it is the responsibility of the caller to eventually
13891
+** free the statement handle using sqlite3_finalize().
13892
+*/
13893
+static sqlite3_stmt *recoverInsertStmt(
13894
+ sqlite3_recover *p,
13895
+ RecoverTable *pTab,
13896
+ int nField
13897
+){
13898
+ sqlite3_stmt *pRet = 0;
13899
+ const char *zSep = "";
13900
+ const char *zSqlSep = "";
13901
+ char *zSql = 0;
13902
+ char *zFinal = 0;
13903
+ char *zBind = 0;
13904
+ int ii;
13905
+ int bSql = p->xSql ? 1 : 0;
13906
+
13907
+ if( nField<=0 ) return 0;
13908
+
13909
+ assert( nField<=pTab->nCol );
13910
+
13911
+ zSql = recoverMPrintf(p, "INSERT OR IGNORE INTO %Q(", pTab->zTab);
13912
+
13913
+ if( pTab->iRowidBind ){
13914
+ assert( pTab->bIntkey );
13915
+ zSql = recoverMPrintf(p, "%z_rowid_", zSql);
13916
+ if( bSql ){
13917
+ zBind = recoverMPrintf(p, "%zquote(?%d)", zBind, pTab->iRowidBind);
13918
+ }else{
13919
+ zBind = recoverMPrintf(p, "%z?%d", zBind, pTab->iRowidBind);
13920
+ }
13921
+ zSqlSep = "||', '||";
13922
+ zSep = ", ";
13923
+ }
13924
+
13925
+ for(ii=0; ii<nField; ii++){
13926
+ int eHidden = pTab->aCol[ii].eHidden;
13927
+ if( eHidden!=RECOVER_EHIDDEN_VIRTUAL
13928
+ && eHidden!=RECOVER_EHIDDEN_STORED
13929
+ ){
13930
+ assert( pTab->aCol[ii].iField>=0 && pTab->aCol[ii].iBind>=1 );
13931
+ zSql = recoverMPrintf(p, "%z%s%Q", zSql, zSep, pTab->aCol[ii].zCol);
13932
+
13933
+ if( bSql ){
13934
+ zBind = recoverMPrintf(p,
13935
+ "%z%sescape_crnl(quote(?%d))", zBind, zSqlSep, pTab->aCol[ii].iBind
13936
+ );
13937
+ zSqlSep = "||', '||";
13938
+ }else{
13939
+ zBind = recoverMPrintf(p, "%z%s?%d", zBind, zSep, pTab->aCol[ii].iBind);
13940
+ }
13941
+ zSep = ", ";
13942
+ }
13943
+ }
13944
+
13945
+ if( bSql ){
13946
+ zFinal = recoverMPrintf(p, "SELECT %Q || ') VALUES (' || %s || ')'",
13947
+ zSql, zBind
13948
+ );
13949
+ }else{
13950
+ zFinal = recoverMPrintf(p, "%s) VALUES (%s)", zSql, zBind);
13951
+ }
13952
+
13953
+ pRet = recoverPrepare(p, p->dbOut, zFinal);
13954
+ sqlite3_free(zSql);
13955
+ sqlite3_free(zBind);
13956
+ sqlite3_free(zFinal);
13957
+
13958
+ return pRet;
13959
+}
13960
+
13961
+
13962
+/*
13963
+** Search the list of RecoverTable objects at p->pTblList for one that
13964
+** has root page iRoot in the input database. If such an object is found,
13965
+** return a pointer to it. Otherwise, return NULL.
13966
+*/
13967
+static RecoverTable *recoverFindTable(sqlite3_recover *p, u32 iRoot){
13968
+ RecoverTable *pRet = 0;
13969
+ for(pRet=p->pTblList; pRet && pRet->iRoot!=iRoot; pRet=pRet->pNext);
13970
+ return pRet;
13971
+}
13972
+
13973
+/*
13974
+** This function attempts to create a lost and found table within the
13975
+** output db. If successful, it returns a pointer to a buffer containing
13976
+** the name of the new table. It is the responsibility of the caller to
13977
+** eventually free this buffer using sqlite3_free().
13978
+**
13979
+** If an error occurs, NULL is returned and an error code and error
13980
+** message left in the recover handle.
13981
+*/
13982
+static char *recoverLostAndFoundCreate(
13983
+ sqlite3_recover *p, /* Recover object */
13984
+ int nField /* Number of column fields in new table */
13985
+){
13986
+ char *zTbl = 0;
13987
+ sqlite3_stmt *pProbe = 0;
13988
+ int ii = 0;
13989
+
13990
+ pProbe = recoverPrepare(p, p->dbOut,
13991
+ "SELECT 1 FROM sqlite_schema WHERE name=?"
13992
+ );
13993
+ for(ii=-1; zTbl==0 && p->errCode==SQLITE_OK && ii<1000; ii++){
13994
+ int bFail = 0;
13995
+ if( ii<0 ){
13996
+ zTbl = recoverMPrintf(p, "%s", p->zLostAndFound);
13997
+ }else{
13998
+ zTbl = recoverMPrintf(p, "%s_%d", p->zLostAndFound, ii);
13999
+ }
14000
+
14001
+ if( p->errCode==SQLITE_OK ){
14002
+ sqlite3_bind_text(pProbe, 1, zTbl, -1, SQLITE_STATIC);
14003
+ if( SQLITE_ROW==sqlite3_step(pProbe) ){
14004
+ bFail = 1;
14005
+ }
14006
+ recoverReset(p, pProbe);
14007
+ }
14008
+
14009
+ if( bFail ){
14010
+ sqlite3_clear_bindings(pProbe);
14011
+ sqlite3_free(zTbl);
14012
+ zTbl = 0;
14013
+ }
14014
+ }
14015
+ recoverFinalize(p, pProbe);
14016
+
14017
+ if( zTbl ){
14018
+ const char *zSep = 0;
14019
+ char *zField = 0;
14020
+ char *zSql = 0;
14021
+
14022
+ zSep = "rootpgno INTEGER, pgno INTEGER, nfield INTEGER, id INTEGER, ";
14023
+ for(ii=0; p->errCode==SQLITE_OK && ii<nField; ii++){
14024
+ zField = recoverMPrintf(p, "%z%sc%d", zField, zSep, ii);
14025
+ zSep = ", ";
14026
+ }
14027
+
14028
+ zSql = recoverMPrintf(p, "CREATE TABLE %s(%s)", zTbl, zField);
14029
+ sqlite3_free(zField);
14030
+
14031
+ recoverExec(p, p->dbOut, zSql);
14032
+ recoverSqlCallback(p, zSql);
14033
+ sqlite3_free(zSql);
14034
+ }else if( p->errCode==SQLITE_OK ){
14035
+ recoverError(
14036
+ p, SQLITE_ERROR, "failed to create %s output table", p->zLostAndFound
14037
+ );
14038
+ }
14039
+
14040
+ return zTbl;
14041
+}
14042
+
14043
+/*
14044
+** Synthesize and prepare an INSERT statement to write to the lost_and_found
14045
+** table in the output database. The name of the table is zTab, and it has
14046
+** nField c* fields.
14047
+*/
14048
+static sqlite3_stmt *recoverLostAndFoundInsert(
14049
+ sqlite3_recover *p,
14050
+ const char *zTab,
14051
+ int nField
14052
+){
14053
+ int nTotal = nField + 4;
14054
+ int ii;
14055
+ char *zBind = 0;
14056
+ sqlite3_stmt *pRet = 0;
14057
+
14058
+ if( p->xSql==0 ){
14059
+ for(ii=0; ii<nTotal; ii++){
14060
+ zBind = recoverMPrintf(p, "%z%s?", zBind, zBind?", ":"", ii);
14061
+ }
14062
+ pRet = recoverPreparePrintf(
14063
+ p, p->dbOut, "INSERT INTO %s VALUES(%s)", zTab, zBind
14064
+ );
14065
+ }else{
14066
+ const char *zSep = "";
14067
+ for(ii=0; ii<nTotal; ii++){
14068
+ zBind = recoverMPrintf(p, "%z%squote(?)", zBind, zSep);
14069
+ zSep = "|| ', ' ||";
14070
+ }
14071
+ pRet = recoverPreparePrintf(
14072
+ p, p->dbOut, "SELECT 'INSERT INTO %s VALUES(' || %s || ')'", zTab, zBind
14073
+ );
14074
+ }
14075
+
14076
+ sqlite3_free(zBind);
14077
+ return pRet;
14078
+}
14079
+
14080
+/*
14081
+** Input database page iPg contains data that will be written to the
14082
+** lost-and-found table of the output database. This function attempts
14083
+** to identify the root page of the tree that page iPg belonged to.
14084
+** If successful, it sets output variable (*piRoot) to the page number
14085
+** of the root page and returns SQLITE_OK. Otherwise, if an error occurs,
14086
+** an SQLite error code is returned and the final value of *piRoot
14087
+** undefined.
14088
+*/
14089
+static int recoverLostAndFoundFindRoot(
14090
+ sqlite3_recover *p,
14091
+ i64 iPg,
14092
+ i64 *piRoot
14093
+){
14094
+ RecoverStateLAF *pLaf = &p->laf;
14095
+
14096
+ if( pLaf->pFindRoot==0 ){
14097
+ pLaf->pFindRoot = recoverPrepare(p, p->dbOut,
14098
+ "WITH RECURSIVE p(pgno) AS ("
14099
+ " SELECT ?"
14100
+ " UNION"
14101
+ " SELECT parent FROM recovery.map AS m, p WHERE m.pgno=p.pgno"
14102
+ ") "
14103
+ "SELECT p.pgno FROM p, recovery.map m WHERE m.pgno=p.pgno "
14104
+ " AND m.parent IS NULL"
14105
+ );
14106
+ }
14107
+ if( p->errCode==SQLITE_OK ){
14108
+ sqlite3_bind_int64(pLaf->pFindRoot, 1, iPg);
14109
+ if( sqlite3_step(pLaf->pFindRoot)==SQLITE_ROW ){
14110
+ *piRoot = sqlite3_column_int64(pLaf->pFindRoot, 0);
14111
+ }else{
14112
+ *piRoot = iPg;
14113
+ }
14114
+ recoverReset(p, pLaf->pFindRoot);
14115
+ }
14116
+ return p->errCode;
14117
+}
14118
+
14119
+/*
14120
+** Recover data from page iPage of the input database and write it to
14121
+** the lost-and-found table in the output database.
14122
+*/
14123
+static void recoverLostAndFoundOnePage(sqlite3_recover *p, i64 iPage){
14124
+ RecoverStateLAF *pLaf = &p->laf;
14125
+ sqlite3_value **apVal = pLaf->apVal;
14126
+ sqlite3_stmt *pPageData = pLaf->pPageData;
14127
+ sqlite3_stmt *pInsert = pLaf->pInsert;
14128
+
14129
+ int nVal = -1;
14130
+ int iPrevCell = 0;
14131
+ i64 iRoot = 0;
14132
+ int bHaveRowid = 0;
14133
+ i64 iRowid = 0;
14134
+ int ii = 0;
14135
+
14136
+ if( recoverLostAndFoundFindRoot(p, iPage, &iRoot) ) return;
14137
+ sqlite3_bind_int64(pPageData, 1, iPage);
14138
+ while( p->errCode==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPageData) ){
14139
+ int iCell = sqlite3_column_int64(pPageData, 0);
14140
+ int iField = sqlite3_column_int64(pPageData, 1);
14141
+
14142
+ if( iPrevCell!=iCell && nVal>=0 ){
14143
+ /* Insert the new row */
14144
+ sqlite3_bind_int64(pInsert, 1, iRoot); /* rootpgno */
14145
+ sqlite3_bind_int64(pInsert, 2, iPage); /* pgno */
14146
+ sqlite3_bind_int(pInsert, 3, nVal); /* nfield */
14147
+ if( bHaveRowid ){
14148
+ sqlite3_bind_int64(pInsert, 4, iRowid); /* id */
14149
+ }
14150
+ for(ii=0; ii<nVal; ii++){
14151
+ recoverBindValue(p, pInsert, 5+ii, apVal[ii]);
14152
+ }
14153
+ if( sqlite3_step(pInsert)==SQLITE_ROW ){
14154
+ recoverSqlCallback(p, (const char*)sqlite3_column_text(pInsert, 0));
14155
+ }
14156
+ recoverReset(p, pInsert);
14157
+
14158
+ /* Discard the accumulated row data */
14159
+ for(ii=0; ii<nVal; ii++){
14160
+ sqlite3_value_free(apVal[ii]);
14161
+ apVal[ii] = 0;
14162
+ }
14163
+ sqlite3_clear_bindings(pInsert);
14164
+ bHaveRowid = 0;
14165
+ nVal = -1;
14166
+ }
14167
+
14168
+ if( iCell<0 ) break;
14169
+
14170
+ if( iField<0 ){
14171
+ assert( nVal==-1 );
14172
+ iRowid = sqlite3_column_int64(pPageData, 2);
14173
+ bHaveRowid = 1;
14174
+ nVal = 0;
14175
+ }else if( iField<pLaf->nMaxField ){
14176
+ sqlite3_value *pVal = sqlite3_column_value(pPageData, 2);
14177
+ apVal[iField] = sqlite3_value_dup(pVal);
14178
+ assert( iField==nVal || (nVal==-1 && iField==0) );
14179
+ nVal = iField+1;
14180
+ if( apVal[iField]==0 ){
14181
+ recoverError(p, SQLITE_NOMEM, 0);
14182
+ }
14183
+ }
14184
+
14185
+ iPrevCell = iCell;
14186
+ }
14187
+ recoverReset(p, pPageData);
14188
+
14189
+ for(ii=0; ii<nVal; ii++){
14190
+ sqlite3_value_free(apVal[ii]);
14191
+ apVal[ii] = 0;
14192
+ }
14193
+}
14194
+
14195
+/*
14196
+** Perform one step (sqlite3_recover_step()) of work for the connection
14197
+** passed as the only argument, which is guaranteed to be in
14198
+** RECOVER_STATE_LOSTANDFOUND3 state - during which the lost-and-found
14199
+** table of the output database is populated with recovered data that can
14200
+** not be assigned to any recovered schema object.
14201
+*/
14202
+static int recoverLostAndFound3Step(sqlite3_recover *p){
14203
+ RecoverStateLAF *pLaf = &p->laf;
14204
+ if( p->errCode==SQLITE_OK ){
14205
+ if( pLaf->pInsert==0 ){
14206
+ return SQLITE_DONE;
14207
+ }else{
14208
+ if( p->errCode==SQLITE_OK ){
14209
+ int res = sqlite3_step(pLaf->pAllPage);
14210
+ if( res==SQLITE_ROW ){
14211
+ i64 iPage = sqlite3_column_int64(pLaf->pAllPage, 0);
14212
+ if( recoverBitmapQuery(pLaf->pUsed, iPage)==0 ){
14213
+ recoverLostAndFoundOnePage(p, iPage);
14214
+ }
14215
+ }else{
14216
+ recoverReset(p, pLaf->pAllPage);
14217
+ return SQLITE_DONE;
14218
+ }
14219
+ }
14220
+ }
14221
+ }
14222
+ return SQLITE_OK;
14223
+}
14224
+
14225
+/*
14226
+** Initialize resources required in RECOVER_STATE_LOSTANDFOUND3
14227
+** state - during which the lost-and-found table of the output database
14228
+** is populated with recovered data that can not be assigned to any
14229
+** recovered schema object.
14230
+*/
14231
+static void recoverLostAndFound3Init(sqlite3_recover *p){
14232
+ RecoverStateLAF *pLaf = &p->laf;
14233
+
14234
+ if( pLaf->nMaxField>0 ){
14235
+ char *zTab = 0; /* Name of lost_and_found table */
14236
+
14237
+ zTab = recoverLostAndFoundCreate(p, pLaf->nMaxField);
14238
+ pLaf->pInsert = recoverLostAndFoundInsert(p, zTab, pLaf->nMaxField);
14239
+ sqlite3_free(zTab);
14240
+
14241
+ pLaf->pAllPage = recoverPreparePrintf(p, p->dbOut,
14242
+ "WITH RECURSIVE seq(ii) AS ("
14243
+ " SELECT 1 UNION ALL SELECT ii+1 FROM seq WHERE ii<%lld"
14244
+ ")"
14245
+ "SELECT ii FROM seq" , p->laf.nPg
14246
+ );
14247
+ pLaf->pPageData = recoverPrepare(p, p->dbOut,
14248
+ "SELECT cell, field, value "
14249
+ "FROM sqlite_dbdata('getpage()') d WHERE d.pgno=? "
14250
+ "UNION ALL "
14251
+ "SELECT -1, -1, -1"
14252
+ );
14253
+
14254
+ pLaf->apVal = (sqlite3_value**)recoverMalloc(p,
14255
+ pLaf->nMaxField*sizeof(sqlite3_value*)
14256
+ );
14257
+ }
14258
+}
14259
+
14260
+/*
14261
+** Initialize resources required in RECOVER_STATE_WRITING state - during which
14262
+** tables recovered from the schema of the input database are populated with
14263
+** recovered data.
14264
+*/
14265
+static int recoverWriteDataInit(sqlite3_recover *p){
14266
+ RecoverStateW1 *p1 = &p->w1;
14267
+ RecoverTable *pTbl = 0;
14268
+ int nByte = 0;
14269
+
14270
+ /* Figure out the maximum number of columns for any table in the schema */
14271
+ assert( p1->nMax==0 );
14272
+ for(pTbl=p->pTblList; pTbl; pTbl=pTbl->pNext){
14273
+ if( pTbl->nCol>p1->nMax ) p1->nMax = pTbl->nCol;
14274
+ }
14275
+
14276
+ /* Allocate an array of (sqlite3_value*) in which to accumulate the values
14277
+ ** that will be written to the output database in a single row. */
14278
+ nByte = sizeof(sqlite3_value*) * (p1->nMax+1);
14279
+ p1->apVal = (sqlite3_value**)recoverMalloc(p, nByte);
14280
+ if( p1->apVal==0 ) return p->errCode;
14281
+
14282
+ /* Prepare the SELECT to loop through schema tables (pTbls) and the SELECT
14283
+ ** to loop through cells that appear to belong to a single table (pSel). */
14284
+ p1->pTbls = recoverPrepare(p, p->dbOut,
14285
+ "SELECT rootpage FROM recovery.schema "
14286
+ " WHERE type='table' AND (sql NOT LIKE 'create virtual%')"
14287
+ " ORDER BY (tbl_name='sqlite_sequence') ASC"
14288
+ );
14289
+ p1->pSel = recoverPrepare(p, p->dbOut,
14290
+ "WITH RECURSIVE pages(page) AS ("
14291
+ " SELECT ?1"
14292
+ " UNION"
14293
+ " SELECT child FROM sqlite_dbptr('getpage()'), pages "
14294
+ " WHERE pgno=page"
14295
+ ") "
14296
+ "SELECT page, cell, field, value "
14297
+ "FROM sqlite_dbdata('getpage()') d, pages p WHERE p.page=d.pgno "
14298
+ "UNION ALL "
14299
+ "SELECT 0, 0, 0, 0"
14300
+ );
14301
+
14302
+ return p->errCode;
14303
+}
14304
+
14305
+/*
14306
+** Clean up resources allocated by recoverWriteDataInit() (stuff in
14307
+** sqlite3_recover.w1).
14308
+*/
14309
+static void recoverWriteDataCleanup(sqlite3_recover *p){
14310
+ RecoverStateW1 *p1 = &p->w1;
14311
+ int ii;
14312
+ for(ii=0; ii<p1->nVal; ii++){
14313
+ sqlite3_value_free(p1->apVal[ii]);
14314
+ }
14315
+ sqlite3_free(p1->apVal);
14316
+ recoverFinalize(p, p1->pInsert);
14317
+ recoverFinalize(p, p1->pTbls);
14318
+ recoverFinalize(p, p1->pSel);
14319
+ memset(p1, 0, sizeof(*p1));
14320
+}
14321
+
14322
+/*
14323
+** Perform one step (sqlite3_recover_step()) of work for the connection
14324
+** passed as the only argument, which is guaranteed to be in
14325
+** RECOVER_STATE_WRITING state - during which tables recovered from the
14326
+** schema of the input database are populated with recovered data.
14327
+*/
14328
+static int recoverWriteDataStep(sqlite3_recover *p){
14329
+ RecoverStateW1 *p1 = &p->w1;
14330
+ sqlite3_stmt *pSel = p1->pSel;
14331
+ sqlite3_value **apVal = p1->apVal;
14332
+
14333
+ if( p->errCode==SQLITE_OK && p1->pTab==0 ){
14334
+ if( sqlite3_step(p1->pTbls)==SQLITE_ROW ){
14335
+ i64 iRoot = sqlite3_column_int64(p1->pTbls, 0);
14336
+ p1->pTab = recoverFindTable(p, iRoot);
14337
+
14338
+ recoverFinalize(p, p1->pInsert);
14339
+ p1->pInsert = 0;
14340
+
14341
+ /* If this table is unknown, return early. The caller will invoke this
14342
+ ** function again and it will move on to the next table. */
14343
+ if( p1->pTab==0 ) return p->errCode;
14344
+
14345
+ /* If this is the sqlite_sequence table, delete any rows added by
14346
+ ** earlier INSERT statements on tables with AUTOINCREMENT primary
14347
+ ** keys before recovering its contents. The p1->pTbls SELECT statement
14348
+ ** is rigged to deliver "sqlite_sequence" last of all, so we don't
14349
+ ** worry about it being modified after it is recovered. */
14350
+ if( sqlite3_stricmp("sqlite_sequence", p1->pTab->zTab)==0 ){
14351
+ recoverExec(p, p->dbOut, "DELETE FROM sqlite_sequence");
14352
+ recoverSqlCallback(p, "DELETE FROM sqlite_sequence");
14353
+ }
14354
+
14355
+ /* Bind the root page of this table within the original database to
14356
+ ** SELECT statement p1->pSel. The SELECT statement will then iterate
14357
+ ** through cells that look like they belong to table pTab. */
14358
+ sqlite3_bind_int64(pSel, 1, iRoot);
14359
+
14360
+ p1->nVal = 0;
14361
+ p1->bHaveRowid = 0;
14362
+ p1->iPrevPage = -1;
14363
+ p1->iPrevCell = -1;
14364
+ }else{
14365
+ return SQLITE_DONE;
14366
+ }
14367
+ }
14368
+ assert( p->errCode!=SQLITE_OK || p1->pTab );
14369
+
14370
+ if( p->errCode==SQLITE_OK && sqlite3_step(pSel)==SQLITE_ROW ){
14371
+ RecoverTable *pTab = p1->pTab;
14372
+
14373
+ i64 iPage = sqlite3_column_int64(pSel, 0);
14374
+ int iCell = sqlite3_column_int(pSel, 1);
14375
+ int iField = sqlite3_column_int(pSel, 2);
14376
+ sqlite3_value *pVal = sqlite3_column_value(pSel, 3);
14377
+ int bNewCell = (p1->iPrevPage!=iPage || p1->iPrevCell!=iCell);
14378
+
14379
+ assert( bNewCell==0 || (iField==-1 || iField==0) );
14380
+ assert( bNewCell || iField==p1->nVal || p1->nVal==pTab->nCol );
14381
+
14382
+ if( bNewCell ){
14383
+ int ii = 0;
14384
+ if( p1->nVal>=0 ){
14385
+ if( p1->pInsert==0 || p1->nVal!=p1->nInsert ){
14386
+ recoverFinalize(p, p1->pInsert);
14387
+ p1->pInsert = recoverInsertStmt(p, pTab, p1->nVal);
14388
+ p1->nInsert = p1->nVal;
14389
+ }
14390
+ if( p1->nVal>0 ){
14391
+ sqlite3_stmt *pInsert = p1->pInsert;
14392
+ for(ii=0; ii<pTab->nCol; ii++){
14393
+ RecoverColumn *pCol = &pTab->aCol[ii];
14394
+ int iBind = pCol->iBind;
14395
+ if( iBind>0 ){
14396
+ if( pCol->bIPK ){
14397
+ sqlite3_bind_int64(pInsert, iBind, p1->iRowid);
14398
+ }else if( pCol->iField<p1->nVal ){
14399
+ recoverBindValue(p, pInsert, iBind, apVal[pCol->iField]);
14400
+ }
14401
+ }
14402
+ }
14403
+ if( p->bRecoverRowid && pTab->iRowidBind>0 && p1->bHaveRowid ){
14404
+ sqlite3_bind_int64(pInsert, pTab->iRowidBind, p1->iRowid);
14405
+ }
14406
+ if( SQLITE_ROW==sqlite3_step(pInsert) ){
14407
+ const char *z = (const char*)sqlite3_column_text(pInsert, 0);
14408
+ recoverSqlCallback(p, z);
14409
+ }
14410
+ recoverReset(p, pInsert);
14411
+ assert( p->errCode || pInsert );
14412
+ if( pInsert ) sqlite3_clear_bindings(pInsert);
14413
+ }
14414
+ }
14415
+
14416
+ for(ii=0; ii<p1->nVal; ii++){
14417
+ sqlite3_value_free(apVal[ii]);
14418
+ apVal[ii] = 0;
14419
+ }
14420
+ p1->nVal = -1;
14421
+ p1->bHaveRowid = 0;
14422
+ }
14423
+
14424
+ if( iPage!=0 ){
14425
+ if( iField<0 ){
14426
+ p1->iRowid = sqlite3_column_int64(pSel, 3);
14427
+ assert( p1->nVal==-1 );
14428
+ p1->nVal = 0;
14429
+ p1->bHaveRowid = 1;
14430
+ }else if( iField<pTab->nCol ){
14431
+ assert( apVal[iField]==0 );
14432
+ apVal[iField] = sqlite3_value_dup( pVal );
14433
+ if( apVal[iField]==0 ){
14434
+ recoverError(p, SQLITE_NOMEM, 0);
14435
+ }
14436
+ p1->nVal = iField+1;
14437
+ }
14438
+ p1->iPrevCell = iCell;
14439
+ p1->iPrevPage = iPage;
14440
+ }
14441
+ }else{
14442
+ recoverReset(p, pSel);
14443
+ p1->pTab = 0;
14444
+ }
14445
+
14446
+ return p->errCode;
14447
+}
14448
+
14449
+/*
14450
+** Initialize resources required by sqlite3_recover_step() in
14451
+** RECOVER_STATE_LOSTANDFOUND1 state - during which the set of pages not
14452
+** already allocated to a recovered schema element is determined.
14453
+*/
14454
+static void recoverLostAndFound1Init(sqlite3_recover *p){
14455
+ RecoverStateLAF *pLaf = &p->laf;
14456
+ sqlite3_stmt *pStmt = 0;
14457
+
14458
+ assert( p->laf.pUsed==0 );
14459
+ pLaf->nPg = recoverPageCount(p);
14460
+ pLaf->pUsed = recoverBitmapAlloc(p, pLaf->nPg);
14461
+
14462
+ /* Prepare a statement to iterate through all pages that are part of any tree
14463
+ ** in the recoverable part of the input database schema to the bitmap. And,
14464
+ ** if !p->bFreelistCorrupt, add all pages that appear to be part of the
14465
+ ** freelist. */
14466
+ pStmt = recoverPrepare(
14467
+ p, p->dbOut,
14468
+ "WITH trunk(pgno) AS ("
14469
+ " SELECT read_i32(getpage(1), 8) AS x WHERE x>0"
14470
+ " UNION"
14471
+ " SELECT read_i32(getpage(trunk.pgno), 0) AS x FROM trunk WHERE x>0"
14472
+ "),"
14473
+ "trunkdata(pgno, data) AS ("
14474
+ " SELECT pgno, getpage(pgno) FROM trunk"
14475
+ "),"
14476
+ "freelist(data, n, freepgno) AS ("
14477
+ " SELECT data, min(16384, read_i32(data, 1)-1), pgno FROM trunkdata"
14478
+ " UNION ALL"
14479
+ " SELECT data, n-1, read_i32(data, 2+n) FROM freelist WHERE n>=0"
14480
+ "),"
14481
+ ""
14482
+ "roots(r) AS ("
14483
+ " SELECT 1 UNION ALL"
14484
+ " SELECT rootpage FROM recovery.schema WHERE rootpage>0"
14485
+ "),"
14486
+ "used(page) AS ("
14487
+ " SELECT r FROM roots"
14488
+ " UNION"
14489
+ " SELECT child FROM sqlite_dbptr('getpage()'), used "
14490
+ " WHERE pgno=page"
14491
+ ") "
14492
+ "SELECT page FROM used"
14493
+ " UNION ALL "
14494
+ "SELECT freepgno FROM freelist WHERE NOT ?"
14495
+ );
14496
+ if( pStmt ) sqlite3_bind_int(pStmt, 1, p->bFreelistCorrupt);
14497
+ pLaf->pUsedPages = pStmt;
14498
+}
14499
+
14500
+/*
14501
+** Perform one step (sqlite3_recover_step()) of work for the connection
14502
+** passed as the only argument, which is guaranteed to be in
14503
+** RECOVER_STATE_LOSTANDFOUND1 state - during which the set of pages not
14504
+** already allocated to a recovered schema element is determined.
14505
+*/
14506
+static int recoverLostAndFound1Step(sqlite3_recover *p){
14507
+ RecoverStateLAF *pLaf = &p->laf;
14508
+ int rc = p->errCode;
14509
+ if( rc==SQLITE_OK ){
14510
+ rc = sqlite3_step(pLaf->pUsedPages);
14511
+ if( rc==SQLITE_ROW ){
14512
+ i64 iPg = sqlite3_column_int64(pLaf->pUsedPages, 0);
14513
+ recoverBitmapSet(pLaf->pUsed, iPg);
14514
+ rc = SQLITE_OK;
14515
+ }else{
14516
+ recoverFinalize(p, pLaf->pUsedPages);
14517
+ pLaf->pUsedPages = 0;
14518
+ }
14519
+ }
14520
+ return rc;
14521
+}
14522
+
14523
+/*
14524
+** Initialize resources required by RECOVER_STATE_LOSTANDFOUND2
14525
+** state - during which the pages identified in RECOVER_STATE_LOSTANDFOUND1
14526
+** are sorted into sets that likely belonged to the same database tree.
14527
+*/
14528
+static void recoverLostAndFound2Init(sqlite3_recover *p){
14529
+ RecoverStateLAF *pLaf = &p->laf;
14530
+
14531
+ assert( p->laf.pAllAndParent==0 );
14532
+ assert( p->laf.pMapInsert==0 );
14533
+ assert( p->laf.pMaxField==0 );
14534
+ assert( p->laf.nMaxField==0 );
14535
+
14536
+ pLaf->pMapInsert = recoverPrepare(p, p->dbOut,
14537
+ "INSERT OR IGNORE INTO recovery.map(pgno, parent) VALUES(?, ?)"
14538
+ );
14539
+ pLaf->pAllAndParent = recoverPreparePrintf(p, p->dbOut,
14540
+ "WITH RECURSIVE seq(ii) AS ("
14541
+ " SELECT 1 UNION ALL SELECT ii+1 FROM seq WHERE ii<%lld"
14542
+ ")"
14543
+ "SELECT pgno, child FROM sqlite_dbptr('getpage()') "
14544
+ " UNION ALL "
14545
+ "SELECT NULL, ii FROM seq", p->laf.nPg
14546
+ );
14547
+ pLaf->pMaxField = recoverPreparePrintf(p, p->dbOut,
14548
+ "SELECT max(field)+1 FROM sqlite_dbdata('getpage') WHERE pgno = ?"
14549
+ );
14550
+}
14551
+
14552
+/*
14553
+** Perform one step (sqlite3_recover_step()) of work for the connection
14554
+** passed as the only argument, which is guaranteed to be in
14555
+** RECOVER_STATE_LOSTANDFOUND2 state - during which the pages identified
14556
+** in RECOVER_STATE_LOSTANDFOUND1 are sorted into sets that likely belonged
14557
+** to the same database tree.
14558
+*/
14559
+static int recoverLostAndFound2Step(sqlite3_recover *p){
14560
+ RecoverStateLAF *pLaf = &p->laf;
14561
+ if( p->errCode==SQLITE_OK ){
14562
+ int res = sqlite3_step(pLaf->pAllAndParent);
14563
+ if( res==SQLITE_ROW ){
14564
+ i64 iChild = sqlite3_column_int(pLaf->pAllAndParent, 1);
14565
+ if( recoverBitmapQuery(pLaf->pUsed, iChild)==0 ){
14566
+ sqlite3_bind_int64(pLaf->pMapInsert, 1, iChild);
14567
+ sqlite3_bind_value(pLaf->pMapInsert, 2,
14568
+ sqlite3_column_value(pLaf->pAllAndParent, 0)
14569
+ );
14570
+ sqlite3_step(pLaf->pMapInsert);
14571
+ recoverReset(p, pLaf->pMapInsert);
14572
+ sqlite3_bind_int64(pLaf->pMaxField, 1, iChild);
14573
+ if( SQLITE_ROW==sqlite3_step(pLaf->pMaxField) ){
14574
+ int nMax = sqlite3_column_int(pLaf->pMaxField, 0);
14575
+ if( nMax>pLaf->nMaxField ) pLaf->nMaxField = nMax;
14576
+ }
14577
+ recoverReset(p, pLaf->pMaxField);
14578
+ }
14579
+ }else{
14580
+ recoverFinalize(p, pLaf->pAllAndParent);
14581
+ pLaf->pAllAndParent =0;
14582
+ return SQLITE_DONE;
14583
+ }
14584
+ }
14585
+ return p->errCode;
14586
+}
14587
+
14588
+/*
14589
+** Free all resources allocated as part of sqlite3_recover_step() calls
14590
+** in one of the RECOVER_STATE_LOSTANDFOUND[123] states.
14591
+*/
14592
+static void recoverLostAndFoundCleanup(sqlite3_recover *p){
14593
+ recoverBitmapFree(p->laf.pUsed);
14594
+ p->laf.pUsed = 0;
14595
+ sqlite3_finalize(p->laf.pUsedPages);
14596
+ sqlite3_finalize(p->laf.pAllAndParent);
14597
+ sqlite3_finalize(p->laf.pMapInsert);
14598
+ sqlite3_finalize(p->laf.pMaxField);
14599
+ sqlite3_finalize(p->laf.pFindRoot);
14600
+ sqlite3_finalize(p->laf.pInsert);
14601
+ sqlite3_finalize(p->laf.pAllPage);
14602
+ sqlite3_finalize(p->laf.pPageData);
14603
+ p->laf.pUsedPages = 0;
14604
+ p->laf.pAllAndParent = 0;
14605
+ p->laf.pMapInsert = 0;
14606
+ p->laf.pMaxField = 0;
14607
+ p->laf.pFindRoot = 0;
14608
+ p->laf.pInsert = 0;
14609
+ p->laf.pAllPage = 0;
14610
+ p->laf.pPageData = 0;
14611
+ sqlite3_free(p->laf.apVal);
14612
+ p->laf.apVal = 0;
14613
+}
14614
+
14615
+/*
14616
+** Free all resources allocated as part of sqlite3_recover_step() calls.
14617
+*/
14618
+static void recoverFinalCleanup(sqlite3_recover *p){
14619
+ RecoverTable *pTab = 0;
14620
+ RecoverTable *pNext = 0;
14621
+
14622
+ recoverWriteDataCleanup(p);
14623
+ recoverLostAndFoundCleanup(p);
14624
+
14625
+ for(pTab=p->pTblList; pTab; pTab=pNext){
14626
+ pNext = pTab->pNext;
14627
+ sqlite3_free(pTab);
14628
+ }
14629
+ p->pTblList = 0;
14630
+ sqlite3_finalize(p->pGetPage);
14631
+ p->pGetPage = 0;
14632
+
14633
+ {
14634
+#ifndef NDEBUG
14635
+ int res =
14636
+#endif
14637
+ sqlite3_close(p->dbOut);
14638
+ assert( res==SQLITE_OK );
14639
+ }
14640
+ p->dbOut = 0;
14641
+}
14642
+
14643
+/*
14644
+** Decode and return an unsigned 16-bit big-endian integer value from
14645
+** buffer a[].
14646
+*/
14647
+static u32 recoverGetU16(const u8 *a){
14648
+ return (((u32)a[0])<<8) + ((u32)a[1]);
14649
+}
14650
+
14651
+/*
14652
+** Decode and return an unsigned 32-bit big-endian integer value from
14653
+** buffer a[].
14654
+*/
14655
+static u32 recoverGetU32(const u8 *a){
14656
+ return (((u32)a[0])<<24) + (((u32)a[1])<<16) + (((u32)a[2])<<8) + ((u32)a[3]);
14657
+}
14658
+
14659
+/*
14660
+** Decode an SQLite varint from buffer a[]. Write the decoded value to (*pVal)
14661
+** and return the number of bytes consumed.
14662
+*/
14663
+static int recoverGetVarint(const u8 *a, i64 *pVal){
14664
+ sqlite3_uint64 u = 0;
14665
+ int i;
14666
+ for(i=0; i<8; i++){
14667
+ u = (u<<7) + (a[i]&0x7f);
14668
+ if( (a[i]&0x80)==0 ){ *pVal = (sqlite3_int64)u; return i+1; }
14669
+ }
14670
+ u = (u<<8) + (a[i]&0xff);
14671
+ *pVal = (sqlite3_int64)u;
14672
+ return 9;
14673
+}
14674
+
14675
+/*
14676
+** The second argument points to a buffer n bytes in size. If this buffer
14677
+** or a prefix thereof appears to contain a well-formed SQLite b-tree page,
14678
+** return the page-size in bytes. Otherwise, if the buffer does not
14679
+** appear to contain a well-formed b-tree page, return 0.
14680
+*/
14681
+static int recoverIsValidPage(u8 *aTmp, const u8 *a, int n){
14682
+ u8 *aUsed = aTmp;
14683
+ int nFrag = 0;
14684
+ int nActual = 0;
14685
+ int iFree = 0;
14686
+ int nCell = 0; /* Number of cells on page */
14687
+ int iCellOff = 0; /* Offset of cell array in page */
14688
+ int iContent = 0;
14689
+ int eType = 0;
14690
+ int ii = 0;
14691
+
14692
+ eType = (int)a[0];
14693
+ if( eType!=0x02 && eType!=0x05 && eType!=0x0A && eType!=0x0D ) return 0;
14694
+
14695
+ iFree = (int)recoverGetU16(&a[1]);
14696
+ nCell = (int)recoverGetU16(&a[3]);
14697
+ iContent = (int)recoverGetU16(&a[5]);
14698
+ if( iContent==0 ) iContent = 65536;
14699
+ nFrag = (int)a[7];
14700
+
14701
+ if( iContent>n ) return 0;
14702
+
14703
+ memset(aUsed, 0, n);
14704
+ memset(aUsed, 0xFF, iContent);
14705
+
14706
+ /* Follow the free-list. This is the same format for all b-tree pages. */
14707
+ if( iFree && iFree<=iContent ) return 0;
14708
+ while( iFree ){
14709
+ int iNext = 0;
14710
+ int nByte = 0;
14711
+ if( iFree>(n-4) ) return 0;
14712
+ iNext = recoverGetU16(&a[iFree]);
14713
+ nByte = recoverGetU16(&a[iFree+2]);
14714
+ if( iFree+nByte>n ) return 0;
14715
+ if( iNext && iNext<iFree+nByte ) return 0;
14716
+ memset(&aUsed[iFree], 0xFF, nByte);
14717
+ iFree = iNext;
14718
+ }
14719
+
14720
+ /* Run through the cells */
14721
+ if( eType==0x02 || eType==0x05 ){
14722
+ iCellOff = 12;
14723
+ }else{
14724
+ iCellOff = 8;
14725
+ }
14726
+ if( (iCellOff + 2*nCell)>iContent ) return 0;
14727
+ for(ii=0; ii<nCell; ii++){
14728
+ int iByte;
14729
+ i64 nPayload = 0;
14730
+ int nByte = 0;
14731
+ int iOff = recoverGetU16(&a[iCellOff + 2*ii]);
14732
+ if( iOff<iContent || iOff>n ){
14733
+ return 0;
14734
+ }
14735
+ if( eType==0x05 || eType==0x02 ) nByte += 4;
14736
+ nByte += recoverGetVarint(&a[iOff+nByte], &nPayload);
14737
+ if( eType==0x0D ){
14738
+ i64 dummy = 0;
14739
+ nByte += recoverGetVarint(&a[iOff+nByte], &dummy);
14740
+ }
14741
+ if( eType!=0x05 ){
14742
+ int X = (eType==0x0D) ? n-35 : (((n-12)*64/255)-23);
14743
+ int M = ((n-12)*32/255)-23;
14744
+ int K = M+((nPayload-M)%(n-4));
14745
+
14746
+ if( nPayload<X ){
14747
+ nByte += nPayload;
14748
+ }else if( K<=X ){
14749
+ nByte += K+4;
14750
+ }else{
14751
+ nByte += M+4;
14752
+ }
14753
+ }
14754
+
14755
+ if( iOff+nByte>n ){
14756
+ return 0;
14757
+ }
14758
+ for(iByte=iOff; iByte<(iOff+nByte); iByte++){
14759
+ if( aUsed[iByte]!=0 ){
14760
+ return 0;
14761
+ }
14762
+ aUsed[iByte] = 0xFF;
14763
+ }
14764
+ }
14765
+
14766
+ nActual = 0;
14767
+ for(ii=0; ii<n; ii++){
14768
+ if( aUsed[ii]==0 ) nActual++;
14769
+ }
14770
+ return (nActual==nFrag);
14771
+}
14772
+
14773
+
14774
+static int recoverVfsClose(sqlite3_file*);
14775
+static int recoverVfsRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
14776
+static int recoverVfsWrite(sqlite3_file*, const void*, int, sqlite3_int64);
14777
+static int recoverVfsTruncate(sqlite3_file*, sqlite3_int64 size);
14778
+static int recoverVfsSync(sqlite3_file*, int flags);
14779
+static int recoverVfsFileSize(sqlite3_file*, sqlite3_int64 *pSize);
14780
+static int recoverVfsLock(sqlite3_file*, int);
14781
+static int recoverVfsUnlock(sqlite3_file*, int);
14782
+static int recoverVfsCheckReservedLock(sqlite3_file*, int *pResOut);
14783
+static int recoverVfsFileControl(sqlite3_file*, int op, void *pArg);
14784
+static int recoverVfsSectorSize(sqlite3_file*);
14785
+static int recoverVfsDeviceCharacteristics(sqlite3_file*);
14786
+static int recoverVfsShmMap(sqlite3_file*, int, int, int, void volatile**);
14787
+static int recoverVfsShmLock(sqlite3_file*, int offset, int n, int flags);
14788
+static void recoverVfsShmBarrier(sqlite3_file*);
14789
+static int recoverVfsShmUnmap(sqlite3_file*, int deleteFlag);
14790
+static int recoverVfsFetch(sqlite3_file*, sqlite3_int64, int, void**);
14791
+static int recoverVfsUnfetch(sqlite3_file *pFd, sqlite3_int64 iOff, void *p);
14792
+
14793
+static sqlite3_io_methods recover_methods = {
14794
+ 2, /* iVersion */
14795
+ recoverVfsClose,
14796
+ recoverVfsRead,
14797
+ recoverVfsWrite,
14798
+ recoverVfsTruncate,
14799
+ recoverVfsSync,
14800
+ recoverVfsFileSize,
14801
+ recoverVfsLock,
14802
+ recoverVfsUnlock,
14803
+ recoverVfsCheckReservedLock,
14804
+ recoverVfsFileControl,
14805
+ recoverVfsSectorSize,
14806
+ recoverVfsDeviceCharacteristics,
14807
+ recoverVfsShmMap,
14808
+ recoverVfsShmLock,
14809
+ recoverVfsShmBarrier,
14810
+ recoverVfsShmUnmap,
14811
+ recoverVfsFetch,
14812
+ recoverVfsUnfetch
14813
+};
14814
+
14815
+static int recoverVfsClose(sqlite3_file *pFd){
14816
+ assert( pFd->pMethods!=&recover_methods );
14817
+ return pFd->pMethods->xClose(pFd);
14818
+}
14819
+
14820
+/*
14821
+** Write value v to buffer a[] as a 16-bit big-endian unsigned integer.
14822
+*/
14823
+static void recoverPutU16(u8 *a, u32 v){
14824
+ a[0] = (v>>8) & 0x00FF;
14825
+ a[1] = (v>>0) & 0x00FF;
14826
+}
14827
+
14828
+/*
14829
+** Write value v to buffer a[] as a 32-bit big-endian unsigned integer.
14830
+*/
14831
+static void recoverPutU32(u8 *a, u32 v){
14832
+ a[0] = (v>>24) & 0x00FF;
14833
+ a[1] = (v>>16) & 0x00FF;
14834
+ a[2] = (v>>8) & 0x00FF;
14835
+ a[3] = (v>>0) & 0x00FF;
14836
+}
14837
+
14838
+/*
14839
+** Detect the page-size of the database opened by file-handle pFd by
14840
+** searching the first part of the file for a well-formed SQLite b-tree
14841
+** page. If parameter nReserve is non-zero, then as well as searching for
14842
+** a b-tree page with zero reserved bytes, this function searches for one
14843
+** with nReserve reserved bytes at the end of it.
14844
+**
14845
+** If successful, set variable p->detected_pgsz to the detected page-size
14846
+** in bytes and return SQLITE_OK. Or, if no error occurs but no valid page
14847
+** can be found, return SQLITE_OK but leave p->detected_pgsz set to 0. Or,
14848
+** if an error occurs (e.g. an IO or OOM error), then an SQLite error code
14849
+** is returned. The final value of p->detected_pgsz is undefined in this
14850
+** case.
14851
+*/
14852
+static int recoverVfsDetectPagesize(
14853
+ sqlite3_recover *p, /* Recover handle */
14854
+ sqlite3_file *pFd, /* File-handle open on input database */
14855
+ u32 nReserve, /* Possible nReserve value */
14856
+ i64 nSz /* Size of database file in bytes */
14857
+){
14858
+ int rc = SQLITE_OK;
14859
+ const int nMin = 512;
14860
+ const int nMax = 65536;
14861
+ const int nMaxBlk = 4;
14862
+ u32 pgsz = 0;
14863
+ int iBlk = 0;
14864
+ u8 *aPg = 0;
14865
+ u8 *aTmp = 0;
14866
+ int nBlk = 0;
14867
+
14868
+ aPg = (u8*)sqlite3_malloc(2*nMax);
14869
+ if( aPg==0 ) return SQLITE_NOMEM;
14870
+ aTmp = &aPg[nMax];
14871
+
14872
+ nBlk = (nSz+nMax-1)/nMax;
14873
+ if( nBlk>nMaxBlk ) nBlk = nMaxBlk;
14874
+
14875
+ do {
14876
+ for(iBlk=0; rc==SQLITE_OK && iBlk<nBlk; iBlk++){
14877
+ int nByte = (nSz>=((iBlk+1)*nMax)) ? nMax : (nSz % nMax);
14878
+ memset(aPg, 0, nMax);
14879
+ rc = pFd->pMethods->xRead(pFd, aPg, nByte, iBlk*nMax);
14880
+ if( rc==SQLITE_OK ){
14881
+ int pgsz2;
14882
+ for(pgsz2=(pgsz ? pgsz*2 : nMin); pgsz2<=nMax; pgsz2=pgsz2*2){
14883
+ int iOff;
14884
+ for(iOff=0; iOff<nMax; iOff+=pgsz2){
14885
+ if( recoverIsValidPage(aTmp, &aPg[iOff], pgsz2-nReserve) ){
14886
+ pgsz = pgsz2;
14887
+ break;
14888
+ }
14889
+ }
14890
+ }
14891
+ }
14892
+ }
14893
+ if( pgsz>(u32)p->detected_pgsz ){
14894
+ p->detected_pgsz = pgsz;
14895
+ p->nReserve = nReserve;
14896
+ }
14897
+ if( nReserve==0 ) break;
14898
+ nReserve = 0;
14899
+ }while( 1 );
14900
+
14901
+ p->detected_pgsz = pgsz;
14902
+ sqlite3_free(aPg);
14903
+ return rc;
14904
+}
14905
+
14906
+/*
14907
+** The xRead() method of the wrapper VFS. This is used to intercept calls
14908
+** to read page 1 of the input database.
14909
+*/
14910
+static int recoverVfsRead(sqlite3_file *pFd, void *aBuf, int nByte, i64 iOff){
14911
+ int rc = SQLITE_OK;
14912
+ if( pFd->pMethods==&recover_methods ){
14913
+ pFd->pMethods = recover_g.pMethods;
14914
+ rc = pFd->pMethods->xRead(pFd, aBuf, nByte, iOff);
14915
+ if( nByte==16 ){
14916
+ sqlite3_randomness(16, aBuf);
14917
+ }else
14918
+ if( rc==SQLITE_OK && iOff==0 && nByte>=108 ){
14919
+ /* Ensure that the database has a valid header file. The only fields
14920
+ ** that really matter to recovery are:
14921
+ **
14922
+ ** + Database page size (16-bits at offset 16)
14923
+ ** + Size of db in pages (32-bits at offset 28)
14924
+ ** + Database encoding (32-bits at offset 56)
14925
+ **
14926
+ ** Also preserved are:
14927
+ **
14928
+ ** + first freelist page (32-bits at offset 32)
14929
+ ** + size of freelist (32-bits at offset 36)
14930
+ **
14931
+ ** We also try to preserve the auto-vacuum, incr-value, user-version
14932
+ ** and application-id fields - all 32 bit quantities at offsets
14933
+ ** 52, 60, 64 and 68. All other fields are set to known good values.
14934
+ **
14935
+ ** Byte offset 105 should also contain the page-size as a 16-bit
14936
+ ** integer.
14937
+ */
14938
+ const int aPreserve[] = {32, 36, 52, 60, 64, 68};
14939
+ u8 aHdr[108] = {
14940
+ 0x53, 0x51, 0x4c, 0x69, 0x74, 0x65, 0x20, 0x66,
14941
+ 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x20, 0x33, 0x00,
14942
+ 0xFF, 0xFF, 0x01, 0x01, 0x00, 0x40, 0x20, 0x20,
14943
+ 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
14944
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
14945
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
14946
+ 0x00, 0x00, 0x10, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
14947
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
14948
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
14949
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
14950
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
14951
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
14952
+ 0x00, 0x2e, 0x5b, 0x30,
14953
+
14954
+ 0x0D, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00
14955
+ };
14956
+ u8 *a = (u8*)aBuf;
14957
+
14958
+ u32 pgsz = recoverGetU16(&a[16]);
14959
+ u32 nReserve = a[20];
14960
+ u32 enc = recoverGetU32(&a[56]);
14961
+ u32 dbsz = 0;
14962
+ i64 dbFileSize = 0;
14963
+ int ii;
14964
+ sqlite3_recover *p = recover_g.p;
14965
+
14966
+ if( pgsz==0x01 ) pgsz = 65536;
14967
+ rc = pFd->pMethods->xFileSize(pFd, &dbFileSize);
14968
+
14969
+ if( rc==SQLITE_OK && p->detected_pgsz==0 ){
14970
+ rc = recoverVfsDetectPagesize(p, pFd, nReserve, dbFileSize);
14971
+ }
14972
+ if( p->detected_pgsz ){
14973
+ pgsz = p->detected_pgsz;
14974
+ nReserve = p->nReserve;
14975
+ }
14976
+
14977
+ if( pgsz ){
14978
+ dbsz = dbFileSize / pgsz;
14979
+ }
14980
+ if( enc!=SQLITE_UTF8 && enc!=SQLITE_UTF16BE && enc!=SQLITE_UTF16LE ){
14981
+ enc = SQLITE_UTF8;
14982
+ }
14983
+
14984
+ sqlite3_free(p->pPage1Cache);
14985
+ p->pPage1Cache = 0;
14986
+ p->pPage1Disk = 0;
14987
+
14988
+ p->pgsz = nByte;
14989
+ p->pPage1Cache = (u8*)recoverMalloc(p, nByte*2);
14990
+ if( p->pPage1Cache ){
14991
+ p->pPage1Disk = &p->pPage1Cache[nByte];
14992
+ memcpy(p->pPage1Disk, aBuf, nByte);
14993
+
14994
+ recoverPutU32(&aHdr[28], dbsz);
14995
+ recoverPutU32(&aHdr[56], enc);
14996
+ recoverPutU16(&aHdr[105], pgsz-nReserve);
14997
+ if( pgsz==65536 ) pgsz = 1;
14998
+ recoverPutU16(&aHdr[16], pgsz);
14999
+ aHdr[20] = nReserve;
15000
+ for(ii=0; ii<sizeof(aPreserve)/sizeof(aPreserve[0]); ii++){
15001
+ memcpy(&aHdr[aPreserve[ii]], &a[aPreserve[ii]], 4);
15002
+ }
15003
+ memcpy(aBuf, aHdr, sizeof(aHdr));
15004
+ memset(&((u8*)aBuf)[sizeof(aHdr)], 0, nByte-sizeof(aHdr));
15005
+
15006
+ memcpy(p->pPage1Cache, aBuf, nByte);
15007
+ }else{
15008
+ rc = p->errCode;
15009
+ }
15010
+
15011
+ }
15012
+ pFd->pMethods = &recover_methods;
15013
+ }else{
15014
+ rc = pFd->pMethods->xRead(pFd, aBuf, nByte, iOff);
15015
+ }
15016
+ return rc;
15017
+}
15018
+
15019
+/*
15020
+** Used to make sqlite3_io_methods wrapper methods less verbose.
15021
+*/
15022
+#define RECOVER_VFS_WRAPPER(code) \
15023
+ int rc = SQLITE_OK; \
15024
+ if( pFd->pMethods==&recover_methods ){ \
15025
+ pFd->pMethods = recover_g.pMethods; \
15026
+ rc = code; \
15027
+ pFd->pMethods = &recover_methods; \
15028
+ }else{ \
15029
+ rc = code; \
15030
+ } \
15031
+ return rc;
15032
+
15033
+/*
15034
+** Methods of the wrapper VFS. All methods except for xRead() and xClose()
15035
+** simply uninstall the sqlite3_io_methods wrapper, invoke the equivalent
15036
+** method on the lower level VFS, then reinstall the wrapper before returning.
15037
+** Those that return an integer value use the RECOVER_VFS_WRAPPER macro.
15038
+*/
15039
+static int recoverVfsWrite(
15040
+ sqlite3_file *pFd, const void *aBuf, int nByte, i64 iOff
15041
+){
15042
+ RECOVER_VFS_WRAPPER (
15043
+ pFd->pMethods->xWrite(pFd, aBuf, nByte, iOff)
15044
+ );
15045
+}
15046
+static int recoverVfsTruncate(sqlite3_file *pFd, sqlite3_int64 size){
15047
+ RECOVER_VFS_WRAPPER (
15048
+ pFd->pMethods->xTruncate(pFd, size)
15049
+ );
15050
+}
15051
+static int recoverVfsSync(sqlite3_file *pFd, int flags){
15052
+ RECOVER_VFS_WRAPPER (
15053
+ pFd->pMethods->xSync(pFd, flags)
15054
+ );
15055
+}
15056
+static int recoverVfsFileSize(sqlite3_file *pFd, sqlite3_int64 *pSize){
15057
+ RECOVER_VFS_WRAPPER (
15058
+ pFd->pMethods->xFileSize(pFd, pSize)
15059
+ );
15060
+}
15061
+static int recoverVfsLock(sqlite3_file *pFd, int eLock){
15062
+ RECOVER_VFS_WRAPPER (
15063
+ pFd->pMethods->xLock(pFd, eLock)
15064
+ );
15065
+}
15066
+static int recoverVfsUnlock(sqlite3_file *pFd, int eLock){
15067
+ RECOVER_VFS_WRAPPER (
15068
+ pFd->pMethods->xUnlock(pFd, eLock)
15069
+ );
15070
+}
15071
+static int recoverVfsCheckReservedLock(sqlite3_file *pFd, int *pResOut){
15072
+ RECOVER_VFS_WRAPPER (
15073
+ pFd->pMethods->xCheckReservedLock(pFd, pResOut)
15074
+ );
15075
+}
15076
+static int recoverVfsFileControl(sqlite3_file *pFd, int op, void *pArg){
15077
+ RECOVER_VFS_WRAPPER (
15078
+ (pFd->pMethods ? pFd->pMethods->xFileControl(pFd, op, pArg) : SQLITE_NOTFOUND)
15079
+ );
15080
+}
15081
+static int recoverVfsSectorSize(sqlite3_file *pFd){
15082
+ RECOVER_VFS_WRAPPER (
15083
+ pFd->pMethods->xSectorSize(pFd)
15084
+ );
15085
+}
15086
+static int recoverVfsDeviceCharacteristics(sqlite3_file *pFd){
15087
+ RECOVER_VFS_WRAPPER (
15088
+ pFd->pMethods->xDeviceCharacteristics(pFd)
15089
+ );
15090
+}
15091
+static int recoverVfsShmMap(
15092
+ sqlite3_file *pFd, int iPg, int pgsz, int bExtend, void volatile **pp
15093
+){
15094
+ RECOVER_VFS_WRAPPER (
15095
+ pFd->pMethods->xShmMap(pFd, iPg, pgsz, bExtend, pp)
15096
+ );
15097
+}
15098
+static int recoverVfsShmLock(sqlite3_file *pFd, int offset, int n, int flags){
15099
+ RECOVER_VFS_WRAPPER (
15100
+ pFd->pMethods->xShmLock(pFd, offset, n, flags)
15101
+ );
15102
+}
15103
+static void recoverVfsShmBarrier(sqlite3_file *pFd){
15104
+ if( pFd->pMethods==&recover_methods ){
15105
+ pFd->pMethods = recover_g.pMethods;
15106
+ pFd->pMethods->xShmBarrier(pFd);
15107
+ pFd->pMethods = &recover_methods;
15108
+ }else{
15109
+ pFd->pMethods->xShmBarrier(pFd);
15110
+ }
15111
+}
15112
+static int recoverVfsShmUnmap(sqlite3_file *pFd, int deleteFlag){
15113
+ RECOVER_VFS_WRAPPER (
15114
+ pFd->pMethods->xShmUnmap(pFd, deleteFlag)
15115
+ );
15116
+}
15117
+
15118
+static int recoverVfsFetch(
15119
+ sqlite3_file *pFd,
15120
+ sqlite3_int64 iOff,
15121
+ int iAmt,
15122
+ void **pp
15123
+){
15124
+ *pp = 0;
15125
+ return SQLITE_OK;
15126
+}
15127
+static int recoverVfsUnfetch(sqlite3_file *pFd, sqlite3_int64 iOff, void *p){
15128
+ return SQLITE_OK;
15129
+}
15130
+
15131
+/*
15132
+** Install the VFS wrapper around the file-descriptor open on the input
15133
+** database for recover handle p. Mutex RECOVER_MUTEX_ID must be held
15134
+** when this function is called.
15135
+*/
15136
+static void recoverInstallWrapper(sqlite3_recover *p){
15137
+ sqlite3_file *pFd = 0;
15138
+ assert( recover_g.pMethods==0 );
15139
+ recoverAssertMutexHeld();
15140
+ sqlite3_file_control(p->dbIn, p->zDb, SQLITE_FCNTL_FILE_POINTER, (void*)&pFd);
15141
+ assert( pFd==0 || pFd->pMethods!=&recover_methods );
15142
+ if( pFd && pFd->pMethods ){
15143
+ int iVersion = 1 + (pFd->pMethods->iVersion>1 && pFd->pMethods->xShmMap!=0);
15144
+ recover_g.pMethods = pFd->pMethods;
15145
+ recover_g.p = p;
15146
+ recover_methods.iVersion = iVersion;
15147
+ pFd->pMethods = &recover_methods;
15148
+ }
15149
+}
15150
+
15151
+/*
15152
+** Uninstall the VFS wrapper that was installed around the file-descriptor open
15153
+** on the input database for recover handle p. Mutex RECOVER_MUTEX_ID must be
15154
+** held when this function is called.
15155
+*/
15156
+static void recoverUninstallWrapper(sqlite3_recover *p){
15157
+ sqlite3_file *pFd = 0;
15158
+ recoverAssertMutexHeld();
15159
+ sqlite3_file_control(p->dbIn, p->zDb,SQLITE_FCNTL_FILE_POINTER,(void*)&pFd);
15160
+ if( pFd && pFd->pMethods ){
15161
+ pFd->pMethods = recover_g.pMethods;
15162
+ recover_g.pMethods = 0;
15163
+ recover_g.p = 0;
15164
+ }
15165
+}
15166
+
15167
+/*
15168
+** This function does the work of a single sqlite3_recover_step() call. It
15169
+** is guaranteed that the handle is not in an error state when this
15170
+** function is called.
15171
+*/
15172
+static void recoverStep(sqlite3_recover *p){
15173
+ assert( p && p->errCode==SQLITE_OK );
15174
+ switch( p->eState ){
15175
+ case RECOVER_STATE_INIT:
15176
+ /* This is the very first call to sqlite3_recover_step() on this object.
15177
+ */
15178
+ recoverSqlCallback(p, "BEGIN");
15179
+ recoverSqlCallback(p, "PRAGMA writable_schema = on");
15180
+
15181
+ recoverEnterMutex();
15182
+ recoverInstallWrapper(p);
15183
+
15184
+ /* Open the output database. And register required virtual tables and
15185
+ ** user functions with the new handle. */
15186
+ recoverOpenOutput(p);
15187
+
15188
+ /* Open transactions on both the input and output databases. */
15189
+ recoverExec(p, p->dbIn, "PRAGMA writable_schema = on");
15190
+ recoverExec(p, p->dbIn, "BEGIN");
15191
+ if( p->errCode==SQLITE_OK ) p->bCloseTransaction = 1;
15192
+ recoverExec(p, p->dbIn, "SELECT 1 FROM sqlite_schema");
15193
+ recoverTransferSettings(p);
15194
+ recoverOpenRecovery(p);
15195
+ recoverCacheSchema(p);
15196
+
15197
+ recoverUninstallWrapper(p);
15198
+ recoverLeaveMutex();
15199
+
15200
+ recoverExec(p, p->dbOut, "BEGIN");
15201
+
15202
+ recoverWriteSchema1(p);
15203
+ p->eState = RECOVER_STATE_WRITING;
15204
+ break;
15205
+
15206
+ case RECOVER_STATE_WRITING: {
15207
+ if( p->w1.pTbls==0 ){
15208
+ recoverWriteDataInit(p);
15209
+ }
15210
+ if( SQLITE_DONE==recoverWriteDataStep(p) ){
15211
+ recoverWriteDataCleanup(p);
15212
+ if( p->zLostAndFound ){
15213
+ p->eState = RECOVER_STATE_LOSTANDFOUND1;
15214
+ }else{
15215
+ p->eState = RECOVER_STATE_SCHEMA2;
15216
+ }
15217
+ }
15218
+ break;
15219
+ }
15220
+
15221
+ case RECOVER_STATE_LOSTANDFOUND1: {
15222
+ if( p->laf.pUsed==0 ){
15223
+ recoverLostAndFound1Init(p);
15224
+ }
15225
+ if( SQLITE_DONE==recoverLostAndFound1Step(p) ){
15226
+ p->eState = RECOVER_STATE_LOSTANDFOUND2;
15227
+ }
15228
+ break;
15229
+ }
15230
+ case RECOVER_STATE_LOSTANDFOUND2: {
15231
+ if( p->laf.pAllAndParent==0 ){
15232
+ recoverLostAndFound2Init(p);
15233
+ }
15234
+ if( SQLITE_DONE==recoverLostAndFound2Step(p) ){
15235
+ p->eState = RECOVER_STATE_LOSTANDFOUND3;
15236
+ }
15237
+ break;
15238
+ }
15239
+
15240
+ case RECOVER_STATE_LOSTANDFOUND3: {
15241
+ if( p->laf.pInsert==0 ){
15242
+ recoverLostAndFound3Init(p);
15243
+ }
15244
+ if( SQLITE_DONE==recoverLostAndFound3Step(p) ){
15245
+ p->eState = RECOVER_STATE_SCHEMA2;
15246
+ }
15247
+ break;
15248
+ }
15249
+
15250
+ case RECOVER_STATE_SCHEMA2: {
15251
+ int rc = SQLITE_OK;
15252
+
15253
+ recoverWriteSchema2(p);
15254
+ p->eState = RECOVER_STATE_DONE;
15255
+
15256
+ /* If no error has occurred, commit the write transaction on the output
15257
+ ** database. Regardless of whether or not an error has occurred, make
15258
+ ** an attempt to end the read transaction on the input database. */
15259
+ recoverExec(p, p->dbOut, "COMMIT");
15260
+ rc = sqlite3_exec(p->dbIn, "END", 0, 0, 0);
15261
+ if( p->errCode==SQLITE_OK ) p->errCode = rc;
15262
+
15263
+ recoverSqlCallback(p, "PRAGMA writable_schema = off");
15264
+ recoverSqlCallback(p, "COMMIT");
15265
+ p->eState = RECOVER_STATE_DONE;
15266
+ recoverFinalCleanup(p);
15267
+ break;
15268
+ };
15269
+
15270
+ case RECOVER_STATE_DONE: {
15271
+ /* no-op */
15272
+ break;
15273
+ };
15274
+ }
15275
+}
15276
+
15277
+
15278
+/*
15279
+** This is a worker function that does the heavy lifting for both init
15280
+** functions:
15281
+**
15282
+** sqlite3_recover_init()
15283
+** sqlite3_recover_init_sql()
15284
+**
15285
+** All this function does is allocate space for the recover handle and
15286
+** take copies of the input parameters. All the real work is done within
15287
+** sqlite3_recover_run().
15288
+*/
15289
+sqlite3_recover *recoverInit(
15290
+ sqlite3* db,
15291
+ const char *zDb,
15292
+ const char *zUri, /* Output URI for _recover_init() */
15293
+ int (*xSql)(void*, const char*),/* SQL callback for _recover_init_sql() */
15294
+ void *pSqlCtx /* Context arg for _recover_init_sql() */
15295
+){
15296
+ sqlite3_recover *pRet = 0;
15297
+ int nDb = 0;
15298
+ int nUri = 0;
15299
+ int nByte = 0;
15300
+
15301
+ if( zDb==0 ){ zDb = "main"; }
15302
+
15303
+ nDb = recoverStrlen(zDb);
15304
+ nUri = recoverStrlen(zUri);
15305
+
15306
+ nByte = sizeof(sqlite3_recover) + nDb+1 + nUri+1;
15307
+ pRet = (sqlite3_recover*)sqlite3_malloc(nByte);
15308
+ if( pRet ){
15309
+ memset(pRet, 0, nByte);
15310
+ pRet->dbIn = db;
15311
+ pRet->zDb = (char*)&pRet[1];
15312
+ pRet->zUri = &pRet->zDb[nDb+1];
15313
+ memcpy(pRet->zDb, zDb, nDb);
15314
+ if( nUri>0 && zUri ) memcpy(pRet->zUri, zUri, nUri);
15315
+ pRet->xSql = xSql;
15316
+ pRet->pSqlCtx = pSqlCtx;
15317
+ pRet->bRecoverRowid = RECOVER_ROWID_DEFAULT;
15318
+ }
15319
+
15320
+ return pRet;
15321
+}
15322
+
15323
+/*
15324
+** Initialize a recovery handle that creates a new database containing
15325
+** the recovered data.
15326
+*/
15327
+sqlite3_recover *sqlite3_recover_init(
15328
+ sqlite3* db,
15329
+ const char *zDb,
15330
+ const char *zUri
15331
+){
15332
+ return recoverInit(db, zDb, zUri, 0, 0);
15333
+}
15334
+
15335
+/*
15336
+** Initialize a recovery handle that returns recovered data in the
15337
+** form of SQL statements via a callback.
15338
+*/
15339
+sqlite3_recover *sqlite3_recover_init_sql(
15340
+ sqlite3* db,
15341
+ const char *zDb,
15342
+ int (*xSql)(void*, const char*),
15343
+ void *pSqlCtx
15344
+){
15345
+ return recoverInit(db, zDb, 0, xSql, pSqlCtx);
15346
+}
15347
+
15348
+/*
15349
+** Return the handle error message, if any.
15350
+*/
15351
+const char *sqlite3_recover_errmsg(sqlite3_recover *p){
15352
+ return (p && p->errCode!=SQLITE_NOMEM) ? p->zErrMsg : "out of memory";
15353
+}
15354
+
15355
+/*
15356
+** Return the handle error code.
15357
+*/
15358
+int sqlite3_recover_errcode(sqlite3_recover *p){
15359
+ return p ? p->errCode : SQLITE_NOMEM;
15360
+}
15361
+
15362
+/*
15363
+** Configure the handle.
15364
+*/
15365
+int sqlite3_recover_config(sqlite3_recover *p, int op, void *pArg){
15366
+ int rc = SQLITE_OK;
15367
+ if( p==0 ){
15368
+ rc = SQLITE_NOMEM;
15369
+ }else if( p->eState!=RECOVER_STATE_INIT ){
15370
+ rc = SQLITE_MISUSE;
15371
+ }else{
15372
+ switch( op ){
15373
+ case 789:
15374
+ /* This undocumented magic configuration option is used to set the
15375
+ ** name of the auxiliary database that is ATTACH-ed to the database
15376
+ ** connection and used to hold state information during the
15377
+ ** recovery process. This option is for debugging use only and
15378
+ ** is subject to change or removal at any time. */
15379
+ sqlite3_free(p->zStateDb);
15380
+ p->zStateDb = recoverMPrintf(p, "%s", (char*)pArg);
15381
+ break;
15382
+
15383
+ case SQLITE_RECOVER_LOST_AND_FOUND: {
15384
+ const char *zArg = (const char*)pArg;
15385
+ sqlite3_free(p->zLostAndFound);
15386
+ if( zArg ){
15387
+ p->zLostAndFound = recoverMPrintf(p, "%s", zArg);
15388
+ }else{
15389
+ p->zLostAndFound = 0;
15390
+ }
15391
+ break;
15392
+ }
15393
+
15394
+ case SQLITE_RECOVER_FREELIST_CORRUPT:
15395
+ p->bFreelistCorrupt = *(int*)pArg;
15396
+ break;
15397
+
15398
+ case SQLITE_RECOVER_ROWIDS:
15399
+ p->bRecoverRowid = *(int*)pArg;
15400
+ break;
15401
+
15402
+ case SQLITE_RECOVER_SLOWINDEXES:
15403
+ p->bSlowIndexes = *(int*)pArg;
15404
+ break;
15405
+
15406
+ default:
15407
+ rc = SQLITE_NOTFOUND;
15408
+ break;
15409
+ }
15410
+ }
15411
+
15412
+ return rc;
15413
+}
15414
+
15415
+/*
15416
+** Do a unit of work towards the recovery job. Return SQLITE_OK if
15417
+** no error has occurred but database recovery is not finished, SQLITE_DONE
15418
+** if database recovery has been successfully completed, or an SQLite
15419
+** error code if an error has occurred.
15420
+*/
15421
+int sqlite3_recover_step(sqlite3_recover *p){
15422
+ if( p==0 ) return SQLITE_NOMEM;
15423
+ if( p->errCode==SQLITE_OK ) recoverStep(p);
15424
+ if( p->eState==RECOVER_STATE_DONE && p->errCode==SQLITE_OK ){
15425
+ return SQLITE_DONE;
15426
+ }
15427
+ return p->errCode;
15428
+}
15429
+
15430
+/*
15431
+** Do the configured recovery operation. Return SQLITE_OK if successful, or
15432
+** else an SQLite error code.
15433
+*/
15434
+int sqlite3_recover_run(sqlite3_recover *p){
15435
+ while( SQLITE_OK==sqlite3_recover_step(p) );
15436
+ return sqlite3_recover_errcode(p);
15437
+}
15438
+
15439
+
15440
+/*
15441
+** Free all resources associated with the recover handle passed as the only
15442
+** argument. The results of using a handle with any sqlite3_recover_**
15443
+** API function after it has been passed to this function are undefined.
15444
+**
15445
+** A copy of the value returned by the first call made to sqlite3_recover_run()
15446
+** on this handle is returned, or SQLITE_OK if sqlite3_recover_run() has
15447
+** not been called on this handle.
15448
+*/
15449
+int sqlite3_recover_finish(sqlite3_recover *p){
15450
+ int rc;
15451
+ if( p==0 ){
15452
+ rc = SQLITE_NOMEM;
15453
+ }else{
15454
+ recoverFinalCleanup(p);
15455
+ if( p->bCloseTransaction && sqlite3_get_autocommit(p->dbIn)==0 ){
15456
+ rc = sqlite3_exec(p->dbIn, "END", 0, 0, 0);
15457
+ if( p->errCode==SQLITE_OK ) p->errCode = rc;
15458
+ }
15459
+ rc = p->errCode;
15460
+ sqlite3_free(p->zErrMsg);
15461
+ sqlite3_free(p->zStateDb);
15462
+ sqlite3_free(p->zLostAndFound);
15463
+ sqlite3_free(p->pPage1Cache);
15464
+ sqlite3_free(p);
15465
+ }
15466
+ return rc;
15467
+}
15468
+
15469
+#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
15470
+
15471
+/************************* End ../ext/recover/sqlite3recover.c ********************/
1226115472
#endif
1226215473
1226315474
#if defined(SQLITE_ENABLE_SESSION)
1226415475
/*
1226515476
** State information for a single open session
@@ -13853,10 +17064,11 @@
1385317064
if( len>78 ){
1385417065
len = 78;
1385517066
while( (zSql[len]&0xc0)==0x80 ) len--;
1385617067
}
1385717068
zCode = sqlite3_mprintf("%.*s", len, zSql);
17069
+ shell_check_oom(zCode);
1385817070
for(i=0; zCode[i]; i++){ if( IsSpace(zSql[i]) ) zCode[i] = ' '; }
1385917071
if( iOffset<25 ){
1386017072
zMsg = sqlite3_mprintf("\n %z\n %*s^--- error here", zCode, iOffset, "");
1386117073
}else{
1386217074
zMsg = sqlite3_mprintf("\n %z\n %*serror here ---^", zCode, iOffset-14, "");
@@ -15552,11 +18764,11 @@
1555218764
".clone NEWDB Clone data into NEWDB from the existing database",
1555318765
#endif
1555418766
".connection [close] [#] Open or close an auxiliary database connection",
1555518767
".databases List names and files of attached databases",
1555618768
".dbconfig ?op? ?val? List or change sqlite3_db_config() options",
15557
-#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
18769
+#if SQLITE_SHELL_HAVE_RECOVER
1555818770
".dbinfo ?DB? Show status information about the database",
1555918771
#endif
1556018772
".dump ?OBJECTS? Render database content as SQL",
1556118773
" Options:",
1556218774
" --data-only Output only INSERT statements",
@@ -15700,14 +18912,13 @@
1570018912
#ifndef SQLITE_SHELL_FIDDLE
1570118913
".quit Exit this program",
1570218914
".read FILE Read input from FILE or command output",
1570318915
" If FILE begins with \"|\", it is a command that generates the input.",
1570418916
#endif
15705
-#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
18917
+#if SQLITE_SHELL_HAVE_RECOVER
1570618918
".recover Recover as much data as possible from corrupt db.",
15707
- " --freelist-corrupt Assume the freelist is corrupt",
15708
- " --recovery-db NAME Store recovery metadata in database file NAME",
18919
+ " --ignore-freelist Ignore pages that appear to be on db freelist",
1570918920
" --lost-and-found TABLE Alternative name for the lost-and-found table",
1571018921
" --no-rowids Do not attempt to recover rowid values",
1571118922
" that are not also INTEGER PRIMARY KEYs",
1571218923
#endif
1571318924
#ifndef SQLITE_SHELL_FIDDLE
@@ -16315,11 +19526,11 @@
1631519526
sqlite3_series_init(p->db, 0, 0);
1631619527
#ifndef SQLITE_SHELL_FIDDLE
1631719528
sqlite3_fileio_init(p->db, 0, 0);
1631819529
sqlite3_completion_init(p->db, 0, 0);
1631919530
#endif
16320
-#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
19531
+#if SQLITE_SHELL_HAVE_RECOVER
1632119532
sqlite3_dbdata_init(p->db, 0, 0);
1632219533
#endif
1632319534
#ifdef SQLITE_HAVE_ZLIB
1632419535
if( !p->bSafeModePersist ){
1632519536
sqlite3_zipfile_init(p->db, 0, 0);
@@ -17200,12 +20411,11 @@
1720020411
sqlite3_free(zSchemaTab);
1720120412
sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
1720220413
utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion);
1720320414
return 0;
1720420415
}
17205
-#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE)
17206
- && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
20416
+#endif /* SQLITE_SHELL_HAVE_RECOVER */
1720720417
1720820418
/*
1720920419
** Print the current sqlite3_errmsg() value to stderr and return 1.
1721020420
*/
1721120421
static int shellDatabaseError(sqlite3 *db){
@@ -18474,403 +21684,56 @@
1847421684
}
1847521685
/* End of the ".archive" or ".ar" command logic
1847621686
*******************************************************************************/
1847721687
#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
1847821688
18479
-#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
18480
-/*
18481
-** If (*pRc) is not SQLITE_OK when this function is called, it is a no-op.
18482
-** Otherwise, the SQL statement or statements in zSql are executed using
18483
-** database connection db and the error code written to *pRc before
18484
-** this function returns.
18485
-*/
18486
-static void shellExec(sqlite3 *db, int *pRc, const char *zSql){
18487
- int rc = *pRc;
18488
- if( rc==SQLITE_OK ){
18489
- char *zErr = 0;
18490
- rc = sqlite3_exec(db, zSql, 0, 0, &zErr);
18491
- if( rc!=SQLITE_OK ){
18492
- raw_printf(stderr, "SQL error: %s\n", zErr);
18493
- }
18494
- sqlite3_free(zErr);
18495
- *pRc = rc;
18496
- }
18497
-}
18498
-
18499
-/*
18500
-** Like shellExec(), except that zFmt is a printf() style format string.
18501
-*/
18502
-static void shellExecPrintf(sqlite3 *db, int *pRc, const char *zFmt, ...){
18503
- char *z = 0;
18504
- if( *pRc==SQLITE_OK ){
18505
- va_list ap;
18506
- va_start(ap, zFmt);
18507
- z = sqlite3_vmprintf(zFmt, ap);
18508
- va_end(ap);
18509
- if( z==0 ){
18510
- *pRc = SQLITE_NOMEM;
18511
- }else{
18512
- shellExec(db, pRc, z);
18513
- }
18514
- sqlite3_free(z);
18515
- }
18516
-}
18517
-
18518
-/*
18519
-** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
18520
-** Otherwise, an attempt is made to allocate, zero and return a pointer
18521
-** to a buffer nByte bytes in size. If an OOM error occurs, *pRc is set
18522
-** to SQLITE_NOMEM and NULL returned.
18523
-*/
18524
-static void *shellMalloc(int *pRc, sqlite3_int64 nByte){
18525
- void *pRet = 0;
18526
- if( *pRc==SQLITE_OK ){
18527
- pRet = sqlite3_malloc64(nByte);
18528
- if( pRet==0 ){
18529
- *pRc = SQLITE_NOMEM;
18530
- }else{
18531
- memset(pRet, 0, nByte);
18532
- }
18533
- }
18534
- return pRet;
18535
-}
18536
-
18537
-/*
18538
-** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
18539
-** Otherwise, zFmt is treated as a printf() style string. The result of
18540
-** formatting it along with any trailing arguments is written into a
18541
-** buffer obtained from sqlite3_malloc(), and pointer to which is returned.
18542
-** It is the responsibility of the caller to eventually free this buffer
18543
-** using a call to sqlite3_free().
18544
-**
18545
-** If an OOM error occurs, (*pRc) is set to SQLITE_NOMEM and a NULL
18546
-** pointer returned.
18547
-*/
18548
-static char *shellMPrintf(int *pRc, const char *zFmt, ...){
18549
- char *z = 0;
18550
- if( *pRc==SQLITE_OK ){
18551
- va_list ap;
18552
- va_start(ap, zFmt);
18553
- z = sqlite3_vmprintf(zFmt, ap);
18554
- va_end(ap);
18555
- if( z==0 ){
18556
- *pRc = SQLITE_NOMEM;
18557
- }
18558
- }
18559
- return z;
18560
-}
18561
-
18562
-
18563
-/*
18564
-** When running the ".recover" command, each output table, and the special
18565
-** orphaned row table if it is required, is represented by an instance
18566
-** of the following struct.
18567
-*/
18568
-typedef struct RecoverTable RecoverTable;
18569
-struct RecoverTable {
18570
- char *zQuoted; /* Quoted version of table name */
18571
- int nCol; /* Number of columns in table */
18572
- char **azlCol; /* Array of column lists */
18573
- int iPk; /* Index of IPK column */
18574
-};
18575
-
18576
-/*
18577
-** Free a RecoverTable object allocated by recoverFindTable() or
18578
-** recoverOrphanTable().
18579
-*/
18580
-static void recoverFreeTable(RecoverTable *pTab){
18581
- if( pTab ){
18582
- sqlite3_free(pTab->zQuoted);
18583
- if( pTab->azlCol ){
18584
- int i;
18585
- for(i=0; i<=pTab->nCol; i++){
18586
- sqlite3_free(pTab->azlCol[i]);
18587
- }
18588
- sqlite3_free(pTab->azlCol);
18589
- }
18590
- sqlite3_free(pTab);
18591
- }
18592
-}
18593
-
18594
-/*
18595
-** This function is a no-op if (*pRc) is not SQLITE_OK when it is called.
18596
-** Otherwise, it allocates and returns a RecoverTable object based on the
18597
-** final four arguments passed to this function. It is the responsibility
18598
-** of the caller to eventually free the returned object using
18599
-** recoverFreeTable().
18600
-*/
18601
-static RecoverTable *recoverNewTable(
18602
- int *pRc, /* IN/OUT: Error code */
18603
- const char *zName, /* Name of table */
18604
- const char *zSql, /* CREATE TABLE statement */
18605
- int bIntkey,
18606
- int nCol
18607
-){
18608
- sqlite3 *dbtmp = 0; /* sqlite3 handle for testing CREATE TABLE */
18609
- int rc = *pRc;
18610
- RecoverTable *pTab = 0;
18611
-
18612
- pTab = (RecoverTable*)shellMalloc(&rc, sizeof(RecoverTable));
18613
- if( rc==SQLITE_OK ){
18614
- int nSqlCol = 0;
18615
- int bSqlIntkey = 0;
18616
- sqlite3_stmt *pStmt = 0;
18617
-
18618
- rc = sqlite3_open("", &dbtmp);
18619
- if( rc==SQLITE_OK ){
18620
- sqlite3_create_function(dbtmp, "shell_idquote", 1, SQLITE_UTF8, 0,
18621
- shellIdQuote, 0, 0);
18622
- }
18623
- if( rc==SQLITE_OK ){
18624
- rc = sqlite3_exec(dbtmp, "PRAGMA writable_schema = on", 0, 0, 0);
18625
- }
18626
- if( rc==SQLITE_OK ){
18627
- rc = sqlite3_exec(dbtmp, zSql, 0, 0, 0);
18628
- if( rc==SQLITE_ERROR ){
18629
- rc = SQLITE_OK;
18630
- goto finished;
18631
- }
18632
- }
18633
- shellPreparePrintf(dbtmp, &rc, &pStmt,
18634
- "SELECT count(*) FROM pragma_table_info(%Q)", zName
18635
- );
18636
- if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
18637
- nSqlCol = sqlite3_column_int(pStmt, 0);
18638
- }
18639
- shellFinalize(&rc, pStmt);
18640
-
18641
- if( rc!=SQLITE_OK || nSqlCol<nCol ){
18642
- goto finished;
18643
- }
18644
-
18645
- shellPreparePrintf(dbtmp, &rc, &pStmt,
18646
- "SELECT ("
18647
- " SELECT substr(data,1,1)==X'0D' FROM sqlite_dbpage WHERE pgno=rootpage"
18648
- ") FROM sqlite_schema WHERE name = %Q", zName
18649
- );
18650
- if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
18651
- bSqlIntkey = sqlite3_column_int(pStmt, 0);
18652
- }
18653
- shellFinalize(&rc, pStmt);
18654
-
18655
- if( bIntkey==bSqlIntkey ){
18656
- int i;
18657
- const char *zPk = "_rowid_";
18658
- sqlite3_stmt *pPkFinder = 0;
18659
-
18660
- /* If this is an intkey table and there is an INTEGER PRIMARY KEY,
18661
- ** set zPk to the name of the PK column, and pTab->iPk to the index
18662
- ** of the column, where columns are 0-numbered from left to right.
18663
- ** Or, if this is a WITHOUT ROWID table or if there is no IPK column,
18664
- ** leave zPk as "_rowid_" and pTab->iPk at -2. */
18665
- pTab->iPk = -2;
18666
- if( bIntkey ){
18667
- shellPreparePrintf(dbtmp, &rc, &pPkFinder,
18668
- "SELECT cid, name FROM pragma_table_info(%Q) "
18669
- " WHERE pk=1 AND type='integer' COLLATE nocase"
18670
- " AND NOT EXISTS (SELECT cid FROM pragma_table_info(%Q) WHERE pk=2)"
18671
- , zName, zName
18672
- );
18673
- if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPkFinder) ){
18674
- pTab->iPk = sqlite3_column_int(pPkFinder, 0);
18675
- zPk = (const char*)sqlite3_column_text(pPkFinder, 1);
18676
- if( zPk==0 ){ zPk = "_"; /* Defensive. Should never happen */ }
18677
- }
18678
- }
18679
-
18680
- pTab->zQuoted = shellMPrintf(&rc, "\"%w\"", zName);
18681
- pTab->azlCol = (char**)shellMalloc(&rc, sizeof(char*) * (nSqlCol+1));
18682
- pTab->nCol = nSqlCol;
18683
-
18684
- if( bIntkey ){
18685
- pTab->azlCol[0] = shellMPrintf(&rc, "\"%w\"", zPk);
18686
- }else{
18687
- pTab->azlCol[0] = shellMPrintf(&rc, "");
18688
- }
18689
- i = 1;
18690
- shellPreparePrintf(dbtmp, &rc, &pStmt,
18691
- "SELECT %Q || group_concat(shell_idquote(name), ', ') "
18692
- " FILTER (WHERE cid!=%d) OVER (ORDER BY %s cid) "
18693
- "FROM pragma_table_info(%Q)",
18694
- bIntkey ? ", " : "", pTab->iPk,
18695
- bIntkey ? "" : "(CASE WHEN pk=0 THEN 1000000 ELSE pk END), ",
18696
- zName
18697
- );
18698
- while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
18699
- const char *zText = (const char*)sqlite3_column_text(pStmt, 0);
18700
- pTab->azlCol[i] = shellMPrintf(&rc, "%s%s", pTab->azlCol[0], zText);
18701
- i++;
18702
- }
18703
- shellFinalize(&rc, pStmt);
18704
-
18705
- shellFinalize(&rc, pPkFinder);
18706
- }
18707
- }
18708
-
18709
- finished:
18710
- sqlite3_close(dbtmp);
18711
- *pRc = rc;
18712
- if( rc!=SQLITE_OK || (pTab && pTab->zQuoted==0) ){
18713
- recoverFreeTable(pTab);
18714
- pTab = 0;
18715
- }
18716
- return pTab;
18717
-}
18718
-
18719
-/*
18720
-** This function is called to search the schema recovered from the
18721
-** sqlite_schema table of the (possibly) corrupt database as part
18722
-** of a ".recover" command. Specifically, for a table with root page
18723
-** iRoot and at least nCol columns. Additionally, if bIntkey is 0, the
18724
-** table must be a WITHOUT ROWID table, or if non-zero, not one of
18725
-** those.
18726
-**
18727
-** If a table is found, a (RecoverTable*) object is returned. Or, if
18728
-** no such table is found, but bIntkey is false and iRoot is the
18729
-** root page of an index in the recovered schema, then (*pbNoop) is
18730
-** set to true and NULL returned. Or, if there is no such table or
18731
-** index, NULL is returned and (*pbNoop) set to 0, indicating that
18732
-** the caller should write data to the orphans table.
18733
-*/
18734
-static RecoverTable *recoverFindTable(
18735
- ShellState *pState, /* Shell state object */
18736
- int *pRc, /* IN/OUT: Error code */
18737
- int iRoot, /* Root page of table */
18738
- int bIntkey, /* True for an intkey table */
18739
- int nCol, /* Number of columns in table */
18740
- int *pbNoop /* OUT: True if iRoot is root of index */
18741
-){
18742
- sqlite3_stmt *pStmt = 0;
18743
- RecoverTable *pRet = 0;
18744
- int bNoop = 0;
18745
- const char *zSql = 0;
18746
- const char *zName = 0;
18747
-
18748
- /* Search the recovered schema for an object with root page iRoot. */
18749
- shellPreparePrintf(pState->db, pRc, &pStmt,
18750
- "SELECT type, name, sql FROM recovery.schema WHERE rootpage=%d", iRoot
18751
- );
18752
- while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
18753
- const char *zType = (const char*)sqlite3_column_text(pStmt, 0);
18754
- if( bIntkey==0 && sqlite3_stricmp(zType, "index")==0 ){
18755
- bNoop = 1;
18756
- break;
18757
- }
18758
- if( sqlite3_stricmp(zType, "table")==0 ){
18759
- zName = (const char*)sqlite3_column_text(pStmt, 1);
18760
- zSql = (const char*)sqlite3_column_text(pStmt, 2);
18761
- if( zName!=0 && zSql!=0 ){
18762
- pRet = recoverNewTable(pRc, zName, zSql, bIntkey, nCol);
18763
- break;
18764
- }
18765
- }
18766
- }
18767
-
18768
- shellFinalize(pRc, pStmt);
18769
- *pbNoop = bNoop;
18770
- return pRet;
18771
-}
18772
-
18773
-/*
18774
-** Return a RecoverTable object representing the orphans table.
18775
-*/
18776
-static RecoverTable *recoverOrphanTable(
18777
- ShellState *pState, /* Shell state object */
18778
- int *pRc, /* IN/OUT: Error code */
18779
- const char *zLostAndFound, /* Base name for orphans table */
18780
- int nCol /* Number of user data columns */
18781
-){
18782
- RecoverTable *pTab = 0;
18783
- if( nCol>=0 && *pRc==SQLITE_OK ){
18784
- int i;
18785
-
18786
- /* This block determines the name of the orphan table. The prefered
18787
- ** name is zLostAndFound. But if that clashes with another name
18788
- ** in the recovered schema, try zLostAndFound_0, zLostAndFound_1
18789
- ** and so on until a non-clashing name is found. */
18790
- int iTab = 0;
18791
- char *zTab = shellMPrintf(pRc, "%s", zLostAndFound);
18792
- sqlite3_stmt *pTest = 0;
18793
- shellPrepare(pState->db, pRc,
18794
- "SELECT 1 FROM recovery.schema WHERE name=?", &pTest
18795
- );
18796
- if( pTest ) sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
18797
- while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pTest) ){
18798
- shellReset(pRc, pTest);
18799
- sqlite3_free(zTab);
18800
- zTab = shellMPrintf(pRc, "%s_%d", zLostAndFound, iTab++);
18801
- sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
18802
- }
18803
- shellFinalize(pRc, pTest);
18804
-
18805
- pTab = (RecoverTable*)shellMalloc(pRc, sizeof(RecoverTable));
18806
- if( pTab ){
18807
- pTab->zQuoted = shellMPrintf(pRc, "\"%w\"", zTab);
18808
- pTab->nCol = nCol;
18809
- pTab->iPk = -2;
18810
- if( nCol>0 ){
18811
- pTab->azlCol = (char**)shellMalloc(pRc, sizeof(char*) * (nCol+1));
18812
- if( pTab->azlCol ){
18813
- pTab->azlCol[nCol] = shellMPrintf(pRc, "");
18814
- for(i=nCol-1; i>=0; i--){
18815
- pTab->azlCol[i] = shellMPrintf(pRc, "%s, NULL", pTab->azlCol[i+1]);
18816
- }
18817
- }
18818
- }
18819
-
18820
- if( *pRc!=SQLITE_OK ){
18821
- recoverFreeTable(pTab);
18822
- pTab = 0;
18823
- }else{
18824
- raw_printf(pState->out,
18825
- "CREATE TABLE %s(rootpgno INTEGER, "
18826
- "pgno INTEGER, nfield INTEGER, id INTEGER", pTab->zQuoted
18827
- );
18828
- for(i=0; i<nCol; i++){
18829
- raw_printf(pState->out, ", c%d", i);
18830
- }
18831
- raw_printf(pState->out, ");\n");
18832
- }
18833
- }
18834
- sqlite3_free(zTab);
18835
- }
18836
- return pTab;
21689
+#if SQLITE_SHELL_HAVE_RECOVER
21690
+
21691
+/*
21692
+** This function is used as a callback by the recover extension. Simply
21693
+** print the supplied SQL statement to stdout.
21694
+*/
21695
+static int recoverSqlCb(void *pCtx, const char *zSql){
21696
+ ShellState *pState = (ShellState*)pCtx;
21697
+ utf8_printf(pState->out, "%s;\n", zSql);
21698
+ return SQLITE_OK;
1883721699
}
1883821700
1883921701
/*
1884021702
** This function is called to recover data from the database. A script
1884121703
** to construct a new database containing all recovered data is output
1884221704
** on stream pState->out.
1884321705
*/
1884421706
static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){
1884521707
int rc = SQLITE_OK;
18846
- sqlite3_stmt *pLoop = 0; /* Loop through all root pages */
18847
- sqlite3_stmt *pPages = 0; /* Loop through all pages in a group */
18848
- sqlite3_stmt *pCells = 0; /* Loop through all cells in a page */
18849
- const char *zRecoveryDb = ""; /* Name of "recovery" database */
18850
- const char *zLostAndFound = "lost_and_found";
18851
- int i;
18852
- int nOrphan = -1;
18853
- RecoverTable *pOrphan = 0;
18854
-
18855
- int bFreelist = 1; /* 0 if --freelist-corrupt is specified */
21708
+ const char *zRecoveryDb = ""; /* Name of "recovery" database. Debug only */
21709
+ const char *zLAF = "lost_and_found";
21710
+ int bFreelist = 1; /* 0 if --ignore-freelist is specified */
1885621711
int bRowids = 1; /* 0 if --no-rowids */
21712
+ sqlite3_recover *p = 0;
21713
+ int i = 0;
21714
+
1885721715
for(i=1; i<nArg; i++){
1885821716
char *z = azArg[i];
1885921717
int n;
1886021718
if( z[0]=='-' && z[1]=='-' ) z++;
1886121719
n = strlen30(z);
18862
- if( n<=17 && memcmp("-freelist-corrupt", z, n)==0 ){
21720
+ if( n<=17 && memcmp("-ignore-freelist", z, n)==0 ){
1886321721
bFreelist = 0;
1886421722
}else
1886521723
if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){
21724
+ /* This option determines the name of the ATTACH-ed database used
21725
+ ** internally by the recovery extension. The default is "" which
21726
+ ** means to use a temporary database that is automatically deleted
21727
+ ** when closed. This option is undocumented and might disappear at
21728
+ ** any moment. */
1886621729
i++;
1886721730
zRecoveryDb = azArg[i];
1886821731
}else
1886921732
if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){
1887021733
i++;
18871
- zLostAndFound = azArg[i];
21734
+ zLAF = azArg[i];
1887221735
}else
1887321736
if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
1887421737
bRowids = 0;
1887521738
}
1887621739
else{
@@ -18878,285 +21741,29 @@
1887821741
showHelp(pState->out, azArg[0]);
1887921742
return 1;
1888021743
}
1888121744
}
1888221745
18883
- shellExecPrintf(pState->db, &rc,
18884
- /* Attach an in-memory database named 'recovery'. Create an indexed
18885
- ** cache of the sqlite_dbptr virtual table. */
18886
- "PRAGMA writable_schema = on;"
18887
- "ATTACH %Q AS recovery;"
18888
- "DROP TABLE IF EXISTS recovery.dbptr;"
18889
- "DROP TABLE IF EXISTS recovery.freelist;"
18890
- "DROP TABLE IF EXISTS recovery.map;"
18891
- "DROP TABLE IF EXISTS recovery.schema;"
18892
- "CREATE TABLE recovery.freelist(pgno INTEGER PRIMARY KEY);", zRecoveryDb
18893
- );
18894
-
18895
- if( bFreelist ){
18896
- shellExec(pState->db, &rc,
18897
- "WITH trunk(pgno) AS ("
18898
- " SELECT shell_int32("
18899
- " (SELECT data FROM sqlite_dbpage WHERE pgno=1), 8) AS x "
18900
- " WHERE x>0"
18901
- " UNION"
18902
- " SELECT shell_int32("
18903
- " (SELECT data FROM sqlite_dbpage WHERE pgno=trunk.pgno), 0) AS x "
18904
- " FROM trunk WHERE x>0"
18905
- "),"
18906
- "freelist(data, n, freepgno) AS ("
18907
- " SELECT data, min(16384, shell_int32(data, 1)-1), t.pgno "
18908
- " FROM trunk t, sqlite_dbpage s WHERE s.pgno=t.pgno"
18909
- " UNION ALL"
18910
- " SELECT data, n-1, shell_int32(data, 2+n) "
18911
- " FROM freelist WHERE n>=0"
18912
- ")"
18913
- "REPLACE INTO recovery.freelist SELECT freepgno FROM freelist;"
18914
- );
18915
- }
18916
-
18917
- /* If this is an auto-vacuum database, add all pointer-map pages to
18918
- ** the freelist table. Do this regardless of whether or not
18919
- ** --freelist-corrupt was specified. */
18920
- shellExec(pState->db, &rc,
18921
- "WITH ptrmap(pgno) AS ("
18922
- " SELECT 2 WHERE shell_int32("
18923
- " (SELECT data FROM sqlite_dbpage WHERE pgno=1), 13"
18924
- " )"
18925
- " UNION ALL "
18926
- " SELECT pgno+1+(SELECT page_size FROM pragma_page_size)/5 AS pp "
18927
- " FROM ptrmap WHERE pp<=(SELECT page_count FROM pragma_page_count)"
18928
- ")"
18929
- "REPLACE INTO recovery.freelist SELECT pgno FROM ptrmap"
18930
- );
18931
-
18932
- shellExec(pState->db, &rc,
18933
- "CREATE TABLE recovery.dbptr("
18934
- " pgno, child, PRIMARY KEY(child, pgno)"
18935
- ") WITHOUT ROWID;"
18936
- "INSERT OR IGNORE INTO recovery.dbptr(pgno, child) "
18937
- " SELECT * FROM sqlite_dbptr"
18938
- " WHERE pgno NOT IN freelist AND child NOT IN freelist;"
18939
-
18940
- /* Delete any pointer to page 1. This ensures that page 1 is considered
18941
- ** a root page, regardless of how corrupt the db is. */
18942
- "DELETE FROM recovery.dbptr WHERE child = 1;"
18943
-
18944
- /* Delete all pointers to any pages that have more than one pointer
18945
- ** to them. Such pages will be treated as root pages when recovering
18946
- ** data. */
18947
- "DELETE FROM recovery.dbptr WHERE child IN ("
18948
- " SELECT child FROM recovery.dbptr GROUP BY child HAVING count(*)>1"
18949
- ");"
18950
-
18951
- /* Create the "map" table that will (eventually) contain instructions
18952
- ** for dealing with each page in the db that contains one or more
18953
- ** records. */
18954
- "CREATE TABLE recovery.map("
18955
- "pgno INTEGER PRIMARY KEY, maxlen INT, intkey, root INT"
18956
- ");"
18957
-
18958
- /* Populate table [map]. If there are circular loops of pages in the
18959
- ** database, the following adds all pages in such a loop to the map
18960
- ** as individual root pages. This could be handled better. */
18961
- "WITH pages(i, maxlen) AS ("
18962
- " SELECT page_count, ("
18963
- " SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=page_count"
18964
- " ) FROM pragma_page_count WHERE page_count>0"
18965
- " UNION ALL"
18966
- " SELECT i-1, ("
18967
- " SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=i-1"
18968
- " ) FROM pages WHERE i>=2"
18969
- ")"
18970
- "INSERT INTO recovery.map(pgno, maxlen, intkey, root) "
18971
- " SELECT i, maxlen, NULL, ("
18972
- " WITH p(orig, pgno, parent) AS ("
18973
- " SELECT 0, i, (SELECT pgno FROM recovery.dbptr WHERE child=i)"
18974
- " UNION "
18975
- " SELECT i, p.parent, "
18976
- " (SELECT pgno FROM recovery.dbptr WHERE child=p.parent) FROM p"
18977
- " )"
18978
- " SELECT pgno FROM p WHERE (parent IS NULL OR pgno = orig)"
18979
- ") "
18980
- "FROM pages WHERE maxlen IS NOT NULL AND i NOT IN freelist;"
18981
- "UPDATE recovery.map AS o SET intkey = ("
18982
- " SELECT substr(data, 1, 1)==X'0D' FROM sqlite_dbpage WHERE pgno=o.pgno"
18983
- ");"
18984
-
18985
- /* Extract data from page 1 and any linked pages into table
18986
- ** recovery.schema. With the same schema as an sqlite_schema table. */
18987
- "CREATE TABLE recovery.schema(type, name, tbl_name, rootpage, sql);"
18988
- "INSERT INTO recovery.schema SELECT "
18989
- " max(CASE WHEN field=0 THEN value ELSE NULL END),"
18990
- " max(CASE WHEN field=1 THEN value ELSE NULL END),"
18991
- " max(CASE WHEN field=2 THEN value ELSE NULL END),"
18992
- " max(CASE WHEN field=3 THEN value ELSE NULL END),"
18993
- " max(CASE WHEN field=4 THEN value ELSE NULL END)"
18994
- "FROM sqlite_dbdata WHERE pgno IN ("
18995
- " SELECT pgno FROM recovery.map WHERE root=1"
18996
- ")"
18997
- "GROUP BY pgno, cell;"
18998
- "CREATE INDEX recovery.schema_rootpage ON schema(rootpage);"
18999
- );
19000
-
19001
- /* Open a transaction, then print out all non-virtual, non-"sqlite_%"
19002
- ** CREATE TABLE statements that extracted from the existing schema. */
19003
- if( rc==SQLITE_OK ){
19004
- sqlite3_stmt *pStmt = 0;
19005
- /* ".recover" might output content in an order which causes immediate
19006
- ** foreign key constraints to be violated. So disable foreign-key
19007
- ** constraint enforcement to prevent problems when running the output
19008
- ** script. */
19009
- raw_printf(pState->out, "PRAGMA foreign_keys=OFF;\n");
19010
- raw_printf(pState->out, "BEGIN;\n");
19011
- raw_printf(pState->out, "PRAGMA writable_schema = on;\n");
19012
- shellPrepare(pState->db, &rc,
19013
- "SELECT sql FROM recovery.schema "
19014
- "WHERE type='table' AND sql LIKE 'create table%'", &pStmt
19015
- );
19016
- while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
19017
- const char *zCreateTable = (const char*)sqlite3_column_text(pStmt, 0);
19018
- raw_printf(pState->out, "CREATE TABLE IF NOT EXISTS %s;\n",
19019
- &zCreateTable[12]
19020
- );
19021
- }
19022
- shellFinalize(&rc, pStmt);
19023
- }
19024
-
19025
- /* Figure out if an orphan table will be required. And if so, how many
19026
- ** user columns it should contain */
19027
- shellPrepare(pState->db, &rc,
19028
- "SELECT coalesce(max(maxlen), -2) FROM recovery.map WHERE root>1"
19029
- , &pLoop
19030
- );
19031
- if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
19032
- nOrphan = sqlite3_column_int(pLoop, 0);
19033
- }
19034
- shellFinalize(&rc, pLoop);
19035
- pLoop = 0;
19036
-
19037
- shellPrepare(pState->db, &rc,
19038
- "SELECT pgno FROM recovery.map WHERE root=?", &pPages
19039
- );
19040
-
19041
- shellPrepare(pState->db, &rc,
19042
- "SELECT max(field), group_concat(shell_escape_crnl(quote"
19043
- "(case when (? AND field<0) then NULL else value end)"
19044
- "), ', ')"
19045
- ", min(field) "
19046
- "FROM sqlite_dbdata WHERE pgno = ? AND field != ?"
19047
- "GROUP BY cell", &pCells
19048
- );
19049
-
19050
- /* Loop through each root page. */
19051
- shellPrepare(pState->db, &rc,
19052
- "SELECT root, intkey, max(maxlen) FROM recovery.map"
19053
- " WHERE root>1 GROUP BY root, intkey ORDER BY root=("
19054
- " SELECT rootpage FROM recovery.schema WHERE name='sqlite_sequence'"
19055
- ")", &pLoop
19056
- );
19057
- while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
19058
- int iRoot = sqlite3_column_int(pLoop, 0);
19059
- int bIntkey = sqlite3_column_int(pLoop, 1);
19060
- int nCol = sqlite3_column_int(pLoop, 2);
19061
- int bNoop = 0;
19062
- RecoverTable *pTab;
19063
-
19064
- assert( bIntkey==0 || bIntkey==1 );
19065
- pTab = recoverFindTable(pState, &rc, iRoot, bIntkey, nCol, &bNoop);
19066
- if( bNoop || rc ) continue;
19067
- if( pTab==0 ){
19068
- if( pOrphan==0 ){
19069
- pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
19070
- }
19071
- pTab = pOrphan;
19072
- if( pTab==0 ) break;
19073
- }
19074
-
19075
- if( 0==sqlite3_stricmp(pTab->zQuoted, "\"sqlite_sequence\"") ){
19076
- raw_printf(pState->out, "DELETE FROM sqlite_sequence;\n");
19077
- }
19078
- sqlite3_bind_int(pPages, 1, iRoot);
19079
- if( bRowids==0 && pTab->iPk<0 ){
19080
- sqlite3_bind_int(pCells, 1, 1);
19081
- }else{
19082
- sqlite3_bind_int(pCells, 1, 0);
19083
- }
19084
- sqlite3_bind_int(pCells, 3, pTab->iPk);
19085
-
19086
- while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPages) ){
19087
- int iPgno = sqlite3_column_int(pPages, 0);
19088
- sqlite3_bind_int(pCells, 2, iPgno);
19089
- while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pCells) ){
19090
- int nField = sqlite3_column_int(pCells, 0);
19091
- int iMin = sqlite3_column_int(pCells, 2);
19092
- const char *zVal = (const char*)sqlite3_column_text(pCells, 1);
19093
-
19094
- RecoverTable *pTab2 = pTab;
19095
- if( pTab!=pOrphan && (iMin<0)!=bIntkey ){
19096
- if( pOrphan==0 ){
19097
- pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
19098
- }
19099
- pTab2 = pOrphan;
19100
- if( pTab2==0 ) break;
19101
- }
19102
-
19103
- nField = nField+1;
19104
- if( pTab2==pOrphan ){
19105
- raw_printf(pState->out,
19106
- "INSERT INTO %s VALUES(%d, %d, %d, %s%s%s);\n",
19107
- pTab2->zQuoted, iRoot, iPgno, nField,
19108
- iMin<0 ? "" : "NULL, ", zVal, pTab2->azlCol[nField]
19109
- );
19110
- }else{
19111
- raw_printf(pState->out, "INSERT INTO %s(%s) VALUES( %s );\n",
19112
- pTab2->zQuoted, pTab2->azlCol[nField], zVal
19113
- );
19114
- }
19115
- }
19116
- shellReset(&rc, pCells);
19117
- }
19118
- shellReset(&rc, pPages);
19119
- if( pTab!=pOrphan ) recoverFreeTable(pTab);
19120
- }
19121
- shellFinalize(&rc, pLoop);
19122
- shellFinalize(&rc, pPages);
19123
- shellFinalize(&rc, pCells);
19124
- recoverFreeTable(pOrphan);
19125
-
19126
- /* The rest of the schema */
19127
- if( rc==SQLITE_OK ){
19128
- sqlite3_stmt *pStmt = 0;
19129
- shellPrepare(pState->db, &rc,
19130
- "SELECT sql, name FROM recovery.schema "
19131
- "WHERE sql NOT LIKE 'create table%'", &pStmt
19132
- );
19133
- while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
19134
- const char *zSql = (const char*)sqlite3_column_text(pStmt, 0);
19135
- if( sqlite3_strnicmp(zSql, "create virt", 11)==0 ){
19136
- const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
19137
- char *zPrint = shellMPrintf(&rc,
19138
- "INSERT INTO sqlite_schema VALUES('table', %Q, %Q, 0, %Q)",
19139
- zName, zName, zSql
19140
- );
19141
- raw_printf(pState->out, "%s;\n", zPrint);
19142
- sqlite3_free(zPrint);
19143
- }else{
19144
- raw_printf(pState->out, "%s;\n", zSql);
19145
- }
19146
- }
19147
- shellFinalize(&rc, pStmt);
19148
- }
19149
-
19150
- if( rc==SQLITE_OK ){
19151
- raw_printf(pState->out, "PRAGMA writable_schema = off;\n");
19152
- raw_printf(pState->out, "COMMIT;\n");
19153
- }
19154
- sqlite3_exec(pState->db, "DETACH recovery", 0, 0, 0);
19155
- return rc;
19156
-}
19157
-#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
21746
+ p = sqlite3_recover_init_sql(
21747
+ pState->db, "main", recoverSqlCb, (void*)pState
21748
+ );
21749
+
21750
+ sqlite3_recover_config(p, 789, (void*)zRecoveryDb); /* Debug use only */
21751
+ sqlite3_recover_config(p, SQLITE_RECOVER_LOST_AND_FOUND, (void*)zLAF);
21752
+ sqlite3_recover_config(p, SQLITE_RECOVER_ROWIDS, (void*)&bRowids);
21753
+ sqlite3_recover_config(p, SQLITE_RECOVER_FREELIST_CORRUPT,(void*)&bFreelist);
21754
+
21755
+ sqlite3_recover_run(p);
21756
+ if( sqlite3_recover_errcode(p)!=SQLITE_OK ){
21757
+ const char *zErr = sqlite3_recover_errmsg(p);
21758
+ int errCode = sqlite3_recover_errcode(p);
21759
+ raw_printf(stderr, "sql error: %s (%d)\n", zErr, errCode);
21760
+ }
21761
+ rc = sqlite3_recover_finish(p);
21762
+ return rc;
21763
+}
21764
+#endif /* SQLITE_SHELL_HAVE_RECOVER */
1915821765
1915921766
1916021767
/*
1916121768
* zAutoColumn(zCol, &db, ?) => Maybe init db, add column zCol to it.
1916221769
* zAutoColumn(0, &db, ?) => (db!=0) Form columns spec for CREATE TABLE,
@@ -19744,20 +22351,20 @@
1974422351
utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]);
1974522352
utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");
1974622353
}
1974722354
}else
1974822355
19749
-#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
22356
+#if SQLITE_SHELL_HAVE_RECOVER
1975022357
if( c=='d' && n>=3 && cli_strncmp(azArg[0], "dbinfo", n)==0 ){
1975122358
rc = shell_dbinfo_command(p, nArg, azArg);
1975222359
}else
1975322360
1975422361
if( c=='r' && cli_strncmp(azArg[0], "recover", n)==0 ){
1975522362
open_db(p, 0);
1975622363
rc = recoverDatabaseCmd(p, nArg, azArg);
1975722364
}else
19758
-#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
22365
+#endif /* SQLITE_SHELL_HAVE_RECOVER */
1975922366
1976022367
if( c=='d' && cli_strncmp(azArg[0], "dump", n)==0 ){
1976122368
char *zLike = 0;
1976222369
char *zSql;
1976322370
int i;
1976422371
--- extsrc/shell.c
+++ extsrc/shell.c
@@ -32,10 +32,12 @@
32 */
33 #if (defined(_WIN32) || defined(WIN32)) && !defined(_CRT_SECURE_NO_WARNINGS)
34 /* This needs to come before any includes for MSVC compiler */
35 #define _CRT_SECURE_NO_WARNINGS
36 #endif
 
 
37
38 /*
39 ** Optionally #include a user-defined header, whereby compilation options
40 ** may be set prior to where they take effect, but after platform setup.
41 ** If SQLITE_CUSTOM_INCLUDE=? is defined, its value names the #include
@@ -565,10 +567,11 @@
565 */
566 static void utf8_width_print(FILE *pOut, int w, const char *zUtf){
567 int i;
568 int n;
569 int aw = w<0 ? -w : w;
 
570 for(i=n=0; zUtf[i]; i++){
571 if( (zUtf[i]&0xc0)!=0x80 ){
572 n++;
573 if( n==aw ){
574 do{ i++; }while( (zUtf[i]&0xc0)==0x80 );
@@ -6890,12 +6893,12 @@
6890 # define UINT16_TYPE unsigned short int
6891 # endif
6892 #endif
6893 /* typedef sqlite3_int64 i64; */
6894 /* typedef unsigned char u8; */
6895 typedef UINT32_TYPE u32; /* 4-byte unsigned integer */
6896 typedef UINT16_TYPE u16; /* 2-byte unsigned integer */
6897 #define MIN(a,b) ((a)<(b) ? (a) : (b))
6898
6899 #if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
6900 # define SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS 1
6901 #endif
@@ -11402,11 +11405,16 @@
11402 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
11403
11404 /************************* End ../ext/expert/sqlite3expert.c ********************/
11405
11406 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
11407 /************************* Begin ../ext/misc/dbdata.c ******************/
 
 
 
 
 
11408 /*
11409 ** 2019-04-17
11410 **
11411 ** The author disclaims copyright to this source code. In place of
11412 ** a legal notice, here is a blessing:
@@ -11476,19 +11484,23 @@
11476 ** );
11477 **
11478 ** It contains one entry for each b-tree pointer between a parent and
11479 ** child page in the database.
11480 */
 
11481 #if !defined(SQLITEINT_H)
11482 /* #include "sqlite3ext.h" */
11483
11484 /* typedef unsigned char u8; */
 
11485
11486 #endif
11487 SQLITE_EXTENSION_INIT1
11488 #include <string.h>
11489 #include <assert.h>
 
 
11490
11491 #define DBDATA_PADDING_BYTES 100
11492
11493 typedef struct DbdataTable DbdataTable;
11494 typedef struct DbdataCursor DbdataCursor;
@@ -11507,15 +11519,16 @@
11507 int szDb;
11508 sqlite3_int64 iRowid;
11509
11510 /* Only for the sqlite_dbdata table */
11511 u8 *pRec; /* Buffer containing current record */
11512 int nRec; /* Size of pRec[] in bytes */
11513 int nHdr; /* Size of header in bytes */
11514 int iField; /* Current field number */
11515 u8 *pHdrPtr;
11516 u8 *pPtr;
 
11517
11518 sqlite3_int64 iIntkey; /* Integer key value */
11519 };
11520
11521 /* Table object */
@@ -11704,18 +11717,18 @@
11704 }
11705
11706 /*
11707 ** Utility methods to decode 16 and 32-bit big-endian unsigned integers.
11708 */
11709 static unsigned int get_uint16(unsigned char *a){
11710 return (a[0]<<8)|a[1];
11711 }
11712 static unsigned int get_uint32(unsigned char *a){
11713 return ((unsigned int)a[0]<<24)
11714 | ((unsigned int)a[1]<<16)
11715 | ((unsigned int)a[2]<<8)
11716 | ((unsigned int)a[3]);
11717 }
11718
11719 /*
11720 ** Load page pgno from the database via the sqlite_dbpage virtual table.
11721 ** If successful, set (*ppPage) to point to a buffer containing the page
@@ -11726,57 +11739,72 @@
11726 ** Or, if an error occurs, set both (*ppPage) and (*pnPage) to 0 and
11727 ** return an SQLite error code.
11728 */
11729 static int dbdataLoadPage(
11730 DbdataCursor *pCsr, /* Cursor object */
11731 unsigned int pgno, /* Page number of page to load */
11732 u8 **ppPage, /* OUT: pointer to page buffer */
11733 int *pnPage /* OUT: Size of (*ppPage) in bytes */
11734 ){
11735 int rc2;
11736 int rc = SQLITE_OK;
11737 sqlite3_stmt *pStmt = pCsr->pStmt;
11738
11739 *ppPage = 0;
11740 *pnPage = 0;
11741 sqlite3_bind_int64(pStmt, 2, pgno);
11742 if( SQLITE_ROW==sqlite3_step(pStmt) ){
11743 int nCopy = sqlite3_column_bytes(pStmt, 0);
11744 if( nCopy>0 ){
11745 u8 *pPage;
11746 pPage = (u8*)sqlite3_malloc64(nCopy + DBDATA_PADDING_BYTES);
11747 if( pPage==0 ){
11748 rc = SQLITE_NOMEM;
11749 }else{
11750 const u8 *pCopy = sqlite3_column_blob(pStmt, 0);
11751 memcpy(pPage, pCopy, nCopy);
11752 memset(&pPage[nCopy], 0, DBDATA_PADDING_BYTES);
11753 }
11754 *ppPage = pPage;
11755 *pnPage = nCopy;
11756 }
11757 }
11758 rc2 = sqlite3_reset(pStmt);
11759 if( rc==SQLITE_OK ) rc = rc2;
 
 
11760
11761 return rc;
11762 }
11763
11764 /*
11765 ** Read a varint. Put the value in *pVal and return the number of bytes.
11766 */
11767 static int dbdataGetVarint(const u8 *z, sqlite3_int64 *pVal){
11768 sqlite3_int64 v = 0;
11769 int i;
11770 for(i=0; i<8; i++){
11771 v = (v<<7) + (z[i]&0x7f);
11772 if( (z[i]&0x80)==0 ){ *pVal = v; return i+1; }
11773 }
11774 v = (v<<8) + (z[i]&0xff);
11775 *pVal = v;
11776 return 9;
11777 }
 
 
 
 
 
 
 
 
 
 
 
 
 
11778
11779 /*
11780 ** Return the number of bytes of space used by an SQLite value of type
11781 ** eType.
11782 */
@@ -11810,10 +11838,11 @@
11810 ** Load a value of type eType from buffer pData and use it to set the
11811 ** result of context object pCtx.
11812 */
11813 static void dbdataValue(
11814 sqlite3_context *pCtx,
 
11815 int eType,
11816 u8 *pData,
11817 int nData
11818 ){
11819 if( eType>=0 && dbdataValueBytes(eType)<=nData ){
@@ -11854,11 +11883,23 @@
11854 }
11855
11856 default: {
11857 int n = ((eType-12) / 2);
11858 if( eType % 2 ){
11859 sqlite3_result_text(pCtx, (const char*)pData, n, SQLITE_TRANSIENT);
 
 
 
 
 
 
 
 
 
 
 
 
11860 }else{
11861 sqlite3_result_blob(pCtx, pData, n, SQLITE_TRANSIENT);
11862 }
11863 }
11864 }
@@ -11882,10 +11923,11 @@
11882 while( 1 ){
11883 if( pCsr->bOnePage==0 && pCsr->iPgno>pCsr->szDb ) return SQLITE_OK;
11884 rc = dbdataLoadPage(pCsr, pCsr->iPgno, &pCsr->aPage, &pCsr->nPage);
11885 if( rc!=SQLITE_OK ) return rc;
11886 if( pCsr->aPage ) break;
 
11887 pCsr->iPgno++;
11888 }
11889 pCsr->iCell = pTab->bPtr ? -2 : 0;
11890 pCsr->nCell = get_uint16(&pCsr->aPage[iOff+3]);
11891 }
@@ -11945,11 +11987,11 @@
11945
11946 /* Load the "byte of payload including overflow" field */
11947 if( bNextPage || iOff>pCsr->nPage ){
11948 bNextPage = 1;
11949 }else{
11950 iOff += dbdataGetVarint(&pCsr->aPage[iOff], &nPayload);
11951 }
11952
11953 /* If this is a leaf intkey cell, load the rowid */
11954 if( bHasRowid && !bNextPage && iOff<pCsr->nPage ){
11955 iOff += dbdataGetVarint(&pCsr->aPage[iOff], &pCsr->iIntkey);
@@ -11992,11 +12034,11 @@
11992 iOff += nLocal;
11993
11994 /* Load content from overflow pages */
11995 if( nPayload>nLocal ){
11996 sqlite3_int64 nRem = nPayload - nLocal;
11997 unsigned int pgnoOvfl = get_uint32(&pCsr->aPage[iOff]);
11998 while( nRem>0 ){
11999 u8 *aOvfl = 0;
12000 int nOvfl = 0;
12001 int nCopy;
12002 rc = dbdataLoadPage(pCsr, pgnoOvfl, &aOvfl, &nOvfl);
@@ -12012,11 +12054,12 @@
12012 pgnoOvfl = get_uint32(aOvfl);
12013 sqlite3_free(aOvfl);
12014 }
12015 }
12016
12017 iHdr = dbdataGetVarint(pCsr->pRec, &nHdr);
 
12018 pCsr->nHdr = nHdr;
12019 pCsr->pHdrPtr = &pCsr->pRec[iHdr];
12020 pCsr->pPtr = &pCsr->pRec[pCsr->nHdr];
12021 pCsr->iField = (bHasRowid ? -1 : 0);
12022 }
@@ -12026,11 +12069,11 @@
12026 if( pCsr->iField>0 ){
12027 sqlite3_int64 iType;
12028 if( pCsr->pHdrPtr>&pCsr->pRec[pCsr->nRec] ){
12029 bNextPage = 1;
12030 }else{
12031 pCsr->pHdrPtr += dbdataGetVarint(pCsr->pHdrPtr, &iType);
12032 pCsr->pPtr += dbdataValueBytes(iType);
12033 }
12034 }
12035 }
12036
@@ -12064,10 +12107,22 @@
12064 */
12065 static int dbdataEof(sqlite3_vtab_cursor *pCursor){
12066 DbdataCursor *pCsr = (DbdataCursor*)pCursor;
12067 return pCsr->aPage==0;
12068 }
 
 
 
 
 
 
 
 
 
 
 
 
12069
12070 /*
12071 ** Determine the size in pages of database zSchema (where zSchema is
12072 ** "main", "temp" or the name of an attached database) and set
12073 ** pCsr->szDb accordingly. If successful, return SQLITE_OK. Otherwise,
@@ -12075,23 +12130,48 @@
12075 */
12076 static int dbdataDbsize(DbdataCursor *pCsr, const char *zSchema){
12077 DbdataTable *pTab = (DbdataTable*)pCsr->base.pVtab;
12078 char *zSql = 0;
12079 int rc, rc2;
 
12080 sqlite3_stmt *pStmt = 0;
12081
12082 zSql = sqlite3_mprintf("PRAGMA %Q.page_count", zSchema);
 
 
 
 
12083 if( zSql==0 ) return SQLITE_NOMEM;
 
12084 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pStmt, 0);
12085 sqlite3_free(zSql);
12086 if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
12087 pCsr->szDb = sqlite3_column_int(pStmt, 0);
12088 }
12089 rc2 = sqlite3_finalize(pStmt);
12090 if( rc==SQLITE_OK ) rc = rc2;
12091 return rc;
12092 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12093
12094 /*
12095 ** xFilter method for sqlite_dbdata and sqlite_dbptr.
12096 */
12097 static int dbdataFilter(
@@ -12106,23 +12186,32 @@
12106
12107 dbdataResetCursor(pCsr);
12108 assert( pCsr->iPgno==1 );
12109 if( idxNum & 0x01 ){
12110 zSchema = (const char*)sqlite3_value_text(argv[0]);
 
12111 }
12112 if( idxNum & 0x02 ){
12113 pCsr->iPgno = sqlite3_value_int(argv[(idxNum & 0x01)]);
12114 pCsr->bOnePage = 1;
12115 }else{
12116 pCsr->nPage = dbdataDbsize(pCsr, zSchema);
12117 rc = dbdataDbsize(pCsr, zSchema);
12118 }
12119
12120 if( rc==SQLITE_OK ){
 
12121 if( pTab->pStmt ){
12122 pCsr->pStmt = pTab->pStmt;
12123 pTab->pStmt = 0;
 
 
 
 
 
 
 
 
12124 }else{
12125 rc = sqlite3_prepare_v2(pTab->db,
12126 "SELECT data FROM sqlite_dbpage(?) WHERE pgno=?", -1,
12127 &pCsr->pStmt, 0
12128 );
@@ -12131,17 +12220,24 @@
12131 if( rc==SQLITE_OK ){
12132 rc = sqlite3_bind_text(pCsr->pStmt, 1, zSchema, -1, SQLITE_TRANSIENT);
12133 }else{
12134 pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
12135 }
 
 
 
 
 
 
 
12136 if( rc==SQLITE_OK ){
12137 rc = dbdataNext(pCursor);
12138 }
12139 return rc;
12140 }
12141
12142 /*
12143 ** Return a column for the sqlite_dbdata or sqlite_dbptr table.
12144 */
12145 static int dbdataColumn(
12146 sqlite3_vtab_cursor *pCursor,
12147 sqlite3_context *ctx,
@@ -12183,13 +12279,14 @@
12183 case DBDATA_COLUMN_VALUE: {
12184 if( pCsr->iField<0 ){
12185 sqlite3_result_int64(ctx, pCsr->iIntkey);
12186 }else{
12187 sqlite3_int64 iType;
12188 dbdataGetVarint(pCsr->pHdrPtr, &iType);
12189 dbdataValue(
12190 ctx, iType, pCsr->pPtr, &pCsr->pRec[pCsr->nRec] - pCsr->pPtr
 
12191 );
12192 }
12193 break;
12194 }
12195 }
@@ -12255,11 +12352,3125 @@
12255 ){
12256 SQLITE_EXTENSION_INIT2(pApi);
12257 return sqlite3DbdataRegister(db);
12258 }
12259
12260 /************************* End ../ext/misc/dbdata.c ********************/
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12261 #endif
12262
12263 #if defined(SQLITE_ENABLE_SESSION)
12264 /*
12265 ** State information for a single open session
@@ -13853,10 +17064,11 @@
13853 if( len>78 ){
13854 len = 78;
13855 while( (zSql[len]&0xc0)==0x80 ) len--;
13856 }
13857 zCode = sqlite3_mprintf("%.*s", len, zSql);
 
13858 for(i=0; zCode[i]; i++){ if( IsSpace(zSql[i]) ) zCode[i] = ' '; }
13859 if( iOffset<25 ){
13860 zMsg = sqlite3_mprintf("\n %z\n %*s^--- error here", zCode, iOffset, "");
13861 }else{
13862 zMsg = sqlite3_mprintf("\n %z\n %*serror here ---^", zCode, iOffset-14, "");
@@ -15552,11 +18764,11 @@
15552 ".clone NEWDB Clone data into NEWDB from the existing database",
15553 #endif
15554 ".connection [close] [#] Open or close an auxiliary database connection",
15555 ".databases List names and files of attached databases",
15556 ".dbconfig ?op? ?val? List or change sqlite3_db_config() options",
15557 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
15558 ".dbinfo ?DB? Show status information about the database",
15559 #endif
15560 ".dump ?OBJECTS? Render database content as SQL",
15561 " Options:",
15562 " --data-only Output only INSERT statements",
@@ -15700,14 +18912,13 @@
15700 #ifndef SQLITE_SHELL_FIDDLE
15701 ".quit Exit this program",
15702 ".read FILE Read input from FILE or command output",
15703 " If FILE begins with \"|\", it is a command that generates the input.",
15704 #endif
15705 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
15706 ".recover Recover as much data as possible from corrupt db.",
15707 " --freelist-corrupt Assume the freelist is corrupt",
15708 " --recovery-db NAME Store recovery metadata in database file NAME",
15709 " --lost-and-found TABLE Alternative name for the lost-and-found table",
15710 " --no-rowids Do not attempt to recover rowid values",
15711 " that are not also INTEGER PRIMARY KEYs",
15712 #endif
15713 #ifndef SQLITE_SHELL_FIDDLE
@@ -16315,11 +19526,11 @@
16315 sqlite3_series_init(p->db, 0, 0);
16316 #ifndef SQLITE_SHELL_FIDDLE
16317 sqlite3_fileio_init(p->db, 0, 0);
16318 sqlite3_completion_init(p->db, 0, 0);
16319 #endif
16320 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
16321 sqlite3_dbdata_init(p->db, 0, 0);
16322 #endif
16323 #ifdef SQLITE_HAVE_ZLIB
16324 if( !p->bSafeModePersist ){
16325 sqlite3_zipfile_init(p->db, 0, 0);
@@ -17200,12 +20411,11 @@
17200 sqlite3_free(zSchemaTab);
17201 sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
17202 utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion);
17203 return 0;
17204 }
17205 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE)
17206 && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
17207
17208 /*
17209 ** Print the current sqlite3_errmsg() value to stderr and return 1.
17210 */
17211 static int shellDatabaseError(sqlite3 *db){
@@ -18474,403 +21684,56 @@
18474 }
18475 /* End of the ".archive" or ".ar" command logic
18476 *******************************************************************************/
18477 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
18478
18479 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
18480 /*
18481 ** If (*pRc) is not SQLITE_OK when this function is called, it is a no-op.
18482 ** Otherwise, the SQL statement or statements in zSql are executed using
18483 ** database connection db and the error code written to *pRc before
18484 ** this function returns.
18485 */
18486 static void shellExec(sqlite3 *db, int *pRc, const char *zSql){
18487 int rc = *pRc;
18488 if( rc==SQLITE_OK ){
18489 char *zErr = 0;
18490 rc = sqlite3_exec(db, zSql, 0, 0, &zErr);
18491 if( rc!=SQLITE_OK ){
18492 raw_printf(stderr, "SQL error: %s\n", zErr);
18493 }
18494 sqlite3_free(zErr);
18495 *pRc = rc;
18496 }
18497 }
18498
18499 /*
18500 ** Like shellExec(), except that zFmt is a printf() style format string.
18501 */
18502 static void shellExecPrintf(sqlite3 *db, int *pRc, const char *zFmt, ...){
18503 char *z = 0;
18504 if( *pRc==SQLITE_OK ){
18505 va_list ap;
18506 va_start(ap, zFmt);
18507 z = sqlite3_vmprintf(zFmt, ap);
18508 va_end(ap);
18509 if( z==0 ){
18510 *pRc = SQLITE_NOMEM;
18511 }else{
18512 shellExec(db, pRc, z);
18513 }
18514 sqlite3_free(z);
18515 }
18516 }
18517
18518 /*
18519 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
18520 ** Otherwise, an attempt is made to allocate, zero and return a pointer
18521 ** to a buffer nByte bytes in size. If an OOM error occurs, *pRc is set
18522 ** to SQLITE_NOMEM and NULL returned.
18523 */
18524 static void *shellMalloc(int *pRc, sqlite3_int64 nByte){
18525 void *pRet = 0;
18526 if( *pRc==SQLITE_OK ){
18527 pRet = sqlite3_malloc64(nByte);
18528 if( pRet==0 ){
18529 *pRc = SQLITE_NOMEM;
18530 }else{
18531 memset(pRet, 0, nByte);
18532 }
18533 }
18534 return pRet;
18535 }
18536
18537 /*
18538 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
18539 ** Otherwise, zFmt is treated as a printf() style string. The result of
18540 ** formatting it along with any trailing arguments is written into a
18541 ** buffer obtained from sqlite3_malloc(), and pointer to which is returned.
18542 ** It is the responsibility of the caller to eventually free this buffer
18543 ** using a call to sqlite3_free().
18544 **
18545 ** If an OOM error occurs, (*pRc) is set to SQLITE_NOMEM and a NULL
18546 ** pointer returned.
18547 */
18548 static char *shellMPrintf(int *pRc, const char *zFmt, ...){
18549 char *z = 0;
18550 if( *pRc==SQLITE_OK ){
18551 va_list ap;
18552 va_start(ap, zFmt);
18553 z = sqlite3_vmprintf(zFmt, ap);
18554 va_end(ap);
18555 if( z==0 ){
18556 *pRc = SQLITE_NOMEM;
18557 }
18558 }
18559 return z;
18560 }
18561
18562
18563 /*
18564 ** When running the ".recover" command, each output table, and the special
18565 ** orphaned row table if it is required, is represented by an instance
18566 ** of the following struct.
18567 */
18568 typedef struct RecoverTable RecoverTable;
18569 struct RecoverTable {
18570 char *zQuoted; /* Quoted version of table name */
18571 int nCol; /* Number of columns in table */
18572 char **azlCol; /* Array of column lists */
18573 int iPk; /* Index of IPK column */
18574 };
18575
18576 /*
18577 ** Free a RecoverTable object allocated by recoverFindTable() or
18578 ** recoverOrphanTable().
18579 */
18580 static void recoverFreeTable(RecoverTable *pTab){
18581 if( pTab ){
18582 sqlite3_free(pTab->zQuoted);
18583 if( pTab->azlCol ){
18584 int i;
18585 for(i=0; i<=pTab->nCol; i++){
18586 sqlite3_free(pTab->azlCol[i]);
18587 }
18588 sqlite3_free(pTab->azlCol);
18589 }
18590 sqlite3_free(pTab);
18591 }
18592 }
18593
18594 /*
18595 ** This function is a no-op if (*pRc) is not SQLITE_OK when it is called.
18596 ** Otherwise, it allocates and returns a RecoverTable object based on the
18597 ** final four arguments passed to this function. It is the responsibility
18598 ** of the caller to eventually free the returned object using
18599 ** recoverFreeTable().
18600 */
18601 static RecoverTable *recoverNewTable(
18602 int *pRc, /* IN/OUT: Error code */
18603 const char *zName, /* Name of table */
18604 const char *zSql, /* CREATE TABLE statement */
18605 int bIntkey,
18606 int nCol
18607 ){
18608 sqlite3 *dbtmp = 0; /* sqlite3 handle for testing CREATE TABLE */
18609 int rc = *pRc;
18610 RecoverTable *pTab = 0;
18611
18612 pTab = (RecoverTable*)shellMalloc(&rc, sizeof(RecoverTable));
18613 if( rc==SQLITE_OK ){
18614 int nSqlCol = 0;
18615 int bSqlIntkey = 0;
18616 sqlite3_stmt *pStmt = 0;
18617
18618 rc = sqlite3_open("", &dbtmp);
18619 if( rc==SQLITE_OK ){
18620 sqlite3_create_function(dbtmp, "shell_idquote", 1, SQLITE_UTF8, 0,
18621 shellIdQuote, 0, 0);
18622 }
18623 if( rc==SQLITE_OK ){
18624 rc = sqlite3_exec(dbtmp, "PRAGMA writable_schema = on", 0, 0, 0);
18625 }
18626 if( rc==SQLITE_OK ){
18627 rc = sqlite3_exec(dbtmp, zSql, 0, 0, 0);
18628 if( rc==SQLITE_ERROR ){
18629 rc = SQLITE_OK;
18630 goto finished;
18631 }
18632 }
18633 shellPreparePrintf(dbtmp, &rc, &pStmt,
18634 "SELECT count(*) FROM pragma_table_info(%Q)", zName
18635 );
18636 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
18637 nSqlCol = sqlite3_column_int(pStmt, 0);
18638 }
18639 shellFinalize(&rc, pStmt);
18640
18641 if( rc!=SQLITE_OK || nSqlCol<nCol ){
18642 goto finished;
18643 }
18644
18645 shellPreparePrintf(dbtmp, &rc, &pStmt,
18646 "SELECT ("
18647 " SELECT substr(data,1,1)==X'0D' FROM sqlite_dbpage WHERE pgno=rootpage"
18648 ") FROM sqlite_schema WHERE name = %Q", zName
18649 );
18650 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
18651 bSqlIntkey = sqlite3_column_int(pStmt, 0);
18652 }
18653 shellFinalize(&rc, pStmt);
18654
18655 if( bIntkey==bSqlIntkey ){
18656 int i;
18657 const char *zPk = "_rowid_";
18658 sqlite3_stmt *pPkFinder = 0;
18659
18660 /* If this is an intkey table and there is an INTEGER PRIMARY KEY,
18661 ** set zPk to the name of the PK column, and pTab->iPk to the index
18662 ** of the column, where columns are 0-numbered from left to right.
18663 ** Or, if this is a WITHOUT ROWID table or if there is no IPK column,
18664 ** leave zPk as "_rowid_" and pTab->iPk at -2. */
18665 pTab->iPk = -2;
18666 if( bIntkey ){
18667 shellPreparePrintf(dbtmp, &rc, &pPkFinder,
18668 "SELECT cid, name FROM pragma_table_info(%Q) "
18669 " WHERE pk=1 AND type='integer' COLLATE nocase"
18670 " AND NOT EXISTS (SELECT cid FROM pragma_table_info(%Q) WHERE pk=2)"
18671 , zName, zName
18672 );
18673 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPkFinder) ){
18674 pTab->iPk = sqlite3_column_int(pPkFinder, 0);
18675 zPk = (const char*)sqlite3_column_text(pPkFinder, 1);
18676 if( zPk==0 ){ zPk = "_"; /* Defensive. Should never happen */ }
18677 }
18678 }
18679
18680 pTab->zQuoted = shellMPrintf(&rc, "\"%w\"", zName);
18681 pTab->azlCol = (char**)shellMalloc(&rc, sizeof(char*) * (nSqlCol+1));
18682 pTab->nCol = nSqlCol;
18683
18684 if( bIntkey ){
18685 pTab->azlCol[0] = shellMPrintf(&rc, "\"%w\"", zPk);
18686 }else{
18687 pTab->azlCol[0] = shellMPrintf(&rc, "");
18688 }
18689 i = 1;
18690 shellPreparePrintf(dbtmp, &rc, &pStmt,
18691 "SELECT %Q || group_concat(shell_idquote(name), ', ') "
18692 " FILTER (WHERE cid!=%d) OVER (ORDER BY %s cid) "
18693 "FROM pragma_table_info(%Q)",
18694 bIntkey ? ", " : "", pTab->iPk,
18695 bIntkey ? "" : "(CASE WHEN pk=0 THEN 1000000 ELSE pk END), ",
18696 zName
18697 );
18698 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
18699 const char *zText = (const char*)sqlite3_column_text(pStmt, 0);
18700 pTab->azlCol[i] = shellMPrintf(&rc, "%s%s", pTab->azlCol[0], zText);
18701 i++;
18702 }
18703 shellFinalize(&rc, pStmt);
18704
18705 shellFinalize(&rc, pPkFinder);
18706 }
18707 }
18708
18709 finished:
18710 sqlite3_close(dbtmp);
18711 *pRc = rc;
18712 if( rc!=SQLITE_OK || (pTab && pTab->zQuoted==0) ){
18713 recoverFreeTable(pTab);
18714 pTab = 0;
18715 }
18716 return pTab;
18717 }
18718
18719 /*
18720 ** This function is called to search the schema recovered from the
18721 ** sqlite_schema table of the (possibly) corrupt database as part
18722 ** of a ".recover" command. Specifically, for a table with root page
18723 ** iRoot and at least nCol columns. Additionally, if bIntkey is 0, the
18724 ** table must be a WITHOUT ROWID table, or if non-zero, not one of
18725 ** those.
18726 **
18727 ** If a table is found, a (RecoverTable*) object is returned. Or, if
18728 ** no such table is found, but bIntkey is false and iRoot is the
18729 ** root page of an index in the recovered schema, then (*pbNoop) is
18730 ** set to true and NULL returned. Or, if there is no such table or
18731 ** index, NULL is returned and (*pbNoop) set to 0, indicating that
18732 ** the caller should write data to the orphans table.
18733 */
18734 static RecoverTable *recoverFindTable(
18735 ShellState *pState, /* Shell state object */
18736 int *pRc, /* IN/OUT: Error code */
18737 int iRoot, /* Root page of table */
18738 int bIntkey, /* True for an intkey table */
18739 int nCol, /* Number of columns in table */
18740 int *pbNoop /* OUT: True if iRoot is root of index */
18741 ){
18742 sqlite3_stmt *pStmt = 0;
18743 RecoverTable *pRet = 0;
18744 int bNoop = 0;
18745 const char *zSql = 0;
18746 const char *zName = 0;
18747
18748 /* Search the recovered schema for an object with root page iRoot. */
18749 shellPreparePrintf(pState->db, pRc, &pStmt,
18750 "SELECT type, name, sql FROM recovery.schema WHERE rootpage=%d", iRoot
18751 );
18752 while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
18753 const char *zType = (const char*)sqlite3_column_text(pStmt, 0);
18754 if( bIntkey==0 && sqlite3_stricmp(zType, "index")==0 ){
18755 bNoop = 1;
18756 break;
18757 }
18758 if( sqlite3_stricmp(zType, "table")==0 ){
18759 zName = (const char*)sqlite3_column_text(pStmt, 1);
18760 zSql = (const char*)sqlite3_column_text(pStmt, 2);
18761 if( zName!=0 && zSql!=0 ){
18762 pRet = recoverNewTable(pRc, zName, zSql, bIntkey, nCol);
18763 break;
18764 }
18765 }
18766 }
18767
18768 shellFinalize(pRc, pStmt);
18769 *pbNoop = bNoop;
18770 return pRet;
18771 }
18772
18773 /*
18774 ** Return a RecoverTable object representing the orphans table.
18775 */
18776 static RecoverTable *recoverOrphanTable(
18777 ShellState *pState, /* Shell state object */
18778 int *pRc, /* IN/OUT: Error code */
18779 const char *zLostAndFound, /* Base name for orphans table */
18780 int nCol /* Number of user data columns */
18781 ){
18782 RecoverTable *pTab = 0;
18783 if( nCol>=0 && *pRc==SQLITE_OK ){
18784 int i;
18785
18786 /* This block determines the name of the orphan table. The prefered
18787 ** name is zLostAndFound. But if that clashes with another name
18788 ** in the recovered schema, try zLostAndFound_0, zLostAndFound_1
18789 ** and so on until a non-clashing name is found. */
18790 int iTab = 0;
18791 char *zTab = shellMPrintf(pRc, "%s", zLostAndFound);
18792 sqlite3_stmt *pTest = 0;
18793 shellPrepare(pState->db, pRc,
18794 "SELECT 1 FROM recovery.schema WHERE name=?", &pTest
18795 );
18796 if( pTest ) sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
18797 while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pTest) ){
18798 shellReset(pRc, pTest);
18799 sqlite3_free(zTab);
18800 zTab = shellMPrintf(pRc, "%s_%d", zLostAndFound, iTab++);
18801 sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
18802 }
18803 shellFinalize(pRc, pTest);
18804
18805 pTab = (RecoverTable*)shellMalloc(pRc, sizeof(RecoverTable));
18806 if( pTab ){
18807 pTab->zQuoted = shellMPrintf(pRc, "\"%w\"", zTab);
18808 pTab->nCol = nCol;
18809 pTab->iPk = -2;
18810 if( nCol>0 ){
18811 pTab->azlCol = (char**)shellMalloc(pRc, sizeof(char*) * (nCol+1));
18812 if( pTab->azlCol ){
18813 pTab->azlCol[nCol] = shellMPrintf(pRc, "");
18814 for(i=nCol-1; i>=0; i--){
18815 pTab->azlCol[i] = shellMPrintf(pRc, "%s, NULL", pTab->azlCol[i+1]);
18816 }
18817 }
18818 }
18819
18820 if( *pRc!=SQLITE_OK ){
18821 recoverFreeTable(pTab);
18822 pTab = 0;
18823 }else{
18824 raw_printf(pState->out,
18825 "CREATE TABLE %s(rootpgno INTEGER, "
18826 "pgno INTEGER, nfield INTEGER, id INTEGER", pTab->zQuoted
18827 );
18828 for(i=0; i<nCol; i++){
18829 raw_printf(pState->out, ", c%d", i);
18830 }
18831 raw_printf(pState->out, ");\n");
18832 }
18833 }
18834 sqlite3_free(zTab);
18835 }
18836 return pTab;
18837 }
18838
18839 /*
18840 ** This function is called to recover data from the database. A script
18841 ** to construct a new database containing all recovered data is output
18842 ** on stream pState->out.
18843 */
18844 static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){
18845 int rc = SQLITE_OK;
18846 sqlite3_stmt *pLoop = 0; /* Loop through all root pages */
18847 sqlite3_stmt *pPages = 0; /* Loop through all pages in a group */
18848 sqlite3_stmt *pCells = 0; /* Loop through all cells in a page */
18849 const char *zRecoveryDb = ""; /* Name of "recovery" database */
18850 const char *zLostAndFound = "lost_and_found";
18851 int i;
18852 int nOrphan = -1;
18853 RecoverTable *pOrphan = 0;
18854
18855 int bFreelist = 1; /* 0 if --freelist-corrupt is specified */
18856 int bRowids = 1; /* 0 if --no-rowids */
 
 
 
18857 for(i=1; i<nArg; i++){
18858 char *z = azArg[i];
18859 int n;
18860 if( z[0]=='-' && z[1]=='-' ) z++;
18861 n = strlen30(z);
18862 if( n<=17 && memcmp("-freelist-corrupt", z, n)==0 ){
18863 bFreelist = 0;
18864 }else
18865 if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){
 
 
 
 
 
18866 i++;
18867 zRecoveryDb = azArg[i];
18868 }else
18869 if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){
18870 i++;
18871 zLostAndFound = azArg[i];
18872 }else
18873 if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
18874 bRowids = 0;
18875 }
18876 else{
@@ -18878,285 +21741,29 @@
18878 showHelp(pState->out, azArg[0]);
18879 return 1;
18880 }
18881 }
18882
18883 shellExecPrintf(pState->db, &rc,
18884 /* Attach an in-memory database named 'recovery'. Create an indexed
18885 ** cache of the sqlite_dbptr virtual table. */
18886 "PRAGMA writable_schema = on;"
18887 "ATTACH %Q AS recovery;"
18888 "DROP TABLE IF EXISTS recovery.dbptr;"
18889 "DROP TABLE IF EXISTS recovery.freelist;"
18890 "DROP TABLE IF EXISTS recovery.map;"
18891 "DROP TABLE IF EXISTS recovery.schema;"
18892 "CREATE TABLE recovery.freelist(pgno INTEGER PRIMARY KEY);", zRecoveryDb
18893 );
18894
18895 if( bFreelist ){
18896 shellExec(pState->db, &rc,
18897 "WITH trunk(pgno) AS ("
18898 " SELECT shell_int32("
18899 " (SELECT data FROM sqlite_dbpage WHERE pgno=1), 8) AS x "
18900 " WHERE x>0"
18901 " UNION"
18902 " SELECT shell_int32("
18903 " (SELECT data FROM sqlite_dbpage WHERE pgno=trunk.pgno), 0) AS x "
18904 " FROM trunk WHERE x>0"
18905 "),"
18906 "freelist(data, n, freepgno) AS ("
18907 " SELECT data, min(16384, shell_int32(data, 1)-1), t.pgno "
18908 " FROM trunk t, sqlite_dbpage s WHERE s.pgno=t.pgno"
18909 " UNION ALL"
18910 " SELECT data, n-1, shell_int32(data, 2+n) "
18911 " FROM freelist WHERE n>=0"
18912 ")"
18913 "REPLACE INTO recovery.freelist SELECT freepgno FROM freelist;"
18914 );
18915 }
18916
18917 /* If this is an auto-vacuum database, add all pointer-map pages to
18918 ** the freelist table. Do this regardless of whether or not
18919 ** --freelist-corrupt was specified. */
18920 shellExec(pState->db, &rc,
18921 "WITH ptrmap(pgno) AS ("
18922 " SELECT 2 WHERE shell_int32("
18923 " (SELECT data FROM sqlite_dbpage WHERE pgno=1), 13"
18924 " )"
18925 " UNION ALL "
18926 " SELECT pgno+1+(SELECT page_size FROM pragma_page_size)/5 AS pp "
18927 " FROM ptrmap WHERE pp<=(SELECT page_count FROM pragma_page_count)"
18928 ")"
18929 "REPLACE INTO recovery.freelist SELECT pgno FROM ptrmap"
18930 );
18931
18932 shellExec(pState->db, &rc,
18933 "CREATE TABLE recovery.dbptr("
18934 " pgno, child, PRIMARY KEY(child, pgno)"
18935 ") WITHOUT ROWID;"
18936 "INSERT OR IGNORE INTO recovery.dbptr(pgno, child) "
18937 " SELECT * FROM sqlite_dbptr"
18938 " WHERE pgno NOT IN freelist AND child NOT IN freelist;"
18939
18940 /* Delete any pointer to page 1. This ensures that page 1 is considered
18941 ** a root page, regardless of how corrupt the db is. */
18942 "DELETE FROM recovery.dbptr WHERE child = 1;"
18943
18944 /* Delete all pointers to any pages that have more than one pointer
18945 ** to them. Such pages will be treated as root pages when recovering
18946 ** data. */
18947 "DELETE FROM recovery.dbptr WHERE child IN ("
18948 " SELECT child FROM recovery.dbptr GROUP BY child HAVING count(*)>1"
18949 ");"
18950
18951 /* Create the "map" table that will (eventually) contain instructions
18952 ** for dealing with each page in the db that contains one or more
18953 ** records. */
18954 "CREATE TABLE recovery.map("
18955 "pgno INTEGER PRIMARY KEY, maxlen INT, intkey, root INT"
18956 ");"
18957
18958 /* Populate table [map]. If there are circular loops of pages in the
18959 ** database, the following adds all pages in such a loop to the map
18960 ** as individual root pages. This could be handled better. */
18961 "WITH pages(i, maxlen) AS ("
18962 " SELECT page_count, ("
18963 " SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=page_count"
18964 " ) FROM pragma_page_count WHERE page_count>0"
18965 " UNION ALL"
18966 " SELECT i-1, ("
18967 " SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=i-1"
18968 " ) FROM pages WHERE i>=2"
18969 ")"
18970 "INSERT INTO recovery.map(pgno, maxlen, intkey, root) "
18971 " SELECT i, maxlen, NULL, ("
18972 " WITH p(orig, pgno, parent) AS ("
18973 " SELECT 0, i, (SELECT pgno FROM recovery.dbptr WHERE child=i)"
18974 " UNION "
18975 " SELECT i, p.parent, "
18976 " (SELECT pgno FROM recovery.dbptr WHERE child=p.parent) FROM p"
18977 " )"
18978 " SELECT pgno FROM p WHERE (parent IS NULL OR pgno = orig)"
18979 ") "
18980 "FROM pages WHERE maxlen IS NOT NULL AND i NOT IN freelist;"
18981 "UPDATE recovery.map AS o SET intkey = ("
18982 " SELECT substr(data, 1, 1)==X'0D' FROM sqlite_dbpage WHERE pgno=o.pgno"
18983 ");"
18984
18985 /* Extract data from page 1 and any linked pages into table
18986 ** recovery.schema. With the same schema as an sqlite_schema table. */
18987 "CREATE TABLE recovery.schema(type, name, tbl_name, rootpage, sql);"
18988 "INSERT INTO recovery.schema SELECT "
18989 " max(CASE WHEN field=0 THEN value ELSE NULL END),"
18990 " max(CASE WHEN field=1 THEN value ELSE NULL END),"
18991 " max(CASE WHEN field=2 THEN value ELSE NULL END),"
18992 " max(CASE WHEN field=3 THEN value ELSE NULL END),"
18993 " max(CASE WHEN field=4 THEN value ELSE NULL END)"
18994 "FROM sqlite_dbdata WHERE pgno IN ("
18995 " SELECT pgno FROM recovery.map WHERE root=1"
18996 ")"
18997 "GROUP BY pgno, cell;"
18998 "CREATE INDEX recovery.schema_rootpage ON schema(rootpage);"
18999 );
19000
19001 /* Open a transaction, then print out all non-virtual, non-"sqlite_%"
19002 ** CREATE TABLE statements that extracted from the existing schema. */
19003 if( rc==SQLITE_OK ){
19004 sqlite3_stmt *pStmt = 0;
19005 /* ".recover" might output content in an order which causes immediate
19006 ** foreign key constraints to be violated. So disable foreign-key
19007 ** constraint enforcement to prevent problems when running the output
19008 ** script. */
19009 raw_printf(pState->out, "PRAGMA foreign_keys=OFF;\n");
19010 raw_printf(pState->out, "BEGIN;\n");
19011 raw_printf(pState->out, "PRAGMA writable_schema = on;\n");
19012 shellPrepare(pState->db, &rc,
19013 "SELECT sql FROM recovery.schema "
19014 "WHERE type='table' AND sql LIKE 'create table%'", &pStmt
19015 );
19016 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
19017 const char *zCreateTable = (const char*)sqlite3_column_text(pStmt, 0);
19018 raw_printf(pState->out, "CREATE TABLE IF NOT EXISTS %s;\n",
19019 &zCreateTable[12]
19020 );
19021 }
19022 shellFinalize(&rc, pStmt);
19023 }
19024
19025 /* Figure out if an orphan table will be required. And if so, how many
19026 ** user columns it should contain */
19027 shellPrepare(pState->db, &rc,
19028 "SELECT coalesce(max(maxlen), -2) FROM recovery.map WHERE root>1"
19029 , &pLoop
19030 );
19031 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
19032 nOrphan = sqlite3_column_int(pLoop, 0);
19033 }
19034 shellFinalize(&rc, pLoop);
19035 pLoop = 0;
19036
19037 shellPrepare(pState->db, &rc,
19038 "SELECT pgno FROM recovery.map WHERE root=?", &pPages
19039 );
19040
19041 shellPrepare(pState->db, &rc,
19042 "SELECT max(field), group_concat(shell_escape_crnl(quote"
19043 "(case when (? AND field<0) then NULL else value end)"
19044 "), ', ')"
19045 ", min(field) "
19046 "FROM sqlite_dbdata WHERE pgno = ? AND field != ?"
19047 "GROUP BY cell", &pCells
19048 );
19049
19050 /* Loop through each root page. */
19051 shellPrepare(pState->db, &rc,
19052 "SELECT root, intkey, max(maxlen) FROM recovery.map"
19053 " WHERE root>1 GROUP BY root, intkey ORDER BY root=("
19054 " SELECT rootpage FROM recovery.schema WHERE name='sqlite_sequence'"
19055 ")", &pLoop
19056 );
19057 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
19058 int iRoot = sqlite3_column_int(pLoop, 0);
19059 int bIntkey = sqlite3_column_int(pLoop, 1);
19060 int nCol = sqlite3_column_int(pLoop, 2);
19061 int bNoop = 0;
19062 RecoverTable *pTab;
19063
19064 assert( bIntkey==0 || bIntkey==1 );
19065 pTab = recoverFindTable(pState, &rc, iRoot, bIntkey, nCol, &bNoop);
19066 if( bNoop || rc ) continue;
19067 if( pTab==0 ){
19068 if( pOrphan==0 ){
19069 pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
19070 }
19071 pTab = pOrphan;
19072 if( pTab==0 ) break;
19073 }
19074
19075 if( 0==sqlite3_stricmp(pTab->zQuoted, "\"sqlite_sequence\"") ){
19076 raw_printf(pState->out, "DELETE FROM sqlite_sequence;\n");
19077 }
19078 sqlite3_bind_int(pPages, 1, iRoot);
19079 if( bRowids==0 && pTab->iPk<0 ){
19080 sqlite3_bind_int(pCells, 1, 1);
19081 }else{
19082 sqlite3_bind_int(pCells, 1, 0);
19083 }
19084 sqlite3_bind_int(pCells, 3, pTab->iPk);
19085
19086 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPages) ){
19087 int iPgno = sqlite3_column_int(pPages, 0);
19088 sqlite3_bind_int(pCells, 2, iPgno);
19089 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pCells) ){
19090 int nField = sqlite3_column_int(pCells, 0);
19091 int iMin = sqlite3_column_int(pCells, 2);
19092 const char *zVal = (const char*)sqlite3_column_text(pCells, 1);
19093
19094 RecoverTable *pTab2 = pTab;
19095 if( pTab!=pOrphan && (iMin<0)!=bIntkey ){
19096 if( pOrphan==0 ){
19097 pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
19098 }
19099 pTab2 = pOrphan;
19100 if( pTab2==0 ) break;
19101 }
19102
19103 nField = nField+1;
19104 if( pTab2==pOrphan ){
19105 raw_printf(pState->out,
19106 "INSERT INTO %s VALUES(%d, %d, %d, %s%s%s);\n",
19107 pTab2->zQuoted, iRoot, iPgno, nField,
19108 iMin<0 ? "" : "NULL, ", zVal, pTab2->azlCol[nField]
19109 );
19110 }else{
19111 raw_printf(pState->out, "INSERT INTO %s(%s) VALUES( %s );\n",
19112 pTab2->zQuoted, pTab2->azlCol[nField], zVal
19113 );
19114 }
19115 }
19116 shellReset(&rc, pCells);
19117 }
19118 shellReset(&rc, pPages);
19119 if( pTab!=pOrphan ) recoverFreeTable(pTab);
19120 }
19121 shellFinalize(&rc, pLoop);
19122 shellFinalize(&rc, pPages);
19123 shellFinalize(&rc, pCells);
19124 recoverFreeTable(pOrphan);
19125
19126 /* The rest of the schema */
19127 if( rc==SQLITE_OK ){
19128 sqlite3_stmt *pStmt = 0;
19129 shellPrepare(pState->db, &rc,
19130 "SELECT sql, name FROM recovery.schema "
19131 "WHERE sql NOT LIKE 'create table%'", &pStmt
19132 );
19133 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
19134 const char *zSql = (const char*)sqlite3_column_text(pStmt, 0);
19135 if( sqlite3_strnicmp(zSql, "create virt", 11)==0 ){
19136 const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
19137 char *zPrint = shellMPrintf(&rc,
19138 "INSERT INTO sqlite_schema VALUES('table', %Q, %Q, 0, %Q)",
19139 zName, zName, zSql
19140 );
19141 raw_printf(pState->out, "%s;\n", zPrint);
19142 sqlite3_free(zPrint);
19143 }else{
19144 raw_printf(pState->out, "%s;\n", zSql);
19145 }
19146 }
19147 shellFinalize(&rc, pStmt);
19148 }
19149
19150 if( rc==SQLITE_OK ){
19151 raw_printf(pState->out, "PRAGMA writable_schema = off;\n");
19152 raw_printf(pState->out, "COMMIT;\n");
19153 }
19154 sqlite3_exec(pState->db, "DETACH recovery", 0, 0, 0);
19155 return rc;
19156 }
19157 #endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
19158
19159
19160 /*
19161 * zAutoColumn(zCol, &db, ?) => Maybe init db, add column zCol to it.
19162 * zAutoColumn(0, &db, ?) => (db!=0) Form columns spec for CREATE TABLE,
@@ -19744,20 +22351,20 @@
19744 utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]);
19745 utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");
19746 }
19747 }else
19748
19749 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
19750 if( c=='d' && n>=3 && cli_strncmp(azArg[0], "dbinfo", n)==0 ){
19751 rc = shell_dbinfo_command(p, nArg, azArg);
19752 }else
19753
19754 if( c=='r' && cli_strncmp(azArg[0], "recover", n)==0 ){
19755 open_db(p, 0);
19756 rc = recoverDatabaseCmd(p, nArg, azArg);
19757 }else
19758 #endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
19759
19760 if( c=='d' && cli_strncmp(azArg[0], "dump", n)==0 ){
19761 char *zLike = 0;
19762 char *zSql;
19763 int i;
19764
--- extsrc/shell.c
+++ extsrc/shell.c
@@ -32,10 +32,12 @@
32 */
33 #if (defined(_WIN32) || defined(WIN32)) && !defined(_CRT_SECURE_NO_WARNINGS)
34 /* This needs to come before any includes for MSVC compiler */
35 #define _CRT_SECURE_NO_WARNINGS
36 #endif
37 typedef unsigned int u32;
38 typedef unsigned short int u16;
39
40 /*
41 ** Optionally #include a user-defined header, whereby compilation options
42 ** may be set prior to where they take effect, but after platform setup.
43 ** If SQLITE_CUSTOM_INCLUDE=? is defined, its value names the #include
@@ -565,10 +567,11 @@
567 */
568 static void utf8_width_print(FILE *pOut, int w, const char *zUtf){
569 int i;
570 int n;
571 int aw = w<0 ? -w : w;
572 if( zUtf==0 ) zUtf = "";
573 for(i=n=0; zUtf[i]; i++){
574 if( (zUtf[i]&0xc0)!=0x80 ){
575 n++;
576 if( n==aw ){
577 do{ i++; }while( (zUtf[i]&0xc0)==0x80 );
@@ -6890,12 +6893,12 @@
6893 # define UINT16_TYPE unsigned short int
6894 # endif
6895 #endif
6896 /* typedef sqlite3_int64 i64; */
6897 /* typedef unsigned char u8; */
6898 /* typedef UINT32_TYPE u32; // 4-byte unsigned integer // */
6899 /* typedef UINT16_TYPE u16; // 2-byte unsigned integer // */
6900 #define MIN(a,b) ((a)<(b) ? (a) : (b))
6901
6902 #if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
6903 # define SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS 1
6904 #endif
@@ -11402,11 +11405,16 @@
11405 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
11406
11407 /************************* End ../ext/expert/sqlite3expert.c ********************/
11408
11409 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
11410 #define SQLITE_SHELL_HAVE_RECOVER 1
11411 #else
11412 #define SQLITE_SHELL_HAVE_RECOVER 0
11413 #endif
11414 #if SQLITE_SHELL_HAVE_RECOVER
11415 /************************* Begin ../ext/recover/dbdata.c ******************/
11416 /*
11417 ** 2019-04-17
11418 **
11419 ** The author disclaims copyright to this source code. In place of
11420 ** a legal notice, here is a blessing:
@@ -11476,19 +11484,23 @@
11484 ** );
11485 **
11486 ** It contains one entry for each b-tree pointer between a parent and
11487 ** child page in the database.
11488 */
11489
11490 #if !defined(SQLITEINT_H)
11491 /* #include "sqlite3ext.h" */
11492
11493 /* typedef unsigned char u8; */
11494 /* typedef unsigned int u32; */
11495
11496 #endif
11497 SQLITE_EXTENSION_INIT1
11498 #include <string.h>
11499 #include <assert.h>
11500
11501 #ifndef SQLITE_OMIT_VIRTUALTABLE
11502
11503 #define DBDATA_PADDING_BYTES 100
11504
11505 typedef struct DbdataTable DbdataTable;
11506 typedef struct DbdataCursor DbdataCursor;
@@ -11507,15 +11519,16 @@
11519 int szDb;
11520 sqlite3_int64 iRowid;
11521
11522 /* Only for the sqlite_dbdata table */
11523 u8 *pRec; /* Buffer containing current record */
11524 sqlite3_int64 nRec; /* Size of pRec[] in bytes */
11525 sqlite3_int64 nHdr; /* Size of header in bytes */
11526 int iField; /* Current field number */
11527 u8 *pHdrPtr;
11528 u8 *pPtr;
11529 u32 enc; /* Text encoding */
11530
11531 sqlite3_int64 iIntkey; /* Integer key value */
11532 };
11533
11534 /* Table object */
@@ -11704,18 +11717,18 @@
11717 }
11718
11719 /*
11720 ** Utility methods to decode 16 and 32-bit big-endian unsigned integers.
11721 */
11722 static u32 get_uint16(unsigned char *a){
11723 return (a[0]<<8)|a[1];
11724 }
11725 static u32 get_uint32(unsigned char *a){
11726 return ((u32)a[0]<<24)
11727 | ((u32)a[1]<<16)
11728 | ((u32)a[2]<<8)
11729 | ((u32)a[3]);
11730 }
11731
11732 /*
11733 ** Load page pgno from the database via the sqlite_dbpage virtual table.
11734 ** If successful, set (*ppPage) to point to a buffer containing the page
@@ -11726,57 +11739,72 @@
11739 ** Or, if an error occurs, set both (*ppPage) and (*pnPage) to 0 and
11740 ** return an SQLite error code.
11741 */
11742 static int dbdataLoadPage(
11743 DbdataCursor *pCsr, /* Cursor object */
11744 u32 pgno, /* Page number of page to load */
11745 u8 **ppPage, /* OUT: pointer to page buffer */
11746 int *pnPage /* OUT: Size of (*ppPage) in bytes */
11747 ){
11748 int rc2;
11749 int rc = SQLITE_OK;
11750 sqlite3_stmt *pStmt = pCsr->pStmt;
11751
11752 *ppPage = 0;
11753 *pnPage = 0;
11754 if( pgno>0 ){
11755 sqlite3_bind_int64(pStmt, 2, pgno);
11756 if( SQLITE_ROW==sqlite3_step(pStmt) ){
11757 int nCopy = sqlite3_column_bytes(pStmt, 0);
11758 if( nCopy>0 ){
11759 u8 *pPage;
11760 pPage = (u8*)sqlite3_malloc64(nCopy + DBDATA_PADDING_BYTES);
11761 if( pPage==0 ){
11762 rc = SQLITE_NOMEM;
11763 }else{
11764 const u8 *pCopy = sqlite3_column_blob(pStmt, 0);
11765 memcpy(pPage, pCopy, nCopy);
11766 memset(&pPage[nCopy], 0, DBDATA_PADDING_BYTES);
11767 }
11768 *ppPage = pPage;
11769 *pnPage = nCopy;
11770 }
11771 }
11772 rc2 = sqlite3_reset(pStmt);
11773 if( rc==SQLITE_OK ) rc = rc2;
11774 }
11775
11776 return rc;
11777 }
11778
11779 /*
11780 ** Read a varint. Put the value in *pVal and return the number of bytes.
11781 */
11782 static int dbdataGetVarint(const u8 *z, sqlite3_int64 *pVal){
11783 sqlite3_uint64 u = 0;
11784 int i;
11785 for(i=0; i<8; i++){
11786 u = (u<<7) + (z[i]&0x7f);
11787 if( (z[i]&0x80)==0 ){ *pVal = (sqlite3_int64)u; return i+1; }
11788 }
11789 u = (u<<8) + (z[i]&0xff);
11790 *pVal = (sqlite3_int64)u;
11791 return 9;
11792 }
11793
11794 /*
11795 ** Like dbdataGetVarint(), but set the output to 0 if it is less than 0
11796 ** or greater than 0xFFFFFFFF. This can be used for all varints in an
11797 ** SQLite database except for key values in intkey tables.
11798 */
11799 static int dbdataGetVarintU32(const u8 *z, sqlite3_int64 *pVal){
11800 sqlite3_int64 val;
11801 int nRet = dbdataGetVarint(z, &val);
11802 if( val<0 || val>0xFFFFFFFF ) val = 0;
11803 *pVal = val;
11804 return nRet;
11805 }
11806
11807 /*
11808 ** Return the number of bytes of space used by an SQLite value of type
11809 ** eType.
11810 */
@@ -11810,10 +11838,11 @@
11838 ** Load a value of type eType from buffer pData and use it to set the
11839 ** result of context object pCtx.
11840 */
11841 static void dbdataValue(
11842 sqlite3_context *pCtx,
11843 u32 enc,
11844 int eType,
11845 u8 *pData,
11846 int nData
11847 ){
11848 if( eType>=0 && dbdataValueBytes(eType)<=nData ){
@@ -11854,11 +11883,23 @@
11883 }
11884
11885 default: {
11886 int n = ((eType-12) / 2);
11887 if( eType % 2 ){
11888 switch( enc ){
11889 #ifndef SQLITE_OMIT_UTF16
11890 case SQLITE_UTF16BE:
11891 sqlite3_result_text16be(pCtx, (void*)pData, n, SQLITE_TRANSIENT);
11892 break;
11893 case SQLITE_UTF16LE:
11894 sqlite3_result_text16le(pCtx, (void*)pData, n, SQLITE_TRANSIENT);
11895 break;
11896 #endif
11897 default:
11898 sqlite3_result_text(pCtx, (char*)pData, n, SQLITE_TRANSIENT);
11899 break;
11900 }
11901 }else{
11902 sqlite3_result_blob(pCtx, pData, n, SQLITE_TRANSIENT);
11903 }
11904 }
11905 }
@@ -11882,10 +11923,11 @@
11923 while( 1 ){
11924 if( pCsr->bOnePage==0 && pCsr->iPgno>pCsr->szDb ) return SQLITE_OK;
11925 rc = dbdataLoadPage(pCsr, pCsr->iPgno, &pCsr->aPage, &pCsr->nPage);
11926 if( rc!=SQLITE_OK ) return rc;
11927 if( pCsr->aPage ) break;
11928 if( pCsr->bOnePage ) return SQLITE_OK;
11929 pCsr->iPgno++;
11930 }
11931 pCsr->iCell = pTab->bPtr ? -2 : 0;
11932 pCsr->nCell = get_uint16(&pCsr->aPage[iOff+3]);
11933 }
@@ -11945,11 +11987,11 @@
11987
11988 /* Load the "byte of payload including overflow" field */
11989 if( bNextPage || iOff>pCsr->nPage ){
11990 bNextPage = 1;
11991 }else{
11992 iOff += dbdataGetVarintU32(&pCsr->aPage[iOff], &nPayload);
11993 }
11994
11995 /* If this is a leaf intkey cell, load the rowid */
11996 if( bHasRowid && !bNextPage && iOff<pCsr->nPage ){
11997 iOff += dbdataGetVarint(&pCsr->aPage[iOff], &pCsr->iIntkey);
@@ -11992,11 +12034,11 @@
12034 iOff += nLocal;
12035
12036 /* Load content from overflow pages */
12037 if( nPayload>nLocal ){
12038 sqlite3_int64 nRem = nPayload - nLocal;
12039 u32 pgnoOvfl = get_uint32(&pCsr->aPage[iOff]);
12040 while( nRem>0 ){
12041 u8 *aOvfl = 0;
12042 int nOvfl = 0;
12043 int nCopy;
12044 rc = dbdataLoadPage(pCsr, pgnoOvfl, &aOvfl, &nOvfl);
@@ -12012,11 +12054,12 @@
12054 pgnoOvfl = get_uint32(aOvfl);
12055 sqlite3_free(aOvfl);
12056 }
12057 }
12058
12059 iHdr = dbdataGetVarintU32(pCsr->pRec, &nHdr);
12060 if( nHdr>nPayload ) nHdr = 0;
12061 pCsr->nHdr = nHdr;
12062 pCsr->pHdrPtr = &pCsr->pRec[iHdr];
12063 pCsr->pPtr = &pCsr->pRec[pCsr->nHdr];
12064 pCsr->iField = (bHasRowid ? -1 : 0);
12065 }
@@ -12026,11 +12069,11 @@
12069 if( pCsr->iField>0 ){
12070 sqlite3_int64 iType;
12071 if( pCsr->pHdrPtr>&pCsr->pRec[pCsr->nRec] ){
12072 bNextPage = 1;
12073 }else{
12074 pCsr->pHdrPtr += dbdataGetVarintU32(pCsr->pHdrPtr, &iType);
12075 pCsr->pPtr += dbdataValueBytes(iType);
12076 }
12077 }
12078 }
12079
@@ -12064,10 +12107,22 @@
12107 */
12108 static int dbdataEof(sqlite3_vtab_cursor *pCursor){
12109 DbdataCursor *pCsr = (DbdataCursor*)pCursor;
12110 return pCsr->aPage==0;
12111 }
12112
12113 /*
12114 ** Return true if nul-terminated string zSchema ends in "()". Or false
12115 ** otherwise.
12116 */
12117 static int dbdataIsFunction(const char *zSchema){
12118 size_t n = strlen(zSchema);
12119 if( n>2 && zSchema[n-2]=='(' && zSchema[n-1]==')' ){
12120 return (int)n-2;
12121 }
12122 return 0;
12123 }
12124
12125 /*
12126 ** Determine the size in pages of database zSchema (where zSchema is
12127 ** "main", "temp" or the name of an attached database) and set
12128 ** pCsr->szDb accordingly. If successful, return SQLITE_OK. Otherwise,
@@ -12075,23 +12130,48 @@
12130 */
12131 static int dbdataDbsize(DbdataCursor *pCsr, const char *zSchema){
12132 DbdataTable *pTab = (DbdataTable*)pCsr->base.pVtab;
12133 char *zSql = 0;
12134 int rc, rc2;
12135 int nFunc = 0;
12136 sqlite3_stmt *pStmt = 0;
12137
12138 if( (nFunc = dbdataIsFunction(zSchema))>0 ){
12139 zSql = sqlite3_mprintf("SELECT %.*s(0)", nFunc, zSchema);
12140 }else{
12141 zSql = sqlite3_mprintf("PRAGMA %Q.page_count", zSchema);
12142 }
12143 if( zSql==0 ) return SQLITE_NOMEM;
12144
12145 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pStmt, 0);
12146 sqlite3_free(zSql);
12147 if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
12148 pCsr->szDb = sqlite3_column_int(pStmt, 0);
12149 }
12150 rc2 = sqlite3_finalize(pStmt);
12151 if( rc==SQLITE_OK ) rc = rc2;
12152 return rc;
12153 }
12154
12155 /*
12156 ** Attempt to figure out the encoding of the database by retrieving page 1
12157 ** and inspecting the header field. If successful, set the pCsr->enc variable
12158 ** and return SQLITE_OK. Otherwise, return an SQLite error code.
12159 */
12160 static int dbdataGetEncoding(DbdataCursor *pCsr){
12161 int rc = SQLITE_OK;
12162 int nPg1 = 0;
12163 u8 *aPg1 = 0;
12164 rc = dbdataLoadPage(pCsr, 1, &aPg1, &nPg1);
12165 assert( rc!=SQLITE_OK || nPg1==0 || nPg1>=512 );
12166 if( rc==SQLITE_OK && nPg1>0 ){
12167 pCsr->enc = get_uint32(&aPg1[56]);
12168 }
12169 sqlite3_free(aPg1);
12170 return rc;
12171 }
12172
12173
12174 /*
12175 ** xFilter method for sqlite_dbdata and sqlite_dbptr.
12176 */
12177 static int dbdataFilter(
@@ -12106,23 +12186,32 @@
12186
12187 dbdataResetCursor(pCsr);
12188 assert( pCsr->iPgno==1 );
12189 if( idxNum & 0x01 ){
12190 zSchema = (const char*)sqlite3_value_text(argv[0]);
12191 if( zSchema==0 ) zSchema = "";
12192 }
12193 if( idxNum & 0x02 ){
12194 pCsr->iPgno = sqlite3_value_int(argv[(idxNum & 0x01)]);
12195 pCsr->bOnePage = 1;
12196 }else{
 
12197 rc = dbdataDbsize(pCsr, zSchema);
12198 }
12199
12200 if( rc==SQLITE_OK ){
12201 int nFunc = 0;
12202 if( pTab->pStmt ){
12203 pCsr->pStmt = pTab->pStmt;
12204 pTab->pStmt = 0;
12205 }else if( (nFunc = dbdataIsFunction(zSchema))>0 ){
12206 char *zSql = sqlite3_mprintf("SELECT %.*s(?2)", nFunc, zSchema);
12207 if( zSql==0 ){
12208 rc = SQLITE_NOMEM;
12209 }else{
12210 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pStmt, 0);
12211 sqlite3_free(zSql);
12212 }
12213 }else{
12214 rc = sqlite3_prepare_v2(pTab->db,
12215 "SELECT data FROM sqlite_dbpage(?) WHERE pgno=?", -1,
12216 &pCsr->pStmt, 0
12217 );
@@ -12131,17 +12220,24 @@
12220 if( rc==SQLITE_OK ){
12221 rc = sqlite3_bind_text(pCsr->pStmt, 1, zSchema, -1, SQLITE_TRANSIENT);
12222 }else{
12223 pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
12224 }
12225
12226 /* Try to determine the encoding of the db by inspecting the header
12227 ** field on page 1. */
12228 if( rc==SQLITE_OK ){
12229 rc = dbdataGetEncoding(pCsr);
12230 }
12231
12232 if( rc==SQLITE_OK ){
12233 rc = dbdataNext(pCursor);
12234 }
12235 return rc;
12236 }
12237
12238 /*
12239 ** Return a column for the sqlite_dbdata or sqlite_dbptr table.
12240 */
12241 static int dbdataColumn(
12242 sqlite3_vtab_cursor *pCursor,
12243 sqlite3_context *ctx,
@@ -12183,13 +12279,14 @@
12279 case DBDATA_COLUMN_VALUE: {
12280 if( pCsr->iField<0 ){
12281 sqlite3_result_int64(ctx, pCsr->iIntkey);
12282 }else{
12283 sqlite3_int64 iType;
12284 dbdataGetVarintU32(pCsr->pHdrPtr, &iType);
12285 dbdataValue(
12286 ctx, pCsr->enc, iType, pCsr->pPtr,
12287 &pCsr->pRec[pCsr->nRec] - pCsr->pPtr
12288 );
12289 }
12290 break;
12291 }
12292 }
@@ -12255,11 +12352,3125 @@
12352 ){
12353 SQLITE_EXTENSION_INIT2(pApi);
12354 return sqlite3DbdataRegister(db);
12355 }
12356
12357 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
12358
12359 /************************* End ../ext/recover/dbdata.c ********************/
12360 /************************* Begin ../ext/recover/sqlite3recover.h ******************/
12361 /*
12362 ** 2022-08-27
12363 **
12364 ** The author disclaims copyright to this source code. In place of
12365 ** a legal notice, here is a blessing:
12366 **
12367 ** May you do good and not evil.
12368 ** May you find forgiveness for yourself and forgive others.
12369 ** May you share freely, never taking more than you give.
12370 **
12371 *************************************************************************
12372 **
12373 ** This file contains the public interface to the "recover" extension -
12374 ** an SQLite extension designed to recover data from corrupted database
12375 ** files.
12376 */
12377
12378 /*
12379 ** OVERVIEW:
12380 **
12381 ** To use the API to recover data from a corrupted database, an
12382 ** application:
12383 **
12384 ** 1) Creates an sqlite3_recover handle by calling either
12385 ** sqlite3_recover_init() or sqlite3_recover_init_sql().
12386 **
12387 ** 2) Configures the new handle using one or more calls to
12388 ** sqlite3_recover_config().
12389 **
12390 ** 3) Executes the recovery by repeatedly calling sqlite3_recover_step() on
12391 ** the handle until it returns something other than SQLITE_OK. If it
12392 ** returns SQLITE_DONE, then the recovery operation completed without
12393 ** error. If it returns some other non-SQLITE_OK value, then an error
12394 ** has occurred.
12395 **
12396 ** 4) Retrieves any error code and English language error message using the
12397 ** sqlite3_recover_errcode() and sqlite3_recover_errmsg() APIs,
12398 ** respectively.
12399 **
12400 ** 5) Destroys the sqlite3_recover handle and frees all resources
12401 ** using sqlite3_recover_finish().
12402 **
12403 ** The application may abandon the recovery operation at any point
12404 ** before it is finished by passing the sqlite3_recover handle to
12405 ** sqlite3_recover_finish(). This is not an error, but the final state
12406 ** of the output database, or the results of running the partial script
12407 ** delivered to the SQL callback, are undefined.
12408 */
12409
12410 #ifndef _SQLITE_RECOVER_H
12411 #define _SQLITE_RECOVER_H
12412
12413 /* #include "sqlite3.h" */
12414
12415 #ifdef __cplusplus
12416 extern "C" {
12417 #endif
12418
12419 /*
12420 ** An instance of the sqlite3_recover object represents a recovery
12421 ** operation in progress.
12422 **
12423 ** Constructors:
12424 **
12425 ** sqlite3_recover_init()
12426 ** sqlite3_recover_init_sql()
12427 **
12428 ** Destructor:
12429 **
12430 ** sqlite3_recover_finish()
12431 **
12432 ** Methods:
12433 **
12434 ** sqlite3_recover_config()
12435 ** sqlite3_recover_errcode()
12436 ** sqlite3_recover_errmsg()
12437 ** sqlite3_recover_run()
12438 ** sqlite3_recover_step()
12439 */
12440 typedef struct sqlite3_recover sqlite3_recover;
12441
12442 /*
12443 ** These two APIs attempt to create and return a new sqlite3_recover object.
12444 ** In both cases the first two arguments identify the (possibly
12445 ** corrupt) database to recover data from. The first argument is an open
12446 ** database handle and the second the name of a database attached to that
12447 ** handle (i.e. "main", "temp" or the name of an attached database).
12448 **
12449 ** If sqlite3_recover_init() is used to create the new sqlite3_recover
12450 ** handle, then data is recovered into a new database, identified by
12451 ** string parameter zUri. zUri may be an absolute or relative file path,
12452 ** or may be an SQLite URI. If the identified database file already exists,
12453 ** it is overwritten.
12454 **
12455 ** If sqlite3_recover_init_sql() is invoked, then any recovered data will
12456 ** be returned to the user as a series of SQL statements. Executing these
12457 ** SQL statements results in the same database as would have been created
12458 ** had sqlite3_recover_init() been used. For each SQL statement in the
12459 ** output, the callback function passed as the third argument (xSql) is
12460 ** invoked once. The first parameter is a passed a copy of the fourth argument
12461 ** to this function (pCtx) as its first parameter, and a pointer to a
12462 ** nul-terminated buffer containing the SQL statement formated as UTF-8 as
12463 ** the second. If the xSql callback returns any value other than SQLITE_OK,
12464 ** then processing is immediately abandoned and the value returned used as
12465 ** the recover handle error code (see below).
12466 **
12467 ** If an out-of-memory error occurs, NULL may be returned instead of
12468 ** a valid handle. In all other cases, it is the responsibility of the
12469 ** application to avoid resource leaks by ensuring that
12470 ** sqlite3_recover_finish() is called on all allocated handles.
12471 */
12472 sqlite3_recover *sqlite3_recover_init(
12473 sqlite3* db,
12474 const char *zDb,
12475 const char *zUri
12476 );
12477 sqlite3_recover *sqlite3_recover_init_sql(
12478 sqlite3* db,
12479 const char *zDb,
12480 int (*xSql)(void*, const char*),
12481 void *pCtx
12482 );
12483
12484 /*
12485 ** Configure an sqlite3_recover object that has just been created using
12486 ** sqlite3_recover_init() or sqlite3_recover_init_sql(). This function
12487 ** may only be called before the first call to sqlite3_recover_step()
12488 ** or sqlite3_recover_run() on the object.
12489 **
12490 ** The second argument passed to this function must be one of the
12491 ** SQLITE_RECOVER_* symbols defined below. Valid values for the third argument
12492 ** depend on the specific SQLITE_RECOVER_* symbol in use.
12493 **
12494 ** SQLITE_OK is returned if the configuration operation was successful,
12495 ** or an SQLite error code otherwise.
12496 */
12497 int sqlite3_recover_config(sqlite3_recover*, int op, void *pArg);
12498
12499 /*
12500 ** SQLITE_RECOVER_LOST_AND_FOUND:
12501 ** The pArg argument points to a string buffer containing the name
12502 ** of a "lost-and-found" table in the output database, or NULL. If
12503 ** the argument is non-NULL and the database contains seemingly
12504 ** valid pages that cannot be associated with any table in the
12505 ** recovered part of the schema, data is extracted from these
12506 ** pages to add to the lost-and-found table.
12507 **
12508 ** SQLITE_RECOVER_FREELIST_CORRUPT:
12509 ** The pArg value must actually be a pointer to a value of type
12510 ** int containing value 0 or 1 cast as a (void*). If this option is set
12511 ** (argument is 1) and a lost-and-found table has been configured using
12512 ** SQLITE_RECOVER_LOST_AND_FOUND, then is assumed that the freelist is
12513 ** corrupt and an attempt is made to recover records from pages that
12514 ** appear to be linked into the freelist. Otherwise, pages on the freelist
12515 ** are ignored. Setting this option can recover more data from the
12516 ** database, but often ends up "recovering" deleted records. The default
12517 ** value is 0 (clear).
12518 **
12519 ** SQLITE_RECOVER_ROWIDS:
12520 ** The pArg value must actually be a pointer to a value of type
12521 ** int containing value 0 or 1 cast as a (void*). If this option is set
12522 ** (argument is 1), then an attempt is made to recover rowid values
12523 ** that are not also INTEGER PRIMARY KEY values. If this option is
12524 ** clear, then new rowids are assigned to all recovered rows. The
12525 ** default value is 1 (set).
12526 **
12527 ** SQLITE_RECOVER_SLOWINDEXES:
12528 ** The pArg value must actually be a pointer to a value of type
12529 ** int containing value 0 or 1 cast as a (void*). If this option is clear
12530 ** (argument is 0), then when creating an output database, the recover
12531 ** module creates and populates non-UNIQUE indexes right at the end of the
12532 ** recovery operation - after all recoverable data has been inserted
12533 ** into the new database. This is faster overall, but means that the
12534 ** final call to sqlite3_recover_step() for a recovery operation may
12535 ** be need to create a large number of indexes, which may be very slow.
12536 **
12537 ** Or, if this option is set (argument is 1), then non-UNIQUE indexes
12538 ** are created in the output database before it is populated with
12539 ** recovered data. This is slower overall, but avoids the slow call
12540 ** to sqlite3_recover_step() at the end of the recovery operation.
12541 **
12542 ** The default option value is 0.
12543 */
12544 #define SQLITE_RECOVER_LOST_AND_FOUND 1
12545 #define SQLITE_RECOVER_FREELIST_CORRUPT 2
12546 #define SQLITE_RECOVER_ROWIDS 3
12547 #define SQLITE_RECOVER_SLOWINDEXES 4
12548
12549 /*
12550 ** Perform a unit of work towards the recovery operation. This function
12551 ** must normally be called multiple times to complete database recovery.
12552 **
12553 ** If no error occurs but the recovery operation is not completed, this
12554 ** function returns SQLITE_OK. If recovery has been completed successfully
12555 ** then SQLITE_DONE is returned. If an error has occurred, then an SQLite
12556 ** error code (e.g. SQLITE_IOERR or SQLITE_NOMEM) is returned. It is not
12557 ** considered an error if some or all of the data cannot be recovered
12558 ** due to database corruption.
12559 **
12560 ** Once sqlite3_recover_step() has returned a value other than SQLITE_OK,
12561 ** all further such calls on the same recover handle are no-ops that return
12562 ** the same non-SQLITE_OK value.
12563 */
12564 int sqlite3_recover_step(sqlite3_recover*);
12565
12566 /*
12567 ** Run the recovery operation to completion. Return SQLITE_OK if successful,
12568 ** or an SQLite error code otherwise. Calling this function is the same
12569 ** as executing:
12570 **
12571 ** while( SQLITE_OK==sqlite3_recover_step(p) );
12572 ** return sqlite3_recover_errcode(p);
12573 */
12574 int sqlite3_recover_run(sqlite3_recover*);
12575
12576 /*
12577 ** If an error has been encountered during a prior call to
12578 ** sqlite3_recover_step(), then this function attempts to return a
12579 ** pointer to a buffer containing an English language explanation of
12580 ** the error. If no error message is available, or if an out-of memory
12581 ** error occurs while attempting to allocate a buffer in which to format
12582 ** the error message, NULL is returned.
12583 **
12584 ** The returned buffer remains valid until the sqlite3_recover handle is
12585 ** destroyed using sqlite3_recover_finish().
12586 */
12587 const char *sqlite3_recover_errmsg(sqlite3_recover*);
12588
12589 /*
12590 ** If this function is called on an sqlite3_recover handle after
12591 ** an error occurs, an SQLite error code is returned. Otherwise, SQLITE_OK.
12592 */
12593 int sqlite3_recover_errcode(sqlite3_recover*);
12594
12595 /*
12596 ** Clean up a recovery object created by a call to sqlite3_recover_init().
12597 ** The results of using a recovery object with any API after it has been
12598 ** passed to this function are undefined.
12599 **
12600 ** This function returns the same value as sqlite3_recover_errcode().
12601 */
12602 int sqlite3_recover_finish(sqlite3_recover*);
12603
12604
12605 #ifdef __cplusplus
12606 } /* end of the 'extern "C"' block */
12607 #endif
12608
12609 #endif /* ifndef _SQLITE_RECOVER_H */
12610
12611 /************************* End ../ext/recover/sqlite3recover.h ********************/
12612 /************************* Begin ../ext/recover/sqlite3recover.c ******************/
12613 /*
12614 ** 2022-08-27
12615 **
12616 ** The author disclaims copyright to this source code. In place of
12617 ** a legal notice, here is a blessing:
12618 **
12619 ** May you do good and not evil.
12620 ** May you find forgiveness for yourself and forgive others.
12621 ** May you share freely, never taking more than you give.
12622 **
12623 *************************************************************************
12624 **
12625 */
12626
12627
12628 /* #include "sqlite3recover.h" */
12629 #include <assert.h>
12630 #include <string.h>
12631
12632 #ifndef SQLITE_OMIT_VIRTUALTABLE
12633
12634 /*
12635 ** Declaration for public API function in file dbdata.c. This may be called
12636 ** with NULL as the final two arguments to register the sqlite_dbptr and
12637 ** sqlite_dbdata virtual tables with a database handle.
12638 */
12639 #ifdef _WIN32
12640
12641 #endif
12642 int sqlite3_dbdata_init(sqlite3*, char**, const sqlite3_api_routines*);
12643
12644 /* typedef unsigned int u32; */
12645 /* typedef unsigned char u8; */
12646 /* typedef sqlite3_int64 i64; */
12647
12648 typedef struct RecoverTable RecoverTable;
12649 typedef struct RecoverColumn RecoverColumn;
12650
12651 /*
12652 ** When recovering rows of data that can be associated with table
12653 ** definitions recovered from the sqlite_schema table, each table is
12654 ** represented by an instance of the following object.
12655 **
12656 ** iRoot:
12657 ** The root page in the original database. Not necessarily (and usually
12658 ** not) the same in the recovered database.
12659 **
12660 ** zTab:
12661 ** Name of the table.
12662 **
12663 ** nCol/aCol[]:
12664 ** aCol[] is an array of nCol columns. In the order in which they appear
12665 ** in the table.
12666 **
12667 ** bIntkey:
12668 ** Set to true for intkey tables, false for WITHOUT ROWID.
12669 **
12670 ** iRowidBind:
12671 ** Each column in the aCol[] array has associated with it the index of
12672 ** the bind parameter its values will be bound to in the INSERT statement
12673 ** used to construct the output database. If the table does has a rowid
12674 ** but not an INTEGER PRIMARY KEY column, then iRowidBind contains the
12675 ** index of the bind paramater to which the rowid value should be bound.
12676 ** Otherwise, it contains -1. If the table does contain an INTEGER PRIMARY
12677 ** KEY column, then the rowid value should be bound to the index associated
12678 ** with the column.
12679 **
12680 ** pNext:
12681 ** All RecoverTable objects used by the recovery operation are allocated
12682 ** and populated as part of creating the recovered database schema in
12683 ** the output database, before any non-schema data are recovered. They
12684 ** are then stored in a singly-linked list linked by this variable beginning
12685 ** at sqlite3_recover.pTblList.
12686 */
12687 struct RecoverTable {
12688 u32 iRoot; /* Root page in original database */
12689 char *zTab; /* Name of table */
12690 int nCol; /* Number of columns in table */
12691 RecoverColumn *aCol; /* Array of columns */
12692 int bIntkey; /* True for intkey, false for without rowid */
12693 int iRowidBind; /* If >0, bind rowid to INSERT here */
12694 RecoverTable *pNext;
12695 };
12696
12697 /*
12698 ** Each database column is represented by an instance of the following object
12699 ** stored in the RecoverTable.aCol[] array of the associated table.
12700 **
12701 ** iField:
12702 ** The index of the associated field within database records. Or -1 if
12703 ** there is no associated field (e.g. for virtual generated columns).
12704 **
12705 ** iBind:
12706 ** The bind index of the INSERT statement to bind this columns values
12707 ** to. Or 0 if there is no such index (iff (iField<0)).
12708 **
12709 ** bIPK:
12710 ** True if this is the INTEGER PRIMARY KEY column.
12711 **
12712 ** zCol:
12713 ** Name of column.
12714 **
12715 ** eHidden:
12716 ** A RECOVER_EHIDDEN_* constant value (see below for interpretation of each).
12717 */
12718 struct RecoverColumn {
12719 int iField; /* Field in record on disk */
12720 int iBind; /* Binding to use in INSERT */
12721 int bIPK; /* True for IPK column */
12722 char *zCol;
12723 int eHidden;
12724 };
12725
12726 #define RECOVER_EHIDDEN_NONE 0 /* Normal database column */
12727 #define RECOVER_EHIDDEN_HIDDEN 1 /* Column is __HIDDEN__ */
12728 #define RECOVER_EHIDDEN_VIRTUAL 2 /* Virtual generated column */
12729 #define RECOVER_EHIDDEN_STORED 3 /* Stored generated column */
12730
12731 /*
12732 ** Bitmap object used to track pages in the input database. Allocated
12733 ** and manipulated only by the following functions:
12734 **
12735 ** recoverBitmapAlloc()
12736 ** recoverBitmapFree()
12737 ** recoverBitmapSet()
12738 ** recoverBitmapQuery()
12739 **
12740 ** nPg:
12741 ** Largest page number that may be stored in the bitmap. The range
12742 ** of valid keys is 1 to nPg, inclusive.
12743 **
12744 ** aElem[]:
12745 ** Array large enough to contain a bit for each key. For key value
12746 ** iKey, the associated bit is the bit (iKey%32) of aElem[iKey/32].
12747 ** In other words, the following is true if bit iKey is set, or
12748 ** false if it is clear:
12749 **
12750 ** (aElem[iKey/32] & (1 << (iKey%32))) ? 1 : 0
12751 */
12752 typedef struct RecoverBitmap RecoverBitmap;
12753 struct RecoverBitmap {
12754 i64 nPg; /* Size of bitmap */
12755 u32 aElem[1]; /* Array of 32-bit bitmasks */
12756 };
12757
12758 /*
12759 ** State variables (part of the sqlite3_recover structure) used while
12760 ** recovering data for tables identified in the recovered schema (state
12761 ** RECOVER_STATE_WRITING).
12762 */
12763 typedef struct RecoverStateW1 RecoverStateW1;
12764 struct RecoverStateW1 {
12765 sqlite3_stmt *pTbls;
12766 sqlite3_stmt *pSel;
12767 sqlite3_stmt *pInsert;
12768 int nInsert;
12769
12770 RecoverTable *pTab; /* Table currently being written */
12771 int nMax; /* Max column count in any schema table */
12772 sqlite3_value **apVal; /* Array of nMax values */
12773 int nVal; /* Number of valid entries in apVal[] */
12774 int bHaveRowid;
12775 i64 iRowid;
12776 i64 iPrevPage;
12777 int iPrevCell;
12778 };
12779
12780 /*
12781 ** State variables (part of the sqlite3_recover structure) used while
12782 ** recovering data destined for the lost and found table (states
12783 ** RECOVER_STATE_LOSTANDFOUND[123]).
12784 */
12785 typedef struct RecoverStateLAF RecoverStateLAF;
12786 struct RecoverStateLAF {
12787 RecoverBitmap *pUsed;
12788 i64 nPg; /* Size of db in pages */
12789 sqlite3_stmt *pAllAndParent;
12790 sqlite3_stmt *pMapInsert;
12791 sqlite3_stmt *pMaxField;
12792 sqlite3_stmt *pUsedPages;
12793 sqlite3_stmt *pFindRoot;
12794 sqlite3_stmt *pInsert; /* INSERT INTO lost_and_found ... */
12795 sqlite3_stmt *pAllPage;
12796 sqlite3_stmt *pPageData;
12797 sqlite3_value **apVal;
12798 int nMaxField;
12799 };
12800
12801 /*
12802 ** Main recover handle structure.
12803 */
12804 struct sqlite3_recover {
12805 /* Copies of sqlite3_recover_init[_sql]() parameters */
12806 sqlite3 *dbIn; /* Input database */
12807 char *zDb; /* Name of input db ("main" etc.) */
12808 char *zUri; /* URI for output database */
12809 void *pSqlCtx; /* SQL callback context */
12810 int (*xSql)(void*,const char*); /* Pointer to SQL callback function */
12811
12812 /* Values configured by sqlite3_recover_config() */
12813 char *zStateDb; /* State database to use (or NULL) */
12814 char *zLostAndFound; /* Name of lost-and-found table (or NULL) */
12815 int bFreelistCorrupt; /* SQLITE_RECOVER_FREELIST_CORRUPT setting */
12816 int bRecoverRowid; /* SQLITE_RECOVER_ROWIDS setting */
12817 int bSlowIndexes; /* SQLITE_RECOVER_SLOWINDEXES setting */
12818
12819 int pgsz;
12820 int detected_pgsz;
12821 int nReserve;
12822 u8 *pPage1Disk;
12823 u8 *pPage1Cache;
12824
12825 /* Error code and error message */
12826 int errCode; /* For sqlite3_recover_errcode() */
12827 char *zErrMsg; /* For sqlite3_recover_errmsg() */
12828
12829 int eState;
12830 int bCloseTransaction;
12831
12832 /* Variables used with eState==RECOVER_STATE_WRITING */
12833 RecoverStateW1 w1;
12834
12835 /* Variables used with states RECOVER_STATE_LOSTANDFOUND[123] */
12836 RecoverStateLAF laf;
12837
12838 /* Fields used within sqlite3_recover_run() */
12839 sqlite3 *dbOut; /* Output database */
12840 sqlite3_stmt *pGetPage; /* SELECT against input db sqlite_dbdata */
12841 RecoverTable *pTblList; /* List of tables recovered from schema */
12842 };
12843
12844 /*
12845 ** The various states in which an sqlite3_recover object may exist:
12846 **
12847 ** RECOVER_STATE_INIT:
12848 ** The object is initially created in this state. sqlite3_recover_step()
12849 ** has yet to be called. This is the only state in which it is permitted
12850 ** to call sqlite3_recover_config().
12851 **
12852 ** RECOVER_STATE_WRITING:
12853 **
12854 ** RECOVER_STATE_LOSTANDFOUND1:
12855 ** State to populate the bitmap of pages used by other tables or the
12856 ** database freelist.
12857 **
12858 ** RECOVER_STATE_LOSTANDFOUND2:
12859 ** Populate the recovery.map table - used to figure out a "root" page
12860 ** for each lost page from in the database from which records are
12861 ** extracted.
12862 **
12863 ** RECOVER_STATE_LOSTANDFOUND3:
12864 ** Populate the lost-and-found table itself.
12865 */
12866 #define RECOVER_STATE_INIT 0
12867 #define RECOVER_STATE_WRITING 1
12868 #define RECOVER_STATE_LOSTANDFOUND1 2
12869 #define RECOVER_STATE_LOSTANDFOUND2 3
12870 #define RECOVER_STATE_LOSTANDFOUND3 4
12871 #define RECOVER_STATE_SCHEMA2 5
12872 #define RECOVER_STATE_DONE 6
12873
12874
12875 /*
12876 ** Global variables used by this extension.
12877 */
12878 typedef struct RecoverGlobal RecoverGlobal;
12879 struct RecoverGlobal {
12880 const sqlite3_io_methods *pMethods;
12881 sqlite3_recover *p;
12882 };
12883 static RecoverGlobal recover_g;
12884
12885 /*
12886 ** Use this static SQLite mutex to protect the globals during the
12887 ** first call to sqlite3_recover_step().
12888 */
12889 #define RECOVER_MUTEX_ID SQLITE_MUTEX_STATIC_APP2
12890
12891
12892 /*
12893 ** Default value for SQLITE_RECOVER_ROWIDS (sqlite3_recover.bRecoverRowid).
12894 */
12895 #define RECOVER_ROWID_DEFAULT 1
12896
12897 /*
12898 ** Mutex handling:
12899 **
12900 ** recoverEnterMutex() - Enter the recovery mutex
12901 ** recoverLeaveMutex() - Leave the recovery mutex
12902 ** recoverAssertMutexHeld() - Assert that the recovery mutex is held
12903 */
12904 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE==0
12905 # define recoverEnterMutex()
12906 # define recoverLeaveMutex()
12907 #else
12908 static void recoverEnterMutex(void){
12909 sqlite3_mutex_enter(sqlite3_mutex_alloc(RECOVER_MUTEX_ID));
12910 }
12911 static void recoverLeaveMutex(void){
12912 sqlite3_mutex_leave(sqlite3_mutex_alloc(RECOVER_MUTEX_ID));
12913 }
12914 #endif
12915 #if SQLITE_THREADSAFE+0>=1 && defined(SQLITE_DEBUG)
12916 static void recoverAssertMutexHeld(void){
12917 assert( sqlite3_mutex_held(sqlite3_mutex_alloc(RECOVER_MUTEX_ID)) );
12918 }
12919 #else
12920 # define recoverAssertMutexHeld()
12921 #endif
12922
12923
12924 /*
12925 ** Like strlen(). But handles NULL pointer arguments.
12926 */
12927 static int recoverStrlen(const char *zStr){
12928 if( zStr==0 ) return 0;
12929 return (int)(strlen(zStr)&0x7fffffff);
12930 }
12931
12932 /*
12933 ** This function is a no-op if the recover handle passed as the first
12934 ** argument already contains an error (if p->errCode!=SQLITE_OK).
12935 **
12936 ** Otherwise, an attempt is made to allocate, zero and return a buffer nByte
12937 ** bytes in size. If successful, a pointer to the new buffer is returned. Or,
12938 ** if an OOM error occurs, NULL is returned and the handle error code
12939 ** (p->errCode) set to SQLITE_NOMEM.
12940 */
12941 static void *recoverMalloc(sqlite3_recover *p, i64 nByte){
12942 void *pRet = 0;
12943 assert( nByte>0 );
12944 if( p->errCode==SQLITE_OK ){
12945 pRet = sqlite3_malloc64(nByte);
12946 if( pRet ){
12947 memset(pRet, 0, nByte);
12948 }else{
12949 p->errCode = SQLITE_NOMEM;
12950 }
12951 }
12952 return pRet;
12953 }
12954
12955 /*
12956 ** Set the error code and error message for the recover handle passed as
12957 ** the first argument. The error code is set to the value of parameter
12958 ** errCode.
12959 **
12960 ** Parameter zFmt must be a printf() style formatting string. The handle
12961 ** error message is set to the result of using any trailing arguments for
12962 ** parameter substitutions in the formatting string.
12963 **
12964 ** For example:
12965 **
12966 ** recoverError(p, SQLITE_ERROR, "no such table: %s", zTablename);
12967 */
12968 static int recoverError(
12969 sqlite3_recover *p,
12970 int errCode,
12971 const char *zFmt, ...
12972 ){
12973 char *z = 0;
12974 va_list ap;
12975 va_start(ap, zFmt);
12976 if( zFmt ){
12977 z = sqlite3_vmprintf(zFmt, ap);
12978 va_end(ap);
12979 }
12980 sqlite3_free(p->zErrMsg);
12981 p->zErrMsg = z;
12982 p->errCode = errCode;
12983 return errCode;
12984 }
12985
12986
12987 /*
12988 ** This function is a no-op if p->errCode is initially other than SQLITE_OK.
12989 ** In this case it returns NULL.
12990 **
12991 ** Otherwise, an attempt is made to allocate and return a bitmap object
12992 ** large enough to store a bit for all page numbers between 1 and nPg,
12993 ** inclusive. The bitmap is initially zeroed.
12994 */
12995 static RecoverBitmap *recoverBitmapAlloc(sqlite3_recover *p, i64 nPg){
12996 int nElem = (nPg+1+31) / 32;
12997 int nByte = sizeof(RecoverBitmap) + nElem*sizeof(u32);
12998 RecoverBitmap *pRet = (RecoverBitmap*)recoverMalloc(p, nByte);
12999
13000 if( pRet ){
13001 pRet->nPg = nPg;
13002 }
13003 return pRet;
13004 }
13005
13006 /*
13007 ** Free a bitmap object allocated by recoverBitmapAlloc().
13008 */
13009 static void recoverBitmapFree(RecoverBitmap *pMap){
13010 sqlite3_free(pMap);
13011 }
13012
13013 /*
13014 ** Set the bit associated with page iPg in bitvec pMap.
13015 */
13016 static void recoverBitmapSet(RecoverBitmap *pMap, i64 iPg){
13017 if( iPg<=pMap->nPg ){
13018 int iElem = (iPg / 32);
13019 int iBit = (iPg % 32);
13020 pMap->aElem[iElem] |= (((u32)1) << iBit);
13021 }
13022 }
13023
13024 /*
13025 ** Query bitmap object pMap for the state of the bit associated with page
13026 ** iPg. Return 1 if it is set, or 0 otherwise.
13027 */
13028 static int recoverBitmapQuery(RecoverBitmap *pMap, i64 iPg){
13029 int ret = 1;
13030 if( iPg<=pMap->nPg && iPg>0 ){
13031 int iElem = (iPg / 32);
13032 int iBit = (iPg % 32);
13033 ret = (pMap->aElem[iElem] & (((u32)1) << iBit)) ? 1 : 0;
13034 }
13035 return ret;
13036 }
13037
13038 /*
13039 ** Set the recover handle error to the error code and message returned by
13040 ** calling sqlite3_errcode() and sqlite3_errmsg(), respectively, on database
13041 ** handle db.
13042 */
13043 static int recoverDbError(sqlite3_recover *p, sqlite3 *db){
13044 return recoverError(p, sqlite3_errcode(db), "%s", sqlite3_errmsg(db));
13045 }
13046
13047 /*
13048 ** This function is a no-op if recover handle p already contains an error
13049 ** (if p->errCode!=SQLITE_OK).
13050 **
13051 ** Otherwise, it attempts to prepare the SQL statement in zSql against
13052 ** database handle db. If successful, the statement handle is returned.
13053 ** Or, if an error occurs, NULL is returned and an error left in the
13054 ** recover handle.
13055 */
13056 static sqlite3_stmt *recoverPrepare(
13057 sqlite3_recover *p,
13058 sqlite3 *db,
13059 const char *zSql
13060 ){
13061 sqlite3_stmt *pStmt = 0;
13062 if( p->errCode==SQLITE_OK ){
13063 if( sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0) ){
13064 recoverDbError(p, db);
13065 }
13066 }
13067 return pStmt;
13068 }
13069
13070 /*
13071 ** This function is a no-op if recover handle p already contains an error
13072 ** (if p->errCode!=SQLITE_OK).
13073 **
13074 ** Otherwise, argument zFmt is used as a printf() style format string,
13075 ** along with any trailing arguments, to create an SQL statement. This
13076 ** SQL statement is prepared against database handle db and, if successful,
13077 ** the statment handle returned. Or, if an error occurs - either during
13078 ** the printf() formatting or when preparing the resulting SQL - an
13079 ** error code and message are left in the recover handle.
13080 */
13081 static sqlite3_stmt *recoverPreparePrintf(
13082 sqlite3_recover *p,
13083 sqlite3 *db,
13084 const char *zFmt, ...
13085 ){
13086 sqlite3_stmt *pStmt = 0;
13087 if( p->errCode==SQLITE_OK ){
13088 va_list ap;
13089 char *z;
13090 va_start(ap, zFmt);
13091 z = sqlite3_vmprintf(zFmt, ap);
13092 va_end(ap);
13093 if( z==0 ){
13094 p->errCode = SQLITE_NOMEM;
13095 }else{
13096 pStmt = recoverPrepare(p, db, z);
13097 sqlite3_free(z);
13098 }
13099 }
13100 return pStmt;
13101 }
13102
13103 /*
13104 ** Reset SQLite statement handle pStmt. If the call to sqlite3_reset()
13105 ** indicates that an error occurred, and there is not already an error
13106 ** in the recover handle passed as the first argument, set the error
13107 ** code and error message appropriately.
13108 **
13109 ** This function returns a copy of the statement handle pointer passed
13110 ** as the second argument.
13111 */
13112 static sqlite3_stmt *recoverReset(sqlite3_recover *p, sqlite3_stmt *pStmt){
13113 int rc = sqlite3_reset(pStmt);
13114 if( rc!=SQLITE_OK && rc!=SQLITE_CONSTRAINT && p->errCode==SQLITE_OK ){
13115 recoverDbError(p, sqlite3_db_handle(pStmt));
13116 }
13117 return pStmt;
13118 }
13119
13120 /*
13121 ** Finalize SQLite statement handle pStmt. If the call to sqlite3_reset()
13122 ** indicates that an error occurred, and there is not already an error
13123 ** in the recover handle passed as the first argument, set the error
13124 ** code and error message appropriately.
13125 */
13126 static void recoverFinalize(sqlite3_recover *p, sqlite3_stmt *pStmt){
13127 sqlite3 *db = sqlite3_db_handle(pStmt);
13128 int rc = sqlite3_finalize(pStmt);
13129 if( rc!=SQLITE_OK && p->errCode==SQLITE_OK ){
13130 recoverDbError(p, db);
13131 }
13132 }
13133
13134 /*
13135 ** This function is a no-op if recover handle p already contains an error
13136 ** (if p->errCode!=SQLITE_OK). A copy of p->errCode is returned in this
13137 ** case.
13138 **
13139 ** Otherwise, execute SQL script zSql. If successful, return SQLITE_OK.
13140 ** Or, if an error occurs, leave an error code and message in the recover
13141 ** handle and return a copy of the error code.
13142 */
13143 static int recoverExec(sqlite3_recover *p, sqlite3 *db, const char *zSql){
13144 if( p->errCode==SQLITE_OK ){
13145 int rc = sqlite3_exec(db, zSql, 0, 0, 0);
13146 if( rc ){
13147 recoverDbError(p, db);
13148 }
13149 }
13150 return p->errCode;
13151 }
13152
13153 /*
13154 ** Bind the value pVal to parameter iBind of statement pStmt. Leave an
13155 ** error in the recover handle passed as the first argument if an error
13156 ** (e.g. an OOM) occurs.
13157 */
13158 static void recoverBindValue(
13159 sqlite3_recover *p,
13160 sqlite3_stmt *pStmt,
13161 int iBind,
13162 sqlite3_value *pVal
13163 ){
13164 if( p->errCode==SQLITE_OK ){
13165 int rc = sqlite3_bind_value(pStmt, iBind, pVal);
13166 if( rc ) recoverError(p, rc, 0);
13167 }
13168 }
13169
13170 /*
13171 ** This function is a no-op if recover handle p already contains an error
13172 ** (if p->errCode!=SQLITE_OK). NULL is returned in this case.
13173 **
13174 ** Otherwise, an attempt is made to interpret zFmt as a printf() style
13175 ** formatting string and the result of using the trailing arguments for
13176 ** parameter substitution with it written into a buffer obtained from
13177 ** sqlite3_malloc(). If successful, a pointer to the buffer is returned.
13178 ** It is the responsibility of the caller to eventually free the buffer
13179 ** using sqlite3_free().
13180 **
13181 ** Or, if an error occurs, an error code and message is left in the recover
13182 ** handle and NULL returned.
13183 */
13184 static char *recoverMPrintf(sqlite3_recover *p, const char *zFmt, ...){
13185 va_list ap;
13186 char *z;
13187 va_start(ap, zFmt);
13188 z = sqlite3_vmprintf(zFmt, ap);
13189 va_end(ap);
13190 if( p->errCode==SQLITE_OK ){
13191 if( z==0 ) p->errCode = SQLITE_NOMEM;
13192 }else{
13193 sqlite3_free(z);
13194 z = 0;
13195 }
13196 return z;
13197 }
13198
13199 /*
13200 ** This function is a no-op if recover handle p already contains an error
13201 ** (if p->errCode!=SQLITE_OK). Zero is returned in this case.
13202 **
13203 ** Otherwise, execute "PRAGMA page_count" against the input database. If
13204 ** successful, return the integer result. Or, if an error occurs, leave an
13205 ** error code and error message in the sqlite3_recover handle and return
13206 ** zero.
13207 */
13208 static i64 recoverPageCount(sqlite3_recover *p){
13209 i64 nPg = 0;
13210 if( p->errCode==SQLITE_OK ){
13211 sqlite3_stmt *pStmt = 0;
13212 pStmt = recoverPreparePrintf(p, p->dbIn, "PRAGMA %Q.page_count", p->zDb);
13213 if( pStmt ){
13214 sqlite3_step(pStmt);
13215 nPg = sqlite3_column_int64(pStmt, 0);
13216 }
13217 recoverFinalize(p, pStmt);
13218 }
13219 return nPg;
13220 }
13221
13222 /*
13223 ** Implementation of SQL scalar function "read_i32". The first argument to
13224 ** this function must be a blob. The second a non-negative integer. This
13225 ** function reads and returns a 32-bit big-endian integer from byte
13226 ** offset (4*<arg2>) of the blob.
13227 **
13228 ** SELECT read_i32(<blob>, <idx>)
13229 */
13230 static void recoverReadI32(
13231 sqlite3_context *context,
13232 int argc,
13233 sqlite3_value **argv
13234 ){
13235 const unsigned char *pBlob;
13236 int nBlob;
13237 int iInt;
13238
13239 assert( argc==2 );
13240 nBlob = sqlite3_value_bytes(argv[0]);
13241 pBlob = (const unsigned char*)sqlite3_value_blob(argv[0]);
13242 iInt = sqlite3_value_int(argv[1]) & 0xFFFF;
13243
13244 if( (iInt+1)*4<=nBlob ){
13245 const unsigned char *a = &pBlob[iInt*4];
13246 i64 iVal = ((i64)a[0]<<24)
13247 + ((i64)a[1]<<16)
13248 + ((i64)a[2]<< 8)
13249 + ((i64)a[3]<< 0);
13250 sqlite3_result_int64(context, iVal);
13251 }
13252 }
13253
13254 /*
13255 ** Implementation of SQL scalar function "page_is_used". This function
13256 ** is used as part of the procedure for locating orphan rows for the
13257 ** lost-and-found table, and it depends on those routines having populated
13258 ** the sqlite3_recover.laf.pUsed variable.
13259 **
13260 ** The only argument to this function is a page-number. It returns true
13261 ** if the page has already been used somehow during data recovery, or false
13262 ** otherwise.
13263 **
13264 ** SELECT page_is_used(<pgno>);
13265 */
13266 static void recoverPageIsUsed(
13267 sqlite3_context *pCtx,
13268 int nArg,
13269 sqlite3_value **apArg
13270 ){
13271 sqlite3_recover *p = (sqlite3_recover*)sqlite3_user_data(pCtx);
13272 i64 pgno = sqlite3_value_int64(apArg[0]);
13273 assert( nArg==1 );
13274 sqlite3_result_int(pCtx, recoverBitmapQuery(p->laf.pUsed, pgno));
13275 }
13276
13277 /*
13278 ** The implementation of a user-defined SQL function invoked by the
13279 ** sqlite_dbdata and sqlite_dbptr virtual table modules to access pages
13280 ** of the database being recovered.
13281 **
13282 ** This function always takes a single integer argument. If the argument
13283 ** is zero, then the value returned is the number of pages in the db being
13284 ** recovered. If the argument is greater than zero, it is a page number.
13285 ** The value returned in this case is an SQL blob containing the data for
13286 ** the identified page of the db being recovered. e.g.
13287 **
13288 ** SELECT getpage(0); -- return number of pages in db
13289 ** SELECT getpage(4); -- return page 4 of db as a blob of data
13290 */
13291 static void recoverGetPage(
13292 sqlite3_context *pCtx,
13293 int nArg,
13294 sqlite3_value **apArg
13295 ){
13296 sqlite3_recover *p = (sqlite3_recover*)sqlite3_user_data(pCtx);
13297 i64 pgno = sqlite3_value_int64(apArg[0]);
13298 sqlite3_stmt *pStmt = 0;
13299
13300 assert( nArg==1 );
13301 if( pgno==0 ){
13302 i64 nPg = recoverPageCount(p);
13303 sqlite3_result_int64(pCtx, nPg);
13304 return;
13305 }else{
13306 if( p->pGetPage==0 ){
13307 pStmt = p->pGetPage = recoverPreparePrintf(
13308 p, p->dbIn, "SELECT data FROM sqlite_dbpage(%Q) WHERE pgno=?", p->zDb
13309 );
13310 }else if( p->errCode==SQLITE_OK ){
13311 pStmt = p->pGetPage;
13312 }
13313
13314 if( pStmt ){
13315 sqlite3_bind_int64(pStmt, 1, pgno);
13316 if( SQLITE_ROW==sqlite3_step(pStmt) ){
13317 const u8 *aPg;
13318 int nPg;
13319 assert( p->errCode==SQLITE_OK );
13320 aPg = sqlite3_column_blob(pStmt, 0);
13321 nPg = sqlite3_column_bytes(pStmt, 0);
13322 if( pgno==1 && nPg==p->pgsz && 0==memcmp(p->pPage1Cache, aPg, nPg) ){
13323 aPg = p->pPage1Disk;
13324 }
13325 sqlite3_result_blob(pCtx, aPg, nPg-p->nReserve, SQLITE_TRANSIENT);
13326 }
13327 recoverReset(p, pStmt);
13328 }
13329 }
13330
13331 if( p->errCode ){
13332 if( p->zErrMsg ) sqlite3_result_error(pCtx, p->zErrMsg, -1);
13333 sqlite3_result_error_code(pCtx, p->errCode);
13334 }
13335 }
13336
13337 /*
13338 ** Find a string that is not found anywhere in z[]. Return a pointer
13339 ** to that string.
13340 **
13341 ** Try to use zA and zB first. If both of those are already found in z[]
13342 ** then make up some string and store it in the buffer zBuf.
13343 */
13344 static const char *recoverUnusedString(
13345 const char *z, /* Result must not appear anywhere in z */
13346 const char *zA, const char *zB, /* Try these first */
13347 char *zBuf /* Space to store a generated string */
13348 ){
13349 unsigned i = 0;
13350 if( strstr(z, zA)==0 ) return zA;
13351 if( strstr(z, zB)==0 ) return zB;
13352 do{
13353 sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
13354 }while( strstr(z,zBuf)!=0 );
13355 return zBuf;
13356 }
13357
13358 /*
13359 ** Implementation of scalar SQL function "escape_crnl". The argument passed to
13360 ** this function is the output of built-in function quote(). If the first
13361 ** character of the input is "'", indicating that the value passed to quote()
13362 ** was a text value, then this function searches the input for "\n" and "\r"
13363 ** characters and adds a wrapper similar to the following:
13364 **
13365 ** replace(replace(<input>, '\n', char(10), '\r', char(13));
13366 **
13367 ** Or, if the first character of the input is not "'", then a copy of the input
13368 ** is returned.
13369 */
13370 static void recoverEscapeCrnl(
13371 sqlite3_context *context,
13372 int argc,
13373 sqlite3_value **argv
13374 ){
13375 const char *zText = (const char*)sqlite3_value_text(argv[0]);
13376 if( zText && zText[0]=='\'' ){
13377 int nText = sqlite3_value_bytes(argv[0]);
13378 int i;
13379 char zBuf1[20];
13380 char zBuf2[20];
13381 const char *zNL = 0;
13382 const char *zCR = 0;
13383 int nCR = 0;
13384 int nNL = 0;
13385
13386 for(i=0; zText[i]; i++){
13387 if( zNL==0 && zText[i]=='\n' ){
13388 zNL = recoverUnusedString(zText, "\\n", "\\012", zBuf1);
13389 nNL = (int)strlen(zNL);
13390 }
13391 if( zCR==0 && zText[i]=='\r' ){
13392 zCR = recoverUnusedString(zText, "\\r", "\\015", zBuf2);
13393 nCR = (int)strlen(zCR);
13394 }
13395 }
13396
13397 if( zNL || zCR ){
13398 int iOut = 0;
13399 i64 nMax = (nNL > nCR) ? nNL : nCR;
13400 i64 nAlloc = nMax * nText + (nMax+64)*2;
13401 char *zOut = (char*)sqlite3_malloc64(nAlloc);
13402 if( zOut==0 ){
13403 sqlite3_result_error_nomem(context);
13404 return;
13405 }
13406
13407 if( zNL && zCR ){
13408 memcpy(&zOut[iOut], "replace(replace(", 16);
13409 iOut += 16;
13410 }else{
13411 memcpy(&zOut[iOut], "replace(", 8);
13412 iOut += 8;
13413 }
13414 for(i=0; zText[i]; i++){
13415 if( zText[i]=='\n' ){
13416 memcpy(&zOut[iOut], zNL, nNL);
13417 iOut += nNL;
13418 }else if( zText[i]=='\r' ){
13419 memcpy(&zOut[iOut], zCR, nCR);
13420 iOut += nCR;
13421 }else{
13422 zOut[iOut] = zText[i];
13423 iOut++;
13424 }
13425 }
13426
13427 if( zNL ){
13428 memcpy(&zOut[iOut], ",'", 2); iOut += 2;
13429 memcpy(&zOut[iOut], zNL, nNL); iOut += nNL;
13430 memcpy(&zOut[iOut], "', char(10))", 12); iOut += 12;
13431 }
13432 if( zCR ){
13433 memcpy(&zOut[iOut], ",'", 2); iOut += 2;
13434 memcpy(&zOut[iOut], zCR, nCR); iOut += nCR;
13435 memcpy(&zOut[iOut], "', char(13))", 12); iOut += 12;
13436 }
13437
13438 sqlite3_result_text(context, zOut, iOut, SQLITE_TRANSIENT);
13439 sqlite3_free(zOut);
13440 return;
13441 }
13442 }
13443
13444 sqlite3_result_value(context, argv[0]);
13445 }
13446
13447 /*
13448 ** This function is a no-op if recover handle p already contains an error
13449 ** (if p->errCode!=SQLITE_OK). A copy of the error code is returned in
13450 ** this case.
13451 **
13452 ** Otherwise, attempt to populate temporary table "recovery.schema" with the
13453 ** parts of the database schema that can be extracted from the input database.
13454 **
13455 ** If no error occurs, SQLITE_OK is returned. Otherwise, an error code
13456 ** and error message are left in the recover handle and a copy of the
13457 ** error code returned. It is not considered an error if part of all of
13458 ** the database schema cannot be recovered due to corruption.
13459 */
13460 static int recoverCacheSchema(sqlite3_recover *p){
13461 return recoverExec(p, p->dbOut,
13462 "WITH RECURSIVE pages(p) AS ("
13463 " SELECT 1"
13464 " UNION"
13465 " SELECT child FROM sqlite_dbptr('getpage()'), pages WHERE pgno=p"
13466 ")"
13467 "INSERT INTO recovery.schema SELECT"
13468 " max(CASE WHEN field=0 THEN value ELSE NULL END),"
13469 " max(CASE WHEN field=1 THEN value ELSE NULL END),"
13470 " max(CASE WHEN field=2 THEN value ELSE NULL END),"
13471 " max(CASE WHEN field=3 THEN value ELSE NULL END),"
13472 " max(CASE WHEN field=4 THEN value ELSE NULL END)"
13473 "FROM sqlite_dbdata('getpage()') WHERE pgno IN ("
13474 " SELECT p FROM pages"
13475 ") GROUP BY pgno, cell"
13476 );
13477 }
13478
13479 /*
13480 ** If this recover handle is not in SQL callback mode (i.e. was not created
13481 ** using sqlite3_recover_init_sql()) of if an error has already occurred,
13482 ** this function is a no-op. Otherwise, issue a callback with SQL statement
13483 ** zSql as the parameter.
13484 **
13485 ** If the callback returns non-zero, set the recover handle error code to
13486 ** the value returned (so that the caller will abandon processing).
13487 */
13488 static void recoverSqlCallback(sqlite3_recover *p, const char *zSql){
13489 if( p->errCode==SQLITE_OK && p->xSql ){
13490 int res = p->xSql(p->pSqlCtx, zSql);
13491 if( res ){
13492 recoverError(p, SQLITE_ERROR, "callback returned an error - %d", res);
13493 }
13494 }
13495 }
13496
13497 /*
13498 ** Transfer the following settings from the input database to the output
13499 ** database:
13500 **
13501 ** + page-size,
13502 ** + auto-vacuum settings,
13503 ** + database encoding,
13504 ** + user-version (PRAGMA user_version), and
13505 ** + application-id (PRAGMA application_id), and
13506 */
13507 static void recoverTransferSettings(sqlite3_recover *p){
13508 const char *aPragma[] = {
13509 "encoding",
13510 "page_size",
13511 "auto_vacuum",
13512 "user_version",
13513 "application_id"
13514 };
13515 int ii;
13516
13517 /* Truncate the output database to 0 pages in size. This is done by
13518 ** opening a new, empty, temp db, then using the backup API to clobber
13519 ** any existing output db with a copy of it. */
13520 if( p->errCode==SQLITE_OK ){
13521 sqlite3 *db2 = 0;
13522 int rc = sqlite3_open("", &db2);
13523 if( rc!=SQLITE_OK ){
13524 recoverDbError(p, db2);
13525 return;
13526 }
13527
13528 for(ii=0; ii<sizeof(aPragma)/sizeof(aPragma[0]); ii++){
13529 const char *zPrag = aPragma[ii];
13530 sqlite3_stmt *p1 = 0;
13531 p1 = recoverPreparePrintf(p, p->dbIn, "PRAGMA %Q.%s", p->zDb, zPrag);
13532 if( p->errCode==SQLITE_OK && sqlite3_step(p1)==SQLITE_ROW ){
13533 const char *zArg = (const char*)sqlite3_column_text(p1, 0);
13534 char *z2 = recoverMPrintf(p, "PRAGMA %s = %Q", zPrag, zArg);
13535 recoverSqlCallback(p, z2);
13536 recoverExec(p, db2, z2);
13537 sqlite3_free(z2);
13538 if( zArg==0 ){
13539 recoverError(p, SQLITE_NOMEM, 0);
13540 }
13541 }
13542 recoverFinalize(p, p1);
13543 }
13544 recoverExec(p, db2, "CREATE TABLE t1(a); DROP TABLE t1;");
13545
13546 if( p->errCode==SQLITE_OK ){
13547 sqlite3 *db = p->dbOut;
13548 sqlite3_backup *pBackup = sqlite3_backup_init(db, "main", db2, "main");
13549 if( pBackup ){
13550 sqlite3_backup_step(pBackup, -1);
13551 p->errCode = sqlite3_backup_finish(pBackup);
13552 }else{
13553 recoverDbError(p, db);
13554 }
13555 }
13556
13557 sqlite3_close(db2);
13558 }
13559 }
13560
13561 /*
13562 ** This function is a no-op if recover handle p already contains an error
13563 ** (if p->errCode!=SQLITE_OK). A copy of the error code is returned in
13564 ** this case.
13565 **
13566 ** Otherwise, an attempt is made to open the output database, attach
13567 ** and create the schema of the temporary database used to store
13568 ** intermediate data, and to register all required user functions and
13569 ** virtual table modules with the output handle.
13570 **
13571 ** If no error occurs, SQLITE_OK is returned. Otherwise, an error code
13572 ** and error message are left in the recover handle and a copy of the
13573 ** error code returned.
13574 */
13575 static int recoverOpenOutput(sqlite3_recover *p){
13576 struct Func {
13577 const char *zName;
13578 int nArg;
13579 void (*xFunc)(sqlite3_context*,int,sqlite3_value **);
13580 } aFunc[] = {
13581 { "getpage", 1, recoverGetPage },
13582 { "page_is_used", 1, recoverPageIsUsed },
13583 { "read_i32", 2, recoverReadI32 },
13584 { "escape_crnl", 1, recoverEscapeCrnl },
13585 };
13586
13587 const int flags = SQLITE_OPEN_URI|SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE;
13588 sqlite3 *db = 0; /* New database handle */
13589 int ii; /* For iterating through aFunc[] */
13590
13591 assert( p->dbOut==0 );
13592
13593 if( sqlite3_open_v2(p->zUri, &db, flags, 0) ){
13594 recoverDbError(p, db);
13595 }
13596
13597 /* Register the sqlite_dbdata and sqlite_dbptr virtual table modules.
13598 ** These two are registered with the output database handle - this
13599 ** module depends on the input handle supporting the sqlite_dbpage
13600 ** virtual table only. */
13601 if( p->errCode==SQLITE_OK ){
13602 p->errCode = sqlite3_dbdata_init(db, 0, 0);
13603 }
13604
13605 /* Register the custom user-functions with the output handle. */
13606 for(ii=0; p->errCode==SQLITE_OK && ii<sizeof(aFunc)/sizeof(aFunc[0]); ii++){
13607 p->errCode = sqlite3_create_function(db, aFunc[ii].zName,
13608 aFunc[ii].nArg, SQLITE_UTF8, (void*)p, aFunc[ii].xFunc, 0, 0
13609 );
13610 }
13611
13612 p->dbOut = db;
13613 return p->errCode;
13614 }
13615
13616 /*
13617 ** Attach the auxiliary database 'recovery' to the output database handle.
13618 ** This temporary database is used during the recovery process and then
13619 ** discarded.
13620 */
13621 static void recoverOpenRecovery(sqlite3_recover *p){
13622 char *zSql = recoverMPrintf(p, "ATTACH %Q AS recovery;", p->zStateDb);
13623 recoverExec(p, p->dbOut, zSql);
13624 recoverExec(p, p->dbOut,
13625 "PRAGMA writable_schema = 1;"
13626 "CREATE TABLE recovery.map(pgno INTEGER PRIMARY KEY, parent INT);"
13627 "CREATE TABLE recovery.schema(type, name, tbl_name, rootpage, sql);"
13628 );
13629 sqlite3_free(zSql);
13630 }
13631
13632
13633 /*
13634 ** This function is a no-op if recover handle p already contains an error
13635 ** (if p->errCode!=SQLITE_OK).
13636 **
13637 ** Otherwise, argument zName must be the name of a table that has just been
13638 ** created in the output database. This function queries the output db
13639 ** for the schema of said table, and creates a RecoverTable object to
13640 ** store the schema in memory. The new RecoverTable object is linked into
13641 ** the list at sqlite3_recover.pTblList.
13642 **
13643 ** Parameter iRoot must be the root page of table zName in the INPUT
13644 ** database.
13645 */
13646 static void recoverAddTable(
13647 sqlite3_recover *p,
13648 const char *zName, /* Name of table created in output db */
13649 i64 iRoot /* Root page of same table in INPUT db */
13650 ){
13651 sqlite3_stmt *pStmt = recoverPreparePrintf(p, p->dbOut,
13652 "PRAGMA table_xinfo(%Q)", zName
13653 );
13654
13655 if( pStmt ){
13656 int iPk = -1;
13657 int iBind = 1;
13658 RecoverTable *pNew = 0;
13659 int nCol = 0;
13660 int nName = recoverStrlen(zName);
13661 int nByte = 0;
13662 while( sqlite3_step(pStmt)==SQLITE_ROW ){
13663 nCol++;
13664 nByte += (sqlite3_column_bytes(pStmt, 1)+1);
13665 }
13666 nByte += sizeof(RecoverTable) + nCol*sizeof(RecoverColumn) + nName+1;
13667 recoverReset(p, pStmt);
13668
13669 pNew = recoverMalloc(p, nByte);
13670 if( pNew ){
13671 int i = 0;
13672 int iField = 0;
13673 char *csr = 0;
13674 pNew->aCol = (RecoverColumn*)&pNew[1];
13675 pNew->zTab = csr = (char*)&pNew->aCol[nCol];
13676 pNew->nCol = nCol;
13677 pNew->iRoot = iRoot;
13678 memcpy(csr, zName, nName);
13679 csr += nName+1;
13680
13681 for(i=0; sqlite3_step(pStmt)==SQLITE_ROW; i++){
13682 int iPKF = sqlite3_column_int(pStmt, 5);
13683 int n = sqlite3_column_bytes(pStmt, 1);
13684 const char *z = (const char*)sqlite3_column_text(pStmt, 1);
13685 const char *zType = (const char*)sqlite3_column_text(pStmt, 2);
13686 int eHidden = sqlite3_column_int(pStmt, 6);
13687
13688 if( iPk==-1 && iPKF==1 && !sqlite3_stricmp("integer", zType) ) iPk = i;
13689 if( iPKF>1 ) iPk = -2;
13690 pNew->aCol[i].zCol = csr;
13691 pNew->aCol[i].eHidden = eHidden;
13692 if( eHidden==RECOVER_EHIDDEN_VIRTUAL ){
13693 pNew->aCol[i].iField = -1;
13694 }else{
13695 pNew->aCol[i].iField = iField++;
13696 }
13697 if( eHidden!=RECOVER_EHIDDEN_VIRTUAL
13698 && eHidden!=RECOVER_EHIDDEN_STORED
13699 ){
13700 pNew->aCol[i].iBind = iBind++;
13701 }
13702 memcpy(csr, z, n);
13703 csr += (n+1);
13704 }
13705
13706 pNew->pNext = p->pTblList;
13707 p->pTblList = pNew;
13708 pNew->bIntkey = 1;
13709 }
13710
13711 recoverFinalize(p, pStmt);
13712
13713 pStmt = recoverPreparePrintf(p, p->dbOut, "PRAGMA index_xinfo(%Q)", zName);
13714 while( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
13715 int iField = sqlite3_column_int(pStmt, 0);
13716 int iCol = sqlite3_column_int(pStmt, 1);
13717
13718 assert( iField<pNew->nCol && iCol<pNew->nCol );
13719 pNew->aCol[iCol].iField = iField;
13720
13721 pNew->bIntkey = 0;
13722 iPk = -2;
13723 }
13724 recoverFinalize(p, pStmt);
13725
13726 if( p->errCode==SQLITE_OK ){
13727 if( iPk>=0 ){
13728 pNew->aCol[iPk].bIPK = 1;
13729 }else if( pNew->bIntkey ){
13730 pNew->iRowidBind = iBind++;
13731 }
13732 }
13733 }
13734 }
13735
13736 /*
13737 ** This function is called after recoverCacheSchema() has cached those parts
13738 ** of the input database schema that could be recovered in temporary table
13739 ** "recovery.schema". This function creates in the output database copies
13740 ** of all parts of that schema that must be created before the tables can
13741 ** be populated. Specifically, this means:
13742 **
13743 ** * all tables that are not VIRTUAL, and
13744 ** * UNIQUE indexes.
13745 **
13746 ** If the recovery handle uses SQL callbacks, then callbacks containing
13747 ** the associated "CREATE TABLE" and "CREATE INDEX" statements are made.
13748 **
13749 ** Additionally, records are added to the sqlite_schema table of the
13750 ** output database for any VIRTUAL tables. The CREATE VIRTUAL TABLE
13751 ** records are written directly to sqlite_schema, not actually executed.
13752 ** If the handle is in SQL callback mode, then callbacks are invoked
13753 ** with equivalent SQL statements.
13754 */
13755 static int recoverWriteSchema1(sqlite3_recover *p){
13756 sqlite3_stmt *pSelect = 0;
13757 sqlite3_stmt *pTblname = 0;
13758
13759 pSelect = recoverPrepare(p, p->dbOut,
13760 "WITH dbschema(rootpage, name, sql, tbl, isVirtual, isIndex) AS ("
13761 " SELECT rootpage, name, sql, "
13762 " type='table', "
13763 " sql LIKE 'create virtual%',"
13764 " (type='index' AND (sql LIKE '%unique%' OR ?1))"
13765 " FROM recovery.schema"
13766 ")"
13767 "SELECT rootpage, tbl, isVirtual, name, sql"
13768 " FROM dbschema "
13769 " WHERE tbl OR isIndex"
13770 " ORDER BY tbl DESC, name=='sqlite_sequence' DESC"
13771 );
13772
13773 pTblname = recoverPrepare(p, p->dbOut,
13774 "SELECT name FROM sqlite_schema "
13775 "WHERE type='table' ORDER BY rowid DESC LIMIT 1"
13776 );
13777
13778 if( pSelect ){
13779 sqlite3_bind_int(pSelect, 1, p->bSlowIndexes);
13780 while( sqlite3_step(pSelect)==SQLITE_ROW ){
13781 i64 iRoot = sqlite3_column_int64(pSelect, 0);
13782 int bTable = sqlite3_column_int(pSelect, 1);
13783 int bVirtual = sqlite3_column_int(pSelect, 2);
13784 const char *zName = (const char*)sqlite3_column_text(pSelect, 3);
13785 const char *zSql = (const char*)sqlite3_column_text(pSelect, 4);
13786 char *zFree = 0;
13787 int rc = SQLITE_OK;
13788
13789 if( bVirtual ){
13790 zSql = (const char*)(zFree = recoverMPrintf(p,
13791 "INSERT INTO sqlite_schema VALUES('table', %Q, %Q, 0, %Q)",
13792 zName, zName, zSql
13793 ));
13794 }
13795 rc = sqlite3_exec(p->dbOut, zSql, 0, 0, 0);
13796 if( rc==SQLITE_OK ){
13797 recoverSqlCallback(p, zSql);
13798 if( bTable && !bVirtual ){
13799 if( SQLITE_ROW==sqlite3_step(pTblname) ){
13800 const char *zTbl = (const char*)sqlite3_column_text(pTblname, 0);
13801 recoverAddTable(p, zTbl, iRoot);
13802 }
13803 recoverReset(p, pTblname);
13804 }
13805 }else if( rc!=SQLITE_ERROR ){
13806 recoverDbError(p, p->dbOut);
13807 }
13808 sqlite3_free(zFree);
13809 }
13810 }
13811 recoverFinalize(p, pSelect);
13812 recoverFinalize(p, pTblname);
13813
13814 return p->errCode;
13815 }
13816
13817 /*
13818 ** This function is called after the output database has been populated. It
13819 ** adds all recovered schema elements that were not created in the output
13820 ** database by recoverWriteSchema1() - everything except for tables and
13821 ** UNIQUE indexes. Specifically:
13822 **
13823 ** * views,
13824 ** * triggers,
13825 ** * non-UNIQUE indexes.
13826 **
13827 ** If the recover handle is in SQL callback mode, then equivalent callbacks
13828 ** are issued to create the schema elements.
13829 */
13830 static int recoverWriteSchema2(sqlite3_recover *p){
13831 sqlite3_stmt *pSelect = 0;
13832
13833 pSelect = recoverPrepare(p, p->dbOut,
13834 p->bSlowIndexes ?
13835 "SELECT rootpage, sql FROM recovery.schema "
13836 " WHERE type!='table' AND type!='index'"
13837 :
13838 "SELECT rootpage, sql FROM recovery.schema "
13839 " WHERE type!='table' AND (type!='index' OR sql NOT LIKE '%unique%')"
13840 );
13841
13842 if( pSelect ){
13843 while( sqlite3_step(pSelect)==SQLITE_ROW ){
13844 const char *zSql = (const char*)sqlite3_column_text(pSelect, 1);
13845 int rc = sqlite3_exec(p->dbOut, zSql, 0, 0, 0);
13846 if( rc==SQLITE_OK ){
13847 recoverSqlCallback(p, zSql);
13848 }else if( rc!=SQLITE_ERROR ){
13849 recoverDbError(p, p->dbOut);
13850 }
13851 }
13852 }
13853 recoverFinalize(p, pSelect);
13854
13855 return p->errCode;
13856 }
13857
13858 /*
13859 ** This function is a no-op if recover handle p already contains an error
13860 ** (if p->errCode!=SQLITE_OK). In this case it returns NULL.
13861 **
13862 ** Otherwise, if the recover handle is configured to create an output
13863 ** database (was created by sqlite3_recover_init()), then this function
13864 ** prepares and returns an SQL statement to INSERT a new record into table
13865 ** pTab, assuming the first nField fields of a record extracted from disk
13866 ** are valid.
13867 **
13868 ** For example, if table pTab is:
13869 **
13870 ** CREATE TABLE name(a, b GENERATED ALWAYS AS (a+1) STORED, c, d, e);
13871 **
13872 ** And nField is 4, then the SQL statement prepared and returned is:
13873 **
13874 ** INSERT INTO (a, c, d) VALUES (?1, ?2, ?3);
13875 **
13876 ** In this case even though 4 values were extracted from the input db,
13877 ** only 3 are written to the output, as the generated STORED column
13878 ** cannot be written.
13879 **
13880 ** If the recover handle is in SQL callback mode, then the SQL statement
13881 ** prepared is such that evaluating it returns a single row containing
13882 ** a single text value - itself an SQL statement similar to the above,
13883 ** except with SQL literals in place of the variables. For example:
13884 **
13885 ** SELECT 'INSERT INTO (a, c, d) VALUES ('
13886 ** || quote(?1) || ', '
13887 ** || quote(?2) || ', '
13888 ** || quote(?3) || ')';
13889 **
13890 ** In either case, it is the responsibility of the caller to eventually
13891 ** free the statement handle using sqlite3_finalize().
13892 */
13893 static sqlite3_stmt *recoverInsertStmt(
13894 sqlite3_recover *p,
13895 RecoverTable *pTab,
13896 int nField
13897 ){
13898 sqlite3_stmt *pRet = 0;
13899 const char *zSep = "";
13900 const char *zSqlSep = "";
13901 char *zSql = 0;
13902 char *zFinal = 0;
13903 char *zBind = 0;
13904 int ii;
13905 int bSql = p->xSql ? 1 : 0;
13906
13907 if( nField<=0 ) return 0;
13908
13909 assert( nField<=pTab->nCol );
13910
13911 zSql = recoverMPrintf(p, "INSERT OR IGNORE INTO %Q(", pTab->zTab);
13912
13913 if( pTab->iRowidBind ){
13914 assert( pTab->bIntkey );
13915 zSql = recoverMPrintf(p, "%z_rowid_", zSql);
13916 if( bSql ){
13917 zBind = recoverMPrintf(p, "%zquote(?%d)", zBind, pTab->iRowidBind);
13918 }else{
13919 zBind = recoverMPrintf(p, "%z?%d", zBind, pTab->iRowidBind);
13920 }
13921 zSqlSep = "||', '||";
13922 zSep = ", ";
13923 }
13924
13925 for(ii=0; ii<nField; ii++){
13926 int eHidden = pTab->aCol[ii].eHidden;
13927 if( eHidden!=RECOVER_EHIDDEN_VIRTUAL
13928 && eHidden!=RECOVER_EHIDDEN_STORED
13929 ){
13930 assert( pTab->aCol[ii].iField>=0 && pTab->aCol[ii].iBind>=1 );
13931 zSql = recoverMPrintf(p, "%z%s%Q", zSql, zSep, pTab->aCol[ii].zCol);
13932
13933 if( bSql ){
13934 zBind = recoverMPrintf(p,
13935 "%z%sescape_crnl(quote(?%d))", zBind, zSqlSep, pTab->aCol[ii].iBind
13936 );
13937 zSqlSep = "||', '||";
13938 }else{
13939 zBind = recoverMPrintf(p, "%z%s?%d", zBind, zSep, pTab->aCol[ii].iBind);
13940 }
13941 zSep = ", ";
13942 }
13943 }
13944
13945 if( bSql ){
13946 zFinal = recoverMPrintf(p, "SELECT %Q || ') VALUES (' || %s || ')'",
13947 zSql, zBind
13948 );
13949 }else{
13950 zFinal = recoverMPrintf(p, "%s) VALUES (%s)", zSql, zBind);
13951 }
13952
13953 pRet = recoverPrepare(p, p->dbOut, zFinal);
13954 sqlite3_free(zSql);
13955 sqlite3_free(zBind);
13956 sqlite3_free(zFinal);
13957
13958 return pRet;
13959 }
13960
13961
13962 /*
13963 ** Search the list of RecoverTable objects at p->pTblList for one that
13964 ** has root page iRoot in the input database. If such an object is found,
13965 ** return a pointer to it. Otherwise, return NULL.
13966 */
13967 static RecoverTable *recoverFindTable(sqlite3_recover *p, u32 iRoot){
13968 RecoverTable *pRet = 0;
13969 for(pRet=p->pTblList; pRet && pRet->iRoot!=iRoot; pRet=pRet->pNext);
13970 return pRet;
13971 }
13972
13973 /*
13974 ** This function attempts to create a lost and found table within the
13975 ** output db. If successful, it returns a pointer to a buffer containing
13976 ** the name of the new table. It is the responsibility of the caller to
13977 ** eventually free this buffer using sqlite3_free().
13978 **
13979 ** If an error occurs, NULL is returned and an error code and error
13980 ** message left in the recover handle.
13981 */
13982 static char *recoverLostAndFoundCreate(
13983 sqlite3_recover *p, /* Recover object */
13984 int nField /* Number of column fields in new table */
13985 ){
13986 char *zTbl = 0;
13987 sqlite3_stmt *pProbe = 0;
13988 int ii = 0;
13989
13990 pProbe = recoverPrepare(p, p->dbOut,
13991 "SELECT 1 FROM sqlite_schema WHERE name=?"
13992 );
13993 for(ii=-1; zTbl==0 && p->errCode==SQLITE_OK && ii<1000; ii++){
13994 int bFail = 0;
13995 if( ii<0 ){
13996 zTbl = recoverMPrintf(p, "%s", p->zLostAndFound);
13997 }else{
13998 zTbl = recoverMPrintf(p, "%s_%d", p->zLostAndFound, ii);
13999 }
14000
14001 if( p->errCode==SQLITE_OK ){
14002 sqlite3_bind_text(pProbe, 1, zTbl, -1, SQLITE_STATIC);
14003 if( SQLITE_ROW==sqlite3_step(pProbe) ){
14004 bFail = 1;
14005 }
14006 recoverReset(p, pProbe);
14007 }
14008
14009 if( bFail ){
14010 sqlite3_clear_bindings(pProbe);
14011 sqlite3_free(zTbl);
14012 zTbl = 0;
14013 }
14014 }
14015 recoverFinalize(p, pProbe);
14016
14017 if( zTbl ){
14018 const char *zSep = 0;
14019 char *zField = 0;
14020 char *zSql = 0;
14021
14022 zSep = "rootpgno INTEGER, pgno INTEGER, nfield INTEGER, id INTEGER, ";
14023 for(ii=0; p->errCode==SQLITE_OK && ii<nField; ii++){
14024 zField = recoverMPrintf(p, "%z%sc%d", zField, zSep, ii);
14025 zSep = ", ";
14026 }
14027
14028 zSql = recoverMPrintf(p, "CREATE TABLE %s(%s)", zTbl, zField);
14029 sqlite3_free(zField);
14030
14031 recoverExec(p, p->dbOut, zSql);
14032 recoverSqlCallback(p, zSql);
14033 sqlite3_free(zSql);
14034 }else if( p->errCode==SQLITE_OK ){
14035 recoverError(
14036 p, SQLITE_ERROR, "failed to create %s output table", p->zLostAndFound
14037 );
14038 }
14039
14040 return zTbl;
14041 }
14042
14043 /*
14044 ** Synthesize and prepare an INSERT statement to write to the lost_and_found
14045 ** table in the output database. The name of the table is zTab, and it has
14046 ** nField c* fields.
14047 */
14048 static sqlite3_stmt *recoverLostAndFoundInsert(
14049 sqlite3_recover *p,
14050 const char *zTab,
14051 int nField
14052 ){
14053 int nTotal = nField + 4;
14054 int ii;
14055 char *zBind = 0;
14056 sqlite3_stmt *pRet = 0;
14057
14058 if( p->xSql==0 ){
14059 for(ii=0; ii<nTotal; ii++){
14060 zBind = recoverMPrintf(p, "%z%s?", zBind, zBind?", ":"", ii);
14061 }
14062 pRet = recoverPreparePrintf(
14063 p, p->dbOut, "INSERT INTO %s VALUES(%s)", zTab, zBind
14064 );
14065 }else{
14066 const char *zSep = "";
14067 for(ii=0; ii<nTotal; ii++){
14068 zBind = recoverMPrintf(p, "%z%squote(?)", zBind, zSep);
14069 zSep = "|| ', ' ||";
14070 }
14071 pRet = recoverPreparePrintf(
14072 p, p->dbOut, "SELECT 'INSERT INTO %s VALUES(' || %s || ')'", zTab, zBind
14073 );
14074 }
14075
14076 sqlite3_free(zBind);
14077 return pRet;
14078 }
14079
14080 /*
14081 ** Input database page iPg contains data that will be written to the
14082 ** lost-and-found table of the output database. This function attempts
14083 ** to identify the root page of the tree that page iPg belonged to.
14084 ** If successful, it sets output variable (*piRoot) to the page number
14085 ** of the root page and returns SQLITE_OK. Otherwise, if an error occurs,
14086 ** an SQLite error code is returned and the final value of *piRoot
14087 ** undefined.
14088 */
14089 static int recoverLostAndFoundFindRoot(
14090 sqlite3_recover *p,
14091 i64 iPg,
14092 i64 *piRoot
14093 ){
14094 RecoverStateLAF *pLaf = &p->laf;
14095
14096 if( pLaf->pFindRoot==0 ){
14097 pLaf->pFindRoot = recoverPrepare(p, p->dbOut,
14098 "WITH RECURSIVE p(pgno) AS ("
14099 " SELECT ?"
14100 " UNION"
14101 " SELECT parent FROM recovery.map AS m, p WHERE m.pgno=p.pgno"
14102 ") "
14103 "SELECT p.pgno FROM p, recovery.map m WHERE m.pgno=p.pgno "
14104 " AND m.parent IS NULL"
14105 );
14106 }
14107 if( p->errCode==SQLITE_OK ){
14108 sqlite3_bind_int64(pLaf->pFindRoot, 1, iPg);
14109 if( sqlite3_step(pLaf->pFindRoot)==SQLITE_ROW ){
14110 *piRoot = sqlite3_column_int64(pLaf->pFindRoot, 0);
14111 }else{
14112 *piRoot = iPg;
14113 }
14114 recoverReset(p, pLaf->pFindRoot);
14115 }
14116 return p->errCode;
14117 }
14118
14119 /*
14120 ** Recover data from page iPage of the input database and write it to
14121 ** the lost-and-found table in the output database.
14122 */
14123 static void recoverLostAndFoundOnePage(sqlite3_recover *p, i64 iPage){
14124 RecoverStateLAF *pLaf = &p->laf;
14125 sqlite3_value **apVal = pLaf->apVal;
14126 sqlite3_stmt *pPageData = pLaf->pPageData;
14127 sqlite3_stmt *pInsert = pLaf->pInsert;
14128
14129 int nVal = -1;
14130 int iPrevCell = 0;
14131 i64 iRoot = 0;
14132 int bHaveRowid = 0;
14133 i64 iRowid = 0;
14134 int ii = 0;
14135
14136 if( recoverLostAndFoundFindRoot(p, iPage, &iRoot) ) return;
14137 sqlite3_bind_int64(pPageData, 1, iPage);
14138 while( p->errCode==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPageData) ){
14139 int iCell = sqlite3_column_int64(pPageData, 0);
14140 int iField = sqlite3_column_int64(pPageData, 1);
14141
14142 if( iPrevCell!=iCell && nVal>=0 ){
14143 /* Insert the new row */
14144 sqlite3_bind_int64(pInsert, 1, iRoot); /* rootpgno */
14145 sqlite3_bind_int64(pInsert, 2, iPage); /* pgno */
14146 sqlite3_bind_int(pInsert, 3, nVal); /* nfield */
14147 if( bHaveRowid ){
14148 sqlite3_bind_int64(pInsert, 4, iRowid); /* id */
14149 }
14150 for(ii=0; ii<nVal; ii++){
14151 recoverBindValue(p, pInsert, 5+ii, apVal[ii]);
14152 }
14153 if( sqlite3_step(pInsert)==SQLITE_ROW ){
14154 recoverSqlCallback(p, (const char*)sqlite3_column_text(pInsert, 0));
14155 }
14156 recoverReset(p, pInsert);
14157
14158 /* Discard the accumulated row data */
14159 for(ii=0; ii<nVal; ii++){
14160 sqlite3_value_free(apVal[ii]);
14161 apVal[ii] = 0;
14162 }
14163 sqlite3_clear_bindings(pInsert);
14164 bHaveRowid = 0;
14165 nVal = -1;
14166 }
14167
14168 if( iCell<0 ) break;
14169
14170 if( iField<0 ){
14171 assert( nVal==-1 );
14172 iRowid = sqlite3_column_int64(pPageData, 2);
14173 bHaveRowid = 1;
14174 nVal = 0;
14175 }else if( iField<pLaf->nMaxField ){
14176 sqlite3_value *pVal = sqlite3_column_value(pPageData, 2);
14177 apVal[iField] = sqlite3_value_dup(pVal);
14178 assert( iField==nVal || (nVal==-1 && iField==0) );
14179 nVal = iField+1;
14180 if( apVal[iField]==0 ){
14181 recoverError(p, SQLITE_NOMEM, 0);
14182 }
14183 }
14184
14185 iPrevCell = iCell;
14186 }
14187 recoverReset(p, pPageData);
14188
14189 for(ii=0; ii<nVal; ii++){
14190 sqlite3_value_free(apVal[ii]);
14191 apVal[ii] = 0;
14192 }
14193 }
14194
14195 /*
14196 ** Perform one step (sqlite3_recover_step()) of work for the connection
14197 ** passed as the only argument, which is guaranteed to be in
14198 ** RECOVER_STATE_LOSTANDFOUND3 state - during which the lost-and-found
14199 ** table of the output database is populated with recovered data that can
14200 ** not be assigned to any recovered schema object.
14201 */
14202 static int recoverLostAndFound3Step(sqlite3_recover *p){
14203 RecoverStateLAF *pLaf = &p->laf;
14204 if( p->errCode==SQLITE_OK ){
14205 if( pLaf->pInsert==0 ){
14206 return SQLITE_DONE;
14207 }else{
14208 if( p->errCode==SQLITE_OK ){
14209 int res = sqlite3_step(pLaf->pAllPage);
14210 if( res==SQLITE_ROW ){
14211 i64 iPage = sqlite3_column_int64(pLaf->pAllPage, 0);
14212 if( recoverBitmapQuery(pLaf->pUsed, iPage)==0 ){
14213 recoverLostAndFoundOnePage(p, iPage);
14214 }
14215 }else{
14216 recoverReset(p, pLaf->pAllPage);
14217 return SQLITE_DONE;
14218 }
14219 }
14220 }
14221 }
14222 return SQLITE_OK;
14223 }
14224
14225 /*
14226 ** Initialize resources required in RECOVER_STATE_LOSTANDFOUND3
14227 ** state - during which the lost-and-found table of the output database
14228 ** is populated with recovered data that can not be assigned to any
14229 ** recovered schema object.
14230 */
14231 static void recoverLostAndFound3Init(sqlite3_recover *p){
14232 RecoverStateLAF *pLaf = &p->laf;
14233
14234 if( pLaf->nMaxField>0 ){
14235 char *zTab = 0; /* Name of lost_and_found table */
14236
14237 zTab = recoverLostAndFoundCreate(p, pLaf->nMaxField);
14238 pLaf->pInsert = recoverLostAndFoundInsert(p, zTab, pLaf->nMaxField);
14239 sqlite3_free(zTab);
14240
14241 pLaf->pAllPage = recoverPreparePrintf(p, p->dbOut,
14242 "WITH RECURSIVE seq(ii) AS ("
14243 " SELECT 1 UNION ALL SELECT ii+1 FROM seq WHERE ii<%lld"
14244 ")"
14245 "SELECT ii FROM seq" , p->laf.nPg
14246 );
14247 pLaf->pPageData = recoverPrepare(p, p->dbOut,
14248 "SELECT cell, field, value "
14249 "FROM sqlite_dbdata('getpage()') d WHERE d.pgno=? "
14250 "UNION ALL "
14251 "SELECT -1, -1, -1"
14252 );
14253
14254 pLaf->apVal = (sqlite3_value**)recoverMalloc(p,
14255 pLaf->nMaxField*sizeof(sqlite3_value*)
14256 );
14257 }
14258 }
14259
14260 /*
14261 ** Initialize resources required in RECOVER_STATE_WRITING state - during which
14262 ** tables recovered from the schema of the input database are populated with
14263 ** recovered data.
14264 */
14265 static int recoverWriteDataInit(sqlite3_recover *p){
14266 RecoverStateW1 *p1 = &p->w1;
14267 RecoverTable *pTbl = 0;
14268 int nByte = 0;
14269
14270 /* Figure out the maximum number of columns for any table in the schema */
14271 assert( p1->nMax==0 );
14272 for(pTbl=p->pTblList; pTbl; pTbl=pTbl->pNext){
14273 if( pTbl->nCol>p1->nMax ) p1->nMax = pTbl->nCol;
14274 }
14275
14276 /* Allocate an array of (sqlite3_value*) in which to accumulate the values
14277 ** that will be written to the output database in a single row. */
14278 nByte = sizeof(sqlite3_value*) * (p1->nMax+1);
14279 p1->apVal = (sqlite3_value**)recoverMalloc(p, nByte);
14280 if( p1->apVal==0 ) return p->errCode;
14281
14282 /* Prepare the SELECT to loop through schema tables (pTbls) and the SELECT
14283 ** to loop through cells that appear to belong to a single table (pSel). */
14284 p1->pTbls = recoverPrepare(p, p->dbOut,
14285 "SELECT rootpage FROM recovery.schema "
14286 " WHERE type='table' AND (sql NOT LIKE 'create virtual%')"
14287 " ORDER BY (tbl_name='sqlite_sequence') ASC"
14288 );
14289 p1->pSel = recoverPrepare(p, p->dbOut,
14290 "WITH RECURSIVE pages(page) AS ("
14291 " SELECT ?1"
14292 " UNION"
14293 " SELECT child FROM sqlite_dbptr('getpage()'), pages "
14294 " WHERE pgno=page"
14295 ") "
14296 "SELECT page, cell, field, value "
14297 "FROM sqlite_dbdata('getpage()') d, pages p WHERE p.page=d.pgno "
14298 "UNION ALL "
14299 "SELECT 0, 0, 0, 0"
14300 );
14301
14302 return p->errCode;
14303 }
14304
14305 /*
14306 ** Clean up resources allocated by recoverWriteDataInit() (stuff in
14307 ** sqlite3_recover.w1).
14308 */
14309 static void recoverWriteDataCleanup(sqlite3_recover *p){
14310 RecoverStateW1 *p1 = &p->w1;
14311 int ii;
14312 for(ii=0; ii<p1->nVal; ii++){
14313 sqlite3_value_free(p1->apVal[ii]);
14314 }
14315 sqlite3_free(p1->apVal);
14316 recoverFinalize(p, p1->pInsert);
14317 recoverFinalize(p, p1->pTbls);
14318 recoverFinalize(p, p1->pSel);
14319 memset(p1, 0, sizeof(*p1));
14320 }
14321
14322 /*
14323 ** Perform one step (sqlite3_recover_step()) of work for the connection
14324 ** passed as the only argument, which is guaranteed to be in
14325 ** RECOVER_STATE_WRITING state - during which tables recovered from the
14326 ** schema of the input database are populated with recovered data.
14327 */
14328 static int recoverWriteDataStep(sqlite3_recover *p){
14329 RecoverStateW1 *p1 = &p->w1;
14330 sqlite3_stmt *pSel = p1->pSel;
14331 sqlite3_value **apVal = p1->apVal;
14332
14333 if( p->errCode==SQLITE_OK && p1->pTab==0 ){
14334 if( sqlite3_step(p1->pTbls)==SQLITE_ROW ){
14335 i64 iRoot = sqlite3_column_int64(p1->pTbls, 0);
14336 p1->pTab = recoverFindTable(p, iRoot);
14337
14338 recoverFinalize(p, p1->pInsert);
14339 p1->pInsert = 0;
14340
14341 /* If this table is unknown, return early. The caller will invoke this
14342 ** function again and it will move on to the next table. */
14343 if( p1->pTab==0 ) return p->errCode;
14344
14345 /* If this is the sqlite_sequence table, delete any rows added by
14346 ** earlier INSERT statements on tables with AUTOINCREMENT primary
14347 ** keys before recovering its contents. The p1->pTbls SELECT statement
14348 ** is rigged to deliver "sqlite_sequence" last of all, so we don't
14349 ** worry about it being modified after it is recovered. */
14350 if( sqlite3_stricmp("sqlite_sequence", p1->pTab->zTab)==0 ){
14351 recoverExec(p, p->dbOut, "DELETE FROM sqlite_sequence");
14352 recoverSqlCallback(p, "DELETE FROM sqlite_sequence");
14353 }
14354
14355 /* Bind the root page of this table within the original database to
14356 ** SELECT statement p1->pSel. The SELECT statement will then iterate
14357 ** through cells that look like they belong to table pTab. */
14358 sqlite3_bind_int64(pSel, 1, iRoot);
14359
14360 p1->nVal = 0;
14361 p1->bHaveRowid = 0;
14362 p1->iPrevPage = -1;
14363 p1->iPrevCell = -1;
14364 }else{
14365 return SQLITE_DONE;
14366 }
14367 }
14368 assert( p->errCode!=SQLITE_OK || p1->pTab );
14369
14370 if( p->errCode==SQLITE_OK && sqlite3_step(pSel)==SQLITE_ROW ){
14371 RecoverTable *pTab = p1->pTab;
14372
14373 i64 iPage = sqlite3_column_int64(pSel, 0);
14374 int iCell = sqlite3_column_int(pSel, 1);
14375 int iField = sqlite3_column_int(pSel, 2);
14376 sqlite3_value *pVal = sqlite3_column_value(pSel, 3);
14377 int bNewCell = (p1->iPrevPage!=iPage || p1->iPrevCell!=iCell);
14378
14379 assert( bNewCell==0 || (iField==-1 || iField==0) );
14380 assert( bNewCell || iField==p1->nVal || p1->nVal==pTab->nCol );
14381
14382 if( bNewCell ){
14383 int ii = 0;
14384 if( p1->nVal>=0 ){
14385 if( p1->pInsert==0 || p1->nVal!=p1->nInsert ){
14386 recoverFinalize(p, p1->pInsert);
14387 p1->pInsert = recoverInsertStmt(p, pTab, p1->nVal);
14388 p1->nInsert = p1->nVal;
14389 }
14390 if( p1->nVal>0 ){
14391 sqlite3_stmt *pInsert = p1->pInsert;
14392 for(ii=0; ii<pTab->nCol; ii++){
14393 RecoverColumn *pCol = &pTab->aCol[ii];
14394 int iBind = pCol->iBind;
14395 if( iBind>0 ){
14396 if( pCol->bIPK ){
14397 sqlite3_bind_int64(pInsert, iBind, p1->iRowid);
14398 }else if( pCol->iField<p1->nVal ){
14399 recoverBindValue(p, pInsert, iBind, apVal[pCol->iField]);
14400 }
14401 }
14402 }
14403 if( p->bRecoverRowid && pTab->iRowidBind>0 && p1->bHaveRowid ){
14404 sqlite3_bind_int64(pInsert, pTab->iRowidBind, p1->iRowid);
14405 }
14406 if( SQLITE_ROW==sqlite3_step(pInsert) ){
14407 const char *z = (const char*)sqlite3_column_text(pInsert, 0);
14408 recoverSqlCallback(p, z);
14409 }
14410 recoverReset(p, pInsert);
14411 assert( p->errCode || pInsert );
14412 if( pInsert ) sqlite3_clear_bindings(pInsert);
14413 }
14414 }
14415
14416 for(ii=0; ii<p1->nVal; ii++){
14417 sqlite3_value_free(apVal[ii]);
14418 apVal[ii] = 0;
14419 }
14420 p1->nVal = -1;
14421 p1->bHaveRowid = 0;
14422 }
14423
14424 if( iPage!=0 ){
14425 if( iField<0 ){
14426 p1->iRowid = sqlite3_column_int64(pSel, 3);
14427 assert( p1->nVal==-1 );
14428 p1->nVal = 0;
14429 p1->bHaveRowid = 1;
14430 }else if( iField<pTab->nCol ){
14431 assert( apVal[iField]==0 );
14432 apVal[iField] = sqlite3_value_dup( pVal );
14433 if( apVal[iField]==0 ){
14434 recoverError(p, SQLITE_NOMEM, 0);
14435 }
14436 p1->nVal = iField+1;
14437 }
14438 p1->iPrevCell = iCell;
14439 p1->iPrevPage = iPage;
14440 }
14441 }else{
14442 recoverReset(p, pSel);
14443 p1->pTab = 0;
14444 }
14445
14446 return p->errCode;
14447 }
14448
14449 /*
14450 ** Initialize resources required by sqlite3_recover_step() in
14451 ** RECOVER_STATE_LOSTANDFOUND1 state - during which the set of pages not
14452 ** already allocated to a recovered schema element is determined.
14453 */
14454 static void recoverLostAndFound1Init(sqlite3_recover *p){
14455 RecoverStateLAF *pLaf = &p->laf;
14456 sqlite3_stmt *pStmt = 0;
14457
14458 assert( p->laf.pUsed==0 );
14459 pLaf->nPg = recoverPageCount(p);
14460 pLaf->pUsed = recoverBitmapAlloc(p, pLaf->nPg);
14461
14462 /* Prepare a statement to iterate through all pages that are part of any tree
14463 ** in the recoverable part of the input database schema to the bitmap. And,
14464 ** if !p->bFreelistCorrupt, add all pages that appear to be part of the
14465 ** freelist. */
14466 pStmt = recoverPrepare(
14467 p, p->dbOut,
14468 "WITH trunk(pgno) AS ("
14469 " SELECT read_i32(getpage(1), 8) AS x WHERE x>0"
14470 " UNION"
14471 " SELECT read_i32(getpage(trunk.pgno), 0) AS x FROM trunk WHERE x>0"
14472 "),"
14473 "trunkdata(pgno, data) AS ("
14474 " SELECT pgno, getpage(pgno) FROM trunk"
14475 "),"
14476 "freelist(data, n, freepgno) AS ("
14477 " SELECT data, min(16384, read_i32(data, 1)-1), pgno FROM trunkdata"
14478 " UNION ALL"
14479 " SELECT data, n-1, read_i32(data, 2+n) FROM freelist WHERE n>=0"
14480 "),"
14481 ""
14482 "roots(r) AS ("
14483 " SELECT 1 UNION ALL"
14484 " SELECT rootpage FROM recovery.schema WHERE rootpage>0"
14485 "),"
14486 "used(page) AS ("
14487 " SELECT r FROM roots"
14488 " UNION"
14489 " SELECT child FROM sqlite_dbptr('getpage()'), used "
14490 " WHERE pgno=page"
14491 ") "
14492 "SELECT page FROM used"
14493 " UNION ALL "
14494 "SELECT freepgno FROM freelist WHERE NOT ?"
14495 );
14496 if( pStmt ) sqlite3_bind_int(pStmt, 1, p->bFreelistCorrupt);
14497 pLaf->pUsedPages = pStmt;
14498 }
14499
14500 /*
14501 ** Perform one step (sqlite3_recover_step()) of work for the connection
14502 ** passed as the only argument, which is guaranteed to be in
14503 ** RECOVER_STATE_LOSTANDFOUND1 state - during which the set of pages not
14504 ** already allocated to a recovered schema element is determined.
14505 */
14506 static int recoverLostAndFound1Step(sqlite3_recover *p){
14507 RecoverStateLAF *pLaf = &p->laf;
14508 int rc = p->errCode;
14509 if( rc==SQLITE_OK ){
14510 rc = sqlite3_step(pLaf->pUsedPages);
14511 if( rc==SQLITE_ROW ){
14512 i64 iPg = sqlite3_column_int64(pLaf->pUsedPages, 0);
14513 recoverBitmapSet(pLaf->pUsed, iPg);
14514 rc = SQLITE_OK;
14515 }else{
14516 recoverFinalize(p, pLaf->pUsedPages);
14517 pLaf->pUsedPages = 0;
14518 }
14519 }
14520 return rc;
14521 }
14522
14523 /*
14524 ** Initialize resources required by RECOVER_STATE_LOSTANDFOUND2
14525 ** state - during which the pages identified in RECOVER_STATE_LOSTANDFOUND1
14526 ** are sorted into sets that likely belonged to the same database tree.
14527 */
14528 static void recoverLostAndFound2Init(sqlite3_recover *p){
14529 RecoverStateLAF *pLaf = &p->laf;
14530
14531 assert( p->laf.pAllAndParent==0 );
14532 assert( p->laf.pMapInsert==0 );
14533 assert( p->laf.pMaxField==0 );
14534 assert( p->laf.nMaxField==0 );
14535
14536 pLaf->pMapInsert = recoverPrepare(p, p->dbOut,
14537 "INSERT OR IGNORE INTO recovery.map(pgno, parent) VALUES(?, ?)"
14538 );
14539 pLaf->pAllAndParent = recoverPreparePrintf(p, p->dbOut,
14540 "WITH RECURSIVE seq(ii) AS ("
14541 " SELECT 1 UNION ALL SELECT ii+1 FROM seq WHERE ii<%lld"
14542 ")"
14543 "SELECT pgno, child FROM sqlite_dbptr('getpage()') "
14544 " UNION ALL "
14545 "SELECT NULL, ii FROM seq", p->laf.nPg
14546 );
14547 pLaf->pMaxField = recoverPreparePrintf(p, p->dbOut,
14548 "SELECT max(field)+1 FROM sqlite_dbdata('getpage') WHERE pgno = ?"
14549 );
14550 }
14551
14552 /*
14553 ** Perform one step (sqlite3_recover_step()) of work for the connection
14554 ** passed as the only argument, which is guaranteed to be in
14555 ** RECOVER_STATE_LOSTANDFOUND2 state - during which the pages identified
14556 ** in RECOVER_STATE_LOSTANDFOUND1 are sorted into sets that likely belonged
14557 ** to the same database tree.
14558 */
14559 static int recoverLostAndFound2Step(sqlite3_recover *p){
14560 RecoverStateLAF *pLaf = &p->laf;
14561 if( p->errCode==SQLITE_OK ){
14562 int res = sqlite3_step(pLaf->pAllAndParent);
14563 if( res==SQLITE_ROW ){
14564 i64 iChild = sqlite3_column_int(pLaf->pAllAndParent, 1);
14565 if( recoverBitmapQuery(pLaf->pUsed, iChild)==0 ){
14566 sqlite3_bind_int64(pLaf->pMapInsert, 1, iChild);
14567 sqlite3_bind_value(pLaf->pMapInsert, 2,
14568 sqlite3_column_value(pLaf->pAllAndParent, 0)
14569 );
14570 sqlite3_step(pLaf->pMapInsert);
14571 recoverReset(p, pLaf->pMapInsert);
14572 sqlite3_bind_int64(pLaf->pMaxField, 1, iChild);
14573 if( SQLITE_ROW==sqlite3_step(pLaf->pMaxField) ){
14574 int nMax = sqlite3_column_int(pLaf->pMaxField, 0);
14575 if( nMax>pLaf->nMaxField ) pLaf->nMaxField = nMax;
14576 }
14577 recoverReset(p, pLaf->pMaxField);
14578 }
14579 }else{
14580 recoverFinalize(p, pLaf->pAllAndParent);
14581 pLaf->pAllAndParent =0;
14582 return SQLITE_DONE;
14583 }
14584 }
14585 return p->errCode;
14586 }
14587
14588 /*
14589 ** Free all resources allocated as part of sqlite3_recover_step() calls
14590 ** in one of the RECOVER_STATE_LOSTANDFOUND[123] states.
14591 */
14592 static void recoverLostAndFoundCleanup(sqlite3_recover *p){
14593 recoverBitmapFree(p->laf.pUsed);
14594 p->laf.pUsed = 0;
14595 sqlite3_finalize(p->laf.pUsedPages);
14596 sqlite3_finalize(p->laf.pAllAndParent);
14597 sqlite3_finalize(p->laf.pMapInsert);
14598 sqlite3_finalize(p->laf.pMaxField);
14599 sqlite3_finalize(p->laf.pFindRoot);
14600 sqlite3_finalize(p->laf.pInsert);
14601 sqlite3_finalize(p->laf.pAllPage);
14602 sqlite3_finalize(p->laf.pPageData);
14603 p->laf.pUsedPages = 0;
14604 p->laf.pAllAndParent = 0;
14605 p->laf.pMapInsert = 0;
14606 p->laf.pMaxField = 0;
14607 p->laf.pFindRoot = 0;
14608 p->laf.pInsert = 0;
14609 p->laf.pAllPage = 0;
14610 p->laf.pPageData = 0;
14611 sqlite3_free(p->laf.apVal);
14612 p->laf.apVal = 0;
14613 }
14614
14615 /*
14616 ** Free all resources allocated as part of sqlite3_recover_step() calls.
14617 */
14618 static void recoverFinalCleanup(sqlite3_recover *p){
14619 RecoverTable *pTab = 0;
14620 RecoverTable *pNext = 0;
14621
14622 recoverWriteDataCleanup(p);
14623 recoverLostAndFoundCleanup(p);
14624
14625 for(pTab=p->pTblList; pTab; pTab=pNext){
14626 pNext = pTab->pNext;
14627 sqlite3_free(pTab);
14628 }
14629 p->pTblList = 0;
14630 sqlite3_finalize(p->pGetPage);
14631 p->pGetPage = 0;
14632
14633 {
14634 #ifndef NDEBUG
14635 int res =
14636 #endif
14637 sqlite3_close(p->dbOut);
14638 assert( res==SQLITE_OK );
14639 }
14640 p->dbOut = 0;
14641 }
14642
14643 /*
14644 ** Decode and return an unsigned 16-bit big-endian integer value from
14645 ** buffer a[].
14646 */
14647 static u32 recoverGetU16(const u8 *a){
14648 return (((u32)a[0])<<8) + ((u32)a[1]);
14649 }
14650
14651 /*
14652 ** Decode and return an unsigned 32-bit big-endian integer value from
14653 ** buffer a[].
14654 */
14655 static u32 recoverGetU32(const u8 *a){
14656 return (((u32)a[0])<<24) + (((u32)a[1])<<16) + (((u32)a[2])<<8) + ((u32)a[3]);
14657 }
14658
14659 /*
14660 ** Decode an SQLite varint from buffer a[]. Write the decoded value to (*pVal)
14661 ** and return the number of bytes consumed.
14662 */
14663 static int recoverGetVarint(const u8 *a, i64 *pVal){
14664 sqlite3_uint64 u = 0;
14665 int i;
14666 for(i=0; i<8; i++){
14667 u = (u<<7) + (a[i]&0x7f);
14668 if( (a[i]&0x80)==0 ){ *pVal = (sqlite3_int64)u; return i+1; }
14669 }
14670 u = (u<<8) + (a[i]&0xff);
14671 *pVal = (sqlite3_int64)u;
14672 return 9;
14673 }
14674
14675 /*
14676 ** The second argument points to a buffer n bytes in size. If this buffer
14677 ** or a prefix thereof appears to contain a well-formed SQLite b-tree page,
14678 ** return the page-size in bytes. Otherwise, if the buffer does not
14679 ** appear to contain a well-formed b-tree page, return 0.
14680 */
14681 static int recoverIsValidPage(u8 *aTmp, const u8 *a, int n){
14682 u8 *aUsed = aTmp;
14683 int nFrag = 0;
14684 int nActual = 0;
14685 int iFree = 0;
14686 int nCell = 0; /* Number of cells on page */
14687 int iCellOff = 0; /* Offset of cell array in page */
14688 int iContent = 0;
14689 int eType = 0;
14690 int ii = 0;
14691
14692 eType = (int)a[0];
14693 if( eType!=0x02 && eType!=0x05 && eType!=0x0A && eType!=0x0D ) return 0;
14694
14695 iFree = (int)recoverGetU16(&a[1]);
14696 nCell = (int)recoverGetU16(&a[3]);
14697 iContent = (int)recoverGetU16(&a[5]);
14698 if( iContent==0 ) iContent = 65536;
14699 nFrag = (int)a[7];
14700
14701 if( iContent>n ) return 0;
14702
14703 memset(aUsed, 0, n);
14704 memset(aUsed, 0xFF, iContent);
14705
14706 /* Follow the free-list. This is the same format for all b-tree pages. */
14707 if( iFree && iFree<=iContent ) return 0;
14708 while( iFree ){
14709 int iNext = 0;
14710 int nByte = 0;
14711 if( iFree>(n-4) ) return 0;
14712 iNext = recoverGetU16(&a[iFree]);
14713 nByte = recoverGetU16(&a[iFree+2]);
14714 if( iFree+nByte>n ) return 0;
14715 if( iNext && iNext<iFree+nByte ) return 0;
14716 memset(&aUsed[iFree], 0xFF, nByte);
14717 iFree = iNext;
14718 }
14719
14720 /* Run through the cells */
14721 if( eType==0x02 || eType==0x05 ){
14722 iCellOff = 12;
14723 }else{
14724 iCellOff = 8;
14725 }
14726 if( (iCellOff + 2*nCell)>iContent ) return 0;
14727 for(ii=0; ii<nCell; ii++){
14728 int iByte;
14729 i64 nPayload = 0;
14730 int nByte = 0;
14731 int iOff = recoverGetU16(&a[iCellOff + 2*ii]);
14732 if( iOff<iContent || iOff>n ){
14733 return 0;
14734 }
14735 if( eType==0x05 || eType==0x02 ) nByte += 4;
14736 nByte += recoverGetVarint(&a[iOff+nByte], &nPayload);
14737 if( eType==0x0D ){
14738 i64 dummy = 0;
14739 nByte += recoverGetVarint(&a[iOff+nByte], &dummy);
14740 }
14741 if( eType!=0x05 ){
14742 int X = (eType==0x0D) ? n-35 : (((n-12)*64/255)-23);
14743 int M = ((n-12)*32/255)-23;
14744 int K = M+((nPayload-M)%(n-4));
14745
14746 if( nPayload<X ){
14747 nByte += nPayload;
14748 }else if( K<=X ){
14749 nByte += K+4;
14750 }else{
14751 nByte += M+4;
14752 }
14753 }
14754
14755 if( iOff+nByte>n ){
14756 return 0;
14757 }
14758 for(iByte=iOff; iByte<(iOff+nByte); iByte++){
14759 if( aUsed[iByte]!=0 ){
14760 return 0;
14761 }
14762 aUsed[iByte] = 0xFF;
14763 }
14764 }
14765
14766 nActual = 0;
14767 for(ii=0; ii<n; ii++){
14768 if( aUsed[ii]==0 ) nActual++;
14769 }
14770 return (nActual==nFrag);
14771 }
14772
14773
14774 static int recoverVfsClose(sqlite3_file*);
14775 static int recoverVfsRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
14776 static int recoverVfsWrite(sqlite3_file*, const void*, int, sqlite3_int64);
14777 static int recoverVfsTruncate(sqlite3_file*, sqlite3_int64 size);
14778 static int recoverVfsSync(sqlite3_file*, int flags);
14779 static int recoverVfsFileSize(sqlite3_file*, sqlite3_int64 *pSize);
14780 static int recoverVfsLock(sqlite3_file*, int);
14781 static int recoverVfsUnlock(sqlite3_file*, int);
14782 static int recoverVfsCheckReservedLock(sqlite3_file*, int *pResOut);
14783 static int recoverVfsFileControl(sqlite3_file*, int op, void *pArg);
14784 static int recoverVfsSectorSize(sqlite3_file*);
14785 static int recoverVfsDeviceCharacteristics(sqlite3_file*);
14786 static int recoverVfsShmMap(sqlite3_file*, int, int, int, void volatile**);
14787 static int recoverVfsShmLock(sqlite3_file*, int offset, int n, int flags);
14788 static void recoverVfsShmBarrier(sqlite3_file*);
14789 static int recoverVfsShmUnmap(sqlite3_file*, int deleteFlag);
14790 static int recoverVfsFetch(sqlite3_file*, sqlite3_int64, int, void**);
14791 static int recoverVfsUnfetch(sqlite3_file *pFd, sqlite3_int64 iOff, void *p);
14792
14793 static sqlite3_io_methods recover_methods = {
14794 2, /* iVersion */
14795 recoverVfsClose,
14796 recoverVfsRead,
14797 recoverVfsWrite,
14798 recoverVfsTruncate,
14799 recoverVfsSync,
14800 recoverVfsFileSize,
14801 recoverVfsLock,
14802 recoverVfsUnlock,
14803 recoverVfsCheckReservedLock,
14804 recoverVfsFileControl,
14805 recoverVfsSectorSize,
14806 recoverVfsDeviceCharacteristics,
14807 recoverVfsShmMap,
14808 recoverVfsShmLock,
14809 recoverVfsShmBarrier,
14810 recoverVfsShmUnmap,
14811 recoverVfsFetch,
14812 recoverVfsUnfetch
14813 };
14814
14815 static int recoverVfsClose(sqlite3_file *pFd){
14816 assert( pFd->pMethods!=&recover_methods );
14817 return pFd->pMethods->xClose(pFd);
14818 }
14819
14820 /*
14821 ** Write value v to buffer a[] as a 16-bit big-endian unsigned integer.
14822 */
14823 static void recoverPutU16(u8 *a, u32 v){
14824 a[0] = (v>>8) & 0x00FF;
14825 a[1] = (v>>0) & 0x00FF;
14826 }
14827
14828 /*
14829 ** Write value v to buffer a[] as a 32-bit big-endian unsigned integer.
14830 */
14831 static void recoverPutU32(u8 *a, u32 v){
14832 a[0] = (v>>24) & 0x00FF;
14833 a[1] = (v>>16) & 0x00FF;
14834 a[2] = (v>>8) & 0x00FF;
14835 a[3] = (v>>0) & 0x00FF;
14836 }
14837
14838 /*
14839 ** Detect the page-size of the database opened by file-handle pFd by
14840 ** searching the first part of the file for a well-formed SQLite b-tree
14841 ** page. If parameter nReserve is non-zero, then as well as searching for
14842 ** a b-tree page with zero reserved bytes, this function searches for one
14843 ** with nReserve reserved bytes at the end of it.
14844 **
14845 ** If successful, set variable p->detected_pgsz to the detected page-size
14846 ** in bytes and return SQLITE_OK. Or, if no error occurs but no valid page
14847 ** can be found, return SQLITE_OK but leave p->detected_pgsz set to 0. Or,
14848 ** if an error occurs (e.g. an IO or OOM error), then an SQLite error code
14849 ** is returned. The final value of p->detected_pgsz is undefined in this
14850 ** case.
14851 */
14852 static int recoverVfsDetectPagesize(
14853 sqlite3_recover *p, /* Recover handle */
14854 sqlite3_file *pFd, /* File-handle open on input database */
14855 u32 nReserve, /* Possible nReserve value */
14856 i64 nSz /* Size of database file in bytes */
14857 ){
14858 int rc = SQLITE_OK;
14859 const int nMin = 512;
14860 const int nMax = 65536;
14861 const int nMaxBlk = 4;
14862 u32 pgsz = 0;
14863 int iBlk = 0;
14864 u8 *aPg = 0;
14865 u8 *aTmp = 0;
14866 int nBlk = 0;
14867
14868 aPg = (u8*)sqlite3_malloc(2*nMax);
14869 if( aPg==0 ) return SQLITE_NOMEM;
14870 aTmp = &aPg[nMax];
14871
14872 nBlk = (nSz+nMax-1)/nMax;
14873 if( nBlk>nMaxBlk ) nBlk = nMaxBlk;
14874
14875 do {
14876 for(iBlk=0; rc==SQLITE_OK && iBlk<nBlk; iBlk++){
14877 int nByte = (nSz>=((iBlk+1)*nMax)) ? nMax : (nSz % nMax);
14878 memset(aPg, 0, nMax);
14879 rc = pFd->pMethods->xRead(pFd, aPg, nByte, iBlk*nMax);
14880 if( rc==SQLITE_OK ){
14881 int pgsz2;
14882 for(pgsz2=(pgsz ? pgsz*2 : nMin); pgsz2<=nMax; pgsz2=pgsz2*2){
14883 int iOff;
14884 for(iOff=0; iOff<nMax; iOff+=pgsz2){
14885 if( recoverIsValidPage(aTmp, &aPg[iOff], pgsz2-nReserve) ){
14886 pgsz = pgsz2;
14887 break;
14888 }
14889 }
14890 }
14891 }
14892 }
14893 if( pgsz>(u32)p->detected_pgsz ){
14894 p->detected_pgsz = pgsz;
14895 p->nReserve = nReserve;
14896 }
14897 if( nReserve==0 ) break;
14898 nReserve = 0;
14899 }while( 1 );
14900
14901 p->detected_pgsz = pgsz;
14902 sqlite3_free(aPg);
14903 return rc;
14904 }
14905
14906 /*
14907 ** The xRead() method of the wrapper VFS. This is used to intercept calls
14908 ** to read page 1 of the input database.
14909 */
14910 static int recoverVfsRead(sqlite3_file *pFd, void *aBuf, int nByte, i64 iOff){
14911 int rc = SQLITE_OK;
14912 if( pFd->pMethods==&recover_methods ){
14913 pFd->pMethods = recover_g.pMethods;
14914 rc = pFd->pMethods->xRead(pFd, aBuf, nByte, iOff);
14915 if( nByte==16 ){
14916 sqlite3_randomness(16, aBuf);
14917 }else
14918 if( rc==SQLITE_OK && iOff==0 && nByte>=108 ){
14919 /* Ensure that the database has a valid header file. The only fields
14920 ** that really matter to recovery are:
14921 **
14922 ** + Database page size (16-bits at offset 16)
14923 ** + Size of db in pages (32-bits at offset 28)
14924 ** + Database encoding (32-bits at offset 56)
14925 **
14926 ** Also preserved are:
14927 **
14928 ** + first freelist page (32-bits at offset 32)
14929 ** + size of freelist (32-bits at offset 36)
14930 **
14931 ** We also try to preserve the auto-vacuum, incr-value, user-version
14932 ** and application-id fields - all 32 bit quantities at offsets
14933 ** 52, 60, 64 and 68. All other fields are set to known good values.
14934 **
14935 ** Byte offset 105 should also contain the page-size as a 16-bit
14936 ** integer.
14937 */
14938 const int aPreserve[] = {32, 36, 52, 60, 64, 68};
14939 u8 aHdr[108] = {
14940 0x53, 0x51, 0x4c, 0x69, 0x74, 0x65, 0x20, 0x66,
14941 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x20, 0x33, 0x00,
14942 0xFF, 0xFF, 0x01, 0x01, 0x00, 0x40, 0x20, 0x20,
14943 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
14944 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
14945 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
14946 0x00, 0x00, 0x10, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
14947 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
14948 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
14949 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
14950 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
14951 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
14952 0x00, 0x2e, 0x5b, 0x30,
14953
14954 0x0D, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00
14955 };
14956 u8 *a = (u8*)aBuf;
14957
14958 u32 pgsz = recoverGetU16(&a[16]);
14959 u32 nReserve = a[20];
14960 u32 enc = recoverGetU32(&a[56]);
14961 u32 dbsz = 0;
14962 i64 dbFileSize = 0;
14963 int ii;
14964 sqlite3_recover *p = recover_g.p;
14965
14966 if( pgsz==0x01 ) pgsz = 65536;
14967 rc = pFd->pMethods->xFileSize(pFd, &dbFileSize);
14968
14969 if( rc==SQLITE_OK && p->detected_pgsz==0 ){
14970 rc = recoverVfsDetectPagesize(p, pFd, nReserve, dbFileSize);
14971 }
14972 if( p->detected_pgsz ){
14973 pgsz = p->detected_pgsz;
14974 nReserve = p->nReserve;
14975 }
14976
14977 if( pgsz ){
14978 dbsz = dbFileSize / pgsz;
14979 }
14980 if( enc!=SQLITE_UTF8 && enc!=SQLITE_UTF16BE && enc!=SQLITE_UTF16LE ){
14981 enc = SQLITE_UTF8;
14982 }
14983
14984 sqlite3_free(p->pPage1Cache);
14985 p->pPage1Cache = 0;
14986 p->pPage1Disk = 0;
14987
14988 p->pgsz = nByte;
14989 p->pPage1Cache = (u8*)recoverMalloc(p, nByte*2);
14990 if( p->pPage1Cache ){
14991 p->pPage1Disk = &p->pPage1Cache[nByte];
14992 memcpy(p->pPage1Disk, aBuf, nByte);
14993
14994 recoverPutU32(&aHdr[28], dbsz);
14995 recoverPutU32(&aHdr[56], enc);
14996 recoverPutU16(&aHdr[105], pgsz-nReserve);
14997 if( pgsz==65536 ) pgsz = 1;
14998 recoverPutU16(&aHdr[16], pgsz);
14999 aHdr[20] = nReserve;
15000 for(ii=0; ii<sizeof(aPreserve)/sizeof(aPreserve[0]); ii++){
15001 memcpy(&aHdr[aPreserve[ii]], &a[aPreserve[ii]], 4);
15002 }
15003 memcpy(aBuf, aHdr, sizeof(aHdr));
15004 memset(&((u8*)aBuf)[sizeof(aHdr)], 0, nByte-sizeof(aHdr));
15005
15006 memcpy(p->pPage1Cache, aBuf, nByte);
15007 }else{
15008 rc = p->errCode;
15009 }
15010
15011 }
15012 pFd->pMethods = &recover_methods;
15013 }else{
15014 rc = pFd->pMethods->xRead(pFd, aBuf, nByte, iOff);
15015 }
15016 return rc;
15017 }
15018
15019 /*
15020 ** Used to make sqlite3_io_methods wrapper methods less verbose.
15021 */
15022 #define RECOVER_VFS_WRAPPER(code) \
15023 int rc = SQLITE_OK; \
15024 if( pFd->pMethods==&recover_methods ){ \
15025 pFd->pMethods = recover_g.pMethods; \
15026 rc = code; \
15027 pFd->pMethods = &recover_methods; \
15028 }else{ \
15029 rc = code; \
15030 } \
15031 return rc;
15032
15033 /*
15034 ** Methods of the wrapper VFS. All methods except for xRead() and xClose()
15035 ** simply uninstall the sqlite3_io_methods wrapper, invoke the equivalent
15036 ** method on the lower level VFS, then reinstall the wrapper before returning.
15037 ** Those that return an integer value use the RECOVER_VFS_WRAPPER macro.
15038 */
15039 static int recoverVfsWrite(
15040 sqlite3_file *pFd, const void *aBuf, int nByte, i64 iOff
15041 ){
15042 RECOVER_VFS_WRAPPER (
15043 pFd->pMethods->xWrite(pFd, aBuf, nByte, iOff)
15044 );
15045 }
15046 static int recoverVfsTruncate(sqlite3_file *pFd, sqlite3_int64 size){
15047 RECOVER_VFS_WRAPPER (
15048 pFd->pMethods->xTruncate(pFd, size)
15049 );
15050 }
15051 static int recoverVfsSync(sqlite3_file *pFd, int flags){
15052 RECOVER_VFS_WRAPPER (
15053 pFd->pMethods->xSync(pFd, flags)
15054 );
15055 }
15056 static int recoverVfsFileSize(sqlite3_file *pFd, sqlite3_int64 *pSize){
15057 RECOVER_VFS_WRAPPER (
15058 pFd->pMethods->xFileSize(pFd, pSize)
15059 );
15060 }
15061 static int recoverVfsLock(sqlite3_file *pFd, int eLock){
15062 RECOVER_VFS_WRAPPER (
15063 pFd->pMethods->xLock(pFd, eLock)
15064 );
15065 }
15066 static int recoverVfsUnlock(sqlite3_file *pFd, int eLock){
15067 RECOVER_VFS_WRAPPER (
15068 pFd->pMethods->xUnlock(pFd, eLock)
15069 );
15070 }
15071 static int recoverVfsCheckReservedLock(sqlite3_file *pFd, int *pResOut){
15072 RECOVER_VFS_WRAPPER (
15073 pFd->pMethods->xCheckReservedLock(pFd, pResOut)
15074 );
15075 }
15076 static int recoverVfsFileControl(sqlite3_file *pFd, int op, void *pArg){
15077 RECOVER_VFS_WRAPPER (
15078 (pFd->pMethods ? pFd->pMethods->xFileControl(pFd, op, pArg) : SQLITE_NOTFOUND)
15079 );
15080 }
15081 static int recoverVfsSectorSize(sqlite3_file *pFd){
15082 RECOVER_VFS_WRAPPER (
15083 pFd->pMethods->xSectorSize(pFd)
15084 );
15085 }
15086 static int recoverVfsDeviceCharacteristics(sqlite3_file *pFd){
15087 RECOVER_VFS_WRAPPER (
15088 pFd->pMethods->xDeviceCharacteristics(pFd)
15089 );
15090 }
15091 static int recoverVfsShmMap(
15092 sqlite3_file *pFd, int iPg, int pgsz, int bExtend, void volatile **pp
15093 ){
15094 RECOVER_VFS_WRAPPER (
15095 pFd->pMethods->xShmMap(pFd, iPg, pgsz, bExtend, pp)
15096 );
15097 }
15098 static int recoverVfsShmLock(sqlite3_file *pFd, int offset, int n, int flags){
15099 RECOVER_VFS_WRAPPER (
15100 pFd->pMethods->xShmLock(pFd, offset, n, flags)
15101 );
15102 }
15103 static void recoverVfsShmBarrier(sqlite3_file *pFd){
15104 if( pFd->pMethods==&recover_methods ){
15105 pFd->pMethods = recover_g.pMethods;
15106 pFd->pMethods->xShmBarrier(pFd);
15107 pFd->pMethods = &recover_methods;
15108 }else{
15109 pFd->pMethods->xShmBarrier(pFd);
15110 }
15111 }
15112 static int recoverVfsShmUnmap(sqlite3_file *pFd, int deleteFlag){
15113 RECOVER_VFS_WRAPPER (
15114 pFd->pMethods->xShmUnmap(pFd, deleteFlag)
15115 );
15116 }
15117
15118 static int recoverVfsFetch(
15119 sqlite3_file *pFd,
15120 sqlite3_int64 iOff,
15121 int iAmt,
15122 void **pp
15123 ){
15124 *pp = 0;
15125 return SQLITE_OK;
15126 }
15127 static int recoverVfsUnfetch(sqlite3_file *pFd, sqlite3_int64 iOff, void *p){
15128 return SQLITE_OK;
15129 }
15130
15131 /*
15132 ** Install the VFS wrapper around the file-descriptor open on the input
15133 ** database for recover handle p. Mutex RECOVER_MUTEX_ID must be held
15134 ** when this function is called.
15135 */
15136 static void recoverInstallWrapper(sqlite3_recover *p){
15137 sqlite3_file *pFd = 0;
15138 assert( recover_g.pMethods==0 );
15139 recoverAssertMutexHeld();
15140 sqlite3_file_control(p->dbIn, p->zDb, SQLITE_FCNTL_FILE_POINTER, (void*)&pFd);
15141 assert( pFd==0 || pFd->pMethods!=&recover_methods );
15142 if( pFd && pFd->pMethods ){
15143 int iVersion = 1 + (pFd->pMethods->iVersion>1 && pFd->pMethods->xShmMap!=0);
15144 recover_g.pMethods = pFd->pMethods;
15145 recover_g.p = p;
15146 recover_methods.iVersion = iVersion;
15147 pFd->pMethods = &recover_methods;
15148 }
15149 }
15150
15151 /*
15152 ** Uninstall the VFS wrapper that was installed around the file-descriptor open
15153 ** on the input database for recover handle p. Mutex RECOVER_MUTEX_ID must be
15154 ** held when this function is called.
15155 */
15156 static void recoverUninstallWrapper(sqlite3_recover *p){
15157 sqlite3_file *pFd = 0;
15158 recoverAssertMutexHeld();
15159 sqlite3_file_control(p->dbIn, p->zDb,SQLITE_FCNTL_FILE_POINTER,(void*)&pFd);
15160 if( pFd && pFd->pMethods ){
15161 pFd->pMethods = recover_g.pMethods;
15162 recover_g.pMethods = 0;
15163 recover_g.p = 0;
15164 }
15165 }
15166
15167 /*
15168 ** This function does the work of a single sqlite3_recover_step() call. It
15169 ** is guaranteed that the handle is not in an error state when this
15170 ** function is called.
15171 */
15172 static void recoverStep(sqlite3_recover *p){
15173 assert( p && p->errCode==SQLITE_OK );
15174 switch( p->eState ){
15175 case RECOVER_STATE_INIT:
15176 /* This is the very first call to sqlite3_recover_step() on this object.
15177 */
15178 recoverSqlCallback(p, "BEGIN");
15179 recoverSqlCallback(p, "PRAGMA writable_schema = on");
15180
15181 recoverEnterMutex();
15182 recoverInstallWrapper(p);
15183
15184 /* Open the output database. And register required virtual tables and
15185 ** user functions with the new handle. */
15186 recoverOpenOutput(p);
15187
15188 /* Open transactions on both the input and output databases. */
15189 recoverExec(p, p->dbIn, "PRAGMA writable_schema = on");
15190 recoverExec(p, p->dbIn, "BEGIN");
15191 if( p->errCode==SQLITE_OK ) p->bCloseTransaction = 1;
15192 recoverExec(p, p->dbIn, "SELECT 1 FROM sqlite_schema");
15193 recoverTransferSettings(p);
15194 recoverOpenRecovery(p);
15195 recoverCacheSchema(p);
15196
15197 recoverUninstallWrapper(p);
15198 recoverLeaveMutex();
15199
15200 recoverExec(p, p->dbOut, "BEGIN");
15201
15202 recoverWriteSchema1(p);
15203 p->eState = RECOVER_STATE_WRITING;
15204 break;
15205
15206 case RECOVER_STATE_WRITING: {
15207 if( p->w1.pTbls==0 ){
15208 recoverWriteDataInit(p);
15209 }
15210 if( SQLITE_DONE==recoverWriteDataStep(p) ){
15211 recoverWriteDataCleanup(p);
15212 if( p->zLostAndFound ){
15213 p->eState = RECOVER_STATE_LOSTANDFOUND1;
15214 }else{
15215 p->eState = RECOVER_STATE_SCHEMA2;
15216 }
15217 }
15218 break;
15219 }
15220
15221 case RECOVER_STATE_LOSTANDFOUND1: {
15222 if( p->laf.pUsed==0 ){
15223 recoverLostAndFound1Init(p);
15224 }
15225 if( SQLITE_DONE==recoverLostAndFound1Step(p) ){
15226 p->eState = RECOVER_STATE_LOSTANDFOUND2;
15227 }
15228 break;
15229 }
15230 case RECOVER_STATE_LOSTANDFOUND2: {
15231 if( p->laf.pAllAndParent==0 ){
15232 recoverLostAndFound2Init(p);
15233 }
15234 if( SQLITE_DONE==recoverLostAndFound2Step(p) ){
15235 p->eState = RECOVER_STATE_LOSTANDFOUND3;
15236 }
15237 break;
15238 }
15239
15240 case RECOVER_STATE_LOSTANDFOUND3: {
15241 if( p->laf.pInsert==0 ){
15242 recoverLostAndFound3Init(p);
15243 }
15244 if( SQLITE_DONE==recoverLostAndFound3Step(p) ){
15245 p->eState = RECOVER_STATE_SCHEMA2;
15246 }
15247 break;
15248 }
15249
15250 case RECOVER_STATE_SCHEMA2: {
15251 int rc = SQLITE_OK;
15252
15253 recoverWriteSchema2(p);
15254 p->eState = RECOVER_STATE_DONE;
15255
15256 /* If no error has occurred, commit the write transaction on the output
15257 ** database. Regardless of whether or not an error has occurred, make
15258 ** an attempt to end the read transaction on the input database. */
15259 recoverExec(p, p->dbOut, "COMMIT");
15260 rc = sqlite3_exec(p->dbIn, "END", 0, 0, 0);
15261 if( p->errCode==SQLITE_OK ) p->errCode = rc;
15262
15263 recoverSqlCallback(p, "PRAGMA writable_schema = off");
15264 recoverSqlCallback(p, "COMMIT");
15265 p->eState = RECOVER_STATE_DONE;
15266 recoverFinalCleanup(p);
15267 break;
15268 };
15269
15270 case RECOVER_STATE_DONE: {
15271 /* no-op */
15272 break;
15273 };
15274 }
15275 }
15276
15277
15278 /*
15279 ** This is a worker function that does the heavy lifting for both init
15280 ** functions:
15281 **
15282 ** sqlite3_recover_init()
15283 ** sqlite3_recover_init_sql()
15284 **
15285 ** All this function does is allocate space for the recover handle and
15286 ** take copies of the input parameters. All the real work is done within
15287 ** sqlite3_recover_run().
15288 */
15289 sqlite3_recover *recoverInit(
15290 sqlite3* db,
15291 const char *zDb,
15292 const char *zUri, /* Output URI for _recover_init() */
15293 int (*xSql)(void*, const char*),/* SQL callback for _recover_init_sql() */
15294 void *pSqlCtx /* Context arg for _recover_init_sql() */
15295 ){
15296 sqlite3_recover *pRet = 0;
15297 int nDb = 0;
15298 int nUri = 0;
15299 int nByte = 0;
15300
15301 if( zDb==0 ){ zDb = "main"; }
15302
15303 nDb = recoverStrlen(zDb);
15304 nUri = recoverStrlen(zUri);
15305
15306 nByte = sizeof(sqlite3_recover) + nDb+1 + nUri+1;
15307 pRet = (sqlite3_recover*)sqlite3_malloc(nByte);
15308 if( pRet ){
15309 memset(pRet, 0, nByte);
15310 pRet->dbIn = db;
15311 pRet->zDb = (char*)&pRet[1];
15312 pRet->zUri = &pRet->zDb[nDb+1];
15313 memcpy(pRet->zDb, zDb, nDb);
15314 if( nUri>0 && zUri ) memcpy(pRet->zUri, zUri, nUri);
15315 pRet->xSql = xSql;
15316 pRet->pSqlCtx = pSqlCtx;
15317 pRet->bRecoverRowid = RECOVER_ROWID_DEFAULT;
15318 }
15319
15320 return pRet;
15321 }
15322
15323 /*
15324 ** Initialize a recovery handle that creates a new database containing
15325 ** the recovered data.
15326 */
15327 sqlite3_recover *sqlite3_recover_init(
15328 sqlite3* db,
15329 const char *zDb,
15330 const char *zUri
15331 ){
15332 return recoverInit(db, zDb, zUri, 0, 0);
15333 }
15334
15335 /*
15336 ** Initialize a recovery handle that returns recovered data in the
15337 ** form of SQL statements via a callback.
15338 */
15339 sqlite3_recover *sqlite3_recover_init_sql(
15340 sqlite3* db,
15341 const char *zDb,
15342 int (*xSql)(void*, const char*),
15343 void *pSqlCtx
15344 ){
15345 return recoverInit(db, zDb, 0, xSql, pSqlCtx);
15346 }
15347
15348 /*
15349 ** Return the handle error message, if any.
15350 */
15351 const char *sqlite3_recover_errmsg(sqlite3_recover *p){
15352 return (p && p->errCode!=SQLITE_NOMEM) ? p->zErrMsg : "out of memory";
15353 }
15354
15355 /*
15356 ** Return the handle error code.
15357 */
15358 int sqlite3_recover_errcode(sqlite3_recover *p){
15359 return p ? p->errCode : SQLITE_NOMEM;
15360 }
15361
15362 /*
15363 ** Configure the handle.
15364 */
15365 int sqlite3_recover_config(sqlite3_recover *p, int op, void *pArg){
15366 int rc = SQLITE_OK;
15367 if( p==0 ){
15368 rc = SQLITE_NOMEM;
15369 }else if( p->eState!=RECOVER_STATE_INIT ){
15370 rc = SQLITE_MISUSE;
15371 }else{
15372 switch( op ){
15373 case 789:
15374 /* This undocumented magic configuration option is used to set the
15375 ** name of the auxiliary database that is ATTACH-ed to the database
15376 ** connection and used to hold state information during the
15377 ** recovery process. This option is for debugging use only and
15378 ** is subject to change or removal at any time. */
15379 sqlite3_free(p->zStateDb);
15380 p->zStateDb = recoverMPrintf(p, "%s", (char*)pArg);
15381 break;
15382
15383 case SQLITE_RECOVER_LOST_AND_FOUND: {
15384 const char *zArg = (const char*)pArg;
15385 sqlite3_free(p->zLostAndFound);
15386 if( zArg ){
15387 p->zLostAndFound = recoverMPrintf(p, "%s", zArg);
15388 }else{
15389 p->zLostAndFound = 0;
15390 }
15391 break;
15392 }
15393
15394 case SQLITE_RECOVER_FREELIST_CORRUPT:
15395 p->bFreelistCorrupt = *(int*)pArg;
15396 break;
15397
15398 case SQLITE_RECOVER_ROWIDS:
15399 p->bRecoverRowid = *(int*)pArg;
15400 break;
15401
15402 case SQLITE_RECOVER_SLOWINDEXES:
15403 p->bSlowIndexes = *(int*)pArg;
15404 break;
15405
15406 default:
15407 rc = SQLITE_NOTFOUND;
15408 break;
15409 }
15410 }
15411
15412 return rc;
15413 }
15414
15415 /*
15416 ** Do a unit of work towards the recovery job. Return SQLITE_OK if
15417 ** no error has occurred but database recovery is not finished, SQLITE_DONE
15418 ** if database recovery has been successfully completed, or an SQLite
15419 ** error code if an error has occurred.
15420 */
15421 int sqlite3_recover_step(sqlite3_recover *p){
15422 if( p==0 ) return SQLITE_NOMEM;
15423 if( p->errCode==SQLITE_OK ) recoverStep(p);
15424 if( p->eState==RECOVER_STATE_DONE && p->errCode==SQLITE_OK ){
15425 return SQLITE_DONE;
15426 }
15427 return p->errCode;
15428 }
15429
15430 /*
15431 ** Do the configured recovery operation. Return SQLITE_OK if successful, or
15432 ** else an SQLite error code.
15433 */
15434 int sqlite3_recover_run(sqlite3_recover *p){
15435 while( SQLITE_OK==sqlite3_recover_step(p) );
15436 return sqlite3_recover_errcode(p);
15437 }
15438
15439
15440 /*
15441 ** Free all resources associated with the recover handle passed as the only
15442 ** argument. The results of using a handle with any sqlite3_recover_**
15443 ** API function after it has been passed to this function are undefined.
15444 **
15445 ** A copy of the value returned by the first call made to sqlite3_recover_run()
15446 ** on this handle is returned, or SQLITE_OK if sqlite3_recover_run() has
15447 ** not been called on this handle.
15448 */
15449 int sqlite3_recover_finish(sqlite3_recover *p){
15450 int rc;
15451 if( p==0 ){
15452 rc = SQLITE_NOMEM;
15453 }else{
15454 recoverFinalCleanup(p);
15455 if( p->bCloseTransaction && sqlite3_get_autocommit(p->dbIn)==0 ){
15456 rc = sqlite3_exec(p->dbIn, "END", 0, 0, 0);
15457 if( p->errCode==SQLITE_OK ) p->errCode = rc;
15458 }
15459 rc = p->errCode;
15460 sqlite3_free(p->zErrMsg);
15461 sqlite3_free(p->zStateDb);
15462 sqlite3_free(p->zLostAndFound);
15463 sqlite3_free(p->pPage1Cache);
15464 sqlite3_free(p);
15465 }
15466 return rc;
15467 }
15468
15469 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
15470
15471 /************************* End ../ext/recover/sqlite3recover.c ********************/
15472 #endif
15473
15474 #if defined(SQLITE_ENABLE_SESSION)
15475 /*
15476 ** State information for a single open session
@@ -13853,10 +17064,11 @@
17064 if( len>78 ){
17065 len = 78;
17066 while( (zSql[len]&0xc0)==0x80 ) len--;
17067 }
17068 zCode = sqlite3_mprintf("%.*s", len, zSql);
17069 shell_check_oom(zCode);
17070 for(i=0; zCode[i]; i++){ if( IsSpace(zSql[i]) ) zCode[i] = ' '; }
17071 if( iOffset<25 ){
17072 zMsg = sqlite3_mprintf("\n %z\n %*s^--- error here", zCode, iOffset, "");
17073 }else{
17074 zMsg = sqlite3_mprintf("\n %z\n %*serror here ---^", zCode, iOffset-14, "");
@@ -15552,11 +18764,11 @@
18764 ".clone NEWDB Clone data into NEWDB from the existing database",
18765 #endif
18766 ".connection [close] [#] Open or close an auxiliary database connection",
18767 ".databases List names and files of attached databases",
18768 ".dbconfig ?op? ?val? List or change sqlite3_db_config() options",
18769 #if SQLITE_SHELL_HAVE_RECOVER
18770 ".dbinfo ?DB? Show status information about the database",
18771 #endif
18772 ".dump ?OBJECTS? Render database content as SQL",
18773 " Options:",
18774 " --data-only Output only INSERT statements",
@@ -15700,14 +18912,13 @@
18912 #ifndef SQLITE_SHELL_FIDDLE
18913 ".quit Exit this program",
18914 ".read FILE Read input from FILE or command output",
18915 " If FILE begins with \"|\", it is a command that generates the input.",
18916 #endif
18917 #if SQLITE_SHELL_HAVE_RECOVER
18918 ".recover Recover as much data as possible from corrupt db.",
18919 " --ignore-freelist Ignore pages that appear to be on db freelist",
 
18920 " --lost-and-found TABLE Alternative name for the lost-and-found table",
18921 " --no-rowids Do not attempt to recover rowid values",
18922 " that are not also INTEGER PRIMARY KEYs",
18923 #endif
18924 #ifndef SQLITE_SHELL_FIDDLE
@@ -16315,11 +19526,11 @@
19526 sqlite3_series_init(p->db, 0, 0);
19527 #ifndef SQLITE_SHELL_FIDDLE
19528 sqlite3_fileio_init(p->db, 0, 0);
19529 sqlite3_completion_init(p->db, 0, 0);
19530 #endif
19531 #if SQLITE_SHELL_HAVE_RECOVER
19532 sqlite3_dbdata_init(p->db, 0, 0);
19533 #endif
19534 #ifdef SQLITE_HAVE_ZLIB
19535 if( !p->bSafeModePersist ){
19536 sqlite3_zipfile_init(p->db, 0, 0);
@@ -17200,12 +20411,11 @@
20411 sqlite3_free(zSchemaTab);
20412 sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
20413 utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion);
20414 return 0;
20415 }
20416 #endif /* SQLITE_SHELL_HAVE_RECOVER */
 
20417
20418 /*
20419 ** Print the current sqlite3_errmsg() value to stderr and return 1.
20420 */
20421 static int shellDatabaseError(sqlite3 *db){
@@ -18474,403 +21684,56 @@
21684 }
21685 /* End of the ".archive" or ".ar" command logic
21686 *******************************************************************************/
21687 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
21688
21689 #if SQLITE_SHELL_HAVE_RECOVER
21690
21691 /*
21692 ** This function is used as a callback by the recover extension. Simply
21693 ** print the supplied SQL statement to stdout.
21694 */
21695 static int recoverSqlCb(void *pCtx, const char *zSql){
21696 ShellState *pState = (ShellState*)pCtx;
21697 utf8_printf(pState->out, "%s;\n", zSql);
21698 return SQLITE_OK;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21699 }
21700
21701 /*
21702 ** This function is called to recover data from the database. A script
21703 ** to construct a new database containing all recovered data is output
21704 ** on stream pState->out.
21705 */
21706 static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){
21707 int rc = SQLITE_OK;
21708 const char *zRecoveryDb = ""; /* Name of "recovery" database. Debug only */
21709 const char *zLAF = "lost_and_found";
21710 int bFreelist = 1; /* 0 if --ignore-freelist is specified */
 
 
 
 
 
 
 
21711 int bRowids = 1; /* 0 if --no-rowids */
21712 sqlite3_recover *p = 0;
21713 int i = 0;
21714
21715 for(i=1; i<nArg; i++){
21716 char *z = azArg[i];
21717 int n;
21718 if( z[0]=='-' && z[1]=='-' ) z++;
21719 n = strlen30(z);
21720 if( n<=17 && memcmp("-ignore-freelist", z, n)==0 ){
21721 bFreelist = 0;
21722 }else
21723 if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){
21724 /* This option determines the name of the ATTACH-ed database used
21725 ** internally by the recovery extension. The default is "" which
21726 ** means to use a temporary database that is automatically deleted
21727 ** when closed. This option is undocumented and might disappear at
21728 ** any moment. */
21729 i++;
21730 zRecoveryDb = azArg[i];
21731 }else
21732 if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){
21733 i++;
21734 zLAF = azArg[i];
21735 }else
21736 if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
21737 bRowids = 0;
21738 }
21739 else{
@@ -18878,285 +21741,29 @@
21741 showHelp(pState->out, azArg[0]);
21742 return 1;
21743 }
21744 }
21745
21746 p = sqlite3_recover_init_sql(
21747 pState->db, "main", recoverSqlCb, (void*)pState
21748 );
21749
21750 sqlite3_recover_config(p, 789, (void*)zRecoveryDb); /* Debug use only */
21751 sqlite3_recover_config(p, SQLITE_RECOVER_LOST_AND_FOUND, (void*)zLAF);
21752 sqlite3_recover_config(p, SQLITE_RECOVER_ROWIDS, (void*)&bRowids);
21753 sqlite3_recover_config(p, SQLITE_RECOVER_FREELIST_CORRUPT,(void*)&bFreelist);
21754
21755 sqlite3_recover_run(p);
21756 if( sqlite3_recover_errcode(p)!=SQLITE_OK ){
21757 const char *zErr = sqlite3_recover_errmsg(p);
21758 int errCode = sqlite3_recover_errcode(p);
21759 raw_printf(stderr, "sql error: %s (%d)\n", zErr, errCode);
21760 }
21761 rc = sqlite3_recover_finish(p);
21762 return rc;
21763 }
21764 #endif /* SQLITE_SHELL_HAVE_RECOVER */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21765
21766
21767 /*
21768 * zAutoColumn(zCol, &db, ?) => Maybe init db, add column zCol to it.
21769 * zAutoColumn(0, &db, ?) => (db!=0) Form columns spec for CREATE TABLE,
@@ -19744,20 +22351,20 @@
22351 utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]);
22352 utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");
22353 }
22354 }else
22355
22356 #if SQLITE_SHELL_HAVE_RECOVER
22357 if( c=='d' && n>=3 && cli_strncmp(azArg[0], "dbinfo", n)==0 ){
22358 rc = shell_dbinfo_command(p, nArg, azArg);
22359 }else
22360
22361 if( c=='r' && cli_strncmp(azArg[0], "recover", n)==0 ){
22362 open_db(p, 0);
22363 rc = recoverDatabaseCmd(p, nArg, azArg);
22364 }else
22365 #endif /* SQLITE_SHELL_HAVE_RECOVER */
22366
22367 if( c=='d' && cli_strncmp(azArg[0], "dump", n)==0 ){
22368 char *zLike = 0;
22369 char *zSql;
22370 int i;
22371
+226 -84
--- extsrc/sqlite3.c
+++ extsrc/sqlite3.c
@@ -452,11 +452,11 @@
452452
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
453453
** [sqlite_version()] and [sqlite_source_id()].
454454
*/
455455
#define SQLITE_VERSION "3.40.0"
456456
#define SQLITE_VERSION_NUMBER 3040000
457
-#define SQLITE_SOURCE_ID "2022-10-26 11:11:31 3dfdfb3f12edb3f4267942598efd05d573e13b7c5d6cdbc3404373f41b8993dd"
457
+#define SQLITE_SOURCE_ID "2022-11-07 19:40:20 55a19677d723147aeb2b4a86bbd01756ddeb2072cba72c3145ad32d335e203b0"
458458
459459
/*
460460
** CAPI3REF: Run-Time Library Version Numbers
461461
** KEYWORDS: sqlite3_version sqlite3_sourceid
462462
**
@@ -1567,10 +1567,30 @@
15671567
** structure must be typedefed in order to work around compiler warnings
15681568
** on some platforms.
15691569
*/
15701570
typedef struct sqlite3_api_routines sqlite3_api_routines;
15711571
1572
+/*
1573
+** CAPI3REF: File Name
1574
+**
1575
+** Type [sqlite3_filename] is used by SQLite to pass filenames to the
1576
+** xOpen method of a [VFS]. It may be cast to (const char*) and treated
1577
+** as a normal, nul-terminated, UTF-8 buffer containing the filename, but
1578
+** may also be passed to special APIs such as:
1579
+**
1580
+** <ul>
1581
+** <li> sqlite3_filename_database()
1582
+** <li> sqlite3_filename_journal()
1583
+** <li> sqlite3_filename_wal()
1584
+** <li> sqlite3_uri_parameter()
1585
+** <li> sqlite3_uri_boolean()
1586
+** <li> sqlite3_uri_int64()
1587
+** <li> sqlite3_uri_key()
1588
+** </ul>
1589
+*/
1590
+typedef const char *sqlite3_filename;
1591
+
15721592
/*
15731593
** CAPI3REF: OS Interface Object
15741594
**
15751595
** An instance of the sqlite3_vfs object defines the interface between
15761596
** the SQLite core and the underlying operating system. The "vfs"
@@ -1745,11 +1765,11 @@
17451765
int szOsFile; /* Size of subclassed sqlite3_file */
17461766
int mxPathname; /* Maximum file pathname length */
17471767
sqlite3_vfs *pNext; /* Next registered VFS */
17481768
const char *zName; /* Name of this virtual file system */
17491769
void *pAppData; /* Pointer to application-specific data */
1750
- int (*xOpen)(sqlite3_vfs*, const char *zName, sqlite3_file*,
1770
+ int (*xOpen)(sqlite3_vfs*, sqlite3_filename zName, sqlite3_file*,
17511771
int flags, int *pOutFlags);
17521772
int (*xDelete)(sqlite3_vfs*, const char *zName, int syncDir);
17531773
int (*xAccess)(sqlite3_vfs*, const char *zName, int flags, int *pResOut);
17541774
int (*xFullPathname)(sqlite3_vfs*, const char *zName, int nOut, char *zOut);
17551775
void *(*xDlOpen)(sqlite3_vfs*, const char *zFilename);
@@ -4015,14 +4035,14 @@
40154035
** it has access to all the same query parameters as were found on the
40164036
** main database file.
40174037
**
40184038
** See the [URI filename] documentation for additional information.
40194039
*/
4020
-SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam);
4021
-SQLITE_API int sqlite3_uri_boolean(const char *zFile, const char *zParam, int bDefault);
4022
-SQLITE_API sqlite3_int64 sqlite3_uri_int64(const char*, const char*, sqlite3_int64);
4023
-SQLITE_API const char *sqlite3_uri_key(const char *zFilename, int N);
4040
+SQLITE_API const char *sqlite3_uri_parameter(sqlite3_filename z, const char *zParam);
4041
+SQLITE_API int sqlite3_uri_boolean(sqlite3_filename z, const char *zParam, int bDefault);
4042
+SQLITE_API sqlite3_int64 sqlite3_uri_int64(sqlite3_filename, const char*, sqlite3_int64);
4043
+SQLITE_API const char *sqlite3_uri_key(sqlite3_filename z, int N);
40244044
40254045
/*
40264046
** CAPI3REF: Translate filenames
40274047
**
40284048
** These routines are available to [VFS|custom VFS implementations] for
@@ -4047,13 +4067,13 @@
40474067
** In all of the above, if F is not the name of a database, journal or WAL
40484068
** filename passed into the VFS from the SQLite core and F is not the
40494069
** return value from [sqlite3_db_filename()], then the result is
40504070
** undefined and is likely a memory access violation.
40514071
*/
4052
-SQLITE_API const char *sqlite3_filename_database(const char*);
4053
-SQLITE_API const char *sqlite3_filename_journal(const char*);
4054
-SQLITE_API const char *sqlite3_filename_wal(const char*);
4072
+SQLITE_API const char *sqlite3_filename_database(sqlite3_filename);
4073
+SQLITE_API const char *sqlite3_filename_journal(sqlite3_filename);
4074
+SQLITE_API const char *sqlite3_filename_wal(sqlite3_filename);
40554075
40564076
/*
40574077
** CAPI3REF: Database File Corresponding To A Journal
40584078
**
40594079
** ^If X is the name of a rollback or WAL-mode journal file that is
@@ -4115,18 +4135,18 @@
41154135
** used again after sqlite3_free_filename(Y) has been called. This means
41164136
** that if the [sqlite3_vfs.xOpen()] method of a VFS has been called using Y,
41174137
** then the corresponding [sqlite3_module.xClose() method should also be
41184138
** invoked prior to calling sqlite3_free_filename(Y).
41194139
*/
4120
-SQLITE_API char *sqlite3_create_filename(
4140
+SQLITE_API sqlite3_filename sqlite3_create_filename(
41214141
const char *zDatabase,
41224142
const char *zJournal,
41234143
const char *zWal,
41244144
int nParam,
41254145
const char **azParam
41264146
);
4127
-SQLITE_API void sqlite3_free_filename(char*);
4147
+SQLITE_API void sqlite3_free_filename(sqlite3_filename);
41284148
41294149
/*
41304150
** CAPI3REF: Error Codes And Messages
41314151
** METHOD: sqlite3
41324152
**
@@ -6158,13 +6178,14 @@
61586178
** application-defined function to be a text string in an encoding
61596179
** specified by the fifth (and last) parameter, which must be one
61606180
** of [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE].
61616181
** ^SQLite takes the text result from the application from
61626182
** the 2nd parameter of the sqlite3_result_text* interfaces.
6163
-** ^If the 3rd parameter to the sqlite3_result_text* interfaces
6164
-** is negative, then SQLite takes result text from the 2nd parameter
6165
-** through the first zero character.
6183
+** ^If the 3rd parameter to any of the sqlite3_result_text* interfaces
6184
+** other than sqlite3_result_text64() is negative, then SQLite computes
6185
+** the string length itself by searching the 2nd parameter for the first
6186
+** zero character.
61666187
** ^If the 3rd parameter to the sqlite3_result_text* interfaces
61676188
** is non-negative, then as many bytes (not characters) of the text
61686189
** pointed to by the 2nd parameter are taken as the application-defined
61696190
** function result. If the 3rd parameter is non-negative, then it
61706191
** must be the byte offset into the string where the NUL terminator would
@@ -6656,11 +6677,11 @@
66566677
** <li> [sqlite3_filename_database()]
66576678
** <li> [sqlite3_filename_journal()]
66586679
** <li> [sqlite3_filename_wal()]
66596680
** </ul>
66606681
*/
6661
-SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName);
6682
+SQLITE_API sqlite3_filename sqlite3_db_filename(sqlite3 *db, const char *zDbName);
66626683
66636684
/*
66646685
** CAPI3REF: Determine if a database is read-only
66656686
** METHOD: sqlite3
66666687
**
@@ -18879,11 +18900,11 @@
1887918900
u8 eDest; /* How to dispose of the results. One of SRT_* above. */
1888018901
int iSDParm; /* A parameter used by the eDest disposal method */
1888118902
int iSDParm2; /* A second parameter for the eDest disposal method */
1888218903
int iSdst; /* Base register where results are written */
1888318904
int nSdst; /* Number of registers allocated */
18884
- char *zAffSdst; /* Affinity used when eDest==SRT_Set */
18905
+ char *zAffSdst; /* Affinity used for SRT_Set, SRT_Table, and similar */
1888518906
ExprList *pOrderBy; /* Key columns for SRT_Queue and SRT_DistQueue */
1888618907
};
1888718908
1888818909
/*
1888918910
** During code generation of statements that do inserts into AUTOINCREMENT
@@ -20349,10 +20370,11 @@
2034920370
#define getVarint sqlite3GetVarint
2035020371
#define putVarint sqlite3PutVarint
2035120372
2035220373
2035320374
SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(sqlite3*, Index*);
20375
+SQLITE_PRIVATE char *sqlite3TableAffinityStr(sqlite3*,const Table*);
2035420376
SQLITE_PRIVATE void sqlite3TableAffinity(Vdbe*, Table*, int);
2035520377
SQLITE_PRIVATE char sqlite3CompareAffinity(const Expr *pExpr, char aff2);
2035620378
SQLITE_PRIVATE int sqlite3IndexAffinityOk(const Expr *pExpr, char idx_affinity);
2035720379
SQLITE_PRIVATE char sqlite3TableColumnAffinity(const Table*,int);
2035820380
SQLITE_PRIVATE char sqlite3ExprAffinity(const Expr *pExpr);
@@ -36281,10 +36303,12 @@
3628136303
char zKey[30];
3628236304
char aData[131073];
3628336305
SQLITE_KV_LOG(("xWrite('%s-db',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
3628436306
assert( iAmt>=512 && iAmt<=65536 );
3628536307
assert( (iAmt & (iAmt-1))==0 );
36308
+ assert( pFile->szPage<0 || pFile->szPage==iAmt );
36309
+ pFile->szPage = iAmt;
3628636310
pgno = 1 + iOfst/iAmt;
3628736311
sqlite3_snprintf(sizeof(zKey), zKey, "%u", pgno);
3628836312
kvvfsEncode(zBuf, iAmt, aData);
3628936313
if( sqlite3KvvfsMethods.xWrite(pFile->zClass, zKey, aData) ){
3629036314
return SQLITE_IOERR;
@@ -36463,10 +36487,11 @@
3646336487
sqlite3_file *pProtoFile,
3646436488
int flags,
3646536489
int *pOutFlags
3646636490
){
3646736491
KVVfsFile *pFile = (KVVfsFile*)pProtoFile;
36492
+ if( zName==0 ) zName = "";
3646836493
SQLITE_KV_LOG(("xOpen(\"%s\")\n", zName));
3646936494
if( strcmp(zName, "local")==0
3647036495
|| strcmp(zName, "session")==0
3647136496
){
3647236497
pFile->isJournal = 0;
@@ -49461,13 +49486,14 @@
4946149486
}
4946249487
return 0;
4946349488
}
4946449489
4946549490
/*
49466
-** If sqlite3_temp_directory is not, take the mutex and return true.
49491
+** If sqlite3_temp_directory is defined, take the mutex and return true.
4946749492
**
49468
-** If sqlite3_temp_directory is NULL, omit the mutex and return false.
49493
+** If sqlite3_temp_directory is NULL (undefined), omit the mutex and
49494
+** return false.
4946949495
*/
4947049496
static int winTempDirDefined(void){
4947149497
sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
4947249498
if( sqlite3_temp_directory!=0 ) return 1;
4947349499
sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
@@ -69701,11 +69727,10 @@
6970169727
6970269728
/* Remove the slot from the free-list. Update the number of
6970369729
** fragmented bytes within the page. */
6970469730
memcpy(&aData[iAddr], &aData[pc], 2);
6970569731
aData[hdr+7] += (u8)x;
69706
- testcase( pc+x>maxPC );
6970769732
return &aData[pc];
6970869733
}else if( x+pc > maxPC ){
6970969734
/* This slot extends off the end of the usable part of the page */
6971069735
*pRc = SQLITE_CORRUPT_PAGE(pPg);
6971169736
return 0;
@@ -74302,12 +74327,12 @@
7430274327
7430374328
assert( sqlite3_mutex_held(pBt->mutex) );
7430474329
assert( eMode==BTALLOC_ANY || (nearby>0 && IfNotOmitAV(pBt->autoVacuum)) );
7430574330
pPage1 = pBt->pPage1;
7430674331
mxPage = btreePagecount(pBt);
74307
- /* EVIDENCE-OF: R-05119-02637 The 4-byte big-endian integer at offset 36
74308
- ** stores stores the total number of pages on the freelist. */
74332
+ /* EVIDENCE-OF: R-21003-45125 The 4-byte big-endian integer at offset 36
74333
+ ** stores the total number of pages on the freelist. */
7430974334
n = get4byte(&pPage1->aData[36]);
7431074335
testcase( n==mxPage-1 );
7431174336
if( n>=mxPage ){
7431274337
return SQLITE_CORRUPT_BKPT;
7431374338
}
@@ -86476,11 +86501,11 @@
8647686501
assert( pPKey2->pKeyInfo->nAllField>=pPKey2->nField
8647786502
|| CORRUPT_DB );
8647886503
assert( pPKey2->pKeyInfo->aSortFlags!=0 );
8647986504
assert( pPKey2->pKeyInfo->nKeyField>0 );
8648086505
assert( idx1<=szHdr1 || CORRUPT_DB );
86481
- do{
86506
+ while( 1 /*exit-by-break*/ ){
8648286507
u32 serial_type;
8648386508
8648486509
/* RHS is an integer */
8648586510
if( pRhs->flags & (MEM_Int|MEM_IntReal) ){
8648686511
testcase( pRhs->flags & MEM_Int );
@@ -86614,12 +86639,17 @@
8661486639
8661586640
i++;
8661686641
if( i==pPKey2->nField ) break;
8661786642
pRhs++;
8661886643
d1 += sqlite3VdbeSerialTypeLen(serial_type);
86644
+ if( d1>(unsigned)nKey1 ) break;
8661986645
idx1 += sqlite3VarintLen(serial_type);
86620
- }while( idx1<(unsigned)szHdr1 && d1<=(unsigned)nKey1 );
86646
+ if( idx1>=(unsigned)szHdr1 ){
86647
+ pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
86648
+ return 0; /* Corrupt index */
86649
+ }
86650
+ }
8662186651
8662286652
/* No memory allocation is ever used on mem1. Prove this using
8662386653
** the following assert(). If the assert() fails, it indicates a
8662486654
** memory leak and a need to call sqlite3VdbeMemRelease(&mem1). */
8662586655
assert( mem1.szMalloc==0 );
@@ -127850,10 +127880,32 @@
127850127880
pIdx->zColAff[n] = 0;
127851127881
}
127852127882
127853127883
return pIdx->zColAff;
127854127884
}
127885
+
127886
+/*
127887
+** Compute an affinity string for a table. Space is obtained
127888
+** from sqlite3DbMalloc(). The caller is responsible for freeing
127889
+** the space when done.
127890
+*/
127891
+SQLITE_PRIVATE char *sqlite3TableAffinityStr(sqlite3 *db, const Table *pTab){
127892
+ char *zColAff;
127893
+ zColAff = (char *)sqlite3DbMallocRaw(db, pTab->nCol+1);
127894
+ if( zColAff ){
127895
+ int i, j;
127896
+ for(i=j=0; i<pTab->nCol; i++){
127897
+ if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ){
127898
+ zColAff[j++] = pTab->aCol[i].affinity;
127899
+ }
127900
+ }
127901
+ do{
127902
+ zColAff[j--] = 0;
127903
+ }while( j>=0 && zColAff[j]<=SQLITE_AFF_BLOB );
127904
+ }
127905
+ return zColAff;
127906
+}
127855127907
127856127908
/*
127857127909
** Make changes to the evolving bytecode to do affinity transformations
127858127910
** of values that are about to be gathered into a row for table pTab.
127859127911
**
@@ -127892,11 +127944,11 @@
127892127944
** register set as the OP_MakeRecord. If iReg>0 then register iReg is
127893127945
** the first of a series of registers that will form the new record.
127894127946
** Apply the type checking to that array of registers.
127895127947
*/
127896127948
SQLITE_PRIVATE void sqlite3TableAffinity(Vdbe *v, Table *pTab, int iReg){
127897
- int i, j;
127949
+ int i;
127898127950
char *zColAff;
127899127951
if( pTab->tabFlags & TF_Strict ){
127900127952
if( iReg==0 ){
127901127953
/* Move the previous opcode (which should be OP_MakeRecord) forward
127902127954
** by one slot and insert a new OP_TypeCheck where the current
@@ -127915,26 +127967,15 @@
127915127967
}
127916127968
return;
127917127969
}
127918127970
zColAff = pTab->zColAff;
127919127971
if( zColAff==0 ){
127920
- sqlite3 *db = sqlite3VdbeDb(v);
127921
- zColAff = (char *)sqlite3DbMallocRaw(0, pTab->nCol+1);
127972
+ zColAff = sqlite3TableAffinityStr(0, pTab);
127922127973
if( !zColAff ){
127923
- sqlite3OomFault(db);
127974
+ sqlite3OomFault(sqlite3VdbeDb(v));
127924127975
return;
127925127976
}
127926
-
127927
- for(i=j=0; i<pTab->nCol; i++){
127928
- assert( pTab->aCol[i].affinity!=0 || sqlite3VdbeParser(v)->nErr>0 );
127929
- if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ){
127930
- zColAff[j++] = pTab->aCol[i].affinity;
127931
- }
127932
- }
127933
- do{
127934
- zColAff[j--] = 0;
127935
- }while( j>=0 && zColAff[j]<=SQLITE_AFF_BLOB );
127936127977
pTab->zColAff = zColAff;
127937127978
}
127938127979
assert( zColAff!=0 );
127939127980
i = sqlite3Strlen30NN(zColAff);
127940127981
if( i ){
@@ -131386,13 +131427,13 @@
131386131427
const char *(*uri_key)(const char*,int);
131387131428
const char *(*filename_database)(const char*);
131388131429
const char *(*filename_journal)(const char*);
131389131430
const char *(*filename_wal)(const char*);
131390131431
/* Version 3.32.0 and later */
131391
- char *(*create_filename)(const char*,const char*,const char*,
131432
+ const char *(*create_filename)(const char*,const char*,const char*,
131392131433
int,const char**);
131393
- void (*free_filename)(char*);
131434
+ void (*free_filename)(const char*);
131394131435
sqlite3_file *(*database_file_object)(const char*);
131395131436
/* Version 3.34.0 and later */
131396131437
int (*txn_state)(sqlite3*,const char*);
131397131438
/* Version 3.36.1 and later */
131398131439
sqlite3_int64 (*changes64)(sqlite3*);
@@ -138504,10 +138545,13 @@
138504138545
testcase( eDest==SRT_Table );
138505138546
testcase( eDest==SRT_EphemTab );
138506138547
testcase( eDest==SRT_Fifo );
138507138548
testcase( eDest==SRT_DistFifo );
138508138549
sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1+nPrefixReg);
138550
+ if( pDest->zAffSdst ){
138551
+ sqlite3VdbeChangeP4(v, -1, pDest->zAffSdst, nResultCol);
138552
+ }
138509138553
#ifndef SQLITE_OMIT_CTE
138510138554
if( eDest==SRT_DistFifo ){
138511138555
/* If the destination is DistFifo, then cursor (iParm+1) is open
138512138556
** on an ephemeral index. If the current row is already present
138513138557
** in the index, do not write it to the output. If not, add the
@@ -140907,12 +140951,12 @@
140907140951
140908140952
/* Jump to the this point in order to terminate the query.
140909140953
*/
140910140954
sqlite3VdbeResolveLabel(v, labelEnd);
140911140955
140912
- /* Reassemble the compound query so that it will be freed correctly
140913
- ** by the calling function */
140956
+ /* Make arrangements to free the 2nd and subsequent arms of the compound
140957
+ ** after the parse has finished */
140914140958
if( pSplit->pPrior ){
140915140959
sqlite3ParserAddCleanup(pParse,
140916140960
(void(*)(sqlite3*,void*))sqlite3SelectDelete, pSplit->pPrior);
140917140961
}
140918140962
pSplit->pPrior = pPrior;
@@ -141366,10 +141410,12 @@
141366141410
** (17e) the subquery may not contain window functions, and
141367141411
** (17f) the subquery must not be the RHS of a LEFT JOIN.
141368141412
** (17g) either the subquery is the first element of the outer
141369141413
** query or there are no RIGHT or FULL JOINs in any arm
141370141414
** of the subquery. (This is a duplicate of condition (27b).)
141415
+** (17h) The corresponding result set expressions in all arms of the
141416
+** compound must have the same affinity.
141371141417
**
141372141418
** The parent and sub-query may contain WHERE clauses. Subject to
141373141419
** rules (11), (13) and (14), they may also contain ORDER BY,
141374141420
** LIMIT and OFFSET clauses. The subquery cannot use any compound
141375141421
** operator other than UNION ALL because all the other compound
@@ -141542,10 +141588,11 @@
141542141588
** use only the UNION ALL operator. And none of the simple select queries
141543141589
** that make up the compound SELECT are allowed to be aggregate or distinct
141544141590
** queries.
141545141591
*/
141546141592
if( pSub->pPrior ){
141593
+ int ii;
141547141594
if( pSub->pOrderBy ){
141548141595
return 0; /* Restriction (20) */
141549141596
}
141550141597
if( isAgg || (p->selFlags & SF_Distinct)!=0 || isOuterJoin>0 ){
141551141598
return 0; /* (17d1), (17d2), or (17f) */
@@ -141574,18 +141621,32 @@
141574141621
testcase( pSub1->pSrc->nSrc>1 );
141575141622
}
141576141623
141577141624
/* Restriction (18). */
141578141625
if( p->pOrderBy ){
141579
- int ii;
141580141626
for(ii=0; ii<p->pOrderBy->nExpr; ii++){
141581141627
if( p->pOrderBy->a[ii].u.x.iOrderByCol==0 ) return 0;
141582141628
}
141583141629
}
141584141630
141585141631
/* Restriction (23) */
141586141632
if( (p->selFlags & SF_Recursive) ) return 0;
141633
+
141634
+ /* Restriction (17h) */
141635
+ for(ii=0; ii<pSub->pEList->nExpr; ii++){
141636
+ char aff;
141637
+ assert( pSub->pEList->a[ii].pExpr!=0 );
141638
+ aff = sqlite3ExprAffinity(pSub->pEList->a[ii].pExpr);
141639
+ for(pSub1=pSub->pPrior; pSub1; pSub1=pSub1->pPrior){
141640
+ assert( pSub1->pEList!=0 );
141641
+ assert( pSub1->pEList->nExpr>ii );
141642
+ assert( pSub1->pEList->a[ii].pExpr!=0 );
141643
+ if( sqlite3ExprAffinity(pSub1->pEList->a[ii].pExpr)!=aff ){
141644
+ return 0;
141645
+ }
141646
+ }
141647
+ }
141587141648
141588141649
if( pSrc->nSrc>1 ){
141589141650
if( pParse->nSelect>500 ) return 0;
141590141651
if( OptimizationDisabled(db, SQLITE_FlttnUnionAll) ) return 0;
141591141652
aCsrMap = sqlite3DbMallocZero(db, ((i64)pParse->nTab+1)*sizeof(int));
@@ -142225,10 +142286,17 @@
142225142286
** window over which any window-function is calculated.
142226142287
**
142227142288
** (7) The inner query is a Common Table Expression (CTE) that should
142228142289
** be materialized. (This restriction is implemented in the calling
142229142290
** routine.)
142291
+**
142292
+** (8) The subquery may not be a compound that uses UNION, INTERSECT,
142293
+** or EXCEPT. (We could, perhaps, relax this restriction to allow
142294
+** this case if none of the comparisons operators between left and
142295
+** right arms of the compound use a collation other than BINARY.
142296
+** But it is a lot of work to check that case for an obscure and
142297
+** minor optimization, so we omit it for now.)
142230142298
**
142231142299
** Return 0 if no changes are made and non-zero if one or more WHERE clause
142232142300
** terms are duplicated into the subquery.
142233142301
*/
142234142302
static int pushDownWhereTerms(
@@ -142245,10 +142313,14 @@
142245142313
142246142314
#ifndef SQLITE_OMIT_WINDOWFUNC
142247142315
if( pSubq->pPrior ){
142248142316
Select *pSel;
142249142317
for(pSel=pSubq; pSel; pSel=pSel->pPrior){
142318
+ u8 op = pSel->op;
142319
+ assert( op==TK_ALL || op==TK_SELECT
142320
+ || op==TK_UNION || op==TK_INTERSECT || op==TK_EXCEPT );
142321
+ if( op!=TK_ALL && op!=TK_SELECT ) return 0; /* restriction (8) */
142250142322
if( pSel->pWin ) return 0; /* restriction (6b) */
142251142323
}
142252142324
}else{
142253142325
if( pSubq->pWin && pSubq->pWin->pPartition==0 ) return 0;
142254142326
}
@@ -144277,11 +144349,14 @@
144277144349
}else{
144278144350
VdbeNoopComment((v, "materialize %!S", pItem));
144279144351
}
144280144352
sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
144281144353
ExplainQueryPlan((pParse, 1, "MATERIALIZE %!S", pItem));
144354
+ dest.zAffSdst = sqlite3TableAffinityStr(db, pItem->pTab);
144282144355
sqlite3Select(pParse, pSub, &dest);
144356
+ sqlite3DbFree(db, dest.zAffSdst);
144357
+ dest.zAffSdst = 0;
144283144358
pItem->pTab->nRowLogEst = pSub->nSelectRow;
144284144359
if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr);
144285144360
sqlite3VdbeAddOp2(v, OP_Return, pItem->regReturn, topAddr+1);
144286144361
VdbeComment((v, "end %!S", pItem));
144287144362
sqlite3VdbeJumpHere(v, topAddr);
@@ -175725,10 +175800,16 @@
175725175800
if( rc==SQLITE_NOMEM ) sqlite3OomFault(db);
175726175801
sqlite3ErrorWithMsg(db, rc, zErrMsg ? "%s" : 0, zErrMsg);
175727175802
sqlite3_free(zErrMsg);
175728175803
goto opendb_out;
175729175804
}
175805
+ assert( db->pVfs!=0 );
175806
+#if SQLITE_OS_KV || defined(SQLITE_OS_KV_OPTIONAL)
175807
+ if( sqlite3_stricmp(db->pVfs->zName, "kvvfs")==0 ){
175808
+ db->temp_store = 2;
175809
+ }
175810
+#endif
175730175811
175731175812
/* Open the backend database driver */
175732175813
rc = sqlite3BtreeOpen(db->pVfs, zOpen, db, &db->aDb[0].pBt, 0,
175733175814
flags | SQLITE_OPEN_MAIN_DB);
175734175815
if( rc!=SQLITE_OK ){
@@ -176834,11 +176915,11 @@
176834176915
** functions.
176835176916
**
176836176917
** Memory layout must be compatible with that generated by the pager
176837176918
** and expected by sqlite3_uri_parameter() and databaseName().
176838176919
*/
176839
-SQLITE_API char *sqlite3_create_filename(
176920
+SQLITE_API const char *sqlite3_create_filename(
176840176921
const char *zDatabase,
176841176922
const char *zJournal,
176842176923
const char *zWal,
176843176924
int nParam,
176844176925
const char **azParam
@@ -176870,14 +176951,14 @@
176870176951
/*
176871176952
** Free memory obtained from sqlite3_create_filename(). It is a severe
176872176953
** error to call this routine with any parameter other than a pointer
176873176954
** previously obtained from sqlite3_create_filename() or a NULL pointer.
176874176955
*/
176875
-SQLITE_API void sqlite3_free_filename(char *p){
176956
+SQLITE_API void sqlite3_free_filename(const char *p){
176876176957
if( p==0 ) return;
176877
- p = (char*)databaseName(p);
176878
- sqlite3_free(p - 4);
176958
+ p = databaseName(p);
176959
+ sqlite3_free((char*)p - 4);
176879176960
}
176880176961
176881176962
176882176963
/*
176883176964
** This is a utility routine, useful to VFS implementations, that checks
@@ -207653,10 +207734,38 @@
207653207734
#define SQLITE_RBU_STATE_CHECKPOINT 3
207654207735
#define SQLITE_RBU_STATE_DONE 4
207655207736
#define SQLITE_RBU_STATE_ERROR 5
207656207737
207657207738
SQLITE_API int sqlite3rbu_state(sqlite3rbu *pRbu);
207739
+
207740
+/*
207741
+** As part of applying an RBU update or performing an RBU vacuum operation,
207742
+** the system must at one point move the *-oal file to the equivalent *-wal
207743
+** path. Normally, it does this by invoking POSIX function rename(2) directly.
207744
+** Except on WINCE platforms, where it uses win32 API MoveFileW(). This
207745
+** function may be used to register a callback that the RBU module will invoke
207746
+** instead of one of these APIs.
207747
+**
207748
+** If a callback is registered with an RBU handle, it invokes it instead
207749
+** of rename(2) when it needs to move a file within the file-system. The
207750
+** first argument passed to the xRename() callback is a copy of the second
207751
+** argument (pArg) passed to this function. The second is the full path
207752
+** to the file to move and the third the full path to which it should be
207753
+** moved. The callback function should return SQLITE_OK to indicate
207754
+** success. If an error occurs, it should return an SQLite error code.
207755
+** In this case the RBU operation will be abandoned and the error returned
207756
+** to the RBU user.
207757
+**
207758
+** Passing a NULL pointer in place of the xRename argument to this function
207759
+** restores the default behaviour.
207760
+*/
207761
+SQLITE_API void sqlite3rbu_rename_handler(
207762
+ sqlite3rbu *pRbu,
207763
+ void *pArg,
207764
+ int (*xRename)(void *pArg, const char *zOld, const char *zNew)
207765
+);
207766
+
207658207767
207659207768
/*
207660207769
** Create an RBU VFS named zName that accesses the underlying file-system
207661207770
** via existing VFS zParent. Or, if the zParent parameter is passed NULL,
207662207771
** then the new RBU VFS uses the default system VFS to access the file-system.
@@ -208021,10 +208130,12 @@
208021208130
const char *zVfsName; /* Name of automatically created rbu vfs */
208022208131
rbu_file *pTargetFd; /* File handle open on target db */
208023208132
int nPagePerSector; /* Pages per sector for pTargetFd */
208024208133
i64 iOalSz;
208025208134
i64 nPhaseOneStep;
208135
+ void *pRenameArg;
208136
+ int (*xRename)(void*, const char*, const char*);
208026208137
208027208138
/* The following state variables are used as part of the incremental
208028208139
** checkpoint stage (eStage==RBU_STAGE_CKPT). See comments surrounding
208029208140
** function rbuSetupCheckpoint() for details. */
208030208141
u32 iMaxFrame; /* Largest iWalFrame value in aFrame[] */
@@ -210869,36 +210980,11 @@
210869210980
assert( p->rc==SQLITE_OK );
210870210981
p->rc = rbuLockDatabase(dbMain);
210871210982
}
210872210983
210873210984
if( p->rc==SQLITE_OK ){
210874
-#if defined(_WIN32_WCE)
210875
- {
210876
- LPWSTR zWideOal;
210877
- LPWSTR zWideWal;
210878
-
210879
- zWideOal = rbuWinUtf8ToUnicode(zOal);
210880
- if( zWideOal ){
210881
- zWideWal = rbuWinUtf8ToUnicode(zWal);
210882
- if( zWideWal ){
210883
- if( MoveFileW(zWideOal, zWideWal) ){
210884
- p->rc = SQLITE_OK;
210885
- }else{
210886
- p->rc = SQLITE_IOERR;
210887
- }
210888
- sqlite3_free(zWideWal);
210889
- }else{
210890
- p->rc = SQLITE_IOERR_NOMEM;
210891
- }
210892
- sqlite3_free(zWideOal);
210893
- }else{
210894
- p->rc = SQLITE_IOERR_NOMEM;
210895
- }
210896
- }
210897
-#else
210898
- p->rc = rename(zOal, zWal) ? SQLITE_IOERR : SQLITE_OK;
210899
-#endif
210985
+ p->rc = p->xRename(p->pRenameArg, zOal, zWal);
210900210986
}
210901210987
210902210988
if( p->rc!=SQLITE_OK
210903210989
|| rbuIsVacuum(p)
210904210990
|| rbuExclusiveCheckpoint(dbMain)==0
@@ -211633,10 +211719,11 @@
211633211719
if( p ){
211634211720
RbuState *pState = 0;
211635211721
211636211722
/* Create the custom VFS. */
211637211723
memset(p, 0, sizeof(sqlite3rbu));
211724
+ sqlite3rbu_rename_handler(p, 0, 0);
211638211725
rbuCreateVfs(p);
211639211726
211640211727
/* Open the target, RBU and state databases */
211641211728
if( p->rc==SQLITE_OK ){
211642211729
char *pCsr = (char*)&p[1];
@@ -212023,10 +212110,58 @@
212023212110
}
212024212111
212025212112
p->rc = rc;
212026212113
return rc;
212027212114
}
212115
+
212116
+/*
212117
+** Default xRename callback for RBU.
212118
+*/
212119
+static int xDefaultRename(void *pArg, const char *zOld, const char *zNew){
212120
+ int rc = SQLITE_OK;
212121
+#if defined(_WIN32_WCE)
212122
+ {
212123
+ LPWSTR zWideOld;
212124
+ LPWSTR zWideNew;
212125
+
212126
+ zWideOld = rbuWinUtf8ToUnicode(zOld);
212127
+ if( zWideOld ){
212128
+ zWideNew = rbuWinUtf8ToUnicode(zNew);
212129
+ if( zWideNew ){
212130
+ if( MoveFileW(zWideOld, zWideNew) ){
212131
+ rc = SQLITE_OK;
212132
+ }else{
212133
+ rc = SQLITE_IOERR;
212134
+ }
212135
+ sqlite3_free(zWideNew);
212136
+ }else{
212137
+ rc = SQLITE_IOERR_NOMEM;
212138
+ }
212139
+ sqlite3_free(zWideOld);
212140
+ }else{
212141
+ rc = SQLITE_IOERR_NOMEM;
212142
+ }
212143
+ }
212144
+#else
212145
+ rc = rename(zOld, zNew) ? SQLITE_IOERR : SQLITE_OK;
212146
+#endif
212147
+ return rc;
212148
+}
212149
+
212150
+SQLITE_API void sqlite3rbu_rename_handler(
212151
+ sqlite3rbu *pRbu,
212152
+ void *pArg,
212153
+ int (*xRename)(void *pArg, const char *zOld, const char *zNew)
212154
+){
212155
+ if( xRename ){
212156
+ pRbu->xRename = xRename;
212157
+ pRbu->pRenameArg = pArg;
212158
+ }else{
212159
+ pRbu->xRename = xDefaultRename;
212160
+ pRbu->pRenameArg = 0;
212161
+ }
212162
+}
212028212163
212029212164
/**************************************************************************
212030212165
** Beginning of RBU VFS shim methods. The VFS shim modifies the behaviour
212031212166
** of a standard VFS in the following ways:
212032212167
**
@@ -214154,25 +214289,31 @@
214154214289
sqlite3_result_int(ctx, pCsr->pgno);
214155214290
break;
214156214291
}
214157214292
case 1: { /* data */
214158214293
DbPage *pDbPage = 0;
214159
- rc = sqlite3PagerGet(pCsr->pPager, pCsr->pgno, (DbPage**)&pDbPage, 0);
214160
- if( rc==SQLITE_OK ){
214161
- sqlite3_result_blob(ctx, sqlite3PagerGetData(pDbPage), pCsr->szPage,
214162
- SQLITE_TRANSIENT);
214294
+ if( pCsr->pgno==((PENDING_BYTE/pCsr->szPage)+1) ){
214295
+ /* The pending byte page. Assume it is zeroed out. Attempting to
214296
+ ** request this page from the page is an SQLITE_CORRUPT error. */
214297
+ sqlite3_result_zeroblob(ctx, pCsr->szPage);
214298
+ }else{
214299
+ rc = sqlite3PagerGet(pCsr->pPager, pCsr->pgno, (DbPage**)&pDbPage, 0);
214300
+ if( rc==SQLITE_OK ){
214301
+ sqlite3_result_blob(ctx, sqlite3PagerGetData(pDbPage), pCsr->szPage,
214302
+ SQLITE_TRANSIENT);
214303
+ }
214304
+ sqlite3PagerUnref(pDbPage);
214163214305
}
214164
- sqlite3PagerUnref(pDbPage);
214165214306
break;
214166214307
}
214167214308
default: { /* schema */
214168214309
sqlite3 *db = sqlite3_context_db_handle(ctx);
214169214310
sqlite3_result_text(ctx, db->aDb[pCsr->iDb].zDbSName, -1, SQLITE_STATIC);
214170214311
break;
214171214312
}
214172214313
}
214173
- return SQLITE_OK;
214314
+ return rc;
214174214315
}
214175214316
214176214317
static int dbpageRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
214177214318
DbpageCursor *pCsr = (DbpageCursor *)pCursor;
214178214319
*pRowid = pCsr->pgno;
@@ -214228,15 +214369,16 @@
214228214369
goto update_fail;
214229214370
}
214230214371
pPager = sqlite3BtreePager(pBt);
214231214372
rc = sqlite3PagerGet(pPager, pgno, (DbPage**)&pDbPage, 0);
214232214373
if( rc==SQLITE_OK ){
214233
- rc = sqlite3PagerWrite(pDbPage);
214234
- if( rc==SQLITE_OK ){
214235
- memcpy(sqlite3PagerGetData(pDbPage),
214236
- sqlite3_value_blob(argv[3]),
214237
- szPage);
214374
+ const void *pData = sqlite3_value_blob(argv[3]);
214375
+ assert( pData!=0 || pTab->db->mallocFailed );
214376
+ if( pData
214377
+ && (rc = sqlite3PagerWrite(pDbPage))==SQLITE_OK
214378
+ ){
214379
+ memcpy(sqlite3PagerGetData(pDbPage), pData, szPage);
214238214380
}
214239214381
}
214240214382
sqlite3PagerUnref(pDbPage);
214241214383
return rc;
214242214384
@@ -238386,11 +238528,11 @@
238386238528
int nArg, /* Number of args */
238387238529
sqlite3_value **apUnused /* Function arguments */
238388238530
){
238389238531
assert( nArg==0 );
238390238532
UNUSED_PARAM2(nArg, apUnused);
238391
- sqlite3_result_text(pCtx, "fts5: 2022-10-26 11:11:31 3dfdfb3f12edb3f4267942598efd05d573e13b7c5d6cdbc3404373f41b8993dd", -1, SQLITE_TRANSIENT);
238533
+ sqlite3_result_text(pCtx, "fts5: 2022-11-07 18:36:02 3645585f37631d60cefab1d55cdb1ee060aae87317b9b158a01329ca8a4d3e1e", -1, SQLITE_TRANSIENT);
238392238534
}
238393238535
238394238536
/*
238395238537
** Return true if zName is the extension on one of the shadow tables used
238396238538
** by this module.
238397238539
--- extsrc/sqlite3.c
+++ extsrc/sqlite3.c
@@ -452,11 +452,11 @@
452 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
453 ** [sqlite_version()] and [sqlite_source_id()].
454 */
455 #define SQLITE_VERSION "3.40.0"
456 #define SQLITE_VERSION_NUMBER 3040000
457 #define SQLITE_SOURCE_ID "2022-10-26 11:11:31 3dfdfb3f12edb3f4267942598efd05d573e13b7c5d6cdbc3404373f41b8993dd"
458
459 /*
460 ** CAPI3REF: Run-Time Library Version Numbers
461 ** KEYWORDS: sqlite3_version sqlite3_sourceid
462 **
@@ -1567,10 +1567,30 @@
1567 ** structure must be typedefed in order to work around compiler warnings
1568 ** on some platforms.
1569 */
1570 typedef struct sqlite3_api_routines sqlite3_api_routines;
1571
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1572 /*
1573 ** CAPI3REF: OS Interface Object
1574 **
1575 ** An instance of the sqlite3_vfs object defines the interface between
1576 ** the SQLite core and the underlying operating system. The "vfs"
@@ -1745,11 +1765,11 @@
1745 int szOsFile; /* Size of subclassed sqlite3_file */
1746 int mxPathname; /* Maximum file pathname length */
1747 sqlite3_vfs *pNext; /* Next registered VFS */
1748 const char *zName; /* Name of this virtual file system */
1749 void *pAppData; /* Pointer to application-specific data */
1750 int (*xOpen)(sqlite3_vfs*, const char *zName, sqlite3_file*,
1751 int flags, int *pOutFlags);
1752 int (*xDelete)(sqlite3_vfs*, const char *zName, int syncDir);
1753 int (*xAccess)(sqlite3_vfs*, const char *zName, int flags, int *pResOut);
1754 int (*xFullPathname)(sqlite3_vfs*, const char *zName, int nOut, char *zOut);
1755 void *(*xDlOpen)(sqlite3_vfs*, const char *zFilename);
@@ -4015,14 +4035,14 @@
4015 ** it has access to all the same query parameters as were found on the
4016 ** main database file.
4017 **
4018 ** See the [URI filename] documentation for additional information.
4019 */
4020 SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam);
4021 SQLITE_API int sqlite3_uri_boolean(const char *zFile, const char *zParam, int bDefault);
4022 SQLITE_API sqlite3_int64 sqlite3_uri_int64(const char*, const char*, sqlite3_int64);
4023 SQLITE_API const char *sqlite3_uri_key(const char *zFilename, int N);
4024
4025 /*
4026 ** CAPI3REF: Translate filenames
4027 **
4028 ** These routines are available to [VFS|custom VFS implementations] for
@@ -4047,13 +4067,13 @@
4047 ** In all of the above, if F is not the name of a database, journal or WAL
4048 ** filename passed into the VFS from the SQLite core and F is not the
4049 ** return value from [sqlite3_db_filename()], then the result is
4050 ** undefined and is likely a memory access violation.
4051 */
4052 SQLITE_API const char *sqlite3_filename_database(const char*);
4053 SQLITE_API const char *sqlite3_filename_journal(const char*);
4054 SQLITE_API const char *sqlite3_filename_wal(const char*);
4055
4056 /*
4057 ** CAPI3REF: Database File Corresponding To A Journal
4058 **
4059 ** ^If X is the name of a rollback or WAL-mode journal file that is
@@ -4115,18 +4135,18 @@
4115 ** used again after sqlite3_free_filename(Y) has been called. This means
4116 ** that if the [sqlite3_vfs.xOpen()] method of a VFS has been called using Y,
4117 ** then the corresponding [sqlite3_module.xClose() method should also be
4118 ** invoked prior to calling sqlite3_free_filename(Y).
4119 */
4120 SQLITE_API char *sqlite3_create_filename(
4121 const char *zDatabase,
4122 const char *zJournal,
4123 const char *zWal,
4124 int nParam,
4125 const char **azParam
4126 );
4127 SQLITE_API void sqlite3_free_filename(char*);
4128
4129 /*
4130 ** CAPI3REF: Error Codes And Messages
4131 ** METHOD: sqlite3
4132 **
@@ -6158,13 +6178,14 @@
6158 ** application-defined function to be a text string in an encoding
6159 ** specified by the fifth (and last) parameter, which must be one
6160 ** of [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE].
6161 ** ^SQLite takes the text result from the application from
6162 ** the 2nd parameter of the sqlite3_result_text* interfaces.
6163 ** ^If the 3rd parameter to the sqlite3_result_text* interfaces
6164 ** is negative, then SQLite takes result text from the 2nd parameter
6165 ** through the first zero character.
 
6166 ** ^If the 3rd parameter to the sqlite3_result_text* interfaces
6167 ** is non-negative, then as many bytes (not characters) of the text
6168 ** pointed to by the 2nd parameter are taken as the application-defined
6169 ** function result. If the 3rd parameter is non-negative, then it
6170 ** must be the byte offset into the string where the NUL terminator would
@@ -6656,11 +6677,11 @@
6656 ** <li> [sqlite3_filename_database()]
6657 ** <li> [sqlite3_filename_journal()]
6658 ** <li> [sqlite3_filename_wal()]
6659 ** </ul>
6660 */
6661 SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName);
6662
6663 /*
6664 ** CAPI3REF: Determine if a database is read-only
6665 ** METHOD: sqlite3
6666 **
@@ -18879,11 +18900,11 @@
18879 u8 eDest; /* How to dispose of the results. One of SRT_* above. */
18880 int iSDParm; /* A parameter used by the eDest disposal method */
18881 int iSDParm2; /* A second parameter for the eDest disposal method */
18882 int iSdst; /* Base register where results are written */
18883 int nSdst; /* Number of registers allocated */
18884 char *zAffSdst; /* Affinity used when eDest==SRT_Set */
18885 ExprList *pOrderBy; /* Key columns for SRT_Queue and SRT_DistQueue */
18886 };
18887
18888 /*
18889 ** During code generation of statements that do inserts into AUTOINCREMENT
@@ -20349,10 +20370,11 @@
20349 #define getVarint sqlite3GetVarint
20350 #define putVarint sqlite3PutVarint
20351
20352
20353 SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(sqlite3*, Index*);
 
20354 SQLITE_PRIVATE void sqlite3TableAffinity(Vdbe*, Table*, int);
20355 SQLITE_PRIVATE char sqlite3CompareAffinity(const Expr *pExpr, char aff2);
20356 SQLITE_PRIVATE int sqlite3IndexAffinityOk(const Expr *pExpr, char idx_affinity);
20357 SQLITE_PRIVATE char sqlite3TableColumnAffinity(const Table*,int);
20358 SQLITE_PRIVATE char sqlite3ExprAffinity(const Expr *pExpr);
@@ -36281,10 +36303,12 @@
36281 char zKey[30];
36282 char aData[131073];
36283 SQLITE_KV_LOG(("xWrite('%s-db',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
36284 assert( iAmt>=512 && iAmt<=65536 );
36285 assert( (iAmt & (iAmt-1))==0 );
 
 
36286 pgno = 1 + iOfst/iAmt;
36287 sqlite3_snprintf(sizeof(zKey), zKey, "%u", pgno);
36288 kvvfsEncode(zBuf, iAmt, aData);
36289 if( sqlite3KvvfsMethods.xWrite(pFile->zClass, zKey, aData) ){
36290 return SQLITE_IOERR;
@@ -36463,10 +36487,11 @@
36463 sqlite3_file *pProtoFile,
36464 int flags,
36465 int *pOutFlags
36466 ){
36467 KVVfsFile *pFile = (KVVfsFile*)pProtoFile;
 
36468 SQLITE_KV_LOG(("xOpen(\"%s\")\n", zName));
36469 if( strcmp(zName, "local")==0
36470 || strcmp(zName, "session")==0
36471 ){
36472 pFile->isJournal = 0;
@@ -49461,13 +49486,14 @@
49461 }
49462 return 0;
49463 }
49464
49465 /*
49466 ** If sqlite3_temp_directory is not, take the mutex and return true.
49467 **
49468 ** If sqlite3_temp_directory is NULL, omit the mutex and return false.
 
49469 */
49470 static int winTempDirDefined(void){
49471 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
49472 if( sqlite3_temp_directory!=0 ) return 1;
49473 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
@@ -69701,11 +69727,10 @@
69701
69702 /* Remove the slot from the free-list. Update the number of
69703 ** fragmented bytes within the page. */
69704 memcpy(&aData[iAddr], &aData[pc], 2);
69705 aData[hdr+7] += (u8)x;
69706 testcase( pc+x>maxPC );
69707 return &aData[pc];
69708 }else if( x+pc > maxPC ){
69709 /* This slot extends off the end of the usable part of the page */
69710 *pRc = SQLITE_CORRUPT_PAGE(pPg);
69711 return 0;
@@ -74302,12 +74327,12 @@
74302
74303 assert( sqlite3_mutex_held(pBt->mutex) );
74304 assert( eMode==BTALLOC_ANY || (nearby>0 && IfNotOmitAV(pBt->autoVacuum)) );
74305 pPage1 = pBt->pPage1;
74306 mxPage = btreePagecount(pBt);
74307 /* EVIDENCE-OF: R-05119-02637 The 4-byte big-endian integer at offset 36
74308 ** stores stores the total number of pages on the freelist. */
74309 n = get4byte(&pPage1->aData[36]);
74310 testcase( n==mxPage-1 );
74311 if( n>=mxPage ){
74312 return SQLITE_CORRUPT_BKPT;
74313 }
@@ -86476,11 +86501,11 @@
86476 assert( pPKey2->pKeyInfo->nAllField>=pPKey2->nField
86477 || CORRUPT_DB );
86478 assert( pPKey2->pKeyInfo->aSortFlags!=0 );
86479 assert( pPKey2->pKeyInfo->nKeyField>0 );
86480 assert( idx1<=szHdr1 || CORRUPT_DB );
86481 do{
86482 u32 serial_type;
86483
86484 /* RHS is an integer */
86485 if( pRhs->flags & (MEM_Int|MEM_IntReal) ){
86486 testcase( pRhs->flags & MEM_Int );
@@ -86614,12 +86639,17 @@
86614
86615 i++;
86616 if( i==pPKey2->nField ) break;
86617 pRhs++;
86618 d1 += sqlite3VdbeSerialTypeLen(serial_type);
 
86619 idx1 += sqlite3VarintLen(serial_type);
86620 }while( idx1<(unsigned)szHdr1 && d1<=(unsigned)nKey1 );
 
 
 
 
86621
86622 /* No memory allocation is ever used on mem1. Prove this using
86623 ** the following assert(). If the assert() fails, it indicates a
86624 ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1). */
86625 assert( mem1.szMalloc==0 );
@@ -127850,10 +127880,32 @@
127850 pIdx->zColAff[n] = 0;
127851 }
127852
127853 return pIdx->zColAff;
127854 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127855
127856 /*
127857 ** Make changes to the evolving bytecode to do affinity transformations
127858 ** of values that are about to be gathered into a row for table pTab.
127859 **
@@ -127892,11 +127944,11 @@
127892 ** register set as the OP_MakeRecord. If iReg>0 then register iReg is
127893 ** the first of a series of registers that will form the new record.
127894 ** Apply the type checking to that array of registers.
127895 */
127896 SQLITE_PRIVATE void sqlite3TableAffinity(Vdbe *v, Table *pTab, int iReg){
127897 int i, j;
127898 char *zColAff;
127899 if( pTab->tabFlags & TF_Strict ){
127900 if( iReg==0 ){
127901 /* Move the previous opcode (which should be OP_MakeRecord) forward
127902 ** by one slot and insert a new OP_TypeCheck where the current
@@ -127915,26 +127967,15 @@
127915 }
127916 return;
127917 }
127918 zColAff = pTab->zColAff;
127919 if( zColAff==0 ){
127920 sqlite3 *db = sqlite3VdbeDb(v);
127921 zColAff = (char *)sqlite3DbMallocRaw(0, pTab->nCol+1);
127922 if( !zColAff ){
127923 sqlite3OomFault(db);
127924 return;
127925 }
127926
127927 for(i=j=0; i<pTab->nCol; i++){
127928 assert( pTab->aCol[i].affinity!=0 || sqlite3VdbeParser(v)->nErr>0 );
127929 if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ){
127930 zColAff[j++] = pTab->aCol[i].affinity;
127931 }
127932 }
127933 do{
127934 zColAff[j--] = 0;
127935 }while( j>=0 && zColAff[j]<=SQLITE_AFF_BLOB );
127936 pTab->zColAff = zColAff;
127937 }
127938 assert( zColAff!=0 );
127939 i = sqlite3Strlen30NN(zColAff);
127940 if( i ){
@@ -131386,13 +131427,13 @@
131386 const char *(*uri_key)(const char*,int);
131387 const char *(*filename_database)(const char*);
131388 const char *(*filename_journal)(const char*);
131389 const char *(*filename_wal)(const char*);
131390 /* Version 3.32.0 and later */
131391 char *(*create_filename)(const char*,const char*,const char*,
131392 int,const char**);
131393 void (*free_filename)(char*);
131394 sqlite3_file *(*database_file_object)(const char*);
131395 /* Version 3.34.0 and later */
131396 int (*txn_state)(sqlite3*,const char*);
131397 /* Version 3.36.1 and later */
131398 sqlite3_int64 (*changes64)(sqlite3*);
@@ -138504,10 +138545,13 @@
138504 testcase( eDest==SRT_Table );
138505 testcase( eDest==SRT_EphemTab );
138506 testcase( eDest==SRT_Fifo );
138507 testcase( eDest==SRT_DistFifo );
138508 sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1+nPrefixReg);
 
 
 
138509 #ifndef SQLITE_OMIT_CTE
138510 if( eDest==SRT_DistFifo ){
138511 /* If the destination is DistFifo, then cursor (iParm+1) is open
138512 ** on an ephemeral index. If the current row is already present
138513 ** in the index, do not write it to the output. If not, add the
@@ -140907,12 +140951,12 @@
140907
140908 /* Jump to the this point in order to terminate the query.
140909 */
140910 sqlite3VdbeResolveLabel(v, labelEnd);
140911
140912 /* Reassemble the compound query so that it will be freed correctly
140913 ** by the calling function */
140914 if( pSplit->pPrior ){
140915 sqlite3ParserAddCleanup(pParse,
140916 (void(*)(sqlite3*,void*))sqlite3SelectDelete, pSplit->pPrior);
140917 }
140918 pSplit->pPrior = pPrior;
@@ -141366,10 +141410,12 @@
141366 ** (17e) the subquery may not contain window functions, and
141367 ** (17f) the subquery must not be the RHS of a LEFT JOIN.
141368 ** (17g) either the subquery is the first element of the outer
141369 ** query or there are no RIGHT or FULL JOINs in any arm
141370 ** of the subquery. (This is a duplicate of condition (27b).)
 
 
141371 **
141372 ** The parent and sub-query may contain WHERE clauses. Subject to
141373 ** rules (11), (13) and (14), they may also contain ORDER BY,
141374 ** LIMIT and OFFSET clauses. The subquery cannot use any compound
141375 ** operator other than UNION ALL because all the other compound
@@ -141542,10 +141588,11 @@
141542 ** use only the UNION ALL operator. And none of the simple select queries
141543 ** that make up the compound SELECT are allowed to be aggregate or distinct
141544 ** queries.
141545 */
141546 if( pSub->pPrior ){
 
141547 if( pSub->pOrderBy ){
141548 return 0; /* Restriction (20) */
141549 }
141550 if( isAgg || (p->selFlags & SF_Distinct)!=0 || isOuterJoin>0 ){
141551 return 0; /* (17d1), (17d2), or (17f) */
@@ -141574,18 +141621,32 @@
141574 testcase( pSub1->pSrc->nSrc>1 );
141575 }
141576
141577 /* Restriction (18). */
141578 if( p->pOrderBy ){
141579 int ii;
141580 for(ii=0; ii<p->pOrderBy->nExpr; ii++){
141581 if( p->pOrderBy->a[ii].u.x.iOrderByCol==0 ) return 0;
141582 }
141583 }
141584
141585 /* Restriction (23) */
141586 if( (p->selFlags & SF_Recursive) ) return 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
141587
141588 if( pSrc->nSrc>1 ){
141589 if( pParse->nSelect>500 ) return 0;
141590 if( OptimizationDisabled(db, SQLITE_FlttnUnionAll) ) return 0;
141591 aCsrMap = sqlite3DbMallocZero(db, ((i64)pParse->nTab+1)*sizeof(int));
@@ -142225,10 +142286,17 @@
142225 ** window over which any window-function is calculated.
142226 **
142227 ** (7) The inner query is a Common Table Expression (CTE) that should
142228 ** be materialized. (This restriction is implemented in the calling
142229 ** routine.)
 
 
 
 
 
 
 
142230 **
142231 ** Return 0 if no changes are made and non-zero if one or more WHERE clause
142232 ** terms are duplicated into the subquery.
142233 */
142234 static int pushDownWhereTerms(
@@ -142245,10 +142313,14 @@
142245
142246 #ifndef SQLITE_OMIT_WINDOWFUNC
142247 if( pSubq->pPrior ){
142248 Select *pSel;
142249 for(pSel=pSubq; pSel; pSel=pSel->pPrior){
 
 
 
 
142250 if( pSel->pWin ) return 0; /* restriction (6b) */
142251 }
142252 }else{
142253 if( pSubq->pWin && pSubq->pWin->pPartition==0 ) return 0;
142254 }
@@ -144277,11 +144349,14 @@
144277 }else{
144278 VdbeNoopComment((v, "materialize %!S", pItem));
144279 }
144280 sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
144281 ExplainQueryPlan((pParse, 1, "MATERIALIZE %!S", pItem));
 
144282 sqlite3Select(pParse, pSub, &dest);
 
 
144283 pItem->pTab->nRowLogEst = pSub->nSelectRow;
144284 if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr);
144285 sqlite3VdbeAddOp2(v, OP_Return, pItem->regReturn, topAddr+1);
144286 VdbeComment((v, "end %!S", pItem));
144287 sqlite3VdbeJumpHere(v, topAddr);
@@ -175725,10 +175800,16 @@
175725 if( rc==SQLITE_NOMEM ) sqlite3OomFault(db);
175726 sqlite3ErrorWithMsg(db, rc, zErrMsg ? "%s" : 0, zErrMsg);
175727 sqlite3_free(zErrMsg);
175728 goto opendb_out;
175729 }
 
 
 
 
 
 
175730
175731 /* Open the backend database driver */
175732 rc = sqlite3BtreeOpen(db->pVfs, zOpen, db, &db->aDb[0].pBt, 0,
175733 flags | SQLITE_OPEN_MAIN_DB);
175734 if( rc!=SQLITE_OK ){
@@ -176834,11 +176915,11 @@
176834 ** functions.
176835 **
176836 ** Memory layout must be compatible with that generated by the pager
176837 ** and expected by sqlite3_uri_parameter() and databaseName().
176838 */
176839 SQLITE_API char *sqlite3_create_filename(
176840 const char *zDatabase,
176841 const char *zJournal,
176842 const char *zWal,
176843 int nParam,
176844 const char **azParam
@@ -176870,14 +176951,14 @@
176870 /*
176871 ** Free memory obtained from sqlite3_create_filename(). It is a severe
176872 ** error to call this routine with any parameter other than a pointer
176873 ** previously obtained from sqlite3_create_filename() or a NULL pointer.
176874 */
176875 SQLITE_API void sqlite3_free_filename(char *p){
176876 if( p==0 ) return;
176877 p = (char*)databaseName(p);
176878 sqlite3_free(p - 4);
176879 }
176880
176881
176882 /*
176883 ** This is a utility routine, useful to VFS implementations, that checks
@@ -207653,10 +207734,38 @@
207653 #define SQLITE_RBU_STATE_CHECKPOINT 3
207654 #define SQLITE_RBU_STATE_DONE 4
207655 #define SQLITE_RBU_STATE_ERROR 5
207656
207657 SQLITE_API int sqlite3rbu_state(sqlite3rbu *pRbu);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
207658
207659 /*
207660 ** Create an RBU VFS named zName that accesses the underlying file-system
207661 ** via existing VFS zParent. Or, if the zParent parameter is passed NULL,
207662 ** then the new RBU VFS uses the default system VFS to access the file-system.
@@ -208021,10 +208130,12 @@
208021 const char *zVfsName; /* Name of automatically created rbu vfs */
208022 rbu_file *pTargetFd; /* File handle open on target db */
208023 int nPagePerSector; /* Pages per sector for pTargetFd */
208024 i64 iOalSz;
208025 i64 nPhaseOneStep;
 
 
208026
208027 /* The following state variables are used as part of the incremental
208028 ** checkpoint stage (eStage==RBU_STAGE_CKPT). See comments surrounding
208029 ** function rbuSetupCheckpoint() for details. */
208030 u32 iMaxFrame; /* Largest iWalFrame value in aFrame[] */
@@ -210869,36 +210980,11 @@
210869 assert( p->rc==SQLITE_OK );
210870 p->rc = rbuLockDatabase(dbMain);
210871 }
210872
210873 if( p->rc==SQLITE_OK ){
210874 #if defined(_WIN32_WCE)
210875 {
210876 LPWSTR zWideOal;
210877 LPWSTR zWideWal;
210878
210879 zWideOal = rbuWinUtf8ToUnicode(zOal);
210880 if( zWideOal ){
210881 zWideWal = rbuWinUtf8ToUnicode(zWal);
210882 if( zWideWal ){
210883 if( MoveFileW(zWideOal, zWideWal) ){
210884 p->rc = SQLITE_OK;
210885 }else{
210886 p->rc = SQLITE_IOERR;
210887 }
210888 sqlite3_free(zWideWal);
210889 }else{
210890 p->rc = SQLITE_IOERR_NOMEM;
210891 }
210892 sqlite3_free(zWideOal);
210893 }else{
210894 p->rc = SQLITE_IOERR_NOMEM;
210895 }
210896 }
210897 #else
210898 p->rc = rename(zOal, zWal) ? SQLITE_IOERR : SQLITE_OK;
210899 #endif
210900 }
210901
210902 if( p->rc!=SQLITE_OK
210903 || rbuIsVacuum(p)
210904 || rbuExclusiveCheckpoint(dbMain)==0
@@ -211633,10 +211719,11 @@
211633 if( p ){
211634 RbuState *pState = 0;
211635
211636 /* Create the custom VFS. */
211637 memset(p, 0, sizeof(sqlite3rbu));
 
211638 rbuCreateVfs(p);
211639
211640 /* Open the target, RBU and state databases */
211641 if( p->rc==SQLITE_OK ){
211642 char *pCsr = (char*)&p[1];
@@ -212023,10 +212110,58 @@
212023 }
212024
212025 p->rc = rc;
212026 return rc;
212027 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
212028
212029 /**************************************************************************
212030 ** Beginning of RBU VFS shim methods. The VFS shim modifies the behaviour
212031 ** of a standard VFS in the following ways:
212032 **
@@ -214154,25 +214289,31 @@
214154 sqlite3_result_int(ctx, pCsr->pgno);
214155 break;
214156 }
214157 case 1: { /* data */
214158 DbPage *pDbPage = 0;
214159 rc = sqlite3PagerGet(pCsr->pPager, pCsr->pgno, (DbPage**)&pDbPage, 0);
214160 if( rc==SQLITE_OK ){
214161 sqlite3_result_blob(ctx, sqlite3PagerGetData(pDbPage), pCsr->szPage,
214162 SQLITE_TRANSIENT);
 
 
 
 
 
 
 
214163 }
214164 sqlite3PagerUnref(pDbPage);
214165 break;
214166 }
214167 default: { /* schema */
214168 sqlite3 *db = sqlite3_context_db_handle(ctx);
214169 sqlite3_result_text(ctx, db->aDb[pCsr->iDb].zDbSName, -1, SQLITE_STATIC);
214170 break;
214171 }
214172 }
214173 return SQLITE_OK;
214174 }
214175
214176 static int dbpageRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
214177 DbpageCursor *pCsr = (DbpageCursor *)pCursor;
214178 *pRowid = pCsr->pgno;
@@ -214228,15 +214369,16 @@
214228 goto update_fail;
214229 }
214230 pPager = sqlite3BtreePager(pBt);
214231 rc = sqlite3PagerGet(pPager, pgno, (DbPage**)&pDbPage, 0);
214232 if( rc==SQLITE_OK ){
214233 rc = sqlite3PagerWrite(pDbPage);
214234 if( rc==SQLITE_OK ){
214235 memcpy(sqlite3PagerGetData(pDbPage),
214236 sqlite3_value_blob(argv[3]),
214237 szPage);
 
214238 }
214239 }
214240 sqlite3PagerUnref(pDbPage);
214241 return rc;
214242
@@ -238386,11 +238528,11 @@
238386 int nArg, /* Number of args */
238387 sqlite3_value **apUnused /* Function arguments */
238388 ){
238389 assert( nArg==0 );
238390 UNUSED_PARAM2(nArg, apUnused);
238391 sqlite3_result_text(pCtx, "fts5: 2022-10-26 11:11:31 3dfdfb3f12edb3f4267942598efd05d573e13b7c5d6cdbc3404373f41b8993dd", -1, SQLITE_TRANSIENT);
238392 }
238393
238394 /*
238395 ** Return true if zName is the extension on one of the shadow tables used
238396 ** by this module.
238397
--- extsrc/sqlite3.c
+++ extsrc/sqlite3.c
@@ -452,11 +452,11 @@
452 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
453 ** [sqlite_version()] and [sqlite_source_id()].
454 */
455 #define SQLITE_VERSION "3.40.0"
456 #define SQLITE_VERSION_NUMBER 3040000
457 #define SQLITE_SOURCE_ID "2022-11-07 19:40:20 55a19677d723147aeb2b4a86bbd01756ddeb2072cba72c3145ad32d335e203b0"
458
459 /*
460 ** CAPI3REF: Run-Time Library Version Numbers
461 ** KEYWORDS: sqlite3_version sqlite3_sourceid
462 **
@@ -1567,10 +1567,30 @@
1567 ** structure must be typedefed in order to work around compiler warnings
1568 ** on some platforms.
1569 */
1570 typedef struct sqlite3_api_routines sqlite3_api_routines;
1571
1572 /*
1573 ** CAPI3REF: File Name
1574 **
1575 ** Type [sqlite3_filename] is used by SQLite to pass filenames to the
1576 ** xOpen method of a [VFS]. It may be cast to (const char*) and treated
1577 ** as a normal, nul-terminated, UTF-8 buffer containing the filename, but
1578 ** may also be passed to special APIs such as:
1579 **
1580 ** <ul>
1581 ** <li> sqlite3_filename_database()
1582 ** <li> sqlite3_filename_journal()
1583 ** <li> sqlite3_filename_wal()
1584 ** <li> sqlite3_uri_parameter()
1585 ** <li> sqlite3_uri_boolean()
1586 ** <li> sqlite3_uri_int64()
1587 ** <li> sqlite3_uri_key()
1588 ** </ul>
1589 */
1590 typedef const char *sqlite3_filename;
1591
1592 /*
1593 ** CAPI3REF: OS Interface Object
1594 **
1595 ** An instance of the sqlite3_vfs object defines the interface between
1596 ** the SQLite core and the underlying operating system. The "vfs"
@@ -1745,11 +1765,11 @@
1765 int szOsFile; /* Size of subclassed sqlite3_file */
1766 int mxPathname; /* Maximum file pathname length */
1767 sqlite3_vfs *pNext; /* Next registered VFS */
1768 const char *zName; /* Name of this virtual file system */
1769 void *pAppData; /* Pointer to application-specific data */
1770 int (*xOpen)(sqlite3_vfs*, sqlite3_filename zName, sqlite3_file*,
1771 int flags, int *pOutFlags);
1772 int (*xDelete)(sqlite3_vfs*, const char *zName, int syncDir);
1773 int (*xAccess)(sqlite3_vfs*, const char *zName, int flags, int *pResOut);
1774 int (*xFullPathname)(sqlite3_vfs*, const char *zName, int nOut, char *zOut);
1775 void *(*xDlOpen)(sqlite3_vfs*, const char *zFilename);
@@ -4015,14 +4035,14 @@
4035 ** it has access to all the same query parameters as were found on the
4036 ** main database file.
4037 **
4038 ** See the [URI filename] documentation for additional information.
4039 */
4040 SQLITE_API const char *sqlite3_uri_parameter(sqlite3_filename z, const char *zParam);
4041 SQLITE_API int sqlite3_uri_boolean(sqlite3_filename z, const char *zParam, int bDefault);
4042 SQLITE_API sqlite3_int64 sqlite3_uri_int64(sqlite3_filename, const char*, sqlite3_int64);
4043 SQLITE_API const char *sqlite3_uri_key(sqlite3_filename z, int N);
4044
4045 /*
4046 ** CAPI3REF: Translate filenames
4047 **
4048 ** These routines are available to [VFS|custom VFS implementations] for
@@ -4047,13 +4067,13 @@
4067 ** In all of the above, if F is not the name of a database, journal or WAL
4068 ** filename passed into the VFS from the SQLite core and F is not the
4069 ** return value from [sqlite3_db_filename()], then the result is
4070 ** undefined and is likely a memory access violation.
4071 */
4072 SQLITE_API const char *sqlite3_filename_database(sqlite3_filename);
4073 SQLITE_API const char *sqlite3_filename_journal(sqlite3_filename);
4074 SQLITE_API const char *sqlite3_filename_wal(sqlite3_filename);
4075
4076 /*
4077 ** CAPI3REF: Database File Corresponding To A Journal
4078 **
4079 ** ^If X is the name of a rollback or WAL-mode journal file that is
@@ -4115,18 +4135,18 @@
4135 ** used again after sqlite3_free_filename(Y) has been called. This means
4136 ** that if the [sqlite3_vfs.xOpen()] method of a VFS has been called using Y,
4137 ** then the corresponding [sqlite3_module.xClose() method should also be
4138 ** invoked prior to calling sqlite3_free_filename(Y).
4139 */
4140 SQLITE_API sqlite3_filename sqlite3_create_filename(
4141 const char *zDatabase,
4142 const char *zJournal,
4143 const char *zWal,
4144 int nParam,
4145 const char **azParam
4146 );
4147 SQLITE_API void sqlite3_free_filename(sqlite3_filename);
4148
4149 /*
4150 ** CAPI3REF: Error Codes And Messages
4151 ** METHOD: sqlite3
4152 **
@@ -6158,13 +6178,14 @@
6178 ** application-defined function to be a text string in an encoding
6179 ** specified by the fifth (and last) parameter, which must be one
6180 ** of [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE].
6181 ** ^SQLite takes the text result from the application from
6182 ** the 2nd parameter of the sqlite3_result_text* interfaces.
6183 ** ^If the 3rd parameter to any of the sqlite3_result_text* interfaces
6184 ** other than sqlite3_result_text64() is negative, then SQLite computes
6185 ** the string length itself by searching the 2nd parameter for the first
6186 ** zero character.
6187 ** ^If the 3rd parameter to the sqlite3_result_text* interfaces
6188 ** is non-negative, then as many bytes (not characters) of the text
6189 ** pointed to by the 2nd parameter are taken as the application-defined
6190 ** function result. If the 3rd parameter is non-negative, then it
6191 ** must be the byte offset into the string where the NUL terminator would
@@ -6656,11 +6677,11 @@
6677 ** <li> [sqlite3_filename_database()]
6678 ** <li> [sqlite3_filename_journal()]
6679 ** <li> [sqlite3_filename_wal()]
6680 ** </ul>
6681 */
6682 SQLITE_API sqlite3_filename sqlite3_db_filename(sqlite3 *db, const char *zDbName);
6683
6684 /*
6685 ** CAPI3REF: Determine if a database is read-only
6686 ** METHOD: sqlite3
6687 **
@@ -18879,11 +18900,11 @@
18900 u8 eDest; /* How to dispose of the results. One of SRT_* above. */
18901 int iSDParm; /* A parameter used by the eDest disposal method */
18902 int iSDParm2; /* A second parameter for the eDest disposal method */
18903 int iSdst; /* Base register where results are written */
18904 int nSdst; /* Number of registers allocated */
18905 char *zAffSdst; /* Affinity used for SRT_Set, SRT_Table, and similar */
18906 ExprList *pOrderBy; /* Key columns for SRT_Queue and SRT_DistQueue */
18907 };
18908
18909 /*
18910 ** During code generation of statements that do inserts into AUTOINCREMENT
@@ -20349,10 +20370,11 @@
20370 #define getVarint sqlite3GetVarint
20371 #define putVarint sqlite3PutVarint
20372
20373
20374 SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(sqlite3*, Index*);
20375 SQLITE_PRIVATE char *sqlite3TableAffinityStr(sqlite3*,const Table*);
20376 SQLITE_PRIVATE void sqlite3TableAffinity(Vdbe*, Table*, int);
20377 SQLITE_PRIVATE char sqlite3CompareAffinity(const Expr *pExpr, char aff2);
20378 SQLITE_PRIVATE int sqlite3IndexAffinityOk(const Expr *pExpr, char idx_affinity);
20379 SQLITE_PRIVATE char sqlite3TableColumnAffinity(const Table*,int);
20380 SQLITE_PRIVATE char sqlite3ExprAffinity(const Expr *pExpr);
@@ -36281,10 +36303,12 @@
36303 char zKey[30];
36304 char aData[131073];
36305 SQLITE_KV_LOG(("xWrite('%s-db',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
36306 assert( iAmt>=512 && iAmt<=65536 );
36307 assert( (iAmt & (iAmt-1))==0 );
36308 assert( pFile->szPage<0 || pFile->szPage==iAmt );
36309 pFile->szPage = iAmt;
36310 pgno = 1 + iOfst/iAmt;
36311 sqlite3_snprintf(sizeof(zKey), zKey, "%u", pgno);
36312 kvvfsEncode(zBuf, iAmt, aData);
36313 if( sqlite3KvvfsMethods.xWrite(pFile->zClass, zKey, aData) ){
36314 return SQLITE_IOERR;
@@ -36463,10 +36487,11 @@
36487 sqlite3_file *pProtoFile,
36488 int flags,
36489 int *pOutFlags
36490 ){
36491 KVVfsFile *pFile = (KVVfsFile*)pProtoFile;
36492 if( zName==0 ) zName = "";
36493 SQLITE_KV_LOG(("xOpen(\"%s\")\n", zName));
36494 if( strcmp(zName, "local")==0
36495 || strcmp(zName, "session")==0
36496 ){
36497 pFile->isJournal = 0;
@@ -49461,13 +49486,14 @@
49486 }
49487 return 0;
49488 }
49489
49490 /*
49491 ** If sqlite3_temp_directory is defined, take the mutex and return true.
49492 **
49493 ** If sqlite3_temp_directory is NULL (undefined), omit the mutex and
49494 ** return false.
49495 */
49496 static int winTempDirDefined(void){
49497 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
49498 if( sqlite3_temp_directory!=0 ) return 1;
49499 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
@@ -69701,11 +69727,10 @@
69727
69728 /* Remove the slot from the free-list. Update the number of
69729 ** fragmented bytes within the page. */
69730 memcpy(&aData[iAddr], &aData[pc], 2);
69731 aData[hdr+7] += (u8)x;
 
69732 return &aData[pc];
69733 }else if( x+pc > maxPC ){
69734 /* This slot extends off the end of the usable part of the page */
69735 *pRc = SQLITE_CORRUPT_PAGE(pPg);
69736 return 0;
@@ -74302,12 +74327,12 @@
74327
74328 assert( sqlite3_mutex_held(pBt->mutex) );
74329 assert( eMode==BTALLOC_ANY || (nearby>0 && IfNotOmitAV(pBt->autoVacuum)) );
74330 pPage1 = pBt->pPage1;
74331 mxPage = btreePagecount(pBt);
74332 /* EVIDENCE-OF: R-21003-45125 The 4-byte big-endian integer at offset 36
74333 ** stores the total number of pages on the freelist. */
74334 n = get4byte(&pPage1->aData[36]);
74335 testcase( n==mxPage-1 );
74336 if( n>=mxPage ){
74337 return SQLITE_CORRUPT_BKPT;
74338 }
@@ -86476,11 +86501,11 @@
86501 assert( pPKey2->pKeyInfo->nAllField>=pPKey2->nField
86502 || CORRUPT_DB );
86503 assert( pPKey2->pKeyInfo->aSortFlags!=0 );
86504 assert( pPKey2->pKeyInfo->nKeyField>0 );
86505 assert( idx1<=szHdr1 || CORRUPT_DB );
86506 while( 1 /*exit-by-break*/ ){
86507 u32 serial_type;
86508
86509 /* RHS is an integer */
86510 if( pRhs->flags & (MEM_Int|MEM_IntReal) ){
86511 testcase( pRhs->flags & MEM_Int );
@@ -86614,12 +86639,17 @@
86639
86640 i++;
86641 if( i==pPKey2->nField ) break;
86642 pRhs++;
86643 d1 += sqlite3VdbeSerialTypeLen(serial_type);
86644 if( d1>(unsigned)nKey1 ) break;
86645 idx1 += sqlite3VarintLen(serial_type);
86646 if( idx1>=(unsigned)szHdr1 ){
86647 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
86648 return 0; /* Corrupt index */
86649 }
86650 }
86651
86652 /* No memory allocation is ever used on mem1. Prove this using
86653 ** the following assert(). If the assert() fails, it indicates a
86654 ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1). */
86655 assert( mem1.szMalloc==0 );
@@ -127850,10 +127880,32 @@
127880 pIdx->zColAff[n] = 0;
127881 }
127882
127883 return pIdx->zColAff;
127884 }
127885
127886 /*
127887 ** Compute an affinity string for a table. Space is obtained
127888 ** from sqlite3DbMalloc(). The caller is responsible for freeing
127889 ** the space when done.
127890 */
127891 SQLITE_PRIVATE char *sqlite3TableAffinityStr(sqlite3 *db, const Table *pTab){
127892 char *zColAff;
127893 zColAff = (char *)sqlite3DbMallocRaw(db, pTab->nCol+1);
127894 if( zColAff ){
127895 int i, j;
127896 for(i=j=0; i<pTab->nCol; i++){
127897 if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ){
127898 zColAff[j++] = pTab->aCol[i].affinity;
127899 }
127900 }
127901 do{
127902 zColAff[j--] = 0;
127903 }while( j>=0 && zColAff[j]<=SQLITE_AFF_BLOB );
127904 }
127905 return zColAff;
127906 }
127907
127908 /*
127909 ** Make changes to the evolving bytecode to do affinity transformations
127910 ** of values that are about to be gathered into a row for table pTab.
127911 **
@@ -127892,11 +127944,11 @@
127944 ** register set as the OP_MakeRecord. If iReg>0 then register iReg is
127945 ** the first of a series of registers that will form the new record.
127946 ** Apply the type checking to that array of registers.
127947 */
127948 SQLITE_PRIVATE void sqlite3TableAffinity(Vdbe *v, Table *pTab, int iReg){
127949 int i;
127950 char *zColAff;
127951 if( pTab->tabFlags & TF_Strict ){
127952 if( iReg==0 ){
127953 /* Move the previous opcode (which should be OP_MakeRecord) forward
127954 ** by one slot and insert a new OP_TypeCheck where the current
@@ -127915,26 +127967,15 @@
127967 }
127968 return;
127969 }
127970 zColAff = pTab->zColAff;
127971 if( zColAff==0 ){
127972 zColAff = sqlite3TableAffinityStr(0, pTab);
 
127973 if( !zColAff ){
127974 sqlite3OomFault(sqlite3VdbeDb(v));
127975 return;
127976 }
 
 
 
 
 
 
 
 
 
 
127977 pTab->zColAff = zColAff;
127978 }
127979 assert( zColAff!=0 );
127980 i = sqlite3Strlen30NN(zColAff);
127981 if( i ){
@@ -131386,13 +131427,13 @@
131427 const char *(*uri_key)(const char*,int);
131428 const char *(*filename_database)(const char*);
131429 const char *(*filename_journal)(const char*);
131430 const char *(*filename_wal)(const char*);
131431 /* Version 3.32.0 and later */
131432 const char *(*create_filename)(const char*,const char*,const char*,
131433 int,const char**);
131434 void (*free_filename)(const char*);
131435 sqlite3_file *(*database_file_object)(const char*);
131436 /* Version 3.34.0 and later */
131437 int (*txn_state)(sqlite3*,const char*);
131438 /* Version 3.36.1 and later */
131439 sqlite3_int64 (*changes64)(sqlite3*);
@@ -138504,10 +138545,13 @@
138545 testcase( eDest==SRT_Table );
138546 testcase( eDest==SRT_EphemTab );
138547 testcase( eDest==SRT_Fifo );
138548 testcase( eDest==SRT_DistFifo );
138549 sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1+nPrefixReg);
138550 if( pDest->zAffSdst ){
138551 sqlite3VdbeChangeP4(v, -1, pDest->zAffSdst, nResultCol);
138552 }
138553 #ifndef SQLITE_OMIT_CTE
138554 if( eDest==SRT_DistFifo ){
138555 /* If the destination is DistFifo, then cursor (iParm+1) is open
138556 ** on an ephemeral index. If the current row is already present
138557 ** in the index, do not write it to the output. If not, add the
@@ -140907,12 +140951,12 @@
140951
140952 /* Jump to the this point in order to terminate the query.
140953 */
140954 sqlite3VdbeResolveLabel(v, labelEnd);
140955
140956 /* Make arrangements to free the 2nd and subsequent arms of the compound
140957 ** after the parse has finished */
140958 if( pSplit->pPrior ){
140959 sqlite3ParserAddCleanup(pParse,
140960 (void(*)(sqlite3*,void*))sqlite3SelectDelete, pSplit->pPrior);
140961 }
140962 pSplit->pPrior = pPrior;
@@ -141366,10 +141410,12 @@
141410 ** (17e) the subquery may not contain window functions, and
141411 ** (17f) the subquery must not be the RHS of a LEFT JOIN.
141412 ** (17g) either the subquery is the first element of the outer
141413 ** query or there are no RIGHT or FULL JOINs in any arm
141414 ** of the subquery. (This is a duplicate of condition (27b).)
141415 ** (17h) The corresponding result set expressions in all arms of the
141416 ** compound must have the same affinity.
141417 **
141418 ** The parent and sub-query may contain WHERE clauses. Subject to
141419 ** rules (11), (13) and (14), they may also contain ORDER BY,
141420 ** LIMIT and OFFSET clauses. The subquery cannot use any compound
141421 ** operator other than UNION ALL because all the other compound
@@ -141542,10 +141588,11 @@
141588 ** use only the UNION ALL operator. And none of the simple select queries
141589 ** that make up the compound SELECT are allowed to be aggregate or distinct
141590 ** queries.
141591 */
141592 if( pSub->pPrior ){
141593 int ii;
141594 if( pSub->pOrderBy ){
141595 return 0; /* Restriction (20) */
141596 }
141597 if( isAgg || (p->selFlags & SF_Distinct)!=0 || isOuterJoin>0 ){
141598 return 0; /* (17d1), (17d2), or (17f) */
@@ -141574,18 +141621,32 @@
141621 testcase( pSub1->pSrc->nSrc>1 );
141622 }
141623
141624 /* Restriction (18). */
141625 if( p->pOrderBy ){
 
141626 for(ii=0; ii<p->pOrderBy->nExpr; ii++){
141627 if( p->pOrderBy->a[ii].u.x.iOrderByCol==0 ) return 0;
141628 }
141629 }
141630
141631 /* Restriction (23) */
141632 if( (p->selFlags & SF_Recursive) ) return 0;
141633
141634 /* Restriction (17h) */
141635 for(ii=0; ii<pSub->pEList->nExpr; ii++){
141636 char aff;
141637 assert( pSub->pEList->a[ii].pExpr!=0 );
141638 aff = sqlite3ExprAffinity(pSub->pEList->a[ii].pExpr);
141639 for(pSub1=pSub->pPrior; pSub1; pSub1=pSub1->pPrior){
141640 assert( pSub1->pEList!=0 );
141641 assert( pSub1->pEList->nExpr>ii );
141642 assert( pSub1->pEList->a[ii].pExpr!=0 );
141643 if( sqlite3ExprAffinity(pSub1->pEList->a[ii].pExpr)!=aff ){
141644 return 0;
141645 }
141646 }
141647 }
141648
141649 if( pSrc->nSrc>1 ){
141650 if( pParse->nSelect>500 ) return 0;
141651 if( OptimizationDisabled(db, SQLITE_FlttnUnionAll) ) return 0;
141652 aCsrMap = sqlite3DbMallocZero(db, ((i64)pParse->nTab+1)*sizeof(int));
@@ -142225,10 +142286,17 @@
142286 ** window over which any window-function is calculated.
142287 **
142288 ** (7) The inner query is a Common Table Expression (CTE) that should
142289 ** be materialized. (This restriction is implemented in the calling
142290 ** routine.)
142291 **
142292 ** (8) The subquery may not be a compound that uses UNION, INTERSECT,
142293 ** or EXCEPT. (We could, perhaps, relax this restriction to allow
142294 ** this case if none of the comparisons operators between left and
142295 ** right arms of the compound use a collation other than BINARY.
142296 ** But it is a lot of work to check that case for an obscure and
142297 ** minor optimization, so we omit it for now.)
142298 **
142299 ** Return 0 if no changes are made and non-zero if one or more WHERE clause
142300 ** terms are duplicated into the subquery.
142301 */
142302 static int pushDownWhereTerms(
@@ -142245,10 +142313,14 @@
142313
142314 #ifndef SQLITE_OMIT_WINDOWFUNC
142315 if( pSubq->pPrior ){
142316 Select *pSel;
142317 for(pSel=pSubq; pSel; pSel=pSel->pPrior){
142318 u8 op = pSel->op;
142319 assert( op==TK_ALL || op==TK_SELECT
142320 || op==TK_UNION || op==TK_INTERSECT || op==TK_EXCEPT );
142321 if( op!=TK_ALL && op!=TK_SELECT ) return 0; /* restriction (8) */
142322 if( pSel->pWin ) return 0; /* restriction (6b) */
142323 }
142324 }else{
142325 if( pSubq->pWin && pSubq->pWin->pPartition==0 ) return 0;
142326 }
@@ -144277,11 +144349,14 @@
144349 }else{
144350 VdbeNoopComment((v, "materialize %!S", pItem));
144351 }
144352 sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
144353 ExplainQueryPlan((pParse, 1, "MATERIALIZE %!S", pItem));
144354 dest.zAffSdst = sqlite3TableAffinityStr(db, pItem->pTab);
144355 sqlite3Select(pParse, pSub, &dest);
144356 sqlite3DbFree(db, dest.zAffSdst);
144357 dest.zAffSdst = 0;
144358 pItem->pTab->nRowLogEst = pSub->nSelectRow;
144359 if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr);
144360 sqlite3VdbeAddOp2(v, OP_Return, pItem->regReturn, topAddr+1);
144361 VdbeComment((v, "end %!S", pItem));
144362 sqlite3VdbeJumpHere(v, topAddr);
@@ -175725,10 +175800,16 @@
175800 if( rc==SQLITE_NOMEM ) sqlite3OomFault(db);
175801 sqlite3ErrorWithMsg(db, rc, zErrMsg ? "%s" : 0, zErrMsg);
175802 sqlite3_free(zErrMsg);
175803 goto opendb_out;
175804 }
175805 assert( db->pVfs!=0 );
175806 #if SQLITE_OS_KV || defined(SQLITE_OS_KV_OPTIONAL)
175807 if( sqlite3_stricmp(db->pVfs->zName, "kvvfs")==0 ){
175808 db->temp_store = 2;
175809 }
175810 #endif
175811
175812 /* Open the backend database driver */
175813 rc = sqlite3BtreeOpen(db->pVfs, zOpen, db, &db->aDb[0].pBt, 0,
175814 flags | SQLITE_OPEN_MAIN_DB);
175815 if( rc!=SQLITE_OK ){
@@ -176834,11 +176915,11 @@
176915 ** functions.
176916 **
176917 ** Memory layout must be compatible with that generated by the pager
176918 ** and expected by sqlite3_uri_parameter() and databaseName().
176919 */
176920 SQLITE_API const char *sqlite3_create_filename(
176921 const char *zDatabase,
176922 const char *zJournal,
176923 const char *zWal,
176924 int nParam,
176925 const char **azParam
@@ -176870,14 +176951,14 @@
176951 /*
176952 ** Free memory obtained from sqlite3_create_filename(). It is a severe
176953 ** error to call this routine with any parameter other than a pointer
176954 ** previously obtained from sqlite3_create_filename() or a NULL pointer.
176955 */
176956 SQLITE_API void sqlite3_free_filename(const char *p){
176957 if( p==0 ) return;
176958 p = databaseName(p);
176959 sqlite3_free((char*)p - 4);
176960 }
176961
176962
176963 /*
176964 ** This is a utility routine, useful to VFS implementations, that checks
@@ -207653,10 +207734,38 @@
207734 #define SQLITE_RBU_STATE_CHECKPOINT 3
207735 #define SQLITE_RBU_STATE_DONE 4
207736 #define SQLITE_RBU_STATE_ERROR 5
207737
207738 SQLITE_API int sqlite3rbu_state(sqlite3rbu *pRbu);
207739
207740 /*
207741 ** As part of applying an RBU update or performing an RBU vacuum operation,
207742 ** the system must at one point move the *-oal file to the equivalent *-wal
207743 ** path. Normally, it does this by invoking POSIX function rename(2) directly.
207744 ** Except on WINCE platforms, where it uses win32 API MoveFileW(). This
207745 ** function may be used to register a callback that the RBU module will invoke
207746 ** instead of one of these APIs.
207747 **
207748 ** If a callback is registered with an RBU handle, it invokes it instead
207749 ** of rename(2) when it needs to move a file within the file-system. The
207750 ** first argument passed to the xRename() callback is a copy of the second
207751 ** argument (pArg) passed to this function. The second is the full path
207752 ** to the file to move and the third the full path to which it should be
207753 ** moved. The callback function should return SQLITE_OK to indicate
207754 ** success. If an error occurs, it should return an SQLite error code.
207755 ** In this case the RBU operation will be abandoned and the error returned
207756 ** to the RBU user.
207757 **
207758 ** Passing a NULL pointer in place of the xRename argument to this function
207759 ** restores the default behaviour.
207760 */
207761 SQLITE_API void sqlite3rbu_rename_handler(
207762 sqlite3rbu *pRbu,
207763 void *pArg,
207764 int (*xRename)(void *pArg, const char *zOld, const char *zNew)
207765 );
207766
207767
207768 /*
207769 ** Create an RBU VFS named zName that accesses the underlying file-system
207770 ** via existing VFS zParent. Or, if the zParent parameter is passed NULL,
207771 ** then the new RBU VFS uses the default system VFS to access the file-system.
@@ -208021,10 +208130,12 @@
208130 const char *zVfsName; /* Name of automatically created rbu vfs */
208131 rbu_file *pTargetFd; /* File handle open on target db */
208132 int nPagePerSector; /* Pages per sector for pTargetFd */
208133 i64 iOalSz;
208134 i64 nPhaseOneStep;
208135 void *pRenameArg;
208136 int (*xRename)(void*, const char*, const char*);
208137
208138 /* The following state variables are used as part of the incremental
208139 ** checkpoint stage (eStage==RBU_STAGE_CKPT). See comments surrounding
208140 ** function rbuSetupCheckpoint() for details. */
208141 u32 iMaxFrame; /* Largest iWalFrame value in aFrame[] */
@@ -210869,36 +210980,11 @@
210980 assert( p->rc==SQLITE_OK );
210981 p->rc = rbuLockDatabase(dbMain);
210982 }
210983
210984 if( p->rc==SQLITE_OK ){
210985 p->rc = p->xRename(p->pRenameArg, zOal, zWal);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
210986 }
210987
210988 if( p->rc!=SQLITE_OK
210989 || rbuIsVacuum(p)
210990 || rbuExclusiveCheckpoint(dbMain)==0
@@ -211633,10 +211719,11 @@
211719 if( p ){
211720 RbuState *pState = 0;
211721
211722 /* Create the custom VFS. */
211723 memset(p, 0, sizeof(sqlite3rbu));
211724 sqlite3rbu_rename_handler(p, 0, 0);
211725 rbuCreateVfs(p);
211726
211727 /* Open the target, RBU and state databases */
211728 if( p->rc==SQLITE_OK ){
211729 char *pCsr = (char*)&p[1];
@@ -212023,10 +212110,58 @@
212110 }
212111
212112 p->rc = rc;
212113 return rc;
212114 }
212115
212116 /*
212117 ** Default xRename callback for RBU.
212118 */
212119 static int xDefaultRename(void *pArg, const char *zOld, const char *zNew){
212120 int rc = SQLITE_OK;
212121 #if defined(_WIN32_WCE)
212122 {
212123 LPWSTR zWideOld;
212124 LPWSTR zWideNew;
212125
212126 zWideOld = rbuWinUtf8ToUnicode(zOld);
212127 if( zWideOld ){
212128 zWideNew = rbuWinUtf8ToUnicode(zNew);
212129 if( zWideNew ){
212130 if( MoveFileW(zWideOld, zWideNew) ){
212131 rc = SQLITE_OK;
212132 }else{
212133 rc = SQLITE_IOERR;
212134 }
212135 sqlite3_free(zWideNew);
212136 }else{
212137 rc = SQLITE_IOERR_NOMEM;
212138 }
212139 sqlite3_free(zWideOld);
212140 }else{
212141 rc = SQLITE_IOERR_NOMEM;
212142 }
212143 }
212144 #else
212145 rc = rename(zOld, zNew) ? SQLITE_IOERR : SQLITE_OK;
212146 #endif
212147 return rc;
212148 }
212149
212150 SQLITE_API void sqlite3rbu_rename_handler(
212151 sqlite3rbu *pRbu,
212152 void *pArg,
212153 int (*xRename)(void *pArg, const char *zOld, const char *zNew)
212154 ){
212155 if( xRename ){
212156 pRbu->xRename = xRename;
212157 pRbu->pRenameArg = pArg;
212158 }else{
212159 pRbu->xRename = xDefaultRename;
212160 pRbu->pRenameArg = 0;
212161 }
212162 }
212163
212164 /**************************************************************************
212165 ** Beginning of RBU VFS shim methods. The VFS shim modifies the behaviour
212166 ** of a standard VFS in the following ways:
212167 **
@@ -214154,25 +214289,31 @@
214289 sqlite3_result_int(ctx, pCsr->pgno);
214290 break;
214291 }
214292 case 1: { /* data */
214293 DbPage *pDbPage = 0;
214294 if( pCsr->pgno==((PENDING_BYTE/pCsr->szPage)+1) ){
214295 /* The pending byte page. Assume it is zeroed out. Attempting to
214296 ** request this page from the page is an SQLITE_CORRUPT error. */
214297 sqlite3_result_zeroblob(ctx, pCsr->szPage);
214298 }else{
214299 rc = sqlite3PagerGet(pCsr->pPager, pCsr->pgno, (DbPage**)&pDbPage, 0);
214300 if( rc==SQLITE_OK ){
214301 sqlite3_result_blob(ctx, sqlite3PagerGetData(pDbPage), pCsr->szPage,
214302 SQLITE_TRANSIENT);
214303 }
214304 sqlite3PagerUnref(pDbPage);
214305 }
 
214306 break;
214307 }
214308 default: { /* schema */
214309 sqlite3 *db = sqlite3_context_db_handle(ctx);
214310 sqlite3_result_text(ctx, db->aDb[pCsr->iDb].zDbSName, -1, SQLITE_STATIC);
214311 break;
214312 }
214313 }
214314 return rc;
214315 }
214316
214317 static int dbpageRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
214318 DbpageCursor *pCsr = (DbpageCursor *)pCursor;
214319 *pRowid = pCsr->pgno;
@@ -214228,15 +214369,16 @@
214369 goto update_fail;
214370 }
214371 pPager = sqlite3BtreePager(pBt);
214372 rc = sqlite3PagerGet(pPager, pgno, (DbPage**)&pDbPage, 0);
214373 if( rc==SQLITE_OK ){
214374 const void *pData = sqlite3_value_blob(argv[3]);
214375 assert( pData!=0 || pTab->db->mallocFailed );
214376 if( pData
214377 && (rc = sqlite3PagerWrite(pDbPage))==SQLITE_OK
214378 ){
214379 memcpy(sqlite3PagerGetData(pDbPage), pData, szPage);
214380 }
214381 }
214382 sqlite3PagerUnref(pDbPage);
214383 return rc;
214384
@@ -238386,11 +238528,11 @@
238528 int nArg, /* Number of args */
238529 sqlite3_value **apUnused /* Function arguments */
238530 ){
238531 assert( nArg==0 );
238532 UNUSED_PARAM2(nArg, apUnused);
238533 sqlite3_result_text(pCtx, "fts5: 2022-11-07 18:36:02 3645585f37631d60cefab1d55cdb1ee060aae87317b9b158a01329ca8a4d3e1e", -1, SQLITE_TRANSIENT);
238534 }
238535
238536 /*
238537 ** Return true if zName is the extension on one of the shadow tables used
238538 ** by this module.
238539
+36 -15
--- extsrc/sqlite3.h
+++ extsrc/sqlite3.h
@@ -146,11 +146,11 @@
146146
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
147147
** [sqlite_version()] and [sqlite_source_id()].
148148
*/
149149
#define SQLITE_VERSION "3.40.0"
150150
#define SQLITE_VERSION_NUMBER 3040000
151
-#define SQLITE_SOURCE_ID "2022-10-26 11:11:31 3dfdfb3f12edb3f4267942598efd05d573e13b7c5d6cdbc3404373f41b8993dd"
151
+#define SQLITE_SOURCE_ID "2022-11-07 19:40:20 55a19677d723147aeb2b4a86bbd01756ddeb2072cba72c3145ad32d335e203b0"
152152
153153
/*
154154
** CAPI3REF: Run-Time Library Version Numbers
155155
** KEYWORDS: sqlite3_version sqlite3_sourceid
156156
**
@@ -1261,10 +1261,30 @@
12611261
** structure must be typedefed in order to work around compiler warnings
12621262
** on some platforms.
12631263
*/
12641264
typedef struct sqlite3_api_routines sqlite3_api_routines;
12651265
1266
+/*
1267
+** CAPI3REF: File Name
1268
+**
1269
+** Type [sqlite3_filename] is used by SQLite to pass filenames to the
1270
+** xOpen method of a [VFS]. It may be cast to (const char*) and treated
1271
+** as a normal, nul-terminated, UTF-8 buffer containing the filename, but
1272
+** may also be passed to special APIs such as:
1273
+**
1274
+** <ul>
1275
+** <li> sqlite3_filename_database()
1276
+** <li> sqlite3_filename_journal()
1277
+** <li> sqlite3_filename_wal()
1278
+** <li> sqlite3_uri_parameter()
1279
+** <li> sqlite3_uri_boolean()
1280
+** <li> sqlite3_uri_int64()
1281
+** <li> sqlite3_uri_key()
1282
+** </ul>
1283
+*/
1284
+typedef const char *sqlite3_filename;
1285
+
12661286
/*
12671287
** CAPI3REF: OS Interface Object
12681288
**
12691289
** An instance of the sqlite3_vfs object defines the interface between
12701290
** the SQLite core and the underlying operating system. The "vfs"
@@ -1439,11 +1459,11 @@
14391459
int szOsFile; /* Size of subclassed sqlite3_file */
14401460
int mxPathname; /* Maximum file pathname length */
14411461
sqlite3_vfs *pNext; /* Next registered VFS */
14421462
const char *zName; /* Name of this virtual file system */
14431463
void *pAppData; /* Pointer to application-specific data */
1444
- int (*xOpen)(sqlite3_vfs*, const char *zName, sqlite3_file*,
1464
+ int (*xOpen)(sqlite3_vfs*, sqlite3_filename zName, sqlite3_file*,
14451465
int flags, int *pOutFlags);
14461466
int (*xDelete)(sqlite3_vfs*, const char *zName, int syncDir);
14471467
int (*xAccess)(sqlite3_vfs*, const char *zName, int flags, int *pResOut);
14481468
int (*xFullPathname)(sqlite3_vfs*, const char *zName, int nOut, char *zOut);
14491469
void *(*xDlOpen)(sqlite3_vfs*, const char *zFilename);
@@ -3709,14 +3729,14 @@
37093729
** it has access to all the same query parameters as were found on the
37103730
** main database file.
37113731
**
37123732
** See the [URI filename] documentation for additional information.
37133733
*/
3714
-SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam);
3715
-SQLITE_API int sqlite3_uri_boolean(const char *zFile, const char *zParam, int bDefault);
3716
-SQLITE_API sqlite3_int64 sqlite3_uri_int64(const char*, const char*, sqlite3_int64);
3717
-SQLITE_API const char *sqlite3_uri_key(const char *zFilename, int N);
3734
+SQLITE_API const char *sqlite3_uri_parameter(sqlite3_filename z, const char *zParam);
3735
+SQLITE_API int sqlite3_uri_boolean(sqlite3_filename z, const char *zParam, int bDefault);
3736
+SQLITE_API sqlite3_int64 sqlite3_uri_int64(sqlite3_filename, const char*, sqlite3_int64);
3737
+SQLITE_API const char *sqlite3_uri_key(sqlite3_filename z, int N);
37183738
37193739
/*
37203740
** CAPI3REF: Translate filenames
37213741
**
37223742
** These routines are available to [VFS|custom VFS implementations] for
@@ -3741,13 +3761,13 @@
37413761
** In all of the above, if F is not the name of a database, journal or WAL
37423762
** filename passed into the VFS from the SQLite core and F is not the
37433763
** return value from [sqlite3_db_filename()], then the result is
37443764
** undefined and is likely a memory access violation.
37453765
*/
3746
-SQLITE_API const char *sqlite3_filename_database(const char*);
3747
-SQLITE_API const char *sqlite3_filename_journal(const char*);
3748
-SQLITE_API const char *sqlite3_filename_wal(const char*);
3766
+SQLITE_API const char *sqlite3_filename_database(sqlite3_filename);
3767
+SQLITE_API const char *sqlite3_filename_journal(sqlite3_filename);
3768
+SQLITE_API const char *sqlite3_filename_wal(sqlite3_filename);
37493769
37503770
/*
37513771
** CAPI3REF: Database File Corresponding To A Journal
37523772
**
37533773
** ^If X is the name of a rollback or WAL-mode journal file that is
@@ -3809,18 +3829,18 @@
38093829
** used again after sqlite3_free_filename(Y) has been called. This means
38103830
** that if the [sqlite3_vfs.xOpen()] method of a VFS has been called using Y,
38113831
** then the corresponding [sqlite3_module.xClose() method should also be
38123832
** invoked prior to calling sqlite3_free_filename(Y).
38133833
*/
3814
-SQLITE_API char *sqlite3_create_filename(
3834
+SQLITE_API sqlite3_filename sqlite3_create_filename(
38153835
const char *zDatabase,
38163836
const char *zJournal,
38173837
const char *zWal,
38183838
int nParam,
38193839
const char **azParam
38203840
);
3821
-SQLITE_API void sqlite3_free_filename(char*);
3841
+SQLITE_API void sqlite3_free_filename(sqlite3_filename);
38223842
38233843
/*
38243844
** CAPI3REF: Error Codes And Messages
38253845
** METHOD: sqlite3
38263846
**
@@ -5852,13 +5872,14 @@
58525872
** application-defined function to be a text string in an encoding
58535873
** specified by the fifth (and last) parameter, which must be one
58545874
** of [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE].
58555875
** ^SQLite takes the text result from the application from
58565876
** the 2nd parameter of the sqlite3_result_text* interfaces.
5857
-** ^If the 3rd parameter to the sqlite3_result_text* interfaces
5858
-** is negative, then SQLite takes result text from the 2nd parameter
5859
-** through the first zero character.
5877
+** ^If the 3rd parameter to any of the sqlite3_result_text* interfaces
5878
+** other than sqlite3_result_text64() is negative, then SQLite computes
5879
+** the string length itself by searching the 2nd parameter for the first
5880
+** zero character.
58605881
** ^If the 3rd parameter to the sqlite3_result_text* interfaces
58615882
** is non-negative, then as many bytes (not characters) of the text
58625883
** pointed to by the 2nd parameter are taken as the application-defined
58635884
** function result. If the 3rd parameter is non-negative, then it
58645885
** must be the byte offset into the string where the NUL terminator would
@@ -6350,11 +6371,11 @@
63506371
** <li> [sqlite3_filename_database()]
63516372
** <li> [sqlite3_filename_journal()]
63526373
** <li> [sqlite3_filename_wal()]
63536374
** </ul>
63546375
*/
6355
-SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName);
6376
+SQLITE_API sqlite3_filename sqlite3_db_filename(sqlite3 *db, const char *zDbName);
63566377
63576378
/*
63586379
** CAPI3REF: Determine if a database is read-only
63596380
** METHOD: sqlite3
63606381
**
63616382
--- extsrc/sqlite3.h
+++ extsrc/sqlite3.h
@@ -146,11 +146,11 @@
146 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
147 ** [sqlite_version()] and [sqlite_source_id()].
148 */
149 #define SQLITE_VERSION "3.40.0"
150 #define SQLITE_VERSION_NUMBER 3040000
151 #define SQLITE_SOURCE_ID "2022-10-26 11:11:31 3dfdfb3f12edb3f4267942598efd05d573e13b7c5d6cdbc3404373f41b8993dd"
152
153 /*
154 ** CAPI3REF: Run-Time Library Version Numbers
155 ** KEYWORDS: sqlite3_version sqlite3_sourceid
156 **
@@ -1261,10 +1261,30 @@
1261 ** structure must be typedefed in order to work around compiler warnings
1262 ** on some platforms.
1263 */
1264 typedef struct sqlite3_api_routines sqlite3_api_routines;
1265
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1266 /*
1267 ** CAPI3REF: OS Interface Object
1268 **
1269 ** An instance of the sqlite3_vfs object defines the interface between
1270 ** the SQLite core and the underlying operating system. The "vfs"
@@ -1439,11 +1459,11 @@
1439 int szOsFile; /* Size of subclassed sqlite3_file */
1440 int mxPathname; /* Maximum file pathname length */
1441 sqlite3_vfs *pNext; /* Next registered VFS */
1442 const char *zName; /* Name of this virtual file system */
1443 void *pAppData; /* Pointer to application-specific data */
1444 int (*xOpen)(sqlite3_vfs*, const char *zName, sqlite3_file*,
1445 int flags, int *pOutFlags);
1446 int (*xDelete)(sqlite3_vfs*, const char *zName, int syncDir);
1447 int (*xAccess)(sqlite3_vfs*, const char *zName, int flags, int *pResOut);
1448 int (*xFullPathname)(sqlite3_vfs*, const char *zName, int nOut, char *zOut);
1449 void *(*xDlOpen)(sqlite3_vfs*, const char *zFilename);
@@ -3709,14 +3729,14 @@
3709 ** it has access to all the same query parameters as were found on the
3710 ** main database file.
3711 **
3712 ** See the [URI filename] documentation for additional information.
3713 */
3714 SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam);
3715 SQLITE_API int sqlite3_uri_boolean(const char *zFile, const char *zParam, int bDefault);
3716 SQLITE_API sqlite3_int64 sqlite3_uri_int64(const char*, const char*, sqlite3_int64);
3717 SQLITE_API const char *sqlite3_uri_key(const char *zFilename, int N);
3718
3719 /*
3720 ** CAPI3REF: Translate filenames
3721 **
3722 ** These routines are available to [VFS|custom VFS implementations] for
@@ -3741,13 +3761,13 @@
3741 ** In all of the above, if F is not the name of a database, journal or WAL
3742 ** filename passed into the VFS from the SQLite core and F is not the
3743 ** return value from [sqlite3_db_filename()], then the result is
3744 ** undefined and is likely a memory access violation.
3745 */
3746 SQLITE_API const char *sqlite3_filename_database(const char*);
3747 SQLITE_API const char *sqlite3_filename_journal(const char*);
3748 SQLITE_API const char *sqlite3_filename_wal(const char*);
3749
3750 /*
3751 ** CAPI3REF: Database File Corresponding To A Journal
3752 **
3753 ** ^If X is the name of a rollback or WAL-mode journal file that is
@@ -3809,18 +3829,18 @@
3809 ** used again after sqlite3_free_filename(Y) has been called. This means
3810 ** that if the [sqlite3_vfs.xOpen()] method of a VFS has been called using Y,
3811 ** then the corresponding [sqlite3_module.xClose() method should also be
3812 ** invoked prior to calling sqlite3_free_filename(Y).
3813 */
3814 SQLITE_API char *sqlite3_create_filename(
3815 const char *zDatabase,
3816 const char *zJournal,
3817 const char *zWal,
3818 int nParam,
3819 const char **azParam
3820 );
3821 SQLITE_API void sqlite3_free_filename(char*);
3822
3823 /*
3824 ** CAPI3REF: Error Codes And Messages
3825 ** METHOD: sqlite3
3826 **
@@ -5852,13 +5872,14 @@
5852 ** application-defined function to be a text string in an encoding
5853 ** specified by the fifth (and last) parameter, which must be one
5854 ** of [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE].
5855 ** ^SQLite takes the text result from the application from
5856 ** the 2nd parameter of the sqlite3_result_text* interfaces.
5857 ** ^If the 3rd parameter to the sqlite3_result_text* interfaces
5858 ** is negative, then SQLite takes result text from the 2nd parameter
5859 ** through the first zero character.
 
5860 ** ^If the 3rd parameter to the sqlite3_result_text* interfaces
5861 ** is non-negative, then as many bytes (not characters) of the text
5862 ** pointed to by the 2nd parameter are taken as the application-defined
5863 ** function result. If the 3rd parameter is non-negative, then it
5864 ** must be the byte offset into the string where the NUL terminator would
@@ -6350,11 +6371,11 @@
6350 ** <li> [sqlite3_filename_database()]
6351 ** <li> [sqlite3_filename_journal()]
6352 ** <li> [sqlite3_filename_wal()]
6353 ** </ul>
6354 */
6355 SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName);
6356
6357 /*
6358 ** CAPI3REF: Determine if a database is read-only
6359 ** METHOD: sqlite3
6360 **
6361
--- extsrc/sqlite3.h
+++ extsrc/sqlite3.h
@@ -146,11 +146,11 @@
146 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
147 ** [sqlite_version()] and [sqlite_source_id()].
148 */
149 #define SQLITE_VERSION "3.40.0"
150 #define SQLITE_VERSION_NUMBER 3040000
151 #define SQLITE_SOURCE_ID "2022-11-07 19:40:20 55a19677d723147aeb2b4a86bbd01756ddeb2072cba72c3145ad32d335e203b0"
152
153 /*
154 ** CAPI3REF: Run-Time Library Version Numbers
155 ** KEYWORDS: sqlite3_version sqlite3_sourceid
156 **
@@ -1261,10 +1261,30 @@
1261 ** structure must be typedefed in order to work around compiler warnings
1262 ** on some platforms.
1263 */
1264 typedef struct sqlite3_api_routines sqlite3_api_routines;
1265
1266 /*
1267 ** CAPI3REF: File Name
1268 **
1269 ** Type [sqlite3_filename] is used by SQLite to pass filenames to the
1270 ** xOpen method of a [VFS]. It may be cast to (const char*) and treated
1271 ** as a normal, nul-terminated, UTF-8 buffer containing the filename, but
1272 ** may also be passed to special APIs such as:
1273 **
1274 ** <ul>
1275 ** <li> sqlite3_filename_database()
1276 ** <li> sqlite3_filename_journal()
1277 ** <li> sqlite3_filename_wal()
1278 ** <li> sqlite3_uri_parameter()
1279 ** <li> sqlite3_uri_boolean()
1280 ** <li> sqlite3_uri_int64()
1281 ** <li> sqlite3_uri_key()
1282 ** </ul>
1283 */
1284 typedef const char *sqlite3_filename;
1285
1286 /*
1287 ** CAPI3REF: OS Interface Object
1288 **
1289 ** An instance of the sqlite3_vfs object defines the interface between
1290 ** the SQLite core and the underlying operating system. The "vfs"
@@ -1439,11 +1459,11 @@
1459 int szOsFile; /* Size of subclassed sqlite3_file */
1460 int mxPathname; /* Maximum file pathname length */
1461 sqlite3_vfs *pNext; /* Next registered VFS */
1462 const char *zName; /* Name of this virtual file system */
1463 void *pAppData; /* Pointer to application-specific data */
1464 int (*xOpen)(sqlite3_vfs*, sqlite3_filename zName, sqlite3_file*,
1465 int flags, int *pOutFlags);
1466 int (*xDelete)(sqlite3_vfs*, const char *zName, int syncDir);
1467 int (*xAccess)(sqlite3_vfs*, const char *zName, int flags, int *pResOut);
1468 int (*xFullPathname)(sqlite3_vfs*, const char *zName, int nOut, char *zOut);
1469 void *(*xDlOpen)(sqlite3_vfs*, const char *zFilename);
@@ -3709,14 +3729,14 @@
3729 ** it has access to all the same query parameters as were found on the
3730 ** main database file.
3731 **
3732 ** See the [URI filename] documentation for additional information.
3733 */
3734 SQLITE_API const char *sqlite3_uri_parameter(sqlite3_filename z, const char *zParam);
3735 SQLITE_API int sqlite3_uri_boolean(sqlite3_filename z, const char *zParam, int bDefault);
3736 SQLITE_API sqlite3_int64 sqlite3_uri_int64(sqlite3_filename, const char*, sqlite3_int64);
3737 SQLITE_API const char *sqlite3_uri_key(sqlite3_filename z, int N);
3738
3739 /*
3740 ** CAPI3REF: Translate filenames
3741 **
3742 ** These routines are available to [VFS|custom VFS implementations] for
@@ -3741,13 +3761,13 @@
3761 ** In all of the above, if F is not the name of a database, journal or WAL
3762 ** filename passed into the VFS from the SQLite core and F is not the
3763 ** return value from [sqlite3_db_filename()], then the result is
3764 ** undefined and is likely a memory access violation.
3765 */
3766 SQLITE_API const char *sqlite3_filename_database(sqlite3_filename);
3767 SQLITE_API const char *sqlite3_filename_journal(sqlite3_filename);
3768 SQLITE_API const char *sqlite3_filename_wal(sqlite3_filename);
3769
3770 /*
3771 ** CAPI3REF: Database File Corresponding To A Journal
3772 **
3773 ** ^If X is the name of a rollback or WAL-mode journal file that is
@@ -3809,18 +3829,18 @@
3829 ** used again after sqlite3_free_filename(Y) has been called. This means
3830 ** that if the [sqlite3_vfs.xOpen()] method of a VFS has been called using Y,
3831 ** then the corresponding [sqlite3_module.xClose() method should also be
3832 ** invoked prior to calling sqlite3_free_filename(Y).
3833 */
3834 SQLITE_API sqlite3_filename sqlite3_create_filename(
3835 const char *zDatabase,
3836 const char *zJournal,
3837 const char *zWal,
3838 int nParam,
3839 const char **azParam
3840 );
3841 SQLITE_API void sqlite3_free_filename(sqlite3_filename);
3842
3843 /*
3844 ** CAPI3REF: Error Codes And Messages
3845 ** METHOD: sqlite3
3846 **
@@ -5852,13 +5872,14 @@
5872 ** application-defined function to be a text string in an encoding
5873 ** specified by the fifth (and last) parameter, which must be one
5874 ** of [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE].
5875 ** ^SQLite takes the text result from the application from
5876 ** the 2nd parameter of the sqlite3_result_text* interfaces.
5877 ** ^If the 3rd parameter to any of the sqlite3_result_text* interfaces
5878 ** other than sqlite3_result_text64() is negative, then SQLite computes
5879 ** the string length itself by searching the 2nd parameter for the first
5880 ** zero character.
5881 ** ^If the 3rd parameter to the sqlite3_result_text* interfaces
5882 ** is non-negative, then as many bytes (not characters) of the text
5883 ** pointed to by the 2nd parameter are taken as the application-defined
5884 ** function result. If the 3rd parameter is non-negative, then it
5885 ** must be the byte offset into the string where the NUL terminator would
@@ -6350,11 +6371,11 @@
6371 ** <li> [sqlite3_filename_database()]
6372 ** <li> [sqlite3_filename_journal()]
6373 ** <li> [sqlite3_filename_wal()]
6374 ** </ul>
6375 */
6376 SQLITE_API sqlite3_filename sqlite3_db_filename(sqlite3 *db, const char *zDbName);
6377
6378 /*
6379 ** CAPI3REF: Determine if a database is read-only
6380 ** METHOD: sqlite3
6381 **
6382
--- src/alerts.c
+++ src/alerts.c
@@ -2932,10 +2932,15 @@
29322932
" sentMod"
29332933
" FROM pending_alert"
29342934
" WHERE sentSep IS FALSE;"
29352935
"DELETE FROM wantalert WHERE needMod AND sentMod;"
29362936
);
2937
+ }
2938
+ if( g.fSqlTrace ){
2939
+ fossil_trace("-- wantalert contains %d rows\n",
2940
+ db_int(0, "SELECT count(*) FROM wantalert")
2941
+ );
29372942
}
29382943
29392944
/* Step 2: compute EmailEvent objects for every notification that
29402945
** needs sending.
29412946
*/
29422947
--- src/alerts.c
+++ src/alerts.c
@@ -2932,10 +2932,15 @@
2932 " sentMod"
2933 " FROM pending_alert"
2934 " WHERE sentSep IS FALSE;"
2935 "DELETE FROM wantalert WHERE needMod AND sentMod;"
2936 );
 
 
 
 
 
2937 }
2938
2939 /* Step 2: compute EmailEvent objects for every notification that
2940 ** needs sending.
2941 */
2942
--- src/alerts.c
+++ src/alerts.c
@@ -2932,10 +2932,15 @@
2932 " sentMod"
2933 " FROM pending_alert"
2934 " WHERE sentSep IS FALSE;"
2935 "DELETE FROM wantalert WHERE needMod AND sentMod;"
2936 );
2937 }
2938 if( g.fSqlTrace ){
2939 fossil_trace("-- wantalert contains %d rows\n",
2940 db_int(0, "SELECT count(*) FROM wantalert")
2941 );
2942 }
2943
2944 /* Step 2: compute EmailEvent objects for every notification that
2945 ** needs sending.
2946 */
2947
--- src/alerts.c
+++ src/alerts.c
@@ -2932,10 +2932,15 @@
29322932
" sentMod"
29332933
" FROM pending_alert"
29342934
" WHERE sentSep IS FALSE;"
29352935
"DELETE FROM wantalert WHERE needMod AND sentMod;"
29362936
);
2937
+ }
2938
+ if( g.fSqlTrace ){
2939
+ fossil_trace("-- wantalert contains %d rows\n",
2940
+ db_int(0, "SELECT count(*) FROM wantalert")
2941
+ );
29372942
}
29382943
29392944
/* Step 2: compute EmailEvent objects for every notification that
29402945
** needs sending.
29412946
*/
29422947
--- src/alerts.c
+++ src/alerts.c
@@ -2932,10 +2932,15 @@
2932 " sentMod"
2933 " FROM pending_alert"
2934 " WHERE sentSep IS FALSE;"
2935 "DELETE FROM wantalert WHERE needMod AND sentMod;"
2936 );
 
 
 
 
 
2937 }
2938
2939 /* Step 2: compute EmailEvent objects for every notification that
2940 ** needs sending.
2941 */
2942
--- src/alerts.c
+++ src/alerts.c
@@ -2932,10 +2932,15 @@
2932 " sentMod"
2933 " FROM pending_alert"
2934 " WHERE sentSep IS FALSE;"
2935 "DELETE FROM wantalert WHERE needMod AND sentMod;"
2936 );
2937 }
2938 if( g.fSqlTrace ){
2939 fossil_trace("-- wantalert contains %d rows\n",
2940 db_int(0, "SELECT count(*) FROM wantalert")
2941 );
2942 }
2943
2944 /* Step 2: compute EmailEvent objects for every notification that
2945 ** needs sending.
2946 */
2947
+1 -1
--- src/info.c
+++ src/info.c
@@ -2747,11 +2747,11 @@
27472747
style_submenu_element("Shun", "%R/shun?shun=%s#addshun", zUuid);
27482748
}
27492749
}
27502750
pTktChng = manifest_get(rid, CFTYPE_TICKET, 0);
27512751
if( pTktChng==0 ) fossil_redirect_home();
2752
- zDate = db_text(0, "SELECT datetime(%.12f)", pTktChng->rDate);
2752
+ zDate = db_text(0, "SELECT datetime(%.12f,toLocal())", pTktChng->rDate);
27532753
sqlite3_snprintf(sizeof(zTktName), zTktName, "%s", pTktChng->zTicketUuid);
27542754
if( g.perm.ModTkt && (zModAction = P("modaction"))!=0 ){
27552755
if( strcmp(zModAction,"delete")==0 ){
27562756
moderation_disapprove(rid);
27572757
/*
27582758
--- src/info.c
+++ src/info.c
@@ -2747,11 +2747,11 @@
2747 style_submenu_element("Shun", "%R/shun?shun=%s#addshun", zUuid);
2748 }
2749 }
2750 pTktChng = manifest_get(rid, CFTYPE_TICKET, 0);
2751 if( pTktChng==0 ) fossil_redirect_home();
2752 zDate = db_text(0, "SELECT datetime(%.12f)", pTktChng->rDate);
2753 sqlite3_snprintf(sizeof(zTktName), zTktName, "%s", pTktChng->zTicketUuid);
2754 if( g.perm.ModTkt && (zModAction = P("modaction"))!=0 ){
2755 if( strcmp(zModAction,"delete")==0 ){
2756 moderation_disapprove(rid);
2757 /*
2758
--- src/info.c
+++ src/info.c
@@ -2747,11 +2747,11 @@
2747 style_submenu_element("Shun", "%R/shun?shun=%s#addshun", zUuid);
2748 }
2749 }
2750 pTktChng = manifest_get(rid, CFTYPE_TICKET, 0);
2751 if( pTktChng==0 ) fossil_redirect_home();
2752 zDate = db_text(0, "SELECT datetime(%.12f,toLocal())", pTktChng->rDate);
2753 sqlite3_snprintf(sizeof(zTktName), zTktName, "%s", pTktChng->zTicketUuid);
2754 if( g.perm.ModTkt && (zModAction = P("modaction"))!=0 ){
2755 if( strcmp(zModAction,"delete")==0 ){
2756 moderation_disapprove(rid);
2757 /*
2758
+1 -1
--- src/info.c
+++ src/info.c
@@ -2747,11 +2747,11 @@
27472747
style_submenu_element("Shun", "%R/shun?shun=%s#addshun", zUuid);
27482748
}
27492749
}
27502750
pTktChng = manifest_get(rid, CFTYPE_TICKET, 0);
27512751
if( pTktChng==0 ) fossil_redirect_home();
2752
- zDate = db_text(0, "SELECT datetime(%.12f)", pTktChng->rDate);
2752
+ zDate = db_text(0, "SELECT datetime(%.12f,toLocal())", pTktChng->rDate);
27532753
sqlite3_snprintf(sizeof(zTktName), zTktName, "%s", pTktChng->zTicketUuid);
27542754
if( g.perm.ModTkt && (zModAction = P("modaction"))!=0 ){
27552755
if( strcmp(zModAction,"delete")==0 ){
27562756
moderation_disapprove(rid);
27572757
/*
27582758
--- src/info.c
+++ src/info.c
@@ -2747,11 +2747,11 @@
2747 style_submenu_element("Shun", "%R/shun?shun=%s#addshun", zUuid);
2748 }
2749 }
2750 pTktChng = manifest_get(rid, CFTYPE_TICKET, 0);
2751 if( pTktChng==0 ) fossil_redirect_home();
2752 zDate = db_text(0, "SELECT datetime(%.12f)", pTktChng->rDate);
2753 sqlite3_snprintf(sizeof(zTktName), zTktName, "%s", pTktChng->zTicketUuid);
2754 if( g.perm.ModTkt && (zModAction = P("modaction"))!=0 ){
2755 if( strcmp(zModAction,"delete")==0 ){
2756 moderation_disapprove(rid);
2757 /*
2758
--- src/info.c
+++ src/info.c
@@ -2747,11 +2747,11 @@
2747 style_submenu_element("Shun", "%R/shun?shun=%s#addshun", zUuid);
2748 }
2749 }
2750 pTktChng = manifest_get(rid, CFTYPE_TICKET, 0);
2751 if( pTktChng==0 ) fossil_redirect_home();
2752 zDate = db_text(0, "SELECT datetime(%.12f,toLocal())", pTktChng->rDate);
2753 sqlite3_snprintf(sizeof(zTktName), zTktName, "%s", pTktChng->zTicketUuid);
2754 if( g.perm.ModTkt && (zModAction = P("modaction"))!=0 ){
2755 if( strcmp(zModAction,"delete")==0 ){
2756 moderation_disapprove(rid);
2757 /*
2758
+19 -6
--- src/manifest.c
+++ src/manifest.c
@@ -2197,16 +2197,29 @@
21972197
blob_appendf(&brief, "New ticket [%!S|%S].", pManifest->zTicketUuid,
21982198
pManifest->zTicketUuid);
21992199
}
22002200
fossil_free(zTitle);
22012201
manifest_create_event_triggers();
2202
- db_multi_exec(
2203
- "REPLACE INTO event(type,tagid,mtime,objid,user,comment,brief)"
2204
- "VALUES('t',%d,%.17g,%d,%Q,%Q,%Q)",
2205
- tktTagId, pManifest->rDate, rid, pManifest->zUser,
2206
- blob_str(&comment), blob_str(&brief)
2207
- );
2202
+ if( db_exists("SELECT 1 FROM event WHERE type='t' AND objid=%d", rid) ){
2203
+ /* The ticket_rebuild_entry() function redoes all of the event entries
2204
+ ** for a ticket whenever a new event appears. Be careful to only UPDATE
2205
+ ** existing events, so that they do not get turned into alerts by
2206
+ ** the alert trigger. */
2207
+ db_multi_exec(
2208
+ "UPDATE event SET tagid=%d, mtime=%.17g, user=%Q, comment=%Q, brief=%Q"
2209
+ " WHERE objid=%d",
2210
+ tktTagId, pManifest->rDate, pManifest->zUser,
2211
+ blob_str(&comment), blob_str(&brief), rid
2212
+ );
2213
+ }else{
2214
+ db_multi_exec(
2215
+ "REPLACE INTO event(type,tagid,mtime,objid,user,comment,brief)"
2216
+ "VALUES('t',%d,%.17g,%d,%Q,%Q,%Q)",
2217
+ tktTagId, pManifest->rDate, rid, pManifest->zUser,
2218
+ blob_str(&comment), blob_str(&brief)
2219
+ );
2220
+ }
22082221
blob_reset(&comment);
22092222
blob_reset(&brief);
22102223
}
22112224
22122225
/*
22132226
--- src/manifest.c
+++ src/manifest.c
@@ -2197,16 +2197,29 @@
2197 blob_appendf(&brief, "New ticket [%!S|%S].", pManifest->zTicketUuid,
2198 pManifest->zTicketUuid);
2199 }
2200 fossil_free(zTitle);
2201 manifest_create_event_triggers();
2202 db_multi_exec(
2203 "REPLACE INTO event(type,tagid,mtime,objid,user,comment,brief)"
2204 "VALUES('t',%d,%.17g,%d,%Q,%Q,%Q)",
2205 tktTagId, pManifest->rDate, rid, pManifest->zUser,
2206 blob_str(&comment), blob_str(&brief)
2207 );
 
 
 
 
 
 
 
 
 
 
 
 
 
2208 blob_reset(&comment);
2209 blob_reset(&brief);
2210 }
2211
2212 /*
2213
--- src/manifest.c
+++ src/manifest.c
@@ -2197,16 +2197,29 @@
2197 blob_appendf(&brief, "New ticket [%!S|%S].", pManifest->zTicketUuid,
2198 pManifest->zTicketUuid);
2199 }
2200 fossil_free(zTitle);
2201 manifest_create_event_triggers();
2202 if( db_exists("SELECT 1 FROM event WHERE type='t' AND objid=%d", rid) ){
2203 /* The ticket_rebuild_entry() function redoes all of the event entries
2204 ** for a ticket whenever a new event appears. Be careful to only UPDATE
2205 ** existing events, so that they do not get turned into alerts by
2206 ** the alert trigger. */
2207 db_multi_exec(
2208 "UPDATE event SET tagid=%d, mtime=%.17g, user=%Q, comment=%Q, brief=%Q"
2209 " WHERE objid=%d",
2210 tktTagId, pManifest->rDate, pManifest->zUser,
2211 blob_str(&comment), blob_str(&brief), rid
2212 );
2213 }else{
2214 db_multi_exec(
2215 "REPLACE INTO event(type,tagid,mtime,objid,user,comment,brief)"
2216 "VALUES('t',%d,%.17g,%d,%Q,%Q,%Q)",
2217 tktTagId, pManifest->rDate, rid, pManifest->zUser,
2218 blob_str(&comment), blob_str(&brief)
2219 );
2220 }
2221 blob_reset(&comment);
2222 blob_reset(&brief);
2223 }
2224
2225 /*
2226
+19 -6
--- src/manifest.c
+++ src/manifest.c
@@ -2197,16 +2197,29 @@
21972197
blob_appendf(&brief, "New ticket [%!S|%S].", pManifest->zTicketUuid,
21982198
pManifest->zTicketUuid);
21992199
}
22002200
fossil_free(zTitle);
22012201
manifest_create_event_triggers();
2202
- db_multi_exec(
2203
- "REPLACE INTO event(type,tagid,mtime,objid,user,comment,brief)"
2204
- "VALUES('t',%d,%.17g,%d,%Q,%Q,%Q)",
2205
- tktTagId, pManifest->rDate, rid, pManifest->zUser,
2206
- blob_str(&comment), blob_str(&brief)
2207
- );
2202
+ if( db_exists("SELECT 1 FROM event WHERE type='t' AND objid=%d", rid) ){
2203
+ /* The ticket_rebuild_entry() function redoes all of the event entries
2204
+ ** for a ticket whenever a new event appears. Be careful to only UPDATE
2205
+ ** existing events, so that they do not get turned into alerts by
2206
+ ** the alert trigger. */
2207
+ db_multi_exec(
2208
+ "UPDATE event SET tagid=%d, mtime=%.17g, user=%Q, comment=%Q, brief=%Q"
2209
+ " WHERE objid=%d",
2210
+ tktTagId, pManifest->rDate, pManifest->zUser,
2211
+ blob_str(&comment), blob_str(&brief), rid
2212
+ );
2213
+ }else{
2214
+ db_multi_exec(
2215
+ "REPLACE INTO event(type,tagid,mtime,objid,user,comment,brief)"
2216
+ "VALUES('t',%d,%.17g,%d,%Q,%Q,%Q)",
2217
+ tktTagId, pManifest->rDate, rid, pManifest->zUser,
2218
+ blob_str(&comment), blob_str(&brief)
2219
+ );
2220
+ }
22082221
blob_reset(&comment);
22092222
blob_reset(&brief);
22102223
}
22112224
22122225
/*
22132226
--- src/manifest.c
+++ src/manifest.c
@@ -2197,16 +2197,29 @@
2197 blob_appendf(&brief, "New ticket [%!S|%S].", pManifest->zTicketUuid,
2198 pManifest->zTicketUuid);
2199 }
2200 fossil_free(zTitle);
2201 manifest_create_event_triggers();
2202 db_multi_exec(
2203 "REPLACE INTO event(type,tagid,mtime,objid,user,comment,brief)"
2204 "VALUES('t',%d,%.17g,%d,%Q,%Q,%Q)",
2205 tktTagId, pManifest->rDate, rid, pManifest->zUser,
2206 blob_str(&comment), blob_str(&brief)
2207 );
 
 
 
 
 
 
 
 
 
 
 
 
 
2208 blob_reset(&comment);
2209 blob_reset(&brief);
2210 }
2211
2212 /*
2213
--- src/manifest.c
+++ src/manifest.c
@@ -2197,16 +2197,29 @@
2197 blob_appendf(&brief, "New ticket [%!S|%S].", pManifest->zTicketUuid,
2198 pManifest->zTicketUuid);
2199 }
2200 fossil_free(zTitle);
2201 manifest_create_event_triggers();
2202 if( db_exists("SELECT 1 FROM event WHERE type='t' AND objid=%d", rid) ){
2203 /* The ticket_rebuild_entry() function redoes all of the event entries
2204 ** for a ticket whenever a new event appears. Be careful to only UPDATE
2205 ** existing events, so that they do not get turned into alerts by
2206 ** the alert trigger. */
2207 db_multi_exec(
2208 "UPDATE event SET tagid=%d, mtime=%.17g, user=%Q, comment=%Q, brief=%Q"
2209 " WHERE objid=%d",
2210 tktTagId, pManifest->rDate, pManifest->zUser,
2211 blob_str(&comment), blob_str(&brief), rid
2212 );
2213 }else{
2214 db_multi_exec(
2215 "REPLACE INTO event(type,tagid,mtime,objid,user,comment,brief)"
2216 "VALUES('t',%d,%.17g,%d,%Q,%Q,%Q)",
2217 tktTagId, pManifest->rDate, rid, pManifest->zUser,
2218 blob_str(&comment), blob_str(&brief)
2219 );
2220 }
2221 blob_reset(&comment);
2222 blob_reset(&brief);
2223 }
2224
2225 /*
2226
+13 -4
--- src/th.c
+++ src/th.c
@@ -1750,12 +1750,12 @@
17501750
int nElem /* Length of nElem */
17511751
){
17521752
Buffer output;
17531753
int i;
17541754
1755
- int hasSpecialChar = 0;
1756
- int hasEscapeChar = 0;
1755
+ int hasSpecialChar = 0; /* Whitespace or {}[]'" */
1756
+ int hasEscapeChar = 0; /* '}' without matching '{' to the left or a '\\' */
17571757
int nBrace = 0;
17581758
17591759
output.zBuf = *pzList;
17601760
output.nBuf = *pnList;
17611761
output.nBufAlloc = output.nBuf;
@@ -1768,13 +1768,22 @@
17681768
}
17691769
17701770
for(i=0; i<nElem; i++){
17711771
char c = zElem[i];
17721772
if( th_isspecial(c) ) hasSpecialChar = 1;
1773
- if( c=='\\' ) hasEscapeChar = 1;
1773
+ if( c=='\\' ){ hasEscapeChar = 1; break; }
17741774
if( c=='{' ) nBrace++;
1775
- if( c=='}' ) nBrace--;
1775
+ if( c=='}' ){
1776
+ if( nBrace==0 ){
1777
+ /* A closing brace that does not have a matching open brace to
1778
+ ** its left needs to be excaped. See ticket 4d73b4a2258a78e2 */
1779
+ hasEscapeChar = 1;
1780
+ break;
1781
+ }else{
1782
+ nBrace--;
1783
+ }
1784
+ }
17761785
}
17771786
17781787
if( nElem==0 || (!hasEscapeChar && hasSpecialChar && nBrace==0) ){
17791788
thBufferAddChar(interp, &output, '{');
17801789
thBufferWrite(interp, &output, zElem, nElem);
17811790
--- src/th.c
+++ src/th.c
@@ -1750,12 +1750,12 @@
1750 int nElem /* Length of nElem */
1751 ){
1752 Buffer output;
1753 int i;
1754
1755 int hasSpecialChar = 0;
1756 int hasEscapeChar = 0;
1757 int nBrace = 0;
1758
1759 output.zBuf = *pzList;
1760 output.nBuf = *pnList;
1761 output.nBufAlloc = output.nBuf;
@@ -1768,13 +1768,22 @@
1768 }
1769
1770 for(i=0; i<nElem; i++){
1771 char c = zElem[i];
1772 if( th_isspecial(c) ) hasSpecialChar = 1;
1773 if( c=='\\' ) hasEscapeChar = 1;
1774 if( c=='{' ) nBrace++;
1775 if( c=='}' ) nBrace--;
 
 
 
 
 
 
 
 
 
1776 }
1777
1778 if( nElem==0 || (!hasEscapeChar && hasSpecialChar && nBrace==0) ){
1779 thBufferAddChar(interp, &output, '{');
1780 thBufferWrite(interp, &output, zElem, nElem);
1781
--- src/th.c
+++ src/th.c
@@ -1750,12 +1750,12 @@
1750 int nElem /* Length of nElem */
1751 ){
1752 Buffer output;
1753 int i;
1754
1755 int hasSpecialChar = 0; /* Whitespace or {}[]'" */
1756 int hasEscapeChar = 0; /* '}' without matching '{' to the left or a '\\' */
1757 int nBrace = 0;
1758
1759 output.zBuf = *pzList;
1760 output.nBuf = *pnList;
1761 output.nBufAlloc = output.nBuf;
@@ -1768,13 +1768,22 @@
1768 }
1769
1770 for(i=0; i<nElem; i++){
1771 char c = zElem[i];
1772 if( th_isspecial(c) ) hasSpecialChar = 1;
1773 if( c=='\\' ){ hasEscapeChar = 1; break; }
1774 if( c=='{' ) nBrace++;
1775 if( c=='}' ){
1776 if( nBrace==0 ){
1777 /* A closing brace that does not have a matching open brace to
1778 ** its left needs to be excaped. See ticket 4d73b4a2258a78e2 */
1779 hasEscapeChar = 1;
1780 break;
1781 }else{
1782 nBrace--;
1783 }
1784 }
1785 }
1786
1787 if( nElem==0 || (!hasEscapeChar && hasSpecialChar && nBrace==0) ){
1788 thBufferAddChar(interp, &output, '{');
1789 thBufferWrite(interp, &output, zElem, nElem);
1790

Keyboard Shortcuts

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