Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Add further tests for changebatch. And a fix to prevent a changeset from conflicting with itself. |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | changebatch |
Files: | files | file ages | folders |
SHA1: |
506d6ff4b64c72d4ca65f0d15e1fdf8a |
User & Date: | dan 2016-08-23 19:02:21.914 |
Context
2016-08-24
| ||
19:14 | Add the sqlite3changebatch_db() API. (check-in: bee44ebc53 user: dan tags: changebatch) | |
2016-08-23
| ||
19:02 | Add further tests for changebatch. And a fix to prevent a changeset from conflicting with itself. (check-in: 506d6ff4b6 user: dan tags: changebatch) | |
18:09 | Fix harmless compiler warnings in changebatch. (check-in: a721a73818 user: drh tags: changebatch) | |
Changes
Changes to ext/session/changebatch1.test.
︙ | ︙ | |||
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 | if {![info exists testdir]} { set testdir [file join [file dirname [info script]] .. .. test] } source $testdir/tester.tcl ifcapable !session {finish_test; return} set testprefix changebatch1 proc sql_to_changeset {method sql} { sqlite3session S db main S attach * execsql $sql set ret [S $method] S delete return $ret } proc do_changebatch_test {tn method args} { set C [list] foreach a $args { lappend C [sql_to_changeset $method $a] } sqlite3changebatch cb db set i 1 foreach ::cs [lrange $C 0 end-1] { | > | > | < | 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 | if {![info exists testdir]} { set testdir [file join [file dirname [info script]] .. .. test] } source $testdir/tester.tcl ifcapable !session {finish_test; return} set testprefix changebatch1 proc sql_to_changeset {method sql} { sqlite3session S db main S attach * execsql $sql set ret [S $method] S delete return $ret } proc do_changebatch_test {tn method args} { set C [list] foreach a $args { lappend C [sql_to_changeset $method $a] } sqlite3changebatch cb db set i 1 foreach ::cs [lrange $C 0 end-1] { set rc [cb add $::cs] if {$rc!="SQLITE_OK"} { error "expected SQLITE_OK, got $rc (i=$i)" } incr i } set ::cs [lindex $C end] do_test $tn { cb add [set ::cs] } SQLITE_CONSTRAINT cb delete } proc do_changebatch_test1 {tn args} { uplevel do_changebatch_test $tn changeset $args } proc do_changebatch_test2 {tn args} { |
︙ | ︙ | |||
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 | } #------------------------------------------------------------------------- # do_execsql_test $tn.2.0 { CREATE TABLE x1(a, b PRIMARY KEY, c UNIQUE); CREATE TABLE x2(a PRIMARY KEY, b UNIQUE, c UNIQUE); INSERT INTO x1 VALUES(1, 1, 'a'); INSERT INTO x1 VALUES(1, 2, 'b'); INSERT INTO x1 VALUES(1, 3, 'c'); } $testfunction $tn.2.1 { DELETE FROM x1 WHERE b=2; } { UPDATE x1 SET c='b' WHERE b=3; } $testfunction $tn.2.2 { DELETE FROM x1 WHERE b=1; } { INSERT INTO x1 VALUES(1, 5, 'a'); } } #------------------------------------------------------------------------- # Test some multi-column UNIQUE constraints. First Using _changeset() to # demonstrate the problem, then using _fullchangeset() to show that it has # been fixed. # | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 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 | } #------------------------------------------------------------------------- # do_execsql_test $tn.2.0 { CREATE TABLE x1(a, b PRIMARY KEY, c UNIQUE); CREATE TABLE x2(a PRIMARY KEY, b UNIQUE, c UNIQUE); CREATE INDEX x1a ON x1(a); INSERT INTO x1 VALUES(1, 1, 'a'); INSERT INTO x1 VALUES(1, 2, 'b'); INSERT INTO x1 VALUES(1, 3, 'c'); } $testfunction $tn.2.1 { DELETE FROM x1 WHERE b=2; } { UPDATE x1 SET c='b' WHERE b=3; } $testfunction $tn.2.2 { DELETE FROM x1 WHERE b=1; } { INSERT INTO x1 VALUES(1, 5, 'a'); } set L [list] for {set i 1000} {$i < 10000} {incr i} { lappend L "INSERT INTO x2 VALUES($i, $i, 'x' || $i)" } lappend L "DELETE FROM x2 WHERE b=1005" $testfunction $tn.2.3 {*}$L execsql { INSERT INTO x1 VALUES('f', 'f', 'f') } $testfunction $tn.2.4 { INSERT INTO x2 VALUES('f', 'f', 'f'); } { INSERT INTO x1 VALUES('g', 'g', 'g'); } { DELETE FROM x1 WHERE b='f'; } { INSERT INTO x2 VALUES('g', 'g', 'g'); } { INSERT INTO x1 VALUES('f', 'f', 'f'); } execsql { DELETE FROM x1; INSERT INTO x1 VALUES(1.5, 1.5, 1.5); } $testfunction $tn.2.5 { DELETE FROM x1 WHERE b BETWEEN 1 AND 2; } { INSERT INTO x1 VALUES(2.5, 2.5, 2.5); } { INSERT INTO x1 VALUES(1.5, 1.5, 1.5); } execsql { DELETE FROM x2; INSERT INTO x2 VALUES(X'abcd', X'1234', X'7890'); INSERT INTO x2 VALUES(X'0000', X'0000', X'0000'); } breakpoint $testfunction $tn.2.6 { UPDATE x2 SET c = X'1234' WHERE a=X'abcd'; INSERT INTO x2 VALUES(X'1234', X'abcd', X'7890'); } { DELETE FROM x2 WHERE b=X'0000'; } { INSERT INTO x2 VALUES(1, X'0000', NULL); } } #------------------------------------------------------------------------- # Test some multi-column UNIQUE constraints. First Using _changeset() to # demonstrate the problem, then using _fullchangeset() to show that it has # been fixed. # |
︙ | ︙ | |||
147 148 149 150 151 152 153 154 155 156 | cb add $c1 cb add $c2 } {SQLITE_CONSTRAINT} do_test 3.1.2 { cb delete } {} finish_test | > > > > > > > > > > > > > > > > > < | 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 | cb add $c1 cb add $c2 } {SQLITE_CONSTRAINT} do_test 3.1.2 { cb delete } {} #------------------------------------------------------------------------- # reset_db do_execsql_test 4.0 { CREATE TABLE t1(x, y, z, PRIMARY KEY(x, y), UNIQUE(z)); } do_test 4.1 { set c1 [sql_to_changeset fullchangeset { INSERT INTO t1 VALUES(1, 2, 3) }] execsql { DROP TABLE t1; CREATE TABLE t1(w, x, y, z, PRIMARY KEY(x, y), UNIQUE(z)); } sqlite3changebatch cb db list [catch { cb add $c1 } msg] $msg } {1 SQLITE_RANGE} finish_test |
Added ext/session/changebatchfault.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 | # 2011 Mar 21 # # 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. # #*********************************************************************** # # The focus of this file is testing the session module. # if {![info exists testdir]} { set testdir [file join [file dirname [info script]] .. .. test] } source [file join [file dirname [info script]] session_common.tcl] source $testdir/tester.tcl ifcapable !session {finish_test; return} set testprefix changebatchfault do_execsql_test 1.0 { CREATE TABLE t1(a, b, c PRIMARY KEY, UNIQUE(a, b)); INSERT INTO t1 VALUES('a', 'a', 'a'); INSERT INTO t1 VALUES('b', 'b', 'b'); } set ::c1 [changeset_from_sql { delete from t1 where c='a' }] set ::c2 [changeset_from_sql { insert into t1 values('c', 'c', 'c') }] do_faultsim_test 1 -faults oom-* -body { sqlite3changebatch cb db cb add $::c1 cb add $::c2 } -test { faultsim_test_result {0 SQLITE_OK} {1 SQLITE_NOMEM} catch { cb delete } } finish_test |
Changes to ext/session/sqlite3changebatch.c.
︙ | ︙ | |||
150 151 152 153 154 155 156 | rc = sqlite3_prepare_v2(p->db, zIndexInfo, -1, &pIndexInfo, 0); sqlite3_free(zIndexInfo); }else{ rc = SQLITE_NOMEM; } if( rc==SQLITE_OK ){ | < | < | < > > > > > > > > > > > > > | 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 | rc = sqlite3_prepare_v2(p->db, zIndexInfo, -1, &pIndexInfo, 0); sqlite3_free(zIndexInfo); }else{ rc = SQLITE_NOMEM; } if( rc==SQLITE_OK ){ while( SQLITE_ROW==sqlite3_step(pIndexInfo) ){ nCol++; } rc = sqlite3_reset(pIndexInfo); } pNew = (BatchIndex*)cbMalloc(&rc, sizeof(BatchIndex) + sizeof(int) * nCol); if( rc==SQLITE_OK ){ int rc2; pNew->nCol = nCol; pNew->bPk = bPk; pNew->aiCol = (int*)&pNew[1]; pNew->iId = p->iNextIdxId++; while( SQLITE_ROW==sqlite3_step(pIndexInfo) ){ int i = sqlite3_column_int(pIndexInfo, 0); int j = sqlite3_column_int(pIndexInfo, 1); pNew->aiCol[i] = j; } rc = sqlite3_reset(pIndexInfo); } if( rc==SQLITE_OK ){ pNew->pNext = pTab->pIdx; pTab->pIdx = pNew; }else{ cbFree(pNew); } sqlite3_finalize(pIndexInfo); return rc; } /* ** Free the object passed as the first argument. */ static void cbFreeTable(BatchTable *pTab){ BatchIndex *pIdx; BatchIndex *pIdxNext; for(pIdx=pTab->pIdx; pIdx; pIdx=pIdxNext){ pIdxNext = pIdx->pNext; cbFree(pIdx); } cbFree(pTab); } /* ** Find or create the BatchTable object named zTab. */ static int cbFindTable( sqlite3_changebatch *p, const char *zTab, |
︙ | ︙ | |||
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | } rc2 = sqlite3_finalize(pIndexList); if( rc==SQLITE_OK ) rc = rc2; if( rc==SQLITE_OK ){ pRet->pNext = p->pTab; p->pTab = pRet; } } } *ppTab = pRet; return rc; } static int cbGetChangesetValue( sqlite3_changeset_iter *pIter, int (*xVal)(sqlite3_changeset_iter*,int,sqlite3_value**), int (*xFallback)(sqlite3_changeset_iter*,int,sqlite3_value**), int iVal, sqlite3_value **ppVal ){ | > > > > > > > > > > > > > | 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 | } rc2 = sqlite3_finalize(pIndexList); if( rc==SQLITE_OK ) rc = rc2; if( rc==SQLITE_OK ){ pRet->pNext = p->pTab; p->pTab = pRet; }else{ cbFreeTable(pRet); pRet = 0; } } } *ppTab = pRet; return rc; } /* ** Extract value iVal from the changeset iterator passed as the first ** argument. Set *ppVal to point to the value before returning. ** ** This function attempts to extract the value using function xVal ** (which is always either sqlite3changeset_new or sqlite3changeset_old). ** If the call returns SQLITE_OK but does not supply an sqlite3_value* ** pointer, an attempt to extract the value is made using the xFallback ** function. */ static int cbGetChangesetValue( sqlite3_changeset_iter *pIter, int (*xVal)(sqlite3_changeset_iter*,int,sqlite3_value**), int (*xFallback)(sqlite3_changeset_iter*,int,sqlite3_value**), int iVal, sqlite3_value **ppVal ){ |
︙ | ︙ | |||
296 297 298 299 300 301 302 | pNew = cbMalloc(&rc, sizeof(BatchIndexEntry) + sz); if( pNew ){ pNew->iChangesetId = p->iChangesetId; pNew->iIdxId = pIdx->iId; pNew->szRecord = sz; | | > | | | | | | | | | | | | | | | | | | | | | | < | 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 | pNew = cbMalloc(&rc, sizeof(BatchIndexEntry) + sz); if( pNew ){ pNew->iChangesetId = p->iChangesetId; pNew->iIdxId = pIdx->iId; pNew->szRecord = sz; for(i=0; i<pIdx->nCol; i++){ int eType; sqlite3_value *pVal; rc = cbGetChangesetValue(pIter, xVal, xFallback, pIdx->aiCol[i], &pVal); if( rc!=SQLITE_OK ) break; /* coverage: condition is never true */ eType = sqlite3_value_type(pVal); pNew->aRecord[iOut++] = eType; switch( eType ){ case SQLITE_INTEGER: { sqlite3_int64 i64 = sqlite3_value_int64(pVal); memcpy(&pNew->aRecord[iOut], &i64, 8); iOut += 8; break; } case SQLITE_FLOAT: { double d64 = sqlite3_value_double(pVal); memcpy(&pNew->aRecord[iOut], &d64, sizeof(double)); iOut += sizeof(double); break; } default: { int nByte = sqlite3_value_bytes(pVal); const char *z = (const char*)sqlite3_value_blob(pVal); memcpy(&pNew->aRecord[iOut], z, nByte); iOut += nByte; break; } } } } if( rc==SQLITE_OK && p->nEntry>=(p->nHash/2) ){ rc = cbHashResize(p); |
︙ | ︙ | |||
355 356 357 358 359 360 361 362 363 | } if( pNew ){ pNew->pNext = p->apHash[iHash]; p->apHash[iHash] = pNew; p->nEntry++; } } | > > < | 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 | } if( pNew ){ pNew->pNext = p->apHash[iHash]; p->apHash[iHash] = pNew; p->nEntry++; } }else{ cbFree(pNew); } return rc; } /* ** Add a changeset to the current batch. */ |
︙ | ︙ | |||
387 388 389 390 391 392 393 | int nCol; /* Number of columns in table */ int op; /* UPDATE, INSERT or DELETE */ sqlite3changeset_op(pIter, &zTab, &nCol, &op, 0); assert( op==SQLITE_INSERT || op==SQLITE_UPDATE || op==SQLITE_DELETE ); rc = cbFindTable(p, zTab, &pTab); | > > | | | | | | | | | > > | 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 442 443 444 445 446 447 448 449 | int nCol; /* Number of columns in table */ int op; /* UPDATE, INSERT or DELETE */ sqlite3changeset_op(pIter, &zTab, &nCol, &op, 0); assert( op==SQLITE_INSERT || op==SQLITE_UPDATE || op==SQLITE_DELETE ); rc = cbFindTable(p, zTab, &pTab); assert( pTab || rc!=SQLITE_OK ); if( pTab ){ for(pIdx=pTab->pIdx; pIdx && rc==SQLITE_OK; pIdx=pIdx->pNext){ if( op==SQLITE_UPDATE && pIdx->bPk ) continue; if( op==SQLITE_UPDATE || op==SQLITE_DELETE ){ rc = cbAddToHash(p, pIter, pIdx, sqlite3changeset_old, 0, &bConf); } if( op==SQLITE_UPDATE || op==SQLITE_INSERT ){ rc = cbAddToHash(p, pIter, pIdx, sqlite3changeset_new, sqlite3changeset_old, &bConf ); } } } if( rc!=SQLITE_OK ) break; } rc2 = sqlite3changeset_finalize(pIter); if( rc==SQLITE_OK ) rc = rc2; } if( rc==SQLITE_OK && bConf ){ rc = SQLITE_CONSTRAINT; } p->iChangesetId++; return rc; } /* ** Zero an existing changebatch object. */ void sqlite3changebatch_zero(sqlite3_changebatch *p){ |
︙ | ︙ | |||
438 439 440 441 442 443 444 | */ void sqlite3changebatch_delete(sqlite3_changebatch *p){ BatchTable *pTab; BatchTable *pTabNext; sqlite3changebatch_zero(p); for(pTab=p->pTab; pTab; pTab=pTabNext){ | < < < < < < | | 466 467 468 469 470 471 472 473 474 475 476 477 478 479 | */ void sqlite3changebatch_delete(sqlite3_changebatch *p){ BatchTable *pTab; BatchTable *pTabNext; sqlite3changebatch_zero(p); for(pTab=p->pTab; pTab; pTab=pTabNext){ pTabNext = pTab->pNext; cbFreeTable(pTab); } cbFree(p); } #endif /* SQLITE_ENABLE_SESSION && SQLITE_ENABLE_PREUPDATE_HOOK */ |