Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Modify the documentation for sqlite3_changes() to make it more testable. Add tests and minor fixes for the same. |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
41cdd0c422d61533a94870cb5ad09468 |
User & Date: | dan 2014-10-28 18:24:16.387 |
Context
2014-10-28
| ||
20:35 | Update the documentation on the sqlite3_randomness() interface to conform to enhancements associated with the SQLITE_ENABLE_API_ARMOR change. (check-in: 96e9917c35 user: drh tags: trunk) | |
18:24 | Modify the documentation for sqlite3_changes() to make it more testable. Add tests and minor fixes for the same. (check-in: 41cdd0c422 user: dan tags: trunk) | |
16:50 | Fix a crash that could occur if the WHERE clause of an UPDATE statement on a view that does not feature a column named "rowid" contains a term such as "rowid=?". (check-in: 8523670d50 user: dan tags: trunk) | |
Changes
Changes to src/sqlite.h.in.
︙ | ︙ | |||
1870 1871 1872 1873 1874 1875 1876 | ** last insert [rowid]. */ sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*); /* ** CAPI3REF: Count The Number Of Rows Modified ** | | | | > > > | | | < < | | > > > | < < < < > > | > | < < < < < < < | > | < > | < | > > > > | | | | > | | < | < < | | 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 | ** last insert [rowid]. */ sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*); /* ** CAPI3REF: Count The Number Of Rows Modified ** ** ^This function returns the number of rows modified, inserted or ** deleted by the most recently completed INSERT, UPDATE or DELETE ** statement on the database connection specified by the only parameter. ** ^Executing any other type of SQL statement does not modify the value ** returned by this function. ** ** ^Only changes made directly by the INSERT, UPDATE or DELETE statement are ** considered - auxiliary changes caused by [CREATE TRIGGER | triggers], ** [foreign key actions] or [REPLACE] constraint resolution are not counted. ** ** Changes to a view that are intercepted by ** [INSTEAD OF trigger | INSTEAD OF triggers] are not counted. ^The value ** returned by sqlite3_changes() immediately after an INSERT, UPDATE or ** DELETE statement run on a view is always zero. Only changes made to real ** tables are counted. ** ** Things are more complicated if the sqlite3_changes() function is ** executed while a trigger program is running. This may happen if the ** program uses the [changes() SQL function], or if some other callback ** function invokes sqlite3_changes() directly. Essentially: ** ** <ul> ** <li> ^(Before entering a trigger program the value returned by ** sqlite3_changes() function is saved. After the trigger program ** has finished, the original value is restored.)^ ** ** <li> ^(Within a trigger program each INSERT, UPDATE and DELETE ** statement sets the value returned by sqlite3_changes() ** upon completion as normal. Of course, this value will not include ** any changes performed by sub-triggers, as the sqlite3_changes() ** value will be saved and restored after each sub-trigger has run.)^ ** </ul> ** ** ^This means that if the changes() SQL function (or similar) is used ** by the first INSERT, UPDATE or DELETE statement within a trigger, it ** returns the value as set when the calling statement began executing. ** ^If it is used by the second or subsequent such statement within a trigger ** program, the value returned reflects the number of rows modified by the ** previous INSERT, UPDATE or DELETE statement within the same trigger. ** ** See also the [sqlite3_total_changes()] interface, the ** [count_changes pragma], and the [changes() SQL function]. ** ** If a separate thread makes changes on the same database connection ** while [sqlite3_changes()] is running then the value returned ** is unpredictable and not meaningful. */ int sqlite3_changes(sqlite3*); /* ** CAPI3REF: Total Number Of Rows Modified ** ** ^This function returns the number of row changes caused by [INSERT], ** [UPDATE] or [DELETE] statements since the [database connection] was opened. ** ^(The count returned by sqlite3_total_changes() includes all changes ** from all [CREATE TRIGGER | trigger] contexts and changes made by ** [foreign key actions]. However, ** the count does not include changes used to implement [REPLACE] constraints, ** rollbacks or [DROP TABLE] commands. The ** count does not include rows of views that fire an [INSTEAD OF trigger], ** though if the INSTEAD OF trigger makes changes of its own, those changes ** are counted.)^ ** ^The sqlite3_total_changes() function counts the changes as soon as ** the statement that makes them is completed (when the statement handle ** is passed to [sqlite3_reset()] or [sqlite3_finalize()]). ** |
︙ | ︙ |
Changes to src/vdbe.c.
︙ | ︙ | |||
5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 | assert( pc==pFrame->pc ); } p->nFrame++; pFrame->pParent = p->pFrame; pFrame->lastRowid = lastRowid; pFrame->nChange = p->nChange; p->nChange = 0; p->pFrame = pFrame; p->aMem = aMem = &VdbeFrameMem(pFrame)[-1]; p->nMem = pFrame->nChildMem; p->nCursor = (u16)pFrame->nChildCsr; p->apCsr = (VdbeCursor **)&aMem[p->nMem+1]; p->aOp = aOp = pProgram->aOp; | > | 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 | assert( pc==pFrame->pc ); } p->nFrame++; pFrame->pParent = p->pFrame; pFrame->lastRowid = lastRowid; pFrame->nChange = p->nChange; pFrame->nDbChange = p->db->nChange; p->nChange = 0; p->pFrame = pFrame; p->aMem = aMem = &VdbeFrameMem(pFrame)[-1]; p->nMem = pFrame->nChildMem; p->nCursor = (u16)pFrame->nChildCsr; p->apCsr = (VdbeCursor **)&aMem[p->nMem+1]; p->aOp = aOp = pProgram->aOp; |
︙ | ︙ |
Changes to src/vdbeInt.h.
︙ | ︙ | |||
140 141 142 143 144 145 146 | int nCursor; /* Number of entries in apCsr */ int pc; /* Program Counter in parent (calling) frame */ int nOp; /* Size of aOp array */ int nMem; /* Number of entries in aMem */ int nOnceFlag; /* Number of entries in aOnceFlag */ int nChildMem; /* Number of memory cells for child frame */ int nChildCsr; /* Number of cursors for child frame */ | | > | 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | int nCursor; /* Number of entries in apCsr */ int pc; /* Program Counter in parent (calling) frame */ int nOp; /* Size of aOp array */ int nMem; /* Number of entries in aMem */ int nOnceFlag; /* Number of entries in aOnceFlag */ int nChildMem; /* Number of memory cells for child frame */ int nChildCsr; /* Number of cursors for child frame */ int nChange; /* Statement changes (Vdbe.nChange) */ int nDbChange; /* Value of db->nChange */ }; #define VdbeFrameMem(p) ((Mem *)&((u8 *)p)[ROUND8(sizeof(VdbeFrame))]) /* ** A value for VdbeCursor.cacheValid that means the cache is always invalid. */ |
︙ | ︙ |
Changes to src/vdbeaux.c.
︙ | ︙ | |||
1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 | v->nOp = pFrame->nOp; v->aMem = pFrame->aMem; v->nMem = pFrame->nMem; v->apCsr = pFrame->apCsr; v->nCursor = pFrame->nCursor; v->db->lastRowid = pFrame->lastRowid; v->nChange = pFrame->nChange; return pFrame->pc; } /* ** Close all cursors. ** ** Also release any dynamic memory held by the VM in the Vdbe.aMem memory | > | 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 | v->nOp = pFrame->nOp; v->aMem = pFrame->aMem; v->nMem = pFrame->nMem; v->apCsr = pFrame->apCsr; v->nCursor = pFrame->nCursor; v->db->lastRowid = pFrame->lastRowid; v->nChange = pFrame->nChange; v->db->nChange = pFrame->nDbChange; return pFrame->pc; } /* ** Close all cursors. ** ** Also release any dynamic memory held by the VM in the Vdbe.aMem memory |
︙ | ︙ | |||
2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 | }else{ /* We are forced to roll back the active transaction. Before doing ** so, abort any other statements this handle currently has active. */ sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK); sqlite3CloseSavepoints(db); db->autoCommit = 1; } } } /* Check for immediate foreign key violations. */ if( p->rc==SQLITE_OK ){ sqlite3VdbeCheckFk(p, 0); | > | 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 | }else{ /* We are forced to roll back the active transaction. Before doing ** so, abort any other statements this handle currently has active. */ sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK); sqlite3CloseSavepoints(db); db->autoCommit = 1; p->nChange = 0; } } } /* Check for immediate foreign key violations. */ if( p->rc==SQLITE_OK ){ sqlite3VdbeCheckFk(p, 0); |
︙ | ︙ | |||
2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 | } if( rc==SQLITE_BUSY && p->readOnly ){ sqlite3VdbeLeave(p); return SQLITE_BUSY; }else if( rc!=SQLITE_OK ){ p->rc = rc; sqlite3RollbackAll(db, SQLITE_OK); }else{ db->nDeferredCons = 0; db->nDeferredImmCons = 0; db->flags &= ~SQLITE_DeferFKs; sqlite3CommitInternalChanges(db); } }else{ sqlite3RollbackAll(db, SQLITE_OK); } db->nStatement = 0; }else if( eStatementOp==0 ){ if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){ eStatementOp = SAVEPOINT_RELEASE; }else if( p->errorAction==OE_Abort ){ eStatementOp = SAVEPOINT_ROLLBACK; }else{ sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK); sqlite3CloseSavepoints(db); db->autoCommit = 1; } } /* If eStatementOp is non-zero, then a statement transaction needs to ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to ** do so. If this operation returns an error, and the current statement ** error code is SQLITE_OK or SQLITE_CONSTRAINT, then promote the ** current statement error code. */ if( eStatementOp ){ rc = sqlite3VdbeCloseStatement(p, eStatementOp); if( rc ){ if( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ){ p->rc = rc; sqlite3DbFree(db, p->zErrMsg); p->zErrMsg = 0; } sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK); sqlite3CloseSavepoints(db); db->autoCommit = 1; } } /* If this was an INSERT, UPDATE or DELETE and no statement transaction ** has been rolled back, update the database connection change-counter. */ if( p->changeCntOn ){ | > > > > | 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 | } if( rc==SQLITE_BUSY && p->readOnly ){ sqlite3VdbeLeave(p); return SQLITE_BUSY; }else if( rc!=SQLITE_OK ){ p->rc = rc; sqlite3RollbackAll(db, SQLITE_OK); p->nChange = 0; }else{ db->nDeferredCons = 0; db->nDeferredImmCons = 0; db->flags &= ~SQLITE_DeferFKs; sqlite3CommitInternalChanges(db); } }else{ sqlite3RollbackAll(db, SQLITE_OK); p->nChange = 0; } db->nStatement = 0; }else if( eStatementOp==0 ){ if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){ eStatementOp = SAVEPOINT_RELEASE; }else if( p->errorAction==OE_Abort ){ eStatementOp = SAVEPOINT_ROLLBACK; }else{ sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK); sqlite3CloseSavepoints(db); db->autoCommit = 1; p->nChange = 0; } } /* If eStatementOp is non-zero, then a statement transaction needs to ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to ** do so. If this operation returns an error, and the current statement ** error code is SQLITE_OK or SQLITE_CONSTRAINT, then promote the ** current statement error code. */ if( eStatementOp ){ rc = sqlite3VdbeCloseStatement(p, eStatementOp); if( rc ){ if( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ){ p->rc = rc; sqlite3DbFree(db, p->zErrMsg); p->zErrMsg = 0; } sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK); sqlite3CloseSavepoints(db); db->autoCommit = 1; p->nChange = 0; } } /* If this was an INSERT, UPDATE or DELETE and no statement transaction ** has been rolled back, update the database connection change-counter. */ if( p->changeCntOn ){ |
︙ | ︙ |
Added test/e_changes.test.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 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 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 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 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 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 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 | # 2011 October 28 # # 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. # #*********************************************************************** # set testdir [file dirname $argv0] source $testdir/tester.tcl set testprefix e_changes # Like [do_execsql_test], except it appends the value returned by # [db changes] to the result of executing the SQL script. # proc do_changes_test {tn sql res} { uplevel [list \ do_test $tn "concat \[execsql {$sql}\] \[db changes\]" $res ] } #-------------------------------------------------------------------------- # EVIDENCE-OF: R-15996-49369 This function returns the number of rows # modified, inserted or deleted by the most recently completed INSERT, # UPDATE or DELETE statement on the database connection specified by the # only parameter. # do_execsql_test 1.0 { CREATE TABLE t1(a, b); CREATE TABLE t2(x, y, PRIMARY KEY(x, y)) WITHOUT ROWID; CREATE INDEX i1 ON t1(a); CREATE INDEX i2 ON t2(y); } foreach {tn schema} { 1 { CREATE TABLE t1(a, b); CREATE INDEX i1 ON t1(b); } 2 { CREATE TABLE t1(a, b, PRIMARY KEY(a, b)) WITHOUT ROWID; CREATE INDEX i1 ON t1(b); } } { reset_db execsql $schema # Insert 1 row. do_changes_test 1.$tn.1 { INSERT INTO t1 VALUES(0, 0) } 1 # Insert 10 rows. do_changes_test 1.$tn.2 { WITH rows(i, j) AS ( SELECT 1, 1 UNION ALL SELECT i+1, j+i FROM rows WHERE i<10 ) INSERT INTO t1 SELECT * FROM rows } 10 # Modify 5 rows. do_changes_test 1.$tn.3 { UPDATE t1 SET b=b+1 WHERE a<5; } 5 # Delete 4 rows do_changes_test 1.$tn.4 { DELETE FROM t1 WHERE a>6 } 4 # Check the "on the database connecton specified" part of hte # requirement - changes made by other connections do not show up in # the return value of sqlite3_changes(). do_test 1.$tn.5 { sqlite3 db2 test.db execsql { INSERT INTO t1 VALUES(-1, -1) } db2 db2 changes } 1 do_test 1.$tn.6 { db changes } 4 db2 close # Test that statements that modify no rows because they hit UNIQUE # constraints set the sqlite3_changes() value to 0. Regardless of # whether or not they are executed inside an explicit transaction. # # 1.$tn.8-9: outside of a transaction # 1.$tn.10-12: inside a transaction # do_changes_test 1.$tn.7 { CREATE UNIQUE INDEX i2 ON t1(a); } 4 do_catchsql_test 1.$tn.8 { INSERT INTO t1 VALUES('a', 0), ('b', 0), ('c', 0), (0, 11); } {1 {UNIQUE constraint failed: t1.a}} do_test 1.$tn.9 { db changes } 0 do_catchsql_test 1.$tn.10 { BEGIN; INSERT INTO t1 VALUES('a', 0), ('b', 0), ('c', 0), (0, 11); } {1 {UNIQUE constraint failed: t1.a}} do_test 1.$tn.11 { db changes } 0 do_changes_test 1.$tn.12 COMMIT 0 } #-------------------------------------------------------------------------- # EVIDENCE-OF: R-44877-05564 Executing any other type of SQL statement # does not modify the value returned by this function. # reset_db do_changes_test 2.1 { CREATE TABLE t1(x) } 0 do_changes_test 2.2 { WITH d(y) AS (SELECT 1 UNION ALL SELECT y+1 FROM d WHERE y<47) INSERT INTO t1 SELECT y FROM d; } 47 # The statement above set changes() to 47. Check that none of the following # modify this. do_changes_test 2.3 { SELECT count(x) FROM t1 } {47 47} do_changes_test 2.4 { DROP TABLE t1 } 47 do_changes_test 2.5 { CREATE TABLE t1(x) } 47 do_changes_test 2.6 { ALTER TABLE t1 ADD COLUMN b } 47 #-------------------------------------------------------------------------- # EVIDENCE-OF: R-53938-27527 Only changes made directly by the INSERT, # UPDATE or DELETE statement are considered - auxiliary changes caused # by triggers, foreign key actions or REPLACE constraint resolution are # not counted. # # 3.1.*: triggers # 3.2.*: foreign key actions # 3.3.*: replace constraints # reset_db do_execsql_test 3.1.0 { CREATE TABLE log(x); CREATE TABLE p1(one PRIMARY KEY, two); CREATE TRIGGER tr_ai AFTER INSERT ON p1 BEGIN INSERT INTO log VALUES('insert'); END; CREATE TRIGGER tr_bd BEFORE DELETE ON p1 BEGIN INSERT INTO log VALUES('delete'); END; CREATE TRIGGER tr_au AFTER UPDATE ON p1 BEGIN INSERT INTO log VALUES('update'); END; } do_changes_test 3.1.1 { INSERT INTO p1 VALUES('a', 'A'), ('b', 'B'), ('c', 'C'); } 3 do_changes_test 3.1.2 { UPDATE p1 SET two = two||two; } 3 do_changes_test 3.1.3 { DELETE FROM p1 WHERE one IN ('a', 'c'); } 2 do_execsql_test 3.1.4 { -- None of the inserts on table log were counted. SELECT count(*) FROM log } 8 do_execsql_test 3.2.0 { DELETE FROM p1; INSERT INTO p1 VALUES('a', 'A'), ('b', 'B'), ('c', 'C'); CREATE TABLE c1(a, b, FOREIGN KEY(a) REFERENCES p1 ON DELETE SET NULL); CREATE TABLE c2(a, b, FOREIGN KEY(a) REFERENCES p1 ON DELETE SET DEFAULT); CREATE TABLE c3(a, b, FOREIGN KEY(a) REFERENCES p1 ON DELETE CASCADE); INSERT INTO c1 VALUES('a', 'aaa'); INSERT INTO c2 VALUES('b', 'bbb'); INSERT INTO c3 VALUES('c', 'ccc'); INSERT INTO p1 VALUES('d', 'D'), ('e', 'E'), ('f', 'F'); CREATE TABLE c4(a, b, FOREIGN KEY(a) REFERENCES p1 ON UPDATE SET NULL); CREATE TABLE c5(a, b, FOREIGN KEY(a) REFERENCES p1 ON UPDATE SET DEFAULT); CREATE TABLE c6(a, b, FOREIGN KEY(a) REFERENCES p1 ON UPDATE CASCADE); INSERT INTO c4 VALUES('d', 'aaa'); INSERT INTO c5 VALUES('e', 'bbb'); INSERT INTO c6 VALUES('f', 'ccc'); PRAGMA foreign_keys = ON; } do_changes_test 3.2.1 { DELETE FROM p1 WHERE one = 'a' } 1 do_changes_test 3.2.2 { DELETE FROM p1 WHERE one = 'b' } 1 do_changes_test 3.2.3 { DELETE FROM p1 WHERE one = 'c' } 1 do_execsql_test 3.2.4 { SELECT * FROM c1; SELECT * FROM c2; SELECT * FROM c3; } {{} aaa {} bbb} do_changes_test 3.2.5 { UPDATE p1 SET one = 'g' WHERE one = 'd' } 1 do_changes_test 3.2.6 { UPDATE p1 SET one = 'h' WHERE one = 'e' } 1 do_changes_test 3.2.7 { UPDATE p1 SET one = 'i' WHERE one = 'f' } 1 do_execsql_test 3.2.8 { SELECT * FROM c4; SELECT * FROM c5; SELECT * FROM c6; } {{} aaa {} bbb i ccc} do_execsql_test 3.3.0 { CREATE TABLE r1(a UNIQUE, b UNIQUE); INSERT INTO r1 VALUES('i', 'i'); INSERT INTO r1 VALUES('ii', 'ii'); INSERT INTO r1 VALUES('iii', 'iii'); INSERT INTO r1 VALUES('iv', 'iv'); INSERT INTO r1 VALUES('v', 'v'); INSERT INTO r1 VALUES('vi', 'vi'); INSERT INTO r1 VALUES('vii', 'vii'); } do_changes_test 3.3.1 { INSERT OR REPLACE INTO r1 VALUES('i', 1) } 1 do_changes_test 3.3.2 { INSERT OR REPLACE INTO r1 VALUES('iv', 'v') } 1 do_changes_test 3.3.3 { UPDATE OR REPLACE r1 SET b='v' WHERE a='iii' } 1 do_changes_test 3.3.4 { UPDATE OR REPLACE r1 SET b='vi',a='vii' WHERE a='ii' } 1 do_execsql_test 3.3.5 { SELECT * FROM r1 ORDER BY a; } {i 1 iii v vii vi} #-------------------------------------------------------------------------- # EVIDENCE-OF: R-09813-48563 The value returned by sqlite3_changes() # immediately after an INSERT, UPDATE or DELETE statement run on a view # is always zero. # reset_db do_execsql_test 4.1 { CREATE TABLE log(log); CREATE TABLE t1(x, y); INSERT INTO t1 VALUES(1, 2); INSERT INTO t1 VALUES(3, 4); INSERT INTO t1 VALUES(5, 6); CREATE VIEW v1 AS SELECT * FROM t1; CREATE TRIGGER v1_i INSTEAD OF INSERT ON v1 BEGIN INSERT INTO log VALUES('insert'); END; CREATE TRIGGER v1_u INSTEAD OF UPDATE ON v1 BEGIN INSERT INTO log VALUES('update'), ('update'); END; CREATE TRIGGER v1_d INSTEAD OF DELETE ON v1 BEGIN INSERT INTO log VALUES('delete'), ('delete'), ('delete'); END; } do_changes_test 4.2.1 { INSERT INTO t1 SELECT * FROM t1 } 3 do_changes_test 4.2.2 { INSERT INTO v1 VALUES(1, 2) } 0 do_changes_test 4.3.1 { INSERT INTO t1 SELECT * FROM t1 } 6 do_changes_test 4.3.2 { UPDATE v1 SET y='xyz' WHERE x=1 } 0 do_changes_test 4.4.1 { INSERT INTO t1 SELECT * FROM t1 } 12 do_changes_test 4.4.2 { DELETE FROM v1 WHERE x=5 } 0 #-------------------------------------------------------------------------- # EVIDENCE-OF: R-32918-61474 Before entering a trigger program the value # returned by sqlite3_changes() function is saved. After the trigger # program has finished, the original value is restored. # reset_db db func my_changes my_changes set ::changes [list] proc my_changes {x} { set res [db changes] lappend ::changes $x $res return $res } do_execsql_test 5.1.0 { CREATE TABLE t1(a INTEGER PRIMARY KEY, b); CREATE TABLE t2(x); INSERT INTO t1 VALUES(1, NULL); INSERT INTO t1 VALUES(2, NULL); INSERT INTO t1 VALUES(3, NULL); CREATE TRIGGER AFTER UPDATE ON t1 BEGIN INSERT INTO t2 VALUES('a'), ('b'), ('c'); SELECT my_changes('trigger'); END; } do_execsql_test 5.1.1 { INSERT INTO t2 VALUES('a'), ('b'); UPDATE t1 SET b = my_changes('update'); SELECT * FROM t1; } {1 2 2 2 3 2} # Value is being restored to "2" when the trigger program exits. do_test 5.1.2 { set ::changes } {update 2 trigger 3 update 2 trigger 3 update 2 trigger 3} reset_db do_execsql_test 5.2.0 { CREATE TABLE t1(a, b); CREATE TABLE log(x); INSERT INTO t1 VALUES(1, 0); INSERT INTO t1 VALUES(2, 0); INSERT INTO t1 VALUES(3, 0); CREATE TRIGGER t1_a_u AFTER UPDATE ON t1 BEGIN INSERT INTO log VALUES(old.b || ' -> ' || new.b || ' c = ' || changes() ); END; CREATE TABLE t2(a); INSERT INTO t2 VALUES(1), (2), (3); UPDATE t1 SET b = changes(); } do_execsql_test 5.2.1 { SELECT * FROM t1; } {1 3 2 3 3 3} do_execsql_test 5.2.2 { SELECT * FROM log; } {{0 -> 3 c = 3} {0 -> 3 c = 3} {0 -> 3 c = 3}} #-------------------------------------------------------------------------- # EVIDENCE-OF: R-17146-37073 Within a trigger program each INSERT, # UPDATE and DELETE statement sets the value returned by # sqlite3_changes() upon completion as normal. Of course, this value # will not include any changes performed by sub-triggers, as the # sqlite3_changes() value will be saved and restored after each # sub-trigger has run. reset_db do_execsql_test 6.0 { CREATE TABLE t1(a, b); CREATE TABLE t2(a, b); CREATE TABLE t3(a, b); CREATE TABLE log(x); CREATE TRIGGER t1_i BEFORE INSERT ON t1 BEGIN INSERT INTO t2 VALUES(new.a, new.b), (new.a, new.b); INSERT INTO log VALUES('t2->' || changes()); END; CREATE TRIGGER t2_i AFTER INSERT ON t2 BEGIN INSERT INTO t3 VALUES(new.a, new.b), (new.a, new.b), (new.a, new.b); INSERT INTO log VALUES('t3->' || changes()); END; CREATE TRIGGER t1_u AFTER UPDATE ON t1 BEGIN UPDATE t2 SET b=new.b WHERE a=old.a; INSERT INTO log VALUES('t2->' || changes()); END; CREATE TRIGGER t2_u BEFORE UPDATE ON t2 BEGIN UPDATE t3 SET b=new.b WHERE a=old.a; INSERT INTO log VALUES('t3->' || changes()); END; CREATE TRIGGER t1_d AFTER DELETE ON t1 BEGIN DELETE FROM t2 WHERE a=old.a AND b=old.b; INSERT INTO log VALUES('t2->' || changes()); END; CREATE TRIGGER t2_d BEFORE DELETE ON t2 BEGIN DELETE FROM t3 WHERE a=old.a AND b=old.b; INSERT INTO log VALUES('t3->' || changes()); END; } do_changes_test 6.1 { INSERT INTO t1 VALUES('+', 'o'); SELECT * FROM log; } {t3->3 t3->3 t2->2 1} do_changes_test 6.2 { DELETE FROM log; UPDATE t1 SET b='*'; SELECT * FROM log; } {t3->6 t3->6 t2->2 1} do_changes_test 6.3 { DELETE FROM log; DELETE FROM t1; SELECT * FROM log; } {t3->6 t3->0 t2->2 1} #-------------------------------------------------------------------------- # EVIDENCE-OF: R-43399-09409 This means that if the changes() SQL # function (or similar) is used by the first INSERT, UPDATE or DELETE # statement within a trigger, it returns the value as set when the # calling statement began executing. # # EVIDENCE-OF: R-53215-27584 If it is used by the second or subsequent # such statement within a trigger program, the value returned reflects # the number of rows modified by the previous INSERT, UPDATE or DELETE # statement within the same trigger. # reset_db do_execsql_test 7.1 { CREATE TABLE q1(t); CREATE TABLE q2(u, v); CREATE TABLE q3(w); CREATE TRIGGER q2_insert BEFORE INSERT ON q2 BEGIN /* changes() returns value from previous I/U/D in callers context */ INSERT INTO q1 VALUES('1:' || changes()); /* changes() returns value of previous I/U/D in this context */ INSERT INTO q3 VALUES(changes()), (2), (3); INSERT INTO q1 VALUES('2:' || changes()); INSERT INTO q3 VALUES(changes() + 3), (changes()+4); SELECT 'this does not affect things!'; INSERT INTO q1 VALUES('3:' || changes()); UPDATE q3 SET w = w+10 WHERE w%2; INSERT INTO q1 VALUES('4:' || changes()); DELETE FROM q3; INSERT INTO q1 VALUES('5:' || changes()); END; } do_execsql_test 7.2 { INSERT INTO q2 VALUES('x', 'y'); SELECT * FROM q1; } { 1:0 2:3 3:2 4:3 5:5 } do_execsql_test 7.3 { DELETE FROM q1; INSERT INTO q2 VALUES('x', 'y'); SELECT * FROM q1; } { 1:5 2:3 3:2 4:3 5:5 } finish_test |