drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2 | ** 2001 September 15 |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
| 12 | ** This file contains C code routines that are called by the parser |
| 13 | ** to handle DELETE FROM statements. |
| 14 | ** |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame^] | 15 | ** $Id: delete.c,v 1.20 2001/11/07 14:22:00 drh Exp $ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 16 | */ |
| 17 | #include "sqliteInt.h" |
| 18 | |
| 19 | /* |
| 20 | ** Process a DELETE FROM statement. |
| 21 | */ |
| 22 | void sqliteDeleteFrom( |
| 23 | Parse *pParse, /* The parser context */ |
| 24 | Token *pTableName, /* The table from which we should delete things */ |
| 25 | Expr *pWhere /* The WHERE clause. May be null */ |
| 26 | ){ |
| 27 | Vdbe *v; /* The virtual database engine */ |
| 28 | Table *pTab; /* The table from which records will be deleted */ |
| 29 | IdList *pTabList; /* An ID list holding pTab and nothing else */ |
| 30 | int end, addr; /* A couple addresses of generated code */ |
| 31 | int i; /* Loop counter */ |
| 32 | WhereInfo *pWInfo; /* Information about the WHERE clause */ |
| 33 | Index *pIdx; /* For looping over indices of the table */ |
drh | 4794b98 | 2000-06-06 13:54:14 +0000 | [diff] [blame] | 34 | int base; /* Index of the first available table cursor */ |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 35 | sqlite *db; /* Main database structure */ |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 36 | int openOp; /* Opcode used to open a cursor to the table */ |
| 37 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 38 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 39 | if( pParse->nErr || sqlite_malloc_failed ){ |
| 40 | pTabList = 0; |
| 41 | goto delete_from_cleanup; |
| 42 | } |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 43 | db = pParse->db; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 44 | |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 45 | /* Locate the table which we want to delete. This table has to be |
| 46 | ** put in an IdList structure because some of the subroutines we |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 47 | ** will be calling are designed to work with multiple tables and expect |
| 48 | ** an IdList* parameter instead of just a Table* parameger. |
| 49 | */ |
| 50 | pTabList = sqliteIdListAppend(0, pTableName); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 51 | if( pTabList==0 ) goto delete_from_cleanup; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 52 | for(i=0; i<pTabList->nId; i++){ |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 53 | pTabList->a[i].pTab = sqliteFindTable(db, pTabList->a[i].zName); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 54 | if( pTabList->a[i].pTab==0 ){ |
| 55 | sqliteSetString(&pParse->zErrMsg, "no such table: ", |
| 56 | pTabList->a[i].zName, 0); |
| 57 | pParse->nErr++; |
| 58 | goto delete_from_cleanup; |
| 59 | } |
| 60 | if( pTabList->a[i].pTab->readOnly ){ |
| 61 | sqliteSetString(&pParse->zErrMsg, "table ", pTabList->a[i].zName, |
| 62 | " may not be modified", 0); |
| 63 | pParse->nErr++; |
| 64 | goto delete_from_cleanup; |
| 65 | } |
| 66 | } |
| 67 | pTab = pTabList->a[0].pTab; |
| 68 | |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 69 | /* Resolve the column names in all the expressions. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 70 | */ |
| 71 | if( pWhere ){ |
drh | 4794b98 | 2000-06-06 13:54:14 +0000 | [diff] [blame] | 72 | sqliteExprResolveInSelect(pParse, pWhere); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 73 | if( sqliteExprResolveIds(pParse, pTabList, pWhere) ){ |
| 74 | goto delete_from_cleanup; |
| 75 | } |
| 76 | if( sqliteExprCheck(pParse, pWhere, 0, 0) ){ |
| 77 | goto delete_from_cleanup; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /* Begin generating code. |
| 82 | */ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 83 | v = sqliteGetVdbe(pParse); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 84 | if( v==0 ) goto delete_from_cleanup; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 85 | if( (db->flags & SQLITE_InTrans)==0 ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 86 | sqliteVdbeAddOp(v, OP_Transaction, 0, 0); |
| 87 | sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 88 | pParse->schemaVerified = 1; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 89 | } |
| 90 | |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 91 | /* Initialize the counter of the number of rows deleted, if |
| 92 | ** we are counting rows. |
| 93 | */ |
| 94 | if( db->flags & SQLITE_CountRows ){ |
| 95 | sqliteVdbeAddOp(v, OP_Integer, 0, 0); |
| 96 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 97 | |
drh | 0353ced | 2001-03-20 22:05:00 +0000 | [diff] [blame] | 98 | /* Special case: A DELETE without a WHERE clause deletes everything. |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 99 | ** It is easier just to erase the whole table. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 100 | */ |
drh | 0353ced | 2001-03-20 22:05:00 +0000 | [diff] [blame] | 101 | if( pWhere==0 ){ |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 102 | if( db->flags & SQLITE_CountRows ){ |
| 103 | /* If counting rows deleted, just count the total number of |
| 104 | ** entries in the table. */ |
| 105 | int endOfLoop = sqliteVdbeMakeLabel(v); |
| 106 | int addr; |
| 107 | openOp = pTab->isTemp ? OP_OpenAux : OP_Open; |
| 108 | sqliteVdbeAddOp(v, openOp, 0, pTab->tnum); |
| 109 | sqliteVdbeAddOp(v, OP_Rewind, 0, 0); |
| 110 | addr = sqliteVdbeAddOp(v, OP_Next, 0, endOfLoop); |
| 111 | sqliteVdbeAddOp(v, OP_AddImm, 1, 0); |
| 112 | sqliteVdbeAddOp(v, OP_Goto, 0, addr); |
| 113 | sqliteVdbeResolveLabel(v, endOfLoop); |
| 114 | sqliteVdbeAddOp(v, OP_Close, 0, 0); |
| 115 | } |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 116 | sqliteVdbeAddOp(v, OP_Clear, pTab->tnum, pTab->isTemp); |
drh | 0353ced | 2001-03-20 22:05:00 +0000 | [diff] [blame] | 117 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 118 | sqliteVdbeAddOp(v, OP_Clear, pIdx->tnum, pTab->isTemp); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 119 | } |
| 120 | } |
drh | 0353ced | 2001-03-20 22:05:00 +0000 | [diff] [blame] | 121 | |
| 122 | /* The usual case: There is a WHERE clause so we have to scan through |
| 123 | ** the table an pick which records to delete. |
| 124 | */ |
| 125 | else{ |
| 126 | /* Begin the database scan |
| 127 | */ |
drh | 0353ced | 2001-03-20 22:05:00 +0000 | [diff] [blame] | 128 | pWInfo = sqliteWhereBegin(pParse, pTabList, pWhere, 1); |
| 129 | if( pWInfo==0 ) goto delete_from_cleanup; |
| 130 | |
| 131 | /* Remember the key of every item to be deleted. |
| 132 | */ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 133 | sqliteVdbeAddOp(v, OP_ListWrite, 0, 0); |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 134 | if( db->flags & SQLITE_CountRows ){ |
| 135 | sqliteVdbeAddOp(v, OP_AddImm, 1, 0); |
| 136 | } |
drh | 0353ced | 2001-03-20 22:05:00 +0000 | [diff] [blame] | 137 | |
| 138 | /* End the database scan loop. |
| 139 | */ |
| 140 | sqliteWhereEnd(pWInfo); |
| 141 | |
| 142 | /* Delete every item whose key was written to the list during the |
| 143 | ** database scan. We have to delete items after the scan is complete |
| 144 | ** because deleting an item can change the scan order. |
| 145 | */ |
| 146 | base = pParse->nTab; |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 147 | sqliteVdbeAddOp(v, OP_ListRewind, 0, 0); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 148 | openOp = pTab->isTemp ? OP_OpenWrAux : OP_OpenWrite; |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 149 | sqliteVdbeAddOp(v, openOp, base, pTab->tnum); |
drh | 0353ced | 2001-03-20 22:05:00 +0000 | [diff] [blame] | 150 | for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 151 | sqliteVdbeAddOp(v, openOp, base+i, pIdx->tnum); |
drh | 0353ced | 2001-03-20 22:05:00 +0000 | [diff] [blame] | 152 | } |
| 153 | end = sqliteVdbeMakeLabel(v); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 154 | addr = sqliteVdbeAddOp(v, OP_ListRead, 0, end); |
| 155 | sqliteVdbeAddOp(v, OP_MoveTo, base, 0); |
drh | 0353ced | 2001-03-20 22:05:00 +0000 | [diff] [blame] | 156 | if( pTab->pIndex ){ |
drh | 0353ced | 2001-03-20 22:05:00 +0000 | [diff] [blame] | 157 | for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){ |
| 158 | int j; |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 159 | sqliteVdbeAddOp(v, OP_Recno, base, 0); |
drh | 0353ced | 2001-03-20 22:05:00 +0000 | [diff] [blame] | 160 | for(j=0; j<pIdx->nColumn; j++){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 161 | sqliteVdbeAddOp(v, OP_Column, base, pIdx->aiColumn[j]); |
drh | 0353ced | 2001-03-20 22:05:00 +0000 | [diff] [blame] | 162 | } |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 163 | sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0); |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame^] | 164 | sqliteVdbeAddOp(v, OP_IdxDelete, base+i, 0); |
drh | 0353ced | 2001-03-20 22:05:00 +0000 | [diff] [blame] | 165 | } |
| 166 | } |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 167 | sqliteVdbeAddOp(v, OP_Delete, base, 0); |
| 168 | sqliteVdbeAddOp(v, OP_Goto, 0, addr); |
| 169 | sqliteVdbeResolveLabel(v, end); |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 170 | sqliteVdbeAddOp(v, OP_ListReset, 0, 0); |
drh | 0353ced | 2001-03-20 22:05:00 +0000 | [diff] [blame] | 171 | } |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 172 | if( (db->flags & SQLITE_InTrans)==0 ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 173 | sqliteVdbeAddOp(v, OP_Commit, 0, 0); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 174 | } |
| 175 | |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 176 | /* |
| 177 | ** Return the number of rows that were deleted. |
| 178 | */ |
| 179 | if( db->flags & SQLITE_CountRows ){ |
| 180 | sqliteVdbeAddOp(v, OP_ColumnCount, 1, 0); |
| 181 | sqliteVdbeAddOp(v, OP_ColumnName, 0, 0); |
| 182 | sqliteVdbeChangeP3(v, -1, "rows deleted", P3_STATIC); |
| 183 | sqliteVdbeAddOp(v, OP_Callback, 1, 0); |
| 184 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 185 | |
| 186 | delete_from_cleanup: |
| 187 | sqliteIdListDelete(pTabList); |
| 188 | sqliteExprDelete(pWhere); |
| 189 | return; |
| 190 | } |