blob: 5f8269d9fd2fe26dc09ba0ecb0208f2fac9bbb82 [file] [log] [blame]
drhcce7d172000-05-31 15:34:51 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drhcce7d172000-05-31 15:34:51 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drhcce7d172000-05-31 15:34:51 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** 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.
drhcce7d172000-05-31 15:34:51 +000010**
11*************************************************************************
12** This file contains C code routines that are called by the parser
drh23bf66d2004-12-14 03:34:34 +000013** in order to generate code for DELETE FROM statements.
drhcce7d172000-05-31 15:34:51 +000014**
drh66a51672008-01-03 00:01:23 +000015** $Id: delete.c,v 1.140 2008/01/03 00:01:24 drh Exp $
drhcce7d172000-05-31 15:34:51 +000016*/
17#include "sqliteInt.h"
18
drha76b5df2002-02-23 02:32:10 +000019/*
drh812d7a22003-03-27 13:50:00 +000020** Look up every table that is named in pSrc. If any table is not found,
21** add an error message to pParse->zErrMsg and return NULL. If all tables
22** are found, return a pointer to the last table.
drha76b5df2002-02-23 02:32:10 +000023*/
danielk19774adee202004-05-08 08:23:19 +000024Table *sqlite3SrcListLookup(Parse *pParse, SrcList *pSrc){
drh812d7a22003-03-27 13:50:00 +000025 Table *pTab = 0;
26 int i;
drh855eb1c2004-08-31 13:45:11 +000027 struct SrcList_item *pItem;
28 for(i=0, pItem=pSrc->a; i<pSrc->nSrc; i++, pItem++){
29 pTab = sqlite3LocateTable(pParse, pItem->zName, pItem->zDatabase);
danielk1977a04a34f2007-04-16 15:06:25 +000030 sqlite3DeleteTable(pItem->pTab);
drh855eb1c2004-08-31 13:45:11 +000031 pItem->pTab = pTab;
drhed8a3bb2005-06-06 21:19:56 +000032 if( pTab ){
33 pTab->nRef++;
34 }
drha76b5df2002-02-23 02:32:10 +000035 }
36 return pTab;
37}
38
39/*
drh812d7a22003-03-27 13:50:00 +000040** Check to make sure the given table is writable. If it is not
41** writable, generate an error message and return 1. If it is
42** writable return 0;
43*/
danielk19774adee202004-05-08 08:23:19 +000044int sqlite3IsReadOnly(Parse *pParse, Table *pTab, int viewOk){
drh4cbdda92006-06-14 19:00:20 +000045 if( (pTab->readOnly && (pParse->db->flags & SQLITE_WriteSchema)==0
46 && pParse->nested==0)
47#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk1977d1ab1ba2006-06-15 04:28:13 +000048 || (pTab->pMod && pTab->pMod->pModule->xUpdate==0)
drh4cbdda92006-06-14 19:00:20 +000049#endif
50 ){
danielk19774adee202004-05-08 08:23:19 +000051 sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName);
drh5cf590c2003-04-24 01:45:04 +000052 return 1;
53 }
danielk1977b84f96f2005-01-20 11:32:23 +000054#ifndef SQLITE_OMIT_VIEW
drh5cf590c2003-04-24 01:45:04 +000055 if( !viewOk && pTab->pSelect ){
drhda71ce12004-06-21 18:14:45 +000056 sqlite3ErrorMsg(pParse,"cannot modify %s because it is a view",pTab->zName);
drh812d7a22003-03-27 13:50:00 +000057 return 1;
58 }
danielk1977b84f96f2005-01-20 11:32:23 +000059#endif
drh812d7a22003-03-27 13:50:00 +000060 return 0;
61}
62
63/*
drhad6d9462004-09-19 02:15:24 +000064** Generate code that will open a table for reading.
65*/
danielk1977c00da102006-01-07 13:21:04 +000066void sqlite3OpenTable(
67 Parse *p, /* Generate code into this VDBE */
drhad6d9462004-09-19 02:15:24 +000068 int iCur, /* The cursor number of the table */
danielk1977da184232006-01-05 11:34:32 +000069 int iDb, /* The database index in sqlite3.aDb[] */
danielk1977c00da102006-01-07 13:21:04 +000070 Table *pTab, /* The table to be opened */
71 int opcode /* OP_OpenRead or OP_OpenWrite */
drhad6d9462004-09-19 02:15:24 +000072){
drh4cbdda92006-06-14 19:00:20 +000073 Vdbe *v;
74 if( IsVirtual(pTab) ) return;
75 v = sqlite3GetVdbe(p);
danielk1977c00da102006-01-07 13:21:04 +000076 assert( opcode==OP_OpenWrite || opcode==OP_OpenRead );
77 sqlite3TableLock(p, iDb, pTab->tnum, (opcode==OP_OpenWrite), pTab->zName);
drh66a51672008-01-03 00:01:23 +000078 sqlite3VdbeAddOp1(v, OP_Integer, iDb);
drhd4e70eb2008-01-02 00:34:36 +000079 VdbeComment((v, "%s", pTab->zName));
drh66a51672008-01-03 00:01:23 +000080 sqlite3VdbeAddOp2(v, opcode, iCur, pTab->tnum);
81 sqlite3VdbeAddOp2(v, OP_SetNumColumns, iCur, pTab->nCol);
drhad6d9462004-09-19 02:15:24 +000082}
83
danielk1977299b1872004-11-22 10:02:10 +000084
drhad6d9462004-09-19 02:15:24 +000085/*
drh23bf66d2004-12-14 03:34:34 +000086** Generate code for a DELETE FROM statement.
87**
88** DELETE FROM table_wxyz WHERE a<5 AND b NOT NULL;
89** \________/ \________________/
90** pTabList pWhere
drhcce7d172000-05-31 15:34:51 +000091*/
danielk19774adee202004-05-08 08:23:19 +000092void sqlite3DeleteFrom(
drhcce7d172000-05-31 15:34:51 +000093 Parse *pParse, /* The parser context */
drh113088e2003-03-20 01:16:58 +000094 SrcList *pTabList, /* The table from which we should delete things */
drhcce7d172000-05-31 15:34:51 +000095 Expr *pWhere /* The WHERE clause. May be null */
96){
97 Vdbe *v; /* The virtual database engine */
98 Table *pTab; /* The table from which records will be deleted */
drhe22a3342003-04-22 20:30:37 +000099 const char *zDb; /* Name of database holding pTab */
danielk1977299b1872004-11-22 10:02:10 +0000100 int end, addr = 0; /* A couple addresses of generated code */
drhcce7d172000-05-31 15:34:51 +0000101 int i; /* Loop counter */
102 WhereInfo *pWInfo; /* Information about the WHERE clause */
103 Index *pIdx; /* For looping over indices of the table */
drh6a3ea0e2003-05-02 14:32:12 +0000104 int iCur; /* VDBE Cursor number for pTab */
drh9bb575f2004-09-06 17:24:11 +0000105 sqlite3 *db; /* Main database structure */
drh85e20962003-04-25 17:52:11 +0000106 AuthContext sContext; /* Authorization context */
drh798da522004-11-04 04:42:28 +0000107 int oldIdx = -1; /* Cursor for the OLD table of AFTER triggers */
danielk1977b3bce662005-01-29 08:32:43 +0000108 NameContext sNC; /* Name context to resolve expressions in */
drh53a67772007-02-07 01:06:52 +0000109 int iDb; /* Database number */
110 int memCnt = 0; /* Memory cell used for change counting */
drh1bee3d72001-10-15 00:44:35 +0000111
drh798da522004-11-04 04:42:28 +0000112#ifndef SQLITE_OMIT_TRIGGER
113 int isView; /* True if attempting to delete from a view */
drhdca76842004-12-07 14:06:13 +0000114 int triggers_exist = 0; /* True if any triggers exist */
drh798da522004-11-04 04:42:28 +0000115#endif
danielk19778f2c54e2008-01-01 19:02:09 +0000116 int iBeginAfterTrigger; /* Address of after trigger program */
117 int iEndAfterTrigger; /* Exit of after trigger program */
118 int iBeginBeforeTrigger; /* Address of before trigger program */
119 int iEndBeforeTrigger; /* Exit of before trigger program */
120 u32 old_col_mask = 0; /* Mask of OLD.* columns in use */
drhcce7d172000-05-31 15:34:51 +0000121
drh85e20962003-04-25 17:52:11 +0000122 sContext.pParse = 0;
drh17435752007-08-16 04:30:38 +0000123 db = pParse->db;
124 if( pParse->nErr || db->mallocFailed ){
drhdaffd0e2001-04-11 14:28:42 +0000125 goto delete_from_cleanup;
126 }
drh113088e2003-03-20 01:16:58 +0000127 assert( pTabList->nSrc==1 );
drhdaffd0e2001-04-11 14:28:42 +0000128
drh1ccde152000-06-17 13:12:39 +0000129 /* Locate the table which we want to delete. This table has to be
drhad3cab52002-05-24 02:04:32 +0000130 ** put in an SrcList structure because some of the subroutines we
drhcce7d172000-05-31 15:34:51 +0000131 ** will be calling are designed to work with multiple tables and expect
drhad3cab52002-05-24 02:04:32 +0000132 ** an SrcList* parameter instead of just a Table* parameter.
drhcce7d172000-05-31 15:34:51 +0000133 */
danielk19774adee202004-05-08 08:23:19 +0000134 pTab = sqlite3SrcListLookup(pParse, pTabList);
drha69d9162003-04-17 22:57:53 +0000135 if( pTab==0 ) goto delete_from_cleanup;
drhb7f91642004-10-31 02:22:47 +0000136
137 /* Figure out if we have any triggers and if the table being
138 ** deleted from is a view
139 */
140#ifndef SQLITE_OMIT_TRIGGER
drhdca76842004-12-07 14:06:13 +0000141 triggers_exist = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0);
drh5cf590c2003-04-24 01:45:04 +0000142 isView = pTab->pSelect!=0;
drhb7f91642004-10-31 02:22:47 +0000143#else
drhdca76842004-12-07 14:06:13 +0000144# define triggers_exist 0
drhb7f91642004-10-31 02:22:47 +0000145# define isView 0
146#endif
147#ifdef SQLITE_OMIT_VIEW
148# undef isView
149# define isView 0
150#endif
151
drhdca76842004-12-07 14:06:13 +0000152 if( sqlite3IsReadOnly(pParse, pTab, triggers_exist) ){
drh5cf590c2003-04-24 01:45:04 +0000153 goto delete_from_cleanup;
drh113088e2003-03-20 01:16:58 +0000154 }
danielk1977da184232006-01-05 11:34:32 +0000155 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
156 assert( iDb<db->nDb );
157 zDb = db->aDb[iDb].zName;
danielk19774adee202004-05-08 08:23:19 +0000158 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
drhe5f9c642003-01-13 23:27:31 +0000159 goto delete_from_cleanup;
160 }
drhcce7d172000-05-31 15:34:51 +0000161
drh5cf590c2003-04-24 01:45:04 +0000162 /* If pTab is really a view, make sure it has been initialized.
163 */
danielk1977b3d24bf2006-06-19 03:05:10 +0000164 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
drh5cf590c2003-04-24 01:45:04 +0000165 goto delete_from_cleanup;
166 }
167
drhc977f7f2002-05-21 11:38:11 +0000168 /* Allocate a cursor used to store the old.* data for a trigger.
169 */
drhdca76842004-12-07 14:06:13 +0000170 if( triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000171 oldIdx = pParse->nTab++;
danielk1977f29ce552002-05-19 23:43:12 +0000172 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000173
danielk1977b3bce662005-01-29 08:32:43 +0000174 /* Resolve the column names in the WHERE clause.
drhcce7d172000-05-31 15:34:51 +0000175 */
drh6a3ea0e2003-05-02 14:32:12 +0000176 assert( pTabList->nSrc==1 );
177 iCur = pTabList->a[0].iCursor = pParse->nTab++;
danielk1977e448dc42008-01-02 11:50:51 +0000178 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
179 pParse->nTab++;
180 }
danielk1977b3bce662005-01-29 08:32:43 +0000181 memset(&sNC, 0, sizeof(sNC));
182 sNC.pParse = pParse;
183 sNC.pSrcList = pTabList;
184 if( sqlite3ExprResolveNames(&sNC, pWhere) ){
drh290c1942004-08-21 17:54:45 +0000185 goto delete_from_cleanup;
drhcce7d172000-05-31 15:34:51 +0000186 }
187
drh85e20962003-04-25 17:52:11 +0000188 /* Start the view context
189 */
190 if( isView ){
danielk19774adee202004-05-08 08:23:19 +0000191 sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
drh85e20962003-04-25 17:52:11 +0000192 }
193
drhcce7d172000-05-31 15:34:51 +0000194 /* Begin generating code.
195 */
danielk19774adee202004-05-08 08:23:19 +0000196 v = sqlite3GetVdbe(pParse);
danielk1977f29ce552002-05-19 23:43:12 +0000197 if( v==0 ){
198 goto delete_from_cleanup;
199 }
drh4794f732004-11-05 17:17:50 +0000200 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
danielk1977da184232006-01-05 11:34:32 +0000201 sqlite3BeginWriteOperation(pParse, triggers_exist, iDb);
drh5e00f6c2001-09-13 13:46:56 +0000202
danielk19778f2c54e2008-01-01 19:02:09 +0000203 if( triggers_exist ){
drh66a51672008-01-03 00:01:23 +0000204 int iGoto = sqlite3VdbeAddOp0(v, OP_Goto);
danielk19778f2c54e2008-01-01 19:02:09 +0000205 addr = sqlite3VdbeMakeLabel(v);
206 iBeginBeforeTrigger = sqlite3VdbeCurrentAddr(v);
207 (void)sqlite3CodeRowTrigger(pParse, TK_DELETE, 0, TRIGGER_BEFORE, pTab,
208 -1, oldIdx, (pParse->trigStack)?pParse->trigStack->orconf:OE_Default,
209 addr, &old_col_mask, 0);
drh66a51672008-01-03 00:01:23 +0000210 iEndBeforeTrigger = sqlite3VdbeAddOp0(v, OP_Goto);
danielk19778f2c54e2008-01-01 19:02:09 +0000211 iBeginAfterTrigger = sqlite3VdbeCurrentAddr(v);
212 (void)sqlite3CodeRowTrigger(pParse, TK_DELETE, 0, TRIGGER_AFTER, pTab, -1,
213 oldIdx, (pParse->trigStack)?pParse->trigStack->orconf:OE_Default,
214 addr, &old_col_mask, 0);
drh66a51672008-01-03 00:01:23 +0000215 iEndAfterTrigger = sqlite3VdbeAddOp0(v, OP_Goto);
danielk19778f2c54e2008-01-01 19:02:09 +0000216 sqlite3VdbeJumpHere(v, iGoto);
217 }
218
drh9d2985c2005-09-08 01:58:42 +0000219 /* If we are trying to delete from a view, realize that view into
220 ** a ephemeral table.
drh5cf590c2003-04-24 01:45:04 +0000221 */
222 if( isView ){
danielk19776c8c8ce2008-01-02 16:27:09 +0000223 SelectDest dest = {SRT_EphemTab, 0, 0};
drh17435752007-08-16 04:30:38 +0000224 Select *pView = sqlite3SelectDup(db, pTab->pSelect);
danielk19778f2c54e2008-01-01 19:02:09 +0000225 sqlite3SelectMask(pParse, pView, old_col_mask);
danielk19776c8c8ce2008-01-02 16:27:09 +0000226 dest.iParm = iCur;
227 sqlite3Select(pParse, pView, &dest, 0, 0, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000228 sqlite3SelectDelete(pView);
drh5cf590c2003-04-24 01:45:04 +0000229 }
230
drh1bee3d72001-10-15 00:44:35 +0000231 /* Initialize the counter of the number of rows deleted, if
232 ** we are counting rows.
233 */
234 if( db->flags & SQLITE_CountRows ){
drh53a67772007-02-07 01:06:52 +0000235 memCnt = pParse->nMem++;
drh66a51672008-01-03 00:01:23 +0000236 sqlite3VdbeAddOp2(v, OP_MemInt, 0, memCnt);
drh1bee3d72001-10-15 00:44:35 +0000237 }
drhcce7d172000-05-31 15:34:51 +0000238
drh0353ced2001-03-20 22:05:00 +0000239 /* Special case: A DELETE without a WHERE clause deletes everything.
drhc977f7f2002-05-21 11:38:11 +0000240 ** It is easier just to erase the whole table. Note, however, that
241 ** this means that the row change count will be incorrect.
drhcce7d172000-05-31 15:34:51 +0000242 */
drh4cbdda92006-06-14 19:00:20 +0000243 if( pWhere==0 && !triggers_exist && !IsVirtual(pTab) ){
drh1bee3d72001-10-15 00:44:35 +0000244 if( db->flags & SQLITE_CountRows ){
245 /* If counting rows deleted, just count the total number of
246 ** entries in the table. */
danielk1977f0113002006-01-24 12:09:17 +0000247 int addr2;
drh5cf590c2003-04-24 01:45:04 +0000248 if( !isView ){
danielk1977c00da102006-01-07 13:21:04 +0000249 sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenRead);
drh5cf590c2003-04-24 01:45:04 +0000250 }
drh66a51672008-01-03 00:01:23 +0000251 sqlite3VdbeAddOp2(v, OP_Rewind, iCur, sqlite3VdbeCurrentAddr(v)+2);
252 addr2 = sqlite3VdbeAddOp2(v, OP_MemIncr, 1, memCnt);
253 sqlite3VdbeAddOp2(v, OP_Next, iCur, addr2);
254 sqlite3VdbeAddOp1(v, OP_Close, iCur);
drh1bee3d72001-10-15 00:44:35 +0000255 }
drh5cf590c2003-04-24 01:45:04 +0000256 if( !isView ){
drh66a51672008-01-03 00:01:23 +0000257 sqlite3VdbeAddOp2(v, OP_Clear, pTab->tnum, iDb);
danielk197794eb6a12005-12-15 15:22:08 +0000258 if( !pParse->nested ){
drh66a51672008-01-03 00:01:23 +0000259 sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_STATIC);
danielk197794eb6a12005-12-15 15:22:08 +0000260 }
drh5cf590c2003-04-24 01:45:04 +0000261 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
danielk1977da184232006-01-05 11:34:32 +0000262 assert( pIdx->pSchema==pTab->pSchema );
drh66a51672008-01-03 00:01:23 +0000263 sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb);
drh5cf590c2003-04-24 01:45:04 +0000264 }
drhcce7d172000-05-31 15:34:51 +0000265 }
danielk1977f9e7dda2006-06-16 16:08:53 +0000266 }
drh0353ced2001-03-20 22:05:00 +0000267 /* The usual case: There is a WHERE clause so we have to scan through
jplyon8bc03a72004-01-19 04:54:28 +0000268 ** the table and pick which records to delete.
drh0353ced2001-03-20 22:05:00 +0000269 */
270 else{
danielk1977299b1872004-11-22 10:02:10 +0000271 /* Begin the database scan
272 */
drhf8db1bc2005-04-22 02:38:37 +0000273 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0);
danielk1977299b1872004-11-22 10:02:10 +0000274 if( pWInfo==0 ) goto delete_from_cleanup;
275
drhe6f85e72004-12-25 01:03:13 +0000276 /* Remember the rowid of every item to be deleted.
danielk1977299b1872004-11-22 10:02:10 +0000277 */
drh66a51672008-01-03 00:01:23 +0000278 sqlite3VdbeAddOp1(v, IsVirtual(pTab) ? OP_VRowid : OP_Rowid, iCur);
279 sqlite3VdbeAddOp0(v, OP_FifoWrite);
danielk1977299b1872004-11-22 10:02:10 +0000280 if( db->flags & SQLITE_CountRows ){
drh66a51672008-01-03 00:01:23 +0000281 sqlite3VdbeAddOp2(v, OP_MemIncr, 1, memCnt);
danielk1977299b1872004-11-22 10:02:10 +0000282 }
283
284 /* End the database scan loop.
285 */
286 sqlite3WhereEnd(pWInfo);
287
drh70ce3f02003-04-15 19:22:22 +0000288 /* Open the pseudo-table used to store OLD if there are triggers.
289 */
drhdca76842004-12-07 14:06:13 +0000290 if( triggers_exist ){
drh66a51672008-01-03 00:01:23 +0000291 sqlite3VdbeAddOp1(v, OP_OpenPseudo, oldIdx);
292 sqlite3VdbeAddOp2(v, OP_SetNumColumns, oldIdx, pTab->nCol);
drh70ce3f02003-04-15 19:22:22 +0000293 }
294
danielk1977299b1872004-11-22 10:02:10 +0000295 /* Delete every item whose key was written to the list during the
296 ** database scan. We have to delete items after the scan is complete
297 ** because deleting an item can change the scan order.
danielk1977ed326d72004-11-16 15:50:19 +0000298 */
danielk1977299b1872004-11-22 10:02:10 +0000299 end = sqlite3VdbeMakeLabel(v);
danielk1977ed326d72004-11-16 15:50:19 +0000300
danielk1977e448dc42008-01-02 11:50:51 +0000301 if( !isView ){
302 /* Open cursors for the table we are deleting from and
303 ** all its indices.
304 */
305 sqlite3OpenTableAndIndices(pParse, pTab, iCur, OP_OpenWrite);
306 }
307
308 /* This is the beginning of the delete loop. If a trigger encounters
309 ** an IGNORE constraint, it jumps back to here.
drhc977f7f2002-05-21 11:38:11 +0000310 */
drhdca76842004-12-07 14:06:13 +0000311 if( triggers_exist ){
danielk19778f2c54e2008-01-01 19:02:09 +0000312 sqlite3VdbeResolveLabel(v, addr);
danielk1977e448dc42008-01-02 11:50:51 +0000313 }
drh66a51672008-01-03 00:01:23 +0000314 addr = sqlite3VdbeAddOp2(v, OP_FifoRead, 0, end);
315 sqlite3VdbeAddOp1(v, OP_StackDepth, -1);
danielk1977e448dc42008-01-02 11:50:51 +0000316
317 if( triggers_exist ){
318 int mem1 = pParse->nMem++;
danielk1977299b1872004-11-22 10:02:10 +0000319 if( !isView ){
drh66a51672008-01-03 00:01:23 +0000320 sqlite3VdbeAddOp1(v, OP_MemStore, mem1);
danielk1977299b1872004-11-22 10:02:10 +0000321 }
drh66a51672008-01-03 00:01:23 +0000322 sqlite3VdbeAddOp2(v, OP_NotExists, iCur, addr);
323 sqlite3VdbeAddOp1(v, OP_Rowid, iCur);
danielk19778f2c54e2008-01-01 19:02:09 +0000324 if( old_col_mask ){
drh66a51672008-01-03 00:01:23 +0000325 sqlite3VdbeAddOp1(v, OP_RowData, iCur);
danielk19778f2c54e2008-01-01 19:02:09 +0000326 }else{
drh66a51672008-01-03 00:01:23 +0000327 sqlite3VdbeAddOp0(v, OP_Null);
danielk19778f2c54e2008-01-01 19:02:09 +0000328 }
drh66a51672008-01-03 00:01:23 +0000329 sqlite3VdbeAddOp1(v, OP_Insert, oldIdx);
danielk1977299b1872004-11-22 10:02:10 +0000330
danielk19778f2c54e2008-01-01 19:02:09 +0000331 /* Jump back and run the BEFORE triggers */
drh66a51672008-01-03 00:01:23 +0000332 sqlite3VdbeAddOp2(v, OP_Goto, 0, iBeginBeforeTrigger);
danielk19778f2c54e2008-01-01 19:02:09 +0000333 sqlite3VdbeJumpHere(v, iEndBeforeTrigger);
334
drh95c07752007-11-11 18:36:34 +0000335 if( !isView ){
drh66a51672008-01-03 00:01:23 +0000336 sqlite3VdbeAddOp1(v, OP_MemLoad, mem1);
drh95c07752007-11-11 18:36:34 +0000337 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000338 }
339
danielk1977299b1872004-11-22 10:02:10 +0000340 if( !isView ){
danielk1977299b1872004-11-22 10:02:10 +0000341 /* Delete the row */
drh4cbdda92006-06-14 19:00:20 +0000342#ifndef SQLITE_OMIT_VIRTUALTABLE
343 if( IsVirtual(pTab) ){
danielk1977f9e7dda2006-06-16 16:08:53 +0000344 pParse->pVirtualLock = pTab;
drh66a51672008-01-03 00:01:23 +0000345 sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, 0,
346 (const char*)pTab->pVtab, P4_VTAB);
drh4cbdda92006-06-14 19:00:20 +0000347 }else
348#endif
349 {
350 sqlite3GenerateRowDelete(db, v, pTab, iCur, pParse->nested==0);
351 }
danielk1977299b1872004-11-22 10:02:10 +0000352 }
353
354 /* If there are row triggers, close all cursors then invoke
355 ** the AFTER triggers
356 */
drhdca76842004-12-07 14:06:13 +0000357 if( triggers_exist ){
danielk19778f2c54e2008-01-01 19:02:09 +0000358 /* Jump back and run the AFTER triggers */
drh66a51672008-01-03 00:01:23 +0000359 sqlite3VdbeAddOp2(v, OP_Goto, 0, iBeginAfterTrigger);
danielk19778f2c54e2008-01-01 19:02:09 +0000360 sqlite3VdbeJumpHere(v, iEndAfterTrigger);
danielk1977c3f9bad2002-05-15 08:30:12 +0000361 }
362
danielk1977299b1872004-11-22 10:02:10 +0000363 /* End of the delete loop */
drh66a51672008-01-03 00:01:23 +0000364 sqlite3VdbeAddOp2(v, OP_Goto, 0, addr);
danielk1977299b1872004-11-22 10:02:10 +0000365 sqlite3VdbeResolveLabel(v, end);
danielk1977299b1872004-11-22 10:02:10 +0000366
367 /* Close the cursors after the loop if there are no row triggers */
danielk1977e448dc42008-01-02 11:50:51 +0000368 if( !isView && !IsVirtual(pTab) ){
danielk1977299b1872004-11-22 10:02:10 +0000369 for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
drh66a51672008-01-03 00:01:23 +0000370 sqlite3VdbeAddOp2(v, OP_Close, iCur + i, pIdx->tnum);
danielk1977299b1872004-11-22 10:02:10 +0000371 }
drh66a51672008-01-03 00:01:23 +0000372 sqlite3VdbeAddOp1(v, OP_Close, iCur);
danielk1977c3f9bad2002-05-15 08:30:12 +0000373 }
drh0353ced2001-03-20 22:05:00 +0000374 }
drh5e00f6c2001-09-13 13:46:56 +0000375
drh1bee3d72001-10-15 00:44:35 +0000376 /*
danielk1977e7de6f22004-11-05 06:02:06 +0000377 ** Return the number of rows that were deleted. If this routine is
378 ** generating code because of a call to sqlite3NestedParse(), do not
379 ** invoke the callback function.
drh1bee3d72001-10-15 00:44:35 +0000380 */
danielk1977cc6bd382005-01-10 02:48:49 +0000381 if( db->flags & SQLITE_CountRows && pParse->nested==0 && !pParse->trigStack ){
drh66a51672008-01-03 00:01:23 +0000382 sqlite3VdbeAddOp2(v, OP_ResultRow, memCnt, 1);
danielk197722322fd2004-05-25 23:35:17 +0000383 sqlite3VdbeSetNumCols(v, 1);
drh66a51672008-01-03 00:01:23 +0000384 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows deleted", P4_STATIC);
drh1bee3d72001-10-15 00:44:35 +0000385 }
drhcce7d172000-05-31 15:34:51 +0000386
387delete_from_cleanup:
danielk19774adee202004-05-08 08:23:19 +0000388 sqlite3AuthContextPop(&sContext);
389 sqlite3SrcListDelete(pTabList);
390 sqlite3ExprDelete(pWhere);
drhcce7d172000-05-31 15:34:51 +0000391 return;
392}
drh9cfcf5d2002-01-29 18:41:24 +0000393
394/*
395** This routine generates VDBE code that causes a single row of a
396** single table to be deleted.
397**
398** The VDBE must be in a particular state when this routine is called.
399** These are the requirements:
400**
401** 1. A read/write cursor pointing to pTab, the table containing the row
402** to be deleted, must be opened as cursor number "base".
403**
404** 2. Read/write cursors for all indices of pTab must be open as
405** cursor number base+i for the i-th index.
406**
407** 3. The record number of the row to be deleted must be on the top
408** of the stack.
409**
410** This routine pops the top of the stack to remove the record number
411** and then generates code to remove both the table record and all index
412** entries that point to that record.
413*/
danielk19774adee202004-05-08 08:23:19 +0000414void sqlite3GenerateRowDelete(
drh9bb575f2004-09-06 17:24:11 +0000415 sqlite3 *db, /* The database containing the index */
drh9cfcf5d2002-01-29 18:41:24 +0000416 Vdbe *v, /* Generate code into this VDBE */
417 Table *pTab, /* Table containing the row to be deleted */
drh6a3ea0e2003-05-02 14:32:12 +0000418 int iCur, /* Cursor number for the table */
drhc8d30ac2002-04-12 10:08:59 +0000419 int count /* Increment the row change counter */
drh9cfcf5d2002-01-29 18:41:24 +0000420){
drh07d6e3a2002-05-23 12:50:18 +0000421 int addr;
drh66a51672008-01-03 00:01:23 +0000422 addr = sqlite3VdbeAddOp1(v, OP_NotExists, iCur);
drh74161702006-02-24 02:53:49 +0000423 sqlite3GenerateRowIndexDelete(v, pTab, iCur, 0);
drh66a51672008-01-03 00:01:23 +0000424 sqlite3VdbeAddOp2(v, OP_Delete, iCur, (count?OPFLAG_NCHANGE:0));
danielk197794eb6a12005-12-15 15:22:08 +0000425 if( count ){
drh66a51672008-01-03 00:01:23 +0000426 sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_STATIC);
danielk197794eb6a12005-12-15 15:22:08 +0000427 }
drhd654be82005-09-20 17:42:23 +0000428 sqlite3VdbeJumpHere(v, addr);
drh0ca3e242002-01-29 23:07:02 +0000429}
430
431/*
432** This routine generates VDBE code that causes the deletion of all
433** index entries associated with a single row of a single table.
434**
435** The VDBE must be in a particular state when this routine is called.
436** These are the requirements:
437**
438** 1. A read/write cursor pointing to pTab, the table containing the row
drh6a3ea0e2003-05-02 14:32:12 +0000439** to be deleted, must be opened as cursor number "iCur".
drh0ca3e242002-01-29 23:07:02 +0000440**
441** 2. Read/write cursors for all indices of pTab must be open as
drh6a3ea0e2003-05-02 14:32:12 +0000442** cursor number iCur+i for the i-th index.
drh0ca3e242002-01-29 23:07:02 +0000443**
drh6a3ea0e2003-05-02 14:32:12 +0000444** 3. The "iCur" cursor must be pointing to the row that is to be
drh0ca3e242002-01-29 23:07:02 +0000445** deleted.
446*/
danielk19774adee202004-05-08 08:23:19 +0000447void sqlite3GenerateRowIndexDelete(
drh0ca3e242002-01-29 23:07:02 +0000448 Vdbe *v, /* Generate code into this VDBE */
449 Table *pTab, /* Table containing the row to be deleted */
drh6a3ea0e2003-05-02 14:32:12 +0000450 int iCur, /* Cursor number for the table */
drh0ca3e242002-01-29 23:07:02 +0000451 char *aIdxUsed /* Only delete if aIdxUsed!=0 && aIdxUsed[i]!=0 */
452){
drh9cfcf5d2002-01-29 18:41:24 +0000453 int i;
454 Index *pIdx;
455
drh0ca3e242002-01-29 23:07:02 +0000456 for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
drh0ca3e242002-01-29 23:07:02 +0000457 if( aIdxUsed!=0 && aIdxUsed[i-1]==0 ) continue;
drh51846b52004-05-28 16:00:21 +0000458 sqlite3GenerateIndexKey(v, pIdx, iCur);
drh66a51672008-01-03 00:01:23 +0000459 sqlite3VdbeAddOp1(v, OP_IdxDelete, iCur+i);
drh9cfcf5d2002-01-29 18:41:24 +0000460 }
drh9cfcf5d2002-01-29 18:41:24 +0000461}
drh51846b52004-05-28 16:00:21 +0000462
463/*
464** Generate code that will assemble an index key and put it on the top
465** of the tack. The key with be for index pIdx which is an index on pTab.
466** iCur is the index of a cursor open on the pTab table and pointing to
467** the entry that needs indexing.
468*/
469void sqlite3GenerateIndexKey(
470 Vdbe *v, /* Generate code into this VDBE */
471 Index *pIdx, /* The index for which to generate a key */
472 int iCur /* Cursor number for the pIdx->pTable table */
473){
474 int j;
475 Table *pTab = pIdx->pTable;
476
drh66a51672008-01-03 00:01:23 +0000477 sqlite3VdbeAddOp1(v, OP_Rowid, iCur);
drh51846b52004-05-28 16:00:21 +0000478 for(j=0; j<pIdx->nColumn; j++){
479 int idx = pIdx->aiColumn[j];
480 if( idx==pTab->iPKey ){
drh66a51672008-01-03 00:01:23 +0000481 sqlite3VdbeAddOp1(v, OP_Dup, j);
drh51846b52004-05-28 16:00:21 +0000482 }else{
drh66a51672008-01-03 00:01:23 +0000483 sqlite3VdbeAddOp2(v, OP_Column, iCur, idx);
danielk1977aee18ef2005-03-09 12:26:50 +0000484 sqlite3ColumnDefault(v, pTab, idx);
drh51846b52004-05-28 16:00:21 +0000485 }
486 }
drh66a51672008-01-03 00:01:23 +0000487 sqlite3VdbeAddOp1(v, OP_MakeIdxRec, pIdx->nColumn);
drh51846b52004-05-28 16:00:21 +0000488 sqlite3IndexAffinityStr(v, pIdx);
489}