SQLite

Check-in [3213a15f21]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add the sqlite_dbptr virtual table to the dbdata extension. For querying the links between b-tree pages.
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | dbdata
Files: files | file ages | folders
SHA3-256: 3213a15f2133afbb0a4fec3b8f6e0eeca8c0befafd6658c41074e84f589d5d32
User & Date: dan 2019-04-18 21:14:11.260
Context
2019-04-20
20:57
Add the ".recovery" command to the shell tool. For recovering the maximum amount data from corrupt databases. Still needs work. (check-in: 7461d2e120 user: dan tags: dbdata)
2019-04-18
21:14
Add the sqlite_dbptr virtual table to the dbdata extension. For querying the links between b-tree pages. (check-in: 3213a15f21 user: dan tags: dbdata)
2019-04-17
21:17
Add the experimental dbdata extension. (check-in: a3ab588329 user: dan tags: dbdata)
Changes
Unified Diff Ignore Whitespace Patch
Changes to ext/misc/dbdata.c.
19
20
21
22
23
24
25



26
27
28
29
30
31
32
**       pgno INTEGER,
**       cell INTEGER,
**       field INTEGER,
**       value ANY,
**       schema TEXT HIDDEN
**     );
**



** Each page of the database is inspected. If it cannot be interpreted as a
** b-tree page, or if it is a b-tree page containing 0 entries, the
** sqlite_dbdata table contains no rows for that page.  Otherwise, the table
** contains one row for each field in the record associated with each
** cell on the page. For intkey b-trees, the key value is stored in field -1.
**
** For example, for the database:







>
>
>







19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
**       pgno INTEGER,
**       cell INTEGER,
**       field INTEGER,
**       value ANY,
**       schema TEXT HIDDEN
**     );
**
** IMPORTANT: THE VIRTUAL TABLE SCHEMA ABOVE IS SUBJECT TO CHANGE. IN THE
** FUTURE NEW NON-HIDDEN COLUMNS MAY BE ADDED BETWEEN "value" AND "schema".
**
** Each page of the database is inspected. If it cannot be interpreted as a
** b-tree page, or if it is a b-tree page containing 0 entries, the
** sqlite_dbdata table contains no rows for that page.  Otherwise, the table
** contains one row for each field in the record associated with each
** cell on the page. For intkey b-trees, the key value is stored in field -1.
**
** For example, for the database:
48
49
50
51
52
53
54







55
56
57
58
59
60
61
62
63
64
65
66
67
68

69
70
71
72
73
74
75
76
77
78
79




80
81
82
83
84
85
86
87
88
89
90
91
92

93
94
95
96
97
98
99
100




101
102
103
104
105
106
107
108







109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129

130
131
132
133
134
135
136
**
** If database corruption is encountered, this module does not report an
** error. Instead, it attempts to extract as much data as possible and
** ignores the corruption.
**
** This module requires that the "sqlite_dbpage" eponymous virtual table be
** available.







*/
#if !defined(SQLITEINT_H)
#include "sqlite3ext.h"

typedef unsigned char u8;
typedef unsigned int u32;

#endif
SQLITE_EXTENSION_INIT1
#include <string.h>
#include <assert.h>

typedef struct DbdataTable DbdataTable;
typedef struct DbdataCursor DbdataCursor;


/* A cursor for the sqlite_dbdata table */
struct DbdataCursor {
  sqlite3_vtab_cursor base;       /* Base class.  Must be first */
  sqlite3_stmt *pStmt;            /* For fetching database pages */

  int iPgno;                      /* Current page number */
  u8 *aPage;                      /* Buffer containing page */
  int nPage;                      /* Size of aPage[] in bytes */
  int nCell;                      /* Number of cells on aPage[] */
  int iCell;                      /* Current cell number */




  u8 *pRec;                       /* Buffer containing current record */
  int nRec;                       /* Size of pRec[] in bytes */
  int nField;                     /* Number of fields in pRec */
  int iField;                     /* Current field number */
  sqlite3_int64 iIntkey;          /* Integer key value */

  sqlite3_int64 iRowid;
};

/* The sqlite_dbdata table */
struct DbdataTable {
  sqlite3_vtab base;              /* Base class.  Must be first */
  sqlite3 *db;                    /* The database connection */

};

#define DBDATA_COLUMN_PGNO        0
#define DBDATA_COLUMN_CELL        1
#define DBDATA_COLUMN_FIELD       2
#define DBDATA_COLUMN_VALUE       3
#define DBDATA_COLUMN_SCHEMA      4





#define DBDATA_SCHEMA             \
      "CREATE TABLE x("           \
      "  pgno INTEGER,"           \
      "  cell INTEGER,"           \
      "  field INTEGER,"          \
      "  value ANY,"              \
      "  schema TEXT HIDDEN"      \
      ")"








/*
** Connect to the sqlite_dbdata virtual table.
*/
static int dbdataConnect(
  sqlite3 *db,
  void *pAux,
  int argc, const char *const*argv,
  sqlite3_vtab **ppVtab,
  char **pzErr
){
  DbdataTable *pTab = 0;
  int rc = sqlite3_declare_vtab(db, DBDATA_SCHEMA);

  if( rc==SQLITE_OK ){
    pTab = (DbdataTable*)sqlite3_malloc64(sizeof(DbdataTable));
    if( pTab==0 ){
      rc = SQLITE_NOMEM;
    }else{
      memset(pTab, 0, sizeof(DbdataTable));
      pTab->db = db;

    }
  }

  *ppVtab = (sqlite3_vtab*)pTab;
  return rc;
}








>
>
>
>
>
>
>














>











>
>
>
>





<
<






>








>
>
>
>








>
>
>
>
>
>
>












|








>







51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
**
** If database corruption is encountered, this module does not report an
** error. Instead, it attempts to extract as much data as possible and
** ignores the corruption.
**
** This module requires that the "sqlite_dbpage" eponymous virtual table be
** available.
**
**
**     CREATE TABLE sqlite_dbptr(
**       pgno INTEGER,
**       child INTEGER,
**       schema TEXT HIDDEN
**     );
*/
#if !defined(SQLITEINT_H)
#include "sqlite3ext.h"

typedef unsigned char u8;
typedef unsigned int u32;

#endif
SQLITE_EXTENSION_INIT1
#include <string.h>
#include <assert.h>

typedef struct DbdataTable DbdataTable;
typedef struct DbdataCursor DbdataCursor;


/* A cursor for the sqlite_dbdata table */
struct DbdataCursor {
  sqlite3_vtab_cursor base;       /* Base class.  Must be first */
  sqlite3_stmt *pStmt;            /* For fetching database pages */

  int iPgno;                      /* Current page number */
  u8 *aPage;                      /* Buffer containing page */
  int nPage;                      /* Size of aPage[] in bytes */
  int nCell;                      /* Number of cells on aPage[] */
  int iCell;                      /* Current cell number */
  int bOnePage;                   /* True to stop after one page */
  sqlite3_int64 iRowid;

  /* Only for the sqlite_dbdata table */
  u8 *pRec;                       /* Buffer containing current record */
  int nRec;                       /* Size of pRec[] in bytes */
  int nField;                     /* Number of fields in pRec */
  int iField;                     /* Current field number */
  sqlite3_int64 iIntkey;          /* Integer key value */


};

/* The sqlite_dbdata table */
struct DbdataTable {
  sqlite3_vtab base;              /* Base class.  Must be first */
  sqlite3 *db;                    /* The database connection */
  int bPtr;                       /* True for sqlite3_dbptr table */
};

#define DBDATA_COLUMN_PGNO        0
#define DBDATA_COLUMN_CELL        1
#define DBDATA_COLUMN_FIELD       2
#define DBDATA_COLUMN_VALUE       3
#define DBDATA_COLUMN_SCHEMA      4

#define DBPTR_COLUMN_PGNO         0
#define DBPTR_COLUMN_CHILD        1
#define DBPTR_COLUMN_SCHEMA       2

#define DBDATA_SCHEMA             \
      "CREATE TABLE x("           \
      "  pgno INTEGER,"           \
      "  cell INTEGER,"           \
      "  field INTEGER,"          \
      "  value ANY,"              \
      "  schema TEXT HIDDEN"      \
      ")"

#define DBPTR_SCHEMA              \
      "CREATE TABLE x("           \
      "  pgno INTEGER,"           \
      "  child INTEGER,"          \
      "  schema TEXT HIDDEN"      \
      ")"

/*
** Connect to the sqlite_dbdata virtual table.
*/
static int dbdataConnect(
  sqlite3 *db,
  void *pAux,
  int argc, const char *const*argv,
  sqlite3_vtab **ppVtab,
  char **pzErr
){
  DbdataTable *pTab = 0;
  int rc = sqlite3_declare_vtab(db, pAux ? DBPTR_SCHEMA : DBDATA_SCHEMA);

  if( rc==SQLITE_OK ){
    pTab = (DbdataTable*)sqlite3_malloc64(sizeof(DbdataTable));
    if( pTab==0 ){
      rc = SQLITE_NOMEM;
    }else{
      memset(pTab, 0, sizeof(DbdataTable));
      pTab->db = db;
      pTab->bPtr = (pAux!=0);
    }
  }

  *ppVtab = (sqlite3_vtab*)pTab;
  return rc;
}

153
154
155
156
157
158
159

160
161
162

163
164
165
166
167
168
169
170
171
172
173
174
** the 0x01 bit in idxNum is set. If pgno=? is present, the 0x02 bit
** in idxNum is set.
**
** If both parameters are present, schema is in position 0 and pgno in
** position 1.
*/
static int dbdataBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){

  int i;
  int iSchema = -1;
  int iPgno = -1;


  for(i=0; i<pIdxInfo->nConstraint; i++){
    struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[i];
    if( p->op==SQLITE_INDEX_CONSTRAINT_EQ ){
      if( p->iColumn==DBDATA_COLUMN_SCHEMA ){
        if( p->usable==0 ) return SQLITE_CONSTRAINT;
        iSchema = i;
      }
      if( p->iColumn==DBDATA_COLUMN_PGNO && p->usable ){
        iPgno = i;
      }
    }







>



>




|







179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
** the 0x01 bit in idxNum is set. If pgno=? is present, the 0x02 bit
** in idxNum is set.
**
** If both parameters are present, schema is in position 0 and pgno in
** position 1.
*/
static int dbdataBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
  DbdataTable *pTab = (DbdataTable*)tab;
  int i;
  int iSchema = -1;
  int iPgno = -1;
  int colSchema = (pTab->bPtr ? DBPTR_COLUMN_SCHEMA : DBDATA_COLUMN_SCHEMA);

  for(i=0; i<pIdxInfo->nConstraint; i++){
    struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[i];
    if( p->op==SQLITE_INDEX_CONSTRAINT_EQ ){
      if( p->iColumn==colSchema ){
        if( p->usable==0 ) return SQLITE_CONSTRAINT;
        iSchema = i;
      }
      if( p->iColumn==DBDATA_COLUMN_PGNO && p->usable ){
        iPgno = i;
      }
    }
206
207
208
209
210
211
212

213
214
215
216
217
218
219

static void dbdataResetCursor(DbdataCursor *pCsr){
  sqlite3_finalize(pCsr->pStmt);
  pCsr->pStmt = 0;
  pCsr->iPgno = 1;
  pCsr->iCell = 0;
  pCsr->iField = 0;

}

/*
** Close a dbdata cursor.
*/
static int dbdataClose(sqlite3_vtab_cursor *pCursor){
  DbdataCursor *pCsr = (DbdataCursor*)pCursor;







>







234
235
236
237
238
239
240
241
242
243
244
245
246
247
248

static void dbdataResetCursor(DbdataCursor *pCsr){
  sqlite3_finalize(pCsr->pStmt);
  pCsr->pStmt = 0;
  pCsr->iPgno = 1;
  pCsr->iCell = 0;
  pCsr->iField = 0;
  pCsr->bOnePage = 0;
}

/*
** Close a dbdata cursor.
*/
static int dbdataClose(sqlite3_vtab_cursor *pCursor){
  DbdataCursor *pCsr = (DbdataCursor*)pCursor;
277
278
279
280
281
282
283
284

285
286
287
288

289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320

321
322
323



324
325

































326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409

410
411
412
413
414
415
416
  return 9;
}

/*
** Move a dbdata cursor to the next entry in the file.
*/
static int dbdataNext(sqlite3_vtab_cursor *pCursor){
  DbdataCursor *pCsr = (DbdataCursor *)pCursor;


  pCsr->iRowid++;
  while( 1 ){
    int rc;


    if( pCsr->aPage==0 ){
      rc = dbdataLoadPage(pCsr, pCsr->iPgno, &pCsr->aPage, &pCsr->nPage);
      if( rc!=SQLITE_OK ) return rc;
      pCsr->iCell = 0;
      pCsr->nCell = get_uint16(&pCsr->aPage[pCsr->iPgno==1 ? 103 : 3]);
    }

    /* If there is no record loaded, load it now. */
    if( pCsr->pRec==0 ){
      int iOff = (pCsr->iPgno==1 ? 100 : 0);
      int bHasRowid = 0;
      int nPointer = 0;
      sqlite3_int64 nPayload = 0;
      sqlite3_int64 nHdr = 0;
      int iHdr;
      int U, X;
      int nLocal;

      switch( pCsr->aPage[iOff] ){
        case 0x02:
          nPointer = 4;
          break;
        case 0x0a:
          break;
        case 0x0d:
          bHasRowid = 1;
          break;
        default:
          pCsr->iCell = pCsr->nCell;
          break;
      }

      if( pCsr->iCell>=pCsr->nCell ){
        sqlite3_free(pCsr->aPage);
        pCsr->aPage = 0;



        return SQLITE_OK;
      }


































      iOff += 8 + nPointer + pCsr->iCell*2;
      iOff = get_uint16(&pCsr->aPage[iOff]);

      /* For an interior node cell, skip past the child-page number */
      iOff += nPointer;

      /* Load the "byte of payload including overflow" field */
      iOff += dbdataGetVarint(&pCsr->aPage[iOff], &nPayload);

      /* If this is a leaf intkey cell, load the rowid */
      if( bHasRowid ){
        iOff += dbdataGetVarint(&pCsr->aPage[iOff], &pCsr->iIntkey);
      }

      /* Allocate space for payload */
      pCsr->pRec = (u8*)sqlite3_malloc64(nPayload);
      if( pCsr->pRec==0 ) return SQLITE_NOMEM;
      pCsr->nRec = nPayload;

      U = pCsr->nPage;
      if( bHasRowid ){
        X = U-35;
      }else{
        X = ((U-12)*64/255)-23;
      }
      if( nPayload<=X ){
        nLocal = nPayload;
      }else{
        int M, K;
        M = ((U-12)*32/255)-23;
        K = M+((nPayload-M)%(U-4));
        if( K<=X ){
          nLocal = K;
        }else{
          nLocal = M;
        }
      }

      /* Load the nLocal bytes of payload */
      memcpy(pCsr->pRec, &pCsr->aPage[iOff], nLocal);
      iOff += nLocal;

      /* Load content from overflow pages */
      if( nPayload>nLocal ){
        sqlite3_int64 nRem = nPayload - nLocal;
        u32 pgnoOvfl = get_uint32(&pCsr->aPage[iOff]);
        while( nRem>0 ){
          u8 *aOvfl = 0;
          int nOvfl = 0;
          int nCopy;
          rc = dbdataLoadPage(pCsr, pgnoOvfl, &aOvfl, &nOvfl);
          assert( rc!=SQLITE_OK || nOvfl==pCsr->nPage );
          if( rc!=SQLITE_OK ) return rc;

          nCopy = U-4;
          if( nCopy>nRem ) nCopy = nRem;
          memcpy(&pCsr->pRec[nPayload-nRem], &aOvfl[4], nCopy);
          nRem -= nCopy;

          sqlite3_free(aOvfl);
        }
      }

      /* Figure out how many fields in the record */
      pCsr->nField = 0;
      iHdr = dbdataGetVarint(pCsr->pRec, &nHdr);
      while( iHdr<nHdr ){
        sqlite3_int64 iDummy;
        iHdr += dbdataGetVarint(&pCsr->pRec[iHdr], &iDummy);
        pCsr->nField++;
      }

      pCsr->iField = (bHasRowid ? -2 : -1);
    }

    pCsr->iField++;
    if( pCsr->iField<pCsr->nField ) return SQLITE_OK;

    /* Advance to the next cell. The next iteration of the loop will load
    ** the record and so on. */
    sqlite3_free(pCsr->pRec);
    pCsr->pRec = 0;
    pCsr->iCell++;

  }

  assert( !"can't get here" );
  return SQLITE_OK;
}

/* We have reached EOF if previous sqlite3_step() returned







|
>




>



|
|
|


<
<
<
<
<
<
<
<
<
<
|
|
<
<
<
<
<
<
<
<
<
|
<

>



>
>
>


>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
>







306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327










328
329









330

331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
  return 9;
}

/*
** Move a dbdata cursor to the next entry in the file.
*/
static int dbdataNext(sqlite3_vtab_cursor *pCursor){
  DbdataCursor *pCsr = (DbdataCursor*)pCursor;
  DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;

  pCsr->iRowid++;
  while( 1 ){
    int rc;
    int iOff = (pCsr->iPgno==1 ? 100 : 0);

    if( pCsr->aPage==0 ){
      rc = dbdataLoadPage(pCsr, pCsr->iPgno, &pCsr->aPage, &pCsr->nPage);
      if( rc!=SQLITE_OK || pCsr->aPage==0 ) return rc;
      pCsr->iCell = pTab->bPtr ? -2 : 0;
      pCsr->nCell = get_uint16(&pCsr->aPage[iOff+3]);
    }











    if( pTab->bPtr ){
      if( pCsr->aPage[iOff]!=0x02 && pCsr->aPage[iOff]!=0x05 ){









        pCsr->iCell = pCsr->nCell;

      }
      pCsr->iCell++;
      if( pCsr->iCell>=pCsr->nCell ){
        sqlite3_free(pCsr->aPage);
        pCsr->aPage = 0;
        if( pCsr->bOnePage ) return SQLITE_OK;
        pCsr->iPgno++;
      }else{
        return SQLITE_OK;
      }
    }else{
      /* If there is no record loaded, load it now. */
      if( pCsr->pRec==0 ){
        int bHasRowid = 0;
        int nPointer = 0;
        sqlite3_int64 nPayload = 0;
        sqlite3_int64 nHdr = 0;
        int iHdr;
        int U, X;
        int nLocal;
  
        switch( pCsr->aPage[iOff] ){
          case 0x02:
            nPointer = 4;
            break;
          case 0x0a:
            break;
          case 0x0d:
            bHasRowid = 1;
            break;
          default:
            /* This is not a b-tree page with records on it. Continue. */
            pCsr->iCell = pCsr->nCell;
            break;
        }

        if( pCsr->iCell>=pCsr->nCell ){
          sqlite3_free(pCsr->aPage);
          pCsr->aPage = 0;
          if( pCsr->bOnePage ) return SQLITE_OK;
          pCsr->iPgno++;
          continue;
        }
  
        iOff += 8 + nPointer + pCsr->iCell*2;
        iOff = get_uint16(&pCsr->aPage[iOff]);
  
        /* For an interior node cell, skip past the child-page number */
        iOff += nPointer;
  
        /* Load the "byte of payload including overflow" field */
        iOff += dbdataGetVarint(&pCsr->aPage[iOff], &nPayload);
  
        /* If this is a leaf intkey cell, load the rowid */
        if( bHasRowid ){
          iOff += dbdataGetVarint(&pCsr->aPage[iOff], &pCsr->iIntkey);
        }
  
        /* Allocate space for payload */
        pCsr->pRec = (u8*)sqlite3_malloc64(nPayload);
        if( pCsr->pRec==0 ) return SQLITE_NOMEM;
        pCsr->nRec = nPayload;
  
        U = pCsr->nPage;
        if( bHasRowid ){
          X = U-35;
        }else{
          X = ((U-12)*64/255)-23;
        }
        if( nPayload<=X ){
          nLocal = nPayload;
        }else{
          int M, K;
          M = ((U-12)*32/255)-23;
          K = M+((nPayload-M)%(U-4));
          if( K<=X ){
            nLocal = K;
          }else{
            nLocal = M;
          }
        }
  
        /* Load the nLocal bytes of payload */
        memcpy(pCsr->pRec, &pCsr->aPage[iOff], nLocal);
        iOff += nLocal;
  
        /* Load content from overflow pages */
        if( nPayload>nLocal ){
          sqlite3_int64 nRem = nPayload - nLocal;
          u32 pgnoOvfl = get_uint32(&pCsr->aPage[iOff]);
          while( nRem>0 ){
            u8 *aOvfl = 0;
            int nOvfl = 0;
            int nCopy;
            rc = dbdataLoadPage(pCsr, pgnoOvfl, &aOvfl, &nOvfl);
            assert( rc!=SQLITE_OK || nOvfl==pCsr->nPage );
            if( rc!=SQLITE_OK ) return rc;
  
            nCopy = U-4;
            if( nCopy>nRem ) nCopy = nRem;
            memcpy(&pCsr->pRec[nPayload-nRem], &aOvfl[4], nCopy);
            nRem -= nCopy;
  
            sqlite3_free(aOvfl);
          }
        }
  
        /* Figure out how many fields in the record */
        pCsr->nField = 0;
        iHdr = dbdataGetVarint(pCsr->pRec, &nHdr);
        while( iHdr<nHdr ){
          sqlite3_int64 iDummy;
          iHdr += dbdataGetVarint(&pCsr->pRec[iHdr], &iDummy);
          pCsr->nField++;
        }
  
        pCsr->iField = (bHasRowid ? -2 : -1);
      }
  
      pCsr->iField++;
      if( pCsr->iField<pCsr->nField ) return SQLITE_OK;
  
      /* Advance to the next cell. The next iteration of the loop will load
      ** the record and so on. */
      sqlite3_free(pCsr->pRec);
      pCsr->pRec = 0;
      pCsr->iCell++;
    }
  }

  assert( !"can't get here" );
  return SQLITE_OK;
}

/* We have reached EOF if previous sqlite3_step() returned
436
437
438
439
440
441
442

443
444
445
446
447
448
449
  dbdataResetCursor(pCsr);
  assert( pCsr->iPgno==1 );
  if( idxNum & 0x01 ){
    zSchema = sqlite3_value_text(argv[0]);
  }
  if( idxNum & 0x02 ){
    pCsr->iPgno = sqlite3_value_int(argv[(idxNum & 0x01)]);

  }

  rc = sqlite3_prepare_v2(pTab->db, 
      "SELECT data FROM sqlite_dbpage(?) WHERE pgno=?", -1,
      &pCsr->pStmt, 0
  );
  if( rc==SQLITE_OK ){







>







485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
  dbdataResetCursor(pCsr);
  assert( pCsr->iPgno==1 );
  if( idxNum & 0x01 ){
    zSchema = sqlite3_value_text(argv[0]);
  }
  if( idxNum & 0x02 ){
    pCsr->iPgno = sqlite3_value_int(argv[(idxNum & 0x01)]);
    pCsr->bOnePage = 1;
  }

  rc = sqlite3_prepare_v2(pTab->db, 
      "SELECT data FROM sqlite_dbpage(?) WHERE pgno=?", -1,
      &pCsr->pStmt, 0
  );
  if( rc==SQLITE_OK ){
529
530
531
532
533
534
535



















536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563

564
565
566
567
568
569
570
/* Return a column for the sqlite_dbdata table */
static int dbdataColumn(
  sqlite3_vtab_cursor *pCursor, 
  sqlite3_context *ctx, 
  int i
){
  DbdataCursor *pCsr = (DbdataCursor*)pCursor;



















  switch( i ){
    case DBDATA_COLUMN_PGNO:
      sqlite3_result_int64(ctx, pCsr->iPgno);
      break;
    case DBDATA_COLUMN_CELL:
      sqlite3_result_int(ctx, pCsr->iCell);
      break;
    case DBDATA_COLUMN_FIELD:
      sqlite3_result_int(ctx, pCsr->iField);
      break;
    case DBDATA_COLUMN_VALUE: {
      if( pCsr->iField<0 ){
        sqlite3_result_int64(ctx, pCsr->iIntkey);
      }else{
        int iHdr;
        sqlite3_int64 iType;
        sqlite3_int64 iOff;
        int i;
        iHdr = dbdataGetVarint(pCsr->pRec, &iOff);
        for(i=0; i<pCsr->iField; i++){
          iHdr += dbdataGetVarint(&pCsr->pRec[iHdr], &iType);
          iOff += dbdataValueBytes(iType);
        }
        dbdataGetVarint(&pCsr->pRec[iHdr], &iType);

        dbdataValue(ctx, iType, &pCsr->pRec[iOff]);
      }
      break;

    }
  }
  return SQLITE_OK;
}

/* Return the ROWID for the sqlite_dbdata table */
static int dbdataRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|

|
|
|
>







579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
/* Return a column for the sqlite_dbdata table */
static int dbdataColumn(
  sqlite3_vtab_cursor *pCursor, 
  sqlite3_context *ctx, 
  int i
){
  DbdataCursor *pCsr = (DbdataCursor*)pCursor;
  DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
  if( pTab->bPtr ){
    switch( i ){
      case DBPTR_COLUMN_PGNO:
        sqlite3_result_int64(ctx, pCsr->iPgno);
        break;
      case DBPTR_COLUMN_CHILD: {
        int iOff = pCsr->iPgno==1 ? 100 : 0;
        if( pCsr->iCell<0 ){
          iOff += 8;
        }else{
          iOff += 12 + pCsr->iCell*2;
          iOff = get_uint16(&pCsr->aPage[iOff]);
        }
        sqlite3_result_int64(ctx, get_uint32(&pCsr->aPage[iOff]));
        break;
      }
    }
  }else{
    switch( i ){
      case DBDATA_COLUMN_PGNO:
        sqlite3_result_int64(ctx, pCsr->iPgno);
        break;
      case DBDATA_COLUMN_CELL:
        sqlite3_result_int(ctx, pCsr->iCell);
        break;
      case DBDATA_COLUMN_FIELD:
        sqlite3_result_int(ctx, pCsr->iField);
        break;
      case DBDATA_COLUMN_VALUE: {
        if( pCsr->iField<0 ){
          sqlite3_result_int64(ctx, pCsr->iIntkey);
        }else{
          int iHdr;
          sqlite3_int64 iType;
          sqlite3_int64 iOff;
          int i;
          iHdr = dbdataGetVarint(pCsr->pRec, &iOff);
          for(i=0; i<pCsr->iField; i++){
            iHdr += dbdataGetVarint(&pCsr->pRec[iHdr], &iType);
            iOff += dbdataValueBytes(iType);
          }
          dbdataGetVarint(&pCsr->pRec[iHdr], &iType);

          dbdataValue(ctx, iType, &pCsr->pRec[iOff]);
        }
        break;
      }
    }
  }
  return SQLITE_OK;
}

/* Return the ROWID for the sqlite_dbdata table */
static int dbdataRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
600
601
602
603
604
605
606

607




608
609
610
611
612
613
614
615
616
617
618
619
620
    0,                            /* xFindMethod */
    0,                            /* xRename */
    0,                            /* xSavepoint */
    0,                            /* xRelease */
    0,                            /* xRollbackTo */
    0                             /* xShadowName */
  };

  return sqlite3_create_module(db, "sqlite_dbdata", &dbdata_module, 0);




}

#ifdef _WIN32
__declspec(dllexport)
#endif
int sqlite3_dbdata_init(
  sqlite3 *db, 
  char **pzErrMsg, 
  const sqlite3_api_routines *pApi
){
  SQLITE_EXTENSION_INIT2(pApi);
  return sqlite3DbdataRegister(db);
}







>
|
>
>
>
>













670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
    0,                            /* xFindMethod */
    0,                            /* xRename */
    0,                            /* xSavepoint */
    0,                            /* xRelease */
    0,                            /* xRollbackTo */
    0                             /* xShadowName */
  };

  int rc = sqlite3_create_module(db, "sqlite_dbdata", &dbdata_module, 0);
  if( rc==SQLITE_OK ){
    rc = sqlite3_create_module(db, "sqlite_dbptr", &dbdata_module, (void*)1);
  }
  return rc;
}

#ifdef _WIN32
__declspec(dllexport)
#endif
int sqlite3_dbdata_init(
  sqlite3 *db, 
  char **pzErrMsg, 
  const sqlite3_api_routines *pApi
){
  SQLITE_EXTENSION_INIT2(pApi);
  return sqlite3DbdataRegister(db);
}
Changes to test/dbdata.test.
39
40
41
42
43
44
45


















46
47
48
49
50
51
































52
53
54
  2 0  0 'v' 
  2 0  1 'five' 
  2 1 -1 10 
  2 1  0 'x' 
  2 1  1 'ten'
}



















set big [string repeat big 2000]
do_execsql_test 1.2 {
  INSERT INTO t1 VALUES(NULL, $big);
  SELECT value FROM sqlite_dbdata WHERE pgno=2 AND cell=2 AND field=1;
} $big



































finish_test







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

|




>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
  2 0  0 'v' 
  2 0  1 'five' 
  2 1 -1 10 
  2 1  0 'x' 
  2 1  1 'ten'
}

breakpoint
do_execsql_test 1.2 {
  SELECT pgno, cell, field, quote(value) FROM sqlite_dbdata;
} {
  1 0 -1 1 
  1 0 0 'table' 
  1 0 1 'T1' 
  1 0 2 'T1' 
  1 0 3 2 
  1 0 4 {'CREATE TABLE T1(a, b)'}
  2 0 -1 5 
  2 0  0 'v' 
  2 0  1 'five' 
  2 1 -1 10 
  2 1  0 'x' 
  2 1  1 'ten'
}

set big [string repeat big 2000]
do_execsql_test 1.3 {
  INSERT INTO t1 VALUES(NULL, $big);
  SELECT value FROM sqlite_dbdata WHERE pgno=2 AND cell=2 AND field=1;
} $big

#-------------------------------------------------------------------------
reset_db
db enable_load_extension 1
db eval { SELECT load_extension('../dbdata') }

do_execsql_test 2.0 {
  CREATE TABLE t1(a);
  CREATE INDEX i1 ON t1(a);
  WITH s(i) AS (
    SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<10
  )
  INSERT INTO t1 SELECT randomblob(900) FROM s;
}

do_execsql_test 2.1 {
  SELECT * FROM sqlite_dbptr WHERE pgno=2;
} {
  2 25   2 6   2 7   2 9   2 11   2 13   2 15   2 17   2 19   2 21
}

do_execsql_test 2.2 {
  SELECT * FROM sqlite_dbptr WHERE pgno=3;
} {
  3 24   3 23
}

do_execsql_test 2.3 {
  SELECT * FROM sqlite_dbptr
} {
  2 25   2 6   2 7   2 9   2 11   2 13   2 15   2 17   2 19   2 21
  3 24   3 23
}


finish_test