#if !defined(SQLITEINT_H)
#include "sqlite3ext.h"
#endif
SQLITE_EXTENSION_INIT1
#include <string.h>
#include <assert.h>
typedef struct templatevtab_vtab templatevtab_vtab;
struct templatevtab_vtab {
sqlite3_vtab base;
};
typedef struct templatevtab_cursor templatevtab_cursor;
struct templatevtab_cursor {
sqlite3_vtab_cursor base;
sqlite3_int64 iRowid;
};
static int templatevtabConnect(
sqlite3 *db,
void *pAux,
int argc, const char *const*argv,
sqlite3_vtab **ppVtab,
char **pzErr
){
templatevtab_vtab *pNew;
int rc;
rc = sqlite3_declare_vtab(db,
"CREATE TABLE x(a,b)"
);
#define TEMPLATEVTAB_A 0
#define TEMPLATEVTAB_B 1
if( rc==SQLITE_OK ){
pNew = sqlite3_malloc( sizeof(*pNew) );
*ppVtab = (sqlite3_vtab*)pNew;
if( pNew==0 ) return SQLITE_NOMEM;
memset(pNew, 0, sizeof(*pNew));
}
return rc;
}
static int templatevtabDisconnect(sqlite3_vtab *pVtab){
templatevtab_vtab *p = (templatevtab_vtab*)pVtab;
sqlite3_free(p);
return SQLITE_OK;
}
static int templatevtabOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
templatevtab_cursor *pCur;
pCur = sqlite3_malloc( sizeof(*pCur) );
if( pCur==0 ) return SQLITE_NOMEM;
memset(pCur, 0, sizeof(*pCur));
*ppCursor = &pCur->base;
return SQLITE_OK;
}
static int templatevtabClose(sqlite3_vtab_cursor *cur){
templatevtab_cursor *pCur = (templatevtab_cursor*)cur;
sqlite3_free(pCur);
return SQLITE_OK;
}
static int templatevtabNext(sqlite3_vtab_cursor *cur){
templatevtab_cursor *pCur = (templatevtab_cursor*)cur;
pCur->iRowid++;
return SQLITE_OK;
}
static int templatevtabColumn(
sqlite3_vtab_cursor *cur,
sqlite3_context *ctx,
int i
){
templatevtab_cursor *pCur = (templatevtab_cursor*)cur;
switch( i ){
case TEMPLATEVTAB_A:
sqlite3_result_int(ctx, 1000 + pCur->iRowid);
break;
default:
assert( i==TEMPLATEVTAB_B );
sqlite3_result_int(ctx, 2000 + pCur->iRowid);
break;
}
return SQLITE_OK;
}
static int templatevtabRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
templatevtab_cursor *pCur = (templatevtab_cursor*)cur;
*pRowid = pCur->iRowid;
return SQLITE_OK;
}
static int templatevtabEof(sqlite3_vtab_cursor *cur){
templatevtab_cursor *pCur = (templatevtab_cursor*)cur;
return pCur->iRowid>=10;
}
static int templatevtabFilter(
sqlite3_vtab_cursor *pVtabCursor,
int idxNum, const char *idxStr,
int argc, sqlite3_value **argv
){
templatevtab_cursor *pCur = (templatevtab_cursor *)pVtabCursor;
pCur->iRowid = 1;
return SQLITE_OK;
}
static int templatevtabBestIndex(
sqlite3_vtab *tab,
sqlite3_index_info *pIdxInfo
){
pIdxInfo->estimatedCost = (double)10;
pIdxInfo->estimatedRows = 10;
return SQLITE_OK;
}
static sqlite3_module templatevtabModule = {
0,
0,
templatevtabConnect,
templatevtabBestIndex,
templatevtabDisconnect,
0,
templatevtabOpen,
templatevtabClose,
templatevtabFilter,
templatevtabNext,
templatevtabEof,
templatevtabColumn,
templatevtabRowid,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0
};
#ifdef _WIN32
__declspec(dllexport)
#endif
int sqlite3_templatevtab_init(
sqlite3 *db,
char **pzErrMsg,
const sqlite3_api_routines *pApi
){
int rc = SQLITE_OK;
SQLITE_EXTENSION_INIT2(pApi);
rc = sqlite3_create_module(db, "templatevtab", &templatevtabModule, 0);
return rc;
}