14fccf43aSdan 
29b1c62d4Sdrh #if defined(SQLITE_ENABLE_SESSION) && defined(SQLITE_ENABLE_PREUPDATE_HOOK)
34fccf43aSdan #include "sqlite3session.h"
44fccf43aSdan #include <assert.h>
54fccf43aSdan #include <string.h>
64fccf43aSdan 
75d8a2984Sdan #ifndef SQLITE_AMALGAMATION
84fccf43aSdan # include "sqliteInt.h"
94fccf43aSdan # include "vdbeInt.h"
105d8a2984Sdan #endif
114fccf43aSdan 
124fccf43aSdan typedef struct SessionTable SessionTable;
134fccf43aSdan typedef struct SessionChange SessionChange;
14296c7658Sdan typedef struct SessionBuffer SessionBuffer;
15ef7a6304Sdan typedef struct SessionInput SessionInput;
16ef7a6304Sdan 
17ef7a6304Sdan /*
18ef7a6304Sdan ** Minimum chunk size used by streaming versions of functions.
19ef7a6304Sdan */
20f1a08ad8Sdrh #ifndef SESSIONS_STRM_CHUNK_SIZE
214757c658Sdan # ifdef SQLITE_TEST
22f1a08ad8Sdrh #   define SESSIONS_STRM_CHUNK_SIZE 64
234757c658Sdan # else
24f1a08ad8Sdrh #   define SESSIONS_STRM_CHUNK_SIZE 1024
25f1a08ad8Sdrh # endif
264757c658Sdan #endif
274fccf43aSdan 
28cf8e9144Sdan typedef struct SessionHook SessionHook;
29cf8e9144Sdan struct SessionHook {
30cf8e9144Sdan   void *pCtx;
31cf8e9144Sdan   int (*xOld)(void*,int,sqlite3_value**);
32cf8e9144Sdan   int (*xNew)(void*,int,sqlite3_value**);
33cf8e9144Sdan   int (*xCount)(void*);
34cf8e9144Sdan   int (*xDepth)(void*);
35cf8e9144Sdan };
36cf8e9144Sdan 
37296c7658Sdan /*
38296c7658Sdan ** Session handle structure.
39296c7658Sdan */
404fccf43aSdan struct sqlite3_session {
414fccf43aSdan   sqlite3 *db;                    /* Database handle session is attached to */
424fccf43aSdan   char *zDb;                      /* Name of database session is attached to */
43296c7658Sdan   int bEnable;                    /* True if currently recording */
44b4480e94Sdan   int bIndirect;                  /* True if all changes are indirect */
45ff4d0f41Sdan   int bAutoAttach;                /* True to auto-attach tables */
464fccf43aSdan   int rc;                         /* Non-zero if an error has occurred */
477531a5a3Sdan   void *pFilterCtx;               /* First argument to pass to xTableFilter */
487531a5a3Sdan   int (*xTableFilter)(void *pCtx, const char *zTab);
494fccf43aSdan   sqlite3_session *pNext;         /* Next session object on same db. */
504fccf43aSdan   SessionTable *pTable;           /* List of attached tables */
51cf8e9144Sdan   SessionHook hook;               /* APIs to grab new and old data with */
524fccf43aSdan };
534fccf43aSdan 
544fccf43aSdan /*
55ef7a6304Sdan ** Instances of this structure are used to build strings or binary records.
56ef7a6304Sdan */
57ef7a6304Sdan struct SessionBuffer {
58ef7a6304Sdan   u8 *aBuf;                       /* Pointer to changeset buffer */
59ef7a6304Sdan   int nBuf;                       /* Size of buffer aBuf */
60ef7a6304Sdan   int nAlloc;                     /* Size of allocation containing aBuf */
61ef7a6304Sdan };
62ef7a6304Sdan 
63ef7a6304Sdan /*
6416228167Sdan ** An object of this type is used internally as an abstraction for
6516228167Sdan ** input data. Input data may be supplied either as a single large buffer
6616228167Sdan ** (e.g. sqlite3changeset_start()) or using a stream function (e.g.
67f1a08ad8Sdrh **  sqlite3changeset_start_strm()).
68ef7a6304Sdan */
69ef7a6304Sdan struct SessionInput {
70d9151526Sdan   int bNoDiscard;                 /* If true, discard no data */
71d9151526Sdan   int iCurrent;                   /* Offset in aData[] of current change */
724757c658Sdan   int iNext;                      /* Offset in aData[] of next change */
734757c658Sdan   u8 *aData;                      /* Pointer to buffer containing changeset */
744757c658Sdan   int nData;                      /* Number of bytes in aData */
754757c658Sdan 
76ef7a6304Sdan   SessionBuffer buf;              /* Current read buffer */
77ef7a6304Sdan   int (*xInput)(void*, void*, int*);        /* Input stream call (or NULL) */
78ef7a6304Sdan   void *pIn;                                /* First argument to xInput */
79ef7a6304Sdan   int bEof;                       /* Set to true after xInput finished */
80ef7a6304Sdan };
81ef7a6304Sdan 
82ef7a6304Sdan /*
83296c7658Sdan ** Structure for changeset iterators.
84296c7658Sdan */
85296c7658Sdan struct sqlite3_changeset_iter {
86ef7a6304Sdan   SessionInput in;                /* Input buffer or stream */
87ef7a6304Sdan   SessionBuffer tblhdr;           /* Buffer to hold apValue/zTab/abPK/ */
8873b3c055Sdan   int bPatchset;                  /* True if this is a patchset */
89296c7658Sdan   int rc;                         /* Iterator error code */
90296c7658Sdan   sqlite3_stmt *pConflict;        /* Points to conflicting row, if any */
91296c7658Sdan   char *zTab;                     /* Current table */
92296c7658Sdan   int nCol;                       /* Number of columns in zTab */
93296c7658Sdan   int op;                         /* Current operation */
94b4480e94Sdan   int bIndirect;                  /* True if current change was indirect */
95244593c8Sdan   u8 *abPK;                       /* Primary key array */
96296c7658Sdan   sqlite3_value **apValue;        /* old.* and new.* values */
97296c7658Sdan };
98296c7658Sdan 
99296c7658Sdan /*
1004fccf43aSdan ** Each session object maintains a set of the following structures, one
1014fccf43aSdan ** for each table the session object is monitoring. The structures are
1024fccf43aSdan ** stored in a linked list starting at sqlite3_session.pTable.
1034fccf43aSdan **
1044fccf43aSdan ** The keys of the SessionTable.aChange[] hash table are all rows that have
1054fccf43aSdan ** been modified in any way since the session object was attached to the
1064fccf43aSdan ** table.
1074fccf43aSdan **
1084fccf43aSdan ** The data associated with each hash-table entry is a structure containing
1094fccf43aSdan ** a subset of the initial values that the modified row contained at the
1104fccf43aSdan ** start of the session. Or no initial values if the row was inserted.
1114fccf43aSdan */
1124fccf43aSdan struct SessionTable {
1134fccf43aSdan   SessionTable *pNext;
1144fccf43aSdan   char *zName;                    /* Local name of table */
1154fccf43aSdan   int nCol;                       /* Number of columns in table zName */
116e8d5648eSdan   const char **azCol;             /* Column names */
117e8d5648eSdan   u8 *abPK;                       /* Array of primary key flags */
118296c7658Sdan   int nEntry;                     /* Total number of entries in hash table */
1194fccf43aSdan   int nChange;                    /* Size of apChange[] array */
1204fccf43aSdan   SessionChange **apChange;       /* Hash table buckets */
1214fccf43aSdan };
1224fccf43aSdan 
1234fccf43aSdan /*
1244fccf43aSdan ** RECORD FORMAT:
1254fccf43aSdan **
1264fccf43aSdan ** The following record format is similar to (but not compatible with) that
1274fccf43aSdan ** used in SQLite database files. This format is used as part of the
1284fccf43aSdan ** change-set binary format, and so must be architecture independent.
1294fccf43aSdan **
1304fccf43aSdan ** Unlike the SQLite database record format, each field is self-contained -
1314fccf43aSdan ** there is no separation of header and data. Each field begins with a
1324fccf43aSdan ** single byte describing its type, as follows:
1334fccf43aSdan **
1344fccf43aSdan **       0x00: Undefined value.
1354fccf43aSdan **       0x01: Integer value.
1364fccf43aSdan **       0x02: Real value.
1374fccf43aSdan **       0x03: Text value.
1384fccf43aSdan **       0x04: Blob value.
1394fccf43aSdan **       0x05: SQL NULL value.
1404fccf43aSdan **
1414fccf43aSdan ** Note that the above match the definitions of SQLITE_INTEGER, SQLITE_TEXT
1424fccf43aSdan ** and so on in sqlite3.h. For undefined and NULL values, the field consists
1434fccf43aSdan ** only of the single type byte. For other types of values, the type byte
1444fccf43aSdan ** is followed by:
1454fccf43aSdan **
1464fccf43aSdan **   Text values:
1474fccf43aSdan **     A varint containing the number of bytes in the value (encoded using
1484fccf43aSdan **     UTF-8). Followed by a buffer containing the UTF-8 representation
1494fccf43aSdan **     of the text value. There is no nul terminator.
1504fccf43aSdan **
1514fccf43aSdan **   Blob values:
1524fccf43aSdan **     A varint containing the number of bytes in the value, followed by
1534fccf43aSdan **     a buffer containing the value itself.
1544fccf43aSdan **
1554fccf43aSdan **   Integer values:
1564fccf43aSdan **     An 8-byte big-endian integer value.
1574fccf43aSdan **
1584fccf43aSdan **   Real values:
1594fccf43aSdan **     An 8-byte big-endian IEEE 754-2008 real value.
1604fccf43aSdan **
1614fccf43aSdan ** Varint values are encoded in the same way as varints in the SQLite
1624fccf43aSdan ** record format.
1634fccf43aSdan **
1644fccf43aSdan ** CHANGESET FORMAT:
1654fccf43aSdan **
1664fccf43aSdan ** A changeset is a collection of DELETE, UPDATE and INSERT operations on
1674fccf43aSdan ** one or more tables. Operations on a single table are grouped together,
1684fccf43aSdan ** but may occur in any order (i.e. deletes, updates and inserts are all
1694fccf43aSdan ** mixed together).
1704fccf43aSdan **
1714fccf43aSdan ** Each group of changes begins with a table header:
1724fccf43aSdan **
1734fccf43aSdan **   1 byte: Constant 0x54 (capital 'T')
174730bb805Sdan **   Varint: Number of columns in the table.
17573b3c055Sdan **   nCol bytes: 0x01 for PK columns, 0x00 otherwise.
1764fccf43aSdan **   N bytes: Unqualified table name (encoded using UTF-8). Nul-terminated.
1774fccf43aSdan **
1784fccf43aSdan ** Followed by one or more changes to the table.
1794fccf43aSdan **
180*c8be6437Sdrh **   1 byte: Either SQLITE_INSERT (0x12), UPDATE (0x17) or DELETE (0x09).
1815d607a6eSdan **   1 byte: The "indirect-change" flag.
1824fccf43aSdan **   old.* record: (delete and update only)
1834fccf43aSdan **   new.* record: (insert and update only)
18473b3c055Sdan **
185730bb805Sdan ** The "old.*" and "new.*" records, if present, are N field records in the
186730bb805Sdan ** format described above under "RECORD FORMAT", where N is the number of
187730bb805Sdan ** columns in the table. The i'th field of each record is associated with
188730bb805Sdan ** the i'th column of the table, counting from left to right in the order
189730bb805Sdan ** in which columns were declared in the CREATE TABLE statement.
190730bb805Sdan **
191730bb805Sdan ** The new.* record that is part of each INSERT change contains the values
192730bb805Sdan ** that make up the new row. Similarly, the old.* record that is part of each
193730bb805Sdan ** DELETE change contains the values that made up the row that was deleted
194730bb805Sdan ** from the database. In the changeset format, the records that are part
195730bb805Sdan ** of INSERT or DELETE changes never contain any undefined (type byte 0x00)
196730bb805Sdan ** fields.
197730bb805Sdan **
198730bb805Sdan ** Within the old.* record associated with an UPDATE change, all fields
199730bb805Sdan ** associated with table columns that are not PRIMARY KEY columns and are
200730bb805Sdan ** not modified by the UPDATE change are set to "undefined". Other fields
201730bb805Sdan ** are set to the values that made up the row before the UPDATE that the
202730bb805Sdan ** change records took place. Within the new.* record, fields associated
203730bb805Sdan ** with table columns modified by the UPDATE change contain the new
204730bb805Sdan ** values. Fields associated with table columns that are not modified
205730bb805Sdan ** are set to "undefined".
206730bb805Sdan **
20773b3c055Sdan ** PATCHSET FORMAT:
20873b3c055Sdan **
20973b3c055Sdan ** A patchset is also a collection of changes. It is similar to a changeset,
210730bb805Sdan ** but leaves undefined those fields that are not useful if no conflict
211730bb805Sdan ** resolution is required when applying the changeset.
21273b3c055Sdan **
21373b3c055Sdan ** Each group of changes begins with a table header:
21473b3c055Sdan **
21573b3c055Sdan **   1 byte: Constant 0x50 (capital 'P')
216730bb805Sdan **   Varint: Number of columns in the table.
21773b3c055Sdan **   nCol bytes: 0x01 for PK columns, 0x00 otherwise.
21873b3c055Sdan **   N bytes: Unqualified table name (encoded using UTF-8). Nul-terminated.
21973b3c055Sdan **
22073b3c055Sdan ** Followed by one or more changes to the table.
22173b3c055Sdan **
222*c8be6437Sdrh **   1 byte: Either SQLITE_INSERT (0x12), UPDATE (0x17) or DELETE (0x09).
22373b3c055Sdan **   1 byte: The "indirect-change" flag.
224730bb805Sdan **   single record: (PK fields for DELETE, PK and modified fields for UPDATE,
225730bb805Sdan **                   full record for INSERT).
226730bb805Sdan **
227730bb805Sdan ** As in the changeset format, each field of the single record that is part
228730bb805Sdan ** of a patchset change is associated with the correspondingly positioned
229730bb805Sdan ** table column, counting from left to right within the CREATE TABLE
230730bb805Sdan ** statement.
231730bb805Sdan **
232730bb805Sdan ** For a DELETE change, all fields within the record except those associated
233730bb805Sdan ** with PRIMARY KEY columns are set to "undefined". The PRIMARY KEY fields
234730bb805Sdan ** contain the values identifying the row to delete.
235730bb805Sdan **
236730bb805Sdan ** For an UPDATE change, all fields except those associated with PRIMARY KEY
237730bb805Sdan ** columns and columns that are modified by the UPDATE are set to "undefined".
238730bb805Sdan ** PRIMARY KEY fields contain the values identifying the table row to update,
239730bb805Sdan ** and fields associated with modified columns contain the new column values.
240730bb805Sdan **
241730bb805Sdan ** The records associated with INSERT changes are in the same format as for
242730bb805Sdan ** changesets. It is not possible for a record associated with an INSERT
243730bb805Sdan ** change to contain a field set to "undefined".
2444fccf43aSdan */
2454fccf43aSdan 
2464fccf43aSdan /*
2474fccf43aSdan ** For each row modified during a session, there exists a single instance of
2484fccf43aSdan ** this structure stored in a SessionTable.aChange[] hash table.
2494fccf43aSdan */
2504fccf43aSdan struct SessionChange {
251798693b2Sdan   int op;                         /* One of UPDATE, DELETE, INSERT */
252b4480e94Sdan   int bIndirect;                  /* True if this change is "indirect" */
2534fccf43aSdan   int nRecord;                    /* Number of bytes in buffer aRecord[] */
2544fccf43aSdan   u8 *aRecord;                    /* Buffer containing old.* record */
2554fccf43aSdan   SessionChange *pNext;           /* For hash-table collisions */
2564fccf43aSdan };
2574fccf43aSdan 
258296c7658Sdan /*
259296c7658Sdan ** Write a varint with value iVal into the buffer at aBuf. Return the
260296c7658Sdan ** number of bytes written.
261296c7658Sdan */
262296c7658Sdan static int sessionVarintPut(u8 *aBuf, int iVal){
263296c7658Sdan   return putVarint32(aBuf, iVal);
2644fccf43aSdan }
2654fccf43aSdan 
266296c7658Sdan /*
267296c7658Sdan ** Return the number of bytes required to store value iVal as a varint.
268296c7658Sdan */
269296c7658Sdan static int sessionVarintLen(int iVal){
270296c7658Sdan   return sqlite3VarintLen(iVal);
271296c7658Sdan }
272296c7658Sdan 
273296c7658Sdan /*
274296c7658Sdan ** Read a varint value from aBuf[] into *piVal. Return the number of
275296c7658Sdan ** bytes read.
276296c7658Sdan */
2774fccf43aSdan static int sessionVarintGet(u8 *aBuf, int *piVal){
278296c7658Sdan   return getVarint32(aBuf, *piVal);
2794fccf43aSdan }
2804fccf43aSdan 
281296c7658Sdan /*
282296c7658Sdan ** Read a 64-bit big-endian integer value from buffer aRec[]. Return
283296c7658Sdan ** the value read.
284296c7658Sdan */
2854fccf43aSdan static sqlite3_int64 sessionGetI64(u8 *aRec){
2864fccf43aSdan   return (((sqlite3_int64)aRec[0]) << 56)
2874fccf43aSdan        + (((sqlite3_int64)aRec[1]) << 48)
2884fccf43aSdan        + (((sqlite3_int64)aRec[2]) << 40)
2894fccf43aSdan        + (((sqlite3_int64)aRec[3]) << 32)
2904fccf43aSdan        + (((sqlite3_int64)aRec[4]) << 24)
2914fccf43aSdan        + (((sqlite3_int64)aRec[5]) << 16)
2924fccf43aSdan        + (((sqlite3_int64)aRec[6]) <<  8)
2934fccf43aSdan        + (((sqlite3_int64)aRec[7]) <<  0);
2944fccf43aSdan }
2954fccf43aSdan 
2964fccf43aSdan /*
297296c7658Sdan ** Write a 64-bit big-endian integer value to the buffer aBuf[].
298296c7658Sdan */
299296c7658Sdan static void sessionPutI64(u8 *aBuf, sqlite3_int64 i){
300296c7658Sdan   aBuf[0] = (i>>56) & 0xFF;
301296c7658Sdan   aBuf[1] = (i>>48) & 0xFF;
302296c7658Sdan   aBuf[2] = (i>>40) & 0xFF;
303296c7658Sdan   aBuf[3] = (i>>32) & 0xFF;
304296c7658Sdan   aBuf[4] = (i>>24) & 0xFF;
305296c7658Sdan   aBuf[5] = (i>>16) & 0xFF;
306296c7658Sdan   aBuf[6] = (i>> 8) & 0xFF;
307296c7658Sdan   aBuf[7] = (i>> 0) & 0xFF;
308296c7658Sdan }
309296c7658Sdan 
310296c7658Sdan /*
3114fccf43aSdan ** This function is used to serialize the contents of value pValue (see
3124fccf43aSdan ** comment titled "RECORD FORMAT" above).
3134fccf43aSdan **
3144fccf43aSdan ** If it is non-NULL, the serialized form of the value is written to
3154fccf43aSdan ** buffer aBuf. *pnWrite is set to the number of bytes written before
3164fccf43aSdan ** returning. Or, if aBuf is NULL, the only thing this function does is
3174fccf43aSdan ** set *pnWrite.
3184fccf43aSdan **
3194fccf43aSdan ** If no error occurs, SQLITE_OK is returned. Or, if an OOM error occurs
3204fccf43aSdan ** within a call to sqlite3_value_text() (may fail if the db is utf-16))
3214fccf43aSdan ** SQLITE_NOMEM is returned.
3224fccf43aSdan */
3234fccf43aSdan static int sessionSerializeValue(
3244fccf43aSdan   u8 *aBuf,                       /* If non-NULL, write serialized value here */
3254fccf43aSdan   sqlite3_value *pValue,          /* Value to serialize */
3264fccf43aSdan   int *pnWrite                    /* IN/OUT: Increment by bytes written */
3274fccf43aSdan ){
328296c7658Sdan   int nByte;                      /* Size of serialized value in bytes */
3294fccf43aSdan 
33080fe2d93Sdan   if( pValue ){
33180fe2d93Sdan     int eType;                    /* Value type (SQLITE_NULL, TEXT etc.) */
33280fe2d93Sdan 
3334fccf43aSdan     eType = sqlite3_value_type(pValue);
3344fccf43aSdan     if( aBuf ) aBuf[0] = eType;
3354fccf43aSdan 
3364fccf43aSdan     switch( eType ){
3374fccf43aSdan       case SQLITE_NULL:
3384fccf43aSdan         nByte = 1;
3394fccf43aSdan         break;
3404fccf43aSdan 
3414fccf43aSdan       case SQLITE_INTEGER:
3424fccf43aSdan       case SQLITE_FLOAT:
3434fccf43aSdan         if( aBuf ){
3444fccf43aSdan           /* TODO: SQLite does something special to deal with mixed-endian
3454fccf43aSdan           ** floating point values (e.g. ARM7). This code probably should
3464fccf43aSdan           ** too.  */
3474fccf43aSdan           u64 i;
3484fccf43aSdan           if( eType==SQLITE_INTEGER ){
3494fccf43aSdan             i = (u64)sqlite3_value_int64(pValue);
3504fccf43aSdan           }else{
3514fccf43aSdan             double r;
3524fccf43aSdan             assert( sizeof(double)==8 && sizeof(u64)==8 );
3534fccf43aSdan             r = sqlite3_value_double(pValue);
3544fccf43aSdan             memcpy(&i, &r, 8);
3554fccf43aSdan           }
356296c7658Sdan           sessionPutI64(&aBuf[1], i);
3574fccf43aSdan         }
3584fccf43aSdan         nByte = 9;
3594fccf43aSdan         break;
3604fccf43aSdan 
3614e895da1Sdan       default: {
36280fe2d93Sdan         u8 *z;
36380fe2d93Sdan         int n;
36480fe2d93Sdan         int nVarint;
36580fe2d93Sdan 
3664e895da1Sdan         assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
36780fe2d93Sdan         if( eType==SQLITE_TEXT ){
36880fe2d93Sdan           z = (u8 *)sqlite3_value_text(pValue);
36980fe2d93Sdan         }else{
37080fe2d93Sdan           z = (u8 *)sqlite3_value_blob(pValue);
37180fe2d93Sdan         }
37280fe2d93Sdan         n = sqlite3_value_bytes(pValue);
3733cc89d95Sdan         if( z==0 && (eType!=SQLITE_BLOB || n>0) ) return SQLITE_NOMEM;
37480fe2d93Sdan         nVarint = sessionVarintLen(n);
37580fe2d93Sdan 
3764fccf43aSdan         if( aBuf ){
3774fccf43aSdan           sessionVarintPut(&aBuf[1], n);
3784fccf43aSdan           memcpy(&aBuf[nVarint + 1], eType==SQLITE_TEXT ?
3794fccf43aSdan               sqlite3_value_text(pValue) : sqlite3_value_blob(pValue), n
3804fccf43aSdan           );
3814fccf43aSdan         }
3824fccf43aSdan 
3834fccf43aSdan         nByte = 1 + nVarint + n;
3844fccf43aSdan         break;
3854fccf43aSdan       }
3864fccf43aSdan     }
38780fe2d93Sdan   }else{
38880fe2d93Sdan     nByte = 1;
38980fe2d93Sdan     if( aBuf ) aBuf[0] = '\0';
39080fe2d93Sdan   }
3914fccf43aSdan 
392fa122adaSdan   if( pnWrite ) *pnWrite += nByte;
3934fccf43aSdan   return SQLITE_OK;
3944fccf43aSdan }
3954fccf43aSdan 
396fa122adaSdan 
397798693b2Sdan /*
398798693b2Sdan ** This macro is used to calculate hash key values for data structures. In
399798693b2Sdan ** order to use this macro, the entire data structure must be represented
400798693b2Sdan ** as a series of unsigned integers. In order to calculate a hash-key value
401798693b2Sdan ** for a data structure represented as three such integers, the macro may
402798693b2Sdan ** then be used as follows:
403798693b2Sdan **
404798693b2Sdan **    int hash_key_value;
405798693b2Sdan **    hash_key_value = HASH_APPEND(0, <value 1>);
406798693b2Sdan **    hash_key_value = HASH_APPEND(hash_key_value, <value 2>);
407798693b2Sdan **    hash_key_value = HASH_APPEND(hash_key_value, <value 3>);
408798693b2Sdan **
409798693b2Sdan ** In practice, the data structures this macro is used for are the primary
410798693b2Sdan ** key values of modified rows.
411798693b2Sdan */
4124131639cSdan #define HASH_APPEND(hash, add) ((hash) << 3) ^ (hash) ^ (unsigned int)(add)
413798693b2Sdan 
414798693b2Sdan /*
415798693b2Sdan ** Append the hash of the 64-bit integer passed as the second argument to the
416798693b2Sdan ** hash-key value passed as the first. Return the new hash-key value.
417798693b2Sdan */
4184131639cSdan static unsigned int sessionHashAppendI64(unsigned int h, i64 i){
419e8d5648eSdan   h = HASH_APPEND(h, i & 0xFFFFFFFF);
420e8d5648eSdan   return HASH_APPEND(h, (i>>32)&0xFFFFFFFF);
421e8d5648eSdan }
422798693b2Sdan 
423798693b2Sdan /*
424798693b2Sdan ** Append the hash of the blob passed via the second and third arguments to
425798693b2Sdan ** the hash-key value passed as the first. Return the new hash-key value.
426798693b2Sdan */
4274131639cSdan static unsigned int sessionHashAppendBlob(unsigned int h, int n, const u8 *z){
428e8d5648eSdan   int i;
429e8d5648eSdan   for(i=0; i<n; i++) h = HASH_APPEND(h, z[i]);
430e8d5648eSdan   return h;
431e8d5648eSdan }
432e8d5648eSdan 
4334fccf43aSdan /*
434798693b2Sdan ** Append the hash of the data type passed as the second argument to the
435798693b2Sdan ** hash-key value passed as the first. Return the new hash-key value.
436798693b2Sdan */
437798693b2Sdan static unsigned int sessionHashAppendType(unsigned int h, int eType){
438798693b2Sdan   return HASH_APPEND(h, eType);
439798693b2Sdan }
440798693b2Sdan 
441798693b2Sdan /*
4424131639cSdan ** This function may only be called from within a pre-update callback.
4434131639cSdan ** It calculates a hash based on the primary key values of the old.* or
444798693b2Sdan ** new.* row currently available and, assuming no error occurs, writes it to
445798693b2Sdan ** *piHash before returning. If the primary key contains one or more NULL
446798693b2Sdan ** values, *pbNullPK is set to true before returning.
447798693b2Sdan **
448798693b2Sdan ** If an error occurs, an SQLite error code is returned and the final values
449798693b2Sdan ** of *piHash asn *pbNullPK are undefined. Otherwise, SQLITE_OK is returned
450798693b2Sdan ** and the output variables are set as described above.
4514fccf43aSdan */
452798693b2Sdan static int sessionPreupdateHash(
453cf8e9144Sdan   sqlite3_session *pSession,      /* Session object that owns pTab */
454e8d5648eSdan   SessionTable *pTab,             /* Session table handle */
455e8d5648eSdan   int bNew,                       /* True to hash the new.* PK */
45627453faeSdan   int *piHash,                    /* OUT: Hash value */
457798693b2Sdan   int *pbNullPK                   /* OUT: True if there are NULL values in PK */
458e8d5648eSdan ){
4594131639cSdan   unsigned int h = 0;             /* Hash value to return */
4604131639cSdan   int i;                          /* Used to iterate through columns */
461e8d5648eSdan 
46227453faeSdan   assert( *pbNullPK==0 );
463cf8e9144Sdan   assert( pTab->nCol==pSession->hook.xCount(pSession->hook.pCtx) );
464e8d5648eSdan   for(i=0; i<pTab->nCol; i++){
465e8d5648eSdan     if( pTab->abPK[i] ){
466e8d5648eSdan       int rc;
467e8d5648eSdan       int eType;
468e8d5648eSdan       sqlite3_value *pVal;
469e8d5648eSdan 
470e8d5648eSdan       if( bNew ){
471cf8e9144Sdan         rc = pSession->hook.xNew(pSession->hook.pCtx, i, &pVal);
472e8d5648eSdan       }else{
473cf8e9144Sdan         rc = pSession->hook.xOld(pSession->hook.pCtx, i, &pVal);
474e8d5648eSdan       }
47512ca0b56Sdan       if( rc!=SQLITE_OK ) return rc;
476e8d5648eSdan 
477e8d5648eSdan       eType = sqlite3_value_type(pVal);
478798693b2Sdan       h = sessionHashAppendType(h, eType);
4796734007dSdan       if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
480e8d5648eSdan         i64 iVal;
481e8d5648eSdan         if( eType==SQLITE_INTEGER ){
482e8d5648eSdan           iVal = sqlite3_value_int64(pVal);
483e8d5648eSdan         }else{
484e8d5648eSdan           double rVal = sqlite3_value_double(pVal);
485e8d5648eSdan           assert( sizeof(iVal)==8 && sizeof(rVal)==8 );
486e8d5648eSdan           memcpy(&iVal, &rVal, 8);
487e8d5648eSdan         }
488e8d5648eSdan         h = sessionHashAppendI64(h, iVal);
4896734007dSdan       }else if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
4906734007dSdan         const u8 *z;
4913cc89d95Sdan         int n;
4926734007dSdan         if( eType==SQLITE_TEXT ){
4936734007dSdan           z = (const u8 *)sqlite3_value_text(pVal);
4946734007dSdan         }else{
4956734007dSdan           z = (const u8 *)sqlite3_value_blob(pVal);
496e8d5648eSdan         }
4973cc89d95Sdan         n = sqlite3_value_bytes(pVal);
4983cc89d95Sdan         if( !z && (eType!=SQLITE_BLOB || n>0) ) return SQLITE_NOMEM;
4993cc89d95Sdan         h = sessionHashAppendBlob(h, n, z);
5006734007dSdan       }else{
50127453faeSdan         assert( eType==SQLITE_NULL );
50227453faeSdan         *pbNullPK = 1;
503e8d5648eSdan       }
504e8d5648eSdan     }
505e8d5648eSdan   }
506e8d5648eSdan 
507e8d5648eSdan   *piHash = (h % pTab->nChange);
508e8d5648eSdan   return SQLITE_OK;
509e8d5648eSdan }
510e8d5648eSdan 
5114131639cSdan /*
5126cda207fSdan ** The buffer that the argument points to contains a serialized SQL value.
5136cda207fSdan ** Return the number of bytes of space occupied by the value (including
5146cda207fSdan ** the type byte).
5156cda207fSdan */
5166cda207fSdan static int sessionSerialLen(u8 *a){
5176cda207fSdan   int e = *a;
5186cda207fSdan   int n;
5196cda207fSdan   if( e==0 ) return 1;
5206cda207fSdan   if( e==SQLITE_NULL ) return 1;
5216cda207fSdan   if( e==SQLITE_INTEGER || e==SQLITE_FLOAT ) return 9;
5226cda207fSdan   return sessionVarintGet(&a[1], &n) + 1 + n;
5236cda207fSdan }
5246cda207fSdan 
5256cda207fSdan /*
5265d607a6eSdan ** Based on the primary key values stored in change aRecord, calculate a
527798693b2Sdan ** hash key. Assume the has table has nBucket buckets. The hash keys
5284131639cSdan ** calculated by this function are compatible with those calculated by
5294131639cSdan ** sessionPreupdateHash().
53064277f4aSdan **
53164277f4aSdan ** The bPkOnly argument is non-zero if the record at aRecord[] is from
53264277f4aSdan ** a patchset DELETE. In this case the non-PK fields are omitted entirely.
5334131639cSdan */
5344131639cSdan static unsigned int sessionChangeHash(
5354131639cSdan   SessionTable *pTab,             /* Table handle */
53664277f4aSdan   int bPkOnly,                    /* Record consists of PK fields only */
5375d607a6eSdan   u8 *aRecord,                    /* Change record */
5384131639cSdan   int nBucket                     /* Assume this many buckets in hash table */
539e8d5648eSdan ){
5404131639cSdan   unsigned int h = 0;             /* Value to return */
5414131639cSdan   int i;                          /* Used to iterate through columns */
5425d607a6eSdan   u8 *a = aRecord;                /* Used to iterate through change record */
543e8d5648eSdan 
544e8d5648eSdan   for(i=0; i<pTab->nCol; i++){
5456cda207fSdan     int eType = *a;
546e8d5648eSdan     int isPK = pTab->abPK[i];
54764277f4aSdan     if( bPkOnly && isPK==0 ) continue;
548e8d5648eSdan 
54927453faeSdan     /* It is not possible for eType to be SQLITE_NULL here. The session
55027453faeSdan     ** module does not record changes for rows with NULL values stored in
55127453faeSdan     ** primary key columns. */
55227453faeSdan     assert( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT
55327453faeSdan          || eType==SQLITE_TEXT || eType==SQLITE_BLOB
5546cda207fSdan          || eType==SQLITE_NULL || eType==0
55527453faeSdan     );
5566cda207fSdan     assert( !isPK || (eType!=0 && eType!=SQLITE_NULL) );
55727453faeSdan 
5586cda207fSdan     if( isPK ){
5596cda207fSdan       a++;
560798693b2Sdan       h = sessionHashAppendType(h, eType);
56127453faeSdan       if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
5626734007dSdan         h = sessionHashAppendI64(h, sessionGetI64(a));
563e8d5648eSdan         a += 8;
56427453faeSdan       }else{
565e8d5648eSdan         int n;
566e8d5648eSdan         a += sessionVarintGet(a, &n);
5676734007dSdan         h = sessionHashAppendBlob(h, n, a);
568e8d5648eSdan         a += n;
569e8d5648eSdan       }
5706cda207fSdan     }else{
5716cda207fSdan       a += sessionSerialLen(a);
5726cda207fSdan     }
573e8d5648eSdan   }
574e8d5648eSdan   return (h % nBucket);
575e8d5648eSdan }
576e8d5648eSdan 
577798693b2Sdan /*
578798693b2Sdan ** Arguments aLeft and aRight are pointers to change records for table pTab.
579798693b2Sdan ** This function returns true if the two records apply to the same row (i.e.
580798693b2Sdan ** have the same values stored in the primary key columns), or false
581798693b2Sdan ** otherwise.
582798693b2Sdan */
5835d607a6eSdan static int sessionChangeEqual(
584798693b2Sdan   SessionTable *pTab,             /* Table used for PK definition */
585a71d2371Sdan   int bLeftPkOnly,                /* True if aLeft[] contains PK fields only */
5865d607a6eSdan   u8 *aLeft,                      /* Change record */
587a71d2371Sdan   int bRightPkOnly,               /* True if aRight[] contains PK fields only */
5885d607a6eSdan   u8 *aRight                      /* Change record */
5895d607a6eSdan ){
590798693b2Sdan   u8 *a1 = aLeft;                 /* Cursor to iterate through aLeft */
591798693b2Sdan   u8 *a2 = aRight;                /* Cursor to iterate through aRight */
592798693b2Sdan   int iCol;                       /* Used to iterate through table columns */
5935d607a6eSdan 
594798693b2Sdan   for(iCol=0; iCol<pTab->nCol; iCol++){
5955d607a6eSdan     int n1 = sessionSerialLen(a1);
5965d607a6eSdan     int n2 = sessionSerialLen(a2);
5975d607a6eSdan 
598798693b2Sdan     if( pTab->abPK[iCol] && (n1!=n2 || memcmp(a1, a2, n1)) ){
5995d607a6eSdan       return 0;
6005d607a6eSdan     }
60164277f4aSdan     if( pTab->abPK[iCol] || bLeftPkOnly==0 ) a1 += n1;
60264277f4aSdan     if( pTab->abPK[iCol] || bRightPkOnly==0 ) a2 += n2;
6035d607a6eSdan   }
6045d607a6eSdan 
6055d607a6eSdan   return 1;
6065d607a6eSdan }
6075d607a6eSdan 
608798693b2Sdan /*
609798693b2Sdan ** Arguments aLeft and aRight both point to buffers containing change
610798693b2Sdan ** records with nCol columns. This function "merges" the two records into
611798693b2Sdan ** a single records which is written to the buffer at *paOut. *paOut is
612798693b2Sdan ** then set to point to one byte after the last byte written before
613798693b2Sdan ** returning.
614798693b2Sdan **
615798693b2Sdan ** The merging of records is done as follows: For each column, if the
616798693b2Sdan ** aRight record contains a value for the column, copy the value from
617798693b2Sdan ** their. Otherwise, if aLeft contains a value, copy it. If neither
618798693b2Sdan ** record contains a value for a given column, then neither does the
619798693b2Sdan ** output record.
620798693b2Sdan */
6215d607a6eSdan static void sessionMergeRecord(
6225d607a6eSdan   u8 **paOut,
623798693b2Sdan   int nCol,
6245d607a6eSdan   u8 *aLeft,
6255d607a6eSdan   u8 *aRight
6265d607a6eSdan ){
627798693b2Sdan   u8 *a1 = aLeft;                 /* Cursor used to iterate through aLeft */
628798693b2Sdan   u8 *a2 = aRight;                /* Cursor used to iterate through aRight */
629798693b2Sdan   u8 *aOut = *paOut;              /* Output cursor */
630798693b2Sdan   int iCol;                       /* Used to iterate from 0 to nCol */
6315d607a6eSdan 
632798693b2Sdan   for(iCol=0; iCol<nCol; iCol++){
6335d607a6eSdan     int n1 = sessionSerialLen(a1);
6345d607a6eSdan     int n2 = sessionSerialLen(a2);
6355d607a6eSdan     if( *a2 ){
6365d607a6eSdan       memcpy(aOut, a2, n2);
6375d607a6eSdan       aOut += n2;
6385d607a6eSdan     }else{
6395d607a6eSdan       memcpy(aOut, a1, n1);
6405d607a6eSdan       aOut += n1;
6415d607a6eSdan     }
6425d607a6eSdan     a1 += n1;
6435d607a6eSdan     a2 += n2;
6445d607a6eSdan   }
6455d607a6eSdan 
6465d607a6eSdan   *paOut = aOut;
6475d607a6eSdan }
6485d607a6eSdan 
649798693b2Sdan /*
650798693b2Sdan ** This is a helper function used by sessionMergeUpdate().
651798693b2Sdan **
652798693b2Sdan ** When this function is called, both *paOne and *paTwo point to a value
653798693b2Sdan ** within a change record. Before it returns, both have been advanced so
654798693b2Sdan ** as to point to the next value in the record.
655798693b2Sdan **
656798693b2Sdan ** If, when this function is called, *paTwo points to a valid value (i.e.
657fa29ecc4Sdan ** *paTwo[0] is not 0x00 - the "no value" placeholder), a copy of the *paTwo
658798693b2Sdan ** pointer is returned and *pnVal is set to the number of bytes in the
659798693b2Sdan ** serialized value. Otherwise, a copy of *paOne is returned and *pnVal
660798693b2Sdan ** set to the number of bytes in the value at *paOne. If *paOne points
661fa29ecc4Sdan ** to the "no value" placeholder, *pnVal is set to 1. In other words:
662fa29ecc4Sdan **
663fa29ecc4Sdan **   if( *paTwo is valid ) return *paTwo;
664fa29ecc4Sdan **   return *paOne;
665fa29ecc4Sdan **
666798693b2Sdan */
6675d607a6eSdan static u8 *sessionMergeValue(
668798693b2Sdan   u8 **paOne,                     /* IN/OUT: Left-hand buffer pointer */
669798693b2Sdan   u8 **paTwo,                     /* IN/OUT: Right-hand buffer pointer */
670798693b2Sdan   int *pnVal                      /* OUT: Bytes in returned value */
6715d607a6eSdan ){
6725d607a6eSdan   u8 *a1 = *paOne;
6735d607a6eSdan   u8 *a2 = *paTwo;
6745d607a6eSdan   u8 *pRet = 0;
6755d607a6eSdan   int n1;
6765d607a6eSdan 
6775d607a6eSdan   assert( a1 );
6785d607a6eSdan   if( a2 ){
6795d607a6eSdan     int n2 = sessionSerialLen(a2);
6805d607a6eSdan     if( *a2 ){
6815d607a6eSdan       *pnVal = n2;
6825d607a6eSdan       pRet = a2;
6835d607a6eSdan     }
6845d607a6eSdan     *paTwo = &a2[n2];
6855d607a6eSdan   }
6865d607a6eSdan 
6875d607a6eSdan   n1 = sessionSerialLen(a1);
6885d607a6eSdan   if( pRet==0 ){
6895d607a6eSdan     *pnVal = n1;
6905d607a6eSdan     pRet = a1;
6915d607a6eSdan   }
6925d607a6eSdan   *paOne = &a1[n1];
6935d607a6eSdan 
6945d607a6eSdan   return pRet;
6955d607a6eSdan }
6965d607a6eSdan 
697798693b2Sdan /*
698798693b2Sdan ** This function is used by changeset_concat() to merge two UPDATE changes
699798693b2Sdan ** on the same row.
700798693b2Sdan */
7015d607a6eSdan static int sessionMergeUpdate(
702798693b2Sdan   u8 **paOut,                     /* IN/OUT: Pointer to output buffer */
703798693b2Sdan   SessionTable *pTab,             /* Table change pertains to */
704a71d2371Sdan   int bPatchset,                  /* True if records are patchset records */
705798693b2Sdan   u8 *aOldRecord1,                /* old.* record for first change */
706798693b2Sdan   u8 *aOldRecord2,                /* old.* record for second change */
707798693b2Sdan   u8 *aNewRecord1,                /* new.* record for first change */
708798693b2Sdan   u8 *aNewRecord2                 /* new.* record for second change */
7095d607a6eSdan ){
7105d607a6eSdan   u8 *aOld1 = aOldRecord1;
7115d607a6eSdan   u8 *aOld2 = aOldRecord2;
7125d607a6eSdan   u8 *aNew1 = aNewRecord1;
7135d607a6eSdan   u8 *aNew2 = aNewRecord2;
7145d607a6eSdan 
7155d607a6eSdan   u8 *aOut = *paOut;
7165d607a6eSdan   int i;
71764277f4aSdan 
71864277f4aSdan   if( bPatchset==0 ){
7195d607a6eSdan     int bRequired = 0;
7205d607a6eSdan 
7215d607a6eSdan     assert( aOldRecord1 && aNewRecord1 );
7225d607a6eSdan 
7235d607a6eSdan     /* Write the old.* vector first. */
7245d607a6eSdan     for(i=0; i<pTab->nCol; i++){
7255d607a6eSdan       int nOld;
7265d607a6eSdan       u8 *aOld;
7275d607a6eSdan       int nNew;
7285d607a6eSdan       u8 *aNew;
7295d607a6eSdan 
7305d607a6eSdan       aOld = sessionMergeValue(&aOld1, &aOld2, &nOld);
7315d607a6eSdan       aNew = sessionMergeValue(&aNew1, &aNew2, &nNew);
7325d607a6eSdan       if( pTab->abPK[i] || nOld!=nNew || memcmp(aOld, aNew, nNew) ){
7335d607a6eSdan         if( pTab->abPK[i]==0 ) bRequired = 1;
7345d607a6eSdan         memcpy(aOut, aOld, nOld);
7355d607a6eSdan         aOut += nOld;
7365d607a6eSdan       }else{
7375d607a6eSdan         *(aOut++) = '\0';
7385d607a6eSdan       }
7395d607a6eSdan     }
7405d607a6eSdan 
7415d607a6eSdan     if( !bRequired ) return 0;
74264277f4aSdan   }
7435d607a6eSdan 
7445d607a6eSdan   /* Write the new.* vector */
7455d607a6eSdan   aOld1 = aOldRecord1;
7465d607a6eSdan   aOld2 = aOldRecord2;
7475d607a6eSdan   aNew1 = aNewRecord1;
7485d607a6eSdan   aNew2 = aNewRecord2;
7495d607a6eSdan   for(i=0; i<pTab->nCol; i++){
7505d607a6eSdan     int nOld;
7515d607a6eSdan     u8 *aOld;
7525d607a6eSdan     int nNew;
7535d607a6eSdan     u8 *aNew;
7545d607a6eSdan 
7555d607a6eSdan     aOld = sessionMergeValue(&aOld1, &aOld2, &nOld);
7565d607a6eSdan     aNew = sessionMergeValue(&aNew1, &aNew2, &nNew);
75764277f4aSdan     if( bPatchset==0
75864277f4aSdan      && (pTab->abPK[i] || (nOld==nNew && 0==memcmp(aOld, aNew, nNew)))
75964277f4aSdan     ){
7605d607a6eSdan       *(aOut++) = '\0';
7615d607a6eSdan     }else{
7625d607a6eSdan       memcpy(aOut, aNew, nNew);
7635d607a6eSdan       aOut += nNew;
7645d607a6eSdan     }
7655d607a6eSdan   }
7665d607a6eSdan 
7675d607a6eSdan   *paOut = aOut;
7685d607a6eSdan   return 1;
7695d607a6eSdan }
7705d607a6eSdan 
77177fc1d5bSdan /*
77277fc1d5bSdan ** This function is only called from within a pre-update-hook callback.
77377fc1d5bSdan ** It determines if the current pre-update-hook change affects the same row
77477fc1d5bSdan ** as the change stored in argument pChange. If so, it returns true. Otherwise
77577fc1d5bSdan ** if the pre-update-hook does not affect the same row as pChange, it returns
77677fc1d5bSdan ** false.
77777fc1d5bSdan */
77877fc1d5bSdan static int sessionPreupdateEqual(
779cf8e9144Sdan   sqlite3_session *pSession,      /* Session object that owns SessionTable */
78077fc1d5bSdan   SessionTable *pTab,             /* Table associated with change */
78177fc1d5bSdan   SessionChange *pChange,         /* Change to compare to */
78277fc1d5bSdan   int op                          /* Current pre-update operation */
783e8d5648eSdan ){
78477fc1d5bSdan   int iCol;                       /* Used to iterate through columns */
78577fc1d5bSdan   u8 *a = pChange->aRecord;       /* Cursor used to scan change record */
786e8d5648eSdan 
78777fc1d5bSdan   assert( op==SQLITE_INSERT || op==SQLITE_UPDATE || op==SQLITE_DELETE );
78877fc1d5bSdan   for(iCol=0; iCol<pTab->nCol; iCol++){
78977fc1d5bSdan     if( !pTab->abPK[iCol] ){
790798693b2Sdan       a += sessionSerialLen(a);
791e8d5648eSdan     }else{
7926734007dSdan       sqlite3_value *pVal;        /* Value returned by preupdate_new/old */
7936734007dSdan       int rc;                     /* Error code from preupdate_new/old */
794798693b2Sdan       int eType = *a++;           /* Type of value from change record */
7956734007dSdan 
7966734007dSdan       /* The following calls to preupdate_new() and preupdate_old() can not
7976734007dSdan       ** fail. This is because they cache their return values, and by the
7986734007dSdan       ** time control flows to here they have already been called once from
7996734007dSdan       ** within sessionPreupdateHash(). The first two asserts below verify
8006734007dSdan       ** this (that the method has already been called). */
80177fc1d5bSdan       if( op==SQLITE_INSERT ){
802cf8e9144Sdan         /* assert( db->pPreUpdate->pNewUnpacked || db->pPreUpdate->aNew ); */
803cf8e9144Sdan         rc = pSession->hook.xNew(pSession->hook.pCtx, iCol, &pVal);
804e8d5648eSdan       }else{
805cf8e9144Sdan         /* assert( db->pPreUpdate->pUnpacked ); */
806cf8e9144Sdan         rc = pSession->hook.xOld(pSession->hook.pCtx, iCol, &pVal);
807e8d5648eSdan       }
8086734007dSdan       assert( rc==SQLITE_OK );
80977fc1d5bSdan       if( sqlite3_value_type(pVal)!=eType ) return 0;
810e8d5648eSdan 
81112ca0b56Sdan       /* A SessionChange object never has a NULL value in a PK column */
81212ca0b56Sdan       assert( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT
81312ca0b56Sdan            || eType==SQLITE_BLOB    || eType==SQLITE_TEXT
81412ca0b56Sdan       );
81512ca0b56Sdan 
81612ca0b56Sdan       if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
817e8d5648eSdan         i64 iVal = sessionGetI64(a);
818e8d5648eSdan         a += 8;
819e8d5648eSdan         if( eType==SQLITE_INTEGER ){
82077fc1d5bSdan           if( sqlite3_value_int64(pVal)!=iVal ) return 0;
821e8d5648eSdan         }else{
822e8d5648eSdan           double rVal;
823e8d5648eSdan           assert( sizeof(iVal)==8 && sizeof(rVal)==8 );
824e8d5648eSdan           memcpy(&rVal, &iVal, 8);
82577fc1d5bSdan           if( sqlite3_value_double(pVal)!=rVal ) return 0;
826e8d5648eSdan         }
82712ca0b56Sdan       }else{
828e8d5648eSdan         int n;
829e8d5648eSdan         const u8 *z;
830e8d5648eSdan         a += sessionVarintGet(a, &n);
83177fc1d5bSdan         if( sqlite3_value_bytes(pVal)!=n ) return 0;
83212ca0b56Sdan         if( eType==SQLITE_TEXT ){
83312ca0b56Sdan           z = sqlite3_value_text(pVal);
83412ca0b56Sdan         }else{
83512ca0b56Sdan           z = sqlite3_value_blob(pVal);
83612ca0b56Sdan         }
83777fc1d5bSdan         if( memcmp(a, z, n) ) return 0;
838e8d5648eSdan         a += n;
839e8d5648eSdan         break;
840e8d5648eSdan       }
841e8d5648eSdan     }
842e8d5648eSdan   }
843e8d5648eSdan 
84477fc1d5bSdan   return 1;
8454fccf43aSdan }
8464fccf43aSdan 
8474fccf43aSdan /*
8484fccf43aSdan ** If required, grow the hash table used to store changes on table pTab
8494fccf43aSdan ** (part of the session pSession). If a fatal OOM error occurs, set the
8504fccf43aSdan ** session object to failed and return SQLITE_ERROR. Otherwise, return
8514fccf43aSdan ** SQLITE_OK.
8524fccf43aSdan **
8534fccf43aSdan ** It is possible that a non-fatal OOM error occurs in this function. In
8544fccf43aSdan ** that case the hash-table does not grow, but SQLITE_OK is returned anyway.
8554fccf43aSdan ** Growing the hash table in this case is a performance optimization only,
8564fccf43aSdan ** it is not required for correct operation.
8574fccf43aSdan */
85864277f4aSdan static int sessionGrowHash(int bPatchset, SessionTable *pTab){
8594fccf43aSdan   if( pTab->nChange==0 || pTab->nEntry>=(pTab->nChange/2) ){
8604fccf43aSdan     int i;
8614fccf43aSdan     SessionChange **apNew;
8624fccf43aSdan     int nNew = (pTab->nChange ? pTab->nChange : 128) * 2;
8634fccf43aSdan 
8644fccf43aSdan     apNew = (SessionChange **)sqlite3_malloc(sizeof(SessionChange *) * nNew);
8654fccf43aSdan     if( apNew==0 ){
8664fccf43aSdan       if( pTab->nChange==0 ){
8674fccf43aSdan         return SQLITE_ERROR;
8684fccf43aSdan       }
8694fccf43aSdan       return SQLITE_OK;
8704fccf43aSdan     }
8714fccf43aSdan     memset(apNew, 0, sizeof(SessionChange *) * nNew);
8724fccf43aSdan 
8734fccf43aSdan     for(i=0; i<pTab->nChange; i++){
8744fccf43aSdan       SessionChange *p;
8754fccf43aSdan       SessionChange *pNext;
8764fccf43aSdan       for(p=pTab->apChange[i]; p; p=pNext){
87764277f4aSdan         int bPkOnly = (p->op==SQLITE_DELETE && bPatchset);
87864277f4aSdan         int iHash = sessionChangeHash(pTab, bPkOnly, p->aRecord, nNew);
8794fccf43aSdan         pNext = p->pNext;
8804fccf43aSdan         p->pNext = apNew[iHash];
8814fccf43aSdan         apNew[iHash] = p;
8824fccf43aSdan       }
8834fccf43aSdan     }
8844fccf43aSdan 
8854fccf43aSdan     sqlite3_free(pTab->apChange);
8864fccf43aSdan     pTab->nChange = nNew;
8874fccf43aSdan     pTab->apChange = apNew;
8884fccf43aSdan   }
8894fccf43aSdan 
8904fccf43aSdan   return SQLITE_OK;
8914fccf43aSdan }
8924fccf43aSdan 
893296c7658Sdan /*
894e8d5648eSdan ** This function queries the database for the names of the columns of table
895e8d5648eSdan ** zThis, in schema zDb. It is expected that the table has nCol columns. If
896e8d5648eSdan ** not, SQLITE_SCHEMA is returned and none of the output variables are
897e8d5648eSdan ** populated.
898e8d5648eSdan **
89977fc1d5bSdan ** Otherwise, if they are not NULL, variable *pnCol is set to the number
90077fc1d5bSdan ** of columns in the database table and variable *pzTab is set to point to a
901e8d5648eSdan ** nul-terminated copy of the table name. *pazCol (if not NULL) is set to
902e8d5648eSdan ** point to an array of pointers to column names. And *pabPK (again, if not
903e8d5648eSdan ** NULL) is set to point to an array of booleans - true if the corresponding
904e8d5648eSdan ** column is part of the primary key.
905e8d5648eSdan **
906e8d5648eSdan ** For example, if the table is declared as:
907e8d5648eSdan **
908e8d5648eSdan **     CREATE TABLE tbl1(w, x, y, z, PRIMARY KEY(w, z));
909e8d5648eSdan **
91077fc1d5bSdan ** Then the four output variables are populated as follows:
911e8d5648eSdan **
91277fc1d5bSdan **     *pnCol  = 4
913e8d5648eSdan **     *pzTab  = "tbl1"
914e8d5648eSdan **     *pazCol = {"w", "x", "y", "z"}
915e8d5648eSdan **     *pabPK  = {1, 0, 0, 1}
916e8d5648eSdan **
917e8d5648eSdan ** All returned buffers are part of the same single allocation, which must
918e8d5648eSdan ** be freed using sqlite3_free() by the caller. If pazCol was not NULL, then
919e8d5648eSdan ** pointer *pazCol should be freed to release all memory. Otherwise, pointer
920e8d5648eSdan ** *pabPK. It is illegal for both pazCol and pabPK to be NULL.
921e8d5648eSdan */
922e8d5648eSdan static int sessionTableInfo(
923e8d5648eSdan   sqlite3 *db,                    /* Database connection */
924e8d5648eSdan   const char *zDb,                /* Name of attached database (e.g. "main") */
925e8d5648eSdan   const char *zThis,              /* Table name */
926ca62ad57Sdan   int *pnCol,                     /* OUT: number of columns */
927e8d5648eSdan   const char **pzTab,             /* OUT: Copy of zThis */
928e8d5648eSdan   const char ***pazCol,           /* OUT: Array of column names for table */
929e8d5648eSdan   u8 **pabPK                      /* OUT: Array of booleans - true for PK col */
930e8d5648eSdan ){
931e8d5648eSdan   char *zPragma;
932e8d5648eSdan   sqlite3_stmt *pStmt;
933e8d5648eSdan   int rc;
934e8d5648eSdan   int nByte;
935e8d5648eSdan   int nDbCol = 0;
936e8d5648eSdan   int nThis;
937e8d5648eSdan   int i;
938e8d5648eSdan   u8 *pAlloc;
939db04571cSdan   char **azCol = 0;
940e8d5648eSdan   u8 *abPK;
941e8d5648eSdan 
942db04571cSdan   assert( pazCol && pabPK );
943e8d5648eSdan 
944cfdbde21Sdrh   nThis = sqlite3Strlen30(zThis);
945e8d5648eSdan   zPragma = sqlite3_mprintf("PRAGMA '%q'.table_info('%q')", zDb, zThis);
946e8d5648eSdan   if( !zPragma ) return SQLITE_NOMEM;
947e8d5648eSdan 
948e8d5648eSdan   rc = sqlite3_prepare_v2(db, zPragma, -1, &pStmt, 0);
949e8d5648eSdan   sqlite3_free(zPragma);
950e8d5648eSdan   if( rc!=SQLITE_OK ) return rc;
951e8d5648eSdan 
952e8d5648eSdan   nByte = nThis + 1;
953e8d5648eSdan   while( SQLITE_ROW==sqlite3_step(pStmt) ){
954e8d5648eSdan     nByte += sqlite3_column_bytes(pStmt, 1);
955e8d5648eSdan     nDbCol++;
956e8d5648eSdan   }
957e8d5648eSdan   rc = sqlite3_reset(pStmt);
958e8d5648eSdan 
959e8d5648eSdan   if( rc==SQLITE_OK ){
960e8d5648eSdan     nByte += nDbCol * (sizeof(const char *) + sizeof(u8) + 1);
961e8d5648eSdan     pAlloc = sqlite3_malloc(nByte);
962e8d5648eSdan     if( pAlloc==0 ){
963e8d5648eSdan       rc = SQLITE_NOMEM;
964e8d5648eSdan     }
965e8d5648eSdan   }
966e8d5648eSdan   if( rc==SQLITE_OK ){
967e8d5648eSdan     azCol = (char **)pAlloc;
968ca62ad57Sdan     pAlloc = (u8 *)&azCol[nDbCol];
969e8d5648eSdan     abPK = (u8 *)pAlloc;
970ca62ad57Sdan     pAlloc = &abPK[nDbCol];
971e8d5648eSdan     if( pzTab ){
972e8d5648eSdan       memcpy(pAlloc, zThis, nThis+1);
973e8d5648eSdan       *pzTab = (char *)pAlloc;
974e8d5648eSdan       pAlloc += nThis+1;
975e8d5648eSdan     }
976e8d5648eSdan 
977e8d5648eSdan     i = 0;
978e8d5648eSdan     while( SQLITE_ROW==sqlite3_step(pStmt) ){
979e8d5648eSdan       int nName = sqlite3_column_bytes(pStmt, 1);
980e8d5648eSdan       const unsigned char *zName = sqlite3_column_text(pStmt, 1);
981e8d5648eSdan       if( zName==0 ) break;
982e8d5648eSdan       memcpy(pAlloc, zName, nName+1);
983e8d5648eSdan       azCol[i] = (char *)pAlloc;
984e8d5648eSdan       pAlloc += nName+1;
985db04571cSdan       abPK[i] = sqlite3_column_int(pStmt, 5);
986e8d5648eSdan       i++;
987e8d5648eSdan     }
988e8d5648eSdan     rc = sqlite3_reset(pStmt);
989e8d5648eSdan 
990e8d5648eSdan   }
991e8d5648eSdan 
992e8d5648eSdan   /* If successful, populate the output variables. Otherwise, zero them and
993e8d5648eSdan   ** free any allocation made. An error code will be returned in this case.
994e8d5648eSdan   */
995e8d5648eSdan   if( rc==SQLITE_OK ){
996db04571cSdan     *pazCol = (const char **)azCol;
997db04571cSdan     *pabPK = abPK;
998ca62ad57Sdan     *pnCol = nDbCol;
999e8d5648eSdan   }else{
1000db04571cSdan     *pazCol = 0;
1001db04571cSdan     *pabPK = 0;
1002ca62ad57Sdan     *pnCol = 0;
1003e8d5648eSdan     if( pzTab ) *pzTab = 0;
1004db04571cSdan     sqlite3_free(azCol);
1005e8d5648eSdan   }
1006e8d5648eSdan   sqlite3_finalize(pStmt);
1007e8d5648eSdan   return rc;
1008e8d5648eSdan }
1009e8d5648eSdan 
1010e8d5648eSdan /*
1011296c7658Sdan ** This function is only called from within a pre-update handler for a
1012296c7658Sdan ** write to table pTab, part of session pSession. If this is the first
10136dc29e60Sdan ** write to this table, initalize the SessionTable.nCol, azCol[] and
10146dc29e60Sdan ** abPK[] arrays accordingly.
1015296c7658Sdan **
10166dc29e60Sdan ** If an error occurs, an error code is stored in sqlite3_session.rc and
10176dc29e60Sdan ** non-zero returned. Or, if no error occurs but the table has no primary
10186dc29e60Sdan ** key, sqlite3_session.rc is left set to SQLITE_OK and non-zero returned to
10196dc29e60Sdan ** indicate that updates on this table should be ignored. SessionTable.abPK
10206dc29e60Sdan ** is set to NULL in this case.
1021296c7658Sdan */
10224fccf43aSdan static int sessionInitTable(sqlite3_session *pSession, SessionTable *pTab){
10234fccf43aSdan   if( pTab->nCol==0 ){
10246dc29e60Sdan     u8 *abPK;
1025e8d5648eSdan     assert( pTab->azCol==0 || pTab->abPK==0 );
1026e8d5648eSdan     pSession->rc = sessionTableInfo(pSession->db, pSession->zDb,
10276dc29e60Sdan         pTab->zName, &pTab->nCol, 0, &pTab->azCol, &abPK
1028e8d5648eSdan     );
10296dc29e60Sdan     if( pSession->rc==SQLITE_OK ){
10306dc29e60Sdan       int i;
10316dc29e60Sdan       for(i=0; i<pTab->nCol; i++){
10326dc29e60Sdan         if( abPK[i] ){
10336dc29e60Sdan           pTab->abPK = abPK;
10346dc29e60Sdan           break;
1035ca62ad57Sdan         }
10364fccf43aSdan       }
10376dc29e60Sdan     }
10386dc29e60Sdan   }
10396dc29e60Sdan   return (pSession->rc || pTab->abPK==0);
1040e8d5648eSdan }
1041e8d5648eSdan 
104277fc1d5bSdan /*
104377fc1d5bSdan ** This function is only called from with a pre-update-hook reporting a
104477fc1d5bSdan ** change on table pTab (attached to session pSession). The type of change
104577fc1d5bSdan ** (UPDATE, INSERT, DELETE) is specified by the first argument.
104677fc1d5bSdan **
104777fc1d5bSdan ** Unless one is already present or an error occurs, an entry is added
104877fc1d5bSdan ** to the changed-rows hash table associated with table pTab.
104977fc1d5bSdan */
1050e8d5648eSdan static void sessionPreupdateOneChange(
105177fc1d5bSdan   int op,                         /* One of SQLITE_UPDATE, INSERT, DELETE */
105277fc1d5bSdan   sqlite3_session *pSession,      /* Session object pTab is attached to */
105377fc1d5bSdan   SessionTable *pTab              /* Table that change applies to */
1054e8d5648eSdan ){
1055e8d5648eSdan   int iHash;
1056cf8e9144Sdan   int bNull = 0;
1057e8d5648eSdan   int rc = SQLITE_OK;
1058e8d5648eSdan 
1059e8d5648eSdan   if( pSession->rc ) return;
1060e8d5648eSdan 
1061e8d5648eSdan   /* Load table details if required */
1062e8d5648eSdan   if( sessionInitTable(pSession, pTab) ) return;
1063e8d5648eSdan 
10646dc29e60Sdan   /* Check the number of columns in this xPreUpdate call matches the
10656dc29e60Sdan   ** number of columns in the table.  */
10666dc29e60Sdan   if( pTab->nCol!=pSession->hook.xCount(pSession->hook.pCtx) ){
10676dc29e60Sdan     pSession->rc = SQLITE_SCHEMA;
10686dc29e60Sdan     return;
10696dc29e60Sdan   }
10706dc29e60Sdan 
1071e8d5648eSdan   /* Grow the hash table if required */
107264277f4aSdan   if( sessionGrowHash(0, pTab) ){
10735d607a6eSdan     pSession->rc = SQLITE_NOMEM;
10745d607a6eSdan     return;
10755d607a6eSdan   }
1076e8d5648eSdan 
107780fe2d93Sdan   /* Calculate the hash-key for this change. If the primary key of the row
107880fe2d93Sdan   ** includes a NULL value, exit early. Such changes are ignored by the
107980fe2d93Sdan   ** session module. */
1080cf8e9144Sdan   rc = sessionPreupdateHash(pSession, pTab, op==SQLITE_INSERT, &iHash, &bNull);
108180fe2d93Sdan   if( rc!=SQLITE_OK ) goto error_out;
108280fe2d93Sdan 
1083cf8e9144Sdan   if( bNull==0 ){
108480fe2d93Sdan     /* Search the hash table for an existing record for this row. */
1085b4480e94Sdan     SessionChange *pC;
10866734007dSdan     for(pC=pTab->apChange[iHash]; pC; pC=pC->pNext){
1087cf8e9144Sdan       if( sessionPreupdateEqual(pSession, pTab, pC, op) ) break;
1088e8d5648eSdan     }
108980fe2d93Sdan 
1090e8d5648eSdan     if( pC==0 ){
1091e8d5648eSdan       /* Create a new change object containing all the old values (if
1092e8d5648eSdan       ** this is an SQLITE_UPDATE or SQLITE_DELETE), or just the PK
1093e8d5648eSdan       ** values (if this is an INSERT). */
1094b4480e94Sdan       SessionChange *pChange; /* New change object */
1095e8d5648eSdan       int nByte;              /* Number of bytes to allocate */
1096e8d5648eSdan       int i;                  /* Used to iterate through columns */
1097e8d5648eSdan 
1098b4480e94Sdan       assert( rc==SQLITE_OK );
1099e8d5648eSdan       pTab->nEntry++;
1100e8d5648eSdan 
1101e8d5648eSdan       /* Figure out how large an allocation is required */
1102e8d5648eSdan       nByte = sizeof(SessionChange);
110380fe2d93Sdan       for(i=0; i<pTab->nCol; i++){
1104e8d5648eSdan         sqlite3_value *p = 0;
1105e8d5648eSdan         if( op!=SQLITE_INSERT ){
1106cf8e9144Sdan           TESTONLY(int trc = ) pSession->hook.xOld(pSession->hook.pCtx, i, &p);
110780fe2d93Sdan           assert( trc==SQLITE_OK );
110880fe2d93Sdan         }else if( pTab->abPK[i] ){
1109cf8e9144Sdan           TESTONLY(int trc = ) pSession->hook.xNew(pSession->hook.pCtx, i, &p);
111080fe2d93Sdan           assert( trc==SQLITE_OK );
1111e8d5648eSdan         }
111280fe2d93Sdan 
111380fe2d93Sdan         /* This may fail if SQLite value p contains a utf-16 string that must
111480fe2d93Sdan         ** be converted to utf-8 and an OOM error occurs while doing so. */
1115e8d5648eSdan         rc = sessionSerializeValue(0, p, &nByte);
111680fe2d93Sdan         if( rc!=SQLITE_OK ) goto error_out;
1117e8d5648eSdan       }
1118e8d5648eSdan 
1119e8d5648eSdan       /* Allocate the change object */
1120e8d5648eSdan       pChange = (SessionChange *)sqlite3_malloc(nByte);
1121e8d5648eSdan       if( !pChange ){
1122e8d5648eSdan         rc = SQLITE_NOMEM;
112380fe2d93Sdan         goto error_out;
1124e8d5648eSdan       }else{
1125e8d5648eSdan         memset(pChange, 0, sizeof(SessionChange));
1126e8d5648eSdan         pChange->aRecord = (u8 *)&pChange[1];
1127e8d5648eSdan       }
1128e8d5648eSdan 
112980fe2d93Sdan       /* Populate the change object. None of the preupdate_old(),
113080fe2d93Sdan       ** preupdate_new() or SerializeValue() calls below may fail as all
113180fe2d93Sdan       ** required values and encodings have already been cached in memory.
113280fe2d93Sdan       ** It is not possible for an OOM to occur in this block. */
1133e8d5648eSdan       nByte = 0;
113480fe2d93Sdan       for(i=0; i<pTab->nCol; i++){
1135e8d5648eSdan         sqlite3_value *p = 0;
1136e8d5648eSdan         if( op!=SQLITE_INSERT ){
1137cf8e9144Sdan           pSession->hook.xOld(pSession->hook.pCtx, i, &p);
113880fe2d93Sdan         }else if( pTab->abPK[i] ){
1139cf8e9144Sdan           pSession->hook.xNew(pSession->hook.pCtx, i, &p);
1140e8d5648eSdan         }
114180fe2d93Sdan         sessionSerializeValue(&pChange->aRecord[nByte], p, &nByte);
1142e8d5648eSdan       }
114380fe2d93Sdan 
114480fe2d93Sdan       /* Add the change to the hash-table */
1145cf8e9144Sdan       if( pSession->bIndirect || pSession->hook.xDepth(pSession->hook.pCtx) ){
1146b4480e94Sdan         pChange->bIndirect = 1;
1147b4480e94Sdan       }
114812ca0b56Sdan       pChange->nRecord = nByte;
1149798693b2Sdan       pChange->op = op;
1150e8d5648eSdan       pChange->pNext = pTab->apChange[iHash];
1151e8d5648eSdan       pTab->apChange[iHash] = pChange;
115280fe2d93Sdan 
115380fe2d93Sdan     }else if( pC->bIndirect ){
1154b4480e94Sdan       /* If the existing change is considered "indirect", but this current
1155b4480e94Sdan       ** change is "direct", mark the change object as direct. */
1156cf8e9144Sdan       if( pSession->hook.xDepth(pSession->hook.pCtx)==0
1157cf8e9144Sdan        && pSession->bIndirect==0
1158cf8e9144Sdan       ){
1159b4480e94Sdan         pC->bIndirect = 0;
1160b4480e94Sdan       }
1161e8d5648eSdan     }
11624fccf43aSdan   }
116312ca0b56Sdan 
116412ca0b56Sdan   /* If an error has occurred, mark the session object as failed. */
116580fe2d93Sdan  error_out:
116612ca0b56Sdan   if( rc!=SQLITE_OK ){
116712ca0b56Sdan     pSession->rc = rc;
116812ca0b56Sdan   }
116927453faeSdan }
11704fccf43aSdan 
1171cf8e9144Sdan static int sessionFindTable(
1172cf8e9144Sdan   sqlite3_session *pSession,
1173cf8e9144Sdan   const char *zName,
1174cf8e9144Sdan   SessionTable **ppTab
1175cf8e9144Sdan ){
1176cf8e9144Sdan   int rc = SQLITE_OK;
1177cf8e9144Sdan   int nName = sqlite3Strlen30(zName);
1178cf8e9144Sdan   SessionTable *pRet;
1179cf8e9144Sdan 
1180cf8e9144Sdan   /* Search for an existing table */
1181cf8e9144Sdan   for(pRet=pSession->pTable; pRet; pRet=pRet->pNext){
1182cf8e9144Sdan     if( 0==sqlite3_strnicmp(pRet->zName, zName, nName+1) ) break;
1183cf8e9144Sdan   }
1184cf8e9144Sdan 
1185cf8e9144Sdan   if( pRet==0 && pSession->bAutoAttach ){
1186cf8e9144Sdan     /* If there is a table-filter configured, invoke it. If it returns 0,
1187cf8e9144Sdan     ** do not automatically add the new table. */
1188cf8e9144Sdan     if( pSession->xTableFilter==0
1189cf8e9144Sdan      || pSession->xTableFilter(pSession->pFilterCtx, zName)
1190cf8e9144Sdan     ){
1191cf8e9144Sdan       rc = sqlite3session_attach(pSession, zName);
1192cf8e9144Sdan       if( rc==SQLITE_OK ){
11936c39e6a8Sdan         for(pRet=pSession->pTable; pRet->pNext; pRet=pRet->pNext);
1194cf8e9144Sdan         assert( 0==sqlite3_strnicmp(pRet->zName, zName, nName+1) );
1195cf8e9144Sdan       }
1196cf8e9144Sdan     }
1197cf8e9144Sdan   }
1198cf8e9144Sdan 
1199cf8e9144Sdan   assert( rc==SQLITE_OK || pRet==0 );
1200cf8e9144Sdan   *ppTab = pRet;
1201cf8e9144Sdan   return rc;
1202cf8e9144Sdan }
1203cf8e9144Sdan 
12044fccf43aSdan /*
12054fccf43aSdan ** The 'pre-update' hook registered by this module with SQLite databases.
12064fccf43aSdan */
12074fccf43aSdan static void xPreUpdate(
12084fccf43aSdan   void *pCtx,                     /* Copy of third arg to preupdate_hook() */
12094fccf43aSdan   sqlite3 *db,                    /* Database handle */
12104fccf43aSdan   int op,                         /* SQLITE_UPDATE, DELETE or INSERT */
12114fccf43aSdan   char const *zDb,                /* Database name */
12124fccf43aSdan   char const *zName,              /* Table name */
12134fccf43aSdan   sqlite3_int64 iKey1,            /* Rowid of row about to be deleted/updated */
12144fccf43aSdan   sqlite3_int64 iKey2             /* New rowid value (for a rowid UPDATE) */
12154fccf43aSdan ){
12164fccf43aSdan   sqlite3_session *pSession;
1217cfdbde21Sdrh   int nDb = sqlite3Strlen30(zDb);
12184fccf43aSdan 
12194c220252Sdan   assert( sqlite3_mutex_held(db->mutex) );
12204c220252Sdan 
12214fccf43aSdan   for(pSession=(sqlite3_session *)pCtx; pSession; pSession=pSession->pNext){
12224fccf43aSdan     SessionTable *pTab;
1223296c7658Sdan 
1224e8d5648eSdan     /* If this session is attached to a different database ("main", "temp"
1225e8d5648eSdan     ** etc.), or if it is not currently enabled, there is nothing to do. Skip
1226e8d5648eSdan     ** to the next session object attached to this database. */
1227296c7658Sdan     if( pSession->bEnable==0 ) continue;
12284fccf43aSdan     if( pSession->rc ) continue;
12294fccf43aSdan     if( sqlite3_strnicmp(zDb, pSession->zDb, nDb+1) ) continue;
1230296c7658Sdan 
1231cf8e9144Sdan     pSession->rc = sessionFindTable(pSession, zName, &pTab);
1232cf8e9144Sdan     if( pTab ){
1233cf8e9144Sdan       assert( pSession->rc==SQLITE_OK );
1234e8d5648eSdan       sessionPreupdateOneChange(op, pSession, pTab);
1235e8d5648eSdan       if( op==SQLITE_UPDATE ){
1236e8d5648eSdan         sessionPreupdateOneChange(SQLITE_INSERT, pSession, pTab);
12374fccf43aSdan       }
12384fccf43aSdan     }
12394fccf43aSdan   }
12404fccf43aSdan }
1241cf8e9144Sdan 
1242cf8e9144Sdan /*
1243cf8e9144Sdan ** The pre-update hook implementations.
1244cf8e9144Sdan */
1245cf8e9144Sdan static int sessionPreupdateOld(void *pCtx, int iVal, sqlite3_value **ppVal){
1246cf8e9144Sdan   return sqlite3_preupdate_old((sqlite3*)pCtx, iVal, ppVal);
1247cf8e9144Sdan }
1248cf8e9144Sdan static int sessionPreupdateNew(void *pCtx, int iVal, sqlite3_value **ppVal){
1249cf8e9144Sdan   return sqlite3_preupdate_new((sqlite3*)pCtx, iVal, ppVal);
1250cf8e9144Sdan }
1251cf8e9144Sdan static int sessionPreupdateCount(void *pCtx){
1252cf8e9144Sdan   return sqlite3_preupdate_count((sqlite3*)pCtx);
1253cf8e9144Sdan }
1254cf8e9144Sdan static int sessionPreupdateDepth(void *pCtx){
1255cf8e9144Sdan   return sqlite3_preupdate_depth((sqlite3*)pCtx);
1256cf8e9144Sdan }
1257cf8e9144Sdan 
1258cf8e9144Sdan /*
1259cf8e9144Sdan ** Install the pre-update hooks on the session object passed as the only
1260cf8e9144Sdan ** argument.
1261cf8e9144Sdan */
1262cf8e9144Sdan static void sessionPreupdateHooks(
1263cf8e9144Sdan   sqlite3_session *pSession
1264cf8e9144Sdan ){
1265cf8e9144Sdan   pSession->hook.pCtx = (void*)pSession->db;
1266cf8e9144Sdan   pSession->hook.xOld = sessionPreupdateOld;
1267cf8e9144Sdan   pSession->hook.xNew = sessionPreupdateNew;
1268cf8e9144Sdan   pSession->hook.xCount = sessionPreupdateCount;
1269cf8e9144Sdan   pSession->hook.xDepth = sessionPreupdateDepth;
1270cf8e9144Sdan }
1271cf8e9144Sdan 
1272cf8e9144Sdan typedef struct SessionDiffCtx SessionDiffCtx;
1273cf8e9144Sdan struct SessionDiffCtx {
1274cf8e9144Sdan   sqlite3_stmt *pStmt;
1275cf8e9144Sdan   int nOldOff;
1276cf8e9144Sdan };
1277cf8e9144Sdan 
1278cf8e9144Sdan /*
1279cf8e9144Sdan ** The diff hook implementations.
1280cf8e9144Sdan */
1281cf8e9144Sdan static int sessionDiffOld(void *pCtx, int iVal, sqlite3_value **ppVal){
1282cf8e9144Sdan   SessionDiffCtx *p = (SessionDiffCtx*)pCtx;
1283cf8e9144Sdan   *ppVal = sqlite3_column_value(p->pStmt, iVal+p->nOldOff);
1284cf8e9144Sdan   return SQLITE_OK;
1285cf8e9144Sdan }
1286cf8e9144Sdan static int sessionDiffNew(void *pCtx, int iVal, sqlite3_value **ppVal){
1287cf8e9144Sdan   SessionDiffCtx *p = (SessionDiffCtx*)pCtx;
1288cf8e9144Sdan   *ppVal = sqlite3_column_value(p->pStmt, iVal);
1289cf8e9144Sdan    return SQLITE_OK;
1290cf8e9144Sdan }
1291cf8e9144Sdan static int sessionDiffCount(void *pCtx){
1292cf8e9144Sdan   SessionDiffCtx *p = (SessionDiffCtx*)pCtx;
1293cf8e9144Sdan   return p->nOldOff ? p->nOldOff : sqlite3_column_count(p->pStmt);
1294cf8e9144Sdan }
1295cf8e9144Sdan static int sessionDiffDepth(void *pCtx){
1296cf8e9144Sdan   return 0;
1297cf8e9144Sdan }
1298cf8e9144Sdan 
1299cf8e9144Sdan /*
1300cf8e9144Sdan ** Install the diff hooks on the session object passed as the only
1301cf8e9144Sdan ** argument.
1302cf8e9144Sdan */
1303cf8e9144Sdan static void sessionDiffHooks(
1304cf8e9144Sdan   sqlite3_session *pSession,
1305cf8e9144Sdan   SessionDiffCtx *pDiffCtx
1306cf8e9144Sdan ){
1307cf8e9144Sdan   pSession->hook.pCtx = (void*)pDiffCtx;
1308cf8e9144Sdan   pSession->hook.xOld = sessionDiffOld;
1309cf8e9144Sdan   pSession->hook.xNew = sessionDiffNew;
1310cf8e9144Sdan   pSession->hook.xCount = sessionDiffCount;
1311cf8e9144Sdan   pSession->hook.xDepth = sessionDiffDepth;
1312cf8e9144Sdan }
1313cf8e9144Sdan 
1314cf8e9144Sdan static char *sessionExprComparePK(
1315cf8e9144Sdan   int nCol,
1316cf8e9144Sdan   const char *zDb1, const char *zDb2,
1317cf8e9144Sdan   const char *zTab,
1318cf8e9144Sdan   const char **azCol, u8 *abPK
1319cf8e9144Sdan ){
1320cf8e9144Sdan   int i;
1321cf8e9144Sdan   const char *zSep = "";
1322cf8e9144Sdan   char *zRet = 0;
1323cf8e9144Sdan 
1324cf8e9144Sdan   for(i=0; i<nCol; i++){
1325cf8e9144Sdan     if( abPK[i] ){
1326cf8e9144Sdan       zRet = sqlite3_mprintf("%z%s\"%w\".\"%w\".\"%w\"=\"%w\".\"%w\".\"%w\"",
1327cf8e9144Sdan           zRet, zSep, zDb1, zTab, azCol[i], zDb2, zTab, azCol[i]
1328cf8e9144Sdan       );
1329cf8e9144Sdan       zSep = " AND ";
1330cf8e9144Sdan       if( zRet==0 ) break;
1331cf8e9144Sdan     }
1332cf8e9144Sdan   }
1333cf8e9144Sdan 
1334cf8e9144Sdan   return zRet;
1335cf8e9144Sdan }
1336cf8e9144Sdan 
1337cf8e9144Sdan static char *sessionExprCompareOther(
1338cf8e9144Sdan   int nCol,
1339cf8e9144Sdan   const char *zDb1, const char *zDb2,
1340cf8e9144Sdan   const char *zTab,
1341cf8e9144Sdan   const char **azCol, u8 *abPK
1342cf8e9144Sdan ){
1343cf8e9144Sdan   int i;
1344cf8e9144Sdan   const char *zSep = "";
1345cf8e9144Sdan   char *zRet = 0;
1346cf8e9144Sdan   int bHave = 0;
1347cf8e9144Sdan 
1348cf8e9144Sdan   for(i=0; i<nCol; i++){
1349cf8e9144Sdan     if( abPK[i]==0 ){
1350cf8e9144Sdan       bHave = 1;
1351cf8e9144Sdan       zRet = sqlite3_mprintf(
1352cf8e9144Sdan           "%z%s\"%w\".\"%w\".\"%w\" IS NOT \"%w\".\"%w\".\"%w\"",
1353cf8e9144Sdan           zRet, zSep, zDb1, zTab, azCol[i], zDb2, zTab, azCol[i]
1354cf8e9144Sdan       );
1355cf8e9144Sdan       zSep = " OR ";
1356cf8e9144Sdan       if( zRet==0 ) break;
1357cf8e9144Sdan     }
1358cf8e9144Sdan   }
1359cf8e9144Sdan 
1360cf8e9144Sdan   if( bHave==0 ){
1361cf8e9144Sdan     assert( zRet==0 );
1362cf8e9144Sdan     zRet = sqlite3_mprintf("0");
1363cf8e9144Sdan   }
1364cf8e9144Sdan 
1365cf8e9144Sdan   return zRet;
1366cf8e9144Sdan }
1367cf8e9144Sdan 
1368cf8e9144Sdan static char *sessionSelectFindNew(
1369cf8e9144Sdan   int nCol,
1370cf8e9144Sdan   const char *zDb1,      /* Pick rows in this db only */
1371cf8e9144Sdan   const char *zDb2,      /* But not in this one */
1372cf8e9144Sdan   const char *zTbl,      /* Table name */
1373cf8e9144Sdan   const char *zExpr
1374cf8e9144Sdan ){
1375cf8e9144Sdan   char *zRet = sqlite3_mprintf(
1376cf8e9144Sdan       "SELECT * FROM \"%w\".\"%w\" WHERE NOT EXISTS ("
1377cf8e9144Sdan       "  SELECT 1 FROM \"%w\".\"%w\" WHERE %s"
1378cf8e9144Sdan       ")",
1379cf8e9144Sdan       zDb1, zTbl, zDb2, zTbl, zExpr
1380cf8e9144Sdan   );
1381cf8e9144Sdan   return zRet;
1382cf8e9144Sdan }
1383cf8e9144Sdan 
1384cf8e9144Sdan static int sessionDiffFindNew(
1385cf8e9144Sdan   int op,
1386cf8e9144Sdan   sqlite3_session *pSession,
1387cf8e9144Sdan   SessionTable *pTab,
1388cf8e9144Sdan   const char *zDb1,
1389cf8e9144Sdan   const char *zDb2,
1390cf8e9144Sdan   char *zExpr
1391cf8e9144Sdan ){
1392cf8e9144Sdan   int rc = SQLITE_OK;
1393cf8e9144Sdan   char *zStmt = sessionSelectFindNew(pTab->nCol, zDb1, zDb2, pTab->zName,zExpr);
1394cf8e9144Sdan 
1395cf8e9144Sdan   if( zStmt==0 ){
1396cf8e9144Sdan     rc = SQLITE_NOMEM;
1397cf8e9144Sdan   }else{
1398cf8e9144Sdan     sqlite3_stmt *pStmt;
1399cf8e9144Sdan     rc = sqlite3_prepare(pSession->db, zStmt, -1, &pStmt, 0);
1400cf8e9144Sdan     if( rc==SQLITE_OK ){
1401cf8e9144Sdan       SessionDiffCtx *pDiffCtx = (SessionDiffCtx*)pSession->hook.pCtx;
1402cf8e9144Sdan       pDiffCtx->pStmt = pStmt;
1403cf8e9144Sdan       pDiffCtx->nOldOff = 0;
1404cf8e9144Sdan       while( SQLITE_ROW==sqlite3_step(pStmt) ){
1405cf8e9144Sdan         sessionPreupdateOneChange(op, pSession, pTab);
1406cf8e9144Sdan       }
1407cf8e9144Sdan       rc = sqlite3_finalize(pStmt);
1408cf8e9144Sdan     }
1409cf8e9144Sdan     sqlite3_free(zStmt);
1410cf8e9144Sdan   }
1411cf8e9144Sdan 
1412cf8e9144Sdan   return rc;
1413cf8e9144Sdan }
1414cf8e9144Sdan 
1415cf8e9144Sdan static int sessionDiffFindModified(
1416cf8e9144Sdan   sqlite3_session *pSession,
1417cf8e9144Sdan   SessionTable *pTab,
1418cf8e9144Sdan   const char *zFrom,
1419cf8e9144Sdan   const char *zExpr
1420cf8e9144Sdan ){
1421cf8e9144Sdan   int rc = SQLITE_OK;
1422cf8e9144Sdan 
1423cf8e9144Sdan   char *zExpr2 = sessionExprCompareOther(pTab->nCol,
1424cf8e9144Sdan       pSession->zDb, zFrom, pTab->zName, pTab->azCol, pTab->abPK
1425cf8e9144Sdan   );
1426cf8e9144Sdan   if( zExpr2==0 ){
1427cf8e9144Sdan     rc = SQLITE_NOMEM;
1428cf8e9144Sdan   }else{
1429cf8e9144Sdan     char *zStmt = sqlite3_mprintf(
1430dd009f83Sdan         "SELECT * FROM \"%w\".\"%w\", \"%w\".\"%w\" WHERE %s AND (%z)",
1431cf8e9144Sdan         pSession->zDb, pTab->zName, zFrom, pTab->zName, zExpr, zExpr2
1432cf8e9144Sdan     );
1433cf8e9144Sdan     if( zStmt==0 ){
1434cf8e9144Sdan       rc = SQLITE_NOMEM;
1435cf8e9144Sdan     }else{
1436cf8e9144Sdan       sqlite3_stmt *pStmt;
1437cf8e9144Sdan       rc = sqlite3_prepare(pSession->db, zStmt, -1, &pStmt, 0);
1438cf8e9144Sdan 
1439cf8e9144Sdan       if( rc==SQLITE_OK ){
1440cf8e9144Sdan         SessionDiffCtx *pDiffCtx = (SessionDiffCtx*)pSession->hook.pCtx;
1441cf8e9144Sdan         pDiffCtx->pStmt = pStmt;
1442cf8e9144Sdan         pDiffCtx->nOldOff = pTab->nCol;
1443cf8e9144Sdan         while( SQLITE_ROW==sqlite3_step(pStmt) ){
1444cf8e9144Sdan           sessionPreupdateOneChange(SQLITE_UPDATE, pSession, pTab);
1445cf8e9144Sdan         }
1446cf8e9144Sdan         rc = sqlite3_finalize(pStmt);
1447cf8e9144Sdan       }
1448cf8e9144Sdan       sqlite3_free(zStmt);
1449cf8e9144Sdan     }
1450cf8e9144Sdan   }
1451cf8e9144Sdan 
1452cf8e9144Sdan   return rc;
1453cf8e9144Sdan }
1454cf8e9144Sdan 
1455cf8e9144Sdan int sqlite3session_diff(
1456cf8e9144Sdan   sqlite3_session *pSession,
1457cf8e9144Sdan   const char *zFrom,
1458cf8e9144Sdan   const char *zTbl,
1459cf8e9144Sdan   char **pzErrMsg
1460cf8e9144Sdan ){
1461cf8e9144Sdan   const char *zDb = pSession->zDb;
1462cf8e9144Sdan   int rc = pSession->rc;
1463cf8e9144Sdan   SessionDiffCtx d;
1464cf8e9144Sdan 
1465cf8e9144Sdan   memset(&d, 0, sizeof(d));
1466cf8e9144Sdan   sessionDiffHooks(pSession, &d);
1467cf8e9144Sdan 
146810dc553cSdan   sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
1469cf8e9144Sdan   if( pzErrMsg ) *pzErrMsg = 0;
1470cf8e9144Sdan   if( rc==SQLITE_OK ){
1471cf8e9144Sdan     char *zExpr = 0;
1472cf8e9144Sdan     sqlite3 *db = pSession->db;
1473cf8e9144Sdan     SessionTable *pTo;            /* Table zTbl */
1474cf8e9144Sdan 
1475cf8e9144Sdan     /* Locate and if necessary initialize the target table object */
1476cf8e9144Sdan     rc = sessionFindTable(pSession, zTbl, &pTo);
1477cf8e9144Sdan     if( pTo==0 ) goto diff_out;
14786dc29e60Sdan     if( sessionInitTable(pSession, pTo) ){
14796dc29e60Sdan       rc = pSession->rc;
14806dc29e60Sdan       goto diff_out;
1481cf8e9144Sdan     }
1482cf8e9144Sdan 
1483cf8e9144Sdan     /* Check the table schemas match */
1484cf8e9144Sdan     if( rc==SQLITE_OK ){
14854cc923e3Sdan       int bHasPk = 0;
1486b9db9099Sdan       int bMismatch = 0;
1487cf8e9144Sdan       int nCol;                   /* Columns in zFrom.zTbl */
1488cf8e9144Sdan       u8 *abPK;
1489cf8e9144Sdan       const char **azCol = 0;
1490cf8e9144Sdan       rc = sessionTableInfo(db, zFrom, zTbl, &nCol, 0, &azCol, &abPK);
1491cf8e9144Sdan       if( rc==SQLITE_OK ){
14924cc923e3Sdan         if( pTo->nCol!=nCol ){
1493cf8e9144Sdan           bMismatch = 1;
1494cf8e9144Sdan         }else{
1495cf8e9144Sdan           int i;
1496cf8e9144Sdan           for(i=0; i<nCol; i++){
14974cc923e3Sdan             if( pTo->abPK[i]!=abPK[i] ) bMismatch = 1;
1498cf8e9144Sdan             if( sqlite3_stricmp(azCol[i], pTo->azCol[i]) ) bMismatch = 1;
14994cc923e3Sdan             if( abPK[i] ) bHasPk = 1;
1500cf8e9144Sdan           }
1501cf8e9144Sdan         }
1502cf8e9144Sdan 
1503cf8e9144Sdan       }
1504dbbd8160Sdrh       sqlite3_free((char*)azCol);
1505b9db9099Sdan       if( bMismatch ){
1506b9db9099Sdan         *pzErrMsg = sqlite3_mprintf("table schemas do not match");
1507b9db9099Sdan         rc = SQLITE_SCHEMA;
1508b9db9099Sdan       }
1509b9db9099Sdan       if( bHasPk==0 ){
1510b9db9099Sdan         /* Ignore tables with no primary keys */
1511b9db9099Sdan         goto diff_out;
1512b9db9099Sdan       }
1513cf8e9144Sdan     }
1514cf8e9144Sdan 
1515cf8e9144Sdan     if( rc==SQLITE_OK ){
1516cf8e9144Sdan       zExpr = sessionExprComparePK(pTo->nCol,
1517cf8e9144Sdan           zDb, zFrom, pTo->zName, pTo->azCol, pTo->abPK
1518cf8e9144Sdan       );
1519cf8e9144Sdan     }
1520cf8e9144Sdan 
1521cf8e9144Sdan     /* Find new rows */
1522cf8e9144Sdan     if( rc==SQLITE_OK ){
1523cf8e9144Sdan       rc = sessionDiffFindNew(SQLITE_INSERT, pSession, pTo, zDb, zFrom, zExpr);
1524cf8e9144Sdan     }
1525cf8e9144Sdan 
1526cf8e9144Sdan     /* Find old rows */
1527cf8e9144Sdan     if( rc==SQLITE_OK ){
1528cf8e9144Sdan       rc = sessionDiffFindNew(SQLITE_DELETE, pSession, pTo, zFrom, zDb, zExpr);
1529cf8e9144Sdan     }
1530cf8e9144Sdan 
1531cf8e9144Sdan     /* Find modified rows */
1532cf8e9144Sdan     if( rc==SQLITE_OK ){
1533cf8e9144Sdan       rc = sessionDiffFindModified(pSession, pTo, zFrom, zExpr);
1534cf8e9144Sdan     }
1535cf8e9144Sdan 
1536cf8e9144Sdan     sqlite3_free(zExpr);
1537cf8e9144Sdan   }
1538cf8e9144Sdan 
1539cf8e9144Sdan  diff_out:
1540cf8e9144Sdan   sessionPreupdateHooks(pSession);
154110dc553cSdan   sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
1542cf8e9144Sdan   return rc;
1543296c7658Sdan }
15444fccf43aSdan 
15454fccf43aSdan /*
15464fccf43aSdan ** Create a session object. This session object will record changes to
15474fccf43aSdan ** database zDb attached to connection db.
15484fccf43aSdan */
15494fccf43aSdan int sqlite3session_create(
15504fccf43aSdan   sqlite3 *db,                    /* Database handle */
15514fccf43aSdan   const char *zDb,                /* Name of db (e.g. "main") */
15524fccf43aSdan   sqlite3_session **ppSession     /* OUT: New session object */
15534fccf43aSdan ){
1554296c7658Sdan   sqlite3_session *pNew;          /* Newly allocated session object */
1555296c7658Sdan   sqlite3_session *pOld;          /* Session object already attached to db */
1556cfdbde21Sdrh   int nDb = sqlite3Strlen30(zDb); /* Length of zDb in bytes */
15574fccf43aSdan 
1558296c7658Sdan   /* Zero the output value in case an error occurs. */
15594fccf43aSdan   *ppSession = 0;
15604fccf43aSdan 
15614fccf43aSdan   /* Allocate and populate the new session object. */
15624fccf43aSdan   pNew = (sqlite3_session *)sqlite3_malloc(sizeof(sqlite3_session) + nDb + 1);
15634fccf43aSdan   if( !pNew ) return SQLITE_NOMEM;
15644fccf43aSdan   memset(pNew, 0, sizeof(sqlite3_session));
15654fccf43aSdan   pNew->db = db;
15664fccf43aSdan   pNew->zDb = (char *)&pNew[1];
1567296c7658Sdan   pNew->bEnable = 1;
15684fccf43aSdan   memcpy(pNew->zDb, zDb, nDb+1);
1569cf8e9144Sdan   sessionPreupdateHooks(pNew);
15704fccf43aSdan 
15714fccf43aSdan   /* Add the new session object to the linked list of session objects
15724fccf43aSdan   ** attached to database handle $db. Do this under the cover of the db
15734fccf43aSdan   ** handle mutex.  */
15744fccf43aSdan   sqlite3_mutex_enter(sqlite3_db_mutex(db));
15754fccf43aSdan   pOld = (sqlite3_session*)sqlite3_preupdate_hook(db, xPreUpdate, (void*)pNew);
15764fccf43aSdan   pNew->pNext = pOld;
15774fccf43aSdan   sqlite3_mutex_leave(sqlite3_db_mutex(db));
15784fccf43aSdan 
15794fccf43aSdan   *ppSession = pNew;
15804fccf43aSdan   return SQLITE_OK;
15814fccf43aSdan }
15824fccf43aSdan 
158377fc1d5bSdan /*
158477fc1d5bSdan ** Free the list of table objects passed as the first argument. The contents
158577fc1d5bSdan ** of the changed-rows hash tables are also deleted.
158677fc1d5bSdan */
15871ffe7c7fSdrh static void sessionDeleteTable(SessionTable *pList){
15885d607a6eSdan   SessionTable *pNext;
15895d607a6eSdan   SessionTable *pTab;
15905d607a6eSdan 
15915d607a6eSdan   for(pTab=pList; pTab; pTab=pNext){
15925d607a6eSdan     int i;
15935d607a6eSdan     pNext = pTab->pNext;
15945d607a6eSdan     for(i=0; i<pTab->nChange; i++){
15955d607a6eSdan       SessionChange *p;
15965d607a6eSdan       SessionChange *pNext;
15975d607a6eSdan       for(p=pTab->apChange[i]; p; p=pNext){
15985d607a6eSdan         pNext = p->pNext;
15995d607a6eSdan         sqlite3_free(p);
16005d607a6eSdan       }
16015d607a6eSdan     }
16025d607a6eSdan     sqlite3_free((char*)pTab->azCol);  /* cast works around VC++ bug */
16035d607a6eSdan     sqlite3_free(pTab->apChange);
16045d607a6eSdan     sqlite3_free(pTab);
16055d607a6eSdan   }
16065d607a6eSdan }
16075d607a6eSdan 
16084fccf43aSdan /*
16094fccf43aSdan ** Delete a session object previously allocated using sqlite3session_create().
16104fccf43aSdan */
16114fccf43aSdan void sqlite3session_delete(sqlite3_session *pSession){
16124fccf43aSdan   sqlite3 *db = pSession->db;
16134fccf43aSdan   sqlite3_session *pHead;
16144fccf43aSdan   sqlite3_session **pp;
16154fccf43aSdan 
1616296c7658Sdan   /* Unlink the session from the linked list of sessions attached to the
1617296c7658Sdan   ** database handle. Hold the db mutex while doing so.  */
16184fccf43aSdan   sqlite3_mutex_enter(sqlite3_db_mutex(db));
16194fccf43aSdan   pHead = (sqlite3_session*)sqlite3_preupdate_hook(db, 0, 0);
162050d348b1Sdrh   for(pp=&pHead; ALWAYS((*pp)!=0); pp=&((*pp)->pNext)){
162150d348b1Sdrh     if( (*pp)==pSession ){
16224fccf43aSdan       *pp = (*pp)->pNext;
16234fccf43aSdan       if( pHead ) sqlite3_preupdate_hook(db, xPreUpdate, (void*)pHead);
162450d348b1Sdrh       break;
162550d348b1Sdrh     }
162650d348b1Sdrh   }
16274fccf43aSdan   sqlite3_mutex_leave(sqlite3_db_mutex(db));
16284fccf43aSdan 
1629296c7658Sdan   /* Delete all attached table objects. And the contents of their
1630296c7658Sdan   ** associated hash-tables. */
16315d607a6eSdan   sessionDeleteTable(pSession->pTable);
16324fccf43aSdan 
1633296c7658Sdan   /* Free the session object itself. */
16344fccf43aSdan   sqlite3_free(pSession);
16354fccf43aSdan }
16364fccf43aSdan 
16374fccf43aSdan /*
16387531a5a3Sdan ** Set a table filter on a Session Object.
16397531a5a3Sdan */
16407531a5a3Sdan void sqlite3session_table_filter(
16417531a5a3Sdan   sqlite3_session *pSession,
16427531a5a3Sdan   int(*xFilter)(void*, const char*),
16437531a5a3Sdan   void *pCtx                      /* First argument passed to xFilter */
16447531a5a3Sdan ){
16457531a5a3Sdan   pSession->bAutoAttach = 1;
16467531a5a3Sdan   pSession->pFilterCtx = pCtx;
16477531a5a3Sdan   pSession->xTableFilter = xFilter;
16487531a5a3Sdan }
16497531a5a3Sdan 
16507531a5a3Sdan /*
16514fccf43aSdan ** Attach a table to a session. All subsequent changes made to the table
16524fccf43aSdan ** while the session object is enabled will be recorded.
16534fccf43aSdan **
16544fccf43aSdan ** Only tables that have a PRIMARY KEY defined may be attached. It does
16554fccf43aSdan ** not matter if the PRIMARY KEY is an "INTEGER PRIMARY KEY" (rowid alias)
16564fccf43aSdan ** or not.
16574fccf43aSdan */
16584fccf43aSdan int sqlite3session_attach(
16594fccf43aSdan   sqlite3_session *pSession,      /* Session object */
16604fccf43aSdan   const char *zName               /* Table name */
16614fccf43aSdan ){
1662ff4d0f41Sdan   int rc = SQLITE_OK;
1663ff4d0f41Sdan   sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
1664ff4d0f41Sdan 
1665ff4d0f41Sdan   if( !zName ){
1666ff4d0f41Sdan     pSession->bAutoAttach = 1;
1667ff4d0f41Sdan   }else{
1668296c7658Sdan     SessionTable *pTab;           /* New table object (if required) */
1669296c7658Sdan     int nName;                    /* Number of bytes in string zName */
16704fccf43aSdan 
16714fccf43aSdan     /* First search for an existing entry. If one is found, this call is
16724fccf43aSdan     ** a no-op. Return early. */
1673cfdbde21Sdrh     nName = sqlite3Strlen30(zName);
16744fccf43aSdan     for(pTab=pSession->pTable; pTab; pTab=pTab->pNext){
16754c220252Sdan       if( 0==sqlite3_strnicmp(pTab->zName, zName, nName+1) ) break;
16764fccf43aSdan     }
16774fccf43aSdan 
16784c220252Sdan     if( !pTab ){
16794fccf43aSdan       /* Allocate new SessionTable object. */
16804fccf43aSdan       pTab = (SessionTable *)sqlite3_malloc(sizeof(SessionTable) + nName + 1);
16814c220252Sdan       if( !pTab ){
16824c220252Sdan         rc = SQLITE_NOMEM;
16834c220252Sdan       }else{
16846c39e6a8Sdan         /* Populate the new SessionTable object and link it into the list.
16856c39e6a8Sdan         ** The new object must be linked onto the end of the list, not
16866c39e6a8Sdan         ** simply added to the start of it in order to ensure that tables
16876c39e6a8Sdan         ** appear in the correct order when a changeset or patchset is
16886c39e6a8Sdan         ** eventually generated. */
16896c39e6a8Sdan         SessionTable **ppTab;
16904fccf43aSdan         memset(pTab, 0, sizeof(SessionTable));
16914fccf43aSdan         pTab->zName = (char *)&pTab[1];
16924fccf43aSdan         memcpy(pTab->zName, zName, nName+1);
16936c39e6a8Sdan         for(ppTab=&pSession->pTable; *ppTab; ppTab=&(*ppTab)->pNext);
16946c39e6a8Sdan         *ppTab = pTab;
16954c220252Sdan       }
16964c220252Sdan     }
1697ff4d0f41Sdan   }
16984fccf43aSdan 
16994c220252Sdan   sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
17004c220252Sdan   return rc;
17014fccf43aSdan }
17024fccf43aSdan 
1703296c7658Sdan /*
1704296c7658Sdan ** Ensure that there is room in the buffer to append nByte bytes of data.
1705296c7658Sdan ** If not, use sqlite3_realloc() to grow the buffer so that there is.
1706296c7658Sdan **
1707296c7658Sdan ** If successful, return zero. Otherwise, if an OOM condition is encountered,
1708296c7658Sdan ** set *pRc to SQLITE_NOMEM and return non-zero.
1709296c7658Sdan */
17104fccf43aSdan static int sessionBufferGrow(SessionBuffer *p, int nByte, int *pRc){
171180fe2d93Sdan   if( *pRc==SQLITE_OK && p->nAlloc-p->nBuf<nByte ){
17124fccf43aSdan     u8 *aNew;
17134fccf43aSdan     int nNew = p->nAlloc ? p->nAlloc : 128;
17144fccf43aSdan     do {
17154fccf43aSdan       nNew = nNew*2;
1716ef7a6304Sdan     }while( nNew<(p->nBuf+nByte) );
17174fccf43aSdan 
17184fccf43aSdan     aNew = (u8 *)sqlite3_realloc(p->aBuf, nNew);
17194fccf43aSdan     if( 0==aNew ){
17204fccf43aSdan       *pRc = SQLITE_NOMEM;
172180fe2d93Sdan     }else{
17224fccf43aSdan       p->aBuf = aNew;
17234fccf43aSdan       p->nAlloc = nNew;
17244fccf43aSdan     }
172580fe2d93Sdan   }
172680fe2d93Sdan   return (*pRc!=SQLITE_OK);
17274fccf43aSdan }
17284fccf43aSdan 
1729296c7658Sdan /*
1730fa122adaSdan ** Append the value passed as the second argument to the buffer passed
1731fa122adaSdan ** as the first.
1732fa122adaSdan **
1733fa122adaSdan ** This function is a no-op if *pRc is non-zero when it is called.
1734fa122adaSdan ** Otherwise, if an error occurs, *pRc is set to an SQLite error code
1735fa122adaSdan ** before returning.
1736fa122adaSdan */
1737fa122adaSdan static void sessionAppendValue(SessionBuffer *p, sqlite3_value *pVal, int *pRc){
1738fa122adaSdan   int rc = *pRc;
1739fa122adaSdan   if( rc==SQLITE_OK ){
1740fa122adaSdan     int nByte = 0;
1741e8fa8c96Sdan     rc = sessionSerializeValue(0, pVal, &nByte);
1742fa122adaSdan     sessionBufferGrow(p, nByte, &rc);
1743fa122adaSdan     if( rc==SQLITE_OK ){
1744fa122adaSdan       rc = sessionSerializeValue(&p->aBuf[p->nBuf], pVal, 0);
1745fa122adaSdan       p->nBuf += nByte;
1746fa122adaSdan     }else{
1747fa122adaSdan       *pRc = rc;
1748fa122adaSdan     }
1749fa122adaSdan   }
1750fa122adaSdan }
1751fa122adaSdan 
1752fa122adaSdan /*
1753296c7658Sdan ** This function is a no-op if *pRc is other than SQLITE_OK when it is
1754296c7658Sdan ** called. Otherwise, append a single byte to the buffer.
1755296c7658Sdan **
1756296c7658Sdan ** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
1757296c7658Sdan ** returning.
1758296c7658Sdan */
17594fccf43aSdan static void sessionAppendByte(SessionBuffer *p, u8 v, int *pRc){
176080fe2d93Sdan   if( 0==sessionBufferGrow(p, 1, pRc) ){
17614fccf43aSdan     p->aBuf[p->nBuf++] = v;
17624fccf43aSdan   }
17634fccf43aSdan }
17644fccf43aSdan 
1765296c7658Sdan /*
1766296c7658Sdan ** This function is a no-op if *pRc is other than SQLITE_OK when it is
1767296c7658Sdan ** called. Otherwise, append a single varint to the buffer.
1768296c7658Sdan **
1769296c7658Sdan ** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
1770296c7658Sdan ** returning.
1771296c7658Sdan */
1772cfdbde21Sdrh static void sessionAppendVarint(SessionBuffer *p, int v, int *pRc){
177380fe2d93Sdan   if( 0==sessionBufferGrow(p, 9, pRc) ){
17744fccf43aSdan     p->nBuf += sessionVarintPut(&p->aBuf[p->nBuf], v);
17754fccf43aSdan   }
17764fccf43aSdan }
17774fccf43aSdan 
1778296c7658Sdan /*
1779296c7658Sdan ** This function is a no-op if *pRc is other than SQLITE_OK when it is
1780296c7658Sdan ** called. Otherwise, append a blob of data to the buffer.
1781296c7658Sdan **
1782296c7658Sdan ** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
1783296c7658Sdan ** returning.
1784296c7658Sdan */
17854fccf43aSdan static void sessionAppendBlob(
17864fccf43aSdan   SessionBuffer *p,
17874fccf43aSdan   const u8 *aBlob,
17884fccf43aSdan   int nBlob,
17894fccf43aSdan   int *pRc
17904fccf43aSdan ){
179180fe2d93Sdan   if( 0==sessionBufferGrow(p, nBlob, pRc) ){
17924fccf43aSdan     memcpy(&p->aBuf[p->nBuf], aBlob, nBlob);
17934fccf43aSdan     p->nBuf += nBlob;
17944fccf43aSdan   }
17954fccf43aSdan }
17964fccf43aSdan 
1797296c7658Sdan /*
1798296c7658Sdan ** This function is a no-op if *pRc is other than SQLITE_OK when it is
1799296c7658Sdan ** called. Otherwise, append a string to the buffer. All bytes in the string
1800296c7658Sdan ** up to (but not including) the nul-terminator are written to the buffer.
1801296c7658Sdan **
1802296c7658Sdan ** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
1803296c7658Sdan ** returning.
1804296c7658Sdan */
1805d5f0767cSdan static void sessionAppendStr(
1806d5f0767cSdan   SessionBuffer *p,
1807d5f0767cSdan   const char *zStr,
1808d5f0767cSdan   int *pRc
1809d5f0767cSdan ){
1810cfdbde21Sdrh   int nStr = sqlite3Strlen30(zStr);
181180fe2d93Sdan   if( 0==sessionBufferGrow(p, nStr, pRc) ){
1812d5f0767cSdan     memcpy(&p->aBuf[p->nBuf], zStr, nStr);
1813d5f0767cSdan     p->nBuf += nStr;
1814d5f0767cSdan   }
1815d5f0767cSdan }
1816d5f0767cSdan 
1817296c7658Sdan /*
1818296c7658Sdan ** This function is a no-op if *pRc is other than SQLITE_OK when it is
1819296c7658Sdan ** called. Otherwise, append the string representation of integer iVal
1820296c7658Sdan ** to the buffer. No nul-terminator is written.
1821296c7658Sdan **
1822296c7658Sdan ** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
1823296c7658Sdan ** returning.
1824296c7658Sdan */
1825d5f0767cSdan static void sessionAppendInteger(
1826296c7658Sdan   SessionBuffer *p,               /* Buffer to append to */
1827296c7658Sdan   int iVal,                       /* Value to write the string rep. of */
1828296c7658Sdan   int *pRc                        /* IN/OUT: Error code */
1829d5f0767cSdan ){
1830d5f0767cSdan   char aBuf[24];
1831d5f0767cSdan   sqlite3_snprintf(sizeof(aBuf)-1, aBuf, "%d", iVal);
1832d5f0767cSdan   sessionAppendStr(p, aBuf, pRc);
1833d5f0767cSdan }
1834d5f0767cSdan 
1835296c7658Sdan /*
1836296c7658Sdan ** This function is a no-op if *pRc is other than SQLITE_OK when it is
1837296c7658Sdan ** called. Otherwise, append the string zStr enclosed in quotes (") and
1838296c7658Sdan ** with any embedded quote characters escaped to the buffer. No
1839296c7658Sdan ** nul-terminator byte is written.
1840296c7658Sdan **
1841296c7658Sdan ** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
1842296c7658Sdan ** returning.
1843296c7658Sdan */
1844d5f0767cSdan static void sessionAppendIdent(
1845296c7658Sdan   SessionBuffer *p,               /* Buffer to a append to */
1846296c7658Sdan   const char *zStr,               /* String to quote, escape and append */
1847296c7658Sdan   int *pRc                        /* IN/OUT: Error code */
1848d5f0767cSdan ){
1849cfdbde21Sdrh   int nStr = sqlite3Strlen30(zStr)*2 + 2 + 1;
185080fe2d93Sdan   if( 0==sessionBufferGrow(p, nStr, pRc) ){
1851d5f0767cSdan     char *zOut = (char *)&p->aBuf[p->nBuf];
1852d5f0767cSdan     const char *zIn = zStr;
1853d5f0767cSdan     *zOut++ = '"';
1854d5f0767cSdan     while( *zIn ){
1855d5f0767cSdan       if( *zIn=='"' ) *zOut++ = '"';
1856d5f0767cSdan       *zOut++ = *(zIn++);
1857d5f0767cSdan     }
1858d5f0767cSdan     *zOut++ = '"';
1859cfdbde21Sdrh     p->nBuf = (int)((u8 *)zOut - p->aBuf);
1860d5f0767cSdan   }
1861d5f0767cSdan }
1862d5f0767cSdan 
1863296c7658Sdan /*
1864296c7658Sdan ** This function is a no-op if *pRc is other than SQLITE_OK when it is
1865296c7658Sdan ** called. Otherwse, it appends the serialized version of the value stored
1866296c7658Sdan ** in column iCol of the row that SQL statement pStmt currently points
1867296c7658Sdan ** to to the buffer.
1868296c7658Sdan */
18694fccf43aSdan static void sessionAppendCol(
1870296c7658Sdan   SessionBuffer *p,               /* Buffer to append to */
1871296c7658Sdan   sqlite3_stmt *pStmt,            /* Handle pointing to row containing value */
1872296c7658Sdan   int iCol,                       /* Column to read value from */
1873296c7658Sdan   int *pRc                        /* IN/OUT: Error code */
18744fccf43aSdan ){
18754fccf43aSdan   if( *pRc==SQLITE_OK ){
18764fccf43aSdan     int eType = sqlite3_column_type(pStmt, iCol);
18774fccf43aSdan     sessionAppendByte(p, (u8)eType, pRc);
18784fccf43aSdan     if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
18794fccf43aSdan       sqlite3_int64 i;
18804fccf43aSdan       u8 aBuf[8];
18814fccf43aSdan       if( eType==SQLITE_INTEGER ){
18824fccf43aSdan         i = sqlite3_column_int64(pStmt, iCol);
18834fccf43aSdan       }else{
18844fccf43aSdan         double r = sqlite3_column_double(pStmt, iCol);
18854fccf43aSdan         memcpy(&i, &r, 8);
18864fccf43aSdan       }
1887296c7658Sdan       sessionPutI64(aBuf, i);
18884fccf43aSdan       sessionAppendBlob(p, aBuf, 8, pRc);
18894fccf43aSdan     }
18904fccf43aSdan     if( eType==SQLITE_BLOB || eType==SQLITE_TEXT ){
18916734007dSdan       u8 *z;
18923cc89d95Sdan       int nByte;
18936734007dSdan       if( eType==SQLITE_BLOB ){
18946734007dSdan         z = (u8 *)sqlite3_column_blob(pStmt, iCol);
18956734007dSdan       }else{
18966734007dSdan         z = (u8 *)sqlite3_column_text(pStmt, iCol);
18976734007dSdan       }
18983cc89d95Sdan       nByte = sqlite3_column_bytes(pStmt, iCol);
18993cc89d95Sdan       if( z || (eType==SQLITE_BLOB && nByte==0) ){
19004fccf43aSdan         sessionAppendVarint(p, nByte, pRc);
19016734007dSdan         sessionAppendBlob(p, z, nByte, pRc);
19026734007dSdan       }else{
19036734007dSdan         *pRc = SQLITE_NOMEM;
19046734007dSdan       }
19054fccf43aSdan     }
19064fccf43aSdan   }
19074fccf43aSdan }
19084fccf43aSdan 
1909296c7658Sdan /*
1910296c7658Sdan **
191180fe2d93Sdan ** This function appends an update change to the buffer (see the comments
191280fe2d93Sdan ** under "CHANGESET FORMAT" at the top of the file). An update change
191380fe2d93Sdan ** consists of:
1914296c7658Sdan **
1915296c7658Sdan **   1 byte:  SQLITE_UPDATE (0x17)
1916296c7658Sdan **   n bytes: old.* record (see RECORD FORMAT)
1917296c7658Sdan **   m bytes: new.* record (see RECORD FORMAT)
1918296c7658Sdan **
1919296c7658Sdan ** The SessionChange object passed as the third argument contains the
1920296c7658Sdan ** values that were stored in the row when the session began (the old.*
1921296c7658Sdan ** values). The statement handle passed as the second argument points
1922296c7658Sdan ** at the current version of the row (the new.* values).
1923296c7658Sdan **
1924296c7658Sdan ** If all of the old.* values are equal to their corresponding new.* value
1925296c7658Sdan ** (i.e. nothing has changed), then no data at all is appended to the buffer.
1926296c7658Sdan **
1927296c7658Sdan ** Otherwise, the old.* record contains all primary key values and the
1928296c7658Sdan ** original values of any fields that have been modified. The new.* record
1929296c7658Sdan ** contains the new values of only those fields that have been modified.
1930296c7658Sdan */
193180fe2d93Sdan static int sessionAppendUpdate(
1932296c7658Sdan   SessionBuffer *pBuf,            /* Buffer to append to */
193373b3c055Sdan   int bPatchset,                  /* True for "patchset", 0 for "changeset" */
1934296c7658Sdan   sqlite3_stmt *pStmt,            /* Statement handle pointing at new row */
1935296c7658Sdan   SessionChange *p,               /* Object containing old values */
193680fe2d93Sdan   u8 *abPK                        /* Boolean array - true for PK columns */
19374fccf43aSdan ){
193880fe2d93Sdan   int rc = SQLITE_OK;
1939296c7658Sdan   SessionBuffer buf2 = {0,0,0}; /* Buffer to accumulate new.* record in */
1940296c7658Sdan   int bNoop = 1;                /* Set to zero if any values are modified */
19411f34f8ccSdan   int nRewind = pBuf->nBuf;     /* Set to zero if any values are modified */
1942296c7658Sdan   int i;                        /* Used to iterate through columns */
1943296c7658Sdan   u8 *pCsr = p->aRecord;        /* Used to iterate through old.* values */
1944296c7658Sdan 
194580fe2d93Sdan   sessionAppendByte(pBuf, SQLITE_UPDATE, &rc);
194680fe2d93Sdan   sessionAppendByte(pBuf, p->bIndirect, &rc);
19474fccf43aSdan   for(i=0; i<sqlite3_column_count(pStmt); i++){
194837f133ecSdan     int bChanged = 0;
19494fccf43aSdan     int nAdvance;
19504fccf43aSdan     int eType = *pCsr;
19514fccf43aSdan     switch( eType ){
19524fccf43aSdan       case SQLITE_NULL:
19534fccf43aSdan         nAdvance = 1;
19544fccf43aSdan         if( sqlite3_column_type(pStmt, i)!=SQLITE_NULL ){
195537f133ecSdan           bChanged = 1;
19564fccf43aSdan         }
19574fccf43aSdan         break;
19584fccf43aSdan 
19594fccf43aSdan       case SQLITE_FLOAT:
19604fccf43aSdan       case SQLITE_INTEGER: {
19614fccf43aSdan         nAdvance = 9;
19624fccf43aSdan         if( eType==sqlite3_column_type(pStmt, i) ){
19634fccf43aSdan           sqlite3_int64 iVal = sessionGetI64(&pCsr[1]);
19644fccf43aSdan           if( eType==SQLITE_INTEGER ){
19654fccf43aSdan             if( iVal==sqlite3_column_int64(pStmt, i) ) break;
19664fccf43aSdan           }else{
19674fccf43aSdan             double dVal;
19684fccf43aSdan             memcpy(&dVal, &iVal, 8);
19694fccf43aSdan             if( dVal==sqlite3_column_double(pStmt, i) ) break;
19704fccf43aSdan           }
19714fccf43aSdan         }
197237f133ecSdan         bChanged = 1;
19734fccf43aSdan         break;
19744fccf43aSdan       }
19754fccf43aSdan 
1976e5754eecSdan       default: {
19774fccf43aSdan         int nByte;
19784fccf43aSdan         int nHdr = 1 + sessionVarintGet(&pCsr[1], &nByte);
1979e5754eecSdan         assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
19804fccf43aSdan         nAdvance = nHdr + nByte;
19814fccf43aSdan         if( eType==sqlite3_column_type(pStmt, i)
19824fccf43aSdan          && nByte==sqlite3_column_bytes(pStmt, i)
19834fccf43aSdan          && 0==memcmp(&pCsr[nHdr], sqlite3_column_blob(pStmt, i), nByte)
19844fccf43aSdan         ){
19854fccf43aSdan           break;
19864fccf43aSdan         }
198737f133ecSdan         bChanged = 1;
19884fccf43aSdan       }
19894fccf43aSdan     }
19904fccf43aSdan 
199173b3c055Sdan     /* If at least one field has been modified, this is not a no-op. */
199273b3c055Sdan     if( bChanged ) bNoop = 0;
199373b3c055Sdan 
199473b3c055Sdan     /* Add a field to the old.* record. This is omitted if this modules is
199573b3c055Sdan     ** currently generating a patchset. */
199673b3c055Sdan     if( bPatchset==0 ){
199737f133ecSdan       if( bChanged || abPK[i] ){
199880fe2d93Sdan         sessionAppendBlob(pBuf, pCsr, nAdvance, &rc);
19994fccf43aSdan       }else{
200080fe2d93Sdan         sessionAppendByte(pBuf, 0, &rc);
200137f133ecSdan       }
200273b3c055Sdan     }
200337f133ecSdan 
200473b3c055Sdan     /* Add a field to the new.* record. Or the only record if currently
200573b3c055Sdan     ** generating a patchset.  */
200673b3c055Sdan     if( bChanged || (bPatchset && abPK[i]) ){
200780fe2d93Sdan       sessionAppendCol(&buf2, pStmt, i, &rc);
200837f133ecSdan     }else{
200980fe2d93Sdan       sessionAppendByte(&buf2, 0, &rc);
20104fccf43aSdan     }
201137f133ecSdan 
20124fccf43aSdan     pCsr += nAdvance;
20134fccf43aSdan   }
20144fccf43aSdan 
20154fccf43aSdan   if( bNoop ){
20161f34f8ccSdan     pBuf->nBuf = nRewind;
20174fccf43aSdan   }else{
201880fe2d93Sdan     sessionAppendBlob(pBuf, buf2.aBuf, buf2.nBuf, &rc);
20194fccf43aSdan   }
20201f34f8ccSdan   sqlite3_free(buf2.aBuf);
202180fe2d93Sdan 
202280fe2d93Sdan   return rc;
2023d5f0767cSdan }
20244fccf43aSdan 
2025a71d2371Sdan /*
2026a71d2371Sdan ** Append a DELETE change to the buffer passed as the first argument. Use
2027a71d2371Sdan ** the changeset format if argument bPatchset is zero, or the patchset
2028a71d2371Sdan ** format otherwise.
2029a71d2371Sdan */
203073b3c055Sdan static int sessionAppendDelete(
203173b3c055Sdan   SessionBuffer *pBuf,            /* Buffer to append to */
203273b3c055Sdan   int bPatchset,                  /* True for "patchset", 0 for "changeset" */
203373b3c055Sdan   SessionChange *p,               /* Object containing old values */
2034a71d2371Sdan   int nCol,                       /* Number of columns in table */
203573b3c055Sdan   u8 *abPK                        /* Boolean array - true for PK columns */
203673b3c055Sdan ){
203773b3c055Sdan   int rc = SQLITE_OK;
203873b3c055Sdan 
203973b3c055Sdan   sessionAppendByte(pBuf, SQLITE_DELETE, &rc);
204073b3c055Sdan   sessionAppendByte(pBuf, p->bIndirect, &rc);
204173b3c055Sdan 
204273b3c055Sdan   if( bPatchset==0 ){
204373b3c055Sdan     sessionAppendBlob(pBuf, p->aRecord, p->nRecord, &rc);
204473b3c055Sdan   }else{
204573b3c055Sdan     int i;
204673b3c055Sdan     u8 *a = p->aRecord;
204773b3c055Sdan     for(i=0; i<nCol; i++){
204873b3c055Sdan       u8 *pStart = a;
204973b3c055Sdan       int eType = *a++;
205073b3c055Sdan 
205173b3c055Sdan       switch( eType ){
205273b3c055Sdan         case 0:
205373b3c055Sdan         case SQLITE_NULL:
205473b3c055Sdan           assert( abPK[i]==0 );
205573b3c055Sdan           break;
205673b3c055Sdan 
205773b3c055Sdan         case SQLITE_FLOAT:
205873b3c055Sdan         case SQLITE_INTEGER:
205973b3c055Sdan           a += 8;
206073b3c055Sdan           break;
206173b3c055Sdan 
206273b3c055Sdan         default: {
206373b3c055Sdan           int n;
206473b3c055Sdan           a += sessionVarintGet(a, &n);
206573b3c055Sdan           a += n;
206673b3c055Sdan           break;
206773b3c055Sdan         }
206873b3c055Sdan       }
206973b3c055Sdan       if( abPK[i] ){
2070f5ab08c7Sdrh         sessionAppendBlob(pBuf, pStart, (int)(a-pStart), &rc);
207173b3c055Sdan       }
207273b3c055Sdan     }
207373b3c055Sdan     assert( (a - p->aRecord)==p->nRecord );
207473b3c055Sdan   }
207573b3c055Sdan 
207673b3c055Sdan   return rc;
207773b3c055Sdan }
207873b3c055Sdan 
207977fc1d5bSdan /*
208077fc1d5bSdan ** Formulate and prepare a SELECT statement to retrieve a row from table
208177fc1d5bSdan ** zTab in database zDb based on its primary key. i.e.
208277fc1d5bSdan **
208377fc1d5bSdan **   SELECT * FROM zDb.zTab WHERE pk1 = ? AND pk2 = ? AND ...
208477fc1d5bSdan */
2085e8d5648eSdan static int sessionSelectStmt(
2086e8d5648eSdan   sqlite3 *db,                    /* Database handle */
2087d7fb7d24Sdan   const char *zDb,                /* Database name */
2088e8d5648eSdan   const char *zTab,               /* Table name */
208977fc1d5bSdan   int nCol,                       /* Number of columns in table */
209077fc1d5bSdan   const char **azCol,             /* Names of table columns */
209177fc1d5bSdan   u8 *abPK,                       /* PRIMARY KEY  array */
209277fc1d5bSdan   sqlite3_stmt **ppStmt           /* OUT: Prepared SELECT statement */
2093d5f0767cSdan ){
2094e8d5648eSdan   int rc = SQLITE_OK;
2095d5f0767cSdan   int i;
2096e8d5648eSdan   const char *zSep = "";
2097e8d5648eSdan   SessionBuffer buf = {0, 0, 0};
2098d5f0767cSdan 
2099e8d5648eSdan   sessionAppendStr(&buf, "SELECT * FROM ", &rc);
2100d7fb7d24Sdan   sessionAppendIdent(&buf, zDb, &rc);
2101d7fb7d24Sdan   sessionAppendStr(&buf, ".", &rc);
2102e8d5648eSdan   sessionAppendIdent(&buf, zTab, &rc);
2103e8d5648eSdan   sessionAppendStr(&buf, " WHERE ", &rc);
2104e8d5648eSdan   for(i=0; i<nCol; i++){
2105e8d5648eSdan     if( abPK[i] ){
2106e8d5648eSdan       sessionAppendStr(&buf, zSep, &rc);
2107e8d5648eSdan       sessionAppendIdent(&buf, azCol[i], &rc);
2108e8d5648eSdan       sessionAppendStr(&buf, " = ?", &rc);
2109e8d5648eSdan       sessionAppendInteger(&buf, i+1, &rc);
2110e8d5648eSdan       zSep = " AND ";
2111d5f0767cSdan     }
2112d5f0767cSdan   }
2113d5f0767cSdan   if( rc==SQLITE_OK ){
2114e8d5648eSdan     rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, ppStmt, 0);
2115d5f0767cSdan   }
2116e8d5648eSdan   sqlite3_free(buf.aBuf);
2117e8d5648eSdan   return rc;
2118d5f0767cSdan }
2119d5f0767cSdan 
212077fc1d5bSdan /*
212177fc1d5bSdan ** Bind the PRIMARY KEY values from the change passed in argument pChange
212277fc1d5bSdan ** to the SELECT statement passed as the first argument. The SELECT statement
212377fc1d5bSdan ** is as prepared by function sessionSelectStmt().
212477fc1d5bSdan **
212577fc1d5bSdan ** Return SQLITE_OK if all PK values are successfully bound, or an SQLite
212677fc1d5bSdan ** error code (e.g. SQLITE_NOMEM) otherwise.
212777fc1d5bSdan */
2128e8d5648eSdan static int sessionSelectBind(
212977fc1d5bSdan   sqlite3_stmt *pSelect,          /* SELECT from sessionSelectStmt() */
213077fc1d5bSdan   int nCol,                       /* Number of columns in table */
213177fc1d5bSdan   u8 *abPK,                       /* PRIMARY KEY array */
213277fc1d5bSdan   SessionChange *pChange          /* Change structure */
2133e8d5648eSdan ){
2134e8d5648eSdan   int i;
2135e8d5648eSdan   int rc = SQLITE_OK;
2136e5754eecSdan   u8 *a = pChange->aRecord;
2137d5f0767cSdan 
2138e8d5648eSdan   for(i=0; i<nCol && rc==SQLITE_OK; i++){
2139e8d5648eSdan     int eType = *a++;
2140e8d5648eSdan 
2141e8d5648eSdan     switch( eType ){
214280fe2d93Sdan       case 0:
2143e8d5648eSdan       case SQLITE_NULL:
2144e5754eecSdan         assert( abPK[i]==0 );
2145e8d5648eSdan         break;
2146e8d5648eSdan 
2147e8d5648eSdan       case SQLITE_INTEGER: {
2148e8d5648eSdan         if( abPK[i] ){
2149e8d5648eSdan           i64 iVal = sessionGetI64(a);
2150e8d5648eSdan           rc = sqlite3_bind_int64(pSelect, i+1, iVal);
2151e8d5648eSdan         }
2152e8d5648eSdan         a += 8;
2153e8d5648eSdan         break;
2154d5f0767cSdan       }
2155296c7658Sdan 
2156e8d5648eSdan       case SQLITE_FLOAT: {
2157e8d5648eSdan         if( abPK[i] ){
2158e8d5648eSdan           double rVal;
2159e8d5648eSdan           i64 iVal = sessionGetI64(a);
2160e8d5648eSdan           memcpy(&rVal, &iVal, 8);
21614e895da1Sdan           rc = sqlite3_bind_double(pSelect, i+1, rVal);
2162d5f0767cSdan         }
2163e8d5648eSdan         a += 8;
2164e8d5648eSdan         break;
2165e8d5648eSdan       }
2166e8d5648eSdan 
2167e8d5648eSdan       case SQLITE_TEXT: {
2168e8d5648eSdan         int n;
2169e8d5648eSdan         a += sessionVarintGet(a, &n);
2170e8d5648eSdan         if( abPK[i] ){
2171e8d5648eSdan           rc = sqlite3_bind_text(pSelect, i+1, (char *)a, n, SQLITE_TRANSIENT);
2172e8d5648eSdan         }
2173e8d5648eSdan         a += n;
2174e8d5648eSdan         break;
2175e8d5648eSdan       }
2176e8d5648eSdan 
2177e5754eecSdan       default: {
2178e8d5648eSdan         int n;
2179e5754eecSdan         assert( eType==SQLITE_BLOB );
2180e8d5648eSdan         a += sessionVarintGet(a, &n);
2181e8d5648eSdan         if( abPK[i] ){
2182e8d5648eSdan           rc = sqlite3_bind_blob(pSelect, i+1, a, n, SQLITE_TRANSIENT);
2183e8d5648eSdan         }
2184e8d5648eSdan         a += n;
2185e8d5648eSdan         break;
2186e8d5648eSdan       }
2187e8d5648eSdan     }
2188e8d5648eSdan   }
2189e8d5648eSdan 
2190d5f0767cSdan   return rc;
21914fccf43aSdan }
21924fccf43aSdan 
219377fc1d5bSdan /*
219477fc1d5bSdan ** This function is a no-op if *pRc is set to other than SQLITE_OK when it
219577fc1d5bSdan ** is called. Otherwise, append a serialized table header (part of the binary
219677fc1d5bSdan ** changeset format) to buffer *pBuf. If an error occurs, set *pRc to an
219777fc1d5bSdan ** SQLite error code before returning.
219877fc1d5bSdan */
21995d607a6eSdan static void sessionAppendTableHdr(
2200a71d2371Sdan   SessionBuffer *pBuf,            /* Append header to this buffer */
2201a71d2371Sdan   int bPatchset,                  /* Use the patchset format if true */
2202a71d2371Sdan   SessionTable *pTab,             /* Table object to append header for */
2203a71d2371Sdan   int *pRc                        /* IN/OUT: Error code */
22045d607a6eSdan ){
22055d607a6eSdan   /* Write a table header */
220673b3c055Sdan   sessionAppendByte(pBuf, (bPatchset ? 'P' : 'T'), pRc);
22075d607a6eSdan   sessionAppendVarint(pBuf, pTab->nCol, pRc);
22085d607a6eSdan   sessionAppendBlob(pBuf, pTab->abPK, pTab->nCol, pRc);
22094f528042Sdan   sessionAppendBlob(pBuf, (u8 *)pTab->zName, (int)strlen(pTab->zName)+1, pRc);
22105d607a6eSdan }
22115d607a6eSdan 
2212a71d2371Sdan /*
2213a71d2371Sdan ** Generate either a changeset (if argument bPatchset is zero) or a patchset
2214a71d2371Sdan ** (if it is non-zero) based on the current contents of the session object
2215a71d2371Sdan ** passed as the first argument.
2216a71d2371Sdan **
2217a71d2371Sdan ** If no error occurs, SQLITE_OK is returned and the new changeset/patchset
2218a71d2371Sdan ** stored in output variables *pnChangeset and *ppChangeset. Or, if an error
2219a71d2371Sdan ** occurs, an SQLite error code is returned and both output variables set
2220a71d2371Sdan ** to 0.
2221a71d2371Sdan */
2222adf3bf58Sdrh static int sessionGenerateChangeset(
22234fccf43aSdan   sqlite3_session *pSession,      /* Session object */
222473b3c055Sdan   int bPatchset,                  /* True for patchset, false for changeset */
2225ef7a6304Sdan   int (*xOutput)(void *pOut, const void *pData, int nData),
2226ef7a6304Sdan   void *pOut,                     /* First argument for xOutput */
22274fccf43aSdan   int *pnChangeset,               /* OUT: Size of buffer at *ppChangeset */
22284fccf43aSdan   void **ppChangeset              /* OUT: Buffer containing changeset */
22294fccf43aSdan ){
2230296c7658Sdan   sqlite3 *db = pSession->db;     /* Source database handle */
2231296c7658Sdan   SessionTable *pTab;             /* Used to iterate through attached tables */
2232296c7658Sdan   SessionBuffer buf = {0,0,0};    /* Buffer in which to accumlate changeset */
2233296c7658Sdan   int rc;                         /* Return code */
22344fccf43aSdan 
2235ef7a6304Sdan   assert( xOutput==0 || (pnChangeset==0 && ppChangeset==0 ) );
2236ef7a6304Sdan 
2237296c7658Sdan   /* Zero the output variables in case an error occurs. If this session
2238296c7658Sdan   ** object is already in the error state (sqlite3_session.rc != SQLITE_OK),
2239296c7658Sdan   ** this call will be a no-op.  */
2240ef7a6304Sdan   if( xOutput==0 ){
22414fccf43aSdan     *pnChangeset = 0;
22424fccf43aSdan     *ppChangeset = 0;
2243ef7a6304Sdan   }
2244e5754eecSdan 
2245e5754eecSdan   if( pSession->rc ) return pSession->rc;
2246e5754eecSdan   rc = sqlite3_exec(pSession->db, "SAVEPOINT changeset", 0, 0, 0);
2247e5754eecSdan   if( rc!=SQLITE_OK ) return rc;
2248e5754eecSdan 
2249e5754eecSdan   sqlite3_mutex_enter(sqlite3_db_mutex(db));
22504fccf43aSdan 
22514fccf43aSdan   for(pTab=pSession->pTable; rc==SQLITE_OK && pTab; pTab=pTab->pNext){
22524fccf43aSdan     if( pTab->nEntry ){
2253d7fb7d24Sdan       const char *zName = pTab->zName;
2254a9605b91Sdan       int nCol;                   /* Number of columns in table */
2255a9605b91Sdan       u8 *abPK;                   /* Primary key array */
2256a9605b91Sdan       const char **azCol = 0;     /* Table columns */
22571f34f8ccSdan       int i;                      /* Used to iterate through hash buckets */
22581f34f8ccSdan       sqlite3_stmt *pSel = 0;     /* SELECT statement to query table pTab */
22591f34f8ccSdan       int nRewind = buf.nBuf;     /* Initial size of write buffer */
22601f34f8ccSdan       int nNoop;                  /* Size of buffer after writing tbl header */
22614fccf43aSdan 
2262a9605b91Sdan       /* Check the table schema is still Ok. */
2263a9605b91Sdan       rc = sessionTableInfo(db, pSession->zDb, zName, &nCol, 0, &azCol, &abPK);
2264a9605b91Sdan       if( !rc && (pTab->nCol!=nCol || memcmp(abPK, pTab->abPK, nCol)) ){
2265a9605b91Sdan         rc = SQLITE_SCHEMA;
2266a9605b91Sdan       }
2267a9605b91Sdan 
22684fccf43aSdan       /* Write a table header */
226973b3c055Sdan       sessionAppendTableHdr(&buf, bPatchset, pTab, &rc);
22704fccf43aSdan 
22714fccf43aSdan       /* Build and compile a statement to execute: */
22724fccf43aSdan       if( rc==SQLITE_OK ){
2273d7fb7d24Sdan         rc = sessionSelectStmt(
2274a9605b91Sdan             db, pSession->zDb, zName, nCol, azCol, abPK, &pSel);
22754fccf43aSdan       }
22764fccf43aSdan 
22771f34f8ccSdan       nNoop = buf.nBuf;
227812ca0b56Sdan       for(i=0; i<pTab->nChange && rc==SQLITE_OK; i++){
2279e8d5648eSdan         SessionChange *p;         /* Used to iterate through changes */
2280e8d5648eSdan 
22814fccf43aSdan         for(p=pTab->apChange[i]; rc==SQLITE_OK && p; p=p->pNext){
2282e5754eecSdan           rc = sessionSelectBind(pSel, nCol, abPK, p);
228380fe2d93Sdan           if( rc!=SQLITE_OK ) continue;
22841f34f8ccSdan           if( sqlite3_step(pSel)==SQLITE_ROW ){
2285798693b2Sdan             if( p->op==SQLITE_INSERT ){
22864fccf43aSdan               int iCol;
22874fccf43aSdan               sessionAppendByte(&buf, SQLITE_INSERT, &rc);
2288b4480e94Sdan               sessionAppendByte(&buf, p->bIndirect, &rc);
2289e8d5648eSdan               for(iCol=0; iCol<nCol; iCol++){
22901f34f8ccSdan                 sessionAppendCol(&buf, pSel, iCol, &rc);
22914fccf43aSdan               }
2292e8d5648eSdan             }else{
229373b3c055Sdan               rc = sessionAppendUpdate(&buf, bPatchset, pSel, p, abPK);
22944fccf43aSdan             }
2295798693b2Sdan           }else if( p->op!=SQLITE_INSERT ){
2296a71d2371Sdan             rc = sessionAppendDelete(&buf, bPatchset, p, nCol, abPK);
22974fccf43aSdan           }
229812ca0b56Sdan           if( rc==SQLITE_OK ){
22991f34f8ccSdan             rc = sqlite3_reset(pSel);
23004fccf43aSdan           }
2301ef7a6304Sdan 
2302f1a08ad8Sdrh           /* If the buffer is now larger than SESSIONS_STRM_CHUNK_SIZE, pass
2303ef7a6304Sdan           ** its contents to the xOutput() callback. */
2304ef7a6304Sdan           if( xOutput
2305ef7a6304Sdan            && rc==SQLITE_OK
2306ef7a6304Sdan            && buf.nBuf>nNoop
2307f1a08ad8Sdrh            && buf.nBuf>SESSIONS_STRM_CHUNK_SIZE
2308ef7a6304Sdan           ){
2309ef7a6304Sdan             rc = xOutput(pOut, (void*)buf.aBuf, buf.nBuf);
2310ef7a6304Sdan             nNoop = -1;
2311ef7a6304Sdan             buf.nBuf = 0;
2312ef7a6304Sdan           }
2313ef7a6304Sdan 
23144fccf43aSdan         }
2315e8d5648eSdan       }
23164fccf43aSdan 
23171f34f8ccSdan       sqlite3_finalize(pSel);
23181f34f8ccSdan       if( buf.nBuf==nNoop ){
23194fccf43aSdan         buf.nBuf = nRewind;
23204fccf43aSdan       }
2321cfdbde21Sdrh       sqlite3_free((char*)azCol);  /* cast works around VC++ bug */
23224fccf43aSdan     }
23234fccf43aSdan   }
23244fccf43aSdan 
23254fccf43aSdan   if( rc==SQLITE_OK ){
2326ef7a6304Sdan     if( xOutput==0 ){
23274fccf43aSdan       *pnChangeset = buf.nBuf;
23284fccf43aSdan       *ppChangeset = buf.aBuf;
2329ef7a6304Sdan       buf.aBuf = 0;
2330ef7a6304Sdan     }else if( buf.nBuf>0 ){
2331ef7a6304Sdan       rc = xOutput(pOut, (void*)buf.aBuf, buf.nBuf);
2332ef7a6304Sdan     }
23334fccf43aSdan   }
23344c220252Sdan 
2335ef7a6304Sdan   sqlite3_free(buf.aBuf);
2336e5754eecSdan   sqlite3_exec(db, "RELEASE changeset", 0, 0, 0);
23374c220252Sdan   sqlite3_mutex_leave(sqlite3_db_mutex(db));
23384fccf43aSdan   return rc;
23394fccf43aSdan }
23404fccf43aSdan 
2341296c7658Sdan /*
234273b3c055Sdan ** Obtain a changeset object containing all changes recorded by the
234373b3c055Sdan ** session object passed as the first argument.
234473b3c055Sdan **
234573b3c055Sdan ** It is the responsibility of the caller to eventually free the buffer
234673b3c055Sdan ** using sqlite3_free().
234773b3c055Sdan */
234873b3c055Sdan int sqlite3session_changeset(
234973b3c055Sdan   sqlite3_session *pSession,      /* Session object */
235073b3c055Sdan   int *pnChangeset,               /* OUT: Size of buffer at *ppChangeset */
235173b3c055Sdan   void **ppChangeset              /* OUT: Buffer containing changeset */
235273b3c055Sdan ){
2353ef7a6304Sdan   return sessionGenerateChangeset(pSession, 0, 0, 0, pnChangeset, ppChangeset);
2354ef7a6304Sdan }
2355ef7a6304Sdan 
2356ef7a6304Sdan /*
2357ef7a6304Sdan ** Streaming version of sqlite3session_changeset().
2358ef7a6304Sdan */
2359f1a08ad8Sdrh int sqlite3session_changeset_strm(
2360ef7a6304Sdan   sqlite3_session *pSession,
2361ef7a6304Sdan   int (*xOutput)(void *pOut, const void *pData, int nData),
2362ef7a6304Sdan   void *pOut
2363ef7a6304Sdan ){
2364ef7a6304Sdan   return sessionGenerateChangeset(pSession, 0, xOutput, pOut, 0, 0);
2365ef7a6304Sdan }
2366ef7a6304Sdan 
2367ef7a6304Sdan /*
2368ef7a6304Sdan ** Streaming version of sqlite3session_patchset().
2369ef7a6304Sdan */
2370f1a08ad8Sdrh int sqlite3session_patchset_strm(
2371ef7a6304Sdan   sqlite3_session *pSession,
2372ef7a6304Sdan   int (*xOutput)(void *pOut, const void *pData, int nData),
2373ef7a6304Sdan   void *pOut
2374ef7a6304Sdan ){
2375ef7a6304Sdan   return sessionGenerateChangeset(pSession, 1, xOutput, pOut, 0, 0);
237673b3c055Sdan }
237773b3c055Sdan 
237873b3c055Sdan /*
237973b3c055Sdan ** Obtain a patchset object containing all changes recorded by the
238073b3c055Sdan ** session object passed as the first argument.
238173b3c055Sdan **
238273b3c055Sdan ** It is the responsibility of the caller to eventually free the buffer
238373b3c055Sdan ** using sqlite3_free().
238473b3c055Sdan */
238573b3c055Sdan int sqlite3session_patchset(
238673b3c055Sdan   sqlite3_session *pSession,      /* Session object */
238773b3c055Sdan   int *pnPatchset,                /* OUT: Size of buffer at *ppChangeset */
238873b3c055Sdan   void **ppPatchset               /* OUT: Buffer containing changeset */
238973b3c055Sdan ){
2390ef7a6304Sdan   return sessionGenerateChangeset(pSession, 1, 0, 0, pnPatchset, ppPatchset);
239173b3c055Sdan }
239273b3c055Sdan 
239373b3c055Sdan /*
2394296c7658Sdan ** Enable or disable the session object passed as the first argument.
2395296c7658Sdan */
23964fccf43aSdan int sqlite3session_enable(sqlite3_session *pSession, int bEnable){
23974c220252Sdan   int ret;
23984c220252Sdan   sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
2399296c7658Sdan   if( bEnable>=0 ){
2400296c7658Sdan     pSession->bEnable = bEnable;
24014fccf43aSdan   }
24024c220252Sdan   ret = pSession->bEnable;
24034c220252Sdan   sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
24044c220252Sdan   return ret;
2405296c7658Sdan }
24064fccf43aSdan 
24074fccf43aSdan /*
2408b4480e94Sdan ** Enable or disable the session object passed as the first argument.
2409b4480e94Sdan */
2410b4480e94Sdan int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect){
2411b4480e94Sdan   int ret;
2412b4480e94Sdan   sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
2413b4480e94Sdan   if( bIndirect>=0 ){
2414b4480e94Sdan     pSession->bIndirect = bIndirect;
2415b4480e94Sdan   }
2416b4480e94Sdan   ret = pSession->bIndirect;
2417b4480e94Sdan   sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
2418b4480e94Sdan   return ret;
2419b4480e94Sdan }
2420b4480e94Sdan 
2421b4480e94Sdan /*
2422b69ec348Sdan ** Return true if there have been no changes to monitored tables recorded
2423b69ec348Sdan ** by the session object passed as the only argument.
2424b69ec348Sdan */
2425b69ec348Sdan int sqlite3session_isempty(sqlite3_session *pSession){
2426b69ec348Sdan   int ret = 0;
2427b69ec348Sdan   SessionTable *pTab;
2428b69ec348Sdan 
2429b69ec348Sdan   sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
2430b69ec348Sdan   for(pTab=pSession->pTable; pTab && ret==0; pTab=pTab->pNext){
2431b69ec348Sdan     ret = (pTab->nEntry>0);
2432b69ec348Sdan   }
2433b69ec348Sdan   sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
2434b69ec348Sdan 
2435ff530326Sdan   return (ret==0);
2436b69ec348Sdan }
2437b69ec348Sdan 
2438b69ec348Sdan /*
2439f1a08ad8Sdrh ** Do the work for either sqlite3changeset_start() or start_strm().
24404fccf43aSdan */
2441adf3bf58Sdrh static int sessionChangesetStart(
2442296c7658Sdan   sqlite3_changeset_iter **pp,    /* OUT: Changeset iterator handle */
2443ef7a6304Sdan   int (*xInput)(void *pIn, void *pData, int *pnData),
2444ef7a6304Sdan   void *pIn,
2445296c7658Sdan   int nChangeset,                 /* Size of buffer pChangeset in bytes */
2446296c7658Sdan   void *pChangeset                /* Pointer to buffer containing changeset */
24474fccf43aSdan ){
24484fccf43aSdan   sqlite3_changeset_iter *pRet;   /* Iterator to return */
24494fccf43aSdan   int nByte;                      /* Number of bytes to allocate for iterator */
24504fccf43aSdan 
2451ef7a6304Sdan   assert( xInput==0 || (pChangeset==0 && nChangeset==0) );
2452ef7a6304Sdan 
2453296c7658Sdan   /* Zero the output variable in case an error occurs. */
2454296c7658Sdan   *pp = 0;
24554fccf43aSdan 
2456296c7658Sdan   /* Allocate and initialize the iterator structure. */
24574fccf43aSdan   nByte = sizeof(sqlite3_changeset_iter);
24584fccf43aSdan   pRet = (sqlite3_changeset_iter *)sqlite3_malloc(nByte);
24594fccf43aSdan   if( !pRet ) return SQLITE_NOMEM;
24604fccf43aSdan   memset(pRet, 0, sizeof(sqlite3_changeset_iter));
24614757c658Sdan   pRet->in.aData = (u8 *)pChangeset;
24624757c658Sdan   pRet->in.nData = nChangeset;
2463ef7a6304Sdan   pRet->in.xInput = xInput;
2464ef7a6304Sdan   pRet->in.pIn = pIn;
2465ef7a6304Sdan   pRet->in.bEof = (xInput ? 0 : 1);
24664fccf43aSdan 
2467296c7658Sdan   /* Populate the output variable and return success. */
2468296c7658Sdan   *pp = pRet;
24694fccf43aSdan   return SQLITE_OK;
24704fccf43aSdan }
24714fccf43aSdan 
2472296c7658Sdan /*
2473ef7a6304Sdan ** Create an iterator used to iterate through the contents of a changeset.
2474ef7a6304Sdan */
2475ef7a6304Sdan int sqlite3changeset_start(
2476ef7a6304Sdan   sqlite3_changeset_iter **pp,    /* OUT: Changeset iterator handle */
2477ef7a6304Sdan   int nChangeset,                 /* Size of buffer pChangeset in bytes */
2478ef7a6304Sdan   void *pChangeset                /* Pointer to buffer containing changeset */
2479ef7a6304Sdan ){
2480ef7a6304Sdan   return sessionChangesetStart(pp, 0, 0, nChangeset, pChangeset);
2481ef7a6304Sdan }
2482ef7a6304Sdan 
2483ef7a6304Sdan /*
2484ef7a6304Sdan ** Streaming version of sqlite3changeset_start().
2485ef7a6304Sdan */
2486f1a08ad8Sdrh int sqlite3changeset_start_strm(
2487ef7a6304Sdan   sqlite3_changeset_iter **pp,    /* OUT: Changeset iterator handle */
2488ef7a6304Sdan   int (*xInput)(void *pIn, void *pData, int *pnData),
2489ef7a6304Sdan   void *pIn
2490ef7a6304Sdan ){
2491ef7a6304Sdan   return sessionChangesetStart(pp, xInput, pIn, 0, 0);
2492ef7a6304Sdan }
2493ef7a6304Sdan 
2494ef7a6304Sdan /*
2495d9151526Sdan ** If the SessionInput object passed as the only argument is a streaming
2496d9151526Sdan ** object and the buffer is full, discard some data to free up space.
2497d9151526Sdan */
2498d9151526Sdan static void sessionDiscardData(SessionInput *pIn){
2499d9151526Sdan   if( pIn->bEof && pIn->xInput && pIn->iNext>=SESSIONS_STRM_CHUNK_SIZE ){
2500d9151526Sdan     int nMove = pIn->buf.nBuf - pIn->iNext;
2501d9151526Sdan     assert( nMove>=0 );
2502d9151526Sdan     if( nMove>0 ){
2503d9151526Sdan       memmove(pIn->buf.aBuf, &pIn->buf.aBuf[pIn->iNext], nMove);
2504d9151526Sdan     }
2505d9151526Sdan     pIn->buf.nBuf -= pIn->iNext;
2506d9151526Sdan     pIn->iNext = 0;
2507d9151526Sdan     pIn->nData = pIn->buf.nBuf;
2508d9151526Sdan   }
2509d9151526Sdan }
2510d9151526Sdan 
2511d9151526Sdan /*
2512ef7a6304Sdan ** Ensure that there are at least nByte bytes available in the buffer. Or,
2513ef7a6304Sdan ** if there are not nByte bytes remaining in the input, that all available
2514ef7a6304Sdan ** data is in the buffer.
2515ef7a6304Sdan **
2516ef7a6304Sdan ** Return an SQLite error code if an error occurs, or SQLITE_OK otherwise.
2517ef7a6304Sdan */
25184757c658Sdan static int sessionInputBuffer(SessionInput *pIn, int nByte){
2519ef7a6304Sdan   int rc = SQLITE_OK;
25204757c658Sdan   if( pIn->xInput ){
25214757c658Sdan     while( !pIn->bEof && (pIn->iNext+nByte)>=pIn->nData && rc==SQLITE_OK ){
2522f1a08ad8Sdrh       int nNew = SESSIONS_STRM_CHUNK_SIZE;
25234757c658Sdan 
2524d9151526Sdan       if( pIn->bNoDiscard==0 ) sessionDiscardData(pIn);
25254757c658Sdan       if( SQLITE_OK==sessionBufferGrow(&pIn->buf, nNew, &rc) ){
25264757c658Sdan         rc = pIn->xInput(pIn->pIn, &pIn->buf.aBuf[pIn->buf.nBuf], &nNew);
25274757c658Sdan         if( nNew==0 ){
25284757c658Sdan           pIn->bEof = 1;
25294757c658Sdan         }else{
25304757c658Sdan           pIn->buf.nBuf += nNew;
25314757c658Sdan         }
25324757c658Sdan       }
25334757c658Sdan 
25344757c658Sdan       pIn->aData = pIn->buf.aBuf;
25354757c658Sdan       pIn->nData = pIn->buf.nBuf;
25364757c658Sdan     }
2537ef7a6304Sdan   }
2538ef7a6304Sdan   return rc;
2539ef7a6304Sdan }
2540ef7a6304Sdan 
2541ef7a6304Sdan /*
2542ef7a6304Sdan ** When this function is called, *ppRec points to the start of a record
2543ef7a6304Sdan ** that contains nCol values. This function advances the pointer *ppRec
2544ef7a6304Sdan ** until it points to the byte immediately following that record.
2545ef7a6304Sdan */
2546ef7a6304Sdan static void sessionSkipRecord(
2547ef7a6304Sdan   u8 **ppRec,                     /* IN/OUT: Record pointer */
2548ef7a6304Sdan   int nCol                        /* Number of values in record */
2549ef7a6304Sdan ){
2550ef7a6304Sdan   u8 *aRec = *ppRec;
2551ef7a6304Sdan   int i;
2552ef7a6304Sdan   for(i=0; i<nCol; i++){
2553ef7a6304Sdan     int eType = *aRec++;
2554ef7a6304Sdan     if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
2555ef7a6304Sdan       int nByte;
2556ef7a6304Sdan       aRec += sessionVarintGet((u8*)aRec, &nByte);
2557ef7a6304Sdan       aRec += nByte;
2558ef7a6304Sdan     }else if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
2559ef7a6304Sdan       aRec += 8;
2560ef7a6304Sdan     }
2561ef7a6304Sdan   }
2562ef7a6304Sdan 
2563ef7a6304Sdan   *ppRec = aRec;
2564ef7a6304Sdan }
2565ef7a6304Sdan 
2566ef7a6304Sdan /*
25674757c658Sdan ** This function sets the value of the sqlite3_value object passed as the
25684757c658Sdan ** first argument to a copy of the string or blob held in the aData[]
25694757c658Sdan ** buffer. SQLITE_OK is returned if successful, or SQLITE_NOMEM if an OOM
25704757c658Sdan ** error occurs.
25714757c658Sdan */
25724757c658Sdan static int sessionValueSetStr(
25734757c658Sdan   sqlite3_value *pVal,            /* Set the value of this object */
25744757c658Sdan   u8 *aData,                      /* Buffer containing string or blob data */
25754757c658Sdan   int nData,                      /* Size of buffer aData[] in bytes */
25764757c658Sdan   u8 enc                          /* String encoding (0 for blobs) */
25774757c658Sdan ){
257816228167Sdan   /* In theory this code could just pass SQLITE_TRANSIENT as the final
257916228167Sdan   ** argument to sqlite3ValueSetStr() and have the copy created
258016228167Sdan   ** automatically. But doing so makes it difficult to detect any OOM
258116228167Sdan   ** error. Hence the code to create the copy externally. */
25823cc89d95Sdan   u8 *aCopy = sqlite3_malloc(nData+1);
25834757c658Sdan   if( aCopy==0 ) return SQLITE_NOMEM;
25844757c658Sdan   memcpy(aCopy, aData, nData);
25854757c658Sdan   sqlite3ValueSetStr(pVal, nData, (char*)aCopy, enc, sqlite3_free);
25864757c658Sdan   return SQLITE_OK;
25874757c658Sdan }
25884757c658Sdan 
25894757c658Sdan /*
2590296c7658Sdan ** Deserialize a single record from a buffer in memory. See "RECORD FORMAT"
2591296c7658Sdan ** for details.
2592296c7658Sdan **
2593296c7658Sdan ** When this function is called, *paChange points to the start of the record
2594296c7658Sdan ** to deserialize. Assuming no error occurs, *paChange is set to point to
2595296c7658Sdan ** one byte after the end of the same record before this function returns.
2596a71d2371Sdan ** If the argument abPK is NULL, then the record contains nCol values. Or,
2597a71d2371Sdan ** if abPK is other than NULL, then the record contains only the PK fields
2598a71d2371Sdan ** (in other words, it is a patchset DELETE record).
2599296c7658Sdan **
2600296c7658Sdan ** If successful, each element of the apOut[] array (allocated by the caller)
2601296c7658Sdan ** is set to point to an sqlite3_value object containing the value read
2602296c7658Sdan ** from the corresponding position in the record. If that value is not
2603296c7658Sdan ** included in the record (i.e. because the record is part of an UPDATE change
2604296c7658Sdan ** and the field was not modified), the corresponding element of apOut[] is
2605296c7658Sdan ** set to NULL.
2606296c7658Sdan **
2607296c7658Sdan ** It is the responsibility of the caller to free all sqlite_value structures
2608296c7658Sdan ** using sqlite3_free().
2609296c7658Sdan **
2610296c7658Sdan ** If an error occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned.
2611296c7658Sdan ** The apOut[] array may have been partially populated in this case.
2612296c7658Sdan */
26134fccf43aSdan static int sessionReadRecord(
2614ef7a6304Sdan   SessionInput *pIn,              /* Input data */
26154fccf43aSdan   int nCol,                       /* Number of values in record */
261673b3c055Sdan   u8 *abPK,                       /* Array of primary key flags, or NULL */
26174fccf43aSdan   sqlite3_value **apOut           /* Write values to this array */
26184fccf43aSdan ){
2619296c7658Sdan   int i;                          /* Used to iterate through columns */
2620ef7a6304Sdan   int rc = SQLITE_OK;
26214fccf43aSdan 
2622ef7a6304Sdan   for(i=0; i<nCol && rc==SQLITE_OK; i++){
2623ef7a6304Sdan     int eType = 0;                /* Type of value (SQLITE_NULL, TEXT etc.) */
262473b3c055Sdan     if( abPK && abPK[i]==0 ) continue;
2625ef7a6304Sdan     rc = sessionInputBuffer(pIn, 9);
2626ef7a6304Sdan     if( rc==SQLITE_OK ){
26274757c658Sdan       eType = pIn->aData[pIn->iNext++];
2628ef7a6304Sdan     }
2629ef7a6304Sdan 
2630e8fa8c96Sdan     assert( apOut[i]==0 );
26314fccf43aSdan     if( eType ){
26324fccf43aSdan       apOut[i] = sqlite3ValueNew(0);
2633ef7a6304Sdan       if( !apOut[i] ) rc = SQLITE_NOMEM;
2634ef7a6304Sdan     }
26354fccf43aSdan 
2636ef7a6304Sdan     if( rc==SQLITE_OK ){
26374757c658Sdan       u8 *aVal = &pIn->aData[pIn->iNext];
26384fccf43aSdan       if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
26394fccf43aSdan         int nByte;
2640ef7a6304Sdan         pIn->iNext += sessionVarintGet(aVal, &nByte);
2641ef7a6304Sdan         rc = sessionInputBuffer(pIn, nByte);
2642e8fa8c96Sdan         if( rc==SQLITE_OK ){
26436734007dSdan           u8 enc = (eType==SQLITE_TEXT ? SQLITE_UTF8 : 0);
26444757c658Sdan           rc = sessionValueSetStr(apOut[i],&pIn->aData[pIn->iNext],nByte,enc);
264591ddd559Sdan         }
2646ef7a6304Sdan         pIn->iNext += nByte;
26474fccf43aSdan       }
26484fccf43aSdan       if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
2649ef7a6304Sdan         sqlite3_int64 v = sessionGetI64(aVal);
26504fccf43aSdan         if( eType==SQLITE_INTEGER ){
26514fccf43aSdan           sqlite3VdbeMemSetInt64(apOut[i], v);
26524fccf43aSdan         }else{
26534fccf43aSdan           double d;
26544e895da1Sdan           memcpy(&d, &v, 8);
26554fccf43aSdan           sqlite3VdbeMemSetDouble(apOut[i], d);
26564fccf43aSdan         }
2657ef7a6304Sdan         pIn->iNext += 8;
265891ddd559Sdan       }
26594fccf43aSdan     }
26604fccf43aSdan   }
26614fccf43aSdan 
2662ef7a6304Sdan   return rc;
2663ef7a6304Sdan }
2664ef7a6304Sdan 
2665ef7a6304Sdan /*
2666ef7a6304Sdan ** The input pointer currently points to the second byte of a table-header.
2667ef7a6304Sdan ** Specifically, to the following:
2668ef7a6304Sdan **
2669ef7a6304Sdan **   + number of columns in table (varint)
2670ef7a6304Sdan **   + array of PK flags (1 byte per column),
2671ef7a6304Sdan **   + table name (nul terminated).
2672ef7a6304Sdan **
2673ef7a6304Sdan ** This function ensures that all of the above is present in the input
2674ef7a6304Sdan ** buffer (i.e. that it can be accessed without any calls to xInput()).
2675ef7a6304Sdan ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
2676ef7a6304Sdan ** The input pointer is not moved.
2677ef7a6304Sdan */
2678ef7a6304Sdan static int sessionChangesetBufferTblhdr(SessionInput *pIn, int *pnByte){
2679ef7a6304Sdan   int rc = SQLITE_OK;
2680ef7a6304Sdan   int nCol = 0;
26814757c658Sdan   int nRead = 0;
2682ef7a6304Sdan 
2683ef7a6304Sdan   rc = sessionInputBuffer(pIn, 9);
2684ef7a6304Sdan   if( rc==SQLITE_OK ){
26854757c658Sdan     nRead += sessionVarintGet(&pIn->aData[pIn->iNext + nRead], &nCol);
26864757c658Sdan     rc = sessionInputBuffer(pIn, nRead+nCol+100);
26874757c658Sdan     nRead += nCol;
2688ef7a6304Sdan   }
26894757c658Sdan 
2690ef7a6304Sdan   while( rc==SQLITE_OK ){
26914757c658Sdan     while( (pIn->iNext + nRead)<pIn->nData && pIn->aData[pIn->iNext + nRead] ){
26924757c658Sdan       nRead++;
2693ef7a6304Sdan     }
2694e8fa8c96Sdan     if( (pIn->iNext + nRead)<pIn->nData ) break;
26954757c658Sdan     rc = sessionInputBuffer(pIn, nRead + 100);
26964757c658Sdan   }
2697e8fa8c96Sdan   *pnByte = nRead+1;
2698ef7a6304Sdan   return rc;
2699ef7a6304Sdan }
2700ef7a6304Sdan 
2701ef7a6304Sdan /*
2702fa122adaSdan ** The input pointer currently points to the first byte of the first field
2703fa122adaSdan ** of a record consisting of nCol columns. This function ensures the entire
270416228167Sdan ** record is buffered. It does not move the input pointer.
270516228167Sdan **
270616228167Sdan ** If successful, SQLITE_OK is returned and *pnByte is set to the size of
270716228167Sdan ** the record in bytes. Otherwise, an SQLite error code is returned. The
270816228167Sdan ** final value of *pnByte is undefined in this case.
2709fa122adaSdan */
2710fa122adaSdan static int sessionChangesetBufferRecord(
271116228167Sdan   SessionInput *pIn,              /* Input data */
271216228167Sdan   int nCol,                       /* Number of columns in record */
271316228167Sdan   int *pnByte                     /* OUT: Size of record in bytes */
2714fa122adaSdan ){
2715fa122adaSdan   int rc = SQLITE_OK;
2716fa122adaSdan   int nByte = 0;
2717fa122adaSdan   int i;
2718fa122adaSdan   for(i=0; rc==SQLITE_OK && i<nCol; i++){
2719fa122adaSdan     int eType;
2720fa122adaSdan     rc = sessionInputBuffer(pIn, nByte + 10);
2721fa122adaSdan     if( rc==SQLITE_OK ){
2722fa122adaSdan       eType = pIn->aData[pIn->iNext + nByte++];
2723fa122adaSdan       if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
2724fa122adaSdan         int n;
2725fa122adaSdan         nByte += sessionVarintGet(&pIn->aData[pIn->iNext+nByte], &n);
2726fa122adaSdan         nByte += n;
2727fa122adaSdan         rc = sessionInputBuffer(pIn, nByte);
2728fa122adaSdan       }else if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
2729fa122adaSdan         nByte += 8;
2730fa122adaSdan       }
2731fa122adaSdan     }
2732fa122adaSdan   }
2733fa122adaSdan   *pnByte = nByte;
2734fa122adaSdan   return rc;
2735fa122adaSdan }
2736fa122adaSdan 
2737fa122adaSdan /*
2738ef7a6304Sdan ** The input pointer currently points to the second byte of a table-header.
2739ef7a6304Sdan ** Specifically, to the following:
2740ef7a6304Sdan **
2741ef7a6304Sdan **   + number of columns in table (varint)
2742ef7a6304Sdan **   + array of PK flags (1 byte per column),
2743ef7a6304Sdan **   + table name (nul terminated).
274416228167Sdan **
274516228167Sdan ** This function decodes the table-header and populates the p->nCol,
274616228167Sdan ** p->zTab and p->abPK[] variables accordingly. The p->apValue[] array is
274716228167Sdan ** also allocated or resized according to the new value of p->nCol. The
274816228167Sdan ** input pointer is left pointing to the byte following the table header.
274916228167Sdan **
275016228167Sdan ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code
275116228167Sdan ** is returned and the final values of the various fields enumerated above
275216228167Sdan ** are undefined.
2753ef7a6304Sdan */
2754ef7a6304Sdan static int sessionChangesetReadTblhdr(sqlite3_changeset_iter *p){
2755ef7a6304Sdan   int rc;
2756ef7a6304Sdan   int nCopy;
2757ef7a6304Sdan   assert( p->rc==SQLITE_OK );
2758ef7a6304Sdan 
2759ef7a6304Sdan   rc = sessionChangesetBufferTblhdr(&p->in, &nCopy);
2760ef7a6304Sdan   if( rc==SQLITE_OK ){
2761ef7a6304Sdan     int nByte;
2762ef7a6304Sdan     int nVarint;
27634757c658Sdan     nVarint = sessionVarintGet(&p->in.aData[p->in.iNext], &p->nCol);
2764ef7a6304Sdan     nCopy -= nVarint;
2765ef7a6304Sdan     p->in.iNext += nVarint;
2766ef7a6304Sdan     nByte = p->nCol * sizeof(sqlite3_value*) * 2 + nCopy;
2767ef7a6304Sdan     p->tblhdr.nBuf = 0;
2768ef7a6304Sdan     sessionBufferGrow(&p->tblhdr, nByte, &rc);
2769ef7a6304Sdan   }
2770ef7a6304Sdan 
2771ef7a6304Sdan   if( rc==SQLITE_OK ){
2772ef7a6304Sdan     int iPK = sizeof(sqlite3_value*)*p->nCol*2;
2773ef7a6304Sdan     memset(p->tblhdr.aBuf, 0, iPK);
27744757c658Sdan     memcpy(&p->tblhdr.aBuf[iPK], &p->in.aData[p->in.iNext], nCopy);
2775ef7a6304Sdan     p->in.iNext += nCopy;
2776ef7a6304Sdan   }
2777ef7a6304Sdan 
2778ef7a6304Sdan   p->apValue = (sqlite3_value**)p->tblhdr.aBuf;
2779ef7a6304Sdan   p->abPK = (u8*)&p->apValue[p->nCol*2];
2780ef7a6304Sdan   p->zTab = (char*)&p->abPK[p->nCol];
2781ef7a6304Sdan   return (p->rc = rc);
27824fccf43aSdan }
27834fccf43aSdan 
278477fc1d5bSdan /*
278577fc1d5bSdan ** Advance the changeset iterator to the next change.
278677fc1d5bSdan **
278777fc1d5bSdan ** If both paRec and pnRec are NULL, then this function works like the public
278877fc1d5bSdan ** API sqlite3changeset_next(). If SQLITE_ROW is returned, then the
278977fc1d5bSdan ** sqlite3changeset_new() and old() APIs may be used to query for values.
279077fc1d5bSdan **
279177fc1d5bSdan ** Otherwise, if paRec and pnRec are not NULL, then a pointer to the change
279277fc1d5bSdan ** record is written to *paRec before returning and the number of bytes in
279377fc1d5bSdan ** the record to *pnRec.
279477fc1d5bSdan **
279577fc1d5bSdan ** Either way, this function returns SQLITE_ROW if the iterator is
279677fc1d5bSdan ** successfully advanced to the next change in the changeset, an SQLite
279777fc1d5bSdan ** error code if an error occurs, or SQLITE_DONE if there are no further
279877fc1d5bSdan ** changes in the changeset.
279977fc1d5bSdan */
28005d607a6eSdan static int sessionChangesetNext(
280177fc1d5bSdan   sqlite3_changeset_iter *p,      /* Changeset iterator */
280277fc1d5bSdan   u8 **paRec,                     /* If non-NULL, store record pointer here */
280377fc1d5bSdan   int *pnRec                      /* If non-NULL, store size of record here */
28045d607a6eSdan ){
28054fccf43aSdan   int i;
2806ef7a6304Sdan   u8 op;
28074fccf43aSdan 
28085d607a6eSdan   assert( (paRec==0 && pnRec==0) || (paRec && pnRec) );
28095d607a6eSdan 
2810296c7658Sdan   /* If the iterator is in the error-state, return immediately. */
28114fccf43aSdan   if( p->rc!=SQLITE_OK ) return p->rc;
28124fccf43aSdan 
28135d607a6eSdan   /* Free the current contents of p->apValue[], if any. */
28144fccf43aSdan   if( p->apValue ){
28154fccf43aSdan     for(i=0; i<p->nCol*2; i++){
28164fccf43aSdan       sqlite3ValueFree(p->apValue[i]);
28174fccf43aSdan     }
28184fccf43aSdan     memset(p->apValue, 0, sizeof(sqlite3_value*)*p->nCol*2);
28194fccf43aSdan   }
28204fccf43aSdan 
2821ef7a6304Sdan   /* Make sure the buffer contains at least 10 bytes of input data, or all
2822ef7a6304Sdan   ** remaining data if there are less than 10 bytes available. This is
2823ef7a6304Sdan   ** sufficient either for the 'T' or 'P' byte and the varint that follows
2824ef7a6304Sdan   ** it, or for the two single byte values otherwise. */
2825ef7a6304Sdan   p->rc = sessionInputBuffer(&p->in, 2);
2826ef7a6304Sdan   if( p->rc!=SQLITE_OK ) return p->rc;
2827ef7a6304Sdan 
28284fccf43aSdan   /* If the iterator is already at the end of the changeset, return DONE. */
28294757c658Sdan   if( p->in.iNext>=p->in.nData ){
28304fccf43aSdan     return SQLITE_DONE;
28314fccf43aSdan   }
28324fccf43aSdan 
2833d9151526Sdan   sessionDiscardData(&p->in);
2834d9151526Sdan   p->in.iCurrent = p->in.iNext;
2835d9151526Sdan 
28364757c658Sdan   op = p->in.aData[p->in.iNext++];
2837ef7a6304Sdan   if( op=='T' || op=='P' ){
2838ef7a6304Sdan     p->bPatchset = (op=='P');
2839ef7a6304Sdan     if( sessionChangesetReadTblhdr(p) ) return p->rc;
2840ef7a6304Sdan     if( (p->rc = sessionInputBuffer(&p->in, 2)) ) return p->rc;
2841d9151526Sdan     p->in.iCurrent = p->in.iNext;
28424757c658Sdan     op = p->in.aData[p->in.iNext++];
28435d607a6eSdan   }
28445d607a6eSdan 
2845ef7a6304Sdan   p->op = op;
28464757c658Sdan   p->bIndirect = p->in.aData[p->in.iNext++];
28474fccf43aSdan   if( p->op!=SQLITE_UPDATE && p->op!=SQLITE_DELETE && p->op!=SQLITE_INSERT ){
28484757c658Sdan     return (p->rc = SQLITE_CORRUPT_BKPT);
28494fccf43aSdan   }
28504fccf43aSdan 
2851cbf6d2d2Sdan   if( paRec ){
2852cbf6d2d2Sdan     int nVal;                     /* Number of values to buffer */
2853cbf6d2d2Sdan     if( p->bPatchset==0 && op==SQLITE_UPDATE ){
2854cbf6d2d2Sdan       nVal = p->nCol * 2;
2855cbf6d2d2Sdan     }else if( p->bPatchset && op==SQLITE_DELETE ){
2856cbf6d2d2Sdan       nVal = 0;
2857cbf6d2d2Sdan       for(i=0; i<p->nCol; i++) if( p->abPK[i] ) nVal++;
2858cbf6d2d2Sdan     }else{
2859cbf6d2d2Sdan       nVal = p->nCol;
2860cbf6d2d2Sdan     }
2861cbf6d2d2Sdan     p->rc = sessionChangesetBufferRecord(&p->in, nVal, pnRec);
2862cbf6d2d2Sdan     if( p->rc!=SQLITE_OK ) return p->rc;
2863cbf6d2d2Sdan     *paRec = &p->in.aData[p->in.iNext];
2864cbf6d2d2Sdan     p->in.iNext += *pnRec;
2865cbf6d2d2Sdan   }else{
28665d607a6eSdan 
28674fccf43aSdan     /* If this is an UPDATE or DELETE, read the old.* record. */
286873b3c055Sdan     if( p->op!=SQLITE_INSERT && (p->bPatchset==0 || p->op==SQLITE_DELETE) ){
286973b3c055Sdan       u8 *abPK = p->bPatchset ? p->abPK : 0;
2870cbf6d2d2Sdan       p->rc = sessionReadRecord(&p->in, p->nCol, abPK, p->apValue);
28714fccf43aSdan       if( p->rc!=SQLITE_OK ) return p->rc;
28724fccf43aSdan     }
28734fccf43aSdan 
28744fccf43aSdan     /* If this is an INSERT or UPDATE, read the new.* record. */
28754fccf43aSdan     if( p->op!=SQLITE_DELETE ){
2876cbf6d2d2Sdan       p->rc = sessionReadRecord(&p->in, p->nCol, 0, &p->apValue[p->nCol]);
28774fccf43aSdan       if( p->rc!=SQLITE_OK ) return p->rc;
28784fccf43aSdan     }
28794fccf43aSdan 
2880cbf6d2d2Sdan     if( p->bPatchset && p->op==SQLITE_UPDATE ){
288173b3c055Sdan       /* If this is an UPDATE that is part of a patchset, then all PK and
288273b3c055Sdan       ** modified fields are present in the new.* record. The old.* record
288373b3c055Sdan       ** is currently completely empty. This block shifts the PK fields from
288473b3c055Sdan       ** new.* to old.*, to accommodate the code that reads these arrays.  */
288573b3c055Sdan       int i;
288673b3c055Sdan       for(i=0; i<p->nCol; i++){
288773b3c055Sdan         assert( p->apValue[i]==0 );
288873b3c055Sdan         assert( p->abPK[i]==0 || p->apValue[i+p->nCol] );
288973b3c055Sdan         if( p->abPK[i] ){
289073b3c055Sdan           p->apValue[i] = p->apValue[i+p->nCol];
289173b3c055Sdan           p->apValue[i+p->nCol] = 0;
289273b3c055Sdan         }
289373b3c055Sdan       }
289473b3c055Sdan     }
2895cbf6d2d2Sdan   }
2896ef7a6304Sdan 
28974fccf43aSdan   return SQLITE_ROW;
28984fccf43aSdan }
28994fccf43aSdan 
29004fccf43aSdan /*
29015d607a6eSdan ** Advance an iterator created by sqlite3changeset_start() to the next
29025d607a6eSdan ** change in the changeset. This function may return SQLITE_ROW, SQLITE_DONE
29035d607a6eSdan ** or SQLITE_CORRUPT.
29045d607a6eSdan **
29055d607a6eSdan ** This function may not be called on iterators passed to a conflict handler
29065d607a6eSdan ** callback by changeset_apply().
29075d607a6eSdan */
29085d607a6eSdan int sqlite3changeset_next(sqlite3_changeset_iter *p){
29095d607a6eSdan   return sessionChangesetNext(p, 0, 0);
29105d607a6eSdan }
29115d607a6eSdan 
29125d607a6eSdan /*
2913244593c8Sdan ** The following function extracts information on the current change
291477fc1d5bSdan ** from a changeset iterator. It may only be called after changeset_next()
29154fccf43aSdan ** has returned SQLITE_ROW.
29164fccf43aSdan */
29174fccf43aSdan int sqlite3changeset_op(
2918296c7658Sdan   sqlite3_changeset_iter *pIter,  /* Iterator handle */
29194fccf43aSdan   const char **pzTab,             /* OUT: Pointer to table name */
29204fccf43aSdan   int *pnCol,                     /* OUT: Number of columns in table */
2921b4480e94Sdan   int *pOp,                       /* OUT: SQLITE_INSERT, DELETE or UPDATE */
2922b4480e94Sdan   int *pbIndirect                 /* OUT: True if change is indirect */
29234fccf43aSdan ){
29244fccf43aSdan   *pOp = pIter->op;
29254fccf43aSdan   *pnCol = pIter->nCol;
29264fccf43aSdan   *pzTab = pIter->zTab;
2927b4480e94Sdan   if( pbIndirect ) *pbIndirect = pIter->bIndirect;
29284fccf43aSdan   return SQLITE_OK;
29294fccf43aSdan }
29304fccf43aSdan 
293177fc1d5bSdan /*
293277fc1d5bSdan ** Return information regarding the PRIMARY KEY and number of columns in
293377fc1d5bSdan ** the database table affected by the change that pIter currently points
293477fc1d5bSdan ** to. This function may only be called after changeset_next() returns
293577fc1d5bSdan ** SQLITE_ROW.
293677fc1d5bSdan */
2937244593c8Sdan int sqlite3changeset_pk(
2938244593c8Sdan   sqlite3_changeset_iter *pIter,  /* Iterator object */
2939244593c8Sdan   unsigned char **pabPK,          /* OUT: Array of boolean - true for PK cols */
2940244593c8Sdan   int *pnCol                      /* OUT: Number of entries in output array */
2941244593c8Sdan ){
2942244593c8Sdan   *pabPK = pIter->abPK;
2943244593c8Sdan   if( pnCol ) *pnCol = pIter->nCol;
2944244593c8Sdan   return SQLITE_OK;
2945244593c8Sdan }
2946244593c8Sdan 
2947296c7658Sdan /*
2948296c7658Sdan ** This function may only be called while the iterator is pointing to an
2949296c7658Sdan ** SQLITE_UPDATE or SQLITE_DELETE change (see sqlite3changeset_op()).
2950296c7658Sdan ** Otherwise, SQLITE_MISUSE is returned.
2951296c7658Sdan **
2952296c7658Sdan ** It sets *ppValue to point to an sqlite3_value structure containing the
2953296c7658Sdan ** iVal'th value in the old.* record. Or, if that particular value is not
2954296c7658Sdan ** included in the record (because the change is an UPDATE and the field
2955296c7658Sdan ** was not modified and is not a PK column), set *ppValue to NULL.
2956296c7658Sdan **
2957296c7658Sdan ** If value iVal is out-of-range, SQLITE_RANGE is returned and *ppValue is
2958296c7658Sdan ** not modified. Otherwise, SQLITE_OK.
2959296c7658Sdan */
29604fccf43aSdan int sqlite3changeset_old(
2961296c7658Sdan   sqlite3_changeset_iter *pIter,  /* Changeset iterator */
2962296c7658Sdan   int iVal,                       /* Index of old.* value to retrieve */
29634fccf43aSdan   sqlite3_value **ppValue         /* OUT: Old value (or NULL pointer) */
29644fccf43aSdan ){
2965d5f0767cSdan   if( pIter->op!=SQLITE_UPDATE && pIter->op!=SQLITE_DELETE ){
2966d5f0767cSdan     return SQLITE_MISUSE;
2967d5f0767cSdan   }
29684fccf43aSdan   if( iVal<0 || iVal>=pIter->nCol ){
29694fccf43aSdan     return SQLITE_RANGE;
29704fccf43aSdan   }
29714fccf43aSdan   *ppValue = pIter->apValue[iVal];
29724fccf43aSdan   return SQLITE_OK;
29734fccf43aSdan }
29744fccf43aSdan 
2975296c7658Sdan /*
2976296c7658Sdan ** This function may only be called while the iterator is pointing to an
2977296c7658Sdan ** SQLITE_UPDATE or SQLITE_INSERT change (see sqlite3changeset_op()).
2978296c7658Sdan ** Otherwise, SQLITE_MISUSE is returned.
2979296c7658Sdan **
2980296c7658Sdan ** It sets *ppValue to point to an sqlite3_value structure containing the
2981296c7658Sdan ** iVal'th value in the new.* record. Or, if that particular value is not
2982296c7658Sdan ** included in the record (because the change is an UPDATE and the field
2983296c7658Sdan ** was not modified), set *ppValue to NULL.
2984296c7658Sdan **
2985296c7658Sdan ** If value iVal is out-of-range, SQLITE_RANGE is returned and *ppValue is
2986296c7658Sdan ** not modified. Otherwise, SQLITE_OK.
2987296c7658Sdan */
29884fccf43aSdan int sqlite3changeset_new(
2989296c7658Sdan   sqlite3_changeset_iter *pIter,  /* Changeset iterator */
2990296c7658Sdan   int iVal,                       /* Index of new.* value to retrieve */
29914fccf43aSdan   sqlite3_value **ppValue         /* OUT: New value (or NULL pointer) */
29924fccf43aSdan ){
2993d5f0767cSdan   if( pIter->op!=SQLITE_UPDATE && pIter->op!=SQLITE_INSERT ){
2994d5f0767cSdan     return SQLITE_MISUSE;
2995d5f0767cSdan   }
29964fccf43aSdan   if( iVal<0 || iVal>=pIter->nCol ){
29974fccf43aSdan     return SQLITE_RANGE;
29984fccf43aSdan   }
29994fccf43aSdan   *ppValue = pIter->apValue[pIter->nCol+iVal];
30004fccf43aSdan   return SQLITE_OK;
30014fccf43aSdan }
30024fccf43aSdan 
3003296c7658Sdan /*
30047aa469cdSdan ** The following two macros are used internally. They are similar to the
30057aa469cdSdan ** sqlite3changeset_new() and sqlite3changeset_old() functions, except that
30067aa469cdSdan ** they omit all error checking and return a pointer to the requested value.
30077aa469cdSdan */
30087aa469cdSdan #define sessionChangesetNew(pIter, iVal) (pIter)->apValue[(pIter)->nCol+(iVal)]
30097aa469cdSdan #define sessionChangesetOld(pIter, iVal) (pIter)->apValue[(iVal)]
30107aa469cdSdan 
30117aa469cdSdan /*
3012296c7658Sdan ** This function may only be called with a changeset iterator that has been
3013296c7658Sdan ** passed to an SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT
3014296c7658Sdan ** conflict-handler function. Otherwise, SQLITE_MISUSE is returned.
3015296c7658Sdan **
3016296c7658Sdan ** If successful, *ppValue is set to point to an sqlite3_value structure
3017296c7658Sdan ** containing the iVal'th value of the conflicting record.
3018296c7658Sdan **
3019296c7658Sdan ** If value iVal is out-of-range or some other error occurs, an SQLite error
3020296c7658Sdan ** code is returned. Otherwise, SQLITE_OK.
3021296c7658Sdan */
3022d5f0767cSdan int sqlite3changeset_conflict(
3023296c7658Sdan   sqlite3_changeset_iter *pIter,  /* Changeset iterator */
3024296c7658Sdan   int iVal,                       /* Index of conflict record value to fetch */
3025d5f0767cSdan   sqlite3_value **ppValue         /* OUT: Value from conflicting row */
3026d5f0767cSdan ){
3027d5f0767cSdan   if( !pIter->pConflict ){
3028d5f0767cSdan     return SQLITE_MISUSE;
3029d5f0767cSdan   }
3030d5f0767cSdan   if( iVal<0 || iVal>=sqlite3_column_count(pIter->pConflict) ){
3031d5f0767cSdan     return SQLITE_RANGE;
3032d5f0767cSdan   }
3033d5f0767cSdan   *ppValue = sqlite3_column_value(pIter->pConflict, iVal);
3034d5f0767cSdan   return SQLITE_OK;
3035d5f0767cSdan }
3036d5f0767cSdan 
30374fccf43aSdan /*
3038cb3e4b79Sdan ** This function may only be called with an iterator passed to an
3039cb3e4b79Sdan ** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case
3040cb3e4b79Sdan ** it sets the output variable to the total number of known foreign key
3041cb3e4b79Sdan ** violations in the destination database and returns SQLITE_OK.
3042cb3e4b79Sdan **
3043cb3e4b79Sdan ** In all other cases this function returns SQLITE_MISUSE.
3044cb3e4b79Sdan */
3045cb3e4b79Sdan int sqlite3changeset_fk_conflicts(
3046cb3e4b79Sdan   sqlite3_changeset_iter *pIter,  /* Changeset iterator */
3047cb3e4b79Sdan   int *pnOut                      /* OUT: Number of FK violations */
3048cb3e4b79Sdan ){
3049cb3e4b79Sdan   if( pIter->pConflict || pIter->apValue ){
3050cb3e4b79Sdan     return SQLITE_MISUSE;
3051cb3e4b79Sdan   }
3052cb3e4b79Sdan   *pnOut = pIter->nCol;
3053cb3e4b79Sdan   return SQLITE_OK;
3054cb3e4b79Sdan }
3055cb3e4b79Sdan 
3056cb3e4b79Sdan 
3057cb3e4b79Sdan /*
30584fccf43aSdan ** Finalize an iterator allocated with sqlite3changeset_start().
30594fccf43aSdan **
30604fccf43aSdan ** This function may not be called on iterators passed to a conflict handler
30614fccf43aSdan ** callback by changeset_apply().
30624fccf43aSdan */
30634fccf43aSdan int sqlite3changeset_finalize(sqlite3_changeset_iter *p){
3064cbf6d2d2Sdan   int rc = SQLITE_OK;
3065cbf6d2d2Sdan   if( p ){
3066296c7658Sdan     int i;                        /* Used to iterate through p->apValue[] */
3067cbf6d2d2Sdan     rc = p->rc;
306812ca0b56Sdan     if( p->apValue ){
30694fccf43aSdan       for(i=0; i<p->nCol*2; i++) sqlite3ValueFree(p->apValue[i]);
307012ca0b56Sdan     }
3071ef7a6304Sdan     sqlite3_free(p->tblhdr.aBuf);
30724757c658Sdan     sqlite3_free(p->in.buf.aBuf);
30734fccf43aSdan     sqlite3_free(p);
3074cbf6d2d2Sdan   }
30754fccf43aSdan   return rc;
30764fccf43aSdan }
30774fccf43aSdan 
3078fa122adaSdan static int sessionChangesetInvert(
3079fa122adaSdan   SessionInput *pInput,           /* Input changeset */
3080fa122adaSdan   int (*xOutput)(void *pOut, const void *pData, int nData),
3081fa122adaSdan   void *pOut,
308291ddd559Sdan   int *pnInverted,                /* OUT: Number of bytes in output changeset */
308391ddd559Sdan   void **ppInverted               /* OUT: Inverse of pChangeset */
308491ddd559Sdan ){
3085cfec7eeeSdan   int rc = SQLITE_OK;             /* Return value */
3086fa122adaSdan   SessionBuffer sOut;             /* Output buffer */
3087cfec7eeeSdan   int nCol = 0;                   /* Number of cols in current table */
3088cfec7eeeSdan   u8 *abPK = 0;                   /* PK array for current table */
3089cfec7eeeSdan   sqlite3_value **apVal = 0;      /* Space for values for UPDATE inversion */
3090ef7a6304Sdan   SessionBuffer sPK = {0, 0, 0};  /* PK array for current table */
309191ddd559Sdan 
3092fa122adaSdan   /* Initialize the output buffer */
3093fa122adaSdan   memset(&sOut, 0, sizeof(SessionBuffer));
3094fa122adaSdan 
309591ddd559Sdan   /* Zero the output variables in case an error occurs. */
3096fa122adaSdan   if( ppInverted ){
309791ddd559Sdan     *ppInverted = 0;
309891ddd559Sdan     *pnInverted = 0;
3099fa122adaSdan   }
310091ddd559Sdan 
3101fa122adaSdan   while( 1 ){
3102ef7a6304Sdan     u8 eType;
3103fa122adaSdan 
3104fa122adaSdan     /* Test for EOF. */
3105fa122adaSdan     if( (rc = sessionInputBuffer(pInput, 2)) ) goto finished_invert;
3106fa122adaSdan     if( pInput->iNext>=pInput->nData ) break;
3107fa122adaSdan     eType = pInput->aData[pInput->iNext];
3108fa122adaSdan 
310991ddd559Sdan     switch( eType ){
311091ddd559Sdan       case 'T': {
3111244593c8Sdan         /* A 'table' record consists of:
3112244593c8Sdan         **
3113244593c8Sdan         **   * A constant 'T' character,
3114244593c8Sdan         **   * Number of columns in said table (a varint),
3115ef7a6304Sdan         **   * An array of nCol bytes (sPK),
3116244593c8Sdan         **   * A nul-terminated table name.
3117244593c8Sdan         */
3118ef7a6304Sdan         int nByte;
3119fa122adaSdan         int nVar;
3120fa122adaSdan         pInput->iNext++;
3121fa122adaSdan         if( (rc = sessionChangesetBufferTblhdr(pInput, &nByte)) ){
3122ef7a6304Sdan           goto finished_invert;
3123ef7a6304Sdan         }
3124fa122adaSdan         nVar = sessionVarintGet(&pInput->aData[pInput->iNext], &nCol);
3125ef7a6304Sdan         sPK.nBuf = 0;
3126fa122adaSdan         sessionAppendBlob(&sPK, &pInput->aData[pInput->iNext+nVar], nCol, &rc);
3127fa122adaSdan         sessionAppendByte(&sOut, eType, &rc);
3128fa122adaSdan         sessionAppendBlob(&sOut, &pInput->aData[pInput->iNext], nByte, &rc);
3129ef7a6304Sdan         if( rc ) goto finished_invert;
3130fa122adaSdan 
3131fa122adaSdan         pInput->iNext += nByte;
3132cfec7eeeSdan         sqlite3_free(apVal);
3133cfec7eeeSdan         apVal = 0;
3134ef7a6304Sdan         abPK = sPK.aBuf;
313591ddd559Sdan         break;
313691ddd559Sdan       }
313791ddd559Sdan 
313891ddd559Sdan       case SQLITE_INSERT:
313991ddd559Sdan       case SQLITE_DELETE: {
314091ddd559Sdan         int nByte;
3141fa122adaSdan         int bIndirect = pInput->aData[pInput->iNext+1];
3142fa122adaSdan         int eType2 = (eType==SQLITE_DELETE ? SQLITE_INSERT : SQLITE_DELETE);
3143fa122adaSdan         pInput->iNext += 2;
3144fa122adaSdan         assert( rc==SQLITE_OK );
3145fa122adaSdan         rc = sessionChangesetBufferRecord(pInput, nCol, &nByte);
3146fa122adaSdan         sessionAppendByte(&sOut, eType2, &rc);
3147fa122adaSdan         sessionAppendByte(&sOut, bIndirect, &rc);
3148fa122adaSdan         sessionAppendBlob(&sOut, &pInput->aData[pInput->iNext], nByte, &rc);
3149fa122adaSdan         pInput->iNext += nByte;
3150fa122adaSdan         if( rc ) goto finished_invert;
315191ddd559Sdan         break;
315291ddd559Sdan       }
315391ddd559Sdan 
315491ddd559Sdan       case SQLITE_UPDATE: {
3155cfec7eeeSdan         int iCol;
315691ddd559Sdan 
3157cfec7eeeSdan         if( 0==apVal ){
3158cfec7eeeSdan           apVal = (sqlite3_value **)sqlite3_malloc(sizeof(apVal[0])*nCol*2);
3159cfec7eeeSdan           if( 0==apVal ){
3160cfec7eeeSdan             rc = SQLITE_NOMEM;
3161cfec7eeeSdan             goto finished_invert;
3162cfec7eeeSdan           }
3163cfec7eeeSdan           memset(apVal, 0, sizeof(apVal[0])*nCol*2);
3164cfec7eeeSdan         }
316591ddd559Sdan 
3166cfec7eeeSdan         /* Write the header for the new UPDATE change. Same as the original. */
3167fa122adaSdan         sessionAppendByte(&sOut, eType, &rc);
3168fa122adaSdan         sessionAppendByte(&sOut, pInput->aData[pInput->iNext+1], &rc);
316991ddd559Sdan 
3170ef7a6304Sdan         /* Read the old.* and new.* records for the update change. */
3171fa122adaSdan         pInput->iNext += 2;
3172fa122adaSdan         rc = sessionReadRecord(pInput, nCol, 0, &apVal[0]);
3173ef7a6304Sdan         if( rc==SQLITE_OK ){
3174fa122adaSdan           rc = sessionReadRecord(pInput, nCol, 0, &apVal[nCol]);
3175ef7a6304Sdan         }
3176ef7a6304Sdan 
3177cfec7eeeSdan         /* Write the new old.* record. Consists of the PK columns from the
3178cfec7eeeSdan         ** original old.* record, and the other values from the original
3179cfec7eeeSdan         ** new.* record. */
3180e8fa8c96Sdan         for(iCol=0; iCol<nCol; iCol++){
3181cfec7eeeSdan           sqlite3_value *pVal = apVal[iCol + (abPK[iCol] ? 0 : nCol)];
3182fa122adaSdan           sessionAppendValue(&sOut, pVal, &rc);
3183cfec7eeeSdan         }
3184cfec7eeeSdan 
3185cfec7eeeSdan         /* Write the new new.* record. Consists of a copy of all values
3186cfec7eeeSdan         ** from the original old.* record, except for the PK columns, which
3187cfec7eeeSdan         ** are set to "undefined". */
3188e8fa8c96Sdan         for(iCol=0; iCol<nCol; iCol++){
3189cfec7eeeSdan           sqlite3_value *pVal = (abPK[iCol] ? 0 : apVal[iCol]);
3190fa122adaSdan           sessionAppendValue(&sOut, pVal, &rc);
3191cfec7eeeSdan         }
3192cfec7eeeSdan 
3193cfec7eeeSdan         for(iCol=0; iCol<nCol*2; iCol++){
3194cfec7eeeSdan           sqlite3ValueFree(apVal[iCol]);
3195cfec7eeeSdan         }
3196cfec7eeeSdan         memset(apVal, 0, sizeof(apVal[0])*nCol*2);
3197cfec7eeeSdan         if( rc!=SQLITE_OK ){
3198cfec7eeeSdan           goto finished_invert;
3199cfec7eeeSdan         }
3200cfec7eeeSdan 
320191ddd559Sdan         break;
320291ddd559Sdan       }
320391ddd559Sdan 
320491ddd559Sdan       default:
32054757c658Sdan         rc = SQLITE_CORRUPT_BKPT;
3206cfec7eeeSdan         goto finished_invert;
320791ddd559Sdan     }
3208fa122adaSdan 
3209fa122adaSdan     assert( rc==SQLITE_OK );
3210f1a08ad8Sdrh     if( xOutput && sOut.nBuf>=SESSIONS_STRM_CHUNK_SIZE ){
3211fa122adaSdan       rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
3212fa122adaSdan       sOut.nBuf = 0;
3213fa122adaSdan       if( rc!=SQLITE_OK ) goto finished_invert;
3214fa122adaSdan     }
321591ddd559Sdan   }
321691ddd559Sdan 
3217cfec7eeeSdan   assert( rc==SQLITE_OK );
3218fa122adaSdan   if( pnInverted ){
3219fa122adaSdan     *pnInverted = sOut.nBuf;
3220fa122adaSdan     *ppInverted = sOut.aBuf;
3221fa122adaSdan     sOut.aBuf = 0;
3222fa122adaSdan   }else if( sOut.nBuf>0 ){
3223fa122adaSdan     rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
3224fa122adaSdan   }
3225cfec7eeeSdan 
3226cfec7eeeSdan  finished_invert:
3227fa122adaSdan   sqlite3_free(sOut.aBuf);
3228cfec7eeeSdan   sqlite3_free(apVal);
3229ef7a6304Sdan   sqlite3_free(sPK.aBuf);
3230cfec7eeeSdan   return rc;
323191ddd559Sdan }
323291ddd559Sdan 
3233fa122adaSdan 
3234fa122adaSdan /*
3235fa122adaSdan ** Invert a changeset object.
3236fa122adaSdan */
3237fa122adaSdan int sqlite3changeset_invert(
3238fa122adaSdan   int nChangeset,                 /* Number of bytes in input */
3239fa122adaSdan   const void *pChangeset,         /* Input changeset */
3240fa122adaSdan   int *pnInverted,                /* OUT: Number of bytes in output changeset */
3241fa122adaSdan   void **ppInverted               /* OUT: Inverse of pChangeset */
3242fa122adaSdan ){
3243fa122adaSdan   SessionInput sInput;
3244fa122adaSdan 
3245fa122adaSdan   /* Set up the input stream */
3246fa122adaSdan   memset(&sInput, 0, sizeof(SessionInput));
3247fa122adaSdan   sInput.nData = nChangeset;
3248fa122adaSdan   sInput.aData = (u8*)pChangeset;
3249fa122adaSdan 
3250fa122adaSdan   return sessionChangesetInvert(&sInput, 0, 0, pnInverted, ppInverted);
3251fa122adaSdan }
3252fa122adaSdan 
3253fa122adaSdan /*
3254fa122adaSdan ** Streaming version of sqlite3changeset_invert().
3255fa122adaSdan */
3256f1a08ad8Sdrh int sqlite3changeset_invert_strm(
3257fa122adaSdan   int (*xInput)(void *pIn, void *pData, int *pnData),
3258fa122adaSdan   void *pIn,
3259fa122adaSdan   int (*xOutput)(void *pOut, const void *pData, int nData),
3260fa122adaSdan   void *pOut
3261fa122adaSdan ){
3262fa122adaSdan   SessionInput sInput;
3263fa122adaSdan   int rc;
3264fa122adaSdan 
3265fa122adaSdan   /* Set up the input stream */
3266fa122adaSdan   memset(&sInput, 0, sizeof(SessionInput));
3267fa122adaSdan   sInput.xInput = xInput;
3268fa122adaSdan   sInput.pIn = pIn;
3269fa122adaSdan 
3270fa122adaSdan   rc = sessionChangesetInvert(&sInput, xOutput, pOut, 0, 0);
3271fa122adaSdan   sqlite3_free(sInput.buf.aBuf);
3272fa122adaSdan   return rc;
3273fa122adaSdan }
3274fa122adaSdan 
32750c698471Sdan typedef struct SessionApplyCtx SessionApplyCtx;
32760c698471Sdan struct SessionApplyCtx {
32770c698471Sdan   sqlite3 *db;
32780c698471Sdan   sqlite3_stmt *pDelete;          /* DELETE statement */
3279cfec7eeeSdan   sqlite3_stmt *pUpdate;          /* UPDATE statement */
32800c698471Sdan   sqlite3_stmt *pInsert;          /* INSERT statement */
32810c698471Sdan   sqlite3_stmt *pSelect;          /* SELECT statement */
32820c698471Sdan   int nCol;                       /* Size of azCol[] and abPK[] arrays */
32830c698471Sdan   const char **azCol;             /* Array of column names */
32840c698471Sdan   u8 *abPK;                       /* Boolean array - true if column is in PK */
3285d9151526Sdan 
3286d9151526Sdan   int bDeferConstraints;          /* True to defer constraints */
3287d9151526Sdan   SessionBuffer constraints;      /* Deferred constraints are stored here */
32880c698471Sdan };
32890c698471Sdan 
3290d5f0767cSdan /*
3291d5f0767cSdan ** Formulate a statement to DELETE a row from database db. Assuming a table
3292d5f0767cSdan ** structure like this:
3293d5f0767cSdan **
3294d5f0767cSdan **     CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c));
3295d5f0767cSdan **
3296d5f0767cSdan ** The DELETE statement looks like this:
3297d5f0767cSdan **
3298db04571cSdan **     DELETE FROM x WHERE a = :1 AND c = :3 AND (:5 OR b IS :2 AND d IS :4)
3299d5f0767cSdan **
3300d5f0767cSdan ** Variable :5 (nCol+1) is a boolean. It should be set to 0 if we require
3301d5f0767cSdan ** matching b and d values, or 1 otherwise. The second case comes up if the
3302d5f0767cSdan ** conflict handler is invoked with NOTFOUND and returns CHANGESET_REPLACE.
3303296c7658Sdan **
3304296c7658Sdan ** If successful, SQLITE_OK is returned and SessionApplyCtx.pDelete is left
3305296c7658Sdan ** pointing to the prepared version of the SQL statement.
3306d5f0767cSdan */
3307d5f0767cSdan static int sessionDeleteRow(
3308d5f0767cSdan   sqlite3 *db,                    /* Database handle */
3309d5f0767cSdan   const char *zTab,               /* Table name */
33100c698471Sdan   SessionApplyCtx *p              /* Session changeset-apply context */
3311d5f0767cSdan ){
3312296c7658Sdan   int i;
3313296c7658Sdan   const char *zSep = "";
3314d5f0767cSdan   int rc = SQLITE_OK;
3315d5f0767cSdan   SessionBuffer buf = {0, 0, 0};
33167cf7df7dSdan   int nPk = 0;
3317d5f0767cSdan 
3318d5f0767cSdan   sessionAppendStr(&buf, "DELETE FROM ", &rc);
3319d5f0767cSdan   sessionAppendIdent(&buf, zTab, &rc);
3320296c7658Sdan   sessionAppendStr(&buf, " WHERE ", &rc);
3321296c7658Sdan 
3322296c7658Sdan   for(i=0; i<p->nCol; i++){
3323296c7658Sdan     if( p->abPK[i] ){
33247cf7df7dSdan       nPk++;
3325296c7658Sdan       sessionAppendStr(&buf, zSep, &rc);
3326296c7658Sdan       sessionAppendIdent(&buf, p->azCol[i], &rc);
3327296c7658Sdan       sessionAppendStr(&buf, " = ?", &rc);
3328296c7658Sdan       sessionAppendInteger(&buf, i+1, &rc);
3329296c7658Sdan       zSep = " AND ";
3330296c7658Sdan     }
3331296c7658Sdan   }
3332296c7658Sdan 
33337cf7df7dSdan   if( nPk<p->nCol ){
3334296c7658Sdan     sessionAppendStr(&buf, " AND (?", &rc);
3335296c7658Sdan     sessionAppendInteger(&buf, p->nCol+1, &rc);
3336296c7658Sdan     sessionAppendStr(&buf, " OR ", &rc);
3337296c7658Sdan 
3338296c7658Sdan     zSep = "";
3339296c7658Sdan     for(i=0; i<p->nCol; i++){
3340296c7658Sdan       if( !p->abPK[i] ){
3341296c7658Sdan         sessionAppendStr(&buf, zSep, &rc);
3342296c7658Sdan         sessionAppendIdent(&buf, p->azCol[i], &rc);
3343296c7658Sdan         sessionAppendStr(&buf, " IS ?", &rc);
3344296c7658Sdan         sessionAppendInteger(&buf, i+1, &rc);
3345296c7658Sdan         zSep = "AND ";
3346296c7658Sdan       }
3347296c7658Sdan     }
3348296c7658Sdan     sessionAppendStr(&buf, ")", &rc);
33497cf7df7dSdan   }
3350d5f0767cSdan 
3351d5f0767cSdan   if( rc==SQLITE_OK ){
33520c698471Sdan     rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pDelete, 0);
3353d5f0767cSdan   }
3354d5f0767cSdan   sqlite3_free(buf.aBuf);
3355d5f0767cSdan 
3356d5f0767cSdan   return rc;
3357d5f0767cSdan }
3358d5f0767cSdan 
3359d5f0767cSdan /*
3360d5f0767cSdan ** Formulate and prepare a statement to UPDATE a row from database db.
3361d5f0767cSdan ** Assuming a table structure like this:
3362d5f0767cSdan **
3363d5f0767cSdan **     CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c));
3364d5f0767cSdan **
3365d5f0767cSdan ** The UPDATE statement looks like this:
3366d5f0767cSdan **
3367d5f0767cSdan **     UPDATE x SET
3368d5f0767cSdan **     a = CASE WHEN ?2  THEN ?3  ELSE a END,
3369964cbd46Sdan **     b = CASE WHEN ?5  THEN ?6  ELSE b END,
3370964cbd46Sdan **     c = CASE WHEN ?8  THEN ?9  ELSE c END,
3371964cbd46Sdan **     d = CASE WHEN ?11 THEN ?12 ELSE d END
3372d5f0767cSdan **     WHERE a = ?1 AND c = ?7 AND (?13 OR
3373964cbd46Sdan **       (?5==0 OR b IS ?4) AND (?11==0 OR d IS ?10) AND
3374d5f0767cSdan **     )
3375d5f0767cSdan **
3376d5f0767cSdan ** For each column in the table, there are three variables to bind:
3377d5f0767cSdan **
3378d5f0767cSdan **     ?(i*3+1)    The old.* value of the column, if any.
3379d5f0767cSdan **     ?(i*3+2)    A boolean flag indicating that the value is being modified.
3380d5f0767cSdan **     ?(i*3+3)    The new.* value of the column, if any.
3381d5f0767cSdan **
3382d5f0767cSdan ** Also, a boolean flag that, if set to true, causes the statement to update
3383d5f0767cSdan ** a row even if the non-PK values do not match. This is required if the
3384d5f0767cSdan ** conflict-handler is invoked with CHANGESET_DATA and returns
3385d5f0767cSdan ** CHANGESET_REPLACE. This is variable "?(nCol*3+1)".
3386d5f0767cSdan **
3387296c7658Sdan ** If successful, SQLITE_OK is returned and SessionApplyCtx.pUpdate is left
3388296c7658Sdan ** pointing to the prepared version of the SQL statement.
3389d5f0767cSdan */
3390d5f0767cSdan static int sessionUpdateRow(
3391d5f0767cSdan   sqlite3 *db,                    /* Database handle */
3392d5f0767cSdan   const char *zTab,               /* Table name */
33930c698471Sdan   SessionApplyCtx *p              /* Session changeset-apply context */
3394d5f0767cSdan ){
3395d5f0767cSdan   int rc = SQLITE_OK;
3396d5f0767cSdan   int i;
3397d5f0767cSdan   const char *zSep = "";
3398d5f0767cSdan   SessionBuffer buf = {0, 0, 0};
3399d5f0767cSdan 
3400d5f0767cSdan   /* Append "UPDATE tbl SET " */
3401d5f0767cSdan   sessionAppendStr(&buf, "UPDATE ", &rc);
3402d5f0767cSdan   sessionAppendIdent(&buf, zTab, &rc);
3403d5f0767cSdan   sessionAppendStr(&buf, " SET ", &rc);
3404d5f0767cSdan 
3405d5f0767cSdan   /* Append the assignments */
34060c698471Sdan   for(i=0; i<p->nCol; i++){
3407d5f0767cSdan     sessionAppendStr(&buf, zSep, &rc);
34080c698471Sdan     sessionAppendIdent(&buf, p->azCol[i], &rc);
3409d5f0767cSdan     sessionAppendStr(&buf, " = CASE WHEN ?", &rc);
3410d5f0767cSdan     sessionAppendInteger(&buf, i*3+2, &rc);
3411d5f0767cSdan     sessionAppendStr(&buf, " THEN ?", &rc);
3412d5f0767cSdan     sessionAppendInteger(&buf, i*3+3, &rc);
3413d5f0767cSdan     sessionAppendStr(&buf, " ELSE ", &rc);
34140c698471Sdan     sessionAppendIdent(&buf, p->azCol[i], &rc);
3415d5f0767cSdan     sessionAppendStr(&buf, " END", &rc);
3416d5f0767cSdan     zSep = ", ";
3417d5f0767cSdan   }
3418d5f0767cSdan 
3419d5f0767cSdan   /* Append the PK part of the WHERE clause */
3420d5f0767cSdan   sessionAppendStr(&buf, " WHERE ", &rc);
34210c698471Sdan   for(i=0; i<p->nCol; i++){
34220c698471Sdan     if( p->abPK[i] ){
34230c698471Sdan       sessionAppendIdent(&buf, p->azCol[i], &rc);
3424d5f0767cSdan       sessionAppendStr(&buf, " = ?", &rc);
3425d5f0767cSdan       sessionAppendInteger(&buf, i*3+1, &rc);
3426d5f0767cSdan       sessionAppendStr(&buf, " AND ", &rc);
3427d5f0767cSdan     }
3428d5f0767cSdan   }
3429d5f0767cSdan 
3430d5f0767cSdan   /* Append the non-PK part of the WHERE clause */
3431d5f0767cSdan   sessionAppendStr(&buf, " (?", &rc);
34320c698471Sdan   sessionAppendInteger(&buf, p->nCol*3+1, &rc);
3433d5f0767cSdan   sessionAppendStr(&buf, " OR 1", &rc);
34340c698471Sdan   for(i=0; i<p->nCol; i++){
34350c698471Sdan     if( !p->abPK[i] ){
3436d5f0767cSdan       sessionAppendStr(&buf, " AND (?", &rc);
3437d5f0767cSdan       sessionAppendInteger(&buf, i*3+2, &rc);
3438d5f0767cSdan       sessionAppendStr(&buf, "=0 OR ", &rc);
34390c698471Sdan       sessionAppendIdent(&buf, p->azCol[i], &rc);
3440d5f0767cSdan       sessionAppendStr(&buf, " IS ?", &rc);
3441d5f0767cSdan       sessionAppendInteger(&buf, i*3+1, &rc);
3442d5f0767cSdan       sessionAppendStr(&buf, ")", &rc);
3443d5f0767cSdan     }
3444d5f0767cSdan   }
3445d5f0767cSdan   sessionAppendStr(&buf, ")", &rc);
3446d5f0767cSdan 
3447d5f0767cSdan   if( rc==SQLITE_OK ){
34480c698471Sdan     rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pUpdate, 0);
3449d5f0767cSdan   }
3450d5f0767cSdan   sqlite3_free(buf.aBuf);
3451d5f0767cSdan 
3452d5f0767cSdan   return rc;
3453d5f0767cSdan }
3454d5f0767cSdan 
3455296c7658Sdan /*
3456296c7658Sdan ** Formulate and prepare an SQL statement to query table zTab by primary
3457296c7658Sdan ** key. Assuming the following table structure:
3458296c7658Sdan **
3459296c7658Sdan **     CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c));
3460296c7658Sdan **
3461296c7658Sdan ** The SELECT statement looks like this:
3462296c7658Sdan **
3463296c7658Sdan **     SELECT * FROM x WHERE a = ?1 AND c = ?3
3464296c7658Sdan **
3465296c7658Sdan ** If successful, SQLITE_OK is returned and SessionApplyCtx.pSelect is left
3466296c7658Sdan ** pointing to the prepared version of the SQL statement.
3467296c7658Sdan */
3468d5f0767cSdan static int sessionSelectRow(
3469d5f0767cSdan   sqlite3 *db,                    /* Database handle */
3470d5f0767cSdan   const char *zTab,               /* Table name */
34710c698471Sdan   SessionApplyCtx *p              /* Session changeset-apply context */
3472d5f0767cSdan ){
3473d7fb7d24Sdan   return sessionSelectStmt(
3474d7fb7d24Sdan       db, "main", zTab, p->nCol, p->azCol, p->abPK, &p->pSelect);
3475d5f0767cSdan }
3476d5f0767cSdan 
3477296c7658Sdan /*
3478296c7658Sdan ** Formulate and prepare an INSERT statement to add a record to table zTab.
3479296c7658Sdan ** For example:
3480296c7658Sdan **
3481296c7658Sdan **     INSERT INTO main."zTab" VALUES(?1, ?2, ?3 ...);
3482296c7658Sdan **
3483296c7658Sdan ** If successful, SQLITE_OK is returned and SessionApplyCtx.pInsert is left
3484296c7658Sdan ** pointing to the prepared version of the SQL statement.
3485296c7658Sdan */
34860c698471Sdan static int sessionInsertRow(
34870c698471Sdan   sqlite3 *db,                    /* Database handle */
34880c698471Sdan   const char *zTab,               /* Table name */
34890c698471Sdan   SessionApplyCtx *p              /* Session changeset-apply context */
34900c698471Sdan ){
34910c698471Sdan   int rc = SQLITE_OK;
34920c698471Sdan   int i;
34930c698471Sdan   SessionBuffer buf = {0, 0, 0};
34940c698471Sdan 
34950c698471Sdan   sessionAppendStr(&buf, "INSERT INTO main.", &rc);
34960c698471Sdan   sessionAppendIdent(&buf, zTab, &rc);
34970c698471Sdan   sessionAppendStr(&buf, " VALUES(?", &rc);
34980c698471Sdan   for(i=1; i<p->nCol; i++){
34990c698471Sdan     sessionAppendStr(&buf, ", ?", &rc);
35000c698471Sdan   }
35010c698471Sdan   sessionAppendStr(&buf, ")", &rc);
35020c698471Sdan 
35030c698471Sdan   if( rc==SQLITE_OK ){
35040c698471Sdan     rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pInsert, 0);
35050c698471Sdan   }
35060c698471Sdan   sqlite3_free(buf.aBuf);
35070c698471Sdan   return rc;
35080c698471Sdan }
35090c698471Sdan 
3510296c7658Sdan /*
35117aa469cdSdan ** A wrapper around sqlite3_bind_value() that detects an extra problem.
35127aa469cdSdan ** See comments in the body of this function for details.
35137aa469cdSdan */
35147aa469cdSdan static int sessionBindValue(
35157aa469cdSdan   sqlite3_stmt *pStmt,            /* Statement to bind value to */
35167aa469cdSdan   int i,                          /* Parameter number to bind to */
35177aa469cdSdan   sqlite3_value *pVal             /* Value to bind */
35187aa469cdSdan ){
35195671ef69Sdrh   int eType = sqlite3_value_type(pVal);
3520082c96dfSdan   /* COVERAGE: The (pVal->z==0) branch is never true using current versions
3521082c96dfSdan   ** of SQLite. If a malloc fails in an sqlite3_value_xxx() function, either
3522082c96dfSdan   ** the (pVal->z) variable remains as it was or the type of the value is
3523082c96dfSdan   ** set to SQLITE_NULL.  */
35245671ef69Sdrh   if( (eType==SQLITE_TEXT || eType==SQLITE_BLOB) && pVal->z==0 ){
35257aa469cdSdan     /* This condition occurs when an earlier OOM in a call to
35267aa469cdSdan     ** sqlite3_value_text() or sqlite3_value_blob() (perhaps from within
3527082c96dfSdan     ** a conflict-handler) has zeroed the pVal->z pointer. Return NOMEM. */
35287aa469cdSdan     return SQLITE_NOMEM;
35297aa469cdSdan   }
35307aa469cdSdan   return sqlite3_bind_value(pStmt, i, pVal);
35317aa469cdSdan }
35327aa469cdSdan 
35337aa469cdSdan /*
3534db04571cSdan ** Iterator pIter must point to an SQLITE_INSERT entry. This function
3535db04571cSdan ** transfers new.* values from the current iterator entry to statement
3536db04571cSdan ** pStmt. The table being inserted into has nCol columns.
3537db04571cSdan **
3538d9151526Sdan ** New.* value $i from the iterator is bound to variable ($i+1) of
3539db04571cSdan ** statement pStmt. If parameter abPK is NULL, all values from 0 to (nCol-1)
3540db04571cSdan ** are transfered to the statement. Otherwise, if abPK is not NULL, it points
3541db04571cSdan ** to an array nCol elements in size. In this case only those values for
3542db04571cSdan ** which abPK[$i] is true are read from the iterator and bound to the
3543db04571cSdan ** statement.
3544db04571cSdan **
3545db04571cSdan ** An SQLite error code is returned if an error occurs. Otherwise, SQLITE_OK.
3546db04571cSdan */
35477aa469cdSdan static int sessionBindRow(
3548db04571cSdan   sqlite3_changeset_iter *pIter,  /* Iterator to read values from */
35497aa469cdSdan   int(*xValue)(sqlite3_changeset_iter *, int, sqlite3_value **),
3550db04571cSdan   int nCol,                       /* Number of columns */
3551db04571cSdan   u8 *abPK,                       /* If not NULL, bind only if true */
3552db04571cSdan   sqlite3_stmt *pStmt             /* Bind values to this statement */
3553db04571cSdan ){
3554db04571cSdan   int i;
3555db04571cSdan   int rc = SQLITE_OK;
35567aa469cdSdan 
35577aa469cdSdan   /* Neither sqlite3changeset_old or sqlite3changeset_new can fail if the
35587aa469cdSdan   ** argument iterator points to a suitable entry. Make sure that xValue
35597aa469cdSdan   ** is one of these to guarantee that it is safe to ignore the return
35607aa469cdSdan   ** in the code below. */
35617aa469cdSdan   assert( xValue==sqlite3changeset_old || xValue==sqlite3changeset_new );
35627aa469cdSdan 
3563db04571cSdan   for(i=0; rc==SQLITE_OK && i<nCol; i++){
3564db04571cSdan     if( !abPK || abPK[i] ){
3565db04571cSdan       sqlite3_value *pVal;
35667aa469cdSdan       (void)xValue(pIter, i, &pVal);
35677aa469cdSdan       rc = sessionBindValue(pStmt, i+1, pVal);
3568db04571cSdan     }
3569db04571cSdan   }
3570db04571cSdan   return rc;
3571db04571cSdan }
3572db04571cSdan 
3573db04571cSdan /*
3574296c7658Sdan ** SQL statement pSelect is as generated by the sessionSelectRow() function.
3575296c7658Sdan ** This function binds the primary key values from the change that changeset
3576296c7658Sdan ** iterator pIter points to to the SELECT and attempts to seek to the table
3577296c7658Sdan ** entry. If a row is found, the SELECT statement left pointing at the row
3578296c7658Sdan ** and SQLITE_ROW is returned. Otherwise, if no row is found and no error
3579296c7658Sdan ** has occured, the statement is reset and SQLITE_OK is returned. If an
35807aa469cdSdan ** error occurs, the statement is reset and an SQLite error code is returned.
35817aa469cdSdan **
35827aa469cdSdan ** If this function returns SQLITE_ROW, the caller must eventually reset()
35837aa469cdSdan ** statement pSelect. If any other value is returned, the statement does
35847aa469cdSdan ** not require a reset().
3585296c7658Sdan **
3586296c7658Sdan ** If the iterator currently points to an INSERT record, bind values from the
3587db04571cSdan ** new.* record to the SELECT statement. Or, if it points to a DELETE or
3588db04571cSdan ** UPDATE, bind values from the old.* record.
3589296c7658Sdan */
35900c698471Sdan static int sessionSeekToRow(
359137f133ecSdan   sqlite3 *db,                    /* Database handle */
359237f133ecSdan   sqlite3_changeset_iter *pIter,  /* Changeset iterator */
359337f133ecSdan   u8 *abPK,                       /* Primary key flags array */
35940c698471Sdan   sqlite3_stmt *pSelect           /* SELECT statement from sessionSelectRow() */
359537f133ecSdan ){
35967aa469cdSdan   int rc;                         /* Return code */
3597296c7658Sdan   int nCol;                       /* Number of columns in table */
3598296c7658Sdan   int op;                         /* Changset operation (SQLITE_UPDATE etc.) */
3599296c7658Sdan   const char *zDummy;             /* Unused */
360037f133ecSdan 
3601b4480e94Sdan   sqlite3changeset_op(pIter, &zDummy, &nCol, &op, 0);
36027aa469cdSdan   rc = sessionBindRow(pIter,
3603db04571cSdan       op==SQLITE_INSERT ? sqlite3changeset_new : sqlite3changeset_old,
3604db04571cSdan       nCol, abPK, pSelect
3605db04571cSdan   );
36060c698471Sdan 
36070c698471Sdan   if( rc==SQLITE_OK ){
36080c698471Sdan     rc = sqlite3_step(pSelect);
36090c698471Sdan     if( rc!=SQLITE_ROW ) rc = sqlite3_reset(pSelect);
36100c698471Sdan   }
36110c698471Sdan 
36120c698471Sdan   return rc;
36130c698471Sdan }
36140c698471Sdan 
3615296c7658Sdan /*
3616296c7658Sdan ** Invoke the conflict handler for the change that the changeset iterator
3617296c7658Sdan ** currently points to.
3618296c7658Sdan **
3619296c7658Sdan ** Argument eType must be either CHANGESET_DATA or CHANGESET_CONFLICT.
3620296c7658Sdan ** If argument pbReplace is NULL, then the type of conflict handler invoked
3621296c7658Sdan ** depends solely on eType, as follows:
3622296c7658Sdan **
3623296c7658Sdan **    eType value                 Value passed to xConflict
3624296c7658Sdan **    -------------------------------------------------
3625296c7658Sdan **    CHANGESET_DATA              CHANGESET_NOTFOUND
3626296c7658Sdan **    CHANGESET_CONFLICT          CHANGESET_CONSTRAINT
3627296c7658Sdan **
3628296c7658Sdan ** Or, if pbReplace is not NULL, then an attempt is made to find an existing
3629296c7658Sdan ** record with the same primary key as the record about to be deleted, updated
3630296c7658Sdan ** or inserted. If such a record can be found, it is available to the conflict
3631296c7658Sdan ** handler as the "conflicting" record. In this case the type of conflict
3632296c7658Sdan ** handler invoked is as follows:
3633296c7658Sdan **
3634296c7658Sdan **    eType value         PK Record found?   Value passed to xConflict
3635296c7658Sdan **    ----------------------------------------------------------------
3636296c7658Sdan **    CHANGESET_DATA      Yes                CHANGESET_DATA
3637296c7658Sdan **    CHANGESET_DATA      No                 CHANGESET_NOTFOUND
3638296c7658Sdan **    CHANGESET_CONFLICT  Yes                CHANGESET_CONFLICT
3639296c7658Sdan **    CHANGESET_CONFLICT  No                 CHANGESET_CONSTRAINT
3640296c7658Sdan **
3641296c7658Sdan ** If pbReplace is not NULL, and a record with a matching PK is found, and
3642296c7658Sdan ** the conflict handler function returns SQLITE_CHANGESET_REPLACE, *pbReplace
3643296c7658Sdan ** is set to non-zero before returning SQLITE_OK.
3644296c7658Sdan **
3645296c7658Sdan ** If the conflict handler returns SQLITE_CHANGESET_ABORT, SQLITE_ABORT is
3646296c7658Sdan ** returned. Or, if the conflict handler returns an invalid value,
3647296c7658Sdan ** SQLITE_MISUSE. If the conflict handler returns SQLITE_CHANGESET_OMIT,
3648296c7658Sdan ** this function returns SQLITE_OK.
3649296c7658Sdan */
36500c698471Sdan static int sessionConflictHandler(
3651296c7658Sdan   int eType,                      /* Either CHANGESET_DATA or CONFLICT */
3652296c7658Sdan   SessionApplyCtx *p,             /* changeset_apply() context */
36530c698471Sdan   sqlite3_changeset_iter *pIter,  /* Changeset iterator */
36540c698471Sdan   int(*xConflict)(void *, int, sqlite3_changeset_iter*),
3655296c7658Sdan   void *pCtx,                     /* First argument for conflict handler */
3656296c7658Sdan   int *pbReplace                  /* OUT: Set to true if PK row is found */
36570c698471Sdan ){
3658296c7658Sdan   int res;                        /* Value returned by conflict handler */
36590c698471Sdan   int rc;
36600c698471Sdan   int nCol;
36610c698471Sdan   int op;
36620c698471Sdan   const char *zDummy;
36630c698471Sdan 
3664b4480e94Sdan   sqlite3changeset_op(pIter, &zDummy, &nCol, &op, 0);
36650c698471Sdan 
36660c698471Sdan   assert( eType==SQLITE_CHANGESET_CONFLICT || eType==SQLITE_CHANGESET_DATA );
36670c698471Sdan   assert( SQLITE_CHANGESET_CONFLICT+1==SQLITE_CHANGESET_CONSTRAINT );
36680c698471Sdan   assert( SQLITE_CHANGESET_DATA+1==SQLITE_CHANGESET_NOTFOUND );
366937f133ecSdan 
367037f133ecSdan   /* Bind the new.* PRIMARY KEY values to the SELECT statement. */
36710c698471Sdan   if( pbReplace ){
36720c698471Sdan     rc = sessionSeekToRow(p->db, pIter, p->abPK, p->pSelect);
36730c698471Sdan   }else{
3674db04571cSdan     rc = SQLITE_OK;
36750c698471Sdan   }
36760c698471Sdan 
36770c698471Sdan   if( rc==SQLITE_ROW ){
36780c698471Sdan     /* There exists another row with the new.* primary key. */
36790c698471Sdan     pIter->pConflict = p->pSelect;
36800c698471Sdan     res = xConflict(pCtx, eType, pIter);
36810c698471Sdan     pIter->pConflict = 0;
36820c698471Sdan     rc = sqlite3_reset(p->pSelect);
3683db04571cSdan   }else if( rc==SQLITE_OK ){
3684d9151526Sdan     if( p->bDeferConstraints && eType==SQLITE_CHANGESET_CONFLICT ){
3685d9151526Sdan       /* Instead of invoking the conflict handler, append the change blob
3686d9151526Sdan       ** to the SessionApplyCtx.constraints buffer. */
3687d9151526Sdan       u8 *aBlob = &pIter->in.aData[pIter->in.iCurrent];
3688d9151526Sdan       int nBlob = pIter->in.iNext - pIter->in.iCurrent;
3689d9151526Sdan       sessionAppendBlob(&p->constraints, aBlob, nBlob, &rc);
3690d9151526Sdan       res = SQLITE_CHANGESET_OMIT;
3691d9151526Sdan     }else{
36920c698471Sdan       /* No other row with the new.* primary key. */
36930c698471Sdan       res = xConflict(pCtx, eType+1, pIter);
36940c698471Sdan       if( res==SQLITE_CHANGESET_REPLACE ) rc = SQLITE_MISUSE;
369537f133ecSdan     }
3696d9151526Sdan   }
369737f133ecSdan 
369837f133ecSdan   if( rc==SQLITE_OK ){
36990c698471Sdan     switch( res ){
37000c698471Sdan       case SQLITE_CHANGESET_REPLACE:
3701f51e5f6cSdan         assert( pbReplace );
3702f51e5f6cSdan         *pbReplace = 1;
37030c698471Sdan         break;
37040c698471Sdan 
37050c698471Sdan       case SQLITE_CHANGESET_OMIT:
37060c698471Sdan         break;
37070c698471Sdan 
37080c698471Sdan       case SQLITE_CHANGESET_ABORT:
37090c698471Sdan         rc = SQLITE_ABORT;
37100c698471Sdan         break;
37110c698471Sdan 
37120c698471Sdan       default:
37130c698471Sdan         rc = SQLITE_MISUSE;
37140c698471Sdan         break;
37150c698471Sdan     }
37160c698471Sdan   }
37170c698471Sdan 
37180c698471Sdan   return rc;
37190c698471Sdan }
37200c698471Sdan 
3721296c7658Sdan /*
3722296c7658Sdan ** Attempt to apply the change that the iterator passed as the first argument
3723296c7658Sdan ** currently points to to the database. If a conflict is encountered, invoke
3724296c7658Sdan ** the conflict handler callback.
3725296c7658Sdan **
3726296c7658Sdan ** If argument pbRetry is NULL, then ignore any CHANGESET_DATA conflict. If
3727296c7658Sdan ** one is encountered, update or delete the row with the matching primary key
3728296c7658Sdan ** instead. Or, if pbRetry is not NULL and a CHANGESET_DATA conflict occurs,
3729296c7658Sdan ** invoke the conflict handler. If it returns CHANGESET_REPLACE, set *pbRetry
3730296c7658Sdan ** to true before returning. In this case the caller will invoke this function
3731296c7658Sdan ** again, this time with pbRetry set to NULL.
3732296c7658Sdan **
3733296c7658Sdan ** If argument pbReplace is NULL and a CHANGESET_CONFLICT conflict is
3734296c7658Sdan ** encountered invoke the conflict handler with CHANGESET_CONSTRAINT instead.
3735296c7658Sdan ** Or, if pbReplace is not NULL, invoke it with CHANGESET_CONFLICT. If such
3736296c7658Sdan ** an invocation returns SQLITE_CHANGESET_REPLACE, set *pbReplace to true
3737296c7658Sdan ** before retrying. In this case the caller attempts to remove the conflicting
3738296c7658Sdan ** row before invoking this function again, this time with pbReplace set
3739296c7658Sdan ** to NULL.
3740296c7658Sdan **
3741296c7658Sdan ** If any conflict handler returns SQLITE_CHANGESET_ABORT, this function
3742296c7658Sdan ** returns SQLITE_ABORT. Otherwise, if no error occurs, SQLITE_OK is
3743296c7658Sdan ** returned.
3744296c7658Sdan */
37450c698471Sdan static int sessionApplyOneOp(
3746296c7658Sdan   sqlite3_changeset_iter *pIter,  /* Changeset iterator */
3747296c7658Sdan   SessionApplyCtx *p,             /* changeset_apply() context */
37480c698471Sdan   int(*xConflict)(void *, int, sqlite3_changeset_iter *),
3749296c7658Sdan   void *pCtx,                     /* First argument for the conflict handler */
3750296c7658Sdan   int *pbReplace,                 /* OUT: True to remove PK row and retry */
3751296c7658Sdan   int *pbRetry                    /* OUT: True to retry. */
37520c698471Sdan ){
37530c698471Sdan   const char *zDummy;
37540c698471Sdan   int op;
37550c698471Sdan   int nCol;
37560c698471Sdan   int rc = SQLITE_OK;
37570c698471Sdan 
37580c698471Sdan   assert( p->pDelete && p->pUpdate && p->pInsert && p->pSelect );
37590c698471Sdan   assert( p->azCol && p->abPK );
37600c698471Sdan   assert( !pbReplace || *pbReplace==0 );
37610c698471Sdan 
3762b4480e94Sdan   sqlite3changeset_op(pIter, &zDummy, &nCol, &op, 0);
37630c698471Sdan 
37640c698471Sdan   if( op==SQLITE_DELETE ){
37650c698471Sdan 
376673b3c055Sdan     /* Bind values to the DELETE statement. If conflict handling is required,
376773b3c055Sdan     ** bind values for all columns and set bound variable (nCol+1) to true.
376873b3c055Sdan     ** Or, if conflict handling is not required, bind just the PK column
376973b3c055Sdan     ** values and, if it exists, set (nCol+1) to false. Conflict handling
377073b3c055Sdan     ** is not required if:
377173b3c055Sdan     **
377273b3c055Sdan     **   * this is a patchset, or
377373b3c055Sdan     **   * (pbRetry==0), or
377473b3c055Sdan     **   * all columns of the table are PK columns (in this case there is
377573b3c055Sdan     **     no (nCol+1) variable to bind to).
377673b3c055Sdan     */
377773b3c055Sdan     u8 *abPK = (pIter->bPatchset ? p->abPK : 0);
377873b3c055Sdan     rc = sessionBindRow(pIter, sqlite3changeset_old, nCol, abPK, p->pDelete);
37797cf7df7dSdan     if( rc==SQLITE_OK && sqlite3_bind_parameter_count(p->pDelete)>nCol ){
378073b3c055Sdan       rc = sqlite3_bind_int(p->pDelete, nCol+1, (pbRetry==0 || abPK));
37817cf7df7dSdan     }
37820c698471Sdan     if( rc!=SQLITE_OK ) return rc;
37830c698471Sdan 
37840c698471Sdan     sqlite3_step(p->pDelete);
37850c698471Sdan     rc = sqlite3_reset(p->pDelete);
37860c698471Sdan     if( rc==SQLITE_OK && sqlite3_changes(p->db)==0 ){
37870c698471Sdan       rc = sessionConflictHandler(
37880c698471Sdan           SQLITE_CHANGESET_DATA, p, pIter, xConflict, pCtx, pbRetry
37890c698471Sdan       );
379035e2858eSdan     }else if( (rc&0xff)==SQLITE_CONSTRAINT ){
37910c698471Sdan       rc = sessionConflictHandler(
37920c698471Sdan           SQLITE_CHANGESET_CONFLICT, p, pIter, xConflict, pCtx, 0
37930c698471Sdan       );
37940c698471Sdan     }
37950c698471Sdan 
37960c698471Sdan   }else if( op==SQLITE_UPDATE ){
37970c698471Sdan     int i;
37980c698471Sdan 
37990c698471Sdan     /* Bind values to the UPDATE statement. */
38000c698471Sdan     for(i=0; rc==SQLITE_OK && i<nCol; i++){
38017aa469cdSdan       sqlite3_value *pOld = sessionChangesetOld(pIter, i);
38027aa469cdSdan       sqlite3_value *pNew = sessionChangesetNew(pIter, i);
38037aa469cdSdan 
38040c698471Sdan       sqlite3_bind_int(p->pUpdate, i*3+2, !!pNew);
38057aa469cdSdan       if( pOld ){
38067aa469cdSdan         rc = sessionBindValue(p->pUpdate, i*3+1, pOld);
38077aa469cdSdan       }
38087aa469cdSdan       if( rc==SQLITE_OK && pNew ){
38097aa469cdSdan         rc = sessionBindValue(p->pUpdate, i*3+3, pNew);
38100c698471Sdan       }
38110c698471Sdan     }
381273b3c055Sdan     if( rc==SQLITE_OK ){
381373b3c055Sdan       sqlite3_bind_int(p->pUpdate, nCol*3+1, pbRetry==0 || pIter->bPatchset);
381473b3c055Sdan     }
38150c698471Sdan     if( rc!=SQLITE_OK ) return rc;
38160c698471Sdan 
38170c698471Sdan     /* Attempt the UPDATE. In the case of a NOTFOUND or DATA conflict,
38180c698471Sdan     ** the result will be SQLITE_OK with 0 rows modified. */
38190c698471Sdan     sqlite3_step(p->pUpdate);
38200c698471Sdan     rc = sqlite3_reset(p->pUpdate);
38210c698471Sdan 
38220c698471Sdan     if( rc==SQLITE_OK && sqlite3_changes(p->db)==0 ){
38230c698471Sdan       /* A NOTFOUND or DATA error. Search the table to see if it contains
38240c698471Sdan       ** a row with a matching primary key. If so, this is a DATA conflict.
38250c698471Sdan       ** Otherwise, if there is no primary key match, it is a NOTFOUND. */
38260c698471Sdan 
38270c698471Sdan       rc = sessionConflictHandler(
38280c698471Sdan           SQLITE_CHANGESET_DATA, p, pIter, xConflict, pCtx, pbRetry
38290c698471Sdan       );
38300c698471Sdan 
383135e2858eSdan     }else if( (rc&0xff)==SQLITE_CONSTRAINT ){
3832db04571cSdan       /* This is always a CONSTRAINT conflict. */
3833db04571cSdan       rc = sessionConflictHandler(
3834db04571cSdan           SQLITE_CHANGESET_CONFLICT, p, pIter, xConflict, pCtx, 0
38350c698471Sdan       );
38360c698471Sdan     }
38370c698471Sdan 
38380c698471Sdan   }else{
38390c698471Sdan     assert( op==SQLITE_INSERT );
38407aa469cdSdan     rc = sessionBindRow(pIter, sqlite3changeset_new, nCol, 0, p->pInsert);
38410c698471Sdan     if( rc!=SQLITE_OK ) return rc;
38420c698471Sdan 
38430c698471Sdan     sqlite3_step(p->pInsert);
38440c698471Sdan     rc = sqlite3_reset(p->pInsert);
384535e2858eSdan     if( (rc&0xff)==SQLITE_CONSTRAINT ){
38460c698471Sdan       rc = sessionConflictHandler(
38470c698471Sdan           SQLITE_CHANGESET_CONFLICT, p, pIter, xConflict, pCtx, pbReplace
38480c698471Sdan       );
384937f133ecSdan     }
385037f133ecSdan   }
385137f133ecSdan 
385237f133ecSdan   return rc;
385337f133ecSdan }
385437f133ecSdan 
3855d9151526Sdan static int sessionApplyOneWithRetry(
3856d9151526Sdan   sqlite3 *db,                    /* Apply change to "main" db of this handle */
3857d9151526Sdan   sqlite3_changeset_iter *pIter,  /* Changeset iterator to read change from */
3858d9151526Sdan   SessionApplyCtx *pApply,        /* Apply context */
3859d9151526Sdan   int(*xConflict)(void*, int, sqlite3_changeset_iter*),
3860d9151526Sdan   void *pCtx                      /* First argument passed to xConflict */
3861d9151526Sdan ){
3862d9151526Sdan   int bReplace = 0;
3863d9151526Sdan   int bRetry = 0;
3864d9151526Sdan   int rc;
3865d9151526Sdan 
3866d9151526Sdan   rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, &bReplace, &bRetry);
3867d9151526Sdan 
3868d9151526Sdan   if( rc==SQLITE_OK && bRetry ){
3869d9151526Sdan     rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, &bReplace, 0);
3870d9151526Sdan   }
3871d9151526Sdan 
3872d9151526Sdan   if( bReplace ){
3873d9151526Sdan     assert( pIter->op==SQLITE_INSERT );
3874d9151526Sdan     rc = sqlite3_exec(db, "SAVEPOINT replace_op", 0, 0, 0);
3875d9151526Sdan     if( rc==SQLITE_OK ){
3876d9151526Sdan       rc = sessionBindRow(pIter,
3877d9151526Sdan           sqlite3changeset_new, pApply->nCol, pApply->abPK, pApply->pDelete);
3878d9151526Sdan       sqlite3_bind_int(pApply->pDelete, pApply->nCol+1, 1);
3879d9151526Sdan     }
3880d9151526Sdan     if( rc==SQLITE_OK ){
3881d9151526Sdan       sqlite3_step(pApply->pDelete);
3882d9151526Sdan       rc = sqlite3_reset(pApply->pDelete);
3883d9151526Sdan     }
3884d9151526Sdan     if( rc==SQLITE_OK ){
3885d9151526Sdan       rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, 0, 0);
3886d9151526Sdan     }
3887d9151526Sdan     if( rc==SQLITE_OK ){
3888d9151526Sdan       rc = sqlite3_exec(db, "RELEASE replace_op", 0, 0, 0);
3889d9151526Sdan     }
3890d9151526Sdan   }
3891d9151526Sdan 
3892d9151526Sdan   return rc;
3893d9151526Sdan }
3894d9151526Sdan 
3895d9151526Sdan /*
3896d9151526Sdan ** Retry the changes accumulated in the pApply->constraints buffer.
3897d9151526Sdan */
3898d9151526Sdan static int sessionRetryConstraints(
3899d9151526Sdan   sqlite3 *db,
3900d9151526Sdan   int bPatchset,
3901d9151526Sdan   const char *zTab,
3902d9151526Sdan   SessionApplyCtx *pApply,
3903d9151526Sdan   int(*xConflict)(void*, int, sqlite3_changeset_iter*),
3904d9151526Sdan   void *pCtx                      /* First argument passed to xConflict */
3905d9151526Sdan ){
3906d9151526Sdan   int rc = SQLITE_OK;
3907d9151526Sdan 
3908d9151526Sdan   while( pApply->constraints.nBuf ){
3909d9151526Sdan     sqlite3_changeset_iter *pIter2 = 0;
3910d9151526Sdan     SessionBuffer cons = pApply->constraints;
3911d9151526Sdan     memset(&pApply->constraints, 0, sizeof(SessionBuffer));
3912d9151526Sdan 
3913d9151526Sdan     rc = sessionChangesetStart(&pIter2, 0, 0, cons.nBuf, cons.aBuf);
3914d9151526Sdan     if( rc==SQLITE_OK ){
3915d9151526Sdan       int nByte = 2*pApply->nCol*sizeof(sqlite3_value*);
3916d9151526Sdan       int rc2;
3917d9151526Sdan       pIter2->bPatchset = bPatchset;
3918d9151526Sdan       pIter2->zTab = (char*)zTab;
3919d9151526Sdan       pIter2->nCol = pApply->nCol;
3920d9151526Sdan       pIter2->abPK = pApply->abPK;
3921d9151526Sdan       sessionBufferGrow(&pIter2->tblhdr, nByte, &rc);
3922d9151526Sdan       pIter2->apValue = (sqlite3_value**)pIter2->tblhdr.aBuf;
3923d9151526Sdan       if( rc==SQLITE_OK ) memset(pIter2->apValue, 0, nByte);
3924d9151526Sdan 
3925d9151526Sdan       while( rc==SQLITE_OK && SQLITE_ROW==sqlite3changeset_next(pIter2) ){
3926d9151526Sdan         rc = sessionApplyOneWithRetry(db, pIter2, pApply, xConflict, pCtx);
3927d9151526Sdan       }
3928d9151526Sdan 
3929d9151526Sdan       rc2 = sqlite3changeset_finalize(pIter2);
39307e0765a9Sdrh       if( rc==SQLITE_OK ) rc = rc2;
3931d9151526Sdan     }
3932d9151526Sdan     assert( pApply->bDeferConstraints || pApply->constraints.nBuf==0 );
3933d9151526Sdan 
3934d9151526Sdan     sqlite3_free(cons.aBuf);
3935d9151526Sdan     if( rc!=SQLITE_OK ) break;
3936d9151526Sdan     if( pApply->constraints.nBuf>=cons.nBuf ){
3937d9151526Sdan       /* No progress was made on the last round. */
3938d9151526Sdan       pApply->bDeferConstraints = 0;
3939d9151526Sdan     }
3940d9151526Sdan   }
3941d9151526Sdan 
3942d9151526Sdan   return rc;
3943d9151526Sdan }
3944d9151526Sdan 
3945296c7658Sdan /*
39464757c658Sdan ** Argument pIter is a changeset iterator that has been initialized, but
39474757c658Sdan ** not yet passed to sqlite3changeset_next(). This function applies the
39484757c658Sdan ** changeset to the main database attached to handle "db". The supplied
39494757c658Sdan ** conflict handler callback is invoked to resolve any conflicts encountered
39504757c658Sdan ** while applying the change.
3951296c7658Sdan */
39524757c658Sdan static int sessionChangesetApply(
3953296c7658Sdan   sqlite3 *db,                    /* Apply change to "main" db of this handle */
39544757c658Sdan   sqlite3_changeset_iter *pIter,  /* Changeset to apply */
395540368988Sdan   int(*xFilter)(
395640368988Sdan     void *pCtx,                   /* Copy of sixth arg to _apply() */
395740368988Sdan     const char *zTab              /* Table name */
395840368988Sdan   ),
3959d5f0767cSdan   int(*xConflict)(
3960d5f0767cSdan     void *pCtx,                   /* Copy of fifth arg to _apply() */
3961d5f0767cSdan     int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
3962d5f0767cSdan     sqlite3_changeset_iter *p     /* Handle describing change and conflict */
3963d5f0767cSdan   ),
3964296c7658Sdan   void *pCtx                      /* First argument passed to xConflict */
3965d5f0767cSdan ){
3966ca62ad57Sdan   int schemaMismatch = 0;
3967296c7658Sdan   int rc;                         /* Return code */
3968d5f0767cSdan   const char *zTab = 0;           /* Name of current table */
3969cfdbde21Sdrh   int nTab = 0;                   /* Result of sqlite3Strlen30(zTab) */
3970296c7658Sdan   SessionApplyCtx sApply;         /* changeset_apply() context object */
3971d5f0767cSdan 
3972082c96dfSdan   assert( xConflict!=0 );
3973082c96dfSdan 
3974d9151526Sdan   pIter->in.bNoDiscard = 1;
39750c698471Sdan   memset(&sApply, 0, sizeof(sApply));
39764c220252Sdan   sqlite3_mutex_enter(sqlite3_db_mutex(db));
39770c698471Sdan   rc = sqlite3_exec(db, "SAVEPOINT changeset_apply", 0, 0, 0);
3978cb3e4b79Sdan   if( rc==SQLITE_OK ){
3979cb3e4b79Sdan     rc = sqlite3_exec(db, "PRAGMA defer_foreign_keys = 1", 0, 0, 0);
3980cb3e4b79Sdan   }
39810c698471Sdan   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3changeset_next(pIter) ){
39820c698471Sdan     int nCol;
3983d5f0767cSdan     int op;
39840c698471Sdan     const char *zNew;
3985ca62ad57Sdan 
3986b4480e94Sdan     sqlite3changeset_op(pIter, &zNew, &nCol, &op, 0);
3987d5f0767cSdan 
39880c698471Sdan     if( zTab==0 || sqlite3_strnicmp(zNew, zTab, nTab+1) ){
3989ca62ad57Sdan       u8 *abPK;
3990ca62ad57Sdan 
3991d9151526Sdan       rc = sessionRetryConstraints(
3992d9151526Sdan           db, pIter->bPatchset, zTab, &sApply, xConflict, pCtx
3993d9151526Sdan       );
3994d9151526Sdan       if( rc!=SQLITE_OK ) break;
3995d9151526Sdan 
3996cfdbde21Sdrh       sqlite3_free((char*)sApply.azCol);  /* cast works around VC++ bug */
39970c698471Sdan       sqlite3_finalize(sApply.pDelete);
39980c698471Sdan       sqlite3_finalize(sApply.pUpdate);
39990c698471Sdan       sqlite3_finalize(sApply.pInsert);
40000c698471Sdan       sqlite3_finalize(sApply.pSelect);
40010c698471Sdan       memset(&sApply, 0, sizeof(sApply));
40020c698471Sdan       sApply.db = db;
4003d9151526Sdan       sApply.bDeferConstraints = 1;
400437f133ecSdan 
400540368988Sdan       /* If an xFilter() callback was specified, invoke it now. If the
400640368988Sdan       ** xFilter callback returns zero, skip this table. If it returns
400740368988Sdan       ** non-zero, proceed. */
400840368988Sdan       schemaMismatch = (xFilter && (0==xFilter(pCtx, zNew)));
400940368988Sdan       if( schemaMismatch ){
401040368988Sdan         zTab = sqlite3_mprintf("%s", zNew);
4011f05ac112Sdan         if( zTab==0 ){
4012f05ac112Sdan           rc = SQLITE_NOMEM;
4013f05ac112Sdan           break;
4014f05ac112Sdan         }
40154f528042Sdan         nTab = (int)strlen(zTab);
401640368988Sdan         sApply.azCol = (const char **)zTab;
401740368988Sdan       }else{
4018ca62ad57Sdan         sqlite3changeset_pk(pIter, &abPK, 0);
4019296c7658Sdan         rc = sessionTableInfo(
4020ca62ad57Sdan             db, "main", zNew, &sApply.nCol, &zTab, &sApply.azCol, &sApply.abPK
4021ca62ad57Sdan         );
4022ca62ad57Sdan         if( rc!=SQLITE_OK ) break;
40230c698471Sdan 
4024ca62ad57Sdan         if( sApply.nCol==0 ){
4025ca62ad57Sdan           schemaMismatch = 1;
4026ca62ad57Sdan           sqlite3_log(SQLITE_SCHEMA,
4027ca62ad57Sdan               "sqlite3changeset_apply(): no such table: %s", zTab
4028ca62ad57Sdan           );
4029ca62ad57Sdan         }
4030ca62ad57Sdan         else if( sApply.nCol!=nCol ){
4031ca62ad57Sdan           schemaMismatch = 1;
4032ca62ad57Sdan           sqlite3_log(SQLITE_SCHEMA,
4033ca62ad57Sdan               "sqlite3changeset_apply(): table %s has %d columns, expected %d",
4034ca62ad57Sdan               zTab, sApply.nCol, nCol
4035ca62ad57Sdan           );
4036ca62ad57Sdan         }
4037ca62ad57Sdan         else if( memcmp(sApply.abPK, abPK, nCol)!=0 ){
4038ca62ad57Sdan           schemaMismatch = 1;
403940368988Sdan           sqlite3_log(SQLITE_SCHEMA, "sqlite3changeset_apply(): "
404040368988Sdan               "primary key mismatch for table %s", zTab
4041ca62ad57Sdan           );
4042ca62ad57Sdan         }
4043ca62ad57Sdan         else if(
4044ca62ad57Sdan             (rc = sessionSelectRow(db, zTab, &sApply))
40450c698471Sdan          || (rc = sessionUpdateRow(db, zTab, &sApply))
40460c698471Sdan          || (rc = sessionDeleteRow(db, zTab, &sApply))
40470c698471Sdan          || (rc = sessionInsertRow(db, zTab, &sApply))
404837f133ecSdan         ){
404937f133ecSdan           break;
405037f133ecSdan         }
4051cfdbde21Sdrh         nTab = sqlite3Strlen30(zTab);
4052d5f0767cSdan       }
405340368988Sdan     }
4054d5f0767cSdan 
4055ca62ad57Sdan     /* If there is a schema mismatch on the current table, proceed to the
4056ca62ad57Sdan     ** next change. A log message has already been issued. */
4057ca62ad57Sdan     if( schemaMismatch ) continue;
4058ca62ad57Sdan 
4059d9151526Sdan     rc = sessionApplyOneWithRetry(db, pIter, &sApply, xConflict, pCtx);
40600c698471Sdan   }
40610c698471Sdan 
40620c698471Sdan   if( rc==SQLITE_OK ){
4063d9151526Sdan     rc = sessionRetryConstraints(
4064d9151526Sdan         db, pIter->bPatchset, zTab, &sApply, xConflict, pCtx
4065d9151526Sdan     );
40660c698471Sdan   }
4067d5f0767cSdan 
4068296c7658Sdan   if( rc==SQLITE_OK ){
4069296c7658Sdan     rc = sqlite3changeset_finalize(pIter);
4070296c7658Sdan   }else{
4071296c7658Sdan     sqlite3changeset_finalize(pIter);
4072296c7658Sdan   }
4073d5f0767cSdan 
4074d5f0767cSdan   if( rc==SQLITE_OK ){
407507001c45Sdrh     int nFk, notUsed;
407607001c45Sdrh     sqlite3_db_status(db, SQLITE_DBSTATUS_DEFERRED_FKS, &nFk, &notUsed, 0);
407707001c45Sdrh     if( nFk!=0 ){
4078cb3e4b79Sdan       int res = SQLITE_CHANGESET_ABORT;
4079cb3e4b79Sdan       sqlite3_changeset_iter sIter;
4080cb3e4b79Sdan       memset(&sIter, 0, sizeof(sIter));
4081cb3e4b79Sdan       sIter.nCol = nFk;
4082cb3e4b79Sdan       res = xConflict(pCtx, SQLITE_CHANGESET_FOREIGN_KEY, &sIter);
4083cb3e4b79Sdan       if( res!=SQLITE_CHANGESET_OMIT ){
4084cb3e4b79Sdan         rc = SQLITE_CONSTRAINT;
4085cb3e4b79Sdan       }
4086cb3e4b79Sdan     }
4087cb3e4b79Sdan   }
4088cb3e4b79Sdan   sqlite3_exec(db, "PRAGMA defer_foreign_keys = 0", 0, 0, 0);
4089cb3e4b79Sdan 
4090cb3e4b79Sdan   if( rc==SQLITE_OK ){
4091d5f0767cSdan     rc = sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0);
4092d5f0767cSdan   }else{
4093d5f0767cSdan     sqlite3_exec(db, "ROLLBACK TO changeset_apply", 0, 0, 0);
4094d5f0767cSdan     sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0);
4095d5f0767cSdan   }
4096d5f0767cSdan 
40970c698471Sdan   sqlite3_finalize(sApply.pInsert);
40980c698471Sdan   sqlite3_finalize(sApply.pDelete);
40990c698471Sdan   sqlite3_finalize(sApply.pUpdate);
41000c698471Sdan   sqlite3_finalize(sApply.pSelect);
4101cfdbde21Sdrh   sqlite3_free((char*)sApply.azCol);  /* cast works around VC++ bug */
4102d9151526Sdan   sqlite3_free((char*)sApply.constraints.aBuf);
41034c220252Sdan   sqlite3_mutex_leave(sqlite3_db_mutex(db));
4104d5f0767cSdan   return rc;
4105d5f0767cSdan }
410691ddd559Sdan 
410777fc1d5bSdan /*
41084757c658Sdan ** Apply the changeset passed via pChangeset/nChangeset to the main database
41094757c658Sdan ** attached to handle "db". Invoke the supplied conflict handler callback
41104757c658Sdan ** to resolve any conflicts encountered while applying the change.
41114757c658Sdan */
41124757c658Sdan int sqlite3changeset_apply(
41134757c658Sdan   sqlite3 *db,                    /* Apply change to "main" db of this handle */
41144757c658Sdan   int nChangeset,                 /* Size of changeset in bytes */
41154757c658Sdan   void *pChangeset,               /* Changeset blob */
41164757c658Sdan   int(*xFilter)(
41174757c658Sdan     void *pCtx,                   /* Copy of sixth arg to _apply() */
41184757c658Sdan     const char *zTab              /* Table name */
41194757c658Sdan   ),
41204757c658Sdan   int(*xConflict)(
41214757c658Sdan     void *pCtx,                   /* Copy of fifth arg to _apply() */
41224757c658Sdan     int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
41234757c658Sdan     sqlite3_changeset_iter *p     /* Handle describing change and conflict */
41244757c658Sdan   ),
41254757c658Sdan   void *pCtx                      /* First argument passed to xConflict */
41264757c658Sdan ){
41274757c658Sdan   sqlite3_changeset_iter *pIter;  /* Iterator to skip through changeset */
41284757c658Sdan   int rc = sqlite3changeset_start(&pIter, nChangeset, pChangeset);
41294757c658Sdan   if( rc==SQLITE_OK ){
41304757c658Sdan     rc = sessionChangesetApply(db, pIter, xFilter, xConflict, pCtx);
41314757c658Sdan   }
41324757c658Sdan   return rc;
41334757c658Sdan }
41344757c658Sdan 
41354757c658Sdan /*
41364757c658Sdan ** Apply the changeset passed via xInput/pIn to the main database
41374757c658Sdan ** attached to handle "db". Invoke the supplied conflict handler callback
41384757c658Sdan ** to resolve any conflicts encountered while applying the change.
41394757c658Sdan */
4140f1a08ad8Sdrh int sqlite3changeset_apply_strm(
41414757c658Sdan   sqlite3 *db,                    /* Apply change to "main" db of this handle */
41424757c658Sdan   int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
41434757c658Sdan   void *pIn,                                          /* First arg for xInput */
41444757c658Sdan   int(*xFilter)(
41454757c658Sdan     void *pCtx,                   /* Copy of sixth arg to _apply() */
41464757c658Sdan     const char *zTab              /* Table name */
41474757c658Sdan   ),
41484757c658Sdan   int(*xConflict)(
41494757c658Sdan     void *pCtx,                   /* Copy of sixth arg to _apply() */
41504757c658Sdan     int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
41514757c658Sdan     sqlite3_changeset_iter *p     /* Handle describing change and conflict */
41524757c658Sdan   ),
41534757c658Sdan   void *pCtx                      /* First argument passed to xConflict */
41544757c658Sdan ){
41554757c658Sdan   sqlite3_changeset_iter *pIter;  /* Iterator to skip through changeset */
4156f1a08ad8Sdrh   int rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
41574757c658Sdan   if( rc==SQLITE_OK ){
41584757c658Sdan     rc = sessionChangesetApply(db, pIter, xFilter, xConflict, pCtx);
41594757c658Sdan   }
41604757c658Sdan   return rc;
41614757c658Sdan }
41624757c658Sdan 
41634757c658Sdan /*
41645898ad69Sdan ** sqlite3_changegroup handle.
41655898ad69Sdan */
41665898ad69Sdan struct sqlite3_changegroup {
41675898ad69Sdan   int rc;                         /* Error code */
41685898ad69Sdan   int bPatch;                     /* True to accumulate patchsets */
41695898ad69Sdan   SessionTable *pList;            /* List of tables in current patch */
41705898ad69Sdan };
41715898ad69Sdan 
41725898ad69Sdan /*
417377fc1d5bSdan ** This function is called to merge two changes to the same row together as
417477fc1d5bSdan ** part of an sqlite3changeset_concat() operation. A new change object is
417577fc1d5bSdan ** allocated and a pointer to it stored in *ppNew.
417677fc1d5bSdan */
41775d607a6eSdan static int sessionChangeMerge(
417877fc1d5bSdan   SessionTable *pTab,             /* Table structure */
417964277f4aSdan   int bPatchset,                  /* True for patchsets */
418077fc1d5bSdan   SessionChange *pExist,          /* Existing change */
418177fc1d5bSdan   int op2,                        /* Second change operation */
418277fc1d5bSdan   int bIndirect,                  /* True if second change is indirect */
418377fc1d5bSdan   u8 *aRec,                       /* Second change record */
418477fc1d5bSdan   int nRec,                       /* Number of bytes in aRec */
418577fc1d5bSdan   SessionChange **ppNew           /* OUT: Merged change */
41865d607a6eSdan ){
41875d607a6eSdan   SessionChange *pNew = 0;
41885d607a6eSdan 
41895d607a6eSdan   if( !pExist ){
4190cbf6d2d2Sdan     pNew = (SessionChange *)sqlite3_malloc(sizeof(SessionChange) + nRec);
41915d607a6eSdan     if( !pNew ){
41925d607a6eSdan       return SQLITE_NOMEM;
41935d607a6eSdan     }
41945d607a6eSdan     memset(pNew, 0, sizeof(SessionChange));
4195798693b2Sdan     pNew->op = op2;
41965d607a6eSdan     pNew->bIndirect = bIndirect;
41975d607a6eSdan     pNew->nRecord = nRec;
4198cbf6d2d2Sdan     pNew->aRecord = (u8*)&pNew[1];
4199cbf6d2d2Sdan     memcpy(pNew->aRecord, aRec, nRec);
42005d607a6eSdan   }else{
4201798693b2Sdan     int op1 = pExist->op;
42025d607a6eSdan 
42035d607a6eSdan     /*
42045d607a6eSdan     **   op1=INSERT, op2=INSERT      ->      Unsupported. Discard op2.
42055d607a6eSdan     **   op1=INSERT, op2=UPDATE      ->      INSERT.
42065d607a6eSdan     **   op1=INSERT, op2=DELETE      ->      (none)
42075d607a6eSdan     **
42085d607a6eSdan     **   op1=UPDATE, op2=INSERT      ->      Unsupported. Discard op2.
42095d607a6eSdan     **   op1=UPDATE, op2=UPDATE      ->      UPDATE.
42105d607a6eSdan     **   op1=UPDATE, op2=DELETE      ->      DELETE.
42115d607a6eSdan     **
42125d607a6eSdan     **   op1=DELETE, op2=INSERT      ->      UPDATE.
42135d607a6eSdan     **   op1=DELETE, op2=UPDATE      ->      Unsupported. Discard op2.
42145d607a6eSdan     **   op1=DELETE, op2=DELETE      ->      Unsupported. Discard op2.
42155d607a6eSdan     */
42165d607a6eSdan     if( (op1==SQLITE_INSERT && op2==SQLITE_INSERT)
42175d607a6eSdan      || (op1==SQLITE_UPDATE && op2==SQLITE_INSERT)
42185d607a6eSdan      || (op1==SQLITE_DELETE && op2==SQLITE_UPDATE)
42195d607a6eSdan      || (op1==SQLITE_DELETE && op2==SQLITE_DELETE)
42205d607a6eSdan     ){
42215d607a6eSdan       pNew = pExist;
42225d607a6eSdan     }else if( op1==SQLITE_INSERT && op2==SQLITE_DELETE ){
42235d607a6eSdan       sqlite3_free(pExist);
42245d607a6eSdan       assert( pNew==0 );
42255d607a6eSdan     }else{
422664277f4aSdan       u8 *aExist = pExist->aRecord;
42275d607a6eSdan       int nByte;
42285d607a6eSdan       u8 *aCsr;
42295d607a6eSdan 
423064277f4aSdan       /* Allocate a new SessionChange object. Ensure that the aRecord[]
423164277f4aSdan       ** buffer of the new object is large enough to hold any record that
423264277f4aSdan       ** may be generated by combining the input records.  */
42335d607a6eSdan       nByte = sizeof(SessionChange) + pExist->nRecord + nRec;
42345d607a6eSdan       pNew = (SessionChange *)sqlite3_malloc(nByte);
42355d607a6eSdan       if( !pNew ){
42361756ae10Sdan         sqlite3_free(pExist);
42375d607a6eSdan         return SQLITE_NOMEM;
42385d607a6eSdan       }
42395d607a6eSdan       memset(pNew, 0, sizeof(SessionChange));
42405d607a6eSdan       pNew->bIndirect = (bIndirect && pExist->bIndirect);
42415d607a6eSdan       aCsr = pNew->aRecord = (u8 *)&pNew[1];
42425d607a6eSdan 
4243b08a1efaSdan       if( op1==SQLITE_INSERT ){             /* INSERT + UPDATE */
42445d607a6eSdan         u8 *a1 = aRec;
4245b08a1efaSdan         assert( op2==SQLITE_UPDATE );
4246798693b2Sdan         pNew->op = SQLITE_INSERT;
4247ef7a6304Sdan         if( bPatchset==0 ) sessionSkipRecord(&a1, pTab->nCol);
424864277f4aSdan         sessionMergeRecord(&aCsr, pTab->nCol, aExist, a1);
4249b08a1efaSdan       }else if( op1==SQLITE_DELETE ){       /* DELETE + INSERT */
4250b08a1efaSdan         assert( op2==SQLITE_INSERT );
4251798693b2Sdan         pNew->op = SQLITE_UPDATE;
4252fa29ecc4Sdan         if( bPatchset ){
4253fa29ecc4Sdan           memcpy(aCsr, aRec, nRec);
4254fa29ecc4Sdan           aCsr += nRec;
4255fa29ecc4Sdan         }else{
425664277f4aSdan           if( 0==sessionMergeUpdate(&aCsr, pTab, bPatchset, aExist, 0,aRec,0) ){
4257b08a1efaSdan             sqlite3_free(pNew);
4258b08a1efaSdan             pNew = 0;
42595d607a6eSdan           }
4260fa29ecc4Sdan         }
4261b08a1efaSdan       }else if( op2==SQLITE_UPDATE ){       /* UPDATE + UPDATE */
426264277f4aSdan         u8 *a1 = aExist;
42635d607a6eSdan         u8 *a2 = aRec;
4264cfec7eeeSdan         assert( op1==SQLITE_UPDATE );
426564277f4aSdan         if( bPatchset==0 ){
4266ef7a6304Sdan           sessionSkipRecord(&a1, pTab->nCol);
4267ef7a6304Sdan           sessionSkipRecord(&a2, pTab->nCol);
426864277f4aSdan         }
4269798693b2Sdan         pNew->op = SQLITE_UPDATE;
427064277f4aSdan         if( 0==sessionMergeUpdate(&aCsr, pTab, bPatchset, aRec, aExist,a1,a2) ){
42715d607a6eSdan           sqlite3_free(pNew);
42725d607a6eSdan           pNew = 0;
42735d607a6eSdan         }
4274b08a1efaSdan       }else{                                /* UPDATE + DELETE */
4275b08a1efaSdan         assert( op1==SQLITE_UPDATE && op2==SQLITE_DELETE );
4276798693b2Sdan         pNew->op = SQLITE_DELETE;
427764277f4aSdan         if( bPatchset ){
427864277f4aSdan           memcpy(aCsr, aRec, nRec);
427964277f4aSdan           aCsr += nRec;
428064277f4aSdan         }else{
428164277f4aSdan           sessionMergeRecord(&aCsr, pTab->nCol, aRec, aExist);
428264277f4aSdan         }
42835d607a6eSdan       }
42845d607a6eSdan 
42855d607a6eSdan       if( pNew ){
42864f528042Sdan         pNew->nRecord = (int)(aCsr - pNew->aRecord);
42875d607a6eSdan       }
42885d607a6eSdan       sqlite3_free(pExist);
42895d607a6eSdan     }
42905d607a6eSdan   }
42915d607a6eSdan 
42925d607a6eSdan   *ppNew = pNew;
42935d607a6eSdan   return SQLITE_OK;
42945d607a6eSdan }
42955d607a6eSdan 
429677fc1d5bSdan /*
42975898ad69Sdan ** Add all changes in the changeset traversed by the iterator passed as
42985898ad69Sdan ** the first argument to the changegroup hash tables.
429977fc1d5bSdan */
430016228167Sdan static int sessionChangesetToHash(
4301cbf6d2d2Sdan   sqlite3_changeset_iter *pIter,   /* Iterator to read from */
43025898ad69Sdan   sqlite3_changegroup *pGrp        /* Changegroup object to add changeset to */
43035d607a6eSdan ){
43045d607a6eSdan   u8 *aRec;
43055d607a6eSdan   int nRec;
4306cbf6d2d2Sdan   int rc = SQLITE_OK;
43075d607a6eSdan   SessionTable *pTab = 0;
43085d607a6eSdan 
43095898ad69Sdan 
43105d607a6eSdan   while( SQLITE_ROW==sessionChangesetNext(pIter, &aRec, &nRec) ){
43115d607a6eSdan     const char *zNew;
43125d607a6eSdan     int nCol;
43135d607a6eSdan     int op;
43145d607a6eSdan     int iHash;
43155d607a6eSdan     int bIndirect;
43165d607a6eSdan     SessionChange *pChange;
43175d607a6eSdan     SessionChange *pExist = 0;
43185d607a6eSdan     SessionChange **pp;
43195d607a6eSdan 
43205898ad69Sdan     if( pGrp->pList==0 ){
43215898ad69Sdan       pGrp->bPatch = pIter->bPatchset;
43225898ad69Sdan     }else if( pIter->bPatchset!=pGrp->bPatch ){
43235898ad69Sdan       rc = SQLITE_ERROR;
43245898ad69Sdan       break;
43255898ad69Sdan     }
43265898ad69Sdan 
43275d607a6eSdan     sqlite3changeset_op(pIter, &zNew, &nCol, &op, &bIndirect);
4328ef7a6304Sdan     if( !pTab || sqlite3_stricmp(zNew, pTab->zName) ){
43295d607a6eSdan       /* Search the list for a matching table */
43304f528042Sdan       int nNew = (int)strlen(zNew);
4331f29123b5Sdan       u8 *abPK;
4332f29123b5Sdan 
4333f29123b5Sdan       sqlite3changeset_pk(pIter, &abPK, 0);
43345898ad69Sdan       for(pTab = pGrp->pList; pTab; pTab=pTab->pNext){
43355d607a6eSdan         if( 0==sqlite3_strnicmp(pTab->zName, zNew, nNew+1) ) break;
43365d607a6eSdan       }
43375d607a6eSdan       if( !pTab ){
43386c39e6a8Sdan         SessionTable **ppTab;
43396c39e6a8Sdan 
4340ef7a6304Sdan         pTab = sqlite3_malloc(sizeof(SessionTable) + nCol + nNew+1);
4341f29123b5Sdan         if( !pTab ){
4342f29123b5Sdan           rc = SQLITE_NOMEM;
4343f29123b5Sdan           break;
4344f29123b5Sdan         }
43455d607a6eSdan         memset(pTab, 0, sizeof(SessionTable));
4346f29123b5Sdan         pTab->nCol = nCol;
4347ef7a6304Sdan         pTab->abPK = (u8*)&pTab[1];
4348ef7a6304Sdan         memcpy(pTab->abPK, abPK, nCol);
4349ef7a6304Sdan         pTab->zName = (char*)&pTab->abPK[nCol];
4350ef7a6304Sdan         memcpy(pTab->zName, zNew, nNew+1);
43516c39e6a8Sdan 
43526c39e6a8Sdan         /* The new object must be linked on to the end of the list, not
43536c39e6a8Sdan         ** simply added to the start of it. This is to ensure that the
43546c39e6a8Sdan         ** tables within the output of sqlite3changegroup_output() are in
43556c39e6a8Sdan         ** the right order.  */
43566c39e6a8Sdan         for(ppTab=&pGrp->pList; *ppTab; ppTab=&(*ppTab)->pNext);
43576c39e6a8Sdan         *ppTab = pTab;
4358f29123b5Sdan       }else if( pTab->nCol!=nCol || memcmp(pTab->abPK, abPK, nCol) ){
4359f29123b5Sdan         rc = SQLITE_SCHEMA;
4360f29123b5Sdan         break;
43615d607a6eSdan       }
43625d607a6eSdan     }
43635d607a6eSdan 
4364cbf6d2d2Sdan     if( sessionGrowHash(pIter->bPatchset, pTab) ){
43651756ae10Sdan       rc = SQLITE_NOMEM;
43661756ae10Sdan       break;
43671756ae10Sdan     }
436864277f4aSdan     iHash = sessionChangeHash(
4369cbf6d2d2Sdan         pTab, (pIter->bPatchset && op==SQLITE_DELETE), aRec, pTab->nChange
437064277f4aSdan     );
43715d607a6eSdan 
43725d607a6eSdan     /* Search for existing entry. If found, remove it from the hash table.
43735d607a6eSdan     ** Code below may link it back in.
43745d607a6eSdan     */
43755d607a6eSdan     for(pp=&pTab->apChange[iHash]; *pp; pp=&(*pp)->pNext){
437664277f4aSdan       int bPkOnly1 = 0;
437764277f4aSdan       int bPkOnly2 = 0;
4378cbf6d2d2Sdan       if( pIter->bPatchset ){
437964277f4aSdan         bPkOnly1 = (*pp)->op==SQLITE_DELETE;
438064277f4aSdan         bPkOnly2 = op==SQLITE_DELETE;
438164277f4aSdan       }
438264277f4aSdan       if( sessionChangeEqual(pTab, bPkOnly1, (*pp)->aRecord, bPkOnly2, aRec) ){
43835d607a6eSdan         pExist = *pp;
43845d607a6eSdan         *pp = (*pp)->pNext;
43855d607a6eSdan         pTab->nEntry--;
43865d607a6eSdan         break;
43875d607a6eSdan       }
43885d607a6eSdan     }
43895d607a6eSdan 
439064277f4aSdan     rc = sessionChangeMerge(pTab,
4391cbf6d2d2Sdan         pIter->bPatchset, pExist, op, bIndirect, aRec, nRec, &pChange
439264277f4aSdan     );
43935d607a6eSdan     if( rc ) break;
43945d607a6eSdan     if( pChange ){
43955d607a6eSdan       pChange->pNext = pTab->apChange[iHash];
43965d607a6eSdan       pTab->apChange[iHash] = pChange;
43975d607a6eSdan       pTab->nEntry++;
43985d607a6eSdan     }
43995d607a6eSdan   }
44005d607a6eSdan 
4401cbf6d2d2Sdan   if( rc==SQLITE_OK ) rc = pIter->rc;
44025d607a6eSdan   return rc;
44035d607a6eSdan }
44045d607a6eSdan 
44055d607a6eSdan /*
44065898ad69Sdan ** Serialize a changeset (or patchset) based on all changesets (or patchsets)
44075898ad69Sdan ** added to the changegroup object passed as the first argument.
44085d607a6eSdan **
44095898ad69Sdan ** If xOutput is not NULL, then the changeset/patchset is returned to the
44105898ad69Sdan ** user via one or more calls to xOutput, as with the other streaming
44115898ad69Sdan ** interfaces.
44125d607a6eSdan **
44135898ad69Sdan ** Or, if xOutput is NULL, then (*ppOut) is populated with a pointer to a
44145898ad69Sdan ** buffer containing the output changeset before this function returns. In
44155898ad69Sdan ** this case (*pnOut) is set to the size of the output buffer in bytes. It
44165898ad69Sdan ** is the responsibility of the caller to free the output buffer using
44175898ad69Sdan ** sqlite3_free() when it is no longer required.
44185898ad69Sdan **
44195898ad69Sdan ** If successful, SQLITE_OK is returned. Or, if an error occurs, an SQLite
44205898ad69Sdan ** error code. If an error occurs and xOutput is NULL, (*ppOut) and (*pnOut)
44215898ad69Sdan ** are both set to 0 before returning.
44225d607a6eSdan */
44235898ad69Sdan static int sessionChangegroupOutput(
44245898ad69Sdan   sqlite3_changegroup *pGrp,
4425cbf6d2d2Sdan   int (*xOutput)(void *pOut, const void *pData, int nData),
4426cbf6d2d2Sdan   void *pOut,
4427cbf6d2d2Sdan   int *pnOut,
4428cbf6d2d2Sdan   void **ppOut
44295d607a6eSdan ){
44305898ad69Sdan   int rc = SQLITE_OK;
4431e8fa8c96Sdan   SessionBuffer buf = {0, 0, 0};
44325898ad69Sdan   SessionTable *pTab;
4433cbf6d2d2Sdan   assert( xOutput==0 || (ppOut==0 && pnOut==0) );
44345d607a6eSdan 
44355d607a6eSdan   /* Create the serialized output changeset based on the contents of the
44365898ad69Sdan   ** hash tables attached to the SessionTable objects in list p->pList.
44375d607a6eSdan   */
44385898ad69Sdan   for(pTab=pGrp->pList; rc==SQLITE_OK && pTab; pTab=pTab->pNext){
44395d607a6eSdan     int i;
44405d607a6eSdan     if( pTab->nEntry==0 ) continue;
44415d607a6eSdan 
44425898ad69Sdan     sessionAppendTableHdr(&buf, pGrp->bPatch, pTab, &rc);
44435d607a6eSdan     for(i=0; i<pTab->nChange; i++){
44445d607a6eSdan       SessionChange *p;
44455d607a6eSdan       for(p=pTab->apChange[i]; p; p=p->pNext){
4446798693b2Sdan         sessionAppendByte(&buf, p->op, &rc);
44475d607a6eSdan         sessionAppendByte(&buf, p->bIndirect, &rc);
44485d607a6eSdan         sessionAppendBlob(&buf, p->aRecord, p->nRecord, &rc);
44495d607a6eSdan       }
44505d607a6eSdan     }
4451cbf6d2d2Sdan 
4452f1a08ad8Sdrh     if( rc==SQLITE_OK && xOutput && buf.nBuf>=SESSIONS_STRM_CHUNK_SIZE ){
4453cbf6d2d2Sdan       rc = xOutput(pOut, buf.aBuf, buf.nBuf);
4454cbf6d2d2Sdan       buf.nBuf = 0;
4455cbf6d2d2Sdan     }
44565d607a6eSdan   }
44575d607a6eSdan 
44585d607a6eSdan   if( rc==SQLITE_OK ){
4459cbf6d2d2Sdan     if( xOutput ){
4460cbf6d2d2Sdan       if( buf.nBuf>0 ) rc = xOutput(pOut, buf.aBuf, buf.nBuf);
4461cbf6d2d2Sdan     }else{
44625d607a6eSdan       *ppOut = buf.aBuf;
44635d607a6eSdan       *pnOut = buf.nBuf;
4464cbf6d2d2Sdan       buf.aBuf = 0;
44655d607a6eSdan     }
44665d607a6eSdan   }
4467cbf6d2d2Sdan   sqlite3_free(buf.aBuf);
44685d607a6eSdan 
44695d607a6eSdan   return rc;
44705d607a6eSdan }
44715d607a6eSdan 
4472cbf6d2d2Sdan /*
44735898ad69Sdan ** Allocate a new, empty, sqlite3_changegroup.
44745898ad69Sdan */
44755898ad69Sdan int sqlite3changegroup_new(sqlite3_changegroup **pp){
44765898ad69Sdan   int rc = SQLITE_OK;             /* Return code */
44775898ad69Sdan   sqlite3_changegroup *p;         /* New object */
44785898ad69Sdan   p = (sqlite3_changegroup*)sqlite3_malloc(sizeof(sqlite3_changegroup));
44795898ad69Sdan   if( p==0 ){
44805898ad69Sdan     rc = SQLITE_NOMEM;
44815898ad69Sdan   }else{
44825898ad69Sdan     memset(p, 0, sizeof(sqlite3_changegroup));
44835898ad69Sdan   }
44845898ad69Sdan   *pp = p;
44855898ad69Sdan   return rc;
44865898ad69Sdan }
44875898ad69Sdan 
44885898ad69Sdan /*
44895898ad69Sdan ** Add the changeset currently stored in buffer pData, size nData bytes,
44905898ad69Sdan ** to changeset-group p.
44915898ad69Sdan */
44925898ad69Sdan int sqlite3changegroup_add(sqlite3_changegroup *pGrp, int nData, void *pData){
44935898ad69Sdan   sqlite3_changeset_iter *pIter;  /* Iterator opened on pData/nData */
44945898ad69Sdan   int rc;                         /* Return code */
44955898ad69Sdan 
44965898ad69Sdan   rc = sqlite3changeset_start(&pIter, nData, pData);
44975898ad69Sdan   if( rc==SQLITE_OK ){
44985898ad69Sdan     rc = sessionChangesetToHash(pIter, pGrp);
44995898ad69Sdan   }
45005898ad69Sdan   sqlite3changeset_finalize(pIter);
45015898ad69Sdan   return rc;
45025898ad69Sdan }
45035898ad69Sdan 
45045898ad69Sdan /*
45055898ad69Sdan ** Obtain a buffer containing a changeset representing the concatenation
45065898ad69Sdan ** of all changesets added to the group so far.
45075898ad69Sdan */
45085898ad69Sdan int sqlite3changegroup_output(
45095898ad69Sdan     sqlite3_changegroup *pGrp,
45105898ad69Sdan     int *pnData,
45115898ad69Sdan     void **ppData
45125898ad69Sdan ){
45135898ad69Sdan   return sessionChangegroupOutput(pGrp, 0, 0, pnData, ppData);
45145898ad69Sdan }
45155898ad69Sdan 
45165898ad69Sdan /*
45175898ad69Sdan ** Streaming versions of changegroup_add().
45185898ad69Sdan */
45195898ad69Sdan int sqlite3changegroup_add_strm(
45205898ad69Sdan   sqlite3_changegroup *pGrp,
45215898ad69Sdan   int (*xInput)(void *pIn, void *pData, int *pnData),
45225898ad69Sdan   void *pIn
45235898ad69Sdan ){
45245898ad69Sdan   sqlite3_changeset_iter *pIter;  /* Iterator opened on pData/nData */
45255898ad69Sdan   int rc;                         /* Return code */
45265898ad69Sdan 
45275898ad69Sdan   rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
45285898ad69Sdan   if( rc==SQLITE_OK ){
45295898ad69Sdan     rc = sessionChangesetToHash(pIter, pGrp);
45305898ad69Sdan   }
45315898ad69Sdan   sqlite3changeset_finalize(pIter);
45325898ad69Sdan   return rc;
45335898ad69Sdan }
45345898ad69Sdan 
45355898ad69Sdan /*
45365898ad69Sdan ** Streaming versions of changegroup_output().
45375898ad69Sdan */
45385898ad69Sdan int sqlite3changegroup_output_strm(
45395898ad69Sdan   sqlite3_changegroup *pGrp,
45405898ad69Sdan   int (*xOutput)(void *pOut, const void *pData, int nData),
45415898ad69Sdan   void *pOut
45425898ad69Sdan ){
45435898ad69Sdan   return sessionChangegroupOutput(pGrp, xOutput, pOut, 0, 0);
45445898ad69Sdan }
45455898ad69Sdan 
45465898ad69Sdan /*
45475898ad69Sdan ** Delete a changegroup object.
45485898ad69Sdan */
45495898ad69Sdan void sqlite3changegroup_delete(sqlite3_changegroup *pGrp){
45505898ad69Sdan   if( pGrp ){
45515898ad69Sdan     sessionDeleteTable(pGrp->pList);
45525898ad69Sdan     sqlite3_free(pGrp);
45535898ad69Sdan   }
45545898ad69Sdan }
45555898ad69Sdan 
45565898ad69Sdan /*
4557cbf6d2d2Sdan ** Combine two changesets together.
4558cbf6d2d2Sdan */
4559cbf6d2d2Sdan int sqlite3changeset_concat(
4560cbf6d2d2Sdan   int nLeft,                      /* Number of bytes in lhs input */
4561cbf6d2d2Sdan   void *pLeft,                    /* Lhs input changeset */
4562cbf6d2d2Sdan   int nRight                      /* Number of bytes in rhs input */,
4563cbf6d2d2Sdan   void *pRight,                   /* Rhs input changeset */
4564cbf6d2d2Sdan   int *pnOut,                     /* OUT: Number of bytes in output changeset */
4565cbf6d2d2Sdan   void **ppOut                    /* OUT: changeset (left <concat> right) */
4566cbf6d2d2Sdan ){
45675898ad69Sdan   sqlite3_changegroup *pGrp;
4568cbf6d2d2Sdan   int rc;
4569cbf6d2d2Sdan 
45705898ad69Sdan   rc = sqlite3changegroup_new(&pGrp);
4571cbf6d2d2Sdan   if( rc==SQLITE_OK ){
45725898ad69Sdan     rc = sqlite3changegroup_add(pGrp, nLeft, pLeft);
4573cbf6d2d2Sdan   }
4574cbf6d2d2Sdan   if( rc==SQLITE_OK ){
45755898ad69Sdan     rc = sqlite3changegroup_add(pGrp, nRight, pRight);
4576cbf6d2d2Sdan   }
45775898ad69Sdan   if( rc==SQLITE_OK ){
45785898ad69Sdan     rc = sqlite3changegroup_output(pGrp, pnOut, ppOut);
45795898ad69Sdan   }
45805898ad69Sdan   sqlite3changegroup_delete(pGrp);
4581cbf6d2d2Sdan 
4582cbf6d2d2Sdan   return rc;
4583cbf6d2d2Sdan }
4584cbf6d2d2Sdan 
4585cbf6d2d2Sdan /*
4586cbf6d2d2Sdan ** Streaming version of sqlite3changeset_concat().
4587cbf6d2d2Sdan */
4588f1a08ad8Sdrh int sqlite3changeset_concat_strm(
4589cbf6d2d2Sdan   int (*xInputA)(void *pIn, void *pData, int *pnData),
4590cbf6d2d2Sdan   void *pInA,
4591cbf6d2d2Sdan   int (*xInputB)(void *pIn, void *pData, int *pnData),
4592cbf6d2d2Sdan   void *pInB,
4593cbf6d2d2Sdan   int (*xOutput)(void *pOut, const void *pData, int nData),
4594cbf6d2d2Sdan   void *pOut
4595cbf6d2d2Sdan ){
45965898ad69Sdan   sqlite3_changegroup *pGrp;
4597cbf6d2d2Sdan   int rc;
4598cbf6d2d2Sdan 
45995898ad69Sdan   rc = sqlite3changegroup_new(&pGrp);
4600cbf6d2d2Sdan   if( rc==SQLITE_OK ){
46015898ad69Sdan     rc = sqlite3changegroup_add_strm(pGrp, xInputA, pInA);
4602cbf6d2d2Sdan   }
4603cbf6d2d2Sdan   if( rc==SQLITE_OK ){
46045898ad69Sdan     rc = sqlite3changegroup_add_strm(pGrp, xInputB, pInB);
4605cbf6d2d2Sdan   }
46065898ad69Sdan   if( rc==SQLITE_OK ){
46075898ad69Sdan     rc = sqlite3changegroup_output_strm(pGrp, xOutput, pOut);
46085898ad69Sdan   }
46095898ad69Sdan   sqlite3changegroup_delete(pGrp);
4610cbf6d2d2Sdan 
4611cbf6d2d2Sdan   return rc;
4612cbf6d2d2Sdan }
4613cbf6d2d2Sdan 
46149b1c62d4Sdrh #endif /* SQLITE_ENABLE_SESSION && SQLITE_ENABLE_PREUPDATE_HOOK */
4615