test_intarray.c 11 KB

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