1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/extensions/sqlite/sqlite-source/vdbeInt.h Sun Mar 30 07:00:22 2008 +0000
1.3 @@ -0,0 +1,427 @@
1.4 +/*
1.5 +** 2003 September 6
1.6 +**
1.7 +** The author disclaims copyright to this source code. In place of
1.8 +** a legal notice, here is a blessing:
1.9 +**
1.10 +** May you do good and not evil.
1.11 +** May you find forgiveness for yourself and forgive others.
1.12 +** May you share freely, never taking more than you give.
1.13 +**
1.14 +*************************************************************************
1.15 +** This is the header file for information that is private to the
1.16 +** VDBE. This information used to all be at the top of the single
1.17 +** source code file "vdbe.c". When that file became too big (over
1.18 +** 6000 lines long) it was split up into several smaller files and
1.19 +** this header information was factored out.
1.20 +*/
1.21 +#ifndef _VDBEINT_H_
1.22 +#define _VDBEINT_H_
1.23 +
1.24 +/*
1.25 +** intToKey() and keyToInt() used to transform the rowid. But with
1.26 +** the latest versions of the design they are no-ops.
1.27 +*/
1.28 +#define keyToInt(X) (X)
1.29 +#define intToKey(X) (X)
1.30 +
1.31 +
1.32 +/*
1.33 +** SQL is translated into a sequence of instructions to be
1.34 +** executed by a virtual machine. Each instruction is an instance
1.35 +** of the following structure.
1.36 +*/
1.37 +typedef struct VdbeOp Op;
1.38 +
1.39 +/*
1.40 +** Boolean values
1.41 +*/
1.42 +typedef unsigned char Bool;
1.43 +
1.44 +/*
1.45 +** A cursor is a pointer into a single BTree within a database file.
1.46 +** The cursor can seek to a BTree entry with a particular key, or
1.47 +** loop over all entries of the Btree. You can also insert new BTree
1.48 +** entries or retrieve the key or data from the entry that the cursor
1.49 +** is currently pointing to.
1.50 +**
1.51 +** Every cursor that the virtual machine has open is represented by an
1.52 +** instance of the following structure.
1.53 +**
1.54 +** If the Cursor.isTriggerRow flag is set it means that this cursor is
1.55 +** really a single row that represents the NEW or OLD pseudo-table of
1.56 +** a row trigger. The data for the row is stored in Cursor.pData and
1.57 +** the rowid is in Cursor.iKey.
1.58 +*/
1.59 +struct Cursor {
1.60 + BtCursor *pCursor; /* The cursor structure of the backend */
1.61 + int iDb; /* Index of cursor database in db->aDb[] (or -1) */
1.62 + i64 lastRowid; /* Last rowid from a Next or NextIdx operation */
1.63 + i64 nextRowid; /* Next rowid returned by OP_NewRowid */
1.64 + Bool zeroed; /* True if zeroed out and ready for reuse */
1.65 + Bool rowidIsValid; /* True if lastRowid is valid */
1.66 + Bool atFirst; /* True if pointing to first entry */
1.67 + Bool useRandomRowid; /* Generate new record numbers semi-randomly */
1.68 + Bool nullRow; /* True if pointing to a row with no data */
1.69 + Bool nextRowidValid; /* True if the nextRowid field is valid */
1.70 + Bool pseudoTable; /* This is a NEW or OLD pseudo-tables of a trigger */
1.71 + Bool deferredMoveto; /* A call to sqlite3BtreeMoveto() is needed */
1.72 + Bool isTable; /* True if a table requiring integer keys */
1.73 + Bool isIndex; /* True if an index containing keys only - no data */
1.74 + u8 bogusIncrKey; /* Something for pIncrKey to point to if pKeyInfo==0 */
1.75 + i64 movetoTarget; /* Argument to the deferred sqlite3BtreeMoveto() */
1.76 + Btree *pBt; /* Separate file holding temporary table */
1.77 + int nData; /* Number of bytes in pData */
1.78 + char *pData; /* Data for a NEW or OLD pseudo-table */
1.79 + i64 iKey; /* Key for the NEW or OLD pseudo-table row */
1.80 + u8 *pIncrKey; /* Pointer to pKeyInfo->incrKey */
1.81 + KeyInfo *pKeyInfo; /* Info about index keys needed by index cursors */
1.82 + int nField; /* Number of fields in the header */
1.83 + i64 seqCount; /* Sequence counter */
1.84 + sqlite3_vtab_cursor *pVtabCursor; /* The cursor for a virtual table */
1.85 + const sqlite3_module *pModule; /* Module for cursor pVtabCursor */
1.86 +
1.87 + /* Cached information about the header for the data record that the
1.88 + ** cursor is currently pointing to. Only valid if cacheValid is true.
1.89 + ** aRow might point to (ephemeral) data for the current row, or it might
1.90 + ** be NULL.
1.91 + */
1.92 + int cacheStatus; /* Cache is valid if this matches Vdbe.cacheCtr */
1.93 + int payloadSize; /* Total number of bytes in the record */
1.94 + u32 *aType; /* Type values for all entries in the record */
1.95 + u32 *aOffset; /* Cached offsets to the start of each columns data */
1.96 + u8 *aRow; /* Data for the current row, if all on one page */
1.97 +};
1.98 +typedef struct Cursor Cursor;
1.99 +
1.100 +/*
1.101 +** Number of bytes of string storage space available to each stack
1.102 +** layer without having to malloc. NBFS is short for Number of Bytes
1.103 +** For Strings.
1.104 +*/
1.105 +#define NBFS 32
1.106 +
1.107 +/*
1.108 +** A value for Cursor.cacheValid that means the cache is always invalid.
1.109 +*/
1.110 +#define CACHE_STALE 0
1.111 +
1.112 +/*
1.113 +** Internally, the vdbe manipulates nearly all SQL values as Mem
1.114 +** structures. Each Mem struct may cache multiple representations (string,
1.115 +** integer etc.) of the same value. A value (and therefore Mem structure)
1.116 +** has the following properties:
1.117 +**
1.118 +** Each value has a manifest type. The manifest type of the value stored
1.119 +** in a Mem struct is returned by the MemType(Mem*) macro. The type is
1.120 +** one of SQLITE_NULL, SQLITE_INTEGER, SQLITE_REAL, SQLITE_TEXT or
1.121 +** SQLITE_BLOB.
1.122 +*/
1.123 +struct Mem {
1.124 + union {
1.125 + i64 i; /* Integer value. Or FuncDef* when flags==MEM_Agg */
1.126 + FuncDef *pDef; /* Used only when flags==MEM_Agg */
1.127 + } u;
1.128 + double r; /* Real value */
1.129 + sqlite3 *db; /* The associated database connection */
1.130 + char *z; /* String or BLOB value */
1.131 + int n; /* Number of characters in string value, including '\0' */
1.132 + u16 flags; /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */
1.133 + u8 type; /* One of SQLITE_NULL, SQLITE_TEXT, SQLITE_INTEGER, etc */
1.134 + u8 enc; /* SQLITE_UTF8, SQLITE_UTF16BE, SQLITE_UTF16LE */
1.135 + void (*xDel)(void *); /* If not null, call this function to delete Mem.z */
1.136 + char zShort[NBFS]; /* Space for short strings */
1.137 +};
1.138 +typedef struct Mem Mem;
1.139 +
1.140 +/* One or more of the following flags are set to indicate the validOK
1.141 +** representations of the value stored in the Mem struct.
1.142 +**
1.143 +** If the MEM_Null flag is set, then the value is an SQL NULL value.
1.144 +** No other flags may be set in this case.
1.145 +**
1.146 +** If the MEM_Str flag is set then Mem.z points at a string representation.
1.147 +** Usually this is encoded in the same unicode encoding as the main
1.148 +** database (see below for exceptions). If the MEM_Term flag is also
1.149 +** set, then the string is nul terminated. The MEM_Int and MEM_Real
1.150 +** flags may coexist with the MEM_Str flag.
1.151 +**
1.152 +** Multiple of these values can appear in Mem.flags. But only one
1.153 +** at a time can appear in Mem.type.
1.154 +*/
1.155 +#define MEM_Null 0x0001 /* Value is NULL */
1.156 +#define MEM_Str 0x0002 /* Value is a string */
1.157 +#define MEM_Int 0x0004 /* Value is an integer */
1.158 +#define MEM_Real 0x0008 /* Value is a real number */
1.159 +#define MEM_Blob 0x0010 /* Value is a BLOB */
1.160 +
1.161 +/* Whenever Mem contains a valid string or blob representation, one of
1.162 +** the following flags must be set to determine the memory management
1.163 +** policy for Mem.z. The MEM_Term flag tells us whether or not the
1.164 +** string is \000 or \u0000 terminated
1.165 +*/
1.166 +#define MEM_Term 0x0020 /* String rep is nul terminated */
1.167 +#define MEM_Dyn 0x0040 /* Need to call sqliteFree() on Mem.z */
1.168 +#define MEM_Static 0x0080 /* Mem.z points to a static string */
1.169 +#define MEM_Ephem 0x0100 /* Mem.z points to an ephemeral string */
1.170 +#define MEM_Short 0x0200 /* Mem.z points to Mem.zShort */
1.171 +#define MEM_Agg 0x0400 /* Mem.z points to an agg function context */
1.172 +#define MEM_Zero 0x0800 /* Mem.i contains count of 0s appended to blob */
1.173 +
1.174 +#ifdef SQLITE_OMIT_INCRBLOB
1.175 + #undef MEM_Zero
1.176 + #define MEM_Zero 0x0000
1.177 +#endif
1.178 +
1.179 +
1.180 +/* A VdbeFunc is just a FuncDef (defined in sqliteInt.h) that contains
1.181 +** additional information about auxiliary information bound to arguments
1.182 +** of the function. This is used to implement the sqlite3_get_auxdata()
1.183 +** and sqlite3_set_auxdata() APIs. The "auxdata" is some auxiliary data
1.184 +** that can be associated with a constant argument to a function. This
1.185 +** allows functions such as "regexp" to compile their constant regular
1.186 +** expression argument once and reused the compiled code for multiple
1.187 +** invocations.
1.188 +*/
1.189 +struct VdbeFunc {
1.190 + FuncDef *pFunc; /* The definition of the function */
1.191 + int nAux; /* Number of entries allocated for apAux[] */
1.192 + struct AuxData {
1.193 + void *pAux; /* Aux data for the i-th argument */
1.194 + void (*xDelete)(void *); /* Destructor for the aux data */
1.195 + } apAux[1]; /* One slot for each function argument */
1.196 +};
1.197 +typedef struct VdbeFunc VdbeFunc;
1.198 +
1.199 +/*
1.200 +** The "context" argument for a installable function. A pointer to an
1.201 +** instance of this structure is the first argument to the routines used
1.202 +** implement the SQL functions.
1.203 +**
1.204 +** There is a typedef for this structure in sqlite.h. So all routines,
1.205 +** even the public interface to SQLite, can use a pointer to this structure.
1.206 +** But this file is the only place where the internal details of this
1.207 +** structure are known.
1.208 +**
1.209 +** This structure is defined inside of vdbeInt.h because it uses substructures
1.210 +** (Mem) which are only defined there.
1.211 +*/
1.212 +struct sqlite3_context {
1.213 + FuncDef *pFunc; /* Pointer to function information. MUST BE FIRST */
1.214 + VdbeFunc *pVdbeFunc; /* Auxilary data, if created. */
1.215 + Mem s; /* The return value is stored here */
1.216 + Mem *pMem; /* Memory cell used to store aggregate context */
1.217 + u8 isError; /* Set to true for an error */
1.218 + CollSeq *pColl; /* Collating sequence */
1.219 +};
1.220 +
1.221 +/*
1.222 +** A Set structure is used for quick testing to see if a value
1.223 +** is part of a small set. Sets are used to implement code like
1.224 +** this:
1.225 +** x.y IN ('hi','hoo','hum')
1.226 +*/
1.227 +typedef struct Set Set;
1.228 +struct Set {
1.229 + Hash hash; /* A set is just a hash table */
1.230 + HashElem *prev; /* Previously accessed hash elemen */
1.231 +};
1.232 +
1.233 +/*
1.234 +** A FifoPage structure holds a single page of valves. Pages are arranged
1.235 +** in a list.
1.236 +*/
1.237 +typedef struct FifoPage FifoPage;
1.238 +struct FifoPage {
1.239 + int nSlot; /* Number of entries aSlot[] */
1.240 + int iWrite; /* Push the next value into this entry in aSlot[] */
1.241 + int iRead; /* Read the next value from this entry in aSlot[] */
1.242 + FifoPage *pNext; /* Next page in the fifo */
1.243 + i64 aSlot[1]; /* One or more slots for rowid values */
1.244 +};
1.245 +
1.246 +/*
1.247 +** The Fifo structure is typedef-ed in vdbeInt.h. But the implementation
1.248 +** of that structure is private to this file.
1.249 +**
1.250 +** The Fifo structure describes the entire fifo.
1.251 +*/
1.252 +typedef struct Fifo Fifo;
1.253 +struct Fifo {
1.254 + int nEntry; /* Total number of entries */
1.255 + FifoPage *pFirst; /* First page on the list */
1.256 + FifoPage *pLast; /* Last page on the list */
1.257 +};
1.258 +
1.259 +/*
1.260 +** A Context stores the last insert rowid, the last statement change count,
1.261 +** and the current statement change count (i.e. changes since last statement).
1.262 +** The current keylist is also stored in the context.
1.263 +** Elements of Context structure type make up the ContextStack, which is
1.264 +** updated by the ContextPush and ContextPop opcodes (used by triggers).
1.265 +** The context is pushed before executing a trigger a popped when the
1.266 +** trigger finishes.
1.267 +*/
1.268 +typedef struct Context Context;
1.269 +struct Context {
1.270 + i64 lastRowid; /* Last insert rowid (sqlite3.lastRowid) */
1.271 + int nChange; /* Statement changes (Vdbe.nChanges) */
1.272 + Fifo sFifo; /* Records that will participate in a DELETE or UPDATE */
1.273 +};
1.274 +
1.275 +/*
1.276 +** An instance of the virtual machine. This structure contains the complete
1.277 +** state of the virtual machine.
1.278 +**
1.279 +** The "sqlite3_stmt" structure pointer that is returned by sqlite3_compile()
1.280 +** is really a pointer to an instance of this structure.
1.281 +**
1.282 +** The Vdbe.inVtabMethod variable is set to non-zero for the duration of
1.283 +** any virtual table method invocations made by the vdbe program. It is
1.284 +** set to 2 for xDestroy method calls and 1 for all other methods. This
1.285 +** variable is used for two purposes: to allow xDestroy methods to execute
1.286 +** "DROP TABLE" statements and to prevent some nasty side effects of
1.287 +** malloc failure when SQLite is invoked recursively by a virtual table
1.288 +** method function.
1.289 +*/
1.290 +struct Vdbe {
1.291 + sqlite3 *db; /* The whole database */
1.292 + Vdbe *pPrev,*pNext; /* Linked list of VDBEs with the same Vdbe.db */
1.293 + int nOp; /* Number of instructions in the program */
1.294 + int nOpAlloc; /* Number of slots allocated for aOp[] */
1.295 + Op *aOp; /* Space to hold the virtual machine's program */
1.296 + int nLabel; /* Number of labels used */
1.297 + int nLabelAlloc; /* Number of slots allocated in aLabel[] */
1.298 + int *aLabel; /* Space to hold the labels */
1.299 + Mem *aStack; /* The operand stack, except string values */
1.300 + Mem *pTos; /* Top entry in the operand stack */
1.301 + Mem **apArg; /* Arguments to currently executing user function */
1.302 + Mem *aColName; /* Column names to return */
1.303 + int nCursor; /* Number of slots in apCsr[] */
1.304 + Cursor **apCsr; /* One element of this array for each open cursor */
1.305 + int nVar; /* Number of entries in aVar[] */
1.306 + Mem *aVar; /* Values for the OP_Variable opcode. */
1.307 + char **azVar; /* Name of variables */
1.308 + int okVar; /* True if azVar[] has been initialized */
1.309 + int magic; /* Magic number for sanity checking */
1.310 + int nMem; /* Number of memory locations currently allocated */
1.311 + Mem *aMem; /* The memory locations */
1.312 + int nCallback; /* Number of callbacks invoked so far */
1.313 + int cacheCtr; /* Cursor row cache generation counter */
1.314 + Fifo sFifo; /* A list of ROWIDs */
1.315 + int contextStackTop; /* Index of top element in the context stack */
1.316 + int contextStackDepth; /* The size of the "context" stack */
1.317 + Context *contextStack; /* Stack used by opcodes ContextPush & ContextPop*/
1.318 + int pc; /* The program counter */
1.319 + int rc; /* Value to return */
1.320 + unsigned uniqueCnt; /* Used by OP_MakeRecord when P2!=0 */
1.321 + int errorAction; /* Recovery action to do in case of an error */
1.322 + int inTempTrans; /* True if temp database is transactioned */
1.323 + int returnStack[25]; /* Return address stack for OP_Gosub & OP_Return */
1.324 + int returnDepth; /* Next unused element in returnStack[] */
1.325 + int nResColumn; /* Number of columns in one row of the result set */
1.326 + char **azResColumn; /* Values for one row of result */
1.327 + int popStack; /* Pop the stack this much on entry to VdbeExec() */
1.328 + char *zErrMsg; /* Error message written here */
1.329 + u8 resOnStack; /* True if there are result values on the stack */
1.330 + u8 explain; /* True if EXPLAIN present on SQL command */
1.331 + u8 changeCntOn; /* True to update the change-counter */
1.332 + u8 aborted; /* True if ROLLBACK in another VM causes an abort */
1.333 + u8 expired; /* True if the VM needs to be recompiled */
1.334 + u8 minWriteFileFormat; /* Minimum file format for writable database files */
1.335 + u8 inVtabMethod; /* See comments above */
1.336 + int nChange; /* Number of db changes made since last reset */
1.337 + i64 startTime; /* Time when query started - used for profiling */
1.338 + int btreeMask; /* Bitmask of db->aDb[] entries referenced */
1.339 + BtreeMutexArray aMutex; /* An array of Btree used here and needing locks */
1.340 + int nSql; /* Number of bytes in zSql */
1.341 + char *zSql; /* Text of the SQL statement that generated this */
1.342 +#ifdef SQLITE_DEBUG
1.343 + FILE *trace; /* Write an execution trace here, if not NULL */
1.344 +#endif
1.345 + int openedStatement; /* True if this VM has opened a statement journal */
1.346 +#ifdef SQLITE_SSE
1.347 + int fetchId; /* Statement number used by sqlite3_fetch_statement */
1.348 + int lru; /* Counter used for LRU cache replacement */
1.349 +#endif
1.350 +};
1.351 +
1.352 +/*
1.353 +** The following are allowed values for Vdbe.magic
1.354 +*/
1.355 +#define VDBE_MAGIC_INIT 0x26bceaa5 /* Building a VDBE program */
1.356 +#define VDBE_MAGIC_RUN 0xbdf20da3 /* VDBE is ready to execute */
1.357 +#define VDBE_MAGIC_HALT 0x519c2973 /* VDBE has completed execution */
1.358 +#define VDBE_MAGIC_DEAD 0xb606c3c8 /* The VDBE has been deallocated */
1.359 +
1.360 +/*
1.361 +** Function prototypes
1.362 +*/
1.363 +void sqlite3VdbeFreeCursor(Vdbe *, Cursor*);
1.364 +void sqliteVdbePopStack(Vdbe*,int);
1.365 +int sqlite3VdbeCursorMoveto(Cursor*);
1.366 +#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
1.367 +void sqlite3VdbePrintOp(FILE*, int, Op*);
1.368 +#endif
1.369 +int sqlite3VdbeSerialTypeLen(u32);
1.370 +u32 sqlite3VdbeSerialType(Mem*, int);
1.371 +int sqlite3VdbeSerialPut(unsigned char*, int, Mem*, int);
1.372 +int sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*);
1.373 +void sqlite3VdbeDeleteAuxData(VdbeFunc*, int);
1.374 +
1.375 +int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
1.376 +int sqlite3VdbeIdxKeyCompare(Cursor*,int,const unsigned char*,int*);
1.377 +int sqlite3VdbeIdxRowid(BtCursor *, i64 *);
1.378 +int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
1.379 +int sqlite3VdbeRecordCompare(void*,int,const void*,int, const void*);
1.380 +int sqlite3VdbeIdxRowidLen(const u8*);
1.381 +int sqlite3VdbeExec(Vdbe*);
1.382 +int sqlite3VdbeList(Vdbe*);
1.383 +int sqlite3VdbeHalt(Vdbe*);
1.384 +int sqlite3VdbeChangeEncoding(Mem *, int);
1.385 +int sqlite3VdbeMemTooBig(Mem*);
1.386 +int sqlite3VdbeMemCopy(Mem*, const Mem*);
1.387 +void sqlite3VdbeMemShallowCopy(Mem*, const Mem*, int);
1.388 +int sqlite3VdbeMemMove(Mem*, Mem*);
1.389 +int sqlite3VdbeMemNulTerminate(Mem*);
1.390 +int sqlite3VdbeMemSetStr(Mem*, const char*, int, u8, void(*)(void*));
1.391 +void sqlite3VdbeMemSetInt64(Mem*, i64);
1.392 +void sqlite3VdbeMemSetDouble(Mem*, double);
1.393 +void sqlite3VdbeMemSetNull(Mem*);
1.394 +void sqlite3VdbeMemSetZeroBlob(Mem*,int);
1.395 +int sqlite3VdbeMemMakeWriteable(Mem*);
1.396 +int sqlite3VdbeMemDynamicify(Mem*);
1.397 +int sqlite3VdbeMemStringify(Mem*, int);
1.398 +i64 sqlite3VdbeIntValue(Mem*);
1.399 +int sqlite3VdbeMemIntegerify(Mem*);
1.400 +double sqlite3VdbeRealValue(Mem*);
1.401 +void sqlite3VdbeIntegerAffinity(Mem*);
1.402 +int sqlite3VdbeMemRealify(Mem*);
1.403 +int sqlite3VdbeMemNumerify(Mem*);
1.404 +int sqlite3VdbeMemFromBtree(BtCursor*,int,int,int,Mem*);
1.405 +void sqlite3VdbeMemRelease(Mem *p);
1.406 +int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
1.407 +const char *sqlite3OpcodeName(int);
1.408 +
1.409 +#ifndef NDEBUG
1.410 + void sqlite3VdbeMemSanity(Mem*);
1.411 + int sqlite3VdbeOpcodeNoPush(u8);
1.412 +#endif
1.413 +int sqlite3VdbeMemTranslate(Mem*, u8);
1.414 +#ifdef SQLITE_DEBUG
1.415 + void sqlite3VdbePrintSql(Vdbe*);
1.416 + void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf);
1.417 +#endif
1.418 +int sqlite3VdbeMemHandleBom(Mem *pMem);
1.419 +void sqlite3VdbeFifoInit(Fifo*);
1.420 +int sqlite3VdbeFifoPush(Fifo*, i64);
1.421 +int sqlite3VdbeFifoPop(Fifo*, i64*);
1.422 +void sqlite3VdbeFifoClear(Fifo*);
1.423 +
1.424 +#ifndef SQLITE_OMIT_INCRBLOB
1.425 + int sqlite3VdbeMemExpandBlob(Mem *);
1.426 +#else
1.427 + #define sqlite3VdbeMemExpandBlob(x) SQLITE_OK
1.428 +#endif
1.429 +
1.430 +#endif /* !defined(_VDBEINT_H_) */