Advertisement
Guest User

Untitled

a guest
Oct 21st, 2016
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.79 KB | None | 0 0
  1.  
  2. #include "sqlite3ext.h"
  3. SQLITE_EXTENSION_INIT1
  4. #include <assert.h>
  5. #include <string.h>
  6.  
  7. #include <stdio.h>
  8.  
  9. #ifndef SQLITE_OMIT_VIRTUALTABLE
  10.  
  11.  
  12. typedef struct wrapper_vtab wrapper_vtab;
  13. struct wrapper_vtab {
  14.   sqlite3_vtab base;         /* Base class - must be first */
  15.   char* zSql;
  16.  
  17. sqlite3_stmt *pStmt;
  18.   sqlite3 *db;               /* The database connection */
  19. };
  20.  
  21. /* A wrapper cursor object */
  22. typedef struct wrapper_cursor wrapper_cursor;
  23. struct wrapper_cursor {
  24.   sqlite3_vtab_cursor base;  /* Base class - must be first */
  25.  sqlite3_int64 id;
  26.   wrapper_vtab* pVtab;
  27. };
  28.  
  29.  
  30. static int wrapperConnect(
  31.   sqlite3 *db,
  32.   void *pAux,
  33.   int argc, const char *const*argv,
  34.   sqlite3_vtab **ppVtab,
  35.   char **pzErr
  36. ){
  37.  
  38.   sqlite3_vtab *pNew;
  39.   pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) );
  40.   if( pNew==0 ) return SQLITE_NOMEM;
  41.   sqlite3_declare_vtab(db, "CREATE TABLE x(id INTEGER)");
  42.   memset(pNew, 0, sizeof(*pNew));
  43.   int rc;
  44.   wrapper_vtab *pVt = 0;        
  45.   pVt = sqlite3_malloc( sizeof(*pVt) );
  46.   if( pVt==0 ) return SQLITE_NOMEM;
  47.   rc = SQLITE_NOMEM;
  48.   memset(pVt, 0, sizeof(*pVt));
  49.   pVt->db = db;
  50.  
  51.  
  52.   *ppVtab = &pVt->base;
  53.  
  54.  
  55.  
  56. sqlite3_stmt *pStmt;
  57. pVt->pStmt = pStmt;
  58.  
  59.  
  60. pVt->zSql= sqlite3_mprintf("select id \
  61. from \
  62. B \
  63. where \
  64. id=?1 ;");
  65.  
  66. if( pVt->zSql==0 ){
  67.     return SQLITE_NOMEM;
  68.   }else{
  69.  
  70.     rc = sqlite3_prepare_v2(pVt->db, pVt->zSql, -1, &pVt->pStmt, NULL);
  71.  
  72.     sqlite3_free(pVt->zSql);
  73.  
  74.   }
  75.  
  76.  
  77.   return SQLITE_OK;
  78. }
  79.  
  80. static int wrapperDisconnect(sqlite3_vtab *pVtab){
  81. wrapper_vtab *p = (wrapper_vtab*)pVtab;
  82. sqlite3_finalize(p->pStmt);
  83.   sqlite3_free(pVtab);
  84.   return SQLITE_OK;
  85. }
  86.  
  87.  
  88.  
  89.  
  90. static int wrapperOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
  91.  
  92.   wrapper_vtab *p = (wrapper_vtab*)pVTab;
  93.   wrapper_cursor *pCur;
  94.   pCur = sqlite3_malloc( sizeof(*pCur) );
  95.   if( pCur==0 ) return SQLITE_NOMEM;
  96.   memset(pCur, 0, sizeof(*pCur));
  97.   pCur->pVtab = p;
  98.   *ppCursor = &pCur->base;
  99.  
  100.  
  101.   return SQLITE_OK;
  102. }
  103.  
  104.  
  105. static int wrapperClose(sqlite3_vtab_cursor *cur){
  106.  
  107.  wrapper_cursor *pCur = (wrapper_cursor*)cur;
  108. pCur->id=0;
  109.  
  110.   sqlite3_free(cur);
  111.   return SQLITE_OK;
  112. }
  113.  
  114.  
  115.  
  116. static int wrapperNext(sqlite3_vtab_cursor *cur){
  117.  
  118.  
  119.   wrapper_cursor *pCur = (wrapper_cursor*)cur;
  120. wrapper_vtab *pVtab = pCur->pVtab;
  121.  
  122.  
  123.   if(sqlite3_step(pVtab->pStmt)==SQLITE_ROW ){
  124.  
  125.  
  126.         pCur-> id = sqlite3_column_int64(pVtab->pStmt, 0);
  127.  
  128.        
  129.   }
  130. else
  131. {
  132.  
  133. pCur-> id = 0;
  134. }
  135.  
  136.  
  137.   return SQLITE_OK;
  138. }
  139.  
  140.  
  141. static int wrapperColumn(
  142.   sqlite3_vtab_cursor *cur,
  143.   sqlite3_context *ctx,
  144.   int i
  145. ){
  146.  
  147.  
  148.   wrapper_cursor *pCur = (wrapper_cursor*)cur;
  149.  
  150. if(i==0)
  151.       sqlite3_result_int64(ctx,pCur->id);
  152.  
  153.   return SQLITE_OK;
  154. }
  155.  
  156. static int wrapperRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
  157.   wrapper_cursor *pCur = (wrapper_cursor*)cur;
  158.   //*pRowid = pCur->iValue;
  159.   return SQLITE_OK;
  160. }
  161.  
  162.  
  163. static int wrapperEof(sqlite3_vtab_cursor *cur){
  164.   wrapper_cursor *pCur = (wrapper_cursor*)cur;
  165.   return pCur->id==0;
  166. }
  167.  
  168.  
  169. static int wrapperFilter(
  170.   sqlite3_vtab_cursor *pVtabCursor,
  171.   int idxNum, const char *idxStr,
  172.   int argc, sqlite3_value **argv
  173. ){
  174.  
  175.   wrapper_cursor *pCur = (wrapper_cursor *)pVtabCursor;
  176.  
  177.   int rc;
  178.  
  179.   wrapper_vtab *pVtab = pCur->pVtab;
  180.  
  181.  
  182.    pCur -> id =   sqlite3_value_int64(argv[0]);
  183.  
  184.  
  185.  
  186. sqlite3_reset(pVtab->pStmt);
  187.  
  188.  
  189. rc=sqlite3_bind_int64(pVtab->pStmt, 1, pCur -> id);
  190.  
  191.  
  192. if(rc!= SQLITE_OK){
  193. printf("error no:%i",rc);
  194. }
  195.  
  196.   if( sqlite3_step(pVtab->pStmt)==SQLITE_ROW ){
  197.         pCur-> id= sqlite3_column_int64(pVtab->pStmt, 0);
  198.   }
  199. else{
  200.  pCur-> id = 0;
  201. }
  202.  
  203.   return SQLITE_OK;
  204. }
  205.  
  206.  
  207. static int wrapperBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo)
  208. {
  209.  
  210.  
  211.   double rCost = 1.0;
  212. int i;
  213.   int idxNum = 0;
  214.   int ltIdx = -1;
  215. int col=0;
  216.  
  217. pIdxInfo->estimatedCost = 100;
  218.    pIdxInfo-> estimatedRows = 1000;
  219.   const struct sqlite3_index_constraint *pConstraint;
  220.   pConstraint = pIdxInfo->aConstraint;
  221.   for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
  222.     if( pConstraint->usable==0 ) continue;
  223.  
  224.   col=pConstraint->iColumn;
  225.  
  226.  
  227.     if( (idxNum & 3)==0 && pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ ){
  228.       idxNum |= 1;
  229.       ltIdx = i;
  230.     }
  231.   }
  232.  
  233.   if( ltIdx>=0 ){
  234. pIdxInfo->idxNum = col;
  235.     pIdxInfo->aConstraintUsage[ltIdx].argvIndex = 1;
  236.     pIdxInfo->aConstraintUsage[ltIdx].omit = 1;
  237.  
  238.  
  239. pIdxInfo->estimatedCost = rCost;
  240.  
  241. pIdxInfo-> estimatedRows = 1;
  242.   }
  243.    
  244.    
  245.  
  246.   return SQLITE_OK;
  247.  
  248. }
  249.  
  250. /*
  251. ** A virtual table module that provides read-only access to a
  252. ** Tcl global variable namespace.
  253. */
  254. static sqlite3_module wrapperModule = {
  255.   0,                         /* iVersion */
  256.   wrapperConnect,
  257.   wrapperConnect,
  258.   wrapperBestIndex,
  259.   wrapperDisconnect,
  260.   wrapperDisconnect,
  261.   wrapperOpen,           /* xOpen - open a cursor */
  262.   wrapperClose,          /* xClose - close a cursor */
  263.   wrapperFilter,         /* xFilter - configure scan constraints */
  264.   wrapperNext,           /* xNext - advance a cursor */
  265.   wrapperEof,            /* xEof - check for end of scan */
  266.   wrapperColumn,         /* xColumn - read data */
  267.   wrapperRowid,          /* xRowid - read data */
  268.   0,                         /* xUpdate */
  269.   0,                         /* xBegin */
  270.   0,                         /* xSync */
  271.   0,                         /* xCommit */
  272.   0,                         /* xRollback */
  273.   0,                         /* xFindMethod */
  274.   0,                         /* xRename */
  275. };
  276.  
  277. #endif /* SQLITE_OMIT_VIRTUALTABLE */
  278.  
  279. #ifdef _WIN32
  280. __declspec(dllexport)
  281. #endif
  282. int sqlite3_wrapper_init(
  283.   sqlite3 *db,
  284.   char **pzErrMsg,
  285.   const sqlite3_api_routines *pApi
  286. ){
  287.   int rc = SQLITE_OK;
  288.   SQLITE_EXTENSION_INIT2(pApi);
  289. #ifndef SQLITE_OMIT_VIRTUALTABLE
  290.   rc = sqlite3_create_module(db, "wrapper", &wrapperModule, 0);
  291. #endif
  292.   return rc;
  293. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement