/ Check-in [d9ebe9d7]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Cleanup and simplification of constraint processing. Simplifications to the VM for better test coverage. (CVS 4729)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:d9ebe9d78c558af050c44ac4437ce0ef8193a4a8
User & Date: drh 2008-01-19 03:35:59
Context
2008-01-19
20:11
Miscellaneous code simplifications and cleanup and test coverage enhancements. (CVS 4730) check-in: af129b6d user: drh tags: trunk
03:35
Cleanup and simplification of constraint processing. Simplifications to the VM for better test coverage. (CVS 4729) check-in: d9ebe9d7 user: drh tags: trunk
2008-01-18
17:03
Fix test instrumentation problems on shared_err.test. (CVS 4728) check-in: 5aef5b0d user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Show Whitespace Changes Patch

Changes to src/insert.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains C code routines that are called by the parser
    13     13   ** to handle INSERT statements in SQLite.
    14     14   **
    15         -** $Id: insert.c,v 1.225 2008/01/17 16:22:15 drh Exp $
           15  +** $Id: insert.c,v 1.226 2008/01/19 03:35:59 drh Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   
    19     19   /*
    20     20   ** Set P4 of the most recently inserted opcode to a column affinity
    21     21   ** string for index pIdx. A column affinity string has one character
    22     22   ** for each column in the table, according to the affinity of the column:
................................................................................
   939    939   /*
   940    940   ** Generate code to do constraint checks prior to an INSERT or an UPDATE.
   941    941   **
   942    942   ** The input is a range of consecutive registers as follows:
   943    943   **
   944    944   **    1.  The rowid of the row to be updated before the update.  This
   945    945   **        value is omitted unless we are doing an UPDATE that involves a
   946         -**        change to the record number. (Or writing to a virtual table.)
          946  +**        change to the record number or writing to a virtual table.
   947    947   **
   948    948   **    2.  The rowid of the row after the update.
   949    949   **
   950    950   **    3.  The data in the first column of the entry after the update.
   951    951   **
   952    952   **    i.  Data from middle columns...
   953    953   **
   954    954   **    N.  The data in the last column of the entry after the update.
   955    955   **
   956    956   ** The regRowid parameter is the index of the register containing (2).
   957    957   **
   958    958   ** The old rowid shown as entry (1) above is omitted unless both isUpdate
   959    959   ** and rowidChng are 1.  isUpdate is true for UPDATEs and false for
   960         -** INSERTs and rowidChng is true if the record number is being changed.
          960  +** INSERTs.  RowidChng means that the new rowid is explicitly specified by
          961  +** the update or insert statement.  If rowidChng is false, it means that
          962  +** the rowid is computed automatically in an insert or that the rowid value
          963  +** is not modified by the update.
   961    964   **
   962    965   ** The code generated by this routine store new index entries into
   963    966   ** registers identified by aRegIdx[].  No index entry is created for
   964    967   ** indices where aRegIdx[i]==0.  The order of indices in aRegIdx[] is
   965    968   ** the same as the order of indices on the linked list of indices
   966    969   ** attached to the table.
   967    970   **
................................................................................
  1005   1008   ** for the constraint is used.
  1006   1009   **
  1007   1010   ** The calling routine must open a read/write cursor for pTab with
  1008   1011   ** cursor number "baseCur".  All indices of pTab must also have open
  1009   1012   ** read/write cursors with cursor number baseCur+i for the i-th cursor.
  1010   1013   ** Except, if there is no possibility of a REPLACE action then
  1011   1014   ** cursors do not need to be open for indices where aRegIdx[i]==0.
  1012         -**
  1013         -** If the isUpdate flag is true, it means that the "baseCur" cursor is
  1014         -** initially pointing to an entry that is being updated.  The isUpdate
  1015         -** flag causes extra code to be generated so that the "baseCur" cursor
  1016         -** is still pointing at the same entry after the routine returns.
  1017         -** Without the isUpdate flag, the "baseCur" cursor might be moved.
  1018   1015   */
  1019   1016   void sqlite3GenerateConstraintChecks(
  1020   1017     Parse *pParse,      /* The parser context */
  1021   1018     Table *pTab,        /* the table into which we are inserting */
  1022   1019     int baseCur,        /* Index of a read/write cursor pointing at pTab */
  1023   1020     int regRowid,       /* Index of the range of input registers */
  1024   1021     int *aRegIdx,       /* Register used by each index.  0 for unused indices */
  1025         -  int rowidChng,      /* True if the rowid will change */
         1022  +  int rowidChng,      /* True if the rowid might collide with existing entry */
  1026   1023     int isUpdate,       /* True for UPDATE, False for INSERT */
  1027   1024     int overrideError,  /* Override onError to this if not OE_Default */
  1028   1025     int ignoreDest      /* Jump to this label on an OE_Ignore resolution */
  1029   1026   ){
  1030   1027     int i;
  1031   1028     Vdbe *v;
  1032   1029     int nCol;
  1033   1030     int onError;
  1034         -  int j1, j2, j3;     /* Address of jump instructions */
         1031  +  int j1, j2, j3;     /* Addresses of jump instructions */
  1035   1032     int regData;        /* Register containing first data column */
  1036   1033     int iCur;
  1037   1034     Index *pIdx;
  1038   1035     int seenReplace = 0;
  1039   1036     int hasTwoRowids = (isUpdate && rowidChng);
  1040   1037   
  1041   1038     v = sqlite3GetVdbe(pParse);
................................................................................
  1112   1109       onError = pTab->keyConf;
  1113   1110       if( overrideError!=OE_Default ){
  1114   1111         onError = overrideError;
  1115   1112       }else if( onError==OE_Default ){
  1116   1113         onError = OE_Abort;
  1117   1114       }
  1118   1115       
         1116  +    if( onError==OE_Replace && pTab->pIndex==0 ){
         1117  +      seenReplace = 1;
         1118  +    }else{
  1119   1119       if( isUpdate ){
  1120   1120         j2 = sqlite3VdbeAddOp3(v, OP_Eq, regRowid, 0, regRowid-1);
  1121   1121       }
  1122   1122       j3 = sqlite3VdbeAddOp3(v, OP_NotExists, baseCur, 0, regRowid);
  1123   1123       switch( onError ){
  1124   1124         default: {
  1125   1125           onError = OE_Abort;
................................................................................
  1130   1130         case OE_Fail: {
  1131   1131           sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, onError, 0,
  1132   1132                            "PRIMARY KEY must be unique", P4_STATIC);
  1133   1133           break;
  1134   1134         }
  1135   1135         case OE_Replace: {
  1136   1136           sqlite3GenerateRowIndexDelete(pParse, pTab, baseCur, 0);
  1137         -        if( isUpdate ){
  1138         -          sqlite3VdbeAddOp3(v, OP_MoveGe, baseCur, 0, regRowid-hasTwoRowids);
  1139         -        }
  1140   1137           seenReplace = 1;
  1141   1138           break;
  1142   1139         }
  1143   1140         case OE_Ignore: {
  1144   1141           assert( seenReplace==0 );
  1145   1142           sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
  1146   1143           break;
  1147   1144         }
  1148   1145       }
  1149   1146       sqlite3VdbeJumpHere(v, j3);
  1150   1147       if( isUpdate ){
  1151   1148         sqlite3VdbeJumpHere(v, j2);
  1152         -      sqlite3VdbeAddOp3(v, OP_MoveGe, baseCur, 0, regRowid-1);
         1149  +      }
  1153   1150       }
  1154   1151     }
  1155   1152   
  1156   1153     /* Test all UNIQUE constraints by creating entries for each UNIQUE
  1157   1154     ** index and making sure that duplicate entries do not already exist.
  1158   1155     ** Add the new records to the indices as we go.
  1159   1156     */
