⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vdbeaux.c

📁 嵌入式数据系统软件!
💻 C
📖 第 1 页 / 共 5 页
字号:
  ** same loop.  So the total number of instructions is an upper bound  ** on the maximum stack depth required.  (Added later:)  The  ** resolveP2Values() call computes a tighter upper bound on the  ** stack size.  **  ** Allocation all the stack space we will ever need.  */  if( p->aStack==0 ){    int nArg;       /* Maximum number of args passed to a user function. */    int nStack;     /* Maximum number of stack entries required */    resolveP2Values(p, &nArg, &nStack);    resizeOpArray(p, p->nOp);    assert( nVar>=0 );    assert( nStack<p->nOp );    if( isExplain ){      nStack = 10;    }    p->aStack = sqlite3DbMallocZero(db,        nStack*sizeof(p->aStack[0])    /* aStack */      + nArg*sizeof(Mem*)              /* apArg */      + nVar*sizeof(Mem)               /* aVar */      + nVar*sizeof(char*)             /* azVar */      + nMem*sizeof(Mem)               /* aMem */      + nCursor*sizeof(Cursor*)        /* apCsr */    );    if( !db->mallocFailed ){      p->aMem = &p->aStack[nStack];      p->nMem = nMem;      p->aVar = &p->aMem[nMem];      p->nVar = nVar;      p->okVar = 0;      p->apArg = (Mem**)&p->aVar[nVar];      p->azVar = (char**)&p->apArg[nArg];      p->apCsr = (Cursor**)&p->azVar[nVar];      p->nCursor = nCursor;      for(n=0; n<nVar; n++){        p->aVar[n].flags = MEM_Null;        p->aVar[n].db = db;      }      for(n=0; n<nStack; n++){        p->aStack[n].db = db;      }    }  }  for(n=0; n<p->nMem; n++){    p->aMem[n].flags = MEM_Null;    p->aMem[n].db = db;  }  p->pTos = &p->aStack[-1];  p->pc = -1;  p->rc = SQLITE_OK;  p->uniqueCnt = 0;  p->returnDepth = 0;  p->errorAction = OE_Abort;  p->popStack =  0;  p->explain |= isExplain;  p->magic = VDBE_MAGIC_RUN;  p->nChange = 0;  p->cacheCtr = 1;  p->minWriteFileFormat = 255;  p->openedStatement = 0;#ifdef VDBE_PROFILE  {    int i;    for(i=0; i<p->nOp; i++){      p->aOp[i].cnt = 0;      p->aOp[i].cycles = 0;    }  }#endif}/*** Close a VDBE cursor and release all the resources that cursor happens** to hold.*/void sqlite3VdbeFreeCursor(Vdbe *p, Cursor *pCx){  if( pCx==0 ){    return;  }  if( pCx->pCursor ){    sqlite3BtreeCloseCursor(pCx->pCursor);  }  if( pCx->pBt ){    sqlite3BtreeClose(pCx->pBt);  }#ifndef SQLITE_OMIT_VIRTUALTABLE  if( pCx->pVtabCursor ){    sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor;    const sqlite3_module *pModule = pCx->pModule;    p->inVtabMethod = 1;    sqlite3SafetyOff(p->db);    pModule->xClose(pVtabCursor);    sqlite3SafetyOn(p->db);    p->inVtabMethod = 0;  }#endif  sqlite3_free(pCx->pData);  sqlite3_free(pCx->aType);  sqlite3_free(pCx);}/*** Close all cursors except for VTab cursors that are currently** in use.*/static void closeAllCursorsExceptActiveVtabs(Vdbe *p){  int i;  if( p->apCsr==0 ) return;  for(i=0; i<p->nCursor; i++){    Cursor *pC = p->apCsr[i];    if( pC && (!p->inVtabMethod || !pC->pVtabCursor) ){      sqlite3VdbeFreeCursor(p, pC);      p->apCsr[i] = 0;    }  }}/*** Clean up the VM after execution.**** This routine will automatically close any cursors, lists, and/or** sorters that were left open.  It also deletes the values of** variables in the aVar[] array.*/static void Cleanup(Vdbe *p){  int i;  if( p->aStack ){    releaseMemArray(p->aStack, 1 + (p->pTos - p->aStack));    p->pTos = &p->aStack[-1];  }  closeAllCursorsExceptActiveVtabs(p);  releaseMemArray(p->aMem, p->nMem);  sqlite3VdbeFifoClear(&p->sFifo);  if( p->contextStack ){    for(i=0; i<p->contextStackTop; i++){      sqlite3VdbeFifoClear(&p->contextStack[i].sFifo);    }    sqlite3_free(p->contextStack);  }  p->contextStack = 0;  p->contextStackDepth = 0;  p->contextStackTop = 0;  sqlite3_free(p->zErrMsg);  p->zErrMsg = 0;  p->resOnStack = 0;}/*** Set the number of result columns that will be returned by this SQL** statement. This is now set at compile time, rather than during** execution of the vdbe program so that sqlite3_column_count() can** be called on an SQL statement before sqlite3_step().*/void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){  Mem *pColName;  int n;  releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);  sqlite3_free(p->aColName);  n = nResColumn*COLNAME_N;  p->nResColumn = nResColumn;  p->aColName = pColName = (Mem*)sqlite3DbMallocZero(p->db, sizeof(Mem)*n );  if( p->aColName==0 ) return;  while( n-- > 0 ){    pColName->flags = MEM_Null;    pColName->db = p->db;    pColName++;  }}/*** Set the name of the idx'th column to be returned by the SQL statement.** zName must be a pointer to a nul terminated string.**** This call must be made after a call to sqlite3VdbeSetNumCols().**** If N==P3_STATIC  it means that zName is a pointer to a constant static** string and we can just copy the pointer. If it is P3_DYNAMIC, then ** the string is freed using sqlite3_free() when the vdbe is finished with** it. Otherwise, N bytes of zName are copied.*/int sqlite3VdbeSetColName(Vdbe *p, int idx, int var, const char *zName, int N){  int rc;  Mem *pColName;  assert( idx<p->nResColumn );  assert( var<COLNAME_N );  if( p->db->mallocFailed ) return SQLITE_NOMEM;  assert( p->aColName!=0 );  pColName = &(p->aColName[idx+var*p->nResColumn]);  if( N==P3_DYNAMIC || N==P3_STATIC ){    rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, SQLITE_STATIC);  }else{    rc = sqlite3VdbeMemSetStr(pColName, zName, N, SQLITE_UTF8,SQLITE_TRANSIENT);  }  if( rc==SQLITE_OK && N==P3_DYNAMIC ){    pColName->flags = (pColName->flags&(~MEM_Static))|MEM_Dyn;    pColName->xDel = 0;  }  return rc;}/*** A read or write transaction may or may not be active on database handle** db. If a transaction is active, commit it. If there is a** write-transaction spanning more than one database file, this routine** takes care of the master journal trickery.*/static int vdbeCommit(sqlite3 *db){  int i;  int nTrans = 0;  /* Number of databases with an active write-transaction */  int rc = SQLITE_OK;  int needXcommit = 0;  /* Before doing anything else, call the xSync() callback for any  ** virtual module tables written in this transaction. This has to  ** be done before determining whether a master journal file is   ** required, as an xSync() callback may add an attached database  ** to the transaction.  */  rc = sqlite3VtabSync(db, rc);  if( rc!=SQLITE_OK ){    return rc;  }  /* This loop determines (a) if the commit hook should be invoked and  ** (b) how many database files have open write transactions, not   ** including the temp database. (b) is important because if more than   ** one database file has an open write transaction, a master journal  ** file is required for an atomic commit.  */   for(i=0; i<db->nDb; i++){     Btree *pBt = db->aDb[i].pBt;    if( sqlite3BtreeIsInTrans(pBt) ){      needXcommit = 1;      if( i!=1 ) nTrans++;    }  }  /* If there are any write-transactions at all, invoke the commit hook */  if( needXcommit && db->xCommitCallback ){    sqlite3SafetyOff(db);    rc = db->xCommitCallback(db->pCommitArg);    sqlite3SafetyOn(db);    if( rc ){      return SQLITE_CONSTRAINT;    }  }  /* The simple case - no more than one database file (not counting the  ** TEMP database) has a transaction active.   There is no need for the  ** master-journal.  **  ** If the return value of sqlite3BtreeGetFilename() is a zero length  ** string, it means the main database is :memory:.  In that case we do  ** not support atomic multi-file commits, so use the simple case then  ** too.  */  if( 0==strlen(sqlite3BtreeGetFilename(db->aDb[0].pBt)) || nTrans<=1 ){    for(i=0; rc==SQLITE_OK && i<db->nDb; i++){       Btree *pBt = db->aDb[i].pBt;      if( pBt ){        rc = sqlite3BtreeCommitPhaseOne(pBt, 0);      }    }    /* Do the commit only if all databases successfully complete phase 1.     ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an    ** IO error while deleting or truncating a journal file. It is unlikely,    ** but could happen. In this case abandon processing and return the error.    */    for(i=0; rc==SQLITE_OK && i<db->nDb; i++){      Btree *pBt = db->aDb[i].pBt;      if( pBt ){        rc = sqlite3BtreeCommitPhaseTwo(pBt);      }    }    if( rc==SQLITE_OK ){      sqlite3VtabCommit(db);    }  }  /* The complex case - There is a multi-file write-transaction active.  ** This requires a master journal file to ensure the transaction is  ** committed atomicly.  */#ifndef SQLITE_OMIT_DISKIO  else{    sqlite3_vfs *pVfs = db->pVfs;    int needSync = 0;    char *zMaster = 0;   /* File-name for the master journal */    char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);    sqlite3_file *pMaster = 0;    i64 offset = 0;    /* Select a master journal file name */    do {      u32 random;      sqlite3_free(zMaster);      sqlite3Randomness(sizeof(random), &random);      zMaster = sqlite3MPrintf(db, "%s-mj%08X", zMainFile, random&0x7fffffff);      if( !zMaster ){        return SQLITE_NOMEM;      }    }while( sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS) );    /* Open the master journal. */    rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster,         SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|        SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0    );    if( rc!=SQLITE_OK ){      sqlite3_free(zMaster);      return rc;    }     /* Write the name of each database file in the transaction into the new    ** master journal file. If an error occurs at this point close    ** and delete the master journal file. All the individual journal files    ** still have 'null' as the master journal pointer, so they will roll    ** back independently if a failure occurs.    */    for(i=0; i<db->nDb; i++){      Btree *pBt = db->aDb[i].pBt;      if( i==1 ) continue;   /* Ignore the TEMP database */      if( sqlite3BtreeIsInTrans(pBt) ){        char const *zFile = sqlite3BtreeGetJournalname(pBt);        if( zFile[0]==0 ) continue;  /* Ignore :memory: databases */        if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){          needSync = 1;        }        rc = sqlite3OsWrite(pMaster, zFile, strlen(zFile)+1, offset);        offset += strlen(zFile)+1;        if( rc!=SQLITE_OK ){          sqlite3OsCloseFree(pMaster);          sqlite3OsDelete(pVfs, zMaster, 0);          sqlite3_free(zMaster);          return rc;        }      }    }    /* Sync the master journal file. If the IOCAP_SEQUENTIAL device    ** flag is set this is not required.    */    zMainFile = sqlite3BtreeGetDirname(db->aDb[0].pBt);    if( (needSync      && (0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL))     && (rc=sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL))!=SQLITE_OK) ){      sqlite3OsCloseFree(pMaster);      sqlite3OsDelete(pVfs, zMaster, 0);      sqlite3_free(zMaster);      return rc;    }    /* Sync all the db files involved in the transaction. The same call    ** sets the master journal pointer in each individual journal. If    ** an error occurs here, do not delete the master journal file.    **    ** If the error occurs during the first call to    ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the    ** master journal file will be orphaned. But we cannot delete it,    ** in case the master journal file name was written into the journal    ** file before the failure occured.    */    for(i=0; rc==SQLITE_OK && i<db->nDb; i++){       Btree *pBt = db->aDb[i].pBt;      if( pBt ){        rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster);      }    }    sqlite3OsCloseFree(pMaster);    if( rc!=SQLITE_OK ){      sqlite3_free(zMaster);      return rc;    }    /* Delete the master journal file. This commits the transaction. After    ** doing this the directory is synced again before any individual    ** transaction files are deleted.    */    rc = sqlite3OsDelete(pVfs, zMaster, 1);    sqlite3_free(zMaster);    zMaster = 0;    if( rc ){      return rc;    }    /* All files and directories have already been synced, so the following    ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and    ** deleting or truncating journals. If something goes wrong while    ** this is happening we don't really care. The integrity of the    ** transaction is already guaranteed, but some stray 'cold' journals    ** may be lying around. Returning an error code won't help matters.    */    disable_simulated_io_errors();    for(i=0; i<db->nDb; i++){       Btree *pBt = db->aDb[i].pBt;      if( pBt ){        sqlite3BtreeCommitPhaseTwo(pBt);      }    }    enable_simulated_io_errors();    sqlite3VtabCommit(db);  }#endif  return rc;}/* ** This routine checks that the sqlite3.activeVdbeCnt count variable** matches the number of vdbe's in the list sqlite3.pVdbe that are** currently active. An assertion fails if the two counts do not match.** This is an internal self-check only - it is not an essential processing** step.**** This is a no-op if NDEBUG is defined.*/#ifndef NDEBUGstatic void checkActiveVdbeCnt(sqlite3 *db){  Vdbe *p;  int cnt = 0;  p = db->pVdbe;  while( p ){    if( p->magic==VDBE_MAGIC_RUN && p->pc>=0 ){      cnt++;    }    p = p->pNext;  }  assert( cnt==db->activeVdbeCnt );}#else#define checkActiveVdbeCnt(x)#endif/*** For every Btree that in database connection db which 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -