Add a notice to the user-authentication documentation to say that the
[sqlite.git] / src / test_bestindex.c
blob8128530b402c64900c343922fb1ec79455ccd3f5
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));
220 if( rc!=SQLITE_OK ){
221 sqlite3_free(pTab);
222 pTab = 0;
224 }else{
225 rc = SQLITE_NOMEM;
228 sqlite3_free(zCmd);
229 *ppVtab = &pTab->base;
230 return rc;
233 /* The xDisconnect and xDestroy methods are also the same */
234 static int tclDisconnect(sqlite3_vtab *pVtab){
235 tcl_vtab *pTab = (tcl_vtab*)pVtab;
236 while( pTab->pFindFunctionList ){
237 TestFindFunction *p = pTab->pFindFunctionList;
238 pTab->pFindFunctionList = p->pNext;
239 sqlite3_free(p);
241 Tcl_DecrRefCount(pTab->pCmd);
242 sqlite3_free(pTab);
243 return SQLITE_OK;
247 ** Open a new tcl cursor.
249 static int tclOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
250 tcl_cursor *pCur;
251 pCur = sqlite3_malloc(sizeof(tcl_cursor));
252 if( pCur==0 ) return SQLITE_NOMEM;
253 memset(pCur, 0, sizeof(tcl_cursor));
254 *ppCursor = &pCur->base;
255 return SQLITE_OK;
259 ** Close a tcl cursor.
261 static int tclClose(sqlite3_vtab_cursor *cur){
262 tcl_cursor *pCur = (tcl_cursor *)cur;
263 if( pCur ){
264 sqlite3_finalize(pCur->pStmt);
265 sqlite3_free(pCur);
267 return SQLITE_OK;
270 static int tclNext(sqlite3_vtab_cursor *pVtabCursor){
271 tcl_cursor *pCsr = (tcl_cursor*)pVtabCursor;
272 if( pCsr->pStmt ){
273 tcl_vtab *pTab = (tcl_vtab*)(pVtabCursor->pVtab);
274 int rc = sqlite3_step(pCsr->pStmt);
275 if( rc!=SQLITE_ROW ){
276 const char *zErr;
277 rc = sqlite3_finalize(pCsr->pStmt);
278 pCsr->pStmt = 0;
279 if( rc!=SQLITE_OK ){
280 zErr = sqlite3_errmsg(pTab->db);
281 pTab->base.zErrMsg = sqlite3_mprintf("%s", zErr);
285 return SQLITE_OK;
288 static int tclFilter(
289 sqlite3_vtab_cursor *pVtabCursor,
290 int idxNum, const char *idxStr,
291 int argc, sqlite3_value **argv
293 tcl_cursor *pCsr = (tcl_cursor*)pVtabCursor;
294 tcl_vtab *pTab = (tcl_vtab*)(pVtabCursor->pVtab);
295 Tcl_Interp *interp = pTab->interp;
296 Tcl_Obj *pScript;
297 Tcl_Obj *pArg;
298 int ii;
299 int rc;
301 pScript = Tcl_DuplicateObj(pTab->pCmd);
302 Tcl_IncrRefCount(pScript);
303 Tcl_ListObjAppendElement(interp, pScript, Tcl_NewStringObj("xFilter", -1));
304 Tcl_ListObjAppendElement(interp, pScript, Tcl_NewIntObj(idxNum));
305 if( idxStr ){
306 Tcl_ListObjAppendElement(interp, pScript, Tcl_NewStringObj(idxStr, -1));
307 }else{
308 Tcl_ListObjAppendElement(interp, pScript, Tcl_NewStringObj("", -1));
311 pArg = Tcl_NewObj();
312 Tcl_IncrRefCount(pArg);
313 for(ii=0; ii<argc; ii++){
314 const char *zVal = (const char*)sqlite3_value_text(argv[ii]);
315 Tcl_Obj *pVal;
316 if( zVal==0 ){
317 sqlite3_value *pMem;
318 pVal = Tcl_NewObj();
319 for(rc=sqlite3_vtab_in_first(argv[ii], &pMem);
320 rc==SQLITE_OK && pMem;
321 rc=sqlite3_vtab_in_next(argv[ii], &pMem)
323 Tcl_Obj *pVal2 = 0;
324 zVal = (const char*)sqlite3_value_text(pMem);
325 if( zVal ){
326 pVal2 = Tcl_NewStringObj(zVal, -1);
327 }else{
328 pVal2 = Tcl_NewObj();
330 Tcl_ListObjAppendElement(interp, pVal, pVal2);
332 }else{
333 pVal = Tcl_NewStringObj(zVal, -1);
335 Tcl_ListObjAppendElement(interp, pArg, pVal);
337 Tcl_ListObjAppendElement(interp, pScript, pArg);
338 Tcl_DecrRefCount(pArg);
340 rc = Tcl_EvalObjEx(interp, pScript, TCL_EVAL_GLOBAL);
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 /* Analyze the scripts return value. The return value should be a tcl
347 ** list object with an even number of elements. The first element of each
348 ** pair must be one of:
350 ** "sql" (SQL statement to return data)
352 Tcl_Obj *pRes = Tcl_GetObjResult(interp);
353 Tcl_Obj **apElem = 0;
354 int nElem;
355 rc = Tcl_ListObjGetElements(interp, pRes, &nElem, &apElem);
356 if( rc!=TCL_OK ){
357 const char *zErr = Tcl_GetStringResult(interp);
358 rc = SQLITE_ERROR;
359 pTab->base.zErrMsg = sqlite3_mprintf("%s", zErr);
360 }else{
361 for(ii=0; rc==SQLITE_OK && ii<nElem; ii+=2){
362 const char *zCmd = Tcl_GetString(apElem[ii]);
363 Tcl_Obj *p = apElem[ii+1];
364 if( sqlite3_stricmp("sql", zCmd)==0 ){
365 const char *zSql = Tcl_GetString(p);
366 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pStmt, 0);
367 if( rc!=SQLITE_OK ){
368 const char *zErr = sqlite3_errmsg(pTab->db);
369 pTab->base.zErrMsg = sqlite3_mprintf("unexpected: %s", zErr);
371 }else{
372 rc = SQLITE_ERROR;
373 pTab->base.zErrMsg = sqlite3_mprintf("unexpected: %s", zCmd);
379 if( rc==SQLITE_OK ){
380 rc = tclNext(pVtabCursor);
382 return rc;
385 static int tclColumn(
386 sqlite3_vtab_cursor *pVtabCursor,
387 sqlite3_context *ctx,
388 int i
390 tcl_cursor *pCsr = (tcl_cursor*)pVtabCursor;
391 sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pStmt, i+1));
392 return SQLITE_OK;
395 static int tclRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *pRowid){
396 tcl_cursor *pCsr = (tcl_cursor*)pVtabCursor;
397 *pRowid = sqlite3_column_int64(pCsr->pStmt, 0);
398 return SQLITE_OK;
401 static int tclEof(sqlite3_vtab_cursor *pVtabCursor){
402 tcl_cursor *pCsr = (tcl_cursor*)pVtabCursor;
403 return (pCsr->pStmt==0);
406 static void testBestIndexObjConstraints(
407 Tcl_Interp *interp,
408 sqlite3_index_info *pIdxInfo
410 int ii;
411 Tcl_Obj *pRes = Tcl_NewObj();
412 Tcl_IncrRefCount(pRes);
413 for(ii=0; ii<pIdxInfo->nConstraint; ii++){
414 struct sqlite3_index_constraint const *pCons = &pIdxInfo->aConstraint[ii];
415 Tcl_Obj *pElem = Tcl_NewObj();
416 const char *zOp = 0;
418 Tcl_IncrRefCount(pElem);
420 switch( pCons->op ){
421 case SQLITE_INDEX_CONSTRAINT_EQ:
422 zOp = "eq"; break;
423 case SQLITE_INDEX_CONSTRAINT_GT:
424 zOp = "gt"; break;
425 case SQLITE_INDEX_CONSTRAINT_LE:
426 zOp = "le"; break;
427 case SQLITE_INDEX_CONSTRAINT_LT:
428 zOp = "lt"; break;
429 case SQLITE_INDEX_CONSTRAINT_GE:
430 zOp = "ge"; break;
431 case SQLITE_INDEX_CONSTRAINT_MATCH:
432 zOp = "match"; break;
433 case SQLITE_INDEX_CONSTRAINT_LIKE:
434 zOp = "like"; break;
435 case SQLITE_INDEX_CONSTRAINT_GLOB:
436 zOp = "glob"; break;
437 case SQLITE_INDEX_CONSTRAINT_REGEXP:
438 zOp = "regexp"; break;
439 case SQLITE_INDEX_CONSTRAINT_NE:
440 zOp = "ne"; break;
441 case SQLITE_INDEX_CONSTRAINT_ISNOT:
442 zOp = "isnot"; break;
443 case SQLITE_INDEX_CONSTRAINT_ISNOTNULL:
444 zOp = "isnotnull"; break;
445 case SQLITE_INDEX_CONSTRAINT_ISNULL:
446 zOp = "isnull"; break;
447 case SQLITE_INDEX_CONSTRAINT_IS:
448 zOp = "is"; break;
449 case SQLITE_INDEX_CONSTRAINT_LIMIT:
450 zOp = "limit"; break;
451 case SQLITE_INDEX_CONSTRAINT_OFFSET:
452 zOp = "offset"; break;
455 Tcl_ListObjAppendElement(0, pElem, Tcl_NewStringObj("op", -1));
456 if( zOp ){
457 Tcl_ListObjAppendElement(0, pElem, Tcl_NewStringObj(zOp, -1));
458 }else{
459 Tcl_ListObjAppendElement(0, pElem, Tcl_NewIntObj(pCons->op));
461 Tcl_ListObjAppendElement(0, pElem, Tcl_NewStringObj("column", -1));
462 Tcl_ListObjAppendElement(0, pElem, Tcl_NewIntObj(pCons->iColumn));
463 Tcl_ListObjAppendElement(0, pElem, Tcl_NewStringObj("usable", -1));
464 Tcl_ListObjAppendElement(0, pElem, Tcl_NewIntObj(pCons->usable));
466 Tcl_ListObjAppendElement(0, pRes, pElem);
467 Tcl_DecrRefCount(pElem);
470 Tcl_SetObjResult(interp, pRes);
471 Tcl_DecrRefCount(pRes);
474 static void testBestIndexObjOrderby(
475 Tcl_Interp *interp,
476 sqlite3_index_info *pIdxInfo
478 int ii;
479 Tcl_Obj *pRes = Tcl_NewObj();
480 Tcl_IncrRefCount(pRes);
481 for(ii=0; ii<pIdxInfo->nOrderBy; ii++){
482 struct sqlite3_index_orderby const *pOrder = &pIdxInfo->aOrderBy[ii];
483 Tcl_Obj *pElem = Tcl_NewObj();
484 Tcl_IncrRefCount(pElem);
486 Tcl_ListObjAppendElement(0, pElem, Tcl_NewStringObj("column", -1));
487 Tcl_ListObjAppendElement(0, pElem, Tcl_NewIntObj(pOrder->iColumn));
488 Tcl_ListObjAppendElement(0, pElem, Tcl_NewStringObj("desc", -1));
489 Tcl_ListObjAppendElement(0, pElem, Tcl_NewIntObj(pOrder->desc));
491 Tcl_ListObjAppendElement(0, pRes, pElem);
492 Tcl_DecrRefCount(pElem);
495 Tcl_SetObjResult(interp, pRes);
496 Tcl_DecrRefCount(pRes);
500 ** Implementation of the handle passed to each xBestIndex callback. This
501 ** object features the following sub-commands:
503 ** $hdl constraints
504 ** $hdl orderby
505 ** $hdl mask
507 ** $hdl distinct
508 ** Return the result (an integer) of calling sqlite3_vtab_distinct()
509 ** on the index-info structure.
511 ** $hdl in IDX BOOLEAN
512 ** Wrapper around sqlite3_vtab_in(). Returns an integer.
514 ** $hdl rhs_value IDX ?DEFAULT?
515 ** Wrapper around sqlite3_vtab_rhs_value().
517 static int SQLITE_TCLAPI testBestIndexObj(
518 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
519 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
520 int objc, /* Number of arguments */
521 Tcl_Obj *CONST objv[] /* Command arguments */
523 const char *azSub[] = {
524 "constraints", /* 0 */
525 "orderby", /* 1 */
526 "mask", /* 2 */
527 "distinct", /* 3 */
528 "in", /* 4 */
529 "rhs_value", /* 5 */
532 int ii;
533 sqlite3_index_info *pIdxInfo = (sqlite3_index_info*)clientData;
535 if( objc<2 ){
536 Tcl_WrongNumArgs(interp, 1, objv, "SUB-COMMAND");
537 return TCL_ERROR;
539 if( Tcl_GetIndexFromObj(interp, objv[1], azSub, "sub-command", 0, &ii) ){
540 return TCL_ERROR;
543 if( ii<4 && objc!=2 ){
544 Tcl_WrongNumArgs(interp, 2, objv, "");
545 return TCL_ERROR;
547 if( ii==4 && objc!=4 ){
548 Tcl_WrongNumArgs(interp, 2, objv, "INDEX BOOLEAN");
549 return TCL_ERROR;
551 if( ii==5 && objc!=3 && objc!=4 ){
552 Tcl_WrongNumArgs(interp, 2, objv, "INDEX ?DEFAULT?");
553 return TCL_ERROR;
556 switch( ii ){
557 case 0: assert( sqlite3_stricmp(azSub[ii], "constraints")==0 );
558 testBestIndexObjConstraints(interp, pIdxInfo);
559 break;
561 case 1: assert( sqlite3_stricmp(azSub[ii], "orderby")==0 );
562 testBestIndexObjOrderby(interp, pIdxInfo);
563 break;
565 case 2: assert( sqlite3_stricmp(azSub[ii], "mask")==0 );
566 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(pIdxInfo->colUsed));
567 break;
569 case 3: assert( sqlite3_stricmp(azSub[ii], "distinct")==0 ); {
570 int bDistinct = sqlite3_vtab_distinct(pIdxInfo);
571 Tcl_SetObjResult(interp, Tcl_NewIntObj(bDistinct));
572 break;
575 case 4: assert( sqlite3_stricmp(azSub[ii], "in")==0 ); {
576 int iCons;
577 int bHandle;
578 if( Tcl_GetIntFromObj(interp, objv[2], &iCons)
579 || Tcl_GetBooleanFromObj(interp, objv[3], &bHandle)
581 return TCL_ERROR;
583 Tcl_SetObjResult(interp,
584 Tcl_NewIntObj(sqlite3_vtab_in(pIdxInfo, iCons, bHandle))
586 break;
589 case 5: assert( sqlite3_stricmp(azSub[ii], "rhs_value")==0 ); {
590 int iCons = 0;
591 int rc;
592 sqlite3_value *pVal = 0;
593 const char *zVal = "";
594 if( Tcl_GetIntFromObj(interp, objv[2], &iCons) ){
595 return TCL_ERROR;
597 rc = sqlite3_vtab_rhs_value(pIdxInfo, iCons, &pVal);
598 if( rc!=SQLITE_OK && rc!=SQLITE_NOTFOUND ){
599 Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE);
600 return TCL_ERROR;
602 if( pVal ){
603 zVal = (const char*)sqlite3_value_text(pVal);
604 }else if( objc==4 ){
605 zVal = Tcl_GetString(objv[3]);
607 Tcl_SetObjResult(interp, Tcl_NewStringObj(zVal, -1));
608 break;
612 return TCL_OK;
615 static int tclBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
616 tcl_vtab *pTab = (tcl_vtab*)tab;
617 Tcl_Interp *interp = pTab->interp;
618 int rc = SQLITE_OK;
620 static int iNext = 43;
621 char zHdl[24];
622 Tcl_Obj *pScript;
624 pScript = Tcl_DuplicateObj(pTab->pCmd);
625 Tcl_IncrRefCount(pScript);
626 Tcl_ListObjAppendElement(interp, pScript, Tcl_NewStringObj("xBestIndex", -1));
628 sqlite3_snprintf(sizeof(zHdl), zHdl, "bestindex%d", iNext++);
629 Tcl_CreateObjCommand(interp, zHdl, testBestIndexObj, pIdxInfo, 0);
630 Tcl_ListObjAppendElement(interp, pScript, Tcl_NewStringObj(zHdl, -1));
631 rc = Tcl_EvalObjEx(interp, pScript, TCL_EVAL_GLOBAL);
632 Tcl_DeleteCommand(interp, zHdl);
633 Tcl_DecrRefCount(pScript);
635 if( rc!=TCL_OK ){
636 const char *zErr = Tcl_GetStringResult(interp);
637 rc = SQLITE_ERROR;
638 pTab->base.zErrMsg = sqlite3_mprintf("%s", zErr);
639 }else{
640 /* Analyze the scripts return value. The return value should be a tcl
641 ** list object with an even number of elements. The first element of each
642 ** pair must be one of:
644 ** "orderby" (value of orderByConsumed flag)
645 ** "cost" (value of estimatedCost field)
646 ** "rows" (value of estimatedRows field)
647 ** "use" (index of used constraint in aConstraint[])
648 ** "idxnum" (value of idxNum field)
649 ** "idxstr" (value of idxStr field)
650 ** "omit" (index of omitted constraint in aConstraint[])
652 Tcl_Obj *pRes = Tcl_GetObjResult(interp);
653 Tcl_Obj **apElem = 0;
654 int nElem;
655 rc = Tcl_ListObjGetElements(interp, pRes, &nElem, &apElem);
656 if( rc!=TCL_OK ){
657 const char *zErr = Tcl_GetStringResult(interp);
658 rc = SQLITE_ERROR;
659 pTab->base.zErrMsg = sqlite3_mprintf("%s", zErr);
660 }else{
661 int ii;
662 int iArgv = 1;
663 for(ii=0; rc==SQLITE_OK && ii<nElem; ii+=2){
664 const char *zCmd = Tcl_GetString(apElem[ii]);
665 Tcl_Obj *p = apElem[ii+1];
666 if( sqlite3_stricmp("cost", zCmd)==0 ){
667 rc = Tcl_GetDoubleFromObj(interp, p, &pIdxInfo->estimatedCost);
668 }else
669 if( sqlite3_stricmp("orderby", zCmd)==0 ){
670 rc = Tcl_GetIntFromObj(interp, p, &pIdxInfo->orderByConsumed);
671 }else
672 if( sqlite3_stricmp("idxnum", zCmd)==0 ){
673 rc = Tcl_GetIntFromObj(interp, p, &pIdxInfo->idxNum);
674 }else
675 if( sqlite3_stricmp("idxstr", zCmd)==0 ){
676 sqlite3_free(pIdxInfo->idxStr);
677 pIdxInfo->idxStr = sqlite3_mprintf("%s", Tcl_GetString(p));
678 pIdxInfo->needToFreeIdxStr = 1;
679 }else
680 if( sqlite3_stricmp("rows", zCmd)==0 ){
681 Tcl_WideInt x = 0;
682 rc = Tcl_GetWideIntFromObj(interp, p, &x);
683 pIdxInfo->estimatedRows = (tRowcnt)x;
684 }else
685 if( sqlite3_stricmp("use", zCmd)==0
686 || sqlite3_stricmp("omit", zCmd)==0
688 int iCons;
689 rc = Tcl_GetIntFromObj(interp, p, &iCons);
690 if( rc==SQLITE_OK ){
691 if( iCons<0 || iCons>=pIdxInfo->nConstraint ){
692 rc = SQLITE_ERROR;
693 pTab->base.zErrMsg = sqlite3_mprintf("unexpected: %d", iCons);
694 }else{
695 int bOmit = (zCmd[0]=='o' || zCmd[0]=='O');
696 pIdxInfo->aConstraintUsage[iCons].argvIndex = iArgv++;
697 pIdxInfo->aConstraintUsage[iCons].omit = bOmit;
700 }else{
701 rc = SQLITE_ERROR;
702 pTab->base.zErrMsg = sqlite3_mprintf("unexpected: %s", zCmd);
704 if( rc!=SQLITE_OK && pTab->base.zErrMsg==0 ){
705 const char *zErr = Tcl_GetStringResult(interp);
706 pTab->base.zErrMsg = sqlite3_mprintf("%s", zErr);
712 return rc;
715 static void tclFunction(sqlite3_context *pCtx, int nArg, sqlite3_value **apArg){
716 TestFindFunction *p = (TestFindFunction*)sqlite3_user_data(pCtx);
717 Tcl_Interp *interp = p->pTab->interp;
718 Tcl_Obj *pScript = 0;
719 Tcl_Obj *pRet = 0;
720 int ii;
722 pScript = Tcl_DuplicateObj(p->pTab->pCmd);
723 Tcl_IncrRefCount(pScript);
724 Tcl_ListObjAppendElement(interp, pScript, Tcl_NewStringObj("function", -1));
725 Tcl_ListObjAppendElement(interp, pScript, Tcl_NewStringObj(p->zName, -1));
727 for(ii=0; ii<nArg; ii++){
728 const char *zArg = (const char*)sqlite3_value_text(apArg[ii]);
729 Tcl_ListObjAppendElement(interp, pScript,
730 (zArg ? Tcl_NewStringObj(zArg, -1) : Tcl_NewObj())
733 Tcl_EvalObjEx(interp, pScript, TCL_EVAL_GLOBAL);
734 Tcl_DecrRefCount(pScript);
736 pRet = Tcl_GetObjResult(interp);
737 sqlite3_result_text(pCtx, Tcl_GetString(pRet), -1, SQLITE_TRANSIENT);
740 static int tclFindFunction(
741 sqlite3_vtab *tab,
742 int nArg,
743 const char *zName,
744 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT */
745 void **ppArg /* OUT */
747 int iRet = 0;
748 tcl_vtab *pTab = (tcl_vtab*)tab;
749 Tcl_Interp *interp = pTab->interp;
750 Tcl_Obj *pScript = 0;
751 int rc = SQLITE_OK;
753 pScript = Tcl_DuplicateObj(pTab->pCmd);
754 Tcl_IncrRefCount(pScript);
755 Tcl_ListObjAppendElement(
756 interp, pScript, Tcl_NewStringObj("xFindFunction", -1)
758 Tcl_ListObjAppendElement(interp, pScript, Tcl_NewIntObj(nArg));
759 Tcl_ListObjAppendElement(interp, pScript, Tcl_NewStringObj(zName, -1));
760 rc = Tcl_EvalObjEx(interp, pScript, TCL_EVAL_GLOBAL);
761 Tcl_DecrRefCount(pScript);
763 if( rc==SQLITE_OK ){
764 Tcl_Obj *pObj = Tcl_GetObjResult(interp);
766 if( Tcl_GetIntFromObj(interp, pObj, &iRet) ){
767 rc = SQLITE_ERROR;
768 }else if( iRet>0 ){
769 sqlite3_int64 nName = strlen(zName);
770 sqlite3_int64 nByte = nName + 1 + sizeof(TestFindFunction);
771 TestFindFunction *pNew = 0;
773 pNew = (TestFindFunction*)sqlite3_malloc64(nByte);
774 if( pNew==0 ){
775 iRet = 0;
776 }else{
777 memset(pNew, 0, nByte);
778 pNew->zName = (const char*)&pNew[1];
779 memcpy((char*)pNew->zName, zName, nName);
780 pNew->pTab = pTab;
781 pNew->pNext = pTab->pFindFunctionList;
782 pTab->pFindFunctionList = pNew;
783 *ppArg = (void*)pNew;
784 *pxFunc = tclFunction;
789 return iRet;
793 ** A virtual table module that provides read-only access to a
794 ** Tcl global variable namespace.
796 static sqlite3_module tclModule = {
797 0, /* iVersion */
798 tclConnect,
799 tclConnect,
800 tclBestIndex,
801 tclDisconnect,
802 tclDisconnect,
803 tclOpen, /* xOpen - open a cursor */
804 tclClose, /* xClose - close a cursor */
805 tclFilter, /* xFilter - configure scan constraints */
806 tclNext, /* xNext - advance a cursor */
807 tclEof, /* xEof - check for end of scan */
808 tclColumn, /* xColumn - read data */
809 tclRowid, /* xRowid - read data */
810 0, /* xUpdate */
811 0, /* xBegin */
812 0, /* xSync */
813 0, /* xCommit */
814 0, /* xRollback */
815 tclFindFunction, /* xFindFunction */
816 0, /* xRename */
817 0, /* xSavepoint */
818 0, /* xRelease */
819 0, /* xRollbackTo */
820 0, /* xShadowName */
821 0 /* xIntegrity */
825 ** Decode a pointer to an sqlite3 object.
827 extern int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb);
830 ** Register the echo virtual table module.
832 static int SQLITE_TCLAPI register_tcl_module(
833 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
834 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
835 int objc, /* Number of arguments */
836 Tcl_Obj *CONST objv[] /* Command arguments */
838 sqlite3 *db;
839 if( objc!=2 ){
840 Tcl_WrongNumArgs(interp, 1, objv, "DB");
841 return TCL_ERROR;
843 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
844 #ifndef SQLITE_OMIT_VIRTUALTABLE
845 sqlite3_create_module(db, "tcl", &tclModule, (void *)interp);
846 #endif
847 return TCL_OK;
850 #endif
854 ** Register commands with the TCL interpreter.
856 int Sqlitetesttcl_Init(Tcl_Interp *interp){
857 #ifndef SQLITE_OMIT_VIRTUALTABLE
858 static struct {
859 char *zName;
860 Tcl_ObjCmdProc *xProc;
861 void *clientData;
862 } aObjCmd[] = {
863 { "register_tcl_module", register_tcl_module, 0 },
865 int i;
866 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
867 Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
868 aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
870 #endif
871 return TCL_OK;