/ Check-in [6d3017f9]
Login
Overview
Comment:Add infrastructure for doing an UPDATE as part of an UPSERT. Still no actual UPDATE code, however.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | upsert
Files: files | file ages | folders
SHA3-256:6d3017f92bce3e50a91fab2f605e2af8b913b1b374adbfd977299eb042683de8
User & Date: drh 2018-04-13 18:59:17
Context
2018-04-13
21:55
First cut at logic to perform DO UPDATE for rowid tables. check-in: a9080bc8 user: drh tags: upsert
18:59
Add infrastructure for doing an UPDATE as part of an UPSERT. Still no actual UPDATE code, however. check-in: 6d3017f9 user: drh tags: upsert
16:29
Merge the preupdate hook change from trunk. check-in: 7353caab user: drh tags: upsert
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/insert.c.

  1480   1480               sqlite3MultiWrite(pParse);
  1481   1481               sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,-1);
  1482   1482             }
  1483   1483           }
  1484   1484           seenReplace = 1;
  1485   1485           break;
  1486   1486         }
         1487  +#ifndef SQLITE_OMIT_UPSERT
         1488  +      case OE_Update: {
         1489  +        sqlite3UpsertDoUpdate(pParse, pUpsert, pTab, 0, iDataCur, 0);
         1490  +        /* Fall through */
         1491  +      }
         1492  +#endif
  1487   1493         case OE_Ignore: {
  1488   1494           sqlite3VdbeGoto(v, ignoreDest);
  1489   1495           break;
  1490   1496         }
  1491   1497       }
  1492   1498       sqlite3VdbeResolveLabel(v, addrRowidOk);
  1493   1499       if( ipkTop ){
................................................................................
  1662   1668             }
  1663   1669           }
  1664   1670         }
  1665   1671       }
  1666   1672   
  1667   1673       /* Generate code that executes if the new index entry is not unique */
  1668   1674       assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
  1669         -        || onError==OE_Ignore || onError==OE_Replace );
         1675  +        || onError==OE_Ignore || onError==OE_Replace || onError==OE_Update );
  1670   1676       switch( onError ){
  1671   1677         case OE_Rollback:
  1672   1678         case OE_Abort:
  1673   1679         case OE_Fail: {
  1674   1680           sqlite3UniqueConstraint(pParse, onError, pIdx);
  1675   1681           break;
  1676   1682         }
         1683  +#ifndef SQLITE_OMIT_UPSERT
         1684  +      case OE_Update: {
         1685  +        sqlite3UpsertDoUpdate(pParse, pUpsert, pTab, pIdx, iDataCur, iIdxCur);
         1686  +        /* Fall through */
         1687  +      }
         1688  +#endif
  1677   1689         case OE_Ignore: {
  1678   1690           sqlite3VdbeGoto(v, ignoreDest);
  1679   1691           break;
  1680   1692         }
  1681   1693         default: {
  1682   1694           Trigger *pTrigger = 0;
  1683   1695           assert( onError==OE_Replace );

Changes to src/sqliteInt.h.

  4280   4280   #define sqlite3WithDelete(x,y)
  4281   4281   #endif
  4282   4282   #ifndef SQLITE_OMIT_UPSERT
  4283   4283     Upsert *sqlite3UpsertNew(sqlite3*,ExprList*,Expr*,ExprList*,Expr*);
  4284   4284     void sqlite3UpsertDelete(sqlite3*,Upsert*);
  4285   4285     Upsert *sqlite3UpsertDup(sqlite3*,Upsert*);
  4286   4286     int sqlite3UpsertAnalyzeTarget(Parse*,SrcList*,Upsert*);
         4287  +  void sqlite3UpsertDoUpdate(Parse*,Upsert*,Table*,Index*,int,int);
  4287   4288   #else
  4288   4289   #define sqlite3UpsertNew(x,y,z,w) ((Upsert*)0)
  4289   4290   #define sqlite3UpsertDelete(x,y)
  4290   4291   #define sqlite3UpsertDup(x,y)     ((Upsert*)0)
  4291   4292   #endif
  4292   4293   
  4293   4294   

Changes to src/upsert.c.

   175    175       pUpsert->pUpsertIdx = pIdx;
   176    176       return SQLITE_OK;
   177    177     }
   178    178     sqlite3ErrorMsg(pParse, "ON CONFLICT clause does not match any "
   179    179                             "PRIMARY KEY or UNIQUE constraint");
   180    180     return SQLITE_ERROR;
   181    181   }
          182  +
          183  +/*
          184  +** Generate bytecode that does an UPDATE as part of an upsert.
          185  +*/
          186  +void sqlite3UpsertDoUpdate(
          187  +  Parse *pParse,        /* The parsing and code-generating context */
          188  +  Upsert *pUpsert,      /* The ON CONFLICT clause for the upsert */
          189  +  Table *pTab,          /* The table being updated */
          190  +  Index *pIdx,          /* The UNIQUE constraint that failed */
          191  +  int iDataCur,         /* Cursor for the pTab, table being updated */
          192  +  int iIdxCur           /* Cursor for the pIdx */
          193  +){
          194  +  Vdbe *v = pParse->pVdbe;
          195  +  assert( v!=0 );
          196  +  VdbeNoopComment((v, "Begin DO UPDATE of UPSERT"));
          197  +  VdbeNoopComment((v, "End DO UPDATE of UPSERT"));
          198  +}
   182    199   
   183    200   #endif /* SQLITE_OMIT_UPSERT */