sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
| 1 | /* |
| 2 | ** |
| 3 | ** The author disclaims copyright to this source code. In place of |
| 4 | ** a legal notice, here is a blessing: |
| 5 | ** |
| 6 | ** May you do good and not evil. |
| 7 | ** May you find forgiveness for yourself and forgive others. |
| 8 | ** May you share freely, never taking more than you give. |
| 9 | ** |
| 10 | ************************************************************************* |
| 11 | ** This file contains the implementation for TRIGGERs |
| 12 | */ |
| 13 | #include "sqliteInt.h" |
| 14 | |
| 15 | #ifndef SQLITE_OMIT_TRIGGER |
| 16 | /* |
| 17 | ** Delete a linked list of TriggerStep structures. |
| 18 | */ |
| 19 | void sqlite3DeleteTriggerStep(sqlite3 *db, TriggerStep *pTriggerStep){ |
| 20 | while( pTriggerStep ){ |
| 21 | TriggerStep * pTmp = pTriggerStep; |
| 22 | pTriggerStep = pTriggerStep->pNext; |
| 23 | |
| 24 | sqlite3ExprDelete(db, pTmp->pWhere); |
| 25 | sqlite3ExprListDelete(db, pTmp->pExprList); |
| 26 | sqlite3SelectDelete(db, pTmp->pSelect); |
| 27 | sqlite3IdListDelete(db, pTmp->pIdList); |
| 28 | sqlite3UpsertDelete(db, pTmp->pUpsert); |
| 29 | sqlite3SrcListDelete(db, pTmp->pSrc); |
| 30 | sqlite3DbFree(db, pTmp->zSpan); |
| 31 | |
| 32 | sqlite3DbFree(db, pTmp); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | /* |
| 37 | ** Given table pTab, return a list of all the triggers attached to |
| 38 | ** the table. The list is connected by Trigger.pNext pointers. |
| 39 | ** |
| 40 | ** All of the triggers on pTab that are in the same database as pTab |
| 41 | ** are already attached to pTab->pTrigger. But there might be additional |
| 42 | ** triggers on pTab in the TEMP schema. This routine prepends all |
| 43 | ** TEMP triggers on pTab to the beginning of the pTab->pTrigger list |
| 44 | ** and returns the combined list. |
| 45 | ** |
| 46 | ** To state it another way: This routine returns a list of all triggers |
| 47 | ** that fire off of pTab. The list will include any TEMP triggers on |
| 48 | ** pTab as well as the triggers lised in pTab->pTrigger. |
| 49 | */ |
| 50 | Trigger *sqlite3TriggerList(Parse *pParse, Table *pTab){ |
| 51 | Schema *pTmpSchema; /* Schema of the pTab table */ |
| 52 | Trigger *pList; /* List of triggers to return */ |
| 53 | HashElem *p; /* Loop variable for TEMP triggers */ |
| 54 | |
| 55 | assert( pParse->disableTriggers==0 ); |
| 56 | pTmpSchema = pParse->db->aDb[1].pSchema; |
| 57 | p = sqliteHashFirst(&pTmpSchema->trigHash); |
| 58 | pList = pTab->pTrigger; |
| 59 | while( p ){ |
| 60 | Trigger *pTrig = (Trigger *)sqliteHashData(p); |
| 61 | if( pTrig->pTabSchema==pTab->pSchema |
| 62 | && pTrig->table |
| 63 | && 0==sqlite3StrICmp(pTrig->table, pTab->zName) |
| 64 | && (pTrig->pTabSchema!=pTmpSchema || pTrig->bReturning) |
| 65 | ){ |
| 66 | pTrig->pNext = pList; |
| 67 | pList = pTrig; |
| 68 | }else if( pTrig->op==TK_RETURNING ){ |
| 69 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 70 | assert( pParse->db->pVtabCtx==0 ); |
| 71 | #endif |
| 72 | assert( pParse->bReturning ); |
| 73 | assert( !pParse->isCreate ); |
| 74 | assert( &(pParse->u1.d.pReturning->retTrig) == pTrig ); |
| 75 | pTrig->table = pTab->zName; |
| 76 | pTrig->pTabSchema = pTab->pSchema; |
| 77 | pTrig->pNext = pList; |
| 78 | pList = pTrig; |
| 79 | } |
| 80 | p = sqliteHashNext(p); |
| 81 | } |
| 82 | #if 0 |
| 83 | if( pList ){ |
| 84 | Trigger *pX; |
| 85 | printf("Triggers for %s:", pTab->zName); |
| 86 | for(pX=pList; pX; pX=pX->pNext){ |
| 87 | printf(" %s", pX->zName); |
| 88 | } |
| 89 | printf("\n"); |
| 90 | fflush(stdout); |
| 91 | } |
| 92 | #endif |
| 93 | return pList; |
| 94 | } |
| 95 | |
| 96 | /* |
| 97 | ** This is called by the parser when it sees a CREATE TRIGGER statement |
| 98 | ** up to the point of the BEGIN before the trigger actions. A Trigger |
| 99 | ** structure is generated based on the information available and stored |
| 100 | ** in pParse->pNewTrigger. After the trigger actions have been parsed, the |
| 101 | ** sqlite3FinishTrigger() function is called to complete the trigger |
| 102 | ** construction process. |
| 103 | */ |
| 104 | void sqlite3BeginTrigger( |
| 105 | Parse *pParse, /* The parse context of the CREATE TRIGGER statement */ |
| 106 | Token *pName1, /* The name of the trigger */ |
| 107 | Token *pName2, /* The name of the trigger */ |
| 108 | int tr_tm, /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */ |
| 109 | int op, /* One of TK_INSERT, TK_UPDATE, TK_DELETE */ |
| 110 | IdList *pColumns, /* column list if this is an UPDATE OF trigger */ |
| 111 | SrcList *pTableName,/* The name of the table/view the trigger applies to */ |
| 112 | Expr *pWhen, /* WHEN clause */ |
| 113 | int isTemp, /* True if the TEMPORARY keyword is present */ |
| 114 | int noErr /* Suppress errors if the trigger already exists */ |
| 115 | ){ |
| 116 | Trigger *pTrigger = 0; /* The new trigger */ |
| 117 | Table *pTab; /* Table that the trigger fires off of */ |
| 118 | char *zName = 0; /* Name of the trigger */ |
| 119 | sqlite3 *db = pParse->db; /* The database connection */ |
| 120 | int iDb; /* The database to store the trigger in */ |
| 121 | Token *pName; /* The unqualified db name */ |
| 122 | DbFixer sFix; /* State vector for the DB fixer */ |
| 123 | |
| 124 | assert( pName1!=0 ); /* pName1->z might be NULL, but not pName1 itself */ |
| 125 | assert( pName2!=0 ); |
| 126 | assert( op==TK_INSERT || op==TK_UPDATE || op==TK_DELETE ); |
| 127 | assert( op>0 && op<0xff ); |
| 128 | if( isTemp ){ |
| 129 | /* If TEMP was specified, then the trigger name may not be qualified. */ |
| 130 | if( pName2->n>0 ){ |
| 131 | sqlite3ErrorMsg(pParse, "temporary trigger may not have qualified name"); |
| 132 | goto trigger_cleanup; |
| 133 | } |
| 134 | iDb = 1; |
| 135 | pName = pName1; |
| 136 | }else{ |
| 137 | /* Figure out the db that the trigger will be created in */ |
| 138 | iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName); |
| 139 | if( iDb<0 ){ |
| 140 | goto trigger_cleanup; |
| 141 | } |
| 142 | } |
| 143 | if( !pTableName || db->mallocFailed ){ |
| 144 | goto trigger_cleanup; |
| 145 | } |
| 146 | |
| 147 | /* A long-standing parser bug is that this syntax was allowed: |
| 148 | ** |
| 149 | ** CREATE TRIGGER attached.demo AFTER INSERT ON attached.tab .... |
| 150 | ** ^^^^^^^^ |
| 151 | ** |
| 152 | ** To maintain backwards compatibility, ignore the database |
| 153 | ** name on pTableName if we are reparsing out of the schema table |
| 154 | */ |
| 155 | if( db->init.busy && iDb!=1 ){ |
| 156 | assert( pTableName->a[0].fg.fixedSchema==0 ); |
| 157 | assert( pTableName->a[0].fg.isSubquery==0 ); |
| 158 | sqlite3DbFree(db, pTableName->a[0].u4.zDatabase); |
| 159 | pTableName->a[0].u4.zDatabase = 0; |
| 160 | } |
| 161 | |
| 162 | /* If the trigger name was unqualified, and the table is a temp table, |
| 163 | ** then set iDb to 1 to create the trigger in the temporary database. |
| 164 | ** If sqlite3SrcListLookup() returns 0, indicating the table does not |
| 165 | ** exist, the error is caught by the block below. |
| 166 | ** |
| 167 | ** Do not do this if ALTER TABLE is reparsing an existing trigger. In |
| 168 | ** that case iDb is already set correctly. */ |
| 169 | if( !IN_RENAME_OBJECT ){ |
| 170 | pTab = sqlite3SrcListLookup(pParse, pTableName); |
| 171 | if( db->init.busy==0 && pName2->n==0 && pTab |
| 172 | && pTab->pSchema==db->aDb[1].pSchema ){ |
| 173 | iDb = 1; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | /* Ensure the table name matches database name and that the table exists */ |
| 178 | if( db->mallocFailed ) goto trigger_cleanup; |
| 179 | assert( pTableName->nSrc==1 ); |
| 180 | sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName); |
| 181 | if( sqlite3FixSrcList(&sFix, pTableName) ){ |
| 182 | goto trigger_cleanup; |
| 183 | } |
| 184 | pTab = sqlite3SrcListLookup(pParse, pTableName); |
| 185 | if( !pTab ){ |
| 186 | /* The table does not exist. */ |
| 187 | goto trigger_orphan_error; |
| 188 | } |
| 189 | if( IsVirtual(pTab) ){ |
| 190 | sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables"); |
| 191 | goto trigger_orphan_error; |
| 192 | } |
| 193 | if( (pTab->tabFlags & TF_Shadow)!=0 && sqlite3ReadOnlyShadowTables(db) ){ |
| 194 | sqlite3ErrorMsg(pParse, "cannot create triggers on shadow tables"); |
| 195 | goto trigger_orphan_error; |
| 196 | } |
| 197 | |
| 198 | /* Check that the trigger name is not reserved and that no trigger of the |
| 199 | ** specified name exists */ |
| 200 | zName = sqlite3NameFromToken(db, pName); |
| 201 | if( zName==0 ){ |
| 202 | assert( db->mallocFailed ); |
| 203 | goto trigger_cleanup; |
| 204 | } |
| 205 | if( sqlite3CheckObjectName(pParse, zName, "trigger", pTab->zName, |
| 206 | db->aDb[iDb].zDbSName) ){ |
| 207 | goto trigger_cleanup; |
| 208 | } |
| 209 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
| 210 | if( !IN_RENAME_OBJECT ){ |
| 211 | if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),zName) ){ |
| 212 | if( !noErr ){ |
| 213 | sqlite3ErrorMsg(pParse, "trigger %T already exists", pName); |
| 214 | }else if( db->init.busy ){ |
| 215 | sqlite3ErrorMsg(pParse,""); /* Err msg generated by corruptSchema() */ |
| 216 | }else{ |
| 217 | sqlite3CodeVerifySchema(pParse, iDb); |
| 218 | VVA_ONLY( pParse->ifNotExists = 1; ) |
| 219 | } |
| 220 | goto trigger_cleanup; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | /* NB: The SQLITE_ALLOW_TRIGGERS_ON_SYSTEM_TABLES compile-time option is |
| 225 | ** experimental and unsupported. Do not use it unless understand the |
| 226 | ** implications and you cannot get by without this capability. */ |
| 227 | #if !defined(SQLITE_ALLOW_TRIGGERS_ON_SYSTEM_TABLES) /* Experimental */ |
| 228 | /* Do not create a trigger on a system table */ |
| 229 | if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){ |
| 230 | sqlite3ErrorMsg(pParse, "cannot create trigger on system table"); |
| 231 | goto trigger_cleanup; |
| 232 | } |
| 233 | #endif |
| 234 | |
| 235 | /* INSTEAD of triggers are only for views and views only support INSTEAD |
| 236 | ** of triggers. |
| 237 | */ |
| 238 | if( IsView(pTab) && tr_tm!=TK_INSTEAD ){ |
| 239 | sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S", |
| 240 | (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName->a); |
| 241 | goto trigger_orphan_error; |
| 242 | } |
| 243 | if( !IsView(pTab) && tr_tm==TK_INSTEAD ){ |
| 244 | sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF" |
| 245 | " trigger on table: %S", pTableName->a); |
| 246 | goto trigger_orphan_error; |
| 247 | } |
| 248 | |
| 249 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 250 | if( !IN_RENAME_OBJECT ){ |
| 251 | int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
| 252 | int code = SQLITE_CREATE_TRIGGER; |
| 253 | const char *zDb = db->aDb[iTabDb].zDbSName; |
| 254 | const char *zDbTrig = isTemp ? db->aDb[1].zDbSName : zDb; |
| 255 | if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER; |
| 256 | if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){ |
| 257 | goto trigger_cleanup; |
| 258 | } |
| 259 | if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iTabDb),0,zDb)){ |
| 260 | goto trigger_cleanup; |
| 261 | } |
| 262 | } |
| 263 | #endif |
| 264 | |
| 265 | /* INSTEAD OF triggers can only appear on views and BEFORE triggers |
| 266 | ** cannot appear on views. So we might as well translate every |
| 267 | ** INSTEAD OF trigger into a BEFORE trigger. It simplifies code |
| 268 | ** elsewhere. |
| 269 | */ |
| 270 | if (tr_tm == TK_INSTEAD){ |
| 271 | tr_tm = TK_BEFORE; |
| 272 | } |
| 273 | |
| 274 | /* Build the Trigger object */ |
| 275 | pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger)); |
| 276 | if( pTrigger==0 ) goto trigger_cleanup; |
| 277 | pTrigger->zName = zName; |
| 278 | zName = 0; |
| 279 | pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName); |
| 280 | pTrigger->pSchema = db->aDb[iDb].pSchema; |
| 281 | pTrigger->pTabSchema = pTab->pSchema; |
| 282 | pTrigger->op = (u8)op; |
| 283 | pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER; |
| 284 | if( IN_RENAME_OBJECT ){ |
| 285 | sqlite3RenameTokenRemap(pParse, pTrigger->table, pTableName->a[0].zName); |
| 286 | pTrigger->pWhen = pWhen; |
| 287 | pWhen = 0; |
| 288 | }else{ |
| 289 | pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE); |
| 290 | } |
| 291 | pTrigger->pColumns = pColumns; |
| 292 | pColumns = 0; |
| 293 | assert( pParse->pNewTrigger==0 ); |
| 294 | pParse->pNewTrigger = pTrigger; |
| 295 | |
| 296 | trigger_cleanup: |
| 297 | sqlite3DbFree(db, zName); |
| 298 | sqlite3SrcListDelete(db, pTableName); |
| 299 | sqlite3IdListDelete(db, pColumns); |
| 300 | sqlite3ExprDelete(db, pWhen); |
| 301 | if( !pParse->pNewTrigger ){ |
| 302 | sqlite3DeleteTrigger(db, pTrigger); |
| 303 | }else{ |
| 304 | assert( pParse->pNewTrigger==pTrigger ); |
| 305 | } |
| 306 | return; |
| 307 | |
| 308 | trigger_orphan_error: |
| 309 | if( db->init.iDb==1 ){ |
| 310 | /* Ticket #3810. |
| 311 | ** Normally, whenever a table is dropped, all associated triggers are |
| 312 | ** dropped too. But if a TEMP trigger is created on a non-TEMP table |
| 313 | ** and the table is dropped by a different database connection, the |
| 314 | ** trigger is not visible to the database connection that does the |
| 315 | ** drop so the trigger cannot be dropped. This results in an |
| 316 | ** "orphaned trigger" - a trigger whose associated table is missing. |
| 317 | ** |
| 318 | ** 2020-11-05 see also https://sqlite.org/forum/forumpost/157dc791df |
| 319 | */ |
| 320 | db->init.orphanTrigger = 1; |
| 321 | } |
| 322 | goto trigger_cleanup; |
| 323 | } |
| 324 | |
| 325 | /* |
| 326 | ** This routine is called after all of the trigger actions have been parsed |
| 327 | ** in order to complete the process of building the trigger. |
| 328 | */ |
| 329 | void sqlite3FinishTrigger( |
| 330 | Parse *pParse, /* Parser context */ |
| 331 | TriggerStep *pStepList, /* The triggered program */ |
| 332 | Token *pAll /* Token that describes the complete CREATE TRIGGER */ |
| 333 | ){ |
| 334 | Trigger *pTrig = pParse->pNewTrigger; /* Trigger being finished */ |
| 335 | char *zName; /* Name of trigger */ |
| 336 | sqlite3 *db = pParse->db; /* The database */ |
| 337 | DbFixer sFix; /* Fixer object */ |
| 338 | int iDb; /* Database containing the trigger */ |
| 339 | Token nameToken; /* Trigger name for error reporting */ |
| 340 | i64 n; /* Number of steps */ |
| 341 | |
| 342 | pParse->pNewTrigger = 0; |
| 343 | if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup; |
| 344 | zName = pTrig->zName; |
| 345 | iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema); |
| 346 | assert( iDb>=00 && iDb<db->nDb ); |
| 347 | pTrig->step_list = pStepList; |
| 348 | pTrig->nSelect = pParse->nSelect; |
| 349 | for(n=0; pStepList; n++){ |
| 350 | pStepList->pTrig = pTrig; |
| 351 | pStepList = pStepList->pNext; |
| 352 | } |
| 353 | if( n>pParse->db->aLimit[SQLITE_LIMIT_TRIGGER_STEPS] ){ |
| 354 | sqlite3ErrorMsg(pParse, "trigger \"%w\" contains too many steps", zName); |
| 355 | goto triggerfinish_cleanup; |
| 356 | } |
| 357 | sqlite3TokenInit(&nameToken, pTrig->zName); |
| 358 | sqlite3FixInit(&sFix, pParse, iDb, "trigger", &nameToken); |
| 359 | if( sqlite3FixTriggerStep(&sFix, pTrig->step_list) |
| 360 | || sqlite3FixExpr(&sFix, pTrig->pWhen) |
| 361 | ){ |
| 362 | goto triggerfinish_cleanup; |
| 363 | } |
| 364 | |
| 365 | #ifndef SQLITE_OMIT_ALTERTABLE |
| 366 | if( IN_RENAME_OBJECT ){ |
| 367 | assert( !db->init.busy ); |
| 368 | pParse->pNewTrigger = pTrig; |
| 369 | pTrig = 0; |
| 370 | }else |
| 371 | #endif |
| 372 | |
| 373 | /* if we are not initializing, |
| 374 | ** build the sqlite_schema entry |
| 375 | */ |
| 376 | if( !db->init.busy ){ |
| 377 | Vdbe *v; |
| 378 | char *z; |
| 379 | |
| 380 | /* If this is a new CREATE TABLE statement, and if shadow tables |
| 381 | ** are read-only, and the trigger makes a change to a shadow table, |
| 382 | ** then raise an error - do not allow the trigger to be created. |
| 383 | ** The targets of a TEMP trigger may be in any schema, so check all |
| 384 | ** of them in that case. */ |
| 385 | if( sqlite3ReadOnlyShadowTables(db) ){ |
| 386 | TriggerStep *pStep; |
| 387 | const char *zDb = iDb==1 ? 0 : db->aDb[iDb].zDbSName; |
| 388 | for(pStep=pTrig->step_list; pStep; pStep=pStep->pNext){ |
| 389 | if( pStep->pSrc!=0 |
| 390 | && sqlite3ShadowTableName(db, pStep->pSrc->a[0].zName, zDb) |
| 391 | ){ |
| 392 | sqlite3ErrorMsg(pParse, |
| 393 | "trigger \"%s\" may not write to shadow table \"%s\"", |
| 394 | pTrig->zName, pStep->pSrc->a[0].zName); |
| 395 | goto triggerfinish_cleanup; |
| 396 | } |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | /* Make an entry in the sqlite_schema table */ |
| 401 | v = sqlite3GetVdbe(pParse); |
| 402 | if( v==0 ) goto triggerfinish_cleanup; |
| 403 | sqlite3BeginWriteOperation(pParse, 0, iDb); |
| 404 | z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n); |
| 405 | testcase( z==0 ); |
| 406 | sqlite3NestedParse(pParse, |
| 407 | "INSERT INTO %Q." LEGACY_SCHEMA_TABLE |
| 408 | " VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')", |
| 409 | db->aDb[iDb].zDbSName, zName, |
| 410 | pTrig->table, z); |
| 411 | sqlite3DbFree(db, z); |
| 412 | sqlite3ChangeCookie(pParse, iDb); |
| 413 | sqlite3VdbeAddParseSchemaOp(v, iDb, |
| 414 | sqlite3MPrintf(db, "type='trigger' AND name='%q'", zName), 0); |
| 415 | } |
| 416 | |
| 417 | if( db->init.busy ){ |
| 418 | Trigger *pLink = pTrig; |
| 419 | Hash *pHash = &db->aDb[iDb].pSchema->trigHash; |
| 420 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
| 421 | assert( pLink!=0 ); |
| 422 | pTrig = sqlite3HashInsert(pHash, zName, pTrig); |
| 423 | if( pTrig ){ |
| 424 | sqlite3OomFault(db); |
| 425 | }else if( pLink->pSchema==pLink->pTabSchema ){ |
| 426 | Table *pTab; |
| 427 | pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table); |
| 428 | assert( pTab!=0 ); |
| 429 | pLink->pNext = pTab->pTrigger; |
| 430 | pTab->pTrigger = pLink; |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | triggerfinish_cleanup: |
| 435 | sqlite3DeleteTrigger(db, pTrig); |
| 436 | assert( IN_RENAME_OBJECT || !pParse->pNewTrigger ); |
| 437 | sqlite3DeleteTriggerStep(db, pStepList); |
| 438 | } |
| 439 | |
| 440 | /* |
| 441 | ** Duplicate a range of text from an SQL statement, then convert all |
| 442 | ** whitespace characters into ordinary space characters. |
| 443 | */ |
| 444 | static char *triggerSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){ |
| 445 | char *z = sqlite3DbSpanDup(db, zStart, zEnd); |
| 446 | int i; |
| 447 | if( z ) for(i=0; z[i]; i++) if( sqlite3Isspace(z[i]) ) z[i] = ' '; |
| 448 | return z; |
| 449 | } |
| 450 | |
| 451 | /* |
| 452 | ** Turn a SELECT statement (that the pSelect parameter points to) into |
| 453 | ** a trigger step. Return a pointer to a TriggerStep structure. |
| 454 | ** |
| 455 | ** The parser calls this routine when it finds a SELECT statement in |
| 456 | ** body of a TRIGGER. |
| 457 | */ |
| 458 | TriggerStep *sqlite3TriggerSelectStep( |
| 459 | sqlite3 *db, /* Database connection */ |
| 460 | Select *pSelect, /* The SELECT statement */ |
| 461 | const char *zStart, /* Start of SQL text */ |
| 462 | const char *zEnd /* End of SQL text */ |
| 463 | ){ |
| 464 | TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep)); |
| 465 | if( pTriggerStep==0 ) { |
| 466 | sqlite3SelectDelete(db, pSelect); |
| 467 | return 0; |
| 468 | } |
| 469 | pTriggerStep->op = TK_SELECT; |
| 470 | pTriggerStep->pSelect = pSelect; |
| 471 | pTriggerStep->orconf = OE_Default; |
| 472 | pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd); |
| 473 | return pTriggerStep; |
| 474 | } |
| 475 | |
| 476 | /* |
| 477 | ** Allocate space to hold a new trigger step. The allocated space |
| 478 | ** holds both the TriggerStep object and the TriggerStep.target.z string. |
| 479 | ** |
| 480 | ** If an OOM error occurs, NULL is returned and db->mallocFailed is set. |
| 481 | */ |
| 482 | static TriggerStep *triggerStepAllocate( |
| 483 | Parse *pParse, /* Parser context */ |
| 484 | u8 op, /* Trigger opcode */ |
| 485 | SrcList *pTabList, /* Target table */ |
| 486 | const char *zStart, /* Start of SQL text */ |
| 487 | const char *zEnd /* End of SQL text */ |
| 488 | ){ |
| 489 | Trigger *pNew = pParse->pNewTrigger; |
| 490 | sqlite3 *db = pParse->db; |
| 491 | TriggerStep *pTriggerStep = 0; |
| 492 | |
| 493 | if( pParse->nErr==0 ){ |
| 494 | if( pNew |
| 495 | && pNew->pSchema!=db->aDb[1].pSchema |
| 496 | && pTabList->a[0].u4.zDatabase |
| 497 | ){ |
| 498 | sqlite3ErrorMsg(pParse, |
| 499 | "qualified table names are not allowed on INSERT, UPDATE, and DELETE " |
| 500 | "statements within triggers"); |
| 501 | }else{ |
| 502 | pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep)); |
| 503 | if( pTriggerStep ){ |
| 504 | pTriggerStep->pSrc = sqlite3SrcListDup(db, pTabList, EXPRDUP_REDUCE); |
| 505 | pTriggerStep->op = op; |
| 506 | pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd); |
| 507 | if( pTriggerStep->pSrc && IN_RENAME_OBJECT ){ |
| 508 | sqlite3RenameTokenRemap(pParse, |
| 509 | pTriggerStep->pSrc->a[0].zName, |
| 510 | pTabList->a[0].zName |
| 511 | ); |
| 512 | } |
| 513 | } |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | sqlite3SrcListDelete(db, pTabList); |
| 518 | return pTriggerStep; |
| 519 | } |
| 520 | |
| 521 | /* |
| 522 | ** Build a trigger step out of an INSERT statement. Return a pointer |
| 523 | ** to the new trigger step. |
| 524 | ** |
| 525 | ** The parser calls this routine when it sees an INSERT inside the |
| 526 | ** body of a trigger. |
| 527 | */ |
| 528 | TriggerStep *sqlite3TriggerInsertStep( |
| 529 | Parse *pParse, /* Parser */ |
| 530 | SrcList *pTabList, /* Table to INSERT into */ |
| 531 | IdList *pColumn, /* List of columns in pTableName to insert into */ |
| 532 | Select *pSelect, /* A SELECT statement that supplies values */ |
| 533 | u8 orconf, /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */ |
| 534 | Upsert *pUpsert, /* ON CONFLICT clauses for upsert */ |
| 535 | const char *zStart, /* Start of SQL text */ |
| 536 | const char *zEnd /* End of SQL text */ |
| 537 | ){ |
| 538 | sqlite3 *db = pParse->db; |
| 539 | TriggerStep *pTriggerStep; |
| 540 | |
| 541 | assert(pSelect != 0 || db->mallocFailed); |
| 542 | |
| 543 | pTriggerStep = triggerStepAllocate(pParse, TK_INSERT, pTabList, zStart, zEnd); |
| 544 | if( pTriggerStep ){ |
| 545 | if( IN_RENAME_OBJECT ){ |
| 546 | pTriggerStep->pSelect = pSelect; |
| 547 | pSelect = 0; |
| 548 | }else{ |
| 549 | pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE); |
| 550 | } |
| 551 | pTriggerStep->pIdList = pColumn; |
| 552 | pTriggerStep->pUpsert = pUpsert; |
| 553 | pTriggerStep->orconf = orconf; |
| 554 | if( pUpsert ){ |
| 555 | sqlite3HasExplicitNulls(pParse, pUpsert->pUpsertTarget); |
| 556 | } |
| 557 | }else{ |
| 558 | testcase( pColumn ); |
| 559 | sqlite3IdListDelete(db, pColumn); |
| 560 | testcase( pUpsert ); |
| 561 | sqlite3UpsertDelete(db, pUpsert); |
| 562 | } |
| 563 | sqlite3SelectDelete(db, pSelect); |
| 564 | |
| 565 | return pTriggerStep; |
| 566 | } |
| 567 | |
| 568 | /* |
| 569 | ** Construct a trigger step that implements an UPDATE statement and return |
| 570 | ** a pointer to that trigger step. The parser calls this routine when it |
| 571 | ** sees an UPDATE statement inside the body of a CREATE TRIGGER. |
| 572 | */ |
| 573 | TriggerStep *sqlite3TriggerUpdateStep( |
| 574 | Parse *pParse, /* Parser */ |
| 575 | SrcList *pTabList, /* Name of the table to be updated */ |
| 576 | SrcList *pFrom, /* FROM clause for an UPDATE-FROM, or NULL */ |
| 577 | ExprList *pEList, /* The SET clause: list of column and new values */ |
| 578 | Expr *pWhere, /* The WHERE clause */ |
| 579 | u8 orconf, /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */ |
| 580 | const char *zStart, /* Start of SQL text */ |
| 581 | const char *zEnd /* End of SQL text */ |
| 582 | ){ |
| 583 | sqlite3 *db = pParse->db; |
| 584 | TriggerStep *pTriggerStep; |
| 585 | |
| 586 | pTriggerStep = triggerStepAllocate(pParse, TK_UPDATE, pTabList, zStart, zEnd); |
| 587 | if( pTriggerStep ){ |
| 588 | SrcList *pFromDup = 0; |
| 589 | if( IN_RENAME_OBJECT ){ |
| 590 | pTriggerStep->pExprList = pEList; |
| 591 | pTriggerStep->pWhere = pWhere; |
| 592 | pFromDup = pFrom; |
| 593 | pEList = 0; |
| 594 | pWhere = 0; |
| 595 | pFrom = 0; |
| 596 | }else{ |
| 597 | pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE); |
| 598 | pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE); |
| 599 | pFromDup = sqlite3SrcListDup(db, pFrom, EXPRDUP_REDUCE); |
| 600 | } |
| 601 | pTriggerStep->orconf = orconf; |
| 602 | |
| 603 | if( pFromDup && !IN_RENAME_OBJECT){ |
| 604 | Select *pSub; |
| 605 | Token as = {0, 0}; |
| 606 | pSub = sqlite3SelectNew(pParse, 0, pFromDup, 0,0,0,0, SF_NestedFrom, 0); |
| 607 | pFromDup = sqlite3SrcListAppendFromTerm(pParse, 0, 0, 0, &as, pSub ,0); |
| 608 | } |
| 609 | if( pFromDup && pTriggerStep->pSrc ){ |
| 610 | pTriggerStep->pSrc = sqlite3SrcListAppendList( |
| 611 | pParse, pTriggerStep->pSrc, pFromDup |
| 612 | ); |
| 613 | }else{ |
| 614 | sqlite3SrcListDelete(db, pFromDup); |
| 615 | } |
| 616 | } |
| 617 | sqlite3ExprListDelete(db, pEList); |
| 618 | sqlite3ExprDelete(db, pWhere); |
| 619 | sqlite3SrcListDelete(db, pFrom); |
| 620 | return pTriggerStep; |
| 621 | } |
| 622 | |
| 623 | /* |
| 624 | ** Construct a trigger step that implements a DELETE statement and return |
| 625 | ** a pointer to that trigger step. The parser calls this routine when it |
| 626 | ** sees a DELETE statement inside the body of a CREATE TRIGGER. |
| 627 | */ |
| 628 | TriggerStep *sqlite3TriggerDeleteStep( |
| 629 | Parse *pParse, /* Parser */ |
| 630 | SrcList *pTabList, /* The table from which rows are deleted */ |
| 631 | Expr *pWhere, /* The WHERE clause */ |
| 632 | const char *zStart, /* Start of SQL text */ |
| 633 | const char *zEnd /* End of SQL text */ |
| 634 | ){ |
| 635 | sqlite3 *db = pParse->db; |
| 636 | TriggerStep *pTriggerStep; |
| 637 | |
| 638 | pTriggerStep = triggerStepAllocate(pParse, TK_DELETE, pTabList, zStart, zEnd); |
| 639 | if( pTriggerStep ){ |
| 640 | if( IN_RENAME_OBJECT ){ |
| 641 | pTriggerStep->pWhere = pWhere; |
| 642 | pWhere = 0; |
| 643 | }else{ |
| 644 | pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE); |
| 645 | } |
| 646 | pTriggerStep->orconf = OE_Default; |
| 647 | } |
| 648 | sqlite3ExprDelete(db, pWhere); |
| 649 | return pTriggerStep; |
| 650 | } |
| 651 | |
| 652 | /* |
| 653 | ** Recursively delete a Trigger structure |
| 654 | */ |
| 655 | void sqlite3DeleteTrigger(sqlite3 *db, Trigger *pTrigger){ |
| 656 | if( pTrigger==0 || pTrigger->bReturning ) return; |
| 657 | sqlite3DeleteTriggerStep(db, pTrigger->step_list); |
| 658 | sqlite3DbFree(db, pTrigger->zName); |
| 659 | sqlite3DbFree(db, pTrigger->table); |
| 660 | sqlite3ExprDelete(db, pTrigger->pWhen); |
| 661 | sqlite3IdListDelete(db, pTrigger->pColumns); |
| 662 | sqlite3DbFree(db, pTrigger); |
| 663 | } |
| 664 | |
| 665 | /* |
| 666 | ** This function is called to drop a trigger from the database schema. |
| 667 | ** |
| 668 | ** This may be called directly from the parser and therefore identifies |
| 669 | ** the trigger by name. The sqlite3DropTriggerPtr() routine does the |
| 670 | ** same job as this routine except it takes a pointer to the trigger |
| 671 | ** instead of the trigger name. |
| 672 | **/ |
| 673 | void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){ |
| 674 | Trigger *pTrigger = 0; |
| 675 | int i; |
| 676 | const char *zDb; |
| 677 | const char *zName; |
| 678 | sqlite3 *db = pParse->db; |
| 679 | |
| 680 | if( db->mallocFailed ) goto drop_trigger_cleanup; |
| 681 | if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ |
| 682 | goto drop_trigger_cleanup; |
| 683 | } |
| 684 | |
| 685 | assert( pName->nSrc==1 ); |
| 686 | assert( pName->a[0].fg.fixedSchema==0 && pName->a[0].fg.isSubquery==0 ); |
| 687 | zDb = pName->a[0].u4.zDatabase; |
| 688 | zName = pName->a[0].zName; |
| 689 | assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) ); |
| 690 | for(i=OMIT_TEMPDB; i<db->nDb; i++){ |
| 691 | int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */ |
| 692 | if( zDb && sqlite3DbIsNamed(db, j, zDb)==0 ) continue; |
| 693 | assert( sqlite3SchemaMutexHeld(db, j, 0) ); |
| 694 | pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName); |
| 695 | if( pTrigger ) break; |
| 696 | } |
| 697 | if( !pTrigger ){ |
| 698 | if( !noErr ){ |
| 699 | sqlite3ErrorMsg(pParse, "no such trigger: %S", pName->a); |
| 700 | }else{ |
| 701 | sqlite3CodeVerifyNamedSchema(pParse, zDb); |
| 702 | } |
| 703 | pParse->checkSchema = 1; |
| 704 | goto drop_trigger_cleanup; |
| 705 | } |
| 706 | sqlite3DropTriggerPtr(pParse, pTrigger); |
| 707 | |
| 708 | drop_trigger_cleanup: |
| 709 | sqlite3SrcListDelete(db, pName); |
| 710 | } |
| 711 | |
| 712 | /* |
| 713 | ** Return a pointer to the Table structure for the table that a trigger |
| 714 | ** is set on. |
| 715 | */ |
| 716 | static Table *tableOfTrigger(Trigger *pTrigger){ |
| 717 | return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table); |
| 718 | } |
| 719 | |
| 720 | |
| 721 | /* |
| 722 | ** Drop a trigger given a pointer to that trigger. |
| 723 | */ |
| 724 | void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){ |
| 725 | Table *pTable; |
| 726 | Vdbe *v; |
| 727 | sqlite3 *db = pParse->db; |
| 728 | int iDb; |
| 729 | |
| 730 | iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema); |
| 731 | assert( iDb>=0 && iDb<db->nDb ); |
| 732 | pTable = tableOfTrigger(pTrigger); |
| 733 | assert( (pTable && pTable->pSchema==pTrigger->pSchema) || iDb==1 ); |
| 734 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 735 | if( pTable ){ |
| 736 | int code = SQLITE_DROP_TRIGGER; |
| 737 | const char *zDb = db->aDb[iDb].zDbSName; |
| 738 | const char *zTab = SCHEMA_TABLE(iDb); |
| 739 | if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER; |
| 740 | if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) || |
| 741 | sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){ |
| 742 | return; |
| 743 | } |
| 744 | } |
| 745 | #endif |
| 746 | |
| 747 | /* Generate code to destroy the database record of the trigger. |
| 748 | */ |
| 749 | if( (v = sqlite3GetVdbe(pParse))!=0 ){ |
| 750 | sqlite3NestedParse(pParse, |
| 751 | "DELETE FROM %Q." LEGACY_SCHEMA_TABLE " WHERE name=%Q AND type='trigger'", |
| 752 | db->aDb[iDb].zDbSName, pTrigger->zName |
| 753 | ); |
| 754 | sqlite3ChangeCookie(pParse, iDb); |
| 755 | sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0); |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | /* |
| 760 | ** Remove a trigger from the hash tables of the sqlite* pointer. |
| 761 | */ |
| 762 | void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){ |
| 763 | Trigger *pTrigger; |
| 764 | Hash *pHash; |
| 765 | |
| 766 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
| 767 | pHash = &(db->aDb[iDb].pSchema->trigHash); |
| 768 | pTrigger = sqlite3HashInsert(pHash, zName, 0); |
| 769 | if( ALWAYS(pTrigger) ){ |
| 770 | if( pTrigger->pSchema==pTrigger->pTabSchema ){ |
| 771 | Table *pTab = tableOfTrigger(pTrigger); |
| 772 | if( pTab ){ |
| 773 | Trigger **pp; |
| 774 | for(pp=&pTab->pTrigger; *pp; pp=&((*pp)->pNext)){ |
| 775 | if( *pp==pTrigger ){ |
| 776 | *pp = (*pp)->pNext; |
| 777 | break; |
| 778 | } |
| 779 | } |
| 780 | } |
| 781 | } |
| 782 | sqlite3DeleteTrigger(db, pTrigger); |
| 783 | db->mDbFlags |= DBFLAG_SchemaChange; |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | /* |
| 788 | ** pEList is the SET clause of an UPDATE statement. Each entry |
| 789 | ** in pEList is of the format <id>=<expr>. If any of the entries |
| 790 | ** in pEList have an <id> which matches an identifier in pIdList, |
| 791 | ** then return TRUE. If pIdList==NULL, then it is considered a |
| 792 | ** wildcard that matches anything. Likewise if pEList==NULL then |
| 793 | ** it matches anything so always return true. Return false only |
| 794 | ** if there is no match. |
| 795 | */ |
| 796 | static int checkColumnOverlap(IdList *pIdList, ExprList *pEList){ |
| 797 | int e; |
| 798 | if( pIdList==0 || NEVER(pEList==0) ) return 1; |
| 799 | for(e=0; e<pEList->nExpr; e++){ |
| 800 | if( sqlite3IdListIndex(pIdList, pEList->a[e].zEName)>=0 ) return 1; |
| 801 | } |
| 802 | return 0; |
| 803 | } |
| 804 | |
| 805 | /* |
| 806 | ** Return true if any TEMP triggers exist |
| 807 | */ |
| 808 | static int tempTriggersExist(sqlite3 *db){ |
| 809 | if( NEVER(db->aDb[1].pSchema==0) ) return 0; |
| 810 | if( sqliteHashFirst(&db->aDb[1].pSchema->trigHash)==0 ) return 0; |
| 811 | return 1; |
| 812 | } |
| 813 | |
| 814 | /* |
| 815 | ** Return a list of all triggers on table pTab if there exists at least |
| 816 | ** one trigger that must be fired when an operation of type 'op' is |
| 817 | ** performed on the table, and, if that operation is an UPDATE, if at |
| 818 | ** least one of the columns in pChanges is being modified. |
| 819 | */ |
| 820 | static SQLITE_NOINLINE Trigger *triggersReallyExist( |
| 821 | Parse *pParse, /* Parse context */ |
| 822 | Table *pTab, /* The table the contains the triggers */ |
| 823 | int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */ |
| 824 | ExprList *pChanges, /* Columns that change in an UPDATE statement */ |
| 825 | int *pMask /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */ |
| 826 | ){ |
| 827 | int mask = 0; |
| 828 | Trigger *pList = 0; |
| 829 | Trigger *p; |
| 830 | |
| 831 | pList = sqlite3TriggerList(pParse, pTab); |
| 832 | assert( pList==0 || IsVirtual(pTab)==0 |
| 833 | || (pList->bReturning && pList->pNext==0) ); |
| 834 | if( pList!=0 ){ |
| 835 | p = pList; |
| 836 | if( (pParse->db->flags & SQLITE_EnableTrigger)==0 |
| 837 | && pTab->pTrigger!=0 |
| 838 | && sqlite3SchemaToIndex(pParse->db, pTab->pTrigger->pSchema)!=1 |
| 839 | ){ |
| 840 | /* The SQLITE_DBCONFIG_ENABLE_TRIGGER setting is off. That means that |
| 841 | ** only TEMP triggers are allowed. Truncate the pList so that it |
| 842 | ** includes only TEMP triggers */ |
| 843 | if( pList==pTab->pTrigger ){ |
| 844 | pList = 0; |
| 845 | goto exit_triggers_exist; |
| 846 | } |
| 847 | while( ALWAYS(p->pNext) && p->pNext!=pTab->pTrigger ) p = p->pNext; |
| 848 | p->pNext = 0; |
| 849 | p = pList; |
| 850 | } |
| 851 | do{ |
| 852 | if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){ |
| 853 | mask |= p->tr_tm; |
| 854 | }else if( p->op==TK_RETURNING ){ |
| 855 | /* The first time a RETURNING trigger is seen, the "op" value tells |
| 856 | ** us what time of trigger it should be. */ |
| 857 | assert( sqlite3IsToplevel(pParse) ); |
| 858 | p->op = op; |
| 859 | if( IsVirtual(pTab) ){ |
| 860 | if( op!=TK_INSERT ){ |
| 861 | sqlite3ErrorMsg(pParse, |
| 862 | "%s RETURNING is not available on virtual tables", |
| 863 | op==TK_DELETE ? "DELETE" : "UPDATE"); |
| 864 | } |
| 865 | p->tr_tm = TRIGGER_BEFORE; |
| 866 | }else{ |
| 867 | p->tr_tm = TRIGGER_AFTER; |
| 868 | } |
| 869 | mask |= p->tr_tm; |
| 870 | }else if( p->bReturning && p->op==TK_INSERT && op==TK_UPDATE |
| 871 | && sqlite3IsToplevel(pParse) ){ |
| 872 | /* Also fire a RETURNING trigger for an UPSERT */ |
| 873 | mask |= p->tr_tm; |
| 874 | } |
| 875 | p = p->pNext; |
| 876 | }while( p ); |
| 877 | } |
| 878 | exit_triggers_exist: |
| 879 | if( pMask ){ |
| 880 | *pMask = mask; |
| 881 | } |
| 882 | return (mask ? pList : 0); |
| 883 | } |
| 884 | Trigger *sqlite3TriggersExist( |
| 885 | Parse *pParse, /* Parse context */ |
| 886 | Table *pTab, /* The table the contains the triggers */ |
| 887 | int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */ |
| 888 | ExprList *pChanges, /* Columns that change in an UPDATE statement */ |
| 889 | int *pMask /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */ |
| 890 | ){ |
| 891 | assert( pTab!=0 ); |
| 892 | if( (pTab->pTrigger==0 && !tempTriggersExist(pParse->db)) |
| 893 | || pParse->disableTriggers |
| 894 | ){ |
| 895 | if( pMask ) *pMask = 0; |
| 896 | return 0; |
| 897 | } |
| 898 | return triggersReallyExist(pParse,pTab,op,pChanges,pMask); |
| 899 | } |
| 900 | |
| 901 | /* |
| 902 | ** Return true if the pExpr term from the RETURNING clause argument |
| 903 | ** list is of the form "*". Raise an error if the terms if of the |
| 904 | ** form "table.*". |
| 905 | */ |
| 906 | static int isAsteriskTerm( |
| 907 | Parse *pParse, /* Parsing context */ |
| 908 | Expr *pTerm /* A term in the RETURNING clause */ |
| 909 | ){ |
| 910 | assert( pTerm!=0 ); |
| 911 | if( pTerm->op==TK_ASTERISK ) return 1; |
| 912 | if( pTerm->op!=TK_DOT ) return 0; |
| 913 | assert( pTerm->pRight!=0 ); |
| 914 | assert( pTerm->pLeft!=0 ); |
| 915 | if( pTerm->pRight->op!=TK_ASTERISK ) return 0; |
| 916 | sqlite3ErrorMsg(pParse, "RETURNING may not use \"TABLE.*\" wildcards"); |
| 917 | return 1; |
| 918 | } |
| 919 | |
| 920 | /* The input list pList is the list of result set terms from a RETURNING |
| 921 | ** clause. The table that we are returning from is pTab. |
| 922 | ** |
| 923 | ** This routine makes a copy of the pList, and at the same time expands |
| 924 | ** any "*" wildcards to be the complete set of columns from pTab. |
| 925 | */ |
| 926 | static ExprList *sqlite3ExpandReturning( |
| 927 | Parse *pParse, /* Parsing context */ |
| 928 | ExprList *pList, /* The arguments to RETURNING */ |
| 929 | Table *pTab /* The table being updated */ |
| 930 | ){ |
| 931 | ExprList *pNew = 0; |
| 932 | sqlite3 *db = pParse->db; |
| 933 | int i; |
| 934 | |
| 935 | for(i=0; i<pList->nExpr; i++){ |
| 936 | Expr *pOldExpr = pList->a[i].pExpr; |
| 937 | if( NEVER(pOldExpr==0) ) continue; |
| 938 | if( isAsteriskTerm(pParse, pOldExpr) ){ |
| 939 | int jj; |
| 940 | for(jj=0; jj<pTab->nCol; jj++){ |
| 941 | Expr *pNewExpr; |
| 942 | if( IsHiddenColumn(pTab->aCol+jj) ) continue; |
| 943 | pNewExpr = sqlite3Expr(db, TK_ID, pTab->aCol[jj].zCnName); |
| 944 | pNew = sqlite3ExprListAppend(pParse, pNew, pNewExpr); |
| 945 | if( !db->mallocFailed ){ |
| 946 | struct ExprList_item *pItem = &pNew->a[pNew->nExpr-1]; |
| 947 | pItem->zEName = sqlite3DbStrDup(db, pTab->aCol[jj].zCnName); |
| 948 | pItem->fg.eEName = ENAME_NAME; |
| 949 | } |
| 950 | } |
| 951 | }else{ |
| 952 | Expr *pNewExpr = sqlite3ExprDup(db, pOldExpr, 0); |
| 953 | pNew = sqlite3ExprListAppend(pParse, pNew, pNewExpr); |
| 954 | if( !db->mallocFailed && ALWAYS(pList->a[i].zEName!=0) ){ |
| 955 | struct ExprList_item *pItem = &pNew->a[pNew->nExpr-1]; |
| 956 | pItem->zEName = sqlite3DbStrDup(db, pList->a[i].zEName); |
| 957 | pItem->fg.eEName = pList->a[i].fg.eEName; |
| 958 | } |
| 959 | } |
| 960 | } |
| 961 | return pNew; |
| 962 | } |
| 963 | |
| 964 | /* If the Expr node is a subquery or an EXISTS operator or an IN operator that |
| 965 | ** uses a subquery, and if the subquery is SF_Correlated, then mark the |
| 966 | ** expression as EP_VarSelect. |
| 967 | */ |
| 968 | static int sqlite3ReturningSubqueryVarSelect(Walker *NotUsed, Expr *pExpr){ |
| 969 | UNUSED_PARAMETER(NotUsed); |
| 970 | if( ExprUseXSelect(pExpr) |
| 971 | && (pExpr->x.pSelect->selFlags & SF_Correlated)!=0 |
| 972 | ){ |
| 973 | testcase( ExprHasProperty(pExpr, EP_VarSelect) ); |
| 974 | ExprSetProperty(pExpr, EP_VarSelect); |
| 975 | } |
| 976 | return WRC_Continue; |
| 977 | } |
| 978 | |
| 979 | |
| 980 | /* |
| 981 | ** If the SELECT references the table pWalker->u.pTab, then do two things: |
| 982 | ** |
| 983 | ** (1) Mark the SELECT as as SF_Correlated. |
| 984 | ** (2) Set pWalker->eCode to non-zero so that the caller will know |
| 985 | ** that (1) has happened. |
| 986 | */ |
| 987 | static int sqlite3ReturningSubqueryCorrelated(Walker *pWalker, Select *pSelect){ |
| 988 | int i; |
| 989 | SrcList *pSrc; |
| 990 | assert( pSelect!=0 ); |
| 991 | pSrc = pSelect->pSrc; |
| 992 | assert( pSrc!=0 ); |
| 993 | for(i=0; i<pSrc->nSrc; i++){ |
| 994 | if( pSrc->a[i].pSTab==pWalker->u.pTab ){ |
| 995 | testcase( pSelect->selFlags & SF_Correlated ); |
| 996 | pSelect->selFlags |= SF_Correlated; |
| 997 | pWalker->eCode = 1; |
| 998 | break; |
| 999 | } |
| 1000 | } |
| 1001 | return WRC_Continue; |
| 1002 | } |
| 1003 | |
| 1004 | /* |
| 1005 | ** Scan the expression list that is the argument to RETURNING looking |
| 1006 | ** for subqueries that depend on the table which is being modified in the |
| 1007 | ** statement that is hosting the RETURNING clause (pTab). Mark all such |
| 1008 | ** subqueries as SF_Correlated. If the subqueries are part of an |
| 1009 | ** expression, mark the expression as EP_VarSelect. |
| 1010 | ** |
| 1011 | ** https://sqlite.org/forum/forumpost/2c83569ce8945d39 |
| 1012 | */ |
| 1013 | static void sqlite3ProcessReturningSubqueries( |
| 1014 | ExprList *pEList, |
| 1015 | Table *pTab |
| 1016 | ){ |
| 1017 | Walker w; |
| 1018 | memset(&w, 0, sizeof(w)); |
| 1019 | w.xExprCallback = sqlite3ExprWalkNoop; |
| 1020 | w.xSelectCallback = sqlite3ReturningSubqueryCorrelated; |
| 1021 | w.u.pTab = pTab; |
| 1022 | sqlite3WalkExprList(&w, pEList); |
| 1023 | if( w.eCode ){ |
| 1024 | w.xExprCallback = sqlite3ReturningSubqueryVarSelect; |
| 1025 | w.xSelectCallback = sqlite3SelectWalkNoop; |
| 1026 | sqlite3WalkExprList(&w, pEList); |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | /* |
| 1031 | ** Generate code for the RETURNING trigger. Unlike other triggers |
| 1032 | ** that invoke a subprogram in the bytecode, the code for RETURNING |
| 1033 | ** is generated in-line. |
| 1034 | */ |
| 1035 | static void codeReturningTrigger( |
| 1036 | Parse *pParse, /* Parse context */ |
| 1037 | Trigger *pTrigger, /* The trigger step that defines the RETURNING */ |
| 1038 | Table *pTab, /* The table to code triggers from */ |
| 1039 | int regIn /* The first in an array of registers */ |
| 1040 | ){ |
| 1041 | Vdbe *v = pParse->pVdbe; |
| 1042 | sqlite3 *db = pParse->db; |
| 1043 | ExprList *pNew; |
| 1044 | Returning *pReturning; |
| 1045 | Select sSelect; |
| 1046 | SrcList *pFrom; |
| 1047 | union { |
| 1048 | SrcList sSrc; |
| 1049 | u8 fromSpace[SZ_SRCLIST_1]; |
| 1050 | } uSrc; |
| 1051 | |
| 1052 | assert( v!=0 ); |
| 1053 | if( !pParse->bReturning ){ |
| 1054 | /* This RETURNING trigger must be for a different statement as |
| 1055 | ** this statement lacks a RETURNING clause. */ |
| 1056 | return; |
| 1057 | } |
| 1058 | assert( db->pParse==pParse ); |
| 1059 | assert( !pParse->isCreate ); |
| 1060 | pReturning = pParse->u1.d.pReturning; |
| 1061 | if( pTrigger != &(pReturning->retTrig) ){ |
| 1062 | /* This RETURNING trigger is for a different statement */ |
| 1063 | return; |
| 1064 | } |
| 1065 | memset(&sSelect, 0, sizeof(sSelect)); |
| 1066 | memset(&uSrc, 0, sizeof(uSrc)); |
| 1067 | pFrom = &uSrc.sSrc; |
| 1068 | sSelect.pEList = sqlite3ExprListDup(db, pReturning->pReturnEL, 0); |
| 1069 | sSelect.pSrc = pFrom; |
| 1070 | pFrom->nSrc = 1; |
| 1071 | pFrom->a[0].pSTab = pTab; |
| 1072 | pFrom->a[0].zName = pTab->zName; /* tag-20240424-1 */ |
| 1073 | pFrom->a[0].iCursor = -1; |
| 1074 | sqlite3SelectPrep(pParse, &sSelect, 0); |
| 1075 | if( pParse->nErr==0 ){ |
| 1076 | assert( db->mallocFailed==0 ); |
| 1077 | sqlite3GenerateColumnNames(pParse, &sSelect); |
| 1078 | } |
| 1079 | sqlite3ExprListDelete(db, sSelect.pEList); |
| 1080 | pNew = sqlite3ExpandReturning(pParse, pReturning->pReturnEL, pTab); |
| 1081 | if( pParse->nErr==0 ){ |
| 1082 | NameContext sNC; |
| 1083 | memset(&sNC, 0, sizeof(sNC)); |
| 1084 | if( pReturning->nRetCol==0 ){ |
| 1085 | pReturning->nRetCol = pNew->nExpr; |
| 1086 | pReturning->iRetCur = pParse->nTab++; |
| 1087 | } |
| 1088 | sNC.pParse = pParse; |
| 1089 | sNC.uNC.iBaseReg = regIn; |
| 1090 | sNC.ncFlags = NC_UBaseReg; |
| 1091 | pParse->eTriggerOp = pTrigger->op; |
| 1092 | pParse->pTriggerTab = pTab; |
| 1093 | if( sqlite3ResolveExprListNames(&sNC, pNew)==SQLITE_OK |
| 1094 | && ALWAYS(!db->mallocFailed) |
| 1095 | ){ |
| 1096 | int i; |
| 1097 | int nCol = pNew->nExpr; |
| 1098 | int reg = pParse->nMem+1; |
| 1099 | sqlite3ProcessReturningSubqueries(pNew, pTab); |
| 1100 | pParse->nMem += nCol+2; |
| 1101 | pReturning->iRetReg = reg; |
| 1102 | for(i=0; i<nCol; i++){ |
| 1103 | Expr *pCol = pNew->a[i].pExpr; |
| 1104 | assert( pCol!=0 ); /* Due to !db->mallocFailed ~9 lines above */ |
| 1105 | sqlite3ExprCodeFactorable(pParse, pCol, reg+i); |
| 1106 | if( sqlite3ExprAffinity(pCol)==SQLITE_AFF_REAL ){ |
| 1107 | sqlite3VdbeAddOp1(v, OP_RealAffinity, reg+i); |
| 1108 | } |
| 1109 | } |
| 1110 | sqlite3VdbeAddOp3(v, OP_MakeRecord, reg, i, reg+i); |
| 1111 | sqlite3VdbeAddOp2(v, OP_NewRowid, pReturning->iRetCur, reg+i+1); |
| 1112 | sqlite3VdbeAddOp3(v, OP_Insert, pReturning->iRetCur, reg+i, reg+i+1); |
| 1113 | } |
| 1114 | } |
| 1115 | sqlite3ExprListDelete(db, pNew); |
| 1116 | pParse->eTriggerOp = 0; |
| 1117 | pParse->pTriggerTab = 0; |
| 1118 | } |
| 1119 | |
| 1120 | |
| 1121 | |
| 1122 | /* |
| 1123 | ** Generate VDBE code for the statements inside the body of a single |
| 1124 | ** trigger. |
| 1125 | */ |
| 1126 | static int codeTriggerProgram( |
| 1127 | Parse *pParse, /* The parser context */ |
| 1128 | TriggerStep *pStepList, /* List of statements inside the trigger body */ |
| 1129 | int orconf /* Conflict algorithm. (OE_Abort, etc) */ |
| 1130 | ){ |
| 1131 | TriggerStep *pStep; |
| 1132 | Vdbe *v = pParse->pVdbe; |
| 1133 | sqlite3 *db = pParse->db; |
| 1134 | |
| 1135 | assert( pParse->pTriggerTab && pParse->pToplevel ); |
| 1136 | assert( pStepList ); |
| 1137 | assert( v!=0 ); |
| 1138 | for(pStep=pStepList; pStep; pStep=pStep->pNext){ |
| 1139 | /* Figure out the ON CONFLICT policy that will be used for this step |
| 1140 | ** of the trigger program. If the statement that caused this trigger |
| 1141 | ** to fire had an explicit ON CONFLICT, then use it. Otherwise, use |
| 1142 | ** the ON CONFLICT policy that was specified as part of the trigger |
| 1143 | ** step statement. Example: |
| 1144 | ** |
| 1145 | ** CREATE TRIGGER AFTER INSERT ON t1 BEGIN; |
| 1146 | ** INSERT OR REPLACE INTO t2 VALUES(new.a, new.b); |
| 1147 | ** END; |
| 1148 | ** |
| 1149 | ** INSERT INTO t1 ... ; -- insert into t2 uses REPLACE policy |
| 1150 | ** INSERT OR IGNORE INTO t1 ... ; -- insert into t2 uses IGNORE policy |
| 1151 | */ |
| 1152 | pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf; |
| 1153 | assert( pParse->okConstFactor==0 ); |
| 1154 | |
| 1155 | #ifndef SQLITE_OMIT_TRACE |
| 1156 | if( pStep->zSpan ){ |
| 1157 | sqlite3VdbeAddOp4(v, OP_Trace, 0x7fffffff, 1, 0, |
| 1158 | sqlite3MPrintf(db, "-- %s", pStep->zSpan), |
| 1159 | P4_DYNAMIC); |
| 1160 | } |
| 1161 | #endif |
| 1162 | |
| 1163 | switch( pStep->op ){ |
| 1164 | case TK_UPDATE: { |
| 1165 | sqlite3Update(pParse, |
| 1166 | sqlite3SrcListDup(db, pStep->pSrc, 0), |
| 1167 | sqlite3ExprListDup(db, pStep->pExprList, 0), |
| 1168 | sqlite3ExprDup(db, pStep->pWhere, 0), |
| 1169 | pParse->eOrconf, 0, 0, 0 |
| 1170 | ); |
| 1171 | sqlite3VdbeAddOp0(v, OP_ResetCount); |
| 1172 | break; |
| 1173 | } |
| 1174 | case TK_INSERT: { |
| 1175 | sqlite3Insert(pParse, |
| 1176 | sqlite3SrcListDup(db, pStep->pSrc, 0), |
| 1177 | sqlite3SelectDup(db, pStep->pSelect, 0), |
| 1178 | sqlite3IdListDup(db, pStep->pIdList), |
| 1179 | pParse->eOrconf, |
| 1180 | sqlite3UpsertDup(db, pStep->pUpsert) |
| 1181 | ); |
| 1182 | sqlite3VdbeAddOp0(v, OP_ResetCount); |
| 1183 | break; |
| 1184 | } |
| 1185 | case TK_DELETE: { |
| 1186 | sqlite3DeleteFrom(pParse, |
| 1187 | sqlite3SrcListDup(db, pStep->pSrc, 0), |
| 1188 | sqlite3ExprDup(db, pStep->pWhere, 0), 0, 0 |
| 1189 | ); |
| 1190 | sqlite3VdbeAddOp0(v, OP_ResetCount); |
| 1191 | break; |
| 1192 | } |
| 1193 | default: assert( pStep->op==TK_SELECT ); { |
| 1194 | SelectDest sDest; |
| 1195 | Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0); |
| 1196 | sqlite3SelectDestInit(&sDest, SRT_Discard, 0); |
| 1197 | sqlite3Select(pParse, pSelect, &sDest); |
| 1198 | sqlite3SelectDelete(db, pSelect); |
| 1199 | break; |
| 1200 | } |
| 1201 | } |
| 1202 | } |
| 1203 | |
| 1204 | return 0; |
| 1205 | } |
| 1206 | |
| 1207 | #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS |
| 1208 | /* |
| 1209 | ** This function is used to add VdbeComment() annotations to a VDBE |
| 1210 | ** program. It is not used in production code, only for debugging. |
| 1211 | */ |
| 1212 | static const char *onErrorText(int onError){ |
| 1213 | switch( onError ){ |
| 1214 | case OE_Abort: return "abort"; |
| 1215 | case OE_Rollback: return "rollback"; |
| 1216 | case OE_Fail: return "fail"; |
| 1217 | case OE_Replace: return "replace"; |
| 1218 | case OE_Ignore: return "ignore"; |
| 1219 | case OE_Default: return "default"; |
| 1220 | } |
| 1221 | return "n/a"; |
| 1222 | } |
| 1223 | #endif |
| 1224 | |
| 1225 | /* |
| 1226 | ** Parse context structure pFrom has just been used to create a sub-vdbe |
| 1227 | ** (trigger program). If an error has occurred, transfer error information |
| 1228 | ** from pFrom to pTo. |
| 1229 | */ |
| 1230 | static void transferParseError(Parse *pTo, Parse *pFrom){ |
| 1231 | assert( pFrom->zErrMsg==0 || pFrom->nErr ); |
| 1232 | assert( pTo->zErrMsg==0 || pTo->nErr ); |
| 1233 | if( pTo->nErr==0 ){ |
| 1234 | pTo->zErrMsg = pFrom->zErrMsg; |
| 1235 | pTo->nErr = pFrom->nErr; |
| 1236 | pTo->rc = pFrom->rc; |
| 1237 | }else{ |
| 1238 | sqlite3DbFree(pFrom->db, pFrom->zErrMsg); |
| 1239 | } |
| 1240 | } |
| 1241 | |
| 1242 | /* |
| 1243 | ** Create and populate a new TriggerPrg object with a sub-program |
| 1244 | ** implementing trigger pTrigger with ON CONFLICT policy orconf. |
| 1245 | */ |
| 1246 | static TriggerPrg *codeRowTrigger( |
| 1247 | Parse *pParse, /* Current parse context */ |
| 1248 | Trigger *pTrigger, /* Trigger to code */ |
| 1249 | Table *pTab, /* The table pTrigger is attached to */ |
| 1250 | int orconf /* ON CONFLICT policy to code trigger program with */ |
| 1251 | ){ |
| 1252 | Parse *pTop; /* Top level Parse object */ |
| 1253 | sqlite3 *db = pParse->db; /* Database handle */ |
| 1254 | TriggerPrg *pPrg; /* Value to return */ |
| 1255 | Expr *pWhen = 0; /* Duplicate of trigger WHEN expression */ |
| 1256 | Vdbe *v; /* Temporary VM */ |
| 1257 | NameContext sNC; /* Name context for sub-vdbe */ |
| 1258 | SubProgram *pProgram = 0; /* Sub-vdbe for trigger program */ |
| 1259 | int iEndTrigger = 0; /* Label to jump to if WHEN is false */ |
| 1260 | Parse sSubParse; /* Parse context for sub-vdbe */ |
| 1261 | int nDepth; /* Trigger depth */ |
| 1262 | |
| 1263 | /* Ensure that triggers are not chained too deep. This test is linear |
| 1264 | ** in the chaining depth, but sensible code ought not be chaining |
| 1265 | ** triggers excessively, so that shouldn't be a problem. |
| 1266 | */ |
| 1267 | pTop = pParse; |
| 1268 | for(nDepth=0; pTop->pOuterParse; pTop = pTop->pOuterParse, nDepth++){} |
| 1269 | if( nDepth>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){ |
| 1270 | sqlite3ErrorMsg(pParse, "triggers nested too deep"); |
| 1271 | return 0; |
| 1272 | } |
| 1273 | |
| 1274 | pTop = sqlite3ParseToplevel(pParse); |
| 1275 | assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) ); |
| 1276 | assert( pTop->pVdbe ); |
| 1277 | |
| 1278 | |
| 1279 | /* Allocate the TriggerPrg and SubProgram objects. To ensure that they |
| 1280 | ** are freed if an error occurs, link them into the Parse.pTriggerPrg |
| 1281 | ** list of the top-level Parse object sooner rather than later. */ |
| 1282 | pPrg = sqlite3DbMallocZero(db, sizeof(TriggerPrg)); |
| 1283 | if( !pPrg ) return 0; |
| 1284 | pPrg->pNext = pTop->pTriggerPrg; |
| 1285 | pTop->pTriggerPrg = pPrg; |
| 1286 | pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram)); |
| 1287 | if( !pProgram ) return 0; |
| 1288 | sqlite3VdbeLinkSubProgram(pTop->pVdbe, pProgram); |
| 1289 | pPrg->pTrigger = pTrigger; |
| 1290 | pPrg->orconf = orconf; |
| 1291 | pPrg->aColmask[0] = 0xffffffff; |
| 1292 | pPrg->aColmask[1] = 0xffffffff; |
| 1293 | |
| 1294 | /* Allocate and populate a new Parse context to use for coding the |
| 1295 | ** trigger sub-program. */ |
| 1296 | sqlite3ParseObjectInit(&sSubParse, db); |
| 1297 | memset(&sNC, 0, sizeof(sNC)); |
| 1298 | sNC.pParse = &sSubParse; |
| 1299 | sSubParse.pTriggerTab = pTab; |
| 1300 | sSubParse.pToplevel = pTop; |
| 1301 | sSubParse.pTrigCoded = pTrigger; |
| 1302 | sSubParse.nSelect = pTrigger->nSelect; |
| 1303 | sSubParse.zAuthContext = pTrigger->zName; |
| 1304 | sSubParse.eTriggerOp = pTrigger->op; |
| 1305 | sSubParse.nQueryLoop = pParse->nQueryLoop; |
| 1306 | sSubParse.prepFlags = pParse->prepFlags; |
| 1307 | sSubParse.oldmask = 0; |
| 1308 | sSubParse.newmask = 0; |
| 1309 | |
| 1310 | v = sqlite3GetVdbe(&sSubParse); |
| 1311 | if( v ){ |
| 1312 | VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)", |
| 1313 | pTrigger->zName, onErrorText(orconf), |
| 1314 | (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"), |
| 1315 | (pTrigger->op==TK_UPDATE ? "UPDATE" : ""), |
| 1316 | (pTrigger->op==TK_INSERT ? "INSERT" : ""), |
| 1317 | (pTrigger->op==TK_DELETE ? "DELETE" : ""), |
| 1318 | pTab->zName |
| 1319 | )); |
| 1320 | #ifndef SQLITE_OMIT_TRACE |
| 1321 | if( pTrigger->zName ){ |
| 1322 | sqlite3VdbeChangeP4(v, -1, |
| 1323 | sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC |
| 1324 | ); |
| 1325 | } |
| 1326 | #endif |
| 1327 | |
| 1328 | /* If one was specified, code the WHEN clause. If it evaluates to false |
| 1329 | ** (or NULL) the sub-vdbe is immediately halted by jumping to the |
| 1330 | ** OP_Halt inserted at the end of the program. */ |
| 1331 | if( pTrigger->pWhen ){ |
| 1332 | pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0); |
| 1333 | if( db->mallocFailed==0 |
| 1334 | && SQLITE_OK==sqlite3ResolveExprNames(&sNC, pWhen) |
| 1335 | ){ |
| 1336 | iEndTrigger = sqlite3VdbeMakeLabel(&sSubParse); |
| 1337 | sqlite3ExprIfFalse(&sSubParse, pWhen, iEndTrigger, SQLITE_JUMPIFNULL); |
| 1338 | } |
| 1339 | sqlite3ExprDelete(db, pWhen); |
| 1340 | } |
| 1341 | |
| 1342 | /* Code the trigger program into the sub-vdbe. */ |
| 1343 | codeTriggerProgram(&sSubParse, pTrigger->step_list, orconf); |
| 1344 | |
| 1345 | /* Insert an OP_Halt at the end of the sub-program. */ |
| 1346 | if( iEndTrigger ){ |
| 1347 | sqlite3VdbeResolveLabel(v, iEndTrigger); |
| 1348 | } |
| 1349 | sqlite3VdbeAddOp0(v, OP_Halt); |
| 1350 | VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf))); |
| 1351 | transferParseError(pParse, &sSubParse); |
| 1352 | |
| 1353 | if( pParse->nErr==0 ){ |
| 1354 | assert( db->mallocFailed==0 ); |
| 1355 | pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg); |
| 1356 | } |
| 1357 | pProgram->nMem = sSubParse.nMem; |
| 1358 | pProgram->nCsr = sSubParse.nTab; |
| 1359 | pProgram->token = (void *)pTrigger; |
| 1360 | pPrg->aColmask[0] = sSubParse.oldmask; |
| 1361 | pPrg->aColmask[1] = sSubParse.newmask; |
| 1362 | sqlite3VdbeDelete(v); |
| 1363 | }else{ |
| 1364 | transferParseError(pParse, &sSubParse); |
| 1365 | } |
| 1366 | |
| 1367 | assert( !sSubParse.pTriggerPrg && !sSubParse.nMaxArg ); |
| 1368 | sqlite3ParseObjectReset(&sSubParse); |
| 1369 | return pPrg; |
| 1370 | } |
| 1371 | |
| 1372 | /* |
| 1373 | ** Return a pointer to a TriggerPrg object containing the sub-program for |
| 1374 | ** trigger pTrigger with default ON CONFLICT algorithm orconf. If no such |
| 1375 | ** TriggerPrg object exists, a new object is allocated and populated before |
| 1376 | ** being returned. |
| 1377 | */ |
| 1378 | static TriggerPrg *getRowTrigger( |
| 1379 | Parse *pParse, /* Current parse context */ |
| 1380 | Trigger *pTrigger, /* Trigger to code */ |
| 1381 | Table *pTab, /* The table trigger pTrigger is attached to */ |
| 1382 | int orconf /* ON CONFLICT algorithm. */ |
| 1383 | ){ |
| 1384 | Parse *pRoot = sqlite3ParseToplevel(pParse); |
| 1385 | TriggerPrg *pPrg; |
| 1386 | |
| 1387 | assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) ); |
| 1388 | |
| 1389 | /* It may be that this trigger has already been coded (or is in the |
| 1390 | ** process of being coded). If this is the case, then an entry with |
| 1391 | ** a matching TriggerPrg.pTrigger field will be present somewhere |
| 1392 | ** in the Parse.pTriggerPrg list. Search for such an entry. */ |
| 1393 | for(pPrg=pRoot->pTriggerPrg; |
| 1394 | pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf); |
| 1395 | pPrg=pPrg->pNext |
| 1396 | ); |
| 1397 | |
| 1398 | /* If an existing TriggerPrg could not be located, create a new one. */ |
| 1399 | if( !pPrg ){ |
| 1400 | pPrg = codeRowTrigger(pParse, pTrigger, pTab, orconf); |
| 1401 | pParse->db->errByteOffset = -1; |
| 1402 | } |
| 1403 | |
| 1404 | return pPrg; |
| 1405 | } |
| 1406 | |
| 1407 | /* |
| 1408 | ** Generate code for the trigger program associated with trigger p on |
| 1409 | ** table pTab. The reg, orconf and ignoreJump parameters passed to this |
| 1410 | ** function are the same as those described in the header function for |
| 1411 | ** sqlite3CodeRowTrigger() |
| 1412 | */ |
| 1413 | void sqlite3CodeRowTriggerDirect( |
| 1414 | Parse *pParse, /* Parse context */ |
| 1415 | Trigger *p, /* Trigger to code */ |
| 1416 | Table *pTab, /* The table to code triggers from */ |
| 1417 | int reg, /* Reg array containing OLD.* and NEW.* values */ |
| 1418 | int orconf, /* ON CONFLICT policy */ |
| 1419 | int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */ |
| 1420 | ){ |
| 1421 | Vdbe *v = sqlite3GetVdbe(pParse); /* Main VM */ |
| 1422 | TriggerPrg *pPrg; |
| 1423 | pPrg = getRowTrigger(pParse, p, pTab, orconf); |
| 1424 | assert( pPrg || pParse->nErr ); |
| 1425 | |
| 1426 | /* Code the OP_Program opcode in the parent VDBE. P4 of the OP_Program |
| 1427 | ** is a pointer to the sub-vdbe containing the trigger program. */ |
| 1428 | if( pPrg ){ |
| 1429 | int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers)); |
| 1430 | |
| 1431 | sqlite3VdbeAddOp4(v, OP_Program, reg, ignoreJump, ++pParse->nMem, |
| 1432 | (const char *)pPrg->pProgram, P4_SUBPROGRAM); |
| 1433 | VdbeComment( |
| 1434 | (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf))); |
| 1435 | |
| 1436 | /* Set the P5 operand of the OP_Program instruction to non-zero if |
| 1437 | ** recursive invocation of this trigger program is disallowed. Recursive |
| 1438 | ** invocation is disallowed if (a) the sub-program is really a trigger, |
| 1439 | ** not a foreign key action, and (b) the flag to enable recursive triggers |
| 1440 | ** is clear. */ |
| 1441 | sqlite3VdbeChangeP5(v, (u16)bRecursive); |
| 1442 | } |
| 1443 | } |
| 1444 | |
| 1445 | /* |
| 1446 | ** This is called to code the required FOR EACH ROW triggers for an operation |
| 1447 | ** on table pTab. The operation to code triggers for (INSERT, UPDATE or DELETE) |
| 1448 | ** is given by the op parameter. The tr_tm parameter determines whether the |
| 1449 | ** BEFORE or AFTER triggers are coded. If the operation is an UPDATE, then |
| 1450 | ** parameter pChanges is passed the list of columns being modified. |
| 1451 | ** |
| 1452 | ** If there are no triggers that fire at the specified time for the specified |
| 1453 | ** operation on pTab, this function is a no-op. |
| 1454 | ** |
| 1455 | ** The reg argument is the address of the first in an array of registers |
| 1456 | ** that contain the values substituted for the new.* and old.* references |
| 1457 | ** in the trigger program. If N is the number of columns in table pTab |
| 1458 | ** (a copy of pTab->nCol), then registers are populated as follows: |
| 1459 | ** |
| 1460 | ** Register Contains |
| 1461 | ** ------------------------------------------------------ |
| 1462 | ** reg+0 OLD.rowid |
| 1463 | ** reg+1 OLD.* value of left-most column of pTab |
| 1464 | ** ... ... |
| 1465 | ** reg+N OLD.* value of right-most column of pTab |
| 1466 | ** reg+N+1 NEW.rowid |
| 1467 | ** reg+N+2 NEW.* value of left-most column of pTab |
| 1468 | ** ... ... |
| 1469 | ** reg+N+N+1 NEW.* value of right-most column of pTab |
| 1470 | ** |
| 1471 | ** For ON DELETE triggers, the registers containing the NEW.* values will |
| 1472 | ** never be accessed by the trigger program, so they are not allocated or |
| 1473 | ** populated by the caller (there is no data to populate them with anyway). |
| 1474 | ** Similarly, for ON INSERT triggers the values stored in the OLD.* registers |
| 1475 | ** are never accessed, and so are not allocated by the caller. So, for an |
| 1476 | ** ON INSERT trigger, the value passed to this function as parameter reg |
| 1477 | ** is not a readable register, although registers (reg+N) through |
| 1478 | ** (reg+N+N+1) are. |
| 1479 | ** |
| 1480 | ** Parameter orconf is the default conflict resolution algorithm for the |
| 1481 | ** trigger program to use (REPLACE, IGNORE etc.). Parameter ignoreJump |
| 1482 | ** is the instruction that control should jump to if a trigger program |
| 1483 | ** raises an IGNORE exception. |
| 1484 | */ |
| 1485 | void sqlite3CodeRowTrigger( |
| 1486 | Parse *pParse, /* Parse context */ |
| 1487 | Trigger *pTrigger, /* List of triggers on table pTab */ |
| 1488 | int op, /* One of TK_UPDATE, TK_INSERT, TK_DELETE */ |
| 1489 | ExprList *pChanges, /* Changes list for any UPDATE OF triggers */ |
| 1490 | int tr_tm, /* One of TRIGGER_BEFORE, TRIGGER_AFTER */ |
| 1491 | Table *pTab, /* The table to code triggers from */ |
| 1492 | int reg, /* The first in an array of registers (see above) */ |
| 1493 | int orconf, /* ON CONFLICT policy */ |
| 1494 | int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */ |
| 1495 | ){ |
| 1496 | Trigger *p; /* Used to iterate through pTrigger list */ |
| 1497 | |
| 1498 | assert( op==TK_UPDATE || op==TK_INSERT || op==TK_DELETE ); |
| 1499 | assert( tr_tm==TRIGGER_BEFORE || tr_tm==TRIGGER_AFTER ); |
| 1500 | assert( (op==TK_UPDATE)==(pChanges!=0) ); |
| 1501 | |
| 1502 | for(p=pTrigger; p; p=p->pNext){ |
| 1503 | |
| 1504 | /* Sanity checking: The schema for the trigger and for the table are |
| 1505 | ** always defined. The trigger must be in the same schema as the table |
| 1506 | ** or else it must be a TEMP trigger. */ |
| 1507 | assert( p->pSchema!=0 ); |
| 1508 | assert( p->pTabSchema!=0 ); |
| 1509 | assert( p->pSchema==p->pTabSchema |
| 1510 | || p->pSchema==pParse->db->aDb[1].pSchema ); |
| 1511 | |
| 1512 | /* Determine whether we should code this trigger. One of two choices: |
| 1513 | ** 1. The trigger is an exact match to the current DML statement |
| 1514 | ** 2. This is a RETURNING trigger for INSERT but we are currently |
| 1515 | ** doing the UPDATE part of an UPSERT. |
| 1516 | */ |
| 1517 | if( (p->op==op || (p->bReturning && p->op==TK_INSERT && op==TK_UPDATE)) |
| 1518 | && p->tr_tm==tr_tm |
| 1519 | && checkColumnOverlap(p->pColumns, pChanges) |
| 1520 | ){ |
| 1521 | if( !p->bReturning ){ |
| 1522 | sqlite3CodeRowTriggerDirect(pParse, p, pTab, reg, orconf, ignoreJump); |
| 1523 | }else if( sqlite3IsToplevel(pParse) ){ |
| 1524 | codeReturningTrigger(pParse, p, pTab, reg); |
| 1525 | } |
| 1526 | } |
| 1527 | } |
| 1528 | } |
| 1529 | |
| 1530 | /* |
| 1531 | ** Triggers may access values stored in the old.* or new.* pseudo-table. |
| 1532 | ** This function returns a 32-bit bitmask indicating which columns of the |
| 1533 | ** old.* or new.* tables actually are used by triggers. This information |
| 1534 | ** may be used by the caller, for example, to avoid having to load the entire |
| 1535 | ** old.* record into memory when executing an UPDATE or DELETE command. |
| 1536 | ** |
| 1537 | ** Bit 0 of the returned mask is set if the left-most column of the |
| 1538 | ** table may be accessed using an [old|new].<col> reference. Bit 1 is set if |
| 1539 | ** the second leftmost column value is required, and so on. If there |
| 1540 | ** are more than 32 columns in the table, and at least one of the columns |
| 1541 | ** with an index greater than 32 may be accessed, 0xffffffff is returned. |
| 1542 | ** |
| 1543 | ** It is not possible to determine if the old.rowid or new.rowid column is |
| 1544 | ** accessed by triggers. The caller must always assume that it is. |
| 1545 | ** |
| 1546 | ** Parameter isNew must be either 1 or 0. If it is 0, then the mask returned |
| 1547 | ** applies to the old.* table. If 1, the new.* table. |
| 1548 | ** |
| 1549 | ** Parameter tr_tm must be a mask with one or both of the TRIGGER_BEFORE |
| 1550 | ** and TRIGGER_AFTER bits set. Values accessed by BEFORE triggers are only |
| 1551 | ** included in the returned mask if the TRIGGER_BEFORE bit is set in the |
| 1552 | ** tr_tm parameter. Similarly, values accessed by AFTER triggers are only |
| 1553 | ** included in the returned mask if the TRIGGER_AFTER bit is set in tr_tm. |
| 1554 | */ |
| 1555 | u32 sqlite3TriggerColmask( |
| 1556 | Parse *pParse, /* Parse context */ |
| 1557 | Trigger *pTrigger, /* List of triggers on table pTab */ |
| 1558 | ExprList *pChanges, /* Changes list for any UPDATE OF triggers */ |
| 1559 | int isNew, /* 1 for new.* ref mask, 0 for old.* ref mask */ |
| 1560 | int tr_tm, /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */ |
| 1561 | Table *pTab, /* The table to code triggers from */ |
| 1562 | int orconf /* Default ON CONFLICT policy for trigger steps */ |
| 1563 | ){ |
| 1564 | const int op = pChanges ? TK_UPDATE : TK_DELETE; |
| 1565 | u32 mask = 0; |
| 1566 | Trigger *p; |
| 1567 | |
| 1568 | assert( isNew==1 || isNew==0 ); |
| 1569 | if( IsView(pTab) ){ |
| 1570 | return 0xffffffff; |
| 1571 | } |
| 1572 | for(p=pTrigger; p; p=p->pNext){ |
| 1573 | if( p->op==op |
| 1574 | && (tr_tm&p->tr_tm) |
| 1575 | && checkColumnOverlap(p->pColumns,pChanges) |
| 1576 | ){ |
| 1577 | if( p->bReturning ){ |
| 1578 | mask = 0xffffffff; |
| 1579 | }else{ |
| 1580 | TriggerPrg *pPrg; |
| 1581 | pPrg = getRowTrigger(pParse, p, pTab, orconf); |
| 1582 | if( pPrg ){ |
| 1583 | mask |= pPrg->aColmask[isNew]; |
| 1584 | } |
| 1585 | } |
| 1586 | } |
| 1587 | } |
| 1588 | |
| 1589 | return mask; |
| 1590 | } |
| 1591 | |
| 1592 | #endif /* !defined(SQLITE_OMIT_TRIGGER) */ |