| OLD | NEW |
| 1 /* | 1 /* |
| 2 ** | 2 ** |
| 3 ** The author disclaims copyright to this source code. In place of | 3 ** The author disclaims copyright to this source code. In place of |
| 4 ** a legal notice, here is a blessing: | 4 ** a legal notice, here is a blessing: |
| 5 ** | 5 ** |
| 6 ** May you do good and not evil. | 6 ** May you do good and not evil. |
| 7 ** May you find forgiveness for yourself and forgive others. | 7 ** May you find forgiveness for yourself and forgive others. |
| 8 ** May you share freely, never taking more than you give. | 8 ** May you share freely, never taking more than you give. |
| 9 ** | 9 ** |
| 10 ************************************************************************* | 10 ************************************************************************* |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 assert( op>0 && op<0xff ); | 104 assert( op>0 && op<0xff ); |
| 105 if( isTemp ){ | 105 if( isTemp ){ |
| 106 /* If TEMP was specified, then the trigger name may not be qualified. */ | 106 /* If TEMP was specified, then the trigger name may not be qualified. */ |
| 107 if( pName2->n>0 ){ | 107 if( pName2->n>0 ){ |
| 108 sqlite3ErrorMsg(pParse, "temporary trigger may not have qualified name"); | 108 sqlite3ErrorMsg(pParse, "temporary trigger may not have qualified name"); |
| 109 goto trigger_cleanup; | 109 goto trigger_cleanup; |
| 110 } | 110 } |
| 111 iDb = 1; | 111 iDb = 1; |
| 112 pName = pName1; | 112 pName = pName1; |
| 113 }else{ | 113 }else{ |
| 114 /* Figure out the db that the the trigger will be created in */ | 114 /* Figure out the db that the trigger will be created in */ |
| 115 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName); | 115 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName); |
| 116 if( iDb<0 ){ | 116 if( iDb<0 ){ |
| 117 goto trigger_cleanup; | 117 goto trigger_cleanup; |
| 118 } | 118 } |
| 119 } | 119 } |
| 120 if( !pTableName || db->mallocFailed ){ |
| 121 goto trigger_cleanup; |
| 122 } |
| 123 |
| 124 /* A long-standing parser bug is that this syntax was allowed: |
| 125 ** |
| 126 ** CREATE TRIGGER attached.demo AFTER INSERT ON attached.tab .... |
| 127 ** ^^^^^^^^ |
| 128 ** |
| 129 ** To maintain backwards compatibility, ignore the database |
| 130 ** name on pTableName if we are reparsing out of SQLITE_MASTER. |
| 131 */ |
| 132 if( db->init.busy && iDb!=1 ){ |
| 133 sqlite3DbFree(db, pTableName->a[0].zDatabase); |
| 134 pTableName->a[0].zDatabase = 0; |
| 135 } |
| 120 | 136 |
| 121 /* If the trigger name was unqualified, and the table is a temp table, | 137 /* If the trigger name was unqualified, and the table is a temp table, |
| 122 ** then set iDb to 1 to create the trigger in the temporary database. | 138 ** then set iDb to 1 to create the trigger in the temporary database. |
| 123 ** If sqlite3SrcListLookup() returns 0, indicating the table does not | 139 ** If sqlite3SrcListLookup() returns 0, indicating the table does not |
| 124 ** exist, the error is caught by the block below. | 140 ** exist, the error is caught by the block below. |
| 125 */ | 141 */ |
| 126 if( !pTableName || db->mallocFailed ){ | |
| 127 goto trigger_cleanup; | |
| 128 } | |
| 129 pTab = sqlite3SrcListLookup(pParse, pTableName); | 142 pTab = sqlite3SrcListLookup(pParse, pTableName); |
| 130 if( db->init.busy==0 && pName2->n==0 && pTab | 143 if( db->init.busy==0 && pName2->n==0 && pTab |
| 131 && pTab->pSchema==db->aDb[1].pSchema ){ | 144 && pTab->pSchema==db->aDb[1].pSchema ){ |
| 132 iDb = 1; | 145 iDb = 1; |
| 133 } | 146 } |
| 134 | 147 |
| 135 /* Ensure the table name matches database name and that the table exists */ | 148 /* Ensure the table name matches database name and that the table exists */ |
| 136 if( db->mallocFailed ) goto trigger_cleanup; | 149 if( db->mallocFailed ) goto trigger_cleanup; |
| 137 assert( pTableName->nSrc==1 ); | 150 assert( pTableName->nSrc==1 ); |
| 138 if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName) && | 151 sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName); |
| 139 sqlite3FixSrcList(&sFix, pTableName) ){ | 152 if( sqlite3FixSrcList(&sFix, pTableName) ){ |
| 140 goto trigger_cleanup; | 153 goto trigger_cleanup; |
| 141 } | 154 } |
| 142 pTab = sqlite3SrcListLookup(pParse, pTableName); | 155 pTab = sqlite3SrcListLookup(pParse, pTableName); |
| 143 if( !pTab ){ | 156 if( !pTab ){ |
| 144 /* The table does not exist. */ | 157 /* The table does not exist. */ |
| 145 if( db->init.iDb==1 ){ | 158 if( db->init.iDb==1 ){ |
| 146 /* Ticket #3810. | 159 /* Ticket #3810. |
| 147 ** Normally, whenever a table is dropped, all associated triggers are | 160 ** Normally, whenever a table is dropped, all associated triggers are |
| 148 ** dropped too. But if a TEMP trigger is created on a non-TEMP table | 161 ** dropped too. But if a TEMP trigger is created on a non-TEMP table |
| 149 ** and the table is dropped by a different database connection, the | 162 ** and the table is dropped by a different database connection, the |
| (...skipping 10 matching lines...) Expand all Loading... |
| 160 goto trigger_cleanup; | 173 goto trigger_cleanup; |
| 161 } | 174 } |
| 162 | 175 |
| 163 /* Check that the trigger name is not reserved and that no trigger of the | 176 /* Check that the trigger name is not reserved and that no trigger of the |
| 164 ** specified name exists */ | 177 ** specified name exists */ |
| 165 zName = sqlite3NameFromToken(db, pName); | 178 zName = sqlite3NameFromToken(db, pName); |
| 166 if( !zName || SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ | 179 if( !zName || SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ |
| 167 goto trigger_cleanup; | 180 goto trigger_cleanup; |
| 168 } | 181 } |
| 169 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); | 182 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
| 170 if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash), | 183 if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),zName) ){ |
| 171 zName, sqlite3Strlen30(zName)) ){ | |
| 172 if( !noErr ){ | 184 if( !noErr ){ |
| 173 sqlite3ErrorMsg(pParse, "trigger %T already exists", pName); | 185 sqlite3ErrorMsg(pParse, "trigger %T already exists", pName); |
| 174 }else{ | 186 }else{ |
| 175 assert( !db->init.busy ); | 187 assert( !db->init.busy ); |
| 176 sqlite3CodeVerifySchema(pParse, iDb); | 188 sqlite3CodeVerifySchema(pParse, iDb); |
| 177 } | 189 } |
| 178 goto trigger_cleanup; | 190 goto trigger_cleanup; |
| 179 } | 191 } |
| 180 | 192 |
| 181 /* Do not create a trigger on a system table */ | 193 /* Do not create a trigger on a system table */ |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 271 if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup; | 283 if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup; |
| 272 zName = pTrig->zName; | 284 zName = pTrig->zName; |
| 273 iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema); | 285 iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema); |
| 274 pTrig->step_list = pStepList; | 286 pTrig->step_list = pStepList; |
| 275 while( pStepList ){ | 287 while( pStepList ){ |
| 276 pStepList->pTrig = pTrig; | 288 pStepList->pTrig = pTrig; |
| 277 pStepList = pStepList->pNext; | 289 pStepList = pStepList->pNext; |
| 278 } | 290 } |
| 279 nameToken.z = pTrig->zName; | 291 nameToken.z = pTrig->zName; |
| 280 nameToken.n = sqlite3Strlen30(nameToken.z); | 292 nameToken.n = sqlite3Strlen30(nameToken.z); |
| 281 if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", &nameToken) | 293 sqlite3FixInit(&sFix, pParse, iDb, "trigger", &nameToken); |
| 282 && sqlite3FixTriggerStep(&sFix, pTrig->step_list) ){ | 294 if( sqlite3FixTriggerStep(&sFix, pTrig->step_list) |
| 295 || sqlite3FixExpr(&sFix, pTrig->pWhen) |
| 296 ){ |
| 283 goto triggerfinish_cleanup; | 297 goto triggerfinish_cleanup; |
| 284 } | 298 } |
| 285 | 299 |
| 286 /* if we are not initializing, | 300 /* if we are not initializing, |
| 287 ** build the sqlite_master entry | 301 ** build the sqlite_master entry |
| 288 */ | 302 */ |
| 289 if( !db->init.busy ){ | 303 if( !db->init.busy ){ |
| 290 Vdbe *v; | 304 Vdbe *v; |
| 291 char *z; | 305 char *z; |
| 292 | 306 |
| 293 /* Make an entry in the sqlite_master table */ | 307 /* Make an entry in the sqlite_master table */ |
| 294 v = sqlite3GetVdbe(pParse); | 308 v = sqlite3GetVdbe(pParse); |
| 295 if( v==0 ) goto triggerfinish_cleanup; | 309 if( v==0 ) goto triggerfinish_cleanup; |
| 296 sqlite3BeginWriteOperation(pParse, 0, iDb); | 310 sqlite3BeginWriteOperation(pParse, 0, iDb); |
| 297 z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n); | 311 z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n); |
| 298 sqlite3NestedParse(pParse, | 312 sqlite3NestedParse(pParse, |
| 299 "INSERT INTO %Q.%s VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')", | 313 "INSERT INTO %Q.%s VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')", |
| 300 db->aDb[iDb].zName, SCHEMA_TABLE(iDb), zName, | 314 db->aDb[iDb].zName, SCHEMA_TABLE(iDb), zName, |
| 301 pTrig->table, z); | 315 pTrig->table, z); |
| 302 sqlite3DbFree(db, z); | 316 sqlite3DbFree(db, z); |
| 303 sqlite3ChangeCookie(pParse, iDb); | 317 sqlite3ChangeCookie(pParse, iDb); |
| 304 sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 0, 0, sqlite3MPrintf( | 318 sqlite3VdbeAddParseSchemaOp(v, iDb, |
| 305 db, "type='trigger' AND name='%q'", zName), P4_DYNAMIC | 319 sqlite3MPrintf(db, "type='trigger' AND name='%q'", zName)); |
| 306 ); | |
| 307 } | 320 } |
| 308 | 321 |
| 309 if( db->init.busy ){ | 322 if( db->init.busy ){ |
| 310 Trigger *pLink = pTrig; | 323 Trigger *pLink = pTrig; |
| 311 Hash *pHash = &db->aDb[iDb].pSchema->trigHash; | 324 Hash *pHash = &db->aDb[iDb].pSchema->trigHash; |
| 312 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); | 325 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
| 313 pTrig = sqlite3HashInsert(pHash, zName, sqlite3Strlen30(zName), pTrig); | 326 pTrig = sqlite3HashInsert(pHash, zName, pTrig); |
| 314 if( pTrig ){ | 327 if( pTrig ){ |
| 315 db->mallocFailed = 1; | 328 db->mallocFailed = 1; |
| 316 }else if( pLink->pSchema==pLink->pTabSchema ){ | 329 }else if( pLink->pSchema==pLink->pTabSchema ){ |
| 317 Table *pTab; | 330 Table *pTab; |
| 318 int n = sqlite3Strlen30(pLink->table); | 331 pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table); |
| 319 pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table, n); | |
| 320 assert( pTab!=0 ); | 332 assert( pTab!=0 ); |
| 321 pLink->pNext = pTab->pTrigger; | 333 pLink->pNext = pTab->pTrigger; |
| 322 pTab->pTrigger = pLink; | 334 pTab->pTrigger = pLink; |
| 323 } | 335 } |
| 324 } | 336 } |
| 325 | 337 |
| 326 triggerfinish_cleanup: | 338 triggerfinish_cleanup: |
| 327 sqlite3DeleteTrigger(db, pTrig); | 339 sqlite3DeleteTrigger(db, pTrig); |
| 328 assert( !pParse->pNewTrigger ); | 340 assert( !pParse->pNewTrigger ); |
| 329 sqlite3DeleteTriggerStep(db, pStepList); | 341 sqlite3DeleteTriggerStep(db, pStepList); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 376 ** Build a trigger step out of an INSERT statement. Return a pointer | 388 ** Build a trigger step out of an INSERT statement. Return a pointer |
| 377 ** to the new trigger step. | 389 ** to the new trigger step. |
| 378 ** | 390 ** |
| 379 ** The parser calls this routine when it sees an INSERT inside the | 391 ** The parser calls this routine when it sees an INSERT inside the |
| 380 ** body of a trigger. | 392 ** body of a trigger. |
| 381 */ | 393 */ |
| 382 TriggerStep *sqlite3TriggerInsertStep( | 394 TriggerStep *sqlite3TriggerInsertStep( |
| 383 sqlite3 *db, /* The database connection */ | 395 sqlite3 *db, /* The database connection */ |
| 384 Token *pTableName, /* Name of the table into which we insert */ | 396 Token *pTableName, /* Name of the table into which we insert */ |
| 385 IdList *pColumn, /* List of columns in pTableName to insert into */ | 397 IdList *pColumn, /* List of columns in pTableName to insert into */ |
| 386 ExprList *pEList, /* The VALUE clause: a list of values to be inserted */ | |
| 387 Select *pSelect, /* A SELECT statement that supplies values */ | 398 Select *pSelect, /* A SELECT statement that supplies values */ |
| 388 u8 orconf /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */ | 399 u8 orconf /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */ |
| 389 ){ | 400 ){ |
| 390 TriggerStep *pTriggerStep; | 401 TriggerStep *pTriggerStep; |
| 391 | 402 |
| 392 assert(pEList == 0 || pSelect == 0); | 403 assert(pSelect != 0 || db->mallocFailed); |
| 393 assert(pEList != 0 || pSelect != 0 || db->mallocFailed); | |
| 394 | 404 |
| 395 pTriggerStep = triggerStepAllocate(db, TK_INSERT, pTableName); | 405 pTriggerStep = triggerStepAllocate(db, TK_INSERT, pTableName); |
| 396 if( pTriggerStep ){ | 406 if( pTriggerStep ){ |
| 397 pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE); | 407 pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE); |
| 398 pTriggerStep->pIdList = pColumn; | 408 pTriggerStep->pIdList = pColumn; |
| 399 pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE); | |
| 400 pTriggerStep->orconf = orconf; | 409 pTriggerStep->orconf = orconf; |
| 401 }else{ | 410 }else{ |
| 402 sqlite3IdListDelete(db, pColumn); | 411 sqlite3IdListDelete(db, pColumn); |
| 403 } | 412 } |
| 404 sqlite3ExprListDelete(db, pEList); | |
| 405 sqlite3SelectDelete(db, pSelect); | 413 sqlite3SelectDelete(db, pSelect); |
| 406 | 414 |
| 407 return pTriggerStep; | 415 return pTriggerStep; |
| 408 } | 416 } |
| 409 | 417 |
| 410 /* | 418 /* |
| 411 ** Construct a trigger step that implements an UPDATE statement and return | 419 ** Construct a trigger step that implements an UPDATE statement and return |
| 412 ** a pointer to that trigger step. The parser calls this routine when it | 420 ** a pointer to that trigger step. The parser calls this routine when it |
| 413 ** sees an UPDATE statement inside the body of a CREATE TRIGGER. | 421 ** sees an UPDATE statement inside the body of a CREATE TRIGGER. |
| 414 */ | 422 */ |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 472 ** This may be called directly from the parser and therefore identifies | 480 ** This may be called directly from the parser and therefore identifies |
| 473 ** the trigger by name. The sqlite3DropTriggerPtr() routine does the | 481 ** the trigger by name. The sqlite3DropTriggerPtr() routine does the |
| 474 ** same job as this routine except it takes a pointer to the trigger | 482 ** same job as this routine except it takes a pointer to the trigger |
| 475 ** instead of the trigger name. | 483 ** instead of the trigger name. |
| 476 **/ | 484 **/ |
| 477 void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){ | 485 void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){ |
| 478 Trigger *pTrigger = 0; | 486 Trigger *pTrigger = 0; |
| 479 int i; | 487 int i; |
| 480 const char *zDb; | 488 const char *zDb; |
| 481 const char *zName; | 489 const char *zName; |
| 482 int nName; | |
| 483 sqlite3 *db = pParse->db; | 490 sqlite3 *db = pParse->db; |
| 484 | 491 |
| 485 if( db->mallocFailed ) goto drop_trigger_cleanup; | 492 if( db->mallocFailed ) goto drop_trigger_cleanup; |
| 486 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ | 493 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ |
| 487 goto drop_trigger_cleanup; | 494 goto drop_trigger_cleanup; |
| 488 } | 495 } |
| 489 | 496 |
| 490 assert( pName->nSrc==1 ); | 497 assert( pName->nSrc==1 ); |
| 491 zDb = pName->a[0].zDatabase; | 498 zDb = pName->a[0].zDatabase; |
| 492 zName = pName->a[0].zName; | 499 zName = pName->a[0].zName; |
| 493 nName = sqlite3Strlen30(zName); | |
| 494 assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) ); | 500 assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) ); |
| 495 for(i=OMIT_TEMPDB; i<db->nDb; i++){ | 501 for(i=OMIT_TEMPDB; i<db->nDb; i++){ |
| 496 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */ | 502 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */ |
| 497 if( zDb && sqlite3StrICmp(db->aDb[j].zName, zDb) ) continue; | 503 if( zDb && sqlite3StrICmp(db->aDb[j].zName, zDb) ) continue; |
| 498 assert( sqlite3SchemaMutexHeld(db, j, 0) ); | 504 assert( sqlite3SchemaMutexHeld(db, j, 0) ); |
| 499 pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName, nName); | 505 pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName); |
| 500 if( pTrigger ) break; | 506 if( pTrigger ) break; |
| 501 } | 507 } |
| 502 if( !pTrigger ){ | 508 if( !pTrigger ){ |
| 503 if( !noErr ){ | 509 if( !noErr ){ |
| 504 sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0); | 510 sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0); |
| 505 }else{ | 511 }else{ |
| 506 sqlite3CodeVerifyNamedSchema(pParse, zDb); | 512 sqlite3CodeVerifyNamedSchema(pParse, zDb); |
| 507 } | 513 } |
| 508 pParse->checkSchema = 1; | 514 pParse->checkSchema = 1; |
| 509 goto drop_trigger_cleanup; | 515 goto drop_trigger_cleanup; |
| 510 } | 516 } |
| 511 sqlite3DropTriggerPtr(pParse, pTrigger); | 517 sqlite3DropTriggerPtr(pParse, pTrigger); |
| 512 | 518 |
| 513 drop_trigger_cleanup: | 519 drop_trigger_cleanup: |
| 514 sqlite3SrcListDelete(db, pName); | 520 sqlite3SrcListDelete(db, pName); |
| 515 } | 521 } |
| 516 | 522 |
| 517 /* | 523 /* |
| 518 ** Return a pointer to the Table structure for the table that a trigger | 524 ** Return a pointer to the Table structure for the table that a trigger |
| 519 ** is set on. | 525 ** is set on. |
| 520 */ | 526 */ |
| 521 static Table *tableOfTrigger(Trigger *pTrigger){ | 527 static Table *tableOfTrigger(Trigger *pTrigger){ |
| 522 int n = sqlite3Strlen30(pTrigger->table); | 528 return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table); |
| 523 return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table, n); | |
| 524 } | 529 } |
| 525 | 530 |
| 526 | 531 |
| 527 /* | 532 /* |
| 528 ** Drop a trigger given a pointer to that trigger. | 533 ** Drop a trigger given a pointer to that trigger. |
| 529 */ | 534 */ |
| 530 void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){ | 535 void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){ |
| 531 Table *pTable; | 536 Table *pTable; |
| 532 Vdbe *v; | 537 Vdbe *v; |
| 533 sqlite3 *db = pParse->db; | 538 sqlite3 *db = pParse->db; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 549 return; | 554 return; |
| 550 } | 555 } |
| 551 } | 556 } |
| 552 #endif | 557 #endif |
| 553 | 558 |
| 554 /* Generate code to destroy the database record of the trigger. | 559 /* Generate code to destroy the database record of the trigger. |
| 555 */ | 560 */ |
| 556 assert( pTable!=0 ); | 561 assert( pTable!=0 ); |
| 557 if( (v = sqlite3GetVdbe(pParse))!=0 ){ | 562 if( (v = sqlite3GetVdbe(pParse))!=0 ){ |
| 558 int base; | 563 int base; |
| 564 static const int iLn = VDBE_OFFSET_LINENO(2); |
| 559 static const VdbeOpList dropTrigger[] = { | 565 static const VdbeOpList dropTrigger[] = { |
| 560 { OP_Rewind, 0, ADDR(9), 0}, | 566 { OP_Rewind, 0, ADDR(9), 0}, |
| 561 { OP_String8, 0, 1, 0}, /* 1 */ | 567 { OP_String8, 0, 1, 0}, /* 1 */ |
| 562 { OP_Column, 0, 1, 2}, | 568 { OP_Column, 0, 1, 2}, |
| 563 { OP_Ne, 2, ADDR(8), 1}, | 569 { OP_Ne, 2, ADDR(8), 1}, |
| 564 { OP_String8, 0, 1, 0}, /* 4: "trigger" */ | 570 { OP_String8, 0, 1, 0}, /* 4: "trigger" */ |
| 565 { OP_Column, 0, 0, 2}, | 571 { OP_Column, 0, 0, 2}, |
| 566 { OP_Ne, 2, ADDR(8), 1}, | 572 { OP_Ne, 2, ADDR(8), 1}, |
| 567 { OP_Delete, 0, 0, 0}, | 573 { OP_Delete, 0, 0, 0}, |
| 568 { OP_Next, 0, ADDR(1), 0}, /* 8 */ | 574 { OP_Next, 0, ADDR(1), 0}, /* 8 */ |
| 569 }; | 575 }; |
| 570 | 576 |
| 571 sqlite3BeginWriteOperation(pParse, 0, iDb); | 577 sqlite3BeginWriteOperation(pParse, 0, iDb); |
| 572 sqlite3OpenMasterTable(pParse, iDb); | 578 sqlite3OpenMasterTable(pParse, iDb); |
| 573 base = sqlite3VdbeAddOpList(v, ArraySize(dropTrigger), dropTrigger); | 579 base = sqlite3VdbeAddOpList(v, ArraySize(dropTrigger), dropTrigger, iLn); |
| 574 sqlite3VdbeChangeP4(v, base+1, pTrigger->zName, P4_TRANSIENT); | 580 sqlite3VdbeChangeP4(v, base+1, pTrigger->zName, P4_TRANSIENT); |
| 575 sqlite3VdbeChangeP4(v, base+4, "trigger", P4_STATIC); | 581 sqlite3VdbeChangeP4(v, base+4, "trigger", P4_STATIC); |
| 576 sqlite3ChangeCookie(pParse, iDb); | 582 sqlite3ChangeCookie(pParse, iDb); |
| 577 sqlite3VdbeAddOp2(v, OP_Close, 0, 0); | 583 sqlite3VdbeAddOp2(v, OP_Close, 0, 0); |
| 578 sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0); | 584 sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0); |
| 579 if( pParse->nMem<3 ){ | 585 if( pParse->nMem<3 ){ |
| 580 pParse->nMem = 3; | 586 pParse->nMem = 3; |
| 581 } | 587 } |
| 582 } | 588 } |
| 583 } | 589 } |
| 584 | 590 |
| 585 /* | 591 /* |
| 586 ** Remove a trigger from the hash tables of the sqlite* pointer. | 592 ** Remove a trigger from the hash tables of the sqlite* pointer. |
| 587 */ | 593 */ |
| 588 void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){ | 594 void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){ |
| 589 Trigger *pTrigger; | 595 Trigger *pTrigger; |
| 590 Hash *pHash; | 596 Hash *pHash; |
| 591 | 597 |
| 592 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); | 598 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
| 593 pHash = &(db->aDb[iDb].pSchema->trigHash); | 599 pHash = &(db->aDb[iDb].pSchema->trigHash); |
| 594 pTrigger = sqlite3HashInsert(pHash, zName, sqlite3Strlen30(zName), 0); | 600 pTrigger = sqlite3HashInsert(pHash, zName, 0); |
| 595 if( ALWAYS(pTrigger) ){ | 601 if( ALWAYS(pTrigger) ){ |
| 596 if( pTrigger->pSchema==pTrigger->pTabSchema ){ | 602 if( pTrigger->pSchema==pTrigger->pTabSchema ){ |
| 597 Table *pTab = tableOfTrigger(pTrigger); | 603 Table *pTab = tableOfTrigger(pTrigger); |
| 598 Trigger **pp; | 604 Trigger **pp; |
| 599 for(pp=&pTab->pTrigger; *pp!=pTrigger; pp=&((*pp)->pNext)); | 605 for(pp=&pTab->pTrigger; *pp!=pTrigger; pp=&((*pp)->pNext)); |
| 600 *pp = (*pp)->pNext; | 606 *pp = (*pp)->pNext; |
| 601 } | 607 } |
| 602 sqlite3DeleteTrigger(db, pTrigger); | 608 sqlite3DeleteTrigger(db, pTrigger); |
| 603 db->flags |= SQLITE_InternChanges; | 609 db->flags |= SQLITE_InternChanges; |
| 604 } | 610 } |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 709 ** step statement. Example: | 715 ** step statement. Example: |
| 710 ** | 716 ** |
| 711 ** CREATE TRIGGER AFTER INSERT ON t1 BEGIN; | 717 ** CREATE TRIGGER AFTER INSERT ON t1 BEGIN; |
| 712 ** INSERT OR REPLACE INTO t2 VALUES(new.a, new.b); | 718 ** INSERT OR REPLACE INTO t2 VALUES(new.a, new.b); |
| 713 ** END; | 719 ** END; |
| 714 ** | 720 ** |
| 715 ** INSERT INTO t1 ... ; -- insert into t2 uses REPLACE policy | 721 ** INSERT INTO t1 ... ; -- insert into t2 uses REPLACE policy |
| 716 ** INSERT OR IGNORE INTO t1 ... ; -- insert into t2 uses IGNORE policy | 722 ** INSERT OR IGNORE INTO t1 ... ; -- insert into t2 uses IGNORE policy |
| 717 */ | 723 */ |
| 718 pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf; | 724 pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf; |
| 725 assert( pParse->okConstFactor==0 ); |
| 719 | 726 |
| 720 switch( pStep->op ){ | 727 switch( pStep->op ){ |
| 721 case TK_UPDATE: { | 728 case TK_UPDATE: { |
| 722 sqlite3Update(pParse, | 729 sqlite3Update(pParse, |
| 723 targetSrcList(pParse, pStep), | 730 targetSrcList(pParse, pStep), |
| 724 sqlite3ExprListDup(db, pStep->pExprList, 0), | 731 sqlite3ExprListDup(db, pStep->pExprList, 0), |
| 725 sqlite3ExprDup(db, pStep->pWhere, 0), | 732 sqlite3ExprDup(db, pStep->pWhere, 0), |
| 726 pParse->eOrconf | 733 pParse->eOrconf |
| 727 ); | 734 ); |
| 728 break; | 735 break; |
| 729 } | 736 } |
| 730 case TK_INSERT: { | 737 case TK_INSERT: { |
| 731 sqlite3Insert(pParse, | 738 sqlite3Insert(pParse, |
| 732 targetSrcList(pParse, pStep), | 739 targetSrcList(pParse, pStep), |
| 733 sqlite3ExprListDup(db, pStep->pExprList, 0), | |
| 734 sqlite3SelectDup(db, pStep->pSelect, 0), | 740 sqlite3SelectDup(db, pStep->pSelect, 0), |
| 735 sqlite3IdListDup(db, pStep->pIdList), | 741 sqlite3IdListDup(db, pStep->pIdList), |
| 736 pParse->eOrconf | 742 pParse->eOrconf |
| 737 ); | 743 ); |
| 738 break; | 744 break; |
| 739 } | 745 } |
| 740 case TK_DELETE: { | 746 case TK_DELETE: { |
| 741 sqlite3DeleteFrom(pParse, | 747 sqlite3DeleteFrom(pParse, |
| 742 targetSrcList(pParse, pStep), | 748 targetSrcList(pParse, pStep), |
| 743 sqlite3ExprDup(db, pStep->pWhere, 0) | 749 sqlite3ExprDup(db, pStep->pWhere, 0) |
| (...skipping 10 matching lines...) Expand all Loading... |
| 754 } | 760 } |
| 755 } | 761 } |
| 756 if( pStep->op!=TK_SELECT ){ | 762 if( pStep->op!=TK_SELECT ){ |
| 757 sqlite3VdbeAddOp0(v, OP_ResetCount); | 763 sqlite3VdbeAddOp0(v, OP_ResetCount); |
| 758 } | 764 } |
| 759 } | 765 } |
| 760 | 766 |
| 761 return 0; | 767 return 0; |
| 762 } | 768 } |
| 763 | 769 |
| 764 #ifdef SQLITE_DEBUG | 770 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS |
| 765 /* | 771 /* |
| 766 ** This function is used to add VdbeComment() annotations to a VDBE | 772 ** This function is used to add VdbeComment() annotations to a VDBE |
| 767 ** program. It is not used in production code, only for debugging. | 773 ** program. It is not used in production code, only for debugging. |
| 768 */ | 774 */ |
| 769 static const char *onErrorText(int onError){ | 775 static const char *onErrorText(int onError){ |
| 770 switch( onError ){ | 776 switch( onError ){ |
| 771 case OE_Abort: return "abort"; | 777 case OE_Abort: return "abort"; |
| 772 case OE_Rollback: return "rollback"; | 778 case OE_Rollback: return "rollback"; |
| 773 case OE_Fail: return "fail"; | 779 case OE_Fail: return "fail"; |
| 774 case OE_Replace: return "replace"; | 780 case OE_Replace: return "replace"; |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 885 } | 891 } |
| 886 sqlite3VdbeAddOp0(v, OP_Halt); | 892 sqlite3VdbeAddOp0(v, OP_Halt); |
| 887 VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf))); | 893 VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf))); |
| 888 | 894 |
| 889 transferParseError(pParse, pSubParse); | 895 transferParseError(pParse, pSubParse); |
| 890 if( db->mallocFailed==0 ){ | 896 if( db->mallocFailed==0 ){ |
| 891 pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg); | 897 pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg); |
| 892 } | 898 } |
| 893 pProgram->nMem = pSubParse->nMem; | 899 pProgram->nMem = pSubParse->nMem; |
| 894 pProgram->nCsr = pSubParse->nTab; | 900 pProgram->nCsr = pSubParse->nTab; |
| 901 pProgram->nOnce = pSubParse->nOnce; |
| 895 pProgram->token = (void *)pTrigger; | 902 pProgram->token = (void *)pTrigger; |
| 896 pPrg->aColmask[0] = pSubParse->oldmask; | 903 pPrg->aColmask[0] = pSubParse->oldmask; |
| 897 pPrg->aColmask[1] = pSubParse->newmask; | 904 pPrg->aColmask[1] = pSubParse->newmask; |
| 898 sqlite3VdbeDelete(v); | 905 sqlite3VdbeDelete(v); |
| 899 } | 906 } |
| 900 | 907 |
| 901 assert( !pSubParse->pAinc && !pSubParse->pZombieTab ); | 908 assert( !pSubParse->pAinc && !pSubParse->pZombieTab ); |
| 902 assert( !pSubParse->pTriggerPrg && !pSubParse->nMaxArg ); | 909 assert( !pSubParse->pTriggerPrg && !pSubParse->nMaxArg ); |
| 910 sqlite3ParserReset(pSubParse); |
| 903 sqlite3StackFree(db, pSubParse); | 911 sqlite3StackFree(db, pSubParse); |
| 904 | 912 |
| 905 return pPrg; | 913 return pPrg; |
| 906 } | 914 } |
| 907 | 915 |
| 908 /* | 916 /* |
| 909 ** Return a pointer to a TriggerPrg object containing the sub-program for | 917 ** Return a pointer to a TriggerPrg object containing the sub-program for |
| 910 ** trigger pTrigger with default ON CONFLICT algorithm orconf. If no such | 918 ** trigger pTrigger with default ON CONFLICT algorithm orconf. If no such |
| 911 ** TriggerPrg object exists, a new object is allocated and populated before | 919 ** TriggerPrg object exists, a new object is allocated and populated before |
| 912 ** being returned. | 920 ** being returned. |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 973 ** invocation is disallowed if (a) the sub-program is really a trigger, | 981 ** invocation is disallowed if (a) the sub-program is really a trigger, |
| 974 ** not a foreign key action, and (b) the flag to enable recursive triggers | 982 ** not a foreign key action, and (b) the flag to enable recursive triggers |
| 975 ** is clear. */ | 983 ** is clear. */ |
| 976 sqlite3VdbeChangeP5(v, (u8)bRecursive); | 984 sqlite3VdbeChangeP5(v, (u8)bRecursive); |
| 977 } | 985 } |
| 978 } | 986 } |
| 979 | 987 |
| 980 /* | 988 /* |
| 981 ** This is called to code the required FOR EACH ROW triggers for an operation | 989 ** This is called to code the required FOR EACH ROW triggers for an operation |
| 982 ** on table pTab. The operation to code triggers for (INSERT, UPDATE or DELETE) | 990 ** on table pTab. The operation to code triggers for (INSERT, UPDATE or DELETE) |
| 983 ** is given by the op paramater. The tr_tm parameter determines whether the | 991 ** is given by the op parameter. The tr_tm parameter determines whether the |
| 984 ** BEFORE or AFTER triggers are coded. If the operation is an UPDATE, then | 992 ** BEFORE or AFTER triggers are coded. If the operation is an UPDATE, then |
| 985 ** parameter pChanges is passed the list of columns being modified. | 993 ** parameter pChanges is passed the list of columns being modified. |
| 986 ** | 994 ** |
| 987 ** If there are no triggers that fire at the specified time for the specified | 995 ** If there are no triggers that fire at the specified time for the specified |
| 988 ** operation on pTab, this function is a no-op. | 996 ** operation on pTab, this function is a no-op. |
| 989 ** | 997 ** |
| 990 ** The reg argument is the address of the first in an array of registers | 998 ** The reg argument is the address of the first in an array of registers |
| 991 ** that contain the values substituted for the new.* and old.* references | 999 ** that contain the values substituted for the new.* and old.* references |
| 992 ** in the trigger program. If N is the number of columns in table pTab | 1000 ** in the trigger program. If N is the number of columns in table pTab |
| 993 ** (a copy of pTab->nCol), then registers are populated as follows: | 1001 ** (a copy of pTab->nCol), then registers are populated as follows: |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1102 if( pPrg ){ | 1110 if( pPrg ){ |
| 1103 mask |= pPrg->aColmask[isNew]; | 1111 mask |= pPrg->aColmask[isNew]; |
| 1104 } | 1112 } |
| 1105 } | 1113 } |
| 1106 } | 1114 } |
| 1107 | 1115 |
| 1108 return mask; | 1116 return mask; |
| 1109 } | 1117 } |
| 1110 | 1118 |
| 1111 #endif /* !defined(SQLITE_OMIT_TRIGGER) */ | 1119 #endif /* !defined(SQLITE_OMIT_TRIGGER) */ |
| OLD | NEW |