blob: 5654b6ccef12e487e351a8c3cbea52505b980778 [file] [log] [blame]
drhcce7d172000-05-31 15:34:51 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drhcce7d172000-05-31 15:34:51 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drhcce7d172000-05-31 15:34:51 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** 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.
drhcce7d172000-05-31 15:34:51 +000010**
11*************************************************************************
12** This file contains C code routines that are called by the parser
13** to handle DELETE FROM statements.
14**
drh8721ce42001-11-07 14:22:00 +000015** $Id: delete.c,v 1.20 2001/11/07 14:22:00 drh Exp $
drhcce7d172000-05-31 15:34:51 +000016*/
17#include "sqliteInt.h"
18
19/*
20** Process a DELETE FROM statement.
21*/
22void 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 */
drh4794b982000-06-06 13:54:14 +000034 int base; /* Index of the first available table cursor */
drhecdc7532001-09-23 02:35:53 +000035 sqlite *db; /* Main database structure */
drh1bee3d72001-10-15 00:44:35 +000036 int openOp; /* Opcode used to open a cursor to the table */
37
drhcce7d172000-05-31 15:34:51 +000038
drhdaffd0e2001-04-11 14:28:42 +000039 if( pParse->nErr || sqlite_malloc_failed ){
40 pTabList = 0;
41 goto delete_from_cleanup;
42 }
drhecdc7532001-09-23 02:35:53 +000043 db = pParse->db;
drhdaffd0e2001-04-11 14:28:42 +000044
drh1ccde152000-06-17 13:12:39 +000045 /* 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
drhcce7d172000-05-31 15:34:51 +000047 ** 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);
drhdaffd0e2001-04-11 14:28:42 +000051 if( pTabList==0 ) goto delete_from_cleanup;
drhcce7d172000-05-31 15:34:51 +000052 for(i=0; i<pTabList->nId; i++){
drhecdc7532001-09-23 02:35:53 +000053 pTabList->a[i].pTab = sqliteFindTable(db, pTabList->a[i].zName);
drhcce7d172000-05-31 15:34:51 +000054 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
drh967e8b72000-06-21 13:59:10 +000069 /* Resolve the column names in all the expressions.
drhcce7d172000-05-31 15:34:51 +000070 */
71 if( pWhere ){
drh4794b982000-06-06 13:54:14 +000072 sqliteExprResolveInSelect(pParse, pWhere);
drhcce7d172000-05-31 15:34:51 +000073 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 */
drhd8bc7082000-06-07 23:51:50 +000083 v = sqliteGetVdbe(pParse);
drhcce7d172000-05-31 15:34:51 +000084 if( v==0 ) goto delete_from_cleanup;
drhecdc7532001-09-23 02:35:53 +000085 if( (db->flags & SQLITE_InTrans)==0 ){
drh99fcd712001-10-13 01:06:47 +000086 sqliteVdbeAddOp(v, OP_Transaction, 0, 0);
87 sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0);
drhecdc7532001-09-23 02:35:53 +000088 pParse->schemaVerified = 1;
drh5e00f6c2001-09-13 13:46:56 +000089 }
90
drh1bee3d72001-10-15 00:44:35 +000091 /* 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 }
drhcce7d172000-05-31 15:34:51 +000097
drh0353ced2001-03-20 22:05:00 +000098 /* Special case: A DELETE without a WHERE clause deletes everything.
drhecdc7532001-09-23 02:35:53 +000099 ** It is easier just to erase the whole table.
drhcce7d172000-05-31 15:34:51 +0000100 */
drh0353ced2001-03-20 22:05:00 +0000101 if( pWhere==0 ){
drh1bee3d72001-10-15 00:44:35 +0000102 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 }
drh99fcd712001-10-13 01:06:47 +0000116 sqliteVdbeAddOp(v, OP_Clear, pTab->tnum, pTab->isTemp);
drh0353ced2001-03-20 22:05:00 +0000117 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh99fcd712001-10-13 01:06:47 +0000118 sqliteVdbeAddOp(v, OP_Clear, pIdx->tnum, pTab->isTemp);
drhcce7d172000-05-31 15:34:51 +0000119 }
120 }
drh0353ced2001-03-20 22:05:00 +0000121
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 */
drh0353ced2001-03-20 22:05:00 +0000128 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 */
drh99fcd712001-10-13 01:06:47 +0000133 sqliteVdbeAddOp(v, OP_ListWrite, 0, 0);
drh1bee3d72001-10-15 00:44:35 +0000134 if( db->flags & SQLITE_CountRows ){
135 sqliteVdbeAddOp(v, OP_AddImm, 1, 0);
136 }
drh0353ced2001-03-20 22:05:00 +0000137
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;
drh99fcd712001-10-13 01:06:47 +0000147 sqliteVdbeAddOp(v, OP_ListRewind, 0, 0);
drhf57b3392001-10-08 13:22:32 +0000148 openOp = pTab->isTemp ? OP_OpenWrAux : OP_OpenWrite;
drh99fcd712001-10-13 01:06:47 +0000149 sqliteVdbeAddOp(v, openOp, base, pTab->tnum);
drh0353ced2001-03-20 22:05:00 +0000150 for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
drh99fcd712001-10-13 01:06:47 +0000151 sqliteVdbeAddOp(v, openOp, base+i, pIdx->tnum);
drh0353ced2001-03-20 22:05:00 +0000152 }
153 end = sqliteVdbeMakeLabel(v);
drh99fcd712001-10-13 01:06:47 +0000154 addr = sqliteVdbeAddOp(v, OP_ListRead, 0, end);
155 sqliteVdbeAddOp(v, OP_MoveTo, base, 0);
drh0353ced2001-03-20 22:05:00 +0000156 if( pTab->pIndex ){
drh0353ced2001-03-20 22:05:00 +0000157 for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
158 int j;
drh99fcd712001-10-13 01:06:47 +0000159 sqliteVdbeAddOp(v, OP_Recno, base, 0);
drh0353ced2001-03-20 22:05:00 +0000160 for(j=0; j<pIdx->nColumn; j++){
drh99fcd712001-10-13 01:06:47 +0000161 sqliteVdbeAddOp(v, OP_Column, base, pIdx->aiColumn[j]);
drh0353ced2001-03-20 22:05:00 +0000162 }
drh99fcd712001-10-13 01:06:47 +0000163 sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0);
drh8721ce42001-11-07 14:22:00 +0000164 sqliteVdbeAddOp(v, OP_IdxDelete, base+i, 0);
drh0353ced2001-03-20 22:05:00 +0000165 }
166 }
drh99fcd712001-10-13 01:06:47 +0000167 sqliteVdbeAddOp(v, OP_Delete, base, 0);
168 sqliteVdbeAddOp(v, OP_Goto, 0, addr);
169 sqliteVdbeResolveLabel(v, end);
drha8b38d22001-11-01 14:41:34 +0000170 sqliteVdbeAddOp(v, OP_ListReset, 0, 0);
drh0353ced2001-03-20 22:05:00 +0000171 }
drhecdc7532001-09-23 02:35:53 +0000172 if( (db->flags & SQLITE_InTrans)==0 ){
drh99fcd712001-10-13 01:06:47 +0000173 sqliteVdbeAddOp(v, OP_Commit, 0, 0);
drh5e00f6c2001-09-13 13:46:56 +0000174 }
175
drh1bee3d72001-10-15 00:44:35 +0000176 /*
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 }
drhcce7d172000-05-31 15:34:51 +0000185
186delete_from_cleanup:
187 sqliteIdListDelete(pTabList);
188 sqliteExprDelete(pWhere);
189 return;
190}