📄 vdbe.c
字号:
** and the error message pointer.**** Whenever a row or result data is available, this routine will either** invoke the result callback (if there is one) or return with** SQLITE_ROW.**** If an attempt is made to open a locked database, then this routine** will either invoke the busy callback (if there is one) or it will** return SQLITE_BUSY.**** If an error occurs, an error message is written to memory obtained** from sqliteMalloc() and p->zErrMsg is made to point to that memory.** The error code is stored in p->rc and this routine returns SQLITE_ERROR.**** If the callback ever returns non-zero, then the program exits** immediately. There will be no error message but the p->rc field is** set to SQLITE_ABORT and this routine will return SQLITE_ERROR.**** A memory allocation error causes p->rc to be set to SQLITE_NOMEM and this** routine to return SQLITE_ERROR.**** Other fatal errors return SQLITE_ERROR.**** After this routine has finished, sqlite3VdbeFinalize() should be** used to clean up the mess that was left behind.*/int sqlite3VdbeExec( Vdbe *p /* The VDBE */){ int pc; /* The program counter */ Op *pOp; /* Current operation */ int rc = SQLITE_OK; /* Value to return */ sqlite3 *db = p->db; /* The database */ u8 encoding = ENC(db); /* The database encoding */ Mem *pTos; /* Top entry in the operand stack */#ifdef VDBE_PROFILE unsigned long long start; /* CPU clock count at start of opcode */ int origPc; /* Program counter at start of opcode */#endif#ifndef SQLITE_OMIT_PROGRESS_CALLBACK int nProgressOps = 0; /* Opcodes executed since progress callback. */#endif#ifndef NDEBUG Mem *pStackLimit;#endif if( p->magic!=VDBE_MAGIC_RUN ) return SQLITE_MISUSE; assert( db->magic==SQLITE_MAGIC_BUSY ); pTos = p->pTos; if( p->rc==SQLITE_NOMEM ){ /* This happens if a malloc() inside a call to sqlite3_column_text() or ** sqlite3_column_text16() failed. */ goto no_mem; } assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY ); p->rc = SQLITE_OK; assert( p->explain==0 ); if( p->popStack ){ popStack(&pTos, p->popStack); p->popStack = 0; } p->resOnStack = 0; db->busyHandler.nBusy = 0; CHECK_FOR_INTERRUPT; sqlite3VdbeIOTraceSql(p);#ifdef SQLITE_DEBUG if( (p->db->flags & SQLITE_VdbeListing)!=0 || sqlite3OsFileExists("vdbe_explain") ){ int i; printf("VDBE Program Listing:\n"); sqlite3VdbePrintSql(p); for(i=0; i<p->nOp; i++){ sqlite3VdbePrintOp(stdout, i, &p->aOp[i]); } } if( sqlite3OsFileExists("vdbe_trace") ){ p->trace = stdout; }#endif for(pc=p->pc; rc==SQLITE_OK; pc++){ assert( pc>=0 && pc<p->nOp ); assert( pTos<=&p->aStack[pc] ); if( sqlite3MallocFailed() ) goto no_mem;#ifdef VDBE_PROFILE origPc = pc; start = hwtime();#endif pOp = &p->aOp[pc]; /* Only allow tracing if SQLITE_DEBUG is defined. */#ifdef SQLITE_DEBUG if( p->trace ){ if( pc==0 ){ printf("VDBE Execution Trace:\n"); sqlite3VdbePrintSql(p); } sqlite3VdbePrintOp(p->trace, pc, pOp); } if( p->trace==0 && pc==0 && sqlite3OsFileExists("vdbe_sqltrace") ){ sqlite3VdbePrintSql(p); }#endif /* Check to see if we need to simulate an interrupt. This only happens ** if we have a special test build. */#ifdef SQLITE_TEST if( sqlite3_interrupt_count>0 ){ sqlite3_interrupt_count--; if( sqlite3_interrupt_count==0 ){ sqlite3_interrupt(db); } }#endif#ifndef SQLITE_OMIT_PROGRESS_CALLBACK /* Call the progress callback if it is configured and the required number ** of VDBE ops have been executed (either since this invocation of ** sqlite3VdbeExec() or since last time the progress callback was called). ** If the progress callback returns non-zero, exit the virtual machine with ** a return code SQLITE_ABORT. */ if( db->xProgress ){ if( db->nProgressOps==nProgressOps ){ int prc; if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; prc =db->xProgress(db->pProgressArg); if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; if( prc!=0 ){ rc = SQLITE_INTERRUPT; goto vdbe_halt; } nProgressOps = 0; } nProgressOps++; }#endif#ifndef NDEBUG /* This is to check that the return value of static function ** opcodeNoPush() (see vdbeaux.c) returns values that match the ** implementation of the virtual machine in this file. If ** opcodeNoPush() returns non-zero, then the stack is guarenteed ** not to grow when the opcode is executed. If it returns zero, then ** the stack may grow by at most 1. ** ** The global wrapper function sqlite3VdbeOpcodeUsesStack() is not ** available if NDEBUG is defined at build time. */ pStackLimit = pTos; if( !sqlite3VdbeOpcodeNoPush(pOp->opcode) ){ pStackLimit++; }#endif switch( pOp->opcode ){/******************************************************************************* What follows is a massive switch statement where each case implements a** separate instruction in the virtual machine. If we follow the usual** indentation conventions, each case should be indented by 6 spaces. But** that is a lot of wasted space on the left margin. So the code within** the switch statement will break with convention and be flush-left. Another** big comment (similar to this one) will mark the point in the code where** we transition back to normal indentation.**** The formatting of each case is important. The makefile for SQLite** generates two C files "opcodes.h" and "opcodes.c" by scanning this** file looking for lines that begin with "case OP_". The opcodes.h files** will be filled with #defines that give unique integer values to each** opcode and the opcodes.c file is filled with an array of strings where** each string is the symbolic name for the corresponding opcode. If the** case statement is followed by a comment of the form "/# same as ... #/"** that comment is used to determine the particular value of the opcode.**** If a comment on the same line as the "case OP_" construction contains** the word "no-push", then the opcode is guarenteed not to grow the ** vdbe stack when it is executed. See function opcode() in** vdbeaux.c for details.**** Documentation about VDBE opcodes is generated by scanning this file** for lines of that contain "Opcode:". That line and all subsequent** comment lines are used in the generation of the opcode.html documentation** file.**** SUMMARY:**** Formatting is important to scripts that scan this file.** Do not deviate from the formatting style currently in use.*******************************************************************************//* Opcode: Goto * P2 ***** An unconditional jump to address P2.** The next instruction executed will be ** the one at index P2 from the beginning of** the program.*/case OP_Goto: { /* no-push */ CHECK_FOR_INTERRUPT; pc = pOp->p2 - 1; break;}/* Opcode: Gosub * P2 ***** Push the current address plus 1 onto the return address stack** and then jump to address P2.**** The return address stack is of limited depth. If too many** OP_Gosub operations occur without intervening OP_Returns, then** the return address stack will fill up and processing will abort** with a fatal error.*/case OP_Gosub: { /* no-push */ assert( p->returnDepth<sizeof(p->returnStack)/sizeof(p->returnStack[0]) ); p->returnStack[p->returnDepth++] = pc+1; pc = pOp->p2 - 1; break;}/* Opcode: Return * * ***** Jump immediately to the next instruction after the last unreturned** OP_Gosub. If an OP_Return has occurred for all OP_Gosubs, then** processing aborts with a fatal error.*/case OP_Return: { /* no-push */ assert( p->returnDepth>0 ); p->returnDepth--; pc = p->returnStack[p->returnDepth] - 1; break;}/* Opcode: Halt P1 P2 P3**** Exit immediately. All open cursors, Fifos, etc are closed** automatically.**** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0).** For errors, it can be some other value. If P1!=0 then P2 will determine** whether or not to rollback the current transaction. Do not rollback** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort,** then back out all changes that have occurred during this execution of the** VDBE, but do not rollback the transaction. **** If P3 is not null then it is an error message string.**** There is an implied "Halt 0 0 0" instruction inserted at the very end of** every program. So a jump past the last instruction of the program** is the same as executing Halt.*/case OP_Halt: { /* no-push */ p->pTos = pTos; p->rc = pOp->p1; p->pc = pc; p->errorAction = pOp->p2; if( pOp->p3 ){ sqlite3SetString(&p->zErrMsg, pOp->p3, (char*)0); } rc = sqlite3VdbeHalt(p); assert( rc==SQLITE_BUSY || rc==SQLITE_OK ); if( rc==SQLITE_BUSY ){ p->rc = SQLITE_BUSY; return SQLITE_BUSY; } return p->rc ? SQLITE_ERROR : SQLITE_DONE;}/* Opcode: Integer P1 * ***** The 32-bit integer value P1 is pushed onto the stack.*/case OP_Integer: { pTos++; pTos->flags = MEM_Int; pTos->u.i = pOp->p1; break;}/* Opcode: Int64 * * P3**** P3 is a string representation of an integer. Convert that integer** to a 64-bit value and push it onto the stack.*/case OP_Int64: { pTos++; assert( pOp->p3!=0 ); pTos->flags = MEM_Str|MEM_Static|MEM_Term; pTos->z = pOp->p3; pTos->n = strlen(pTos->z); pTos->enc = SQLITE_UTF8; pTos->u.i = sqlite3VdbeIntValue(pTos); pTos->flags |= MEM_Int; break;}/* Opcode: Real * * P3**** The string value P3 is converted to a real and pushed on to the stack.*/case OP_Real: { /* same as TK_FLOAT, */ pTos++; pTos->flags = MEM_Str|MEM_Static|MEM_Term; pTos->z = pOp->p3; pTos->n = strlen(pTos->z); pTos->enc = SQLITE_UTF8; pTos->r = sqlite3VdbeRealValue(pTos); pTos->flags |= MEM_Real; sqlite3VdbeChangeEncoding(pTos, encoding); break;}/* Opcode: String8 * * P3**** P3 points to a nul terminated UTF-8 string. This opcode is transformed ** into an OP_String before it is executed for the first time.*/case OP_String8: { /* same as TK_STRING */ assert( pOp->p3!=0 ); pOp->opcode = OP_String; pOp->p1 = strlen(pOp->p3); assert( SQLITE_MAX_SQL_LENGTH < SQLITE_MAX_LENGTH ); assert( pOp->p1 < SQLITE_MAX_LENGTH );#ifndef SQLITE_OMIT_UTF16 if( encoding!=SQLITE_UTF8 ){ pTos++; sqlite3VdbeMemSetStr(pTos, pOp->p3, -1, SQLITE_UTF8, SQLITE_STATIC); if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pTos, encoding) ) goto no_mem; if( SQLITE_OK!=sqlite3VdbeMemDynamicify(pTos) ) goto no_mem; pTos->flags &= ~(MEM_Dyn); pTos->flags |= MEM_Static; if( pOp->p3type==P3_DYNAMIC ){ sqliteFree(pOp->p3); } pOp->p3type = P3_DYNAMIC; pOp->p3 = pTos->z; pOp->p1 = pTos->n; assert( pOp->p1 < SQLITE_MAX_LENGTH ); /* Due to SQLITE_MAX_SQL_LENGTH */ break; }#endif /* Otherwise fall through to the next case, OP_String */} /* Opcode: String P1 * P3**** The string value P3 of length P1 (bytes) is pushed onto the stack.*/case OP_String: { assert( pOp->p1 < SQLITE_MAX_LENGTH ); /* Due to SQLITE_MAX_SQL_LENGTH */ pTos++; assert( pOp->p3!=0 ); pTos->flags = MEM_Str|MEM_Static|MEM_Term; pTos->z = pOp->p3; pTos->n = pOp->p1; pTos->enc = encoding; break;}/* Opcode: Null * * ***** Push a NULL onto the stack.*/case OP_Null: { pTos++; pTos->flags = MEM_Null; pTos->n = 0; break;}#ifndef SQLITE_OMIT_BLOB_LITERAL/* Opcode: HexBlob * * P3**** P3 is an UTF-8 SQL hex encoding of a blob. The blob is pushed onto the** vdbe stack.**** The first time this instruction executes, in transforms itself into a** 'Blob' opcode with a binary blob as P3.*/case OP_HexBlob: { /* same as TK_BLOB */ pOp->opcode = OP_Blob; pOp->p1 = strlen(pOp->p3)/2; assert( SQLITE_MAX_SQL_LENGTH < SQLITE_MAX_LENGTH ); assert( pOp->p1 < SQLITE_MAX_LENGTH ); if( pOp->p1 ){ char *zBlob = sqlite3HexToBlob(pOp->p3); if( !zBlob ) goto no_mem; if( pOp->p3type==P3_DYNAMIC ){ sqliteFree(pOp->p3); } pOp->p3 = zBlob; pOp->p3type = P3_DYNAMIC; }else{ if( pOp->p3type==P3_DYNAMIC ){ sqliteFree(pOp->p3); } pOp->p3type = P3_STATIC; pOp->p3 = ""; } /* Fall through to the next case, OP_Blob. */}/* Opcode: Blob P1 * P3**** P3 points to a blob of data P1 bytes long. Push this** value onto the stack. This instruction is not coded directly** by the compiler. Instead, the compiler layer specifies** an OP_HexBlob opcode, with the hex string representation of** the blob as P3. This opcode is transformed to an OP_Blob** the first time it is executed.*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -