blob: e9b2bf70703b21770c965b4bc326997277e5e164 [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
13** to handle DELETE FROM statements.
14**
drhb7f91642004-10-31 02:22:47 +000015** $Id: delete.c,v 1.84 2004/10/31 02:22:49 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);
30 pItem->pTab = pTab;
drha76b5df2002-02-23 02:32:10 +000031 }
32 return pTab;
33}
34
35/*
drh812d7a22003-03-27 13:50:00 +000036** Check to make sure the given table is writable. If it is not
37** writable, generate an error message and return 1. If it is
38** writable return 0;
39*/
danielk19774adee202004-05-08 08:23:19 +000040int sqlite3IsReadOnly(Parse *pParse, Table *pTab, int viewOk){
drhf4040832004-10-22 20:29:21 +000041 if( pTab->readOnly && (pParse->db->flags & SQLITE_WriteSchema)==0 ){
danielk19774adee202004-05-08 08:23:19 +000042 sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName);
drh5cf590c2003-04-24 01:45:04 +000043 return 1;
44 }
45 if( !viewOk && pTab->pSelect ){
drhda71ce12004-06-21 18:14:45 +000046 sqlite3ErrorMsg(pParse,"cannot modify %s because it is a view",pTab->zName);
drh812d7a22003-03-27 13:50:00 +000047 return 1;
48 }
49 return 0;
50}
51
52/*
drhad6d9462004-09-19 02:15:24 +000053** Generate code that will open a table for reading.
54*/
55void sqlite3OpenTableForReading(
56 Vdbe *v, /* Generate code into this VDBE */
57 int iCur, /* The cursor number of the table */
58 Table *pTab /* The table to be opened */
59){
60 sqlite3VdbeAddOp(v, OP_Integer, pTab->iDb, 0);
61 sqlite3VdbeAddOp(v, OP_OpenRead, iCur, pTab->tnum);
62 VdbeComment((v, "# %s", pTab->zName));
63 sqlite3VdbeAddOp(v, OP_SetNumColumns, iCur, pTab->nCol);
64}
65
66
67/*
drhcce7d172000-05-31 15:34:51 +000068** Process a DELETE FROM statement.
69*/
danielk19774adee202004-05-08 08:23:19 +000070void sqlite3DeleteFrom(
drhcce7d172000-05-31 15:34:51 +000071 Parse *pParse, /* The parser context */
drh113088e2003-03-20 01:16:58 +000072 SrcList *pTabList, /* The table from which we should delete things */
drhcce7d172000-05-31 15:34:51 +000073 Expr *pWhere /* The WHERE clause. May be null */
74){
75 Vdbe *v; /* The virtual database engine */
76 Table *pTab; /* The table from which records will be deleted */
drhe22a3342003-04-22 20:30:37 +000077 const char *zDb; /* Name of database holding pTab */
danielk1977cfe9a692004-06-16 12:00:29 +000078 int end, addr = 0; /* A couple addresses of generated code */
drhcce7d172000-05-31 15:34:51 +000079 int i; /* Loop counter */
80 WhereInfo *pWInfo; /* Information about the WHERE clause */
81 Index *pIdx; /* For looping over indices of the table */
drh6a3ea0e2003-05-02 14:32:12 +000082 int iCur; /* VDBE Cursor number for pTab */
drh9bb575f2004-09-06 17:24:11 +000083 sqlite3 *db; /* Main database structure */
drh5cf590c2003-04-24 01:45:04 +000084 int isView; /* True if attempting to delete from a view */
drh85e20962003-04-25 17:52:11 +000085 AuthContext sContext; /* Authorization context */
drh1bee3d72001-10-15 00:44:35 +000086
drh70ce3f02003-04-15 19:22:22 +000087 int row_triggers_exist = 0; /* True if any triggers exist */
88 int before_triggers; /* True if there are BEFORE triggers */
89 int after_triggers; /* True if there are AFTER triggers */
90 int oldIdx = -1; /* Cursor for the OLD table of AFTER triggers */
drhcce7d172000-05-31 15:34:51 +000091
drh85e20962003-04-25 17:52:11 +000092 sContext.pParse = 0;
danielk197724b03fd2004-05-10 10:34:34 +000093 if( pParse->nErr || sqlite3_malloc_failed ){
drhdaffd0e2001-04-11 14:28:42 +000094 pTabList = 0;
95 goto delete_from_cleanup;
96 }
drhecdc7532001-09-23 02:35:53 +000097 db = pParse->db;
drh113088e2003-03-20 01:16:58 +000098 assert( pTabList->nSrc==1 );
drhdaffd0e2001-04-11 14:28:42 +000099
drh1ccde152000-06-17 13:12:39 +0000100 /* Locate the table which we want to delete. This table has to be
drhad3cab52002-05-24 02:04:32 +0000101 ** put in an SrcList structure because some of the subroutines we
drhcce7d172000-05-31 15:34:51 +0000102 ** will be calling are designed to work with multiple tables and expect
drhad3cab52002-05-24 02:04:32 +0000103 ** an SrcList* parameter instead of just a Table* parameter.
drhcce7d172000-05-31 15:34:51 +0000104 */
danielk19774adee202004-05-08 08:23:19 +0000105 pTab = sqlite3SrcListLookup(pParse, pTabList);
drha69d9162003-04-17 22:57:53 +0000106 if( pTab==0 ) goto delete_from_cleanup;
drhb7f91642004-10-31 02:22:47 +0000107
108 /* Figure out if we have any triggers and if the table being
109 ** deleted from is a view
110 */
111#ifndef SQLITE_OMIT_TRIGGER
danielk19774adee202004-05-08 08:23:19 +0000112 before_triggers = sqlite3TriggersExist(pParse, pTab->pTrigger,
drha69d9162003-04-17 22:57:53 +0000113 TK_DELETE, TK_BEFORE, TK_ROW, 0);
danielk19774adee202004-05-08 08:23:19 +0000114 after_triggers = sqlite3TriggersExist(pParse, pTab->pTrigger,
drha69d9162003-04-17 22:57:53 +0000115 TK_DELETE, TK_AFTER, TK_ROW, 0);
116 row_triggers_exist = before_triggers || after_triggers;
drh5cf590c2003-04-24 01:45:04 +0000117 isView = pTab->pSelect!=0;
drhb7f91642004-10-31 02:22:47 +0000118#else
119# define before_triggers 0
120# define after_triggers 0
121# define row_triggers_exist 0
122# define isView 0
123#endif
124#ifdef SQLITE_OMIT_VIEW
125# undef isView
126# define isView 0
127#endif
128
danielk19774adee202004-05-08 08:23:19 +0000129 if( sqlite3IsReadOnly(pParse, pTab, before_triggers) ){
drh5cf590c2003-04-24 01:45:04 +0000130 goto delete_from_cleanup;
drh113088e2003-03-20 01:16:58 +0000131 }
drhe22a3342003-04-22 20:30:37 +0000132 assert( pTab->iDb<db->nDb );
133 zDb = db->aDb[pTab->iDb].zName;
danielk19774adee202004-05-08 08:23:19 +0000134 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
drhe5f9c642003-01-13 23:27:31 +0000135 goto delete_from_cleanup;
136 }
drhcce7d172000-05-31 15:34:51 +0000137
drh5cf590c2003-04-24 01:45:04 +0000138 /* If pTab is really a view, make sure it has been initialized.
139 */
danielk19774adee202004-05-08 08:23:19 +0000140 if( isView && sqlite3ViewGetColumnNames(pParse, pTab) ){
drh5cf590c2003-04-24 01:45:04 +0000141 goto delete_from_cleanup;
142 }
143
drhc977f7f2002-05-21 11:38:11 +0000144 /* Allocate a cursor used to store the old.* data for a trigger.
145 */
danielk1977f29ce552002-05-19 23:43:12 +0000146 if( row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000147 oldIdx = pParse->nTab++;
danielk1977f29ce552002-05-19 23:43:12 +0000148 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000149
drh967e8b72000-06-21 13:59:10 +0000150 /* Resolve the column names in all the expressions.
drhcce7d172000-05-31 15:34:51 +0000151 */
drh6a3ea0e2003-05-02 14:32:12 +0000152 assert( pTabList->nSrc==1 );
153 iCur = pTabList->a[0].iCursor = pParse->nTab++;
drh290c1942004-08-21 17:54:45 +0000154 if( sqlite3ExprResolveAndCheck(pParse, pTabList, 0, pWhere, 0, 0) ){
155 goto delete_from_cleanup;
drhcce7d172000-05-31 15:34:51 +0000156 }
157
drh85e20962003-04-25 17:52:11 +0000158 /* Start the view context
159 */
160 if( isView ){
danielk19774adee202004-05-08 08:23:19 +0000161 sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
drh85e20962003-04-25 17:52:11 +0000162 }
163
drhcce7d172000-05-31 15:34:51 +0000164 /* Begin generating code.
165 */
danielk19774adee202004-05-08 08:23:19 +0000166 v = sqlite3GetVdbe(pParse);
danielk1977f29ce552002-05-19 23:43:12 +0000167 if( v==0 ){
168 goto delete_from_cleanup;
169 }
danielk1977b28af712004-06-21 06:50:26 +0000170 sqlite3VdbeCountChanges(v);
danielk19774adee202004-05-08 08:23:19 +0000171 sqlite3BeginWriteOperation(pParse, row_triggers_exist, pTab->iDb);
drh5e00f6c2001-09-13 13:46:56 +0000172
drh5cf590c2003-04-24 01:45:04 +0000173 /* If we are trying to delete from a view, construct that view into
174 ** a temporary table.
175 */
176 if( isView ){
danielk19774adee202004-05-08 08:23:19 +0000177 Select *pView = sqlite3SelectDup(pTab->pSelect);
danielk197784ac9d02004-05-18 09:58:06 +0000178 sqlite3Select(pParse, pView, SRT_TempTable, iCur, 0, 0, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000179 sqlite3SelectDelete(pView);
drh5cf590c2003-04-24 01:45:04 +0000180 }
181
drh1bee3d72001-10-15 00:44:35 +0000182 /* Initialize the counter of the number of rows deleted, if
183 ** we are counting rows.
184 */
185 if( db->flags & SQLITE_CountRows ){
danielk19774adee202004-05-08 08:23:19 +0000186 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
drh1bee3d72001-10-15 00:44:35 +0000187 }
drhcce7d172000-05-31 15:34:51 +0000188
drh0353ced2001-03-20 22:05:00 +0000189 /* Special case: A DELETE without a WHERE clause deletes everything.
drhc977f7f2002-05-21 11:38:11 +0000190 ** It is easier just to erase the whole table. Note, however, that
191 ** this means that the row change count will be incorrect.
drhcce7d172000-05-31 15:34:51 +0000192 */
danielk1977f29ce552002-05-19 23:43:12 +0000193 if( pWhere==0 && !row_triggers_exist ){
drh1bee3d72001-10-15 00:44:35 +0000194 if( db->flags & SQLITE_CountRows ){
195 /* If counting rows deleted, just count the total number of
196 ** entries in the table. */
danielk19774adee202004-05-08 08:23:19 +0000197 int endOfLoop = sqlite3VdbeMakeLabel(v);
drh1bee3d72001-10-15 00:44:35 +0000198 int addr;
drh5cf590c2003-04-24 01:45:04 +0000199 if( !isView ){
drhad6d9462004-09-19 02:15:24 +0000200 sqlite3OpenTableForReading(v, iCur, pTab);
drh5cf590c2003-04-24 01:45:04 +0000201 }
danielk19774adee202004-05-08 08:23:19 +0000202 sqlite3VdbeAddOp(v, OP_Rewind, iCur, sqlite3VdbeCurrentAddr(v)+2);
203 addr = sqlite3VdbeAddOp(v, OP_AddImm, 1, 0);
204 sqlite3VdbeAddOp(v, OP_Next, iCur, addr);
205 sqlite3VdbeResolveLabel(v, endOfLoop);
206 sqlite3VdbeAddOp(v, OP_Close, iCur, 0);
drh1bee3d72001-10-15 00:44:35 +0000207 }
drh5cf590c2003-04-24 01:45:04 +0000208 if( !isView ){
danielk19774adee202004-05-08 08:23:19 +0000209 sqlite3VdbeAddOp(v, OP_Clear, pTab->tnum, pTab->iDb);
drh5cf590c2003-04-24 01:45:04 +0000210 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
danielk19774adee202004-05-08 08:23:19 +0000211 sqlite3VdbeAddOp(v, OP_Clear, pIdx->tnum, pIdx->iDb);
drh5cf590c2003-04-24 01:45:04 +0000212 }
drhcce7d172000-05-31 15:34:51 +0000213 }
214 }
drh0353ced2001-03-20 22:05:00 +0000215
216 /* The usual case: There is a WHERE clause so we have to scan through
jplyon8bc03a72004-01-19 04:54:28 +0000217 ** the table and pick which records to delete.
drh0353ced2001-03-20 22:05:00 +0000218 */
219 else{
danielk19777cedc8d2004-06-10 10:50:08 +0000220 /* Ensure all required collation sequences are available. */
221 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
222 if( sqlite3CheckIndexCollSeq(pParse, pIdx) ){
223 goto delete_from_cleanup;
224 }
225 }
226
drh0353ced2001-03-20 22:05:00 +0000227 /* Begin the database scan
228 */
danielk19774adee202004-05-08 08:23:19 +0000229 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 1, 0);
drh0353ced2001-03-20 22:05:00 +0000230 if( pWInfo==0 ) goto delete_from_cleanup;
231
232 /* Remember the key of every item to be deleted.
233 */
danielk19774adee202004-05-08 08:23:19 +0000234 sqlite3VdbeAddOp(v, OP_ListWrite, 0, 0);
drh1bee3d72001-10-15 00:44:35 +0000235 if( db->flags & SQLITE_CountRows ){
danielk19774adee202004-05-08 08:23:19 +0000236 sqlite3VdbeAddOp(v, OP_AddImm, 1, 0);
drh1bee3d72001-10-15 00:44:35 +0000237 }
drh0353ced2001-03-20 22:05:00 +0000238
239 /* End the database scan loop.
240 */
danielk19774adee202004-05-08 08:23:19 +0000241 sqlite3WhereEnd(pWInfo);
drh0353ced2001-03-20 22:05:00 +0000242
drh70ce3f02003-04-15 19:22:22 +0000243 /* Open the pseudo-table used to store OLD if there are triggers.
244 */
245 if( row_triggers_exist ){
danielk19774adee202004-05-08 08:23:19 +0000246 sqlite3VdbeAddOp(v, OP_OpenPseudo, oldIdx, 0);
danielk197784ac9d02004-05-18 09:58:06 +0000247 sqlite3VdbeAddOp(v, OP_SetNumColumns, oldIdx, pTab->nCol);
drh70ce3f02003-04-15 19:22:22 +0000248 }
249
drh0353ced2001-03-20 22:05:00 +0000250 /* Delete every item whose key was written to the list during the
251 ** database scan. We have to delete items after the scan is complete
252 ** because deleting an item can change the scan order.
253 */
danielk19774adee202004-05-08 08:23:19 +0000254 sqlite3VdbeAddOp(v, OP_ListRewind, 0, 0);
255 end = sqlite3VdbeMakeLabel(v);
danielk1977c3f9bad2002-05-15 08:30:12 +0000256
drhc977f7f2002-05-21 11:38:11 +0000257 /* This is the beginning of the delete loop when there are
258 ** row triggers.
259 */
danielk1977f29ce552002-05-19 23:43:12 +0000260 if( row_triggers_exist ){
danielk19774adee202004-05-08 08:23:19 +0000261 addr = sqlite3VdbeAddOp(v, OP_ListRead, 0, end);
262 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drh5cf590c2003-04-24 01:45:04 +0000263 if( !isView ){
drhad6d9462004-09-19 02:15:24 +0000264 sqlite3OpenTableForReading(v, iCur, pTab);
drh5cf590c2003-04-24 01:45:04 +0000265 }
drh7cf6e4d2004-05-19 14:56:55 +0000266 sqlite3VdbeAddOp(v, OP_MoveGe, iCur, 0);
danielk19774adee202004-05-08 08:23:19 +0000267 sqlite3VdbeAddOp(v, OP_Recno, iCur, 0);
268 sqlite3VdbeAddOp(v, OP_RowData, iCur, 0);
269 sqlite3VdbeAddOp(v, OP_PutIntKey, oldIdx, 0);
drh5cf590c2003-04-24 01:45:04 +0000270 if( !isView ){
danielk19774adee202004-05-08 08:23:19 +0000271 sqlite3VdbeAddOp(v, OP_Close, iCur, 0);
drh5cf590c2003-04-24 01:45:04 +0000272 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000273
danielk19774adee202004-05-08 08:23:19 +0000274 sqlite3CodeRowTrigger(pParse, TK_DELETE, 0, TK_BEFORE, pTab, -1,
danielk19776f349032002-06-11 02:25:40 +0000275 oldIdx, (pParse->trigStack)?pParse->trigStack->orconf:OE_Default,
276 addr);
danielk1977c3f9bad2002-05-15 08:30:12 +0000277 }
278
drh5cf590c2003-04-24 01:45:04 +0000279 if( !isView ){
280 /* Open cursors for the table we are deleting from and all its
281 ** indices. If there are row triggers, this happens inside the
282 ** OP_ListRead loop because the cursor have to all be closed
283 ** before the trigger fires. If there are no row triggers, the
284 ** cursors are opened only once on the outside the loop.
285 */
drh290c1942004-08-21 17:54:45 +0000286 sqlite3OpenTableAndIndices(pParse, pTab, iCur, OP_OpenWrite);
danielk1977c3f9bad2002-05-15 08:30:12 +0000287
drh5cf590c2003-04-24 01:45:04 +0000288 /* This is the beginning of the delete loop when there are no
289 ** row triggers */
290 if( !row_triggers_exist ){
danielk19774adee202004-05-08 08:23:19 +0000291 addr = sqlite3VdbeAddOp(v, OP_ListRead, 0, end);
drh5cf590c2003-04-24 01:45:04 +0000292 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000293
drh5cf590c2003-04-24 01:45:04 +0000294 /* Delete the row */
danielk1977b28af712004-06-21 06:50:26 +0000295 sqlite3GenerateRowDelete(db, v, pTab, iCur, 1);
drh5cf590c2003-04-24 01:45:04 +0000296 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000297
drhc977f7f2002-05-21 11:38:11 +0000298 /* If there are row triggers, close all cursors then invoke
299 ** the AFTER triggers
300 */
danielk1977f29ce552002-05-19 23:43:12 +0000301 if( row_triggers_exist ){
drh5cf590c2003-04-24 01:45:04 +0000302 if( !isView ){
303 for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
danielk19774adee202004-05-08 08:23:19 +0000304 sqlite3VdbeAddOp(v, OP_Close, iCur + i, pIdx->tnum);
drh5cf590c2003-04-24 01:45:04 +0000305 }
danielk19774adee202004-05-08 08:23:19 +0000306 sqlite3VdbeAddOp(v, OP_Close, iCur, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000307 }
danielk19774adee202004-05-08 08:23:19 +0000308 sqlite3CodeRowTrigger(pParse, TK_DELETE, 0, TK_AFTER, pTab, -1,
danielk19776f349032002-06-11 02:25:40 +0000309 oldIdx, (pParse->trigStack)?pParse->trigStack->orconf:OE_Default,
310 addr);
danielk1977c3f9bad2002-05-15 08:30:12 +0000311 }
312
drhc977f7f2002-05-21 11:38:11 +0000313 /* End of the delete loop */
danielk19774adee202004-05-08 08:23:19 +0000314 sqlite3VdbeAddOp(v, OP_Goto, 0, addr);
315 sqlite3VdbeResolveLabel(v, end);
316 sqlite3VdbeAddOp(v, OP_ListReset, 0, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000317
drhc977f7f2002-05-21 11:38:11 +0000318 /* Close the cursors after the loop if there are no row triggers */
danielk1977f29ce552002-05-19 23:43:12 +0000319 if( !row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000320 for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
danielk19774adee202004-05-08 08:23:19 +0000321 sqlite3VdbeAddOp(v, OP_Close, iCur + i, pIdx->tnum);
danielk1977c3f9bad2002-05-15 08:30:12 +0000322 }
danielk19774adee202004-05-08 08:23:19 +0000323 sqlite3VdbeAddOp(v, OP_Close, iCur, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000324 }
drh0353ced2001-03-20 22:05:00 +0000325 }
drh5e00f6c2001-09-13 13:46:56 +0000326
drh1bee3d72001-10-15 00:44:35 +0000327 /*
328 ** Return the number of rows that were deleted.
329 */
330 if( db->flags & SQLITE_CountRows ){
danielk19774adee202004-05-08 08:23:19 +0000331 sqlite3VdbeAddOp(v, OP_Callback, 1, 0);
danielk197722322fd2004-05-25 23:35:17 +0000332 sqlite3VdbeSetNumCols(v, 1);
danielk19773cf86062004-05-26 10:11:05 +0000333 sqlite3VdbeSetColName(v, 0, "rows deleted", P3_STATIC);
drh1bee3d72001-10-15 00:44:35 +0000334 }
drhcce7d172000-05-31 15:34:51 +0000335
336delete_from_cleanup:
danielk19774adee202004-05-08 08:23:19 +0000337 sqlite3AuthContextPop(&sContext);
338 sqlite3SrcListDelete(pTabList);
339 sqlite3ExprDelete(pWhere);
drhcce7d172000-05-31 15:34:51 +0000340 return;
341}
drh9cfcf5d2002-01-29 18:41:24 +0000342
343/*
344** This routine generates VDBE code that causes a single row of a
345** single table to be deleted.
346**
347** The VDBE must be in a particular state when this routine is called.
348** These are the requirements:
349**
350** 1. A read/write cursor pointing to pTab, the table containing the row
351** to be deleted, must be opened as cursor number "base".
352**
353** 2. Read/write cursors for all indices of pTab must be open as
354** cursor number base+i for the i-th index.
355**
356** 3. The record number of the row to be deleted must be on the top
357** of the stack.
358**
359** This routine pops the top of the stack to remove the record number
360** and then generates code to remove both the table record and all index
361** entries that point to that record.
362*/
danielk19774adee202004-05-08 08:23:19 +0000363void sqlite3GenerateRowDelete(
drh9bb575f2004-09-06 17:24:11 +0000364 sqlite3 *db, /* The database containing the index */
drh9cfcf5d2002-01-29 18:41:24 +0000365 Vdbe *v, /* Generate code into this VDBE */
366 Table *pTab, /* Table containing the row to be deleted */
drh6a3ea0e2003-05-02 14:32:12 +0000367 int iCur, /* Cursor number for the table */
drhc8d30ac2002-04-12 10:08:59 +0000368 int count /* Increment the row change counter */
drh9cfcf5d2002-01-29 18:41:24 +0000369){
drh07d6e3a2002-05-23 12:50:18 +0000370 int addr;
danielk19774adee202004-05-08 08:23:19 +0000371 addr = sqlite3VdbeAddOp(v, OP_NotExists, iCur, 0);
372 sqlite3GenerateRowIndexDelete(db, v, pTab, iCur, 0);
danielk1977b28af712004-06-21 06:50:26 +0000373 sqlite3VdbeAddOp(v, OP_Delete, iCur, (count?OPFLAG_NCHANGE:0));
danielk19774adee202004-05-08 08:23:19 +0000374 sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v));
drh0ca3e242002-01-29 23:07:02 +0000375}
376
377/*
378** This routine generates VDBE code that causes the deletion of all
379** index entries associated with a single row of a single table.
380**
381** The VDBE must be in a particular state when this routine is called.
382** These are the requirements:
383**
384** 1. A read/write cursor pointing to pTab, the table containing the row
drh6a3ea0e2003-05-02 14:32:12 +0000385** to be deleted, must be opened as cursor number "iCur".
drh0ca3e242002-01-29 23:07:02 +0000386**
387** 2. Read/write cursors for all indices of pTab must be open as
drh6a3ea0e2003-05-02 14:32:12 +0000388** cursor number iCur+i for the i-th index.
drh0ca3e242002-01-29 23:07:02 +0000389**
drh6a3ea0e2003-05-02 14:32:12 +0000390** 3. The "iCur" cursor must be pointing to the row that is to be
drh0ca3e242002-01-29 23:07:02 +0000391** deleted.
392*/
danielk19774adee202004-05-08 08:23:19 +0000393void sqlite3GenerateRowIndexDelete(
drh9bb575f2004-09-06 17:24:11 +0000394 sqlite3 *db, /* The database containing the index */
drh0ca3e242002-01-29 23:07:02 +0000395 Vdbe *v, /* Generate code into this VDBE */
396 Table *pTab, /* Table containing the row to be deleted */
drh6a3ea0e2003-05-02 14:32:12 +0000397 int iCur, /* Cursor number for the table */
drh0ca3e242002-01-29 23:07:02 +0000398 char *aIdxUsed /* Only delete if aIdxUsed!=0 && aIdxUsed[i]!=0 */
399){
drh9cfcf5d2002-01-29 18:41:24 +0000400 int i;
401 Index *pIdx;
402
drh0ca3e242002-01-29 23:07:02 +0000403 for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
drh0ca3e242002-01-29 23:07:02 +0000404 if( aIdxUsed!=0 && aIdxUsed[i-1]==0 ) continue;
drh51846b52004-05-28 16:00:21 +0000405 sqlite3GenerateIndexKey(v, pIdx, iCur);
danielk19774adee202004-05-08 08:23:19 +0000406 sqlite3VdbeAddOp(v, OP_IdxDelete, iCur+i, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000407 }
drh9cfcf5d2002-01-29 18:41:24 +0000408}
drh51846b52004-05-28 16:00:21 +0000409
410/*
411** Generate code that will assemble an index key and put it on the top
412** of the tack. The key with be for index pIdx which is an index on pTab.
413** iCur is the index of a cursor open on the pTab table and pointing to
414** the entry that needs indexing.
415*/
416void sqlite3GenerateIndexKey(
417 Vdbe *v, /* Generate code into this VDBE */
418 Index *pIdx, /* The index for which to generate a key */
419 int iCur /* Cursor number for the pIdx->pTable table */
420){
421 int j;
422 Table *pTab = pIdx->pTable;
423
424 sqlite3VdbeAddOp(v, OP_Recno, iCur, 0);
425 for(j=0; j<pIdx->nColumn; j++){
426 int idx = pIdx->aiColumn[j];
427 if( idx==pTab->iPKey ){
428 sqlite3VdbeAddOp(v, OP_Dup, j, 0);
429 }else{
430 sqlite3VdbeAddOp(v, OP_Column, iCur, idx);
431 }
432 }
danielk1977ededfd52004-06-17 07:53:01 +0000433 sqlite3VdbeAddOp(v, OP_MakeRecord, pIdx->nColumn, (1<<24));
drh51846b52004-05-28 16:00:21 +0000434 sqlite3IndexAffinityStr(v, pIdx);
435}