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