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 **
180c8be6437Sdrh **   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 **
222c8be6437Sdrh **   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 
28148cd59a5Sdrh /* Load an unaligned and unsigned 32-bit integer */
28248cd59a5Sdrh #define SESSION_UINT32(x) (((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
28348cd59a5Sdrh 
284296c7658Sdan /*
285296c7658Sdan ** Read a 64-bit big-endian integer value from buffer aRec[]. Return
286296c7658Sdan ** the value read.
287296c7658Sdan */
2884fccf43aSdan static sqlite3_int64 sessionGetI64(u8 *aRec){
28948cd59a5Sdrh   u64 x = SESSION_UINT32(aRec);
29048cd59a5Sdrh   u32 y = SESSION_UINT32(aRec+4);
29148cd59a5Sdrh   x = (x<<32) + y;
29248cd59a5Sdrh   return (sqlite3_int64)x;
2934fccf43aSdan }
2944fccf43aSdan 
2954fccf43aSdan /*
296296c7658Sdan ** Write a 64-bit big-endian integer value to the buffer aBuf[].
297296c7658Sdan */
298296c7658Sdan static void sessionPutI64(u8 *aBuf, sqlite3_int64 i){
299296c7658Sdan   aBuf[0] = (i>>56) & 0xFF;
300296c7658Sdan   aBuf[1] = (i>>48) & 0xFF;
301296c7658Sdan   aBuf[2] = (i>>40) & 0xFF;
302296c7658Sdan   aBuf[3] = (i>>32) & 0xFF;
303296c7658Sdan   aBuf[4] = (i>>24) & 0xFF;
304296c7658Sdan   aBuf[5] = (i>>16) & 0xFF;
305296c7658Sdan   aBuf[6] = (i>> 8) & 0xFF;
306296c7658Sdan   aBuf[7] = (i>> 0) & 0xFF;
307296c7658Sdan }
308296c7658Sdan 
309296c7658Sdan /*
3104fccf43aSdan ** This function is used to serialize the contents of value pValue (see
3114fccf43aSdan ** comment titled "RECORD FORMAT" above).
3124fccf43aSdan **
3134fccf43aSdan ** If it is non-NULL, the serialized form of the value is written to
3144fccf43aSdan ** buffer aBuf. *pnWrite is set to the number of bytes written before
3154fccf43aSdan ** returning. Or, if aBuf is NULL, the only thing this function does is
3164fccf43aSdan ** set *pnWrite.
3174fccf43aSdan **
3184fccf43aSdan ** If no error occurs, SQLITE_OK is returned. Or, if an OOM error occurs
3194fccf43aSdan ** within a call to sqlite3_value_text() (may fail if the db is utf-16))
3204fccf43aSdan ** SQLITE_NOMEM is returned.
3214fccf43aSdan */
3224fccf43aSdan static int sessionSerializeValue(
3234fccf43aSdan   u8 *aBuf,                       /* If non-NULL, write serialized value here */
3244fccf43aSdan   sqlite3_value *pValue,          /* Value to serialize */
3254fccf43aSdan   int *pnWrite                    /* IN/OUT: Increment by bytes written */
3264fccf43aSdan ){
327296c7658Sdan   int nByte;                      /* Size of serialized value in bytes */
3284fccf43aSdan 
32980fe2d93Sdan   if( pValue ){
33080fe2d93Sdan     int eType;                    /* Value type (SQLITE_NULL, TEXT etc.) */
33180fe2d93Sdan 
3324fccf43aSdan     eType = sqlite3_value_type(pValue);
3334fccf43aSdan     if( aBuf ) aBuf[0] = eType;
3344fccf43aSdan 
3354fccf43aSdan     switch( eType ){
3364fccf43aSdan       case SQLITE_NULL:
3374fccf43aSdan         nByte = 1;
3384fccf43aSdan         break;
3394fccf43aSdan 
3404fccf43aSdan       case SQLITE_INTEGER:
3414fccf43aSdan       case SQLITE_FLOAT:
3424fccf43aSdan         if( aBuf ){
3434fccf43aSdan           /* TODO: SQLite does something special to deal with mixed-endian
3444fccf43aSdan           ** floating point values (e.g. ARM7). This code probably should
3454fccf43aSdan           ** too.  */
3464fccf43aSdan           u64 i;
3474fccf43aSdan           if( eType==SQLITE_INTEGER ){
3484fccf43aSdan             i = (u64)sqlite3_value_int64(pValue);
3494fccf43aSdan           }else{
3504fccf43aSdan             double r;
3514fccf43aSdan             assert( sizeof(double)==8 && sizeof(u64)==8 );
3524fccf43aSdan             r = sqlite3_value_double(pValue);
3534fccf43aSdan             memcpy(&i, &r, 8);
3544fccf43aSdan           }
355296c7658Sdan           sessionPutI64(&aBuf[1], i);
3564fccf43aSdan         }
3574fccf43aSdan         nByte = 9;
3584fccf43aSdan         break;
3594fccf43aSdan 
3604e895da1Sdan       default: {
36180fe2d93Sdan         u8 *z;
36280fe2d93Sdan         int n;
36380fe2d93Sdan         int nVarint;
36480fe2d93Sdan 
3654e895da1Sdan         assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
36680fe2d93Sdan         if( eType==SQLITE_TEXT ){
36780fe2d93Sdan           z = (u8 *)sqlite3_value_text(pValue);
36880fe2d93Sdan         }else{
36980fe2d93Sdan           z = (u8 *)sqlite3_value_blob(pValue);
37080fe2d93Sdan         }
37180fe2d93Sdan         n = sqlite3_value_bytes(pValue);
3723cc89d95Sdan         if( z==0 && (eType!=SQLITE_BLOB || n>0) ) return SQLITE_NOMEM;
37380fe2d93Sdan         nVarint = sessionVarintLen(n);
37480fe2d93Sdan 
3754fccf43aSdan         if( aBuf ){
3764fccf43aSdan           sessionVarintPut(&aBuf[1], n);
377895decf6Sdan           if( n ) memcpy(&aBuf[nVarint + 1], z, n);
3784fccf43aSdan         }
3794fccf43aSdan 
3804fccf43aSdan         nByte = 1 + nVarint + n;
3814fccf43aSdan         break;
3824fccf43aSdan       }
3834fccf43aSdan     }
38480fe2d93Sdan   }else{
38580fe2d93Sdan     nByte = 1;
38680fe2d93Sdan     if( aBuf ) aBuf[0] = '\0';
38780fe2d93Sdan   }
3884fccf43aSdan 
389fa122adaSdan   if( pnWrite ) *pnWrite += nByte;
3904fccf43aSdan   return SQLITE_OK;
3914fccf43aSdan }
3924fccf43aSdan 
393fa122adaSdan 
394798693b2Sdan /*
395798693b2Sdan ** This macro is used to calculate hash key values for data structures. In
396798693b2Sdan ** order to use this macro, the entire data structure must be represented
397798693b2Sdan ** as a series of unsigned integers. In order to calculate a hash-key value
398798693b2Sdan ** for a data structure represented as three such integers, the macro may
399798693b2Sdan ** then be used as follows:
400798693b2Sdan **
401798693b2Sdan **    int hash_key_value;
402798693b2Sdan **    hash_key_value = HASH_APPEND(0, <value 1>);
403798693b2Sdan **    hash_key_value = HASH_APPEND(hash_key_value, <value 2>);
404798693b2Sdan **    hash_key_value = HASH_APPEND(hash_key_value, <value 3>);
405798693b2Sdan **
406798693b2Sdan ** In practice, the data structures this macro is used for are the primary
407798693b2Sdan ** key values of modified rows.
408798693b2Sdan */
4094131639cSdan #define HASH_APPEND(hash, add) ((hash) << 3) ^ (hash) ^ (unsigned int)(add)
410798693b2Sdan 
411798693b2Sdan /*
412798693b2Sdan ** Append the hash of the 64-bit integer passed as the second argument to the
413798693b2Sdan ** hash-key value passed as the first. Return the new hash-key value.
414798693b2Sdan */
4154131639cSdan static unsigned int sessionHashAppendI64(unsigned int h, i64 i){
416e8d5648eSdan   h = HASH_APPEND(h, i & 0xFFFFFFFF);
417e8d5648eSdan   return HASH_APPEND(h, (i>>32)&0xFFFFFFFF);
418e8d5648eSdan }
419798693b2Sdan 
420798693b2Sdan /*
421798693b2Sdan ** Append the hash of the blob passed via the second and third arguments to
422798693b2Sdan ** the hash-key value passed as the first. Return the new hash-key value.
423798693b2Sdan */
4244131639cSdan static unsigned int sessionHashAppendBlob(unsigned int h, int n, const u8 *z){
425e8d5648eSdan   int i;
426e8d5648eSdan   for(i=0; i<n; i++) h = HASH_APPEND(h, z[i]);
427e8d5648eSdan   return h;
428e8d5648eSdan }
429e8d5648eSdan 
4304fccf43aSdan /*
431798693b2Sdan ** Append the hash of the data type passed as the second argument to the
432798693b2Sdan ** hash-key value passed as the first. Return the new hash-key value.
433798693b2Sdan */
434798693b2Sdan static unsigned int sessionHashAppendType(unsigned int h, int eType){
435798693b2Sdan   return HASH_APPEND(h, eType);
436798693b2Sdan }
437798693b2Sdan 
438798693b2Sdan /*
4394131639cSdan ** This function may only be called from within a pre-update callback.
4404131639cSdan ** It calculates a hash based on the primary key values of the old.* or
441798693b2Sdan ** new.* row currently available and, assuming no error occurs, writes it to
442798693b2Sdan ** *piHash before returning. If the primary key contains one or more NULL
443798693b2Sdan ** values, *pbNullPK is set to true before returning.
444798693b2Sdan **
445798693b2Sdan ** If an error occurs, an SQLite error code is returned and the final values
446798693b2Sdan ** of *piHash asn *pbNullPK are undefined. Otherwise, SQLITE_OK is returned
447798693b2Sdan ** and the output variables are set as described above.
4484fccf43aSdan */
449798693b2Sdan static int sessionPreupdateHash(
450cf8e9144Sdan   sqlite3_session *pSession,      /* Session object that owns pTab */
451e8d5648eSdan   SessionTable *pTab,             /* Session table handle */
452e8d5648eSdan   int bNew,                       /* True to hash the new.* PK */
45327453faeSdan   int *piHash,                    /* OUT: Hash value */
454798693b2Sdan   int *pbNullPK                   /* OUT: True if there are NULL values in PK */
455e8d5648eSdan ){
4564131639cSdan   unsigned int h = 0;             /* Hash value to return */
4574131639cSdan   int i;                          /* Used to iterate through columns */
458e8d5648eSdan 
45927453faeSdan   assert( *pbNullPK==0 );
460cf8e9144Sdan   assert( pTab->nCol==pSession->hook.xCount(pSession->hook.pCtx) );
461e8d5648eSdan   for(i=0; i<pTab->nCol; i++){
462e8d5648eSdan     if( pTab->abPK[i] ){
463e8d5648eSdan       int rc;
464e8d5648eSdan       int eType;
465e8d5648eSdan       sqlite3_value *pVal;
466e8d5648eSdan 
467e8d5648eSdan       if( bNew ){
468cf8e9144Sdan         rc = pSession->hook.xNew(pSession->hook.pCtx, i, &pVal);
469e8d5648eSdan       }else{
470cf8e9144Sdan         rc = pSession->hook.xOld(pSession->hook.pCtx, i, &pVal);
471e8d5648eSdan       }
47212ca0b56Sdan       if( rc!=SQLITE_OK ) return rc;
473e8d5648eSdan 
474e8d5648eSdan       eType = sqlite3_value_type(pVal);
475798693b2Sdan       h = sessionHashAppendType(h, eType);
4766734007dSdan       if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
477e8d5648eSdan         i64 iVal;
478e8d5648eSdan         if( eType==SQLITE_INTEGER ){
479e8d5648eSdan           iVal = sqlite3_value_int64(pVal);
480e8d5648eSdan         }else{
481e8d5648eSdan           double rVal = sqlite3_value_double(pVal);
482e8d5648eSdan           assert( sizeof(iVal)==8 && sizeof(rVal)==8 );
483e8d5648eSdan           memcpy(&iVal, &rVal, 8);
484e8d5648eSdan         }
485e8d5648eSdan         h = sessionHashAppendI64(h, iVal);
4866734007dSdan       }else if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
4876734007dSdan         const u8 *z;
4883cc89d95Sdan         int n;
4896734007dSdan         if( eType==SQLITE_TEXT ){
4906734007dSdan           z = (const u8 *)sqlite3_value_text(pVal);
4916734007dSdan         }else{
4926734007dSdan           z = (const u8 *)sqlite3_value_blob(pVal);
493e8d5648eSdan         }
4943cc89d95Sdan         n = sqlite3_value_bytes(pVal);
4953cc89d95Sdan         if( !z && (eType!=SQLITE_BLOB || n>0) ) return SQLITE_NOMEM;
4963cc89d95Sdan         h = sessionHashAppendBlob(h, n, z);
4976734007dSdan       }else{
49827453faeSdan         assert( eType==SQLITE_NULL );
49927453faeSdan         *pbNullPK = 1;
500e8d5648eSdan       }
501e8d5648eSdan     }
502e8d5648eSdan   }
503e8d5648eSdan 
504e8d5648eSdan   *piHash = (h % pTab->nChange);
505e8d5648eSdan   return SQLITE_OK;
506e8d5648eSdan }
507e8d5648eSdan 
5084131639cSdan /*
5096cda207fSdan ** The buffer that the argument points to contains a serialized SQL value.
5106cda207fSdan ** Return the number of bytes of space occupied by the value (including
5116cda207fSdan ** the type byte).
5126cda207fSdan */
5136cda207fSdan static int sessionSerialLen(u8 *a){
5146cda207fSdan   int e = *a;
5156cda207fSdan   int n;
5166cda207fSdan   if( e==0 ) return 1;
5176cda207fSdan   if( e==SQLITE_NULL ) return 1;
5186cda207fSdan   if( e==SQLITE_INTEGER || e==SQLITE_FLOAT ) return 9;
5196cda207fSdan   return sessionVarintGet(&a[1], &n) + 1 + n;
5206cda207fSdan }
5216cda207fSdan 
5226cda207fSdan /*
5235d607a6eSdan ** Based on the primary key values stored in change aRecord, calculate a
524798693b2Sdan ** hash key. Assume the has table has nBucket buckets. The hash keys
5254131639cSdan ** calculated by this function are compatible with those calculated by
5264131639cSdan ** sessionPreupdateHash().
52764277f4aSdan **
52864277f4aSdan ** The bPkOnly argument is non-zero if the record at aRecord[] is from
52964277f4aSdan ** a patchset DELETE. In this case the non-PK fields are omitted entirely.
5304131639cSdan */
5314131639cSdan static unsigned int sessionChangeHash(
5324131639cSdan   SessionTable *pTab,             /* Table handle */
53364277f4aSdan   int bPkOnly,                    /* Record consists of PK fields only */
5345d607a6eSdan   u8 *aRecord,                    /* Change record */
5354131639cSdan   int nBucket                     /* Assume this many buckets in hash table */
536e8d5648eSdan ){
5374131639cSdan   unsigned int h = 0;             /* Value to return */
5384131639cSdan   int i;                          /* Used to iterate through columns */
5395d607a6eSdan   u8 *a = aRecord;                /* Used to iterate through change record */
540e8d5648eSdan 
541e8d5648eSdan   for(i=0; i<pTab->nCol; i++){
5426cda207fSdan     int eType = *a;
543e8d5648eSdan     int isPK = pTab->abPK[i];
54464277f4aSdan     if( bPkOnly && isPK==0 ) continue;
545e8d5648eSdan 
54627453faeSdan     /* It is not possible for eType to be SQLITE_NULL here. The session
54727453faeSdan     ** module does not record changes for rows with NULL values stored in
54827453faeSdan     ** primary key columns. */
54927453faeSdan     assert( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT
55027453faeSdan          || eType==SQLITE_TEXT || eType==SQLITE_BLOB
5516cda207fSdan          || eType==SQLITE_NULL || eType==0
55227453faeSdan     );
5536cda207fSdan     assert( !isPK || (eType!=0 && eType!=SQLITE_NULL) );
55427453faeSdan 
5556cda207fSdan     if( isPK ){
5566cda207fSdan       a++;
557798693b2Sdan       h = sessionHashAppendType(h, eType);
55827453faeSdan       if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
5596734007dSdan         h = sessionHashAppendI64(h, sessionGetI64(a));
560e8d5648eSdan         a += 8;
56127453faeSdan       }else{
562e8d5648eSdan         int n;
563e8d5648eSdan         a += sessionVarintGet(a, &n);
5646734007dSdan         h = sessionHashAppendBlob(h, n, a);
565e8d5648eSdan         a += n;
566e8d5648eSdan       }
5676cda207fSdan     }else{
5686cda207fSdan       a += sessionSerialLen(a);
5696cda207fSdan     }
570e8d5648eSdan   }
571e8d5648eSdan   return (h % nBucket);
572e8d5648eSdan }
573e8d5648eSdan 
574798693b2Sdan /*
575798693b2Sdan ** Arguments aLeft and aRight are pointers to change records for table pTab.
576798693b2Sdan ** This function returns true if the two records apply to the same row (i.e.
577798693b2Sdan ** have the same values stored in the primary key columns), or false
578798693b2Sdan ** otherwise.
579798693b2Sdan */
5805d607a6eSdan static int sessionChangeEqual(
581798693b2Sdan   SessionTable *pTab,             /* Table used for PK definition */
582a71d2371Sdan   int bLeftPkOnly,                /* True if aLeft[] contains PK fields only */
5835d607a6eSdan   u8 *aLeft,                      /* Change record */
584a71d2371Sdan   int bRightPkOnly,               /* True if aRight[] contains PK fields only */
5855d607a6eSdan   u8 *aRight                      /* Change record */
5865d607a6eSdan ){
587798693b2Sdan   u8 *a1 = aLeft;                 /* Cursor to iterate through aLeft */
588798693b2Sdan   u8 *a2 = aRight;                /* Cursor to iterate through aRight */
589798693b2Sdan   int iCol;                       /* Used to iterate through table columns */
5905d607a6eSdan 
591798693b2Sdan   for(iCol=0; iCol<pTab->nCol; iCol++){
59214faa061Sdan     if( pTab->abPK[iCol] ){
5935d607a6eSdan       int n1 = sessionSerialLen(a1);
5945d607a6eSdan       int n2 = sessionSerialLen(a2);
5955d607a6eSdan 
596798693b2Sdan       if( pTab->abPK[iCol] && (n1!=n2 || memcmp(a1, a2, n1)) ){
5975d607a6eSdan         return 0;
5985d607a6eSdan       }
59914faa061Sdan       a1 += n1;
60014faa061Sdan       a2 += n2;
60114faa061Sdan     }else{
60214faa061Sdan       if( bLeftPkOnly==0 ) a1 += sessionSerialLen(a1);
60314faa061Sdan       if( bRightPkOnly==0 ) a2 += sessionSerialLen(a2);
60414faa061Sdan     }
6055d607a6eSdan   }
6065d607a6eSdan 
6075d607a6eSdan   return 1;
6085d607a6eSdan }
6095d607a6eSdan 
610798693b2Sdan /*
611798693b2Sdan ** Arguments aLeft and aRight both point to buffers containing change
612798693b2Sdan ** records with nCol columns. This function "merges" the two records into
613798693b2Sdan ** a single records which is written to the buffer at *paOut. *paOut is
614798693b2Sdan ** then set to point to one byte after the last byte written before
615798693b2Sdan ** returning.
616798693b2Sdan **
617798693b2Sdan ** The merging of records is done as follows: For each column, if the
618798693b2Sdan ** aRight record contains a value for the column, copy the value from
619798693b2Sdan ** their. Otherwise, if aLeft contains a value, copy it. If neither
620798693b2Sdan ** record contains a value for a given column, then neither does the
621798693b2Sdan ** output record.
622798693b2Sdan */
6235d607a6eSdan static void sessionMergeRecord(
6245d607a6eSdan   u8 **paOut,
625798693b2Sdan   int nCol,
6265d607a6eSdan   u8 *aLeft,
6275d607a6eSdan   u8 *aRight
6285d607a6eSdan ){
629798693b2Sdan   u8 *a1 = aLeft;                 /* Cursor used to iterate through aLeft */
630798693b2Sdan   u8 *a2 = aRight;                /* Cursor used to iterate through aRight */
631798693b2Sdan   u8 *aOut = *paOut;              /* Output cursor */
632798693b2Sdan   int iCol;                       /* Used to iterate from 0 to nCol */
6335d607a6eSdan 
634798693b2Sdan   for(iCol=0; iCol<nCol; iCol++){
6355d607a6eSdan     int n1 = sessionSerialLen(a1);
6365d607a6eSdan     int n2 = sessionSerialLen(a2);
6375d607a6eSdan     if( *a2 ){
6385d607a6eSdan       memcpy(aOut, a2, n2);
6395d607a6eSdan       aOut += n2;
6405d607a6eSdan     }else{
6415d607a6eSdan       memcpy(aOut, a1, n1);
6425d607a6eSdan       aOut += n1;
6435d607a6eSdan     }
6445d607a6eSdan     a1 += n1;
6455d607a6eSdan     a2 += n2;
6465d607a6eSdan   }
6475d607a6eSdan 
6485d607a6eSdan   *paOut = aOut;
6495d607a6eSdan }
6505d607a6eSdan 
651798693b2Sdan /*
652798693b2Sdan ** This is a helper function used by sessionMergeUpdate().
653798693b2Sdan **
654798693b2Sdan ** When this function is called, both *paOne and *paTwo point to a value
655798693b2Sdan ** within a change record. Before it returns, both have been advanced so
656798693b2Sdan ** as to point to the next value in the record.
657798693b2Sdan **
658798693b2Sdan ** If, when this function is called, *paTwo points to a valid value (i.e.
659fa29ecc4Sdan ** *paTwo[0] is not 0x00 - the "no value" placeholder), a copy of the *paTwo
660798693b2Sdan ** pointer is returned and *pnVal is set to the number of bytes in the
661798693b2Sdan ** serialized value. Otherwise, a copy of *paOne is returned and *pnVal
662798693b2Sdan ** set to the number of bytes in the value at *paOne. If *paOne points
663fa29ecc4Sdan ** to the "no value" placeholder, *pnVal is set to 1. In other words:
664fa29ecc4Sdan **
665fa29ecc4Sdan **   if( *paTwo is valid ) return *paTwo;
666fa29ecc4Sdan **   return *paOne;
667fa29ecc4Sdan **
668798693b2Sdan */
6695d607a6eSdan static u8 *sessionMergeValue(
670798693b2Sdan   u8 **paOne,                     /* IN/OUT: Left-hand buffer pointer */
671798693b2Sdan   u8 **paTwo,                     /* IN/OUT: Right-hand buffer pointer */
672798693b2Sdan   int *pnVal                      /* OUT: Bytes in returned value */
6735d607a6eSdan ){
6745d607a6eSdan   u8 *a1 = *paOne;
6755d607a6eSdan   u8 *a2 = *paTwo;
6765d607a6eSdan   u8 *pRet = 0;
6775d607a6eSdan   int n1;
6785d607a6eSdan 
6795d607a6eSdan   assert( a1 );
6805d607a6eSdan   if( a2 ){
6815d607a6eSdan     int n2 = sessionSerialLen(a2);
6825d607a6eSdan     if( *a2 ){
6835d607a6eSdan       *pnVal = n2;
6845d607a6eSdan       pRet = a2;
6855d607a6eSdan     }
6865d607a6eSdan     *paTwo = &a2[n2];
6875d607a6eSdan   }
6885d607a6eSdan 
6895d607a6eSdan   n1 = sessionSerialLen(a1);
6905d607a6eSdan   if( pRet==0 ){
6915d607a6eSdan     *pnVal = n1;
6925d607a6eSdan     pRet = a1;
6935d607a6eSdan   }
6945d607a6eSdan   *paOne = &a1[n1];
6955d607a6eSdan 
6965d607a6eSdan   return pRet;
6975d607a6eSdan }
6985d607a6eSdan 
699798693b2Sdan /*
700798693b2Sdan ** This function is used by changeset_concat() to merge two UPDATE changes
701798693b2Sdan ** on the same row.
702798693b2Sdan */
7035d607a6eSdan static int sessionMergeUpdate(
704798693b2Sdan   u8 **paOut,                     /* IN/OUT: Pointer to output buffer */
705798693b2Sdan   SessionTable *pTab,             /* Table change pertains to */
706a71d2371Sdan   int bPatchset,                  /* True if records are patchset records */
707798693b2Sdan   u8 *aOldRecord1,                /* old.* record for first change */
708798693b2Sdan   u8 *aOldRecord2,                /* old.* record for second change */
709798693b2Sdan   u8 *aNewRecord1,                /* new.* record for first change */
710798693b2Sdan   u8 *aNewRecord2                 /* new.* record for second change */
7115d607a6eSdan ){
7125d607a6eSdan   u8 *aOld1 = aOldRecord1;
7135d607a6eSdan   u8 *aOld2 = aOldRecord2;
7145d607a6eSdan   u8 *aNew1 = aNewRecord1;
7155d607a6eSdan   u8 *aNew2 = aNewRecord2;
7165d607a6eSdan 
7175d607a6eSdan   u8 *aOut = *paOut;
7185d607a6eSdan   int i;
71964277f4aSdan 
72064277f4aSdan   if( bPatchset==0 ){
7215d607a6eSdan     int bRequired = 0;
7225d607a6eSdan 
7235d607a6eSdan     assert( aOldRecord1 && aNewRecord1 );
7245d607a6eSdan 
7255d607a6eSdan     /* Write the old.* vector first. */
7265d607a6eSdan     for(i=0; i<pTab->nCol; i++){
7275d607a6eSdan       int nOld;
7285d607a6eSdan       u8 *aOld;
7295d607a6eSdan       int nNew;
7305d607a6eSdan       u8 *aNew;
7315d607a6eSdan 
7325d607a6eSdan       aOld = sessionMergeValue(&aOld1, &aOld2, &nOld);
7335d607a6eSdan       aNew = sessionMergeValue(&aNew1, &aNew2, &nNew);
7345d607a6eSdan       if( pTab->abPK[i] || nOld!=nNew || memcmp(aOld, aNew, nNew) ){
7355d607a6eSdan         if( pTab->abPK[i]==0 ) bRequired = 1;
7365d607a6eSdan         memcpy(aOut, aOld, nOld);
7375d607a6eSdan         aOut += nOld;
7385d607a6eSdan       }else{
7395d607a6eSdan         *(aOut++) = '\0';
7405d607a6eSdan       }
7415d607a6eSdan     }
7425d607a6eSdan 
7435d607a6eSdan     if( !bRequired ) return 0;
74464277f4aSdan   }
7455d607a6eSdan 
7465d607a6eSdan   /* Write the new.* vector */
7475d607a6eSdan   aOld1 = aOldRecord1;
7485d607a6eSdan   aOld2 = aOldRecord2;
7495d607a6eSdan   aNew1 = aNewRecord1;
7505d607a6eSdan   aNew2 = aNewRecord2;
7515d607a6eSdan   for(i=0; i<pTab->nCol; i++){
7525d607a6eSdan     int nOld;
7535d607a6eSdan     u8 *aOld;
7545d607a6eSdan     int nNew;
7555d607a6eSdan     u8 *aNew;
7565d607a6eSdan 
7575d607a6eSdan     aOld = sessionMergeValue(&aOld1, &aOld2, &nOld);
7585d607a6eSdan     aNew = sessionMergeValue(&aNew1, &aNew2, &nNew);
75964277f4aSdan     if( bPatchset==0
76064277f4aSdan      && (pTab->abPK[i] || (nOld==nNew && 0==memcmp(aOld, aNew, nNew)))
76164277f4aSdan     ){
7625d607a6eSdan       *(aOut++) = '\0';
7635d607a6eSdan     }else{
7645d607a6eSdan       memcpy(aOut, aNew, nNew);
7655d607a6eSdan       aOut += nNew;
7665d607a6eSdan     }
7675d607a6eSdan   }
7685d607a6eSdan 
7695d607a6eSdan   *paOut = aOut;
7705d607a6eSdan   return 1;
7715d607a6eSdan }
7725d607a6eSdan 
77377fc1d5bSdan /*
77477fc1d5bSdan ** This function is only called from within a pre-update-hook callback.
77577fc1d5bSdan ** It determines if the current pre-update-hook change affects the same row
77677fc1d5bSdan ** as the change stored in argument pChange. If so, it returns true. Otherwise
77777fc1d5bSdan ** if the pre-update-hook does not affect the same row as pChange, it returns
77877fc1d5bSdan ** false.
77977fc1d5bSdan */
78077fc1d5bSdan static int sessionPreupdateEqual(
781cf8e9144Sdan   sqlite3_session *pSession,      /* Session object that owns SessionTable */
78277fc1d5bSdan   SessionTable *pTab,             /* Table associated with change */
78377fc1d5bSdan   SessionChange *pChange,         /* Change to compare to */
78477fc1d5bSdan   int op                          /* Current pre-update operation */
785e8d5648eSdan ){
78677fc1d5bSdan   int iCol;                       /* Used to iterate through columns */
78777fc1d5bSdan   u8 *a = pChange->aRecord;       /* Cursor used to scan change record */
788e8d5648eSdan 
78977fc1d5bSdan   assert( op==SQLITE_INSERT || op==SQLITE_UPDATE || op==SQLITE_DELETE );
79077fc1d5bSdan   for(iCol=0; iCol<pTab->nCol; iCol++){
79177fc1d5bSdan     if( !pTab->abPK[iCol] ){
792798693b2Sdan       a += sessionSerialLen(a);
793e8d5648eSdan     }else{
7946734007dSdan       sqlite3_value *pVal;        /* Value returned by preupdate_new/old */
7956734007dSdan       int rc;                     /* Error code from preupdate_new/old */
796798693b2Sdan       int eType = *a++;           /* Type of value from change record */
7976734007dSdan 
7986734007dSdan       /* The following calls to preupdate_new() and preupdate_old() can not
7996734007dSdan       ** fail. This is because they cache their return values, and by the
8006734007dSdan       ** time control flows to here they have already been called once from
8016734007dSdan       ** within sessionPreupdateHash(). The first two asserts below verify
8026734007dSdan       ** this (that the method has already been called). */
80377fc1d5bSdan       if( op==SQLITE_INSERT ){
804cf8e9144Sdan         /* assert( db->pPreUpdate->pNewUnpacked || db->pPreUpdate->aNew ); */
805cf8e9144Sdan         rc = pSession->hook.xNew(pSession->hook.pCtx, iCol, &pVal);
806e8d5648eSdan       }else{
807cf8e9144Sdan         /* assert( db->pPreUpdate->pUnpacked ); */
808cf8e9144Sdan         rc = pSession->hook.xOld(pSession->hook.pCtx, iCol, &pVal);
809e8d5648eSdan       }
8106734007dSdan       assert( rc==SQLITE_OK );
81177fc1d5bSdan       if( sqlite3_value_type(pVal)!=eType ) return 0;
812e8d5648eSdan 
81312ca0b56Sdan       /* A SessionChange object never has a NULL value in a PK column */
81412ca0b56Sdan       assert( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT
81512ca0b56Sdan            || eType==SQLITE_BLOB    || eType==SQLITE_TEXT
81612ca0b56Sdan       );
81712ca0b56Sdan 
81812ca0b56Sdan       if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
819e8d5648eSdan         i64 iVal = sessionGetI64(a);
820e8d5648eSdan         a += 8;
821e8d5648eSdan         if( eType==SQLITE_INTEGER ){
82277fc1d5bSdan           if( sqlite3_value_int64(pVal)!=iVal ) return 0;
823e8d5648eSdan         }else{
824e8d5648eSdan           double rVal;
825e8d5648eSdan           assert( sizeof(iVal)==8 && sizeof(rVal)==8 );
826e8d5648eSdan           memcpy(&rVal, &iVal, 8);
82777fc1d5bSdan           if( sqlite3_value_double(pVal)!=rVal ) return 0;
828e8d5648eSdan         }
82912ca0b56Sdan       }else{
830e8d5648eSdan         int n;
831e8d5648eSdan         const u8 *z;
832e8d5648eSdan         a += sessionVarintGet(a, &n);
83377fc1d5bSdan         if( sqlite3_value_bytes(pVal)!=n ) return 0;
83412ca0b56Sdan         if( eType==SQLITE_TEXT ){
83512ca0b56Sdan           z = sqlite3_value_text(pVal);
83612ca0b56Sdan         }else{
83712ca0b56Sdan           z = sqlite3_value_blob(pVal);
83812ca0b56Sdan         }
83977fc1d5bSdan         if( memcmp(a, z, n) ) return 0;
840e8d5648eSdan         a += n;
841e8d5648eSdan         break;
842e8d5648eSdan       }
843e8d5648eSdan     }
844e8d5648eSdan   }
845e8d5648eSdan 
84677fc1d5bSdan   return 1;
8474fccf43aSdan }
8484fccf43aSdan 
8494fccf43aSdan /*
8504fccf43aSdan ** If required, grow the hash table used to store changes on table pTab
8514fccf43aSdan ** (part of the session pSession). If a fatal OOM error occurs, set the
8524fccf43aSdan ** session object to failed and return SQLITE_ERROR. Otherwise, return
8534fccf43aSdan ** SQLITE_OK.
8544fccf43aSdan **
8554fccf43aSdan ** It is possible that a non-fatal OOM error occurs in this function. In
8564fccf43aSdan ** that case the hash-table does not grow, but SQLITE_OK is returned anyway.
8574fccf43aSdan ** Growing the hash table in this case is a performance optimization only,
8584fccf43aSdan ** it is not required for correct operation.
8594fccf43aSdan */
86064277f4aSdan static int sessionGrowHash(int bPatchset, SessionTable *pTab){
8614fccf43aSdan   if( pTab->nChange==0 || pTab->nEntry>=(pTab->nChange/2) ){
8624fccf43aSdan     int i;
8634fccf43aSdan     SessionChange **apNew;
8644fccf43aSdan     int nNew = (pTab->nChange ? pTab->nChange : 128) * 2;
8654fccf43aSdan 
8664fccf43aSdan     apNew = (SessionChange **)sqlite3_malloc(sizeof(SessionChange *) * nNew);
8674fccf43aSdan     if( apNew==0 ){
8684fccf43aSdan       if( pTab->nChange==0 ){
8694fccf43aSdan         return SQLITE_ERROR;
8704fccf43aSdan       }
8714fccf43aSdan       return SQLITE_OK;
8724fccf43aSdan     }
8734fccf43aSdan     memset(apNew, 0, sizeof(SessionChange *) * nNew);
8744fccf43aSdan 
8754fccf43aSdan     for(i=0; i<pTab->nChange; i++){
8764fccf43aSdan       SessionChange *p;
8774fccf43aSdan       SessionChange *pNext;
8784fccf43aSdan       for(p=pTab->apChange[i]; p; p=pNext){
87964277f4aSdan         int bPkOnly = (p->op==SQLITE_DELETE && bPatchset);
88064277f4aSdan         int iHash = sessionChangeHash(pTab, bPkOnly, p->aRecord, nNew);
8814fccf43aSdan         pNext = p->pNext;
8824fccf43aSdan         p->pNext = apNew[iHash];
8834fccf43aSdan         apNew[iHash] = p;
8844fccf43aSdan       }
8854fccf43aSdan     }
8864fccf43aSdan 
8874fccf43aSdan     sqlite3_free(pTab->apChange);
8884fccf43aSdan     pTab->nChange = nNew;
8894fccf43aSdan     pTab->apChange = apNew;
8904fccf43aSdan   }
8914fccf43aSdan 
8924fccf43aSdan   return SQLITE_OK;
8934fccf43aSdan }
8944fccf43aSdan 
895296c7658Sdan /*
896e8d5648eSdan ** This function queries the database for the names of the columns of table
897e8d5648eSdan ** zThis, in schema zDb. It is expected that the table has nCol columns. If
898e8d5648eSdan ** not, SQLITE_SCHEMA is returned and none of the output variables are
899e8d5648eSdan ** populated.
900e8d5648eSdan **
90177fc1d5bSdan ** Otherwise, if they are not NULL, variable *pnCol is set to the number
90277fc1d5bSdan ** of columns in the database table and variable *pzTab is set to point to a
903e8d5648eSdan ** nul-terminated copy of the table name. *pazCol (if not NULL) is set to
904e8d5648eSdan ** point to an array of pointers to column names. And *pabPK (again, if not
905e8d5648eSdan ** NULL) is set to point to an array of booleans - true if the corresponding
906e8d5648eSdan ** column is part of the primary key.
907e8d5648eSdan **
908e8d5648eSdan ** For example, if the table is declared as:
909e8d5648eSdan **
910e8d5648eSdan **     CREATE TABLE tbl1(w, x, y, z, PRIMARY KEY(w, z));
911e8d5648eSdan **
91277fc1d5bSdan ** Then the four output variables are populated as follows:
913e8d5648eSdan **
91477fc1d5bSdan **     *pnCol  = 4
915e8d5648eSdan **     *pzTab  = "tbl1"
916e8d5648eSdan **     *pazCol = {"w", "x", "y", "z"}
917e8d5648eSdan **     *pabPK  = {1, 0, 0, 1}
918e8d5648eSdan **
919e8d5648eSdan ** All returned buffers are part of the same single allocation, which must
920e8d5648eSdan ** be freed using sqlite3_free() by the caller. If pazCol was not NULL, then
921e8d5648eSdan ** pointer *pazCol should be freed to release all memory. Otherwise, pointer
922e8d5648eSdan ** *pabPK. It is illegal for both pazCol and pabPK to be NULL.
923e8d5648eSdan */
924e8d5648eSdan static int sessionTableInfo(
925e8d5648eSdan   sqlite3 *db,                    /* Database connection */
926e8d5648eSdan   const char *zDb,                /* Name of attached database (e.g. "main") */
927e8d5648eSdan   const char *zThis,              /* Table name */
928ca62ad57Sdan   int *pnCol,                     /* OUT: number of columns */
929e8d5648eSdan   const char **pzTab,             /* OUT: Copy of zThis */
930e8d5648eSdan   const char ***pazCol,           /* OUT: Array of column names for table */
931e8d5648eSdan   u8 **pabPK                      /* OUT: Array of booleans - true for PK col */
932e8d5648eSdan ){
933e8d5648eSdan   char *zPragma;
934e8d5648eSdan   sqlite3_stmt *pStmt;
935e8d5648eSdan   int rc;
936e8d5648eSdan   int nByte;
937e8d5648eSdan   int nDbCol = 0;
938e8d5648eSdan   int nThis;
939e8d5648eSdan   int i;
94074f598b6Smistachkin   u8 *pAlloc = 0;
941db04571cSdan   char **azCol = 0;
94274f598b6Smistachkin   u8 *abPK = 0;
943e8d5648eSdan 
944db04571cSdan   assert( pazCol && pabPK );
945e8d5648eSdan 
946cfdbde21Sdrh   nThis = sqlite3Strlen30(zThis);
947*614efe2bSdan   if( nThis==12 && 0==sqlite3_stricmp("sqlite_stat1", zThis) ){
948*614efe2bSdan     /* For sqlite_stat1, pretend that (tbl,idx) is the PRIMARY KEY. */
949*614efe2bSdan     zPragma = sqlite3_mprintf(
950*614efe2bSdan         "SELECT 0, 'tbl',  '', 0, '', 1     UNION ALL "
951*614efe2bSdan         "SELECT 1, 'idx',  '', 0, '', 2     UNION ALL "
952*614efe2bSdan         "SELECT 2, 'stat', '', 0, '', 0"
953*614efe2bSdan     );
954*614efe2bSdan   }else{
955e8d5648eSdan     zPragma = sqlite3_mprintf("PRAGMA '%q'.table_info('%q')", zDb, zThis);
956*614efe2bSdan   }
957e8d5648eSdan   if( !zPragma ) return SQLITE_NOMEM;
958e8d5648eSdan 
959e8d5648eSdan   rc = sqlite3_prepare_v2(db, zPragma, -1, &pStmt, 0);
960e8d5648eSdan   sqlite3_free(zPragma);
961e8d5648eSdan   if( rc!=SQLITE_OK ) return rc;
962e8d5648eSdan 
963e8d5648eSdan   nByte = nThis + 1;
964e8d5648eSdan   while( SQLITE_ROW==sqlite3_step(pStmt) ){
965e8d5648eSdan     nByte += sqlite3_column_bytes(pStmt, 1);
966e8d5648eSdan     nDbCol++;
967e8d5648eSdan   }
968e8d5648eSdan   rc = sqlite3_reset(pStmt);
969e8d5648eSdan 
970e8d5648eSdan   if( rc==SQLITE_OK ){
971e8d5648eSdan     nByte += nDbCol * (sizeof(const char *) + sizeof(u8) + 1);
972e8d5648eSdan     pAlloc = sqlite3_malloc(nByte);
973e8d5648eSdan     if( pAlloc==0 ){
974e8d5648eSdan       rc = SQLITE_NOMEM;
975e8d5648eSdan     }
976e8d5648eSdan   }
977e8d5648eSdan   if( rc==SQLITE_OK ){
978e8d5648eSdan     azCol = (char **)pAlloc;
979ca62ad57Sdan     pAlloc = (u8 *)&azCol[nDbCol];
980e8d5648eSdan     abPK = (u8 *)pAlloc;
981ca62ad57Sdan     pAlloc = &abPK[nDbCol];
982e8d5648eSdan     if( pzTab ){
983e8d5648eSdan       memcpy(pAlloc, zThis, nThis+1);
984e8d5648eSdan       *pzTab = (char *)pAlloc;
985e8d5648eSdan       pAlloc += nThis+1;
986e8d5648eSdan     }
987e8d5648eSdan 
988e8d5648eSdan     i = 0;
989e8d5648eSdan     while( SQLITE_ROW==sqlite3_step(pStmt) ){
990e8d5648eSdan       int nName = sqlite3_column_bytes(pStmt, 1);
991e8d5648eSdan       const unsigned char *zName = sqlite3_column_text(pStmt, 1);
992e8d5648eSdan       if( zName==0 ) break;
993e8d5648eSdan       memcpy(pAlloc, zName, nName+1);
994e8d5648eSdan       azCol[i] = (char *)pAlloc;
995e8d5648eSdan       pAlloc += nName+1;
996db04571cSdan       abPK[i] = sqlite3_column_int(pStmt, 5);
997e8d5648eSdan       i++;
998e8d5648eSdan     }
999e8d5648eSdan     rc = sqlite3_reset(pStmt);
1000e8d5648eSdan 
1001e8d5648eSdan   }
1002e8d5648eSdan 
1003e8d5648eSdan   /* If successful, populate the output variables. Otherwise, zero them and
1004e8d5648eSdan   ** free any allocation made. An error code will be returned in this case.
1005e8d5648eSdan   */
1006e8d5648eSdan   if( rc==SQLITE_OK ){
1007db04571cSdan     *pazCol = (const char **)azCol;
1008db04571cSdan     *pabPK = abPK;
1009ca62ad57Sdan     *pnCol = nDbCol;
1010e8d5648eSdan   }else{
1011db04571cSdan     *pazCol = 0;
1012db04571cSdan     *pabPK = 0;
1013ca62ad57Sdan     *pnCol = 0;
1014e8d5648eSdan     if( pzTab ) *pzTab = 0;
1015db04571cSdan     sqlite3_free(azCol);
1016e8d5648eSdan   }
1017e8d5648eSdan   sqlite3_finalize(pStmt);
1018e8d5648eSdan   return rc;
1019e8d5648eSdan }
1020e8d5648eSdan 
1021e8d5648eSdan /*
1022296c7658Sdan ** This function is only called from within a pre-update handler for a
1023296c7658Sdan ** write to table pTab, part of session pSession. If this is the first
10246dc29e60Sdan ** write to this table, initalize the SessionTable.nCol, azCol[] and
10256dc29e60Sdan ** abPK[] arrays accordingly.
1026296c7658Sdan **
10276dc29e60Sdan ** If an error occurs, an error code is stored in sqlite3_session.rc and
10286dc29e60Sdan ** non-zero returned. Or, if no error occurs but the table has no primary
10296dc29e60Sdan ** key, sqlite3_session.rc is left set to SQLITE_OK and non-zero returned to
10306dc29e60Sdan ** indicate that updates on this table should be ignored. SessionTable.abPK
10316dc29e60Sdan ** is set to NULL in this case.
1032296c7658Sdan */
10334fccf43aSdan static int sessionInitTable(sqlite3_session *pSession, SessionTable *pTab){
10344fccf43aSdan   if( pTab->nCol==0 ){
10356dc29e60Sdan     u8 *abPK;
1036e8d5648eSdan     assert( pTab->azCol==0 || pTab->abPK==0 );
1037e8d5648eSdan     pSession->rc = sessionTableInfo(pSession->db, pSession->zDb,
10386dc29e60Sdan         pTab->zName, &pTab->nCol, 0, &pTab->azCol, &abPK
1039e8d5648eSdan     );
10406dc29e60Sdan     if( pSession->rc==SQLITE_OK ){
10416dc29e60Sdan       int i;
10426dc29e60Sdan       for(i=0; i<pTab->nCol; i++){
10436dc29e60Sdan         if( abPK[i] ){
10446dc29e60Sdan           pTab->abPK = abPK;
10456dc29e60Sdan           break;
1046ca62ad57Sdan         }
10474fccf43aSdan       }
10486dc29e60Sdan     }
10496dc29e60Sdan   }
10506dc29e60Sdan   return (pSession->rc || pTab->abPK==0);
1051e8d5648eSdan }
1052e8d5648eSdan 
105377fc1d5bSdan /*
105477fc1d5bSdan ** This function is only called from with a pre-update-hook reporting a
105577fc1d5bSdan ** change on table pTab (attached to session pSession). The type of change
105677fc1d5bSdan ** (UPDATE, INSERT, DELETE) is specified by the first argument.
105777fc1d5bSdan **
105877fc1d5bSdan ** Unless one is already present or an error occurs, an entry is added
105977fc1d5bSdan ** to the changed-rows hash table associated with table pTab.
106077fc1d5bSdan */
1061e8d5648eSdan static void sessionPreupdateOneChange(
106277fc1d5bSdan   int op,                         /* One of SQLITE_UPDATE, INSERT, DELETE */
106377fc1d5bSdan   sqlite3_session *pSession,      /* Session object pTab is attached to */
106477fc1d5bSdan   SessionTable *pTab              /* Table that change applies to */
1065e8d5648eSdan ){
1066e8d5648eSdan   int iHash;
1067cf8e9144Sdan   int bNull = 0;
1068e8d5648eSdan   int rc = SQLITE_OK;
1069e8d5648eSdan 
1070e8d5648eSdan   if( pSession->rc ) return;
1071e8d5648eSdan 
1072e8d5648eSdan   /* Load table details if required */
1073e8d5648eSdan   if( sessionInitTable(pSession, pTab) ) return;
1074e8d5648eSdan 
10756dc29e60Sdan   /* Check the number of columns in this xPreUpdate call matches the
10766dc29e60Sdan   ** number of columns in the table.  */
10776dc29e60Sdan   if( pTab->nCol!=pSession->hook.xCount(pSession->hook.pCtx) ){
10786dc29e60Sdan     pSession->rc = SQLITE_SCHEMA;
10796dc29e60Sdan     return;
10806dc29e60Sdan   }
10816dc29e60Sdan 
1082e8d5648eSdan   /* Grow the hash table if required */
108364277f4aSdan   if( sessionGrowHash(0, pTab) ){
10845d607a6eSdan     pSession->rc = SQLITE_NOMEM;
10855d607a6eSdan     return;
10865d607a6eSdan   }
1087e8d5648eSdan 
108880fe2d93Sdan   /* Calculate the hash-key for this change. If the primary key of the row
108980fe2d93Sdan   ** includes a NULL value, exit early. Such changes are ignored by the
109080fe2d93Sdan   ** session module. */
1091cf8e9144Sdan   rc = sessionPreupdateHash(pSession, pTab, op==SQLITE_INSERT, &iHash, &bNull);
109280fe2d93Sdan   if( rc!=SQLITE_OK ) goto error_out;
109380fe2d93Sdan 
1094cf8e9144Sdan   if( bNull==0 ){
109580fe2d93Sdan     /* Search the hash table for an existing record for this row. */
1096b4480e94Sdan     SessionChange *pC;
10976734007dSdan     for(pC=pTab->apChange[iHash]; pC; pC=pC->pNext){
1098cf8e9144Sdan       if( sessionPreupdateEqual(pSession, pTab, pC, op) ) break;
1099e8d5648eSdan     }
110080fe2d93Sdan 
1101e8d5648eSdan     if( pC==0 ){
1102e8d5648eSdan       /* Create a new change object containing all the old values (if
1103e8d5648eSdan       ** this is an SQLITE_UPDATE or SQLITE_DELETE), or just the PK
1104e8d5648eSdan       ** values (if this is an INSERT). */
1105b4480e94Sdan       SessionChange *pChange; /* New change object */
1106e8d5648eSdan       int nByte;              /* Number of bytes to allocate */
1107e8d5648eSdan       int i;                  /* Used to iterate through columns */
1108e8d5648eSdan 
1109b4480e94Sdan       assert( rc==SQLITE_OK );
1110e8d5648eSdan       pTab->nEntry++;
1111e8d5648eSdan 
1112e8d5648eSdan       /* Figure out how large an allocation is required */
1113e8d5648eSdan       nByte = sizeof(SessionChange);
111480fe2d93Sdan       for(i=0; i<pTab->nCol; i++){
1115e8d5648eSdan         sqlite3_value *p = 0;
1116e8d5648eSdan         if( op!=SQLITE_INSERT ){
1117cf8e9144Sdan           TESTONLY(int trc = ) pSession->hook.xOld(pSession->hook.pCtx, i, &p);
111880fe2d93Sdan           assert( trc==SQLITE_OK );
111980fe2d93Sdan         }else if( pTab->abPK[i] ){
1120cf8e9144Sdan           TESTONLY(int trc = ) pSession->hook.xNew(pSession->hook.pCtx, i, &p);
112180fe2d93Sdan           assert( trc==SQLITE_OK );
1122e8d5648eSdan         }
112380fe2d93Sdan 
112480fe2d93Sdan         /* This may fail if SQLite value p contains a utf-16 string that must
112580fe2d93Sdan         ** be converted to utf-8 and an OOM error occurs while doing so. */
1126e8d5648eSdan         rc = sessionSerializeValue(0, p, &nByte);
112780fe2d93Sdan         if( rc!=SQLITE_OK ) goto error_out;
1128e8d5648eSdan       }
1129e8d5648eSdan 
1130e8d5648eSdan       /* Allocate the change object */
1131e8d5648eSdan       pChange = (SessionChange *)sqlite3_malloc(nByte);
1132e8d5648eSdan       if( !pChange ){
1133e8d5648eSdan         rc = SQLITE_NOMEM;
113480fe2d93Sdan         goto error_out;
1135e8d5648eSdan       }else{
1136e8d5648eSdan         memset(pChange, 0, sizeof(SessionChange));
1137e8d5648eSdan         pChange->aRecord = (u8 *)&pChange[1];
1138e8d5648eSdan       }
1139e8d5648eSdan 
114080fe2d93Sdan       /* Populate the change object. None of the preupdate_old(),
114180fe2d93Sdan       ** preupdate_new() or SerializeValue() calls below may fail as all
114280fe2d93Sdan       ** required values and encodings have already been cached in memory.
114380fe2d93Sdan       ** It is not possible for an OOM to occur in this block. */
1144e8d5648eSdan       nByte = 0;
114580fe2d93Sdan       for(i=0; i<pTab->nCol; i++){
1146e8d5648eSdan         sqlite3_value *p = 0;
1147e8d5648eSdan         if( op!=SQLITE_INSERT ){
1148cf8e9144Sdan           pSession->hook.xOld(pSession->hook.pCtx, i, &p);
114980fe2d93Sdan         }else if( pTab->abPK[i] ){
1150cf8e9144Sdan           pSession->hook.xNew(pSession->hook.pCtx, i, &p);
1151e8d5648eSdan         }
115280fe2d93Sdan         sessionSerializeValue(&pChange->aRecord[nByte], p, &nByte);
1153e8d5648eSdan       }
115480fe2d93Sdan 
115580fe2d93Sdan       /* Add the change to the hash-table */
1156cf8e9144Sdan       if( pSession->bIndirect || pSession->hook.xDepth(pSession->hook.pCtx) ){
1157b4480e94Sdan         pChange->bIndirect = 1;
1158b4480e94Sdan       }
115912ca0b56Sdan       pChange->nRecord = nByte;
1160798693b2Sdan       pChange->op = op;
1161e8d5648eSdan       pChange->pNext = pTab->apChange[iHash];
1162e8d5648eSdan       pTab->apChange[iHash] = pChange;
116380fe2d93Sdan 
116480fe2d93Sdan     }else if( pC->bIndirect ){
1165b4480e94Sdan       /* If the existing change is considered "indirect", but this current
1166b4480e94Sdan       ** change is "direct", mark the change object as direct. */
1167cf8e9144Sdan       if( pSession->hook.xDepth(pSession->hook.pCtx)==0
1168cf8e9144Sdan        && pSession->bIndirect==0
1169cf8e9144Sdan       ){
1170b4480e94Sdan         pC->bIndirect = 0;
1171b4480e94Sdan       }
1172e8d5648eSdan     }
11734fccf43aSdan   }
117412ca0b56Sdan 
117512ca0b56Sdan   /* If an error has occurred, mark the session object as failed. */
117680fe2d93Sdan  error_out:
117712ca0b56Sdan   if( rc!=SQLITE_OK ){
117812ca0b56Sdan     pSession->rc = rc;
117912ca0b56Sdan   }
118027453faeSdan }
11814fccf43aSdan 
1182cf8e9144Sdan static int sessionFindTable(
1183cf8e9144Sdan   sqlite3_session *pSession,
1184cf8e9144Sdan   const char *zName,
1185cf8e9144Sdan   SessionTable **ppTab
1186cf8e9144Sdan ){
1187cf8e9144Sdan   int rc = SQLITE_OK;
1188cf8e9144Sdan   int nName = sqlite3Strlen30(zName);
1189cf8e9144Sdan   SessionTable *pRet;
1190cf8e9144Sdan 
1191cf8e9144Sdan   /* Search for an existing table */
1192cf8e9144Sdan   for(pRet=pSession->pTable; pRet; pRet=pRet->pNext){
1193cf8e9144Sdan     if( 0==sqlite3_strnicmp(pRet->zName, zName, nName+1) ) break;
1194cf8e9144Sdan   }
1195cf8e9144Sdan 
1196cf8e9144Sdan   if( pRet==0 && pSession->bAutoAttach ){
1197cf8e9144Sdan     /* If there is a table-filter configured, invoke it. If it returns 0,
1198cf8e9144Sdan     ** do not automatically add the new table. */
1199cf8e9144Sdan     if( pSession->xTableFilter==0
1200cf8e9144Sdan      || pSession->xTableFilter(pSession->pFilterCtx, zName)
1201cf8e9144Sdan     ){
1202cf8e9144Sdan       rc = sqlite3session_attach(pSession, zName);
1203cf8e9144Sdan       if( rc==SQLITE_OK ){
12046c39e6a8Sdan         for(pRet=pSession->pTable; pRet->pNext; pRet=pRet->pNext);
1205cf8e9144Sdan         assert( 0==sqlite3_strnicmp(pRet->zName, zName, nName+1) );
1206cf8e9144Sdan       }
1207cf8e9144Sdan     }
1208cf8e9144Sdan   }
1209cf8e9144Sdan 
1210cf8e9144Sdan   assert( rc==SQLITE_OK || pRet==0 );
1211cf8e9144Sdan   *ppTab = pRet;
1212cf8e9144Sdan   return rc;
1213cf8e9144Sdan }
1214cf8e9144Sdan 
12154fccf43aSdan /*
12164fccf43aSdan ** The 'pre-update' hook registered by this module with SQLite databases.
12174fccf43aSdan */
12184fccf43aSdan static void xPreUpdate(
12194fccf43aSdan   void *pCtx,                     /* Copy of third arg to preupdate_hook() */
12204fccf43aSdan   sqlite3 *db,                    /* Database handle */
12214fccf43aSdan   int op,                         /* SQLITE_UPDATE, DELETE or INSERT */
12224fccf43aSdan   char const *zDb,                /* Database name */
12234fccf43aSdan   char const *zName,              /* Table name */
12244fccf43aSdan   sqlite3_int64 iKey1,            /* Rowid of row about to be deleted/updated */
12254fccf43aSdan   sqlite3_int64 iKey2             /* New rowid value (for a rowid UPDATE) */
12264fccf43aSdan ){
12274fccf43aSdan   sqlite3_session *pSession;
1228cfdbde21Sdrh   int nDb = sqlite3Strlen30(zDb);
12294fccf43aSdan 
12304c220252Sdan   assert( sqlite3_mutex_held(db->mutex) );
12314c220252Sdan 
12324fccf43aSdan   for(pSession=(sqlite3_session *)pCtx; pSession; pSession=pSession->pNext){
12334fccf43aSdan     SessionTable *pTab;
1234296c7658Sdan 
1235e8d5648eSdan     /* If this session is attached to a different database ("main", "temp"
1236e8d5648eSdan     ** etc.), or if it is not currently enabled, there is nothing to do. Skip
1237e8d5648eSdan     ** to the next session object attached to this database. */
1238296c7658Sdan     if( pSession->bEnable==0 ) continue;
12394fccf43aSdan     if( pSession->rc ) continue;
12404fccf43aSdan     if( sqlite3_strnicmp(zDb, pSession->zDb, nDb+1) ) continue;
1241296c7658Sdan 
1242cf8e9144Sdan     pSession->rc = sessionFindTable(pSession, zName, &pTab);
1243cf8e9144Sdan     if( pTab ){
1244cf8e9144Sdan       assert( pSession->rc==SQLITE_OK );
1245e8d5648eSdan       sessionPreupdateOneChange(op, pSession, pTab);
1246e8d5648eSdan       if( op==SQLITE_UPDATE ){
1247e8d5648eSdan         sessionPreupdateOneChange(SQLITE_INSERT, pSession, pTab);
12484fccf43aSdan       }
12494fccf43aSdan     }
12504fccf43aSdan   }
12514fccf43aSdan }
1252cf8e9144Sdan 
1253cf8e9144Sdan /*
1254cf8e9144Sdan ** The pre-update hook implementations.
1255cf8e9144Sdan */
1256cf8e9144Sdan static int sessionPreupdateOld(void *pCtx, int iVal, sqlite3_value **ppVal){
1257cf8e9144Sdan   return sqlite3_preupdate_old((sqlite3*)pCtx, iVal, ppVal);
1258cf8e9144Sdan }
1259cf8e9144Sdan static int sessionPreupdateNew(void *pCtx, int iVal, sqlite3_value **ppVal){
1260cf8e9144Sdan   return sqlite3_preupdate_new((sqlite3*)pCtx, iVal, ppVal);
1261cf8e9144Sdan }
1262cf8e9144Sdan static int sessionPreupdateCount(void *pCtx){
1263cf8e9144Sdan   return sqlite3_preupdate_count((sqlite3*)pCtx);
1264cf8e9144Sdan }
1265cf8e9144Sdan static int sessionPreupdateDepth(void *pCtx){
1266cf8e9144Sdan   return sqlite3_preupdate_depth((sqlite3*)pCtx);
1267cf8e9144Sdan }
1268cf8e9144Sdan 
1269cf8e9144Sdan /*
1270cf8e9144Sdan ** Install the pre-update hooks on the session object passed as the only
1271cf8e9144Sdan ** argument.
1272cf8e9144Sdan */
1273cf8e9144Sdan static void sessionPreupdateHooks(
1274cf8e9144Sdan   sqlite3_session *pSession
1275cf8e9144Sdan ){
1276cf8e9144Sdan   pSession->hook.pCtx = (void*)pSession->db;
1277cf8e9144Sdan   pSession->hook.xOld = sessionPreupdateOld;
1278cf8e9144Sdan   pSession->hook.xNew = sessionPreupdateNew;
1279cf8e9144Sdan   pSession->hook.xCount = sessionPreupdateCount;
1280cf8e9144Sdan   pSession->hook.xDepth = sessionPreupdateDepth;
1281cf8e9144Sdan }
1282cf8e9144Sdan 
1283cf8e9144Sdan typedef struct SessionDiffCtx SessionDiffCtx;
1284cf8e9144Sdan struct SessionDiffCtx {
1285cf8e9144Sdan   sqlite3_stmt *pStmt;
1286cf8e9144Sdan   int nOldOff;
1287cf8e9144Sdan };
1288cf8e9144Sdan 
1289cf8e9144Sdan /*
1290cf8e9144Sdan ** The diff hook implementations.
1291cf8e9144Sdan */
1292cf8e9144Sdan static int sessionDiffOld(void *pCtx, int iVal, sqlite3_value **ppVal){
1293cf8e9144Sdan   SessionDiffCtx *p = (SessionDiffCtx*)pCtx;
1294cf8e9144Sdan   *ppVal = sqlite3_column_value(p->pStmt, iVal+p->nOldOff);
1295cf8e9144Sdan   return SQLITE_OK;
1296cf8e9144Sdan }
1297cf8e9144Sdan static int sessionDiffNew(void *pCtx, int iVal, sqlite3_value **ppVal){
1298cf8e9144Sdan   SessionDiffCtx *p = (SessionDiffCtx*)pCtx;
1299cf8e9144Sdan   *ppVal = sqlite3_column_value(p->pStmt, iVal);
1300cf8e9144Sdan    return SQLITE_OK;
1301cf8e9144Sdan }
1302cf8e9144Sdan static int sessionDiffCount(void *pCtx){
1303cf8e9144Sdan   SessionDiffCtx *p = (SessionDiffCtx*)pCtx;
1304cf8e9144Sdan   return p->nOldOff ? p->nOldOff : sqlite3_column_count(p->pStmt);
1305cf8e9144Sdan }
1306cf8e9144Sdan static int sessionDiffDepth(void *pCtx){
1307cf8e9144Sdan   return 0;
1308cf8e9144Sdan }
1309cf8e9144Sdan 
1310cf8e9144Sdan /*
1311cf8e9144Sdan ** Install the diff hooks on the session object passed as the only
1312cf8e9144Sdan ** argument.
1313cf8e9144Sdan */
1314cf8e9144Sdan static void sessionDiffHooks(
1315cf8e9144Sdan   sqlite3_session *pSession,
1316cf8e9144Sdan   SessionDiffCtx *pDiffCtx
1317cf8e9144Sdan ){
1318cf8e9144Sdan   pSession->hook.pCtx = (void*)pDiffCtx;
1319cf8e9144Sdan   pSession->hook.xOld = sessionDiffOld;
1320cf8e9144Sdan   pSession->hook.xNew = sessionDiffNew;
1321cf8e9144Sdan   pSession->hook.xCount = sessionDiffCount;
1322cf8e9144Sdan   pSession->hook.xDepth = sessionDiffDepth;
1323cf8e9144Sdan }
1324cf8e9144Sdan 
1325cf8e9144Sdan static char *sessionExprComparePK(
1326cf8e9144Sdan   int nCol,
1327cf8e9144Sdan   const char *zDb1, const char *zDb2,
1328cf8e9144Sdan   const char *zTab,
1329cf8e9144Sdan   const char **azCol, u8 *abPK
1330cf8e9144Sdan ){
1331cf8e9144Sdan   int i;
1332cf8e9144Sdan   const char *zSep = "";
1333cf8e9144Sdan   char *zRet = 0;
1334cf8e9144Sdan 
1335cf8e9144Sdan   for(i=0; i<nCol; i++){
1336cf8e9144Sdan     if( abPK[i] ){
1337cf8e9144Sdan       zRet = sqlite3_mprintf("%z%s\"%w\".\"%w\".\"%w\"=\"%w\".\"%w\".\"%w\"",
1338cf8e9144Sdan           zRet, zSep, zDb1, zTab, azCol[i], zDb2, zTab, azCol[i]
1339cf8e9144Sdan       );
1340cf8e9144Sdan       zSep = " AND ";
1341cf8e9144Sdan       if( zRet==0 ) break;
1342cf8e9144Sdan     }
1343cf8e9144Sdan   }
1344cf8e9144Sdan 
1345cf8e9144Sdan   return zRet;
1346cf8e9144Sdan }
1347cf8e9144Sdan 
1348cf8e9144Sdan static char *sessionExprCompareOther(
1349cf8e9144Sdan   int nCol,
1350cf8e9144Sdan   const char *zDb1, const char *zDb2,
1351cf8e9144Sdan   const char *zTab,
1352cf8e9144Sdan   const char **azCol, u8 *abPK
1353cf8e9144Sdan ){
1354cf8e9144Sdan   int i;
1355cf8e9144Sdan   const char *zSep = "";
1356cf8e9144Sdan   char *zRet = 0;
1357cf8e9144Sdan   int bHave = 0;
1358cf8e9144Sdan 
1359cf8e9144Sdan   for(i=0; i<nCol; i++){
1360cf8e9144Sdan     if( abPK[i]==0 ){
1361cf8e9144Sdan       bHave = 1;
1362cf8e9144Sdan       zRet = sqlite3_mprintf(
1363cf8e9144Sdan           "%z%s\"%w\".\"%w\".\"%w\" IS NOT \"%w\".\"%w\".\"%w\"",
1364cf8e9144Sdan           zRet, zSep, zDb1, zTab, azCol[i], zDb2, zTab, azCol[i]
1365cf8e9144Sdan       );
1366cf8e9144Sdan       zSep = " OR ";
1367cf8e9144Sdan       if( zRet==0 ) break;
1368cf8e9144Sdan     }
1369cf8e9144Sdan   }
1370cf8e9144Sdan 
1371cf8e9144Sdan   if( bHave==0 ){
1372cf8e9144Sdan     assert( zRet==0 );
1373cf8e9144Sdan     zRet = sqlite3_mprintf("0");
1374cf8e9144Sdan   }
1375cf8e9144Sdan 
1376cf8e9144Sdan   return zRet;
1377cf8e9144Sdan }
1378cf8e9144Sdan 
1379cf8e9144Sdan static char *sessionSelectFindNew(
1380cf8e9144Sdan   int nCol,
1381cf8e9144Sdan   const char *zDb1,      /* Pick rows in this db only */
1382cf8e9144Sdan   const char *zDb2,      /* But not in this one */
1383cf8e9144Sdan   const char *zTbl,      /* Table name */
1384cf8e9144Sdan   const char *zExpr
1385cf8e9144Sdan ){
1386cf8e9144Sdan   char *zRet = sqlite3_mprintf(
1387cf8e9144Sdan       "SELECT * FROM \"%w\".\"%w\" WHERE NOT EXISTS ("
1388cf8e9144Sdan       "  SELECT 1 FROM \"%w\".\"%w\" WHERE %s"
1389cf8e9144Sdan       ")",
1390cf8e9144Sdan       zDb1, zTbl, zDb2, zTbl, zExpr
1391cf8e9144Sdan   );
1392cf8e9144Sdan   return zRet;
1393cf8e9144Sdan }
1394cf8e9144Sdan 
1395cf8e9144Sdan static int sessionDiffFindNew(
1396cf8e9144Sdan   int op,
1397cf8e9144Sdan   sqlite3_session *pSession,
1398cf8e9144Sdan   SessionTable *pTab,
1399cf8e9144Sdan   const char *zDb1,
1400cf8e9144Sdan   const char *zDb2,
1401cf8e9144Sdan   char *zExpr
1402cf8e9144Sdan ){
1403cf8e9144Sdan   int rc = SQLITE_OK;
1404cf8e9144Sdan   char *zStmt = sessionSelectFindNew(pTab->nCol, zDb1, zDb2, pTab->zName,zExpr);
1405cf8e9144Sdan 
1406cf8e9144Sdan   if( zStmt==0 ){
1407cf8e9144Sdan     rc = SQLITE_NOMEM;
1408cf8e9144Sdan   }else{
1409cf8e9144Sdan     sqlite3_stmt *pStmt;
1410cf8e9144Sdan     rc = sqlite3_prepare(pSession->db, zStmt, -1, &pStmt, 0);
1411cf8e9144Sdan     if( rc==SQLITE_OK ){
1412cf8e9144Sdan       SessionDiffCtx *pDiffCtx = (SessionDiffCtx*)pSession->hook.pCtx;
1413cf8e9144Sdan       pDiffCtx->pStmt = pStmt;
1414cf8e9144Sdan       pDiffCtx->nOldOff = 0;
1415cf8e9144Sdan       while( SQLITE_ROW==sqlite3_step(pStmt) ){
1416cf8e9144Sdan         sessionPreupdateOneChange(op, pSession, pTab);
1417cf8e9144Sdan       }
1418cf8e9144Sdan       rc = sqlite3_finalize(pStmt);
1419cf8e9144Sdan     }
1420cf8e9144Sdan     sqlite3_free(zStmt);
1421cf8e9144Sdan   }
1422cf8e9144Sdan 
1423cf8e9144Sdan   return rc;
1424cf8e9144Sdan }
1425cf8e9144Sdan 
1426cf8e9144Sdan static int sessionDiffFindModified(
1427cf8e9144Sdan   sqlite3_session *pSession,
1428cf8e9144Sdan   SessionTable *pTab,
1429cf8e9144Sdan   const char *zFrom,
1430cf8e9144Sdan   const char *zExpr
1431cf8e9144Sdan ){
1432cf8e9144Sdan   int rc = SQLITE_OK;
1433cf8e9144Sdan 
1434cf8e9144Sdan   char *zExpr2 = sessionExprCompareOther(pTab->nCol,
1435cf8e9144Sdan       pSession->zDb, zFrom, pTab->zName, pTab->azCol, pTab->abPK
1436cf8e9144Sdan   );
1437cf8e9144Sdan   if( zExpr2==0 ){
1438cf8e9144Sdan     rc = SQLITE_NOMEM;
1439cf8e9144Sdan   }else{
1440cf8e9144Sdan     char *zStmt = sqlite3_mprintf(
1441dd009f83Sdan         "SELECT * FROM \"%w\".\"%w\", \"%w\".\"%w\" WHERE %s AND (%z)",
1442cf8e9144Sdan         pSession->zDb, pTab->zName, zFrom, pTab->zName, zExpr, zExpr2
1443cf8e9144Sdan     );
1444cf8e9144Sdan     if( zStmt==0 ){
1445cf8e9144Sdan       rc = SQLITE_NOMEM;
1446cf8e9144Sdan     }else{
1447cf8e9144Sdan       sqlite3_stmt *pStmt;
1448cf8e9144Sdan       rc = sqlite3_prepare(pSession->db, zStmt, -1, &pStmt, 0);
1449cf8e9144Sdan 
1450cf8e9144Sdan       if( rc==SQLITE_OK ){
1451cf8e9144Sdan         SessionDiffCtx *pDiffCtx = (SessionDiffCtx*)pSession->hook.pCtx;
1452cf8e9144Sdan         pDiffCtx->pStmt = pStmt;
1453cf8e9144Sdan         pDiffCtx->nOldOff = pTab->nCol;
1454cf8e9144Sdan         while( SQLITE_ROW==sqlite3_step(pStmt) ){
1455cf8e9144Sdan           sessionPreupdateOneChange(SQLITE_UPDATE, pSession, pTab);
1456cf8e9144Sdan         }
1457cf8e9144Sdan         rc = sqlite3_finalize(pStmt);
1458cf8e9144Sdan       }
1459cf8e9144Sdan       sqlite3_free(zStmt);
1460cf8e9144Sdan     }
1461cf8e9144Sdan   }
1462cf8e9144Sdan 
1463cf8e9144Sdan   return rc;
1464cf8e9144Sdan }
1465cf8e9144Sdan 
1466cf8e9144Sdan int sqlite3session_diff(
1467cf8e9144Sdan   sqlite3_session *pSession,
1468cf8e9144Sdan   const char *zFrom,
1469cf8e9144Sdan   const char *zTbl,
1470cf8e9144Sdan   char **pzErrMsg
1471cf8e9144Sdan ){
1472cf8e9144Sdan   const char *zDb = pSession->zDb;
1473cf8e9144Sdan   int rc = pSession->rc;
1474cf8e9144Sdan   SessionDiffCtx d;
1475cf8e9144Sdan 
1476cf8e9144Sdan   memset(&d, 0, sizeof(d));
1477cf8e9144Sdan   sessionDiffHooks(pSession, &d);
1478cf8e9144Sdan 
147910dc553cSdan   sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
1480cf8e9144Sdan   if( pzErrMsg ) *pzErrMsg = 0;
1481cf8e9144Sdan   if( rc==SQLITE_OK ){
1482cf8e9144Sdan     char *zExpr = 0;
1483cf8e9144Sdan     sqlite3 *db = pSession->db;
1484cf8e9144Sdan     SessionTable *pTo;            /* Table zTbl */
1485cf8e9144Sdan 
1486cf8e9144Sdan     /* Locate and if necessary initialize the target table object */
1487cf8e9144Sdan     rc = sessionFindTable(pSession, zTbl, &pTo);
1488cf8e9144Sdan     if( pTo==0 ) goto diff_out;
14896dc29e60Sdan     if( sessionInitTable(pSession, pTo) ){
14906dc29e60Sdan       rc = pSession->rc;
14916dc29e60Sdan       goto diff_out;
1492cf8e9144Sdan     }
1493cf8e9144Sdan 
1494cf8e9144Sdan     /* Check the table schemas match */
1495cf8e9144Sdan     if( rc==SQLITE_OK ){
14964cc923e3Sdan       int bHasPk = 0;
1497b9db9099Sdan       int bMismatch = 0;
1498cf8e9144Sdan       int nCol;                   /* Columns in zFrom.zTbl */
1499cf8e9144Sdan       u8 *abPK;
1500cf8e9144Sdan       const char **azCol = 0;
1501cf8e9144Sdan       rc = sessionTableInfo(db, zFrom, zTbl, &nCol, 0, &azCol, &abPK);
1502cf8e9144Sdan       if( rc==SQLITE_OK ){
15034cc923e3Sdan         if( pTo->nCol!=nCol ){
1504cf8e9144Sdan           bMismatch = 1;
1505cf8e9144Sdan         }else{
1506cf8e9144Sdan           int i;
1507cf8e9144Sdan           for(i=0; i<nCol; i++){
15084cc923e3Sdan             if( pTo->abPK[i]!=abPK[i] ) bMismatch = 1;
1509cf8e9144Sdan             if( sqlite3_stricmp(azCol[i], pTo->azCol[i]) ) bMismatch = 1;
15104cc923e3Sdan             if( abPK[i] ) bHasPk = 1;
1511cf8e9144Sdan           }
1512cf8e9144Sdan         }
1513cf8e9144Sdan 
1514cf8e9144Sdan       }
1515dbbd8160Sdrh       sqlite3_free((char*)azCol);
1516b9db9099Sdan       if( bMismatch ){
1517b9db9099Sdan         *pzErrMsg = sqlite3_mprintf("table schemas do not match");
1518b9db9099Sdan         rc = SQLITE_SCHEMA;
1519b9db9099Sdan       }
1520b9db9099Sdan       if( bHasPk==0 ){
1521b9db9099Sdan         /* Ignore tables with no primary keys */
1522b9db9099Sdan         goto diff_out;
1523b9db9099Sdan       }
1524cf8e9144Sdan     }
1525cf8e9144Sdan 
1526cf8e9144Sdan     if( rc==SQLITE_OK ){
1527cf8e9144Sdan       zExpr = sessionExprComparePK(pTo->nCol,
1528cf8e9144Sdan           zDb, zFrom, pTo->zName, pTo->azCol, pTo->abPK
1529cf8e9144Sdan       );
1530cf8e9144Sdan     }
1531cf8e9144Sdan 
1532cf8e9144Sdan     /* Find new rows */
1533cf8e9144Sdan     if( rc==SQLITE_OK ){
1534cf8e9144Sdan       rc = sessionDiffFindNew(SQLITE_INSERT, pSession, pTo, zDb, zFrom, zExpr);
1535cf8e9144Sdan     }
1536cf8e9144Sdan 
1537cf8e9144Sdan     /* Find old rows */
1538cf8e9144Sdan     if( rc==SQLITE_OK ){
1539cf8e9144Sdan       rc = sessionDiffFindNew(SQLITE_DELETE, pSession, pTo, zFrom, zDb, zExpr);
1540cf8e9144Sdan     }
1541cf8e9144Sdan 
1542cf8e9144Sdan     /* Find modified rows */
1543cf8e9144Sdan     if( rc==SQLITE_OK ){
1544cf8e9144Sdan       rc = sessionDiffFindModified(pSession, pTo, zFrom, zExpr);
1545cf8e9144Sdan     }
1546cf8e9144Sdan 
1547cf8e9144Sdan     sqlite3_free(zExpr);
1548cf8e9144Sdan   }
1549cf8e9144Sdan 
1550cf8e9144Sdan  diff_out:
1551cf8e9144Sdan   sessionPreupdateHooks(pSession);
155210dc553cSdan   sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
1553cf8e9144Sdan   return rc;
1554296c7658Sdan }
15554fccf43aSdan 
15564fccf43aSdan /*
15574fccf43aSdan ** Create a session object. This session object will record changes to
15584fccf43aSdan ** database zDb attached to connection db.
15594fccf43aSdan */
15604fccf43aSdan int sqlite3session_create(
15614fccf43aSdan   sqlite3 *db,                    /* Database handle */
15624fccf43aSdan   const char *zDb,                /* Name of db (e.g. "main") */
15634fccf43aSdan   sqlite3_session **ppSession     /* OUT: New session object */
15644fccf43aSdan ){
1565296c7658Sdan   sqlite3_session *pNew;          /* Newly allocated session object */
1566296c7658Sdan   sqlite3_session *pOld;          /* Session object already attached to db */
1567cfdbde21Sdrh   int nDb = sqlite3Strlen30(zDb); /* Length of zDb in bytes */
15684fccf43aSdan 
1569296c7658Sdan   /* Zero the output value in case an error occurs. */
15704fccf43aSdan   *ppSession = 0;
15714fccf43aSdan 
15724fccf43aSdan   /* Allocate and populate the new session object. */
15734fccf43aSdan   pNew = (sqlite3_session *)sqlite3_malloc(sizeof(sqlite3_session) + nDb + 1);
15744fccf43aSdan   if( !pNew ) return SQLITE_NOMEM;
15754fccf43aSdan   memset(pNew, 0, sizeof(sqlite3_session));
15764fccf43aSdan   pNew->db = db;
15774fccf43aSdan   pNew->zDb = (char *)&pNew[1];
1578296c7658Sdan   pNew->bEnable = 1;
15794fccf43aSdan   memcpy(pNew->zDb, zDb, nDb+1);
1580cf8e9144Sdan   sessionPreupdateHooks(pNew);
15814fccf43aSdan 
15824fccf43aSdan   /* Add the new session object to the linked list of session objects
15834fccf43aSdan   ** attached to database handle $db. Do this under the cover of the db
15844fccf43aSdan   ** handle mutex.  */
15854fccf43aSdan   sqlite3_mutex_enter(sqlite3_db_mutex(db));
15864fccf43aSdan   pOld = (sqlite3_session*)sqlite3_preupdate_hook(db, xPreUpdate, (void*)pNew);
15874fccf43aSdan   pNew->pNext = pOld;
15884fccf43aSdan   sqlite3_mutex_leave(sqlite3_db_mutex(db));
15894fccf43aSdan 
15904fccf43aSdan   *ppSession = pNew;
15914fccf43aSdan   return SQLITE_OK;
15924fccf43aSdan }
15934fccf43aSdan 
159477fc1d5bSdan /*
159577fc1d5bSdan ** Free the list of table objects passed as the first argument. The contents
159677fc1d5bSdan ** of the changed-rows hash tables are also deleted.
159777fc1d5bSdan */
15981ffe7c7fSdrh static void sessionDeleteTable(SessionTable *pList){
15995d607a6eSdan   SessionTable *pNext;
16005d607a6eSdan   SessionTable *pTab;
16015d607a6eSdan 
16025d607a6eSdan   for(pTab=pList; pTab; pTab=pNext){
16035d607a6eSdan     int i;
16045d607a6eSdan     pNext = pTab->pNext;
16055d607a6eSdan     for(i=0; i<pTab->nChange; i++){
16065d607a6eSdan       SessionChange *p;
160702d436b1Smistachkin       SessionChange *pNextChange;
160802d436b1Smistachkin       for(p=pTab->apChange[i]; p; p=pNextChange){
160902d436b1Smistachkin         pNextChange = p->pNext;
16105d607a6eSdan         sqlite3_free(p);
16115d607a6eSdan       }
16125d607a6eSdan     }
16135d607a6eSdan     sqlite3_free((char*)pTab->azCol);  /* cast works around VC++ bug */
16145d607a6eSdan     sqlite3_free(pTab->apChange);
16155d607a6eSdan     sqlite3_free(pTab);
16165d607a6eSdan   }
16175d607a6eSdan }
16185d607a6eSdan 
16194fccf43aSdan /*
16204fccf43aSdan ** Delete a session object previously allocated using sqlite3session_create().
16214fccf43aSdan */
16224fccf43aSdan void sqlite3session_delete(sqlite3_session *pSession){
16234fccf43aSdan   sqlite3 *db = pSession->db;
16244fccf43aSdan   sqlite3_session *pHead;
16254fccf43aSdan   sqlite3_session **pp;
16264fccf43aSdan 
1627296c7658Sdan   /* Unlink the session from the linked list of sessions attached to the
1628296c7658Sdan   ** database handle. Hold the db mutex while doing so.  */
16294fccf43aSdan   sqlite3_mutex_enter(sqlite3_db_mutex(db));
16304fccf43aSdan   pHead = (sqlite3_session*)sqlite3_preupdate_hook(db, 0, 0);
163150d348b1Sdrh   for(pp=&pHead; ALWAYS((*pp)!=0); pp=&((*pp)->pNext)){
163250d348b1Sdrh     if( (*pp)==pSession ){
16334fccf43aSdan       *pp = (*pp)->pNext;
16344fccf43aSdan       if( pHead ) sqlite3_preupdate_hook(db, xPreUpdate, (void*)pHead);
163550d348b1Sdrh       break;
163650d348b1Sdrh     }
163750d348b1Sdrh   }
16384fccf43aSdan   sqlite3_mutex_leave(sqlite3_db_mutex(db));
16394fccf43aSdan 
1640296c7658Sdan   /* Delete all attached table objects. And the contents of their
1641296c7658Sdan   ** associated hash-tables. */
16425d607a6eSdan   sessionDeleteTable(pSession->pTable);
16434fccf43aSdan 
1644296c7658Sdan   /* Free the session object itself. */
16454fccf43aSdan   sqlite3_free(pSession);
16464fccf43aSdan }
16474fccf43aSdan 
16484fccf43aSdan /*
16497531a5a3Sdan ** Set a table filter on a Session Object.
16507531a5a3Sdan */
16517531a5a3Sdan void sqlite3session_table_filter(
16527531a5a3Sdan   sqlite3_session *pSession,
16537531a5a3Sdan   int(*xFilter)(void*, const char*),
16547531a5a3Sdan   void *pCtx                      /* First argument passed to xFilter */
16557531a5a3Sdan ){
16567531a5a3Sdan   pSession->bAutoAttach = 1;
16577531a5a3Sdan   pSession->pFilterCtx = pCtx;
16587531a5a3Sdan   pSession->xTableFilter = xFilter;
16597531a5a3Sdan }
16607531a5a3Sdan 
16617531a5a3Sdan /*
16624fccf43aSdan ** Attach a table to a session. All subsequent changes made to the table
16634fccf43aSdan ** while the session object is enabled will be recorded.
16644fccf43aSdan **
16654fccf43aSdan ** Only tables that have a PRIMARY KEY defined may be attached. It does
16664fccf43aSdan ** not matter if the PRIMARY KEY is an "INTEGER PRIMARY KEY" (rowid alias)
16674fccf43aSdan ** or not.
16684fccf43aSdan */
16694fccf43aSdan int sqlite3session_attach(
16704fccf43aSdan   sqlite3_session *pSession,      /* Session object */
16714fccf43aSdan   const char *zName               /* Table name */
16724fccf43aSdan ){
1673ff4d0f41Sdan   int rc = SQLITE_OK;
1674ff4d0f41Sdan   sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
1675ff4d0f41Sdan 
1676ff4d0f41Sdan   if( !zName ){
1677ff4d0f41Sdan     pSession->bAutoAttach = 1;
1678ff4d0f41Sdan   }else{
1679296c7658Sdan     SessionTable *pTab;           /* New table object (if required) */
1680296c7658Sdan     int nName;                    /* Number of bytes in string zName */
16814fccf43aSdan 
16824fccf43aSdan     /* First search for an existing entry. If one is found, this call is
16834fccf43aSdan     ** a no-op. Return early. */
1684cfdbde21Sdrh     nName = sqlite3Strlen30(zName);
16854fccf43aSdan     for(pTab=pSession->pTable; pTab; pTab=pTab->pNext){
16864c220252Sdan       if( 0==sqlite3_strnicmp(pTab->zName, zName, nName+1) ) break;
16874fccf43aSdan     }
16884fccf43aSdan 
16894c220252Sdan     if( !pTab ){
16904fccf43aSdan       /* Allocate new SessionTable object. */
16914fccf43aSdan       pTab = (SessionTable *)sqlite3_malloc(sizeof(SessionTable) + nName + 1);
16924c220252Sdan       if( !pTab ){
16934c220252Sdan         rc = SQLITE_NOMEM;
16944c220252Sdan       }else{
16956c39e6a8Sdan         /* Populate the new SessionTable object and link it into the list.
16966c39e6a8Sdan         ** The new object must be linked onto the end of the list, not
16976c39e6a8Sdan         ** simply added to the start of it in order to ensure that tables
16986c39e6a8Sdan         ** appear in the correct order when a changeset or patchset is
16996c39e6a8Sdan         ** eventually generated. */
17006c39e6a8Sdan         SessionTable **ppTab;
17014fccf43aSdan         memset(pTab, 0, sizeof(SessionTable));
17024fccf43aSdan         pTab->zName = (char *)&pTab[1];
17034fccf43aSdan         memcpy(pTab->zName, zName, nName+1);
17046c39e6a8Sdan         for(ppTab=&pSession->pTable; *ppTab; ppTab=&(*ppTab)->pNext);
17056c39e6a8Sdan         *ppTab = pTab;
17064c220252Sdan       }
17074c220252Sdan     }
1708ff4d0f41Sdan   }
17094fccf43aSdan 
17104c220252Sdan   sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
17114c220252Sdan   return rc;
17124fccf43aSdan }
17134fccf43aSdan 
1714296c7658Sdan /*
1715296c7658Sdan ** Ensure that there is room in the buffer to append nByte bytes of data.
1716296c7658Sdan ** If not, use sqlite3_realloc() to grow the buffer so that there is.
1717296c7658Sdan **
1718296c7658Sdan ** If successful, return zero. Otherwise, if an OOM condition is encountered,
1719296c7658Sdan ** set *pRc to SQLITE_NOMEM and return non-zero.
1720296c7658Sdan */
17214fccf43aSdan static int sessionBufferGrow(SessionBuffer *p, int nByte, int *pRc){
172280fe2d93Sdan   if( *pRc==SQLITE_OK && p->nAlloc-p->nBuf<nByte ){
17234fccf43aSdan     u8 *aNew;
17244fccf43aSdan     int nNew = p->nAlloc ? p->nAlloc : 128;
17254fccf43aSdan     do {
17264fccf43aSdan       nNew = nNew*2;
1727ef7a6304Sdan     }while( nNew<(p->nBuf+nByte) );
17284fccf43aSdan 
17294fccf43aSdan     aNew = (u8 *)sqlite3_realloc(p->aBuf, nNew);
17304fccf43aSdan     if( 0==aNew ){
17314fccf43aSdan       *pRc = SQLITE_NOMEM;
173280fe2d93Sdan     }else{
17334fccf43aSdan       p->aBuf = aNew;
17344fccf43aSdan       p->nAlloc = nNew;
17354fccf43aSdan     }
173680fe2d93Sdan   }
173780fe2d93Sdan   return (*pRc!=SQLITE_OK);
17384fccf43aSdan }
17394fccf43aSdan 
1740296c7658Sdan /*
1741fa122adaSdan ** Append the value passed as the second argument to the buffer passed
1742fa122adaSdan ** as the first.
1743fa122adaSdan **
1744fa122adaSdan ** This function is a no-op if *pRc is non-zero when it is called.
1745fa122adaSdan ** Otherwise, if an error occurs, *pRc is set to an SQLite error code
1746fa122adaSdan ** before returning.
1747fa122adaSdan */
1748fa122adaSdan static void sessionAppendValue(SessionBuffer *p, sqlite3_value *pVal, int *pRc){
1749fa122adaSdan   int rc = *pRc;
1750fa122adaSdan   if( rc==SQLITE_OK ){
1751fa122adaSdan     int nByte = 0;
1752e8fa8c96Sdan     rc = sessionSerializeValue(0, pVal, &nByte);
1753fa122adaSdan     sessionBufferGrow(p, nByte, &rc);
1754fa122adaSdan     if( rc==SQLITE_OK ){
1755fa122adaSdan       rc = sessionSerializeValue(&p->aBuf[p->nBuf], pVal, 0);
1756fa122adaSdan       p->nBuf += nByte;
1757fa122adaSdan     }else{
1758fa122adaSdan       *pRc = rc;
1759fa122adaSdan     }
1760fa122adaSdan   }
1761fa122adaSdan }
1762fa122adaSdan 
1763fa122adaSdan /*
1764296c7658Sdan ** This function is a no-op if *pRc is other than SQLITE_OK when it is
1765296c7658Sdan ** called. Otherwise, append a single byte to the buffer.
1766296c7658Sdan **
1767296c7658Sdan ** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
1768296c7658Sdan ** returning.
1769296c7658Sdan */
17704fccf43aSdan static void sessionAppendByte(SessionBuffer *p, u8 v, int *pRc){
177180fe2d93Sdan   if( 0==sessionBufferGrow(p, 1, pRc) ){
17724fccf43aSdan     p->aBuf[p->nBuf++] = v;
17734fccf43aSdan   }
17744fccf43aSdan }
17754fccf43aSdan 
1776296c7658Sdan /*
1777296c7658Sdan ** This function is a no-op if *pRc is other than SQLITE_OK when it is
1778296c7658Sdan ** called. Otherwise, append a single varint to the buffer.
1779296c7658Sdan **
1780296c7658Sdan ** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
1781296c7658Sdan ** returning.
1782296c7658Sdan */
1783cfdbde21Sdrh static void sessionAppendVarint(SessionBuffer *p, int v, int *pRc){
178480fe2d93Sdan   if( 0==sessionBufferGrow(p, 9, pRc) ){
17854fccf43aSdan     p->nBuf += sessionVarintPut(&p->aBuf[p->nBuf], v);
17864fccf43aSdan   }
17874fccf43aSdan }
17884fccf43aSdan 
1789296c7658Sdan /*
1790296c7658Sdan ** This function is a no-op if *pRc is other than SQLITE_OK when it is
1791296c7658Sdan ** called. Otherwise, append a blob of data to the buffer.
1792296c7658Sdan **
1793296c7658Sdan ** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
1794296c7658Sdan ** returning.
1795296c7658Sdan */
17964fccf43aSdan static void sessionAppendBlob(
17974fccf43aSdan   SessionBuffer *p,
17984fccf43aSdan   const u8 *aBlob,
17994fccf43aSdan   int nBlob,
18004fccf43aSdan   int *pRc
18014fccf43aSdan ){
1802895decf6Sdan   if( nBlob>0 && 0==sessionBufferGrow(p, nBlob, pRc) ){
18034fccf43aSdan     memcpy(&p->aBuf[p->nBuf], aBlob, nBlob);
18044fccf43aSdan     p->nBuf += nBlob;
18054fccf43aSdan   }
18064fccf43aSdan }
18074fccf43aSdan 
1808296c7658Sdan /*
1809296c7658Sdan ** This function is a no-op if *pRc is other than SQLITE_OK when it is
1810296c7658Sdan ** called. Otherwise, append a string to the buffer. All bytes in the string
1811296c7658Sdan ** up to (but not including) the nul-terminator are written to the buffer.
1812296c7658Sdan **
1813296c7658Sdan ** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
1814296c7658Sdan ** returning.
1815296c7658Sdan */
1816d5f0767cSdan static void sessionAppendStr(
1817d5f0767cSdan   SessionBuffer *p,
1818d5f0767cSdan   const char *zStr,
1819d5f0767cSdan   int *pRc
1820d5f0767cSdan ){
1821cfdbde21Sdrh   int nStr = sqlite3Strlen30(zStr);
182280fe2d93Sdan   if( 0==sessionBufferGrow(p, nStr, pRc) ){
1823d5f0767cSdan     memcpy(&p->aBuf[p->nBuf], zStr, nStr);
1824d5f0767cSdan     p->nBuf += nStr;
1825d5f0767cSdan   }
1826d5f0767cSdan }
1827d5f0767cSdan 
1828296c7658Sdan /*
1829296c7658Sdan ** This function is a no-op if *pRc is other than SQLITE_OK when it is
1830296c7658Sdan ** called. Otherwise, append the string representation of integer iVal
1831296c7658Sdan ** to the buffer. No nul-terminator is written.
1832296c7658Sdan **
1833296c7658Sdan ** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
1834296c7658Sdan ** returning.
1835296c7658Sdan */
1836d5f0767cSdan static void sessionAppendInteger(
1837296c7658Sdan   SessionBuffer *p,               /* Buffer to append to */
1838296c7658Sdan   int iVal,                       /* Value to write the string rep. of */
1839296c7658Sdan   int *pRc                        /* IN/OUT: Error code */
1840d5f0767cSdan ){
1841d5f0767cSdan   char aBuf[24];
1842d5f0767cSdan   sqlite3_snprintf(sizeof(aBuf)-1, aBuf, "%d", iVal);
1843d5f0767cSdan   sessionAppendStr(p, aBuf, pRc);
1844d5f0767cSdan }
1845d5f0767cSdan 
1846296c7658Sdan /*
1847296c7658Sdan ** This function is a no-op if *pRc is other than SQLITE_OK when it is
1848296c7658Sdan ** called. Otherwise, append the string zStr enclosed in quotes (") and
1849296c7658Sdan ** with any embedded quote characters escaped to the buffer. No
1850296c7658Sdan ** nul-terminator byte is written.
1851296c7658Sdan **
1852296c7658Sdan ** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
1853296c7658Sdan ** returning.
1854296c7658Sdan */
1855d5f0767cSdan static void sessionAppendIdent(
1856296c7658Sdan   SessionBuffer *p,               /* Buffer to a append to */
1857296c7658Sdan   const char *zStr,               /* String to quote, escape and append */
1858296c7658Sdan   int *pRc                        /* IN/OUT: Error code */
1859d5f0767cSdan ){
1860cfdbde21Sdrh   int nStr = sqlite3Strlen30(zStr)*2 + 2 + 1;
186180fe2d93Sdan   if( 0==sessionBufferGrow(p, nStr, pRc) ){
1862d5f0767cSdan     char *zOut = (char *)&p->aBuf[p->nBuf];
1863d5f0767cSdan     const char *zIn = zStr;
1864d5f0767cSdan     *zOut++ = '"';
1865d5f0767cSdan     while( *zIn ){
1866d5f0767cSdan       if( *zIn=='"' ) *zOut++ = '"';
1867d5f0767cSdan       *zOut++ = *(zIn++);
1868d5f0767cSdan     }
1869d5f0767cSdan     *zOut++ = '"';
1870cfdbde21Sdrh     p->nBuf = (int)((u8 *)zOut - p->aBuf);
1871d5f0767cSdan   }
1872d5f0767cSdan }
1873d5f0767cSdan 
1874296c7658Sdan /*
1875296c7658Sdan ** This function is a no-op if *pRc is other than SQLITE_OK when it is
1876296c7658Sdan ** called. Otherwse, it appends the serialized version of the value stored
1877296c7658Sdan ** in column iCol of the row that SQL statement pStmt currently points
1878296c7658Sdan ** to to the buffer.
1879296c7658Sdan */
18804fccf43aSdan static void sessionAppendCol(
1881296c7658Sdan   SessionBuffer *p,               /* Buffer to append to */
1882296c7658Sdan   sqlite3_stmt *pStmt,            /* Handle pointing to row containing value */
1883296c7658Sdan   int iCol,                       /* Column to read value from */
1884296c7658Sdan   int *pRc                        /* IN/OUT: Error code */
18854fccf43aSdan ){
18864fccf43aSdan   if( *pRc==SQLITE_OK ){
18874fccf43aSdan     int eType = sqlite3_column_type(pStmt, iCol);
18884fccf43aSdan     sessionAppendByte(p, (u8)eType, pRc);
18894fccf43aSdan     if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
18904fccf43aSdan       sqlite3_int64 i;
18914fccf43aSdan       u8 aBuf[8];
18924fccf43aSdan       if( eType==SQLITE_INTEGER ){
18934fccf43aSdan         i = sqlite3_column_int64(pStmt, iCol);
18944fccf43aSdan       }else{
18954fccf43aSdan         double r = sqlite3_column_double(pStmt, iCol);
18964fccf43aSdan         memcpy(&i, &r, 8);
18974fccf43aSdan       }
1898296c7658Sdan       sessionPutI64(aBuf, i);
18994fccf43aSdan       sessionAppendBlob(p, aBuf, 8, pRc);
19004fccf43aSdan     }
19014fccf43aSdan     if( eType==SQLITE_BLOB || eType==SQLITE_TEXT ){
19026734007dSdan       u8 *z;
19033cc89d95Sdan       int nByte;
19046734007dSdan       if( eType==SQLITE_BLOB ){
19056734007dSdan         z = (u8 *)sqlite3_column_blob(pStmt, iCol);
19066734007dSdan       }else{
19076734007dSdan         z = (u8 *)sqlite3_column_text(pStmt, iCol);
19086734007dSdan       }
19093cc89d95Sdan       nByte = sqlite3_column_bytes(pStmt, iCol);
19103cc89d95Sdan       if( z || (eType==SQLITE_BLOB && nByte==0) ){
19114fccf43aSdan         sessionAppendVarint(p, nByte, pRc);
19126734007dSdan         sessionAppendBlob(p, z, nByte, pRc);
19136734007dSdan       }else{
19146734007dSdan         *pRc = SQLITE_NOMEM;
19156734007dSdan       }
19164fccf43aSdan     }
19174fccf43aSdan   }
19184fccf43aSdan }
19194fccf43aSdan 
1920296c7658Sdan /*
1921296c7658Sdan **
192280fe2d93Sdan ** This function appends an update change to the buffer (see the comments
192380fe2d93Sdan ** under "CHANGESET FORMAT" at the top of the file). An update change
192480fe2d93Sdan ** consists of:
1925296c7658Sdan **
1926296c7658Sdan **   1 byte:  SQLITE_UPDATE (0x17)
1927296c7658Sdan **   n bytes: old.* record (see RECORD FORMAT)
1928296c7658Sdan **   m bytes: new.* record (see RECORD FORMAT)
1929296c7658Sdan **
1930296c7658Sdan ** The SessionChange object passed as the third argument contains the
1931296c7658Sdan ** values that were stored in the row when the session began (the old.*
1932296c7658Sdan ** values). The statement handle passed as the second argument points
1933296c7658Sdan ** at the current version of the row (the new.* values).
1934296c7658Sdan **
1935296c7658Sdan ** If all of the old.* values are equal to their corresponding new.* value
1936296c7658Sdan ** (i.e. nothing has changed), then no data at all is appended to the buffer.
1937296c7658Sdan **
1938296c7658Sdan ** Otherwise, the old.* record contains all primary key values and the
1939296c7658Sdan ** original values of any fields that have been modified. The new.* record
1940296c7658Sdan ** contains the new values of only those fields that have been modified.
1941296c7658Sdan */
194280fe2d93Sdan static int sessionAppendUpdate(
1943296c7658Sdan   SessionBuffer *pBuf,            /* Buffer to append to */
194473b3c055Sdan   int bPatchset,                  /* True for "patchset", 0 for "changeset" */
1945296c7658Sdan   sqlite3_stmt *pStmt,            /* Statement handle pointing at new row */
1946296c7658Sdan   SessionChange *p,               /* Object containing old values */
194780fe2d93Sdan   u8 *abPK                        /* Boolean array - true for PK columns */
19484fccf43aSdan ){
194980fe2d93Sdan   int rc = SQLITE_OK;
1950296c7658Sdan   SessionBuffer buf2 = {0,0,0}; /* Buffer to accumulate new.* record in */
1951296c7658Sdan   int bNoop = 1;                /* Set to zero if any values are modified */
19521f34f8ccSdan   int nRewind = pBuf->nBuf;     /* Set to zero if any values are modified */
1953296c7658Sdan   int i;                        /* Used to iterate through columns */
1954296c7658Sdan   u8 *pCsr = p->aRecord;        /* Used to iterate through old.* values */
1955296c7658Sdan 
195680fe2d93Sdan   sessionAppendByte(pBuf, SQLITE_UPDATE, &rc);
195780fe2d93Sdan   sessionAppendByte(pBuf, p->bIndirect, &rc);
19584fccf43aSdan   for(i=0; i<sqlite3_column_count(pStmt); i++){
195937f133ecSdan     int bChanged = 0;
19604fccf43aSdan     int nAdvance;
19614fccf43aSdan     int eType = *pCsr;
19624fccf43aSdan     switch( eType ){
19634fccf43aSdan       case SQLITE_NULL:
19644fccf43aSdan         nAdvance = 1;
19654fccf43aSdan         if( sqlite3_column_type(pStmt, i)!=SQLITE_NULL ){
196637f133ecSdan           bChanged = 1;
19674fccf43aSdan         }
19684fccf43aSdan         break;
19694fccf43aSdan 
19704fccf43aSdan       case SQLITE_FLOAT:
19714fccf43aSdan       case SQLITE_INTEGER: {
19724fccf43aSdan         nAdvance = 9;
19734fccf43aSdan         if( eType==sqlite3_column_type(pStmt, i) ){
19744fccf43aSdan           sqlite3_int64 iVal = sessionGetI64(&pCsr[1]);
19754fccf43aSdan           if( eType==SQLITE_INTEGER ){
19764fccf43aSdan             if( iVal==sqlite3_column_int64(pStmt, i) ) break;
19774fccf43aSdan           }else{
19784fccf43aSdan             double dVal;
19794fccf43aSdan             memcpy(&dVal, &iVal, 8);
19804fccf43aSdan             if( dVal==sqlite3_column_double(pStmt, i) ) break;
19814fccf43aSdan           }
19824fccf43aSdan         }
198337f133ecSdan         bChanged = 1;
19844fccf43aSdan         break;
19854fccf43aSdan       }
19864fccf43aSdan 
1987e5754eecSdan       default: {
1988895decf6Sdan         int n;
1989895decf6Sdan         int nHdr = 1 + sessionVarintGet(&pCsr[1], &n);
1990e5754eecSdan         assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
1991895decf6Sdan         nAdvance = nHdr + n;
19924fccf43aSdan         if( eType==sqlite3_column_type(pStmt, i)
1993895decf6Sdan          && n==sqlite3_column_bytes(pStmt, i)
1994895decf6Sdan          && (n==0 || 0==memcmp(&pCsr[nHdr], sqlite3_column_blob(pStmt, i), n))
19954fccf43aSdan         ){
19964fccf43aSdan           break;
19974fccf43aSdan         }
199837f133ecSdan         bChanged = 1;
19994fccf43aSdan       }
20004fccf43aSdan     }
20014fccf43aSdan 
200273b3c055Sdan     /* If at least one field has been modified, this is not a no-op. */
200373b3c055Sdan     if( bChanged ) bNoop = 0;
200473b3c055Sdan 
200573b3c055Sdan     /* Add a field to the old.* record. This is omitted if this modules is
200673b3c055Sdan     ** currently generating a patchset. */
200773b3c055Sdan     if( bPatchset==0 ){
200837f133ecSdan       if( bChanged || abPK[i] ){
200980fe2d93Sdan         sessionAppendBlob(pBuf, pCsr, nAdvance, &rc);
20104fccf43aSdan       }else{
201180fe2d93Sdan         sessionAppendByte(pBuf, 0, &rc);
201237f133ecSdan       }
201373b3c055Sdan     }
201437f133ecSdan 
201573b3c055Sdan     /* Add a field to the new.* record. Or the only record if currently
201673b3c055Sdan     ** generating a patchset.  */
201773b3c055Sdan     if( bChanged || (bPatchset && abPK[i]) ){
201880fe2d93Sdan       sessionAppendCol(&buf2, pStmt, i, &rc);
201937f133ecSdan     }else{
202080fe2d93Sdan       sessionAppendByte(&buf2, 0, &rc);
20214fccf43aSdan     }
202237f133ecSdan 
20234fccf43aSdan     pCsr += nAdvance;
20244fccf43aSdan   }
20254fccf43aSdan 
20264fccf43aSdan   if( bNoop ){
20271f34f8ccSdan     pBuf->nBuf = nRewind;
20284fccf43aSdan   }else{
202980fe2d93Sdan     sessionAppendBlob(pBuf, buf2.aBuf, buf2.nBuf, &rc);
20304fccf43aSdan   }
20311f34f8ccSdan   sqlite3_free(buf2.aBuf);
203280fe2d93Sdan 
203380fe2d93Sdan   return rc;
2034d5f0767cSdan }
20354fccf43aSdan 
2036a71d2371Sdan /*
2037a71d2371Sdan ** Append a DELETE change to the buffer passed as the first argument. Use
2038a71d2371Sdan ** the changeset format if argument bPatchset is zero, or the patchset
2039a71d2371Sdan ** format otherwise.
2040a71d2371Sdan */
204173b3c055Sdan static int sessionAppendDelete(
204273b3c055Sdan   SessionBuffer *pBuf,            /* Buffer to append to */
204373b3c055Sdan   int bPatchset,                  /* True for "patchset", 0 for "changeset" */
204473b3c055Sdan   SessionChange *p,               /* Object containing old values */
2045a71d2371Sdan   int nCol,                       /* Number of columns in table */
204673b3c055Sdan   u8 *abPK                        /* Boolean array - true for PK columns */
204773b3c055Sdan ){
204873b3c055Sdan   int rc = SQLITE_OK;
204973b3c055Sdan 
205073b3c055Sdan   sessionAppendByte(pBuf, SQLITE_DELETE, &rc);
205173b3c055Sdan   sessionAppendByte(pBuf, p->bIndirect, &rc);
205273b3c055Sdan 
205373b3c055Sdan   if( bPatchset==0 ){
205473b3c055Sdan     sessionAppendBlob(pBuf, p->aRecord, p->nRecord, &rc);
205573b3c055Sdan   }else{
205673b3c055Sdan     int i;
205773b3c055Sdan     u8 *a = p->aRecord;
205873b3c055Sdan     for(i=0; i<nCol; i++){
205973b3c055Sdan       u8 *pStart = a;
206073b3c055Sdan       int eType = *a++;
206173b3c055Sdan 
206273b3c055Sdan       switch( eType ){
206373b3c055Sdan         case 0:
206473b3c055Sdan         case SQLITE_NULL:
206573b3c055Sdan           assert( abPK[i]==0 );
206673b3c055Sdan           break;
206773b3c055Sdan 
206873b3c055Sdan         case SQLITE_FLOAT:
206973b3c055Sdan         case SQLITE_INTEGER:
207073b3c055Sdan           a += 8;
207173b3c055Sdan           break;
207273b3c055Sdan 
207373b3c055Sdan         default: {
207473b3c055Sdan           int n;
207573b3c055Sdan           a += sessionVarintGet(a, &n);
207673b3c055Sdan           a += n;
207773b3c055Sdan           break;
207873b3c055Sdan         }
207973b3c055Sdan       }
208073b3c055Sdan       if( abPK[i] ){
2081f5ab08c7Sdrh         sessionAppendBlob(pBuf, pStart, (int)(a-pStart), &rc);
208273b3c055Sdan       }
208373b3c055Sdan     }
208473b3c055Sdan     assert( (a - p->aRecord)==p->nRecord );
208573b3c055Sdan   }
208673b3c055Sdan 
208773b3c055Sdan   return rc;
208873b3c055Sdan }
208973b3c055Sdan 
209077fc1d5bSdan /*
209177fc1d5bSdan ** Formulate and prepare a SELECT statement to retrieve a row from table
209277fc1d5bSdan ** zTab in database zDb based on its primary key. i.e.
209377fc1d5bSdan **
209477fc1d5bSdan **   SELECT * FROM zDb.zTab WHERE pk1 = ? AND pk2 = ? AND ...
209577fc1d5bSdan */
2096e8d5648eSdan static int sessionSelectStmt(
2097e8d5648eSdan   sqlite3 *db,                    /* Database handle */
2098d7fb7d24Sdan   const char *zDb,                /* Database name */
2099e8d5648eSdan   const char *zTab,               /* Table name */
210077fc1d5bSdan   int nCol,                       /* Number of columns in table */
210177fc1d5bSdan   const char **azCol,             /* Names of table columns */
210277fc1d5bSdan   u8 *abPK,                       /* PRIMARY KEY  array */
210377fc1d5bSdan   sqlite3_stmt **ppStmt           /* OUT: Prepared SELECT statement */
2104d5f0767cSdan ){
2105e8d5648eSdan   int rc = SQLITE_OK;
2106d5f0767cSdan   int i;
2107e8d5648eSdan   const char *zSep = "";
2108e8d5648eSdan   SessionBuffer buf = {0, 0, 0};
2109d5f0767cSdan 
2110e8d5648eSdan   sessionAppendStr(&buf, "SELECT * FROM ", &rc);
2111d7fb7d24Sdan   sessionAppendIdent(&buf, zDb, &rc);
2112d7fb7d24Sdan   sessionAppendStr(&buf, ".", &rc);
2113e8d5648eSdan   sessionAppendIdent(&buf, zTab, &rc);
2114e8d5648eSdan   sessionAppendStr(&buf, " WHERE ", &rc);
2115e8d5648eSdan   for(i=0; i<nCol; i++){
2116e8d5648eSdan     if( abPK[i] ){
2117e8d5648eSdan       sessionAppendStr(&buf, zSep, &rc);
2118e8d5648eSdan       sessionAppendIdent(&buf, azCol[i], &rc);
2119e8d5648eSdan       sessionAppendStr(&buf, " = ?", &rc);
2120e8d5648eSdan       sessionAppendInteger(&buf, i+1, &rc);
2121e8d5648eSdan       zSep = " AND ";
2122d5f0767cSdan     }
2123d5f0767cSdan   }
2124d5f0767cSdan   if( rc==SQLITE_OK ){
2125e8d5648eSdan     rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, ppStmt, 0);
2126d5f0767cSdan   }
2127e8d5648eSdan   sqlite3_free(buf.aBuf);
2128e8d5648eSdan   return rc;
2129d5f0767cSdan }
2130d5f0767cSdan 
213177fc1d5bSdan /*
213277fc1d5bSdan ** Bind the PRIMARY KEY values from the change passed in argument pChange
213377fc1d5bSdan ** to the SELECT statement passed as the first argument. The SELECT statement
213477fc1d5bSdan ** is as prepared by function sessionSelectStmt().
213577fc1d5bSdan **
213677fc1d5bSdan ** Return SQLITE_OK if all PK values are successfully bound, or an SQLite
213777fc1d5bSdan ** error code (e.g. SQLITE_NOMEM) otherwise.
213877fc1d5bSdan */
2139e8d5648eSdan static int sessionSelectBind(
214077fc1d5bSdan   sqlite3_stmt *pSelect,          /* SELECT from sessionSelectStmt() */
214177fc1d5bSdan   int nCol,                       /* Number of columns in table */
214277fc1d5bSdan   u8 *abPK,                       /* PRIMARY KEY array */
214377fc1d5bSdan   SessionChange *pChange          /* Change structure */
2144e8d5648eSdan ){
2145e8d5648eSdan   int i;
2146e8d5648eSdan   int rc = SQLITE_OK;
2147e5754eecSdan   u8 *a = pChange->aRecord;
2148d5f0767cSdan 
2149e8d5648eSdan   for(i=0; i<nCol && rc==SQLITE_OK; i++){
2150e8d5648eSdan     int eType = *a++;
2151e8d5648eSdan 
2152e8d5648eSdan     switch( eType ){
215380fe2d93Sdan       case 0:
2154e8d5648eSdan       case SQLITE_NULL:
2155e5754eecSdan         assert( abPK[i]==0 );
2156e8d5648eSdan         break;
2157e8d5648eSdan 
2158e8d5648eSdan       case SQLITE_INTEGER: {
2159e8d5648eSdan         if( abPK[i] ){
2160e8d5648eSdan           i64 iVal = sessionGetI64(a);
2161e8d5648eSdan           rc = sqlite3_bind_int64(pSelect, i+1, iVal);
2162e8d5648eSdan         }
2163e8d5648eSdan         a += 8;
2164e8d5648eSdan         break;
2165d5f0767cSdan       }
2166296c7658Sdan 
2167e8d5648eSdan       case SQLITE_FLOAT: {
2168e8d5648eSdan         if( abPK[i] ){
2169e8d5648eSdan           double rVal;
2170e8d5648eSdan           i64 iVal = sessionGetI64(a);
2171e8d5648eSdan           memcpy(&rVal, &iVal, 8);
21724e895da1Sdan           rc = sqlite3_bind_double(pSelect, i+1, rVal);
2173d5f0767cSdan         }
2174e8d5648eSdan         a += 8;
2175e8d5648eSdan         break;
2176e8d5648eSdan       }
2177e8d5648eSdan 
2178e8d5648eSdan       case SQLITE_TEXT: {
2179e8d5648eSdan         int n;
2180e8d5648eSdan         a += sessionVarintGet(a, &n);
2181e8d5648eSdan         if( abPK[i] ){
2182e8d5648eSdan           rc = sqlite3_bind_text(pSelect, i+1, (char *)a, n, SQLITE_TRANSIENT);
2183e8d5648eSdan         }
2184e8d5648eSdan         a += n;
2185e8d5648eSdan         break;
2186e8d5648eSdan       }
2187e8d5648eSdan 
2188e5754eecSdan       default: {
2189e8d5648eSdan         int n;
2190e5754eecSdan         assert( eType==SQLITE_BLOB );
2191e8d5648eSdan         a += sessionVarintGet(a, &n);
2192e8d5648eSdan         if( abPK[i] ){
2193e8d5648eSdan           rc = sqlite3_bind_blob(pSelect, i+1, a, n, SQLITE_TRANSIENT);
2194e8d5648eSdan         }
2195e8d5648eSdan         a += n;
2196e8d5648eSdan         break;
2197e8d5648eSdan       }
2198e8d5648eSdan     }
2199e8d5648eSdan   }
2200e8d5648eSdan 
2201d5f0767cSdan   return rc;
22024fccf43aSdan }
22034fccf43aSdan 
220477fc1d5bSdan /*
220577fc1d5bSdan ** This function is a no-op if *pRc is set to other than SQLITE_OK when it
220677fc1d5bSdan ** is called. Otherwise, append a serialized table header (part of the binary
220777fc1d5bSdan ** changeset format) to buffer *pBuf. If an error occurs, set *pRc to an
220877fc1d5bSdan ** SQLite error code before returning.
220977fc1d5bSdan */
22105d607a6eSdan static void sessionAppendTableHdr(
2211a71d2371Sdan   SessionBuffer *pBuf,            /* Append header to this buffer */
2212a71d2371Sdan   int bPatchset,                  /* Use the patchset format if true */
2213a71d2371Sdan   SessionTable *pTab,             /* Table object to append header for */
2214a71d2371Sdan   int *pRc                        /* IN/OUT: Error code */
22155d607a6eSdan ){
22165d607a6eSdan   /* Write a table header */
221773b3c055Sdan   sessionAppendByte(pBuf, (bPatchset ? 'P' : 'T'), pRc);
22185d607a6eSdan   sessionAppendVarint(pBuf, pTab->nCol, pRc);
22195d607a6eSdan   sessionAppendBlob(pBuf, pTab->abPK, pTab->nCol, pRc);
22204f528042Sdan   sessionAppendBlob(pBuf, (u8 *)pTab->zName, (int)strlen(pTab->zName)+1, pRc);
22215d607a6eSdan }
22225d607a6eSdan 
2223a71d2371Sdan /*
2224a71d2371Sdan ** Generate either a changeset (if argument bPatchset is zero) or a patchset
2225a71d2371Sdan ** (if it is non-zero) based on the current contents of the session object
2226a71d2371Sdan ** passed as the first argument.
2227a71d2371Sdan **
2228a71d2371Sdan ** If no error occurs, SQLITE_OK is returned and the new changeset/patchset
2229a71d2371Sdan ** stored in output variables *pnChangeset and *ppChangeset. Or, if an error
2230a71d2371Sdan ** occurs, an SQLite error code is returned and both output variables set
2231a71d2371Sdan ** to 0.
2232a71d2371Sdan */
2233adf3bf58Sdrh static int sessionGenerateChangeset(
22344fccf43aSdan   sqlite3_session *pSession,      /* Session object */
223573b3c055Sdan   int bPatchset,                  /* True for patchset, false for changeset */
2236ef7a6304Sdan   int (*xOutput)(void *pOut, const void *pData, int nData),
2237ef7a6304Sdan   void *pOut,                     /* First argument for xOutput */
22384fccf43aSdan   int *pnChangeset,               /* OUT: Size of buffer at *ppChangeset */
22394fccf43aSdan   void **ppChangeset              /* OUT: Buffer containing changeset */
22404fccf43aSdan ){
2241296c7658Sdan   sqlite3 *db = pSession->db;     /* Source database handle */
2242296c7658Sdan   SessionTable *pTab;             /* Used to iterate through attached tables */
2243296c7658Sdan   SessionBuffer buf = {0,0,0};    /* Buffer in which to accumlate changeset */
2244296c7658Sdan   int rc;                         /* Return code */
22454fccf43aSdan 
2246ef7a6304Sdan   assert( xOutput==0 || (pnChangeset==0 && ppChangeset==0 ) );
2247ef7a6304Sdan 
2248296c7658Sdan   /* Zero the output variables in case an error occurs. If this session
2249296c7658Sdan   ** object is already in the error state (sqlite3_session.rc != SQLITE_OK),
2250296c7658Sdan   ** this call will be a no-op.  */
2251ef7a6304Sdan   if( xOutput==0 ){
22524fccf43aSdan     *pnChangeset = 0;
22534fccf43aSdan     *ppChangeset = 0;
2254ef7a6304Sdan   }
2255e5754eecSdan 
2256e5754eecSdan   if( pSession->rc ) return pSession->rc;
2257e5754eecSdan   rc = sqlite3_exec(pSession->db, "SAVEPOINT changeset", 0, 0, 0);
2258e5754eecSdan   if( rc!=SQLITE_OK ) return rc;
2259e5754eecSdan 
2260e5754eecSdan   sqlite3_mutex_enter(sqlite3_db_mutex(db));
22614fccf43aSdan 
22624fccf43aSdan   for(pTab=pSession->pTable; rc==SQLITE_OK && pTab; pTab=pTab->pNext){
22634fccf43aSdan     if( pTab->nEntry ){
2264d7fb7d24Sdan       const char *zName = pTab->zName;
2265a9605b91Sdan       int nCol;                   /* Number of columns in table */
2266a9605b91Sdan       u8 *abPK;                   /* Primary key array */
2267a9605b91Sdan       const char **azCol = 0;     /* Table columns */
22681f34f8ccSdan       int i;                      /* Used to iterate through hash buckets */
22691f34f8ccSdan       sqlite3_stmt *pSel = 0;     /* SELECT statement to query table pTab */
22701f34f8ccSdan       int nRewind = buf.nBuf;     /* Initial size of write buffer */
22711f34f8ccSdan       int nNoop;                  /* Size of buffer after writing tbl header */
22724fccf43aSdan 
2273a9605b91Sdan       /* Check the table schema is still Ok. */
2274a9605b91Sdan       rc = sessionTableInfo(db, pSession->zDb, zName, &nCol, 0, &azCol, &abPK);
2275a9605b91Sdan       if( !rc && (pTab->nCol!=nCol || memcmp(abPK, pTab->abPK, nCol)) ){
2276a9605b91Sdan         rc = SQLITE_SCHEMA;
2277a9605b91Sdan       }
2278a9605b91Sdan 
22794fccf43aSdan       /* Write a table header */
228073b3c055Sdan       sessionAppendTableHdr(&buf, bPatchset, pTab, &rc);
22814fccf43aSdan 
22824fccf43aSdan       /* Build and compile a statement to execute: */
22834fccf43aSdan       if( rc==SQLITE_OK ){
2284d7fb7d24Sdan         rc = sessionSelectStmt(
2285a9605b91Sdan             db, pSession->zDb, zName, nCol, azCol, abPK, &pSel);
22864fccf43aSdan       }
22874fccf43aSdan 
22881f34f8ccSdan       nNoop = buf.nBuf;
228912ca0b56Sdan       for(i=0; i<pTab->nChange && rc==SQLITE_OK; i++){
2290e8d5648eSdan         SessionChange *p;         /* Used to iterate through changes */
2291e8d5648eSdan 
22924fccf43aSdan         for(p=pTab->apChange[i]; rc==SQLITE_OK && p; p=p->pNext){
2293e5754eecSdan           rc = sessionSelectBind(pSel, nCol, abPK, p);
229480fe2d93Sdan           if( rc!=SQLITE_OK ) continue;
22951f34f8ccSdan           if( sqlite3_step(pSel)==SQLITE_ROW ){
2296798693b2Sdan             if( p->op==SQLITE_INSERT ){
22974fccf43aSdan               int iCol;
22984fccf43aSdan               sessionAppendByte(&buf, SQLITE_INSERT, &rc);
2299b4480e94Sdan               sessionAppendByte(&buf, p->bIndirect, &rc);
2300e8d5648eSdan               for(iCol=0; iCol<nCol; iCol++){
23011f34f8ccSdan                 sessionAppendCol(&buf, pSel, iCol, &rc);
23024fccf43aSdan               }
2303e8d5648eSdan             }else{
230473b3c055Sdan               rc = sessionAppendUpdate(&buf, bPatchset, pSel, p, abPK);
23054fccf43aSdan             }
2306798693b2Sdan           }else if( p->op!=SQLITE_INSERT ){
2307a71d2371Sdan             rc = sessionAppendDelete(&buf, bPatchset, p, nCol, abPK);
23084fccf43aSdan           }
230912ca0b56Sdan           if( rc==SQLITE_OK ){
23101f34f8ccSdan             rc = sqlite3_reset(pSel);
23114fccf43aSdan           }
2312ef7a6304Sdan 
2313f1a08ad8Sdrh           /* If the buffer is now larger than SESSIONS_STRM_CHUNK_SIZE, pass
2314ef7a6304Sdan           ** its contents to the xOutput() callback. */
2315ef7a6304Sdan           if( xOutput
2316ef7a6304Sdan            && rc==SQLITE_OK
2317ef7a6304Sdan            && buf.nBuf>nNoop
2318f1a08ad8Sdrh            && buf.nBuf>SESSIONS_STRM_CHUNK_SIZE
2319ef7a6304Sdan           ){
2320ef7a6304Sdan             rc = xOutput(pOut, (void*)buf.aBuf, buf.nBuf);
2321ef7a6304Sdan             nNoop = -1;
2322ef7a6304Sdan             buf.nBuf = 0;
2323ef7a6304Sdan           }
2324ef7a6304Sdan 
23254fccf43aSdan         }
2326e8d5648eSdan       }
23274fccf43aSdan 
23281f34f8ccSdan       sqlite3_finalize(pSel);
23291f34f8ccSdan       if( buf.nBuf==nNoop ){
23304fccf43aSdan         buf.nBuf = nRewind;
23314fccf43aSdan       }
2332cfdbde21Sdrh       sqlite3_free((char*)azCol);  /* cast works around VC++ bug */
23334fccf43aSdan     }
23344fccf43aSdan   }
23354fccf43aSdan 
23364fccf43aSdan   if( rc==SQLITE_OK ){
2337ef7a6304Sdan     if( xOutput==0 ){
23384fccf43aSdan       *pnChangeset = buf.nBuf;
23394fccf43aSdan       *ppChangeset = buf.aBuf;
2340ef7a6304Sdan       buf.aBuf = 0;
2341ef7a6304Sdan     }else if( buf.nBuf>0 ){
2342ef7a6304Sdan       rc = xOutput(pOut, (void*)buf.aBuf, buf.nBuf);
2343ef7a6304Sdan     }
23444fccf43aSdan   }
23454c220252Sdan 
2346ef7a6304Sdan   sqlite3_free(buf.aBuf);
2347e5754eecSdan   sqlite3_exec(db, "RELEASE changeset", 0, 0, 0);
23484c220252Sdan   sqlite3_mutex_leave(sqlite3_db_mutex(db));
23494fccf43aSdan   return rc;
23504fccf43aSdan }
23514fccf43aSdan 
2352296c7658Sdan /*
235373b3c055Sdan ** Obtain a changeset object containing all changes recorded by the
235473b3c055Sdan ** session object passed as the first argument.
235573b3c055Sdan **
235673b3c055Sdan ** It is the responsibility of the caller to eventually free the buffer
235773b3c055Sdan ** using sqlite3_free().
235873b3c055Sdan */
235973b3c055Sdan int sqlite3session_changeset(
236073b3c055Sdan   sqlite3_session *pSession,      /* Session object */
236173b3c055Sdan   int *pnChangeset,               /* OUT: Size of buffer at *ppChangeset */
236273b3c055Sdan   void **ppChangeset              /* OUT: Buffer containing changeset */
236373b3c055Sdan ){
2364ef7a6304Sdan   return sessionGenerateChangeset(pSession, 0, 0, 0, pnChangeset, ppChangeset);
2365ef7a6304Sdan }
2366ef7a6304Sdan 
2367ef7a6304Sdan /*
2368ef7a6304Sdan ** Streaming version of sqlite3session_changeset().
2369ef7a6304Sdan */
2370f1a08ad8Sdrh int sqlite3session_changeset_strm(
2371ef7a6304Sdan   sqlite3_session *pSession,
2372ef7a6304Sdan   int (*xOutput)(void *pOut, const void *pData, int nData),
2373ef7a6304Sdan   void *pOut
2374ef7a6304Sdan ){
2375ef7a6304Sdan   return sessionGenerateChangeset(pSession, 0, xOutput, pOut, 0, 0);
2376ef7a6304Sdan }
2377ef7a6304Sdan 
2378ef7a6304Sdan /*
2379ef7a6304Sdan ** Streaming version of sqlite3session_patchset().
2380ef7a6304Sdan */
2381f1a08ad8Sdrh int sqlite3session_patchset_strm(
2382ef7a6304Sdan   sqlite3_session *pSession,
2383ef7a6304Sdan   int (*xOutput)(void *pOut, const void *pData, int nData),
2384ef7a6304Sdan   void *pOut
2385ef7a6304Sdan ){
2386ef7a6304Sdan   return sessionGenerateChangeset(pSession, 1, xOutput, pOut, 0, 0);
238773b3c055Sdan }
238873b3c055Sdan 
238973b3c055Sdan /*
239073b3c055Sdan ** Obtain a patchset object containing all changes recorded by the
239173b3c055Sdan ** session object passed as the first argument.
239273b3c055Sdan **
239373b3c055Sdan ** It is the responsibility of the caller to eventually free the buffer
239473b3c055Sdan ** using sqlite3_free().
239573b3c055Sdan */
239673b3c055Sdan int sqlite3session_patchset(
239773b3c055Sdan   sqlite3_session *pSession,      /* Session object */
239873b3c055Sdan   int *pnPatchset,                /* OUT: Size of buffer at *ppChangeset */
239973b3c055Sdan   void **ppPatchset               /* OUT: Buffer containing changeset */
240073b3c055Sdan ){
2401ef7a6304Sdan   return sessionGenerateChangeset(pSession, 1, 0, 0, pnPatchset, ppPatchset);
240273b3c055Sdan }
240373b3c055Sdan 
240473b3c055Sdan /*
2405296c7658Sdan ** Enable or disable the session object passed as the first argument.
2406296c7658Sdan */
24074fccf43aSdan int sqlite3session_enable(sqlite3_session *pSession, int bEnable){
24084c220252Sdan   int ret;
24094c220252Sdan   sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
2410296c7658Sdan   if( bEnable>=0 ){
2411296c7658Sdan     pSession->bEnable = bEnable;
24124fccf43aSdan   }
24134c220252Sdan   ret = pSession->bEnable;
24144c220252Sdan   sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
24154c220252Sdan   return ret;
2416296c7658Sdan }
24174fccf43aSdan 
24184fccf43aSdan /*
2419b4480e94Sdan ** Enable or disable the session object passed as the first argument.
2420b4480e94Sdan */
2421b4480e94Sdan int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect){
2422b4480e94Sdan   int ret;
2423b4480e94Sdan   sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
2424b4480e94Sdan   if( bIndirect>=0 ){
2425b4480e94Sdan     pSession->bIndirect = bIndirect;
2426b4480e94Sdan   }
2427b4480e94Sdan   ret = pSession->bIndirect;
2428b4480e94Sdan   sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
2429b4480e94Sdan   return ret;
2430b4480e94Sdan }
2431b4480e94Sdan 
2432b4480e94Sdan /*
2433b69ec348Sdan ** Return true if there have been no changes to monitored tables recorded
2434b69ec348Sdan ** by the session object passed as the only argument.
2435b69ec348Sdan */
2436b69ec348Sdan int sqlite3session_isempty(sqlite3_session *pSession){
2437b69ec348Sdan   int ret = 0;
2438b69ec348Sdan   SessionTable *pTab;
2439b69ec348Sdan 
2440b69ec348Sdan   sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
2441b69ec348Sdan   for(pTab=pSession->pTable; pTab && ret==0; pTab=pTab->pNext){
2442b69ec348Sdan     ret = (pTab->nEntry>0);
2443b69ec348Sdan   }
2444b69ec348Sdan   sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
2445b69ec348Sdan 
2446ff530326Sdan   return (ret==0);
2447b69ec348Sdan }
2448b69ec348Sdan 
2449b69ec348Sdan /*
2450f1a08ad8Sdrh ** Do the work for either sqlite3changeset_start() or start_strm().
24514fccf43aSdan */
2452adf3bf58Sdrh static int sessionChangesetStart(
2453296c7658Sdan   sqlite3_changeset_iter **pp,    /* OUT: Changeset iterator handle */
2454ef7a6304Sdan   int (*xInput)(void *pIn, void *pData, int *pnData),
2455ef7a6304Sdan   void *pIn,
2456296c7658Sdan   int nChangeset,                 /* Size of buffer pChangeset in bytes */
2457296c7658Sdan   void *pChangeset                /* Pointer to buffer containing changeset */
24584fccf43aSdan ){
24594fccf43aSdan   sqlite3_changeset_iter *pRet;   /* Iterator to return */
24604fccf43aSdan   int nByte;                      /* Number of bytes to allocate for iterator */
24614fccf43aSdan 
2462ef7a6304Sdan   assert( xInput==0 || (pChangeset==0 && nChangeset==0) );
2463ef7a6304Sdan 
2464296c7658Sdan   /* Zero the output variable in case an error occurs. */
2465296c7658Sdan   *pp = 0;
24664fccf43aSdan 
2467296c7658Sdan   /* Allocate and initialize the iterator structure. */
24684fccf43aSdan   nByte = sizeof(sqlite3_changeset_iter);
24694fccf43aSdan   pRet = (sqlite3_changeset_iter *)sqlite3_malloc(nByte);
24704fccf43aSdan   if( !pRet ) return SQLITE_NOMEM;
24714fccf43aSdan   memset(pRet, 0, sizeof(sqlite3_changeset_iter));
24724757c658Sdan   pRet->in.aData = (u8 *)pChangeset;
24734757c658Sdan   pRet->in.nData = nChangeset;
2474ef7a6304Sdan   pRet->in.xInput = xInput;
2475ef7a6304Sdan   pRet->in.pIn = pIn;
2476ef7a6304Sdan   pRet->in.bEof = (xInput ? 0 : 1);
24774fccf43aSdan 
2478296c7658Sdan   /* Populate the output variable and return success. */
2479296c7658Sdan   *pp = pRet;
24804fccf43aSdan   return SQLITE_OK;
24814fccf43aSdan }
24824fccf43aSdan 
2483296c7658Sdan /*
2484ef7a6304Sdan ** Create an iterator used to iterate through the contents of a changeset.
2485ef7a6304Sdan */
2486ef7a6304Sdan int sqlite3changeset_start(
2487ef7a6304Sdan   sqlite3_changeset_iter **pp,    /* OUT: Changeset iterator handle */
2488ef7a6304Sdan   int nChangeset,                 /* Size of buffer pChangeset in bytes */
2489ef7a6304Sdan   void *pChangeset                /* Pointer to buffer containing changeset */
2490ef7a6304Sdan ){
2491ef7a6304Sdan   return sessionChangesetStart(pp, 0, 0, nChangeset, pChangeset);
2492ef7a6304Sdan }
2493ef7a6304Sdan 
2494ef7a6304Sdan /*
2495ef7a6304Sdan ** Streaming version of sqlite3changeset_start().
2496ef7a6304Sdan */
2497f1a08ad8Sdrh int sqlite3changeset_start_strm(
2498ef7a6304Sdan   sqlite3_changeset_iter **pp,    /* OUT: Changeset iterator handle */
2499ef7a6304Sdan   int (*xInput)(void *pIn, void *pData, int *pnData),
2500ef7a6304Sdan   void *pIn
2501ef7a6304Sdan ){
2502ef7a6304Sdan   return sessionChangesetStart(pp, xInput, pIn, 0, 0);
2503ef7a6304Sdan }
2504ef7a6304Sdan 
2505ef7a6304Sdan /*
2506d9151526Sdan ** If the SessionInput object passed as the only argument is a streaming
2507d9151526Sdan ** object and the buffer is full, discard some data to free up space.
2508d9151526Sdan */
2509d9151526Sdan static void sessionDiscardData(SessionInput *pIn){
2510d9151526Sdan   if( pIn->bEof && pIn->xInput && pIn->iNext>=SESSIONS_STRM_CHUNK_SIZE ){
2511d9151526Sdan     int nMove = pIn->buf.nBuf - pIn->iNext;
2512d9151526Sdan     assert( nMove>=0 );
2513d9151526Sdan     if( nMove>0 ){
2514d9151526Sdan       memmove(pIn->buf.aBuf, &pIn->buf.aBuf[pIn->iNext], nMove);
2515d9151526Sdan     }
2516d9151526Sdan     pIn->buf.nBuf -= pIn->iNext;
2517d9151526Sdan     pIn->iNext = 0;
2518d9151526Sdan     pIn->nData = pIn->buf.nBuf;
2519d9151526Sdan   }
2520d9151526Sdan }
2521d9151526Sdan 
2522d9151526Sdan /*
2523ef7a6304Sdan ** Ensure that there are at least nByte bytes available in the buffer. Or,
2524ef7a6304Sdan ** if there are not nByte bytes remaining in the input, that all available
2525ef7a6304Sdan ** data is in the buffer.
2526ef7a6304Sdan **
2527ef7a6304Sdan ** Return an SQLite error code if an error occurs, or SQLITE_OK otherwise.
2528ef7a6304Sdan */
25294757c658Sdan static int sessionInputBuffer(SessionInput *pIn, int nByte){
2530ef7a6304Sdan   int rc = SQLITE_OK;
25314757c658Sdan   if( pIn->xInput ){
25324757c658Sdan     while( !pIn->bEof && (pIn->iNext+nByte)>=pIn->nData && rc==SQLITE_OK ){
2533f1a08ad8Sdrh       int nNew = SESSIONS_STRM_CHUNK_SIZE;
25344757c658Sdan 
2535d9151526Sdan       if( pIn->bNoDiscard==0 ) sessionDiscardData(pIn);
25364757c658Sdan       if( SQLITE_OK==sessionBufferGrow(&pIn->buf, nNew, &rc) ){
25374757c658Sdan         rc = pIn->xInput(pIn->pIn, &pIn->buf.aBuf[pIn->buf.nBuf], &nNew);
25384757c658Sdan         if( nNew==0 ){
25394757c658Sdan           pIn->bEof = 1;
25404757c658Sdan         }else{
25414757c658Sdan           pIn->buf.nBuf += nNew;
25424757c658Sdan         }
25434757c658Sdan       }
25444757c658Sdan 
25454757c658Sdan       pIn->aData = pIn->buf.aBuf;
25464757c658Sdan       pIn->nData = pIn->buf.nBuf;
25474757c658Sdan     }
2548ef7a6304Sdan   }
2549ef7a6304Sdan   return rc;
2550ef7a6304Sdan }
2551ef7a6304Sdan 
2552ef7a6304Sdan /*
2553ef7a6304Sdan ** When this function is called, *ppRec points to the start of a record
2554ef7a6304Sdan ** that contains nCol values. This function advances the pointer *ppRec
2555ef7a6304Sdan ** until it points to the byte immediately following that record.
2556ef7a6304Sdan */
2557ef7a6304Sdan static void sessionSkipRecord(
2558ef7a6304Sdan   u8 **ppRec,                     /* IN/OUT: Record pointer */
2559ef7a6304Sdan   int nCol                        /* Number of values in record */
2560ef7a6304Sdan ){
2561ef7a6304Sdan   u8 *aRec = *ppRec;
2562ef7a6304Sdan   int i;
2563ef7a6304Sdan   for(i=0; i<nCol; i++){
2564ef7a6304Sdan     int eType = *aRec++;
2565ef7a6304Sdan     if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
2566ef7a6304Sdan       int nByte;
2567ef7a6304Sdan       aRec += sessionVarintGet((u8*)aRec, &nByte);
2568ef7a6304Sdan       aRec += nByte;
2569ef7a6304Sdan     }else if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
2570ef7a6304Sdan       aRec += 8;
2571ef7a6304Sdan     }
2572ef7a6304Sdan   }
2573ef7a6304Sdan 
2574ef7a6304Sdan   *ppRec = aRec;
2575ef7a6304Sdan }
2576ef7a6304Sdan 
2577ef7a6304Sdan /*
25784757c658Sdan ** This function sets the value of the sqlite3_value object passed as the
25794757c658Sdan ** first argument to a copy of the string or blob held in the aData[]
25804757c658Sdan ** buffer. SQLITE_OK is returned if successful, or SQLITE_NOMEM if an OOM
25814757c658Sdan ** error occurs.
25824757c658Sdan */
25834757c658Sdan static int sessionValueSetStr(
25844757c658Sdan   sqlite3_value *pVal,            /* Set the value of this object */
25854757c658Sdan   u8 *aData,                      /* Buffer containing string or blob data */
25864757c658Sdan   int nData,                      /* Size of buffer aData[] in bytes */
25874757c658Sdan   u8 enc                          /* String encoding (0 for blobs) */
25884757c658Sdan ){
258916228167Sdan   /* In theory this code could just pass SQLITE_TRANSIENT as the final
259016228167Sdan   ** argument to sqlite3ValueSetStr() and have the copy created
259116228167Sdan   ** automatically. But doing so makes it difficult to detect any OOM
259216228167Sdan   ** error. Hence the code to create the copy externally. */
25933cc89d95Sdan   u8 *aCopy = sqlite3_malloc(nData+1);
25944757c658Sdan   if( aCopy==0 ) return SQLITE_NOMEM;
25954757c658Sdan   memcpy(aCopy, aData, nData);
25964757c658Sdan   sqlite3ValueSetStr(pVal, nData, (char*)aCopy, enc, sqlite3_free);
25974757c658Sdan   return SQLITE_OK;
25984757c658Sdan }
25994757c658Sdan 
26004757c658Sdan /*
2601296c7658Sdan ** Deserialize a single record from a buffer in memory. See "RECORD FORMAT"
2602296c7658Sdan ** for details.
2603296c7658Sdan **
2604296c7658Sdan ** When this function is called, *paChange points to the start of the record
2605296c7658Sdan ** to deserialize. Assuming no error occurs, *paChange is set to point to
2606296c7658Sdan ** one byte after the end of the same record before this function returns.
2607a71d2371Sdan ** If the argument abPK is NULL, then the record contains nCol values. Or,
2608a71d2371Sdan ** if abPK is other than NULL, then the record contains only the PK fields
2609a71d2371Sdan ** (in other words, it is a patchset DELETE record).
2610296c7658Sdan **
2611296c7658Sdan ** If successful, each element of the apOut[] array (allocated by the caller)
2612296c7658Sdan ** is set to point to an sqlite3_value object containing the value read
2613296c7658Sdan ** from the corresponding position in the record. If that value is not
2614296c7658Sdan ** included in the record (i.e. because the record is part of an UPDATE change
2615296c7658Sdan ** and the field was not modified), the corresponding element of apOut[] is
2616296c7658Sdan ** set to NULL.
2617296c7658Sdan **
2618296c7658Sdan ** It is the responsibility of the caller to free all sqlite_value structures
2619296c7658Sdan ** using sqlite3_free().
2620296c7658Sdan **
2621296c7658Sdan ** If an error occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned.
2622296c7658Sdan ** The apOut[] array may have been partially populated in this case.
2623296c7658Sdan */
26244fccf43aSdan static int sessionReadRecord(
2625ef7a6304Sdan   SessionInput *pIn,              /* Input data */
26264fccf43aSdan   int nCol,                       /* Number of values in record */
262773b3c055Sdan   u8 *abPK,                       /* Array of primary key flags, or NULL */
26284fccf43aSdan   sqlite3_value **apOut           /* Write values to this array */
26294fccf43aSdan ){
2630296c7658Sdan   int i;                          /* Used to iterate through columns */
2631ef7a6304Sdan   int rc = SQLITE_OK;
26324fccf43aSdan 
2633ef7a6304Sdan   for(i=0; i<nCol && rc==SQLITE_OK; i++){
2634ef7a6304Sdan     int eType = 0;                /* Type of value (SQLITE_NULL, TEXT etc.) */
263573b3c055Sdan     if( abPK && abPK[i]==0 ) continue;
2636ef7a6304Sdan     rc = sessionInputBuffer(pIn, 9);
2637ef7a6304Sdan     if( rc==SQLITE_OK ){
26384757c658Sdan       eType = pIn->aData[pIn->iNext++];
2639ef7a6304Sdan     }
2640ef7a6304Sdan 
2641e8fa8c96Sdan     assert( apOut[i]==0 );
26424fccf43aSdan     if( eType ){
26434fccf43aSdan       apOut[i] = sqlite3ValueNew(0);
2644ef7a6304Sdan       if( !apOut[i] ) rc = SQLITE_NOMEM;
2645ef7a6304Sdan     }
26464fccf43aSdan 
2647ef7a6304Sdan     if( rc==SQLITE_OK ){
26484757c658Sdan       u8 *aVal = &pIn->aData[pIn->iNext];
26494fccf43aSdan       if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
26504fccf43aSdan         int nByte;
2651ef7a6304Sdan         pIn->iNext += sessionVarintGet(aVal, &nByte);
2652ef7a6304Sdan         rc = sessionInputBuffer(pIn, nByte);
2653e8fa8c96Sdan         if( rc==SQLITE_OK ){
26546734007dSdan           u8 enc = (eType==SQLITE_TEXT ? SQLITE_UTF8 : 0);
26554757c658Sdan           rc = sessionValueSetStr(apOut[i],&pIn->aData[pIn->iNext],nByte,enc);
265691ddd559Sdan         }
2657ef7a6304Sdan         pIn->iNext += nByte;
26584fccf43aSdan       }
26594fccf43aSdan       if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
2660ef7a6304Sdan         sqlite3_int64 v = sessionGetI64(aVal);
26614fccf43aSdan         if( eType==SQLITE_INTEGER ){
26624fccf43aSdan           sqlite3VdbeMemSetInt64(apOut[i], v);
26634fccf43aSdan         }else{
26644fccf43aSdan           double d;
26654e895da1Sdan           memcpy(&d, &v, 8);
26664fccf43aSdan           sqlite3VdbeMemSetDouble(apOut[i], d);
26674fccf43aSdan         }
2668ef7a6304Sdan         pIn->iNext += 8;
266991ddd559Sdan       }
26704fccf43aSdan     }
26714fccf43aSdan   }
26724fccf43aSdan 
2673ef7a6304Sdan   return rc;
2674ef7a6304Sdan }
2675ef7a6304Sdan 
2676ef7a6304Sdan /*
2677ef7a6304Sdan ** The input pointer currently points to the second byte of a table-header.
2678ef7a6304Sdan ** Specifically, to the following:
2679ef7a6304Sdan **
2680ef7a6304Sdan **   + number of columns in table (varint)
2681ef7a6304Sdan **   + array of PK flags (1 byte per column),
2682ef7a6304Sdan **   + table name (nul terminated).
2683ef7a6304Sdan **
2684ef7a6304Sdan ** This function ensures that all of the above is present in the input
2685ef7a6304Sdan ** buffer (i.e. that it can be accessed without any calls to xInput()).
2686ef7a6304Sdan ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
2687ef7a6304Sdan ** The input pointer is not moved.
2688ef7a6304Sdan */
2689ef7a6304Sdan static int sessionChangesetBufferTblhdr(SessionInput *pIn, int *pnByte){
2690ef7a6304Sdan   int rc = SQLITE_OK;
2691ef7a6304Sdan   int nCol = 0;
26924757c658Sdan   int nRead = 0;
2693ef7a6304Sdan 
2694ef7a6304Sdan   rc = sessionInputBuffer(pIn, 9);
2695ef7a6304Sdan   if( rc==SQLITE_OK ){
26964757c658Sdan     nRead += sessionVarintGet(&pIn->aData[pIn->iNext + nRead], &nCol);
26974757c658Sdan     rc = sessionInputBuffer(pIn, nRead+nCol+100);
26984757c658Sdan     nRead += nCol;
2699ef7a6304Sdan   }
27004757c658Sdan 
2701ef7a6304Sdan   while( rc==SQLITE_OK ){
27024757c658Sdan     while( (pIn->iNext + nRead)<pIn->nData && pIn->aData[pIn->iNext + nRead] ){
27034757c658Sdan       nRead++;
2704ef7a6304Sdan     }
2705e8fa8c96Sdan     if( (pIn->iNext + nRead)<pIn->nData ) break;
27064757c658Sdan     rc = sessionInputBuffer(pIn, nRead + 100);
27074757c658Sdan   }
2708e8fa8c96Sdan   *pnByte = nRead+1;
2709ef7a6304Sdan   return rc;
2710ef7a6304Sdan }
2711ef7a6304Sdan 
2712ef7a6304Sdan /*
2713fa122adaSdan ** The input pointer currently points to the first byte of the first field
2714fa122adaSdan ** of a record consisting of nCol columns. This function ensures the entire
271516228167Sdan ** record is buffered. It does not move the input pointer.
271616228167Sdan **
271716228167Sdan ** If successful, SQLITE_OK is returned and *pnByte is set to the size of
271816228167Sdan ** the record in bytes. Otherwise, an SQLite error code is returned. The
271916228167Sdan ** final value of *pnByte is undefined in this case.
2720fa122adaSdan */
2721fa122adaSdan static int sessionChangesetBufferRecord(
272216228167Sdan   SessionInput *pIn,              /* Input data */
272316228167Sdan   int nCol,                       /* Number of columns in record */
272416228167Sdan   int *pnByte                     /* OUT: Size of record in bytes */
2725fa122adaSdan ){
2726fa122adaSdan   int rc = SQLITE_OK;
2727fa122adaSdan   int nByte = 0;
2728fa122adaSdan   int i;
2729fa122adaSdan   for(i=0; rc==SQLITE_OK && i<nCol; i++){
2730fa122adaSdan     int eType;
2731fa122adaSdan     rc = sessionInputBuffer(pIn, nByte + 10);
2732fa122adaSdan     if( rc==SQLITE_OK ){
2733fa122adaSdan       eType = pIn->aData[pIn->iNext + nByte++];
2734fa122adaSdan       if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
2735fa122adaSdan         int n;
2736fa122adaSdan         nByte += sessionVarintGet(&pIn->aData[pIn->iNext+nByte], &n);
2737fa122adaSdan         nByte += n;
2738fa122adaSdan         rc = sessionInputBuffer(pIn, nByte);
2739fa122adaSdan       }else if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
2740fa122adaSdan         nByte += 8;
2741fa122adaSdan       }
2742fa122adaSdan     }
2743fa122adaSdan   }
2744fa122adaSdan   *pnByte = nByte;
2745fa122adaSdan   return rc;
2746fa122adaSdan }
2747fa122adaSdan 
2748fa122adaSdan /*
2749ef7a6304Sdan ** The input pointer currently points to the second byte of a table-header.
2750ef7a6304Sdan ** Specifically, to the following:
2751ef7a6304Sdan **
2752ef7a6304Sdan **   + number of columns in table (varint)
2753ef7a6304Sdan **   + array of PK flags (1 byte per column),
2754ef7a6304Sdan **   + table name (nul terminated).
275516228167Sdan **
275616228167Sdan ** This function decodes the table-header and populates the p->nCol,
275716228167Sdan ** p->zTab and p->abPK[] variables accordingly. The p->apValue[] array is
275816228167Sdan ** also allocated or resized according to the new value of p->nCol. The
275916228167Sdan ** input pointer is left pointing to the byte following the table header.
276016228167Sdan **
276116228167Sdan ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code
276216228167Sdan ** is returned and the final values of the various fields enumerated above
276316228167Sdan ** are undefined.
2764ef7a6304Sdan */
2765ef7a6304Sdan static int sessionChangesetReadTblhdr(sqlite3_changeset_iter *p){
2766ef7a6304Sdan   int rc;
2767ef7a6304Sdan   int nCopy;
2768ef7a6304Sdan   assert( p->rc==SQLITE_OK );
2769ef7a6304Sdan 
2770ef7a6304Sdan   rc = sessionChangesetBufferTblhdr(&p->in, &nCopy);
2771ef7a6304Sdan   if( rc==SQLITE_OK ){
2772ef7a6304Sdan     int nByte;
2773ef7a6304Sdan     int nVarint;
27744757c658Sdan     nVarint = sessionVarintGet(&p->in.aData[p->in.iNext], &p->nCol);
2775ef7a6304Sdan     nCopy -= nVarint;
2776ef7a6304Sdan     p->in.iNext += nVarint;
2777ef7a6304Sdan     nByte = p->nCol * sizeof(sqlite3_value*) * 2 + nCopy;
2778ef7a6304Sdan     p->tblhdr.nBuf = 0;
2779ef7a6304Sdan     sessionBufferGrow(&p->tblhdr, nByte, &rc);
2780ef7a6304Sdan   }
2781ef7a6304Sdan 
2782ef7a6304Sdan   if( rc==SQLITE_OK ){
2783ef7a6304Sdan     int iPK = sizeof(sqlite3_value*)*p->nCol*2;
2784ef7a6304Sdan     memset(p->tblhdr.aBuf, 0, iPK);
27854757c658Sdan     memcpy(&p->tblhdr.aBuf[iPK], &p->in.aData[p->in.iNext], nCopy);
2786ef7a6304Sdan     p->in.iNext += nCopy;
2787ef7a6304Sdan   }
2788ef7a6304Sdan 
2789ef7a6304Sdan   p->apValue = (sqlite3_value**)p->tblhdr.aBuf;
2790ef7a6304Sdan   p->abPK = (u8*)&p->apValue[p->nCol*2];
2791ef7a6304Sdan   p->zTab = (char*)&p->abPK[p->nCol];
2792ef7a6304Sdan   return (p->rc = rc);
27934fccf43aSdan }
27944fccf43aSdan 
279577fc1d5bSdan /*
279677fc1d5bSdan ** Advance the changeset iterator to the next change.
279777fc1d5bSdan **
279877fc1d5bSdan ** If both paRec and pnRec are NULL, then this function works like the public
279977fc1d5bSdan ** API sqlite3changeset_next(). If SQLITE_ROW is returned, then the
280077fc1d5bSdan ** sqlite3changeset_new() and old() APIs may be used to query for values.
280177fc1d5bSdan **
280277fc1d5bSdan ** Otherwise, if paRec and pnRec are not NULL, then a pointer to the change
280377fc1d5bSdan ** record is written to *paRec before returning and the number of bytes in
280477fc1d5bSdan ** the record to *pnRec.
280577fc1d5bSdan **
280677fc1d5bSdan ** Either way, this function returns SQLITE_ROW if the iterator is
280777fc1d5bSdan ** successfully advanced to the next change in the changeset, an SQLite
280877fc1d5bSdan ** error code if an error occurs, or SQLITE_DONE if there are no further
280977fc1d5bSdan ** changes in the changeset.
281077fc1d5bSdan */
28115d607a6eSdan static int sessionChangesetNext(
281277fc1d5bSdan   sqlite3_changeset_iter *p,      /* Changeset iterator */
281377fc1d5bSdan   u8 **paRec,                     /* If non-NULL, store record pointer here */
281477fc1d5bSdan   int *pnRec                      /* If non-NULL, store size of record here */
28155d607a6eSdan ){
28164fccf43aSdan   int i;
2817ef7a6304Sdan   u8 op;
28184fccf43aSdan 
28195d607a6eSdan   assert( (paRec==0 && pnRec==0) || (paRec && pnRec) );
28205d607a6eSdan 
2821296c7658Sdan   /* If the iterator is in the error-state, return immediately. */
28224fccf43aSdan   if( p->rc!=SQLITE_OK ) return p->rc;
28234fccf43aSdan 
28245d607a6eSdan   /* Free the current contents of p->apValue[], if any. */
28254fccf43aSdan   if( p->apValue ){
28264fccf43aSdan     for(i=0; i<p->nCol*2; i++){
28274fccf43aSdan       sqlite3ValueFree(p->apValue[i]);
28284fccf43aSdan     }
28294fccf43aSdan     memset(p->apValue, 0, sizeof(sqlite3_value*)*p->nCol*2);
28304fccf43aSdan   }
28314fccf43aSdan 
2832ef7a6304Sdan   /* Make sure the buffer contains at least 10 bytes of input data, or all
2833ef7a6304Sdan   ** remaining data if there are less than 10 bytes available. This is
2834ef7a6304Sdan   ** sufficient either for the 'T' or 'P' byte and the varint that follows
2835ef7a6304Sdan   ** it, or for the two single byte values otherwise. */
2836ef7a6304Sdan   p->rc = sessionInputBuffer(&p->in, 2);
2837ef7a6304Sdan   if( p->rc!=SQLITE_OK ) return p->rc;
2838ef7a6304Sdan 
28394fccf43aSdan   /* If the iterator is already at the end of the changeset, return DONE. */
28404757c658Sdan   if( p->in.iNext>=p->in.nData ){
28414fccf43aSdan     return SQLITE_DONE;
28424fccf43aSdan   }
28434fccf43aSdan 
2844d9151526Sdan   sessionDiscardData(&p->in);
2845d9151526Sdan   p->in.iCurrent = p->in.iNext;
2846d9151526Sdan 
28474757c658Sdan   op = p->in.aData[p->in.iNext++];
284807d0f15eSdan   while( op=='T' || op=='P' ){
2849ef7a6304Sdan     p->bPatchset = (op=='P');
2850ef7a6304Sdan     if( sessionChangesetReadTblhdr(p) ) return p->rc;
2851ef7a6304Sdan     if( (p->rc = sessionInputBuffer(&p->in, 2)) ) return p->rc;
2852d9151526Sdan     p->in.iCurrent = p->in.iNext;
285307d0f15eSdan     if( p->in.iNext>=p->in.nData ) return SQLITE_DONE;
28544757c658Sdan     op = p->in.aData[p->in.iNext++];
28555d607a6eSdan   }
28565d607a6eSdan 
2857ef7a6304Sdan   p->op = op;
28584757c658Sdan   p->bIndirect = p->in.aData[p->in.iNext++];
28594fccf43aSdan   if( p->op!=SQLITE_UPDATE && p->op!=SQLITE_DELETE && p->op!=SQLITE_INSERT ){
28604757c658Sdan     return (p->rc = SQLITE_CORRUPT_BKPT);
28614fccf43aSdan   }
28624fccf43aSdan 
2863cbf6d2d2Sdan   if( paRec ){
2864cbf6d2d2Sdan     int nVal;                     /* Number of values to buffer */
2865cbf6d2d2Sdan     if( p->bPatchset==0 && op==SQLITE_UPDATE ){
2866cbf6d2d2Sdan       nVal = p->nCol * 2;
2867cbf6d2d2Sdan     }else if( p->bPatchset && op==SQLITE_DELETE ){
2868cbf6d2d2Sdan       nVal = 0;
2869cbf6d2d2Sdan       for(i=0; i<p->nCol; i++) if( p->abPK[i] ) nVal++;
2870cbf6d2d2Sdan     }else{
2871cbf6d2d2Sdan       nVal = p->nCol;
2872cbf6d2d2Sdan     }
2873cbf6d2d2Sdan     p->rc = sessionChangesetBufferRecord(&p->in, nVal, pnRec);
2874cbf6d2d2Sdan     if( p->rc!=SQLITE_OK ) return p->rc;
2875cbf6d2d2Sdan     *paRec = &p->in.aData[p->in.iNext];
2876cbf6d2d2Sdan     p->in.iNext += *pnRec;
2877cbf6d2d2Sdan   }else{
28785d607a6eSdan 
28794fccf43aSdan     /* If this is an UPDATE or DELETE, read the old.* record. */
288073b3c055Sdan     if( p->op!=SQLITE_INSERT && (p->bPatchset==0 || p->op==SQLITE_DELETE) ){
288173b3c055Sdan       u8 *abPK = p->bPatchset ? p->abPK : 0;
2882cbf6d2d2Sdan       p->rc = sessionReadRecord(&p->in, p->nCol, abPK, p->apValue);
28834fccf43aSdan       if( p->rc!=SQLITE_OK ) return p->rc;
28844fccf43aSdan     }
28854fccf43aSdan 
28864fccf43aSdan     /* If this is an INSERT or UPDATE, read the new.* record. */
28874fccf43aSdan     if( p->op!=SQLITE_DELETE ){
2888cbf6d2d2Sdan       p->rc = sessionReadRecord(&p->in, p->nCol, 0, &p->apValue[p->nCol]);
28894fccf43aSdan       if( p->rc!=SQLITE_OK ) return p->rc;
28904fccf43aSdan     }
28914fccf43aSdan 
2892cbf6d2d2Sdan     if( p->bPatchset && p->op==SQLITE_UPDATE ){
289373b3c055Sdan       /* If this is an UPDATE that is part of a patchset, then all PK and
289473b3c055Sdan       ** modified fields are present in the new.* record. The old.* record
289573b3c055Sdan       ** is currently completely empty. This block shifts the PK fields from
289673b3c055Sdan       ** new.* to old.*, to accommodate the code that reads these arrays.  */
289773b3c055Sdan       for(i=0; i<p->nCol; i++){
289873b3c055Sdan         assert( p->apValue[i]==0 );
289973b3c055Sdan         assert( p->abPK[i]==0 || p->apValue[i+p->nCol] );
290073b3c055Sdan         if( p->abPK[i] ){
290173b3c055Sdan           p->apValue[i] = p->apValue[i+p->nCol];
290273b3c055Sdan           p->apValue[i+p->nCol] = 0;
290373b3c055Sdan         }
290473b3c055Sdan       }
290573b3c055Sdan     }
2906cbf6d2d2Sdan   }
2907ef7a6304Sdan 
29084fccf43aSdan   return SQLITE_ROW;
29094fccf43aSdan }
29104fccf43aSdan 
29114fccf43aSdan /*
29125d607a6eSdan ** Advance an iterator created by sqlite3changeset_start() to the next
29135d607a6eSdan ** change in the changeset. This function may return SQLITE_ROW, SQLITE_DONE
29145d607a6eSdan ** or SQLITE_CORRUPT.
29155d607a6eSdan **
29165d607a6eSdan ** This function may not be called on iterators passed to a conflict handler
29175d607a6eSdan ** callback by changeset_apply().
29185d607a6eSdan */
29195d607a6eSdan int sqlite3changeset_next(sqlite3_changeset_iter *p){
29205d607a6eSdan   return sessionChangesetNext(p, 0, 0);
29215d607a6eSdan }
29225d607a6eSdan 
29235d607a6eSdan /*
2924244593c8Sdan ** The following function extracts information on the current change
292577fc1d5bSdan ** from a changeset iterator. It may only be called after changeset_next()
29264fccf43aSdan ** has returned SQLITE_ROW.
29274fccf43aSdan */
29284fccf43aSdan int sqlite3changeset_op(
2929296c7658Sdan   sqlite3_changeset_iter *pIter,  /* Iterator handle */
29304fccf43aSdan   const char **pzTab,             /* OUT: Pointer to table name */
29314fccf43aSdan   int *pnCol,                     /* OUT: Number of columns in table */
2932b4480e94Sdan   int *pOp,                       /* OUT: SQLITE_INSERT, DELETE or UPDATE */
2933b4480e94Sdan   int *pbIndirect                 /* OUT: True if change is indirect */
29344fccf43aSdan ){
29354fccf43aSdan   *pOp = pIter->op;
29364fccf43aSdan   *pnCol = pIter->nCol;
29374fccf43aSdan   *pzTab = pIter->zTab;
2938b4480e94Sdan   if( pbIndirect ) *pbIndirect = pIter->bIndirect;
29394fccf43aSdan   return SQLITE_OK;
29404fccf43aSdan }
29414fccf43aSdan 
294277fc1d5bSdan /*
294377fc1d5bSdan ** Return information regarding the PRIMARY KEY and number of columns in
294477fc1d5bSdan ** the database table affected by the change that pIter currently points
294577fc1d5bSdan ** to. This function may only be called after changeset_next() returns
294677fc1d5bSdan ** SQLITE_ROW.
294777fc1d5bSdan */
2948244593c8Sdan int sqlite3changeset_pk(
2949244593c8Sdan   sqlite3_changeset_iter *pIter,  /* Iterator object */
2950244593c8Sdan   unsigned char **pabPK,          /* OUT: Array of boolean - true for PK cols */
2951244593c8Sdan   int *pnCol                      /* OUT: Number of entries in output array */
2952244593c8Sdan ){
2953244593c8Sdan   *pabPK = pIter->abPK;
2954244593c8Sdan   if( pnCol ) *pnCol = pIter->nCol;
2955244593c8Sdan   return SQLITE_OK;
2956244593c8Sdan }
2957244593c8Sdan 
2958296c7658Sdan /*
2959296c7658Sdan ** This function may only be called while the iterator is pointing to an
2960296c7658Sdan ** SQLITE_UPDATE or SQLITE_DELETE change (see sqlite3changeset_op()).
2961296c7658Sdan ** Otherwise, SQLITE_MISUSE is returned.
2962296c7658Sdan **
2963296c7658Sdan ** It sets *ppValue to point to an sqlite3_value structure containing the
2964296c7658Sdan ** iVal'th value in the old.* record. Or, if that particular value is not
2965296c7658Sdan ** included in the record (because the change is an UPDATE and the field
2966296c7658Sdan ** was not modified and is not a PK column), set *ppValue to NULL.
2967296c7658Sdan **
2968296c7658Sdan ** If value iVal is out-of-range, SQLITE_RANGE is returned and *ppValue is
2969296c7658Sdan ** not modified. Otherwise, SQLITE_OK.
2970296c7658Sdan */
29714fccf43aSdan int sqlite3changeset_old(
2972296c7658Sdan   sqlite3_changeset_iter *pIter,  /* Changeset iterator */
2973296c7658Sdan   int iVal,                       /* Index of old.* value to retrieve */
29744fccf43aSdan   sqlite3_value **ppValue         /* OUT: Old value (or NULL pointer) */
29754fccf43aSdan ){
2976d5f0767cSdan   if( pIter->op!=SQLITE_UPDATE && pIter->op!=SQLITE_DELETE ){
2977d5f0767cSdan     return SQLITE_MISUSE;
2978d5f0767cSdan   }
29794fccf43aSdan   if( iVal<0 || iVal>=pIter->nCol ){
29804fccf43aSdan     return SQLITE_RANGE;
29814fccf43aSdan   }
29824fccf43aSdan   *ppValue = pIter->apValue[iVal];
29834fccf43aSdan   return SQLITE_OK;
29844fccf43aSdan }
29854fccf43aSdan 
2986296c7658Sdan /*
2987296c7658Sdan ** This function may only be called while the iterator is pointing to an
2988296c7658Sdan ** SQLITE_UPDATE or SQLITE_INSERT change (see sqlite3changeset_op()).
2989296c7658Sdan ** Otherwise, SQLITE_MISUSE is returned.
2990296c7658Sdan **
2991296c7658Sdan ** It sets *ppValue to point to an sqlite3_value structure containing the
2992296c7658Sdan ** iVal'th value in the new.* record. Or, if that particular value is not
2993296c7658Sdan ** included in the record (because the change is an UPDATE and the field
2994296c7658Sdan ** was not modified), set *ppValue to NULL.
2995296c7658Sdan **
2996296c7658Sdan ** If value iVal is out-of-range, SQLITE_RANGE is returned and *ppValue is
2997296c7658Sdan ** not modified. Otherwise, SQLITE_OK.
2998296c7658Sdan */
29994fccf43aSdan int sqlite3changeset_new(
3000296c7658Sdan   sqlite3_changeset_iter *pIter,  /* Changeset iterator */
3001296c7658Sdan   int iVal,                       /* Index of new.* value to retrieve */
30024fccf43aSdan   sqlite3_value **ppValue         /* OUT: New value (or NULL pointer) */
30034fccf43aSdan ){
3004d5f0767cSdan   if( pIter->op!=SQLITE_UPDATE && pIter->op!=SQLITE_INSERT ){
3005d5f0767cSdan     return SQLITE_MISUSE;
3006d5f0767cSdan   }
30074fccf43aSdan   if( iVal<0 || iVal>=pIter->nCol ){
30084fccf43aSdan     return SQLITE_RANGE;
30094fccf43aSdan   }
30104fccf43aSdan   *ppValue = pIter->apValue[pIter->nCol+iVal];
30114fccf43aSdan   return SQLITE_OK;
30124fccf43aSdan }
30134fccf43aSdan 
3014296c7658Sdan /*
30157aa469cdSdan ** The following two macros are used internally. They are similar to the
30167aa469cdSdan ** sqlite3changeset_new() and sqlite3changeset_old() functions, except that
30177aa469cdSdan ** they omit all error checking and return a pointer to the requested value.
30187aa469cdSdan */
30197aa469cdSdan #define sessionChangesetNew(pIter, iVal) (pIter)->apValue[(pIter)->nCol+(iVal)]
30207aa469cdSdan #define sessionChangesetOld(pIter, iVal) (pIter)->apValue[(iVal)]
30217aa469cdSdan 
30227aa469cdSdan /*
3023296c7658Sdan ** This function may only be called with a changeset iterator that has been
3024296c7658Sdan ** passed to an SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT
3025296c7658Sdan ** conflict-handler function. Otherwise, SQLITE_MISUSE is returned.
3026296c7658Sdan **
3027296c7658Sdan ** If successful, *ppValue is set to point to an sqlite3_value structure
3028296c7658Sdan ** containing the iVal'th value of the conflicting record.
3029296c7658Sdan **
3030296c7658Sdan ** If value iVal is out-of-range or some other error occurs, an SQLite error
3031296c7658Sdan ** code is returned. Otherwise, SQLITE_OK.
3032296c7658Sdan */
3033d5f0767cSdan int sqlite3changeset_conflict(
3034296c7658Sdan   sqlite3_changeset_iter *pIter,  /* Changeset iterator */
3035296c7658Sdan   int iVal,                       /* Index of conflict record value to fetch */
3036d5f0767cSdan   sqlite3_value **ppValue         /* OUT: Value from conflicting row */
3037d5f0767cSdan ){
3038d5f0767cSdan   if( !pIter->pConflict ){
3039d5f0767cSdan     return SQLITE_MISUSE;
3040d5f0767cSdan   }
3041ff677b20Sdan   if( iVal<0 || iVal>=pIter->nCol ){
3042d5f0767cSdan     return SQLITE_RANGE;
3043d5f0767cSdan   }
3044d5f0767cSdan   *ppValue = sqlite3_column_value(pIter->pConflict, iVal);
3045d5f0767cSdan   return SQLITE_OK;
3046d5f0767cSdan }
3047d5f0767cSdan 
30484fccf43aSdan /*
3049cb3e4b79Sdan ** This function may only be called with an iterator passed to an
3050cb3e4b79Sdan ** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case
3051cb3e4b79Sdan ** it sets the output variable to the total number of known foreign key
3052cb3e4b79Sdan ** violations in the destination database and returns SQLITE_OK.
3053cb3e4b79Sdan **
3054cb3e4b79Sdan ** In all other cases this function returns SQLITE_MISUSE.
3055cb3e4b79Sdan */
3056cb3e4b79Sdan int sqlite3changeset_fk_conflicts(
3057cb3e4b79Sdan   sqlite3_changeset_iter *pIter,  /* Changeset iterator */
3058cb3e4b79Sdan   int *pnOut                      /* OUT: Number of FK violations */
3059cb3e4b79Sdan ){
3060cb3e4b79Sdan   if( pIter->pConflict || pIter->apValue ){
3061cb3e4b79Sdan     return SQLITE_MISUSE;
3062cb3e4b79Sdan   }
3063cb3e4b79Sdan   *pnOut = pIter->nCol;
3064cb3e4b79Sdan   return SQLITE_OK;
3065cb3e4b79Sdan }
3066cb3e4b79Sdan 
3067cb3e4b79Sdan 
3068cb3e4b79Sdan /*
30694fccf43aSdan ** Finalize an iterator allocated with sqlite3changeset_start().
30704fccf43aSdan **
30714fccf43aSdan ** This function may not be called on iterators passed to a conflict handler
30724fccf43aSdan ** callback by changeset_apply().
30734fccf43aSdan */
30744fccf43aSdan int sqlite3changeset_finalize(sqlite3_changeset_iter *p){
3075cbf6d2d2Sdan   int rc = SQLITE_OK;
3076cbf6d2d2Sdan   if( p ){
3077296c7658Sdan     int i;                        /* Used to iterate through p->apValue[] */
3078cbf6d2d2Sdan     rc = p->rc;
307912ca0b56Sdan     if( p->apValue ){
30804fccf43aSdan       for(i=0; i<p->nCol*2; i++) sqlite3ValueFree(p->apValue[i]);
308112ca0b56Sdan     }
3082ef7a6304Sdan     sqlite3_free(p->tblhdr.aBuf);
30834757c658Sdan     sqlite3_free(p->in.buf.aBuf);
30844fccf43aSdan     sqlite3_free(p);
3085cbf6d2d2Sdan   }
30864fccf43aSdan   return rc;
30874fccf43aSdan }
30884fccf43aSdan 
3089fa122adaSdan static int sessionChangesetInvert(
3090fa122adaSdan   SessionInput *pInput,           /* Input changeset */
3091fa122adaSdan   int (*xOutput)(void *pOut, const void *pData, int nData),
3092fa122adaSdan   void *pOut,
309391ddd559Sdan   int *pnInverted,                /* OUT: Number of bytes in output changeset */
309491ddd559Sdan   void **ppInverted               /* OUT: Inverse of pChangeset */
309591ddd559Sdan ){
3096cfec7eeeSdan   int rc = SQLITE_OK;             /* Return value */
3097fa122adaSdan   SessionBuffer sOut;             /* Output buffer */
3098cfec7eeeSdan   int nCol = 0;                   /* Number of cols in current table */
3099cfec7eeeSdan   u8 *abPK = 0;                   /* PK array for current table */
3100cfec7eeeSdan   sqlite3_value **apVal = 0;      /* Space for values for UPDATE inversion */
3101ef7a6304Sdan   SessionBuffer sPK = {0, 0, 0};  /* PK array for current table */
310291ddd559Sdan 
3103fa122adaSdan   /* Initialize the output buffer */
3104fa122adaSdan   memset(&sOut, 0, sizeof(SessionBuffer));
3105fa122adaSdan 
310691ddd559Sdan   /* Zero the output variables in case an error occurs. */
3107fa122adaSdan   if( ppInverted ){
310891ddd559Sdan     *ppInverted = 0;
310991ddd559Sdan     *pnInverted = 0;
3110fa122adaSdan   }
311191ddd559Sdan 
3112fa122adaSdan   while( 1 ){
3113ef7a6304Sdan     u8 eType;
3114fa122adaSdan 
3115fa122adaSdan     /* Test for EOF. */
3116fa122adaSdan     if( (rc = sessionInputBuffer(pInput, 2)) ) goto finished_invert;
3117fa122adaSdan     if( pInput->iNext>=pInput->nData ) break;
3118fa122adaSdan     eType = pInput->aData[pInput->iNext];
3119fa122adaSdan 
312091ddd559Sdan     switch( eType ){
312191ddd559Sdan       case 'T': {
3122244593c8Sdan         /* A 'table' record consists of:
3123244593c8Sdan         **
3124244593c8Sdan         **   * A constant 'T' character,
3125244593c8Sdan         **   * Number of columns in said table (a varint),
3126ef7a6304Sdan         **   * An array of nCol bytes (sPK),
3127244593c8Sdan         **   * A nul-terminated table name.
3128244593c8Sdan         */
3129ef7a6304Sdan         int nByte;
3130fa122adaSdan         int nVar;
3131fa122adaSdan         pInput->iNext++;
3132fa122adaSdan         if( (rc = sessionChangesetBufferTblhdr(pInput, &nByte)) ){
3133ef7a6304Sdan           goto finished_invert;
3134ef7a6304Sdan         }
3135fa122adaSdan         nVar = sessionVarintGet(&pInput->aData[pInput->iNext], &nCol);
3136ef7a6304Sdan         sPK.nBuf = 0;
3137fa122adaSdan         sessionAppendBlob(&sPK, &pInput->aData[pInput->iNext+nVar], nCol, &rc);
3138fa122adaSdan         sessionAppendByte(&sOut, eType, &rc);
3139fa122adaSdan         sessionAppendBlob(&sOut, &pInput->aData[pInput->iNext], nByte, &rc);
3140ef7a6304Sdan         if( rc ) goto finished_invert;
3141fa122adaSdan 
3142fa122adaSdan         pInput->iNext += nByte;
3143cfec7eeeSdan         sqlite3_free(apVal);
3144cfec7eeeSdan         apVal = 0;
3145ef7a6304Sdan         abPK = sPK.aBuf;
314691ddd559Sdan         break;
314791ddd559Sdan       }
314891ddd559Sdan 
314991ddd559Sdan       case SQLITE_INSERT:
315091ddd559Sdan       case SQLITE_DELETE: {
315191ddd559Sdan         int nByte;
3152fa122adaSdan         int bIndirect = pInput->aData[pInput->iNext+1];
3153fa122adaSdan         int eType2 = (eType==SQLITE_DELETE ? SQLITE_INSERT : SQLITE_DELETE);
3154fa122adaSdan         pInput->iNext += 2;
3155fa122adaSdan         assert( rc==SQLITE_OK );
3156fa122adaSdan         rc = sessionChangesetBufferRecord(pInput, nCol, &nByte);
3157fa122adaSdan         sessionAppendByte(&sOut, eType2, &rc);
3158fa122adaSdan         sessionAppendByte(&sOut, bIndirect, &rc);
3159fa122adaSdan         sessionAppendBlob(&sOut, &pInput->aData[pInput->iNext], nByte, &rc);
3160fa122adaSdan         pInput->iNext += nByte;
3161fa122adaSdan         if( rc ) goto finished_invert;
316291ddd559Sdan         break;
316391ddd559Sdan       }
316491ddd559Sdan 
316591ddd559Sdan       case SQLITE_UPDATE: {
3166cfec7eeeSdan         int iCol;
316791ddd559Sdan 
3168cfec7eeeSdan         if( 0==apVal ){
3169cfec7eeeSdan           apVal = (sqlite3_value **)sqlite3_malloc(sizeof(apVal[0])*nCol*2);
3170cfec7eeeSdan           if( 0==apVal ){
3171cfec7eeeSdan             rc = SQLITE_NOMEM;
3172cfec7eeeSdan             goto finished_invert;
3173cfec7eeeSdan           }
3174cfec7eeeSdan           memset(apVal, 0, sizeof(apVal[0])*nCol*2);
3175cfec7eeeSdan         }
317691ddd559Sdan 
3177cfec7eeeSdan         /* Write the header for the new UPDATE change. Same as the original. */
3178fa122adaSdan         sessionAppendByte(&sOut, eType, &rc);
3179fa122adaSdan         sessionAppendByte(&sOut, pInput->aData[pInput->iNext+1], &rc);
318091ddd559Sdan 
3181ef7a6304Sdan         /* Read the old.* and new.* records for the update change. */
3182fa122adaSdan         pInput->iNext += 2;
3183fa122adaSdan         rc = sessionReadRecord(pInput, nCol, 0, &apVal[0]);
3184ef7a6304Sdan         if( rc==SQLITE_OK ){
3185fa122adaSdan           rc = sessionReadRecord(pInput, nCol, 0, &apVal[nCol]);
3186ef7a6304Sdan         }
3187ef7a6304Sdan 
3188cfec7eeeSdan         /* Write the new old.* record. Consists of the PK columns from the
3189cfec7eeeSdan         ** original old.* record, and the other values from the original
3190cfec7eeeSdan         ** new.* record. */
3191e8fa8c96Sdan         for(iCol=0; iCol<nCol; iCol++){
3192cfec7eeeSdan           sqlite3_value *pVal = apVal[iCol + (abPK[iCol] ? 0 : nCol)];
3193fa122adaSdan           sessionAppendValue(&sOut, pVal, &rc);
3194cfec7eeeSdan         }
3195cfec7eeeSdan 
3196cfec7eeeSdan         /* Write the new new.* record. Consists of a copy of all values
3197cfec7eeeSdan         ** from the original old.* record, except for the PK columns, which
3198cfec7eeeSdan         ** are set to "undefined". */
3199e8fa8c96Sdan         for(iCol=0; iCol<nCol; iCol++){
3200cfec7eeeSdan           sqlite3_value *pVal = (abPK[iCol] ? 0 : apVal[iCol]);
3201fa122adaSdan           sessionAppendValue(&sOut, pVal, &rc);
3202cfec7eeeSdan         }
3203cfec7eeeSdan 
3204cfec7eeeSdan         for(iCol=0; iCol<nCol*2; iCol++){
3205cfec7eeeSdan           sqlite3ValueFree(apVal[iCol]);
3206cfec7eeeSdan         }
3207cfec7eeeSdan         memset(apVal, 0, sizeof(apVal[0])*nCol*2);
3208cfec7eeeSdan         if( rc!=SQLITE_OK ){
3209cfec7eeeSdan           goto finished_invert;
3210cfec7eeeSdan         }
3211cfec7eeeSdan 
321291ddd559Sdan         break;
321391ddd559Sdan       }
321491ddd559Sdan 
321591ddd559Sdan       default:
32164757c658Sdan         rc = SQLITE_CORRUPT_BKPT;
3217cfec7eeeSdan         goto finished_invert;
321891ddd559Sdan     }
3219fa122adaSdan 
3220fa122adaSdan     assert( rc==SQLITE_OK );
3221f1a08ad8Sdrh     if( xOutput && sOut.nBuf>=SESSIONS_STRM_CHUNK_SIZE ){
3222fa122adaSdan       rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
3223fa122adaSdan       sOut.nBuf = 0;
3224fa122adaSdan       if( rc!=SQLITE_OK ) goto finished_invert;
3225fa122adaSdan     }
322691ddd559Sdan   }
322791ddd559Sdan 
3228cfec7eeeSdan   assert( rc==SQLITE_OK );
3229fa122adaSdan   if( pnInverted ){
3230fa122adaSdan     *pnInverted = sOut.nBuf;
3231fa122adaSdan     *ppInverted = sOut.aBuf;
3232fa122adaSdan     sOut.aBuf = 0;
3233fa122adaSdan   }else if( sOut.nBuf>0 ){
3234fa122adaSdan     rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
3235fa122adaSdan   }
3236cfec7eeeSdan 
3237cfec7eeeSdan  finished_invert:
3238fa122adaSdan   sqlite3_free(sOut.aBuf);
3239cfec7eeeSdan   sqlite3_free(apVal);
3240ef7a6304Sdan   sqlite3_free(sPK.aBuf);
3241cfec7eeeSdan   return rc;
324291ddd559Sdan }
324391ddd559Sdan 
3244fa122adaSdan 
3245fa122adaSdan /*
3246fa122adaSdan ** Invert a changeset object.
3247fa122adaSdan */
3248fa122adaSdan int sqlite3changeset_invert(
3249fa122adaSdan   int nChangeset,                 /* Number of bytes in input */
3250fa122adaSdan   const void *pChangeset,         /* Input changeset */
3251fa122adaSdan   int *pnInverted,                /* OUT: Number of bytes in output changeset */
3252fa122adaSdan   void **ppInverted               /* OUT: Inverse of pChangeset */
3253fa122adaSdan ){
3254fa122adaSdan   SessionInput sInput;
3255fa122adaSdan 
3256fa122adaSdan   /* Set up the input stream */
3257fa122adaSdan   memset(&sInput, 0, sizeof(SessionInput));
3258fa122adaSdan   sInput.nData = nChangeset;
3259fa122adaSdan   sInput.aData = (u8*)pChangeset;
3260fa122adaSdan 
3261fa122adaSdan   return sessionChangesetInvert(&sInput, 0, 0, pnInverted, ppInverted);
3262fa122adaSdan }
3263fa122adaSdan 
3264fa122adaSdan /*
3265fa122adaSdan ** Streaming version of sqlite3changeset_invert().
3266fa122adaSdan */
3267f1a08ad8Sdrh int sqlite3changeset_invert_strm(
3268fa122adaSdan   int (*xInput)(void *pIn, void *pData, int *pnData),
3269fa122adaSdan   void *pIn,
3270fa122adaSdan   int (*xOutput)(void *pOut, const void *pData, int nData),
3271fa122adaSdan   void *pOut
3272fa122adaSdan ){
3273fa122adaSdan   SessionInput sInput;
3274fa122adaSdan   int rc;
3275fa122adaSdan 
3276fa122adaSdan   /* Set up the input stream */
3277fa122adaSdan   memset(&sInput, 0, sizeof(SessionInput));
3278fa122adaSdan   sInput.xInput = xInput;
3279fa122adaSdan   sInput.pIn = pIn;
3280fa122adaSdan 
3281fa122adaSdan   rc = sessionChangesetInvert(&sInput, xOutput, pOut, 0, 0);
3282fa122adaSdan   sqlite3_free(sInput.buf.aBuf);
3283fa122adaSdan   return rc;
3284fa122adaSdan }
3285fa122adaSdan 
32860c698471Sdan typedef struct SessionApplyCtx SessionApplyCtx;
32870c698471Sdan struct SessionApplyCtx {
32880c698471Sdan   sqlite3 *db;
32890c698471Sdan   sqlite3_stmt *pDelete;          /* DELETE statement */
3290cfec7eeeSdan   sqlite3_stmt *pUpdate;          /* UPDATE statement */
32910c698471Sdan   sqlite3_stmt *pInsert;          /* INSERT statement */
32920c698471Sdan   sqlite3_stmt *pSelect;          /* SELECT statement */
32930c698471Sdan   int nCol;                       /* Size of azCol[] and abPK[] arrays */
32940c698471Sdan   const char **azCol;             /* Array of column names */
32950c698471Sdan   u8 *abPK;                       /* Boolean array - true if column is in PK */
3296d9151526Sdan 
3297d9151526Sdan   int bDeferConstraints;          /* True to defer constraints */
3298d9151526Sdan   SessionBuffer constraints;      /* Deferred constraints are stored here */
32990c698471Sdan };
33000c698471Sdan 
3301d5f0767cSdan /*
3302d5f0767cSdan ** Formulate a statement to DELETE a row from database db. Assuming a table
3303d5f0767cSdan ** structure like this:
3304d5f0767cSdan **
3305d5f0767cSdan **     CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c));
3306d5f0767cSdan **
3307d5f0767cSdan ** The DELETE statement looks like this:
3308d5f0767cSdan **
3309db04571cSdan **     DELETE FROM x WHERE a = :1 AND c = :3 AND (:5 OR b IS :2 AND d IS :4)
3310d5f0767cSdan **
3311d5f0767cSdan ** Variable :5 (nCol+1) is a boolean. It should be set to 0 if we require
3312d5f0767cSdan ** matching b and d values, or 1 otherwise. The second case comes up if the
3313d5f0767cSdan ** conflict handler is invoked with NOTFOUND and returns CHANGESET_REPLACE.
3314296c7658Sdan **
3315296c7658Sdan ** If successful, SQLITE_OK is returned and SessionApplyCtx.pDelete is left
3316296c7658Sdan ** pointing to the prepared version of the SQL statement.
3317d5f0767cSdan */
3318d5f0767cSdan static int sessionDeleteRow(
3319d5f0767cSdan   sqlite3 *db,                    /* Database handle */
3320d5f0767cSdan   const char *zTab,               /* Table name */
33210c698471Sdan   SessionApplyCtx *p              /* Session changeset-apply context */
3322d5f0767cSdan ){
3323296c7658Sdan   int i;
3324296c7658Sdan   const char *zSep = "";
3325d5f0767cSdan   int rc = SQLITE_OK;
3326d5f0767cSdan   SessionBuffer buf = {0, 0, 0};
33277cf7df7dSdan   int nPk = 0;
3328d5f0767cSdan 
3329d5f0767cSdan   sessionAppendStr(&buf, "DELETE FROM ", &rc);
3330d5f0767cSdan   sessionAppendIdent(&buf, zTab, &rc);
3331296c7658Sdan   sessionAppendStr(&buf, " WHERE ", &rc);
3332296c7658Sdan 
3333296c7658Sdan   for(i=0; i<p->nCol; i++){
3334296c7658Sdan     if( p->abPK[i] ){
33357cf7df7dSdan       nPk++;
3336296c7658Sdan       sessionAppendStr(&buf, zSep, &rc);
3337296c7658Sdan       sessionAppendIdent(&buf, p->azCol[i], &rc);
3338296c7658Sdan       sessionAppendStr(&buf, " = ?", &rc);
3339296c7658Sdan       sessionAppendInteger(&buf, i+1, &rc);
3340296c7658Sdan       zSep = " AND ";
3341296c7658Sdan     }
3342296c7658Sdan   }
3343296c7658Sdan 
33447cf7df7dSdan   if( nPk<p->nCol ){
3345296c7658Sdan     sessionAppendStr(&buf, " AND (?", &rc);
3346296c7658Sdan     sessionAppendInteger(&buf, p->nCol+1, &rc);
3347296c7658Sdan     sessionAppendStr(&buf, " OR ", &rc);
3348296c7658Sdan 
3349296c7658Sdan     zSep = "";
3350296c7658Sdan     for(i=0; i<p->nCol; i++){
3351296c7658Sdan       if( !p->abPK[i] ){
3352296c7658Sdan         sessionAppendStr(&buf, zSep, &rc);
3353296c7658Sdan         sessionAppendIdent(&buf, p->azCol[i], &rc);
3354296c7658Sdan         sessionAppendStr(&buf, " IS ?", &rc);
3355296c7658Sdan         sessionAppendInteger(&buf, i+1, &rc);
3356296c7658Sdan         zSep = "AND ";
3357296c7658Sdan       }
3358296c7658Sdan     }
3359296c7658Sdan     sessionAppendStr(&buf, ")", &rc);
33607cf7df7dSdan   }
3361d5f0767cSdan 
3362d5f0767cSdan   if( rc==SQLITE_OK ){
33630c698471Sdan     rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pDelete, 0);
3364d5f0767cSdan   }
3365d5f0767cSdan   sqlite3_free(buf.aBuf);
3366d5f0767cSdan 
3367d5f0767cSdan   return rc;
3368d5f0767cSdan }
3369d5f0767cSdan 
3370d5f0767cSdan /*
3371d5f0767cSdan ** Formulate and prepare a statement to UPDATE a row from database db.
3372d5f0767cSdan ** Assuming a table structure like this:
3373d5f0767cSdan **
3374d5f0767cSdan **     CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c));
3375d5f0767cSdan **
3376d5f0767cSdan ** The UPDATE statement looks like this:
3377d5f0767cSdan **
3378d5f0767cSdan **     UPDATE x SET
3379d5f0767cSdan **     a = CASE WHEN ?2  THEN ?3  ELSE a END,
3380964cbd46Sdan **     b = CASE WHEN ?5  THEN ?6  ELSE b END,
3381964cbd46Sdan **     c = CASE WHEN ?8  THEN ?9  ELSE c END,
3382964cbd46Sdan **     d = CASE WHEN ?11 THEN ?12 ELSE d END
3383d5f0767cSdan **     WHERE a = ?1 AND c = ?7 AND (?13 OR
3384964cbd46Sdan **       (?5==0 OR b IS ?4) AND (?11==0 OR d IS ?10) AND
3385d5f0767cSdan **     )
3386d5f0767cSdan **
3387d5f0767cSdan ** For each column in the table, there are three variables to bind:
3388d5f0767cSdan **
3389d5f0767cSdan **     ?(i*3+1)    The old.* value of the column, if any.
3390d5f0767cSdan **     ?(i*3+2)    A boolean flag indicating that the value is being modified.
3391d5f0767cSdan **     ?(i*3+3)    The new.* value of the column, if any.
3392d5f0767cSdan **
3393d5f0767cSdan ** Also, a boolean flag that, if set to true, causes the statement to update
3394d5f0767cSdan ** a row even if the non-PK values do not match. This is required if the
3395d5f0767cSdan ** conflict-handler is invoked with CHANGESET_DATA and returns
3396d5f0767cSdan ** CHANGESET_REPLACE. This is variable "?(nCol*3+1)".
3397d5f0767cSdan **
3398296c7658Sdan ** If successful, SQLITE_OK is returned and SessionApplyCtx.pUpdate is left
3399296c7658Sdan ** pointing to the prepared version of the SQL statement.
3400d5f0767cSdan */
3401d5f0767cSdan static int sessionUpdateRow(
3402d5f0767cSdan   sqlite3 *db,                    /* Database handle */
3403d5f0767cSdan   const char *zTab,               /* Table name */
34040c698471Sdan   SessionApplyCtx *p              /* Session changeset-apply context */
3405d5f0767cSdan ){
3406d5f0767cSdan   int rc = SQLITE_OK;
3407d5f0767cSdan   int i;
3408d5f0767cSdan   const char *zSep = "";
3409d5f0767cSdan   SessionBuffer buf = {0, 0, 0};
3410d5f0767cSdan 
3411d5f0767cSdan   /* Append "UPDATE tbl SET " */
3412d5f0767cSdan   sessionAppendStr(&buf, "UPDATE ", &rc);
3413d5f0767cSdan   sessionAppendIdent(&buf, zTab, &rc);
3414d5f0767cSdan   sessionAppendStr(&buf, " SET ", &rc);
3415d5f0767cSdan 
3416d5f0767cSdan   /* Append the assignments */
34170c698471Sdan   for(i=0; i<p->nCol; i++){
3418d5f0767cSdan     sessionAppendStr(&buf, zSep, &rc);
34190c698471Sdan     sessionAppendIdent(&buf, p->azCol[i], &rc);
3420d5f0767cSdan     sessionAppendStr(&buf, " = CASE WHEN ?", &rc);
3421d5f0767cSdan     sessionAppendInteger(&buf, i*3+2, &rc);
3422d5f0767cSdan     sessionAppendStr(&buf, " THEN ?", &rc);
3423d5f0767cSdan     sessionAppendInteger(&buf, i*3+3, &rc);
3424d5f0767cSdan     sessionAppendStr(&buf, " ELSE ", &rc);
34250c698471Sdan     sessionAppendIdent(&buf, p->azCol[i], &rc);
3426d5f0767cSdan     sessionAppendStr(&buf, " END", &rc);
3427d5f0767cSdan     zSep = ", ";
3428d5f0767cSdan   }
3429d5f0767cSdan 
3430d5f0767cSdan   /* Append the PK part of the WHERE clause */
3431d5f0767cSdan   sessionAppendStr(&buf, " WHERE ", &rc);
34320c698471Sdan   for(i=0; i<p->nCol; i++){
34330c698471Sdan     if( p->abPK[i] ){
34340c698471Sdan       sessionAppendIdent(&buf, p->azCol[i], &rc);
3435d5f0767cSdan       sessionAppendStr(&buf, " = ?", &rc);
3436d5f0767cSdan       sessionAppendInteger(&buf, i*3+1, &rc);
3437d5f0767cSdan       sessionAppendStr(&buf, " AND ", &rc);
3438d5f0767cSdan     }
3439d5f0767cSdan   }
3440d5f0767cSdan 
3441d5f0767cSdan   /* Append the non-PK part of the WHERE clause */
3442d5f0767cSdan   sessionAppendStr(&buf, " (?", &rc);
34430c698471Sdan   sessionAppendInteger(&buf, p->nCol*3+1, &rc);
3444d5f0767cSdan   sessionAppendStr(&buf, " OR 1", &rc);
34450c698471Sdan   for(i=0; i<p->nCol; i++){
34460c698471Sdan     if( !p->abPK[i] ){
3447d5f0767cSdan       sessionAppendStr(&buf, " AND (?", &rc);
3448d5f0767cSdan       sessionAppendInteger(&buf, i*3+2, &rc);
3449d5f0767cSdan       sessionAppendStr(&buf, "=0 OR ", &rc);
34500c698471Sdan       sessionAppendIdent(&buf, p->azCol[i], &rc);
3451d5f0767cSdan       sessionAppendStr(&buf, " IS ?", &rc);
3452d5f0767cSdan       sessionAppendInteger(&buf, i*3+1, &rc);
3453d5f0767cSdan       sessionAppendStr(&buf, ")", &rc);
3454d5f0767cSdan     }
3455d5f0767cSdan   }
3456d5f0767cSdan   sessionAppendStr(&buf, ")", &rc);
3457d5f0767cSdan 
3458d5f0767cSdan   if( rc==SQLITE_OK ){
34590c698471Sdan     rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pUpdate, 0);
3460d5f0767cSdan   }
3461d5f0767cSdan   sqlite3_free(buf.aBuf);
3462d5f0767cSdan 
3463d5f0767cSdan   return rc;
3464d5f0767cSdan }
3465d5f0767cSdan 
3466296c7658Sdan /*
3467296c7658Sdan ** Formulate and prepare an SQL statement to query table zTab by primary
3468296c7658Sdan ** key. Assuming the following table structure:
3469296c7658Sdan **
3470296c7658Sdan **     CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c));
3471296c7658Sdan **
3472296c7658Sdan ** The SELECT statement looks like this:
3473296c7658Sdan **
3474296c7658Sdan **     SELECT * FROM x WHERE a = ?1 AND c = ?3
3475296c7658Sdan **
3476296c7658Sdan ** If successful, SQLITE_OK is returned and SessionApplyCtx.pSelect is left
3477296c7658Sdan ** pointing to the prepared version of the SQL statement.
3478296c7658Sdan */
3479d5f0767cSdan static int sessionSelectRow(
3480d5f0767cSdan   sqlite3 *db,                    /* Database handle */
3481d5f0767cSdan   const char *zTab,               /* Table name */
34820c698471Sdan   SessionApplyCtx *p              /* Session changeset-apply context */
3483d5f0767cSdan ){
3484d7fb7d24Sdan   return sessionSelectStmt(
3485d7fb7d24Sdan       db, "main", zTab, p->nCol, p->azCol, p->abPK, &p->pSelect);
3486d5f0767cSdan }
3487d5f0767cSdan 
3488296c7658Sdan /*
3489296c7658Sdan ** Formulate and prepare an INSERT statement to add a record to table zTab.
3490296c7658Sdan ** For example:
3491296c7658Sdan **
3492296c7658Sdan **     INSERT INTO main."zTab" VALUES(?1, ?2, ?3 ...);
3493296c7658Sdan **
3494296c7658Sdan ** If successful, SQLITE_OK is returned and SessionApplyCtx.pInsert is left
3495296c7658Sdan ** pointing to the prepared version of the SQL statement.
3496296c7658Sdan */
34970c698471Sdan static int sessionInsertRow(
34980c698471Sdan   sqlite3 *db,                    /* Database handle */
34990c698471Sdan   const char *zTab,               /* Table name */
35000c698471Sdan   SessionApplyCtx *p              /* Session changeset-apply context */
35010c698471Sdan ){
35020c698471Sdan   int rc = SQLITE_OK;
35030c698471Sdan   int i;
35040c698471Sdan   SessionBuffer buf = {0, 0, 0};
35050c698471Sdan 
35060c698471Sdan   sessionAppendStr(&buf, "INSERT INTO main.", &rc);
35070c698471Sdan   sessionAppendIdent(&buf, zTab, &rc);
3508ff677b20Sdan   sessionAppendStr(&buf, "(", &rc);
3509ff677b20Sdan   for(i=0; i<p->nCol; i++){
3510ff677b20Sdan     if( i!=0 ) sessionAppendStr(&buf, ", ", &rc);
3511ff677b20Sdan     sessionAppendIdent(&buf, p->azCol[i], &rc);
3512ff677b20Sdan   }
3513ff677b20Sdan 
3514ff677b20Sdan   sessionAppendStr(&buf, ") VALUES(?", &rc);
35150c698471Sdan   for(i=1; i<p->nCol; i++){
35160c698471Sdan     sessionAppendStr(&buf, ", ?", &rc);
35170c698471Sdan   }
35180c698471Sdan   sessionAppendStr(&buf, ")", &rc);
35190c698471Sdan 
35200c698471Sdan   if( rc==SQLITE_OK ){
35210c698471Sdan     rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pInsert, 0);
35220c698471Sdan   }
35230c698471Sdan   sqlite3_free(buf.aBuf);
35240c698471Sdan   return rc;
35250c698471Sdan }
35260c698471Sdan 
3527296c7658Sdan /*
35287aa469cdSdan ** A wrapper around sqlite3_bind_value() that detects an extra problem.
35297aa469cdSdan ** See comments in the body of this function for details.
35307aa469cdSdan */
35317aa469cdSdan static int sessionBindValue(
35327aa469cdSdan   sqlite3_stmt *pStmt,            /* Statement to bind value to */
35337aa469cdSdan   int i,                          /* Parameter number to bind to */
35347aa469cdSdan   sqlite3_value *pVal             /* Value to bind */
35357aa469cdSdan ){
35365671ef69Sdrh   int eType = sqlite3_value_type(pVal);
3537082c96dfSdan   /* COVERAGE: The (pVal->z==0) branch is never true using current versions
3538082c96dfSdan   ** of SQLite. If a malloc fails in an sqlite3_value_xxx() function, either
3539082c96dfSdan   ** the (pVal->z) variable remains as it was or the type of the value is
3540082c96dfSdan   ** set to SQLITE_NULL.  */
35415671ef69Sdrh   if( (eType==SQLITE_TEXT || eType==SQLITE_BLOB) && pVal->z==0 ){
35427aa469cdSdan     /* This condition occurs when an earlier OOM in a call to
35437aa469cdSdan     ** sqlite3_value_text() or sqlite3_value_blob() (perhaps from within
3544082c96dfSdan     ** a conflict-handler) has zeroed the pVal->z pointer. Return NOMEM. */
35457aa469cdSdan     return SQLITE_NOMEM;
35467aa469cdSdan   }
35477aa469cdSdan   return sqlite3_bind_value(pStmt, i, pVal);
35487aa469cdSdan }
35497aa469cdSdan 
35507aa469cdSdan /*
3551db04571cSdan ** Iterator pIter must point to an SQLITE_INSERT entry. This function
3552db04571cSdan ** transfers new.* values from the current iterator entry to statement
3553db04571cSdan ** pStmt. The table being inserted into has nCol columns.
3554db04571cSdan **
3555d9151526Sdan ** New.* value $i from the iterator is bound to variable ($i+1) of
3556db04571cSdan ** statement pStmt. If parameter abPK is NULL, all values from 0 to (nCol-1)
3557db04571cSdan ** are transfered to the statement. Otherwise, if abPK is not NULL, it points
3558db04571cSdan ** to an array nCol elements in size. In this case only those values for
3559db04571cSdan ** which abPK[$i] is true are read from the iterator and bound to the
3560db04571cSdan ** statement.
3561db04571cSdan **
3562db04571cSdan ** An SQLite error code is returned if an error occurs. Otherwise, SQLITE_OK.
3563db04571cSdan */
35647aa469cdSdan static int sessionBindRow(
3565db04571cSdan   sqlite3_changeset_iter *pIter,  /* Iterator to read values from */
35667aa469cdSdan   int(*xValue)(sqlite3_changeset_iter *, int, sqlite3_value **),
3567db04571cSdan   int nCol,                       /* Number of columns */
3568db04571cSdan   u8 *abPK,                       /* If not NULL, bind only if true */
3569db04571cSdan   sqlite3_stmt *pStmt             /* Bind values to this statement */
3570db04571cSdan ){
3571db04571cSdan   int i;
3572db04571cSdan   int rc = SQLITE_OK;
35737aa469cdSdan 
35747aa469cdSdan   /* Neither sqlite3changeset_old or sqlite3changeset_new can fail if the
35757aa469cdSdan   ** argument iterator points to a suitable entry. Make sure that xValue
35767aa469cdSdan   ** is one of these to guarantee that it is safe to ignore the return
35777aa469cdSdan   ** in the code below. */
35787aa469cdSdan   assert( xValue==sqlite3changeset_old || xValue==sqlite3changeset_new );
35797aa469cdSdan 
3580db04571cSdan   for(i=0; rc==SQLITE_OK && i<nCol; i++){
3581db04571cSdan     if( !abPK || abPK[i] ){
3582db04571cSdan       sqlite3_value *pVal;
35837aa469cdSdan       (void)xValue(pIter, i, &pVal);
35847aa469cdSdan       rc = sessionBindValue(pStmt, i+1, pVal);
3585db04571cSdan     }
3586db04571cSdan   }
3587db04571cSdan   return rc;
3588db04571cSdan }
3589db04571cSdan 
3590db04571cSdan /*
3591296c7658Sdan ** SQL statement pSelect is as generated by the sessionSelectRow() function.
3592296c7658Sdan ** This function binds the primary key values from the change that changeset
3593296c7658Sdan ** iterator pIter points to to the SELECT and attempts to seek to the table
3594296c7658Sdan ** entry. If a row is found, the SELECT statement left pointing at the row
3595296c7658Sdan ** and SQLITE_ROW is returned. Otherwise, if no row is found and no error
3596296c7658Sdan ** has occured, the statement is reset and SQLITE_OK is returned. If an
35977aa469cdSdan ** error occurs, the statement is reset and an SQLite error code is returned.
35987aa469cdSdan **
35997aa469cdSdan ** If this function returns SQLITE_ROW, the caller must eventually reset()
36007aa469cdSdan ** statement pSelect. If any other value is returned, the statement does
36017aa469cdSdan ** not require a reset().
3602296c7658Sdan **
3603296c7658Sdan ** If the iterator currently points to an INSERT record, bind values from the
3604db04571cSdan ** new.* record to the SELECT statement. Or, if it points to a DELETE or
3605db04571cSdan ** UPDATE, bind values from the old.* record.
3606296c7658Sdan */
36070c698471Sdan static int sessionSeekToRow(
360837f133ecSdan   sqlite3 *db,                    /* Database handle */
360937f133ecSdan   sqlite3_changeset_iter *pIter,  /* Changeset iterator */
361037f133ecSdan   u8 *abPK,                       /* Primary key flags array */
36110c698471Sdan   sqlite3_stmt *pSelect           /* SELECT statement from sessionSelectRow() */
361237f133ecSdan ){
36137aa469cdSdan   int rc;                         /* Return code */
3614296c7658Sdan   int nCol;                       /* Number of columns in table */
3615296c7658Sdan   int op;                         /* Changset operation (SQLITE_UPDATE etc.) */
3616296c7658Sdan   const char *zDummy;             /* Unused */
361737f133ecSdan 
3618b4480e94Sdan   sqlite3changeset_op(pIter, &zDummy, &nCol, &op, 0);
36197aa469cdSdan   rc = sessionBindRow(pIter,
3620db04571cSdan       op==SQLITE_INSERT ? sqlite3changeset_new : sqlite3changeset_old,
3621db04571cSdan       nCol, abPK, pSelect
3622db04571cSdan   );
36230c698471Sdan 
36240c698471Sdan   if( rc==SQLITE_OK ){
36250c698471Sdan     rc = sqlite3_step(pSelect);
36260c698471Sdan     if( rc!=SQLITE_ROW ) rc = sqlite3_reset(pSelect);
36270c698471Sdan   }
36280c698471Sdan 
36290c698471Sdan   return rc;
36300c698471Sdan }
36310c698471Sdan 
3632296c7658Sdan /*
3633296c7658Sdan ** Invoke the conflict handler for the change that the changeset iterator
3634296c7658Sdan ** currently points to.
3635296c7658Sdan **
3636296c7658Sdan ** Argument eType must be either CHANGESET_DATA or CHANGESET_CONFLICT.
3637296c7658Sdan ** If argument pbReplace is NULL, then the type of conflict handler invoked
3638296c7658Sdan ** depends solely on eType, as follows:
3639296c7658Sdan **
3640296c7658Sdan **    eType value                 Value passed to xConflict
3641296c7658Sdan **    -------------------------------------------------
3642296c7658Sdan **    CHANGESET_DATA              CHANGESET_NOTFOUND
3643296c7658Sdan **    CHANGESET_CONFLICT          CHANGESET_CONSTRAINT
3644296c7658Sdan **
3645296c7658Sdan ** Or, if pbReplace is not NULL, then an attempt is made to find an existing
3646296c7658Sdan ** record with the same primary key as the record about to be deleted, updated
3647296c7658Sdan ** or inserted. If such a record can be found, it is available to the conflict
3648296c7658Sdan ** handler as the "conflicting" record. In this case the type of conflict
3649296c7658Sdan ** handler invoked is as follows:
3650296c7658Sdan **
3651296c7658Sdan **    eType value         PK Record found?   Value passed to xConflict
3652296c7658Sdan **    ----------------------------------------------------------------
3653296c7658Sdan **    CHANGESET_DATA      Yes                CHANGESET_DATA
3654296c7658Sdan **    CHANGESET_DATA      No                 CHANGESET_NOTFOUND
3655296c7658Sdan **    CHANGESET_CONFLICT  Yes                CHANGESET_CONFLICT
3656296c7658Sdan **    CHANGESET_CONFLICT  No                 CHANGESET_CONSTRAINT
3657296c7658Sdan **
3658296c7658Sdan ** If pbReplace is not NULL, and a record with a matching PK is found, and
3659296c7658Sdan ** the conflict handler function returns SQLITE_CHANGESET_REPLACE, *pbReplace
3660296c7658Sdan ** is set to non-zero before returning SQLITE_OK.
3661296c7658Sdan **
3662296c7658Sdan ** If the conflict handler returns SQLITE_CHANGESET_ABORT, SQLITE_ABORT is
3663296c7658Sdan ** returned. Or, if the conflict handler returns an invalid value,
3664296c7658Sdan ** SQLITE_MISUSE. If the conflict handler returns SQLITE_CHANGESET_OMIT,
3665296c7658Sdan ** this function returns SQLITE_OK.
3666296c7658Sdan */
36670c698471Sdan static int sessionConflictHandler(
3668296c7658Sdan   int eType,                      /* Either CHANGESET_DATA or CONFLICT */
3669296c7658Sdan   SessionApplyCtx *p,             /* changeset_apply() context */
36700c698471Sdan   sqlite3_changeset_iter *pIter,  /* Changeset iterator */
36710c698471Sdan   int(*xConflict)(void *, int, sqlite3_changeset_iter*),
3672296c7658Sdan   void *pCtx,                     /* First argument for conflict handler */
3673296c7658Sdan   int *pbReplace                  /* OUT: Set to true if PK row is found */
36740c698471Sdan ){
367574f598b6Smistachkin   int res = 0;                    /* Value returned by conflict handler */
36760c698471Sdan   int rc;
36770c698471Sdan   int nCol;
36780c698471Sdan   int op;
36790c698471Sdan   const char *zDummy;
36800c698471Sdan 
3681b4480e94Sdan   sqlite3changeset_op(pIter, &zDummy, &nCol, &op, 0);
36820c698471Sdan 
36830c698471Sdan   assert( eType==SQLITE_CHANGESET_CONFLICT || eType==SQLITE_CHANGESET_DATA );
36840c698471Sdan   assert( SQLITE_CHANGESET_CONFLICT+1==SQLITE_CHANGESET_CONSTRAINT );
36850c698471Sdan   assert( SQLITE_CHANGESET_DATA+1==SQLITE_CHANGESET_NOTFOUND );
368637f133ecSdan 
368737f133ecSdan   /* Bind the new.* PRIMARY KEY values to the SELECT statement. */
36880c698471Sdan   if( pbReplace ){
36890c698471Sdan     rc = sessionSeekToRow(p->db, pIter, p->abPK, p->pSelect);
36900c698471Sdan   }else{
3691db04571cSdan     rc = SQLITE_OK;
36920c698471Sdan   }
36930c698471Sdan 
36940c698471Sdan   if( rc==SQLITE_ROW ){
36950c698471Sdan     /* There exists another row with the new.* primary key. */
36960c698471Sdan     pIter->pConflict = p->pSelect;
36970c698471Sdan     res = xConflict(pCtx, eType, pIter);
36980c698471Sdan     pIter->pConflict = 0;
36990c698471Sdan     rc = sqlite3_reset(p->pSelect);
3700db04571cSdan   }else if( rc==SQLITE_OK ){
3701d9151526Sdan     if( p->bDeferConstraints && eType==SQLITE_CHANGESET_CONFLICT ){
3702d9151526Sdan       /* Instead of invoking the conflict handler, append the change blob
3703d9151526Sdan       ** to the SessionApplyCtx.constraints buffer. */
3704d9151526Sdan       u8 *aBlob = &pIter->in.aData[pIter->in.iCurrent];
3705d9151526Sdan       int nBlob = pIter->in.iNext - pIter->in.iCurrent;
3706d9151526Sdan       sessionAppendBlob(&p->constraints, aBlob, nBlob, &rc);
3707d9151526Sdan       res = SQLITE_CHANGESET_OMIT;
3708d9151526Sdan     }else{
37090c698471Sdan       /* No other row with the new.* primary key. */
37100c698471Sdan       res = xConflict(pCtx, eType+1, pIter);
37110c698471Sdan       if( res==SQLITE_CHANGESET_REPLACE ) rc = SQLITE_MISUSE;
371237f133ecSdan     }
3713d9151526Sdan   }
371437f133ecSdan 
371537f133ecSdan   if( rc==SQLITE_OK ){
37160c698471Sdan     switch( res ){
37170c698471Sdan       case SQLITE_CHANGESET_REPLACE:
3718f51e5f6cSdan         assert( pbReplace );
3719f51e5f6cSdan         *pbReplace = 1;
37200c698471Sdan         break;
37210c698471Sdan 
37220c698471Sdan       case SQLITE_CHANGESET_OMIT:
37230c698471Sdan         break;
37240c698471Sdan 
37250c698471Sdan       case SQLITE_CHANGESET_ABORT:
37260c698471Sdan         rc = SQLITE_ABORT;
37270c698471Sdan         break;
37280c698471Sdan 
37290c698471Sdan       default:
37300c698471Sdan         rc = SQLITE_MISUSE;
37310c698471Sdan         break;
37320c698471Sdan     }
37330c698471Sdan   }
37340c698471Sdan 
37350c698471Sdan   return rc;
37360c698471Sdan }
37370c698471Sdan 
3738296c7658Sdan /*
3739296c7658Sdan ** Attempt to apply the change that the iterator passed as the first argument
3740296c7658Sdan ** currently points to to the database. If a conflict is encountered, invoke
3741296c7658Sdan ** the conflict handler callback.
3742296c7658Sdan **
3743296c7658Sdan ** If argument pbRetry is NULL, then ignore any CHANGESET_DATA conflict. If
3744296c7658Sdan ** one is encountered, update or delete the row with the matching primary key
3745296c7658Sdan ** instead. Or, if pbRetry is not NULL and a CHANGESET_DATA conflict occurs,
3746296c7658Sdan ** invoke the conflict handler. If it returns CHANGESET_REPLACE, set *pbRetry
3747296c7658Sdan ** to true before returning. In this case the caller will invoke this function
3748296c7658Sdan ** again, this time with pbRetry set to NULL.
3749296c7658Sdan **
3750296c7658Sdan ** If argument pbReplace is NULL and a CHANGESET_CONFLICT conflict is
3751296c7658Sdan ** encountered invoke the conflict handler with CHANGESET_CONSTRAINT instead.
3752296c7658Sdan ** Or, if pbReplace is not NULL, invoke it with CHANGESET_CONFLICT. If such
3753296c7658Sdan ** an invocation returns SQLITE_CHANGESET_REPLACE, set *pbReplace to true
3754296c7658Sdan ** before retrying. In this case the caller attempts to remove the conflicting
3755296c7658Sdan ** row before invoking this function again, this time with pbReplace set
3756296c7658Sdan ** to NULL.
3757296c7658Sdan **
3758296c7658Sdan ** If any conflict handler returns SQLITE_CHANGESET_ABORT, this function
3759296c7658Sdan ** returns SQLITE_ABORT. Otherwise, if no error occurs, SQLITE_OK is
3760296c7658Sdan ** returned.
3761296c7658Sdan */
37620c698471Sdan static int sessionApplyOneOp(
3763296c7658Sdan   sqlite3_changeset_iter *pIter,  /* Changeset iterator */
3764296c7658Sdan   SessionApplyCtx *p,             /* changeset_apply() context */
37650c698471Sdan   int(*xConflict)(void *, int, sqlite3_changeset_iter *),
3766296c7658Sdan   void *pCtx,                     /* First argument for the conflict handler */
3767296c7658Sdan   int *pbReplace,                 /* OUT: True to remove PK row and retry */
3768296c7658Sdan   int *pbRetry                    /* OUT: True to retry. */
37690c698471Sdan ){
37700c698471Sdan   const char *zDummy;
37710c698471Sdan   int op;
37720c698471Sdan   int nCol;
37730c698471Sdan   int rc = SQLITE_OK;
37740c698471Sdan 
37750c698471Sdan   assert( p->pDelete && p->pUpdate && p->pInsert && p->pSelect );
37760c698471Sdan   assert( p->azCol && p->abPK );
37770c698471Sdan   assert( !pbReplace || *pbReplace==0 );
37780c698471Sdan 
3779b4480e94Sdan   sqlite3changeset_op(pIter, &zDummy, &nCol, &op, 0);
37800c698471Sdan 
37810c698471Sdan   if( op==SQLITE_DELETE ){
37820c698471Sdan 
378373b3c055Sdan     /* Bind values to the DELETE statement. If conflict handling is required,
378473b3c055Sdan     ** bind values for all columns and set bound variable (nCol+1) to true.
378573b3c055Sdan     ** Or, if conflict handling is not required, bind just the PK column
378673b3c055Sdan     ** values and, if it exists, set (nCol+1) to false. Conflict handling
378773b3c055Sdan     ** is not required if:
378873b3c055Sdan     **
378973b3c055Sdan     **   * this is a patchset, or
379073b3c055Sdan     **   * (pbRetry==0), or
379173b3c055Sdan     **   * all columns of the table are PK columns (in this case there is
379273b3c055Sdan     **     no (nCol+1) variable to bind to).
379373b3c055Sdan     */
379473b3c055Sdan     u8 *abPK = (pIter->bPatchset ? p->abPK : 0);
379573b3c055Sdan     rc = sessionBindRow(pIter, sqlite3changeset_old, nCol, abPK, p->pDelete);
37967cf7df7dSdan     if( rc==SQLITE_OK && sqlite3_bind_parameter_count(p->pDelete)>nCol ){
379773b3c055Sdan       rc = sqlite3_bind_int(p->pDelete, nCol+1, (pbRetry==0 || abPK));
37987cf7df7dSdan     }
37990c698471Sdan     if( rc!=SQLITE_OK ) return rc;
38000c698471Sdan 
38010c698471Sdan     sqlite3_step(p->pDelete);
38020c698471Sdan     rc = sqlite3_reset(p->pDelete);
38030c698471Sdan     if( rc==SQLITE_OK && sqlite3_changes(p->db)==0 ){
38040c698471Sdan       rc = sessionConflictHandler(
38050c698471Sdan           SQLITE_CHANGESET_DATA, p, pIter, xConflict, pCtx, pbRetry
38060c698471Sdan       );
380735e2858eSdan     }else if( (rc&0xff)==SQLITE_CONSTRAINT ){
38080c698471Sdan       rc = sessionConflictHandler(
38090c698471Sdan           SQLITE_CHANGESET_CONFLICT, p, pIter, xConflict, pCtx, 0
38100c698471Sdan       );
38110c698471Sdan     }
38120c698471Sdan 
38130c698471Sdan   }else if( op==SQLITE_UPDATE ){
38140c698471Sdan     int i;
38150c698471Sdan 
38160c698471Sdan     /* Bind values to the UPDATE statement. */
38170c698471Sdan     for(i=0; rc==SQLITE_OK && i<nCol; i++){
38187aa469cdSdan       sqlite3_value *pOld = sessionChangesetOld(pIter, i);
38197aa469cdSdan       sqlite3_value *pNew = sessionChangesetNew(pIter, i);
38207aa469cdSdan 
38210c698471Sdan       sqlite3_bind_int(p->pUpdate, i*3+2, !!pNew);
38227aa469cdSdan       if( pOld ){
38237aa469cdSdan         rc = sessionBindValue(p->pUpdate, i*3+1, pOld);
38247aa469cdSdan       }
38257aa469cdSdan       if( rc==SQLITE_OK && pNew ){
38267aa469cdSdan         rc = sessionBindValue(p->pUpdate, i*3+3, pNew);
38270c698471Sdan       }
38280c698471Sdan     }
382973b3c055Sdan     if( rc==SQLITE_OK ){
383073b3c055Sdan       sqlite3_bind_int(p->pUpdate, nCol*3+1, pbRetry==0 || pIter->bPatchset);
383173b3c055Sdan     }
38320c698471Sdan     if( rc!=SQLITE_OK ) return rc;
38330c698471Sdan 
38340c698471Sdan     /* Attempt the UPDATE. In the case of a NOTFOUND or DATA conflict,
38350c698471Sdan     ** the result will be SQLITE_OK with 0 rows modified. */
38360c698471Sdan     sqlite3_step(p->pUpdate);
38370c698471Sdan     rc = sqlite3_reset(p->pUpdate);
38380c698471Sdan 
38390c698471Sdan     if( rc==SQLITE_OK && sqlite3_changes(p->db)==0 ){
38400c698471Sdan       /* A NOTFOUND or DATA error. Search the table to see if it contains
38410c698471Sdan       ** a row with a matching primary key. If so, this is a DATA conflict.
38420c698471Sdan       ** Otherwise, if there is no primary key match, it is a NOTFOUND. */
38430c698471Sdan 
38440c698471Sdan       rc = sessionConflictHandler(
38450c698471Sdan           SQLITE_CHANGESET_DATA, p, pIter, xConflict, pCtx, pbRetry
38460c698471Sdan       );
38470c698471Sdan 
384835e2858eSdan     }else if( (rc&0xff)==SQLITE_CONSTRAINT ){
3849db04571cSdan       /* This is always a CONSTRAINT conflict. */
3850db04571cSdan       rc = sessionConflictHandler(
3851db04571cSdan           SQLITE_CHANGESET_CONFLICT, p, pIter, xConflict, pCtx, 0
38520c698471Sdan       );
38530c698471Sdan     }
38540c698471Sdan 
38550c698471Sdan   }else{
38560c698471Sdan     assert( op==SQLITE_INSERT );
38577aa469cdSdan     rc = sessionBindRow(pIter, sqlite3changeset_new, nCol, 0, p->pInsert);
38580c698471Sdan     if( rc!=SQLITE_OK ) return rc;
38590c698471Sdan 
38600c698471Sdan     sqlite3_step(p->pInsert);
38610c698471Sdan     rc = sqlite3_reset(p->pInsert);
386235e2858eSdan     if( (rc&0xff)==SQLITE_CONSTRAINT ){
38630c698471Sdan       rc = sessionConflictHandler(
38640c698471Sdan           SQLITE_CHANGESET_CONFLICT, p, pIter, xConflict, pCtx, pbReplace
38650c698471Sdan       );
386637f133ecSdan     }
386737f133ecSdan   }
386837f133ecSdan 
386937f133ecSdan   return rc;
387037f133ecSdan }
387137f133ecSdan 
38725f5663dcSdan /*
38735f5663dcSdan ** Attempt to apply the change that the iterator passed as the first argument
38745f5663dcSdan ** currently points to to the database. If a conflict is encountered, invoke
38755f5663dcSdan ** the conflict handler callback.
38765f5663dcSdan **
38775f5663dcSdan ** The difference between this function and sessionApplyOne() is that this
38785f5663dcSdan ** function handles the case where the conflict-handler is invoked and
38795f5663dcSdan ** returns SQLITE_CHANGESET_REPLACE - indicating that the change should be
38805f5663dcSdan ** retried in some manner.
38815f5663dcSdan */
3882d9151526Sdan static int sessionApplyOneWithRetry(
3883d9151526Sdan   sqlite3 *db,                    /* Apply change to "main" db of this handle */
3884d9151526Sdan   sqlite3_changeset_iter *pIter,  /* Changeset iterator to read change from */
3885d9151526Sdan   SessionApplyCtx *pApply,        /* Apply context */
3886d9151526Sdan   int(*xConflict)(void*, int, sqlite3_changeset_iter*),
3887d9151526Sdan   void *pCtx                      /* First argument passed to xConflict */
3888d9151526Sdan ){
3889d9151526Sdan   int bReplace = 0;
3890d9151526Sdan   int bRetry = 0;
3891d9151526Sdan   int rc;
3892d9151526Sdan 
3893d9151526Sdan   rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, &bReplace, &bRetry);
38945f5663dcSdan   assert( rc==SQLITE_OK || (bRetry==0 && bReplace==0) );
3895d9151526Sdan 
38965f5663dcSdan   /* If the bRetry flag is set, the change has not been applied due to an
38975f5663dcSdan   ** SQLITE_CHANGESET_DATA problem (i.e. this is an UPDATE or DELETE and
38985f5663dcSdan   ** a row with the correct PK is present in the db, but one or more other
38995f5663dcSdan   ** fields do not contain the expected values) and the conflict handler
39005f5663dcSdan   ** returned SQLITE_CHANGESET_REPLACE. In this case retry the operation,
39015f5663dcSdan   ** but pass NULL as the final argument so that sessionApplyOneOp() ignores
39025f5663dcSdan   ** the SQLITE_CHANGESET_DATA problem.  */
39035f5663dcSdan   if( bRetry ){
39045f5663dcSdan     assert( pIter->op==SQLITE_UPDATE || pIter->op==SQLITE_DELETE );
39055f5663dcSdan     rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, 0, 0);
3906d9151526Sdan   }
3907d9151526Sdan 
39085f5663dcSdan   /* If the bReplace flag is set, the change is an INSERT that has not
39095f5663dcSdan   ** been performed because the database already contains a row with the
39105f5663dcSdan   ** specified primary key and the conflict handler returned
39115f5663dcSdan   ** SQLITE_CHANGESET_REPLACE. In this case remove the conflicting row
39125f5663dcSdan   ** before reattempting the INSERT.  */
39135f5663dcSdan   else if( bReplace ){
3914d9151526Sdan     assert( pIter->op==SQLITE_INSERT );
3915d9151526Sdan     rc = sqlite3_exec(db, "SAVEPOINT replace_op", 0, 0, 0);
3916d9151526Sdan     if( rc==SQLITE_OK ){
3917d9151526Sdan       rc = sessionBindRow(pIter,
3918d9151526Sdan           sqlite3changeset_new, pApply->nCol, pApply->abPK, pApply->pDelete);
3919d9151526Sdan       sqlite3_bind_int(pApply->pDelete, pApply->nCol+1, 1);
3920d9151526Sdan     }
3921d9151526Sdan     if( rc==SQLITE_OK ){
3922d9151526Sdan       sqlite3_step(pApply->pDelete);
3923d9151526Sdan       rc = sqlite3_reset(pApply->pDelete);
3924d9151526Sdan     }
3925d9151526Sdan     if( rc==SQLITE_OK ){
3926d9151526Sdan       rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, 0, 0);
3927d9151526Sdan     }
3928d9151526Sdan     if( rc==SQLITE_OK ){
3929d9151526Sdan       rc = sqlite3_exec(db, "RELEASE replace_op", 0, 0, 0);
3930d9151526Sdan     }
3931d9151526Sdan   }
3932d9151526Sdan 
3933d9151526Sdan   return rc;
3934d9151526Sdan }
3935d9151526Sdan 
3936d9151526Sdan /*
3937d9151526Sdan ** Retry the changes accumulated in the pApply->constraints buffer.
3938d9151526Sdan */
3939d9151526Sdan static int sessionRetryConstraints(
3940d9151526Sdan   sqlite3 *db,
3941d9151526Sdan   int bPatchset,
3942d9151526Sdan   const char *zTab,
3943d9151526Sdan   SessionApplyCtx *pApply,
3944d9151526Sdan   int(*xConflict)(void*, int, sqlite3_changeset_iter*),
3945d9151526Sdan   void *pCtx                      /* First argument passed to xConflict */
3946d9151526Sdan ){
3947d9151526Sdan   int rc = SQLITE_OK;
3948d9151526Sdan 
3949d9151526Sdan   while( pApply->constraints.nBuf ){
3950d9151526Sdan     sqlite3_changeset_iter *pIter2 = 0;
3951d9151526Sdan     SessionBuffer cons = pApply->constraints;
3952d9151526Sdan     memset(&pApply->constraints, 0, sizeof(SessionBuffer));
3953d9151526Sdan 
3954d9151526Sdan     rc = sessionChangesetStart(&pIter2, 0, 0, cons.nBuf, cons.aBuf);
3955d9151526Sdan     if( rc==SQLITE_OK ){
3956d9151526Sdan       int nByte = 2*pApply->nCol*sizeof(sqlite3_value*);
3957d9151526Sdan       int rc2;
3958d9151526Sdan       pIter2->bPatchset = bPatchset;
3959d9151526Sdan       pIter2->zTab = (char*)zTab;
3960d9151526Sdan       pIter2->nCol = pApply->nCol;
3961d9151526Sdan       pIter2->abPK = pApply->abPK;
3962d9151526Sdan       sessionBufferGrow(&pIter2->tblhdr, nByte, &rc);
3963d9151526Sdan       pIter2->apValue = (sqlite3_value**)pIter2->tblhdr.aBuf;
3964d9151526Sdan       if( rc==SQLITE_OK ) memset(pIter2->apValue, 0, nByte);
3965d9151526Sdan 
3966d9151526Sdan       while( rc==SQLITE_OK && SQLITE_ROW==sqlite3changeset_next(pIter2) ){
3967d9151526Sdan         rc = sessionApplyOneWithRetry(db, pIter2, pApply, xConflict, pCtx);
3968d9151526Sdan       }
3969d9151526Sdan 
3970d9151526Sdan       rc2 = sqlite3changeset_finalize(pIter2);
39717e0765a9Sdrh       if( rc==SQLITE_OK ) rc = rc2;
3972d9151526Sdan     }
3973d9151526Sdan     assert( pApply->bDeferConstraints || pApply->constraints.nBuf==0 );
3974d9151526Sdan 
3975d9151526Sdan     sqlite3_free(cons.aBuf);
3976d9151526Sdan     if( rc!=SQLITE_OK ) break;
3977d9151526Sdan     if( pApply->constraints.nBuf>=cons.nBuf ){
3978d9151526Sdan       /* No progress was made on the last round. */
3979d9151526Sdan       pApply->bDeferConstraints = 0;
3980d9151526Sdan     }
3981d9151526Sdan   }
3982d9151526Sdan 
3983d9151526Sdan   return rc;
3984d9151526Sdan }
3985d9151526Sdan 
3986296c7658Sdan /*
39874757c658Sdan ** Argument pIter is a changeset iterator that has been initialized, but
39884757c658Sdan ** not yet passed to sqlite3changeset_next(). This function applies the
39894757c658Sdan ** changeset to the main database attached to handle "db". The supplied
39904757c658Sdan ** conflict handler callback is invoked to resolve any conflicts encountered
39914757c658Sdan ** while applying the change.
3992296c7658Sdan */
39934757c658Sdan static int sessionChangesetApply(
3994296c7658Sdan   sqlite3 *db,                    /* Apply change to "main" db of this handle */
39954757c658Sdan   sqlite3_changeset_iter *pIter,  /* Changeset to apply */
399640368988Sdan   int(*xFilter)(
399740368988Sdan     void *pCtx,                   /* Copy of sixth arg to _apply() */
399840368988Sdan     const char *zTab              /* Table name */
399940368988Sdan   ),
4000d5f0767cSdan   int(*xConflict)(
4001d5f0767cSdan     void *pCtx,                   /* Copy of fifth arg to _apply() */
4002d5f0767cSdan     int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
4003d5f0767cSdan     sqlite3_changeset_iter *p     /* Handle describing change and conflict */
4004d5f0767cSdan   ),
4005296c7658Sdan   void *pCtx                      /* First argument passed to xConflict */
4006d5f0767cSdan ){
4007ca62ad57Sdan   int schemaMismatch = 0;
4008296c7658Sdan   int rc;                         /* Return code */
4009d5f0767cSdan   const char *zTab = 0;           /* Name of current table */
4010cfdbde21Sdrh   int nTab = 0;                   /* Result of sqlite3Strlen30(zTab) */
4011296c7658Sdan   SessionApplyCtx sApply;         /* changeset_apply() context object */
40125f5663dcSdan   int bPatchset;
4013d5f0767cSdan 
4014082c96dfSdan   assert( xConflict!=0 );
4015082c96dfSdan 
4016d9151526Sdan   pIter->in.bNoDiscard = 1;
40170c698471Sdan   memset(&sApply, 0, sizeof(sApply));
40184c220252Sdan   sqlite3_mutex_enter(sqlite3_db_mutex(db));
40190c698471Sdan   rc = sqlite3_exec(db, "SAVEPOINT changeset_apply", 0, 0, 0);
4020cb3e4b79Sdan   if( rc==SQLITE_OK ){
4021cb3e4b79Sdan     rc = sqlite3_exec(db, "PRAGMA defer_foreign_keys = 1", 0, 0, 0);
4022cb3e4b79Sdan   }
40230c698471Sdan   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3changeset_next(pIter) ){
40240c698471Sdan     int nCol;
4025d5f0767cSdan     int op;
40260c698471Sdan     const char *zNew;
4027ca62ad57Sdan 
4028b4480e94Sdan     sqlite3changeset_op(pIter, &zNew, &nCol, &op, 0);
4029d5f0767cSdan 
40300c698471Sdan     if( zTab==0 || sqlite3_strnicmp(zNew, zTab, nTab+1) ){
4031ca62ad57Sdan       u8 *abPK;
4032ca62ad57Sdan 
4033d9151526Sdan       rc = sessionRetryConstraints(
4034d9151526Sdan           db, pIter->bPatchset, zTab, &sApply, xConflict, pCtx
4035d9151526Sdan       );
4036d9151526Sdan       if( rc!=SQLITE_OK ) break;
4037d9151526Sdan 
4038cfdbde21Sdrh       sqlite3_free((char*)sApply.azCol);  /* cast works around VC++ bug */
40390c698471Sdan       sqlite3_finalize(sApply.pDelete);
40400c698471Sdan       sqlite3_finalize(sApply.pUpdate);
40410c698471Sdan       sqlite3_finalize(sApply.pInsert);
40420c698471Sdan       sqlite3_finalize(sApply.pSelect);
40430c698471Sdan       memset(&sApply, 0, sizeof(sApply));
40440c698471Sdan       sApply.db = db;
4045d9151526Sdan       sApply.bDeferConstraints = 1;
404637f133ecSdan 
404740368988Sdan       /* If an xFilter() callback was specified, invoke it now. If the
404840368988Sdan       ** xFilter callback returns zero, skip this table. If it returns
404940368988Sdan       ** non-zero, proceed. */
405040368988Sdan       schemaMismatch = (xFilter && (0==xFilter(pCtx, zNew)));
405140368988Sdan       if( schemaMismatch ){
405240368988Sdan         zTab = sqlite3_mprintf("%s", zNew);
4053f05ac112Sdan         if( zTab==0 ){
4054f05ac112Sdan           rc = SQLITE_NOMEM;
4055f05ac112Sdan           break;
4056f05ac112Sdan         }
40574f528042Sdan         nTab = (int)strlen(zTab);
405840368988Sdan         sApply.azCol = (const char **)zTab;
405940368988Sdan       }else{
4060ff677b20Sdan         int nMinCol = 0;
4061ff677b20Sdan         int i;
4062ff677b20Sdan 
4063ca62ad57Sdan         sqlite3changeset_pk(pIter, &abPK, 0);
4064296c7658Sdan         rc = sessionTableInfo(
4065ca62ad57Sdan             db, "main", zNew, &sApply.nCol, &zTab, &sApply.azCol, &sApply.abPK
4066ca62ad57Sdan         );
4067ca62ad57Sdan         if( rc!=SQLITE_OK ) break;
4068ff677b20Sdan         for(i=0; i<sApply.nCol; i++){
4069ff677b20Sdan           if( sApply.abPK[i] ) nMinCol = i+1;
4070ff677b20Sdan         }
40710c698471Sdan 
4072ca62ad57Sdan         if( sApply.nCol==0 ){
4073ca62ad57Sdan           schemaMismatch = 1;
4074ca62ad57Sdan           sqlite3_log(SQLITE_SCHEMA,
4075ca62ad57Sdan               "sqlite3changeset_apply(): no such table: %s", zTab
4076ca62ad57Sdan           );
4077ca62ad57Sdan         }
4078ff677b20Sdan         else if( sApply.nCol<nCol ){
4079ca62ad57Sdan           schemaMismatch = 1;
4080ca62ad57Sdan           sqlite3_log(SQLITE_SCHEMA,
4081ff677b20Sdan               "sqlite3changeset_apply(): table %s has %d columns, "
4082ff677b20Sdan               "expected %d or more",
4083ca62ad57Sdan               zTab, sApply.nCol, nCol
4084ca62ad57Sdan           );
4085ca62ad57Sdan         }
4086ff677b20Sdan         else if( nCol<nMinCol || memcmp(sApply.abPK, abPK, nCol)!=0 ){
4087ca62ad57Sdan           schemaMismatch = 1;
408840368988Sdan           sqlite3_log(SQLITE_SCHEMA, "sqlite3changeset_apply(): "
408940368988Sdan               "primary key mismatch for table %s", zTab
4090ca62ad57Sdan           );
4091ca62ad57Sdan         }
4092ff677b20Sdan         else{
4093ff677b20Sdan           sApply.nCol = nCol;
4094ff677b20Sdan           if((rc = sessionSelectRow(db, zTab, &sApply))
40950c698471Sdan           || (rc = sessionUpdateRow(db, zTab, &sApply))
40960c698471Sdan           || (rc = sessionDeleteRow(db, zTab, &sApply))
40970c698471Sdan           || (rc = sessionInsertRow(db, zTab, &sApply))
409837f133ecSdan           ){
409937f133ecSdan             break;
410037f133ecSdan           }
4101ff677b20Sdan         }
4102cfdbde21Sdrh         nTab = sqlite3Strlen30(zTab);
4103d5f0767cSdan       }
410440368988Sdan     }
4105d5f0767cSdan 
4106ca62ad57Sdan     /* If there is a schema mismatch on the current table, proceed to the
4107ca62ad57Sdan     ** next change. A log message has already been issued. */
4108ca62ad57Sdan     if( schemaMismatch ) continue;
4109ca62ad57Sdan 
4110d9151526Sdan     rc = sessionApplyOneWithRetry(db, pIter, &sApply, xConflict, pCtx);
41110c698471Sdan   }
41120c698471Sdan 
41135f5663dcSdan   bPatchset = pIter->bPatchset;
4114296c7658Sdan   if( rc==SQLITE_OK ){
4115296c7658Sdan     rc = sqlite3changeset_finalize(pIter);
4116296c7658Sdan   }else{
4117296c7658Sdan     sqlite3changeset_finalize(pIter);
4118296c7658Sdan   }
4119d5f0767cSdan 
4120d5f0767cSdan   if( rc==SQLITE_OK ){
41215f5663dcSdan     rc = sessionRetryConstraints(db, bPatchset, zTab, &sApply, xConflict, pCtx);
41225f5663dcSdan   }
41235f5663dcSdan 
41245f5663dcSdan   if( rc==SQLITE_OK ){
412507001c45Sdrh     int nFk, notUsed;
412607001c45Sdrh     sqlite3_db_status(db, SQLITE_DBSTATUS_DEFERRED_FKS, &nFk, &notUsed, 0);
412707001c45Sdrh     if( nFk!=0 ){
4128cb3e4b79Sdan       int res = SQLITE_CHANGESET_ABORT;
4129cb3e4b79Sdan       sqlite3_changeset_iter sIter;
4130cb3e4b79Sdan       memset(&sIter, 0, sizeof(sIter));
4131cb3e4b79Sdan       sIter.nCol = nFk;
4132cb3e4b79Sdan       res = xConflict(pCtx, SQLITE_CHANGESET_FOREIGN_KEY, &sIter);
4133cb3e4b79Sdan       if( res!=SQLITE_CHANGESET_OMIT ){
4134cb3e4b79Sdan         rc = SQLITE_CONSTRAINT;
4135cb3e4b79Sdan       }
4136cb3e4b79Sdan     }
4137cb3e4b79Sdan   }
4138cb3e4b79Sdan   sqlite3_exec(db, "PRAGMA defer_foreign_keys = 0", 0, 0, 0);
4139cb3e4b79Sdan 
4140cb3e4b79Sdan   if( rc==SQLITE_OK ){
4141d5f0767cSdan     rc = sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0);
4142d5f0767cSdan   }else{
4143d5f0767cSdan     sqlite3_exec(db, "ROLLBACK TO changeset_apply", 0, 0, 0);
4144d5f0767cSdan     sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0);
4145d5f0767cSdan   }
4146d5f0767cSdan 
41470c698471Sdan   sqlite3_finalize(sApply.pInsert);
41480c698471Sdan   sqlite3_finalize(sApply.pDelete);
41490c698471Sdan   sqlite3_finalize(sApply.pUpdate);
41500c698471Sdan   sqlite3_finalize(sApply.pSelect);
4151cfdbde21Sdrh   sqlite3_free((char*)sApply.azCol);  /* cast works around VC++ bug */
4152d9151526Sdan   sqlite3_free((char*)sApply.constraints.aBuf);
41534c220252Sdan   sqlite3_mutex_leave(sqlite3_db_mutex(db));
4154d5f0767cSdan   return rc;
4155d5f0767cSdan }
415691ddd559Sdan 
415777fc1d5bSdan /*
41584757c658Sdan ** Apply the changeset passed via pChangeset/nChangeset to the main database
41594757c658Sdan ** attached to handle "db". Invoke the supplied conflict handler callback
41604757c658Sdan ** to resolve any conflicts encountered while applying the change.
41614757c658Sdan */
41624757c658Sdan int sqlite3changeset_apply(
41634757c658Sdan   sqlite3 *db,                    /* Apply change to "main" db of this handle */
41644757c658Sdan   int nChangeset,                 /* Size of changeset in bytes */
41654757c658Sdan   void *pChangeset,               /* Changeset blob */
41664757c658Sdan   int(*xFilter)(
41674757c658Sdan     void *pCtx,                   /* Copy of sixth arg to _apply() */
41684757c658Sdan     const char *zTab              /* Table name */
41694757c658Sdan   ),
41704757c658Sdan   int(*xConflict)(
41714757c658Sdan     void *pCtx,                   /* Copy of fifth arg to _apply() */
41724757c658Sdan     int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
41734757c658Sdan     sqlite3_changeset_iter *p     /* Handle describing change and conflict */
41744757c658Sdan   ),
41754757c658Sdan   void *pCtx                      /* First argument passed to xConflict */
41764757c658Sdan ){
41774757c658Sdan   sqlite3_changeset_iter *pIter;  /* Iterator to skip through changeset */
41784757c658Sdan   int rc = sqlite3changeset_start(&pIter, nChangeset, pChangeset);
41794757c658Sdan   if( rc==SQLITE_OK ){
41804757c658Sdan     rc = sessionChangesetApply(db, pIter, xFilter, xConflict, pCtx);
41814757c658Sdan   }
41824757c658Sdan   return rc;
41834757c658Sdan }
41844757c658Sdan 
41854757c658Sdan /*
41864757c658Sdan ** Apply the changeset passed via xInput/pIn to the main database
41874757c658Sdan ** attached to handle "db". Invoke the supplied conflict handler callback
41884757c658Sdan ** to resolve any conflicts encountered while applying the change.
41894757c658Sdan */
4190f1a08ad8Sdrh int sqlite3changeset_apply_strm(
41914757c658Sdan   sqlite3 *db,                    /* Apply change to "main" db of this handle */
41924757c658Sdan   int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
41934757c658Sdan   void *pIn,                                          /* First arg for xInput */
41944757c658Sdan   int(*xFilter)(
41954757c658Sdan     void *pCtx,                   /* Copy of sixth arg to _apply() */
41964757c658Sdan     const char *zTab              /* Table name */
41974757c658Sdan   ),
41984757c658Sdan   int(*xConflict)(
41994757c658Sdan     void *pCtx,                   /* Copy of sixth arg to _apply() */
42004757c658Sdan     int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
42014757c658Sdan     sqlite3_changeset_iter *p     /* Handle describing change and conflict */
42024757c658Sdan   ),
42034757c658Sdan   void *pCtx                      /* First argument passed to xConflict */
42044757c658Sdan ){
42054757c658Sdan   sqlite3_changeset_iter *pIter;  /* Iterator to skip through changeset */
4206f1a08ad8Sdrh   int rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
42074757c658Sdan   if( rc==SQLITE_OK ){
42084757c658Sdan     rc = sessionChangesetApply(db, pIter, xFilter, xConflict, pCtx);
42094757c658Sdan   }
42104757c658Sdan   return rc;
42114757c658Sdan }
42124757c658Sdan 
42134757c658Sdan /*
42145898ad69Sdan ** sqlite3_changegroup handle.
42155898ad69Sdan */
42165898ad69Sdan struct sqlite3_changegroup {
42175898ad69Sdan   int rc;                         /* Error code */
42185898ad69Sdan   int bPatch;                     /* True to accumulate patchsets */
42195898ad69Sdan   SessionTable *pList;            /* List of tables in current patch */
42205898ad69Sdan };
42215898ad69Sdan 
42225898ad69Sdan /*
422377fc1d5bSdan ** This function is called to merge two changes to the same row together as
422477fc1d5bSdan ** part of an sqlite3changeset_concat() operation. A new change object is
422577fc1d5bSdan ** allocated and a pointer to it stored in *ppNew.
422677fc1d5bSdan */
42275d607a6eSdan static int sessionChangeMerge(
422877fc1d5bSdan   SessionTable *pTab,             /* Table structure */
422964277f4aSdan   int bPatchset,                  /* True for patchsets */
423077fc1d5bSdan   SessionChange *pExist,          /* Existing change */
423177fc1d5bSdan   int op2,                        /* Second change operation */
423277fc1d5bSdan   int bIndirect,                  /* True if second change is indirect */
423377fc1d5bSdan   u8 *aRec,                       /* Second change record */
423477fc1d5bSdan   int nRec,                       /* Number of bytes in aRec */
423577fc1d5bSdan   SessionChange **ppNew           /* OUT: Merged change */
42365d607a6eSdan ){
42375d607a6eSdan   SessionChange *pNew = 0;
42385d607a6eSdan 
42395d607a6eSdan   if( !pExist ){
4240cbf6d2d2Sdan     pNew = (SessionChange *)sqlite3_malloc(sizeof(SessionChange) + nRec);
42415d607a6eSdan     if( !pNew ){
42425d607a6eSdan       return SQLITE_NOMEM;
42435d607a6eSdan     }
42445d607a6eSdan     memset(pNew, 0, sizeof(SessionChange));
4245798693b2Sdan     pNew->op = op2;
42465d607a6eSdan     pNew->bIndirect = bIndirect;
42475d607a6eSdan     pNew->nRecord = nRec;
4248cbf6d2d2Sdan     pNew->aRecord = (u8*)&pNew[1];
4249cbf6d2d2Sdan     memcpy(pNew->aRecord, aRec, nRec);
42505d607a6eSdan   }else{
4251798693b2Sdan     int op1 = pExist->op;
42525d607a6eSdan 
42535d607a6eSdan     /*
42545d607a6eSdan     **   op1=INSERT, op2=INSERT      ->      Unsupported. Discard op2.
42555d607a6eSdan     **   op1=INSERT, op2=UPDATE      ->      INSERT.
42565d607a6eSdan     **   op1=INSERT, op2=DELETE      ->      (none)
42575d607a6eSdan     **
42585d607a6eSdan     **   op1=UPDATE, op2=INSERT      ->      Unsupported. Discard op2.
42595d607a6eSdan     **   op1=UPDATE, op2=UPDATE      ->      UPDATE.
42605d607a6eSdan     **   op1=UPDATE, op2=DELETE      ->      DELETE.
42615d607a6eSdan     **
42625d607a6eSdan     **   op1=DELETE, op2=INSERT      ->      UPDATE.
42635d607a6eSdan     **   op1=DELETE, op2=UPDATE      ->      Unsupported. Discard op2.
42645d607a6eSdan     **   op1=DELETE, op2=DELETE      ->      Unsupported. Discard op2.
42655d607a6eSdan     */
42665d607a6eSdan     if( (op1==SQLITE_INSERT && op2==SQLITE_INSERT)
42675d607a6eSdan      || (op1==SQLITE_UPDATE && op2==SQLITE_INSERT)
42685d607a6eSdan      || (op1==SQLITE_DELETE && op2==SQLITE_UPDATE)
42695d607a6eSdan      || (op1==SQLITE_DELETE && op2==SQLITE_DELETE)
42705d607a6eSdan     ){
42715d607a6eSdan       pNew = pExist;
42725d607a6eSdan     }else if( op1==SQLITE_INSERT && op2==SQLITE_DELETE ){
42735d607a6eSdan       sqlite3_free(pExist);
42745d607a6eSdan       assert( pNew==0 );
42755d607a6eSdan     }else{
427664277f4aSdan       u8 *aExist = pExist->aRecord;
42775d607a6eSdan       int nByte;
42785d607a6eSdan       u8 *aCsr;
42795d607a6eSdan 
428064277f4aSdan       /* Allocate a new SessionChange object. Ensure that the aRecord[]
428164277f4aSdan       ** buffer of the new object is large enough to hold any record that
428264277f4aSdan       ** may be generated by combining the input records.  */
42835d607a6eSdan       nByte = sizeof(SessionChange) + pExist->nRecord + nRec;
42845d607a6eSdan       pNew = (SessionChange *)sqlite3_malloc(nByte);
42855d607a6eSdan       if( !pNew ){
42861756ae10Sdan         sqlite3_free(pExist);
42875d607a6eSdan         return SQLITE_NOMEM;
42885d607a6eSdan       }
42895d607a6eSdan       memset(pNew, 0, sizeof(SessionChange));
42905d607a6eSdan       pNew->bIndirect = (bIndirect && pExist->bIndirect);
42915d607a6eSdan       aCsr = pNew->aRecord = (u8 *)&pNew[1];
42925d607a6eSdan 
4293b08a1efaSdan       if( op1==SQLITE_INSERT ){             /* INSERT + UPDATE */
42945d607a6eSdan         u8 *a1 = aRec;
4295b08a1efaSdan         assert( op2==SQLITE_UPDATE );
4296798693b2Sdan         pNew->op = SQLITE_INSERT;
4297ef7a6304Sdan         if( bPatchset==0 ) sessionSkipRecord(&a1, pTab->nCol);
429864277f4aSdan         sessionMergeRecord(&aCsr, pTab->nCol, aExist, a1);
4299b08a1efaSdan       }else if( op1==SQLITE_DELETE ){       /* DELETE + INSERT */
4300b08a1efaSdan         assert( op2==SQLITE_INSERT );
4301798693b2Sdan         pNew->op = SQLITE_UPDATE;
4302fa29ecc4Sdan         if( bPatchset ){
4303fa29ecc4Sdan           memcpy(aCsr, aRec, nRec);
4304fa29ecc4Sdan           aCsr += nRec;
4305fa29ecc4Sdan         }else{
430664277f4aSdan           if( 0==sessionMergeUpdate(&aCsr, pTab, bPatchset, aExist, 0,aRec,0) ){
4307b08a1efaSdan             sqlite3_free(pNew);
4308b08a1efaSdan             pNew = 0;
43095d607a6eSdan           }
4310fa29ecc4Sdan         }
4311b08a1efaSdan       }else if( op2==SQLITE_UPDATE ){       /* UPDATE + UPDATE */
431264277f4aSdan         u8 *a1 = aExist;
43135d607a6eSdan         u8 *a2 = aRec;
4314cfec7eeeSdan         assert( op1==SQLITE_UPDATE );
431564277f4aSdan         if( bPatchset==0 ){
4316ef7a6304Sdan           sessionSkipRecord(&a1, pTab->nCol);
4317ef7a6304Sdan           sessionSkipRecord(&a2, pTab->nCol);
431864277f4aSdan         }
4319798693b2Sdan         pNew->op = SQLITE_UPDATE;
432064277f4aSdan         if( 0==sessionMergeUpdate(&aCsr, pTab, bPatchset, aRec, aExist,a1,a2) ){
43215d607a6eSdan           sqlite3_free(pNew);
43225d607a6eSdan           pNew = 0;
43235d607a6eSdan         }
4324b08a1efaSdan       }else{                                /* UPDATE + DELETE */
4325b08a1efaSdan         assert( op1==SQLITE_UPDATE && op2==SQLITE_DELETE );
4326798693b2Sdan         pNew->op = SQLITE_DELETE;
432764277f4aSdan         if( bPatchset ){
432864277f4aSdan           memcpy(aCsr, aRec, nRec);
432964277f4aSdan           aCsr += nRec;
433064277f4aSdan         }else{
433164277f4aSdan           sessionMergeRecord(&aCsr, pTab->nCol, aRec, aExist);
433264277f4aSdan         }
43335d607a6eSdan       }
43345d607a6eSdan 
43355d607a6eSdan       if( pNew ){
43364f528042Sdan         pNew->nRecord = (int)(aCsr - pNew->aRecord);
43375d607a6eSdan       }
43385d607a6eSdan       sqlite3_free(pExist);
43395d607a6eSdan     }
43405d607a6eSdan   }
43415d607a6eSdan 
43425d607a6eSdan   *ppNew = pNew;
43435d607a6eSdan   return SQLITE_OK;
43445d607a6eSdan }
43455d607a6eSdan 
434677fc1d5bSdan /*
43475898ad69Sdan ** Add all changes in the changeset traversed by the iterator passed as
43485898ad69Sdan ** the first argument to the changegroup hash tables.
434977fc1d5bSdan */
435016228167Sdan static int sessionChangesetToHash(
4351cbf6d2d2Sdan   sqlite3_changeset_iter *pIter,   /* Iterator to read from */
43525898ad69Sdan   sqlite3_changegroup *pGrp        /* Changegroup object to add changeset to */
43535d607a6eSdan ){
43545d607a6eSdan   u8 *aRec;
43555d607a6eSdan   int nRec;
4356cbf6d2d2Sdan   int rc = SQLITE_OK;
43575d607a6eSdan   SessionTable *pTab = 0;
43585d607a6eSdan 
43595898ad69Sdan 
43605d607a6eSdan   while( SQLITE_ROW==sessionChangesetNext(pIter, &aRec, &nRec) ){
43615d607a6eSdan     const char *zNew;
43625d607a6eSdan     int nCol;
43635d607a6eSdan     int op;
43645d607a6eSdan     int iHash;
43655d607a6eSdan     int bIndirect;
43665d607a6eSdan     SessionChange *pChange;
43675d607a6eSdan     SessionChange *pExist = 0;
43685d607a6eSdan     SessionChange **pp;
43695d607a6eSdan 
43705898ad69Sdan     if( pGrp->pList==0 ){
43715898ad69Sdan       pGrp->bPatch = pIter->bPatchset;
43725898ad69Sdan     }else if( pIter->bPatchset!=pGrp->bPatch ){
43735898ad69Sdan       rc = SQLITE_ERROR;
43745898ad69Sdan       break;
43755898ad69Sdan     }
43765898ad69Sdan 
43775d607a6eSdan     sqlite3changeset_op(pIter, &zNew, &nCol, &op, &bIndirect);
4378ef7a6304Sdan     if( !pTab || sqlite3_stricmp(zNew, pTab->zName) ){
43795d607a6eSdan       /* Search the list for a matching table */
43804f528042Sdan       int nNew = (int)strlen(zNew);
4381f29123b5Sdan       u8 *abPK;
4382f29123b5Sdan 
4383f29123b5Sdan       sqlite3changeset_pk(pIter, &abPK, 0);
43845898ad69Sdan       for(pTab = pGrp->pList; pTab; pTab=pTab->pNext){
43855d607a6eSdan         if( 0==sqlite3_strnicmp(pTab->zName, zNew, nNew+1) ) break;
43865d607a6eSdan       }
43875d607a6eSdan       if( !pTab ){
43886c39e6a8Sdan         SessionTable **ppTab;
43896c39e6a8Sdan 
4390ef7a6304Sdan         pTab = sqlite3_malloc(sizeof(SessionTable) + nCol + nNew+1);
4391f29123b5Sdan         if( !pTab ){
4392f29123b5Sdan           rc = SQLITE_NOMEM;
4393f29123b5Sdan           break;
4394f29123b5Sdan         }
43955d607a6eSdan         memset(pTab, 0, sizeof(SessionTable));
4396f29123b5Sdan         pTab->nCol = nCol;
4397ef7a6304Sdan         pTab->abPK = (u8*)&pTab[1];
4398ef7a6304Sdan         memcpy(pTab->abPK, abPK, nCol);
4399ef7a6304Sdan         pTab->zName = (char*)&pTab->abPK[nCol];
4400ef7a6304Sdan         memcpy(pTab->zName, zNew, nNew+1);
44016c39e6a8Sdan 
44026c39e6a8Sdan         /* The new object must be linked on to the end of the list, not
44036c39e6a8Sdan         ** simply added to the start of it. This is to ensure that the
44046c39e6a8Sdan         ** tables within the output of sqlite3changegroup_output() are in
44056c39e6a8Sdan         ** the right order.  */
44066c39e6a8Sdan         for(ppTab=&pGrp->pList; *ppTab; ppTab=&(*ppTab)->pNext);
44076c39e6a8Sdan         *ppTab = pTab;
4408f29123b5Sdan       }else if( pTab->nCol!=nCol || memcmp(pTab->abPK, abPK, nCol) ){
4409f29123b5Sdan         rc = SQLITE_SCHEMA;
4410f29123b5Sdan         break;
44115d607a6eSdan       }
44125d607a6eSdan     }
44135d607a6eSdan 
4414cbf6d2d2Sdan     if( sessionGrowHash(pIter->bPatchset, pTab) ){
44151756ae10Sdan       rc = SQLITE_NOMEM;
44161756ae10Sdan       break;
44171756ae10Sdan     }
441864277f4aSdan     iHash = sessionChangeHash(
4419cbf6d2d2Sdan         pTab, (pIter->bPatchset && op==SQLITE_DELETE), aRec, pTab->nChange
442064277f4aSdan     );
44215d607a6eSdan 
44225d607a6eSdan     /* Search for existing entry. If found, remove it from the hash table.
44235d607a6eSdan     ** Code below may link it back in.
44245d607a6eSdan     */
44255d607a6eSdan     for(pp=&pTab->apChange[iHash]; *pp; pp=&(*pp)->pNext){
442664277f4aSdan       int bPkOnly1 = 0;
442764277f4aSdan       int bPkOnly2 = 0;
4428cbf6d2d2Sdan       if( pIter->bPatchset ){
442964277f4aSdan         bPkOnly1 = (*pp)->op==SQLITE_DELETE;
443064277f4aSdan         bPkOnly2 = op==SQLITE_DELETE;
443164277f4aSdan       }
443264277f4aSdan       if( sessionChangeEqual(pTab, bPkOnly1, (*pp)->aRecord, bPkOnly2, aRec) ){
44335d607a6eSdan         pExist = *pp;
44345d607a6eSdan         *pp = (*pp)->pNext;
44355d607a6eSdan         pTab->nEntry--;
44365d607a6eSdan         break;
44375d607a6eSdan       }
44385d607a6eSdan     }
44395d607a6eSdan 
444064277f4aSdan     rc = sessionChangeMerge(pTab,
4441cbf6d2d2Sdan         pIter->bPatchset, pExist, op, bIndirect, aRec, nRec, &pChange
444264277f4aSdan     );
44435d607a6eSdan     if( rc ) break;
44445d607a6eSdan     if( pChange ){
44455d607a6eSdan       pChange->pNext = pTab->apChange[iHash];
44465d607a6eSdan       pTab->apChange[iHash] = pChange;
44475d607a6eSdan       pTab->nEntry++;
44485d607a6eSdan     }
44495d607a6eSdan   }
44505d607a6eSdan 
4451cbf6d2d2Sdan   if( rc==SQLITE_OK ) rc = pIter->rc;
44525d607a6eSdan   return rc;
44535d607a6eSdan }
44545d607a6eSdan 
44555d607a6eSdan /*
44565898ad69Sdan ** Serialize a changeset (or patchset) based on all changesets (or patchsets)
44575898ad69Sdan ** added to the changegroup object passed as the first argument.
44585d607a6eSdan **
44595898ad69Sdan ** If xOutput is not NULL, then the changeset/patchset is returned to the
44605898ad69Sdan ** user via one or more calls to xOutput, as with the other streaming
44615898ad69Sdan ** interfaces.
44625d607a6eSdan **
44635898ad69Sdan ** Or, if xOutput is NULL, then (*ppOut) is populated with a pointer to a
44645898ad69Sdan ** buffer containing the output changeset before this function returns. In
44655898ad69Sdan ** this case (*pnOut) is set to the size of the output buffer in bytes. It
44665898ad69Sdan ** is the responsibility of the caller to free the output buffer using
44675898ad69Sdan ** sqlite3_free() when it is no longer required.
44685898ad69Sdan **
44695898ad69Sdan ** If successful, SQLITE_OK is returned. Or, if an error occurs, an SQLite
44705898ad69Sdan ** error code. If an error occurs and xOutput is NULL, (*ppOut) and (*pnOut)
44715898ad69Sdan ** are both set to 0 before returning.
44725d607a6eSdan */
44735898ad69Sdan static int sessionChangegroupOutput(
44745898ad69Sdan   sqlite3_changegroup *pGrp,
4475cbf6d2d2Sdan   int (*xOutput)(void *pOut, const void *pData, int nData),
4476cbf6d2d2Sdan   void *pOut,
4477cbf6d2d2Sdan   int *pnOut,
4478cbf6d2d2Sdan   void **ppOut
44795d607a6eSdan ){
44805898ad69Sdan   int rc = SQLITE_OK;
4481e8fa8c96Sdan   SessionBuffer buf = {0, 0, 0};
44825898ad69Sdan   SessionTable *pTab;
4483cbf6d2d2Sdan   assert( xOutput==0 || (ppOut==0 && pnOut==0) );
44845d607a6eSdan 
44855d607a6eSdan   /* Create the serialized output changeset based on the contents of the
44865898ad69Sdan   ** hash tables attached to the SessionTable objects in list p->pList.
44875d607a6eSdan   */
44885898ad69Sdan   for(pTab=pGrp->pList; rc==SQLITE_OK && pTab; pTab=pTab->pNext){
44895d607a6eSdan     int i;
44905d607a6eSdan     if( pTab->nEntry==0 ) continue;
44915d607a6eSdan 
44925898ad69Sdan     sessionAppendTableHdr(&buf, pGrp->bPatch, pTab, &rc);
44935d607a6eSdan     for(i=0; i<pTab->nChange; i++){
44945d607a6eSdan       SessionChange *p;
44955d607a6eSdan       for(p=pTab->apChange[i]; p; p=p->pNext){
4496798693b2Sdan         sessionAppendByte(&buf, p->op, &rc);
44975d607a6eSdan         sessionAppendByte(&buf, p->bIndirect, &rc);
44985d607a6eSdan         sessionAppendBlob(&buf, p->aRecord, p->nRecord, &rc);
44995d607a6eSdan       }
45005d607a6eSdan     }
4501cbf6d2d2Sdan 
4502f1a08ad8Sdrh     if( rc==SQLITE_OK && xOutput && buf.nBuf>=SESSIONS_STRM_CHUNK_SIZE ){
4503cbf6d2d2Sdan       rc = xOutput(pOut, buf.aBuf, buf.nBuf);
4504cbf6d2d2Sdan       buf.nBuf = 0;
4505cbf6d2d2Sdan     }
45065d607a6eSdan   }
45075d607a6eSdan 
45085d607a6eSdan   if( rc==SQLITE_OK ){
4509cbf6d2d2Sdan     if( xOutput ){
4510cbf6d2d2Sdan       if( buf.nBuf>0 ) rc = xOutput(pOut, buf.aBuf, buf.nBuf);
4511cbf6d2d2Sdan     }else{
45125d607a6eSdan       *ppOut = buf.aBuf;
45135d607a6eSdan       *pnOut = buf.nBuf;
4514cbf6d2d2Sdan       buf.aBuf = 0;
45155d607a6eSdan     }
45165d607a6eSdan   }
4517cbf6d2d2Sdan   sqlite3_free(buf.aBuf);
45185d607a6eSdan 
45195d607a6eSdan   return rc;
45205d607a6eSdan }
45215d607a6eSdan 
4522cbf6d2d2Sdan /*
45235898ad69Sdan ** Allocate a new, empty, sqlite3_changegroup.
45245898ad69Sdan */
45255898ad69Sdan int sqlite3changegroup_new(sqlite3_changegroup **pp){
45265898ad69Sdan   int rc = SQLITE_OK;             /* Return code */
45275898ad69Sdan   sqlite3_changegroup *p;         /* New object */
45285898ad69Sdan   p = (sqlite3_changegroup*)sqlite3_malloc(sizeof(sqlite3_changegroup));
45295898ad69Sdan   if( p==0 ){
45305898ad69Sdan     rc = SQLITE_NOMEM;
45315898ad69Sdan   }else{
45325898ad69Sdan     memset(p, 0, sizeof(sqlite3_changegroup));
45335898ad69Sdan   }
45345898ad69Sdan   *pp = p;
45355898ad69Sdan   return rc;
45365898ad69Sdan }
45375898ad69Sdan 
45385898ad69Sdan /*
45395898ad69Sdan ** Add the changeset currently stored in buffer pData, size nData bytes,
45405898ad69Sdan ** to changeset-group p.
45415898ad69Sdan */
45425898ad69Sdan int sqlite3changegroup_add(sqlite3_changegroup *pGrp, int nData, void *pData){
45435898ad69Sdan   sqlite3_changeset_iter *pIter;  /* Iterator opened on pData/nData */
45445898ad69Sdan   int rc;                         /* Return code */
45455898ad69Sdan 
45465898ad69Sdan   rc = sqlite3changeset_start(&pIter, nData, pData);
45475898ad69Sdan   if( rc==SQLITE_OK ){
45485898ad69Sdan     rc = sessionChangesetToHash(pIter, pGrp);
45495898ad69Sdan   }
45505898ad69Sdan   sqlite3changeset_finalize(pIter);
45515898ad69Sdan   return rc;
45525898ad69Sdan }
45535898ad69Sdan 
45545898ad69Sdan /*
45555898ad69Sdan ** Obtain a buffer containing a changeset representing the concatenation
45565898ad69Sdan ** of all changesets added to the group so far.
45575898ad69Sdan */
45585898ad69Sdan int sqlite3changegroup_output(
45595898ad69Sdan     sqlite3_changegroup *pGrp,
45605898ad69Sdan     int *pnData,
45615898ad69Sdan     void **ppData
45625898ad69Sdan ){
45635898ad69Sdan   return sessionChangegroupOutput(pGrp, 0, 0, pnData, ppData);
45645898ad69Sdan }
45655898ad69Sdan 
45665898ad69Sdan /*
45675898ad69Sdan ** Streaming versions of changegroup_add().
45685898ad69Sdan */
45695898ad69Sdan int sqlite3changegroup_add_strm(
45705898ad69Sdan   sqlite3_changegroup *pGrp,
45715898ad69Sdan   int (*xInput)(void *pIn, void *pData, int *pnData),
45725898ad69Sdan   void *pIn
45735898ad69Sdan ){
45745898ad69Sdan   sqlite3_changeset_iter *pIter;  /* Iterator opened on pData/nData */
45755898ad69Sdan   int rc;                         /* Return code */
45765898ad69Sdan 
45775898ad69Sdan   rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
45785898ad69Sdan   if( rc==SQLITE_OK ){
45795898ad69Sdan     rc = sessionChangesetToHash(pIter, pGrp);
45805898ad69Sdan   }
45815898ad69Sdan   sqlite3changeset_finalize(pIter);
45825898ad69Sdan   return rc;
45835898ad69Sdan }
45845898ad69Sdan 
45855898ad69Sdan /*
45865898ad69Sdan ** Streaming versions of changegroup_output().
45875898ad69Sdan */
45885898ad69Sdan int sqlite3changegroup_output_strm(
45895898ad69Sdan   sqlite3_changegroup *pGrp,
45905898ad69Sdan   int (*xOutput)(void *pOut, const void *pData, int nData),
45915898ad69Sdan   void *pOut
45925898ad69Sdan ){
45935898ad69Sdan   return sessionChangegroupOutput(pGrp, xOutput, pOut, 0, 0);
45945898ad69Sdan }
45955898ad69Sdan 
45965898ad69Sdan /*
45975898ad69Sdan ** Delete a changegroup object.
45985898ad69Sdan */
45995898ad69Sdan void sqlite3changegroup_delete(sqlite3_changegroup *pGrp){
46005898ad69Sdan   if( pGrp ){
46015898ad69Sdan     sessionDeleteTable(pGrp->pList);
46025898ad69Sdan     sqlite3_free(pGrp);
46035898ad69Sdan   }
46045898ad69Sdan }
46055898ad69Sdan 
46065898ad69Sdan /*
4607cbf6d2d2Sdan ** Combine two changesets together.
4608cbf6d2d2Sdan */
4609cbf6d2d2Sdan int sqlite3changeset_concat(
4610cbf6d2d2Sdan   int nLeft,                      /* Number of bytes in lhs input */
4611cbf6d2d2Sdan   void *pLeft,                    /* Lhs input changeset */
4612cbf6d2d2Sdan   int nRight                      /* Number of bytes in rhs input */,
4613cbf6d2d2Sdan   void *pRight,                   /* Rhs input changeset */
4614cbf6d2d2Sdan   int *pnOut,                     /* OUT: Number of bytes in output changeset */
4615cbf6d2d2Sdan   void **ppOut                    /* OUT: changeset (left <concat> right) */
4616cbf6d2d2Sdan ){
46175898ad69Sdan   sqlite3_changegroup *pGrp;
4618cbf6d2d2Sdan   int rc;
4619cbf6d2d2Sdan 
46205898ad69Sdan   rc = sqlite3changegroup_new(&pGrp);
4621cbf6d2d2Sdan   if( rc==SQLITE_OK ){
46225898ad69Sdan     rc = sqlite3changegroup_add(pGrp, nLeft, pLeft);
4623cbf6d2d2Sdan   }
4624cbf6d2d2Sdan   if( rc==SQLITE_OK ){
46255898ad69Sdan     rc = sqlite3changegroup_add(pGrp, nRight, pRight);
4626cbf6d2d2Sdan   }
46275898ad69Sdan   if( rc==SQLITE_OK ){
46285898ad69Sdan     rc = sqlite3changegroup_output(pGrp, pnOut, ppOut);
46295898ad69Sdan   }
46305898ad69Sdan   sqlite3changegroup_delete(pGrp);
4631cbf6d2d2Sdan 
4632cbf6d2d2Sdan   return rc;
4633cbf6d2d2Sdan }
4634cbf6d2d2Sdan 
4635cbf6d2d2Sdan /*
4636cbf6d2d2Sdan ** Streaming version of sqlite3changeset_concat().
4637cbf6d2d2Sdan */
4638f1a08ad8Sdrh int sqlite3changeset_concat_strm(
4639cbf6d2d2Sdan   int (*xInputA)(void *pIn, void *pData, int *pnData),
4640cbf6d2d2Sdan   void *pInA,
4641cbf6d2d2Sdan   int (*xInputB)(void *pIn, void *pData, int *pnData),
4642cbf6d2d2Sdan   void *pInB,
4643cbf6d2d2Sdan   int (*xOutput)(void *pOut, const void *pData, int nData),
4644cbf6d2d2Sdan   void *pOut
4645cbf6d2d2Sdan ){
46465898ad69Sdan   sqlite3_changegroup *pGrp;
4647cbf6d2d2Sdan   int rc;
4648cbf6d2d2Sdan 
46495898ad69Sdan   rc = sqlite3changegroup_new(&pGrp);
4650cbf6d2d2Sdan   if( rc==SQLITE_OK ){
46515898ad69Sdan     rc = sqlite3changegroup_add_strm(pGrp, xInputA, pInA);
4652cbf6d2d2Sdan   }
4653cbf6d2d2Sdan   if( rc==SQLITE_OK ){
46545898ad69Sdan     rc = sqlite3changegroup_add_strm(pGrp, xInputB, pInB);
4655cbf6d2d2Sdan   }
46565898ad69Sdan   if( rc==SQLITE_OK ){
46575898ad69Sdan     rc = sqlite3changegroup_output_strm(pGrp, xOutput, pOut);
46585898ad69Sdan   }
46595898ad69Sdan   sqlite3changegroup_delete(pGrp);
4660cbf6d2d2Sdan 
4661cbf6d2d2Sdan   return rc;
4662cbf6d2d2Sdan }
4663cbf6d2d2Sdan 
46649b1c62d4Sdrh #endif /* SQLITE_ENABLE_SESSION && SQLITE_ENABLE_PREUPDATE_HOOK */
4665