Thanks to visit codestin.com
Credit goes to chromium.googlesource.com

blob: 9e4629467e6de729007d2942daaee3ea42063748 [file] [log] [blame]
drh522efc62009-11-10 17:24:371/*
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.
drhf8181ea2018-10-31 18:24:2916**
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.
drh522efc62009-11-10 17:24:3720*/
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*/
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
peter.d.reid60ec9142014-09-06 16:39:4644/* An intarray table object */
drh522efc62009-11-10 17:24:3745struct intarray_vtab {
46 sqlite3_vtab base; /* Base class */
47 sqlite3_intarray *pContent; /* Content of the integer array */
48};
49
peter.d.reid60ec9142014-09-06 16:39:4650/* An intarray cursor object */
drh522efc62009-11-10 17:24:3751struct 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/*
drh63b38782009-11-10 17:55:4762** Free an sqlite3_intarray object.
drh522efc62009-11-10 17:24:3763*/
drh43afab22025-01-13 11:28:3464static void intarrayFree(void *pX){
65 sqlite3_intarray *p = (sqlite3_intarray*)pX;
drh522efc62009-11-10 17:24:3766 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;
drhf3cdcdc2015-04-29 16:50:2893 intarray_vtab *pVtab = sqlite3_malloc64(sizeof(intarray_vtab));
drh522efc62009-11-10 17:24:3794
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;
drhf3cdcdc2015-04-29 16:50:28110 pCur = sqlite3_malloc64(sizeof(intarray_cursor));
drh522efc62009-11-10 17:24:37111 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 **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*/
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 TCL
186** 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 */
drh19358872023-10-06 12:51:05209 0, /* xSavepoint */
210 0, /* xRelease */
211 0, /* xRollbackTo */
212 0, /* xShadowName */
213 0 /* xIntegrity */
drh522efc62009-11-10 17:24:37214};
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*/
drhfb908412014-08-20 13:25:06229SQLITE_API int sqlite3_intarray_create(
drh522efc62009-11-10 17:24:37230 sqlite3 *db,
231 const char *zName,
232 sqlite3_intarray **ppReturn
233){
drh5c03f302009-11-13 15:03:59234 int rc = SQLITE_OK;
235#ifndef SQLITE_OMIT_VIRTUALTABLE
drh522efc62009-11-10 17:24:37236 sqlite3_intarray *p;
237
drhf3cdcdc2015-04-29 16:50:28238 *ppReturn = p = sqlite3_malloc64( sizeof(*p) );
drh522efc62009-11-10 17:24:37239 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 }
drh5c03f302009-11-13 15:03:59252#endif
drh522efc62009-11-10 17:24:37253 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*/
drhfb908412014-08-20 13:25:06263SQLITE_API int sqlite3_intarray_bind(
drh522efc62009-11-10 17:24:37264 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
drh064b6812024-07-30 15:49:02283#include "tclsqlite.h"
drh522efc62009-11-10 17:24:37284
285/*
286** Routines to encode and decode pointers
287*/
288extern int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb);
drhd8267b82009-12-01 18:46:06289extern void *sqlite3TestTextToPtr(const char*);
drh522efc62009-11-10 17:24:37290extern int sqlite3TestMakePointerStr(Tcl_Interp*, char *zPtr, void*);
mistachkine84d8d32013-04-29 03:09:10291extern const char *sqlite3ErrName(int);
drh522efc62009-11-10 17:24:37292
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*/
mistachkin7617e4a2016-07-28 17:11:20299static int SQLITE_TCLAPI test_intarray_create(
drh522efc62009-11-10 17:24:37300 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;
drh5c03f302009-11-13 15:03:59308 int rc = SQLITE_OK;
drh522efc62009-11-10 17:24:37309 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]);
drh5c03f302009-11-13 15:03:59317#ifndef SQLITE_OMIT_VIRTUALTABLE
drh522efc62009-11-10 17:24:37318 rc = sqlite3_intarray_create(db, zName, &pArray);
drh5c03f302009-11-13 15:03:59319#endif
drh522efc62009-11-10 17:24:37320 if( rc!=SQLITE_OK ){
mistachkine84d8d32013-04-29 03:09:10321 Tcl_AppendResult(interp, sqlite3ErrName(rc), (char*)0);
drh522efc62009-11-10 17:24:37322 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*/
mistachkin7617e4a2016-07-28 17:11:20334static int SQLITE_TCLAPI test_intarray_bind(
drh522efc62009-11-10 17:24:37335 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;
drh5c03f302009-11-13 15:03:59341 int rc = SQLITE_OK;
drh522efc62009-11-10 17:24:37342 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;
drh5c03f302009-11-13 15:03:59351#ifndef SQLITE_OMIT_VIRTUALTABLE
drhf3cdcdc2015-04-29 16:50:28352 a = sqlite3_malloc64( sizeof(a[0])*n );
drh522efc62009-11-10 17:24:37353 if( a==0 ){
354 Tcl_AppendResult(interp, "SQLITE_NOMEM", (char*)0);
355 return TCL_ERROR;
356 }
357 for(i=0; i<n; i++){
drhb3f787f2012-09-29 14:45:54358 Tcl_WideInt x = 0;
359 Tcl_GetWideIntFromObj(0, objv[i+2], &x);
360 a[i] = x;
drh522efc62009-11-10 17:24:37361 }
362 rc = sqlite3_intarray_bind(pArray, n, a, sqlite3_free);
363 if( rc!=SQLITE_OK ){
mistachkine84d8d32013-04-29 03:09:10364 Tcl_AppendResult(interp, sqlite3ErrName(rc), (char*)0);
drh522efc62009-11-10 17:24:37365 return TCL_ERROR;
366 }
drh5c03f302009-11-13 15:03:59367#endif
drh522efc62009-11-10 17:24:37368 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 */