Replaces use of deprecated WINAPI_FAMILY_APP macro with WINAPI_FAMILY_PC_APP
[sqlcipher.git] / src / test_bestindex.c
blob0e1e86a81ca78ed9ebf57a33aca1790c4bac6ffc
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
105 typedef struct tcl_vtab tcl_vtab;
106 typedef struct tcl_cursor tcl_cursor;
107 typedef struct TestFindFunction TestFindFunction;
110 ** A fs virtual-table object
112 struct tcl_vtab {
113 sqlite3_vtab base;
114 Tcl_Interp *interp;
115 Tcl_Obj *pCmd;
116 TestFindFunction *pFindFunctionList;
117 sqlite3 *db;
120 /* A tcl cursor object */
121 struct tcl_cursor {
122 sqlite3_vtab_cursor base;
123 sqlite3_stmt *pStmt; /* Read data from here */
126 struct TestFindFunction {
127 tcl_vtab *pTab;
128 const char *zName;
129 TestFindFunction *pNext;
134 ** Dequote string z in place.
136 static void tclDequote(char *z){
137 char q = z[0];
139 /* Set stack variable q to the close-quote character */
140 if( q=='[' || q=='\'' || q=='"' || q=='`' ){
141 int iIn = 1;
142 int iOut = 0;
143 if( q=='[' ) q = ']';
145 while( ALWAYS(z[iIn]) ){
146 if( z[iIn]==q ){
147 if( z[iIn+1]!=q ){
148 /* Character iIn was the close quote. */
149 iIn++;
150 break;
151 }else{
152 /* Character iIn and iIn+1 form an escaped quote character. Skip
153 ** the input cursor past both and copy a single quote character
154 ** to the output buffer. */
155 iIn += 2;
156 z[iOut++] = q;
158 }else{
159 z[iOut++] = z[iIn++];
163 z[iOut] = '\0';
168 ** This function is the implementation of both the xConnect and xCreate
169 ** methods of the fs virtual table.
171 ** The argv[] array contains the following:
173 ** argv[0] -> module name ("fs")
174 ** argv[1] -> database name
175 ** argv[2] -> table name
176 ** argv[...] -> other module argument fields.
178 static int tclConnect(
179 sqlite3 *db,
180 void *pAux,
181 int argc, const char *const*argv,
182 sqlite3_vtab **ppVtab,
183 char **pzErr
185 Tcl_Interp *interp = (Tcl_Interp*)pAux;
186 tcl_vtab *pTab = 0;
187 char *zCmd = 0;
188 Tcl_Obj *pScript = 0;
189 int rc = SQLITE_OK;
191 if( argc!=4 ){
192 *pzErr = sqlite3_mprintf("wrong number of arguments");
193 return SQLITE_ERROR;
196 zCmd = sqlite3_malloc64(strlen(argv[3])+1);
197 pTab = (tcl_vtab*)sqlite3_malloc64(sizeof(tcl_vtab));
198 if( zCmd && pTab ){
199 memcpy(zCmd, argv[3], strlen(argv[3])+1);
200 tclDequote(zCmd);
201 memset(pTab, 0, sizeof(tcl_vtab));
203 pTab->pCmd = Tcl_NewStringObj(zCmd, -1);
204 pTab->interp = interp;
205 pTab->db = db;
206 Tcl_IncrRefCount(pTab->pCmd);
208 pScript = Tcl_DuplicateObj(pTab->pCmd);
209 Tcl_IncrRefCount(pScript);
210 Tcl_ListObjAppendElement(interp, pScript, Tcl_NewStringObj("xConnect", -1));
212 rc = Tcl_EvalObjEx(interp, pScript, TCL_EVAL_GLOBAL);
213 if( rc!=TCL_OK ){
214 *pzErr = sqlite3_mprintf("%s", Tcl_GetStringResult(interp));
215 rc = SQLITE_ERROR;
216 }else{
217 rc = sqlite3_declare_vtab(db, Tcl_GetStringResult(interp));
218 if( rc!=SQLITE_OK ){
219 *pzErr = sqlite3_mprintf("declare_vtab: %s", sqlite3_errmsg(db));
223 if( rc!=SQLITE_OK ){
224 sqlite3_free(pTab);
225 pTab = 0;
227 }else{
228 rc = SQLITE_NOMEM;
231 sqlite3_free(zCmd);
232 *ppVtab = pTab ? &pTab->base : 0;
233 return rc;
236 /* The xDisconnect and xDestroy methods are also the same */
237 static int tclDisconnect(sqlite3_vtab *pVtab){
238 tcl_vtab *pTab = (tcl_vtab*)pVtab;
239 while( pTab->pFindFunctionList ){
240 TestFindFunction *p = pTab->pFindFunctionList;
241 pTab->pFindFunctionList = p->pNext;
242 sqlite3_free(p);
244 Tcl_DecrRefCount(pTab->pCmd);
245 sqlite3_free(pTab);
246 return SQLITE_OK;
250 ** Open a new tcl cursor.
252 static int tclOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
253 tcl_cursor *pCur;
254 pCur = sqlite3_malloc(sizeof(tcl_cursor));
255 if( pCur==0 ) return SQLITE_NOMEM;
256 memset(pCur, 0, sizeof(tcl_cursor));
257 *ppCursor = &pCur->base;
258 return SQLITE_OK;
262 ** Close a tcl cursor.
264 static int tclClose(sqlite3_vtab_cursor *cur){
265 tcl_cursor *pCur = (tcl_cursor *)cur;
266 if( pCur ){
267 sqlite3_finalize(pCur->pStmt);
268 sqlite3_free(pCur);
270 return SQLITE_OK;
273 static int tclNext(sqlite3_vtab_cursor *pVtabCursor){
274 tcl_cursor *pCsr = (tcl_cursor*)pVtabCursor;
275 if( pCsr->pStmt ){
276 tcl_vtab *pTab = (tcl_vtab*)(pVtabCursor->pVtab);
277 int rc = sqlite3_step(pCsr->pStmt);
278 if( rc!=SQLITE_ROW ){
279 const char *zErr;
280 rc = sqlite3_finalize(pCsr->pStmt);
281 pCsr->pStmt = 0;
282 if( rc!=SQLITE_OK ){
283 zErr = sqlite3_errmsg(pTab->db);
284 pTab->base.zErrMsg = sqlite3_mprintf("%s", zErr);
288 return SQLITE_OK;
291 static int tclFilter(
292 sqlite3_vtab_cursor *pVtabCursor,
293 int idxNum, const char *idxStr,
294 int argc, sqlite3_value **argv
296 tcl_cursor *pCsr = (tcl_cursor*)pVtabCursor;
297 tcl_vtab *pTab = (tcl_vtab*)(pVtabCursor->pVtab);
298 Tcl_Interp *interp = pTab->interp;
299 Tcl_Obj *pScript;
300 Tcl_Obj *pArg;
301 int ii;
302 int rc;
304 pScript = Tcl_DuplicateObj(pTab->pCmd);
305 Tcl_IncrRefCount(pScript);
306 Tcl_ListObjAppendElement(interp, pScript, Tcl_NewStringObj("xFilter", -1));
307 Tcl_ListObjAppendElement(interp, pScript, Tcl_NewIntObj(idxNum));
308 if( idxStr ){
309 Tcl_ListObjAppendElement(interp, pScript, Tcl_NewStringObj(idxStr, -1));
310 }else{
311 Tcl_ListObjAppendElement(interp, pScript, Tcl_NewStringObj("", -1));
314 pArg = Tcl_NewObj();
315 Tcl_IncrRefCount(pArg);
316 for(ii=0; ii<argc; ii++){
317 const char *zVal = (const char*)sqlite3_value_text(argv[ii]);
318 Tcl_Obj *pVal;
319 if( zVal==0 ){
320 sqlite3_value *pMem;
321 pVal = Tcl_NewObj();
322 for(rc=sqlite3_vtab_in_first(argv[ii], &pMem);
323 rc==SQLITE_OK && pMem;
324 rc=sqlite3_vtab_in_next(argv[ii], &pMem)
326 Tcl_Obj *pVal2 = 0;
327 zVal = (const char*)sqlite3_value_text(pMem);
328 if( zVal ){
329 pVal2 = Tcl_NewStringObj(zVal, -1);
330 }else{
331 pVal2 = Tcl_NewObj();
333 Tcl_ListObjAppendElement(interp, pVal, pVal2);
335 }else{
336 pVal = Tcl_NewStringObj(zVal, -1);
338 Tcl_ListObjAppendElement(interp, pArg, pVal);
340 Tcl_ListObjAppendElement(interp, pScript, pArg);
341 Tcl_DecrRefCount(pArg);
343 rc = Tcl_EvalObjEx(interp, pScript, TCL_EVAL_GLOBAL);
344 if( rc!=TCL_OK ){
345 const char *zErr = Tcl_GetStringResult(interp);
346 rc = SQLITE_ERROR;
347 pTab->base.zErrMsg = sqlite3_mprintf("%s", zErr);
348 }else{
349 /* Analyze the scripts return value. The return value should be a tcl
350 ** list object with an even number of elements. The first element of each
351 ** pair must be one of:
353 ** "sql" (SQL statement to return data)
355 Tcl_Obj *pRes = Tcl_GetObjResult(interp);
356 Tcl_Obj **apElem = 0;
357 int nElem;
358 rc = Tcl_ListObjGetElements(interp, pRes, &nElem, &apElem);
359 if( rc!=TCL_OK ){
360 const char *zErr = Tcl_GetStringResult(interp);
361 rc = SQLITE_ERROR;
362 pTab->base.zErrMsg = sqlite3_mprintf("%s", zErr);
363 }else{
364 for(ii=0; rc==SQLITE_OK && ii<nElem; ii+=2){
365 const char *zCmd = Tcl_GetString(apElem[ii]);
366 Tcl_Obj *p = apElem[ii+1];
367 if( sqlite3_stricmp("sql", zCmd)==0 ){
368 const char *zSql = Tcl_GetString(p);
369 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pStmt, 0);
370 if( rc!=SQLITE_OK ){
371 const char *zErr = sqlite3_errmsg(pTab->db);
372 pTab->base.zErrMsg = sqlite3_mprintf("unexpected: %s", zErr);
374 }else{
375 rc = SQLITE_ERROR;
376 pTab->base.zErrMsg = sqlite3_mprintf("unexpected: %s", zCmd);
382 if( rc==SQLITE_OK ){
383 rc = tclNext(pVtabCursor);
385 return rc;
388 static int tclColumn(
389 sqlite3_vtab_cursor *pVtabCursor,
390 sqlite3_context *ctx,
391 int i
393 tcl_cursor *pCsr = (tcl_cursor*)pVtabCursor;
394 sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pStmt, i+1));
395 return SQLITE_OK;
398 static int tclRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *pRowid){
399 tcl_cursor *pCsr = (tcl_cursor*)pVtabCursor;
400 *pRowid = sqlite3_column_int64(pCsr->pStmt, 0);
401 return SQLITE_OK;
404 static int tclEof(sqlite3_vtab_cursor *pVtabCursor){
405 tcl_cursor *pCsr = (tcl_cursor*)pVtabCursor;
406 return (pCsr->pStmt==0);
409 static void testBestIndexObjConstraints(
410 Tcl_Interp *interp,
411 sqlite3_index_info *pIdxInfo
413 int ii;
414 Tcl_Obj *pRes = Tcl_NewObj();
415 Tcl_IncrRefCount(pRes);
416 for(ii=0; ii<pIdxInfo->nConstraint; ii++){
417 struct sqlite3_index_constraint const *pCons = &pIdxInfo->aConstraint[ii];
418 Tcl_Obj *pElem = Tcl_NewObj();
419 const char *zOp = 0;
421 Tcl_IncrRefCount(pElem);
423 switch( pCons->op ){
424 case SQLITE_INDEX_CONSTRAINT_EQ:
425 zOp = "eq"; break;
426 case SQLITE_INDEX_CONSTRAINT_GT:
427 zOp = "gt"; break;
428 case SQLITE_INDEX_CONSTRAINT_LE:
429 zOp = "le"; break;
430 case SQLITE_INDEX_CONSTRAINT_LT:
431 zOp = "lt"; break;
432 case SQLITE_INDEX_CONSTRAINT_GE:
433 zOp = "ge"; break;
434 case SQLITE_INDEX_CONSTRAINT_MATCH:
435 zOp = "match"; break;
436 case SQLITE_INDEX_CONSTRAINT_LIKE:
437 zOp = "like"; break;
438 case SQLITE_INDEX_CONSTRAINT_GLOB:
439 zOp = "glob"; break;
440 case SQLITE_INDEX_CONSTRAINT_REGEXP:
441 zOp = "regexp"; break;
442 case SQLITE_INDEX_CONSTRAINT_NE:
443 zOp = "ne"; break;
444 case SQLITE_INDEX_CONSTRAINT_ISNOT:
445 zOp = "isnot"; break;
446 case SQLITE_INDEX_CONSTRAINT_ISNOTNULL:
447 zOp = "isnotnull"; break;
448 case SQLITE_INDEX_CONSTRAINT_ISNULL:
449 zOp = "isnull"; break;
450 case SQLITE_INDEX_CONSTRAINT_IS:
451 zOp = "is"; break;
452 case SQLITE_INDEX_CONSTRAINT_LIMIT:
453 zOp = "limit"; break;
454 case SQLITE_INDEX_CONSTRAINT_OFFSET:
455 zOp = "offset"; break;
458 Tcl_ListObjAppendElement(0, pElem, Tcl_NewStringObj("op", -1));
459 if( zOp ){
460 Tcl_ListObjAppendElement(0, pElem, Tcl_NewStringObj(zOp, -1));
461 }else{
462 Tcl_ListObjAppendElement(0, pElem, Tcl_NewIntObj(pCons->op));
464 Tcl_ListObjAppendElement(0, pElem, Tcl_NewStringObj("column", -1));
465 Tcl_ListObjAppendElement(0, pElem, Tcl_NewIntObj(pCons->iColumn));
466 Tcl_ListObjAppendElement(0, pElem, Tcl_NewStringObj("usable", -1));
467 Tcl_ListObjAppendElement(0, pElem, Tcl_NewIntObj(pCons->usable));
469 Tcl_ListObjAppendElement(0, pRes, pElem);
470 Tcl_DecrRefCount(pElem);
473 Tcl_SetObjResult(interp, pRes);
474 Tcl_DecrRefCount(pRes);
477 static void testBestIndexObjOrderby(
478 Tcl_Interp *interp,
479 sqlite3_index_info *pIdxInfo
481 int ii;
482 Tcl_Obj *pRes = Tcl_NewObj();
483 Tcl_IncrRefCount(pRes);
484 for(ii=0; ii<pIdxInfo->nOrderBy; ii++){
485 struct sqlite3_index_orderby const *pOrder = &pIdxInfo->aOrderBy[ii];
486 Tcl_Obj *pElem = Tcl_NewObj();
487 Tcl_IncrRefCount(pElem);
489 Tcl_ListObjAppendElement(0, pElem, Tcl_NewStringObj("column", -1));
490 Tcl_ListObjAppendElement(0, pElem, Tcl_NewIntObj(pOrder->iColumn));
491 Tcl_ListObjAppendElement(0, pElem, Tcl_NewStringObj("desc", -1));
492 Tcl_ListObjAppendElement(0, pElem, Tcl_NewIntObj(pOrder->desc));
494 Tcl_ListObjAppendElement(0, pRes, pElem);
495 Tcl_DecrRefCount(pElem);
498 Tcl_SetObjResult(interp, pRes);
499 Tcl_DecrRefCount(pRes);
503 ** Implementation of the handle passed to each xBestIndex callback. This
504 ** object features the following sub-commands:
506 ** $hdl constraints
507 ** $hdl orderby
508 ** $hdl mask
510 ** $hdl distinct
511 ** Return the result (an integer) of calling sqlite3_vtab_distinct()
512 ** on the index-info structure.
514 ** $hdl in IDX BOOLEAN
515 ** Wrapper around sqlite3_vtab_in(). Returns an integer.
517 ** $hdl rhs_value IDX ?DEFAULT?
518 ** Wrapper around sqlite3_vtab_rhs_value().
520 static int SQLITE_TCLAPI testBestIndexObj(
521 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
522 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
523 int objc, /* Number of arguments */
524 Tcl_Obj *CONST objv[] /* Command arguments */
526 const char *azSub[] = {
527 "constraints", /* 0 */
528 "orderby", /* 1 */
529 "mask", /* 2 */
530 "distinct", /* 3 */
531 "in", /* 4 */
532 "rhs_value", /* 5 */
535 int ii;
536 sqlite3_index_info *pIdxInfo = (sqlite3_index_info*)clientData;
538 if( objc<2 ){
539 Tcl_WrongNumArgs(interp, 1, objv, "SUB-COMMAND");
540 return TCL_ERROR;
542 if( Tcl_GetIndexFromObj(interp, objv[1], azSub, "sub-command", 0, &ii) ){
543 return TCL_ERROR;
546 if( ii<4 && objc!=2 ){
547 Tcl_WrongNumArgs(interp, 2, objv, "");
548 return TCL_ERROR;
550 if( ii==4 && objc!=4 ){
551 Tcl_WrongNumArgs(interp, 2, objv, "INDEX BOOLEAN");
552 return TCL_ERROR;
554 if( ii==5 && objc!=3 && objc!=4 ){
555 Tcl_WrongNumArgs(interp, 2, objv, "INDEX ?DEFAULT?");
556 return TCL_ERROR;
559 switch( ii ){
560 case 0: assert( sqlite3_stricmp(azSub[ii], "constraints")==0 );
561 testBestIndexObjConstraints(interp, pIdxInfo);
562 break;
564 case 1: assert( sqlite3_stricmp(azSub[ii], "orderby")==0 );
565 testBestIndexObjOrderby(interp, pIdxInfo);
566 break;
568 case 2: assert( sqlite3_stricmp(azSub[ii], "mask")==0 );
569 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(pIdxInfo->colUsed));
570 break;
572 case 3: assert( sqlite3_stricmp(azSub[ii], "distinct")==0 ); {
573 int bDistinct = sqlite3_vtab_distinct(pIdxInfo);
574 Tcl_SetObjResult(interp, Tcl_NewIntObj(bDistinct));
575 break;
578 case 4: assert( sqlite3_stricmp(azSub[ii], "in")==0 ); {
579 int iCons;
580 int bHandle;
581 if( Tcl_GetIntFromObj(interp, objv[2], &iCons)
582 || Tcl_GetBooleanFromObj(interp, objv[3], &bHandle)
584 return TCL_ERROR;
586 Tcl_SetObjResult(interp,
587 Tcl_NewIntObj(sqlite3_vtab_in(pIdxInfo, iCons, bHandle))
589 break;
592 case 5: assert( sqlite3_stricmp(azSub[ii], "rhs_value")==0 ); {
593 int iCons = 0;
594 int rc;
595 sqlite3_value *pVal = 0;
596 const char *zVal = "";
597 if( Tcl_GetIntFromObj(interp, objv[2], &iCons) ){
598 return TCL_ERROR;
600 rc = sqlite3_vtab_rhs_value(pIdxInfo, iCons, &pVal);
601 if( rc!=SQLITE_OK && rc!=SQLITE_NOTFOUND ){
602 Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE);
603 return TCL_ERROR;
605 if( pVal ){
606 zVal = (const char*)sqlite3_value_text(pVal);
607 }else if( objc==4 ){
608 zVal = Tcl_GetString(objv[3]);
610 Tcl_SetObjResult(interp, Tcl_NewStringObj(zVal, -1));
611 break;
615 return TCL_OK;
618 static int tclBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
619 tcl_vtab *pTab = (tcl_vtab*)tab;
620 Tcl_Interp *interp = pTab->interp;
621 int rc = SQLITE_OK;
623 static int iNext = 43;
624 char zHdl[24];
625 Tcl_Obj *pScript;
627 pScript = Tcl_DuplicateObj(pTab->pCmd);
628 Tcl_IncrRefCount(pScript);
629 Tcl_ListObjAppendElement(interp, pScript, Tcl_NewStringObj("xBestIndex", -1));
631 sqlite3_snprintf(sizeof(zHdl), zHdl, "bestindex%d", iNext++);
632 Tcl_CreateObjCommand(interp, zHdl, testBestIndexObj, pIdxInfo, 0);
633 Tcl_ListObjAppendElement(interp, pScript, Tcl_NewStringObj(zHdl, -1));
634 rc = Tcl_EvalObjEx(interp, pScript, TCL_EVAL_GLOBAL);
635 Tcl_DeleteCommand(interp, zHdl);
636 Tcl_DecrRefCount(pScript);
638 if( rc!=TCL_OK ){
639 const char *zErr = Tcl_GetStringResult(interp);
640 rc = SQLITE_ERROR;
641 pTab->base.zErrMsg = sqlite3_mprintf("%s", zErr);
642 }else{
643 /* Analyze the scripts return value. The return value should be a tcl
644 ** list object with an even number of elements. The first element of each
645 ** pair must be one of:
647 ** "orderby" (value of orderByConsumed flag)
648 ** "cost" (value of estimatedCost field)
649 ** "rows" (value of estimatedRows field)
650 ** "use" (index of used constraint in aConstraint[])
651 ** "idxnum" (value of idxNum field)
652 ** "idxstr" (value of idxStr field)
653 ** "omit" (index of omitted constraint in aConstraint[])
655 Tcl_Obj *pRes = Tcl_GetObjResult(interp);
656 Tcl_Obj **apElem = 0;
657 int nElem;
658 rc = Tcl_ListObjGetElements(interp, pRes, &nElem, &apElem);
659 if( rc!=TCL_OK ){
660 const char *zErr = Tcl_GetStringResult(interp);
661 rc = SQLITE_ERROR;
662 pTab->base.zErrMsg = sqlite3_mprintf("%s", zErr);
663 }else{
664 int ii;
665 int iArgv = 1;
666 for(ii=0; rc==SQLITE_OK && ii<nElem; ii+=2){
667 const char *zCmd = Tcl_GetString(apElem[ii]);
668 Tcl_Obj *p = apElem[ii+1];
669 if( sqlite3_stricmp("cost", zCmd)==0 ){
670 rc = Tcl_GetDoubleFromObj(interp, p, &pIdxInfo->estimatedCost);
671 }else
672 if( sqlite3_stricmp("orderby", zCmd)==0 ){
673 rc = Tcl_GetIntFromObj(interp, p, &pIdxInfo->orderByConsumed);
674 }else
675 if( sqlite3_stricmp("idxnum", zCmd)==0 ){
676 rc = Tcl_GetIntFromObj(interp, p, &pIdxInfo->idxNum);
677 }else
678 if( sqlite3_stricmp("idxstr", zCmd)==0 ){
679 sqlite3_free(pIdxInfo->idxStr);
680 pIdxInfo->idxStr = sqlite3_mprintf("%s", Tcl_GetString(p));
681 pIdxInfo->needToFreeIdxStr = 1;
682 }else
683 if( sqlite3_stricmp("rows", zCmd)==0 ){
684 Tcl_WideInt x = 0;
685 rc = Tcl_GetWideIntFromObj(interp, p, &x);
686 pIdxInfo->estimatedRows = (tRowcnt)x;
687 }else
688 if( sqlite3_stricmp("use", zCmd)==0
689 || sqlite3_stricmp("omit", zCmd)==0
691 int iCons;
692 rc = Tcl_GetIntFromObj(interp, p, &iCons);
693 if( rc==SQLITE_OK ){
694 if( iCons<0 || iCons>=pIdxInfo->nConstraint ){
695 rc = SQLITE_ERROR;
696 pTab->base.zErrMsg = sqlite3_mprintf("unexpected: %d", iCons);
697 }else{
698 int bOmit = (zCmd[0]=='o' || zCmd[0]=='O');
699 pIdxInfo->aConstraintUsage[iCons].argvIndex = iArgv++;
700 pIdxInfo->aConstraintUsage[iCons].omit = bOmit;
703 }else{
704 rc = SQLITE_ERROR;
705 pTab->base.zErrMsg = sqlite3_mprintf("unexpected: %s", zCmd);
707 if( rc!=SQLITE_OK && pTab->base.zErrMsg==0 ){
708 const char *zErr = Tcl_GetStringResult(interp);
709 pTab->base.zErrMsg = sqlite3_mprintf("%s", zErr);
715 return rc;
718 static void tclFunction(sqlite3_context *pCtx, int nArg, sqlite3_value **apArg){
719 TestFindFunction *p = (TestFindFunction*)sqlite3_user_data(pCtx);
720 Tcl_Interp *interp = p->pTab->interp;
721 Tcl_Obj *pScript = 0;
722 Tcl_Obj *pRet = 0;
723 int ii;
725 pScript = Tcl_DuplicateObj(p->pTab->pCmd);
726 Tcl_IncrRefCount(pScript);
727 Tcl_ListObjAppendElement(interp, pScript, Tcl_NewStringObj("function", -1));
728 Tcl_ListObjAppendElement(interp, pScript, Tcl_NewStringObj(p->zName, -1));
730 for(ii=0; ii<nArg; ii++){
731 const char *zArg = (const char*)sqlite3_value_text(apArg[ii]);
732 Tcl_ListObjAppendElement(interp, pScript,
733 (zArg ? Tcl_NewStringObj(zArg, -1) : Tcl_NewObj())
736 Tcl_EvalObjEx(interp, pScript, TCL_EVAL_GLOBAL);
737 Tcl_DecrRefCount(pScript);
739 pRet = Tcl_GetObjResult(interp);
740 sqlite3_result_text(pCtx, Tcl_GetString(pRet), -1, SQLITE_TRANSIENT);
743 static int tclFindFunction(
744 sqlite3_vtab *tab,
745 int nArg,
746 const char *zName,
747 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT */
748 void **ppArg /* OUT */
750 int iRet = 0;
751 tcl_vtab *pTab = (tcl_vtab*)tab;
752 Tcl_Interp *interp = pTab->interp;
753 Tcl_Obj *pScript = 0;
754 int rc = SQLITE_OK;
756 pScript = Tcl_DuplicateObj(pTab->pCmd);
757 Tcl_IncrRefCount(pScript);
758 Tcl_ListObjAppendElement(
759 interp, pScript, Tcl_NewStringObj("xFindFunction", -1)
761 Tcl_ListObjAppendElement(interp, pScript, Tcl_NewIntObj(nArg));
762 Tcl_ListObjAppendElement(interp, pScript, Tcl_NewStringObj(zName, -1));
763 rc = Tcl_EvalObjEx(interp, pScript, TCL_EVAL_GLOBAL);
764 Tcl_DecrRefCount(pScript);
766 if( rc==SQLITE_OK ){
767 Tcl_Obj *pObj = Tcl_GetObjResult(interp);
769 if( Tcl_GetIntFromObj(interp, pObj, &iRet) ){
770 rc = SQLITE_ERROR;
771 }else if( iRet>0 ){
772 sqlite3_int64 nName = strlen(zName);
773 sqlite3_int64 nByte = nName + 1 + sizeof(TestFindFunction);
774 TestFindFunction *pNew = 0;
776 pNew = (TestFindFunction*)sqlite3_malloc64(nByte);
777 if( pNew==0 ){
778 iRet = 0;
779 }else{
780 memset(pNew, 0, nByte);
781 pNew->zName = (const char*)&pNew[1];
782 memcpy((char*)pNew->zName, zName, nName);
783 pNew->pTab = pTab;
784 pNew->pNext = pTab->pFindFunctionList;
785 pTab->pFindFunctionList = pNew;
786 *ppArg = (void*)pNew;
787 *pxFunc = tclFunction;
792 return iRet;
796 ** A virtual table module that provides read-only access to a
797 ** Tcl global variable namespace.
799 static sqlite3_module tclModule = {
800 0, /* iVersion */
801 tclConnect,
802 tclConnect,
803 tclBestIndex,
804 tclDisconnect,
805 tclDisconnect,
806 tclOpen, /* xOpen - open a cursor */
807 tclClose, /* xClose - close a cursor */
808 tclFilter, /* xFilter - configure scan constraints */
809 tclNext, /* xNext - advance a cursor */
810 tclEof, /* xEof - check for end of scan */
811 tclColumn, /* xColumn - read data */
812 tclRowid, /* xRowid - read data */
813 0, /* xUpdate */
814 0, /* xBegin */
815 0, /* xSync */
816 0, /* xCommit */
817 0, /* xRollback */
818 tclFindFunction, /* xFindFunction */
819 0, /* xRename */
820 0, /* xSavepoint */
821 0, /* xRelease */
822 0, /* xRollbackTo */
823 0, /* xShadowName */
824 0 /* xIntegrity */
828 ** Decode a pointer to an sqlite3 object.
830 extern int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb);
833 ** Register the echo virtual table module.
835 static int SQLITE_TCLAPI register_tcl_module(
836 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
837 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
838 int objc, /* Number of arguments */
839 Tcl_Obj *CONST objv[] /* Command arguments */
841 sqlite3 *db;
842 if( objc!=2 ){
843 Tcl_WrongNumArgs(interp, 1, objv, "DB");
844 return TCL_ERROR;
846 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
847 #ifndef SQLITE_OMIT_VIRTUALTABLE
848 sqlite3_create_module(db, "tcl", &tclModule, (void *)interp);
849 #endif
850 return TCL_OK;
853 #endif
857 ** Register commands with the TCL interpreter.
859 int Sqlitetesttcl_Init(Tcl_Interp *interp){
860 #ifndef SQLITE_OMIT_VIRTUALTABLE
861 static struct {
862 char *zName;
863 Tcl_ObjCmdProc *xProc;
864 void *clientData;
865 } aObjCmd[] = {
866 { "register_tcl_module", register_tcl_module, 0 },
868 int i;
869 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
870 Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
871 aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
873 #endif
874 return TCL_OK;