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 / test_bestindex.c

981 lines · 29961 bytes Raw Blame History
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.
10**
11*************************************************************************
12** Code for testing the virtual table xBestIndex method and the query
13** planner.
14*/
15
16
17/*
18** INSTRUCTIONS
19**
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.
23**
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:
27**
28** CREATE VIRTUAL TABLE x1 USING tcl(tcl_command);
29**
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:
34**
35** tcl_command xConnect
36**
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.
39**
40** When the xBestIndex() method is called by SQLite, the Tcl command is
41** invoked as:
42**
43** tcl_command xBestIndex CONSTRAINTS ORDERBY MASK
44**
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:
49**
50** CREATE TABLE x1(a, b, c)
51**
52** and the query is:
53**
54** SELECT * FROM x1 WHERE a=? AND c<? ORDER BY b, c;
55**
56** then the Tcl command is:
57**
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
62**
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)
74**
75** Refer to code below for further details.
76**
77** When SQLite calls the xFilter() method, this module invokes the following
78** Tcl script:
79**
80** tcl_command xFilter IDXNUM IDXSTR ARGLIST
81**
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.
85**
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.
92*/
93
94
95#include "sqliteInt.h"
96#include "tclsqlite.h"
97
98#ifndef SQLITE_OMIT_VIRTUALTABLE
99
100
101typedef struct tcl_vtab tcl_vtab;
102typedef struct tcl_cursor tcl_cursor;
103typedef struct TestFindFunction TestFindFunction;
104typedef struct TestVtabContext TestVtabContext;
105
106/*
107** A fs virtual-table object
108*/
109struct tcl_vtab {
110 sqlite3_vtab base;
111 Tcl_Interp *interp;
112 Tcl_Obj *pCmd;
113 TestFindFunction *pFindFunctionList;
114 sqlite3 *db;
115};
116
117/* A tcl cursor object */
118struct tcl_cursor {
119 sqlite3_vtab_cursor base;
120 sqlite3_stmt *pStmt; /* Read data from here */
121};
122
123struct TestFindFunction {
124 tcl_vtab *pTab;
125 const char *zName;
126 TestFindFunction *pNext;
127};
128
129struct TestVtabContext {
130 Tcl_Interp *interp;
131 Tcl_Obj *pDefault;
132};
133
134/*
135** Dequote string z in place.
136*/
137static void tclDequote(char *z){
138 char q = z[0];
139
140 /* Set stack variable q to the close-quote character */
141 if( q=='[' || q=='\'' || q=='"' || q=='`' ){
142 int iIn = 1;
143 int iOut = 0;
144 if( q=='[' ) q = ']';
145
146 while( ALWAYS(z[iIn]) ){
147 if( z[iIn]==q ){
148 if( z[iIn+1]!=q ){
149 /* Character iIn was the close quote. */
150 iIn++;
151 break;
152 }else{
153 /* Character iIn and iIn+1 form an escaped quote character. Skip
154 ** the input cursor past both and copy a single quote character
155 ** to the output buffer. */
156 iIn += 2;
157 z[iOut++] = q;
158 }
159 }else{
160 z[iOut++] = z[iIn++];
161 }
162 }
163
164 z[iOut] = '\0';
165 }
166}
167
168/*
169** This function is the implementation of both the xConnect and xCreate
170** methods of the fs virtual table.
171**
172** The argv[] array contains the following:
173**
174** argv[0] -> module name ("fs")
175** argv[1] -> database name
176** argv[2] -> table name
177** argv[...] -> other module argument fields.
178*/
179static int tclConnect(
180 sqlite3 *db,
181 void *pAux,
182 int argc, const char *const*argv,
183 sqlite3_vtab **ppVtab,
184 char **pzErr
185){
186 TestVtabContext *pCtx = (TestVtabContext*)pAux;
187 Tcl_Interp *interp = pCtx->interp;
188 tcl_vtab *pTab = 0;
189 char *zCmd = 0;
190 Tcl_Obj *pScript = 0;
191 int rc = SQLITE_OK;
192
193 if( argc!=4 && (argc!=3 || pCtx->pDefault==0) ){
194 *pzErr = sqlite3_mprintf("wrong number of arguments");
195 return SQLITE_ERROR;
196 }
197
198 if( argc==4 ){
199 zCmd = sqlite3_malloc64(strlen(argv[3])+1);
200 }
201 pTab = (tcl_vtab*)sqlite3_malloc64(sizeof(tcl_vtab));
202 if( (zCmd || argc==3) && pTab ){
203 memset(pTab, 0, sizeof(tcl_vtab));
204
205 if( zCmd ){
206 memcpy(zCmd, argv[3], strlen(argv[3])+1);
207 tclDequote(zCmd);
208 pTab->pCmd = Tcl_NewStringObj(zCmd, -1);
209 }else{
210 pTab->pCmd = Tcl_DuplicateObj(pCtx->pDefault);
211 }
212
213 pTab->interp = interp;
214 pTab->db = db;
215 Tcl_IncrRefCount(pTab->pCmd);
216
217 pScript = Tcl_DuplicateObj(pTab->pCmd);
218 Tcl_IncrRefCount(pScript);
219 Tcl_ListObjAppendElement(interp, pScript, Tcl_NewStringObj("xConnect", -1));
220
221 rc = Tcl_EvalObjEx(interp, pScript, TCL_EVAL_GLOBAL);
222 if( rc!=TCL_OK ){
223 *pzErr = sqlite3_mprintf("%s", Tcl_GetStringResult(interp));
224 if( sqlite3_stricmp(*pzErr, "database schema has changed")==0 ){
225 rc = SQLITE_SCHEMA;
226 }else{
227 rc = SQLITE_ERROR;
228 }
229 }else{
230 rc = sqlite3_declare_vtab(db, Tcl_GetStringResult(interp));
231 if( rc!=SQLITE_OK ){
232 *pzErr = sqlite3_mprintf("declare_vtab: %s", sqlite3_errmsg(db));
233 }
234 }
235
236 if( rc!=SQLITE_OK ){
237 sqlite3_free(pTab);
238 pTab = 0;
239 }
240 }else{
241 rc = SQLITE_NOMEM;
242 }
243
244 sqlite3_free(zCmd);
245 *ppVtab = pTab ? &pTab->base : 0;
246 return rc;
247}
248
249/* The xDisconnect and xDestroy methods are also the same */
250static int tclDisconnect(sqlite3_vtab *pVtab){
251 tcl_vtab *pTab = (tcl_vtab*)pVtab;
252 while( pTab->pFindFunctionList ){
253 TestFindFunction *p = pTab->pFindFunctionList;
254 pTab->pFindFunctionList = p->pNext;
255 sqlite3_free(p);
256 }
257 Tcl_DecrRefCount(pTab->pCmd);
258 sqlite3_free(pTab);
259 return SQLITE_OK;
260}
261
262/*
263** Open a new tcl cursor.
264*/
265static int tclOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
266 tcl_cursor *pCur;
267 pCur = sqlite3_malloc(sizeof(tcl_cursor));
268 if( pCur==0 ) return SQLITE_NOMEM;
269 memset(pCur, 0, sizeof(tcl_cursor));
270 *ppCursor = &pCur->base;
271 return SQLITE_OK;
272}
273
274/*
275** Close a tcl cursor.
276*/
277static int tclClose(sqlite3_vtab_cursor *cur){
278 tcl_cursor *pCur = (tcl_cursor *)cur;
279 if( pCur ){
280 sqlite3_finalize(pCur->pStmt);
281 sqlite3_free(pCur);
282 }
283 return SQLITE_OK;
284}
285
286static int tclNext(sqlite3_vtab_cursor *pVtabCursor){
287 tcl_cursor *pCsr = (tcl_cursor*)pVtabCursor;
288 if( pCsr->pStmt ){
289 tcl_vtab *pTab = (tcl_vtab*)(pVtabCursor->pVtab);
290 int rc = sqlite3_step(pCsr->pStmt);
291 if( rc!=SQLITE_ROW ){
292 const char *zErr;
293 rc = sqlite3_finalize(pCsr->pStmt);
294 pCsr->pStmt = 0;
295 if( rc!=SQLITE_OK ){
296 zErr = sqlite3_errmsg(pTab->db);
297 pTab->base.zErrMsg = sqlite3_mprintf("%s", zErr);
298 }
299 }
300 }
301 return SQLITE_OK;
302}
303
304static int tclFilter(
305 sqlite3_vtab_cursor *pVtabCursor,
306 int idxNum, const char *idxStr,
307 int argc, sqlite3_value **argv
308){
309 tcl_cursor *pCsr = (tcl_cursor*)pVtabCursor;
310 tcl_vtab *pTab = (tcl_vtab*)(pVtabCursor->pVtab);
311 Tcl_Interp *interp = pTab->interp;
312 Tcl_Obj *pScript;
313 Tcl_Obj *pArg;
314 int ii;
315 int rc;
316
317 pScript = Tcl_DuplicateObj(pTab->pCmd);
318 Tcl_IncrRefCount(pScript);
319 Tcl_ListObjAppendElement(interp, pScript, Tcl_NewStringObj("xFilter", -1));
320 Tcl_ListObjAppendElement(interp, pScript, Tcl_NewIntObj(idxNum));
321 Tcl_ListObjAppendElement(
322 interp, pScript, Tcl_NewStringObj(idxStr ? idxStr : "", -1)
323 );
324
325 pArg = Tcl_NewObj();
326 Tcl_IncrRefCount(pArg);
327 for(ii=0; ii<argc; ii++){
328 const char *zVal = (const char*)sqlite3_value_text(argv[ii]);
329 Tcl_Obj *pVal;
330 if( zVal==0 ){
331 sqlite3_value *pMem;
332 pVal = Tcl_NewObj();
333 for(rc=sqlite3_vtab_in_first(argv[ii], &pMem);
334 rc==SQLITE_OK && pMem;
335 rc=sqlite3_vtab_in_next(argv[ii], &pMem)
336 ){
337 Tcl_Obj *pVal2 = 0;
338 zVal = (const char*)sqlite3_value_text(pMem);
339 if( zVal ){
340 pVal2 = Tcl_NewStringObj(zVal, -1);
341 }else{
342 pVal2 = Tcl_NewObj();
343 }
344 Tcl_ListObjAppendElement(interp, pVal, pVal2);
345 }
346 }else{
347 pVal = Tcl_NewStringObj(zVal, -1);
348 }
349 Tcl_ListObjAppendElement(interp, pArg, pVal);
350 }
351 Tcl_ListObjAppendElement(interp, pScript, pArg);
352 Tcl_DecrRefCount(pArg);
353
354 rc = Tcl_EvalObjEx(interp, pScript, TCL_EVAL_GLOBAL);
355 if( rc!=TCL_OK ){
356 const char *zErr = Tcl_GetStringResult(interp);
357 rc = SQLITE_ERROR;
358 pTab->base.zErrMsg = sqlite3_mprintf("%s", zErr);
359 }else{
360 /* Analyze the scripts return value. The return value should be a tcl
361 ** list object with an even number of elements. The first element of each
362 ** pair must be one of:
363 **
364 ** "sql" (SQL statement to return data)
365 */
366 Tcl_Obj *pRes = Tcl_GetObjResult(interp);
367 Tcl_Obj **apElem = 0;
368 Tcl_Size nElem;
369 rc = Tcl_ListObjGetElements(interp, pRes, &nElem, &apElem);
370 if( rc!=TCL_OK ){
371 const char *zErr = Tcl_GetStringResult(interp);
372 rc = SQLITE_ERROR;
373 pTab->base.zErrMsg = sqlite3_mprintf("%s", zErr);
374 }else{
375 for(ii=0; rc==SQLITE_OK && ii<(int)nElem; ii+=2){
376 const char *zCmd = Tcl_GetString(apElem[ii]);
377 Tcl_Obj *p = apElem[ii+1];
378 if( sqlite3_stricmp("sql", zCmd)==0 ){
379 const char *zSql = Tcl_GetString(p);
380 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pStmt, 0);
381 if( rc!=SQLITE_OK ){
382 const char *zErr = sqlite3_errmsg(pTab->db);
383 pTab->base.zErrMsg = sqlite3_mprintf("unexpected: %s", zErr);
384 }
385 }else{
386 rc = SQLITE_ERROR;
387 pTab->base.zErrMsg = sqlite3_mprintf("unexpected: %s", zCmd);
388 }
389 }
390 }
391 }
392
393 if( rc==SQLITE_OK ){
394 rc = tclNext(pVtabCursor);
395 }
396 return rc;
397}
398
399static int tclColumn(
400 sqlite3_vtab_cursor *pVtabCursor,
401 sqlite3_context *ctx,
402 int i
403){
404 tcl_cursor *pCsr = (tcl_cursor*)pVtabCursor;
405 sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pStmt, i+1));
406 return SQLITE_OK;
407}
408
409static int tclRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *pRowid){
410 tcl_cursor *pCsr = (tcl_cursor*)pVtabCursor;
411 *pRowid = sqlite3_column_int64(pCsr->pStmt, 0);
412 return SQLITE_OK;
413}
414
415static int tclEof(sqlite3_vtab_cursor *pVtabCursor){
416 tcl_cursor *pCsr = (tcl_cursor*)pVtabCursor;
417 return (pCsr->pStmt==0);
418}
419
420static void testBestIndexObjConstraints(
421 Tcl_Interp *interp,
422 sqlite3_index_info *pIdxInfo
423){
424 int ii;
425 Tcl_Obj *pRes = Tcl_NewObj();
426 Tcl_IncrRefCount(pRes);
427 for(ii=0; ii<pIdxInfo->nConstraint; ii++){
428 struct sqlite3_index_constraint const *pCons = &pIdxInfo->aConstraint[ii];
429 Tcl_Obj *pElem = Tcl_NewObj();
430 const char *zOp = 0;
431
432 Tcl_IncrRefCount(pElem);
433
434 switch( pCons->op ){
435 case SQLITE_INDEX_CONSTRAINT_EQ:
436 zOp = "eq"; break;
437 case SQLITE_INDEX_CONSTRAINT_GT:
438 zOp = "gt"; break;
439 case SQLITE_INDEX_CONSTRAINT_LE:
440 zOp = "le"; break;
441 case SQLITE_INDEX_CONSTRAINT_LT:
442 zOp = "lt"; break;
443 case SQLITE_INDEX_CONSTRAINT_GE:
444 zOp = "ge"; break;
445 case SQLITE_INDEX_CONSTRAINT_MATCH:
446 zOp = "match"; break;
447 case SQLITE_INDEX_CONSTRAINT_LIKE:
448 zOp = "like"; break;
449 case SQLITE_INDEX_CONSTRAINT_GLOB:
450 zOp = "glob"; break;
451 case SQLITE_INDEX_CONSTRAINT_REGEXP:
452 zOp = "regexp"; break;
453 case SQLITE_INDEX_CONSTRAINT_NE:
454 zOp = "ne"; break;
455 case SQLITE_INDEX_CONSTRAINT_ISNOT:
456 zOp = "isnot"; break;
457 case SQLITE_INDEX_CONSTRAINT_ISNOTNULL:
458 zOp = "isnotnull"; break;
459 case SQLITE_INDEX_CONSTRAINT_ISNULL:
460 zOp = "isnull"; break;
461 case SQLITE_INDEX_CONSTRAINT_IS:
462 zOp = "is"; break;
463 case SQLITE_INDEX_CONSTRAINT_LIMIT:
464 zOp = "limit"; break;
465 case SQLITE_INDEX_CONSTRAINT_OFFSET:
466 zOp = "offset"; break;
467 }
468
469 Tcl_ListObjAppendElement(0, pElem, Tcl_NewStringObj("op", -1));
470 if( zOp ){
471 Tcl_ListObjAppendElement(0, pElem, Tcl_NewStringObj(zOp, -1));
472 }else{
473 Tcl_ListObjAppendElement(0, pElem, Tcl_NewIntObj(pCons->op));
474 }
475 Tcl_ListObjAppendElement(0, pElem, Tcl_NewStringObj("column", -1));
476 Tcl_ListObjAppendElement(0, pElem, Tcl_NewIntObj(pCons->iColumn));
477 Tcl_ListObjAppendElement(0, pElem, Tcl_NewStringObj("usable", -1));
478 Tcl_ListObjAppendElement(0, pElem, Tcl_NewIntObj(pCons->usable));
479
480 Tcl_ListObjAppendElement(0, pRes, pElem);
481 Tcl_DecrRefCount(pElem);
482 }
483
484 Tcl_SetObjResult(interp, pRes);
485 Tcl_DecrRefCount(pRes);
486}
487
488static void testBestIndexObjOrderby(
489 Tcl_Interp *interp,
490 sqlite3_index_info *pIdxInfo
491){
492 int ii;
493 Tcl_Obj *pRes = Tcl_NewObj();
494 Tcl_IncrRefCount(pRes);
495 for(ii=0; ii<pIdxInfo->nOrderBy; ii++){
496 struct sqlite3_index_orderby const *pOrder = &pIdxInfo->aOrderBy[ii];
497 Tcl_Obj *pElem = Tcl_NewObj();
498 Tcl_IncrRefCount(pElem);
499
500 Tcl_ListObjAppendElement(0, pElem, Tcl_NewStringObj("column", -1));
501 Tcl_ListObjAppendElement(0, pElem, Tcl_NewIntObj(pOrder->iColumn));
502 Tcl_ListObjAppendElement(0, pElem, Tcl_NewStringObj("desc", -1));
503 Tcl_ListObjAppendElement(0, pElem, Tcl_NewIntObj(pOrder->desc));
504
505 Tcl_ListObjAppendElement(0, pRes, pElem);
506 Tcl_DecrRefCount(pElem);
507 }
508
509 Tcl_SetObjResult(interp, pRes);
510 Tcl_DecrRefCount(pRes);
511}
512
513/*
514** Implementation of the handle passed to each xBestIndex callback. This
515** object features the following sub-commands:
516**
517** $hdl constraints
518** $hdl orderby
519** $hdl mask
520**
521** $hdl distinct
522** Return the result (an integer) of calling sqlite3_vtab_distinct()
523** on the index-info structure.
524**
525** $hdl in IDX BOOLEAN
526** Wrapper around sqlite3_vtab_in(). Returns an integer.
527**
528** $hdl rhs_value IDX ?DEFAULT?
529** Wrapper around sqlite3_vtab_rhs_value().
530*/
531static int SQLITE_TCLAPI testBestIndexObj(
532 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
533 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
534 int objc, /* Number of arguments */
535 Tcl_Obj *CONST objv[] /* Command arguments */
536){
537 const char *azSub[] = {
538 "constraints", /* 0 */
539 "orderby", /* 1 */
540 "mask", /* 2 */
541 "distinct", /* 3 */
542 "in", /* 4 */
543 "rhs_value", /* 5 */
544 "collation", /* 6 */
545 0
546 };
547 int ii;
548 sqlite3_index_info *pIdxInfo = (sqlite3_index_info*)clientData;
549
550 if( objc<2 ){
551 Tcl_WrongNumArgs(interp, 1, objv, "SUB-COMMAND");
552 return TCL_ERROR;
553 }
554 if( Tcl_GetIndexFromObj(interp, objv[1], azSub, "sub-command", 0, &ii) ){
555 return TCL_ERROR;
556 }
557
558 if( ii<4 && objc!=2 ){
559 Tcl_WrongNumArgs(interp, 2, objv, "");
560 return TCL_ERROR;
561 }
562 if( ii==4 && objc!=4 ){
563 Tcl_WrongNumArgs(interp, 2, objv, "INDEX BOOLEAN");
564 return TCL_ERROR;
565 }
566 if( ii==5 && objc!=3 && objc!=4 ){
567 Tcl_WrongNumArgs(interp, 2, objv, "INDEX ?DEFAULT?");
568 return TCL_ERROR;
569 }
570
571 switch( ii ){
572 case 0: assert( sqlite3_stricmp(azSub[ii], "constraints")==0 );
573 testBestIndexObjConstraints(interp, pIdxInfo);
574 break;
575
576 case 1: assert( sqlite3_stricmp(azSub[ii], "orderby")==0 );
577 testBestIndexObjOrderby(interp, pIdxInfo);
578 break;
579
580 case 2: assert( sqlite3_stricmp(azSub[ii], "mask")==0 );
581 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(pIdxInfo->colUsed));
582 break;
583
584 case 3: assert( sqlite3_stricmp(azSub[ii], "distinct")==0 ); {
585 int bDistinct = sqlite3_vtab_distinct(pIdxInfo);
586 Tcl_SetObjResult(interp, Tcl_NewIntObj(bDistinct));
587 break;
588 }
589
590 case 4: assert( sqlite3_stricmp(azSub[ii], "in")==0 ); {
591 int iCons;
592 int bHandle;
593 if( Tcl_GetIntFromObj(interp, objv[2], &iCons)
594 || Tcl_GetBooleanFromObj(interp, objv[3], &bHandle)
595 ){
596 return TCL_ERROR;
597 }
598 Tcl_SetObjResult(interp,
599 Tcl_NewIntObj(sqlite3_vtab_in(pIdxInfo, iCons, bHandle))
600 );
601 break;
602 }
603
604 case 5: assert( sqlite3_stricmp(azSub[ii], "rhs_value")==0 ); {
605 int iCons = 0;
606 int rc;
607 sqlite3_value *pVal = 0;
608 const char *zVal = "";
609 if( Tcl_GetIntFromObj(interp, objv[2], &iCons) ){
610 return TCL_ERROR;
611 }
612 rc = sqlite3_vtab_rhs_value(pIdxInfo, iCons, &pVal);
613 if( rc!=SQLITE_OK && rc!=SQLITE_NOTFOUND ){
614 Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE);
615 return TCL_ERROR;
616 }
617 if( pVal ){
618 zVal = (const char*)sqlite3_value_text(pVal);
619 }else if( objc==4 ){
620 zVal = Tcl_GetString(objv[3]);
621 }
622 Tcl_SetObjResult(interp, Tcl_NewStringObj(zVal, -1));
623 break;
624 }
625
626 case 6: assert( sqlite3_stricmp(azSub[ii], "collation")==0 ); {
627 int iCons = 0;
628 const char *zColl = "";
629 if( Tcl_GetIntFromObj(interp, objv[2], &iCons) ){
630 return TCL_ERROR;
631 }
632 zColl = sqlite3_vtab_collation(pIdxInfo, iCons);
633 Tcl_SetObjResult(interp, Tcl_NewStringObj(zColl, -1));
634 break;
635 }
636 }
637
638 return TCL_OK;
639}
640
641static int tclBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
642 tcl_vtab *pTab = (tcl_vtab*)tab;
643 Tcl_Interp *interp = pTab->interp;
644 int rc = SQLITE_OK;
645
646 static int iNext = 43;
647 char zHdl[24];
648 Tcl_Obj *pScript;
649
650 pScript = Tcl_DuplicateObj(pTab->pCmd);
651 Tcl_IncrRefCount(pScript);
652 Tcl_ListObjAppendElement(interp, pScript, Tcl_NewStringObj("xBestIndex", -1));
653
654 sqlite3_snprintf(sizeof(zHdl), zHdl, "bestindex%d", iNext++);
655 Tcl_CreateObjCommand(interp, zHdl, testBestIndexObj, pIdxInfo, 0);
656 Tcl_ListObjAppendElement(interp, pScript, Tcl_NewStringObj(zHdl, -1));
657 rc = Tcl_EvalObjEx(interp, pScript, TCL_EVAL_GLOBAL);
658 Tcl_DeleteCommand(interp, zHdl);
659 Tcl_DecrRefCount(pScript);
660
661 if( rc!=TCL_OK ){
662 const char *zErr = Tcl_GetStringResult(interp);
663 rc = SQLITE_ERROR;
664 pTab->base.zErrMsg = sqlite3_mprintf("%s", zErr);
665 }else{
666 /* Analyze the scripts return value. The return value should be a tcl
667 ** list object with an even number of elements. The first element of each
668 ** pair must be one of:
669 **
670 ** "orderby" (value of orderByConsumed flag)
671 ** "cost" (value of estimatedCost field)
672 ** "rows" (value of estimatedRows field)
673 ** "use" (index of used constraint in aConstraint[])
674 ** "idxnum" (value of idxNum field)
675 ** "idxstr" (value of idxStr field)
676 ** "omit" (index of omitted constraint in aConstraint[])
677 */
678 Tcl_Obj *pRes = Tcl_GetObjResult(interp);
679 Tcl_Obj **apElem = 0;
680 Tcl_Size nElem;
681 rc = Tcl_ListObjGetElements(interp, pRes, &nElem, &apElem);
682 if( rc!=TCL_OK ){
683 const char *zErr = Tcl_GetStringResult(interp);
684 rc = SQLITE_ERROR;
685 pTab->base.zErrMsg = sqlite3_mprintf("%s", zErr);
686 }else{
687 int ii;
688 int iArgv = 1;
689 for(ii=0; rc==SQLITE_OK && ii<(int)nElem; ii+=2){
690 const char *zCmd = Tcl_GetString(apElem[ii]);
691 Tcl_Obj *p = apElem[ii+1];
692 if( sqlite3_stricmp("cost", zCmd)==0 ){
693 rc = Tcl_GetDoubleFromObj(interp, p, &pIdxInfo->estimatedCost);
694 }else
695 if( sqlite3_stricmp("orderby", zCmd)==0 ){
696 rc = Tcl_GetIntFromObj(interp, p, &pIdxInfo->orderByConsumed);
697 }else
698 if( sqlite3_stricmp("idxnum", zCmd)==0 ){
699 rc = Tcl_GetIntFromObj(interp, p, &pIdxInfo->idxNum);
700 }else
701 if( sqlite3_stricmp("idxstr", zCmd)==0 ){
702 sqlite3_free(pIdxInfo->idxStr);
703 pIdxInfo->idxStr = sqlite3_mprintf("%s", Tcl_GetString(p));
704 pIdxInfo->needToFreeIdxStr = 1;
705 }else
706 if( sqlite3_stricmp("rows", zCmd)==0 ){
707 Tcl_WideInt x = 0;
708 rc = Tcl_GetWideIntFromObj(interp, p, &x);
709 pIdxInfo->estimatedRows = (tRowcnt)x;
710 }else
711 if( sqlite3_stricmp("use", zCmd)==0
712 || sqlite3_stricmp("omit", zCmd)==0
713 ){
714 int iCons;
715 rc = Tcl_GetIntFromObj(interp, p, &iCons);
716 if( rc==SQLITE_OK ){
717 if( iCons<0 || iCons>=pIdxInfo->nConstraint ){
718 rc = SQLITE_ERROR;
719 pTab->base.zErrMsg = sqlite3_mprintf("unexpected: %d", iCons);
720 }else{
721 int bOmit = (zCmd[0]=='o' || zCmd[0]=='O');
722 pIdxInfo->aConstraintUsage[iCons].argvIndex = iArgv++;
723 pIdxInfo->aConstraintUsage[iCons].omit = bOmit;
724 }
725 }
726 }else
727 if( sqlite3_stricmp("constraint", zCmd)==0 ){
728 rc = SQLITE_CONSTRAINT;
729 pTab->base.zErrMsg = sqlite3_mprintf("%s", Tcl_GetString(p));
730 }else{
731 rc = SQLITE_ERROR;
732 pTab->base.zErrMsg = sqlite3_mprintf("unexpected: %s", zCmd);
733 }
734 if( rc!=SQLITE_OK && pTab->base.zErrMsg==0 ){
735 const char *zErr = Tcl_GetStringResult(interp);
736 pTab->base.zErrMsg = sqlite3_mprintf("%s", zErr);
737 }
738 }
739 }
740 }
741
742 return rc;
743}
744
745static void tclFunction(sqlite3_context *pCtx, int nArg, sqlite3_value **apArg){
746 TestFindFunction *p = (TestFindFunction*)sqlite3_user_data(pCtx);
747 Tcl_Interp *interp = p->pTab->interp;
748 Tcl_Obj *pScript = 0;
749 Tcl_Obj *pRet = 0;
750 int ii;
751
752 pScript = Tcl_DuplicateObj(p->pTab->pCmd);
753 Tcl_IncrRefCount(pScript);
754 Tcl_ListObjAppendElement(interp, pScript, Tcl_NewStringObj("function", -1));
755 Tcl_ListObjAppendElement(interp, pScript, Tcl_NewStringObj(p->zName, -1));
756
757 for(ii=0; ii<nArg; ii++){
758 const char *zArg = (const char*)sqlite3_value_text(apArg[ii]);
759 Tcl_ListObjAppendElement(interp, pScript,
760 (zArg ? Tcl_NewStringObj(zArg, -1) : Tcl_NewObj())
761 );
762 }
763 Tcl_EvalObjEx(interp, pScript, TCL_EVAL_GLOBAL);
764 Tcl_DecrRefCount(pScript);
765
766 pRet = Tcl_GetObjResult(interp);
767 sqlite3_result_text(pCtx, Tcl_GetString(pRet), -1, SQLITE_TRANSIENT);
768}
769
770static int tclFindFunction(
771 sqlite3_vtab *tab,
772 int nArg,
773 const char *zName,
774 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT */
775 void **ppArg /* OUT */
776){
777 int iRet = 0;
778 tcl_vtab *pTab = (tcl_vtab*)tab;
779 Tcl_Interp *interp = pTab->interp;
780 Tcl_Obj *pScript = 0;
781 int rc = SQLITE_OK;
782
783 pScript = Tcl_DuplicateObj(pTab->pCmd);
784 Tcl_IncrRefCount(pScript);
785 Tcl_ListObjAppendElement(
786 interp, pScript, Tcl_NewStringObj("xFindFunction", -1)
787 );
788 Tcl_ListObjAppendElement(interp, pScript, Tcl_NewIntObj(nArg));
789 Tcl_ListObjAppendElement(interp, pScript, Tcl_NewStringObj(zName, -1));
790 rc = Tcl_EvalObjEx(interp, pScript, TCL_EVAL_GLOBAL);
791 Tcl_DecrRefCount(pScript);
792
793 if( rc==SQLITE_OK ){
794 Tcl_Obj *pObj = Tcl_GetObjResult(interp);
795
796 if( Tcl_GetIntFromObj(interp, pObj, &iRet) ){
797 rc = SQLITE_ERROR;
798 }else if( iRet>0 ){
799 sqlite3_int64 nName = strlen(zName);
800 sqlite3_int64 nByte = nName + 1 + sizeof(TestFindFunction);
801 TestFindFunction *pNew = 0;
802
803 pNew = (TestFindFunction*)sqlite3_malloc64(nByte);
804 if( pNew==0 ){
805 iRet = 0;
806 }else{
807 memset(pNew, 0, nByte);
808 pNew->zName = (const char*)&pNew[1];
809 memcpy((char*)pNew->zName, zName, nName);
810 pNew->pTab = pTab;
811 pNew->pNext = pTab->pFindFunctionList;
812 pTab->pFindFunctionList = pNew;
813 *ppArg = (void*)pNew;
814 *pxFunc = tclFunction;
815 }
816 }
817 }
818
819 return iRet;
820}
821
822static int tclUpdate(
823 sqlite3_vtab *tab,
824 int nArg,
825 sqlite3_value **apVal,
826 sqlite3_int64 *piRowid
827){
828 tcl_vtab *pTab = (tcl_vtab*)tab;
829 Tcl_Interp *interp = pTab->interp;
830 Tcl_Obj *pEval = Tcl_DuplicateObj(pTab->pCmd);
831 int rc = TCL_OK;
832
833 Tcl_IncrRefCount(pEval);
834 Tcl_ListObjAppendElement(interp, pEval, Tcl_NewStringObj("xUpdate",-1));
835
836 rc = Tcl_EvalObjEx(interp, pEval, TCL_EVAL_GLOBAL);
837 Tcl_DecrRefCount(pEval);
838
839 if( rc==TCL_OK ){
840 Tcl_Obj *pRes = Tcl_GetObjResult(interp);
841 Tcl_WideInt v;
842 rc = Tcl_GetWideIntFromObj(interp, pRes, &v);
843 *piRowid = (sqlite3_int64)v;
844 }
845
846 if( rc!=TCL_OK ){
847 tab->zErrMsg = sqlite3_mprintf("%s", Tcl_GetStringResult(pTab->interp));
848 return rc;
849 }
850
851 return SQLITE_OK;
852}
853
854/*
855** A virtual table module that provides read-only access to a
856** Tcl global variable namespace.
857*/
858static sqlite3_module tclModule = {
859 0, /* iVersion */
860 tclConnect,
861 tclConnect,
862 tclBestIndex,
863 tclDisconnect,
864 tclDisconnect,
865 tclOpen, /* xOpen - open a cursor */
866 tclClose, /* xClose - close a cursor */
867 tclFilter, /* xFilter - configure scan constraints */
868 tclNext, /* xNext - advance a cursor */
869 tclEof, /* xEof - check for end of scan */
870 tclColumn, /* xColumn - read data */
871 tclRowid, /* xRowid - read data */
872 0, /* xUpdate */
873 0, /* xBegin */
874 0, /* xSync */
875 0, /* xCommit */
876 0, /* xRollback */
877 tclFindFunction, /* xFindFunction */
878 0, /* xRename */
879 0, /* xSavepoint */
880 0, /* xRelease */
881 0, /* xRollbackTo */
882 0, /* xShadowName */
883 0 /* xIntegrity */
884};
885static sqlite3_module tclModuleUpdate = {
886 0, /* iVersion */
887 tclConnect,
888 tclConnect,
889 tclBestIndex,
890 tclDisconnect,
891 tclDisconnect,
892 tclOpen, /* xOpen - open a cursor */
893 tclClose, /* xClose - close a cursor */
894 tclFilter, /* xFilter - configure scan constraints */
895 tclNext, /* xNext - advance a cursor */
896 tclEof, /* xEof - check for end of scan */
897 tclColumn, /* xColumn - read data */
898 tclRowid, /* xRowid - read data */
899 tclUpdate, /* xUpdate */
900 0, /* xBegin */
901 0, /* xSync */
902 0, /* xCommit */
903 0, /* xRollback */
904 tclFindFunction, /* xFindFunction */
905 0, /* xRename */
906 0, /* xSavepoint */
907 0, /* xRelease */
908 0, /* xRollbackTo */
909 0, /* xShadowName */
910 0 /* xIntegrity */
911};
912
913/*
914** Decode a pointer to an sqlite3 object.
915*/
916extern int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb);
917
918static void delTestVtabCtx(void *p){
919 TestVtabContext *pCtx = (TestVtabContext*)p;
920 if( pCtx->pDefault ){
921 Tcl_DecrRefCount(pCtx->pDefault);
922 }
923 ckfree(pCtx);
924}
925
926/*
927** Register the echo virtual table module.
928*/
929static int SQLITE_TCLAPI register_tcl_module(
930 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
931 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
932 int objc, /* Number of arguments */
933 Tcl_Obj *CONST objv[] /* Command arguments */
934){
935 sqlite3 *db;
936 if( objc!=2 && objc!=3 ){
937 Tcl_WrongNumArgs(interp, 1, objv, "DB ?DEFAULT-CMD?");
938 return TCL_ERROR;
939 }
940 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
941#ifndef SQLITE_OMIT_VIRTUALTABLE
942 {
943 sqlite3_module *pMod = &tclModule;
944 TestVtabContext *pCtx = (TestVtabContext*)ckalloc(sizeof(TestVtabContext));
945 pCtx->interp = interp;
946 pCtx->pDefault = 0;
947 if( objc==3 ){
948 pCtx->pDefault = objv[2];
949 Tcl_IncrRefCount(pCtx->pDefault);
950 }
951
952 if( objc==3 ){ pMod = &tclModuleUpdate; }
953 sqlite3_create_module_v2(db, "tcl", pMod, (void*)pCtx, delTestVtabCtx);
954 }
955#endif
956 return TCL_OK;
957}
958
959#endif
960
961
962/*
963** Register commands with the TCL interpreter.
964*/
965int Sqlitetesttcl_Init(Tcl_Interp *interp){
966#ifndef SQLITE_OMIT_VIRTUALTABLE
967 static struct {
968 char *zName;
969 Tcl_ObjCmdProc *xProc;
970 void *clientData;
971 } aObjCmd[] = {
972 { "register_tcl_module", register_tcl_module, 0 },
973 };
974 int i;
975 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
976 Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
977 aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
978 }
979#endif
980 return TCL_OK;
981}