interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / src / carray.c

567 lines · 17179 bytes Raw Blame History
1/*
2** 2016-06-29
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 table-valued-function that
14** returns the values in a C-language array.
15** Examples:
16**
17** SELECT * FROM carray($ptr,5)
18**
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".
23** For example:
24**
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);
28**
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:
32**
33** SELECT * FROM carray($ptr,10,'char*');
34**
35** The default value of the third parameter is 'int32'.
36**
37** HOW IT WORKS
38**
39** The carray "function" is really a virtual table with the
40** following schema:
41**
42** CREATE TABLE carray(
43** value,
44** pointer HIDDEN,
45** count HIDDEN,
46** ctype TEXT HIDDEN
47** );
48**
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.
54*/
55#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_CARRAY)
56#include "sqliteInt.h"
57#if defined(_WIN32) || defined(__RTP__) || defined(_WRS_KERNEL)
58 struct iovec {
59 void *iov_base;
60 size_t iov_len;
61 };
62#else
63# include <sys/uio.h>
64#endif
65
66/*
67** Names of allowed datatypes
68*/
69static const char *azCarrayType[] = {
70 "int32", "int64", "double", "char*", "struct iovec"
71};
72
73/*
74** Structure used to hold the sqlite3_carray_bind() information
75*/
76typedef struct carray_bind carray_bind;
77struct carray_bind {
78 void *aData; /* The data */
79 int nData; /* Number of elements */
80 int mFlags; /* Control flags */
81 void (*xDel)(void*); /* Destructor for aData */
82 void *pDel; /* Alternative argument to xDel() */
83};
84
85
86/* carray_cursor is a subclass of sqlite3_vtab_cursor which will
87** serve as the underlying representation of a cursor that scans
88** over rows of the result
89*/
90typedef struct carray_cursor carray_cursor;
91struct carray_cursor {
92 sqlite3_vtab_cursor base; /* Base class - must be first */
93 sqlite3_int64 iRowid; /* The rowid */
94 void *pPtr; /* Pointer to the array of values */
95 sqlite3_int64 iCnt; /* Number of integers in the array */
96 unsigned char eType; /* One of the CARRAY_type values */
97};
98
99/*
100** The carrayConnect() method is invoked to create a new
101** carray_vtab that describes the carray virtual table.
102**
103** Think of this routine as the constructor for carray_vtab objects.
104**
105** All this routine needs to do is:
106**
107** (1) Allocate the carray_vtab object and initialize all fields.
108**
109** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
110** result set of queries against carray will look like.
111*/
112static int carrayConnect(
113 sqlite3 *db,
114 void *pAux,
115 int argc, const char *const*argv,
116 sqlite3_vtab **ppVtab,
117 char **pzErr
118){
119 sqlite3_vtab *pNew;
120 int rc;
121
122/* Column numbers */
123#define CARRAY_COLUMN_VALUE 0
124#define CARRAY_COLUMN_POINTER 1
125#define CARRAY_COLUMN_COUNT 2
126#define CARRAY_COLUMN_CTYPE 3
127
128 rc = sqlite3_declare_vtab(db,
129 "CREATE TABLE x(value,pointer hidden,count hidden,ctype hidden)");
130 if( rc==SQLITE_OK ){
131 pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) );
132 if( pNew==0 ) return SQLITE_NOMEM;
133 memset(pNew, 0, sizeof(*pNew));
134 }
135 return rc;
136}
137
138/*
139** This method is the destructor for carray_cursor objects.
140*/
141static int carrayDisconnect(sqlite3_vtab *pVtab){
142 sqlite3_free(pVtab);
143 return SQLITE_OK;
144}
145
146/*
147** Constructor for a new carray_cursor object.
148*/
149static int carrayOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
150 carray_cursor *pCur;
151 pCur = sqlite3_malloc( sizeof(*pCur) );
152 if( pCur==0 ) return SQLITE_NOMEM;
153 memset(pCur, 0, sizeof(*pCur));
154 *ppCursor = &pCur->base;
155 return SQLITE_OK;
156}
157
158/*
159** Destructor for a carray_cursor.
160*/
161static int carrayClose(sqlite3_vtab_cursor *cur){
162 sqlite3_free(cur);
163 return SQLITE_OK;
164}
165
166
167/*
168** Advance a carray_cursor to its next row of output.
169*/
170static int carrayNext(sqlite3_vtab_cursor *cur){
171 carray_cursor *pCur = (carray_cursor*)cur;
172 pCur->iRowid++;
173 return SQLITE_OK;
174}
175
176/*
177** Return values of columns for the row at which the carray_cursor
178** is currently pointing.
179*/
180static int carrayColumn(
181 sqlite3_vtab_cursor *cur, /* The cursor */
182 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
183 int i /* Which column to return */
184){
185 carray_cursor *pCur = (carray_cursor*)cur;
186 sqlite3_int64 x = 0;
187 switch( i ){
188 case CARRAY_COLUMN_POINTER: return SQLITE_OK;
189 case CARRAY_COLUMN_COUNT: x = pCur->iCnt; break;
190 case CARRAY_COLUMN_CTYPE: {
191 sqlite3_result_text(ctx, azCarrayType[pCur->eType], -1, SQLITE_STATIC);
192 return SQLITE_OK;
193 }
194 default: {
195 switch( pCur->eType ){
196 case CARRAY_INT32: {
197 int *p = (int*)pCur->pPtr;
198 sqlite3_result_int(ctx, p[pCur->iRowid-1]);
199 return SQLITE_OK;
200 }
201 case CARRAY_INT64: {
202 sqlite3_int64 *p = (sqlite3_int64*)pCur->pPtr;
203 sqlite3_result_int64(ctx, p[pCur->iRowid-1]);
204 return SQLITE_OK;
205 }
206 case CARRAY_DOUBLE: {
207 double *p = (double*)pCur->pPtr;
208 sqlite3_result_double(ctx, p[pCur->iRowid-1]);
209 return SQLITE_OK;
210 }
211 case CARRAY_TEXT: {
212 const char **p = (const char**)pCur->pPtr;
213 sqlite3_result_text(ctx, p[pCur->iRowid-1], -1, SQLITE_TRANSIENT);
214 return SQLITE_OK;
215 }
216 default: {
217 const struct iovec *p = (struct iovec*)pCur->pPtr;
218 assert( pCur->eType==CARRAY_BLOB );
219 p += pCur->iRowid-1;
220 if( p->iov_len>0x7fffffff ){
221 sqlite3_result_error_toobig(ctx);
222 return SQLITE_TOOBIG;
223 }else{
224 sqlite3_result_blob(ctx, p->iov_base, (int)p->iov_len,
225 SQLITE_TRANSIENT);
226 }
227 return SQLITE_OK;
228 }
229 }
230 }
231 }
232 sqlite3_result_int64(ctx, x);
233 return SQLITE_OK;
234}
235
236/*
237** Return the rowid for the current row. In this implementation, the
238** rowid is the same as the output value.
239*/
240static int carrayRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
241 carray_cursor *pCur = (carray_cursor*)cur;
242 *pRowid = pCur->iRowid;
243 return SQLITE_OK;
244}
245
246/*
247** Return TRUE if the cursor has been moved off of the last
248** row of output.
249*/
250static int carrayEof(sqlite3_vtab_cursor *cur){
251 carray_cursor *pCur = (carray_cursor*)cur;
252 return pCur->iRowid>pCur->iCnt;
253}
254
255/*
256** This method is called to "rewind" the carray_cursor object back
257** to the first row of output.
258*/
259static int carrayFilter(
260 sqlite3_vtab_cursor *pVtabCursor,
261 int idxNum, const char *idxStr,
262 int argc, sqlite3_value **argv
263){
264 carray_cursor *pCur = (carray_cursor *)pVtabCursor;
265 pCur->pPtr = 0;
266 pCur->iCnt = 0;
267 switch( idxNum ){
268 case 1: {
269 carray_bind *pBind = sqlite3_value_pointer(argv[0], "carray-bind");
270 if( pBind==0 ) break;
271 pCur->pPtr = pBind->aData;
272 pCur->iCnt = pBind->nData;
273 pCur->eType = pBind->mFlags & 0x07;
274 break;
275 }
276 case 2:
277 case 3: {
278 pCur->pPtr = sqlite3_value_pointer(argv[0], "carray");
279 pCur->iCnt = pCur->pPtr ? sqlite3_value_int64(argv[1]) : 0;
280 if( idxNum<3 ){
281 pCur->eType = CARRAY_INT32;
282 }else{
283 unsigned char i;
284 const char *zType = (const char*)sqlite3_value_text(argv[2]);
285 for(i=0; i<sizeof(azCarrayType)/sizeof(azCarrayType[0]); i++){
286 if( sqlite3_stricmp(zType, azCarrayType[i])==0 ) break;
287 }
288 if( i>=sizeof(azCarrayType)/sizeof(azCarrayType[0]) ){
289 pVtabCursor->pVtab->zErrMsg = sqlite3_mprintf(
290 "unknown datatype: %Q", zType);
291 return SQLITE_ERROR;
292 }else{
293 pCur->eType = i;
294 }
295 }
296 break;
297 }
298 }
299 pCur->iRowid = 1;
300 return SQLITE_OK;
301}
302
303/*
304** SQLite will invoke this method one or more times while planning a query
305** that uses the carray virtual table. This routine needs to create
306** a query plan for each invocation and compute an estimated cost for that
307** plan.
308**
309** In this implementation idxNum is used to represent the
310** query plan. idxStr is unused.
311**
312** idxNum is:
313**
314** 1 If only the pointer= constraint exists. In this case, the
315** parameter must be bound using sqlite3_carray_bind().
316**
317** 2 if the pointer= and count= constraints exist.
318**
319** 3 if the ctype= constraint also exists.
320**
321** idxNum is 0 otherwise and carray becomes an empty table.
322*/
323static int carrayBestIndex(
324 sqlite3_vtab *tab,
325 sqlite3_index_info *pIdxInfo
326){
327 int i; /* Loop over constraints */
328 int ptrIdx = -1; /* Index of the pointer= constraint, or -1 if none */
329 int cntIdx = -1; /* Index of the count= constraint, or -1 if none */
330 int ctypeIdx = -1; /* Index of the ctype= constraint, or -1 if none */
331 unsigned seen = 0; /* Bitmask of == constrainted columns */
332
333 const struct sqlite3_index_constraint *pConstraint;
334 pConstraint = pIdxInfo->aConstraint;
335 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
336 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
337 if( pConstraint->iColumn>=0 ) seen |= 1 << pConstraint->iColumn;
338 if( pConstraint->usable==0 ) continue;
339 switch( pConstraint->iColumn ){
340 case CARRAY_COLUMN_POINTER:
341 ptrIdx = i;
342 break;
343 case CARRAY_COLUMN_COUNT:
344 cntIdx = i;
345 break;
346 case CARRAY_COLUMN_CTYPE:
347 ctypeIdx = i;
348 break;
349 }
350 }
351 if( ptrIdx>=0 ){
352 pIdxInfo->aConstraintUsage[ptrIdx].argvIndex = 1;
353 pIdxInfo->aConstraintUsage[ptrIdx].omit = 1;
354 pIdxInfo->estimatedCost = (double)1;
355 pIdxInfo->estimatedRows = 100;
356 pIdxInfo->idxNum = 1;
357 if( cntIdx>=0 ){
358 pIdxInfo->aConstraintUsage[cntIdx].argvIndex = 2;
359 pIdxInfo->aConstraintUsage[cntIdx].omit = 1;
360 pIdxInfo->idxNum = 2;
361 if( ctypeIdx>=0 ){
362 pIdxInfo->aConstraintUsage[ctypeIdx].argvIndex = 3;
363 pIdxInfo->aConstraintUsage[ctypeIdx].omit = 1;
364 pIdxInfo->idxNum = 3;
365 }else if( seen & (1<<CARRAY_COLUMN_CTYPE) ){
366 /* In a three-argument carray(), we need to know the value of all
367 ** three arguments */
368 return SQLITE_CONSTRAINT;
369 }
370 }else if( seen & (1<<CARRAY_COLUMN_COUNT) ){
371 /* In a two-argument carray(), we need to know the value of both
372 ** arguments */
373 return SQLITE_CONSTRAINT;
374 }
375 }else{
376 pIdxInfo->estimatedCost = (double)2147483647;
377 pIdxInfo->estimatedRows = 2147483647;
378 pIdxInfo->idxNum = 0;
379 }
380 return SQLITE_OK;
381}
382
383/*
384** This following structure defines all the methods for the
385** carray virtual table.
386*/
387static sqlite3_module carrayModule = {
388 0, /* iVersion */
389 0, /* xCreate */
390 carrayConnect, /* xConnect */
391 carrayBestIndex, /* xBestIndex */
392 carrayDisconnect, /* xDisconnect */
393 0, /* xDestroy */
394 carrayOpen, /* xOpen - open a cursor */
395 carrayClose, /* xClose - close a cursor */
396 carrayFilter, /* xFilter - configure scan constraints */
397 carrayNext, /* xNext - advance a cursor */
398 carrayEof, /* xEof - check for end of scan */
399 carrayColumn, /* xColumn - read data */
400 carrayRowid, /* xRowid - read data */
401 0, /* xUpdate */
402 0, /* xBegin */
403 0, /* xSync */
404 0, /* xCommit */
405 0, /* xRollback */
406 0, /* xFindMethod */
407 0, /* xRename */
408 0, /* xSavepoint */
409 0, /* xRelease */
410 0, /* xRollbackTo */
411 0, /* xShadow */
412 0 /* xIntegrity */
413};
414
415/*
416** Destructor for the carray_bind object
417*/
418static void carrayBindDel(void *pPtr){
419 carray_bind *p = (carray_bind*)pPtr;
420 if( p->xDel!=SQLITE_STATIC ){
421 p->xDel(p->pDel);
422 }
423 sqlite3_free(p);
424}
425
426/*
427** Invoke this interface in order to bind to the single-argument
428** version of CARRAY().
429**
430** pStmt The prepared statement to which to bind
431** idx The index of the parameter of pStmt to which to bind
432** aData The data to be bound
433** nData The number of elements in aData
434** mFlags One of SQLITE_CARRAY_xxxx indicating datatype of aData
435** xDestroy Destructor for pDestroy or aData if pDestroy==NULL.
436** pDestroy Invoke xDestroy on this pointer if not NULL
437**
438** The destructor is called pDestroy if pDestroy!=NULL, or against
439** aData if pDestroy==NULL.
440*/
441SQLITE_API int sqlite3_carray_bind_v2(
442 sqlite3_stmt *pStmt,
443 int idx,
444 void *aData,
445 int nData,
446 int mFlags,
447 void (*xDestroy)(void*),
448 void *pDestroy
449){
450 carray_bind *pNew = 0;
451 int i;
452 int rc = SQLITE_OK;
453
454 /* Ensure that the mFlags value is acceptable. */
455 assert( CARRAY_INT32==0 && CARRAY_INT64==1 && CARRAY_DOUBLE==2 );
456 assert( CARRAY_TEXT==3 && CARRAY_BLOB==4 );
457 if( mFlags<CARRAY_INT32 || mFlags>CARRAY_BLOB ){
458 rc = SQLITE_ERROR;
459 goto carray_bind_error;
460 }
461
462 pNew = sqlite3_malloc64(sizeof(*pNew));
463 if( pNew==0 ){
464 rc = SQLITE_NOMEM;
465 goto carray_bind_error;
466 }
467
468 pNew->nData = nData;
469 pNew->mFlags = mFlags;
470 if( xDestroy==SQLITE_TRANSIENT ){
471 sqlite3_int64 sz = nData;
472 switch( mFlags ){
473 case CARRAY_INT32: sz *= 4; break;
474 case CARRAY_INT64: sz *= 8; break;
475 case CARRAY_DOUBLE: sz *= 8; break;
476 case CARRAY_TEXT: sz *= sizeof(char*); break;
477 default: sz *= sizeof(struct iovec); break;
478 }
479 if( mFlags==CARRAY_TEXT ){
480 for(i=0; i<nData; i++){
481 const char *z = ((char**)aData)[i];
482 if( z ) sz += strlen(z) + 1;
483 }
484 }else if( mFlags==CARRAY_BLOB ){
485 for(i=0; i<nData; i++){
486 sz += ((struct iovec*)aData)[i].iov_len;
487 }
488 }
489
490 pNew->aData = sqlite3_malloc64( sz>0 ? sz : 1 );
491 if( pNew->aData==0 ){
492 rc = SQLITE_NOMEM;
493 goto carray_bind_error;
494 }
495
496 if( mFlags==CARRAY_TEXT ){
497 char **az = (char**)pNew->aData;
498 char *z = (char*)&az[nData];
499 for(i=0; i<nData; i++){
500 const char *zData = ((char**)aData)[i];
501 sqlite3_int64 n;
502 if( zData==0 ){
503 az[i] = 0;
504 continue;
505 }
506 az[i] = z;
507 n = strlen(zData);
508 memcpy(z, zData, n+1);
509 z += n+1;
510 }
511 }else if( mFlags==CARRAY_BLOB ){
512 struct iovec *p = (struct iovec*)pNew->aData;
513 unsigned char *z = (unsigned char*)&p[nData];
514 for(i=0; i<nData; i++){
515 size_t n = ((struct iovec*)aData)[i].iov_len;
516 void *a = ((struct iovec*)aData)[i].iov_base;
517 p[i].iov_len = n;
518 p[i].iov_base = a ? z : 0;
519 if( n ){
520 z += n;
521 memcpy(p[i].iov_base, a, n);
522 }
523 }
524 }else if( sz>0 ){
525 memcpy(pNew->aData, aData, sz);
526 }
527 pNew->xDel = sqlite3_free;
528 pNew->pDel = pNew->aData;
529 }else{
530 pNew->aData = aData;
531 pNew->xDel = xDestroy;
532 pNew->pDel = pDestroy;
533 }
534 return sqlite3_bind_pointer(pStmt, idx, pNew, "carray-bind", carrayBindDel);
535
536 carray_bind_error:
537 if( xDestroy!=SQLITE_STATIC && xDestroy!=SQLITE_TRANSIENT ){
538 xDestroy(pDestroy);
539 }
540 sqlite3_free(pNew);
541 return rc;
542}
543
544/*
545** Invoke this interface in order to bind to the single-argument
546** version of CARRAY(). Same as sqlite3_carray_bind_v2() with the
547** pDestroy parameter set to NULL.
548*/
549SQLITE_API int sqlite3_carray_bind(
550 sqlite3_stmt *pStmt,
551 int idx,
552 void *aData,
553 int nData,
554 int mFlags,
555 void (*xDestroy)(void*)
556){
557 return sqlite3_carray_bind_v2(pStmt,idx,aData,nData,mFlags,xDestroy,aData);
558}
559
560/*
561** Invoke this routine to register the carray() function.
562*/
563Module *sqlite3CarrayRegister(sqlite3 *db){
564 return sqlite3VtabCreateModule(db, "carray", &carrayModule, 0, 0);
565}
566
567#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_CARRAY) */