blob: 20aebdb4fa46eed6a5e5d67a5ada2c98e95bcae2 [file] [log] [blame]
drh9a324642003-09-06 20:12:01 +00001/*
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
19/*
20** The makefile scans this source file and creates the following
21** array of string constants which are the names of all VDBE opcodes.
22** This array is defined in a separate source code file named opcode.c
23** which is automatically generated by the makefile.
24*/
25extern char *sqliteOpcodeNames[];
26
27/*
28** SQL is translated into a sequence of instructions to be
29** executed by a virtual machine. Each instruction is an instance
30** of the following structure.
31*/
32typedef struct VdbeOp Op;
33
34/*
35** Boolean values
36*/
37typedef unsigned char Bool;
38
39/*
40** A cursor is a pointer into a single BTree within a database file.
41** The cursor can seek to a BTree entry with a particular key, or
42** loop over all entries of the Btree. You can also insert new BTree
43** entries or retrieve the key or data from the entry that the cursor
44** is currently pointing to.
45**
46** Every cursor that the virtual machine has open is represented by an
47** instance of the following structure.
48**
49** If the Cursor.isTriggerRow flag is set it means that this cursor is
50** really a single row that represents the NEW or OLD pseudo-table of
51** a row trigger. The data for the row is stored in Cursor.pData and
52** the rowid is in Cursor.iKey.
53*/
54struct Cursor {
55 BtCursor *pCursor; /* The cursor structure of the backend */
56 int lastRecno; /* Last recno from a Next or NextIdx operation */
57 int nextRowid; /* Next rowid returned by OP_NewRowid */
58 Bool recnoIsValid; /* True if lastRecno is valid */
59 Bool keyAsData; /* The OP_Column command works on key instead of data */
60 Bool atFirst; /* True if pointing to first entry */
61 Bool useRandomRowid; /* Generate new record numbers semi-randomly */
62 Bool nullRow; /* True if pointing to a row with no data */
63 Bool nextRowidValid; /* True if the nextRowid field is valid */
64 Bool pseudoTable; /* This is a NEW or OLD pseudo-tables of a trigger */
65 Btree *pBt; /* Separate file holding temporary table */
66 int nData; /* Number of bytes in pData */
67 char *pData; /* Data for a NEW or OLD pseudo-table */
68 int iKey; /* Key for the NEW or OLD pseudo-table row */
69};
70typedef struct Cursor Cursor;
71
72/*
73** A sorter builds a list of elements to be sorted. Each element of
74** the list is an instance of the following structure.
75*/
76typedef struct Sorter Sorter;
77struct Sorter {
78 int nKey; /* Number of bytes in the key */
79 char *zKey; /* The key by which we will sort */
80 int nData; /* Number of bytes in the data */
81 char *pData; /* The data associated with this key */
82 Sorter *pNext; /* Next in the list */
83};
84
85/*
86** Number of buckets used for merge-sort.
87*/
88#define NSORT 30
89
90/*
91** Number of bytes of string storage space available to each stack
92** layer without having to malloc. NBFS is short for Number of Bytes
93** For Strings.
94*/
95#define NBFS 32
96
97/*
98** A single level of the stack is an instance of the following
99** structure. Except, string values are stored on a separate
100** list of of pointers to character. The reason for storing
101** strings separately is so that they can be easily passed
102** to the callback function.
103*/
104struct Stack {
105 int i; /* Integer value */
106 int n; /* Number of characters in string value, including '\0' */
107 int flags; /* Some combination of STK_Null, STK_Str, STK_Dyn, etc. */
108 double r; /* Real value */
109 char z[NBFS]; /* Space for short strings */
110};
111typedef struct Stack Stack;
112
113/*
114** Memory cells use the same structure as the stack except that space
115** for an arbitrary string is added.
116*/
117struct Mem {
118 Stack s; /* All values of the memory cell besides string */
119 char *z; /* String value for this memory cell */
120};
121typedef struct Mem Mem;
122
123/*
124** Allowed values for Stack.flags
125*/
126#define STK_Null 0x0001 /* Value is NULL */
127#define STK_Str 0x0002 /* Value is a string */
128#define STK_Int 0x0004 /* Value is an integer */
129#define STK_Real 0x0008 /* Value is a real number */
130#define STK_Dyn 0x0010 /* Need to call sqliteFree() on zStack[] */
131#define STK_Static 0x0020 /* zStack[] points to a static string */
132#define STK_Ephem 0x0040 /* zStack[] points to an ephemeral string */
133
134/* The following STK_ value appears only in AggElem.aMem.s.flag fields.
135** It indicates that the corresponding AggElem.aMem.z points to a
136** aggregate function context that needs to be finalized.
137*/
138#define STK_AggCtx 0x0040 /* zStack[] points to an agg function context */
139
140/*
141** The "context" argument for a installable function. A pointer to an
142** instance of this structure is the first argument to the routines used
143** implement the SQL functions.
144**
145** There is a typedef for this structure in sqlite.h. So all routines,
146** even the public interface to SQLite, can use a pointer to this structure.
147** But this file is the only place where the internal details of this
148** structure are known.
149**
150** This structure is defined inside of vdbe.c because it uses substructures
151** (Stack) which are only defined there.
152*/
153struct sqlite_func {
154 FuncDef *pFunc; /* Pointer to function information. MUST BE FIRST */
155 Stack s; /* Small strings, ints, and double values go here */
156 char *z; /* Space for holding dynamic string results */
157 void *pAgg; /* Aggregate context */
158 u8 isError; /* Set to true for an error */
159 u8 isStep; /* Current in the step function */
160 int cnt; /* Number of times that the step function has been called */
161};
162
163/*
164** An Agg structure describes an Aggregator. Each Agg consists of
165** zero or more Aggregator elements (AggElem). Each AggElem contains
166** a key and one or more values. The values are used in processing
167** aggregate functions in a SELECT. The key is used to implement
168** the GROUP BY clause of a select.
169*/
170typedef struct Agg Agg;
171typedef struct AggElem AggElem;
172struct Agg {
173 int nMem; /* Number of values stored in each AggElem */
174 AggElem *pCurrent; /* The AggElem currently in focus */
175 HashElem *pSearch; /* The hash element for pCurrent */
176 Hash hash; /* Hash table of all aggregate elements */
177 FuncDef **apFunc; /* Information about aggregate functions */
178};
179struct AggElem {
180 char *zKey; /* The key to this AggElem */
181 int nKey; /* Number of bytes in the key, including '\0' at end */
182 Mem aMem[1]; /* The values for this AggElem */
183};
184
185/*
186** A Set structure is used for quick testing to see if a value
187** is part of a small set. Sets are used to implement code like
188** this:
189** x.y IN ('hi','hoo','hum')
190*/
191typedef struct Set Set;
192struct Set {
193 Hash hash; /* A set is just a hash table */
194 HashElem *prev; /* Previously accessed hash elemen */
195};
196
197/*
198** A Keylist is a bunch of keys into a table. The keylist can
199** grow without bound. The keylist stores the ROWIDs of database
200** records that need to be deleted or updated.
201*/
202typedef struct Keylist Keylist;
203struct Keylist {
204 int nKey; /* Number of slots in aKey[] */
205 int nUsed; /* Next unwritten slot in aKey[] */
206 int nRead; /* Next unread slot in aKey[] */
207 Keylist *pNext; /* Next block of keys */
208 int aKey[1]; /* One or more keys. Extra space allocated as needed */
209};
210
211/*
212** An instance of the virtual machine. This structure contains the complete
213** state of the virtual machine.
214**
215** The "sqlite_vm" structure pointer that is returned by sqlite_compile()
216** is really a pointer to an instance of this structure.
217*/
218struct Vdbe {
219 sqlite *db; /* The whole database */
220 Vdbe *pPrev,*pNext; /* Linked list of VDBEs with the same Vdbe.db */
221 FILE *trace; /* Write an execution trace here, if not NULL */
222 int nOp; /* Number of instructions in the program */
223 int nOpAlloc; /* Number of slots allocated for aOp[] */
224 Op *aOp; /* Space to hold the virtual machine's program */
225 int nLabel; /* Number of labels used */
226 int nLabelAlloc; /* Number of slots allocated in aLabel[] */
227 int *aLabel; /* Space to hold the labels */
228 int tos; /* Index of top of stack */
229 Stack *aStack; /* The operand stack, except string values */
230 char **zStack; /* Text or binary values of the stack */
231 char **azColName; /* Becomes the 4th parameter to callbacks */
232 int nCursor; /* Number of slots in aCsr[] */
233 Cursor *aCsr; /* One element of this array for each open cursor */
234 Sorter *pSort; /* A linked list of objects to be sorted */
235 FILE *pFile; /* At most one open file handler */
236 int nField; /* Number of file fields */
237 char **azField; /* Data for each file field */
238 int nVariable; /* Number of entries in azVariable[] */
239 char **azVariable; /* Values for the OP_Variable opcode */
240 char *zLine; /* A single line from the input file */
241 int nLineAlloc; /* Number of spaces allocated for zLine */
242 int magic; /* Magic number for sanity checking */
243 int nMem; /* Number of memory locations currently allocated */
244 Mem *aMem; /* The memory locations */
245 Agg agg; /* Aggregate information */
246 int nSet; /* Number of sets allocated */
247 Set *aSet; /* An array of sets */
248 int nCallback; /* Number of callbacks invoked so far */
249 Keylist *pList; /* A list of ROWIDs */
250 int keylistStackDepth; /* The size of the "keylist" stack */
251 Keylist **keylistStack; /* The stack used by opcodes ListPush & ListPop */
252 int pc; /* The program counter */
253 int rc; /* Value to return */
254 unsigned uniqueCnt; /* Used by OP_MakeRecord when P2!=0 */
255 int errorAction; /* Recovery action to do in case of an error */
256 int undoTransOnError; /* If error, either ROLLBACK or COMMIT */
257 int inTempTrans; /* True if temp database is transactioned */
258 int returnStack[100]; /* Return address stack for OP_Gosub & OP_Return */
259 int returnDepth; /* Next unused element in returnStack[] */
260 int nResColumn; /* Number of columns in one row of the result set */
261 char **azResColumn; /* Values for one row of result */
262 int (*xCallback)(void*,int,char**,char**); /* Callback for SELECT results */
263 void *pCbArg; /* First argument to xCallback() */
264 int popStack; /* Pop the stack this much on entry to VdbeExec() */
265 char *zErrMsg; /* Error message written here */
266 u8 explain; /* True if EXPLAIN present on SQL command */
267};
268
269/*
270** The following are allowed values for Vdbe.magic
271*/
272#define VDBE_MAGIC_INIT 0x26bceaa5 /* Building a VDBE program */
273#define VDBE_MAGIC_RUN 0xbdf20da3 /* VDBE is ready to execute */
274#define VDBE_MAGIC_HALT 0x519c2973 /* VDBE has completed execution */
275#define VDBE_MAGIC_DEAD 0xb606c3c8 /* The VDBE has been deallocated */
276
277/*
278** Here is a macro to handle the common case of popping the stack
279** once. This macro only works from within the sqliteVdbeExec()
280** function.
281*/
282#define POPSTACK \
283 assert(p->tos>=0); \
284 if( aStack[p->tos].flags & STK_Dyn ) sqliteFree(zStack[p->tos]); \
285 p->tos--;
286
287/*
288** Function prototypes
289*/
290void sqliteVdbeCleanupCursor(Cursor*);
291void sqliteVdbeSorterReset(Vdbe*);
292void sqliteVdbeAggReset(Agg*);
293void sqliteVdbeKeylistFree(Keylist*);
294void sqliteVdbePopStack(Vdbe*,int);
295#if !defined(NDEBUG) || defined(VDBE_PROFILE)
296void sqliteVdbePrintOp(FILE*, int, Op*);
297#endif