drh | 522efc6 | 2009-11-10 17:24:37 | [diff] [blame] | 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. |
drh | f8181ea | 2018-10-31 18:24:29 | [diff] [blame] | 16 | ** |
| 17 | ** This virtual table is used for internal testing of SQLite only. It is |
| 18 | ** not recommended for use in production. For a similar virtual table that |
| 19 | ** is production-ready, see the "carray" virtual table over in ext/misc. |
drh | 522efc6 | 2009-11-10 17:24:37 | [diff] [blame] | 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 subject |
| 30 | ** to change, is not externally visible, and should be used by |
| 31 | ** the implementation of intarray only. This object is opaque |
| 32 | ** to users. |
| 33 | */ |
| 34 | struct 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 */ |
| 41 | typedef struct intarray_vtab intarray_vtab; |
| 42 | typedef struct intarray_cursor intarray_cursor; |
| 43 | |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 | [diff] [blame] | 44 | /* An intarray table object */ |
drh | 522efc6 | 2009-11-10 17:24:37 | [diff] [blame] | 45 | struct intarray_vtab { |
| 46 | sqlite3_vtab base; /* Base class */ |
| 47 | sqlite3_intarray *pContent; /* Content of the integer array */ |
| 48 | }; |
| 49 | |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 | [diff] [blame] | 50 | /* An intarray cursor object */ |
drh | 522efc6 | 2009-11-10 17:24:37 | [diff] [blame] | 51 | struct 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_VIRTUALTABLE |
| 60 | |
| 61 | /* |
drh | 63b3878 | 2009-11-10 17:55:47 | [diff] [blame] | 62 | ** Free an sqlite3_intarray object. |
drh | 522efc6 | 2009-11-10 17:24:37 | [diff] [blame] | 63 | */ |
drh | 43afab2 | 2025-01-13 11:28:34 | [diff] [blame] | 64 | static void intarrayFree(void *pX){ |
| 65 | sqlite3_intarray *p = (sqlite3_intarray*)pX; |
drh | 522efc6 | 2009-11-10 17:24:37 | [diff] [blame] | 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 | */ |
| 75 | static 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 | */ |
| 84 | static 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; |
drh | f3cdcdc | 2015-04-29 16:50:28 | [diff] [blame] | 93 | intarray_vtab *pVtab = sqlite3_malloc64(sizeof(intarray_vtab)); |
drh | 522efc6 | 2009-11-10 17:24:37 | [diff] [blame] | 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 | */ |
| 107 | static int intarrayOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){ |
| 108 | int rc = SQLITE_NOMEM; |
| 109 | intarray_cursor *pCur; |
drh | f3cdcdc | 2015-04-29 16:50:28 | [diff] [blame] | 110 | pCur = sqlite3_malloc64(sizeof(intarray_cursor)); |
drh | 522efc6 | 2009-11-10 17:24:37 | [diff] [blame] | 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 | */ |
| 122 | static 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 | */ |
| 131 | static 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 | */ |
| 143 | static 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 | |
| 149 | static 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 | */ |
| 158 | static 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 | */ |
| 167 | static int intarrayFilter( |
| 168 | sqlite3_vtab_cursor *pVtabCursor, |
| 169 | int idxNum, const char *idxStr, |
| 170 | int argc, sqlite3_value **argv |
| 171 | ){ |
| 172 | intarray_cursor *pCur = (intarray_cursor *)pVtabCursor; |
| 173 | pCur->i = 0; |
| 174 | return SQLITE_OK; |
| 175 | } |
| 176 | |
| 177 | /* |
| 178 | ** Analyse the WHERE condition. |
| 179 | */ |
| 180 | static 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 TCL |
| 186 | ** variables. |
| 187 | */ |
| 188 | static 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 */ |
drh | 1935887 | 2023-10-06 12:51:05 | [diff] [blame] | 209 | 0, /* xSavepoint */ |
| 210 | 0, /* xRelease */ |
| 211 | 0, /* xRollbackTo */ |
| 212 | 0, /* xShadowName */ |
| 213 | 0 /* xIntegrity */ |
drh | 522efc6 | 2009-11-10 17:24:37 | [diff] [blame] | 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 table |
| 223 | ** with a name of zName. |
| 224 | ** |
| 225 | ** Destroy the intarray object by dropping the virtual table. If not done |
| 226 | ** explicitly by the application, the virtual table will be dropped implicitly |
| 227 | ** by the system when the database connection is closed. |
| 228 | */ |
drh | fb90841 | 2014-08-20 13:25:06 | [diff] [blame] | 229 | SQLITE_API int sqlite3_intarray_create( |
drh | 522efc6 | 2009-11-10 17:24:37 | [diff] [blame] | 230 | sqlite3 *db, |
| 231 | const char *zName, |
| 232 | sqlite3_intarray **ppReturn |
| 233 | ){ |
drh | 5c03f30 | 2009-11-13 15:03:59 | [diff] [blame] | 234 | int rc = SQLITE_OK; |
| 235 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 522efc6 | 2009-11-10 17:24:37 | [diff] [blame] | 236 | sqlite3_intarray *p; |
| 237 | |
drh | f3cdcdc | 2015-04-29 16:50:28 | [diff] [blame] | 238 | *ppReturn = p = sqlite3_malloc64( sizeof(*p) ); |
drh | 522efc6 | 2009-11-10 17:24:37 | [diff] [blame] | 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 | } |
drh | 5c03f30 | 2009-11-13 15:03:59 | [diff] [blame] | 252 | #endif |
drh | 522efc6 | 2009-11-10 17:24:37 | [diff] [blame] | 253 | 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 of |
| 260 | ** any query against the corresponding virtual table. If the integer |
| 261 | ** array does change or is deallocated undefined behavior will result. |
| 262 | */ |
drh | fb90841 | 2014-08-20 13:25:06 | [diff] [blame] | 263 | SQLITE_API int sqlite3_intarray_bind( |
drh | 522efc6 | 2009-11-10 17:24:37 | [diff] [blame] | 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_TEST |
drh | 064b681 | 2024-07-30 15:49:02 | [diff] [blame] | 283 | #include "tclsqlite.h" |
drh | 522efc6 | 2009-11-10 17:24:37 | [diff] [blame] | 284 | |
| 285 | /* |
| 286 | ** Routines to encode and decode pointers |
| 287 | */ |
| 288 | extern int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb); |
drh | d8267b8 | 2009-12-01 18:46:06 | [diff] [blame] | 289 | extern void *sqlite3TestTextToPtr(const char*); |
drh | 522efc6 | 2009-11-10 17:24:37 | [diff] [blame] | 290 | extern int sqlite3TestMakePointerStr(Tcl_Interp*, char *zPtr, void*); |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 | [diff] [blame] | 291 | extern const char *sqlite3ErrName(int); |
drh | 522efc6 | 2009-11-10 17:24:37 | [diff] [blame] | 292 | |
| 293 | /* |
| 294 | ** sqlite3_intarray_create DB NAME |
| 295 | ** |
| 296 | ** Invoke the sqlite3_intarray_create interface. A string that becomes |
| 297 | ** the first parameter to sqlite3_intarray_bind. |
| 298 | */ |
mistachkin | 7617e4a | 2016-07-28 17:11:20 | [diff] [blame] | 299 | static int SQLITE_TCLAPI test_intarray_create( |
drh | 522efc6 | 2009-11-10 17:24:37 | [diff] [blame] | 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; |
drh | 5c03f30 | 2009-11-13 15:03:59 | [diff] [blame] | 308 | int rc = SQLITE_OK; |
drh | 522efc6 | 2009-11-10 17:24:37 | [diff] [blame] | 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]); |
drh | 5c03f30 | 2009-11-13 15:03:59 | [diff] [blame] | 317 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 522efc6 | 2009-11-10 17:24:37 | [diff] [blame] | 318 | rc = sqlite3_intarray_create(db, zName, &pArray); |
drh | 5c03f30 | 2009-11-13 15:03:59 | [diff] [blame] | 319 | #endif |
drh | 522efc6 | 2009-11-10 17:24:37 | [diff] [blame] | 320 | if( rc!=SQLITE_OK ){ |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 | [diff] [blame] | 321 | Tcl_AppendResult(interp, sqlite3ErrName(rc), (char*)0); |
drh | 522efc6 | 2009-11-10 17:24:37 | [diff] [blame] | 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 | */ |
mistachkin | 7617e4a | 2016-07-28 17:11:20 | [diff] [blame] | 334 | static int SQLITE_TCLAPI test_intarray_bind( |
drh | 522efc6 | 2009-11-10 17:24:37 | [diff] [blame] | 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; |
drh | 5c03f30 | 2009-11-13 15:03:59 | [diff] [blame] | 341 | int rc = SQLITE_OK; |
drh | 522efc6 | 2009-11-10 17:24:37 | [diff] [blame] | 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; |
drh | 5c03f30 | 2009-11-13 15:03:59 | [diff] [blame] | 351 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | f3cdcdc | 2015-04-29 16:50:28 | [diff] [blame] | 352 | a = sqlite3_malloc64( sizeof(a[0])*n ); |
drh | 522efc6 | 2009-11-10 17:24:37 | [diff] [blame] | 353 | if( a==0 ){ |
| 354 | Tcl_AppendResult(interp, "SQLITE_NOMEM", (char*)0); |
| 355 | return TCL_ERROR; |
| 356 | } |
| 357 | for(i=0; i<n; i++){ |
drh | b3f787f | 2012-09-29 14:45:54 | [diff] [blame] | 358 | Tcl_WideInt x = 0; |
| 359 | Tcl_GetWideIntFromObj(0, objv[i+2], &x); |
| 360 | a[i] = x; |
drh | 522efc6 | 2009-11-10 17:24:37 | [diff] [blame] | 361 | } |
| 362 | rc = sqlite3_intarray_bind(pArray, n, a, sqlite3_free); |
| 363 | if( rc!=SQLITE_OK ){ |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 | [diff] [blame] | 364 | Tcl_AppendResult(interp, sqlite3ErrName(rc), (char*)0); |
drh | 522efc6 | 2009-11-10 17:24:37 | [diff] [blame] | 365 | return TCL_ERROR; |
| 366 | } |
drh | 5c03f30 | 2009-11-13 15:03:59 | [diff] [blame] | 367 | #endif |
drh | 522efc6 | 2009-11-10 17:24:37 | [diff] [blame] | 368 | return TCL_OK; |
| 369 | } |
| 370 | |
| 371 | /* |
| 372 | ** Register commands with the TCL interpreter. |
| 373 | */ |
| 374 | int 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 */ |