SQLite
Check-in [698ec7769d]
Not logged in
Overview
SHA1 Hash:698ec7769d4feea1782401479c61ce67d2113fd4
Date: 2012-10-05 17:44:51
User: dan
Comment:Merge the shared-cache related fixes from the shared-cache-fix branch to the trunk.
Tags And Properties
Changes
hide diffs unified diffs patch

Changes to src/alter.c

410 int savedDbFlags; /* Saved value of db->flags */ 410 int savedDbFlags; /* Saved value of db->flags */ 411 411 412 savedDbFlags = db->flags; 412 savedDbFlags = db->flags; 413 if( NEVER(db->mallocFailed) ) goto exit_rename_table; 413 if( NEVER(db->mallocFailed) ) goto exit_rename_table; 414 assert( pSrc->nSrc==1 ); 414 assert( pSrc->nSrc==1 ); 415 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) ); 415 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) ); 416 416 417 pTab = sqlite3LocateTable(pParse, 0, pSrc->a[0].zName, pSrc->a[0].zDatabase); | 417 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]); 418 if( !pTab ) goto exit_rename_table; 418 if( !pTab ) goto exit_rename_table; 419 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); 419 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); 420 zDb = db->aDb[iDb].zName; 420 zDb = db->aDb[iDb].zName; 421 db->flags |= SQLITE_PreferBuiltin; 421 db->flags |= SQLITE_PreferBuiltin; 422 422 423 /* Get a NULL terminated version of the new table name. */ 423 /* Get a NULL terminated version of the new table name. */ 424 zName = sqlite3NameFromToken(db, pName); 424 zName = sqlite3NameFromToken(db, pName); ................................................................................................................................................................................ 753 int nAlloc; 753 int nAlloc; 754 sqlite3 *db = pParse->db; 754 sqlite3 *db = pParse->db; 755 755 756 /* Look up the table being altered. */ 756 /* Look up the table being altered. */ 757 assert( pParse->pNewTable==0 ); 757 assert( pParse->pNewTable==0 ); 758 assert( sqlite3BtreeHoldsAllMutexes(db) ); 758 assert( sqlite3BtreeHoldsAllMutexes(db) ); 759 if( db->mallocFailed ) goto exit_begin_add_column; 759 if( db->mallocFailed ) goto exit_begin_add_column; 760 pTab = sqlite3LocateTable(pParse, 0, pSrc->a[0].zName, pSrc->a[0].zDatabase); | 760 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]); 761 if( !pTab ) goto exit_begin_add_column; 761 if( !pTab ) goto exit_begin_add_column; 762 762 763 #ifndef SQLITE_OMIT_VIRTUALTABLE 763 #ifndef SQLITE_OMIT_VIRTUALTABLE 764 if( IsVirtual(pTab) ){ 764 if( IsVirtual(pTab) ){ 765 sqlite3ErrorMsg(pParse, "virtual tables may not be altered"); 765 sqlite3ErrorMsg(pParse, "virtual tables may not be altered"); 766 goto exit_begin_add_column; 766 goto exit_begin_add_column; 767 } 767 }

Changes to src/attach.c

