update.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. /*
  2. ** 2001 September 15
  3. **
  4. ** The author disclaims copyright to this source code. In place of
  5. ** a legal notice, here is a blessing:
  6. **
  7. ** May you do good and not evil.
  8. ** May you find forgiveness for yourself and forgive others.
  9. ** May you share freely, never taking more than you give.
  10. **
  11. *************************************************************************
  12. ** This file contains C code routines that are called by the parser
  13. ** to handle UPDATE statements.
  14. **
  15. ** $Id: update.c,v 1.144 2007/12/12 16:06:23 danielk1977 Exp $
  16. */
  17. #include "sqliteInt.h"
  18. #ifndef SQLITE_OMIT_VIRTUALTABLE
  19. /* Forward declaration */
  20. static void updateVirtualTable(
  21. Parse *pParse, /* The parsing context */
  22. SrcList *pSrc, /* The virtual table to be modified */
  23. Table *pTab, /* The virtual table */
  24. ExprList *pChanges, /* The columns to change in the UPDATE statement */
  25. Expr *pRowidExpr, /* Expression used to recompute the rowid */
  26. int *aXRef, /* Mapping from columns of pTab to entries in pChanges */
  27. Expr *pWhere /* WHERE clause of the UPDATE statement */
  28. );
  29. #endif /* SQLITE_OMIT_VIRTUALTABLE */
  30. /*
  31. ** The most recently coded instruction was an OP_Column to retrieve the
  32. ** i-th column of table pTab. This routine sets the P3 parameter of the
  33. ** OP_Column to the default value, if any.
  34. **
  35. ** The default value of a column is specified by a DEFAULT clause in the
  36. ** column definition. This was either supplied by the user when the table
  37. ** was created, or added later to the table definition by an ALTER TABLE
  38. ** command. If the latter, then the row-records in the table btree on disk
  39. ** may not contain a value for the column and the default value, taken
  40. ** from the P3 parameter of the OP_Column instruction, is returned instead.
  41. ** If the former, then all row-records are guaranteed to include a value
  42. ** for the column and the P3 value is not required.
  43. **
  44. ** Column definitions created by an ALTER TABLE command may only have
  45. ** literal default values specified: a number, null or a string. (If a more
  46. ** complicated default expression value was provided, it is evaluated
  47. ** when the ALTER TABLE is executed and one of the literal values written
  48. ** into the sqlite_master table.)
  49. **
  50. ** Therefore, the P3 parameter is only required if the default value for
  51. ** the column is a literal number, string or null. The sqlite3ValueFromExpr()
  52. ** function is capable of transforming these types of expressions into
  53. ** sqlite3_value objects.
  54. */
  55. void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i){
  56. if( pTab && !pTab->pSelect ){
  57. sqlite3_value *pValue;
  58. u8 enc = ENC(sqlite3VdbeDb(v));
  59. Column *pCol = &pTab->aCol[i];
  60. assert( i<pTab->nCol );
  61. sqlite3ValueFromExpr(sqlite3VdbeDb(v), pCol->pDflt, enc, pCol->affinity, &pValue);
  62. if( pValue ){
  63. sqlite3VdbeChangeP3(v, -1, (const char *)pValue, P3_MEM);
  64. }else{
  65. VdbeComment((v, "# %s.%s", pTab->zName, pCol->zName));
  66. }
  67. }
  68. }
  69. /*
  70. ** Process an UPDATE statement.
  71. **
  72. ** UPDATE OR IGNORE table_wxyz SET a=b, c=d WHERE e<5 AND f NOT NULL;
  73. ** \_______/ \________/ \______/ \________________/
  74. * onError pTabList pChanges pWhere
  75. */
  76. void sqlite3Update(
  77. Parse *pParse, /* The parser context */
  78. SrcList *pTabList, /* The table in which we should change things */
  79. ExprList *pChanges, /* Things to be changed */
  80. Expr *pWhere, /* The WHERE clause. May be null */
  81. int onError /* How to handle constraint errors */
  82. ){
  83. int i, j; /* Loop counters */
  84. Table *pTab; /* The table to be updated */
  85. int addr = 0; /* VDBE instruction address of the start of the loop */
  86. WhereInfo *pWInfo; /* Information about the WHERE clause */
  87. Vdbe *v; /* The virtual database engine */
  88. Index *pIdx; /* For looping over indices */
  89. int nIdx; /* Number of indices that need updating */
  90. int nIdxTotal; /* Total number of indices */
  91. int iCur; /* VDBE Cursor number of pTab */
  92. sqlite3 *db; /* The database structure */
  93. Index **apIdx = 0; /* An array of indices that need updating too */
  94. char *aIdxUsed = 0; /* aIdxUsed[i]==1 if the i-th index is used */
  95. int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the
  96. ** an expression for the i-th column of the table.
  97. ** aXRef[i]==-1 if the i-th column is not changed. */
  98. int chngRowid; /* True if the record number is being changed */
  99. Expr *pRowidExpr = 0; /* Expression defining the new record number */
  100. int openAll = 0; /* True if all indices need to be opened */
  101. AuthContext sContext; /* The authorization context */
  102. NameContext sNC; /* The name-context to resolve expressions in */
  103. int iDb; /* Database containing the table being updated */
  104. int memCnt = 0; /* Memory cell used for counting rows changed */
  105. int mem1; /* Memory address storing the rowid for next row to update */
  106. #ifndef SQLITE_OMIT_TRIGGER
  107. int isView; /* Trying to update a view */
  108. int triggers_exist = 0; /* True if any row triggers exist */
  109. #endif
  110. int newIdx = -1; /* index of trigger "new" temp table */
  111. int oldIdx = -1; /* index of trigger "old" temp table */
  112. sContext.pParse = 0;
  113. db = pParse->db;
  114. if( pParse->nErr || db->mallocFailed ){
  115. goto update_cleanup;
  116. }
  117. assert( pTabList->nSrc==1 );
  118. /* Locate the table which we want to update.
  119. */
  120. pTab = sqlite3SrcListLookup(pParse, pTabList);
  121. if( pTab==0 ) goto update_cleanup;
  122. iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
  123. /* Figure out if we have any triggers and if the table being
  124. ** updated is a view
  125. */
  126. #ifndef SQLITE_OMIT_TRIGGER
  127. triggers_exist = sqlite3TriggersExist(pParse, pTab, TK_UPDATE, pChanges);
  128. isView = pTab->pSelect!=0;
  129. #else
  130. # define triggers_exist 0
  131. # define isView 0
  132. #endif
  133. #ifdef SQLITE_OMIT_VIEW
  134. # undef isView
  135. # define isView 0
  136. #endif
  137. if( sqlite3IsReadOnly(pParse, pTab, triggers_exist) ){
  138. goto update_cleanup;
  139. }
  140. if( sqlite3ViewGetColumnNames(pParse, pTab) ){
  141. goto update_cleanup;
  142. }
  143. aXRef = sqlite3DbMallocRaw(db, sizeof(int) * pTab->nCol );
  144. if( aXRef==0 ) goto update_cleanup;
  145. for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
  146. /* If there are FOR EACH ROW triggers, allocate cursors for the
  147. ** special OLD and NEW tables
  148. */
  149. if( triggers_exist ){
  150. newIdx = pParse->nTab++;
  151. oldIdx = pParse->nTab++;
  152. }
  153. /* Allocate a cursors for the main database table and for all indices.
  154. ** The index cursors might not be used, but if they are used they
  155. ** need to occur right after the database cursor. So go ahead and
  156. ** allocate enough space, just in case.
  157. */
  158. pTabList->a[0].iCursor = iCur = pParse->nTab++;
  159. for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
  160. pParse->nTab++;
  161. }
  162. /* Initialize the name-context */
  163. memset(&sNC, 0, sizeof(sNC));
  164. sNC.pParse = pParse;
  165. sNC.pSrcList = pTabList;
  166. /* Resolve the column names in all the expressions of the
  167. ** of the UPDATE statement. Also find the column index
  168. ** for each column to be updated in the pChanges array. For each
  169. ** column to be updated, make sure we have authorization to change
  170. ** that column.
  171. */
  172. chngRowid = 0;
  173. for(i=0; i<pChanges->nExpr; i++){
  174. if( sqlite3ExprResolveNames(&sNC, pChanges->a[i].pExpr) ){
  175. goto update_cleanup;
  176. }
  177. for(j=0; j<pTab->nCol; j++){
  178. if( sqlite3StrICmp(pTab->aCol[j].zName, pChanges->a[i].zName)==0 ){
  179. if( j==pTab->iPKey ){
  180. chngRowid = 1;
  181. pRowidExpr = pChanges->a[i].pExpr;
  182. }
  183. aXRef[j] = i;
  184. break;
  185. }
  186. }
  187. if( j>=pTab->nCol ){
  188. if( sqlite3IsRowid(pChanges->a[i].zName) ){
  189. chngRowid = 1;
  190. pRowidExpr = pChanges->a[i].pExpr;
  191. }else{
  192. sqlite3ErrorMsg(pParse, "no such column: %s", pChanges->a[i].zName);
  193. goto update_cleanup;
  194. }
  195. }
  196. #ifndef SQLITE_OMIT_AUTHORIZATION
  197. {
  198. int rc;
  199. rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName,
  200. pTab->aCol[j].zName, db->aDb[iDb].zName);
  201. if( rc==SQLITE_DENY ){
  202. goto update_cleanup;
  203. }else if( rc==SQLITE_IGNORE ){
  204. aXRef[j] = -1;
  205. }
  206. }
  207. #endif
  208. }
  209. /* Allocate memory for the array apIdx[] and fill it with pointers to every
  210. ** index that needs to be updated. Indices only need updating if their
  211. ** key includes one of the columns named in pChanges or if the record
  212. ** number of the original table entry is changing.
  213. */
  214. for(nIdx=nIdxTotal=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdxTotal++){
  215. if( chngRowid ){
  216. i = 0;
  217. }else {
  218. for(i=0; i<pIdx->nColumn; i++){
  219. if( aXRef[pIdx->aiColumn[i]]>=0 ) break;
  220. }
  221. }
  222. if( i<pIdx->nColumn ) nIdx++;
  223. }
  224. if( nIdxTotal>0 ){
  225. apIdx = sqlite3DbMallocRaw(db, sizeof(Index*) * nIdx + nIdxTotal );
  226. if( apIdx==0 ) goto update_cleanup;
  227. aIdxUsed = (char*)&apIdx[nIdx];
  228. }
  229. for(nIdx=j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
  230. if( chngRowid ){
  231. i = 0;
  232. }else{
  233. for(i=0; i<pIdx->nColumn; i++){
  234. if( aXRef[pIdx->aiColumn[i]]>=0 ) break;
  235. }
  236. }
  237. if( i<pIdx->nColumn ){
  238. apIdx[nIdx++] = pIdx;
  239. aIdxUsed[j] = 1;
  240. }else{
  241. aIdxUsed[j] = 0;
  242. }
  243. }
  244. /* Begin generating code.
  245. */
  246. v = sqlite3GetVdbe(pParse);
  247. if( v==0 ) goto update_cleanup;
  248. if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
  249. sqlite3BeginWriteOperation(pParse, 1, iDb);
  250. mem1 = pParse->nMem++;
  251. #ifndef SQLITE_OMIT_VIRTUALTABLE
  252. /* Virtual tables must be handled separately */
  253. if( IsVirtual(pTab) ){
  254. updateVirtualTable(pParse, pTabList, pTab, pChanges, pRowidExpr, aXRef,
  255. pWhere);
  256. pWhere = 0;
  257. pTabList = 0;
  258. goto update_cleanup;
  259. }
  260. #endif
  261. /* Resolve the column names in all the expressions in the
  262. ** WHERE clause.
  263. */
  264. if( sqlite3ExprResolveNames(&sNC, pWhere) ){
  265. goto update_cleanup;
  266. }
  267. /* Start the view context
  268. */
  269. if( isView ){
  270. sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
  271. }
  272. /* If we are trying to update a view, realize that view into
  273. ** a ephemeral table.
  274. */
  275. if( isView ){
  276. Select *pView;
  277. pView = sqlite3SelectDup(db, pTab->pSelect);
  278. sqlite3Select(pParse, pView, SRT_EphemTab, iCur, 0, 0, 0, 0);
  279. sqlite3SelectDelete(pView);
  280. }
  281. /* Begin the database scan
  282. */
  283. pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0);
  284. if( pWInfo==0 ) goto update_cleanup;
  285. /* Remember the rowid of every item to be updated.
  286. */
  287. sqlite3VdbeAddOp(v, IsVirtual(pTab) ? OP_VRowid : OP_Rowid, iCur, 0);
  288. sqlite3VdbeAddOp(v, OP_FifoWrite, 0, 0);
  289. /* End the database scan loop.
  290. */
  291. sqlite3WhereEnd(pWInfo);
  292. /* Initialize the count of updated rows
  293. */
  294. if( db->flags & SQLITE_CountRows && !pParse->trigStack ){
  295. memCnt = pParse->nMem++;
  296. sqlite3VdbeAddOp(v, OP_MemInt, 0, memCnt);
  297. }
  298. if( triggers_exist ){
  299. /* Create pseudo-tables for NEW and OLD
  300. */
  301. sqlite3VdbeAddOp(v, OP_OpenPseudo, oldIdx, 0);
  302. sqlite3VdbeAddOp(v, OP_SetNumColumns, oldIdx, pTab->nCol);
  303. sqlite3VdbeAddOp(v, OP_OpenPseudo, newIdx, 0);
  304. sqlite3VdbeAddOp(v, OP_SetNumColumns, newIdx, pTab->nCol);
  305. /* The top of the update loop for when there are triggers.
  306. */
  307. addr = sqlite3VdbeAddOp(v, OP_FifoRead, 0, 0);
  308. sqlite3VdbeAddOp(v, OP_StackDepth, -1, 0);
  309. sqlite3VdbeAddOp(v, OP_MemStore, mem1, 0);
  310. if( !isView ){
  311. /* Open a cursor and make it point to the record that is
  312. ** being updated.
  313. */
  314. sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenRead);
  315. }
  316. sqlite3VdbeAddOp(v, OP_NotExists, iCur, addr);
  317. /* Generate the OLD table
  318. */
  319. sqlite3VdbeAddOp(v, OP_Rowid, iCur, 0);
  320. sqlite3VdbeAddOp(v, OP_RowData, iCur, 0);
  321. sqlite3VdbeAddOp(v, OP_Insert, oldIdx, 0);
  322. /* Generate the NEW table
  323. */
  324. if( chngRowid ){
  325. sqlite3ExprCodeAndCache(pParse, pRowidExpr);
  326. }else{
  327. sqlite3VdbeAddOp(v, OP_Rowid, iCur, 0);
  328. }
  329. for(i=0; i<pTab->nCol; i++){
  330. if( i==pTab->iPKey ){
  331. sqlite3VdbeAddOp(v, OP_Null, 0, 0);
  332. continue;
  333. }
  334. j = aXRef[i];
  335. if( j<0 ){
  336. sqlite3VdbeAddOp(v, OP_Column, iCur, i);
  337. sqlite3ColumnDefault(v, pTab, i);
  338. }else{
  339. sqlite3ExprCodeAndCache(pParse, pChanges->a[j].pExpr);
  340. }
  341. }
  342. sqlite3VdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
  343. if( !isView ){
  344. sqlite3TableAffinityStr(v, pTab);
  345. }
  346. if( pParse->nErr ) goto update_cleanup;
  347. sqlite3VdbeAddOp(v, OP_Insert, newIdx, 0);
  348. if( !isView ){
  349. sqlite3VdbeAddOp(v, OP_Close, iCur, 0);
  350. }
  351. /* Fire the BEFORE and INSTEAD OF triggers
  352. */
  353. if( sqlite3CodeRowTrigger(pParse, TK_UPDATE, pChanges, TRIGGER_BEFORE, pTab,
  354. newIdx, oldIdx, onError, addr) ){
  355. goto update_cleanup;
  356. }
  357. if( !isView ){
  358. sqlite3VdbeAddOp(v, OP_MemLoad, mem1, 0);
  359. }
  360. }
  361. if( !isView && !IsVirtual(pTab) ){
  362. /*
  363. ** Open every index that needs updating. Note that if any
  364. ** index could potentially invoke a REPLACE conflict resolution
  365. ** action, then we need to open all indices because we might need
  366. ** to be deleting some records.
  367. */
  368. sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenWrite);
  369. if( onError==OE_Replace ){
  370. openAll = 1;
  371. }else{
  372. openAll = 0;
  373. for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
  374. if( pIdx->onError==OE_Replace ){
  375. openAll = 1;
  376. break;
  377. }
  378. }
  379. }
  380. for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
  381. if( openAll || aIdxUsed[i] ){
  382. KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
  383. sqlite3VdbeAddOp(v, OP_Integer, iDb, 0);
  384. sqlite3VdbeOp3(v, OP_OpenWrite, iCur+i+1, pIdx->tnum,
  385. (char*)pKey, P3_KEYINFO_HANDOFF);
  386. assert( pParse->nTab>iCur+i+1 );
  387. }
  388. }
  389. /* Loop over every record that needs updating. We have to load
  390. ** the old data for each record to be updated because some columns
  391. ** might not change and we will need to copy the old value.
  392. ** Also, the old data is needed to delete the old index entries.
  393. ** So make the cursor point at the old record.
  394. */
  395. if( !triggers_exist ){
  396. addr = sqlite3VdbeAddOp(v, OP_FifoRead, 0, 0);
  397. sqlite3VdbeAddOp(v, OP_StackDepth, -1, 0);
  398. sqlite3VdbeAddOp(v, OP_MemStore, mem1, 0);
  399. }
  400. sqlite3VdbeAddOp(v, OP_NotExists, iCur, addr);
  401. sqlite3VdbeAddOp(v, OP_MemLoad, mem1, 0);
  402. /* If the record number will change, push the record number as it
  403. ** will be after the update. (The old record number is currently
  404. ** on top of the stack.)
  405. */
  406. if( chngRowid ){
  407. sqlite3ExprCode(pParse, pRowidExpr);
  408. sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0);
  409. }
  410. /* Compute new data for this record.
  411. */
  412. for(i=0; i<pTab->nCol; i++){
  413. if( i==pTab->iPKey ){
  414. sqlite3VdbeAddOp(v, OP_Null, 0, 0);
  415. continue;
  416. }
  417. j = aXRef[i];
  418. if( j<0 ){
  419. sqlite3VdbeAddOp(v, OP_Column, iCur, i);
  420. sqlite3ColumnDefault(v, pTab, i);
  421. }else{
  422. sqlite3ExprCode(pParse, pChanges->a[j].pExpr);
  423. }
  424. }
  425. /* Do constraint checks
  426. */
  427. sqlite3GenerateConstraintChecks(pParse, pTab, iCur, aIdxUsed, chngRowid, 1,
  428. onError, addr);
  429. /* Delete the old indices for the current record.
  430. */
  431. sqlite3GenerateRowIndexDelete(v, pTab, iCur, aIdxUsed);
  432. /* If changing the record number, delete the old record.
  433. */
  434. if( chngRowid ){
  435. sqlite3VdbeAddOp(v, OP_Delete, iCur, 0);
  436. }
  437. /* Create the new index entries and the new record.
  438. */
  439. sqlite3CompleteInsertion(pParse, pTab, iCur, aIdxUsed, chngRowid, 1, -1, 0);
  440. }
  441. /* Increment the row counter
  442. */
  443. if( db->flags & SQLITE_CountRows && !pParse->trigStack){
  444. sqlite3VdbeAddOp(v, OP_MemIncr, 1, memCnt);
  445. }
  446. /* If there are triggers, close all the cursors after each iteration
  447. ** through the loop. The fire the after triggers.
  448. */
  449. if( triggers_exist ){
  450. if( !isView ){
  451. for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
  452. if( openAll || aIdxUsed[i] )
  453. sqlite3VdbeAddOp(v, OP_Close, iCur+i+1, 0);
  454. }
  455. sqlite3VdbeAddOp(v, OP_Close, iCur, 0);
  456. }
  457. if( sqlite3CodeRowTrigger(pParse, TK_UPDATE, pChanges, TRIGGER_AFTER, pTab,
  458. newIdx, oldIdx, onError, addr) ){
  459. goto update_cleanup;
  460. }
  461. }
  462. /* Repeat the above with the next record to be updated, until
  463. ** all record selected by the WHERE clause have been updated.
  464. */
  465. sqlite3VdbeAddOp(v, OP_Goto, 0, addr);
  466. sqlite3VdbeJumpHere(v, addr);
  467. /* Close all tables if there were no FOR EACH ROW triggers */
  468. if( !triggers_exist ){
  469. for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
  470. if( openAll || aIdxUsed[i] ){
  471. sqlite3VdbeAddOp(v, OP_Close, iCur+i+1, 0);
  472. }
  473. }
  474. sqlite3VdbeAddOp(v, OP_Close, iCur, 0);
  475. }else{
  476. sqlite3VdbeAddOp(v, OP_Close, newIdx, 0);
  477. sqlite3VdbeAddOp(v, OP_Close, oldIdx, 0);
  478. }
  479. /*
  480. ** Return the number of rows that were changed. If this routine is
  481. ** generating code because of a call to sqlite3NestedParse(), do not
  482. ** invoke the callback function.
  483. */
  484. if( db->flags & SQLITE_CountRows && !pParse->trigStack && pParse->nested==0 ){
  485. sqlite3VdbeAddOp(v, OP_MemLoad, memCnt, 0);
  486. sqlite3VdbeAddOp(v, OP_Callback, 1, 0);
  487. sqlite3VdbeSetNumCols(v, 1);
  488. sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows updated", P3_STATIC);
  489. }
  490. update_cleanup:
  491. sqlite3AuthContextPop(&sContext);
  492. sqlite3_free(apIdx);
  493. sqlite3_free(aXRef);
  494. sqlite3SrcListDelete(pTabList);
  495. sqlite3ExprListDelete(pChanges);
  496. sqlite3ExprDelete(pWhere);
  497. return;
  498. }
  499. #ifndef SQLITE_OMIT_VIRTUALTABLE
  500. /*
  501. ** Generate code for an UPDATE of a virtual table.
  502. **
  503. ** The strategy is that we create an ephemerial table that contains
  504. ** for each row to be changed:
  505. **
  506. ** (A) The original rowid of that row.
  507. ** (B) The revised rowid for the row. (note1)
  508. ** (C) The content of every column in the row.
  509. **
  510. ** Then we loop over this ephemeral table and for each row in
  511. ** the ephermeral table call VUpdate.
  512. **
  513. ** When finished, drop the ephemeral table.
  514. **
  515. ** (note1) Actually, if we know in advance that (A) is always the same
  516. ** as (B) we only store (A), then duplicate (A) when pulling
  517. ** it out of the ephemeral table before calling VUpdate.
  518. */
  519. static void updateVirtualTable(
  520. Parse *pParse, /* The parsing context */
  521. SrcList *pSrc, /* The virtual table to be modified */
  522. Table *pTab, /* The virtual table */
  523. ExprList *pChanges, /* The columns to change in the UPDATE statement */
  524. Expr *pRowid, /* Expression used to recompute the rowid */
  525. int *aXRef, /* Mapping from columns of pTab to entries in pChanges */
  526. Expr *pWhere /* WHERE clause of the UPDATE statement */
  527. ){
  528. Vdbe *v = pParse->pVdbe; /* Virtual machine under construction */
  529. ExprList *pEList = 0; /* The result set of the SELECT statement */
  530. Select *pSelect = 0; /* The SELECT statement */
  531. Expr *pExpr; /* Temporary expression */
  532. int ephemTab; /* Table holding the result of the SELECT */
  533. int i; /* Loop counter */
  534. int addr; /* Address of top of loop */
  535. sqlite3 *db = pParse->db; /* Database connection */
  536. /* Construct the SELECT statement that will find the new values for
  537. ** all updated rows.
  538. */
  539. pEList = sqlite3ExprListAppend(pParse, 0,
  540. sqlite3CreateIdExpr(pParse, "_rowid_"), 0);
  541. if( pRowid ){
  542. pEList = sqlite3ExprListAppend(pParse, pEList,
  543. sqlite3ExprDup(db, pRowid), 0);
  544. }
  545. assert( pTab->iPKey<0 );
  546. for(i=0; i<pTab->nCol; i++){
  547. if( aXRef[i]>=0 ){
  548. pExpr = sqlite3ExprDup(db, pChanges->a[aXRef[i]].pExpr);
  549. }else{
  550. pExpr = sqlite3CreateIdExpr(pParse, pTab->aCol[i].zName);
  551. }
  552. pEList = sqlite3ExprListAppend(pParse, pEList, pExpr, 0);
  553. }
  554. pSelect = sqlite3SelectNew(pParse, pEList, pSrc, pWhere, 0, 0, 0, 0, 0, 0);
  555. /* Create the ephemeral table into which the update results will
  556. ** be stored.
  557. */
  558. assert( v );
  559. ephemTab = pParse->nTab++;
  560. sqlite3VdbeAddOp(v, OP_OpenEphemeral, ephemTab, pTab->nCol+1+(pRowid!=0));
  561. /* fill the ephemeral table
  562. */
  563. sqlite3Select(pParse, pSelect, SRT_Table, ephemTab, 0, 0, 0, 0);
  564. /*
  565. ** Generate code to scan the ephemeral table and call VDelete and
  566. ** VInsert
  567. */
  568. sqlite3VdbeAddOp(v, OP_Rewind, ephemTab, 0);
  569. addr = sqlite3VdbeCurrentAddr(v);
  570. sqlite3VdbeAddOp(v, OP_Column, ephemTab, 0);
  571. if( pRowid ){
  572. sqlite3VdbeAddOp(v, OP_Column, ephemTab, 1);
  573. }else{
  574. sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
  575. }
  576. for(i=0; i<pTab->nCol; i++){
  577. sqlite3VdbeAddOp(v, OP_Column, ephemTab, i+1+(pRowid!=0));
  578. }
  579. pParse->pVirtualLock = pTab;
  580. sqlite3VdbeOp3(v, OP_VUpdate, 0, pTab->nCol+2,
  581. (const char*)pTab->pVtab, P3_VTAB);
  582. sqlite3VdbeAddOp(v, OP_Next, ephemTab, addr);
  583. sqlite3VdbeJumpHere(v, addr-1);
  584. sqlite3VdbeAddOp(v, OP_Close, ephemTab, 0);
  585. /* Cleanup */
  586. sqlite3SelectDelete(pSelect);
  587. }
  588. #endif /* SQLITE_OMIT_VIRTUALTABLE */