/ Check-in [57862efe]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Fix some issues with UPDATE changes in the session module.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | sessions
Files: files | file ages | folders
SHA1:57862efe718fdc93401998f9058511292a0e1a50
User & Date: dan 2011-03-12 17:22:46
Context
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: cbbb274e user: dan tags: sessions
2011-03-12
17:22
Fix some issues with UPDATE changes in the session module. check-in: 57862efe user: dan tags: sessions
2011-03-11
19:05
Add the sqlite3changeset_apply() function. Does not yet handle all cases. check-in: 2b19be7b user: dan tags: sessions
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to ext/session/sqlite3session.c.

663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
...
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
...
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716

717
718
719
720



721
722


723

724
725
726
727
728
729
730
....
1447
1448
1449
1450
1451
1452
1453














































1454
1455
1456
1457
1458
1459
1460
....
1475
1476
1477
1478
1479
1480
1481


1482
1483
1484
1485
1486
1487
1488
....
1490
1491
1492
1493
1494
1495
1496







1497
1498
1499
1500
1501
1502
1503
....
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
....
1588
1589
1590
1591
1592
1593
1594
1595

























1596
1597
1598
1599
1600
1601
1602
....
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
  if( *pRc==SQLITE_OK ){
    SessionBuffer buf2 = {0, 0, 0};
    int bNoop = 1;
    int i;
    u8 *pCsr = p->aRecord;
    sessionAppendByte(pBuf, SQLITE_UPDATE, pRc);
    for(i=0; i<sqlite3_column_count(pStmt); i++){
      int nCopy = 0;
      int nAdvance;
      int eType = *pCsr;
      switch( eType ){
        case SQLITE_NULL:
          nAdvance = 1;
          if( sqlite3_column_type(pStmt, i)!=SQLITE_NULL ){
            nCopy = 1;
          }
          break;

        case SQLITE_FLOAT:
        case SQLITE_INTEGER: {
          nAdvance = 9;
          if( eType==sqlite3_column_type(pStmt, i) ){
................................................................................
              if( iVal==sqlite3_column_int64(pStmt, i) ) break;
            }else{
              double dVal;
              memcpy(&dVal, &iVal, 8);
              if( dVal==sqlite3_column_double(pStmt, i) ) break;
            }
          }
          nCopy = 9;
          break;
        }

        case SQLITE_TEXT:
        case SQLITE_BLOB: {
          int nByte;
          int nHdr = 1 + sessionVarintGet(&pCsr[1], &nByte);
................................................................................
          nAdvance = nHdr + nByte;
          if( eType==sqlite3_column_type(pStmt, i) 
           && nByte==sqlite3_column_bytes(pStmt, i) 
           && 0==memcmp(&pCsr[nHdr], sqlite3_column_blob(pStmt, i), nByte)
          ){
            break;
          }
          nCopy = nAdvance;
        }
      }
      if( abPK[i] ){
        nCopy = nAdvance;
      }

      if( nCopy==0 ){

        sessionAppendByte(pBuf, 0, pRc);
        sessionAppendByte(&buf2, 0, pRc);
      }else{
        sessionAppendBlob(pBuf, pCsr, nCopy, pRc);



        sessionAppendCol(&buf2, pStmt, i, pRc);
        bNoop = 0;


      }

      pCsr += nAdvance;
    }

    if( bNoop ){
      pBuf->nBuf -= (1 + sqlite3_column_count(pStmt));
    }else{
      sessionAppendBlob(pBuf, buf2.aBuf, buf2.nBuf, pRc);
................................................................................
    if( rc==SQLITE_OK ){
      rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, ppStmt, 0);
    }
    sqlite3_free(buf.aBuf);
  }
  return rc;
}















































