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 | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame^] | 15 | ** $Id: delete.c,v 1.36 2002/05/24 02:04:33 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 | |
| 20 | /* |
| 21 | ** Given a table name, find the corresponding table and make sure the |
| 22 | ** table is writeable. Generate an error and return NULL if not. If |
| 23 | ** everything checks out, return a pointer to the Table structure. |
| 24 | */ |
| 25 | Table *sqliteTableNameToTable(Parse *pParse, const char *zTab){ |
| 26 | Table *pTab; |
| 27 | pTab = sqliteFindTable(pParse->db, zTab); |
| 28 | if( pTab==0 ){ |
| 29 | sqliteSetString(&pParse->zErrMsg, "no such table: ", zTab, 0); |
| 30 | pParse->nErr++; |
| 31 | return 0; |
| 32 | } |
| 33 | if( pTab->readOnly || pTab->pSelect ){ |
| 34 | sqliteSetString(&pParse->zErrMsg, |
| 35 | pTab->pSelect ? "view " : "table ", |
| 36 | zTab, |
| 37 | " may not be modified", 0); |
| 38 | pParse->nErr++; |
| 39 | return 0; |
| 40 | } |
| 41 | return pTab; |
| 42 | } |
| 43 | |
| 44 | /* |
| 45 | ** Given a table name, check to make sure the table exists, is writable |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame^] | 46 | ** and is not a view. If everything is OK, construct an SrcList holding |
| 47 | ** the table and return a pointer to the SrcList. The calling function |
| 48 | ** is responsible for freeing the SrcList when it has finished with it. |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 49 | ** If there is an error, leave a message on pParse->zErrMsg and return |
| 50 | ** NULL. |
| 51 | */ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame^] | 52 | SrcList *sqliteTableTokenToSrcList(Parse *pParse, Token *pTableName){ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 53 | Table *pTab; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame^] | 54 | SrcList *pTabList; |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 55 | |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame^] | 56 | pTabList = sqliteSrcListAppend(0, pTableName); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 57 | if( pTabList==0 ) return 0; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame^] | 58 | assert( pTabList->nSrc==1 ); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 59 | pTab = sqliteTableNameToTable(pParse, pTabList->a[0].zName); |
| 60 | if( pTab==0 ){ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame^] | 61 | sqliteSrcListDelete(pTabList); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 62 | return 0; |
| 63 | } |
| 64 | pTabList->a[0].pTab = pTab; |
| 65 | return pTabList; |
| 66 | } |
| 67 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 68 | /* |
| 69 | ** Process a DELETE FROM statement. |
| 70 | */ |
| 71 | void sqliteDeleteFrom( |
| 72 | Parse *pParse, /* The parser context */ |
| 73 | Token *pTableName, /* The table from which we should delete things */ |
| 74 | Expr *pWhere /* The WHERE clause. May be null */ |
| 75 | ){ |
| 76 | Vdbe *v; /* The virtual database engine */ |
| 77 | Table *pTab; /* The table from which records will be deleted */ |
drh | c977f7f | 2002-05-21 11:38:11 +0000 | [diff] [blame] | 78 | char *zTab; /* Name of the table from which we are deleting */ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame^] | 79 | SrcList *pTabList; /* A fake FROM clause holding just pTab */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 80 | int end, addr; /* A couple addresses of generated code */ |
| 81 | int i; /* Loop counter */ |
| 82 | WhereInfo *pWInfo; /* Information about the WHERE clause */ |
| 83 | Index *pIdx; /* For looping over indices of the table */ |
drh | 4794b98 | 2000-06-06 13:54:14 +0000 | [diff] [blame] | 84 | int base; /* Index of the first available table cursor */ |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 85 | sqlite *db; /* Main database structure */ |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 86 | int openOp; /* Opcode used to open a cursor to the table */ |
| 87 | |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 88 | int row_triggers_exist = 0; |
| 89 | int oldIdx = -1; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 90 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 91 | if( pParse->nErr || sqlite_malloc_failed ){ |
| 92 | pTabList = 0; |
| 93 | goto delete_from_cleanup; |
| 94 | } |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 95 | db = pParse->db; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 96 | |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 97 | /* Check for the special case of a VIEW with one or more ON DELETE triggers |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 98 | ** defined |
| 99 | */ |
drh | c977f7f | 2002-05-21 11:38:11 +0000 | [diff] [blame] | 100 | zTab = sqliteTableNameFromToken(pTableName); |
| 101 | if( zTab != 0 ){ |
| 102 | pTab = sqliteFindTable(pParse->db, zTab); |
| 103 | if( pTab ){ |
| 104 | row_triggers_exist = |
| 105 | sqliteTriggersExist(pParse, pTab->pTrigger, |
| 106 | TK_DELETE, TK_BEFORE, TK_ROW, 0) || |
| 107 | sqliteTriggersExist(pParse, pTab->pTrigger, |
| 108 | TK_DELETE, TK_AFTER, TK_ROW, 0); |
| 109 | } |
| 110 | sqliteFree(zTab); |
| 111 | if( row_triggers_exist && pTab->pSelect ){ |
| 112 | /* Just fire VIEW triggers */ |
| 113 | sqliteViewTriggers(pParse, pTab, pWhere, OE_Replace, 0); |
| 114 | return; |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 118 | /* Locate the table which we want to delete. This table has to be |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame^] | 119 | ** put in an SrcList structure because some of the subroutines we |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 120 | ** will be calling are designed to work with multiple tables and expect |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame^] | 121 | ** an SrcList* parameter instead of just a Table* parameter. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 122 | */ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame^] | 123 | pTabList = sqliteTableTokenToSrcList(pParse, pTableName); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 124 | if( pTabList==0 ) goto delete_from_cleanup; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame^] | 125 | assert( pTabList->nSrc==1 ); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 126 | pTab = pTabList->a[0].pTab; |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 127 | assert( pTab->pSelect==0 ); /* This table is not a view */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 128 | |
drh | c977f7f | 2002-05-21 11:38:11 +0000 | [diff] [blame] | 129 | /* Allocate a cursor used to store the old.* data for a trigger. |
| 130 | */ |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 131 | if( row_triggers_exist ){ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 132 | oldIdx = pParse->nTab++; |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 133 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 134 | |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 135 | /* Resolve the column names in all the expressions. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 136 | */ |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 137 | base = pParse->nTab++; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 138 | if( pWhere ){ |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 139 | if( sqliteExprResolveIds(pParse, base, pTabList, 0, pWhere) ){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 140 | goto delete_from_cleanup; |
| 141 | } |
| 142 | if( sqliteExprCheck(pParse, pWhere, 0, 0) ){ |
| 143 | goto delete_from_cleanup; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | /* Begin generating code. |
| 148 | */ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 149 | v = sqliteGetVdbe(pParse); |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 150 | if( v==0 ){ |
| 151 | goto delete_from_cleanup; |
| 152 | } |
drh | c977f7f | 2002-05-21 11:38:11 +0000 | [diff] [blame] | 153 | sqliteBeginWriteOperation(pParse, row_triggers_exist); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 154 | |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 155 | /* Initialize the counter of the number of rows deleted, if |
| 156 | ** we are counting rows. |
| 157 | */ |
| 158 | if( db->flags & SQLITE_CountRows ){ |
| 159 | sqliteVdbeAddOp(v, OP_Integer, 0, 0); |
| 160 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 161 | |
drh | 0353ced | 2001-03-20 22:05:00 +0000 | [diff] [blame] | 162 | /* Special case: A DELETE without a WHERE clause deletes everything. |
drh | c977f7f | 2002-05-21 11:38:11 +0000 | [diff] [blame] | 163 | ** It is easier just to erase the whole table. Note, however, that |
| 164 | ** this means that the row change count will be incorrect. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 165 | */ |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 166 | if( pWhere==0 && !row_triggers_exist ){ |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 167 | if( db->flags & SQLITE_CountRows ){ |
| 168 | /* If counting rows deleted, just count the total number of |
| 169 | ** entries in the table. */ |
| 170 | int endOfLoop = sqliteVdbeMakeLabel(v); |
| 171 | int addr; |
| 172 | openOp = pTab->isTemp ? OP_OpenAux : OP_Open; |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 173 | assert( base==0 ); |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 174 | sqliteVdbeAddOp(v, openOp, 0, pTab->tnum); |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 175 | sqliteVdbeAddOp(v, OP_Rewind, 0, sqliteVdbeCurrentAddr(v)+2); |
| 176 | addr = sqliteVdbeAddOp(v, OP_AddImm, 1, 0); |
| 177 | sqliteVdbeAddOp(v, OP_Next, 0, addr); |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 178 | sqliteVdbeResolveLabel(v, endOfLoop); |
| 179 | sqliteVdbeAddOp(v, OP_Close, 0, 0); |
| 180 | } |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 181 | sqliteVdbeAddOp(v, OP_Clear, pTab->tnum, pTab->isTemp); |
drh | 0353ced | 2001-03-20 22:05:00 +0000 | [diff] [blame] | 182 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 183 | sqliteVdbeAddOp(v, OP_Clear, pIdx->tnum, pTab->isTemp); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 184 | } |
| 185 | } |
drh | 0353ced | 2001-03-20 22:05:00 +0000 | [diff] [blame] | 186 | |
| 187 | /* The usual case: There is a WHERE clause so we have to scan through |
| 188 | ** the table an pick which records to delete. |
| 189 | */ |
| 190 | else{ |
| 191 | /* Begin the database scan |
| 192 | */ |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 193 | pWInfo = sqliteWhereBegin(pParse, base, pTabList, pWhere, 1); |
drh | 0353ced | 2001-03-20 22:05:00 +0000 | [diff] [blame] | 194 | if( pWInfo==0 ) goto delete_from_cleanup; |
| 195 | |
| 196 | /* Remember the key of every item to be deleted. |
| 197 | */ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 198 | sqliteVdbeAddOp(v, OP_ListWrite, 0, 0); |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 199 | if( db->flags & SQLITE_CountRows ){ |
| 200 | sqliteVdbeAddOp(v, OP_AddImm, 1, 0); |
| 201 | } |
drh | 0353ced | 2001-03-20 22:05:00 +0000 | [diff] [blame] | 202 | |
| 203 | /* End the database scan loop. |
| 204 | */ |
| 205 | sqliteWhereEnd(pWInfo); |
| 206 | |
| 207 | /* Delete every item whose key was written to the list during the |
| 208 | ** database scan. We have to delete items after the scan is complete |
| 209 | ** because deleting an item can change the scan order. |
| 210 | */ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 211 | sqliteVdbeAddOp(v, OP_ListRewind, 0, 0); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 212 | end = sqliteVdbeMakeLabel(v); |
| 213 | |
drh | c977f7f | 2002-05-21 11:38:11 +0000 | [diff] [blame] | 214 | /* This is the beginning of the delete loop when there are |
| 215 | ** row triggers. |
| 216 | */ |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 217 | if( row_triggers_exist ){ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 218 | addr = sqliteVdbeAddOp(v, OP_ListRead, 0, end); |
| 219 | sqliteVdbeAddOp(v, OP_Dup, 0, 0); |
| 220 | |
| 221 | openOp = pTab->isTemp ? OP_OpenAux : OP_Open; |
| 222 | sqliteVdbeAddOp(v, openOp, base, pTab->tnum); |
| 223 | sqliteVdbeAddOp(v, OP_MoveTo, base, 0); |
| 224 | sqliteVdbeAddOp(v, OP_OpenTemp, oldIdx, 0); |
| 225 | |
| 226 | sqliteVdbeAddOp(v, OP_Integer, 13, 0); |
drh | c977f7f | 2002-05-21 11:38:11 +0000 | [diff] [blame] | 227 | for(i = 0; i<pTab->nCol; i++){ |
| 228 | if( i==pTab->iPKey ){ |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 229 | sqliteVdbeAddOp(v, OP_Recno, base, 0); |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 230 | } else { |
drh | c977f7f | 2002-05-21 11:38:11 +0000 | [diff] [blame] | 231 | sqliteVdbeAddOp(v, OP_Column, base, i); |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 232 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 233 | } |
| 234 | sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0); |
| 235 | sqliteVdbeAddOp(v, OP_PutIntKey, oldIdx, 0); |
| 236 | sqliteVdbeAddOp(v, OP_Close, base, 0); |
| 237 | sqliteVdbeAddOp(v, OP_Rewind, oldIdx, 0); |
| 238 | |
| 239 | sqliteCodeRowTrigger(pParse, TK_DELETE, 0, TK_BEFORE, pTab, -1, |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 240 | oldIdx, (pParse->trigStack)?pParse->trigStack->orconf:OE_Default); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 241 | } |
| 242 | |
drh | c977f7f | 2002-05-21 11:38:11 +0000 | [diff] [blame] | 243 | /* Open cursors for the table we are deleting from and all its |
| 244 | ** indices. If there are row triggers, this happens inside the |
| 245 | ** OP_ListRead loop because the cursor have to all be closed |
| 246 | ** before the trigger fires. If there are no row triggers, the |
| 247 | ** cursors are opened only once on the outside the loop. |
| 248 | */ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 249 | pParse->nTab = base + 1; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 250 | openOp = pTab->isTemp ? OP_OpenWrAux : OP_OpenWrite; |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 251 | sqliteVdbeAddOp(v, openOp, base, pTab->tnum); |
drh | 0353ced | 2001-03-20 22:05:00 +0000 | [diff] [blame] | 252 | for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 253 | sqliteVdbeAddOp(v, openOp, pParse->nTab++, pIdx->tnum); |
drh | 0353ced | 2001-03-20 22:05:00 +0000 | [diff] [blame] | 254 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 255 | |
drh | c977f7f | 2002-05-21 11:38:11 +0000 | [diff] [blame] | 256 | /* This is the beginning of the delete loop when there are no |
| 257 | ** row triggers */ |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 258 | if( !row_triggers_exist ){ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 259 | addr = sqliteVdbeAddOp(v, OP_ListRead, 0, end); |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 260 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 261 | |
drh | c977f7f | 2002-05-21 11:38:11 +0000 | [diff] [blame] | 262 | /* Delete the row */ |
| 263 | sqliteGenerateRowDelete(v, pTab, base, pParse->trigStack==0); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 264 | |
drh | c977f7f | 2002-05-21 11:38:11 +0000 | [diff] [blame] | 265 | /* If there are row triggers, close all cursors then invoke |
| 266 | ** the AFTER triggers |
| 267 | */ |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 268 | if( row_triggers_exist ){ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 269 | for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){ |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 270 | sqliteVdbeAddOp(v, OP_Close, base + i, pIdx->tnum); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 271 | } |
| 272 | sqliteVdbeAddOp(v, OP_Close, base, 0); |
| 273 | sqliteCodeRowTrigger(pParse, TK_DELETE, 0, TK_AFTER, pTab, -1, |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 274 | oldIdx, (pParse->trigStack)?pParse->trigStack->orconf:OE_Default); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 275 | } |
| 276 | |
drh | c977f7f | 2002-05-21 11:38:11 +0000 | [diff] [blame] | 277 | /* End of the delete loop */ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 278 | sqliteVdbeAddOp(v, OP_Goto, 0, addr); |
| 279 | sqliteVdbeResolveLabel(v, end); |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 280 | sqliteVdbeAddOp(v, OP_ListReset, 0, 0); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 281 | |
drh | c977f7f | 2002-05-21 11:38:11 +0000 | [diff] [blame] | 282 | /* Close the cursors after the loop if there are no row triggers */ |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 283 | if( !row_triggers_exist ){ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 284 | for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){ |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 285 | sqliteVdbeAddOp(v, OP_Close, base + i, pIdx->tnum); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 286 | } |
| 287 | sqliteVdbeAddOp(v, OP_Close, base, 0); |
| 288 | pParse->nTab = base; |
| 289 | } |
drh | 0353ced | 2001-03-20 22:05:00 +0000 | [diff] [blame] | 290 | } |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 291 | sqliteEndWriteOperation(pParse); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 292 | |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 293 | /* |
| 294 | ** Return the number of rows that were deleted. |
| 295 | */ |
| 296 | if( db->flags & SQLITE_CountRows ){ |
| 297 | sqliteVdbeAddOp(v, OP_ColumnCount, 1, 0); |
| 298 | sqliteVdbeAddOp(v, OP_ColumnName, 0, 0); |
| 299 | sqliteVdbeChangeP3(v, -1, "rows deleted", P3_STATIC); |
| 300 | sqliteVdbeAddOp(v, OP_Callback, 1, 0); |
| 301 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 302 | |
| 303 | delete_from_cleanup: |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame^] | 304 | sqliteSrcListDelete(pTabList); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 305 | sqliteExprDelete(pWhere); |
| 306 | return; |
| 307 | } |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 308 | |
| 309 | /* |
| 310 | ** This routine generates VDBE code that causes a single row of a |
| 311 | ** single table to be deleted. |
| 312 | ** |
| 313 | ** The VDBE must be in a particular state when this routine is called. |
| 314 | ** These are the requirements: |
| 315 | ** |
| 316 | ** 1. A read/write cursor pointing to pTab, the table containing the row |
| 317 | ** to be deleted, must be opened as cursor number "base". |
| 318 | ** |
| 319 | ** 2. Read/write cursors for all indices of pTab must be open as |
| 320 | ** cursor number base+i for the i-th index. |
| 321 | ** |
| 322 | ** 3. The record number of the row to be deleted must be on the top |
| 323 | ** of the stack. |
| 324 | ** |
| 325 | ** This routine pops the top of the stack to remove the record number |
| 326 | ** and then generates code to remove both the table record and all index |
| 327 | ** entries that point to that record. |
| 328 | */ |
| 329 | void sqliteGenerateRowDelete( |
| 330 | Vdbe *v, /* Generate code into this VDBE */ |
| 331 | Table *pTab, /* Table containing the row to be deleted */ |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 332 | int base, /* Cursor number for the table */ |
| 333 | int count /* Increment the row change counter */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 334 | ){ |
drh | 07d6e3a | 2002-05-23 12:50:18 +0000 | [diff] [blame] | 335 | int addr; |
| 336 | addr = sqliteVdbeAddOp(v, OP_NotExists, base, 0); |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 337 | sqliteGenerateRowIndexDelete(v, pTab, base, 0); |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 338 | sqliteVdbeAddOp(v, OP_Delete, base, count); |
drh | 07d6e3a | 2002-05-23 12:50:18 +0000 | [diff] [blame] | 339 | sqliteVdbeChangeP2(v, addr, sqliteVdbeCurrentAddr(v)); |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | /* |
| 343 | ** This routine generates VDBE code that causes the deletion of all |
| 344 | ** index entries associated with a single row of a single table. |
| 345 | ** |
| 346 | ** The VDBE must be in a particular state when this routine is called. |
| 347 | ** These are the requirements: |
| 348 | ** |
| 349 | ** 1. A read/write cursor pointing to pTab, the table containing the row |
| 350 | ** to be deleted, must be opened as cursor number "base". |
| 351 | ** |
| 352 | ** 2. Read/write cursors for all indices of pTab must be open as |
| 353 | ** cursor number base+i for the i-th index. |
| 354 | ** |
| 355 | ** 3. The "base" cursor must be pointing to the row that is to be |
| 356 | ** deleted. |
| 357 | */ |
| 358 | void sqliteGenerateRowIndexDelete( |
| 359 | Vdbe *v, /* Generate code into this VDBE */ |
| 360 | Table *pTab, /* Table containing the row to be deleted */ |
| 361 | int base, /* Cursor number for the table */ |
| 362 | char *aIdxUsed /* Only delete if aIdxUsed!=0 && aIdxUsed[i]!=0 */ |
| 363 | ){ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 364 | int i; |
| 365 | Index *pIdx; |
| 366 | |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 367 | for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){ |
| 368 | int j; |
| 369 | if( aIdxUsed!=0 && aIdxUsed[i-1]==0 ) continue; |
| 370 | sqliteVdbeAddOp(v, OP_Recno, base, 0); |
| 371 | for(j=0; j<pIdx->nColumn; j++){ |
| 372 | int idx = pIdx->aiColumn[j]; |
| 373 | if( idx==pTab->iPKey ){ |
| 374 | sqliteVdbeAddOp(v, OP_Dup, j, 0); |
| 375 | }else{ |
| 376 | sqliteVdbeAddOp(v, OP_Column, base, idx); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 377 | } |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 378 | } |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 379 | sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0); |
| 380 | sqliteVdbeAddOp(v, OP_IdxDelete, base+i, 0); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 381 | } |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 382 | } |