Merge sqlite-release(3.40.1) into prerelease-integration
[sqlcipher.git] / src / test_bestindex.c
blob67a0c8258d66c931039c2ed65050cc9f08bfe31d
1 /*
2 ** 2016-03-01
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 *************************************************************************
12 ** Code for testing the virtual table xBestIndex method and the query
13 ** planner.
18 ** INSTRUCTIONS
20 ** This module exports a single tcl command - [register_tcl_module]. When
21 ** invoked, it registers a special virtual table module with a database
22 ** connection.
24 ** The virtual table is currently read-only. And always returns zero rows.
25 ** It is created with a single argument - the name of a Tcl command - as
26 ** follows:
28 ** CREATE VIRTUAL TABLE x1 USING tcl(tcl_command);
30 ** The command [tcl_command] is invoked when the table is first created (or
31 ** connected), when the xBestIndex() method is invoked and when the xFilter()
32 ** method is called. When it is created (or connected), it is invoked as
33 ** follows:
35 ** tcl_command xConnect
37 ** In this case the return value of the script is passed to the
38 ** sqlite3_declare_vtab() function to create the virtual table schema.
40 ** When the xBestIndex() method is called by SQLite, the Tcl command is
41 ** invoked as:
43 ** tcl_command xBestIndex CONSTRAINTS ORDERBY MASK
45 ** where CONSTRAINTS is a tcl representation of the aConstraints[] array,
46 ** ORDERBY is a representation of the contents of the aOrderBy[] array and
47 ** MASK is a copy of sqlite3_index_info.colUsed. For example if the virtual
48 ** table is declared as:
50 ** CREATE TABLE x1(a, b, c)
52 ** and the query is:
54 ** SELECT * FROM x1 WHERE a=? AND c<? ORDER BY b, c;
56 ** then the Tcl command is:
58 ** tcl_command xBestIndex \
59 ** {{op eq column 0 usable 1} {op lt column 2 usable 1}} \
60 ** {{column 1 desc 0} {column 2 desc 0}} \
61 ** 7
63 ** The return value of the script is a list of key-value pairs used to
64 ** populate the output fields of the sqlite3_index_info structure. Possible
65 ** keys and the usage of the accompanying values are:
66 **
67 ** "orderby" (value of orderByConsumed flag)
68 ** "cost" (value of estimatedCost field)
69 ** "rows" (value of estimatedRows field)
70 ** "use" (index of used constraint in aConstraint[])
71 ** "omit" (like "use", but also sets omit flag)
72 ** "idxnum" (value of idxNum field)
73 ** "idxstr" (value of idxStr field)
75 ** Refer to code below for further details.
77 ** When SQLite calls the xFilter() method, this module invokes the following
78 ** Tcl script:
80 ** tcl_command xFilter IDXNUM IDXSTR ARGLIST
82 ** IDXNUM and IDXSTR are the values of the idxNum and idxStr parameters
83 ** passed to xFilter. ARGLIST is a Tcl list containing each of the arguments
84 ** passed to xFilter in text form.
86 ** As with xBestIndex(), the return value of the script is interpreted as a
87 ** list of key-value pairs. There is currently only one key defined - "sql".
88 ** The value must be the full text of an SQL statement that returns the data
89 ** for the current scan. The leftmost column returned by the SELECT is assumed
90 ** to contain the rowid. Other columns must follow, in order from left to
91 ** right.
95 #include "sqliteInt.h"
96 #if defined(INCLUDE_SQLITE_TCL_H)
97 # include "sqlite_tcl.h"
98 #else
99 # include "tcl.h"
100 #endif
102 #ifndef SQLITE_OMIT_VIRTUALTABLE
104 typedef struct tcl_vtab tcl_vtab;
105 typedef struct tcl_cursor tcl_cursor;
108 ** A fs virtual-table object
110 struct tcl_vtab {
111 sqlite3_vtab base;
112 Tcl_Interp *interp;
113 Tcl_Obj *pCmd;
114 sqlite3 *db;
117 /* A tcl cursor object */
118 struct tcl_cursor {
119 sqlite3_vtab_cursor base;
120 sqlite3_stmt *pStmt; /* Read data from here */
124 ** Dequote string z in place.
126 static void tclDequote(char *z){
127 char q = z[0];
129 /* Set stack variable q to the close-quote character */
130 if( q=='[' || q=='\'' || q=='"' || q=='`' ){
131 int iIn = 1;
132 int iOut = 0;
133 if( q=='[' ) q = ']';
135 while( ALWAYS(z[iIn]) ){
136 if( z[iIn]==q ){
137 if( z[iIn+1]!=q ){
138 /* Character iIn was the close quote. */
139 iIn++;
140 break;
141 }else{
142 /* Character iIn and iIn+1 form an escaped quote character. Skip
143 ** the input cursor past both and copy a single quote character
144 ** to the output buffer. */
145 iIn += 2;
146 z[iOut++] = q;
148 }else{
149 z[iOut++] = z[iIn++];
153 z[iOut] = '\0';
158 ** This function is the implementation of both the xConnect and xCreate
159 ** methods of the fs virtual table.
161 ** The argv[] array contains the following:
163 ** argv[0] -> module name ("fs")
164 ** argv[1] -> database name
165 ** argv[2] -> table name
166 ** argv[...] -> other module argument fields.
168 static int tclConnect(
169 sqlite3 *db,
170 void *pAux,
171 int argc, const char *const*argv,
172 sqlite3_vtab **ppVtab,
173 char **pzErr
175 Tcl_Interp *interp = (Tcl_Interp*)pAux;
176 tcl_vtab *pTab = 0;
177 char *zCmd = 0;
178 Tcl_Obj *pScript = 0;
179 int rc = SQLITE_OK;
181 if( argc!=4 ){
182 *pzErr = sqlite3_mprintf("wrong number of arguments");
183 return SQLITE_ERROR;
186 zCmd = sqlite3_malloc64(strlen(argv[3])+1);
187 pTab = (tcl_vtab*)sqlite3_malloc64(sizeof(tcl_vtab));
188 if( zCmd && pTab ){
189 memcpy(zCmd, argv[3], strlen(argv[3])+1);
190 tclDequote(zCmd);
191 memset(pTab, 0, sizeof(tcl_vtab));
193 pTab->pCmd = Tcl_NewStringObj(zCmd, -1);
194 pTab->interp = interp;
195 pTab->db = db;
196 Tcl_IncrRefCount(pTab->pCmd);
198 pScript = Tcl_DuplicateObj(pTab->pCmd);
199 Tcl_IncrRefCount(pScript);
200 Tcl_ListObjAppendElement(interp, pScript, Tcl_NewStringObj("xConnect", -1));
202 rc = Tcl_EvalObjEx(interp, pScript, TCL_EVAL_GLOBAL);
203 if( rc!=TCL_OK ){
204 *pzErr = sqlite3_mprintf("%s", Tcl_GetStringResult(interp));
205 rc = SQLITE_ERROR;
206 }else{
207 rc = sqlite3_declare_vtab(db, Tcl_GetStringResult(interp));
210 if( rc!=SQLITE_OK ){
211 sqlite3_free(pTab);
212 pTab = 0;
214 }else{
215 rc = SQLITE_NOMEM;
218 sqlite3_free(zCmd);
219 *ppVtab = &pTab->base;
220 return rc;
223 /* The xDisconnect and xDestroy methods are also the same */
224 static int tclDisconnect(sqlite3_vtab *pVtab){
225 tcl_vtab *pTab = (tcl_vtab*)pVtab;
226 Tcl_DecrRefCount(pTab->pCmd);
227 sqlite3_free(pTab);
228 return SQLITE_OK;
232 ** Open a new tcl cursor.
234 static int tclOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
235 tcl_cursor *pCur;
236 pCur = sqlite3_malloc(sizeof(tcl_cursor));
237 if( pCur==0 ) return SQLITE_NOMEM;
238 memset(pCur, 0, sizeof(tcl_cursor));
239 *ppCursor = &pCur->base;
240 return SQLITE_OK;
244 ** Close a tcl cursor.
246 static int tclClose(sqlite3_vtab_cursor *cur){
247 tcl_cursor *pCur = (tcl_cursor *)cur;
248 if( pCur ){
249 sqlite3_finalize(pCur->pStmt);
250 sqlite3_free(pCur);
252 return SQLITE_OK;
255 static int tclNext(sqlite3_vtab_cursor *pVtabCursor){
256 tcl_cursor *pCsr = (tcl_cursor*)pVtabCursor;
257 if( pCsr->pStmt ){
258 tcl_vtab *pTab = (tcl_vtab*)(pVtabCursor->pVtab);
259 int rc = sqlite3_step(pCsr->pStmt);
260 if( rc!=SQLITE_ROW ){
261 const char *zErr;
262 rc = sqlite3_finalize(pCsr->pStmt);
263 pCsr->pStmt = 0;
264 if( rc!=SQLITE_OK ){
265 zErr = sqlite3_errmsg(pTab->db);
266 pTab->base.zErrMsg = sqlite3_mprintf("%s", zErr);
270 return SQLITE_OK;
273 static int tclFilter(
274 sqlite3_vtab_cursor *pVtabCursor,
275 int idxNum, const char *idxStr,
276 int argc, sqlite3_value **argv
278 tcl_cursor *pCsr = (tcl_cursor*)pVtabCursor;
279 tcl_vtab *pTab = (tcl_vtab*)(pVtabCursor->pVtab);
280 Tcl_Interp *interp = pTab->interp;
281 Tcl_Obj *pScript;
282 Tcl_Obj *pArg;
283 int ii;
284 int rc;
286 pScript = Tcl_DuplicateObj(pTab->pCmd);
287 Tcl_IncrRefCount(pScript);
288 Tcl_ListObjAppendElement(interp, pScript, Tcl_NewStringObj("xFilter", -1));
289 Tcl_ListObjAppendElement(interp, pScript, Tcl_NewIntObj(idxNum));
290 if( idxStr ){
291 Tcl_ListObjAppendElement(interp, pScript, Tcl_NewStringObj(idxStr, -1));
292 }else{
293 Tcl_ListObjAppendElement(interp, pScript, Tcl_NewStringObj("", -1));
296 pArg = Tcl_NewObj();
297 Tcl_IncrRefCount(pArg);
298 for(ii=0; ii<argc; ii++){
299 const char *zVal = (const char*)sqlite3_value_text(argv[ii]);
300 Tcl_Obj *pVal;
301 if( zVal==0 ){
302 sqlite3_value *pMem;
303 pVal = Tcl_NewObj();
304 for(rc=sqlite3_vtab_in_first(argv[ii], &pMem);
305 rc==SQLITE_OK && pMem;
306 rc=sqlite3_vtab_in_next(argv[ii], &pMem)
308 Tcl_Obj *pVal2 = 0;
309 zVal = (const char*)sqlite3_value_text(pMem);
310 if( zVal ){
311 pVal2 = Tcl_NewStringObj(zVal, -1);
312 }else{
313 pVal2 = Tcl_NewObj();
315 Tcl_ListObjAppendElement(interp, pVal, pVal2);
317 }else{
318 pVal = Tcl_NewStringObj(zVal, -1);
320 Tcl_ListObjAppendElement(interp, pArg, pVal);
322 Tcl_ListObjAppendElement(interp, pScript, pArg);
323 Tcl_DecrRefCount(pArg);
325 rc = Tcl_EvalObjEx(interp, pScript, TCL_EVAL_GLOBAL);
326 if( rc!=TCL_OK ){
327 const char *zErr = Tcl_GetStringResult(interp);
328 rc = SQLITE_ERROR;
329 pTab->base.zErrMsg = sqlite3_mprintf("%s", zErr);
330 }else{
331 /* Analyze the scripts return value. The return value should be a tcl
332 ** list object with an even number of elements. The first element of each
333 ** pair must be one of:
335 ** "sql" (SQL statement to return data)
337 Tcl_Obj *pRes = Tcl_GetObjResult(interp);
338 Tcl_Obj **apElem = 0;
339 int nElem;
340 rc = Tcl_ListObjGetElements(interp, pRes, &nElem, &apElem);
341 if( rc!=TCL_OK ){
342 const char *zErr = Tcl_GetStringResult(interp);
343 rc = SQLITE_ERROR;
344 pTab->base.zErrMsg = sqlite3_mprintf("%s", zErr);
345 }else{
346 for(ii=0; rc==SQLITE_OK && ii<nElem; ii+=2){
347 const char *zCmd = Tcl_GetString(apElem[ii]);
348 Tcl_Obj *p = apElem[ii+1];
349 if( sqlite3_stricmp("sql", zCmd)==0 ){
350 const char *zSql = Tcl_GetString(p);
351 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pStmt, 0);
352 if( rc!=SQLITE_OK ){
353 const char *zErr = sqlite3_errmsg(pTab->db);
354 pTab->base.zErrMsg = sqlite3_mprintf("unexpected: %s", zErr);
356 }else{
357 rc = SQLITE_ERROR;
358 pTab->base.zErrMsg = sqlite3_mprintf("unexpected: %s", zCmd);
364 if( rc==SQLITE_OK ){
365 rc = tclNext(pVtabCursor);
367 return rc;
370 static int tclColumn(
371 sqlite3_vtab_cursor *pVtabCursor,
372 sqlite3_context *ctx,
373 int i
375 tcl_cursor *pCsr = (tcl_cursor*)pVtabCursor;
376 sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pStmt, i+1));
377 return SQLITE_OK;
380 static int tclRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *pRowid){
381 tcl_cursor *pCsr = (tcl_cursor*)pVtabCursor;
382 *pRowid = sqlite3_column_int64(pCsr->pStmt, 0);
383 return SQLITE_OK;
386 static int tclEof(sqlite3_vtab_cursor *pVtabCursor){
387 tcl_cursor *pCsr = (tcl_cursor*)pVtabCursor;
388 return (pCsr->pStmt==0);
391 static void testBestIndexObjConstraints(
392 Tcl_Interp *interp,
393 sqlite3_index_info *pIdxInfo
395 int ii;
396 Tcl_Obj *pRes = Tcl_NewObj();
397 Tcl_IncrRefCount(pRes);
398 for(ii=0; ii<pIdxInfo->nConstraint; ii++){
399 struct sqlite3_index_constraint const *pCons = &pIdxInfo->aConstraint[ii];
400 Tcl_Obj *pElem = Tcl_NewObj();
401 const char *zOp = "?";
403 Tcl_IncrRefCount(pElem);
405 switch( pCons->op ){
406 case SQLITE_INDEX_CONSTRAINT_EQ:
407 zOp = "eq"; break;
408 case SQLITE_INDEX_CONSTRAINT_GT:
409 zOp = "gt"; break;
410 case SQLITE_INDEX_CONSTRAINT_LE:
411 zOp = "le"; break;
412 case SQLITE_INDEX_CONSTRAINT_LT:
413 zOp = "lt"; break;
414 case SQLITE_INDEX_CONSTRAINT_GE:
415 zOp = "ge"; break;
416 case SQLITE_INDEX_CONSTRAINT_MATCH:
417 zOp = "match"; break;
418 case SQLITE_INDEX_CONSTRAINT_LIKE:
419 zOp = "like"; break;
420 case SQLITE_INDEX_CONSTRAINT_GLOB:
421 zOp = "glob"; break;
422 case SQLITE_INDEX_CONSTRAINT_REGEXP:
423 zOp = "regexp"; break;
424 case SQLITE_INDEX_CONSTRAINT_NE:
425 zOp = "ne"; break;
426 case SQLITE_INDEX_CONSTRAINT_ISNOT:
427 zOp = "isnot"; break;
428 case SQLITE_INDEX_CONSTRAINT_ISNOTNULL:
429 zOp = "isnotnull"; break;
430 case SQLITE_INDEX_CONSTRAINT_ISNULL:
431 zOp = "isnull"; break;
432 case SQLITE_INDEX_CONSTRAINT_IS:
433 zOp = "is"; break;
434 case SQLITE_INDEX_CONSTRAINT_LIMIT:
435 zOp = "limit"; break;
436 case SQLITE_INDEX_CONSTRAINT_OFFSET:
437 zOp = "offset"; break;
440 Tcl_ListObjAppendElement(0, pElem, Tcl_NewStringObj("op", -1));
441 Tcl_ListObjAppendElement(0, pElem, Tcl_NewStringObj(zOp, -1));
442 Tcl_ListObjAppendElement(0, pElem, Tcl_NewStringObj("column", -1));
443 Tcl_ListObjAppendElement(0, pElem, Tcl_NewIntObj(pCons->iColumn));
444 Tcl_ListObjAppendElement(0, pElem, Tcl_NewStringObj("usable", -1));
445 Tcl_ListObjAppendElement(0, pElem, Tcl_NewIntObj(pCons->usable));
447 Tcl_ListObjAppendElement(0, pRes, pElem);
448 Tcl_DecrRefCount(pElem);
451 Tcl_SetObjResult(interp, pRes);
452 Tcl_DecrRefCount(pRes);
455 static void testBestIndexObjOrderby(
456 Tcl_Interp *interp,
457 sqlite3_index_info *pIdxInfo
459 int ii;
460 Tcl_Obj *pRes = Tcl_NewObj();
461 Tcl_IncrRefCount(pRes);
462 for(ii=0; ii<pIdxInfo->nOrderBy; ii++){
463 struct sqlite3_index_orderby const *pOrder = &pIdxInfo->aOrderBy[ii];
464 Tcl_Obj *pElem = Tcl_NewObj();
465 Tcl_IncrRefCount(pElem);
467 Tcl_ListObjAppendElement(0, pElem, Tcl_NewStringObj("column", -1));
468 Tcl_ListObjAppendElement(0, pElem, Tcl_NewIntObj(pOrder->iColumn));
469 Tcl_ListObjAppendElement(0, pElem, Tcl_NewStringObj("desc", -1));
470 Tcl_ListObjAppendElement(0, pElem, Tcl_NewIntObj(pOrder->desc));
472 Tcl_ListObjAppendElement(0, pRes, pElem);
473 Tcl_DecrRefCount(pElem);
476 Tcl_SetObjResult(interp, pRes);
477 Tcl_DecrRefCount(pRes);
481 ** Implementation of the handle passed to each xBestIndex callback. This
482 ** object features the following sub-commands:
484 ** $hdl constraints
485 ** $hdl orderby
486 ** $hdl mask
488 ** $hdl distinct
489 ** Return the result (an integer) of calling sqlite3_vtab_distinct()
490 ** on the index-info structure.
492 ** $hdl in IDX BOOLEAN
493 ** Wrapper around sqlite3_vtab_in(). Returns an integer.
495 ** $hdl rhs_value IDX ?DEFAULT?
496 ** Wrapper around sqlite3_vtab_rhs_value().
498 static int SQLITE_TCLAPI testBestIndexObj(
499 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
500 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
501 int objc, /* Number of arguments */
502 Tcl_Obj *CONST objv[] /* Command arguments */
504 const char *azSub[] = {
505 "constraints", /* 0 */
506 "orderby", /* 1 */
507 "mask", /* 2 */
508 "distinct", /* 3 */
509 "in", /* 4 */
510 "rhs_value", /* 5 */
513 int ii;
514 sqlite3_index_info *pIdxInfo = (sqlite3_index_info*)clientData;
516 if( objc<2 ){
517 Tcl_WrongNumArgs(interp, 1, objv, "SUB-COMMAND");
518 return TCL_ERROR;
520 if( Tcl_GetIndexFromObj(interp, objv[1], azSub, "sub-command", 0, &ii) ){
521 return TCL_ERROR;
524 if( ii<4 && objc!=2 ){
525 Tcl_WrongNumArgs(interp, 2, objv, "");
526 return TCL_ERROR;
528 if( ii==4 && objc!=4 ){
529 Tcl_WrongNumArgs(interp, 2, objv, "INDEX BOOLEAN");
530 return TCL_ERROR;
532 if( ii==5 && objc!=3 && objc!=4 ){
533 Tcl_WrongNumArgs(interp, 2, objv, "INDEX ?DEFAULT?");
534 return TCL_ERROR;
537 switch( ii ){
538 case 0: assert( sqlite3_stricmp(azSub[ii], "constraints")==0 );
539 testBestIndexObjConstraints(interp, pIdxInfo);
540 break;
542 case 1: assert( sqlite3_stricmp(azSub[ii], "orderby")==0 );
543 testBestIndexObjOrderby(interp, pIdxInfo);
544 break;
546 case 2: assert( sqlite3_stricmp(azSub[ii], "mask")==0 );
547 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(pIdxInfo->colUsed));
548 break;
550 case 3: assert( sqlite3_stricmp(azSub[ii], "distinct")==0 ); {
551 int bDistinct = sqlite3_vtab_distinct(pIdxInfo);
552 Tcl_SetObjResult(interp, Tcl_NewIntObj(bDistinct));
553 break;
556 case 4: assert( sqlite3_stricmp(azSub[ii], "in")==0 ); {
557 int iCons;
558 int bHandle;
559 if( Tcl_GetIntFromObj(interp, objv[2], &iCons)
560 || Tcl_GetBooleanFromObj(interp, objv[3], &bHandle)
562 return TCL_ERROR;
564 Tcl_SetObjResult(interp,
565 Tcl_NewIntObj(sqlite3_vtab_in(pIdxInfo, iCons, bHandle))
567 break;
570 case 5: assert( sqlite3_stricmp(azSub[ii], "rhs_value")==0 ); {
571 int iCons = 0;
572 int rc;
573 sqlite3_value *pVal = 0;
574 const char *zVal = "";
575 if( Tcl_GetIntFromObj(interp, objv[2], &iCons) ){
576 return TCL_ERROR;
578 rc = sqlite3_vtab_rhs_value(pIdxInfo, iCons, &pVal);
579 if( rc!=SQLITE_OK && rc!=SQLITE_NOTFOUND ){
580 Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE);
581 return TCL_ERROR;
583 if( pVal ){
584 zVal = (const char*)sqlite3_value_text(pVal);
585 }else if( objc==4 ){
586 zVal = Tcl_GetString(objv[3]);
588 Tcl_SetObjResult(interp, Tcl_NewStringObj(zVal, -1));
589 break;
593 return TCL_OK;
596 static int tclBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
597 tcl_vtab *pTab = (tcl_vtab*)tab;
598 Tcl_Interp *interp = pTab->interp;
599 int rc = SQLITE_OK;
601 static int iNext = 43;
602 char zHdl[24];
603 Tcl_Obj *pScript;
605 pScript = Tcl_DuplicateObj(pTab->pCmd);
606 Tcl_IncrRefCount(pScript);
607 Tcl_ListObjAppendElement(interp, pScript, Tcl_NewStringObj("xBestIndex", -1));
609 sqlite3_snprintf(sizeof(zHdl), zHdl, "bestindex%d", iNext++);
610 Tcl_CreateObjCommand(interp, zHdl, testBestIndexObj, pIdxInfo, 0);
611 Tcl_ListObjAppendElement(interp, pScript, Tcl_NewStringObj(zHdl, -1));
612 rc = Tcl_EvalObjEx(interp, pScript, TCL_EVAL_GLOBAL);
613 Tcl_DeleteCommand(interp, zHdl);
614 Tcl_DecrRefCount(pScript);
616 if( rc!=TCL_OK ){
617 const char *zErr = Tcl_GetStringResult(interp);
618 rc = SQLITE_ERROR;
619 pTab->base.zErrMsg = sqlite3_mprintf("%s", zErr);
620 }else{
621 /* Analyze the scripts return value. The return value should be a tcl
622 ** list object with an even number of elements. The first element of each
623 ** pair must be one of:
625 ** "orderby" (value of orderByConsumed flag)
626 ** "cost" (value of estimatedCost field)
627 ** "rows" (value of estimatedRows field)
628 ** "use" (index of used constraint in aConstraint[])
629 ** "idxnum" (value of idxNum field)
630 ** "idxstr" (value of idxStr field)
631 ** "omit" (index of omitted constraint in aConstraint[])
633 Tcl_Obj *pRes = Tcl_GetObjResult(interp);
634 Tcl_Obj **apElem = 0;
635 int nElem;
636 rc = Tcl_ListObjGetElements(interp, pRes, &nElem, &apElem);
637 if( rc!=TCL_OK ){
638 const char *zErr = Tcl_GetStringResult(interp);
639 rc = SQLITE_ERROR;
640 pTab->base.zErrMsg = sqlite3_mprintf("%s", zErr);
641 }else{
642 int ii;
643 int iArgv = 1;
644 for(ii=0; rc==SQLITE_OK && ii<nElem; ii+=2){
645 const char *zCmd = Tcl_GetString(apElem[ii]);
646 Tcl_Obj *p = apElem[ii+1];
647 if( sqlite3_stricmp("cost", zCmd)==0 ){
648 rc = Tcl_GetDoubleFromObj(interp, p, &pIdxInfo->estimatedCost);
649 }else
650 if( sqlite3_stricmp("orderby", zCmd)==0 ){
651 rc = Tcl_GetIntFromObj(interp, p, &pIdxInfo->orderByConsumed);
652 }else
653 if( sqlite3_stricmp("idxnum", zCmd)==0 ){
654 rc = Tcl_GetIntFromObj(interp, p, &pIdxInfo->idxNum);
655 }else
656 if( sqlite3_stricmp("idxstr", zCmd)==0 ){
657 sqlite3_free(pIdxInfo->idxStr);
658 pIdxInfo->idxStr = sqlite3_mprintf("%s", Tcl_GetString(p));
659 pIdxInfo->needToFreeIdxStr = 1;
660 }else
661 if( sqlite3_stricmp("rows", zCmd)==0 ){
662 Tcl_WideInt x = 0;
663 rc = Tcl_GetWideIntFromObj(interp, p, &x);
664 pIdxInfo->estimatedRows = (tRowcnt)x;
665 }else
666 if( sqlite3_stricmp("use", zCmd)==0
667 || sqlite3_stricmp("omit", zCmd)==0
669 int iCons;
670 rc = Tcl_GetIntFromObj(interp, p, &iCons);
671 if( rc==SQLITE_OK ){
672 if( iCons<0 || iCons>=pIdxInfo->nConstraint ){
673 rc = SQLITE_ERROR;
674 pTab->base.zErrMsg = sqlite3_mprintf("unexpected: %d", iCons);
675 }else{
676 int bOmit = (zCmd[0]=='o' || zCmd[0]=='O');
677 pIdxInfo->aConstraintUsage[iCons].argvIndex = iArgv++;
678 pIdxInfo->aConstraintUsage[iCons].omit = bOmit;
681 }else{
682 rc = SQLITE_ERROR;
683 pTab->base.zErrMsg = sqlite3_mprintf("unexpected: %s", zCmd);
685 if( rc!=SQLITE_OK && pTab->base.zErrMsg==0 ){
686 const char *zErr = Tcl_GetStringResult(interp);
687 pTab->base.zErrMsg = sqlite3_mprintf("%s", zErr);
693 return rc;
697 ** A virtual table module that provides read-only access to a
698 ** Tcl global variable namespace.
700 static sqlite3_module tclModule = {
701 0, /* iVersion */
702 tclConnect,
703 tclConnect,
704 tclBestIndex,
705 tclDisconnect,
706 tclDisconnect,
707 tclOpen, /* xOpen - open a cursor */
708 tclClose, /* xClose - close a cursor */
709 tclFilter, /* xFilter - configure scan constraints */
710 tclNext, /* xNext - advance a cursor */
711 tclEof, /* xEof - check for end of scan */
712 tclColumn, /* xColumn - read data */
713 tclRowid, /* xRowid - read data */
714 0, /* xUpdate */
715 0, /* xBegin */
716 0, /* xSync */
717 0, /* xCommit */
718 0, /* xRollback */
719 0, /* xFindMethod */
720 0, /* xRename */
724 ** Decode a pointer to an sqlite3 object.
726 extern int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb);
729 ** Register the echo virtual table module.
731 static int SQLITE_TCLAPI register_tcl_module(
732 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
733 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
734 int objc, /* Number of arguments */
735 Tcl_Obj *CONST objv[] /* Command arguments */
737 sqlite3 *db;
738 if( objc!=2 ){
739 Tcl_WrongNumArgs(interp, 1, objv, "DB");
740 return TCL_ERROR;
742 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
743 #ifndef SQLITE_OMIT_VIRTUALTABLE
744 sqlite3_create_module(db, "tcl", &tclModule, (void *)interp);
745 #endif
746 return TCL_OK;
749 #endif
753 ** Register commands with the TCL interpreter.
755 int Sqlitetesttcl_Init(Tcl_Interp *interp){
756 #ifndef SQLITE_OMIT_VIRTUALTABLE
757 static struct {
758 char *zName;
759 Tcl_ObjCmdProc *xProc;
760 void *clientData;
761 } aObjCmd[] = {
762 { "register_tcl_module", register_tcl_module, 0 },
764 int i;
765 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
766 Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
767 aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
769 #endif
770 return TCL_OK;