/ Check-in [8ac75b8a88]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Improve error messages caused by corrupt database schemas in OPEN_SHARED_SCHEMA mode.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | reuse-schema
Files: files | file ages | folders
SHA3-256: 8ac75b8a880447ea67cf7646fc5af1379ce35861f396634119d7381e1dde404a
User & Date: dan 2019-02-19 18:00:28
Wiki:reuse-schema
Context
2019-02-20
17:36
Add test and fixes for SQLITE_OPEN_SHARED_SCHEMA mode. check-in: 9a78d89c84 user: dan tags: reuse-schema
2019-02-19
18:00
Improve error messages caused by corrupt database schemas in OPEN_SHARED_SCHEMA mode. check-in: 8ac75b8a88 user: dan tags: reuse-schema
2019-02-18
18:16
Ensure that creating temp schema items does not cause an OPEN_SHARABLE_SCHEMA connection to load all schemas into memory. check-in: 88cbf54eee user: dan tags: reuse-schema
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/alter.c.

   120    120     /* Get a NULL terminated version of the new table name. */
   121    121     zName = sqlite3NameFromToken(db, pName);
   122    122     if( !zName ) goto exit_rename_table;
   123    123   
   124    124     /* Check that a table or index named 'zName' does not already exist
   125    125     ** in database iDb. If so, this is an error.
   126    126     */
   127         -  if( sqlite3FindTable(0, db, zName, zDb) || sqlite3FindIndex(db, zName, zDb) ){
          127  +  if( sqlite3FindTable(db, zName, zDb) || sqlite3FindIndex(db, zName, zDb) ){
   128    128       sqlite3ErrorMsg(pParse, 
   129    129           "there is already another table or index with this name: %s", zName);
   130    130       goto exit_rename_table;
   131    131     }
   132    132   
   133    133     /* Make sure it is not a system table being altered, or a reserved name
   134    134     ** that the table is being renamed to.
................................................................................
   207    207         nTabName, zTabName
   208    208     );
   209    209   
   210    210   #ifndef SQLITE_OMIT_AUTOINCREMENT
   211    211     /* If the sqlite_sequence table exists in this database, then update 
   212    212     ** it with the new table name.
   213    213     */
   214         -  if( sqlite3FindTable(0, db, "sqlite_sequence", zDb) ){
          214  +  if( sqlite3FindTable(db, "sqlite_sequence", zDb) ){
   215    215       sqlite3NestedParse(pParse,
   216    216           "UPDATE \"%w\".sqlite_sequence set name = %Q WHERE name = %Q",
   217    217           zDb, zName, pTab->zName);
   218    218     }
   219    219   #endif
   220    220   
   221    221     /* If the table being renamed is not itself part of the temp database,
................................................................................
   284    284   
   285    285     assert( sqlite3BtreeHoldsAllMutexes(db) );
   286    286     iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
   287    287     zDb = db->aDb[iDb].zDbSName;
   288    288     zTab = &pNew->zName[16];  /* Skip the "sqlite_altertab_" prefix on the name */
   289    289     pCol = &pNew->aCol[pNew->nCol-1];
   290    290     pDflt = pCol->pDflt;
   291         -  pTab = sqlite3FindTable(0, db, zTab, zDb);
          291  +  pTab = sqlite3FindTable(db, zTab, zDb);
   292    292     assert( pTab );
   293    293   
   294    294   #ifndef SQLITE_OMIT_AUTHORIZATION
   295    295     /* Invoke the authorization callback. */
   296    296     if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
   297    297       return;
   298    298     }
................................................................................
  1082   1082     TriggerStep *pStep;
  1083   1083     NameContext sNC;
  1084   1084     int rc = SQLITE_OK;
  1085   1085   
  1086   1086     memset(&sNC, 0, sizeof(sNC));
  1087   1087     sNC.pParse = pParse;
  1088   1088     assert( pNew->pTabSchema );
  1089         -  pParse->pTriggerTab = sqlite3FindTable(0, db, pNew->table, 
         1089  +  pParse->pTriggerTab = sqlite3FindTable(db, pNew->table, 
  1090   1090         db->aDb[sqlite3SchemaToIndex(db, pNew->pTabSchema)].zDbSName
  1091   1091     );
  1092   1092     pParse->eTriggerOp = pNew->op;
  1093   1093     /* ALWAYS() because if the table of the trigger does not exist, the
  1094   1094     ** error would have been hit before this point */
  1095   1095     if( ALWAYS(pParse->pTriggerTab) ){
  1096   1096       rc = sqlite3ViewGetColumnNames(pParse, pParse->pTriggerTab);
................................................................................
  1246   1246   
  1247   1247     UNUSED_PARAMETER(NotUsed);
  1248   1248     if( zSql==0 ) return;
  1249   1249     if( zTable==0 ) return;
  1250   1250     if( zNew==0 ) return;
  1251   1251     if( iCol<0 ) return;
  1252   1252     sqlite3BtreeEnterAll(db);
  1253         -  pTab = sqlite3FindTable(0, db, zTable, zDb);
         1253  +  pTab = sqlite3FindTable(db, zTable, zDb);
  1254   1254     if( pTab==0 || iCol>=pTab->nCol ){
  1255   1255       sqlite3BtreeLeaveAll(db);
  1256   1256       return;
  1257   1257     }
  1258   1258     zOld = pTab->aCol[iCol].zName;
  1259   1259     memset(&sCtx, 0, sizeof(sCtx));
  1260   1260     sCtx.iCol = ((iCol==pTab->iPKey) ? -1 : iCol);
................................................................................
  1448   1448       sqlite3_xauth xAuth = db->xAuth;
  1449   1449       db->xAuth = 0;
  1450   1450   #endif
  1451   1451   
  1452   1452       sqlite3BtreeEnterAll(db);
  1453   1453   
  1454   1454       memset(&sCtx, 0, sizeof(RenameCtx));
  1455         -    sCtx.pTab = sqlite3FindTable(0, db, zOld, zDb);
         1455  +    sCtx.pTab = sqlite3FindTable(db, zOld, zDb);
  1456   1456       memset(&sWalker, 0, sizeof(Walker));
  1457   1457       sWalker.pParse = &sParse;
  1458   1458       sWalker.xExprCallback = renameTableExprCb;
  1459   1459       sWalker.xSelectCallback = renameTableSelectCb;
  1460   1460       sWalker.u.pRename = &sCtx;
  1461   1461   
  1462   1462       rc = renameParseSql(&sParse, zDb, 1, db, zInput, bTemp);

Changes to src/analyze.c.

   206    206   
   207    207     /* Create new statistic tables if they do not exist, or clear them
   208    208     ** if they do already exist.
   209    209     */
   210    210     for(i=0; i<ArraySize(aTable); i++){
   211    211       const char *zTab = aTable[i].zName;
   212    212       Table *pStat;
   213         -    if( (pStat = sqlite3FindTable(0, db, zTab, pDb->zDbSName))==0 ){
          213  +    if( (pStat = sqlite3FindTable(db, zTab, pDb->zDbSName))==0 ){
   214    214         if( aTable[i].zCols ){
   215    215           /* The sqlite_statN table does not exist. Create it. Note that a 
   216    216           ** side-effect of the CREATE TABLE statement is to leave the rootpage 
   217    217           ** of the new table in register pParse->regRoot. This is important 
   218    218           ** because the OpenWrite opcode below will be needing it. */
   219    219           sqlite3SchemaWritable(pParse, iDb);
   220    220           sqlite3NestedParse(pParse,
................................................................................
  1532   1532   
  1533   1533     assert( argc==3 );
  1534   1534     UNUSED_PARAMETER2(NotUsed, argc);
  1535   1535   
  1536   1536     if( argv==0 || argv[0]==0 || argv[2]==0 ){
  1537   1537       return 0;
  1538   1538     }
  1539         -  pTable = sqlite3FindTable(0, pInfo->db, argv[0], pInfo->zDatabase);
         1539  +  pTable = sqlite3FindTable(pInfo->db, argv[0], pInfo->zDatabase);
  1540   1540     if( pTable==0 ){
  1541   1541       return 0;
  1542   1542     }
  1543   1543     if( argv[1]==0 ){
  1544   1544       pIndex = 0;
  1545   1545     }else if( sqlite3_stricmp(argv[0],argv[1])==0 ){
  1546   1546       pIndex = sqlite3PrimaryKeyIndex(pTable);
................................................................................
  1674   1674   static Index *findIndexOrPrimaryKey(
  1675   1675     sqlite3 *db,
  1676   1676     const char *zName,
  1677   1677     const char *zDb
  1678   1678   ){
  1679   1679     Index *pIdx = sqlite3FindIndex(db, zName, zDb);
  1680   1680     if( pIdx==0 ){
  1681         -    Table *pTab = sqlite3FindTable(0, db, zName, zDb);
         1681  +    Table *pTab = sqlite3FindTable(db, zName, zDb);
  1682   1682       if( pTab && !HasRowid(pTab) ) pIdx = sqlite3PrimaryKeyIndex(pTab);
  1683   1683     }
  1684   1684     return pIdx;
  1685   1685   }
  1686   1686   
  1687   1687   /*
  1688   1688   ** Load the content from either the sqlite_stat4 or sqlite_stat3 table 
................................................................................
  1823   1823   ** Load content from the sqlite_stat4 and sqlite_stat3 tables into 
  1824   1824   ** the Index.aSample[] arrays of all indices.
  1825   1825   */
  1826   1826   static int loadStat4(sqlite3 *db, const char *zDb){
  1827   1827     int rc = SQLITE_OK;             /* Result codes from subroutines */
  1828   1828   
  1829   1829     assert( db->lookaside.bDisable );
  1830         -  if( sqlite3FindTable(0, db, "sqlite_stat4", zDb) ){
         1830  +  if( sqlite3FindTable(db, "sqlite_stat4", zDb) ){
  1831   1831       rc = loadStatTbl(db, 0,
  1832   1832         "SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx", 
  1833   1833         "SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4",
  1834   1834         zDb
  1835   1835       );
  1836   1836     }
  1837   1837   
  1838         -  if( rc==SQLITE_OK && sqlite3FindTable(0, db, "sqlite_stat3", zDb) ){
         1838  +  if( rc==SQLITE_OK && sqlite3FindTable(db, "sqlite_stat3", zDb) ){
  1839   1839       rc = loadStatTbl(db, 1,
  1840   1840         "SELECT idx,count(*) FROM %Q.sqlite_stat3 GROUP BY idx", 
  1841   1841         "SELECT idx,neq,nlt,ndlt,sqlite_record(sample) FROM %Q.sqlite_stat3",
  1842   1842         zDb
  1843   1843       );
  1844   1844     }
  1845   1845   
................................................................................
  1891   1891       pIdx->aSample = 0;
  1892   1892   #endif
  1893   1893     }
  1894   1894   
  1895   1895     /* Load new statistics out of the sqlite_stat1 table */
  1896   1896     sInfo.db = db;
  1897   1897     sInfo.zDatabase = db->aDb[iDb].zDbSName;
  1898         -  if( sqlite3FindTable(0, db, "sqlite_stat1", sInfo.zDatabase)!=0 ){
         1898  +  if( sqlite3FindTable(db, "sqlite_stat1", sInfo.zDatabase)!=0 ){
  1899   1899       zSql = sqlite3MPrintf(db, 
  1900   1900           "SELECT tbl,idx,stat FROM %Q.sqlite_stat1", sInfo.zDatabase);
  1901   1901       if( zSql==0 ){
  1902   1902         rc = SQLITE_NOMEM_BKPT;
  1903   1903       }else{
  1904   1904         rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
  1905   1905         sqlite3DbFree(db, zSql);

Changes to src/build.c.

   317    317   ** If zDatabase is 0, all databases are searched for the table and the
   318    318   ** first matching table is returned.  (No checking for duplicate table
   319    319   ** names is done.)  The search order is TEMP first, then MAIN, then any
   320    320   ** auxiliary databases added using the ATTACH command.
   321    321   **
   322    322   ** See also sqlite3LocateTable().
   323    323   */
   324         -Table *sqlite3FindTable(
   325         -  Parse *pParse, 
   326         -  sqlite3 *db, 
   327         -  const char *zName, 
   328         -  const char *zDatabase
   329         -){
          324  +Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){
   330    325     Table *p = 0;
   331    326     int i;
   332    327   
   333    328     /* All mutexes are required for schema access.  Make sure we hold them. */
   334    329     assert( zDatabase!=0 || sqlite3BtreeHoldsAllMutexes(db) );
   335    330   #if SQLITE_USER_AUTHENTICATION
   336    331     /* Only the admin user is allowed to know that the sqlite_user table
................................................................................
   341    336   #endif
   342    337     while(1){
   343    338       for(i=OMIT_TEMPDB; i<db->nDb; i++){
   344    339         int j = (i<2) ? i^1 : i;   /* Search TEMP before MAIN */
   345    340         if( zDatabase==0 || sqlite3StrICmp(zDatabase, db->aDb[j].zDbSName)==0 ){
   346    341           int bUnload = 0;
   347    342           assert( sqlite3SchemaMutexHeld(db, j, 0) );
   348         -        if( IsReuseSchema(db) && pParse && pParse->nErr==0 ){
   349         -          pParse->rc = sqlite3SchemaLoad(db, j, &bUnload, &pParse->zErrMsg);
   350         -          if( pParse->rc ) pParse->nErr++;
          343  +        if( IsReuseSchema(db) ){
          344  +          Parse *pParse = db->pParse;
          345  +          if( pParse && pParse->nErr==0 ){
          346  +            pParse->rc = sqlite3SchemaLoad(db, j, &bUnload, &pParse->zErrMsg);
          347  +            if( pParse->rc ) pParse->nErr++;
          348  +          }
   351    349           }
   352    350           p = sqlite3HashFind(&db->aDb[j].pSchema->tblHash, zName);
   353    351           if( p ) return p;
   354    352           if( bUnload ){
   355    353             sqlite3SchemaRelease(db, j);
   356    354           }
   357    355         }
................................................................................
   389    387     if( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0 
   390    388      && !IsReuseSchema(db)
   391    389      && SQLITE_OK!=sqlite3ReadSchema(pParse)
   392    390     ){
   393    391       return 0;
   394    392     }
   395    393   
   396         -  p = sqlite3FindTable(pParse, db, zName, zDbase);
          394  +  p = sqlite3FindTable(db, zName, zDbase);
   397    395     if( p==0 ){
   398    396   #ifndef SQLITE_OMIT_VIRTUALTABLE
   399    397       /* If zName is the not the name of a table in the schema created using
   400    398       ** CREATE, then check to see if it is the name of an virtual table that
   401    399       ** can be an eponymous virtual table. */
   402    400       if( pParse->disableVtab==0 ){
   403    401         Module *pMod = (Module*)sqlite3HashFind(&db->aModule, zName);
................................................................................
   421    419   #endif
   422    420       if( flags & LOCATE_NOERR ) return 0;
   423    421       pParse->checkSchema = 1;
   424    422     }else if( IsVirtual(p) && pParse->disableVtab ){
   425    423       p = 0;
   426    424     }
   427    425   
   428         -  if( p==0 ){
          426  +  if( p==0 && pParse->nErr==0 ){
   429    427       const char *zMsg = flags & LOCATE_VIEW ? "no such view" : "no such table";
   430    428       if( zDbase ){
   431    429         sqlite3ErrorMsg(pParse, "%s: %s.%s", zMsg, zDbase, zName);
   432    430       }else{
   433    431         sqlite3ErrorMsg(pParse, "%s: %s", zMsg, zName);
   434    432       }
   435    433     }
................................................................................
   991    989     ** collisions.
   992    990     */
   993    991     if( !IN_SPECIAL_PARSE ){
   994    992       char *zDb = db->aDb[iDb].zDbSName;
   995    993       if( !IsReuseSchema(db) && SQLITE_OK!=sqlite3ReadSchema(pParse) ){
   996    994         goto begin_table_error;
   997    995       }
   998         -    pTable = sqlite3FindTable(pParse, db, zName, zDb);
          996  +    pTable = sqlite3FindTable(db, zName, zDb);
   999    997       if( pTable ){
  1000    998         if( !noErr ){
  1001    999           sqlite3ErrorMsg(pParse, "table %T already exists", pName);
  1002   1000         }else{
  1003   1001           assert( !db->init.busy || CORRUPT_DB );
  1004   1002           sqlite3CodeVerifySchema(pParse, iDb);
  1005   1003         }
................................................................................
  1963   1961     char *zTail;                  /* Pointer to the last "_" in zName */
  1964   1962     Table *pTab;                  /* Table that zName is a shadow of */
  1965   1963     Module *pMod;                 /* Module for the virtual table */
  1966   1964   
  1967   1965     zTail = strrchr(zName, '_');
  1968   1966     if( zTail==0 ) return 0;
  1969   1967     *zTail = 0;
  1970         -  pTab = sqlite3FindTable(0, db, zName, 0);
         1968  +  pTab = sqlite3FindTable(db, zName, 0);
  1971   1969     *zTail = '_';
  1972   1970     if( pTab==0 ) return 0;
  1973   1971     if( !IsVirtual(pTab) ) return 0;
  1974   1972     pMod = (Module*)sqlite3HashFind(&db->aModule, pTab->azModuleArg[0]);
  1975   1973     if( pMod==0 ) return 0;
  1976   1974     if( pMod->pModule->iVersion<3 ) return 0;
  1977   1975     if( pMod->pModule->xShadowName==0 ) return 0;
................................................................................
  2613   2611     const char *zName      /* Name of index or table */
  2614   2612   ){
  2615   2613     int i;
  2616   2614     const char *zDbName = pParse->db->aDb[iDb].zDbSName;
  2617   2615     for(i=1; i<=4; i++){
  2618   2616       char zTab[24];
  2619   2617       sqlite3_snprintf(sizeof(zTab),zTab,"sqlite_stat%d",i);
  2620         -    if( sqlite3FindTable(0, pParse->db, zTab, zDbName) ){
         2618  +    if( sqlite3FindTable(pParse->db, zTab, zDbName) ){
  2621   2619         sqlite3NestedParse(pParse,
  2622   2620           "DELETE FROM %Q.%s WHERE %s=%Q",
  2623   2621           zDbName, zTab, zType, zName
  2624   2622         );
  2625   2623       }
  2626   2624     }
  2627   2625   }
................................................................................
  3238   3236       if( zName==0 ) goto exit_create_index;
  3239   3237       assert( pName->z!=0 );
  3240   3238       if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
  3241   3239         goto exit_create_index;
  3242   3240       }
  3243   3241       if( !IN_RENAME_OBJECT ){
  3244   3242         if( !db->init.busy ){
  3245         -        if( sqlite3FindTable(0, db, zName, 0)!=0 ){
         3243  +        if( sqlite3FindTable(db, zName, 0)!=0 ){
  3246   3244             sqlite3ErrorMsg(pParse, "there is already a table named %s", zName);
  3247   3245             goto exit_create_index;
  3248   3246           }
  3249   3247         }
  3250   3248         if( sqlite3FindIndex(db, zName, pDb->zDbSName)!=0 ){
  3251   3249           if( !ifNotExist ){
  3252   3250             sqlite3ErrorMsg(pParse, "index %s already exists", zName);
................................................................................
  4592   4590       sqlite3DbFree(db, zColl);
  4593   4591     }
  4594   4592     iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pObjName);
  4595   4593     if( iDb<0 ) return;
  4596   4594     z = sqlite3NameFromToken(db, pObjName);
  4597   4595     if( z==0 ) return;
  4598   4596     zDb = db->aDb[iDb].zDbSName;
  4599         -  pTab = sqlite3FindTable(0, db, z, zDb);
         4597  +  pTab = sqlite3FindTable(db, z, zDb);
  4600   4598     if( pTab ){
  4601   4599       reindexTable(pParse, pTab, 0);
  4602   4600       sqlite3DbFree(db, z);
  4603   4601       return;
  4604   4602     }
  4605   4603     pIndex = sqlite3FindIndex(db, z, zDb);
  4606   4604     sqlite3DbFree(db, z);

Changes to src/fkey.c.

   904    904       }
   905    905   
   906    906       /* Find the parent table of this foreign key. Also find a unique index 
   907    907       ** on the parent key columns in the parent table. If either of these 
   908    908       ** schema items cannot be located, set an error in pParse and return 
   909    909       ** early.  */
   910    910       if( pParse->disableTriggers ){
   911         -      pTo = sqlite3FindTable(0, db, pFKey->zTo, zDb);
          911  +      pTo = sqlite3FindTable(db, pFKey->zTo, zDb);
   912    912       }else{
   913    913         pTo = sqlite3LocateTable(pParse, 0, pFKey->zTo, zDb);
   914    914       }
   915    915       if( !pTo || sqlite3FkLocateIndex(pParse, pTo, pFKey, &pIdx, &aiFree) ){
   916    916         assert( isIgnoreErrors==0 || (regOld!=0 && regNew==0) );
   917    917         if( !isIgnoreErrors || db->mallocFailed ) return;
   918    918         if( pTo==0 ){

Changes to src/main.c.

  3629   3629     if( IsReuseSchema(db)==0 ){
  3630   3630       rc = sqlite3Init(db, &zErrMsg);
  3631   3631     }
  3632   3632   
  3633   3633     /* Locate the table in question */
  3634   3634     if( rc==SQLITE_OK ){
  3635   3635       Parse sParse;                   /* Fake Parse object for FindTable */
         3636  +    Parse *pSaved = db->pParse;
  3636   3637       memset(&sParse, 0, sizeof(sParse));
  3637         -    pTab = sqlite3FindTable(&sParse, db, zTableName, zDbName);
         3638  +    db->pParse = &sParse;
         3639  +    pTab = sqlite3FindTable(db, zTableName, zDbName);
  3638   3640       sqlite3_free(sParse.zErrMsg);
  3639   3641       rc = sParse.rc;
         3642  +    db->pParse = pSaved;
  3640   3643     }
  3641   3644     if( SQLITE_OK!=rc ) goto error_out;
  3642   3645   
  3643   3646     if( !pTab || pTab->pSelect ){
  3644   3647       pTab = 0;
  3645   3648       goto error_out;
  3646   3649     }

Changes to src/pragma.c.

  1184   1184     }
  1185   1185     break;
  1186   1186   
  1187   1187     case PragTyp_INDEX_LIST: if( zRight ){
  1188   1188       Index *pIdx;
  1189   1189       Table *pTab;
  1190   1190       int i;
  1191         -    pTab = sqlite3FindTable(0, db, zRight, zDb);
         1191  +    pTab = sqlite3FindTable(db, zRight, zDb);
  1192   1192       if( pTab ){
  1193   1193         int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
  1194   1194         pParse->nMem = 5;
  1195   1195         sqlite3CodeVerifySchema(pParse, iTabDb);
  1196   1196         for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
  1197   1197           const char *azOrigin[] = { "c", "u", "pk" };
  1198   1198           sqlite3VdbeMultiLoad(v, 1, "isisi",
................................................................................
  1273   1273   
  1274   1274   #endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
  1275   1275   
  1276   1276   #ifndef SQLITE_OMIT_FOREIGN_KEY
  1277   1277     case PragTyp_FOREIGN_KEY_LIST: if( zRight ){
  1278   1278       FKey *pFK;
  1279   1279       Table *pTab;
  1280         -    pTab = sqlite3FindTable(0, db, zRight, zDb);
         1280  +    pTab = sqlite3FindTable(db, zRight, zDb);
  1281   1281       if( pTab ){
  1282   1282         pFK = pTab->pFKey;
  1283   1283         if( pFK ){
  1284   1284           int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
  1285   1285           int i = 0; 
  1286   1286           pParse->nMem = 8;
  1287   1287           sqlite3CodeVerifySchema(pParse, iTabDb);
................................................................................
  1343   1343         iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
  1344   1344         sqlite3CodeVerifySchema(pParse, iTabDb);
  1345   1345         sqlite3TableLock(pParse, iTabDb, pTab->tnum, 0, pTab->zName);
  1346   1346         if( pTab->nCol+regRow>pParse->nMem ) pParse->nMem = pTab->nCol + regRow;
  1347   1347         sqlite3OpenTable(pParse, 0, iTabDb, pTab, OP_OpenRead);
  1348   1348         sqlite3VdbeLoadString(v, regResult, pTab->zName);
  1349   1349         for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
  1350         -        pParent = sqlite3FindTable(0, db, pFK->zTo, zDb);
         1350  +        pParent = sqlite3FindTable(db, pFK->zTo, zDb);
  1351   1351           if( pParent==0 ) continue;
  1352   1352           pIdx = 0;
  1353   1353           sqlite3TableLock(pParse, iTabDb, pParent->tnum, 0, pParent->zName);
  1354   1354           x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, 0);
  1355   1355           if( x==0 ){
  1356   1356             if( pIdx==0 ){
  1357   1357               sqlite3OpenTable(pParse, i, iTabDb, pParent, OP_OpenRead);
................................................................................
  1365   1365           }
  1366   1366         }
  1367   1367         assert( pParse->nErr>0 || pFK==0 );
  1368   1368         if( pFK ) break;
  1369   1369         if( pParse->nTab<i ) pParse->nTab = i;
  1370   1370         addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, 0); VdbeCoverage(v);
  1371   1371         for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
  1372         -        pParent = sqlite3FindTable(0, db, pFK->zTo, zDb);
         1372  +        pParent = sqlite3FindTable(db, pFK->zTo, zDb);
  1373   1373           pIdx = 0;
  1374   1374           aiCols = 0;
  1375   1375           if( pParent ){
  1376   1376             x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, &aiCols);
  1377   1377             assert( x==0 );
  1378   1378           }
  1379   1379           addrOk = sqlite3VdbeMakeLabel(pParse);

Changes to src/prepare.c.

    30     30     }else if( pData->pzErrMsg[0]!=0 ){
    31     31       /* A error message has already been generated.  Do not overwrite it */
    32     32     }else if( pData->mInitFlags & INITFLAG_AlterTable ){
    33     33       *pData->pzErrMsg = sqlite3DbStrDup(db, zExtra);
    34     34       pData->rc = SQLITE_ERROR;
    35     35     }else if( db->flags & SQLITE_WriteSchema ){
    36     36       pData->rc = SQLITE_CORRUPT_BKPT;
           37  +  }else if( IsReuseSchema(db) 
           38  +         && 0==sqlite3StrNICmp(zExtra, "malformed database schema", 17)
           39  +  ){
           40  +    pData->rc = SQLITE_CORRUPT_BKPT;
           41  +    *pData->pzErrMsg = sqlite3DbStrDup(db, zExtra);
    37     42     }else{
    38     43       char *z;
    39     44       if( zObj==0 ) zObj = "?";
    40     45       z = sqlite3MPrintf(db, "malformed database schema (%s)", zObj);
    41     46       if( zExtra && zExtra[0] ) z = sqlite3MPrintf(db, "%z - %s", z, zExtra);
    42     47       *pData->pzErrMsg = z;
    43     48       pData->rc = SQLITE_CORRUPT_BKPT;

Changes to src/sqliteInt.h.

  4009   4009   #define SQLITE_ECEL_DUP      0x01  /* Deep, not shallow copies */
  4010   4010   #define SQLITE_ECEL_FACTOR   0x02  /* Factor out constant terms */
  4011   4011   #define SQLITE_ECEL_REF      0x04  /* Use ExprList.u.x.iOrderByCol */
  4012   4012   #define SQLITE_ECEL_OMITREF  0x08  /* Omit if ExprList.u.x.iOrderByCol */
  4013   4013   void sqlite3ExprIfTrue(Parse*, Expr*, int, int);
  4014   4014   void sqlite3ExprIfFalse(Parse*, Expr*, int, int);
  4015   4015   void sqlite3ExprIfFalseDup(Parse*, Expr*, int, int);
  4016         -Table *sqlite3FindTable(Parse*,sqlite3*,const char*, const char*);
         4016  +Table *sqlite3FindTable(sqlite3*,const char*, const char*);
  4017   4017   #define LOCATE_VIEW    0x01
  4018   4018   #define LOCATE_NOERR   0x02
  4019   4019   Table *sqlite3LocateTable(Parse*,u32 flags,const char*, const char*);
  4020   4020   Table *sqlite3LocateTableItem(Parse*,u32 flags,struct SrcList_item *);
  4021   4021   Index *sqlite3FindIndex(sqlite3*,const char*, const char*);
  4022   4022   void sqlite3UnlinkAndDeleteTable(sqlite3*,int,const char*);
  4023   4023   void sqlite3UnlinkAndDeleteIndex(sqlite3*,int,const char*);

Changes to src/vtab.c.

   715    715   */
   716    716   int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){
   717    717     int rc = SQLITE_OK;
   718    718     Table *pTab;
   719    719     Module *pMod;
   720    720     const char *zMod;
   721    721   
   722         -  pTab = sqlite3FindTable(0, db, zTab, db->aDb[iDb].zDbSName);
          722  +  pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zDbSName);
   723    723     assert( pTab && IsVirtual(pTab) && !pTab->pVTable );
   724    724   
   725    725     /* Locate the required virtual table module */
   726    726     zMod = pTab->azModuleArg[0];
   727    727     pMod = (Module*)sqlite3HashFind(&db->aModule, zMod);
   728    728   
   729    729     /* If the module has been registered and includes a Create method, 
................................................................................
   839    839   **
   840    840   ** This call is a no-op if zTab is not a virtual table.
   841    841   */
   842    842   int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab){
   843    843     int rc = SQLITE_OK;
   844    844     Table *pTab;
   845    845   
   846         -  pTab = sqlite3FindTable(0, db, zTab, db->aDb[iDb].zDbSName);
          846  +  pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zDbSName);
   847    847     if( pTab!=0 && ALWAYS(pTab->pVTable!=0) ){
   848    848       VTable *p;
   849    849       int (*xDestroy)(sqlite3_vtab *);
   850    850       for(p=pTab->pVTable; p; p=p->pNext){
   851    851         assert( p->pVtab );
   852    852         if( p->pVtab->nRef>0 ){
   853    853           return SQLITE_LOCKED;

Changes to test/reuse2.test.

   174    174   } {nref=2 nschema=1 ndelete=0}
   175    175   do_execsql_test -db db2 4.1.7 {
   176    176     SELECT * FROM x1
   177    177   }
   178    178   do_execsql_test 4.1.8 {
   179    179     SELECT 'nref=' || nRef, 'nschema=' || nSchema, 'ndelete=' || nDelete
   180    180     FROM schemapool;
   181         -} {nref=3 nschema=1 ndelete=0}
          181  +} {nref=6 nschema=1 ndelete=0}
   182    182   
   183    183   do_test 4.2.1 {
   184    184     catchsql { SELECT * FROM abc } db2
   185    185   } {1 {no such table: abc}}
   186    186   do_execsql_test 4.2.2 {
   187    187     SELECT 'nref=' || nRef, 'nschema=' || nSchema, 'ndelete=' || nDelete
   188    188     FROM schemapool;

Changes to test/reuse3.test.

    78     78     UPDATE sqlite_master SET sql = 'CREATE TBL y1(d, e, f)' WHERE name = 'y1';
    79     79   }
    80     80   db close
    81     81   
    82     82   sqlite3 db test.db -shared-schema 1
    83     83   do_catchsql_test 2.1 {
    84     84     SELECT * FROM x1;
    85         -} {1 {no such table: x1}}
           85  +} {1 {malformed database schema (y1) - near "TBL": syntax error}}
    86     86   
    87     87   do_catchsql_test 2.2 {
    88     88     SELECT * FROM x1;
    89         -} {1 {no such table: x1}}
           89  +} {1 {malformed database schema (y1) - near "TBL": syntax error}}
    90     90   
    91     91   #-------------------------------------------------------------------------
    92     92   reset_db
    93     93   do_execsql_test 3.0 {
    94     94     CREATE TABLE x1(a, b, c);
    95     95     CREATE INDEX i1 ON x1(a, b, c);
    96     96     CREATE TRIGGER tr1 AFTER INSERT ON x1 BEGIN
................................................................................
   236    236   
   237    237   do_execsql_test 4.2.7 {
   238    238     DROP TRIGGER tr1;
   239    239     SELECT 'nref=' || nRef, 'nschema=' || nSchema, 'ndelete=' || nDelete 
   240    240       FROM schemapool; 
   241    241   } {nref=5 nschema=1 ndelete=4}
   242    242   
          243  +#--------------------------------------------------------------------------
          244  +reset_db
          245  +do_execsql_test 5.0 {
          246  +  CREATE TABLE t1(a, b);
          247  +  CREATE TABLE t2(a, b);
          248  +  CREATE TABLE t3(a, b);
          249  +}
          250  +
          251  +sqlite3 db2 test.db -shared-schema 1
          252  +register_schemapool_module db2
          253  +
          254  +do_execsql_test 5.1 {
          255  +  PRAGMA writable_schema = 1;
          256  +  UPDATE sqlite_master SET sql='CREATE TABLE t3 a,b' WHERE name = 't3';
          257  +}
          258  +
          259  +do_test 5.2 { 
          260  +  catchsql { SELECT * FROM t1 } db2
          261  +} {1 {malformed database schema (t3) - near "a": syntax error}}
   243    262   
   244    263   finish_test
   245    264   

Changes to test/with3.test.

    26     26   #
    27     27   do_catchsql_test 1.0 {
    28     28     WITH i(x) AS (
    29     29       WITH j AS (SELECT 10)
    30     30       SELECT 5 FROM t0 UNION SELECT 8 FROM m
    31     31     )
    32     32     SELECT * FROM i;
    33         -} {1 {no such table: m}}
           33  +} {1 {no such table: t0}}
    34     34   
    35     35   # Additional test cases that came out of the work to
    36     36   # fix for Kostya's problem.
    37     37   #
    38     38   do_execsql_test 2.0 {
    39     39    WITH
    40     40     x1 AS (SELECT 10),