................................................................................
  1235   1232         case OE_Ignore: {
  1236   1233           assert( seenReplace==0 );
  1237   1234           sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
  1238   1235           break;
  1239   1236         }
  1240   1237         case OE_Replace: {
  1241   1238           sqlite3GenerateRowDelete(pParse, pTab, baseCur, regR, 0);
  1242         -        if( isUpdate ){
  1243         -          sqlite3VdbeAddOp3(v, OP_MoveGe, baseCur, 0, regRowid-hasTwoRowids);
  1244         -        }
  1245   1239           seenReplace = 1;
  1246   1240           break;
  1247   1241         }
  1248   1242       }
  1249   1243       sqlite3VdbeJumpHere(v, j2);
  1250   1244       sqlite3VdbeJumpHere(v, j3);
  1251   1245       sqlite3ReleaseTempReg(pParse, regR);

Changes to src/select.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains C code routines that are called by the parser
    13     13   ** to handle SELECT statements in SQLite.
    14     14   **
    15         -** $Id: select.c,v 1.405 2008/01/17 17:15:56 drh Exp $
           15  +** $Id: select.c,v 1.406 2008/01/19 03:35:59 drh Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   
    19     19   
    20     20   /*
    21     21   ** Delete all the content of a Select structure but do not deallocate
    22     22   ** the select structure itself.
................................................................................
   598    598       }
   599    599   
   600    600       /* Construct a record from the query result, but instead of
   601    601       ** saving that record, use it as a key to delete elements from
   602    602       ** the temporary table iParm.
   603    603       */
   604    604       case SRT_Except: {
   605         -      int addr, r1;
          605  +      int r1;
   606    606         r1 = sqlite3GetTempReg(pParse);
   607         -      addr = sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nColumn, r1);
          607  +      sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nColumn, r1);
   608    608         sqlite3VdbeChangeP4(v, -1, aff, P4_STATIC);
   609         -      sqlite3VdbeAddOp3(v, OP_NotFound, iParm, addr+3, r1);
   610         -      sqlite3VdbeAddOp1(v, OP_Delete, iParm);
          609  +      sqlite3VdbeAddOp2(v, OP_IdxDelete, iParm, r1);
   611    610         sqlite3ReleaseTempReg(pParse, r1);
   612    611         break;
   613    612       }
   614    613   #endif
   615    614   
   616    615       /* Store the result as data using a unique key.
   617    616       */

Changes to src/update.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains C code routines that are called by the parser
    13     13   ** to handle UPDATE statements.
    14     14   **
    15         -** $Id: update.c,v 1.169 2008/01/17 16:22:15 drh Exp $
           15  +** $Id: update.c,v 1.170 2008/01/19 03:35:59 drh Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   
    19     19   #ifndef SQLITE_OMIT_VIRTUALTABLE
    20     20   /* Forward declaration */
    21     21   static void updateVirtualTable(
    22     22     Parse *pParse,       /* The parsing context */
................................................................................
    98     98                            ** aXRef[i]==-1 if the i-th column is not changed. */
    99     99     int chngRowid;         /* True if the record number is being changed */
   100    100     Expr *pRowidExpr = 0;  /* Expression defining the new record number */
   101    101     int openAll = 0;       /* True if all indices need to be opened */
   102    102     AuthContext sContext;  /* The authorization context */
   103    103     NameContext sNC;       /* The name-context to resolve expressions in */
   104    104     int iDb;               /* Database containing the table being updated */
          105  +  int j1;                /* Addresses of jump instructions */
   105    106   
   106    107   #ifndef SQLITE_OMIT_TRIGGER
   107    108     int isView;                  /* Trying to update a view */
   108    109     int triggers_exist = 0;      /* True if any row triggers exist */
   109    110   #endif
   110    111     int iBeginAfterTrigger;      /* Address of after trigger program */
   111    112     int iEndAfterTrigger;        /* Exit of after trigger program */
................................................................................
   500    501       */
   501    502       sqlite3GenerateConstraintChecks(pParse, pTab, iCur, regNewRowid,
   502    503                                       aRegIdx, chngRowid, 1,
   503    504                                       onError, addr);
   504    505   
   505    506       /* Delete the old indices for the current record.
   506    507       */
          508  +    j1 = sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, regOldRowid);
   507    509       sqlite3GenerateRowIndexDelete(pParse, pTab, iCur, aRegIdx);
   508    510   
   509    511       /* If changing the record number, delete the old record.
   510    512       */
   511    513       if( chngRowid ){
   512    514         sqlite3VdbeAddOp2(v, OP_Delete, iCur, 0);
   513    515       }
          516  +    sqlite3VdbeJumpHere(v, j1);
   514    517   
   515    518       /* Create the new index entries and the new record.
   516    519       */
   517    520       sqlite3CompleteInsertion(pParse, pTab, iCur, regNewRowid, 
   518    521                                aRegIdx, chngRowid, 1, -1, 0);
   519    522     }
   520    523   

