drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2 | ** 2001 September 15 |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 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. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
| 12 | ** This file contains C code routines that are called by the parser |
| 13 | ** to handle DELETE FROM statements. |
| 14 | ** |
drh | ad6d946 | 2004-09-19 02:15:24 +0000 | [diff] [blame^] | 15 | ** $Id: delete.c,v 1.81 2004/09/19 02:15:25 drh Exp $ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 16 | */ |
| 17 | #include "sqliteInt.h" |
| 18 | |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 19 | /* |
drh | 812d7a2 | 2003-03-27 13:50:00 +0000 | [diff] [blame] | 20 | ** 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. |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 23 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 24 | Table *sqlite3SrcListLookup(Parse *pParse, SrcList *pSrc){ |
drh | 812d7a2 | 2003-03-27 13:50:00 +0000 | [diff] [blame] | 25 | Table *pTab = 0; |
| 26 | int i; |
drh | 855eb1c | 2004-08-31 13:45:11 +0000 | [diff] [blame] | 27 | 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; |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 31 | } |
| 32 | return pTab; |
| 33 | } |
| 34 | |
| 35 | /* |
drh | 812d7a2 | 2003-03-27 13:50:00 +0000 | [diff] [blame] | 36 | ** 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 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 40 | int sqlite3IsReadOnly(Parse *pParse, Table *pTab, int viewOk){ |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 41 | if( pTab->readOnly ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 42 | sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName); |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 43 | return 1; |
| 44 | } |
| 45 | if( !viewOk && pTab->pSelect ){ |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame] | 46 | sqlite3ErrorMsg(pParse,"cannot modify %s because it is a view",pTab->zName); |
drh | 812d7a2 | 2003-03-27 13:50:00 +0000 | [diff] [blame] | 47 | return 1; |
| 48 | } |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | /* |
drh | ad6d946 | 2004-09-19 02:15:24 +0000 | [diff] [blame^] | 53 | ** Generate code that will open a table for reading. |
| 54 | */ |
| 55 | void 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 | /* |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 68 | ** Process a DELETE FROM statement. |
| 69 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 70 | void sqlite3DeleteFrom( |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 71 | Parse *pParse, /* The parser context */ |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 72 | SrcList *pTabList, /* The table from which we should delete things */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 73 | 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 */ |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 77 | const char *zDb; /* Name of database holding pTab */ |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 78 | int end, addr = 0; /* A couple addresses of generated code */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 79 | int i; /* Loop counter */ |
| 80 | WhereInfo *pWInfo; /* Information about the WHERE clause */ |
| 81 | Index *pIdx; /* For looping over indices of the table */ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 82 | int iCur; /* VDBE Cursor number for pTab */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 83 | sqlite3 *db; /* Main database structure */ |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 84 | int isView; /* True if attempting to delete from a view */ |
drh | 85e2096 | 2003-04-25 17:52:11 +0000 | [diff] [blame] | 85 | AuthContext sContext; /* Authorization context */ |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 86 | |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 87 | 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 */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 91 | |
drh | 85e2096 | 2003-04-25 17:52:11 +0000 | [diff] [blame] | 92 | sContext.pParse = 0; |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 93 | if( pParse->nErr || sqlite3_malloc_failed ){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 94 | pTabList = 0; |
| 95 | goto delete_from_cleanup; |
| 96 | } |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 97 | db = pParse->db; |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 98 | assert( pTabList->nSrc==1 ); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 99 | |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 100 | /* Locate the table which we want to delete. This table has to be |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 101 | ** put in an SrcList structure because some of the subroutines we |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 102 | ** will be calling are designed to work with multiple tables and expect |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 103 | ** an SrcList* parameter instead of just a Table* parameter. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 104 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 105 | pTab = sqlite3SrcListLookup(pParse, pTabList); |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 106 | if( pTab==0 ) goto delete_from_cleanup; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 107 | before_triggers = sqlite3TriggersExist(pParse, pTab->pTrigger, |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 108 | TK_DELETE, TK_BEFORE, TK_ROW, 0); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 109 | after_triggers = sqlite3TriggersExist(pParse, pTab->pTrigger, |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 110 | TK_DELETE, TK_AFTER, TK_ROW, 0); |
| 111 | row_triggers_exist = before_triggers || after_triggers; |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 112 | isView = pTab->pSelect!=0; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 113 | if( sqlite3IsReadOnly(pParse, pTab, before_triggers) ){ |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 114 | goto delete_from_cleanup; |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 115 | } |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 116 | assert( pTab->iDb<db->nDb ); |
| 117 | zDb = db->aDb[pTab->iDb].zName; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 118 | if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){ |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 119 | goto delete_from_cleanup; |
| 120 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 121 | |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 122 | /* If pTab is really a view, make sure it has been initialized. |
| 123 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 124 | if( isView && sqlite3ViewGetColumnNames(pParse, pTab) ){ |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 125 | goto delete_from_cleanup; |
| 126 | } |
| 127 | |
drh | c977f7f | 2002-05-21 11:38:11 +0000 | [diff] [blame] | 128 | /* Allocate a cursor used to store the old.* data for a trigger. |
| 129 | */ |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 130 | if( row_triggers_exist ){ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 131 | oldIdx = pParse->nTab++; |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 132 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 133 | |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 134 | /* Resolve the column names in all the expressions. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 135 | */ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 136 | assert( pTabList->nSrc==1 ); |
| 137 | iCur = pTabList->a[0].iCursor = pParse->nTab++; |
drh | 290c194 | 2004-08-21 17:54:45 +0000 | [diff] [blame] | 138 | if( sqlite3ExprResolveAndCheck(pParse, pTabList, 0, pWhere, 0, 0) ){ |
| 139 | goto delete_from_cleanup; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 140 | } |
| 141 | |
drh | 85e2096 | 2003-04-25 17:52:11 +0000 | [diff] [blame] | 142 | /* Start the view context |
| 143 | */ |
| 144 | if( isView ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 145 | sqlite3AuthContextPush(pParse, &sContext, pTab->zName); |
drh | 85e2096 | 2003-04-25 17:52:11 +0000 | [diff] [blame] | 146 | } |
| 147 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 148 | /* Begin generating code. |
| 149 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 150 | v = sqlite3GetVdbe(pParse); |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 151 | if( v==0 ){ |
| 152 | goto delete_from_cleanup; |
| 153 | } |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 154 | sqlite3VdbeCountChanges(v); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 155 | sqlite3BeginWriteOperation(pParse, row_triggers_exist, pTab->iDb); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 156 | |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 157 | /* If we are trying to delete from a view, construct that view into |
| 158 | ** a temporary table. |
| 159 | */ |
| 160 | if( isView ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 161 | Select *pView = sqlite3SelectDup(pTab->pSelect); |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 162 | sqlite3Select(pParse, pView, SRT_TempTable, iCur, 0, 0, 0, 0); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 163 | sqlite3SelectDelete(pView); |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 164 | } |
| 165 | |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 166 | /* Initialize the counter of the number of rows deleted, if |
| 167 | ** we are counting rows. |
| 168 | */ |
| 169 | if( db->flags & SQLITE_CountRows ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 170 | sqlite3VdbeAddOp(v, OP_Integer, 0, 0); |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 171 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 172 | |
drh | 0353ced | 2001-03-20 22:05:00 +0000 | [diff] [blame] | 173 | /* Special case: A DELETE without a WHERE clause deletes everything. |
drh | c977f7f | 2002-05-21 11:38:11 +0000 | [diff] [blame] | 174 | ** It is easier just to erase the whole table. Note, however, that |
| 175 | ** this means that the row change count will be incorrect. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 176 | */ |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 177 | if( pWhere==0 && !row_triggers_exist ){ |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 178 | if( db->flags & SQLITE_CountRows ){ |
| 179 | /* If counting rows deleted, just count the total number of |
| 180 | ** entries in the table. */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 181 | int endOfLoop = sqlite3VdbeMakeLabel(v); |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 182 | int addr; |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 183 | if( !isView ){ |
drh | ad6d946 | 2004-09-19 02:15:24 +0000 | [diff] [blame^] | 184 | sqlite3OpenTableForReading(v, iCur, pTab); |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 185 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 186 | sqlite3VdbeAddOp(v, OP_Rewind, iCur, sqlite3VdbeCurrentAddr(v)+2); |
| 187 | addr = sqlite3VdbeAddOp(v, OP_AddImm, 1, 0); |
| 188 | sqlite3VdbeAddOp(v, OP_Next, iCur, addr); |
| 189 | sqlite3VdbeResolveLabel(v, endOfLoop); |
| 190 | sqlite3VdbeAddOp(v, OP_Close, iCur, 0); |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 191 | } |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 192 | if( !isView ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 193 | sqlite3VdbeAddOp(v, OP_Clear, pTab->tnum, pTab->iDb); |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 194 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 195 | sqlite3VdbeAddOp(v, OP_Clear, pIdx->tnum, pIdx->iDb); |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 196 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 197 | } |
| 198 | } |
drh | 0353ced | 2001-03-20 22:05:00 +0000 | [diff] [blame] | 199 | |
| 200 | /* The usual case: There is a WHERE clause so we have to scan through |
jplyon | 8bc03a7 | 2004-01-19 04:54:28 +0000 | [diff] [blame] | 201 | ** the table and pick which records to delete. |
drh | 0353ced | 2001-03-20 22:05:00 +0000 | [diff] [blame] | 202 | */ |
| 203 | else{ |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 204 | /* Ensure all required collation sequences are available. */ |
| 205 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 206 | if( sqlite3CheckIndexCollSeq(pParse, pIdx) ){ |
| 207 | goto delete_from_cleanup; |
| 208 | } |
| 209 | } |
| 210 | |
drh | 0353ced | 2001-03-20 22:05:00 +0000 | [diff] [blame] | 211 | /* Begin the database scan |
| 212 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 213 | pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 1, 0); |
drh | 0353ced | 2001-03-20 22:05:00 +0000 | [diff] [blame] | 214 | if( pWInfo==0 ) goto delete_from_cleanup; |
| 215 | |
| 216 | /* Remember the key of every item to be deleted. |
| 217 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 218 | sqlite3VdbeAddOp(v, OP_ListWrite, 0, 0); |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 219 | if( db->flags & SQLITE_CountRows ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 220 | sqlite3VdbeAddOp(v, OP_AddImm, 1, 0); |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 221 | } |
drh | 0353ced | 2001-03-20 22:05:00 +0000 | [diff] [blame] | 222 | |
| 223 | /* End the database scan loop. |
| 224 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 225 | sqlite3WhereEnd(pWInfo); |
drh | 0353ced | 2001-03-20 22:05:00 +0000 | [diff] [blame] | 226 | |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 227 | /* Open the pseudo-table used to store OLD if there are triggers. |
| 228 | */ |
| 229 | if( row_triggers_exist ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 230 | sqlite3VdbeAddOp(v, OP_OpenPseudo, oldIdx, 0); |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 231 | sqlite3VdbeAddOp(v, OP_SetNumColumns, oldIdx, pTab->nCol); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 232 | } |
| 233 | |
drh | 0353ced | 2001-03-20 22:05:00 +0000 | [diff] [blame] | 234 | /* Delete every item whose key was written to the list during the |
| 235 | ** database scan. We have to delete items after the scan is complete |
| 236 | ** because deleting an item can change the scan order. |
| 237 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 238 | sqlite3VdbeAddOp(v, OP_ListRewind, 0, 0); |
| 239 | end = sqlite3VdbeMakeLabel(v); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 240 | |
drh | c977f7f | 2002-05-21 11:38:11 +0000 | [diff] [blame] | 241 | /* This is the beginning of the delete loop when there are |
| 242 | ** row triggers. |
| 243 | */ |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 244 | if( row_triggers_exist ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 245 | addr = sqlite3VdbeAddOp(v, OP_ListRead, 0, end); |
| 246 | sqlite3VdbeAddOp(v, OP_Dup, 0, 0); |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 247 | if( !isView ){ |
drh | ad6d946 | 2004-09-19 02:15:24 +0000 | [diff] [blame^] | 248 | sqlite3OpenTableForReading(v, iCur, pTab); |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 249 | } |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 250 | sqlite3VdbeAddOp(v, OP_MoveGe, iCur, 0); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 251 | sqlite3VdbeAddOp(v, OP_Recno, iCur, 0); |
| 252 | sqlite3VdbeAddOp(v, OP_RowData, iCur, 0); |
| 253 | sqlite3VdbeAddOp(v, OP_PutIntKey, oldIdx, 0); |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 254 | if( !isView ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 255 | sqlite3VdbeAddOp(v, OP_Close, iCur, 0); |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 256 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 257 | |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 258 | sqlite3CodeRowTrigger(pParse, TK_DELETE, 0, TK_BEFORE, pTab, -1, |
danielk1977 | 6f34903 | 2002-06-11 02:25:40 +0000 | [diff] [blame] | 259 | oldIdx, (pParse->trigStack)?pParse->trigStack->orconf:OE_Default, |
| 260 | addr); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 261 | } |
| 262 | |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 263 | if( !isView ){ |
| 264 | /* Open cursors for the table we are deleting from and all its |
| 265 | ** indices. If there are row triggers, this happens inside the |
| 266 | ** OP_ListRead loop because the cursor have to all be closed |
| 267 | ** before the trigger fires. If there are no row triggers, the |
| 268 | ** cursors are opened only once on the outside the loop. |
| 269 | */ |
drh | 290c194 | 2004-08-21 17:54:45 +0000 | [diff] [blame] | 270 | sqlite3OpenTableAndIndices(pParse, pTab, iCur, OP_OpenWrite); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 271 | |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 272 | /* This is the beginning of the delete loop when there are no |
| 273 | ** row triggers */ |
| 274 | if( !row_triggers_exist ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 275 | addr = sqlite3VdbeAddOp(v, OP_ListRead, 0, end); |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 276 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 277 | |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 278 | /* Delete the row */ |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 279 | sqlite3GenerateRowDelete(db, v, pTab, iCur, 1); |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 280 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 281 | |
drh | c977f7f | 2002-05-21 11:38:11 +0000 | [diff] [blame] | 282 | /* If there are row triggers, close all cursors then invoke |
| 283 | ** the AFTER triggers |
| 284 | */ |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 285 | if( row_triggers_exist ){ |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 286 | if( !isView ){ |
| 287 | for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 288 | sqlite3VdbeAddOp(v, OP_Close, iCur + i, pIdx->tnum); |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 289 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 290 | sqlite3VdbeAddOp(v, OP_Close, iCur, 0); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 291 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 292 | sqlite3CodeRowTrigger(pParse, TK_DELETE, 0, TK_AFTER, pTab, -1, |
danielk1977 | 6f34903 | 2002-06-11 02:25:40 +0000 | [diff] [blame] | 293 | oldIdx, (pParse->trigStack)?pParse->trigStack->orconf:OE_Default, |
| 294 | addr); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 295 | } |
| 296 | |
drh | c977f7f | 2002-05-21 11:38:11 +0000 | [diff] [blame] | 297 | /* End of the delete loop */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 298 | sqlite3VdbeAddOp(v, OP_Goto, 0, addr); |
| 299 | sqlite3VdbeResolveLabel(v, end); |
| 300 | sqlite3VdbeAddOp(v, OP_ListReset, 0, 0); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 301 | |
drh | c977f7f | 2002-05-21 11:38:11 +0000 | [diff] [blame] | 302 | /* Close the cursors after the loop if there are no row triggers */ |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 303 | if( !row_triggers_exist ){ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 304 | for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 305 | sqlite3VdbeAddOp(v, OP_Close, iCur + i, pIdx->tnum); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 306 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 307 | sqlite3VdbeAddOp(v, OP_Close, iCur, 0); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 308 | } |
drh | 0353ced | 2001-03-20 22:05:00 +0000 | [diff] [blame] | 309 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 310 | sqlite3EndWriteOperation(pParse); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 311 | |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 312 | /* |
| 313 | ** Return the number of rows that were deleted. |
| 314 | */ |
| 315 | if( db->flags & SQLITE_CountRows ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 316 | sqlite3VdbeAddOp(v, OP_Callback, 1, 0); |
danielk1977 | 22322fd | 2004-05-25 23:35:17 +0000 | [diff] [blame] | 317 | sqlite3VdbeSetNumCols(v, 1); |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 318 | sqlite3VdbeSetColName(v, 0, "rows deleted", P3_STATIC); |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 319 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 320 | |
| 321 | delete_from_cleanup: |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 322 | sqlite3AuthContextPop(&sContext); |
| 323 | sqlite3SrcListDelete(pTabList); |
| 324 | sqlite3ExprDelete(pWhere); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 325 | return; |
| 326 | } |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 327 | |
| 328 | /* |
| 329 | ** This routine generates VDBE code that causes a single row of a |
| 330 | ** single table to be deleted. |
| 331 | ** |
| 332 | ** The VDBE must be in a particular state when this routine is called. |
| 333 | ** These are the requirements: |
| 334 | ** |
| 335 | ** 1. A read/write cursor pointing to pTab, the table containing the row |
| 336 | ** to be deleted, must be opened as cursor number "base". |
| 337 | ** |
| 338 | ** 2. Read/write cursors for all indices of pTab must be open as |
| 339 | ** cursor number base+i for the i-th index. |
| 340 | ** |
| 341 | ** 3. The record number of the row to be deleted must be on the top |
| 342 | ** of the stack. |
| 343 | ** |
| 344 | ** This routine pops the top of the stack to remove the record number |
| 345 | ** and then generates code to remove both the table record and all index |
| 346 | ** entries that point to that record. |
| 347 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 348 | void sqlite3GenerateRowDelete( |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 349 | sqlite3 *db, /* The database containing the index */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 350 | Vdbe *v, /* Generate code into this VDBE */ |
| 351 | Table *pTab, /* Table containing the row to be deleted */ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 352 | int iCur, /* Cursor number for the table */ |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 353 | int count /* Increment the row change counter */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 354 | ){ |
drh | 07d6e3a | 2002-05-23 12:50:18 +0000 | [diff] [blame] | 355 | int addr; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 356 | addr = sqlite3VdbeAddOp(v, OP_NotExists, iCur, 0); |
| 357 | sqlite3GenerateRowIndexDelete(db, v, pTab, iCur, 0); |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 358 | sqlite3VdbeAddOp(v, OP_Delete, iCur, (count?OPFLAG_NCHANGE:0)); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 359 | sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v)); |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | /* |
| 363 | ** This routine generates VDBE code that causes the deletion of all |
| 364 | ** index entries associated with a single row of a single table. |
| 365 | ** |
| 366 | ** The VDBE must be in a particular state when this routine is called. |
| 367 | ** These are the requirements: |
| 368 | ** |
| 369 | ** 1. A read/write cursor pointing to pTab, the table containing the row |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 370 | ** to be deleted, must be opened as cursor number "iCur". |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 371 | ** |
| 372 | ** 2. Read/write cursors for all indices of pTab must be open as |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 373 | ** cursor number iCur+i for the i-th index. |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 374 | ** |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 375 | ** 3. The "iCur" cursor must be pointing to the row that is to be |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 376 | ** deleted. |
| 377 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 378 | void sqlite3GenerateRowIndexDelete( |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 379 | sqlite3 *db, /* The database containing the index */ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 380 | Vdbe *v, /* Generate code into this VDBE */ |
| 381 | Table *pTab, /* Table containing the row to be deleted */ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 382 | int iCur, /* Cursor number for the table */ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 383 | char *aIdxUsed /* Only delete if aIdxUsed!=0 && aIdxUsed[i]!=0 */ |
| 384 | ){ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 385 | int i; |
| 386 | Index *pIdx; |
| 387 | |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 388 | for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 389 | if( aIdxUsed!=0 && aIdxUsed[i-1]==0 ) continue; |
drh | 51846b5 | 2004-05-28 16:00:21 +0000 | [diff] [blame] | 390 | sqlite3GenerateIndexKey(v, pIdx, iCur); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 391 | sqlite3VdbeAddOp(v, OP_IdxDelete, iCur+i, 0); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 392 | } |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 393 | } |
drh | 51846b5 | 2004-05-28 16:00:21 +0000 | [diff] [blame] | 394 | |
| 395 | /* |
| 396 | ** Generate code that will assemble an index key and put it on the top |
| 397 | ** of the tack. The key with be for index pIdx which is an index on pTab. |
| 398 | ** iCur is the index of a cursor open on the pTab table and pointing to |
| 399 | ** the entry that needs indexing. |
| 400 | */ |
| 401 | void sqlite3GenerateIndexKey( |
| 402 | Vdbe *v, /* Generate code into this VDBE */ |
| 403 | Index *pIdx, /* The index for which to generate a key */ |
| 404 | int iCur /* Cursor number for the pIdx->pTable table */ |
| 405 | ){ |
| 406 | int j; |
| 407 | Table *pTab = pIdx->pTable; |
| 408 | |
| 409 | sqlite3VdbeAddOp(v, OP_Recno, iCur, 0); |
| 410 | for(j=0; j<pIdx->nColumn; j++){ |
| 411 | int idx = pIdx->aiColumn[j]; |
| 412 | if( idx==pTab->iPKey ){ |
| 413 | sqlite3VdbeAddOp(v, OP_Dup, j, 0); |
| 414 | }else{ |
| 415 | sqlite3VdbeAddOp(v, OP_Column, iCur, idx); |
| 416 | } |
| 417 | } |
danielk1977 | ededfd5 | 2004-06-17 07:53:01 +0000 | [diff] [blame] | 418 | sqlite3VdbeAddOp(v, OP_MakeRecord, pIdx->nColumn, (1<<24)); |
drh | 51846b5 | 2004-05-28 16:00:21 +0000 | [diff] [blame] | 419 | sqlite3IndexAffinityStr(v, pIdx); |
| 420 | } |