000001  /*
000002  ** 2001 September 15
000003  **
000004  ** The author disclaims copyright to this source code.  In place of
000005  ** a legal notice, here is a blessing:
000006  **
000007  **    May you do good and not evil.
000008  **    May you find forgiveness for yourself and forgive others.
000009  **    May you share freely, never taking more than you give.
000010  **
000011  *************************************************************************
000012  ** This file contains C code routines that are called by the SQLite parser
000013  ** when syntax rules are reduced.  The routines in this file handle the
000014  ** following kinds of SQL syntax:
000015  **
000016  **     CREATE TABLE
000017  **     DROP TABLE
000018  **     CREATE INDEX
000019  **     DROP INDEX
000020  **     creating ID lists
000021  **     BEGIN TRANSACTION
000022  **     COMMIT
000023  **     ROLLBACK
000024  */
000025  #include "sqliteInt.h"
000026  
000027  #ifndef SQLITE_OMIT_SHARED_CACHE
000028  /*
000029  ** The TableLock structure is only used by the sqlite3TableLock() and
000030  ** codeTableLocks() functions.
000031  */
000032  struct TableLock {
000033    int iDb;               /* The database containing the table to be locked */
000034    Pgno iTab;             /* The root page of the table to be locked */
000035    u8 isWriteLock;        /* True for write lock.  False for a read lock */
000036    const char *zLockName; /* Name of the table */
000037  };
000038  
000039  /*
000040  ** Record the fact that we want to lock a table at run-time.  
000041  **
000042  ** The table to be locked has root page iTab and is found in database iDb.
000043  ** A read or a write lock can be taken depending on isWritelock.
000044  **
000045  ** This routine just records the fact that the lock is desired.  The
000046  ** code to make the lock occur is generated by a later call to
000047  ** codeTableLocks() which occurs during sqlite3FinishCoding().
000048  */
000049  static SQLITE_NOINLINE void lockTable(
000050    Parse *pParse,     /* Parsing context */
000051    int iDb,           /* Index of the database containing the table to lock */
000052    Pgno iTab,         /* Root page number of the table to be locked */
000053    u8 isWriteLock,    /* True for a write lock */
000054    const char *zName  /* Name of the table to be locked */
000055  ){
000056    Parse *pToplevel;
000057    int i;
000058    int nBytes;
000059    TableLock *p;
000060    assert( iDb>=0 );
000061  
000062    pToplevel = sqlite3ParseToplevel(pParse);
000063    for(i=0; i<pToplevel->nTableLock; i++){
000064      p = &pToplevel->aTableLock[i];
000065      if( p->iDb==iDb && p->iTab==iTab ){
000066        p->isWriteLock = (p->isWriteLock || isWriteLock);
000067        return;
000068      }
000069    }
000070  
000071    nBytes = sizeof(TableLock) * (pToplevel->nTableLock+1);
000072    pToplevel->aTableLock =
000073        sqlite3DbReallocOrFree(pToplevel->db, pToplevel->aTableLock, nBytes);
000074    if( pToplevel->aTableLock ){
000075      p = &pToplevel->aTableLock[pToplevel->nTableLock++];
000076      p->iDb = iDb;
000077      p->iTab = iTab;
000078      p->isWriteLock = isWriteLock;
000079      p->zLockName = zName;
000080    }else{
000081      pToplevel->nTableLock = 0;
000082      sqlite3OomFault(pToplevel->db);
000083    }
000084  }
000085  void sqlite3TableLock(
000086    Parse *pParse,     /* Parsing context */
000087    int iDb,           /* Index of the database containing the table to lock */
000088    Pgno iTab,         /* Root page number of the table to be locked */
000089    u8 isWriteLock,    /* True for a write lock */
000090    const char *zName  /* Name of the table to be locked */
000091  ){
000092    if( iDb==1 ) return;
000093    if( !sqlite3BtreeSharable(pParse->db->aDb[iDb].pBt) ) return;
000094    lockTable(pParse, iDb, iTab, isWriteLock, zName);
000095  }
000096  
000097  /*
000098  ** Code an OP_TableLock instruction for each table locked by the
000099  ** statement (configured by calls to sqlite3TableLock()).
000100  */
000101  static void codeTableLocks(Parse *pParse){
000102    int i;
000103    Vdbe *pVdbe = pParse->pVdbe; 
000104    assert( pVdbe!=0 );
000105  
000106    for(i=0; i<pParse->nTableLock; i++){
000107      TableLock *p = &pParse->aTableLock[i];
000108      int p1 = p->iDb;
000109      sqlite3VdbeAddOp4(pVdbe, OP_TableLock, p1, p->iTab, p->isWriteLock,
000110                        p->zLockName, P4_STATIC);
000111    }
000112  }
000113  #else
000114    #define codeTableLocks(x)
000115  #endif
000116  
000117  /*
000118  ** Return TRUE if the given yDbMask object is empty - if it contains no
000119  ** 1 bits.  This routine is used by the DbMaskAllZero() and DbMaskNotZero()
000120  ** macros when SQLITE_MAX_ATTACHED is greater than 30.
000121  */
000122  #if SQLITE_MAX_ATTACHED>30
000123  int sqlite3DbMaskAllZero(yDbMask m){
000124    int i;
000125    for(i=0; i<sizeof(yDbMask); i++) if( m[i] ) return 0;
000126    return 1;
000127  }
000128  #endif
000129  
000130  /*
000131  ** This routine is called after a single SQL statement has been
000132  ** parsed and a VDBE program to execute that statement has been
000133  ** prepared.  This routine puts the finishing touches on the
000134  ** VDBE program and resets the pParse structure for the next
000135  ** parse.
000136  **
000137  ** Note that if an error occurred, it might be the case that
000138  ** no VDBE code was generated.
000139  */
000140  void sqlite3FinishCoding(Parse *pParse){
000141    sqlite3 *db;
000142    Vdbe *v;
000143  
000144    assert( pParse->pToplevel==0 );
000145    db = pParse->db;
000146    assert( db->pParse==pParse );
000147    if( pParse->nested ) return;
000148    if( pParse->nErr ){
000149      if( db->mallocFailed ) pParse->rc = SQLITE_NOMEM;
000150      return;
000151    }
000152    assert( db->mallocFailed==0 );
000153  
000154    /* Begin by generating some termination code at the end of the
000155    ** vdbe program
000156    */
000157    v = pParse->pVdbe;
000158    if( v==0 ){
000159      if( db->init.busy ){
000160        pParse->rc = SQLITE_DONE;
000161        return;
000162      }
000163      v = sqlite3GetVdbe(pParse);
000164      if( v==0 ) pParse->rc = SQLITE_ERROR;
000165    }
000166    assert( !pParse->isMultiWrite 
000167         || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort));
000168    if( v ){
000169      if( pParse->bReturning ){
000170        Returning *pReturning = pParse->u1.pReturning;
000171        int addrRewind;
000172        int i;
000173        int reg;
000174  
000175        if( NEVER(pReturning->nRetCol==0) ){
000176          assert( CORRUPT_DB );
000177        }else{
000178          sqlite3VdbeAddOp0(v, OP_FkCheck);
000179          addrRewind =
000180             sqlite3VdbeAddOp1(v, OP_Rewind, pReturning->iRetCur);
000181          VdbeCoverage(v);
000182          reg = pReturning->iRetReg;
000183          for(i=0; i<pReturning->nRetCol; i++){
000184            sqlite3VdbeAddOp3(v, OP_Column, pReturning->iRetCur, i, reg+i);
000185          }
000186          sqlite3VdbeAddOp2(v, OP_ResultRow, reg, i);
000187          sqlite3VdbeAddOp2(v, OP_Next, pReturning->iRetCur, addrRewind+1);
000188          VdbeCoverage(v);
000189          sqlite3VdbeJumpHere(v, addrRewind);
000190        }
000191      }
000192      sqlite3VdbeAddOp0(v, OP_Halt);
000193  
000194  #if SQLITE_USER_AUTHENTICATION
000195      if( pParse->nTableLock>0 && db->init.busy==0 ){
000196        sqlite3UserAuthInit(db);
000197        if( db->auth.authLevel<UAUTH_User ){
000198          sqlite3ErrorMsg(pParse, "user not authenticated");
000199          pParse->rc = SQLITE_AUTH_USER;
000200          return;
000201        }
000202      }
000203  #endif
000204  
000205      /* The cookie mask contains one bit for each database file open.
000206      ** (Bit 0 is for main, bit 1 is for temp, and so forth.)  Bits are
000207      ** set for each database that is used.  Generate code to start a
000208      ** transaction on each used database and to verify the schema cookie
000209      ** on each used database.
000210      */
000211      if( db->mallocFailed==0 
000212       && (DbMaskNonZero(pParse->cookieMask) || pParse->pConstExpr)
000213      ){
000214        int iDb, i;
000215        assert( sqlite3VdbeGetOp(v, 0)->opcode==OP_Init );
000216        sqlite3VdbeJumpHere(v, 0);
000217        for(iDb=0; iDb<db->nDb; iDb++){
000218          Schema *pSchema;
000219          if( DbMaskTest(pParse->cookieMask, iDb)==0 ) continue;
000220          sqlite3VdbeUsesBtree(v, iDb);
000221          pSchema = db->aDb[iDb].pSchema;
000222          sqlite3VdbeAddOp4Int(v,
000223            OP_Transaction,                    /* Opcode */
000224            iDb,                               /* P1 */
000225            DbMaskTest(pParse->writeMask,iDb), /* P2 */
000226            pSchema->schema_cookie,            /* P3 */
000227            pSchema->iGeneration               /* P4 */
000228          );
000229          if( db->init.busy==0 ) sqlite3VdbeChangeP5(v, 1);
000230          VdbeComment((v,
000231                "usesStmtJournal=%d", pParse->mayAbort && pParse->isMultiWrite));
000232        }
000233  #ifndef SQLITE_OMIT_VIRTUALTABLE
000234        for(i=0; i<pParse->nVtabLock; i++){
000235          char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]);
000236          sqlite3VdbeAddOp4(v, OP_VBegin, 0, 0, 0, vtab, P4_VTAB);
000237        }
000238        pParse->nVtabLock = 0;
000239  #endif
000240  
000241        /* Once all the cookies have been verified and transactions opened, 
000242        ** obtain the required table-locks. This is a no-op unless the 
000243        ** shared-cache feature is enabled.
000244        */
000245        codeTableLocks(pParse);
000246  
000247        /* Initialize any AUTOINCREMENT data structures required.
000248        */
000249        sqlite3AutoincrementBegin(pParse);
000250  
000251        /* Code constant expressions that where factored out of inner loops.
000252        **
000253        ** The pConstExpr list might also contain expressions that we simply
000254        ** want to keep around until the Parse object is deleted.  Such
000255        ** expressions have iConstExprReg==0.  Do not generate code for
000256        ** those expressions, of course.
000257        */
000258        if( pParse->pConstExpr ){
000259          ExprList *pEL = pParse->pConstExpr;
000260          pParse->okConstFactor = 0;
000261          for(i=0; i<pEL->nExpr; i++){
000262            int iReg = pEL->a[i].u.iConstExprReg;
000263            if( iReg>0 ){
000264              sqlite3ExprCode(pParse, pEL->a[i].pExpr, iReg);
000265            }
000266          }
000267        }
000268  
000269        if( pParse->bReturning ){
000270          Returning *pRet = pParse->u1.pReturning;
000271          if( NEVER(pRet->nRetCol==0) ){
000272            assert( CORRUPT_DB );
000273          }else{
000274            sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pRet->iRetCur, pRet->nRetCol);
000275          }
000276        }
000277  
000278        /* Finally, jump back to the beginning of the executable code. */
000279        sqlite3VdbeGoto(v, 1);
000280      }
000281    }
000282  
000283    /* Get the VDBE program ready for execution
000284    */
000285    assert( v!=0 || pParse->nErr );
000286    assert( db->mallocFailed==0 || pParse->nErr );
000287    if( pParse->nErr==0 ){
000288      /* A minimum of one cursor is required if autoincrement is used
000289      *  See ticket [a696379c1f08866] */
000290      assert( pParse->pAinc==0 || pParse->nTab>0 );
000291      sqlite3VdbeMakeReady(v, pParse);
000292      pParse->rc = SQLITE_DONE;
000293    }else{
000294      pParse->rc = SQLITE_ERROR;
000295    }
000296  }
000297  
000298  /*
000299  ** Run the parser and code generator recursively in order to generate
000300  ** code for the SQL statement given onto the end of the pParse context
000301  ** currently under construction.  Notes:
000302  **
000303  **   *  The final OP_Halt is not appended and other initialization
000304  **      and finalization steps are omitted because those are handling by the
000305  **      outermost parser.
000306  **
000307  **   *  Built-in SQL functions always take precedence over application-defined
000308  **      SQL functions.  In other words, it is not possible to override a
000309  **      built-in function.
000310  */
000311  void sqlite3NestedParse(Parse *pParse, const char *zFormat, ...){
000312    va_list ap;
000313    char *zSql;
000314    sqlite3 *db = pParse->db;
000315    u32 savedDbFlags = db->mDbFlags;
000316    char saveBuf[PARSE_TAIL_SZ];
000317  
000318    if( pParse->nErr ) return;
000319    assert( pParse->nested<10 );  /* Nesting should only be of limited depth */
000320    va_start(ap, zFormat);
000321    zSql = sqlite3VMPrintf(db, zFormat, ap);
000322    va_end(ap);
000323    if( zSql==0 ){
000324      /* This can result either from an OOM or because the formatted string
000325      ** exceeds SQLITE_LIMIT_LENGTH.  In the latter case, we need to set
000326      ** an error */
000327      if( !db->mallocFailed ) pParse->rc = SQLITE_TOOBIG;
000328      pParse->nErr++;
000329      return;
000330    }
000331    pParse->nested++;
000332    memcpy(saveBuf, PARSE_TAIL(pParse), PARSE_TAIL_SZ);
000333    memset(PARSE_TAIL(pParse), 0, PARSE_TAIL_SZ);
000334    db->mDbFlags |= DBFLAG_PreferBuiltin;
000335    sqlite3RunParser(pParse, zSql);
000336    sqlite3DbFree(db, pParse->zErrMsg);
000337    pParse->zErrMsg = 0;
000338    db->mDbFlags = savedDbFlags;
000339    sqlite3DbFree(db, zSql);
000340    memcpy(PARSE_TAIL(pParse), saveBuf, PARSE_TAIL_SZ);
000341    pParse->nested--;
000342  }
000343  
000344  #if SQLITE_USER_AUTHENTICATION
000345  /*
000346  ** Return TRUE if zTable is the name of the system table that stores the
000347  ** list of users and their access credentials.
000348  */
000349  int sqlite3UserAuthTable(const char *zTable){
000350    return sqlite3_stricmp(zTable, "sqlite_user")==0;
000351  }
000352  #endif
000353  
000354  /*
000355  ** Locate the in-memory structure that describes a particular database
000356  ** table given the name of that table and (optionally) the name of the
000357  ** database containing the table.  Return NULL if not found.
000358  **
000359  ** If zDatabase is 0, all databases are searched for the table and the
000360  ** first matching table is returned.  (No checking for duplicate table
000361  ** names is done.)  The search order is TEMP first, then MAIN, then any
000362  ** auxiliary databases added using the ATTACH command.
000363  **
000364  ** See also sqlite3LocateTable().
000365  */
000366  Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){
000367    Table *p = 0;
000368    int i;
000369  
000370    /* All mutexes are required for schema access.  Make sure we hold them. */
000371    assert( zDatabase!=0 || sqlite3BtreeHoldsAllMutexes(db) );
000372  #if SQLITE_USER_AUTHENTICATION
000373    /* Only the admin user is allowed to know that the sqlite_user table
000374    ** exists */
000375    if( db->auth.authLevel<UAUTH_Admin && sqlite3UserAuthTable(zName)!=0 ){
000376      return 0;
000377    }
000378  #endif
000379    if( zDatabase ){
000380      for(i=0; i<db->nDb; i++){
000381        if( sqlite3StrICmp(zDatabase, db->aDb[i].zDbSName)==0 ) break;
000382      }
000383      if( i>=db->nDb ){
000384        /* No match against the official names.  But always match "main"
000385        ** to schema 0 as a legacy fallback. */
000386        if( sqlite3StrICmp(zDatabase,"main")==0 ){
000387          i = 0;
000388        }else{
000389          return 0;
000390        }
000391      }
000392      p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash, zName);
000393      if( p==0 && sqlite3StrNICmp(zName, "sqlite_", 7)==0 ){
000394        if( i==1 ){
000395          if( sqlite3StrICmp(zName+7, &PREFERRED_TEMP_SCHEMA_TABLE[7])==0
000396           || sqlite3StrICmp(zName+7, &PREFERRED_SCHEMA_TABLE[7])==0
000397           || sqlite3StrICmp(zName+7, &LEGACY_SCHEMA_TABLE[7])==0
000398          ){
000399            p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash, 
000400                                LEGACY_TEMP_SCHEMA_TABLE);
000401          }
000402        }else{
000403          if( sqlite3StrICmp(zName+7, &PREFERRED_SCHEMA_TABLE[7])==0 ){
000404            p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash,
000405                                LEGACY_SCHEMA_TABLE);
000406          }
000407        }
000408      }
000409    }else{
000410      /* Match against TEMP first */
000411      p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash, zName);
000412      if( p ) return p;
000413      /* The main database is second */
000414      p = sqlite3HashFind(&db->aDb[0].pSchema->tblHash, zName);
000415      if( p ) return p;
000416      /* Attached databases are in order of attachment */
000417      for(i=2; i<db->nDb; i++){
000418        assert( sqlite3SchemaMutexHeld(db, i, 0) );
000419        p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash, zName);
000420        if( p ) break;
000421      }
000422      if( p==0 && sqlite3StrNICmp(zName, "sqlite_", 7)==0 ){
000423        if( sqlite3StrICmp(zName+7, &PREFERRED_SCHEMA_TABLE[7])==0 ){
000424          p = sqlite3HashFind(&db->aDb[0].pSchema->tblHash, LEGACY_SCHEMA_TABLE);
000425        }else if( sqlite3StrICmp(zName+7, &PREFERRED_TEMP_SCHEMA_TABLE[7])==0 ){
000426          p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash, 
000427                              LEGACY_TEMP_SCHEMA_TABLE);
000428        }
000429      }
000430    }
000431    return p;
000432  }
000433  
000434  /*
000435  ** Locate the in-memory structure that describes a particular database
000436  ** table given the name of that table and (optionally) the name of the
000437  ** database containing the table.  Return NULL if not found.  Also leave an
000438  ** error message in pParse->zErrMsg.
000439  **
000440  ** The difference between this routine and sqlite3FindTable() is that this
000441  ** routine leaves an error message in pParse->zErrMsg where
000442  ** sqlite3FindTable() does not.
000443  */
000444  Table *sqlite3LocateTable(
000445    Parse *pParse,         /* context in which to report errors */
000446    u32 flags,             /* LOCATE_VIEW or LOCATE_NOERR */
000447    const char *zName,     /* Name of the table we are looking for */
000448    const char *zDbase     /* Name of the database.  Might be NULL */
000449  ){
000450    Table *p;
000451    sqlite3 *db = pParse->db;
000452  
000453    /* Read the database schema. If an error occurs, leave an error message
000454    ** and code in pParse and return NULL. */
000455    if( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0 
000456     && SQLITE_OK!=sqlite3ReadSchema(pParse)
000457    ){
000458      return 0;
000459    }
000460  
000461    p = sqlite3FindTable(db, zName, zDbase);
000462    if( p==0 ){
000463  #ifndef SQLITE_OMIT_VIRTUALTABLE
000464      /* If zName is the not the name of a table in the schema created using
000465      ** CREATE, then check to see if it is the name of an virtual table that
000466      ** can be an eponymous virtual table. */
000467      if( pParse->disableVtab==0 && db->init.busy==0 ){
000468        Module *pMod = (Module*)sqlite3HashFind(&db->aModule, zName);
000469        if( pMod==0 && sqlite3_strnicmp(zName, "pragma_", 7)==0 ){
000470          pMod = sqlite3PragmaVtabRegister(db, zName);
000471        }
000472        if( pMod && sqlite3VtabEponymousTableInit(pParse, pMod) ){
000473          testcase( pMod->pEpoTab==0 );
000474          return pMod->pEpoTab;
000475        }
000476      }
000477  #endif
000478      if( flags & LOCATE_NOERR ) return 0;
000479      pParse->checkSchema = 1;
000480    }else if( IsVirtual(p) && pParse->disableVtab ){
000481      p = 0;
000482    }
000483  
000484    if( p==0 ){
000485      const char *zMsg = flags & LOCATE_VIEW ? "no such view" : "no such table";
000486      if( zDbase ){
000487        sqlite3ErrorMsg(pParse, "%s: %s.%s", zMsg, zDbase, zName);
000488      }else{
000489        sqlite3ErrorMsg(pParse, "%s: %s", zMsg, zName);
000490      }
000491    }else{
000492      assert( HasRowid(p) || p->iPKey<0 );
000493    }
000494  
000495    return p;
000496  }
000497  
000498  /*
000499  ** Locate the table identified by *p.
000500  **
000501  ** This is a wrapper around sqlite3LocateTable(). The difference between
000502  ** sqlite3LocateTable() and this function is that this function restricts
000503  ** the search to schema (p->pSchema) if it is not NULL. p->pSchema may be
000504  ** non-NULL if it is part of a view or trigger program definition. See
000505  ** sqlite3FixSrcList() for details.
000506  */
000507  Table *sqlite3LocateTableItem(
000508    Parse *pParse, 
000509    u32 flags,
000510    SrcItem *p
000511  ){
000512    const char *zDb;
000513    assert( p->pSchema==0 || p->zDatabase==0 );
000514    if( p->pSchema ){
000515      int iDb = sqlite3SchemaToIndex(pParse->db, p->pSchema);
000516      zDb = pParse->db->aDb[iDb].zDbSName;
000517    }else{
000518      zDb = p->zDatabase;
000519    }
000520    return sqlite3LocateTable(pParse, flags, p->zName, zDb);
000521  }
000522  
000523  /*
000524  ** Return the preferred table name for system tables.  Translate legacy
000525  ** names into the new preferred names, as appropriate.
000526  */
000527  const char *sqlite3PreferredTableName(const char *zName){
000528    if( sqlite3StrNICmp(zName, "sqlite_", 7)==0 ){
000529      if( sqlite3StrICmp(zName+7, &LEGACY_SCHEMA_TABLE[7])==0 ){
000530        return PREFERRED_SCHEMA_TABLE;
000531      }
000532      if( sqlite3StrICmp(zName+7, &LEGACY_TEMP_SCHEMA_TABLE[7])==0 ){
000533        return PREFERRED_TEMP_SCHEMA_TABLE;
000534      }
000535    }
000536    return zName;
000537  }
000538  
000539  /*
000540  ** Locate the in-memory structure that describes 
000541  ** a particular index given the name of that index
000542  ** and the name of the database that contains the index.
000543  ** Return NULL if not found.
000544  **
000545  ** If zDatabase is 0, all databases are searched for the
000546  ** table and the first matching index is returned.  (No checking
000547  ** for duplicate index names is done.)  The search order is
000548  ** TEMP first, then MAIN, then any auxiliary databases added
000549  ** using the ATTACH command.
000550  */
000551  Index *sqlite3FindIndex(sqlite3 *db, const char *zName, const char *zDb){
000552    Index *p = 0;
000553    int i;
000554    /* All mutexes are required for schema access.  Make sure we hold them. */
000555    assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
000556    for(i=OMIT_TEMPDB; i<db->nDb; i++){
000557      int j = (i<2) ? i^1 : i;  /* Search TEMP before MAIN */
000558      Schema *pSchema = db->aDb[j].pSchema;
000559      assert( pSchema );
000560      if( zDb && sqlite3DbIsNamed(db, j, zDb)==0 ) continue;
000561      assert( sqlite3SchemaMutexHeld(db, j, 0) );
000562      p = sqlite3HashFind(&pSchema->idxHash, zName);
000563      if( p ) break;
000564    }
000565    return p;
000566  }
000567  
000568  /*
000569  ** Reclaim the memory used by an index
000570  */
000571  void sqlite3FreeIndex(sqlite3 *db, Index *p){
000572  #ifndef SQLITE_OMIT_ANALYZE
000573    sqlite3DeleteIndexSamples(db, p);
000574  #endif
000575    sqlite3ExprDelete(db, p->pPartIdxWhere);
000576    sqlite3ExprListDelete(db, p->aColExpr);
000577    sqlite3DbFree(db, p->zColAff);
000578    if( p->isResized ) sqlite3DbFree(db, (void *)p->azColl);
000579  #ifdef SQLITE_ENABLE_STAT4
000580    sqlite3_free(p->aiRowEst);
000581  #endif
000582    sqlite3DbFree(db, p);
000583  }
000584  
000585  /*
000586  ** For the index called zIdxName which is found in the database iDb,
000587  ** unlike that index from its Table then remove the index from
000588  ** the index hash table and free all memory structures associated
000589  ** with the index.
000590  */
000591  void sqlite3UnlinkAndDeleteIndex(sqlite3 *db, int iDb, const char *zIdxName){
000592    Index *pIndex;
000593    Hash *pHash;
000594  
000595    assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
000596    pHash = &db->aDb[iDb].pSchema->idxHash;
000597    pIndex = sqlite3HashInsert(pHash, zIdxName, 0);
000598    if( ALWAYS(pIndex) ){
000599      if( pIndex->pTable->pIndex==pIndex ){
000600        pIndex->pTable->pIndex = pIndex->pNext;
000601      }else{
000602        Index *p;
000603        /* Justification of ALWAYS();  The index must be on the list of
000604        ** indices. */
000605        p = pIndex->pTable->pIndex;
000606        while( ALWAYS(p) && p->pNext!=pIndex ){ p = p->pNext; }
000607        if( ALWAYS(p && p->pNext==pIndex) ){
000608          p->pNext = pIndex->pNext;
000609        }
000610      }
000611      sqlite3FreeIndex(db, pIndex);
000612    }
000613    db->mDbFlags |= DBFLAG_SchemaChange;
000614  }
000615  
000616  /*
000617  ** Look through the list of open database files in db->aDb[] and if
000618  ** any have been closed, remove them from the list.  Reallocate the
000619  ** db->aDb[] structure to a smaller size, if possible.
000620  **
000621  ** Entry 0 (the "main" database) and entry 1 (the "temp" database)
000622  ** are never candidates for being collapsed.
000623  */
000624  void sqlite3CollapseDatabaseArray(sqlite3 *db){
000625    int i, j;
000626    for(i=j=2; i<db->nDb; i++){
000627      struct Db *pDb = &db->aDb[i];
000628      if( pDb->pBt==0 ){
000629        sqlite3DbFree(db, pDb->zDbSName);
000630        pDb->zDbSName = 0;
000631        continue;
000632      }
000633      if( j<i ){
000634        db->aDb[j] = db->aDb[i];
000635      }
000636      j++;
000637    }
000638    db->nDb = j;
000639    if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
000640      memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0]));
000641      sqlite3DbFree(db, db->aDb);
000642      db->aDb = db->aDbStatic;
000643    }
000644  }
000645  
000646  /*
000647  ** Reset the schema for the database at index iDb.  Also reset the
000648  ** TEMP schema.  The reset is deferred if db->nSchemaLock is not zero.
000649  ** Deferred resets may be run by calling with iDb<0.
000650  */
000651  void sqlite3ResetOneSchema(sqlite3 *db, int iDb){
000652    int i;
000653    assert( iDb<db->nDb );
000654  
000655    if( iDb>=0 ){
000656      assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
000657      DbSetProperty(db, iDb, DB_ResetWanted);
000658      DbSetProperty(db, 1, DB_ResetWanted);
000659      db->mDbFlags &= ~DBFLAG_SchemaKnownOk;
000660    }
000661  
000662    if( db->nSchemaLock==0 ){
000663      for(i=0; i<db->nDb; i++){
000664        if( DbHasProperty(db, i, DB_ResetWanted) ){
000665          sqlite3SchemaClear(db->aDb[i].pSchema);
000666        }
000667      }
000668    }
000669  }
000670  
000671  /*
000672  ** Erase all schema information from all attached databases (including
000673  ** "main" and "temp") for a single database connection.
000674  */
000675  void sqlite3ResetAllSchemasOfConnection(sqlite3 *db){
000676    int i;
000677    sqlite3BtreeEnterAll(db);
000678    for(i=0; i<db->nDb; i++){
000679      Db *pDb = &db->aDb[i];
000680      if( pDb->pSchema ){
000681        if( db->nSchemaLock==0 ){
000682          sqlite3SchemaClear(pDb->pSchema);
000683        }else{
000684          DbSetProperty(db, i, DB_ResetWanted);
000685        }
000686      }
000687    }
000688    db->mDbFlags &= ~(DBFLAG_SchemaChange|DBFLAG_SchemaKnownOk);
000689    sqlite3VtabUnlockList(db);
000690    sqlite3BtreeLeaveAll(db);
000691    if( db->nSchemaLock==0 ){
000692      sqlite3CollapseDatabaseArray(db);
000693    }
000694  }
000695  
000696  /*
000697  ** This routine is called when a commit occurs.
000698  */
000699  void sqlite3CommitInternalChanges(sqlite3 *db){
000700    db->mDbFlags &= ~DBFLAG_SchemaChange;
000701  }
000702  
000703  /*
000704  ** Set the expression associated with a column.  This is usually
000705  ** the DEFAULT value, but might also be the expression that computes
000706  ** the value for a generated column.
000707  */
000708  void sqlite3ColumnSetExpr(
000709    Parse *pParse,    /* Parsing context */
000710    Table *pTab,      /* The table containing the column */
000711    Column *pCol,     /* The column to receive the new DEFAULT expression */
000712    Expr *pExpr       /* The new default expression */
000713  ){
000714    ExprList *pList;
000715    assert( IsOrdinaryTable(pTab) );
000716    pList = pTab->u.tab.pDfltList;
000717    if( pCol->iDflt==0
000718     || NEVER(pList==0)
000719     || NEVER(pList->nExpr<pCol->iDflt)
000720    ){
000721      pCol->iDflt = pList==0 ? 1 : pList->nExpr+1;
000722      pTab->u.tab.pDfltList = sqlite3ExprListAppend(pParse, pList, pExpr);
000723    }else{
000724      sqlite3ExprDelete(pParse->db, pList->a[pCol->iDflt-1].pExpr);
000725      pList->a[pCol->iDflt-1].pExpr = pExpr;
000726    }
000727  }
000728  
000729  /*
000730  ** Return the expression associated with a column.  The expression might be
000731  ** the DEFAULT clause or the AS clause of a generated column.
000732  ** Return NULL if the column has no associated expression.
000733  */
000734  Expr *sqlite3ColumnExpr(Table *pTab, Column *pCol){
000735    if( pCol->iDflt==0 ) return 0;
000736    if( NEVER(!IsOrdinaryTable(pTab)) ) return 0;
000737    if( NEVER(pTab->u.tab.pDfltList==0) ) return 0;
000738    if( NEVER(pTab->u.tab.pDfltList->nExpr<pCol->iDflt) ) return 0;
000739    return pTab->u.tab.pDfltList->a[pCol->iDflt-1].pExpr;
000740  }
000741  
000742  /*
000743  ** Set the collating sequence name for a column.
000744  */
000745  void sqlite3ColumnSetColl(
000746    sqlite3 *db,
000747    Column *pCol,
000748    const char *zColl
000749  ){
000750    i64 nColl;
000751    i64 n;
000752    char *zNew;
000753    assert( zColl!=0 );
000754    n = sqlite3Strlen30(pCol->zCnName) + 1;
000755    if( pCol->colFlags & COLFLAG_HASTYPE ){
000756      n += sqlite3Strlen30(pCol->zCnName+n) + 1;
000757    }
000758    nColl = sqlite3Strlen30(zColl) + 1;
000759    zNew = sqlite3DbRealloc(db, pCol->zCnName, nColl+n);
000760    if( zNew ){
000761      pCol->zCnName = zNew;
000762      memcpy(pCol->zCnName + n, zColl, nColl);
000763      pCol->colFlags |= COLFLAG_HASCOLL;
000764    }
000765  }
000766  
000767  /*
000768  ** Return the collating squence name for a column
000769  */
000770  const char *sqlite3ColumnColl(Column *pCol){
000771    const char *z;
000772    if( (pCol->colFlags & COLFLAG_HASCOLL)==0 ) return 0;
000773    z = pCol->zCnName;
000774    while( *z ){ z++; }
000775    if( pCol->colFlags & COLFLAG_HASTYPE ){
000776      do{ z++; }while( *z );
000777    }
000778    return z+1;
000779  }
000780  
000781  /*
000782  ** Delete memory allocated for the column names of a table or view (the
000783  ** Table.aCol[] array).
000784  */
000785  void sqlite3DeleteColumnNames(sqlite3 *db, Table *pTable){
000786    int i;
000787    Column *pCol;
000788    assert( pTable!=0 );
000789    if( (pCol = pTable->aCol)!=0 ){
000790      for(i=0; i<pTable->nCol; i++, pCol++){
000791        assert( pCol->zCnName==0 || pCol->hName==sqlite3StrIHash(pCol->zCnName) );
000792        sqlite3DbFree(db, pCol->zCnName);
000793      }
000794      sqlite3DbFree(db, pTable->aCol);
000795      if( IsOrdinaryTable(pTable) ){
000796        sqlite3ExprListDelete(db, pTable->u.tab.pDfltList);
000797      }
000798      if( db==0 || db->pnBytesFreed==0 ){
000799        pTable->aCol = 0;
000800        pTable->nCol = 0;
000801        if( IsOrdinaryTable(pTable) ){
000802          pTable->u.tab.pDfltList = 0;
000803        }
000804      }
000805    }
000806  }
000807  
000808  /*
000809  ** Remove the memory data structures associated with the given
000810  ** Table.  No changes are made to disk by this routine.
000811  **
000812  ** This routine just deletes the data structure.  It does not unlink
000813  ** the table data structure from the hash table.  But it does destroy
000814  ** memory structures of the indices and foreign keys associated with 
000815  ** the table.
000816  **
000817  ** The db parameter is optional.  It is needed if the Table object 
000818  ** contains lookaside memory.  (Table objects in the schema do not use
000819  ** lookaside memory, but some ephemeral Table objects do.)  Or the
000820  ** db parameter can be used with db->pnBytesFreed to measure the memory
000821  ** used by the Table object.
000822  */
000823  static void SQLITE_NOINLINE deleteTable(sqlite3 *db, Table *pTable){
000824    Index *pIndex, *pNext;
000825  
000826  #ifdef SQLITE_DEBUG
000827    /* Record the number of outstanding lookaside allocations in schema Tables
000828    ** prior to doing any free() operations. Since schema Tables do not use
000829    ** lookaside, this number should not change. 
000830    **
000831    ** If malloc has already failed, it may be that it failed while allocating
000832    ** a Table object that was going to be marked ephemeral. So do not check
000833    ** that no lookaside memory is used in this case either. */
000834    int nLookaside = 0;
000835    if( db && !db->mallocFailed && (pTable->tabFlags & TF_Ephemeral)==0 ){
000836      nLookaside = sqlite3LookasideUsed(db, 0);
000837    }
000838  #endif
000839  
000840    /* Delete all indices associated with this table. */
000841    for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
000842      pNext = pIndex->pNext;
000843      assert( pIndex->pSchema==pTable->pSchema
000844           || (IsVirtual(pTable) && pIndex->idxType!=SQLITE_IDXTYPE_APPDEF) );
000845      if( (db==0 || db->pnBytesFreed==0) && !IsVirtual(pTable) ){
000846        char *zName = pIndex->zName; 
000847        TESTONLY ( Index *pOld = ) sqlite3HashInsert(
000848           &pIndex->pSchema->idxHash, zName, 0
000849        );
000850        assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
000851        assert( pOld==pIndex || pOld==0 );
000852      }
000853      sqlite3FreeIndex(db, pIndex);
000854    }
000855  
000856    if( IsOrdinaryTable(pTable) ){
000857      sqlite3FkDelete(db, pTable);
000858    }
000859  #ifndef SQLITE_OMIT_VIRTUAL_TABLE
000860    else if( IsVirtual(pTable) ){
000861      sqlite3VtabClear(db, pTable);
000862    }
000863  #endif
000864    else{
000865      assert( IsView(pTable) );
000866      sqlite3SelectDelete(db, pTable->u.view.pSelect);
000867    }
000868  
000869    /* Delete the Table structure itself.
000870    */
000871    sqlite3DeleteColumnNames(db, pTable);
000872    sqlite3DbFree(db, pTable->zName);
000873    sqlite3DbFree(db, pTable->zColAff);
000874    sqlite3ExprListDelete(db, pTable->pCheck);
000875    sqlite3DbFree(db, pTable);
000876  
000877    /* Verify that no lookaside memory was used by schema tables */
000878    assert( nLookaside==0 || nLookaside==sqlite3LookasideUsed(db,0) );
000879  }
000880  void sqlite3DeleteTable(sqlite3 *db, Table *pTable){
000881    /* Do not delete the table until the reference count reaches zero. */
000882    if( !pTable ) return;
000883    if( ((!db || db->pnBytesFreed==0) && (--pTable->nTabRef)>0) ) return;
000884    deleteTable(db, pTable);
000885  }
000886  
000887  
000888  /*
000889  ** Unlink the given table from the hash tables and the delete the
000890  ** table structure with all its indices and foreign keys.
000891  */
000892  void sqlite3UnlinkAndDeleteTable(sqlite3 *db, int iDb, const char *zTabName){
000893    Table *p;
000894    Db *pDb;
000895  
000896    assert( db!=0 );
000897    assert( iDb>=0 && iDb<db->nDb );
000898    assert( zTabName );
000899    assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
000900    testcase( zTabName[0]==0 );  /* Zero-length table names are allowed */
000901    pDb = &db->aDb[iDb];
000902    p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName, 0);
000903    sqlite3DeleteTable(db, p);
000904    db->mDbFlags |= DBFLAG_SchemaChange;
000905  }
000906  
000907  /*
000908  ** Given a token, return a string that consists of the text of that
000909  ** token.  Space to hold the returned string
000910  ** is obtained from sqliteMalloc() and must be freed by the calling
000911  ** function.
000912  **
000913  ** Any quotation marks (ex:  "name", 'name', [name], or `name`) that
000914  ** surround the body of the token are removed.
000915  **
000916  ** Tokens are often just pointers into the original SQL text and so
000917  ** are not \000 terminated and are not persistent.  The returned string
000918  ** is \000 terminated and is persistent.
000919  */
000920  char *sqlite3NameFromToken(sqlite3 *db, const Token *pName){
000921    char *zName;
000922    if( pName ){
000923      zName = sqlite3DbStrNDup(db, (const char*)pName->z, pName->n);
000924      sqlite3Dequote(zName);
000925    }else{
000926      zName = 0;
000927    }
000928    return zName;
000929  }
000930  
000931  /*
000932  ** Open the sqlite_schema table stored in database number iDb for
000933  ** writing. The table is opened using cursor 0.
000934  */
000935  void sqlite3OpenSchemaTable(Parse *p, int iDb){
000936    Vdbe *v = sqlite3GetVdbe(p);
000937    sqlite3TableLock(p, iDb, SCHEMA_ROOT, 1, LEGACY_SCHEMA_TABLE);
000938    sqlite3VdbeAddOp4Int(v, OP_OpenWrite, 0, SCHEMA_ROOT, iDb, 5);
000939    if( p->nTab==0 ){
000940      p->nTab = 1;
000941    }
000942  }
000943  
000944  /*
000945  ** Parameter zName points to a nul-terminated buffer containing the name
000946  ** of a database ("main", "temp" or the name of an attached db). This
000947  ** function returns the index of the named database in db->aDb[], or
000948  ** -1 if the named db cannot be found.
000949  */
000950  int sqlite3FindDbName(sqlite3 *db, const char *zName){
000951    int i = -1;         /* Database number */
000952    if( zName ){
000953      Db *pDb;
000954      for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){
000955        if( 0==sqlite3_stricmp(pDb->zDbSName, zName) ) break;
000956        /* "main" is always an acceptable alias for the primary database
000957        ** even if it has been renamed using SQLITE_DBCONFIG_MAINDBNAME. */
000958        if( i==0 && 0==sqlite3_stricmp("main", zName) ) break;
000959      }
000960    }
000961    return i;
000962  }
000963  
000964  /*
000965  ** The token *pName contains the name of a database (either "main" or
000966  ** "temp" or the name of an attached db). This routine returns the
000967  ** index of the named database in db->aDb[], or -1 if the named db 
000968  ** does not exist.
000969  */
000970  int sqlite3FindDb(sqlite3 *db, Token *pName){
000971    int i;                               /* Database number */
000972    char *zName;                         /* Name we are searching for */
000973    zName = sqlite3NameFromToken(db, pName);
000974    i = sqlite3FindDbName(db, zName);
000975    sqlite3DbFree(db, zName);
000976    return i;
000977  }
000978  
000979  /* The table or view or trigger name is passed to this routine via tokens
000980  ** pName1 and pName2. If the table name was fully qualified, for example:
000981  **
000982  ** CREATE TABLE xxx.yyy (...);
000983  ** 
000984  ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
000985  ** the table name is not fully qualified, i.e.:
000986  **
000987  ** CREATE TABLE yyy(...);
000988  **
000989  ** Then pName1 is set to "yyy" and pName2 is "".
000990  **
000991  ** This routine sets the *ppUnqual pointer to point at the token (pName1 or
000992  ** pName2) that stores the unqualified table name.  The index of the
000993  ** database "xxx" is returned.
000994  */
000995  int sqlite3TwoPartName(
000996    Parse *pParse,      /* Parsing and code generating context */
000997    Token *pName1,      /* The "xxx" in the name "xxx.yyy" or "xxx" */
000998    Token *pName2,      /* The "yyy" in the name "xxx.yyy" */
000999    Token **pUnqual     /* Write the unqualified object name here */
001000  ){
001001    int iDb;                    /* Database holding the object */
001002    sqlite3 *db = pParse->db;
001003  
001004    assert( pName2!=0 );
001005    if( pName2->n>0 ){
001006      if( db->init.busy ) {
001007        sqlite3ErrorMsg(pParse, "corrupt database");
001008        return -1;
001009      }
001010      *pUnqual = pName2;
001011      iDb = sqlite3FindDb(db, pName1);
001012      if( iDb<0 ){
001013        sqlite3ErrorMsg(pParse, "unknown database %T", pName1);
001014        return -1;
001015      }
001016    }else{
001017      assert( db->init.iDb==0 || db->init.busy || IN_SPECIAL_PARSE
001018               || (db->mDbFlags & DBFLAG_Vacuum)!=0);
001019      iDb = db->init.iDb;
001020      *pUnqual = pName1;
001021    }
001022    return iDb;
001023  }
001024  
001025  /*
001026  ** True if PRAGMA writable_schema is ON
001027  */
001028  int sqlite3WritableSchema(sqlite3 *db){
001029    testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==0 );
001030    testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
001031                 SQLITE_WriteSchema );
001032    testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
001033                 SQLITE_Defensive );
001034    testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
001035                 (SQLITE_WriteSchema|SQLITE_Defensive) );
001036    return (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==SQLITE_WriteSchema;
001037  }
001038  
001039  /*
001040  ** This routine is used to check if the UTF-8 string zName is a legal
001041  ** unqualified name for a new schema object (table, index, view or
001042  ** trigger). All names are legal except those that begin with the string
001043  ** "sqlite_" (in upper, lower or mixed case). This portion of the namespace
001044  ** is reserved for internal use.
001045  **
001046  ** When parsing the sqlite_schema table, this routine also checks to
001047  ** make sure the "type", "name", and "tbl_name" columns are consistent
001048  ** with the SQL.
001049  */
001050  int sqlite3CheckObjectName(
001051    Parse *pParse,            /* Parsing context */
001052    const char *zName,        /* Name of the object to check */
001053    const char *zType,        /* Type of this object */
001054    const char *zTblName      /* Parent table name for triggers and indexes */
001055  ){
001056    sqlite3 *db = pParse->db;
001057    if( sqlite3WritableSchema(db)
001058     || db->init.imposterTable
001059     || !sqlite3Config.bExtraSchemaChecks
001060    ){
001061      /* Skip these error checks for writable_schema=ON */
001062      return SQLITE_OK;
001063    }
001064    if( db->init.busy ){
001065      if( sqlite3_stricmp(zType, db->init.azInit[0])
001066       || sqlite3_stricmp(zName, db->init.azInit[1])
001067       || sqlite3_stricmp(zTblName, db->init.azInit[2])
001068      ){
001069        sqlite3ErrorMsg(pParse, ""); /* corruptSchema() will supply the error */
001070        return SQLITE_ERROR;
001071      }
001072    }else{
001073      if( (pParse->nested==0 && 0==sqlite3StrNICmp(zName, "sqlite_", 7))
001074       || (sqlite3ReadOnlyShadowTables(db) && sqlite3ShadowTableName(db, zName))
001075      ){
001076        sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s",
001077                        zName);
001078        return SQLITE_ERROR;
001079      }
001080  
001081    }
001082    return SQLITE_OK;
001083  }
001084  
001085  /*
001086  ** Return the PRIMARY KEY index of a table
001087  */
001088  Index *sqlite3PrimaryKeyIndex(Table *pTab){
001089    Index *p;
001090    for(p=pTab->pIndex; p && !IsPrimaryKeyIndex(p); p=p->pNext){}
001091    return p;
001092  }
001093  
001094  /*
001095  ** Convert an table column number into a index column number.  That is,
001096  ** for the column iCol in the table (as defined by the CREATE TABLE statement)
001097  ** find the (first) offset of that column in index pIdx.  Or return -1
001098  ** if column iCol is not used in index pIdx.
001099  */
001100  i16 sqlite3TableColumnToIndex(Index *pIdx, i16 iCol){
001101    int i;
001102    for(i=0; i<pIdx->nColumn; i++){
001103      if( iCol==pIdx->aiColumn[i] ) return i;
001104    }
001105    return -1;
001106  }
001107  
001108  #ifndef SQLITE_OMIT_GENERATED_COLUMNS
001109  /* Convert a storage column number into a table column number.
001110  **
001111  ** The storage column number (0,1,2,....) is the index of the value
001112  ** as it appears in the record on disk.  The true column number
001113  ** is the index (0,1,2,...) of the column in the CREATE TABLE statement.
001114  **
001115  ** The storage column number is less than the table column number if
001116  ** and only there are VIRTUAL columns to the left.
001117  **
001118  ** If SQLITE_OMIT_GENERATED_COLUMNS, this routine is a no-op macro.
001119  */
001120  i16 sqlite3StorageColumnToTable(Table *pTab, i16 iCol){
001121    if( pTab->tabFlags & TF_HasVirtual ){
001122      int i;
001123      for(i=0; i<=iCol; i++){
001124        if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ) iCol++;
001125      }
001126    }
001127    return iCol;
001128  }
001129  #endif
001130  
001131  #ifndef SQLITE_OMIT_GENERATED_COLUMNS
001132  /* Convert a table column number into a storage column number.
001133  **
001134  ** The storage column number (0,1,2,....) is the index of the value
001135  ** as it appears in the record on disk.  Or, if the input column is
001136  ** the N-th virtual column (zero-based) then the storage number is
001137  ** the number of non-virtual columns in the table plus N.  
001138  **
001139  ** The true column number is the index (0,1,2,...) of the column in
001140  ** the CREATE TABLE statement.
001141  **
001142  ** If the input column is a VIRTUAL column, then it should not appear
001143  ** in storage.  But the value sometimes is cached in registers that
001144  ** follow the range of registers used to construct storage.  This
001145  ** avoids computing the same VIRTUAL column multiple times, and provides
001146  ** values for use by OP_Param opcodes in triggers.  Hence, if the
001147  ** input column is a VIRTUAL table, put it after all the other columns.
001148  **
001149  ** In the following, N means "normal column", S means STORED, and
001150  ** V means VIRTUAL.  Suppose the CREATE TABLE has columns like this:
001151  **
001152  **        CREATE TABLE ex(N,S,V,N,S,V,N,S,V);
001153  **                     -- 0 1 2 3 4 5 6 7 8
001154  **
001155  ** Then the mapping from this function is as follows:
001156  **
001157  **    INPUTS:     0 1 2 3 4 5 6 7 8
001158  **    OUTPUTS:    0 1 6 2 3 7 4 5 8
001159  **
001160  ** So, in other words, this routine shifts all the virtual columns to
001161  ** the end.
001162  **
001163  ** If SQLITE_OMIT_GENERATED_COLUMNS then there are no virtual columns and
001164  ** this routine is a no-op macro.  If the pTab does not have any virtual
001165  ** columns, then this routine is no-op that always return iCol.  If iCol
001166  ** is negative (indicating the ROWID column) then this routine return iCol.
001167  */
001168  i16 sqlite3TableColumnToStorage(Table *pTab, i16 iCol){
001169    int i;
001170    i16 n;
001171    assert( iCol<pTab->nCol );
001172    if( (pTab->tabFlags & TF_HasVirtual)==0 || iCol<0 ) return iCol;
001173    for(i=0, n=0; i<iCol; i++){
001174      if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ) n++;
001175    }
001176    if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ){
001177      /* iCol is a virtual column itself */
001178      return pTab->nNVCol + i - n;
001179    }else{
001180      /* iCol is a normal or stored column */
001181      return n;
001182    }
001183  }
001184  #endif
001185  
001186  /*
001187  ** Insert a single OP_JournalMode query opcode in order to force the
001188  ** prepared statement to return false for sqlite3_stmt_readonly().  This
001189  ** is used by CREATE TABLE IF NOT EXISTS and similar if the table already
001190  ** exists, so that the prepared statement for CREATE TABLE IF NOT EXISTS
001191  ** will return false for sqlite3_stmt_readonly() even if that statement
001192  ** is a read-only no-op.
001193  */
001194  static void sqlite3ForceNotReadOnly(Parse *pParse){
001195    int iReg = ++pParse->nMem;
001196    Vdbe *v = sqlite3GetVdbe(pParse);
001197    if( v ){
001198      sqlite3VdbeAddOp3(v, OP_JournalMode, 0, iReg, PAGER_JOURNALMODE_QUERY);
001199      sqlite3VdbeUsesBtree(v, 0);
001200    }
001201  }
001202  
001203  /*
001204  ** Begin constructing a new table representation in memory.  This is
001205  ** the first of several action routines that get called in response
001206  ** to a CREATE TABLE statement.  In particular, this routine is called
001207  ** after seeing tokens "CREATE" and "TABLE" and the table name. The isTemp
001208  ** flag is true if the table should be stored in the auxiliary database
001209  ** file instead of in the main database file.  This is normally the case
001210  ** when the "TEMP" or "TEMPORARY" keyword occurs in between
001211  ** CREATE and TABLE.
001212  **
001213  ** The new table record is initialized and put in pParse->pNewTable.
001214  ** As more of the CREATE TABLE statement is parsed, additional action
001215  ** routines will be called to add more information to this record.
001216  ** At the end of the CREATE TABLE statement, the sqlite3EndTable() routine
001217  ** is called to complete the construction of the new table record.
001218  */
001219  void sqlite3StartTable(
001220    Parse *pParse,   /* Parser context */
001221    Token *pName1,   /* First part of the name of the table or view */
001222    Token *pName2,   /* Second part of the name of the table or view */
001223    int isTemp,      /* True if this is a TEMP table */
001224    int isView,      /* True if this is a VIEW */
001225    int isVirtual,   /* True if this is a VIRTUAL table */
001226    int noErr        /* Do nothing if table already exists */
001227  ){
001228    Table *pTable;
001229    char *zName = 0; /* The name of the new table */
001230    sqlite3 *db = pParse->db;
001231    Vdbe *v;
001232    int iDb;         /* Database number to create the table in */
001233    Token *pName;    /* Unqualified name of the table to create */
001234  
001235    if( db->init.busy && db->init.newTnum==1 ){
001236      /* Special case:  Parsing the sqlite_schema or sqlite_temp_schema schema */
001237      iDb = db->init.iDb;
001238      zName = sqlite3DbStrDup(db, SCHEMA_TABLE(iDb));
001239      pName = pName1;
001240    }else{
001241      /* The common case */
001242      iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
001243      if( iDb<0 ) return;
001244      if( !OMIT_TEMPDB && isTemp && pName2->n>0 && iDb!=1 ){
001245        /* If creating a temp table, the name may not be qualified. Unless 
001246        ** the database name is "temp" anyway.  */
001247        sqlite3ErrorMsg(pParse, "temporary table name must be unqualified");
001248        return;
001249      }
001250      if( !OMIT_TEMPDB && isTemp ) iDb = 1;
001251      zName = sqlite3NameFromToken(db, pName);
001252      if( IN_RENAME_OBJECT ){
001253        sqlite3RenameTokenMap(pParse, (void*)zName, pName);
001254      }
001255    }
001256    pParse->sNameToken = *pName;
001257    if( zName==0 ) return;
001258    if( sqlite3CheckObjectName(pParse, zName, isView?"view":"table", zName) ){
001259      goto begin_table_error;
001260    }
001261    if( db->init.iDb==1 ) isTemp = 1;
001262  #ifndef SQLITE_OMIT_AUTHORIZATION
001263    assert( isTemp==0 || isTemp==1 );
001264    assert( isView==0 || isView==1 );
001265    {
001266      static const u8 aCode[] = {
001267         SQLITE_CREATE_TABLE,
001268         SQLITE_CREATE_TEMP_TABLE,
001269         SQLITE_CREATE_VIEW,
001270         SQLITE_CREATE_TEMP_VIEW
001271      };
001272      char *zDb = db->aDb[iDb].zDbSName;
001273      if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
001274        goto begin_table_error;
001275      }
001276      if( !isVirtual && sqlite3AuthCheck(pParse, (int)aCode[isTemp+2*isView],
001277                                         zName, 0, zDb) ){
001278        goto begin_table_error;
001279      }
001280    }
001281  #endif
001282  
001283    /* Make sure the new table name does not collide with an existing
001284    ** index or table name in the same database.  Issue an error message if
001285    ** it does. The exception is if the statement being parsed was passed
001286    ** to an sqlite3_declare_vtab() call. In that case only the column names
001287    ** and types will be used, so there is no need to test for namespace
001288    ** collisions.
001289    */
001290    if( !IN_SPECIAL_PARSE ){
001291      char *zDb = db->aDb[iDb].zDbSName;
001292      if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
001293        goto begin_table_error;
001294      }
001295      pTable = sqlite3FindTable(db, zName, zDb);
001296      if( pTable ){
001297        if( !noErr ){
001298          sqlite3ErrorMsg(pParse, "%s %T already exists",
001299                          (IsView(pTable)? "view" : "table"), pName);
001300        }else{
001301          assert( !db->init.busy || CORRUPT_DB );
001302          sqlite3CodeVerifySchema(pParse, iDb);
001303          sqlite3ForceNotReadOnly(pParse);
001304        }
001305        goto begin_table_error;
001306      }
001307      if( sqlite3FindIndex(db, zName, zDb)!=0 ){
001308        sqlite3ErrorMsg(pParse, "there is already an index named %s", zName);
001309        goto begin_table_error;
001310      }
001311    }
001312  
001313    pTable = sqlite3DbMallocZero(db, sizeof(Table));
001314    if( pTable==0 ){
001315      assert( db->mallocFailed );
001316      pParse->rc = SQLITE_NOMEM_BKPT;
001317      pParse->nErr++;
001318      goto begin_table_error;
001319    }
001320    pTable->zName = zName;
001321    pTable->iPKey = -1;
001322    pTable->pSchema = db->aDb[iDb].pSchema;
001323    pTable->nTabRef = 1;
001324  #ifdef SQLITE_DEFAULT_ROWEST
001325    pTable->nRowLogEst = sqlite3LogEst(SQLITE_DEFAULT_ROWEST);
001326  #else
001327    pTable->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
001328  #endif
001329    assert( pParse->pNewTable==0 );
001330    pParse->pNewTable = pTable;
001331  
001332    /* Begin generating the code that will insert the table record into
001333    ** the schema table.  Note in particular that we must go ahead
001334    ** and allocate the record number for the table entry now.  Before any
001335    ** PRIMARY KEY or UNIQUE keywords are parsed.  Those keywords will cause
001336    ** indices to be created and the table record must come before the 
001337    ** indices.  Hence, the record number for the table must be allocated
001338    ** now.
001339    */
001340    if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
001341      int addr1;
001342      int fileFormat;
001343      int reg1, reg2, reg3;
001344      /* nullRow[] is an OP_Record encoding of a row containing 5 NULLs */
001345      static const char nullRow[] = { 6, 0, 0, 0, 0, 0 };
001346      sqlite3BeginWriteOperation(pParse, 1, iDb);
001347  
001348  #ifndef SQLITE_OMIT_VIRTUALTABLE
001349      if( isVirtual ){
001350        sqlite3VdbeAddOp0(v, OP_VBegin);
001351      }
001352  #endif
001353  
001354      /* If the file format and encoding in the database have not been set, 
001355      ** set them now.
001356      */
001357      reg1 = pParse->regRowid = ++pParse->nMem;
001358      reg2 = pParse->regRoot = ++pParse->nMem;
001359      reg3 = ++pParse->nMem;
001360      sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, reg3, BTREE_FILE_FORMAT);
001361      sqlite3VdbeUsesBtree(v, iDb);
001362      addr1 = sqlite3VdbeAddOp1(v, OP_If, reg3); VdbeCoverage(v);
001363      fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ?
001364                    1 : SQLITE_MAX_FILE_FORMAT;
001365      sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, fileFormat);
001366      sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_TEXT_ENCODING, ENC(db));
001367      sqlite3VdbeJumpHere(v, addr1);
001368  
001369      /* This just creates a place-holder record in the sqlite_schema table.
001370      ** The record created does not contain anything yet.  It will be replaced
001371      ** by the real entry in code generated at sqlite3EndTable().
001372      **
001373      ** The rowid for the new entry is left in register pParse->regRowid.
001374      ** The root page number of the new table is left in reg pParse->regRoot.
001375      ** The rowid and root page number values are needed by the code that
001376      ** sqlite3EndTable will generate.
001377      */
001378  #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
001379      if( isView || isVirtual ){
001380        sqlite3VdbeAddOp2(v, OP_Integer, 0, reg2);
001381      }else
001382  #endif
001383      {
001384        assert( !pParse->bReturning );
001385        pParse->u1.addrCrTab =
001386           sqlite3VdbeAddOp3(v, OP_CreateBtree, iDb, reg2, BTREE_INTKEY);
001387      }
001388      sqlite3OpenSchemaTable(pParse, iDb);
001389      sqlite3VdbeAddOp2(v, OP_NewRowid, 0, reg1);
001390      sqlite3VdbeAddOp4(v, OP_Blob, 6, reg3, 0, nullRow, P4_STATIC);
001391      sqlite3VdbeAddOp3(v, OP_Insert, 0, reg3, reg1);
001392      sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
001393      sqlite3VdbeAddOp0(v, OP_Close);
001394    }
001395  
001396    /* Normal (non-error) return. */
001397    return;
001398  
001399    /* If an error occurs, we jump here */
001400  begin_table_error:
001401    pParse->checkSchema = 1;
001402    sqlite3DbFree(db, zName);
001403    return;
001404  }
001405  
001406  /* Set properties of a table column based on the (magical)
001407  ** name of the column.
001408  */
001409  #if SQLITE_ENABLE_HIDDEN_COLUMNS
001410  void sqlite3ColumnPropertiesFromName(Table *pTab, Column *pCol){
001411    if( sqlite3_strnicmp(pCol->zCnName, "__hidden__", 10)==0 ){
001412      pCol->colFlags |= COLFLAG_HIDDEN;
001413      if( pTab ) pTab->tabFlags |= TF_HasHidden;
001414    }else if( pTab && pCol!=pTab->aCol && (pCol[-1].colFlags & COLFLAG_HIDDEN) ){
001415      pTab->tabFlags |= TF_OOOHidden;
001416    }
001417  }
001418  #endif
001419  
001420  /*
001421  ** Name of the special TEMP trigger used to implement RETURNING.  The
001422  ** name begins with "sqlite_" so that it is guaranteed not to collide
001423  ** with any application-generated triggers.
001424  */
001425  #define RETURNING_TRIGGER_NAME  "sqlite_returning"
001426  
001427  /*
001428  ** Clean up the data structures associated with the RETURNING clause.
001429  */
001430  static void sqlite3DeleteReturning(sqlite3 *db, Returning *pRet){
001431    Hash *pHash;
001432    pHash = &(db->aDb[1].pSchema->trigHash);
001433    sqlite3HashInsert(pHash, RETURNING_TRIGGER_NAME, 0);
001434    sqlite3ExprListDelete(db, pRet->pReturnEL);
001435    sqlite3DbFree(db, pRet);
001436  }
001437  
001438  /*
001439  ** Add the RETURNING clause to the parse currently underway.
001440  **
001441  ** This routine creates a special TEMP trigger that will fire for each row
001442  ** of the DML statement.  That TEMP trigger contains a single SELECT
001443  ** statement with a result set that is the argument of the RETURNING clause.
001444  ** The trigger has the Trigger.bReturning flag and an opcode of
001445  ** TK_RETURNING instead of TK_SELECT, so that the trigger code generator
001446  ** knows to handle it specially.  The TEMP trigger is automatically
001447  ** removed at the end of the parse.
001448  **
001449  ** When this routine is called, we do not yet know if the RETURNING clause
001450  ** is attached to a DELETE, INSERT, or UPDATE, so construct it as a
001451  ** RETURNING trigger instead.  It will then be converted into the appropriate
001452  ** type on the first call to sqlite3TriggersExist().
001453  */
001454  void sqlite3AddReturning(Parse *pParse, ExprList *pList){
001455    Returning *pRet;
001456    Hash *pHash;
001457    sqlite3 *db = pParse->db;
001458    if( pParse->pNewTrigger ){
001459      sqlite3ErrorMsg(pParse, "cannot use RETURNING in a trigger");
001460    }else{
001461      assert( pParse->bReturning==0 );
001462    }
001463    pParse->bReturning = 1;
001464    pRet = sqlite3DbMallocZero(db, sizeof(*pRet));
001465    if( pRet==0 ){
001466      sqlite3ExprListDelete(db, pList);
001467      return;
001468    }
001469    pParse->u1.pReturning = pRet;
001470    pRet->pParse = pParse;
001471    pRet->pReturnEL = pList;
001472    sqlite3ParserAddCleanup(pParse,
001473       (void(*)(sqlite3*,void*))sqlite3DeleteReturning, pRet);
001474    testcase( pParse->earlyCleanup );
001475    if( db->mallocFailed ) return;
001476    pRet->retTrig.zName = RETURNING_TRIGGER_NAME;
001477    pRet->retTrig.op = TK_RETURNING;
001478    pRet->retTrig.tr_tm = TRIGGER_AFTER;
001479    pRet->retTrig.bReturning = 1;
001480    pRet->retTrig.pSchema = db->aDb[1].pSchema;
001481    pRet->retTrig.pTabSchema = db->aDb[1].pSchema;
001482    pRet->retTrig.step_list = &pRet->retTStep;
001483    pRet->retTStep.op = TK_RETURNING;
001484    pRet->retTStep.pTrig = &pRet->retTrig;
001485    pRet->retTStep.pExprList = pList;
001486    pHash = &(db->aDb[1].pSchema->trigHash);
001487    assert( sqlite3HashFind(pHash, RETURNING_TRIGGER_NAME)==0 || pParse->nErr );
001488    if( sqlite3HashInsert(pHash, RETURNING_TRIGGER_NAME, &pRet->retTrig)
001489            ==&pRet->retTrig ){
001490      sqlite3OomFault(db);
001491    }
001492  }
001493  
001494  /*
001495  ** Add a new column to the table currently being constructed.
001496  **
001497  ** The parser calls this routine once for each column declaration
001498  ** in a CREATE TABLE statement.  sqlite3StartTable() gets called
001499  ** first to get things going.  Then this routine is called for each
001500  ** column.
001501  */
001502  void sqlite3AddColumn(Parse *pParse, Token sName, Token sType){
001503    Table *p;
001504    int i;
001505    char *z;
001506    char *zType;
001507    Column *pCol;
001508    sqlite3 *db = pParse->db;
001509    u8 hName;
001510    Column *aNew;
001511    u8 eType = COLTYPE_CUSTOM;
001512    u8 szEst = 1;
001513    char affinity = SQLITE_AFF_BLOB;
001514  
001515    if( (p = pParse->pNewTable)==0 ) return;
001516    if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){
001517      sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName);
001518      return;
001519    }
001520    if( !IN_RENAME_OBJECT ) sqlite3DequoteToken(&sName);
001521  
001522    /* Because keywords GENERATE ALWAYS can be converted into indentifiers
001523    ** by the parser, we can sometimes end up with a typename that ends
001524    ** with "generated always".  Check for this case and omit the surplus
001525    ** text. */
001526    if( sType.n>=16
001527     && sqlite3_strnicmp(sType.z+(sType.n-6),"always",6)==0
001528    ){
001529      sType.n -= 6;
001530      while( ALWAYS(sType.n>0) && sqlite3Isspace(sType.z[sType.n-1]) ) sType.n--;
001531      if( sType.n>=9
001532       && sqlite3_strnicmp(sType.z+(sType.n-9),"generated",9)==0
001533      ){
001534        sType.n -= 9;
001535        while( sType.n>0 && sqlite3Isspace(sType.z[sType.n-1]) ) sType.n--;
001536      }
001537    }
001538  
001539    /* Check for standard typenames.  For standard typenames we will
001540    ** set the Column.eType field rather than storing the typename after
001541    ** the column name, in order to save space. */
001542    if( sType.n>=3 ){
001543      sqlite3DequoteToken(&sType);
001544      for(i=0; i<SQLITE_N_STDTYPE; i++){
001545         if( sType.n==sqlite3StdTypeLen[i]
001546          && sqlite3_strnicmp(sType.z, sqlite3StdType[i], sType.n)==0
001547         ){
001548           sType.n = 0;
001549           eType = i+1;
001550           affinity = sqlite3StdTypeAffinity[i];
001551           if( affinity<=SQLITE_AFF_TEXT ) szEst = 5;
001552           break;
001553         }
001554      }
001555    }
001556  
001557    z = sqlite3DbMallocRaw(db, (i64)sName.n + 1 + (i64)sType.n + (sType.n>0) );
001558    if( z==0 ) return;
001559    if( IN_RENAME_OBJECT ) sqlite3RenameTokenMap(pParse, (void*)z, &sName);
001560    memcpy(z, sName.z, sName.n);
001561    z[sName.n] = 0;
001562    sqlite3Dequote(z);
001563    hName = sqlite3StrIHash(z);
001564    for(i=0; i<p->nCol; i++){
001565      if( p->aCol[i].hName==hName && sqlite3StrICmp(z, p->aCol[i].zCnName)==0 ){
001566        sqlite3ErrorMsg(pParse, "duplicate column name: %s", z);
001567        sqlite3DbFree(db, z);
001568        return;
001569      }
001570    }
001571    aNew = sqlite3DbRealloc(db,p->aCol,((i64)p->nCol+1)*sizeof(p->aCol[0]));
001572    if( aNew==0 ){
001573      sqlite3DbFree(db, z);
001574      return;
001575    }
001576    p->aCol = aNew;
001577    pCol = &p->aCol[p->nCol];
001578    memset(pCol, 0, sizeof(p->aCol[0]));
001579    pCol->zCnName = z;
001580    pCol->hName = hName;
001581    sqlite3ColumnPropertiesFromName(p, pCol);
001582   
001583    if( sType.n==0 ){
001584      /* If there is no type specified, columns have the default affinity
001585      ** 'BLOB' with a default size of 4 bytes. */
001586      pCol->affinity = affinity;
001587      pCol->eCType = eType;
001588      pCol->szEst = szEst;
001589  #ifdef SQLITE_ENABLE_SORTER_REFERENCES
001590      if( affinity==SQLITE_AFF_BLOB ){
001591        if( 4>=sqlite3GlobalConfig.szSorterRef ){
001592          pCol->colFlags |= COLFLAG_SORTERREF;
001593        }
001594      }
001595  #endif
001596    }else{
001597      zType = z + sqlite3Strlen30(z) + 1;
001598      memcpy(zType, sType.z, sType.n);
001599      zType[sType.n] = 0;
001600      sqlite3Dequote(zType);
001601      pCol->affinity = sqlite3AffinityType(zType, pCol);
001602      pCol->colFlags |= COLFLAG_HASTYPE;
001603    }
001604    p->nCol++;
001605    p->nNVCol++;
001606    pParse->constraintName.n = 0;
001607  }
001608  
001609  /*
001610  ** This routine is called by the parser while in the middle of
001611  ** parsing a CREATE TABLE statement.  A "NOT NULL" constraint has
001612  ** been seen on a column.  This routine sets the notNull flag on
001613  ** the column currently under construction.
001614  */
001615  void sqlite3AddNotNull(Parse *pParse, int onError){
001616    Table *p;
001617    Column *pCol;
001618    p = pParse->pNewTable;
001619    if( p==0 || NEVER(p->nCol<1) ) return;
001620    pCol = &p->aCol[p->nCol-1];
001621    pCol->notNull = (u8)onError;
001622    p->tabFlags |= TF_HasNotNull;
001623  
001624    /* Set the uniqNotNull flag on any UNIQUE or PK indexes already created
001625    ** on this column.  */
001626    if( pCol->colFlags & COLFLAG_UNIQUE ){
001627      Index *pIdx;
001628      for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
001629        assert( pIdx->nKeyCol==1 && pIdx->onError!=OE_None );
001630        if( pIdx->aiColumn[0]==p->nCol-1 ){
001631          pIdx->uniqNotNull = 1;
001632        }
001633      }
001634    }
001635  }
001636  
001637  /*
001638  ** Scan the column type name zType (length nType) and return the
001639  ** associated affinity type.
001640  **
001641  ** This routine does a case-independent search of zType for the 
001642  ** substrings in the following table. If one of the substrings is
001643  ** found, the corresponding affinity is returned. If zType contains
001644  ** more than one of the substrings, entries toward the top of 
001645  ** the table take priority. For example, if zType is 'BLOBINT', 
001646  ** SQLITE_AFF_INTEGER is returned.
001647  **
001648  ** Substring     | Affinity
001649  ** --------------------------------
001650  ** 'INT'         | SQLITE_AFF_INTEGER
001651  ** 'CHAR'        | SQLITE_AFF_TEXT
001652  ** 'CLOB'        | SQLITE_AFF_TEXT
001653  ** 'TEXT'        | SQLITE_AFF_TEXT
001654  ** 'BLOB'        | SQLITE_AFF_BLOB
001655  ** 'REAL'        | SQLITE_AFF_REAL
001656  ** 'FLOA'        | SQLITE_AFF_REAL
001657  ** 'DOUB'        | SQLITE_AFF_REAL
001658  **
001659  ** If none of the substrings in the above table are found,
001660  ** SQLITE_AFF_NUMERIC is returned.
001661  */
001662  char sqlite3AffinityType(const char *zIn, Column *pCol){
001663    u32 h = 0;
001664    char aff = SQLITE_AFF_NUMERIC;
001665    const char *zChar = 0;
001666  
001667    assert( zIn!=0 );
001668    while( zIn[0] ){
001669      h = (h<<8) + sqlite3UpperToLower[(*zIn)&0xff];
001670      zIn++;
001671      if( h==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){             /* CHAR */
001672        aff = SQLITE_AFF_TEXT;
001673        zChar = zIn;
001674      }else if( h==(('c'<<24)+('l'<<16)+('o'<<8)+'b') ){       /* CLOB */
001675        aff = SQLITE_AFF_TEXT;
001676      }else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){       /* TEXT */
001677        aff = SQLITE_AFF_TEXT;
001678      }else if( h==(('b'<<24)+('l'<<16)+('o'<<8)+'b')          /* BLOB */
001679          && (aff==SQLITE_AFF_NUMERIC || aff==SQLITE_AFF_REAL) ){
001680        aff = SQLITE_AFF_BLOB;
001681        if( zIn[0]=='(' ) zChar = zIn;
001682  #ifndef SQLITE_OMIT_FLOATING_POINT
001683      }else if( h==(('r'<<24)+('e'<<16)+('a'<<8)+'l')          /* REAL */
001684          && aff==SQLITE_AFF_NUMERIC ){
001685        aff = SQLITE_AFF_REAL;
001686      }else if( h==(('f'<<24)+('l'<<16)+('o'<<8)+'a')          /* FLOA */
001687          && aff==SQLITE_AFF_NUMERIC ){
001688        aff = SQLITE_AFF_REAL;
001689      }else if( h==(('d'<<24)+('o'<<16)+('u'<<8)+'b')          /* DOUB */
001690          && aff==SQLITE_AFF_NUMERIC ){
001691        aff = SQLITE_AFF_REAL;
001692  #endif
001693      }else if( (h&0x00FFFFFF)==(('i'<<16)+('n'<<8)+'t') ){    /* INT */
001694        aff = SQLITE_AFF_INTEGER;
001695        break;
001696      }
001697    }
001698  
001699    /* If pCol is not NULL, store an estimate of the field size.  The
001700    ** estimate is scaled so that the size of an integer is 1.  */
001701    if( pCol ){
001702      int v = 0;   /* default size is approx 4 bytes */
001703      if( aff<SQLITE_AFF_NUMERIC ){
001704        if( zChar ){
001705          while( zChar[0] ){
001706            if( sqlite3Isdigit(zChar[0]) ){
001707              /* BLOB(k), VARCHAR(k), CHAR(k) -> r=(k/4+1) */
001708              sqlite3GetInt32(zChar, &v);
001709              break;
001710            }
001711            zChar++;
001712          }
001713        }else{
001714          v = 16;   /* BLOB, TEXT, CLOB -> r=5  (approx 20 bytes)*/
001715        }
001716      }
001717  #ifdef SQLITE_ENABLE_SORTER_REFERENCES
001718      if( v>=sqlite3GlobalConfig.szSorterRef ){
001719        pCol->colFlags |= COLFLAG_SORTERREF;
001720      }
001721  #endif
001722      v = v/4 + 1;
001723      if( v>255 ) v = 255;
001724      pCol->szEst = v;
001725    }
001726    return aff;
001727  }
001728  
001729  /*
001730  ** The expression is the default value for the most recently added column
001731  ** of the table currently under construction.
001732  **
001733  ** Default value expressions must be constant.  Raise an exception if this
001734  ** is not the case.
001735  **
001736  ** This routine is called by the parser while in the middle of
001737  ** parsing a CREATE TABLE statement.
001738  */
001739  void sqlite3AddDefaultValue(
001740    Parse *pParse,           /* Parsing context */
001741    Expr *pExpr,             /* The parsed expression of the default value */
001742    const char *zStart,      /* Start of the default value text */
001743    const char *zEnd         /* First character past end of defaut value text */
001744  ){
001745    Table *p;
001746    Column *pCol;
001747    sqlite3 *db = pParse->db;
001748    p = pParse->pNewTable;
001749    if( p!=0 ){
001750      int isInit = db->init.busy && db->init.iDb!=1;
001751      pCol = &(p->aCol[p->nCol-1]);
001752      if( !sqlite3ExprIsConstantOrFunction(pExpr, isInit) ){
001753        sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant",
001754            pCol->zCnName);
001755  #ifndef SQLITE_OMIT_GENERATED_COLUMNS
001756      }else if( pCol->colFlags & COLFLAG_GENERATED ){
001757        testcase( pCol->colFlags & COLFLAG_VIRTUAL );
001758        testcase( pCol->colFlags & COLFLAG_STORED );
001759        sqlite3ErrorMsg(pParse, "cannot use DEFAULT on a generated column");
001760  #endif
001761      }else{
001762        /* A copy of pExpr is used instead of the original, as pExpr contains
001763        ** tokens that point to volatile memory.
001764        */
001765        Expr x, *pDfltExpr;
001766        memset(&x, 0, sizeof(x));
001767        x.op = TK_SPAN;
001768        x.u.zToken = sqlite3DbSpanDup(db, zStart, zEnd);
001769        x.pLeft = pExpr;
001770        x.flags = EP_Skip;
001771        pDfltExpr = sqlite3ExprDup(db, &x, EXPRDUP_REDUCE);
001772        sqlite3DbFree(db, x.u.zToken);
001773        sqlite3ColumnSetExpr(pParse, p, pCol, pDfltExpr);
001774      }
001775    }
001776    if( IN_RENAME_OBJECT ){
001777      sqlite3RenameExprUnmap(pParse, pExpr);
001778    }
001779    sqlite3ExprDelete(db, pExpr);
001780  }
001781  
001782  /*
001783  ** Backwards Compatibility Hack:
001784  ** 
001785  ** Historical versions of SQLite accepted strings as column names in
001786  ** indexes and PRIMARY KEY constraints and in UNIQUE constraints.  Example:
001787  **
001788  **     CREATE TABLE xyz(a,b,c,d,e,PRIMARY KEY('a'),UNIQUE('b','c' COLLATE trim)
001789  **     CREATE INDEX abc ON xyz('c','d' DESC,'e' COLLATE nocase DESC);
001790  **
001791  ** This is goofy.  But to preserve backwards compatibility we continue to
001792  ** accept it.  This routine does the necessary conversion.  It converts
001793  ** the expression given in its argument from a TK_STRING into a TK_ID
001794  ** if the expression is just a TK_STRING with an optional COLLATE clause.
001795  ** If the expression is anything other than TK_STRING, the expression is
001796  ** unchanged.
001797  */
001798  static void sqlite3StringToId(Expr *p){
001799    if( p->op==TK_STRING ){
001800      p->op = TK_ID;
001801    }else if( p->op==TK_COLLATE && p->pLeft->op==TK_STRING ){
001802      p->pLeft->op = TK_ID;
001803    }
001804  }
001805  
001806  /*
001807  ** Tag the given column as being part of the PRIMARY KEY
001808  */
001809  static void makeColumnPartOfPrimaryKey(Parse *pParse, Column *pCol){
001810    pCol->colFlags |= COLFLAG_PRIMKEY;
001811  #ifndef SQLITE_OMIT_GENERATED_COLUMNS
001812    if( pCol->colFlags & COLFLAG_GENERATED ){
001813      testcase( pCol->colFlags & COLFLAG_VIRTUAL );
001814      testcase( pCol->colFlags & COLFLAG_STORED );
001815      sqlite3ErrorMsg(pParse,
001816        "generated columns cannot be part of the PRIMARY KEY");
001817    }
001818  #endif          
001819  }
001820  
001821  /*
001822  ** Designate the PRIMARY KEY for the table.  pList is a list of names 
001823  ** of columns that form the primary key.  If pList is NULL, then the
001824  ** most recently added column of the table is the primary key.
001825  **
001826  ** A table can have at most one primary key.  If the table already has
001827  ** a primary key (and this is the second primary key) then create an
001828  ** error.
001829  **
001830  ** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
001831  ** then we will try to use that column as the rowid.  Set the Table.iPKey
001832  ** field of the table under construction to be the index of the
001833  ** INTEGER PRIMARY KEY column.  Table.iPKey is set to -1 if there is
001834  ** no INTEGER PRIMARY KEY.
001835  **
001836  ** If the key is not an INTEGER PRIMARY KEY, then create a unique
001837  ** index for the key.  No index is created for INTEGER PRIMARY KEYs.
001838  */
001839  void sqlite3AddPrimaryKey(
001840    Parse *pParse,    /* Parsing context */
001841    ExprList *pList,  /* List of field names to be indexed */
001842    int onError,      /* What to do with a uniqueness conflict */
001843    int autoInc,      /* True if the AUTOINCREMENT keyword is present */
001844    int sortOrder     /* SQLITE_SO_ASC or SQLITE_SO_DESC */
001845  ){
001846    Table *pTab = pParse->pNewTable;
001847    Column *pCol = 0;
001848    int iCol = -1, i;
001849    int nTerm;
001850    if( pTab==0 ) goto primary_key_exit;
001851    if( pTab->tabFlags & TF_HasPrimaryKey ){
001852      sqlite3ErrorMsg(pParse, 
001853        "table \"%s\" has more than one primary key", pTab->zName);
001854      goto primary_key_exit;
001855    }
001856    pTab->tabFlags |= TF_HasPrimaryKey;
001857    if( pList==0 ){
001858      iCol = pTab->nCol - 1;
001859      pCol = &pTab->aCol[iCol];
001860      makeColumnPartOfPrimaryKey(pParse, pCol);
001861      nTerm = 1;
001862    }else{
001863      nTerm = pList->nExpr;
001864      for(i=0; i<nTerm; i++){
001865        Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[i].pExpr);
001866        assert( pCExpr!=0 );
001867        sqlite3StringToId(pCExpr);
001868        if( pCExpr->op==TK_ID ){
001869          const char *zCName;
001870          assert( !ExprHasProperty(pCExpr, EP_IntValue) );
001871          zCName = pCExpr->u.zToken;
001872          for(iCol=0; iCol<pTab->nCol; iCol++){
001873            if( sqlite3StrICmp(zCName, pTab->aCol[iCol].zCnName)==0 ){
001874              pCol = &pTab->aCol[iCol];
001875              makeColumnPartOfPrimaryKey(pParse, pCol);
001876              break;
001877            }
001878          }
001879        }
001880      }
001881    }
001882    if( nTerm==1
001883     && pCol
001884     && pCol->eCType==COLTYPE_INTEGER
001885     && sortOrder!=SQLITE_SO_DESC
001886    ){
001887      if( IN_RENAME_OBJECT && pList ){
001888        Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[0].pExpr);
001889        sqlite3RenameTokenRemap(pParse, &pTab->iPKey, pCExpr);
001890      }
001891      pTab->iPKey = iCol;
001892      pTab->keyConf = (u8)onError;
001893      assert( autoInc==0 || autoInc==1 );
001894      pTab->tabFlags |= autoInc*TF_Autoincrement;
001895      if( pList ) pParse->iPkSortOrder = pList->a[0].sortFlags;
001896      (void)sqlite3HasExplicitNulls(pParse, pList);
001897    }else if( autoInc ){
001898  #ifndef SQLITE_OMIT_AUTOINCREMENT
001899      sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an "
001900         "INTEGER PRIMARY KEY");
001901  #endif
001902    }else{
001903      sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0,
001904                             0, sortOrder, 0, SQLITE_IDXTYPE_PRIMARYKEY);
001905      pList = 0;
001906    }
001907  
001908  primary_key_exit:
001909    sqlite3ExprListDelete(pParse->db, pList);
001910    return;
001911  }
001912  
001913  /*
001914  ** Add a new CHECK constraint to the table currently under construction.
001915  */
001916  void sqlite3AddCheckConstraint(
001917    Parse *pParse,      /* Parsing context */
001918    Expr *pCheckExpr,   /* The check expression */
001919    const char *zStart, /* Opening "(" */
001920    const char *zEnd    /* Closing ")" */
001921  ){
001922  #ifndef SQLITE_OMIT_CHECK
001923    Table *pTab = pParse->pNewTable;
001924    sqlite3 *db = pParse->db;
001925    if( pTab && !IN_DECLARE_VTAB
001926     && !sqlite3BtreeIsReadonly(db->aDb[db->init.iDb].pBt)
001927    ){
001928      pTab->pCheck = sqlite3ExprListAppend(pParse, pTab->pCheck, pCheckExpr);
001929      if( pParse->constraintName.n ){
001930        sqlite3ExprListSetName(pParse, pTab->pCheck, &pParse->constraintName, 1);
001931      }else{
001932        Token t;
001933        for(zStart++; sqlite3Isspace(zStart[0]); zStart++){}
001934        while( sqlite3Isspace(zEnd[-1]) ){ zEnd--; }
001935        t.z = zStart;
001936        t.n = (int)(zEnd - t.z);
001937        sqlite3ExprListSetName(pParse, pTab->pCheck, &t, 1);    
001938      }
001939    }else
001940  #endif
001941    {
001942      sqlite3ExprDelete(pParse->db, pCheckExpr);
001943    }
001944  }
001945  
001946  /*
001947  ** Set the collation function of the most recently parsed table column
001948  ** to the CollSeq given.
001949  */
001950  void sqlite3AddCollateType(Parse *pParse, Token *pToken){
001951    Table *p;
001952    int i;
001953    char *zColl;              /* Dequoted name of collation sequence */
001954    sqlite3 *db;
001955  
001956    if( (p = pParse->pNewTable)==0 || IN_RENAME_OBJECT ) return;
001957    i = p->nCol-1;
001958    db = pParse->db;
001959    zColl = sqlite3NameFromToken(db, pToken);
001960    if( !zColl ) return;
001961  
001962    if( sqlite3LocateCollSeq(pParse, zColl) ){
001963      Index *pIdx;
001964      sqlite3ColumnSetColl(db, &p->aCol[i], zColl);
001965    
001966      /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>",
001967      ** then an index may have been created on this column before the
001968      ** collation type was added. Correct this if it is the case.
001969      */
001970      for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
001971        assert( pIdx->nKeyCol==1 );
001972        if( pIdx->aiColumn[0]==i ){
001973          pIdx->azColl[0] = sqlite3ColumnColl(&p->aCol[i]);
001974        }
001975      }
001976    }
001977    sqlite3DbFree(db, zColl);
001978  }
001979  
001980  /* Change the most recently parsed column to be a GENERATED ALWAYS AS
001981  ** column.
001982  */
001983  void sqlite3AddGenerated(Parse *pParse, Expr *pExpr, Token *pType){
001984  #ifndef SQLITE_OMIT_GENERATED_COLUMNS
001985    u8 eType = COLFLAG_VIRTUAL;
001986    Table *pTab = pParse->pNewTable;
001987    Column *pCol;
001988    if( pTab==0 ){
001989      /* generated column in an CREATE TABLE IF NOT EXISTS that already exists */
001990      goto generated_done;
001991    }
001992    pCol = &(pTab->aCol[pTab->nCol-1]);
001993    if( IN_DECLARE_VTAB ){
001994      sqlite3ErrorMsg(pParse, "virtual tables cannot use computed columns");
001995      goto generated_done;
001996    }
001997    if( pCol->iDflt>0 ) goto generated_error;
001998    if( pType ){
001999      if( pType->n==7 && sqlite3StrNICmp("virtual",pType->z,7)==0 ){
002000        /* no-op */
002001      }else if( pType->n==6 && sqlite3StrNICmp("stored",pType->z,6)==0 ){
002002        eType = COLFLAG_STORED;
002003      }else{
002004        goto generated_error;
002005      }
002006    }
002007    if( eType==COLFLAG_VIRTUAL ) pTab->nNVCol--;
002008    pCol->colFlags |= eType;
002009    assert( TF_HasVirtual==COLFLAG_VIRTUAL );
002010    assert( TF_HasStored==COLFLAG_STORED );
002011    pTab->tabFlags |= eType;
002012    if( pCol->colFlags & COLFLAG_PRIMKEY ){
002013      makeColumnPartOfPrimaryKey(pParse, pCol); /* For the error message */
002014    }
002015    sqlite3ColumnSetExpr(pParse, pTab, pCol, pExpr);
002016    pExpr = 0;
002017    goto generated_done;
002018  
002019  generated_error:
002020    sqlite3ErrorMsg(pParse, "error in generated column \"%s\"",
002021                    pCol->zCnName);
002022  generated_done:
002023    sqlite3ExprDelete(pParse->db, pExpr);
002024  #else
002025    /* Throw and error for the GENERATED ALWAYS AS clause if the
002026    ** SQLITE_OMIT_GENERATED_COLUMNS compile-time option is used. */
002027    sqlite3ErrorMsg(pParse, "generated columns not supported");
002028    sqlite3ExprDelete(pParse->db, pExpr);
002029  #endif
002030  }
002031  
002032  /*
002033  ** Generate code that will increment the schema cookie.
002034  **
002035  ** The schema cookie is used to determine when the schema for the
002036  ** database changes.  After each schema change, the cookie value
002037  ** changes.  When a process first reads the schema it records the
002038  ** cookie.  Thereafter, whenever it goes to access the database,
002039  ** it checks the cookie to make sure the schema has not changed
002040  ** since it was last read.
002041  **
002042  ** This plan is not completely bullet-proof.  It is possible for
002043  ** the schema to change multiple times and for the cookie to be
002044  ** set back to prior value.  But schema changes are infrequent
002045  ** and the probability of hitting the same cookie value is only
002046  ** 1 chance in 2^32.  So we're safe enough.
002047  **
002048  ** IMPLEMENTATION-OF: R-34230-56049 SQLite automatically increments
002049  ** the schema-version whenever the schema changes.
002050  */
002051  void sqlite3ChangeCookie(Parse *pParse, int iDb){
002052    sqlite3 *db = pParse->db;
002053    Vdbe *v = pParse->pVdbe;
002054    assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
002055    sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_SCHEMA_VERSION, 
002056                     (int)(1+(unsigned)db->aDb[iDb].pSchema->schema_cookie));
002057  }
002058  
002059  /*
002060  ** Measure the number of characters needed to output the given
002061  ** identifier.  The number returned includes any quotes used
002062  ** but does not include the null terminator.
002063  **
002064  ** The estimate is conservative.  It might be larger that what is
002065  ** really needed.
002066  */
002067  static int identLength(const char *z){
002068    int n;
002069    for(n=0; *z; n++, z++){
002070      if( *z=='"' ){ n++; }
002071    }
002072    return n + 2;
002073  }
002074  
002075  /*
002076  ** The first parameter is a pointer to an output buffer. The second 
002077  ** parameter is a pointer to an integer that contains the offset at
002078  ** which to write into the output buffer. This function copies the
002079  ** nul-terminated string pointed to by the third parameter, zSignedIdent,
002080  ** to the specified offset in the buffer and updates *pIdx to refer
002081  ** to the first byte after the last byte written before returning.
002082  ** 
002083  ** If the string zSignedIdent consists entirely of alpha-numeric
002084  ** characters, does not begin with a digit and is not an SQL keyword,
002085  ** then it is copied to the output buffer exactly as it is. Otherwise,
002086  ** it is quoted using double-quotes.
002087  */
002088  static void identPut(char *z, int *pIdx, char *zSignedIdent){
002089    unsigned char *zIdent = (unsigned char*)zSignedIdent;
002090    int i, j, needQuote;
002091    i = *pIdx;
002092  
002093    for(j=0; zIdent[j]; j++){
002094      if( !sqlite3Isalnum(zIdent[j]) && zIdent[j]!='_' ) break;
002095    }
002096    needQuote = sqlite3Isdigit(zIdent[0])
002097              || sqlite3KeywordCode(zIdent, j)!=TK_ID
002098              || zIdent[j]!=0
002099              || j==0;
002100  
002101    if( needQuote ) z[i++] = '"';
002102    for(j=0; zIdent[j]; j++){
002103      z[i++] = zIdent[j];
002104      if( zIdent[j]=='"' ) z[i++] = '"';
002105    }
002106    if( needQuote ) z[i++] = '"';
002107    z[i] = 0;
002108    *pIdx = i;
002109  }
002110  
002111  /*
002112  ** Generate a CREATE TABLE statement appropriate for the given
002113  ** table.  Memory to hold the text of the statement is obtained
002114  ** from sqliteMalloc() and must be freed by the calling function.
002115  */
002116  static char *createTableStmt(sqlite3 *db, Table *p){
002117    int i, k, n;
002118    char *zStmt;
002119    char *zSep, *zSep2, *zEnd;
002120    Column *pCol;
002121    n = 0;
002122    for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){
002123      n += identLength(pCol->zCnName) + 5;
002124    }
002125    n += identLength(p->zName);
002126    if( n<50 ){ 
002127      zSep = "";
002128      zSep2 = ",";
002129      zEnd = ")";
002130    }else{
002131      zSep = "\n  ";
002132      zSep2 = ",\n  ";
002133      zEnd = "\n)";
002134    }
002135    n += 35 + 6*p->nCol;
002136    zStmt = sqlite3DbMallocRaw(0, n);
002137    if( zStmt==0 ){
002138      sqlite3OomFault(db);
002139      return 0;
002140    }
002141    sqlite3_snprintf(n, zStmt, "CREATE TABLE ");
002142    k = sqlite3Strlen30(zStmt);
002143    identPut(zStmt, &k, p->zName);
002144    zStmt[k++] = '(';
002145    for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){
002146      static const char * const azType[] = {
002147          /* SQLITE_AFF_BLOB    */ "",
002148          /* SQLITE_AFF_TEXT    */ " TEXT",
002149          /* SQLITE_AFF_NUMERIC */ " NUM",
002150          /* SQLITE_AFF_INTEGER */ " INT",
002151          /* SQLITE_AFF_REAL    */ " REAL"
002152      };
002153      int len;
002154      const char *zType;
002155  
002156      sqlite3_snprintf(n-k, &zStmt[k], zSep);
002157      k += sqlite3Strlen30(&zStmt[k]);
002158      zSep = zSep2;
002159      identPut(zStmt, &k, pCol->zCnName);
002160      assert( pCol->affinity-SQLITE_AFF_BLOB >= 0 );
002161      assert( pCol->affinity-SQLITE_AFF_BLOB < ArraySize(azType) );
002162      testcase( pCol->affinity==SQLITE_AFF_BLOB );
002163      testcase( pCol->affinity==SQLITE_AFF_TEXT );
002164      testcase( pCol->affinity==SQLITE_AFF_NUMERIC );
002165      testcase( pCol->affinity==SQLITE_AFF_INTEGER );
002166      testcase( pCol->affinity==SQLITE_AFF_REAL );
002167      
002168      zType = azType[pCol->affinity - SQLITE_AFF_BLOB];
002169      len = sqlite3Strlen30(zType);
002170      assert( pCol->affinity==SQLITE_AFF_BLOB 
002171              || pCol->affinity==sqlite3AffinityType(zType, 0) );
002172      memcpy(&zStmt[k], zType, len);
002173      k += len;
002174      assert( k<=n );
002175    }
002176    sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd);
002177    return zStmt;
002178  }
002179  
002180  /*
002181  ** Resize an Index object to hold N columns total.  Return SQLITE_OK
002182  ** on success and SQLITE_NOMEM on an OOM error.
002183  */
002184  static int resizeIndexObject(sqlite3 *db, Index *pIdx, int N){
002185    char *zExtra;
002186    int nByte;
002187    if( pIdx->nColumn>=N ) return SQLITE_OK;
002188    assert( pIdx->isResized==0 );
002189    nByte = (sizeof(char*) + sizeof(LogEst) + sizeof(i16) + 1)*N;
002190    zExtra = sqlite3DbMallocZero(db, nByte);
002191    if( zExtra==0 ) return SQLITE_NOMEM_BKPT;
002192    memcpy(zExtra, pIdx->azColl, sizeof(char*)*pIdx->nColumn);
002193    pIdx->azColl = (const char**)zExtra;
002194    zExtra += sizeof(char*)*N;
002195    memcpy(zExtra, pIdx->aiRowLogEst, sizeof(LogEst)*(pIdx->nKeyCol+1));
002196    pIdx->aiRowLogEst = (LogEst*)zExtra;
002197    zExtra += sizeof(LogEst)*N;
002198    memcpy(zExtra, pIdx->aiColumn, sizeof(i16)*pIdx->nColumn);
002199    pIdx->aiColumn = (i16*)zExtra;
002200    zExtra += sizeof(i16)*N;
002201    memcpy(zExtra, pIdx->aSortOrder, pIdx->nColumn);
002202    pIdx->aSortOrder = (u8*)zExtra;
002203    pIdx->nColumn = N;
002204    pIdx->isResized = 1;
002205    return SQLITE_OK;
002206  }
002207  
002208  /*
002209  ** Estimate the total row width for a table.
002210  */
002211  static void estimateTableWidth(Table *pTab){
002212    unsigned wTable = 0;
002213    const Column *pTabCol;
002214    int i;
002215    for(i=pTab->nCol, pTabCol=pTab->aCol; i>0; i--, pTabCol++){
002216      wTable += pTabCol->szEst;
002217    }
002218    if( pTab->iPKey<0 ) wTable++;
002219    pTab->szTabRow = sqlite3LogEst(wTable*4);
002220  }
002221  
002222  /*
002223  ** Estimate the average size of a row for an index.
002224  */
002225  static void estimateIndexWidth(Index *pIdx){
002226    unsigned wIndex = 0;
002227    int i;
002228    const Column *aCol = pIdx->pTable->aCol;
002229    for(i=0; i<pIdx->nColumn; i++){
002230      i16 x = pIdx->aiColumn[i];
002231      assert( x<pIdx->pTable->nCol );
002232      wIndex += x<0 ? 1 : aCol[pIdx->aiColumn[i]].szEst;
002233    }
002234    pIdx->szIdxRow = sqlite3LogEst(wIndex*4);
002235  }
002236  
002237  /* Return true if column number x is any of the first nCol entries of aiCol[].
002238  ** This is used to determine if the column number x appears in any of the
002239  ** first nCol entries of an index.
002240  */
002241  static int hasColumn(const i16 *aiCol, int nCol, int x){
002242    while( nCol-- > 0 ){
002243      if( x==*(aiCol++) ){
002244        return 1;
002245      }
002246    }
002247    return 0;
002248  }
002249  
002250  /*
002251  ** Return true if any of the first nKey entries of index pIdx exactly
002252  ** match the iCol-th entry of pPk.  pPk is always a WITHOUT ROWID
002253  ** PRIMARY KEY index.  pIdx is an index on the same table.  pIdx may
002254  ** or may not be the same index as pPk.
002255  **
002256  ** The first nKey entries of pIdx are guaranteed to be ordinary columns,
002257  ** not a rowid or expression.
002258  **
002259  ** This routine differs from hasColumn() in that both the column and the
002260  ** collating sequence must match for this routine, but for hasColumn() only
002261  ** the column name must match.
002262  */
002263  static int isDupColumn(Index *pIdx, int nKey, Index *pPk, int iCol){
002264    int i, j;
002265    assert( nKey<=pIdx->nColumn );
002266    assert( iCol<MAX(pPk->nColumn,pPk->nKeyCol) );
002267    assert( pPk->idxType==SQLITE_IDXTYPE_PRIMARYKEY );
002268    assert( pPk->pTable->tabFlags & TF_WithoutRowid );
002269    assert( pPk->pTable==pIdx->pTable );
002270    testcase( pPk==pIdx );
002271    j = pPk->aiColumn[iCol];
002272    assert( j!=XN_ROWID && j!=XN_EXPR );
002273    for(i=0; i<nKey; i++){
002274      assert( pIdx->aiColumn[i]>=0 || j>=0 );
002275      if( pIdx->aiColumn[i]==j 
002276       && sqlite3StrICmp(pIdx->azColl[i], pPk->azColl[iCol])==0
002277      ){
002278        return 1;
002279      }
002280    }
002281    return 0;
002282  }
002283  
002284  /* Recompute the colNotIdxed field of the Index.
002285  **
002286  ** colNotIdxed is a bitmask that has a 0 bit representing each indexed
002287  ** columns that are within the first 63 columns of the table.  The
002288  ** high-order bit of colNotIdxed is always 1.  All unindexed columns
002289  ** of the table have a 1.
002290  **
002291  ** 2019-10-24:  For the purpose of this computation, virtual columns are
002292  ** not considered to be covered by the index, even if they are in the
002293  ** index, because we do not trust the logic in whereIndexExprTrans() to be
002294  ** able to find all instances of a reference to the indexed table column
002295  ** and convert them into references to the index.  Hence we always want
002296  ** the actual table at hand in order to recompute the virtual column, if
002297  ** necessary.
002298  **
002299  ** The colNotIdxed mask is AND-ed with the SrcList.a[].colUsed mask
002300  ** to determine if the index is covering index.
002301  */
002302  static void recomputeColumnsNotIndexed(Index *pIdx){
002303    Bitmask m = 0;
002304    int j;
002305    Table *pTab = pIdx->pTable;
002306    for(j=pIdx->nColumn-1; j>=0; j--){
002307      int x = pIdx->aiColumn[j];
002308      if( x>=0 && (pTab->aCol[x].colFlags & COLFLAG_VIRTUAL)==0 ){
002309        testcase( x==BMS-1 );
002310        testcase( x==BMS-2 );
002311        if( x<BMS-1 ) m |= MASKBIT(x);
002312      }
002313    }
002314    pIdx->colNotIdxed = ~m;
002315    assert( (pIdx->colNotIdxed>>63)==1 );
002316  }
002317  
002318  /*
002319  ** This routine runs at the end of parsing a CREATE TABLE statement that
002320  ** has a WITHOUT ROWID clause.  The job of this routine is to convert both
002321  ** internal schema data structures and the generated VDBE code so that they
002322  ** are appropriate for a WITHOUT ROWID table instead of a rowid table.
002323  ** Changes include:
002324  **
002325  **     (1)  Set all columns of the PRIMARY KEY schema object to be NOT NULL.
002326  **     (2)  Convert P3 parameter of the OP_CreateBtree from BTREE_INTKEY 
002327  **          into BTREE_BLOBKEY.
002328  **     (3)  Bypass the creation of the sqlite_schema table entry
002329  **          for the PRIMARY KEY as the primary key index is now
002330  **          identified by the sqlite_schema table entry of the table itself.
002331  **     (4)  Set the Index.tnum of the PRIMARY KEY Index object in the
002332  **          schema to the rootpage from the main table.
002333  **     (5)  Add all table columns to the PRIMARY KEY Index object
002334  **          so that the PRIMARY KEY is a covering index.  The surplus
002335  **          columns are part of KeyInfo.nAllField and are not used for
002336  **          sorting or lookup or uniqueness checks.
002337  **     (6)  Replace the rowid tail on all automatically generated UNIQUE
002338  **          indices with the PRIMARY KEY columns.
002339  **
002340  ** For virtual tables, only (1) is performed.
002341  */
002342  static void convertToWithoutRowidTable(Parse *pParse, Table *pTab){
002343    Index *pIdx;
002344    Index *pPk;
002345    int nPk;
002346    int nExtra;
002347    int i, j;
002348    sqlite3 *db = pParse->db;
002349    Vdbe *v = pParse->pVdbe;
002350  
002351    /* Mark every PRIMARY KEY column as NOT NULL (except for imposter tables)
002352    */
002353    if( !db->init.imposterTable ){
002354      for(i=0; i<pTab->nCol; i++){
002355        if( (pTab->aCol[i].colFlags & COLFLAG_PRIMKEY)!=0
002356         && (pTab->aCol[i].notNull==OE_None)
002357        ){
002358          pTab->aCol[i].notNull = OE_Abort;
002359        }
002360      }
002361      pTab->tabFlags |= TF_HasNotNull;
002362    }
002363  
002364    /* Convert the P3 operand of the OP_CreateBtree opcode from BTREE_INTKEY
002365    ** into BTREE_BLOBKEY.
002366    */
002367    assert( !pParse->bReturning );
002368    if( pParse->u1.addrCrTab ){
002369      assert( v );
002370      sqlite3VdbeChangeP3(v, pParse->u1.addrCrTab, BTREE_BLOBKEY);
002371    }
002372  
002373    /* Locate the PRIMARY KEY index.  Or, if this table was originally
002374    ** an INTEGER PRIMARY KEY table, create a new PRIMARY KEY index. 
002375    */
002376    if( pTab->iPKey>=0 ){
002377      ExprList *pList;
002378      Token ipkToken;
002379      sqlite3TokenInit(&ipkToken, pTab->aCol[pTab->iPKey].zCnName);
002380      pList = sqlite3ExprListAppend(pParse, 0, 
002381                    sqlite3ExprAlloc(db, TK_ID, &ipkToken, 0));
002382      if( pList==0 ){
002383        pTab->tabFlags &= ~TF_WithoutRowid;
002384        return;
002385      }
002386      if( IN_RENAME_OBJECT ){
002387        sqlite3RenameTokenRemap(pParse, pList->a[0].pExpr, &pTab->iPKey);
002388      }
002389      pList->a[0].sortFlags = pParse->iPkSortOrder;
002390      assert( pParse->pNewTable==pTab );
002391      pTab->iPKey = -1;
002392      sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0,
002393                         SQLITE_IDXTYPE_PRIMARYKEY);
002394      if( pParse->nErr ){
002395        pTab->tabFlags &= ~TF_WithoutRowid;
002396        return;
002397      }
002398      assert( db->mallocFailed==0 );
002399      pPk = sqlite3PrimaryKeyIndex(pTab);
002400      assert( pPk->nKeyCol==1 );
002401    }else{
002402      pPk = sqlite3PrimaryKeyIndex(pTab);
002403      assert( pPk!=0 );
002404  
002405      /*
002406      ** Remove all redundant columns from the PRIMARY KEY.  For example, change
002407      ** "PRIMARY KEY(a,b,a,b,c,b,c,d)" into just "PRIMARY KEY(a,b,c,d)".  Later
002408      ** code assumes the PRIMARY KEY contains no repeated columns.
002409      */
002410      for(i=j=1; i<pPk->nKeyCol; i++){
002411        if( isDupColumn(pPk, j, pPk, i) ){
002412          pPk->nColumn--;
002413        }else{
002414          testcase( hasColumn(pPk->aiColumn, j, pPk->aiColumn[i]) );
002415          pPk->azColl[j] = pPk->azColl[i];
002416          pPk->aSortOrder[j] = pPk->aSortOrder[i];
002417          pPk->aiColumn[j++] = pPk->aiColumn[i];
002418        }
002419      }
002420      pPk->nKeyCol = j;
002421    }
002422    assert( pPk!=0 );
002423    pPk->isCovering = 1;
002424    if( !db->init.imposterTable ) pPk->uniqNotNull = 1;
002425    nPk = pPk->nColumn = pPk->nKeyCol;
002426  
002427    /* Bypass the creation of the PRIMARY KEY btree and the sqlite_schema
002428    ** table entry. This is only required if currently generating VDBE
002429    ** code for a CREATE TABLE (not when parsing one as part of reading
002430    ** a database schema).  */
002431    if( v && pPk->tnum>0 ){
002432      assert( db->init.busy==0 );
002433      sqlite3VdbeChangeOpcode(v, (int)pPk->tnum, OP_Goto);
002434    }
002435  
002436    /* The root page of the PRIMARY KEY is the table root page */
002437    pPk->tnum = pTab->tnum;
002438  
002439    /* Update the in-memory representation of all UNIQUE indices by converting
002440    ** the final rowid column into one or more columns of the PRIMARY KEY.
002441    */
002442    for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
002443      int n;
002444      if( IsPrimaryKeyIndex(pIdx) ) continue;
002445      for(i=n=0; i<nPk; i++){
002446        if( !isDupColumn(pIdx, pIdx->nKeyCol, pPk, i) ){
002447          testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) );
002448          n++;
002449        }
002450      }
002451      if( n==0 ){
002452        /* This index is a superset of the primary key */
002453        pIdx->nColumn = pIdx->nKeyCol;
002454        continue;
002455      }
002456      if( resizeIndexObject(db, pIdx, pIdx->nKeyCol+n) ) return;
002457      for(i=0, j=pIdx->nKeyCol; i<nPk; i++){
002458        if( !isDupColumn(pIdx, pIdx->nKeyCol, pPk, i) ){
002459          testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) );
002460          pIdx->aiColumn[j] = pPk->aiColumn[i];
002461          pIdx->azColl[j] = pPk->azColl[i];
002462          if( pPk->aSortOrder[i] ){
002463            /* See ticket https://www.sqlite.org/src/info/bba7b69f9849b5bf */
002464            pIdx->bAscKeyBug = 1;
002465          }
002466          j++;
002467        }
002468      }
002469      assert( pIdx->nColumn>=pIdx->nKeyCol+n );
002470      assert( pIdx->nColumn>=j );
002471    }
002472  
002473    /* Add all table columns to the PRIMARY KEY index
002474    */
002475    nExtra = 0;
002476    for(i=0; i<pTab->nCol; i++){
002477      if( !hasColumn(pPk->aiColumn, nPk, i)
002478       && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ) nExtra++;
002479    }
002480    if( resizeIndexObject(db, pPk, nPk+nExtra) ) return;
002481    for(i=0, j=nPk; i<pTab->nCol; i++){
002482      if( !hasColumn(pPk->aiColumn, j, i)
002483       && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0
002484      ){
002485        assert( j<pPk->nColumn );
002486        pPk->aiColumn[j] = i;
002487        pPk->azColl[j] = sqlite3StrBINARY;
002488        j++;
002489      }
002490    }
002491    assert( pPk->nColumn==j );
002492    assert( pTab->nNVCol<=j );
002493    recomputeColumnsNotIndexed(pPk);
002494  }
002495  
002496  
002497  #ifndef SQLITE_OMIT_VIRTUALTABLE
002498  /*
002499  ** Return true if pTab is a virtual table and zName is a shadow table name
002500  ** for that virtual table.
002501  */
002502  int sqlite3IsShadowTableOf(sqlite3 *db, Table *pTab, const char *zName){
002503    int nName;                    /* Length of zName */
002504    Module *pMod;                 /* Module for the virtual table */
002505  
002506    if( !IsVirtual(pTab) ) return 0;
002507    nName = sqlite3Strlen30(pTab->zName);
002508    if( sqlite3_strnicmp(zName, pTab->zName, nName)!=0 ) return 0;
002509    if( zName[nName]!='_' ) return 0;
002510    pMod = (Module*)sqlite3HashFind(&db->aModule, pTab->u.vtab.azArg[0]);
002511    if( pMod==0 ) return 0;
002512    if( pMod->pModule->iVersion<3 ) return 0;
002513    if( pMod->pModule->xShadowName==0 ) return 0;
002514    return pMod->pModule->xShadowName(zName+nName+1);
002515  }
002516  #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
002517  
002518  #ifndef SQLITE_OMIT_VIRTUALTABLE
002519  /*
002520  ** Table pTab is a virtual table.  If it the virtual table implementation
002521  ** exists and has an xShadowName method, then loop over all other ordinary
002522  ** tables within the same schema looking for shadow tables of pTab, and mark
002523  ** any shadow tables seen using the TF_Shadow flag.
002524  */
002525  void sqlite3MarkAllShadowTablesOf(sqlite3 *db, Table *pTab){
002526    int nName;                    /* Length of pTab->zName */
002527    Module *pMod;                 /* Module for the virtual table */
002528    HashElem *k;                  /* For looping through the symbol table */
002529  
002530    assert( IsVirtual(pTab) );
002531    pMod = (Module*)sqlite3HashFind(&db->aModule, pTab->u.vtab.azArg[0]);
002532    if( pMod==0 ) return;
002533    if( NEVER(pMod->pModule==0) ) return;
002534    if( pMod->pModule->iVersion<3 ) return;
002535    if( pMod->pModule->xShadowName==0 ) return;
002536    assert( pTab->zName!=0 );
002537    nName = sqlite3Strlen30(pTab->zName);
002538    for(k=sqliteHashFirst(&pTab->pSchema->tblHash); k; k=sqliteHashNext(k)){
002539      Table *pOther = sqliteHashData(k);
002540      assert( pOther->zName!=0 );
002541      if( !IsOrdinaryTable(pOther) ) continue;
002542      if( pOther->tabFlags & TF_Shadow ) continue;
002543      if( sqlite3StrNICmp(pOther->zName, pTab->zName, nName)==0
002544       && pOther->zName[nName]=='_'
002545       && pMod->pModule->xShadowName(pOther->zName+nName+1)
002546      ){
002547        pOther->tabFlags |= TF_Shadow;
002548      }
002549    }
002550  }
002551  #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
002552  
002553  #ifndef SQLITE_OMIT_VIRTUALTABLE
002554  /*
002555  ** Return true if zName is a shadow table name in the current database
002556  ** connection.
002557  **
002558  ** zName is temporarily modified while this routine is running, but is
002559  ** restored to its original value prior to this routine returning.
002560  */
002561  int sqlite3ShadowTableName(sqlite3 *db, const char *zName){
002562    char *zTail;                  /* Pointer to the last "_" in zName */
002563    Table *pTab;                  /* Table that zName is a shadow of */
002564    zTail = strrchr(zName, '_');
002565    if( zTail==0 ) return 0;
002566    *zTail = 0;
002567    pTab = sqlite3FindTable(db, zName, 0);
002568    *zTail = '_';
002569    if( pTab==0 ) return 0;
002570    if( !IsVirtual(pTab) ) return 0;
002571    return sqlite3IsShadowTableOf(db, pTab, zName);
002572  }
002573  #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
002574  
002575  
002576  #ifdef SQLITE_DEBUG
002577  /*
002578  ** Mark all nodes of an expression as EP_Immutable, indicating that
002579  ** they should not be changed.  Expressions attached to a table or
002580  ** index definition are tagged this way to help ensure that we do
002581  ** not pass them into code generator routines by mistake.
002582  */
002583  static int markImmutableExprStep(Walker *pWalker, Expr *pExpr){
002584    ExprSetVVAProperty(pExpr, EP_Immutable);
002585    return WRC_Continue;
002586  }
002587  static void markExprListImmutable(ExprList *pList){
002588    if( pList ){
002589      Walker w;
002590      memset(&w, 0, sizeof(w));
002591      w.xExprCallback = markImmutableExprStep;
002592      w.xSelectCallback = sqlite3SelectWalkNoop;
002593      w.xSelectCallback2 = 0;
002594      sqlite3WalkExprList(&w, pList);
002595    }
002596  }
002597  #else
002598  #define markExprListImmutable(X)  /* no-op */
002599  #endif /* SQLITE_DEBUG */
002600  
002601  
002602  /*
002603  ** This routine is called to report the final ")" that terminates
002604  ** a CREATE TABLE statement.
002605  **
002606  ** The table structure that other action routines have been building
002607  ** is added to the internal hash tables, assuming no errors have
002608  ** occurred.
002609  **
002610  ** An entry for the table is made in the schema table on disk, unless
002611  ** this is a temporary table or db->init.busy==1.  When db->init.busy==1
002612  ** it means we are reading the sqlite_schema table because we just
002613  ** connected to the database or because the sqlite_schema table has
002614  ** recently changed, so the entry for this table already exists in
002615  ** the sqlite_schema table.  We do not want to create it again.
002616  **
002617  ** If the pSelect argument is not NULL, it means that this routine
002618  ** was called to create a table generated from a 
002619  ** "CREATE TABLE ... AS SELECT ..." statement.  The column names of
002620  ** the new table will match the result set of the SELECT.
002621  */
002622  void sqlite3EndTable(
002623    Parse *pParse,          /* Parse context */
002624    Token *pCons,           /* The ',' token after the last column defn. */
002625    Token *pEnd,            /* The ')' before options in the CREATE TABLE */
002626    u32 tabOpts,            /* Extra table options. Usually 0. */
002627    Select *pSelect         /* Select from a "CREATE ... AS SELECT" */
002628  ){
002629    Table *p;                 /* The new table */
002630    sqlite3 *db = pParse->db; /* The database connection */
002631    int iDb;                  /* Database in which the table lives */
002632    Index *pIdx;              /* An implied index of the table */
002633  
002634    if( pEnd==0 && pSelect==0 ){
002635      return;
002636    }
002637    p = pParse->pNewTable;
002638    if( p==0 ) return;
002639  
002640    if( pSelect==0 && sqlite3ShadowTableName(db, p->zName) ){
002641      p->tabFlags |= TF_Shadow;
002642    }
002643  
002644    /* If the db->init.busy is 1 it means we are reading the SQL off the
002645    ** "sqlite_schema" or "sqlite_temp_schema" table on the disk.
002646    ** So do not write to the disk again.  Extract the root page number
002647    ** for the table from the db->init.newTnum field.  (The page number
002648    ** should have been put there by the sqliteOpenCb routine.)
002649    **
002650    ** If the root page number is 1, that means this is the sqlite_schema
002651    ** table itself.  So mark it read-only.
002652    */
002653    if( db->init.busy ){
002654      if( pSelect || (!IsOrdinaryTable(p) && db->init.newTnum) ){
002655        sqlite3ErrorMsg(pParse, "");
002656        return;
002657      }
002658      p->tnum = db->init.newTnum;
002659      if( p->tnum==1 ) p->tabFlags |= TF_Readonly;
002660    }
002661  
002662    /* Special processing for tables that include the STRICT keyword:
002663    **
002664    **   *  Do not allow custom column datatypes.  Every column must have
002665    **      a datatype that is one of INT, INTEGER, REAL, TEXT, or BLOB.
002666    **
002667    **   *  If a PRIMARY KEY is defined, other than the INTEGER PRIMARY KEY,
002668    **      then all columns of the PRIMARY KEY must have a NOT NULL
002669    **      constraint.
002670    */
002671    if( tabOpts & TF_Strict ){
002672      int ii;
002673      p->tabFlags |= TF_Strict;
002674      for(ii=0; ii<p->nCol; ii++){
002675        Column *pCol = &p->aCol[ii];
002676        if( pCol->eCType==COLTYPE_CUSTOM ){
002677          if( pCol->colFlags & COLFLAG_HASTYPE ){
002678            sqlite3ErrorMsg(pParse,
002679              "unknown datatype for %s.%s: \"%s\"",
002680              p->zName, pCol->zCnName, sqlite3ColumnType(pCol, "")
002681            );
002682          }else{
002683            sqlite3ErrorMsg(pParse, "missing datatype for %s.%s",
002684                            p->zName, pCol->zCnName);
002685          }
002686          return;
002687        }else if( pCol->eCType==COLTYPE_ANY ){
002688          pCol->affinity = SQLITE_AFF_BLOB;
002689        }
002690        if( (pCol->colFlags & COLFLAG_PRIMKEY)!=0
002691         && p->iPKey!=ii
002692         && pCol->notNull == OE_None
002693        ){
002694          pCol->notNull = OE_Abort;
002695          p->tabFlags |= TF_HasNotNull;
002696        }
002697      }    
002698    }
002699  
002700    assert( (p->tabFlags & TF_HasPrimaryKey)==0
002701         || p->iPKey>=0 || sqlite3PrimaryKeyIndex(p)!=0 );
002702    assert( (p->tabFlags & TF_HasPrimaryKey)!=0
002703         || (p->iPKey<0 && sqlite3PrimaryKeyIndex(p)==0) );
002704  
002705    /* Special processing for WITHOUT ROWID Tables */
002706    if( tabOpts & TF_WithoutRowid ){
002707      if( (p->tabFlags & TF_Autoincrement) ){
002708        sqlite3ErrorMsg(pParse,
002709            "AUTOINCREMENT not allowed on WITHOUT ROWID tables");
002710        return;
002711      }
002712      if( (p->tabFlags & TF_HasPrimaryKey)==0 ){
002713        sqlite3ErrorMsg(pParse, "PRIMARY KEY missing on table %s", p->zName);
002714        return;
002715      }
002716      p->tabFlags |= TF_WithoutRowid | TF_NoVisibleRowid;
002717      convertToWithoutRowidTable(pParse, p);
002718    }
002719    iDb = sqlite3SchemaToIndex(db, p->pSchema);
002720  
002721  #ifndef SQLITE_OMIT_CHECK
002722    /* Resolve names in all CHECK constraint expressions.
002723    */
002724    if( p->pCheck ){
002725      sqlite3ResolveSelfReference(pParse, p, NC_IsCheck, 0, p->pCheck);
002726      if( pParse->nErr ){
002727        /* If errors are seen, delete the CHECK constraints now, else they might
002728        ** actually be used if PRAGMA writable_schema=ON is set. */
002729        sqlite3ExprListDelete(db, p->pCheck);
002730        p->pCheck = 0;
002731      }else{
002732        markExprListImmutable(p->pCheck);
002733      }
002734    }
002735  #endif /* !defined(SQLITE_OMIT_CHECK) */
002736  #ifndef SQLITE_OMIT_GENERATED_COLUMNS
002737    if( p->tabFlags & TF_HasGenerated ){
002738      int ii, nNG = 0;
002739      testcase( p->tabFlags & TF_HasVirtual );
002740      testcase( p->tabFlags & TF_HasStored );
002741      for(ii=0; ii<p->nCol; ii++){
002742        u32 colFlags = p->aCol[ii].colFlags;
002743        if( (colFlags & COLFLAG_GENERATED)!=0 ){
002744          Expr *pX = sqlite3ColumnExpr(p, &p->aCol[ii]);
002745          testcase( colFlags & COLFLAG_VIRTUAL );
002746          testcase( colFlags & COLFLAG_STORED );
002747          if( sqlite3ResolveSelfReference(pParse, p, NC_GenCol, pX, 0) ){
002748            /* If there are errors in resolving the expression, change the
002749            ** expression to a NULL.  This prevents code generators that operate
002750            ** on the expression from inserting extra parts into the expression
002751            ** tree that have been allocated from lookaside memory, which is
002752            ** illegal in a schema and will lead to errors or heap corruption
002753            ** when the database connection closes. */
002754            sqlite3ColumnSetExpr(pParse, p, &p->aCol[ii], 
002755                 sqlite3ExprAlloc(db, TK_NULL, 0, 0));
002756          }
002757        }else{
002758          nNG++;
002759        }
002760      }
002761      if( nNG==0 ){
002762        sqlite3ErrorMsg(pParse, "must have at least one non-generated column");
002763        return;
002764      }
002765    }
002766  #endif
002767  
002768    /* Estimate the average row size for the table and for all implied indices */
002769    estimateTableWidth(p);
002770    for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
002771      estimateIndexWidth(pIdx);
002772    }
002773  
002774    /* If not initializing, then create a record for the new table
002775    ** in the schema table of the database.
002776    **
002777    ** If this is a TEMPORARY table, write the entry into the auxiliary
002778    ** file instead of into the main database file.
002779    */
002780    if( !db->init.busy ){
002781      int n;
002782      Vdbe *v;
002783      char *zType;    /* "view" or "table" */
002784      char *zType2;   /* "VIEW" or "TABLE" */
002785      char *zStmt;    /* Text of the CREATE TABLE or CREATE VIEW statement */
002786  
002787      v = sqlite3GetVdbe(pParse);
002788      if( NEVER(v==0) ) return;
002789  
002790      sqlite3VdbeAddOp1(v, OP_Close, 0);
002791  
002792      /* 
002793      ** Initialize zType for the new view or table.
002794      */
002795      if( IsOrdinaryTable(p) ){
002796        /* A regular table */
002797        zType = "table";
002798        zType2 = "TABLE";
002799  #ifndef SQLITE_OMIT_VIEW
002800      }else{
002801        /* A view */
002802        zType = "view";
002803        zType2 = "VIEW";
002804  #endif
002805      }
002806  
002807      /* If this is a CREATE TABLE xx AS SELECT ..., execute the SELECT
002808      ** statement to populate the new table. The root-page number for the
002809      ** new table is in register pParse->regRoot.
002810      **
002811      ** Once the SELECT has been coded by sqlite3Select(), it is in a
002812      ** suitable state to query for the column names and types to be used
002813      ** by the new table.
002814      **
002815      ** A shared-cache write-lock is not required to write to the new table,
002816      ** as a schema-lock must have already been obtained to create it. Since
002817      ** a schema-lock excludes all other database users, the write-lock would
002818      ** be redundant.
002819      */
002820      if( pSelect ){
002821        SelectDest dest;    /* Where the SELECT should store results */
002822        int regYield;       /* Register holding co-routine entry-point */
002823        int addrTop;        /* Top of the co-routine */
002824        int regRec;         /* A record to be insert into the new table */
002825        int regRowid;       /* Rowid of the next row to insert */
002826        int addrInsLoop;    /* Top of the loop for inserting rows */
002827        Table *pSelTab;     /* A table that describes the SELECT results */
002828  
002829        if( IN_SPECIAL_PARSE ){
002830          pParse->rc = SQLITE_ERROR;
002831          pParse->nErr++;
002832          return;
002833        }
002834        regYield = ++pParse->nMem;
002835        regRec = ++pParse->nMem;
002836        regRowid = ++pParse->nMem;
002837        assert(pParse->nTab==1);
002838        sqlite3MayAbort(pParse);
002839        sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb);
002840        sqlite3VdbeChangeP5(v, OPFLAG_P2ISREG);
002841        pParse->nTab = 2;
002842        addrTop = sqlite3VdbeCurrentAddr(v) + 1;
002843        sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop);
002844        if( pParse->nErr ) return;
002845        pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect, SQLITE_AFF_BLOB);
002846        if( pSelTab==0 ) return;
002847        assert( p->aCol==0 );
002848        p->nCol = p->nNVCol = pSelTab->nCol;
002849        p->aCol = pSelTab->aCol;
002850        pSelTab->nCol = 0;
002851        pSelTab->aCol = 0;
002852        sqlite3DeleteTable(db, pSelTab);
002853        sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield);
002854        sqlite3Select(pParse, pSelect, &dest);
002855        if( pParse->nErr ) return;
002856        sqlite3VdbeEndCoroutine(v, regYield);
002857        sqlite3VdbeJumpHere(v, addrTop - 1);
002858        addrInsLoop = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm);
002859        VdbeCoverage(v);
002860        sqlite3VdbeAddOp3(v, OP_MakeRecord, dest.iSdst, dest.nSdst, regRec);
002861        sqlite3TableAffinity(v, p, 0);
002862        sqlite3VdbeAddOp2(v, OP_NewRowid, 1, regRowid);
002863        sqlite3VdbeAddOp3(v, OP_Insert, 1, regRec, regRowid);
002864        sqlite3VdbeGoto(v, addrInsLoop);
002865        sqlite3VdbeJumpHere(v, addrInsLoop);
002866        sqlite3VdbeAddOp1(v, OP_Close, 1);
002867      }
002868  
002869      /* Compute the complete text of the CREATE statement */
002870      if( pSelect ){
002871        zStmt = createTableStmt(db, p);
002872      }else{
002873        Token *pEnd2 = tabOpts ? &pParse->sLastToken : pEnd;
002874        n = (int)(pEnd2->z - pParse->sNameToken.z);
002875        if( pEnd2->z[0]!=';' ) n += pEnd2->n;
002876        zStmt = sqlite3MPrintf(db, 
002877            "CREATE %s %.*s", zType2, n, pParse->sNameToken.z
002878        );
002879      }
002880  
002881      /* A slot for the record has already been allocated in the 
002882      ** schema table.  We just need to update that slot with all
002883      ** the information we've collected.
002884      */
002885      sqlite3NestedParse(pParse,
002886        "UPDATE %Q." LEGACY_SCHEMA_TABLE
002887        " SET type='%s', name=%Q, tbl_name=%Q, rootpage=#%d, sql=%Q"
002888        " WHERE rowid=#%d",
002889        db->aDb[iDb].zDbSName,
002890        zType,
002891        p->zName,
002892        p->zName,
002893        pParse->regRoot,
002894        zStmt,
002895        pParse->regRowid
002896      );
002897      sqlite3DbFree(db, zStmt);
002898      sqlite3ChangeCookie(pParse, iDb);
002899  
002900  #ifndef SQLITE_OMIT_AUTOINCREMENT
002901      /* Check to see if we need to create an sqlite_sequence table for
002902      ** keeping track of autoincrement keys.
002903      */
002904      if( (p->tabFlags & TF_Autoincrement)!=0 && !IN_SPECIAL_PARSE ){
002905        Db *pDb = &db->aDb[iDb];
002906        assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
002907        if( pDb->pSchema->pSeqTab==0 ){
002908          sqlite3NestedParse(pParse,
002909            "CREATE TABLE %Q.sqlite_sequence(name,seq)",
002910            pDb->zDbSName
002911          );
002912        }
002913      }
002914  #endif
002915  
002916      /* Reparse everything to update our internal data structures */
002917      sqlite3VdbeAddParseSchemaOp(v, iDb,
002918             sqlite3MPrintf(db, "tbl_name='%q' AND type!='trigger'", p->zName),0);
002919    }
002920  
002921    /* Add the table to the in-memory representation of the database.
002922    */
002923    if( db->init.busy ){
002924      Table *pOld;
002925      Schema *pSchema = p->pSchema;
002926      assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
002927      assert( HasRowid(p) || p->iPKey<0 );
002928      pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName, p);
002929      if( pOld ){
002930        assert( p==pOld );  /* Malloc must have failed inside HashInsert() */
002931        sqlite3OomFault(db);
002932        return;
002933      }
002934      pParse->pNewTable = 0;
002935      db->mDbFlags |= DBFLAG_SchemaChange;
002936  
002937      /* If this is the magic sqlite_sequence table used by autoincrement,
002938      ** then record a pointer to this table in the main database structure
002939      ** so that INSERT can find the table easily.  */
002940      assert( !pParse->nested );
002941  #ifndef SQLITE_OMIT_AUTOINCREMENT
002942      if( strcmp(p->zName, "sqlite_sequence")==0 ){
002943        assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
002944        p->pSchema->pSeqTab = p;
002945      }
002946  #endif
002947    }
002948  
002949  #ifndef SQLITE_OMIT_ALTERTABLE
002950    if( !pSelect && IsOrdinaryTable(p) ){
002951      assert( pCons && pEnd );
002952      if( pCons->z==0 ){
002953        pCons = pEnd;
002954      }
002955      p->u.tab.addColOffset = 13 + (int)(pCons->z - pParse->sNameToken.z);
002956    }
002957  #endif
002958  }
002959  
002960  #ifndef SQLITE_OMIT_VIEW
002961  /*
002962  ** The parser calls this routine in order to create a new VIEW
002963  */
002964  void sqlite3CreateView(
002965    Parse *pParse,     /* The parsing context */
002966    Token *pBegin,     /* The CREATE token that begins the statement */
002967    Token *pName1,     /* The token that holds the name of the view */
002968    Token *pName2,     /* The token that holds the name of the view */
002969    ExprList *pCNames, /* Optional list of view column names */
002970    Select *pSelect,   /* A SELECT statement that will become the new view */
002971    int isTemp,        /* TRUE for a TEMPORARY view */
002972    int noErr          /* Suppress error messages if VIEW already exists */
002973  ){
002974    Table *p;
002975    int n;
002976    const char *z;
002977    Token sEnd;
002978    DbFixer sFix;
002979    Token *pName = 0;
002980    int iDb;
002981    sqlite3 *db = pParse->db;
002982  
002983    if( pParse->nVar>0 ){
002984      sqlite3ErrorMsg(pParse, "parameters are not allowed in views");
002985      goto create_view_fail;
002986    }
002987    sqlite3StartTable(pParse, pName1, pName2, isTemp, 1, 0, noErr);
002988    p = pParse->pNewTable;
002989    if( p==0 || pParse->nErr ) goto create_view_fail;
002990  
002991    /* Legacy versions of SQLite allowed the use of the magic "rowid" column
002992    ** on a view, even though views do not have rowids.  The following flag
002993    ** setting fixes this problem.  But the fix can be disabled by compiling
002994    ** with -DSQLITE_ALLOW_ROWID_IN_VIEW in case there are legacy apps that
002995    ** depend upon the old buggy behavior. */
002996  #ifndef SQLITE_ALLOW_ROWID_IN_VIEW
002997    p->tabFlags |= TF_NoVisibleRowid;
002998  #endif
002999  
003000    sqlite3TwoPartName(pParse, pName1, pName2, &pName);
003001    iDb = sqlite3SchemaToIndex(db, p->pSchema);
003002    sqlite3FixInit(&sFix, pParse, iDb, "view", pName);
003003    if( sqlite3FixSelect(&sFix, pSelect) ) goto create_view_fail;
003004  
003005    /* Make a copy of the entire SELECT statement that defines the view.
003006    ** This will force all the Expr.token.z values to be dynamically
003007    ** allocated rather than point to the input string - which means that
003008    ** they will persist after the current sqlite3_exec() call returns.
003009    */
003010    pSelect->selFlags |= SF_View;
003011    if( IN_RENAME_OBJECT ){
003012      p->u.view.pSelect = pSelect;
003013      pSelect = 0;
003014    }else{
003015      p->u.view.pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
003016    }
003017    p->pCheck = sqlite3ExprListDup(db, pCNames, EXPRDUP_REDUCE);
003018    p->eTabType = TABTYP_VIEW;
003019    if( db->mallocFailed ) goto create_view_fail;
003020  
003021    /* Locate the end of the CREATE VIEW statement.  Make sEnd point to
003022    ** the end.
003023    */
003024    sEnd = pParse->sLastToken;
003025    assert( sEnd.z[0]!=0 || sEnd.n==0 );
003026    if( sEnd.z[0]!=';' ){
003027      sEnd.z += sEnd.n;
003028    }
003029    sEnd.n = 0;
003030    n = (int)(sEnd.z - pBegin->z);
003031    assert( n>0 );
003032    z = pBegin->z;
003033    while( sqlite3Isspace(z[n-1]) ){ n--; }
003034    sEnd.z = &z[n-1];
003035    sEnd.n = 1;
003036  
003037    /* Use sqlite3EndTable() to add the view to the schema table */
003038    sqlite3EndTable(pParse, 0, &sEnd, 0, 0);
003039  
003040  create_view_fail:
003041    sqlite3SelectDelete(db, pSelect);
003042    if( IN_RENAME_OBJECT ){
003043      sqlite3RenameExprlistUnmap(pParse, pCNames);
003044    }
003045    sqlite3ExprListDelete(db, pCNames);
003046    return;
003047  }
003048  #endif /* SQLITE_OMIT_VIEW */
003049  
003050  #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
003051  /*
003052  ** The Table structure pTable is really a VIEW.  Fill in the names of
003053  ** the columns of the view in the pTable structure.  Return the number
003054  ** of errors.  If an error is seen leave an error message in pParse->zErrMsg.
003055  */
003056  int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
003057    Table *pSelTab;   /* A fake table from which we get the result set */
003058    Select *pSel;     /* Copy of the SELECT that implements the view */
003059    int nErr = 0;     /* Number of errors encountered */
003060    int n;            /* Temporarily holds the number of cursors assigned */
003061    sqlite3 *db = pParse->db;  /* Database connection for malloc errors */
003062  #ifndef SQLITE_OMIT_VIRTUALTABLE
003063    int rc;
003064  #endif
003065  #ifndef SQLITE_OMIT_AUTHORIZATION
003066    sqlite3_xauth xAuth;       /* Saved xAuth pointer */
003067  #endif
003068  
003069    assert( pTable );
003070  
003071  #ifndef SQLITE_OMIT_VIRTUALTABLE
003072    if( IsVirtual(pTable) ){
003073      db->nSchemaLock++;
003074      rc = sqlite3VtabCallConnect(pParse, pTable);
003075      db->nSchemaLock--;
003076      return rc;
003077    }
003078  #endif
003079  
003080  #ifndef SQLITE_OMIT_VIEW
003081    /* A positive nCol means the columns names for this view are
003082    ** already known.
003083    */
003084    if( pTable->nCol>0 ) return 0;
003085  
003086    /* A negative nCol is a special marker meaning that we are currently
003087    ** trying to compute the column names.  If we enter this routine with
003088    ** a negative nCol, it means two or more views form a loop, like this:
003089    **
003090    **     CREATE VIEW one AS SELECT * FROM two;
003091    **     CREATE VIEW two AS SELECT * FROM one;
003092    **
003093    ** Actually, the error above is now caught prior to reaching this point.
003094    ** But the following test is still important as it does come up
003095    ** in the following:
003096    ** 
003097    **     CREATE TABLE main.ex1(a);
003098    **     CREATE TEMP VIEW ex1 AS SELECT a FROM ex1;
003099    **     SELECT * FROM temp.ex1;
003100    */
003101    if( pTable->nCol<0 ){
003102      sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName);
003103      return 1;
003104    }
003105    assert( pTable->nCol>=0 );
003106  
003107    /* If we get this far, it means we need to compute the table names.
003108    ** Note that the call to sqlite3ResultSetOfSelect() will expand any
003109    ** "*" elements in the results set of the view and will assign cursors
003110    ** to the elements of the FROM clause.  But we do not want these changes
003111    ** to be permanent.  So the computation is done on a copy of the SELECT
003112    ** statement that defines the view.
003113    */
003114    assert( IsView(pTable) );
003115    pSel = sqlite3SelectDup(db, pTable->u.view.pSelect, 0);
003116    if( pSel ){
003117      u8 eParseMode = pParse->eParseMode;
003118      pParse->eParseMode = PARSE_MODE_NORMAL;
003119      n = pParse->nTab;
003120      sqlite3SrcListAssignCursors(pParse, pSel->pSrc);
003121      pTable->nCol = -1;
003122      DisableLookaside;
003123  #ifndef SQLITE_OMIT_AUTHORIZATION
003124      xAuth = db->xAuth;
003125      db->xAuth = 0;
003126      pSelTab = sqlite3ResultSetOfSelect(pParse, pSel, SQLITE_AFF_NONE);
003127      db->xAuth = xAuth;
003128  #else
003129      pSelTab = sqlite3ResultSetOfSelect(pParse, pSel, SQLITE_AFF_NONE);
003130  #endif
003131      pParse->nTab = n;
003132      if( pSelTab==0 ){
003133        pTable->nCol = 0;
003134        nErr++;
003135      }else if( pTable->pCheck ){
003136        /* CREATE VIEW name(arglist) AS ...
003137        ** The names of the columns in the table are taken from
003138        ** arglist which is stored in pTable->pCheck.  The pCheck field
003139        ** normally holds CHECK constraints on an ordinary table, but for
003140        ** a VIEW it holds the list of column names.
003141        */
003142        sqlite3ColumnsFromExprList(pParse, pTable->pCheck, 
003143                                   &pTable->nCol, &pTable->aCol);
003144        if( pParse->nErr==0
003145         && pTable->nCol==pSel->pEList->nExpr
003146        ){
003147          assert( db->mallocFailed==0 );
003148          sqlite3SelectAddColumnTypeAndCollation(pParse, pTable, pSel,
003149                                                 SQLITE_AFF_NONE);
003150        }
003151      }else{
003152        /* CREATE VIEW name AS...  without an argument list.  Construct
003153        ** the column names from the SELECT statement that defines the view.
003154        */
003155        assert( pTable->aCol==0 );
003156        pTable->nCol = pSelTab->nCol;
003157        pTable->aCol = pSelTab->aCol;
003158        pTable->tabFlags |= (pSelTab->tabFlags & COLFLAG_NOINSERT);
003159        pSelTab->nCol = 0;
003160        pSelTab->aCol = 0;
003161        assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) );
003162      }
003163      pTable->nNVCol = pTable->nCol;
003164      sqlite3DeleteTable(db, pSelTab);
003165      sqlite3SelectDelete(db, pSel);
003166      EnableLookaside;
003167      pParse->eParseMode = eParseMode;
003168    } else {
003169      nErr++;
003170    }
003171    pTable->pSchema->schemaFlags |= DB_UnresetViews;
003172    if( db->mallocFailed ){
003173      sqlite3DeleteColumnNames(db, pTable);
003174    }
003175  #endif /* SQLITE_OMIT_VIEW */
003176    return nErr;  
003177  }
003178  #endif /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */
003179  
003180  #ifndef SQLITE_OMIT_VIEW
003181  /*
003182  ** Clear the column names from every VIEW in database idx.
003183  */
003184  static void sqliteViewResetAll(sqlite3 *db, int idx){
003185    HashElem *i;
003186    assert( sqlite3SchemaMutexHeld(db, idx, 0) );
003187    if( !DbHasProperty(db, idx, DB_UnresetViews) ) return;
003188    for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){
003189      Table *pTab = sqliteHashData(i);
003190      if( IsView(pTab) ){
003191        sqlite3DeleteColumnNames(db, pTab);
003192      }
003193    }
003194    DbClearProperty(db, idx, DB_UnresetViews);
003195  }
003196  #else
003197  # define sqliteViewResetAll(A,B)
003198  #endif /* SQLITE_OMIT_VIEW */
003199  
003200  /*
003201  ** This function is called by the VDBE to adjust the internal schema
003202  ** used by SQLite when the btree layer moves a table root page. The
003203  ** root-page of a table or index in database iDb has changed from iFrom
003204  ** to iTo.
003205  **
003206  ** Ticket #1728:  The symbol table might still contain information
003207  ** on tables and/or indices that are the process of being deleted.
003208  ** If you are unlucky, one of those deleted indices or tables might
003209  ** have the same rootpage number as the real table or index that is
003210  ** being moved.  So we cannot stop searching after the first match 
003211  ** because the first match might be for one of the deleted indices
003212  ** or tables and not the table/index that is actually being moved.
003213  ** We must continue looping until all tables and indices with
003214  ** rootpage==iFrom have been converted to have a rootpage of iTo
003215  ** in order to be certain that we got the right one.
003216  */
003217  #ifndef SQLITE_OMIT_AUTOVACUUM
003218  void sqlite3RootPageMoved(sqlite3 *db, int iDb, Pgno iFrom, Pgno iTo){
003219    HashElem *pElem;
003220    Hash *pHash;
003221    Db *pDb;
003222  
003223    assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
003224    pDb = &db->aDb[iDb];
003225    pHash = &pDb->pSchema->tblHash;
003226    for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){
003227      Table *pTab = sqliteHashData(pElem);
003228      if( pTab->tnum==iFrom ){
003229        pTab->tnum = iTo;
003230      }
003231    }
003232    pHash = &pDb->pSchema->idxHash;
003233    for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){
003234      Index *pIdx = sqliteHashData(pElem);
003235      if( pIdx->tnum==iFrom ){
003236        pIdx->tnum = iTo;
003237      }
003238    }
003239  }
003240  #endif
003241  
003242  /*
003243  ** Write code to erase the table with root-page iTable from database iDb.
003244  ** Also write code to modify the sqlite_schema table and internal schema
003245  ** if a root-page of another table is moved by the btree-layer whilst
003246  ** erasing iTable (this can happen with an auto-vacuum database).
003247  */ 
003248  static void destroyRootPage(Parse *pParse, int iTable, int iDb){
003249    Vdbe *v = sqlite3GetVdbe(pParse);
003250    int r1 = sqlite3GetTempReg(pParse);
003251    if( iTable<2 ) sqlite3ErrorMsg(pParse, "corrupt schema");
003252    sqlite3VdbeAddOp3(v, OP_Destroy, iTable, r1, iDb);
003253    sqlite3MayAbort(pParse);
003254  #ifndef SQLITE_OMIT_AUTOVACUUM
003255    /* OP_Destroy stores an in integer r1. If this integer
003256    ** is non-zero, then it is the root page number of a table moved to
003257    ** location iTable. The following code modifies the sqlite_schema table to
003258    ** reflect this.
003259    **
003260    ** The "#NNN" in the SQL is a special constant that means whatever value
003261    ** is in register NNN.  See grammar rules associated with the TK_REGISTER
003262    ** token for additional information.
003263    */
003264    sqlite3NestedParse(pParse, 
003265       "UPDATE %Q." LEGACY_SCHEMA_TABLE
003266       " SET rootpage=%d WHERE #%d AND rootpage=#%d",
003267       pParse->db->aDb[iDb].zDbSName, iTable, r1, r1);
003268  #endif
003269    sqlite3ReleaseTempReg(pParse, r1);
003270  }
003271  
003272  /*
003273  ** Write VDBE code to erase table pTab and all associated indices on disk.
003274  ** Code to update the sqlite_schema tables and internal schema definitions
003275  ** in case a root-page belonging to another table is moved by the btree layer
003276  ** is also added (this can happen with an auto-vacuum database).
003277  */
003278  static void destroyTable(Parse *pParse, Table *pTab){
003279    /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM
003280    ** is not defined), then it is important to call OP_Destroy on the
003281    ** table and index root-pages in order, starting with the numerically 
003282    ** largest root-page number. This guarantees that none of the root-pages
003283    ** to be destroyed is relocated by an earlier OP_Destroy. i.e. if the
003284    ** following were coded:
003285    **
003286    ** OP_Destroy 4 0
003287    ** ...
003288    ** OP_Destroy 5 0
003289    **
003290    ** and root page 5 happened to be the largest root-page number in the
003291    ** database, then root page 5 would be moved to page 4 by the 
003292    ** "OP_Destroy 4 0" opcode. The subsequent "OP_Destroy 5 0" would hit
003293    ** a free-list page.
003294    */
003295    Pgno iTab = pTab->tnum;
003296    Pgno iDestroyed = 0;
003297  
003298    while( 1 ){
003299      Index *pIdx;
003300      Pgno iLargest = 0;
003301  
003302      if( iDestroyed==0 || iTab<iDestroyed ){
003303        iLargest = iTab;
003304      }
003305      for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
003306        Pgno iIdx = pIdx->tnum;
003307        assert( pIdx->pSchema==pTab->pSchema );
003308        if( (iDestroyed==0 || (iIdx<iDestroyed)) && iIdx>iLargest ){
003309          iLargest = iIdx;
003310        }
003311      }
003312      if( iLargest==0 ){
003313        return;
003314      }else{
003315        int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
003316        assert( iDb>=0 && iDb<pParse->db->nDb );
003317        destroyRootPage(pParse, iLargest, iDb);
003318        iDestroyed = iLargest;
003319      }
003320    }
003321  }
003322  
003323  /*
003324  ** Remove entries from the sqlite_statN tables (for N in (1,2,3))
003325  ** after a DROP INDEX or DROP TABLE command.
003326  */
003327  static void sqlite3ClearStatTables(
003328    Parse *pParse,         /* The parsing context */
003329    int iDb,               /* The database number */
003330    const char *zType,     /* "idx" or "tbl" */
003331    const char *zName      /* Name of index or table */
003332  ){
003333    int i;
003334    const char *zDbName = pParse->db->aDb[iDb].zDbSName;
003335    for(i=1; i<=4; i++){
003336      char zTab[24];
003337      sqlite3_snprintf(sizeof(zTab),zTab,"sqlite_stat%d",i);
003338      if( sqlite3FindTable(pParse->db, zTab, zDbName) ){
003339        sqlite3NestedParse(pParse,
003340          "DELETE FROM %Q.%s WHERE %s=%Q",
003341          zDbName, zTab, zType, zName
003342        );
003343      }
003344    }
003345  }
003346  
003347  /*
003348  ** Generate code to drop a table.
003349  */
003350  void sqlite3CodeDropTable(Parse *pParse, Table *pTab, int iDb, int isView){
003351    Vdbe *v;
003352    sqlite3 *db = pParse->db;
003353    Trigger *pTrigger;
003354    Db *pDb = &db->aDb[iDb];
003355  
003356    v = sqlite3GetVdbe(pParse);
003357    assert( v!=0 );
003358    sqlite3BeginWriteOperation(pParse, 1, iDb);
003359  
003360  #ifndef SQLITE_OMIT_VIRTUALTABLE
003361    if( IsVirtual(pTab) ){
003362      sqlite3VdbeAddOp0(v, OP_VBegin);
003363    }
003364  #endif
003365  
003366    /* Drop all triggers associated with the table being dropped. Code
003367    ** is generated to remove entries from sqlite_schema and/or
003368    ** sqlite_temp_schema if required.
003369    */
003370    pTrigger = sqlite3TriggerList(pParse, pTab);
003371    while( pTrigger ){
003372      assert( pTrigger->pSchema==pTab->pSchema || 
003373          pTrigger->pSchema==db->aDb[1].pSchema );
003374      sqlite3DropTriggerPtr(pParse, pTrigger);
003375      pTrigger = pTrigger->pNext;
003376    }
003377  
003378  #ifndef SQLITE_OMIT_AUTOINCREMENT
003379    /* Remove any entries of the sqlite_sequence table associated with
003380    ** the table being dropped. This is done before the table is dropped
003381    ** at the btree level, in case the sqlite_sequence table needs to
003382    ** move as a result of the drop (can happen in auto-vacuum mode).
003383    */
003384    if( pTab->tabFlags & TF_Autoincrement ){
003385      sqlite3NestedParse(pParse,
003386        "DELETE FROM %Q.sqlite_sequence WHERE name=%Q",
003387        pDb->zDbSName, pTab->zName
003388      );
003389    }
003390  #endif
003391  
003392    /* Drop all entries in the schema table that refer to the
003393    ** table. The program name loops through the schema table and deletes
003394    ** every row that refers to a table of the same name as the one being
003395    ** dropped. Triggers are handled separately because a trigger can be
003396    ** created in the temp database that refers to a table in another
003397    ** database.
003398    */
003399    sqlite3NestedParse(pParse, 
003400        "DELETE FROM %Q." LEGACY_SCHEMA_TABLE
003401        " WHERE tbl_name=%Q and type!='trigger'",
003402        pDb->zDbSName, pTab->zName);
003403    if( !isView && !IsVirtual(pTab) ){
003404      destroyTable(pParse, pTab);
003405    }
003406  
003407    /* Remove the table entry from SQLite's internal schema and modify
003408    ** the schema cookie.
003409    */
003410    if( IsVirtual(pTab) ){
003411      sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0);
003412      sqlite3MayAbort(pParse);
003413    }
003414    sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
003415    sqlite3ChangeCookie(pParse, iDb);
003416    sqliteViewResetAll(db, iDb);
003417  }
003418  
003419  /*
003420  ** Return TRUE if shadow tables should be read-only in the current
003421  ** context.
003422  */
003423  int sqlite3ReadOnlyShadowTables(sqlite3 *db){
003424  #ifndef SQLITE_OMIT_VIRTUALTABLE
003425    if( (db->flags & SQLITE_Defensive)!=0
003426     && db->pVtabCtx==0
003427     && db->nVdbeExec==0
003428     && !sqlite3VtabInSync(db)
003429    ){
003430      return 1;
003431    }
003432  #endif
003433    return 0;
003434  }
003435  
003436  /*
003437  ** Return true if it is not allowed to drop the given table
003438  */
003439  static int tableMayNotBeDropped(sqlite3 *db, Table *pTab){
003440    if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
003441      if( sqlite3StrNICmp(pTab->zName+7, "stat", 4)==0 ) return 0;
003442      if( sqlite3StrNICmp(pTab->zName+7, "parameters", 10)==0 ) return 0;
003443      return 1;
003444    }
003445    if( (pTab->tabFlags & TF_Shadow)!=0 && sqlite3ReadOnlyShadowTables(db) ){
003446      return 1;
003447    }
003448    if( pTab->tabFlags & TF_Eponymous ){
003449      return 1;
003450    }
003451    return 0;
003452  }
003453  
003454  /*
003455  ** This routine is called to do the work of a DROP TABLE statement.
003456  ** pName is the name of the table to be dropped.
003457  */
003458  void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView, int noErr){
003459    Table *pTab;
003460    Vdbe *v;
003461    sqlite3 *db = pParse->db;
003462    int iDb;
003463  
003464    if( db->mallocFailed ){
003465      goto exit_drop_table;
003466    }
003467    assert( pParse->nErr==0 );
003468    assert( pName->nSrc==1 );
003469    if( sqlite3ReadSchema(pParse) ) goto exit_drop_table;
003470    if( noErr ) db->suppressErr++;
003471    assert( isView==0 || isView==LOCATE_VIEW );
003472    pTab = sqlite3LocateTableItem(pParse, isView, &pName->a[0]);
003473    if( noErr ) db->suppressErr--;
003474  
003475    if( pTab==0 ){
003476      if( noErr ){
003477        sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
003478        sqlite3ForceNotReadOnly(pParse);
003479      }
003480      goto exit_drop_table;
003481    }
003482    iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
003483    assert( iDb>=0 && iDb<db->nDb );
003484  
003485    /* If pTab is a virtual table, call ViewGetColumnNames() to ensure
003486    ** it is initialized.
003487    */
003488    if( IsVirtual(pTab) && sqlite3ViewGetColumnNames(pParse, pTab) ){
003489      goto exit_drop_table;
003490    }
003491  #ifndef SQLITE_OMIT_AUTHORIZATION
003492    {
003493      int code;
003494      const char *zTab = SCHEMA_TABLE(iDb);
003495      const char *zDb = db->aDb[iDb].zDbSName;
003496      const char *zArg2 = 0;
003497      if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){
003498        goto exit_drop_table;
003499      }
003500      if( isView ){
003501        if( !OMIT_TEMPDB && iDb==1 ){
003502          code = SQLITE_DROP_TEMP_VIEW;
003503        }else{
003504          code = SQLITE_DROP_VIEW;
003505        }
003506  #ifndef SQLITE_OMIT_VIRTUALTABLE
003507      }else if( IsVirtual(pTab) ){
003508        code = SQLITE_DROP_VTABLE;
003509        zArg2 = sqlite3GetVTable(db, pTab)->pMod->zName;
003510  #endif
003511      }else{
003512        if( !OMIT_TEMPDB && iDb==1 ){
003513          code = SQLITE_DROP_TEMP_TABLE;
003514        }else{
003515          code = SQLITE_DROP_TABLE;
003516        }
003517      }
003518      if( sqlite3AuthCheck(pParse, code, pTab->zName, zArg2, zDb) ){
003519        goto exit_drop_table;
003520      }
003521      if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
003522        goto exit_drop_table;
003523      }
003524    }
003525  #endif
003526    if( tableMayNotBeDropped(db, pTab) ){
003527      sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
003528      goto exit_drop_table;
003529    }
003530  
003531  #ifndef SQLITE_OMIT_VIEW
003532    /* Ensure DROP TABLE is not used on a view, and DROP VIEW is not used
003533    ** on a table.
003534    */
003535    if( isView && !IsView(pTab) ){
003536      sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName);
003537      goto exit_drop_table;
003538    }
003539    if( !isView && IsView(pTab) ){
003540      sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName);
003541      goto exit_drop_table;
003542    }
003543  #endif
003544  
003545    /* Generate code to remove the table from the schema table
003546    ** on disk.
003547    */
003548    v = sqlite3GetVdbe(pParse);
003549    if( v ){
003550      sqlite3BeginWriteOperation(pParse, 1, iDb);
003551      if( !isView ){
003552        sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName);
003553        sqlite3FkDropTable(pParse, pName, pTab);
003554      }
003555      sqlite3CodeDropTable(pParse, pTab, iDb, isView);
003556    }
003557  
003558  exit_drop_table:
003559    sqlite3SrcListDelete(db, pName);
003560  }
003561  
003562  /*
003563  ** This routine is called to create a new foreign key on the table
003564  ** currently under construction.  pFromCol determines which columns
003565  ** in the current table point to the foreign key.  If pFromCol==0 then
003566  ** connect the key to the last column inserted.  pTo is the name of
003567  ** the table referred to (a.k.a the "parent" table).  pToCol is a list
003568  ** of tables in the parent pTo table.  flags contains all
003569  ** information about the conflict resolution algorithms specified
003570  ** in the ON DELETE, ON UPDATE and ON INSERT clauses.
003571  **
003572  ** An FKey structure is created and added to the table currently
003573  ** under construction in the pParse->pNewTable field.
003574  **
003575  ** The foreign key is set for IMMEDIATE processing.  A subsequent call
003576  ** to sqlite3DeferForeignKey() might change this to DEFERRED.
003577  */
003578  void sqlite3CreateForeignKey(
003579    Parse *pParse,       /* Parsing context */
003580    ExprList *pFromCol,  /* Columns in this table that point to other table */
003581    Token *pTo,          /* Name of the other table */
003582    ExprList *pToCol,    /* Columns in the other table */
003583    int flags            /* Conflict resolution algorithms. */
003584  ){
003585    sqlite3 *db = pParse->db;
003586  #ifndef SQLITE_OMIT_FOREIGN_KEY
003587    FKey *pFKey = 0;
003588    FKey *pNextTo;
003589    Table *p = pParse->pNewTable;
003590    i64 nByte;
003591    int i;
003592    int nCol;
003593    char *z;
003594  
003595    assert( pTo!=0 );
003596    if( p==0 || IN_DECLARE_VTAB ) goto fk_end;
003597    if( pFromCol==0 ){
003598      int iCol = p->nCol-1;
003599      if( NEVER(iCol<0) ) goto fk_end;
003600      if( pToCol && pToCol->nExpr!=1 ){
003601        sqlite3ErrorMsg(pParse, "foreign key on %s"
003602           " should reference only one column of table %T",
003603           p->aCol[iCol].zCnName, pTo);
003604        goto fk_end;
003605      }
003606      nCol = 1;
003607    }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){
003608      sqlite3ErrorMsg(pParse,
003609          "number of columns in foreign key does not match the number of "
003610          "columns in the referenced table");
003611      goto fk_end;
003612    }else{
003613      nCol = pFromCol->nExpr;
003614    }
003615    nByte = sizeof(*pFKey) + (nCol-1)*sizeof(pFKey->aCol[0]) + pTo->n + 1;
003616    if( pToCol ){
003617      for(i=0; i<pToCol->nExpr; i++){
003618        nByte += sqlite3Strlen30(pToCol->a[i].zEName) + 1;
003619      }
003620    }
003621    pFKey = sqlite3DbMallocZero(db, nByte );
003622    if( pFKey==0 ){
003623      goto fk_end;
003624    }
003625    pFKey->pFrom = p;
003626    assert( IsOrdinaryTable(p) );
003627    pFKey->pNextFrom = p->u.tab.pFKey;
003628    z = (char*)&pFKey->aCol[nCol];
003629    pFKey->zTo = z;
003630    if( IN_RENAME_OBJECT ){
003631      sqlite3RenameTokenMap(pParse, (void*)z, pTo);
003632    }
003633    memcpy(z, pTo->z, pTo->n);
003634    z[pTo->n] = 0;
003635    sqlite3Dequote(z);
003636    z += pTo->n+1;
003637    pFKey->nCol = nCol;
003638    if( pFromCol==0 ){
003639      pFKey->aCol[0].iFrom = p->nCol-1;
003640    }else{
003641      for(i=0; i<nCol; i++){
003642        int j;
003643        for(j=0; j<p->nCol; j++){
003644          if( sqlite3StrICmp(p->aCol[j].zCnName, pFromCol->a[i].zEName)==0 ){
003645            pFKey->aCol[i].iFrom = j;
003646            break;
003647          }
003648        }
003649        if( j>=p->nCol ){
003650          sqlite3ErrorMsg(pParse, 
003651            "unknown column \"%s\" in foreign key definition", 
003652            pFromCol->a[i].zEName);
003653          goto fk_end;
003654        }
003655        if( IN_RENAME_OBJECT ){
003656          sqlite3RenameTokenRemap(pParse, &pFKey->aCol[i], pFromCol->a[i].zEName);
003657        }
003658      }
003659    }
003660    if( pToCol ){
003661      for(i=0; i<nCol; i++){
003662        int n = sqlite3Strlen30(pToCol->a[i].zEName);
003663        pFKey->aCol[i].zCol = z;
003664        if( IN_RENAME_OBJECT ){
003665          sqlite3RenameTokenRemap(pParse, z, pToCol->a[i].zEName);
003666        }
003667        memcpy(z, pToCol->a[i].zEName, n);
003668        z[n] = 0;
003669        z += n+1;
003670      }
003671    }
003672    pFKey->isDeferred = 0;
003673    pFKey->aAction[0] = (u8)(flags & 0xff);            /* ON DELETE action */
003674    pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff);    /* ON UPDATE action */
003675  
003676    assert( sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
003677    pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash, 
003678        pFKey->zTo, (void *)pFKey
003679    );
003680    if( pNextTo==pFKey ){
003681      sqlite3OomFault(db);
003682      goto fk_end;
003683    }
003684    if( pNextTo ){
003685      assert( pNextTo->pPrevTo==0 );
003686      pFKey->pNextTo = pNextTo;
003687      pNextTo->pPrevTo = pFKey;
003688    }
003689  
003690    /* Link the foreign key to the table as the last step.
003691    */
003692    assert( IsOrdinaryTable(p) );
003693    p->u.tab.pFKey = pFKey;
003694    pFKey = 0;
003695  
003696  fk_end:
003697    sqlite3DbFree(db, pFKey);
003698  #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
003699    sqlite3ExprListDelete(db, pFromCol);
003700    sqlite3ExprListDelete(db, pToCol);
003701  }
003702  
003703  /*
003704  ** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED
003705  ** clause is seen as part of a foreign key definition.  The isDeferred
003706  ** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE.
003707  ** The behavior of the most recently created foreign key is adjusted
003708  ** accordingly.
003709  */
003710  void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){
003711  #ifndef SQLITE_OMIT_FOREIGN_KEY
003712    Table *pTab;
003713    FKey *pFKey;
003714    if( (pTab = pParse->pNewTable)==0 ) return;
003715    if( NEVER(!IsOrdinaryTable(pTab)) ) return;
003716    if( (pFKey = pTab->u.tab.pFKey)==0 ) return;
003717    assert( isDeferred==0 || isDeferred==1 ); /* EV: R-30323-21917 */
003718    pFKey->isDeferred = (u8)isDeferred;
003719  #endif
003720  }
003721  
003722  /*
003723  ** Generate code that will erase and refill index *pIdx.  This is
003724  ** used to initialize a newly created index or to recompute the
003725  ** content of an index in response to a REINDEX command.
003726  **
003727  ** if memRootPage is not negative, it means that the index is newly
003728  ** created.  The register specified by memRootPage contains the
003729  ** root page number of the index.  If memRootPage is negative, then
003730  ** the index already exists and must be cleared before being refilled and
003731  ** the root page number of the index is taken from pIndex->tnum.
003732  */
003733  static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
003734    Table *pTab = pIndex->pTable;  /* The table that is indexed */
003735    int iTab = pParse->nTab++;     /* Btree cursor used for pTab */
003736    int iIdx = pParse->nTab++;     /* Btree cursor used for pIndex */
003737    int iSorter;                   /* Cursor opened by OpenSorter (if in use) */
003738    int addr1;                     /* Address of top of loop */
003739    int addr2;                     /* Address to jump to for next iteration */
003740    Pgno tnum;                     /* Root page of index */
003741    int iPartIdxLabel;             /* Jump to this label to skip a row */
003742    Vdbe *v;                       /* Generate code into this virtual machine */
003743    KeyInfo *pKey;                 /* KeyInfo for index */
003744    int regRecord;                 /* Register holding assembled index record */
003745    sqlite3 *db = pParse->db;      /* The database connection */
003746    int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
003747  
003748  #ifndef SQLITE_OMIT_AUTHORIZATION
003749    if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0,
003750        db->aDb[iDb].zDbSName ) ){
003751      return;
003752    }
003753  #endif
003754  
003755    /* Require a write-lock on the table to perform this operation */
003756    sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
003757  
003758    v = sqlite3GetVdbe(pParse);
003759    if( v==0 ) return;
003760    if( memRootPage>=0 ){
003761      tnum = (Pgno)memRootPage;
003762    }else{
003763      tnum = pIndex->tnum;
003764    }
003765    pKey = sqlite3KeyInfoOfIndex(pParse, pIndex);
003766    assert( pKey!=0 || pParse->nErr );
003767  
003768    /* Open the sorter cursor if we are to use one. */
003769    iSorter = pParse->nTab++;
003770    sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, pIndex->nKeyCol, (char*)
003771                      sqlite3KeyInfoRef(pKey), P4_KEYINFO);
003772  
003773    /* Open the table. Loop through all rows of the table, inserting index
003774    ** records into the sorter. */
003775    sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
003776    addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0); VdbeCoverage(v);
003777    regRecord = sqlite3GetTempReg(pParse);
003778    sqlite3MultiWrite(pParse);
003779  
003780    sqlite3GenerateIndexKey(pParse,pIndex,iTab,regRecord,0,&iPartIdxLabel,0,0);
003781    sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord);
003782    sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel);
003783    sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1); VdbeCoverage(v);
003784    sqlite3VdbeJumpHere(v, addr1);
003785    if( memRootPage<0 ) sqlite3VdbeAddOp2(v, OP_Clear, tnum, iDb);
003786    sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, (int)tnum, iDb, 
003787                      (char *)pKey, P4_KEYINFO);
003788    sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR|((memRootPage>=0)?OPFLAG_P2ISREG:0));
003789  
003790    addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0); VdbeCoverage(v);
003791    if( IsUniqueIndex(pIndex) ){
003792      int j2 = sqlite3VdbeGoto(v, 1);
003793      addr2 = sqlite3VdbeCurrentAddr(v);
003794      sqlite3VdbeVerifyAbortable(v, OE_Abort);
003795      sqlite3VdbeAddOp4Int(v, OP_SorterCompare, iSorter, j2, regRecord,
003796                           pIndex->nKeyCol); VdbeCoverage(v);
003797      sqlite3UniqueConstraint(pParse, OE_Abort, pIndex);
003798      sqlite3VdbeJumpHere(v, j2);
003799    }else{
003800      /* Most CREATE INDEX and REINDEX statements that are not UNIQUE can not
003801      ** abort. The exception is if one of the indexed expressions contains a
003802      ** user function that throws an exception when it is evaluated. But the
003803      ** overhead of adding a statement journal to a CREATE INDEX statement is
003804      ** very small (since most of the pages written do not contain content that
003805      ** needs to be restored if the statement aborts), so we call 
003806      ** sqlite3MayAbort() for all CREATE INDEX statements.  */
003807      sqlite3MayAbort(pParse);
003808      addr2 = sqlite3VdbeCurrentAddr(v);
003809    }
003810    sqlite3VdbeAddOp3(v, OP_SorterData, iSorter, regRecord, iIdx);
003811    if( !pIndex->bAscKeyBug ){
003812      /* This OP_SeekEnd opcode makes index insert for a REINDEX go much
003813      ** faster by avoiding unnecessary seeks.  But the optimization does
003814      ** not work for UNIQUE constraint indexes on WITHOUT ROWID tables
003815      ** with DESC primary keys, since those indexes have there keys in
003816      ** a different order from the main table.
003817      ** See ticket: https://www.sqlite.org/src/info/bba7b69f9849b5bf
003818      */
003819      sqlite3VdbeAddOp1(v, OP_SeekEnd, iIdx);
003820    }
003821    sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdx, regRecord);
003822    sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
003823    sqlite3ReleaseTempReg(pParse, regRecord);
003824    sqlite3VdbeAddOp2(v, OP_SorterNext, iSorter, addr2); VdbeCoverage(v);
003825    sqlite3VdbeJumpHere(v, addr1);
003826  
003827    sqlite3VdbeAddOp1(v, OP_Close, iTab);
003828    sqlite3VdbeAddOp1(v, OP_Close, iIdx);
003829    sqlite3VdbeAddOp1(v, OP_Close, iSorter);
003830  }
003831  
003832  /*
003833  ** Allocate heap space to hold an Index object with nCol columns.
003834  **
003835  ** Increase the allocation size to provide an extra nExtra bytes
003836  ** of 8-byte aligned space after the Index object and return a
003837  ** pointer to this extra space in *ppExtra.
003838  */
003839  Index *sqlite3AllocateIndexObject(
003840    sqlite3 *db,         /* Database connection */
003841    i16 nCol,            /* Total number of columns in the index */
003842    int nExtra,          /* Number of bytes of extra space to alloc */
003843    char **ppExtra       /* Pointer to the "extra" space */
003844  ){
003845    Index *p;            /* Allocated index object */
003846    int nByte;           /* Bytes of space for Index object + arrays */
003847  
003848    nByte = ROUND8(sizeof(Index)) +              /* Index structure  */
003849            ROUND8(sizeof(char*)*nCol) +         /* Index.azColl     */
003850            ROUND8(sizeof(LogEst)*(nCol+1) +     /* Index.aiRowLogEst   */
003851                   sizeof(i16)*nCol +            /* Index.aiColumn   */
003852                   sizeof(u8)*nCol);             /* Index.aSortOrder */
003853    p = sqlite3DbMallocZero(db, nByte + nExtra);
003854    if( p ){
003855      char *pExtra = ((char*)p)+ROUND8(sizeof(Index));
003856      p->azColl = (const char**)pExtra; pExtra += ROUND8(sizeof(char*)*nCol);
003857      p->aiRowLogEst = (LogEst*)pExtra; pExtra += sizeof(LogEst)*(nCol+1);
003858      p->aiColumn = (i16*)pExtra;       pExtra += sizeof(i16)*nCol;
003859      p->aSortOrder = (u8*)pExtra;
003860      p->nColumn = nCol;
003861      p->nKeyCol = nCol - 1;
003862      *ppExtra = ((char*)p) + nByte;
003863    }
003864    return p;
003865  }
003866  
003867  /*
003868  ** If expression list pList contains an expression that was parsed with
003869  ** an explicit "NULLS FIRST" or "NULLS LAST" clause, leave an error in
003870  ** pParse and return non-zero. Otherwise, return zero.
003871  */
003872  int sqlite3HasExplicitNulls(Parse *pParse, ExprList *pList){
003873    if( pList ){
003874      int i;
003875      for(i=0; i<pList->nExpr; i++){
003876        if( pList->a[i].bNulls ){
003877          u8 sf = pList->a[i].sortFlags;
003878          sqlite3ErrorMsg(pParse, "unsupported use of NULLS %s", 
003879              (sf==0 || sf==3) ? "FIRST" : "LAST"
003880          );
003881          return 1;
003882        }
003883      }
003884    }
003885    return 0;
003886  }
003887  
003888  /*
003889  ** Create a new index for an SQL table.  pName1.pName2 is the name of the index 
003890  ** and pTblList is the name of the table that is to be indexed.  Both will 
003891  ** be NULL for a primary key or an index that is created to satisfy a
003892  ** UNIQUE constraint.  If pTable and pIndex are NULL, use pParse->pNewTable
003893  ** as the table to be indexed.  pParse->pNewTable is a table that is
003894  ** currently being constructed by a CREATE TABLE statement.
003895  **
003896  ** pList is a list of columns to be indexed.  pList will be NULL if this
003897  ** is a primary key or unique-constraint on the most recent column added
003898  ** to the table currently under construction.  
003899  */
003900  void sqlite3CreateIndex(
003901    Parse *pParse,     /* All information about this parse */
003902    Token *pName1,     /* First part of index name. May be NULL */
003903    Token *pName2,     /* Second part of index name. May be NULL */
003904    SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */
003905    ExprList *pList,   /* A list of columns to be indexed */
003906    int onError,       /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
003907    Token *pStart,     /* The CREATE token that begins this statement */
003908    Expr *pPIWhere,    /* WHERE clause for partial indices */
003909    int sortOrder,     /* Sort order of primary key when pList==NULL */
003910    int ifNotExist,    /* Omit error if index already exists */
003911    u8 idxType         /* The index type */
003912  ){
003913    Table *pTab = 0;     /* Table to be indexed */
003914    Index *pIndex = 0;   /* The index to be created */
003915    char *zName = 0;     /* Name of the index */
003916    int nName;           /* Number of characters in zName */
003917    int i, j;
003918    DbFixer sFix;        /* For assigning database names to pTable */
003919    int sortOrderMask;   /* 1 to honor DESC in index.  0 to ignore. */
003920    sqlite3 *db = pParse->db;
003921    Db *pDb;             /* The specific table containing the indexed database */
003922    int iDb;             /* Index of the database that is being written */
003923    Token *pName = 0;    /* Unqualified name of the index to create */
003924    struct ExprList_item *pListItem; /* For looping over pList */
003925    int nExtra = 0;                  /* Space allocated for zExtra[] */
003926    int nExtraCol;                   /* Number of extra columns needed */
003927    char *zExtra = 0;                /* Extra space after the Index object */
003928    Index *pPk = 0;      /* PRIMARY KEY index for WITHOUT ROWID tables */
003929  
003930    assert( db->pParse==pParse );
003931    if( pParse->nErr ){
003932      goto exit_create_index;
003933    }
003934    assert( db->mallocFailed==0 );
003935    if( IN_DECLARE_VTAB && idxType!=SQLITE_IDXTYPE_PRIMARYKEY ){
003936      goto exit_create_index;
003937    }
003938    if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
003939      goto exit_create_index;
003940    }
003941    if( sqlite3HasExplicitNulls(pParse, pList) ){
003942      goto exit_create_index;
003943    }
003944  
003945    /*
003946    ** Find the table that is to be indexed.  Return early if not found.
003947    */
003948    if( pTblName!=0 ){
003949  
003950      /* Use the two-part index name to determine the database 
003951      ** to search for the table. 'Fix' the table name to this db
003952      ** before looking up the table.
003953      */
003954      assert( pName1 && pName2 );
003955      iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
003956      if( iDb<0 ) goto exit_create_index;
003957      assert( pName && pName->z );
003958  
003959  #ifndef SQLITE_OMIT_TEMPDB
003960      /* If the index name was unqualified, check if the table
003961      ** is a temp table. If so, set the database to 1. Do not do this
003962      ** if initialising a database schema.
003963      */
003964      if( !db->init.busy ){
003965        pTab = sqlite3SrcListLookup(pParse, pTblName);
003966        if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
003967          iDb = 1;
003968        }
003969      }
003970  #endif
003971  
003972      sqlite3FixInit(&sFix, pParse, iDb, "index", pName);
003973      if( sqlite3FixSrcList(&sFix, pTblName) ){
003974        /* Because the parser constructs pTblName from a single identifier,
003975        ** sqlite3FixSrcList can never fail. */
003976        assert(0);
003977      }
003978      pTab = sqlite3LocateTableItem(pParse, 0, &pTblName->a[0]);
003979      assert( db->mallocFailed==0 || pTab==0 );
003980      if( pTab==0 ) goto exit_create_index;
003981      if( iDb==1 && db->aDb[iDb].pSchema!=pTab->pSchema ){
003982        sqlite3ErrorMsg(pParse, 
003983             "cannot create a TEMP index on non-TEMP table \"%s\"",
003984             pTab->zName);
003985        goto exit_create_index;
003986      }
003987      if( !HasRowid(pTab) ) pPk = sqlite3PrimaryKeyIndex(pTab);
003988    }else{
003989      assert( pName==0 );
003990      assert( pStart==0 );
003991      pTab = pParse->pNewTable;
003992      if( !pTab ) goto exit_create_index;
003993      iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
003994    }
003995    pDb = &db->aDb[iDb];
003996  
003997    assert( pTab!=0 );
003998    if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 
003999         && db->init.busy==0
004000         && pTblName!=0
004001  #if SQLITE_USER_AUTHENTICATION
004002         && sqlite3UserAuthTable(pTab->zName)==0
004003  #endif
004004    ){
004005      sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
004006      goto exit_create_index;
004007    }
004008  #ifndef SQLITE_OMIT_VIEW
004009    if( IsView(pTab) ){
004010      sqlite3ErrorMsg(pParse, "views may not be indexed");
004011      goto exit_create_index;
004012    }
004013  #endif
004014  #ifndef SQLITE_OMIT_VIRTUALTABLE
004015    if( IsVirtual(pTab) ){
004016      sqlite3ErrorMsg(pParse, "virtual tables may not be indexed");
004017      goto exit_create_index;
004018    }
004019  #endif
004020  
004021    /*
004022    ** Find the name of the index.  Make sure there is not already another
004023    ** index or table with the same name.  
004024    **
004025    ** Exception:  If we are reading the names of permanent indices from the
004026    ** sqlite_schema table (because some other process changed the schema) and
004027    ** one of the index names collides with the name of a temporary table or
004028    ** index, then we will continue to process this index.
004029    **
004030    ** If pName==0 it means that we are
004031    ** dealing with a primary key or UNIQUE constraint.  We have to invent our
004032    ** own name.
004033    */
004034    if( pName ){
004035      zName = sqlite3NameFromToken(db, pName);
004036      if( zName==0 ) goto exit_create_index;
004037      assert( pName->z!=0 );
004038      if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName,"index",pTab->zName) ){
004039        goto exit_create_index;
004040      }
004041      if( !IN_RENAME_OBJECT ){
004042        if( !db->init.busy ){
004043          if( sqlite3FindTable(db, zName, 0)!=0 ){
004044            sqlite3ErrorMsg(pParse, "there is already a table named %s", zName);
004045            goto exit_create_index;
004046          }
004047        }
004048        if( sqlite3FindIndex(db, zName, pDb->zDbSName)!=0 ){
004049          if( !ifNotExist ){
004050            sqlite3ErrorMsg(pParse, "index %s already exists", zName);
004051          }else{
004052            assert( !db->init.busy );
004053            sqlite3CodeVerifySchema(pParse, iDb);
004054            sqlite3ForceNotReadOnly(pParse);
004055          }
004056          goto exit_create_index;
004057        }
004058      }
004059    }else{
004060      int n;
004061      Index *pLoop;
004062      for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
004063      zName = sqlite3MPrintf(db, "sqlite_autoindex_%s_%d", pTab->zName, n);
004064      if( zName==0 ){
004065        goto exit_create_index;
004066      }
004067  
004068      /* Automatic index names generated from within sqlite3_declare_vtab()
004069      ** must have names that are distinct from normal automatic index names.
004070      ** The following statement converts "sqlite3_autoindex..." into
004071      ** "sqlite3_butoindex..." in order to make the names distinct.
004072      ** The "vtab_err.test" test demonstrates the need of this statement. */
004073      if( IN_SPECIAL_PARSE ) zName[7]++;
004074    }
004075  
004076    /* Check for authorization to create an index.
004077    */
004078  #ifndef SQLITE_OMIT_AUTHORIZATION
004079    if( !IN_RENAME_OBJECT ){
004080      const char *zDb = pDb->zDbSName;
004081      if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iDb), 0, zDb) ){
004082        goto exit_create_index;
004083      }
004084      i = SQLITE_CREATE_INDEX;
004085      if( !OMIT_TEMPDB && iDb==1 ) i = SQLITE_CREATE_TEMP_INDEX;
004086      if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){
004087        goto exit_create_index;
004088      }
004089    }
004090  #endif
004091  
004092    /* If pList==0, it means this routine was called to make a primary
004093    ** key out of the last column added to the table under construction.
004094    ** So create a fake list to simulate this.
004095    */
004096    if( pList==0 ){
004097      Token prevCol;
004098      Column *pCol = &pTab->aCol[pTab->nCol-1];
004099      pCol->colFlags |= COLFLAG_UNIQUE;
004100      sqlite3TokenInit(&prevCol, pCol->zCnName);
004101      pList = sqlite3ExprListAppend(pParse, 0,
004102                sqlite3ExprAlloc(db, TK_ID, &prevCol, 0));
004103      if( pList==0 ) goto exit_create_index;
004104      assert( pList->nExpr==1 );
004105      sqlite3ExprListSetSortOrder(pList, sortOrder, SQLITE_SO_UNDEFINED);
004106    }else{
004107      sqlite3ExprListCheckLength(pParse, pList, "index");
004108      if( pParse->nErr ) goto exit_create_index;
004109    }
004110  
004111    /* Figure out how many bytes of space are required to store explicitly
004112    ** specified collation sequence names.
004113    */
004114    for(i=0; i<pList->nExpr; i++){
004115      Expr *pExpr = pList->a[i].pExpr;
004116      assert( pExpr!=0 );
004117      if( pExpr->op==TK_COLLATE ){
004118        assert( !ExprHasProperty(pExpr, EP_IntValue) );
004119        nExtra += (1 + sqlite3Strlen30(pExpr->u.zToken));
004120      }
004121    }
004122  
004123    /* 
004124    ** Allocate the index structure. 
004125    */
004126    nName = sqlite3Strlen30(zName);
004127    nExtraCol = pPk ? pPk->nKeyCol : 1;
004128    assert( pList->nExpr + nExtraCol <= 32767 /* Fits in i16 */ );
004129    pIndex = sqlite3AllocateIndexObject(db, pList->nExpr + nExtraCol,
004130                                        nName + nExtra + 1, &zExtra);
004131    if( db->mallocFailed ){
004132      goto exit_create_index;
004133    }
004134    assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowLogEst) );
004135    assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) );
004136    pIndex->zName = zExtra;
004137    zExtra += nName + 1;
004138    memcpy(pIndex->zName, zName, nName+1);
004139    pIndex->pTable = pTab;
004140    pIndex->onError = (u8)onError;
004141    pIndex->uniqNotNull = onError!=OE_None;
004142    pIndex->idxType = idxType;
004143    pIndex->pSchema = db->aDb[iDb].pSchema;
004144    pIndex->nKeyCol = pList->nExpr;
004145    if( pPIWhere ){
004146      sqlite3ResolveSelfReference(pParse, pTab, NC_PartIdx, pPIWhere, 0);
004147      pIndex->pPartIdxWhere = pPIWhere;
004148      pPIWhere = 0;
004149    }
004150    assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
004151  
004152    /* Check to see if we should honor DESC requests on index columns
004153    */
004154    if( pDb->pSchema->file_format>=4 ){
004155      sortOrderMask = -1;   /* Honor DESC */
004156    }else{
004157      sortOrderMask = 0;    /* Ignore DESC */
004158    }
004159  
004160    /* Analyze the list of expressions that form the terms of the index and
004161    ** report any errors.  In the common case where the expression is exactly
004162    ** a table column, store that column in aiColumn[].  For general expressions,
004163    ** populate pIndex->aColExpr and store XN_EXPR (-2) in aiColumn[].
004164    **
004165    ** TODO: Issue a warning if two or more columns of the index are identical.
004166    ** TODO: Issue a warning if the table primary key is used as part of the
004167    ** index key.
004168    */
004169    pListItem = pList->a;
004170    if( IN_RENAME_OBJECT ){
004171      pIndex->aColExpr = pList;
004172      pList = 0;
004173    }
004174    for(i=0; i<pIndex->nKeyCol; i++, pListItem++){
004175      Expr *pCExpr;                  /* The i-th index expression */
004176      int requestedSortOrder;        /* ASC or DESC on the i-th expression */
004177      const char *zColl;             /* Collation sequence name */
004178  
004179      sqlite3StringToId(pListItem->pExpr);
004180      sqlite3ResolveSelfReference(pParse, pTab, NC_IdxExpr, pListItem->pExpr, 0);
004181      if( pParse->nErr ) goto exit_create_index;
004182      pCExpr = sqlite3ExprSkipCollate(pListItem->pExpr);
004183      if( pCExpr->op!=TK_COLUMN ){
004184        if( pTab==pParse->pNewTable ){
004185          sqlite3ErrorMsg(pParse, "expressions prohibited in PRIMARY KEY and "
004186                                  "UNIQUE constraints");
004187          goto exit_create_index;
004188        }
004189        if( pIndex->aColExpr==0 ){
004190          pIndex->aColExpr = pList;
004191          pList = 0;
004192        }
004193        j = XN_EXPR;
004194        pIndex->aiColumn[i] = XN_EXPR;
004195        pIndex->uniqNotNull = 0;
004196      }else{
004197        j = pCExpr->iColumn;
004198        assert( j<=0x7fff );
004199        if( j<0 ){
004200          j = pTab->iPKey;
004201        }else{
004202          if( pTab->aCol[j].notNull==0 ){
004203            pIndex->uniqNotNull = 0;
004204          }
004205          if( pTab->aCol[j].colFlags & COLFLAG_VIRTUAL ){
004206            pIndex->bHasVCol = 1;
004207          }
004208        }
004209        pIndex->aiColumn[i] = (i16)j;
004210      }
004211      zColl = 0;
004212      if( pListItem->pExpr->op==TK_COLLATE ){
004213        int nColl;
004214        assert( !ExprHasProperty(pListItem->pExpr, EP_IntValue) );
004215        zColl = pListItem->pExpr->u.zToken;
004216        nColl = sqlite3Strlen30(zColl) + 1;
004217        assert( nExtra>=nColl );
004218        memcpy(zExtra, zColl, nColl);
004219        zColl = zExtra;
004220        zExtra += nColl;
004221        nExtra -= nColl;
004222      }else if( j>=0 ){
004223        zColl = sqlite3ColumnColl(&pTab->aCol[j]);
004224      }
004225      if( !zColl ) zColl = sqlite3StrBINARY;
004226      if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl) ){
004227        goto exit_create_index;
004228      }
004229      pIndex->azColl[i] = zColl;
004230      requestedSortOrder = pListItem->sortFlags & sortOrderMask;
004231      pIndex->aSortOrder[i] = (u8)requestedSortOrder;
004232    }
004233  
004234    /* Append the table key to the end of the index.  For WITHOUT ROWID
004235    ** tables (when pPk!=0) this will be the declared PRIMARY KEY.  For
004236    ** normal tables (when pPk==0) this will be the rowid.
004237    */
004238    if( pPk ){
004239      for(j=0; j<pPk->nKeyCol; j++){
004240        int x = pPk->aiColumn[j];
004241        assert( x>=0 );
004242        if( isDupColumn(pIndex, pIndex->nKeyCol, pPk, j) ){
004243          pIndex->nColumn--; 
004244        }else{
004245          testcase( hasColumn(pIndex->aiColumn,pIndex->nKeyCol,x) );
004246          pIndex->aiColumn[i] = x;
004247          pIndex->azColl[i] = pPk->azColl[j];
004248          pIndex->aSortOrder[i] = pPk->aSortOrder[j];
004249          i++;
004250        }
004251      }
004252      assert( i==pIndex->nColumn );
004253    }else{
004254      pIndex->aiColumn[i] = XN_ROWID;
004255      pIndex->azColl[i] = sqlite3StrBINARY;
004256    }
004257    sqlite3DefaultRowEst(pIndex);
004258    if( pParse->pNewTable==0 ) estimateIndexWidth(pIndex);
004259  
004260    /* If this index contains every column of its table, then mark
004261    ** it as a covering index */
004262    assert( HasRowid(pTab) 
004263        || pTab->iPKey<0 || sqlite3TableColumnToIndex(pIndex, pTab->iPKey)>=0 );
004264    recomputeColumnsNotIndexed(pIndex);
004265    if( pTblName!=0 && pIndex->nColumn>=pTab->nCol ){
004266      pIndex->isCovering = 1;
004267      for(j=0; j<pTab->nCol; j++){
004268        if( j==pTab->iPKey ) continue;
004269        if( sqlite3TableColumnToIndex(pIndex,j)>=0 ) continue;
004270        pIndex->isCovering = 0;
004271        break;
004272      }
004273    }
004274  
004275    if( pTab==pParse->pNewTable ){
004276      /* This routine has been called to create an automatic index as a
004277      ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or
004278      ** a PRIMARY KEY or UNIQUE clause following the column definitions.
004279      ** i.e. one of:
004280      **
004281      ** CREATE TABLE t(x PRIMARY KEY, y);
004282      ** CREATE TABLE t(x, y, UNIQUE(x, y));
004283      **
004284      ** Either way, check to see if the table already has such an index. If
004285      ** so, don't bother creating this one. This only applies to
004286      ** automatically created indices. Users can do as they wish with
004287      ** explicit indices.
004288      **
004289      ** Two UNIQUE or PRIMARY KEY constraints are considered equivalent
004290      ** (and thus suppressing the second one) even if they have different
004291      ** sort orders.
004292      **
004293      ** If there are different collating sequences or if the columns of
004294      ** the constraint occur in different orders, then the constraints are
004295      ** considered distinct and both result in separate indices.
004296      */
004297      Index *pIdx;
004298      for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
004299        int k;
004300        assert( IsUniqueIndex(pIdx) );
004301        assert( pIdx->idxType!=SQLITE_IDXTYPE_APPDEF );
004302        assert( IsUniqueIndex(pIndex) );
004303  
004304        if( pIdx->nKeyCol!=pIndex->nKeyCol ) continue;
004305        for(k=0; k<pIdx->nKeyCol; k++){
004306          const char *z1;
004307          const char *z2;
004308          assert( pIdx->aiColumn[k]>=0 );
004309          if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break;
004310          z1 = pIdx->azColl[k];
004311          z2 = pIndex->azColl[k];
004312          if( sqlite3StrICmp(z1, z2) ) break;
004313        }
004314        if( k==pIdx->nKeyCol ){
004315          if( pIdx->onError!=pIndex->onError ){
004316            /* This constraint creates the same index as a previous
004317            ** constraint specified somewhere in the CREATE TABLE statement.
004318            ** However the ON CONFLICT clauses are different. If both this 
004319            ** constraint and the previous equivalent constraint have explicit
004320            ** ON CONFLICT clauses this is an error. Otherwise, use the
004321            ** explicitly specified behavior for the index.
004322            */
004323            if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){
004324              sqlite3ErrorMsg(pParse, 
004325                  "conflicting ON CONFLICT clauses specified", 0);
004326            }
004327            if( pIdx->onError==OE_Default ){
004328              pIdx->onError = pIndex->onError;
004329            }
004330          }
004331          if( idxType==SQLITE_IDXTYPE_PRIMARYKEY ) pIdx->idxType = idxType;
004332          if( IN_RENAME_OBJECT ){
004333            pIndex->pNext = pParse->pNewIndex;
004334            pParse->pNewIndex = pIndex;
004335            pIndex = 0;
004336          }
004337          goto exit_create_index;
004338        }
004339      }
004340    }
004341  
004342    if( !IN_RENAME_OBJECT ){
004343  
004344      /* Link the new Index structure to its table and to the other
004345      ** in-memory database structures. 
004346      */
004347      assert( pParse->nErr==0 );
004348      if( db->init.busy ){
004349        Index *p;
004350        assert( !IN_SPECIAL_PARSE );
004351        assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
004352        if( pTblName!=0 ){
004353          pIndex->tnum = db->init.newTnum;
004354          if( sqlite3IndexHasDuplicateRootPage(pIndex) ){
004355            sqlite3ErrorMsg(pParse, "invalid rootpage");
004356            pParse->rc = SQLITE_CORRUPT_BKPT;
004357            goto exit_create_index;
004358          }
004359        }
004360        p = sqlite3HashInsert(&pIndex->pSchema->idxHash, 
004361            pIndex->zName, pIndex);
004362        if( p ){
004363          assert( p==pIndex );  /* Malloc must have failed */
004364          sqlite3OomFault(db);
004365          goto exit_create_index;
004366        }
004367        db->mDbFlags |= DBFLAG_SchemaChange;
004368      }
004369  
004370      /* If this is the initial CREATE INDEX statement (or CREATE TABLE if the
004371      ** index is an implied index for a UNIQUE or PRIMARY KEY constraint) then
004372      ** emit code to allocate the index rootpage on disk and make an entry for
004373      ** the index in the sqlite_schema table and populate the index with
004374      ** content.  But, do not do this if we are simply reading the sqlite_schema
004375      ** table to parse the schema, or if this index is the PRIMARY KEY index
004376      ** of a WITHOUT ROWID table.
004377      **
004378      ** If pTblName==0 it means this index is generated as an implied PRIMARY KEY
004379      ** or UNIQUE index in a CREATE TABLE statement.  Since the table
004380      ** has just been created, it contains no data and the index initialization
004381      ** step can be skipped.
004382      */
004383      else if( HasRowid(pTab) || pTblName!=0 ){
004384        Vdbe *v;
004385        char *zStmt;
004386        int iMem = ++pParse->nMem;
004387  
004388        v = sqlite3GetVdbe(pParse);
004389        if( v==0 ) goto exit_create_index;
004390  
004391        sqlite3BeginWriteOperation(pParse, 1, iDb);
004392  
004393        /* Create the rootpage for the index using CreateIndex. But before
004394        ** doing so, code a Noop instruction and store its address in 
004395        ** Index.tnum. This is required in case this index is actually a 
004396        ** PRIMARY KEY and the table is actually a WITHOUT ROWID table. In 
004397        ** that case the convertToWithoutRowidTable() routine will replace
004398        ** the Noop with a Goto to jump over the VDBE code generated below. */
004399        pIndex->tnum = (Pgno)sqlite3VdbeAddOp0(v, OP_Noop);
004400        sqlite3VdbeAddOp3(v, OP_CreateBtree, iDb, iMem, BTREE_BLOBKEY);
004401  
004402        /* Gather the complete text of the CREATE INDEX statement into
004403        ** the zStmt variable
004404        */
004405        assert( pName!=0 || pStart==0 );
004406        if( pStart ){
004407          int n = (int)(pParse->sLastToken.z - pName->z) + pParse->sLastToken.n;
004408          if( pName->z[n-1]==';' ) n--;
004409          /* A named index with an explicit CREATE INDEX statement */
004410          zStmt = sqlite3MPrintf(db, "CREATE%s INDEX %.*s",
004411              onError==OE_None ? "" : " UNIQUE", n, pName->z);
004412        }else{
004413          /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */
004414          /* zStmt = sqlite3MPrintf(""); */
004415          zStmt = 0;
004416        }
004417  
004418        /* Add an entry in sqlite_schema for this index
004419        */
004420        sqlite3NestedParse(pParse, 
004421           "INSERT INTO %Q." LEGACY_SCHEMA_TABLE " VALUES('index',%Q,%Q,#%d,%Q);",
004422           db->aDb[iDb].zDbSName,
004423           pIndex->zName,
004424           pTab->zName,
004425           iMem,
004426           zStmt
004427        );
004428        sqlite3DbFree(db, zStmt);
004429  
004430        /* Fill the index with data and reparse the schema. Code an OP_Expire
004431        ** to invalidate all pre-compiled statements.
004432        */
004433        if( pTblName ){
004434          sqlite3RefillIndex(pParse, pIndex, iMem);
004435          sqlite3ChangeCookie(pParse, iDb);
004436          sqlite3VdbeAddParseSchemaOp(v, iDb,
004437              sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName), 0);
004438          sqlite3VdbeAddOp2(v, OP_Expire, 0, 1);
004439        }
004440  
004441        sqlite3VdbeJumpHere(v, (int)pIndex->tnum);
004442      }
004443    }
004444    if( db->init.busy || pTblName==0 ){
004445      pIndex->pNext = pTab->pIndex;
004446      pTab->pIndex = pIndex;
004447      pIndex = 0;
004448    }
004449    else if( IN_RENAME_OBJECT ){
004450      assert( pParse->pNewIndex==0 );
004451      pParse->pNewIndex = pIndex;
004452      pIndex = 0;
004453    }
004454  
004455    /* Clean up before exiting */
004456  exit_create_index:
004457    if( pIndex ) sqlite3FreeIndex(db, pIndex);
004458    if( pTab ){
004459      /* Ensure all REPLACE indexes on pTab are at the end of the pIndex list.
004460      ** The list was already ordered when this routine was entered, so at this
004461      ** point at most a single index (the newly added index) will be out of
004462      ** order.  So we have to reorder at most one index. */
004463      Index **ppFrom;
004464      Index *pThis;
004465      for(ppFrom=&pTab->pIndex; (pThis = *ppFrom)!=0; ppFrom=&pThis->pNext){
004466        Index *pNext;
004467        if( pThis->onError!=OE_Replace ) continue;
004468        while( (pNext = pThis->pNext)!=0 && pNext->onError!=OE_Replace ){
004469          *ppFrom = pNext;
004470          pThis->pNext = pNext->pNext;
004471          pNext->pNext = pThis;
004472          ppFrom = &pNext->pNext;
004473        }
004474        break;
004475      }
004476  #ifdef SQLITE_DEBUG
004477      /* Verify that all REPLACE indexes really are now at the end
004478      ** of the index list.  In other words, no other index type ever
004479      ** comes after a REPLACE index on the list. */
004480      for(pThis = pTab->pIndex; pThis; pThis=pThis->pNext){
004481        assert( pThis->onError!=OE_Replace
004482             || pThis->pNext==0
004483             || pThis->pNext->onError==OE_Replace );
004484      }
004485  #endif
004486    }
004487    sqlite3ExprDelete(db, pPIWhere);
004488    sqlite3ExprListDelete(db, pList);
004489    sqlite3SrcListDelete(db, pTblName);
004490    sqlite3DbFree(db, zName);
004491  }
004492  
004493  /*
004494  ** Fill the Index.aiRowEst[] array with default information - information
004495  ** to be used when we have not run the ANALYZE command.
004496  **
004497  ** aiRowEst[0] is supposed to contain the number of elements in the index.
004498  ** Since we do not know, guess 1 million.  aiRowEst[1] is an estimate of the
004499  ** number of rows in the table that match any particular value of the
004500  ** first column of the index.  aiRowEst[2] is an estimate of the number
004501  ** of rows that match any particular combination of the first 2 columns
004502  ** of the index.  And so forth.  It must always be the case that
004503  *
004504  **           aiRowEst[N]<=aiRowEst[N-1]
004505  **           aiRowEst[N]>=1
004506  **
004507  ** Apart from that, we have little to go on besides intuition as to
004508  ** how aiRowEst[] should be initialized.  The numbers generated here
004509  ** are based on typical values found in actual indices.
004510  */
004511  void sqlite3DefaultRowEst(Index *pIdx){
004512                 /*                10,  9,  8,  7,  6 */
004513    static const LogEst aVal[] = { 33, 32, 30, 28, 26 };
004514    LogEst *a = pIdx->aiRowLogEst;
004515    LogEst x;
004516    int nCopy = MIN(ArraySize(aVal), pIdx->nKeyCol);
004517    int i;
004518  
004519    /* Indexes with default row estimates should not have stat1 data */
004520    assert( !pIdx->hasStat1 );
004521  
004522    /* Set the first entry (number of rows in the index) to the estimated 
004523    ** number of rows in the table, or half the number of rows in the table
004524    ** for a partial index.
004525    **
004526    ** 2020-05-27:  If some of the stat data is coming from the sqlite_stat1
004527    ** table but other parts we are having to guess at, then do not let the
004528    ** estimated number of rows in the table be less than 1000 (LogEst 99).
004529    ** Failure to do this can cause the indexes for which we do not have
004530    ** stat1 data to be ignored by the query planner.
004531    */
004532    x = pIdx->pTable->nRowLogEst;
004533    assert( 99==sqlite3LogEst(1000) );
004534    if( x<99 ){
004535      pIdx->pTable->nRowLogEst = x = 99;
004536    }
004537    if( pIdx->pPartIdxWhere!=0 ){ x -= 10;  assert( 10==sqlite3LogEst(2) ); }
004538    a[0] = x;
004539  
004540    /* Estimate that a[1] is 10, a[2] is 9, a[3] is 8, a[4] is 7, a[5] is
004541    ** 6 and each subsequent value (if any) is 5.  */
004542    memcpy(&a[1], aVal, nCopy*sizeof(LogEst));
004543    for(i=nCopy+1; i<=pIdx->nKeyCol; i++){
004544      a[i] = 23;                    assert( 23==sqlite3LogEst(5) );
004545    }
004546  
004547    assert( 0==sqlite3LogEst(1) );
004548    if( IsUniqueIndex(pIdx) ) a[pIdx->nKeyCol] = 0;
004549  }
004550  
004551  /*
004552  ** This routine will drop an existing named index.  This routine
004553  ** implements the DROP INDEX statement.
004554  */
004555  void sqlite3DropIndex(Parse *pParse, SrcList *pName, int ifExists){
004556    Index *pIndex;
004557    Vdbe *v;
004558    sqlite3 *db = pParse->db;
004559    int iDb;
004560  
004561    if( db->mallocFailed ){
004562      goto exit_drop_index;
004563    }
004564    assert( pParse->nErr==0 );   /* Never called with prior non-OOM errors */
004565    assert( pName->nSrc==1 );
004566    if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
004567      goto exit_drop_index;
004568    }
004569    pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
004570    if( pIndex==0 ){
004571      if( !ifExists ){
004572        sqlite3ErrorMsg(pParse, "no such index: %S", pName->a);
004573      }else{
004574        sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
004575        sqlite3ForceNotReadOnly(pParse);
004576      }
004577      pParse->checkSchema = 1;
004578      goto exit_drop_index;
004579    }
004580    if( pIndex->idxType!=SQLITE_IDXTYPE_APPDEF ){
004581      sqlite3ErrorMsg(pParse, "index associated with UNIQUE "
004582        "or PRIMARY KEY constraint cannot be dropped", 0);
004583      goto exit_drop_index;
004584    }
004585    iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
004586  #ifndef SQLITE_OMIT_AUTHORIZATION
004587    {
004588      int code = SQLITE_DROP_INDEX;
004589      Table *pTab = pIndex->pTable;
004590      const char *zDb = db->aDb[iDb].zDbSName;
004591      const char *zTab = SCHEMA_TABLE(iDb);
004592      if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
004593        goto exit_drop_index;
004594      }
004595      if( !OMIT_TEMPDB && iDb==1 ) code = SQLITE_DROP_TEMP_INDEX;
004596      if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
004597        goto exit_drop_index;
004598      }
004599    }
004600  #endif
004601  
004602    /* Generate code to remove the index and from the schema table */
004603    v = sqlite3GetVdbe(pParse);
004604    if( v ){
004605      sqlite3BeginWriteOperation(pParse, 1, iDb);
004606      sqlite3NestedParse(pParse,
004607         "DELETE FROM %Q." LEGACY_SCHEMA_TABLE " WHERE name=%Q AND type='index'",
004608         db->aDb[iDb].zDbSName, pIndex->zName
004609      );
004610      sqlite3ClearStatTables(pParse, iDb, "idx", pIndex->zName);
004611      sqlite3ChangeCookie(pParse, iDb);
004612      destroyRootPage(pParse, pIndex->tnum, iDb);
004613      sqlite3VdbeAddOp4(v, OP_DropIndex, iDb, 0, 0, pIndex->zName, 0);
004614    }
004615  
004616  exit_drop_index:
004617    sqlite3SrcListDelete(db, pName);
004618  }
004619  
004620  /*
004621  ** pArray is a pointer to an array of objects. Each object in the
004622  ** array is szEntry bytes in size. This routine uses sqlite3DbRealloc()
004623  ** to extend the array so that there is space for a new object at the end.
004624  **
004625  ** When this function is called, *pnEntry contains the current size of
004626  ** the array (in entries - so the allocation is ((*pnEntry) * szEntry) bytes
004627  ** in total).
004628  **
004629  ** If the realloc() is successful (i.e. if no OOM condition occurs), the
004630  ** space allocated for the new object is zeroed, *pnEntry updated to
004631  ** reflect the new size of the array and a pointer to the new allocation
004632  ** returned. *pIdx is set to the index of the new array entry in this case.
004633  **
004634  ** Otherwise, if the realloc() fails, *pIdx is set to -1, *pnEntry remains
004635  ** unchanged and a copy of pArray returned.
004636  */
004637  void *sqlite3ArrayAllocate(
004638    sqlite3 *db,      /* Connection to notify of malloc failures */
004639    void *pArray,     /* Array of objects.  Might be reallocated */
004640    int szEntry,      /* Size of each object in the array */
004641    int *pnEntry,     /* Number of objects currently in use */
004642    int *pIdx         /* Write the index of a new slot here */
004643  ){
004644    char *z;
004645    sqlite3_int64 n = *pIdx = *pnEntry;
004646    if( (n & (n-1))==0 ){
004647      sqlite3_int64 sz = (n==0) ? 1 : 2*n;
004648      void *pNew = sqlite3DbRealloc(db, pArray, sz*szEntry);
004649      if( pNew==0 ){
004650        *pIdx = -1;
004651        return pArray;
004652      }
004653      pArray = pNew;
004654    }
004655    z = (char*)pArray;
004656    memset(&z[n * szEntry], 0, szEntry);
004657    ++*pnEntry;
004658    return pArray;
004659  }
004660  
004661  /*
004662  ** Append a new element to the given IdList.  Create a new IdList if
004663  ** need be.
004664  **
004665  ** A new IdList is returned, or NULL if malloc() fails.
004666  */
004667  IdList *sqlite3IdListAppend(Parse *pParse, IdList *pList, Token *pToken){
004668    sqlite3 *db = pParse->db;
004669    int i;
004670    if( pList==0 ){
004671      pList = sqlite3DbMallocZero(db, sizeof(IdList) );
004672      if( pList==0 ) return 0;
004673    }
004674    pList->a = sqlite3ArrayAllocate(
004675        db,
004676        pList->a,
004677        sizeof(pList->a[0]),
004678        &pList->nId,
004679        &i
004680    );
004681    if( i<0 ){
004682      sqlite3IdListDelete(db, pList);
004683      return 0;
004684    }
004685    pList->a[i].zName = sqlite3NameFromToken(db, pToken);
004686    if( IN_RENAME_OBJECT && pList->a[i].zName ){
004687      sqlite3RenameTokenMap(pParse, (void*)pList->a[i].zName, pToken);
004688    }
004689    return pList;
004690  }
004691  
004692  /*
004693  ** Delete an IdList.
004694  */
004695  void sqlite3IdListDelete(sqlite3 *db, IdList *pList){
004696    int i;
004697    if( pList==0 ) return;
004698    for(i=0; i<pList->nId; i++){
004699      sqlite3DbFree(db, pList->a[i].zName);
004700    }
004701    sqlite3DbFree(db, pList->a);
004702    sqlite3DbFreeNN(db, pList);
004703  }
004704  
004705  /*
004706  ** Return the index in pList of the identifier named zId.  Return -1
004707  ** if not found.
004708  */
004709  int sqlite3IdListIndex(IdList *pList, const char *zName){
004710    int i;
004711    if( pList==0 ) return -1;
004712    for(i=0; i<pList->nId; i++){
004713      if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i;
004714    }
004715    return -1;
004716  }
004717  
004718  /*
004719  ** Maximum size of a SrcList object.
004720  ** The SrcList object is used to represent the FROM clause of a
004721  ** SELECT statement, and the query planner cannot deal with more
004722  ** than 64 tables in a join.  So any value larger than 64 here
004723  ** is sufficient for most uses.  Smaller values, like say 10, are
004724  ** appropriate for small and memory-limited applications.
004725  */
004726  #ifndef SQLITE_MAX_SRCLIST
004727  # define SQLITE_MAX_SRCLIST 200
004728  #endif
004729  
004730  /*
004731  ** Expand the space allocated for the given SrcList object by
004732  ** creating nExtra new slots beginning at iStart.  iStart is zero based.
004733  ** New slots are zeroed.
004734  **
004735  ** For example, suppose a SrcList initially contains two entries: A,B.
004736  ** To append 3 new entries onto the end, do this:
004737  **
004738  **    sqlite3SrcListEnlarge(db, pSrclist, 3, 2);
004739  **
004740  ** After the call above it would contain:  A, B, nil, nil, nil.
004741  ** If the iStart argument had been 1 instead of 2, then the result
004742  ** would have been:  A, nil, nil, nil, B.  To prepend the new slots,
004743  ** the iStart value would be 0.  The result then would
004744  ** be: nil, nil, nil, A, B.
004745  **
004746  ** If a memory allocation fails or the SrcList becomes too large, leave
004747  ** the original SrcList unchanged, return NULL, and leave an error message
004748  ** in pParse.
004749  */
004750  SrcList *sqlite3SrcListEnlarge(
004751    Parse *pParse,     /* Parsing context into which errors are reported */
004752    SrcList *pSrc,     /* The SrcList to be enlarged */
004753    int nExtra,        /* Number of new slots to add to pSrc->a[] */
004754    int iStart         /* Index in pSrc->a[] of first new slot */
004755  ){
004756    int i;
004757  
004758    /* Sanity checking on calling parameters */
004759    assert( iStart>=0 );
004760    assert( nExtra>=1 );
004761    assert( pSrc!=0 );
004762    assert( iStart<=pSrc->nSrc );
004763  
004764    /* Allocate additional space if needed */
004765    if( (u32)pSrc->nSrc+nExtra>pSrc->nAlloc ){
004766      SrcList *pNew;
004767      sqlite3_int64 nAlloc = 2*(sqlite3_int64)pSrc->nSrc+nExtra;
004768      sqlite3 *db = pParse->db;
004769  
004770      if( pSrc->nSrc+nExtra>=SQLITE_MAX_SRCLIST ){
004771        sqlite3ErrorMsg(pParse, "too many FROM clause terms, max: %d",
004772                        SQLITE_MAX_SRCLIST);
004773        return 0;
004774      }
004775      if( nAlloc>SQLITE_MAX_SRCLIST ) nAlloc = SQLITE_MAX_SRCLIST;
004776      pNew = sqlite3DbRealloc(db, pSrc,
004777                 sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc->a[0]) );
004778      if( pNew==0 ){
004779        assert( db->mallocFailed );
004780        return 0;
004781      }
004782      pSrc = pNew;
004783      pSrc->nAlloc = nAlloc;
004784    }
004785  
004786    /* Move existing slots that come after the newly inserted slots
004787    ** out of the way */
004788    for(i=pSrc->nSrc-1; i>=iStart; i--){
004789      pSrc->a[i+nExtra] = pSrc->a[i];
004790    }
004791    pSrc->nSrc += nExtra;
004792  
004793    /* Zero the newly allocated slots */
004794    memset(&pSrc->a[iStart], 0, sizeof(pSrc->a[0])*nExtra);
004795    for(i=iStart; i<iStart+nExtra; i++){
004796      pSrc->a[i].iCursor = -1;
004797    }
004798  
004799    /* Return a pointer to the enlarged SrcList */
004800    return pSrc;
004801  }
004802  
004803  
004804  /*
004805  ** Append a new table name to the given SrcList.  Create a new SrcList if
004806  ** need be.  A new entry is created in the SrcList even if pTable is NULL.
004807  **
004808  ** A SrcList is returned, or NULL if there is an OOM error or if the
004809  ** SrcList grows to large.  The returned
004810  ** SrcList might be the same as the SrcList that was input or it might be
004811  ** a new one.  If an OOM error does occurs, then the prior value of pList
004812  ** that is input to this routine is automatically freed.
004813  **
004814  ** If pDatabase is not null, it means that the table has an optional
004815  ** database name prefix.  Like this:  "database.table".  The pDatabase
004816  ** points to the table name and the pTable points to the database name.
004817  ** The SrcList.a[].zName field is filled with the table name which might
004818  ** come from pTable (if pDatabase is NULL) or from pDatabase.  
004819  ** SrcList.a[].zDatabase is filled with the database name from pTable,
004820  ** or with NULL if no database is specified.
004821  **
004822  ** In other words, if call like this:
004823  **
004824  **         sqlite3SrcListAppend(D,A,B,0);
004825  **
004826  ** Then B is a table name and the database name is unspecified.  If called
004827  ** like this:
004828  **
004829  **         sqlite3SrcListAppend(D,A,B,C);
004830  **
004831  ** Then C is the table name and B is the database name.  If C is defined
004832  ** then so is B.  In other words, we never have a case where:
004833  **
004834  **         sqlite3SrcListAppend(D,A,0,C);
004835  **
004836  ** Both pTable and pDatabase are assumed to be quoted.  They are dequoted
004837  ** before being added to the SrcList.
004838  */
004839  SrcList *sqlite3SrcListAppend(
004840    Parse *pParse,      /* Parsing context, in which errors are reported */
004841    SrcList *pList,     /* Append to this SrcList. NULL creates a new SrcList */
004842    Token *pTable,      /* Table to append */
004843    Token *pDatabase    /* Database of the table */
004844  ){
004845    SrcItem *pItem;
004846    sqlite3 *db;
004847    assert( pDatabase==0 || pTable!=0 );  /* Cannot have C without B */
004848    assert( pParse!=0 );
004849    assert( pParse->db!=0 );
004850    db = pParse->db;
004851    if( pList==0 ){
004852      pList = sqlite3DbMallocRawNN(pParse->db, sizeof(SrcList) );
004853      if( pList==0 ) return 0;
004854      pList->nAlloc = 1;
004855      pList->nSrc = 1;
004856      memset(&pList->a[0], 0, sizeof(pList->a[0]));
004857      pList->a[0].iCursor = -1;
004858    }else{
004859      SrcList *pNew = sqlite3SrcListEnlarge(pParse, pList, 1, pList->nSrc);
004860      if( pNew==0 ){
004861        sqlite3SrcListDelete(db, pList);
004862        return 0;
004863      }else{
004864        pList = pNew;
004865      }
004866    }
004867    pItem = &pList->a[pList->nSrc-1];
004868    if( pDatabase && pDatabase->z==0 ){
004869      pDatabase = 0;
004870    }
004871    if( pDatabase ){
004872      pItem->zName = sqlite3NameFromToken(db, pDatabase);
004873      pItem->zDatabase = sqlite3NameFromToken(db, pTable);
004874    }else{
004875      pItem->zName = sqlite3NameFromToken(db, pTable);
004876      pItem->zDatabase = 0;
004877    }
004878    return pList;
004879  }
004880  
004881  /*
004882  ** Assign VdbeCursor index numbers to all tables in a SrcList
004883  */
004884  void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){
004885    int i;
004886    SrcItem *pItem;
004887    assert( pList || pParse->db->mallocFailed );
004888    if( ALWAYS(pList) ){
004889      for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
004890        if( pItem->iCursor>=0 ) continue;
004891        pItem->iCursor = pParse->nTab++;
004892        if( pItem->pSelect ){
004893          sqlite3SrcListAssignCursors(pParse, pItem->pSelect->pSrc);
004894        }
004895      }
004896    }
004897  }
004898  
004899  /*
004900  ** Delete an entire SrcList including all its substructure.
004901  */
004902  void sqlite3SrcListDelete(sqlite3 *db, SrcList *pList){
004903    int i;
004904    SrcItem *pItem;
004905    if( pList==0 ) return;
004906    for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
004907      if( pItem->zDatabase ) sqlite3DbFreeNN(db, pItem->zDatabase);
004908      sqlite3DbFree(db, pItem->zName);
004909      if( pItem->zAlias ) sqlite3DbFreeNN(db, pItem->zAlias);
004910      if( pItem->fg.isIndexedBy ) sqlite3DbFree(db, pItem->u1.zIndexedBy);
004911      if( pItem->fg.isTabFunc ) sqlite3ExprListDelete(db, pItem->u1.pFuncArg);
004912      sqlite3DeleteTable(db, pItem->pTab);
004913      if( pItem->pSelect ) sqlite3SelectDelete(db, pItem->pSelect);
004914      if( pItem->pOn ) sqlite3ExprDelete(db, pItem->pOn);
004915      if( pItem->pUsing ) sqlite3IdListDelete(db, pItem->pUsing);
004916    }
004917    sqlite3DbFreeNN(db, pList);
004918  }
004919  
004920  /*
004921  ** This routine is called by the parser to add a new term to the
004922  ** end of a growing FROM clause.  The "p" parameter is the part of
004923  ** the FROM clause that has already been constructed.  "p" is NULL
004924  ** if this is the first term of the FROM clause.  pTable and pDatabase
004925  ** are the name of the table and database named in the FROM clause term.
004926  ** pDatabase is NULL if the database name qualifier is missing - the
004927  ** usual case.  If the term has an alias, then pAlias points to the
004928  ** alias token.  If the term is a subquery, then pSubquery is the
004929  ** SELECT statement that the subquery encodes.  The pTable and
004930  ** pDatabase parameters are NULL for subqueries.  The pOn and pUsing
004931  ** parameters are the content of the ON and USING clauses.
004932  **
004933  ** Return a new SrcList which encodes is the FROM with the new
004934  ** term added.
004935  */
004936  SrcList *sqlite3SrcListAppendFromTerm(
004937    Parse *pParse,          /* Parsing context */
004938    SrcList *p,             /* The left part of the FROM clause already seen */
004939    Token *pTable,          /* Name of the table to add to the FROM clause */
004940    Token *pDatabase,       /* Name of the database containing pTable */
004941    Token *pAlias,          /* The right-hand side of the AS subexpression */
004942    Select *pSubquery,      /* A subquery used in place of a table name */
004943    Expr *pOn,              /* The ON clause of a join */
004944    IdList *pUsing          /* The USING clause of a join */
004945  ){
004946    SrcItem *pItem;
004947    sqlite3 *db = pParse->db;
004948    if( !p && (pOn || pUsing) ){
004949      sqlite3ErrorMsg(pParse, "a JOIN clause is required before %s", 
004950        (pOn ? "ON" : "USING")
004951      );
004952      goto append_from_error;
004953    }
004954    p = sqlite3SrcListAppend(pParse, p, pTable, pDatabase);
004955    if( p==0 ){
004956      goto append_from_error;
004957    }
004958    assert( p->nSrc>0 );
004959    pItem = &p->a[p->nSrc-1];
004960    assert( (pTable==0)==(pDatabase==0) );
004961    assert( pItem->zName==0 || pDatabase!=0 );
004962    if( IN_RENAME_OBJECT && pItem->zName ){
004963      Token *pToken = (ALWAYS(pDatabase) && pDatabase->z) ? pDatabase : pTable;
004964      sqlite3RenameTokenMap(pParse, pItem->zName, pToken);
004965    }
004966    assert( pAlias!=0 );
004967    if( pAlias->n ){
004968      pItem->zAlias = sqlite3NameFromToken(db, pAlias);
004969    }
004970    pItem->pSelect = pSubquery;
004971    pItem->pOn = pOn;
004972    pItem->pUsing = pUsing;
004973    return p;
004974  
004975  append_from_error:
004976    assert( p==0 );
004977    sqlite3ExprDelete(db, pOn);
004978    sqlite3IdListDelete(db, pUsing);
004979    sqlite3SelectDelete(db, pSubquery);
004980    return 0;
004981  }
004982  
004983  /*
004984  ** Add an INDEXED BY or NOT INDEXED clause to the most recently added 
004985  ** element of the source-list passed as the second argument.
004986  */
004987  void sqlite3SrcListIndexedBy(Parse *pParse, SrcList *p, Token *pIndexedBy){
004988    assert( pIndexedBy!=0 );
004989    if( p && pIndexedBy->n>0 ){
004990      SrcItem *pItem;
004991      assert( p->nSrc>0 );
004992      pItem = &p->a[p->nSrc-1];
004993      assert( pItem->fg.notIndexed==0 );
004994      assert( pItem->fg.isIndexedBy==0 );
004995      assert( pItem->fg.isTabFunc==0 );
004996      if( pIndexedBy->n==1 && !pIndexedBy->z ){
004997        /* A "NOT INDEXED" clause was supplied. See parse.y 
004998        ** construct "indexed_opt" for details. */
004999        pItem->fg.notIndexed = 1;
005000      }else{
005001        pItem->u1.zIndexedBy = sqlite3NameFromToken(pParse->db, pIndexedBy);
005002        pItem->fg.isIndexedBy = 1;
005003        assert( pItem->fg.isCte==0 );  /* No collision on union u2 */
005004      }
005005    }
005006  }
005007  
005008  /*
005009  ** Append the contents of SrcList p2 to SrcList p1 and return the resulting
005010  ** SrcList. Or, if an error occurs, return NULL. In all cases, p1 and p2
005011  ** are deleted by this function.
005012  */ 
005013  SrcList *sqlite3SrcListAppendList(Parse *pParse, SrcList *p1, SrcList *p2){
005014    assert( p1 && p1->nSrc==1 );
005015    if( p2 ){
005016      SrcList *pNew = sqlite3SrcListEnlarge(pParse, p1, p2->nSrc, 1);
005017      if( pNew==0 ){
005018        sqlite3SrcListDelete(pParse->db, p2);
005019      }else{
005020        p1 = pNew;
005021        memcpy(&p1->a[1], p2->a, p2->nSrc*sizeof(SrcItem));
005022        sqlite3DbFree(pParse->db, p2);
005023      }
005024    }
005025    return p1;
005026  }
005027  
005028  /*
005029  ** Add the list of function arguments to the SrcList entry for a
005030  ** table-valued-function.
005031  */
005032  void sqlite3SrcListFuncArgs(Parse *pParse, SrcList *p, ExprList *pList){
005033    if( p ){
005034      SrcItem *pItem = &p->a[p->nSrc-1];
005035      assert( pItem->fg.notIndexed==0 );
005036      assert( pItem->fg.isIndexedBy==0 );
005037      assert( pItem->fg.isTabFunc==0 );
005038      pItem->u1.pFuncArg = pList;
005039      pItem->fg.isTabFunc = 1;
005040    }else{
005041      sqlite3ExprListDelete(pParse->db, pList);
005042    }
005043  }
005044  
005045  /*
005046  ** When building up a FROM clause in the parser, the join operator
005047  ** is initially attached to the left operand.  But the code generator
005048  ** expects the join operator to be on the right operand.  This routine
005049  ** Shifts all join operators from left to right for an entire FROM
005050  ** clause.
005051  **
005052  ** Example: Suppose the join is like this:
005053  **
005054  **           A natural cross join B
005055  **
005056  ** The operator is "natural cross join".  The A and B operands are stored
005057  ** in p->a[0] and p->a[1], respectively.  The parser initially stores the
005058  ** operator with A.  This routine shifts that operator over to B.
005059  */
005060  void sqlite3SrcListShiftJoinType(SrcList *p){
005061    if( p ){
005062      int i;
005063      for(i=p->nSrc-1; i>0; i--){
005064        p->a[i].fg.jointype = p->a[i-1].fg.jointype;
005065      }
005066      p->a[0].fg.jointype = 0;
005067    }
005068  }
005069  
005070  /*
005071  ** Generate VDBE code for a BEGIN statement.
005072  */
005073  void sqlite3BeginTransaction(Parse *pParse, int type){
005074    sqlite3 *db;
005075    Vdbe *v;
005076    int i;
005077  
005078    assert( pParse!=0 );
005079    db = pParse->db;
005080    assert( db!=0 );
005081    if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ){
005082      return;
005083    }
005084    v = sqlite3GetVdbe(pParse);
005085    if( !v ) return;
005086    if( type!=TK_DEFERRED ){
005087      for(i=0; i<db->nDb; i++){
005088        int eTxnType;
005089        Btree *pBt = db->aDb[i].pBt;
005090        if( pBt && sqlite3BtreeIsReadonly(pBt) ){
005091          eTxnType = 0;  /* Read txn */
005092        }else if( type==TK_EXCLUSIVE ){
005093          eTxnType = 2;  /* Exclusive txn */
005094        }else{
005095          eTxnType = 1;  /* Write txn */
005096        }
005097        sqlite3VdbeAddOp2(v, OP_Transaction, i, eTxnType);
005098        sqlite3VdbeUsesBtree(v, i);
005099      }
005100    }
005101    sqlite3VdbeAddOp0(v, OP_AutoCommit);
005102  }
005103  
005104  /*
005105  ** Generate VDBE code for a COMMIT or ROLLBACK statement.
005106  ** Code for ROLLBACK is generated if eType==TK_ROLLBACK.  Otherwise
005107  ** code is generated for a COMMIT.
005108  */
005109  void sqlite3EndTransaction(Parse *pParse, int eType){
005110    Vdbe *v;
005111    int isRollback;
005112  
005113    assert( pParse!=0 );
005114    assert( pParse->db!=0 );
005115    assert( eType==TK_COMMIT || eType==TK_END || eType==TK_ROLLBACK );
005116    isRollback = eType==TK_ROLLBACK;
005117    if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, 
005118         isRollback ? "ROLLBACK" : "COMMIT", 0, 0) ){
005119      return;
005120    }
005121    v = sqlite3GetVdbe(pParse);
005122    if( v ){
005123      sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, isRollback);
005124    }
005125  }
005126  
005127  /*
005128  ** This function is called by the parser when it parses a command to create,
005129  ** release or rollback an SQL savepoint. 
005130  */
005131  void sqlite3Savepoint(Parse *pParse, int op, Token *pName){
005132    char *zName = sqlite3NameFromToken(pParse->db, pName);
005133    if( zName ){
005134      Vdbe *v = sqlite3GetVdbe(pParse);
005135  #ifndef SQLITE_OMIT_AUTHORIZATION
005136      static const char * const az[] = { "BEGIN", "RELEASE", "ROLLBACK" };
005137      assert( !SAVEPOINT_BEGIN && SAVEPOINT_RELEASE==1 && SAVEPOINT_ROLLBACK==2 );
005138  #endif
005139      if( !v || sqlite3AuthCheck(pParse, SQLITE_SAVEPOINT, az[op], zName, 0) ){
005140        sqlite3DbFree(pParse->db, zName);
005141        return;
005142      }
005143      sqlite3VdbeAddOp4(v, OP_Savepoint, op, 0, 0, zName, P4_DYNAMIC);
005144    }
005145  }
005146  
005147  /*
005148  ** Make sure the TEMP database is open and available for use.  Return
005149  ** the number of errors.  Leave any error messages in the pParse structure.
005150  */
005151  int sqlite3OpenTempDatabase(Parse *pParse){
005152    sqlite3 *db = pParse->db;
005153    if( db->aDb[1].pBt==0 && !pParse->explain ){
005154      int rc;
005155      Btree *pBt;
005156      static const int flags = 
005157            SQLITE_OPEN_READWRITE |
005158            SQLITE_OPEN_CREATE |
005159            SQLITE_OPEN_EXCLUSIVE |
005160            SQLITE_OPEN_DELETEONCLOSE |
005161            SQLITE_OPEN_TEMP_DB;
005162  
005163      rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pBt, 0, flags);
005164      if( rc!=SQLITE_OK ){
005165        sqlite3ErrorMsg(pParse, "unable to open a temporary database "
005166          "file for storing temporary tables");
005167        pParse->rc = rc;
005168        return 1;
005169      }
005170      db->aDb[1].pBt = pBt;
005171      assert( db->aDb[1].pSchema );
005172      if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, 0, 0) ){
005173        sqlite3OomFault(db);
005174        return 1;
005175      }
005176    }
005177    return 0;
005178  }
005179  
005180  /*
005181  ** Record the fact that the schema cookie will need to be verified
005182  ** for database iDb.  The code to actually verify the schema cookie
005183  ** will occur at the end of the top-level VDBE and will be generated
005184  ** later, by sqlite3FinishCoding().
005185  */
005186  static void sqlite3CodeVerifySchemaAtToplevel(Parse *pToplevel, int iDb){
005187    assert( iDb>=0 && iDb<pToplevel->db->nDb );
005188    assert( pToplevel->db->aDb[iDb].pBt!=0 || iDb==1 );
005189    assert( iDb<SQLITE_MAX_DB );
005190    assert( sqlite3SchemaMutexHeld(pToplevel->db, iDb, 0) );
005191    if( DbMaskTest(pToplevel->cookieMask, iDb)==0 ){
005192      DbMaskSet(pToplevel->cookieMask, iDb);
005193      if( !OMIT_TEMPDB && iDb==1 ){
005194        sqlite3OpenTempDatabase(pToplevel);
005195      }
005196    }
005197  }
005198  void sqlite3CodeVerifySchema(Parse *pParse, int iDb){
005199    sqlite3CodeVerifySchemaAtToplevel(sqlite3ParseToplevel(pParse), iDb);
005200  }
005201  
005202  
005203  /*
005204  ** If argument zDb is NULL, then call sqlite3CodeVerifySchema() for each 
005205  ** attached database. Otherwise, invoke it for the database named zDb only.
005206  */
005207  void sqlite3CodeVerifyNamedSchema(Parse *pParse, const char *zDb){
005208    sqlite3 *db = pParse->db;
005209    int i;
005210    for(i=0; i<db->nDb; i++){
005211      Db *pDb = &db->aDb[i];
005212      if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zDb, pDb->zDbSName)) ){
005213        sqlite3CodeVerifySchema(pParse, i);
005214      }
005215    }
005216  }
005217  
005218  /*
005219  ** Generate VDBE code that prepares for doing an operation that
005220  ** might change the database.
005221  **
005222  ** This routine starts a new transaction if we are not already within
005223  ** a transaction.  If we are already within a transaction, then a checkpoint
005224  ** is set if the setStatement parameter is true.  A checkpoint should
005225  ** be set for operations that might fail (due to a constraint) part of
005226  ** the way through and which will need to undo some writes without having to
005227  ** rollback the whole transaction.  For operations where all constraints
005228  ** can be checked before any changes are made to the database, it is never
005229  ** necessary to undo a write and the checkpoint should not be set.
005230  */
005231  void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){
005232    Parse *pToplevel = sqlite3ParseToplevel(pParse);
005233    sqlite3CodeVerifySchemaAtToplevel(pToplevel, iDb);
005234    DbMaskSet(pToplevel->writeMask, iDb);
005235    pToplevel->isMultiWrite |= setStatement;
005236  }
005237  
005238  /*
005239  ** Indicate that the statement currently under construction might write
005240  ** more than one entry (example: deleting one row then inserting another,
005241  ** inserting multiple rows in a table, or inserting a row and index entries.)
005242  ** If an abort occurs after some of these writes have completed, then it will
005243  ** be necessary to undo the completed writes.
005244  */
005245  void sqlite3MultiWrite(Parse *pParse){
005246    Parse *pToplevel = sqlite3ParseToplevel(pParse);
005247    pToplevel->isMultiWrite = 1;
005248  }
005249  
005250  /* 
005251  ** The code generator calls this routine if is discovers that it is
005252  ** possible to abort a statement prior to completion.  In order to 
005253  ** perform this abort without corrupting the database, we need to make
005254  ** sure that the statement is protected by a statement transaction.
005255  **
005256  ** Technically, we only need to set the mayAbort flag if the
005257  ** isMultiWrite flag was previously set.  There is a time dependency
005258  ** such that the abort must occur after the multiwrite.  This makes
005259  ** some statements involving the REPLACE conflict resolution algorithm
005260  ** go a little faster.  But taking advantage of this time dependency
005261  ** makes it more difficult to prove that the code is correct (in 
005262  ** particular, it prevents us from writing an effective
005263  ** implementation of sqlite3AssertMayAbort()) and so we have chosen
005264  ** to take the safe route and skip the optimization.
005265  */
005266  void sqlite3MayAbort(Parse *pParse){
005267    Parse *pToplevel = sqlite3ParseToplevel(pParse);
005268    pToplevel->mayAbort = 1;
005269  }
005270  
005271  /*
005272  ** Code an OP_Halt that causes the vdbe to return an SQLITE_CONSTRAINT
005273  ** error. The onError parameter determines which (if any) of the statement
005274  ** and/or current transaction is rolled back.
005275  */
005276  void sqlite3HaltConstraint(
005277    Parse *pParse,    /* Parsing context */
005278    int errCode,      /* extended error code */
005279    int onError,      /* Constraint type */
005280    char *p4,         /* Error message */
005281    i8 p4type,        /* P4_STATIC or P4_TRANSIENT */
005282    u8 p5Errmsg       /* P5_ErrMsg type */
005283  ){
005284    Vdbe *v;
005285    assert( pParse->pVdbe!=0 );
005286    v = sqlite3GetVdbe(pParse);
005287    assert( (errCode&0xff)==SQLITE_CONSTRAINT || pParse->nested );
005288    if( onError==OE_Abort ){
005289      sqlite3MayAbort(pParse);
005290    }
005291    sqlite3VdbeAddOp4(v, OP_Halt, errCode, onError, 0, p4, p4type);
005292    sqlite3VdbeChangeP5(v, p5Errmsg);
005293  }
005294  
005295  /*
005296  ** Code an OP_Halt due to UNIQUE or PRIMARY KEY constraint violation.
005297  */
005298  void sqlite3UniqueConstraint(
005299    Parse *pParse,    /* Parsing context */
005300    int onError,      /* Constraint type */
005301    Index *pIdx       /* The index that triggers the constraint */
005302  ){
005303    char *zErr;
005304    int j;
005305    StrAccum errMsg;
005306    Table *pTab = pIdx->pTable;
005307  
005308    sqlite3StrAccumInit(&errMsg, pParse->db, 0, 0, 
005309                        pParse->db->aLimit[SQLITE_LIMIT_LENGTH]);
005310    if( pIdx->aColExpr ){
005311      sqlite3_str_appendf(&errMsg, "index '%q'", pIdx->zName);
005312    }else{
005313      for(j=0; j<pIdx->nKeyCol; j++){
005314        char *zCol;
005315        assert( pIdx->aiColumn[j]>=0 );
005316        zCol = pTab->aCol[pIdx->aiColumn[j]].zCnName;
005317        if( j ) sqlite3_str_append(&errMsg, ", ", 2);
005318        sqlite3_str_appendall(&errMsg, pTab->zName);
005319        sqlite3_str_append(&errMsg, ".", 1);
005320        sqlite3_str_appendall(&errMsg, zCol);
005321      }
005322    }
005323    zErr = sqlite3StrAccumFinish(&errMsg);
005324    sqlite3HaltConstraint(pParse, 
005325      IsPrimaryKeyIndex(pIdx) ? SQLITE_CONSTRAINT_PRIMARYKEY 
005326                              : SQLITE_CONSTRAINT_UNIQUE,
005327      onError, zErr, P4_DYNAMIC, P5_ConstraintUnique);
005328  }
005329  
005330  
005331  /*
005332  ** Code an OP_Halt due to non-unique rowid.
005333  */
005334  void sqlite3RowidConstraint(
005335    Parse *pParse,    /* Parsing context */
005336    int onError,      /* Conflict resolution algorithm */
005337    Table *pTab       /* The table with the non-unique rowid */ 
005338  ){
005339    char *zMsg;
005340    int rc;
005341    if( pTab->iPKey>=0 ){
005342      zMsg = sqlite3MPrintf(pParse->db, "%s.%s", pTab->zName,
005343                            pTab->aCol[pTab->iPKey].zCnName);
005344      rc = SQLITE_CONSTRAINT_PRIMARYKEY;
005345    }else{
005346      zMsg = sqlite3MPrintf(pParse->db, "%s.rowid", pTab->zName);
005347      rc = SQLITE_CONSTRAINT_ROWID;
005348    }
005349    sqlite3HaltConstraint(pParse, rc, onError, zMsg, P4_DYNAMIC,
005350                          P5_ConstraintUnique);
005351  }
005352  
005353  /*
005354  ** Check to see if pIndex uses the collating sequence pColl.  Return
005355  ** true if it does and false if it does not.
005356  */
005357  #ifndef SQLITE_OMIT_REINDEX
005358  static int collationMatch(const char *zColl, Index *pIndex){
005359    int i;
005360    assert( zColl!=0 );
005361    for(i=0; i<pIndex->nColumn; i++){
005362      const char *z = pIndex->azColl[i];
005363      assert( z!=0 || pIndex->aiColumn[i]<0 );
005364      if( pIndex->aiColumn[i]>=0 && 0==sqlite3StrICmp(z, zColl) ){
005365        return 1;
005366      }
005367    }
005368    return 0;
005369  }
005370  #endif
005371  
005372  /*
005373  ** Recompute all indices of pTab that use the collating sequence pColl.
005374  ** If pColl==0 then recompute all indices of pTab.
005375  */
005376  #ifndef SQLITE_OMIT_REINDEX
005377  static void reindexTable(Parse *pParse, Table *pTab, char const *zColl){
005378    if( !IsVirtual(pTab) ){
005379      Index *pIndex;              /* An index associated with pTab */
005380  
005381      for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
005382        if( zColl==0 || collationMatch(zColl, pIndex) ){
005383          int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
005384          sqlite3BeginWriteOperation(pParse, 0, iDb);
005385          sqlite3RefillIndex(pParse, pIndex, -1);
005386        }
005387      }
005388    }
005389  }
005390  #endif
005391  
005392  /*
005393  ** Recompute all indices of all tables in all databases where the
005394  ** indices use the collating sequence pColl.  If pColl==0 then recompute
005395  ** all indices everywhere.
005396  */
005397  #ifndef SQLITE_OMIT_REINDEX
005398  static void reindexDatabases(Parse *pParse, char const *zColl){
005399    Db *pDb;                    /* A single database */
005400    int iDb;                    /* The database index number */
005401    sqlite3 *db = pParse->db;   /* The database connection */
005402    HashElem *k;                /* For looping over tables in pDb */
005403    Table *pTab;                /* A table in the database */
005404  
005405    assert( sqlite3BtreeHoldsAllMutexes(db) );  /* Needed for schema access */
005406    for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){
005407      assert( pDb!=0 );
005408      for(k=sqliteHashFirst(&pDb->pSchema->tblHash);  k; k=sqliteHashNext(k)){
005409        pTab = (Table*)sqliteHashData(k);
005410        reindexTable(pParse, pTab, zColl);
005411      }
005412    }
005413  }
005414  #endif
005415  
005416  /*
005417  ** Generate code for the REINDEX command.
005418  **
005419  **        REINDEX                            -- 1
005420  **        REINDEX  <collation>               -- 2
005421  **        REINDEX  ?<database>.?<tablename>  -- 3
005422  **        REINDEX  ?<database>.?<indexname>  -- 4
005423  **
005424  ** Form 1 causes all indices in all attached databases to be rebuilt.
005425  ** Form 2 rebuilds all indices in all databases that use the named
005426  ** collating function.  Forms 3 and 4 rebuild the named index or all
005427  ** indices associated with the named table.
005428  */
005429  #ifndef SQLITE_OMIT_REINDEX
005430  void sqlite3Reindex(Parse *pParse, Token *pName1, Token *pName2){
005431    CollSeq *pColl;             /* Collating sequence to be reindexed, or NULL */
005432    char *z;                    /* Name of a table or index */
005433    const char *zDb;            /* Name of the database */
005434    Table *pTab;                /* A table in the database */
005435    Index *pIndex;              /* An index associated with pTab */
005436    int iDb;                    /* The database index number */
005437    sqlite3 *db = pParse->db;   /* The database connection */
005438    Token *pObjName;            /* Name of the table or index to be reindexed */
005439  
005440    /* Read the database schema. If an error occurs, leave an error message
005441    ** and code in pParse and return NULL. */
005442    if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
005443      return;
005444    }
005445  
005446    if( pName1==0 ){
005447      reindexDatabases(pParse, 0);
005448      return;
005449    }else if( NEVER(pName2==0) || pName2->z==0 ){
005450      char *zColl;
005451      assert( pName1->z );
005452      zColl = sqlite3NameFromToken(pParse->db, pName1);
005453      if( !zColl ) return;
005454      pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
005455      if( pColl ){
005456        reindexDatabases(pParse, zColl);
005457        sqlite3DbFree(db, zColl);
005458        return;
005459      }
005460      sqlite3DbFree(db, zColl);
005461    }
005462    iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pObjName);
005463    if( iDb<0 ) return;
005464    z = sqlite3NameFromToken(db, pObjName);
005465    if( z==0 ) return;
005466    zDb = db->aDb[iDb].zDbSName;
005467    pTab = sqlite3FindTable(db, z, zDb);
005468    if( pTab ){
005469      reindexTable(pParse, pTab, 0);
005470      sqlite3DbFree(db, z);
005471      return;
005472    }
005473    pIndex = sqlite3FindIndex(db, z, zDb);
005474    sqlite3DbFree(db, z);
005475    if( pIndex ){
005476      sqlite3BeginWriteOperation(pParse, 0, iDb);
005477      sqlite3RefillIndex(pParse, pIndex, -1);
005478      return;
005479    }
005480    sqlite3ErrorMsg(pParse, "unable to identify the object to be reindexed");
005481  }
005482  #endif
005483  
005484  /*
005485  ** Return a KeyInfo structure that is appropriate for the given Index.
005486  **
005487  ** The caller should invoke sqlite3KeyInfoUnref() on the returned object
005488  ** when it has finished using it.
005489  */
005490  KeyInfo *sqlite3KeyInfoOfIndex(Parse *pParse, Index *pIdx){
005491    int i;
005492    int nCol = pIdx->nColumn;
005493    int nKey = pIdx->nKeyCol;
005494    KeyInfo *pKey;
005495    if( pParse->nErr ) return 0;
005496    if( pIdx->uniqNotNull ){
005497      pKey = sqlite3KeyInfoAlloc(pParse->db, nKey, nCol-nKey);
005498    }else{
005499      pKey = sqlite3KeyInfoAlloc(pParse->db, nCol, 0);
005500    }
005501    if( pKey ){
005502      assert( sqlite3KeyInfoIsWriteable(pKey) );
005503      for(i=0; i<nCol; i++){
005504        const char *zColl = pIdx->azColl[i];
005505        pKey->aColl[i] = zColl==sqlite3StrBINARY ? 0 :
005506                          sqlite3LocateCollSeq(pParse, zColl);
005507        pKey->aSortFlags[i] = pIdx->aSortOrder[i];
005508        assert( 0==(pKey->aSortFlags[i] & KEYINFO_ORDER_BIGNULL) );
005509      }
005510      if( pParse->nErr ){
005511        assert( pParse->rc==SQLITE_ERROR_MISSING_COLLSEQ );
005512        if( pIdx->bNoQuery==0 ){
005513          /* Deactivate the index because it contains an unknown collating
005514          ** sequence.  The only way to reactive the index is to reload the
005515          ** schema.  Adding the missing collating sequence later does not
005516          ** reactive the index.  The application had the chance to register
005517          ** the missing index using the collation-needed callback.  For
005518          ** simplicity, SQLite will not give the application a second chance.
005519          */
005520          pIdx->bNoQuery = 1;
005521          pParse->rc = SQLITE_ERROR_RETRY;
005522        }
005523        sqlite3KeyInfoUnref(pKey);
005524        pKey = 0;
005525      }
005526    }
005527    return pKey;
005528  }
005529  
005530  #ifndef SQLITE_OMIT_CTE
005531  /*
005532  ** Create a new CTE object
005533  */
005534  Cte *sqlite3CteNew(
005535    Parse *pParse,          /* Parsing context */
005536    Token *pName,           /* Name of the common-table */
005537    ExprList *pArglist,     /* Optional column name list for the table */
005538    Select *pQuery,         /* Query used to initialize the table */
005539    u8 eM10d                /* The MATERIALIZED flag */
005540  ){
005541    Cte *pNew;
005542    sqlite3 *db = pParse->db;
005543  
005544    pNew = sqlite3DbMallocZero(db, sizeof(*pNew));
005545    assert( pNew!=0 || db->mallocFailed );
005546  
005547    if( db->mallocFailed ){
005548      sqlite3ExprListDelete(db, pArglist);
005549      sqlite3SelectDelete(db, pQuery);
005550    }else{
005551      pNew->pSelect = pQuery;
005552      pNew->pCols = pArglist;
005553      pNew->zName = sqlite3NameFromToken(pParse->db, pName);
005554      pNew->eM10d = eM10d;
005555    }
005556    return pNew;
005557  }
005558  
005559  /*
005560  ** Clear information from a Cte object, but do not deallocate storage
005561  ** for the object itself.
005562  */
005563  static void cteClear(sqlite3 *db, Cte *pCte){
005564    assert( pCte!=0 );
005565    sqlite3ExprListDelete(db, pCte->pCols);
005566    sqlite3SelectDelete(db, pCte->pSelect);
005567    sqlite3DbFree(db, pCte->zName);
005568  }
005569  
005570  /*
005571  ** Free the contents of the CTE object passed as the second argument.
005572  */
005573  void sqlite3CteDelete(sqlite3 *db, Cte *pCte){
005574    assert( pCte!=0 );
005575    cteClear(db, pCte);
005576    sqlite3DbFree(db, pCte);
005577  }
005578  
005579  /* 
005580  ** This routine is invoked once per CTE by the parser while parsing a 
005581  ** WITH clause.  The CTE described by teh third argument is added to
005582  ** the WITH clause of the second argument.  If the second argument is
005583  ** NULL, then a new WITH argument is created.
005584  */
005585  With *sqlite3WithAdd(
005586    Parse *pParse,          /* Parsing context */
005587    With *pWith,            /* Existing WITH clause, or NULL */
005588    Cte *pCte               /* CTE to add to the WITH clause */
005589  ){
005590    sqlite3 *db = pParse->db;
005591    With *pNew;
005592    char *zName;
005593  
005594    if( pCte==0 ){
005595      return pWith;
005596    }
005597  
005598    /* Check that the CTE name is unique within this WITH clause. If
005599    ** not, store an error in the Parse structure. */
005600    zName = pCte->zName;
005601    if( zName && pWith ){
005602      int i;
005603      for(i=0; i<pWith->nCte; i++){
005604        if( sqlite3StrICmp(zName, pWith->a[i].zName)==0 ){
005605          sqlite3ErrorMsg(pParse, "duplicate WITH table name: %s", zName);
005606        }
005607      }
005608    }
005609  
005610    if( pWith ){
005611      sqlite3_int64 nByte = sizeof(*pWith) + (sizeof(pWith->a[1]) * pWith->nCte);
005612      pNew = sqlite3DbRealloc(db, pWith, nByte);
005613    }else{
005614      pNew = sqlite3DbMallocZero(db, sizeof(*pWith));
005615    }
005616    assert( (pNew!=0 && zName!=0) || db->mallocFailed );
005617  
005618    if( db->mallocFailed ){
005619      sqlite3CteDelete(db, pCte);
005620      pNew = pWith;
005621    }else{
005622      pNew->a[pNew->nCte++] = *pCte;
005623      sqlite3DbFree(db, pCte);
005624    }
005625  
005626    return pNew;
005627  }
005628  
005629  /*
005630  ** Free the contents of the With object passed as the second argument.
005631  */
005632  void sqlite3WithDelete(sqlite3 *db, With *pWith){
005633    if( pWith ){
005634      int i;
005635      for(i=0; i<pWith->nCte; i++){
005636        cteClear(db, &pWith->a[i]);
005637      }
005638      sqlite3DbFree(db, pWith);
005639    }
005640  }
005641  #endif /* !defined(SQLITE_OMIT_CTE) */