drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** Copyright (c) 1999, 2000 D. Richard Hipp |
| 3 | ** |
| 4 | ** This program is free software; you can redistribute it and/or |
| 5 | ** modify it under the terms of the GNU General Public |
| 6 | ** License as published by the Free Software Foundation; either |
| 7 | ** version 2 of the License, or (at your option) any later version. |
| 8 | ** |
| 9 | ** This program is distributed in the hope that it will be useful, |
| 10 | ** but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | ** General Public License for more details. |
| 13 | ** |
| 14 | ** You should have received a copy of the GNU General Public |
| 15 | ** License along with this library; if not, write to the |
| 16 | ** Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 17 | ** Boston, MA 02111-1307, USA. |
| 18 | ** |
| 19 | ** Author contact information: |
| 20 | ** drh@hwaci.com |
| 21 | ** http://www.hwaci.com/drh/ |
| 22 | ** |
| 23 | ************************************************************************* |
| 24 | ** This file contains C code routines that are called by the parser |
| 25 | ** to handle DELETE FROM statements. |
| 26 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame^] | 27 | ** $Id: delete.c,v 1.10 2001/09/13 13:46:56 drh Exp $ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 28 | */ |
| 29 | #include "sqliteInt.h" |
| 30 | |
| 31 | /* |
| 32 | ** Process a DELETE FROM statement. |
| 33 | */ |
| 34 | void sqliteDeleteFrom( |
| 35 | Parse *pParse, /* The parser context */ |
| 36 | Token *pTableName, /* The table from which we should delete things */ |
| 37 | Expr *pWhere /* The WHERE clause. May be null */ |
| 38 | ){ |
| 39 | Vdbe *v; /* The virtual database engine */ |
| 40 | Table *pTab; /* The table from which records will be deleted */ |
| 41 | IdList *pTabList; /* An ID list holding pTab and nothing else */ |
| 42 | int end, addr; /* A couple addresses of generated code */ |
| 43 | int i; /* Loop counter */ |
| 44 | WhereInfo *pWInfo; /* Information about the WHERE clause */ |
| 45 | Index *pIdx; /* For looping over indices of the table */ |
drh | 4794b98 | 2000-06-06 13:54:14 +0000 | [diff] [blame] | 46 | int base; /* Index of the first available table cursor */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 47 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 48 | if( pParse->nErr || sqlite_malloc_failed ){ |
| 49 | pTabList = 0; |
| 50 | goto delete_from_cleanup; |
| 51 | } |
| 52 | |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 53 | /* Locate the table which we want to delete. This table has to be |
| 54 | ** put in an IdList structure because some of the subroutines we |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 55 | ** will be calling are designed to work with multiple tables and expect |
| 56 | ** an IdList* parameter instead of just a Table* parameger. |
| 57 | */ |
| 58 | pTabList = sqliteIdListAppend(0, pTableName); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 59 | if( pTabList==0 ) goto delete_from_cleanup; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 60 | for(i=0; i<pTabList->nId; i++){ |
| 61 | pTabList->a[i].pTab = sqliteFindTable(pParse->db, pTabList->a[i].zName); |
| 62 | if( pTabList->a[i].pTab==0 ){ |
| 63 | sqliteSetString(&pParse->zErrMsg, "no such table: ", |
| 64 | pTabList->a[i].zName, 0); |
| 65 | pParse->nErr++; |
| 66 | goto delete_from_cleanup; |
| 67 | } |
| 68 | if( pTabList->a[i].pTab->readOnly ){ |
| 69 | sqliteSetString(&pParse->zErrMsg, "table ", pTabList->a[i].zName, |
| 70 | " may not be modified", 0); |
| 71 | pParse->nErr++; |
| 72 | goto delete_from_cleanup; |
| 73 | } |
| 74 | } |
| 75 | pTab = pTabList->a[0].pTab; |
| 76 | |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 77 | /* Resolve the column names in all the expressions. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 78 | */ |
| 79 | if( pWhere ){ |
drh | 4794b98 | 2000-06-06 13:54:14 +0000 | [diff] [blame] | 80 | sqliteExprResolveInSelect(pParse, pWhere); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 81 | if( sqliteExprResolveIds(pParse, pTabList, pWhere) ){ |
| 82 | goto delete_from_cleanup; |
| 83 | } |
| 84 | if( sqliteExprCheck(pParse, pWhere, 0, 0) ){ |
| 85 | goto delete_from_cleanup; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /* Begin generating code. |
| 90 | */ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 91 | v = sqliteGetVdbe(pParse); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 92 | if( v==0 ) goto delete_from_cleanup; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame^] | 93 | if( (pParse->db->flags & SQLITE_InTrans)==0 ){ |
| 94 | sqliteVdbeAddOp(v, OP_Transaction, 0, 0, 0, 0); |
| 95 | } |
| 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. |
| 99 | ** It is easier just to deleted the database files directly. |
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 | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame^] | 102 | sqliteVdbeAddOp(v, OP_Destroy, pTab->tnum, 0, 0, 0); |
drh | 0353ced | 2001-03-20 22:05:00 +0000 | [diff] [blame] | 103 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame^] | 104 | sqliteVdbeAddOp(v, OP_Destroy, pIdx->tnum, 0, 0, 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 105 | } |
| 106 | } |
drh | 0353ced | 2001-03-20 22:05:00 +0000 | [diff] [blame] | 107 | |
| 108 | /* The usual case: There is a WHERE clause so we have to scan through |
| 109 | ** the table an pick which records to delete. |
| 110 | */ |
| 111 | else{ |
| 112 | /* Begin the database scan |
| 113 | */ |
| 114 | sqliteVdbeAddOp(v, OP_ListOpen, 0, 0, 0, 0); |
| 115 | pWInfo = sqliteWhereBegin(pParse, pTabList, pWhere, 1); |
| 116 | if( pWInfo==0 ) goto delete_from_cleanup; |
| 117 | |
| 118 | /* Remember the key of every item to be deleted. |
| 119 | */ |
| 120 | sqliteVdbeAddOp(v, OP_ListWrite, 0, 0, 0, 0); |
| 121 | |
| 122 | /* End the database scan loop. |
| 123 | */ |
| 124 | sqliteWhereEnd(pWInfo); |
| 125 | |
| 126 | /* Delete every item whose key was written to the list during the |
| 127 | ** database scan. We have to delete items after the scan is complete |
| 128 | ** because deleting an item can change the scan order. |
| 129 | */ |
| 130 | base = pParse->nTab; |
| 131 | sqliteVdbeAddOp(v, OP_ListRewind, 0, 0, 0, 0); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame^] | 132 | sqliteVdbeAddOp(v, OP_Open, base, pTab->tnum, 0, 0); |
drh | 0353ced | 2001-03-20 22:05:00 +0000 | [diff] [blame] | 133 | for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame^] | 134 | sqliteVdbeAddOp(v, OP_Open, base+i, pIdx->tnum, 0, 0); |
drh | 0353ced | 2001-03-20 22:05:00 +0000 | [diff] [blame] | 135 | } |
| 136 | end = sqliteVdbeMakeLabel(v); |
| 137 | addr = sqliteVdbeAddOp(v, OP_ListRead, 0, end, 0, 0); |
| 138 | if( pTab->pIndex ){ |
| 139 | sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0); |
| 140 | sqliteVdbeAddOp(v, OP_Fetch, base, 0, 0, 0); |
| 141 | for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){ |
| 142 | int j; |
| 143 | sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0); |
| 144 | for(j=0; j<pIdx->nColumn; j++){ |
| 145 | sqliteVdbeAddOp(v, OP_Field, base, pIdx->aiColumn[j], 0, 0); |
| 146 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame^] | 147 | sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0, 0, 0); |
drh | 0353ced | 2001-03-20 22:05:00 +0000 | [diff] [blame] | 148 | sqliteVdbeAddOp(v, OP_DeleteIdx, base+i, 0, 0, 0); |
| 149 | } |
| 150 | } |
| 151 | sqliteVdbeAddOp(v, OP_Delete, base, 0, 0, 0); |
| 152 | sqliteVdbeAddOp(v, OP_Goto, 0, addr, 0, 0); |
| 153 | sqliteVdbeAddOp(v, OP_ListClose, 0, 0, 0, end); |
| 154 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame^] | 155 | if( (pParse->db->flags & SQLITE_InTrans)==0 ){ |
| 156 | sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0); |
| 157 | } |
| 158 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 159 | |
| 160 | delete_from_cleanup: |
| 161 | sqliteIdListDelete(pTabList); |
| 162 | sqliteExprDelete(pWhere); |
| 163 | return; |
| 164 | } |