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 |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 13 | ** in order to generate code for DELETE FROM statements. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 14 | ** |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame^] | 15 | ** $Id: delete.c,v 1.164 2008/03/25 17:23: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 | /* |
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++){ |
drh | ca42411 | 2008-01-25 15:04:48 +0000 | [diff] [blame] | 29 | pTab = sqlite3LocateTable(pParse, 0, pItem->zName, pItem->zDatabase); |
danielk1977 | a04a34f | 2007-04-16 15:06:25 +0000 | [diff] [blame] | 30 | sqlite3DeleteTable(pItem->pTab); |
drh | 855eb1c | 2004-08-31 13:45:11 +0000 | [diff] [blame] | 31 | pItem->pTab = pTab; |
drh | ed8a3bb | 2005-06-06 21:19:56 +0000 | [diff] [blame] | 32 | if( pTab ){ |
| 33 | pTab->nRef++; |
| 34 | } |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 35 | } |
| 36 | return pTab; |
| 37 | } |
| 38 | |
| 39 | /* |
drh | 812d7a2 | 2003-03-27 13:50:00 +0000 | [diff] [blame] | 40 | ** Check to make sure the given table is writable. If it is not |
| 41 | ** writable, generate an error message and return 1. If it is |
| 42 | ** writable return 0; |
| 43 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 44 | int sqlite3IsReadOnly(Parse *pParse, Table *pTab, int viewOk){ |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 45 | if( (pTab->readOnly && (pParse->db->flags & SQLITE_WriteSchema)==0 |
| 46 | && pParse->nested==0) |
| 47 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
danielk1977 | d1ab1ba | 2006-06-15 04:28:13 +0000 | [diff] [blame] | 48 | || (pTab->pMod && pTab->pMod->pModule->xUpdate==0) |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 49 | #endif |
| 50 | ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 51 | sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName); |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 52 | return 1; |
| 53 | } |
danielk1977 | b84f96f | 2005-01-20 11:32:23 +0000 | [diff] [blame] | 54 | #ifndef SQLITE_OMIT_VIEW |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 55 | if( !viewOk && pTab->pSelect ){ |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame] | 56 | sqlite3ErrorMsg(pParse,"cannot modify %s because it is a view",pTab->zName); |
drh | 812d7a2 | 2003-03-27 13:50:00 +0000 | [diff] [blame] | 57 | return 1; |
| 58 | } |
danielk1977 | b84f96f | 2005-01-20 11:32:23 +0000 | [diff] [blame] | 59 | #endif |
drh | 812d7a2 | 2003-03-27 13:50:00 +0000 | [diff] [blame] | 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | /* |
drh | ad6d946 | 2004-09-19 02:15:24 +0000 | [diff] [blame] | 64 | ** Generate code that will open a table for reading. |
| 65 | */ |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 66 | void sqlite3OpenTable( |
| 67 | Parse *p, /* Generate code into this VDBE */ |
drh | ad6d946 | 2004-09-19 02:15:24 +0000 | [diff] [blame] | 68 | int iCur, /* The cursor number of the table */ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 69 | int iDb, /* The database index in sqlite3.aDb[] */ |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 70 | Table *pTab, /* The table to be opened */ |
| 71 | int opcode /* OP_OpenRead or OP_OpenWrite */ |
drh | ad6d946 | 2004-09-19 02:15:24 +0000 | [diff] [blame] | 72 | ){ |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 73 | Vdbe *v; |
| 74 | if( IsVirtual(pTab) ) return; |
| 75 | v = sqlite3GetVdbe(p); |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 76 | assert( opcode==OP_OpenWrite || opcode==OP_OpenRead ); |
| 77 | sqlite3TableLock(p, iDb, pTab->tnum, (opcode==OP_OpenWrite), pTab->zName); |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 78 | sqlite3VdbeAddOp2(v, OP_SetNumColumns, 0, pTab->nCol); |
danielk1977 | 207872a | 2008-01-03 07:54:23 +0000 | [diff] [blame] | 79 | sqlite3VdbeAddOp3(v, opcode, iCur, pTab->tnum, iDb); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 80 | VdbeComment((v, "%s", pTab->zName)); |
drh | ad6d946 | 2004-09-19 02:15:24 +0000 | [diff] [blame] | 81 | } |
| 82 | |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 83 | |
drh | 0f35a6b | 2008-02-12 16:52:14 +0000 | [diff] [blame] | 84 | #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) |
| 85 | /* |
| 86 | ** Evaluate a view and store its result in an ephemeral table. The |
| 87 | ** pWhere argument is an optional WHERE clause that restricts the |
| 88 | ** set of rows in the view that are to be added to the ephemeral table. |
| 89 | */ |
| 90 | void sqlite3MaterializeView( |
| 91 | Parse *pParse, /* Parsing context */ |
| 92 | Select *pView, /* View definition */ |
| 93 | Expr *pWhere, /* Optional WHERE clause to be added */ |
| 94 | u32 col_mask, /* Render only the columns in this mask. */ |
| 95 | int iCur /* Cursor number for ephemerial table */ |
| 96 | ){ |
| 97 | SelectDest dest; |
| 98 | Select *pDup; |
| 99 | sqlite3 *db = pParse->db; |
| 100 | |
| 101 | pDup = sqlite3SelectDup(db, pView); |
| 102 | if( pWhere ){ |
| 103 | SrcList *pFrom; |
| 104 | |
| 105 | pWhere = sqlite3ExprDup(db, pWhere); |
| 106 | pFrom = sqlite3SrcListAppendFromTerm(pParse, 0, 0, 0, 0, pDup, 0, 0); |
| 107 | pDup = sqlite3SelectNew(pParse, 0, pFrom, pWhere, 0, 0, 0, 0, 0, 0); |
| 108 | } |
| 109 | sqlite3SelectMask(pParse, pDup, col_mask); |
| 110 | sqlite3SelectDestInit(&dest, SRT_EphemTab, iCur); |
| 111 | sqlite3Select(pParse, pDup, &dest, 0, 0, 0, 0); |
| 112 | sqlite3SelectDelete(pDup); |
| 113 | } |
| 114 | #endif /* !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) */ |
| 115 | |
| 116 | |
drh | ad6d946 | 2004-09-19 02:15:24 +0000 | [diff] [blame] | 117 | /* |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 118 | ** Generate code for a DELETE FROM statement. |
| 119 | ** |
| 120 | ** DELETE FROM table_wxyz WHERE a<5 AND b NOT NULL; |
| 121 | ** \________/ \________________/ |
| 122 | ** pTabList pWhere |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 123 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 124 | void sqlite3DeleteFrom( |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 125 | Parse *pParse, /* The parser context */ |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 126 | SrcList *pTabList, /* The table from which we should delete things */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 127 | Expr *pWhere /* The WHERE clause. May be null */ |
| 128 | ){ |
| 129 | Vdbe *v; /* The virtual database engine */ |
| 130 | Table *pTab; /* The table from which records will be deleted */ |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 131 | const char *zDb; /* Name of database holding pTab */ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 132 | int end, addr = 0; /* A couple addresses of generated code */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 133 | int i; /* Loop counter */ |
| 134 | WhereInfo *pWInfo; /* Information about the WHERE clause */ |
| 135 | Index *pIdx; /* For looping over indices of the table */ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 136 | int iCur; /* VDBE Cursor number for pTab */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 137 | sqlite3 *db; /* Main database structure */ |
drh | 85e2096 | 2003-04-25 17:52:11 +0000 | [diff] [blame] | 138 | AuthContext sContext; /* Authorization context */ |
drh | 798da52 | 2004-11-04 04:42:28 +0000 | [diff] [blame] | 139 | int oldIdx = -1; /* Cursor for the OLD table of AFTER triggers */ |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 140 | NameContext sNC; /* Name context to resolve expressions in */ |
drh | 53a6777 | 2007-02-07 01:06:52 +0000 | [diff] [blame] | 141 | int iDb; /* Database number */ |
| 142 | int memCnt = 0; /* Memory cell used for change counting */ |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 143 | |
drh | 798da52 | 2004-11-04 04:42:28 +0000 | [diff] [blame] | 144 | #ifndef SQLITE_OMIT_TRIGGER |
| 145 | int isView; /* True if attempting to delete from a view */ |
drh | dca7684 | 2004-12-07 14:06:13 +0000 | [diff] [blame] | 146 | int triggers_exist = 0; /* True if any triggers exist */ |
drh | 798da52 | 2004-11-04 04:42:28 +0000 | [diff] [blame] | 147 | #endif |
danielk1977 | 8f2c54e | 2008-01-01 19:02:09 +0000 | [diff] [blame] | 148 | int iBeginAfterTrigger; /* Address of after trigger program */ |
| 149 | int iEndAfterTrigger; /* Exit of after trigger program */ |
| 150 | int iBeginBeforeTrigger; /* Address of before trigger program */ |
| 151 | int iEndBeforeTrigger; /* Exit of before trigger program */ |
| 152 | u32 old_col_mask = 0; /* Mask of OLD.* columns in use */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 153 | |
drh | 85e2096 | 2003-04-25 17:52:11 +0000 | [diff] [blame] | 154 | sContext.pParse = 0; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 155 | db = pParse->db; |
| 156 | if( pParse->nErr || db->mallocFailed ){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 157 | goto delete_from_cleanup; |
| 158 | } |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 159 | assert( pTabList->nSrc==1 ); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 160 | |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 161 | /* Locate the table which we want to delete. This table has to be |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 162 | ** put in an SrcList structure because some of the subroutines we |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 163 | ** will be calling are designed to work with multiple tables and expect |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 164 | ** an SrcList* parameter instead of just a Table* parameter. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 165 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 166 | pTab = sqlite3SrcListLookup(pParse, pTabList); |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 167 | if( pTab==0 ) goto delete_from_cleanup; |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 168 | |
| 169 | /* Figure out if we have any triggers and if the table being |
| 170 | ** deleted from is a view |
| 171 | */ |
| 172 | #ifndef SQLITE_OMIT_TRIGGER |
drh | dca7684 | 2004-12-07 14:06:13 +0000 | [diff] [blame] | 173 | triggers_exist = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0); |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 174 | isView = pTab->pSelect!=0; |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 175 | #else |
drh | dca7684 | 2004-12-07 14:06:13 +0000 | [diff] [blame] | 176 | # define triggers_exist 0 |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 177 | # define isView 0 |
| 178 | #endif |
| 179 | #ifdef SQLITE_OMIT_VIEW |
| 180 | # undef isView |
| 181 | # define isView 0 |
| 182 | #endif |
| 183 | |
drh | dca7684 | 2004-12-07 14:06:13 +0000 | [diff] [blame] | 184 | if( sqlite3IsReadOnly(pParse, pTab, triggers_exist) ){ |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 185 | goto delete_from_cleanup; |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 186 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 187 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
| 188 | assert( iDb<db->nDb ); |
| 189 | zDb = db->aDb[iDb].zName; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 190 | if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){ |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 191 | goto delete_from_cleanup; |
| 192 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 193 | |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 194 | /* If pTab is really a view, make sure it has been initialized. |
| 195 | */ |
danielk1977 | b3d24bf | 2006-06-19 03:05:10 +0000 | [diff] [blame] | 196 | if( sqlite3ViewGetColumnNames(pParse, pTab) ){ |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 197 | goto delete_from_cleanup; |
| 198 | } |
| 199 | |
drh | c977f7f | 2002-05-21 11:38:11 +0000 | [diff] [blame] | 200 | /* Allocate a cursor used to store the old.* data for a trigger. |
| 201 | */ |
drh | dca7684 | 2004-12-07 14:06:13 +0000 | [diff] [blame] | 202 | if( triggers_exist ){ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 203 | oldIdx = pParse->nTab++; |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 204 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 205 | |
drh | 0f35a6b | 2008-02-12 16:52:14 +0000 | [diff] [blame] | 206 | /* Assign cursor number to the table and all its indices. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 207 | */ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 208 | assert( pTabList->nSrc==1 ); |
| 209 | iCur = pTabList->a[0].iCursor = pParse->nTab++; |
danielk1977 | e448dc4 | 2008-01-02 11:50:51 +0000 | [diff] [blame] | 210 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 211 | pParse->nTab++; |
| 212 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 213 | |
drh | 85e2096 | 2003-04-25 17:52:11 +0000 | [diff] [blame] | 214 | /* Start the view context |
| 215 | */ |
| 216 | if( isView ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 217 | sqlite3AuthContextPush(pParse, &sContext, pTab->zName); |
drh | 85e2096 | 2003-04-25 17:52:11 +0000 | [diff] [blame] | 218 | } |
| 219 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 220 | /* Begin generating code. |
| 221 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 222 | v = sqlite3GetVdbe(pParse); |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 223 | if( v==0 ){ |
| 224 | goto delete_from_cleanup; |
| 225 | } |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 226 | if( pParse->nested==0 ) sqlite3VdbeCountChanges(v); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 227 | sqlite3BeginWriteOperation(pParse, triggers_exist, iDb); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 228 | |
danielk1977 | 8f2c54e | 2008-01-01 19:02:09 +0000 | [diff] [blame] | 229 | if( triggers_exist ){ |
danielk1977 | dd2fb29 | 2008-01-04 13:57:26 +0000 | [diff] [blame] | 230 | int orconf = ((pParse->trigStack)?pParse->trigStack->orconf:OE_Default); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 231 | int iGoto = sqlite3VdbeAddOp0(v, OP_Goto); |
danielk1977 | 8f2c54e | 2008-01-01 19:02:09 +0000 | [diff] [blame] | 232 | addr = sqlite3VdbeMakeLabel(v); |
danielk1977 | dd2fb29 | 2008-01-04 13:57:26 +0000 | [diff] [blame] | 233 | |
danielk1977 | 8f2c54e | 2008-01-01 19:02:09 +0000 | [diff] [blame] | 234 | iBeginBeforeTrigger = sqlite3VdbeCurrentAddr(v); |
| 235 | (void)sqlite3CodeRowTrigger(pParse, TK_DELETE, 0, TRIGGER_BEFORE, pTab, |
danielk1977 | dd2fb29 | 2008-01-04 13:57:26 +0000 | [diff] [blame] | 236 | -1, oldIdx, orconf, addr, &old_col_mask, 0); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 237 | iEndBeforeTrigger = sqlite3VdbeAddOp0(v, OP_Goto); |
danielk1977 | dd2fb29 | 2008-01-04 13:57:26 +0000 | [diff] [blame] | 238 | |
danielk1977 | 8f2c54e | 2008-01-01 19:02:09 +0000 | [diff] [blame] | 239 | iBeginAfterTrigger = sqlite3VdbeCurrentAddr(v); |
| 240 | (void)sqlite3CodeRowTrigger(pParse, TK_DELETE, 0, TRIGGER_AFTER, pTab, -1, |
danielk1977 | dd2fb29 | 2008-01-04 13:57:26 +0000 | [diff] [blame] | 241 | oldIdx, orconf, addr, &old_col_mask, 0); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 242 | iEndAfterTrigger = sqlite3VdbeAddOp0(v, OP_Goto); |
danielk1977 | dd2fb29 | 2008-01-04 13:57:26 +0000 | [diff] [blame] | 243 | |
danielk1977 | 8f2c54e | 2008-01-01 19:02:09 +0000 | [diff] [blame] | 244 | sqlite3VdbeJumpHere(v, iGoto); |
| 245 | } |
| 246 | |
drh | 9d2985c | 2005-09-08 01:58:42 +0000 | [diff] [blame] | 247 | /* If we are trying to delete from a view, realize that view into |
| 248 | ** a ephemeral table. |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 249 | */ |
| 250 | if( isView ){ |
drh | 0f35a6b | 2008-02-12 16:52:14 +0000 | [diff] [blame] | 251 | sqlite3MaterializeView(pParse, pTab->pSelect, pWhere, old_col_mask, iCur); |
| 252 | } |
drh | 1013c93 | 2008-01-06 00:25:21 +0000 | [diff] [blame] | 253 | |
drh | 0f35a6b | 2008-02-12 16:52:14 +0000 | [diff] [blame] | 254 | /* Resolve the column names in the WHERE clause. |
| 255 | */ |
| 256 | memset(&sNC, 0, sizeof(sNC)); |
| 257 | sNC.pParse = pParse; |
| 258 | sNC.pSrcList = pTabList; |
| 259 | if( sqlite3ExprResolveNames(&sNC, pWhere) ){ |
| 260 | goto delete_from_cleanup; |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 261 | } |
| 262 | |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 263 | /* Initialize the counter of the number of rows deleted, if |
| 264 | ** we are counting rows. |
| 265 | */ |
| 266 | if( db->flags & SQLITE_CountRows ){ |
drh | 0a07c10 | 2008-01-03 18:03:08 +0000 | [diff] [blame] | 267 | memCnt = ++pParse->nMem; |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 268 | sqlite3VdbeAddOp2(v, OP_Integer, 0, memCnt); |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 269 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 270 | |
drh | 0353ced | 2001-03-20 22:05:00 +0000 | [diff] [blame] | 271 | /* Special case: A DELETE without a WHERE clause deletes everything. |
drh | c977f7f | 2002-05-21 11:38:11 +0000 | [diff] [blame] | 272 | ** It is easier just to erase the whole table. Note, however, that |
| 273 | ** this means that the row change count will be incorrect. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 274 | */ |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 275 | if( pWhere==0 && !triggers_exist && !IsVirtual(pTab) ){ |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 276 | if( db->flags & SQLITE_CountRows ){ |
| 277 | /* If counting rows deleted, just count the total number of |
| 278 | ** entries in the table. */ |
danielk1977 | f011300 | 2006-01-24 12:09:17 +0000 | [diff] [blame] | 279 | int addr2; |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 280 | if( !isView ){ |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 281 | sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenRead); |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 282 | } |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 283 | sqlite3VdbeAddOp2(v, OP_Rewind, iCur, sqlite3VdbeCurrentAddr(v)+2); |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 284 | addr2 = sqlite3VdbeAddOp2(v, OP_AddImm, memCnt, 1); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 285 | sqlite3VdbeAddOp2(v, OP_Next, iCur, addr2); |
| 286 | sqlite3VdbeAddOp1(v, OP_Close, iCur); |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 287 | } |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 288 | if( !isView ){ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 289 | sqlite3VdbeAddOp2(v, OP_Clear, pTab->tnum, iDb); |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 290 | if( !pParse->nested ){ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 291 | sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_STATIC); |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 292 | } |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 293 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 294 | assert( pIdx->pSchema==pTab->pSchema ); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 295 | sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb); |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 296 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 297 | } |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 298 | } |
drh | 0353ced | 2001-03-20 22:05:00 +0000 | [diff] [blame] | 299 | /* 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] | 300 | ** the table and pick which records to delete. |
drh | 0353ced | 2001-03-20 22:05:00 +0000 | [diff] [blame] | 301 | */ |
| 302 | else{ |
danielk1977 | dd2fb29 | 2008-01-04 13:57:26 +0000 | [diff] [blame] | 303 | int iRowid = ++pParse->nMem; /* Used for storing rowid values. */ |
danielk1977 | 96cb76f | 2008-01-04 13:24:28 +0000 | [diff] [blame] | 304 | |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 305 | /* Begin the database scan |
| 306 | */ |
danielk1977 | a9d1ccb | 2008-01-05 17:39:29 +0000 | [diff] [blame] | 307 | pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0); |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 308 | if( pWInfo==0 ) goto delete_from_cleanup; |
| 309 | |
drh | e6f85e7 | 2004-12-25 01:03:13 +0000 | [diff] [blame] | 310 | /* Remember the rowid of every item to be deleted. |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 311 | */ |
danielk1977 | dd2fb29 | 2008-01-04 13:57:26 +0000 | [diff] [blame] | 312 | sqlite3VdbeAddOp2(v, IsVirtual(pTab) ? OP_VRowid : OP_Rowid, iCur, iRowid); |
| 313 | sqlite3VdbeAddOp1(v, OP_FifoWrite, iRowid); |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 314 | if( db->flags & SQLITE_CountRows ){ |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 315 | sqlite3VdbeAddOp2(v, OP_AddImm, memCnt, 1); |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | /* End the database scan loop. |
| 319 | */ |
| 320 | sqlite3WhereEnd(pWInfo); |
| 321 | |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 322 | /* Open the pseudo-table used to store OLD if there are triggers. |
| 323 | */ |
drh | dca7684 | 2004-12-07 14:06:13 +0000 | [diff] [blame] | 324 | if( triggers_exist ){ |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 325 | sqlite3VdbeAddOp2(v, OP_SetNumColumns, 0, pTab->nCol); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 326 | sqlite3VdbeAddOp1(v, OP_OpenPseudo, oldIdx); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 327 | } |
| 328 | |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 329 | /* Delete every item whose key was written to the list during the |
| 330 | ** database scan. We have to delete items after the scan is complete |
| 331 | ** because deleting an item can change the scan order. |
danielk1977 | ed326d7 | 2004-11-16 15:50:19 +0000 | [diff] [blame] | 332 | */ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 333 | end = sqlite3VdbeMakeLabel(v); |
danielk1977 | ed326d7 | 2004-11-16 15:50:19 +0000 | [diff] [blame] | 334 | |
danielk1977 | e448dc4 | 2008-01-02 11:50:51 +0000 | [diff] [blame] | 335 | if( !isView ){ |
| 336 | /* Open cursors for the table we are deleting from and |
| 337 | ** all its indices. |
| 338 | */ |
| 339 | sqlite3OpenTableAndIndices(pParse, pTab, iCur, OP_OpenWrite); |
| 340 | } |
| 341 | |
| 342 | /* This is the beginning of the delete loop. If a trigger encounters |
| 343 | ** an IGNORE constraint, it jumps back to here. |
drh | c977f7f | 2002-05-21 11:38:11 +0000 | [diff] [blame] | 344 | */ |
drh | dca7684 | 2004-12-07 14:06:13 +0000 | [diff] [blame] | 345 | if( triggers_exist ){ |
danielk1977 | 8f2c54e | 2008-01-01 19:02:09 +0000 | [diff] [blame] | 346 | sqlite3VdbeResolveLabel(v, addr); |
danielk1977 | e448dc4 | 2008-01-02 11:50:51 +0000 | [diff] [blame] | 347 | } |
danielk1977 | 96cb76f | 2008-01-04 13:24:28 +0000 | [diff] [blame] | 348 | addr = sqlite3VdbeAddOp2(v, OP_FifoRead, iRowid, end); |
danielk1977 | e448dc4 | 2008-01-02 11:50:51 +0000 | [diff] [blame] | 349 | |
| 350 | if( triggers_exist ){ |
danielk1977 | 96cb76f | 2008-01-04 13:24:28 +0000 | [diff] [blame] | 351 | int iData = ++pParse->nMem; /* For storing row data of OLD table */ |
| 352 | |
| 353 | /* If the record is no longer present in the table, jump to the |
| 354 | ** next iteration of the loop through the contents of the fifo. |
| 355 | */ |
| 356 | sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addr, iRowid); |
| 357 | |
| 358 | /* Populate the OLD.* pseudo-table */ |
danielk1977 | 8f2c54e | 2008-01-01 19:02:09 +0000 | [diff] [blame] | 359 | if( old_col_mask ){ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 360 | sqlite3VdbeAddOp2(v, OP_RowData, iCur, iData); |
danielk1977 | 8f2c54e | 2008-01-01 19:02:09 +0000 | [diff] [blame] | 361 | }else{ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 362 | sqlite3VdbeAddOp2(v, OP_Null, 0, iData); |
danielk1977 | 8f2c54e | 2008-01-01 19:02:09 +0000 | [diff] [blame] | 363 | } |
danielk1977 | 96cb76f | 2008-01-04 13:24:28 +0000 | [diff] [blame] | 364 | sqlite3VdbeAddOp3(v, OP_Insert, oldIdx, iData, iRowid); |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 365 | |
danielk1977 | 8f2c54e | 2008-01-01 19:02:09 +0000 | [diff] [blame] | 366 | /* Jump back and run the BEFORE triggers */ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 367 | sqlite3VdbeAddOp2(v, OP_Goto, 0, iBeginBeforeTrigger); |
danielk1977 | 8f2c54e | 2008-01-01 19:02:09 +0000 | [diff] [blame] | 368 | sqlite3VdbeJumpHere(v, iEndBeforeTrigger); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 369 | } |
| 370 | |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 371 | if( !isView ){ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 372 | /* Delete the row */ |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 373 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 374 | if( IsVirtual(pTab) ){ |
danielk1977 | 96cb76f | 2008-01-04 13:24:28 +0000 | [diff] [blame] | 375 | const char *pVtab = (const char *)pTab->pVtab; |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 376 | pParse->pVirtualLock = pTab; |
danielk1977 | 96cb76f | 2008-01-04 13:24:28 +0000 | [diff] [blame] | 377 | sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, iRowid, pVtab, P4_VTAB); |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 378 | }else |
| 379 | #endif |
| 380 | { |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 381 | sqlite3GenerateRowDelete(pParse, pTab, iCur, iRowid, pParse->nested==0); |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 382 | } |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | /* If there are row triggers, close all cursors then invoke |
| 386 | ** the AFTER triggers |
| 387 | */ |
drh | dca7684 | 2004-12-07 14:06:13 +0000 | [diff] [blame] | 388 | if( triggers_exist ){ |
danielk1977 | 8f2c54e | 2008-01-01 19:02:09 +0000 | [diff] [blame] | 389 | /* Jump back and run the AFTER triggers */ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 390 | sqlite3VdbeAddOp2(v, OP_Goto, 0, iBeginAfterTrigger); |
danielk1977 | 8f2c54e | 2008-01-01 19:02:09 +0000 | [diff] [blame] | 391 | sqlite3VdbeJumpHere(v, iEndAfterTrigger); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 392 | } |
| 393 | |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 394 | /* End of the delete loop */ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 395 | sqlite3VdbeAddOp2(v, OP_Goto, 0, addr); |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 396 | sqlite3VdbeResolveLabel(v, end); |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 397 | |
| 398 | /* Close the cursors after the loop if there are no row triggers */ |
danielk1977 | e448dc4 | 2008-01-02 11:50:51 +0000 | [diff] [blame] | 399 | if( !isView && !IsVirtual(pTab) ){ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 400 | for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 401 | sqlite3VdbeAddOp2(v, OP_Close, iCur + i, pIdx->tnum); |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 402 | } |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 403 | sqlite3VdbeAddOp1(v, OP_Close, iCur); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 404 | } |
drh | 0353ced | 2001-03-20 22:05:00 +0000 | [diff] [blame] | 405 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 406 | |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 407 | /* |
danielk1977 | e7de6f2 | 2004-11-05 06:02:06 +0000 | [diff] [blame] | 408 | ** Return the number of rows that were deleted. If this routine is |
| 409 | ** generating code because of a call to sqlite3NestedParse(), do not |
| 410 | ** invoke the callback function. |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 411 | */ |
danielk1977 | cc6bd38 | 2005-01-10 02:48:49 +0000 | [diff] [blame] | 412 | if( db->flags & SQLITE_CountRows && pParse->nested==0 && !pParse->trigStack ){ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 413 | sqlite3VdbeAddOp2(v, OP_ResultRow, memCnt, 1); |
danielk1977 | 22322fd | 2004-05-25 23:35:17 +0000 | [diff] [blame] | 414 | sqlite3VdbeSetNumCols(v, 1); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 415 | sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows deleted", P4_STATIC); |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 416 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 417 | |
| 418 | delete_from_cleanup: |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 419 | sqlite3AuthContextPop(&sContext); |
| 420 | sqlite3SrcListDelete(pTabList); |
| 421 | sqlite3ExprDelete(pWhere); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 422 | return; |
| 423 | } |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 424 | |
| 425 | /* |
| 426 | ** This routine generates VDBE code that causes a single row of a |
| 427 | ** single table to be deleted. |
| 428 | ** |
| 429 | ** The VDBE must be in a particular state when this routine is called. |
| 430 | ** These are the requirements: |
| 431 | ** |
| 432 | ** 1. A read/write cursor pointing to pTab, the table containing the row |
| 433 | ** to be deleted, must be opened as cursor number "base". |
| 434 | ** |
| 435 | ** 2. Read/write cursors for all indices of pTab must be open as |
| 436 | ** cursor number base+i for the i-th index. |
| 437 | ** |
danielk1977 | 96cb76f | 2008-01-04 13:24:28 +0000 | [diff] [blame] | 438 | ** 3. The record number of the row to be deleted must be stored in |
| 439 | ** memory cell iRowid. |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 440 | ** |
| 441 | ** This routine pops the top of the stack to remove the record number |
| 442 | ** and then generates code to remove both the table record and all index |
| 443 | ** entries that point to that record. |
| 444 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 445 | void sqlite3GenerateRowDelete( |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 446 | Parse *pParse, /* Parsing context */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 447 | Table *pTab, /* Table containing the row to be deleted */ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 448 | int iCur, /* Cursor number for the table */ |
danielk1977 | 96cb76f | 2008-01-04 13:24:28 +0000 | [diff] [blame] | 449 | int iRowid, /* Memory cell that contains the rowid to delete */ |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 450 | int count /* Increment the row change counter */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 451 | ){ |
drh | 07d6e3a | 2002-05-23 12:50:18 +0000 | [diff] [blame] | 452 | int addr; |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 453 | Vdbe *v; |
| 454 | |
| 455 | v = pParse->pVdbe; |
danielk1977 | 96cb76f | 2008-01-04 13:24:28 +0000 | [diff] [blame] | 456 | addr = sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, iRowid); |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 457 | sqlite3GenerateRowIndexDelete(pParse, pTab, iCur, 0); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 458 | sqlite3VdbeAddOp2(v, OP_Delete, iCur, (count?OPFLAG_NCHANGE:0)); |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 459 | if( count ){ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 460 | sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_STATIC); |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 461 | } |
drh | d654be8 | 2005-09-20 17:42:23 +0000 | [diff] [blame] | 462 | sqlite3VdbeJumpHere(v, addr); |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | /* |
| 466 | ** This routine generates VDBE code that causes the deletion of all |
| 467 | ** index entries associated with a single row of a single table. |
| 468 | ** |
| 469 | ** The VDBE must be in a particular state when this routine is called. |
| 470 | ** These are the requirements: |
| 471 | ** |
| 472 | ** 1. A read/write cursor pointing to pTab, the table containing the row |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 473 | ** to be deleted, must be opened as cursor number "iCur". |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 474 | ** |
| 475 | ** 2. Read/write cursors for all indices of pTab must be open as |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 476 | ** cursor number iCur+i for the i-th index. |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 477 | ** |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 478 | ** 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] | 479 | ** deleted. |
| 480 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 481 | void sqlite3GenerateRowIndexDelete( |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 482 | Parse *pParse, /* Parsing and code generating context */ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 483 | Table *pTab, /* Table containing the row to be deleted */ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 484 | int iCur, /* Cursor number for the table */ |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 485 | int *aRegIdx /* Only delete if aRegIdx!=0 && aRegIdx[i]>0 */ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 486 | ){ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 487 | int i; |
| 488 | Index *pIdx; |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 489 | int r1; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 490 | |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 491 | for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){ |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 492 | if( aRegIdx!=0 && aRegIdx[i-1]==0 ) continue; |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame^] | 493 | r1 = sqlite3GenerateIndexKey(pParse, pIdx, iCur, r1, 0); |
| 494 | sqlite3VdbeAddOp3(pParse->pVdbe, OP_IdxDelete, iCur+i, r1,pIdx->nColumn+1); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 495 | } |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 496 | } |
drh | 51846b5 | 2004-05-28 16:00:21 +0000 | [diff] [blame] | 497 | |
| 498 | /* |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame^] | 499 | ** Generate code that will assemble an index key and put it in register |
| 500 | ** regOut. The key with be for index pIdx which is an index on pTab. |
drh | 51846b5 | 2004-05-28 16:00:21 +0000 | [diff] [blame] | 501 | ** iCur is the index of a cursor open on the pTab table and pointing to |
| 502 | ** the entry that needs indexing. |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 503 | ** |
| 504 | ** Return a register number which is the first in a block of |
| 505 | ** registers that holds the elements of the index key. The |
| 506 | ** block of registers has already been deallocated by the time |
| 507 | ** this routine returns. |
drh | 51846b5 | 2004-05-28 16:00:21 +0000 | [diff] [blame] | 508 | */ |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 509 | int sqlite3GenerateIndexKey( |
| 510 | Parse *pParse, /* Parsing context */ |
drh | 51846b5 | 2004-05-28 16:00:21 +0000 | [diff] [blame] | 511 | Index *pIdx, /* The index for which to generate a key */ |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 512 | int iCur, /* Cursor number for the pIdx->pTable table */ |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame^] | 513 | int regOut, /* Write the new index key to this register */ |
| 514 | int doMakeRec /* Run the OP_MakeRecord instruction if true */ |
drh | 51846b5 | 2004-05-28 16:00:21 +0000 | [diff] [blame] | 515 | ){ |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 516 | Vdbe *v = pParse->pVdbe; |
drh | 51846b5 | 2004-05-28 16:00:21 +0000 | [diff] [blame] | 517 | int j; |
| 518 | Table *pTab = pIdx->pTable; |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 519 | int regBase; |
| 520 | int nCol; |
drh | 51846b5 | 2004-05-28 16:00:21 +0000 | [diff] [blame] | 521 | |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 522 | nCol = pIdx->nColumn; |
| 523 | regBase = sqlite3GetTempRange(pParse, nCol+1); |
| 524 | sqlite3VdbeAddOp2(v, OP_Rowid, iCur, regBase+nCol); |
| 525 | for(j=0; j<nCol; j++){ |
drh | 51846b5 | 2004-05-28 16:00:21 +0000 | [diff] [blame] | 526 | int idx = pIdx->aiColumn[j]; |
| 527 | if( idx==pTab->iPKey ){ |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 528 | sqlite3VdbeAddOp2(v, OP_SCopy, regBase+nCol, regBase+j); |
drh | 51846b5 | 2004-05-28 16:00:21 +0000 | [diff] [blame] | 529 | }else{ |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 530 | sqlite3VdbeAddOp3(v, OP_Column, iCur, idx, regBase+j); |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 531 | sqlite3ColumnDefault(v, pTab, idx); |
drh | 51846b5 | 2004-05-28 16:00:21 +0000 | [diff] [blame] | 532 | } |
| 533 | } |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame^] | 534 | if( doMakeRec ){ |
| 535 | sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol+1, regOut); |
| 536 | sqlite3IndexAffinityStr(v, pIdx); |
| 537 | } |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 538 | sqlite3ReleaseTempRange(pParse, regBase, nCol+1); |
| 539 | return regBase; |
drh | 51846b5 | 2004-05-28 16:00:21 +0000 | [diff] [blame] | 540 | } |
drh | f39d958 | 2008-03-19 20:42:13 +0000 | [diff] [blame] | 541 | |
| 542 | /* Make sure "isView" gets undefined in case this file becomes part of |
| 543 | ** the amalgamation - so that subsequent files do not see isView as a |
| 544 | ** macro. */ |
| 545 | #undef isView |