Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Enhance the "swarmvtab" extension. See header comments in ext/misc/unionvtab.c for details. |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
01c173651ab22b7b0c139eded6f2ad85 |
User & Date: | dan 2017-12-15 20:21:17.831 |
Context
2017-12-16
| ||
04:37 | Add unnecessary initializations to some local variables in the rtree module to suppress false-positive compiler warnings coming out of MSVC. (check-in: 64487d658c user: drh tags: trunk) | |
2017-12-15
| ||
20:21 | Enhance the "swarmvtab" extension. See header comments in ext/misc/unionvtab.c for details. (check-in: 01c173651a user: dan tags: trunk) | |
12:22 | In the LEMON parser generator, provide reduce actions with access to the lookahead token. (check-in: 42af190f4f user: drh tags: trunk) | |
Changes
Changes to ext/misc/unionvtab.c.
︙ | |||
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + | ** 3. The smallest rowid in the range of rowids that may be stored in the ** database table (an integer). ** ** 4. The largest rowid in the range of rowids that may be stored in the ** database table (an integer). ** ** SWARMVTAB ** ** LEGACY SYNTAX: ** ** A "swarmvtab" virtual table is created similarly to a unionvtab table: ** ** CREATE VIRTUAL TABLE <name> ** USING swarmvtab(<sql-statement>, <callback>); ** ** The difference is that for a swarmvtab table, the first column returned ** by the <sql statement> must return a path or URI that can be used to open ** the database file containing the source table. The <callback> option ** is optional. If included, it is the name of an application-defined ** SQL function that is invoked with the URI of the file, if the file |
︙ | |||
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 | + + - + + + | char *zDb; /* Database containing source table */ char *zTab; /* Source table name */ sqlite3_int64 iMin; /* Minimum rowid */ sqlite3_int64 iMax; /* Maximum rowid */ /* Fields used by swarmvtab only */ char *zFile; /* Database file containing table zTab */ char *zContext; /* Context string, if any */ int nUser; /* Current number of users */ sqlite3 *db; /* Database handle */ UnionSrc *pNextClosable; /* Next in list of closable sources */ }; /* ** Virtual table type for union vtab. */ struct UnionTab { sqlite3_vtab base; /* Base class - must be first */ sqlite3 *db; /* Database handle */ int bSwarm; /* 1 for "swarmvtab", 0 for "unionvtab" */ int iPK; /* INTEGER PRIMARY KEY column, or -1 */ int nSrc; /* Number of elements in the aSrc[] array */ UnionSrc *aSrc; /* Array of source tables, sorted by rowid */ /* Used by swarmvtab only */ int bHasContext; /* Has context strings */ char *zSourceStr; /* Expected unionSourceToStr() value */ |
︙ | |||
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 | 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 | + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - + + + + + + + + - - + + + | if( *pRc==SQLITE_OK ){ *pRc = rc; if( rc ){ *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db)); } } } /* ** If an "openclose" UDF was supplied when this virtual table was created, ** invoke it now. The first argument passed is the name of the database ** file for source pSrc. The second is integer value bClose. ** ** If successful, return SQLITE_OK. Otherwise an SQLite error code. In this ** case if argument pzErr is not NULL, also set (*pzErr) to an English ** language error message. The caller is responsible for eventually freeing ** any error message using sqlite3_free(). */ static int unionInvokeOpenClose( UnionTab *pTab, UnionSrc *pSrc, int bClose, char **pzErr ){ int rc = SQLITE_OK; if( pTab->pOpenClose ){ sqlite3_bind_text(pTab->pOpenClose, 1, pSrc->zFile, -1, SQLITE_STATIC); if( pTab->bHasContext ){ sqlite3_bind_text(pTab->pOpenClose, 2, pSrc->zContext, -1, SQLITE_STATIC); } sqlite3_bind_int(pTab->pOpenClose, 2+pTab->bHasContext, bClose); sqlite3_step(pTab->pOpenClose); if( SQLITE_OK!=(rc = sqlite3_reset(pTab->pOpenClose)) ){ if( pzErr ){ *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db)); } } } return rc; } /* ** This function is a no-op for unionvtab. For swarmvtab, it attempts to ** close open database files until at most nMax are open. An SQLite error ** code is returned if an error occurs, or SQLITE_OK otherwise. */ static void unionCloseSources(UnionTab *pTab, int nMax){ while( pTab->pClosable && pTab->nOpen>nMax ){ UnionSrc *p; UnionSrc **pp; for(pp=&pTab->pClosable; (*pp)->pNextClosable; pp=&(*pp)->pNextClosable); p = *pp; |
︙ | |||
492 493 494 495 496 497 498 | 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 | - + - - - + + + + + - + - - + - - - + + + + + + + - - - | sqlite3_free(z); } sqlite3_free(z0); return rc; } |
︙ | |||
568 569 570 571 572 573 574 575 576 577 578 579 580 581 | 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 | + | if( rc==SQLITE_OK ){ pSrc->pNextClosable = pTab->pClosable; pTab->pClosable = pSrc; pTab->nOpen++; }else{ sqlite3_close(pSrc->db); pSrc->db = 0; unionInvokeOpenClose(pTab, pSrc, 1, 0); } } return rc; } |
︙ | |||
622 623 624 625 626 627 628 629 630 631 632 633 634 635 | 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 | + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + | pTab->pClosable = pSrc; } unionCloseSources(pTab, pTab->nMaxOpen); } } return rc; } /* ** Return true if the argument is a space, tab, CR or LF character. */ static int union_isspace(char c){ return (c==' ' || c=='\n' || c=='\r' || c=='\t'); } /* ** Return true if the argument is an alphanumeric character in the ** ASCII range. */ static int union_isidchar(char c){ return ((c>='a' && c<='z') || (c>='A' && c<'Z') || (c>='0' && c<='9')); } /* ** This function is called to handle all arguments following the first ** (the SQL statement) passed to a swarmvtab (not unionvtab) CREATE ** VIRTUAL TABLE statement. It may bind parameters to the SQL statement ** or configure members of the UnionTab object passed as the second ** argument. ** ** Refer to header comments at the top of this file for a description ** of the arguments parsed. ** ** This function is a no-op if *pRc is other than SQLITE_OK when it is ** called. Otherwise, if an error occurs, *pRc is set to an SQLite error ** code. In this case *pzErr may be set to point to a buffer containing ** an English language error message. It is the responsibility of the ** caller to eventually free the buffer using sqlite3_free(). */ static void unionConfigureVtab( int *pRc, /* IN/OUT: Error code */ UnionTab *pTab, /* Table to configure */ sqlite3_stmt *pStmt, /* SQL statement to find sources */ int nArg, /* Number of entries in azArg[] array */ const char * const *azArg, /* Array of arguments to consider */ char **pzErr /* OUT: Error message */ ){ int rc = *pRc; int i; if( rc==SQLITE_OK ){ pTab->bHasContext = (sqlite3_column_count(pStmt)>4); } for(i=0; rc==SQLITE_OK && i<nArg; i++){ char *zArg = unionStrdup(&rc, azArg[i]); if( zArg ){ int nOpt = 0; /* Size of option name in bytes */ char *zOpt; /* Pointer to option name */ char *zVal; /* Pointer to value */ unionDequote(zArg); zOpt = zArg; while( union_isspace(*zOpt) ) zOpt++; zVal = zOpt; if( *zVal==':' ) zVal++; while( union_isidchar(*zVal) ) zVal++; nOpt = zVal-zOpt; while( union_isspace(*zVal) ) zVal++; if( *zVal=='=' ){ zOpt[nOpt] = '\0'; zVal++; while( union_isspace(*zVal) ) zVal++; zVal = unionStrdup(&rc, zVal); if( zVal ){ unionDequote(zVal); if( zOpt[0]==':' ){ /* A value to bind to the SQL statement */ int iParam = sqlite3_bind_parameter_index(pStmt, zOpt); if( iParam==0 ){ *pzErr = sqlite3_mprintf( "swarmvtab: no such SQL parameter: %s", zOpt ); rc = SQLITE_ERROR; }else{ rc = sqlite3_bind_text(pStmt, iParam, zVal, -1, SQLITE_TRANSIENT); } }else if( nOpt==7 && 0==sqlite3_strnicmp(zOpt, "maxopen", 7) ){ pTab->nMaxOpen = atoi(zVal); if( pTab->nMaxOpen<=0 ){ *pzErr = sqlite3_mprintf("swarmvtab: illegal maxopen value"); rc = SQLITE_ERROR; } }else if( nOpt==7 && 0==sqlite3_strnicmp(zOpt, "missing", 7) ){ if( pTab->pNotFound ){ *pzErr = sqlite3_mprintf( "swarmvtab: duplicate \"missing\" option"); rc = SQLITE_ERROR; }else{ pTab->pNotFound = unionPreparePrintf(&rc, pzErr, pTab->db, "SELECT \"%w\"(?%s)", zVal, pTab->bHasContext ? ",?" : "" ); } }else if( nOpt==9 && 0==sqlite3_strnicmp(zOpt, "openclose", 9) ){ if( pTab->pOpenClose ){ *pzErr = sqlite3_mprintf( "swarmvtab: duplicate \"openclose\" option"); rc = SQLITE_ERROR; }else{ pTab->pOpenClose = unionPreparePrintf(&rc, pzErr, pTab->db, "SELECT \"%w\"(?,?%s)", zVal, pTab->bHasContext ? ",?" : "" ); } }else{ *pzErr = sqlite3_mprintf("swarmvtab: unrecognized option: %s",zOpt); rc = SQLITE_ERROR; } sqlite3_free(zVal); } }else{ if( i==0 && nArg==1 ){ pTab->pNotFound = unionPreparePrintf(&rc, pzErr, pTab->db, "SELECT \"%w\"(?)", zArg ); }else{ *pzErr = sqlite3_mprintf( "swarmvtab: parse error: %s", azArg[i]); rc = SQLITE_ERROR; } } sqlite3_free(zArg); } } *pRc = rc; } /* ** xConnect/xCreate method. ** ** The argv[] array contains the following: ** ** argv[0] -> module name ("unionvtab" or "swarmvtab") |
︙ | |||
650 651 652 653 654 655 656 | 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 | - + + + + + + + + + + + + | int bSwarm = (pAux==0 ? 0 : 1); const char *zVtab = (bSwarm ? "swarmvtab" : "unionvtab"); if( sqlite3_stricmp("temp", argv[1]) ){ /* unionvtab tables may only be created in the temp schema */ *pzErr = sqlite3_mprintf("%s tables must be created in TEMP schema", zVtab); rc = SQLITE_ERROR; |
︙ | |||
711 712 713 714 715 716 717 718 719 720 721 722 | 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 | + + + + - - - - - - - - - | pSrc->iMin = iMin; pSrc->iMax = iMax; if( bSwarm ){ pSrc->zFile = unionStrdup(&rc, zDb); }else{ pSrc->zDb = unionStrdup(&rc, zDb); } if( pTab->bHasContext ){ const char *zContext = (const char*)sqlite3_column_text(pStmt, 4); pSrc->zContext = unionStrdup(&rc, zContext); } } } unionFinalize(&rc, pStmt, pzErr); pStmt = 0; |
︙ |
Changes to test/swarmvtab.test.
︙ | |||
209 210 211 212 213 214 215 | 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | - + | do_catchsql_test 3.1 { CREATE VIRTUAL TABLE temp.xyz USING swarmvtab( 'VALUES ("test.db1", "t1", 1, 10), ("test.db2", "t1", 11, 20) ', 'fetch_db_no_such_function' ); |
︙ |
Changes to test/swarmvtab2.test.
︙ | |||
10 11 12 13 14 15 16 | 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | - + | #*********************************************************************** # This file implements regression tests for SQLite library. The # focus of this file is the "swarmvtab" extension # set testdir [file dirname $argv0] source $testdir/tester.tcl |
︙ |
Added test/swarmvtab3.test.