Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
| Comment: | Update the built-in SQLite to the latest 3.40.0 beta, for testing. |
|---|---|
| Downloads: | Tarball | ZIP archive |
| Timelines: | family | ancestors | descendants | both | trunk |
| Files: | files | file ages | folders |
| SHA3-256: |
10d71748479b884c6b17706f9a1c4875 |
| User & Date: | drh 2022-11-04 18:02:25.360 |
References
|
2022-11-11
| ||
| 13:33 | • New ticket [ad9aa572e7] Multiple notifications to a single ticket change. artifact: ce00ac53ba user: drh | |
Context
|
2022-11-05
| ||
| 22:10 | Respect `"timeline-utc"` setting when a ticket change artifact is displayed on the <tt>/info</tt> page. This makes it consistent with other places and fixes a hyperlink to the corresponding item of the <tt>/timeline</tt>. check-in: 0028a3ca6a user: george tags: trunk | |
|
2022-11-04
| ||
| 18:02 | Update the built-in SQLite to the latest 3.40.0 beta, for testing. check-in: 10d7174847 user: drh tags: trunk | |
|
2022-10-28
| ||
| 19:48 | Also documented the new "clone -u -v" feature. check-in: 0d61fd2310 user: wyoung tags: trunk | |
Changes
Changes to extsrc/shell.c.
| ︙ | ︙ | |||
563 564 565 566 567 568 569 570 571 572 573 574 575 576 |
** in bytes. This is different from the %*.*s specification in printf
** since with %*.*s the width is measured in bytes, not characters.
*/
static void utf8_width_print(FILE *pOut, int w, const char *zUtf){
int i;
int n;
int aw = w<0 ? -w : w;
for(i=n=0; zUtf[i]; i++){
if( (zUtf[i]&0xc0)!=0x80 ){
n++;
if( n==aw ){
do{ i++; }while( (zUtf[i]&0xc0)==0x80 );
break;
}
| > | 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 |
** in bytes. This is different from the %*.*s specification in printf
** since with %*.*s the width is measured in bytes, not characters.
*/
static void utf8_width_print(FILE *pOut, int w, const char *zUtf){
int i;
int n;
int aw = w<0 ? -w : w;
if( zUtf==0 ) zUtf = "";
for(i=n=0; zUtf[i]; i++){
if( (zUtf[i]&0xc0)!=0x80 ){
n++;
if( n==aw ){
do{ i++; }while( (zUtf[i]&0xc0)==0x80 );
break;
}
|
| ︙ | ︙ | |||
11400 11401 11402 11403 11404 11405 11406 | } #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */ /************************* End ../ext/expert/sqlite3expert.c ********************/ #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) | > > > > > | | 11401 11402 11403 11404 11405 11406 11407 11408 11409 11410 11411 11412 11413 11414 11415 11416 11417 11418 11419 11420 | } #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */ /************************* End ../ext/expert/sqlite3expert.c ********************/ #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) #define SQLITE_SHELL_HAVE_RECOVER 1 #else #define SQLITE_SHELL_HAVE_RECOVER 0 #endif #if SQLITE_SHELL_HAVE_RECOVER /************************* Begin ../ext/recover/dbdata.c ******************/ /* ** 2019-04-17 ** ** The author disclaims copyright to this source code. In place of ** a legal notice, here is a blessing: ** ** May you do good and not evil. |
| ︙ | ︙ | |||
11474 11475 11476 11477 11478 11479 11480 11481 11482 11483 11484 11485 11486 11487 11488 11489 11490 11491 11492 11493 11494 11495 11496 | ** child INTEGER, ** schema TEXT HIDDEN ** ); ** ** It contains one entry for each b-tree pointer between a parent and ** child page in the database. */ #if !defined(SQLITEINT_H) /* #include "sqlite3ext.h" */ /* typedef unsigned char u8; */ #endif SQLITE_EXTENSION_INIT1 #include <string.h> #include <assert.h> #define DBDATA_PADDING_BYTES 100 typedef struct DbdataTable DbdataTable; typedef struct DbdataCursor DbdataCursor; /* Cursor object */ | > > > > | 11480 11481 11482 11483 11484 11485 11486 11487 11488 11489 11490 11491 11492 11493 11494 11495 11496 11497 11498 11499 11500 11501 11502 11503 11504 11505 11506 | ** child INTEGER, ** schema TEXT HIDDEN ** ); ** ** It contains one entry for each b-tree pointer between a parent and ** child page in the database. */ #if !defined(SQLITEINT_H) /* #include "sqlite3ext.h" */ /* typedef unsigned char u8; */ typedef unsigned int u32; #endif SQLITE_EXTENSION_INIT1 #include <string.h> #include <assert.h> #ifndef SQLITE_OMIT_VIRTUALTABLE #define DBDATA_PADDING_BYTES 100 typedef struct DbdataTable DbdataTable; typedef struct DbdataCursor DbdataCursor; /* Cursor object */ |
| ︙ | ︙ | |||
11505 11506 11507 11508 11509 11510 11511 | int iCell; /* Current cell number */ int bOnePage; /* True to stop after one page */ int szDb; sqlite3_int64 iRowid; /* Only for the sqlite_dbdata table */ u8 *pRec; /* Buffer containing current record */ | | | > | 11515 11516 11517 11518 11519 11520 11521 11522 11523 11524 11525 11526 11527 11528 11529 11530 11531 11532 11533 11534 |
int iCell; /* Current cell number */
int bOnePage; /* True to stop after one page */
int szDb;
sqlite3_int64 iRowid;
/* Only for the sqlite_dbdata table */
u8 *pRec; /* Buffer containing current record */
sqlite3_int64 nRec; /* Size of pRec[] in bytes */
sqlite3_int64 nHdr; /* Size of header in bytes */
int iField; /* Current field number */
u8 *pHdrPtr;
u8 *pPtr;
u32 enc; /* Text encoding */
sqlite3_int64 iIntkey; /* Integer key value */
};
/* Table object */
struct DbdataTable {
sqlite3_vtab base; /* Base class. Must be first */
|
| ︙ | ︙ | |||
11702 11703 11704 11705 11706 11707 11708 | sqlite3_free(pCsr); return SQLITE_OK; } /* ** Utility methods to decode 16 and 32-bit big-endian unsigned integers. */ | | | | | | | | > | | | | | | | | | | | | | | | | | | | > | | | | | > > > > > > > > > > > > > | 11713 11714 11715 11716 11717 11718 11719 11720 11721 11722 11723 11724 11725 11726 11727 11728 11729 11730 11731 11732 11733 11734 11735 11736 11737 11738 11739 11740 11741 11742 11743 11744 11745 11746 11747 11748 11749 11750 11751 11752 11753 11754 11755 11756 11757 11758 11759 11760 11761 11762 11763 11764 11765 11766 11767 11768 11769 11770 11771 11772 11773 11774 11775 11776 11777 11778 11779 11780 11781 11782 11783 11784 11785 11786 11787 11788 11789 11790 11791 11792 11793 11794 11795 11796 11797 11798 11799 11800 11801 11802 11803 11804 11805 11806 11807 11808 11809 11810 |
sqlite3_free(pCsr);
return SQLITE_OK;
}
/*
** Utility methods to decode 16 and 32-bit big-endian unsigned integers.
*/
static u32 get_uint16(unsigned char *a){
return (a[0]<<8)|a[1];
}
static u32 get_uint32(unsigned char *a){
return ((u32)a[0]<<24)
| ((u32)a[1]<<16)
| ((u32)a[2]<<8)
| ((u32)a[3]);
}
/*
** Load page pgno from the database via the sqlite_dbpage virtual table.
** If successful, set (*ppPage) to point to a buffer containing the page
** data, (*pnPage) to the size of that buffer in bytes and return
** SQLITE_OK. In this case it is the responsibility of the caller to
** eventually free the buffer using sqlite3_free().
**
** Or, if an error occurs, set both (*ppPage) and (*pnPage) to 0 and
** return an SQLite error code.
*/
static int dbdataLoadPage(
DbdataCursor *pCsr, /* Cursor object */
u32 pgno, /* Page number of page to load */
u8 **ppPage, /* OUT: pointer to page buffer */
int *pnPage /* OUT: Size of (*ppPage) in bytes */
){
int rc2;
int rc = SQLITE_OK;
sqlite3_stmt *pStmt = pCsr->pStmt;
*ppPage = 0;
*pnPage = 0;
if( pgno>0 ){
sqlite3_bind_int64(pStmt, 2, pgno);
if( SQLITE_ROW==sqlite3_step(pStmt) ){
int nCopy = sqlite3_column_bytes(pStmt, 0);
if( nCopy>0 ){
u8 *pPage;
pPage = (u8*)sqlite3_malloc64(nCopy + DBDATA_PADDING_BYTES);
if( pPage==0 ){
rc = SQLITE_NOMEM;
}else{
const u8 *pCopy = sqlite3_column_blob(pStmt, 0);
memcpy(pPage, pCopy, nCopy);
memset(&pPage[nCopy], 0, DBDATA_PADDING_BYTES);
}
*ppPage = pPage;
*pnPage = nCopy;
}
}
rc2 = sqlite3_reset(pStmt);
if( rc==SQLITE_OK ) rc = rc2;
}
return rc;
}
/*
** Read a varint. Put the value in *pVal and return the number of bytes.
*/
static int dbdataGetVarint(const u8 *z, sqlite3_int64 *pVal){
sqlite3_uint64 u = 0;
int i;
for(i=0; i<8; i++){
u = (u<<7) + (z[i]&0x7f);
if( (z[i]&0x80)==0 ){ *pVal = (sqlite3_int64)u; return i+1; }
}
u = (u<<8) + (z[i]&0xff);
*pVal = (sqlite3_int64)u;
return 9;
}
/*
** Like dbdataGetVarint(), but set the output to 0 if it is less than 0
** or greater than 0xFFFFFFFF. This can be used for all varints in an
** SQLite database except for key values in intkey tables.
*/
static int dbdataGetVarintU32(const u8 *z, sqlite3_int64 *pVal){
sqlite3_int64 val;
int nRet = dbdataGetVarint(z, &val);
if( val<0 || val>0xFFFFFFFF ) val = 0;
*pVal = val;
return nRet;
}
/*
** Return the number of bytes of space used by an SQLite value of type
** eType.
*/
static int dbdataValueBytes(int eType){
switch( eType ){
|
| ︙ | ︙ | |||
11808 11809 11810 11811 11812 11813 11814 11815 11816 11817 11818 11819 11820 11821 |
/*
** Load a value of type eType from buffer pData and use it to set the
** result of context object pCtx.
*/
static void dbdataValue(
sqlite3_context *pCtx,
int eType,
u8 *pData,
int nData
){
if( eType>=0 && dbdataValueBytes(eType)<=nData ){
switch( eType ){
case 0:
| > | 11834 11835 11836 11837 11838 11839 11840 11841 11842 11843 11844 11845 11846 11847 11848 |
/*
** Load a value of type eType from buffer pData and use it to set the
** result of context object pCtx.
*/
static void dbdataValue(
sqlite3_context *pCtx,
u32 enc,
int eType,
u8 *pData,
int nData
){
if( eType>=0 && dbdataValueBytes(eType)<=nData ){
switch( eType ){
case 0:
|
| ︙ | ︙ | |||
11852 11853 11854 11855 11856 11857 11858 |
}
break;
}
default: {
int n = ((eType-12) / 2);
if( eType % 2 ){
| > > > > > > > > > > | > > | 11879 11880 11881 11882 11883 11884 11885 11886 11887 11888 11889 11890 11891 11892 11893 11894 11895 11896 11897 11898 11899 11900 11901 11902 11903 11904 11905 |
}
break;
}
default: {
int n = ((eType-12) / 2);
if( eType % 2 ){
switch( enc ){
#ifndef SQLITE_OMIT_UTF16
case SQLITE_UTF16BE:
sqlite3_result_text16be(pCtx, (void*)pData, n, SQLITE_TRANSIENT);
break;
case SQLITE_UTF16LE:
sqlite3_result_text16le(pCtx, (void*)pData, n, SQLITE_TRANSIENT);
break;
#endif
default:
sqlite3_result_text(pCtx, (char*)pData, n, SQLITE_TRANSIENT);
break;
}
}else{
sqlite3_result_blob(pCtx, pData, n, SQLITE_TRANSIENT);
}
}
}
}
}
|
| ︙ | ︙ | |||
11880 11881 11882 11883 11884 11885 11886 11887 11888 11889 11890 11891 11892 11893 |
if( pCsr->aPage==0 ){
while( 1 ){
if( pCsr->bOnePage==0 && pCsr->iPgno>pCsr->szDb ) return SQLITE_OK;
rc = dbdataLoadPage(pCsr, pCsr->iPgno, &pCsr->aPage, &pCsr->nPage);
if( rc!=SQLITE_OK ) return rc;
if( pCsr->aPage ) break;
pCsr->iPgno++;
}
pCsr->iCell = pTab->bPtr ? -2 : 0;
pCsr->nCell = get_uint16(&pCsr->aPage[iOff+3]);
}
if( pTab->bPtr ){
| > | 11919 11920 11921 11922 11923 11924 11925 11926 11927 11928 11929 11930 11931 11932 11933 |
if( pCsr->aPage==0 ){
while( 1 ){
if( pCsr->bOnePage==0 && pCsr->iPgno>pCsr->szDb ) return SQLITE_OK;
rc = dbdataLoadPage(pCsr, pCsr->iPgno, &pCsr->aPage, &pCsr->nPage);
if( rc!=SQLITE_OK ) return rc;
if( pCsr->aPage ) break;
if( pCsr->bOnePage ) return SQLITE_OK;
pCsr->iPgno++;
}
pCsr->iCell = pTab->bPtr ? -2 : 0;
pCsr->nCell = get_uint16(&pCsr->aPage[iOff+3]);
}
if( pTab->bPtr ){
|
| ︙ | ︙ | |||
11943 11944 11945 11946 11947 11948 11949 |
/* For an interior node cell, skip past the child-page number */
iOff += nPointer;
/* Load the "byte of payload including overflow" field */
if( bNextPage || iOff>pCsr->nPage ){
bNextPage = 1;
}else{
| | | 11983 11984 11985 11986 11987 11988 11989 11990 11991 11992 11993 11994 11995 11996 11997 |
/* For an interior node cell, skip past the child-page number */
iOff += nPointer;
/* Load the "byte of payload including overflow" field */
if( bNextPage || iOff>pCsr->nPage ){
bNextPage = 1;
}else{
iOff += dbdataGetVarintU32(&pCsr->aPage[iOff], &nPayload);
}
/* If this is a leaf intkey cell, load the rowid */
if( bHasRowid && !bNextPage && iOff<pCsr->nPage ){
iOff += dbdataGetVarint(&pCsr->aPage[iOff], &pCsr->iIntkey);
}
|
| ︙ | ︙ | |||
11990 11991 11992 11993 11994 11995 11996 |
/* Load the nLocal bytes of payload */
memcpy(pCsr->pRec, &pCsr->aPage[iOff], nLocal);
iOff += nLocal;
/* Load content from overflow pages */
if( nPayload>nLocal ){
sqlite3_int64 nRem = nPayload - nLocal;
| | | > | | 12030 12031 12032 12033 12034 12035 12036 12037 12038 12039 12040 12041 12042 12043 12044 12045 12046 12047 12048 12049 12050 12051 12052 12053 12054 12055 12056 12057 12058 12059 12060 12061 12062 12063 12064 12065 12066 12067 12068 12069 12070 12071 12072 12073 12074 12075 12076 12077 12078 12079 |
/* Load the nLocal bytes of payload */
memcpy(pCsr->pRec, &pCsr->aPage[iOff], nLocal);
iOff += nLocal;
/* Load content from overflow pages */
if( nPayload>nLocal ){
sqlite3_int64 nRem = nPayload - nLocal;
u32 pgnoOvfl = get_uint32(&pCsr->aPage[iOff]);
while( nRem>0 ){
u8 *aOvfl = 0;
int nOvfl = 0;
int nCopy;
rc = dbdataLoadPage(pCsr, pgnoOvfl, &aOvfl, &nOvfl);
assert( rc!=SQLITE_OK || aOvfl==0 || nOvfl==pCsr->nPage );
if( rc!=SQLITE_OK ) return rc;
if( aOvfl==0 ) break;
nCopy = U-4;
if( nCopy>nRem ) nCopy = nRem;
memcpy(&pCsr->pRec[nPayload-nRem], &aOvfl[4], nCopy);
nRem -= nCopy;
pgnoOvfl = get_uint32(aOvfl);
sqlite3_free(aOvfl);
}
}
iHdr = dbdataGetVarintU32(pCsr->pRec, &nHdr);
if( nHdr>nPayload ) nHdr = 0;
pCsr->nHdr = nHdr;
pCsr->pHdrPtr = &pCsr->pRec[iHdr];
pCsr->pPtr = &pCsr->pRec[pCsr->nHdr];
pCsr->iField = (bHasRowid ? -1 : 0);
}
}
}else{
pCsr->iField++;
if( pCsr->iField>0 ){
sqlite3_int64 iType;
if( pCsr->pHdrPtr>&pCsr->pRec[pCsr->nRec] ){
bNextPage = 1;
}else{
pCsr->pHdrPtr += dbdataGetVarintU32(pCsr->pHdrPtr, &iType);
pCsr->pPtr += dbdataValueBytes(iType);
}
}
}
if( bNextPage ){
sqlite3_free(pCsr->aPage);
|
| ︙ | ︙ | |||
12062 12063 12064 12065 12066 12067 12068 12069 12070 12071 12072 12073 12074 12075 12076 12077 12078 12079 12080 12081 |
/*
** Return true if the cursor is at EOF.
*/
static int dbdataEof(sqlite3_vtab_cursor *pCursor){
DbdataCursor *pCsr = (DbdataCursor*)pCursor;
return pCsr->aPage==0;
}
/*
** Determine the size in pages of database zSchema (where zSchema is
** "main", "temp" or the name of an attached database) and set
** pCsr->szDb accordingly. If successful, return SQLITE_OK. Otherwise,
** an SQLite error code.
*/
static int dbdataDbsize(DbdataCursor *pCsr, const char *zSchema){
DbdataTable *pTab = (DbdataTable*)pCsr->base.pVtab;
char *zSql = 0;
int rc, rc2;
sqlite3_stmt *pStmt = 0;
| > > > > > > > > > > > > > > > > | > > > > > > > > > > > > > > > > > > > > > > < > > > > > > > > > > > > > > > > | | 12103 12104 12105 12106 12107 12108 12109 12110 12111 12112 12113 12114 12115 12116 12117 12118 12119 12120 12121 12122 12123 12124 12125 12126 12127 12128 12129 12130 12131 12132 12133 12134 12135 12136 12137 12138 12139 12140 12141 12142 12143 12144 12145 12146 12147 12148 12149 12150 12151 12152 12153 12154 12155 12156 12157 12158 12159 12160 12161 12162 12163 12164 12165 12166 12167 12168 12169 12170 12171 12172 12173 12174 12175 12176 12177 12178 12179 12180 12181 12182 12183 12184 12185 12186 12187 12188 12189 12190 12191 12192 12193 12194 12195 12196 12197 12198 12199 12200 12201 12202 12203 12204 12205 12206 12207 12208 12209 12210 12211 12212 12213 12214 12215 12216 12217 12218 12219 12220 12221 12222 12223 12224 12225 12226 12227 12228 12229 12230 12231 12232 12233 12234 12235 12236 12237 12238 12239 12240 12241 12242 12243 |
/*
** Return true if the cursor is at EOF.
*/
static int dbdataEof(sqlite3_vtab_cursor *pCursor){
DbdataCursor *pCsr = (DbdataCursor*)pCursor;
return pCsr->aPage==0;
}
/*
** Return true if nul-terminated string zSchema ends in "()". Or false
** otherwise.
*/
static int dbdataIsFunction(const char *zSchema){
size_t n = strlen(zSchema);
if( n>2 && zSchema[n-2]=='(' && zSchema[n-1]==')' ){
return (int)n-2;
}
return 0;
}
/*
** Determine the size in pages of database zSchema (where zSchema is
** "main", "temp" or the name of an attached database) and set
** pCsr->szDb accordingly. If successful, return SQLITE_OK. Otherwise,
** an SQLite error code.
*/
static int dbdataDbsize(DbdataCursor *pCsr, const char *zSchema){
DbdataTable *pTab = (DbdataTable*)pCsr->base.pVtab;
char *zSql = 0;
int rc, rc2;
int nFunc = 0;
sqlite3_stmt *pStmt = 0;
if( (nFunc = dbdataIsFunction(zSchema))>0 ){
zSql = sqlite3_mprintf("SELECT %.*s(0)", nFunc, zSchema);
}else{
zSql = sqlite3_mprintf("PRAGMA %Q.page_count", zSchema);
}
if( zSql==0 ) return SQLITE_NOMEM;
rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pStmt, 0);
sqlite3_free(zSql);
if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
pCsr->szDb = sqlite3_column_int(pStmt, 0);
}
rc2 = sqlite3_finalize(pStmt);
if( rc==SQLITE_OK ) rc = rc2;
return rc;
}
/*
** Attempt to figure out the encoding of the database by retrieving page 1
** and inspecting the header field. If successful, set the pCsr->enc variable
** and return SQLITE_OK. Otherwise, return an SQLite error code.
*/
static int dbdataGetEncoding(DbdataCursor *pCsr){
int rc = SQLITE_OK;
int nPg1 = 0;
u8 *aPg1 = 0;
rc = dbdataLoadPage(pCsr, 1, &aPg1, &nPg1);
assert( rc!=SQLITE_OK || nPg1==0 || nPg1>=512 );
if( rc==SQLITE_OK && nPg1>0 ){
pCsr->enc = get_uint32(&aPg1[56]);
}
sqlite3_free(aPg1);
return rc;
}
/*
** xFilter method for sqlite_dbdata and sqlite_dbptr.
*/
static int dbdataFilter(
sqlite3_vtab_cursor *pCursor,
int idxNum, const char *idxStr,
int argc, sqlite3_value **argv
){
DbdataCursor *pCsr = (DbdataCursor*)pCursor;
DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
int rc = SQLITE_OK;
const char *zSchema = "main";
dbdataResetCursor(pCsr);
assert( pCsr->iPgno==1 );
if( idxNum & 0x01 ){
zSchema = (const char*)sqlite3_value_text(argv[0]);
if( zSchema==0 ) zSchema = "";
}
if( idxNum & 0x02 ){
pCsr->iPgno = sqlite3_value_int(argv[(idxNum & 0x01)]);
pCsr->bOnePage = 1;
}else{
rc = dbdataDbsize(pCsr, zSchema);
}
if( rc==SQLITE_OK ){
int nFunc = 0;
if( pTab->pStmt ){
pCsr->pStmt = pTab->pStmt;
pTab->pStmt = 0;
}else if( (nFunc = dbdataIsFunction(zSchema))>0 ){
char *zSql = sqlite3_mprintf("SELECT %.*s(?2)", nFunc, zSchema);
if( zSql==0 ){
rc = SQLITE_NOMEM;
}else{
rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pStmt, 0);
sqlite3_free(zSql);
}
}else{
rc = sqlite3_prepare_v2(pTab->db,
"SELECT data FROM sqlite_dbpage(?) WHERE pgno=?", -1,
&pCsr->pStmt, 0
);
}
}
if( rc==SQLITE_OK ){
rc = sqlite3_bind_text(pCsr->pStmt, 1, zSchema, -1, SQLITE_TRANSIENT);
}else{
pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
}
/* Try to determine the encoding of the db by inspecting the header
** field on page 1. */
if( rc==SQLITE_OK ){
rc = dbdataGetEncoding(pCsr);
}
if( rc==SQLITE_OK ){
rc = dbdataNext(pCursor);
}
return rc;
}
/*
** Return a column for the sqlite_dbdata or sqlite_dbptr table.
*/
static int dbdataColumn(
sqlite3_vtab_cursor *pCursor,
sqlite3_context *ctx,
int i
){
|
| ︙ | ︙ | |||
12181 12182 12183 12184 12185 12186 12187 |
sqlite3_result_int(ctx, pCsr->iField);
break;
case DBDATA_COLUMN_VALUE: {
if( pCsr->iField<0 ){
sqlite3_result_int64(ctx, pCsr->iIntkey);
}else{
sqlite3_int64 iType;
| | > | | 12275 12276 12277 12278 12279 12280 12281 12282 12283 12284 12285 12286 12287 12288 12289 12290 12291 12292 |
sqlite3_result_int(ctx, pCsr->iField);
break;
case DBDATA_COLUMN_VALUE: {
if( pCsr->iField<0 ){
sqlite3_result_int64(ctx, pCsr->iIntkey);
}else{
sqlite3_int64 iType;
dbdataGetVarintU32(pCsr->pHdrPtr, &iType);
dbdataValue(
ctx, pCsr->enc, iType, pCsr->pPtr,
&pCsr->pRec[pCsr->nRec] - pCsr->pPtr
);
}
break;
}
}
}
return SQLITE_OK;
|
| ︙ | ︙ | |||
12253 12254 12255 12256 12257 12258 12259 |
char **pzErrMsg,
const sqlite3_api_routines *pApi
){
SQLITE_EXTENSION_INIT2(pApi);
return sqlite3DbdataRegister(db);
}
| > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | | 12348 12349 12350 12351 12352 12353 12354 12355 12356 12357 12358 12359 12360 12361 12362 12363 12364 12365 12366 12367 12368 12369 12370 12371 12372 12373 12374 12375 12376 12377 12378 12379 12380 12381 12382 12383 12384 12385 12386 12387 12388 12389 12390 12391 12392 12393 12394 12395 12396 12397 12398 12399 12400 12401 12402 12403 12404 12405 12406 12407 12408 12409 12410 12411 12412 12413 12414 12415 12416 12417 12418 12419 12420 12421 12422 12423 12424 12425 12426 12427 12428 12429 12430 12431 12432 12433 12434 12435 12436 12437 12438 12439 12440 12441 12442 12443 12444 12445 12446 12447 12448 12449 12450 12451 12452 12453 12454 12455 12456 12457 12458 12459 12460 12461 12462 12463 12464 12465 12466 12467 12468 12469 12470 12471 12472 12473 12474 12475 12476 12477 12478 12479 12480 12481 12482 12483 12484 12485 12486 12487 12488 12489 12490 12491 12492 12493 12494 12495 12496 12497 12498 12499 12500 12501 12502 12503 12504 12505 12506 12507 12508 12509 12510 12511 12512 12513 12514 12515 12516 12517 12518 12519 12520 12521 12522 12523 12524 12525 12526 12527 12528 12529 12530 12531 12532 12533 12534 12535 12536 12537 12538 12539 12540 12541 12542 12543 12544 12545 12546 12547 12548 12549 12550 12551 12552 12553 12554 12555 12556 12557 12558 12559 12560 12561 12562 12563 12564 12565 12566 12567 12568 12569 12570 12571 12572 12573 12574 12575 12576 12577 12578 12579 12580 12581 12582 12583 12584 12585 12586 12587 12588 12589 12590 12591 12592 12593 12594 12595 12596 12597 12598 12599 12600 12601 12602 12603 12604 12605 12606 12607 12608 12609 12610 12611 12612 12613 12614 12615 12616 12617 12618 12619 12620 12621 12622 12623 12624 12625 12626 12627 12628 12629 12630 12631 12632 12633 12634 12635 12636 12637 12638 12639 12640 12641 12642 12643 12644 12645 12646 12647 12648 12649 12650 12651 12652 12653 12654 12655 12656 12657 12658 12659 12660 12661 12662 12663 12664 12665 12666 12667 12668 12669 12670 12671 12672 12673 12674 12675 12676 12677 12678 12679 12680 12681 12682 12683 12684 12685 12686 12687 12688 12689 12690 12691 12692 12693 12694 12695 12696 12697 12698 12699 12700 12701 12702 12703 12704 12705 12706 12707 12708 12709 12710 12711 12712 12713 12714 12715 12716 12717 12718 12719 12720 12721 12722 12723 12724 12725 12726 12727 12728 12729 12730 12731 12732 12733 12734 12735 12736 12737 12738 12739 12740 12741 12742 12743 12744 12745 12746 12747 12748 12749 12750 12751 12752 12753 12754 12755 12756 12757 12758 12759 12760 12761 12762 12763 12764 12765 12766 12767 12768 12769 12770 12771 12772 12773 12774 12775 12776 12777 12778 12779 12780 12781 12782 12783 12784 12785 12786 12787 12788 12789 12790 12791 12792 12793 12794 12795 12796 12797 12798 12799 12800 12801 12802 12803 12804 12805 12806 12807 12808 12809 12810 12811 12812 12813 12814 12815 12816 12817 12818 12819 12820 12821 12822 12823 12824 12825 12826 12827 12828 12829 12830 12831 12832 12833 12834 12835 12836 12837 12838 12839 12840 12841 12842 12843 12844 12845 12846 12847 12848 12849 12850 12851 12852 12853 12854 12855 12856 12857 12858 12859 12860 12861 12862 12863 12864 12865 12866 12867 12868 12869 12870 12871 12872 12873 12874 12875 12876 12877 12878 12879 12880 12881 12882 12883 12884 12885 12886 12887 12888 12889 12890 12891 12892 12893 12894 12895 12896 12897 12898 12899 12900 12901 12902 12903 12904 12905 12906 12907 12908 12909 12910 12911 12912 12913 12914 12915 12916 12917 12918 12919 12920 12921 12922 12923 12924 12925 12926 12927 12928 12929 12930 12931 12932 12933 12934 12935 12936 12937 12938 12939 12940 12941 12942 12943 12944 12945 12946 12947 12948 12949 12950 12951 12952 12953 12954 12955 12956 12957 12958 12959 12960 12961 12962 12963 12964 12965 12966 12967 12968 12969 12970 12971 12972 12973 12974 12975 12976 12977 12978 12979 12980 12981 12982 12983 12984 12985 12986 12987 12988 12989 12990 12991 12992 12993 12994 12995 12996 12997 12998 12999 13000 13001 13002 13003 13004 13005 13006 13007 13008 13009 13010 13011 13012 13013 13014 13015 13016 13017 13018 13019 13020 13021 13022 13023 13024 13025 13026 13027 13028 13029 13030 13031 13032 13033 13034 13035 13036 13037 13038 13039 13040 13041 13042 13043 13044 13045 13046 13047 13048 13049 13050 13051 13052 13053 13054 13055 13056 13057 13058 13059 13060 13061 13062 13063 13064 13065 13066 13067 13068 13069 13070 13071 13072 13073 13074 13075 13076 13077 13078 13079 13080 13081 13082 13083 13084 13085 13086 13087 13088 13089 13090 13091 13092 13093 13094 13095 13096 13097 13098 13099 13100 13101 13102 13103 13104 13105 13106 13107 13108 13109 13110 13111 13112 13113 13114 13115 13116 13117 13118 13119 13120 13121 13122 13123 13124 13125 13126 13127 13128 13129 13130 13131 13132 13133 13134 13135 13136 13137 13138 13139 13140 13141 13142 13143 13144 13145 13146 13147 13148 13149 13150 13151 13152 13153 13154 13155 13156 13157 13158 13159 13160 13161 13162 13163 13164 13165 13166 13167 13168 13169 13170 13171 13172 13173 13174 13175 13176 13177 13178 13179 13180 13181 13182 13183 13184 13185 13186 13187 13188 13189 13190 13191 13192 13193 13194 13195 13196 13197 13198 13199 13200 13201 13202 13203 13204 13205 13206 13207 13208 13209 13210 13211 13212 13213 13214 13215 13216 13217 13218 13219 13220 13221 13222 13223 13224 13225 13226 13227 13228 13229 13230 13231 13232 13233 13234 13235 13236 13237 13238 13239 13240 13241 13242 13243 13244 13245 13246 13247 13248 13249 13250 13251 13252 13253 13254 13255 13256 13257 13258 13259 13260 13261 13262 13263 13264 13265 13266 13267 13268 13269 13270 13271 13272 13273 13274 13275 13276 13277 13278 13279 13280 13281 13282 13283 13284 13285 13286 13287 13288 13289 13290 13291 13292 13293 13294 13295 13296 13297 13298 13299 13300 13301 13302 13303 13304 13305 13306 13307 13308 13309 13310 13311 13312 13313 13314 13315 13316 13317 13318 13319 13320 13321 13322 13323 13324 13325 13326 13327 13328 13329 13330 13331 13332 13333 13334 13335 13336 13337 13338 13339 13340 13341 13342 13343 13344 13345 13346 13347 13348 13349 13350 13351 13352 13353 13354 13355 13356 13357 13358 13359 13360 13361 13362 13363 13364 13365 13366 13367 13368 13369 13370 13371 13372 13373 13374 13375 13376 13377 13378 13379 13380 13381 13382 13383 13384 13385 13386 13387 13388 13389 13390 13391 13392 13393 13394 13395 13396 13397 13398 13399 13400 13401 13402 13403 13404 13405 13406 13407 13408 13409 13410 13411 13412 13413 13414 13415 13416 13417 13418 13419 13420 13421 13422 13423 13424 13425 13426 13427 13428 13429 13430 13431 13432 13433 13434 13435 13436 13437 13438 13439 13440 13441 13442 13443 13444 13445 13446 13447 13448 13449 13450 13451 13452 13453 13454 13455 13456 13457 13458 13459 13460 13461 13462 13463 13464 13465 13466 13467 13468 13469 13470 13471 13472 13473 13474 13475 13476 13477 13478 13479 13480 13481 13482 13483 13484 13485 13486 13487 13488 13489 13490 13491 13492 13493 13494 13495 13496 13497 13498 13499 13500 13501 13502 13503 13504 13505 13506 13507 13508 13509 13510 13511 13512 13513 13514 13515 13516 13517 13518 13519 13520 13521 13522 13523 13524 13525 13526 13527 13528 13529 13530 13531 13532 13533 13534 13535 13536 13537 13538 13539 13540 13541 13542 13543 13544 13545 13546 13547 13548 13549 13550 13551 13552 13553 13554 13555 13556 13557 13558 13559 13560 13561 13562 13563 13564 13565 13566 13567 13568 13569 13570 13571 13572 13573 13574 13575 13576 13577 13578 13579 13580 13581 13582 13583 13584 13585 13586 13587 13588 13589 13590 13591 13592 13593 13594 13595 13596 13597 13598 13599 13600 13601 13602 13603 13604 13605 13606 13607 13608 13609 13610 13611 13612 13613 13614 13615 13616 13617 13618 13619 13620 13621 13622 13623 13624 13625 13626 13627 13628 13629 13630 13631 13632 13633 13634 13635 13636 13637 13638 13639 13640 13641 13642 13643 13644 13645 13646 13647 13648 13649 13650 13651 13652 13653 13654 13655 13656 13657 13658 13659 13660 13661 13662 13663 13664 13665 13666 13667 13668 13669 13670 13671 13672 13673 13674 13675 13676 13677 13678 13679 13680 13681 13682 13683 13684 13685 13686 13687 13688 13689 13690 13691 13692 13693 13694 13695 13696 13697 13698 13699 13700 13701 13702 13703 13704 13705 13706 13707 13708 13709 13710 13711 13712 13713 13714 13715 13716 13717 13718 13719 13720 13721 13722 13723 13724 13725 13726 13727 13728 13729 13730 13731 13732 13733 13734 13735 13736 13737 13738 13739 13740 13741 13742 13743 13744 13745 13746 13747 13748 13749 13750 13751 13752 13753 13754 13755 13756 13757 13758 13759 13760 13761 13762 13763 13764 13765 13766 13767 13768 13769 13770 13771 13772 13773 13774 13775 13776 13777 13778 13779 13780 13781 13782 13783 13784 13785 13786 13787 13788 13789 13790 13791 13792 13793 13794 13795 13796 13797 13798 13799 13800 13801 13802 13803 13804 13805 13806 13807 13808 13809 13810 13811 13812 13813 13814 13815 13816 13817 13818 13819 13820 13821 13822 13823 13824 13825 13826 13827 13828 13829 13830 13831 13832 13833 13834 13835 13836 13837 13838 13839 13840 13841 13842 13843 13844 13845 13846 13847 13848 13849 13850 13851 13852 13853 13854 13855 13856 13857 13858 13859 13860 13861 13862 13863 13864 13865 13866 13867 13868 13869 13870 13871 13872 13873 13874 13875 13876 13877 13878 13879 13880 13881 13882 13883 13884 13885 13886 13887 13888 13889 13890 13891 13892 13893 13894 13895 13896 13897 13898 13899 13900 13901 13902 13903 13904 13905 13906 13907 13908 13909 13910 13911 13912 13913 13914 13915 13916 13917 13918 13919 13920 13921 13922 13923 13924 13925 13926 13927 13928 13929 13930 13931 13932 13933 13934 13935 13936 13937 13938 13939 13940 13941 13942 13943 13944 13945 13946 13947 13948 13949 13950 13951 13952 13953 13954 13955 13956 13957 13958 13959 13960 13961 13962 13963 13964 13965 13966 13967 13968 13969 13970 13971 13972 13973 13974 13975 13976 13977 13978 13979 13980 13981 13982 13983 13984 13985 13986 13987 13988 13989 13990 13991 13992 13993 13994 13995 13996 13997 13998 13999 14000 14001 14002 14003 14004 14005 14006 14007 14008 14009 14010 14011 14012 14013 14014 14015 14016 14017 14018 14019 14020 14021 14022 14023 14024 14025 14026 14027 14028 14029 14030 14031 14032 14033 14034 14035 14036 14037 14038 14039 14040 14041 14042 14043 14044 14045 14046 14047 14048 14049 14050 14051 14052 14053 14054 14055 14056 14057 14058 14059 14060 14061 14062 14063 14064 14065 14066 14067 14068 14069 14070 14071 14072 14073 14074 14075 14076 14077 14078 14079 14080 14081 14082 14083 14084 14085 14086 14087 14088 14089 14090 14091 14092 14093 14094 14095 14096 14097 14098 14099 14100 14101 14102 14103 14104 14105 14106 14107 14108 14109 14110 14111 14112 14113 14114 14115 14116 14117 14118 14119 14120 14121 14122 14123 14124 14125 14126 14127 14128 14129 14130 14131 14132 14133 14134 14135 14136 14137 14138 14139 14140 14141 14142 14143 14144 14145 14146 14147 14148 14149 14150 14151 14152 14153 14154 14155 14156 14157 14158 14159 14160 14161 14162 14163 14164 14165 14166 14167 14168 14169 14170 14171 14172 14173 14174 14175 14176 14177 14178 14179 14180 14181 14182 14183 14184 14185 14186 14187 14188 14189 14190 14191 14192 14193 14194 14195 14196 14197 14198 14199 14200 14201 14202 14203 14204 14205 14206 14207 14208 14209 14210 14211 14212 14213 14214 14215 14216 14217 14218 14219 14220 14221 14222 14223 14224 14225 14226 14227 14228 14229 14230 14231 14232 14233 14234 14235 14236 14237 14238 14239 14240 14241 14242 14243 14244 14245 14246 14247 14248 14249 14250 14251 14252 14253 14254 14255 14256 14257 14258 14259 14260 14261 14262 14263 14264 14265 14266 14267 14268 14269 14270 14271 14272 14273 14274 14275 14276 14277 14278 14279 14280 14281 14282 14283 14284 14285 14286 14287 14288 14289 14290 14291 14292 14293 14294 14295 14296 14297 14298 14299 14300 14301 14302 14303 14304 14305 14306 14307 14308 14309 14310 14311 14312 14313 14314 14315 14316 14317 14318 14319 14320 14321 14322 14323 14324 14325 14326 14327 14328 14329 14330 14331 14332 14333 14334 14335 14336 14337 14338 14339 14340 14341 14342 14343 14344 14345 14346 14347 14348 14349 14350 14351 14352 14353 14354 14355 14356 14357 14358 14359 14360 14361 14362 14363 14364 14365 14366 14367 14368 14369 14370 14371 14372 14373 14374 14375 14376 14377 14378 14379 14380 14381 14382 14383 14384 14385 14386 14387 14388 14389 14390 14391 14392 14393 14394 14395 14396 14397 14398 14399 14400 14401 14402 14403 14404 14405 14406 14407 14408 14409 14410 14411 14412 14413 14414 14415 14416 14417 14418 14419 14420 14421 14422 14423 14424 14425 14426 14427 14428 14429 14430 14431 14432 14433 14434 14435 14436 14437 14438 14439 14440 14441 14442 14443 14444 14445 14446 14447 14448 14449 14450 14451 14452 14453 14454 14455 14456 14457 14458 14459 14460 14461 14462 14463 14464 14465 14466 14467 14468 14469 14470 14471 14472 14473 14474 14475 14476 14477 14478 14479 14480 14481 14482 14483 14484 14485 14486 14487 14488 14489 14490 14491 14492 14493 14494 14495 14496 14497 14498 14499 14500 14501 14502 14503 14504 14505 14506 14507 14508 14509 14510 14511 14512 14513 14514 14515 14516 14517 14518 14519 14520 14521 14522 14523 14524 14525 14526 14527 14528 14529 14530 14531 14532 14533 14534 14535 14536 14537 14538 14539 14540 14541 14542 14543 14544 14545 14546 14547 14548 14549 14550 14551 14552 14553 14554 14555 14556 14557 14558 14559 14560 14561 14562 14563 14564 14565 14566 14567 14568 14569 14570 14571 14572 14573 14574 14575 14576 14577 14578 14579 14580 14581 14582 14583 14584 14585 14586 14587 14588 14589 14590 14591 14592 14593 14594 14595 14596 14597 14598 14599 14600 14601 14602 14603 14604 14605 14606 14607 14608 14609 14610 14611 14612 14613 14614 14615 14616 14617 14618 14619 14620 14621 14622 14623 14624 14625 14626 14627 14628 14629 14630 14631 14632 14633 14634 14635 14636 14637 14638 14639 14640 14641 14642 14643 14644 14645 14646 14647 14648 14649 14650 14651 14652 14653 14654 14655 14656 14657 14658 14659 14660 14661 14662 14663 14664 14665 14666 14667 14668 14669 14670 14671 14672 14673 14674 14675 14676 14677 14678 14679 14680 14681 14682 14683 14684 14685 14686 14687 14688 14689 14690 14691 14692 14693 14694 14695 14696 14697 14698 14699 14700 14701 14702 14703 14704 14705 14706 14707 14708 14709 14710 14711 14712 14713 14714 14715 14716 14717 14718 14719 14720 14721 14722 14723 14724 14725 14726 14727 14728 14729 14730 14731 14732 14733 14734 14735 14736 14737 14738 14739 14740 14741 14742 14743 14744 14745 14746 14747 14748 14749 14750 14751 14752 14753 14754 14755 14756 14757 14758 14759 14760 14761 14762 14763 14764 14765 14766 14767 14768 14769 14770 14771 14772 14773 14774 14775 14776 14777 14778 14779 14780 14781 14782 14783 14784 14785 14786 14787 14788 14789 14790 14791 14792 14793 14794 14795 14796 14797 14798 14799 14800 14801 14802 14803 14804 14805 14806 14807 14808 14809 14810 14811 14812 14813 14814 14815 14816 14817 14818 14819 14820 14821 14822 14823 14824 14825 14826 14827 14828 14829 14830 14831 14832 14833 14834 14835 14836 14837 14838 14839 14840 14841 14842 14843 14844 14845 14846 14847 14848 14849 14850 14851 14852 14853 14854 14855 14856 14857 14858 14859 14860 14861 14862 14863 14864 14865 14866 14867 14868 14869 14870 14871 14872 14873 14874 14875 14876 14877 14878 14879 14880 14881 14882 14883 14884 14885 14886 14887 14888 14889 14890 14891 14892 14893 14894 14895 14896 14897 14898 14899 14900 14901 14902 14903 14904 14905 14906 14907 14908 14909 14910 14911 14912 14913 14914 14915 14916 14917 14918 14919 14920 14921 14922 14923 14924 14925 14926 14927 14928 14929 14930 14931 14932 14933 14934 14935 14936 14937 14938 14939 14940 14941 14942 14943 14944 14945 14946 14947 14948 14949 14950 14951 14952 14953 14954 14955 14956 14957 14958 14959 14960 14961 14962 14963 14964 14965 14966 14967 14968 14969 14970 14971 14972 14973 14974 14975 14976 14977 14978 14979 14980 14981 14982 14983 14984 14985 14986 14987 14988 14989 14990 14991 14992 14993 14994 14995 14996 14997 14998 14999 15000 15001 15002 15003 15004 15005 15006 15007 15008 15009 15010 15011 15012 15013 15014 15015 15016 15017 15018 15019 15020 15021 15022 15023 15024 15025 15026 15027 15028 15029 15030 15031 15032 15033 15034 15035 15036 15037 15038 15039 15040 15041 15042 15043 15044 15045 15046 15047 15048 15049 15050 15051 15052 15053 15054 15055 15056 15057 15058 15059 15060 15061 15062 15063 15064 15065 15066 15067 15068 15069 15070 15071 15072 15073 15074 15075 15076 15077 15078 15079 15080 15081 15082 15083 15084 15085 15086 15087 15088 15089 15090 15091 15092 15093 15094 15095 15096 15097 15098 15099 15100 15101 15102 15103 15104 15105 15106 15107 15108 15109 15110 15111 15112 15113 15114 15115 15116 15117 15118 15119 15120 15121 15122 15123 15124 15125 15126 15127 15128 15129 15130 15131 15132 15133 15134 15135 15136 15137 15138 15139 15140 15141 15142 15143 15144 15145 15146 15147 15148 15149 15150 15151 15152 15153 15154 15155 15156 15157 15158 15159 15160 15161 15162 15163 15164 15165 15166 15167 15168 15169 15170 15171 15172 15173 15174 15175 15176 15177 15178 15179 15180 15181 15182 15183 15184 15185 15186 15187 15188 15189 15190 15191 15192 15193 15194 15195 15196 15197 15198 15199 15200 15201 15202 15203 15204 15205 15206 15207 15208 15209 15210 15211 15212 15213 15214 15215 15216 15217 15218 15219 15220 15221 15222 15223 15224 15225 15226 15227 15228 15229 15230 15231 15232 15233 15234 15235 15236 15237 15238 15239 15240 15241 15242 15243 15244 15245 15246 15247 15248 15249 15250 15251 15252 15253 15254 15255 15256 15257 15258 15259 15260 15261 15262 15263 15264 15265 15266 15267 15268 15269 15270 15271 15272 15273 15274 15275 15276 15277 15278 15279 15280 15281 15282 15283 15284 15285 15286 15287 15288 15289 15290 15291 15292 15293 15294 15295 15296 15297 15298 15299 15300 15301 15302 15303 15304 15305 15306 15307 15308 15309 15310 15311 15312 15313 15314 15315 15316 15317 15318 15319 15320 15321 15322 15323 15324 15325 15326 15327 15328 15329 15330 15331 15332 15333 15334 15335 15336 15337 15338 15339 15340 15341 15342 15343 15344 15345 15346 15347 15348 15349 15350 15351 15352 15353 15354 15355 15356 15357 15358 15359 15360 15361 15362 15363 15364 15365 15366 15367 15368 15369 15370 15371 15372 15373 15374 15375 15376 15377 15378 15379 15380 15381 15382 15383 15384 15385 15386 15387 15388 15389 15390 15391 15392 15393 15394 15395 15396 15397 15398 15399 15400 15401 15402 15403 15404 15405 15406 15407 15408 15409 15410 15411 15412 15413 15414 15415 15416 15417 15418 15419 15420 15421 15422 15423 15424 15425 15426 15427 15428 15429 15430 15431 15432 15433 15434 15435 15436 15437 15438 15439 15440 15441 15442 15443 15444 15445 15446 15447 15448 15449 15450 15451 15452 15453 15454 15455 15456 15457 15458 15459 15460 15461 15462 15463 15464 15465 15466 15467 15468 15469 15470 |
char **pzErrMsg,
const sqlite3_api_routines *pApi
){
SQLITE_EXTENSION_INIT2(pApi);
return sqlite3DbdataRegister(db);
}
#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
/************************* End ../ext/recover/dbdata.c ********************/
/************************* Begin ../ext/recover/sqlite3recover.h ******************/
/*
** 2022-08-27
**
** The author disclaims copyright to this source code. In place of
** a legal notice, here is a blessing:
**
** May you do good and not evil.
** May you find forgiveness for yourself and forgive others.
** May you share freely, never taking more than you give.
**
*************************************************************************
**
** This file contains the public interface to the "recover" extension -
** an SQLite extension designed to recover data from corrupted database
** files.
*/
/*
** OVERVIEW:
**
** To use the API to recover data from a corrupted database, an
** application:
**
** 1) Creates an sqlite3_recover handle by calling either
** sqlite3_recover_init() or sqlite3_recover_init_sql().
**
** 2) Configures the new handle using one or more calls to
** sqlite3_recover_config().
**
** 3) Executes the recovery by repeatedly calling sqlite3_recover_step() on
** the handle until it returns something other than SQLITE_OK. If it
** returns SQLITE_DONE, then the recovery operation completed without
** error. If it returns some other non-SQLITE_OK value, then an error
** has occurred.
**
** 4) Retrieves any error code and English language error message using the
** sqlite3_recover_errcode() and sqlite3_recover_errmsg() APIs,
** respectively.
**
** 5) Destroys the sqlite3_recover handle and frees all resources
** using sqlite3_recover_finish().
**
** The application may abandon the recovery operation at any point
** before it is finished by passing the sqlite3_recover handle to
** sqlite3_recover_finish(). This is not an error, but the final state
** of the output database, or the results of running the partial script
** delivered to the SQL callback, are undefined.
*/
#ifndef _SQLITE_RECOVER_H
#define _SQLITE_RECOVER_H
/* #include "sqlite3.h" */
#ifdef __cplusplus
extern "C" {
#endif
/*
** An instance of the sqlite3_recover object represents a recovery
** operation in progress.
**
** Constructors:
**
** sqlite3_recover_init()
** sqlite3_recover_init_sql()
**
** Destructor:
**
** sqlite3_recover_finish()
**
** Methods:
**
** sqlite3_recover_config()
** sqlite3_recover_errcode()
** sqlite3_recover_errmsg()
** sqlite3_recover_run()
** sqlite3_recover_step()
*/
typedef struct sqlite3_recover sqlite3_recover;
/*
** These two APIs attempt to create and return a new sqlite3_recover object.
** In both cases the first two arguments identify the (possibly
** corrupt) database to recover data from. The first argument is an open
** database handle and the second the name of a database attached to that
** handle (i.e. "main", "temp" or the name of an attached database).
**
** If sqlite3_recover_init() is used to create the new sqlite3_recover
** handle, then data is recovered into a new database, identified by
** string parameter zUri. zUri may be an absolute or relative file path,
** or may be an SQLite URI. If the identified database file already exists,
** it is overwritten.
**
** If sqlite3_recover_init_sql() is invoked, then any recovered data will
** be returned to the user as a series of SQL statements. Executing these
** SQL statements results in the same database as would have been created
** had sqlite3_recover_init() been used. For each SQL statement in the
** output, the callback function passed as the third argument (xSql) is
** invoked once. The first parameter is a passed a copy of the fourth argument
** to this function (pCtx) as its first parameter, and a pointer to a
** nul-terminated buffer containing the SQL statement formated as UTF-8 as
** the second. If the xSql callback returns any value other than SQLITE_OK,
** then processing is immediately abandoned and the value returned used as
** the recover handle error code (see below).
**
** If an out-of-memory error occurs, NULL may be returned instead of
** a valid handle. In all other cases, it is the responsibility of the
** application to avoid resource leaks by ensuring that
** sqlite3_recover_finish() is called on all allocated handles.
*/
sqlite3_recover *sqlite3_recover_init(
sqlite3* db,
const char *zDb,
const char *zUri
);
sqlite3_recover *sqlite3_recover_init_sql(
sqlite3* db,
const char *zDb,
int (*xSql)(void*, const char*),
void *pCtx
);
/*
** Configure an sqlite3_recover object that has just been created using
** sqlite3_recover_init() or sqlite3_recover_init_sql(). This function
** may only be called before the first call to sqlite3_recover_step()
** or sqlite3_recover_run() on the object.
**
** The second argument passed to this function must be one of the
** SQLITE_RECOVER_* symbols defined below. Valid values for the third argument
** depend on the specific SQLITE_RECOVER_* symbol in use.
**
** SQLITE_OK is returned if the configuration operation was successful,
** or an SQLite error code otherwise.
*/
int sqlite3_recover_config(sqlite3_recover*, int op, void *pArg);
/*
** SQLITE_RECOVER_LOST_AND_FOUND:
** The pArg argument points to a string buffer containing the name
** of a "lost-and-found" table in the output database, or NULL. If
** the argument is non-NULL and the database contains seemingly
** valid pages that cannot be associated with any table in the
** recovered part of the schema, data is extracted from these
** pages to add to the lost-and-found table.
**
** SQLITE_RECOVER_FREELIST_CORRUPT:
** The pArg value must actually be a pointer to a value of type
** int containing value 0 or 1 cast as a (void*). If this option is set
** (argument is 1) and a lost-and-found table has been configured using
** SQLITE_RECOVER_LOST_AND_FOUND, then is assumed that the freelist is
** corrupt and an attempt is made to recover records from pages that
** appear to be linked into the freelist. Otherwise, pages on the freelist
** are ignored. Setting this option can recover more data from the
** database, but often ends up "recovering" deleted records. The default
** value is 0 (clear).
**
** SQLITE_RECOVER_ROWIDS:
** The pArg value must actually be a pointer to a value of type
** int containing value 0 or 1 cast as a (void*). If this option is set
** (argument is 1), then an attempt is made to recover rowid values
** that are not also INTEGER PRIMARY KEY values. If this option is
** clear, then new rowids are assigned to all recovered rows. The
** default value is 1 (set).
**
** SQLITE_RECOVER_SLOWINDEXES:
** The pArg value must actually be a pointer to a value of type
** int containing value 0 or 1 cast as a (void*). If this option is clear
** (argument is 0), then when creating an output database, the recover
** module creates and populates non-UNIQUE indexes right at the end of the
** recovery operation - after all recoverable data has been inserted
** into the new database. This is faster overall, but means that the
** final call to sqlite3_recover_step() for a recovery operation may
** be need to create a large number of indexes, which may be very slow.
**
** Or, if this option is set (argument is 1), then non-UNIQUE indexes
** are created in the output database before it is populated with
** recovered data. This is slower overall, but avoids the slow call
** to sqlite3_recover_step() at the end of the recovery operation.
**
** The default option value is 0.
*/
#define SQLITE_RECOVER_LOST_AND_FOUND 1
#define SQLITE_RECOVER_FREELIST_CORRUPT 2
#define SQLITE_RECOVER_ROWIDS 3
#define SQLITE_RECOVER_SLOWINDEXES 4
/*
** Perform a unit of work towards the recovery operation. This function
** must normally be called multiple times to complete database recovery.
**
** If no error occurs but the recovery operation is not completed, this
** function returns SQLITE_OK. If recovery has been completed successfully
** then SQLITE_DONE is returned. If an error has occurred, then an SQLite
** error code (e.g. SQLITE_IOERR or SQLITE_NOMEM) is returned. It is not
** considered an error if some or all of the data cannot be recovered
** due to database corruption.
**
** Once sqlite3_recover_step() has returned a value other than SQLITE_OK,
** all further such calls on the same recover handle are no-ops that return
** the same non-SQLITE_OK value.
*/
int sqlite3_recover_step(sqlite3_recover*);
/*
** Run the recovery operation to completion. Return SQLITE_OK if successful,
** or an SQLite error code otherwise. Calling this function is the same
** as executing:
**
** while( SQLITE_OK==sqlite3_recover_step(p) );
** return sqlite3_recover_errcode(p);
*/
int sqlite3_recover_run(sqlite3_recover*);
/*
** If an error has been encountered during a prior call to
** sqlite3_recover_step(), then this function attempts to return a
** pointer to a buffer containing an English language explanation of
** the error. If no error message is available, or if an out-of memory
** error occurs while attempting to allocate a buffer in which to format
** the error message, NULL is returned.
**
** The returned buffer remains valid until the sqlite3_recover handle is
** destroyed using sqlite3_recover_finish().
*/
const char *sqlite3_recover_errmsg(sqlite3_recover*);
/*
** If this function is called on an sqlite3_recover handle after
** an error occurs, an SQLite error code is returned. Otherwise, SQLITE_OK.
*/
int sqlite3_recover_errcode(sqlite3_recover*);
/*
** Clean up a recovery object created by a call to sqlite3_recover_init().
** The results of using a recovery object with any API after it has been
** passed to this function are undefined.
**
** This function returns the same value as sqlite3_recover_errcode().
*/
int sqlite3_recover_finish(sqlite3_recover*);
#ifdef __cplusplus
} /* end of the 'extern "C"' block */
#endif
#endif /* ifndef _SQLITE_RECOVER_H */
/************************* End ../ext/recover/sqlite3recover.h ********************/
/************************* Begin ../ext/recover/sqlite3recover.c ******************/
/*
** 2022-08-27
**
** The author disclaims copyright to this source code. In place of
** a legal notice, here is a blessing:
**
** May you do good and not evil.
** May you find forgiveness for yourself and forgive others.
** May you share freely, never taking more than you give.
**
*************************************************************************
**
*/
/* #include "sqlite3recover.h" */
#include <assert.h>
#include <string.h>
#ifndef SQLITE_OMIT_VIRTUALTABLE
/*
** Declaration for public API function in file dbdata.c. This may be called
** with NULL as the final two arguments to register the sqlite_dbptr and
** sqlite_dbdata virtual tables with a database handle.
*/
#ifdef _WIN32
#endif
int sqlite3_dbdata_init(sqlite3*, char**, const sqlite3_api_routines*);
/* typedef unsigned int u32; */
/* typedef unsigned char u8; */
/* typedef sqlite3_int64 i64; */
typedef struct RecoverTable RecoverTable;
typedef struct RecoverColumn RecoverColumn;
/*
** When recovering rows of data that can be associated with table
** definitions recovered from the sqlite_schema table, each table is
** represented by an instance of the following object.
**
** iRoot:
** The root page in the original database. Not necessarily (and usually
** not) the same in the recovered database.
**
** zTab:
** Name of the table.
**
** nCol/aCol[]:
** aCol[] is an array of nCol columns. In the order in which they appear
** in the table.
**
** bIntkey:
** Set to true for intkey tables, false for WITHOUT ROWID.
**
** iRowidBind:
** Each column in the aCol[] array has associated with it the index of
** the bind parameter its values will be bound to in the INSERT statement
** used to construct the output database. If the table does has a rowid
** but not an INTEGER PRIMARY KEY column, then iRowidBind contains the
** index of the bind paramater to which the rowid value should be bound.
** Otherwise, it contains -1. If the table does contain an INTEGER PRIMARY
** KEY column, then the rowid value should be bound to the index associated
** with the column.
**
** pNext:
** All RecoverTable objects used by the recovery operation are allocated
** and populated as part of creating the recovered database schema in
** the output database, before any non-schema data are recovered. They
** are then stored in a singly-linked list linked by this variable beginning
** at sqlite3_recover.pTblList.
*/
struct RecoverTable {
u32 iRoot; /* Root page in original database */
char *zTab; /* Name of table */
int nCol; /* Number of columns in table */
RecoverColumn *aCol; /* Array of columns */
int bIntkey; /* True for intkey, false for without rowid */
int iRowidBind; /* If >0, bind rowid to INSERT here */
RecoverTable *pNext;
};
/*
** Each database column is represented by an instance of the following object
** stored in the RecoverTable.aCol[] array of the associated table.
**
** iField:
** The index of the associated field within database records. Or -1 if
** there is no associated field (e.g. for virtual generated columns).
**
** iBind:
** The bind index of the INSERT statement to bind this columns values
** to. Or 0 if there is no such index (iff (iField<0)).
**
** bIPK:
** True if this is the INTEGER PRIMARY KEY column.
**
** zCol:
** Name of column.
**
** eHidden:
** A RECOVER_EHIDDEN_* constant value (see below for interpretation of each).
*/
struct RecoverColumn {
int iField; /* Field in record on disk */
int iBind; /* Binding to use in INSERT */
int bIPK; /* True for IPK column */
char *zCol;
int eHidden;
};
#define RECOVER_EHIDDEN_NONE 0 /* Normal database column */
#define RECOVER_EHIDDEN_HIDDEN 1 /* Column is __HIDDEN__ */
#define RECOVER_EHIDDEN_VIRTUAL 2 /* Virtual generated column */
#define RECOVER_EHIDDEN_STORED 3 /* Stored generated column */
/*
** Bitmap object used to track pages in the input database. Allocated
** and manipulated only by the following functions:
**
** recoverBitmapAlloc()
** recoverBitmapFree()
** recoverBitmapSet()
** recoverBitmapQuery()
**
** nPg:
** Largest page number that may be stored in the bitmap. The range
** of valid keys is 1 to nPg, inclusive.
**
** aElem[]:
** Array large enough to contain a bit for each key. For key value
** iKey, the associated bit is the bit (iKey%32) of aElem[iKey/32].
** In other words, the following is true if bit iKey is set, or
** false if it is clear:
**
** (aElem[iKey/32] & (1 << (iKey%32))) ? 1 : 0
*/
typedef struct RecoverBitmap RecoverBitmap;
struct RecoverBitmap {
i64 nPg; /* Size of bitmap */
u32 aElem[1]; /* Array of 32-bit bitmasks */
};
/*
** State variables (part of the sqlite3_recover structure) used while
** recovering data for tables identified in the recovered schema (state
** RECOVER_STATE_WRITING).
*/
typedef struct RecoverStateW1 RecoverStateW1;
struct RecoverStateW1 {
sqlite3_stmt *pTbls;
sqlite3_stmt *pSel;
sqlite3_stmt *pInsert;
int nInsert;
RecoverTable *pTab; /* Table currently being written */
int nMax; /* Max column count in any schema table */
sqlite3_value **apVal; /* Array of nMax values */
int nVal; /* Number of valid entries in apVal[] */
int bHaveRowid;
i64 iRowid;
i64 iPrevPage;
int iPrevCell;
};
/*
** State variables (part of the sqlite3_recover structure) used while
** recovering data destined for the lost and found table (states
** RECOVER_STATE_LOSTANDFOUND[123]).
*/
typedef struct RecoverStateLAF RecoverStateLAF;
struct RecoverStateLAF {
RecoverBitmap *pUsed;
i64 nPg; /* Size of db in pages */
sqlite3_stmt *pAllAndParent;
sqlite3_stmt *pMapInsert;
sqlite3_stmt *pMaxField;
sqlite3_stmt *pUsedPages;
sqlite3_stmt *pFindRoot;
sqlite3_stmt *pInsert; /* INSERT INTO lost_and_found ... */
sqlite3_stmt *pAllPage;
sqlite3_stmt *pPageData;
sqlite3_value **apVal;
int nMaxField;
};
/*
** Main recover handle structure.
*/
struct sqlite3_recover {
/* Copies of sqlite3_recover_init[_sql]() parameters */
sqlite3 *dbIn; /* Input database */
char *zDb; /* Name of input db ("main" etc.) */
char *zUri; /* URI for output database */
void *pSqlCtx; /* SQL callback context */
int (*xSql)(void*,const char*); /* Pointer to SQL callback function */
/* Values configured by sqlite3_recover_config() */
char *zStateDb; /* State database to use (or NULL) */
char *zLostAndFound; /* Name of lost-and-found table (or NULL) */
int bFreelistCorrupt; /* SQLITE_RECOVER_FREELIST_CORRUPT setting */
int bRecoverRowid; /* SQLITE_RECOVER_ROWIDS setting */
int bSlowIndexes; /* SQLITE_RECOVER_SLOWINDEXES setting */
int pgsz;
int detected_pgsz;
int nReserve;
u8 *pPage1Disk;
u8 *pPage1Cache;
/* Error code and error message */
int errCode; /* For sqlite3_recover_errcode() */
char *zErrMsg; /* For sqlite3_recover_errmsg() */
int eState;
int bCloseTransaction;
/* Variables used with eState==RECOVER_STATE_WRITING */
RecoverStateW1 w1;
/* Variables used with states RECOVER_STATE_LOSTANDFOUND[123] */
RecoverStateLAF laf;
/* Fields used within sqlite3_recover_run() */
sqlite3 *dbOut; /* Output database */
sqlite3_stmt *pGetPage; /* SELECT against input db sqlite_dbdata */
RecoverTable *pTblList; /* List of tables recovered from schema */
};
/*
** The various states in which an sqlite3_recover object may exist:
**
** RECOVER_STATE_INIT:
** The object is initially created in this state. sqlite3_recover_step()
** has yet to be called. This is the only state in which it is permitted
** to call sqlite3_recover_config().
**
** RECOVER_STATE_WRITING:
**
** RECOVER_STATE_LOSTANDFOUND1:
** State to populate the bitmap of pages used by other tables or the
** database freelist.
**
** RECOVER_STATE_LOSTANDFOUND2:
** Populate the recovery.map table - used to figure out a "root" page
** for each lost page from in the database from which records are
** extracted.
**
** RECOVER_STATE_LOSTANDFOUND3:
** Populate the lost-and-found table itself.
*/
#define RECOVER_STATE_INIT 0
#define RECOVER_STATE_WRITING 1
#define RECOVER_STATE_LOSTANDFOUND1 2
#define RECOVER_STATE_LOSTANDFOUND2 3
#define RECOVER_STATE_LOSTANDFOUND3 4
#define RECOVER_STATE_SCHEMA2 5
#define RECOVER_STATE_DONE 6
/*
** Global variables used by this extension.
*/
typedef struct RecoverGlobal RecoverGlobal;
struct RecoverGlobal {
const sqlite3_io_methods *pMethods;
sqlite3_recover *p;
};
static RecoverGlobal recover_g;
/*
** Use this static SQLite mutex to protect the globals during the
** first call to sqlite3_recover_step().
*/
#define RECOVER_MUTEX_ID SQLITE_MUTEX_STATIC_APP2
/*
** Default value for SQLITE_RECOVER_ROWIDS (sqlite3_recover.bRecoverRowid).
*/
#define RECOVER_ROWID_DEFAULT 1
#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE==0
# define recoverEnterMutex()
# define recoverLeaveMutex()
# define recoverAssertMutexHeld()
#else
static void recoverEnterMutex(void){
sqlite3_mutex_enter(sqlite3_mutex_alloc(RECOVER_MUTEX_ID));
}
static void recoverLeaveMutex(void){
sqlite3_mutex_leave(sqlite3_mutex_alloc(RECOVER_MUTEX_ID));
}
static void recoverAssertMutexHeld(void){
assert( sqlite3_mutex_held(sqlite3_mutex_alloc(RECOVER_MUTEX_ID)) );
}
#endif
/*
** Like strlen(). But handles NULL pointer arguments.
*/
static int recoverStrlen(const char *zStr){
int nRet = 0;
if( zStr ){
while( zStr[nRet] ) nRet++;
}
return nRet;
}
/*
** This function is a no-op if the recover handle passed as the first
** argument already contains an error (if p->errCode!=SQLITE_OK).
**
** Otherwise, an attempt is made to allocate, zero and return a buffer nByte
** bytes in size. If successful, a pointer to the new buffer is returned. Or,
** if an OOM error occurs, NULL is returned and the handle error code
** (p->errCode) set to SQLITE_NOMEM.
*/
static void *recoverMalloc(sqlite3_recover *p, i64 nByte){
void *pRet = 0;
assert( nByte>0 );
if( p->errCode==SQLITE_OK ){
pRet = sqlite3_malloc64(nByte);
if( pRet ){
memset(pRet, 0, nByte);
}else{
p->errCode = SQLITE_NOMEM;
}
}
return pRet;
}
/*
** Set the error code and error message for the recover handle passed as
** the first argument. The error code is set to the value of parameter
** errCode.
**
** Parameter zFmt must be a printf() style formatting string. The handle
** error message is set to the result of using any trailing arguments for
** parameter substitutions in the formatting string.
**
** For example:
**
** recoverError(p, SQLITE_ERROR, "no such table: %s", zTablename);
*/
static int recoverError(
sqlite3_recover *p,
int errCode,
const char *zFmt, ...
){
char *z = 0;
va_list ap;
va_start(ap, zFmt);
if( zFmt ){
z = sqlite3_vmprintf(zFmt, ap);
va_end(ap);
}
sqlite3_free(p->zErrMsg);
p->zErrMsg = z;
p->errCode = errCode;
return errCode;
}
/*
** This function is a no-op if p->errCode is initially other than SQLITE_OK.
** In this case it returns NULL.
**
** Otherwise, an attempt is made to allocate and return a bitmap object
** large enough to store a bit for all page numbers between 1 and nPg,
** inclusive. The bitmap is initially zeroed.
*/
static RecoverBitmap *recoverBitmapAlloc(sqlite3_recover *p, i64 nPg){
int nElem = (nPg+1+31) / 32;
int nByte = sizeof(RecoverBitmap) + nElem*sizeof(u32);
RecoverBitmap *pRet = (RecoverBitmap*)recoverMalloc(p, nByte);
if( pRet ){
pRet->nPg = nPg;
}
return pRet;
}
/*
** Free a bitmap object allocated by recoverBitmapAlloc().
*/
static void recoverBitmapFree(RecoverBitmap *pMap){
sqlite3_free(pMap);
}
/*
** Set the bit associated with page iPg in bitvec pMap.
*/
static void recoverBitmapSet(RecoverBitmap *pMap, i64 iPg){
if( iPg<=pMap->nPg ){
int iElem = (iPg / 32);
int iBit = (iPg % 32);
pMap->aElem[iElem] |= (((u32)1) << iBit);
}
}
/*
** Query bitmap object pMap for the state of the bit associated with page
** iPg. Return 1 if it is set, or 0 otherwise.
*/
static int recoverBitmapQuery(RecoverBitmap *pMap, i64 iPg){
int ret = 1;
if( iPg<=pMap->nPg && iPg>0 ){
int iElem = (iPg / 32);
int iBit = (iPg % 32);
ret = (pMap->aElem[iElem] & (((u32)1) << iBit)) ? 1 : 0;
}
return ret;
}
/*
** Set the recover handle error to the error code and message returned by
** calling sqlite3_errcode() and sqlite3_errmsg(), respectively, on database
** handle db.
*/
static int recoverDbError(sqlite3_recover *p, sqlite3 *db){
return recoverError(p, sqlite3_errcode(db), "%s", sqlite3_errmsg(db));
}
/*
** This function is a no-op if recover handle p already contains an error
** (if p->errCode!=SQLITE_OK).
**
** Otherwise, it attempts to prepare the SQL statement in zSql against
** database handle db. If successful, the statement handle is returned.
** Or, if an error occurs, NULL is returned and an error left in the
** recover handle.
*/
static sqlite3_stmt *recoverPrepare(
sqlite3_recover *p,
sqlite3 *db,
const char *zSql
){
sqlite3_stmt *pStmt = 0;
if( p->errCode==SQLITE_OK ){
if( sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0) ){
recoverDbError(p, db);
}
}
return pStmt;
}
/*
** This function is a no-op if recover handle p already contains an error
** (if p->errCode!=SQLITE_OK).
**
** Otherwise, argument zFmt is used as a printf() style format string,
** along with any trailing arguments, to create an SQL statement. This
** SQL statement is prepared against database handle db and, if successful,
** the statment handle returned. Or, if an error occurs - either during
** the printf() formatting or when preparing the resulting SQL - an
** error code and message are left in the recover handle.
*/
static sqlite3_stmt *recoverPreparePrintf(
sqlite3_recover *p,
sqlite3 *db,
const char *zFmt, ...
){
sqlite3_stmt *pStmt = 0;
if( p->errCode==SQLITE_OK ){
va_list ap;
char *z;
va_start(ap, zFmt);
z = sqlite3_vmprintf(zFmt, ap);
va_end(ap);
if( z==0 ){
p->errCode = SQLITE_NOMEM;
}else{
pStmt = recoverPrepare(p, db, z);
sqlite3_free(z);
}
}
return pStmt;
}
/*
** Reset SQLite statement handle pStmt. If the call to sqlite3_reset()
** indicates that an error occurred, and there is not already an error
** in the recover handle passed as the first argument, set the error
** code and error message appropriately.
**
** This function returns a copy of the statement handle pointer passed
** as the second argument.
*/
static sqlite3_stmt *recoverReset(sqlite3_recover *p, sqlite3_stmt *pStmt){
int rc = sqlite3_reset(pStmt);
if( rc!=SQLITE_OK && rc!=SQLITE_CONSTRAINT && p->errCode==SQLITE_OK ){
recoverDbError(p, sqlite3_db_handle(pStmt));
}
return pStmt;
}
/*
** Finalize SQLite statement handle pStmt. If the call to sqlite3_reset()
** indicates that an error occurred, and there is not already an error
** in the recover handle passed as the first argument, set the error
** code and error message appropriately.
*/
static void recoverFinalize(sqlite3_recover *p, sqlite3_stmt *pStmt){
sqlite3 *db = sqlite3_db_handle(pStmt);
int rc = sqlite3_finalize(pStmt);
if( rc!=SQLITE_OK && p->errCode==SQLITE_OK ){
recoverDbError(p, db);
}
}
/*
** This function is a no-op if recover handle p already contains an error
** (if p->errCode!=SQLITE_OK). A copy of p->errCode is returned in this
** case.
**
** Otherwise, execute SQL script zSql. If successful, return SQLITE_OK.
** Or, if an error occurs, leave an error code and message in the recover
** handle and return a copy of the error code.
*/
static int recoverExec(sqlite3_recover *p, sqlite3 *db, const char *zSql){
if( p->errCode==SQLITE_OK ){
int rc = sqlite3_exec(db, zSql, 0, 0, 0);
if( rc ){
recoverDbError(p, db);
}
}
return p->errCode;
}
/*
** Bind the value pVal to parameter iBind of statement pStmt. Leave an
** error in the recover handle passed as the first argument if an error
** (e.g. an OOM) occurs.
*/
static void recoverBindValue(
sqlite3_recover *p,
sqlite3_stmt *pStmt,
int iBind,
sqlite3_value *pVal
){
if( p->errCode==SQLITE_OK ){
int rc = sqlite3_bind_value(pStmt, iBind, pVal);
if( rc ) recoverError(p, rc, 0);
}
}
/*
** This function is a no-op if recover handle p already contains an error
** (if p->errCode!=SQLITE_OK). NULL is returned in this case.
**
** Otherwise, an attempt is made to interpret zFmt as a printf() style
** formatting string and the result of using the trailing arguments for
** parameter substitution with it written into a buffer obtained from
** sqlite3_malloc(). If successful, a pointer to the buffer is returned.
** It is the responsibility of the caller to eventually free the buffer
** using sqlite3_free().
**
** Or, if an error occurs, an error code and message is left in the recover
** handle and NULL returned.
*/
static char *recoverMPrintf(sqlite3_recover *p, const char *zFmt, ...){
va_list ap;
char *z;
va_start(ap, zFmt);
z = sqlite3_vmprintf(zFmt, ap);
va_end(ap);
if( p->errCode==SQLITE_OK ){
if( z==0 ) p->errCode = SQLITE_NOMEM;
}else{
sqlite3_free(z);
z = 0;
}
return z;
}
/*
** This function is a no-op if recover handle p already contains an error
** (if p->errCode!=SQLITE_OK). Zero is returned in this case.
**
** Otherwise, execute "PRAGMA page_count" against the input database. If
** successful, return the integer result. Or, if an error occurs, leave an
** error code and error message in the sqlite3_recover handle and return
** zero.
*/
static i64 recoverPageCount(sqlite3_recover *p){
i64 nPg = 0;
if( p->errCode==SQLITE_OK ){
sqlite3_stmt *pStmt = 0;
pStmt = recoverPreparePrintf(p, p->dbIn, "PRAGMA %Q.page_count", p->zDb);
if( pStmt ){
sqlite3_step(pStmt);
nPg = sqlite3_column_int64(pStmt, 0);
}
recoverFinalize(p, pStmt);
}
return nPg;
}
/*
** Implementation of SQL scalar function "read_i32". The first argument to
** this function must be a blob. The second a non-negative integer. This
** function reads and returns a 32-bit big-endian integer from byte
** offset (4*<arg2>) of the blob.
**
** SELECT read_i32(<blob>, <idx>)
*/
static void recoverReadI32(
sqlite3_context *context,
int argc,
sqlite3_value **argv
){
const unsigned char *pBlob;
int nBlob;
int iInt;
assert( argc==2 );
nBlob = sqlite3_value_bytes(argv[0]);
pBlob = (const unsigned char*)sqlite3_value_blob(argv[0]);
iInt = sqlite3_value_int(argv[1]) & 0xFFFF;
if( (iInt+1)*4<=nBlob ){
const unsigned char *a = &pBlob[iInt*4];
i64 iVal = ((i64)a[0]<<24)
+ ((i64)a[1]<<16)
+ ((i64)a[2]<< 8)
+ ((i64)a[3]<< 0);
sqlite3_result_int64(context, iVal);
}
}
/*
** Implementation of SQL scalar function "page_is_used". This function
** is used as part of the procedure for locating orphan rows for the
** lost-and-found table, and it depends on those routines having populated
** the sqlite3_recover.laf.pUsed variable.
**
** The only argument to this function is a page-number. It returns true
** if the page has already been used somehow during data recovery, or false
** otherwise.
**
** SELECT page_is_used(<pgno>);
*/
static void recoverPageIsUsed(
sqlite3_context *pCtx,
int nArg,
sqlite3_value **apArg
){
sqlite3_recover *p = (sqlite3_recover*)sqlite3_user_data(pCtx);
i64 pgno = sqlite3_value_int64(apArg[0]);
assert( nArg==1 );
sqlite3_result_int(pCtx, recoverBitmapQuery(p->laf.pUsed, pgno));
}
/*
** The implementation of a user-defined SQL function invoked by the
** sqlite_dbdata and sqlite_dbptr virtual table modules to access pages
** of the database being recovered.
**
** This function always takes a single integer argument. If the argument
** is zero, then the value returned is the number of pages in the db being
** recovered. If the argument is greater than zero, it is a page number.
** The value returned in this case is an SQL blob containing the data for
** the identified page of the db being recovered. e.g.
**
** SELECT getpage(0); -- return number of pages in db
** SELECT getpage(4); -- return page 4 of db as a blob of data
*/
static void recoverGetPage(
sqlite3_context *pCtx,
int nArg,
sqlite3_value **apArg
){
sqlite3_recover *p = (sqlite3_recover*)sqlite3_user_data(pCtx);
i64 pgno = sqlite3_value_int64(apArg[0]);
sqlite3_stmt *pStmt = 0;
assert( nArg==1 );
if( pgno==0 ){
i64 nPg = recoverPageCount(p);
sqlite3_result_int64(pCtx, nPg);
return;
}else{
if( p->pGetPage==0 ){
pStmt = p->pGetPage = recoverPreparePrintf(
p, p->dbIn, "SELECT data FROM sqlite_dbpage(%Q) WHERE pgno=?", p->zDb
);
}else if( p->errCode==SQLITE_OK ){
pStmt = p->pGetPage;
}
if( pStmt ){
sqlite3_bind_int64(pStmt, 1, pgno);
if( SQLITE_ROW==sqlite3_step(pStmt) ){
const u8 *aPg;
int nPg;
assert( p->errCode==SQLITE_OK );
aPg = sqlite3_column_blob(pStmt, 0);
nPg = sqlite3_column_bytes(pStmt, 0);
if( pgno==1 && nPg==p->pgsz && 0==memcmp(p->pPage1Cache, aPg, nPg) ){
aPg = p->pPage1Disk;
}
sqlite3_result_blob(pCtx, aPg, nPg-p->nReserve, SQLITE_TRANSIENT);
}
recoverReset(p, pStmt);
}
}
if( p->errCode ){
if( p->zErrMsg ) sqlite3_result_error(pCtx, p->zErrMsg, -1);
sqlite3_result_error_code(pCtx, p->errCode);
}
}
/*
** Find a string that is not found anywhere in z[]. Return a pointer
** to that string.
**
** Try to use zA and zB first. If both of those are already found in z[]
** then make up some string and store it in the buffer zBuf.
*/
static const char *recoverUnusedString(
const char *z, /* Result must not appear anywhere in z */
const char *zA, const char *zB, /* Try these first */
char *zBuf /* Space to store a generated string */
){
unsigned i = 0;
if( strstr(z, zA)==0 ) return zA;
if( strstr(z, zB)==0 ) return zB;
do{
sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
}while( strstr(z,zBuf)!=0 );
return zBuf;
}
/*
** Implementation of scalar SQL function "escape_crnl". The argument passed to
** this function is the output of built-in function quote(). If the first
** character of the input is "'", indicating that the value passed to quote()
** was a text value, then this function searches the input for "\n" and "\r"
** characters and adds a wrapper similar to the following:
**
** replace(replace(<input>, '\n', char(10), '\r', char(13));
**
** Or, if the first character of the input is not "'", then a copy of the input
** is returned.
*/
static void recoverEscapeCrnl(
sqlite3_context *context,
int argc,
sqlite3_value **argv
){
const char *zText = (const char*)sqlite3_value_text(argv[0]);
if( zText && zText[0]=='\'' ){
int nText = sqlite3_value_bytes(argv[0]);
int i;
char zBuf1[20];
char zBuf2[20];
const char *zNL = 0;
const char *zCR = 0;
int nCR = 0;
int nNL = 0;
for(i=0; zText[i]; i++){
if( zNL==0 && zText[i]=='\n' ){
zNL = recoverUnusedString(zText, "\\n", "\\012", zBuf1);
nNL = (int)strlen(zNL);
}
if( zCR==0 && zText[i]=='\r' ){
zCR = recoverUnusedString(zText, "\\r", "\\015", zBuf2);
nCR = (int)strlen(zCR);
}
}
if( zNL || zCR ){
int iOut = 0;
i64 nMax = (nNL > nCR) ? nNL : nCR;
i64 nAlloc = nMax * nText + (nMax+64)*2;
char *zOut = (char*)sqlite3_malloc64(nAlloc);
if( zOut==0 ){
sqlite3_result_error_nomem(context);
return;
}
if( zNL && zCR ){
memcpy(&zOut[iOut], "replace(replace(", 16);
iOut += 16;
}else{
memcpy(&zOut[iOut], "replace(", 8);
iOut += 8;
}
for(i=0; zText[i]; i++){
if( zText[i]=='\n' ){
memcpy(&zOut[iOut], zNL, nNL);
iOut += nNL;
}else if( zText[i]=='\r' ){
memcpy(&zOut[iOut], zCR, nCR);
iOut += nCR;
}else{
zOut[iOut] = zText[i];
iOut++;
}
}
if( zNL ){
memcpy(&zOut[iOut], ",'", 2); iOut += 2;
memcpy(&zOut[iOut], zNL, nNL); iOut += nNL;
memcpy(&zOut[iOut], "', char(10))", 12); iOut += 12;
}
if( zCR ){
memcpy(&zOut[iOut], ",'", 2); iOut += 2;
memcpy(&zOut[iOut], zCR, nCR); iOut += nCR;
memcpy(&zOut[iOut], "', char(13))", 12); iOut += 12;
}
sqlite3_result_text(context, zOut, iOut, SQLITE_TRANSIENT);
sqlite3_free(zOut);
return;
}
}
sqlite3_result_value(context, argv[0]);
}
/*
** This function is a no-op if recover handle p already contains an error
** (if p->errCode!=SQLITE_OK). A copy of the error code is returned in
** this case.
**
** Otherwise, attempt to populate temporary table "recovery.schema" with the
** parts of the database schema that can be extracted from the input database.
**
** If no error occurs, SQLITE_OK is returned. Otherwise, an error code
** and error message are left in the recover handle and a copy of the
** error code returned. It is not considered an error if part of all of
** the database schema cannot be recovered due to corruption.
*/
static int recoverCacheSchema(sqlite3_recover *p){
return recoverExec(p, p->dbOut,
"WITH RECURSIVE pages(p) AS ("
" SELECT 1"
" UNION"
" SELECT child FROM sqlite_dbptr('getpage()'), pages WHERE pgno=p"
")"
"INSERT INTO recovery.schema SELECT"
" max(CASE WHEN field=0 THEN value ELSE NULL END),"
" max(CASE WHEN field=1 THEN value ELSE NULL END),"
" max(CASE WHEN field=2 THEN value ELSE NULL END),"
" max(CASE WHEN field=3 THEN value ELSE NULL END),"
" max(CASE WHEN field=4 THEN value ELSE NULL END)"
"FROM sqlite_dbdata('getpage()') WHERE pgno IN ("
" SELECT p FROM pages"
") GROUP BY pgno, cell"
);
}
/*
** If this recover handle is not in SQL callback mode (i.e. was not created
** using sqlite3_recover_init_sql()) of if an error has already occurred,
** this function is a no-op. Otherwise, issue a callback with SQL statement
** zSql as the parameter.
**
** If the callback returns non-zero, set the recover handle error code to
** the value returned (so that the caller will abandon processing).
*/
static void recoverSqlCallback(sqlite3_recover *p, const char *zSql){
if( p->errCode==SQLITE_OK && p->xSql ){
int res = p->xSql(p->pSqlCtx, zSql);
if( res ){
recoverError(p, SQLITE_ERROR, "callback returned an error - %d", res);
}
}
}
/*
** Transfer the following settings from the input database to the output
** database:
**
** + page-size,
** + auto-vacuum settings,
** + database encoding,
** + user-version (PRAGMA user_version), and
** + application-id (PRAGMA application_id), and
*/
static void recoverTransferSettings(sqlite3_recover *p){
const char *aPragma[] = {
"encoding",
"page_size",
"auto_vacuum",
"user_version",
"application_id"
};
int ii;
/* Truncate the output database to 0 pages in size. This is done by
** opening a new, empty, temp db, then using the backup API to clobber
** any existing output db with a copy of it. */
if( p->errCode==SQLITE_OK ){
sqlite3 *db2 = 0;
int rc = sqlite3_open("", &db2);
if( rc!=SQLITE_OK ){
recoverDbError(p, db2);
return;
}
for(ii=0; ii<sizeof(aPragma)/sizeof(aPragma[0]); ii++){
const char *zPrag = aPragma[ii];
sqlite3_stmt *p1 = 0;
p1 = recoverPreparePrintf(p, p->dbIn, "PRAGMA %Q.%s", p->zDb, zPrag);
if( p->errCode==SQLITE_OK && sqlite3_step(p1)==SQLITE_ROW ){
const char *zArg = (const char*)sqlite3_column_text(p1, 0);
char *z2 = recoverMPrintf(p, "PRAGMA %s = %Q", zPrag, zArg);
recoverSqlCallback(p, z2);
recoverExec(p, db2, z2);
sqlite3_free(z2);
if( zArg==0 ){
recoverError(p, SQLITE_NOMEM, 0);
}
}
recoverFinalize(p, p1);
}
recoverExec(p, db2, "CREATE TABLE t1(a); DROP TABLE t1;");
if( p->errCode==SQLITE_OK ){
sqlite3 *db = p->dbOut;
sqlite3_backup *pBackup = sqlite3_backup_init(db, "main", db2, "main");
if( pBackup ){
sqlite3_backup_step(pBackup, -1);
p->errCode = sqlite3_backup_finish(pBackup);
}else{
recoverDbError(p, db);
}
}
sqlite3_close(db2);
}
}
/*
** This function is a no-op if recover handle p already contains an error
** (if p->errCode!=SQLITE_OK). A copy of the error code is returned in
** this case.
**
** Otherwise, an attempt is made to open the output database, attach
** and create the schema of the temporary database used to store
** intermediate data, and to register all required user functions and
** virtual table modules with the output handle.
**
** If no error occurs, SQLITE_OK is returned. Otherwise, an error code
** and error message are left in the recover handle and a copy of the
** error code returned.
*/
static int recoverOpenOutput(sqlite3_recover *p){
struct Func {
const char *zName;
int nArg;
void (*xFunc)(sqlite3_context*,int,sqlite3_value **);
} aFunc[] = {
{ "getpage", 1, recoverGetPage },
{ "page_is_used", 1, recoverPageIsUsed },
{ "read_i32", 2, recoverReadI32 },
{ "escape_crnl", 1, recoverEscapeCrnl },
};
const int flags = SQLITE_OPEN_URI|SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE;
sqlite3 *db = 0; /* New database handle */
int ii; /* For iterating through aFunc[] */
assert( p->dbOut==0 );
if( sqlite3_open_v2(p->zUri, &db, flags, 0) ){
recoverDbError(p, db);
}
/* Register the sqlite_dbdata and sqlite_dbptr virtual table modules.
** These two are registered with the output database handle - this
** module depends on the input handle supporting the sqlite_dbpage
** virtual table only. */
if( p->errCode==SQLITE_OK ){
p->errCode = sqlite3_dbdata_init(db, 0, 0);
}
/* Register the custom user-functions with the output handle. */
for(ii=0; p->errCode==SQLITE_OK && ii<sizeof(aFunc)/sizeof(aFunc[0]); ii++){
p->errCode = sqlite3_create_function(db, aFunc[ii].zName,
aFunc[ii].nArg, SQLITE_UTF8, (void*)p, aFunc[ii].xFunc, 0, 0
);
}
p->dbOut = db;
return p->errCode;
}
/*
** Attach the auxiliary database 'recovery' to the output database handle.
** This temporary database is used during the recovery process and then
** discarded.
*/
static void recoverOpenRecovery(sqlite3_recover *p){
char *zSql = recoverMPrintf(p, "ATTACH %Q AS recovery;", p->zStateDb);
recoverExec(p, p->dbOut, zSql);
recoverExec(p, p->dbOut,
"PRAGMA writable_schema = 1;"
"CREATE TABLE recovery.map(pgno INTEGER PRIMARY KEY, parent INT);"
"CREATE TABLE recovery.schema(type, name, tbl_name, rootpage, sql);"
);
sqlite3_free(zSql);
}
/*
** This function is a no-op if recover handle p already contains an error
** (if p->errCode!=SQLITE_OK).
**
** Otherwise, argument zName must be the name of a table that has just been
** created in the output database. This function queries the output db
** for the schema of said table, and creates a RecoverTable object to
** store the schema in memory. The new RecoverTable object is linked into
** the list at sqlite3_recover.pTblList.
**
** Parameter iRoot must be the root page of table zName in the INPUT
** database.
*/
static void recoverAddTable(
sqlite3_recover *p,
const char *zName, /* Name of table created in output db */
i64 iRoot /* Root page of same table in INPUT db */
){
sqlite3_stmt *pStmt = recoverPreparePrintf(p, p->dbOut,
"PRAGMA table_xinfo(%Q)", zName
);
if( pStmt ){
int iPk = -1;
int iBind = 1;
RecoverTable *pNew = 0;
int nCol = 0;
int nName = recoverStrlen(zName);
int nByte = 0;
while( sqlite3_step(pStmt)==SQLITE_ROW ){
nCol++;
nByte += (sqlite3_column_bytes(pStmt, 1)+1);
}
nByte += sizeof(RecoverTable) + nCol*sizeof(RecoverColumn) + nName+1;
recoverReset(p, pStmt);
pNew = recoverMalloc(p, nByte);
if( pNew ){
int i = 0;
int iField = 0;
char *csr = 0;
pNew->aCol = (RecoverColumn*)&pNew[1];
pNew->zTab = csr = (char*)&pNew->aCol[nCol];
pNew->nCol = nCol;
pNew->iRoot = iRoot;
memcpy(csr, zName, nName);
csr += nName+1;
for(i=0; sqlite3_step(pStmt)==SQLITE_ROW; i++){
int iPKF = sqlite3_column_int(pStmt, 5);
int n = sqlite3_column_bytes(pStmt, 1);
const char *z = (const char*)sqlite3_column_text(pStmt, 1);
const char *zType = (const char*)sqlite3_column_text(pStmt, 2);
int eHidden = sqlite3_column_int(pStmt, 6);
if( iPk==-1 && iPKF==1 && !sqlite3_stricmp("integer", zType) ) iPk = i;
if( iPKF>1 ) iPk = -2;
pNew->aCol[i].zCol = csr;
pNew->aCol[i].eHidden = eHidden;
if( eHidden==RECOVER_EHIDDEN_VIRTUAL ){
pNew->aCol[i].iField = -1;
}else{
pNew->aCol[i].iField = iField++;
}
if( eHidden!=RECOVER_EHIDDEN_VIRTUAL
&& eHidden!=RECOVER_EHIDDEN_STORED
){
pNew->aCol[i].iBind = iBind++;
}
memcpy(csr, z, n);
csr += (n+1);
}
pNew->pNext = p->pTblList;
p->pTblList = pNew;
pNew->bIntkey = 1;
}
recoverFinalize(p, pStmt);
pStmt = recoverPreparePrintf(p, p->dbOut, "PRAGMA index_xinfo(%Q)", zName);
while( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
int iField = sqlite3_column_int(pStmt, 0);
int iCol = sqlite3_column_int(pStmt, 1);
assert( iField<pNew->nCol && iCol<pNew->nCol );
pNew->aCol[iCol].iField = iField;
pNew->bIntkey = 0;
iPk = -2;
}
recoverFinalize(p, pStmt);
if( p->errCode==SQLITE_OK ){
if( iPk>=0 ){
pNew->aCol[iPk].bIPK = 1;
}else if( pNew->bIntkey ){
pNew->iRowidBind = iBind++;
}
}
}
}
/*
** This function is called after recoverCacheSchema() has cached those parts
** of the input database schema that could be recovered in temporary table
** "recovery.schema". This function creates in the output database copies
** of all parts of that schema that must be created before the tables can
** be populated. Specifically, this means:
**
** * all tables that are not VIRTUAL, and
** * UNIQUE indexes.
**
** If the recovery handle uses SQL callbacks, then callbacks containing
** the associated "CREATE TABLE" and "CREATE INDEX" statements are made.
**
** Additionally, records are added to the sqlite_schema table of the
** output database for any VIRTUAL tables. The CREATE VIRTUAL TABLE
** records are written directly to sqlite_schema, not actually executed.
** If the handle is in SQL callback mode, then callbacks are invoked
** with equivalent SQL statements.
*/
static int recoverWriteSchema1(sqlite3_recover *p){
sqlite3_stmt *pSelect = 0;
sqlite3_stmt *pTblname = 0;
pSelect = recoverPrepare(p, p->dbOut,
"WITH dbschema(rootpage, name, sql, tbl, isVirtual, isIndex) AS ("
" SELECT rootpage, name, sql, "
" type='table', "
" sql LIKE 'create virtual%',"
" (type='index' AND (sql LIKE '%unique%' OR ?1))"
" FROM recovery.schema"
")"
"SELECT rootpage, tbl, isVirtual, name, sql"
" FROM dbschema "
" WHERE tbl OR isIndex"
" ORDER BY tbl DESC, name=='sqlite_sequence' DESC"
);
pTblname = recoverPrepare(p, p->dbOut,
"SELECT name FROM sqlite_schema "
"WHERE type='table' ORDER BY rowid DESC LIMIT 1"
);
if( pSelect ){
sqlite3_bind_int(pSelect, 1, p->bSlowIndexes);
while( sqlite3_step(pSelect)==SQLITE_ROW ){
i64 iRoot = sqlite3_column_int64(pSelect, 0);
int bTable = sqlite3_column_int(pSelect, 1);
int bVirtual = sqlite3_column_int(pSelect, 2);
const char *zName = (const char*)sqlite3_column_text(pSelect, 3);
const char *zSql = (const char*)sqlite3_column_text(pSelect, 4);
char *zFree = 0;
int rc = SQLITE_OK;
if( bVirtual ){
zSql = (const char*)(zFree = recoverMPrintf(p,
"INSERT INTO sqlite_schema VALUES('table', %Q, %Q, 0, %Q)",
zName, zName, zSql
));
}
rc = sqlite3_exec(p->dbOut, zSql, 0, 0, 0);
if( rc==SQLITE_OK ){
recoverSqlCallback(p, zSql);
if( bTable && !bVirtual ){
if( SQLITE_ROW==sqlite3_step(pTblname) ){
const char *zTbl = (const char*)sqlite3_column_text(pTblname, 0);
recoverAddTable(p, zTbl, iRoot);
}
recoverReset(p, pTblname);
}
}else if( rc!=SQLITE_ERROR ){
recoverDbError(p, p->dbOut);
}
sqlite3_free(zFree);
}
}
recoverFinalize(p, pSelect);
recoverFinalize(p, pTblname);
return p->errCode;
}
/*
** This function is called after the output database has been populated. It
** adds all recovered schema elements that were not created in the output
** database by recoverWriteSchema1() - everything except for tables and
** UNIQUE indexes. Specifically:
**
** * views,
** * triggers,
** * non-UNIQUE indexes.
**
** If the recover handle is in SQL callback mode, then equivalent callbacks
** are issued to create the schema elements.
*/
static int recoverWriteSchema2(sqlite3_recover *p){
sqlite3_stmt *pSelect = 0;
pSelect = recoverPrepare(p, p->dbOut,
p->bSlowIndexes ?
"SELECT rootpage, sql FROM recovery.schema "
" WHERE type!='table' AND type!='index'"
:
"SELECT rootpage, sql FROM recovery.schema "
" WHERE type!='table' AND (type!='index' OR sql NOT LIKE '%unique%')"
);
if( pSelect ){
while( sqlite3_step(pSelect)==SQLITE_ROW ){
const char *zSql = (const char*)sqlite3_column_text(pSelect, 1);
int rc = sqlite3_exec(p->dbOut, zSql, 0, 0, 0);
if( rc==SQLITE_OK ){
recoverSqlCallback(p, zSql);
}else if( rc!=SQLITE_ERROR ){
recoverDbError(p, p->dbOut);
}
}
}
recoverFinalize(p, pSelect);
return p->errCode;
}
/*
** This function is a no-op if recover handle p already contains an error
** (if p->errCode!=SQLITE_OK). In this case it returns NULL.
**
** Otherwise, if the recover handle is configured to create an output
** database (was created by sqlite3_recover_init()), then this function
** prepares and returns an SQL statement to INSERT a new record into table
** pTab, assuming the first nField fields of a record extracted from disk
** are valid.
**
** For example, if table pTab is:
**
** CREATE TABLE name(a, b GENERATED ALWAYS AS (a+1) STORED, c, d, e);
**
** And nField is 4, then the SQL statement prepared and returned is:
**
** INSERT INTO (a, c, d) VALUES (?1, ?2, ?3);
**
** In this case even though 4 values were extracted from the input db,
** only 3 are written to the output, as the generated STORED column
** cannot be written.
**
** If the recover handle is in SQL callback mode, then the SQL statement
** prepared is such that evaluating it returns a single row containing
** a single text value - itself an SQL statement similar to the above,
** except with SQL literals in place of the variables. For example:
**
** SELECT 'INSERT INTO (a, c, d) VALUES ('
** || quote(?1) || ', '
** || quote(?2) || ', '
** || quote(?3) || ')';
**
** In either case, it is the responsibility of the caller to eventually
** free the statement handle using sqlite3_finalize().
*/
static sqlite3_stmt *recoverInsertStmt(
sqlite3_recover *p,
RecoverTable *pTab,
int nField
){
sqlite3_stmt *pRet = 0;
const char *zSep = "";
const char *zSqlSep = "";
char *zSql = 0;
char *zFinal = 0;
char *zBind = 0;
int ii;
int bSql = p->xSql ? 1 : 0;
if( nField<=0 ) return 0;
assert( nField<=pTab->nCol );
zSql = recoverMPrintf(p, "INSERT OR IGNORE INTO %Q(", pTab->zTab);
if( pTab->iRowidBind ){
assert( pTab->bIntkey );
zSql = recoverMPrintf(p, "%z_rowid_", zSql);
if( bSql ){
zBind = recoverMPrintf(p, "%zquote(?%d)", zBind, pTab->iRowidBind);
}else{
zBind = recoverMPrintf(p, "%z?%d", zBind, pTab->iRowidBind);
}
zSqlSep = "||', '||";
zSep = ", ";
}
for(ii=0; ii<nField; ii++){
int eHidden = pTab->aCol[ii].eHidden;
if( eHidden!=RECOVER_EHIDDEN_VIRTUAL
&& eHidden!=RECOVER_EHIDDEN_STORED
){
assert( pTab->aCol[ii].iField>=0 && pTab->aCol[ii].iBind>=1 );
zSql = recoverMPrintf(p, "%z%s%Q", zSql, zSep, pTab->aCol[ii].zCol);
if( bSql ){
zBind = recoverMPrintf(p,
"%z%sescape_crnl(quote(?%d))", zBind, zSqlSep, pTab->aCol[ii].iBind
);
zSqlSep = "||', '||";
}else{
zBind = recoverMPrintf(p, "%z%s?%d", zBind, zSep, pTab->aCol[ii].iBind);
}
zSep = ", ";
}
}
if( bSql ){
zFinal = recoverMPrintf(p, "SELECT %Q || ') VALUES (' || %s || ')'",
zSql, zBind
);
}else{
zFinal = recoverMPrintf(p, "%s) VALUES (%s)", zSql, zBind);
}
pRet = recoverPrepare(p, p->dbOut, zFinal);
sqlite3_free(zSql);
sqlite3_free(zBind);
sqlite3_free(zFinal);
return pRet;
}
/*
** Search the list of RecoverTable objects at p->pTblList for one that
** has root page iRoot in the input database. If such an object is found,
** return a pointer to it. Otherwise, return NULL.
*/
static RecoverTable *recoverFindTable(sqlite3_recover *p, u32 iRoot){
RecoverTable *pRet = 0;
for(pRet=p->pTblList; pRet && pRet->iRoot!=iRoot; pRet=pRet->pNext);
return pRet;
}
/*
** This function attempts to create a lost and found table within the
** output db. If successful, it returns a pointer to a buffer containing
** the name of the new table. It is the responsibility of the caller to
** eventually free this buffer using sqlite3_free().
**
** If an error occurs, NULL is returned and an error code and error
** message left in the recover handle.
*/
static char *recoverLostAndFoundCreate(
sqlite3_recover *p, /* Recover object */
int nField /* Number of column fields in new table */
){
char *zTbl = 0;
sqlite3_stmt *pProbe = 0;
int ii = 0;
pProbe = recoverPrepare(p, p->dbOut,
"SELECT 1 FROM sqlite_schema WHERE name=?"
);
for(ii=-1; zTbl==0 && p->errCode==SQLITE_OK && ii<1000; ii++){
int bFail = 0;
if( ii<0 ){
zTbl = recoverMPrintf(p, "%s", p->zLostAndFound);
}else{
zTbl = recoverMPrintf(p, "%s_%d", p->zLostAndFound, ii);
}
if( p->errCode==SQLITE_OK ){
sqlite3_bind_text(pProbe, 1, zTbl, -1, SQLITE_STATIC);
if( SQLITE_ROW==sqlite3_step(pProbe) ){
bFail = 1;
}
recoverReset(p, pProbe);
}
if( bFail ){
sqlite3_clear_bindings(pProbe);
sqlite3_free(zTbl);
zTbl = 0;
}
}
recoverFinalize(p, pProbe);
if( zTbl ){
const char *zSep = 0;
char *zField = 0;
char *zSql = 0;
zSep = "rootpgno INTEGER, pgno INTEGER, nfield INTEGER, id INTEGER, ";
for(ii=0; p->errCode==SQLITE_OK && ii<nField; ii++){
zField = recoverMPrintf(p, "%z%sc%d", zField, zSep, ii);
zSep = ", ";
}
zSql = recoverMPrintf(p, "CREATE TABLE %s(%s)", zTbl, zField);
sqlite3_free(zField);
recoverExec(p, p->dbOut, zSql);
recoverSqlCallback(p, zSql);
sqlite3_free(zSql);
}else if( p->errCode==SQLITE_OK ){
recoverError(
p, SQLITE_ERROR, "failed to create %s output table", p->zLostAndFound
);
}
return zTbl;
}
/*
** Synthesize and prepare an INSERT statement to write to the lost_and_found
** table in the output database. The name of the table is zTab, and it has
** nField c* fields.
*/
static sqlite3_stmt *recoverLostAndFoundInsert(
sqlite3_recover *p,
const char *zTab,
int nField
){
int nTotal = nField + 4;
int ii;
char *zBind = 0;
sqlite3_stmt *pRet = 0;
if( p->xSql==0 ){
for(ii=0; ii<nTotal; ii++){
zBind = recoverMPrintf(p, "%z%s?", zBind, zBind?", ":"", ii);
}
pRet = recoverPreparePrintf(
p, p->dbOut, "INSERT INTO %s VALUES(%s)", zTab, zBind
);
}else{
const char *zSep = "";
for(ii=0; ii<nTotal; ii++){
zBind = recoverMPrintf(p, "%z%squote(?)", zBind, zSep);
zSep = "|| ', ' ||";
}
pRet = recoverPreparePrintf(
p, p->dbOut, "SELECT 'INSERT INTO %s VALUES(' || %s || ')'", zTab, zBind
);
}
sqlite3_free(zBind);
return pRet;
}
/*
** Input database page iPg contains data that will be written to the
** lost-and-found table of the output database. This function attempts
** to identify the root page of the tree that page iPg belonged to.
** If successful, it sets output variable (*piRoot) to the page number
** of the root page and returns SQLITE_OK. Otherwise, if an error occurs,
** an SQLite error code is returned and the final value of *piRoot
** undefined.
*/
static int recoverLostAndFoundFindRoot(
sqlite3_recover *p,
i64 iPg,
i64 *piRoot
){
RecoverStateLAF *pLaf = &p->laf;
if( pLaf->pFindRoot==0 ){
pLaf->pFindRoot = recoverPrepare(p, p->dbOut,
"WITH RECURSIVE p(pgno) AS ("
" SELECT ?"
" UNION"
" SELECT parent FROM recovery.map AS m, p WHERE m.pgno=p.pgno"
") "
"SELECT p.pgno FROM p, recovery.map m WHERE m.pgno=p.pgno "
" AND m.parent IS NULL"
);
}
if( p->errCode==SQLITE_OK ){
sqlite3_bind_int64(pLaf->pFindRoot, 1, iPg);
if( sqlite3_step(pLaf->pFindRoot)==SQLITE_ROW ){
*piRoot = sqlite3_column_int64(pLaf->pFindRoot, 0);
}else{
*piRoot = iPg;
}
recoverReset(p, pLaf->pFindRoot);
}
return p->errCode;
}
/*
** Recover data from page iPage of the input database and write it to
** the lost-and-found table in the output database.
*/
static void recoverLostAndFoundOnePage(sqlite3_recover *p, i64 iPage){
RecoverStateLAF *pLaf = &p->laf;
sqlite3_value **apVal = pLaf->apVal;
sqlite3_stmt *pPageData = pLaf->pPageData;
sqlite3_stmt *pInsert = pLaf->pInsert;
int nVal = -1;
int iPrevCell = 0;
i64 iRoot = 0;
int bHaveRowid = 0;
i64 iRowid = 0;
int ii = 0;
if( recoverLostAndFoundFindRoot(p, iPage, &iRoot) ) return;
sqlite3_bind_int64(pPageData, 1, iPage);
while( p->errCode==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPageData) ){
int iCell = sqlite3_column_int64(pPageData, 0);
int iField = sqlite3_column_int64(pPageData, 1);
if( iPrevCell!=iCell && nVal>=0 ){
/* Insert the new row */
sqlite3_bind_int64(pInsert, 1, iRoot); /* rootpgno */
sqlite3_bind_int64(pInsert, 2, iPage); /* pgno */
sqlite3_bind_int(pInsert, 3, nVal); /* nfield */
if( bHaveRowid ){
sqlite3_bind_int64(pInsert, 4, iRowid); /* id */
}
for(ii=0; ii<nVal; ii++){
recoverBindValue(p, pInsert, 5+ii, apVal[ii]);
}
if( sqlite3_step(pInsert)==SQLITE_ROW ){
recoverSqlCallback(p, (const char*)sqlite3_column_text(pInsert, 0));
}
recoverReset(p, pInsert);
/* Discard the accumulated row data */
for(ii=0; ii<nVal; ii++){
sqlite3_value_free(apVal[ii]);
apVal[ii] = 0;
}
sqlite3_clear_bindings(pInsert);
bHaveRowid = 0;
nVal = -1;
}
if( iCell<0 ) break;
if( iField<0 ){
assert( nVal==-1 );
iRowid = sqlite3_column_int64(pPageData, 2);
bHaveRowid = 1;
nVal = 0;
}else if( iField<pLaf->nMaxField ){
sqlite3_value *pVal = sqlite3_column_value(pPageData, 2);
apVal[iField] = sqlite3_value_dup(pVal);
assert( iField==nVal || (nVal==-1 && iField==0) );
nVal = iField+1;
if( apVal[iField]==0 ){
recoverError(p, SQLITE_NOMEM, 0);
}
}
iPrevCell = iCell;
}
recoverReset(p, pPageData);
for(ii=0; ii<nVal; ii++){
sqlite3_value_free(apVal[ii]);
apVal[ii] = 0;
}
}
/*
** Perform one step (sqlite3_recover_step()) of work for the connection
** passed as the only argument, which is guaranteed to be in
** RECOVER_STATE_LOSTANDFOUND3 state - during which the lost-and-found
** table of the output database is populated with recovered data that can
** not be assigned to any recovered schema object.
*/
static int recoverLostAndFound3Step(sqlite3_recover *p){
RecoverStateLAF *pLaf = &p->laf;
if( p->errCode==SQLITE_OK ){
if( pLaf->pInsert==0 ){
return SQLITE_DONE;
}else{
if( p->errCode==SQLITE_OK ){
int res = sqlite3_step(pLaf->pAllPage);
if( res==SQLITE_ROW ){
i64 iPage = sqlite3_column_int64(pLaf->pAllPage, 0);
if( recoverBitmapQuery(pLaf->pUsed, iPage)==0 ){
recoverLostAndFoundOnePage(p, iPage);
}
}else{
recoverReset(p, pLaf->pAllPage);
return SQLITE_DONE;
}
}
}
}
return SQLITE_OK;
}
/*
** Initialize resources required in RECOVER_STATE_LOSTANDFOUND3
** state - during which the lost-and-found table of the output database
** is populated with recovered data that can not be assigned to any
** recovered schema object.
*/
static void recoverLostAndFound3Init(sqlite3_recover *p){
RecoverStateLAF *pLaf = &p->laf;
if( pLaf->nMaxField>0 ){
char *zTab = 0; /* Name of lost_and_found table */
zTab = recoverLostAndFoundCreate(p, pLaf->nMaxField);
pLaf->pInsert = recoverLostAndFoundInsert(p, zTab, pLaf->nMaxField);
sqlite3_free(zTab);
pLaf->pAllPage = recoverPreparePrintf(p, p->dbOut,
"WITH RECURSIVE seq(ii) AS ("
" SELECT 1 UNION ALL SELECT ii+1 FROM seq WHERE ii<%lld"
")"
"SELECT ii FROM seq" , p->laf.nPg
);
pLaf->pPageData = recoverPrepare(p, p->dbOut,
"SELECT cell, field, value "
"FROM sqlite_dbdata('getpage()') d WHERE d.pgno=? "
"UNION ALL "
"SELECT -1, -1, -1"
);
pLaf->apVal = (sqlite3_value**)recoverMalloc(p,
pLaf->nMaxField*sizeof(sqlite3_value*)
);
}
}
/*
** Initialize resources required in RECOVER_STATE_WRITING state - during which
** tables recovered from the schema of the input database are populated with
** recovered data.
*/
static int recoverWriteDataInit(sqlite3_recover *p){
RecoverStateW1 *p1 = &p->w1;
RecoverTable *pTbl = 0;
int nByte = 0;
/* Figure out the maximum number of columns for any table in the schema */
assert( p1->nMax==0 );
for(pTbl=p->pTblList; pTbl; pTbl=pTbl->pNext){
if( pTbl->nCol>p1->nMax ) p1->nMax = pTbl->nCol;
}
/* Allocate an array of (sqlite3_value*) in which to accumulate the values
** that will be written to the output database in a single row. */
nByte = sizeof(sqlite3_value*) * (p1->nMax+1);
p1->apVal = (sqlite3_value**)recoverMalloc(p, nByte);
if( p1->apVal==0 ) return p->errCode;
/* Prepare the SELECT to loop through schema tables (pTbls) and the SELECT
** to loop through cells that appear to belong to a single table (pSel). */
p1->pTbls = recoverPrepare(p, p->dbOut,
"SELECT rootpage FROM recovery.schema "
" WHERE type='table' AND (sql NOT LIKE 'create virtual%')"
" ORDER BY (tbl_name='sqlite_sequence') ASC"
);
p1->pSel = recoverPrepare(p, p->dbOut,
"WITH RECURSIVE pages(page) AS ("
" SELECT ?1"
" UNION"
" SELECT child FROM sqlite_dbptr('getpage()'), pages "
" WHERE pgno=page"
") "
"SELECT page, cell, field, value "
"FROM sqlite_dbdata('getpage()') d, pages p WHERE p.page=d.pgno "
"UNION ALL "
"SELECT 0, 0, 0, 0"
);
return p->errCode;
}
/*
** Clean up resources allocated by recoverWriteDataInit() (stuff in
** sqlite3_recover.w1).
*/
static void recoverWriteDataCleanup(sqlite3_recover *p){
RecoverStateW1 *p1 = &p->w1;
int ii;
for(ii=0; ii<p1->nVal; ii++){
sqlite3_value_free(p1->apVal[ii]);
}
sqlite3_free(p1->apVal);
recoverFinalize(p, p1->pInsert);
recoverFinalize(p, p1->pTbls);
recoverFinalize(p, p1->pSel);
memset(p1, 0, sizeof(*p1));
}
/*
** Perform one step (sqlite3_recover_step()) of work for the connection
** passed as the only argument, which is guaranteed to be in
** RECOVER_STATE_WRITING state - during which tables recovered from the
** schema of the input database are populated with recovered data.
*/
static int recoverWriteDataStep(sqlite3_recover *p){
RecoverStateW1 *p1 = &p->w1;
sqlite3_stmt *pSel = p1->pSel;
sqlite3_value **apVal = p1->apVal;
if( p->errCode==SQLITE_OK && p1->pTab==0 ){
if( sqlite3_step(p1->pTbls)==SQLITE_ROW ){
i64 iRoot = sqlite3_column_int64(p1->pTbls, 0);
p1->pTab = recoverFindTable(p, iRoot);
recoverFinalize(p, p1->pInsert);
p1->pInsert = 0;
/* If this table is unknown, return early. The caller will invoke this
** function again and it will move on to the next table. */
if( p1->pTab==0 ) return p->errCode;
/* If this is the sqlite_sequence table, delete any rows added by
** earlier INSERT statements on tables with AUTOINCREMENT primary
** keys before recovering its contents. The p1->pTbls SELECT statement
** is rigged to deliver "sqlite_sequence" last of all, so we don't
** worry about it being modified after it is recovered. */
if( sqlite3_stricmp("sqlite_sequence", p1->pTab->zTab)==0 ){
recoverExec(p, p->dbOut, "DELETE FROM sqlite_sequence");
recoverSqlCallback(p, "DELETE FROM sqlite_sequence");
}
/* Bind the root page of this table within the original database to
** SELECT statement p1->pSel. The SELECT statement will then iterate
** through cells that look like they belong to table pTab. */
sqlite3_bind_int64(pSel, 1, iRoot);
p1->nVal = 0;
p1->bHaveRowid = 0;
p1->iPrevPage = -1;
p1->iPrevCell = -1;
}else{
return SQLITE_DONE;
}
}
assert( p->errCode!=SQLITE_OK || p1->pTab );
if( p->errCode==SQLITE_OK && sqlite3_step(pSel)==SQLITE_ROW ){
RecoverTable *pTab = p1->pTab;
i64 iPage = sqlite3_column_int64(pSel, 0);
int iCell = sqlite3_column_int(pSel, 1);
int iField = sqlite3_column_int(pSel, 2);
sqlite3_value *pVal = sqlite3_column_value(pSel, 3);
int bNewCell = (p1->iPrevPage!=iPage || p1->iPrevCell!=iCell);
assert( bNewCell==0 || (iField==-1 || iField==0) );
assert( bNewCell || iField==p1->nVal || p1->nVal==pTab->nCol );
if( bNewCell ){
int ii = 0;
if( p1->nVal>=0 ){
if( p1->pInsert==0 || p1->nVal!=p1->nInsert ){
recoverFinalize(p, p1->pInsert);
p1->pInsert = recoverInsertStmt(p, pTab, p1->nVal);
p1->nInsert = p1->nVal;
}
if( p1->nVal>0 ){
sqlite3_stmt *pInsert = p1->pInsert;
for(ii=0; ii<pTab->nCol; ii++){
RecoverColumn *pCol = &pTab->aCol[ii];
int iBind = pCol->iBind;
if( iBind>0 ){
if( pCol->bIPK ){
sqlite3_bind_int64(pInsert, iBind, p1->iRowid);
}else if( pCol->iField<p1->nVal ){
recoverBindValue(p, pInsert, iBind, apVal[pCol->iField]);
}
}
}
if( p->bRecoverRowid && pTab->iRowidBind>0 && p1->bHaveRowid ){
sqlite3_bind_int64(pInsert, pTab->iRowidBind, p1->iRowid);
}
if( SQLITE_ROW==sqlite3_step(pInsert) ){
const char *z = (const char*)sqlite3_column_text(pInsert, 0);
recoverSqlCallback(p, z);
}
recoverReset(p, pInsert);
assert( p->errCode || pInsert );
if( pInsert ) sqlite3_clear_bindings(pInsert);
}
}
for(ii=0; ii<p1->nVal; ii++){
sqlite3_value_free(apVal[ii]);
apVal[ii] = 0;
}
p1->nVal = -1;
p1->bHaveRowid = 0;
}
if( iPage!=0 ){
if( iField<0 ){
p1->iRowid = sqlite3_column_int64(pSel, 3);
assert( p1->nVal==-1 );
p1->nVal = 0;
p1->bHaveRowid = 1;
}else if( iField<pTab->nCol ){
assert( apVal[iField]==0 );
apVal[iField] = sqlite3_value_dup( pVal );
if( apVal[iField]==0 ){
recoverError(p, SQLITE_NOMEM, 0);
}
p1->nVal = iField+1;
}
p1->iPrevCell = iCell;
p1->iPrevPage = iPage;
}
}else{
recoverReset(p, pSel);
p1->pTab = 0;
}
return p->errCode;
}
/*
** Initialize resources required by sqlite3_recover_step() in
** RECOVER_STATE_LOSTANDFOUND1 state - during which the set of pages not
** already allocated to a recovered schema element is determined.
*/
static void recoverLostAndFound1Init(sqlite3_recover *p){
RecoverStateLAF *pLaf = &p->laf;
sqlite3_stmt *pStmt = 0;
assert( p->laf.pUsed==0 );
pLaf->nPg = recoverPageCount(p);
pLaf->pUsed = recoverBitmapAlloc(p, pLaf->nPg);
/* Prepare a statement to iterate through all pages that are part of any tree
** in the recoverable part of the input database schema to the bitmap. And,
** if !p->bFreelistCorrupt, add all pages that appear to be part of the
** freelist. */
pStmt = recoverPrepare(
p, p->dbOut,
"WITH trunk(pgno) AS ("
" SELECT read_i32(getpage(1), 8) AS x WHERE x>0"
" UNION"
" SELECT read_i32(getpage(trunk.pgno), 0) AS x FROM trunk WHERE x>0"
"),"
"trunkdata(pgno, data) AS ("
" SELECT pgno, getpage(pgno) FROM trunk"
"),"
"freelist(data, n, freepgno) AS ("
" SELECT data, min(16384, read_i32(data, 1)-1), pgno FROM trunkdata"
" UNION ALL"
" SELECT data, n-1, read_i32(data, 2+n) FROM freelist WHERE n>=0"
"),"
""
"roots(r) AS ("
" SELECT 1 UNION ALL"
" SELECT rootpage FROM recovery.schema WHERE rootpage>0"
"),"
"used(page) AS ("
" SELECT r FROM roots"
" UNION"
" SELECT child FROM sqlite_dbptr('getpage()'), used "
" WHERE pgno=page"
") "
"SELECT page FROM used"
" UNION ALL "
"SELECT freepgno FROM freelist WHERE NOT ?"
);
if( pStmt ) sqlite3_bind_int(pStmt, 1, p->bFreelistCorrupt);
pLaf->pUsedPages = pStmt;
}
/*
** Perform one step (sqlite3_recover_step()) of work for the connection
** passed as the only argument, which is guaranteed to be in
** RECOVER_STATE_LOSTANDFOUND1 state - during which the set of pages not
** already allocated to a recovered schema element is determined.
*/
static int recoverLostAndFound1Step(sqlite3_recover *p){
RecoverStateLAF *pLaf = &p->laf;
int rc = p->errCode;
if( rc==SQLITE_OK ){
rc = sqlite3_step(pLaf->pUsedPages);
if( rc==SQLITE_ROW ){
i64 iPg = sqlite3_column_int64(pLaf->pUsedPages, 0);
recoverBitmapSet(pLaf->pUsed, iPg);
rc = SQLITE_OK;
}else{
recoverFinalize(p, pLaf->pUsedPages);
pLaf->pUsedPages = 0;
}
}
return rc;
}
/*
** Initialize resources required by RECOVER_STATE_LOSTANDFOUND2
** state - during which the pages identified in RECOVER_STATE_LOSTANDFOUND1
** are sorted into sets that likely belonged to the same database tree.
*/
static void recoverLostAndFound2Init(sqlite3_recover *p){
RecoverStateLAF *pLaf = &p->laf;
assert( p->laf.pAllAndParent==0 );
assert( p->laf.pMapInsert==0 );
assert( p->laf.pMaxField==0 );
assert( p->laf.nMaxField==0 );
pLaf->pMapInsert = recoverPrepare(p, p->dbOut,
"INSERT OR IGNORE INTO recovery.map(pgno, parent) VALUES(?, ?)"
);
pLaf->pAllAndParent = recoverPreparePrintf(p, p->dbOut,
"WITH RECURSIVE seq(ii) AS ("
" SELECT 1 UNION ALL SELECT ii+1 FROM seq WHERE ii<%lld"
")"
"SELECT pgno, child FROM sqlite_dbptr('getpage()') "
" UNION ALL "
"SELECT NULL, ii FROM seq", p->laf.nPg
);
pLaf->pMaxField = recoverPreparePrintf(p, p->dbOut,
"SELECT max(field)+1 FROM sqlite_dbdata('getpage') WHERE pgno = ?"
);
}
/*
** Perform one step (sqlite3_recover_step()) of work for the connection
** passed as the only argument, which is guaranteed to be in
** RECOVER_STATE_LOSTANDFOUND2 state - during which the pages identified
** in RECOVER_STATE_LOSTANDFOUND1 are sorted into sets that likely belonged
** to the same database tree.
*/
static int recoverLostAndFound2Step(sqlite3_recover *p){
RecoverStateLAF *pLaf = &p->laf;
if( p->errCode==SQLITE_OK ){
int res = sqlite3_step(pLaf->pAllAndParent);
if( res==SQLITE_ROW ){
i64 iChild = sqlite3_column_int(pLaf->pAllAndParent, 1);
if( recoverBitmapQuery(pLaf->pUsed, iChild)==0 ){
sqlite3_bind_int64(pLaf->pMapInsert, 1, iChild);
sqlite3_bind_value(pLaf->pMapInsert, 2,
sqlite3_column_value(pLaf->pAllAndParent, 0)
);
sqlite3_step(pLaf->pMapInsert);
recoverReset(p, pLaf->pMapInsert);
sqlite3_bind_int64(pLaf->pMaxField, 1, iChild);
if( SQLITE_ROW==sqlite3_step(pLaf->pMaxField) ){
int nMax = sqlite3_column_int(pLaf->pMaxField, 0);
if( nMax>pLaf->nMaxField ) pLaf->nMaxField = nMax;
}
recoverReset(p, pLaf->pMaxField);
}
}else{
recoverFinalize(p, pLaf->pAllAndParent);
pLaf->pAllAndParent =0;
return SQLITE_DONE;
}
}
return p->errCode;
}
/*
** Free all resources allocated as part of sqlite3_recover_step() calls
** in one of the RECOVER_STATE_LOSTANDFOUND[123] states.
*/
static void recoverLostAndFoundCleanup(sqlite3_recover *p){
recoverBitmapFree(p->laf.pUsed);
p->laf.pUsed = 0;
sqlite3_finalize(p->laf.pUsedPages);
sqlite3_finalize(p->laf.pAllAndParent);
sqlite3_finalize(p->laf.pMapInsert);
sqlite3_finalize(p->laf.pMaxField);
sqlite3_finalize(p->laf.pFindRoot);
sqlite3_finalize(p->laf.pInsert);
sqlite3_finalize(p->laf.pAllPage);
sqlite3_finalize(p->laf.pPageData);
p->laf.pUsedPages = 0;
p->laf.pAllAndParent = 0;
p->laf.pMapInsert = 0;
p->laf.pMaxField = 0;
p->laf.pFindRoot = 0;
p->laf.pInsert = 0;
p->laf.pAllPage = 0;
p->laf.pPageData = 0;
sqlite3_free(p->laf.apVal);
p->laf.apVal = 0;
}
/*
** Free all resources allocated as part of sqlite3_recover_step() calls.
*/
static void recoverFinalCleanup(sqlite3_recover *p){
RecoverTable *pTab = 0;
RecoverTable *pNext = 0;
recoverWriteDataCleanup(p);
recoverLostAndFoundCleanup(p);
for(pTab=p->pTblList; pTab; pTab=pNext){
pNext = pTab->pNext;
sqlite3_free(pTab);
}
p->pTblList = 0;
sqlite3_finalize(p->pGetPage);
p->pGetPage = 0;
{
#ifndef NDEBUG
int res =
#endif
sqlite3_close(p->dbOut);
assert( res==SQLITE_OK );
}
p->dbOut = 0;
}
/*
** Decode and return an unsigned 16-bit big-endian integer value from
** buffer a[].
*/
static u32 recoverGetU16(const u8 *a){
return (((u32)a[0])<<8) + ((u32)a[1]);
}
/*
** Decode and return an unsigned 32-bit big-endian integer value from
** buffer a[].
*/
static u32 recoverGetU32(const u8 *a){
return (((u32)a[0])<<24) + (((u32)a[1])<<16) + (((u32)a[2])<<8) + ((u32)a[3]);
}
/*
** Decode an SQLite varint from buffer a[]. Write the decoded value to (*pVal)
** and return the number of bytes consumed.
*/
static int recoverGetVarint(const u8 *a, i64 *pVal){
sqlite3_uint64 u = 0;
int i;
for(i=0; i<8; i++){
u = (u<<7) + (a[i]&0x7f);
if( (a[i]&0x80)==0 ){ *pVal = (sqlite3_int64)u; return i+1; }
}
u = (u<<8) + (a[i]&0xff);
*pVal = (sqlite3_int64)u;
return 9;
}
/*
** The second argument points to a buffer n bytes in size. If this buffer
** or a prefix thereof appears to contain a well-formed SQLite b-tree page,
** return the page-size in bytes. Otherwise, if the buffer does not
** appear to contain a well-formed b-tree page, return 0.
*/
static int recoverIsValidPage(u8 *aTmp, const u8 *a, int n){
u8 *aUsed = aTmp;
int nFrag = 0;
int nActual = 0;
int iFree = 0;
int nCell = 0; /* Number of cells on page */
int iCellOff = 0; /* Offset of cell array in page */
int iContent = 0;
int eType = 0;
int ii = 0;
eType = (int)a[0];
if( eType!=0x02 && eType!=0x05 && eType!=0x0A && eType!=0x0D ) return 0;
iFree = (int)recoverGetU16(&a[1]);
nCell = (int)recoverGetU16(&a[3]);
iContent = (int)recoverGetU16(&a[5]);
if( iContent==0 ) iContent = 65536;
nFrag = (int)a[7];
if( iContent>n ) return 0;
memset(aUsed, 0, n);
memset(aUsed, 0xFF, iContent);
/* Follow the free-list. This is the same format for all b-tree pages. */
if( iFree && iFree<=iContent ) return 0;
while( iFree ){
int iNext = 0;
int nByte = 0;
if( iFree>(n-4) ) return 0;
iNext = recoverGetU16(&a[iFree]);
nByte = recoverGetU16(&a[iFree+2]);
if( iFree+nByte>n ) return 0;
if( iNext && iNext<iFree+nByte ) return 0;
memset(&aUsed[iFree], 0xFF, nByte);
iFree = iNext;
}
/* Run through the cells */
if( eType==0x02 || eType==0x05 ){
iCellOff = 12;
}else{
iCellOff = 8;
}
if( (iCellOff + 2*nCell)>iContent ) return 0;
for(ii=0; ii<nCell; ii++){
int iByte;
i64 nPayload = 0;
int nByte = 0;
int iOff = recoverGetU16(&a[iCellOff + 2*ii]);
if( iOff<iContent || iOff>n ){
return 0;
}
if( eType==0x05 || eType==0x02 ) nByte += 4;
nByte += recoverGetVarint(&a[iOff+nByte], &nPayload);
if( eType==0x0D ){
i64 dummy = 0;
nByte += recoverGetVarint(&a[iOff+nByte], &dummy);
}
if( eType!=0x05 ){
int X = (eType==0x0D) ? n-35 : (((n-12)*64/255)-23);
int M = ((n-12)*32/255)-23;
int K = M+((nPayload-M)%(n-4));
if( nPayload<X ){
nByte += nPayload;
}else if( K<=X ){
nByte += K+4;
}else{
nByte += M+4;
}
}
if( iOff+nByte>n ){
return 0;
}
for(iByte=iOff; iByte<(iOff+nByte); iByte++){
if( aUsed[iByte]!=0 ){
return 0;
}
aUsed[iByte] = 0xFF;
}
}
nActual = 0;
for(ii=0; ii<n; ii++){
if( aUsed[ii]==0 ) nActual++;
}
return (nActual==nFrag);
}
static int recoverVfsClose(sqlite3_file*);
static int recoverVfsRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
static int recoverVfsWrite(sqlite3_file*, const void*, int, sqlite3_int64);
static int recoverVfsTruncate(sqlite3_file*, sqlite3_int64 size);
static int recoverVfsSync(sqlite3_file*, int flags);
static int recoverVfsFileSize(sqlite3_file*, sqlite3_int64 *pSize);
static int recoverVfsLock(sqlite3_file*, int);
static int recoverVfsUnlock(sqlite3_file*, int);
static int recoverVfsCheckReservedLock(sqlite3_file*, int *pResOut);
static int recoverVfsFileControl(sqlite3_file*, int op, void *pArg);
static int recoverVfsSectorSize(sqlite3_file*);
static int recoverVfsDeviceCharacteristics(sqlite3_file*);
static int recoverVfsShmMap(sqlite3_file*, int, int, int, void volatile**);
static int recoverVfsShmLock(sqlite3_file*, int offset, int n, int flags);
static void recoverVfsShmBarrier(sqlite3_file*);
static int recoverVfsShmUnmap(sqlite3_file*, int deleteFlag);
static int recoverVfsFetch(sqlite3_file*, sqlite3_int64, int, void**);
static int recoverVfsUnfetch(sqlite3_file *pFd, sqlite3_int64 iOff, void *p);
static sqlite3_io_methods recover_methods = {
2, /* iVersion */
recoverVfsClose,
recoverVfsRead,
recoverVfsWrite,
recoverVfsTruncate,
recoverVfsSync,
recoverVfsFileSize,
recoverVfsLock,
recoverVfsUnlock,
recoverVfsCheckReservedLock,
recoverVfsFileControl,
recoverVfsSectorSize,
recoverVfsDeviceCharacteristics,
recoverVfsShmMap,
recoverVfsShmLock,
recoverVfsShmBarrier,
recoverVfsShmUnmap,
recoverVfsFetch,
recoverVfsUnfetch
};
static int recoverVfsClose(sqlite3_file *pFd){
assert( pFd->pMethods!=&recover_methods );
return pFd->pMethods->xClose(pFd);
}
/*
** Write value v to buffer a[] as a 16-bit big-endian unsigned integer.
*/
static void recoverPutU16(u8 *a, u32 v){
a[0] = (v>>8) & 0x00FF;
a[1] = (v>>0) & 0x00FF;
}
/*
** Write value v to buffer a[] as a 32-bit big-endian unsigned integer.
*/
static void recoverPutU32(u8 *a, u32 v){
a[0] = (v>>24) & 0x00FF;
a[1] = (v>>16) & 0x00FF;
a[2] = (v>>8) & 0x00FF;
a[3] = (v>>0) & 0x00FF;
}
/*
** Detect the page-size of the database opened by file-handle pFd by
** searching the first part of the file for a well-formed SQLite b-tree
** page. If parameter nReserve is non-zero, then as well as searching for
** a b-tree page with zero reserved bytes, this function searches for one
** with nReserve reserved bytes at the end of it.
**
** If successful, set variable p->detected_pgsz to the detected page-size
** in bytes and return SQLITE_OK. Or, if no error occurs but no valid page
** can be found, return SQLITE_OK but leave p->detected_pgsz set to 0. Or,
** if an error occurs (e.g. an IO or OOM error), then an SQLite error code
** is returned. The final value of p->detected_pgsz is undefined in this
** case.
*/
static int recoverVfsDetectPagesize(
sqlite3_recover *p, /* Recover handle */
sqlite3_file *pFd, /* File-handle open on input database */
u32 nReserve, /* Possible nReserve value */
i64 nSz /* Size of database file in bytes */
){
int rc = SQLITE_OK;
const int nMin = 512;
const int nMax = 65536;
const int nMaxBlk = 4;
u32 pgsz = 0;
int iBlk = 0;
u8 *aPg = 0;
u8 *aTmp = 0;
int nBlk = 0;
aPg = (u8*)sqlite3_malloc(2*nMax);
if( aPg==0 ) return SQLITE_NOMEM;
aTmp = &aPg[nMax];
nBlk = (nSz+nMax-1)/nMax;
if( nBlk>nMaxBlk ) nBlk = nMaxBlk;
do {
for(iBlk=0; rc==SQLITE_OK && iBlk<nBlk; iBlk++){
int nByte = (nSz>=((iBlk+1)*nMax)) ? nMax : (nSz % nMax);
memset(aPg, 0, nMax);
rc = pFd->pMethods->xRead(pFd, aPg, nByte, iBlk*nMax);
if( rc==SQLITE_OK ){
int pgsz2;
for(pgsz2=(pgsz ? pgsz*2 : nMin); pgsz2<=nMax; pgsz2=pgsz2*2){
int iOff;
for(iOff=0; iOff<nMax; iOff+=pgsz2){
if( recoverIsValidPage(aTmp, &aPg[iOff], pgsz2-nReserve) ){
pgsz = pgsz2;
break;
}
}
}
}
}
if( pgsz>(u32)p->detected_pgsz ){
p->detected_pgsz = pgsz;
p->nReserve = nReserve;
}
if( nReserve==0 ) break;
nReserve = 0;
}while( 1 );
p->detected_pgsz = pgsz;
sqlite3_free(aPg);
return rc;
}
/*
** The xRead() method of the wrapper VFS. This is used to intercept calls
** to read page 1 of the input database.
*/
static int recoverVfsRead(sqlite3_file *pFd, void *aBuf, int nByte, i64 iOff){
int rc = SQLITE_OK;
if( pFd->pMethods==&recover_methods ){
pFd->pMethods = recover_g.pMethods;
rc = pFd->pMethods->xRead(pFd, aBuf, nByte, iOff);
if( nByte==16 ){
sqlite3_randomness(16, aBuf);
}else
if( rc==SQLITE_OK && iOff==0 && nByte>=108 ){
/* Ensure that the database has a valid header file. The only fields
** that really matter to recovery are:
**
** + Database page size (16-bits at offset 16)
** + Size of db in pages (32-bits at offset 28)
** + Database encoding (32-bits at offset 56)
**
** Also preserved are:
**
** + first freelist page (32-bits at offset 32)
** + size of freelist (32-bits at offset 36)
**
** We also try to preserve the auto-vacuum, incr-value, user-version
** and application-id fields - all 32 bit quantities at offsets
** 52, 60, 64 and 68. All other fields are set to known good values.
**
** Byte offset 105 should also contain the page-size as a 16-bit
** integer.
*/
const int aPreserve[] = {32, 36, 52, 60, 64, 68};
u8 aHdr[108] = {
0x53, 0x51, 0x4c, 0x69, 0x74, 0x65, 0x20, 0x66,
0x6f, 0x72, 0x6d, 0x61, 0x74, 0x20, 0x33, 0x00,
0xFF, 0xFF, 0x01, 0x01, 0x00, 0x40, 0x20, 0x20,
0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
0x00, 0x00, 0x10, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x2e, 0x5b, 0x30,
0x0D, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00
};
u8 *a = (u8*)aBuf;
u32 pgsz = recoverGetU16(&a[16]);
u32 nReserve = a[20];
u32 enc = recoverGetU32(&a[56]);
u32 dbsz = 0;
i64 dbFileSize = 0;
int ii;
sqlite3_recover *p = recover_g.p;
if( pgsz==0x01 ) pgsz = 65536;
rc = pFd->pMethods->xFileSize(pFd, &dbFileSize);
if( rc==SQLITE_OK && p->detected_pgsz==0 ){
rc = recoverVfsDetectPagesize(p, pFd, nReserve, dbFileSize);
}
if( p->detected_pgsz ){
pgsz = p->detected_pgsz;
nReserve = p->nReserve;
}
if( pgsz ){
dbsz = dbFileSize / pgsz;
}
if( enc!=SQLITE_UTF8 && enc!=SQLITE_UTF16BE && enc!=SQLITE_UTF16LE ){
enc = SQLITE_UTF8;
}
sqlite3_free(p->pPage1Cache);
p->pPage1Cache = 0;
p->pPage1Disk = 0;
p->pgsz = nByte;
p->pPage1Cache = (u8*)recoverMalloc(p, nByte*2);
if( p->pPage1Cache ){
p->pPage1Disk = &p->pPage1Cache[nByte];
memcpy(p->pPage1Disk, aBuf, nByte);
recoverPutU32(&aHdr[28], dbsz);
recoverPutU32(&aHdr[56], enc);
recoverPutU16(&aHdr[105], pgsz-nReserve);
if( pgsz==65536 ) pgsz = 1;
recoverPutU16(&aHdr[16], pgsz);
aHdr[20] = nReserve;
for(ii=0; ii<sizeof(aPreserve)/sizeof(aPreserve[0]); ii++){
memcpy(&aHdr[aPreserve[ii]], &a[aPreserve[ii]], 4);
}
memcpy(aBuf, aHdr, sizeof(aHdr));
memset(&((u8*)aBuf)[sizeof(aHdr)], 0, nByte-sizeof(aHdr));
memcpy(p->pPage1Cache, aBuf, nByte);
}else{
rc = p->errCode;
}
}
pFd->pMethods = &recover_methods;
}else{
rc = pFd->pMethods->xRead(pFd, aBuf, nByte, iOff);
}
return rc;
}
/*
** Used to make sqlite3_io_methods wrapper methods less verbose.
*/
#define RECOVER_VFS_WRAPPER(code) \
int rc = SQLITE_OK; \
if( pFd->pMethods==&recover_methods ){ \
pFd->pMethods = recover_g.pMethods; \
rc = code; \
pFd->pMethods = &recover_methods; \
}else{ \
rc = code; \
} \
return rc;
/*
** Methods of the wrapper VFS. All methods except for xRead() and xClose()
** simply uninstall the sqlite3_io_methods wrapper, invoke the equivalent
** method on the lower level VFS, then reinstall the wrapper before returning.
** Those that return an integer value use the RECOVER_VFS_WRAPPER macro.
*/
static int recoverVfsWrite(
sqlite3_file *pFd, const void *aBuf, int nByte, i64 iOff
){
RECOVER_VFS_WRAPPER (
pFd->pMethods->xWrite(pFd, aBuf, nByte, iOff)
);
}
static int recoverVfsTruncate(sqlite3_file *pFd, sqlite3_int64 size){
RECOVER_VFS_WRAPPER (
pFd->pMethods->xTruncate(pFd, size)
);
}
static int recoverVfsSync(sqlite3_file *pFd, int flags){
RECOVER_VFS_WRAPPER (
pFd->pMethods->xSync(pFd, flags)
);
}
static int recoverVfsFileSize(sqlite3_file *pFd, sqlite3_int64 *pSize){
RECOVER_VFS_WRAPPER (
pFd->pMethods->xFileSize(pFd, pSize)
);
}
static int recoverVfsLock(sqlite3_file *pFd, int eLock){
RECOVER_VFS_WRAPPER (
pFd->pMethods->xLock(pFd, eLock)
);
}
static int recoverVfsUnlock(sqlite3_file *pFd, int eLock){
RECOVER_VFS_WRAPPER (
pFd->pMethods->xUnlock(pFd, eLock)
);
}
static int recoverVfsCheckReservedLock(sqlite3_file *pFd, int *pResOut){
RECOVER_VFS_WRAPPER (
pFd->pMethods->xCheckReservedLock(pFd, pResOut)
);
}
static int recoverVfsFileControl(sqlite3_file *pFd, int op, void *pArg){
RECOVER_VFS_WRAPPER (
(pFd->pMethods ? pFd->pMethods->xFileControl(pFd, op, pArg) : SQLITE_NOTFOUND)
);
}
static int recoverVfsSectorSize(sqlite3_file *pFd){
RECOVER_VFS_WRAPPER (
pFd->pMethods->xSectorSize(pFd)
);
}
static int recoverVfsDeviceCharacteristics(sqlite3_file *pFd){
RECOVER_VFS_WRAPPER (
pFd->pMethods->xDeviceCharacteristics(pFd)
);
}
static int recoverVfsShmMap(
sqlite3_file *pFd, int iPg, int pgsz, int bExtend, void volatile **pp
){
RECOVER_VFS_WRAPPER (
pFd->pMethods->xShmMap(pFd, iPg, pgsz, bExtend, pp)
);
}
static int recoverVfsShmLock(sqlite3_file *pFd, int offset, int n, int flags){
RECOVER_VFS_WRAPPER (
pFd->pMethods->xShmLock(pFd, offset, n, flags)
);
}
static void recoverVfsShmBarrier(sqlite3_file *pFd){
if( pFd->pMethods==&recover_methods ){
pFd->pMethods = recover_g.pMethods;
pFd->pMethods->xShmBarrier(pFd);
pFd->pMethods = &recover_methods;
}else{
pFd->pMethods->xShmBarrier(pFd);
}
}
static int recoverVfsShmUnmap(sqlite3_file *pFd, int deleteFlag){
RECOVER_VFS_WRAPPER (
pFd->pMethods->xShmUnmap(pFd, deleteFlag)
);
}
static int recoverVfsFetch(
sqlite3_file *pFd,
sqlite3_int64 iOff,
int iAmt,
void **pp
){
*pp = 0;
return SQLITE_OK;
}
static int recoverVfsUnfetch(sqlite3_file *pFd, sqlite3_int64 iOff, void *p){
return SQLITE_OK;
}
/*
** Install the VFS wrapper around the file-descriptor open on the input
** database for recover handle p. Mutex RECOVER_MUTEX_ID must be held
** when this function is called.
*/
static void recoverInstallWrapper(sqlite3_recover *p){
sqlite3_file *pFd = 0;
assert( recover_g.pMethods==0 );
recoverAssertMutexHeld();
sqlite3_file_control(p->dbIn, p->zDb, SQLITE_FCNTL_FILE_POINTER, (void*)&pFd);
assert( pFd==0 || pFd->pMethods!=&recover_methods );
if( pFd && pFd->pMethods ){
int iVersion = 1 + (pFd->pMethods->iVersion>1 && pFd->pMethods->xShmMap!=0);
recover_g.pMethods = pFd->pMethods;
recover_g.p = p;
recover_methods.iVersion = iVersion;
pFd->pMethods = &recover_methods;
}
}
/*
** Uninstall the VFS wrapper that was installed around the file-descriptor open
** on the input database for recover handle p. Mutex RECOVER_MUTEX_ID must be
** held when this function is called.
*/
static void recoverUninstallWrapper(sqlite3_recover *p){
sqlite3_file *pFd = 0;
recoverAssertMutexHeld();
sqlite3_file_control(p->dbIn, p->zDb,SQLITE_FCNTL_FILE_POINTER,(void*)&pFd);
if( pFd && pFd->pMethods ){
pFd->pMethods = recover_g.pMethods;
recover_g.pMethods = 0;
recover_g.p = 0;
}
}
/*
** This function does the work of a single sqlite3_recover_step() call. It
** is guaranteed that the handle is not in an error state when this
** function is called.
*/
static void recoverStep(sqlite3_recover *p){
assert( p && p->errCode==SQLITE_OK );
switch( p->eState ){
case RECOVER_STATE_INIT:
/* This is the very first call to sqlite3_recover_step() on this object.
*/
recoverSqlCallback(p, "BEGIN");
recoverSqlCallback(p, "PRAGMA writable_schema = on");
recoverEnterMutex();
recoverInstallWrapper(p);
/* Open the output database. And register required virtual tables and
** user functions with the new handle. */
recoverOpenOutput(p);
/* Open transactions on both the input and output databases. */
recoverExec(p, p->dbIn, "PRAGMA writable_schema = on");
recoverExec(p, p->dbIn, "BEGIN");
if( p->errCode==SQLITE_OK ) p->bCloseTransaction = 1;
recoverExec(p, p->dbIn, "SELECT 1 FROM sqlite_schema");
recoverTransferSettings(p);
recoverOpenRecovery(p);
recoverCacheSchema(p);
recoverUninstallWrapper(p);
recoverLeaveMutex();
recoverExec(p, p->dbOut, "BEGIN");
recoverWriteSchema1(p);
p->eState = RECOVER_STATE_WRITING;
break;
case RECOVER_STATE_WRITING: {
if( p->w1.pTbls==0 ){
recoverWriteDataInit(p);
}
if( SQLITE_DONE==recoverWriteDataStep(p) ){
recoverWriteDataCleanup(p);
if( p->zLostAndFound ){
p->eState = RECOVER_STATE_LOSTANDFOUND1;
}else{
p->eState = RECOVER_STATE_SCHEMA2;
}
}
break;
}
case RECOVER_STATE_LOSTANDFOUND1: {
if( p->laf.pUsed==0 ){
recoverLostAndFound1Init(p);
}
if( SQLITE_DONE==recoverLostAndFound1Step(p) ){
p->eState = RECOVER_STATE_LOSTANDFOUND2;
}
break;
}
case RECOVER_STATE_LOSTANDFOUND2: {
if( p->laf.pAllAndParent==0 ){
recoverLostAndFound2Init(p);
}
if( SQLITE_DONE==recoverLostAndFound2Step(p) ){
p->eState = RECOVER_STATE_LOSTANDFOUND3;
}
break;
}
case RECOVER_STATE_LOSTANDFOUND3: {
if( p->laf.pInsert==0 ){
recoverLostAndFound3Init(p);
}
if( SQLITE_DONE==recoverLostAndFound3Step(p) ){
p->eState = RECOVER_STATE_SCHEMA2;
}
break;
}
case RECOVER_STATE_SCHEMA2: {
int rc = SQLITE_OK;
recoverWriteSchema2(p);
p->eState = RECOVER_STATE_DONE;
/* If no error has occurred, commit the write transaction on the output
** database. Regardless of whether or not an error has occurred, make
** an attempt to end the read transaction on the input database. */
recoverExec(p, p->dbOut, "COMMIT");
rc = sqlite3_exec(p->dbIn, "END", 0, 0, 0);
if( p->errCode==SQLITE_OK ) p->errCode = rc;
recoverSqlCallback(p, "PRAGMA writable_schema = off");
recoverSqlCallback(p, "COMMIT");
p->eState = RECOVER_STATE_DONE;
recoverFinalCleanup(p);
break;
};
case RECOVER_STATE_DONE: {
/* no-op */
break;
};
}
}
/*
** This is a worker function that does the heavy lifting for both init
** functions:
**
** sqlite3_recover_init()
** sqlite3_recover_init_sql()
**
** All this function does is allocate space for the recover handle and
** take copies of the input parameters. All the real work is done within
** sqlite3_recover_run().
*/
sqlite3_recover *recoverInit(
sqlite3* db,
const char *zDb,
const char *zUri, /* Output URI for _recover_init() */
int (*xSql)(void*, const char*),/* SQL callback for _recover_init_sql() */
void *pSqlCtx /* Context arg for _recover_init_sql() */
){
sqlite3_recover *pRet = 0;
int nDb = 0;
int nUri = 0;
int nByte = 0;
if( zDb==0 ){ zDb = "main"; }
nDb = recoverStrlen(zDb);
nUri = recoverStrlen(zUri);
nByte = sizeof(sqlite3_recover) + nDb+1 + nUri+1;
pRet = (sqlite3_recover*)sqlite3_malloc(nByte);
if( pRet ){
memset(pRet, 0, nByte);
pRet->dbIn = db;
pRet->zDb = (char*)&pRet[1];
pRet->zUri = &pRet->zDb[nDb+1];
memcpy(pRet->zDb, zDb, nDb);
if( nUri>0 && zUri ) memcpy(pRet->zUri, zUri, nUri);
pRet->xSql = xSql;
pRet->pSqlCtx = pSqlCtx;
pRet->bRecoverRowid = RECOVER_ROWID_DEFAULT;
}
return pRet;
}
/*
** Initialize a recovery handle that creates a new database containing
** the recovered data.
*/
sqlite3_recover *sqlite3_recover_init(
sqlite3* db,
const char *zDb,
const char *zUri
){
return recoverInit(db, zDb, zUri, 0, 0);
}
/*
** Initialize a recovery handle that returns recovered data in the
** form of SQL statements via a callback.
*/
sqlite3_recover *sqlite3_recover_init_sql(
sqlite3* db,
const char *zDb,
int (*xSql)(void*, const char*),
void *pSqlCtx
){
return recoverInit(db, zDb, 0, xSql, pSqlCtx);
}
/*
** Return the handle error message, if any.
*/
const char *sqlite3_recover_errmsg(sqlite3_recover *p){
return (p && p->errCode!=SQLITE_NOMEM) ? p->zErrMsg : "out of memory";
}
/*
** Return the handle error code.
*/
int sqlite3_recover_errcode(sqlite3_recover *p){
return p ? p->errCode : SQLITE_NOMEM;
}
/*
** Configure the handle.
*/
int sqlite3_recover_config(sqlite3_recover *p, int op, void *pArg){
int rc = SQLITE_OK;
if( p==0 ){
rc = SQLITE_NOMEM;
}else if( p->eState!=RECOVER_STATE_INIT ){
rc = SQLITE_MISUSE;
}else{
switch( op ){
case 789:
/* This undocumented magic configuration option is used to set the
** name of the auxiliary database that is ATTACH-ed to the database
** connection and used to hold state information during the
** recovery process. This option is for debugging use only and
** is subject to change or removal at any time. */
sqlite3_free(p->zStateDb);
p->zStateDb = recoverMPrintf(p, "%s", (char*)pArg);
break;
case SQLITE_RECOVER_LOST_AND_FOUND: {
const char *zArg = (const char*)pArg;
sqlite3_free(p->zLostAndFound);
if( zArg ){
p->zLostAndFound = recoverMPrintf(p, "%s", zArg);
}else{
p->zLostAndFound = 0;
}
break;
}
case SQLITE_RECOVER_FREELIST_CORRUPT:
p->bFreelistCorrupt = *(int*)pArg;
break;
case SQLITE_RECOVER_ROWIDS:
p->bRecoverRowid = *(int*)pArg;
break;
case SQLITE_RECOVER_SLOWINDEXES:
p->bSlowIndexes = *(int*)pArg;
break;
default:
rc = SQLITE_NOTFOUND;
break;
}
}
return rc;
}
/*
** Do a unit of work towards the recovery job. Return SQLITE_OK if
** no error has occurred but database recovery is not finished, SQLITE_DONE
** if database recovery has been successfully completed, or an SQLite
** error code if an error has occurred.
*/
int sqlite3_recover_step(sqlite3_recover *p){
if( p==0 ) return SQLITE_NOMEM;
if( p->errCode==SQLITE_OK ) recoverStep(p);
if( p->eState==RECOVER_STATE_DONE && p->errCode==SQLITE_OK ){
return SQLITE_DONE;
}
return p->errCode;
}
/*
** Do the configured recovery operation. Return SQLITE_OK if successful, or
** else an SQLite error code.
*/
int sqlite3_recover_run(sqlite3_recover *p){
while( SQLITE_OK==sqlite3_recover_step(p) );
return sqlite3_recover_errcode(p);
}
/*
** Free all resources associated with the recover handle passed as the only
** argument. The results of using a handle with any sqlite3_recover_**
** API function after it has been passed to this function are undefined.
**
** A copy of the value returned by the first call made to sqlite3_recover_run()
** on this handle is returned, or SQLITE_OK if sqlite3_recover_run() has
** not been called on this handle.
*/
int sqlite3_recover_finish(sqlite3_recover *p){
int rc;
if( p==0 ){
rc = SQLITE_NOMEM;
}else{
recoverFinalCleanup(p);
if( p->bCloseTransaction && sqlite3_get_autocommit(p->dbIn)==0 ){
rc = sqlite3_exec(p->dbIn, "END", 0, 0, 0);
if( p->errCode==SQLITE_OK ) p->errCode = rc;
}
rc = p->errCode;
sqlite3_free(p->zErrMsg);
sqlite3_free(p->zStateDb);
sqlite3_free(p->zLostAndFound);
sqlite3_free(p->pPage1Cache);
sqlite3_free(p);
}
return rc;
}
#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
/************************* End ../ext/recover/sqlite3recover.c ********************/
#endif
#if defined(SQLITE_ENABLE_SESSION)
/*
** State information for a single open session
*/
typedef struct OpenSession OpenSession;
|
| ︙ | ︙ | |||
13851 13852 13853 13854 13855 13856 13857 13858 13859 13860 13861 13862 13863 13864 |
}
len = strlen(zSql);
if( len>78 ){
len = 78;
while( (zSql[len]&0xc0)==0x80 ) len--;
}
zCode = sqlite3_mprintf("%.*s", len, zSql);
for(i=0; zCode[i]; i++){ if( IsSpace(zSql[i]) ) zCode[i] = ' '; }
if( iOffset<25 ){
zMsg = sqlite3_mprintf("\n %z\n %*s^--- error here", zCode, iOffset, "");
}else{
zMsg = sqlite3_mprintf("\n %z\n %*serror here ---^", zCode, iOffset-14, "");
}
return zMsg;
| > | 17054 17055 17056 17057 17058 17059 17060 17061 17062 17063 17064 17065 17066 17067 17068 |
}
len = strlen(zSql);
if( len>78 ){
len = 78;
while( (zSql[len]&0xc0)==0x80 ) len--;
}
zCode = sqlite3_mprintf("%.*s", len, zSql);
shell_check_oom(zCode);
for(i=0; zCode[i]; i++){ if( IsSpace(zSql[i]) ) zCode[i] = ' '; }
if( iOffset<25 ){
zMsg = sqlite3_mprintf("\n %z\n %*s^--- error here", zCode, iOffset, "");
}else{
zMsg = sqlite3_mprintf("\n %z\n %*serror here ---^", zCode, iOffset-14, "");
}
return zMsg;
|
| ︙ | ︙ | |||
15550 15551 15552 15553 15554 15555 15556 | #ifndef SQLITE_SHELL_FIDDLE ".check GLOB Fail if output since .testcase does not match", ".clone NEWDB Clone data into NEWDB from the existing database", #endif ".connection [close] [#] Open or close an auxiliary database connection", ".databases List names and files of attached databases", ".dbconfig ?op? ?val? List or change sqlite3_db_config() options", | | | 18754 18755 18756 18757 18758 18759 18760 18761 18762 18763 18764 18765 18766 18767 18768 | #ifndef SQLITE_SHELL_FIDDLE ".check GLOB Fail if output since .testcase does not match", ".clone NEWDB Clone data into NEWDB from the existing database", #endif ".connection [close] [#] Open or close an auxiliary database connection", ".databases List names and files of attached databases", ".dbconfig ?op? ?val? List or change sqlite3_db_config() options", #if SQLITE_SHELL_HAVE_RECOVER ".dbinfo ?DB? Show status information about the database", #endif ".dump ?OBJECTS? Render database content as SQL", " Options:", " --data-only Output only INSERT statements", " --newlines Allow unescaped newline characters in output", " --nosys Omit system tables (ex: \"sqlite_stat1\")", |
| ︙ | ︙ | |||
15698 15699 15700 15701 15702 15703 15704 | #endif ".prompt MAIN CONTINUE Replace the standard prompts", #ifndef SQLITE_SHELL_FIDDLE ".quit Exit this program", ".read FILE Read input from FILE or command output", " If FILE begins with \"|\", it is a command that generates the input.", #endif | | | < | 18902 18903 18904 18905 18906 18907 18908 18909 18910 18911 18912 18913 18914 18915 18916 18917 18918 | #endif ".prompt MAIN CONTINUE Replace the standard prompts", #ifndef SQLITE_SHELL_FIDDLE ".quit Exit this program", ".read FILE Read input from FILE or command output", " If FILE begins with \"|\", it is a command that generates the input.", #endif #if SQLITE_SHELL_HAVE_RECOVER ".recover Recover as much data as possible from corrupt db.", " --ignore-freelist Ignore pages that appear to be on db freelist", " --lost-and-found TABLE Alternative name for the lost-and-found table", " --no-rowids Do not attempt to recover rowid values", " that are not also INTEGER PRIMARY KEYs", #endif #ifndef SQLITE_SHELL_FIDDLE ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE", ".save ?OPTIONS? FILE Write database to FILE (an alias for .backup ...)", |
| ︙ | ︙ | |||
16313 16314 16315 16316 16317 16318 16319 |
sqlite3_regexp_init(p->db, 0, 0);
sqlite3_ieee_init(p->db, 0, 0);
sqlite3_series_init(p->db, 0, 0);
#ifndef SQLITE_SHELL_FIDDLE
sqlite3_fileio_init(p->db, 0, 0);
sqlite3_completion_init(p->db, 0, 0);
#endif
| | | 19516 19517 19518 19519 19520 19521 19522 19523 19524 19525 19526 19527 19528 19529 19530 |
sqlite3_regexp_init(p->db, 0, 0);
sqlite3_ieee_init(p->db, 0, 0);
sqlite3_series_init(p->db, 0, 0);
#ifndef SQLITE_SHELL_FIDDLE
sqlite3_fileio_init(p->db, 0, 0);
sqlite3_completion_init(p->db, 0, 0);
#endif
#if SQLITE_SHELL_HAVE_RECOVER
sqlite3_dbdata_init(p->db, 0, 0);
#endif
#ifdef SQLITE_HAVE_ZLIB
if( !p->bSafeModePersist ){
sqlite3_zipfile_init(p->db, 0, 0);
sqlite3_sqlar_init(p->db, 0, 0);
}
|
| ︙ | ︙ | |||
17198 17199 17200 17201 17202 17203 17204 |
utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
}
sqlite3_free(zSchemaTab);
sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion);
return 0;
}
| | < | 20401 20402 20403 20404 20405 20406 20407 20408 20409 20410 20411 20412 20413 20414 20415 |
utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
}
sqlite3_free(zSchemaTab);
sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion);
return 0;
}
#endif /* SQLITE_SHELL_HAVE_RECOVER */
/*
** Print the current sqlite3_errmsg() value to stderr and return 1.
*/
static int shellDatabaseError(sqlite3 *db){
const char *zErr = sqlite3_errmsg(db);
utf8_printf(stderr, "Error: %s\n", zErr);
|
| ︙ | ︙ | |||
18472 18473 18474 18475 18476 18477 18478 | return rc; } /* End of the ".archive" or ".ar" command logic *******************************************************************************/ #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */ | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < > > < < < < < < < < < | < < < < < < < < < < < < < < < < < < | < < < < < < < < < < < < < < < < < < < | < < < < < < < < < < < | < < < | | < < < < | > > > | > > > > > | < < < < < < < < < < < | < | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | | < < | < < < < < < < < < < < | < < < | < < < < < < < < | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | < < < < < < < < < < < < < < < < < < < < < < < < | < < < < < < | < < < < < < < | < | | < < < | < < < < < | | 21674 21675 21676 21677 21678 21679 21680 21681 21682 21683 21684 21685 21686 21687 21688 21689 21690 21691 21692 21693 21694 21695 21696 21697 21698 21699 21700 21701 21702 21703 21704 21705 21706 21707 21708 21709 21710 21711 21712 21713 21714 21715 21716 21717 21718 21719 21720 21721 21722 21723 21724 21725 21726 21727 21728 21729 21730 21731 21732 21733 21734 21735 21736 21737 21738 21739 21740 21741 21742 21743 21744 21745 21746 21747 21748 21749 21750 21751 21752 21753 21754 21755 21756 21757 21758 21759 21760 21761 21762 21763 |
return rc;
}
/* End of the ".archive" or ".ar" command logic
*******************************************************************************/
#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
#if SQLITE_SHELL_HAVE_RECOVER
/*
** This function is used as a callback by the recover extension. Simply
** print the supplied SQL statement to stdout.
*/
static int recoverSqlCb(void *pCtx, const char *zSql){
ShellState *pState = (ShellState*)pCtx;
utf8_printf(pState->out, "%s;\n", zSql);
return SQLITE_OK;
}
/*
** This function is called to recover data from the database. A script
** to construct a new database containing all recovered data is output
** on stream pState->out.
*/
static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){
int rc = SQLITE_OK;
const char *zRecoveryDb = ""; /* Name of "recovery" database. Debug only */
const char *zLAF = "lost_and_found";
int bFreelist = 1; /* 0 if --ignore-freelist is specified */
int bRowids = 1; /* 0 if --no-rowids */
sqlite3_recover *p = 0;
int i = 0;
for(i=1; i<nArg; i++){
char *z = azArg[i];
int n;
if( z[0]=='-' && z[1]=='-' ) z++;
n = strlen30(z);
if( n<=17 && memcmp("-ignore-freelist", z, n)==0 ){
bFreelist = 0;
}else
if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){
/* This option determines the name of the ATTACH-ed database used
** internally by the recovery extension. The default is "" which
** means to use a temporary database that is automatically deleted
** when closed. This option is undocumented and might disappear at
** any moment. */
i++;
zRecoveryDb = azArg[i];
}else
if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){
i++;
zLAF = azArg[i];
}else
if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
bRowids = 0;
}
else{
utf8_printf(stderr, "unexpected option: %s\n", azArg[i]);
showHelp(pState->out, azArg[0]);
return 1;
}
}
p = sqlite3_recover_init_sql(
pState->db, "main", recoverSqlCb, (void*)pState
);
sqlite3_recover_config(p, 789, (void*)zRecoveryDb); /* Debug use only */
sqlite3_recover_config(p, SQLITE_RECOVER_LOST_AND_FOUND, (void*)zLAF);
sqlite3_recover_config(p, SQLITE_RECOVER_ROWIDS, (void*)&bRowids);
sqlite3_recover_config(p, SQLITE_RECOVER_FREELIST_CORRUPT,(void*)&bFreelist);
sqlite3_recover_run(p);
if( sqlite3_recover_errcode(p)!=SQLITE_OK ){
const char *zErr = sqlite3_recover_errmsg(p);
int errCode = sqlite3_recover_errcode(p);
raw_printf(stderr, "sql error: %s (%d)\n", zErr, errCode);
}
rc = sqlite3_recover_finish(p);
return rc;
}
#endif /* SQLITE_SHELL_HAVE_RECOVER */
/*
* zAutoColumn(zCol, &db, ?) => Maybe init db, add column zCol to it.
* zAutoColumn(0, &db, ?) => (db!=0) Form columns spec for CREATE TABLE,
* close db and set it to 0, and return the columns spec, to later
* be sqlite3_free()'ed by the caller.
|
| ︙ | ︙ | |||
19742 19743 19744 19745 19746 19747 19748 |
}
if( nArg>1 && ii==ArraySize(aDbConfig) ){
utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]);
utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");
}
}else
| | | | 22341 22342 22343 22344 22345 22346 22347 22348 22349 22350 22351 22352 22353 22354 22355 22356 22357 22358 22359 22360 22361 22362 22363 22364 |
}
if( nArg>1 && ii==ArraySize(aDbConfig) ){
utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]);
utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");
}
}else
#if SQLITE_SHELL_HAVE_RECOVER
if( c=='d' && n>=3 && cli_strncmp(azArg[0], "dbinfo", n)==0 ){
rc = shell_dbinfo_command(p, nArg, azArg);
}else
if( c=='r' && cli_strncmp(azArg[0], "recover", n)==0 ){
open_db(p, 0);
rc = recoverDatabaseCmd(p, nArg, azArg);
}else
#endif /* SQLITE_SHELL_HAVE_RECOVER */
if( c=='d' && cli_strncmp(azArg[0], "dump", n)==0 ){
char *zLike = 0;
char *zSql;
int i;
int savedShowHeader = p->showHeader;
int savedShellFlags = p->shellFlgs;
|
| ︙ | ︙ |
Changes to extsrc/sqlite3.c.
| ︙ | ︙ | |||
450 451 452 453 454 455 456 | ** ** See also: [sqlite3_libversion()], ** [sqlite3_libversion_number()], [sqlite3_sourceid()], ** [sqlite_version()] and [sqlite_source_id()]. */ #define SQLITE_VERSION "3.40.0" #define SQLITE_VERSION_NUMBER 3040000 | | | 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 | ** ** See also: [sqlite3_libversion()], ** [sqlite3_libversion_number()], [sqlite3_sourceid()], ** [sqlite_version()] and [sqlite_source_id()]. */ #define SQLITE_VERSION "3.40.0" #define SQLITE_VERSION_NUMBER 3040000 #define SQLITE_SOURCE_ID "2022-11-04 16:39:39 0e5597ce5353dea2cdb092b166b57ba1d60f8115eb468349f2b2869803691a2c" /* ** CAPI3REF: Run-Time Library Version Numbers ** KEYWORDS: sqlite3_version sqlite3_sourceid ** ** These interfaces provide the same information as the [SQLITE_VERSION], ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros |
| ︙ | ︙ | |||
1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 | ** A pointer to the opaque sqlite3_api_routines structure is passed as ** the third parameter to entry points of [loadable extensions]. This ** structure must be typedefed in order to work around compiler warnings ** on some platforms. */ typedef struct sqlite3_api_routines sqlite3_api_routines; /* ** CAPI3REF: OS Interface Object ** ** An instance of the sqlite3_vfs object defines the interface between ** the SQLite core and the underlying operating system. The "vfs" ** in the name of the object stands for "virtual file system". See ** the [VFS | VFS documentation] for further information. | > > > > > > > > > > > > > > > > > > > > | 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 | ** A pointer to the opaque sqlite3_api_routines structure is passed as ** the third parameter to entry points of [loadable extensions]. This ** structure must be typedefed in order to work around compiler warnings ** on some platforms. */ typedef struct sqlite3_api_routines sqlite3_api_routines; /* ** CAPI3REF: File Name ** ** Type [sqlite3_filename] is used by SQLite to pass filenames to the ** xOpen method of a [VFS]. It may be cast to (const char*) and treated ** as a normal, nul-terminated, UTF-8 buffer containing the filename, but ** may also be passed to special APIs such as: ** ** <ul> ** <li> sqlite3_filename_database() ** <li> sqlite3_filename_journal() ** <li> sqlite3_filename_wal() ** <li> sqlite3_uri_parameter() ** <li> sqlite3_uri_boolean() ** <li> sqlite3_uri_int64() ** <li> sqlite3_uri_key() ** </ul> */ typedef const char *sqlite3_filename; /* ** CAPI3REF: OS Interface Object ** ** An instance of the sqlite3_vfs object defines the interface between ** the SQLite core and the underlying operating system. The "vfs" ** in the name of the object stands for "virtual file system". See ** the [VFS | VFS documentation] for further information. |
| ︙ | ︙ | |||
1743 1744 1745 1746 1747 1748 1749 |
struct sqlite3_vfs {
int iVersion; /* Structure version number (currently 3) */
int szOsFile; /* Size of subclassed sqlite3_file */
int mxPathname; /* Maximum file pathname length */
sqlite3_vfs *pNext; /* Next registered VFS */
const char *zName; /* Name of this virtual file system */
void *pAppData; /* Pointer to application-specific data */
| | | 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 |
struct sqlite3_vfs {
int iVersion; /* Structure version number (currently 3) */
int szOsFile; /* Size of subclassed sqlite3_file */
int mxPathname; /* Maximum file pathname length */
sqlite3_vfs *pNext; /* Next registered VFS */
const char *zName; /* Name of this virtual file system */
void *pAppData; /* Pointer to application-specific data */
int (*xOpen)(sqlite3_vfs*, sqlite3_filename zName, sqlite3_file*,
int flags, int *pOutFlags);
int (*xDelete)(sqlite3_vfs*, const char *zName, int syncDir);
int (*xAccess)(sqlite3_vfs*, const char *zName, int flags, int *pResOut);
int (*xFullPathname)(sqlite3_vfs*, const char *zName, int nOut, char *zOut);
void *(*xDlOpen)(sqlite3_vfs*, const char *zFilename);
void (*xDlError)(sqlite3_vfs*, int nByte, char *zErrMsg);
void (*(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol))(void);
|
| ︙ | ︙ | |||
4013 4014 4015 4016 4017 4018 4019 | ** routines would only work if F was the name of the main database file. ** When the F parameter is the name of the rollback journal or WAL file, ** it has access to all the same query parameters as were found on the ** main database file. ** ** See the [URI filename] documentation for additional information. */ | | | | | | 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 | ** routines would only work if F was the name of the main database file. ** When the F parameter is the name of the rollback journal or WAL file, ** it has access to all the same query parameters as were found on the ** main database file. ** ** See the [URI filename] documentation for additional information. */ SQLITE_API const char *sqlite3_uri_parameter(sqlite3_filename z, const char *zParam); SQLITE_API int sqlite3_uri_boolean(sqlite3_filename z, const char *zParam, int bDefault); SQLITE_API sqlite3_int64 sqlite3_uri_int64(sqlite3_filename, const char*, sqlite3_int64); SQLITE_API const char *sqlite3_uri_key(sqlite3_filename z, int N); /* ** CAPI3REF: Translate filenames ** ** These routines are available to [VFS|custom VFS implementations] for ** translating filenames between the main database file, the journal file, ** and the WAL file. |
| ︙ | ︙ | |||
4045 4046 4047 4048 4049 4050 4051 | ** WAL file. ** ** In all of the above, if F is not the name of a database, journal or WAL ** filename passed into the VFS from the SQLite core and F is not the ** return value from [sqlite3_db_filename()], then the result is ** undefined and is likely a memory access violation. */ | | | | | 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 | ** WAL file. ** ** In all of the above, if F is not the name of a database, journal or WAL ** filename passed into the VFS from the SQLite core and F is not the ** return value from [sqlite3_db_filename()], then the result is ** undefined and is likely a memory access violation. */ SQLITE_API const char *sqlite3_filename_database(sqlite3_filename); SQLITE_API const char *sqlite3_filename_journal(sqlite3_filename); SQLITE_API const char *sqlite3_filename_wal(sqlite3_filename); /* ** CAPI3REF: Database File Corresponding To A Journal ** ** ^If X is the name of a rollback or WAL-mode journal file that is ** passed into the xOpen method of [sqlite3_vfs], then ** sqlite3_database_file_object(X) returns a pointer to the [sqlite3_file] |
| ︙ | ︙ | |||
4113 4114 4115 4116 4117 4118 4119 | ** sqlite3_create_filename(), then bad things such as heap ** corruption or segfaults may occur. The value Y should not be ** used again after sqlite3_free_filename(Y) has been called. This means ** that if the [sqlite3_vfs.xOpen()] method of a VFS has been called using Y, ** then the corresponding [sqlite3_module.xClose() method should also be ** invoked prior to calling sqlite3_free_filename(Y). */ | | | | 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 | ** sqlite3_create_filename(), then bad things such as heap ** corruption or segfaults may occur. The value Y should not be ** used again after sqlite3_free_filename(Y) has been called. This means ** that if the [sqlite3_vfs.xOpen()] method of a VFS has been called using Y, ** then the corresponding [sqlite3_module.xClose() method should also be ** invoked prior to calling sqlite3_free_filename(Y). */ SQLITE_API sqlite3_filename sqlite3_create_filename( const char *zDatabase, const char *zJournal, const char *zWal, int nParam, const char **azParam ); SQLITE_API void sqlite3_free_filename(sqlite3_filename); /* ** CAPI3REF: Error Codes And Messages ** METHOD: sqlite3 ** ** ^If the most recent sqlite3_* API call associated with ** [database connection] D failed, then the sqlite3_errcode(D) interface |
| ︙ | ︙ | |||
6654 6655 6656 6657 6658 6659 6660 | ** <li> [sqlite3_uri_boolean()] ** <li> [sqlite3_uri_int64()] ** <li> [sqlite3_filename_database()] ** <li> [sqlite3_filename_journal()] ** <li> [sqlite3_filename_wal()] ** </ul> */ | | | 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 | ** <li> [sqlite3_uri_boolean()] ** <li> [sqlite3_uri_int64()] ** <li> [sqlite3_filename_database()] ** <li> [sqlite3_filename_journal()] ** <li> [sqlite3_filename_wal()] ** </ul> */ SQLITE_API sqlite3_filename sqlite3_db_filename(sqlite3 *db, const char *zDbName); /* ** CAPI3REF: Determine if a database is read-only ** METHOD: sqlite3 ** ** ^The sqlite3_db_readonly(D,N) interface returns 1 if the database N ** of connection D is read-only, 0 if it is read/write, or -1 if N is not |
| ︙ | ︙ | |||
18877 18878 18879 18880 18881 18882 18883 |
*/
struct SelectDest {
u8 eDest; /* How to dispose of the results. One of SRT_* above. */
int iSDParm; /* A parameter used by the eDest disposal method */
int iSDParm2; /* A second parameter for the eDest disposal method */
int iSdst; /* Base register where results are written */
int nSdst; /* Number of registers allocated */
| | | 18897 18898 18899 18900 18901 18902 18903 18904 18905 18906 18907 18908 18909 18910 18911 |
*/
struct SelectDest {
u8 eDest; /* How to dispose of the results. One of SRT_* above. */
int iSDParm; /* A parameter used by the eDest disposal method */
int iSDParm2; /* A second parameter for the eDest disposal method */
int iSdst; /* Base register where results are written */
int nSdst; /* Number of registers allocated */
char *zAffSdst; /* Affinity used for SRT_Set, SRT_Table, and similar */
ExprList *pOrderBy; /* Key columns for SRT_Queue and SRT_DistQueue */
};
/*
** During code generation of statements that do inserts into AUTOINCREMENT
** tables, the following information is attached to the Table.u.autoInc.p
** pointer of each autoincrement table to record some side information that
|
| ︙ | ︙ | |||
20347 20348 20349 20350 20351 20352 20353 20354 20355 20356 20357 20358 20359 20360 | (u8)(((u32)(B)<(u32)0x80)?(*(A)=(unsigned char)(B)),1:\ sqlite3PutVarint((A),(B))) #define getVarint sqlite3GetVarint #define putVarint sqlite3PutVarint SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(sqlite3*, Index*); SQLITE_PRIVATE void sqlite3TableAffinity(Vdbe*, Table*, int); SQLITE_PRIVATE char sqlite3CompareAffinity(const Expr *pExpr, char aff2); SQLITE_PRIVATE int sqlite3IndexAffinityOk(const Expr *pExpr, char idx_affinity); SQLITE_PRIVATE char sqlite3TableColumnAffinity(const Table*,int); SQLITE_PRIVATE char sqlite3ExprAffinity(const Expr *pExpr); SQLITE_PRIVATE int sqlite3Atoi64(const char*, i64*, int, u8); SQLITE_PRIVATE int sqlite3DecOrHexToI64(const char*, i64*); | > | 20367 20368 20369 20370 20371 20372 20373 20374 20375 20376 20377 20378 20379 20380 20381 | (u8)(((u32)(B)<(u32)0x80)?(*(A)=(unsigned char)(B)),1:\ sqlite3PutVarint((A),(B))) #define getVarint sqlite3GetVarint #define putVarint sqlite3PutVarint SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(sqlite3*, Index*); SQLITE_PRIVATE char *sqlite3TableAffinityStr(sqlite3*,const Table*); SQLITE_PRIVATE void sqlite3TableAffinity(Vdbe*, Table*, int); SQLITE_PRIVATE char sqlite3CompareAffinity(const Expr *pExpr, char aff2); SQLITE_PRIVATE int sqlite3IndexAffinityOk(const Expr *pExpr, char idx_affinity); SQLITE_PRIVATE char sqlite3TableColumnAffinity(const Table*,int); SQLITE_PRIVATE char sqlite3ExprAffinity(const Expr *pExpr); SQLITE_PRIVATE int sqlite3Atoi64(const char*, i64*, int, u8); SQLITE_PRIVATE int sqlite3DecOrHexToI64(const char*, i64*); |
| ︙ | ︙ | |||
36279 36280 36281 36282 36283 36284 36285 36286 36287 36288 36289 36290 36291 36292 |
KVVfsFile *pFile = (KVVfsFile*)pProtoFile;
unsigned int pgno;
char zKey[30];
char aData[131073];
SQLITE_KV_LOG(("xWrite('%s-db',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
assert( iAmt>=512 && iAmt<=65536 );
assert( (iAmt & (iAmt-1))==0 );
pgno = 1 + iOfst/iAmt;
sqlite3_snprintf(sizeof(zKey), zKey, "%u", pgno);
kvvfsEncode(zBuf, iAmt, aData);
if( sqlite3KvvfsMethods.xWrite(pFile->zClass, zKey, aData) ){
return SQLITE_IOERR;
}
if( iOfst+iAmt > pFile->szDb ){
| > > | 36300 36301 36302 36303 36304 36305 36306 36307 36308 36309 36310 36311 36312 36313 36314 36315 |
KVVfsFile *pFile = (KVVfsFile*)pProtoFile;
unsigned int pgno;
char zKey[30];
char aData[131073];
SQLITE_KV_LOG(("xWrite('%s-db',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
assert( iAmt>=512 && iAmt<=65536 );
assert( (iAmt & (iAmt-1))==0 );
assert( pFile->szPage<0 || pFile->szPage==iAmt );
pFile->szPage = iAmt;
pgno = 1 + iOfst/iAmt;
sqlite3_snprintf(sizeof(zKey), zKey, "%u", pgno);
kvvfsEncode(zBuf, iAmt, aData);
if( sqlite3KvvfsMethods.xWrite(pFile->zClass, zKey, aData) ){
return SQLITE_IOERR;
}
if( iOfst+iAmt > pFile->szDb ){
|
| ︙ | ︙ | |||
36461 36462 36463 36464 36465 36466 36467 36468 36469 36470 36471 36472 36473 36474 |
sqlite3_vfs *pProtoVfs,
const char *zName,
sqlite3_file *pProtoFile,
int flags,
int *pOutFlags
){
KVVfsFile *pFile = (KVVfsFile*)pProtoFile;
SQLITE_KV_LOG(("xOpen(\"%s\")\n", zName));
if( strcmp(zName, "local")==0
|| strcmp(zName, "session")==0
){
pFile->isJournal = 0;
pFile->base.pMethods = &kvvfs_db_io_methods;
}else
| > | 36484 36485 36486 36487 36488 36489 36490 36491 36492 36493 36494 36495 36496 36497 36498 |
sqlite3_vfs *pProtoVfs,
const char *zName,
sqlite3_file *pProtoFile,
int flags,
int *pOutFlags
){
KVVfsFile *pFile = (KVVfsFile*)pProtoFile;
if( zName==0 ) zName = "";
SQLITE_KV_LOG(("xOpen(\"%s\")\n", zName));
if( strcmp(zName, "local")==0
|| strcmp(zName, "session")==0
){
pFile->isJournal = 0;
pFile->base.pMethods = &kvvfs_db_io_methods;
}else
|
| ︙ | ︙ | |||
69699 69700 69701 69702 69703 69704 69705 |
** number of bytes in fragments may not exceed 60. */
if( aData[hdr+7]>57 ) return 0;
/* Remove the slot from the free-list. Update the number of
** fragmented bytes within the page. */
memcpy(&aData[iAddr], &aData[pc], 2);
aData[hdr+7] += (u8)x;
| < | 69723 69724 69725 69726 69727 69728 69729 69730 69731 69732 69733 69734 69735 69736 |
** number of bytes in fragments may not exceed 60. */
if( aData[hdr+7]>57 ) return 0;
/* Remove the slot from the free-list. Update the number of
** fragmented bytes within the page. */
memcpy(&aData[iAddr], &aData[pc], 2);
aData[hdr+7] += (u8)x;
return &aData[pc];
}else if( x+pc > maxPC ){
/* This slot extends off the end of the usable part of the page */
*pRc = SQLITE_CORRUPT_PAGE(pPg);
return 0;
}else{
/* The slot remains on the free-list. Reduce its size to account
|
| ︙ | ︙ | |||
86474 86475 86476 86477 86478 86479 86480 |
VVA_ONLY( mem1.szMalloc = 0; ) /* Only needed by assert() statements */
assert( pPKey2->pKeyInfo->nAllField>=pPKey2->nField
|| CORRUPT_DB );
assert( pPKey2->pKeyInfo->aSortFlags!=0 );
assert( pPKey2->pKeyInfo->nKeyField>0 );
assert( idx1<=szHdr1 || CORRUPT_DB );
| < > | 86497 86498 86499 86500 86501 86502 86503 86504 86505 86506 86507 86508 86509 86510 86511 |
VVA_ONLY( mem1.szMalloc = 0; ) /* Only needed by assert() statements */
assert( pPKey2->pKeyInfo->nAllField>=pPKey2->nField
|| CORRUPT_DB );
assert( pPKey2->pKeyInfo->aSortFlags!=0 );
assert( pPKey2->pKeyInfo->nKeyField>0 );
assert( idx1<=szHdr1 || CORRUPT_DB );
while( 1 /*exit-by-break*/ ){
u32 serial_type;
/* RHS is an integer */
if( pRhs->flags & (MEM_Int|MEM_IntReal) ){
testcase( pRhs->flags & MEM_Int );
testcase( pRhs->flags & MEM_IntReal );
serial_type = aKey1[idx1];
|
| ︙ | ︙ | |||
86612 86613 86614 86615 86616 86617 86618 86619 |
return rc;
}
i++;
if( i==pPKey2->nField ) break;
pRhs++;
d1 += sqlite3VdbeSerialTypeLen(serial_type);
idx1 += sqlite3VarintLen(serial_type);
| > | > > > > | 86635 86636 86637 86638 86639 86640 86641 86642 86643 86644 86645 86646 86647 86648 86649 86650 86651 86652 86653 86654 86655 |
return rc;
}
i++;
if( i==pPKey2->nField ) break;
pRhs++;
d1 += sqlite3VdbeSerialTypeLen(serial_type);
if( d1>(unsigned)nKey1 ) break;
idx1 += sqlite3VarintLen(serial_type);
if( idx1>=(unsigned)szHdr1 ){
pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
return 0; /* Corrupt index */
}
}
/* No memory allocation is ever used on mem1. Prove this using
** the following assert(). If the assert() fails, it indicates a
** memory leak and a need to call sqlite3VdbeMemRelease(&mem1). */
assert( mem1.szMalloc==0 );
/* rc==0 here means that one or both of the keys ran out of fields and
|
| ︙ | ︙ | |||
127848 127849 127850 127851 127852 127853 127854 127855 127856 127857 127858 127859 127860 127861 |
pIdx->zColAff[n] = aff;
}
pIdx->zColAff[n] = 0;
}
return pIdx->zColAff;
}
/*
** Make changes to the evolving bytecode to do affinity transformations
** of values that are about to be gathered into a row for table pTab.
**
** For ordinary (legacy, non-strict) tables:
** -----------------------------------------
| > > > > > > > > > > > > > > > > > > > > > > | 127876 127877 127878 127879 127880 127881 127882 127883 127884 127885 127886 127887 127888 127889 127890 127891 127892 127893 127894 127895 127896 127897 127898 127899 127900 127901 127902 127903 127904 127905 127906 127907 127908 127909 127910 127911 |
pIdx->zColAff[n] = aff;
}
pIdx->zColAff[n] = 0;
}
return pIdx->zColAff;
}
/*
** Compute an affinity string for a table. Space is obtained
** from sqlite3DbMalloc(). The caller is responsible for freeing
** the space when done.
*/
SQLITE_PRIVATE char *sqlite3TableAffinityStr(sqlite3 *db, const Table *pTab){
char *zColAff;
zColAff = (char *)sqlite3DbMallocRaw(db, pTab->nCol+1);
if( zColAff ){
int i, j;
for(i=j=0; i<pTab->nCol; i++){
if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ){
zColAff[j++] = pTab->aCol[i].affinity;
}
}
do{
zColAff[j--] = 0;
}while( j>=0 && zColAff[j]<=SQLITE_AFF_BLOB );
}
return zColAff;
}
/*
** Make changes to the evolving bytecode to do affinity transformations
** of values that are about to be gathered into a row for table pTab.
**
** For ordinary (legacy, non-strict) tables:
** -----------------------------------------
|
| ︙ | ︙ | |||
127890 127891 127892 127893 127894 127895 127896 |
** the last opcode generated. The new OP_TypeCheck needs to be inserted
** before the OP_MakeRecord. The new OP_TypeCheck should use the same
** register set as the OP_MakeRecord. If iReg>0 then register iReg is
** the first of a series of registers that will form the new record.
** Apply the type checking to that array of registers.
*/
SQLITE_PRIVATE void sqlite3TableAffinity(Vdbe *v, Table *pTab, int iReg){
| | | 127940 127941 127942 127943 127944 127945 127946 127947 127948 127949 127950 127951 127952 127953 127954 |
** the last opcode generated. The new OP_TypeCheck needs to be inserted
** before the OP_MakeRecord. The new OP_TypeCheck should use the same
** register set as the OP_MakeRecord. If iReg>0 then register iReg is
** the first of a series of registers that will form the new record.
** Apply the type checking to that array of registers.
*/
SQLITE_PRIVATE void sqlite3TableAffinity(Vdbe *v, Table *pTab, int iReg){
int i;
char *zColAff;
if( pTab->tabFlags & TF_Strict ){
if( iReg==0 ){
/* Move the previous opcode (which should be OP_MakeRecord) forward
** by one slot and insert a new OP_TypeCheck where the current
** OP_MakeRecord is found */
VdbeOp *pPrev;
|
| ︙ | ︙ | |||
127913 127914 127915 127916 127917 127918 127919 |
sqlite3VdbeAddOp2(v, OP_TypeCheck, iReg, pTab->nNVCol);
sqlite3VdbeAppendP4(v, pTab, P4_TABLE);
}
return;
}
zColAff = pTab->zColAff;
if( zColAff==0 ){
| | < | < < < < < < < < < < | 127963 127964 127965 127966 127967 127968 127969 127970 127971 127972 127973 127974 127975 127976 127977 127978 127979 127980 127981 |
sqlite3VdbeAddOp2(v, OP_TypeCheck, iReg, pTab->nNVCol);
sqlite3VdbeAppendP4(v, pTab, P4_TABLE);
}
return;
}
zColAff = pTab->zColAff;
if( zColAff==0 ){
zColAff = sqlite3TableAffinityStr(0, pTab);
if( !zColAff ){
sqlite3OomFault(sqlite3VdbeDb(v));
return;
}
pTab->zColAff = zColAff;
}
assert( zColAff!=0 );
i = sqlite3Strlen30NN(zColAff);
if( i ){
if( iReg ){
sqlite3VdbeAddOp4(v, OP_Affinity, iReg, i, 0, zColAff, i);
|
| ︙ | ︙ | |||
131384 131385 131386 131387 131388 131389 131390 | /* Version 3.31.0 and later */ sqlite3_int64 (*hard_heap_limit64)(sqlite3_int64); const char *(*uri_key)(const char*,int); const char *(*filename_database)(const char*); const char *(*filename_journal)(const char*); const char *(*filename_wal)(const char*); /* Version 3.32.0 and later */ | | | | 131423 131424 131425 131426 131427 131428 131429 131430 131431 131432 131433 131434 131435 131436 131437 131438 131439 |
/* Version 3.31.0 and later */
sqlite3_int64 (*hard_heap_limit64)(sqlite3_int64);
const char *(*uri_key)(const char*,int);
const char *(*filename_database)(const char*);
const char *(*filename_journal)(const char*);
const char *(*filename_wal)(const char*);
/* Version 3.32.0 and later */
const char *(*create_filename)(const char*,const char*,const char*,
int,const char**);
void (*free_filename)(const char*);
sqlite3_file *(*database_file_object)(const char*);
/* Version 3.34.0 and later */
int (*txn_state)(sqlite3*,const char*);
/* Version 3.36.1 and later */
sqlite3_int64 (*changes64)(sqlite3*);
sqlite3_int64 (*total_changes64)(sqlite3*);
/* Version 3.37.0 and later */
|
| ︙ | ︙ | |||
138502 138503 138504 138505 138506 138507 138508 138509 138510 138511 138512 138513 138514 138515 |
case SRT_EphemTab: {
int r1 = sqlite3GetTempRange(pParse, nPrefixReg+1);
testcase( eDest==SRT_Table );
testcase( eDest==SRT_EphemTab );
testcase( eDest==SRT_Fifo );
testcase( eDest==SRT_DistFifo );
sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1+nPrefixReg);
#ifndef SQLITE_OMIT_CTE
if( eDest==SRT_DistFifo ){
/* If the destination is DistFifo, then cursor (iParm+1) is open
** on an ephemeral index. If the current row is already present
** in the index, do not write it to the output. If not, add the
** current row to the index and proceed with writing it to the
** output table as well. */
| > > > | 138541 138542 138543 138544 138545 138546 138547 138548 138549 138550 138551 138552 138553 138554 138555 138556 138557 |
case SRT_EphemTab: {
int r1 = sqlite3GetTempRange(pParse, nPrefixReg+1);
testcase( eDest==SRT_Table );
testcase( eDest==SRT_EphemTab );
testcase( eDest==SRT_Fifo );
testcase( eDest==SRT_DistFifo );
sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1+nPrefixReg);
if( pDest->zAffSdst ){
sqlite3VdbeChangeP4(v, -1, pDest->zAffSdst, nResultCol);
}
#ifndef SQLITE_OMIT_CTE
if( eDest==SRT_DistFifo ){
/* If the destination is DistFifo, then cursor (iParm+1) is open
** on an ephemeral index. If the current row is already present
** in the index, do not write it to the output. If not, add the
** current row to the index and proceed with writing it to the
** output table as well. */
|
| ︙ | ︙ | |||
141364 141365 141366 141367 141368 141369 141370 141371 141372 141373 141374 141375 141376 141377 | ** (17d1) aggregate, or ** (17d2) DISTINCT ** (17e) the subquery may not contain window functions, and ** (17f) the subquery must not be the RHS of a LEFT JOIN. ** (17g) either the subquery is the first element of the outer ** query or there are no RIGHT or FULL JOINs in any arm ** of the subquery. (This is a duplicate of condition (27b).) ** ** The parent and sub-query may contain WHERE clauses. Subject to ** rules (11), (13) and (14), they may also contain ORDER BY, ** LIMIT and OFFSET clauses. The subquery cannot use any compound ** operator other than UNION ALL because all the other compound ** operators have an implied DISTINCT which is disallowed by ** restriction (4). | > > | 141406 141407 141408 141409 141410 141411 141412 141413 141414 141415 141416 141417 141418 141419 141420 141421 | ** (17d1) aggregate, or ** (17d2) DISTINCT ** (17e) the subquery may not contain window functions, and ** (17f) the subquery must not be the RHS of a LEFT JOIN. ** (17g) either the subquery is the first element of the outer ** query or there are no RIGHT or FULL JOINs in any arm ** of the subquery. (This is a duplicate of condition (27b).) ** (17h) The corresponding result set expressions in all arms of the ** compound must have the same affinity. ** ** The parent and sub-query may contain WHERE clauses. Subject to ** rules (11), (13) and (14), they may also contain ORDER BY, ** LIMIT and OFFSET clauses. The subquery cannot use any compound ** operator other than UNION ALL because all the other compound ** operators have an implied DISTINCT which is disallowed by ** restriction (4). |
| ︙ | ︙ | |||
141540 141541 141542 141543 141544 141545 141546 141547 141548 141549 141550 141551 141552 141553 |
/* Restriction (17): If the sub-query is a compound SELECT, then it must
** use only the UNION ALL operator. And none of the simple select queries
** that make up the compound SELECT are allowed to be aggregate or distinct
** queries.
*/
if( pSub->pPrior ){
if( pSub->pOrderBy ){
return 0; /* Restriction (20) */
}
if( isAgg || (p->selFlags & SF_Distinct)!=0 || isOuterJoin>0 ){
return 0; /* (17d1), (17d2), or (17f) */
}
for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){
| > | 141584 141585 141586 141587 141588 141589 141590 141591 141592 141593 141594 141595 141596 141597 141598 |
/* Restriction (17): If the sub-query is a compound SELECT, then it must
** use only the UNION ALL operator. And none of the simple select queries
** that make up the compound SELECT are allowed to be aggregate or distinct
** queries.
*/
if( pSub->pPrior ){
int ii;
if( pSub->pOrderBy ){
return 0; /* Restriction (20) */
}
if( isAgg || (p->selFlags & SF_Distinct)!=0 || isOuterJoin>0 ){
return 0; /* (17d1), (17d2), or (17f) */
}
for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){
|
| ︙ | ︙ | |||
141572 141573 141574 141575 141576 141577 141578 |
return 0; /* Restrictions (17g), (27b) */
}
testcase( pSub1->pSrc->nSrc>1 );
}
/* Restriction (18). */
if( p->pOrderBy ){
| < > > > > > > > > > > > > > > > | 141617 141618 141619 141620 141621 141622 141623 141624 141625 141626 141627 141628 141629 141630 141631 141632 141633 141634 141635 141636 141637 141638 141639 141640 141641 141642 141643 141644 141645 141646 141647 141648 141649 141650 141651 141652 |
return 0; /* Restrictions (17g), (27b) */
}
testcase( pSub1->pSrc->nSrc>1 );
}
/* Restriction (18). */
if( p->pOrderBy ){
for(ii=0; ii<p->pOrderBy->nExpr; ii++){
if( p->pOrderBy->a[ii].u.x.iOrderByCol==0 ) return 0;
}
}
/* Restriction (23) */
if( (p->selFlags & SF_Recursive) ) return 0;
/* Restriction (17h) */
for(ii=0; ii<pSub->pEList->nExpr; ii++){
char aff;
assert( pSub->pEList->a[ii].pExpr!=0 );
aff = sqlite3ExprAffinity(pSub->pEList->a[ii].pExpr);
for(pSub1=pSub->pPrior; pSub1; pSub1=pSub1->pPrior){
assert( pSub1->pEList!=0 );
assert( pSub1->pEList->nExpr>ii );
assert( pSub1->pEList->a[ii].pExpr!=0 );
if( sqlite3ExprAffinity(pSub1->pEList->a[ii].pExpr)!=aff ){
return 0;
}
}
}
if( pSrc->nSrc>1 ){
if( pParse->nSelect>500 ) return 0;
if( OptimizationDisabled(db, SQLITE_FlttnUnionAll) ) return 0;
aCsrMap = sqlite3DbMallocZero(db, ((i64)pParse->nTab+1)*sizeof(int));
if( aCsrMap ) aCsrMap[0] = pParse->nTab;
}
|
| ︙ | ︙ | |||
142223 142224 142225 142226 142227 142228 142229 142230 142231 142232 142233 142234 142235 142236 142237 142238 142239 142240 142241 142242 142243 142244 142245 142246 142247 142248 142249 142250 142251 142252 142253 142254 142255 142256 |
** all window-functions used by the sub-query. It is safe to
** filter out entire partitions, as this does not change the
** window over which any window-function is calculated.
**
** (7) The inner query is a Common Table Expression (CTE) that should
** be materialized. (This restriction is implemented in the calling
** routine.)
**
** Return 0 if no changes are made and non-zero if one or more WHERE clause
** terms are duplicated into the subquery.
*/
static int pushDownWhereTerms(
Parse *pParse, /* Parse context (for malloc() and error reporting) */
Select *pSubq, /* The subquery whose WHERE clause is to be augmented */
Expr *pWhere, /* The WHERE clause of the outer query */
SrcItem *pSrc /* The subquery term of the outer FROM clause */
){
Expr *pNew;
int nChng = 0;
if( pWhere==0 ) return 0;
if( pSubq->selFlags & (SF_Recursive|SF_MultiPart) ) return 0;
if( pSrc->fg.jointype & (JT_LTORJ|JT_RIGHT) ) return 0;
#ifndef SQLITE_OMIT_WINDOWFUNC
if( pSubq->pPrior ){
Select *pSel;
for(pSel=pSubq; pSel; pSel=pSel->pPrior){
if( pSel->pWin ) return 0; /* restriction (6b) */
}
}else{
if( pSubq->pWin && pSubq->pWin->pPartition==0 ) return 0;
}
#endif
| > > > > > > > > > > > | 142282 142283 142284 142285 142286 142287 142288 142289 142290 142291 142292 142293 142294 142295 142296 142297 142298 142299 142300 142301 142302 142303 142304 142305 142306 142307 142308 142309 142310 142311 142312 142313 142314 142315 142316 142317 142318 142319 142320 142321 142322 142323 142324 142325 142326 |
** all window-functions used by the sub-query. It is safe to
** filter out entire partitions, as this does not change the
** window over which any window-function is calculated.
**
** (7) The inner query is a Common Table Expression (CTE) that should
** be materialized. (This restriction is implemented in the calling
** routine.)
**
** (8) The subquery may not be a compound that uses UNION, INTERSECT,
** or EXCEPT. (We could, perhaps, relax this restriction to allow
** this case if none of the comparisons operators between left and
** right arms of the compound use a collation other than BINARY.
** But it is a lot of work to check that case for an obscure and
** minor optimization, so we omit it for now.)
**
** Return 0 if no changes are made and non-zero if one or more WHERE clause
** terms are duplicated into the subquery.
*/
static int pushDownWhereTerms(
Parse *pParse, /* Parse context (for malloc() and error reporting) */
Select *pSubq, /* The subquery whose WHERE clause is to be augmented */
Expr *pWhere, /* The WHERE clause of the outer query */
SrcItem *pSrc /* The subquery term of the outer FROM clause */
){
Expr *pNew;
int nChng = 0;
if( pWhere==0 ) return 0;
if( pSubq->selFlags & (SF_Recursive|SF_MultiPart) ) return 0;
if( pSrc->fg.jointype & (JT_LTORJ|JT_RIGHT) ) return 0;
#ifndef SQLITE_OMIT_WINDOWFUNC
if( pSubq->pPrior ){
Select *pSel;
for(pSel=pSubq; pSel; pSel=pSel->pPrior){
u8 op = pSel->op;
assert( op==TK_ALL || op==TK_SELECT
|| op==TK_UNION || op==TK_INTERSECT || op==TK_EXCEPT );
if( op!=TK_ALL && op!=TK_SELECT ) return 0; /* restriction (8) */
if( pSel->pWin ) return 0; /* restriction (6b) */
}
}else{
if( pSubq->pWin && pSubq->pWin->pPartition==0 ) return 0;
}
#endif
|
| ︙ | ︙ | |||
144275 144276 144277 144278 144279 144280 144281 144282 144283 144284 144285 144286 144287 144288 144289 |
onceAddr = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
VdbeComment((v, "materialize %!S", pItem));
}else{
VdbeNoopComment((v, "materialize %!S", pItem));
}
sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
ExplainQueryPlan((pParse, 1, "MATERIALIZE %!S", pItem));
sqlite3Select(pParse, pSub, &dest);
pItem->pTab->nRowLogEst = pSub->nSelectRow;
if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr);
sqlite3VdbeAddOp2(v, OP_Return, pItem->regReturn, topAddr+1);
VdbeComment((v, "end %!S", pItem));
sqlite3VdbeJumpHere(v, topAddr);
sqlite3ClearTempRegCache(pParse);
if( pItem->fg.isCte && pItem->fg.isCorrelated==0 ){
| > > > | 144345 144346 144347 144348 144349 144350 144351 144352 144353 144354 144355 144356 144357 144358 144359 144360 144361 144362 |
onceAddr = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
VdbeComment((v, "materialize %!S", pItem));
}else{
VdbeNoopComment((v, "materialize %!S", pItem));
}
sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
ExplainQueryPlan((pParse, 1, "MATERIALIZE %!S", pItem));
dest.zAffSdst = sqlite3TableAffinityStr(db, pItem->pTab);
sqlite3Select(pParse, pSub, &dest);
sqlite3DbFree(db, dest.zAffSdst);
dest.zAffSdst = 0;
pItem->pTab->nRowLogEst = pSub->nSelectRow;
if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr);
sqlite3VdbeAddOp2(v, OP_Return, pItem->regReturn, topAddr+1);
VdbeComment((v, "end %!S", pItem));
sqlite3VdbeJumpHere(v, topAddr);
sqlite3ClearTempRegCache(pParse);
if( pItem->fg.isCte && pItem->fg.isCorrelated==0 ){
|
| ︙ | ︙ | |||
175723 175724 175725 175726 175727 175728 175729 175730 175731 175732 175733 175734 175735 175736 |
}
if( rc!=SQLITE_OK ){
if( rc==SQLITE_NOMEM ) sqlite3OomFault(db);
sqlite3ErrorWithMsg(db, rc, zErrMsg ? "%s" : 0, zErrMsg);
sqlite3_free(zErrMsg);
goto opendb_out;
}
/* Open the backend database driver */
rc = sqlite3BtreeOpen(db->pVfs, zOpen, db, &db->aDb[0].pBt, 0,
flags | SQLITE_OPEN_MAIN_DB);
if( rc!=SQLITE_OK ){
if( rc==SQLITE_IOERR_NOMEM ){
rc = SQLITE_NOMEM_BKPT;
| > > > > > > | 175796 175797 175798 175799 175800 175801 175802 175803 175804 175805 175806 175807 175808 175809 175810 175811 175812 175813 175814 175815 |
}
if( rc!=SQLITE_OK ){
if( rc==SQLITE_NOMEM ) sqlite3OomFault(db);
sqlite3ErrorWithMsg(db, rc, zErrMsg ? "%s" : 0, zErrMsg);
sqlite3_free(zErrMsg);
goto opendb_out;
}
assert( db->pVfs!=0 );
#if SQLITE_OS_KV || defined(SQLITE_OS_KV_OPTIONAL)
if( sqlite3_stricmp(db->pVfs->zName, "kvvfs")==0 ){
db->temp_store = 2;
}
#endif
/* Open the backend database driver */
rc = sqlite3BtreeOpen(db->pVfs, zOpen, db, &db->aDb[0].pBt, 0,
flags | SQLITE_OPEN_MAIN_DB);
if( rc!=SQLITE_OK ){
if( rc==SQLITE_IOERR_NOMEM ){
rc = SQLITE_NOMEM_BKPT;
|
| ︙ | ︙ | |||
176832 176833 176834 176835 176836 176837 176838 | ** and query parameters. The pointer returned is valid for use by ** sqlite3_filename_database() and sqlite3_uri_parameter() and related ** functions. ** ** Memory layout must be compatible with that generated by the pager ** and expected by sqlite3_uri_parameter() and databaseName(). */ | | | 176911 176912 176913 176914 176915 176916 176917 176918 176919 176920 176921 176922 176923 176924 176925 |
** and query parameters. The pointer returned is valid for use by
** sqlite3_filename_database() and sqlite3_uri_parameter() and related
** functions.
**
** Memory layout must be compatible with that generated by the pager
** and expected by sqlite3_uri_parameter() and databaseName().
*/
SQLITE_API const char *sqlite3_create_filename(
const char *zDatabase,
const char *zJournal,
const char *zWal,
int nParam,
const char **azParam
){
sqlite3_int64 nByte;
|
| ︙ | ︙ | |||
176868 176869 176870 176871 176872 176873 176874 | } /* ** Free memory obtained from sqlite3_create_filename(). It is a severe ** error to call this routine with any parameter other than a pointer ** previously obtained from sqlite3_create_filename() or a NULL pointer. */ | | | | | 176947 176948 176949 176950 176951 176952 176953 176954 176955 176956 176957 176958 176959 176960 176961 176962 176963 176964 |
}
/*
** Free memory obtained from sqlite3_create_filename(). It is a severe
** error to call this routine with any parameter other than a pointer
** previously obtained from sqlite3_create_filename() or a NULL pointer.
*/
SQLITE_API void sqlite3_free_filename(const char *p){
if( p==0 ) return;
p = databaseName(p);
sqlite3_free((char*)p - 4);
}
/*
** This is a utility routine, useful to VFS implementations, that checks
** to see if a database file was a URI that contained a specific query
** parameter, and if so obtains the value of the query parameter.
|
| ︙ | ︙ | |||
214152 214153 214154 214155 214156 214157 214158 |
switch( i ){
case 0: { /* pgno */
sqlite3_result_int(ctx, pCsr->pgno);
break;
}
case 1: { /* data */
DbPage *pDbPage = 0;
| > > > > > | | | | | | > | | 214231 214232 214233 214234 214235 214236 214237 214238 214239 214240 214241 214242 214243 214244 214245 214246 214247 214248 214249 214250 214251 214252 214253 214254 214255 214256 214257 214258 214259 214260 214261 214262 214263 214264 214265 |
switch( i ){
case 0: { /* pgno */
sqlite3_result_int(ctx, pCsr->pgno);
break;
}
case 1: { /* data */
DbPage *pDbPage = 0;
if( pCsr->pgno==((PENDING_BYTE/pCsr->szPage)+1) ){
/* The pending byte page. Assume it is zeroed out. Attempting to
** request this page from the page is an SQLITE_CORRUPT error. */
sqlite3_result_zeroblob(ctx, pCsr->szPage);
}else{
rc = sqlite3PagerGet(pCsr->pPager, pCsr->pgno, (DbPage**)&pDbPage, 0);
if( rc==SQLITE_OK ){
sqlite3_result_blob(ctx, sqlite3PagerGetData(pDbPage), pCsr->szPage,
SQLITE_TRANSIENT);
}
sqlite3PagerUnref(pDbPage);
}
break;
}
default: { /* schema */
sqlite3 *db = sqlite3_context_db_handle(ctx);
sqlite3_result_text(ctx, db->aDb[pCsr->iDb].zDbSName, -1, SQLITE_STATIC);
break;
}
}
return rc;
}
static int dbpageRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
DbpageCursor *pCsr = (DbpageCursor *)pCursor;
*pRowid = pCsr->pgno;
return SQLITE_OK;
}
|
| ︙ | ︙ | |||
214226 214227 214228 214229 214230 214231 214232 |
){
zErr = "bad page value";
goto update_fail;
}
pPager = sqlite3BtreePager(pBt);
rc = sqlite3PagerGet(pPager, pgno, (DbPage**)&pDbPage, 0);
if( rc==SQLITE_OK ){
| > > > | | | < < | 214311 214312 214313 214314 214315 214316 214317 214318 214319 214320 214321 214322 214323 214324 214325 214326 214327 214328 214329 214330 |
){
zErr = "bad page value";
goto update_fail;
}
pPager = sqlite3BtreePager(pBt);
rc = sqlite3PagerGet(pPager, pgno, (DbPage**)&pDbPage, 0);
if( rc==SQLITE_OK ){
const void *pData = sqlite3_value_blob(argv[3]);
assert( pData!=0 || pTab->db->mallocFailed );
if( pData
&& (rc = sqlite3PagerWrite(pDbPage))==SQLITE_OK
){
memcpy(sqlite3PagerGetData(pDbPage), pData, szPage);
}
}
sqlite3PagerUnref(pDbPage);
return rc;
update_fail:
sqlite3_free(pVtab->zErrMsg);
|
| ︙ | ︙ | |||
238384 238385 238386 238387 238388 238389 238390 |
static void fts5SourceIdFunc(
sqlite3_context *pCtx, /* Function call context */
int nArg, /* Number of args */
sqlite3_value **apUnused /* Function arguments */
){
assert( nArg==0 );
UNUSED_PARAM2(nArg, apUnused);
| | | 238470 238471 238472 238473 238474 238475 238476 238477 238478 238479 238480 238481 238482 238483 238484 |
static void fts5SourceIdFunc(
sqlite3_context *pCtx, /* Function call context */
int nArg, /* Number of args */
sqlite3_value **apUnused /* Function arguments */
){
assert( nArg==0 );
UNUSED_PARAM2(nArg, apUnused);
sqlite3_result_text(pCtx, "fts5: 2022-11-04 16:39:39 0e5597ce5353dea2cdb092b166b57ba1d60f8115eb468349f2b2869803691a2c", -1, SQLITE_TRANSIENT);
}
/*
** Return true if zName is the extension on one of the shadow tables used
** by this module.
*/
static int fts5ShadowName(const char *zName){
|
| ︙ | ︙ |
Changes to extsrc/sqlite3.h.
| ︙ | ︙ | |||
144 145 146 147 148 149 150 | ** ** See also: [sqlite3_libversion()], ** [sqlite3_libversion_number()], [sqlite3_sourceid()], ** [sqlite_version()] and [sqlite_source_id()]. */ #define SQLITE_VERSION "3.40.0" #define SQLITE_VERSION_NUMBER 3040000 | | | 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | ** ** See also: [sqlite3_libversion()], ** [sqlite3_libversion_number()], [sqlite3_sourceid()], ** [sqlite_version()] and [sqlite_source_id()]. */ #define SQLITE_VERSION "3.40.0" #define SQLITE_VERSION_NUMBER 3040000 #define SQLITE_SOURCE_ID "2022-11-04 16:39:39 0e5597ce5353dea2cdb092b166b57ba1d60f8115eb468349f2b2869803691a2c" /* ** CAPI3REF: Run-Time Library Version Numbers ** KEYWORDS: sqlite3_version sqlite3_sourceid ** ** These interfaces provide the same information as the [SQLITE_VERSION], ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros |
| ︙ | ︙ | |||
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 | ** A pointer to the opaque sqlite3_api_routines structure is passed as ** the third parameter to entry points of [loadable extensions]. This ** structure must be typedefed in order to work around compiler warnings ** on some platforms. */ typedef struct sqlite3_api_routines sqlite3_api_routines; /* ** CAPI3REF: OS Interface Object ** ** An instance of the sqlite3_vfs object defines the interface between ** the SQLite core and the underlying operating system. The "vfs" ** in the name of the object stands for "virtual file system". See ** the [VFS | VFS documentation] for further information. | > > > > > > > > > > > > > > > > > > > > | 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 | ** A pointer to the opaque sqlite3_api_routines structure is passed as ** the third parameter to entry points of [loadable extensions]. This ** structure must be typedefed in order to work around compiler warnings ** on some platforms. */ typedef struct sqlite3_api_routines sqlite3_api_routines; /* ** CAPI3REF: File Name ** ** Type [sqlite3_filename] is used by SQLite to pass filenames to the ** xOpen method of a [VFS]. It may be cast to (const char*) and treated ** as a normal, nul-terminated, UTF-8 buffer containing the filename, but ** may also be passed to special APIs such as: ** ** <ul> ** <li> sqlite3_filename_database() ** <li> sqlite3_filename_journal() ** <li> sqlite3_filename_wal() ** <li> sqlite3_uri_parameter() ** <li> sqlite3_uri_boolean() ** <li> sqlite3_uri_int64() ** <li> sqlite3_uri_key() ** </ul> */ typedef const char *sqlite3_filename; /* ** CAPI3REF: OS Interface Object ** ** An instance of the sqlite3_vfs object defines the interface between ** the SQLite core and the underlying operating system. The "vfs" ** in the name of the object stands for "virtual file system". See ** the [VFS | VFS documentation] for further information. |
| ︙ | ︙ | |||
1437 1438 1439 1440 1441 1442 1443 |
struct sqlite3_vfs {
int iVersion; /* Structure version number (currently 3) */
int szOsFile; /* Size of subclassed sqlite3_file */
int mxPathname; /* Maximum file pathname length */
sqlite3_vfs *pNext; /* Next registered VFS */
const char *zName; /* Name of this virtual file system */
void *pAppData; /* Pointer to application-specific data */
| | | 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 |
struct sqlite3_vfs {
int iVersion; /* Structure version number (currently 3) */
int szOsFile; /* Size of subclassed sqlite3_file */
int mxPathname; /* Maximum file pathname length */
sqlite3_vfs *pNext; /* Next registered VFS */
const char *zName; /* Name of this virtual file system */
void *pAppData; /* Pointer to application-specific data */
int (*xOpen)(sqlite3_vfs*, sqlite3_filename zName, sqlite3_file*,
int flags, int *pOutFlags);
int (*xDelete)(sqlite3_vfs*, const char *zName, int syncDir);
int (*xAccess)(sqlite3_vfs*, const char *zName, int flags, int *pResOut);
int (*xFullPathname)(sqlite3_vfs*, const char *zName, int nOut, char *zOut);
void *(*xDlOpen)(sqlite3_vfs*, const char *zFilename);
void (*xDlError)(sqlite3_vfs*, int nByte, char *zErrMsg);
void (*(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol))(void);
|
| ︙ | ︙ | |||
3707 3708 3709 3710 3711 3712 3713 | ** routines would only work if F was the name of the main database file. ** When the F parameter is the name of the rollback journal or WAL file, ** it has access to all the same query parameters as were found on the ** main database file. ** ** See the [URI filename] documentation for additional information. */ | | | | | | 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 | ** routines would only work if F was the name of the main database file. ** When the F parameter is the name of the rollback journal or WAL file, ** it has access to all the same query parameters as were found on the ** main database file. ** ** See the [URI filename] documentation for additional information. */ SQLITE_API const char *sqlite3_uri_parameter(sqlite3_filename z, const char *zParam); SQLITE_API int sqlite3_uri_boolean(sqlite3_filename z, const char *zParam, int bDefault); SQLITE_API sqlite3_int64 sqlite3_uri_int64(sqlite3_filename, const char*, sqlite3_int64); SQLITE_API const char *sqlite3_uri_key(sqlite3_filename z, int N); /* ** CAPI3REF: Translate filenames ** ** These routines are available to [VFS|custom VFS implementations] for ** translating filenames between the main database file, the journal file, ** and the WAL file. |
| ︙ | ︙ | |||
3739 3740 3741 3742 3743 3744 3745 | ** WAL file. ** ** In all of the above, if F is not the name of a database, journal or WAL ** filename passed into the VFS from the SQLite core and F is not the ** return value from [sqlite3_db_filename()], then the result is ** undefined and is likely a memory access violation. */ | | | | | 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 | ** WAL file. ** ** In all of the above, if F is not the name of a database, journal or WAL ** filename passed into the VFS from the SQLite core and F is not the ** return value from [sqlite3_db_filename()], then the result is ** undefined and is likely a memory access violation. */ SQLITE_API const char *sqlite3_filename_database(sqlite3_filename); SQLITE_API const char *sqlite3_filename_journal(sqlite3_filename); SQLITE_API const char *sqlite3_filename_wal(sqlite3_filename); /* ** CAPI3REF: Database File Corresponding To A Journal ** ** ^If X is the name of a rollback or WAL-mode journal file that is ** passed into the xOpen method of [sqlite3_vfs], then ** sqlite3_database_file_object(X) returns a pointer to the [sqlite3_file] |
| ︙ | ︙ | |||
3807 3808 3809 3810 3811 3812 3813 | ** sqlite3_create_filename(), then bad things such as heap ** corruption or segfaults may occur. The value Y should not be ** used again after sqlite3_free_filename(Y) has been called. This means ** that if the [sqlite3_vfs.xOpen()] method of a VFS has been called using Y, ** then the corresponding [sqlite3_module.xClose() method should also be ** invoked prior to calling sqlite3_free_filename(Y). */ | | | | 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 | ** sqlite3_create_filename(), then bad things such as heap ** corruption or segfaults may occur. The value Y should not be ** used again after sqlite3_free_filename(Y) has been called. This means ** that if the [sqlite3_vfs.xOpen()] method of a VFS has been called using Y, ** then the corresponding [sqlite3_module.xClose() method should also be ** invoked prior to calling sqlite3_free_filename(Y). */ SQLITE_API sqlite3_filename sqlite3_create_filename( const char *zDatabase, const char *zJournal, const char *zWal, int nParam, const char **azParam ); SQLITE_API void sqlite3_free_filename(sqlite3_filename); /* ** CAPI3REF: Error Codes And Messages ** METHOD: sqlite3 ** ** ^If the most recent sqlite3_* API call associated with ** [database connection] D failed, then the sqlite3_errcode(D) interface |
| ︙ | ︙ | |||
6348 6349 6350 6351 6352 6353 6354 | ** <li> [sqlite3_uri_boolean()] ** <li> [sqlite3_uri_int64()] ** <li> [sqlite3_filename_database()] ** <li> [sqlite3_filename_journal()] ** <li> [sqlite3_filename_wal()] ** </ul> */ | | | 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 | ** <li> [sqlite3_uri_boolean()] ** <li> [sqlite3_uri_int64()] ** <li> [sqlite3_filename_database()] ** <li> [sqlite3_filename_journal()] ** <li> [sqlite3_filename_wal()] ** </ul> */ SQLITE_API sqlite3_filename sqlite3_db_filename(sqlite3 *db, const char *zDbName); /* ** CAPI3REF: Determine if a database is read-only ** METHOD: sqlite3 ** ** ^The sqlite3_db_readonly(D,N) interface returns 1 if the database N ** of connection D is read-only, 0 if it is read/write, or -1 if N is not |
| ︙ | ︙ |