int sqlite3changeset_apply(
  sqlite3 *db,
  int nChangeset,
  void *pChangeset,
  int(*xConflict)(
    void *pCtx,                   /* Copy of fifth arg to _apply() */
................................................................................

  sqlite3_stmt *pDelete = 0;      /* DELETE statement */
  sqlite3_stmt *pUpdate = 0;      /* DELETE statement */
  sqlite3_stmt *pInsert = 0;      /* INSERT statement */
  sqlite3_stmt *pSelect = 0;      /* SELECT statement */

  rc = sqlite3_exec(db, "SAVEPOINT changeset_apply", 0, 0, 0);


  sqlite3changeset_start(&pIter, nChangeset, pChangeset);
  while( SQLITE_ROW==sqlite3changeset_next(pIter) ){
    int op;
    const char *zThis;
    sqlite3changeset_op(pIter, &zThis, &nCol, &op);
    if( zTab==0 || sqlite3_strnicmp(zThis, zTab, nTab+1) ){
      sqlite3_free(azCol);
................................................................................
      nTab = strlen(zTab);

      sqlite3_finalize(pDelete);
      sqlite3_finalize(pUpdate);
      sqlite3_finalize(pInsert);
      sqlite3_finalize(pSelect);
      pSelect = pUpdate = pInsert = pDelete = 0;







    }

    if( op==SQLITE_DELETE ){
      int res;
      int i;
      rc = sessionDeleteRow(db, zTab, nCol, azCol, abPK, &pDelete);
      for(i=0; rc==SQLITE_OK && i<nCol; i++){
................................................................................
        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(pUpdate, i*3+1, pOld);
          sqlite3_bind_int(pUpdate, i*3+2, !!pNew);
          if( pNew ) sqlite3_bind_value(pUpdate, i*3+3, pOld);
        }
      }
      if( rc==SQLITE_OK ) rc = sqlite3_bind_int(pUpdate, nCol*3+1, 0);
      if( rc!=SQLITE_OK ) break;

      sqlite3_step(pUpdate);
      rc = sqlite3_reset(pUpdate);
................................................................................
        }else{
          rc = sqlite3_reset(pSelect);
          if( rc==SQLITE_OK ){
            res = xConflict(pCtx, SQLITE_CHANGESET_NOTFOUND, pIter);
          }
        }
      }else if( rc==SQLITE_CONSTRAINT ){
        assert(0);

























      }

    }else{
      int i;
      assert( op==SQLITE_INSERT );
      if( pInsert==0 ){
        SessionBuffer buf = {0, 0, 0};
................................................................................
        }
      }
      if( rc!=SQLITE_OK ) break;

      sqlite3_step(pInsert);
      rc = sqlite3_reset(pInsert);
      if( rc==SQLITE_CONSTRAINT && xConflict ){
        int res;

        /* Figure out if this is a primary key or other constraint. */
        rc = sessionSelectRow(db, zTab, nCol, azCol, abPK, &pSelect);
        for(i=0; rc==SQLITE_OK && i<nCol; i++){
          if( abPK[i] ){
            sqlite3_value *pVal;
            rc = sqlite3changeset_new(pIter, i, &pVal);
            if( rc==SQLITE_OK ) sqlite3_bind_value(pSelect, i+1, pVal);
          }
        }
        if( rc!=SQLITE_OK ) break;
        if( SQLITE_ROW==sqlite3_step(pSelect) ){
          pIter->pConflict = pSelect;
          res = xConflict(pCtx, SQLITE_CHANGESET_CONFLICT, pIter);
          pIter->pConflict = 0;
          sqlite3_reset(pSelect);
        }else{
          rc = sqlite3_reset(pSelect);
          if( rc==SQLITE_OK ){
            res = xConflict(pCtx, SQLITE_CHANGESET_CONSTRAINT, pIter);
          }
        }


      }
    }
  }
  rc2 = sqlite3changeset_finalize(pIter);
  if( rc==SQLITE_DONE ) rc = rc2;

  if( rc==SQLITE_OK ){







|






|







 







|







 







|


<
<
|
<
<
>
|
<

|
>
>
>


>
>

>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







>
>







 







>
>
>
>
>
>
>







 







|







 







|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
>
>







663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
...
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
...
702
703
704
705
706
707
708
709
710
711


712


713
714

715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
....
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
....
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
....
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
....
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
....
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
....
1702
1703
1704
1705
1706
1707
1708
1709






















1710
1711
1712
1713
1714
1715
1716
1717
1718
  if( *pRc==SQLITE_OK ){
    SessionBuffer buf2 = {0, 0, 0};
    int bNoop = 1;
    int i;
    u8 *pCsr = p->aRecord;
    sessionAppendByte(pBuf, SQLITE_UPDATE, pRc);
    for(i=0; i<sqlite3_column_count(pStmt); i++){
      int bChanged = 0;
      int nAdvance;
      int eType = *pCsr;
      switch( eType ){
        case SQLITE_NULL:
          nAdvance = 1;
          if( sqlite3_column_type(pStmt, i)!=SQLITE_NULL ){
            bChanged = 1;
          }
          break;

        case SQLITE_FLOAT:
        case SQLITE_INTEGER: {
          nAdvance = 9;
          if( eType==sqlite3_column_type(pStmt, i) ){
................................................................................
              if( iVal==sqlite3_column_int64(pStmt, i) ) break;
            }else{
              double dVal;
              memcpy(&dVal, &iVal, 8);
              if( dVal==sqlite3_column_double(pStmt, i) ) break;
            }
          }
          bChanged = 1;
          break;
        }

        case SQLITE_TEXT:
        case SQLITE_BLOB: {
          int nByte;
          int nHdr = 1 + sessionVarintGet(&pCsr[1], &nByte);
................................................................................
          nAdvance = nHdr + nByte;
          if( eType==sqlite3_column_type(pStmt, i) 
           && nByte==sqlite3_column_bytes(pStmt, i) 
           && 0==memcmp(&pCsr[nHdr], sqlite3_column_blob(pStmt, i), nByte)
          ){
            break;
          }
          bChanged = 1;
        }
      }





      if( bChanged || abPK[i] ){
        sessionAppendBlob(pBuf, pCsr, nAdvance, pRc);

      }else{
        sessionAppendByte(pBuf, 0, pRc);
      }

      if( bChanged ){
        sessionAppendCol(&buf2, pStmt, i, pRc);
        bNoop = 0;
      }else{
        sessionAppendByte(&buf2, 0, pRc);
      }

      pCsr += nAdvance;
    }

    if( bNoop ){
      pBuf->nBuf -= (1 + sqlite3_column_count(pStmt));
    }else{
      sessionAppendBlob(pBuf, buf2.aBuf, buf2.nBuf, pRc);
................................................................................
    if( rc==SQLITE_OK ){
      rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, ppStmt, 0);
    }
    sqlite3_free(buf.aBuf);
  }
  return rc;
}

