xref: /sqlite-3.40.0/src/vdbeInt.h (revision be217793)
1 /*
2 ** 2003 September 6
3 **
4 ** The author disclaims copyright to this source code.  In place of
5 ** a legal notice, here is a blessing:
6 **
7 **    May you do good and not evil.
8 **    May you find forgiveness for yourself and forgive others.
9 **    May you share freely, never taking more than you give.
10 **
11 *************************************************************************
12 ** This is the header file for information that is private to the
13 ** VDBE.  This information used to all be at the top of the single
14 ** source code file "vdbe.c".  When that file became too big (over
15 ** 6000 lines long) it was split up into several smaller files and
16 ** this header information was factored out.
17 **
18 ** $Id: vdbeInt.h,v 1.164 2009/02/20 01:28:59 drh Exp $
19 */
20 #ifndef _VDBEINT_H_
21 #define _VDBEINT_H_
22 
23 /*
24 ** intToKey() and keyToInt() used to transform the rowid.  But with
25 ** the latest versions of the design they are no-ops.
26 */
27 #define keyToInt(X)   (X)
28 #define intToKey(X)   (X)
29 
30 
31 /*
32 ** SQL is translated into a sequence of instructions to be
33 ** executed by a virtual machine.  Each instruction is an instance
34 ** of the following structure.
35 */
36 typedef struct VdbeOp Op;
37 
38 /*
39 ** Boolean values
40 */
41 typedef unsigned char Bool;
42 
43 /*
44 ** A cursor is a pointer into a single BTree within a database file.
45 ** The cursor can seek to a BTree entry with a particular key, or
46 ** loop over all entries of the Btree.  You can also insert new BTree
47 ** entries or retrieve the key or data from the entry that the cursor
48 ** is currently pointing to.
49 **
50 ** Every cursor that the virtual machine has open is represented by an
51 ** instance of the following structure.
52 **
53 ** If the VdbeCursor.isTriggerRow flag is set it means that this cursor is
54 ** really a single row that represents the NEW or OLD pseudo-table of
55 ** a row trigger.  The data for the row is stored in VdbeCursor.pData and
56 ** the rowid is in VdbeCursor.iKey.
57 */
58 struct VdbeCursor {
59   BtCursor *pCursor;    /* The cursor structure of the backend */
60   int iDb;              /* Index of cursor database in db->aDb[] (or -1) */
61   i64 lastRowid;        /* Last rowid from a Next or NextIdx operation */
62   i64 nextRowid;        /* Next rowid returned by OP_NewRowid */
63   Bool zeroed;          /* True if zeroed out and ready for reuse */
64   Bool rowidIsValid;    /* True if lastRowid is valid */
65   Bool atFirst;         /* True if pointing to first entry */
66   Bool useRandomRowid;  /* Generate new record numbers semi-randomly */
67   Bool nullRow;         /* True if pointing to a row with no data */
68   Bool nextRowidValid;  /* True if the nextRowid field is valid */
69   Bool pseudoTable;     /* This is a NEW or OLD pseudo-tables of a trigger */
70   Bool ephemPseudoTable;
71   Bool deferredMoveto;  /* A call to sqlite3BtreeMoveto() is needed */
72   Bool isTable;         /* True if a table requiring integer keys */
73   Bool isIndex;         /* True if an index containing keys only - no data */
74   i64 movetoTarget;     /* Argument to the deferred sqlite3BtreeMoveto() */
75   Btree *pBt;           /* Separate file holding temporary table */
76   int nData;            /* Number of bytes in pData */
77   char *pData;          /* Data for a NEW or OLD pseudo-table */
78   i64 iKey;             /* Key for the NEW or OLD pseudo-table row */
79   KeyInfo *pKeyInfo;    /* Info about index keys needed by index cursors */
80   int nField;           /* Number of fields in the header */
81   i64 seqCount;         /* Sequence counter */
82   sqlite3_vtab_cursor *pVtabCursor;  /* The cursor for a virtual table */
83   const sqlite3_module *pModule;     /* Module for cursor pVtabCursor */
84 
85   /* Cached information about the header for the data record that the
86   ** cursor is currently pointing to.  Only valid if cacheValid is true.
87   ** aRow might point to (ephemeral) data for the current row, or it might
88   ** be NULL.
89   */
90   int cacheStatus;      /* Cache is valid if this matches Vdbe.cacheCtr */
91   int payloadSize;      /* Total number of bytes in the record */
92   u32 *aType;           /* Type values for all entries in the record */
93   u32 *aOffset;         /* Cached offsets to the start of each columns data */
94   u8 *aRow;             /* Data for the current row, if all on one page */
95 };
96 typedef struct VdbeCursor VdbeCursor;
97 
98 /*
99 ** A value for VdbeCursor.cacheValid that means the cache is always invalid.
100 */
101 #define CACHE_STALE 0
102 
103 /*
104 ** Internally, the vdbe manipulates nearly all SQL values as Mem
105 ** structures. Each Mem struct may cache multiple representations (string,
106 ** integer etc.) of the same value.  A value (and therefore Mem structure)
107 ** has the following properties:
108 **
109 ** Each value has a manifest type. The manifest type of the value stored
110 ** in a Mem struct is returned by the MemType(Mem*) macro. The type is
111 ** one of SQLITE_NULL, SQLITE_INTEGER, SQLITE_REAL, SQLITE_TEXT or
112 ** SQLITE_BLOB.
113 */
114 struct Mem {
115   union {
116     i64 i;              /* Integer value. */
117     int nZero;          /* Used when bit MEM_Zero is set in flags */
118     FuncDef *pDef;      /* Used only when flags==MEM_Agg */
119     RowSet *pRowSet;    /* Used only when flags==MEM_RowSet */
120   } u;
121   double r;           /* Real value */
122   sqlite3 *db;        /* The associated database connection */
123   char *z;            /* String or BLOB value */
124   int n;              /* Number of characters in string value, excluding '\0' */
125   u16 flags;          /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */
126   u8  type;           /* One of SQLITE_NULL, SQLITE_TEXT, SQLITE_INTEGER, etc */
127   u8  enc;            /* SQLITE_UTF8, SQLITE_UTF16BE, SQLITE_UTF16LE */
128   void (*xDel)(void *);  /* If not null, call this function to delete Mem.z */
129   char *zMalloc;      /* Dynamic buffer allocated by sqlite3_malloc() */
130 };
131 
132 /* One or more of the following flags are set to indicate the validOK
133 ** representations of the value stored in the Mem struct.
134 **
135 ** If the MEM_Null flag is set, then the value is an SQL NULL value.
136 ** No other flags may be set in this case.
137 **
138 ** If the MEM_Str flag is set then Mem.z points at a string representation.
139 ** Usually this is encoded in the same unicode encoding as the main
140 ** database (see below for exceptions). If the MEM_Term flag is also
141 ** set, then the string is nul terminated. The MEM_Int and MEM_Real
142 ** flags may coexist with the MEM_Str flag.
143 **
144 ** Multiple of these values can appear in Mem.flags.  But only one
145 ** at a time can appear in Mem.type.
146 */
147 #define MEM_Null      0x0001   /* Value is NULL */
148 #define MEM_Str       0x0002   /* Value is a string */
149 #define MEM_Int       0x0004   /* Value is an integer */
150 #define MEM_Real      0x0008   /* Value is a real number */
151 #define MEM_Blob      0x0010   /* Value is a BLOB */
152 #define MEM_RowSet    0x0020   /* Value is a RowSet object */
153 #define MEM_TypeMask  0x00ff   /* Mask of type bits */
154 
155 /* Whenever Mem contains a valid string or blob representation, one of
156 ** the following flags must be set to determine the memory management
157 ** policy for Mem.z.  The MEM_Term flag tells us whether or not the
158 ** string is \000 or \u0000 terminated
159 */
160 #define MEM_Term      0x0200   /* String rep is nul terminated */
161 #define MEM_Dyn       0x0400   /* Need to call sqliteFree() on Mem.z */
162 #define MEM_Static    0x0800   /* Mem.z points to a static string */
163 #define MEM_Ephem     0x1000   /* Mem.z points to an ephemeral string */
164 #define MEM_Agg       0x2000   /* Mem.z points to an agg function context */
165 #define MEM_Zero      0x4000   /* Mem.i contains count of 0s appended to blob */
166 
167 #ifdef SQLITE_OMIT_INCRBLOB
168   #undef MEM_Zero
169   #define MEM_Zero 0x0000
170 #endif
171 
172 
173 /*
174 ** Clear any existing type flags from a Mem and replace them with f
175 */
176 #define MemSetTypeFlag(p, f) \
177    ((p)->flags = ((p)->flags&~(MEM_TypeMask|MEM_Zero))|f)
178 
179 
180 /* A VdbeFunc is just a FuncDef (defined in sqliteInt.h) that contains
181 ** additional information about auxiliary information bound to arguments
182 ** of the function.  This is used to implement the sqlite3_get_auxdata()
183 ** and sqlite3_set_auxdata() APIs.  The "auxdata" is some auxiliary data
184 ** that can be associated with a constant argument to a function.  This
185 ** allows functions such as "regexp" to compile their constant regular
186 ** expression argument once and reused the compiled code for multiple
187 ** invocations.
188 */
189 struct VdbeFunc {
190   FuncDef *pFunc;               /* The definition of the function */
191   int nAux;                     /* Number of entries allocated for apAux[] */
192   struct AuxData {
193     void *pAux;                   /* Aux data for the i-th argument */
194     void (*xDelete)(void *);      /* Destructor for the aux data */
195   } apAux[1];                   /* One slot for each function argument */
196 };
197 
198 /*
199 ** The "context" argument for a installable function.  A pointer to an
200 ** instance of this structure is the first argument to the routines used
201 ** implement the SQL functions.
202 **
203 ** There is a typedef for this structure in sqlite.h.  So all routines,
204 ** even the public interface to SQLite, can use a pointer to this structure.
205 ** But this file is the only place where the internal details of this
206 ** structure are known.
207 **
208 ** This structure is defined inside of vdbeInt.h because it uses substructures
209 ** (Mem) which are only defined there.
210 */
211 struct sqlite3_context {
212   FuncDef *pFunc;       /* Pointer to function information.  MUST BE FIRST */
213   VdbeFunc *pVdbeFunc;  /* Auxilary data, if created. */
214   Mem s;                /* The return value is stored here */
215   Mem *pMem;            /* Memory cell used to store aggregate context */
216   int isError;          /* Error code returned by the function. */
217   CollSeq *pColl;       /* Collating sequence */
218 };
219 
220 /*
221 ** A Set structure is used for quick testing to see if a value
222 ** is part of a small set.  Sets are used to implement code like
223 ** this:
224 **            x.y IN ('hi','hoo','hum')
225 */
226 typedef struct Set Set;
227 struct Set {
228   Hash hash;             /* A set is just a hash table */
229   HashElem *prev;        /* Previously accessed hash elemen */
230 };
231 
232 /*
233 ** A Context stores the last insert rowid, the last statement change count,
234 ** and the current statement change count (i.e. changes since last statement).
235 ** The current keylist is also stored in the context.
236 ** Elements of Context structure type make up the ContextStack, which is
237 ** updated by the ContextPush and ContextPop opcodes (used by triggers).
238 ** The context is pushed before executing a trigger a popped when the
239 ** trigger finishes.
240 */
241 typedef struct Context Context;
242 struct Context {
243   i64 lastRowid;    /* Last insert rowid (sqlite3.lastRowid) */
244   int nChange;      /* Statement changes (Vdbe.nChanges)     */
245 };
246 
247 /*
248 ** An instance of the virtual machine.  This structure contains the complete
249 ** state of the virtual machine.
250 **
251 ** The "sqlite3_stmt" structure pointer that is returned by sqlite3_compile()
252 ** is really a pointer to an instance of this structure.
253 **
254 ** The Vdbe.inVtabMethod variable is set to non-zero for the duration of
255 ** any virtual table method invocations made by the vdbe program. It is
256 ** set to 2 for xDestroy method calls and 1 for all other methods. This
257 ** variable is used for two purposes: to allow xDestroy methods to execute
258 ** "DROP TABLE" statements and to prevent some nasty side effects of
259 ** malloc failure when SQLite is invoked recursively by a virtual table
260 ** method function.
261 */
262 struct Vdbe {
263   sqlite3 *db;        /* The whole database */
264   Vdbe *pPrev,*pNext; /* Linked list of VDBEs with the same Vdbe.db */
265   int nOp;            /* Number of instructions in the program */
266   int nOpAlloc;       /* Number of slots allocated for aOp[] */
267   Op *aOp;            /* Space to hold the virtual machine's program */
268   int nLabel;         /* Number of labels used */
269   int nLabelAlloc;    /* Number of slots allocated in aLabel[] */
270   int *aLabel;        /* Space to hold the labels */
271   Mem **apArg;        /* Arguments to currently executing user function */
272   Mem *aColName;      /* Column names to return */
273   int nCursor;        /* Number of slots in apCsr[] */
274   VdbeCursor **apCsr; /* One element of this array for each open cursor */
275   int nVar;           /* Number of entries in aVar[] */
276   Mem *aVar;          /* Values for the OP_Variable opcode. */
277   char **azVar;       /* Name of variables */
278   int okVar;          /* True if azVar[] has been initialized */
279   u32 magic;              /* Magic number for sanity checking */
280   int nMem;               /* Number of memory locations currently allocated */
281   Mem *aMem;              /* The memory locations */
282   int cacheCtr;           /* VdbeCursor row cache generation counter */
283   int contextStackTop;    /* Index of top element in the context stack */
284   int contextStackDepth;  /* The size of the "context" stack */
285   Context *contextStack;  /* Stack used by opcodes ContextPush & ContextPop*/
286   int pc;                 /* The program counter */
287   int rc;                 /* Value to return */
288   int errorAction;        /* Recovery action to do in case of an error */
289   int nResColumn;         /* Number of columns in one row of the result set */
290   char **azResColumn;     /* Values for one row of result */
291   char *zErrMsg;          /* Error message written here */
292   Mem *pResultSet;        /* Pointer to an array of results */
293   u8 explain;             /* True if EXPLAIN present on SQL command */
294   u8 changeCntOn;         /* True to update the change-counter */
295   u8 expired;             /* True if the VM needs to be recompiled */
296   u8 minWriteFileFormat;  /* Minimum file format for writable database files */
297   u8 inVtabMethod;        /* See comments above */
298   u8 usesStmtJournal;     /* True if uses a statement journal */
299   u8 readOnly;            /* True for read-only statements */
300   u8 isPrepareV2;         /* True if prepared with prepare_v2() */
301   int nChange;            /* Number of db changes made since last reset */
302   i64 startTime;          /* Time when query started - used for profiling */
303   int btreeMask;          /* Bitmask of db->aDb[] entries referenced */
304   BtreeMutexArray aMutex; /* An array of Btree used here and needing locks */
305   int aCounter[2];        /* Counters used by sqlite3_stmt_status() */
306   char *zSql;           /* Text of the SQL statement that generated this */
307   void *pFree;            /* Free this when deleting the vdbe */
308 #ifdef SQLITE_DEBUG
309   FILE *trace;          /* Write an execution trace here, if not NULL */
310 #endif
311   int openedStatement;  /* True if this VM has opened a statement journal */
312 #ifdef SQLITE_SSE
313   int fetchId;          /* Statement number used by sqlite3_fetch_statement */
314   int lru;              /* Counter used for LRU cache replacement */
315 #endif
316 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
317   Vdbe *pLruPrev;
318   Vdbe *pLruNext;
319 #endif
320 };
321 
322 /*
323 ** The following are allowed values for Vdbe.magic
324 */
325 #define VDBE_MAGIC_INIT     0x26bceaa5    /* Building a VDBE program */
326 #define VDBE_MAGIC_RUN      0xbdf20da3    /* VDBE is ready to execute */
327 #define VDBE_MAGIC_HALT     0x519c2973    /* VDBE has completed execution */
328 #define VDBE_MAGIC_DEAD     0xb606c3c8    /* The VDBE has been deallocated */
329 
330 /*
331 ** Function prototypes
332 */
333 void sqlite3VdbeFreeCursor(Vdbe *, VdbeCursor*);
334 void sqliteVdbePopStack(Vdbe*,int);
335 int sqlite3VdbeCursorMoveto(VdbeCursor*);
336 #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
337 void sqlite3VdbePrintOp(FILE*, int, Op*);
338 #endif
339 int sqlite3VdbeSerialTypeLen(u32);
340 u32 sqlite3VdbeSerialType(Mem*, int);
341 int sqlite3VdbeSerialPut(unsigned char*, int, Mem*, int);
342 int sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*);
343 void sqlite3VdbeDeleteAuxData(VdbeFunc*, int);
344 
345 int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
346 int sqlite3VdbeIdxKeyCompare(VdbeCursor*,UnpackedRecord*,int*);
347 int sqlite3VdbeIdxRowid(BtCursor *, i64 *);
348 int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
349 int sqlite3VdbeExec(Vdbe*);
350 int sqlite3VdbeList(Vdbe*);
351 int sqlite3VdbeHalt(Vdbe*);
352 int sqlite3VdbeChangeEncoding(Mem *, int);
353 int sqlite3VdbeMemTooBig(Mem*);
354 int sqlite3VdbeMemCopy(Mem*, const Mem*);
355 void sqlite3VdbeMemShallowCopy(Mem*, const Mem*, int);
356 void sqlite3VdbeMemMove(Mem*, Mem*);
357 int sqlite3VdbeMemNulTerminate(Mem*);
358 int sqlite3VdbeMemSetStr(Mem*, const char*, int, u8, void(*)(void*));
359 void sqlite3VdbeMemSetInt64(Mem*, i64);
360 void sqlite3VdbeMemSetDouble(Mem*, double);
361 void sqlite3VdbeMemSetNull(Mem*);
362 void sqlite3VdbeMemSetZeroBlob(Mem*,int);
363 void sqlite3VdbeMemSetRowSet(Mem*);
364 int sqlite3VdbeMemMakeWriteable(Mem*);
365 int sqlite3VdbeMemStringify(Mem*, int);
366 i64 sqlite3VdbeIntValue(Mem*);
367 int sqlite3VdbeMemIntegerify(Mem*);
368 double sqlite3VdbeRealValue(Mem*);
369 void sqlite3VdbeIntegerAffinity(Mem*);
370 int sqlite3VdbeMemRealify(Mem*);
371 int sqlite3VdbeMemNumerify(Mem*);
372 int sqlite3VdbeMemFromBtree(BtCursor*,int,int,int,Mem*);
373 void sqlite3VdbeMemRelease(Mem *p);
374 void sqlite3VdbeMemReleaseExternal(Mem *p);
375 int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
376 const char *sqlite3OpcodeName(int);
377 int sqlite3VdbeOpcodeHasProperty(int, int);
378 int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
379 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
380 int sqlite3VdbeReleaseBuffers(Vdbe *p);
381 #endif
382 
383 int sqlite3VdbeMemTranslate(Mem*, u8);
384 #ifdef SQLITE_DEBUG
385   void sqlite3VdbePrintSql(Vdbe*);
386   void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf);
387 #endif
388 int sqlite3VdbeMemHandleBom(Mem *pMem);
389 
390 #ifndef SQLITE_OMIT_INCRBLOB
391   int sqlite3VdbeMemExpandBlob(Mem *);
392 #else
393   #define sqlite3VdbeMemExpandBlob(x) SQLITE_OK
394 #endif
395 
396 #endif /* !defined(_VDBEINT_H_) */
397