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