static int sessionConstraintConflict(
  sqlite3 *db,                    /* Database handle */
  sqlite3_changeset_iter *pIter,  /* Changeset iterator */
  u8 *abPK,                       /* Primary key flags array */
  sqlite3_stmt *pSelect,          /* SELECT statement from sessionSelectRow() */
  int(*xConflict)(void *, int, sqlite3_changeset_iter*),
  void *pCtx
){
  int res;
  int rc;
  int i;
  int nCol;
  int op;
  const char *zDummy;

  sqlite3changeset_op(pIter, &zDummy, &nCol, &op);
  assert( op==SQLITE_UPDATE || op==SQLITE_INSERT );

  /* Bind the new.* PRIMARY KEY values to the SELECT statement. */
  for(i=0; i<nCol; i++){
    if( abPK[i] ){
      sqlite3_value *pVal;
      if( op==SQLITE_UPDATE ) rc = sqlite3changeset_old(pIter, i, &pVal);
      else                    rc = sqlite3changeset_new(pIter, i, &pVal);
      if( rc!=SQLITE_OK ) return rc;
      sqlite3_bind_value(pSelect, i+1, pVal);
    }
  }

  if( SQLITE_ROW==sqlite3_step(pSelect) ){
    /* There exists another row with the new.* primary key. */
    pIter->pConflict = pSelect;
    res = xConflict(pCtx, SQLITE_CHANGESET_CONFLICT, pIter);
    pIter->pConflict = 0;
    sqlite3_reset(pSelect);
  }else{
    /* No other row with the new.* primary key. */
    rc = sqlite3_reset(pSelect);
    if( rc==SQLITE_OK ){
      res = xConflict(pCtx, SQLITE_CHANGESET_CONSTRAINT, pIter);
    }
  }

  return rc;
}

