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