430 sqlite3 *db; 430 sqlite3 *db; 431 431 432 if( NEVER(iDb<0) || iDb==1 ) return 0; 432 if( NEVER(iDb<0) || iDb==1 ) return 0; 433 db = pParse->db; 433 db = pParse->db; 434 assert( db->nDb>iDb ); 434 assert( db->nDb>iDb ); 435 pFix->pParse = pParse; 435 pFix->pParse = pParse; 436 pFix->zDb = db->aDb[iDb].zName; 436 pFix->zDb = db->aDb[iDb].zName; > 437 pFix->pSchema = db->aDb[iDb].pSchema; 437 pFix->zType = zType; 438 pFix->zType = zType; 438 pFix->pName = pName; 439 pFix->pName = pName; 439 return 1; 440 return 1; 440 } 441 } 441 442 442 /* 443 /* 443 ** The following set of routines walk through the parse tree and assign 444 ** The following set of routines walk through the parse tree and assign ................................................................................................................................................................................ 460 int i; 461 int i; 461 const char *zDb; 462 const char *zDb; 462 struct SrcList_item *pItem; 463 struct SrcList_item *pItem; 463 464 464 if( NEVER(pList==0) ) return 0; 465 if( NEVER(pList==0) ) return 0; 465 zDb = pFix->zDb; 466 zDb = pFix->zDb; 466 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){ 467 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){ 467 if( pItem->zDatabase==0 ){ < 468 pItem->zDatabase = sqlite3DbStrDup(pFix->pParse->db, zDb); < 469 }else if( sqlite3StrICmp(pItem->zDatabase,zDb)!=0 ){ | 468 if( pItem->zDatabase && sqlite3StrICmp(pItem->zDatabase, zDb) ){ 470 sqlite3ErrorMsg(pFix->pParse, 469 sqlite3ErrorMsg(pFix->pParse, 471 "%s %T cannot reference objects in database %s", 470 "%s %T cannot reference objects in database %s", 472 pFix->zType, pFix->pName, pItem->zDatabase); 471 pFix->zType, pFix->pName, pItem->zDatabase); 473 return 1; 472 return 1; 474 } 473 } > 474 sqlite3_free(pItem->zDatabase); > 475 pItem->zDatabase = 0; > 476 pItem->pSchema = pFix->pSchema; 475 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) 477 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) 476 if( sqlite3FixSelect(pFix, pItem->pSelect) ) return 1; 478 if( sqlite3FixSelect(pFix, pItem->pSelect) ) return 1; 477 if( sqlite3FixExpr(pFix, pItem->pOn) ) return 1; 479 if( sqlite3FixExpr(pFix, pItem->pOn) ) return 1; 478 #endif 480 #endif 479 } 481 } 480 return 0; 482 return 0; 481 } 483 }

Changes to src/build.c

314 }else{ 314 }else{ 315 sqlite3ErrorMsg(pParse, "%s: %s", zMsg, zName); 315 sqlite3ErrorMsg(pParse, "%s: %s", zMsg, zName); 316 } 316 } 317 pParse->checkSchema = 1; 317 pParse->checkSchema = 1; 318 } 318 } 319 return p; 319 return p; 320 } 320 } > 321 > 322 /* > 323 ** Locate the table identified by *p. > 324 ** > 325 ** This is a wrapper around sqlite3LocateTable(). The difference between > 326 ** sqlite3LocateTable() and this function is that this function restricts > 327 ** the search to schema (p->pSchema) if it is not NULL. p->pSchema may be > 328 ** non-NULL if it is part of a view or trigger program definition. See > 329 ** sqlite3FixSrcList() for details. > 330 */ > 331 Table *sqlite3LocateTableItem( > 332 Parse *pParse, > 333 int isView, > 334 struct SrcList_item *p > 335 ){ > 336 const char *zDb; > 337 assert( p->pSchema==0 || p->zDatabase==0 ); > 338 if( p->pSchema ){ > 339 int iDb = sqlite3SchemaToIndex(pParse->db, p->pSchema); > 340 zDb = pParse->db->aDb[iDb].zName; > 341 }else{ > 342 zDb = p->zDatabase; > 343 } > 344 return sqlite3LocateTable(pParse, isView, p->zName, zDb); > 345 } 321 346 322 /* 347 /* 323 ** Locate the in-memory structure that describes 348 ** Locate the in-memory structure that describes 324 ** a particular index given the name of that index 349 ** a particular index given the name of that index 325 ** and the name of the database that contains the index. 350 ** and the name of the database that contains the index. 326 ** Return NULL if not found. 351 ** Return NULL if not found. 327 ** 352 ** ................................................................................................................................................................................ 2107 2132 2108 if( db->mallocFailed ){ 2133 if( db->mallocFailed ){ 2109 goto exit_drop_table; 2134 goto exit_drop_table; 2110 } 2135 } 2111 assert( pParse->nErr==0 ); 2136 assert( pParse->nErr==0 ); 2112 assert( pName->nSrc==1 ); 2137 assert( pName->nSrc==1 ); 2113 if( noErr ) db->suppressErr++; 2138 if( noErr ) db->suppressErr++; 2114 pTab = sqlite3LocateTable(pParse, isView, | 2139 pTab = sqlite3LocateTableItem(pParse, isView, &pName->a[0]); 2115 pName->a[0].zName, pName->a[0].zDatabase); < 2116 if( noErr ) db->suppressErr--; 2140 if( noErr ) db->suppressErr--; 2117 2141 2118 if( pTab==0 ){ 2142 if( pTab==0 ){ 2119 if( noErr ) sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase); 2143 if( noErr ) sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase); 2120 goto exit_drop_table; 2144 goto exit_drop_table; 2121 } 2145 } 2122 iDb = sqlite3SchemaToIndex(db, pTab->pSchema); 2146 iDb = sqlite3SchemaToIndex(db, pTab->pSchema); ................................................................................................................................................................................ 2548 if( sqlite3FixInit(&sFix, pParse, iDb, "index", pName) && 2572 if( sqlite3FixInit(&sFix, pParse, iDb, "index", pName) && 2549 sqlite3FixSrcList(&sFix, pTblName) 2573 sqlite3FixSrcList(&sFix, pTblName) 2550 ){ 2574 ){ 2551 /* Because the parser constructs pTblName from a single identifier, 2575 /* Because the parser constructs pTblName from a single identifier, 2552 ** sqlite3FixSrcList can never fail. */ 2576 ** sqlite3FixSrcList can never fail. */ 2553 assert(0); 2577 assert(0); 2554 } 2578 } 2555 pTab = sqlite3LocateTable(pParse, 0, pTblName->a[0].zName, | 2579 pTab = sqlite3LocateTableItem(pParse, 0, &pTblName->a[0]); 2556 pTblName->a[0].zDatabase); < 2557 if( !pTab || db->mallocFailed ) goto exit_create_index; 2580 if( !pTab || db->mallocFailed ) goto exit_create_index; 2558 assert( db->aDb[iDb].pSchema==pTab->pSchema ); 2581 assert( db->aDb[iDb].pSchema==pTab->pSchema ); 2559 }else{ 2582 }else{ 2560 assert( pName==0 ); 2583 assert( pName==0 ); 2561 assert( pStart==0 ); 2584 assert( pStart==0 ); 2562 pTab = pParse->pNewTable; 2585 pTab = pParse->pNewTable; 2563 if( !pTab ) goto exit_create_index; 2586 if( !pTab ) goto exit_create_index;

Changes to src/delete.c

28 ** pSrc->a[0].pIndex Pointer to the INDEXED BY index, if there is one 28 ** pSrc->a[0].pIndex Pointer to the INDEXED BY index, if there is one 29 ** 29 ** 30 */ 30 */ 31 Table *sqlite3SrcListLookup(Parse *pParse, SrcList *pSrc){ 31 Table *sqlite3SrcListLookup(Parse *pParse, SrcList *pSrc){ 32 struct SrcList_item *pItem = pSrc->a; 32 struct SrcList_item *pItem = pSrc->a; 33 Table *pTab; 33 Table *pTab; 34 assert( pItem && pSrc->nSrc==1 ); 34 assert( pItem && pSrc->nSrc==1 ); 35 pTab = sqlite3LocateTable(pParse, 0, pItem->zName, pItem->zDatabase); | 35 pTab = sqlite3LocateTableItem(pParse, 0, pItem); 36 sqlite3DeleteTable(pParse->db, pItem->pTab); 36 sqlite3DeleteTable(pParse->db, pItem->pTab); 37 pItem->pTab = pTab; 37 pItem->pTab = pTab; 38 if( pTab ){ 38 if( pTab ){ 39 pTab->nRef++; 39 pTab->nRef++; 40 } 40 } 41 if( sqlite3IndexedByLookup(pParse, pItem) ){ 41 if( sqlite3IndexedByLookup(pParse, pItem) ){ 42 pTab = 0; 42 pTab = 0;

Changes to src/expr.c

926 pNew = sqlite3DbMallocRaw(db, nByte ); 926 pNew = sqlite3DbMallocRaw(db, nByte ); 927 if( pNew==0 ) return 0; 927 if( pNew==0 ) return 0; 928 pNew->nSrc = pNew->nAlloc = p->nSrc; 928 pNew->nSrc = pNew->nAlloc = p->nSrc; 929 for(i=0; i<p->nSrc; i++){ 929 for(i=0; i<p->nSrc; i++){ 930 struct SrcList_item *pNewItem = &pNew->a[i]; 930 struct SrcList_item *pNewItem = &pNew->a[i]; 931 struct SrcList_item *pOldItem = &p->a[i]; 931 struct SrcList_item *pOldItem = &p->a[i]; 932 Table *pTab; 932 Table *pTab; > 933 pNewItem->pSchema = pOldItem->pSchema; 933 pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase); 934 pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase); 934 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName); 935 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName); 935 pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias); 936 pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias); 936 pNewItem->jointype = pOldItem->jointype; 937 pNewItem->jointype = pOldItem->jointype; 937 pNewItem->iCursor = pOldItem->iCursor; 938 pNewItem->iCursor = pOldItem->iCursor; 938 pNewItem->addrFillSub = pOldItem->addrFillSub; 939 pNewItem->addrFillSub = pOldItem->addrFillSub; 939 pNewItem->regReturn = pOldItem->regReturn; 940 pNewItem->regReturn = pOldItem->regReturn;

Changes to src/insert.c

1270 #ifndef SQLITE_OMIT_CHECK 1270 #ifndef SQLITE_OMIT_CHECK 1271 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){ 1271 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){ 1272 ExprList *pCheck = pTab->pCheck; 1272 ExprList *pCheck = pTab->pCheck; 1273 pParse->ckBase = regData; 1273 pParse->ckBase = regData; 1274 onError = overrideError!=OE_Default ? overrideError : OE_Abort; 1274 onError = overrideError!=OE_Default ? overrideError : OE_Abort; 1275 for(i=0; i<pCheck->nExpr; i++){ 1275 for(i=0; i<pCheck->nExpr; i++){ 1276 int allOk = sqlite3VdbeMakeLabel(v); 1276 int allOk = sqlite3VdbeMakeLabel(v); > 1277 Expr *pDup = sqlite3ExprDup(db, pCheck->a[i].pExpr, 0); > 1278 if( pDup==0 ) break; 1277 sqlite3ExprIfTrue(pParse, pCheck->a[i].pExpr, allOk, SQLITE_JUMPIFNULL); | 1279 sqlite3ExprIfTrue(pParse, pDup, allOk, SQLITE_JUMPIFNULL); 1278 if( onError==OE_Ignore ){ 1280 if( onError==OE_Ignore ){ 1279 sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest); 1281 sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest); 1280 }else{ 1282 }else{ 1281 char *zConsName = pCheck->a[i].zName; 1283 char *zConsName = pCheck->a[i].zName; 1282 if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-15569-63625 */ 1284 if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-15569-63625 */ 1283 if( zConsName ){ 1285 if( zConsName ){ 1284 zConsName = sqlite3MPrintf(db, "constraint %s failed", zConsName); 1286 zConsName = sqlite3MPrintf(db, "constraint %s failed", zConsName); 1285 }else{ 1287 }else{ 1286 zConsName = 0; 1288 zConsName = 0; 1287 } 1289 } 1288 sqlite3HaltConstraint(pParse, onError, zConsName, P4_DYNAMIC); 1290 sqlite3HaltConstraint(pParse, onError, zConsName, P4_DYNAMIC); 1289 } 1291 } 1290 sqlite3VdbeResolveLabel(v, allOk); 1292 sqlite3VdbeResolveLabel(v, allOk); > 1293 sqlite3ExprDelete(db, pDup); 1291 } 1294 } 1292 } 1295 } 1293 #endif /* !defined(SQLITE_OMIT_CHECK) */ 1296 #endif /* !defined(SQLITE_OMIT_CHECK) */ 1294 1297 1295 /* If we have an INTEGER PRIMARY KEY, make sure the primary key 1298 /* If we have an INTEGER PRIMARY KEY, make sure the primary key 1296 ** of the new record does not previously exist. Except, if this 1299 ** of the new record does not previously exist. Except, if this 1297 ** is an UPDATE and the primary key is not changing, that is OK. 1300 ** is an UPDATE and the primary key is not changing, that is OK. ................................................................................................................................................................................ 1739 } 1742 } 1740 1743 1741 /* At this point we have established that the statement is of the 1744 /* At this point we have established that the statement is of the 1742 ** correct syntactic form to participate in this optimization. Now 1745 ** correct syntactic form to participate in this optimization. Now 1743 ** we have to check the semantics. 1746 ** we have to check the semantics. 1744 */ 1747 */ 1745 pItem = pSelect->pSrc->a; 1748 pItem = pSelect->pSrc->a; 1746 pSrc = sqlite3LocateTable(pParse, 0, pItem->zName, pItem->zDatabase); | 1749 pSrc = sqlite3LocateTableItem(pParse, 0, pItem); 1747 if( pSrc==0 ){ 1750 if( pSrc==0 ){ 1748 return 0; /* FROM clause does not contain a real table */ 1751 return 0; /* FROM clause does not contain a real table */ 1749 } 1752 } 1750 if( pSrc==pDest ){ 1753 if( pSrc==pDest ){ 1751 return 0; /* tab1 and tab2 may not be the same table */ 1754 return 0; /* tab1 and tab2 may not be the same table */ 1752 } 1755 } 1753 #ifndef SQLITE_OMIT_VIRTUALTABLE 1756 #ifndef SQLITE_OMIT_VIRTUALTABLE

Changes to src/select.c

3328 pTab->iPKey = -1; 3328 pTab->iPKey = -1; 3329 pTab->nRowEst = 1000000; 3329 pTab->nRowEst = 1000000; 3330 pTab->tabFlags |= TF_Ephemeral; 3330 pTab->tabFlags |= TF_Ephemeral; 3331 #endif 3331 #endif 3332 }else{ 3332 }else{ 3333 /* An ordinary table or view name in the FROM clause */ 3333 /* An ordinary table or view name in the FROM clause */ 3334 assert( pFrom->pTab==0 ); 3334 assert( pFrom->pTab==0 ); 3335 pFrom->pTab = pTab = | 3335 pFrom->pTab = pTab = sqlite3LocateTableItem(pParse, 0, pFrom); 3336 sqlite3LocateTable(pParse,0,pFrom->zName,pFrom->zDatabase); < 3337 if( pTab==0 ) return WRC_Abort; 3336 if( pTab==0 ) return WRC_Abort; 3338 pTab->nRef++; 3337 pTab->nRef++; 3339 #if !defined(SQLITE_OMIT_VIEW) || !defined (SQLITE_OMIT_VIRTUALTABLE) 3338 #if !defined(SQLITE_OMIT_VIEW) || !defined (SQLITE_OMIT_VIRTUALTABLE) 3340 if( pTab->pSelect || IsVirtual(pTab) ){ 3339 if( pTab->pSelect || IsVirtual(pTab) ){ 3341 /* We reach here if the named table is a really a view */ 3340 /* We reach here if the named table is a really a view */ 3342 if( sqlite3ViewGetColumnNames(pParse, pTab) ) return WRC_Abort; 3341 if( sqlite3ViewGetColumnNames(pParse, pTab) ) return WRC_Abort; 3343 assert( pFrom->pSelect==0 ); 3342 assert( pFrom->pSelect==0 );

Changes to src/sqliteInt.h

1865 ** In the colUsed field, the high-order bit (bit 63) is set if the table 1865 ** In the colUsed field, the high-order bit (bit 63) is set if the table 1866 ** contains more than 63 columns and the 64-th or later column is used. 1866 ** contains more than 63 columns and the 64-th or later column is used. 1867 */ 1867 */ 1868 struct SrcList { 1868 struct SrcList { 1869 i16 nSrc; /* Number of tables or subqueries in the FROM clause */ 1869 i16 nSrc; /* Number of tables or subqueries in the FROM clause */ 1870 i16 nAlloc; /* Number of entries allocated in a[] below */ 1870 i16 nAlloc; /* Number of entries allocated in a[] below */ 1871 struct SrcList_item { 1871 struct SrcList_item { > 1872 Schema *pSchema; /* Schema to which this item is fixed */ 1872 char *zDatabase; /* Name of database holding this table */ 1873 char *zDatabase; /* Name of database holding this table */ 1873 char *zName; /* Name of the table */ 1874 char *zName; /* Name of the table */ 1874 char *zAlias; /* The "B" part of a "A AS B" phrase. zName is the "A" */ 1875 char *zAlias; /* The "B" part of a "A AS B" phrase. zName is the "A" */ 1875 Table *pTab; /* An SQL table corresponding to zName */ 1876 Table *pTab; /* An SQL table corresponding to zName */ 1876 Select *pSelect; /* A SELECT statement used in place of a table name */ 1877 Select *pSelect; /* A SELECT statement used in place of a table name */ 1877 int addrFillSub; /* Address of subroutine to manifest a subquery */ 1878 int addrFillSub; /* Address of subroutine to manifest a subquery */ 1878 int regReturn; /* Register holding return address of addrFillSub */ 1879 int regReturn; /* Register holding return address of addrFillSub */ ................................................................................................................................................................................ 2433 ** The following structure contains information used by the sqliteFix... 2434 ** The following structure contains information used by the sqliteFix... 2434 ** routines as they walk the parse tree to make database references 2435 ** routines as they walk the parse tree to make database references 2435 ** explicit. 2436 ** explicit. 2436 */ 2437 */ 2437 typedef struct DbFixer DbFixer; 2438 typedef struct DbFixer DbFixer; 2438 struct DbFixer { 2439 struct DbFixer { 2439 Parse *pParse; /* The parsing context. Error messages written here */ 2440 Parse *pParse; /* The parsing context. Error messages written here */ > 2441 Schema *pSchema; /* Fix items to this schema */ 2440 const char *zDb; /* Make sure all objects are contained in this database */ 2442 const char *zDb; /* Make sure all objects are contained in this database */ 2441 const char *zType; /* Type of the container - used for error messages */ 2443 const char *zType; /* Type of the container - used for error messages */ 2442 const Token *pName; /* Name of the container - used for error messages */ 2444 const Token *pName; /* Name of the container - used for error messages */ 2443 }; 2445 }; 2444 2446 2445 /* 2447 /* 2446 ** An objected used to accumulate the text of a string where we 2448 ** An objected used to accumulate the text of a string where we ................................................................................................................................................................................ 2843 int sqlite3ExprCodeAndCache(Parse*, Expr*, int); 2845 int sqlite3ExprCodeAndCache(Parse*, Expr*, int); 2844 void sqlite3ExprCodeConstants(Parse*, Expr*); 2846 void sqlite3ExprCodeConstants(Parse*, Expr*); 2845 int sqlite3ExprCodeExprList(Parse*, ExprList*, int, int); 2847 int sqlite3ExprCodeExprList(Parse*, ExprList*, int, int); 2846 void sqlite3ExprIfTrue(Parse*, Expr*, int, int); 2848 void sqlite3ExprIfTrue(Parse*, Expr*, int, int); 2847 void sqlite3ExprIfFalse(Parse*, Expr*, int, int); 2849 void sqlite3ExprIfFalse(Parse*, Expr*, int, int); 2848 Table *sqlite3FindTable(sqlite3*,const char*, const char*); 2850 Table *sqlite3FindTable(sqlite3*,const char*, const char*); 2849 Table *sqlite3LocateTable(Parse*,int isView,const char*, const char*); 2851 Table *sqlite3LocateTable(Parse*,int isView,const char*, const char*); > 2852 Table *sqlite3LocateTableItem(Parse*,int isView,struct SrcList_item *); 2850 Index *sqlite3FindIndex(sqlite3*,const char*, const char*); 2853 Index *sqlite3FindIndex(sqlite3*,const char*, const char*); 2851 void sqlite3UnlinkAndDeleteTable(sqlite3*,int,const char*); 2854 void sqlite3UnlinkAndDeleteTable(sqlite3*,int,const char*); 2852 void sqlite3UnlinkAndDeleteIndex(sqlite3*,int,const char*); 2855 void sqlite3UnlinkAndDeleteIndex(sqlite3*,int,const char*); 2853 void sqlite3Vacuum(Parse*); 2856 void sqlite3Vacuum(Parse*); 2854 int sqlite3RunVacuum(char**, sqlite3*); 2857 int sqlite3RunVacuum(char**, sqlite3*); 2855 char *sqlite3NameFromToken(sqlite3*, Token*); 2858 char *sqlite3NameFromToken(sqlite3*, Token*); 2856 int sqlite3ExprCompare(Expr*, Expr*); 2859 int sqlite3ExprCompare(Expr*, Expr*);

Changes to src/vtab.c

259 ** in the list are moved to the sqlite3.pDisconnect list of the associated 259 ** in the list are moved to the sqlite3.pDisconnect list of the associated 260 ** database connection. 260 ** database connection. 261 */ 261 */ 262 void sqlite3VtabClear(sqlite3 *db, Table *p){ 262 void sqlite3VtabClear(sqlite3 *db, Table *p){ 263 if( !db || db->pnBytesFreed==0 ) vtabDisconnectAll(0, p); 263 if( !db || db->pnBytesFreed==0 ) vtabDisconnectAll(0, p); 264 if( p->azModuleArg ){ 264 if( p->azModuleArg ){ 265 int i; 265 int i; > 266 assert( p->nModuleArg<2 || p->azModuleArg[1]==0 ); 266 for(i=0; i<p->nModuleArg; i++){ 267 for(i=0; i<p->nModuleArg; i++){ 267 sqlite3DbFree(db, p->azModuleArg[i]); 268 sqlite3DbFree(db, p->azModuleArg[i]); 268 } 269 } 269 sqlite3DbFree(db, p->azModuleArg); 270 sqlite3DbFree(db, p->azModuleArg); 270 } 271 } 271 } 272 } 272 273 ................................................................................................................................................................................ 320 db = pParse->db; 321 db = pParse->db; 321 iDb = sqlite3SchemaToIndex(db, pTable->pSchema); 322 iDb = sqlite3SchemaToIndex(db, pTable->pSchema); 322 assert( iDb>=0 ); 323 assert( iDb>=0 ); 323 324 324 pTable->tabFlags |= TF_Virtual; 325 pTable->tabFlags |= TF_Virtual; 325 pTable->nModuleArg = 0; 326 pTable->nModuleArg = 0; 326 addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName)); 327 addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName)); 327 addModuleArgument(db, pTable, sqlite3DbStrDup(db, db->aDb[iDb].zName)); | 328 addModuleArgument(db, pTable, 0); 328 addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName)); 329 addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName)); 329 pParse->sNameToken.n = (int)(&pModuleName->z[pModuleName->n] - pName1->z); 330 pParse->sNameToken.n = (int)(&pModuleName->z[pModuleName->n] - pName1->z); 330 331 331 #ifndef SQLITE_OMIT_AUTHORIZATION 332 #ifndef SQLITE_OMIT_AUTHORIZATION 332 /* Creating a virtual table invokes the authorization callback twice. 333 /* Creating a virtual table invokes the authorization callback twice. 333 ** The first invocation, to obtain permission to INSERT a row into the 334 ** The first invocation, to obtain permission to INSERT a row into the 334 ** sqlite_master table, has already been made by sqlite3StartTable(). 335 ** sqlite_master table, has already been made by sqlite3StartTable(). ................................................................................................................................................................................ 477 VtabCtx sCtx, *pPriorCtx; 478 VtabCtx sCtx, *pPriorCtx; 478 VTable *pVTable; 479 VTable *pVTable; 479 int rc; 480 int rc; 480 const char *const*azArg = (const char *const*)pTab->azModuleArg; 481 const char *const*azArg = (const char *const*)pTab->azModuleArg; 481 int nArg = pTab->nModuleArg; 482 int nArg = pTab->nModuleArg; 482 char *zErr = 0; 483 char *zErr = 0; 483 char *zModuleName = sqlite3MPrintf(db, "%s", pTab->zName); 484 char *zModuleName = sqlite3MPrintf(db, "%s", pTab->zName); > 485 int iDb; 484 486 485 if( !zModuleName ){ 487 if( !zModuleName ){ 486 return SQLITE_NOMEM; 488 return SQLITE_NOMEM; 487 } 489 } 488 490 489 pVTable = sqlite3DbMallocZero(db, sizeof(VTable)); 491 pVTable = sqlite3DbMallocZero(db, sizeof(VTable)); 490 if( !pVTable ){ 492 if( !pVTable ){ 491 sqlite3DbFree(db, zModuleName); 493 sqlite3DbFree(db, zModuleName); 492 return SQLITE_NOMEM; 494 return SQLITE_NOMEM; 493 } 495 } 494 pVTable->db = db; 496 pVTable->db = db; 495 pVTable->pMod = pMod; 497 pVTable->pMod = pMod; 496 498 > 499 assert( pTab->azModuleArg[1]==0 ); > 500 iDb = sqlite3SchemaToIndex(db, pTab->pSchema); > 501 pTab->azModuleArg[1] = db->aDb[iDb].zName; > 502 497 /* Invoke the virtual table constructor */ 503 /* Invoke the virtual table constructor */ 498 assert( &db->pVtabCtx ); 504 assert( &db->pVtabCtx ); 499 assert( xConstruct ); 505 assert( xConstruct ); 500 sCtx.pTab = pTab; 506 sCtx.pTab = pTab; 501 sCtx.pVTable = pVTable; 507 sCtx.pVTable = pVTable; 502 pPriorCtx = db->pVtabCtx; 508 pPriorCtx = db->pVtabCtx; 503 db->pVtabCtx = &sCtx; 509 db->pVtabCtx = &sCtx; 504 rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr); 510 rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr); 505 db->pVtabCtx = pPriorCtx; 511 db->pVtabCtx = pPriorCtx; 506 if( rc==SQLITE_NOMEM ) db->mallocFailed = 1; 512 if( rc==SQLITE_NOMEM ) db->mallocFailed = 1; > 513 pTab->azModuleArg[1] = 0; 507 514 508 if( SQLITE_OK!=rc ){ 515 if( SQLITE_OK!=rc ){ 509 if( zErr==0 ){ 516 if( zErr==0 ){ 510 *pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName); 517 *pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName); 511 }else { 518 }else { 512 *pzErr = sqlite3MPrintf(db, "%s", zErr); 519 *pzErr = sqlite3MPrintf(db, "%s", zErr); 513 sqlite3_free(zErr); 520 sqlite3_free(zErr);

