Add RAISE() function, which allows more advanced flow-control in trigger programs (ticket #55) (CVS 614)

FossilOrigin-Name: d4a2fb10067203a0d49317db747759872e62927e
This commit is contained in:
danielk1977
2002-06-11 02:25:40 +00:00
parent 28f4b6885b
commit 6f34903e85
12 changed files with 256 additions and 42 deletions
+5 -3
View File
@@ -12,7 +12,7 @@
** This file contains C code routines that are called by the parser
** to handle DELETE FROM statements.
**
** $Id: delete.c,v 1.36 2002/05/24 02:04:33 drh Exp $
** $Id: delete.c,v 1.37 2002/06/11 02:25:41 danielk1977 Exp $
*/
#include "sqliteInt.h"
@@ -237,7 +237,8 @@ void sqliteDeleteFrom(
sqliteVdbeAddOp(v, OP_Rewind, oldIdx, 0);
sqliteCodeRowTrigger(pParse, TK_DELETE, 0, TK_BEFORE, pTab, -1,
oldIdx, (pParse->trigStack)?pParse->trigStack->orconf:OE_Default);
oldIdx, (pParse->trigStack)?pParse->trigStack->orconf:OE_Default,
addr);
}
/* Open cursors for the table we are deleting from and all its
@@ -271,7 +272,8 @@ void sqliteDeleteFrom(
}
sqliteVdbeAddOp(v, OP_Close, base, 0);
sqliteCodeRowTrigger(pParse, TK_DELETE, 0, TK_AFTER, pTab, -1,
oldIdx, (pParse->trigStack)?pParse->trigStack->orconf:OE_Default);
oldIdx, (pParse->trigStack)?pParse->trigStack->orconf:OE_Default,
addr);
}
/* End of the delete loop */
+24 -2
View File
@@ -12,7 +12,7 @@
** This file contains routines used for analyzing expressions and
** for generating VDBE code that evaluates expressions in SQLite.
**
** $Id: expr.c,v 1.70 2002/06/09 01:16:01 drh Exp $
** $Id: expr.c,v 1.71 2002/06/11 02:25:41 danielk1977 Exp $
*/
#include "sqliteInt.h"
#include <ctype.h>
@@ -204,7 +204,7 @@ SrcList *sqliteSrcListDup(SrcList *p){
if( pNew==0 ) return 0;
pNew->nSrc = p->nSrc;
pNew->a = sqliteMalloc( p->nSrc*sizeof(p->a[0]) );
if( pNew->a==0 ) return 0;
if( pNew->a==0 && p->nSrc != 0 ) return 0;
for(i=0; i<p->nSrc; i++){
pNew->a[i].zName = sqliteStrDup(p->a[i].zName);
pNew->a[i].zAlias = sqliteStrDup(p->a[i].zAlias);
@@ -1014,6 +1014,28 @@ void sqliteExprCode(Parse *pParse, Expr *pExpr){
sqliteVdbeAddOp(v, OP_String, 0, 0);
}
sqliteVdbeResolveLabel(v, expr_end_label);
break;
}
case TK_RAISE: {
if( !pParse->trigStack ){
sqliteSetNString(&pParse->zErrMsg,
"RAISE() may only be used within a trigger-program", -1, 0);
pParse->nErr++;
return;
}
if( pExpr->iColumn == OE_Rollback ||
pExpr->iColumn == OE_Abort ||
pExpr->iColumn == OE_Fail ){
char * msg = sqliteStrNDup(pExpr->token.z, pExpr->token.n);
sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn);
sqliteDequote(msg);
sqliteVdbeChangeP3(v, -1, msg, 0);
sqliteFree(msg);
} else {
assert( pExpr->iColumn == OE_Ignore );
sqliteVdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
sqliteVdbeChangeP3(v, -1, "(IGNORE jump)", -1);
}
}
break;
}
+4 -4
View File
@@ -12,7 +12,7 @@
** This file contains C code routines that are called by the parser
** to handle INSERT statements in SQLite.
**
** $Id: insert.c,v 1.60 2002/06/06 18:54:40 drh Exp $
** $Id: insert.c,v 1.61 2002/06/11 02:25:42 danielk1977 Exp $
*/
#include "sqliteInt.h"
@@ -235,6 +235,7 @@ void sqliteInsert(
iCont = sqliteVdbeCurrentAddr(v);
}
endOfLoop = sqliteVdbeMakeLabel(v);
if( row_triggers_exist ){
/* build the new.* reference row */
@@ -262,7 +263,7 @@ void sqliteInsert(
/* Fire BEFORE triggers */
if( sqliteCodeRowTrigger(pParse, TK_INSERT, 0, TK_BEFORE, pTab, newIdx, -1,
onError) ){
onError, endOfLoop) ){
goto insert_cleanup;
}
@@ -336,7 +337,6 @@ void sqliteInsert(
/* Generate code to check constraints and generate index keys and
** do the insertion.
*/
endOfLoop = sqliteVdbeMakeLabel(v);
sqliteGenerateConstraintChecks(pParse, pTab, base, 0,0,0,onError,endOfLoop);
sqliteCompleteInsertion(pParse, pTab, base, 0,0,0);
@@ -358,7 +358,7 @@ void sqliteInsert(
/* Code AFTER triggers */
if( sqliteCodeRowTrigger(pParse, TK_INSERT, 0, TK_AFTER, pTab, newIdx, -1,
onError) ){
onError, endOfLoop) ){
goto insert_cleanup;
}
}
+15 -2
View File
@@ -14,7 +14,7 @@
** the parser. Lemon will also generate a header file containing
** numeric codes for all of the tokens.
**
** @(#) $Id: parse.y,v 1.72 2002/06/06 19:04:16 drh Exp $
** @(#) $Id: parse.y,v 1.73 2002/06/11 02:25:42 danielk1977 Exp $
*/
%token_prefix TK_
%token_type {Token}
@@ -120,7 +120,7 @@ id(A) ::= ID(X). {A = X;}
ABORT AFTER ASC BEFORE BEGIN CASCADE CLUSTER CONFLICT
COPY DEFERRED DELIMITERS DESC EACH END EXPLAIN FAIL FOR
FULL IGNORE IMMEDIATE INITIALLY INSTEAD MATCH JOIN KEY
OF OFFSET PARTIAL PRAGMA REPLACE RESTRICT ROW STATEMENT
OF OFFSET PARTIAL PRAGMA RAISE REPLACE RESTRICT ROW STATEMENT
TEMP TRIGGER VACUUM VIEW.
// And "ids" is an identifer-or-string.
@@ -756,6 +756,19 @@ trigger_cmd(A) ::= DELETE FROM ids(X) where_opt(Y).
// SELECT
trigger_cmd(A) ::= select(X). {A = sqliteTriggerSelectStep(X); }
// The special RAISE expression that may occur in trigger programs
expr(A) ::= RAISE(X) LP IGNORE RP(Y). { A = sqliteExpr(TK_RAISE, 0, 0, 0);
A->iColumn = OE_Ignore; sqliteExprSpan(A, &X, &Y);}
expr(A) ::= RAISE(X) LP ROLLBACK COMMA ids(Z) RP(Y).
{ A = sqliteExpr(TK_RAISE, 0, 0, &Z);
A->iColumn = OE_Rollback; sqliteExprSpan(A, &X, &Y);}
expr(A) ::= RAISE(X) LP ABORT COMMA ids(Z) RP(Y).
{ A = sqliteExpr(TK_RAISE, 0, 0, &Z);
A->iColumn = OE_Abort; sqliteExprSpan(A, &X, &Y);}
expr(A) ::= RAISE(X) LP FAIL COMMA ids(Z) RP(Y).
{ A = sqliteExpr(TK_RAISE, 0, 0, &Z);
A->iColumn = OE_Fail; sqliteExprSpan(A, &X, &Y);}
//////////////////////// DROP TRIGGER statement //////////////////////////////
cmd ::= DROP TRIGGER ids(X). {
sqliteDropTrigger(pParse,&X,0);
+4 -2
View File
@@ -11,7 +11,7 @@
*************************************************************************
** Internal interface definitions for SQLite.
**
** @(#) $Id: sqliteInt.h,v 1.122 2002/06/09 10:14:19 drh Exp $
** @(#) $Id: sqliteInt.h,v 1.123 2002/06/11 02:25:42 danielk1977 Exp $
*/
#include "sqlite.h"
#include "hash.h"
@@ -764,6 +764,7 @@ struct TriggerStack {
int newIdx; /* Index of vdbe cursor to "new" temp table */
int oldIdx; /* Index of vdbe cursor to "old" temp table */
int orconf; /* Current orconf policy */
int ignoreJump; /* where to jump to for a RAISE(IGNORE) */
Trigger *pTrigger;
TriggerStack *pNext;
@@ -895,7 +896,8 @@ void sqliteCreateTrigger(Parse*, Token*, int, int, IdList*, Token*,
int, Expr*, TriggerStep*, char const*,int);
void sqliteDropTrigger(Parse*, Token*, int);
int sqliteTriggersExist(Parse* , Trigger* , int , int , int, ExprList*);
int sqliteCodeRowTrigger(Parse*, int, ExprList*, int, Table *, int, int, int);
int sqliteCodeRowTrigger(Parse*, int, ExprList*, int, Table *, int, int,
int, int);
void sqliteViewTriggers(Parse*, Table*, Expr*, int, ExprList*);
TriggerStep *sqliteTriggerSelectStep(Select*);
TriggerStep *sqliteTriggerInsertStep(Token*, IdList*, ExprList*, Select*, int);
+2 -1
View File
@@ -15,7 +15,7 @@
** individual tokens and sends those tokens one-by-one over to the
** parser for analysis.
**
** $Id: tokenize.c,v 1.44 2002/06/02 18:19:00 drh Exp $
** $Id: tokenize.c,v 1.45 2002/06/11 02:25:42 danielk1977 Exp $
*/
#include "sqliteInt.h"
#include "os.h"
@@ -105,6 +105,7 @@ static Keyword aKeywordTable[] = {
{ "PARTIAL", 0, TK_PARTIAL, 0 },
{ "PRAGMA", 0, TK_PRAGMA, 0 },
{ "PRIMARY", 0, TK_PRIMARY, 0 },
{ "RAISE", 0, TK_RAISE, 0 },
{ "REFERENCES", 0, TK_REFERENCES, 0 },
{ "REPLACE", 0, TK_REPLACE, 0 },
{ "RESTRICT", 0, TK_RESTRICT, 0 },
+14 -8
View File
@@ -480,14 +480,18 @@ static int codeTriggerProgram(
pParse->trigStack->orconf = orconf;
switch( pTriggerStep->op ){
case TK_SELECT: {
sqliteSelect(pParse, pTriggerStep->pSelect, SRT_Discard, 0, 0, 0, 0);
Select * ss = sqliteSelectDup(pTriggerStep->pSelect);
assert(ss);
assert(ss->pSrc);
sqliteSelect(pParse, ss, SRT_Discard, 0, 0, 0, 0);
sqliteSelectDelete(ss);
break;
}
case TK_UPDATE: {
sqliteVdbeAddOp(pParse->pVdbe, OP_ListPush, 0, 0);
sqliteUpdate(pParse, &pTriggerStep->target,
sqliteExprListDup(pTriggerStep->pExprList),
sqliteExprDup(pTriggerStep->pWhere), orconf);
sqliteExprListDup(pTriggerStep->pExprList),
sqliteExprDup(pTriggerStep->pWhere), orconf);
sqliteVdbeAddOp(pParse->pVdbe, OP_ListPop, 0, 0);
break;
}
@@ -543,7 +547,8 @@ int sqliteCodeRowTrigger(
Table *pTab, /* The table to code triggers from */
int newIdx, /* The indice of the "new" row to access */
int oldIdx, /* The indice of the "old" row to access */
int orconf /* ON CONFLICT policy */
int orconf, /* ON CONFLICT policy */
int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
){
Trigger * pTrigger;
TriggerStack * pTriggerStack;
@@ -588,6 +593,7 @@ int sqliteCodeRowTrigger(
pTriggerStack->oldIdx = oldIdx;
pTriggerStack->pTab = pTab;
pTriggerStack->pNext = pParse->trigStack;
pTriggerStack->ignoreJump = ignoreJump;
pParse->trigStack = pTriggerStack;
/* code the WHEN clause */
@@ -735,14 +741,14 @@ void sqliteViewTriggers(
sqliteVdbeAddOp(v, OP_Rewind, newIdx, 0);
sqliteCodeRowTrigger(pParse, TK_UPDATE, pChanges, TK_BEFORE,
pTab, newIdx, oldIdx, orconf);
pTab, newIdx, oldIdx, orconf, endOfLoop);
sqliteCodeRowTrigger(pParse, TK_UPDATE, pChanges, TK_AFTER,
pTab, newIdx, oldIdx, orconf);
pTab, newIdx, oldIdx, orconf, endOfLoop);
}else{
sqliteCodeRowTrigger(pParse, TK_DELETE, 0, TK_BEFORE, pTab, -1, oldIdx,
orconf);
orconf, endOfLoop);
sqliteCodeRowTrigger(pParse, TK_DELETE, 0, TK_AFTER, pTab, -1, oldIdx,
orconf);
orconf, endOfLoop);
}
sqliteVdbeAddOp(v, OP_Next, oldIdx, startOfLoop);
+3 -3
View File
@@ -12,7 +12,7 @@
** This file contains C code routines that are called by the parser
** to handle UPDATE statements.
**
** $Id: update.c,v 1.43 2002/05/24 02:04:34 drh Exp $
** $Id: update.c,v 1.44 2002/06/11 02:25:42 danielk1977 Exp $
*/
#include "sqliteInt.h"
@@ -243,7 +243,7 @@ void sqliteUpdate(
sqliteVdbeAddOp(v, OP_Rewind, newIdx, 0);
if( sqliteCodeRowTrigger(pParse, TK_UPDATE, pChanges, TK_BEFORE, pTab,
newIdx, oldIdx, onError) ){
newIdx, oldIdx, onError, addr) ){
goto update_cleanup;
}
}
@@ -350,7 +350,7 @@ void sqliteUpdate(
pParse->nTab = base;
if( sqliteCodeRowTrigger(pParse, TK_UPDATE, pChanges, TK_AFTER, pTab,
newIdx, oldIdx, onError) ){
newIdx, oldIdx, onError, addr) ){
goto update_cleanup;
}
}
+5 -1
View File
@@ -30,7 +30,7 @@
** But other routines are also provided to help in building up
** a program instruction by instruction.
**
** $Id: vdbe.c,v 1.154 2002/06/08 23:25:09 drh Exp $
** $Id: vdbe.c,v 1.155 2002/06/11 02:25:42 danielk1977 Exp $
*/
#include "sqliteInt.h"
#include <ctype.h>
@@ -1395,6 +1395,10 @@ case OP_Halt: {
if( pOp->p1!=SQLITE_OK ){
rc = pOp->p1;
errorAction = pOp->p2;
if( pOp->p3 ){
sqliteSetString(pzErrMsg, pOp->p3, 0);
goto cleanup;
}
goto abort_due_to_error;
}else{
pc = p->nOp-1;