1 From 13a64d3c7fb541be8cb753c575837c1b84f5d9fd Mon Sep 17 00:00:00 2001
2 From: Scott Hess <shess@chromium.org>
3 Date: Sat, 20 Jul 2013 11:42:21 -0700
4 Subject: [PATCH 06/16] Virtual table supporting recovery of corrupted
7 "recover" implements a virtual table which uses the SQLite pager layer
8 to read table pages and pull out the data which is structurally sound
9 (at least at the storage layer).
13 Since this implements a new feature for SQLite, the review URLs aren't
14 listed. This patch and the top of recover.c should be considered
15 authoritative. The history is mostly under
16 third_party/sqlite/src/src/{recover,recover-alt}.c .
18 third_party/sqlite/src/Makefile.in | 1 +
19 third_party/sqlite/src/Makefile.linux-gcc | 4 +
20 third_party/sqlite/src/main.mk | 4 +-
21 third_party/sqlite/src/src/main.c | 8 +
22 third_party/sqlite/src/src/recover.c | 2164 ++++++++++++++++++++++++++++
23 third_party/sqlite/src/src/sqlite.h.in | 11 +
24 third_party/sqlite/src/test/recover.test | 147 ++
25 third_party/sqlite/src/test/recover0.test | 532 +++++++
26 third_party/sqlite/src/test/recover1.test | 429 ++++++
27 third_party/sqlite/src/test/recover2.test | 157 ++
28 third_party/sqlite/src/tool/mksqlite3c.tcl | 2 +
29 11 files changed, 3458 insertions(+), 1 deletion(-)
30 create mode 100644 third_party/sqlite/src/src/recover.c
31 create mode 100644 third_party/sqlite/src/test/recover.test
32 create mode 100644 third_party/sqlite/src/test/recover0.test
33 create mode 100644 third_party/sqlite/src/test/recover1.test
34 create mode 100644 third_party/sqlite/src/test/recover2.test
36 diff --git a/third_party/sqlite/src/Makefile.in b/third_party/sqlite/src/Makefile.in
37 index a2213e8..1389486 100644
38 --- a/third_party/sqlite/src/Makefile.in
39 +++ b/third_party/sqlite/src/Makefile.in
40 @@ -253,6 +253,7 @@ SRC = \
41 $(TOP)/src/prepare.c \
44 + $(TOP)/src/recover.c \
45 $(TOP)/src/resolve.c \
48 diff --git a/third_party/sqlite/src/Makefile.linux-gcc b/third_party/sqlite/src/Makefile.linux-gcc
49 index 554bf56..e631816 100644
50 --- a/third_party/sqlite/src/Makefile.linux-gcc
51 +++ b/third_party/sqlite/src/Makefile.linux-gcc
52 @@ -82,6 +82,10 @@ OPTS += -DSQLITE_MEMDEBUG=1
53 # TODO(shess) I can't see why I need this setting.
56 +# The recover virtual table is not generally enabled. Enable it for testing
58 +OPTS += -DDEFAULT_ENABLE_RECOVER=1
60 #### The suffix to add to executable files. ".exe" for windows.
63 diff --git a/third_party/sqlite/src/main.mk b/third_party/sqlite/src/main.mk
64 index dc56b0d..2189fd6 100644
65 --- a/third_party/sqlite/src/main.mk
66 +++ b/third_party/sqlite/src/main.mk
67 @@ -65,7 +65,7 @@ LIBOBJ+= vdbe.o parse.o \
68 mutex.o mutex_noop.o mutex_unix.o mutex_w32.o \
69 notify.o opcodes.o os.o os_unix.o os_win.o \
70 pager.o pcache.o pcache1.o pragma.o prepare.o printf.o \
71 - random.o resolve.o rowset.o rtree.o select.o status.o \
72 + random.o recover.o resolve.o rowset.o rtree.o select.o status.o \
73 table.o threads.o tokenize.o trigger.o \
74 update.o userauth.o util.o vacuum.o \
75 vdbeapi.o vdbeaux.o vdbeblob.o vdbemem.o vdbesort.o \
76 @@ -135,6 +135,7 @@ SRC = \
77 $(TOP)/src/prepare.c \
80 + $(TOP)/src/recover.c \
81 $(TOP)/src/resolve.c \
84 @@ -315,6 +316,7 @@ TESTSRC2 = \
85 $(TOP)/src/prepare.c \
88 + $(TOP)/src/recover.c \
90 $(TOP)/src/pcache1.c \
92 diff --git a/third_party/sqlite/src/src/main.c b/third_party/sqlite/src/src/main.c
93 index fc03700..d15ab9bb 100644
94 --- a/third_party/sqlite/src/src/main.c
95 +++ b/third_party/sqlite/src/src/main.c
96 @@ -2644,6 +2644,14 @@ static int openDatabase(
100 +#ifdef DEFAULT_ENABLE_RECOVER
101 + /* Initialize recover virtual table for testing. */
102 + extern int recoverVtableInit(sqlite3 *db);
103 + if( !db->mallocFailed && rc==SQLITE_OK ){
104 + rc = recoverVtableInit(db);
108 #ifdef SQLITE_ENABLE_ICU
109 if( !db->mallocFailed && rc==SQLITE_OK ){
110 rc = sqlite3IcuInit(db);
111 diff --git a/third_party/sqlite/src/src/recover.c b/third_party/sqlite/src/src/recover.c
113 index 0000000..c996d53
115 +++ b/third_party/sqlite/src/src/recover.c
120 +** The author disclaims copyright to this source code. In place of
121 +** a legal notice, here is a blessing:
123 +** May you do good and not evil.
124 +** May you find forgiveness for yourself and forgive others.
125 +** May you share freely, never taking more than you give.
127 +/* TODO(shess): THIS MODULE IS STILL EXPERIMENTAL. DO NOT USE IT. */
128 +/* Implements a virtual table "recover" which can be used to recover
129 + * data from a corrupt table. The table is walked manually, with
130 + * corrupt items skipped. Additionally, any errors while reading will
133 + * Given a table with this definition:
135 + * CREATE TABLE Stuff (
136 + * name TEXT PRIMARY KEY,
137 + * value TEXT NOT NULL
140 + * to recover the data from teh table, you could do something like:
142 + * -- Attach another database, the original is not trustworthy.
143 + * ATTACH DATABASE '/tmp/db.db' AS rdb;
144 + * -- Create a new version of the table.
145 + * CREATE TABLE rdb.Stuff (
146 + * name TEXT PRIMARY KEY,
147 + * value TEXT NOT NULL
149 + * -- This will read the original table's data.
150 + * CREATE VIRTUAL TABLE temp.recover_Stuff using recover(
152 + * name TEXT STRICT NOT NULL, -- only real TEXT data allowed
153 + * value TEXT STRICT NOT NULL
155 + * -- Corruption means the UNIQUE constraint may no longer hold for
156 + * -- Stuff, so either OR REPLACE or OR IGNORE must be used.
157 + * INSERT OR REPLACE INTO rdb.Stuff (rowid, name, value )
158 + * SELECT rowid, name, value FROM temp.recover_Stuff;
159 + * DROP TABLE temp.recover_Stuff;
160 + * DETACH DATABASE rdb;
161 + * -- Move db.db to replace original db in filesystem.
166 + * Given the goal of dealing with corruption, it would not be safe to
167 + * create a recovery table in the database being recovered. So
168 + * recovery tables must be created in the temp database. They are not
169 + * appropriate to persist, in any case. [As a bonus, sqlite_master
170 + * tables can be recovered. Perhaps more cute than useful, though.]
172 + * The parameters are a specifier for the table to read, and a column
173 + * definition for each bit of data stored in that table. The named
174 + * table must be convertable to a root page number by reading the
175 + * sqlite_master table. Bare table names are assumed to be in
176 + * database 0 ("main"), other databases can be specified in db.table
179 + * Column definitions are similar to BUT NOT THE SAME AS those
180 + * provided to CREATE statements:
181 + * column-def: column-name [type-name [STRICT] [NOT NULL]]
182 + * type-name: (ANY|ROWID|INTEGER|FLOAT|NUMERIC|TEXT|BLOB)
184 + * Only those exact type names are accepted, there is no type
185 + * intuition. The only constraints accepted are STRICT (see below)
186 + * and NOT NULL. Anything unexpected will cause the create to fail.
188 + * ANY is a convenience to indicate that manifest typing is desired.
189 + * It is equivalent to not specifying a type at all. The results for
190 + * such columns will have the type of the data's storage. The exposed
191 + * schema will contain no type for that column.
193 + * ROWID is used for columns representing aliases to the rowid
194 + * (INTEGER PRIMARY KEY, with or without AUTOINCREMENT), to make the
195 + * concept explicit. Such columns are actually stored as NULL, so
196 + * they cannot be simply ignored. The exposed schema will be INTEGER
199 + * NOT NULL causes rows with a NULL in that column to be skipped. It
200 + * also adds NOT NULL to the column in the exposed schema. If the
201 + * table has ever had columns added using ALTER TABLE, then those
202 + * columns implicitly contain NULL for rows which have not been
203 + * updated. [Workaround using COALESCE() in your SELECT statement.]
205 + * The created table is read-only, with no indices. Any SELECT will
206 + * be a full-table scan, returning each valid row read from the
207 + * storage of the backing table. The rowid will be the rowid of the
208 + * row from the backing table. "Valid" means:
209 + * - The cell metadata for the row is well-formed. Mainly this means that
210 + * the cell header info describes a payload of the size indicated by
211 + * the cell's payload size.
212 + * - The cell does not run off the page.
213 + * - The cell does not overlap any other cell on the page.
214 + * - The cell contains doesn't contain too many columns.
215 + * - The types of the serialized data match the indicated types (see below).
218 + * Type affinity versus type storage.
220 + * http://www.sqlite.org/datatype3.html describes SQLite's type
221 + * affinity system. The system provides for automated coercion of
222 + * types in certain cases, transparently enough that many developers
223 + * do not realize that it is happening. Importantly, it implies that
224 + * the raw data stored in the database may not have the obvious type.
226 + * Differences between the stored data types and the expected data
227 + * types may be a signal of corruption. This module makes some
228 + * allowances for automatic coercion. It is important to be concious
229 + * of the difference between the schema exposed by the module, and the
230 + * data types read from storage. The following table describes how
231 + * the module interprets things:
233 + * type schema data STRICT
234 + * ---- ------ ---- ------
235 + * ANY <none> any any
236 + * ROWID INTEGER n/a n/a
237 + * INTEGER INTEGER integer integer
238 + * FLOAT FLOAT integer or float float
239 + * NUMERIC NUMERIC integer, float, or text integer or float
240 + * TEXT TEXT text or blob text
241 + * BLOB BLOB blob blob
243 + * type is the type provided to the recover module, schema is the
244 + * schema exposed by the module, data is the acceptable types of data
245 + * decoded from storage, and STRICT is a modification of that.
247 + * A very loose recovery system might use ANY for all columns, then
248 + * use the appropriate sqlite3_column_*() calls to coerce to expected
249 + * types. This doesn't provide much protection if a page from a
250 + * different table with the same column count is linked into an
251 + * inappropriate btree.
253 + * A very tight recovery system might use STRICT to enforce typing on
254 + * all columns, preferring to skip rows which are valid at the storage
255 + * level but don't contain the right types. Note that FLOAT STRICT is
256 + * almost certainly not appropriate, since integral values are
257 + * transparently stored as integers, when that is more efficient.
259 + * Another option is to use ANY for all columns and inspect each
260 + * result manually (using sqlite3_column_*). This should only be
261 + * necessary in cases where developers have used manifest typing (test
262 + * to make sure before you decide that you aren't using manifest
268 + * Leaf pages not referenced by interior nodes will not be found.
270 + * Leaf pages referenced from interior nodes of other tables will not
273 + * Rows referencing invalid overflow pages will be skipped.
275 + * SQlite rows have a header which describes how to interpret the rest
276 + * of the payload. The header can be valid in cases where the rest of
277 + * the record is actually corrupt (in the sense that the data is not
278 + * the intended data). This can especially happen WRT overflow pages,
279 + * as lack of atomic updates between pages is the primary form of
280 + * corruption I have seen in the wild.
282 +/* The implementation is via a series of cursors. The cursor
283 + * implementations follow the pattern:
285 + * // Creates the cursor using various initialization info.
286 + * int cursorCreate(...);
288 + * // Returns 1 if there is no more data, 0 otherwise.
289 + * int cursorEOF(Cursor *pCursor);
291 + * // Various accessors can be used if not at EOF.
293 + * // Move to the next item.
294 + * int cursorNext(Cursor *pCursor);
296 + * // Destroy the memory associated with the cursor.
297 + * void cursorDestroy(Cursor *pCursor);
299 + * References in the following are to sections at
300 + * http://www.sqlite.org/fileformat2.html .
302 + * RecoverLeafCursor iterates the records in a leaf table node
303 + * described in section 1.5 "B-tree Pages". When the node is
304 + * exhausted, an interior cursor is used to get the next leaf node,
305 + * and iteration continues there.
307 + * RecoverInteriorCursor iterates the child pages in an interior table
308 + * node described in section 1.5 "B-tree Pages". When the node is
309 + * exhausted, a parent interior cursor is used to get the next
310 + * interior node at the same level, and iteration continues there.
312 + * Together these record the path from the leaf level to the root of
313 + * the tree. Iteration happens from the leaves rather than the root
314 + * both for efficiency and putting the special case at the front of
315 + * the list is easier to implement.
317 + * RecoverCursor uses a RecoverLeafCursor to iterate the rows of a
318 + * table, returning results via the SQLite virtual table interface.
320 +/* TODO(shess): It might be useful to allow DEFAULT in types to
321 + * specify what to do for NULL when an ALTER TABLE case comes up.
322 + * Unfortunately, simply adding it to the exposed schema and using
323 + * sqlite3_result_null() does not cause the default to be generate.
324 + * Handling it ourselves seems hard, unfortunately.
332 +/* Internal SQLite things that are used:
333 + * u32, u64, i64 types.
334 + * Btree, Pager, and DbPage structs.
335 + * DbPage.pData, .pPager, and .pgno
337 + * sqlite3BtreePager() and sqlite3BtreeGetPageSize()
338 + * sqlite3PagerAcquire() and sqlite3PagerUnref()
341 +#include "sqliteInt.h"
343 +/* For debugging. */
345 +#define FNENTRY() fprintf(stderr, "In %s\n", __FUNCTION__)
350 +/* Generic constants and helper functions. */
352 +static const unsigned char kTableLeafPage = 0x0D;
353 +static const unsigned char kTableInteriorPage = 0x05;
355 +/* From section 1.5. */
356 +static const unsigned kiPageTypeOffset = 0;
357 +static const unsigned kiPageFreeBlockOffset = 1;
358 +static const unsigned kiPageCellCountOffset = 3;
359 +static const unsigned kiPageCellContentOffset = 5;
360 +static const unsigned kiPageFragmentedBytesOffset = 7;
361 +static const unsigned knPageLeafHeaderBytes = 8;
362 +/* Interior pages contain an additional field. */
363 +static const unsigned kiPageRightChildOffset = 8;
364 +static const unsigned kiPageInteriorHeaderBytes = 12;
366 +/* Accepted types are specified by a mask. */
367 +#define MASK_ROWID (1<<0)
368 +#define MASK_INTEGER (1<<1)
369 +#define MASK_FLOAT (1<<2)
370 +#define MASK_TEXT (1<<3)
371 +#define MASK_BLOB (1<<4)
372 +#define MASK_NULL (1<<5)
374 +/* Helpers to decode fixed-size fields. */
375 +static u32 decodeUnsigned16(const unsigned char *pData){
376 + return (pData[0]<<8) + pData[1];
378 +static u32 decodeUnsigned32(const unsigned char *pData){
379 + return (decodeUnsigned16(pData)<<16) + decodeUnsigned16(pData+2);
381 +static i64 decodeSigned(const unsigned char *pData, unsigned nBytes){
382 + i64 r = (char)(*pData);
389 +/* Derived from vdbeaux.c, sqlite3VdbeSerialGet(), case 7. */
390 +/* TODO(shess): Determine if swapMixedEndianFloat() applies. */
391 +static double decodeFloat64(const unsigned char *pData){
392 +#if !defined(NDEBUG)
393 + static const u64 t1 = ((u64)0x3ff00000)<<32;
394 + static const double r1 = 1.0;
396 + assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
398 + i64 x = decodeSigned(pData, 8);
400 + memcpy(&d, &x, sizeof(x));
404 +/* Return true if a varint can safely be read from pData/nData. */
405 +/* TODO(shess): DbPage points into the middle of a buffer which
406 + * contains the page data before DbPage. So code should always be
407 + * able to read a small number of varints safely. Consider whether to
408 + * trust that or not.
410 +static int checkVarint(const unsigned char *pData, unsigned nData){
413 + /* In the worst case the decoder takes all 8 bits of the 9th byte. */
418 + /* Look for a high-bit-clear byte in what's left. */
419 + for( i=0; i<nData; ++i ){
420 + if( !(pData[i]&0x80) ){
425 + /* Cannot decode in the space given. */
429 +/* Return 1 if n varints can be read from pData/nData. */
430 +static int checkVarints(const unsigned char *pData, unsigned nData,
432 + unsigned nCur = 0; /* Byte offset within current varint. */
433 + unsigned nFound = 0; /* Number of varints found. */
436 + /* In the worst case the decoder takes all 8 bits of the 9th byte. */
441 + for( i=0; nFound<n && i<nData; ++i ){
443 + if( nCur==9 || !(pData[i]&0x80) ){
452 +/* ctype and str[n]casecmp() can be affected by locale (eg, tr_TR).
453 + * These versions consider only the ASCII space.
455 +/* TODO(shess): It may be reasonable to just remove the need for these
456 + * entirely. The module could require "TEXT STRICT NOT NULL", not
457 + * "Text Strict Not Null" or whatever the developer felt like typing
458 + * that day. Handling corrupt data is a PERFECT place to be pedantic.
460 +static int ascii_isspace(char c){
461 + /* From fts3_expr.c */
462 + return c==' ' || c=='\t' || c=='\n' || c=='\r' || c=='\v' || c=='\f';
464 +static int ascii_isalnum(int x){
465 + /* From fts3_tokenizer1.c */
466 + return (x>='0' && x<='9') || (x>='A' && x<='Z') || (x>='a' && x<='z');
468 +static int ascii_tolower(int x){
469 + /* From fts3_tokenizer1.c */
470 + return (x>='A' && x<='Z') ? x-'A'+'a' : x;
472 +/* TODO(shess): Consider sqlite3_strnicmp() */
473 +static int ascii_strncasecmp(const char *s1, const char *s2, size_t n){
474 + const unsigned char *us1 = (const unsigned char *)s1;
475 + const unsigned char *us2 = (const unsigned char *)s2;
476 + while( *us1 && *us2 && n && ascii_tolower(*us1)==ascii_tolower(*us2) ){
479 + return n ? ascii_tolower(*us1)-ascii_tolower(*us2) : 0;
481 +static int ascii_strcasecmp(const char *s1, const char *s2){
482 + /* If s2 is equal through strlen(s1), will exit while() due to s1's
483 + * trailing NUL, and return NUL-s2[strlen(s1)].
485 + return ascii_strncasecmp(s1, s2, strlen(s1)+1);
488 +/* For some reason I kept making mistakes with offset calculations. */
489 +static const unsigned char *PageData(DbPage *pPage, unsigned iOffset){
490 + assert( iOffset<=pPage->nPageSize );
491 + return (unsigned char *)pPage->pData + iOffset;
494 +/* The first page in the file contains a file header in the first 100
495 + * bytes. The page's header information comes after that. Note that
496 + * the offsets in the page's header information are relative to the
497 + * beginning of the page, NOT the end of the page header.
499 +static const unsigned char *PageHeader(DbPage *pPage){
500 + if( pPage->pgno==1 ){
501 + const unsigned nDatabaseHeader = 100;
502 + return PageData(pPage, nDatabaseHeader);
504 + return PageData(pPage, 0);
508 +/* Helper to fetch the pager and page size for the named database. */
509 +static int GetPager(sqlite3 *db, const char *zName,
510 + Pager **pPager, unsigned *pnPageSize){
513 + for( i=0; i<db->nDb; ++i ){
514 + if( ascii_strcasecmp(db->aDb[i].zName, zName)==0 ){
515 + pBt = db->aDb[i].pBt;
520 + return SQLITE_ERROR;
523 + *pPager = sqlite3BtreePager(pBt);
524 + *pnPageSize = sqlite3BtreeGetPageSize(pBt) - sqlite3BtreeGetReserve(pBt);
528 +/* iSerialType is a type read from a record header. See "2.1 Record Format".
531 +/* Storage size of iSerialType in bytes. My interpretation of SQLite
532 + * documentation is that text and blob fields can have 32-bit length.
533 + * Values past 2^31-12 will need more than 32 bits to encode, which is
534 + * why iSerialType is u64.
536 +static u32 SerialTypeLength(u64 iSerialType){
537 + switch( iSerialType ){
538 + case 0 : return 0; /* NULL */
539 + case 1 : return 1; /* Various integers. */
545 + case 7 : return 8; /* 64-bit float. */
546 + case 8 : return 0; /* Constant 0. */
547 + case 9 : return 0; /* Constant 1. */
548 + case 10 : case 11 : assert( !"RESERVED TYPE"); return 0;
550 + return (u32)((iSerialType>>1) - 6);
553 +/* True if iSerialType refers to a blob. */
554 +static int SerialTypeIsBlob(u64 iSerialType){
555 + assert( iSerialType>=12 );
556 + return (iSerialType%2)==0;
559 +/* Returns true if the serialized type represented by iSerialType is
560 + * compatible with the given type mask.
562 +static int SerialTypeIsCompatible(u64 iSerialType, unsigned char mask){
563 + switch( iSerialType ){
564 + case 0 : return (mask&MASK_NULL)!=0;
565 + case 1 : return (mask&MASK_INTEGER)!=0;
566 + case 2 : return (mask&MASK_INTEGER)!=0;
567 + case 3 : return (mask&MASK_INTEGER)!=0;
568 + case 4 : return (mask&MASK_INTEGER)!=0;
569 + case 5 : return (mask&MASK_INTEGER)!=0;
570 + case 6 : return (mask&MASK_INTEGER)!=0;
571 + case 7 : return (mask&MASK_FLOAT)!=0;
572 + case 8 : return (mask&MASK_INTEGER)!=0;
573 + case 9 : return (mask&MASK_INTEGER)!=0;
574 + case 10 : assert( !"RESERVED TYPE"); return 0;
575 + case 11 : assert( !"RESERVED TYPE"); return 0;
577 + return (mask&(SerialTypeIsBlob(iSerialType) ? MASK_BLOB : MASK_TEXT));
580 +/* Versions of strdup() with return values appropriate for
581 + * sqlite3_free(). malloc.c has sqlite3DbStrDup()/NDup(), but those
582 + * need sqlite3DbFree(), which seems intrusive.
584 +static char *sqlite3_strndup(const char *z, unsigned n){
591 + zNew = sqlite3_malloc(n+1);
593 + memcpy(zNew, z, n);
598 +static char *sqlite3_strdup(const char *z){
602 + return sqlite3_strndup(z, strlen(z));
605 +/* Fetch the page number of zTable in zDb from sqlite_master in zDb,
606 + * and put it in *piRootPage.
608 +static int getRootPage(sqlite3 *db, const char *zDb, const char *zTable,
610 + char *zSql; /* SQL selecting root page of named element. */
611 + sqlite3_stmt *pStmt;
614 + if( strcmp(zTable, "sqlite_master")==0 ){
619 + zSql = sqlite3_mprintf("SELECT rootpage FROM %s.sqlite_master "
620 + "WHERE type = 'table' AND tbl_name = %Q",
623 + return SQLITE_NOMEM;
626 + rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
627 + sqlite3_free(zSql);
628 + if( rc!=SQLITE_OK ){
632 + /* Require a result. */
633 + rc = sqlite3_step(pStmt);
634 + if( rc==SQLITE_DONE ){
635 + rc = SQLITE_CORRUPT;
636 + }else if( rc==SQLITE_ROW ){
637 + *piRootPage = sqlite3_column_int(pStmt, 0);
639 + /* Require only one result. */
640 + rc = sqlite3_step(pStmt);
641 + if( rc==SQLITE_DONE ){
643 + }else if( rc==SQLITE_ROW ){
644 + rc = SQLITE_CORRUPT;
647 + sqlite3_finalize(pStmt);
651 +static int getEncoding(sqlite3 *db, const char *zDb, int* piEncoding){
652 + sqlite3_stmt *pStmt;
654 + char *zSql = sqlite3_mprintf("PRAGMA %s.encoding", zDb);
656 + return SQLITE_NOMEM;
659 + rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
660 + sqlite3_free(zSql);
661 + if( rc!=SQLITE_OK ){
665 + /* Require a result. */
666 + rc = sqlite3_step(pStmt);
667 + if( rc==SQLITE_DONE ){
668 + /* This case should not be possible. */
669 + rc = SQLITE_CORRUPT;
670 + }else if( rc==SQLITE_ROW ){
671 + if( sqlite3_column_type(pStmt, 0)==SQLITE_TEXT ){
672 + const char* z = (const char *)sqlite3_column_text(pStmt, 0);
673 + /* These strings match the literals in pragma.c. */
674 + if( !strcmp(z, "UTF-16le") ){
675 + *piEncoding = SQLITE_UTF16LE;
676 + }else if( !strcmp(z, "UTF-16be") ){
677 + *piEncoding = SQLITE_UTF16BE;
678 + }else if( !strcmp(z, "UTF-8") ){
679 + *piEncoding = SQLITE_UTF8;
681 + /* This case should not be possible. */
682 + *piEncoding = SQLITE_UTF8;
685 + /* This case should not be possible. */
686 + *piEncoding = SQLITE_UTF8;
689 + /* Require only one result. */
690 + rc = sqlite3_step(pStmt);
691 + if( rc==SQLITE_DONE ){
693 + }else if( rc==SQLITE_ROW ){
694 + /* This case should not be possible. */
695 + rc = SQLITE_CORRUPT;
698 + sqlite3_finalize(pStmt);
702 +/* Cursor for iterating interior nodes. Interior page cells contain a
703 + * child page number and a rowid. The child page contains items left
704 + * of the rowid (less than). The rightmost page of the subtree is
705 + * stored in the page header.
707 + * interiorCursorDestroy - release all resources associated with the
708 + * cursor and any parent cursors.
709 + * interiorCursorCreate - create a cursor with the given parent and page.
710 + * interiorCursorEOF - returns true if neither the cursor nor the
711 + * parent cursors can return any more data.
712 + * interiorCursorNextPage - fetch the next child page from the cursor.
714 + * Logically, interiorCursorNextPage() returns the next child page
715 + * number from the page the cursor is currently reading, calling the
716 + * parent cursor as necessary to get new pages to read, until done.
717 + * SQLITE_ROW if a page is returned, SQLITE_DONE if out of pages,
718 + * error otherwise. Unfortunately, if the table is corrupted
719 + * unexpected pages can be returned. If any unexpected page is found,
720 + * leaf or otherwise, it is returned to the caller for processing,
721 + * with the interior cursor left empty. The next call to
722 + * interiorCursorNextPage() will recurse to the parent cursor until an
723 + * interior page to iterate is returned.
725 + * Note that while interiorCursorNextPage() will refuse to follow
726 + * loops, it does not keep track of pages returned for purposes of
727 + * preventing duplication.
729 + * Note that interiorCursorEOF() could return false (not at EOF), and
730 + * interiorCursorNextPage() could still return SQLITE_DONE. This
731 + * could happen if there are more cells to iterate in an interior
732 + * page, but those cells refer to invalid pages.
734 +typedef struct RecoverInteriorCursor RecoverInteriorCursor;
735 +struct RecoverInteriorCursor {
736 + RecoverInteriorCursor *pParent; /* Parent node to this node. */
737 + DbPage *pPage; /* Reference to leaf page. */
738 + unsigned nPageSize; /* Size of page. */
739 + unsigned nChildren; /* Number of children on the page. */
740 + unsigned iChild; /* Index of next child to return. */
743 +static void interiorCursorDestroy(RecoverInteriorCursor *pCursor){
744 + /* Destroy all the cursors to the root. */
746 + RecoverInteriorCursor *p = pCursor;
747 + pCursor = pCursor->pParent;
750 + sqlite3PagerUnref(p->pPage);
754 + memset(p, 0xA5, sizeof(*p));
759 +/* Internal helper. Reset storage in preparation for iterating pPage. */
760 +static void interiorCursorSetPage(RecoverInteriorCursor *pCursor,
762 + const unsigned knMinCellLength = 2 + 4 + 1;
763 + unsigned nMaxChildren;
764 + assert( PageHeader(pPage)[kiPageTypeOffset]==kTableInteriorPage );
766 + if( pCursor->pPage ){
767 + sqlite3PagerUnref(pCursor->pPage);
768 + pCursor->pPage = NULL;
770 + pCursor->pPage = pPage;
771 + pCursor->iChild = 0;
773 + /* A child for each cell, plus one in the header. */
774 + pCursor->nChildren = decodeUnsigned16(PageHeader(pPage) +
775 + kiPageCellCountOffset) + 1;
777 + /* Each child requires a 16-bit offset from an array after the header,
778 + * and each child contains a 32-bit page number and at least a varint
779 + * (min size of one byte). The final child page is in the header. So
780 + * the maximum value for nChildren is:
781 + * (nPageSize - kiPageInteriorHeaderBytes) /
782 + * (sizeof(uint16) + sizeof(uint32) + 1) + 1
784 + /* TODO(shess): This count is very unlikely to be corrupted in
785 + * isolation, so seeing this could signal to skip the page. OTOH, I
786 + * can't offhand think of how to get here unless this or the page-type
787 + * byte is corrupted. Could be an overflow page, but it would require
788 + * a very large database.
791 + (pCursor->nPageSize - kiPageInteriorHeaderBytes) / knMinCellLength + 1;
792 + if (pCursor->nChildren > nMaxChildren) {
793 + pCursor->nChildren = nMaxChildren;
797 +static int interiorCursorCreate(RecoverInteriorCursor *pParent,
798 + DbPage *pPage, int nPageSize,
799 + RecoverInteriorCursor **ppCursor){
800 + RecoverInteriorCursor *pCursor =
801 + sqlite3_malloc(sizeof(RecoverInteriorCursor));
803 + return SQLITE_NOMEM;
806 + memset(pCursor, 0, sizeof(*pCursor));
807 + pCursor->pParent = pParent;
808 + pCursor->nPageSize = nPageSize;
809 + interiorCursorSetPage(pCursor, pPage);
810 + *ppCursor = pCursor;
814 +/* Internal helper. Return the child page number at iChild. */
815 +static unsigned interiorCursorChildPage(RecoverInteriorCursor *pCursor){
816 + const unsigned char *pPageHeader; /* Header of the current page. */
817 + const unsigned char *pCellOffsets; /* Offset to page's cell offsets. */
818 + unsigned iCellOffset; /* Offset of target cell. */
820 + assert( pCursor->iChild<pCursor->nChildren );
822 + /* Rightmost child is in the header. */
823 + pPageHeader = PageHeader(pCursor->pPage);
824 + if( pCursor->iChild==pCursor->nChildren-1 ){
825 + return decodeUnsigned32(pPageHeader + kiPageRightChildOffset);
828 + /* Each cell is a 4-byte integer page number and a varint rowid
829 + * which is greater than the rowid of items in that sub-tree (this
830 + * module ignores ordering). The offset is from the beginning of the
831 + * page, not from the page header.
833 + pCellOffsets = pPageHeader + kiPageInteriorHeaderBytes;
834 + iCellOffset = decodeUnsigned16(pCellOffsets + pCursor->iChild*2);
835 + if( iCellOffset<=pCursor->nPageSize-4 ){
836 + return decodeUnsigned32(PageData(pCursor->pPage, iCellOffset));
839 + /* TODO(shess): Check for cell overlaps? Cells require 4 bytes plus
840 + * a varint. Check could be identical to leaf check (or even a
841 + * shared helper testing for "Cells starting in this range"?).
844 + /* If the offset is broken, return an invalid page number. */
848 +static int interiorCursorEOF(RecoverInteriorCursor *pCursor){
849 + /* Find a parent with remaining children. EOF if none found. */
850 + while( pCursor && pCursor->iChild>=pCursor->nChildren ){
851 + pCursor = pCursor->pParent;
853 + return pCursor==NULL;
856 +/* Internal helper. Used to detect if iPage would cause a loop. */
857 +static int interiorCursorPageInUse(RecoverInteriorCursor *pCursor,
859 + /* Find any parent using the indicated page. */
860 + while( pCursor && pCursor->pPage->pgno!=iPage ){
861 + pCursor = pCursor->pParent;
863 + return pCursor!=NULL;
866 +/* Get the next page from the interior cursor at *ppCursor. Returns
867 + * SQLITE_ROW with the page in *ppPage, or SQLITE_DONE if out of
868 + * pages, or the error SQLite returned.
870 + * If the tree is uneven, then when the cursor attempts to get a new
871 + * interior page from the parent cursor, it may get a non-interior
872 + * page. In that case, the new page is returned, and *ppCursor is
873 + * updated to point to the parent cursor (this cursor is freed).
875 +/* TODO(shess): I've tried to avoid recursion in most of this code,
876 + * but this case is more challenging because the recursive call is in
877 + * the middle of operation. One option for converting it without
878 + * adding memory management would be to retain the head pointer and
879 + * use a helper to "back up" as needed. Another option would be to
880 + * reverse the list during traversal.
882 +static int interiorCursorNextPage(RecoverInteriorCursor **ppCursor,
884 + RecoverInteriorCursor *pCursor = *ppCursor;
887 + const unsigned char *pPageHeader; /* Header of found page. */
889 + /* Find a valid child page which isn't on the stack. */
890 + while( pCursor->iChild<pCursor->nChildren ){
891 + const unsigned iPage = interiorCursorChildPage(pCursor);
893 + if( interiorCursorPageInUse(pCursor, iPage) ){
894 + fprintf(stderr, "Loop detected at %d\n", iPage);
896 + int rc = sqlite3PagerAcquire(pCursor->pPage->pPager, iPage, ppPage, 0);
897 + if( rc==SQLITE_OK ){
903 + /* This page has no more children. Get next page from parent. */
904 + if( !pCursor->pParent ){
905 + return SQLITE_DONE;
907 + rc = interiorCursorNextPage(&pCursor->pParent, ppPage);
908 + if( rc!=SQLITE_ROW ){
912 + /* If a non-interior page is received, that either means that the
913 + * tree is uneven, or that a child was re-used (say as an overflow
914 + * page). Remove this cursor and let the caller handle the page.
916 + pPageHeader = PageHeader(*ppPage);
917 + if( pPageHeader[kiPageTypeOffset]!=kTableInteriorPage ){
918 + *ppCursor = pCursor->pParent;
919 + pCursor->pParent = NULL;
920 + interiorCursorDestroy(pCursor);
924 + /* Iterate the new page. */
925 + interiorCursorSetPage(pCursor, *ppPage);
929 + assert(NULL); /* NOTREACHED() */
930 + return SQLITE_CORRUPT;
933 +/* Large rows are spilled to overflow pages. The row's main page
934 + * stores the overflow page number after the local payload, with a
935 + * linked list forward from there as necessary. overflowMaybeCreate()
936 + * and overflowGetSegment() provide an abstraction for accessing such
937 + * data while centralizing the code.
939 + * overflowDestroy - releases all resources associated with the structure.
940 + * overflowMaybeCreate - create the overflow structure if it is needed
941 + * to represent the given record. See function comment.
942 + * overflowGetSegment - fetch a segment from the record, accounting
943 + * for overflow pages. Segments which are not
944 + * entirely contained with a page are constructed
945 + * into a buffer which is returned. See function comment.
947 +typedef struct RecoverOverflow RecoverOverflow;
948 +struct RecoverOverflow {
949 + RecoverOverflow *pNextOverflow;
951 + unsigned nPageSize;
954 +static void overflowDestroy(RecoverOverflow *pOverflow){
955 + while( pOverflow ){
956 + RecoverOverflow *p = pOverflow;
957 + pOverflow = p->pNextOverflow;
960 + sqlite3PagerUnref(p->pPage);
964 + memset(p, 0xA5, sizeof(*p));
969 +/* Internal helper. Used to detect if iPage would cause a loop. */
970 +static int overflowPageInUse(RecoverOverflow *pOverflow, unsigned iPage){
971 + while( pOverflow && pOverflow->pPage->pgno!=iPage ){
972 + pOverflow = pOverflow->pNextOverflow;
974 + return pOverflow!=NULL;
977 +/* Setup to access an nRecordBytes record beginning at iRecordOffset
978 + * in pPage. If nRecordBytes can be satisfied entirely from pPage,
979 + * then no overflow pages are needed an *pnLocalRecordBytes is set to
980 + * nRecordBytes. Otherwise, *ppOverflow is set to the head of a list
981 + * of overflow pages, and *pnLocalRecordBytes is set to the number of
982 + * bytes local to pPage.
984 + * overflowGetSegment() will do the right thing regardless of whether
985 + * those values are set to be in-page or not.
987 +static int overflowMaybeCreate(DbPage *pPage, unsigned nPageSize,
988 + unsigned iRecordOffset, unsigned nRecordBytes,
989 + unsigned *pnLocalRecordBytes,
990 + RecoverOverflow **ppOverflow){
991 + unsigned nLocalRecordBytes; /* Record bytes in the leaf page. */
992 + unsigned iNextPage; /* Next page number for record data. */
993 + unsigned nBytes; /* Maximum record bytes as of current page. */
995 + RecoverOverflow *pFirstOverflow; /* First in linked list of pages. */
996 + RecoverOverflow *pLastOverflow; /* End of linked list. */
998 + /* Calculations from the "Table B-Tree Leaf Cell" part of section
999 + * 1.5 of http://www.sqlite.org/fileformat2.html . maxLocal and
1000 + * minLocal to match naming in btree.c.
1002 + const unsigned maxLocal = nPageSize - 35;
1003 + const unsigned minLocal = ((nPageSize-12)*32/255)-23; /* m */
1005 + /* Always fit anything smaller than maxLocal. */
1006 + if( nRecordBytes<=maxLocal ){
1007 + *pnLocalRecordBytes = nRecordBytes;
1008 + *ppOverflow = NULL;
1012 + /* Calculate the remainder after accounting for minLocal on the leaf
1013 + * page and what packs evenly into overflow pages. If the remainder
1014 + * does not fit into maxLocal, then a partially-full overflow page
1015 + * will be required in any case, so store as little as possible locally.
1017 + nLocalRecordBytes = minLocal+((nRecordBytes-minLocal)%(nPageSize-4));
1018 + if( maxLocal<nLocalRecordBytes ){
1019 + nLocalRecordBytes = minLocal;
1022 + /* Don't read off the end of the page. */
1023 + if( iRecordOffset+nLocalRecordBytes+4>nPageSize ){
1024 + return SQLITE_CORRUPT;
1027 + /* First overflow page number is after the local bytes. */
1029 + decodeUnsigned32(PageData(pPage, iRecordOffset + nLocalRecordBytes));
1030 + nBytes = nLocalRecordBytes;
1032 + /* While there are more pages to read, and more bytes are needed,
1033 + * get another page.
1035 + pFirstOverflow = pLastOverflow = NULL;
1037 + while( iNextPage && nBytes<nRecordBytes ){
1038 + RecoverOverflow *pOverflow; /* New overflow page for the list. */
1040 + rc = sqlite3PagerAcquire(pPage->pPager, iNextPage, &pPage, 0);
1041 + if( rc!=SQLITE_OK ){
1045 + pOverflow = sqlite3_malloc(sizeof(RecoverOverflow));
1047 + sqlite3PagerUnref(pPage);
1048 + rc = SQLITE_NOMEM;
1051 + memset(pOverflow, 0, sizeof(*pOverflow));
1052 + pOverflow->pPage = pPage;
1053 + pOverflow->nPageSize = nPageSize;
1055 + if( !pFirstOverflow ){
1056 + pFirstOverflow = pOverflow;
1058 + pLastOverflow->pNextOverflow = pOverflow;
1060 + pLastOverflow = pOverflow;
1062 + iNextPage = decodeUnsigned32(pPage->pData);
1063 + nBytes += nPageSize-4;
1065 + /* Avoid loops. */
1066 + if( overflowPageInUse(pFirstOverflow, iNextPage) ){
1067 + fprintf(stderr, "Overflow loop detected at %d\n", iNextPage);
1068 + rc = SQLITE_CORRUPT;
1073 + /* If there were not enough pages, or too many, things are corrupt.
1074 + * Not having enough pages is an obvious problem, all the data
1075 + * cannot be read. Too many pages means that the contents of the
1076 + * row between the main page and the overflow page(s) is
1077 + * inconsistent (most likely one or more of the overflow pages does
1078 + * not really belong to this row).
1080 + if( rc==SQLITE_OK && (nBytes<nRecordBytes || iNextPage) ){
1081 + rc = SQLITE_CORRUPT;
1084 + if( rc==SQLITE_OK ){
1085 + *ppOverflow = pFirstOverflow;
1086 + *pnLocalRecordBytes = nLocalRecordBytes;
1087 + }else if( pFirstOverflow ){
1088 + overflowDestroy(pFirstOverflow);
1093 +/* Use in concert with overflowMaybeCreate() to efficiently read parts
1094 + * of a potentially-overflowing record. pPage and iRecordOffset are
1095 + * the values passed into overflowMaybeCreate(), nLocalRecordBytes and
1096 + * pOverflow are the values returned by that call.
1098 + * On SQLITE_OK, *ppBase points to nRequestBytes of data at
1099 + * iRequestOffset within the record. If the data exists contiguously
1100 + * in a page, a direct pointer is returned, otherwise a buffer from
1101 + * sqlite3_malloc() is returned with the data. *pbFree is set true if
1102 + * sqlite3_free() should be called on *ppBase.
1104 +/* Operation of this function is subtle. At any time, pPage is the
1105 + * current page, with iRecordOffset and nLocalRecordBytes being record
1106 + * data within pPage, and pOverflow being the overflow page after
1107 + * pPage. This allows the code to handle both the initial leaf page
1108 + * and overflow pages consistently by adjusting the values
1111 +static int overflowGetSegment(DbPage *pPage, unsigned iRecordOffset,
1112 + unsigned nLocalRecordBytes,
1113 + RecoverOverflow *pOverflow,
1114 + unsigned iRequestOffset, unsigned nRequestBytes,
1115 + unsigned char **ppBase, int *pbFree){
1116 + unsigned nBase; /* Amount of data currently collected. */
1117 + unsigned char *pBase; /* Buffer to collect record data into. */
1119 + /* Skip to the page containing the start of the data. */
1120 + while( iRequestOffset>=nLocalRecordBytes && pOverflow ){
1121 + /* Factor out current page's contribution. */
1122 + iRequestOffset -= nLocalRecordBytes;
1124 + /* Move forward to the next page in the list. */
1125 + pPage = pOverflow->pPage;
1126 + iRecordOffset = 4;
1127 + nLocalRecordBytes = pOverflow->nPageSize - iRecordOffset;
1128 + pOverflow = pOverflow->pNextOverflow;
1131 + /* If the requested data is entirely within this page, return a
1132 + * pointer into the page.
1134 + if( iRequestOffset+nRequestBytes<=nLocalRecordBytes ){
1135 + /* TODO(shess): "assignment discards qualifiers from pointer target type"
1136 + * Having ppBase be const makes sense, but sqlite3_free() takes non-const.
1138 + *ppBase = (unsigned char *)PageData(pPage, iRecordOffset + iRequestOffset);
1143 + /* The data range would require additional pages. */
1145 + /* Should never happen, the range is outside the nRecordBytes
1146 + * passed to overflowMaybeCreate().
1148 + assert(NULL); /* NOTREACHED */
1149 + return SQLITE_ERROR;
1152 + /* Get a buffer to construct into. */
1154 + pBase = sqlite3_malloc(nRequestBytes);
1156 + return SQLITE_NOMEM;
1158 + while( nBase<nRequestBytes ){
1159 + /* Copy over data present on this page. */
1160 + unsigned nCopyBytes = nRequestBytes - nBase;
1161 + if( nLocalRecordBytes-iRequestOffset<nCopyBytes ){
1162 + nCopyBytes = nLocalRecordBytes - iRequestOffset;
1164 + memcpy(pBase + nBase, PageData(pPage, iRecordOffset + iRequestOffset),
1166 + nBase += nCopyBytes;
1169 + /* Copy from start of record data in future pages. */
1170 + iRequestOffset = 0;
1172 + /* Move forward to the next page in the list. Should match
1173 + * first while() loop.
1175 + pPage = pOverflow->pPage;
1176 + iRecordOffset = 4;
1177 + nLocalRecordBytes = pOverflow->nPageSize - iRecordOffset;
1178 + pOverflow = pOverflow->pNextOverflow;
1179 + }else if( nBase<nRequestBytes ){
1180 + /* Ran out of overflow pages with data left to deliver. Not
1181 + * possible if the requested range fits within nRecordBytes
1182 + * passed to overflowMaybeCreate() when creating pOverflow.
1184 + assert(NULL); /* NOTREACHED */
1185 + sqlite3_free(pBase);
1186 + return SQLITE_ERROR;
1189 + assert( nBase==nRequestBytes );
1195 +/* Primary structure for iterating the contents of a table.
1197 + * leafCursorDestroy - release all resources associated with the cursor.
1198 + * leafCursorCreate - create a cursor to iterate items from tree at
1199 + * the provided root page.
1200 + * leafCursorNextValidCell - get the cursor ready to access data from
1201 + * the next valid cell in the table.
1202 + * leafCursorCellRowid - get the current cell's rowid.
1203 + * leafCursorCellColumns - get current cell's column count.
1204 + * leafCursorCellColInfo - get type and data for a column in current cell.
1206 + * leafCursorNextValidCell skips cells which fail simple integrity
1207 + * checks, such as overlapping other cells, or being located at
1208 + * impossible offsets, or where header data doesn't correctly describe
1209 + * payload data. Returns SQLITE_ROW if a valid cell is found,
1210 + * SQLITE_DONE if all pages in the tree were exhausted.
1212 + * leafCursorCellColInfo() accounts for overflow pages in the style of
1213 + * overflowGetSegment().
1215 +typedef struct RecoverLeafCursor RecoverLeafCursor;
1216 +struct RecoverLeafCursor {
1217 + RecoverInteriorCursor *pParent; /* Parent node to this node. */
1218 + DbPage *pPage; /* Reference to leaf page. */
1219 + unsigned nPageSize; /* Size of pPage. */
1220 + unsigned nCells; /* Number of cells in pPage. */
1221 + unsigned iCell; /* Current cell. */
1223 + /* Info parsed from data in iCell. */
1224 + i64 iRowid; /* rowid parsed. */
1225 + unsigned nRecordCols; /* how many items in the record. */
1226 + u64 iRecordOffset; /* offset to record data. */
1227 + /* TODO(shess): nRecordBytes and nRecordHeaderBytes are used in
1228 + * leafCursorCellColInfo() to prevent buffer overruns.
1229 + * leafCursorCellDecode() already verified that the cell is valid, so
1230 + * those checks should be redundant.
1232 + u64 nRecordBytes; /* Size of record data. */
1233 + unsigned nLocalRecordBytes; /* Amount of record data in-page. */
1234 + unsigned nRecordHeaderBytes; /* Size of record header data. */
1235 + unsigned char *pRecordHeader; /* Pointer to record header data. */
1236 + int bFreeRecordHeader; /* True if record header requires free. */
1237 + RecoverOverflow *pOverflow; /* Cell overflow info, if needed. */
1240 +/* Internal helper shared between next-page and create-cursor. If
1241 + * pPage is a leaf page, it will be stored in the cursor and state
1242 + * initialized for reading cells.
1244 + * If pPage is an interior page, a new parent cursor is created and
1245 + * injected on the stack. This is necessary to handle trees with
1246 + * uneven depth, but also is used during initial setup.
1248 + * If pPage is not a table page at all, it is discarded.
1250 + * If SQLITE_OK is returned, the caller no longer owns pPage,
1251 + * otherwise the caller is responsible for discarding it.
1253 +static int leafCursorLoadPage(RecoverLeafCursor *pCursor, DbPage *pPage){
1254 + const unsigned char *pPageHeader; /* Header of *pPage */
1256 + /* Release the current page. */
1257 + if( pCursor->pPage ){
1258 + sqlite3PagerUnref(pCursor->pPage);
1259 + pCursor->pPage = NULL;
1260 + pCursor->iCell = pCursor->nCells = 0;
1263 + /* If the page is an unexpected interior node, inject a new stack
1264 + * layer and try again from there.
1266 + pPageHeader = PageHeader(pPage);
1267 + if( pPageHeader[kiPageTypeOffset]==kTableInteriorPage ){
1268 + RecoverInteriorCursor *pParent;
1269 + int rc = interiorCursorCreate(pCursor->pParent, pPage, pCursor->nPageSize,
1271 + if( rc!=SQLITE_OK ){
1274 + pCursor->pParent = pParent;
1278 + /* Not a leaf page, skip it. */
1279 + if( pPageHeader[kiPageTypeOffset]!=kTableLeafPage ){
1280 + sqlite3PagerUnref(pPage);
1284 + /* Take ownership of the page and start decoding. */
1285 + pCursor->pPage = pPage;
1286 + pCursor->iCell = 0;
1287 + pCursor->nCells = decodeUnsigned16(pPageHeader + kiPageCellCountOffset);
1291 +/* Get the next leaf-level page in the tree. Returns SQLITE_ROW when
1292 + * a leaf page is found, SQLITE_DONE when no more leaves exist, or any
1293 + * error which occurred.
1295 +static int leafCursorNextPage(RecoverLeafCursor *pCursor){
1296 + if( !pCursor->pParent ){
1297 + return SQLITE_DONE;
1300 + /* Repeatedly load the parent's next child page until a leaf is found. */
1302 + DbPage *pNextPage;
1303 + int rc = interiorCursorNextPage(&pCursor->pParent, &pNextPage);
1304 + if( rc!=SQLITE_ROW ){
1305 + assert( rc==SQLITE_DONE );
1309 + rc = leafCursorLoadPage(pCursor, pNextPage);
1310 + if( rc!=SQLITE_OK ){
1311 + sqlite3PagerUnref(pNextPage);
1314 + } while( !pCursor->pPage );
1316 + return SQLITE_ROW;
1319 +static void leafCursorDestroyCellData(RecoverLeafCursor *pCursor){
1320 + if( pCursor->bFreeRecordHeader ){
1321 + sqlite3_free(pCursor->pRecordHeader);
1323 + pCursor->bFreeRecordHeader = 0;
1324 + pCursor->pRecordHeader = NULL;
1326 + if( pCursor->pOverflow ){
1327 + overflowDestroy(pCursor->pOverflow);
1328 + pCursor->pOverflow = NULL;
1332 +static void leafCursorDestroy(RecoverLeafCursor *pCursor){
1333 + leafCursorDestroyCellData(pCursor);
1335 + if( pCursor->pParent ){
1336 + interiorCursorDestroy(pCursor->pParent);
1337 + pCursor->pParent = NULL;
1340 + if( pCursor->pPage ){
1341 + sqlite3PagerUnref(pCursor->pPage);
1342 + pCursor->pPage = NULL;
1345 + memset(pCursor, 0xA5, sizeof(*pCursor));
1346 + sqlite3_free(pCursor);
1349 +/* Create a cursor to iterate the rows from the leaf pages of a table
1350 + * rooted at iRootPage.
1352 +/* TODO(shess): recoverOpen() calls this to setup the cursor, and I
1353 + * think that recoverFilter() may make a hard assumption that the
1354 + * cursor returned will turn up at least one valid cell.
1356 + * The cases I can think of which break this assumption are:
1357 + * - pPage is a valid leaf page with no valid cells.
1358 + * - pPage is a valid interior page with no valid leaves.
1359 + * - pPage is a valid interior page who's leaves contain no valid cells.
1360 + * - pPage is not a valid leaf or interior page.
1362 +static int leafCursorCreate(Pager *pPager, unsigned nPageSize,
1363 + u32 iRootPage, RecoverLeafCursor **ppCursor){
1364 + DbPage *pPage; /* Reference to page at iRootPage. */
1365 + RecoverLeafCursor *pCursor; /* Leaf cursor being constructed. */
1368 + /* Start out with the root page. */
1369 + rc = sqlite3PagerAcquire(pPager, iRootPage, &pPage, 0);
1370 + if( rc!=SQLITE_OK ){
1374 + pCursor = sqlite3_malloc(sizeof(RecoverLeafCursor));
1376 + sqlite3PagerUnref(pPage);
1377 + return SQLITE_NOMEM;
1379 + memset(pCursor, 0, sizeof(*pCursor));
1381 + pCursor->nPageSize = nPageSize;
1383 + rc = leafCursorLoadPage(pCursor, pPage);
1384 + if( rc!=SQLITE_OK ){
1385 + sqlite3PagerUnref(pPage);
1386 + leafCursorDestroy(pCursor);
1390 + /* pPage wasn't a leaf page, find the next leaf page. */
1391 + if( !pCursor->pPage ){
1392 + rc = leafCursorNextPage(pCursor);
1393 + if( rc!=SQLITE_DONE && rc!=SQLITE_ROW ){
1394 + leafCursorDestroy(pCursor);
1399 + *ppCursor = pCursor;
1403 +/* Useful for setting breakpoints. */
1404 +static int ValidateError(){
1405 + return SQLITE_ERROR;
1408 +/* Setup the cursor for reading the information from cell iCell. */
1409 +static int leafCursorCellDecode(RecoverLeafCursor *pCursor){
1410 + const unsigned char *pPageHeader; /* Header of current page. */
1411 + const unsigned char *pPageEnd; /* Byte after end of current page. */
1412 + const unsigned char *pCellOffsets; /* Pointer to page's cell offsets. */
1413 + unsigned iCellOffset; /* Offset of current cell (iCell). */
1414 + const unsigned char *pCell; /* Pointer to data at iCellOffset. */
1415 + unsigned nCellMaxBytes; /* Maximum local size of iCell. */
1416 + unsigned iEndOffset; /* End of iCell's in-page data. */
1417 + u64 nRecordBytes; /* Expected size of cell, w/overflow. */
1418 + u64 iRowid; /* iCell's rowid (in table). */
1419 + unsigned nRead; /* Amount of cell read. */
1420 + unsigned nRecordHeaderRead; /* Header data read. */
1421 + u64 nRecordHeaderBytes; /* Header size expected. */
1422 + unsigned nRecordCols; /* Columns read from header. */
1423 + u64 nRecordColBytes; /* Bytes in payload for those columns. */
1427 + assert( pCursor->iCell<pCursor->nCells );
1429 + leafCursorDestroyCellData(pCursor);
1431 + /* Find the offset to the row. */
1432 + pPageHeader = PageHeader(pCursor->pPage);
1433 + pCellOffsets = pPageHeader + knPageLeafHeaderBytes;
1434 + pPageEnd = PageData(pCursor->pPage, pCursor->nPageSize);
1435 + if( pCellOffsets + pCursor->iCell*2 + 2 > pPageEnd ){
1436 + return ValidateError();
1438 + iCellOffset = decodeUnsigned16(pCellOffsets + pCursor->iCell*2);
1439 + if( iCellOffset>=pCursor->nPageSize ){
1440 + return ValidateError();
1443 + pCell = PageData(pCursor->pPage, iCellOffset);
1444 + nCellMaxBytes = pCursor->nPageSize - iCellOffset;
1446 + /* B-tree leaf cells lead with varint record size, varint rowid and
1447 + * varint header size.
1449 + /* TODO(shess): The smallest page size is 512 bytes, which has an m
1450 + * of 39. Three varints need at most 27 bytes to encode. I think.
1452 + if( !checkVarints(pCell, nCellMaxBytes, 3) ){
1453 + return ValidateError();
1456 + nRead = getVarint(pCell, &nRecordBytes);
1457 + assert( iCellOffset+nRead<=pCursor->nPageSize );
1458 + pCursor->nRecordBytes = nRecordBytes;
1460 + nRead += getVarint(pCell + nRead, &iRowid);
1461 + assert( iCellOffset+nRead<=pCursor->nPageSize );
1462 + pCursor->iRowid = (i64)iRowid;
1464 + pCursor->iRecordOffset = iCellOffset + nRead;
1466 + /* Start overflow setup here because nLocalRecordBytes is needed to
1467 + * check cell overlap.
1469 + rc = overflowMaybeCreate(pCursor->pPage, pCursor->nPageSize,
1470 + pCursor->iRecordOffset, pCursor->nRecordBytes,
1471 + &pCursor->nLocalRecordBytes,
1472 + &pCursor->pOverflow);
1473 + if( rc!=SQLITE_OK ){
1474 + return ValidateError();
1477 + /* Check that no other cell starts within this cell. */
1478 + iEndOffset = pCursor->iRecordOffset + pCursor->nLocalRecordBytes;
1479 + for( i=0; i<pCursor->nCells && pCellOffsets + i*2 + 2 <= pPageEnd; ++i ){
1480 + const unsigned iOtherOffset = decodeUnsigned16(pCellOffsets + i*2);
1481 + if( iOtherOffset>iCellOffset && iOtherOffset<iEndOffset ){
1482 + return ValidateError();
1486 + nRecordHeaderRead = getVarint(pCell + nRead, &nRecordHeaderBytes);
1487 + assert( nRecordHeaderBytes<=nRecordBytes );
1488 + pCursor->nRecordHeaderBytes = nRecordHeaderBytes;
1490 + /* Large headers could overflow if pages are small. */
1491 + rc = overflowGetSegment(pCursor->pPage,
1492 + pCursor->iRecordOffset, pCursor->nLocalRecordBytes,
1493 + pCursor->pOverflow, 0, nRecordHeaderBytes,
1494 + &pCursor->pRecordHeader, &pCursor->bFreeRecordHeader);
1495 + if( rc!=SQLITE_OK ){
1496 + return ValidateError();
1499 + /* Tally up the column count and size of data. */
1501 + nRecordColBytes = 0;
1502 + while( nRecordHeaderRead<nRecordHeaderBytes ){
1503 + u64 iSerialType; /* Type descriptor for current column. */
1504 + if( !checkVarint(pCursor->pRecordHeader + nRecordHeaderRead,
1505 + nRecordHeaderBytes - nRecordHeaderRead) ){
1506 + return ValidateError();
1508 + nRecordHeaderRead += getVarint(pCursor->pRecordHeader + nRecordHeaderRead,
1510 + if( iSerialType==10 || iSerialType==11 ){
1511 + return ValidateError();
1513 + nRecordColBytes += SerialTypeLength(iSerialType);
1516 + pCursor->nRecordCols = nRecordCols;
1518 + /* Parsing the header used as many bytes as expected. */
1519 + if( nRecordHeaderRead!=nRecordHeaderBytes ){
1520 + return ValidateError();
1523 + /* Calculated record is size of expected record. */
1524 + if( nRecordHeaderBytes+nRecordColBytes!=nRecordBytes ){
1525 + return ValidateError();
1531 +static i64 leafCursorCellRowid(RecoverLeafCursor *pCursor){
1532 + return pCursor->iRowid;
1535 +static unsigned leafCursorCellColumns(RecoverLeafCursor *pCursor){
1536 + return pCursor->nRecordCols;
1539 +/* Get the column info for the cell. Pass NULL for ppBase to prevent
1540 + * retrieving the data segment. If *pbFree is true, *ppBase must be
1541 + * freed by the caller using sqlite3_free().
1543 +static int leafCursorCellColInfo(RecoverLeafCursor *pCursor,
1544 + unsigned iCol, u64 *piColType,
1545 + unsigned char **ppBase, int *pbFree){
1546 + const unsigned char *pRecordHeader; /* Current cell's header. */
1547 + u64 nRecordHeaderBytes; /* Bytes in pRecordHeader. */
1548 + unsigned nRead; /* Bytes read from header. */
1549 + u64 iColEndOffset; /* Offset to end of column in cell. */
1550 + unsigned nColsSkipped; /* Count columns as procesed. */
1551 + u64 iSerialType; /* Type descriptor for current column. */
1553 + /* Implicit NULL for columns past the end. This case happens when
1554 + * rows have not been updated since an ALTER TABLE added columns.
1555 + * It is more convenient to address here than in callers.
1557 + if( iCol>=pCursor->nRecordCols ){
1566 + /* Must be able to decode header size. */
1567 + pRecordHeader = pCursor->pRecordHeader;
1568 + if( !checkVarint(pRecordHeader, pCursor->nRecordHeaderBytes) ){
1569 + return SQLITE_CORRUPT;
1572 + /* Rather than caching the header size and how many bytes it took,
1573 + * decode it every time.
1575 + nRead = getVarint(pRecordHeader, &nRecordHeaderBytes);
1576 + assert( nRecordHeaderBytes==pCursor->nRecordHeaderBytes );
1578 + /* Scan forward to the indicated column. Scans to _after_ column
1579 + * for later range checking.
1581 + /* TODO(shess): This could get expensive for very wide tables. An
1582 + * array of iSerialType could be built in leafCursorCellDecode(), but
1583 + * the number of columns is dynamic per row, so it would add memory
1584 + * management complexity. Enough info to efficiently forward
1585 + * iterate could be kept, if all clients forward iterate
1586 + * (recoverColumn() may not).
1588 + iColEndOffset = 0;
1590 + while( nColsSkipped<=iCol && nRead<nRecordHeaderBytes ){
1591 + if( !checkVarint(pRecordHeader + nRead, nRecordHeaderBytes - nRead) ){
1592 + return SQLITE_CORRUPT;
1594 + nRead += getVarint(pRecordHeader + nRead, &iSerialType);
1595 + iColEndOffset += SerialTypeLength(iSerialType);
1599 + /* Column's data extends past record's end. */
1600 + if( nRecordHeaderBytes+iColEndOffset>pCursor->nRecordBytes ){
1601 + return SQLITE_CORRUPT;
1604 + *piColType = iSerialType;
1606 + const u32 nColBytes = SerialTypeLength(iSerialType);
1608 + /* Offset from start of record to beginning of column. */
1609 + const unsigned iColOffset = nRecordHeaderBytes+iColEndOffset-nColBytes;
1611 + return overflowGetSegment(pCursor->pPage, pCursor->iRecordOffset,
1612 + pCursor->nLocalRecordBytes, pCursor->pOverflow,
1613 + iColOffset, nColBytes, ppBase, pbFree);
1618 +static int leafCursorNextValidCell(RecoverLeafCursor *pCursor){
1622 + /* Move to the next cell. */
1625 + /* No more cells, get the next leaf. */
1626 + if( pCursor->iCell>=pCursor->nCells ){
1627 + rc = leafCursorNextPage(pCursor);
1628 + if( rc!=SQLITE_ROW ){
1631 + assert( pCursor->iCell==0 );
1634 + /* If the cell is valid, indicate that a row is available. */
1635 + rc = leafCursorCellDecode(pCursor);
1636 + if( rc==SQLITE_OK ){
1637 + return SQLITE_ROW;
1640 + /* Iterate until done or a valid row is found. */
1641 + /* TODO(shess): Remove debugging output. */
1642 + fprintf(stderr, "Skipping invalid cell\n");
1644 + return SQLITE_ERROR;
1647 +typedef struct Recover Recover;
1649 + sqlite3_vtab base;
1650 + sqlite3 *db; /* Host database connection */
1651 + char *zDb; /* Database containing target table */
1652 + char *zTable; /* Target table */
1653 + unsigned nCols; /* Number of columns in target table */
1654 + unsigned char *pTypes; /* Types of columns in target table */
1657 +/* Internal helper for deleting the module. */
1658 +static void recoverRelease(Recover *pRecover){
1659 + sqlite3_free(pRecover->zDb);
1660 + sqlite3_free(pRecover->zTable);
1661 + sqlite3_free(pRecover->pTypes);
1662 + memset(pRecover, 0xA5, sizeof(*pRecover));
1663 + sqlite3_free(pRecover);
1666 +/* Helper function for initializing the module. Forward-declared so
1667 + * recoverCreate() and recoverConnect() can see it.
1669 +static int recoverInit(
1670 + sqlite3 *, void *, int, const char *const*, sqlite3_vtab **, char **
1673 +static int recoverCreate(
1676 + int argc, const char *const*argv,
1677 + sqlite3_vtab **ppVtab,
1681 + return recoverInit(db, pAux, argc, argv, ppVtab, pzErr);
1684 +/* This should never be called. */
1685 +static int recoverConnect(
1688 + int argc, const char *const*argv,
1689 + sqlite3_vtab **ppVtab,
1693 + return recoverInit(db, pAux, argc, argv, ppVtab, pzErr);
1696 +/* No indices supported. */
1697 +static int recoverBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
1702 +/* Logically, this should never be called. */
1703 +static int recoverDisconnect(sqlite3_vtab *pVtab){
1705 + recoverRelease((Recover*)pVtab);
1709 +static int recoverDestroy(sqlite3_vtab *pVtab){
1711 + recoverRelease((Recover*)pVtab);
1715 +typedef struct RecoverCursor RecoverCursor;
1716 +struct RecoverCursor {
1717 + sqlite3_vtab_cursor base;
1718 + RecoverLeafCursor *pLeafCursor;
1723 +static int recoverOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
1724 + Recover *pRecover = (Recover*)pVTab;
1725 + u32 iRootPage; /* Root page of the backing table. */
1726 + int iEncoding; /* UTF encoding for backing database. */
1727 + unsigned nPageSize; /* Size of pages in backing database. */
1728 + Pager *pPager; /* Backing database pager. */
1729 + RecoverLeafCursor *pLeafCursor; /* Cursor to read table's leaf pages. */
1730 + RecoverCursor *pCursor; /* Cursor to read rows from leaves. */
1736 + rc = getRootPage(pRecover->db, pRecover->zDb, pRecover->zTable,
1738 + if( rc!=SQLITE_OK ){
1743 + rc = getEncoding(pRecover->db, pRecover->zDb, &iEncoding);
1744 + if( rc!=SQLITE_OK ){
1748 + rc = GetPager(pRecover->db, pRecover->zDb, &pPager, &nPageSize);
1749 + if( rc!=SQLITE_OK ){
1753 + rc = leafCursorCreate(pPager, nPageSize, iRootPage, &pLeafCursor);
1754 + if( rc!=SQLITE_OK ){
1758 + pCursor = sqlite3_malloc(sizeof(RecoverCursor));
1760 + leafCursorDestroy(pLeafCursor);
1761 + return SQLITE_NOMEM;
1763 + memset(pCursor, 0, sizeof(*pCursor));
1764 + pCursor->base.pVtab = pVTab;
1765 + pCursor->pLeafCursor = pLeafCursor;
1766 + pCursor->iEncoding = iEncoding;
1768 + /* If no leaf pages were found, empty result set. */
1769 + /* TODO(shess): leafCursorNextValidCell() would return SQLITE_ROW or
1770 + * SQLITE_DONE to indicate whether there is further data to consider.
1772 + pCursor->bEOF = (pLeafCursor->pPage==NULL);
1774 + *ppCursor = (sqlite3_vtab_cursor*)pCursor;
1778 +static int recoverClose(sqlite3_vtab_cursor *cur){
1779 + RecoverCursor *pCursor = (RecoverCursor*)cur;
1781 + if( pCursor->pLeafCursor ){
1782 + leafCursorDestroy(pCursor->pLeafCursor);
1783 + pCursor->pLeafCursor = NULL;
1785 + memset(pCursor, 0xA5, sizeof(*pCursor));
1786 + sqlite3_free(cur);
1790 +/* Helpful place to set a breakpoint. */
1791 +static int RecoverInvalidCell(){
1792 + return SQLITE_ERROR;
1795 +/* Returns SQLITE_OK if the cell has an appropriate number of columns
1796 + * with the appropriate types of data.
1798 +static int recoverValidateLeafCell(Recover *pRecover, RecoverCursor *pCursor){
1801 + /* If the row's storage has too many columns, skip it. */
1802 + if( leafCursorCellColumns(pCursor->pLeafCursor)>pRecover->nCols ){
1803 + return RecoverInvalidCell();
1806 + /* Skip rows with unexpected types. */
1807 + for( i=0; i<pRecover->nCols; ++i ){
1808 + u64 iType; /* Storage type of column i. */
1811 + /* ROWID alias. */
1812 + if( (pRecover->pTypes[i]&MASK_ROWID) ){
1816 + rc = leafCursorCellColInfo(pCursor->pLeafCursor, i, &iType, NULL, NULL);
1817 + assert( rc==SQLITE_OK );
1818 + if( rc!=SQLITE_OK || !SerialTypeIsCompatible(iType, pRecover->pTypes[i]) ){
1819 + return RecoverInvalidCell();
1826 +static int recoverNext(sqlite3_vtab_cursor *pVtabCursor){
1827 + RecoverCursor *pCursor = (RecoverCursor*)pVtabCursor;
1828 + Recover *pRecover = (Recover*)pCursor->base.pVtab;
1833 + /* Scan forward to the next cell with valid storage, then check that
1834 + * the stored data matches the schema.
1836 + while( (rc = leafCursorNextValidCell(pCursor->pLeafCursor))==SQLITE_ROW ){
1837 + if( recoverValidateLeafCell(pRecover, pCursor)==SQLITE_OK ){
1842 + if( rc==SQLITE_DONE ){
1843 + pCursor->bEOF = 1;
1847 + assert( rc!=SQLITE_OK );
1851 +static int recoverFilter(
1852 + sqlite3_vtab_cursor *pVtabCursor,
1853 + int idxNum, const char *idxStr,
1854 + int argc, sqlite3_value **argv
1856 + RecoverCursor *pCursor = (RecoverCursor*)pVtabCursor;
1857 + Recover *pRecover = (Recover*)pCursor->base.pVtab;
1862 + /* There were no valid leaf pages in the table. */
1863 + if( pCursor->bEOF ){
1867 + /* Load the first cell, and iterate forward if it's not valid. If no cells at
1868 + * all are valid, recoverNext() sets bEOF and returns appropriately.
1870 + rc = leafCursorCellDecode(pCursor->pLeafCursor);
1871 + if( rc!=SQLITE_OK || recoverValidateLeafCell(pRecover, pCursor)!=SQLITE_OK ){
1872 + return recoverNext(pVtabCursor);
1878 +static int recoverEof(sqlite3_vtab_cursor *pVtabCursor){
1879 + RecoverCursor *pCursor = (RecoverCursor*)pVtabCursor;
1881 + return pCursor->bEOF;
1884 +static int recoverColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
1885 + RecoverCursor *pCursor = (RecoverCursor*)cur;
1886 + Recover *pRecover = (Recover*)pCursor->base.pVtab;
1887 + u64 iColType; /* Storage type of column i. */
1888 + unsigned char *pColData; /* Column i's data. */
1889 + int shouldFree; /* Non-zero if pColData should be freed. */
1894 + if( i>=pRecover->nCols ){
1895 + return SQLITE_ERROR;
1898 + /* ROWID alias. */
1899 + if( (pRecover->pTypes[i]&MASK_ROWID) ){
1900 + sqlite3_result_int64(ctx, leafCursorCellRowid(pCursor->pLeafCursor));
1906 + rc = leafCursorCellColInfo(pCursor->pLeafCursor, i, &iColType,
1907 + &pColData, &shouldFree);
1908 + if( rc!=SQLITE_OK ){
1911 + /* recoverValidateLeafCell() should guarantee that this will never
1914 + if( !SerialTypeIsCompatible(iColType, pRecover->pTypes[i]) ){
1916 + sqlite3_free(pColData);
1918 + return SQLITE_ERROR;
1921 + switch( iColType ){
1922 + case 0 : sqlite3_result_null(ctx); break;
1923 + case 1 : sqlite3_result_int64(ctx, decodeSigned(pColData, 1)); break;
1924 + case 2 : sqlite3_result_int64(ctx, decodeSigned(pColData, 2)); break;
1925 + case 3 : sqlite3_result_int64(ctx, decodeSigned(pColData, 3)); break;
1926 + case 4 : sqlite3_result_int64(ctx, decodeSigned(pColData, 4)); break;
1927 + case 5 : sqlite3_result_int64(ctx, decodeSigned(pColData, 6)); break;
1928 + case 6 : sqlite3_result_int64(ctx, decodeSigned(pColData, 8)); break;
1929 + case 7 : sqlite3_result_double(ctx, decodeFloat64(pColData)); break;
1930 + case 8 : sqlite3_result_int(ctx, 0); break;
1931 + case 9 : sqlite3_result_int(ctx, 1); break;
1932 + case 10 : assert( iColType!=10 ); break;
1933 + case 11 : assert( iColType!=11 ); break;
1936 + u32 l = SerialTypeLength(iColType);
1938 + /* If pColData was already allocated, arrange to pass ownership. */
1939 + sqlite3_destructor_type pFn = SQLITE_TRANSIENT;
1941 + pFn = sqlite3_free;
1945 + if( SerialTypeIsBlob(iColType) ){
1946 + sqlite3_result_blob(ctx, pColData, l, pFn);
1948 + if( pCursor->iEncoding==SQLITE_UTF16LE ){
1949 + sqlite3_result_text16le(ctx, (const void*)pColData, l, pFn);
1950 + }else if( pCursor->iEncoding==SQLITE_UTF16BE ){
1951 + sqlite3_result_text16be(ctx, (const void*)pColData, l, pFn);
1953 + sqlite3_result_text(ctx, (const char*)pColData, l, pFn);
1959 + sqlite3_free(pColData);
1964 +static int recoverRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *pRowid){
1965 + RecoverCursor *pCursor = (RecoverCursor*)pVtabCursor;
1967 + *pRowid = leafCursorCellRowid(pCursor->pLeafCursor);
1971 +static sqlite3_module recoverModule = {
1973 + recoverCreate, /* xCreate - create a table */
1974 + recoverConnect, /* xConnect - connect to an existing table */
1975 + recoverBestIndex, /* xBestIndex - Determine search strategy */
1976 + recoverDisconnect, /* xDisconnect - Disconnect from a table */
1977 + recoverDestroy, /* xDestroy - Drop a table */
1978 + recoverOpen, /* xOpen - open a cursor */
1979 + recoverClose, /* xClose - close a cursor */
1980 + recoverFilter, /* xFilter - configure scan constraints */
1981 + recoverNext, /* xNext - advance a cursor */
1982 + recoverEof, /* xEof */
1983 + recoverColumn, /* xColumn - read data */
1984 + recoverRowid, /* xRowid - read data */
1985 + 0, /* xUpdate - write data */
1986 + 0, /* xBegin - begin transaction */
1987 + 0, /* xSync - sync transaction */
1988 + 0, /* xCommit - commit transaction */
1989 + 0, /* xRollback - rollback transaction */
1990 + 0, /* xFindFunction - function overloading */
1991 + 0, /* xRename - rename the table */
1994 +int recoverVtableInit(sqlite3 *db){
1995 + return sqlite3_create_module_v2(db, "recover", &recoverModule, NULL, 0);
1998 +/* This section of code is for parsing the create input and
1999 + * initializing the module.
2002 +/* Find the next word in zText and place the endpoints in pzWord*.
2003 + * Returns true if the word is non-empty. "Word" is defined as
2004 + * ASCII alphanumeric plus '_' at this time.
2006 +static int findWord(const char *zText,
2007 + const char **pzWordStart, const char **pzWordEnd){
2009 + while( ascii_isspace(*zText) ){
2012 + *pzWordStart = zText;
2013 + while( ascii_isalnum(*zText) || *zText=='_' ){
2016 + r = zText>*pzWordStart; /* In case pzWordStart==pzWordEnd */
2017 + *pzWordEnd = zText;
2021 +/* Return true if the next word in zText is zWord, also setting
2022 + * *pzContinue to the character after the word.
2024 +static int expectWord(const char *zText, const char *zWord,
2025 + const char **pzContinue){
2026 + const char *zWordStart, *zWordEnd;
2027 + if( findWord(zText, &zWordStart, &zWordEnd) &&
2028 + ascii_strncasecmp(zWord, zWordStart, zWordEnd - zWordStart)==0 ){
2029 + *pzContinue = zWordEnd;
2035 +/* Parse the name and type information out of parameter. In case of
2036 + * success, *pzNameStart/End contain the name of the column,
2037 + * *pzTypeStart/End contain the top-level type, and *pTypeMask has the
2038 + * type mask to use for the column.
2040 +static int findNameAndType(const char *parameter,
2041 + const char **pzNameStart, const char **pzNameEnd,
2042 + const char **pzTypeStart, const char **pzTypeEnd,
2043 + unsigned char *pTypeMask){
2044 + unsigned nNameLen; /* Length of found name. */
2045 + const char *zEnd; /* Current end of parsed column information. */
2046 + int bNotNull; /* Non-zero if NULL is not allowed for name. */
2047 + int bStrict; /* Non-zero if column requires exact type match. */
2048 + const char *zDummy; /* Dummy parameter, result unused. */
2051 + /* strictMask is used for STRICT, strictMask|otherMask if STRICT is
2052 + * not supplied. zReplace provides an alternate type to expose to
2056 + const char *zName;
2057 + unsigned char strictMask;
2058 + unsigned char otherMask;
2059 + const char *zReplace;
2062 + MASK_INTEGER | MASK_FLOAT | MASK_BLOB | MASK_TEXT | MASK_NULL,
2065 + { "ROWID", MASK_INTEGER | MASK_ROWID, 0, "INTEGER", },
2066 + { "INTEGER", MASK_INTEGER | MASK_NULL, 0, NULL, },
2067 + { "FLOAT", MASK_FLOAT | MASK_NULL, MASK_INTEGER, NULL, },
2068 + { "NUMERIC", MASK_INTEGER | MASK_FLOAT | MASK_NULL, MASK_TEXT, NULL, },
2069 + { "TEXT", MASK_TEXT | MASK_NULL, MASK_BLOB, NULL, },
2070 + { "BLOB", MASK_BLOB | MASK_NULL, 0, NULL, },
2073 + if( !findWord(parameter, pzNameStart, pzNameEnd) ){
2074 + return SQLITE_MISUSE;
2077 + /* Manifest typing, accept any storage type. */
2078 + if( !findWord(*pzNameEnd, pzTypeStart, pzTypeEnd) ){
2079 + *pzTypeEnd = *pzTypeStart = "";
2080 + *pTypeMask = MASK_INTEGER | MASK_FLOAT | MASK_BLOB | MASK_TEXT | MASK_NULL;
2084 + nNameLen = *pzTypeEnd - *pzTypeStart;
2085 + for( i=0; i<ArraySize(kTypeInfo); ++i ){
2086 + if( ascii_strncasecmp(kTypeInfo[i].zName, *pzTypeStart, nNameLen)==0 ){
2090 + if( i==ArraySize(kTypeInfo) ){
2091 + return SQLITE_MISUSE;
2094 + zEnd = *pzTypeEnd;
2096 + if( expectWord(zEnd, "STRICT", &zEnd) ){
2097 + /* TODO(shess): Ick. But I don't want another single-purpose
2100 + if( kTypeInfo[i].zReplace && !kTypeInfo[i].zReplace[0] ){
2101 + return SQLITE_MISUSE;
2107 + if( expectWord(zEnd, "NOT", &zEnd) ){
2108 + if( expectWord(zEnd, "NULL", &zEnd) ){
2111 + /* Anything other than NULL after NOT is an error. */
2112 + return SQLITE_MISUSE;
2116 + /* Anything else is an error. */
2117 + if( findWord(zEnd, &zDummy, &zDummy) ){
2118 + return SQLITE_MISUSE;
2121 + *pTypeMask = kTypeInfo[i].strictMask;
2123 + *pTypeMask |= kTypeInfo[i].otherMask;
2126 + *pTypeMask &= ~MASK_NULL;
2128 + if( kTypeInfo[i].zReplace ){
2129 + *pzTypeStart = kTypeInfo[i].zReplace;
2130 + *pzTypeEnd = *pzTypeStart + strlen(*pzTypeStart);
2135 +/* Parse the arguments, placing type masks in *pTypes and the exposed
2136 + * schema in *pzCreateSql (for sqlite3_declare_vtab).
2138 +static int ParseColumnsAndGenerateCreate(unsigned nCols,
2139 + const char *const *pCols,
2140 + char **pzCreateSql,
2141 + unsigned char *pTypes,
2144 + char *zCreateSql = sqlite3_mprintf("CREATE TABLE x(");
2145 + if( !zCreateSql ){
2146 + return SQLITE_NOMEM;
2149 + for( i=0; i<nCols; i++ ){
2150 + const char *zSep = (i < nCols - 1 ? ", " : ")");
2151 + const char *zNotNull = "";
2152 + const char *zNameStart, *zNameEnd;
2153 + const char *zTypeStart, *zTypeEnd;
2154 + int rc = findNameAndType(pCols[i],
2155 + &zNameStart, &zNameEnd,
2156 + &zTypeStart, &zTypeEnd,
2158 + if( rc!=SQLITE_OK ){
2159 + *pzErr = sqlite3_mprintf("unable to parse column %d", i);
2160 + sqlite3_free(zCreateSql);
2164 + if( !(pTypes[i]&MASK_NULL) ){
2165 + zNotNull = " NOT NULL";
2168 + /* Add name and type to the create statement. */
2169 + zCreateSql = sqlite3_mprintf("%z%.*s %.*s%s%s",
2171 + zNameEnd - zNameStart, zNameStart,
2172 + zTypeEnd - zTypeStart, zTypeStart,
2174 + if( !zCreateSql ){
2175 + return SQLITE_NOMEM;
2179 + *pzCreateSql = zCreateSql;
2183 +/* Helper function for initializing the module. */
2184 +/* argv[0] module name
2185 + * argv[1] db name for virtual table
2186 + * argv[2] virtual table name
2187 + * argv[3] backing table name
2190 +/* TODO(shess): Since connect isn't supported, could inline into
2191 + * recoverCreate().
2193 +/* TODO(shess): Explore cases where it would make sense to set *pzErr. */
2194 +static int recoverInit(
2195 + sqlite3 *db, /* Database connection */
2196 + void *pAux, /* unused */
2197 + int argc, const char *const*argv, /* Parameters to CREATE TABLE statement */
2198 + sqlite3_vtab **ppVtab, /* OUT: New virtual table */
2199 + char **pzErr /* OUT: Error message, if any */
2201 + const unsigned kTypeCol = 4; /* First argument with column type info. */
2202 + Recover *pRecover; /* Virtual table structure being created. */
2203 + char *zDot; /* Any dot found in "db.table" backing. */
2204 + u32 iRootPage; /* Root page of backing table. */
2205 + char *zCreateSql; /* Schema of created virtual table. */
2208 + /* Require to be in the temp database. */
2209 + if( ascii_strcasecmp(argv[1], "temp")!=0 ){
2210 + *pzErr = sqlite3_mprintf("recover table must be in temp database");
2211 + return SQLITE_MISUSE;
2214 + /* Need the backing table and at least one column. */
2215 + if( argc<=kTypeCol ){
2216 + *pzErr = sqlite3_mprintf("no columns specified");
2217 + return SQLITE_MISUSE;
2220 + pRecover = sqlite3_malloc(sizeof(Recover));
2222 + return SQLITE_NOMEM;
2224 + memset(pRecover, 0, sizeof(*pRecover));
2225 + pRecover->base.pModule = &recoverModule;
2226 + pRecover->db = db;
2228 + /* Parse out db.table, assuming main if no dot. */
2229 + zDot = strchr(argv[3], '.');
2231 + pRecover->zDb = sqlite3_strdup(db->aDb[0].zName);
2232 + pRecover->zTable = sqlite3_strdup(argv[3]);
2233 + }else if( zDot>argv[3] && zDot[1]!='\0' ){
2234 + pRecover->zDb = sqlite3_strndup(argv[3], zDot - argv[3]);
2235 + pRecover->zTable = sqlite3_strdup(zDot + 1);
2237 + /* ".table" or "db." not allowed. */
2238 + *pzErr = sqlite3_mprintf("ill-formed table specifier");
2239 + recoverRelease(pRecover);
2240 + return SQLITE_ERROR;
2243 + pRecover->nCols = argc - kTypeCol;
2244 + pRecover->pTypes = sqlite3_malloc(pRecover->nCols);
2245 + if( !pRecover->zDb || !pRecover->zTable || !pRecover->pTypes ){
2246 + recoverRelease(pRecover);
2247 + return SQLITE_NOMEM;
2250 + /* Require the backing table to exist. */
2251 + /* TODO(shess): Be more pedantic about the form of the descriptor
2252 + * string. This already fails for poorly-formed strings, simply
2253 + * because there won't be a root page, but it would make more sense
2256 + rc = getRootPage(pRecover->db, pRecover->zDb, pRecover->zTable, &iRootPage);
2257 + if( rc!=SQLITE_OK ){
2258 + *pzErr = sqlite3_mprintf("unable to find backing table");
2259 + recoverRelease(pRecover);
2263 + /* Parse the column definitions. */
2264 + rc = ParseColumnsAndGenerateCreate(pRecover->nCols, argv + kTypeCol,
2265 + &zCreateSql, pRecover->pTypes, pzErr);
2266 + if( rc!=SQLITE_OK ){
2267 + recoverRelease(pRecover);
2271 + rc = sqlite3_declare_vtab(db, zCreateSql);
2272 + sqlite3_free(zCreateSql);
2273 + if( rc!=SQLITE_OK ){
2274 + recoverRelease(pRecover);
2278 + *ppVtab = (sqlite3_vtab *)pRecover;
2281 diff --git a/third_party/sqlite/src/src/sqlite.h.in b/third_party/sqlite/src/src/sqlite.h.in
2282 index f1d4e40..28b5ef1 100644
2283 --- a/third_party/sqlite/src/src/sqlite.h.in
2284 +++ b/third_party/sqlite/src/src/sqlite.h.in
2285 @@ -7408,6 +7408,17 @@ int sqlite3_vtab_on_conflict(sqlite3 *);
2289 +/* Begin recover.patch for Chromium */
2291 +** Call to initialize the recover virtual-table modules (see recover.c).
2293 +** This could be loaded by default in main.c, but that would make the
2294 +** virtual table available to Web SQL. Breaking it out allows only
2295 +** selected users to enable it (currently sql/recovery.cc).
2297 +int recoverVtableInit(sqlite3 *db);
2298 +/* End recover.patch for Chromium */
2301 ** Undo the hack that converts floating point types to integer for
2302 ** builds on processors without floating point support.
2303 diff --git a/third_party/sqlite/src/test/recover.test b/third_party/sqlite/src/test/recover.test
2304 new file mode 100644
2305 index 0000000..b5aa182
2307 +++ b/third_party/sqlite/src/test/recover.test
2309 +# 2012 January 11 {}
2311 +# The author disclaims copyright to this source code. In place of
2312 +# a legal notice, here is a blessing:
2314 +# May you do good and not evil.
2315 +# May you find forgiveness for yourself and forgive others.
2316 +# May you share freely, never taking more than you give.
2318 +#***********************************************************************
2319 +# This file implements regression tests for SQLite library.
2321 +# This file implements tests for the recover module, which can read
2322 +# through corrupt rows and pages.
2326 +# TODO(shess): These all test that the module correctly reads good
2327 +# data. It would be good to implement tests of corrupt data.
2329 +set testdir [file dirname $argv0]
2330 +source $testdir/tester.tcl
2333 + DROP TABLE IF EXISTS altered;
2334 + CREATE TABLE altered (
2337 + INSERT INTO altered VALUES ('a');
2338 + INSERT INTO altered VALUES ('b');
2339 + INSERT INTO altered VALUES ('c');
2340 + ALTER TABLE altered ADD COLUMN i INTEGER NOT NULL DEFAULT 10;
2341 + INSERT INTO altered VALUES ('d', 5);
2344 +# SQLite will fill the earlier rows with the default.
2345 +do_test recover-alter-1.0 {
2346 + execsql {SELECT c, i FROM altered ORDER BY rowid}
2347 +} {a 10 b 10 c 10 d 5}
2349 +# recover sees NULL for those rows.
2350 +do_test recover-alter-1.1 {
2352 + DROP TABLE IF EXISTS temp.altered_recover;
2353 + CREATE VIRTUAL TABLE temp.altered_recover USING recover(
2359 + execsql {SELECT c, i FROM altered_recover ORDER BY rowid}
2360 +} {a {} b {} c {} d 5}
2362 +# Can skip those NULL columns like if they contained a real NULL.
2363 +do_test recover-alter-1.2 {
2365 + DROP TABLE IF EXISTS temp.altered_recover;
2366 + CREATE VIRTUAL TABLE temp.altered_recover USING recover(
2369 + i INTEGER NOT NULL
2372 + execsql {SELECT c, i FROM altered_recover ORDER BY rowid}
2376 +# It would be neat if this could work. I tried putting "DEFAULT ..."
2377 +# in the schema exposed by the recover table, but it doesn't do the
2379 +do_test recover-alter-1.2 {
2381 + DROP TABLE IF EXISTS temp.altered_recover;
2382 + CREATE VIRTUAL TABLE temp.altered_recover USING recover(
2385 + i INTEGER NOT NULL DEFAULT 10
2388 + execsql {SELECT c, i FROM altered_recover ORDER BY rowid}
2389 +} {a 10 b 10 c 10 d 5}
2392 +# Helper function to generate an arbitrarily-sized table.
2393 +proc generate {table base count} {
2394 + db eval "DROP TABLE IF EXISTS $table"
2395 + db transaction immediate {
2396 + db eval "CREATE TABLE $table (t TEXT,n INT)"
2397 + for {set i 0} {$i<$count} {incr i} {
2398 + set t [concat $base $i]
2399 + db eval [concat {INSERT INTO} $table {VALUES ($t, $i)}]
2404 +# Leaf-only database parses.
2405 +do_test recover-leaf-1.0 {
2407 + sqlite3 db test.db
2408 + generate "leaf" "Leaf-node-generating line " 10
2411 + DROP TABLE IF EXISTS temp.leaf_recover;
2412 + CREATE VIRTUAL TABLE temp.leaf_recover USING recover(
2418 + execsql {SELECT t, n FROM leaf_recover ORDER BY rowid}
2419 +} {{Leaf-node-generating line 0} 0 {Leaf-node-generating line 1} 1 {Leaf-node-generating line 2} 2 {Leaf-node-generating line 3} 3 {Leaf-node-generating line 4} 4 {Leaf-node-generating line 5} 5 {Leaf-node-generating line 6} 6 {Leaf-node-generating line 7} 7 {Leaf-node-generating line 8} 8 {Leaf-node-generating line 9} 9}
2421 +# Single level of interior node.
2422 +do_test recover-interior-1.0 {
2424 + sqlite3 db test.db
2425 + generate "interior" "Interior-node-generating line " 100
2428 + DROP TABLE IF EXISTS temp.interior_recover;
2429 + CREATE VIRTUAL TABLE temp.interior_recover USING recover(
2435 + execsql {SELECT t, n FROM interior_recover WHERE (rowid%10)=0 ORDER BY rowid}
2436 +} {{Interior-node-generating line 9} 9 {Interior-node-generating line 19} 19 {Interior-node-generating line 29} 29 {Interior-node-generating line 39} 39 {Interior-node-generating line 49} 49 {Interior-node-generating line 59} 59 {Interior-node-generating line 69} 69 {Interior-node-generating line 79} 79 {Interior-node-generating line 89} 89 {Interior-node-generating line 99} 99}
2438 +# Multiple levels of interior node.
2439 +do_test recover-interior-2.0 {
2441 + sqlite3 db test.db
2442 + generate "interior2" "Interior-node-generating line " 5000
2445 + DROP TABLE IF EXISTS temp.interior2_recover;
2446 + CREATE VIRTUAL TABLE temp.interior2_recover USING recover(
2452 + execsql {SELECT t, n FROM interior2_recover WHERE (rowid%500)=0 ORDER BY rowid}
2453 +} {{Interior-node-generating line 499} 499 {Interior-node-generating line 999} 999 {Interior-node-generating line 1499} 1499 {Interior-node-generating line 1999} 1999 {Interior-node-generating line 2499} 2499 {Interior-node-generating line 2999} 2999 {Interior-node-generating line 3499} 3499 {Interior-node-generating line 3999} 3999 {Interior-node-generating line 4499} 4499 {Interior-node-generating line 4999} 4999}
2456 diff --git a/third_party/sqlite/src/test/recover0.test b/third_party/sqlite/src/test/recover0.test
2457 new file mode 100644
2458 index 0000000..aac2ed9
2460 +++ b/third_party/sqlite/src/test/recover0.test
2462 +# 2012 January 4 {}
2464 +# The author disclaims copyright to this source code. In place of
2465 +# a legal notice, here is a blessing:
2467 +# May you do good and not evil.
2468 +# May you find forgiveness for yourself and forgive others.
2469 +# May you share freely, never taking more than you give.
2471 +#***********************************************************************
2472 +# This file implements regression tests for SQLite library.
2474 +# Test recover module syntax.
2478 +# TODO(shess): Test with attached databases.
2480 +# TODO(shess): Handle column mismatches? As things stand, the code
2481 +# only needs to pull the root page, so that may not be completely
2484 +set testdir [file dirname $argv0]
2485 +source $testdir/tester.tcl
2488 + DROP TABLE IF EXISTS backing;
2489 + CREATE TABLE backing (t TEXT);
2491 + DROP TABLE IF EXISTS backing2;
2492 + CREATE TABLE backing2 (id INTEGER PRIMARY KEY, t TEXT);
2495 +# Baseline create works.
2496 +do_test recover-syntax-0.0 {
2497 + db eval {DROP TABLE IF EXISTS temp.syntax}
2499 + CREATE VIRTUAL TABLE temp.syntax USING recover(
2506 +# Can specify database.
2507 +do_test recover-syntax-0.1 {
2508 + db eval {DROP TABLE IF EXISTS temp.syntax}
2510 + CREATE VIRTUAL TABLE temp.syntax USING recover(
2517 +# Can specify sqlite_master.
2518 +do_test recover-syntax-0.2 {
2519 + db eval {DROP TABLE IF EXISTS temp.syntax}
2521 + CREATE VIRTUAL TABLE temp.syntax USING recover(
2532 +# Fails if virtual table is not in the temp database.
2533 +do_test recover-syntax-1.0 {
2534 + db eval {DROP TABLE IF EXISTS temp.syntax;}
2536 + CREATE VIRTUAL TABLE syntax USING recover(
2541 +} {1 {recover table must be in temp database}}
2543 +# Fails if mentions missing table.
2544 +do_test recover-syntax-2.0 {
2545 + db eval {DROP TABLE IF EXISTS temp.syntax;}
2547 + CREATE VIRTUAL TABLE temp.syntax USING recover(
2552 +} {1 {unable to find backing table}}
2554 +# Fails if mentions missing database.
2555 +do_test recover-syntax-2.1 {
2556 + db eval {DROP TABLE IF EXISTS temp.syntax;}
2558 + CREATE VIRTUAL TABLE temp.syntax USING recover(
2563 +} {1 {unable to find backing table}}
2565 +# Fails if mentions garbage backing.
2566 +do_test recover-syntax-2.2 {
2567 + db eval {DROP TABLE IF EXISTS temp.syntax;}
2569 + CREATE VIRTUAL TABLE temp.syntax USING recover(
2570 + main.backing excess,
2574 +} {1 {unable to find backing table}}
2576 +# Database only fails.
2577 +do_test recover-syntax-2.3 {
2578 + db eval {DROP TABLE IF EXISTS temp.syntax;}
2580 + CREATE VIRTUAL TABLE temp.syntax USING recover(
2585 +} {1 {ill-formed table specifier}}
2587 +# Table only fails.
2588 +do_test recover-syntax-2.4 {
2589 + db eval {DROP TABLE IF EXISTS temp.syntax;}
2591 + CREATE VIRTUAL TABLE temp.syntax USING recover(
2596 +} {1 {ill-formed table specifier}}
2599 +do_test recover-syntax-3.0 {
2600 + db eval {DROP TABLE IF EXISTS temp.syntax}
2602 + CREATE VIRTUAL TABLE temp.syntax USING recover(
2606 + PRAGMA table_info(syntax);
2610 +# ANY as an alternative for manifest typing.
2611 +do_test recover-syntax-3.1 {
2612 + db eval {DROP TABLE IF EXISTS temp.syntax}
2614 + CREATE VIRTUAL TABLE temp.syntax USING recover(
2618 + PRAGMA table_info(syntax);
2623 +do_test recover-syntax-3.2 {
2624 + db eval {DROP TABLE IF EXISTS temp.syntax}
2626 + CREATE VIRTUAL TABLE temp.syntax USING recover(
2630 + PRAGMA table_info(syntax);
2634 +# ANY STRICT is not sensible.
2635 +do_test recover-syntax-3.3 {
2636 + db eval {DROP TABLE IF EXISTS temp.syntax}
2638 + CREATE VIRTUAL TABLE temp.syntax USING recover(
2642 + PRAGMA table_info(syntax);
2644 +} {1 {unable to parse column 0}}
2646 +# TEXT column by type works.
2647 +do_test recover-syntax-4.0 {
2648 + db eval {DROP TABLE IF EXISTS temp.syntax}
2650 + CREATE VIRTUAL TABLE temp.syntax USING recover(
2654 + PRAGMA table_info(syntax);
2656 +} {0 t TEXT 0 {} 0}
2659 +do_test recover-syntax-4.1 {
2660 + db eval {DROP TABLE IF EXISTS temp.syntax}
2662 + CREATE VIRTUAL TABLE temp.syntax USING recover(
2666 + PRAGMA table_info(syntax);
2668 +} {0 t TEXT 1 {} 0}
2671 +do_test recover-syntax-4.2 {
2672 + db eval {DROP TABLE IF EXISTS temp.syntax}
2674 + CREATE VIRTUAL TABLE temp.syntax USING recover(
2678 + PRAGMA table_info(syntax);
2680 +} {0 t TEXT 0 {} 0}
2682 +# TEXT STRICT NOT NULL
2683 +do_test recover-syntax-4.3 {
2684 + db eval {DROP TABLE IF EXISTS temp.syntax}
2686 + CREATE VIRTUAL TABLE temp.syntax USING recover(
2688 + t TEXT STRICT NOT NULL
2690 + PRAGMA table_info(syntax);
2692 +} {0 t TEXT 1 {} 0}
2695 +do_test recover-syntax-5.0 {
2696 + db eval {DROP TABLE IF EXISTS temp.syntax}
2698 + CREATE VIRTUAL TABLE temp.syntax USING recover(
2702 + PRAGMA table_info(syntax);
2704 +} {0 i INTEGER 0 {} 0}
2707 +do_test recover-syntax-5.1 {
2708 + db eval {DROP TABLE IF EXISTS temp.syntax}
2710 + CREATE VIRTUAL TABLE temp.syntax USING recover(
2712 + i INTEGER NOT NULL
2714 + PRAGMA table_info(syntax);
2716 +} {0 i INTEGER 1 {} 0}
2719 +do_test recover-syntax-5.2 {
2720 + db eval {DROP TABLE IF EXISTS temp.syntax}
2722 + CREATE VIRTUAL TABLE temp.syntax USING recover(
2726 + PRAGMA table_info(syntax);
2728 +} {0 i INTEGER 0 {} 0}
2730 +# INTEGER STRICT NOT NULL
2731 +do_test recover-syntax-5.3 {
2732 + db eval {DROP TABLE IF EXISTS temp.syntax}
2734 + CREATE VIRTUAL TABLE temp.syntax USING recover(
2736 + i INTEGER STRICT NOT NULL
2738 + PRAGMA table_info(syntax);
2740 +} {0 i INTEGER 1 {} 0}
2743 +do_test recover-syntax-6.0 {
2744 + db eval {DROP TABLE IF EXISTS temp.syntax}
2746 + CREATE VIRTUAL TABLE temp.syntax USING recover(
2750 + PRAGMA table_info(syntax);
2752 +} {0 b BLOB 0 {} 0}
2755 +do_test recover-syntax-6.1 {
2756 + db eval {DROP TABLE IF EXISTS temp.syntax}
2758 + CREATE VIRTUAL TABLE temp.syntax USING recover(
2762 + PRAGMA table_info(syntax);
2764 +} {0 b BLOB 1 {} 0}
2767 +do_test recover-syntax-6.2 {
2768 + db eval {DROP TABLE IF EXISTS temp.syntax}
2770 + CREATE VIRTUAL TABLE temp.syntax USING recover(
2774 + PRAGMA table_info(syntax);
2776 +} {0 b BLOB 0 {} 0}
2778 +# BLOB STRICT NOT NULL
2779 +do_test recover-syntax-6.3 {
2780 + db eval {DROP TABLE IF EXISTS temp.syntax}
2782 + CREATE VIRTUAL TABLE temp.syntax USING recover(
2784 + b BLOB STRICT NOT NULL
2786 + PRAGMA table_info(syntax);
2788 +} {0 b BLOB 1 {} 0}
2791 +do_test recover-syntax-7.0 {
2792 + db eval {DROP TABLE IF EXISTS temp.syntax}
2794 + CREATE VIRTUAL TABLE temp.syntax USING recover(
2798 + PRAGMA table_info(syntax);
2800 +} {0 f FLOAT 0 {} 0}
2803 +do_test recover-syntax-7.1 {
2804 + db eval {DROP TABLE IF EXISTS temp.syntax}
2806 + CREATE VIRTUAL TABLE temp.syntax USING recover(
2810 + PRAGMA table_info(syntax);
2812 +} {0 f FLOAT 1 {} 0}
2815 +do_test recover-syntax-7.2 {
2816 + db eval {DROP TABLE IF EXISTS temp.syntax}
2818 + CREATE VIRTUAL TABLE temp.syntax USING recover(
2822 + PRAGMA table_info(syntax);
2824 +} {0 f FLOAT 0 {} 0}
2826 +# FLOAT STRICT NOT NULL
2827 +do_test recover-syntax-7.3 {
2828 + db eval {DROP TABLE IF EXISTS temp.syntax}
2830 + CREATE VIRTUAL TABLE temp.syntax USING recover(
2832 + f FLOAT STRICT NOT NULL
2834 + PRAGMA table_info(syntax);
2836 +} {0 f FLOAT 1 {} 0}
2839 +do_test recover-syntax-8.0 {
2840 + db eval {DROP TABLE IF EXISTS temp.syntax}
2842 + CREATE VIRTUAL TABLE temp.syntax USING recover(
2846 + PRAGMA table_info(syntax);
2848 +} {0 f NUMERIC 0 {} 0}
2851 +do_test recover-syntax-8.1 {
2852 + db eval {DROP TABLE IF EXISTS temp.syntax}
2854 + CREATE VIRTUAL TABLE temp.syntax USING recover(
2856 + f NUMERIC NOT NULL
2858 + PRAGMA table_info(syntax);
2860 +} {0 f NUMERIC 1 {} 0}
2863 +do_test recover-syntax-8.2 {
2864 + db eval {DROP TABLE IF EXISTS temp.syntax}
2866 + CREATE VIRTUAL TABLE temp.syntax USING recover(
2870 + PRAGMA table_info(syntax);
2872 +} {0 f NUMERIC 0 {} 0}
2874 +# NUMERIC STRICT NOT NULL
2875 +do_test recover-syntax-8.3 {
2876 + db eval {DROP TABLE IF EXISTS temp.syntax}
2878 + CREATE VIRTUAL TABLE temp.syntax USING recover(
2880 + f NUMERIC STRICT NOT NULL
2882 + PRAGMA table_info(syntax);
2884 +} {0 f NUMERIC 1 {} 0}
2887 +do_test recover-syntax-9.0 {
2888 + db eval {DROP TABLE IF EXISTS temp.syntax}
2890 + CREATE VIRTUAL TABLE temp.syntax USING recover(
2895 + PRAGMA table_info(syntax);
2897 +} {0 id INTEGER 1 {} 0 1 v {} 0 {} 0}
2899 +# ROWID NOT NULL (is default)
2900 +do_test recover-syntax-9.1 {
2901 + db eval {DROP TABLE IF EXISTS temp.syntax}
2903 + CREATE VIRTUAL TABLE temp.syntax USING recover(
2905 + id ROWID NOT NULL,
2908 + PRAGMA table_info(syntax);
2910 +} {0 id INTEGER 1 {} 0 1 v {} 0 {} 0}
2913 +do_test recover-syntax-9.0 {
2914 + db eval {DROP TABLE IF EXISTS temp.syntax}
2916 + CREATE VIRTUAL TABLE temp.syntax USING recover(
2921 + PRAGMA table_info(syntax);
2923 +} {0 id INTEGER 1 {} 0 1 v {} 0 {} 0}
2925 +# ROWID STRICT NOT NULL (is default)
2926 +do_test recover-syntax-9.1 {
2927 + db eval {DROP TABLE IF EXISTS temp.syntax}
2929 + CREATE VIRTUAL TABLE temp.syntax USING recover(
2931 + id ROWID STRICT NOT NULL,
2934 + PRAGMA table_info(syntax);
2936 +} {0 id INTEGER 1 {} 0 1 v {} 0 {} 0}
2938 +# Invalid type info is not ignored.
2939 +do_test recover-syntax-10.0 {
2940 + db eval {DROP TABLE IF EXISTS temp.syntax}
2942 + CREATE VIRTUAL TABLE temp.syntax USING recover(
2947 +} {1 {unable to parse column 0}}
2949 +# Extraneous type info is not ignored.
2950 +do_test recover-syntax-10.1 {
2951 + db eval {DROP TABLE IF EXISTS temp.syntax}
2953 + CREATE VIRTUAL TABLE temp.syntax USING recover(
2958 +} {1 {unable to parse column 0}}
2960 +# Extraneous type info is not ignored.
2961 +do_test recover-syntax-10.2 {
2962 + db eval {DROP TABLE IF EXISTS temp.syntax}
2964 + CREATE VIRTUAL TABLE temp.syntax USING recover(
2966 + v INTEGER NOT NULL GARBAGE
2969 +} {1 {unable to parse column 0}}
2971 +# Multiple types don't work.
2972 +do_test recover-syntax-10.3 {
2973 + db eval {DROP TABLE IF EXISTS temp.syntax}
2975 + CREATE VIRTUAL TABLE temp.syntax USING recover(
2977 + v INTEGER FLOAT BLOB
2980 +} {1 {unable to parse column 0}}
2982 +# Multiple types don't work.
2983 +do_test recover-syntax-10.4 {
2984 + db eval {DROP TABLE IF EXISTS temp.syntax}
2986 + CREATE VIRTUAL TABLE temp.syntax USING recover(
2988 + v INTEGER NOT NULL TEXT
2991 +} {1 {unable to parse column 0}}
2994 diff --git a/third_party/sqlite/src/test/recover1.test b/third_party/sqlite/src/test/recover1.test
2995 new file mode 100644
2996 index 0000000..1d90f09
2998 +++ b/third_party/sqlite/src/test/recover1.test
3000 +# 2012 January 4 {}
3002 +# The author disclaims copyright to this source code. In place of
3003 +# a legal notice, here is a blessing:
3005 +# May you do good and not evil.
3006 +# May you find forgiveness for yourself and forgive others.
3007 +# May you share freely, never taking more than you give.
3009 +#***********************************************************************
3010 +# This file implements regression tests for SQLite library.
3012 +# Use tables to test leaf-node reading, and also type checking.
3016 +set testdir [file dirname $argv0]
3017 +source $testdir/tester.tcl
3019 +# A really basic table with manifest typing and a row of each type.
3023 + DROP TABLE IF EXISTS types;
3024 + CREATE TABLE types (rowtype TEXT, value);
3025 + INSERT INTO types VALUES ("NULL", NULL);
3026 + INSERT INTO types VALUES ("INTEGER", 17);
3027 + INSERT INTO types VALUES ("FLOAT", 3.1415927);
3028 + INSERT INTO types VALUES ("TEXT", "This is text");
3029 + INSERT INTO types VALUES ("BLOB", CAST("This is a blob" AS BLOB));
3031 + -- Same contents, with an alias for rowid. Testing separately
3032 + -- because it changes the structure of the data (the alias column is
3033 + -- serialized as NULL).
3034 + DROP TABLE IF EXISTS types2;
3035 + CREATE TABLE types2 (id INTEGER PRIMARY KEY, rowtype TEXT, value);
3036 + INSERT INTO types2 (id, rowtype, value)
3037 + SELECT rowid, rowtype, value FROM types;
3040 +# Baseline results.
3041 +do_test recover-types-0.0 {
3042 + execsql {SELECT rowid, rowtype, value, TYPEOF(value) FROM types}
3043 +} {1 NULL {} null 2 INTEGER 17 integer 3 FLOAT 3.1415927 real 4 TEXT {This is text} text 5 BLOB {This is a blob} blob}
3045 +# With no restrictions, recover table shows identical results.
3046 +do_test recover-types-0.1 {
3048 + DROP TABLE IF EXISTS temp.types_recover;
3049 + CREATE VIRTUAL TABLE temp.types_recover USING recover(
3055 + execsql {SELECT rowid, rowtype, value, TYPEOF(value) FROM types_recover}
3056 +} {1 NULL {} null 2 INTEGER 17 integer 3 FLOAT 3.1415927 real 4 TEXT {This is text} text 5 BLOB {This is a blob} blob}
3058 +# Restrict by INTEGER
3059 +do_test recover-types-1.0 {
3061 + DROP TABLE IF EXISTS temp.types_recover;
3062 + CREATE VIRTUAL TABLE temp.types_recover USING recover(
3068 + execsql {SELECT rowid, rowtype, value, TYPEOF(value) FROM types_recover}
3069 +} {1 NULL {} null 2 INTEGER 17 integer}
3071 +# Restrict by INTEGER NOT NULL
3072 +do_test recover-types-1.1 {
3074 + DROP TABLE IF EXISTS temp.types_recover;
3075 + CREATE VIRTUAL TABLE temp.types_recover USING recover(
3078 + value INTEGER NOT NULL
3081 + execsql {SELECT rowid, rowtype, value, TYPEOF(value) FROM types_recover}
3082 +} {2 INTEGER 17 integer}
3084 +# Restrict by FLOAT
3085 +do_test recover-types-2.0 {
3087 + DROP TABLE IF EXISTS temp.types_recover;
3088 + CREATE VIRTUAL TABLE temp.types_recover USING recover(
3094 + execsql {SELECT rowid, rowtype, value, TYPEOF(value) FROM types_recover}
3095 +} {1 NULL {} null 2 INTEGER 17.0 real 3 FLOAT 3.1415927 real}
3097 +# Restrict by FLOAT NOT NULL
3098 +do_test recover-types-2.1 {
3100 + DROP TABLE IF EXISTS temp.types_recover;
3101 + CREATE VIRTUAL TABLE temp.types_recover USING recover(
3104 + value FLOAT NOT NULL
3107 + execsql {SELECT rowid, rowtype, value, TYPEOF(value) FROM types_recover}
3108 +} {2 INTEGER 17.0 real 3 FLOAT 3.1415927 real}
3110 +# Restrict by FLOAT STRICT
3111 +do_test recover-types-2.2 {
3113 + DROP TABLE IF EXISTS temp.types_recover;
3114 + CREATE VIRTUAL TABLE temp.types_recover USING recover(
3117 + value FLOAT STRICT
3120 + execsql {SELECT rowid, rowtype, value, TYPEOF(value) FROM types_recover}
3121 +} {1 NULL {} null 3 FLOAT 3.1415927 real}
3123 +# Restrict by FLOAT STRICT NOT NULL
3124 +do_test recover-types-2.3 {
3126 + DROP TABLE IF EXISTS temp.types_recover;
3127 + CREATE VIRTUAL TABLE temp.types_recover USING recover(
3130 + value FLOAT STRICT NOT NULL
3133 + execsql {SELECT rowid, rowtype, value, TYPEOF(value) FROM types_recover}
3134 +} {3 FLOAT 3.1415927 real}
3137 +do_test recover-types-3.0 {
3139 + DROP TABLE IF EXISTS temp.types_recover;
3140 + CREATE VIRTUAL TABLE temp.types_recover USING recover(
3146 + execsql {SELECT rowid, rowtype, value, TYPEOF(value) FROM types_recover}
3147 +} {1 NULL {} null 4 TEXT {This is text} text 5 BLOB {This is a blob} blob}
3149 +# Restrict by TEXT NOT NULL
3150 +do_test recover-types-3.1 {
3152 + DROP TABLE IF EXISTS temp.types_recover;
3153 + CREATE VIRTUAL TABLE temp.types_recover USING recover(
3156 + value TEXT NOT NULL
3159 + execsql {SELECT rowid, rowtype, value, TYPEOF(value) FROM types_recover}
3160 +} {4 TEXT {This is text} text 5 BLOB {This is a blob} blob}
3162 +# Restrict by TEXT STRICT
3163 +do_test recover-types-3.2 {
3165 + DROP TABLE IF EXISTS temp.types_recover;
3166 + CREATE VIRTUAL TABLE temp.types_recover USING recover(
3172 + execsql {SELECT rowid, rowtype, value, TYPEOF(value) FROM types_recover}
3173 +} {1 NULL {} null 4 TEXT {This is text} text}
3175 +# Restrict by TEXT STRICT NOT NULL
3176 +do_test recover-types-3.3 {
3178 + DROP TABLE IF EXISTS temp.types_recover;
3179 + CREATE VIRTUAL TABLE temp.types_recover USING recover(
3182 + value TEXT STRICT NOT NULL
3185 + execsql {SELECT rowid, rowtype, value, TYPEOF(value) FROM types_recover}
3186 +} {4 TEXT {This is text} text}
3189 +do_test recover-types-4.0 {
3191 + DROP TABLE IF EXISTS temp.types_recover;
3192 + CREATE VIRTUAL TABLE temp.types_recover USING recover(
3198 + execsql {SELECT rowid, rowtype, value, TYPEOF(value) FROM types_recover}
3199 +} {1 NULL {} null 5 BLOB {This is a blob} blob}
3201 +# Restrict by BLOB NOT NULL
3202 +do_test recover-types-4.1 {
3204 + DROP TABLE IF EXISTS temp.types_recover;
3205 + CREATE VIRTUAL TABLE temp.types_recover USING recover(
3208 + value BLOB NOT NULL
3211 + execsql {SELECT rowid, rowtype, value, TYPEOF(value) FROM types_recover}
3212 +} {5 BLOB {This is a blob} blob}
3215 +do_test recover-types-5.0 {
3217 + DROP TABLE IF EXISTS temp.types_recover;
3218 + CREATE VIRTUAL TABLE temp.types_recover USING recover(
3224 + execsql {SELECT rowid, rowtype, value, TYPEOF(value) FROM types_recover}
3225 +} {1 NULL {} null 2 INTEGER 17 integer 3 FLOAT 3.1415927 real 4 TEXT {This is text} text 5 BLOB {This is a blob} blob}
3227 +# Should get same results specifying manifest typing explicitly.
3228 +do_test recover-types-5.1 {
3230 + DROP TABLE IF EXISTS temp.types_recover;
3231 + CREATE VIRTUAL TABLE temp.types_recover USING recover(
3237 + execsql {SELECT rowid, rowtype, value, TYPEOF(value) FROM types_recover}
3238 +} {1 NULL {} null 2 INTEGER 17 integer 3 FLOAT 3.1415927 real 4 TEXT {This is text} text 5 BLOB {This is a blob} blob}
3240 +# Same results, skipping the NULL row.
3241 +do_test recover-types-5.2 {
3243 + DROP TABLE IF EXISTS temp.types_recover;
3244 + CREATE VIRTUAL TABLE temp.types_recover USING recover(
3247 + value ANY NOT NULL
3250 + execsql {SELECT rowid, rowtype, value, TYPEOF(value) FROM types_recover}
3251 +} {2 INTEGER 17 integer 3 FLOAT 3.1415927 real 4 TEXT {This is text} text 5 BLOB {This is a blob} blob}
3253 +# Test ROWID values.
3254 +do_test recover-types-6.0 {
3256 + DROP TABLE IF EXISTS temp.types2_recover;
3257 + CREATE VIRTUAL TABLE temp.types2_recover USING recover(
3264 + execsql {SELECT rowid, id, rowtype, value, TYPEOF(value) FROM types2_recover}
3265 +} {1 1 NULL {} null 2 2 INTEGER 17 integer 3 3 FLOAT 3.1415927 real 4 4 TEXT {This is text} text 5 5 BLOB {This is a blob} blob}
3267 +# ROWID NOT NULL is identical.
3268 +do_test recover-types-6.1 {
3270 + DROP TABLE IF EXISTS temp.types2_recover;
3271 + CREATE VIRTUAL TABLE temp.types2_recover USING recover(
3273 + id ROWID NOT NULL,
3278 + execsql {SELECT rowid, id, rowtype, value, TYPEOF(value) FROM types2_recover}
3279 +} {1 1 NULL {} null 2 2 INTEGER 17 integer 3 3 FLOAT 3.1415927 real 4 4 TEXT {This is text} text 5 5 BLOB {This is a blob} blob}
3281 +# Check that each of the possible integer sizes is being decoded.
3282 +# TODO(shess): It would be neat to ACTUALLY test these things. As-is,
3283 +# this should exercise the code paths, but one needs logging or a
3284 +# debugger to verify that things are stored as expected.
3285 +do_test recover-types-7.0 {
3287 + DROP TABLE IF EXISTS integers;
3288 + CREATE TABLE integers (value);
3290 + -- encoded directly in type info.
3291 + INSERT INTO integers VALUES (0);
3292 + INSERT INTO integers VALUES (1);
3295 + INSERT INTO integers VALUES (2);
3296 + INSERT INTO integers VALUES (-2);
3297 + INSERT INTO integers VALUES (127);
3298 + INSERT INTO integers VALUES (-128);
3301 + INSERT INTO integers VALUES (12345);
3302 + INSERT INTO integers VALUES (-12345);
3303 + INSERT INTO integers VALUES (32767);
3304 + INSERT INTO integers VALUES (-32768);
3307 + INSERT INTO integers VALUES (1234567);
3308 + INSERT INTO integers VALUES (-1234567);
3309 + INSERT INTO integers VALUES (8388607);
3310 + INSERT INTO integers VALUES (-8388608);
3313 + INSERT INTO integers VALUES (1234567890);
3314 + INSERT INTO integers VALUES (-1234567890);
3315 + INSERT INTO integers VALUES (2147483647);
3316 + INSERT INTO integers VALUES (-2147483648);
3319 + INSERT INTO integers VALUES (123456789012345);
3320 + INSERT INTO integers VALUES (-123456789012345);
3321 + INSERT INTO integers VALUES (140737488355327);
3322 + INSERT INTO integers VALUES (-140737488355328);
3325 + INSERT INTO integers VALUES (9223372036854775807);
3326 + INSERT INTO integers VALUES (-9223372036854775808);
3328 + DROP TABLE IF EXISTS integers_recover;
3329 + CREATE VIRTUAL TABLE temp.integers_recover USING recover(
3334 + execsql {SELECT rowid, value FROM integers_recover}
3335 +} {1 0 2 1 3 2 4 -2 5 127 6 -128 7 12345 8 -12345 9 32767 10 -32768 11 1234567 12 -1234567 13 8388607 14 -8388608 15 1234567890 16 -1234567890 17 2147483647 18 -2147483648 19 123456789012345 20 -123456789012345 21 140737488355327 22 -140737488355328 23 9223372036854775807 24 -9223372036854775808}
3337 +# If UTF16 support is disabled, ignore the rest of the tests.
3339 +ifcapable {!utf16} {
3345 +file delete -force test.db
3346 +sqlite3 db test.db;
3348 + PRAGMA encoding = 'UTF-8';
3351 +do_test recover-encoding-1.0 {
3353 + DROP TABLE IF EXISTS e;
3354 + CREATE TABLE e (v TEXT);
3355 + INSERT INTO e VALUES('Mjollnir');
3356 + INSERT INTO e VALUES('Mjölnir');
3357 + INSERT INTO e VALUES('Mjǫlnir');
3358 + INSERT INTO e VALUES('Mjölner');
3359 + INSERT INTO e VALUES('Mjølner');
3360 + INSERT INTO e VALUES('ハンマー');
3363 + DROP TABLE IF EXISTS e_recover;
3364 + CREATE VIRTUAL TABLE temp.e_recover USING recover(
3368 + SELECT rowid, v FROM e_recover ORDER BY rowid;
3370 +} {UTF-8 1 Mjollnir 2 Mjölnir 3 Mjǫlnir 4 Mjölner 5 Mjølner 6 ハンマー}
3372 +# Reset the database to UTF-16LE.
3373 +file delete -force test.db
3374 +sqlite3 db test.db;
3376 + PRAGMA encoding = 'UTF-16LE';
3379 +do_test recover-encoding-2.0 {
3381 + DROP TABLE IF EXISTS e;
3382 + CREATE TABLE e (v TEXT);
3383 + INSERT INTO e VALUES('Mjollnir');
3384 + INSERT INTO e VALUES('Mjölnir');
3385 + INSERT INTO e VALUES('Mjǫlnir');
3386 + INSERT INTO e VALUES('Mjölner');
3387 + INSERT INTO e VALUES('Mjølner');
3388 + INSERT INTO e VALUES('ハンマー');
3391 + DROP TABLE IF EXISTS e_recover;
3392 + CREATE VIRTUAL TABLE temp.e_recover USING recover(
3396 + SELECT rowid, v FROM e_recover ORDER BY rowid;
3398 +} {UTF-16le 1 Mjollnir 2 Mjölnir 3 Mjǫlnir 4 Mjölner 5 Mjølner 6 ハンマー}
3400 +# Reset the database to UTF-16BE.
3401 +file delete -force test.db
3402 +sqlite3 db test.db;
3404 + PRAGMA encoding = 'UTF-16BE';
3407 +do_test recover-encoding-3.0 {
3409 + DROP TABLE IF EXISTS e;
3410 + CREATE TABLE e (v TEXT);
3411 + INSERT INTO e VALUES('Mjollnir');
3412 + INSERT INTO e VALUES('Mjölnir');
3413 + INSERT INTO e VALUES('Mjǫlnir');
3414 + INSERT INTO e VALUES('Mjölner');
3415 + INSERT INTO e VALUES('Mjølner');
3416 + INSERT INTO e VALUES('ハンマー');
3419 + DROP TABLE IF EXISTS e_recover;
3420 + CREATE VIRTUAL TABLE temp.e_recover USING recover(
3424 + SELECT rowid, v FROM e_recover ORDER BY rowid;
3426 +} {UTF-16be 1 Mjollnir 2 Mjölnir 3 Mjǫlnir 4 Mjölner 5 Mjølner 6 ハンマー}
3429 diff --git a/third_party/sqlite/src/test/recover2.test b/third_party/sqlite/src/test/recover2.test
3430 new file mode 100644
3431 index 0000000..8aa4e04
3433 +++ b/third_party/sqlite/src/test/recover2.test
3435 +# 2012 January 4 {}
3437 +# The author disclaims copyright to this source code. In place of
3438 +# a legal notice, here is a blessing:
3440 +# May you do good and not evil.
3441 +# May you find forgiveness for yourself and forgive others.
3442 +# May you share freely, never taking more than you give.
3444 +#***********************************************************************
3445 +# This file implements regression tests for SQLite library.
3447 +# This file implements tests for how the recover module handles cell
3452 +set testdir [file dirname $argv0]
3453 +source $testdir/tester.tcl
3455 +set lorem "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam sagittis gravida odio vitae ultrices. Nulla facilisi. Maecenas pulvinar, tellus ut bibendum semper, nibh tellus auctor nulla, in dignissim nisi ipsum id arcu. Nullam tincidunt arcu malesuada turpis faucibus in adipiscing enim mattis. Fusce augue magna, scelerisque sollicitudin egestas in, cursus eu sapien. Pellentesque tempor risus at lectus convallis a convallis orci ornare. Integer tristique aliquam leo vel interdum.
3457 +Phasellus quis dictum nisi. Curabitur at enim non felis pharetra imperdiet. Duis tempus massa eu leo varius porta. Vestibulum sodales nulla at purus tincidunt ultrices. Nam euismod posuere nibh, nec sodales magna luctus ac. Ut commodo hendrerit mauris vitae gravida. In interdum justo ut sem eleifend convallis. Donec cursus molestie elementum. Suspendisse at nisl tellus, vel consequat mauris. Nullam non justo nibh.
3459 +Maecenas varius sollicitudin libero, nec feugiat turpis facilisis eget. Quisque et sem risus. Aenean a magna quis purus hendrerit mattis eu vel lorem. Aenean fringilla diam eget tortor lacinia sed mollis eros feugiat. Quisque ac purus sapien. Nullam quis tellus vel magna convallis tincidunt. Donec eget ligula at libero tincidunt congue ut ut lacus. Integer dignissim aliquet congue. Pellentesque sed risus vitae lorem porta viverra ac eu risus. Vivamus congue suscipit odio pulvinar aliquet. Aliquam porttitor nunc non sapien auctor et vehicula augue molestie.
3461 +Aliquam et dui ac sem tempus dictum. Fusce arcu nulla, viverra sit amet suscipit quis, malesuada at felis. Fusce ut diam felis. Fusce id ligula non eros fermentum sodales in nec quam. Donec tempor bibendum arcu ac adipiscing. Praesent nisl lectus, tempor ut vehicula eget, mattis a justo. Mauris condimentum luctus eros a varius. Morbi mollis elit eget velit convallis eu sodales odio egestas. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Vivamus interdum, metus sit amet varius varius, lectus eros semper risus, sed sagittis ipsum libero in sapien. Nam lacinia nulla vitae magna facilisis scelerisque. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
3463 +Donec gravida dignissim eleifend. Aliquam vel tincidunt tortor. Curabitur massa ante, blandit a auctor at, ullamcorper sed nisl. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Suspendisse ut felis a eros egestas ultricies et quis mi. Vivamus ut risus massa. Donec nec ornare erat. Aliquam ornare, lorem a rhoncus aliquam, tellus diam tincidunt tellus, a mattis nunc ante ac arcu. Curabitur nec metus id risus commodo ullamcorper eu ut tortor."
3465 +# Create a database which needs a multiple overflow pages to test the
3466 +# transition from main page to overflow, and then overflow to
3468 +do_test recover-overflow-1.0 {
3470 + DROP TABLE IF EXISTS overflow;
3471 + CREATE TABLE overflow (value TEXT);
3472 + INSERT INTO overflow VALUES ($lorem);
3474 + DROP TABLE IF EXISTS overflow_recover;
3475 + CREATE VIRTUAL TABLE temp.overflow_recover USING recover(
3481 + # Should have root page, leaf page, and 2 overflow pages, because
3482 + # length(value) is more than 2x page_size.
3484 + PRAGMA page_count;
3486 + SELECT rowid, TYPEOF(value), length(value), value FROM overflow_recover;
3488 +} [list 4 1024 1 text [string length $lorem] $lorem]
3490 +# No overflow. [1024-35 == 990, overhead of 1-byte rowid, 2-byte
3491 +# record length, 1-byte header length, 2-byte field type.]
3492 +set substr [string range $lorem 0 985]
3493 +do_test recover-overflow-1.1 {
3495 + DROP TABLE IF EXISTS overflow;
3496 + CREATE TABLE overflow (value TEXT);
3497 + INSERT INTO overflow VALUES ($substr);
3499 + DROP TABLE IF EXISTS overflow_recover;
3500 + CREATE VIRTUAL TABLE temp.overflow_recover USING recover(
3506 + # Trim to remove excess pages from prior tests.
3510 + PRAGMA page_count;
3512 + SELECT rowid, TYPEOF(value), length(value), value FROM overflow_recover;
3514 +} [list 2 1024 1 text [string length $substr] $substr]
3516 +# One byte of overflow.
3517 +set substr [string range $lorem 0 986]
3518 +do_test recover-overflow-1.2 {
3520 + DROP TABLE IF EXISTS overflow;
3521 + CREATE TABLE overflow (value TEXT);
3522 + INSERT INTO overflow VALUES ($substr);
3524 + DROP TABLE IF EXISTS overflow_recover;
3525 + CREATE VIRTUAL TABLE temp.overflow_recover USING recover(
3531 + # Trim to remove excess pages from prior tests.
3535 + PRAGMA page_count;
3537 + SELECT rowid, TYPEOF(value), length(value), value FROM overflow_recover;
3539 +} [list 3 1024 1 text [string length $substr] $substr]
3541 +# One full overflow page, plus maxLocal in-leaf. [985+1020]
3542 +set substr [string range $lorem 0 2005]
3543 +do_test recover-overflow-1.3 {
3545 + DROP TABLE IF EXISTS overflow;
3546 + CREATE TABLE overflow (value TEXT);
3547 + INSERT INTO overflow VALUES ($substr);
3549 + DROP TABLE IF EXISTS overflow_recover;
3550 + CREATE VIRTUAL TABLE temp.overflow_recover USING recover(
3556 + # Trim to remove excess pages from prior tests.
3560 + PRAGMA page_count;
3562 + SELECT rowid, TYPEOF(value), length(value), value FROM overflow_recover;
3564 +} [list 3 1024 1 text [string length $substr] $substr]
3566 +# Overflow to a second overflow page.
3567 +set substr [string range $lorem 0 2006]
3568 +do_test recover-overflow-1.4 {
3570 + DROP TABLE IF EXISTS overflow;
3571 + CREATE TABLE overflow (value TEXT);
3572 + INSERT INTO overflow VALUES ($substr);
3574 + DROP TABLE IF EXISTS overflow_recover;
3575 + CREATE VIRTUAL TABLE temp.overflow_recover USING recover(
3581 + # Trim to remove excess pages from prior tests.
3585 + PRAGMA page_count;
3587 + SELECT rowid, TYPEOF(value), length(value), value FROM overflow_recover;
3589 +} [list 4 1024 1 text [string length $substr] $substr]
3592 diff --git a/third_party/sqlite/src/tool/mksqlite3c.tcl b/third_party/sqlite/src/tool/mksqlite3c.tcl
3593 index 0e97923..9e2b0fe 100644
3594 --- a/third_party/sqlite/src/tool/mksqlite3c.tcl
3595 +++ b/third_party/sqlite/src/tool/mksqlite3c.tcl
3596 @@ -316,6 +316,8 @@ foreach file {