Team Ai
Modelpublic

AryaWu/sqlite

sourceHugging Faceupdated 10mo agoView on Hugging Face
0likes
test_intarray.c392 linesDownload Raw Back to src
1/*2** 2009 November 103**4** The author disclaims copyright to this source code.  In place of5** a legal notice, here is a blessing:6**7**    May you do good and not evil.8**    May you find forgiveness for yourself and forgive others.9**    May you share freely, never taking more than you give.10**11*************************************************************************12**13** This file implements a read-only VIRTUAL TABLE that contains the14** content of a C-language array of integer values.  See the corresponding15** header file for full details.16**17** This virtual table is used for internal testing of SQLite only.  It is18** not recommended for use in production.  For a similar virtual table that19** is production-ready, see the "carray" virtual table over in ext/misc.20*/21#include "test_intarray.h"22#include <string.h>23#include <assert.h>24 25 26/*27** Definition of the sqlite3_intarray object.28**29** The internal representation of an intarray object is subject30** to change, is not externally visible, and should be used by31** the implementation of intarray only.  This object is opaque32** to users.33*/34struct sqlite3_intarray {35  int n;                    /* Number of elements in the array */36  sqlite3_int64 *a;         /* Contents of the array */37  void (*xFree)(void*);     /* Function used to free a[] */38};39 40/* Objects used internally by the virtual table implementation */41typedef struct intarray_vtab intarray_vtab;42typedef struct intarray_cursor intarray_cursor;43 44/* An intarray table object */45struct intarray_vtab {46  sqlite3_vtab base;            /* Base class */47  sqlite3_intarray *pContent;   /* Content of the integer array */48};49 50/* An intarray cursor object */51struct intarray_cursor {52  sqlite3_vtab_cursor base;    /* Base class */53  int i;                       /* Current cursor position */54};55 56/*57** None of this works unless we have virtual tables.58*/59#ifndef SQLITE_OMIT_VIRTUALTABLE60 61/*62** Free an sqlite3_intarray object.63*/64static void intarrayFree(void *pX){65  sqlite3_intarray *p = (sqlite3_intarray*)pX;66  if( p->xFree ){67    p->xFree(p->a);68  }69  sqlite3_free(p);70}71 72/*73** Table destructor for the intarray module.74*/75static int intarrayDestroy(sqlite3_vtab *p){76  intarray_vtab *pVtab = (intarray_vtab*)p;77  sqlite3_free(pVtab);78  return 0;79}80 81/*82** Table constructor for the intarray module.83*/84static int intarrayCreate(85  sqlite3 *db,              /* Database where module is created */86  void *pAux,               /* clientdata for the module */87  int argc,                 /* Number of arguments */88  const char *const*argv,   /* Value for all arguments */89  sqlite3_vtab **ppVtab,    /* Write the new virtual table object here */90  char **pzErr              /* Put error message text here */91){92  int rc = SQLITE_NOMEM;93  intarray_vtab *pVtab = sqlite3_malloc64(sizeof(intarray_vtab));94 95  if( pVtab ){96    memset(pVtab, 0, sizeof(intarray_vtab));97    pVtab->pContent = (sqlite3_intarray*)pAux;98    rc = sqlite3_declare_vtab(db, "CREATE TABLE x(value INTEGER PRIMARY KEY)");99  }100  *ppVtab = (sqlite3_vtab *)pVtab;101  return rc;102}103 104/*105** Open a new cursor on the intarray table.106*/107static int intarrayOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){108  int rc = SQLITE_NOMEM;109  intarray_cursor *pCur;110  pCur = sqlite3_malloc64(sizeof(intarray_cursor));111  if( pCur ){112    memset(pCur, 0, sizeof(intarray_cursor));113    *ppCursor = (sqlite3_vtab_cursor *)pCur;114    rc = SQLITE_OK;115  }116  return rc;117}118 119/*120** Close a intarray table cursor.121*/122static int intarrayClose(sqlite3_vtab_cursor *cur){123  intarray_cursor *pCur = (intarray_cursor *)cur;124  sqlite3_free(pCur);125  return SQLITE_OK;126}127 128/*129** Retrieve a column of data.130*/131static int intarrayColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){132  intarray_cursor *pCur = (intarray_cursor*)cur;133  intarray_vtab *pVtab = (intarray_vtab*)cur->pVtab;134  if( pCur->i>=0 && pCur->i<pVtab->pContent->n ){135    sqlite3_result_int64(ctx, pVtab->pContent->a[pCur->i]);136  }137  return SQLITE_OK;138}139 140/*141** Retrieve the current rowid.142*/143static int intarrayRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){144  intarray_cursor *pCur = (intarray_cursor *)cur;145  *pRowid = pCur->i;146  return SQLITE_OK;147}148 149static int intarrayEof(sqlite3_vtab_cursor *cur){150  intarray_cursor *pCur = (intarray_cursor *)cur;151  intarray_vtab *pVtab = (intarray_vtab *)cur->pVtab;152  return pCur->i>=pVtab->pContent->n;153}154 155/*156** Advance the cursor to the next row.157*/158static int intarrayNext(sqlite3_vtab_cursor *cur){159  intarray_cursor *pCur = (intarray_cursor *)cur;160  pCur->i++;161  return SQLITE_OK;162}163 164/*165** Reset a intarray table cursor.166*/167static int intarrayFilter(168  sqlite3_vtab_cursor *pVtabCursor, 169  int idxNum, const char *idxStr,170  int argc, sqlite3_value **argv171){172  intarray_cursor *pCur = (intarray_cursor *)pVtabCursor;173  pCur->i = 0;174  return SQLITE_OK;175}176 177/*178** Analyse the WHERE condition.179*/180static int intarrayBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){181  return SQLITE_OK;182}183 184/*185** A virtual table module that merely echos method calls into TCL186** variables.187*/188static sqlite3_module intarrayModule = {189  0,                           /* iVersion */190  intarrayCreate,              /* xCreate - create a new virtual table */191  intarrayCreate,              /* xConnect - connect to an existing vtab */192  intarrayBestIndex,           /* xBestIndex - find the best query index */193  intarrayDestroy,             /* xDisconnect - disconnect a vtab */194  intarrayDestroy,             /* xDestroy - destroy a vtab */195  intarrayOpen,                /* xOpen - open a cursor */196  intarrayClose,               /* xClose - close a cursor */197  intarrayFilter,              /* xFilter - configure scan constraints */198  intarrayNext,                /* xNext - advance a cursor */199  intarrayEof,                 /* xEof */200  intarrayColumn,              /* xColumn - read data */201  intarrayRowid,               /* xRowid - read data */202  0,                           /* xUpdate */203  0,                           /* xBegin */204  0,                           /* xSync */205  0,                           /* xCommit */206  0,                           /* xRollback */207  0,                           /* xFindMethod */208  0,                           /* xRename */209  0,                           /* xSavepoint */210  0,                           /* xRelease */211  0,                           /* xRollbackTo */212  0,                           /* xShadowName */213  0                            /* xIntegrity */214};215 216#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */217 218/*219** Invoke this routine to create a specific instance of an intarray object.220** The new intarray object is returned by the 3rd parameter.221**222** Each intarray object corresponds to a virtual table in the TEMP table223** with a name of zName.224**225** Destroy the intarray object by dropping the virtual table.  If not done226** explicitly by the application, the virtual table will be dropped implicitly227** by the system when the database connection is closed.228*/229SQLITE_API int sqlite3_intarray_create(230  sqlite3 *db,231  const char *zName,232  sqlite3_intarray **ppReturn233){234  int rc = SQLITE_OK;235#ifndef SQLITE_OMIT_VIRTUALTABLE236  sqlite3_intarray *p;237 238  *ppReturn = p = sqlite3_malloc64( sizeof(*p) );239  if( p==0 ){240    return SQLITE_NOMEM;241  }242  memset(p, 0, sizeof(*p));243  rc = sqlite3_create_module_v2(db, zName, &intarrayModule, p,244                                (void(*)(void*))intarrayFree);245  if( rc==SQLITE_OK ){246    char *zSql;247    zSql = sqlite3_mprintf("CREATE VIRTUAL TABLE temp.%Q USING %Q",248                           zName, zName);249    rc = sqlite3_exec(db, zSql, 0, 0, 0);250    sqlite3_free(zSql);251  }252#endif253  return rc;254}255 256/*257** Bind a new array array of integers to a specific intarray object.258**259** The array of integers bound must be unchanged for the duration of260** any query against the corresponding virtual table.  If the integer261** array does change or is deallocated undefined behavior will result.262*/263SQLITE_API int sqlite3_intarray_bind(264  sqlite3_intarray *pIntArray,   /* The intarray object to bind to */265  int nElements,                 /* Number of elements in the intarray */266  sqlite3_int64 *aElements,      /* Content of the intarray */267  void (*xFree)(void*)           /* How to dispose of the intarray when done */268){269  if( pIntArray->xFree ){270    pIntArray->xFree(pIntArray->a);271  }272  pIntArray->n = nElements;273  pIntArray->a = aElements;274  pIntArray->xFree = xFree;275  return SQLITE_OK;276}277 278 279/*****************************************************************************280** Everything below is interface for testing this module.281*/282#ifdef SQLITE_TEST283#include "tclsqlite.h"284 285/*286** Routines to encode and decode pointers287*/288extern int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb);289extern void *sqlite3TestTextToPtr(const char*);290extern int sqlite3TestMakePointerStr(Tcl_Interp*, char *zPtr, void*);291extern const char *sqlite3ErrName(int);292 293/*294**    sqlite3_intarray_create  DB  NAME295**296** Invoke the sqlite3_intarray_create interface.  A string that becomes297** the first parameter to sqlite3_intarray_bind.298*/299static int SQLITE_TCLAPI test_intarray_create(300  ClientData clientData, /* Not used */301  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */302  int objc,              /* Number of arguments */303  Tcl_Obj *CONST objv[]  /* Command arguments */304){305  sqlite3 *db;306  const char *zName;307  sqlite3_intarray *pArray;308  int rc = SQLITE_OK;309  char zPtr[100];310 311  if( objc!=3 ){312    Tcl_WrongNumArgs(interp, 1, objv, "DB");313    return TCL_ERROR;314  }315  if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;316  zName = Tcl_GetString(objv[2]);317#ifndef SQLITE_OMIT_VIRTUALTABLE318  rc = sqlite3_intarray_create(db, zName, &pArray);319#endif320  if( rc!=SQLITE_OK ){321    Tcl_AppendResult(interp, sqlite3ErrName(rc), (char*)0);322    return TCL_ERROR;323  }324  sqlite3TestMakePointerStr(interp, zPtr, pArray);325  Tcl_AppendResult(interp, zPtr, (char*)0);326  return TCL_OK;327}328 329/*330**    sqlite3_intarray_bind  INTARRAY  ?VALUE ...?331**332** Invoke the sqlite3_intarray_bind interface on the given array of integers.333*/334static int SQLITE_TCLAPI test_intarray_bind(335  ClientData clientData, /* Not used */336  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */337  int objc,              /* Number of arguments */338  Tcl_Obj *CONST objv[]  /* Command arguments */339){340  sqlite3_intarray *pArray;341  int rc = SQLITE_OK;342  int i, n;343  sqlite3_int64 *a;344 345  if( objc<2 ){346    Tcl_WrongNumArgs(interp, 1, objv, "INTARRAY");347    return TCL_ERROR;348  }349  pArray = (sqlite3_intarray*)sqlite3TestTextToPtr(Tcl_GetString(objv[1]));350  n = objc - 2;351#ifndef SQLITE_OMIT_VIRTUALTABLE352  a = sqlite3_malloc64( sizeof(a[0])*n );353  if( a==0 ){354    Tcl_AppendResult(interp, "SQLITE_NOMEM", (char*)0);355    return TCL_ERROR;356  }357  for(i=0; i<n; i++){358    Tcl_WideInt x = 0;359    Tcl_GetWideIntFromObj(0, objv[i+2], &x);360    a[i] = x;361  }362  rc = sqlite3_intarray_bind(pArray, n, a, sqlite3_free);363  if( rc!=SQLITE_OK ){364    Tcl_AppendResult(interp, sqlite3ErrName(rc), (char*)0);365    return TCL_ERROR;366  }367#endif368  return TCL_OK;369}370 371/*372** Register commands with the TCL interpreter.373*/374int Sqlitetestintarray_Init(Tcl_Interp *interp){375  static struct {376     char *zName;377     Tcl_ObjCmdProc *xProc;378     void *clientData;379  } aObjCmd[] = {380     { "sqlite3_intarray_create", test_intarray_create, 0 },381     { "sqlite3_intarray_bind", test_intarray_bind, 0 },382  };383  int i;384  for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){385    Tcl_CreateObjCommand(interp, aObjCmd[i].zName, 386        aObjCmd[i].xProc, aObjCmd[i].clientData, 0);387  }388  return TCL_OK;389}390 391#endif /* SQLITE_TEST */392