00001 /* 00002 ** 2003 September 6 00003 ** 00004 ** The author disclaims copyright to this source code. In place of 00005 ** a legal notice, here is a blessing: 00006 ** 00007 ** May you do good and not evil. 00008 ** May you find forgiveness for yourself and forgive others. 00009 ** May you share freely, never taking more than you give. 00010 ** 00011 ************************************************************************* 00012 ** This is the header file for information that is private to the 00013 ** VDBE. This information used to all be at the top of the single 00014 ** source code file "vdbe.c". When that file became too big (over 00015 ** 6000 lines long) it was split up into several smaller files and 00016 ** this header information was factored out. 00017 ** 00018 ** $Id: vdbeInt.h,v 1.157 2008/11/05 16:37:35 drh Exp $ 00019 */ 00020 #ifndef _VDBEINT_H_ 00021 #define _VDBEINT_H_ 00022 00023 /* 00024 ** intToKey() and keyToInt() used to transform the rowid. But with 00025 ** the latest versions of the design they are no-ops. 00026 */ 00027 #define keyToInt(X) (X) 00028 #define intToKey(X) (X) 00029 00030 00031 /* 00032 ** SQL is translated into a sequence of instructions to be 00033 ** executed by a virtual machine. Each instruction is an instance 00034 ** of the following structure. 00035 */ 00036 typedef struct VdbeOp Op; 00037 00038 /* 00039 ** Boolean values 00040 */ 00041 typedef unsigned char Bool; 00042 00043 /* 00044 ** A cursor is a pointer into a single BTree within a database file. 00045 ** The cursor can seek to a BTree entry with a particular key, or 00046 ** loop over all entries of the Btree. You can also insert new BTree 00047 ** entries or retrieve the key or data from the entry that the cursor 00048 ** is currently pointing to. 00049 ** 00050 ** Every cursor that the virtual machine has open is represented by an 00051 ** instance of the following structure. 00052 ** 00053 ** If the VdbeCursor.isTriggerRow flag is set it means that this cursor is 00054 ** really a single row that represents the NEW or OLD pseudo-table of 00055 ** a row trigger. The data for the row is stored in VdbeCursor.pData and 00056 ** the rowid is in VdbeCursor.iKey. 00057 */ 00058 struct VdbeCursor { 00059 BtCursor *pCursor; /* The cursor structure of the backend */ 00060 int iDb; /* Index of cursor database in db->aDb[] (or -1) */ 00061 i64 lastRowid; /* Last rowid from a Next or NextIdx operation */ 00062 i64 nextRowid; /* Next rowid returned by OP_NewRowid */ 00063 Bool zeroed; /* True if zeroed out and ready for reuse */ 00064 Bool rowidIsValid; /* True if lastRowid is valid */ 00065 Bool atFirst; /* True if pointing to first entry */ 00066 Bool useRandomRowid; /* Generate new record numbers semi-randomly */ 00067 Bool nullRow; /* True if pointing to a row with no data */ 00068 Bool nextRowidValid; /* True if the nextRowid field is valid */ 00069 Bool pseudoTable; /* This is a NEW or OLD pseudo-tables of a trigger */ 00070 Bool ephemPseudoTable; 00071 Bool deferredMoveto; /* A call to sqlite3BtreeMoveto() is needed */ 00072 Bool isTable; /* True if a table requiring integer keys */ 00073 Bool isIndex; /* True if an index containing keys only - no data */ 00074 i64 movetoTarget; /* Argument to the deferred sqlite3BtreeMoveto() */ 00075 Btree *pBt; /* Separate file holding temporary table */ 00076 int nData; /* Number of bytes in pData */ 00077 char *pData; /* Data for a NEW or OLD pseudo-table */ 00078 i64 iKey; /* Key for the NEW or OLD pseudo-table row */ 00079 KeyInfo *pKeyInfo; /* Info about index keys needed by index cursors */ 00080 int nField; /* Number of fields in the header */ 00081 i64 seqCount; /* Sequence counter */ 00082 sqlite3_vtab_cursor *pVtabCursor; /* The cursor for a virtual table */ 00083 const sqlite3_module *pModule; /* Module for cursor pVtabCursor */ 00084 00085 /* Cached information about the header for the data record that the 00086 ** cursor is currently pointing to. Only valid if cacheValid is true. 00087 ** aRow might point to (ephemeral) data for the current row, or it might 00088 ** be NULL. 00089 */ 00090 int cacheStatus; /* Cache is valid if this matches Vdbe.cacheCtr */ 00091 int payloadSize; /* Total number of bytes in the record */ 00092 u32 *aType; /* Type values for all entries in the record */ 00093 u32 *aOffset; /* Cached offsets to the start of each columns data */ 00094 u8 *aRow; /* Data for the current row, if all on one page */ 00095 }; 00096 typedef struct VdbeCursor VdbeCursor; 00097 00098 /* 00099 ** A value for VdbeCursor.cacheValid that means the cache is always invalid. 00100 */ 00101 #define CACHE_STALE 0 00102 00103 /* 00104 ** Internally, the vdbe manipulates nearly all SQL values as Mem 00105 ** structures. Each Mem struct may cache multiple representations (string, 00106 ** integer etc.) of the same value. A value (and therefore Mem structure) 00107 ** has the following properties: 00108 ** 00109 ** Each value has a manifest type. The manifest type of the value stored 00110 ** in a Mem struct is returned by the MemType(Mem*) macro. The type is 00111 ** one of SQLITE_NULL, SQLITE_INTEGER, SQLITE_REAL, SQLITE_TEXT or 00112 ** SQLITE_BLOB. 00113 */ 00114 struct Mem { 00115 union { 00116 i64 i; /* Integer value. Or FuncDef* when flags==MEM_Agg */ 00117 FuncDef *pDef; /* Used only when flags==MEM_Agg */ 00118 } u; 00119 double r; /* Real value */ 00120 sqlite3 *db; /* The associated database connection */ 00121 char *z; /* String or BLOB value */ 00122 int n; /* Number of characters in string value, excluding '\0' */ 00123 u16 flags; /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */ 00124 u8 type; /* One of SQLITE_NULL, SQLITE_TEXT, SQLITE_INTEGER, etc */ 00125 u8 enc; /* SQLITE_UTF8, SQLITE_UTF16BE, SQLITE_UTF16LE */ 00126 void (*xDel)(void *); /* If not null, call this function to delete Mem.z */ 00127 char *zMalloc; /* Dynamic buffer allocated by sqlite3_malloc() */ 00128 }; 00129 00130 /* One or more of the following flags are set to indicate the validOK 00131 ** representations of the value stored in the Mem struct. 00132 ** 00133 ** If the MEM_Null flag is set, then the value is an SQL NULL value. 00134 ** No other flags may be set in this case. 00135 ** 00136 ** If the MEM_Str flag is set then Mem.z points at a string representation. 00137 ** Usually this is encoded in the same unicode encoding as the main 00138 ** database (see below for exceptions). If the MEM_Term flag is also 00139 ** set, then the string is nul terminated. The MEM_Int and MEM_Real 00140 ** flags may coexist with the MEM_Str flag. 00141 ** 00142 ** Multiple of these values can appear in Mem.flags. But only one 00143 ** at a time can appear in Mem.type. 00144 */ 00145 #define MEM_Null 0x0001 /* Value is NULL */ 00146 #define MEM_Str 0x0002 /* Value is a string */ 00147 #define MEM_Int 0x0004 /* Value is an integer */ 00148 #define MEM_Real 0x0008 /* Value is a real number */ 00149 #define MEM_Blob 0x0010 /* Value is a BLOB */ 00150 00151 #define MemSetTypeFlag(p, f) \ 00152 ((p)->flags = ((p)->flags&~(MEM_Int|MEM_Real|MEM_Null|MEM_Blob|MEM_Str))|f) 00153 00154 /* Whenever Mem contains a valid string or blob representation, one of 00155 ** the following flags must be set to determine the memory management 00156 ** policy for Mem.z. The MEM_Term flag tells us whether or not the 00157 ** string is \000 or \u0000 terminated 00158 */ 00159 #define MEM_Term 0x0020 /* String rep is nul terminated */ 00160 #define MEM_Dyn 0x0040 /* Need to call sqliteFree() on Mem.z */ 00161 #define MEM_Static 0x0080 /* Mem.z points to a static string */ 00162 #define MEM_Ephem 0x0100 /* Mem.z points to an ephemeral string */ 00163 #define MEM_Agg 0x0400 /* Mem.z points to an agg function context */ 00164 #define MEM_Zero 0x0800 /* Mem.i contains count of 0s appended to blob */ 00165 00166 #ifdef SQLITE_OMIT_INCRBLOB 00167 #undef MEM_Zero 00168 #define MEM_Zero 0x0000 00169 #endif 00170 00171 00172 /* A VdbeFunc is just a FuncDef (defined in sqliteInt.h) that contains 00173 ** additional information about auxiliary information bound to arguments 00174 ** of the function. This is used to implement the sqlite3_get_auxdata() 00175 ** and sqlite3_set_auxdata() APIs. The "auxdata" is some auxiliary data 00176 ** that can be associated with a constant argument to a function. This 00177 ** allows functions such as "regexp" to compile their constant regular 00178 ** expression argument once and reused the compiled code for multiple 00179 ** invocations. 00180 */ 00181 struct VdbeFunc { 00182 FuncDef *pFunc; /* The definition of the function */ 00183 int nAux; /* Number of entries allocated for apAux[] */ 00184 struct AuxData { 00185 void *pAux; /* Aux data for the i-th argument */ 00186 void (*xDelete)(void *); /* Destructor for the aux data */ 00187 } apAux[1]; /* One slot for each function argument */ 00188 }; 00189 00190 /* 00191 ** The "context" argument for a installable function. A pointer to an 00192 ** instance of this structure is the first argument to the routines used 00193 ** implement the SQL functions. 00194 ** 00195 ** There is a typedef for this structure in sqlite.h. So all routines, 00196 ** even the public interface to SQLite, can use a pointer to this structure. 00197 ** But this file is the only place where the internal details of this 00198 ** structure are known. 00199 ** 00200 ** This structure is defined inside of vdbeInt.h because it uses substructures 00201 ** (Mem) which are only defined there. 00202 */ 00203 struct sqlite3_context { 00204 FuncDef *pFunc; /* Pointer to function information. MUST BE FIRST */ 00205 VdbeFunc *pVdbeFunc; /* Auxilary data, if created. */ 00206 Mem s; /* The return value is stored here */ 00207 Mem *pMem; /* Memory cell used to store aggregate context */ 00208 int isError; /* Error code returned by the function. */ 00209 CollSeq *pColl; /* Collating sequence */ 00210 }; 00211 00212 /* 00213 ** A Set structure is used for quick testing to see if a value 00214 ** is part of a small set. Sets are used to implement code like 00215 ** this: 00216 ** x.y IN ('hi','hoo','hum') 00217 */ 00218 typedef struct Set Set; 00219 struct Set { 00220 Hash hash; /* A set is just a hash table */ 00221 HashElem *prev; /* Previously accessed hash elemen */ 00222 }; 00223 00224 /* 00225 ** A FifoPage structure holds a single page of valves. Pages are arranged 00226 ** in a list. 00227 */ 00228 typedef struct FifoPage FifoPage; 00229 struct FifoPage { 00230 int nSlot; /* Number of entries aSlot[] */ 00231 int iWrite; /* Push the next value into this entry in aSlot[] */ 00232 int iRead; /* Read the next value from this entry in aSlot[] */ 00233 FifoPage *pNext; /* Next page in the fifo */ 00234 i64 aSlot[1]; /* One or more slots for rowid values */ 00235 }; 00236 00237 /* 00238 ** The Fifo structure is typedef-ed in vdbeInt.h. But the implementation 00239 ** of that structure is private to this file. 00240 ** 00241 ** The Fifo structure describes the entire fifo. 00242 */ 00243 typedef struct Fifo Fifo; 00244 struct Fifo { 00245 int nEntry; /* Total number of entries */ 00246 sqlite3 *db; /* The associated database connection */ 00247 FifoPage *pFirst; /* First page on the list */ 00248 FifoPage *pLast; /* Last page on the list */ 00249 }; 00250 00251 /* 00252 ** A Context stores the last insert rowid, the last statement change count, 00253 ** and the current statement change count (i.e. changes since last statement). 00254 ** The current keylist is also stored in the context. 00255 ** Elements of Context structure type make up the ContextStack, which is 00256 ** updated by the ContextPush and ContextPop opcodes (used by triggers). 00257 ** The context is pushed before executing a trigger a popped when the 00258 ** trigger finishes. 00259 */ 00260 typedef struct Context Context; 00261 struct Context { 00262 i64 lastRowid; /* Last insert rowid (sqlite3.lastRowid) */ 00263 int nChange; /* Statement changes (Vdbe.nChanges) */ 00264 Fifo sFifo; /* Records that will participate in a DELETE or UPDATE */ 00265 }; 00266 00267 /* 00268 ** An instance of the virtual machine. This structure contains the complete 00269 ** state of the virtual machine. 00270 ** 00271 ** The "sqlite3_stmt" structure pointer that is returned by sqlite3_compile() 00272 ** is really a pointer to an instance of this structure. 00273 ** 00274 ** The Vdbe.inVtabMethod variable is set to non-zero for the duration of 00275 ** any virtual table method invocations made by the vdbe program. It is 00276 ** set to 2 for xDestroy method calls and 1 for all other methods. This 00277 ** variable is used for two purposes: to allow xDestroy methods to execute 00278 ** "DROP TABLE" statements and to prevent some nasty side effects of 00279 ** malloc failure when SQLite is invoked recursively by a virtual table 00280 ** method function. 00281 */ 00282 struct Vdbe { 00283 sqlite3 *db; /* The whole database */ 00284 Vdbe *pPrev,*pNext; /* Linked list of VDBEs with the same Vdbe.db */ 00285 int nOp; /* Number of instructions in the program */ 00286 int nOpAlloc; /* Number of slots allocated for aOp[] */ 00287 Op *aOp; /* Space to hold the virtual machine's program */ 00288 int nLabel; /* Number of labels used */ 00289 int nLabelAlloc; /* Number of slots allocated in aLabel[] */ 00290 int *aLabel; /* Space to hold the labels */ 00291 Mem **apArg; /* Arguments to currently executing user function */ 00292 Mem *aColName; /* Column names to return */ 00293 int nCursor; /* Number of slots in apCsr[] */ 00294 VdbeCursor **apCsr; /* One element of this array for each open cursor */ 00295 int nVar; /* Number of entries in aVar[] */ 00296 Mem *aVar; /* Values for the OP_Variable opcode. */ 00297 char **azVar; /* Name of variables */ 00298 int okVar; /* True if azVar[] has been initialized */ 00299 int magic; /* Magic number for sanity checking */ 00300 int nMem; /* Number of memory locations currently allocated */ 00301 Mem *aMem; /* The memory locations */ 00302 int nCallback; /* Number of callbacks invoked so far */ 00303 int cacheCtr; /* VdbeCursor row cache generation counter */ 00304 Fifo sFifo; /* A list of ROWIDs */ 00305 int contextStackTop; /* Index of top element in the context stack */ 00306 int contextStackDepth; /* The size of the "context" stack */ 00307 Context *contextStack; /* Stack used by opcodes ContextPush & ContextPop*/ 00308 int pc; /* The program counter */ 00309 int rc; /* Value to return */ 00310 unsigned uniqueCnt; /* Used by OP_MakeRecord when P2!=0 */ 00311 int errorAction; /* Recovery action to do in case of an error */ 00312 int inTempTrans; /* True if temp database is transactioned */ 00313 int nResColumn; /* Number of columns in one row of the result set */ 00314 char **azResColumn; /* Values for one row of result */ 00315 char *zErrMsg; /* Error message written here */ 00316 Mem *pResultSet; /* Pointer to an array of results */ 00317 u8 explain; /* True if EXPLAIN present on SQL command */ 00318 u8 changeCntOn; /* True to update the change-counter */ 00319 u8 expired; /* True if the VM needs to be recompiled */ 00320 u8 minWriteFileFormat; /* Minimum file format for writable database files */ 00321 u8 inVtabMethod; /* See comments above */ 00322 u8 usesStmtJournal; /* True if uses a statement journal */ 00323 u8 readOnly; /* True for read-only statements */ 00324 int nChange; /* Number of db changes made since last reset */ 00325 i64 startTime; /* Time when query started - used for profiling */ 00326 int btreeMask; /* Bitmask of db->aDb[] entries referenced */ 00327 BtreeMutexArray aMutex; /* An array of Btree used here and needing locks */ 00328 int aCounter[2]; /* Counters used by sqlite3_stmt_status() */ 00329 int nSql; /* Number of bytes in zSql */ 00330 char *zSql; /* Text of the SQL statement that generated this */ 00331 #ifdef SQLITE_DEBUG 00332 FILE *trace; /* Write an execution trace here, if not NULL */ 00333 #endif 00334 int openedStatement; /* True if this VM has opened a statement journal */ 00335 #ifdef SQLITE_SSE 00336 int fetchId; /* Statement number used by sqlite3_fetch_statement */ 00337 int lru; /* Counter used for LRU cache replacement */ 00338 #endif 00339 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT 00340 Vdbe *pLruPrev; 00341 Vdbe *pLruNext; 00342 #endif 00343 }; 00344 00345 /* 00346 ** The following are allowed values for Vdbe.magic 00347 */ 00348 #define VDBE_MAGIC_INIT 0x26bceaa5 /* Building a VDBE program */ 00349 #define VDBE_MAGIC_RUN 0xbdf20da3 /* VDBE is ready to execute */ 00350 #define VDBE_MAGIC_HALT 0x519c2973 /* VDBE has completed execution */ 00351 #define VDBE_MAGIC_DEAD 0xb606c3c8 /* The VDBE has been deallocated */ 00352 00353 /* 00354 ** Function prototypes 00355 */ 00356 void sqlite3VdbeFreeCursor(Vdbe *, VdbeCursor*); 00357 void sqliteVdbePopStack(Vdbe*,int); 00358 int sqlite3VdbeCursorMoveto(VdbeCursor*); 00359 #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE) 00360 void sqlite3VdbePrintOp(FILE*, int, Op*); 00361 #endif 00362 int sqlite3VdbeSerialTypeLen(u32); 00363 u32 sqlite3VdbeSerialType(Mem*, int); 00364 int sqlite3VdbeSerialPut(unsigned char*, int, Mem*, int); 00365 int sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*); 00366 void sqlite3VdbeDeleteAuxData(VdbeFunc*, int); 00367 00368 int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *); 00369 int sqlite3VdbeIdxKeyCompare(VdbeCursor*,UnpackedRecord*,int*); 00370 int sqlite3VdbeIdxRowid(BtCursor *, i64 *); 00371 int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*); 00372 int sqlite3VdbeExec(Vdbe*); 00373 int sqlite3VdbeList(Vdbe*); 00374 int sqlite3VdbeHalt(Vdbe*); 00375 int sqlite3VdbeChangeEncoding(Mem *, int); 00376 int sqlite3VdbeMemTooBig(Mem*); 00377 int sqlite3VdbeMemCopy(Mem*, const Mem*); 00378 void sqlite3VdbeMemShallowCopy(Mem*, const Mem*, int); 00379 void sqlite3VdbeMemMove(Mem*, Mem*); 00380 int sqlite3VdbeMemNulTerminate(Mem*); 00381 int sqlite3VdbeMemSetStr(Mem*, const char*, int, u8, void(*)(void*)); 00382 void sqlite3VdbeMemSetInt64(Mem*, i64); 00383 void sqlite3VdbeMemSetDouble(Mem*, double); 00384 void sqlite3VdbeMemSetNull(Mem*); 00385 void sqlite3VdbeMemSetZeroBlob(Mem*,int); 00386 int sqlite3VdbeMemMakeWriteable(Mem*); 00387 int sqlite3VdbeMemStringify(Mem*, int); 00388 i64 sqlite3VdbeIntValue(Mem*); 00389 int sqlite3VdbeMemIntegerify(Mem*); 00390 double sqlite3VdbeRealValue(Mem*); 00391 void sqlite3VdbeIntegerAffinity(Mem*); 00392 int sqlite3VdbeMemRealify(Mem*); 00393 int sqlite3VdbeMemNumerify(Mem*); 00394 int sqlite3VdbeMemFromBtree(BtCursor*,int,int,int,Mem*); 00395 void sqlite3VdbeMemRelease(Mem *p); 00396 void sqlite3VdbeMemReleaseExternal(Mem *p); 00397 int sqlite3VdbeMemFinalize(Mem*, FuncDef*); 00398 const char *sqlite3OpcodeName(int); 00399 int sqlite3VdbeOpcodeHasProperty(int, int); 00400 int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve); 00401 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT 00402 int sqlite3VdbeReleaseBuffers(Vdbe *p); 00403 #endif 00404 00405 #ifndef NDEBUG 00406 void sqlite3VdbeMemSanity(Mem*); 00407 #endif 00408 int sqlite3VdbeMemTranslate(Mem*, u8); 00409 #ifdef SQLITE_DEBUG 00410 void sqlite3VdbePrintSql(Vdbe*); 00411 void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf); 00412 #endif 00413 int sqlite3VdbeMemHandleBom(Mem *pMem); 00414 void sqlite3VdbeFifoInit(Fifo*, sqlite3*); 00415 int sqlite3VdbeFifoPush(Fifo*, i64); 00416 int sqlite3VdbeFifoPop(Fifo*, i64*); 00417 void sqlite3VdbeFifoClear(Fifo*); 00418 00419 #ifndef SQLITE_OMIT_INCRBLOB 00420 int sqlite3VdbeMemExpandBlob(Mem *); 00421 #else 00422 #define sqlite3VdbeMemExpandBlob(x) SQLITE_OK 00423 #endif 00424 00425 #endif /* !defined(_VDBEINT_H_) */
ContextLogger2—ContextLogger2 Logger Daemon Internals—Generated on Mon May 2 13:49:57 2011 by Doxygen 1.6.1