4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give.
11 ******************************************************************************
13 ** This file contains an implementation of two eponymous virtual tables,
14 ** "sqlite_dbdata" and "sqlite_dbptr". Both modules require that the
15 ** "sqlite_dbpage" eponymous virtual table be available.
18 ** sqlite_dbdata is used to extract data directly from a database b-tree
19 ** page and its associated overflow pages, bypassing the b-tree layer.
20 ** The table schema is equivalent to:
22 ** CREATE TABLE sqlite_dbdata(
30 ** IMPORTANT: THE VIRTUAL TABLE SCHEMA ABOVE IS SUBJECT TO CHANGE. IN THE
31 ** FUTURE NEW NON-HIDDEN COLUMNS MAY BE ADDED BETWEEN "value" AND
34 ** Each page of the database is inspected. If it cannot be interpreted as
35 ** a b-tree page, or if it is a b-tree page containing 0 entries, the
36 ** sqlite_dbdata table contains no rows for that page. Otherwise, the
37 ** table contains one row for each field in the record associated with
38 ** each cell on the page. For intkey b-trees, the key value is stored in
41 ** For example, for the database:
43 ** CREATE TABLE t1(a, b); -- root page is page 2
44 ** INSERT INTO t1(rowid, a, b) VALUES(5, 'v', 'five');
45 ** INSERT INTO t1(rowid, a, b) VALUES(10, 'x', 'ten');
47 ** the sqlite_dbdata table contains, as well as from entries related to
48 ** page 1, content equivalent to:
50 ** INSERT INTO sqlite_dbdata(pgno, cell, field, value) VALUES
58 ** If database corruption is encountered, this module does not report an
59 ** error. Instead, it attempts to extract as much data as possible and
60 ** ignores the corruption.
63 ** The sqlite_dbptr table has the following schema:
65 ** CREATE TABLE sqlite_dbptr(
71 ** It contains one entry for each b-tree pointer between a parent and
72 ** child page in the database.
75 #if !defined(SQLITEINT_H)
76 #include "sqlite3ext.h"
78 typedef unsigned char u8
;
79 typedef unsigned int u32
;
82 SQLITE_EXTENSION_INIT1
86 #ifndef SQLITE_OMIT_VIRTUALTABLE
88 #define DBDATA_PADDING_BYTES 100
90 typedef struct DbdataTable DbdataTable
;
91 typedef struct DbdataCursor DbdataCursor
;
95 sqlite3_vtab_cursor base
; /* Base class. Must be first */
96 sqlite3_stmt
*pStmt
; /* For fetching database pages */
98 int iPgno
; /* Current page number */
99 u8
*aPage
; /* Buffer containing page */
100 int nPage
; /* Size of aPage[] in bytes */
101 int nCell
; /* Number of cells on aPage[] */
102 int iCell
; /* Current cell number */
103 int bOnePage
; /* True to stop after one page */
105 sqlite3_int64 iRowid
;
107 /* Only for the sqlite_dbdata table */
108 u8
*pRec
; /* Buffer containing current record */
109 sqlite3_int64 nRec
; /* Size of pRec[] in bytes */
110 sqlite3_int64 nHdr
; /* Size of header in bytes */
111 int iField
; /* Current field number */
114 u32 enc
; /* Text encoding */
116 sqlite3_int64 iIntkey
; /* Integer key value */
121 sqlite3_vtab base
; /* Base class. Must be first */
122 sqlite3
*db
; /* The database connection */
123 sqlite3_stmt
*pStmt
; /* For fetching database pages */
124 int bPtr
; /* True for sqlite3_dbptr table */
127 /* Column and schema definitions for sqlite_dbdata */
128 #define DBDATA_COLUMN_PGNO 0
129 #define DBDATA_COLUMN_CELL 1
130 #define DBDATA_COLUMN_FIELD 2
131 #define DBDATA_COLUMN_VALUE 3
132 #define DBDATA_COLUMN_SCHEMA 4
133 #define DBDATA_SCHEMA \
139 " schema TEXT HIDDEN" \
142 /* Column and schema definitions for sqlite_dbptr */
143 #define DBPTR_COLUMN_PGNO 0
144 #define DBPTR_COLUMN_CHILD 1
145 #define DBPTR_COLUMN_SCHEMA 2
146 #define DBPTR_SCHEMA \
150 " schema TEXT HIDDEN" \
154 ** Connect to an sqlite_dbdata (pAux==0) or sqlite_dbptr (pAux!=0) virtual
157 static int dbdataConnect(
160 int argc
, const char *const*argv
,
161 sqlite3_vtab
**ppVtab
,
164 DbdataTable
*pTab
= 0;
165 int rc
= sqlite3_declare_vtab(db
, pAux
? DBPTR_SCHEMA
: DBDATA_SCHEMA
);
171 pTab
= (DbdataTable
*)sqlite3_malloc64(sizeof(DbdataTable
));
175 memset(pTab
, 0, sizeof(DbdataTable
));
177 pTab
->bPtr
= (pAux
!=0);
181 *ppVtab
= (sqlite3_vtab
*)pTab
;
186 ** Disconnect from or destroy a sqlite_dbdata or sqlite_dbptr virtual table.
188 static int dbdataDisconnect(sqlite3_vtab
*pVtab
){
189 DbdataTable
*pTab
= (DbdataTable
*)pVtab
;
191 sqlite3_finalize(pTab
->pStmt
);
198 ** This function interprets two types of constraints:
203 ** If neither are present, idxNum is set to 0. If schema=? is present,
204 ** the 0x01 bit in idxNum is set. If pgno=? is present, the 0x02 bit
207 ** If both parameters are present, schema is in position 0 and pgno in
210 static int dbdataBestIndex(sqlite3_vtab
*tab
, sqlite3_index_info
*pIdx
){
211 DbdataTable
*pTab
= (DbdataTable
*)tab
;
215 int colSchema
= (pTab
->bPtr
? DBPTR_COLUMN_SCHEMA
: DBDATA_COLUMN_SCHEMA
);
217 for(i
=0; i
<pIdx
->nConstraint
; i
++){
218 struct sqlite3_index_constraint
*p
= &pIdx
->aConstraint
[i
];
219 if( p
->op
==SQLITE_INDEX_CONSTRAINT_EQ
){
220 if( p
->iColumn
==colSchema
){
221 if( p
->usable
==0 ) return SQLITE_CONSTRAINT
;
224 if( p
->iColumn
==DBDATA_COLUMN_PGNO
&& p
->usable
){
231 pIdx
->aConstraintUsage
[iSchema
].argvIndex
= 1;
232 pIdx
->aConstraintUsage
[iSchema
].omit
= 1;
235 pIdx
->aConstraintUsage
[iPgno
].argvIndex
= 1 + (iSchema
>=0);
236 pIdx
->aConstraintUsage
[iPgno
].omit
= 1;
237 pIdx
->estimatedCost
= 100;
238 pIdx
->estimatedRows
= 50;
240 if( pTab
->bPtr
==0 && pIdx
->nOrderBy
&& pIdx
->aOrderBy
[0].desc
==0 ){
241 int iCol
= pIdx
->aOrderBy
[0].iColumn
;
242 if( pIdx
->nOrderBy
==1 ){
243 pIdx
->orderByConsumed
= (iCol
==0 || iCol
==1);
244 }else if( pIdx
->nOrderBy
==2 && pIdx
->aOrderBy
[1].desc
==0 && iCol
==0 ){
245 pIdx
->orderByConsumed
= (pIdx
->aOrderBy
[1].iColumn
==1);
250 pIdx
->estimatedCost
= 100000000;
251 pIdx
->estimatedRows
= 1000000000;
253 pIdx
->idxNum
= (iSchema
>=0 ? 0x01 : 0x00) | (iPgno
>=0 ? 0x02 : 0x00);
258 ** Open a new sqlite_dbdata or sqlite_dbptr cursor.
260 static int dbdataOpen(sqlite3_vtab
*pVTab
, sqlite3_vtab_cursor
**ppCursor
){
263 pCsr
= (DbdataCursor
*)sqlite3_malloc64(sizeof(DbdataCursor
));
267 memset(pCsr
, 0, sizeof(DbdataCursor
));
268 pCsr
->base
.pVtab
= pVTab
;
271 *ppCursor
= (sqlite3_vtab_cursor
*)pCsr
;
276 ** Restore a cursor object to the state it was in when first allocated
279 static void dbdataResetCursor(DbdataCursor
*pCsr
){
280 DbdataTable
*pTab
= (DbdataTable
*)(pCsr
->base
.pVtab
);
281 if( pTab
->pStmt
==0 ){
282 pTab
->pStmt
= pCsr
->pStmt
;
284 sqlite3_finalize(pCsr
->pStmt
);
291 sqlite3_free(pCsr
->aPage
);
292 sqlite3_free(pCsr
->pRec
);
298 ** Close an sqlite_dbdata or sqlite_dbptr cursor.
300 static int dbdataClose(sqlite3_vtab_cursor
*pCursor
){
301 DbdataCursor
*pCsr
= (DbdataCursor
*)pCursor
;
302 dbdataResetCursor(pCsr
);
308 ** Utility methods to decode 16 and 32-bit big-endian unsigned integers.
310 static u32
get_uint16(unsigned char *a
){
311 return (a
[0]<<8)|a
[1];
313 static u32
get_uint32(unsigned char *a
){
314 return ((u32
)a
[0]<<24)
321 ** Load page pgno from the database via the sqlite_dbpage virtual table.
322 ** If successful, set (*ppPage) to point to a buffer containing the page
323 ** data, (*pnPage) to the size of that buffer in bytes and return
324 ** SQLITE_OK. In this case it is the responsibility of the caller to
325 ** eventually free the buffer using sqlite3_free().
327 ** Or, if an error occurs, set both (*ppPage) and (*pnPage) to 0 and
328 ** return an SQLite error code.
330 static int dbdataLoadPage(
331 DbdataCursor
*pCsr
, /* Cursor object */
332 u32 pgno
, /* Page number of page to load */
333 u8
**ppPage
, /* OUT: pointer to page buffer */
334 int *pnPage
/* OUT: Size of (*ppPage) in bytes */
338 sqlite3_stmt
*pStmt
= pCsr
->pStmt
;
343 sqlite3_bind_int64(pStmt
, 2, pgno
);
344 if( SQLITE_ROW
==sqlite3_step(pStmt
) ){
345 int nCopy
= sqlite3_column_bytes(pStmt
, 0);
348 pPage
= (u8
*)sqlite3_malloc64(nCopy
+ DBDATA_PADDING_BYTES
);
352 const u8
*pCopy
= sqlite3_column_blob(pStmt
, 0);
353 memcpy(pPage
, pCopy
, nCopy
);
354 memset(&pPage
[nCopy
], 0, DBDATA_PADDING_BYTES
);
360 rc2
= sqlite3_reset(pStmt
);
361 if( rc
==SQLITE_OK
) rc
= rc2
;
368 ** Read a varint. Put the value in *pVal and return the number of bytes.
370 static int dbdataGetVarint(const u8
*z
, sqlite3_int64
*pVal
){
371 sqlite3_uint64 u
= 0;
374 u
= (u
<<7) + (z
[i
]&0x7f);
375 if( (z
[i
]&0x80)==0 ){ *pVal
= (sqlite3_int64
)u
; return i
+1; }
377 u
= (u
<<8) + (z
[i
]&0xff);
378 *pVal
= (sqlite3_int64
)u
;
383 ** Like dbdataGetVarint(), but set the output to 0 if it is less than 0
384 ** or greater than 0xFFFFFFFF. This can be used for all varints in an
385 ** SQLite database except for key values in intkey tables.
387 static int dbdataGetVarintU32(const u8
*z
, sqlite3_int64
*pVal
){
389 int nRet
= dbdataGetVarint(z
, &val
);
390 if( val
<0 || val
>0xFFFFFFFF ) val
= 0;
396 ** Return the number of bytes of space used by an SQLite value of type
399 static int dbdataValueBytes(int eType
){
401 case 0: case 8: case 9:
419 return ((eType
-12) / 2);
426 ** Load a value of type eType from buffer pData and use it to set the
427 ** result of context object pCtx.
429 static void dbdataValue(
430 sqlite3_context
*pCtx
,
436 if( eType
>=0 && dbdataValueBytes(eType
)<=nData
){
441 sqlite3_result_null(pCtx
);
445 sqlite3_result_int(pCtx
, 0);
448 sqlite3_result_int(pCtx
, 1);
451 case 1: case 2: case 3: case 4: case 5: case 6: case 7: {
452 sqlite3_uint64 v
= (signed char)pData
[0];
456 case 6: v
= (v
<<16) + (pData
[0]<<8) + pData
[1]; pData
+= 2;
457 case 5: v
= (v
<<16) + (pData
[0]<<8) + pData
[1]; pData
+= 2;
458 case 4: v
= (v
<<8) + pData
[0]; pData
++;
459 case 3: v
= (v
<<8) + pData
[0]; pData
++;
460 case 2: v
= (v
<<8) + pData
[0]; pData
++;
465 memcpy(&r
, &v
, sizeof(r
));
466 sqlite3_result_double(pCtx
, r
);
468 sqlite3_result_int64(pCtx
, (sqlite3_int64
)v
);
474 int n
= ((eType
-12) / 2);
477 #ifndef SQLITE_OMIT_UTF16
479 sqlite3_result_text16be(pCtx
, (void*)pData
, n
, SQLITE_TRANSIENT
);
482 sqlite3_result_text16le(pCtx
, (void*)pData
, n
, SQLITE_TRANSIENT
);
486 sqlite3_result_text(pCtx
, (char*)pData
, n
, SQLITE_TRANSIENT
);
490 sqlite3_result_blob(pCtx
, pData
, n
, SQLITE_TRANSIENT
);
498 ** Move an sqlite_dbdata or sqlite_dbptr cursor to the next entry.
500 static int dbdataNext(sqlite3_vtab_cursor
*pCursor
){
501 DbdataCursor
*pCsr
= (DbdataCursor
*)pCursor
;
502 DbdataTable
*pTab
= (DbdataTable
*)pCursor
->pVtab
;
507 int iOff
= (pCsr
->iPgno
==1 ? 100 : 0);
510 if( pCsr
->aPage
==0 ){
512 if( pCsr
->bOnePage
==0 && pCsr
->iPgno
>pCsr
->szDb
) return SQLITE_OK
;
513 rc
= dbdataLoadPage(pCsr
, pCsr
->iPgno
, &pCsr
->aPage
, &pCsr
->nPage
);
514 if( rc
!=SQLITE_OK
) return rc
;
515 if( pCsr
->aPage
&& pCsr
->nPage
>=256 ) break;
516 sqlite3_free(pCsr
->aPage
);
518 if( pCsr
->bOnePage
) return SQLITE_OK
;
522 assert( iOff
+3+2<=pCsr
->nPage
);
523 pCsr
->iCell
= pTab
->bPtr
? -2 : 0;
524 pCsr
->nCell
= get_uint16(&pCsr
->aPage
[iOff
+3]);
528 if( pCsr
->aPage
[iOff
]!=0x02 && pCsr
->aPage
[iOff
]!=0x05 ){
529 pCsr
->iCell
= pCsr
->nCell
;
532 if( pCsr
->iCell
>=pCsr
->nCell
){
533 sqlite3_free(pCsr
->aPage
);
535 if( pCsr
->bOnePage
) return SQLITE_OK
;
541 /* If there is no record loaded, load it now. */
545 sqlite3_int64 nPayload
= 0;
546 sqlite3_int64 nHdr
= 0;
551 switch( pCsr
->aPage
[iOff
] ){
561 /* This is not a b-tree page with records on it. Continue. */
562 pCsr
->iCell
= pCsr
->nCell
;
566 if( pCsr
->iCell
>=pCsr
->nCell
){
570 iOff
+= 8 + nPointer
+ pCsr
->iCell
*2;
571 if( iOff
>pCsr
->nPage
){
574 iOff
= get_uint16(&pCsr
->aPage
[iOff
]);
577 /* For an interior node cell, skip past the child-page number */
580 /* Load the "byte of payload including overflow" field */
581 if( bNextPage
|| iOff
>pCsr
->nPage
){
584 iOff
+= dbdataGetVarintU32(&pCsr
->aPage
[iOff
], &nPayload
);
587 /* If this is a leaf intkey cell, load the rowid */
588 if( bHasRowid
&& !bNextPage
&& iOff
<pCsr
->nPage
){
589 iOff
+= dbdataGetVarint(&pCsr
->aPage
[iOff
], &pCsr
->iIntkey
);
592 /* Figure out how much data to read from the local page */
597 X
= ((U
-12)*64/255)-23;
603 M
= ((U
-12)*32/255)-23;
604 K
= M
+((nPayload
-M
)%(U
-4));
612 if( bNextPage
|| nLocal
+iOff
>pCsr
->nPage
){
616 /* Allocate space for payload. And a bit more to catch small buffer
617 ** overruns caused by attempting to read a varint or similar from
618 ** near the end of a corrupt record. */
619 pCsr
->pRec
= (u8
*)sqlite3_malloc64(nPayload
+DBDATA_PADDING_BYTES
);
620 if( pCsr
->pRec
==0 ) return SQLITE_NOMEM
;
621 memset(pCsr
->pRec
, 0, nPayload
+DBDATA_PADDING_BYTES
);
622 pCsr
->nRec
= nPayload
;
624 /* Load the nLocal bytes of payload */
625 memcpy(pCsr
->pRec
, &pCsr
->aPage
[iOff
], nLocal
);
628 /* Load content from overflow pages */
629 if( nPayload
>nLocal
){
630 sqlite3_int64 nRem
= nPayload
- nLocal
;
631 u32 pgnoOvfl
= get_uint32(&pCsr
->aPage
[iOff
]);
636 rc
= dbdataLoadPage(pCsr
, pgnoOvfl
, &aOvfl
, &nOvfl
);
637 assert( rc
!=SQLITE_OK
|| aOvfl
==0 || nOvfl
==pCsr
->nPage
);
638 if( rc
!=SQLITE_OK
) return rc
;
639 if( aOvfl
==0 ) break;
642 if( nCopy
>nRem
) nCopy
= nRem
;
643 memcpy(&pCsr
->pRec
[nPayload
-nRem
], &aOvfl
[4], nCopy
);
646 pgnoOvfl
= get_uint32(aOvfl
);
651 iHdr
= dbdataGetVarintU32(pCsr
->pRec
, &nHdr
);
652 if( nHdr
>nPayload
) nHdr
= 0;
654 pCsr
->pHdrPtr
= &pCsr
->pRec
[iHdr
];
655 pCsr
->pPtr
= &pCsr
->pRec
[pCsr
->nHdr
];
656 pCsr
->iField
= (bHasRowid
? -1 : 0);
661 if( pCsr
->iField
>0 ){
663 if( pCsr
->pHdrPtr
>&pCsr
->pRec
[pCsr
->nRec
] ){
666 pCsr
->pHdrPtr
+= dbdataGetVarintU32(pCsr
->pHdrPtr
, &iType
);
667 pCsr
->pPtr
+= dbdataValueBytes(iType
);
673 sqlite3_free(pCsr
->aPage
);
674 sqlite3_free(pCsr
->pRec
);
677 if( pCsr
->bOnePage
) return SQLITE_OK
;
680 if( pCsr
->iField
<0 || pCsr
->pHdrPtr
<&pCsr
->pRec
[pCsr
->nHdr
] ){
684 /* Advance to the next cell. The next iteration of the loop will load
685 ** the record and so on. */
686 sqlite3_free(pCsr
->pRec
);
693 assert( !"can't get here" );
698 ** Return true if the cursor is at EOF.
700 static int dbdataEof(sqlite3_vtab_cursor
*pCursor
){
701 DbdataCursor
*pCsr
= (DbdataCursor
*)pCursor
;
702 return pCsr
->aPage
==0;
706 ** Return true if nul-terminated string zSchema ends in "()". Or false
709 static int dbdataIsFunction(const char *zSchema
){
710 size_t n
= strlen(zSchema
);
711 if( n
>2 && zSchema
[n
-2]=='(' && zSchema
[n
-1]==')' ){
718 ** Determine the size in pages of database zSchema (where zSchema is
719 ** "main", "temp" or the name of an attached database) and set
720 ** pCsr->szDb accordingly. If successful, return SQLITE_OK. Otherwise,
721 ** an SQLite error code.
723 static int dbdataDbsize(DbdataCursor
*pCsr
, const char *zSchema
){
724 DbdataTable
*pTab
= (DbdataTable
*)pCsr
->base
.pVtab
;
728 sqlite3_stmt
*pStmt
= 0;
730 if( (nFunc
= dbdataIsFunction(zSchema
))>0 ){
731 zSql
= sqlite3_mprintf("SELECT %.*s(0)", nFunc
, zSchema
);
733 zSql
= sqlite3_mprintf("PRAGMA %Q.page_count", zSchema
);
735 if( zSql
==0 ) return SQLITE_NOMEM
;
737 rc
= sqlite3_prepare_v2(pTab
->db
, zSql
, -1, &pStmt
, 0);
739 if( rc
==SQLITE_OK
&& sqlite3_step(pStmt
)==SQLITE_ROW
){
740 pCsr
->szDb
= sqlite3_column_int(pStmt
, 0);
742 rc2
= sqlite3_finalize(pStmt
);
743 if( rc
==SQLITE_OK
) rc
= rc2
;
748 ** Attempt to figure out the encoding of the database by retrieving page 1
749 ** and inspecting the header field. If successful, set the pCsr->enc variable
750 ** and return SQLITE_OK. Otherwise, return an SQLite error code.
752 static int dbdataGetEncoding(DbdataCursor
*pCsr
){
756 rc
= dbdataLoadPage(pCsr
, 1, &aPg1
, &nPg1
);
757 if( rc
==SQLITE_OK
&& nPg1
>=(56+4) ){
758 pCsr
->enc
= get_uint32(&aPg1
[56]);
766 ** xFilter method for sqlite_dbdata and sqlite_dbptr.
768 static int dbdataFilter(
769 sqlite3_vtab_cursor
*pCursor
,
770 int idxNum
, const char *idxStr
,
771 int argc
, sqlite3_value
**argv
773 DbdataCursor
*pCsr
= (DbdataCursor
*)pCursor
;
774 DbdataTable
*pTab
= (DbdataTable
*)pCursor
->pVtab
;
776 const char *zSchema
= "main";
780 dbdataResetCursor(pCsr
);
781 assert( pCsr
->iPgno
==1 );
783 zSchema
= (const char*)sqlite3_value_text(argv
[0]);
784 if( zSchema
==0 ) zSchema
= "";
787 pCsr
->iPgno
= sqlite3_value_int(argv
[(idxNum
& 0x01)]);
790 rc
= dbdataDbsize(pCsr
, zSchema
);
796 pCsr
->pStmt
= pTab
->pStmt
;
798 }else if( (nFunc
= dbdataIsFunction(zSchema
))>0 ){
799 char *zSql
= sqlite3_mprintf("SELECT %.*s(?2)", nFunc
, zSchema
);
803 rc
= sqlite3_prepare_v2(pTab
->db
, zSql
, -1, &pCsr
->pStmt
, 0);
807 rc
= sqlite3_prepare_v2(pTab
->db
,
808 "SELECT data FROM sqlite_dbpage(?) WHERE pgno=?", -1,
814 rc
= sqlite3_bind_text(pCsr
->pStmt
, 1, zSchema
, -1, SQLITE_TRANSIENT
);
816 pTab
->base
.zErrMsg
= sqlite3_mprintf("%s", sqlite3_errmsg(pTab
->db
));
819 /* Try to determine the encoding of the db by inspecting the header
820 ** field on page 1. */
822 rc
= dbdataGetEncoding(pCsr
);
826 rc
= dbdataNext(pCursor
);
832 ** Return a column for the sqlite_dbdata or sqlite_dbptr table.
834 static int dbdataColumn(
835 sqlite3_vtab_cursor
*pCursor
,
836 sqlite3_context
*ctx
,
839 DbdataCursor
*pCsr
= (DbdataCursor
*)pCursor
;
840 DbdataTable
*pTab
= (DbdataTable
*)pCursor
->pVtab
;
843 case DBPTR_COLUMN_PGNO
:
844 sqlite3_result_int64(ctx
, pCsr
->iPgno
);
846 case DBPTR_COLUMN_CHILD
: {
847 int iOff
= pCsr
->iPgno
==1 ? 100 : 0;
851 iOff
+= 12 + pCsr
->iCell
*2;
852 if( iOff
>pCsr
->nPage
) return SQLITE_OK
;
853 iOff
= get_uint16(&pCsr
->aPage
[iOff
]);
855 if( iOff
<=pCsr
->nPage
){
856 sqlite3_result_int64(ctx
, get_uint32(&pCsr
->aPage
[iOff
]));
863 case DBDATA_COLUMN_PGNO
:
864 sqlite3_result_int64(ctx
, pCsr
->iPgno
);
866 case DBDATA_COLUMN_CELL
:
867 sqlite3_result_int(ctx
, pCsr
->iCell
);
869 case DBDATA_COLUMN_FIELD
:
870 sqlite3_result_int(ctx
, pCsr
->iField
);
872 case DBDATA_COLUMN_VALUE
: {
873 if( pCsr
->iField
<0 ){
874 sqlite3_result_int64(ctx
, pCsr
->iIntkey
);
875 }else if( &pCsr
->pRec
[pCsr
->nRec
] >= pCsr
->pPtr
){
877 dbdataGetVarintU32(pCsr
->pHdrPtr
, &iType
);
879 ctx
, pCsr
->enc
, iType
, pCsr
->pPtr
,
880 &pCsr
->pRec
[pCsr
->nRec
] - pCsr
->pPtr
891 ** Return the rowid for an sqlite_dbdata or sqlite_dptr table.
893 static int dbdataRowid(sqlite3_vtab_cursor
*pCursor
, sqlite_int64
*pRowid
){
894 DbdataCursor
*pCsr
= (DbdataCursor
*)pCursor
;
895 *pRowid
= pCsr
->iRowid
;
901 ** Invoke this routine to register the "sqlite_dbdata" virtual table module
903 static int sqlite3DbdataRegister(sqlite3
*db
){
904 static sqlite3_module dbdata_module
= {
907 dbdataConnect
, /* xConnect */
908 dbdataBestIndex
, /* xBestIndex */
909 dbdataDisconnect
, /* xDisconnect */
911 dbdataOpen
, /* xOpen - open a cursor */
912 dbdataClose
, /* xClose - close a cursor */
913 dbdataFilter
, /* xFilter - configure scan constraints */
914 dbdataNext
, /* xNext - advance a cursor */
915 dbdataEof
, /* xEof - check for end of scan */
916 dbdataColumn
, /* xColumn - read data */
917 dbdataRowid
, /* xRowid - read data */
931 int rc
= sqlite3_create_module(db
, "sqlite_dbdata", &dbdata_module
, 0);
933 rc
= sqlite3_create_module(db
, "sqlite_dbptr", &dbdata_module
, (void*)1);
939 __declspec(dllexport
)
941 int sqlite3_dbdata_init(
944 const sqlite3_api_routines
*pApi
946 SQLITE_EXTENSION_INIT2(pApi
);
948 return sqlite3DbdataRegister(db
);
951 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */