Esempio n. 1
0
/*
** Generate code to drop and reload the internal representation of table
** pTab from the database, including triggers and temporary triggers.
** Argument zName is the name of the table in the database schema at
** the time the generated code is executed. This can be different from
** pTab->zName if this function is being called to code part of an 
** "ALTER TABLE RENAME TO" statement.
生成代码删除和重新加载从数据库表pTab的内部表达示,
包括触发器和临时触发器。
参数zName在数据库模式中的表的名称生成的代码执行。
这可以不同于pTab - > zName如果这个函数被调用的代码的一部分“ALTER TABLE RENAME TO”声明。
*/
static void reloadTableSchema(Parse *pParse, Table *pTab, const char *zName){
  Vdbe *v;
  char *zWhere;
  int iDb;                   /* Index of database containing pTab 包含pTab数据库索引*/
#ifndef SQLITE_OMIT_TRIGGER
  Trigger *pTrig;
#endif

  v = sqlite3GetVdbe(pParse);
  if( NEVER(v==0) ) return;
  assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
  iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
  assert( iDb>=0 );

#ifndef SQLITE_OMIT_TRIGGER
  /* Drop any table triggers from the internal schema. 降低任何表触发器内部模式。*/
  for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){
    int iTrigDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
    assert( iTrigDb==iDb || iTrigDb==1 );
    sqlite3VdbeAddOp4(v, OP_DropTrigger, iTrigDb, 0, 0, pTrig->zName, 0);
  }
#endif

  /* Drop the table and index from the internal schema.  从内部模式中降低表和索引*/
  sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);

  /* Reload the table, index and permanent trigger schemas. 重载表、索引和不变的触发器模式*/
  zWhere = sqlite3MPrintf(pParse->db, "tbl_name=%Q", zName);
  if( !zWhere ) return;
  sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere);

#ifndef SQLITE_OMIT_TRIGGER
  /* Now, if the table is not stored in the temp database, reload any temp 
  ** triggers. Don't use IN(...) in case SQLITE_OMIT_SUBQUERY is defined. 
  现在,如果表没有在临时数据库中存储,那么重载任何临时触发器。
  不要用IN(...)在 SQLITE_OMIT_SUBQUERY被默认的情况下。
  */
  if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
    sqlite3VdbeAddParseSchemaOp(v, 1, zWhere);
  }
#endif
}
Esempio n. 2
0
/*
** The parser calls this routine after the CREATE VIRTUAL TABLE statement
** has been completely parsed.
*/
void sqlite3VtabFinishParse(Parse *pParse, Token *pEnd){
  Table *pTab = pParse->pNewTable;  /* The table being constructed */
  sqlite3 *db = pParse->db;         /* The database connection */

  if( pTab==0 ) return;
  addArgumentToVtab(pParse);
  pParse->sArg.z = 0;
  if( pTab->nModuleArg<1 ) return;
  
  /* If the CREATE VIRTUAL TABLE statement is being entered for the
  ** first time (in other words if the virtual table is actually being
  ** created now instead of just being read out of sqlite_master) then
  ** do additional initialization work and store the statement text
  ** in the sqlite_master table.
  */
  if( !db->init.busy ){
    char *zStmt;
    char *zWhere;
    int iDb;
    int iReg;
    Vdbe *v;

    /* Compute the complete text of the CREATE VIRTUAL TABLE statement */
    if( pEnd ){
      pParse->sNameToken.n = (int)(pEnd->z - pParse->sNameToken.z) + pEnd->n;
    }
    zStmt = sqlite3MPrintf(db, "CREATE VIRTUAL TABLE %T", &pParse->sNameToken);

    /* A slot for the record has already been allocated in the 
    ** SQLITE_MASTER table.  We just need to update that slot with all
    ** the information we've collected.  
    **
    ** The VM register number pParse->regRowid holds the rowid of an
    ** entry in the sqlite_master table tht was created for this vtab
    ** by sqlite3StartTable().
    */
    iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
    sqlite3NestedParse(pParse,
      "UPDATE %Q.%s "
         "SET type='table', name=%Q, tbl_name=%Q, rootpage=0, sql=%Q "
       "WHERE rowid=#%d",
      db->aDb[iDb].zDbSName, MASTER_NAME,
      pTab->zName,
      pTab->zName,
      zStmt,
      pParse->regRowid
    );
    sqlite3DbFree(db, zStmt);
    v = sqlite3GetVdbe(pParse);
    sqlite3ChangeCookie(pParse, iDb);

    sqlite3VdbeAddOp0(v, OP_Expire);
    zWhere = sqlite3MPrintf(db, "name='%q' AND type='table'", pTab->zName);
    sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere);

    iReg = ++pParse->nMem;
    sqlite3VdbeLoadString(v, iReg, pTab->zName);
    sqlite3VdbeAddOp2(v, OP_VCreate, iDb, iReg);
  }

  /* If we are rereading the sqlite_master table create the in-memory
  ** record of the table. The xConnect() method is not called until
  ** the first time the virtual table is used in an SQL statement. This
  ** allows a schema that contains virtual tables to be loaded before
  ** the required virtual table implementations are registered.  */
  else {
    Table *pOld;
    Schema *pSchema = pTab->pSchema;
    const char *zName = pTab->zName;
    assert( sqlite3SchemaMutexHeld(db, 0, pSchema) );
    pOld = sqlite3HashInsert(&pSchema->tblHash, zName, pTab);
    if( pOld ){
      sqlite3OomFault(db);
      assert( pTab==pOld );  /* Malloc must have failed inside HashInsert() */
      return;
    }
    pParse->pNewTable = 0;
  }
}
/*
 ** This routine is called after all of the trigger actions have been parsed
 ** in order to complete the process of building the trigger.
 */
