1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-08-05 15:55:57 +03:00

Add implementations for opcodes required for linear scans of virtual tables. (CVS 3223)

FossilOrigin-Name: 1f20e1832b38c76d2b0dde5fd720670c2ad0438b
This commit is contained in:
danielk1977
2006-06-13 10:24:42 +00:00
parent 48d4580650
commit b7a7b9a3b9
8 changed files with 343 additions and 64 deletions

View File

@@ -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 */
};
/*