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