zName = sqliteStrNDup(pName->z, pName->n);
zName = sqliteTableNameFromToken(pName);
void sqliteCreateIndex(  Parse *pParse,   /* All information about this parse */  Token *pName,    /* Name of the index.  May be NULL */  SrcList *pTable, /* Name of the table to index.  Use pParse->pNewTable if 0 */  IdList *pList,   /* A list of columns to be indexed */  int onError,     /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */  Token *pStart,   /* The CREATE token that begins a CREATE TABLE statement */  Token *pEnd      /* The ")" that closes the CREATE INDEX statement */){  Table *pTab;     /* Table to be indexed */  Index *pIndex;   /* The index to be created */  char *zName = 0;  int i, j;  Token nullId;    /* Fake token for an empty ID list */  DbFixer sFix;    /* For assigning database names to pTable */  int isTemp;      /* True for a temporary index */  sqlite *db = pParse->db;  if( pParse->nErr || sqlite_malloc_failed ) goto exit_create_index;  if( db->init.busy      && sqliteFixInit(&sFix, pParse, db->init.iDb, "index", pName)     && sqliteFixSrcList(&sFix, pTable)  ){    goto exit_create_index;  }  /*  ** Find the table that is to be indexed.  Return early if not found.  */  if( pTable!=0 ){    assert( pName!=0 );    assert( pTable->nSrc==1 );    pTab =  sqliteSrcListLookup(pParse, pTable);  }else{    assert( pName==0 );    pTab =  pParse->pNewTable;  }  if( pTab==0 || pParse->nErr ) goto exit_create_index;  if( pTab->readOnly ){    sqliteErrorMsg(pParse, "table %s may not be indexed", pTab->zName);    goto exit_create_index;  }  if( pTab->iDb>=2 && db->init.busy==0 ){    sqliteErrorMsg(pParse, "table %s may not have indices added", pTab->zName);    goto exit_create_index;  }  if( pTab->pSelect ){    sqliteErrorMsg(pParse, "views may not be indexed");    goto exit_create_index;  }  isTemp = pTab->iDb==1;  /*  ** Find the name of the index.  Make sure there is not already another  ** index or table with the same name.    **  ** Exception:  If we are reading the names of permanent indices from the  ** sqlite_master table (because some other process changed the schema) and  ** one of the index names collides with the name of a temporary table or  ** index, then we will continue to process this index.  **  ** If pName==0 it means that we are  ** dealing with a primary key or UNIQUE constraint.  We have to invent our  ** own name.  */  if( pName && !db->init.busy ){    Index *pISameName;    /* Another index with the same name */    Table *pTSameName;    /* A table with same name as the index */    zName = sqliteStrNDup(pName->z, pName->n);    if( zName==0 ) goto exit_create_index;    if( (pISameName = sqliteFindIndex(db, zName, 0))!=0 ){      sqliteErrorMsg(pParse, "index %s already exists", zName);      goto exit_create_index;    }    if( (pTSameName = sqliteFindTable(db, zName, 0))!=0 ){      sqliteErrorMsg(pParse, "there is already a table named %s", zName);      goto exit_create_index;    }  }else if( pName==0 ){    char zBuf[30];    int n;    Index *pLoop;    for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}    sprintf(zBuf,"%d)",n);    zName = 0;    sqliteSetString(&zName, "(", pTab->zName, " autoindex ", zBuf, (char*)0);    if( zName==0 ) goto exit_create_index;  }else{    zName = sqliteStrNDup(pName->z, pName->n);  }  /* Check for authorization to create an index.  */#ifndef SQLITE_OMIT_AUTHORIZATION  {    const char *zDb = db->aDb[pTab->iDb].zName;    assert( pTab->iDb==db->init.iDb || isTemp );    if( sqliteAuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){      goto exit_create_index;    }    i = SQLITE_CREATE_INDEX;    if( isTemp ) i = SQLITE_CREATE_TEMP_INDEX;    if( sqliteAuthCheck(pParse, i, zName, pTab->zName, zDb) ){      goto exit_create_index;    }  }#endif  /* If pList==0, it means this routine was called to make a primary  ** key out of the last column added to the table under construction.  ** So create a fake list to simulate this.  */  if( pList==0 ){    nullId.z = pTab->aCol[pTab->nCol-1].zName;    nullId.n = strlen(nullId.z);    pList = sqliteIdListAppend(0, &nullId);    if( pList==0 ) goto exit_create_index;  }  /*   ** Allocate the index structure.   */  pIndex = sqliteMalloc( sizeof(Index) + strlen(zName) + 1 +                        sizeof(int)*pList->nId );  if( pIndex==0 ) goto exit_create_index;  pIndex->aiColumn = (int*)&pIndex[1];  pIndex->zName = (char*)&pIndex->aiColumn[pList->nId];  strcpy(pIndex->zName, zName);  pIndex->pTable = pTab;  pIndex->nColumn = pList->nId;  pIndex->onError = onError;  pIndex->autoIndex = pName==0;  pIndex->iDb = isTemp ? 1 : db->init.iDb;  /* Scan the names of the columns of the table to be indexed and  ** load the column indices into the Index structure.  Report an error  ** if any column is not found.  */  for(i=0; i<pList->nId; i++){    for(j=0; j<pTab->nCol; j++){      if( sqliteStrICmp(pList->a[i].zName, pTab->aCol[j].zName)==0 ) break;    }    if( j>=pTab->nCol ){      sqliteErrorMsg(pParse, "table %s has no column named %s",        pTab->zName, pList->a[i].zName);      sqliteFree(pIndex);      goto exit_create_index;    }    pIndex->aiColumn[i] = j;  }  /* Link the new Index structure to its table and to the other  ** in-memory database structures.   */  if( !pParse->explain ){    Index *p;    p = sqliteHashInsert(&db->aDb[pIndex->iDb].idxHash,                          pIndex->zName, strlen(pIndex->zName)+1, pIndex);    if( p ){      assert( p==pIndex );  /* Malloc must have failed */      sqliteFree(pIndex);      goto exit_create_index;    }    db->flags |= SQLITE_InternChanges;  }  /* When adding an index to the list of indices for a table, make  ** sure all indices labeled OE_Replace come after all those labeled  ** OE_Ignore.  This is necessary for the correct operation of UPDATE  ** and INSERT.  */  if( onError!=OE_Replace || pTab->pIndex==0       || pTab->pIndex->onError==OE_Replace){    pIndex->pNext = pTab->pIndex;    pTab->pIndex = pIndex;  }else{    Index *pOther = pTab->pIndex;    while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){      pOther = pOther->pNext;    }    pIndex->pNext = pOther->pNext;    pOther->pNext = pIndex;  }  /* If the db->init.busy is 1 it means we are reading the SQL off the  ** "sqlite_master" table on the disk.  So do not write to the disk  ** again.  Extract the table number from the db->init.newTnum field.  */  if( db->init.busy && pTable!=0 ){    pIndex->tnum = db->init.newTnum;  }  /* If the db->init.busy is 0 then create the index on disk.  This  ** involves writing the index into the master table and filling in the  ** index with the current table contents.  **  ** The db->init.busy is 0 when the user first enters a CREATE INDEX   ** command.  db->init.busy is 1 when a database is opened and   ** CREATE INDEX statements are read out of the master table.  In  ** the latter case the index already exists on disk, which is why  ** we don't want to recreate it.  **  ** If pTable==0 it means this index is generated as a primary key  ** or UNIQUE constraint of a CREATE TABLE statement.  Since the table  ** has just been created, it contains no data and the index initialization  ** step can be skipped.  */  else if( db->init.busy==0 ){    int n;    Vdbe *v;    int lbl1, lbl2;    int i;    int addr;    v = sqliteGetVdbe(pParse);    if( v==0 ) goto exit_create_index;    if( pTable!=0 ){      sqliteBeginWriteOperation(pParse, 0, isTemp);      sqliteOpenMasterTable(v, isTemp);    }    sqliteVdbeAddOp(v, OP_NewRecno, 0, 0);    sqliteVdbeOp3(v, OP_String, 0, 0, "index", P3_STATIC);    sqliteVdbeOp3(v, OP_String, 0, 0, pIndex->zName, 0);    sqliteVdbeOp3(v, OP_String, 0, 0, pTab->zName, 0);    sqliteVdbeOp3(v, OP_CreateIndex, 0, isTemp,(char*)&pIndex->tnum,P3_POINTER);    pIndex->tnum = 0;    if( pTable ){      sqliteVdbeCode(v,          OP_Dup,       0,      0,          OP_Integer,   isTemp, 0,          OP_OpenWrite, 1,      0,      0);    }    addr = sqliteVdbeAddOp(v, OP_String, 0, 0);    if( pStart && pEnd ){      n = Addr(pEnd->z) - Addr(pStart->z) + 1;      sqliteVdbeChangeP3(v, addr, pStart->z, n);    }    sqliteVdbeAddOp(v, OP_MakeRecord, 5, 0);    sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);    if( pTable ){      sqliteVdbeAddOp(v, OP_Integer, pTab->iDb, 0);      sqliteVdbeOp3(v, OP_OpenRead, 2, pTab->tnum, pTab->zName, 0);      lbl2 = sqliteVdbeMakeLabel(v);      sqliteVdbeAddOp(v, OP_Rewind, 2, lbl2);      lbl1 = sqliteVdbeAddOp(v, OP_Recno, 2, 0);      for(i=0; i<pIndex->nColumn; i++){        int iCol = pIndex->aiColumn[i];        if( pTab->iPKey==iCol ){          sqliteVdbeAddOp(v, OP_Dup, i, 0);        }else{          sqliteVdbeAddOp(v, OP_Column, 2, iCol);        }      }      sqliteVdbeAddOp(v, OP_MakeIdxKey, pIndex->nColumn, 0);      if( db->file_format>=4 ) sqliteAddIdxKeyType(v, pIndex);      sqliteVdbeOp3(v, OP_IdxPut, 1, pIndex->onError!=OE_None,                      "indexed columns are not unique", P3_STATIC);      sqliteVdbeAddOp(v, OP_Next, 2, lbl1);      sqliteVdbeResolveLabel(v, lbl2);      sqliteVdbeAddOp(v, OP_Close, 2, 0);      sqliteVdbeAddOp(v, OP_Close, 1, 0);    }    if( pTable!=0 ){      if( !isTemp ){        sqliteChangeCookie(db, v);      }      sqliteVdbeAddOp(v, OP_Close, 0, 0);      sqliteEndWriteOperation(pParse);    }  }  /* Clean up before exiting */exit_create_index:  sqliteIdListDelete(pList);  sqliteSrcListDelete(pTable);  sqliteFree(zName);  return;}