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 | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame^] | 15 | ** $Id: delete.c,v 1.53 2003/04/22 20:30:39 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 | */ |
drh | 812d7a2 | 2003-03-27 13:50:00 +0000 | [diff] [blame] | 24 | Table *sqliteSrcListLookup(Parse *pParse, SrcList *pSrc){ |
| 25 | Table *pTab = 0; |
| 26 | int i; |
| 27 | for(i=0; i<pSrc->nSrc; i++){ |
| 28 | const char *zTab = pSrc->a[i].zName; |
| 29 | const char *zDb = pSrc->a[i].zDatabase; |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 30 | pTab = sqliteLocateTable(pParse, zTab, zDb); |
drh | 812d7a2 | 2003-03-27 13:50:00 +0000 | [diff] [blame] | 31 | pSrc->a[i].pTab = pTab; |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 32 | } |
| 33 | return pTab; |
| 34 | } |
| 35 | |
| 36 | /* |
drh | 812d7a2 | 2003-03-27 13:50:00 +0000 | [diff] [blame] | 37 | ** Check to make sure the given table is writable. If it is not |
| 38 | ** writable, generate an error message and return 1. If it is |
| 39 | ** writable return 0; |
| 40 | */ |
| 41 | int sqliteIsReadOnly(Parse *pParse, Table *pTab){ |
| 42 | if( pTab->readOnly || pTab->pSelect ){ |
drh | da93d23 | 2003-03-31 02:12:46 +0000 | [diff] [blame] | 43 | sqliteErrorMsg(pParse, "%s %s may not be modified", |
| 44 | pTab->pSelect ? "view" : "table", |
| 45 | pTab->zName); |
drh | 812d7a2 | 2003-03-27 13:50:00 +0000 | [diff] [blame] | 46 | return 1; |
| 47 | } |
| 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | /* |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 52 | ** Process a DELETE FROM statement. |
| 53 | */ |
| 54 | void sqliteDeleteFrom( |
| 55 | Parse *pParse, /* The parser context */ |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 56 | SrcList *pTabList, /* The table from which we should delete things */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 57 | Expr *pWhere /* The WHERE clause. May be null */ |
| 58 | ){ |
| 59 | Vdbe *v; /* The virtual database engine */ |
| 60 | Table *pTab; /* The table from which records will be deleted */ |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame^] | 61 | const char *zDb; /* Name of database holding pTab */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 62 | int end, addr; /* A couple addresses of generated code */ |
| 63 | int i; /* Loop counter */ |
| 64 | WhereInfo *pWInfo; /* Information about the WHERE clause */ |
| 65 | Index *pIdx; /* For looping over indices of the table */ |
drh | 4794b98 | 2000-06-06 13:54:14 +0000 | [diff] [blame] | 66 | int base; /* Index of the first available table cursor */ |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 67 | sqlite *db; /* Main database structure */ |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 68 | |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 69 | int row_triggers_exist = 0; /* True if any triggers exist */ |
| 70 | int before_triggers; /* True if there are BEFORE triggers */ |
| 71 | int after_triggers; /* True if there are AFTER triggers */ |
| 72 | int oldIdx = -1; /* Cursor for the OLD table of AFTER triggers */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 73 | |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 74 | if( pParse->nErr || sqlite_malloc_failed ){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 75 | pTabList = 0; |
| 76 | goto delete_from_cleanup; |
| 77 | } |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 78 | db = pParse->db; |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 79 | assert( pTabList->nSrc==1 ); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 80 | |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 81 | /* Locate the table which we want to delete. This table has to be |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 82 | ** put in an SrcList structure because some of the subroutines we |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 83 | ** will be calling are designed to work with multiple tables and expect |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 84 | ** an SrcList* parameter instead of just a Table* parameter. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 85 | */ |
drh | 812d7a2 | 2003-03-27 13:50:00 +0000 | [diff] [blame] | 86 | pTab = sqliteSrcListLookup(pParse, pTabList); |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 87 | if( pTab==0 ) goto delete_from_cleanup; |
| 88 | before_triggers = sqliteTriggersExist(pParse, pTab->pTrigger, |
| 89 | TK_DELETE, TK_BEFORE, TK_ROW, 0); |
| 90 | after_triggers = sqliteTriggersExist(pParse, pTab->pTrigger, |
| 91 | TK_DELETE, TK_AFTER, TK_ROW, 0); |
| 92 | row_triggers_exist = before_triggers || after_triggers; |
| 93 | if( row_triggers_exist && pTab->pSelect ){ |
| 94 | /* Just fire VIEW triggers */ |
| 95 | sqliteSrcListDelete(pTabList); |
| 96 | sqliteViewTriggers(pParse, pTab, pWhere, OE_Replace, 0); |
| 97 | return; |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 98 | } |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 99 | if( sqliteIsReadOnly(pParse, pTab) ) goto delete_from_cleanup; |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 100 | assert( pTab->pSelect==0 ); /* This table is not a view */ |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame^] | 101 | assert( pTab->iDb<db->nDb ); |
| 102 | zDb = db->aDb[pTab->iDb].zName; |
| 103 | if( sqliteAuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){ |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 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, |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 132 | !row_triggers_exist && pTab->iDb==1); |
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 | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 151 | sqliteVdbeAddOp(v, OP_Integer, pTab->iDb, 0); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 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 | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 159 | sqliteVdbeAddOp(v, OP_Clear, pTab->tnum, pTab->iDb); |
drh | 0353ced | 2001-03-20 22:05:00 +0000 | [diff] [blame] | 160 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 161 | sqliteVdbeAddOp(v, OP_Clear, pIdx->tnum, pIdx->iDb); |
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 | |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 185 | /* Open the pseudo-table used to store OLD if there are triggers. |
| 186 | */ |
| 187 | if( row_triggers_exist ){ |
| 188 | sqliteVdbeAddOp(v, OP_OpenPseudo, oldIdx, 0); |
| 189 | } |
| 190 | |
drh | 0353ced | 2001-03-20 22:05:00 +0000 | [diff] [blame] | 191 | /* Delete every item whose key was written to the list during the |
| 192 | ** database scan. We have to delete items after the scan is complete |
| 193 | ** because deleting an item can change the scan order. |
| 194 | */ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 195 | sqliteVdbeAddOp(v, OP_ListRewind, 0, 0); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 196 | end = sqliteVdbeMakeLabel(v); |
| 197 | |
drh | c977f7f | 2002-05-21 11:38:11 +0000 | [diff] [blame] | 198 | /* This is the beginning of the delete loop when there are |
| 199 | ** row triggers. |
| 200 | */ |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 201 | if( row_triggers_exist ){ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 202 | addr = sqliteVdbeAddOp(v, OP_ListRead, 0, end); |
| 203 | sqliteVdbeAddOp(v, OP_Dup, 0, 0); |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 204 | sqliteVdbeAddOp(v, OP_Integer, pTab->iDb, 0); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 205 | sqliteVdbeAddOp(v, OP_OpenRead, base, pTab->tnum); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 206 | sqliteVdbeAddOp(v, OP_MoveTo, base, 0); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 207 | |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 208 | sqliteVdbeAddOp(v, OP_Recno, base, 0); |
| 209 | sqliteVdbeAddOp(v, OP_RowData, base, 0); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 210 | sqliteVdbeAddOp(v, OP_PutIntKey, oldIdx, 0); |
| 211 | sqliteVdbeAddOp(v, OP_Close, base, 0); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 212 | |
| 213 | sqliteCodeRowTrigger(pParse, TK_DELETE, 0, TK_BEFORE, pTab, -1, |
danielk1977 | 6f34903 | 2002-06-11 02:25:40 +0000 | [diff] [blame] | 214 | oldIdx, (pParse->trigStack)?pParse->trigStack->orconf:OE_Default, |
| 215 | addr); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 216 | } |
| 217 | |
drh | c977f7f | 2002-05-21 11:38:11 +0000 | [diff] [blame] | 218 | /* Open cursors for the table we are deleting from and all its |
| 219 | ** indices. If there are row triggers, this happens inside the |
| 220 | ** OP_ListRead loop because the cursor have to all be closed |
| 221 | ** before the trigger fires. If there are no row triggers, the |
| 222 | ** cursors are opened only once on the outside the loop. |
| 223 | */ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 224 | pParse->nTab = base + 1; |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 225 | sqliteVdbeAddOp(v, OP_Integer, pTab->iDb, 0); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 226 | sqliteVdbeAddOp(v, OP_OpenWrite, base, pTab->tnum); |
drh | 0353ced | 2001-03-20 22:05:00 +0000 | [diff] [blame] | 227 | for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 228 | sqliteVdbeAddOp(v, OP_Integer, pIdx->iDb, 0); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 229 | sqliteVdbeAddOp(v, OP_OpenWrite, pParse->nTab++, pIdx->tnum); |
drh | 0353ced | 2001-03-20 22:05:00 +0000 | [diff] [blame] | 230 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 231 | |
drh | c977f7f | 2002-05-21 11:38:11 +0000 | [diff] [blame] | 232 | /* This is the beginning of the delete loop when there are no |
| 233 | ** row triggers */ |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 234 | if( !row_triggers_exist ){ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 235 | addr = sqliteVdbeAddOp(v, OP_ListRead, 0, end); |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 236 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 237 | |
drh | c977f7f | 2002-05-21 11:38:11 +0000 | [diff] [blame] | 238 | /* Delete the row */ |
drh | 38640e1 | 2002-07-05 21:42:36 +0000 | [diff] [blame] | 239 | sqliteGenerateRowDelete(db, v, pTab, base, pParse->trigStack==0); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 240 | |
drh | c977f7f | 2002-05-21 11:38:11 +0000 | [diff] [blame] | 241 | /* If there are row triggers, close all cursors then invoke |
| 242 | ** the AFTER triggers |
| 243 | */ |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 244 | if( row_triggers_exist ){ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 245 | for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){ |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 246 | sqliteVdbeAddOp(v, OP_Close, base + i, pIdx->tnum); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 247 | } |
| 248 | sqliteVdbeAddOp(v, OP_Close, base, 0); |
| 249 | sqliteCodeRowTrigger(pParse, TK_DELETE, 0, TK_AFTER, pTab, -1, |
danielk1977 | 6f34903 | 2002-06-11 02:25:40 +0000 | [diff] [blame] | 250 | oldIdx, (pParse->trigStack)?pParse->trigStack->orconf:OE_Default, |
| 251 | addr); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 252 | } |
| 253 | |
drh | c977f7f | 2002-05-21 11:38:11 +0000 | [diff] [blame] | 254 | /* End of the delete loop */ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 255 | sqliteVdbeAddOp(v, OP_Goto, 0, addr); |
| 256 | sqliteVdbeResolveLabel(v, end); |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 257 | sqliteVdbeAddOp(v, OP_ListReset, 0, 0); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 258 | |
drh | c977f7f | 2002-05-21 11:38:11 +0000 | [diff] [blame] | 259 | /* Close the cursors after the loop if there are no row triggers */ |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 260 | if( !row_triggers_exist ){ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 261 | for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){ |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 262 | sqliteVdbeAddOp(v, OP_Close, base + i, pIdx->tnum); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 263 | } |
| 264 | sqliteVdbeAddOp(v, OP_Close, base, 0); |
| 265 | pParse->nTab = base; |
| 266 | } |
drh | 0353ced | 2001-03-20 22:05:00 +0000 | [diff] [blame] | 267 | } |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 268 | sqliteEndWriteOperation(pParse); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 269 | |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 270 | /* |
| 271 | ** Return the number of rows that were deleted. |
| 272 | */ |
| 273 | if( db->flags & SQLITE_CountRows ){ |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 274 | sqliteVdbeAddOp(v, OP_ColumnName, 0, 0); |
| 275 | sqliteVdbeChangeP3(v, -1, "rows deleted", P3_STATIC); |
| 276 | sqliteVdbeAddOp(v, OP_Callback, 1, 0); |
| 277 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 278 | |
| 279 | delete_from_cleanup: |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 280 | sqliteSrcListDelete(pTabList); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 281 | sqliteExprDelete(pWhere); |
| 282 | return; |
| 283 | } |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 284 | |
| 285 | /* |
| 286 | ** This routine generates VDBE code that causes a single row of a |
| 287 | ** single table to be deleted. |
| 288 | ** |
| 289 | ** The VDBE must be in a particular state when this routine is called. |
| 290 | ** These are the requirements: |
| 291 | ** |
| 292 | ** 1. A read/write cursor pointing to pTab, the table containing the row |
| 293 | ** to be deleted, must be opened as cursor number "base". |
| 294 | ** |
| 295 | ** 2. Read/write cursors for all indices of pTab must be open as |
| 296 | ** cursor number base+i for the i-th index. |
| 297 | ** |
| 298 | ** 3. The record number of the row to be deleted must be on the top |
| 299 | ** of the stack. |
| 300 | ** |
| 301 | ** This routine pops the top of the stack to remove the record number |
| 302 | ** and then generates code to remove both the table record and all index |
| 303 | ** entries that point to that record. |
| 304 | */ |
| 305 | void sqliteGenerateRowDelete( |
drh | 38640e1 | 2002-07-05 21:42:36 +0000 | [diff] [blame] | 306 | sqlite *db, /* The database containing the index */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 307 | Vdbe *v, /* Generate code into this VDBE */ |
| 308 | Table *pTab, /* Table containing the row to be deleted */ |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 309 | int base, /* Cursor number for the table */ |
| 310 | int count /* Increment the row change counter */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 311 | ){ |
drh | 07d6e3a | 2002-05-23 12:50:18 +0000 | [diff] [blame] | 312 | int addr; |
| 313 | addr = sqliteVdbeAddOp(v, OP_NotExists, base, 0); |
drh | 38640e1 | 2002-07-05 21:42:36 +0000 | [diff] [blame] | 314 | sqliteGenerateRowIndexDelete(db, v, pTab, base, 0); |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 315 | sqliteVdbeAddOp(v, OP_Delete, base, count); |
drh | 07d6e3a | 2002-05-23 12:50:18 +0000 | [diff] [blame] | 316 | sqliteVdbeChangeP2(v, addr, sqliteVdbeCurrentAddr(v)); |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | /* |
| 320 | ** This routine generates VDBE code that causes the deletion of all |
| 321 | ** index entries associated with a single row of a single table. |
| 322 | ** |
| 323 | ** The VDBE must be in a particular state when this routine is called. |
| 324 | ** These are the requirements: |
| 325 | ** |
| 326 | ** 1. A read/write cursor pointing to pTab, the table containing the row |
| 327 | ** to be deleted, must be opened as cursor number "base". |
| 328 | ** |
| 329 | ** 2. Read/write cursors for all indices of pTab must be open as |
| 330 | ** cursor number base+i for the i-th index. |
| 331 | ** |
| 332 | ** 3. The "base" cursor must be pointing to the row that is to be |
| 333 | ** deleted. |
| 334 | */ |
| 335 | void sqliteGenerateRowIndexDelete( |
drh | 38640e1 | 2002-07-05 21:42:36 +0000 | [diff] [blame] | 336 | sqlite *db, /* The database containing the index */ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 337 | Vdbe *v, /* Generate code into this VDBE */ |
| 338 | Table *pTab, /* Table containing the row to be deleted */ |
| 339 | int base, /* Cursor number for the table */ |
| 340 | char *aIdxUsed /* Only delete if aIdxUsed!=0 && aIdxUsed[i]!=0 */ |
| 341 | ){ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 342 | int i; |
| 343 | Index *pIdx; |
| 344 | |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 345 | for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){ |
| 346 | int j; |
| 347 | if( aIdxUsed!=0 && aIdxUsed[i-1]==0 ) continue; |
| 348 | sqliteVdbeAddOp(v, OP_Recno, base, 0); |
| 349 | for(j=0; j<pIdx->nColumn; j++){ |
| 350 | int idx = pIdx->aiColumn[j]; |
| 351 | if( idx==pTab->iPKey ){ |
| 352 | sqliteVdbeAddOp(v, OP_Dup, j, 0); |
| 353 | }else{ |
| 354 | sqliteVdbeAddOp(v, OP_Column, base, idx); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 355 | } |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 356 | } |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 357 | sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0); |
drh | 491791a | 2002-07-18 00:34:09 +0000 | [diff] [blame] | 358 | if( db->file_format>=4 ) sqliteAddIdxKeyType(v, pIdx); |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 359 | sqliteVdbeAddOp(v, OP_IdxDelete, base+i, 0); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 360 | } |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 361 | } |