📄 vdbe.c
字号:
/*** 2001 September 15**** The author disclaims copyright to this source code. In place of** a legal notice, here is a blessing:**** May you do good and not evil.** May you find forgiveness for yourself and forgive others.** May you share freely, never taking more than you give.***************************************************************************** The code in this file implements the Virtual Database Engine (VDBE)**** The SQL parser generates a program which is then executed by** the VDBE to do the work of the SQL statement. VDBE programs are ** similar in form to assembly language. The program consists of** a linear sequence of operations. Each operation has an opcode ** and 3 operands. Operands P1 and P2 are integers. Operand P3 ** is a null-terminated string. The P2 operand must be non-negative.** Opcodes will typically ignore one or more operands. Many opcodes** ignore all three operands.**** Computation results are stored on a stack. Each entry on the** stack is either an integer, a null-terminated string, a floating point** number, or the SQL "NULL" value. An inplicit conversion from one** type to the other occurs as necessary.** ** Most of the code in this file is taken up by the sqliteVdbeExec()** function which does the work of interpreting a VDBE program.** But other routines are also provided to help in building up** a program instruction by instruction.**** Various scripts scan this source file in order to generate HTML** documentation, headers files, or other derived files. The formatting** of the code in this file is, therefore, important. See other comments** in this file for details. If in doubt, do not deviate from existing** commenting and indentation practices when changing or adding code.**** $Id: vdbe.c,v 1.236 2003/08/16 12:37:52 drh Exp $*/#include "sqliteInt.h"#include "os.h"#include <ctype.h>/*** The makefile scans this source file and creates the following** array of string constants which are the names of all VDBE opcodes.** This array is defined in a separate source code file named opcode.c** which is automatically generated by the makefile.*/extern char *sqliteOpcodeNames[];/*** The following global variable is incremented every time a cursor** moves, either by the OP_MoveTo or the OP_Next opcode. The test** procedures use this information to make sure that indices are** working correctly. This variable has no function other than to** help verify the correct operation of the library.*/int sqlite_search_count = 0;/*** SQL is translated into a sequence of instructions to be** executed by a virtual machine. Each instruction is an instance** of the following structure.*/typedef struct VdbeOp Op;/*** Boolean values*/typedef unsigned char Bool;/*** A cursor is a pointer into a single BTree within a database file.** The cursor can seek to a BTree entry with a particular key, or** loop over all entries of the Btree. You can also insert new BTree** entries or retrieve the key or data from the entry that the cursor** is currently pointing to.** ** Every cursor that the virtual machine has open is represented by an** instance of the following structure.**** If the Cursor.isTriggerRow flag is set it means that this cursor is** really a single row that represents the NEW or OLD pseudo-table of** a row trigger. The data for the row is stored in Cursor.pData and** the rowid is in Cursor.iKey.*/struct Cursor { BtCursor *pCursor; /* The cursor structure of the backend */ int lastRecno; /* Last recno from a Next or NextIdx operation */ int nextRowid; /* Next rowid returned by OP_NewRowid */ Bool recnoIsValid; /* True if lastRecno is valid */ Bool keyAsData; /* The OP_Column command works on key instead of data */ Bool atFirst; /* True if pointing to first entry */ Bool useRandomRowid; /* Generate new record numbers semi-randomly */ Bool nullRow; /* True if pointing to a row with no data */ Bool nextRowidValid; /* True if the nextRowid field is valid */ Bool pseudoTable; /* This is a NEW or OLD pseudo-tables of a trigger */ Btree *pBt; /* Separate file holding temporary table */ int nData; /* Number of bytes in pData */ char *pData; /* Data for a NEW or OLD pseudo-table */ int iKey; /* Key for the NEW or OLD pseudo-table row */};typedef struct Cursor Cursor;/*** A sorter builds a list of elements to be sorted. Each element of** the list is an instance of the following structure.*/typedef struct Sorter Sorter;struct Sorter { int nKey; /* Number of bytes in the key */ char *zKey; /* The key by which we will sort */ int nData; /* Number of bytes in the data */ char *pData; /* The data associated with this key */ Sorter *pNext; /* Next in the list */};/* ** Number of buckets used for merge-sort. */#define NSORT 30/*** Number of bytes of string storage space available to each stack** layer without having to malloc. NBFS is short for Number of Bytes** For Strings.*/#define NBFS 32/*** A single level of the stack is an instance of the following** structure. Except, string values are stored on a separate** list of of pointers to character. The reason for storing** strings separately is so that they can be easily passed** to the callback function.*/struct Stack { int i; /* Integer value */ int n; /* Number of characters in string value, including '\0' */ int flags; /* Some combination of STK_Null, STK_Str, STK_Dyn, etc. */ double r; /* Real value */ char z[NBFS]; /* Space for short strings */};typedef struct Stack Stack;/*** Memory cells use the same structure as the stack except that space** for an arbitrary string is added.*/struct Mem { Stack s; /* All values of the memory cell besides string */ char *z; /* String value for this memory cell */};typedef struct Mem Mem;/*** Allowed values for Stack.flags*/#define STK_Null 0x0001 /* Value is NULL */#define STK_Str 0x0002 /* Value is a string */#define STK_Int 0x0004 /* Value is an integer */#define STK_Real 0x0008 /* Value is a real number */#define STK_Dyn 0x0010 /* Need to call sqliteFree() on zStack[] */#define STK_Static 0x0020 /* zStack[] points to a static string */#define STK_Ephem 0x0040 /* zStack[] points to an ephemeral string *//* The following STK_ value appears only in AggElem.aMem.s.flag fields.** It indicates that the corresponding AggElem.aMem.z points to a** aggregate function context that needs to be finalized.*/#define STK_AggCtx 0x0040 /* zStack[] points to an agg function context *//*** The "context" argument for a installable function. A pointer to an** instance of this structure is the first argument to the routines used** implement the SQL functions.**** There is a typedef for this structure in sqlite.h. So all routines,** even the public interface to SQLite, can use a pointer to this structure.** But this file is the only place where the internal details of this** structure are known.**** This structure is defined inside of vdbe.c because it uses substructures** (Stack) which are only defined there.*/struct sqlite_func { FuncDef *pFunc; /* Pointer to function information. MUST BE FIRST */ Stack s; /* Small strings, ints, and double values go here */ char *z; /* Space for holding dynamic string results */ void *pAgg; /* Aggregate context */ u8 isError; /* Set to true for an error */ u8 isStep; /* Current in the step function */ int cnt; /* Number of times that the step function has been called */};/*** An Agg structure describes an Aggregator. Each Agg consists of** zero or more Aggregator elements (AggElem). Each AggElem contains** a key and one or more values. The values are used in processing** aggregate functions in a SELECT. The key is used to implement** the GROUP BY clause of a select.*/typedef struct Agg Agg;typedef struct AggElem AggElem;struct Agg { int nMem; /* Number of values stored in each AggElem */ AggElem *pCurrent; /* The AggElem currently in focus */ HashElem *pSearch; /* The hash element for pCurrent */ Hash hash; /* Hash table of all aggregate elements */ FuncDef **apFunc; /* Information about aggregate functions */};struct AggElem { char *zKey; /* The key to this AggElem */ int nKey; /* Number of bytes in the key, including '\0' at end */ Mem aMem[1]; /* The values for this AggElem */};/*** A Set structure is used for quick testing to see if a value** is part of a small set. Sets are used to implement code like** this:** x.y IN ('hi','hoo','hum')*/typedef struct Set Set;struct Set { Hash hash; /* A set is just a hash table */ HashElem *prev; /* Previously accessed hash elemen */};/*** A Keylist is a bunch of keys into a table. The keylist can** grow without bound. The keylist stores the ROWIDs of database** records that need to be deleted or updated.*/typedef struct Keylist Keylist;struct Keylist { int nKey; /* Number of slots in aKey[] */ int nUsed; /* Next unwritten slot in aKey[] */ int nRead; /* Next unread slot in aKey[] */ Keylist *pNext; /* Next block of keys */ int aKey[1]; /* One or more keys. Extra space allocated as needed */};/*** An instance of the virtual machine. This structure contains the complete** state of the virtual machine.**** The "sqlite_vm" structure pointer that is returned by sqlite_compile()** is really a pointer to an instance of this structure.*/struct Vdbe { sqlite *db; /* The whole database */ Vdbe *pPrev,*pNext; /* Linked list of VDBEs with the same Vdbe.db */ FILE *trace; /* Write an execution trace here, if not NULL */ int nOp; /* Number of instructions in the program */ int nOpAlloc; /* Number of slots allocated for aOp[] */ Op *aOp; /* Space to hold the virtual machine's program */ int nLabel; /* Number of labels used */ int nLabelAlloc; /* Number of slots allocated in aLabel[] */ int *aLabel; /* Space to hold the labels */ int tos; /* Index of top of stack */ Stack *aStack; /* The operand stack, except string values */ char **zStack; /* Text or binary values of the stack */ char **azColName; /* Becomes the 4th parameter to callbacks */ int nCursor; /* Number of slots in aCsr[] */ Cursor *aCsr; /* One element of this array for each open cursor */ Sorter *pSort; /* A linked list of objects to be sorted */ FILE *pFile; /* At most one open file handler */ int nField; /* Number of file fields */ char **azField; /* Data for each file field */ char *zLine; /* A single line from the input file */ int magic; /* Magic number for sanity checking */ int nLineAlloc; /* Number of spaces allocated for zLine */ int nMem; /* Number of memory locations currently allocated */ Mem *aMem; /* The memory locations */ Agg agg; /* Aggregate information */ int nSet; /* Number of sets allocated */ Set *aSet; /* An array of sets */ int nCallback; /* Number of callbacks invoked so far */ Keylist *pList; /* A list of ROWIDs */ int keylistStackDepth; /* The size of the "keylist" stack */ Keylist **keylistStack; /* The stack used by opcodes ListPush & ListPop */ int pc; /* The program counter */ int rc; /* Value to return */ unsigned uniqueCnt; /* Used by OP_MakeRecord when P2!=0 */ int errorAction; /* Recovery action to do in case of an error */ int undoTransOnError; /* If error, either ROLLBACK or COMMIT */ int inTempTrans; /* True if temp database is transactioned */ int returnStack[100]; /* Return address stack for OP_Gosub & OP_Return */ int returnDepth; /* Next unused element in returnStack[] */ int nResColumn; /* Number of columns in one row of the result set */ char **azResColumn; /* Values for one row of result */ int (*xCallback)(void*,int,char**,char**); /* Callback for SELECT results */ void *pCbArg; /* First argument to xCallback() */ int popStack; /* Pop the stack this much on entry to VdbeExec() */ char *zErrMsg; /* Error message written here */ u8 explain; /* True if EXPLAIN present on SQL command */};/*** The following are allowed values for Vdbe.magic*/#define VDBE_MAGIC_INIT 0x26bceaa5 /* Building a VDBE program */#define VDBE_MAGIC_RUN 0xbdf20da3 /* VDBE is ready to execute */#define VDBE_MAGIC_HALT 0x519c2973 /* VDBE has completed execution */#define VDBE_MAGIC_DEAD 0xb606c3c8 /* The VDBE has been deallocated *//*** When debugging the code generator in a symbolic debugger, one can** set the sqlite_vdbe_addop_trace to 1 and all opcodes will be printed** as they are added to the instruction stream.*/#ifndef NDEBUGint sqlite_vdbe_addop_trace = 0;static void vdbePrintOp(FILE*, int, Op*);#endif/*** Create a new virtual database engine.*/Vdbe *sqliteVdbeCreate(sqlite *db){ Vdbe *p; p = sqliteMalloc( sizeof(Vdbe) ); if( p==0 ) return 0; p->db = db; if( db->pVdbe ){ db->pVdbe->pPrev = p; } p->pNext = db->pVdbe; p->pPrev = 0; db->pVdbe = p; p->magic = VDBE_MAGIC_INIT; return p;}/*** Turn tracing on or off*/void sqliteVdbeTrace(Vdbe *p, FILE *trace){ p->trace = trace;}/*** Add a new instruction to the list of instructions current in the** VDBE. Return the address of the new instruction.**** Parameters:**** p Pointer to the VDBE**** op The opcode for this instruction**** p1, p2 First two of the three possible operands.**** Use the sqliteVdbeResolveLabel() function to fix an address and** the sqliteVdbeChangeP3() function to change the value of the P3** operand.*/int sqliteVdbeAddOp(Vdbe *p, int op, int p1, int p2){ int i; i = p->nOp; p->nOp++; assert( p->magic==VDBE_MAGIC_INIT ); if( i>=p->nOpAlloc ){ int oldSize = p->nOpAlloc; Op *aNew; p->nOpAlloc = p->nOpAlloc*2 + 100; aNew = sqliteRealloc(p->aOp, p->nOpAlloc*sizeof(Op)); if( aNew==0 ){ p->nOpAlloc = oldSize; return 0; } p->aOp = aNew; memset(&p->aOp[oldSize], 0, (p->nOpAlloc-oldSize)*sizeof(Op)); } p->aOp[i].opcode = op; p->aOp[i].p1 = p1; if( p2<0 && (-1-p2)<p->nLabel && p->aLabel[-1-p2]>=0 ){ p2 = p->aLabel[-1-p2]; } p->aOp[i].p2 = p2; p->aOp[i].p3 = 0; p->aOp[i].p3type = P3_NOTUSED;#ifndef NDEBUG if( sqlite_vdbe_addop_trace ) vdbePrintOp(0, i, &p->aOp[i]);#endif return i;}/*** Create a new symbolic label for an instruction that has yet to be** coded. The symbolic label is really just a negative number. The** label can be used as the P2 value of an operation. Later, when** the label is resolved to a specific address, the VDBE will scan** through its operation list and change all values of P2 which match** the label into the resolved address.**** The VDBE knows that a P2 value is a label because labels are** always negative and P2 values are suppose to be non-negative.** Hence, a negative P2 value is a label that has yet to be resolved.*/int sqliteVdbeMakeLabel(Vdbe *p){ int i; i = p->nLabel++; assert( p->magic==VDBE_MAGIC_INIT ); if( i>=p->nLabelAlloc ){ int *aNew; p->nLabelAlloc = p->nLabelAlloc*2 + 10; aNew = sqliteRealloc( p->aLabel, p->nLabelAlloc*sizeof(p->aLabel[0])); if( aNew==0 ){ sqliteFree(p->aLabel); } p->aLabel = aNew; } if( p->aLabel==0 ){ p->nLabel = 0; p->nLabelAlloc = 0; return 0; } p->aLabel[i] = -1; return -1-i;}/*** Resolve label "x" to be the address of the next instruction to** be inserted. The parameter "x" must have been obtained from** a prior call to sqliteVdbeMakeLabel().*/void sqliteVdbeResolveLabel(Vdbe *p, int x){ int j; assert( p->magic==VDBE_MAGIC_INIT );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -