Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Additional code cleanup resulting from a review of the new trigger code. (CVS 572) |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
37dbdd551e88440933066133ec9cc1e1 |
User & Date: | drh 2002-05-21 11:38:11 |
Context
2002-05-21
| ||
12:56 | Fix for ticket #45: Allow an UPDATE statement to change the INTEGER PRIMARY KEY to itself without triggering a constraint error. (CVS 573) check-in: 592da134 user: drh tags: trunk | |
11:38 | Additional code cleanup resulting from a review of the new trigger code. (CVS 572) check-in: 37dbdd55 user: drh tags: trunk | |
2002-05-19
| ||
23:43 | Style fixes to triggers code in various *.c files (partial fix to ticket #39) (CVS 571) check-in: 8a4195c7 user: danielk1977 tags: trunk | |
Changes
Changes to src/build.c.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 ... 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 .... 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 .... 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 .... 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 .... 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 .... 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 .... 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 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 .... 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 .... 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 |
** COPY ** VACUUM ** BEGIN TRANSACTION ** COMMIT ** ROLLBACK ** PRAGMA ** ** $Id: build.c,v 1.91 2002/05/19 23:43:14 danielk1977 Exp $ */ #include "sqliteInt.h" #include <ctype.h> /* ** This routine is called after a single SQL statement has been ** parsed and we want to execute the VDBE code to implement ................................................................................ ** and allocate the record number for the table entry now. Before any ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause ** indices to be created and the table record must come before the ** indices. Hence, the record number for the table must be allocated ** now. */ if( !pParse->initFlag && (v = sqliteGetVdbe(pParse))!=0 ){ sqliteBeginWriteOperation(pParse); if( !isTemp ){ sqliteVdbeAddOp(v, OP_Integer, db->file_format, 0); sqliteVdbeAddOp(v, OP_SetCookie, 0, 1); sqliteVdbeAddOp(v, OP_OpenWrite, 0, 2); sqliteVdbeChangeP3(v, -1, MASTER_NAME, P3_STATIC); sqliteVdbeAddOp(v, OP_NewRecno, 0, 0); sqliteVdbeAddOp(v, OP_Dup, 0, 0); ................................................................................ { OP_Delete, 0, 0, 0}, { 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); } ................................................................................ int i; int addr; int isTemp = pTab->isTemp; v = sqliteGetVdbe(pParse); if( v==0 ) goto exit_create_index; if( pTable!=0 ){ sqliteBeginWriteOperation(pParse); if( !isTemp ){ sqliteVdbeAddOp(v, OP_OpenWrite, 0, 2); sqliteVdbeChangeP3(v, -1, MASTER_NAME, P3_STATIC); } } if( !isTemp ){ sqliteVdbeAddOp(v, OP_NewRecno, 0, 0); ................................................................................ { OP_Integer, 0, 0, 0}, /* 10 */ { OP_SetCookie, 0, 0, 0}, { OP_Close, 0, 0, 0}, }; int base; Table *pTab = pIndex->pTable; sqliteBeginWriteOperation(pParse); if( !pTab->isTemp ){ base = sqliteVdbeAddOpList(v, ArraySize(dropIndex), dropIndex); sqliteVdbeChangeP3(v, base+2, pIndex->zName, P3_STATIC); sqliteChangeCookie(db); sqliteVdbeChangeP1(v, base+10, db->next_cookie); } sqliteVdbeAddOp(v, OP_Destroy, pIndex->tnum, pTab->isTemp); ................................................................................ if( sqlite_malloc_failed || zTab==0 ) goto copy_cleanup; pTab = sqliteTableNameToTable(pParse, zTab); sqliteFree(zTab); if( pTab==0 ) goto copy_cleanup; v = sqliteGetVdbe(pParse); if( v ){ int openOp; sqliteBeginMultiWriteOperation(pParse); addr = sqliteVdbeAddOp(v, OP_FileOpen, 0, 0); sqliteVdbeChangeP3(v, addr, pFilename->z, pFilename->n); sqliteVdbeDequoteP3(v, addr); openOp = pTab->isTemp ? OP_OpenWrAux : OP_OpenWrite; sqliteVdbeAddOp(v, openOp, 0, pTab->tnum); sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC); for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ ................................................................................ */ void sqliteBeginTransaction(Parse *pParse, int onError){ sqlite *db; if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return; if( pParse->nErr || sqlite_malloc_failed ) return; if( db->flags & SQLITE_InTrans ) return; sqliteBeginWriteOperation(pParse); db->flags |= SQLITE_InTrans; db->onError = onError; } /* ** Commit a transaction */ ................................................................................ } db->flags &= ~SQLITE_InTrans; db->onError = OE_Default; } /* ** Generate VDBE code that prepares for doing an operation that ** might change the database. The operation will be atomic in the ** sense that it will either do its changes completely or not at ** all. So there is no 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( v==0 ) return; if( pRight->z==pLeft->z ){ sqliteVdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize); }else{ int addr; int size = atoi(zRight); if( size<0 ) size = -size; sqliteBeginWriteOperation(pParse); sqliteVdbeAddOp(v, OP_Integer, size, 0); sqliteVdbeAddOp(v, OP_ReadCookie, 0, 2); addr = sqliteVdbeAddOp(v, OP_Integer, 0, 0); sqliteVdbeAddOp(v, OP_Ge, 0, addr+3); sqliteVdbeAddOp(v, OP_Negative, 0, 0); sqliteVdbeAddOp(v, OP_SetCookie, 0, 2); sqliteEndWriteOperation(pParse); ................................................................................ if( v==0 ) return; if( pRight->z==pLeft->z ){ sqliteVdbeAddOpList(v, ArraySize(getSync), getSync); }else{ int addr; int size = db->cache_size; if( size<0 ) size = -size; sqliteBeginWriteOperation(pParse); sqliteVdbeAddOp(v, OP_ReadCookie, 0, 2); sqliteVdbeAddOp(v, OP_Dup, 0, 0); addr = sqliteVdbeAddOp(v, OP_Integer, 0, 0); sqliteVdbeAddOp(v, OP_Ne, 0, addr+3); sqliteVdbeAddOp(v, OP_AddImm, MAX_PAGES, 0); sqliteVdbeAddOp(v, OP_AbsValue, 0, 0); if( !getBoolean(zRight) ){ |
| | | | | | | | < < < | > > > | | | | | < < < < < < < < < < < < < < | | | | |
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 ... 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 .... 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 .... 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 .... 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 .... 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 .... 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 .... 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 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 .... 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 .... 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 |
** COPY ** VACUUM ** BEGIN TRANSACTION ** COMMIT ** ROLLBACK ** PRAGMA ** ** $Id: build.c,v 1.92 2002/05/21 11:38:11 drh Exp $ */ #include "sqliteInt.h" #include <ctype.h> /* ** This routine is called after a single SQL statement has been ** parsed and we want to execute the VDBE code to implement ................................................................................ ** and allocate the record number for the table entry now. Before any ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause ** indices to be created and the table record must come before the ** indices. Hence, the record number for the table must be allocated ** now. */ if( !pParse->initFlag && (v = sqliteGetVdbe(pParse))!=0 ){ sqliteBeginWriteOperation(pParse, 0); if( !isTemp ){ sqliteVdbeAddOp(v, OP_Integer, db->file_format, 0); sqliteVdbeAddOp(v, OP_SetCookie, 0, 1); sqliteVdbeAddOp(v, OP_OpenWrite, 0, 2); sqliteVdbeChangeP3(v, -1, MASTER_NAME, P3_STATIC); sqliteVdbeAddOp(v, OP_NewRecno, 0, 0); sqliteVdbeAddOp(v, OP_Dup, 0, 0); ................................................................................ { OP_Delete, 0, 0, 0}, { 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, 0); /* 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); } ................................................................................ int i; int addr; int isTemp = pTab->isTemp; v = sqliteGetVdbe(pParse); if( v==0 ) goto exit_create_index; if( pTable!=0 ){ sqliteBeginWriteOperation(pParse, 0); if( !isTemp ){ sqliteVdbeAddOp(v, OP_OpenWrite, 0, 2); sqliteVdbeChangeP3(v, -1, MASTER_NAME, P3_STATIC); } } if( !isTemp ){ sqliteVdbeAddOp(v, OP_NewRecno, 0, 0); ................................................................................ { OP_Integer, 0, 0, 0}, /* 10 */ { OP_SetCookie, 0, 0, 0}, { OP_Close, 0, 0, 0}, }; int base; Table *pTab = pIndex->pTable; sqliteBeginWriteOperation(pParse, 0); if( !pTab->isTemp ){ base = sqliteVdbeAddOpList(v, ArraySize(dropIndex), dropIndex); sqliteVdbeChangeP3(v, base+2, pIndex->zName, P3_STATIC); sqliteChangeCookie(db); sqliteVdbeChangeP1(v, base+10, db->next_cookie); } sqliteVdbeAddOp(v, OP_Destroy, pIndex->tnum, pTab->isTemp); ................................................................................ if( sqlite_malloc_failed || zTab==0 ) goto copy_cleanup; pTab = sqliteTableNameToTable(pParse, zTab); sqliteFree(zTab); if( pTab==0 ) goto copy_cleanup; v = sqliteGetVdbe(pParse); if( v ){ int openOp; sqliteBeginWriteOperation(pParse, 1); addr = sqliteVdbeAddOp(v, OP_FileOpen, 0, 0); sqliteVdbeChangeP3(v, addr, pFilename->z, pFilename->n); sqliteVdbeDequoteP3(v, addr); openOp = pTab->isTemp ? OP_OpenWrAux : OP_OpenWrite; sqliteVdbeAddOp(v, openOp, 0, pTab->tnum); sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC); for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ ................................................................................ */ void sqliteBeginTransaction(Parse *pParse, int onError){ sqlite *db; if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return; if( pParse->nErr || sqlite_malloc_failed ) return; if( db->flags & SQLITE_InTrans ) return; sqliteBeginWriteOperation(pParse, 0); db->flags |= SQLITE_InTrans; db->onError = onError; } /* ** Commit a transaction */ ................................................................................ } db->flags &= ~SQLITE_InTrans; db->onError = OE_Default; } /* ** Generate VDBE code that prepares for doing an operation that ** might change the database. ** ** This routine starts a new transaction if we are not already within ** a transaction. If we are already within a transaction, then a checkpoint ** is set if the setCheckpoint parameter is true. A checkpoint should ** be set for operations that might fail (due to a constraint) part of ** the way through and which will need to undo some writes without having to ** rollback the whole transaction. For operations where all constraints ** can be checked before any changes are made to the database, it is never ** necessary to undo a write and the checkpoint should not be set. */ void sqliteBeginWriteOperation(Parse *pParse, int setCheckpoint){ 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 if( setCheckpoint ){ 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( v==0 ) return; if( pRight->z==pLeft->z ){ sqliteVdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize); }else{ int addr; int size = atoi(zRight); if( size<0 ) size = -size; sqliteBeginWriteOperation(pParse, 0); sqliteVdbeAddOp(v, OP_Integer, size, 0); sqliteVdbeAddOp(v, OP_ReadCookie, 0, 2); addr = sqliteVdbeAddOp(v, OP_Integer, 0, 0); sqliteVdbeAddOp(v, OP_Ge, 0, addr+3); sqliteVdbeAddOp(v, OP_Negative, 0, 0); sqliteVdbeAddOp(v, OP_SetCookie, 0, 2); sqliteEndWriteOperation(pParse); ................................................................................ if( v==0 ) return; if( pRight->z==pLeft->z ){ sqliteVdbeAddOpList(v, ArraySize(getSync), getSync); }else{ int addr; int size = db->cache_size; if( size<0 ) size = -size; sqliteBeginWriteOperation(pParse, 0); sqliteVdbeAddOp(v, OP_ReadCookie, 0, 2); sqliteVdbeAddOp(v, OP_Dup, 0, 0); addr = sqliteVdbeAddOp(v, OP_Integer, 0, 0); sqliteVdbeAddOp(v, OP_Ne, 0, addr+3); sqliteVdbeAddOp(v, OP_AddImm, MAX_PAGES, 0); sqliteVdbeAddOp(v, OP_AbsValue, 0, 0); if( !getBoolean(zRight) ){ |
Changes to src/delete.c.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 .. 71 72 73 74 75 76 77 78 79 80 81 82 83 84 .. 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 ... 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 174 175 ... 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 |
** 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. ** ** $Id: delete.c,v 1.33 2002/05/19 23:43:14 danielk1977 Exp $ */ #include "sqliteInt.h" /* ** Given a table name, find the corresponding table and make sure the ** table is writeable. Generate an error and return NULL if not. If ................................................................................ void sqliteDeleteFrom( Parse *pParse, /* The parser context */ Token *pTableName, /* The table from which we should delete things */ Expr *pWhere /* The WHERE clause. May be null */ ){ Vdbe *v; /* The virtual database engine */ Table *pTab; /* The table from which records will be deleted */ IdList *pTabList; /* An ID list holding pTab and nothing else */ int end, addr; /* A couple addresses of generated code */ 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 */ ................................................................................ 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++; ................................................................................ /* Begin generating code. */ v = sqliteGetVdbe(pParse); if( v==0 ){ goto delete_from_cleanup; } if( row_triggers_exist ){ sqliteBeginMultiWriteOperation(pParse); } else { sqliteBeginWriteOperation(pParse); } /* Initialize the counter of the number of rows deleted, if ** we are counting rows. */ if( db->flags & SQLITE_CountRows ){ sqliteVdbeAddOp(v, OP_Integer, 0, 0); } /* Special case: A DELETE without a WHERE clause deletes everything. ** It is easier just to erase the whole table. */ if( pWhere==0 && !row_triggers_exist ){ if( db->flags & SQLITE_CountRows ){ /* If counting rows deleted, just count the total number of ** entries in the table. */ int endOfLoop = sqliteVdbeMakeLabel(v); int addr; ................................................................................ /* 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){ sqliteVdbeAddOp(v, openOp, pParse->nTab++, pIdx->tnum); } if( !row_triggers_exist ){ addr = sqliteVdbeAddOp(v, OP_ListRead, 0, end); } sqliteGenerateRowDelete(v, pTab, base, pParse->trigStack?0:1); if( row_triggers_exist ){ for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){ sqliteVdbeAddOp(v, OP_Close, base + i, pIdx->tnum); } sqliteVdbeAddOp(v, OP_Close, base, 0); sqliteCodeRowTrigger(pParse, TK_DELETE, 0, TK_AFTER, pTab, -1, oldIdx, (pParse->trigStack)?pParse->trigStack->orconf:OE_Default); } sqliteVdbeAddOp(v, OP_Goto, 0, addr); sqliteVdbeResolveLabel(v, end); sqliteVdbeAddOp(v, OP_ListReset, 0, 0); if( !row_triggers_exist ){ for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){ sqliteVdbeAddOp(v, OP_Close, base + i, pIdx->tnum); } sqliteVdbeAddOp(v, OP_Close, base, 0); pParse->nTab = base; } |
| > < < | < | | | | | | | | | | | | | | < | > > < < < | < | > > > > < | | | > > > > > > > > > | > > > > > |
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 .. 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 .. 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 ... 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 ... 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 |
** 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. ** ** $Id: delete.c,v 1.34 2002/05/21 11:38:11 drh Exp $ */ #include "sqliteInt.h" /* ** Given a table name, find the corresponding table and make sure the ** table is writeable. Generate an error and return NULL if not. If ................................................................................ void sqliteDeleteFrom( Parse *pParse, /* The parser context */ Token *pTableName, /* The table from which we should delete things */ Expr *pWhere /* The WHERE clause. May be null */ ){ Vdbe *v; /* The virtual database engine */ Table *pTab; /* The table from which records will be deleted */ char *zTab; /* Name of the table from which we are deleting */ IdList *pTabList; /* An ID list holding pTab and nothing else */ int end, addr; /* A couple addresses of generated code */ 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 */ ................................................................................ goto delete_from_cleanup; } db = pParse->db; /* Check for the special case of a VIEW with one or more ON DELETE triggers ** defined */ 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* parameter. */ 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 */ /* Allocate a cursor used to store the old.* data for a trigger. */ if( row_triggers_exist ){ oldIdx = pParse->nTab++; } /* Resolve the column names in all the expressions. */ base = pParse->nTab++; ................................................................................ /* Begin generating code. */ v = sqliteGetVdbe(pParse); if( v==0 ){ goto delete_from_cleanup; } sqliteBeginWriteOperation(pParse, row_triggers_exist); /* Initialize the counter of the number of rows deleted, if ** we are counting rows. */ if( db->flags & SQLITE_CountRows ){ sqliteVdbeAddOp(v, OP_Integer, 0, 0); } /* Special case: A DELETE without a WHERE clause deletes everything. ** It is easier just to erase the whole table. Note, however, that ** this means that the row change count will be incorrect. */ if( pWhere==0 && !row_triggers_exist ){ if( db->flags & SQLITE_CountRows ){ /* If counting rows deleted, just count the total number of ** entries in the table. */ int endOfLoop = sqliteVdbeMakeLabel(v); int addr; ................................................................................ /* 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); /* This is the beginning of the delete loop when there are ** row triggers. */ if( row_triggers_exist ){ 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(i = 0; i<pTab->nCol; i++){ if( i==pTab->iPKey ){ sqliteVdbeAddOp(v, OP_Recno, base, 0); } else { sqliteVdbeAddOp(v, OP_Column, base, i); } } 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); } /* Open cursors for the table we are deleting from and all its ** indices. If there are row triggers, this happens inside the ** OP_ListRead loop because the cursor have to all be closed ** before the trigger fires. If there are no row triggers, the ** cursors are opened only once on the outside the loop. */ 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){ sqliteVdbeAddOp(v, openOp, pParse->nTab++, pIdx->tnum); } /* This is the beginning of the delete loop when there are no ** row triggers */ if( !row_triggers_exist ){ addr = sqliteVdbeAddOp(v, OP_ListRead, 0, end); } /* Delete the row */ sqliteGenerateRowDelete(v, pTab, base, pParse->trigStack==0); /* If there are row triggers, close all cursors then invoke ** the AFTER triggers */ if( row_triggers_exist ){ for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){ sqliteVdbeAddOp(v, OP_Close, base + i, pIdx->tnum); } sqliteVdbeAddOp(v, OP_Close, base, 0); sqliteCodeRowTrigger(pParse, TK_DELETE, 0, TK_AFTER, pTab, -1, oldIdx, (pParse->trigStack)?pParse->trigStack->orconf:OE_Default); } /* End of the delete loop */ sqliteVdbeAddOp(v, OP_Goto, 0, addr); sqliteVdbeResolveLabel(v, end); sqliteVdbeAddOp(v, OP_ListReset, 0, 0); /* Close the cursors after the loop if there are no row triggers */ if( !row_triggers_exist ){ for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){ sqliteVdbeAddOp(v, OP_Close, base + i, pIdx->tnum); } sqliteVdbeAddOp(v, OP_Close, base, 0); pParse->nTab = base; } |
Changes to src/insert.c.
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
..
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
** 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. ** ** $Id: insert.c,v 1.55 2002/05/19 23:43:14 danielk1977 Exp $ */ #include "sqliteInt.h" /* ** This routine is call to handle SQL of the following forms: ** ** insert into TABLE (IDLIST) values(EXPRLIST) ................................................................................ if( pTab==0 ) goto insert_cleanup; /* Allocate a VDBE */ v = sqliteGetVdbe(pParse); if( v==0 ) goto insert_cleanup; if( pSelect || row_triggers_exist ){ sqliteBeginMultiWriteOperation(pParse); }else{ sqliteBeginWriteOperation(pParse); } /* if there are row triggers, allocate a temp table for new.* references. */ if( row_triggers_exist ){ newIdx = pParse->nTab++; } /* Figure out how many columns of data are supplied. If the data |
|
<
<
<
|
<
|
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
..
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
** 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. ** ** $Id: insert.c,v 1.56 2002/05/21 11:38:11 drh Exp $ */ #include "sqliteInt.h" /* ** This routine is call to handle SQL of the following forms: ** ** insert into TABLE (IDLIST) values(EXPRLIST) ................................................................................ if( pTab==0 ) goto insert_cleanup; /* Allocate a VDBE */ v = sqliteGetVdbe(pParse); if( v==0 ) goto insert_cleanup; sqliteBeginWriteOperation(pParse, pSelect || row_triggers_exist); /* if there are row triggers, allocate a temp table for new.* references. */ if( row_triggers_exist ){ newIdx = pParse->nTab++; } /* Figure out how many columns of data are supplied. If the data |
Changes to src/parse.y.
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
..
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
|
** ************************************************************************* ** 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. ** ** @(#) $Id: parse.y,v 1.65 2002/05/15 14:17:45 drh Exp $ */ %token_prefix TK_ %token_type {Token} %default_type {Token} %extra_argument {Parse *pParse} %syntax_error { sqliteSetString(&pParse->zErrMsg,"syntax error",0); ................................................................................ // an elaborate typename. Perhaps someday we'll do something with it. // column ::= columnid type carglist. columnid ::= ids(X). {sqliteAddColumn(pParse,&X);} // An IDENTIFIER can be a generic identifier, or one of several // keywords. Any non-standard keyword can also be an identifier. // We also make DESC and identifier since it comes up so often (as // an abbreviation of "description"). // %type id {Token} id(A) ::= ABORT(X). {A = X;} id(A) ::= ASC(X). {A = X;} id(A) ::= BEGIN(X). {A = X;} id(A) ::= CLUSTER(X). {A = X;} id(A) ::= CONFLICT(X). {A = X;} id(A) ::= COPY(X). {A = X;} id(A) ::= DELIMITERS(X). {A = X;} id(A) ::= DESC(X). {A = X;} id(A) ::= END(X). {A = X;} id(A) ::= EXPLAIN(X). {A = X;} id(A) ::= FAIL(X). {A = X;} id(A) ::= ID(X). {A = X;} id(A) ::= IGNORE(X). {A = X;} id(A) ::= KEY(X). {A = X;} id(A) ::= OFFSET(X). {A = X;} id(A) ::= PRAGMA(X). {A = X;} id(A) ::= REPLACE(X). {A = X;} id(A) ::= TEMP(X). {A = X;} id(A) ::= TRIGGER(X). {A = X;} id(A) ::= VACUUM(X). {A = X;} id(A) ::= VIEW(X). {A = X;} // And "ids" is an identifer-or-string. // |
|
<
<
>
>
>
>
>
>
>
|
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
..
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
|
** ************************************************************************* ** 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. ** ** @(#) $Id: parse.y,v 1.66 2002/05/21 11:38:12 drh Exp $ */ %token_prefix TK_ %token_type {Token} %default_type {Token} %extra_argument {Parse *pParse} %syntax_error { sqliteSetString(&pParse->zErrMsg,"syntax error",0); ................................................................................ // an elaborate typename. Perhaps someday we'll do something with it. // column ::= columnid type carglist. columnid ::= ids(X). {sqliteAddColumn(pParse,&X);} // An IDENTIFIER can be a generic identifier, or one of several // keywords. Any non-standard keyword can also be an identifier. // %type id {Token} id(A) ::= ABORT(X). {A = X;} id(A) ::= AFTER(X). {A = X;} id(A) ::= ASC(X). {A = X;} id(A) ::= BEFORE(X). {A = X;} id(A) ::= BEGIN(X). {A = X;} id(A) ::= CLUSTER(X). {A = X;} id(A) ::= CONFLICT(X). {A = X;} id(A) ::= COPY(X). {A = X;} id(A) ::= DELIMITERS(X). {A = X;} id(A) ::= DESC(X). {A = X;} id(A) ::= EACH(X). {A = X;} id(A) ::= END(X). {A = X;} id(A) ::= EXPLAIN(X). {A = X;} id(A) ::= FAIL(X). {A = X;} id(A) ::= FOR(X). {A = X;} id(A) ::= ID(X). {A = X;} id(A) ::= IGNORE(X). {A = X;} id(A) ::= INSTEAD(X). {A = X;} id(A) ::= KEY(X). {A = X;} id(A) ::= OF(X). {A = X;} id(A) ::= OFFSET(X). {A = X;} id(A) ::= PRAGMA(X). {A = X;} id(A) ::= REPLACE(X). {A = X;} id(A) ::= ROW(X). {A = X;} id(A) ::= TEMP(X). {A = X;} id(A) ::= TRIGGER(X). {A = X;} id(A) ::= VACUUM(X). {A = X;} id(A) ::= VIEW(X). {A = X;} // And "ids" is an identifer-or-string. // |
Changes to src/sqliteInt.h.
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
...
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
|
** 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. ** ** @(#) $Id: sqliteInt.h,v 1.110 2002/05/16 00:13:12 danielk1977 Exp $ */ #include "sqlite.h" #include "hash.h" #include "vdbe.h" #include "parse.h" #include "btree.h" #include <stdio.h> ................................................................................ void sqliteRollbackTransaction(Parse*); char *sqlite_mprintf(const char *, ...); int sqliteExprIsConstant(Expr*); void sqliteGenerateRowDelete(Vdbe*, Table*, int, int); void sqliteGenerateRowIndexDelete(Vdbe*, Table*, int, char*); void sqliteGenerateConstraintChecks(Parse*,Table*,int,char*,int,int,int,int); void sqliteCompleteInsertion(Parse*, Table*, int, char*, int, int); void sqliteBeginWriteOperation(Parse*); void sqliteBeginMultiWriteOperation(Parse*); void sqliteEndWriteOperation(Parse*); void sqliteExprMoveStrings(Expr*, int); void sqliteExprListMoveStrings(ExprList*, int); void sqliteSelectMoveStrings(Select*, int); Expr *sqliteExprDup(Expr*); ExprList *sqliteExprListDup(ExprList*); IdList *sqliteIdListDup(IdList*); |
|
|
<
|
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
...
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
|
** 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. ** ** @(#) $Id: sqliteInt.h,v 1.111 2002/05/21 11:38:12 drh Exp $ */ #include "sqlite.h" #include "hash.h" #include "vdbe.h" #include "parse.h" #include "btree.h" #include <stdio.h> ................................................................................ void sqliteRollbackTransaction(Parse*); char *sqlite_mprintf(const char *, ...); int sqliteExprIsConstant(Expr*); void sqliteGenerateRowDelete(Vdbe*, Table*, int, int); void sqliteGenerateRowIndexDelete(Vdbe*, Table*, int, char*); void sqliteGenerateConstraintChecks(Parse*,Table*,int,char*,int,int,int,int); void sqliteCompleteInsertion(Parse*, Table*, int, char*, int, int); void sqliteBeginWriteOperation(Parse*, int); void sqliteEndWriteOperation(Parse*); void sqliteExprMoveStrings(Expr*, int); void sqliteExprListMoveStrings(ExprList*, int); void sqliteSelectMoveStrings(Select*, int); Expr *sqliteExprDup(Expr*); ExprList *sqliteExprListDup(ExprList*); IdList *sqliteIdListDup(IdList*); |
Changes to src/trigger.c.
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 ... 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 ... 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 ... 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 ... 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 ... 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 ... 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 ... 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 ... 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 ... 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 |
ss = ss->pNext; } /* if we are not initializing, and this trigger is not on a TEMP table, ** build the sqlite_master entry */ if( !pParse->initFlag && !tab->isTemp ){ /* Make an entry in the sqlite_master table */ sqliteBeginWriteOperation(pParse); sqliteVdbeAddOp(pParse->pVdbe, OP_OpenWrite, 0, 2); sqliteVdbeChangeP3(pParse->pVdbe, -1, MASTER_NAME, P3_STATIC); sqliteVdbeAddOp(pParse->pVdbe, OP_NewRecno, 0, 0); sqliteVdbeAddOp(pParse->pVdbe, OP_String, 0, 0); sqliteVdbeChangeP3(pParse->pVdbe, -1, "trigger", P3_STATIC); sqliteVdbeAddOp(pParse->pVdbe, OP_String, 0, 0); sqliteVdbeChangeP3(pParse->pVdbe, -1, nt->name, 0); sqliteVdbeAddOp(pParse->pVdbe, OP_String, 0, 0); sqliteVdbeChangeP3(pParse->pVdbe, -1, nt->table, 0); sqliteVdbeAddOp(pParse->pVdbe, OP_Integer, 0, 0); sqliteVdbeAddOp(pParse->pVdbe, OP_String, 0, 0); sqliteVdbeChangeP3(pParse->pVdbe, -1, nt->strings, 0); sqliteVdbeAddOp(pParse->pVdbe, OP_MakeRecord, 5, 0); sqliteVdbeAddOp(pParse->pVdbe, OP_PutIntKey, 0, 1); /* Change the cookie, since the schema is changed */ sqliteChangeCookie(pParse->db); sqliteVdbeAddOp(pParse->pVdbe, OP_Integer, pParse->db->next_cookie, 0); sqliteVdbeAddOp(pParse->pVdbe, OP_SetCookie, 0, 0); sqliteVdbeAddOp(pParse->pVdbe, OP_Close, 0, 0); sqliteEndWriteOperation(pParse); } if( !pParse->explain ){ /* Stick it in the hash-table */ sqliteHashInsert(&(pParse->db->trigHash), nt->name, pName->n + 1, nt); ................................................................................ sqliteIdListDelete(pp->pIdList); sqliteFree(pp); pp = nn; } } } TriggerStep *sqliteTriggerSelectStep(Select * pSelect) { TriggerStep *pTriggerStep = sqliteMalloc(sizeof(TriggerStep)); pTriggerStep->op = TK_SELECT; pTriggerStep->pSelect = pSelect; pTriggerStep->orconf = OE_Default; return pTriggerStep; } TriggerStep *sqliteTriggerInsertStep( Token *pTableName, IdList *pColumn, ExprList *pEList, Select *pSelect, int orconf ){ TriggerStep *pTriggerStep = sqliteMalloc(sizeof(TriggerStep)); assert(pEList == 0 || pSelect == 0); assert(pEList != 0 || pSelect != 0); pTriggerStep->op = TK_INSERT; ................................................................................ pTriggerStep->pIdList = pColumn; pTriggerStep->pExprList = pEList; pTriggerStep->orconf = orconf; return pTriggerStep; } TriggerStep *sqliteTriggerUpdateStep( Token *pTableName, ExprList *pEList, Expr *pWhere, int orconf) { TriggerStep *pTriggerStep = sqliteMalloc(sizeof(TriggerStep)); pTriggerStep->op = TK_UPDATE; pTriggerStep->target = *pTableName; pTriggerStep->pExprList = pEList; pTriggerStep->pWhere = pWhere; pTriggerStep->orconf = orconf; return pTriggerStep; } TriggerStep *sqliteTriggerDeleteStep(Token *pTableName, Expr *pWhere) { TriggerStep * pTriggerStep = sqliteMalloc(sizeof(TriggerStep)); pTriggerStep->op = TK_DELETE; pTriggerStep->target = *pTableName; pTriggerStep->pWhere = pWhere; pTriggerStep->orconf = OE_Default; ................................................................................ { OP_Next, 0, ADDR(4), 0}, /* 8 */ { OP_Integer, 0, 0, 0}, /* 9 */ { OP_SetCookie, 0, 0, 0}, { OP_Close, 0, 0, 0}, }; if( !nested ){ sqliteBeginWriteOperation(pParse); } base = sqliteVdbeAddOpList(pParse->pVdbe, ArraySize(dropTrigger), dropTrigger); sqliteVdbeChangeP3(pParse->pVdbe, base+2, zName, 0); if( !nested ){ sqliteChangeCookie(pParse->db); } ................................................................................ sqliteEndWriteOperation(pParse); } } sqliteFree(zName); } static int checkColumnOverLap(IdList * pIdList, ExprList * pEList) { int i, e; if( !pIdList )return 1; if( !pEList )return 1; for(i = 0; i < pIdList->nId; i++){ for(e = 0; e < pEList->nExpr; e++){ if( !sqliteStrICmp(pIdList->a[i].zName, pEList->a[e].zName) ){ ................................................................................ /* * Returns true if a trigger matching op, tr_tm and foreach that is NOT already * on the Parse objects trigger-stack (to prevent recursive trigger firing) is * found in the list specified as pTrigger. */ int sqliteTriggersExist( Parse *pParse, Trigger *pTrigger, int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */ int tr_tm, /* one of TK_BEFORE, TK_AFTER */ int foreach, /* one of TK_ROW or TK_STATEMENT */ ExprList *pChanges) { Trigger * pTriggerCursor; if( always_code_trigger_setup ){ return 1; } pTriggerCursor = pTrigger; ................................................................................ } pTriggerCursor = pTriggerCursor->pNext; } return 0; } static int codeTriggerProgram( Parse *pParse, TriggerStep *pStepList, int orconfin ){ TriggerStep * pTriggerStep = pStepList; int orconf; while( pTriggerStep ){ int saveNTab = pParse->nTab; orconf = (orconfin == OE_Default)?pTriggerStep->orconf:orconfin; ................................................................................ } /* ** This is called to code FOR EACH ROW triggers. ** ** When the code that this function generates is executed, the following ** must be true: ** 1. NO vdbe cursors must be open. ** 2. If the triggers being coded are ON INSERT or ON UPDATE triggers, then ** a temporary vdbe cursor (index newIdx) must be open and pointing at ** a row containing values to be substituted for new.* expressions in the ** trigger program(s). ** 3. If the triggers being coded are ON DELETE or ON UPDATE triggers, then ** a temporary vdbe cursor (index oldIdx) must be open and pointing at ** a row containing values to be substituted for old.* expressions in the ** trigger program(s). ** */ int sqliteCodeRowTrigger( ................................................................................ Parse *pParse, /* Parse context */ int op, /* One of TK_UPDATE, TK_INSERT, TK_DELETE */ ExprList *pChanges, /* Changes list for any UPDATE OF triggers */ int tr_tm, /* One of TK_BEFORE, TK_AFTER */ Table *pTab, /* The table to code triggers from */ int newIdx, /* The indice of the "new" row to access */ int oldIdx, /* The indice of the "old" row to access */ int orconf) /* ON CONFLICT policy */ { Trigger * pTrigger; TriggerStack * pTriggerStack; assert(op == TK_UPDATE || op == TK_INSERT || op == TK_DELETE); assert(tr_tm == TK_BEFORE || tr_tm == TK_AFTER); assert(newIdx != -1 || oldIdx != -1); ................................................................................ theSelect.nLimit = -1; theSelect.nOffset = -1; theSelect.zSelect = 0; theSelect.base = 0; v = sqliteGetVdbe(pParse); assert(v); sqliteBeginMultiWriteOperation(pParse); /* Allocate temp tables */ oldIdx = pParse->nTab++; sqliteVdbeAddOp(v, OP_OpenTemp, oldIdx, 0); if( pChanges ){ newIdx = pParse->nTab++; sqliteVdbeAddOp(v, OP_OpenTemp, newIdx, 0); |
| | | | | | | | | | | | | | | | | | | | | | | | | | > > > > > > > | < > > > > > > > | | | | < > > > > > > | | | < < > > > > > > > | < | > > > > > > > > > | < | | | | > > > > | | < > | > > > > | | | |
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 ... 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 ... 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 ... 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 ... 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 ... 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 ... 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 ... 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 ... 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 ... 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 |
ss = ss->pNext; } /* if we are not initializing, and this trigger is not on a TEMP table, ** build the sqlite_master entry */ if( !pParse->initFlag && !tab->isTemp ){ static VdbeOp insertTrig[] = { { OP_OpenWrite, 0, 2, MASTER_NAME}, { OP_NewRecno, 0, 0, 0 }, { OP_String, 0, 0, "trigger" }, { OP_String, 0, 0, 0 }, /* 3: trigger name */ { OP_String, 0, 0, 0 }, /* 4: table name */ { OP_Integer, 0, 0, 0 }, { OP_String, 0, 0, 0 }, /* 6: SQL */ { OP_MakeRecord, 5, 0, 0 }, { OP_PutIntKey, 0, 0, 0 }, { OP_Integer, 0, 0, 0 }, /* 9: Next cookie */ { OP_SetCookie, 0, 0, 0 }, { OP_Close, 0, 0, 0 }, }; int addr; Vdbe *v; /* Make an entry in the sqlite_master table */ v = sqliteGetVdbe(pParse); sqliteBeginWriteOperation(pParse, 0); addr = sqliteVdbeAddOpList(v, ArraySize(insertTrig), insertTrig); sqliteVdbeChangeP3(v, addr+3, nt->name, 0); sqliteVdbeChangeP3(v, addr+4, nt->table, 0); sqliteVdbeChangeP3(v, addr+6, nt->strings, 0); sqliteChangeCookie(pParse->db); sqliteVdbeChangeP1(v, addr+9, pParse->db->next_cookie); sqliteEndWriteOperation(pParse); } if( !pParse->explain ){ /* Stick it in the hash-table */ sqliteHashInsert(&(pParse->db->trigHash), nt->name, pName->n + 1, nt); ................................................................................ sqliteIdListDelete(pp->pIdList); sqliteFree(pp); pp = nn; } } } /* ** Turn a SELECT statement (that the pSelect parameter points to) into ** a trigger step. Return a pointer to a TriggerStep structure. ** ** The parser calls this routine when it finds a SELECT statement in ** body of a TRIGGER. */ TriggerStep *sqliteTriggerSelectStep(Select *pSelect){ TriggerStep *pTriggerStep = sqliteMalloc(sizeof(TriggerStep)); pTriggerStep->op = TK_SELECT; pTriggerStep->pSelect = pSelect; pTriggerStep->orconf = OE_Default; return pTriggerStep; } /* ** Build a trigger step out of an INSERT statement. Return a pointer ** to the new trigger step. ** ** The parser calls this routine when it sees an INSERT inside the ** body of a trigger. */ TriggerStep *sqliteTriggerInsertStep( Token *pTableName, /* Name of the table into which we insert */ IdList *pColumn, /* List of columns in pTableName to insert into */ ExprList *pEList, /* The VALUE clause: a list of values to be inserted */ Select *pSelect, /* A SELECT statement that supplies values */ int orconf /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */ ){ TriggerStep *pTriggerStep = sqliteMalloc(sizeof(TriggerStep)); assert(pEList == 0 || pSelect == 0); assert(pEList != 0 || pSelect != 0); pTriggerStep->op = TK_INSERT; ................................................................................ pTriggerStep->pIdList = pColumn; pTriggerStep->pExprList = pEList; pTriggerStep->orconf = orconf; return pTriggerStep; } /* ** Construct a trigger step that implements an UPDATE statement and return ** a pointer to that trigger step. The parser calls this routine when it ** sees an UPDATE statement inside the body of a CREATE TRIGGER. */ TriggerStep *sqliteTriggerUpdateStep( Token *pTableName, /* Name of the table to be updated */ ExprList *pEList, /* The SET clause: list of column and new values */ Expr *pWhere, /* The WHERE clause */ int orconf /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */ ){ TriggerStep *pTriggerStep = sqliteMalloc(sizeof(TriggerStep)); pTriggerStep->op = TK_UPDATE; pTriggerStep->target = *pTableName; pTriggerStep->pExprList = pEList; pTriggerStep->pWhere = pWhere; pTriggerStep->orconf = orconf; return pTriggerStep; } /* ** Construct a trigger step that implements a DELETE statement and return ** a pointer to that trigger step. The parser calls this routine when it ** sees a DELETE statement inside the body of a CREATE TRIGGER. */ TriggerStep *sqliteTriggerDeleteStep(Token *pTableName, Expr *pWhere){ TriggerStep * pTriggerStep = sqliteMalloc(sizeof(TriggerStep)); pTriggerStep->op = TK_DELETE; pTriggerStep->target = *pTableName; pTriggerStep->pWhere = pWhere; pTriggerStep->orconf = OE_Default; ................................................................................ { OP_Next, 0, ADDR(4), 0}, /* 8 */ { OP_Integer, 0, 0, 0}, /* 9 */ { OP_SetCookie, 0, 0, 0}, { OP_Close, 0, 0, 0}, }; if( !nested ){ sqliteBeginWriteOperation(pParse, 0); } base = sqliteVdbeAddOpList(pParse->pVdbe, ArraySize(dropTrigger), dropTrigger); sqliteVdbeChangeP3(pParse->pVdbe, base+2, zName, 0); if( !nested ){ sqliteChangeCookie(pParse->db); } ................................................................................ sqliteEndWriteOperation(pParse); } } sqliteFree(zName); } /* ** pEList is the SET clause of an UPDATE statement. Each entry ** in pEList is of the format <id>=<expr>. If any of the entries ** in pEList have an <id> which matches an identifier in pIdList, ** then return TRUE. If pIdList==NULL, then it is considered a ** wildcard that matches anything. Likewise if pEList==NULL then ** it matches anything so always return true. Return false only ** if there is no match. */ static int checkColumnOverLap(IdList *pIdList, ExprList *pEList){ int i, e; if( !pIdList )return 1; if( !pEList )return 1; for(i = 0; i < pIdList->nId; i++){ for(e = 0; e < pEList->nExpr; e++){ if( !sqliteStrICmp(pIdList->a[i].zName, pEList->a[e].zName) ){ ................................................................................ /* * Returns true if a trigger matching op, tr_tm and foreach that is NOT already * on the Parse objects trigger-stack (to prevent recursive trigger firing) is * found in the list specified as pTrigger. */ int sqliteTriggersExist( Parse *pParse, /* Used to check for recursive triggers */ Trigger *pTrigger, /* A list of triggers associated with a table */ int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */ int tr_tm, /* one of TK_BEFORE, TK_AFTER */ int foreach, /* one of TK_ROW or TK_STATEMENT */ ExprList *pChanges /* Columns that change in an UPDATE statement */ ){ Trigger * pTriggerCursor; if( always_code_trigger_setup ){ return 1; } pTriggerCursor = pTrigger; ................................................................................ } pTriggerCursor = pTriggerCursor->pNext; } return 0; } /* ** Generate VDBE code for zero or more statements inside the body of a ** trigger. */ static int codeTriggerProgram( Parse *pParse, /* The parser context */ TriggerStep *pStepList, /* List of statements inside the trigger body */ int orconfin /* Conflict algorithm. (OE_Abort, etc) */ ){ TriggerStep * pTriggerStep = pStepList; int orconf; while( pTriggerStep ){ int saveNTab = pParse->nTab; orconf = (orconfin == OE_Default)?pTriggerStep->orconf:orconfin; ................................................................................ } /* ** This is called to code FOR EACH ROW triggers. ** ** When the code that this function generates is executed, the following ** must be true: ** ** 1. No cursors may be open in the main database. (But newIdx and oldIdx ** can be indices of cursors in temporary tables. See below.) ** ** 2. If the triggers being coded are ON INSERT or ON UPDATE triggers, then ** a temporary vdbe cursor (index newIdx) must be open and pointing at ** a row containing values to be substituted for new.* expressions in the ** trigger program(s). ** ** 3. If the triggers being coded are ON DELETE or ON UPDATE triggers, then ** a temporary vdbe cursor (index oldIdx) must be open and pointing at ** a row containing values to be substituted for old.* expressions in the ** trigger program(s). ** */ int sqliteCodeRowTrigger( ................................................................................ Parse *pParse, /* Parse context */ int op, /* One of TK_UPDATE, TK_INSERT, TK_DELETE */ ExprList *pChanges, /* Changes list for any UPDATE OF triggers */ int tr_tm, /* One of TK_BEFORE, TK_AFTER */ Table *pTab, /* The table to code triggers from */ int newIdx, /* The indice of the "new" row to access */ int oldIdx, /* The indice of the "old" row to access */ int orconf /* ON CONFLICT policy */ ){ Trigger * pTrigger; TriggerStack * pTriggerStack; assert(op == TK_UPDATE || op == TK_INSERT || op == TK_DELETE); assert(tr_tm == TK_BEFORE || tr_tm == TK_AFTER); assert(newIdx != -1 || oldIdx != -1); ................................................................................ theSelect.nLimit = -1; theSelect.nOffset = -1; theSelect.zSelect = 0; theSelect.base = 0; v = sqliteGetVdbe(pParse); assert(v); sqliteBeginWriteOperation(pParse, 1); /* Allocate temp tables */ oldIdx = pParse->nTab++; sqliteVdbeAddOp(v, OP_OpenTemp, oldIdx, 0); if( pChanges ){ newIdx = pParse->nTab++; sqliteVdbeAddOp(v, OP_OpenTemp, newIdx, 0); |
Changes to src/update.c.
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
...
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
** 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 UPDATE statements. ** ** $Id: update.c,v 1.39 2002/05/19 23:43:14 danielk1977 Exp $ */ #include "sqliteInt.h" /* ** Process an UPDATE statement. */ void sqliteUpdate( ................................................................................ } } /* Begin generating code. */ v = sqliteGetVdbe(pParse); if( v==0 ) goto update_cleanup; sqliteBeginMultiWriteOperation(pParse); /* Begin the database scan */ pWInfo = sqliteWhereBegin(pParse, base, pTabList, pWhere, 1); if( pWInfo==0 ) goto update_cleanup; /* Remember the index of every item to be updated. |
|
|
|
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
...
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
** 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 UPDATE statements. ** ** $Id: update.c,v 1.40 2002/05/21 11:38:12 drh Exp $ */ #include "sqliteInt.h" /* ** Process an UPDATE statement. */ void sqliteUpdate( ................................................................................ } } /* Begin generating code. */ v = sqliteGetVdbe(pParse); if( v==0 ) goto update_cleanup; sqliteBeginWriteOperation(pParse, 1); /* Begin the database scan */ pWInfo = sqliteWhereBegin(pParse, base, pTabList, pWhere, 1); if( pWInfo==0 ) goto update_cleanup; /* Remember the index of every item to be updated. |
Changes to test/misuse.test.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# #*********************************************************************** # This file implements regression tests for SQLite library. # # This file implements tests for the SQLITE_MISUSE detection logic. # This test file leaks memory and file descriptors. # # $Id: misuse.test,v 1.2 2002/05/10 14:37:31 drh Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl # Make sure the test logic works # do_test misuse-1.1 { db close set ::DB [sqlite db test2.db] execsql { CREATE TABLE t1(a,b); INSERT INTO t1 VALUES(1,2); } sqlite_exec_printf $::DB {SELECT * FROM t1} {} } {0 {a b 1 2}} |
| > |
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# #*********************************************************************** # This file implements regression tests for SQLite library. # # This file implements tests for the SQLITE_MISUSE detection logic. # This test file leaks memory and file descriptors. # # $Id: misuse.test,v 1.3 2002/05/21 11:38:12 drh Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl # Make sure the test logic works # do_test misuse-1.1 { db close catch {file delete -force test2.db} set ::DB [sqlite db test2.db] execsql { CREATE TABLE t1(a,b); INSERT INTO t1 VALUES(1,2); } sqlite_exec_printf $::DB {SELECT * FROM t1} {} } {0 {a b 1 2}} |