Added test/shared9.test

> 1 # 2012 October 5 > 2 # > 3 # The author disclaims copyright to this source code. In place of > 4 # a legal notice, here is a blessing: > 5 # > 6 # May you do good and not evil. > 7 # May you find forgiveness for yourself and forgive others. > 8 # May you share freely, never taking more than you give. > 9 # > 10 #*********************************************************************** > 11 # > 12 # The tests in this file are intended to show if two connections attach > 13 # to the same shared cache using different database names, views and > 14 # virtual tables may still be accessed. > 15 # > 16 > 17 set testdir [file dirname $argv0] > 18 source $testdir/tester.tcl > 19 set testprefix shared9 > 20 > 21 ifcapable !view||!trigger { > 22 finish_test > 23 return > 24 } > 25 > 26 db close > 27 set enable_shared_cache [sqlite3_enable_shared_cache 1] > 28 > 29 sqlite3 db1 test.db > 30 sqlite3 db2 test.db > 31 forcedelete test.db2 > 32 > 33 do_test 1.1 { > 34 db1 eval { > 35 ATTACH 'test.db2' AS 'fred'; > 36 CREATE TABLE fred.t1(a, b, c); > 37 CREATE VIEW fred.v1 AS SELECT * FROM t1; > 38 > 39 CREATE TABLE fred.t2(a, b); > 40 CREATE TABLE fred.t3(a, b); > 41 CREATE TRIGGER fred.trig AFTER INSERT ON t2 BEGIN > 42 DELETE FROM t3; > 43 INSERT INTO t3 SELECT * FROM t2; > 44 END; > 45 INSERT INTO t2 VALUES(1, 2); > 46 SELECT * FROM t3; > 47 } > 48 } {1 2} > 49 > 50 do_test 1.2 { db2 eval "ATTACH 'test.db2' AS 'jones'" } {} > 51 do_test 1.3 { db2 eval "SELECT * FROM v1" } {} > 52 do_test 1.4 { db2 eval "INSERT INTO t2 VALUES(3, 4)" } {} > 53 > 54 ifcapable fts3 { > 55 do_test 1.5 { > 56 db1 eval { > 57 CREATE VIRTUAL TABLE fred.t4 USING fts4; > 58 INSERT INTO t4 VALUES('hello world'); > 59 } > 60 } {} > 61 > 62 do_test 1.6 { > 63 db2 eval { > 64 INSERT INTO t4 VALUES('shared cache'); > 65 SELECT * FROM t4 WHERE t4 MATCH 'hello'; > 66 } > 67 } {{hello world}} > 68 > 69 do_test 1.7 { > 70 db1 eval { > 71 SELECT * FROM t4 WHERE t4 MATCH 'c*'; > 72 } > 73 } {{shared cache}} > 74 } > 75 > 76 db1 close > 77 db2 close > 78 > 79 #------------------------------------------------------------------------- > 80 # The following tests attempt to find a similar problem with collation > 81 # sequence names - pointers to database handle specific allocations leaking > 82 # into schema objects and being used after the original handle has been > 83 # closed. > 84 # > 85 forcedelete test.db test.db2 > 86 sqlite3 db1 test.db > 87 sqlite3 db2 test.db > 88 foreach x {collate1 collate2 collate3} { > 89 proc $x {a b} { string compare $a $b } > 90 db1 collate $x $x > 91 db2 collate $x $x > 92 } > 93 do_test 2.1 { > 94 db1 eval { > 95 CREATE TABLE t1(a, b, c COLLATE collate1); > 96 CREATE INDEX i1 ON t1(a COLLATE collate2, c, b); > 97 } > 98 } {} > 99 do_test 2.2 { > 100 db1 close > 101 db2 eval "INSERT INTO t1 VALUES('abc', 'def', 'ghi')" > 102 } {} > 103 db2 close > 104 > 105 #------------------------------------------------------------------------- > 106 # At one point, the following would cause a collation sequence belonging > 107 # to connection [db1] to be invoked by a call to [db2 eval]. Which is a > 108 # problem if [db1] has already been closed. > 109 # > 110 forcedelete test.db test.db2 > 111 sqlite3 db1 test.db > 112 sqlite3 db2 test.db > 113 > 114 proc mycollate_db1 {a b} {set ::invoked_mycollate_db1 1 ; string compare $a $b} > 115 proc mycollate_db2 {a b} {string compare $a $b} > 116 > 117 db1 collate mycollate mycollate_db1 > 118 db2 collate mycollate mycollate_db2 > 119 > 120 do_test 2.3 { > 121 set ::invoked_mycollate_db1 0 > 122 db1 eval { > 123 CREATE TABLE t1(a COLLATE mycollate, CHECK (a IN ('one', 'two', 'three'))); > 124 INSERT INTO t1 VALUES('one'); > 125 } > 126 db1 close > 127 set ::invoked_mycollate_db1 > 128 } {1} > 129 do_test 2.4 { > 130 set ::invoked_mycollate_db1 0 > 131 db2 eval { > 132 INSERT INTO t1 VALUES('two'); > 133 } > 134 db2 close > 135 set ::invoked_mycollate_db1 > 136 } {0} > 137 > 138 sqlite3_enable_shared_cache $::enable_shared_cache > 139 finish_test > 140

