Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Added FOR EACH ROW triggers functionality (CVS 562) |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
794bf67b6b36fce8854d5daff12f21db |
User & Date: | danielk1977 2002-05-15 08:30:13.000 |
Context
2002-05-15
| ||
08:43 | Add the Makefile.in that was forgotten with checkin #562 (CVS 563) (check-in: 29b8330ca6 user: danielk1977 tags: trunk) | |
08:30 | Added FOR EACH ROW triggers functionality (CVS 562) (check-in: 794bf67b6b user: danielk1977 tags: trunk) | |
2002-05-10
| ||
14:41 | Version 2.4.12 (CVS 561) (check-in: 06cdaf1c80 user: drh tags: trunk) | |
Changes
Changes to src/build.c.
︙ | |||
21 22 23 24 25 26 27 | 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | - + | ** COPY ** VACUUM ** BEGIN TRANSACTION ** COMMIT ** ROLLBACK ** PRAGMA ** |
︙ | |||
246 247 248 249 250 251 252 253 254 255 256 257 258 259 | 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | + + + + + + + + + + + + + + + + | pIndex->isCommit = 1; } while( (pElem=sqliteHashFirst(&db->idxDrop))!=0 ){ Index *pIndex = sqliteHashData(pElem); sqliteUnlinkAndDeleteIndex(db, pIndex); } sqliteHashClear(&db->idxDrop); /* Set the commit flag on all triggers added this transaction */ for(pElem=sqliteHashFirst(&db->trigHash); pElem; pElem=sqliteHashNext(pElem)){ Trigger *pTrigger = sqliteHashData(pElem); pTrigger->isCommit = 1; } /* Delete the structures for triggers removed this transaction */ pElem = sqliteHashFirst(&db->trigDrop); while (pElem) { Trigger *pTrigger = sqliteHashData(pElem); sqliteDeleteTrigger(pTrigger); pElem = sqliteHashNext(pElem); } sqliteHashClear(&db->trigDrop); db->flags &= ~SQLITE_InternChanges; } /* ** This routine runs when one or more CREATE TABLE, CREATE INDEX, ** DROP TABLE, or DROP INDEX statements gets rolled back. The ** additions or deletions of Table and Index structures in the |
︙ | |||
300 301 302 303 304 305 306 307 308 309 310 311 312 313 | 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 | + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + | Index *pOld, *p = sqliteHashData(pElem); assert( p->isCommit ); p->isDropped = 0; pOld = sqliteHashInsert(&db->idxHash, p->zName, strlen(p->zName)+1, p); assert( pOld==0 || pOld==p ); } sqliteHashClear(&db->idxDrop); /* Remove any triggers that haven't been commited yet */ for(pElem = sqliteHashFirst(&db->trigHash); pElem; pElem = (pElem?sqliteHashNext(pElem):0)) { Trigger * pTrigger = sqliteHashData(pElem); if (!pTrigger->isCommit) { Table * tbl = sqliteFindTable(db, pTrigger->table); if (tbl) { if (tbl->pTrigger == pTrigger) tbl->pTrigger = pTrigger->pNext; else { Trigger * cc = tbl->pTrigger; while (cc) { if (cc->pNext == pTrigger) { cc->pNext = cc->pNext->pNext; break; } cc = cc->pNext; } assert(cc); } } sqliteHashInsert(&db->trigHash, pTrigger->name, 1 + strlen(pTrigger->name), 0); sqliteDeleteTrigger(pTrigger); pElem = sqliteHashFirst(&db->trigHash); } } /* Any triggers that were dropped - put 'em back in place */ for(pElem = sqliteHashFirst(&db->trigDrop); pElem; pElem = sqliteHashNext(pElem)) { Trigger * pTrigger = sqliteHashData(pElem); Table * tab = sqliteFindTable(db, pTrigger->table); sqliteHashInsert(&db->trigHash, pTrigger->name, strlen(pTrigger->name) + 1, pTrigger); pTrigger->pNext = tab->pTrigger; tab->pTrigger = pTrigger; } sqliteHashClear(&db->trigDrop); db->flags &= ~SQLITE_InternChanges; } /* ** Construct the name of a user table or index from a token. ** ** Space to hold the name is obtained from sqliteMalloc() and must |
︙ | |||
591 592 593 594 595 596 597 | 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 | - + | ** ** This plan is not completely bullet-proof. It is possible for ** the schema to change multiple times and for the cookie to be ** set back to prior value. But schema changes are infrequent ** and the probability of hitting the same cookie value is only ** 1 chance in 2^32. So we're safe enough. */ |
︙ | |||
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 | 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 | + + + + + + + | { OP_Next, 0, ADDR(4), 0}, /* 8 */ { OP_Integer, 0, 0, 0}, /* 9 */ { OP_SetCookie, 0, 0, 0}, { OP_Close, 0, 0, 0}, }; Index *pIdx; sqliteBeginWriteOperation(pParse); /* Drop all triggers associated with the table being dropped */ while (pTable->pTrigger) { Token tt; tt.z = pTable->pTrigger->name; tt.n = strlen(pTable->pTrigger->name); sqliteDropTrigger(pParse, &tt, 1); } if( !pTable->isTemp ){ base = sqliteVdbeAddOpList(v, ArraySize(dropTable), dropTable); sqliteVdbeChangeP3(v, base+2, pTable->zName, 0); changeCookie(db); sqliteVdbeChangeP1(v, base+9, db->next_cookie); } if( !isView ){ |
︙ | |||
1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 | 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 | + + + | ** all. So there is not need to set a checkpoint is a transaction ** is already in effect. */ void sqliteBeginWriteOperation(Parse *pParse){ Vdbe *v; v = sqliteGetVdbe(pParse); if( v==0 ) return; if (pParse->trigStack) return; /* if this is in a trigger */ if( (pParse->db->flags & SQLITE_InTrans)==0 ){ sqliteVdbeAddOp(v, OP_Transaction, 0, 0); sqliteVdbeAddOp(v, OP_VerifyCookie, pParse->db->schema_cookie, 0); pParse->schemaVerified = 1; } } /* ** Generate VDBE code that prepares for doing an operation that ** might change the database. The operation might not be atomic in ** the sense that an error may be discovered and the operation might ** abort after some changes have been made. If we are in the middle ** of a transaction, then this sets a checkpoint. If we are not in ** a transaction, then start a transaction. */ void sqliteBeginMultiWriteOperation(Parse *pParse){ Vdbe *v; v = sqliteGetVdbe(pParse); if( v==0 ) return; if (pParse->trigStack) return; /* if this is in a trigger */ if( (pParse->db->flags & SQLITE_InTrans)==0 ){ sqliteVdbeAddOp(v, OP_Transaction, 0, 0); sqliteVdbeAddOp(v, OP_VerifyCookie, pParse->db->schema_cookie, 0); pParse->schemaVerified = 1; }else{ sqliteVdbeAddOp(v, OP_Checkpoint, 0, 0); } } /* ** Generate code that concludes an operation that may have changed ** the database. This is a companion function to BeginWriteOperation(). ** If a transaction was started, then commit it. If a checkpoint was ** started then commit that. */ void sqliteEndWriteOperation(Parse *pParse){ Vdbe *v; if (pParse->trigStack) return; /* if this is in a trigger */ v = sqliteGetVdbe(pParse); if( v==0 ) return; if( pParse->db->flags & SQLITE_InTrans ){ /* Do Nothing */ }else{ sqliteVdbeAddOp(v, OP_Commit, 0, 0); } |
︙ | |||
1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 | 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 | + + + + + + + + | int size = db->cache_size; if( size<0 ) size = -size; if( !getBoolean(zRight) ) size = -size; db->cache_size = size; sqliteBtreeSetCacheSize(db->pBe, db->cache_size); } }else if( sqliteStrICmp(zLeft, "trigger_overhead_test")==0 ){ if( getBoolean(zRight) ){ always_code_trigger_setup = 1; }else{ always_code_trigger_setup = 0; } }else if( sqliteStrICmp(zLeft, "vdbe_trace")==0 ){ if( getBoolean(zRight) ){ db->flags |= SQLITE_VdbeTrace; }else{ db->flags &= ~SQLITE_VdbeTrace; } |
︙ |
Changes to src/delete.c.
︙ | |||
8 9 10 11 12 13 14 | 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | - + | ** May you find forgiveness for yourself and forgive others. ** May you share freely, never taking more than you give. ** ************************************************************************* ** This file contains C code routines that are called by the parser ** to handle DELETE FROM statements. ** |
︙ | |||
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - + | int i; /* Loop counter */ WhereInfo *pWInfo; /* Information about the WHERE clause */ Index *pIdx; /* For looping over indices of the table */ int base; /* Index of the first available table cursor */ sqlite *db; /* Main database structure */ int openOp; /* Opcode used to open a cursor to the table */ int row_triggers_exist = 0; int oldIdx = -1; if( pParse->nErr || sqlite_malloc_failed ){ pTabList = 0; goto delete_from_cleanup; } db = pParse->db; /* Check for the special case of a VIEW with one or more ON DELETE triggers * defined */ { Table * pTab; char * zTab = sqliteTableNameFromToken(pTableName); if(zTab != 0) { pTab = sqliteFindTable(pParse->db, zTab); if (pTab) { row_triggers_exist = sqliteTriggersExist(pParse, pTab->pTrigger, TK_DELETE, TK_BEFORE, TK_ROW, 0) || sqliteTriggersExist(pParse, pTab->pTrigger, TK_DELETE, TK_AFTER, TK_ROW, 0); } sqliteFree(zTab); if (row_triggers_exist && pTab->pSelect ) { /* Just fire VIEW triggers */ sqliteViewTriggers(pParse, pTab, pWhere, OE_Replace, 0); return; } } } /* Locate the table which we want to delete. This table has to be ** put in an IdList structure because some of the subroutines we ** will be calling are designed to work with multiple tables and expect ** an IdList* parameter instead of just a Table* parameger. */ pTabList = sqliteTableTokenToIdList(pParse, pTableName); if( pTabList==0 ) goto delete_from_cleanup; assert( pTabList->nId==1 ); pTab = pTabList->a[0].pTab; assert( pTab->pSelect==0 ); /* This table is not a view */ if (row_triggers_exist) oldIdx = pParse->nTab++; /* Resolve the column names in all the expressions. */ base = pParse->nTab++; if( pWhere ){ if( sqliteExprResolveIds(pParse, base, pTabList, 0, pWhere) ){ goto delete_from_cleanup; } if( sqliteExprCheck(pParse, pWhere, 0, 0) ){ goto delete_from_cleanup; } } /* Begin generating code. */ v = sqliteGetVdbe(pParse); if( v==0 ) goto delete_from_cleanup; if (row_triggers_exist) sqliteBeginMultiWriteOperation(pParse); else |
︙ | |||
172 173 174 175 176 177 178 179 180 181 | 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - - - + + + + + + + + + + + + + + + + + + + + + + + | sqliteWhereEnd(pWInfo); /* Delete every item whose key was written to the list during the ** database scan. We have to delete items after the scan is complete ** because deleting an item can change the scan order. */ sqliteVdbeAddOp(v, OP_ListRewind, 0, 0); end = sqliteVdbeMakeLabel(v); if (row_triggers_exist) { int ii; addr = sqliteVdbeAddOp(v, OP_ListRead, 0, end); sqliteVdbeAddOp(v, OP_Dup, 0, 0); openOp = pTab->isTemp ? OP_OpenAux : OP_Open; sqliteVdbeAddOp(v, openOp, base, pTab->tnum); sqliteVdbeAddOp(v, OP_MoveTo, base, 0); sqliteVdbeAddOp(v, OP_OpenTemp, oldIdx, 0); sqliteVdbeAddOp(v, OP_Integer, 13, 0); for (ii = 0; ii < pTab->nCol; ii++) { if (ii == pTab->iPKey) sqliteVdbeAddOp(v, OP_Recno, base, 0); else sqliteVdbeAddOp(v, OP_Column, base, ii); } sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0); sqliteVdbeAddOp(v, OP_PutIntKey, oldIdx, 0); sqliteVdbeAddOp(v, OP_Close, base, 0); sqliteVdbeAddOp(v, OP_Rewind, oldIdx, 0); sqliteCodeRowTrigger(pParse, TK_DELETE, 0, TK_BEFORE, pTab, -1, oldIdx, (pParse->trigStack)?pParse->trigStack->orconf:OE_Default); } pParse->nTab = base + 1; openOp = pTab->isTemp ? OP_OpenWrAux : OP_OpenWrite; sqliteVdbeAddOp(v, openOp, base, pTab->tnum); for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){ |
︙ |
Changes to src/expr.c.
︙ | |||
8 9 10 11 12 13 14 | 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | - + | ** May you find forgiveness for yourself and forgive others. ** May you share freely, never taking more than you give. ** ************************************************************************* ** This file contains routines used for analyzing expressions and ** for generating VDBE code that evaluates expressions in SQLite. ** |
︙ | |||
477 478 479 480 481 482 483 484 485 486 487 488 489 490 | 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 | + + + + + + + + + + + + + + + + + + + + + + + + + + + | pExpr->iColumn = -1; }else{ pExpr->iColumn = j; } } } } /* If we have not already resolved this *.* expression, then maybe * it is a new.* or old.* trigger argument reference */ if (cnt == 0 && pParse->trigStack != 0) { TriggerStack * tt = pParse->trigStack; int j; int t = 0; if (tt->newIdx != -1 && sqliteStrICmp("new", zLeft) == 0) { pExpr->iTable = tt->newIdx; cntTab++; t = 1; } if (tt->oldIdx != -1 && sqliteStrICmp("old", zLeft) == 0) { pExpr->iTable = tt->oldIdx; cntTab++; t = 1; } if (t) for(j=0; j<tt->pTab->nCol; j++) { if( sqliteStrICmp(tt->pTab->aCol[j].zName, zRight)==0 ){ cnt++; pExpr->iColumn = j; } } } if( cnt==0 && cntTab==1 && sqliteIsRowid(zRight) ){ cnt = 1; pExpr->iColumn = -1; } sqliteFree(zLeft); sqliteFree(zRight); if( cnt==0 ){ |
︙ |
Changes to src/insert.c.
︙ | |||
8 9 10 11 12 13 14 | 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | - + | ** May you find forgiveness for yourself and forgive others. ** May you share freely, never taking more than you give. ** ************************************************************************* ** This file contains C code routines that are called by the parser ** to handle INSERT statements in SQLite. ** |
︙ | |||
36 37 38 39 40 41 42 | 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | - + + + + + + + + + + - + + + + + + + + + + + + + + + + + + + - - + + + + + | Token *pTableName, /* Name of table into which we are inserting */ ExprList *pList, /* List of values to be inserted */ Select *pSelect, /* A SELECT statement to use as the data source */ IdList *pColumn, /* Column names corresponding to IDLIST. */ int onError /* How to handle constraint errors */ ){ Table *pTab; /* The table to insert into */ |
︙ | |||
169 170 171 172 173 174 175 | 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 | - - + + + + + + + + + + + - - - - - - - - - - + + + + + + + + + + - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - + + + + + + - - - - - - - - - - - - + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - + + + + + + - - - - + + + + + + + + + + + + + + + + + + + + + - + + + + - - - - + + + + + + - + - + + | ** key, the set the keyColumn variable to the primary key column index ** in the original table definition. */ if( pColumn==0 ){ keyColumn = pTab->iPKey; } |
︙ | |||
569 570 571 572 573 574 575 | 672 673 674 675 676 677 678 679 680 681 682 683 | - + | assert( pTab->pSelect==0 ); /* This table is not a VIEW */ for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){} for(i=nIdx-1; i>=0; i--){ if( aIdxUsed && aIdxUsed[i]==0 ) continue; sqliteVdbeAddOp(v, OP_IdxPut, base+i+1, 0); } sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0); |
Changes to src/main.c.
︙ | |||
10 11 12 13 14 15 16 | 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | - + | ** ************************************************************************* ** Main file for the SQLite library. The routines in this file ** implement the programmer interface to the library. Routines in ** other files are for internal use by SQLite and should not be ** accessed by users of the library. ** |
︙ | |||
322 323 324 325 326 327 328 329 330 331 332 333 334 335 | 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 | + + | /* Allocate the sqlite data structure */ db = sqliteMalloc( sizeof(sqlite) ); if( pzErrMsg ) *pzErrMsg = 0; if( db==0 ) goto no_mem_on_open; sqliteHashInit(&db->tblHash, SQLITE_HASH_STRING, 0); sqliteHashInit(&db->idxHash, SQLITE_HASH_STRING, 0); sqliteHashInit(&db->trigHash, SQLITE_HASH_STRING, 0); sqliteHashInit(&db->trigDrop, SQLITE_HASH_STRING, 0); sqliteHashInit(&db->tblDrop, SQLITE_HASH_POINTER, 0); sqliteHashInit(&db->idxDrop, SQLITE_HASH_POINTER, 0); sqliteHashInit(&db->aFunc, SQLITE_HASH_STRING, 1); sqliteRegisterBuildinFunctions(db); db->onError = OE_Default; db->priorNewRowid = 0; db->magic = SQLITE_MAGIC_BUSY; |
︙ | |||
379 380 381 382 383 384 385 386 387 388 | 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 | + + - + + + + + + + + + + + + + + + + + | ** This routine erases the stored schema. This erasure occurs because ** either the database is being closed or because some other process ** changed the schema and this process needs to reread it. */ static void clearHashTable(sqlite *db, int preserveTemps){ HashElem *pElem; Hash temp1; Hash temp2; assert( sqliteHashFirst(&db->tblDrop)==0 ); /* There can not be uncommitted */ assert( sqliteHashFirst(&db->idxDrop)==0 ); /* DROP TABLEs or DROP INDEXs */ temp1 = db->tblHash; temp2 = db->trigHash; |
︙ | |||
409 410 411 412 413 414 415 416 417 418 419 420 421 422 | 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 | + | } } }else{ sqliteDeleteTable(db, pTab); } } sqliteHashClear(&temp1); db->flags &= ~SQLITE_Initialized; } /* ** Return the ROWID of the most recent insert */ int sqlite_last_insert_rowid(sqlite *db){ |
︙ | |||
454 455 456 457 458 459 460 461 462 463 464 465 466 467 | 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 | + | } /* ** Return TRUE if the given SQL string ends in a semicolon. */ int sqlite_complete(const char *zSql){ int isComplete = 0; int seenCreate = 0; while( *zSql ){ switch( *zSql ){ case ';': { isComplete = 1; break; } case ' ': |
︙ | |||
497 498 499 500 501 502 503 504 505 506 507 508 509 510 | 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 | + + + + + + + + + + | break; } while( *zSql && *zSql!='\n' ){ zSql++; } if( *zSql==0 ) return isComplete; break; } default: { if (seenCreate && !sqliteStrNICmp(zSql, "trigger", 7)) while (sqliteStrNICmp(zSql, "end", 3)) if (!*++zSql) return 0; if (!sqliteStrNICmp(zSql, "create", 6)) { zSql = zSql + 5; seenCreate = 1; } else seenCreate = 0; isComplete = 0; break; } } zSql++; } return isComplete; |
︙ |
Changes to src/parse.y.
︙ | |||
10 11 12 13 14 15 16 | 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | - + + + + + + | ** ************************************************************************* ** This file contains SQLite's grammar for SQL. Process this file ** using the lemon parser generator to generate C code that runs ** the parser. Lemon will also generate a header file containing ** numeric codes for all of the tokens. ** |
︙ | |||
624 625 626 627 628 629 630 | 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 | + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + | cmd ::= PRAGMA ids(X). {sqlitePragma(pParse,&X,&X,0);} plus_num(A) ::= plus_opt number(X). {A = X;} minus_num(A) ::= MINUS number(X). {A = X;} number(A) ::= INTEGER(X). {A = X;} number(A) ::= FLOAT(X). {A = X;} plus_opt ::= PLUS. plus_opt ::= . //////////////////////////// The CREATE TRIGGER command ///////////////////// cmd ::= CREATE(A) TRIGGER ids(B) trigger_time(C) trigger_event(D) ON ids(E) foreach_clause(F) when_clause(G) BEGIN trigger_cmd_list(S) END(Z). { sqliteCreateTrigger(pParse, &B, C, D.a, D.b, &E, F, G, S, A.z, (int)(Z.z - A.z) + Z.n ); } %type trigger_time {int} trigger_time(A) ::= BEFORE. { A = TK_BEFORE; } trigger_time(A) ::= AFTER. { A = TK_AFTER; } trigger_time(A) ::= INSTEAD OF. { A = TK_INSTEAD;} trigger_time(A) ::= . { A = TK_BEFORE; } %type trigger_event {struct int_idlist} trigger_event(A) ::= DELETE. { A.a = TK_DELETE; A.b = 0; } trigger_event(A) ::= INSERT. { A.a = TK_INSERT; A.b = 0; } trigger_event(A) ::= UPDATE. { A.a = TK_UPDATE; A.b = 0;} trigger_event(A) ::= UPDATE OF inscollist(X). {A.a = TK_UPDATE; A.b = X; } %type foreach_clause {int} foreach_clause(A) ::= . { A = TK_ROW; } foreach_clause(A) ::= FOR EACH ROW. { A = TK_ROW; } foreach_clause(A) ::= FOR EACH STATEMENT. { A = TK_STATEMENT; } %type when_clause {Expr *} when_clause(A) ::= . { A = 0; } when_clause(A) ::= WHEN expr(X). { A = X; } %type trigger_cmd_list {TriggerStep *} trigger_cmd_list(A) ::= trigger_cmd(X) SEMI trigger_cmd_list(Y). { X->pNext = Y ; A = X; } trigger_cmd_list(A) ::= . { A = 0; } %type trigger_cmd {TriggerStep *} // UPDATE trigger_cmd(A) ::= UPDATE orconf(R) ids(X) SET setlist(Y) where_opt(Z). { A = sqliteTriggerUpdateStep(&X, Y, Z, R); } // INSERT trigger_cmd(A) ::= INSERT orconf(R) INTO ids(X) inscollist_opt(F) VALUES LP itemlist(Y) RP. {A = sqliteTriggerInsertStep(&X, F, Y, 0, R);} trigger_cmd(A) ::= INSERT orconf(R) INTO ids(X) inscollist_opt(F) select(S). {A = sqliteTriggerInsertStep(&X, F, 0, S, R);} // DELETE trigger_cmd(A) ::= DELETE FROM ids(X) where_opt(Y). {A = sqliteTriggerDeleteStep(&X, Y);} // SELECT trigger_cmd(A) ::= select(X). {A = sqliteTriggerSelectStep(X); } //////////////////////// DROP TRIGGER statement ////////////////////////////// cmd ::= DROP TRIGGER ids(X). { sqliteDropTrigger(pParse,&X,0); } |
Changes to src/sqliteInt.h.
1 2 3 4 5 6 7 8 9 10 11 12 13 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | - + | /* ** 2001 September 15 ** ** The author disclaims copyright to this source code. In place of ** a legal notice, here is a blessing: ** ** May you do good and not evil. ** May you find forgiveness for yourself and forgive others. ** May you share freely, never taking more than you give. ** ************************************************************************* ** Internal interface definitions for SQLite. ** |
︙ | |||
140 141 142 143 144 145 146 147 148 149 150 151 152 153 | 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | + + + | typedef struct Token Token; typedef struct IdList IdList; typedef struct WhereInfo WhereInfo; typedef struct WhereLevel WhereLevel; typedef struct Select Select; typedef struct AggExpr AggExpr; typedef struct FuncDef FuncDef; typedef struct Trigger Trigger; typedef struct TriggerStep TriggerStep; typedef struct TriggerStack TriggerStack; /* ** Each database is an instance of the following structure */ struct sqlite { Btree *pBe; /* The B*Tree backend */ Btree *pBeTemp; /* Backend for session temporary tables */ |
︙ | |||
166 167 168 169 170 171 172 173 174 175 176 177 178 179 | 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | + + + | Hash aFunc; /* All functions that can be in SQL exprs */ int lastRowid; /* ROWID of most recent insert */ int priorNewRowid; /* Last randomly generated ROWID */ int onError; /* Default conflict algorithm */ int magic; /* Magic number for detect library misuse */ int nChange; /* Number of rows changed */ int recursionDepth; /* Number of nested calls to sqlite_exec() */ Hash trigHash; /* All triggers indexed by name */ Hash trigDrop; /* Uncommited dropped triggers */ }; /* ** Possible values for the sqlite.flags. */ #define SQLITE_VdbeTrace 0x00000001 /* True to trace VDBE execution */ #define SQLITE_Initialized 0x00000002 /* True after initialization */ |
︙ | |||
266 267 268 269 270 271 272 273 274 275 276 277 278 279 | 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 | + + | Select *pSelect; /* NULL for tables. Points to definition if a view. */ u8 readOnly; /* True if this table should not be written by the user */ u8 isCommit; /* True if creation of this table has been committed */ u8 isTemp; /* True if stored in db->pBeTemp instead of db->pBe */ u8 isTransient; /* True if automatically deleted when VDBE finishes */ u8 hasPrimKey; /* True if there exists a primary key */ u8 keyConf; /* What to do in case of uniqueness conflict on iPKey */ Trigger *pTrigger; /* List of SQL triggers on this table */ }; /* ** SQLite supports 5 different ways to resolve a contraint ** error. ROLLBACK processing means that a constraint violation ** causes the operation in proces to fail and for the current transaction ** to be rolled back. ABORT processing means the operation in process |
︙ | |||
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 | 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 | + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + | int nSet; /* Number of sets used so far */ int nAgg; /* Number of aggregate expressions */ AggExpr *aAgg; /* An array of aggregate expressions */ int useAgg; /* If true, extract field values from the aggregator ** while generating expressions. Normally false */ int schemaVerified; /* True if an OP_VerifySchema has been coded someplace ** other than after an OP_Transaction */ TriggerStack * trigStack; }; struct TriggerStack { Trigger * pTrigger; Table * pTab; /* Table that triggers are currently being coded as */ int newIdx; /* Index of "new" temp table */ int oldIdx; /* Index of "old" temp table */ int orconf; /* Current orconf policy */ struct TriggerStack * pNext; }; struct TriggerStep { int op; /* One of TK_DELETE, TK_UPDATE, TK_INSERT, TK_SELECT */ int orconf; Select * pSelect; /* Valid for SELECT and sometimes INSERT steps (when pExprList == 0) */ Token target; /* Valid for DELETE, UPDATE, INSERT steps */ Expr * pWhere; /* Valid for DELETE, UPDATE steps */ ExprList * pExprList; /* Valid for UPDATE statements and sometimes INSERT steps (when pSelect == 0) */ IdList *pIdList; /* Valid for INSERT statements only */ TriggerStep * pNext; /* Next in the link-list */ }; struct Trigger { char * name; /* The name of the trigger */ char * table; /* The table or view to which the trigger applies */ int op; /* One of TK_DELETE, TK_UPDATE, TK_INSERT */ int tr_tm; /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */ Expr * pWhen; /* The WHEN clause of the expresion (may be NULL) */ IdList * pColumns; /* If this is an UPDATE OF <column-list> trigger, the column names are stored in this list */ int foreach; /* One of TK_ROW or TK_STATEMENT */ TriggerStep * step_list; /* Link list of trigger program steps */ char * strings; /* pointer to the allocation of Token strings */ Trigger * pNext; /* Next trigger associated with the table */ int isCommit; }; TriggerStep * sqliteTriggerSelectStep(Select *); TriggerStep * sqliteTriggerInsertStep(Token *, IdList *, ExprList *, Select *, int); TriggerStep * sqliteTriggerUpdateStep(Token *, ExprList *, Expr *, int); TriggerStep * sqliteTriggerDeleteStep(Token *, Expr *); extern int always_code_trigger_setup; void sqliteCreateTrigger(Parse * ,Token *, int, int, IdList *, Token *, int, Expr *, TriggerStep *, char const *,int); void sqliteDropTrigger(Parse *, Token *, int); int sqliteTriggersExist( Parse * , Trigger * , int , int , int, ExprList * ); int sqliteCodeRowTrigger( Parse * pParse, int op, ExprList *, int tr_tm, Table * tbl, int newTable, int oldTable, int onError); void sqliteViewTriggers(Parse *, Table *, Expr *, int, ExprList *); /* ** Internal function prototypes */ int sqliteStrICmp(const char *, const char *); int sqliteStrNICmp(const char *, const char *, int); int sqliteHashNoCase(const char *, int); int sqliteCompare(const char *, const char *); |
︙ | |||
658 659 660 661 662 663 664 | 722 723 724 725 726 727 728 729 730 | + + | IdList *sqliteIdListDup(IdList*); Select *sqliteSelectDup(Select*); FuncDef *sqliteFindFunction(sqlite*,const char*,int,int,int); void sqliteRegisterBuildinFunctions(sqlite*); int sqliteSafetyOn(sqlite*); int sqliteSafetyOff(sqlite*); int sqliteSafetyCheck(sqlite*); void changeCookie(sqlite *); |
Changes to src/tokenize.c.
︙ | |||
11 12 13 14 15 16 17 | 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | - + | ************************************************************************* ** An tokenizer for SQL ** ** This file contains C code that splits an SQL input string up into ** individual tokens and sends those tokens one-by-one over to the ** parser for analysis. ** |
︙ | |||
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | + + + + + + + + | }; /* ** These are the keywords */ static Keyword aKeywordTable[] = { { "ABORT", 0, TK_ABORT, 0 }, { "AFTER", 0, TK_AFTER, 0 }, { "ALL", 0, TK_ALL, 0 }, { "AND", 0, TK_AND, 0 }, { "AS", 0, TK_AS, 0 }, { "ASC", 0, TK_ASC, 0 }, { "BEFORE", 0, TK_BEFORE, 0 }, { "BEGIN", 0, TK_BEGIN, 0 }, { "BETWEEN", 0, TK_BETWEEN, 0 }, { "BY", 0, TK_BY, 0 }, { "CASE", 0, TK_CASE, 0 }, { "CHECK", 0, TK_CHECK, 0 }, { "CLUSTER", 0, TK_CLUSTER, 0 }, { "COMMIT", 0, TK_COMMIT, 0 }, { "CONFLICT", 0, TK_CONFLICT, 0 }, { "CONSTRAINT", 0, TK_CONSTRAINT, 0 }, { "COPY", 0, TK_COPY, 0 }, { "CREATE", 0, TK_CREATE, 0 }, { "DEFAULT", 0, TK_DEFAULT, 0 }, { "DELETE", 0, TK_DELETE, 0 }, { "DELIMITERS", 0, TK_DELIMITERS, 0 }, { "DESC", 0, TK_DESC, 0 }, { "DISTINCT", 0, TK_DISTINCT, 0 }, { "DROP", 0, TK_DROP, 0 }, { "END", 0, TK_END, 0 }, { "EACH", 0, TK_EACH, 0 }, { "ELSE", 0, TK_ELSE, 0 }, { "EXCEPT", 0, TK_EXCEPT, 0 }, { "EXPLAIN", 0, TK_EXPLAIN, 0 }, { "FAIL", 0, TK_FAIL, 0 }, { "FOR", 0, TK_FOR, 0 }, { "FROM", 0, TK_FROM, 0 }, { "GLOB", 0, TK_GLOB, 0 }, { "GROUP", 0, TK_GROUP, 0 }, { "HAVING", 0, TK_HAVING, 0 }, { "IGNORE", 0, TK_IGNORE, 0 }, { "IN", 0, TK_IN, 0 }, { "INDEX", 0, TK_INDEX, 0 }, { "INSERT", 0, TK_INSERT, 0 }, { "INSTEAD", 0, TK_INSTEAD, 0 }, { "INTERSECT", 0, TK_INTERSECT, 0 }, { "INTO", 0, TK_INTO, 0 }, { "IS", 0, TK_IS, 0 }, { "ISNULL", 0, TK_ISNULL, 0 }, { "KEY", 0, TK_KEY, 0 }, { "LIKE", 0, TK_LIKE, 0 }, { "LIMIT", 0, TK_LIMIT, 0 }, { "NOT", 0, TK_NOT, 0 }, { "NOTNULL", 0, TK_NOTNULL, 0 }, { "NULL", 0, TK_NULL, 0 }, { "OF", 0, TK_OF, 0 }, { "OFFSET", 0, TK_OFFSET, 0 }, { "ON", 0, TK_ON, 0 }, { "OR", 0, TK_OR, 0 }, { "ORDER", 0, TK_ORDER, 0 }, { "PRAGMA", 0, TK_PRAGMA, 0 }, { "PRIMARY", 0, TK_PRIMARY, 0 }, { "REPLACE", 0, TK_REPLACE, 0 }, { "ROLLBACK", 0, TK_ROLLBACK, 0 }, { "ROW", 0, TK_ROW, 0 }, { "SELECT", 0, TK_SELECT, 0 }, { "SET", 0, TK_SET, 0 }, { "TABLE", 0, TK_TABLE, 0 }, { "TEMP", 0, TK_TEMP, 0 }, { "TEMPORARY", 0, TK_TEMP, 0 }, { "THEN", 0, TK_THEN, 0 }, { "TRANSACTION", 0, TK_TRANSACTION, 0 }, { "TRIGGER", 0, TK_TRIGGER, 0 }, { "UNION", 0, TK_UNION, 0 }, { "UNIQUE", 0, TK_UNIQUE, 0 }, { "UPDATE", 0, TK_UPDATE, 0 }, { "USING", 0, TK_USING, 0 }, { "VACUUM", 0, TK_VACUUM, 0 }, { "VALUES", 0, TK_VALUES, 0 }, { "VIEW", 0, TK_VIEW, 0 }, |
︙ |
Added src/trigger.c.