Changes to src/vdbe.c.

    39     39   **
    40     40   ** Various scripts scan this source file in order to generate HTML
    41     41   ** documentation, headers files, or other derived files.  The formatting
    42     42   ** of the code in this file is, therefore, important.  See other comments
    43     43   ** in this file for details.  If in doubt, do not deviate from existing
    44     44   ** commenting and indentation practices when changing or adding code.
    45     45   **
    46         -** $Id: vdbe.c,v 1.699 2008/01/18 14:08:25 drh Exp $
           46  +** $Id: vdbe.c,v 1.700 2008/01/19 03:35:59 drh Exp $
    47     47   */
    48     48   #include "sqliteInt.h"
    49     49   #include <ctype.h>
    50     50   #include "vdbeInt.h"
    51     51   
    52     52   /*
    53     53   ** The following global variable is incremented every time a cursor
................................................................................
   591    591         if( db->nProgressOps==nProgressOps ){
   592    592           int prc;
   593    593           if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
   594    594           prc =db->xProgress(db->pProgressArg);
   595    595           if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
   596    596           if( prc!=0 ){
   597    597             rc = SQLITE_INTERRUPT;
   598         -          goto vdbe_halt;
          598  +          goto vdbe_error_halt;
   599    599           }
   600    600           nProgressOps = 0;
   601    601         }
   602    602         nProgressOps++;
   603    603       }
   604    604   #endif
   605    605   
................................................................................
   946    946     assert( pOp->p2>0 );
   947    947     assert( pOp->p2<=p->nMem );
   948    948     pOut = &p->aMem[pOp->p2];
   949    949     assert( pOut!=pIn1 );
   950    950     if( pOp->opcode==OP_Move ){
   951    951       rc = sqlite3VdbeMemMove(pOut, pIn1);
   952    952     }else{
   953         -    Release(pOut);
   954    953       sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
   955    954       if( pOp->opcode==OP_Copy ){
   956    955         Deephemeralize(pOut);
   957    956       }
   958    957     }
   959    958     REGISTER_TRACE(pOp->p2, pOut);
   960    959     break;
................................................................................
  1070   1069   case OP_Add:                   /* same as TK_PLUS, in1, in2, out3 */
  1071   1070   case OP_Subtract:              /* same as TK_MINUS, in1, in2, out3 */
  1072   1071   case OP_Multiply:              /* same as TK_STAR, in1, in2, out3 */
  1073   1072   case OP_Divide:                /* same as TK_SLASH, in1, in2, out3 */
  1074   1073   case OP_Remainder: {           /* same as TK_REM, in1, in2, out3 */
  1075   1074     int flags;
  1076   1075     flags = pIn1->flags | pIn2->flags;
  1077         -  if( (flags & MEM_Null)!=0 ){
  1078         -    Release(pOut);
  1079         -    pOut->flags = MEM_Null;
  1080         -  }else if( (pIn1->flags & pIn2->flags & MEM_Int)==MEM_Int ){
         1076  +  if( (flags & MEM_Null)!=0 ) goto arithmetic_result_is_null;
         1077  +  if( (pIn1->flags & pIn2->flags & MEM_Int)==MEM_Int ){
  1081   1078       i64 a, b;
  1082   1079       a = pIn1->u.i;
  1083   1080       b = pIn2->u.i;
  1084   1081       switch( pOp->opcode ){
  1085   1082         case OP_Add:         b += a;       break;
  1086   1083         case OP_Subtract:    b -= a;       break;
  1087   1084         case OP_Multiply:    b *= a;       break;
  1088   1085         case OP_Divide: {
  1089         -        if( a==0 ) goto divide_by_zero;
         1086  +        if( a==0 ) goto arithmetic_result_is_null;
  1090   1087           /* Dividing the largest possible negative 64-bit integer (1<<63) by 
  1091   1088           ** -1 returns an integer to large to store in a 64-bit data-type. On
  1092   1089           ** some architectures, the value overflows to (1<<63). On others,
  1093   1090           ** a SIGFPE is issued. The following statement normalizes this
  1094   1091           ** behaviour so that all architectures behave as if integer 
  1095   1092           ** overflow occured.
  1096   1093           */
  1097   1094           if( a==-1 && b==(((i64)1)<<63) ) a = 1;
  1098   1095           b /= a;
  1099   1096           break;
  1100   1097         }
  1101   1098         default: {
  1102         -        if( a==0 ) goto divide_by_zero;
         1099  +        if( a==0 ) goto arithmetic_result_is_null;
  1103   1100           if( a==-1 ) a = 1;
  1104   1101           b %= a;
  1105   1102           break;
  1106   1103         }
  1107   1104       }
  1108   1105       Release(pOut);
  1109   1106       pOut->u.i = b;
................................................................................
  1113   1110       a = sqlite3VdbeRealValue(pIn1);
  1114   1111       b = sqlite3VdbeRealValue(pIn2);
  1115   1112       switch( pOp->opcode ){
  1116   1113         case OP_Add:         b += a;       break;
  1117   1114         case OP_Subtract:    b -= a;       break;
  1118   1115         case OP_Multiply:    b *= a;       break;
  1119   1116         case OP_Divide: {
  1120         -        if( a==0.0 ) goto divide_by_zero;
         1117  +        if( a==0.0 ) goto arithmetic_result_is_null;
  1121   1118           b /= a;
  1122   1119           break;
  1123   1120         }
  1124   1121         default: {
  1125   1122           i64 ia = (i64)a;
  1126   1123           i64 ib = (i64)b;
  1127         -        if( ia==0 ) goto divide_by_zero;
         1124  +        if( ia==0 ) goto arithmetic_result_is_null;
  1128   1125           if( ia==-1 ) ia = 1;
  1129   1126           b = ib % ia;
  1130   1127           break;
  1131   1128         }
  1132   1129       }
  1133   1130       if( sqlite3_isnan(b) ){
  1134         -      goto divide_by_zero;
         1131  +      goto arithmetic_result_is_null;
  1135   1132       }
  1136   1133       Release(pOut);
  1137   1134       pOut->r = b;
  1138   1135       pOut->flags = MEM_Real;
  1139   1136       if( (flags & MEM_Real)==0 ){
  1140   1137         sqlite3VdbeIntegerAffinity(pOut);
  1141   1138       }
  1142   1139     }
  1143   1140     break;
  1144   1141   
  1145         -divide_by_zero:
  1146         -  Release(pOut);
  1147         -  pOut->flags = MEM_Null;
         1142  +arithmetic_result_is_null:
         1143  +  sqlite3VdbeMemSetNull(pOut);
  1148   1144     break;
  1149   1145   }
  1150   1146   
  1151   1147   /* Opcode: CollSeq * * P4
  1152   1148   **
  1153   1149   ** P4 is a pointer to a CollSeq struct. If the next call to a user function
  1154   1150   ** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
................................................................................
  1291   1287   case OP_BitAnd:                 /* same as TK_BITAND, in1, in2, out3 */
  1292   1288   case OP_BitOr:                  /* same as TK_BITOR, in1, in2, out3 */
  1293   1289   case OP_ShiftLeft:              /* same as TK_LSHIFT, in1, in2, out3 */
  1294   1290   case OP_ShiftRight: {           /* same as TK_RSHIFT, in1, in2, out3 */
  1295   1291     i64 a, b;
  1296   1292   
  1297   1293     if( (pIn1->flags | pIn2->flags) & MEM_Null ){
  1298         -    Release(pOut);
  1299         -    pOut->flags = MEM_Null;
         1294  +    sqlite3VdbeMemSetNull(pOut);
  1300   1295       break;
  1301   1296     }
  1302   1297     a = sqlite3VdbeIntValue(pIn2);
  1303   1298     b = sqlite3VdbeIntValue(pIn1);
  1304   1299     switch( pOp->opcode ){
  1305   1300       case OP_BitAnd:      a &= b;     break;
  1306   1301       case OP_BitOr:       a |= b;     break;
  1307   1302       case OP_ShiftLeft:   a <<= b;    break;
  1308         -    case OP_ShiftRight:  a >>= b;    break;
  1309         -    default:   /* CANT HAPPEN */     break;
         1303  +    default:  assert( pOp->opcode==OP_ShiftRight );
         1304  +                         a >>= b;    break;
  1310   1305     }
  1311   1306     Release(pOut);
  1312   1307     pOut->u.i = a;
  1313   1308     pOut->flags = MEM_Int;
  1314   1309     break;
  1315   1310   }
  1316   1311   
................................................................................
  1876   1871         i64 payloadSize64;
  1877   1872         sqlite3BtreeKeySize(pCrsr, &payloadSize64);
  1878   1873         payloadSize = payloadSize64;
  1879   1874       }else{
  1880   1875         sqlite3BtreeDataSize(pCrsr, &payloadSize);
  1881   1876       }
  1882   1877       nField = pC->nField;
  1883         -  }else if( pC->pseudoTable ){
         1878  +  }else{
         1879  +    assert( pC->pseudoTable );
  1884   1880       /* The record is the sole entry of a pseudo-table */
  1885   1881       payloadSize = pC->nData;
  1886   1882       zRec = pC->pData;
  1887   1883       pC->cacheStatus = CACHE_STALE;
  1888   1884       assert( payloadSize==0 || zRec!=0 );
  1889   1885       nField = pC->nField;
  1890   1886       pCrsr = 0;
  1891         -  }else{
  1892         -    zRec = 0;
  1893         -    payloadSize = 0;
  1894         -    pCrsr = 0;
  1895         -    nField = 0;
  1896   1887     }
  1897   1888   
  1898   1889     /* If payloadSize is 0, then just store a NULL */
  1899   1890     if( payloadSize==0 ){
  1900   1891       assert( pDest->flags==MEM_Null );
  1901   1892       goto op_column_out;
  1902   1893     }
................................................................................
  1905   1896     }
  1906   1897   
  1907   1898     assert( p2<nField );
  1908   1899   
  1909   1900     /* Read and parse the table header.  Store the results of the parse
  1910   1901     ** into the record header cache fields of the cursor.
  1911   1902     */
  1912         -  if( pC && pC->cacheStatus==p->cacheCtr ){
         1903  +  if( pC->cacheStatus==p->cacheCtr ){
  1913   1904       aType = pC->aType;
  1914   1905       aOffset = pC->aOffset;
  1915   1906     }else{
  1916   1907       u8 *zIdx;        /* Index into header */
  1917   1908       u8 *zEndHdr;     /* Pointer to first byte after the header */
  1918   1909       u32 offset;      /* Offset into the data */
  1919   1910       int szHdrSz;     /* Size of the header size field at start of record */
................................................................................
  2121   2112     */
  2122   2113     for(pRec=pData0; pRec<=pLast; pRec++){
  2123   2114       int len;
  2124   2115       if( zAffinity ){
  2125   2116         applyAffinity(pRec, zAffinity[pRec-pData0], encoding);
  2126   2117       }
  2127   2118       if( pRec->flags&MEM_Zero && pRec->n>0 ){
  2128         -      ExpandBlob(pRec);
         2119  +      sqlite3VdbeMemExpandBlob(pRec);
  2129   2120       }
  2130   2121       serial_type = sqlite3VdbeSerialType(pRec, file_format);
  2131   2122       len = sqlite3VdbeSerialTypeLen(serial_type);
  2132   2123       nData += len;
  2133   2124       nHdr += sqlite3VarintLen(serial_type);
  2134   2125       if( pRec->flags & MEM_Zero ){
  2135   2126         /* Only pure zero-filled BLOBs can be input to this Opcode.
................................................................................
  2205   2196   ** commit when the VDBE halts.
  2206   2197   **
  2207   2198   ** The statement is begun on the database file with index P1.  The main
  2208   2199   ** database file has an index of 0 and the file used for temporary tables
  2209   2200   ** has an index of 1.
  2210   2201   */
  2211   2202   case OP_Statement: {
         2203  +  if( db->autoCommit==0 || db->activeVdbeCnt>1 ){
  2212   2204     int i = pOp->p1;
  2213   2205     Btree *pBt;
  2214         -  if( i>=0 && i<db->nDb && (pBt = db->aDb[i].pBt)!=0
  2215         -        && (db->autoCommit==0 || db->activeVdbeCnt>1) ){
         2206  +    assert( i>=0 && i<db->nDb );
         2207  +    assert( db->aDb[i].pBt!=0 );
         2208  +    pBt = db->aDb[i].pBt;
  2216   2209       assert( sqlite3BtreeIsInTrans(pBt) );
  2217   2210       assert( (p->btreeMask & (1<<i))!=0 );
  2218   2211       if( !sqlite3BtreeIsInStmt(pBt) ){
  2219   2212         rc = sqlite3BtreeBeginStmt(pBt);
  2220   2213         p->openedStatement = 1;
  2221   2214       }
  2222   2215     }
................................................................................
  2529   2522       p2 = pIn2->u.i;
  2530   2523       assert( p2>=2 );
  2531   2524     }
  2532   2525     assert( i>=0 );
  2533   2526     pCur = allocateCursor(p, i, iDb);
  2534   2527     if( pCur==0 ) goto no_mem;
  2535   2528     pCur->nullRow = 1;
  2536         -  if( pX==0 ) break;
  2537   2529     /* We always provide a key comparison function.  If the table being
  2538   2530     ** opened is of type INTKEY, the comparision function will be ignored. */
  2539   2531     rc = sqlite3BtreeCursor(pX, p2, wrFlag,
  2540   2532              sqlite3VdbeRecordCompare, pOp->p4.p,
  2541   2533              &pCur->pCursor);
  2542   2534     if( pOp->p4type==P4_KEYINFO ){
  2543   2535       pCur->pKeyInfo = pOp->p4.pKeyInfo;
................................................................................
  2687   2679   /* Opcode: Close P1 * * * *
  2688   2680   **
  2689   2681   ** Close a cursor previously opened as P1.  If P1 is not
  2690   2682   ** currently open, this instruction is a no-op.
  2691   2683   */
  2692   2684   case OP_Close: {
  2693   2685     int i = pOp->p1;
  2694         -  if( i>=0 && i<p->nCursor ){
         2686  +  assert( i>=0 && i<p->nCursor );
  2695   2687       sqlite3VdbeFreeCursor(p, p->apCsr[i]);
  2696   2688       p->apCsr[i] = 0;
  2697         -  }
  2698   2689     break;
  2699   2690   }
  2700   2691   
  2701   2692   /* Opcode: MoveGe P1 P2 P3 * *
  2702   2693   **
  2703   2694   ** Use the value in register P3 as a key.  Reposition
  2704   2695   ** cursor P1 so that it points to the smallest entry that is greater
................................................................................
  3219   3210   ** This instruction only works on tables.  The equivalent instruction
  3220   3211   ** for indices is OP_IdxInsert.
  3221   3212   */
  3222   3213   case OP_Insert: {
  3223   3214     Mem *pData = &p->aMem[pOp->p2];
  3224   3215     Mem *pKey = &p->aMem[pOp->p3];
  3225   3216   
         3217  +  i64 iKey;   /* The integer ROWID or key for the record to be inserted */
  3226   3218     int i = pOp->p1;
  3227   3219     Cursor *pC;
  3228   3220     assert( i>=0 && i<p->nCursor );
  3229         -  assert( p->apCsr[i]!=0 );
  3230         -  REGISTER_TRACE(pOp->p2, pData);
  3231         -  REGISTER_TRACE(pOp->p3, pKey);
  3232         -  if( ((pC = p->apCsr[i])->pCursor!=0 || pC->pseudoTable) ){
  3233         -    i64 iKey;   /* The integer ROWID or key for the record to be inserted */
  3234         -
         3221  +  pC = p->apCsr[i];
         3222  +  assert( pC!=0 );
         3223  +  assert( pC->pCursor!=0 || pC->pseudoTable );
  3235   3224       assert( pKey->flags & MEM_Int );
  3236   3225       assert( pC->isTable );
         3226  +  REGISTER_TRACE(pOp->p2, pData);
         3227  +  REGISTER_TRACE(pOp->p3, pKey);
         3228  +
  3237   3229       iKey = intToKey(pKey->u.i);
  3238         -
  3239   3230       if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
  3240   3231       if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = pKey->u.i;
  3241   3232       if( pC->nextRowidValid && pKey->u.i>=pC->nextRowid ){
  3242   3233         pC->nextRowidValid = 0;
  3243   3234       }
  3244   3235       if( pData->flags & MEM_Null ){
  3245   3236         pData->z = 0;
................................................................................
  3284   3275         const char *zDb = db->aDb[pC->iDb].zName;
  3285   3276         const char *zTbl = pOp->p4.z;
  3286   3277         int op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
  3287   3278         assert( pC->isTable );
  3288   3279         db->xUpdateCallback(db->pUpdateArg, op, zDb, zTbl, iKey);
  3289   3280         assert( pC->iDb>=0 );
  3290   3281       }
  3291         -  }
  3292         -
  3293   3282     break;
  3294   3283   }
  3295   3284   
  3296   3285   /* Opcode: Delete P1 P2 * P4 *
  3297   3286   **
  3298   3287   ** Delete the record at which the P1 cursor is currently pointing.
  3299   3288   **
................................................................................
  3399   3388     assert( pC!=0 );
  3400   3389     if( pC->nullRow ){
  3401   3390       pOut->flags = MEM_Null;
  3402   3391     }else if( pC->pCursor!=0 ){
  3403   3392       BtCursor *pCrsr = pC->pCursor;
  3404   3393       rc = sqlite3VdbeCursorMoveto(pC);
  3405   3394       if( rc ) goto abort_due_to_error;
  3406         -    if( pC->nullRow ){
  3407         -      pOut->flags = MEM_Null;
  3408         -      break;
  3409         -    }else if( pC->isIndex ){
         3395  +    if( pC->isIndex ){
  3410   3396         i64 n64;
  3411   3397         assert( !pC->isTable );
  3412   3398         sqlite3BtreeKeySize(pCrsr, &n64);
  3413   3399         if( n64>SQLITE_MAX_LENGTH ){
  3414   3400           goto too_big;
  3415   3401         }
  3416   3402         n = n64;
................................................................................
  3432   3418         pOut->z = z;
  3433   3419       }
  3434   3420       if( pC->isIndex ){
  3435   3421         rc = sqlite3BtreeKey(pCrsr, 0, n, pOut->z);
  3436   3422       }else{
  3437   3423         rc = sqlite3BtreeData(pCrsr, 0, n, pOut->z);
  3438   3424       }
  3439         -  }else if( pC->pseudoTable ){
         3425  +  }else{
         3426  +    assert( pC->pseudoTable );
  3440   3427       pOut->n = pC->nData;
  3441   3428       assert( pC->nData<=SQLITE_MAX_LENGTH );
  3442   3429       pOut->z = pC->pData;
  3443   3430       pOut->flags = MEM_Blob|MEM_Ephem;
  3444         -  }else{
  3445         -    pOut->flags = MEM_Null;
  3446   3431     }
  3447   3432     pOut->enc = SQLITE_UTF8;  /* In case the blob is ever cast to text */
  3448   3433     UPDATE_MAX_BLOBSIZE(pOut);
  3449   3434     break;
  3450   3435   }
  3451   3436   
  3452   3437   /* Opcode: Rowid P1 P2 * * *
................................................................................
  3464   3449     assert( pC!=0 );
  3465   3450     rc = sqlite3VdbeCursorMoveto(pC);
  3466   3451     if( rc ) goto abort_due_to_error;
  3467   3452     if( pC->rowidIsValid ){
  3468   3453       v = pC->lastRowid;
  3469   3454     }else if( pC->pseudoTable ){
  3470   3455       v = keyToInt(pC->iKey);
  3471         -  }else if( pC->nullRow || pC->pCursor==0 ){
         3456  +  }else if( pC->nullRow ){
  3472   3457       /* Leave the rowid set to a NULL */
  3473   3458       break;
  3474   3459     }else{
  3475   3460       assert( pC->pCursor!=0 );
  3476   3461       sqlite3BtreeKeySize(pC->pCursor, &v);
  3477   3462       v = keyToInt(v);
  3478   3463     }
................................................................................
  3507   3492   ** If P2 is 0 or if the table or index is not empty, fall through
  3508   3493   ** to the following instruction.
  3509   3494   */
  3510   3495   case OP_Last: {        /* jump */
  3511   3496     int i = pOp->p1;
  3512   3497     Cursor *pC;
  3513   3498     BtCursor *pCrsr;
         3499  +  int res;
  3514   3500   
  3515   3501     assert( i>=0 && i<p->nCursor );
  3516   3502     pC = p->apCsr[i];
  3517   3503     assert( pC!=0 );
  3518         -  if( (pCrsr = pC->pCursor)!=0 ){
  3519         -    int res;
         3504  +  pCrsr = pC->pCursor;
         3505  +  assert( pCrsr!=0 );
  3520   3506       rc = sqlite3BtreeLast(pCrsr, &res);
  3521   3507       pC->nullRow = res;
  3522   3508       pC->deferredMoveto = 0;
  3523   3509       pC->cacheStatus = CACHE_STALE;
  3524   3510       if( res && pOp->p2>0 ){
  3525   3511         pc = pOp->p2 - 1;
  3526   3512       }
  3527         -  }else{
  3528         -    pC->nullRow = 0;
  3529         -  }
  3530   3513     break;
  3531   3514   }
  3532   3515   
  3533   3516   
  3534   3517   /* Opcode: Sort P1 P2 * * *
  3535   3518   **
  3536   3519   ** This opcode does exactly the same thing as OP_Rewind except that
................................................................................
  3572   3555       pC->atFirst = res==0;
  3573   3556       pC->deferredMoveto = 0;
  3574   3557       pC->cacheStatus = CACHE_STALE;
  3575   3558     }else{
  3576   3559       res = 1;
  3577   3560     }
  3578   3561     pC->nullRow = res;
  3579         -  if( res && pOp->p2>0 ){
         3562  +  assert( pOp->p2>0 && pOp->p2<p->nOp );
         3563  +  if( res ){
  3580   3564       pc = pOp->p2 - 1;
  3581   3565     }
  3582   3566     break;
  3583   3567   }
  3584   3568   
  3585   3569   /* Opcode: Next P1 P2 * * *
  3586   3570   **
................................................................................
  3718   3702         pOut->flags = MEM_Int;
  3719   3703         pOut->u.i = rowid;
  3720   3704       }
  3721   3705     }
  3722   3706     break;
  3723   3707   }
  3724   3708   
  3725         -/* Opcode: IdxGT P1 P2 P3 * *
  3726         -**
  3727         -** The value in register P3 is an index entry that omits the ROWID.  Compare
  3728         -** the value in register P3 against the index that P1 is currently pointing to.
  3729         -** Ignore the ROWID on the P1 index.
  3730         -**
  3731         -** The P3 value might have fewer columns that P1 index.
  3732         -**
  3733         -** If the P1 index entry is greater than the value in register P3
  3734         -** then jump to P2.  Otherwise fall through to the next instruction.
  3735         -*/
  3736   3709   /* Opcode: IdxGE P1 P2 P3 * P5
  3737   3710   **
  3738   3711   ** The value in register P3 is an index entry that omits the ROWID.  Compare
  3739   3712   ** this value against the index that P1 is currently pointing to.
  3740   3713   ** Ignore the ROWID on the P1 index.
  3741   3714   **
  3742   3715   ** If the P1 index entry is greater than or equal to the value in 
................................................................................
  3760   3733   **
  3761   3734   ** If P5 is non-zero then the
  3762   3735   ** index taken from register P3 is temporarily increased by
  3763   3736   ** an epsilon prior to the comparison.  This makes the opcode work
  3764   3737   ** like IdxLE.
  3765   3738   */
  3766   3739   case OP_IdxLT:          /* jump, in3 */
  3767         -case OP_IdxGT:          /* jump, in3 */
  3768   3740   case OP_IdxGE: {        /* jump, in3 */
  3769   3741     int i= pOp->p1;
  3770   3742     Cursor *pC;
  3771   3743   
  3772   3744     assert( i>=0 && i<p->nCursor );
  3773   3745     assert( p->apCsr[i]!=0 );
  3774   3746     if( (pC = p->apCsr[i])->pCursor!=0 ){
  3775   3747       int res;
  3776   3748    
  3777   3749       assert( pIn3->flags & MEM_Blob );  /* Created using OP_MakeRecord */
  3778   3750       assert( pC->deferredMoveto==0 );
  3779   3751       ExpandBlob(pIn3);
  3780         -    *pC->pIncrKey = pOp->p5!=0;
  3781         -    assert( pOp->opcode!=OP_IdxGT || pOp->p5==0 );
         3752  +    assert( pOp->p5==0 || pOp->p5==1 );
         3753  +    *pC->pIncrKey = pOp->p5;
  3782   3754       rc = sqlite3VdbeIdxKeyCompare(pC, pIn3->n, (u8*)pIn3->z, &res);
  3783   3755       *pC->pIncrKey = 0;
  3784   3756       if( rc!=SQLITE_OK ){
  3785   3757         break;
  3786   3758       }
  3787   3759       if( pOp->opcode==OP_IdxLT ){
  3788   3760         res = -res;
  3789         -    }else if( pOp->opcode==OP_IdxGE ){
         3761  +    }else{
         3762  +      assert( pOp->opcode==OP_IdxGE );
  3790   3763         res++;
  3791   3764       }
  3792   3765       if( res>0 ){
  3793   3766         pc = pOp->p2 - 1 ;
  3794   3767       }
  3795   3768     }
  3796   3769     break;
................................................................................
  4052   4025     }
  4053   4026     aRoot[j] = 0;
  4054   4027     assert( pOp->p5<db->nDb );
  4055   4028     assert( (p->btreeMask & (1<<pOp->p5))!=0 );
  4056   4029     z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, aRoot, nRoot,
  4057   4030                                    pnErr->u.i, &nErr);
  4058   4031     pnErr->u.i -= nErr;
  4059         -  Release(pIn1);
         4032  +  sqlite3VdbeMemSetNull(pIn1);
  4060   4033     if( nErr==0 ){
  4061   4034       assert( z==0 );
  4062         -    pIn1->flags = MEM_Null;
  4063   4035     }else{
  4064   4036       pIn1->z = z;
  4065   4037       pIn1->n = strlen(z);
  4066   4038       pIn1->flags = MEM_Str | MEM_Dyn | MEM_Term;
  4067   4039       pIn1->xDel = 0;
  4068   4040     }
  4069   4041     pIn1->enc = SQLITE_UTF8;
................................................................................
  4786   4758           registerTrace(p->trace, pOp->p3, pOut);
  4787   4759         }
  4788   4760       }
  4789   4761   #endif  /* SQLITE_DEBUG */
  4790   4762   #endif  /* NDEBUG */
  4791   4763     }  /* The end of the for(;;) loop the loops through opcodes */
  4792   4764   
  4793         -  /* If we reach this point, it means that execution is finished.
         4765  +  /* If we reach this point, it means that execution is finished with
         4766  +  ** an error of some kind.
  4794   4767     */
  4795         -vdbe_halt:
  4796         -  if( rc ){
         4768  +vdbe_error_halt:
         4769  +  assert( rc );
  4797   4770       p->rc = rc;
  4798   4771       rc = SQLITE_ERROR;
  4799         -  }else{
  4800         -    rc = SQLITE_DONE;
  4801         -  }
  4802   4772     sqlite3VdbeHalt(p);
  4803   4773   
  4804   4774     /* This is the only way out of this procedure.  We have to
  4805   4775     ** release the mutexes on btrees that were acquired at the
  4806   4776     ** top. */
  4807   4777   vdbe_return:
  4808   4778     sqlite3BtreeMutexArrayLeave(&p->aMutex);
