vdbeInt.h 18 KB

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