Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Fix handling of return values from the conflict handler. Document the conflict handler arguments and return codes in sqlite3session.h. |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | sessions |
Files: | files | file ages | folders |
SHA1: |
cbbb274e500237dbf7155a51d4f9c175 |
User & Date: | dan 2011-03-14 19:49:23.000 |
Context
2011-03-15
| ||
16:37 | Fix some bugs and other code issues in the session module. (check-in: f2930840e4 user: dan tags: sessions) | |
2011-03-14
| ||
19:49 | Fix handling of return values from the conflict handler. Document the conflict handler arguments and return codes in sqlite3session.h. (check-in: cbbb274e50 user: dan tags: sessions) | |
2011-03-12
| ||
17:22 | Fix some issues with UPDATE changes in the session module. (check-in: 57862efe71 user: dan tags: sessions) | |
Changes
Changes to ext/session/sqlite3session.c.
︙ | ︙ | |||
1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 | zSep = "AND "; } } sessionAppendStr(pBuf, ")", pRc); } } /* ** Formulate a statement to DELETE a row from database db. Assuming a table ** structure like this: ** ** CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c)); ** ** The DELETE statement looks like this: ** ** DELETE FROM x WHERE a = :1 AND c = :3 AND :5 OR (b IS :2 AND d IS :4) ** ** Variable :5 (nCol+1) is a boolean. It should be set to 0 if we require ** matching b and d values, or 1 otherwise. The second case comes up if the ** conflict handler is invoked with NOTFOUND and returns CHANGESET_REPLACE. */ static int sessionDeleteRow( sqlite3 *db, /* Database handle */ const char *zTab, /* Table name */ | > > > > > > > > > > > > > | < < < < | | | < | | | | | < | 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 | zSep = "AND "; } } sessionAppendStr(pBuf, ")", pRc); } } typedef struct SessionApplyCtx SessionApplyCtx; struct SessionApplyCtx { sqlite3 *db; sqlite3_stmt *pDelete; /* DELETE statement */ sqlite3_stmt *pUpdate; /* DELETE statement */ sqlite3_stmt *pInsert; /* INSERT statement */ sqlite3_stmt *pSelect; /* SELECT statement */ int nCol; /* Size of azCol[] and abPK[] arrays */ const char **azCol; /* Array of column names */ u8 *abPK; /* Boolean array - true if column is in PK */ }; /* ** Formulate a statement to DELETE a row from database db. Assuming a table ** structure like this: ** ** CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c)); ** ** The DELETE statement looks like this: ** ** DELETE FROM x WHERE a = :1 AND c = :3 AND :5 OR (b IS :2 AND d IS :4) ** ** Variable :5 (nCol+1) is a boolean. It should be set to 0 if we require ** matching b and d values, or 1 otherwise. The second case comes up if the ** conflict handler is invoked with NOTFOUND and returns CHANGESET_REPLACE. */ static int sessionDeleteRow( sqlite3 *db, /* Database handle */ const char *zTab, /* Table name */ SessionApplyCtx *p /* Session changeset-apply context */ ){ int rc = SQLITE_OK; SessionBuffer buf = {0, 0, 0}; sessionAppendStr(&buf, "DELETE FROM ", &rc); sessionAppendIdent(&buf, zTab, &rc); sessionUpdateDeleteWhere(&buf, p->nCol, p->azCol, p->abPK, &rc); if( rc==SQLITE_OK ){ rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pDelete, 0); } sqlite3_free(buf.aBuf); return rc; } /* ** Formulate and prepare a statement to UPDATE a row from database db. ** Assuming a table structure like this: |
︙ | ︙ | |||
1347 1348 1349 1350 1351 1352 1353 | ** conflict-handler is invoked with CHANGESET_DATA and returns ** CHANGESET_REPLACE. This is variable "?(nCol*3+1)". ** */ static int sessionUpdateRow( sqlite3 *db, /* Database handle */ const char *zTab, /* Table name */ | | < < < < | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | < > > > | | > | > > > > > > > > > > > > > > > > > > | > > > > < | < | | | | | < | < | < < < < | > | < | | | | < | | < < | | | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | > | > > > > > | > > | > > > | > | > > > | | < | | > > > > > > > > > > > > > > > > > | > > | > > > > > > > > > > | > > > > | > > | > > > | > > | > > > > > > > > > > > > > > > > | > > > | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | > > > > | > > > > > > | | | | | < < < < < < | > > > | | > | | < < < | | | | > > > | > > | | | > | | < < < < < < < < < | | < < > < < | | < < < < < < < < < | < < < < < < < < < < < | < | < < < < | | < < | < < | > | < < < | < | < < < < < < < < < < < < < < < < < < | < | < < < < < < < < < | < > | | < < < < < | < < < < | < > | > | < | < < < < < < < < < < < < < < < < < < < < < < < < | < > | < < < < < < | | | | < | < < < < < < | < < | | | | | | | 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 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 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 | ** conflict-handler is invoked with CHANGESET_DATA and returns ** CHANGESET_REPLACE. This is variable "?(nCol*3+1)". ** */ static int sessionUpdateRow( sqlite3 *db, /* Database handle */ const char *zTab, /* Table name */ SessionApplyCtx *p /* Session changeset-apply context */ ){ int rc = SQLITE_OK; int i; const char *zSep = ""; SessionBuffer buf = {0, 0, 0}; /* Append "UPDATE tbl SET " */ sessionAppendStr(&buf, "UPDATE ", &rc); sessionAppendIdent(&buf, zTab, &rc); sessionAppendStr(&buf, " SET ", &rc); /* Append the assignments */ for(i=0; i<p->nCol; i++){ sessionAppendStr(&buf, zSep, &rc); sessionAppendIdent(&buf, p->azCol[i], &rc); sessionAppendStr(&buf, " = CASE WHEN ?", &rc); sessionAppendInteger(&buf, i*3+2, &rc); sessionAppendStr(&buf, " THEN ?", &rc); sessionAppendInteger(&buf, i*3+3, &rc); sessionAppendStr(&buf, " ELSE ", &rc); sessionAppendIdent(&buf, p->azCol[i], &rc); sessionAppendStr(&buf, " END", &rc); zSep = ", "; } /* Append the PK part of the WHERE clause */ sessionAppendStr(&buf, " WHERE ", &rc); for(i=0; i<p->nCol; i++){ if( p->abPK[i] ){ sessionAppendIdent(&buf, p->azCol[i], &rc); sessionAppendStr(&buf, " = ?", &rc); sessionAppendInteger(&buf, i*3+1, &rc); sessionAppendStr(&buf, " AND ", &rc); } } /* Append the non-PK part of the WHERE clause */ sessionAppendStr(&buf, " (?", &rc); sessionAppendInteger(&buf, p->nCol*3+1, &rc); sessionAppendStr(&buf, " OR 1", &rc); for(i=0; i<p->nCol; i++){ if( !p->abPK[i] ){ sessionAppendStr(&buf, " AND (?", &rc); sessionAppendInteger(&buf, i*3+2, &rc); sessionAppendStr(&buf, "=0 OR ", &rc); sessionAppendIdent(&buf, p->azCol[i], &rc); sessionAppendStr(&buf, " IS ?", &rc); sessionAppendInteger(&buf, i*3+1, &rc); sessionAppendStr(&buf, ")", &rc); } } sessionAppendStr(&buf, ")", &rc); if( rc==SQLITE_OK ){ rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pUpdate, 0); } sqlite3_free(buf.aBuf); return rc; } static int sessionSelectRow( sqlite3 *db, /* Database handle */ const char *zTab, /* Table name */ SessionApplyCtx *p /* Session changeset-apply context */ ){ int rc = SQLITE_OK; int i; const char *zSep = ""; SessionBuffer buf = {0, 0, 0}; sessionAppendStr(&buf, "SELECT * FROM ", &rc); sessionAppendIdent(&buf, zTab, &rc); sessionAppendStr(&buf, " WHERE ", &rc); for(i=0; i<p->nCol; i++){ if( p->abPK[i] ){ sessionAppendStr(&buf, zSep, &rc); sessionAppendIdent(&buf, p->azCol[i], &rc); sessionAppendStr(&buf, " = ?", &rc); sessionAppendInteger(&buf, i+1, &rc); zSep = " AND "; } } if( rc==SQLITE_OK ){ rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pSelect, 0); } sqlite3_free(buf.aBuf); return rc; } static int sessionInsertRow( sqlite3 *db, /* Database handle */ const char *zTab, /* Table name */ SessionApplyCtx *p /* Session changeset-apply context */ ){ int rc = SQLITE_OK; int i; SessionBuffer buf = {0, 0, 0}; sessionAppendStr(&buf, "INSERT INTO main.", &rc); sessionAppendIdent(&buf, zTab, &rc); sessionAppendStr(&buf, " VALUES(?", &rc); for(i=1; i<p->nCol; i++){ sessionAppendStr(&buf, ", ?", &rc); } sessionAppendStr(&buf, ")", &rc); if( rc==SQLITE_OK ){ rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pInsert, 0); } sqlite3_free(buf.aBuf); return rc; } static int sessionSeekToRow( sqlite3 *db, /* Database handle */ sqlite3_changeset_iter *pIter, /* Changeset iterator */ u8 *abPK, /* Primary key flags array */ sqlite3_stmt *pSelect /* SELECT statement from sessionSelectRow() */ ){ int rc = SQLITE_OK; int i; int nCol; int op; const char *zDummy; sqlite3changeset_op(pIter, &zDummy, &nCol, &op); for(i=0; rc==SQLITE_OK && i<nCol; i++){ if( abPK[i] ){ sqlite3_value *pVal = 0; if( op!=SQLITE_DELETE ){ rc = sqlite3changeset_new(pIter, i, &pVal); } if( pVal==0 ){ rc = sqlite3changeset_old(pIter, i, &pVal); } if( rc==SQLITE_OK ){ rc = sqlite3_bind_value(pSelect, i+1, pVal); } } } if( rc==SQLITE_OK ){ rc = sqlite3_step(pSelect); if( rc!=SQLITE_ROW ) rc = sqlite3_reset(pSelect); } return rc; } static int sessionConflictHandler( int eType, SessionApplyCtx *p, sqlite3_changeset_iter *pIter, /* Changeset iterator */ int(*xConflict)(void *, int, sqlite3_changeset_iter*), void *pCtx, int *pbReplace ){ int res; int rc; int nCol; int op; const char *zDummy; sqlite3changeset_op(pIter, &zDummy, &nCol, &op); assert( eType==SQLITE_CHANGESET_CONFLICT || eType==SQLITE_CHANGESET_DATA ); assert( SQLITE_CHANGESET_CONFLICT+1==SQLITE_CHANGESET_CONSTRAINT ); assert( SQLITE_CHANGESET_DATA+1==SQLITE_CHANGESET_NOTFOUND ); /* Bind the new.* PRIMARY KEY values to the SELECT statement. */ if( pbReplace ){ rc = sessionSeekToRow(p->db, pIter, p->abPK, p->pSelect); }else{ rc = SQLITE_DONE; } if( rc==SQLITE_ROW ){ /* There exists another row with the new.* primary key. */ pIter->pConflict = p->pSelect; res = xConflict(pCtx, eType, pIter); pIter->pConflict = 0; rc = sqlite3_reset(p->pSelect); }else{ /* No other row with the new.* primary key. */ rc = sqlite3_reset(p->pSelect); if( rc==SQLITE_OK ){ res = xConflict(pCtx, eType+1, pIter); if( res==SQLITE_CHANGESET_REPLACE ) rc = SQLITE_MISUSE; } } if( rc==SQLITE_OK ){ switch( res ){ case SQLITE_CHANGESET_REPLACE: if( pbReplace ) *pbReplace = 1; break; case SQLITE_CHANGESET_OMIT: break; case SQLITE_CHANGESET_ABORT: rc = SQLITE_ABORT; break; default: rc = SQLITE_MISUSE; break; } } return rc; } static int sessionApplyOneOp( sqlite3_changeset_iter *pIter, SessionApplyCtx *p, int(*xConflict)(void *, int, sqlite3_changeset_iter *), void *pCtx, int *pbReplace, int *pbRetry ){ const char *zDummy; int op; int nCol; int rc = SQLITE_OK; assert( p->pDelete && p->pUpdate && p->pInsert && p->pSelect ); assert( p->azCol && p->abPK ); assert( !pbReplace || *pbReplace==0 ); sqlite3changeset_op(pIter, &zDummy, &nCol, &op); if( op==SQLITE_DELETE ){ int i; /* Bind values to the DELETE statement. */ for(i=0; rc==SQLITE_OK && i<nCol; i++){ sqlite3_value *pVal; rc = sqlite3changeset_old(pIter, i, &pVal); if( rc==SQLITE_OK ){ rc = sqlite3_bind_value(p->pDelete, i+1, pVal); } } if( rc==SQLITE_OK ) rc = sqlite3_bind_int(p->pDelete, nCol+1, pbRetry==0); if( rc!=SQLITE_OK ) return rc; sqlite3_step(p->pDelete); rc = sqlite3_reset(p->pDelete); if( rc==SQLITE_OK && sqlite3_changes(p->db)==0 ){ rc = sessionConflictHandler( SQLITE_CHANGESET_DATA, p, pIter, xConflict, pCtx, pbRetry ); }else if( rc==SQLITE_CONSTRAINT ){ rc = sessionConflictHandler( SQLITE_CHANGESET_CONFLICT, p, pIter, xConflict, pCtx, 0 ); } }else if( op==SQLITE_UPDATE ){ int i; /* Bind values to the UPDATE statement. */ for(i=0; rc==SQLITE_OK && i<nCol; i++){ sqlite3_value *pOld = 0; sqlite3_value *pNew = 0; rc = sqlite3changeset_old(pIter, i, &pOld); if( rc==SQLITE_OK ){ rc = sqlite3changeset_new(pIter, i, &pNew); } if( rc==SQLITE_OK ){ if( pOld ) sqlite3_bind_value(p->pUpdate, i*3+1, pOld); sqlite3_bind_int(p->pUpdate, i*3+2, !!pNew); if( pNew ) sqlite3_bind_value(p->pUpdate, i*3+3, pNew); } } if( rc==SQLITE_OK ) rc = sqlite3_bind_int(p->pUpdate, nCol*3+1, pbRetry==0); if( rc!=SQLITE_OK ) return rc; /* Attempt the UPDATE. In the case of a NOTFOUND or DATA conflict, ** the result will be SQLITE_OK with 0 rows modified. */ sqlite3_step(p->pUpdate); rc = sqlite3_reset(p->pUpdate); if( rc==SQLITE_OK && sqlite3_changes(p->db)==0 ){ /* A NOTFOUND or DATA error. Search the table to see if it contains ** a row with a matching primary key. If so, this is a DATA conflict. ** Otherwise, if there is no primary key match, it is a NOTFOUND. */ rc = sessionConflictHandler( SQLITE_CHANGESET_DATA, p, pIter, xConflict, pCtx, pbRetry ); }else if( rc==SQLITE_CONSTRAINT ){ /* This may be a CONSTRAINT or CONFLICT error. It is a CONFLICT if ** the only problem is a duplicate PRIMARY KEY, or a CONSTRAINT ** otherwise. */ int bPKChange = 0; /* Check if the PK has been modified. */ rc = SQLITE_OK; for(i=0; i<nCol && rc==SQLITE_OK; i++){ if( p->abPK[i] ){ sqlite3_value *pNew; rc = sqlite3changeset_new(pIter, i, &pNew); if( rc==SQLITE_OK && pNew ){ bPKChange = 1; break; } } } rc = sessionConflictHandler(SQLITE_CHANGESET_CONFLICT, p, pIter, xConflict, pCtx, (bPKChange ? pbReplace : 0) ); } }else{ int i; assert( op==SQLITE_INSERT ); for(i=0; rc==SQLITE_OK && i<nCol; i++){ sqlite3_value *pVal; rc = sqlite3changeset_new(pIter, i, &pVal); if( rc==SQLITE_OK ){ rc = sqlite3_bind_value(p->pInsert, i+1, pVal); } } if( rc!=SQLITE_OK ) return rc; sqlite3_step(p->pInsert); rc = sqlite3_reset(p->pInsert); if( rc==SQLITE_CONSTRAINT && xConflict ){ rc = sessionConflictHandler( SQLITE_CHANGESET_CONFLICT, p, pIter, xConflict, pCtx, pbReplace ); } } return rc; } int sqlite3changeset_apply( sqlite3 *db, int nChangeset, void *pChangeset, int(*xConflict)( void *pCtx, /* Copy of fifth arg to _apply() */ int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */ sqlite3_changeset_iter *p /* Handle describing change and conflict */ ), void *pCtx ){ sqlite3_changeset_iter *pIter = 0; int rc; int rc2; const char *zTab = 0; /* Name of current table */ int nTab = 0; /* Result of strlen(zTab) */ SessionApplyCtx sApply; memset(&sApply, 0, sizeof(sApply)); sqlite3changeset_start(&pIter, nChangeset, pChangeset); rc = sqlite3_exec(db, "SAVEPOINT changeset_apply", 0, 0, 0); while( rc==SQLITE_OK && SQLITE_ROW==sqlite3changeset_next(pIter) ){ int nCol; int op; int bReplace = 0; int bRetry = 0; const char *zNew; sqlite3changeset_op(pIter, &zNew, &nCol, &op); if( zTab==0 || sqlite3_strnicmp(zNew, zTab, nTab+1) ){ sqlite3_free(sApply.azCol); sqlite3_finalize(sApply.pDelete); sqlite3_finalize(sApply.pUpdate); sqlite3_finalize(sApply.pInsert); sqlite3_finalize(sApply.pSelect); memset(&sApply, 0, sizeof(sApply)); sApply.db = db; sApply.nCol = nCol; rc = sessionTableInfo(db, zNew, nCol, &zTab, &sApply.azCol, &sApply.abPK); if( rc!=SQLITE_OK || (rc = sessionSelectRow(db, zTab, &sApply)) || (rc = sessionUpdateRow(db, zTab, &sApply)) || (rc = sessionDeleteRow(db, zTab, &sApply)) || (rc = sessionInsertRow(db, zTab, &sApply)) ){ break; } nTab = strlen(zTab); } rc = sessionApplyOneOp(pIter, &sApply, xConflict, pCtx, &bReplace, &bRetry); if( rc==SQLITE_OK && bRetry ){ rc = sessionApplyOneOp(pIter, &sApply, xConflict, pCtx, &bReplace, 0); } if( bReplace ){ rc = sqlite3_exec(db, "SAVEPOINT replace_op", 0, 0, 0); if( rc==SQLITE_OK ){ int i; for(i=0; i<sApply.nCol; i++){ if( sApply.abPK[i] ){ sqlite3_value *pVal; rc = sqlite3changeset_new(pIter, i, &pVal); if( rc==SQLITE_OK && pVal==0 ){ rc = sqlite3changeset_old(pIter, i, &pVal); } if( rc==SQLITE_OK ){ rc = sqlite3_bind_value(sApply.pDelete, i+1, pVal); } } } sqlite3_bind_int(sApply.pDelete, sApply.nCol+1, 1); } if( rc==SQLITE_OK ){ sqlite3_step(sApply.pDelete); rc = sqlite3_reset(sApply.pDelete); } if( rc==SQLITE_OK ){ rc = sessionApplyOneOp(pIter, &sApply, xConflict, pCtx, 0, 0); } if( rc==SQLITE_OK ){ rc = sqlite3_exec(db, "RELEASE replace_op", 0, 0, 0); } } } rc2 = sqlite3changeset_finalize(pIter); if( rc==SQLITE_OK ) rc = rc2; if( rc==SQLITE_OK ){ rc = sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0); }else{ sqlite3_exec(db, "ROLLBACK TO changeset_apply", 0, 0, 0); sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0); } sqlite3_finalize(sApply.pInsert); sqlite3_finalize(sApply.pDelete); sqlite3_finalize(sApply.pUpdate); sqlite3_finalize(sApply.pSelect); sqlite3_free(sApply.azCol); return rc; } #endif /* #ifdef SQLITE_ENABLE_SESSION */ |
Changes to ext/session/sqlite3session.h.
︙ | ︙ | |||
160 161 162 163 164 165 166 | void *pCtx, /* Copy of fifth arg to _apply() */ int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */ sqlite3_changeset_iter *p /* Handle describing change and conflict */ ), void *pCtx ); | > | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | > > > > > > > > > > > > > > > > > > > > > > > > > | 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 | void *pCtx, /* Copy of fifth arg to _apply() */ int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */ sqlite3_changeset_iter *p /* Handle describing change and conflict */ ), void *pCtx ); /* ** Values passed as the second argument to a conflict-handler. ** ** SQLITE_CHANGESET_DATA: ** The conflict handler is invoked with CHANGESET_DATA as the second argument ** when processing a DELETE or UPDATE change if a row with the required ** PRIMARY KEY fields is present in the database, but one or more other ** (non primary-key) fields modified by the update do not contain the ** expected "before" values. ** ** The conflicting row, in this case, is the database row with the matching ** primary key. ** ** SQLITE_CHANGESET_NOTFOUND: ** The conflict handler is invoked with CHANGESET_NOTFOUND as the second ** argument when processing a DELETE or UPDATE change if a row with the ** required PRIMARY KEY fields is not present in the database. ** ** There is no conflicting row in this case. The results of invoking the ** sqlite3changeset_conflict() API are undefined. ** ** SQLITE_CHANGESET_CONFLICT: ** CHANGESET_CONFLICT is passed as the second argument to the conflict ** handler while processing an UPDATE or an INSERT if the operation would ** result in duplicate primary key values. ** ** The conflicting row in this case is the database row with the matching ** primary key. ** ** SQLITE_CHANGESET_CONSTRAINT: ** If any other constraint violation occurs while applying a change (i.e. ** a FOREIGN KEY, UNIQUE, CHECK or NOT NULL constraint), the conflict ** handler is invoked with CHANGESET_CONSTRAINT as the second argument. ** ** There is no conflicting row in this case. The results of invoking the ** sqlite3changeset_conflict() API are undefined. */ #define SQLITE_CHANGESET_DATA 1 #define SQLITE_CHANGESET_NOTFOUND 2 #define SQLITE_CHANGESET_CONFLICT 3 #define SQLITE_CHANGESET_CONSTRAINT 4 /* ** Valid return values from a conflict-handler. ** ** SQLITE_CHANGESET_OMIT: ** If a conflict handler returns this value no special action is taken. The ** change is not applied. The session module continues to the next change ** in the changeset. ** ** SQLITE_CHANGESET_REPLACE: ** This value may only be returned if the second argument to the conflict ** handler was SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If this ** is not the case, any changes applied so far are rolled back and the ** call to sqlite3changeset_apply() returns SQLITE_MISUSE. ** ** If CHANGESET_REPLACE is returned by an SQLITE_CHANGESET_DATA conflict ** handler, then the conflicting row is either updated or deleted, depending ** on the type of change. ** ** If CHANGESET_REPLACE is returned by an SQLITE_CHANGESET_CONFLICT conflict ** handler, then the conflicting row is removed from the database and a ** second attempt to apply the change is made. If this second attempt fails, ** the original row is restored to the database before continuing. ** ** SQLITE_CHANGESET_ABORT: ** If this value is returned, any changes applied so far are rolled back ** and the call to sqlite3changeset_apply() returns SQLITE_MISUSE. */ #define SQLITE_CHANGESET_OMIT 0 #define SQLITE_CHANGESET_REPLACE 1 #define SQLITE_CHANGESET_ABORT 2 #endif |
Changes to ext/session/test_session.c.
︙ | ︙ | |||
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | } typedef struct TestConflictHandler TestConflictHandler; struct TestConflictHandler { Tcl_Interp *interp; Tcl_Obj *pScript; }; static int test_conflict_handler( void *pCtx, /* Pointer to TestConflictHandler structure */ int eConf, /* DATA, MISSING, CONFLICT, CONSTRAINT */ sqlite3_changeset_iter *pIter /* Handle describing change and conflict */ ){ TestConflictHandler *p = (TestConflictHandler *)pCtx; Tcl_Obj *pEval; Tcl_Interp *interp = p->interp; int op; /* SQLITE_UPDATE, DELETE or INSERT */ const char *zTab; /* Name of table conflict is on */ int nCol; /* Number of columns in table zTab */ pEval = Tcl_DuplicateObj(p->pScript); Tcl_IncrRefCount(pEval); | > > > > > > > > > > > > | 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 | } typedef struct TestConflictHandler TestConflictHandler; struct TestConflictHandler { Tcl_Interp *interp; Tcl_Obj *pScript; }; static int test_obj_eq_string(Tcl_Obj *p, const char *z){ int n; int nObj; char *zObj; n = strlen(z); zObj = Tcl_GetStringFromObj(p, &nObj); return (nObj==n && (n==0 || 0==memcmp(zObj, z, n))); } static int test_conflict_handler( void *pCtx, /* Pointer to TestConflictHandler structure */ int eConf, /* DATA, MISSING, CONFLICT, CONSTRAINT */ sqlite3_changeset_iter *pIter /* Handle describing change and conflict */ ){ TestConflictHandler *p = (TestConflictHandler *)pCtx; Tcl_Obj *pEval; Tcl_Interp *interp = p->interp; int ret = 0; /* Return value */ int op; /* SQLITE_UPDATE, DELETE or INSERT */ const char *zTab; /* Name of table conflict is on */ int nCol; /* Number of columns in table zTab */ pEval = Tcl_DuplicateObj(p->pScript); Tcl_IncrRefCount(pEval); |
︙ | ︙ | |||
253 254 255 256 257 258 259 | test_append_value(pConflict, pVal); } Tcl_ListObjAppendElement(0, pEval, pConflict); } if( TCL_OK!=Tcl_EvalObjEx(interp, pEval, TCL_EVAL_GLOBAL) ){ Tcl_BackgroundError(interp); | > > > > > > > > > > > > > > > > | > > | | 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 | test_append_value(pConflict, pVal); } Tcl_ListObjAppendElement(0, pEval, pConflict); } if( TCL_OK!=Tcl_EvalObjEx(interp, pEval, TCL_EVAL_GLOBAL) ){ Tcl_BackgroundError(interp); }else{ Tcl_Obj *pRes = Tcl_GetObjResult(interp); if( test_obj_eq_string(pRes, "OMIT") || test_obj_eq_string(pRes, "") ){ ret = SQLITE_CHANGESET_OMIT; }else if( test_obj_eq_string(pRes, "REPLACE") ){ ret = SQLITE_CHANGESET_REPLACE; }else if( test_obj_eq_string(pRes, "ABORT") ){ ret = SQLITE_CHANGESET_ABORT; }else{ Tcl_IncrRefCount(pRes); Tcl_ResetResult(interp); Tcl_AppendResult(interp, "unrecognized conflict handler return: \"", Tcl_GetString(pRes), "\"", 0 ); Tcl_DecrRefCount(pRes); Tcl_BackgroundError(interp); } } Tcl_DecrRefCount(pEval); return ret; } /* ** sqlite3changeset_apply DB CHANGESET SCRIPT */ static int test_sqlite3changeset_apply( void * clientData, |
︙ | ︙ |
Changes to test/session1.test.
︙ | ︙ | |||
144 145 146 147 148 149 150 | execsql { DELETE FROM t1 WHERE x = 100 } } {} do_changeset_test 2.4.2 S {} do_changeset_invert_test 2.4.3 S {} do_test 2.4.4 { S delete } {} #------------------------------------------------------------------------- | | > > > > | 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 | execsql { DELETE FROM t1 WHERE x = 100 } } {} do_changeset_test 2.4.2 S {} do_changeset_invert_test 2.4.3 S {} do_test 2.4.4 { S delete } {} #------------------------------------------------------------------------- # Test the application of simple changesets. These tests also test that # the conflict callback is invoked correctly. For these tests, the # conflict callback always returns OMIT. # db close forcedelete test.db test.db2 sqlite3 db test.db sqlite3 db2 test.db2 proc xConflict {args} { lappend ::xConflict $args return "" } proc bgerror {args} { set ::background_error $args } proc do_conflict_test {tn args} { set O(-tables) [list] set O(-sql) [list] set O(-conflicts) [list] array set V $args |
︙ | ︙ | |||
178 179 180 181 182 183 184 185 186 187 188 189 190 191 | set ::xConflict [list] sqlite3changeset_apply db2 [S changeset] xConflict set conflicts [list] foreach c $O(-conflicts) { lappend conflicts $c } uplevel do_test $tn [list { set ::xConflict }] [list $conflicts] S delete } proc do_db2_test {testname sql {result {}}} { uplevel do_test $testname [list "execsql {$sql} db2"] [list [list {*}$result]] | > > > | 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | set ::xConflict [list] sqlite3changeset_apply db2 [S changeset] xConflict set conflicts [list] foreach c $O(-conflicts) { lappend conflicts $c } after 1 {set go 1} vwait go uplevel do_test $tn [list { set ::xConflict }] [list $conflicts] S delete } proc do_db2_test {testname sql {result {}}} { uplevel do_test $testname [list "execsql {$sql} db2"] [list [list {*}$result]] |
︙ | ︙ | |||
275 276 277 278 279 280 281 | UPDATE t4 SET a = NULL WHERE c = 9; UPDATE t4 SET a = 'x' WHERE b = 11; } -conflicts { {UPDATE t4 DATA {i 1 i 2 i 3} {i -1 {} {} {} {}} {i 0 i 2 i 3}} {UPDATE t4 NOTFOUND {i 4 i 5 i 6} {i -1 {} {} {} {}}} {UPDATE t4 CONSTRAINT {i 7 i 8 i 9} {n {} {} {} {} {}}} } | < > > > | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 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 | UPDATE t4 SET a = NULL WHERE c = 9; UPDATE t4 SET a = 'x' WHERE b = 11; } -conflicts { {UPDATE t4 DATA {i 1 i 2 i 3} {i -1 {} {} {} {}} {i 0 i 2 i 3}} {UPDATE t4 NOTFOUND {i 4 i 5 i 6} {i -1 {} {} {} {}}} {UPDATE t4 CONSTRAINT {i 7 i 8 i 9} {n {} {} {} {} {}}} } do_db2_test 3.3.4 { SELECT * FROM t4 } {0 2 3 4 5 7 7 8 9 x 11 12} do_execsql_test 3.3.5 { SELECT * FROM t4 } {-1 2 3 -1 5 6 {} 8 9 x 11 12} #------------------------------------------------------------------------- # This next block of tests verifies that values returned by the conflict # handler are intepreted correctly. The following cases are tested: # # Test case Operation Conflict Return Code # UPDATE DATA OMIT # UPDATE DATA REPLACE # proc test_reset {} { db close db2 close forcedelete test.db test.db2 sqlite3 db test.db sqlite3 db2 test.db2 } proc xConflict {args} { lappend ::xConflict $args return $::conflict_return } foreach {tn conflict_return after} { 1 OMIT {1 2 value1 4 5 7 7 8 9 10 x x} 2 REPLACE {1 2 value1 4 5 value2 10 8 9} } { test_reset do_test 4.$tn.1 { foreach db {db db2} { execsql { CREATE TABLE t1(a, b, c, PRIMARY KEY(a)); INSERT INTO t1 VALUES(1, 2, 3); INSERT INTO t1 VALUES(4, 5, 6); INSERT INTO t1 VALUES(7, 8, 9); } $db } execsql { REPLACE INTO t1 VALUES(4, 5, 7); REPLACE INTO t1 VALUES(10, 'x', 'x'); } db2 } {} do_conflict_test 4.$tn.2 -tables t1 -sql { UPDATE t1 SET c = 'value1' WHERE a = 1; -- no conflict UPDATE t1 SET c = 'value2' WHERE a = 4; -- DATA conflict UPDATE t1 SET a = 10 WHERE a = 7; -- CONFLICT conflict } -conflicts { {UPDATE t1 DATA {i 4 {} {} i 6} {{} {} {} {} t value2} {i 4 i 5 i 7}} {UPDATE t1 CONFLICT {i 7 {} {} {} {}} {i 10 {} {} {} {}} {i 10 t x t x}} } do_db2_test 4.$tn.3 "SELECT * FROM t1 ORDER BY a" $after } foreach {tn conflict_return} { 1 OMIT 2 REPLACE } { test_reset do_test 5.$tn.1 { # Create an identical schema in both databases. set schema { CREATE TABLE "'foolish name'"(x, y, z, PRIMARY KEY(x, y)); } execsql $schema db execsql $schema db2 # Add some rows to [db2]. These rows will cause conflicts later # on when the changeset from [db] is applied to it. execsql { INSERT INTO "'foolish name'" VALUES('one', 'one', 'ii'); INSERT INTO "'foolish name'" VALUES('one', 'two', 'i'); INSERT INTO "'foolish name'" VALUES('two', 'two', 'ii'); } db2 } {} do_conflict_test 5.$tn.2 -tables {{'foolish name'}} -sql { INSERT INTO "'foolish name'" VALUES('one', 'two', 2); } -conflicts { {INSERT {'foolish name'} CONFLICT {t one t two i 2} {t one t two t i}} } set res(REPLACE) {one one ii one two 2 two two ii} set res(OMIT) {one one ii one two i two two ii} do_db2_test 5.$tn.3 { SELECT * FROM "'foolish name'" ORDER BY x, y } $res($conflict_return) do_test 5.$tn.1 { set schema { CREATE TABLE d1("z""z" PRIMARY KEY, y); INSERT INTO d1 VALUES(1, 'one'); INSERT INTO d1 VALUES(2, 'two'); } execsql $schema db execsql $schema db2 execsql { UPDATE d1 SET y = 'TWO' WHERE "z""z" = 2; } db2 } {} do_conflict_test 5.$tn.2 -tables d1 -sql { DELETE FROM d1 WHERE "z""z" = 2; } -conflicts { {DELETE d1 DATA {i 2 t two} {i 2 t TWO}} } set res(REPLACE) {1 one} set res(OMIT) {1 one 2 TWO} do_db2_test 5.$tn.3 "SELECT * FROM d1" $res($conflict_return) } catch { db2 close } finish_test |