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