Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "sqlite3ext.h"
- SQLITE_EXTENSION_INIT1
- #include <assert.h>
- #include <string.h>
- #include <stdio.h>
- #ifndef SQLITE_OMIT_VIRTUALTABLE
- typedef struct wrapper_vtab wrapper_vtab;
- struct wrapper_vtab {
- sqlite3_vtab base; /* Base class - must be first */
- char* zSql;
- sqlite3_stmt *pStmt;
- sqlite3 *db; /* The database connection */
- };
- /* A wrapper cursor object */
- typedef struct wrapper_cursor wrapper_cursor;
- struct wrapper_cursor {
- sqlite3_vtab_cursor base; /* Base class - must be first */
- sqlite3_int64 id;
- wrapper_vtab* pVtab;
- };
- static int wrapperConnect(
- sqlite3 *db,
- void *pAux,
- int argc, const char *const*argv,
- sqlite3_vtab **ppVtab,
- char **pzErr
- ){
- sqlite3_vtab *pNew;
- pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) );
- if( pNew==0 ) return SQLITE_NOMEM;
- sqlite3_declare_vtab(db, "CREATE TABLE x(id INTEGER)");
- memset(pNew, 0, sizeof(*pNew));
- int rc;
- wrapper_vtab *pVt = 0;
- pVt = sqlite3_malloc( sizeof(*pVt) );
- if( pVt==0 ) return SQLITE_NOMEM;
- rc = SQLITE_NOMEM;
- memset(pVt, 0, sizeof(*pVt));
- pVt->db = db;
- *ppVtab = &pVt->base;
- sqlite3_stmt *pStmt;
- pVt->pStmt = pStmt;
- pVt->zSql= sqlite3_mprintf("select id \
- from \
- B \
- where \
- id=?1 ;");
- if( pVt->zSql==0 ){
- return SQLITE_NOMEM;
- }else{
- rc = sqlite3_prepare_v2(pVt->db, pVt->zSql, -1, &pVt->pStmt, NULL);
- sqlite3_free(pVt->zSql);
- }
- return SQLITE_OK;
- }
- static int wrapperDisconnect(sqlite3_vtab *pVtab){
- wrapper_vtab *p = (wrapper_vtab*)pVtab;
- sqlite3_finalize(p->pStmt);
- sqlite3_free(pVtab);
- return SQLITE_OK;
- }
- static int wrapperOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
- wrapper_vtab *p = (wrapper_vtab*)pVTab;
- wrapper_cursor *pCur;
- pCur = sqlite3_malloc( sizeof(*pCur) );
- if( pCur==0 ) return SQLITE_NOMEM;
- memset(pCur, 0, sizeof(*pCur));
- pCur->pVtab = p;
- *ppCursor = &pCur->base;
- return SQLITE_OK;
- }
- static int wrapperClose(sqlite3_vtab_cursor *cur){
- wrapper_cursor *pCur = (wrapper_cursor*)cur;
- pCur->id=0;
- sqlite3_free(cur);
- return SQLITE_OK;
- }
- static int wrapperNext(sqlite3_vtab_cursor *cur){
- wrapper_cursor *pCur = (wrapper_cursor*)cur;
- wrapper_vtab *pVtab = pCur->pVtab;
- if(sqlite3_step(pVtab->pStmt)==SQLITE_ROW ){
- pCur-> id = sqlite3_column_int64(pVtab->pStmt, 0);
- }
- else
- {
- pCur-> id = 0;
- }
- return SQLITE_OK;
- }
- static int wrapperColumn(
- sqlite3_vtab_cursor *cur,
- sqlite3_context *ctx,
- int i
- ){
- wrapper_cursor *pCur = (wrapper_cursor*)cur;
- if(i==0)
- sqlite3_result_int64(ctx,pCur->id);
- return SQLITE_OK;
- }
- static int wrapperRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
- wrapper_cursor *pCur = (wrapper_cursor*)cur;
- //*pRowid = pCur->iValue;
- return SQLITE_OK;
- }
- static int wrapperEof(sqlite3_vtab_cursor *cur){
- wrapper_cursor *pCur = (wrapper_cursor*)cur;
- return pCur->id==0;
- }
- static int wrapperFilter(
- sqlite3_vtab_cursor *pVtabCursor,
- int idxNum, const char *idxStr,
- int argc, sqlite3_value **argv
- ){
- wrapper_cursor *pCur = (wrapper_cursor *)pVtabCursor;
- int rc;
- wrapper_vtab *pVtab = pCur->pVtab;
- pCur -> id = sqlite3_value_int64(argv[0]);
- sqlite3_reset(pVtab->pStmt);
- rc=sqlite3_bind_int64(pVtab->pStmt, 1, pCur -> id);
- if(rc!= SQLITE_OK){
- printf("error no:%i",rc);
- }
- if( sqlite3_step(pVtab->pStmt)==SQLITE_ROW ){
- pCur-> id= sqlite3_column_int64(pVtab->pStmt, 0);
- }
- else{
- pCur-> id = 0;
- }
- return SQLITE_OK;
- }
- static int wrapperBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo)
- {
- double rCost = 1.0;
- int i;
- int idxNum = 0;
- int ltIdx = -1;
- int col=0;
- pIdxInfo->estimatedCost = 100;
- pIdxInfo-> estimatedRows = 1000;
- const struct sqlite3_index_constraint *pConstraint;
- pConstraint = pIdxInfo->aConstraint;
- for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
- if( pConstraint->usable==0 ) continue;
- col=pConstraint->iColumn;
- if( (idxNum & 3)==0 && pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ ){
- idxNum |= 1;
- ltIdx = i;
- }
- }
- if( ltIdx>=0 ){
- pIdxInfo->idxNum = col;
- pIdxInfo->aConstraintUsage[ltIdx].argvIndex = 1;
- pIdxInfo->aConstraintUsage[ltIdx].omit = 1;
- pIdxInfo->estimatedCost = rCost;
- pIdxInfo-> estimatedRows = 1;
- }
- return SQLITE_OK;
- }
- /*
- ** A virtual table module that provides read-only access to a
- ** Tcl global variable namespace.
- */
- static sqlite3_module wrapperModule = {
- 0, /* iVersion */
- wrapperConnect,
- wrapperConnect,
- wrapperBestIndex,
- wrapperDisconnect,
- wrapperDisconnect,
- wrapperOpen, /* xOpen - open a cursor */
- wrapperClose, /* xClose - close a cursor */
- wrapperFilter, /* xFilter - configure scan constraints */
- wrapperNext, /* xNext - advance a cursor */
- wrapperEof, /* xEof - check for end of scan */
- wrapperColumn, /* xColumn - read data */
- wrapperRowid, /* xRowid - read data */
- 0, /* xUpdate */
- 0, /* xBegin */
- 0, /* xSync */
- 0, /* xCommit */
- 0, /* xRollback */
- 0, /* xFindMethod */
- 0, /* xRename */
- };
- #endif /* SQLITE_OMIT_VIRTUALTABLE */
- #ifdef _WIN32
- __declspec(dllexport)
- #endif
- int sqlite3_wrapper_init(
- sqlite3 *db,
- char **pzErrMsg,
- const sqlite3_api_routines *pApi
- ){
- int rc = SQLITE_OK;
- SQLITE_EXTENSION_INIT2(pApi);
- #ifndef SQLITE_OMIT_VIRTUALTABLE
- rc = sqlite3_create_module(db, "wrapper", &wrapperModule, 0);
- #endif
- return rc;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement