mirror of
https://github.com/sqlite/sqlite.git
synced 2025-10-28 19:36:04 +03:00
Add implementations for opcodes required for linear scans of virtual tables. (CVS 3223)
FossilOrigin-Name: 1f20e1832b38c76d2b0dde5fd720670c2ad0438b
This commit is contained in:
@@ -11,7 +11,7 @@
|
||||
*************************************************************************
|
||||
** Internal interface definitions for SQLite.
|
||||
**
|
||||
** @(#) $Id: sqliteInt.h,v 1.502 2006/06/12 21:59:14 drh Exp $
|
||||
** @(#) $Id: sqliteInt.h,v 1.503 2006/06/13 10:24:43 danielk1977 Exp $
|
||||
*/
|
||||
#ifndef _SQLITEINT_H_
|
||||
#define _SQLITEINT_H_
|
||||
@@ -709,7 +709,6 @@ struct Table {
|
||||
sqlite3_vtab *pVtab; /* Pointer to the module instance */
|
||||
int nModuleArg; /* Number of arguments to the module */
|
||||
char **azModuleArg; /* Text of all module args. [0] is module name */
|
||||
u8 needCreate; /* Need to call pMod->xCreate() */
|
||||
u8 isVirtual; /* True if this is a virtual table */
|
||||
#endif
|
||||
Schema *pSchema;
|
||||
|
||||
199
src/test8.c
199
src/test8.c
@@ -13,7 +13,7 @@
|
||||
** is not included in the SQLite library. It is used for automated
|
||||
** testing of the SQLite library.
|
||||
**
|
||||
** $Id: test8.c,v 1.7 2006/06/13 04:11:44 danielk1977 Exp $
|
||||
** $Id: test8.c,v 1.8 2006/06/13 10:24:43 danielk1977 Exp $
|
||||
*/
|
||||
#include "sqliteInt.h"
|
||||
#include "tcl.h"
|
||||
@@ -21,6 +21,24 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
typedef struct echo_vtab echo_vtab;
|
||||
typedef struct echo_cursor echo_cursor;
|
||||
|
||||
/* An echo vtab object */
|
||||
struct echo_vtab {
|
||||
sqlite3_vtab base;
|
||||
Tcl_Interp *interp;
|
||||
sqlite3 *db;
|
||||
char *zStmt;
|
||||
};
|
||||
|
||||
/* An echo cursor object */
|
||||
struct echo_cursor {
|
||||
sqlite3_vtab_cursor base;
|
||||
sqlite3_stmt *pStmt;
|
||||
int errcode; /* Error code */
|
||||
};
|
||||
|
||||
/*
|
||||
** Global Tcl variable $echo_module is a list. This routine appends
|
||||
** the string element zArg to that list in interpreter interp.
|
||||
@@ -47,7 +65,12 @@ static void appendToEchoModule(Tcl_Interp *interp, const char *zArg){
|
||||
** Hence, the virtual table should have exactly the same column names and
|
||||
** types as the real table.
|
||||
*/
|
||||
static int echoDeclareVtab(sqlite3 *db, int argc, char **argv){
|
||||
static int echoDeclareVtab(
|
||||
echo_vtab *pVtab,
|
||||
sqlite3 *db,
|
||||
int argc,
|
||||
char **argv
|
||||
){
|
||||
int rc = SQLITE_OK;
|
||||
|
||||
if( argc==2 ){
|
||||
@@ -65,17 +88,34 @@ static int echoDeclareVtab(sqlite3 *db, int argc, char **argv){
|
||||
rc = SQLITE_ERROR;
|
||||
}
|
||||
sqlite3_finalize(pStmt);
|
||||
pVtab->zStmt = sqlite3MPrintf("SELECT rowid, * FROM %s ", argv[1]);
|
||||
}
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
/* An echo vtab object */
|
||||
typedef struct echo_vtab echo_vtab;
|
||||
struct echo_vtab {
|
||||
sqlite3_vtab base;
|
||||
Tcl_Interp *interp;
|
||||
};
|
||||
static int echoConstructor(
|
||||
sqlite3 *db,
|
||||
const sqlite3_module *pModule,
|
||||
int argc, char **argv,
|
||||
sqlite3_vtab **ppVtab
|
||||
){
|
||||
int i;
|
||||
echo_vtab *pVtab;
|
||||
|
||||
pVtab = sqliteMalloc( sizeof(*pVtab) );
|
||||
|
||||
*ppVtab = &pVtab->base;
|
||||
pVtab->base.pModule = pModule;
|
||||
pVtab->interp = pModule->pAux;
|
||||
pVtab->db = db;
|
||||
for(i=0; i<argc; i++){
|
||||
appendToEchoModule(pVtab->interp, argv[i]);
|
||||
}
|
||||
|
||||
echoDeclareVtab(pVtab, db, argc, argv);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Methods for the echo module */
|
||||
static int echoCreate(
|
||||
@@ -84,20 +124,8 @@ static int echoCreate(
|
||||
int argc, char **argv,
|
||||
sqlite3_vtab **ppVtab
|
||||
){
|
||||
int i;
|
||||
echo_vtab *pVtab;
|
||||
|
||||
pVtab = sqliteMalloc( sizeof(*pVtab) );
|
||||
*ppVtab = &pVtab->base;
|
||||
pVtab->base.pModule = pModule;
|
||||
pVtab->interp = pModule->pAux;
|
||||
appendToEchoModule(pVtab->interp, "xCreate");
|
||||
for(i=0; i<argc; i++){
|
||||
appendToEchoModule(pVtab->interp, argv[i]);
|
||||
}
|
||||
|
||||
echoDeclareVtab(db, argc, argv);
|
||||
return 0;
|
||||
appendToEchoModule((Tcl_Interp *)(pModule->pAux), "xCreate");
|
||||
return echoConstructor(db, pModule, argc, argv, ppVtab);
|
||||
}
|
||||
static int echoConnect(
|
||||
sqlite3 *db,
|
||||
@@ -105,35 +133,122 @@ static int echoConnect(
|
||||
int argc, char **argv,
|
||||
sqlite3_vtab **ppVtab
|
||||
){
|
||||
int i;
|
||||
Tcl_Interp *interp = pModule->pAux;
|
||||
echo_vtab *pVtab;
|
||||
|
||||
pVtab = sqliteMalloc( sizeof(*pVtab) );
|
||||
*ppVtab = &pVtab->base;
|
||||
pVtab->base.pModule = pModule;
|
||||
pVtab->interp = pModule->pAux;
|
||||
appendToEchoModule(pVtab->interp, "xConnect");
|
||||
for(i=0; i<argc; i++){
|
||||
appendToEchoModule(pVtab->interp, argv[i]);
|
||||
}
|
||||
|
||||
echoDeclareVtab(db, argc, argv);
|
||||
return 0;
|
||||
appendToEchoModule((Tcl_Interp *)(pModule->pAux), "xConnect");
|
||||
return echoConstructor(db, pModule, argc, argv, ppVtab);
|
||||
}
|
||||
|
||||
static int echoDisconnect(sqlite3_vtab *pVtab){
|
||||
echo_vtab *p = (echo_vtab*)pVtab;
|
||||
appendToEchoModule(p->interp, "xDisconnect");
|
||||
sqliteFree(pVtab);
|
||||
sqliteFree(p->zStmt);
|
||||
sqliteFree(p);
|
||||
return 0;
|
||||
}
|
||||
static int echoDestroy(sqlite3_vtab *pVtab){
|
||||
echo_vtab *p = (echo_vtab*)pVtab;
|
||||
appendToEchoModule(p->interp, "xDestroy");
|
||||
sqliteFree(pVtab);
|
||||
sqliteFree(p->zStmt);
|
||||
sqliteFree(p);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int echoOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor)
|
||||
{
|
||||
echo_cursor *pCur;
|
||||
pCur = sqliteMalloc(sizeof(echo_cursor));
|
||||
*ppCursor = (sqlite3_vtab_cursor *)pCur;
|
||||
return SQLITE_OK;
|
||||
}
|
||||
|
||||
static int echoClose(sqlite3_vtab_cursor *cur)
|
||||
{
|
||||
echo_cursor *pCur = (echo_cursor *)cur;
|
||||
sqlite3_finalize(pCur->pStmt);
|
||||
sqliteFree(pCur);
|
||||
return SQLITE_OK;
|
||||
}
|
||||
|
||||
static int echoNext(sqlite3_vtab_cursor *cur)
|
||||
{
|
||||
int rc;
|
||||
echo_cursor *pCur = (echo_cursor *)cur;
|
||||
|
||||
rc = sqlite3_step(pCur->pStmt);
|
||||
|
||||
if( rc==SQLITE_ROW ){
|
||||
rc = 1;
|
||||
} else {
|
||||
pCur->errcode = sqlite3_finalize(pCur->pStmt);
|
||||
pCur->pStmt = 0;
|
||||
rc = 0;
|
||||
}
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
static int echoFilter(
|
||||
sqlite3_vtab_cursor *pVtabCursor,
|
||||
int idx,
|
||||
int argc,
|
||||
sqlite3_value **argv
|
||||
){
|
||||
int rc;
|
||||
|
||||
echo_cursor *pCur = (echo_cursor *)pVtabCursor;
|
||||
echo_vtab *pVtab = (echo_vtab *)pVtabCursor->pVtab;
|
||||
sqlite3 *db = pVtab->db;
|
||||
|
||||
sqlite3_finalize(pCur->pStmt);
|
||||
pCur->pStmt = 0;
|
||||
rc = sqlite3_prepare(db, pVtab->zStmt, -1, &pCur->pStmt, 0);
|
||||
|
||||
if( rc==SQLITE_OK ){
|
||||
rc = echoNext(pVtabCursor);
|
||||
}
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
static int echoColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i)
|
||||
{
|
||||
int iCol = i + 1;
|
||||
sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt;
|
||||
|
||||
assert( sqlite3_data_count(pStmt)>iCol );
|
||||
switch( sqlite3_column_type(pStmt, iCol) ){
|
||||
case SQLITE_INTEGER:
|
||||
sqlite3_result_int64(ctx, sqlite3_column_int64(pStmt, iCol));
|
||||
break;
|
||||
case SQLITE_FLOAT:
|
||||
sqlite3_result_double(ctx, sqlite3_column_double(pStmt, iCol));
|
||||
break;
|
||||
case SQLITE_TEXT:
|
||||
sqlite3_result_text(ctx,
|
||||
sqlite3_column_text(pStmt, iCol),
|
||||
sqlite3_column_bytes(pStmt, iCol),
|
||||
SQLITE_TRANSIENT
|
||||
);
|
||||
break;
|
||||
case SQLITE_BLOB:
|
||||
sqlite3_result_blob(ctx,
|
||||
sqlite3_column_blob(pStmt, iCol),
|
||||
sqlite3_column_bytes(pStmt, iCol),
|
||||
SQLITE_TRANSIENT
|
||||
);
|
||||
break;
|
||||
}
|
||||
return SQLITE_OK;
|
||||
}
|
||||
|
||||
static int echoRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid)
|
||||
{
|
||||
sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt;
|
||||
*pRowid = sqlite3_column_int64(pStmt, 0);
|
||||
return SQLITE_OK;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** The xBestIndex method for the echo module always returns
|
||||
** an index of 123.
|
||||
@@ -156,6 +271,12 @@ static sqlite3_module echoModule = {
|
||||
echoBestIndex,
|
||||
echoDisconnect,
|
||||
echoDestroy,
|
||||
echoOpen, /* xOpen - open a cursor */
|
||||
echoClose, /* xClose - close a cursor */
|
||||
echoFilter, /* xFilter - configure scan constraints */
|
||||
echoNext, /* xNext - advance a cursor */
|
||||
echoColumn, /* xColumn - read data */
|
||||
echoRowid /* xRowid - read data */
|
||||
};
|
||||
|
||||
/*
|
||||
|
||||
132
src/vdbe.c
132
src/vdbe.c
@@ -43,7 +43,7 @@
|
||||
** in this file for details. If in doubt, do not deviate from existing
|
||||
** commenting and indentation practices when changing or adding code.
|
||||
**
|
||||
** $Id: vdbe.c,v 1.554 2006/06/12 21:59:14 drh Exp $
|
||||
** $Id: vdbe.c,v 1.555 2006/06/13 10:24:43 danielk1977 Exp $
|
||||
*/
|
||||
#include "sqliteInt.h"
|
||||
#include "os.h"
|
||||
@@ -4565,6 +4565,24 @@ case OP_VDestroy: {
|
||||
** table and stores that cursor in P1.
|
||||
*/
|
||||
case OP_VOpen: {
|
||||
Cursor *pCur = 0;
|
||||
sqlite3_vtab_cursor *pVtabCursor = 0;
|
||||
|
||||
sqlite3_vtab *pVtab = (sqlite3_vtab *)(pOp->p3);
|
||||
sqlite3_module *pModule = (sqlite3_module *)pVtab->pModule;
|
||||
|
||||
assert(pVtab && pModule);
|
||||
if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
|
||||
rc = pModule->xOpen(pVtab, &pVtabCursor);
|
||||
if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
|
||||
if( SQLITE_OK==rc ){
|
||||
/* Initialise sqlite3_vtab_cursor base class */
|
||||
pVtabCursor->pVtab = pVtab;
|
||||
|
||||
/* Initialise vdbe cursor object */
|
||||
pCur = allocateCursor(p, pOp->p1, -1);
|
||||
pCur->pVtabCursor = pVtabCursor;
|
||||
}
|
||||
break;
|
||||
}
|
||||
#endif /* SQLITE_OMIT_VIRTUALTABLE */
|
||||
@@ -4579,13 +4597,48 @@ case OP_VOpen: {
|
||||
** by P1. The index number parameter to xFilter is the top of the stack.
|
||||
** Next down on the stack is the argc parameter. Beneath the
|
||||
** next of stack are argc additional parameters which are passed to
|
||||
** xFilter as argv. The index number, argc, and all argv stack values
|
||||
** are popped from the stack before this instruction completes.
|
||||
** xFilter as argv. The topmost parameter (i.e. 3rd element popped from
|
||||
** the stack) becomes argv[argc-1] when passed to xFilter.
|
||||
**
|
||||
** The index number, argc, and all argv stack values are popped from the
|
||||
** stack before this instruction completes.
|
||||
**
|
||||
** A jump is made to P2 if the result set after filtering would be
|
||||
** empty.
|
||||
*/
|
||||
case OP_VFilter: {
|
||||
int iIndex;
|
||||
int nArg;
|
||||
|
||||
const sqlite3_module *pModule;
|
||||
|
||||
Cursor *pCur = p->apCsr[pOp->p1];
|
||||
assert( pCur->pVtabCursor );
|
||||
pModule = pCur->pVtabCursor->pVtab->pModule;
|
||||
|
||||
/* Grab the index number and argc parameters off the top of the stack. */
|
||||
assert( (&pTos[-1])>=p->aStack );
|
||||
assert( pTos[0].flags==MEM_Int && pTos[-1].flags==MEM_Int );
|
||||
iIndex = pTos[0].i;
|
||||
nArg = pTos[-1].i;
|
||||
|
||||
/* Invoke the xFilter method if one is defined. */
|
||||
if( pModule->xFilter ){
|
||||
int res;
|
||||
Mem *apArg;
|
||||
apArg = &pTos[1-2-nArg];
|
||||
|
||||
if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
|
||||
res = pModule->xFilter(pCur->pVtabCursor, iIndex, nArg, &apArg);
|
||||
if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
|
||||
|
||||
if( res==0 ){
|
||||
pc = pOp->p2 - 1;
|
||||
}
|
||||
}
|
||||
|
||||
/* Pop the index number, argc value and parameters off the stack */
|
||||
popStack(&pTos, 2+nArg);
|
||||
break;
|
||||
}
|
||||
#endif /* SQLITE_OMIT_VIRTUALTABLE */
|
||||
@@ -4597,6 +4650,26 @@ case OP_VFilter: {
|
||||
** the virtual-table that the P1 cursor is pointing to.
|
||||
*/
|
||||
case OP_VRowid: {
|
||||
const sqlite3_module *pModule;
|
||||
|
||||
Cursor *pCur = p->apCsr[pOp->p1];
|
||||
assert( pCur->pVtabCursor );
|
||||
pModule = pCur->pVtabCursor->pVtab->pModule;
|
||||
if( pModule->xRowid==0 ){
|
||||
sqlite3SetString(&p->zErrMsg, "Unsupported module operation: xRowid", 0);
|
||||
rc = SQLITE_ERROR;
|
||||
} else {
|
||||
sqlite_int64 iRow;
|
||||
|
||||
if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
|
||||
rc = pModule->xRowid(pCur->pVtabCursor, &iRow);
|
||||
if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
|
||||
|
||||
pTos++;
|
||||
pTos->flags = MEM_Int;
|
||||
pTos->i = iRow;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
#endif /* SQLITE_OMIT_VIRTUALTABLE */
|
||||
@@ -4608,6 +4681,33 @@ case OP_VRowid: {
|
||||
** the row of the virtual-table that the P1 cursor is pointing to.
|
||||
*/
|
||||
case OP_VColumn: {
|
||||
const sqlite3_module *pModule;
|
||||
|
||||
Cursor *pCur = p->apCsr[pOp->p1];
|
||||
assert( pCur->pVtabCursor );
|
||||
pModule = pCur->pVtabCursor->pVtab->pModule;
|
||||
if( pModule->xColumn==0 ){
|
||||
sqlite3SetString(&p->zErrMsg, "Unsupported module operation: xColumn", 0);
|
||||
rc = SQLITE_ERROR;
|
||||
} else {
|
||||
sqlite3_context sContext;
|
||||
memset(&sContext, 0, sizeof(sContext));
|
||||
sContext.s.flags = MEM_Null;
|
||||
if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
|
||||
rc = pModule->xColumn(pCur->pVtabCursor, &sContext, pOp->p2);
|
||||
|
||||
/* Copy the result of the function to the top of the stack. We
|
||||
** do this regardless of whether or not an error occured to ensure any
|
||||
** dynamic allocation in sContext.s (a Mem struct) is released.
|
||||
*/
|
||||
sqlite3VdbeChangeEncoding(&sContext.s, encoding);
|
||||
pTos++;
|
||||
pTos->flags = 0;
|
||||
sqlite3VdbeMemMove(pTos, &sContext.s);
|
||||
|
||||
if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
#endif /* SQLITE_OMIT_VIRTUALTABLE */
|
||||
@@ -4620,6 +4720,32 @@ case OP_VColumn: {
|
||||
** the end of its result set, then fall through to the next instruction.
|
||||
*/
|
||||
case OP_VNext: {
|
||||
const sqlite3_module *pModule;
|
||||
int res = 0;
|
||||
|
||||
Cursor *pCur = p->apCsr[pOp->p1];
|
||||
assert( pCur->pVtabCursor );
|
||||
pModule = pCur->pVtabCursor->pVtab->pModule;
|
||||
if( pModule->xNext==0 ){
|
||||
sqlite3SetString(&p->zErrMsg, "Unsupported module operation: xNext", 0);
|
||||
rc = SQLITE_ERROR;
|
||||
} else {
|
||||
/* Invoke the xNext() method of the module. There is no way for the
|
||||
** underlying implementation to return an error if one occurs during
|
||||
** xNext(). Instead, if an error occurs, true is returned (indicating that
|
||||
** data is available) and the error code returned when xColumn or
|
||||
** some other method is next invoked on the save virtual table cursor.
|
||||
*/
|
||||
if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
|
||||
res = pModule->xNext(pCur->pVtabCursor);
|
||||
if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
|
||||
|
||||
if( res ){
|
||||
/* If there is data (or an error), jump to P2 */
|
||||
pc = pOp->p2 - 1;
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
#endif /* SQLITE_OMIT_VIRTUALTABLE */
|
||||
|
||||
@@ -839,7 +839,7 @@ void sqlite3VdbeFreeCursor(Cursor *pCx){
|
||||
if( pCx->pVtabCursor ){
|
||||
sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor;
|
||||
sqlite3_vtab *pVtab = pVtabCursor->pVtab;
|
||||
sqlite3_module *pModule = pVtab->pModule;
|
||||
const sqlite3_module *pModule = pVtab->pModule;
|
||||
pModule->xClose(pVtabCursor);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
*************************************************************************
|
||||
** This file contains code used to help implement virtual tables.
|
||||
**
|
||||
** $Id: vtab.c,v 1.5 2006/06/12 16:01:22 danielk1977 Exp $
|
||||
** $Id: vtab.c,v 1.6 2006/06/13 10:24:43 danielk1977 Exp $
|
||||
*/
|
||||
#ifndef SQLITE_OMIT_VIRTUALTABLE
|
||||
#include "sqliteInt.h"
|
||||
@@ -367,9 +367,6 @@ int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){
|
||||
}
|
||||
}
|
||||
|
||||
if( SQLITE_OK==rc ){
|
||||
pTab->needCreate = 0;
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user