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