2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A/*
2N/A** 2001 September 15
2N/A**
2N/A** The author disclaims copyright to this source code. In place of
2N/A** a legal notice, here is a blessing:
2N/A**
2N/A** May you do good and not evil.
2N/A** May you find forgiveness for yourself and forgive others.
2N/A** May you share freely, never taking more than you give.
2N/A**
2N/A*************************************************************************
2N/A** This file contains C code routines that are called by the parser
2N/A** to handle DELETE FROM statements.
2N/A**
2N/A** $Id: delete.c,v 1.61 2004/02/24 01:05:32 drh Exp $
2N/A*/
2N/A#include "sqliteInt.h"
2N/A
2N/A/*
2N/A** Look up every table that is named in pSrc. If any table is not found,
2N/A** add an error message to pParse->zErrMsg and return NULL. If all tables
2N/A** are found, return a pointer to the last table.
2N/A*/
2N/ATable *sqliteSrcListLookup(Parse *pParse, SrcList *pSrc){
2N/A Table *pTab = 0;
2N/A int i;
2N/A for(i=0; i<pSrc->nSrc; i++){
2N/A const char *zTab = pSrc->a[i].zName;
2N/A const char *zDb = pSrc->a[i].zDatabase;
2N/A pTab = sqliteLocateTable(pParse, zTab, zDb);
2N/A pSrc->a[i].pTab = pTab;
2N/A }
2N/A return pTab;
2N/A}
2N/A
2N/A/*
2N/A** Check to make sure the given table is writable. If it is not
2N/A** writable, generate an error message and return 1. If it is
2N/A** writable return 0;
2N/A*/
2N/Aint sqliteIsReadOnly(Parse *pParse, Table *pTab, int viewOk){
2N/A if( pTab->readOnly ){
2N/A sqliteErrorMsg(pParse, "table %s may not be modified", pTab->zName);
2N/A return 1;
2N/A }
2N/A if( !viewOk && pTab->pSelect ){
2N/A sqliteErrorMsg(pParse, "cannot modify %s because it is a view",pTab->zName);
2N/A return 1;
2N/A }
2N/A return 0;
2N/A}
2N/A
2N/A/*
2N/A** Process a DELETE FROM statement.
2N/A*/
2N/Avoid sqliteDeleteFrom(
2N/A Parse *pParse, /* The parser context */
2N/A SrcList *pTabList, /* The table from which we should delete things */
2N/A Expr *pWhere /* The WHERE clause. May be null */
2N/A){
2N/A Vdbe *v; /* The virtual database engine */
2N/A Table *pTab; /* The table from which records will be deleted */
2N/A const char *zDb; /* Name of database holding pTab */
2N/A int end, addr; /* A couple addresses of generated code */
2N/A int i; /* Loop counter */
2N/A WhereInfo *pWInfo; /* Information about the WHERE clause */
2N/A Index *pIdx; /* For looping over indices of the table */
2N/A int iCur; /* VDBE Cursor number for pTab */
2N/A sqlite *db; /* Main database structure */
2N/A int isView; /* True if attempting to delete from a view */
2N/A AuthContext sContext; /* Authorization context */
2N/A
2N/A int row_triggers_exist = 0; /* True if any triggers exist */
2N/A int before_triggers; /* True if there are BEFORE triggers */
2N/A int after_triggers; /* True if there are AFTER triggers */
2N/A int oldIdx = -1; /* Cursor for the OLD table of AFTER triggers */
2N/A
2N/A sContext.pParse = 0;
2N/A if( pParse->nErr || sqlite_malloc_failed ){
2N/A pTabList = 0;
2N/A goto delete_from_cleanup;
2N/A }
2N/A db = pParse->db;
2N/A assert( pTabList->nSrc==1 );
2N/A
2N/A /* Locate the table which we want to delete. This table has to be
2N/A ** put in an SrcList structure because some of the subroutines we
2N/A ** will be calling are designed to work with multiple tables and expect
2N/A ** an SrcList* parameter instead of just a Table* parameter.
2N/A */
2N/A pTab = sqliteSrcListLookup(pParse, pTabList);
2N/A if( pTab==0 ) goto delete_from_cleanup;
2N/A before_triggers = sqliteTriggersExist(pParse, pTab->pTrigger,
2N/A TK_DELETE, TK_BEFORE, TK_ROW, 0);
2N/A after_triggers = sqliteTriggersExist(pParse, pTab->pTrigger,
2N/A TK_DELETE, TK_AFTER, TK_ROW, 0);
2N/A row_triggers_exist = before_triggers || after_triggers;
2N/A isView = pTab->pSelect!=0;
2N/A if( sqliteIsReadOnly(pParse, pTab, before_triggers) ){
2N/A goto delete_from_cleanup;
2N/A }
2N/A assert( pTab->iDb<db->nDb );
2N/A zDb = db->aDb[pTab->iDb].zName;
2N/A if( sqliteAuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
2N/A goto delete_from_cleanup;
2N/A }
2N/A
2N/A /* If pTab is really a view, make sure it has been initialized.
2N/A */
2N/A if( isView && sqliteViewGetColumnNames(pParse, pTab) ){
2N/A goto delete_from_cleanup;
2N/A }
2N/A
2N/A /* Allocate a cursor used to store the old.* data for a trigger.
2N/A */
2N/A if( row_triggers_exist ){
2N/A oldIdx = pParse->nTab++;
2N/A }
2N/A
2N/A /* Resolve the column names in all the expressions.
2N/A */
2N/A assert( pTabList->nSrc==1 );
2N/A iCur = pTabList->a[0].iCursor = pParse->nTab++;
2N/A if( pWhere ){
2N/A if( sqliteExprResolveIds(pParse, pTabList, 0, pWhere) ){
2N/A goto delete_from_cleanup;
2N/A }
2N/A if( sqliteExprCheck(pParse, pWhere, 0, 0) ){
2N/A goto delete_from_cleanup;
2N/A }
2N/A }
2N/A
2N/A /* Start the view context
2N/A */
2N/A if( isView ){
2N/A sqliteAuthContextPush(pParse, &sContext, pTab->zName);
2N/A }
2N/A
2N/A /* Begin generating code.
2N/A */
2N/A v = sqliteGetVdbe(pParse);
2N/A if( v==0 ){
2N/A goto delete_from_cleanup;
2N/A }
2N/A sqliteBeginWriteOperation(pParse, row_triggers_exist, pTab->iDb);
2N/A
2N/A /* If we are trying to delete from a view, construct that view into
2N/A ** a temporary table.
2N/A */
2N/A if( isView ){
2N/A Select *pView = sqliteSelectDup(pTab->pSelect);
2N/A sqliteSelect(pParse, pView, SRT_TempTable, iCur, 0, 0, 0);
2N/A sqliteSelectDelete(pView);
2N/A }
2N/A
2N/A /* Initialize the counter of the number of rows deleted, if
2N/A ** we are counting rows.
2N/A */
2N/A if( db->flags & SQLITE_CountRows ){
2N/A sqliteVdbeAddOp(v, OP_Integer, 0, 0);
2N/A }
2N/A
2N/A /* Special case: A DELETE without a WHERE clause deletes everything.
2N/A ** It is easier just to erase the whole table. Note, however, that
2N/A ** this means that the row change count will be incorrect.
2N/A */
2N/A if( pWhere==0 && !row_triggers_exist ){
2N/A if( db->flags & SQLITE_CountRows ){
2N/A /* If counting rows deleted, just count the total number of
2N/A ** entries in the table. */
2N/A int endOfLoop = sqliteVdbeMakeLabel(v);
2N/A int addr;
2N/A if( !isView ){
2N/A sqliteVdbeAddOp(v, OP_Integer, pTab->iDb, 0);
2N/A sqliteVdbeAddOp(v, OP_OpenRead, iCur, pTab->tnum);
2N/A }
2N/A sqliteVdbeAddOp(v, OP_Rewind, iCur, sqliteVdbeCurrentAddr(v)+2);
2N/A addr = sqliteVdbeAddOp(v, OP_AddImm, 1, 0);
2N/A sqliteVdbeAddOp(v, OP_Next, iCur, addr);
2N/A sqliteVdbeResolveLabel(v, endOfLoop);
2N/A sqliteVdbeAddOp(v, OP_Close, iCur, 0);
2N/A }
2N/A if( !isView ){
2N/A sqliteVdbeAddOp(v, OP_Clear, pTab->tnum, pTab->iDb);
2N/A for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
2N/A sqliteVdbeAddOp(v, OP_Clear, pIdx->tnum, pIdx->iDb);
2N/A }
2N/A }
2N/A }
2N/A
2N/A /* The usual case: There is a WHERE clause so we have to scan through
2N/A ** the table and pick which records to delete.
2N/A */
2N/A else{
2N/A /* Begin the database scan
2N/A */
2N/A pWInfo = sqliteWhereBegin(pParse, pTabList, pWhere, 1, 0);
2N/A if( pWInfo==0 ) goto delete_from_cleanup;
2N/A
2N/A /* Remember the key of every item to be deleted.
2N/A */
2N/A sqliteVdbeAddOp(v, OP_ListWrite, 0, 0);
2N/A if( db->flags & SQLITE_CountRows ){
2N/A sqliteVdbeAddOp(v, OP_AddImm, 1, 0);
2N/A }
2N/A
2N/A /* End the database scan loop.
2N/A */
2N/A sqliteWhereEnd(pWInfo);
2N/A
2N/A /* Open the pseudo-table used to store OLD if there are triggers.
2N/A */
2N/A if( row_triggers_exist ){
2N/A sqliteVdbeAddOp(v, OP_OpenPseudo, oldIdx, 0);
2N/A }
2N/A
2N/A /* Delete every item whose key was written to the list during the
2N/A ** database scan. We have to delete items after the scan is complete
2N/A ** because deleting an item can change the scan order.
2N/A */
2N/A sqliteVdbeAddOp(v, OP_ListRewind, 0, 0);
2N/A end = sqliteVdbeMakeLabel(v);
2N/A
2N/A /* This is the beginning of the delete loop when there are
2N/A ** row triggers.
2N/A */
2N/A if( row_triggers_exist ){
2N/A addr = sqliteVdbeAddOp(v, OP_ListRead, 0, end);
2N/A sqliteVdbeAddOp(v, OP_Dup, 0, 0);
2N/A if( !isView ){
2N/A sqliteVdbeAddOp(v, OP_Integer, pTab->iDb, 0);
2N/A sqliteVdbeAddOp(v, OP_OpenRead, iCur, pTab->tnum);
2N/A }
2N/A sqliteVdbeAddOp(v, OP_MoveTo, iCur, 0);
2N/A
2N/A sqliteVdbeAddOp(v, OP_Recno, iCur, 0);
2N/A sqliteVdbeAddOp(v, OP_RowData, iCur, 0);
2N/A sqliteVdbeAddOp(v, OP_PutIntKey, oldIdx, 0);
2N/A if( !isView ){
2N/A sqliteVdbeAddOp(v, OP_Close, iCur, 0);
2N/A }
2N/A
2N/A sqliteCodeRowTrigger(pParse, TK_DELETE, 0, TK_BEFORE, pTab, -1,
2N/A oldIdx, (pParse->trigStack)?pParse->trigStack->orconf:OE_Default,
2N/A addr);
2N/A }
2N/A
2N/A if( !isView ){
2N/A /* Open cursors for the table we are deleting from and all its
2N/A ** indices. If there are row triggers, this happens inside the
2N/A ** OP_ListRead loop because the cursor have to all be closed
2N/A ** before the trigger fires. If there are no row triggers, the
2N/A ** cursors are opened only once on the outside the loop.
2N/A */
2N/A pParse->nTab = iCur + 1;
2N/A sqliteOpenTableAndIndices(pParse, pTab, iCur);
2N/A
2N/A /* This is the beginning of the delete loop when there are no
2N/A ** row triggers */
2N/A if( !row_triggers_exist ){
2N/A addr = sqliteVdbeAddOp(v, OP_ListRead, 0, end);
2N/A }
2N/A
2N/A /* Delete the row */
2N/A sqliteGenerateRowDelete(db, v, pTab, iCur, pParse->trigStack==0);
2N/A }
2N/A
2N/A /* If there are row triggers, close all cursors then invoke
2N/A ** the AFTER triggers
2N/A */
2N/A if( row_triggers_exist ){
2N/A if( !isView ){
2N/A for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
2N/A sqliteVdbeAddOp(v, OP_Close, iCur + i, pIdx->tnum);
2N/A }
2N/A sqliteVdbeAddOp(v, OP_Close, iCur, 0);
2N/A }
2N/A sqliteCodeRowTrigger(pParse, TK_DELETE, 0, TK_AFTER, pTab, -1,
2N/A oldIdx, (pParse->trigStack)?pParse->trigStack->orconf:OE_Default,
2N/A addr);
2N/A }
2N/A
2N/A /* End of the delete loop */
2N/A sqliteVdbeAddOp(v, OP_Goto, 0, addr);
2N/A sqliteVdbeResolveLabel(v, end);
2N/A sqliteVdbeAddOp(v, OP_ListReset, 0, 0);
2N/A
2N/A /* Close the cursors after the loop if there are no row triggers */
2N/A if( !row_triggers_exist ){
2N/A for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
2N/A sqliteVdbeAddOp(v, OP_Close, iCur + i, pIdx->tnum);
2N/A }
2N/A sqliteVdbeAddOp(v, OP_Close, iCur, 0);
2N/A pParse->nTab = iCur;
2N/A }
2N/A }
2N/A sqliteVdbeAddOp(v, OP_SetCounts, 0, 0);
2N/A sqliteEndWriteOperation(pParse);
2N/A
2N/A /*
2N/A ** Return the number of rows that were deleted.
2N/A */
2N/A if( db->flags & SQLITE_CountRows ){
2N/A sqliteVdbeAddOp(v, OP_ColumnName, 0, 1);
2N/A sqliteVdbeChangeP3(v, -1, "rows deleted", P3_STATIC);
2N/A sqliteVdbeAddOp(v, OP_Callback, 1, 0);
2N/A }
2N/A
2N/Adelete_from_cleanup:
2N/A sqliteAuthContextPop(&sContext);
2N/A sqliteSrcListDelete(pTabList);
2N/A sqliteExprDelete(pWhere);
2N/A return;
2N/A}
2N/A
2N/A/*
2N/A** This routine generates VDBE code that causes a single row of a
2N/A** single table to be deleted.
2N/A**
2N/A** The VDBE must be in a particular state when this routine is called.
2N/A** These are the requirements:
2N/A**
2N/A** 1. A read/write cursor pointing to pTab, the table containing the row
2N/A** to be deleted, must be opened as cursor number "base".
2N/A**
2N/A** 2. Read/write cursors for all indices of pTab must be open as
2N/A** cursor number base+i for the i-th index.
2N/A**
2N/A** 3. The record number of the row to be deleted must be on the top
2N/A** of the stack.
2N/A**
2N/A** This routine pops the top of the stack to remove the record number
2N/A** and then generates code to remove both the table record and all index
2N/A** entries that point to that record.
2N/A*/
2N/Avoid sqliteGenerateRowDelete(
2N/A sqlite *db, /* The database containing the index */
2N/A Vdbe *v, /* Generate code into this VDBE */
2N/A Table *pTab, /* Table containing the row to be deleted */
2N/A int iCur, /* Cursor number for the table */
2N/A int count /* Increment the row change counter */
2N/A){
2N/A int addr;
2N/A addr = sqliteVdbeAddOp(v, OP_NotExists, iCur, 0);
2N/A sqliteGenerateRowIndexDelete(db, v, pTab, iCur, 0);
2N/A sqliteVdbeAddOp(v, OP_Delete, iCur,
2N/A (count?OPFLAG_NCHANGE:0) | OPFLAG_CSCHANGE);
2N/A sqliteVdbeChangeP2(v, addr, sqliteVdbeCurrentAddr(v));
2N/A}
2N/A
2N/A/*
2N/A** This routine generates VDBE code that causes the deletion of all
2N/A** index entries associated with a single row of a single table.
2N/A**
2N/A** The VDBE must be in a particular state when this routine is called.
2N/A** These are the requirements:
2N/A**
2N/A** 1. A read/write cursor pointing to pTab, the table containing the row
2N/A** to be deleted, must be opened as cursor number "iCur".
2N/A**
2N/A** 2. Read/write cursors for all indices of pTab must be open as
2N/A** cursor number iCur+i for the i-th index.
2N/A**
2N/A** 3. The "iCur" cursor must be pointing to the row that is to be
2N/A** deleted.
2N/A*/
2N/Avoid sqliteGenerateRowIndexDelete(
2N/A sqlite *db, /* The database containing the index */
2N/A Vdbe *v, /* Generate code into this VDBE */
2N/A Table *pTab, /* Table containing the row to be deleted */
2N/A int iCur, /* Cursor number for the table */
2N/A char *aIdxUsed /* Only delete if aIdxUsed!=0 && aIdxUsed[i]!=0 */
2N/A){
2N/A int i;
2N/A Index *pIdx;
2N/A
2N/A for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
2N/A int j;
2N/A if( aIdxUsed!=0 && aIdxUsed[i-1]==0 ) continue;
2N/A sqliteVdbeAddOp(v, OP_Recno, iCur, 0);
2N/A for(j=0; j<pIdx->nColumn; j++){
2N/A int idx = pIdx->aiColumn[j];
2N/A if( idx==pTab->iPKey ){
2N/A sqliteVdbeAddOp(v, OP_Dup, j, 0);
2N/A }else{
2N/A sqliteVdbeAddOp(v, OP_Column, iCur, idx);
2N/A }
2N/A }
2N/A sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0);
2N/A if( db->file_format>=4 ) sqliteAddIdxKeyType(v, pIdx);
2N/A sqliteVdbeAddOp(v, OP_IdxDelete, iCur+i, 0);
2N/A }
2N/A}