You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

303 lines
13 KiB

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