................................................................................
  4810   4780   
  4811   4781     /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
  4812   4782     ** is encountered.
  4813   4783     */
  4814   4784   too_big:
  4815   4785     sqlite3SetString(&p->zErrMsg, "string or blob too big", (char*)0);
  4816   4786     rc = SQLITE_TOOBIG;
  4817         -  goto vdbe_halt;
         4787  +  goto vdbe_error_halt;
  4818   4788   
  4819   4789     /* Jump to here if a malloc() fails.
  4820   4790     */
  4821   4791   no_mem:
  4822   4792     db->mallocFailed = 1;
  4823   4793     sqlite3SetString(&p->zErrMsg, "out of memory", (char*)0);
  4824   4794     rc = SQLITE_NOMEM;
  4825         -  goto vdbe_halt;
         4795  +  goto vdbe_error_halt;
  4826   4796   
  4827   4797     /* Jump to here for an SQLITE_MISUSE error.
  4828   4798     */
  4829   4799   abort_due_to_misuse:
  4830   4800     rc = SQLITE_MISUSE;
  4831   4801     /* Fall thru into abort_due_to_error */
  4832   4802   
  4833   4803     /* Jump to here for any other kind of fatal error.  The "rc" variable
  4834   4804     ** should hold the error number.
  4835   4805     */
  4836   4806   abort_due_to_error:
  4837         -  if( p->zErrMsg==0 ){
         4807  +  assert( p->zErrMsg==0 );
  4838   4808       if( db->mallocFailed ) rc = SQLITE_NOMEM;
  4839   4809       sqlite3SetString(&p->zErrMsg, sqlite3ErrStr(rc), (char*)0);
  4840         -  }
  4841         -  goto vdbe_halt;
         4810  +  goto vdbe_error_halt;
  4842   4811   
  4843   4812     /* Jump to here if the sqlite3_interrupt() API sets the interrupt
  4844   4813     ** flag.
  4845   4814     */
  4846   4815   abort_due_to_interrupt:
  4847   4816     assert( db->u1.isInterrupted );
  4848   4817     if( db->magic!=SQLITE_MAGIC_BUSY ){
  4849   4818       rc = SQLITE_MISUSE;
  4850   4819     }else{
  4851   4820       rc = SQLITE_INTERRUPT;
  4852   4821     }
  4853   4822     p->rc = rc;
  4854   4823     sqlite3SetString(&p->zErrMsg, sqlite3ErrStr(rc), (char*)0);
  4855         -  goto vdbe_halt;
         4824  +  goto vdbe_error_halt;
  4856   4825   }