SQLITE_PRIVATE void sqlite3FinishTrigger(
                                         Parse *pParse,          /* Parser context */
                                         TriggerStep *pStepList, /* The triggered program */
                                         Token *pAll             /* Token that describes the complete CREATE TRIGGER */
){
    Trigger *pTrig = pParse->pNewTrigger;   /* Trigger being finished */
    char *zName;                            /* Name of trigger */
    sqlite3 *db = pParse->db;               /* The database */
    DbFixer sFix;                           /* Fixer object */
    int iDb;                                /* Database containing the trigger */
    Token nameToken;                        /* Trigger name for error reporting */
    
    pParse->pNewTrigger = 0;
    if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
    zName = pTrig->zName;
    iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
    pTrig->step_list = pStepList;
    while( pStepList ){
        pStepList->pTrig = pTrig;
        pStepList = pStepList->pNext;
    }
    nameToken.z = pTrig->zName;
    nameToken.n = sqlite3Strlen30(nameToken.z);
    sqlite3FixInit(&sFix, pParse, iDb, "trigger", &nameToken);
    if( sqlite3FixTriggerStep(&sFix, pTrig->step_list)
       || sqlite3FixExpr(&sFix, pTrig->pWhen)
       ){
        goto triggerfinish_cleanup;
    }
    
    /* if we are not initializing,
     ** build the sqlite_master entry
     */
    if( !db->init.busy ){
        Vdbe *v;
        char *z;
        
        /* Make an entry in the sqlite_master table */
        v = sqlite3GetVdbe(pParse);
        if( v==0 ) goto triggerfinish_cleanup;
        sqlite3BeginWriteOperation(pParse, 0, iDb);
        z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
        sqlite3NestedParse(pParse,
                           "INSERT INTO %Q.%s VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')",
                           db->aDb[iDb].zName, SCHEMA_TABLE(iDb), zName,
                           pTrig->table, z);
        sqlite3DbFree(db, z);
        sqlite3ChangeCookie(pParse, iDb);
        sqlite3VdbeAddParseSchemaOp(v, iDb,
                                    sqlite3MPrintf(db, "type='trigger' AND name='%q'", zName));
    }
    
    if( db->init.busy ){
        Trigger *pLink = pTrig;
        Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
        assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
        pTrig = sqlite3HashInsert(pHash, zName, sqlite3Strlen30(zName), pTrig);
        if( pTrig ){
            db->mallocFailed = 1;
        }else if( pLink->pSchema==pLink->pTabSchema ){
            Table *pTab;
            int n = sqlite3Strlen30(pLink->table);
            pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table, n);
            assert( pTab!=0 );
            pLink->pNext = pTab->pTrigger;
            pTab->pTrigger = pLink;
        }
    }
    
triggerfinish_cleanup:
    sqlite3DeleteTrigger(db, pTrig);
    assert( !pParse->pNewTrigger );
    sqlite3DeleteTriggerStep(db, pStepList);
}