#include "sqliteInt.h"
Table *sqliteSrcListLookup(Parse *pParse, SrcList *pSrc){
Table *pTab = 0;
int i;
for(i=0; i<pSrc->nSrc; i++){
const char *zTab = pSrc->a[i].zName;
const char *zDb = pSrc->a[i].zDatabase;
pTab = sqliteLocateTable(pParse, zTab, zDb);
pSrc->a[i].pTab = pTab;
}
return pTab;
}
int sqliteIsReadOnly(Parse *pParse, Table *pTab, int viewOk){
if( pTab->readOnly ){
sqliteErrorMsg(pParse, "table %s may not be modified", pTab->zName);
return 1;
}
if( !viewOk && pTab->pSelect ){
sqliteErrorMsg(pParse, "cannot modify %s because it is a view",pTab->zName);
return 1;
}
return 0;
}
void sqliteDeleteFrom(
Parse *pParse,
SrcList *pTabList,
Expr *pWhere
){
Vdbe *v;
Table *pTab;
const char *zDb;
int end, addr;
int i;
WhereInfo *pWInfo;
Index *pIdx;
int iCur;
sqlite *db;
int isView;
AuthContext sContext;
int row_triggers_exist = 0;
int before_triggers;
int after_triggers;
int oldIdx = -1;
sContext.pParse = 0;
if( pParse->nErr || sqlite_malloc_failed ){
pTabList = 0;
goto delete_from_cleanup;
}
db = pParse->db;
assert( pTabList->nSrc==1 );
pTab = sqliteSrcListLookup(pParse, pTabList);
if( pTab==0 ) goto delete_from_cleanup;
before_triggers = sqliteTriggersExist(pParse, pTab->pTrigger,
TK_DELETE, TK_BEFORE, TK_ROW, 0);
after_triggers = sqliteTriggersExist(pParse, pTab->pTrigger,
TK_DELETE, TK_AFTER, TK_ROW, 0);
row_triggers_exist = before_triggers || after_triggers;
isView = pTab->pSelect!=0;
if( sqliteIsReadOnly(pParse, pTab, before_triggers) ){
goto delete_from_cleanup;
}
assert( pTab->iDb<db->nDb );
zDb = db->aDb[pTab->iDb].zName;
if( sqliteAuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
goto delete_from_cleanup;
}
if( isView && sqliteViewGetColumnNames(pParse, pTab) ){
goto delete_from_cleanup;
}
if( row_triggers_exist ){
oldIdx = pParse->nTab++;
}
assert( pTabList->nSrc==1 );
iCur = pTabList->a[0].iCursor = pParse->nTab++;
if( pWhere ){
if( sqliteExprResolveIds(pParse, pTabList, 0, pWhere) ){
goto delete_from_cleanup;
}
if( sqliteExprCheck(pParse, pWhere, 0, 0) ){
goto delete_from_cleanup;
}
}
if( isView ){
sqliteAuthContextPush(pParse, &sContext, pTab->zName);
}
v = sqliteGetVdbe(pParse);
if( v==0 ){
goto delete_from_cleanup;
}
sqliteBeginWriteOperation(pParse, row_triggers_exist, pTab->iDb);
if( isView ){
Select *pView = sqliteSelectDup(pTab->pSelect);
sqliteSelect(pParse, pView, SRT_TempTable, iCur, 0, 0, 0);
sqliteSelectDelete(pView);
}
if( db->flags & SQLITE_CountRows ){
sqliteVdbeAddOp(v, OP_Integer, 0, 0);
}
if( pWhere==0 && !row_triggers_exist ){
if( db->flags & SQLITE_CountRows ){
int endOfLoop = sqliteVdbeMakeLabel(v);
int addr;
if( !isView ){
sqliteVdbeAddOp(v, OP_Integer, pTab->iDb, 0);
sqliteVdbeAddOp(v, OP_OpenRead, iCur, pTab->tnum);
}
sqliteVdbeAddOp(v, OP_Rewind, iCur, sqliteVdbeCurrentAddr(v)+2);
addr = sqliteVdbeAddOp(v, OP_AddImm, 1, 0);
sqliteVdbeAddOp(v, OP_Next, iCur, addr);
sqliteVdbeResolveLabel(v, endOfLoop);
sqliteVdbeAddOp(v, OP_Close, iCur, 0);
}
if( !isView ){
sqliteVdbeAddOp(v, OP_Clear, pTab->tnum, pTab->iDb);
for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
sqliteVdbeAddOp(v, OP_Clear, pIdx->tnum, pIdx->iDb);
}
}
}
else{
pWInfo = sqliteWhereBegin(pParse, pTabList, pWhere, 1, 0);
if( pWInfo==0 ) goto delete_from_cleanup;
sqliteVdbeAddOp(v, OP_ListWrite, 0, 0);
if( db->flags & SQLITE_CountRows ){
sqliteVdbeAddOp(v, OP_AddImm, 1, 0);
}
sqliteWhereEnd(pWInfo);
if( row_triggers_exist ){
sqliteVdbeAddOp(v, OP_OpenPseudo, oldIdx, 0);
}
sqliteVdbeAddOp(v, OP_ListRewind, 0, 0);
end = sqliteVdbeMakeLabel(v);
if( row_triggers_exist ){
addr = sqliteVdbeAddOp(v, OP_ListRead, 0, end);
sqliteVdbeAddOp(v, OP_Dup, 0, 0);
if( !isView ){
sqliteVdbeAddOp(v, OP_Integer, pTab->iDb, 0);
sqliteVdbeAddOp(v, OP_OpenRead, iCur, pTab->tnum);
}
sqliteVdbeAddOp(v, OP_MoveTo, iCur, 0);
sqliteVdbeAddOp(v, OP_Recno, iCur, 0);
sqliteVdbeAddOp(v, OP_RowData, iCur, 0);
sqliteVdbeAddOp(v, OP_PutIntKey, oldIdx, 0);
if( !isView ){
sqliteVdbeAddOp(v, OP_Close, iCur, 0);
}
sqliteCodeRowTrigger(pParse, TK_DELETE, 0, TK_BEFORE, pTab, -1,
oldIdx, (pParse->trigStack)?pParse->trigStack->orconf:OE_Default,
addr);
}
if( !isView ){
pParse->nTab = iCur + 1;
sqliteOpenTableAndIndices(pParse, pTab, iCur);
if( !row_triggers_exist ){
addr = sqliteVdbeAddOp(v, OP_ListRead, 0, end);
}
sqliteGenerateRowDelete(db, v, pTab, iCur, pParse->trigStack==0);
}
if( row_triggers_exist ){
if( !isView ){
for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
sqliteVdbeAddOp(v, OP_Close, iCur + i, pIdx->tnum);
}
sqliteVdbeAddOp(v, OP_Close, iCur, 0);
}
sqliteCodeRowTrigger(pParse, TK_DELETE, 0, TK_AFTER, pTab, -1,
oldIdx, (pParse->trigStack)?pParse->trigStack->orconf:OE_Default,
addr);
}
sqliteVdbeAddOp(v, OP_Goto, 0, addr);
sqliteVdbeResolveLabel(v, end);
sqliteVdbeAddOp(v, OP_ListReset, 0, 0);
if( !row_triggers_exist ){
for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
sqliteVdbeAddOp(v, OP_Close, iCur + i, pIdx->tnum);
}
sqliteVdbeAddOp(v, OP_Close, iCur, 0);
pParse->nTab = iCur;
}
}
sqliteVdbeAddOp(v, OP_SetCounts, 0, 0);
sqliteEndWriteOperation(pParse);
if( db->flags & SQLITE_CountRows ){
sqliteVdbeAddOp(v, OP_ColumnName, 0, 1);
sqliteVdbeChangeP3(v, -1, "rows deleted", P3_STATIC);
sqliteVdbeAddOp(v, OP_Callback, 1, 0);
}
delete_from_cleanup:
sqliteAuthContextPop(&sContext);
sqliteSrcListDelete(pTabList);
sqliteExprDelete(pWhere);
return;
}
void sqliteGenerateRowDelete(
sqlite *db,
Vdbe *v,
Table *pTab,
int iCur,
int count
){
int addr;
addr = sqliteVdbeAddOp(v, OP_NotExists, iCur, 0);
sqliteGenerateRowIndexDelete(db, v, pTab, iCur, 0);
sqliteVdbeAddOp(v, OP_Delete, iCur,
(count?OPFLAG_NCHANGE:0) | OPFLAG_CSCHANGE);
sqliteVdbeChangeP2(v, addr, sqliteVdbeCurrentAddr(v));
}
void sqliteGenerateRowIndexDelete(
sqlite *db,
Vdbe *v,
Table *pTab,
int iCur,
char *aIdxUsed
){
int i;
Index *pIdx;
for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
int j;
if( aIdxUsed!=0 && aIdxUsed[i-1]==0 ) continue;
sqliteVdbeAddOp(v, OP_Recno, iCur, 0);
for(j=0; j<pIdx->nColumn; j++){
int idx = pIdx->aiColumn[j];
if( idx==pTab->iPKey ){
sqliteVdbeAddOp(v, OP_Dup, j, 0);
}else{
sqliteVdbeAddOp(v, OP_Column, iCur, idx);
}
}
sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0);
if( db->file_format>=4 ) sqliteAddIdxKeyType(v, pIdx);
sqliteVdbeAddOp(v, OP_IdxDelete, iCur+i, 0);
}
}