aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/sqlite/unix/sqlite-3.5.1/src/dump.txt
diff options
context:
space:
mode:
Diffstat (limited to 'libraries/sqlite/unix/sqlite-3.5.1/src/dump.txt')
-rw-r--r--libraries/sqlite/unix/sqlite-3.5.1/src/dump.txt2267
1 files changed, 2267 insertions, 0 deletions
diff --git a/libraries/sqlite/unix/sqlite-3.5.1/src/dump.txt b/libraries/sqlite/unix/sqlite-3.5.1/src/dump.txt
new file mode 100644
index 0000000..55560ef
--- /dev/null
+++ b/libraries/sqlite/unix/sqlite-3.5.1/src/dump.txt
@@ -0,0 +1,2267 @@
11.1 (drh 06-Sep-03): /*
21.1 (drh 06-Sep-03): ** 2003 September 6
31.1 (drh 06-Sep-03): **
41.1 (drh 06-Sep-03): ** The author disclaims copyright to this source code. In place of
51.1 (drh 06-Sep-03): ** a legal notice, here is a blessing:
61.1 (drh 06-Sep-03): **
71.1 (drh 06-Sep-03): ** May you do good and not evil.
81.1 (drh 06-Sep-03): ** May you find forgiveness for yourself and forgive others.
91.1 (drh 06-Sep-03): ** May you share freely, never taking more than you give.
101.1 (drh 06-Sep-03): **
111.1 (drh 06-Sep-03): *************************************************************************
121.1 (drh 06-Sep-03): ** This file contains code used for creating, destroying, and populating
131.65 (danielk1 26-May-04): ** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.) Prior
141.1 (drh 06-Sep-03): ** to version 2.8.7, all this code was combined into the vdbe.c source file.
151.1 (drh 06-Sep-03): ** But that file was getting too big so this subroutines were split out.
161.1 (drh 06-Sep-03): */
171.1 (drh 06-Sep-03): #include "sqliteInt.h"
181.1 (drh 06-Sep-03): #include <ctype.h>
191.1 (drh 06-Sep-03): #include "vdbeInt.h"
201.1 (drh 06-Sep-03):
211.1 (drh 06-Sep-03):
221.311 (drh 27-Aug-07):
231.1 (drh 06-Sep-03): /*
241.1 (drh 06-Sep-03): ** When debugging the code generator in a symbolic debugger, one can
251.24 (danielk1 10-May-04): ** set the sqlite3_vdbe_addop_trace to 1 and all opcodes will be printed
261.1 (drh 06-Sep-03): ** as they are added to the instruction stream.
271.1 (drh 06-Sep-03): */
281.182 (drh 14-Jun-05): #ifdef SQLITE_DEBUG
291.24 (danielk1 10-May-04): int sqlite3_vdbe_addop_trace = 0;
301.1 (drh 06-Sep-03): #endif
311.1 (drh 06-Sep-03):
321.1 (drh 06-Sep-03):
331.1 (drh 06-Sep-03): /*
341.1 (drh 06-Sep-03): ** Create a new virtual database engine.
351.1 (drh 06-Sep-03): */
361.140 (drh 06-Sep-04): Vdbe *sqlite3VdbeCreate(sqlite3 *db){
371.1 (drh 06-Sep-03): Vdbe *p;
381.299 (drh 16-Aug-07): p = sqlite3DbMallocZero(db, sizeof(Vdbe) );
391.1 (drh 06-Sep-03): if( p==0 ) return 0;
401.1 (drh 06-Sep-03): p->db = db;
411.1 (drh 06-Sep-03): if( db->pVdbe ){
421.1 (drh 06-Sep-03): db->pVdbe->pPrev = p;
431.1 (drh 06-Sep-03): }
441.1 (drh 06-Sep-03): p->pNext = db->pVdbe;
451.1 (drh 06-Sep-03): p->pPrev = 0;
461.1 (drh 06-Sep-03): db->pVdbe = p;
471.1 (drh 06-Sep-03): p->magic = VDBE_MAGIC_INIT;
481.1 (drh 06-Sep-03): return p;
491.1 (drh 06-Sep-03): }
501.1 (drh 06-Sep-03):
511.1 (drh 06-Sep-03): /*
521.268 (drh 09-Nov-06): ** Remember the SQL string for a prepared statement.
531.268 (drh 09-Nov-06): */
541.268 (drh 09-Nov-06): void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n){
551.268 (drh 09-Nov-06): if( p==0 ) return;
561.268 (drh 09-Nov-06): assert( p->zSql==0 );
571.299 (drh 16-Aug-07): p->zSql = sqlite3DbStrNDup(p->db, z, n);
581.268 (drh 09-Nov-06): }
591.268 (drh 09-Nov-06):
601.268 (drh 09-Nov-06): /*
611.268 (drh 09-Nov-06): ** Return the SQL associated with a prepared statement
621.268 (drh 09-Nov-06): */
631.268 (drh 09-Nov-06): const char *sqlite3VdbeGetSql(Vdbe *p){
641.268 (drh 09-Nov-06): return p->zSql;
651.268 (drh 09-Nov-06): }
661.268 (drh 09-Nov-06):
671.268 (drh 09-Nov-06): /*
681.269 (drh 08-Jan-07): ** Swap all content between two VDBE structures.
691.268 (drh 09-Nov-06): */
701.269 (drh 08-Jan-07): void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){
711.269 (drh 08-Jan-07): Vdbe tmp, *pTmp;
721.269 (drh 08-Jan-07): char *zTmp;
731.269 (drh 08-Jan-07): int nTmp;
741.269 (drh 08-Jan-07): tmp = *pA;
751.269 (drh 08-Jan-07): *pA = *pB;
761.269 (drh 08-Jan-07): *pB = tmp;
771.269 (drh 08-Jan-07): pTmp = pA->pNext;
781.269 (drh 08-Jan-07): pA->pNext = pB->pNext;
791.269 (drh 08-Jan-07): pB->pNext = pTmp;
801.269 (drh 08-Jan-07): pTmp = pA->pPrev;
811.269 (drh 08-Jan-07): pA->pPrev = pB->pPrev;
821.269 (drh 08-Jan-07): pB->pPrev = pTmp;
831.269 (drh 08-Jan-07): zTmp = pA->zSql;
841.269 (drh 08-Jan-07): pA->zSql = pB->zSql;
851.269 (drh 08-Jan-07): pB->zSql = zTmp;
861.269 (drh 08-Jan-07): nTmp = pA->nSql;
871.269 (drh 08-Jan-07): pA->nSql = pB->nSql;
881.269 (drh 08-Jan-07): pB->nSql = nTmp;
891.268 (drh 09-Nov-06): }
901.268 (drh 09-Nov-06):
911.287 (drh 08-May-07): #ifdef SQLITE_DEBUG
921.268 (drh 09-Nov-06): /*
931.1 (drh 06-Sep-03): ** Turn tracing on or off
941.1 (drh 06-Sep-03): */
951.19 (danielk1 08-May-04): void sqlite3VdbeTrace(Vdbe *p, FILE *trace){
961.1 (drh 06-Sep-03): p->trace = trace;
971.1 (drh 06-Sep-03): }
981.287 (drh 08-May-07): #endif
991.1 (drh 06-Sep-03):
1001.1 (drh 06-Sep-03): /*
1011.146 (drh 24-Sep-04): ** Resize the Vdbe.aOp array so that it contains at least N
1021.170 (danielk1 28-Mar-05): ** elements. If the Vdbe is in VDBE_MAGIC_RUN state, then
1031.235 (danielk1 26-Jan-06): ** the Vdbe.aOp array will be sized to contain exactly N
1041.235 (danielk1 26-Jan-06): ** elements. Vdbe.nOpAlloc is set to reflect the new size of
1051.235 (danielk1 26-Jan-06): ** the array.
1061.235 (danielk1 26-Jan-06): **
1071.235 (danielk1 26-Jan-06): ** If an out-of-memory error occurs while resizing the array,
1081.235 (danielk1 26-Jan-06): ** Vdbe.aOp and Vdbe.nOpAlloc remain unchanged (this is so that
1091.235 (danielk1 26-Jan-06): ** any opcodes already allocated can be correctly deallocated
1101.235 (danielk1 26-Jan-06): ** along with the rest of the Vdbe).
1111.146 (drh 24-Sep-04): */
1121.146 (drh 24-Sep-04): static void resizeOpArray(Vdbe *p, int N){
1131.199 (drh 16-Sep-05): int runMode = p->magic==VDBE_MAGIC_RUN;
1141.199 (drh 16-Sep-05): if( runMode || p->nOpAlloc<N ){
1151.199 (drh 16-Sep-05): VdbeOp *pNew;
1161.199 (drh 16-Sep-05): int nNew = N + 100*(!runMode);
1171.146 (drh 24-Sep-04): int oldSize = p->nOpAlloc;
1181.315 (danielk1 29-Aug-07): pNew = sqlite3DbRealloc(p->db, p->aOp, nNew*sizeof(Op));
1191.198 (drh 16-Sep-05): if( pNew ){
1201.199 (drh 16-Sep-05): p->nOpAlloc = nNew;
1211.198 (drh 16-Sep-05): p->aOp = pNew;
1221.199 (drh 16-Sep-05): if( nNew>oldSize ){
1231.199 (drh 16-Sep-05): memset(&p->aOp[oldSize], 0, (nNew-oldSize)*sizeof(Op));
1241.199 (drh 16-Sep-05): }
1251.146 (drh 24-Sep-04): }
1261.146 (drh 24-Sep-04): }
1271.146 (drh 24-Sep-04): }
1281.146 (drh 24-Sep-04):
1291.146 (drh 24-Sep-04): /*
1301.1 (drh 06-Sep-03): ** Add a new instruction to the list of instructions current in the
1311.1 (drh 06-Sep-03): ** VDBE. Return the address of the new instruction.
1321.1 (drh 06-Sep-03): **
1331.1 (drh 06-Sep-03): ** Parameters:
1341.1 (drh 06-Sep-03): **
1351.1 (drh 06-Sep-03): ** p Pointer to the VDBE
1361.1 (drh 06-Sep-03): **
1371.1 (drh 06-Sep-03): ** op The opcode for this instruction
1381.1 (drh 06-Sep-03): **
1391.1 (drh 06-Sep-03): ** p1, p2 First two of the three possible operands.
1401.1 (drh 06-Sep-03): **
1411.19 (danielk1 08-May-04): ** Use the sqlite3VdbeResolveLabel() function to fix an address and
1421.19 (danielk1 08-May-04): ** the sqlite3VdbeChangeP3() function to change the value of the P3
1431.1 (drh 06-Sep-03): ** operand.
1441.1 (drh 06-Sep-03): */
1451.19 (danielk1 08-May-04): int sqlite3VdbeAddOp(Vdbe *p, int op, int p1, int p2){
1461.1 (drh 06-Sep-03): int i;
1471.18 (drh 22-Feb-04): VdbeOp *pOp;
1481.1 (drh 06-Sep-03):
1491.1 (drh 06-Sep-03): i = p->nOp;
1501.1 (drh 06-Sep-03): assert( p->magic==VDBE_MAGIC_INIT );
1511.241 (drh 15-Mar-06): if( p->nOpAlloc<=i ){
1521.241 (drh 15-Mar-06): resizeOpArray(p, i+1);
1531.299 (drh 16-Aug-07): if( p->db->mallocFailed ){
1541.241 (drh 15-Mar-06): return 0;
1551.241 (drh 15-Mar-06): }
1561.1 (drh 06-Sep-03): }
1571.282 (danielk1 18-Apr-07): p->nOp++;
1581.18 (drh 22-Feb-04): pOp = &p->aOp[i];
1591.18 (drh 22-Feb-04): pOp->opcode = op;
1601.18 (drh 22-Feb-04): pOp->p1 = p1;
1611.18 (drh 22-Feb-04): pOp->p2 = p2;
1621.18 (drh 22-Feb-04): pOp->p3 = 0;
1631.18 (drh 22-Feb-04): pOp->p3type = P3_NOTUSED;
1641.185 (drh 14-Aug-05): p->expired = 0;
1651.156 (danielk1 12-Jan-05): #ifdef SQLITE_DEBUG
1661.24 (danielk1 10-May-04): if( sqlite3_vdbe_addop_trace ) sqlite3VdbePrintOp(0, i, &p->aOp[i]);
1671.1 (drh 06-Sep-03): #endif
1681.1 (drh 06-Sep-03): return i;
1691.1 (drh 06-Sep-03): }
1701.18 (drh 22-Feb-04):
1711.18 (drh 22-Feb-04): /*
1721.18 (drh 22-Feb-04): ** Add an opcode that includes the p3 value.
1731.18 (drh 22-Feb-04): */
1741.69 (drh 27-May-04): int sqlite3VdbeOp3(Vdbe *p, int op, int p1, int p2, const char *zP3,int p3type){
1751.19 (danielk1 08-May-04): int addr = sqlite3VdbeAddOp(p, op, p1, p2);
1761.19 (danielk1 08-May-04): sqlite3VdbeChangeP3(p, addr, zP3, p3type);
1771.18 (drh 22-Feb-04): return addr;
1781.18 (drh 22-Feb-04): }
1791.18 (drh 22-Feb-04):
1801.18 (drh 22-Feb-04): /*
1811.1 (drh 06-Sep-03): ** Create a new symbolic label for an instruction that has yet to be
1821.1 (drh 06-Sep-03): ** coded. The symbolic label is really just a negative number. The
1831.1 (drh 06-Sep-03): ** label can be used as the P2 value of an operation. Later, when
1841.1 (drh 06-Sep-03): ** the label is resolved to a specific address, the VDBE will scan
1851.1 (drh 06-Sep-03): ** through its operation list and change all values of P2 which match
1861.1 (drh 06-Sep-03): ** the label into the resolved address.
1871.1 (drh 06-Sep-03): **
1881.1 (drh 06-Sep-03): ** The VDBE knows that a P2 value is a label because labels are
1891.1 (drh 06-Sep-03): ** always negative and P2 values are suppose to be non-negative.
1901.1 (drh 06-Sep-03): ** Hence, a negative P2 value is a label that has yet to be resolved.
1911.129 (danielk1 26-Jun-04): **
1921.129 (danielk1 26-Jun-04): ** Zero is returned if a malloc() fails.
1931.1 (drh 06-Sep-03): */
1941.19 (danielk1 08-May-04): int sqlite3VdbeMakeLabel(Vdbe *p){
1951.1 (drh 06-Sep-03): int i;
1961.1 (drh 06-Sep-03): i = p->nLabel++;
1971.1 (drh 06-Sep-03): assert( p->magic==VDBE_MAGIC_INIT );
1981.1 (drh 06-Sep-03): if( i>=p->nLabelAlloc ){
1991.1 (drh 06-Sep-03): p->nLabelAlloc = p->nLabelAlloc*2 + 10;
2001.300 (danielk1 16-Aug-07): p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel,
2011.273 (drh 27-Mar-07): p->nLabelAlloc*sizeof(p->aLabel[0]));
2021.146 (drh 24-Sep-04): }
2031.146 (drh 24-Sep-04): if( p->aLabel ){
2041.146 (drh 24-Sep-04): p->aLabel[i] = -1;
2051.1 (drh 06-Sep-03): }
2061.1 (drh 06-Sep-03): return -1-i;
2071.1 (drh 06-Sep-03): }
2081.1 (drh 06-Sep-03):
2091.1 (drh 06-Sep-03): /*
2101.1 (drh 06-Sep-03): ** Resolve label "x" to be the address of the next instruction to
2111.1 (drh 06-Sep-03): ** be inserted. The parameter "x" must have been obtained from
2121.19 (danielk1 08-May-04): ** a prior call to sqlite3VdbeMakeLabel().
2131.1 (drh 06-Sep-03): */
2141.19 (danielk1 08-May-04): void sqlite3VdbeResolveLabel(Vdbe *p, int x){
2151.146 (drh 24-Sep-04): int j = -1-x;
2161.1 (drh 06-Sep-03): assert( p->magic==VDBE_MAGIC_INIT );
2171.146 (drh 24-Sep-04): assert( j>=0 && j<p->nLabel );
2181.146 (drh 24-Sep-04): if( p->aLabel ){
2191.146 (drh 24-Sep-04): p->aLabel[j] = p->nOp;
2201.146 (drh 24-Sep-04): }
2211.146 (drh 24-Sep-04): }
2221.146 (drh 24-Sep-04):
2231.146 (drh 24-Sep-04): /*
2241.171 (danielk1 29-Mar-05): ** Return non-zero if opcode 'op' is guarenteed not to push more values
2251.171 (danielk1 29-Mar-05): ** onto the VDBE stack than it pops off.
2261.171 (danielk1 29-Mar-05): */
2271.172 (danielk1 29-Mar-05): static int opcodeNoPush(u8 op){
2281.172 (danielk1 29-Mar-05): /* The 10 NOPUSH_MASK_n constants are defined in the automatically
2291.171 (danielk1 29-Mar-05): ** generated header file opcodes.h. Each is a 16-bit bitmask, one
2301.171 (danielk1 29-Mar-05): ** bit corresponding to each opcode implemented by the virtual
2311.172 (danielk1 29-Mar-05): ** machine in vdbe.c. The bit is true if the word "no-push" appears
2321.171 (danielk1 29-Mar-05): ** in a comment on the same line as the "case OP_XXX:" in
2331.171 (danielk1 29-Mar-05): ** sqlite3VdbeExec() in vdbe.c.
2341.171 (danielk1 29-Mar-05): **
2351.171 (danielk1 29-Mar-05): ** If the bit is true, then the corresponding opcode is guarenteed not
2361.171 (danielk1 29-Mar-05): ** to grow the stack when it is executed. Otherwise, it may grow the
2371.171 (danielk1 29-Mar-05): ** stack by at most one entry.
2381.171 (danielk1 29-Mar-05): **
2391.172 (danielk1 29-Mar-05): ** NOPUSH_MASK_0 corresponds to opcodes 0 to 15. NOPUSH_MASK_1 contains
2401.171 (danielk1 29-Mar-05): ** one bit for opcodes 16 to 31, and so on.
2411.171 (danielk1 29-Mar-05): **
2421.171 (danielk1 29-Mar-05): ** 16-bit bitmasks (rather than 32-bit) are specified in opcodes.h
2431.171 (danielk1 29-Mar-05): ** because the file is generated by an awk program. Awk manipulates
2441.171 (danielk1 29-Mar-05): ** all numbers as floating-point and we don't want to risk a rounding
2451.171 (danielk1 29-Mar-05): ** error if someone builds with an awk that uses (for example) 32-bit
2461.171 (danielk1 29-Mar-05): ** IEEE floats.
2471.171 (danielk1 29-Mar-05): */
2481.173 (drh 31-Mar-05): static const u32 masks[5] = {
2491.238 (drh 24-Feb-06): NOPUSH_MASK_0 + (((unsigned)NOPUSH_MASK_1)<<16),
2501.238 (drh 24-Feb-06): NOPUSH_MASK_2 + (((unsigned)NOPUSH_MASK_3)<<16),
2511.238 (drh 24-Feb-06): NOPUSH_MASK_4 + (((unsigned)NOPUSH_MASK_5)<<16),
2521.238 (drh 24-Feb-06): NOPUSH_MASK_6 + (((unsigned)NOPUSH_MASK_7)<<16),
2531.238 (drh 24-Feb-06): NOPUSH_MASK_8 + (((unsigned)NOPUSH_MASK_9)<<16)
2541.171 (danielk1 29-Mar-05): };
2551.206 (drh 29-Nov-05): assert( op<32*5 );
2561.171 (danielk1 29-Mar-05): return (masks[op>>5] & (1<<(op&0x1F)));
2571.171 (danielk1 29-Mar-05): }
2581.171 (danielk1 29-Mar-05):
2591.171 (danielk1 29-Mar-05): #ifndef NDEBUG
2601.172 (danielk1 29-Mar-05): int sqlite3VdbeOpcodeNoPush(u8 op){
2611.172 (danielk1 29-Mar-05): return opcodeNoPush(op);
2621.171 (danielk1 29-Mar-05): }
2631.171 (danielk1 29-Mar-05): #endif
2641.171 (danielk1 29-Mar-05):
2651.171 (danielk1 29-Mar-05): /*
2661.146 (drh 24-Sep-04): ** Loop through the program looking for P2 values that are negative.
2671.146 (drh 24-Sep-04): ** Each such value is a label. Resolve the label by setting the P2
2681.146 (drh 24-Sep-04): ** value to its correct non-zero value.
2691.146 (drh 24-Sep-04): **
2701.146 (drh 24-Sep-04): ** This routine is called once after all opcodes have been inserted.
2711.170 (danielk1 28-Mar-05): **
2721.195 (drh 07-Sep-05): ** Variable *pMaxFuncArgs is set to the maximum value of any P2 argument
2731.247 (danielk1 14-Jun-06): ** to an OP_Function, OP_AggStep or OP_VFilter opcode. This is used by
2741.170 (danielk1 28-Mar-05): ** sqlite3VdbeMakeReady() to size the Vdbe.apArg[] array.
2751.171 (danielk1 29-Mar-05): **
2761.171 (danielk1 29-Mar-05): ** The integer *pMaxStack is set to the maximum number of vdbe stack
2771.171 (danielk1 29-Mar-05): ** entries that static analysis reveals this program might need.
2781.180 (drh 07-Jun-05): **
2791.180 (drh 07-Jun-05): ** This routine also does the following optimization: It scans for
2801.180 (drh 07-Jun-05): ** Halt instructions where P1==SQLITE_CONSTRAINT or P2==OE_Abort or for
2811.181 (drh 12-Jun-05): ** IdxInsert instructions where P2!=0. If no such instruction is
2821.180 (drh 07-Jun-05): ** found, then every Statement instruction is changed to a Noop. In
2831.180 (drh 07-Jun-05): ** this way, we avoid creating the statement journal file unnecessarily.
2841.146 (drh 24-Sep-04): */
2851.171 (danielk1 29-Mar-05): static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs, int *pMaxStack){
2861.146 (drh 24-Sep-04): int i;
2871.171 (danielk1 29-Mar-05): int nMaxArgs = 0;
2881.171 (danielk1 29-Mar-05): int nMaxStack = p->nOp;
2891.146 (drh 24-Sep-04): Op *pOp;
2901.146 (drh 24-Sep-04): int *aLabel = p->aLabel;
2911.180 (drh 07-Jun-05): int doesStatementRollback = 0;
2921.180 (drh 07-Jun-05): int hasStatementBegin = 0;
2931.146 (drh 24-Sep-04): for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
2941.170 (danielk1 28-Mar-05): u8 opcode = pOp->opcode;
2951.170 (danielk1 28-Mar-05):
2961.251 (danielk1 20-Jun-06): if( opcode==OP_Function || opcode==OP_AggStep
2971.247 (danielk1 14-Jun-06): #ifndef SQLITE_OMIT_VIRTUALTABLE
2981.251 (danielk1 20-Jun-06): || opcode==OP_VUpdate
2991.247 (danielk1 14-Jun-06): #endif
3001.247 (danielk1 14-Jun-06): ){
3011.171 (danielk1 29-Mar-05): if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
3021.294 (danielk1 27-Jun-07): }
3031.294 (danielk1 27-Jun-07): if( opcode==OP_Halt ){
3041.180 (drh 07-Jun-05): if( pOp->p1==SQLITE_CONSTRAINT && pOp->p2==OE_Abort ){
3051.180 (drh 07-Jun-05): doesStatementRollback = 1;
3061.180 (drh 07-Jun-05): }
3071.180 (drh 07-Jun-05): }else if( opcode==OP_Statement ){
3081.180 (drh 07-Jun-05): hasStatementBegin = 1;
3091.294 (danielk1 27-Jun-07): #ifndef SQLITE_OMIT_VIRTUALTABLE
3101.294 (danielk1 27-Jun-07): }else if( opcode==OP_VUpdate || opcode==OP_VRename ){
3111.294 (danielk1 27-Jun-07): doesStatementRollback = 1;
3121.246 (drh 13-Jun-06): }else if( opcode==OP_VFilter ){
3131.246 (drh 13-Jun-06): int n;
3141.246 (drh 13-Jun-06): assert( p->nOp - i >= 3 );
3151.246 (drh 13-Jun-06): assert( pOp[-2].opcode==OP_Integer );
3161.246 (drh 13-Jun-06): n = pOp[-2].p1;
3171.246 (drh 13-Jun-06): if( n>nMaxArgs ) nMaxArgs = n;
3181.294 (danielk1 27-Jun-07): #endif
3191.171 (danielk1 29-Mar-05): }
3201.172 (danielk1 29-Mar-05): if( opcodeNoPush(opcode) ){
3211.171 (danielk1 29-Mar-05): nMaxStack--;
3221.170 (danielk1 28-Mar-05): }
3231.170 (danielk1 28-Mar-05):
3241.146 (drh 24-Sep-04): if( pOp->p2>=0 ) continue;
3251.146 (drh 24-Sep-04): assert( -1-pOp->p2<p->nLabel );
3261.146 (drh 24-Sep-04): pOp->p2 = aLabel[-1-pOp->p2];
3271.1 (drh 06-Sep-03): }
3281.299 (drh 16-Aug-07): sqlite3_free(p->aLabel);
3291.146 (drh 24-Sep-04): p->aLabel = 0;
3301.171 (danielk1 29-Mar-05):
3311.171 (danielk1 29-Mar-05): *pMaxFuncArgs = nMaxArgs;
3321.171 (danielk1 29-Mar-05): *pMaxStack = nMaxStack;
3331.180 (drh 07-Jun-05):
3341.180 (drh 07-Jun-05): /* If we never rollback a statement transaction, then statement
3351.180 (drh 07-Jun-05): ** transactions are not needed. So change every OP_Statement
3361.218 (drh 06-Jan-06): ** opcode into an OP_Noop. This avoid a call to sqlite3OsOpenExclusive()
3371.180 (drh 07-Jun-05): ** which can be expensive on some platforms.
3381.180 (drh 07-Jun-05): */
3391.180 (drh 07-Jun-05): if( hasStatementBegin && !doesStatementRollback ){
3401.180 (drh 07-Jun-05): for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
3411.180 (drh 07-Jun-05): if( pOp->opcode==OP_Statement ){
3421.180 (drh 07-Jun-05): pOp->opcode = OP_Noop;
3431.180 (drh 07-Jun-05): }
3441.180 (drh 07-Jun-05): }
3451.180 (drh 07-Jun-05): }
3461.1 (drh 06-Sep-03): }
3471.1 (drh 06-Sep-03):
3481.1 (drh 06-Sep-03): /*
3491.1 (drh 06-Sep-03): ** Return the address of the next instruction to be inserted.
3501.1 (drh 06-Sep-03): */
3511.19 (danielk1 08-May-04): int sqlite3VdbeCurrentAddr(Vdbe *p){
3521.1 (drh 06-Sep-03): assert( p->magic==VDBE_MAGIC_INIT );
3531.1 (drh 06-Sep-03): return p->nOp;
3541.1 (drh 06-Sep-03): }
3551.1 (drh 06-Sep-03):
3561.1 (drh 06-Sep-03): /*
3571.1 (drh 06-Sep-03): ** Add a whole list of operations to the operation stack. Return the
3581.1 (drh 06-Sep-03): ** address of the first operation added.
3591.1 (drh 06-Sep-03): */
3601.19 (danielk1 08-May-04): int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){
3611.1 (drh 06-Sep-03): int addr;
3621.1 (drh 06-Sep-03): assert( p->magic==VDBE_MAGIC_INIT );
3631.146 (drh 24-Sep-04): resizeOpArray(p, p->nOp + nOp);
3641.299 (drh 16-Aug-07): if( p->db->mallocFailed ){
3651.146 (drh 24-Sep-04): return 0;
3661.1 (drh 06-Sep-03): }
3671.1 (drh 06-Sep-03): addr = p->nOp;
3681.1 (drh 06-Sep-03): if( nOp>0 ){
3691.1 (drh 06-Sep-03): int i;
3701.16 (drh 21-Feb-04): VdbeOpList const *pIn = aOp;
3711.16 (drh 21-Feb-04): for(i=0; i<nOp; i++, pIn++){
3721.16 (drh 21-Feb-04): int p2 = pIn->p2;
3731.16 (drh 21-Feb-04): VdbeOp *pOut = &p->aOp[i+addr];
3741.16 (drh 21-Feb-04): pOut->opcode = pIn->opcode;
3751.16 (drh 21-Feb-04): pOut->p1 = pIn->p1;
3761.16 (drh 21-Feb-04): pOut->p2 = p2<0 ? addr + ADDR(p2) : p2;
3771.16 (drh 21-Feb-04): pOut->p3 = pIn->p3;
3781.16 (drh 21-Feb-04): pOut->p3type = pIn->p3 ? P3_STATIC : P3_NOTUSED;
3791.156 (danielk1 12-Jan-05): #ifdef SQLITE_DEBUG
3801.24 (danielk1 10-May-04): if( sqlite3_vdbe_addop_trace ){
3811.19 (danielk1 08-May-04): sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]);
3821.1 (drh 06-Sep-03): }
3831.1 (drh 06-Sep-03): #endif
3841.1 (drh 06-Sep-03): }
3851.1 (drh 06-Sep-03): p->nOp += nOp;
3861.1 (drh 06-Sep-03): }
3871.1 (drh 06-Sep-03): return addr;
3881.1 (drh 06-Sep-03): }
3891.1 (drh 06-Sep-03):
3901.1 (drh 06-Sep-03): /*
3911.1 (drh 06-Sep-03): ** Change the value of the P1 operand for a specific instruction.
3921.1 (drh 06-Sep-03): ** This routine is useful when a large program is loaded from a
3931.19 (danielk1 08-May-04): ** static array using sqlite3VdbeAddOpList but we want to make a
3941.1 (drh 06-Sep-03): ** few minor changes to the program.
3951.1 (drh 06-Sep-03): */
3961.19 (danielk1 08-May-04): void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){
3971.240 (drh 13-Mar-06): assert( p==0 || p->magic==VDBE_MAGIC_INIT );
3981.1 (drh 06-Sep-03): if( p && addr>=0 && p->nOp>addr && p->aOp ){
3991.1 (drh 06-Sep-03): p->aOp[addr].p1 = val;
4001.1 (drh 06-Sep-03): }
4011.1 (drh 06-Sep-03): }
4021.1 (drh 06-Sep-03):
4031.1 (drh 06-Sep-03): /*
4041.1 (drh 06-Sep-03): ** Change the value of the P2 operand for a specific instruction.
4051.1 (drh 06-Sep-03): ** This routine is useful for setting a jump destination.
4061.1 (drh 06-Sep-03): */
4071.19 (danielk1 08-May-04): void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){
4081.1 (drh 06-Sep-03): assert( val>=0 );
4091.240 (drh 13-Mar-06): assert( p==0 || p->magic==VDBE_MAGIC_INIT );
4101.1 (drh 06-Sep-03): if( p && addr>=0 && p->nOp>addr && p->aOp ){
4111.1 (drh 06-Sep-03): p->aOp[addr].p2 = val;
4121.1 (drh 06-Sep-03): }
4131.1 (drh 06-Sep-03): }
4141.1 (drh 06-Sep-03):
4151.202 (drh 20-Sep-05): /*
4161.242 (drh 17-Mar-06): ** Change the P2 operand of instruction addr so that it points to
4171.202 (drh 20-Sep-05): ** the address of the next instruction to be coded.
4181.202 (drh 20-Sep-05): */
4191.202 (drh 20-Sep-05): void sqlite3VdbeJumpHere(Vdbe *p, int addr){
4201.202 (drh 20-Sep-05): sqlite3VdbeChangeP2(p, addr, p->nOp);
4211.202 (drh 20-Sep-05): }
4221.198 (drh 16-Sep-05):
4231.257 (drh 08-Jul-06):
4241.257 (drh 08-Jul-06): /*
4251.257 (drh 08-Jul-06): ** If the input FuncDef structure is ephemeral, then free it. If
4261.257 (drh 08-Jul-06): ** the FuncDef is not ephermal, then do nothing.
4271.257 (drh 08-Jul-06): */
4281.257 (drh 08-Jul-06): static void freeEphemeralFunction(FuncDef *pDef){
4291.257 (drh 08-Jul-06): if( pDef && (pDef->flags & SQLITE_FUNC_EPHEM)!=0 ){
4301.299 (drh 16-Aug-07): sqlite3_free(pDef);
4311.257 (drh 08-Jul-06): }
4321.257 (drh 08-Jul-06): }
4331.257 (drh 08-Jul-06):
4341.198 (drh 16-Sep-05): /*
4351.198 (drh 16-Sep-05): ** Delete a P3 value if necessary.
4361.198 (drh 16-Sep-05): */
4371.198 (drh 16-Sep-05): static void freeP3(int p3type, void *p3){
4381.198 (drh 16-Sep-05): if( p3 ){
4391.201 (drh 17-Sep-05): switch( p3type ){
4401.201 (drh 17-Sep-05): case P3_DYNAMIC:
4411.201 (drh 17-Sep-05): case P3_KEYINFO:
4421.201 (drh 17-Sep-05): case P3_KEYINFO_HANDOFF: {
4431.299 (drh 16-Aug-07): sqlite3_free(p3);
4441.201 (drh 17-Sep-05): break;
4451.201 (drh 17-Sep-05): }
4461.246 (drh 13-Jun-06): case P3_MPRINTF: {
4471.246 (drh 13-Jun-06): sqlite3_free(p3);
4481.246 (drh 13-Jun-06): break;
4491.246 (drh 13-Jun-06): }
4501.201 (drh 17-Sep-05): case P3_VDBEFUNC: {
4511.201 (drh 17-Sep-05): VdbeFunc *pVdbeFunc = (VdbeFunc *)p3;
4521.257 (drh 08-Jul-06): freeEphemeralFunction(pVdbeFunc->pFunc);
4531.201 (drh 17-Sep-05): sqlite3VdbeDeleteAuxData(pVdbeFunc, 0);
4541.299 (drh 16-Aug-07): sqlite3_free(pVdbeFunc);
4551.201 (drh 17-Sep-05): break;
4561.201 (drh 17-Sep-05): }
4571.257 (drh 08-Jul-06): case P3_FUNCDEF: {
4581.257 (drh 08-Jul-06): freeEphemeralFunction((FuncDef*)p3);
4591.257 (drh 08-Jul-06): break;
4601.257 (drh 08-Jul-06): }
4611.201 (drh 17-Sep-05): case P3_MEM: {
4621.201 (drh 17-Sep-05): sqlite3ValueFree((sqlite3_value*)p3);
4631.201 (drh 17-Sep-05): break;
4641.201 (drh 17-Sep-05): }
4651.198 (drh 16-Sep-05): }
4661.198 (drh 16-Sep-05): }
4671.198 (drh 16-Sep-05): }
4681.198 (drh 16-Sep-05):
4691.198 (drh 16-Sep-05):
4701.1 (drh 06-Sep-03): /*
4711.242 (drh 17-Mar-06): ** Change N opcodes starting at addr to No-ops.
4721.242 (drh 17-Mar-06): */
4731.242 (drh 17-Mar-06): void sqlite3VdbeChangeToNoop(Vdbe *p, int addr, int N){
4741.285 (danielk1 04-May-07): if( p && p->aOp ){
4751.285 (danielk1 04-May-07): VdbeOp *pOp = &p->aOp[addr];
4761.285 (danielk1 04-May-07): while( N-- ){
4771.285 (danielk1 04-May-07): freeP3(pOp->p3type, pOp->p3);
4781.285 (danielk1 04-May-07): memset(pOp, 0, sizeof(pOp[0]));
4791.285 (danielk1 04-May-07): pOp->opcode = OP_Noop;
4801.285 (danielk1 04-May-07): pOp++;
4811.285 (danielk1 04-May-07): }
4821.242 (drh 17-Mar-06): }
4831.242 (drh 17-Mar-06): }
4841.242 (drh 17-Mar-06):
4851.242 (drh 17-Mar-06): /*
4861.1 (drh 06-Sep-03): ** Change the value of the P3 operand for a specific instruction.
4871.1 (drh 06-Sep-03): ** This routine is useful when a large program is loaded from a
4881.19 (danielk1 08-May-04): ** static array using sqlite3VdbeAddOpList but we want to make a
4891.1 (drh 06-Sep-03): ** few minor changes to the program.
4901.1 (drh 06-Sep-03): **
4911.1 (drh 06-Sep-03): ** If n>=0 then the P3 operand is dynamic, meaning that a copy of
4921.299 (drh 16-Aug-07): ** the string is made into memory obtained from sqlite3_malloc().
4931.1 (drh 06-Sep-03): ** A value of n==0 means copy bytes of zP3 up to and including the
4941.1 (drh 06-Sep-03): ** first null byte. If n>0 then copy n+1 bytes of zP3.
4951.1 (drh 06-Sep-03): **
4961.176 (danielk1 19-May-05): ** If n==P3_KEYINFO it means that zP3 is a pointer to a KeyInfo structure.
4971.176 (danielk1 19-May-05): ** A copy is made of the KeyInfo structure into memory obtained from
4981.299 (drh 16-Aug-07): ** sqlite3_malloc, to be freed when the Vdbe is finalized.
4991.176 (danielk1 19-May-05): ** n==P3_KEYINFO_HANDOFF indicates that zP3 points to a KeyInfo structure
5001.299 (drh 16-Aug-07): ** stored in memory that the caller has obtained from sqlite3_malloc. The
5011.176 (danielk1 19-May-05): ** caller should not free the allocation, it will be freed when the Vdbe is
5021.176 (danielk1 19-May-05): ** finalized.
5031.176 (danielk1 19-May-05): **
5041.176 (danielk1 19-May-05): ** Other values of n (P3_STATIC, P3_COLLSEQ etc.) indicate that zP3 points
5051.176 (danielk1 19-May-05): ** to a string or structure that is guaranteed to exist for the lifetime of
5061.176 (danielk1 19-May-05): ** the Vdbe. In these cases we can just copy the pointer.
5071.1 (drh 06-Sep-03): **
5081.1 (drh 06-Sep-03): ** If addr<0 then change P3 on the most recently inserted instruction.
5091.1 (drh 06-Sep-03): */
5101.19 (danielk1 08-May-04): void sqlite3VdbeChangeP3(Vdbe *p, int addr, const char *zP3, int n){
5111.1 (drh 06-Sep-03): Op *pOp;
5121.240 (drh 13-Mar-06): assert( p==0 || p->magic==VDBE_MAGIC_INIT );
5131.299 (drh 16-Aug-07): if( p==0 || p->aOp==0 || p->db->mallocFailed ){
5141.208 (danielk1 06-Dec-05): if (n != P3_KEYINFO) {
5151.208 (danielk1 06-Dec-05): freeP3(n, (void*)*(char**)&zP3);
5161.208 (danielk1 06-Dec-05): }
5171.168 (danielk1 16-Mar-05): return;
5181.168 (danielk1 16-Mar-05): }
5191.1 (drh 06-Sep-03): if( addr<0 || addr>=p->nOp ){
5201.1 (drh 06-Sep-03): addr = p->nOp - 1;
5211.1 (drh 06-Sep-03): if( addr<0 ) return;
5221.1 (drh 06-Sep-03): }
5231.1 (drh 06-Sep-03): pOp = &p->aOp[addr];
5241.198 (drh 16-Sep-05): freeP3(pOp->p3type, pOp->p3);
5251.198 (drh 16-Sep-05): pOp->p3 = 0;
5261.1 (drh 06-Sep-03): if( zP3==0 ){
5271.1 (drh 06-Sep-03): pOp->p3 = 0;
5281.1 (drh 06-Sep-03): pOp->p3type = P3_NOTUSED;
5291.50 (drh 20-May-04): }else if( n==P3_KEYINFO ){
5301.50 (drh 20-May-04): KeyInfo *pKeyInfo;
5311.50 (drh 20-May-04): int nField, nByte;
5321.192 (drh 01-Sep-05):
5331.50 (drh 20-May-04): nField = ((KeyInfo*)zP3)->nField;
5341.211 (drh 16-Dec-05): nByte = sizeof(*pKeyInfo) + (nField-1)*sizeof(pKeyInfo->aColl[0]) + nField;
5351.299 (drh 16-Aug-07): pKeyInfo = sqlite3_malloc( nByte );
5361.50 (drh 20-May-04): pOp->p3 = (char*)pKeyInfo;
5371.50 (drh 20-May-04): if( pKeyInfo ){
5381.226 (danielk1 16-Jan-06): unsigned char *aSortOrder;
5391.50 (drh 20-May-04): memcpy(pKeyInfo, zP3, nByte);
5401.211 (drh 16-Dec-05): aSortOrder = pKeyInfo->aSortOrder;
5411.211 (drh 16-Dec-05): if( aSortOrder ){
5421.226 (danielk1 16-Jan-06): pKeyInfo->aSortOrder = (unsigned char*)&pKeyInfo->aColl[nField];
5431.211 (drh 16-Dec-05): memcpy(pKeyInfo->aSortOrder, aSortOrder, nField);
5441.211 (drh 16-Dec-05): }
5451.50 (drh 20-May-04): pOp->p3type = P3_KEYINFO;
5461.50 (drh 20-May-04): }else{
5471.299 (drh 16-Aug-07): p->db->mallocFailed = 1;
5481.50 (drh 20-May-04): pOp->p3type = P3_NOTUSED;
5491.50 (drh 20-May-04): }
5501.51 (drh 21-May-04): }else if( n==P3_KEYINFO_HANDOFF ){
5511.51 (drh 21-May-04): pOp->p3 = (char*)zP3;
5521.51 (drh 21-May-04): pOp->p3type = P3_KEYINFO;
5531.1 (drh 06-Sep-03): }else if( n<0 ){
5541.1 (drh 06-Sep-03): pOp->p3 = (char*)zP3;
5551.1 (drh 06-Sep-03): pOp->p3type = n;
5561.1 (drh 06-Sep-03): }else{
5571.147 (drh 25-Sep-04): if( n==0 ) n = strlen(zP3);
5581.299 (drh 16-Aug-07): pOp->p3 = sqlite3DbStrNDup(p->db, zP3, n);
5591.1 (drh 06-Sep-03): pOp->p3type = P3_DYNAMIC;
5601.1 (drh 06-Sep-03): }
5611.1 (drh 06-Sep-03): }
5621.1 (drh 06-Sep-03):
5631.144 (drh 19-Sep-04): #ifndef NDEBUG
5641.144 (drh 19-Sep-04): /*
5651.144 (drh 19-Sep-04): ** Replace the P3 field of the most recently coded instruction with
5661.144 (drh 19-Sep-04): ** comment text.
5671.144 (drh 19-Sep-04): */
5681.144 (drh 19-Sep-04): void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
5691.144 (drh 19-Sep-04): va_list ap;
5701.282 (danielk1 18-Apr-07): assert( p->nOp>0 || p->aOp==0 );
5711.299 (drh 16-Aug-07): assert( p->aOp==0 || p->aOp[p->nOp-1].p3==0 || p->db->mallocFailed );
5721.144 (drh 19-Sep-04): va_start(ap, zFormat);
5731.300 (danielk1 16-Aug-07): sqlite3VdbeChangeP3(p, -1, sqlite3VMPrintf(p->db, zFormat, ap), P3_DYNAMIC);
5741.144 (drh 19-Sep-04): va_end(ap);
5751.144 (drh 19-Sep-04): }
5761.144 (drh 19-Sep-04): #endif
5771.144 (drh 19-Sep-04):
5781.1 (drh 06-Sep-03): /*
5791.1 (drh 06-Sep-03): ** Return the opcode for a given address.
5801.1 (drh 06-Sep-03): */
5811.19 (danielk1 08-May-04): VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
5821.1 (drh 06-Sep-03): assert( p->magic==VDBE_MAGIC_INIT );
5831.299 (drh 16-Aug-07): assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
5841.282 (danielk1 18-Apr-07): return ((addr>=0 && addr<p->nOp)?(&p->aOp[addr]):0);
5851.1 (drh 06-Sep-03): }
5861.1 (drh 06-Sep-03):
5871.151 (drh 31-Oct-04): #if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \
5881.151 (drh 31-Oct-04): || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
5891.1 (drh 06-Sep-03): /*
5901.50 (drh 20-May-04): ** Compute a string that describes the P3 parameter for an opcode.
5911.50 (drh 20-May-04): ** Use zTemp for any required temporary buffer space.
5921.50 (drh 20-May-04): */
5931.50 (drh 20-May-04): static char *displayP3(Op *pOp, char *zTemp, int nTemp){
5941.50 (drh 20-May-04): char *zP3;
5951.50 (drh 20-May-04): assert( nTemp>=20 );
5961.50 (drh 20-May-04): switch( pOp->p3type ){
5971.50 (drh 20-May-04): case P3_KEYINFO: {
5981.50 (drh 20-May-04): int i, j;
5991.50 (drh 20-May-04): KeyInfo *pKeyInfo = (KeyInfo*)pOp->p3;
6001.286 (drh 04-May-07): sqlite3_snprintf(nTemp, zTemp, "keyinfo(%d", pKeyInfo->nField);
6011.50 (drh 20-May-04): i = strlen(zTemp);
6021.50 (drh 20-May-04): for(j=0; j<pKeyInfo->nField; j++){
6031.50 (drh 20-May-04): CollSeq *pColl = pKeyInfo->aColl[j];
6041.50 (drh 20-May-04): if( pColl ){
6051.50 (drh 20-May-04): int n = strlen(pColl->zName);
6061.50 (drh 20-May-04): if( i+n>nTemp-6 ){
6071.286 (drh 04-May-07): memcpy(&zTemp[i],",...",4);
6081.50 (drh 20-May-04): break;
6091.50 (drh 20-May-04): }
6101.50 (drh 20-May-04): zTemp[i++] = ',';
6111.51 (drh 21-May-04): if( pKeyInfo->aSortOrder && pKeyInfo->aSortOrder[j] ){
6121.50 (drh 20-May-04): zTemp[i++] = '-';
6131.50 (drh 20-May-04): }
6141.286 (drh 04-May-07): memcpy(&zTemp[i], pColl->zName,n+1);
6151.50 (drh 20-May-04): i += n;
6161.50 (drh 20-May-04): }else if( i+4<nTemp-6 ){
6171.286 (drh 04-May-07): memcpy(&zTemp[i],",nil",4);
6181.50 (drh 20-May-04): i += 4;
6191.50 (drh 20-May-04): }
6201.50 (drh 20-May-04): }
6211.50 (drh 20-May-04): zTemp[i++] = ')';
6221.50 (drh 20-May-04): zTemp[i] = 0;
6231.50 (drh 20-May-04): assert( i<nTemp );
6241.50 (drh 20-May-04): zP3 = zTemp;
6251.50 (drh 20-May-04): break;
6261.50 (drh 20-May-04): }
6271.50 (drh 20-May-04): case P3_COLLSEQ: {
6281.50 (drh 20-May-04): CollSeq *pColl = (CollSeq*)pOp->p3;
6291.286 (drh 04-May-07): sqlite3_snprintf(nTemp, zTemp, "collseq(%.20s)", pColl->zName);
6301.50 (drh 20-May-04): zP3 = zTemp;
6311.50 (drh 20-May-04): break;
6321.50 (drh 20-May-04): }
6331.67 (drh 26-May-04): case P3_FUNCDEF: {
6341.67 (drh 26-May-04): FuncDef *pDef = (FuncDef*)pOp->p3;
6351.244 (drh 13-Jun-06): sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg);
6361.244 (drh 13-Jun-06): zP3 = zTemp;
6371.244 (drh 13-Jun-06): break;
6381.244 (drh 13-Jun-06): }
6391.244 (drh 13-Jun-06): #ifndef SQLITE_OMIT_VIRTUALTABLE
6401.244 (drh 13-Jun-06): case P3_VTAB: {
6411.244 (drh 13-Jun-06): sqlite3_vtab *pVtab = (sqlite3_vtab*)pOp->p3;
6421.256 (drh 26-Jun-06): sqlite3_snprintf(nTemp, zTemp, "vtab:%p:%p", pVtab, pVtab->pModule);
6431.67 (drh 26-May-04): zP3 = zTemp;
6441.67 (drh 26-May-04): break;
6451.67 (drh 26-May-04): }
6461.244 (drh 13-Jun-06): #endif
6471.50 (drh 20-May-04): default: {
6481.50 (drh 20-May-04): zP3 = pOp->p3;
6491.135 (drh 24-Jul-04): if( zP3==0 || pOp->opcode==OP_Noop ){
6501.50 (drh 20-May-04): zP3 = "";
6511.50 (drh 20-May-04): }
6521.50 (drh 20-May-04): }
6531.50 (drh 20-May-04): }
6541.249 (drh 15-Jun-06): assert( zP3!=0 );
6551.50 (drh 20-May-04): return zP3;
6561.50 (drh 20-May-04): }
6571.151 (drh 31-Oct-04): #endif
6581.50 (drh 20-May-04):
6591.312 (drh 28-Aug-07): /*
6601.313 (drh 28-Aug-07): ** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
6611.313 (drh 28-Aug-07): **
6621.312 (drh 28-Aug-07): */
6631.317 (drh 30-Aug-07): void sqlite3VdbeUsesBtree(Vdbe *p, int i){
6641.317 (drh 30-Aug-07): int mask;
6651.313 (drh 28-Aug-07): assert( i>=0 && i<p->db->nDb );
6661.313 (drh 28-Aug-07): assert( i<sizeof(p->btreeMask)*8 );
6671.317 (drh 30-Aug-07): mask = 1<<i;
6681.317 (drh 30-Aug-07): if( (p->btreeMask & mask)==0 ){
6691.317 (drh 30-Aug-07): p->btreeMask |= mask;
6701.317 (drh 30-Aug-07): sqlite3BtreeMutexArrayInsert(&p->aMutex, p->db->aDb[i].pBt);
6711.317 (drh 30-Aug-07): }
6721.312 (drh 28-Aug-07): }
6731.312 (drh 28-Aug-07):
6741.50 (drh 20-May-04):
6751.156 (danielk1 12-Jan-05): #if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
6761.1 (drh 06-Sep-03): /*
6771.1 (drh 06-Sep-03): ** Print a single opcode. This routine is used for debugging only.
6781.1 (drh 06-Sep-03): */
6791.19 (danielk1 08-May-04): void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){
6801.1 (drh 06-Sep-03): char *zP3;
6811.50 (drh 20-May-04): char zPtr[50];
6821.50 (drh 20-May-04): static const char *zFormat1 = "%4d %-13s %4d %4d %s\n";
6831.1 (drh 06-Sep-03): if( pOut==0 ) pOut = stdout;
6841.50 (drh 20-May-04): zP3 = displayP3(pOp, zPtr, sizeof(zPtr));
6851.50 (drh 20-May-04): fprintf(pOut, zFormat1,
6861.311 (drh 27-Aug-07): pc, sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, zP3);
6871.1 (drh 06-Sep-03): fflush(pOut);
6881.1 (drh 06-Sep-03): }
6891.1 (drh 06-Sep-03): #endif
6901.1 (drh 06-Sep-03):
6911.1 (drh 06-Sep-03): /*
6921.146 (drh 24-Sep-04): ** Release an array of N Mem elements
6931.146 (drh 24-Sep-04): */
6941.146 (drh 24-Sep-04): static void releaseMemArray(Mem *p, int N){
6951.146 (drh 24-Sep-04): if( p ){
6961.146 (drh 24-Sep-04): while( N-->0 ){
6971.307 (drh 21-Aug-07): assert( N<2 || p[0].db==p[1].db );
6981.146 (drh 24-Sep-04): sqlite3VdbeMemRelease(p++);
6991.146 (drh 24-Sep-04): }
7001.146 (drh 24-Sep-04): }
7011.146 (drh 24-Sep-04): }
7021.146 (drh 24-Sep-04):
7031.151 (drh 31-Oct-04): #ifndef SQLITE_OMIT_EXPLAIN
7041.146 (drh 24-Sep-04): /*
7051.1 (drh 06-Sep-03): ** Give a listing of the program in the virtual machine.
7061.1 (drh 06-Sep-03): **
7071.19 (danielk1 08-May-04): ** The interface is the same as sqlite3VdbeExec(). But instead of
7081.1 (drh 06-Sep-03): ** running the code, it invokes the callback once for each instruction.
7091.1 (drh 06-Sep-03): ** This feature is used to implement "EXPLAIN".
7101.1 (drh 06-Sep-03): */
7111.19 (danielk1 08-May-04): int sqlite3VdbeList(
7121.1 (drh 06-Sep-03): Vdbe *p /* The VDBE */
7131.1 (drh 06-Sep-03): ){
7141.140 (drh 06-Sep-04): sqlite3 *db = p->db;
7151.1 (drh 06-Sep-03): int i;
7161.14 (drh 14-Feb-04): int rc = SQLITE_OK;
7171.1 (drh 06-Sep-03):
7181.1 (drh 06-Sep-03): assert( p->explain );
7191.155 (drh 11-Jan-05): if( p->magic!=VDBE_MAGIC_RUN ) return SQLITE_MISUSE;
7201.155 (drh 11-Jan-05): assert( db->magic==SQLITE_MAGIC_BUSY );
7211.155 (drh 11-Jan-05): assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
7221.56 (danielk1 22-May-04):
7231.56 (danielk1 22-May-04): /* Even though this opcode does not put dynamic strings onto the
7241.56 (danielk1 22-May-04): ** the stack, they may become dynamic if the user calls
7251.68 (drh 26-May-04): ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
7261.56 (danielk1 22-May-04): */
7271.56 (danielk1 22-May-04): if( p->pTos==&p->aStack[4] ){
7281.146 (drh 24-Sep-04): releaseMemArray(p->aStack, 5);
7291.56 (danielk1 22-May-04): }
7301.56 (danielk1 22-May-04): p->resOnStack = 0;
7311.56 (danielk1 22-May-04):
7321.197 (drh 10-Sep-05): do{
7331.197 (drh 10-Sep-05): i = p->pc++;
7341.197 (drh 10-Sep-05): }while( i<p->nOp && p->explain==2 && p->aOp[i].opcode!=OP_Explain );
7351.14 (drh 14-Feb-04): if( i>=p->nOp ){
7361.14 (drh 14-Feb-04): p->rc = SQLITE_OK;
7371.14 (drh 14-Feb-04): rc = SQLITE_DONE;
7381.259 (drh 26-Jul-06): }else if( db->u1.isInterrupted ){
7391.155 (drh 11-Jan-05): p->rc = SQLITE_INTERRUPT;
7401.14 (drh 14-Feb-04): rc = SQLITE_ERROR;
7411.89 (danielk1 31-May-04): sqlite3SetString(&p->zErrMsg, sqlite3ErrStr(p->rc), (char*)0);
7421.14 (drh 14-Feb-04): }else{
7431.50 (drh 20-May-04): Op *pOp = &p->aOp[i];
7441.69 (drh 27-May-04): Mem *pMem = p->aStack;
7451.69 (drh 27-May-04): pMem->flags = MEM_Int;
7461.88 (drh 31-May-04): pMem->type = SQLITE_INTEGER;
7471.276 (drh 30-Mar-07): pMem->u.i = i; /* Program counter */
7481.69 (drh 27-May-04): pMem++;
7491.69 (drh 27-May-04):
7501.69 (drh 27-May-04): pMem->flags = MEM_Static|MEM_Str|MEM_Term;
7511.311 (drh 27-Aug-07): pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */
7521.249 (drh 15-Jun-06): assert( pMem->z!=0 );
7531.69 (drh 27-May-04): pMem->n = strlen(pMem->z);
7541.88 (drh 31-May-04): pMem->type = SQLITE_TEXT;
7551.104 (danielk1 12-Jun-04): pMem->enc = SQLITE_UTF8;
7561.69 (drh 27-May-04): pMem++;
7571.69 (drh 27-May-04):
7581.69 (drh 27-May-04): pMem->flags = MEM_Int;
7591.276 (drh 30-Mar-07): pMem->u.i = pOp->p1; /* P1 */
7601.88 (drh 31-May-04): pMem->type = SQLITE_INTEGER;
7611.69 (drh 27-May-04): pMem++;
7621.69 (drh 27-May-04):
7631.69 (drh 27-May-04): pMem->flags = MEM_Int;
7641.276 (drh 30-Mar-07): pMem->u.i = pOp->p2; /* P2 */
7651.88 (drh 31-May-04): pMem->type = SQLITE_INTEGER;
7661.69 (drh 27-May-04): pMem++;
7671.69 (drh 27-May-04):
7681.239 (drh 03-Mar-06): pMem->flags = MEM_Ephem|MEM_Str|MEM_Term; /* P3 */
7691.69 (drh 27-May-04): pMem->z = displayP3(pOp, pMem->zShort, sizeof(pMem->zShort));
7701.249 (drh 15-Jun-06): assert( pMem->z!=0 );
7711.239 (drh 03-Mar-06): pMem->n = strlen(pMem->z);
7721.88 (drh 31-May-04): pMem->type = SQLITE_TEXT;
7731.104 (danielk1 12-Jun-04): pMem->enc = SQLITE_UTF8;
7741.69 (drh 27-May-04):
7751.197 (drh 10-Sep-05): p->nResColumn = 5 - 2*(p->explain-1);
7761.69 (drh 27-May-04): p->pTos = pMem;
7771.14 (drh 14-Feb-04): p->rc = SQLITE_OK;
7781.56 (danielk1 22-May-04): p->resOnStack = 1;
7791.14 (drh 14-Feb-04): rc = SQLITE_ROW;
7801.1 (drh 06-Sep-03): }
7811.14 (drh 14-Feb-04): return rc;
7821.1 (drh 06-Sep-03): }
7831.151 (drh 31-Oct-04): #endif /* SQLITE_OMIT_EXPLAIN */
7841.1 (drh 06-Sep-03):
7851.281 (drh 05-Apr-07): #ifdef SQLITE_DEBUG
7861.1 (drh 06-Sep-03): /*
7871.135 (drh 24-Jul-04): ** Print the SQL that was used to generate a VDBE program.
7881.135 (drh 24-Jul-04): */
7891.135 (drh 24-Jul-04): void sqlite3VdbePrintSql(Vdbe *p){
7901.135 (drh 24-Jul-04): int nOp = p->nOp;
7911.135 (drh 24-Jul-04): VdbeOp *pOp;
7921.142 (drh 15-Sep-04): if( nOp<1 ) return;
7931.142 (drh 15-Sep-04): pOp = &p->aOp[nOp-1];
7941.135 (drh 24-Jul-04): if( pOp->opcode==OP_Noop && pOp->p3!=0 ){
7951.135 (drh 24-Jul-04): const char *z = pOp->p3;
7961.136 (drh 08-Aug-04): while( isspace(*(u8*)z) ) z++;
7971.135 (drh 24-Jul-04): printf("SQL: [%s]\n", z);
7981.135 (drh 24-Jul-04): }
7991.281 (drh 05-Apr-07): }
8001.135 (drh 24-Jul-04): #endif
8011.135 (drh 24-Jul-04):
8021.271 (drh 01-Mar-07): #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
8031.271 (drh 01-Mar-07): /*
8041.271 (drh 01-Mar-07): ** Print an IOTRACE message showing SQL content.
8051.271 (drh 01-Mar-07): */
8061.271 (drh 01-Mar-07): void sqlite3VdbeIOTraceSql(Vdbe *p){
8071.271 (drh 01-Mar-07): int nOp = p->nOp;
8081.271 (drh 01-Mar-07): VdbeOp *pOp;
8091.271 (drh 01-Mar-07): if( sqlite3_io_trace==0 ) return;
8101.271 (drh 01-Mar-07): if( nOp<1 ) return;
8111.271 (drh 01-Mar-07): pOp = &p->aOp[nOp-1];
8121.271 (drh 01-Mar-07): if( pOp->opcode==OP_Noop && pOp->p3!=0 ){
8131.271 (drh 01-Mar-07): int i, j;
8141.297 (drh 13-Aug-07): char z[1000];
8151.297 (drh 13-Aug-07): sqlite3_snprintf(sizeof(z), z, "%s", pOp->p3);
8161.288 (danielk1 16-May-07): for(i=0; isspace((unsigned char)z[i]); i++){}
8171.271 (drh 01-Mar-07): for(j=0; z[i]; i++){
8181.288 (danielk1 16-May-07): if( isspace((unsigned char)z[i]) ){
8191.271 (drh 01-Mar-07): if( z[i-1]!=' ' ){
8201.271 (drh 01-Mar-07): z[j++] = ' ';
8211.271 (drh 01-Mar-07): }
8221.271 (drh 01-Mar-07): }else{
8231.271 (drh 01-Mar-07): z[j++] = z[i];
8241.271 (drh 01-Mar-07): }
8251.271 (drh 01-Mar-07): }
8261.271 (drh 01-Mar-07): z[j] = 0;
8271.271 (drh 01-Mar-07): sqlite3_io_trace("SQL %s\n", z);
8281.271 (drh 01-Mar-07): }
8291.271 (drh 01-Mar-07): }
8301.271 (drh 01-Mar-07): #endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */
8311.271 (drh 01-Mar-07):
8321.271 (drh 01-Mar-07):
8331.135 (drh 24-Jul-04): /*
8341.1 (drh 06-Sep-03): ** Prepare a virtual machine for execution. This involves things such
8351.1 (drh 06-Sep-03): ** as allocating stack space and initializing the program counter.
8361.1 (drh 06-Sep-03): ** After the VDBE has be prepped, it can be executed by one or more
8371.19 (danielk1 08-May-04): ** calls to sqlite3VdbeExec().
8381.139 (drh 02-Sep-04): **
8391.139 (drh 02-Sep-04): ** This is the only way to move a VDBE from VDBE_MAGIC_INIT to
8401.139 (drh 02-Sep-04): ** VDBE_MAGIC_RUN.
8411.1 (drh 06-Sep-03): */
8421.19 (danielk1 08-May-04): void sqlite3VdbeMakeReady(
8431.1 (drh 06-Sep-03): Vdbe *p, /* The VDBE */
8441.2 (drh 06-Sep-03): int nVar, /* Number of '?' see in the SQL statement */
8451.138 (drh 21-Aug-04): int nMem, /* Number of memory cells to allocate */
8461.138 (drh 21-Aug-04): int nCursor, /* Number of cursors to allocate */
8471.1 (drh 06-Sep-03): int isExplain /* True if the EXPLAIN keywords is present */
8481.1 (drh 06-Sep-03): ){
8491.1 (drh 06-Sep-03): int n;
8501.300 (danielk1 16-Aug-07): sqlite3 *db = p->db;
8511.1 (drh 06-Sep-03):
8521.1 (drh 06-Sep-03): assert( p!=0 );
8531.1 (drh 06-Sep-03): assert( p->magic==VDBE_MAGIC_INIT );
8541.1 (drh 06-Sep-03):
8551.142 (drh 15-Sep-04): /* There should be at least one opcode.
8561.1 (drh 06-Sep-03): */
8571.142 (drh 15-Sep-04): assert( p->nOp>0 );
8581.1 (drh 06-Sep-03):
8591.170 (danielk1 28-Mar-05): /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. This
8601.170 (danielk1 28-Mar-05): * is because the call to resizeOpArray() below may shrink the
8611.170 (danielk1 28-Mar-05): * p->aOp[] array to save memory if called when in VDBE_MAGIC_RUN
8621.170 (danielk1 28-Mar-05): * state.
8631.170 (danielk1 28-Mar-05): */
8641.170 (danielk1 28-Mar-05): p->magic = VDBE_MAGIC_RUN;
8651.170 (danielk1 28-Mar-05):
8661.1 (drh 06-Sep-03): /* No instruction ever pushes more than a single element onto the
8671.1 (drh 06-Sep-03): ** stack. And the stack never grows on successive executions of the
8681.1 (drh 06-Sep-03): ** same loop. So the total number of instructions is an upper bound
8691.180 (drh 07-Jun-05): ** on the maximum stack depth required. (Added later:) The
8701.180 (drh 07-Jun-05): ** resolveP2Values() call computes a tighter upper bound on the
8711.180 (drh 07-Jun-05): ** stack size.
8721.1 (drh 06-Sep-03): **
8731.1 (drh 06-Sep-03): ** Allocation all the stack space we will ever need.
8741.1 (drh 06-Sep-03): */
8751.3 (drh 06-Sep-03): if( p->aStack==0 ){
8761.170 (danielk1 28-Mar-05): int nArg; /* Maximum number of args passed to a user function. */
8771.171 (danielk1 29-Mar-05): int nStack; /* Maximum number of stack entries required */
8781.171 (danielk1 29-Mar-05): resolveP2Values(p, &nArg, &nStack);
8791.170 (danielk1 28-Mar-05): resizeOpArray(p, p->nOp);
8801.3 (drh 06-Sep-03): assert( nVar>=0 );
8811.171 (danielk1 29-Mar-05): assert( nStack<p->nOp );
8821.261 (drh 08-Aug-06): if( isExplain ){
8831.261 (drh 08-Aug-06): nStack = 10;
8841.261 (drh 08-Aug-06): }
8851.300 (danielk1 16-Aug-07): p->aStack = sqlite3DbMallocZero(db,
8861.171 (danielk1 29-Mar-05): nStack*sizeof(p->aStack[0]) /* aStack */
8871.170 (danielk1 28-Mar-05): + nArg*sizeof(Mem*) /* apArg */
8881.149 (drh 05-Oct-04): + nVar*sizeof(Mem) /* aVar */
8891.149 (drh 05-Oct-04): + nVar*sizeof(char*) /* azVar */
8901.149 (drh 05-Oct-04): + nMem*sizeof(Mem) /* aMem */
8911.149 (drh 05-Oct-04): + nCursor*sizeof(Cursor*) /* apCsr */
8921.3 (drh 06-Sep-03): );
8931.299 (drh 16-Aug-07): if( !db->mallocFailed ){
8941.171 (danielk1 29-Mar-05): p->aMem = &p->aStack[nStack];
8951.149 (drh 05-Oct-04): p->nMem = nMem;
8961.149 (drh 05-Oct-04): p->aVar = &p->aMem[nMem];
8971.149 (drh 05-Oct-04): p->nVar = nVar;
8981.138 (drh 21-Aug-04): p->okVar = 0;
8991.149 (drh 05-Oct-04): p->apArg = (Mem**)&p->aVar[nVar];
9001.170 (danielk1 28-Mar-05): p->azVar = (char**)&p->apArg[nArg];
9011.149 (drh 05-Oct-04): p->apCsr = (Cursor**)&p->azVar[nVar];
9021.138 (drh 21-Aug-04): p->nCursor = nCursor;
9031.138 (drh 21-Aug-04): for(n=0; n<nVar; n++){
9041.138 (drh 21-Aug-04): p->aVar[n].flags = MEM_Null;
9051.307 (drh 21-Aug-07): p->aVar[n].db = db;
9061.307 (drh 21-Aug-07): }
9071.307 (drh 21-Aug-07): for(n=0; n<nStack; n++){
9081.307 (drh 21-Aug-07): p->aStack[n].db = db;
9091.138 (drh 21-Aug-04): }
9101.42 (danielk1 19-May-04): }
9111.3 (drh 06-Sep-03): }
9121.166 (danielk1 29-Jan-05): for(n=0; n<p->nMem; n++){
9131.166 (danielk1 29-Jan-05): p->aMem[n].flags = MEM_Null;
9141.307 (drh 21-Aug-07): p->aMem[n].db = db;
9151.166 (danielk1 29-Jan-05): }
9161.1 (drh 06-Sep-03):
9171.10 (drh 31-Jan-04): p->pTos = &p->aStack[-1];
9181.85 (danielk1 31-May-04): p->pc = -1;
9191.1 (drh 06-Sep-03): p->rc = SQLITE_OK;
9201.1 (drh 06-Sep-03): p->uniqueCnt = 0;
9211.1 (drh 06-Sep-03): p->returnDepth = 0;
9221.1 (drh 06-Sep-03): p->errorAction = OE_Abort;
9231.1 (drh 06-Sep-03): p->popStack = 0;
9241.1 (drh 06-Sep-03): p->explain |= isExplain;
9251.1 (drh 06-Sep-03): p->magic = VDBE_MAGIC_RUN;
9261.121 (danielk1 21-Jun-04): p->nChange = 0;
9271.219 (drh 07-Jan-06): p->cacheCtr = 1;
9281.215 (drh 29-Dec-05): p->minWriteFileFormat = 255;
9291.294 (danielk1 27-Jun-07): p->openedStatement = 0;
9301.1 (drh 06-Sep-03): #ifdef VDBE_PROFILE
9311.6 (drh 31-Dec-03): {
9321.6 (drh 31-Dec-03): int i;
9331.6 (drh 31-Dec-03): for(i=0; i<p->nOp; i++){
9341.6 (drh 31-Dec-03): p->aOp[i].cnt = 0;
9351.6 (drh 31-Dec-03): p->aOp[i].cycles = 0;
9361.6 (drh 31-Dec-03): }
9371.1 (drh 06-Sep-03): }
9381.1 (drh 06-Sep-03): #endif
9391.1 (drh 06-Sep-03): }
9401.1 (drh 06-Sep-03):
9411.1 (drh 06-Sep-03): /*
9421.316 (drh 29-Aug-07): ** Close a VDBE cursor and release all the resources that cursor happens
9431.1 (drh 06-Sep-03): ** to hold.
9441.1 (drh 06-Sep-03): */
9451.252 (danielk1 23-Jun-06): void sqlite3VdbeFreeCursor(Vdbe *p, Cursor *pCx){
9461.106 (drh 12-Jun-04): if( pCx==0 ){
9471.106 (drh 12-Jun-04): return;
9481.106 (drh 12-Jun-04): }
9491.1 (drh 06-Sep-03): if( pCx->pCursor ){
9501.19 (danielk1 08-May-04): sqlite3BtreeCloseCursor(pCx->pCursor);
9511.1 (drh 06-Sep-03): }
9521.1 (drh 06-Sep-03): if( pCx->pBt ){
9531.19 (danielk1 08-May-04): sqlite3BtreeClose(pCx->pBt);
9541.1 (drh 06-Sep-03): }
9551.243 (drh 12-Jun-06): #ifndef SQLITE_OMIT_VIRTUALTABLE
9561.243 (drh 12-Jun-06): if( pCx->pVtabCursor ){
9571.243 (drh 12-Jun-06): sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor;
9581.252 (danielk1 23-Jun-06): const sqlite3_module *pModule = pCx->pModule;
9591.252 (danielk1 23-Jun-06): p->inVtabMethod = 1;
9601.258 (danielk1 25-Jul-06): sqlite3SafetyOff(p->db);
9611.243 (drh 12-Jun-06): pModule->xClose(pVtabCursor);
9621.258 (danielk1 25-Jul-06): sqlite3SafetyOn(p->db);
9631.252 (danielk1 23-Jun-06): p->inVtabMethod = 0;
9641.243 (drh 12-Jun-06): }
9651.243 (drh 12-Jun-06): #endif
9661.299 (drh 16-Aug-07): sqlite3_free(pCx->pData);
9671.299 (drh 16-Aug-07): sqlite3_free(pCx->aType);
9681.299 (drh 16-Aug-07): sqlite3_free(pCx);
9691.1 (drh 06-Sep-03): }
9701.1 (drh 06-Sep-03):
9711.1 (drh 06-Sep-03): /*
9721.316 (drh 29-Aug-07): ** Close all cursors except for VTab cursors that are currently
9731.316 (drh 29-Aug-07): ** in use.
9741.1 (drh 06-Sep-03): */
9751.316 (drh 29-Aug-07): static void closeAllCursorsExceptActiveVtabs(Vdbe *p){
9761.1 (drh 06-Sep-03): int i;
9771.138 (drh 21-Aug-04): if( p->apCsr==0 ) return;
9781.1 (drh 06-Sep-03): for(i=0; i<p->nCursor; i++){
9791.316 (drh 29-Aug-07): Cursor *pC = p->apCsr[i];
9801.316 (drh 29-Aug-07): if( pC && (!p->inVtabMethod || !pC->pVtabCursor) ){
9811.316 (drh 29-Aug-07): sqlite3VdbeFreeCursor(p, pC);
9821.253 (danielk1 23-Jun-06): p->apCsr[i] = 0;
9831.252 (danielk1 23-Jun-06): }
9841.1 (drh 06-Sep-03): }
9851.1 (drh 06-Sep-03): }
9861.1 (drh 06-Sep-03):
9871.1 (drh 06-Sep-03): /*
9881.1 (drh 06-Sep-03): ** Clean up the VM after execution.
9891.1 (drh 06-Sep-03): **
9901.1 (drh 06-Sep-03): ** This routine will automatically close any cursors, lists, and/or
9911.1 (drh 06-Sep-03): ** sorters that were left open. It also deletes the values of
9921.43 (drh 19-May-04): ** variables in the aVar[] array.
9931.1 (drh 06-Sep-03): */
9941.1 (drh 06-Sep-03): static void Cleanup(Vdbe *p){
9951.1 (drh 06-Sep-03): int i;
9961.10 (drh 31-Jan-04): if( p->aStack ){
9971.146 (drh 24-Sep-04): releaseMemArray(p->aStack, 1 + (p->pTos - p->aStack));
9981.146 (drh 24-Sep-04): p->pTos = &p->aStack[-1];
9991.10 (drh 31-Jan-04): }
10001.316 (drh 29-Aug-07): closeAllCursorsExceptActiveVtabs(p);
10011.146 (drh 24-Sep-04): releaseMemArray(p->aMem, p->nMem);
10021.183 (drh 08-Jul-05): sqlite3VdbeFifoClear(&p->sFifo);
10031.146 (drh 24-Sep-04): if( p->contextStack ){
10041.146 (drh 24-Sep-04): for(i=0; i<p->contextStackTop; i++){
10051.183 (drh 08-Jul-05): sqlite3VdbeFifoClear(&p->contextStack[i].sFifo);
10061.146 (drh 24-Sep-04): }
10071.299 (drh 16-Aug-07): sqlite3_free(p->contextStack);
10081.143 (drh 19-Sep-04): }
10091.17 (drh 21-Feb-04): p->contextStack = 0;
10101.143 (drh 19-Sep-04): p->contextStackDepth = 0;
10111.143 (drh 19-Sep-04): p->contextStackTop = 0;
10121.299 (drh 16-Aug-07): sqlite3_free(p->zErrMsg);
10131.1 (drh 06-Sep-03): p->zErrMsg = 0;
10141.293 (drh 20-Jun-07): p->resOnStack = 0;
10151.1 (drh 06-Sep-03): }
10161.1 (drh 06-Sep-03):
10171.1 (drh 06-Sep-03): /*
10181.64 (danielk1 25-May-04): ** Set the number of result columns that will be returned by this SQL
10191.64 (danielk1 25-May-04): ** statement. This is now set at compile time, rather than during
10201.64 (danielk1 25-May-04): ** execution of the vdbe program so that sqlite3_column_count() can
10211.64 (danielk1 25-May-04): ** be called on an SQL statement before sqlite3_step().
10221.64 (danielk1 25-May-04): */
10231.64 (danielk1 25-May-04): void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
10241.146 (drh 24-Sep-04): Mem *pColName;
10251.146 (drh 24-Sep-04): int n;
10261.308 (drh 23-Aug-07):
10271.236 (danielk1 10-Feb-06): releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
10281.299 (drh 16-Aug-07): sqlite3_free(p->aColName);
10291.236 (danielk1 10-Feb-06): n = nResColumn*COLNAME_N;
10301.64 (danielk1 25-May-04): p->nResColumn = nResColumn;
10311.300 (danielk1 16-Aug-07): p->aColName = pColName = (Mem*)sqlite3DbMallocZero(p->db, sizeof(Mem)*n );
10321.146 (drh 24-Sep-04): if( p->aColName==0 ) return;
10331.146 (drh 24-Sep-04): while( n-- > 0 ){
10341.308 (drh 23-Aug-07): pColName->flags = MEM_Null;
10351.309 (drh 24-Aug-07): pColName->db = p->db;
10361.308 (drh 23-Aug-07): pColName++;
10371.146 (drh 24-Sep-04): }
10381.66 (danielk1 26-May-04): }
10391.66 (danielk1 26-May-04):
10401.66 (danielk1 26-May-04): /*
10411.66 (danielk1 26-May-04): ** Set the name of the idx'th column to be returned by the SQL statement.
10421.66 (danielk1 26-May-04): ** zName must be a pointer to a nul terminated string.
10431.66 (danielk1 26-May-04): **
10441.66 (danielk1 26-May-04): ** This call must be made after a call to sqlite3VdbeSetNumCols().
10451.66 (danielk1 26-May-04): **
10461.105 (danielk1 12-Jun-04): ** If N==P3_STATIC it means that zName is a pointer to a constant static
10471.105 (danielk1 12-Jun-04): ** string and we can just copy the pointer. If it is P3_DYNAMIC, then
10481.299 (drh 16-Aug-07): ** the string is freed using sqlite3_free() when the vdbe is finished with
10491.105 (danielk1 12-Jun-04): ** it. Otherwise, N bytes of zName are copied.
10501.66 (danielk1 26-May-04): */
10511.236 (danielk1 10-Feb-06): int sqlite3VdbeSetColName(Vdbe *p, int idx, int var, const char *zName, int N){
10521.66 (danielk1 26-May-04): int rc;
10531.66 (danielk1 26-May-04): Mem *pColName;
10541.236 (danielk1 10-Feb-06): assert( idx<p->nResColumn );
10551.236 (danielk1 10-Feb-06): assert( var<COLNAME_N );
10561.299 (drh 16-Aug-07): if( p->db->mallocFailed ) return SQLITE_NOMEM;
10571.146 (drh 24-Sep-04): assert( p->aColName!=0 );
10581.236 (danielk1 10-Feb-06): pColName = &(p->aColName[idx+var*p->nResColumn]);
10591.105 (danielk1 12-Jun-04): if( N==P3_DYNAMIC || N==P3_STATIC ){
10601.307 (drh 21-Aug-07): rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, SQLITE_STATIC);
10611.66 (danielk1 26-May-04): }else{
10621.307 (drh 21-Aug-07): rc = sqlite3VdbeMemSetStr(pColName, zName, N, SQLITE_UTF8,SQLITE_TRANSIENT);
10631.66 (danielk1 26-May-04): }
10641.66 (danielk1 26-May-04): if( rc==SQLITE_OK && N==P3_DYNAMIC ){
10651.66 (danielk1 26-May-04): pColName->flags = (pColName->flags&(~MEM_Static))|MEM_Dyn;
10661.105 (danielk1 12-Jun-04): pColName->xDel = 0;
10671.66 (danielk1 26-May-04): }
10681.66 (danielk1 26-May-04): return rc;
10691.64 (danielk1 25-May-04): }
10701.64 (danielk1 25-May-04):
10711.91 (danielk1 03-Jun-04): /*
10721.91 (danielk1 03-Jun-04): ** A read or write transaction may or may not be active on database handle
10731.91 (danielk1 03-Jun-04): ** db. If a transaction is active, commit it. If there is a
10741.91 (danielk1 03-Jun-04): ** write-transaction spanning more than one database file, this routine
10751.91 (danielk1 03-Jun-04): ** takes care of the master journal trickery.
10761.91 (danielk1 03-Jun-04): */
10771.140 (drh 06-Sep-04): static int vdbeCommit(sqlite3 *db){
10781.91 (danielk1 03-Jun-04): int i;
10791.91 (danielk1 03-Jun-04): int nTrans = 0; /* Number of databases with an active write-transaction */
10801.91 (danielk1 03-Jun-04): int rc = SQLITE_OK;
10811.91 (danielk1 03-Jun-04): int needXcommit = 0;
10821.91 (danielk1 03-Jun-04):
10831.258 (danielk1 25-Jul-06): /* Before doing anything else, call the xSync() callback for any
10841.258 (danielk1 25-Jul-06): ** virtual module tables written in this transaction. This has to
10851.258 (danielk1 25-Jul-06): ** be done before determining whether a master journal file is
10861.258 (danielk1 25-Jul-06): ** required, as an xSync() callback may add an attached database
10871.258 (danielk1 25-Jul-06): ** to the transaction.
10881.258 (danielk1 25-Jul-06): */
10891.258 (danielk1 25-Jul-06): rc = sqlite3VtabSync(db, rc);
10901.258 (danielk1 25-Jul-06): if( rc!=SQLITE_OK ){
10911.258 (danielk1 25-Jul-06): return rc;
10921.258 (danielk1 25-Jul-06): }
10931.258 (danielk1 25-Jul-06):
10941.258 (danielk1 25-Jul-06): /* This loop determines (a) if the commit hook should be invoked and
10951.258 (danielk1 25-Jul-06): ** (b) how many database files have open write transactions, not
10961.258 (danielk1 25-Jul-06): ** including the temp database. (b) is important because if more than
10971.258 (danielk1 25-Jul-06): ** one database file has an open write transaction, a master journal
10981.258 (danielk1 25-Jul-06): ** file is required for an atomic commit.
10991.258 (danielk1 25-Jul-06): */
11001.91 (danielk1 03-Jun-04): for(i=0; i<db->nDb; i++){
11011.91 (danielk1 03-Jun-04): Btree *pBt = db->aDb[i].pBt;
11021.313 (drh 28-Aug-07): if( sqlite3BtreeIsInTrans(pBt) ){
11031.91 (danielk1 03-Jun-04): needXcommit = 1;
11041.91 (danielk1 03-Jun-04): if( i!=1 ) nTrans++;
11051.91 (danielk1 03-Jun-04): }
11061.91 (danielk1 03-Jun-04): }
11071.91 (danielk1 03-Jun-04):
11081.91 (danielk1 03-Jun-04): /* If there are any write-transactions at all, invoke the commit hook */
11091.91 (danielk1 03-Jun-04): if( needXcommit && db->xCommitCallback ){
11101.139 (drh 02-Sep-04): sqlite3SafetyOff(db);
11111.139 (drh 02-Sep-04): rc = db->xCommitCallback(db->pCommitArg);
11121.139 (drh 02-Sep-04): sqlite3SafetyOn(db);
11131.139 (drh 02-Sep-04): if( rc ){
11141.91 (danielk1 03-Jun-04): return SQLITE_CONSTRAINT;
11151.91 (danielk1 03-Jun-04): }
11161.91 (danielk1 03-Jun-04): }
11171.91 (danielk1 03-Jun-04):
11181.128 (danielk1 26-Jun-04): /* The simple case - no more than one database file (not counting the
11191.128 (danielk1 26-Jun-04): ** TEMP database) has a transaction active. There is no need for the
11201.94 (drh 07-Jun-04): ** master-journal.
11211.99 (drh 09-Jun-04): **
11221.128 (danielk1 26-Jun-04): ** If the return value of sqlite3BtreeGetFilename() is a zero length
11231.128 (danielk1 26-Jun-04): ** string, it means the main database is :memory:. In that case we do
11241.128 (danielk1 26-Jun-04): ** not support atomic multi-file commits, so use the simple case then
11251.99 (drh 09-Jun-04): ** too.
11261.91 (danielk1 03-Jun-04): */
11271.128 (danielk1 26-Jun-04): if( 0==strlen(sqlite3BtreeGetFilename(db->aDb[0].pBt)) || nTrans<=1 ){
11281.94 (drh 07-Jun-04): for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
11291.91 (danielk1 03-Jun-04): Btree *pBt = db->aDb[i].pBt;
11301.91 (danielk1 03-Jun-04): if( pBt ){
11311.277 (drh 30-Mar-07): rc = sqlite3BtreeCommitPhaseOne(pBt, 0);
11321.94 (drh 07-Jun-04): }
11331.94 (drh 07-Jun-04): }
11341.94 (drh 07-Jun-04):
11351.277 (drh 30-Mar-07): /* Do the commit only if all databases successfully complete phase 1.
11361.277 (drh 30-Mar-07): ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an
11371.277 (drh 30-Mar-07): ** IO error while deleting or truncating a journal file. It is unlikely,
11381.277 (drh 30-Mar-07): ** but could happen. In this case abandon processing and return the error.
11391.274 (danielk1 27-Mar-07): */
11401.274 (danielk1 27-Mar-07): for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
11411.274 (danielk1 27-Mar-07): Btree *pBt = db->aDb[i].pBt;
11421.274 (danielk1 27-Mar-07): if( pBt ){
11431.277 (drh 30-Mar-07): rc = sqlite3BtreeCommitPhaseTwo(pBt);
11441.274 (danielk1 27-Mar-07): }
11451.274 (danielk1 27-Mar-07): }
11461.94 (drh 07-Jun-04): if( rc==SQLITE_OK ){
11471.250 (danielk1 16-Jun-06): sqlite3VtabCommit(db);
11481.91 (danielk1 03-Jun-04): }
11491.91 (danielk1 03-Jun-04): }
11501.91 (danielk1 03-Jun-04):
11511.91 (danielk1 03-Jun-04): /* The complex case - There is a multi-file write-transaction active.
11521.91 (danielk1 03-Jun-04): ** This requires a master journal file to ensure the transaction is
11531.91 (danielk1 03-Jun-04): ** committed atomicly.
11541.91 (danielk1 03-Jun-04): */
11551.179 (danielk1 27-May-05): #ifndef SQLITE_OMIT_DISKIO
11561.91 (danielk1 03-Jun-04): else{
11571.302 (danielk1 17-Aug-07): sqlite3_vfs *pVfs = db->pVfs;
11581.188 (drh 27-Aug-05): int needSync = 0;
11591.91 (danielk1 03-Jun-04): char *zMaster = 0; /* File-name for the master journal */
11601.91 (danielk1 03-Jun-04): char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
11611.302 (danielk1 17-Aug-07): sqlite3_file *pMaster = 0;
11621.298 (danielk1 15-Aug-07): i64 offset = 0;
11631.91 (danielk1 03-Jun-04):
11641.91 (danielk1 03-Jun-04): /* Select a master journal file name */
11651.91 (danielk1 03-Jun-04): do {
11661.98 (drh 09-Jun-04): u32 random;
11671.299 (drh 16-Aug-07): sqlite3_free(zMaster);
11681.91 (danielk1 03-Jun-04): sqlite3Randomness(sizeof(random), &random);
11691.300 (danielk1 16-Aug-07): zMaster = sqlite3MPrintf(db, "%s-mj%08X", zMainFile, random&0x7fffffff);
11701.91 (danielk1 03-Jun-04): if( !zMaster ){
11711.91 (danielk1 03-Jun-04): return SQLITE_NOMEM;
11721.91 (danielk1 03-Jun-04): }
11731.302 (danielk1 17-Aug-07): }while( sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS) );
11741.91 (danielk1 03-Jun-04):
11751.91 (danielk1 03-Jun-04): /* Open the master journal. */
11761.303 (danielk1 18-Aug-07): rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster,
11771.303 (danielk1 18-Aug-07): SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
11781.305 (danielk1 20-Aug-07): SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0
11791.303 (danielk1 18-Aug-07): );
11801.91 (danielk1 03-Jun-04): if( rc!=SQLITE_OK ){
11811.299 (drh 16-Aug-07): sqlite3_free(zMaster);
11821.91 (danielk1 03-Jun-04): return rc;
11831.91 (danielk1 03-Jun-04): }
11841.91 (danielk1 03-Jun-04):
11851.91 (danielk1 03-Jun-04): /* Write the name of each database file in the transaction into the new
11861.91 (danielk1 03-Jun-04): ** master journal file. If an error occurs at this point close
11871.91 (danielk1 03-Jun-04): ** and delete the master journal file. All the individual journal files
11881.91 (danielk1 03-Jun-04): ** still have 'null' as the master journal pointer, so they will roll
11891.157 (danielk1 13-Jan-05): ** back independently if a failure occurs.
11901.91 (danielk1 03-Jun-04): */
11911.300 (danielk1 16-Aug-07): for(i=0; i<db->nDb; i++){
11921.91 (danielk1 03-Jun-04): Btree *pBt = db->aDb[i].pBt;
11931.99 (drh 09-Jun-04): if( i==1 ) continue; /* Ignore the TEMP database */
11941.313 (drh 28-Aug-07): if( sqlite3BtreeIsInTrans(pBt) ){
11951.109 (danielk1 14-Jun-04): char const *zFile = sqlite3BtreeGetJournalname(pBt);
11961.99 (drh 09-Jun-04): if( zFile[0]==0 ) continue; /* Ignore :memory: databases */
11971.188 (drh 27-Aug-05): if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){
11981.188 (drh 27-Aug-05): needSync = 1;
11991.188 (drh 27-Aug-05): }
12001.302 (danielk1 17-Aug-07): rc = sqlite3OsWrite(pMaster, zFile, strlen(zFile)+1, offset);
12011.298 (danielk1 15-Aug-07): offset += strlen(zFile)+1;
12021.91 (danielk1 03-Jun-04): if( rc!=SQLITE_OK ){
12031.303 (danielk1 18-Aug-07): sqlite3OsCloseFree(pMaster);
12041.303 (danielk1 18-Aug-07): sqlite3OsDelete(pVfs, zMaster, 0);
12051.299 (drh 16-Aug-07): sqlite3_free(zMaster);
12061.91 (danielk1 03-Jun-04): return rc;
12071.91 (danielk1 03-Jun-04): }
12081.91 (danielk1 03-Jun-04): }
12091.91 (danielk1 03-Jun-04): }
12101.91 (danielk1 03-Jun-04):
12111.310 (danielk1 24-Aug-07): /* Sync the master journal file. If the IOCAP_SEQUENTIAL device
12121.310 (danielk1 24-Aug-07): ** flag is set this is not required.
12131.310 (danielk1 24-Aug-07): */
12141.109 (danielk1 14-Jun-04): zMainFile = sqlite3BtreeGetDirname(db->aDb[0].pBt);
12151.304 (danielk1 20-Aug-07): if( (needSync
12161.310 (danielk1 24-Aug-07): && (0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL))
12171.304 (danielk1 20-Aug-07): && (rc=sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL))!=SQLITE_OK) ){
12181.303 (danielk1 18-Aug-07): sqlite3OsCloseFree(pMaster);
12191.303 (danielk1 18-Aug-07): sqlite3OsDelete(pVfs, zMaster, 0);
12201.299 (drh 16-Aug-07): sqlite3_free(zMaster);
12211.109 (danielk1 14-Jun-04): return rc;
12221.109 (danielk1 14-Jun-04): }
12231.91 (danielk1 03-Jun-04):
12241.91 (danielk1 03-Jun-04): /* Sync all the db files involved in the transaction. The same call
12251.91 (danielk1 03-Jun-04): ** sets the master journal pointer in each individual journal. If
12261.91 (danielk1 03-Jun-04): ** an error occurs here, do not delete the master journal file.
12271.91 (danielk1 03-Jun-04): **
12281.277 (drh 30-Mar-07): ** If the error occurs during the first call to
12291.277 (drh 30-Mar-07): ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the
12301.277 (drh 30-Mar-07): ** master journal file will be orphaned. But we cannot delete it,
12311.277 (drh 30-Mar-07): ** in case the master journal file name was written into the journal
12321.277 (drh 30-Mar-07): ** file before the failure occured.
12331.91 (danielk1 03-Jun-04): */
12341.258 (danielk1 25-Jul-06): for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
12351.91 (danielk1 03-Jun-04): Btree *pBt = db->aDb[i].pBt;
12361.313 (drh 28-Aug-07): if( pBt ){
12371.277 (drh 30-Mar-07): rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster);
12381.91 (danielk1 03-Jun-04): }
12391.91 (danielk1 03-Jun-04): }
12401.303 (danielk1 18-Aug-07): sqlite3OsCloseFree(pMaster);
12411.258 (danielk1 25-Jul-06): if( rc!=SQLITE_OK ){
12421.299 (drh 16-Aug-07): sqlite3_free(zMaster);
12431.258 (danielk1 25-Jul-06): return rc;
12441.258 (danielk1 25-Jul-06): }
12451.91 (danielk1 03-Jun-04):
12461.110 (danielk1 14-Jun-04): /* Delete the master journal file. This commits the transaction. After
12471.110 (danielk1 14-Jun-04): ** doing this the directory is synced again before any individual
12481.110 (danielk1 14-Jun-04): ** transaction files are deleted.
12491.110 (danielk1 14-Jun-04): */
12501.303 (danielk1 18-Aug-07): rc = sqlite3OsDelete(pVfs, zMaster, 1);
12511.299 (drh 16-Aug-07): sqlite3_free(zMaster);
12521.278 (drh 30-Mar-07): zMaster = 0;
12531.263 (drh 13-Aug-06): if( rc ){
12541.263 (drh 13-Aug-06): return rc;
12551.263 (drh 13-Aug-06): }
12561.91 (danielk1 03-Jun-04):
12571.91 (danielk1 03-Jun-04): /* All files and directories have already been synced, so the following
12581.277 (drh 30-Mar-07): ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and
12591.277 (drh 30-Mar-07): ** deleting or truncating journals. If something goes wrong while
12601.277 (drh 30-Mar-07): ** this is happening we don't really care. The integrity of the
12611.277 (drh 30-Mar-07): ** transaction is already guaranteed, but some stray 'cold' journals
12621.277 (drh 30-Mar-07): ** may be lying around. Returning an error code won't help matters.
12631.91 (danielk1 03-Jun-04): */
12641.274 (danielk1 27-Mar-07): disable_simulated_io_errors();
12651.91 (danielk1 03-Jun-04): for(i=0; i<db->nDb; i++){
12661.91 (danielk1 03-Jun-04): Btree *pBt = db->aDb[i].pBt;
12671.91 (danielk1 03-Jun-04): if( pBt ){
12681.277 (drh 30-Mar-07): sqlite3BtreeCommitPhaseTwo(pBt);
12691.91 (danielk1 03-Jun-04): }
12701.91 (danielk1 03-Jun-04): }
12711.274 (danielk1 27-Mar-07): enable_simulated_io_errors();
12721.274 (danielk1 27-Mar-07):
12731.250 (danielk1 16-Jun-06): sqlite3VtabCommit(db);
12741.91 (danielk1 03-Jun-04): }
12751.179 (danielk1 27-May-05): #endif
12761.112 (danielk1 14-Jun-04):
12771.94 (drh 07-Jun-04): return rc;
12781.91 (danielk1 03-Jun-04): }
12791.91 (danielk1 03-Jun-04):
12801.85 (danielk1 31-May-04): /*
12811.85 (danielk1 31-May-04): ** This routine checks that the sqlite3.activeVdbeCnt count variable
12821.85 (danielk1 31-May-04): ** matches the number of vdbe's in the list sqlite3.pVdbe that are
12831.85 (danielk1 31-May-04): ** currently active. An assertion fails if the two counts do not match.
12841.139 (drh 02-Sep-04): ** This is an internal self-check only - it is not an essential processing
12851.139 (drh 02-Sep-04): ** step.
12861.85 (danielk1 31-May-04): **
12871.85 (danielk1 31-May-04): ** This is a no-op if NDEBUG is defined.
12881.85 (danielk1 31-May-04): */
12891.85 (danielk1 31-May-04): #ifndef NDEBUG
12901.140 (drh 06-Sep-04): static void checkActiveVdbeCnt(sqlite3 *db){
12911.85 (danielk1 31-May-04): Vdbe *p;
12921.85 (danielk1 31-May-04): int cnt = 0;
12931.85 (danielk1 31-May-04): p = db->pVdbe;
12941.85 (danielk1 31-May-04): while( p ){
12951.139 (drh 02-Sep-04): if( p->magic==VDBE_MAGIC_RUN && p->pc>=0 ){
12961.85 (danielk1 31-May-04): cnt++;
12971.85 (danielk1 31-May-04): }
12981.85 (danielk1 31-May-04): p = p->pNext;
12991.85 (danielk1 31-May-04): }
13001.85 (danielk1 31-May-04): assert( cnt==db->activeVdbeCnt );
13011.85 (danielk1 31-May-04): }
13021.85 (danielk1 31-May-04): #else
13031.85 (danielk1 31-May-04): #define checkActiveVdbeCnt(x)
13041.85 (danielk1 31-May-04): #endif
13051.85 (danielk1 31-May-04):
13061.64 (danielk1 25-May-04): /*
13071.317 (drh 30-Aug-07): ** For every Btree that in database connection db which
13081.317 (drh 30-Aug-07): ** has been modified, "trip" or invalidate each cursor in
13091.317 (drh 30-Aug-07): ** that Btree might have been modified so that the cursor
13101.317 (drh 30-Aug-07): ** can never be used again. This happens when a rollback
13111.317 (drh 30-Aug-07): *** occurs. We have to trip all the other cursors, even
13121.317 (drh 30-Aug-07): ** cursor from other VMs in different database connections,
13131.317 (drh 30-Aug-07): ** so that none of them try to use the data at which they
13141.317 (drh 30-Aug-07): ** were pointing and which now may have been changed due
13151.317 (drh 30-Aug-07): ** to the rollback.
13161.317 (drh 30-Aug-07): **
13171.317 (drh 30-Aug-07): ** Remember that a rollback can delete tables complete and
13181.317 (drh 30-Aug-07): ** reorder rootpages. So it is not sufficient just to save
13191.317 (drh 30-Aug-07): ** the state of the cursor. We have to invalidate the cursor
13201.317 (drh 30-Aug-07): ** so that it is never used again.
13211.317 (drh 30-Aug-07): */
13221.317 (drh 30-Aug-07): void invalidateCursorsOnModifiedBtrees(sqlite3 *db){
13231.317 (drh 30-Aug-07): int i;
13241.317 (drh 30-Aug-07): for(i=0; i<db->nDb; i++){
13251.317 (drh 30-Aug-07): Btree *p = db->aDb[i].pBt;
13261.317 (drh 30-Aug-07): if( p && sqlite3BtreeIsInTrans(p) ){
13271.317 (drh 30-Aug-07): sqlite3BtreeTripAllCursors(p, SQLITE_ABORT);
13281.317 (drh 30-Aug-07): }
13291.252 (danielk1 23-Jun-06): }
13301.252 (danielk1 23-Jun-06): }
13311.252 (danielk1 23-Jun-06):
13321.252 (danielk1 23-Jun-06): /*
13331.139 (drh 02-Sep-04): ** This routine is called the when a VDBE tries to halt. If the VDBE
13341.139 (drh 02-Sep-04): ** has made changes and is in autocommit mode, then commit those
13351.139 (drh 02-Sep-04): ** changes. If a rollback is needed, then do the rollback.
13361.139 (drh 02-Sep-04): **
13371.139 (drh 02-Sep-04): ** This routine is the only way to move the state of a VM from
13381.316 (drh 29-Aug-07): ** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT. It is harmless to
13391.316 (drh 29-Aug-07): ** call this on a VM that is in the SQLITE_MAGIC_HALT state.
13401.1 (drh 06-Sep-03): **
13411.139 (drh 02-Sep-04): ** Return an error code. If the commit could not complete because of
13421.139 (drh 02-Sep-04): ** lock contention, return SQLITE_BUSY. If SQLITE_BUSY is returned, it
13431.139 (drh 02-Sep-04): ** means the close did not happen and needs to be repeated.
13441.1 (drh 06-Sep-03): */
13451.316 (drh 29-Aug-07): int sqlite3VdbeHalt(Vdbe *p){
13461.140 (drh 06-Sep-04): sqlite3 *db = p->db;
13471.1 (drh 06-Sep-03): int i;
13481.85 (danielk1 31-May-04): int (*xFunc)(Btree *pBt) = 0; /* Function to call on each btree backend */
13491.228 (danielk1 20-Jan-06): int isSpecialError; /* Set to true if SQLITE_NOMEM or IOERR */
13501.228 (danielk1 20-Jan-06):
13511.228 (danielk1 20-Jan-06): /* This function contains the logic that determines if a statement or
13521.228 (danielk1 20-Jan-06): ** transaction will be committed or rolled back as a result of the
13531.228 (danielk1 20-Jan-06): ** execution of this virtual machine.
13541.228 (danielk1 20-Jan-06): **
13551.228 (danielk1 20-Jan-06): ** Special errors:
13561.228 (danielk1 20-Jan-06): **
13571.228 (danielk1 20-Jan-06): ** If an SQLITE_NOMEM error has occured in a statement that writes to
13581.228 (danielk1 20-Jan-06): ** the database, then either a statement or transaction must be rolled
13591.228 (danielk1 20-Jan-06): ** back to ensure the tree-structures are in a consistent state. A
13601.228 (danielk1 20-Jan-06): ** statement transaction is rolled back if one is open, otherwise the
13611.228 (danielk1 20-Jan-06): ** entire transaction must be rolled back.
13621.228 (danielk1 20-Jan-06): **
13631.228 (danielk1 20-Jan-06): ** If an SQLITE_IOERR error has occured in a statement that writes to
13641.228 (danielk1 20-Jan-06): ** the database, then the entire transaction must be rolled back. The
13651.228 (danielk1 20-Jan-06): ** I/O error may have caused garbage to be written to the journal
13661.228 (danielk1 20-Jan-06): ** file. Were the transaction to continue and eventually be rolled
13671.228 (danielk1 20-Jan-06): ** back that garbage might end up in the database file.
13681.228 (danielk1 20-Jan-06): **
13691.228 (danielk1 20-Jan-06): ** In both of the above cases, the Vdbe.errorAction variable is
13701.228 (danielk1 20-Jan-06): ** ignored. If the sqlite3.autoCommit flag is false and a transaction
13711.228 (danielk1 20-Jan-06): ** is rolled back, it will be set to true.
13721.228 (danielk1 20-Jan-06): **
13731.228 (danielk1 20-Jan-06): ** Other errors:
13741.228 (danielk1 20-Jan-06): **
13751.228 (danielk1 20-Jan-06): ** No error:
13761.228 (danielk1 20-Jan-06): **
13771.228 (danielk1 20-Jan-06): */
13781.1 (drh 06-Sep-03):
13791.299 (drh 16-Aug-07): if( p->db->mallocFailed ){
13801.208 (danielk1 06-Dec-05): p->rc = SQLITE_NOMEM;
13811.208 (danielk1 06-Dec-05): }
13821.316 (drh 29-Aug-07): closeAllCursorsExceptActiveVtabs(p);
13831.139 (drh 02-Sep-04): if( p->magic!=VDBE_MAGIC_RUN ){
13841.139 (drh 02-Sep-04): return SQLITE_OK;
13851.1 (drh 06-Sep-03): }
13861.85 (danielk1 31-May-04): checkActiveVdbeCnt(db);
13871.208 (danielk1 06-Dec-05):
13881.228 (danielk1 20-Jan-06): /* No commit or rollback needed if the program never started */
13891.228 (danielk1 20-Jan-06): if( p->pc>=0 ){
13901.266 (drh 23-Sep-06): int mrc; /* Primary error code from p->rc */
13911.316 (drh 29-Aug-07):
13921.316 (drh 29-Aug-07): /* Lock all btrees used by the statement */
13931.316 (drh 29-Aug-07): sqlite3BtreeMutexArrayEnter(&p->aMutex);
13941.316 (drh 29-Aug-07):
13951.228 (danielk1 20-Jan-06): /* Check for one of the special errors - SQLITE_NOMEM or SQLITE_IOERR */
13961.266 (drh 23-Sep-06): mrc = p->rc & 0xff;
13971.291 (danielk1 13-Jun-07): isSpecialError = (
13981.291 (danielk1 13-Jun-07): (mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR || mrc==SQLITE_INTERRUPT)?1:0);
13991.228 (danielk1 20-Jan-06): if( isSpecialError ){
14001.208 (danielk1 06-Dec-05): /* This loop does static analysis of the query to see which of the
14011.208 (danielk1 06-Dec-05): ** following three categories it falls into:
14021.208 (danielk1 06-Dec-05): **
14031.208 (danielk1 06-Dec-05): ** Read-only
14041.228 (danielk1 20-Jan-06): ** Query with statement journal
14051.228 (danielk1 20-Jan-06): ** Query without statement journal
14061.208 (danielk1 06-Dec-05): **
14071.208 (danielk1 06-Dec-05): ** We could do something more elegant than this static analysis (i.e.
14081.208 (danielk1 06-Dec-05): ** store the type of query as part of the compliation phase), but
14091.228 (danielk1 20-Jan-06): ** handling malloc() or IO failure is a fairly obscure edge case so
14101.228 (danielk1 20-Jan-06): ** this is probably easier. Todo: Might be an opportunity to reduce
14111.228 (danielk1 20-Jan-06): ** code size a very small amount though...
14121.208 (danielk1 06-Dec-05): */
14131.208 (danielk1 06-Dec-05): int isReadOnly = 1;
14141.208 (danielk1 06-Dec-05): int isStatement = 0;
14151.208 (danielk1 06-Dec-05): assert(p->aOp || p->nOp==0);
14161.208 (danielk1 06-Dec-05): for(i=0; i<p->nOp; i++){
14171.208 (danielk1 06-Dec-05): switch( p->aOp[i].opcode ){
14181.208 (danielk1 06-Dec-05): case OP_Transaction:
14191.292 (danielk1 15-Jun-07): /* This is a bit strange. If we hit a malloc() or IO error and
14201.292 (danielk1 15-Jun-07): ** the statement did not open a statement transaction, we will
14211.292 (danielk1 15-Jun-07): ** rollback any active transaction and abort all other active
14221.292 (danielk1 15-Jun-07): ** statements. Or, if this is an SQLITE_INTERRUPT error, we
14231.292 (danielk1 15-Jun-07): ** will only rollback if the interrupted statement was a write.
14241.292 (danielk1 15-Jun-07): **
14251.292 (danielk1 15-Jun-07): ** It could be argued that read-only statements should never
14261.292 (danielk1 15-Jun-07): ** rollback anything. But careful analysis is required before
14271.292 (danielk1 15-Jun-07): ** making this change
14281.292 (danielk1 15-Jun-07): */
14291.292 (danielk1 15-Jun-07): if( p->aOp[i].p2 || mrc!=SQLITE_INTERRUPT ){
14301.292 (danielk1 15-Jun-07): isReadOnly = 0;
14311.292 (danielk1 15-Jun-07): }
14321.208 (danielk1 06-Dec-05): break;
14331.208 (danielk1 06-Dec-05): case OP_Statement:
14341.208 (danielk1 06-Dec-05): isStatement = 1;
14351.208 (danielk1 06-Dec-05): break;
14361.208 (danielk1 06-Dec-05): }
14371.208 (danielk1 06-Dec-05): }
14381.316 (drh 29-Aug-07):
14391.316 (drh 29-Aug-07):
14401.228 (danielk1 20-Jan-06): /* If the query was read-only, we need do no rollback at all. Otherwise,
14411.228 (danielk1 20-Jan-06): ** proceed with the special handling.
14421.228 (danielk1 20-Jan-06): */
14431.228 (danielk1 20-Jan-06): if( !isReadOnly ){
14441.290 (danielk1 13-Jun-07): if( p->rc==SQLITE_IOERR_BLOCKED && isStatement ){
14451.290 (danielk1 13-Jun-07): xFunc = sqlite3BtreeRollbackStmt;
14461.290 (danielk1 13-Jun-07): p->rc = SQLITE_BUSY;
14471.290 (danielk1 13-Jun-07): } else if( p->rc==SQLITE_NOMEM && isStatement ){
14481.228 (danielk1 20-Jan-06): xFunc = sqlite3BtreeRollbackStmt;
14491.228 (danielk1 20-Jan-06): }else{
14501.228 (danielk1 20-Jan-06): /* We are forced to roll back the active transaction. Before doing
14511.228 (danielk1 20-Jan-06): ** so, abort any other statements this handle currently has active.
14521.228 (danielk1 20-Jan-06): */
14531.317 (drh 30-Aug-07): invalidateCursorsOnModifiedBtrees(db);
14541.229 (danielk1 20-Jan-06): sqlite3RollbackAll(db);
14551.228 (danielk1 20-Jan-06): db->autoCommit = 1;
14561.228 (danielk1 20-Jan-06): }
14571.208 (danielk1 06-Dec-05): }
14581.208 (danielk1 06-Dec-05): }
14591.228 (danielk1 20-Jan-06):
14601.228 (danielk1 20-Jan-06): /* If the auto-commit flag is set and this is the only active vdbe, then
14611.228 (danielk1 20-Jan-06): ** we do either a commit or rollback of the current transaction.
14621.228 (danielk1 20-Jan-06): **
14631.228 (danielk1 20-Jan-06): ** Note: This block also runs if one of the special errors handled
14641.228 (danielk1 20-Jan-06): ** above has occured.
14651.228 (danielk1 20-Jan-06): */
14661.228 (danielk1 20-Jan-06): if( db->autoCommit && db->activeVdbeCnt==1 ){
14671.228 (danielk1 20-Jan-06): if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
14681.296 (drh 07-Aug-07): /* The auto-commit flag is true, and the vdbe program was
14691.228 (danielk1 20-Jan-06): ** successful or hit an 'OR FAIL' constraint. This means a commit
14701.228 (danielk1 20-Jan-06): ** is required.
14711.228 (danielk1 20-Jan-06): */
14721.228 (danielk1 20-Jan-06): int rc = vdbeCommit(db);
14731.228 (danielk1 20-Jan-06): if( rc==SQLITE_BUSY ){
14741.316 (drh 29-Aug-07): sqlite3BtreeMutexArrayLeave(&p->aMutex);
14751.228 (danielk1 20-Jan-06): return SQLITE_BUSY;
14761.228 (danielk1 20-Jan-06): }else if( rc!=SQLITE_OK ){
14771.228 (danielk1 20-Jan-06): p->rc = rc;
14781.229 (danielk1 20-Jan-06): sqlite3RollbackAll(db);
14791.228 (danielk1 20-Jan-06): }else{
14801.228 (danielk1 20-Jan-06): sqlite3CommitInternalChanges(db);
14811.228 (danielk1 20-Jan-06): }
14821.228 (danielk1 20-Jan-06): }else{
14831.229 (danielk1 20-Jan-06): sqlite3RollbackAll(db);
14841.228 (danielk1 20-Jan-06): }
14851.228 (danielk1 20-Jan-06): }else if( !xFunc ){
14861.228 (danielk1 20-Jan-06): if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
14871.294 (danielk1 27-Jun-07): if( p->openedStatement ){
14881.294 (danielk1 27-Jun-07): xFunc = sqlite3BtreeCommitStmt;
14891.294 (danielk1 27-Jun-07): }
14901.228 (danielk1 20-Jan-06): }else if( p->errorAction==OE_Abort ){
14911.228 (danielk1 20-Jan-06): xFunc = sqlite3BtreeRollbackStmt;
14921.228 (danielk1 20-Jan-06): }else{
14931.317 (drh 30-Aug-07): invalidateCursorsOnModifiedBtrees(db);
14941.229 (danielk1 20-Jan-06): sqlite3RollbackAll(db);
14951.228 (danielk1 20-Jan-06): db->autoCommit = 1;
14961.228 (danielk1 20-Jan-06): }
14971.228 (danielk1 20-Jan-06): }
14981.228 (danielk1 20-Jan-06):
14991.228 (danielk1 20-Jan-06): /* If xFunc is not NULL, then it is one of sqlite3BtreeRollbackStmt or
15001.228 (danielk1 20-Jan-06): ** sqlite3BtreeCommitStmt. Call it once on each backend. If an error occurs
15011.228 (danielk1 20-Jan-06): ** and the return code is still SQLITE_OK, set the return code to the new
15021.228 (danielk1 20-Jan-06): ** error value.
15031.228 (danielk1 20-Jan-06): */
15041.228 (danielk1 20-Jan-06): assert(!xFunc ||
15051.228 (danielk1 20-Jan-06): xFunc==sqlite3BtreeCommitStmt ||
15061.228 (danielk1 20-Jan-06): xFunc==sqlite3BtreeRollbackStmt
15071.228 (danielk1 20-Jan-06): );
15081.228 (danielk1 20-Jan-06): for(i=0; xFunc && i<db->nDb; i++){
15091.228 (danielk1 20-Jan-06): int rc;
15101.228 (danielk1 20-Jan-06): Btree *pBt = db->aDb[i].pBt;
15111.228 (danielk1 20-Jan-06): if( pBt ){
15121.228 (danielk1 20-Jan-06): rc = xFunc(pBt);
15131.231 (danielk1 23-Jan-06): if( rc && (p->rc==SQLITE_OK || p->rc==SQLITE_CONSTRAINT) ){
15141.231 (danielk1 23-Jan-06): p->rc = rc;
15151.231 (danielk1 23-Jan-06): sqlite3SetString(&p->zErrMsg, 0);
15161.231 (danielk1 23-Jan-06): }
15171.228 (danielk1 20-Jan-06): }
15181.85 (danielk1 31-May-04): }
15191.228 (danielk1 20-Jan-06):
15201.228 (danielk1 20-Jan-06): /* If this was an INSERT, UPDATE or DELETE and the statement was committed,
15211.228 (danielk1 20-Jan-06): ** set the change counter.
15221.228 (danielk1 20-Jan-06): */
15231.228 (danielk1 20-Jan-06): if( p->changeCntOn && p->pc>=0 ){
15241.228 (danielk1 20-Jan-06): if( !xFunc || xFunc==sqlite3BtreeCommitStmt ){
15251.228 (danielk1 20-Jan-06): sqlite3VdbeSetChanges(db, p->nChange);
15261.228 (danielk1 20-Jan-06): }else{
15271.228 (danielk1 20-Jan-06): sqlite3VdbeSetChanges(db, 0);
15281.228 (danielk1 20-Jan-06): }
15291.228 (danielk1 20-Jan-06): p->nChange = 0;
15301.87 (danielk1 31-May-04): }
15311.228 (danielk1 20-Jan-06):
15321.228 (danielk1 20-Jan-06): /* Rollback or commit any schema changes that occurred. */
15331.228 (danielk1 20-Jan-06): if( p->rc!=SQLITE_OK && db->flags&SQLITE_InternChanges ){
15341.228 (danielk1 20-Jan-06): sqlite3ResetInternalSchema(db, 0);
15351.228 (danielk1 20-Jan-06): db->flags = (db->flags | SQLITE_InternChanges);
15361.121 (danielk1 21-Jun-04): }
15371.316 (drh 29-Aug-07):
15381.316 (drh 29-Aug-07): /* Release the locks */
15391.316 (drh 29-Aug-07): sqlite3BtreeMutexArrayLeave(&p->aMutex);
15401.1 (drh 06-Sep-03): }
15411.85 (danielk1 31-May-04):
15421.254 (danielk1 24-Jun-06): /* We have successfully halted and closed the VM. Record this fact. */
15431.254 (danielk1 24-Jun-06): if( p->pc>=0 ){
15441.85 (danielk1 31-May-04): db->activeVdbeCnt--;
15451.1 (drh 06-Sep-03): }
15461.139 (drh 02-Sep-04): p->magic = VDBE_MAGIC_HALT;
15471.139 (drh 02-Sep-04): checkActiveVdbeCnt(db);
15481.316 (drh 29-Aug-07): if( p->db->mallocFailed ){
15491.316 (drh 29-Aug-07): p->rc = SQLITE_NOMEM;
15501.316 (drh 29-Aug-07): }
15511.316 (drh 29-Aug-07): checkActiveVdbeCnt(db);
15521.139 (drh 02-Sep-04):
15531.139 (drh 02-Sep-04): return SQLITE_OK;
15541.139 (drh 02-Sep-04): }
15551.314 (drh 28-Aug-07):
15561.139 (drh 02-Sep-04):
15571.139 (drh 02-Sep-04): /*
15581.270 (drh 09-Jan-07): ** Each VDBE holds the result of the most recent sqlite3_step() call
15591.270 (drh 09-Jan-07): ** in p->rc. This routine sets that result back to SQLITE_OK.
15601.270 (drh 09-Jan-07): */
15611.270 (drh 09-Jan-07): void sqlite3VdbeResetStepResult(Vdbe *p){
15621.270 (drh 09-Jan-07): p->rc = SQLITE_OK;
15631.270 (drh 09-Jan-07): }
15641.270 (drh 09-Jan-07):
15651.270 (drh 09-Jan-07): /*
15661.139 (drh 02-Sep-04): ** Clean up a VDBE after execution but do not delete the VDBE just yet.
15671.139 (drh 02-Sep-04): ** Write any error messages into *pzErrMsg. Return the result code.
15681.139 (drh 02-Sep-04): **
15691.139 (drh 02-Sep-04): ** After this routine is run, the VDBE should be ready to be executed
15701.139 (drh 02-Sep-04): ** again.
15711.139 (drh 02-Sep-04): **
15721.139 (drh 02-Sep-04): ** To look at it another way, this routine resets the state of the
15731.139 (drh 02-Sep-04): ** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to
15741.139 (drh 02-Sep-04): ** VDBE_MAGIC_INIT.
15751.139 (drh 02-Sep-04): */
15761.139 (drh 02-Sep-04): int sqlite3VdbeReset(Vdbe *p){
15771.265 (drh 15-Sep-06): sqlite3 *db;
15781.265 (drh 15-Sep-06): db = p->db;
15791.85 (danielk1 31-May-04):
15801.139 (drh 02-Sep-04): /* If the VM did not run to completion or if it encountered an
15811.139 (drh 02-Sep-04): ** error, then it might not have been halted properly. So halt
15821.139 (drh 02-Sep-04): ** it now.
15831.139 (drh 02-Sep-04): */
15841.265 (drh 15-Sep-06): sqlite3SafetyOn(db);
15851.139 (drh 02-Sep-04): sqlite3VdbeHalt(p);
15861.265 (drh 15-Sep-06): sqlite3SafetyOff(db);
15871.139 (drh 02-Sep-04):
15881.162 (drh 24-Jan-05): /* If the VDBE has be run even partially, then transfer the error code
15891.162 (drh 24-Jan-05): ** and error message from the VDBE into the main database structure. But
15901.162 (drh 24-Jan-05): ** if the VDBE has just been set to run but has not actually executed any
15911.162 (drh 24-Jan-05): ** instructions yet, leave the main database error information unchanged.
15921.139 (drh 02-Sep-04): */
15931.162 (drh 24-Jan-05): if( p->pc>=0 ){
15941.162 (drh 24-Jan-05): if( p->zErrMsg ){
15951.307 (drh 21-Aug-07): sqlite3ValueSetStr(db->pErr,-1,p->zErrMsg,SQLITE_UTF8,sqlite3_free);
15961.229 (danielk1 20-Jan-06): db->errCode = p->rc;
15971.162 (drh 24-Jan-05): p->zErrMsg = 0;
15981.162 (drh 24-Jan-05): }else if( p->rc ){
15991.265 (drh 15-Sep-06): sqlite3Error(db, p->rc, 0);
16001.162 (drh 24-Jan-05): }else{
16011.265 (drh 15-Sep-06): sqlite3Error(db, SQLITE_OK, 0);
16021.162 (drh 24-Jan-05): }
16031.163 (danielk1 24-Jan-05): }else if( p->rc && p->expired ){
16041.163 (danielk1 24-Jan-05): /* The expired flag was set on the VDBE before the first call
16051.163 (danielk1 24-Jan-05): ** to sqlite3_step(). For consistency (since sqlite3_step() was
16061.163 (danielk1 24-Jan-05): ** called), set the database error in this case as well.
16071.163 (danielk1 24-Jan-05): */
16081.265 (drh 15-Sep-06): sqlite3Error(db, p->rc, 0);
16091.139 (drh 02-Sep-04): }
16101.139 (drh 02-Sep-04):
16111.139 (drh 02-Sep-04): /* Reclaim all memory used by the VDBE
16121.139 (drh 02-Sep-04): */
16131.139 (drh 02-Sep-04): Cleanup(p);
16141.139 (drh 02-Sep-04):
16151.139 (drh 02-Sep-04): /* Save profiling information from this VDBE run.
16161.139 (drh 02-Sep-04): */
16171.208 (danielk1 06-Dec-05): assert( p->pTos<&p->aStack[p->pc<0?0:p->pc] || !p->aStack );
16181.1 (drh 06-Sep-03): #ifdef VDBE_PROFILE
16191.1 (drh 06-Sep-03): {
16201.1 (drh 06-Sep-03): FILE *out = fopen("vdbe_profile.out", "a");
16211.1 (drh 06-Sep-03): if( out ){
16221.1 (drh 06-Sep-03): int i;
16231.1 (drh 06-Sep-03): fprintf(out, "---- ");
16241.1 (drh 06-Sep-03): for(i=0; i<p->nOp; i++){
16251.1 (drh 06-Sep-03): fprintf(out, "%02x", p->aOp[i].opcode);
16261.1 (drh 06-Sep-03): }
16271.1 (drh 06-Sep-03): fprintf(out, "\n");
16281.1 (drh 06-Sep-03): for(i=0; i<p->nOp; i++){
16291.1 (drh 06-Sep-03): fprintf(out, "%6d %10lld %8lld ",
16301.1 (drh 06-Sep-03): p->aOp[i].cnt,
16311.1 (drh 06-Sep-03): p->aOp[i].cycles,
16321.1 (drh 06-Sep-03): p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
16331.1 (drh 06-Sep-03): );
16341.19 (danielk1 08-May-04): sqlite3VdbePrintOp(out, i, &p->aOp[i]);
16351.1 (drh 06-Sep-03): }
16361.1 (drh 06-Sep-03): fclose(out);
16371.1 (drh 06-Sep-03): }
16381.1 (drh 06-Sep-03): }
16391.1 (drh 06-Sep-03): #endif
16401.1 (drh 06-Sep-03): p->magic = VDBE_MAGIC_INIT;
16411.133 (drh 30-Jun-04): p->aborted = 0;
16421.265 (drh 15-Sep-06): return p->rc & db->errMask;
16431.1 (drh 06-Sep-03): }
16441.139 (drh 02-Sep-04):
16451.1 (drh 06-Sep-03): /*
16461.1 (drh 06-Sep-03): ** Clean up and delete a VDBE after execution. Return an integer which is
16471.1 (drh 06-Sep-03): ** the result code. Write any error message text into *pzErrMsg.
16481.1 (drh 06-Sep-03): */
16491.122 (danielk1 21-Jun-04): int sqlite3VdbeFinalize(Vdbe *p){
16501.129 (danielk1 26-Jun-04): int rc = SQLITE_OK;
16511.129 (danielk1 26-Jun-04): if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){
16521.129 (danielk1 26-Jun-04): rc = sqlite3VdbeReset(p);
16531.265 (drh 15-Sep-06): assert( (rc & p->db->errMask)==rc );
16541.129 (danielk1 26-Jun-04): }else if( p->magic!=VDBE_MAGIC_INIT ){
16551.1 (drh 06-Sep-03): return SQLITE_MISUSE;
16561.1 (drh 06-Sep-03): }
16571.19 (danielk1 08-May-04): sqlite3VdbeDelete(p);
16581.1 (drh 06-Sep-03): return rc;
16591.42 (danielk1 19-May-04): }
16601.1 (drh 06-Sep-03):
16611.1 (drh 06-Sep-03): /*
16621.120 (drh 19-Jun-04): ** Call the destructor for each auxdata entry in pVdbeFunc for which
16631.123 (danielk1 21-Jun-04): ** the corresponding bit in mask is clear. Auxdata entries beyond 31
16641.120 (drh 19-Jun-04): ** are always destroyed. To destroy all auxdata entries, call this
16651.123 (danielk1 21-Jun-04): ** routine with mask==0.
16661.120 (drh 19-Jun-04): */
16671.120 (drh 19-Jun-04): void sqlite3VdbeDeleteAuxData(VdbeFunc *pVdbeFunc, int mask){
16681.120 (drh 19-Jun-04): int i;
16691.120 (drh 19-Jun-04): for(i=0; i<pVdbeFunc->nAux; i++){
16701.120 (drh 19-Jun-04): struct AuxData *pAux = &pVdbeFunc->apAux[i];
16711.120 (drh 19-Jun-04): if( (i>31 || !(mask&(1<<i))) && pAux->pAux ){
16721.120 (drh 19-Jun-04): if( pAux->xDelete ){
16731.120 (drh 19-Jun-04): pAux->xDelete(pAux->pAux);
16741.120 (drh 19-Jun-04): }
16751.120 (drh 19-Jun-04): pAux->pAux = 0;
16761.120 (drh 19-Jun-04): }
16771.120 (drh 19-Jun-04): }
16781.120 (drh 19-Jun-04): }
16791.120 (drh 19-Jun-04):
16801.120 (drh 19-Jun-04): /*
16811.1 (drh 06-Sep-03): ** Delete an entire VDBE.
16821.1 (drh 06-Sep-03): */
16831.19 (danielk1 08-May-04): void sqlite3VdbeDelete(Vdbe *p){
16841.1 (drh 06-Sep-03): int i;
16851.1 (drh 06-Sep-03): if( p==0 ) return;
16861.1 (drh 06-Sep-03): Cleanup(p);
16871.1 (drh 06-Sep-03): if( p->pPrev ){
16881.1 (drh 06-Sep-03): p->pPrev->pNext = p->pNext;
16891.1 (drh 06-Sep-03): }else{
16901.1 (drh 06-Sep-03): assert( p->db->pVdbe==p );
16911.1 (drh 06-Sep-03): p->db->pVdbe = p->pNext;
16921.1 (drh 06-Sep-03): }
16931.1 (drh 06-Sep-03): if( p->pNext ){
16941.1 (drh 06-Sep-03): p->pNext->pPrev = p->pPrev;
16951.1 (drh 06-Sep-03): }
16961.146 (drh 24-Sep-04): if( p->aOp ){
16971.146 (drh 24-Sep-04): for(i=0; i<p->nOp; i++){
16981.146 (drh 24-Sep-04): Op *pOp = &p->aOp[i];
16991.198 (drh 16-Sep-05): freeP3(pOp->p3type, pOp->p3);
17001.92 (danielk1 05-Jun-04): }
17011.299 (drh 16-Aug-07): sqlite3_free(p->aOp);
17021.2 (drh 06-Sep-03): }
17031.146 (drh 24-Sep-04): releaseMemArray(p->aVar, p->nVar);
17041.299 (drh 16-Aug-07): sqlite3_free(p->aLabel);
17051.299 (drh 16-Aug-07): sqlite3_free(p->aStack);
17061.236 (danielk1 10-Feb-06): releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
17071.299 (drh 16-Aug-07): sqlite3_free(p->aColName);
17081.299 (drh 16-Aug-07): sqlite3_free(p->zSql);
17091.1 (drh 06-Sep-03): p->magic = VDBE_MAGIC_DEAD;
17101.299 (drh 16-Aug-07): sqlite3_free(p);
17111.1 (drh 06-Sep-03): }
17121.7 (drh 07-Jan-04):
17131.7 (drh 07-Jan-04): /*
17141.7 (drh 07-Jan-04): ** If a MoveTo operation is pending on the given cursor, then do that
17151.7 (drh 07-Jan-04): ** MoveTo now. Return an error code. If no MoveTo is pending, this
17161.7 (drh 07-Jan-04): ** routine does nothing and returns SQLITE_OK.
17171.7 (drh 07-Jan-04): */
17181.19 (danielk1 08-May-04): int sqlite3VdbeCursorMoveto(Cursor *p){
17191.7 (drh 07-Jan-04): if( p->deferredMoveto ){
17201.165 (drh 26-Jan-05): int res, rc;
17211.264 (adamd 14-Sep-06): #ifdef SQLITE_TEST
17221.24 (danielk1 10-May-04): extern int sqlite3_search_count;
17231.264 (adamd 14-Sep-06): #endif
17241.181 (drh 12-Jun-05): assert( p->isTable );
17251.280 (drh 04-Apr-07): rc = sqlite3BtreeMoveto(p->pCursor, 0, p->movetoTarget, 0, &res);
17261.165 (drh 26-Jan-05): if( rc ) return rc;
17271.50 (drh 20-May-04): *p->pIncrKey = 0;
17281.181 (drh 12-Jun-05): p->lastRowid = keyToInt(p->movetoTarget);
17291.181 (drh 12-Jun-05): p->rowidIsValid = res==0;
17301.7 (drh 07-Jan-04): if( res<0 ){
17311.165 (drh 26-Jan-05): rc = sqlite3BtreeNext(p->pCursor, &res);
17321.165 (drh 26-Jan-05): if( rc ) return rc;
17331.7 (drh 07-Jan-04): }
17341.262 (drh 08-Aug-06): #ifdef SQLITE_TEST
17351.24 (danielk1 10-May-04): sqlite3_search_count++;
17361.262 (drh 08-Aug-06): #endif
17371.7 (drh 07-Jan-04): p->deferredMoveto = 0;
17381.219 (drh 07-Jan-06): p->cacheStatus = CACHE_STALE;
17391.7 (drh 07-Jan-04): }
17401.7 (drh 07-Jan-04): return SQLITE_OK;
17411.7 (drh 07-Jan-04): }
17421.19 (danielk1 08-May-04):
17431.20 (drh 08-May-04): /*
17441.28 (danielk1 12-May-04): ** The following functions:
17451.23 (danielk1 10-May-04): **
17461.28 (danielk1 12-May-04): ** sqlite3VdbeSerialType()
17471.28 (danielk1 12-May-04): ** sqlite3VdbeSerialTypeLen()
17481.28 (danielk1 12-May-04): ** sqlite3VdbeSerialRead()
17491.23 (danielk1 10-May-04): ** sqlite3VdbeSerialLen()
17501.28 (danielk1 12-May-04): ** sqlite3VdbeSerialWrite()
17511.23 (danielk1 10-May-04): **
17521.23 (danielk1 10-May-04): ** encapsulate the code that serializes values for storage in SQLite
17531.28 (danielk1 12-May-04): ** data and index records. Each serialized value consists of a
17541.28 (danielk1 12-May-04): ** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
17551.28 (danielk1 12-May-04): ** integer, stored as a varint.
17561.23 (danielk1 10-May-04): **
17571.28 (danielk1 12-May-04): ** In an SQLite index record, the serial type is stored directly before
17581.28 (danielk1 12-May-04): ** the blob of data that it corresponds to. In a table record, all serial
17591.28 (danielk1 12-May-04): ** types are stored at the start of the record, and the blobs of data at
17601.28 (danielk1 12-May-04): ** the end. Hence these functions allow the caller to handle the
17611.28 (danielk1 12-May-04): ** serial-type and data blob seperately.
17621.28 (danielk1 12-May-04): **
17631.28 (danielk1 12-May-04): ** The following table describes the various storage classes for data:
17641.28 (danielk1 12-May-04): **
17651.28 (danielk1 12-May-04): ** serial type bytes of data type
17661.23 (danielk1 10-May-04): ** -------------- --------------- ---------------
17671.84 (drh 30-May-04): ** 0 0 NULL
17681.23 (danielk1 10-May-04): ** 1 1 signed integer
17691.23 (danielk1 10-May-04): ** 2 2 signed integer
17701.84 (drh 30-May-04): ** 3 3 signed integer
17711.84 (drh 30-May-04): ** 4 4 signed integer
17721.84 (drh 30-May-04): ** 5 6 signed integer
17731.84 (drh 30-May-04): ** 6 8 signed integer
17741.84 (drh 30-May-04): ** 7 8 IEEE float
17751.215 (drh 29-Dec-05): ** 8 0 Integer constant 0
17761.215 (drh 29-Dec-05): ** 9 0 Integer constant 1
17771.215 (drh 29-Dec-05): ** 10,11 reserved for expansion
17781.23 (danielk1 10-May-04): ** N>=12 and even (N-12)/2 BLOB
17791.23 (danielk1 10-May-04): ** N>=13 and odd (N-13)/2 text
17801.23 (danielk1 10-May-04): **
17811.217 (drh 02-Jan-06): ** The 8 and 9 types were added in 3.3.0, file format 4. Prior versions
17821.217 (drh 02-Jan-06): ** of SQLite will not understand those serial types.
17831.23 (danielk1 10-May-04): */
17841.23 (danielk1 10-May-04):
17851.23 (danielk1 10-May-04): /*
17861.28 (danielk1 12-May-04): ** Return the serial-type for the value stored in pMem.
17871.22 (danielk1 10-May-04): */
17881.215 (drh 29-Dec-05): u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){
17891.28 (danielk1 12-May-04): int flags = pMem->flags;
17901.283 (drh 02-May-07): int n;
17911.28 (danielk1 12-May-04):
17921.28 (danielk1 12-May-04): if( flags&MEM_Null ){
17931.84 (drh 30-May-04): return 0;
17941.23 (danielk1 10-May-04): }
17951.28 (danielk1 12-May-04): if( flags&MEM_Int ){
17961.158 (drh 20-Jan-05): /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
17971.175 (drh 15-Apr-05): # define MAX_6BYTE ((((i64)0x00001000)<<32)-1)
17981.276 (drh 30-Mar-07): i64 i = pMem->u.i;
17991.215 (drh 29-Dec-05): u64 u;
18001.215 (drh 29-Dec-05): if( file_format>=4 && (i&1)==i ){
18011.215 (drh 29-Dec-05): return 8+i;
18021.215 (drh 29-Dec-05): }
18031.215 (drh 29-Dec-05): u = i<0 ? -i : i;
18041.164 (drh 26-Jan-05): if( u<=127 ) return 1;
18051.164 (drh 26-Jan-05): if( u<=32767 ) return 2;
18061.164 (drh 26-Jan-05): if( u<=8388607 ) return 3;
18071.164 (drh 26-Jan-05): if( u<=2147483647 ) return 4;
18081.164 (drh 26-Jan-05): if( u<=MAX_6BYTE ) return 5;
18091.84 (drh 30-May-04): return 6;
18101.28 (danielk1 12-May-04): }
18111.28 (danielk1 12-May-04): if( flags&MEM_Real ){
18121.84 (drh 30-May-04): return 7;
18131.23 (danielk1 10-May-04): }
18141.283 (drh 02-May-07): assert( flags&(MEM_Str|MEM_Blob) );
18151.283 (drh 02-May-07): n = pMem->n;
18161.283 (drh 02-May-07): if( flags & MEM_Zero ){
18171.283 (drh 02-May-07): n += pMem->u.i;
18181.23 (danielk1 10-May-04): }
18191.283 (drh 02-May-07): assert( n>=0 );
18201.283 (drh 02-May-07): return ((n*2) + 12 + ((flags&MEM_Str)!=0));
18211.22 (danielk1 10-May-04): }
18221.22 (danielk1 10-May-04):
18231.22 (danielk1 10-May-04): /*
18241.28 (danielk1 12-May-04): ** Return the length of the data corresponding to the supplied serial-type.
18251.22 (danielk1 10-May-04): */
18261.75 (drh 28-May-04): int sqlite3VdbeSerialTypeLen(u32 serial_type){
18271.84 (drh 30-May-04): if( serial_type>=12 ){
18281.79 (drh 28-May-04): return (serial_type-12)/2;
18291.79 (drh 28-May-04): }else{
18301.150 (drh 06-Oct-04): static const u8 aSize[] = { 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 };
18311.79 (drh 28-May-04): return aSize[serial_type];
18321.79 (drh 28-May-04): }
18331.28 (danielk1 12-May-04): }
18341.23 (danielk1 10-May-04):
18351.28 (danielk1 12-May-04): /*
18361.284 (drh 04-May-07): ** If we are on an architecture with mixed-endian floating
18371.289 (drh 23-May-07): ** points (ex: ARM7) then swap the lower 4 bytes with the
18381.284 (drh 04-May-07): ** upper 4 bytes. Return the result.
18391.284 (drh 04-May-07): **
18401.289 (drh 23-May-07): ** For most architectures, this is a no-op.
18411.289 (drh 23-May-07): **
18421.289 (drh 23-May-07): ** (later): It is reported to me that the mixed-endian problem
18431.289 (drh 23-May-07): ** on ARM7 is an issue with GCC, not with the ARM7 chip. It seems
18441.289 (drh 23-May-07): ** that early versions of GCC stored the two words of a 64-bit
18451.289 (drh 23-May-07): ** float in the wrong order. And that error has been propagated
18461.289 (drh 23-May-07): ** ever since. The blame is not necessarily with GCC, though.
18471.289 (drh 23-May-07): ** GCC might have just copying the problem from a prior compiler.
18481.289 (drh 23-May-07): ** I am also told that newer versions of GCC that follow a different
18491.289 (drh 23-May-07): ** ABI get the byte order right.
18501.289 (drh 23-May-07): **
18511.289 (drh 23-May-07): ** Developers using SQLite on an ARM7 should compile and run their
18521.289 (drh 23-May-07): ** application using -DSQLITE_DEBUG=1 at least once. With DEBUG
18531.289 (drh 23-May-07): ** enabled, some asserts below will ensure that the byte order of
18541.289 (drh 23-May-07): ** floating point values is correct.
18551.318 (drh 30-Aug-07): **
18561.318 (drh 30-Aug-07): ** (2007-08-30) Frank van Vugt has studied this problem closely
18571.318 (drh 30-Aug-07): ** and has send his findings to the SQLite developers. Frank
18581.318 (drh 30-Aug-07): ** writes that some Linux kernels offer floating point hardware
18591.318 (drh 30-Aug-07): ** emulation that uses only 32-bit mantissas instead of a full
18601.318 (drh 30-Aug-07): ** 48-bits as required by the IEEE standard. (This is the
18611.318 (drh 30-Aug-07): ** CONFIG_FPE_FASTFPE option.) On such systems, floating point
18621.318 (drh 30-Aug-07): ** byte swapping becomes very complicated. To avoid problems,
18631.318 (drh 30-Aug-07): ** the necessary byte swapping is carried out using a 64-bit integer
18641.318 (drh 30-Aug-07): ** rather than a 64-bit float. Frank assures us that the code here
18651.318 (drh 30-Aug-07): ** works for him. We, the developers, have no way to independently
18661.318 (drh 30-Aug-07): ** verify this, but Frank seems to know what he is talking about
18671.318 (drh 30-Aug-07): ** so we trust him.
18681.284 (drh 04-May-07): */
18691.284 (drh 04-May-07): #ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
18701.318 (drh 30-Aug-07): static u64 floatSwap(u64 in){
18711.284 (drh 04-May-07): union {
18721.318 (drh 30-Aug-07): u64 r;
18731.284 (drh 04-May-07): u32 i[2];
18741.284 (drh 04-May-07): } u;
18751.284 (drh 04-May-07): u32 t;
18761.284 (drh 04-May-07):
18771.284 (drh 04-May-07): u.r = in;
18781.284 (drh 04-May-07): t = u.i[0];
18791.284 (drh 04-May-07): u.i[0] = u.i[1];
18801.284 (drh 04-May-07): u.i[1] = t;
18811.284 (drh 04-May-07): return u.r;
18821.284 (drh 04-May-07): }
18831.284 (drh 04-May-07): # define swapMixedEndianFloat(X) X = floatSwap(X)
18841.284 (drh 04-May-07): #else
18851.284 (drh 04-May-07): # define swapMixedEndianFloat(X)
18861.284 (drh 04-May-07): #endif
18871.284 (drh 04-May-07):
18881.284 (drh 04-May-07): /*
18891.28 (danielk1 12-May-04): ** Write the serialized data blob for the value stored in pMem into
18901.28 (danielk1 12-May-04): ** buf. It is assumed that the caller has allocated sufficient space.
18911.28 (danielk1 12-May-04): ** Return the number of bytes written.
18921.283 (drh 02-May-07): **
18931.283 (drh 02-May-07): ** nBuf is the amount of space left in buf[]. nBuf must always be
18941.283 (drh 02-May-07): ** large enough to hold the entire field. Except, if the field is
18951.283 (drh 02-May-07): ** a blob with a zero-filled tail, then buf[] might be just the right
18961.283 (drh 02-May-07): ** size to hold everything except for the zero-filled tail. If buf[]
18971.283 (drh 02-May-07): ** is only big enough to hold the non-zero prefix, then only write that
18981.283 (drh 02-May-07): ** prefix into buf[]. But if buf[] is large enough to hold both the
18991.283 (drh 02-May-07): ** prefix and the tail then write the prefix and set the tail to all
19001.283 (drh 02-May-07): ** zeros.
19011.283 (drh 02-May-07): **
19021.283 (drh 02-May-07): ** Return the number of bytes actually written into buf[]. The number
19031.283 (drh 02-May-07): ** of bytes in the zero-filled tail is included in the return value only
19041.283 (drh 02-May-07): ** if those bytes were zeroed in buf[].
19051.28 (danielk1 12-May-04): */
19061.283 (drh 02-May-07): int sqlite3VdbeSerialPut(u8 *buf, int nBuf, Mem *pMem, int file_format){
19071.215 (drh 29-Dec-05): u32 serial_type = sqlite3VdbeSerialType(pMem, file_format);
19081.28 (danielk1 12-May-04): int len;
19091.30 (danielk1 13-May-04):
19101.54 (drh 21-May-04): /* Integer and Real */
19111.215 (drh 29-Dec-05): if( serial_type<=7 && serial_type>0 ){
19121.54 (drh 21-May-04): u64 v;
19131.54 (drh 21-May-04): int i;
19141.84 (drh 30-May-04): if( serial_type==7 ){
19151.272 (drh 26-Mar-07): assert( sizeof(v)==sizeof(pMem->r) );
19161.272 (drh 26-Mar-07): memcpy(&v, &pMem->r, sizeof(v));
19171.318 (drh 30-Aug-07): swapMixedEndianFloat(v);
19181.54 (drh 21-May-04): }else{
19191.276 (drh 30-Mar-07): v = pMem->u.i;
19201.54 (drh 21-May-04): }
19211.54 (drh 21-May-04): len = i = sqlite3VdbeSerialTypeLen(serial_type);
19221.283 (drh 02-May-07): assert( len<=nBuf );
19231.54 (drh 21-May-04): while( i-- ){
19241.54 (drh 21-May-04): buf[i] = (v&0xFF);
19251.54 (drh 21-May-04): v >>= 8;
19261.54 (drh 21-May-04): }
19271.54 (drh 21-May-04): return len;
19281.28 (danielk1 12-May-04): }
19291.215 (drh 29-Dec-05):
19301.28 (danielk1 12-May-04): /* String or blob */
19311.215 (drh 29-Dec-05): if( serial_type>=12 ){
19321.283 (drh 02-May-07): assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.i:0)
19331.283 (drh 02-May-07): == sqlite3VdbeSerialTypeLen(serial_type) );
19341.283 (drh 02-May-07): assert( pMem->n<=nBuf );
19351.283 (drh 02-May-07): len = pMem->n;
19361.215 (drh 29-Dec-05): memcpy(buf, pMem->z, len);
19371.283 (drh 02-May-07): if( pMem->flags & MEM_Zero ){
19381.283 (drh 02-May-07): len += pMem->u.i;
19391.283 (drh 02-May-07): if( len>nBuf ){
19401.283 (drh 02-May-07): len = nBuf;
19411.283 (drh 02-May-07): }
19421.283 (drh 02-May-07): memset(&buf[pMem->n], 0, len-pMem->n);
19431.283 (drh 02-May-07): }
19441.215 (drh 29-Dec-05): return len;
19451.215 (drh 29-Dec-05): }
19461.215 (drh 29-Dec-05):
19471.215 (drh 29-Dec-05): /* NULL or constants 0 or 1 */
19481.215 (drh 29-Dec-05): return 0;
19491.22 (danielk1 10-May-04): }
19501.22 (danielk1 10-May-04):
19511.22 (danielk1 10-May-04): /*
19521.28 (danielk1 12-May-04): ** Deserialize the data blob pointed to by buf as serial type serial_type
19531.28 (danielk1 12-May-04): ** and store the result in pMem. Return the number of bytes read.
19541.28 (danielk1 12-May-04): */
19551.55 (danielk1 22-May-04): int sqlite3VdbeSerialGet(
19561.58 (danielk1 23-May-04): const unsigned char *buf, /* Buffer to deserialize from */
19571.75 (drh 28-May-04): u32 serial_type, /* Serial type to deserialize */
19581.75 (drh 28-May-04): Mem *pMem /* Memory cell to write value into */
19591.55 (danielk1 22-May-04): ){
19601.177 (drh 21-May-05): switch( serial_type ){
19611.177 (drh 21-May-05): case 10: /* Reserved for future use */
19621.177 (drh 21-May-05): case 11: /* Reserved for future use */
19631.177 (drh 21-May-05): case 0: { /* NULL */
19641.177 (drh 21-May-05): pMem->flags = MEM_Null;
19651.177 (drh 21-May-05): break;
19661.177 (drh 21-May-05): }
19671.177 (drh 21-May-05): case 1: { /* 1-byte signed integer */
19681.276 (drh 30-Mar-07): pMem->u.i = (signed char)buf[0];
19691.177 (drh 21-May-05): pMem->flags = MEM_Int;
19701.177 (drh 21-May-05): return 1;
19711.177 (drh 21-May-05): }
19721.177 (drh 21-May-05): case 2: { /* 2-byte signed integer */
19731.276 (drh 30-Mar-07): pMem->u.i = (((signed char)buf[0])<<8) | buf[1];
19741.177 (drh 21-May-05): pMem->flags = MEM_Int;
19751.177 (drh 21-May-05): return 2;
19761.177 (drh 21-May-05): }
19771.177 (drh 21-May-05): case 3: { /* 3-byte signed integer */
19781.276 (drh 30-Mar-07): pMem->u.i = (((signed char)buf[0])<<16) | (buf[1]<<8) | buf[2];
19791.177 (drh 21-May-05): pMem->flags = MEM_Int;
19801.177 (drh 21-May-05): return 3;
19811.177 (drh 21-May-05): }
19821.177 (drh 21-May-05): case 4: { /* 4-byte signed integer */
19831.276 (drh 30-Mar-07): pMem->u.i = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
19841.177 (drh 21-May-05): pMem->flags = MEM_Int;
19851.177 (drh 21-May-05): return 4;
19861.177 (drh 21-May-05): }
19871.177 (drh 21-May-05): case 5: { /* 6-byte signed integer */
19881.177 (drh 21-May-05): u64 x = (((signed char)buf[0])<<8) | buf[1];
19891.177 (drh 21-May-05): u32 y = (buf[2]<<24) | (buf[3]<<16) | (buf[4]<<8) | buf[5];
19901.177 (drh 21-May-05): x = (x<<32) | y;
19911.276 (drh 30-Mar-07): pMem->u.i = *(i64*)&x;
19921.83 (drh 30-May-04): pMem->flags = MEM_Int;
19931.177 (drh 21-May-05): return 6;
19941.177 (drh 21-May-05): }
19951.186 (drh 18-Aug-05): case 6: /* 8-byte signed integer */
19961.177 (drh 21-May-05): case 7: { /* IEEE floating point */
19971.193 (drh 05-Sep-05): u64 x;
19981.193 (drh 05-Sep-05): u32 y;
19991.232 (drh 23-Jan-06): #if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT)
20001.189 (drh 28-Aug-05): /* Verify that integers and floating point values use the same
20011.284 (drh 04-May-07): ** byte order. Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is
20021.284 (drh 04-May-07): ** defined that 64-bit floating point values really are mixed
20031.284 (drh 04-May-07): ** endian.
20041.190 (drh 28-Aug-05): */
20051.189 (drh 28-Aug-05): static const u64 t1 = ((u64)0x3ff00000)<<32;
20061.272 (drh 26-Mar-07): static const double r1 = 1.0;
20071.318 (drh 30-Aug-07): u64 t2 = t1;
20081.318 (drh 30-Aug-07): swapMixedEndianFloat(t2);
20091.318 (drh 30-Aug-07): assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
20101.189 (drh 28-Aug-05): #endif
20111.190 (drh 28-Aug-05):
20121.193 (drh 05-Sep-05): x = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
20131.193 (drh 05-Sep-05): y = (buf[4]<<24) | (buf[5]<<16) | (buf[6]<<8) | buf[7];
20141.177 (drh 21-May-05): x = (x<<32) | y;
20151.177 (drh 21-May-05): if( serial_type==6 ){
20161.276 (drh 30-Mar-07): pMem->u.i = *(i64*)&x;
20171.177 (drh 21-May-05): pMem->flags = MEM_Int;
20181.177 (drh 21-May-05): }else{
20191.272 (drh 26-Mar-07): assert( sizeof(x)==8 && sizeof(pMem->r)==8 );
20201.318 (drh 30-Aug-07): swapMixedEndianFloat(x);
20211.272 (drh 26-Mar-07): memcpy(&pMem->r, &x, sizeof(x));
20221.177 (drh 21-May-05): pMem->flags = MEM_Real;
20231.83 (drh 30-May-04): }
20241.177 (drh 21-May-05): return 8;
20251.177 (drh 21-May-05): }
20261.215 (drh 29-Dec-05): case 8: /* Integer 0 */
20271.215 (drh 29-Dec-05): case 9: { /* Integer 1 */
20281.276 (drh 30-Mar-07): pMem->u.i = serial_type-8;
20291.215 (drh 29-Dec-05): pMem->flags = MEM_Int;
20301.215 (drh 29-Dec-05): return 0;
20311.215 (drh 29-Dec-05): }
20321.177 (drh 21-May-05): default: {
20331.177 (drh 21-May-05): int len = (serial_type-12)/2;
20341.177 (drh 21-May-05): pMem->z = (char *)buf;
20351.177 (drh 21-May-05): pMem->n = len;
20361.177 (drh 21-May-05): pMem->xDel = 0;
20371.177 (drh 21-May-05): if( serial_type&0x01 ){
20381.177 (drh 21-May-05): pMem->flags = MEM_Str | MEM_Ephem;
20391.83 (drh 30-May-04): }else{
20401.177 (drh 21-May-05): pMem->flags = MEM_Blob | MEM_Ephem;
20411.83 (drh 30-May-04): }
20421.177 (drh 21-May-05): return len;
20431.81 (drh 30-May-04): }
20441.23 (danielk1 10-May-04): }
20451.177 (drh 21-May-05): return 0;
20461.22 (danielk1 10-May-04): }
20471.22 (danielk1 10-May-04):
20481.22 (danielk1 10-May-04): /*
20491.223 (drh 12-Jan-06): ** The header of a record consists of a sequence variable-length integers.
20501.223 (drh 12-Jan-06): ** These integers are almost always small and are encoded as a single byte.
20511.223 (drh 12-Jan-06): ** The following macro takes advantage this fact to provide a fast decode
20521.223 (drh 12-Jan-06): ** of the integers in a record header. It is faster for the common case
20531.223 (drh 12-Jan-06): ** where the integer is a single byte. It is a little slower when the
20541.223 (drh 12-Jan-06): ** integer is two or more bytes. But overall it is faster.
20551.223 (drh 12-Jan-06): **
20561.223 (drh 12-Jan-06): ** The following expressions are equivalent:
20571.223 (drh 12-Jan-06): **
20581.223 (drh 12-Jan-06): ** x = sqlite3GetVarint32( A, &B );
20591.223 (drh 12-Jan-06): **
20601.223 (drh 12-Jan-06): ** x = GetVarint( A, B );
20611.223 (drh 12-Jan-06): **
20621.223 (drh 12-Jan-06): */
20631.223 (drh 12-Jan-06): #define GetVarint(A,B) ((B = *(A))<=0x7f ? 1 : sqlite3GetVarint32(A, &B))
20641.223 (drh 12-Jan-06):
20651.223 (drh 12-Jan-06): /*
20661.90 (drh 02-Jun-04): ** This function compares the two table rows or index records specified by
20671.38 (danielk1 18-May-04): ** {nKey1, pKey1} and {nKey2, pKey2}, returning a negative, zero
20681.38 (danielk1 18-May-04): ** or positive integer if {nKey1, pKey1} is less than, equal to or
20691.90 (drh 02-Jun-04): ** greater than {nKey2, pKey2}. Both Key1 and Key2 must be byte strings
20701.90 (drh 02-Jun-04): ** composed by the OP_MakeRecord opcode of the VDBE.
20711.38 (danielk1 18-May-04): */
20721.90 (drh 02-Jun-04): int sqlite3VdbeRecordCompare(
20731.38 (danielk1 18-May-04): void *userData,
20741.38 (danielk1 18-May-04): int nKey1, const void *pKey1,
20751.38 (danielk1 18-May-04): int nKey2, const void *pKey2
20761.38 (danielk1 18-May-04): ){
20771.50 (drh 20-May-04): KeyInfo *pKeyInfo = (KeyInfo*)userData;
20781.73 (drh 27-May-04): u32 d1, d2; /* Offset into aKey[] of next data element */
20791.73 (drh 27-May-04): u32 idx1, idx2; /* Offset into aKey[] of next header element */
20801.73 (drh 27-May-04): u32 szHdr1, szHdr2; /* Number of bytes in header */
20811.73 (drh 27-May-04): int i = 0;
20821.73 (drh 27-May-04): int nField;
20831.73 (drh 27-May-04): int rc = 0;
20841.38 (danielk1 18-May-04): const unsigned char *aKey1 = (const unsigned char *)pKey1;
20851.38 (danielk1 18-May-04): const unsigned char *aKey2 = (const unsigned char *)pKey2;
20861.96 (danielk1 09-Jun-04):
20871.96 (danielk1 09-Jun-04): Mem mem1;
20881.96 (danielk1 09-Jun-04): Mem mem2;
20891.96 (danielk1 09-Jun-04): mem1.enc = pKeyInfo->enc;
20901.307 (drh 21-Aug-07): mem1.db = pKeyInfo->db;
20911.96 (danielk1 09-Jun-04): mem2.enc = pKeyInfo->enc;
20921.307 (drh 21-Aug-07): mem2.db = pKeyInfo->db;
20931.73 (drh 27-May-04):
20941.223 (drh 12-Jan-06): idx1 = GetVarint(aKey1, szHdr1);
20951.73 (drh 27-May-04): d1 = szHdr1;
20961.223 (drh 12-Jan-06): idx2 = GetVarint(aKey2, szHdr2);
20971.73 (drh 27-May-04): d2 = szHdr2;
20981.73 (drh 27-May-04): nField = pKeyInfo->nField;
20991.76 (drh 28-May-04): while( idx1<szHdr1 && idx2<szHdr2 ){
21001.73 (drh 27-May-04): u32 serial_type1;
21011.73 (drh 27-May-04): u32 serial_type2;
21021.39 (danielk1 18-May-04):
21031.39 (danielk1 18-May-04): /* Read the serial types for the next element in each key. */
21041.223 (drh 12-Jan-06): idx1 += GetVarint( aKey1+idx1, serial_type1 );
21051.76 (drh 28-May-04): if( d1>=nKey1 && sqlite3VdbeSerialTypeLen(serial_type1)>0 ) break;
21061.223 (drh 12-Jan-06): idx2 += GetVarint( aKey2+idx2, serial_type2 );
21071.76 (drh 28-May-04): if( d2>=nKey2 && sqlite3VdbeSerialTypeLen(serial_type2)>0 ) break;
21081.39 (danielk1 18-May-04):
21091.267 (drh 27-Oct-06): /* Extract the values to be compared.
21101.39 (danielk1 18-May-04): */
21111.75 (drh 28-May-04): d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
21121.75 (drh 28-May-04): d2 += sqlite3VdbeSerialGet(&aKey2[d2], serial_type2, &mem2);
21131.39 (danielk1 18-May-04):
21141.267 (drh 27-Oct-06): /* Do the comparison
21151.267 (drh 27-Oct-06): */
21161.76 (drh 28-May-04): rc = sqlite3MemCompare(&mem1, &mem2, i<nField ? pKeyInfo->aColl[i] : 0);
21171.177 (drh 21-May-05): if( mem1.flags & MEM_Dyn ) sqlite3VdbeMemRelease(&mem1);
21181.177 (drh 21-May-05): if( mem2.flags & MEM_Dyn ) sqlite3VdbeMemRelease(&mem2);
21191.39 (danielk1 18-May-04): if( rc!=0 ){
21201.73 (drh 27-May-04): break;
21211.39 (danielk1 18-May-04): }
21221.73 (drh 27-May-04): i++;
21231.73 (drh 27-May-04): }
21241.73 (drh 27-May-04):
21251.73 (drh 27-May-04): /* One of the keys ran out of fields, but all the fields up to that point
21261.73 (drh 27-May-04): ** were equal. If the incrKey flag is true, then the second key is
21271.73 (drh 27-May-04): ** treated as larger.
21281.73 (drh 27-May-04): */
21291.73 (drh 27-May-04): if( rc==0 ){
21301.73 (drh 27-May-04): if( pKeyInfo->incrKey ){
21311.73 (drh 27-May-04): rc = -1;
21321.73 (drh 27-May-04): }else if( d1<nKey1 ){
21331.73 (drh 27-May-04): rc = 1;
21341.73 (drh 27-May-04): }else if( d2<nKey2 ){
21351.73 (drh 27-May-04): rc = -1;
21361.73 (drh 27-May-04): }
21371.214 (drh 21-Dec-05): }else if( pKeyInfo->aSortOrder && i<pKeyInfo->nField
21381.214 (drh 21-Dec-05): && pKeyInfo->aSortOrder[i] ){
21391.73 (drh 27-May-04): rc = -rc;
21401.39 (danielk1 18-May-04): }
21411.39 (danielk1 18-May-04):
21421.73 (drh 27-May-04): return rc;
21431.38 (danielk1 18-May-04): }
21441.76 (drh 28-May-04):
21451.76 (drh 28-May-04): /*
21461.90 (drh 02-Jun-04): ** The argument is an index entry composed using the OP_MakeRecord opcode.
21471.90 (drh 02-Jun-04): ** The last entry in this record should be an integer (specifically
21481.90 (drh 02-Jun-04): ** an integer rowid). This routine returns the number of bytes in
21491.90 (drh 02-Jun-04): ** that integer.
21501.76 (drh 28-May-04): */
21511.237 (drh 24-Feb-06): int sqlite3VdbeIdxRowidLen(const u8 *aKey){
21521.76 (drh 28-May-04): u32 szHdr; /* Size of the header */
21531.76 (drh 28-May-04): u32 typeRowid; /* Serial type of the rowid */
21541.76 (drh 28-May-04):
21551.76 (drh 28-May-04): sqlite3GetVarint32(aKey, &szHdr);
21561.76 (drh 28-May-04): sqlite3GetVarint32(&aKey[szHdr-1], &typeRowid);
21571.76 (drh 28-May-04): return sqlite3VdbeSerialTypeLen(typeRowid);
21581.76 (drh 28-May-04): }
21591.38 (danielk1 18-May-04):
21601.30 (danielk1 13-May-04):
21611.30 (danielk1 13-May-04): /*
21621.90 (drh 02-Jun-04): ** pCur points at an index entry created using the OP_MakeRecord opcode.
21631.90 (drh 02-Jun-04): ** Read the rowid (the last field in the record) and store it in *rowid.
21641.90 (drh 02-Jun-04): ** Return SQLITE_OK if everything works, or an error code otherwise.
21651.30 (danielk1 13-May-04): */
21661.307 (drh 21-Aug-07): int sqlite3VdbeIdxRowid(BtCursor *pCur, i64 *rowid){
21671.279 (drh 01-Apr-07): i64 nCellKey = 0;
21681.30 (danielk1 13-May-04): int rc;
21691.76 (drh 28-May-04): u32 szHdr; /* Size of the header */
21701.76 (drh 28-May-04): u32 typeRowid; /* Serial type of the rowid */
21711.76 (drh 28-May-04): u32 lenRowid; /* Size of the rowid */
21721.76 (drh 28-May-04): Mem m, v;
21731.30 (danielk1 13-May-04):
21741.76 (drh 28-May-04): sqlite3BtreeKeySize(pCur, &nCellKey);
21751.76 (drh 28-May-04): if( nCellKey<=0 ){
21761.200 (drh 17-Sep-05): return SQLITE_CORRUPT_BKPT;
21771.76 (drh 28-May-04): }
21781.307 (drh 21-Aug-07): rc = sqlite3VdbeMemFromBtree(pCur, 0, nCellKey, 1, &m);
21791.76 (drh 28-May-04): if( rc ){
21801.30 (danielk1 13-May-04): return rc;
21811.30 (danielk1 13-May-04): }
21821.210 (drh 09-Dec-05): sqlite3GetVarint32((u8*)m.z, &szHdr);
21831.210 (drh 09-Dec-05): sqlite3GetVarint32((u8*)&m.z[szHdr-1], &typeRowid);
21841.76 (drh 28-May-04): lenRowid = sqlite3VdbeSerialTypeLen(typeRowid);
21851.210 (drh 09-Dec-05): sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
21861.276 (drh 30-Mar-07): *rowid = v.u.i;
21871.105 (danielk1 12-Jun-04): sqlite3VdbeMemRelease(&m);
21881.30 (danielk1 13-May-04): return SQLITE_OK;
21891.30 (danielk1 13-May-04): }
21901.30 (danielk1 13-May-04):
21911.44 (drh 19-May-04): /*
21921.50 (drh 20-May-04): ** Compare the key of the index entry that cursor pC is point to against
21931.44 (drh 19-May-04): ** the key string in pKey (of length nKey). Write into *pRes a number
21941.44 (drh 19-May-04): ** that is negative, zero, or positive if pC is less than, equal to,
21951.44 (drh 19-May-04): ** or greater than pKey. Return SQLITE_OK on success.
21961.50 (drh 20-May-04): **
21971.76 (drh 28-May-04): ** pKey is either created without a rowid or is truncated so that it
21981.76 (drh 28-May-04): ** omits the rowid at the end. The rowid at the end of the index entry
21991.76 (drh 28-May-04): ** is ignored as well.
22001.44 (drh 19-May-04): */
22011.30 (danielk1 13-May-04): int sqlite3VdbeIdxKeyCompare(
22021.44 (drh 19-May-04): Cursor *pC, /* The cursor to compare against */
22031.44 (drh 19-May-04): int nKey, const u8 *pKey, /* The key to compare */
22041.44 (drh 19-May-04): int *res /* Write the comparison result here */
22051.30 (danielk1 13-May-04): ){
22061.279 (drh 01-Apr-07): i64 nCellKey = 0;
22071.30 (danielk1 13-May-04): int rc;
22081.31 (danielk1 14-May-04): BtCursor *pCur = pC->pCursor;
22091.76 (drh 28-May-04): int lenRowid;
22101.76 (drh 28-May-04): Mem m;
22111.30 (danielk1 13-May-04):
22121.30 (danielk1 13-May-04): sqlite3BtreeKeySize(pCur, &nCellKey);
22131.30 (danielk1 13-May-04): if( nCellKey<=0 ){
22141.30 (danielk1 13-May-04): *res = 0;
22151.30 (danielk1 13-May-04): return SQLITE_OK;
22161.30 (danielk1 13-May-04): }
22171.307 (drh 21-Aug-07): rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, nCellKey, 1, &m);
22181.76 (drh 28-May-04): if( rc ){
22191.76 (drh 28-May-04): return rc;
22201.30 (danielk1 13-May-04): }
22211.237 (drh 24-Feb-06): lenRowid = sqlite3VdbeIdxRowidLen((u8*)m.z);
22221.90 (drh 02-Jun-04): *res = sqlite3VdbeRecordCompare(pC->pKeyInfo, m.n-lenRowid, m.z, nKey, pKey);
22231.105 (danielk1 12-Jun-04): sqlite3VdbeMemRelease(&m);
22241.30 (danielk1 13-May-04): return SQLITE_OK;
22251.62 (danielk1 25-May-04): }
22261.121 (danielk1 21-Jun-04):
22271.121 (danielk1 21-Jun-04): /*
22281.121 (danielk1 21-Jun-04): ** This routine sets the value to be returned by subsequent calls to
22291.121 (danielk1 21-Jun-04): ** sqlite3_changes() on the database handle 'db'.
22301.121 (danielk1 21-Jun-04): */
22311.121 (danielk1 21-Jun-04): void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){
22321.307 (drh 21-Aug-07): assert( sqlite3_mutex_held(db->mutex) );
22331.121 (danielk1 21-Jun-04): db->nChange = nChange;
22341.121 (danielk1 21-Jun-04): db->nTotalChange += nChange;
22351.121 (danielk1 21-Jun-04): }
22361.121 (danielk1 21-Jun-04):
22371.121 (danielk1 21-Jun-04): /*
22381.121 (danielk1 21-Jun-04): ** Set a flag in the vdbe to update the change counter when it is finalised
22391.121 (danielk1 21-Jun-04): ** or reset.
22401.121 (danielk1 21-Jun-04): */
22411.152 (drh 05-Nov-04): void sqlite3VdbeCountChanges(Vdbe *v){
22421.152 (drh 05-Nov-04): v->changeCntOn = 1;
22431.121 (danielk1 21-Jun-04): }
22441.159 (drh 22-Jan-05):
22451.159 (drh 22-Jan-05): /*
22461.159 (drh 22-Jan-05): ** Mark every prepared statement associated with a database connection
22471.159 (drh 22-Jan-05): ** as expired.
22481.159 (drh 22-Jan-05): **
22491.159 (drh 22-Jan-05): ** An expired statement means that recompilation of the statement is
22501.159 (drh 22-Jan-05): ** recommend. Statements expire when things happen that make their
22511.159 (drh 22-Jan-05): ** programs obsolete. Removing user-defined functions or collating
22521.159 (drh 22-Jan-05): ** sequences, or changing an authorization function are the types of
22531.159 (drh 22-Jan-05): ** things that make prepared statements obsolete.
22541.159 (drh 22-Jan-05): */
22551.159 (drh 22-Jan-05): void sqlite3ExpirePreparedStatements(sqlite3 *db){
22561.159 (drh 22-Jan-05): Vdbe *p;
22571.159 (drh 22-Jan-05): for(p = db->pVdbe; p; p=p->pNext){
22581.159 (drh 22-Jan-05): p->expired = 1;
22591.159 (drh 22-Jan-05): }
22601.159 (drh 22-Jan-05): }
22611.167 (danielk1 09-Mar-05):
22621.167 (danielk1 09-Mar-05): /*
22631.167 (danielk1 09-Mar-05): ** Return the database associated with the Vdbe.
22641.167 (danielk1 09-Mar-05): */
22651.167 (danielk1 09-Mar-05): sqlite3 *sqlite3VdbeDb(Vdbe *v){
22661.167 (danielk1 09-Mar-05): return v->db;
22671.167 (danielk1 09-Mar-05): }