int sqlite3changeset_apply(
  sqlite3 *db,
  int nChangeset,
  void *pChangeset,
  int(*xConflict)(
    void *pCtx,                   /* Copy of fifth arg to _apply() */
................................................................................

  sqlite3_stmt *pDelete = 0;      /* DELETE statement */
  sqlite3_stmt *pUpdate = 0;      /* DELETE statement */
  sqlite3_stmt *pInsert = 0;      /* INSERT statement */
  sqlite3_stmt *pSelect = 0;      /* SELECT statement */

  rc = sqlite3_exec(db, "SAVEPOINT changeset_apply", 0, 0, 0);
  if( rc!=SQLITE_OK ) return rc;

  sqlite3changeset_start(&pIter, nChangeset, pChangeset);
  while( SQLITE_ROW==sqlite3changeset_next(pIter) ){
    int op;
    const char *zThis;
    sqlite3changeset_op(pIter, &zThis, &nCol, &op);
    if( zTab==0 || sqlite3_strnicmp(zThis, zTab, nTab+1) ){
      sqlite3_free(azCol);
................................................................................
      nTab = strlen(zTab);

      sqlite3_finalize(pDelete);
      sqlite3_finalize(pUpdate);
      sqlite3_finalize(pInsert);
      sqlite3_finalize(pSelect);
      pSelect = pUpdate = pInsert = pDelete = 0;

      if( (rc = sessionSelectRow(db, zTab, nCol, azCol, abPK, &pSelect))
       || (rc = sessionUpdateRow(db, zTab, nCol, azCol, abPK, &pUpdate))
       || (rc = sessionDeleteRow(db, zTab, nCol, azCol, abPK, &pDelete))
      ){
        break;
      }
    }

    if( op==SQLITE_DELETE ){
      int res;
      int i;
      rc = sessionDeleteRow(db, zTab, nCol, azCol, abPK, &pDelete);
      for(i=0; rc==SQLITE_OK && i<nCol; i++){
................................................................................
        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(pUpdate, i*3+1, pOld);
          sqlite3_bind_int(pUpdate, i*3+2, !!pNew);
          if( pNew ) sqlite3_bind_value(pUpdate, i*3+3, pNew);
        }
      }
      if( rc==SQLITE_OK ) rc = sqlite3_bind_int(pUpdate, nCol*3+1, 0);
      if( rc!=SQLITE_OK ) break;

      sqlite3_step(pUpdate);
      rc = sqlite3_reset(pUpdate);
................................................................................
        }else{
          rc = sqlite3_reset(pSelect);
          if( rc==SQLITE_OK ){
            res = xConflict(pCtx, SQLITE_CHANGESET_NOTFOUND, pIter);
          }
        }
      }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( abPK[i] ){
            sqlite3_value *pNew;
            rc = sqlite3changeset_new(pIter, i, &pNew);
            if( rc==SQLITE_OK && pNew ){
              bPKChange = 1;
              break;
            }
          }
        }

        if( bPKChange ){
          /* See if there exists a row with a duplicate primary key. */
          rc = sessionConstraintConflict(
              db, pIter, abPK, pSelect, xConflict, pCtx
          );
        }else{
          res = xConflict(pCtx, SQLITE_CHANGESET_CONSTRAINT, pIter);
        }
      }

    }else{
      int i;
      assert( op==SQLITE_INSERT );
      if( pInsert==0 ){
        SessionBuffer buf = {0, 0, 0};
................................................................................
        }
      }
      if( rc!=SQLITE_OK ) break;

      sqlite3_step(pInsert);
      rc = sqlite3_reset(pInsert);
      if( rc==SQLITE_CONSTRAINT && xConflict ){
        rc = sessionConstraintConflict(






















              db, pIter, abPK, pSelect, xConflict, pCtx
        );
      }
    }
  }
  rc2 = sqlite3changeset_finalize(pIter);
  if( rc==SQLITE_DONE ) rc = rc2;

  if( rc==SQLITE_OK ){

Changes to test/session1.test.

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

# Test UPDATE changesets.
#
do_execsql_test 3.3.1 {
  CREATE TABLE t4(a, b, c, PRIMARY KEY(b, c));
  INSERT INTO t4 VALUES(1, 2, 3);
  INSERT INTO t4 VALUES(4, 5, 6);


}
do_db2_test 3.3.2 {
  CREATE TABLE t4(a, b, c, PRIMARY KEY(b, c));
  INSERT INTO t4 VALUES(0, 2, 3);
  INSERT INTO t4 VALUES(4, 5, 7);


}

do_conflict_test 3.2.3 -tables t4 -sql {
  UPDATE t4 SET a = -1 WHERE b = 2;



} -conflicts {
  {UPDATE t4 DATA {i 1 i 2 i 3} {i -1 i 2 i 3} {i 0 i 2 i 3}}


}

do_db2_test 3.3.4 {
  SELECT * FROM t4
} {0 2 3 4 5 7}



catch { db2 close }
finish_test








>
>


|


>
>

<
|

>
>
>

|
>
>


<
|
<
>





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

# Test UPDATE changesets.
#
do_execsql_test 3.3.1 {
  CREATE TABLE t4(a, b, c, PRIMARY KEY(b, c));
  INSERT INTO t4 VALUES(1, 2, 3);
  INSERT INTO t4 VALUES(4, 5, 6);
  INSERT INTO t4 VALUES(7, 8, 9);
  INSERT INTO t4 VALUES(10, 11, 12);
}
do_db2_test 3.3.2 {
  CREATE TABLE t4(a NOT NULL, b, c, PRIMARY KEY(b, c));
  INSERT INTO t4 VALUES(0, 2, 3);
  INSERT INTO t4 VALUES(4, 5, 7);
  INSERT INTO t4 VALUES(7, 8, 9);
  INSERT INTO t4 VALUES(10, 11, 12);
}

do_conflict_test 3.3.3 -tables t4 -sql {
  UPDATE t4 SET a = -1 WHERE b = 2;
  UPDATE t4 SET a = -1 WHERE b = 5;
  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}


catch { db2 close }
finish_test