4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
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 demonstrates how to create a table-valued-function that
14 ** returns the values in a C-language array.
17 ** SELECT * FROM carray($ptr,5)
19 ** The query above returns 5 integers contained in a C-language array
20 ** at the address $ptr. $ptr is a pointer to the array of integers.
21 ** The pointer value must be assigned to $ptr using the
22 ** sqlite3_bind_pointer() interface with a pointer type of "carray".
25 ** static int aX[] = { 53, 9, 17, 2231, 4, 99 };
26 ** int i = sqlite3_bind_parameter_index(pStmt, "$ptr");
27 ** sqlite3_bind_pointer(pStmt, i, aX, "carray", 0);
29 ** There is an optional third parameter to determine the datatype of
30 ** the C-language array. Allowed values of the third parameter are
31 ** 'int32', 'int64', 'double', 'char*', 'struct iovec'. Example:
33 ** SELECT * FROM carray($ptr,10,'char*');
35 ** The default value of the third parameter is 'int32'.
39 ** The carray "function" is really a virtual table with the
42 ** CREATE TABLE carray(
49 ** If the hidden columns "pointer" and "count" are unconstrained, then
50 ** the virtual table has no rows. Otherwise, the virtual table interprets
51 ** the integer value of "pointer" as a pointer to the array and "count"
52 ** as the number of elements in the array. The virtual table steps through
53 ** the array, element by element.
55 #include "sqlite3ext.h"
56 SQLITE_EXTENSION_INIT1
68 /* Allowed values for the mFlags parameter to sqlite3_carray_bind().
69 ** Must exactly match the definitions in carray.h.
72 # define CARRAY_INT32 0 /* Data is 32-bit signed integers */
73 # define CARRAY_INT64 1 /* Data is 64-bit signed integers */
74 # define CARRAY_DOUBLE 2 /* Data is doubles */
75 # define CARRAY_TEXT 3 /* Data is char* */
76 # define CARRAY_BLOB 4 /* Data is struct iovec* */
81 # define SQLITE_API __declspec(dllexport)
87 #ifndef SQLITE_OMIT_VIRTUALTABLE
90 ** Names of allowed datatypes
92 static const char *azType
[] = { "int32", "int64", "double", "char*",
96 ** Structure used to hold the sqlite3_carray_bind() information
98 typedef struct carray_bind carray_bind
;
100 void *aData
; /* The data */
101 int nData
; /* Number of elements */
102 int mFlags
; /* Control flags */
103 void (*xDel
)(void*); /* Destructor for aData */
107 /* carray_cursor is a subclass of sqlite3_vtab_cursor which will
108 ** serve as the underlying representation of a cursor that scans
109 ** over rows of the result
111 typedef struct carray_cursor carray_cursor
;
112 struct carray_cursor
{
113 sqlite3_vtab_cursor base
; /* Base class - must be first */
114 sqlite3_int64 iRowid
; /* The rowid */
115 void *pPtr
; /* Pointer to the array of values */
116 sqlite3_int64 iCnt
; /* Number of integers in the array */
117 unsigned char eType
; /* One of the CARRAY_type values */
121 ** The carrayConnect() method is invoked to create a new
122 ** carray_vtab that describes the carray virtual table.
124 ** Think of this routine as the constructor for carray_vtab objects.
126 ** All this routine needs to do is:
128 ** (1) Allocate the carray_vtab object and initialize all fields.
130 ** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
131 ** result set of queries against carray will look like.
133 static int carrayConnect(
136 int argc
, const char *const*argv
,
137 sqlite3_vtab
**ppVtab
,
144 #define CARRAY_COLUMN_VALUE 0
145 #define CARRAY_COLUMN_POINTER 1
146 #define CARRAY_COLUMN_COUNT 2
147 #define CARRAY_COLUMN_CTYPE 3
149 rc
= sqlite3_declare_vtab(db
,
150 "CREATE TABLE x(value,pointer hidden,count hidden,ctype hidden)");
152 pNew
= *ppVtab
= sqlite3_malloc( sizeof(*pNew
) );
153 if( pNew
==0 ) return SQLITE_NOMEM
;
154 memset(pNew
, 0, sizeof(*pNew
));
160 ** This method is the destructor for carray_cursor objects.
162 static int carrayDisconnect(sqlite3_vtab
*pVtab
){
168 ** Constructor for a new carray_cursor object.
170 static int carrayOpen(sqlite3_vtab
*p
, sqlite3_vtab_cursor
**ppCursor
){
172 pCur
= sqlite3_malloc( sizeof(*pCur
) );
173 if( pCur
==0 ) return SQLITE_NOMEM
;
174 memset(pCur
, 0, sizeof(*pCur
));
175 *ppCursor
= &pCur
->base
;
180 ** Destructor for a carray_cursor.
182 static int carrayClose(sqlite3_vtab_cursor
*cur
){
189 ** Advance a carray_cursor to its next row of output.
191 static int carrayNext(sqlite3_vtab_cursor
*cur
){
192 carray_cursor
*pCur
= (carray_cursor
*)cur
;
198 ** Return values of columns for the row at which the carray_cursor
199 ** is currently pointing.
201 static int carrayColumn(
202 sqlite3_vtab_cursor
*cur
, /* The cursor */
203 sqlite3_context
*ctx
, /* First argument to sqlite3_result_...() */
204 int i
/* Which column to return */
206 carray_cursor
*pCur
= (carray_cursor
*)cur
;
209 case CARRAY_COLUMN_POINTER
: return SQLITE_OK
;
210 case CARRAY_COLUMN_COUNT
: x
= pCur
->iCnt
; break;
211 case CARRAY_COLUMN_CTYPE
: {
212 sqlite3_result_text(ctx
, azType
[pCur
->eType
], -1, SQLITE_STATIC
);
216 switch( pCur
->eType
){
218 int *p
= (int*)pCur
->pPtr
;
219 sqlite3_result_int(ctx
, p
[pCur
->iRowid
-1]);
223 sqlite3_int64
*p
= (sqlite3_int64
*)pCur
->pPtr
;
224 sqlite3_result_int64(ctx
, p
[pCur
->iRowid
-1]);
227 case CARRAY_DOUBLE
: {
228 double *p
= (double*)pCur
->pPtr
;
229 sqlite3_result_double(ctx
, p
[pCur
->iRowid
-1]);
233 const char **p
= (const char**)pCur
->pPtr
;
234 sqlite3_result_text(ctx
, p
[pCur
->iRowid
-1], -1, SQLITE_TRANSIENT
);
238 const struct iovec
*p
= (struct iovec
*)pCur
->pPtr
;
239 sqlite3_result_blob(ctx
, p
[pCur
->iRowid
-1].iov_base
,
240 (int)p
[pCur
->iRowid
-1].iov_len
, SQLITE_TRANSIENT
);
246 sqlite3_result_int64(ctx
, x
);
251 ** Return the rowid for the current row. In this implementation, the
252 ** rowid is the same as the output value.
254 static int carrayRowid(sqlite3_vtab_cursor
*cur
, sqlite_int64
*pRowid
){
255 carray_cursor
*pCur
= (carray_cursor
*)cur
;
256 *pRowid
= pCur
->iRowid
;
261 ** Return TRUE if the cursor has been moved off of the last
264 static int carrayEof(sqlite3_vtab_cursor
*cur
){
265 carray_cursor
*pCur
= (carray_cursor
*)cur
;
266 return pCur
->iRowid
>pCur
->iCnt
;
270 ** This method is called to "rewind" the carray_cursor object back
271 ** to the first row of output.
273 static int carrayFilter(
274 sqlite3_vtab_cursor
*pVtabCursor
,
275 int idxNum
, const char *idxStr
,
276 int argc
, sqlite3_value
**argv
278 carray_cursor
*pCur
= (carray_cursor
*)pVtabCursor
;
283 carray_bind
*pBind
= sqlite3_value_pointer(argv
[0], "carray-bind");
284 if( pBind
==0 ) break;
285 pCur
->pPtr
= pBind
->aData
;
286 pCur
->iCnt
= pBind
->nData
;
287 pCur
->eType
= pBind
->mFlags
& 0x07;
292 pCur
->pPtr
= sqlite3_value_pointer(argv
[0], "carray");
293 pCur
->iCnt
= pCur
->pPtr
? sqlite3_value_int64(argv
[1]) : 0;
295 pCur
->eType
= CARRAY_INT32
;
298 const char *zType
= (const char*)sqlite3_value_text(argv
[2]);
299 for(i
=0; i
<sizeof(azType
)/sizeof(azType
[0]); i
++){
300 if( sqlite3_stricmp(zType
, azType
[i
])==0 ) break;
302 if( i
>=sizeof(azType
)/sizeof(azType
[0]) ){
303 pVtabCursor
->pVtab
->zErrMsg
= sqlite3_mprintf(
304 "unknown datatype: %Q", zType
);
318 ** SQLite will invoke this method one or more times while planning a query
319 ** that uses the carray virtual table. This routine needs to create
320 ** a query plan for each invocation and compute an estimated cost for that
323 ** In this implementation idxNum is used to represent the
324 ** query plan. idxStr is unused.
328 ** 1 If only the pointer= constraint exists. In this case, the
329 ** parameter must be bound using sqlite3_carray_bind().
331 ** 2 if the pointer= and count= constraints exist.
333 ** 3 if the ctype= constraint also exists.
335 ** idxNum is 0 otherwise and carray becomes an empty table.
337 static int carrayBestIndex(
339 sqlite3_index_info
*pIdxInfo
341 int i
; /* Loop over constraints */
342 int ptrIdx
= -1; /* Index of the pointer= constraint, or -1 if none */
343 int cntIdx
= -1; /* Index of the count= constraint, or -1 if none */
344 int ctypeIdx
= -1; /* Index of the ctype= constraint, or -1 if none */
346 const struct sqlite3_index_constraint
*pConstraint
;
347 pConstraint
= pIdxInfo
->aConstraint
;
348 for(i
=0; i
<pIdxInfo
->nConstraint
; i
++, pConstraint
++){
349 if( pConstraint
->usable
==0 ) continue;
350 if( pConstraint
->op
!=SQLITE_INDEX_CONSTRAINT_EQ
) continue;
351 switch( pConstraint
->iColumn
){
352 case CARRAY_COLUMN_POINTER
:
355 case CARRAY_COLUMN_COUNT
:
358 case CARRAY_COLUMN_CTYPE
:
364 pIdxInfo
->aConstraintUsage
[ptrIdx
].argvIndex
= 1;
365 pIdxInfo
->aConstraintUsage
[ptrIdx
].omit
= 1;
366 pIdxInfo
->estimatedCost
= (double)1;
367 pIdxInfo
->estimatedRows
= 100;
368 pIdxInfo
->idxNum
= 1;
370 pIdxInfo
->aConstraintUsage
[cntIdx
].argvIndex
= 2;
371 pIdxInfo
->aConstraintUsage
[cntIdx
].omit
= 1;
372 pIdxInfo
->idxNum
= 2;
374 pIdxInfo
->aConstraintUsage
[ctypeIdx
].argvIndex
= 3;
375 pIdxInfo
->aConstraintUsage
[ctypeIdx
].omit
= 1;
376 pIdxInfo
->idxNum
= 3;
380 pIdxInfo
->estimatedCost
= (double)2147483647;
381 pIdxInfo
->estimatedRows
= 2147483647;
382 pIdxInfo
->idxNum
= 0;
388 ** This following structure defines all the methods for the
389 ** carray virtual table.
391 static sqlite3_module carrayModule
= {
394 carrayConnect
, /* xConnect */
395 carrayBestIndex
, /* xBestIndex */
396 carrayDisconnect
, /* xDisconnect */
398 carrayOpen
, /* xOpen - open a cursor */
399 carrayClose
, /* xClose - close a cursor */
400 carrayFilter
, /* xFilter - configure scan constraints */
401 carrayNext
, /* xNext - advance a cursor */
402 carrayEof
, /* xEof - check for end of scan */
403 carrayColumn
, /* xColumn - read data */
404 carrayRowid
, /* xRowid - read data */
420 ** Destructor for the carray_bind object
422 static void carrayBindDel(void *pPtr
){
423 carray_bind
*p
= (carray_bind
*)pPtr
;
424 if( p
->xDel
!=SQLITE_STATIC
){
431 ** Invoke this interface in order to bind to the single-argument
432 ** version of CARRAY().
434 SQLITE_API
int sqlite3_carray_bind(
440 void (*xDestroy
)(void*)
444 pNew
= sqlite3_malloc64(sizeof(*pNew
));
446 if( xDestroy
!=SQLITE_STATIC
&& xDestroy
!=SQLITE_TRANSIENT
){
452 pNew
->mFlags
= mFlags
;
453 if( xDestroy
==SQLITE_TRANSIENT
){
454 sqlite3_int64 sz
= nData
;
455 switch( mFlags
& 0x07 ){
456 case CARRAY_INT32
: sz
*= 4; break;
457 case CARRAY_INT64
: sz
*= 8; break;
458 case CARRAY_DOUBLE
: sz
*= 8; break;
459 case CARRAY_TEXT
: sz
*= sizeof(char*); break;
460 case CARRAY_BLOB
: sz
*= sizeof(struct iovec
); break;
462 if( (mFlags
& 0x07)==CARRAY_TEXT
){
463 for(i
=0; i
<nData
; i
++){
464 const char *z
= ((char**)aData
)[i
];
465 if( z
) sz
+= strlen(z
) + 1;
467 }else if( (mFlags
& 0x07)==CARRAY_BLOB
){
468 for(i
=0; i
<nData
; i
++){
469 sz
+= ((struct iovec
*)aData
)[i
].iov_len
;
472 pNew
->aData
= sqlite3_malloc64( sz
);
473 if( pNew
->aData
==0 ){
477 if( (mFlags
& 0x07)==CARRAY_TEXT
){
478 char **az
= (char**)pNew
->aData
;
479 char *z
= (char*)&az
[nData
];
480 for(i
=0; i
<nData
; i
++){
481 const char *zData
= ((char**)aData
)[i
];
489 memcpy(z
, zData
, n
+1);
492 }else if( (mFlags
& 0x07)==CARRAY_BLOB
){
493 struct iovec
*p
= (struct iovec
*)pNew
->aData
;
494 unsigned char *z
= (unsigned char*)&p
[nData
];
495 for(i
=0; i
<nData
; i
++){
496 size_t n
= ((struct iovec
*)aData
)[i
].iov_len
;
500 memcpy(p
[i
].iov_base
, ((struct iovec
*)aData
)[i
].iov_base
, n
);
503 memcpy(pNew
->aData
, aData
, sz
);
505 pNew
->xDel
= sqlite3_free
;
508 pNew
->xDel
= xDestroy
;
510 return sqlite3_bind_pointer(pStmt
, idx
, pNew
, "carray-bind", carrayBindDel
);
515 ** For testing purpose in the TCL test harness, we need a method for
516 ** setting the pointer value. The inttoptr(X) SQL function accomplishes
517 ** this. Tcl script will bind an integer to X and the inttoptr() SQL
518 ** function will use sqlite3_result_pointer() to convert that integer into
521 ** This is for testing on TCL only.
524 static void inttoptrFunc(
525 sqlite3_context
*context
,
531 i64
= sqlite3_value_int64(argv
[0]);
532 if( sizeof(i64
)==sizeof(p
) ){
533 memcpy(&p
, &i64
, sizeof(p
));
535 int i32
= i64
& 0xffffffff;
536 memcpy(&p
, &i32
, sizeof(p
));
538 sqlite3_result_pointer(context
, p
, "carray", 0);
540 #endif /* SQLITE_TEST */
542 #endif /* SQLITE_OMIT_VIRTUALTABLE */
544 SQLITE_API
int sqlite3_carray_init(
547 const sqlite3_api_routines
*pApi
550 SQLITE_EXTENSION_INIT2(pApi
);
551 #ifndef SQLITE_OMIT_VIRTUALTABLE
552 rc
= sqlite3_create_module(db
, "carray", &carrayModule
, 0);
555 rc
= sqlite3_create_function(db
, "inttoptr", 1, SQLITE_UTF8
, 0,
558 #endif /* SQLITE_TEST */
559 #endif /* SQLITE_OMIT_VIRTUALTABLE */