1 /* 2 ** 2006 June 10 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 interfaces. This code 13 ** is not included in the SQLite library. It is used for automated 14 ** testing of the SQLite library. 15 ** 16 ** $Id: test8.c,v 1.54 2007/08/25 13:37:49 danielk1977 Exp $ 17 */ 18 #include "sqliteInt.h" 19 #include "tcl.h" 20 #include <stdlib.h> 21 #include <string.h> 22 23 #ifndef SQLITE_OMIT_VIRTUALTABLE 24 25 typedef struct echo_vtab echo_vtab; 26 typedef struct echo_cursor echo_cursor; 27 28 /* 29 ** The test module defined in this file uses four global Tcl variables to 30 ** commicate with test-scripts: 31 ** 32 ** $::echo_module 33 ** $::echo_module_sync_fail 34 ** $::echo_module_begin_fail 35 ** $::echo_module_cost 36 ** 37 ** The variable ::echo_module is a list. Each time one of the following 38 ** methods is called, one or more elements are appended to the list. 39 ** This is used for automated testing of virtual table modules. 40 ** 41 ** The ::echo_module_sync_fail variable is set by test scripts and read 42 ** by code in this file. If it is set to the name of a real table in the 43 ** the database, then all xSync operations on echo virtual tables that 44 ** use the named table as a backing store will fail. 45 */ 46 47 /* 48 ** An echo virtual-table object. 49 ** 50 ** echo.vtab.aIndex is an array of booleans. The nth entry is true if 51 ** the nth column of the real table is the left-most column of an index 52 ** (implicit or otherwise). In other words, if SQLite can optimize 53 ** a query like "SELECT * FROM real_table WHERE col = ?". 54 ** 55 ** Member variable aCol[] contains copies of the column names of the real 56 ** table. 57 */ 58 struct echo_vtab { 59 sqlite3_vtab base; 60 Tcl_Interp *interp; /* Tcl interpreter containing debug variables */ 61 sqlite3 *db; /* Database connection */ 62 63 int isPattern; 64 char *zThis; /* Name of the echo table */ 65 char *zTableName; /* Name of the real table */ 66 char *zLogName; /* Name of the log table */ 67 int nCol; /* Number of columns in the real table */ 68 int *aIndex; /* Array of size nCol. True if column has an index */ 69 char **aCol; /* Array of size nCol. Column names */ 70 }; 71 72 /* An echo cursor object */ 73 struct echo_cursor { 74 sqlite3_vtab_cursor base; 75 sqlite3_stmt *pStmt; 76 }; 77 78 /* 79 ** Convert an SQL-style quoted string into a normal string by removing 80 ** the quote characters. The conversion is done in-place. If the 81 ** input does not begin with a quote character, then this routine 82 ** is a no-op. 83 ** 84 ** Examples: 85 ** 86 ** "abc" becomes abc 87 ** 'xyz' becomes xyz 88 ** [pqr] becomes pqr 89 ** `mno` becomes mno 90 */ 91 static void dequoteString(char *z){ 92 int quote; 93 int i, j; 94 if( z==0 ) return; 95 quote = z[0]; 96 switch( quote ){ 97 case '\'': break; 98 case '"': break; 99 case '`': break; /* For MySQL compatibility */ 100 case '[': quote = ']'; break; /* For MS SqlServer compatibility */ 101 default: return; 102 } 103 for(i=1, j=0; z[i]; i++){ 104 if( z[i]==quote ){ 105 if( z[i+1]==quote ){ 106 z[j++] = quote; 107 i++; 108 }else{ 109 z[j++] = 0; 110 break; 111 } 112 }else{ 113 z[j++] = z[i]; 114 } 115 } 116 } 117 118 /* 119 ** Retrieve the column names for the table named zTab via database 120 ** connection db. SQLITE_OK is returned on success, or an sqlite error 121 ** code otherwise. 122 ** 123 ** If successful, the number of columns is written to *pnCol. *paCol is 124 ** set to point at sqlite3_malloc()'d space containing the array of 125 ** nCol column names. The caller is responsible for calling sqlite3_free 126 ** on *paCol. 127 */ 128 static int getColumnNames( 129 sqlite3 *db, 130 const char *zTab, 131 char ***paCol, 132 int *pnCol 133 ){ 134 char **aCol = 0; 135 char *zSql; 136 sqlite3_stmt *pStmt = 0; 137 int rc = SQLITE_OK; 138 int nCol = 0; 139 140 /* Prepare the statement "SELECT * FROM <tbl>". The column names 141 ** of the result set of the compiled SELECT will be the same as 142 ** the column names of table <tbl>. 143 */ 144 zSql = sqlite3_mprintf("SELECT * FROM %Q", zTab); 145 if( !zSql ){ 146 rc = SQLITE_NOMEM; 147 goto out; 148 } 149 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0); 150 sqlite3_free(zSql); 151 152 if( rc==SQLITE_OK ){ 153 int ii; 154 int nBytes; 155 char *zSpace; 156 nCol = sqlite3_column_count(pStmt); 157 158 /* Figure out how much space to allocate for the array of column names 159 ** (including space for the strings themselves). Then allocate it. 160 */ 161 nBytes = sizeof(char *) * nCol; 162 for(ii=0; ii<nCol; ii++){ 163 nBytes += (strlen(sqlite3_column_name(pStmt, ii)) + 1); 164 } 165 aCol = (char **)sqlite3MallocZero(nBytes); 166 if( !aCol ){ 167 rc = SQLITE_NOMEM; 168 goto out; 169 } 170 171 /* Copy the column names into the allocated space and set up the 172 ** pointers in the aCol[] array. 173 */ 174 zSpace = (char *)(&aCol[nCol]); 175 for(ii=0; ii<nCol; ii++){ 176 aCol[ii] = zSpace; 177 zSpace += sprintf(zSpace, "%s", sqlite3_column_name(pStmt, ii)); 178 zSpace++; 179 } 180 assert( (zSpace-nBytes)==(char *)aCol ); 181 } 182 183 *paCol = aCol; 184 *pnCol = nCol; 185 186 out: 187 sqlite3_finalize(pStmt); 188 return rc; 189 } 190 191 /* 192 ** Parameter zTab is the name of a table in database db with nCol 193 ** columns. This function allocates an array of integers nCol in 194 ** size and populates it according to any implicit or explicit 195 ** indices on table zTab. 196 ** 197 ** If successful, SQLITE_OK is returned and *paIndex set to point 198 ** at the allocated array. Otherwise, an error code is returned. 199 ** 200 ** See comments associated with the member variable aIndex above 201 ** "struct echo_vtab" for details of the contents of the array. 202 */ 203 static int getIndexArray( 204 sqlite3 *db, /* Database connection */ 205 const char *zTab, /* Name of table in database db */ 206 int nCol, 207 int **paIndex 208 ){ 209 sqlite3_stmt *pStmt = 0; 210 int *aIndex = 0; 211 int rc; 212 char *zSql; 213 214 /* Allocate space for the index array */ 215 aIndex = (int *)sqlite3MallocZero(sizeof(int) * nCol); 216 if( !aIndex ){ 217 rc = SQLITE_NOMEM; 218 goto get_index_array_out; 219 } 220 221 /* Compile an sqlite pragma to loop through all indices on table zTab */ 222 zSql = sqlite3MPrintf(0, "PRAGMA index_list(%s)", zTab); 223 if( !zSql ){ 224 rc = SQLITE_NOMEM; 225 goto get_index_array_out; 226 } 227 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0); 228 sqlite3_free(zSql); 229 230 /* For each index, figure out the left-most column and set the 231 ** corresponding entry in aIndex[] to 1. 232 */ 233 while( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){ 234 const char *zIdx = (const char *)sqlite3_column_text(pStmt, 1); 235 sqlite3_stmt *pStmt2 = 0; 236 zSql = sqlite3MPrintf(0, "PRAGMA index_info(%s)", zIdx); 237 if( !zSql ){ 238 rc = SQLITE_NOMEM; 239 goto get_index_array_out; 240 } 241 rc = sqlite3_prepare(db, zSql, -1, &pStmt2, 0); 242 sqlite3_free(zSql); 243 if( pStmt2 && sqlite3_step(pStmt2)==SQLITE_ROW ){ 244 int cid = sqlite3_column_int(pStmt2, 1); 245 assert( cid>=0 && cid<nCol ); 246 aIndex[cid] = 1; 247 } 248 if( pStmt2 ){ 249 rc = sqlite3_finalize(pStmt2); 250 } 251 if( rc!=SQLITE_OK ){ 252 goto get_index_array_out; 253 } 254 } 255 256 257 get_index_array_out: 258 if( pStmt ){ 259 int rc2 = sqlite3_finalize(pStmt); 260 if( rc==SQLITE_OK ){ 261 rc = rc2; 262 } 263 } 264 if( rc!=SQLITE_OK ){ 265 sqlite3_free(aIndex); 266 aIndex = 0; 267 } 268 *paIndex = aIndex; 269 return rc; 270 } 271 272 /* 273 ** Global Tcl variable $echo_module is a list. This routine appends 274 ** the string element zArg to that list in interpreter interp. 275 */ 276 static void appendToEchoModule(Tcl_Interp *interp, const char *zArg){ 277 int flags = (TCL_APPEND_VALUE | TCL_LIST_ELEMENT | TCL_GLOBAL_ONLY); 278 Tcl_SetVar(interp, "echo_module", (zArg?zArg:""), flags); 279 } 280 281 /* 282 ** This function is called from within the echo-modules xCreate and 283 ** xConnect methods. The argc and argv arguments are copies of those 284 ** passed to the calling method. This function is responsible for 285 ** calling sqlite3_declare_vtab() to declare the schema of the virtual 286 ** table being created or connected. 287 ** 288 ** If the constructor was passed just one argument, i.e.: 289 ** 290 ** CREATE TABLE t1 AS echo(t2); 291 ** 292 ** Then t2 is assumed to be the name of a *real* database table. The 293 ** schema of the virtual table is declared by passing a copy of the 294 ** CREATE TABLE statement for the real table to sqlite3_declare_vtab(). 295 ** Hence, the virtual table should have exactly the same column names and 296 ** types as the real table. 297 */ 298 static int echoDeclareVtab( 299 echo_vtab *pVtab, 300 sqlite3 *db 301 ){ 302 int rc = SQLITE_OK; 303 304 if( pVtab->zTableName ){ 305 sqlite3_stmt *pStmt = 0; 306 rc = sqlite3_prepare(db, 307 "SELECT sql FROM sqlite_master WHERE type = 'table' AND name = ?", 308 -1, &pStmt, 0); 309 if( rc==SQLITE_OK ){ 310 sqlite3_bind_text(pStmt, 1, pVtab->zTableName, -1, 0); 311 if( sqlite3_step(pStmt)==SQLITE_ROW ){ 312 int rc2; 313 const char *zCreateTable = (const char *)sqlite3_column_text(pStmt, 0); 314 rc = sqlite3_declare_vtab(db, zCreateTable); 315 rc2 = sqlite3_finalize(pStmt); 316 if( rc==SQLITE_OK ){ 317 rc = rc2; 318 } 319 } else { 320 rc = sqlite3_finalize(pStmt); 321 if( rc==SQLITE_OK ){ 322 rc = SQLITE_ERROR; 323 } 324 } 325 if( rc==SQLITE_OK ){ 326 rc = getColumnNames(db, pVtab->zTableName, &pVtab->aCol, &pVtab->nCol); 327 } 328 if( rc==SQLITE_OK ){ 329 rc = getIndexArray(db, pVtab->zTableName, pVtab->nCol, &pVtab->aIndex); 330 } 331 } 332 } 333 334 return rc; 335 } 336 337 /* 338 ** This function frees all runtime structures associated with the virtual 339 ** table pVtab. 340 */ 341 static int echoDestructor(sqlite3_vtab *pVtab){ 342 echo_vtab *p = (echo_vtab*)pVtab; 343 sqlite3_free(p->aIndex); 344 sqlite3_free(p->aCol); 345 sqlite3_free(p->zThis); 346 sqlite3_free(p->zTableName); 347 sqlite3_free(p->zLogName); 348 sqlite3_free(p); 349 return 0; 350 } 351 352 /* 353 ** This function is called to do the work of the xConnect() method - 354 ** to allocate the required in-memory structures for a newly connected 355 ** virtual table. 356 */ 357 static int echoConstructor( 358 sqlite3 *db, 359 void *pAux, 360 int argc, const char *const*argv, 361 sqlite3_vtab **ppVtab, 362 char **pzErr 363 ){ 364 int i; 365 echo_vtab *pVtab; 366 367 /* Allocate the sqlite3_vtab/echo_vtab structure itself */ 368 pVtab = sqlite3MallocZero( sizeof(*pVtab) ); 369 if( !pVtab ){ 370 return SQLITE_NOMEM; 371 } 372 pVtab->interp = (Tcl_Interp *)pAux; 373 pVtab->db = db; 374 375 /* Allocate echo_vtab.zThis */ 376 pVtab->zThis = sqlite3MPrintf(0, "%s", argv[2]); 377 if( !pVtab->zThis ){ 378 echoDestructor((sqlite3_vtab *)pVtab); 379 return SQLITE_NOMEM; 380 } 381 382 /* Allocate echo_vtab.zTableName */ 383 if( argc>3 ){ 384 pVtab->zTableName = sqlite3MPrintf(0, "%s", argv[3]); 385 dequoteString(pVtab->zTableName); 386 if( pVtab->zTableName && pVtab->zTableName[0]=='*' ){ 387 char *z = sqlite3MPrintf(0, "%s%s", argv[2], &(pVtab->zTableName[1])); 388 sqlite3_free(pVtab->zTableName); 389 pVtab->zTableName = z; 390 pVtab->isPattern = 1; 391 } 392 if( !pVtab->zTableName ){ 393 echoDestructor((sqlite3_vtab *)pVtab); 394 return SQLITE_NOMEM; 395 } 396 } 397 398 /* Log the arguments to this function to Tcl var ::echo_module */ 399 for(i=0; i<argc; i++){ 400 appendToEchoModule(pVtab->interp, argv[i]); 401 } 402 403 /* Invoke sqlite3_declare_vtab and set up other members of the echo_vtab 404 ** structure. If an error occurs, delete the sqlite3_vtab structure and 405 ** return an error code. 406 */ 407 if( echoDeclareVtab(pVtab, db) ){ 408 echoDestructor((sqlite3_vtab *)pVtab); 409 return SQLITE_ERROR; 410 } 411 412 /* Success. Set *ppVtab and return */ 413 *ppVtab = &pVtab->base; 414 return SQLITE_OK; 415 } 416 417 /* 418 ** Echo virtual table module xCreate method. 419 */ 420 static int echoCreate( 421 sqlite3 *db, 422 void *pAux, 423 int argc, const char *const*argv, 424 sqlite3_vtab **ppVtab, 425 char **pzErr 426 ){ 427 int rc = SQLITE_OK; 428 appendToEchoModule((Tcl_Interp *)(pAux), "xCreate"); 429 rc = echoConstructor(db, pAux, argc, argv, ppVtab, pzErr); 430 431 /* If there were two arguments passed to the module at the SQL level 432 ** (i.e. "CREATE VIRTUAL TABLE tbl USING echo(arg1, arg2)"), then 433 ** the second argument is used as a table name. Attempt to create 434 ** such a table with a single column, "logmsg". This table will 435 ** be used to log calls to the xUpdate method. It will be deleted 436 ** when the virtual table is DROPed. 437 ** 438 ** Note: The main point of this is to test that we can drop tables 439 ** from within an xDestroy method call. 440 */ 441 if( rc==SQLITE_OK && argc==5 ){ 442 char *zSql; 443 echo_vtab *pVtab = *(echo_vtab **)ppVtab; 444 pVtab->zLogName = sqlite3MPrintf(0, "%s", argv[4]); 445 zSql = sqlite3MPrintf(0, "CREATE TABLE %Q(logmsg)", pVtab->zLogName); 446 rc = sqlite3_exec(db, zSql, 0, 0, 0); 447 sqlite3_free(zSql); 448 } 449 450 return rc; 451 } 452 453 /* 454 ** Echo virtual table module xConnect method. 455 */ 456 static int echoConnect( 457 sqlite3 *db, 458 void *pAux, 459 int argc, const char *const*argv, 460 sqlite3_vtab **ppVtab, 461 char **pzErr 462 ){ 463 appendToEchoModule((Tcl_Interp *)(pAux), "xConnect"); 464 return echoConstructor(db, pAux, argc, argv, ppVtab, pzErr); 465 } 466 467 /* 468 ** Echo virtual table module xDisconnect method. 469 */ 470 static int echoDisconnect(sqlite3_vtab *pVtab){ 471 appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDisconnect"); 472 return echoDestructor(pVtab); 473 } 474 475 /* 476 ** Echo virtual table module xDestroy method. 477 */ 478 static int echoDestroy(sqlite3_vtab *pVtab){ 479 int rc = SQLITE_OK; 480 echo_vtab *p = (echo_vtab *)pVtab; 481 appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDestroy"); 482 483 /* Drop the "log" table, if one exists (see echoCreate() for details) */ 484 if( p && p->zLogName ){ 485 char *zSql; 486 zSql = sqlite3MPrintf(0, "DROP TABLE %Q", p->zLogName); 487 rc = sqlite3_exec(p->db, zSql, 0, 0, 0); 488 sqlite3_free(zSql); 489 } 490 491 if( rc==SQLITE_OK ){ 492 rc = echoDestructor(pVtab); 493 } 494 return rc; 495 } 496 497 /* 498 ** Echo virtual table module xOpen method. 499 */ 500 static int echoOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){ 501 echo_cursor *pCur; 502 pCur = sqlite3MallocZero(sizeof(echo_cursor)); 503 *ppCursor = (sqlite3_vtab_cursor *)pCur; 504 return (pCur ? SQLITE_OK : SQLITE_NOMEM); 505 } 506 507 /* 508 ** Echo virtual table module xClose method. 509 */ 510 static int echoClose(sqlite3_vtab_cursor *cur){ 511 int rc; 512 echo_cursor *pCur = (echo_cursor *)cur; 513 sqlite3_stmt *pStmt = pCur->pStmt; 514 pCur->pStmt = 0; 515 sqlite3_free(pCur); 516 rc = sqlite3_finalize(pStmt); 517 return rc; 518 } 519 520 /* 521 ** Return non-zero if the cursor does not currently point to a valid record 522 ** (i.e if the scan has finished), or zero otherwise. 523 */ 524 static int echoEof(sqlite3_vtab_cursor *cur){ 525 return (((echo_cursor *)cur)->pStmt ? 0 : 1); 526 } 527 528 /* 529 ** Echo virtual table module xNext method. 530 */ 531 static int echoNext(sqlite3_vtab_cursor *cur){ 532 int rc = SQLITE_OK; 533 echo_cursor *pCur = (echo_cursor *)cur; 534 535 if( pCur->pStmt ){ 536 rc = sqlite3_step(pCur->pStmt); 537 if( rc==SQLITE_ROW ){ 538 rc = SQLITE_OK; 539 }else{ 540 rc = sqlite3_finalize(pCur->pStmt); 541 pCur->pStmt = 0; 542 } 543 } 544 545 return rc; 546 } 547 548 /* 549 ** Echo virtual table module xColumn method. 550 */ 551 static int echoColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){ 552 int iCol = i + 1; 553 sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt; 554 if( !pStmt ){ 555 sqlite3_result_null(ctx); 556 }else{ 557 assert( sqlite3_data_count(pStmt)>iCol ); 558 sqlite3_result_value(ctx, sqlite3_column_value(pStmt, iCol)); 559 } 560 return SQLITE_OK; 561 } 562 563 /* 564 ** Echo virtual table module xRowid method. 565 */ 566 static int echoRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ 567 sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt; 568 *pRowid = sqlite3_column_int64(pStmt, 0); 569 return SQLITE_OK; 570 } 571 572 /* 573 ** Compute a simple hash of the null terminated string zString. 574 ** 575 ** This module uses only sqlite3_index_info.idxStr, not 576 ** sqlite3_index_info.idxNum. So to test idxNum, when idxStr is set 577 ** in echoBestIndex(), idxNum is set to the corresponding hash value. 578 ** In echoFilter(), code assert()s that the supplied idxNum value is 579 ** indeed the hash of the supplied idxStr. 580 */ 581 static int hashString(const char *zString){ 582 int val = 0; 583 int ii; 584 for(ii=0; zString[ii]; ii++){ 585 val = (val << 3) + (int)zString[ii]; 586 } 587 return val; 588 } 589 590 /* 591 ** Echo virtual table module xFilter method. 592 */ 593 static int echoFilter( 594 sqlite3_vtab_cursor *pVtabCursor, 595 int idxNum, const char *idxStr, 596 int argc, sqlite3_value **argv 597 ){ 598 int rc; 599 int i; 600 601 echo_cursor *pCur = (echo_cursor *)pVtabCursor; 602 echo_vtab *pVtab = (echo_vtab *)pVtabCursor->pVtab; 603 sqlite3 *db = pVtab->db; 604 605 /* Check that idxNum matches idxStr */ 606 assert( idxNum==hashString(idxStr) ); 607 608 /* Log arguments to the ::echo_module Tcl variable */ 609 appendToEchoModule(pVtab->interp, "xFilter"); 610 appendToEchoModule(pVtab->interp, idxStr); 611 for(i=0; i<argc; i++){ 612 appendToEchoModule(pVtab->interp, (const char*)sqlite3_value_text(argv[i])); 613 } 614 615 sqlite3_finalize(pCur->pStmt); 616 pCur->pStmt = 0; 617 618 /* Prepare the SQL statement created by echoBestIndex and bind the 619 ** runtime parameters passed to this function to it. 620 */ 621 rc = sqlite3_prepare(db, idxStr, -1, &pCur->pStmt, 0); 622 assert( pCur->pStmt || rc!=SQLITE_OK ); 623 for(i=0; rc==SQLITE_OK && i<argc; i++){ 624 sqlite3_bind_value(pCur->pStmt, i+1, argv[i]); 625 } 626 627 /* If everything was successful, advance to the first row of the scan */ 628 if( rc==SQLITE_OK ){ 629 rc = echoNext(pVtabCursor); 630 } 631 632 return rc; 633 } 634 635 636 /* 637 ** A helper function used by echoUpdate() and echoBestIndex() for 638 ** manipulating strings in concert with the sqlite3_mprintf() function. 639 ** 640 ** Parameter pzStr points to a pointer to a string allocated with 641 ** sqlite3_mprintf. The second parameter, zAppend, points to another 642 ** string. The two strings are concatenated together and *pzStr 643 ** set to point at the result. The initial buffer pointed to by *pzStr 644 ** is deallocated via sqlite3_free(). 645 ** 646 ** If the third argument, doFree, is true, then sqlite3_free() is 647 ** also called to free the buffer pointed to by zAppend. 648 */ 649 static void string_concat(char **pzStr, char *zAppend, int doFree){ 650 char *zIn = *pzStr; 651 if( zIn ){ 652 char *zTemp = zIn; 653 zIn = sqlite3_mprintf("%s%s", zIn, zAppend); 654 sqlite3_free(zTemp); 655 }else{ 656 zIn = sqlite3_mprintf("%s", zAppend); 657 } 658 *pzStr = zIn; 659 if( doFree ){ 660 sqlite3_free(zAppend); 661 } 662 } 663 664 /* 665 ** The echo module implements the subset of query constraints and sort 666 ** orders that may take advantage of SQLite indices on the underlying 667 ** real table. For example, if the real table is declared as: 668 ** 669 ** CREATE TABLE real(a, b, c); 670 ** CREATE INDEX real_index ON real(b); 671 ** 672 ** then the echo module handles WHERE or ORDER BY clauses that refer 673 ** to the column "b", but not "a" or "c". If a multi-column index is 674 ** present, only it's left most column is considered. 675 ** 676 ** This xBestIndex method encodes the proposed search strategy as 677 ** an SQL query on the real table underlying the virtual echo module 678 ** table and stores the query in sqlite3_index_info.idxStr. The SQL 679 ** statement is of the form: 680 ** 681 ** SELECT rowid, * FROM <real-table> ?<where-clause>? ?<order-by-clause>? 682 ** 683 ** where the <where-clause> and <order-by-clause> are determined 684 ** by the contents of the structure pointed to by the pIdxInfo argument. 685 */ 686 static int echoBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){ 687 int ii; 688 char *zQuery = 0; 689 char *zNew; 690 int nArg = 0; 691 const char *zSep = "WHERE"; 692 echo_vtab *pVtab = (echo_vtab *)tab; 693 sqlite3_stmt *pStmt = 0; 694 Tcl_Interp *interp = pVtab->interp; 695 696 int nRow; 697 int useIdx = 0; 698 int rc = SQLITE_OK; 699 int useCost = 0; 700 double cost; 701 702 /* Determine the number of rows in the table and store this value in local 703 ** variable nRow. The 'estimated-cost' of the scan will be the number of 704 ** rows in the table for a linear scan, or the log (base 2) of the 705 ** number of rows if the proposed scan uses an index. 706 */ 707 if( Tcl_GetVar(interp, "echo_module_cost", TCL_GLOBAL_ONLY) ){ 708 cost = atof(Tcl_GetVar(interp, "echo_module_cost", TCL_GLOBAL_ONLY)); 709 useCost = 1; 710 } else { 711 zQuery = sqlite3_mprintf("SELECT count(*) FROM %Q", pVtab->zTableName); 712 rc = sqlite3_prepare(pVtab->db, zQuery, -1, &pStmt, 0); 713 sqlite3_free(zQuery); 714 if( rc!=SQLITE_OK ){ 715 return rc; 716 } 717 sqlite3_step(pStmt); 718 nRow = sqlite3_column_int(pStmt, 0); 719 rc = sqlite3_finalize(pStmt); 720 if( rc!=SQLITE_OK ){ 721 return rc; 722 } 723 } 724 725 zQuery = sqlite3_mprintf("SELECT rowid, * FROM %Q", pVtab->zTableName); 726 for(ii=0; ii<pIdxInfo->nConstraint; ii++){ 727 const struct sqlite3_index_constraint *pConstraint; 728 struct sqlite3_index_constraint_usage *pUsage; 729 int iCol; 730 731 pConstraint = &pIdxInfo->aConstraint[ii]; 732 pUsage = &pIdxInfo->aConstraintUsage[ii]; 733 734 iCol = pConstraint->iColumn; 735 if( pVtab->aIndex[iCol] ){ 736 char *zCol = pVtab->aCol[iCol]; 737 char *zOp = 0; 738 useIdx = 1; 739 if( iCol<0 ){ 740 zCol = "rowid"; 741 } 742 switch( pConstraint->op ){ 743 case SQLITE_INDEX_CONSTRAINT_EQ: 744 zOp = "="; break; 745 case SQLITE_INDEX_CONSTRAINT_LT: 746 zOp = "<"; break; 747 case SQLITE_INDEX_CONSTRAINT_GT: 748 zOp = ">"; break; 749 case SQLITE_INDEX_CONSTRAINT_LE: 750 zOp = "<="; break; 751 case SQLITE_INDEX_CONSTRAINT_GE: 752 zOp = ">="; break; 753 case SQLITE_INDEX_CONSTRAINT_MATCH: 754 zOp = "LIKE"; break; 755 } 756 if( zOp[0]=='L' ){ 757 zNew = sqlite3_mprintf(" %s %s LIKE (SELECT '%%'||?||'%%')", 758 zSep, zCol); 759 } else { 760 zNew = sqlite3_mprintf(" %s %s %s ?", zSep, zCol, zOp); 761 } 762 string_concat(&zQuery, zNew, 1); 763 764 zSep = "AND"; 765 pUsage->argvIndex = ++nArg; 766 pUsage->omit = 1; 767 } 768 } 769 770 /* If there is only one term in the ORDER BY clause, and it is 771 ** on a column that this virtual table has an index for, then consume 772 ** the ORDER BY clause. 773 */ 774 if( pIdxInfo->nOrderBy==1 && pVtab->aIndex[pIdxInfo->aOrderBy->iColumn] ){ 775 int iCol = pIdxInfo->aOrderBy->iColumn; 776 char *zCol = pVtab->aCol[iCol]; 777 char *zDir = pIdxInfo->aOrderBy->desc?"DESC":"ASC"; 778 if( iCol<0 ){ 779 zCol = "rowid"; 780 } 781 zNew = sqlite3_mprintf(" ORDER BY %s %s", zCol, zDir); 782 string_concat(&zQuery, zNew, 1); 783 pIdxInfo->orderByConsumed = 1; 784 } 785 786 appendToEchoModule(pVtab->interp, "xBestIndex");; 787 appendToEchoModule(pVtab->interp, zQuery); 788 789 if( !zQuery ){ 790 return SQLITE_NOMEM; 791 } 792 pIdxInfo->idxNum = hashString(zQuery); 793 pIdxInfo->idxStr = zQuery; 794 pIdxInfo->needToFreeIdxStr = 1; 795 if (useCost) { 796 pIdxInfo->estimatedCost = cost; 797 } else if( useIdx ){ 798 /* Approximation of log2(nRow). */ 799 for( ii=0; ii<(sizeof(int)*8); ii++ ){ 800 if( nRow & (1<<ii) ){ 801 pIdxInfo->estimatedCost = (double)ii; 802 } 803 } 804 } else { 805 pIdxInfo->estimatedCost = (double)nRow; 806 } 807 return rc; 808 } 809 810 /* 811 ** The xUpdate method for echo module virtual tables. 812 ** 813 ** apData[0] apData[1] apData[2..] 814 ** 815 ** INTEGER DELETE 816 ** 817 ** INTEGER NULL (nCol args) UPDATE (do not set rowid) 818 ** INTEGER INTEGER (nCol args) UPDATE (with SET rowid = <arg1>) 819 ** 820 ** NULL NULL (nCol args) INSERT INTO (automatic rowid value) 821 ** NULL INTEGER (nCol args) INSERT (incl. rowid value) 822 ** 823 */ 824 int echoUpdate( 825 sqlite3_vtab *tab, 826 int nData, 827 sqlite3_value **apData, 828 sqlite_int64 *pRowid 829 ){ 830 echo_vtab *pVtab = (echo_vtab *)tab; 831 sqlite3 *db = pVtab->db; 832 int rc = SQLITE_OK; 833 834 sqlite3_stmt *pStmt; 835 char *z = 0; /* SQL statement to execute */ 836 int bindArgZero = 0; /* True to bind apData[0] to sql var no. nData */ 837 int bindArgOne = 0; /* True to bind apData[1] to sql var no. 1 */ 838 int i; /* Counter variable used by for loops */ 839 840 assert( nData==pVtab->nCol+2 || nData==1 ); 841 842 /* If apData[0] is an integer and nData>1 then do an UPDATE */ 843 if( nData>1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){ 844 char *zSep = " SET"; 845 z = sqlite3_mprintf("UPDATE %Q", pVtab->zTableName); 846 847 bindArgOne = (apData[1] && sqlite3_value_type(apData[1])==SQLITE_INTEGER); 848 bindArgZero = 1; 849 850 if( bindArgOne ){ 851 string_concat(&z, " SET rowid=?1 ", 0); 852 zSep = ","; 853 } 854 for(i=2; i<nData; i++){ 855 if( apData[i]==0 ) continue; 856 string_concat(&z, sqlite3_mprintf( 857 "%s %Q=?%d", zSep, pVtab->aCol[i-2], i), 1); 858 zSep = ","; 859 } 860 string_concat(&z, sqlite3_mprintf(" WHERE rowid=?%d", nData), 1); 861 } 862 863 /* If apData[0] is an integer and nData==1 then do a DELETE */ 864 else if( nData==1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){ 865 z = sqlite3_mprintf("DELETE FROM %Q WHERE rowid = ?1", pVtab->zTableName); 866 bindArgZero = 1; 867 } 868 869 /* If the first argument is NULL and there are more than two args, INSERT */ 870 else if( nData>2 && sqlite3_value_type(apData[0])==SQLITE_NULL ){ 871 int ii; 872 char *zInsert = 0; 873 char *zValues = 0; 874 875 zInsert = sqlite3_mprintf("INSERT INTO %Q (", pVtab->zTableName); 876 if( sqlite3_value_type(apData[1])==SQLITE_INTEGER ){ 877 bindArgOne = 1; 878 zValues = sqlite3_mprintf("?"); 879 string_concat(&zInsert, "rowid", 0); 880 } 881 882 assert((pVtab->nCol+2)==nData); 883 for(ii=2; ii<nData; ii++){ 884 string_concat(&zInsert, 885 sqlite3_mprintf("%s%Q", zValues?", ":"", pVtab->aCol[ii-2]), 1); 886 string_concat(&zValues, 887 sqlite3_mprintf("%s?%d", zValues?", ":"", ii), 1); 888 } 889 890 string_concat(&z, zInsert, 1); 891 string_concat(&z, ") VALUES(", 0); 892 string_concat(&z, zValues, 1); 893 string_concat(&z, ")", 0); 894 } 895 896 /* Anything else is an error */ 897 else{ 898 assert(0); 899 return SQLITE_ERROR; 900 } 901 902 rc = sqlite3_prepare(db, z, -1, &pStmt, 0); 903 assert( rc!=SQLITE_OK || pStmt ); 904 sqlite3_free(z); 905 if( rc==SQLITE_OK ) { 906 if( bindArgZero ){ 907 sqlite3_bind_value(pStmt, nData, apData[0]); 908 } 909 if( bindArgOne ){ 910 sqlite3_bind_value(pStmt, 1, apData[1]); 911 } 912 for(i=2; i<nData; i++){ 913 if( apData[i] ) sqlite3_bind_value(pStmt, i, apData[i]); 914 } 915 sqlite3_step(pStmt); 916 rc = sqlite3_finalize(pStmt); 917 } 918 919 if( pRowid && rc==SQLITE_OK ){ 920 *pRowid = sqlite3_last_insert_rowid(db); 921 } 922 923 return rc; 924 } 925 926 /* 927 ** xBegin, xSync, xCommit and xRollback callbacks for echo module 928 ** virtual tables. Do nothing other than add the name of the callback 929 ** to the $::echo_module Tcl variable. 930 */ 931 static int echoTransactionCall(sqlite3_vtab *tab, const char *zCall){ 932 char *z; 933 echo_vtab *pVtab = (echo_vtab *)tab; 934 z = sqlite3_mprintf("echo(%s)", pVtab->zTableName); 935 appendToEchoModule(pVtab->interp, zCall); 936 appendToEchoModule(pVtab->interp, z); 937 sqlite3_free(z); 938 return SQLITE_OK; 939 } 940 static int echoBegin(sqlite3_vtab *tab){ 941 echo_vtab *pVtab = (echo_vtab *)tab; 942 Tcl_Interp *interp = pVtab->interp; 943 const char *zVal; 944 945 echoTransactionCall(tab, "xBegin"); 946 947 /* Check if the $::echo_module_begin_fail variable is defined. If it is, 948 ** and it is set to the name of the real table underlying this virtual 949 ** echo module table, then cause this xSync operation to fail. 950 */ 951 zVal = Tcl_GetVar(interp, "echo_module_begin_fail", TCL_GLOBAL_ONLY); 952 if( zVal && 0==strcmp(zVal, pVtab->zTableName) ){ 953 return SQLITE_ERROR; 954 } 955 return SQLITE_OK; 956 } 957 static int echoSync(sqlite3_vtab *tab){ 958 echo_vtab *pVtab = (echo_vtab *)tab; 959 Tcl_Interp *interp = pVtab->interp; 960 const char *zVal; 961 962 echoTransactionCall(tab, "xSync"); 963 964 /* Check if the $::echo_module_sync_fail variable is defined. If it is, 965 ** and it is set to the name of the real table underlying this virtual 966 ** echo module table, then cause this xSync operation to fail. 967 */ 968 zVal = Tcl_GetVar(interp, "echo_module_sync_fail", TCL_GLOBAL_ONLY); 969 if( zVal && 0==strcmp(zVal, pVtab->zTableName) ){ 970 return -1; 971 } 972 return SQLITE_OK; 973 } 974 static int echoCommit(sqlite3_vtab *tab){ 975 return echoTransactionCall(tab, "xCommit"); 976 } 977 static int echoRollback(sqlite3_vtab *tab){ 978 return echoTransactionCall(tab, "xRollback"); 979 } 980 981 /* 982 ** Implementation of "GLOB" function on the echo module. Pass 983 ** all arguments to the ::echo_glob_overload procedure of TCL 984 ** and return the result of that procedure as a string. 985 */ 986 static void overloadedGlobFunction( 987 sqlite3_context *pContext, 988 int nArg, 989 sqlite3_value **apArg 990 ){ 991 Tcl_Interp *interp = sqlite3_user_data(pContext); 992 Tcl_DString str; 993 int i; 994 int rc; 995 Tcl_DStringInit(&str); 996 Tcl_DStringAppendElement(&str, "::echo_glob_overload"); 997 for(i=0; i<nArg; i++){ 998 Tcl_DStringAppendElement(&str, (char*)sqlite3_value_text(apArg[i])); 999 } 1000 rc = Tcl_Eval(interp, Tcl_DStringValue(&str)); 1001 Tcl_DStringFree(&str); 1002 if( rc ){ 1003 sqlite3_result_error(pContext, Tcl_GetStringResult(interp), -1); 1004 }else{ 1005 sqlite3_result_text(pContext, Tcl_GetStringResult(interp), 1006 -1, SQLITE_TRANSIENT); 1007 } 1008 Tcl_ResetResult(interp); 1009 } 1010 1011 /* 1012 ** This is the xFindFunction implementation for the echo module. 1013 ** SQLite calls this routine when the first argument of a function 1014 ** is a column of an echo virtual table. This routine can optionally 1015 ** override the implementation of that function. It will choose to 1016 ** do so if the function is named "glob", and a TCL command named 1017 ** ::echo_glob_overload exists. 1018 */ 1019 static int echoFindFunction( 1020 sqlite3_vtab *vtab, 1021 int nArg, 1022 const char *zFuncName, 1023 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), 1024 void **ppArg 1025 ){ 1026 echo_vtab *pVtab = (echo_vtab *)vtab; 1027 Tcl_Interp *interp = pVtab->interp; 1028 Tcl_CmdInfo info; 1029 if( strcmp(zFuncName,"glob")!=0 ){ 1030 return 0; 1031 } 1032 if( Tcl_GetCommandInfo(interp, "::echo_glob_overload", &info)==0 ){ 1033 return 0; 1034 } 1035 *pxFunc = overloadedGlobFunction; 1036 *ppArg = interp; 1037 return 1; 1038 } 1039 1040 static int echoRename(sqlite3_vtab *vtab, const char *zNewName){ 1041 int rc = SQLITE_OK; 1042 echo_vtab *p = (echo_vtab *)vtab; 1043 1044 if( p->isPattern ){ 1045 int nThis = strlen(p->zThis); 1046 char *zSql = sqlite3MPrintf(0, "ALTER TABLE %s RENAME TO %s%s", 1047 p->zTableName, zNewName, &p->zTableName[nThis] 1048 ); 1049 rc = sqlite3_exec(p->db, zSql, 0, 0, 0); 1050 sqlite3_free(zSql); 1051 } 1052 1053 return rc; 1054 } 1055 1056 /* 1057 ** A virtual table module that merely "echos" the contents of another 1058 ** table (like an SQL VIEW). 1059 */ 1060 static sqlite3_module echoModule = { 1061 0, /* iVersion */ 1062 echoCreate, 1063 echoConnect, 1064 echoBestIndex, 1065 echoDisconnect, 1066 echoDestroy, 1067 echoOpen, /* xOpen - open a cursor */ 1068 echoClose, /* xClose - close a cursor */ 1069 echoFilter, /* xFilter - configure scan constraints */ 1070 echoNext, /* xNext - advance a cursor */ 1071 echoEof, /* xEof */ 1072 echoColumn, /* xColumn - read data */ 1073 echoRowid, /* xRowid - read data */ 1074 echoUpdate, /* xUpdate - write data */ 1075 echoBegin, /* xBegin - begin transaction */ 1076 echoSync, /* xSync - sync transaction */ 1077 echoCommit, /* xCommit - commit transaction */ 1078 echoRollback, /* xRollback - rollback transaction */ 1079 echoFindFunction, /* xFindFunction - function overloading */ 1080 echoRename, /* xRename - rename the table */ 1081 }; 1082 1083 /* 1084 ** Decode a pointer to an sqlite3 object. 1085 */ 1086 static int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb){ 1087 *ppDb = (sqlite3*)sqlite3TextToPtr(zA); 1088 return TCL_OK; 1089 } 1090 1091 /* 1092 ** Register the echo virtual table module. 1093 */ 1094 static int register_echo_module( 1095 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 1096 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 1097 int objc, /* Number of arguments */ 1098 Tcl_Obj *CONST objv[] /* Command arguments */ 1099 ){ 1100 sqlite3 *db; 1101 if( objc!=2 ){ 1102 Tcl_WrongNumArgs(interp, 1, objv, "DB"); 1103 return TCL_ERROR; 1104 } 1105 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 1106 sqlite3_create_module(db, "echo", &echoModule, (void *)interp); 1107 return TCL_OK; 1108 } 1109 1110 /* 1111 ** Tcl interface to sqlite3_declare_vtab, invoked as follows from Tcl: 1112 ** 1113 ** sqlite3_declare_vtab DB SQL 1114 */ 1115 static int declare_vtab( 1116 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 1117 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 1118 int objc, /* Number of arguments */ 1119 Tcl_Obj *CONST objv[] /* Command arguments */ 1120 ){ 1121 sqlite3 *db; 1122 int rc; 1123 if( objc!=3 ){ 1124 Tcl_WrongNumArgs(interp, 1, objv, "DB SQL"); 1125 return TCL_ERROR; 1126 } 1127 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 1128 rc = sqlite3_declare_vtab(db, Tcl_GetString(objv[2])); 1129 if( rc!=SQLITE_OK ){ 1130 Tcl_SetResult(interp, (char *)sqlite3_errmsg(db), TCL_VOLATILE); 1131 return TCL_ERROR; 1132 } 1133 return TCL_OK; 1134 } 1135 1136 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */ 1137 1138 /* 1139 ** Register commands with the TCL interpreter. 1140 */ 1141 int Sqlitetest8_Init(Tcl_Interp *interp){ 1142 static struct { 1143 char *zName; 1144 Tcl_ObjCmdProc *xProc; 1145 void *clientData; 1146 } aObjCmd[] = { 1147 #ifndef SQLITE_OMIT_VIRTUALTABLE 1148 { "register_echo_module", register_echo_module, 0 }, 1149 { "sqlite3_declare_vtab", declare_vtab, 0 }, 1150 #endif 1151 }; 1152 int i; 1153 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){ 1154 Tcl_CreateObjCommand(interp, aObjCmd[i].zName, 1155 aObjCmd[i].xProc, aObjCmd[i].clientData, 0); 1156 } 1157 return TCL_OK; 1158 } 1159