Fix rounding in zero-precision %f and %g printf conversions.
[sqlite.git] / src / test_intarray.c
bloba978ed585d4a359a12fb961a856356031a299686
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.
11 *************************************************************************
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.
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.
21 #include "test_intarray.h"
22 #include <string.h>
23 #include <assert.h>
27 ** Definition of the sqlite3_intarray object.
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.
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[] */
40 /* Objects used internally by the virtual table implementation */
41 typedef struct intarray_vtab intarray_vtab;
42 typedef struct intarray_cursor intarray_cursor;
44 /* An intarray table object */
45 struct intarray_vtab {
46 sqlite3_vtab base; /* Base class */
47 sqlite3_intarray *pContent; /* Content of the integer array */
50 /* An intarray cursor object */
51 struct intarray_cursor {
52 sqlite3_vtab_cursor base; /* Base class */
53 int i; /* Current cursor position */
57 ** None of this works unless we have virtual tables.
59 #ifndef SQLITE_OMIT_VIRTUALTABLE
62 ** Free an sqlite3_intarray object.
64 static void intarrayFree(sqlite3_intarray *p){
65 if( p->xFree ){
66 p->xFree(p->a);
68 sqlite3_free(p);
72 ** Table destructor for the intarray module.
74 static int intarrayDestroy(sqlite3_vtab *p){
75 intarray_vtab *pVtab = (intarray_vtab*)p;
76 sqlite3_free(pVtab);
77 return 0;
81 ** Table constructor for the intarray module.
83 static int intarrayCreate(
84 sqlite3 *db, /* Database where module is created */
85 void *pAux, /* clientdata for the module */
86 int argc, /* Number of arguments */
87 const char *const*argv, /* Value for all arguments */
88 sqlite3_vtab **ppVtab, /* Write the new virtual table object here */
89 char **pzErr /* Put error message text here */
91 int rc = SQLITE_NOMEM;
92 intarray_vtab *pVtab = sqlite3_malloc64(sizeof(intarray_vtab));
94 if( pVtab ){
95 memset(pVtab, 0, sizeof(intarray_vtab));
96 pVtab->pContent = (sqlite3_intarray*)pAux;
97 rc = sqlite3_declare_vtab(db, "CREATE TABLE x(value INTEGER PRIMARY KEY)");
99 *ppVtab = (sqlite3_vtab *)pVtab;
100 return rc;
104 ** Open a new cursor on the intarray table.
106 static int intarrayOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
107 int rc = SQLITE_NOMEM;
108 intarray_cursor *pCur;
109 pCur = sqlite3_malloc64(sizeof(intarray_cursor));
110 if( pCur ){
111 memset(pCur, 0, sizeof(intarray_cursor));
112 *ppCursor = (sqlite3_vtab_cursor *)pCur;
113 rc = SQLITE_OK;
115 return rc;
119 ** Close a intarray table cursor.
121 static int intarrayClose(sqlite3_vtab_cursor *cur){
122 intarray_cursor *pCur = (intarray_cursor *)cur;
123 sqlite3_free(pCur);
124 return SQLITE_OK;
128 ** Retrieve a column of data.
130 static int intarrayColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
131 intarray_cursor *pCur = (intarray_cursor*)cur;
132 intarray_vtab *pVtab = (intarray_vtab*)cur->pVtab;
133 if( pCur->i>=0 && pCur->i<pVtab->pContent->n ){
134 sqlite3_result_int64(ctx, pVtab->pContent->a[pCur->i]);
136 return SQLITE_OK;
140 ** Retrieve the current rowid.
142 static int intarrayRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
143 intarray_cursor *pCur = (intarray_cursor *)cur;
144 *pRowid = pCur->i;
145 return SQLITE_OK;
148 static int intarrayEof(sqlite3_vtab_cursor *cur){
149 intarray_cursor *pCur = (intarray_cursor *)cur;
150 intarray_vtab *pVtab = (intarray_vtab *)cur->pVtab;
151 return pCur->i>=pVtab->pContent->n;
155 ** Advance the cursor to the next row.
157 static int intarrayNext(sqlite3_vtab_cursor *cur){
158 intarray_cursor *pCur = (intarray_cursor *)cur;
159 pCur->i++;
160 return SQLITE_OK;
164 ** Reset a intarray table cursor.
166 static int intarrayFilter(
167 sqlite3_vtab_cursor *pVtabCursor,
168 int idxNum, const char *idxStr,
169 int argc, sqlite3_value **argv
171 intarray_cursor *pCur = (intarray_cursor *)pVtabCursor;
172 pCur->i = 0;
173 return SQLITE_OK;
177 ** Analyse the WHERE condition.
179 static int intarrayBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
180 return SQLITE_OK;
184 ** A virtual table module that merely echos method calls into TCL
185 ** variables.
187 static sqlite3_module intarrayModule = {
188 0, /* iVersion */
189 intarrayCreate, /* xCreate - create a new virtual table */
190 intarrayCreate, /* xConnect - connect to an existing vtab */
191 intarrayBestIndex, /* xBestIndex - find the best query index */
192 intarrayDestroy, /* xDisconnect - disconnect a vtab */
193 intarrayDestroy, /* xDestroy - destroy a vtab */
194 intarrayOpen, /* xOpen - open a cursor */
195 intarrayClose, /* xClose - close a cursor */
196 intarrayFilter, /* xFilter - configure scan constraints */
197 intarrayNext, /* xNext - advance a cursor */
198 intarrayEof, /* xEof */
199 intarrayColumn, /* xColumn - read data */
200 intarrayRowid, /* xRowid - read data */
201 0, /* xUpdate */
202 0, /* xBegin */
203 0, /* xSync */
204 0, /* xCommit */
205 0, /* xRollback */
206 0, /* xFindMethod */
207 0, /* xRename */
208 0, /* xSavepoint */
209 0, /* xRelease */
210 0, /* xRollbackTo */
211 0, /* xShadowName */
212 0 /* xIntegrity */
215 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */
218 ** Invoke this routine to create a specific instance of an intarray object.
219 ** The new intarray object is returned by the 3rd parameter.
221 ** Each intarray object corresponds to a virtual table in the TEMP table
222 ** with a name of zName.
224 ** Destroy the intarray object by dropping the virtual table. If not done
225 ** explicitly by the application, the virtual table will be dropped implicitly
226 ** by the system when the database connection is closed.
228 SQLITE_API int sqlite3_intarray_create(
229 sqlite3 *db,
230 const char *zName,
231 sqlite3_intarray **ppReturn
233 int rc = SQLITE_OK;
234 #ifndef SQLITE_OMIT_VIRTUALTABLE
235 sqlite3_intarray *p;
237 *ppReturn = p = sqlite3_malloc64( sizeof(*p) );
238 if( p==0 ){
239 return SQLITE_NOMEM;
241 memset(p, 0, sizeof(*p));
242 rc = sqlite3_create_module_v2(db, zName, &intarrayModule, p,
243 (void(*)(void*))intarrayFree);
244 if( rc==SQLITE_OK ){
245 char *zSql;
246 zSql = sqlite3_mprintf("CREATE VIRTUAL TABLE temp.%Q USING %Q",
247 zName, zName);
248 rc = sqlite3_exec(db, zSql, 0, 0, 0);
249 sqlite3_free(zSql);
251 #endif
252 return rc;
256 ** Bind a new array array of integers to a specific intarray object.
258 ** The array of integers bound must be unchanged for the duration of
259 ** any query against the corresponding virtual table. If the integer
260 ** array does change or is deallocated undefined behavior will result.
262 SQLITE_API int sqlite3_intarray_bind(
263 sqlite3_intarray *pIntArray, /* The intarray object to bind to */
264 int nElements, /* Number of elements in the intarray */
265 sqlite3_int64 *aElements, /* Content of the intarray */
266 void (*xFree)(void*) /* How to dispose of the intarray when done */
268 if( pIntArray->xFree ){
269 pIntArray->xFree(pIntArray->a);
271 pIntArray->n = nElements;
272 pIntArray->a = aElements;
273 pIntArray->xFree = xFree;
274 return SQLITE_OK;
278 /*****************************************************************************
279 ** Everything below is interface for testing this module.
281 #ifdef SQLITE_TEST
282 #if defined(INCLUDE_SQLITE_TCL_H)
283 # include "sqlite_tcl.h"
284 #else
285 # include "tcl.h"
286 # ifndef SQLITE_TCLAPI
287 # define SQLITE_TCLAPI
288 # endif
289 #endif
292 ** Routines to encode and decode pointers
294 extern int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb);
295 extern void *sqlite3TestTextToPtr(const char*);
296 extern int sqlite3TestMakePointerStr(Tcl_Interp*, char *zPtr, void*);
297 extern const char *sqlite3ErrName(int);
300 ** sqlite3_intarray_create DB NAME
302 ** Invoke the sqlite3_intarray_create interface. A string that becomes
303 ** the first parameter to sqlite3_intarray_bind.
305 static int SQLITE_TCLAPI test_intarray_create(
306 ClientData clientData, /* Not used */
307 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
308 int objc, /* Number of arguments */
309 Tcl_Obj *CONST objv[] /* Command arguments */
311 sqlite3 *db;
312 const char *zName;
313 sqlite3_intarray *pArray;
314 int rc = SQLITE_OK;
315 char zPtr[100];
317 if( objc!=3 ){
318 Tcl_WrongNumArgs(interp, 1, objv, "DB");
319 return TCL_ERROR;
321 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
322 zName = Tcl_GetString(objv[2]);
323 #ifndef SQLITE_OMIT_VIRTUALTABLE
324 rc = sqlite3_intarray_create(db, zName, &pArray);
325 #endif
326 if( rc!=SQLITE_OK ){
327 Tcl_AppendResult(interp, sqlite3ErrName(rc), (char*)0);
328 return TCL_ERROR;
330 sqlite3TestMakePointerStr(interp, zPtr, pArray);
331 Tcl_AppendResult(interp, zPtr, (char*)0);
332 return TCL_OK;
336 ** sqlite3_intarray_bind INTARRAY ?VALUE ...?
338 ** Invoke the sqlite3_intarray_bind interface on the given array of integers.
340 static int SQLITE_TCLAPI test_intarray_bind(
341 ClientData clientData, /* Not used */
342 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
343 int objc, /* Number of arguments */
344 Tcl_Obj *CONST objv[] /* Command arguments */
346 sqlite3_intarray *pArray;
347 int rc = SQLITE_OK;
348 int i, n;
349 sqlite3_int64 *a;
351 if( objc<2 ){
352 Tcl_WrongNumArgs(interp, 1, objv, "INTARRAY");
353 return TCL_ERROR;
355 pArray = (sqlite3_intarray*)sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
356 n = objc - 2;
357 #ifndef SQLITE_OMIT_VIRTUALTABLE
358 a = sqlite3_malloc64( sizeof(a[0])*n );
359 if( a==0 ){
360 Tcl_AppendResult(interp, "SQLITE_NOMEM", (char*)0);
361 return TCL_ERROR;
363 for(i=0; i<n; i++){
364 Tcl_WideInt x = 0;
365 Tcl_GetWideIntFromObj(0, objv[i+2], &x);
366 a[i] = x;
368 rc = sqlite3_intarray_bind(pArray, n, a, sqlite3_free);
369 if( rc!=SQLITE_OK ){
370 Tcl_AppendResult(interp, sqlite3ErrName(rc), (char*)0);
371 return TCL_ERROR;
373 #endif
374 return TCL_OK;
378 ** Register commands with the TCL interpreter.
380 int Sqlitetestintarray_Init(Tcl_Interp *interp){
381 static struct {
382 char *zName;
383 Tcl_ObjCmdProc *xProc;
384 void *clientData;
385 } aObjCmd[] = {
386 { "sqlite3_intarray_create", test_intarray_create, 0 },
387 { "sqlite3_intarray_bind", test_intarray_bind, 0 },
389 int i;
390 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
391 Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
392 aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
394 return TCL_OK;
397 #endif /* SQLITE_TEST */