1 
2 #if defined(SQLITE_ENABLE_SESSION) && defined(SQLITE_ENABLE_PREUPDATE_HOOK)
3 #include "sqlite3session.h"
4 #include <assert.h>
5 #include <string.h>
6 
7 #ifndef SQLITE_AMALGAMATION
8 # include "sqliteInt.h"
9 # include "vdbeInt.h"
10 #endif
11 
12 typedef struct SessionTable SessionTable;
13 typedef struct SessionChange SessionChange;
14 typedef struct SessionBuffer SessionBuffer;
15 typedef struct SessionInput SessionInput;
16 
17 /*
18 ** Minimum chunk size used by streaming versions of functions.
19 */
20 #ifndef SESSIONS_STRM_CHUNK_SIZE
21 # ifdef SQLITE_TEST
22 #   define SESSIONS_STRM_CHUNK_SIZE 64
23 # else
24 #   define SESSIONS_STRM_CHUNK_SIZE 1024
25 # endif
26 #endif
27 
28 typedef struct SessionHook SessionHook;
29 struct SessionHook {
30   void *pCtx;
31   int (*xOld)(void*,int,sqlite3_value**);
32   int (*xNew)(void*,int,sqlite3_value**);
33   int (*xCount)(void*);
34   int (*xDepth)(void*);
35 };
36 
37 /*
38 ** Session handle structure.
39 */
40 struct sqlite3_session {
41   sqlite3 *db;                    /* Database handle session is attached to */
42   char *zDb;                      /* Name of database session is attached to */
43   int bEnable;                    /* True if currently recording */
44   int bIndirect;                  /* True if all changes are indirect */
45   int bAutoAttach;                /* True to auto-attach tables */
46   int rc;                         /* Non-zero if an error has occurred */
47   void *pFilterCtx;               /* First argument to pass to xTableFilter */
48   int (*xTableFilter)(void *pCtx, const char *zTab);
49   sqlite3_value *pZeroBlob;       /* Value containing X'' */
50   sqlite3_session *pNext;         /* Next session object on same db. */
51   SessionTable *pTable;           /* List of attached tables */
52   SessionHook hook;               /* APIs to grab new and old data with */
53 };
54 
55 /*
56 ** Instances of this structure are used to build strings or binary records.
57 */
58 struct SessionBuffer {
59   u8 *aBuf;                       /* Pointer to changeset buffer */
60   int nBuf;                       /* Size of buffer aBuf */
61   int nAlloc;                     /* Size of allocation containing aBuf */
62 };
63 
64 /*
65 ** An object of this type is used internally as an abstraction for
66 ** input data. Input data may be supplied either as a single large buffer
67 ** (e.g. sqlite3changeset_start()) or using a stream function (e.g.
68 **  sqlite3changeset_start_strm()).
69 */
70 struct SessionInput {
71   int bNoDiscard;                 /* If true, do not discard in InputBuffer() */
72   int iCurrent;                   /* Offset in aData[] of current change */
73   int iNext;                      /* Offset in aData[] of next change */
74   u8 *aData;                      /* Pointer to buffer containing changeset */
75   int nData;                      /* Number of bytes in aData */
76 
77   SessionBuffer buf;              /* Current read buffer */
78   int (*xInput)(void*, void*, int*);        /* Input stream call (or NULL) */
79   void *pIn;                                /* First argument to xInput */
80   int bEof;                       /* Set to true after xInput finished */
81 };
82 
83 /*
84 ** Structure for changeset iterators.
85 */
86 struct sqlite3_changeset_iter {
87   SessionInput in;                /* Input buffer or stream */
88   SessionBuffer tblhdr;           /* Buffer to hold apValue/zTab/abPK/ */
89   int bPatchset;                  /* True if this is a patchset */
90   int bInvert;                    /* True to invert changeset */
91   int rc;                         /* Iterator error code */
92   sqlite3_stmt *pConflict;        /* Points to conflicting row, if any */
93   char *zTab;                     /* Current table */
94   int nCol;                       /* Number of columns in zTab */
95   int op;                         /* Current operation */
96   int bIndirect;                  /* True if current change was indirect */
97   u8 *abPK;                       /* Primary key array */
98   sqlite3_value **apValue;        /* old.* and new.* values */
99 };
100 
101 /*
102 ** Each session object maintains a set of the following structures, one
103 ** for each table the session object is monitoring. The structures are
104 ** stored in a linked list starting at sqlite3_session.pTable.
105 **
106 ** The keys of the SessionTable.aChange[] hash table are all rows that have
107 ** been modified in any way since the session object was attached to the
108 ** table.
109 **
110 ** The data associated with each hash-table entry is a structure containing
111 ** a subset of the initial values that the modified row contained at the
112 ** start of the session. Or no initial values if the row was inserted.
113 */
114 struct SessionTable {
115   SessionTable *pNext;
116   char *zName;                    /* Local name of table */
117   int nCol;                       /* Number of columns in table zName */
118   int bStat1;                     /* True if this is sqlite_stat1 */
119   const char **azCol;             /* Column names */
120   u8 *abPK;                       /* Array of primary key flags */
121   int nEntry;                     /* Total number of entries in hash table */
122   int nChange;                    /* Size of apChange[] array */
123   SessionChange **apChange;       /* Hash table buckets */
124 };
125 
126 /*
127 ** RECORD FORMAT:
128 **
129 ** The following record format is similar to (but not compatible with) that
130 ** used in SQLite database files. This format is used as part of the
131 ** change-set binary format, and so must be architecture independent.
132 **
133 ** Unlike the SQLite database record format, each field is self-contained -
134 ** there is no separation of header and data. Each field begins with a
135 ** single byte describing its type, as follows:
136 **
137 **       0x00: Undefined value.
138 **       0x01: Integer value.
139 **       0x02: Real value.
140 **       0x03: Text value.
141 **       0x04: Blob value.
142 **       0x05: SQL NULL value.
143 **
144 ** Note that the above match the definitions of SQLITE_INTEGER, SQLITE_TEXT
145 ** and so on in sqlite3.h. For undefined and NULL values, the field consists
146 ** only of the single type byte. For other types of values, the type byte
147 ** is followed by:
148 **
149 **   Text values:
150 **     A varint containing the number of bytes in the value (encoded using
151 **     UTF-8). Followed by a buffer containing the UTF-8 representation
152 **     of the text value. There is no nul terminator.
153 **
154 **   Blob values:
155 **     A varint containing the number of bytes in the value, followed by
156 **     a buffer containing the value itself.
157 **
158 **   Integer values:
159 **     An 8-byte big-endian integer value.
160 **
161 **   Real values:
162 **     An 8-byte big-endian IEEE 754-2008 real value.
163 **
164 ** Varint values are encoded in the same way as varints in the SQLite
165 ** record format.
166 **
167 ** CHANGESET FORMAT:
168 **
169 ** A changeset is a collection of DELETE, UPDATE and INSERT operations on
170 ** one or more tables. Operations on a single table are grouped together,
171 ** but may occur in any order (i.e. deletes, updates and inserts are all
172 ** mixed together).
173 **
174 ** Each group of changes begins with a table header:
175 **
176 **   1 byte: Constant 0x54 (capital 'T')
177 **   Varint: Number of columns in the table.
178 **   nCol bytes: 0x01 for PK columns, 0x00 otherwise.
179 **   N bytes: Unqualified table name (encoded using UTF-8). Nul-terminated.
180 **
181 ** Followed by one or more changes to the table.
182 **
183 **   1 byte: Either SQLITE_INSERT (0x12), UPDATE (0x17) or DELETE (0x09).
184 **   1 byte: The "indirect-change" flag.
185 **   old.* record: (delete and update only)
186 **   new.* record: (insert and update only)
187 **
188 ** The "old.*" and "new.*" records, if present, are N field records in the
189 ** format described above under "RECORD FORMAT", where N is the number of
190 ** columns in the table. The i'th field of each record is associated with
191 ** the i'th column of the table, counting from left to right in the order
192 ** in which columns were declared in the CREATE TABLE statement.
193 **
194 ** The new.* record that is part of each INSERT change contains the values
195 ** that make up the new row. Similarly, the old.* record that is part of each
196 ** DELETE change contains the values that made up the row that was deleted
197 ** from the database. In the changeset format, the records that are part
198 ** of INSERT or DELETE changes never contain any undefined (type byte 0x00)
199 ** fields.
200 **
201 ** Within the old.* record associated with an UPDATE change, all fields
202 ** associated with table columns that are not PRIMARY KEY columns and are
203 ** not modified by the UPDATE change are set to "undefined". Other fields
204 ** are set to the values that made up the row before the UPDATE that the
205 ** change records took place. Within the new.* record, fields associated
206 ** with table columns modified by the UPDATE change contain the new
207 ** values. Fields associated with table columns that are not modified
208 ** are set to "undefined".
209 **
210 ** PATCHSET FORMAT:
211 **
212 ** A patchset is also a collection of changes. It is similar to a changeset,
213 ** but leaves undefined those fields that are not useful if no conflict
214 ** resolution is required when applying the changeset.
215 **
216 ** Each group of changes begins with a table header:
217 **
218 **   1 byte: Constant 0x50 (capital 'P')
219 **   Varint: Number of columns in the table.
220 **   nCol bytes: 0x01 for PK columns, 0x00 otherwise.
221 **   N bytes: Unqualified table name (encoded using UTF-8). Nul-terminated.
222 **
223 ** Followed by one or more changes to the table.
224 **
225 **   1 byte: Either SQLITE_INSERT (0x12), UPDATE (0x17) or DELETE (0x09).
226 **   1 byte: The "indirect-change" flag.
227 **   single record: (PK fields for DELETE, PK and modified fields for UPDATE,
228 **                   full record for INSERT).
229 **
230 ** As in the changeset format, each field of the single record that is part
231 ** of a patchset change is associated with the correspondingly positioned
232 ** table column, counting from left to right within the CREATE TABLE
233 ** statement.
234 **
235 ** For a DELETE change, all fields within the record except those associated
236 ** with PRIMARY KEY columns are omitted. The PRIMARY KEY fields contain the
237 ** values identifying the row to delete.
238 **
239 ** For an UPDATE change, all fields except those associated with PRIMARY KEY
240 ** columns and columns that are modified by the UPDATE are set to "undefined".
241 ** PRIMARY KEY fields contain the values identifying the table row to update,
242 ** and fields associated with modified columns contain the new column values.
243 **
244 ** The records associated with INSERT changes are in the same format as for
245 ** changesets. It is not possible for a record associated with an INSERT
246 ** change to contain a field set to "undefined".
247 */
248 
249 /*
250 ** For each row modified during a session, there exists a single instance of
251 ** this structure stored in a SessionTable.aChange[] hash table.
252 */
253 struct SessionChange {
254   int op;                         /* One of UPDATE, DELETE, INSERT */
255   int bIndirect;                  /* True if this change is "indirect" */
256   int nRecord;                    /* Number of bytes in buffer aRecord[] */
257   u8 *aRecord;                    /* Buffer containing old.* record */
258   SessionChange *pNext;           /* For hash-table collisions */
259 };
260 
261 /*
262 ** Write a varint with value iVal into the buffer at aBuf. Return the
263 ** number of bytes written.
264 */
265 static int sessionVarintPut(u8 *aBuf, int iVal){
266   return putVarint32(aBuf, iVal);
267 }
268 
269 /*
270 ** Return the number of bytes required to store value iVal as a varint.
271 */
272 static int sessionVarintLen(int iVal){
273   return sqlite3VarintLen(iVal);
274 }
275 
276 /*
277 ** Read a varint value from aBuf[] into *piVal. Return the number of
278 ** bytes read.
279 */
280 static int sessionVarintGet(u8 *aBuf, int *piVal){
281   return getVarint32(aBuf, *piVal);
282 }
283 
284 /* Load an unaligned and unsigned 32-bit integer */
285 #define SESSION_UINT32(x) (((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
286 
287 /*
288 ** Read a 64-bit big-endian integer value from buffer aRec[]. Return
289 ** the value read.
290 */
291 static sqlite3_int64 sessionGetI64(u8 *aRec){
292   u64 x = SESSION_UINT32(aRec);
293   u32 y = SESSION_UINT32(aRec+4);
294   x = (x<<32) + y;
295   return (sqlite3_int64)x;
296 }
297 
298 /*
299 ** Write a 64-bit big-endian integer value to the buffer aBuf[].
300 */
301 static void sessionPutI64(u8 *aBuf, sqlite3_int64 i){
302   aBuf[0] = (i>>56) & 0xFF;
303   aBuf[1] = (i>>48) & 0xFF;
304   aBuf[2] = (i>>40) & 0xFF;
305   aBuf[3] = (i>>32) & 0xFF;
306   aBuf[4] = (i>>24) & 0xFF;
307   aBuf[5] = (i>>16) & 0xFF;
308   aBuf[6] = (i>> 8) & 0xFF;
309   aBuf[7] = (i>> 0) & 0xFF;
310 }
311 
312 /*
313 ** This function is used to serialize the contents of value pValue (see
314 ** comment titled "RECORD FORMAT" above).
315 **
316 ** If it is non-NULL, the serialized form of the value is written to
317 ** buffer aBuf. *pnWrite is set to the number of bytes written before
318 ** returning. Or, if aBuf is NULL, the only thing this function does is
319 ** set *pnWrite.
320 **
321 ** If no error occurs, SQLITE_OK is returned. Or, if an OOM error occurs
322 ** within a call to sqlite3_value_text() (may fail if the db is utf-16))
323 ** SQLITE_NOMEM is returned.
324 */
325 static int sessionSerializeValue(
326   u8 *aBuf,                       /* If non-NULL, write serialized value here */
327   sqlite3_value *pValue,          /* Value to serialize */
328   int *pnWrite                    /* IN/OUT: Increment by bytes written */
329 ){
330   int nByte;                      /* Size of serialized value in bytes */
331 
332   if( pValue ){
333     int eType;                    /* Value type (SQLITE_NULL, TEXT etc.) */
334 
335     eType = sqlite3_value_type(pValue);
336     if( aBuf ) aBuf[0] = eType;
337 
338     switch( eType ){
339       case SQLITE_NULL:
340         nByte = 1;
341         break;
342 
343       case SQLITE_INTEGER:
344       case SQLITE_FLOAT:
345         if( aBuf ){
346           /* TODO: SQLite does something special to deal with mixed-endian
347           ** floating point values (e.g. ARM7). This code probably should
348           ** too.  */
349           u64 i;
350           if( eType==SQLITE_INTEGER ){
351             i = (u64)sqlite3_value_int64(pValue);
352           }else{
353             double r;
354             assert( sizeof(double)==8 && sizeof(u64)==8 );
355             r = sqlite3_value_double(pValue);
356             memcpy(&i, &r, 8);
357           }
358           sessionPutI64(&aBuf[1], i);
359         }
360         nByte = 9;
361         break;
362 
363       default: {
364         u8 *z;
365         int n;
366         int nVarint;
367 
368         assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
369         if( eType==SQLITE_TEXT ){
370           z = (u8 *)sqlite3_value_text(pValue);
371         }else{
372           z = (u8 *)sqlite3_value_blob(pValue);
373         }
374         n = sqlite3_value_bytes(pValue);
375         if( z==0 && (eType!=SQLITE_BLOB || n>0) ) return SQLITE_NOMEM;
376         nVarint = sessionVarintLen(n);
377 
378         if( aBuf ){
379           sessionVarintPut(&aBuf[1], n);
380           if( n ) memcpy(&aBuf[nVarint + 1], z, n);
381         }
382 
383         nByte = 1 + nVarint + n;
384         break;
385       }
386     }
387   }else{
388     nByte = 1;
389     if( aBuf ) aBuf[0] = '\0';
390   }
391 
392   if( pnWrite ) *pnWrite += nByte;
393   return SQLITE_OK;
394 }
395 
396 
397 /*
398 ** This macro is used to calculate hash key values for data structures. In
399 ** order to use this macro, the entire data structure must be represented
400 ** as a series of unsigned integers. In order to calculate a hash-key value
401 ** for a data structure represented as three such integers, the macro may
402 ** then be used as follows:
403 **
404 **    int hash_key_value;
405 **    hash_key_value = HASH_APPEND(0, <value 1>);
406 **    hash_key_value = HASH_APPEND(hash_key_value, <value 2>);
407 **    hash_key_value = HASH_APPEND(hash_key_value, <value 3>);
408 **
409 ** In practice, the data structures this macro is used for are the primary
410 ** key values of modified rows.
411 */
412 #define HASH_APPEND(hash, add) ((hash) << 3) ^ (hash) ^ (unsigned int)(add)
413 
414 /*
415 ** Append the hash of the 64-bit integer passed as the second argument to the
416 ** hash-key value passed as the first. Return the new hash-key value.
417 */
418 static unsigned int sessionHashAppendI64(unsigned int h, i64 i){
419   h = HASH_APPEND(h, i & 0xFFFFFFFF);
420   return HASH_APPEND(h, (i>>32)&0xFFFFFFFF);
421 }
422 
423 /*
424 ** Append the hash of the blob passed via the second and third arguments to
425 ** the hash-key value passed as the first. Return the new hash-key value.
426 */
427 static unsigned int sessionHashAppendBlob(unsigned int h, int n, const u8 *z){
428   int i;
429   for(i=0; i<n; i++) h = HASH_APPEND(h, z[i]);
430   return h;
431 }
432 
433 /*
434 ** Append the hash of the data type passed as the second argument to the
435 ** hash-key value passed as the first. Return the new hash-key value.
436 */
437 static unsigned int sessionHashAppendType(unsigned int h, int eType){
438   return HASH_APPEND(h, eType);
439 }
440 
441 /*
442 ** This function may only be called from within a pre-update callback.
443 ** It calculates a hash based on the primary key values of the old.* or
444 ** new.* row currently available and, assuming no error occurs, writes it to
445 ** *piHash before returning. If the primary key contains one or more NULL
446 ** values, *pbNullPK is set to true before returning.
447 **
448 ** If an error occurs, an SQLite error code is returned and the final values
449 ** of *piHash asn *pbNullPK are undefined. Otherwise, SQLITE_OK is returned
450 ** and the output variables are set as described above.
451 */
452 static int sessionPreupdateHash(
453   sqlite3_session *pSession,      /* Session object that owns pTab */
454   SessionTable *pTab,             /* Session table handle */
455   int bNew,                       /* True to hash the new.* PK */
456   int *piHash,                    /* OUT: Hash value */
457   int *pbNullPK                   /* OUT: True if there are NULL values in PK */
458 ){
459   unsigned int h = 0;             /* Hash value to return */
460   int i;                          /* Used to iterate through columns */
461 
462   assert( *pbNullPK==0 );
463   assert( pTab->nCol==pSession->hook.xCount(pSession->hook.pCtx) );
464   for(i=0; i<pTab->nCol; i++){
465     if( pTab->abPK[i] ){
466       int rc;
467       int eType;
468       sqlite3_value *pVal;
469 
470       if( bNew ){
471         rc = pSession->hook.xNew(pSession->hook.pCtx, i, &pVal);
472       }else{
473         rc = pSession->hook.xOld(pSession->hook.pCtx, i, &pVal);
474       }
475       if( rc!=SQLITE_OK ) return rc;
476 
477       eType = sqlite3_value_type(pVal);
478       h = sessionHashAppendType(h, eType);
479       if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
480         i64 iVal;
481         if( eType==SQLITE_INTEGER ){
482           iVal = sqlite3_value_int64(pVal);
483         }else{
484           double rVal = sqlite3_value_double(pVal);
485           assert( sizeof(iVal)==8 && sizeof(rVal)==8 );
486           memcpy(&iVal, &rVal, 8);
487         }
488         h = sessionHashAppendI64(h, iVal);
489       }else if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
490         const u8 *z;
491         int n;
492         if( eType==SQLITE_TEXT ){
493           z = (const u8 *)sqlite3_value_text(pVal);
494         }else{
495           z = (const u8 *)sqlite3_value_blob(pVal);
496         }
497         n = sqlite3_value_bytes(pVal);
498         if( !z && (eType!=SQLITE_BLOB || n>0) ) return SQLITE_NOMEM;
499         h = sessionHashAppendBlob(h, n, z);
500       }else{
501         assert( eType==SQLITE_NULL );
502         assert( pTab->bStat1==0 || i!=1 );
503         *pbNullPK = 1;
504       }
505     }
506   }
507 
508   *piHash = (h % pTab->nChange);
509   return SQLITE_OK;
510 }
511 
512 /*
513 ** The buffer that the argument points to contains a serialized SQL value.
514 ** Return the number of bytes of space occupied by the value (including
515 ** the type byte).
516 */
517 static int sessionSerialLen(u8 *a){
518   int e = *a;
519   int n;
520   if( e==0 || e==0xFF ) return 1;
521   if( e==SQLITE_NULL ) return 1;
522   if( e==SQLITE_INTEGER || e==SQLITE_FLOAT ) return 9;
523   return sessionVarintGet(&a[1], &n) + 1 + n;
524 }
525 
526 /*
527 ** Based on the primary key values stored in change aRecord, calculate a
528 ** hash key. Assume the has table has nBucket buckets. The hash keys
529 ** calculated by this function are compatible with those calculated by
530 ** sessionPreupdateHash().
531 **
532 ** The bPkOnly argument is non-zero if the record at aRecord[] is from
533 ** a patchset DELETE. In this case the non-PK fields are omitted entirely.
534 */
535 static unsigned int sessionChangeHash(
536   SessionTable *pTab,             /* Table handle */
537   int bPkOnly,                    /* Record consists of PK fields only */
538   u8 *aRecord,                    /* Change record */
539   int nBucket                     /* Assume this many buckets in hash table */
540 ){
541   unsigned int h = 0;             /* Value to return */
542   int i;                          /* Used to iterate through columns */
543   u8 *a = aRecord;                /* Used to iterate through change record */
544 
545   for(i=0; i<pTab->nCol; i++){
546     int eType = *a;
547     int isPK = pTab->abPK[i];
548     if( bPkOnly && isPK==0 ) continue;
549 
550     /* It is not possible for eType to be SQLITE_NULL here. The session
551     ** module does not record changes for rows with NULL values stored in
552     ** primary key columns. */
553     assert( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT
554          || eType==SQLITE_TEXT || eType==SQLITE_BLOB
555          || eType==SQLITE_NULL || eType==0
556     );
557     assert( !isPK || (eType!=0 && eType!=SQLITE_NULL) );
558 
559     if( isPK ){
560       a++;
561       h = sessionHashAppendType(h, eType);
562       if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
563         h = sessionHashAppendI64(h, sessionGetI64(a));
564         a += 8;
565       }else{
566         int n;
567         a += sessionVarintGet(a, &n);
568         h = sessionHashAppendBlob(h, n, a);
569         a += n;
570       }
571     }else{
572       a += sessionSerialLen(a);
573     }
574   }
575   return (h % nBucket);
576 }
577 
578 /*
579 ** Arguments aLeft and aRight are pointers to change records for table pTab.
580 ** This function returns true if the two records apply to the same row (i.e.
581 ** have the same values stored in the primary key columns), or false
582 ** otherwise.
583 */
584 static int sessionChangeEqual(
585   SessionTable *pTab,             /* Table used for PK definition */
586   int bLeftPkOnly,                /* True if aLeft[] contains PK fields only */
587   u8 *aLeft,                      /* Change record */
588   int bRightPkOnly,               /* True if aRight[] contains PK fields only */
589   u8 *aRight                      /* Change record */
590 ){
591   u8 *a1 = aLeft;                 /* Cursor to iterate through aLeft */
592   u8 *a2 = aRight;                /* Cursor to iterate through aRight */
593   int iCol;                       /* Used to iterate through table columns */
594 
595   for(iCol=0; iCol<pTab->nCol; iCol++){
596     if( pTab->abPK[iCol] ){
597       int n1 = sessionSerialLen(a1);
598       int n2 = sessionSerialLen(a2);
599 
600       if( n1!=n2 || memcmp(a1, a2, n1) ){
601         return 0;
602       }
603       a1 += n1;
604       a2 += n2;
605     }else{
606       if( bLeftPkOnly==0 ) a1 += sessionSerialLen(a1);
607       if( bRightPkOnly==0 ) a2 += sessionSerialLen(a2);
608     }
609   }
610 
611   return 1;
612 }
613 
614 /*
615 ** Arguments aLeft and aRight both point to buffers containing change
616 ** records with nCol columns. This function "merges" the two records into
617 ** a single records which is written to the buffer at *paOut. *paOut is
618 ** then set to point to one byte after the last byte written before
619 ** returning.
620 **
621 ** The merging of records is done as follows: For each column, if the
622 ** aRight record contains a value for the column, copy the value from
623 ** their. Otherwise, if aLeft contains a value, copy it. If neither
624 ** record contains a value for a given column, then neither does the
625 ** output record.
626 */
627 static void sessionMergeRecord(
628   u8 **paOut,
629   int nCol,
630   u8 *aLeft,
631   u8 *aRight
632 ){
633   u8 *a1 = aLeft;                 /* Cursor used to iterate through aLeft */
634   u8 *a2 = aRight;                /* Cursor used to iterate through aRight */
635   u8 *aOut = *paOut;              /* Output cursor */
636   int iCol;                       /* Used to iterate from 0 to nCol */
637 
638   for(iCol=0; iCol<nCol; iCol++){
639     int n1 = sessionSerialLen(a1);
640     int n2 = sessionSerialLen(a2);
641     if( *a2 ){
642       memcpy(aOut, a2, n2);
643       aOut += n2;
644     }else{
645       memcpy(aOut, a1, n1);
646       aOut += n1;
647     }
648     a1 += n1;
649     a2 += n2;
650   }
651 
652   *paOut = aOut;
653 }
654 
655 /*
656 ** This is a helper function used by sessionMergeUpdate().
657 **
658 ** When this function is called, both *paOne and *paTwo point to a value
659 ** within a change record. Before it returns, both have been advanced so
660 ** as to point to the next value in the record.
661 **
662 ** If, when this function is called, *paTwo points to a valid value (i.e.
663 ** *paTwo[0] is not 0x00 - the "no value" placeholder), a copy of the *paTwo
664 ** pointer is returned and *pnVal is set to the number of bytes in the
665 ** serialized value. Otherwise, a copy of *paOne is returned and *pnVal
666 ** set to the number of bytes in the value at *paOne. If *paOne points
667 ** to the "no value" placeholder, *pnVal is set to 1. In other words:
668 **
669 **   if( *paTwo is valid ) return *paTwo;
670 **   return *paOne;
671 **
672 */
673 static u8 *sessionMergeValue(
674   u8 **paOne,                     /* IN/OUT: Left-hand buffer pointer */
675   u8 **paTwo,                     /* IN/OUT: Right-hand buffer pointer */
676   int *pnVal                      /* OUT: Bytes in returned value */
677 ){
678   u8 *a1 = *paOne;
679   u8 *a2 = *paTwo;
680   u8 *pRet = 0;
681   int n1;
682 
683   assert( a1 );
684   if( a2 ){
685     int n2 = sessionSerialLen(a2);
686     if( *a2 ){
687       *pnVal = n2;
688       pRet = a2;
689     }
690     *paTwo = &a2[n2];
691   }
692 
693   n1 = sessionSerialLen(a1);
694   if( pRet==0 ){
695     *pnVal = n1;
696     pRet = a1;
697   }
698   *paOne = &a1[n1];
699 
700   return pRet;
701 }
702 
703 /*
704 ** This function is used by changeset_concat() to merge two UPDATE changes
705 ** on the same row.
706 */
707 static int sessionMergeUpdate(
708   u8 **paOut,                     /* IN/OUT: Pointer to output buffer */
709   SessionTable *pTab,             /* Table change pertains to */
710   int bPatchset,                  /* True if records are patchset records */
711   u8 *aOldRecord1,                /* old.* record for first change */
712   u8 *aOldRecord2,                /* old.* record for second change */
713   u8 *aNewRecord1,                /* new.* record for first change */
714   u8 *aNewRecord2                 /* new.* record for second change */
715 ){
716   u8 *aOld1 = aOldRecord1;
717   u8 *aOld2 = aOldRecord2;
718   u8 *aNew1 = aNewRecord1;
719   u8 *aNew2 = aNewRecord2;
720 
721   u8 *aOut = *paOut;
722   int i;
723 
724   if( bPatchset==0 ){
725     int bRequired = 0;
726 
727     assert( aOldRecord1 && aNewRecord1 );
728 
729     /* Write the old.* vector first. */
730     for(i=0; i<pTab->nCol; i++){
731       int nOld;
732       u8 *aOld;
733       int nNew;
734       u8 *aNew;
735 
736       aOld = sessionMergeValue(&aOld1, &aOld2, &nOld);
737       aNew = sessionMergeValue(&aNew1, &aNew2, &nNew);
738       if( pTab->abPK[i] || nOld!=nNew || memcmp(aOld, aNew, nNew) ){
739         if( pTab->abPK[i]==0 ) bRequired = 1;
740         memcpy(aOut, aOld, nOld);
741         aOut += nOld;
742       }else{
743         *(aOut++) = '\0';
744       }
745     }
746 
747     if( !bRequired ) return 0;
748   }
749 
750   /* Write the new.* vector */
751   aOld1 = aOldRecord1;
752   aOld2 = aOldRecord2;
753   aNew1 = aNewRecord1;
754   aNew2 = aNewRecord2;
755   for(i=0; i<pTab->nCol; i++){
756     int nOld;
757     u8 *aOld;
758     int nNew;
759     u8 *aNew;
760 
761     aOld = sessionMergeValue(&aOld1, &aOld2, &nOld);
762     aNew = sessionMergeValue(&aNew1, &aNew2, &nNew);
763     if( bPatchset==0
764      && (pTab->abPK[i] || (nOld==nNew && 0==memcmp(aOld, aNew, nNew)))
765     ){
766       *(aOut++) = '\0';
767     }else{
768       memcpy(aOut, aNew, nNew);
769       aOut += nNew;
770     }
771   }
772 
773   *paOut = aOut;
774   return 1;
775 }
776 
777 /*
778 ** This function is only called from within a pre-update-hook callback.
779 ** It determines if the current pre-update-hook change affects the same row
780 ** as the change stored in argument pChange. If so, it returns true. Otherwise
781 ** if the pre-update-hook does not affect the same row as pChange, it returns
782 ** false.
783 */
784 static int sessionPreupdateEqual(
785   sqlite3_session *pSession,      /* Session object that owns SessionTable */
786   SessionTable *pTab,             /* Table associated with change */
787   SessionChange *pChange,         /* Change to compare to */
788   int op                          /* Current pre-update operation */
789 ){
790   int iCol;                       /* Used to iterate through columns */
791   u8 *a = pChange->aRecord;       /* Cursor used to scan change record */
792 
793   assert( op==SQLITE_INSERT || op==SQLITE_UPDATE || op==SQLITE_DELETE );
794   for(iCol=0; iCol<pTab->nCol; iCol++){
795     if( !pTab->abPK[iCol] ){
796       a += sessionSerialLen(a);
797     }else{
798       sqlite3_value *pVal;        /* Value returned by preupdate_new/old */
799       int rc;                     /* Error code from preupdate_new/old */
800       int eType = *a++;           /* Type of value from change record */
801 
802       /* The following calls to preupdate_new() and preupdate_old() can not
803       ** fail. This is because they cache their return values, and by the
804       ** time control flows to here they have already been called once from
805       ** within sessionPreupdateHash(). The first two asserts below verify
806       ** this (that the method has already been called). */
807       if( op==SQLITE_INSERT ){
808         /* assert( db->pPreUpdate->pNewUnpacked || db->pPreUpdate->aNew ); */
809         rc = pSession->hook.xNew(pSession->hook.pCtx, iCol, &pVal);
810       }else{
811         /* assert( db->pPreUpdate->pUnpacked ); */
812         rc = pSession->hook.xOld(pSession->hook.pCtx, iCol, &pVal);
813       }
814       assert( rc==SQLITE_OK );
815       if( sqlite3_value_type(pVal)!=eType ) return 0;
816 
817       /* A SessionChange object never has a NULL value in a PK column */
818       assert( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT
819            || eType==SQLITE_BLOB    || eType==SQLITE_TEXT
820       );
821 
822       if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
823         i64 iVal = sessionGetI64(a);
824         a += 8;
825         if( eType==SQLITE_INTEGER ){
826           if( sqlite3_value_int64(pVal)!=iVal ) return 0;
827         }else{
828           double rVal;
829           assert( sizeof(iVal)==8 && sizeof(rVal)==8 );
830           memcpy(&rVal, &iVal, 8);
831           if( sqlite3_value_double(pVal)!=rVal ) return 0;
832         }
833       }else{
834         int n;
835         const u8 *z;
836         a += sessionVarintGet(a, &n);
837         if( sqlite3_value_bytes(pVal)!=n ) return 0;
838         if( eType==SQLITE_TEXT ){
839           z = sqlite3_value_text(pVal);
840         }else{
841           z = sqlite3_value_blob(pVal);
842         }
843         if( n>0 && memcmp(a, z, n) ) return 0;
844         a += n;
845       }
846     }
847   }
848 
849   return 1;
850 }
851 
852 /*
853 ** If required, grow the hash table used to store changes on table pTab
854 ** (part of the session pSession). If a fatal OOM error occurs, set the
855 ** session object to failed and return SQLITE_ERROR. Otherwise, return
856 ** SQLITE_OK.
857 **
858 ** It is possible that a non-fatal OOM error occurs in this function. In
859 ** that case the hash-table does not grow, but SQLITE_OK is returned anyway.
860 ** Growing the hash table in this case is a performance optimization only,
861 ** it is not required for correct operation.
862 */
863 static int sessionGrowHash(int bPatchset, SessionTable *pTab){
864   if( pTab->nChange==0 || pTab->nEntry>=(pTab->nChange/2) ){
865     int i;
866     SessionChange **apNew;
867     int nNew = (pTab->nChange ? pTab->nChange : 128) * 2;
868 
869     apNew = (SessionChange **)sqlite3_malloc(sizeof(SessionChange *) * nNew);
870     if( apNew==0 ){
871       if( pTab->nChange==0 ){
872         return SQLITE_ERROR;
873       }
874       return SQLITE_OK;
875     }
876     memset(apNew, 0, sizeof(SessionChange *) * nNew);
877 
878     for(i=0; i<pTab->nChange; i++){
879       SessionChange *p;
880       SessionChange *pNext;
881       for(p=pTab->apChange[i]; p; p=pNext){
882         int bPkOnly = (p->op==SQLITE_DELETE && bPatchset);
883         int iHash = sessionChangeHash(pTab, bPkOnly, p->aRecord, nNew);
884         pNext = p->pNext;
885         p->pNext = apNew[iHash];
886         apNew[iHash] = p;
887       }
888     }
889 
890     sqlite3_free(pTab->apChange);
891     pTab->nChange = nNew;
892     pTab->apChange = apNew;
893   }
894 
895   return SQLITE_OK;
896 }
897 
898 /*
899 ** This function queries the database for the names of the columns of table
900 ** zThis, in schema zDb.
901 **
902 ** Otherwise, if they are not NULL, variable *pnCol is set to the number
903 ** of columns in the database table and variable *pzTab is set to point to a
904 ** nul-terminated copy of the table name. *pazCol (if not NULL) is set to
905 ** point to an array of pointers to column names. And *pabPK (again, if not
906 ** NULL) is set to point to an array of booleans - true if the corresponding
907 ** column is part of the primary key.
908 **
909 ** For example, if the table is declared as:
910 **
911 **     CREATE TABLE tbl1(w, x, y, z, PRIMARY KEY(w, z));
912 **
913 ** Then the four output variables are populated as follows:
914 **
915 **     *pnCol  = 4
916 **     *pzTab  = "tbl1"
917 **     *pazCol = {"w", "x", "y", "z"}
918 **     *pabPK  = {1, 0, 0, 1}
919 **
920 ** All returned buffers are part of the same single allocation, which must
921 ** be freed using sqlite3_free() by the caller
922 */
923 static int sessionTableInfo(
924   sqlite3 *db,                    /* Database connection */
925   const char *zDb,                /* Name of attached database (e.g. "main") */
926   const char *zThis,              /* Table name */
927   int *pnCol,                     /* OUT: number of columns */
928   const char **pzTab,             /* OUT: Copy of zThis */
929   const char ***pazCol,           /* OUT: Array of column names for table */
930   u8 **pabPK                      /* OUT: Array of booleans - true for PK col */
931 ){
932   char *zPragma;
933   sqlite3_stmt *pStmt;
934   int rc;
935   int nByte;
936   int nDbCol = 0;
937   int nThis;
938   int i;
939   u8 *pAlloc = 0;
940   char **azCol = 0;
941   u8 *abPK = 0;
942 
943   assert( pazCol && pabPK );
944 
945   nThis = sqlite3Strlen30(zThis);
946   if( nThis==12 && 0==sqlite3_stricmp("sqlite_stat1", zThis) ){
947     rc = sqlite3_table_column_metadata(db, zDb, zThis, 0, 0, 0, 0, 0, 0);
948     if( rc==SQLITE_OK ){
949       /* For sqlite_stat1, pretend that (tbl,idx) is the PRIMARY KEY. */
950       zPragma = sqlite3_mprintf(
951           "SELECT 0, 'tbl',  '', 0, '', 1     UNION ALL "
952           "SELECT 1, 'idx',  '', 0, '', 2     UNION ALL "
953           "SELECT 2, 'stat', '', 0, '', 0"
954       );
955     }else if( rc==SQLITE_ERROR ){
956       zPragma = sqlite3_mprintf("");
957     }else{
958       return rc;
959     }
960   }else{
961     zPragma = sqlite3_mprintf("PRAGMA '%q'.table_info('%q')", zDb, zThis);
962   }
963   if( !zPragma ) return SQLITE_NOMEM;
964 
965   rc = sqlite3_prepare_v2(db, zPragma, -1, &pStmt, 0);
966   sqlite3_free(zPragma);
967   if( rc!=SQLITE_OK ) return rc;
968 
969   nByte = nThis + 1;
970   while( SQLITE_ROW==sqlite3_step(pStmt) ){
971     nByte += sqlite3_column_bytes(pStmt, 1);
972     nDbCol++;
973   }
974   rc = sqlite3_reset(pStmt);
975 
976   if( rc==SQLITE_OK ){
977     nByte += nDbCol * (sizeof(const char *) + sizeof(u8) + 1);
978     pAlloc = sqlite3_malloc(nByte);
979     if( pAlloc==0 ){
980       rc = SQLITE_NOMEM;
981     }
982   }
983   if( rc==SQLITE_OK ){
984     azCol = (char **)pAlloc;
985     pAlloc = (u8 *)&azCol[nDbCol];
986     abPK = (u8 *)pAlloc;
987     pAlloc = &abPK[nDbCol];
988     if( pzTab ){
989       memcpy(pAlloc, zThis, nThis+1);
990       *pzTab = (char *)pAlloc;
991       pAlloc += nThis+1;
992     }
993 
994     i = 0;
995     while( SQLITE_ROW==sqlite3_step(pStmt) ){
996       int nName = sqlite3_column_bytes(pStmt, 1);
997       const unsigned char *zName = sqlite3_column_text(pStmt, 1);
998       if( zName==0 ) break;
999       memcpy(pAlloc, zName, nName+1);
1000       azCol[i] = (char *)pAlloc;
1001       pAlloc += nName+1;
1002       abPK[i] = sqlite3_column_int(pStmt, 5);
1003       i++;
1004     }
1005     rc = sqlite3_reset(pStmt);
1006 
1007   }
1008 
1009   /* If successful, populate the output variables. Otherwise, zero them and
1010   ** free any allocation made. An error code will be returned in this case.
1011   */
1012   if( rc==SQLITE_OK ){
1013     *pazCol = (const char **)azCol;
1014     *pabPK = abPK;
1015     *pnCol = nDbCol;
1016   }else{
1017     *pazCol = 0;
1018     *pabPK = 0;
1019     *pnCol = 0;
1020     if( pzTab ) *pzTab = 0;
1021     sqlite3_free(azCol);
1022   }
1023   sqlite3_finalize(pStmt);
1024   return rc;
1025 }
1026 
1027 /*
1028 ** This function is only called from within a pre-update handler for a
1029 ** write to table pTab, part of session pSession. If this is the first
1030 ** write to this table, initalize the SessionTable.nCol, azCol[] and
1031 ** abPK[] arrays accordingly.
1032 **
1033 ** If an error occurs, an error code is stored in sqlite3_session.rc and
1034 ** non-zero returned. Or, if no error occurs but the table has no primary
1035 ** key, sqlite3_session.rc is left set to SQLITE_OK and non-zero returned to
1036 ** indicate that updates on this table should be ignored. SessionTable.abPK
1037 ** is set to NULL in this case.
1038 */
1039 static int sessionInitTable(sqlite3_session *pSession, SessionTable *pTab){
1040   if( pTab->nCol==0 ){
1041     u8 *abPK;
1042     assert( pTab->azCol==0 || pTab->abPK==0 );
1043     pSession->rc = sessionTableInfo(pSession->db, pSession->zDb,
1044         pTab->zName, &pTab->nCol, 0, &pTab->azCol, &abPK
1045     );
1046     if( pSession->rc==SQLITE_OK ){
1047       int i;
1048       for(i=0; i<pTab->nCol; i++){
1049         if( abPK[i] ){
1050           pTab->abPK = abPK;
1051           break;
1052         }
1053       }
1054       if( 0==sqlite3_stricmp("sqlite_stat1", pTab->zName) ){
1055         pTab->bStat1 = 1;
1056       }
1057     }
1058   }
1059   return (pSession->rc || pTab->abPK==0);
1060 }
1061 
1062 /*
1063 ** Versions of the four methods in object SessionHook for use with the
1064 ** sqlite_stat1 table. The purpose of this is to substitute a zero-length
1065 ** blob each time a NULL value is read from the "idx" column of the
1066 ** sqlite_stat1 table.
1067 */
1068 typedef struct SessionStat1Ctx SessionStat1Ctx;
1069 struct SessionStat1Ctx {
1070   SessionHook hook;
1071   sqlite3_session *pSession;
1072 };
1073 static int sessionStat1Old(void *pCtx, int iCol, sqlite3_value **ppVal){
1074   SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
1075   sqlite3_value *pVal = 0;
1076   int rc = p->hook.xOld(p->hook.pCtx, iCol, &pVal);
1077   if( rc==SQLITE_OK && iCol==1 && sqlite3_value_type(pVal)==SQLITE_NULL ){
1078     pVal = p->pSession->pZeroBlob;
1079   }
1080   *ppVal = pVal;
1081   return rc;
1082 }
1083 static int sessionStat1New(void *pCtx, int iCol, sqlite3_value **ppVal){
1084   SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
1085   sqlite3_value *pVal = 0;
1086   int rc = p->hook.xNew(p->hook.pCtx, iCol, &pVal);
1087   if( rc==SQLITE_OK && iCol==1 && sqlite3_value_type(pVal)==SQLITE_NULL ){
1088     pVal = p->pSession->pZeroBlob;
1089   }
1090   *ppVal = pVal;
1091   return rc;
1092 }
1093 static int sessionStat1Count(void *pCtx){
1094   SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
1095   return p->hook.xCount(p->hook.pCtx);
1096 }
1097 static int sessionStat1Depth(void *pCtx){
1098   SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
1099   return p->hook.xDepth(p->hook.pCtx);
1100 }
1101 
1102 
1103 /*
1104 ** This function is only called from with a pre-update-hook reporting a
1105 ** change on table pTab (attached to session pSession). The type of change
1106 ** (UPDATE, INSERT, DELETE) is specified by the first argument.
1107 **
1108 ** Unless one is already present or an error occurs, an entry is added
1109 ** to the changed-rows hash table associated with table pTab.
1110 */
1111 static void sessionPreupdateOneChange(
1112   int op,                         /* One of SQLITE_UPDATE, INSERT, DELETE */
1113   sqlite3_session *pSession,      /* Session object pTab is attached to */
1114   SessionTable *pTab              /* Table that change applies to */
1115 ){
1116   int iHash;
1117   int bNull = 0;
1118   int rc = SQLITE_OK;
1119   SessionStat1Ctx stat1 = {0};
1120 
1121   if( pSession->rc ) return;
1122 
1123   /* Load table details if required */
1124   if( sessionInitTable(pSession, pTab) ) return;
1125 
1126   /* Check the number of columns in this xPreUpdate call matches the
1127   ** number of columns in the table.  */
1128   if( pTab->nCol!=pSession->hook.xCount(pSession->hook.pCtx) ){
1129     pSession->rc = SQLITE_SCHEMA;
1130     return;
1131   }
1132 
1133   /* Grow the hash table if required */
1134   if( sessionGrowHash(0, pTab) ){
1135     pSession->rc = SQLITE_NOMEM;
1136     return;
1137   }
1138 
1139   if( pTab->bStat1 ){
1140     stat1.hook = pSession->hook;
1141     stat1.pSession = pSession;
1142     pSession->hook.pCtx = (void*)&stat1;
1143     pSession->hook.xNew = sessionStat1New;
1144     pSession->hook.xOld = sessionStat1Old;
1145     pSession->hook.xCount = sessionStat1Count;
1146     pSession->hook.xDepth = sessionStat1Depth;
1147     if( pSession->pZeroBlob==0 ){
1148       sqlite3_value *p = sqlite3ValueNew(0);
1149       if( p==0 ){
1150         rc = SQLITE_NOMEM;
1151         goto error_out;
1152       }
1153       sqlite3ValueSetStr(p, 0, "", 0, SQLITE_STATIC);
1154       pSession->pZeroBlob = p;
1155     }
1156   }
1157 
1158   /* Calculate the hash-key for this change. If the primary key of the row
1159   ** includes a NULL value, exit early. Such changes are ignored by the
1160   ** session module. */
1161   rc = sessionPreupdateHash(pSession, pTab, op==SQLITE_INSERT, &iHash, &bNull);
1162   if( rc!=SQLITE_OK ) goto error_out;
1163 
1164   if( bNull==0 ){
1165     /* Search the hash table for an existing record for this row. */
1166     SessionChange *pC;
1167     for(pC=pTab->apChange[iHash]; pC; pC=pC->pNext){
1168       if( sessionPreupdateEqual(pSession, pTab, pC, op) ) break;
1169     }
1170 
1171     if( pC==0 ){
1172       /* Create a new change object containing all the old values (if
1173       ** this is an SQLITE_UPDATE or SQLITE_DELETE), or just the PK
1174       ** values (if this is an INSERT). */
1175       SessionChange *pChange; /* New change object */
1176       int nByte;              /* Number of bytes to allocate */
1177       int i;                  /* Used to iterate through columns */
1178 
1179       assert( rc==SQLITE_OK );
1180       pTab->nEntry++;
1181 
1182       /* Figure out how large an allocation is required */
1183       nByte = sizeof(SessionChange);
1184       for(i=0; i<pTab->nCol; i++){
1185         sqlite3_value *p = 0;
1186         if( op!=SQLITE_INSERT ){
1187           TESTONLY(int trc = ) pSession->hook.xOld(pSession->hook.pCtx, i, &p);
1188           assert( trc==SQLITE_OK );
1189         }else if( pTab->abPK[i] ){
1190           TESTONLY(int trc = ) pSession->hook.xNew(pSession->hook.pCtx, i, &p);
1191           assert( trc==SQLITE_OK );
1192         }
1193 
1194         /* This may fail if SQLite value p contains a utf-16 string that must
1195         ** be converted to utf-8 and an OOM error occurs while doing so. */
1196         rc = sessionSerializeValue(0, p, &nByte);
1197         if( rc!=SQLITE_OK ) goto error_out;
1198       }
1199 
1200       /* Allocate the change object */
1201       pChange = (SessionChange *)sqlite3_malloc(nByte);
1202       if( !pChange ){
1203         rc = SQLITE_NOMEM;
1204         goto error_out;
1205       }else{
1206         memset(pChange, 0, sizeof(SessionChange));
1207         pChange->aRecord = (u8 *)&pChange[1];
1208       }
1209 
1210       /* Populate the change object. None of the preupdate_old(),
1211       ** preupdate_new() or SerializeValue() calls below may fail as all
1212       ** required values and encodings have already been cached in memory.
1213       ** It is not possible for an OOM to occur in this block. */
1214       nByte = 0;
1215       for(i=0; i<pTab->nCol; i++){
1216         sqlite3_value *p = 0;
1217         if( op!=SQLITE_INSERT ){
1218           pSession->hook.xOld(pSession->hook.pCtx, i, &p);
1219         }else if( pTab->abPK[i] ){
1220           pSession->hook.xNew(pSession->hook.pCtx, i, &p);
1221         }
1222         sessionSerializeValue(&pChange->aRecord[nByte], p, &nByte);
1223       }
1224 
1225       /* Add the change to the hash-table */
1226       if( pSession->bIndirect || pSession->hook.xDepth(pSession->hook.pCtx) ){
1227         pChange->bIndirect = 1;
1228       }
1229       pChange->nRecord = nByte;
1230       pChange->op = op;
1231       pChange->pNext = pTab->apChange[iHash];
1232       pTab->apChange[iHash] = pChange;
1233 
1234     }else if( pC->bIndirect ){
1235       /* If the existing change is considered "indirect", but this current
1236       ** change is "direct", mark the change object as direct. */
1237       if( pSession->hook.xDepth(pSession->hook.pCtx)==0
1238        && pSession->bIndirect==0
1239       ){
1240         pC->bIndirect = 0;
1241       }
1242     }
1243   }
1244 
1245   /* If an error has occurred, mark the session object as failed. */
1246  error_out:
1247   if( pTab->bStat1 ){
1248     pSession->hook = stat1.hook;
1249   }
1250   if( rc!=SQLITE_OK ){
1251     pSession->rc = rc;
1252   }
1253 }
1254 
1255 static int sessionFindTable(
1256   sqlite3_session *pSession,
1257   const char *zName,
1258   SessionTable **ppTab
1259 ){
1260   int rc = SQLITE_OK;
1261   int nName = sqlite3Strlen30(zName);
1262   SessionTable *pRet;
1263 
1264   /* Search for an existing table */
1265   for(pRet=pSession->pTable; pRet; pRet=pRet->pNext){
1266     if( 0==sqlite3_strnicmp(pRet->zName, zName, nName+1) ) break;
1267   }
1268 
1269   if( pRet==0 && pSession->bAutoAttach ){
1270     /* If there is a table-filter configured, invoke it. If it returns 0,
1271     ** do not automatically add the new table. */
1272     if( pSession->xTableFilter==0
1273      || pSession->xTableFilter(pSession->pFilterCtx, zName)
1274     ){
1275       rc = sqlite3session_attach(pSession, zName);
1276       if( rc==SQLITE_OK ){
1277         for(pRet=pSession->pTable; pRet->pNext; pRet=pRet->pNext);
1278         assert( 0==sqlite3_strnicmp(pRet->zName, zName, nName+1) );
1279       }
1280     }
1281   }
1282 
1283   assert( rc==SQLITE_OK || pRet==0 );
1284   *ppTab = pRet;
1285   return rc;
1286 }
1287 
1288 /*
1289 ** The 'pre-update' hook registered by this module with SQLite databases.
1290 */
1291 static void xPreUpdate(
1292   void *pCtx,                     /* Copy of third arg to preupdate_hook() */
1293   sqlite3 *db,                    /* Database handle */
1294   int op,                         /* SQLITE_UPDATE, DELETE or INSERT */
1295   char const *zDb,                /* Database name */
1296   char const *zName,              /* Table name */
1297   sqlite3_int64 iKey1,            /* Rowid of row about to be deleted/updated */
1298   sqlite3_int64 iKey2             /* New rowid value (for a rowid UPDATE) */
1299 ){
1300   sqlite3_session *pSession;
1301   int nDb = sqlite3Strlen30(zDb);
1302 
1303   assert( sqlite3_mutex_held(db->mutex) );
1304 
1305   for(pSession=(sqlite3_session *)pCtx; pSession; pSession=pSession->pNext){
1306     SessionTable *pTab;
1307 
1308     /* If this session is attached to a different database ("main", "temp"
1309     ** etc.), or if it is not currently enabled, there is nothing to do. Skip
1310     ** to the next session object attached to this database. */
1311     if( pSession->bEnable==0 ) continue;
1312     if( pSession->rc ) continue;
1313     if( sqlite3_strnicmp(zDb, pSession->zDb, nDb+1) ) continue;
1314 
1315     pSession->rc = sessionFindTable(pSession, zName, &pTab);
1316     if( pTab ){
1317       assert( pSession->rc==SQLITE_OK );
1318       sessionPreupdateOneChange(op, pSession, pTab);
1319       if( op==SQLITE_UPDATE ){
1320         sessionPreupdateOneChange(SQLITE_INSERT, pSession, pTab);
1321       }
1322     }
1323   }
1324 }
1325 
1326 /*
1327 ** The pre-update hook implementations.
1328 */
1329 static int sessionPreupdateOld(void *pCtx, int iVal, sqlite3_value **ppVal){
1330   return sqlite3_preupdate_old((sqlite3*)pCtx, iVal, ppVal);
1331 }
1332 static int sessionPreupdateNew(void *pCtx, int iVal, sqlite3_value **ppVal){
1333   return sqlite3_preupdate_new((sqlite3*)pCtx, iVal, ppVal);
1334 }
1335 static int sessionPreupdateCount(void *pCtx){
1336   return sqlite3_preupdate_count((sqlite3*)pCtx);
1337 }
1338 static int sessionPreupdateDepth(void *pCtx){
1339   return sqlite3_preupdate_depth((sqlite3*)pCtx);
1340 }
1341 
1342 /*
1343 ** Install the pre-update hooks on the session object passed as the only
1344 ** argument.
1345 */
1346 static void sessionPreupdateHooks(
1347   sqlite3_session *pSession
1348 ){
1349   pSession->hook.pCtx = (void*)pSession->db;
1350   pSession->hook.xOld = sessionPreupdateOld;
1351   pSession->hook.xNew = sessionPreupdateNew;
1352   pSession->hook.xCount = sessionPreupdateCount;
1353   pSession->hook.xDepth = sessionPreupdateDepth;
1354 }
1355 
1356 typedef struct SessionDiffCtx SessionDiffCtx;
1357 struct SessionDiffCtx {
1358   sqlite3_stmt *pStmt;
1359   int nOldOff;
1360 };
1361 
1362 /*
1363 ** The diff hook implementations.
1364 */
1365 static int sessionDiffOld(void *pCtx, int iVal, sqlite3_value **ppVal){
1366   SessionDiffCtx *p = (SessionDiffCtx*)pCtx;
1367   *ppVal = sqlite3_column_value(p->pStmt, iVal+p->nOldOff);
1368   return SQLITE_OK;
1369 }
1370 static int sessionDiffNew(void *pCtx, int iVal, sqlite3_value **ppVal){
1371   SessionDiffCtx *p = (SessionDiffCtx*)pCtx;
1372   *ppVal = sqlite3_column_value(p->pStmt, iVal);
1373    return SQLITE_OK;
1374 }
1375 static int sessionDiffCount(void *pCtx){
1376   SessionDiffCtx *p = (SessionDiffCtx*)pCtx;
1377   return p->nOldOff ? p->nOldOff : sqlite3_column_count(p->pStmt);
1378 }
1379 static int sessionDiffDepth(void *pCtx){
1380   return 0;
1381 }
1382 
1383 /*
1384 ** Install the diff hooks on the session object passed as the only
1385 ** argument.
1386 */
1387 static void sessionDiffHooks(
1388   sqlite3_session *pSession,
1389   SessionDiffCtx *pDiffCtx
1390 ){
1391   pSession->hook.pCtx = (void*)pDiffCtx;
1392   pSession->hook.xOld = sessionDiffOld;
1393   pSession->hook.xNew = sessionDiffNew;
1394   pSession->hook.xCount = sessionDiffCount;
1395   pSession->hook.xDepth = sessionDiffDepth;
1396 }
1397 
1398 static char *sessionExprComparePK(
1399   int nCol,
1400   const char *zDb1, const char *zDb2,
1401   const char *zTab,
1402   const char **azCol, u8 *abPK
1403 ){
1404   int i;
1405   const char *zSep = "";
1406   char *zRet = 0;
1407 
1408   for(i=0; i<nCol; i++){
1409     if( abPK[i] ){
1410       zRet = sqlite3_mprintf("%z%s\"%w\".\"%w\".\"%w\"=\"%w\".\"%w\".\"%w\"",
1411           zRet, zSep, zDb1, zTab, azCol[i], zDb2, zTab, azCol[i]
1412       );
1413       zSep = " AND ";
1414       if( zRet==0 ) break;
1415     }
1416   }
1417 
1418   return zRet;
1419 }
1420 
1421 static char *sessionExprCompareOther(
1422   int nCol,
1423   const char *zDb1, const char *zDb2,
1424   const char *zTab,
1425   const char **azCol, u8 *abPK
1426 ){
1427   int i;
1428   const char *zSep = "";
1429   char *zRet = 0;
1430   int bHave = 0;
1431 
1432   for(i=0; i<nCol; i++){
1433     if( abPK[i]==0 ){
1434       bHave = 1;
1435       zRet = sqlite3_mprintf(
1436           "%z%s\"%w\".\"%w\".\"%w\" IS NOT \"%w\".\"%w\".\"%w\"",
1437           zRet, zSep, zDb1, zTab, azCol[i], zDb2, zTab, azCol[i]
1438       );
1439       zSep = " OR ";
1440       if( zRet==0 ) break;
1441     }
1442   }
1443 
1444   if( bHave==0 ){
1445     assert( zRet==0 );
1446     zRet = sqlite3_mprintf("0");
1447   }
1448 
1449   return zRet;
1450 }
1451 
1452 static char *sessionSelectFindNew(
1453   int nCol,
1454   const char *zDb1,      /* Pick rows in this db only */
1455   const char *zDb2,      /* But not in this one */
1456   const char *zTbl,      /* Table name */
1457   const char *zExpr
1458 ){
1459   char *zRet = sqlite3_mprintf(
1460       "SELECT * FROM \"%w\".\"%w\" WHERE NOT EXISTS ("
1461       "  SELECT 1 FROM \"%w\".\"%w\" WHERE %s"
1462       ")",
1463       zDb1, zTbl, zDb2, zTbl, zExpr
1464   );
1465   return zRet;
1466 }
1467 
1468 static int sessionDiffFindNew(
1469   int op,
1470   sqlite3_session *pSession,
1471   SessionTable *pTab,
1472   const char *zDb1,
1473   const char *zDb2,
1474   char *zExpr
1475 ){
1476   int rc = SQLITE_OK;
1477   char *zStmt = sessionSelectFindNew(pTab->nCol, zDb1, zDb2, pTab->zName,zExpr);
1478 
1479   if( zStmt==0 ){
1480     rc = SQLITE_NOMEM;
1481   }else{
1482     sqlite3_stmt *pStmt;
1483     rc = sqlite3_prepare(pSession->db, zStmt, -1, &pStmt, 0);
1484     if( rc==SQLITE_OK ){
1485       SessionDiffCtx *pDiffCtx = (SessionDiffCtx*)pSession->hook.pCtx;
1486       pDiffCtx->pStmt = pStmt;
1487       pDiffCtx->nOldOff = 0;
1488       while( SQLITE_ROW==sqlite3_step(pStmt) ){
1489         sessionPreupdateOneChange(op, pSession, pTab);
1490       }
1491       rc = sqlite3_finalize(pStmt);
1492     }
1493     sqlite3_free(zStmt);
1494   }
1495 
1496   return rc;
1497 }
1498 
1499 static int sessionDiffFindModified(
1500   sqlite3_session *pSession,
1501   SessionTable *pTab,
1502   const char *zFrom,
1503   const char *zExpr
1504 ){
1505   int rc = SQLITE_OK;
1506 
1507   char *zExpr2 = sessionExprCompareOther(pTab->nCol,
1508       pSession->zDb, zFrom, pTab->zName, pTab->azCol, pTab->abPK
1509   );
1510   if( zExpr2==0 ){
1511     rc = SQLITE_NOMEM;
1512   }else{
1513     char *zStmt = sqlite3_mprintf(
1514         "SELECT * FROM \"%w\".\"%w\", \"%w\".\"%w\" WHERE %s AND (%z)",
1515         pSession->zDb, pTab->zName, zFrom, pTab->zName, zExpr, zExpr2
1516     );
1517     if( zStmt==0 ){
1518       rc = SQLITE_NOMEM;
1519     }else{
1520       sqlite3_stmt *pStmt;
1521       rc = sqlite3_prepare(pSession->db, zStmt, -1, &pStmt, 0);
1522 
1523       if( rc==SQLITE_OK ){
1524         SessionDiffCtx *pDiffCtx = (SessionDiffCtx*)pSession->hook.pCtx;
1525         pDiffCtx->pStmt = pStmt;
1526         pDiffCtx->nOldOff = pTab->nCol;
1527         while( SQLITE_ROW==sqlite3_step(pStmt) ){
1528           sessionPreupdateOneChange(SQLITE_UPDATE, pSession, pTab);
1529         }
1530         rc = sqlite3_finalize(pStmt);
1531       }
1532       sqlite3_free(zStmt);
1533     }
1534   }
1535 
1536   return rc;
1537 }
1538 
1539 int sqlite3session_diff(
1540   sqlite3_session *pSession,
1541   const char *zFrom,
1542   const char *zTbl,
1543   char **pzErrMsg
1544 ){
1545   const char *zDb = pSession->zDb;
1546   int rc = pSession->rc;
1547   SessionDiffCtx d;
1548 
1549   memset(&d, 0, sizeof(d));
1550   sessionDiffHooks(pSession, &d);
1551 
1552   sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
1553   if( pzErrMsg ) *pzErrMsg = 0;
1554   if( rc==SQLITE_OK ){
1555     char *zExpr = 0;
1556     sqlite3 *db = pSession->db;
1557     SessionTable *pTo;            /* Table zTbl */
1558 
1559     /* Locate and if necessary initialize the target table object */
1560     rc = sessionFindTable(pSession, zTbl, &pTo);
1561     if( pTo==0 ) goto diff_out;
1562     if( sessionInitTable(pSession, pTo) ){
1563       rc = pSession->rc;
1564       goto diff_out;
1565     }
1566 
1567     /* Check the table schemas match */
1568     if( rc==SQLITE_OK ){
1569       int bHasPk = 0;
1570       int bMismatch = 0;
1571       int nCol;                   /* Columns in zFrom.zTbl */
1572       u8 *abPK;
1573       const char **azCol = 0;
1574       rc = sessionTableInfo(db, zFrom, zTbl, &nCol, 0, &azCol, &abPK);
1575       if( rc==SQLITE_OK ){
1576         if( pTo->nCol!=nCol ){
1577           bMismatch = 1;
1578         }else{
1579           int i;
1580           for(i=0; i<nCol; i++){
1581             if( pTo->abPK[i]!=abPK[i] ) bMismatch = 1;
1582             if( sqlite3_stricmp(azCol[i], pTo->azCol[i]) ) bMismatch = 1;
1583             if( abPK[i] ) bHasPk = 1;
1584           }
1585         }
1586       }
1587       sqlite3_free((char*)azCol);
1588       if( bMismatch ){
1589         *pzErrMsg = sqlite3_mprintf("table schemas do not match");
1590         rc = SQLITE_SCHEMA;
1591       }
1592       if( bHasPk==0 ){
1593         /* Ignore tables with no primary keys */
1594         goto diff_out;
1595       }
1596     }
1597 
1598     if( rc==SQLITE_OK ){
1599       zExpr = sessionExprComparePK(pTo->nCol,
1600           zDb, zFrom, pTo->zName, pTo->azCol, pTo->abPK
1601       );
1602     }
1603 
1604     /* Find new rows */
1605     if( rc==SQLITE_OK ){
1606       rc = sessionDiffFindNew(SQLITE_INSERT, pSession, pTo, zDb, zFrom, zExpr);
1607     }
1608 
1609     /* Find old rows */
1610     if( rc==SQLITE_OK ){
1611       rc = sessionDiffFindNew(SQLITE_DELETE, pSession, pTo, zFrom, zDb, zExpr);
1612     }
1613 
1614     /* Find modified rows */
1615     if( rc==SQLITE_OK ){
1616       rc = sessionDiffFindModified(pSession, pTo, zFrom, zExpr);
1617     }
1618 
1619     sqlite3_free(zExpr);
1620   }
1621 
1622  diff_out:
1623   sessionPreupdateHooks(pSession);
1624   sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
1625   return rc;
1626 }
1627 
1628 /*
1629 ** Create a session object. This session object will record changes to
1630 ** database zDb attached to connection db.
1631 */
1632 int sqlite3session_create(
1633   sqlite3 *db,                    /* Database handle */
1634   const char *zDb,                /* Name of db (e.g. "main") */
1635   sqlite3_session **ppSession     /* OUT: New session object */
1636 ){
1637   sqlite3_session *pNew;          /* Newly allocated session object */
1638   sqlite3_session *pOld;          /* Session object already attached to db */
1639   int nDb = sqlite3Strlen30(zDb); /* Length of zDb in bytes */
1640 
1641   /* Zero the output value in case an error occurs. */
1642   *ppSession = 0;
1643 
1644   /* Allocate and populate the new session object. */
1645   pNew = (sqlite3_session *)sqlite3_malloc(sizeof(sqlite3_session) + nDb + 1);
1646   if( !pNew ) return SQLITE_NOMEM;
1647   memset(pNew, 0, sizeof(sqlite3_session));
1648   pNew->db = db;
1649   pNew->zDb = (char *)&pNew[1];
1650   pNew->bEnable = 1;
1651   memcpy(pNew->zDb, zDb, nDb+1);
1652   sessionPreupdateHooks(pNew);
1653 
1654   /* Add the new session object to the linked list of session objects
1655   ** attached to database handle $db. Do this under the cover of the db
1656   ** handle mutex.  */
1657   sqlite3_mutex_enter(sqlite3_db_mutex(db));
1658   pOld = (sqlite3_session*)sqlite3_preupdate_hook(db, xPreUpdate, (void*)pNew);
1659   pNew->pNext = pOld;
1660   sqlite3_mutex_leave(sqlite3_db_mutex(db));
1661 
1662   *ppSession = pNew;
1663   return SQLITE_OK;
1664 }
1665 
1666 /*
1667 ** Free the list of table objects passed as the first argument. The contents
1668 ** of the changed-rows hash tables are also deleted.
1669 */
1670 static void sessionDeleteTable(SessionTable *pList){
1671   SessionTable *pNext;
1672   SessionTable *pTab;
1673 
1674   for(pTab=pList; pTab; pTab=pNext){
1675     int i;
1676     pNext = pTab->pNext;
1677     for(i=0; i<pTab->nChange; i++){
1678       SessionChange *p;
1679       SessionChange *pNextChange;
1680       for(p=pTab->apChange[i]; p; p=pNextChange){
1681         pNextChange = p->pNext;
1682         sqlite3_free(p);
1683       }
1684     }
1685     sqlite3_free((char*)pTab->azCol);  /* cast works around VC++ bug */
1686     sqlite3_free(pTab->apChange);
1687     sqlite3_free(pTab);
1688   }
1689 }
1690 
1691 /*
1692 ** Delete a session object previously allocated using sqlite3session_create().
1693 */
1694 void sqlite3session_delete(sqlite3_session *pSession){
1695   sqlite3 *db = pSession->db;
1696   sqlite3_session *pHead;
1697   sqlite3_session **pp;
1698 
1699   /* Unlink the session from the linked list of sessions attached to the
1700   ** database handle. Hold the db mutex while doing so.  */
1701   sqlite3_mutex_enter(sqlite3_db_mutex(db));
1702   pHead = (sqlite3_session*)sqlite3_preupdate_hook(db, 0, 0);
1703   for(pp=&pHead; ALWAYS((*pp)!=0); pp=&((*pp)->pNext)){
1704     if( (*pp)==pSession ){
1705       *pp = (*pp)->pNext;
1706       if( pHead ) sqlite3_preupdate_hook(db, xPreUpdate, (void*)pHead);
1707       break;
1708     }
1709   }
1710   sqlite3_mutex_leave(sqlite3_db_mutex(db));
1711   sqlite3ValueFree(pSession->pZeroBlob);
1712 
1713   /* Delete all attached table objects. And the contents of their
1714   ** associated hash-tables. */
1715   sessionDeleteTable(pSession->pTable);
1716 
1717   /* Free the session object itself. */
1718   sqlite3_free(pSession);
1719 }
1720 
1721 /*
1722 ** Set a table filter on a Session Object.
1723 */
1724 void sqlite3session_table_filter(
1725   sqlite3_session *pSession,
1726   int(*xFilter)(void*, const char*),
1727   void *pCtx                      /* First argument passed to xFilter */
1728 ){
1729   pSession->bAutoAttach = 1;
1730   pSession->pFilterCtx = pCtx;
1731   pSession->xTableFilter = xFilter;
1732 }
1733 
1734 /*
1735 ** Attach a table to a session. All subsequent changes made to the table
1736 ** while the session object is enabled will be recorded.
1737 **
1738 ** Only tables that have a PRIMARY KEY defined may be attached. It does
1739 ** not matter if the PRIMARY KEY is an "INTEGER PRIMARY KEY" (rowid alias)
1740 ** or not.
1741 */
1742 int sqlite3session_attach(
1743   sqlite3_session *pSession,      /* Session object */
1744   const char *zName               /* Table name */
1745 ){
1746   int rc = SQLITE_OK;
1747   sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
1748 
1749   if( !zName ){
1750     pSession->bAutoAttach = 1;
1751   }else{
1752     SessionTable *pTab;           /* New table object (if required) */
1753     int nName;                    /* Number of bytes in string zName */
1754 
1755     /* First search for an existing entry. If one is found, this call is
1756     ** a no-op. Return early. */
1757     nName = sqlite3Strlen30(zName);
1758     for(pTab=pSession->pTable; pTab; pTab=pTab->pNext){
1759       if( 0==sqlite3_strnicmp(pTab->zName, zName, nName+1) ) break;
1760     }
1761 
1762     if( !pTab ){
1763       /* Allocate new SessionTable object. */
1764       pTab = (SessionTable *)sqlite3_malloc(sizeof(SessionTable) + nName + 1);
1765       if( !pTab ){
1766         rc = SQLITE_NOMEM;
1767       }else{
1768         /* Populate the new SessionTable object and link it into the list.
1769         ** The new object must be linked onto the end of the list, not
1770         ** simply added to the start of it in order to ensure that tables
1771         ** appear in the correct order when a changeset or patchset is
1772         ** eventually generated. */
1773         SessionTable **ppTab;
1774         memset(pTab, 0, sizeof(SessionTable));
1775         pTab->zName = (char *)&pTab[1];
1776         memcpy(pTab->zName, zName, nName+1);
1777         for(ppTab=&pSession->pTable; *ppTab; ppTab=&(*ppTab)->pNext);
1778         *ppTab = pTab;
1779       }
1780     }
1781   }
1782 
1783   sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
1784   return rc;
1785 }
1786 
1787 /*
1788 ** Ensure that there is room in the buffer to append nByte bytes of data.
1789 ** If not, use sqlite3_realloc() to grow the buffer so that there is.
1790 **
1791 ** If successful, return zero. Otherwise, if an OOM condition is encountered,
1792 ** set *pRc to SQLITE_NOMEM and return non-zero.
1793 */
1794 static int sessionBufferGrow(SessionBuffer *p, int nByte, int *pRc){
1795   if( *pRc==SQLITE_OK && p->nAlloc-p->nBuf<nByte ){
1796     u8 *aNew;
1797     i64 nNew = p->nAlloc ? p->nAlloc : 128;
1798     do {
1799       nNew = nNew*2;
1800     }while( (nNew-p->nBuf)<nByte );
1801 
1802     aNew = (u8 *)sqlite3_realloc64(p->aBuf, nNew);
1803     if( 0==aNew ){
1804       *pRc = SQLITE_NOMEM;
1805     }else{
1806       p->aBuf = aNew;
1807       p->nAlloc = nNew;
1808     }
1809   }
1810   return (*pRc!=SQLITE_OK);
1811 }
1812 
1813 /*
1814 ** Append the value passed as the second argument to the buffer passed
1815 ** as the first.
1816 **
1817 ** This function is a no-op if *pRc is non-zero when it is called.
1818 ** Otherwise, if an error occurs, *pRc is set to an SQLite error code
1819 ** before returning.
1820 */
1821 static void sessionAppendValue(SessionBuffer *p, sqlite3_value *pVal, int *pRc){
1822   int rc = *pRc;
1823   if( rc==SQLITE_OK ){
1824     int nByte = 0;
1825     rc = sessionSerializeValue(0, pVal, &nByte);
1826     sessionBufferGrow(p, nByte, &rc);
1827     if( rc==SQLITE_OK ){
1828       rc = sessionSerializeValue(&p->aBuf[p->nBuf], pVal, 0);
1829       p->nBuf += nByte;
1830     }else{
1831       *pRc = rc;
1832     }
1833   }
1834 }
1835 
1836 /*
1837 ** This function is a no-op if *pRc is other than SQLITE_OK when it is
1838 ** called. Otherwise, append a single byte to the buffer.
1839 **
1840 ** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
1841 ** returning.
1842 */
1843 static void sessionAppendByte(SessionBuffer *p, u8 v, int *pRc){
1844   if( 0==sessionBufferGrow(p, 1, pRc) ){
1845     p->aBuf[p->nBuf++] = v;
1846   }
1847 }
1848 
1849 /*
1850 ** This function is a no-op if *pRc is other than SQLITE_OK when it is
1851 ** called. Otherwise, append a single varint to the buffer.
1852 **
1853 ** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
1854 ** returning.
1855 */
1856 static void sessionAppendVarint(SessionBuffer *p, int v, int *pRc){
1857   if( 0==sessionBufferGrow(p, 9, pRc) ){
1858     p->nBuf += sessionVarintPut(&p->aBuf[p->nBuf], v);
1859   }
1860 }
1861 
1862 /*
1863 ** This function is a no-op if *pRc is other than SQLITE_OK when it is
1864 ** called. Otherwise, append a blob of data to the buffer.
1865 **
1866 ** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
1867 ** returning.
1868 */
1869 static void sessionAppendBlob(
1870   SessionBuffer *p,
1871   const u8 *aBlob,
1872   int nBlob,
1873   int *pRc
1874 ){
1875   if( nBlob>0 && 0==sessionBufferGrow(p, nBlob, pRc) ){
1876     memcpy(&p->aBuf[p->nBuf], aBlob, nBlob);
1877     p->nBuf += nBlob;
1878   }
1879 }
1880 
1881 /*
1882 ** This function is a no-op if *pRc is other than SQLITE_OK when it is
1883 ** called. Otherwise, append a string to the buffer. All bytes in the string
1884 ** up to (but not including) the nul-terminator are written to the buffer.
1885 **
1886 ** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
1887 ** returning.
1888 */
1889 static void sessionAppendStr(
1890   SessionBuffer *p,
1891   const char *zStr,
1892   int *pRc
1893 ){
1894   int nStr = sqlite3Strlen30(zStr);
1895   if( 0==sessionBufferGrow(p, nStr, pRc) ){
1896     memcpy(&p->aBuf[p->nBuf], zStr, nStr);
1897     p->nBuf += nStr;
1898   }
1899 }
1900 
1901 /*
1902 ** This function is a no-op if *pRc is other than SQLITE_OK when it is
1903 ** called. Otherwise, append the string representation of integer iVal
1904 ** to the buffer. No nul-terminator is written.
1905 **
1906 ** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
1907 ** returning.
1908 */
1909 static void sessionAppendInteger(
1910   SessionBuffer *p,               /* Buffer to append to */
1911   int iVal,                       /* Value to write the string rep. of */
1912   int *pRc                        /* IN/OUT: Error code */
1913 ){
1914   char aBuf[24];
1915   sqlite3_snprintf(sizeof(aBuf)-1, aBuf, "%d", iVal);
1916   sessionAppendStr(p, aBuf, pRc);
1917 }
1918 
1919 /*
1920 ** This function is a no-op if *pRc is other than SQLITE_OK when it is
1921 ** called. Otherwise, append the string zStr enclosed in quotes (") and
1922 ** with any embedded quote characters escaped to the buffer. No
1923 ** nul-terminator byte is written.
1924 **
1925 ** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
1926 ** returning.
1927 */
1928 static void sessionAppendIdent(
1929   SessionBuffer *p,               /* Buffer to a append to */
1930   const char *zStr,               /* String to quote, escape and append */
1931   int *pRc                        /* IN/OUT: Error code */
1932 ){
1933   int nStr = sqlite3Strlen30(zStr)*2 + 2 + 1;
1934   if( 0==sessionBufferGrow(p, nStr, pRc) ){
1935     char *zOut = (char *)&p->aBuf[p->nBuf];
1936     const char *zIn = zStr;
1937     *zOut++ = '"';
1938     while( *zIn ){
1939       if( *zIn=='"' ) *zOut++ = '"';
1940       *zOut++ = *(zIn++);
1941     }
1942     *zOut++ = '"';
1943     p->nBuf = (int)((u8 *)zOut - p->aBuf);
1944   }
1945 }
1946 
1947 /*
1948 ** This function is a no-op if *pRc is other than SQLITE_OK when it is
1949 ** called. Otherwse, it appends the serialized version of the value stored
1950 ** in column iCol of the row that SQL statement pStmt currently points
1951 ** to to the buffer.
1952 */
1953 static void sessionAppendCol(
1954   SessionBuffer *p,               /* Buffer to append to */
1955   sqlite3_stmt *pStmt,            /* Handle pointing to row containing value */
1956   int iCol,                       /* Column to read value from */
1957   int *pRc                        /* IN/OUT: Error code */
1958 ){
1959   if( *pRc==SQLITE_OK ){
1960     int eType = sqlite3_column_type(pStmt, iCol);
1961     sessionAppendByte(p, (u8)eType, pRc);
1962     if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
1963       sqlite3_int64 i;
1964       u8 aBuf[8];
1965       if( eType==SQLITE_INTEGER ){
1966         i = sqlite3_column_int64(pStmt, iCol);
1967       }else{
1968         double r = sqlite3_column_double(pStmt, iCol);
1969         memcpy(&i, &r, 8);
1970       }
1971       sessionPutI64(aBuf, i);
1972       sessionAppendBlob(p, aBuf, 8, pRc);
1973     }
1974     if( eType==SQLITE_BLOB || eType==SQLITE_TEXT ){
1975       u8 *z;
1976       int nByte;
1977       if( eType==SQLITE_BLOB ){
1978         z = (u8 *)sqlite3_column_blob(pStmt, iCol);
1979       }else{
1980         z = (u8 *)sqlite3_column_text(pStmt, iCol);
1981       }
1982       nByte = sqlite3_column_bytes(pStmt, iCol);
1983       if( z || (eType==SQLITE_BLOB && nByte==0) ){
1984         sessionAppendVarint(p, nByte, pRc);
1985         sessionAppendBlob(p, z, nByte, pRc);
1986       }else{
1987         *pRc = SQLITE_NOMEM;
1988       }
1989     }
1990   }
1991 }
1992 
1993 /*
1994 **
1995 ** This function appends an update change to the buffer (see the comments
1996 ** under "CHANGESET FORMAT" at the top of the file). An update change
1997 ** consists of:
1998 **
1999 **   1 byte:  SQLITE_UPDATE (0x17)
2000 **   n bytes: old.* record (see RECORD FORMAT)
2001 **   m bytes: new.* record (see RECORD FORMAT)
2002 **
2003 ** The SessionChange object passed as the third argument contains the
2004 ** values that were stored in the row when the session began (the old.*
2005 ** values). The statement handle passed as the second argument points
2006 ** at the current version of the row (the new.* values).
2007 **
2008 ** If all of the old.* values are equal to their corresponding new.* value
2009 ** (i.e. nothing has changed), then no data at all is appended to the buffer.
2010 **
2011 ** Otherwise, the old.* record contains all primary key values and the
2012 ** original values of any fields that have been modified. The new.* record
2013 ** contains the new values of only those fields that have been modified.
2014 */
2015 static int sessionAppendUpdate(
2016   SessionBuffer *pBuf,            /* Buffer to append to */
2017   int bPatchset,                  /* True for "patchset", 0 for "changeset" */
2018   sqlite3_stmt *pStmt,            /* Statement handle pointing at new row */
2019   SessionChange *p,               /* Object containing old values */
2020   u8 *abPK                        /* Boolean array - true for PK columns */
2021 ){
2022   int rc = SQLITE_OK;
2023   SessionBuffer buf2 = {0,0,0}; /* Buffer to accumulate new.* record in */
2024   int bNoop = 1;                /* Set to zero if any values are modified */
2025   int nRewind = pBuf->nBuf;     /* Set to zero if any values are modified */
2026   int i;                        /* Used to iterate through columns */
2027   u8 *pCsr = p->aRecord;        /* Used to iterate through old.* values */
2028 
2029   sessionAppendByte(pBuf, SQLITE_UPDATE, &rc);
2030   sessionAppendByte(pBuf, p->bIndirect, &rc);
2031   for(i=0; i<sqlite3_column_count(pStmt); i++){
2032     int bChanged = 0;
2033     int nAdvance;
2034     int eType = *pCsr;
2035     switch( eType ){
2036       case SQLITE_NULL:
2037         nAdvance = 1;
2038         if( sqlite3_column_type(pStmt, i)!=SQLITE_NULL ){
2039           bChanged = 1;
2040         }
2041         break;
2042 
2043       case SQLITE_FLOAT:
2044       case SQLITE_INTEGER: {
2045         nAdvance = 9;
2046         if( eType==sqlite3_column_type(pStmt, i) ){
2047           sqlite3_int64 iVal = sessionGetI64(&pCsr[1]);
2048           if( eType==SQLITE_INTEGER ){
2049             if( iVal==sqlite3_column_int64(pStmt, i) ) break;
2050           }else{
2051             double dVal;
2052             memcpy(&dVal, &iVal, 8);
2053             if( dVal==sqlite3_column_double(pStmt, i) ) break;
2054           }
2055         }
2056         bChanged = 1;
2057         break;
2058       }
2059 
2060       default: {
2061         int n;
2062         int nHdr = 1 + sessionVarintGet(&pCsr[1], &n);
2063         assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
2064         nAdvance = nHdr + n;
2065         if( eType==sqlite3_column_type(pStmt, i)
2066          && n==sqlite3_column_bytes(pStmt, i)
2067          && (n==0 || 0==memcmp(&pCsr[nHdr], sqlite3_column_blob(pStmt, i), n))
2068         ){
2069           break;
2070         }
2071         bChanged = 1;
2072       }
2073     }
2074 
2075     /* If at least one field has been modified, this is not a no-op. */
2076     if( bChanged ) bNoop = 0;
2077 
2078     /* Add a field to the old.* record. This is omitted if this modules is
2079     ** currently generating a patchset. */
2080     if( bPatchset==0 ){
2081       if( bChanged || abPK[i] ){
2082         sessionAppendBlob(pBuf, pCsr, nAdvance, &rc);
2083       }else{
2084         sessionAppendByte(pBuf, 0, &rc);
2085       }
2086     }
2087 
2088     /* Add a field to the new.* record. Or the only record if currently
2089     ** generating a patchset.  */
2090     if( bChanged || (bPatchset && abPK[i]) ){
2091       sessionAppendCol(&buf2, pStmt, i, &rc);
2092     }else{
2093       sessionAppendByte(&buf2, 0, &rc);
2094     }
2095 
2096     pCsr += nAdvance;
2097   }
2098 
2099   if( bNoop ){
2100     pBuf->nBuf = nRewind;
2101   }else{
2102     sessionAppendBlob(pBuf, buf2.aBuf, buf2.nBuf, &rc);
2103   }
2104   sqlite3_free(buf2.aBuf);
2105 
2106   return rc;
2107 }
2108 
2109 /*
2110 ** Append a DELETE change to the buffer passed as the first argument. Use
2111 ** the changeset format if argument bPatchset is zero, or the patchset
2112 ** format otherwise.
2113 */
2114 static int sessionAppendDelete(
2115   SessionBuffer *pBuf,            /* Buffer to append to */
2116   int bPatchset,                  /* True for "patchset", 0 for "changeset" */
2117   SessionChange *p,               /* Object containing old values */
2118   int nCol,                       /* Number of columns in table */
2119   u8 *abPK                        /* Boolean array - true for PK columns */
2120 ){
2121   int rc = SQLITE_OK;
2122 
2123   sessionAppendByte(pBuf, SQLITE_DELETE, &rc);
2124   sessionAppendByte(pBuf, p->bIndirect, &rc);
2125 
2126   if( bPatchset==0 ){
2127     sessionAppendBlob(pBuf, p->aRecord, p->nRecord, &rc);
2128   }else{
2129     int i;
2130     u8 *a = p->aRecord;
2131     for(i=0; i<nCol; i++){
2132       u8 *pStart = a;
2133       int eType = *a++;
2134 
2135       switch( eType ){
2136         case 0:
2137         case SQLITE_NULL:
2138           assert( abPK[i]==0 );
2139           break;
2140 
2141         case SQLITE_FLOAT:
2142         case SQLITE_INTEGER:
2143           a += 8;
2144           break;
2145 
2146         default: {
2147           int n;
2148           a += sessionVarintGet(a, &n);
2149           a += n;
2150           break;
2151         }
2152       }
2153       if( abPK[i] ){
2154         sessionAppendBlob(pBuf, pStart, (int)(a-pStart), &rc);
2155       }
2156     }
2157     assert( (a - p->aRecord)==p->nRecord );
2158   }
2159 
2160   return rc;
2161 }
2162 
2163 /*
2164 ** Formulate and prepare a SELECT statement to retrieve a row from table
2165 ** zTab in database zDb based on its primary key. i.e.
2166 **
2167 **   SELECT * FROM zDb.zTab WHERE pk1 = ? AND pk2 = ? AND ...
2168 */
2169 static int sessionSelectStmt(
2170   sqlite3 *db,                    /* Database handle */
2171   const char *zDb,                /* Database name */
2172   const char *zTab,               /* Table name */
2173   int nCol,                       /* Number of columns in table */
2174   const char **azCol,             /* Names of table columns */
2175   u8 *abPK,                       /* PRIMARY KEY  array */
2176   sqlite3_stmt **ppStmt           /* OUT: Prepared SELECT statement */
2177 ){
2178   int rc = SQLITE_OK;
2179   char *zSql = 0;
2180   int nSql = -1;
2181 
2182   if( 0==sqlite3_stricmp("sqlite_stat1", zTab) ){
2183     zSql = sqlite3_mprintf(
2184         "SELECT tbl, ?2, stat FROM %Q.sqlite_stat1 WHERE tbl IS ?1 AND "
2185         "idx IS (CASE WHEN ?2=X'' THEN NULL ELSE ?2 END)", zDb
2186     );
2187     if( zSql==0 ) rc = SQLITE_NOMEM;
2188   }else{
2189     int i;
2190     const char *zSep = "";
2191     SessionBuffer buf = {0, 0, 0};
2192 
2193     sessionAppendStr(&buf, "SELECT * FROM ", &rc);
2194     sessionAppendIdent(&buf, zDb, &rc);
2195     sessionAppendStr(&buf, ".", &rc);
2196     sessionAppendIdent(&buf, zTab, &rc);
2197     sessionAppendStr(&buf, " WHERE ", &rc);
2198     for(i=0; i<nCol; i++){
2199       if( abPK[i] ){
2200         sessionAppendStr(&buf, zSep, &rc);
2201         sessionAppendIdent(&buf, azCol[i], &rc);
2202         sessionAppendStr(&buf, " IS ?", &rc);
2203         sessionAppendInteger(&buf, i+1, &rc);
2204         zSep = " AND ";
2205       }
2206     }
2207     zSql = (char*)buf.aBuf;
2208     nSql = buf.nBuf;
2209   }
2210 
2211   if( rc==SQLITE_OK ){
2212     rc = sqlite3_prepare_v2(db, zSql, nSql, ppStmt, 0);
2213   }
2214   sqlite3_free(zSql);
2215   return rc;
2216 }
2217 
2218 /*
2219 ** Bind the PRIMARY KEY values from the change passed in argument pChange
2220 ** to the SELECT statement passed as the first argument. The SELECT statement
2221 ** is as prepared by function sessionSelectStmt().
2222 **
2223 ** Return SQLITE_OK if all PK values are successfully bound, or an SQLite
2224 ** error code (e.g. SQLITE_NOMEM) otherwise.
2225 */
2226 static int sessionSelectBind(
2227   sqlite3_stmt *pSelect,          /* SELECT from sessionSelectStmt() */
2228   int nCol,                       /* Number of columns in table */
2229   u8 *abPK,                       /* PRIMARY KEY array */
2230   SessionChange *pChange          /* Change structure */
2231 ){
2232   int i;
2233   int rc = SQLITE_OK;
2234   u8 *a = pChange->aRecord;
2235 
2236   for(i=0; i<nCol && rc==SQLITE_OK; i++){
2237     int eType = *a++;
2238 
2239     switch( eType ){
2240       case 0:
2241       case SQLITE_NULL:
2242         assert( abPK[i]==0 );
2243         break;
2244 
2245       case SQLITE_INTEGER: {
2246         if( abPK[i] ){
2247           i64 iVal = sessionGetI64(a);
2248           rc = sqlite3_bind_int64(pSelect, i+1, iVal);
2249         }
2250         a += 8;
2251         break;
2252       }
2253 
2254       case SQLITE_FLOAT: {
2255         if( abPK[i] ){
2256           double rVal;
2257           i64 iVal = sessionGetI64(a);
2258           memcpy(&rVal, &iVal, 8);
2259           rc = sqlite3_bind_double(pSelect, i+1, rVal);
2260         }
2261         a += 8;
2262         break;
2263       }
2264 
2265       case SQLITE_TEXT: {
2266         int n;
2267         a += sessionVarintGet(a, &n);
2268         if( abPK[i] ){
2269           rc = sqlite3_bind_text(pSelect, i+1, (char *)a, n, SQLITE_TRANSIENT);
2270         }
2271         a += n;
2272         break;
2273       }
2274 
2275       default: {
2276         int n;
2277         assert( eType==SQLITE_BLOB );
2278         a += sessionVarintGet(a, &n);
2279         if( abPK[i] ){
2280           rc = sqlite3_bind_blob(pSelect, i+1, a, n, SQLITE_TRANSIENT);
2281         }
2282         a += n;
2283         break;
2284       }
2285     }
2286   }
2287 
2288   return rc;
2289 }
2290 
2291 /*
2292 ** This function is a no-op if *pRc is set to other than SQLITE_OK when it
2293 ** is called. Otherwise, append a serialized table header (part of the binary
2294 ** changeset format) to buffer *pBuf. If an error occurs, set *pRc to an
2295 ** SQLite error code before returning.
2296 */
2297 static void sessionAppendTableHdr(
2298   SessionBuffer *pBuf,            /* Append header to this buffer */
2299   int bPatchset,                  /* Use the patchset format if true */
2300   SessionTable *pTab,             /* Table object to append header for */
2301   int *pRc                        /* IN/OUT: Error code */
2302 ){
2303   /* Write a table header */
2304   sessionAppendByte(pBuf, (bPatchset ? 'P' : 'T'), pRc);
2305   sessionAppendVarint(pBuf, pTab->nCol, pRc);
2306   sessionAppendBlob(pBuf, pTab->abPK, pTab->nCol, pRc);
2307   sessionAppendBlob(pBuf, (u8 *)pTab->zName, (int)strlen(pTab->zName)+1, pRc);
2308 }
2309 
2310 /*
2311 ** Generate either a changeset (if argument bPatchset is zero) or a patchset
2312 ** (if it is non-zero) based on the current contents of the session object
2313 ** passed as the first argument.
2314 **
2315 ** If no error occurs, SQLITE_OK is returned and the new changeset/patchset
2316 ** stored in output variables *pnChangeset and *ppChangeset. Or, if an error
2317 ** occurs, an SQLite error code is returned and both output variables set
2318 ** to 0.
2319 */
2320 static int sessionGenerateChangeset(
2321   sqlite3_session *pSession,      /* Session object */
2322   int bPatchset,                  /* True for patchset, false for changeset */
2323   int (*xOutput)(void *pOut, const void *pData, int nData),
2324   void *pOut,                     /* First argument for xOutput */
2325   int *pnChangeset,               /* OUT: Size of buffer at *ppChangeset */
2326   void **ppChangeset              /* OUT: Buffer containing changeset */
2327 ){
2328   sqlite3 *db = pSession->db;     /* Source database handle */
2329   SessionTable *pTab;             /* Used to iterate through attached tables */
2330   SessionBuffer buf = {0,0,0};    /* Buffer in which to accumlate changeset */
2331   int rc;                         /* Return code */
2332 
2333   assert( xOutput==0 || (pnChangeset==0 && ppChangeset==0 ) );
2334 
2335   /* Zero the output variables in case an error occurs. If this session
2336   ** object is already in the error state (sqlite3_session.rc != SQLITE_OK),
2337   ** this call will be a no-op.  */
2338   if( xOutput==0 ){
2339     *pnChangeset = 0;
2340     *ppChangeset = 0;
2341   }
2342 
2343   if( pSession->rc ) return pSession->rc;
2344   rc = sqlite3_exec(pSession->db, "SAVEPOINT changeset", 0, 0, 0);
2345   if( rc!=SQLITE_OK ) return rc;
2346 
2347   sqlite3_mutex_enter(sqlite3_db_mutex(db));
2348 
2349   for(pTab=pSession->pTable; rc==SQLITE_OK && pTab; pTab=pTab->pNext){
2350     if( pTab->nEntry ){
2351       const char *zName = pTab->zName;
2352       int nCol;                   /* Number of columns in table */
2353       u8 *abPK;                   /* Primary key array */
2354       const char **azCol = 0;     /* Table columns */
2355       int i;                      /* Used to iterate through hash buckets */
2356       sqlite3_stmt *pSel = 0;     /* SELECT statement to query table pTab */
2357       int nRewind = buf.nBuf;     /* Initial size of write buffer */
2358       int nNoop;                  /* Size of buffer after writing tbl header */
2359 
2360       /* Check the table schema is still Ok. */
2361       rc = sessionTableInfo(db, pSession->zDb, zName, &nCol, 0, &azCol, &abPK);
2362       if( !rc && (pTab->nCol!=nCol || memcmp(abPK, pTab->abPK, nCol)) ){
2363         rc = SQLITE_SCHEMA;
2364       }
2365 
2366       /* Write a table header */
2367       sessionAppendTableHdr(&buf, bPatchset, pTab, &rc);
2368 
2369       /* Build and compile a statement to execute: */
2370       if( rc==SQLITE_OK ){
2371         rc = sessionSelectStmt(
2372             db, pSession->zDb, zName, nCol, azCol, abPK, &pSel);
2373       }
2374 
2375       nNoop = buf.nBuf;
2376       for(i=0; i<pTab->nChange && rc==SQLITE_OK; i++){
2377         SessionChange *p;         /* Used to iterate through changes */
2378 
2379         for(p=pTab->apChange[i]; rc==SQLITE_OK && p; p=p->pNext){
2380           rc = sessionSelectBind(pSel, nCol, abPK, p);
2381           if( rc!=SQLITE_OK ) continue;
2382           if( sqlite3_step(pSel)==SQLITE_ROW ){
2383             if( p->op==SQLITE_INSERT ){
2384               int iCol;
2385               sessionAppendByte(&buf, SQLITE_INSERT, &rc);
2386               sessionAppendByte(&buf, p->bIndirect, &rc);
2387               for(iCol=0; iCol<nCol; iCol++){
2388                 sessionAppendCol(&buf, pSel, iCol, &rc);
2389               }
2390             }else{
2391               rc = sessionAppendUpdate(&buf, bPatchset, pSel, p, abPK);
2392             }
2393           }else if( p->op!=SQLITE_INSERT ){
2394             rc = sessionAppendDelete(&buf, bPatchset, p, nCol, abPK);
2395           }
2396           if( rc==SQLITE_OK ){
2397             rc = sqlite3_reset(pSel);
2398           }
2399 
2400           /* If the buffer is now larger than SESSIONS_STRM_CHUNK_SIZE, pass
2401           ** its contents to the xOutput() callback. */
2402           if( xOutput
2403            && rc==SQLITE_OK
2404            && buf.nBuf>nNoop
2405            && buf.nBuf>SESSIONS_STRM_CHUNK_SIZE
2406           ){
2407             rc = xOutput(pOut, (void*)buf.aBuf, buf.nBuf);
2408             nNoop = -1;
2409             buf.nBuf = 0;
2410           }
2411 
2412         }
2413       }
2414 
2415       sqlite3_finalize(pSel);
2416       if( buf.nBuf==nNoop ){
2417         buf.nBuf = nRewind;
2418       }
2419       sqlite3_free((char*)azCol);  /* cast works around VC++ bug */
2420     }
2421   }
2422 
2423   if( rc==SQLITE_OK ){
2424     if( xOutput==0 ){
2425       *pnChangeset = buf.nBuf;
2426       *ppChangeset = buf.aBuf;
2427       buf.aBuf = 0;
2428     }else if( buf.nBuf>0 ){
2429       rc = xOutput(pOut, (void*)buf.aBuf, buf.nBuf);
2430     }
2431   }
2432 
2433   sqlite3_free(buf.aBuf);
2434   sqlite3_exec(db, "RELEASE changeset", 0, 0, 0);
2435   sqlite3_mutex_leave(sqlite3_db_mutex(db));
2436   return rc;
2437 }
2438 
2439 /*
2440 ** Obtain a changeset object containing all changes recorded by the
2441 ** session object passed as the first argument.
2442 **
2443 ** It is the responsibility of the caller to eventually free the buffer
2444 ** using sqlite3_free().
2445 */
2446 int sqlite3session_changeset(
2447   sqlite3_session *pSession,      /* Session object */
2448   int *pnChangeset,               /* OUT: Size of buffer at *ppChangeset */
2449   void **ppChangeset              /* OUT: Buffer containing changeset */
2450 ){
2451   return sessionGenerateChangeset(pSession, 0, 0, 0, pnChangeset, ppChangeset);
2452 }
2453 
2454 /*
2455 ** Streaming version of sqlite3session_changeset().
2456 */
2457 int sqlite3session_changeset_strm(
2458   sqlite3_session *pSession,
2459   int (*xOutput)(void *pOut, const void *pData, int nData),
2460   void *pOut
2461 ){
2462   return sessionGenerateChangeset(pSession, 0, xOutput, pOut, 0, 0);
2463 }
2464 
2465 /*
2466 ** Streaming version of sqlite3session_patchset().
2467 */
2468 int sqlite3session_patchset_strm(
2469   sqlite3_session *pSession,
2470   int (*xOutput)(void *pOut, const void *pData, int nData),
2471   void *pOut
2472 ){
2473   return sessionGenerateChangeset(pSession, 1, xOutput, pOut, 0, 0);
2474 }
2475 
2476 /*
2477 ** Obtain a patchset object containing all changes recorded by the
2478 ** session object passed as the first argument.
2479 **
2480 ** It is the responsibility of the caller to eventually free the buffer
2481 ** using sqlite3_free().
2482 */
2483 int sqlite3session_patchset(
2484   sqlite3_session *pSession,      /* Session object */
2485   int *pnPatchset,                /* OUT: Size of buffer at *ppChangeset */
2486   void **ppPatchset               /* OUT: Buffer containing changeset */
2487 ){
2488   return sessionGenerateChangeset(pSession, 1, 0, 0, pnPatchset, ppPatchset);
2489 }
2490 
2491 /*
2492 ** Enable or disable the session object passed as the first argument.
2493 */
2494 int sqlite3session_enable(sqlite3_session *pSession, int bEnable){
2495   int ret;
2496   sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
2497   if( bEnable>=0 ){
2498     pSession->bEnable = bEnable;
2499   }
2500   ret = pSession->bEnable;
2501   sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
2502   return ret;
2503 }
2504 
2505 /*
2506 ** Enable or disable the session object passed as the first argument.
2507 */
2508 int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect){
2509   int ret;
2510   sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
2511   if( bIndirect>=0 ){
2512     pSession->bIndirect = bIndirect;
2513   }
2514   ret = pSession->bIndirect;
2515   sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
2516   return ret;
2517 }
2518 
2519 /*
2520 ** Return true if there have been no changes to monitored tables recorded
2521 ** by the session object passed as the only argument.
2522 */
2523 int sqlite3session_isempty(sqlite3_session *pSession){
2524   int ret = 0;
2525   SessionTable *pTab;
2526 
2527   sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
2528   for(pTab=pSession->pTable; pTab && ret==0; pTab=pTab->pNext){
2529     ret = (pTab->nEntry>0);
2530   }
2531   sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
2532 
2533   return (ret==0);
2534 }
2535 
2536 /*
2537 ** Do the work for either sqlite3changeset_start() or start_strm().
2538 */
2539 static int sessionChangesetStart(
2540   sqlite3_changeset_iter **pp,    /* OUT: Changeset iterator handle */
2541   int (*xInput)(void *pIn, void *pData, int *pnData),
2542   void *pIn,
2543   int nChangeset,                 /* Size of buffer pChangeset in bytes */
2544   void *pChangeset,               /* Pointer to buffer containing changeset */
2545   int bInvert                     /* True to invert changeset */
2546 ){
2547   sqlite3_changeset_iter *pRet;   /* Iterator to return */
2548   int nByte;                      /* Number of bytes to allocate for iterator */
2549 
2550   assert( xInput==0 || (pChangeset==0 && nChangeset==0) );
2551 
2552   /* Zero the output variable in case an error occurs. */
2553   *pp = 0;
2554 
2555   /* Allocate and initialize the iterator structure. */
2556   nByte = sizeof(sqlite3_changeset_iter);
2557   pRet = (sqlite3_changeset_iter *)sqlite3_malloc(nByte);
2558   if( !pRet ) return SQLITE_NOMEM;
2559   memset(pRet, 0, sizeof(sqlite3_changeset_iter));
2560   pRet->in.aData = (u8 *)pChangeset;
2561   pRet->in.nData = nChangeset;
2562   pRet->in.xInput = xInput;
2563   pRet->in.pIn = pIn;
2564   pRet->in.bEof = (xInput ? 0 : 1);
2565   pRet->bInvert = bInvert;
2566 
2567   /* Populate the output variable and return success. */
2568   *pp = pRet;
2569   return SQLITE_OK;
2570 }
2571 
2572 /*
2573 ** Create an iterator used to iterate through the contents of a changeset.
2574 */
2575 int sqlite3changeset_start(
2576   sqlite3_changeset_iter **pp,    /* OUT: Changeset iterator handle */
2577   int nChangeset,                 /* Size of buffer pChangeset in bytes */
2578   void *pChangeset                /* Pointer to buffer containing changeset */
2579 ){
2580   return sessionChangesetStart(pp, 0, 0, nChangeset, pChangeset, 0);
2581 }
2582 int sqlite3changeset_start_v2(
2583   sqlite3_changeset_iter **pp,    /* OUT: Changeset iterator handle */
2584   int nChangeset,                 /* Size of buffer pChangeset in bytes */
2585   void *pChangeset,               /* Pointer to buffer containing changeset */
2586   int flags
2587 ){
2588   int bInvert = !!(flags & SQLITE_CHANGESETSTART_INVERT);
2589   return sessionChangesetStart(pp, 0, 0, nChangeset, pChangeset, bInvert);
2590 }
2591 
2592 /*
2593 ** Streaming version of sqlite3changeset_start().
2594 */
2595 int sqlite3changeset_start_strm(
2596   sqlite3_changeset_iter **pp,    /* OUT: Changeset iterator handle */
2597   int (*xInput)(void *pIn, void *pData, int *pnData),
2598   void *pIn
2599 ){
2600   return sessionChangesetStart(pp, xInput, pIn, 0, 0, 0);
2601 }
2602 int sqlite3changeset_start_v2_strm(
2603   sqlite3_changeset_iter **pp,    /* OUT: Changeset iterator handle */
2604   int (*xInput)(void *pIn, void *pData, int *pnData),
2605   void *pIn,
2606   int flags
2607 ){
2608   int bInvert = !!(flags & SQLITE_CHANGESETSTART_INVERT);
2609   return sessionChangesetStart(pp, xInput, pIn, 0, 0, bInvert);
2610 }
2611 
2612 /*
2613 ** If the SessionInput object passed as the only argument is a streaming
2614 ** object and the buffer is full, discard some data to free up space.
2615 */
2616 static void sessionDiscardData(SessionInput *pIn){
2617   if( pIn->xInput && pIn->iNext>=SESSIONS_STRM_CHUNK_SIZE ){
2618     int nMove = pIn->buf.nBuf - pIn->iNext;
2619     assert( nMove>=0 );
2620     if( nMove>0 ){
2621       memmove(pIn->buf.aBuf, &pIn->buf.aBuf[pIn->iNext], nMove);
2622     }
2623     pIn->buf.nBuf -= pIn->iNext;
2624     pIn->iNext = 0;
2625     pIn->nData = pIn->buf.nBuf;
2626   }
2627 }
2628 
2629 /*
2630 ** Ensure that there are at least nByte bytes available in the buffer. Or,
2631 ** if there are not nByte bytes remaining in the input, that all available
2632 ** data is in the buffer.
2633 **
2634 ** Return an SQLite error code if an error occurs, or SQLITE_OK otherwise.
2635 */
2636 static int sessionInputBuffer(SessionInput *pIn, int nByte){
2637   int rc = SQLITE_OK;
2638   if( pIn->xInput ){
2639     while( !pIn->bEof && (pIn->iNext+nByte)>=pIn->nData && rc==SQLITE_OK ){
2640       int nNew = SESSIONS_STRM_CHUNK_SIZE;
2641 
2642       if( pIn->bNoDiscard==0 ) sessionDiscardData(pIn);
2643       if( SQLITE_OK==sessionBufferGrow(&pIn->buf, nNew, &rc) ){
2644         rc = pIn->xInput(pIn->pIn, &pIn->buf.aBuf[pIn->buf.nBuf], &nNew);
2645         if( nNew==0 ){
2646           pIn->bEof = 1;
2647         }else{
2648           pIn->buf.nBuf += nNew;
2649         }
2650       }
2651 
2652       pIn->aData = pIn->buf.aBuf;
2653       pIn->nData = pIn->buf.nBuf;
2654     }
2655   }
2656   return rc;
2657 }
2658 
2659 /*
2660 ** When this function is called, *ppRec points to the start of a record
2661 ** that contains nCol values. This function advances the pointer *ppRec
2662 ** until it points to the byte immediately following that record.
2663 */
2664 static void sessionSkipRecord(
2665   u8 **ppRec,                     /* IN/OUT: Record pointer */
2666   int nCol                        /* Number of values in record */
2667 ){
2668   u8 *aRec = *ppRec;
2669   int i;
2670   for(i=0; i<nCol; i++){
2671     int eType = *aRec++;
2672     if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
2673       int nByte;
2674       aRec += sessionVarintGet((u8*)aRec, &nByte);
2675       aRec += nByte;
2676     }else if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
2677       aRec += 8;
2678     }
2679   }
2680 
2681   *ppRec = aRec;
2682 }
2683 
2684 /*
2685 ** This function sets the value of the sqlite3_value object passed as the
2686 ** first argument to a copy of the string or blob held in the aData[]
2687 ** buffer. SQLITE_OK is returned if successful, or SQLITE_NOMEM if an OOM
2688 ** error occurs.
2689 */
2690 static int sessionValueSetStr(
2691   sqlite3_value *pVal,            /* Set the value of this object */
2692   u8 *aData,                      /* Buffer containing string or blob data */
2693   int nData,                      /* Size of buffer aData[] in bytes */
2694   u8 enc                          /* String encoding (0 for blobs) */
2695 ){
2696   /* In theory this code could just pass SQLITE_TRANSIENT as the final
2697   ** argument to sqlite3ValueSetStr() and have the copy created
2698   ** automatically. But doing so makes it difficult to detect any OOM
2699   ** error. Hence the code to create the copy externally. */
2700   u8 *aCopy = sqlite3_malloc(nData+1);
2701   if( aCopy==0 ) return SQLITE_NOMEM;
2702   memcpy(aCopy, aData, nData);
2703   sqlite3ValueSetStr(pVal, nData, (char*)aCopy, enc, sqlite3_free);
2704   return SQLITE_OK;
2705 }
2706 
2707 /*
2708 ** Deserialize a single record from a buffer in memory. See "RECORD FORMAT"
2709 ** for details.
2710 **
2711 ** When this function is called, *paChange points to the start of the record
2712 ** to deserialize. Assuming no error occurs, *paChange is set to point to
2713 ** one byte after the end of the same record before this function returns.
2714 ** If the argument abPK is NULL, then the record contains nCol values. Or,
2715 ** if abPK is other than NULL, then the record contains only the PK fields
2716 ** (in other words, it is a patchset DELETE record).
2717 **
2718 ** If successful, each element of the apOut[] array (allocated by the caller)
2719 ** is set to point to an sqlite3_value object containing the value read
2720 ** from the corresponding position in the record. If that value is not
2721 ** included in the record (i.e. because the record is part of an UPDATE change
2722 ** and the field was not modified), the corresponding element of apOut[] is
2723 ** set to NULL.
2724 **
2725 ** It is the responsibility of the caller to free all sqlite_value structures
2726 ** using sqlite3_free().
2727 **
2728 ** If an error occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned.
2729 ** The apOut[] array may have been partially populated in this case.
2730 */
2731 static int sessionReadRecord(
2732   SessionInput *pIn,              /* Input data */
2733   int nCol,                       /* Number of values in record */
2734   u8 *abPK,                       /* Array of primary key flags, or NULL */
2735   sqlite3_value **apOut           /* Write values to this array */
2736 ){
2737   int i;                          /* Used to iterate through columns */
2738   int rc = SQLITE_OK;
2739 
2740   for(i=0; i<nCol && rc==SQLITE_OK; i++){
2741     int eType = 0;                /* Type of value (SQLITE_NULL, TEXT etc.) */
2742     if( abPK && abPK[i]==0 ) continue;
2743     rc = sessionInputBuffer(pIn, 9);
2744     if( rc==SQLITE_OK ){
2745       if( pIn->iNext>=pIn->nData ){
2746         rc = SQLITE_CORRUPT_BKPT;
2747       }else{
2748         eType = pIn->aData[pIn->iNext++];
2749         assert( apOut[i]==0 );
2750         if( eType ){
2751           apOut[i] = sqlite3ValueNew(0);
2752           if( !apOut[i] ) rc = SQLITE_NOMEM;
2753         }
2754       }
2755     }
2756 
2757     if( rc==SQLITE_OK ){
2758       u8 *aVal = &pIn->aData[pIn->iNext];
2759       if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
2760         int nByte;
2761         pIn->iNext += sessionVarintGet(aVal, &nByte);
2762         rc = sessionInputBuffer(pIn, nByte);
2763         if( rc==SQLITE_OK ){
2764           if( nByte<0 || nByte>pIn->nData-pIn->iNext ){
2765             rc = SQLITE_CORRUPT_BKPT;
2766           }else{
2767             u8 enc = (eType==SQLITE_TEXT ? SQLITE_UTF8 : 0);
2768             rc = sessionValueSetStr(apOut[i],&pIn->aData[pIn->iNext],nByte,enc);
2769             pIn->iNext += nByte;
2770           }
2771         }
2772       }
2773       if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
2774         sqlite3_int64 v = sessionGetI64(aVal);
2775         if( eType==SQLITE_INTEGER ){
2776           sqlite3VdbeMemSetInt64(apOut[i], v);
2777         }else{
2778           double d;
2779           memcpy(&d, &v, 8);
2780           sqlite3VdbeMemSetDouble(apOut[i], d);
2781         }
2782         pIn->iNext += 8;
2783       }
2784     }
2785   }
2786 
2787   return rc;
2788 }
2789 
2790 /*
2791 ** The input pointer currently points to the second byte of a table-header.
2792 ** Specifically, to the following:
2793 **
2794 **   + number of columns in table (varint)
2795 **   + array of PK flags (1 byte per column),
2796 **   + table name (nul terminated).
2797 **
2798 ** This function ensures that all of the above is present in the input
2799 ** buffer (i.e. that it can be accessed without any calls to xInput()).
2800 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
2801 ** The input pointer is not moved.
2802 */
2803 static int sessionChangesetBufferTblhdr(SessionInput *pIn, int *pnByte){
2804   int rc = SQLITE_OK;
2805   int nCol = 0;
2806   int nRead = 0;
2807 
2808   rc = sessionInputBuffer(pIn, 9);
2809   if( rc==SQLITE_OK ){
2810     nRead += sessionVarintGet(&pIn->aData[pIn->iNext + nRead], &nCol);
2811     /* The hard upper limit for the number of columns in an SQLite
2812     ** database table is, according to sqliteLimit.h, 32676. So
2813     ** consider any table-header that purports to have more than 65536
2814     ** columns to be corrupt. This is convenient because otherwise,
2815     ** if the (nCol>65536) condition below were omitted, a sufficiently
2816     ** large value for nCol may cause nRead to wrap around and become
2817     ** negative. Leading to a crash. */
2818     if( nCol<0 || nCol>65536 ){
2819       rc = SQLITE_CORRUPT_BKPT;
2820     }else{
2821       rc = sessionInputBuffer(pIn, nRead+nCol+100);
2822       nRead += nCol;
2823     }
2824   }
2825 
2826   while( rc==SQLITE_OK ){
2827     while( (pIn->iNext + nRead)<pIn->nData && pIn->aData[pIn->iNext + nRead] ){
2828       nRead++;
2829     }
2830     if( (pIn->iNext + nRead)<pIn->nData ) break;
2831     rc = sessionInputBuffer(pIn, nRead + 100);
2832   }
2833   *pnByte = nRead+1;
2834   return rc;
2835 }
2836 
2837 /*
2838 ** The input pointer currently points to the first byte of the first field
2839 ** of a record consisting of nCol columns. This function ensures the entire
2840 ** record is buffered. It does not move the input pointer.
2841 **
2842 ** If successful, SQLITE_OK is returned and *pnByte is set to the size of
2843 ** the record in bytes. Otherwise, an SQLite error code is returned. The
2844 ** final value of *pnByte is undefined in this case.
2845 */
2846 static int sessionChangesetBufferRecord(
2847   SessionInput *pIn,              /* Input data */
2848   int nCol,                       /* Number of columns in record */
2849   int *pnByte                     /* OUT: Size of record in bytes */
2850 ){
2851   int rc = SQLITE_OK;
2852   int nByte = 0;
2853   int i;
2854   for(i=0; rc==SQLITE_OK && i<nCol; i++){
2855     int eType;
2856     rc = sessionInputBuffer(pIn, nByte + 10);
2857     if( rc==SQLITE_OK ){
2858       eType = pIn->aData[pIn->iNext + nByte++];
2859       if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
2860         int n;
2861         nByte += sessionVarintGet(&pIn->aData[pIn->iNext+nByte], &n);
2862         nByte += n;
2863         rc = sessionInputBuffer(pIn, nByte);
2864       }else if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
2865         nByte += 8;
2866       }
2867     }
2868   }
2869   *pnByte = nByte;
2870   return rc;
2871 }
2872 
2873 /*
2874 ** The input pointer currently points to the second byte of a table-header.
2875 ** Specifically, to the following:
2876 **
2877 **   + number of columns in table (varint)
2878 **   + array of PK flags (1 byte per column),
2879 **   + table name (nul terminated).
2880 **
2881 ** This function decodes the table-header and populates the p->nCol,
2882 ** p->zTab and p->abPK[] variables accordingly. The p->apValue[] array is
2883 ** also allocated or resized according to the new value of p->nCol. The
2884 ** input pointer is left pointing to the byte following the table header.
2885 **
2886 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code
2887 ** is returned and the final values of the various fields enumerated above
2888 ** are undefined.
2889 */
2890 static int sessionChangesetReadTblhdr(sqlite3_changeset_iter *p){
2891   int rc;
2892   int nCopy;
2893   assert( p->rc==SQLITE_OK );
2894 
2895   rc = sessionChangesetBufferTblhdr(&p->in, &nCopy);
2896   if( rc==SQLITE_OK ){
2897     int nByte;
2898     int nVarint;
2899     nVarint = sessionVarintGet(&p->in.aData[p->in.iNext], &p->nCol);
2900     if( p->nCol>0 ){
2901       nCopy -= nVarint;
2902       p->in.iNext += nVarint;
2903       nByte = p->nCol * sizeof(sqlite3_value*) * 2 + nCopy;
2904       p->tblhdr.nBuf = 0;
2905       sessionBufferGrow(&p->tblhdr, nByte, &rc);
2906     }else{
2907       rc = SQLITE_CORRUPT_BKPT;
2908     }
2909   }
2910 
2911   if( rc==SQLITE_OK ){
2912     int iPK = sizeof(sqlite3_value*)*p->nCol*2;
2913     memset(p->tblhdr.aBuf, 0, iPK);
2914     memcpy(&p->tblhdr.aBuf[iPK], &p->in.aData[p->in.iNext], nCopy);
2915     p->in.iNext += nCopy;
2916   }
2917 
2918   p->apValue = (sqlite3_value**)p->tblhdr.aBuf;
2919   p->abPK = (u8*)&p->apValue[p->nCol*2];
2920   p->zTab = (char*)&p->abPK[p->nCol];
2921   return (p->rc = rc);
2922 }
2923 
2924 /*
2925 ** Advance the changeset iterator to the next change.
2926 **
2927 ** If both paRec and pnRec are NULL, then this function works like the public
2928 ** API sqlite3changeset_next(). If SQLITE_ROW is returned, then the
2929 ** sqlite3changeset_new() and old() APIs may be used to query for values.
2930 **
2931 ** Otherwise, if paRec and pnRec are not NULL, then a pointer to the change
2932 ** record is written to *paRec before returning and the number of bytes in
2933 ** the record to *pnRec.
2934 **
2935 ** Either way, this function returns SQLITE_ROW if the iterator is
2936 ** successfully advanced to the next change in the changeset, an SQLite
2937 ** error code if an error occurs, or SQLITE_DONE if there are no further
2938 ** changes in the changeset.
2939 */
2940 static int sessionChangesetNext(
2941   sqlite3_changeset_iter *p,      /* Changeset iterator */
2942   u8 **paRec,                     /* If non-NULL, store record pointer here */
2943   int *pnRec,                     /* If non-NULL, store size of record here */
2944   int *pbNew                      /* If non-NULL, true if new table */
2945 ){
2946   int i;
2947   u8 op;
2948 
2949   assert( (paRec==0 && pnRec==0) || (paRec && pnRec) );
2950 
2951   /* If the iterator is in the error-state, return immediately. */
2952   if( p->rc!=SQLITE_OK ) return p->rc;
2953 
2954   /* Free the current contents of p->apValue[], if any. */
2955   if( p->apValue ){
2956     for(i=0; i<p->nCol*2; i++){
2957       sqlite3ValueFree(p->apValue[i]);
2958     }
2959     memset(p->apValue, 0, sizeof(sqlite3_value*)*p->nCol*2);
2960   }
2961 
2962   /* Make sure the buffer contains at least 10 bytes of input data, or all
2963   ** remaining data if there are less than 10 bytes available. This is
2964   ** sufficient either for the 'T' or 'P' byte and the varint that follows
2965   ** it, or for the two single byte values otherwise. */
2966   p->rc = sessionInputBuffer(&p->in, 2);
2967   if( p->rc!=SQLITE_OK ) return p->rc;
2968 
2969   /* If the iterator is already at the end of the changeset, return DONE. */
2970   if( p->in.iNext>=p->in.nData ){
2971     return SQLITE_DONE;
2972   }
2973 
2974   sessionDiscardData(&p->in);
2975   p->in.iCurrent = p->in.iNext;
2976 
2977   op = p->in.aData[p->in.iNext++];
2978   while( op=='T' || op=='P' ){
2979     if( pbNew ) *pbNew = 1;
2980     p->bPatchset = (op=='P');
2981     if( sessionChangesetReadTblhdr(p) ) return p->rc;
2982     if( (p->rc = sessionInputBuffer(&p->in, 2)) ) return p->rc;
2983     p->in.iCurrent = p->in.iNext;
2984     if( p->in.iNext>=p->in.nData ) return SQLITE_DONE;
2985     op = p->in.aData[p->in.iNext++];
2986   }
2987 
2988   if( p->zTab==0 || (p->bPatchset && p->bInvert) ){
2989     /* The first record in the changeset is not a table header. Must be a
2990     ** corrupt changeset. */
2991     assert( p->in.iNext==1 || p->zTab );
2992     return (p->rc = SQLITE_CORRUPT_BKPT);
2993   }
2994 
2995   p->op = op;
2996   p->bIndirect = p->in.aData[p->in.iNext++];
2997   if( p->op!=SQLITE_UPDATE && p->op!=SQLITE_DELETE && p->op!=SQLITE_INSERT ){
2998     return (p->rc = SQLITE_CORRUPT_BKPT);
2999   }
3000 
3001   if( paRec ){
3002     int nVal;                     /* Number of values to buffer */
3003     if( p->bPatchset==0 && op==SQLITE_UPDATE ){
3004       nVal = p->nCol * 2;
3005     }else if( p->bPatchset && op==SQLITE_DELETE ){
3006       nVal = 0;
3007       for(i=0; i<p->nCol; i++) if( p->abPK[i] ) nVal++;
3008     }else{
3009       nVal = p->nCol;
3010     }
3011     p->rc = sessionChangesetBufferRecord(&p->in, nVal, pnRec);
3012     if( p->rc!=SQLITE_OK ) return p->rc;
3013     *paRec = &p->in.aData[p->in.iNext];
3014     p->in.iNext += *pnRec;
3015   }else{
3016     sqlite3_value **apOld = (p->bInvert ? &p->apValue[p->nCol] : p->apValue);
3017     sqlite3_value **apNew = (p->bInvert ? p->apValue : &p->apValue[p->nCol]);
3018 
3019     /* If this is an UPDATE or DELETE, read the old.* record. */
3020     if( p->op!=SQLITE_INSERT && (p->bPatchset==0 || p->op==SQLITE_DELETE) ){
3021       u8 *abPK = p->bPatchset ? p->abPK : 0;
3022       p->rc = sessionReadRecord(&p->in, p->nCol, abPK, apOld);
3023       if( p->rc!=SQLITE_OK ) return p->rc;
3024     }
3025 
3026     /* If this is an INSERT or UPDATE, read the new.* record. */
3027     if( p->op!=SQLITE_DELETE ){
3028       p->rc = sessionReadRecord(&p->in, p->nCol, 0, apNew);
3029       if( p->rc!=SQLITE_OK ) return p->rc;
3030     }
3031 
3032     if( (p->bPatchset || p->bInvert) && p->op==SQLITE_UPDATE ){
3033       /* If this is an UPDATE that is part of a patchset, then all PK and
3034       ** modified fields are present in the new.* record. The old.* record
3035       ** is currently completely empty. This block shifts the PK fields from
3036       ** new.* to old.*, to accommodate the code that reads these arrays.  */
3037       for(i=0; i<p->nCol; i++){
3038         assert( p->bPatchset==0 || p->apValue[i]==0 );
3039         if( p->abPK[i] ){
3040           assert( p->apValue[i]==0 );
3041           p->apValue[i] = p->apValue[i+p->nCol];
3042           if( p->apValue[i]==0 ) return (p->rc = SQLITE_CORRUPT_BKPT);
3043           p->apValue[i+p->nCol] = 0;
3044         }
3045       }
3046     }else if( p->bInvert ){
3047       if( p->op==SQLITE_INSERT ) p->op = SQLITE_DELETE;
3048       else if( p->op==SQLITE_DELETE ) p->op = SQLITE_INSERT;
3049     }
3050   }
3051 
3052   return SQLITE_ROW;
3053 }
3054 
3055 /*
3056 ** Advance an iterator created by sqlite3changeset_start() to the next
3057 ** change in the changeset. This function may return SQLITE_ROW, SQLITE_DONE
3058 ** or SQLITE_CORRUPT.
3059 **
3060 ** This function may not be called on iterators passed to a conflict handler
3061 ** callback by changeset_apply().
3062 */
3063 int sqlite3changeset_next(sqlite3_changeset_iter *p){
3064   return sessionChangesetNext(p, 0, 0, 0);
3065 }
3066 
3067 /*
3068 ** The following function extracts information on the current change
3069 ** from a changeset iterator. It may only be called after changeset_next()
3070 ** has returned SQLITE_ROW.
3071 */
3072 int sqlite3changeset_op(
3073   sqlite3_changeset_iter *pIter,  /* Iterator handle */
3074   const char **pzTab,             /* OUT: Pointer to table name */
3075   int *pnCol,                     /* OUT: Number of columns in table */
3076   int *pOp,                       /* OUT: SQLITE_INSERT, DELETE or UPDATE */
3077   int *pbIndirect                 /* OUT: True if change is indirect */
3078 ){
3079   *pOp = pIter->op;
3080   *pnCol = pIter->nCol;
3081   *pzTab = pIter->zTab;
3082   if( pbIndirect ) *pbIndirect = pIter->bIndirect;
3083   return SQLITE_OK;
3084 }
3085 
3086 /*
3087 ** Return information regarding the PRIMARY KEY and number of columns in
3088 ** the database table affected by the change that pIter currently points
3089 ** to. This function may only be called after changeset_next() returns
3090 ** SQLITE_ROW.
3091 */
3092 int sqlite3changeset_pk(
3093   sqlite3_changeset_iter *pIter,  /* Iterator object */
3094   unsigned char **pabPK,          /* OUT: Array of boolean - true for PK cols */
3095   int *pnCol                      /* OUT: Number of entries in output array */
3096 ){
3097   *pabPK = pIter->abPK;
3098   if( pnCol ) *pnCol = pIter->nCol;
3099   return SQLITE_OK;
3100 }
3101 
3102 /*
3103 ** This function may only be called while the iterator is pointing to an
3104 ** SQLITE_UPDATE or SQLITE_DELETE change (see sqlite3changeset_op()).
3105 ** Otherwise, SQLITE_MISUSE is returned.
3106 **
3107 ** It sets *ppValue to point to an sqlite3_value structure containing the
3108 ** iVal'th value in the old.* record. Or, if that particular value is not
3109 ** included in the record (because the change is an UPDATE and the field
3110 ** was not modified and is not a PK column), set *ppValue to NULL.
3111 **
3112 ** If value iVal is out-of-range, SQLITE_RANGE is returned and *ppValue is
3113 ** not modified. Otherwise, SQLITE_OK.
3114 */
3115 int sqlite3changeset_old(
3116   sqlite3_changeset_iter *pIter,  /* Changeset iterator */
3117   int iVal,                       /* Index of old.* value to retrieve */
3118   sqlite3_value **ppValue         /* OUT: Old value (or NULL pointer) */
3119 ){
3120   if( pIter->op!=SQLITE_UPDATE && pIter->op!=SQLITE_DELETE ){
3121     return SQLITE_MISUSE;
3122   }
3123   if( iVal<0 || iVal>=pIter->nCol ){
3124     return SQLITE_RANGE;
3125   }
3126   *ppValue = pIter->apValue[iVal];
3127   return SQLITE_OK;
3128 }
3129 
3130 /*
3131 ** This function may only be called while the iterator is pointing to an
3132 ** SQLITE_UPDATE or SQLITE_INSERT change (see sqlite3changeset_op()).
3133 ** Otherwise, SQLITE_MISUSE is returned.
3134 **
3135 ** It sets *ppValue to point to an sqlite3_value structure containing the
3136 ** iVal'th value in the new.* record. Or, if that particular value is not
3137 ** included in the record (because the change is an UPDATE and the field
3138 ** was not modified), set *ppValue to NULL.
3139 **
3140 ** If value iVal is out-of-range, SQLITE_RANGE is returned and *ppValue is
3141 ** not modified. Otherwise, SQLITE_OK.
3142 */
3143 int sqlite3changeset_new(
3144   sqlite3_changeset_iter *pIter,  /* Changeset iterator */
3145   int iVal,                       /* Index of new.* value to retrieve */
3146   sqlite3_value **ppValue         /* OUT: New value (or NULL pointer) */
3147 ){
3148   if( pIter->op!=SQLITE_UPDATE && pIter->op!=SQLITE_INSERT ){
3149     return SQLITE_MISUSE;
3150   }
3151   if( iVal<0 || iVal>=pIter->nCol ){
3152     return SQLITE_RANGE;
3153   }
3154   *ppValue = pIter->apValue[pIter->nCol+iVal];
3155   return SQLITE_OK;
3156 }
3157 
3158 /*
3159 ** The following two macros are used internally. They are similar to the
3160 ** sqlite3changeset_new() and sqlite3changeset_old() functions, except that
3161 ** they omit all error checking and return a pointer to the requested value.
3162 */
3163 #define sessionChangesetNew(pIter, iVal) (pIter)->apValue[(pIter)->nCol+(iVal)]
3164 #define sessionChangesetOld(pIter, iVal) (pIter)->apValue[(iVal)]
3165 
3166 /*
3167 ** This function may only be called with a changeset iterator that has been
3168 ** passed to an SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT
3169 ** conflict-handler function. Otherwise, SQLITE_MISUSE is returned.
3170 **
3171 ** If successful, *ppValue is set to point to an sqlite3_value structure
3172 ** containing the iVal'th value of the conflicting record.
3173 **
3174 ** If value iVal is out-of-range or some other error occurs, an SQLite error
3175 ** code is returned. Otherwise, SQLITE_OK.
3176 */
3177 int sqlite3changeset_conflict(
3178   sqlite3_changeset_iter *pIter,  /* Changeset iterator */
3179   int iVal,                       /* Index of conflict record value to fetch */
3180   sqlite3_value **ppValue         /* OUT: Value from conflicting row */
3181 ){
3182   if( !pIter->pConflict ){
3183     return SQLITE_MISUSE;
3184   }
3185   if( iVal<0 || iVal>=pIter->nCol ){
3186     return SQLITE_RANGE;
3187   }
3188   *ppValue = sqlite3_column_value(pIter->pConflict, iVal);
3189   return SQLITE_OK;
3190 }
3191 
3192 /*
3193 ** This function may only be called with an iterator passed to an
3194 ** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case
3195 ** it sets the output variable to the total number of known foreign key
3196 ** violations in the destination database and returns SQLITE_OK.
3197 **
3198 ** In all other cases this function returns SQLITE_MISUSE.
3199 */
3200 int sqlite3changeset_fk_conflicts(
3201   sqlite3_changeset_iter *pIter,  /* Changeset iterator */
3202   int *pnOut                      /* OUT: Number of FK violations */
3203 ){
3204   if( pIter->pConflict || pIter->apValue ){
3205     return SQLITE_MISUSE;
3206   }
3207   *pnOut = pIter->nCol;
3208   return SQLITE_OK;
3209 }
3210 
3211 
3212 /*
3213 ** Finalize an iterator allocated with sqlite3changeset_start().
3214 **
3215 ** This function may not be called on iterators passed to a conflict handler
3216 ** callback by changeset_apply().
3217 */
3218 int sqlite3changeset_finalize(sqlite3_changeset_iter *p){
3219   int rc = SQLITE_OK;
3220   if( p ){
3221     int i;                        /* Used to iterate through p->apValue[] */
3222     rc = p->rc;
3223     if( p->apValue ){
3224       for(i=0; i<p->nCol*2; i++) sqlite3ValueFree(p->apValue[i]);
3225     }
3226     sqlite3_free(p->tblhdr.aBuf);
3227     sqlite3_free(p->in.buf.aBuf);
3228     sqlite3_free(p);
3229   }
3230   return rc;
3231 }
3232 
3233 static int sessionChangesetInvert(
3234   SessionInput *pInput,           /* Input changeset */
3235   int (*xOutput)(void *pOut, const void *pData, int nData),
3236   void *pOut,
3237   int *pnInverted,                /* OUT: Number of bytes in output changeset */
3238   void **ppInverted               /* OUT: Inverse of pChangeset */
3239 ){
3240   int rc = SQLITE_OK;             /* Return value */
3241   SessionBuffer sOut;             /* Output buffer */
3242   int nCol = 0;                   /* Number of cols in current table */
3243   u8 *abPK = 0;                   /* PK array for current table */
3244   sqlite3_value **apVal = 0;      /* Space for values for UPDATE inversion */
3245   SessionBuffer sPK = {0, 0, 0};  /* PK array for current table */
3246 
3247   /* Initialize the output buffer */
3248   memset(&sOut, 0, sizeof(SessionBuffer));
3249 
3250   /* Zero the output variables in case an error occurs. */
3251   if( ppInverted ){
3252     *ppInverted = 0;
3253     *pnInverted = 0;
3254   }
3255 
3256   while( 1 ){
3257     u8 eType;
3258 
3259     /* Test for EOF. */
3260     if( (rc = sessionInputBuffer(pInput, 2)) ) goto finished_invert;
3261     if( pInput->iNext>=pInput->nData ) break;
3262     eType = pInput->aData[pInput->iNext];
3263 
3264     switch( eType ){
3265       case 'T': {
3266         /* A 'table' record consists of:
3267         **
3268         **   * A constant 'T' character,
3269         **   * Number of columns in said table (a varint),
3270         **   * An array of nCol bytes (sPK),
3271         **   * A nul-terminated table name.
3272         */
3273         int nByte;
3274         int nVar;
3275         pInput->iNext++;
3276         if( (rc = sessionChangesetBufferTblhdr(pInput, &nByte)) ){
3277           goto finished_invert;
3278         }
3279         nVar = sessionVarintGet(&pInput->aData[pInput->iNext], &nCol);
3280         sPK.nBuf = 0;
3281         sessionAppendBlob(&sPK, &pInput->aData[pInput->iNext+nVar], nCol, &rc);
3282         sessionAppendByte(&sOut, eType, &rc);
3283         sessionAppendBlob(&sOut, &pInput->aData[pInput->iNext], nByte, &rc);
3284         if( rc ) goto finished_invert;
3285 
3286         pInput->iNext += nByte;
3287         sqlite3_free(apVal);
3288         apVal = 0;
3289         abPK = sPK.aBuf;
3290         break;
3291       }
3292 
3293       case SQLITE_INSERT:
3294       case SQLITE_DELETE: {
3295         int nByte;
3296         int bIndirect = pInput->aData[pInput->iNext+1];
3297         int eType2 = (eType==SQLITE_DELETE ? SQLITE_INSERT : SQLITE_DELETE);
3298         pInput->iNext += 2;
3299         assert( rc==SQLITE_OK );
3300         rc = sessionChangesetBufferRecord(pInput, nCol, &nByte);
3301         sessionAppendByte(&sOut, eType2, &rc);
3302         sessionAppendByte(&sOut, bIndirect, &rc);
3303         sessionAppendBlob(&sOut, &pInput->aData[pInput->iNext], nByte, &rc);
3304         pInput->iNext += nByte;
3305         if( rc ) goto finished_invert;
3306         break;
3307       }
3308 
3309       case SQLITE_UPDATE: {
3310         int iCol;
3311 
3312         if( 0==apVal ){
3313           apVal = (sqlite3_value **)sqlite3_malloc(sizeof(apVal[0])*nCol*2);
3314           if( 0==apVal ){
3315             rc = SQLITE_NOMEM;
3316             goto finished_invert;
3317           }
3318           memset(apVal, 0, sizeof(apVal[0])*nCol*2);
3319         }
3320 
3321         /* Write the header for the new UPDATE change. Same as the original. */
3322         sessionAppendByte(&sOut, eType, &rc);
3323         sessionAppendByte(&sOut, pInput->aData[pInput->iNext+1], &rc);
3324 
3325         /* Read the old.* and new.* records for the update change. */
3326         pInput->iNext += 2;
3327         rc = sessionReadRecord(pInput, nCol, 0, &apVal[0]);
3328         if( rc==SQLITE_OK ){
3329           rc = sessionReadRecord(pInput, nCol, 0, &apVal[nCol]);
3330         }
3331 
3332         /* Write the new old.* record. Consists of the PK columns from the
3333         ** original old.* record, and the other values from the original
3334         ** new.* record. */
3335         for(iCol=0; iCol<nCol; iCol++){
3336           sqlite3_value *pVal = apVal[iCol + (abPK[iCol] ? 0 : nCol)];
3337           sessionAppendValue(&sOut, pVal, &rc);
3338         }
3339 
3340         /* Write the new new.* record. Consists of a copy of all values
3341         ** from the original old.* record, except for the PK columns, which
3342         ** are set to "undefined". */
3343         for(iCol=0; iCol<nCol; iCol++){
3344           sqlite3_value *pVal = (abPK[iCol] ? 0 : apVal[iCol]);
3345           sessionAppendValue(&sOut, pVal, &rc);
3346         }
3347 
3348         for(iCol=0; iCol<nCol*2; iCol++){
3349           sqlite3ValueFree(apVal[iCol]);
3350         }
3351         memset(apVal, 0, sizeof(apVal[0])*nCol*2);
3352         if( rc!=SQLITE_OK ){
3353           goto finished_invert;
3354         }
3355 
3356         break;
3357       }
3358 
3359       default:
3360         rc = SQLITE_CORRUPT_BKPT;
3361         goto finished_invert;
3362     }
3363 
3364     assert( rc==SQLITE_OK );
3365     if( xOutput && sOut.nBuf>=SESSIONS_STRM_CHUNK_SIZE ){
3366       rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
3367       sOut.nBuf = 0;
3368       if( rc!=SQLITE_OK ) goto finished_invert;
3369     }
3370   }
3371 
3372   assert( rc==SQLITE_OK );
3373   if( pnInverted ){
3374     *pnInverted = sOut.nBuf;
3375     *ppInverted = sOut.aBuf;
3376     sOut.aBuf = 0;
3377   }else if( sOut.nBuf>0 ){
3378     rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
3379   }
3380 
3381  finished_invert:
3382   sqlite3_free(sOut.aBuf);
3383   sqlite3_free(apVal);
3384   sqlite3_free(sPK.aBuf);
3385   return rc;
3386 }
3387 
3388 
3389 /*
3390 ** Invert a changeset object.
3391 */
3392 int sqlite3changeset_invert(
3393   int nChangeset,                 /* Number of bytes in input */
3394   const void *pChangeset,         /* Input changeset */
3395   int *pnInverted,                /* OUT: Number of bytes in output changeset */
3396   void **ppInverted               /* OUT: Inverse of pChangeset */
3397 ){
3398   SessionInput sInput;
3399 
3400   /* Set up the input stream */
3401   memset(&sInput, 0, sizeof(SessionInput));
3402   sInput.nData = nChangeset;
3403   sInput.aData = (u8*)pChangeset;
3404 
3405   return sessionChangesetInvert(&sInput, 0, 0, pnInverted, ppInverted);
3406 }
3407 
3408 /*
3409 ** Streaming version of sqlite3changeset_invert().
3410 */
3411 int sqlite3changeset_invert_strm(
3412   int (*xInput)(void *pIn, void *pData, int *pnData),
3413   void *pIn,
3414   int (*xOutput)(void *pOut, const void *pData, int nData),
3415   void *pOut
3416 ){
3417   SessionInput sInput;
3418   int rc;
3419 
3420   /* Set up the input stream */
3421   memset(&sInput, 0, sizeof(SessionInput));
3422   sInput.xInput = xInput;
3423   sInput.pIn = pIn;
3424 
3425   rc = sessionChangesetInvert(&sInput, xOutput, pOut, 0, 0);
3426   sqlite3_free(sInput.buf.aBuf);
3427   return rc;
3428 }
3429 
3430 typedef struct SessionApplyCtx SessionApplyCtx;
3431 struct SessionApplyCtx {
3432   sqlite3 *db;
3433   sqlite3_stmt *pDelete;          /* DELETE statement */
3434   sqlite3_stmt *pUpdate;          /* UPDATE statement */
3435   sqlite3_stmt *pInsert;          /* INSERT statement */
3436   sqlite3_stmt *pSelect;          /* SELECT statement */
3437   int nCol;                       /* Size of azCol[] and abPK[] arrays */
3438   const char **azCol;             /* Array of column names */
3439   u8 *abPK;                       /* Boolean array - true if column is in PK */
3440   int bStat1;                     /* True if table is sqlite_stat1 */
3441   int bDeferConstraints;          /* True to defer constraints */
3442   SessionBuffer constraints;      /* Deferred constraints are stored here */
3443   SessionBuffer rebase;           /* Rebase information (if any) here */
3444   int bRebaseStarted;             /* If table header is already in rebase */
3445 };
3446 
3447 /*
3448 ** Formulate a statement to DELETE a row from database db. Assuming a table
3449 ** structure like this:
3450 **
3451 **     CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c));
3452 **
3453 ** The DELETE statement looks like this:
3454 **
3455 **     DELETE FROM x WHERE a = :1 AND c = :3 AND (:5 OR b IS :2 AND d IS :4)
3456 **
3457 ** Variable :5 (nCol+1) is a boolean. It should be set to 0 if we require
3458 ** matching b and d values, or 1 otherwise. The second case comes up if the
3459 ** conflict handler is invoked with NOTFOUND and returns CHANGESET_REPLACE.
3460 **
3461 ** If successful, SQLITE_OK is returned and SessionApplyCtx.pDelete is left
3462 ** pointing to the prepared version of the SQL statement.
3463 */
3464 static int sessionDeleteRow(
3465   sqlite3 *db,                    /* Database handle */
3466   const char *zTab,               /* Table name */
3467   SessionApplyCtx *p              /* Session changeset-apply context */
3468 ){
3469   int i;
3470   const char *zSep = "";
3471   int rc = SQLITE_OK;
3472   SessionBuffer buf = {0, 0, 0};
3473   int nPk = 0;
3474 
3475   sessionAppendStr(&buf, "DELETE FROM ", &rc);
3476   sessionAppendIdent(&buf, zTab, &rc);
3477   sessionAppendStr(&buf, " WHERE ", &rc);
3478 
3479   for(i=0; i<p->nCol; i++){
3480     if( p->abPK[i] ){
3481       nPk++;
3482       sessionAppendStr(&buf, zSep, &rc);
3483       sessionAppendIdent(&buf, p->azCol[i], &rc);
3484       sessionAppendStr(&buf, " = ?", &rc);
3485       sessionAppendInteger(&buf, i+1, &rc);
3486       zSep = " AND ";
3487     }
3488   }
3489 
3490   if( nPk<p->nCol ){
3491     sessionAppendStr(&buf, " AND (?", &rc);
3492     sessionAppendInteger(&buf, p->nCol+1, &rc);
3493     sessionAppendStr(&buf, " OR ", &rc);
3494 
3495     zSep = "";
3496     for(i=0; i<p->nCol; i++){
3497       if( !p->abPK[i] ){
3498         sessionAppendStr(&buf, zSep, &rc);
3499         sessionAppendIdent(&buf, p->azCol[i], &rc);
3500         sessionAppendStr(&buf, " IS ?", &rc);
3501         sessionAppendInteger(&buf, i+1, &rc);
3502         zSep = "AND ";
3503       }
3504     }
3505     sessionAppendStr(&buf, ")", &rc);
3506   }
3507 
3508   if( rc==SQLITE_OK ){
3509     rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pDelete, 0);
3510   }
3511   sqlite3_free(buf.aBuf);
3512 
3513   return rc;
3514 }
3515 
3516 /*
3517 ** Formulate and prepare a statement to UPDATE a row from database db.
3518 ** Assuming a table structure like this:
3519 **
3520 **     CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c));
3521 **
3522 ** The UPDATE statement looks like this:
3523 **
3524 **     UPDATE x SET
3525 **     a = CASE WHEN ?2  THEN ?3  ELSE a END,
3526 **     b = CASE WHEN ?5  THEN ?6  ELSE b END,
3527 **     c = CASE WHEN ?8  THEN ?9  ELSE c END,
3528 **     d = CASE WHEN ?11 THEN ?12 ELSE d END
3529 **     WHERE a = ?1 AND c = ?7 AND (?13 OR
3530 **       (?5==0 OR b IS ?4) AND (?11==0 OR d IS ?10) AND
3531 **     )
3532 **
3533 ** For each column in the table, there are three variables to bind:
3534 **
3535 **     ?(i*3+1)    The old.* value of the column, if any.
3536 **     ?(i*3+2)    A boolean flag indicating that the value is being modified.
3537 **     ?(i*3+3)    The new.* value of the column, if any.
3538 **
3539 ** Also, a boolean flag that, if set to true, causes the statement to update
3540 ** a row even if the non-PK values do not match. This is required if the
3541 ** conflict-handler is invoked with CHANGESET_DATA and returns
3542 ** CHANGESET_REPLACE. This is variable "?(nCol*3+1)".
3543 **
3544 ** If successful, SQLITE_OK is returned and SessionApplyCtx.pUpdate is left
3545 ** pointing to the prepared version of the SQL statement.
3546 */
3547 static int sessionUpdateRow(
3548   sqlite3 *db,                    /* Database handle */
3549   const char *zTab,               /* Table name */
3550   SessionApplyCtx *p              /* Session changeset-apply context */
3551 ){
3552   int rc = SQLITE_OK;
3553   int i;
3554   const char *zSep = "";
3555   SessionBuffer buf = {0, 0, 0};
3556 
3557   /* Append "UPDATE tbl SET " */
3558   sessionAppendStr(&buf, "UPDATE ", &rc);
3559   sessionAppendIdent(&buf, zTab, &rc);
3560   sessionAppendStr(&buf, " SET ", &rc);
3561 
3562   /* Append the assignments */
3563   for(i=0; i<p->nCol; i++){
3564     sessionAppendStr(&buf, zSep, &rc);
3565     sessionAppendIdent(&buf, p->azCol[i], &rc);
3566     sessionAppendStr(&buf, " = CASE WHEN ?", &rc);
3567     sessionAppendInteger(&buf, i*3+2, &rc);
3568     sessionAppendStr(&buf, " THEN ?", &rc);
3569     sessionAppendInteger(&buf, i*3+3, &rc);
3570     sessionAppendStr(&buf, " ELSE ", &rc);
3571     sessionAppendIdent(&buf, p->azCol[i], &rc);
3572     sessionAppendStr(&buf, " END", &rc);
3573     zSep = ", ";
3574   }
3575 
3576   /* Append the PK part of the WHERE clause */
3577   sessionAppendStr(&buf, " WHERE ", &rc);
3578   for(i=0; i<p->nCol; i++){
3579     if( p->abPK[i] ){
3580       sessionAppendIdent(&buf, p->azCol[i], &rc);
3581       sessionAppendStr(&buf, " = ?", &rc);
3582       sessionAppendInteger(&buf, i*3+1, &rc);
3583       sessionAppendStr(&buf, " AND ", &rc);
3584     }
3585   }
3586 
3587   /* Append the non-PK part of the WHERE clause */
3588   sessionAppendStr(&buf, " (?", &rc);
3589   sessionAppendInteger(&buf, p->nCol*3+1, &rc);
3590   sessionAppendStr(&buf, " OR 1", &rc);
3591   for(i=0; i<p->nCol; i++){
3592     if( !p->abPK[i] ){
3593       sessionAppendStr(&buf, " AND (?", &rc);
3594       sessionAppendInteger(&buf, i*3+2, &rc);
3595       sessionAppendStr(&buf, "=0 OR ", &rc);
3596       sessionAppendIdent(&buf, p->azCol[i], &rc);
3597       sessionAppendStr(&buf, " IS ?", &rc);
3598       sessionAppendInteger(&buf, i*3+1, &rc);
3599       sessionAppendStr(&buf, ")", &rc);
3600     }
3601   }
3602   sessionAppendStr(&buf, ")", &rc);
3603 
3604   if( rc==SQLITE_OK ){
3605     rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pUpdate, 0);
3606   }
3607   sqlite3_free(buf.aBuf);
3608 
3609   return rc;
3610 }
3611 
3612 
3613 /*
3614 ** Formulate and prepare an SQL statement to query table zTab by primary
3615 ** key. Assuming the following table structure:
3616 **
3617 **     CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c));
3618 **
3619 ** The SELECT statement looks like this:
3620 **
3621 **     SELECT * FROM x WHERE a = ?1 AND c = ?3
3622 **
3623 ** If successful, SQLITE_OK is returned and SessionApplyCtx.pSelect is left
3624 ** pointing to the prepared version of the SQL statement.
3625 */
3626 static int sessionSelectRow(
3627   sqlite3 *db,                    /* Database handle */
3628   const char *zTab,               /* Table name */
3629   SessionApplyCtx *p              /* Session changeset-apply context */
3630 ){
3631   return sessionSelectStmt(
3632       db, "main", zTab, p->nCol, p->azCol, p->abPK, &p->pSelect);
3633 }
3634 
3635 /*
3636 ** Formulate and prepare an INSERT statement to add a record to table zTab.
3637 ** For example:
3638 **
3639 **     INSERT INTO main."zTab" VALUES(?1, ?2, ?3 ...);
3640 **
3641 ** If successful, SQLITE_OK is returned and SessionApplyCtx.pInsert is left
3642 ** pointing to the prepared version of the SQL statement.
3643 */
3644 static int sessionInsertRow(
3645   sqlite3 *db,                    /* Database handle */
3646   const char *zTab,               /* Table name */
3647   SessionApplyCtx *p              /* Session changeset-apply context */
3648 ){
3649   int rc = SQLITE_OK;
3650   int i;
3651   SessionBuffer buf = {0, 0, 0};
3652 
3653   sessionAppendStr(&buf, "INSERT INTO main.", &rc);
3654   sessionAppendIdent(&buf, zTab, &rc);
3655   sessionAppendStr(&buf, "(", &rc);
3656   for(i=0; i<p->nCol; i++){
3657     if( i!=0 ) sessionAppendStr(&buf, ", ", &rc);
3658     sessionAppendIdent(&buf, p->azCol[i], &rc);
3659   }
3660 
3661   sessionAppendStr(&buf, ") VALUES(?", &rc);
3662   for(i=1; i<p->nCol; i++){
3663     sessionAppendStr(&buf, ", ?", &rc);
3664   }
3665   sessionAppendStr(&buf, ")", &rc);
3666 
3667   if( rc==SQLITE_OK ){
3668     rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pInsert, 0);
3669   }
3670   sqlite3_free(buf.aBuf);
3671   return rc;
3672 }
3673 
3674 static int sessionPrepare(sqlite3 *db, sqlite3_stmt **pp, const char *zSql){
3675   return sqlite3_prepare_v2(db, zSql, -1, pp, 0);
3676 }
3677 
3678 /*
3679 ** Prepare statements for applying changes to the sqlite_stat1 table.
3680 ** These are similar to those created by sessionSelectRow(),
3681 ** sessionInsertRow(), sessionUpdateRow() and sessionDeleteRow() for
3682 ** other tables.
3683 */
3684 static int sessionStat1Sql(sqlite3 *db, SessionApplyCtx *p){
3685   int rc = sessionSelectRow(db, "sqlite_stat1", p);
3686   if( rc==SQLITE_OK ){
3687     rc = sessionPrepare(db, &p->pInsert,
3688         "INSERT INTO main.sqlite_stat1 VALUES(?1, "
3689         "CASE WHEN length(?2)=0 AND typeof(?2)='blob' THEN NULL ELSE ?2 END, "
3690         "?3)"
3691     );
3692   }
3693   if( rc==SQLITE_OK ){
3694     rc = sessionPrepare(db, &p->pUpdate,
3695         "UPDATE main.sqlite_stat1 SET "
3696         "tbl = CASE WHEN ?2 THEN ?3 ELSE tbl END, "
3697         "idx = CASE WHEN ?5 THEN ?6 ELSE idx END, "
3698         "stat = CASE WHEN ?8 THEN ?9 ELSE stat END  "
3699         "WHERE tbl=?1 AND idx IS "
3700         "CASE WHEN length(?4)=0 AND typeof(?4)='blob' THEN NULL ELSE ?4 END "
3701         "AND (?10 OR ?8=0 OR stat IS ?7)"
3702     );
3703   }
3704   if( rc==SQLITE_OK ){
3705     rc = sessionPrepare(db, &p->pDelete,
3706         "DELETE FROM main.sqlite_stat1 WHERE tbl=?1 AND idx IS "
3707         "CASE WHEN length(?2)=0 AND typeof(?2)='blob' THEN NULL ELSE ?2 END "
3708         "AND (?4 OR stat IS ?3)"
3709     );
3710   }
3711   return rc;
3712 }
3713 
3714 /*
3715 ** A wrapper around sqlite3_bind_value() that detects an extra problem.
3716 ** See comments in the body of this function for details.
3717 */
3718 static int sessionBindValue(
3719   sqlite3_stmt *pStmt,            /* Statement to bind value to */
3720   int i,                          /* Parameter number to bind to */
3721   sqlite3_value *pVal             /* Value to bind */
3722 ){
3723   int eType = sqlite3_value_type(pVal);
3724   /* COVERAGE: The (pVal->z==0) branch is never true using current versions
3725   ** of SQLite. If a malloc fails in an sqlite3_value_xxx() function, either
3726   ** the (pVal->z) variable remains as it was or the type of the value is
3727   ** set to SQLITE_NULL.  */
3728   if( (eType==SQLITE_TEXT || eType==SQLITE_BLOB) && pVal->z==0 ){
3729     /* This condition occurs when an earlier OOM in a call to
3730     ** sqlite3_value_text() or sqlite3_value_blob() (perhaps from within
3731     ** a conflict-handler) has zeroed the pVal->z pointer. Return NOMEM. */
3732     return SQLITE_NOMEM;
3733   }
3734   return sqlite3_bind_value(pStmt, i, pVal);
3735 }
3736 
3737 /*
3738 ** Iterator pIter must point to an SQLITE_INSERT entry. This function
3739 ** transfers new.* values from the current iterator entry to statement
3740 ** pStmt. The table being inserted into has nCol columns.
3741 **
3742 ** New.* value $i from the iterator is bound to variable ($i+1) of
3743 ** statement pStmt. If parameter abPK is NULL, all values from 0 to (nCol-1)
3744 ** are transfered to the statement. Otherwise, if abPK is not NULL, it points
3745 ** to an array nCol elements in size. In this case only those values for
3746 ** which abPK[$i] is true are read from the iterator and bound to the
3747 ** statement.
3748 **
3749 ** An SQLite error code is returned if an error occurs. Otherwise, SQLITE_OK.
3750 */
3751 static int sessionBindRow(
3752   sqlite3_changeset_iter *pIter,  /* Iterator to read values from */
3753   int(*xValue)(sqlite3_changeset_iter *, int, sqlite3_value **),
3754   int nCol,                       /* Number of columns */
3755   u8 *abPK,                       /* If not NULL, bind only if true */
3756   sqlite3_stmt *pStmt             /* Bind values to this statement */
3757 ){
3758   int i;
3759   int rc = SQLITE_OK;
3760 
3761   /* Neither sqlite3changeset_old or sqlite3changeset_new can fail if the
3762   ** argument iterator points to a suitable entry. Make sure that xValue
3763   ** is one of these to guarantee that it is safe to ignore the return
3764   ** in the code below. */
3765   assert( xValue==sqlite3changeset_old || xValue==sqlite3changeset_new );
3766 
3767   for(i=0; rc==SQLITE_OK && i<nCol; i++){
3768     if( !abPK || abPK[i] ){
3769       sqlite3_value *pVal;
3770       (void)xValue(pIter, i, &pVal);
3771       if( pVal==0 ){
3772         /* The value in the changeset was "undefined". This indicates a
3773         ** corrupt changeset blob.  */
3774         rc = SQLITE_CORRUPT_BKPT;
3775       }else{
3776         rc = sessionBindValue(pStmt, i+1, pVal);
3777       }
3778     }
3779   }
3780   return rc;
3781 }
3782 
3783 /*
3784 ** SQL statement pSelect is as generated by the sessionSelectRow() function.
3785 ** This function binds the primary key values from the change that changeset
3786 ** iterator pIter points to to the SELECT and attempts to seek to the table
3787 ** entry. If a row is found, the SELECT statement left pointing at the row
3788 ** and SQLITE_ROW is returned. Otherwise, if no row is found and no error
3789 ** has occured, the statement is reset and SQLITE_OK is returned. If an
3790 ** error occurs, the statement is reset and an SQLite error code is returned.
3791 **
3792 ** If this function returns SQLITE_ROW, the caller must eventually reset()
3793 ** statement pSelect. If any other value is returned, the statement does
3794 ** not require a reset().
3795 **
3796 ** If the iterator currently points to an INSERT record, bind values from the
3797 ** new.* record to the SELECT statement. Or, if it points to a DELETE or
3798 ** UPDATE, bind values from the old.* record.
3799 */
3800 static int sessionSeekToRow(
3801   sqlite3 *db,                    /* Database handle */
3802   sqlite3_changeset_iter *pIter,  /* Changeset iterator */
3803   u8 *abPK,                       /* Primary key flags array */
3804   sqlite3_stmt *pSelect           /* SELECT statement from sessionSelectRow() */
3805 ){
3806   int rc;                         /* Return code */
3807   int nCol;                       /* Number of columns in table */
3808   int op;                         /* Changset operation (SQLITE_UPDATE etc.) */
3809   const char *zDummy;             /* Unused */
3810 
3811   sqlite3changeset_op(pIter, &zDummy, &nCol, &op, 0);
3812   rc = sessionBindRow(pIter,
3813       op==SQLITE_INSERT ? sqlite3changeset_new : sqlite3changeset_old,
3814       nCol, abPK, pSelect
3815   );
3816 
3817   if( rc==SQLITE_OK ){
3818     rc = sqlite3_step(pSelect);
3819     if( rc!=SQLITE_ROW ) rc = sqlite3_reset(pSelect);
3820   }
3821 
3822   return rc;
3823 }
3824 
3825 /*
3826 ** This function is called from within sqlite3changset_apply_v2() when
3827 ** a conflict is encountered and resolved using conflict resolution
3828 ** mode eType (either SQLITE_CHANGESET_OMIT or SQLITE_CHANGESET_REPLACE)..
3829 ** It adds a conflict resolution record to the buffer in
3830 ** SessionApplyCtx.rebase, which will eventually be returned to the caller
3831 ** of apply_v2() as the "rebase" buffer.
3832 **
3833 ** Return SQLITE_OK if successful, or an SQLite error code otherwise.
3834 */
3835 static int sessionRebaseAdd(
3836   SessionApplyCtx *p,             /* Apply context */
3837   int eType,                      /* Conflict resolution (OMIT or REPLACE) */
3838   sqlite3_changeset_iter *pIter   /* Iterator pointing at current change */
3839 ){
3840   int rc = SQLITE_OK;
3841   int i;
3842   int eOp = pIter->op;
3843   if( p->bRebaseStarted==0 ){
3844     /* Append a table-header to the rebase buffer */
3845     const char *zTab = pIter->zTab;
3846     sessionAppendByte(&p->rebase, 'T', &rc);
3847     sessionAppendVarint(&p->rebase, p->nCol, &rc);
3848     sessionAppendBlob(&p->rebase, p->abPK, p->nCol, &rc);
3849     sessionAppendBlob(&p->rebase, (u8*)zTab, (int)strlen(zTab)+1, &rc);
3850     p->bRebaseStarted = 1;
3851   }
3852 
3853   assert( eType==SQLITE_CHANGESET_REPLACE||eType==SQLITE_CHANGESET_OMIT );
3854   assert( eOp==SQLITE_DELETE || eOp==SQLITE_INSERT || eOp==SQLITE_UPDATE );
3855 
3856   sessionAppendByte(&p->rebase,
3857       (eOp==SQLITE_DELETE ? SQLITE_DELETE : SQLITE_INSERT), &rc
3858   );
3859   sessionAppendByte(&p->rebase, (eType==SQLITE_CHANGESET_REPLACE), &rc);
3860   for(i=0; i<p->nCol; i++){
3861     sqlite3_value *pVal = 0;
3862     if( eOp==SQLITE_DELETE || (eOp==SQLITE_UPDATE && p->abPK[i]) ){
3863       sqlite3changeset_old(pIter, i, &pVal);
3864     }else{
3865       sqlite3changeset_new(pIter, i, &pVal);
3866     }
3867     sessionAppendValue(&p->rebase, pVal, &rc);
3868   }
3869 
3870   return rc;
3871 }
3872 
3873 /*
3874 ** Invoke the conflict handler for the change that the changeset iterator
3875 ** currently points to.
3876 **
3877 ** Argument eType must be either CHANGESET_DATA or CHANGESET_CONFLICT.
3878 ** If argument pbReplace is NULL, then the type of conflict handler invoked
3879 ** depends solely on eType, as follows:
3880 **
3881 **    eType value                 Value passed to xConflict
3882 **    -------------------------------------------------
3883 **    CHANGESET_DATA              CHANGESET_NOTFOUND
3884 **    CHANGESET_CONFLICT          CHANGESET_CONSTRAINT
3885 **
3886 ** Or, if pbReplace is not NULL, then an attempt is made to find an existing
3887 ** record with the same primary key as the record about to be deleted, updated
3888 ** or inserted. If such a record can be found, it is available to the conflict
3889 ** handler as the "conflicting" record. In this case the type of conflict
3890 ** handler invoked is as follows:
3891 **
3892 **    eType value         PK Record found?   Value passed to xConflict
3893 **    ----------------------------------------------------------------
3894 **    CHANGESET_DATA      Yes                CHANGESET_DATA
3895 **    CHANGESET_DATA      No                 CHANGESET_NOTFOUND
3896 **    CHANGESET_CONFLICT  Yes                CHANGESET_CONFLICT
3897 **    CHANGESET_CONFLICT  No                 CHANGESET_CONSTRAINT
3898 **
3899 ** If pbReplace is not NULL, and a record with a matching PK is found, and
3900 ** the conflict handler function returns SQLITE_CHANGESET_REPLACE, *pbReplace
3901 ** is set to non-zero before returning SQLITE_OK.
3902 **
3903 ** If the conflict handler returns SQLITE_CHANGESET_ABORT, SQLITE_ABORT is
3904 ** returned. Or, if the conflict handler returns an invalid value,
3905 ** SQLITE_MISUSE. If the conflict handler returns SQLITE_CHANGESET_OMIT,
3906 ** this function returns SQLITE_OK.
3907 */
3908 static int sessionConflictHandler(
3909   int eType,                      /* Either CHANGESET_DATA or CONFLICT */
3910   SessionApplyCtx *p,             /* changeset_apply() context */
3911   sqlite3_changeset_iter *pIter,  /* Changeset iterator */
3912   int(*xConflict)(void *, int, sqlite3_changeset_iter*),
3913   void *pCtx,                     /* First argument for conflict handler */
3914   int *pbReplace                  /* OUT: Set to true if PK row is found */
3915 ){
3916   int res = 0;                    /* Value returned by conflict handler */
3917   int rc;
3918   int nCol;
3919   int op;
3920   const char *zDummy;
3921 
3922   sqlite3changeset_op(pIter, &zDummy, &nCol, &op, 0);
3923 
3924   assert( eType==SQLITE_CHANGESET_CONFLICT || eType==SQLITE_CHANGESET_DATA );
3925   assert( SQLITE_CHANGESET_CONFLICT+1==SQLITE_CHANGESET_CONSTRAINT );
3926   assert( SQLITE_CHANGESET_DATA+1==SQLITE_CHANGESET_NOTFOUND );
3927 
3928   /* Bind the new.* PRIMARY KEY values to the SELECT statement. */
3929   if( pbReplace ){
3930     rc = sessionSeekToRow(p->db, pIter, p->abPK, p->pSelect);
3931   }else{
3932     rc = SQLITE_OK;
3933   }
3934 
3935   if( rc==SQLITE_ROW ){
3936     /* There exists another row with the new.* primary key. */
3937     pIter->pConflict = p->pSelect;
3938     res = xConflict(pCtx, eType, pIter);
3939     pIter->pConflict = 0;
3940     rc = sqlite3_reset(p->pSelect);
3941   }else if( rc==SQLITE_OK ){
3942     if( p->bDeferConstraints && eType==SQLITE_CHANGESET_CONFLICT ){
3943       /* Instead of invoking the conflict handler, append the change blob
3944       ** to the SessionApplyCtx.constraints buffer. */
3945       u8 *aBlob = &pIter->in.aData[pIter->in.iCurrent];
3946       int nBlob = pIter->in.iNext - pIter->in.iCurrent;
3947       sessionAppendBlob(&p->constraints, aBlob, nBlob, &rc);
3948       return SQLITE_OK;
3949     }else{
3950       /* No other row with the new.* primary key. */
3951       res = xConflict(pCtx, eType+1, pIter);
3952       if( res==SQLITE_CHANGESET_REPLACE ) rc = SQLITE_MISUSE;
3953     }
3954   }
3955 
3956   if( rc==SQLITE_OK ){
3957     switch( res ){
3958       case SQLITE_CHANGESET_REPLACE:
3959         assert( pbReplace );
3960         *pbReplace = 1;
3961         break;
3962 
3963       case SQLITE_CHANGESET_OMIT:
3964         break;
3965 
3966       case SQLITE_CHANGESET_ABORT:
3967         rc = SQLITE_ABORT;
3968         break;
3969 
3970       default:
3971         rc = SQLITE_MISUSE;
3972         break;
3973     }
3974     if( rc==SQLITE_OK ){
3975       rc = sessionRebaseAdd(p, res, pIter);
3976     }
3977   }
3978 
3979   return rc;
3980 }
3981 
3982 /*
3983 ** Attempt to apply the change that the iterator passed as the first argument
3984 ** currently points to to the database. If a conflict is encountered, invoke
3985 ** the conflict handler callback.
3986 **
3987 ** If argument pbRetry is NULL, then ignore any CHANGESET_DATA conflict. If
3988 ** one is encountered, update or delete the row with the matching primary key
3989 ** instead. Or, if pbRetry is not NULL and a CHANGESET_DATA conflict occurs,
3990 ** invoke the conflict handler. If it returns CHANGESET_REPLACE, set *pbRetry
3991 ** to true before returning. In this case the caller will invoke this function
3992 ** again, this time with pbRetry set to NULL.
3993 **
3994 ** If argument pbReplace is NULL and a CHANGESET_CONFLICT conflict is
3995 ** encountered invoke the conflict handler with CHANGESET_CONSTRAINT instead.
3996 ** Or, if pbReplace is not NULL, invoke it with CHANGESET_CONFLICT. If such
3997 ** an invocation returns SQLITE_CHANGESET_REPLACE, set *pbReplace to true
3998 ** before retrying. In this case the caller attempts to remove the conflicting
3999 ** row before invoking this function again, this time with pbReplace set
4000 ** to NULL.
4001 **
4002 ** If any conflict handler returns SQLITE_CHANGESET_ABORT, this function
4003 ** returns SQLITE_ABORT. Otherwise, if no error occurs, SQLITE_OK is
4004 ** returned.
4005 */
4006 static int sessionApplyOneOp(
4007   sqlite3_changeset_iter *pIter,  /* Changeset iterator */
4008   SessionApplyCtx *p,             /* changeset_apply() context */
4009   int(*xConflict)(void *, int, sqlite3_changeset_iter *),
4010   void *pCtx,                     /* First argument for the conflict handler */
4011   int *pbReplace,                 /* OUT: True to remove PK row and retry */
4012   int *pbRetry                    /* OUT: True to retry. */
4013 ){
4014   const char *zDummy;
4015   int op;
4016   int nCol;
4017   int rc = SQLITE_OK;
4018 
4019   assert( p->pDelete && p->pUpdate && p->pInsert && p->pSelect );
4020   assert( p->azCol && p->abPK );
4021   assert( !pbReplace || *pbReplace==0 );
4022 
4023   sqlite3changeset_op(pIter, &zDummy, &nCol, &op, 0);
4024 
4025   if( op==SQLITE_DELETE ){
4026 
4027     /* Bind values to the DELETE statement. If conflict handling is required,
4028     ** bind values for all columns and set bound variable (nCol+1) to true.
4029     ** Or, if conflict handling is not required, bind just the PK column
4030     ** values and, if it exists, set (nCol+1) to false. Conflict handling
4031     ** is not required if:
4032     **
4033     **   * this is a patchset, or
4034     **   * (pbRetry==0), or
4035     **   * all columns of the table are PK columns (in this case there is
4036     **     no (nCol+1) variable to bind to).
4037     */
4038     u8 *abPK = (pIter->bPatchset ? p->abPK : 0);
4039     rc = sessionBindRow(pIter, sqlite3changeset_old, nCol, abPK, p->pDelete);
4040     if( rc==SQLITE_OK && sqlite3_bind_parameter_count(p->pDelete)>nCol ){
4041       rc = sqlite3_bind_int(p->pDelete, nCol+1, (pbRetry==0 || abPK));
4042     }
4043     if( rc!=SQLITE_OK ) return rc;
4044 
4045     sqlite3_step(p->pDelete);
4046     rc = sqlite3_reset(p->pDelete);
4047     if( rc==SQLITE_OK && sqlite3_changes(p->db)==0 ){
4048       rc = sessionConflictHandler(
4049           SQLITE_CHANGESET_DATA, p, pIter, xConflict, pCtx, pbRetry
4050       );
4051     }else if( (rc&0xff)==SQLITE_CONSTRAINT ){
4052       rc = sessionConflictHandler(
4053           SQLITE_CHANGESET_CONFLICT, p, pIter, xConflict, pCtx, 0
4054       );
4055     }
4056 
4057   }else if( op==SQLITE_UPDATE ){
4058     int i;
4059 
4060     /* Bind values to the UPDATE statement. */
4061     for(i=0; rc==SQLITE_OK && i<nCol; i++){
4062       sqlite3_value *pOld = sessionChangesetOld(pIter, i);
4063       sqlite3_value *pNew = sessionChangesetNew(pIter, i);
4064 
4065       sqlite3_bind_int(p->pUpdate, i*3+2, !!pNew);
4066       if( pOld ){
4067         rc = sessionBindValue(p->pUpdate, i*3+1, pOld);
4068       }
4069       if( rc==SQLITE_OK && pNew ){
4070         rc = sessionBindValue(p->pUpdate, i*3+3, pNew);
4071       }
4072     }
4073     if( rc==SQLITE_OK ){
4074       sqlite3_bind_int(p->pUpdate, nCol*3+1, pbRetry==0 || pIter->bPatchset);
4075     }
4076     if( rc!=SQLITE_OK ) return rc;
4077 
4078     /* Attempt the UPDATE. In the case of a NOTFOUND or DATA conflict,
4079     ** the result will be SQLITE_OK with 0 rows modified. */
4080     sqlite3_step(p->pUpdate);
4081     rc = sqlite3_reset(p->pUpdate);
4082 
4083     if( rc==SQLITE_OK && sqlite3_changes(p->db)==0 ){
4084       /* A NOTFOUND or DATA error. Search the table to see if it contains
4085       ** a row with a matching primary key. If so, this is a DATA conflict.
4086       ** Otherwise, if there is no primary key match, it is a NOTFOUND. */
4087 
4088       rc = sessionConflictHandler(
4089           SQLITE_CHANGESET_DATA, p, pIter, xConflict, pCtx, pbRetry
4090       );
4091 
4092     }else if( (rc&0xff)==SQLITE_CONSTRAINT ){
4093       /* This is always a CONSTRAINT conflict. */
4094       rc = sessionConflictHandler(
4095           SQLITE_CHANGESET_CONFLICT, p, pIter, xConflict, pCtx, 0
4096       );
4097     }
4098 
4099   }else{
4100     assert( op==SQLITE_INSERT );
4101     if( p->bStat1 ){
4102       /* Check if there is a conflicting row. For sqlite_stat1, this needs
4103       ** to be done using a SELECT, as there is no PRIMARY KEY in the
4104       ** database schema to throw an exception if a duplicate is inserted.  */
4105       rc = sessionSeekToRow(p->db, pIter, p->abPK, p->pSelect);
4106       if( rc==SQLITE_ROW ){
4107         rc = SQLITE_CONSTRAINT;
4108         sqlite3_reset(p->pSelect);
4109       }
4110     }
4111 
4112     if( rc==SQLITE_OK ){
4113       rc = sessionBindRow(pIter, sqlite3changeset_new, nCol, 0, p->pInsert);
4114       if( rc!=SQLITE_OK ) return rc;
4115 
4116       sqlite3_step(p->pInsert);
4117       rc = sqlite3_reset(p->pInsert);
4118     }
4119 
4120     if( (rc&0xff)==SQLITE_CONSTRAINT ){
4121       rc = sessionConflictHandler(
4122           SQLITE_CHANGESET_CONFLICT, p, pIter, xConflict, pCtx, pbReplace
4123       );
4124     }
4125   }
4126 
4127   return rc;
4128 }
4129 
4130 /*
4131 ** Attempt to apply the change that the iterator passed as the first argument
4132 ** currently points to to the database. If a conflict is encountered, invoke
4133 ** the conflict handler callback.
4134 **
4135 ** The difference between this function and sessionApplyOne() is that this
4136 ** function handles the case where the conflict-handler is invoked and
4137 ** returns SQLITE_CHANGESET_REPLACE - indicating that the change should be
4138 ** retried in some manner.
4139 */
4140 static int sessionApplyOneWithRetry(
4141   sqlite3 *db,                    /* Apply change to "main" db of this handle */
4142   sqlite3_changeset_iter *pIter,  /* Changeset iterator to read change from */
4143   SessionApplyCtx *pApply,        /* Apply context */
4144   int(*xConflict)(void*, int, sqlite3_changeset_iter*),
4145   void *pCtx                      /* First argument passed to xConflict */
4146 ){
4147   int bReplace = 0;
4148   int bRetry = 0;
4149   int rc;
4150 
4151   rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, &bReplace, &bRetry);
4152   if( rc==SQLITE_OK ){
4153     /* If the bRetry flag is set, the change has not been applied due to an
4154     ** SQLITE_CHANGESET_DATA problem (i.e. this is an UPDATE or DELETE and
4155     ** a row with the correct PK is present in the db, but one or more other
4156     ** fields do not contain the expected values) and the conflict handler
4157     ** returned SQLITE_CHANGESET_REPLACE. In this case retry the operation,
4158     ** but pass NULL as the final argument so that sessionApplyOneOp() ignores
4159     ** the SQLITE_CHANGESET_DATA problem.  */
4160     if( bRetry ){
4161       assert( pIter->op==SQLITE_UPDATE || pIter->op==SQLITE_DELETE );
4162       rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, 0, 0);
4163     }
4164 
4165     /* If the bReplace flag is set, the change is an INSERT that has not
4166     ** been performed because the database already contains a row with the
4167     ** specified primary key and the conflict handler returned
4168     ** SQLITE_CHANGESET_REPLACE. In this case remove the conflicting row
4169     ** before reattempting the INSERT.  */
4170     else if( bReplace ){
4171       assert( pIter->op==SQLITE_INSERT );
4172       rc = sqlite3_exec(db, "SAVEPOINT replace_op", 0, 0, 0);
4173       if( rc==SQLITE_OK ){
4174         rc = sessionBindRow(pIter,
4175             sqlite3changeset_new, pApply->nCol, pApply->abPK, pApply->pDelete);
4176         sqlite3_bind_int(pApply->pDelete, pApply->nCol+1, 1);
4177       }
4178       if( rc==SQLITE_OK ){
4179         sqlite3_step(pApply->pDelete);
4180         rc = sqlite3_reset(pApply->pDelete);
4181       }
4182       if( rc==SQLITE_OK ){
4183         rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, 0, 0);
4184       }
4185       if( rc==SQLITE_OK ){
4186         rc = sqlite3_exec(db, "RELEASE replace_op", 0, 0, 0);
4187       }
4188     }
4189   }
4190 
4191   return rc;
4192 }
4193 
4194 /*
4195 ** Retry the changes accumulated in the pApply->constraints buffer.
4196 */
4197 static int sessionRetryConstraints(
4198   sqlite3 *db,
4199   int bPatchset,
4200   const char *zTab,
4201   SessionApplyCtx *pApply,
4202   int(*xConflict)(void*, int, sqlite3_changeset_iter*),
4203   void *pCtx                      /* First argument passed to xConflict */
4204 ){
4205   int rc = SQLITE_OK;
4206 
4207   while( pApply->constraints.nBuf ){
4208     sqlite3_changeset_iter *pIter2 = 0;
4209     SessionBuffer cons = pApply->constraints;
4210     memset(&pApply->constraints, 0, sizeof(SessionBuffer));
4211 
4212     rc = sessionChangesetStart(&pIter2, 0, 0, cons.nBuf, cons.aBuf, 0);
4213     if( rc==SQLITE_OK ){
4214       int nByte = 2*pApply->nCol*sizeof(sqlite3_value*);
4215       int rc2;
4216       pIter2->bPatchset = bPatchset;
4217       pIter2->zTab = (char*)zTab;
4218       pIter2->nCol = pApply->nCol;
4219       pIter2->abPK = pApply->abPK;
4220       sessionBufferGrow(&pIter2->tblhdr, nByte, &rc);
4221       pIter2->apValue = (sqlite3_value**)pIter2->tblhdr.aBuf;
4222       if( rc==SQLITE_OK ) memset(pIter2->apValue, 0, nByte);
4223 
4224       while( rc==SQLITE_OK && SQLITE_ROW==sqlite3changeset_next(pIter2) ){
4225         rc = sessionApplyOneWithRetry(db, pIter2, pApply, xConflict, pCtx);
4226       }
4227 
4228       rc2 = sqlite3changeset_finalize(pIter2);
4229       if( rc==SQLITE_OK ) rc = rc2;
4230     }
4231     assert( pApply->bDeferConstraints || pApply->constraints.nBuf==0 );
4232 
4233     sqlite3_free(cons.aBuf);
4234     if( rc!=SQLITE_OK ) break;
4235     if( pApply->constraints.nBuf>=cons.nBuf ){
4236       /* No progress was made on the last round. */
4237       pApply->bDeferConstraints = 0;
4238     }
4239   }
4240 
4241   return rc;
4242 }
4243 
4244 /*
4245 ** Argument pIter is a changeset iterator that has been initialized, but
4246 ** not yet passed to sqlite3changeset_next(). This function applies the
4247 ** changeset to the main database attached to handle "db". The supplied
4248 ** conflict handler callback is invoked to resolve any conflicts encountered
4249 ** while applying the change.
4250 */
4251 static int sessionChangesetApply(
4252   sqlite3 *db,                    /* Apply change to "main" db of this handle */
4253   sqlite3_changeset_iter *pIter,  /* Changeset to apply */
4254   int(*xFilter)(
4255     void *pCtx,                   /* Copy of sixth arg to _apply() */
4256     const char *zTab              /* Table name */
4257   ),
4258   int(*xConflict)(
4259     void *pCtx,                   /* Copy of fifth arg to _apply() */
4260     int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
4261     sqlite3_changeset_iter *p     /* Handle describing change and conflict */
4262   ),
4263   void *pCtx,                     /* First argument passed to xConflict */
4264   void **ppRebase, int *pnRebase, /* OUT: Rebase information */
4265   int flags                       /* SESSION_APPLY_XXX flags */
4266 ){
4267   int schemaMismatch = 0;
4268   int rc = SQLITE_OK;             /* Return code */
4269   const char *zTab = 0;           /* Name of current table */
4270   int nTab = 0;                   /* Result of sqlite3Strlen30(zTab) */
4271   SessionApplyCtx sApply;         /* changeset_apply() context object */
4272   int bPatchset;
4273 
4274   assert( xConflict!=0 );
4275 
4276   pIter->in.bNoDiscard = 1;
4277   memset(&sApply, 0, sizeof(sApply));
4278   sqlite3_mutex_enter(sqlite3_db_mutex(db));
4279   if( (flags & SQLITE_CHANGESETAPPLY_NOSAVEPOINT)==0 ){
4280     rc = sqlite3_exec(db, "SAVEPOINT changeset_apply", 0, 0, 0);
4281   }
4282   if( rc==SQLITE_OK ){
4283     rc = sqlite3_exec(db, "PRAGMA defer_foreign_keys = 1", 0, 0, 0);
4284   }
4285   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3changeset_next(pIter) ){
4286     int nCol;
4287     int op;
4288     const char *zNew;
4289 
4290     sqlite3changeset_op(pIter, &zNew, &nCol, &op, 0);
4291 
4292     if( zTab==0 || sqlite3_strnicmp(zNew, zTab, nTab+1) ){
4293       u8 *abPK;
4294 
4295       rc = sessionRetryConstraints(
4296           db, pIter->bPatchset, zTab, &sApply, xConflict, pCtx
4297       );
4298       if( rc!=SQLITE_OK ) break;
4299 
4300       sqlite3_free((char*)sApply.azCol);  /* cast works around VC++ bug */
4301       sqlite3_finalize(sApply.pDelete);
4302       sqlite3_finalize(sApply.pUpdate);
4303       sqlite3_finalize(sApply.pInsert);
4304       sqlite3_finalize(sApply.pSelect);
4305       sApply.db = db;
4306       sApply.pDelete = 0;
4307       sApply.pUpdate = 0;
4308       sApply.pInsert = 0;
4309       sApply.pSelect = 0;
4310       sApply.nCol = 0;
4311       sApply.azCol = 0;
4312       sApply.abPK = 0;
4313       sApply.bStat1 = 0;
4314       sApply.bDeferConstraints = 1;
4315       sApply.bRebaseStarted = 0;
4316       memset(&sApply.constraints, 0, sizeof(SessionBuffer));
4317 
4318       /* If an xFilter() callback was specified, invoke it now. If the
4319       ** xFilter callback returns zero, skip this table. If it returns
4320       ** non-zero, proceed. */
4321       schemaMismatch = (xFilter && (0==xFilter(pCtx, zNew)));
4322       if( schemaMismatch ){
4323         zTab = sqlite3_mprintf("%s", zNew);
4324         if( zTab==0 ){
4325           rc = SQLITE_NOMEM;
4326           break;
4327         }
4328         nTab = (int)strlen(zTab);
4329         sApply.azCol = (const char **)zTab;
4330       }else{
4331         int nMinCol = 0;
4332         int i;
4333 
4334         sqlite3changeset_pk(pIter, &abPK, 0);
4335         rc = sessionTableInfo(
4336             db, "main", zNew, &sApply.nCol, &zTab, &sApply.azCol, &sApply.abPK
4337         );
4338         if( rc!=SQLITE_OK ) break;
4339         for(i=0; i<sApply.nCol; i++){
4340           if( sApply.abPK[i] ) nMinCol = i+1;
4341         }
4342 
4343         if( sApply.nCol==0 ){
4344           schemaMismatch = 1;
4345           sqlite3_log(SQLITE_SCHEMA,
4346               "sqlite3changeset_apply(): no such table: %s", zTab
4347           );
4348         }
4349         else if( sApply.nCol<nCol ){
4350           schemaMismatch = 1;
4351           sqlite3_log(SQLITE_SCHEMA,
4352               "sqlite3changeset_apply(): table %s has %d columns, "
4353               "expected %d or more",
4354               zTab, sApply.nCol, nCol
4355           );
4356         }
4357         else if( nCol<nMinCol || memcmp(sApply.abPK, abPK, nCol)!=0 ){
4358           schemaMismatch = 1;
4359           sqlite3_log(SQLITE_SCHEMA, "sqlite3changeset_apply(): "
4360               "primary key mismatch for table %s", zTab
4361           );
4362         }
4363         else{
4364           sApply.nCol = nCol;
4365           if( 0==sqlite3_stricmp(zTab, "sqlite_stat1") ){
4366             if( (rc = sessionStat1Sql(db, &sApply) ) ){
4367               break;
4368             }
4369             sApply.bStat1 = 1;
4370           }else{
4371             if((rc = sessionSelectRow(db, zTab, &sApply))
4372                 || (rc = sessionUpdateRow(db, zTab, &sApply))
4373                 || (rc = sessionDeleteRow(db, zTab, &sApply))
4374                 || (rc = sessionInsertRow(db, zTab, &sApply))
4375               ){
4376               break;
4377             }
4378             sApply.bStat1 = 0;
4379           }
4380         }
4381         nTab = sqlite3Strlen30(zTab);
4382       }
4383     }
4384 
4385     /* If there is a schema mismatch on the current table, proceed to the
4386     ** next change. A log message has already been issued. */
4387     if( schemaMismatch ) continue;
4388 
4389     rc = sessionApplyOneWithRetry(db, pIter, &sApply, xConflict, pCtx);
4390   }
4391 
4392   bPatchset = pIter->bPatchset;
4393   if( rc==SQLITE_OK ){
4394     rc = sqlite3changeset_finalize(pIter);
4395   }else{
4396     sqlite3changeset_finalize(pIter);
4397   }
4398 
4399   if( rc==SQLITE_OK ){
4400     rc = sessionRetryConstraints(db, bPatchset, zTab, &sApply, xConflict, pCtx);
4401   }
4402 
4403   if( rc==SQLITE_OK ){
4404     int nFk, notUsed;
4405     sqlite3_db_status(db, SQLITE_DBSTATUS_DEFERRED_FKS, &nFk, &notUsed, 0);
4406     if( nFk!=0 ){
4407       int res = SQLITE_CHANGESET_ABORT;
4408       sqlite3_changeset_iter sIter;
4409       memset(&sIter, 0, sizeof(sIter));
4410       sIter.nCol = nFk;
4411       res = xConflict(pCtx, SQLITE_CHANGESET_FOREIGN_KEY, &sIter);
4412       if( res!=SQLITE_CHANGESET_OMIT ){
4413         rc = SQLITE_CONSTRAINT;
4414       }
4415     }
4416   }
4417   sqlite3_exec(db, "PRAGMA defer_foreign_keys = 0", 0, 0, 0);
4418 
4419   if( (flags & SQLITE_CHANGESETAPPLY_NOSAVEPOINT)==0 ){
4420     if( rc==SQLITE_OK ){
4421       rc = sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0);
4422     }else{
4423       sqlite3_exec(db, "ROLLBACK TO changeset_apply", 0, 0, 0);
4424       sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0);
4425     }
4426   }
4427 
4428   if( rc==SQLITE_OK && bPatchset==0 && ppRebase && pnRebase ){
4429     *ppRebase = (void*)sApply.rebase.aBuf;
4430     *pnRebase = sApply.rebase.nBuf;
4431     sApply.rebase.aBuf = 0;
4432   }
4433   sqlite3_finalize(sApply.pInsert);
4434   sqlite3_finalize(sApply.pDelete);
4435   sqlite3_finalize(sApply.pUpdate);
4436   sqlite3_finalize(sApply.pSelect);
4437   sqlite3_free((char*)sApply.azCol);  /* cast works around VC++ bug */
4438   sqlite3_free((char*)sApply.constraints.aBuf);
4439   sqlite3_free((char*)sApply.rebase.aBuf);
4440   sqlite3_mutex_leave(sqlite3_db_mutex(db));
4441   return rc;
4442 }
4443 
4444 /*
4445 ** Apply the changeset passed via pChangeset/nChangeset to the main
4446 ** database attached to handle "db".
4447 */
4448 int sqlite3changeset_apply_v2(
4449   sqlite3 *db,                    /* Apply change to "main" db of this handle */
4450   int nChangeset,                 /* Size of changeset in bytes */
4451   void *pChangeset,               /* Changeset blob */
4452   int(*xFilter)(
4453     void *pCtx,                   /* Copy of sixth arg to _apply() */
4454     const char *zTab              /* Table name */
4455   ),
4456   int(*xConflict)(
4457     void *pCtx,                   /* Copy of sixth arg to _apply() */
4458     int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
4459     sqlite3_changeset_iter *p     /* Handle describing change and conflict */
4460   ),
4461   void *pCtx,                     /* First argument passed to xConflict */
4462   void **ppRebase, int *pnRebase,
4463   int flags
4464 ){
4465   sqlite3_changeset_iter *pIter;  /* Iterator to skip through changeset */
4466   int bInverse = !!(flags & SQLITE_CHANGESETAPPLY_INVERT);
4467   int rc = sessionChangesetStart(&pIter, 0, 0, nChangeset, pChangeset,bInverse);
4468   if( rc==SQLITE_OK ){
4469     rc = sessionChangesetApply(
4470         db, pIter, xFilter, xConflict, pCtx, ppRebase, pnRebase, flags
4471     );
4472   }
4473   return rc;
4474 }
4475 
4476 /*
4477 ** Apply the changeset passed via pChangeset/nChangeset to the main database
4478 ** attached to handle "db". Invoke the supplied conflict handler callback
4479 ** to resolve any conflicts encountered while applying the change.
4480 */
4481 int sqlite3changeset_apply(
4482   sqlite3 *db,                    /* Apply change to "main" db of this handle */
4483   int nChangeset,                 /* Size of changeset in bytes */
4484   void *pChangeset,               /* Changeset blob */
4485   int(*xFilter)(
4486     void *pCtx,                   /* Copy of sixth arg to _apply() */
4487     const char *zTab              /* Table name */
4488   ),
4489   int(*xConflict)(
4490     void *pCtx,                   /* Copy of fifth arg to _apply() */
4491     int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
4492     sqlite3_changeset_iter *p     /* Handle describing change and conflict */
4493   ),
4494   void *pCtx                      /* First argument passed to xConflict */
4495 ){
4496   return sqlite3changeset_apply_v2(
4497       db, nChangeset, pChangeset, xFilter, xConflict, pCtx, 0, 0, 0
4498   );
4499 }
4500 
4501 /*
4502 ** Apply the changeset passed via xInput/pIn to the main database
4503 ** attached to handle "db". Invoke the supplied conflict handler callback
4504 ** to resolve any conflicts encountered while applying the change.
4505 */
4506 int sqlite3changeset_apply_v2_strm(
4507   sqlite3 *db,                    /* Apply change to "main" db of this handle */
4508   int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
4509   void *pIn,                                          /* First arg for xInput */
4510   int(*xFilter)(
4511     void *pCtx,                   /* Copy of sixth arg to _apply() */
4512     const char *zTab              /* Table name */
4513   ),
4514   int(*xConflict)(
4515     void *pCtx,                   /* Copy of sixth arg to _apply() */
4516     int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
4517     sqlite3_changeset_iter *p     /* Handle describing change and conflict */
4518   ),
4519   void *pCtx,                     /* First argument passed to xConflict */
4520   void **ppRebase, int *pnRebase,
4521   int flags
4522 ){
4523   sqlite3_changeset_iter *pIter;  /* Iterator to skip through changeset */
4524   int bInverse = !!(flags & SQLITE_CHANGESETAPPLY_INVERT);
4525   int rc = sessionChangesetStart(&pIter, xInput, pIn, 0, 0, bInverse);
4526   if( rc==SQLITE_OK ){
4527     rc = sessionChangesetApply(
4528         db, pIter, xFilter, xConflict, pCtx, ppRebase, pnRebase, flags
4529     );
4530   }
4531   return rc;
4532 }
4533 int sqlite3changeset_apply_strm(
4534   sqlite3 *db,                    /* Apply change to "main" db of this handle */
4535   int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
4536   void *pIn,                                          /* First arg for xInput */
4537   int(*xFilter)(
4538     void *pCtx,                   /* Copy of sixth arg to _apply() */
4539     const char *zTab              /* Table name */
4540   ),
4541   int(*xConflict)(
4542     void *pCtx,                   /* Copy of sixth arg to _apply() */
4543     int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
4544     sqlite3_changeset_iter *p     /* Handle describing change and conflict */
4545   ),
4546   void *pCtx                      /* First argument passed to xConflict */
4547 ){
4548   return sqlite3changeset_apply_v2_strm(
4549       db, xInput, pIn, xFilter, xConflict, pCtx, 0, 0, 0
4550   );
4551 }
4552 
4553 /*
4554 ** sqlite3_changegroup handle.
4555 */
4556 struct sqlite3_changegroup {
4557   int rc;                         /* Error code */
4558   int bPatch;                     /* True to accumulate patchsets */
4559   SessionTable *pList;            /* List of tables in current patch */
4560 };
4561 
4562 /*
4563 ** This function is called to merge two changes to the same row together as
4564 ** part of an sqlite3changeset_concat() operation. A new change object is
4565 ** allocated and a pointer to it stored in *ppNew.
4566 */
4567 static int sessionChangeMerge(
4568   SessionTable *pTab,             /* Table structure */
4569   int bRebase,                    /* True for a rebase hash-table */
4570   int bPatchset,                  /* True for patchsets */
4571   SessionChange *pExist,          /* Existing change */
4572   int op2,                        /* Second change operation */
4573   int bIndirect,                  /* True if second change is indirect */
4574   u8 *aRec,                       /* Second change record */
4575   int nRec,                       /* Number of bytes in aRec */
4576   SessionChange **ppNew           /* OUT: Merged change */
4577 ){
4578   SessionChange *pNew = 0;
4579   int rc = SQLITE_OK;
4580 
4581   if( !pExist ){
4582     pNew = (SessionChange *)sqlite3_malloc(sizeof(SessionChange) + nRec);
4583     if( !pNew ){
4584       return SQLITE_NOMEM;
4585     }
4586     memset(pNew, 0, sizeof(SessionChange));
4587     pNew->op = op2;
4588     pNew->bIndirect = bIndirect;
4589     pNew->aRecord = (u8*)&pNew[1];
4590     if( bIndirect==0 || bRebase==0 ){
4591       pNew->nRecord = nRec;
4592       memcpy(pNew->aRecord, aRec, nRec);
4593     }else{
4594       int i;
4595       u8 *pIn = aRec;
4596       u8 *pOut = pNew->aRecord;
4597       for(i=0; i<pTab->nCol; i++){
4598         int nIn = sessionSerialLen(pIn);
4599         if( *pIn==0 ){
4600           *pOut++ = 0;
4601         }else if( pTab->abPK[i]==0 ){
4602           *pOut++ = 0xFF;
4603         }else{
4604           memcpy(pOut, pIn, nIn);
4605           pOut += nIn;
4606         }
4607         pIn += nIn;
4608       }
4609       pNew->nRecord = pOut - pNew->aRecord;
4610     }
4611   }else if( bRebase ){
4612     if( pExist->op==SQLITE_DELETE && pExist->bIndirect ){
4613       *ppNew = pExist;
4614     }else{
4615       int nByte = nRec + pExist->nRecord + sizeof(SessionChange);
4616       pNew = (SessionChange*)sqlite3_malloc(nByte);
4617       if( pNew==0 ){
4618         rc = SQLITE_NOMEM;
4619       }else{
4620         int i;
4621         u8 *a1 = pExist->aRecord;
4622         u8 *a2 = aRec;
4623         u8 *pOut;
4624 
4625         memset(pNew, 0, nByte);
4626         pNew->bIndirect = bIndirect || pExist->bIndirect;
4627         pNew->op = op2;
4628         pOut = pNew->aRecord = (u8*)&pNew[1];
4629 
4630         for(i=0; i<pTab->nCol; i++){
4631           int n1 = sessionSerialLen(a1);
4632           int n2 = sessionSerialLen(a2);
4633           if( *a1==0xFF || (pTab->abPK[i]==0 && bIndirect) ){
4634             *pOut++ = 0xFF;
4635           }else if( *a2==0 ){
4636             memcpy(pOut, a1, n1);
4637             pOut += n1;
4638           }else{
4639             memcpy(pOut, a2, n2);
4640             pOut += n2;
4641           }
4642           a1 += n1;
4643           a2 += n2;
4644         }
4645         pNew->nRecord = pOut - pNew->aRecord;
4646       }
4647       sqlite3_free(pExist);
4648     }
4649   }else{
4650     int op1 = pExist->op;
4651 
4652     /*
4653     **   op1=INSERT, op2=INSERT      ->      Unsupported. Discard op2.
4654     **   op1=INSERT, op2=UPDATE      ->      INSERT.
4655     **   op1=INSERT, op2=DELETE      ->      (none)
4656     **
4657     **   op1=UPDATE, op2=INSERT      ->      Unsupported. Discard op2.
4658     **   op1=UPDATE, op2=UPDATE      ->      UPDATE.
4659     **   op1=UPDATE, op2=DELETE      ->      DELETE.
4660     **
4661     **   op1=DELETE, op2=INSERT      ->      UPDATE.
4662     **   op1=DELETE, op2=UPDATE      ->      Unsupported. Discard op2.
4663     **   op1=DELETE, op2=DELETE      ->      Unsupported. Discard op2.
4664     */
4665     if( (op1==SQLITE_INSERT && op2==SQLITE_INSERT)
4666      || (op1==SQLITE_UPDATE && op2==SQLITE_INSERT)
4667      || (op1==SQLITE_DELETE && op2==SQLITE_UPDATE)
4668      || (op1==SQLITE_DELETE && op2==SQLITE_DELETE)
4669     ){
4670       pNew = pExist;
4671     }else if( op1==SQLITE_INSERT && op2==SQLITE_DELETE ){
4672       sqlite3_free(pExist);
4673       assert( pNew==0 );
4674     }else{
4675       u8 *aExist = pExist->aRecord;
4676       int nByte;
4677       u8 *aCsr;
4678 
4679       /* Allocate a new SessionChange object. Ensure that the aRecord[]
4680       ** buffer of the new object is large enough to hold any record that
4681       ** may be generated by combining the input records.  */
4682       nByte = sizeof(SessionChange) + pExist->nRecord + nRec;
4683       pNew = (SessionChange *)sqlite3_malloc(nByte);
4684       if( !pNew ){
4685         sqlite3_free(pExist);
4686         return SQLITE_NOMEM;
4687       }
4688       memset(pNew, 0, sizeof(SessionChange));
4689       pNew->bIndirect = (bIndirect && pExist->bIndirect);
4690       aCsr = pNew->aRecord = (u8 *)&pNew[1];
4691 
4692       if( op1==SQLITE_INSERT ){             /* INSERT + UPDATE */
4693         u8 *a1 = aRec;
4694         assert( op2==SQLITE_UPDATE );
4695         pNew->op = SQLITE_INSERT;
4696         if( bPatchset==0 ) sessionSkipRecord(&a1, pTab->nCol);
4697         sessionMergeRecord(&aCsr, pTab->nCol, aExist, a1);
4698       }else if( op1==SQLITE_DELETE ){       /* DELETE + INSERT */
4699         assert( op2==SQLITE_INSERT );
4700         pNew->op = SQLITE_UPDATE;
4701         if( bPatchset ){
4702           memcpy(aCsr, aRec, nRec);
4703           aCsr += nRec;
4704         }else{
4705           if( 0==sessionMergeUpdate(&aCsr, pTab, bPatchset, aExist, 0,aRec,0) ){
4706             sqlite3_free(pNew);
4707             pNew = 0;
4708           }
4709         }
4710       }else if( op2==SQLITE_UPDATE ){       /* UPDATE + UPDATE */
4711         u8 *a1 = aExist;
4712         u8 *a2 = aRec;
4713         assert( op1==SQLITE_UPDATE );
4714         if( bPatchset==0 ){
4715           sessionSkipRecord(&a1, pTab->nCol);
4716           sessionSkipRecord(&a2, pTab->nCol);
4717         }
4718         pNew->op = SQLITE_UPDATE;
4719         if( 0==sessionMergeUpdate(&aCsr, pTab, bPatchset, aRec, aExist,a1,a2) ){
4720           sqlite3_free(pNew);
4721           pNew = 0;
4722         }
4723       }else{                                /* UPDATE + DELETE */
4724         assert( op1==SQLITE_UPDATE && op2==SQLITE_DELETE );
4725         pNew->op = SQLITE_DELETE;
4726         if( bPatchset ){
4727           memcpy(aCsr, aRec, nRec);
4728           aCsr += nRec;
4729         }else{
4730           sessionMergeRecord(&aCsr, pTab->nCol, aRec, aExist);
4731         }
4732       }
4733 
4734       if( pNew ){
4735         pNew->nRecord = (int)(aCsr - pNew->aRecord);
4736       }
4737       sqlite3_free(pExist);
4738     }
4739   }
4740 
4741   *ppNew = pNew;
4742   return rc;
4743 }
4744 
4745 /*
4746 ** Add all changes in the changeset traversed by the iterator passed as
4747 ** the first argument to the changegroup hash tables.
4748 */
4749 static int sessionChangesetToHash(
4750   sqlite3_changeset_iter *pIter,   /* Iterator to read from */
4751   sqlite3_changegroup *pGrp,       /* Changegroup object to add changeset to */
4752   int bRebase                      /* True if hash table is for rebasing */
4753 ){
4754   u8 *aRec;
4755   int nRec;
4756   int rc = SQLITE_OK;
4757   SessionTable *pTab = 0;
4758 
4759   while( SQLITE_ROW==sessionChangesetNext(pIter, &aRec, &nRec, 0) ){
4760     const char *zNew;
4761     int nCol;
4762     int op;
4763     int iHash;
4764     int bIndirect;
4765     SessionChange *pChange;
4766     SessionChange *pExist = 0;
4767     SessionChange **pp;
4768 
4769     if( pGrp->pList==0 ){
4770       pGrp->bPatch = pIter->bPatchset;
4771     }else if( pIter->bPatchset!=pGrp->bPatch ){
4772       rc = SQLITE_ERROR;
4773       break;
4774     }
4775 
4776     sqlite3changeset_op(pIter, &zNew, &nCol, &op, &bIndirect);
4777     if( !pTab || sqlite3_stricmp(zNew, pTab->zName) ){
4778       /* Search the list for a matching table */
4779       int nNew = (int)strlen(zNew);
4780       u8 *abPK;
4781 
4782       sqlite3changeset_pk(pIter, &abPK, 0);
4783       for(pTab = pGrp->pList; pTab; pTab=pTab->pNext){
4784         if( 0==sqlite3_strnicmp(pTab->zName, zNew, nNew+1) ) break;
4785       }
4786       if( !pTab ){
4787         SessionTable **ppTab;
4788 
4789         pTab = sqlite3_malloc(sizeof(SessionTable) + nCol + nNew+1);
4790         if( !pTab ){
4791           rc = SQLITE_NOMEM;
4792           break;
4793         }
4794         memset(pTab, 0, sizeof(SessionTable));
4795         pTab->nCol = nCol;
4796         pTab->abPK = (u8*)&pTab[1];
4797         memcpy(pTab->abPK, abPK, nCol);
4798         pTab->zName = (char*)&pTab->abPK[nCol];
4799         memcpy(pTab->zName, zNew, nNew+1);
4800 
4801         /* The new object must be linked on to the end of the list, not
4802         ** simply added to the start of it. This is to ensure that the
4803         ** tables within the output of sqlite3changegroup_output() are in
4804         ** the right order.  */
4805         for(ppTab=&pGrp->pList; *ppTab; ppTab=&(*ppTab)->pNext);
4806         *ppTab = pTab;
4807       }else if( pTab->nCol!=nCol || memcmp(pTab->abPK, abPK, nCol) ){
4808         rc = SQLITE_SCHEMA;
4809         break;
4810       }
4811     }
4812 
4813     if( sessionGrowHash(pIter->bPatchset, pTab) ){
4814       rc = SQLITE_NOMEM;
4815       break;
4816     }
4817     iHash = sessionChangeHash(
4818         pTab, (pIter->bPatchset && op==SQLITE_DELETE), aRec, pTab->nChange
4819     );
4820 
4821     /* Search for existing entry. If found, remove it from the hash table.
4822     ** Code below may link it back in.
4823     */
4824     for(pp=&pTab->apChange[iHash]; *pp; pp=&(*pp)->pNext){
4825       int bPkOnly1 = 0;
4826       int bPkOnly2 = 0;
4827       if( pIter->bPatchset ){
4828         bPkOnly1 = (*pp)->op==SQLITE_DELETE;
4829         bPkOnly2 = op==SQLITE_DELETE;
4830       }
4831       if( sessionChangeEqual(pTab, bPkOnly1, (*pp)->aRecord, bPkOnly2, aRec) ){
4832         pExist = *pp;
4833         *pp = (*pp)->pNext;
4834         pTab->nEntry--;
4835         break;
4836       }
4837     }
4838 
4839     rc = sessionChangeMerge(pTab, bRebase,
4840         pIter->bPatchset, pExist, op, bIndirect, aRec, nRec, &pChange
4841     );
4842     if( rc ) break;
4843     if( pChange ){
4844       pChange->pNext = pTab->apChange[iHash];
4845       pTab->apChange[iHash] = pChange;
4846       pTab->nEntry++;
4847     }
4848   }
4849 
4850   if( rc==SQLITE_OK ) rc = pIter->rc;
4851   return rc;
4852 }
4853 
4854 /*
4855 ** Serialize a changeset (or patchset) based on all changesets (or patchsets)
4856 ** added to the changegroup object passed as the first argument.
4857 **
4858 ** If xOutput is not NULL, then the changeset/patchset is returned to the
4859 ** user via one or more calls to xOutput, as with the other streaming
4860 ** interfaces.
4861 **
4862 ** Or, if xOutput is NULL, then (*ppOut) is populated with a pointer to a
4863 ** buffer containing the output changeset before this function returns. In
4864 ** this case (*pnOut) is set to the size of the output buffer in bytes. It
4865 ** is the responsibility of the caller to free the output buffer using
4866 ** sqlite3_free() when it is no longer required.
4867 **
4868 ** If successful, SQLITE_OK is returned. Or, if an error occurs, an SQLite
4869 ** error code. If an error occurs and xOutput is NULL, (*ppOut) and (*pnOut)
4870 ** are both set to 0 before returning.
4871 */
4872 static int sessionChangegroupOutput(
4873   sqlite3_changegroup *pGrp,
4874   int (*xOutput)(void *pOut, const void *pData, int nData),
4875   void *pOut,
4876   int *pnOut,
4877   void **ppOut
4878 ){
4879   int rc = SQLITE_OK;
4880   SessionBuffer buf = {0, 0, 0};
4881   SessionTable *pTab;
4882   assert( xOutput==0 || (ppOut==0 && pnOut==0) );
4883 
4884   /* Create the serialized output changeset based on the contents of the
4885   ** hash tables attached to the SessionTable objects in list p->pList.
4886   */
4887   for(pTab=pGrp->pList; rc==SQLITE_OK && pTab; pTab=pTab->pNext){
4888     int i;
4889     if( pTab->nEntry==0 ) continue;
4890 
4891     sessionAppendTableHdr(&buf, pGrp->bPatch, pTab, &rc);
4892     for(i=0; i<pTab->nChange; i++){
4893       SessionChange *p;
4894       for(p=pTab->apChange[i]; p; p=p->pNext){
4895         sessionAppendByte(&buf, p->op, &rc);
4896         sessionAppendByte(&buf, p->bIndirect, &rc);
4897         sessionAppendBlob(&buf, p->aRecord, p->nRecord, &rc);
4898         if( rc==SQLITE_OK && xOutput && buf.nBuf>=SESSIONS_STRM_CHUNK_SIZE ){
4899           rc = xOutput(pOut, buf.aBuf, buf.nBuf);
4900           buf.nBuf = 0;
4901         }
4902       }
4903     }
4904   }
4905 
4906   if( rc==SQLITE_OK ){
4907     if( xOutput ){
4908       if( buf.nBuf>0 ) rc = xOutput(pOut, buf.aBuf, buf.nBuf);
4909     }else{
4910       *ppOut = buf.aBuf;
4911       *pnOut = buf.nBuf;
4912       buf.aBuf = 0;
4913     }
4914   }
4915   sqlite3_free(buf.aBuf);
4916 
4917   return rc;
4918 }
4919 
4920 /*
4921 ** Allocate a new, empty, sqlite3_changegroup.
4922 */
4923 int sqlite3changegroup_new(sqlite3_changegroup **pp){
4924   int rc = SQLITE_OK;             /* Return code */
4925   sqlite3_changegroup *p;         /* New object */
4926   p = (sqlite3_changegroup*)sqlite3_malloc(sizeof(sqlite3_changegroup));
4927   if( p==0 ){
4928     rc = SQLITE_NOMEM;
4929   }else{
4930     memset(p, 0, sizeof(sqlite3_changegroup));
4931   }
4932   *pp = p;
4933   return rc;
4934 }
4935 
4936 /*
4937 ** Add the changeset currently stored in buffer pData, size nData bytes,
4938 ** to changeset-group p.
4939 */
4940 int sqlite3changegroup_add(sqlite3_changegroup *pGrp, int nData, void *pData){
4941   sqlite3_changeset_iter *pIter;  /* Iterator opened on pData/nData */
4942   int rc;                         /* Return code */
4943 
4944   rc = sqlite3changeset_start(&pIter, nData, pData);
4945   if( rc==SQLITE_OK ){
4946     rc = sessionChangesetToHash(pIter, pGrp, 0);
4947   }
4948   sqlite3changeset_finalize(pIter);
4949   return rc;
4950 }
4951 
4952 /*
4953 ** Obtain a buffer containing a changeset representing the concatenation
4954 ** of all changesets added to the group so far.
4955 */
4956 int sqlite3changegroup_output(
4957     sqlite3_changegroup *pGrp,
4958     int *pnData,
4959     void **ppData
4960 ){
4961   return sessionChangegroupOutput(pGrp, 0, 0, pnData, ppData);
4962 }
4963 
4964 /*
4965 ** Streaming versions of changegroup_add().
4966 */
4967 int sqlite3changegroup_add_strm(
4968   sqlite3_changegroup *pGrp,
4969   int (*xInput)(void *pIn, void *pData, int *pnData),
4970   void *pIn
4971 ){
4972   sqlite3_changeset_iter *pIter;  /* Iterator opened on pData/nData */
4973   int rc;                         /* Return code */
4974 
4975   rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
4976   if( rc==SQLITE_OK ){
4977     rc = sessionChangesetToHash(pIter, pGrp, 0);
4978   }
4979   sqlite3changeset_finalize(pIter);
4980   return rc;
4981 }
4982 
4983 /*
4984 ** Streaming versions of changegroup_output().
4985 */
4986 int sqlite3changegroup_output_strm(
4987   sqlite3_changegroup *pGrp,
4988   int (*xOutput)(void *pOut, const void *pData, int nData),
4989   void *pOut
4990 ){
4991   return sessionChangegroupOutput(pGrp, xOutput, pOut, 0, 0);
4992 }
4993 
4994 /*
4995 ** Delete a changegroup object.
4996 */
4997 void sqlite3changegroup_delete(sqlite3_changegroup *pGrp){
4998   if( pGrp ){
4999     sessionDeleteTable(pGrp->pList);
5000     sqlite3_free(pGrp);
5001   }
5002 }
5003 
5004 /*
5005 ** Combine two changesets together.
5006 */
5007 int sqlite3changeset_concat(
5008   int nLeft,                      /* Number of bytes in lhs input */
5009   void *pLeft,                    /* Lhs input changeset */
5010   int nRight                      /* Number of bytes in rhs input */,
5011   void *pRight,                   /* Rhs input changeset */
5012   int *pnOut,                     /* OUT: Number of bytes in output changeset */
5013   void **ppOut                    /* OUT: changeset (left <concat> right) */
5014 ){
5015   sqlite3_changegroup *pGrp;
5016   int rc;
5017 
5018   rc = sqlite3changegroup_new(&pGrp);
5019   if( rc==SQLITE_OK ){
5020     rc = sqlite3changegroup_add(pGrp, nLeft, pLeft);
5021   }
5022   if( rc==SQLITE_OK ){
5023     rc = sqlite3changegroup_add(pGrp, nRight, pRight);
5024   }
5025   if( rc==SQLITE_OK ){
5026     rc = sqlite3changegroup_output(pGrp, pnOut, ppOut);
5027   }
5028   sqlite3changegroup_delete(pGrp);
5029 
5030   return rc;
5031 }
5032 
5033 /*
5034 ** Streaming version of sqlite3changeset_concat().
5035 */
5036 int sqlite3changeset_concat_strm(
5037   int (*xInputA)(void *pIn, void *pData, int *pnData),
5038   void *pInA,
5039   int (*xInputB)(void *pIn, void *pData, int *pnData),
5040   void *pInB,
5041   int (*xOutput)(void *pOut, const void *pData, int nData),
5042   void *pOut
5043 ){
5044   sqlite3_changegroup *pGrp;
5045   int rc;
5046 
5047   rc = sqlite3changegroup_new(&pGrp);
5048   if( rc==SQLITE_OK ){
5049     rc = sqlite3changegroup_add_strm(pGrp, xInputA, pInA);
5050   }
5051   if( rc==SQLITE_OK ){
5052     rc = sqlite3changegroup_add_strm(pGrp, xInputB, pInB);
5053   }
5054   if( rc==SQLITE_OK ){
5055     rc = sqlite3changegroup_output_strm(pGrp, xOutput, pOut);
5056   }
5057   sqlite3changegroup_delete(pGrp);
5058 
5059   return rc;
5060 }
5061 
5062 /*
5063 ** Changeset rebaser handle.
5064 */
5065 struct sqlite3_rebaser {
5066   sqlite3_changegroup grp;        /* Hash table */
5067 };
5068 
5069 /*
5070 ** Buffers a1 and a2 must both contain a sessions module record nCol
5071 ** fields in size. This function appends an nCol sessions module
5072 ** record to buffer pBuf that is a copy of a1, except that for
5073 ** each field that is undefined in a1[], swap in the field from a2[].
5074 */
5075 static void sessionAppendRecordMerge(
5076   SessionBuffer *pBuf,            /* Buffer to append to */
5077   int nCol,                       /* Number of columns in each record */
5078   u8 *a1, int n1,                 /* Record 1 */
5079   u8 *a2, int n2,                 /* Record 2 */
5080   int *pRc                        /* IN/OUT: error code */
5081 ){
5082   sessionBufferGrow(pBuf, n1+n2, pRc);
5083   if( *pRc==SQLITE_OK ){
5084     int i;
5085     u8 *pOut = &pBuf->aBuf[pBuf->nBuf];
5086     for(i=0; i<nCol; i++){
5087       int nn1 = sessionSerialLen(a1);
5088       int nn2 = sessionSerialLen(a2);
5089       if( *a1==0 || *a1==0xFF ){
5090         memcpy(pOut, a2, nn2);
5091         pOut += nn2;
5092       }else{
5093         memcpy(pOut, a1, nn1);
5094         pOut += nn1;
5095       }
5096       a1 += nn1;
5097       a2 += nn2;
5098     }
5099 
5100     pBuf->nBuf = pOut-pBuf->aBuf;
5101     assert( pBuf->nBuf<=pBuf->nAlloc );
5102   }
5103 }
5104 
5105 /*
5106 ** This function is called when rebasing a local UPDATE change against one
5107 ** or more remote UPDATE changes. The aRec/nRec buffer contains the current
5108 ** old.* and new.* records for the change. The rebase buffer (a single
5109 ** record) is in aChange/nChange. The rebased change is appended to buffer
5110 ** pBuf.
5111 **
5112 ** Rebasing the UPDATE involves:
5113 **
5114 **   * Removing any changes to fields for which the corresponding field
5115 **     in the rebase buffer is set to "replaced" (type 0xFF). If this
5116 **     means the UPDATE change updates no fields, nothing is appended
5117 **     to the output buffer.
5118 **
5119 **   * For each field modified by the local change for which the
5120 **     corresponding field in the rebase buffer is not "undefined" (0x00)
5121 **     or "replaced" (0xFF), the old.* value is replaced by the value
5122 **     in the rebase buffer.
5123 */
5124 static void sessionAppendPartialUpdate(
5125   SessionBuffer *pBuf,            /* Append record here */
5126   sqlite3_changeset_iter *pIter,  /* Iterator pointed at local change */
5127   u8 *aRec, int nRec,             /* Local change */
5128   u8 *aChange, int nChange,       /* Record to rebase against */
5129   int *pRc                        /* IN/OUT: Return Code */
5130 ){
5131   sessionBufferGrow(pBuf, 2+nRec+nChange, pRc);
5132   if( *pRc==SQLITE_OK ){
5133     int bData = 0;
5134     u8 *pOut = &pBuf->aBuf[pBuf->nBuf];
5135     int i;
5136     u8 *a1 = aRec;
5137     u8 *a2 = aChange;
5138 
5139     *pOut++ = SQLITE_UPDATE;
5140     *pOut++ = pIter->bIndirect;
5141     for(i=0; i<pIter->nCol; i++){
5142       int n1 = sessionSerialLen(a1);
5143       int n2 = sessionSerialLen(a2);
5144       if( pIter->abPK[i] || a2[0]==0 ){
5145         if( !pIter->abPK[i] ) bData = 1;
5146         memcpy(pOut, a1, n1);
5147         pOut += n1;
5148       }else if( a2[0]!=0xFF ){
5149         bData = 1;
5150         memcpy(pOut, a2, n2);
5151         pOut += n2;
5152       }else{
5153         *pOut++ = '\0';
5154       }
5155       a1 += n1;
5156       a2 += n2;
5157     }
5158     if( bData ){
5159       a2 = aChange;
5160       for(i=0; i<pIter->nCol; i++){
5161         int n1 = sessionSerialLen(a1);
5162         int n2 = sessionSerialLen(a2);
5163         if( pIter->abPK[i] || a2[0]!=0xFF ){
5164           memcpy(pOut, a1, n1);
5165           pOut += n1;
5166         }else{
5167           *pOut++ = '\0';
5168         }
5169         a1 += n1;
5170         a2 += n2;
5171       }
5172       pBuf->nBuf = (pOut - pBuf->aBuf);
5173     }
5174   }
5175 }
5176 
5177 /*
5178 ** pIter is configured to iterate through a changeset. This function rebases
5179 ** that changeset according to the current configuration of the rebaser
5180 ** object passed as the first argument. If no error occurs and argument xOutput
5181 ** is not NULL, then the changeset is returned to the caller by invoking
5182 ** xOutput zero or more times and SQLITE_OK returned. Or, if xOutput is NULL,
5183 ** then (*ppOut) is set to point to a buffer containing the rebased changeset
5184 ** before this function returns. In this case (*pnOut) is set to the size of
5185 ** the buffer in bytes.  It is the responsibility of the caller to eventually
5186 ** free the (*ppOut) buffer using sqlite3_free().
5187 **
5188 ** If an error occurs, an SQLite error code is returned. If ppOut and
5189 ** pnOut are not NULL, then the two output parameters are set to 0 before
5190 ** returning.
5191 */
5192 static int sessionRebase(
5193   sqlite3_rebaser *p,             /* Rebaser hash table */
5194   sqlite3_changeset_iter *pIter,  /* Input data */
5195   int (*xOutput)(void *pOut, const void *pData, int nData),
5196   void *pOut,                     /* Context for xOutput callback */
5197   int *pnOut,                     /* OUT: Number of bytes in output changeset */
5198   void **ppOut                    /* OUT: Inverse of pChangeset */
5199 ){
5200   int rc = SQLITE_OK;
5201   u8 *aRec = 0;
5202   int nRec = 0;
5203   int bNew = 0;
5204   SessionTable *pTab = 0;
5205   SessionBuffer sOut = {0,0,0};
5206 
5207   while( SQLITE_ROW==sessionChangesetNext(pIter, &aRec, &nRec, &bNew) ){
5208     SessionChange *pChange = 0;
5209     int bDone = 0;
5210 
5211     if( bNew ){
5212       const char *zTab = pIter->zTab;
5213       for(pTab=p->grp.pList; pTab; pTab=pTab->pNext){
5214         if( 0==sqlite3_stricmp(pTab->zName, zTab) ) break;
5215       }
5216       bNew = 0;
5217 
5218       /* A patchset may not be rebased */
5219       if( pIter->bPatchset ){
5220         rc = SQLITE_ERROR;
5221       }
5222 
5223       /* Append a table header to the output for this new table */
5224       sessionAppendByte(&sOut, pIter->bPatchset ? 'P' : 'T', &rc);
5225       sessionAppendVarint(&sOut, pIter->nCol, &rc);
5226       sessionAppendBlob(&sOut, pIter->abPK, pIter->nCol, &rc);
5227       sessionAppendBlob(&sOut,(u8*)pIter->zTab,(int)strlen(pIter->zTab)+1,&rc);
5228     }
5229 
5230     if( pTab && rc==SQLITE_OK ){
5231       int iHash = sessionChangeHash(pTab, 0, aRec, pTab->nChange);
5232 
5233       for(pChange=pTab->apChange[iHash]; pChange; pChange=pChange->pNext){
5234         if( sessionChangeEqual(pTab, 0, aRec, 0, pChange->aRecord) ){
5235           break;
5236         }
5237       }
5238     }
5239 
5240     if( pChange ){
5241       assert( pChange->op==SQLITE_DELETE || pChange->op==SQLITE_INSERT );
5242       switch( pIter->op ){
5243         case SQLITE_INSERT:
5244           if( pChange->op==SQLITE_INSERT ){
5245             bDone = 1;
5246             if( pChange->bIndirect==0 ){
5247               sessionAppendByte(&sOut, SQLITE_UPDATE, &rc);
5248               sessionAppendByte(&sOut, pIter->bIndirect, &rc);
5249               sessionAppendBlob(&sOut, pChange->aRecord, pChange->nRecord, &rc);
5250               sessionAppendBlob(&sOut, aRec, nRec, &rc);
5251             }
5252           }
5253           break;
5254 
5255         case SQLITE_UPDATE:
5256           bDone = 1;
5257           if( pChange->op==SQLITE_DELETE ){
5258             if( pChange->bIndirect==0 ){
5259               u8 *pCsr = aRec;
5260               sessionSkipRecord(&pCsr, pIter->nCol);
5261               sessionAppendByte(&sOut, SQLITE_INSERT, &rc);
5262               sessionAppendByte(&sOut, pIter->bIndirect, &rc);
5263               sessionAppendRecordMerge(&sOut, pIter->nCol,
5264                   pCsr, nRec-(pCsr-aRec),
5265                   pChange->aRecord, pChange->nRecord, &rc
5266               );
5267             }
5268           }else{
5269             sessionAppendPartialUpdate(&sOut, pIter,
5270                 aRec, nRec, pChange->aRecord, pChange->nRecord, &rc
5271             );
5272           }
5273           break;
5274 
5275         default:
5276           assert( pIter->op==SQLITE_DELETE );
5277           bDone = 1;
5278           if( pChange->op==SQLITE_INSERT ){
5279             sessionAppendByte(&sOut, SQLITE_DELETE, &rc);
5280             sessionAppendByte(&sOut, pIter->bIndirect, &rc);
5281             sessionAppendRecordMerge(&sOut, pIter->nCol,
5282                 pChange->aRecord, pChange->nRecord, aRec, nRec, &rc
5283             );
5284           }
5285           break;
5286       }
5287     }
5288 
5289     if( bDone==0 ){
5290       sessionAppendByte(&sOut, pIter->op, &rc);
5291       sessionAppendByte(&sOut, pIter->bIndirect, &rc);
5292       sessionAppendBlob(&sOut, aRec, nRec, &rc);
5293     }
5294     if( rc==SQLITE_OK && xOutput && sOut.nBuf>SESSIONS_STRM_CHUNK_SIZE ){
5295       rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
5296       sOut.nBuf = 0;
5297     }
5298     if( rc ) break;
5299   }
5300 
5301   if( rc!=SQLITE_OK ){
5302     sqlite3_free(sOut.aBuf);
5303     memset(&sOut, 0, sizeof(sOut));
5304   }
5305 
5306   if( rc==SQLITE_OK ){
5307     if( xOutput ){
5308       if( sOut.nBuf>0 ){
5309         rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
5310       }
5311     }else{
5312       *ppOut = (void*)sOut.aBuf;
5313       *pnOut = sOut.nBuf;
5314       sOut.aBuf = 0;
5315     }
5316   }
5317   sqlite3_free(sOut.aBuf);
5318   return rc;
5319 }
5320 
5321 /*
5322 ** Create a new rebaser object.
5323 */
5324 int sqlite3rebaser_create(sqlite3_rebaser **ppNew){
5325   int rc = SQLITE_OK;
5326   sqlite3_rebaser *pNew;
5327 
5328   pNew = sqlite3_malloc(sizeof(sqlite3_rebaser));
5329   if( pNew==0 ){
5330     rc = SQLITE_NOMEM;
5331   }else{
5332     memset(pNew, 0, sizeof(sqlite3_rebaser));
5333   }
5334   *ppNew = pNew;
5335   return rc;
5336 }
5337 
5338 /*
5339 ** Call this one or more times to configure a rebaser.
5340 */
5341 int sqlite3rebaser_configure(
5342   sqlite3_rebaser *p,
5343   int nRebase, const void *pRebase
5344 ){
5345   sqlite3_changeset_iter *pIter = 0;   /* Iterator opened on pData/nData */
5346   int rc;                              /* Return code */
5347   rc = sqlite3changeset_start(&pIter, nRebase, (void*)pRebase);
5348   if( rc==SQLITE_OK ){
5349     rc = sessionChangesetToHash(pIter, &p->grp, 1);
5350   }
5351   sqlite3changeset_finalize(pIter);
5352   return rc;
5353 }
5354 
5355 /*
5356 ** Rebase a changeset according to current rebaser configuration
5357 */
5358 int sqlite3rebaser_rebase(
5359   sqlite3_rebaser *p,
5360   int nIn, const void *pIn,
5361   int *pnOut, void **ppOut
5362 ){
5363   sqlite3_changeset_iter *pIter = 0;   /* Iterator to skip through input */
5364   int rc = sqlite3changeset_start(&pIter, nIn, (void*)pIn);
5365 
5366   if( rc==SQLITE_OK ){
5367     rc = sessionRebase(p, pIter, 0, 0, pnOut, ppOut);
5368     sqlite3changeset_finalize(pIter);
5369   }
5370 
5371   return rc;
5372 }
5373 
5374 /*
5375 ** Rebase a changeset according to current rebaser configuration
5376 */
5377 int sqlite3rebaser_rebase_strm(
5378   sqlite3_rebaser *p,
5379   int (*xInput)(void *pIn, void *pData, int *pnData),
5380   void *pIn,
5381   int (*xOutput)(void *pOut, const void *pData, int nData),
5382   void *pOut
5383 ){
5384   sqlite3_changeset_iter *pIter = 0;   /* Iterator to skip through input */
5385   int rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
5386 
5387   if( rc==SQLITE_OK ){
5388     rc = sessionRebase(p, pIter, xOutput, pOut, 0, 0);
5389     sqlite3changeset_finalize(pIter);
5390   }
5391 
5392   return rc;
5393 }
5394 
5395 /*
5396 ** Destroy a rebaser object
5397 */
5398 void sqlite3rebaser_delete(sqlite3_rebaser *p){
5399   if( p ){
5400     sessionDeleteTable(p->grp.pList);
5401     sqlite3_free(p);
5402   }
5403 }
5404 
5405 #endif /* SQLITE_ENABLE_SESSION && SQLITE_ENABLE_PREUPDATE_HOOK */
5406