Changes to src/vdbemem.c.

   504    504       return n>SQLITE_MAX_LENGTH;
   505    505     }
   506    506     return 0; 
   507    507   }
   508    508   
   509    509   /*
   510    510   ** Make an shallow copy of pFrom into pTo.  Prior contents of
   511         -** pTo are overwritten.  The pFrom->z field is not duplicated.  If
          511  +** pTo are freed.  The pFrom->z field is not duplicated.  If
   512    512   ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
   513    513   ** and flags gets srcType (either MEM_Ephem or MEM_Static).
   514    514   */
   515    515   void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
          516  +  sqlite3VdbeMemRelease(pTo);
   516    517     memcpy(pTo, pFrom, sizeof(*pFrom)-sizeof(pFrom->zShort));
   517    518     pTo->xDel = 0;
   518    519     if( pTo->flags & (MEM_Str|MEM_Blob) ){
   519    520       pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Short|MEM_Ephem);
   520    521       assert( srcType==MEM_Ephem || srcType==MEM_Static );
   521    522       pTo->flags |= srcType;
   522    523     }
................................................................................
   524    525   
   525    526   /*
   526    527   ** Make a full copy of pFrom into pTo.  Prior contents of pTo are
   527    528   ** freed before the copy is made.
   528    529   */
   529    530   int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
   530    531     int rc;
   531         -  if( pTo->flags & MEM_Dyn ){
   532         -    sqlite3VdbeMemRelease(pTo);
   533         -  }
   534    532     sqlite3VdbeMemShallowCopy(pTo, pFrom, MEM_Ephem);
   535    533     if( pTo->flags & MEM_Ephem ){
   536    534       rc = sqlite3VdbeMemMakeWriteable(pTo);
   537    535     }else{
   538    536       rc = SQLITE_OK;
   539    537     }
   540    538     return rc;