Changes to test/trigger1.test

206 do_test trigger1-1.12 { 206 do_test trigger1-1.12 { 207 catchsql { 207 catchsql { 208 create table t1(a,b); 208 create table t1(a,b); 209 create trigger t1t instead of update on t1 for each row begin 209 create trigger t1t instead of update on t1 for each row begin 210 delete from t1 WHERE a=old.a+2; 210 delete from t1 WHERE a=old.a+2; 211 end; 211 end; 212 } 212 } 213 } {1 {cannot create INSTEAD OF trigger on table: main.t1}} | 213 } {1 {cannot create INSTEAD OF trigger on table: t1}} 214 214 215 ifcapable view { 215 ifcapable view { 216 # Ensure that we cannot create BEFORE triggers on views 216 # Ensure that we cannot create BEFORE triggers on views 217 do_test trigger1-1.13 { 217 do_test trigger1-1.13 { 218 catchsql { 218 catchsql { 219 create view v1 as select * from t1; 219 create view v1 as select * from t1; 220 create trigger v1t before update on v1 for each row begin 220 create trigger v1t before update on v1 for each row begin 221 delete from t1 WHERE a=old.a+2; 221 delete from t1 WHERE a=old.a+2; 222 end; 222 end; 223 } 223 } 224 } {1 {cannot create BEFORE trigger on view: main.v1}} | 224 } {1 {cannot create BEFORE trigger on view: v1}} 225 # Ensure that we cannot create AFTER triggers on views 225 # Ensure that we cannot create AFTER triggers on views 226 do_test trigger1-1.14 { 226 do_test trigger1-1.14 { 227 catchsql { 227 catchsql { 228 drop view v1; 228 drop view v1; 229 create view v1 as select * from t1; 229 create view v1 as select * from t1; 230 create trigger v1t AFTER update on v1 for each row begin 230 create trigger v1t AFTER update on v1 for each row begin 231 delete from t1 WHERE a=old.a+2; 231 delete from t1 WHERE a=old.a+2; 232 end; 232 end; 233 } 233 } 234 } {1 {cannot create AFTER trigger on view: main.v1}} | 234 } {1 {cannot create AFTER trigger on view: v1}} 235 } ;# ifcapable view 235 } ;# ifcapable view 236 236 237 # Check for memory leaks in the trigger parser 237 # Check for memory leaks in the trigger parser 238 # 238 # 239 do_test trigger1-2.1 { 239 do_test trigger1-2.1 { 240 catchsql { 240 catchsql { 241 CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN 241 CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN