Snapshot of upstream SQLite 3.42.0
[sqlcipher.git] / ext / session / sqlite3session.c
blob67940884949c060542d4cca72f116ad2870c53e1
2 #if defined(SQLITE_ENABLE_SESSION) && defined(SQLITE_ENABLE_PREUPDATE_HOOK)
3 #include "sqlite3session.h"
4 #include <assert.h>
5 #include <string.h>
7 #ifndef SQLITE_AMALGAMATION
8 # include "sqliteInt.h"
9 # include "vdbeInt.h"
10 #endif
12 typedef struct SessionTable SessionTable;
13 typedef struct SessionChange SessionChange;
14 typedef struct SessionBuffer SessionBuffer;
15 typedef struct SessionInput SessionInput;
18 ** Minimum chunk size used by streaming versions of functions.
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
28 #define SESSIONS_ROWID "_rowid_"
30 static int sessions_strm_chunk_size = SESSIONS_STRM_CHUNK_SIZE;
32 typedef struct SessionHook SessionHook;
33 struct SessionHook {
34 void *pCtx;
35 int (*xOld)(void*,int,sqlite3_value**);
36 int (*xNew)(void*,int,sqlite3_value**);
37 int (*xCount)(void*);
38 int (*xDepth)(void*);
42 ** Session handle structure.
44 struct sqlite3_session {
45 sqlite3 *db; /* Database handle session is attached to */
46 char *zDb; /* Name of database session is attached to */
47 int bEnableSize; /* True if changeset_size() enabled */
48 int bEnable; /* True if currently recording */
49 int bIndirect; /* True if all changes are indirect */
50 int bAutoAttach; /* True to auto-attach tables */
51 int bImplicitPK; /* True to handle tables with implicit PK */
52 int rc; /* Non-zero if an error has occurred */
53 void *pFilterCtx; /* First argument to pass to xTableFilter */
54 int (*xTableFilter)(void *pCtx, const char *zTab);
55 i64 nMalloc; /* Number of bytes of data allocated */
56 i64 nMaxChangesetSize;
57 sqlite3_value *pZeroBlob; /* Value containing X'' */
58 sqlite3_session *pNext; /* Next session object on same db. */
59 SessionTable *pTable; /* List of attached tables */
60 SessionHook hook; /* APIs to grab new and old data with */
64 ** Instances of this structure are used to build strings or binary records.
66 struct SessionBuffer {
67 u8 *aBuf; /* Pointer to changeset buffer */
68 int nBuf; /* Size of buffer aBuf */
69 int nAlloc; /* Size of allocation containing aBuf */
73 ** An object of this type is used internally as an abstraction for
74 ** input data. Input data may be supplied either as a single large buffer
75 ** (e.g. sqlite3changeset_start()) or using a stream function (e.g.
76 ** sqlite3changeset_start_strm()).
78 struct SessionInput {
79 int bNoDiscard; /* If true, do not discard in InputBuffer() */
80 int iCurrent; /* Offset in aData[] of current change */
81 int iNext; /* Offset in aData[] of next change */
82 u8 *aData; /* Pointer to buffer containing changeset */
83 int nData; /* Number of bytes in aData */
85 SessionBuffer buf; /* Current read buffer */
86 int (*xInput)(void*, void*, int*); /* Input stream call (or NULL) */
87 void *pIn; /* First argument to xInput */
88 int bEof; /* Set to true after xInput finished */
92 ** Structure for changeset iterators.
94 struct sqlite3_changeset_iter {
95 SessionInput in; /* Input buffer or stream */
96 SessionBuffer tblhdr; /* Buffer to hold apValue/zTab/abPK/ */
97 int bPatchset; /* True if this is a patchset */
98 int bInvert; /* True to invert changeset */
99 int bSkipEmpty; /* Skip noop UPDATE changes */
100 int rc; /* Iterator error code */
101 sqlite3_stmt *pConflict; /* Points to conflicting row, if any */
102 char *zTab; /* Current table */
103 int nCol; /* Number of columns in zTab */
104 int op; /* Current operation */
105 int bIndirect; /* True if current change was indirect */
106 u8 *abPK; /* Primary key array */
107 sqlite3_value **apValue; /* old.* and new.* values */
111 ** Each session object maintains a set of the following structures, one
112 ** for each table the session object is monitoring. The structures are
113 ** stored in a linked list starting at sqlite3_session.pTable.
115 ** The keys of the SessionTable.aChange[] hash table are all rows that have
116 ** been modified in any way since the session object was attached to the
117 ** table.
119 ** The data associated with each hash-table entry is a structure containing
120 ** a subset of the initial values that the modified row contained at the
121 ** start of the session. Or no initial values if the row was inserted.
123 struct SessionTable {
124 SessionTable *pNext;
125 char *zName; /* Local name of table */
126 int nCol; /* Number of columns in table zName */
127 int bStat1; /* True if this is sqlite_stat1 */
128 int bRowid; /* True if this table uses rowid for PK */
129 const char **azCol; /* Column names */
130 u8 *abPK; /* Array of primary key flags */
131 int nEntry; /* Total number of entries in hash table */
132 int nChange; /* Size of apChange[] array */
133 SessionChange **apChange; /* Hash table buckets */
137 ** RECORD FORMAT:
139 ** The following record format is similar to (but not compatible with) that
140 ** used in SQLite database files. This format is used as part of the
141 ** change-set binary format, and so must be architecture independent.
143 ** Unlike the SQLite database record format, each field is self-contained -
144 ** there is no separation of header and data. Each field begins with a
145 ** single byte describing its type, as follows:
147 ** 0x00: Undefined value.
148 ** 0x01: Integer value.
149 ** 0x02: Real value.
150 ** 0x03: Text value.
151 ** 0x04: Blob value.
152 ** 0x05: SQL NULL value.
154 ** Note that the above match the definitions of SQLITE_INTEGER, SQLITE_TEXT
155 ** and so on in sqlite3.h. For undefined and NULL values, the field consists
156 ** only of the single type byte. For other types of values, the type byte
157 ** is followed by:
159 ** Text values:
160 ** A varint containing the number of bytes in the value (encoded using
161 ** UTF-8). Followed by a buffer containing the UTF-8 representation
162 ** of the text value. There is no nul terminator.
164 ** Blob values:
165 ** A varint containing the number of bytes in the value, followed by
166 ** a buffer containing the value itself.
168 ** Integer values:
169 ** An 8-byte big-endian integer value.
171 ** Real values:
172 ** An 8-byte big-endian IEEE 754-2008 real value.
174 ** Varint values are encoded in the same way as varints in the SQLite
175 ** record format.
177 ** CHANGESET FORMAT:
179 ** A changeset is a collection of DELETE, UPDATE and INSERT operations on
180 ** one or more tables. Operations on a single table are grouped together,
181 ** but may occur in any order (i.e. deletes, updates and inserts are all
182 ** mixed together).
184 ** Each group of changes begins with a table header:
186 ** 1 byte: Constant 0x54 (capital 'T')
187 ** Varint: Number of columns in the table.
188 ** nCol bytes: 0x01 for PK columns, 0x00 otherwise.
189 ** N bytes: Unqualified table name (encoded using UTF-8). Nul-terminated.
191 ** Followed by one or more changes to the table.
193 ** 1 byte: Either SQLITE_INSERT (0x12), UPDATE (0x17) or DELETE (0x09).
194 ** 1 byte: The "indirect-change" flag.
195 ** old.* record: (delete and update only)
196 ** new.* record: (insert and update only)
198 ** The "old.*" and "new.*" records, if present, are N field records in the
199 ** format described above under "RECORD FORMAT", where N is the number of
200 ** columns in the table. The i'th field of each record is associated with
201 ** the i'th column of the table, counting from left to right in the order
202 ** in which columns were declared in the CREATE TABLE statement.
204 ** The new.* record that is part of each INSERT change contains the values
205 ** that make up the new row. Similarly, the old.* record that is part of each
206 ** DELETE change contains the values that made up the row that was deleted
207 ** from the database. In the changeset format, the records that are part
208 ** of INSERT or DELETE changes never contain any undefined (type byte 0x00)
209 ** fields.
211 ** Within the old.* record associated with an UPDATE change, all fields
212 ** associated with table columns that are not PRIMARY KEY columns and are
213 ** not modified by the UPDATE change are set to "undefined". Other fields
214 ** are set to the values that made up the row before the UPDATE that the
215 ** change records took place. Within the new.* record, fields associated
216 ** with table columns modified by the UPDATE change contain the new
217 ** values. Fields associated with table columns that are not modified
218 ** are set to "undefined".
220 ** PATCHSET FORMAT:
222 ** A patchset is also a collection of changes. It is similar to a changeset,
223 ** but leaves undefined those fields that are not useful if no conflict
224 ** resolution is required when applying the changeset.
226 ** Each group of changes begins with a table header:
228 ** 1 byte: Constant 0x50 (capital 'P')
229 ** Varint: Number of columns in the table.
230 ** nCol bytes: 0x01 for PK columns, 0x00 otherwise.
231 ** N bytes: Unqualified table name (encoded using UTF-8). Nul-terminated.
233 ** Followed by one or more changes to the table.
235 ** 1 byte: Either SQLITE_INSERT (0x12), UPDATE (0x17) or DELETE (0x09).
236 ** 1 byte: The "indirect-change" flag.
237 ** single record: (PK fields for DELETE, PK and modified fields for UPDATE,
238 ** full record for INSERT).
240 ** As in the changeset format, each field of the single record that is part
241 ** of a patchset change is associated with the correspondingly positioned
242 ** table column, counting from left to right within the CREATE TABLE
243 ** statement.
245 ** For a DELETE change, all fields within the record except those associated
246 ** with PRIMARY KEY columns are omitted. The PRIMARY KEY fields contain the
247 ** values identifying the row to delete.
249 ** For an UPDATE change, all fields except those associated with PRIMARY KEY
250 ** columns and columns that are modified by the UPDATE are set to "undefined".
251 ** PRIMARY KEY fields contain the values identifying the table row to update,
252 ** and fields associated with modified columns contain the new column values.
254 ** The records associated with INSERT changes are in the same format as for
255 ** changesets. It is not possible for a record associated with an INSERT
256 ** change to contain a field set to "undefined".
258 ** REBASE BLOB FORMAT:
260 ** A rebase blob may be output by sqlite3changeset_apply_v2() and its
261 ** streaming equivalent for use with the sqlite3_rebaser APIs to rebase
262 ** existing changesets. A rebase blob contains one entry for each conflict
263 ** resolved using either the OMIT or REPLACE strategies within the apply_v2()
264 ** call.
266 ** The format used for a rebase blob is very similar to that used for
267 ** changesets. All entries related to a single table are grouped together.
269 ** Each group of entries begins with a table header in changeset format:
271 ** 1 byte: Constant 0x54 (capital 'T')
272 ** Varint: Number of columns in the table.
273 ** nCol bytes: 0x01 for PK columns, 0x00 otherwise.
274 ** N bytes: Unqualified table name (encoded using UTF-8). Nul-terminated.
276 ** Followed by one or more entries associated with the table.
278 ** 1 byte: Either SQLITE_INSERT (0x12), DELETE (0x09).
279 ** 1 byte: Flag. 0x01 for REPLACE, 0x00 for OMIT.
280 ** record: (in the record format defined above).
282 ** In a rebase blob, the first field is set to SQLITE_INSERT if the change
283 ** that caused the conflict was an INSERT or UPDATE, or to SQLITE_DELETE if
284 ** it was a DELETE. The second field is set to 0x01 if the conflict
285 ** resolution strategy was REPLACE, or 0x00 if it was OMIT.
287 ** If the change that caused the conflict was a DELETE, then the single
288 ** record is a copy of the old.* record from the original changeset. If it
289 ** was an INSERT, then the single record is a copy of the new.* record. If
290 ** the conflicting change was an UPDATE, then the single record is a copy
291 ** of the new.* record with the PK fields filled in based on the original
292 ** old.* record.
296 ** For each row modified during a session, there exists a single instance of
297 ** this structure stored in a SessionTable.aChange[] hash table.
299 struct SessionChange {
300 u8 op; /* One of UPDATE, DELETE, INSERT */
301 u8 bIndirect; /* True if this change is "indirect" */
302 int nMaxSize; /* Max size of eventual changeset record */
303 int nRecord; /* Number of bytes in buffer aRecord[] */
304 u8 *aRecord; /* Buffer containing old.* record */
305 SessionChange *pNext; /* For hash-table collisions */
309 ** Write a varint with value iVal into the buffer at aBuf. Return the
310 ** number of bytes written.
312 static int sessionVarintPut(u8 *aBuf, int iVal){
313 return putVarint32(aBuf, iVal);
317 ** Return the number of bytes required to store value iVal as a varint.
319 static int sessionVarintLen(int iVal){
320 return sqlite3VarintLen(iVal);
324 ** Read a varint value from aBuf[] into *piVal. Return the number of
325 ** bytes read.
327 static int sessionVarintGet(u8 *aBuf, int *piVal){
328 return getVarint32(aBuf, *piVal);
331 /* Load an unaligned and unsigned 32-bit integer */
332 #define SESSION_UINT32(x) (((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
335 ** Read a 64-bit big-endian integer value from buffer aRec[]. Return
336 ** the value read.
338 static sqlite3_int64 sessionGetI64(u8 *aRec){
339 u64 x = SESSION_UINT32(aRec);
340 u32 y = SESSION_UINT32(aRec+4);
341 x = (x<<32) + y;
342 return (sqlite3_int64)x;
346 ** Write a 64-bit big-endian integer value to the buffer aBuf[].
348 static void sessionPutI64(u8 *aBuf, sqlite3_int64 i){
349 aBuf[0] = (i>>56) & 0xFF;
350 aBuf[1] = (i>>48) & 0xFF;
351 aBuf[2] = (i>>40) & 0xFF;
352 aBuf[3] = (i>>32) & 0xFF;
353 aBuf[4] = (i>>24) & 0xFF;
354 aBuf[5] = (i>>16) & 0xFF;
355 aBuf[6] = (i>> 8) & 0xFF;
356 aBuf[7] = (i>> 0) & 0xFF;
360 ** This function is used to serialize the contents of value pValue (see
361 ** comment titled "RECORD FORMAT" above).
363 ** If it is non-NULL, the serialized form of the value is written to
364 ** buffer aBuf. *pnWrite is set to the number of bytes written before
365 ** returning. Or, if aBuf is NULL, the only thing this function does is
366 ** set *pnWrite.
368 ** If no error occurs, SQLITE_OK is returned. Or, if an OOM error occurs
369 ** within a call to sqlite3_value_text() (may fail if the db is utf-16))
370 ** SQLITE_NOMEM is returned.
372 static int sessionSerializeValue(
373 u8 *aBuf, /* If non-NULL, write serialized value here */
374 sqlite3_value *pValue, /* Value to serialize */
375 sqlite3_int64 *pnWrite /* IN/OUT: Increment by bytes written */
377 int nByte; /* Size of serialized value in bytes */
379 if( pValue ){
380 int eType; /* Value type (SQLITE_NULL, TEXT etc.) */
382 eType = sqlite3_value_type(pValue);
383 if( aBuf ) aBuf[0] = eType;
385 switch( eType ){
386 case SQLITE_NULL:
387 nByte = 1;
388 break;
390 case SQLITE_INTEGER:
391 case SQLITE_FLOAT:
392 if( aBuf ){
393 /* TODO: SQLite does something special to deal with mixed-endian
394 ** floating point values (e.g. ARM7). This code probably should
395 ** too. */
396 u64 i;
397 if( eType==SQLITE_INTEGER ){
398 i = (u64)sqlite3_value_int64(pValue);
399 }else{
400 double r;
401 assert( sizeof(double)==8 && sizeof(u64)==8 );
402 r = sqlite3_value_double(pValue);
403 memcpy(&i, &r, 8);
405 sessionPutI64(&aBuf[1], i);
407 nByte = 9;
408 break;
410 default: {
411 u8 *z;
412 int n;
413 int nVarint;
415 assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
416 if( eType==SQLITE_TEXT ){
417 z = (u8 *)sqlite3_value_text(pValue);
418 }else{
419 z = (u8 *)sqlite3_value_blob(pValue);
421 n = sqlite3_value_bytes(pValue);
422 if( z==0 && (eType!=SQLITE_BLOB || n>0) ) return SQLITE_NOMEM;
423 nVarint = sessionVarintLen(n);
425 if( aBuf ){
426 sessionVarintPut(&aBuf[1], n);
427 if( n>0 ) memcpy(&aBuf[nVarint + 1], z, n);
430 nByte = 1 + nVarint + n;
431 break;
434 }else{
435 nByte = 1;
436 if( aBuf ) aBuf[0] = '\0';
439 if( pnWrite ) *pnWrite += nByte;
440 return SQLITE_OK;
444 ** Allocate and return a pointer to a buffer nByte bytes in size. If
445 ** pSession is not NULL, increase the sqlite3_session.nMalloc variable
446 ** by the number of bytes allocated.
448 static void *sessionMalloc64(sqlite3_session *pSession, i64 nByte){
449 void *pRet = sqlite3_malloc64(nByte);
450 if( pSession ) pSession->nMalloc += sqlite3_msize(pRet);
451 return pRet;
455 ** Free buffer pFree, which must have been allocated by an earlier
456 ** call to sessionMalloc64(). If pSession is not NULL, decrease the
457 ** sqlite3_session.nMalloc counter by the number of bytes freed.
459 static void sessionFree(sqlite3_session *pSession, void *pFree){
460 if( pSession ) pSession->nMalloc -= sqlite3_msize(pFree);
461 sqlite3_free(pFree);
465 ** This macro is used to calculate hash key values for data structures. In
466 ** order to use this macro, the entire data structure must be represented
467 ** as a series of unsigned integers. In order to calculate a hash-key value
468 ** for a data structure represented as three such integers, the macro may
469 ** then be used as follows:
471 ** int hash_key_value;
472 ** hash_key_value = HASH_APPEND(0, <value 1>);
473 ** hash_key_value = HASH_APPEND(hash_key_value, <value 2>);
474 ** hash_key_value = HASH_APPEND(hash_key_value, <value 3>);
476 ** In practice, the data structures this macro is used for are the primary
477 ** key values of modified rows.
479 #define HASH_APPEND(hash, add) ((hash) << 3) ^ (hash) ^ (unsigned int)(add)
482 ** Append the hash of the 64-bit integer passed as the second argument to the
483 ** hash-key value passed as the first. Return the new hash-key value.
485 static unsigned int sessionHashAppendI64(unsigned int h, i64 i){
486 h = HASH_APPEND(h, i & 0xFFFFFFFF);
487 return HASH_APPEND(h, (i>>32)&0xFFFFFFFF);
491 ** Append the hash of the blob passed via the second and third arguments to
492 ** the hash-key value passed as the first. Return the new hash-key value.
494 static unsigned int sessionHashAppendBlob(unsigned int h, int n, const u8 *z){
495 int i;
496 for(i=0; i<n; i++) h = HASH_APPEND(h, z[i]);
497 return h;
501 ** Append the hash of the data type passed as the second argument to the
502 ** hash-key value passed as the first. Return the new hash-key value.
504 static unsigned int sessionHashAppendType(unsigned int h, int eType){
505 return HASH_APPEND(h, eType);
509 ** This function may only be called from within a pre-update callback.
510 ** It calculates a hash based on the primary key values of the old.* or
511 ** new.* row currently available and, assuming no error occurs, writes it to
512 ** *piHash before returning. If the primary key contains one or more NULL
513 ** values, *pbNullPK is set to true before returning.
515 ** If an error occurs, an SQLite error code is returned and the final values
516 ** of *piHash asn *pbNullPK are undefined. Otherwise, SQLITE_OK is returned
517 ** and the output variables are set as described above.
519 static int sessionPreupdateHash(
520 sqlite3_session *pSession, /* Session object that owns pTab */
521 i64 iRowid,
522 SessionTable *pTab, /* Session table handle */
523 int bNew, /* True to hash the new.* PK */
524 int *piHash, /* OUT: Hash value */
525 int *pbNullPK /* OUT: True if there are NULL values in PK */
527 unsigned int h = 0; /* Hash value to return */
528 int i; /* Used to iterate through columns */
530 if( pTab->bRowid ){
531 assert( pTab->nCol-1==pSession->hook.xCount(pSession->hook.pCtx) );
532 h = sessionHashAppendI64(h, iRowid);
533 }else{
534 assert( *pbNullPK==0 );
535 assert( pTab->nCol==pSession->hook.xCount(pSession->hook.pCtx) );
536 for(i=0; i<pTab->nCol; i++){
537 if( pTab->abPK[i] ){
538 int rc;
539 int eType;
540 sqlite3_value *pVal;
542 if( bNew ){
543 rc = pSession->hook.xNew(pSession->hook.pCtx, i, &pVal);
544 }else{
545 rc = pSession->hook.xOld(pSession->hook.pCtx, i, &pVal);
547 if( rc!=SQLITE_OK ) return rc;
549 eType = sqlite3_value_type(pVal);
550 h = sessionHashAppendType(h, eType);
551 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
552 i64 iVal;
553 if( eType==SQLITE_INTEGER ){
554 iVal = sqlite3_value_int64(pVal);
555 }else{
556 double rVal = sqlite3_value_double(pVal);
557 assert( sizeof(iVal)==8 && sizeof(rVal)==8 );
558 memcpy(&iVal, &rVal, 8);
560 h = sessionHashAppendI64(h, iVal);
561 }else if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
562 const u8 *z;
563 int n;
564 if( eType==SQLITE_TEXT ){
565 z = (const u8 *)sqlite3_value_text(pVal);
566 }else{
567 z = (const u8 *)sqlite3_value_blob(pVal);
569 n = sqlite3_value_bytes(pVal);
570 if( !z && (eType!=SQLITE_BLOB || n>0) ) return SQLITE_NOMEM;
571 h = sessionHashAppendBlob(h, n, z);
572 }else{
573 assert( eType==SQLITE_NULL );
574 assert( pTab->bStat1==0 || i!=1 );
575 *pbNullPK = 1;
581 *piHash = (h % pTab->nChange);
582 return SQLITE_OK;
586 ** The buffer that the argument points to contains a serialized SQL value.
587 ** Return the number of bytes of space occupied by the value (including
588 ** the type byte).
590 static int sessionSerialLen(u8 *a){
591 int e = *a;
592 int n;
593 if( e==0 || e==0xFF ) return 1;
594 if( e==SQLITE_NULL ) return 1;
595 if( e==SQLITE_INTEGER || e==SQLITE_FLOAT ) return 9;
596 return sessionVarintGet(&a[1], &n) + 1 + n;
600 ** Based on the primary key values stored in change aRecord, calculate a
601 ** hash key. Assume the has table has nBucket buckets. The hash keys
602 ** calculated by this function are compatible with those calculated by
603 ** sessionPreupdateHash().
605 ** The bPkOnly argument is non-zero if the record at aRecord[] is from
606 ** a patchset DELETE. In this case the non-PK fields are omitted entirely.
608 static unsigned int sessionChangeHash(
609 SessionTable *pTab, /* Table handle */
610 int bPkOnly, /* Record consists of PK fields only */
611 u8 *aRecord, /* Change record */
612 int nBucket /* Assume this many buckets in hash table */
614 unsigned int h = 0; /* Value to return */
615 int i; /* Used to iterate through columns */
616 u8 *a = aRecord; /* Used to iterate through change record */
618 for(i=0; i<pTab->nCol; i++){
619 int eType = *a;
620 int isPK = pTab->abPK[i];
621 if( bPkOnly && isPK==0 ) continue;
623 /* It is not possible for eType to be SQLITE_NULL here. The session
624 ** module does not record changes for rows with NULL values stored in
625 ** primary key columns. */
626 assert( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT
627 || eType==SQLITE_TEXT || eType==SQLITE_BLOB
628 || eType==SQLITE_NULL || eType==0
630 assert( !isPK || (eType!=0 && eType!=SQLITE_NULL) );
632 if( isPK ){
633 a++;
634 h = sessionHashAppendType(h, eType);
635 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
636 h = sessionHashAppendI64(h, sessionGetI64(a));
637 a += 8;
638 }else{
639 int n;
640 a += sessionVarintGet(a, &n);
641 h = sessionHashAppendBlob(h, n, a);
642 a += n;
644 }else{
645 a += sessionSerialLen(a);
648 return (h % nBucket);
652 ** Arguments aLeft and aRight are pointers to change records for table pTab.
653 ** This function returns true if the two records apply to the same row (i.e.
654 ** have the same values stored in the primary key columns), or false
655 ** otherwise.
657 static int sessionChangeEqual(
658 SessionTable *pTab, /* Table used for PK definition */
659 int bLeftPkOnly, /* True if aLeft[] contains PK fields only */
660 u8 *aLeft, /* Change record */
661 int bRightPkOnly, /* True if aRight[] contains PK fields only */
662 u8 *aRight /* Change record */
664 u8 *a1 = aLeft; /* Cursor to iterate through aLeft */
665 u8 *a2 = aRight; /* Cursor to iterate through aRight */
666 int iCol; /* Used to iterate through table columns */
668 for(iCol=0; iCol<pTab->nCol; iCol++){
669 if( pTab->abPK[iCol] ){
670 int n1 = sessionSerialLen(a1);
671 int n2 = sessionSerialLen(a2);
673 if( n1!=n2 || memcmp(a1, a2, n1) ){
674 return 0;
676 a1 += n1;
677 a2 += n2;
678 }else{
679 if( bLeftPkOnly==0 ) a1 += sessionSerialLen(a1);
680 if( bRightPkOnly==0 ) a2 += sessionSerialLen(a2);
684 return 1;
688 ** Arguments aLeft and aRight both point to buffers containing change
689 ** records with nCol columns. This function "merges" the two records into
690 ** a single records which is written to the buffer at *paOut. *paOut is
691 ** then set to point to one byte after the last byte written before
692 ** returning.
694 ** The merging of records is done as follows: For each column, if the
695 ** aRight record contains a value for the column, copy the value from
696 ** their. Otherwise, if aLeft contains a value, copy it. If neither
697 ** record contains a value for a given column, then neither does the
698 ** output record.
700 static void sessionMergeRecord(
701 u8 **paOut,
702 int nCol,
703 u8 *aLeft,
704 u8 *aRight
706 u8 *a1 = aLeft; /* Cursor used to iterate through aLeft */
707 u8 *a2 = aRight; /* Cursor used to iterate through aRight */
708 u8 *aOut = *paOut; /* Output cursor */
709 int iCol; /* Used to iterate from 0 to nCol */
711 for(iCol=0; iCol<nCol; iCol++){
712 int n1 = sessionSerialLen(a1);
713 int n2 = sessionSerialLen(a2);
714 if( *a2 ){
715 memcpy(aOut, a2, n2);
716 aOut += n2;
717 }else{
718 memcpy(aOut, a1, n1);
719 aOut += n1;
721 a1 += n1;
722 a2 += n2;
725 *paOut = aOut;
729 ** This is a helper function used by sessionMergeUpdate().
731 ** When this function is called, both *paOne and *paTwo point to a value
732 ** within a change record. Before it returns, both have been advanced so
733 ** as to point to the next value in the record.
735 ** If, when this function is called, *paTwo points to a valid value (i.e.
736 ** *paTwo[0] is not 0x00 - the "no value" placeholder), a copy of the *paTwo
737 ** pointer is returned and *pnVal is set to the number of bytes in the
738 ** serialized value. Otherwise, a copy of *paOne is returned and *pnVal
739 ** set to the number of bytes in the value at *paOne. If *paOne points
740 ** to the "no value" placeholder, *pnVal is set to 1. In other words:
742 ** if( *paTwo is valid ) return *paTwo;
743 ** return *paOne;
746 static u8 *sessionMergeValue(
747 u8 **paOne, /* IN/OUT: Left-hand buffer pointer */
748 u8 **paTwo, /* IN/OUT: Right-hand buffer pointer */
749 int *pnVal /* OUT: Bytes in returned value */
751 u8 *a1 = *paOne;
752 u8 *a2 = *paTwo;
753 u8 *pRet = 0;
754 int n1;
756 assert( a1 );
757 if( a2 ){
758 int n2 = sessionSerialLen(a2);
759 if( *a2 ){
760 *pnVal = n2;
761 pRet = a2;
763 *paTwo = &a2[n2];
766 n1 = sessionSerialLen(a1);
767 if( pRet==0 ){
768 *pnVal = n1;
769 pRet = a1;
771 *paOne = &a1[n1];
773 return pRet;
777 ** This function is used by changeset_concat() to merge two UPDATE changes
778 ** on the same row.
780 static int sessionMergeUpdate(
781 u8 **paOut, /* IN/OUT: Pointer to output buffer */
782 SessionTable *pTab, /* Table change pertains to */
783 int bPatchset, /* True if records are patchset records */
784 u8 *aOldRecord1, /* old.* record for first change */
785 u8 *aOldRecord2, /* old.* record for second change */
786 u8 *aNewRecord1, /* new.* record for first change */
787 u8 *aNewRecord2 /* new.* record for second change */
789 u8 *aOld1 = aOldRecord1;
790 u8 *aOld2 = aOldRecord2;
791 u8 *aNew1 = aNewRecord1;
792 u8 *aNew2 = aNewRecord2;
794 u8 *aOut = *paOut;
795 int i;
797 if( bPatchset==0 ){
798 int bRequired = 0;
800 assert( aOldRecord1 && aNewRecord1 );
802 /* Write the old.* vector first. */
803 for(i=0; i<pTab->nCol; i++){
804 int nOld;
805 u8 *aOld;
806 int nNew;
807 u8 *aNew;
809 aOld = sessionMergeValue(&aOld1, &aOld2, &nOld);
810 aNew = sessionMergeValue(&aNew1, &aNew2, &nNew);
811 if( pTab->abPK[i] || nOld!=nNew || memcmp(aOld, aNew, nNew) ){
812 if( pTab->abPK[i]==0 ) bRequired = 1;
813 memcpy(aOut, aOld, nOld);
814 aOut += nOld;
815 }else{
816 *(aOut++) = '\0';
820 if( !bRequired ) return 0;
823 /* Write the new.* vector */
824 aOld1 = aOldRecord1;
825 aOld2 = aOldRecord2;
826 aNew1 = aNewRecord1;
827 aNew2 = aNewRecord2;
828 for(i=0; i<pTab->nCol; i++){
829 int nOld;
830 u8 *aOld;
831 int nNew;
832 u8 *aNew;
834 aOld = sessionMergeValue(&aOld1, &aOld2, &nOld);
835 aNew = sessionMergeValue(&aNew1, &aNew2, &nNew);
836 if( bPatchset==0
837 && (pTab->abPK[i] || (nOld==nNew && 0==memcmp(aOld, aNew, nNew)))
839 *(aOut++) = '\0';
840 }else{
841 memcpy(aOut, aNew, nNew);
842 aOut += nNew;
846 *paOut = aOut;
847 return 1;
851 ** This function is only called from within a pre-update-hook callback.
852 ** It determines if the current pre-update-hook change affects the same row
853 ** as the change stored in argument pChange. If so, it returns true. Otherwise
854 ** if the pre-update-hook does not affect the same row as pChange, it returns
855 ** false.
857 static int sessionPreupdateEqual(
858 sqlite3_session *pSession, /* Session object that owns SessionTable */
859 i64 iRowid, /* Rowid value if pTab->bRowid */
860 SessionTable *pTab, /* Table associated with change */
861 SessionChange *pChange, /* Change to compare to */
862 int op /* Current pre-update operation */
864 int iCol; /* Used to iterate through columns */
865 u8 *a = pChange->aRecord; /* Cursor used to scan change record */
867 if( pTab->bRowid ){
868 if( a[0]!=SQLITE_INTEGER ) return 0;
869 return sessionGetI64(&a[1])==iRowid;
872 assert( op==SQLITE_INSERT || op==SQLITE_UPDATE || op==SQLITE_DELETE );
873 for(iCol=0; iCol<pTab->nCol; iCol++){
874 if( !pTab->abPK[iCol] ){
875 a += sessionSerialLen(a);
876 }else{
877 sqlite3_value *pVal; /* Value returned by preupdate_new/old */
878 int rc; /* Error code from preupdate_new/old */
879 int eType = *a++; /* Type of value from change record */
881 /* The following calls to preupdate_new() and preupdate_old() can not
882 ** fail. This is because they cache their return values, and by the
883 ** time control flows to here they have already been called once from
884 ** within sessionPreupdateHash(). The first two asserts below verify
885 ** this (that the method has already been called). */
886 if( op==SQLITE_INSERT ){
887 /* assert( db->pPreUpdate->pNewUnpacked || db->pPreUpdate->aNew ); */
888 rc = pSession->hook.xNew(pSession->hook.pCtx, iCol, &pVal);
889 }else{
890 /* assert( db->pPreUpdate->pUnpacked ); */
891 rc = pSession->hook.xOld(pSession->hook.pCtx, iCol, &pVal);
893 assert( rc==SQLITE_OK );
894 if( sqlite3_value_type(pVal)!=eType ) return 0;
896 /* A SessionChange object never has a NULL value in a PK column */
897 assert( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT
898 || eType==SQLITE_BLOB || eType==SQLITE_TEXT
901 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
902 i64 iVal = sessionGetI64(a);
903 a += 8;
904 if( eType==SQLITE_INTEGER ){
905 if( sqlite3_value_int64(pVal)!=iVal ) return 0;
906 }else{
907 double rVal;
908 assert( sizeof(iVal)==8 && sizeof(rVal)==8 );
909 memcpy(&rVal, &iVal, 8);
910 if( sqlite3_value_double(pVal)!=rVal ) return 0;
912 }else{
913 int n;
914 const u8 *z;
915 a += sessionVarintGet(a, &n);
916 if( sqlite3_value_bytes(pVal)!=n ) return 0;
917 if( eType==SQLITE_TEXT ){
918 z = sqlite3_value_text(pVal);
919 }else{
920 z = sqlite3_value_blob(pVal);
922 if( n>0 && memcmp(a, z, n) ) return 0;
923 a += n;
928 return 1;
932 ** If required, grow the hash table used to store changes on table pTab
933 ** (part of the session pSession). If a fatal OOM error occurs, set the
934 ** session object to failed and return SQLITE_ERROR. Otherwise, return
935 ** SQLITE_OK.
937 ** It is possible that a non-fatal OOM error occurs in this function. In
938 ** that case the hash-table does not grow, but SQLITE_OK is returned anyway.
939 ** Growing the hash table in this case is a performance optimization only,
940 ** it is not required for correct operation.
942 static int sessionGrowHash(
943 sqlite3_session *pSession, /* For memory accounting. May be NULL */
944 int bPatchset,
945 SessionTable *pTab
947 if( pTab->nChange==0 || pTab->nEntry>=(pTab->nChange/2) ){
948 int i;
949 SessionChange **apNew;
950 sqlite3_int64 nNew = 2*(sqlite3_int64)(pTab->nChange ? pTab->nChange : 128);
952 apNew = (SessionChange**)sessionMalloc64(
953 pSession, sizeof(SessionChange*) * nNew
955 if( apNew==0 ){
956 if( pTab->nChange==0 ){
957 return SQLITE_ERROR;
959 return SQLITE_OK;
961 memset(apNew, 0, sizeof(SessionChange *) * nNew);
963 for(i=0; i<pTab->nChange; i++){
964 SessionChange *p;
965 SessionChange *pNext;
966 for(p=pTab->apChange[i]; p; p=pNext){
967 int bPkOnly = (p->op==SQLITE_DELETE && bPatchset);
968 int iHash = sessionChangeHash(pTab, bPkOnly, p->aRecord, nNew);
969 pNext = p->pNext;
970 p->pNext = apNew[iHash];
971 apNew[iHash] = p;
975 sessionFree(pSession, pTab->apChange);
976 pTab->nChange = nNew;
977 pTab->apChange = apNew;
980 return SQLITE_OK;
984 ** This function queries the database for the names of the columns of table
985 ** zThis, in schema zDb.
987 ** Otherwise, if they are not NULL, variable *pnCol is set to the number
988 ** of columns in the database table and variable *pzTab is set to point to a
989 ** nul-terminated copy of the table name. *pazCol (if not NULL) is set to
990 ** point to an array of pointers to column names. And *pabPK (again, if not
991 ** NULL) is set to point to an array of booleans - true if the corresponding
992 ** column is part of the primary key.
994 ** For example, if the table is declared as:
996 ** CREATE TABLE tbl1(w, x, y, z, PRIMARY KEY(w, z));
998 ** Then the four output variables are populated as follows:
1000 ** *pnCol = 4
1001 ** *pzTab = "tbl1"
1002 ** *pazCol = {"w", "x", "y", "z"}
1003 ** *pabPK = {1, 0, 0, 1}
1005 ** All returned buffers are part of the same single allocation, which must
1006 ** be freed using sqlite3_free() by the caller
1008 static int sessionTableInfo(
1009 sqlite3_session *pSession, /* For memory accounting. May be NULL */
1010 sqlite3 *db, /* Database connection */
1011 const char *zDb, /* Name of attached database (e.g. "main") */
1012 const char *zThis, /* Table name */
1013 int *pnCol, /* OUT: number of columns */
1014 const char **pzTab, /* OUT: Copy of zThis */
1015 const char ***pazCol, /* OUT: Array of column names for table */
1016 u8 **pabPK, /* OUT: Array of booleans - true for PK col */
1017 int *pbRowid /* OUT: True if only PK is a rowid */
1019 char *zPragma;
1020 sqlite3_stmt *pStmt;
1021 int rc;
1022 sqlite3_int64 nByte;
1023 int nDbCol = 0;
1024 int nThis;
1025 int i;
1026 u8 *pAlloc = 0;
1027 char **azCol = 0;
1028 u8 *abPK = 0;
1029 int bRowid = 0; /* Set to true to use rowid as PK */
1031 assert( pazCol && pabPK );
1033 nThis = sqlite3Strlen30(zThis);
1034 if( nThis==12 && 0==sqlite3_stricmp("sqlite_stat1", zThis) ){
1035 rc = sqlite3_table_column_metadata(db, zDb, zThis, 0, 0, 0, 0, 0, 0);
1036 if( rc==SQLITE_OK ){
1037 /* For sqlite_stat1, pretend that (tbl,idx) is the PRIMARY KEY. */
1038 zPragma = sqlite3_mprintf(
1039 "SELECT 0, 'tbl', '', 0, '', 1 UNION ALL "
1040 "SELECT 1, 'idx', '', 0, '', 2 UNION ALL "
1041 "SELECT 2, 'stat', '', 0, '', 0"
1043 }else if( rc==SQLITE_ERROR ){
1044 zPragma = sqlite3_mprintf("");
1045 }else{
1046 *pazCol = 0;
1047 *pabPK = 0;
1048 *pnCol = 0;
1049 if( pzTab ) *pzTab = 0;
1050 return rc;
1052 }else{
1053 zPragma = sqlite3_mprintf("PRAGMA '%q'.table_info('%q')", zDb, zThis);
1055 if( !zPragma ){
1056 *pazCol = 0;
1057 *pabPK = 0;
1058 *pnCol = 0;
1059 if( pzTab ) *pzTab = 0;
1060 return SQLITE_NOMEM;
1063 rc = sqlite3_prepare_v2(db, zPragma, -1, &pStmt, 0);
1064 sqlite3_free(zPragma);
1065 if( rc!=SQLITE_OK ){
1066 *pazCol = 0;
1067 *pabPK = 0;
1068 *pnCol = 0;
1069 if( pzTab ) *pzTab = 0;
1070 return rc;
1073 nByte = nThis + 1;
1074 bRowid = (pbRowid!=0);
1075 while( SQLITE_ROW==sqlite3_step(pStmt) ){
1076 nByte += sqlite3_column_bytes(pStmt, 1);
1077 nDbCol++;
1078 if( sqlite3_column_int(pStmt, 5) ) bRowid = 0;
1080 if( nDbCol==0 ) bRowid = 0;
1081 nDbCol += bRowid;
1082 nByte += strlen(SESSIONS_ROWID);
1083 rc = sqlite3_reset(pStmt);
1085 if( rc==SQLITE_OK ){
1086 nByte += nDbCol * (sizeof(const char *) + sizeof(u8) + 1);
1087 pAlloc = sessionMalloc64(pSession, nByte);
1088 if( pAlloc==0 ){
1089 rc = SQLITE_NOMEM;
1092 if( rc==SQLITE_OK ){
1093 azCol = (char **)pAlloc;
1094 pAlloc = (u8 *)&azCol[nDbCol];
1095 abPK = (u8 *)pAlloc;
1096 pAlloc = &abPK[nDbCol];
1097 if( pzTab ){
1098 memcpy(pAlloc, zThis, nThis+1);
1099 *pzTab = (char *)pAlloc;
1100 pAlloc += nThis+1;
1103 i = 0;
1104 if( bRowid ){
1105 size_t nName = strlen(SESSIONS_ROWID);
1106 memcpy(pAlloc, SESSIONS_ROWID, nName+1);
1107 azCol[i] = (char*)pAlloc;
1108 pAlloc += nName+1;
1109 abPK[i] = 1;
1110 i++;
1112 while( SQLITE_ROW==sqlite3_step(pStmt) ){
1113 int nName = sqlite3_column_bytes(pStmt, 1);
1114 const unsigned char *zName = sqlite3_column_text(pStmt, 1);
1115 if( zName==0 ) break;
1116 memcpy(pAlloc, zName, nName+1);
1117 azCol[i] = (char *)pAlloc;
1118 pAlloc += nName+1;
1119 abPK[i] = sqlite3_column_int(pStmt, 5);
1120 i++;
1122 rc = sqlite3_reset(pStmt);
1125 /* If successful, populate the output variables. Otherwise, zero them and
1126 ** free any allocation made. An error code will be returned in this case.
1128 if( rc==SQLITE_OK ){
1129 *pazCol = (const char **)azCol;
1130 *pabPK = abPK;
1131 *pnCol = nDbCol;
1132 }else{
1133 *pazCol = 0;
1134 *pabPK = 0;
1135 *pnCol = 0;
1136 if( pzTab ) *pzTab = 0;
1137 sessionFree(pSession, azCol);
1139 if( pbRowid ) *pbRowid = bRowid;
1140 sqlite3_finalize(pStmt);
1141 return rc;
1145 ** This function is only called from within a pre-update handler for a
1146 ** write to table pTab, part of session pSession. If this is the first
1147 ** write to this table, initalize the SessionTable.nCol, azCol[] and
1148 ** abPK[] arrays accordingly.
1150 ** If an error occurs, an error code is stored in sqlite3_session.rc and
1151 ** non-zero returned. Or, if no error occurs but the table has no primary
1152 ** key, sqlite3_session.rc is left set to SQLITE_OK and non-zero returned to
1153 ** indicate that updates on this table should be ignored. SessionTable.abPK
1154 ** is set to NULL in this case.
1156 static int sessionInitTable(sqlite3_session *pSession, SessionTable *pTab){
1157 if( pTab->nCol==0 ){
1158 u8 *abPK;
1159 assert( pTab->azCol==0 || pTab->abPK==0 );
1160 pSession->rc = sessionTableInfo(pSession, pSession->db, pSession->zDb,
1161 pTab->zName, &pTab->nCol, 0, &pTab->azCol, &abPK,
1162 (pSession->bImplicitPK ? &pTab->bRowid : 0)
1164 if( pSession->rc==SQLITE_OK ){
1165 int i;
1166 for(i=0; i<pTab->nCol; i++){
1167 if( abPK[i] ){
1168 pTab->abPK = abPK;
1169 break;
1172 if( 0==sqlite3_stricmp("sqlite_stat1", pTab->zName) ){
1173 pTab->bStat1 = 1;
1176 if( pSession->bEnableSize ){
1177 pSession->nMaxChangesetSize += (
1178 1 + sessionVarintLen(pTab->nCol) + pTab->nCol + strlen(pTab->zName)+1
1183 return (pSession->rc || pTab->abPK==0);
1187 ** Versions of the four methods in object SessionHook for use with the
1188 ** sqlite_stat1 table. The purpose of this is to substitute a zero-length
1189 ** blob each time a NULL value is read from the "idx" column of the
1190 ** sqlite_stat1 table.
1192 typedef struct SessionStat1Ctx SessionStat1Ctx;
1193 struct SessionStat1Ctx {
1194 SessionHook hook;
1195 sqlite3_session *pSession;
1197 static int sessionStat1Old(void *pCtx, int iCol, sqlite3_value **ppVal){
1198 SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
1199 sqlite3_value *pVal = 0;
1200 int rc = p->hook.xOld(p->hook.pCtx, iCol, &pVal);
1201 if( rc==SQLITE_OK && iCol==1 && sqlite3_value_type(pVal)==SQLITE_NULL ){
1202 pVal = p->pSession->pZeroBlob;
1204 *ppVal = pVal;
1205 return rc;
1207 static int sessionStat1New(void *pCtx, int iCol, sqlite3_value **ppVal){
1208 SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
1209 sqlite3_value *pVal = 0;
1210 int rc = p->hook.xNew(p->hook.pCtx, iCol, &pVal);
1211 if( rc==SQLITE_OK && iCol==1 && sqlite3_value_type(pVal)==SQLITE_NULL ){
1212 pVal = p->pSession->pZeroBlob;
1214 *ppVal = pVal;
1215 return rc;
1217 static int sessionStat1Count(void *pCtx){
1218 SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
1219 return p->hook.xCount(p->hook.pCtx);
1221 static int sessionStat1Depth(void *pCtx){
1222 SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
1223 return p->hook.xDepth(p->hook.pCtx);
1226 static int sessionUpdateMaxSize(
1227 int op,
1228 sqlite3_session *pSession, /* Session object pTab is attached to */
1229 SessionTable *pTab, /* Table that change applies to */
1230 SessionChange *pC /* Update pC->nMaxSize */
1232 i64 nNew = 2;
1233 if( pC->op==SQLITE_INSERT ){
1234 if( pTab->bRowid ) nNew += 9;
1235 if( op!=SQLITE_DELETE ){
1236 int ii;
1237 for(ii=0; ii<pTab->nCol; ii++){
1238 sqlite3_value *p = 0;
1239 pSession->hook.xNew(pSession->hook.pCtx, ii, &p);
1240 sessionSerializeValue(0, p, &nNew);
1243 }else if( op==SQLITE_DELETE ){
1244 nNew += pC->nRecord;
1245 if( sqlite3_preupdate_blobwrite(pSession->db)>=0 ){
1246 nNew += pC->nRecord;
1248 }else{
1249 int ii;
1250 u8 *pCsr = pC->aRecord;
1251 if( pTab->bRowid ){
1252 nNew += 9 + 1;
1253 pCsr += 9;
1255 for(ii=pTab->bRowid; ii<pTab->nCol; ii++){
1256 int bChanged = 1;
1257 int nOld = 0;
1258 int eType;
1259 sqlite3_value *p = 0;
1260 pSession->hook.xNew(pSession->hook.pCtx, ii-pTab->bRowid, &p);
1261 if( p==0 ){
1262 return SQLITE_NOMEM;
1265 eType = *pCsr++;
1266 switch( eType ){
1267 case SQLITE_NULL:
1268 bChanged = sqlite3_value_type(p)!=SQLITE_NULL;
1269 break;
1271 case SQLITE_FLOAT:
1272 case SQLITE_INTEGER: {
1273 if( eType==sqlite3_value_type(p) ){
1274 sqlite3_int64 iVal = sessionGetI64(pCsr);
1275 if( eType==SQLITE_INTEGER ){
1276 bChanged = (iVal!=sqlite3_value_int64(p));
1277 }else{
1278 double dVal;
1279 memcpy(&dVal, &iVal, 8);
1280 bChanged = (dVal!=sqlite3_value_double(p));
1283 nOld = 8;
1284 pCsr += 8;
1285 break;
1288 default: {
1289 int nByte;
1290 nOld = sessionVarintGet(pCsr, &nByte);
1291 pCsr += nOld;
1292 nOld += nByte;
1293 assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
1294 if( eType==sqlite3_value_type(p)
1295 && nByte==sqlite3_value_bytes(p)
1296 && (nByte==0 || 0==memcmp(pCsr, sqlite3_value_blob(p), nByte))
1298 bChanged = 0;
1300 pCsr += nByte;
1301 break;
1305 if( bChanged && pTab->abPK[ii] ){
1306 nNew = pC->nRecord + 2;
1307 break;
1310 if( bChanged ){
1311 nNew += 1 + nOld;
1312 sessionSerializeValue(0, p, &nNew);
1313 }else if( pTab->abPK[ii] ){
1314 nNew += 2 + nOld;
1315 }else{
1316 nNew += 2;
1321 if( nNew>pC->nMaxSize ){
1322 int nIncr = nNew - pC->nMaxSize;
1323 pC->nMaxSize = nNew;
1324 pSession->nMaxChangesetSize += nIncr;
1326 return SQLITE_OK;
1330 ** This function is only called from with a pre-update-hook reporting a
1331 ** change on table pTab (attached to session pSession). The type of change
1332 ** (UPDATE, INSERT, DELETE) is specified by the first argument.
1334 ** Unless one is already present or an error occurs, an entry is added
1335 ** to the changed-rows hash table associated with table pTab.
1337 static void sessionPreupdateOneChange(
1338 int op, /* One of SQLITE_UPDATE, INSERT, DELETE */
1339 i64 iRowid,
1340 sqlite3_session *pSession, /* Session object pTab is attached to */
1341 SessionTable *pTab /* Table that change applies to */
1343 int iHash;
1344 int bNull = 0;
1345 int rc = SQLITE_OK;
1346 SessionStat1Ctx stat1 = {{0,0,0,0,0},0};
1348 if( pSession->rc ) return;
1350 /* Load table details if required */
1351 if( sessionInitTable(pSession, pTab) ) return;
1353 /* Check the number of columns in this xPreUpdate call matches the
1354 ** number of columns in the table. */
1355 if( (pTab->nCol-pTab->bRowid)!=pSession->hook.xCount(pSession->hook.pCtx) ){
1356 pSession->rc = SQLITE_SCHEMA;
1357 return;
1360 /* Grow the hash table if required */
1361 if( sessionGrowHash(pSession, 0, pTab) ){
1362 pSession->rc = SQLITE_NOMEM;
1363 return;
1366 if( pTab->bStat1 ){
1367 stat1.hook = pSession->hook;
1368 stat1.pSession = pSession;
1369 pSession->hook.pCtx = (void*)&stat1;
1370 pSession->hook.xNew = sessionStat1New;
1371 pSession->hook.xOld = sessionStat1Old;
1372 pSession->hook.xCount = sessionStat1Count;
1373 pSession->hook.xDepth = sessionStat1Depth;
1374 if( pSession->pZeroBlob==0 ){
1375 sqlite3_value *p = sqlite3ValueNew(0);
1376 if( p==0 ){
1377 rc = SQLITE_NOMEM;
1378 goto error_out;
1380 sqlite3ValueSetStr(p, 0, "", 0, SQLITE_STATIC);
1381 pSession->pZeroBlob = p;
1385 /* Calculate the hash-key for this change. If the primary key of the row
1386 ** includes a NULL value, exit early. Such changes are ignored by the
1387 ** session module. */
1388 rc = sessionPreupdateHash(
1389 pSession, iRowid, pTab, op==SQLITE_INSERT, &iHash, &bNull
1391 if( rc!=SQLITE_OK ) goto error_out;
1393 if( bNull==0 ){
1394 /* Search the hash table for an existing record for this row. */
1395 SessionChange *pC;
1396 for(pC=pTab->apChange[iHash]; pC; pC=pC->pNext){
1397 if( sessionPreupdateEqual(pSession, iRowid, pTab, pC, op) ) break;
1400 if( pC==0 ){
1401 /* Create a new change object containing all the old values (if
1402 ** this is an SQLITE_UPDATE or SQLITE_DELETE), or just the PK
1403 ** values (if this is an INSERT). */
1404 sqlite3_int64 nByte; /* Number of bytes to allocate */
1405 int i; /* Used to iterate through columns */
1407 assert( rc==SQLITE_OK );
1408 pTab->nEntry++;
1410 /* Figure out how large an allocation is required */
1411 nByte = sizeof(SessionChange);
1412 for(i=0; i<(pTab->nCol-pTab->bRowid); i++){
1413 sqlite3_value *p = 0;
1414 if( op!=SQLITE_INSERT ){
1415 TESTONLY(int trc = ) pSession->hook.xOld(pSession->hook.pCtx, i, &p);
1416 assert( trc==SQLITE_OK );
1417 }else if( pTab->abPK[i] ){
1418 TESTONLY(int trc = ) pSession->hook.xNew(pSession->hook.pCtx, i, &p);
1419 assert( trc==SQLITE_OK );
1422 /* This may fail if SQLite value p contains a utf-16 string that must
1423 ** be converted to utf-8 and an OOM error occurs while doing so. */
1424 rc = sessionSerializeValue(0, p, &nByte);
1425 if( rc!=SQLITE_OK ) goto error_out;
1427 if( pTab->bRowid ){
1428 nByte += 9; /* Size of rowid field - an integer */
1431 /* Allocate the change object */
1432 pC = (SessionChange *)sessionMalloc64(pSession, nByte);
1433 if( !pC ){
1434 rc = SQLITE_NOMEM;
1435 goto error_out;
1436 }else{
1437 memset(pC, 0, sizeof(SessionChange));
1438 pC->aRecord = (u8 *)&pC[1];
1441 /* Populate the change object. None of the preupdate_old(),
1442 ** preupdate_new() or SerializeValue() calls below may fail as all
1443 ** required values and encodings have already been cached in memory.
1444 ** It is not possible for an OOM to occur in this block. */
1445 nByte = 0;
1446 if( pTab->bRowid ){
1447 pC->aRecord[0] = SQLITE_INTEGER;
1448 sessionPutI64(&pC->aRecord[1], iRowid);
1449 nByte = 9;
1451 for(i=0; i<(pTab->nCol-pTab->bRowid); i++){
1452 sqlite3_value *p = 0;
1453 if( op!=SQLITE_INSERT ){
1454 pSession->hook.xOld(pSession->hook.pCtx, i, &p);
1455 }else if( pTab->abPK[i] ){
1456 pSession->hook.xNew(pSession->hook.pCtx, i, &p);
1458 sessionSerializeValue(&pC->aRecord[nByte], p, &nByte);
1461 /* Add the change to the hash-table */
1462 if( pSession->bIndirect || pSession->hook.xDepth(pSession->hook.pCtx) ){
1463 pC->bIndirect = 1;
1465 pC->nRecord = nByte;
1466 pC->op = op;
1467 pC->pNext = pTab->apChange[iHash];
1468 pTab->apChange[iHash] = pC;
1470 }else if( pC->bIndirect ){
1471 /* If the existing change is considered "indirect", but this current
1472 ** change is "direct", mark the change object as direct. */
1473 if( pSession->hook.xDepth(pSession->hook.pCtx)==0
1474 && pSession->bIndirect==0
1476 pC->bIndirect = 0;
1480 assert( rc==SQLITE_OK );
1481 if( pSession->bEnableSize ){
1482 rc = sessionUpdateMaxSize(op, pSession, pTab, pC);
1487 /* If an error has occurred, mark the session object as failed. */
1488 error_out:
1489 if( pTab->bStat1 ){
1490 pSession->hook = stat1.hook;
1492 if( rc!=SQLITE_OK ){
1493 pSession->rc = rc;
1497 static int sessionFindTable(
1498 sqlite3_session *pSession,
1499 const char *zName,
1500 SessionTable **ppTab
1502 int rc = SQLITE_OK;
1503 int nName = sqlite3Strlen30(zName);
1504 SessionTable *pRet;
1506 /* Search for an existing table */
1507 for(pRet=pSession->pTable; pRet; pRet=pRet->pNext){
1508 if( 0==sqlite3_strnicmp(pRet->zName, zName, nName+1) ) break;
1511 if( pRet==0 && pSession->bAutoAttach ){
1512 /* If there is a table-filter configured, invoke it. If it returns 0,
1513 ** do not automatically add the new table. */
1514 if( pSession->xTableFilter==0
1515 || pSession->xTableFilter(pSession->pFilterCtx, zName)
1517 rc = sqlite3session_attach(pSession, zName);
1518 if( rc==SQLITE_OK ){
1519 pRet = pSession->pTable;
1520 while( ALWAYS(pRet) && pRet->pNext ){
1521 pRet = pRet->pNext;
1523 assert( pRet!=0 );
1524 assert( 0==sqlite3_strnicmp(pRet->zName, zName, nName+1) );
1529 assert( rc==SQLITE_OK || pRet==0 );
1530 *ppTab = pRet;
1531 return rc;
1535 ** The 'pre-update' hook registered by this module with SQLite databases.
1537 static void xPreUpdate(
1538 void *pCtx, /* Copy of third arg to preupdate_hook() */
1539 sqlite3 *db, /* Database handle */
1540 int op, /* SQLITE_UPDATE, DELETE or INSERT */
1541 char const *zDb, /* Database name */
1542 char const *zName, /* Table name */
1543 sqlite3_int64 iKey1, /* Rowid of row about to be deleted/updated */
1544 sqlite3_int64 iKey2 /* New rowid value (for a rowid UPDATE) */
1546 sqlite3_session *pSession;
1547 int nDb = sqlite3Strlen30(zDb);
1549 assert( sqlite3_mutex_held(db->mutex) );
1550 (void)iKey1;
1551 (void)iKey2;
1553 for(pSession=(sqlite3_session *)pCtx; pSession; pSession=pSession->pNext){
1554 SessionTable *pTab;
1556 /* If this session is attached to a different database ("main", "temp"
1557 ** etc.), or if it is not currently enabled, there is nothing to do. Skip
1558 ** to the next session object attached to this database. */
1559 if( pSession->bEnable==0 ) continue;
1560 if( pSession->rc ) continue;
1561 if( sqlite3_strnicmp(zDb, pSession->zDb, nDb+1) ) continue;
1563 pSession->rc = sessionFindTable(pSession, zName, &pTab);
1564 if( pTab ){
1565 assert( pSession->rc==SQLITE_OK );
1566 assert( op==SQLITE_UPDATE || iKey1==iKey2 );
1567 sessionPreupdateOneChange(op, iKey1, pSession, pTab);
1568 if( op==SQLITE_UPDATE ){
1569 sessionPreupdateOneChange(SQLITE_INSERT, iKey2, pSession, pTab);
1576 ** The pre-update hook implementations.
1578 static int sessionPreupdateOld(void *pCtx, int iVal, sqlite3_value **ppVal){
1579 return sqlite3_preupdate_old((sqlite3*)pCtx, iVal, ppVal);
1581 static int sessionPreupdateNew(void *pCtx, int iVal, sqlite3_value **ppVal){
1582 return sqlite3_preupdate_new((sqlite3*)pCtx, iVal, ppVal);
1584 static int sessionPreupdateCount(void *pCtx){
1585 return sqlite3_preupdate_count((sqlite3*)pCtx);
1587 static int sessionPreupdateDepth(void *pCtx){
1588 return sqlite3_preupdate_depth((sqlite3*)pCtx);
1592 ** Install the pre-update hooks on the session object passed as the only
1593 ** argument.
1595 static void sessionPreupdateHooks(
1596 sqlite3_session *pSession
1598 pSession->hook.pCtx = (void*)pSession->db;
1599 pSession->hook.xOld = sessionPreupdateOld;
1600 pSession->hook.xNew = sessionPreupdateNew;
1601 pSession->hook.xCount = sessionPreupdateCount;
1602 pSession->hook.xDepth = sessionPreupdateDepth;
1605 typedef struct SessionDiffCtx SessionDiffCtx;
1606 struct SessionDiffCtx {
1607 sqlite3_stmt *pStmt;
1608 int bRowid;
1609 int nOldOff;
1613 ** The diff hook implementations.
1615 static int sessionDiffOld(void *pCtx, int iVal, sqlite3_value **ppVal){
1616 SessionDiffCtx *p = (SessionDiffCtx*)pCtx;
1617 *ppVal = sqlite3_column_value(p->pStmt, iVal+p->nOldOff+p->bRowid);
1618 return SQLITE_OK;
1620 static int sessionDiffNew(void *pCtx, int iVal, sqlite3_value **ppVal){
1621 SessionDiffCtx *p = (SessionDiffCtx*)pCtx;
1622 *ppVal = sqlite3_column_value(p->pStmt, iVal+p->bRowid);
1623 return SQLITE_OK;
1625 static int sessionDiffCount(void *pCtx){
1626 SessionDiffCtx *p = (SessionDiffCtx*)pCtx;
1627 return (p->nOldOff ? p->nOldOff : sqlite3_column_count(p->pStmt)) - p->bRowid;
1629 static int sessionDiffDepth(void *pCtx){
1630 (void)pCtx;
1631 return 0;
1635 ** Install the diff hooks on the session object passed as the only
1636 ** argument.
1638 static void sessionDiffHooks(
1639 sqlite3_session *pSession,
1640 SessionDiffCtx *pDiffCtx
1642 pSession->hook.pCtx = (void*)pDiffCtx;
1643 pSession->hook.xOld = sessionDiffOld;
1644 pSession->hook.xNew = sessionDiffNew;
1645 pSession->hook.xCount = sessionDiffCount;
1646 pSession->hook.xDepth = sessionDiffDepth;
1649 static char *sessionExprComparePK(
1650 int nCol,
1651 const char *zDb1, const char *zDb2,
1652 const char *zTab,
1653 const char **azCol, u8 *abPK
1655 int i;
1656 const char *zSep = "";
1657 char *zRet = 0;
1659 for(i=0; i<nCol; i++){
1660 if( abPK[i] ){
1661 zRet = sqlite3_mprintf("%z%s\"%w\".\"%w\".\"%w\"=\"%w\".\"%w\".\"%w\"",
1662 zRet, zSep, zDb1, zTab, azCol[i], zDb2, zTab, azCol[i]
1664 zSep = " AND ";
1665 if( zRet==0 ) break;
1669 return zRet;
1672 static char *sessionExprCompareOther(
1673 int nCol,
1674 const char *zDb1, const char *zDb2,
1675 const char *zTab,
1676 const char **azCol, u8 *abPK
1678 int i;
1679 const char *zSep = "";
1680 char *zRet = 0;
1681 int bHave = 0;
1683 for(i=0; i<nCol; i++){
1684 if( abPK[i]==0 ){
1685 bHave = 1;
1686 zRet = sqlite3_mprintf(
1687 "%z%s\"%w\".\"%w\".\"%w\" IS NOT \"%w\".\"%w\".\"%w\"",
1688 zRet, zSep, zDb1, zTab, azCol[i], zDb2, zTab, azCol[i]
1690 zSep = " OR ";
1691 if( zRet==0 ) break;
1695 if( bHave==0 ){
1696 assert( zRet==0 );
1697 zRet = sqlite3_mprintf("0");
1700 return zRet;
1703 static char *sessionSelectFindNew(
1704 const char *zDb1, /* Pick rows in this db only */
1705 const char *zDb2, /* But not in this one */
1706 int bRowid,
1707 const char *zTbl, /* Table name */
1708 const char *zExpr
1710 const char *zSel = (bRowid ? SESSIONS_ROWID ", *" : "*");
1711 char *zRet = sqlite3_mprintf(
1712 "SELECT %s FROM \"%w\".\"%w\" WHERE NOT EXISTS ("
1713 " SELECT 1 FROM \"%w\".\"%w\" WHERE %s"
1714 ")",
1715 zSel, zDb1, zTbl, zDb2, zTbl, zExpr
1717 return zRet;
1720 static int sessionDiffFindNew(
1721 int op,
1722 sqlite3_session *pSession,
1723 SessionTable *pTab,
1724 const char *zDb1,
1725 const char *zDb2,
1726 char *zExpr
1728 int rc = SQLITE_OK;
1729 char *zStmt = sessionSelectFindNew(
1730 zDb1, zDb2, pTab->bRowid, pTab->zName, zExpr
1733 if( zStmt==0 ){
1734 rc = SQLITE_NOMEM;
1735 }else{
1736 sqlite3_stmt *pStmt;
1737 rc = sqlite3_prepare(pSession->db, zStmt, -1, &pStmt, 0);
1738 if( rc==SQLITE_OK ){
1739 SessionDiffCtx *pDiffCtx = (SessionDiffCtx*)pSession->hook.pCtx;
1740 pDiffCtx->pStmt = pStmt;
1741 pDiffCtx->nOldOff = 0;
1742 pDiffCtx->bRowid = pTab->bRowid;
1743 while( SQLITE_ROW==sqlite3_step(pStmt) ){
1744 i64 iRowid = (pTab->bRowid ? sqlite3_column_int64(pStmt, 0) : 0);
1745 sessionPreupdateOneChange(op, iRowid, pSession, pTab);
1747 rc = sqlite3_finalize(pStmt);
1749 sqlite3_free(zStmt);
1752 return rc;
1756 ** Return a comma-separated list of the fully-qualified (with both database
1757 ** and table name) column names from table pTab. e.g.
1759 ** "main"."t1"."a", "main"."t1"."b", "main"."t1"."c"
1761 static char *sessionAllCols(
1762 const char *zDb,
1763 SessionTable *pTab
1765 int ii;
1766 char *zRet = 0;
1767 for(ii=0; ii<pTab->nCol; ii++){
1768 zRet = sqlite3_mprintf("%z%s\"%w\".\"%w\".\"%w\"",
1769 zRet, (zRet ? ", " : ""), zDb, pTab->zName, pTab->azCol[ii]
1771 if( !zRet ) break;
1773 return zRet;
1776 static int sessionDiffFindModified(
1777 sqlite3_session *pSession,
1778 SessionTable *pTab,
1779 const char *zFrom,
1780 const char *zExpr
1782 int rc = SQLITE_OK;
1784 char *zExpr2 = sessionExprCompareOther(pTab->nCol,
1785 pSession->zDb, zFrom, pTab->zName, pTab->azCol, pTab->abPK
1787 if( zExpr2==0 ){
1788 rc = SQLITE_NOMEM;
1789 }else{
1790 char *z1 = sessionAllCols(pSession->zDb, pTab);
1791 char *z2 = sessionAllCols(zFrom, pTab);
1792 char *zStmt = sqlite3_mprintf(
1793 "SELECT %s,%s FROM \"%w\".\"%w\", \"%w\".\"%w\" WHERE %s AND (%z)",
1794 z1, z2, pSession->zDb, pTab->zName, zFrom, pTab->zName, zExpr, zExpr2
1796 if( zStmt==0 || z1==0 || z2==0 ){
1797 rc = SQLITE_NOMEM;
1798 }else{
1799 sqlite3_stmt *pStmt;
1800 rc = sqlite3_prepare(pSession->db, zStmt, -1, &pStmt, 0);
1802 if( rc==SQLITE_OK ){
1803 SessionDiffCtx *pDiffCtx = (SessionDiffCtx*)pSession->hook.pCtx;
1804 pDiffCtx->pStmt = pStmt;
1805 pDiffCtx->nOldOff = pTab->nCol;
1806 while( SQLITE_ROW==sqlite3_step(pStmt) ){
1807 i64 iRowid = (pTab->bRowid ? sqlite3_column_int64(pStmt, 0) : 0);
1808 sessionPreupdateOneChange(SQLITE_UPDATE, iRowid, pSession, pTab);
1810 rc = sqlite3_finalize(pStmt);
1813 sqlite3_free(zStmt);
1814 sqlite3_free(z1);
1815 sqlite3_free(z2);
1818 return rc;
1821 int sqlite3session_diff(
1822 sqlite3_session *pSession,
1823 const char *zFrom,
1824 const char *zTbl,
1825 char **pzErrMsg
1827 const char *zDb = pSession->zDb;
1828 int rc = pSession->rc;
1829 SessionDiffCtx d;
1831 memset(&d, 0, sizeof(d));
1832 sessionDiffHooks(pSession, &d);
1834 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
1835 if( pzErrMsg ) *pzErrMsg = 0;
1836 if( rc==SQLITE_OK ){
1837 char *zExpr = 0;
1838 sqlite3 *db = pSession->db;
1839 SessionTable *pTo; /* Table zTbl */
1841 /* Locate and if necessary initialize the target table object */
1842 rc = sessionFindTable(pSession, zTbl, &pTo);
1843 if( pTo==0 ) goto diff_out;
1844 if( sessionInitTable(pSession, pTo) ){
1845 rc = pSession->rc;
1846 goto diff_out;
1849 /* Check the table schemas match */
1850 if( rc==SQLITE_OK ){
1851 int bHasPk = 0;
1852 int bMismatch = 0;
1853 int nCol; /* Columns in zFrom.zTbl */
1854 int bRowid = 0;
1855 u8 *abPK;
1856 const char **azCol = 0;
1857 rc = sessionTableInfo(0, db, zFrom, zTbl, &nCol, 0, &azCol, &abPK,
1858 pSession->bImplicitPK ? &bRowid : 0
1860 if( rc==SQLITE_OK ){
1861 if( pTo->nCol!=nCol ){
1862 bMismatch = 1;
1863 }else{
1864 int i;
1865 for(i=0; i<nCol; i++){
1866 if( pTo->abPK[i]!=abPK[i] ) bMismatch = 1;
1867 if( sqlite3_stricmp(azCol[i], pTo->azCol[i]) ) bMismatch = 1;
1868 if( abPK[i] ) bHasPk = 1;
1872 sqlite3_free((char*)azCol);
1873 if( bMismatch ){
1874 if( pzErrMsg ){
1875 *pzErrMsg = sqlite3_mprintf("table schemas do not match");
1877 rc = SQLITE_SCHEMA;
1879 if( bHasPk==0 ){
1880 /* Ignore tables with no primary keys */
1881 goto diff_out;
1885 if( rc==SQLITE_OK ){
1886 zExpr = sessionExprComparePK(pTo->nCol,
1887 zDb, zFrom, pTo->zName, pTo->azCol, pTo->abPK
1891 /* Find new rows */
1892 if( rc==SQLITE_OK ){
1893 rc = sessionDiffFindNew(SQLITE_INSERT, pSession, pTo, zDb, zFrom, zExpr);
1896 /* Find old rows */
1897 if( rc==SQLITE_OK ){
1898 rc = sessionDiffFindNew(SQLITE_DELETE, pSession, pTo, zFrom, zDb, zExpr);
1901 /* Find modified rows */
1902 if( rc==SQLITE_OK ){
1903 rc = sessionDiffFindModified(pSession, pTo, zFrom, zExpr);
1906 sqlite3_free(zExpr);
1909 diff_out:
1910 sessionPreupdateHooks(pSession);
1911 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
1912 return rc;
1916 ** Create a session object. This session object will record changes to
1917 ** database zDb attached to connection db.
1919 int sqlite3session_create(
1920 sqlite3 *db, /* Database handle */
1921 const char *zDb, /* Name of db (e.g. "main") */
1922 sqlite3_session **ppSession /* OUT: New session object */
1924 sqlite3_session *pNew; /* Newly allocated session object */
1925 sqlite3_session *pOld; /* Session object already attached to db */
1926 int nDb = sqlite3Strlen30(zDb); /* Length of zDb in bytes */
1928 /* Zero the output value in case an error occurs. */
1929 *ppSession = 0;
1931 /* Allocate and populate the new session object. */
1932 pNew = (sqlite3_session *)sqlite3_malloc64(sizeof(sqlite3_session) + nDb + 1);
1933 if( !pNew ) return SQLITE_NOMEM;
1934 memset(pNew, 0, sizeof(sqlite3_session));
1935 pNew->db = db;
1936 pNew->zDb = (char *)&pNew[1];
1937 pNew->bEnable = 1;
1938 memcpy(pNew->zDb, zDb, nDb+1);
1939 sessionPreupdateHooks(pNew);
1941 /* Add the new session object to the linked list of session objects
1942 ** attached to database handle $db. Do this under the cover of the db
1943 ** handle mutex. */
1944 sqlite3_mutex_enter(sqlite3_db_mutex(db));
1945 pOld = (sqlite3_session*)sqlite3_preupdate_hook(db, xPreUpdate, (void*)pNew);
1946 pNew->pNext = pOld;
1947 sqlite3_mutex_leave(sqlite3_db_mutex(db));
1949 *ppSession = pNew;
1950 return SQLITE_OK;
1954 ** Free the list of table objects passed as the first argument. The contents
1955 ** of the changed-rows hash tables are also deleted.
1957 static void sessionDeleteTable(sqlite3_session *pSession, SessionTable *pList){
1958 SessionTable *pNext;
1959 SessionTable *pTab;
1961 for(pTab=pList; pTab; pTab=pNext){
1962 int i;
1963 pNext = pTab->pNext;
1964 for(i=0; i<pTab->nChange; i++){
1965 SessionChange *p;
1966 SessionChange *pNextChange;
1967 for(p=pTab->apChange[i]; p; p=pNextChange){
1968 pNextChange = p->pNext;
1969 sessionFree(pSession, p);
1972 sessionFree(pSession, (char*)pTab->azCol); /* cast works around VC++ bug */
1973 sessionFree(pSession, pTab->apChange);
1974 sessionFree(pSession, pTab);
1979 ** Delete a session object previously allocated using sqlite3session_create().
1981 void sqlite3session_delete(sqlite3_session *pSession){
1982 sqlite3 *db = pSession->db;
1983 sqlite3_session *pHead;
1984 sqlite3_session **pp;
1986 /* Unlink the session from the linked list of sessions attached to the
1987 ** database handle. Hold the db mutex while doing so. */
1988 sqlite3_mutex_enter(sqlite3_db_mutex(db));
1989 pHead = (sqlite3_session*)sqlite3_preupdate_hook(db, 0, 0);
1990 for(pp=&pHead; ALWAYS((*pp)!=0); pp=&((*pp)->pNext)){
1991 if( (*pp)==pSession ){
1992 *pp = (*pp)->pNext;
1993 if( pHead ) sqlite3_preupdate_hook(db, xPreUpdate, (void*)pHead);
1994 break;
1997 sqlite3_mutex_leave(sqlite3_db_mutex(db));
1998 sqlite3ValueFree(pSession->pZeroBlob);
2000 /* Delete all attached table objects. And the contents of their
2001 ** associated hash-tables. */
2002 sessionDeleteTable(pSession, pSession->pTable);
2004 /* Assert that all allocations have been freed and then free the
2005 ** session object itself. */
2006 assert( pSession->nMalloc==0 );
2007 sqlite3_free(pSession);
2011 ** Set a table filter on a Session Object.
2013 void sqlite3session_table_filter(
2014 sqlite3_session *pSession,
2015 int(*xFilter)(void*, const char*),
2016 void *pCtx /* First argument passed to xFilter */
2018 pSession->bAutoAttach = 1;
2019 pSession->pFilterCtx = pCtx;
2020 pSession->xTableFilter = xFilter;
2024 ** Attach a table to a session. All subsequent changes made to the table
2025 ** while the session object is enabled will be recorded.
2027 ** Only tables that have a PRIMARY KEY defined may be attached. It does
2028 ** not matter if the PRIMARY KEY is an "INTEGER PRIMARY KEY" (rowid alias)
2029 ** or not.
2031 int sqlite3session_attach(
2032 sqlite3_session *pSession, /* Session object */
2033 const char *zName /* Table name */
2035 int rc = SQLITE_OK;
2036 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
2038 if( !zName ){
2039 pSession->bAutoAttach = 1;
2040 }else{
2041 SessionTable *pTab; /* New table object (if required) */
2042 int nName; /* Number of bytes in string zName */
2044 /* First search for an existing entry. If one is found, this call is
2045 ** a no-op. Return early. */
2046 nName = sqlite3Strlen30(zName);
2047 for(pTab=pSession->pTable; pTab; pTab=pTab->pNext){
2048 if( 0==sqlite3_strnicmp(pTab->zName, zName, nName+1) ) break;
2051 if( !pTab ){
2052 /* Allocate new SessionTable object. */
2053 int nByte = sizeof(SessionTable) + nName + 1;
2054 pTab = (SessionTable*)sessionMalloc64(pSession, nByte);
2055 if( !pTab ){
2056 rc = SQLITE_NOMEM;
2057 }else{
2058 /* Populate the new SessionTable object and link it into the list.
2059 ** The new object must be linked onto the end of the list, not
2060 ** simply added to the start of it in order to ensure that tables
2061 ** appear in the correct order when a changeset or patchset is
2062 ** eventually generated. */
2063 SessionTable **ppTab;
2064 memset(pTab, 0, sizeof(SessionTable));
2065 pTab->zName = (char *)&pTab[1];
2066 memcpy(pTab->zName, zName, nName+1);
2067 for(ppTab=&pSession->pTable; *ppTab; ppTab=&(*ppTab)->pNext);
2068 *ppTab = pTab;
2073 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
2074 return rc;
2078 ** Ensure that there is room in the buffer to append nByte bytes of data.
2079 ** If not, use sqlite3_realloc() to grow the buffer so that there is.
2081 ** If successful, return zero. Otherwise, if an OOM condition is encountered,
2082 ** set *pRc to SQLITE_NOMEM and return non-zero.
2084 static int sessionBufferGrow(SessionBuffer *p, i64 nByte, int *pRc){
2085 #define SESSION_MAX_BUFFER_SZ (0x7FFFFF00 - 1)
2086 i64 nReq = p->nBuf + nByte;
2087 if( *pRc==SQLITE_OK && nReq>p->nAlloc ){
2088 u8 *aNew;
2089 i64 nNew = p->nAlloc ? p->nAlloc : 128;
2091 do {
2092 nNew = nNew*2;
2093 }while( nNew<nReq );
2095 /* The value of SESSION_MAX_BUFFER_SZ is copied from the implementation
2096 ** of sqlite3_realloc64(). Allocations greater than this size in bytes
2097 ** always fail. It is used here to ensure that this routine can always
2098 ** allocate up to this limit - instead of up to the largest power of
2099 ** two smaller than the limit. */
2100 if( nNew>SESSION_MAX_BUFFER_SZ ){
2101 nNew = SESSION_MAX_BUFFER_SZ;
2102 if( nNew<nReq ){
2103 *pRc = SQLITE_NOMEM;
2104 return 1;
2108 aNew = (u8 *)sqlite3_realloc64(p->aBuf, nNew);
2109 if( 0==aNew ){
2110 *pRc = SQLITE_NOMEM;
2111 }else{
2112 p->aBuf = aNew;
2113 p->nAlloc = nNew;
2116 return (*pRc!=SQLITE_OK);
2120 ** Append the value passed as the second argument to the buffer passed
2121 ** as the first.
2123 ** This function is a no-op if *pRc is non-zero when it is called.
2124 ** Otherwise, if an error occurs, *pRc is set to an SQLite error code
2125 ** before returning.
2127 static void sessionAppendValue(SessionBuffer *p, sqlite3_value *pVal, int *pRc){
2128 int rc = *pRc;
2129 if( rc==SQLITE_OK ){
2130 sqlite3_int64 nByte = 0;
2131 rc = sessionSerializeValue(0, pVal, &nByte);
2132 sessionBufferGrow(p, nByte, &rc);
2133 if( rc==SQLITE_OK ){
2134 rc = sessionSerializeValue(&p->aBuf[p->nBuf], pVal, 0);
2135 p->nBuf += nByte;
2136 }else{
2137 *pRc = rc;
2143 ** This function is a no-op if *pRc is other than SQLITE_OK when it is
2144 ** called. Otherwise, append a single byte to the buffer.
2146 ** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
2147 ** returning.
2149 static void sessionAppendByte(SessionBuffer *p, u8 v, int *pRc){
2150 if( 0==sessionBufferGrow(p, 1, pRc) ){
2151 p->aBuf[p->nBuf++] = v;
2156 ** This function is a no-op if *pRc is other than SQLITE_OK when it is
2157 ** called. Otherwise, append a single varint to the buffer.
2159 ** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
2160 ** returning.
2162 static void sessionAppendVarint(SessionBuffer *p, int v, int *pRc){
2163 if( 0==sessionBufferGrow(p, 9, pRc) ){
2164 p->nBuf += sessionVarintPut(&p->aBuf[p->nBuf], v);
2169 ** This function is a no-op if *pRc is other than SQLITE_OK when it is
2170 ** called. Otherwise, append a blob of data to the buffer.
2172 ** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
2173 ** returning.
2175 static void sessionAppendBlob(
2176 SessionBuffer *p,
2177 const u8 *aBlob,
2178 int nBlob,
2179 int *pRc
2181 if( nBlob>0 && 0==sessionBufferGrow(p, nBlob, pRc) ){
2182 memcpy(&p->aBuf[p->nBuf], aBlob, nBlob);
2183 p->nBuf += nBlob;
2188 ** This function is a no-op if *pRc is other than SQLITE_OK when it is
2189 ** called. Otherwise, append a string to the buffer. All bytes in the string
2190 ** up to (but not including) the nul-terminator are written to the buffer.
2192 ** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
2193 ** returning.
2195 static void sessionAppendStr(
2196 SessionBuffer *p,
2197 const char *zStr,
2198 int *pRc
2200 int nStr = sqlite3Strlen30(zStr);
2201 if( 0==sessionBufferGrow(p, nStr+1, pRc) ){
2202 memcpy(&p->aBuf[p->nBuf], zStr, nStr);
2203 p->nBuf += nStr;
2204 p->aBuf[p->nBuf] = 0x00;
2209 ** This function is a no-op if *pRc is other than SQLITE_OK when it is
2210 ** called. Otherwise, append the string representation of integer iVal
2211 ** to the buffer. No nul-terminator is written.
2213 ** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
2214 ** returning.
2216 static void sessionAppendInteger(
2217 SessionBuffer *p, /* Buffer to append to */
2218 int iVal, /* Value to write the string rep. of */
2219 int *pRc /* IN/OUT: Error code */
2221 char aBuf[24];
2222 sqlite3_snprintf(sizeof(aBuf)-1, aBuf, "%d", iVal);
2223 sessionAppendStr(p, aBuf, pRc);
2226 static void sessionAppendPrintf(
2227 SessionBuffer *p, /* Buffer to append to */
2228 int *pRc,
2229 const char *zFmt,
2232 if( *pRc==SQLITE_OK ){
2233 char *zApp = 0;
2234 va_list ap;
2235 va_start(ap, zFmt);
2236 zApp = sqlite3_vmprintf(zFmt, ap);
2237 if( zApp==0 ){
2238 *pRc = SQLITE_NOMEM;
2239 }else{
2240 sessionAppendStr(p, zApp, pRc);
2242 va_end(ap);
2243 sqlite3_free(zApp);
2248 ** This function is a no-op if *pRc is other than SQLITE_OK when it is
2249 ** called. Otherwise, append the string zStr enclosed in quotes (") and
2250 ** with any embedded quote characters escaped to the buffer. No
2251 ** nul-terminator byte is written.
2253 ** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
2254 ** returning.
2256 static void sessionAppendIdent(
2257 SessionBuffer *p, /* Buffer to a append to */
2258 const char *zStr, /* String to quote, escape and append */
2259 int *pRc /* IN/OUT: Error code */
2261 int nStr = sqlite3Strlen30(zStr)*2 + 2 + 2;
2262 if( 0==sessionBufferGrow(p, nStr, pRc) ){
2263 char *zOut = (char *)&p->aBuf[p->nBuf];
2264 const char *zIn = zStr;
2265 *zOut++ = '"';
2266 while( *zIn ){
2267 if( *zIn=='"' ) *zOut++ = '"';
2268 *zOut++ = *(zIn++);
2270 *zOut++ = '"';
2271 p->nBuf = (int)((u8 *)zOut - p->aBuf);
2272 p->aBuf[p->nBuf] = 0x00;
2277 ** This function is a no-op if *pRc is other than SQLITE_OK when it is
2278 ** called. Otherwse, it appends the serialized version of the value stored
2279 ** in column iCol of the row that SQL statement pStmt currently points
2280 ** to to the buffer.
2282 static void sessionAppendCol(
2283 SessionBuffer *p, /* Buffer to append to */
2284 sqlite3_stmt *pStmt, /* Handle pointing to row containing value */
2285 int iCol, /* Column to read value from */
2286 int *pRc /* IN/OUT: Error code */
2288 if( *pRc==SQLITE_OK ){
2289 int eType = sqlite3_column_type(pStmt, iCol);
2290 sessionAppendByte(p, (u8)eType, pRc);
2291 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
2292 sqlite3_int64 i;
2293 u8 aBuf[8];
2294 if( eType==SQLITE_INTEGER ){
2295 i = sqlite3_column_int64(pStmt, iCol);
2296 }else{
2297 double r = sqlite3_column_double(pStmt, iCol);
2298 memcpy(&i, &r, 8);
2300 sessionPutI64(aBuf, i);
2301 sessionAppendBlob(p, aBuf, 8, pRc);
2303 if( eType==SQLITE_BLOB || eType==SQLITE_TEXT ){
2304 u8 *z;
2305 int nByte;
2306 if( eType==SQLITE_BLOB ){
2307 z = (u8 *)sqlite3_column_blob(pStmt, iCol);
2308 }else{
2309 z = (u8 *)sqlite3_column_text(pStmt, iCol);
2311 nByte = sqlite3_column_bytes(pStmt, iCol);
2312 if( z || (eType==SQLITE_BLOB && nByte==0) ){
2313 sessionAppendVarint(p, nByte, pRc);
2314 sessionAppendBlob(p, z, nByte, pRc);
2315 }else{
2316 *pRc = SQLITE_NOMEM;
2324 ** This function appends an update change to the buffer (see the comments
2325 ** under "CHANGESET FORMAT" at the top of the file). An update change
2326 ** consists of:
2328 ** 1 byte: SQLITE_UPDATE (0x17)
2329 ** n bytes: old.* record (see RECORD FORMAT)
2330 ** m bytes: new.* record (see RECORD FORMAT)
2332 ** The SessionChange object passed as the third argument contains the
2333 ** values that were stored in the row when the session began (the old.*
2334 ** values). The statement handle passed as the second argument points
2335 ** at the current version of the row (the new.* values).
2337 ** If all of the old.* values are equal to their corresponding new.* value
2338 ** (i.e. nothing has changed), then no data at all is appended to the buffer.
2340 ** Otherwise, the old.* record contains all primary key values and the
2341 ** original values of any fields that have been modified. The new.* record
2342 ** contains the new values of only those fields that have been modified.
2344 static int sessionAppendUpdate(
2345 SessionBuffer *pBuf, /* Buffer to append to */
2346 int bPatchset, /* True for "patchset", 0 for "changeset" */
2347 sqlite3_stmt *pStmt, /* Statement handle pointing at new row */
2348 SessionChange *p, /* Object containing old values */
2349 u8 *abPK /* Boolean array - true for PK columns */
2351 int rc = SQLITE_OK;
2352 SessionBuffer buf2 = {0,0,0}; /* Buffer to accumulate new.* record in */
2353 int bNoop = 1; /* Set to zero if any values are modified */
2354 int nRewind = pBuf->nBuf; /* Set to zero if any values are modified */
2355 int i; /* Used to iterate through columns */
2356 u8 *pCsr = p->aRecord; /* Used to iterate through old.* values */
2358 assert( abPK!=0 );
2359 sessionAppendByte(pBuf, SQLITE_UPDATE, &rc);
2360 sessionAppendByte(pBuf, p->bIndirect, &rc);
2361 for(i=0; i<sqlite3_column_count(pStmt); i++){
2362 int bChanged = 0;
2363 int nAdvance;
2364 int eType = *pCsr;
2365 switch( eType ){
2366 case SQLITE_NULL:
2367 nAdvance = 1;
2368 if( sqlite3_column_type(pStmt, i)!=SQLITE_NULL ){
2369 bChanged = 1;
2371 break;
2373 case SQLITE_FLOAT:
2374 case SQLITE_INTEGER: {
2375 nAdvance = 9;
2376 if( eType==sqlite3_column_type(pStmt, i) ){
2377 sqlite3_int64 iVal = sessionGetI64(&pCsr[1]);
2378 if( eType==SQLITE_INTEGER ){
2379 if( iVal==sqlite3_column_int64(pStmt, i) ) break;
2380 }else{
2381 double dVal;
2382 memcpy(&dVal, &iVal, 8);
2383 if( dVal==sqlite3_column_double(pStmt, i) ) break;
2386 bChanged = 1;
2387 break;
2390 default: {
2391 int n;
2392 int nHdr = 1 + sessionVarintGet(&pCsr[1], &n);
2393 assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
2394 nAdvance = nHdr + n;
2395 if( eType==sqlite3_column_type(pStmt, i)
2396 && n==sqlite3_column_bytes(pStmt, i)
2397 && (n==0 || 0==memcmp(&pCsr[nHdr], sqlite3_column_blob(pStmt, i), n))
2399 break;
2401 bChanged = 1;
2405 /* If at least one field has been modified, this is not a no-op. */
2406 if( bChanged ) bNoop = 0;
2408 /* Add a field to the old.* record. This is omitted if this module is
2409 ** currently generating a patchset. */
2410 if( bPatchset==0 ){
2411 if( bChanged || abPK[i] ){
2412 sessionAppendBlob(pBuf, pCsr, nAdvance, &rc);
2413 }else{
2414 sessionAppendByte(pBuf, 0, &rc);
2418 /* Add a field to the new.* record. Or the only record if currently
2419 ** generating a patchset. */
2420 if( bChanged || (bPatchset && abPK[i]) ){
2421 sessionAppendCol(&buf2, pStmt, i, &rc);
2422 }else{
2423 sessionAppendByte(&buf2, 0, &rc);
2426 pCsr += nAdvance;
2429 if( bNoop ){
2430 pBuf->nBuf = nRewind;
2431 }else{
2432 sessionAppendBlob(pBuf, buf2.aBuf, buf2.nBuf, &rc);
2434 sqlite3_free(buf2.aBuf);
2436 return rc;
2440 ** Append a DELETE change to the buffer passed as the first argument. Use
2441 ** the changeset format if argument bPatchset is zero, or the patchset
2442 ** format otherwise.
2444 static int sessionAppendDelete(
2445 SessionBuffer *pBuf, /* Buffer to append to */
2446 int bPatchset, /* True for "patchset", 0 for "changeset" */
2447 SessionChange *p, /* Object containing old values */
2448 int nCol, /* Number of columns in table */
2449 u8 *abPK /* Boolean array - true for PK columns */
2451 int rc = SQLITE_OK;
2453 sessionAppendByte(pBuf, SQLITE_DELETE, &rc);
2454 sessionAppendByte(pBuf, p->bIndirect, &rc);
2456 if( bPatchset==0 ){
2457 sessionAppendBlob(pBuf, p->aRecord, p->nRecord, &rc);
2458 }else{
2459 int i;
2460 u8 *a = p->aRecord;
2461 for(i=0; i<nCol; i++){
2462 u8 *pStart = a;
2463 int eType = *a++;
2465 switch( eType ){
2466 case 0:
2467 case SQLITE_NULL:
2468 assert( abPK[i]==0 );
2469 break;
2471 case SQLITE_FLOAT:
2472 case SQLITE_INTEGER:
2473 a += 8;
2474 break;
2476 default: {
2477 int n;
2478 a += sessionVarintGet(a, &n);
2479 a += n;
2480 break;
2483 if( abPK[i] ){
2484 sessionAppendBlob(pBuf, pStart, (int)(a-pStart), &rc);
2487 assert( (a - p->aRecord)==p->nRecord );
2490 return rc;
2494 ** Formulate and prepare a SELECT statement to retrieve a row from table
2495 ** zTab in database zDb based on its primary key. i.e.
2497 ** SELECT *, <noop-test> FROM zDb.zTab WHERE (pk1, pk2,...) IS (?1, ?2,...)
2499 ** where <noop-test> is:
2501 ** 1 AND (?A OR ?1 IS <column>) AND ...
2503 ** for each non-pk <column>.
2505 static int sessionSelectStmt(
2506 sqlite3 *db, /* Database handle */
2507 int bIgnoreNoop,
2508 const char *zDb, /* Database name */
2509 const char *zTab, /* Table name */
2510 int bRowid,
2511 int nCol, /* Number of columns in table */
2512 const char **azCol, /* Names of table columns */
2513 u8 *abPK, /* PRIMARY KEY array */
2514 sqlite3_stmt **ppStmt /* OUT: Prepared SELECT statement */
2516 int rc = SQLITE_OK;
2517 char *zSql = 0;
2518 const char *zSep = "";
2519 const char *zCols = bRowid ? SESSIONS_ROWID ", *" : "*";
2520 int nSql = -1;
2521 int i;
2523 SessionBuffer nooptest = {0, 0, 0};
2524 SessionBuffer pkfield = {0, 0, 0};
2525 SessionBuffer pkvar = {0, 0, 0};
2527 sessionAppendStr(&nooptest, ", 1", &rc);
2529 if( 0==sqlite3_stricmp("sqlite_stat1", zTab) ){
2530 sessionAppendStr(&nooptest, " AND (?6 OR ?3 IS stat)", &rc);
2531 sessionAppendStr(&pkfield, "tbl, idx", &rc);
2532 sessionAppendStr(&pkvar,
2533 "?1, (CASE WHEN ?2=X'' THEN NULL ELSE ?2 END)", &rc
2535 zCols = "tbl, ?2, stat";
2536 }else{
2537 for(i=0; i<nCol; i++){
2538 if( abPK[i] ){
2539 sessionAppendStr(&pkfield, zSep, &rc);
2540 sessionAppendStr(&pkvar, zSep, &rc);
2541 zSep = ", ";
2542 sessionAppendIdent(&pkfield, azCol[i], &rc);
2543 sessionAppendPrintf(&pkvar, &rc, "?%d", i+1);
2544 }else{
2545 sessionAppendPrintf(&nooptest, &rc,
2546 " AND (?%d OR ?%d IS %w.%w)", i+1+nCol, i+1, zTab, azCol[i]
2552 if( rc==SQLITE_OK ){
2553 zSql = sqlite3_mprintf(
2554 "SELECT %s%s FROM %Q.%Q WHERE (%s) IS (%s)",
2555 zCols, (bIgnoreNoop ? (char*)nooptest.aBuf : ""),
2556 zDb, zTab, (char*)pkfield.aBuf, (char*)pkvar.aBuf
2558 if( zSql==0 ) rc = SQLITE_NOMEM;
2561 #if 0
2562 if( 0==sqlite3_stricmp("sqlite_stat1", zTab) ){
2563 zSql = sqlite3_mprintf(
2564 "SELECT tbl, ?2, stat FROM %Q.sqlite_stat1 WHERE tbl IS ?1 AND "
2565 "idx IS (CASE WHEN ?2=X'' THEN NULL ELSE ?2 END)", zDb
2567 if( zSql==0 ) rc = SQLITE_NOMEM;
2568 }else{
2569 const char *zSep = "";
2570 SessionBuffer buf = {0, 0, 0};
2572 sessionAppendStr(&buf, "SELECT * FROM ", &rc);
2573 sessionAppendIdent(&buf, zDb, &rc);
2574 sessionAppendStr(&buf, ".", &rc);
2575 sessionAppendIdent(&buf, zTab, &rc);
2576 sessionAppendStr(&buf, " WHERE ", &rc);
2577 for(i=0; i<nCol; i++){
2578 if( abPK[i] ){
2579 sessionAppendStr(&buf, zSep, &rc);
2580 sessionAppendIdent(&buf, azCol[i], &rc);
2581 sessionAppendStr(&buf, " IS ?", &rc);
2582 sessionAppendInteger(&buf, i+1, &rc);
2583 zSep = " AND ";
2586 zSql = (char*)buf.aBuf;
2587 nSql = buf.nBuf;
2589 #endif
2591 if( rc==SQLITE_OK ){
2592 rc = sqlite3_prepare_v2(db, zSql, nSql, ppStmt, 0);
2594 sqlite3_free(zSql);
2595 sqlite3_free(nooptest.aBuf);
2596 sqlite3_free(pkfield.aBuf);
2597 sqlite3_free(pkvar.aBuf);
2598 return rc;
2602 ** Bind the PRIMARY KEY values from the change passed in argument pChange
2603 ** to the SELECT statement passed as the first argument. The SELECT statement
2604 ** is as prepared by function sessionSelectStmt().
2606 ** Return SQLITE_OK if all PK values are successfully bound, or an SQLite
2607 ** error code (e.g. SQLITE_NOMEM) otherwise.
2609 static int sessionSelectBind(
2610 sqlite3_stmt *pSelect, /* SELECT from sessionSelectStmt() */
2611 int nCol, /* Number of columns in table */
2612 u8 *abPK, /* PRIMARY KEY array */
2613 SessionChange *pChange /* Change structure */
2615 int i;
2616 int rc = SQLITE_OK;
2617 u8 *a = pChange->aRecord;
2619 for(i=0; i<nCol && rc==SQLITE_OK; i++){
2620 int eType = *a++;
2622 switch( eType ){
2623 case 0:
2624 case SQLITE_NULL:
2625 assert( abPK[i]==0 );
2626 break;
2628 case SQLITE_INTEGER: {
2629 if( abPK[i] ){
2630 i64 iVal = sessionGetI64(a);
2631 rc = sqlite3_bind_int64(pSelect, i+1, iVal);
2633 a += 8;
2634 break;
2637 case SQLITE_FLOAT: {
2638 if( abPK[i] ){
2639 double rVal;
2640 i64 iVal = sessionGetI64(a);
2641 memcpy(&rVal, &iVal, 8);
2642 rc = sqlite3_bind_double(pSelect, i+1, rVal);
2644 a += 8;
2645 break;
2648 case SQLITE_TEXT: {
2649 int n;
2650 a += sessionVarintGet(a, &n);
2651 if( abPK[i] ){
2652 rc = sqlite3_bind_text(pSelect, i+1, (char *)a, n, SQLITE_TRANSIENT);
2654 a += n;
2655 break;
2658 default: {
2659 int n;
2660 assert( eType==SQLITE_BLOB );
2661 a += sessionVarintGet(a, &n);
2662 if( abPK[i] ){
2663 rc = sqlite3_bind_blob(pSelect, i+1, a, n, SQLITE_TRANSIENT);
2665 a += n;
2666 break;
2671 return rc;
2675 ** This function is a no-op if *pRc is set to other than SQLITE_OK when it
2676 ** is called. Otherwise, append a serialized table header (part of the binary
2677 ** changeset format) to buffer *pBuf. If an error occurs, set *pRc to an
2678 ** SQLite error code before returning.
2680 static void sessionAppendTableHdr(
2681 SessionBuffer *pBuf, /* Append header to this buffer */
2682 int bPatchset, /* Use the patchset format if true */
2683 SessionTable *pTab, /* Table object to append header for */
2684 int *pRc /* IN/OUT: Error code */
2686 /* Write a table header */
2687 sessionAppendByte(pBuf, (bPatchset ? 'P' : 'T'), pRc);
2688 sessionAppendVarint(pBuf, pTab->nCol, pRc);
2689 sessionAppendBlob(pBuf, pTab->abPK, pTab->nCol, pRc);
2690 sessionAppendBlob(pBuf, (u8 *)pTab->zName, (int)strlen(pTab->zName)+1, pRc);
2694 ** Generate either a changeset (if argument bPatchset is zero) or a patchset
2695 ** (if it is non-zero) based on the current contents of the session object
2696 ** passed as the first argument.
2698 ** If no error occurs, SQLITE_OK is returned and the new changeset/patchset
2699 ** stored in output variables *pnChangeset and *ppChangeset. Or, if an error
2700 ** occurs, an SQLite error code is returned and both output variables set
2701 ** to 0.
2703 static int sessionGenerateChangeset(
2704 sqlite3_session *pSession, /* Session object */
2705 int bPatchset, /* True for patchset, false for changeset */
2706 int (*xOutput)(void *pOut, const void *pData, int nData),
2707 void *pOut, /* First argument for xOutput */
2708 int *pnChangeset, /* OUT: Size of buffer at *ppChangeset */
2709 void **ppChangeset /* OUT: Buffer containing changeset */
2711 sqlite3 *db = pSession->db; /* Source database handle */
2712 SessionTable *pTab; /* Used to iterate through attached tables */
2713 SessionBuffer buf = {0,0,0}; /* Buffer in which to accumlate changeset */
2714 int rc; /* Return code */
2716 assert( xOutput==0 || (pnChangeset==0 && ppChangeset==0) );
2717 assert( xOutput!=0 || (pnChangeset!=0 && ppChangeset!=0) );
2719 /* Zero the output variables in case an error occurs. If this session
2720 ** object is already in the error state (sqlite3_session.rc != SQLITE_OK),
2721 ** this call will be a no-op. */
2722 if( xOutput==0 ){
2723 assert( pnChangeset!=0 && ppChangeset!=0 );
2724 *pnChangeset = 0;
2725 *ppChangeset = 0;
2728 if( pSession->rc ) return pSession->rc;
2729 rc = sqlite3_exec(pSession->db, "SAVEPOINT changeset", 0, 0, 0);
2730 if( rc!=SQLITE_OK ) return rc;
2732 sqlite3_mutex_enter(sqlite3_db_mutex(db));
2734 for(pTab=pSession->pTable; rc==SQLITE_OK && pTab; pTab=pTab->pNext){
2735 if( pTab->nEntry ){
2736 const char *zName = pTab->zName;
2737 int nCol = 0; /* Number of columns in table */
2738 u8 *abPK = 0; /* Primary key array */
2739 const char **azCol = 0; /* Table columns */
2740 int i; /* Used to iterate through hash buckets */
2741 sqlite3_stmt *pSel = 0; /* SELECT statement to query table pTab */
2742 int nRewind = buf.nBuf; /* Initial size of write buffer */
2743 int nNoop; /* Size of buffer after writing tbl header */
2744 int bRowid = 0;
2746 /* Check the table schema is still Ok. */
2747 rc = sessionTableInfo(
2748 0, db, pSession->zDb, zName, &nCol, 0, &azCol, &abPK,
2749 (pSession->bImplicitPK ? &bRowid : 0)
2751 if( rc==SQLITE_OK && (
2752 pTab->nCol!=nCol
2753 || pTab->bRowid!=bRowid
2754 || memcmp(abPK, pTab->abPK, nCol)
2756 rc = SQLITE_SCHEMA;
2759 /* Write a table header */
2760 sessionAppendTableHdr(&buf, bPatchset, pTab, &rc);
2762 /* Build and compile a statement to execute: */
2763 if( rc==SQLITE_OK ){
2764 rc = sessionSelectStmt(
2765 db, 0, pSession->zDb, zName, bRowid, nCol, azCol, abPK, &pSel
2769 nNoop = buf.nBuf;
2770 for(i=0; i<pTab->nChange && rc==SQLITE_OK; i++){
2771 SessionChange *p; /* Used to iterate through changes */
2773 for(p=pTab->apChange[i]; rc==SQLITE_OK && p; p=p->pNext){
2774 rc = sessionSelectBind(pSel, nCol, abPK, p);
2775 if( rc!=SQLITE_OK ) continue;
2776 if( sqlite3_step(pSel)==SQLITE_ROW ){
2777 if( p->op==SQLITE_INSERT ){
2778 int iCol;
2779 sessionAppendByte(&buf, SQLITE_INSERT, &rc);
2780 sessionAppendByte(&buf, p->bIndirect, &rc);
2781 for(iCol=0; iCol<nCol; iCol++){
2782 sessionAppendCol(&buf, pSel, iCol, &rc);
2784 }else{
2785 assert( abPK!=0 ); /* Because sessionSelectStmt() returned ok */
2786 rc = sessionAppendUpdate(&buf, bPatchset, pSel, p, abPK);
2788 }else if( p->op!=SQLITE_INSERT ){
2789 rc = sessionAppendDelete(&buf, bPatchset, p, nCol, abPK);
2791 if( rc==SQLITE_OK ){
2792 rc = sqlite3_reset(pSel);
2795 /* If the buffer is now larger than sessions_strm_chunk_size, pass
2796 ** its contents to the xOutput() callback. */
2797 if( xOutput
2798 && rc==SQLITE_OK
2799 && buf.nBuf>nNoop
2800 && buf.nBuf>sessions_strm_chunk_size
2802 rc = xOutput(pOut, (void*)buf.aBuf, buf.nBuf);
2803 nNoop = -1;
2804 buf.nBuf = 0;
2810 sqlite3_finalize(pSel);
2811 if( buf.nBuf==nNoop ){
2812 buf.nBuf = nRewind;
2814 sqlite3_free((char*)azCol); /* cast works around VC++ bug */
2818 if( rc==SQLITE_OK ){
2819 if( xOutput==0 ){
2820 *pnChangeset = buf.nBuf;
2821 *ppChangeset = buf.aBuf;
2822 buf.aBuf = 0;
2823 }else if( buf.nBuf>0 ){
2824 rc = xOutput(pOut, (void*)buf.aBuf, buf.nBuf);
2828 sqlite3_free(buf.aBuf);
2829 sqlite3_exec(db, "RELEASE changeset", 0, 0, 0);
2830 sqlite3_mutex_leave(sqlite3_db_mutex(db));
2831 return rc;
2835 ** Obtain a changeset object containing all changes recorded by the
2836 ** session object passed as the first argument.
2838 ** It is the responsibility of the caller to eventually free the buffer
2839 ** using sqlite3_free().
2841 int sqlite3session_changeset(
2842 sqlite3_session *pSession, /* Session object */
2843 int *pnChangeset, /* OUT: Size of buffer at *ppChangeset */
2844 void **ppChangeset /* OUT: Buffer containing changeset */
2846 int rc;
2848 if( pnChangeset==0 || ppChangeset==0 ) return SQLITE_MISUSE;
2849 rc = sessionGenerateChangeset(pSession, 0, 0, 0, pnChangeset, ppChangeset);
2850 assert( rc || pnChangeset==0
2851 || pSession->bEnableSize==0 || *pnChangeset<=pSession->nMaxChangesetSize
2853 return rc;
2857 ** Streaming version of sqlite3session_changeset().
2859 int sqlite3session_changeset_strm(
2860 sqlite3_session *pSession,
2861 int (*xOutput)(void *pOut, const void *pData, int nData),
2862 void *pOut
2864 if( xOutput==0 ) return SQLITE_MISUSE;
2865 return sessionGenerateChangeset(pSession, 0, xOutput, pOut, 0, 0);
2869 ** Streaming version of sqlite3session_patchset().
2871 int sqlite3session_patchset_strm(
2872 sqlite3_session *pSession,
2873 int (*xOutput)(void *pOut, const void *pData, int nData),
2874 void *pOut
2876 if( xOutput==0 ) return SQLITE_MISUSE;
2877 return sessionGenerateChangeset(pSession, 1, xOutput, pOut, 0, 0);
2881 ** Obtain a patchset object containing all changes recorded by the
2882 ** session object passed as the first argument.
2884 ** It is the responsibility of the caller to eventually free the buffer
2885 ** using sqlite3_free().
2887 int sqlite3session_patchset(
2888 sqlite3_session *pSession, /* Session object */
2889 int *pnPatchset, /* OUT: Size of buffer at *ppChangeset */
2890 void **ppPatchset /* OUT: Buffer containing changeset */
2892 if( pnPatchset==0 || ppPatchset==0 ) return SQLITE_MISUSE;
2893 return sessionGenerateChangeset(pSession, 1, 0, 0, pnPatchset, ppPatchset);
2897 ** Enable or disable the session object passed as the first argument.
2899 int sqlite3session_enable(sqlite3_session *pSession, int bEnable){
2900 int ret;
2901 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
2902 if( bEnable>=0 ){
2903 pSession->bEnable = bEnable;
2905 ret = pSession->bEnable;
2906 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
2907 return ret;
2911 ** Enable or disable the session object passed as the first argument.
2913 int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect){
2914 int ret;
2915 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
2916 if( bIndirect>=0 ){
2917 pSession->bIndirect = bIndirect;
2919 ret = pSession->bIndirect;
2920 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
2921 return ret;
2925 ** Return true if there have been no changes to monitored tables recorded
2926 ** by the session object passed as the only argument.
2928 int sqlite3session_isempty(sqlite3_session *pSession){
2929 int ret = 0;
2930 SessionTable *pTab;
2932 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
2933 for(pTab=pSession->pTable; pTab && ret==0; pTab=pTab->pNext){
2934 ret = (pTab->nEntry>0);
2936 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
2938 return (ret==0);
2942 ** Return the amount of heap memory in use.
2944 sqlite3_int64 sqlite3session_memory_used(sqlite3_session *pSession){
2945 return pSession->nMalloc;
2949 ** Configure the session object passed as the first argument.
2951 int sqlite3session_object_config(sqlite3_session *pSession, int op, void *pArg){
2952 int rc = SQLITE_OK;
2953 switch( op ){
2954 case SQLITE_SESSION_OBJCONFIG_SIZE: {
2955 int iArg = *(int*)pArg;
2956 if( iArg>=0 ){
2957 if( pSession->pTable ){
2958 rc = SQLITE_MISUSE;
2959 }else{
2960 pSession->bEnableSize = (iArg!=0);
2963 *(int*)pArg = pSession->bEnableSize;
2964 break;
2967 case SQLITE_SESSION_OBJCONFIG_ROWID: {
2968 int iArg = *(int*)pArg;
2969 if( iArg>=0 ){
2970 if( pSession->pTable ){
2971 rc = SQLITE_MISUSE;
2972 }else{
2973 pSession->bImplicitPK = (iArg!=0);
2976 *(int*)pArg = pSession->bImplicitPK;
2977 break;
2980 default:
2981 rc = SQLITE_MISUSE;
2984 return rc;
2988 ** Return the maximum size of sqlite3session_changeset() output.
2990 sqlite3_int64 sqlite3session_changeset_size(sqlite3_session *pSession){
2991 return pSession->nMaxChangesetSize;
2995 ** Do the work for either sqlite3changeset_start() or start_strm().
2997 static int sessionChangesetStart(
2998 sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */
2999 int (*xInput)(void *pIn, void *pData, int *pnData),
3000 void *pIn,
3001 int nChangeset, /* Size of buffer pChangeset in bytes */
3002 void *pChangeset, /* Pointer to buffer containing changeset */
3003 int bInvert, /* True to invert changeset */
3004 int bSkipEmpty /* True to skip empty UPDATE changes */
3006 sqlite3_changeset_iter *pRet; /* Iterator to return */
3007 int nByte; /* Number of bytes to allocate for iterator */
3009 assert( xInput==0 || (pChangeset==0 && nChangeset==0) );
3011 /* Zero the output variable in case an error occurs. */
3012 *pp = 0;
3014 /* Allocate and initialize the iterator structure. */
3015 nByte = sizeof(sqlite3_changeset_iter);
3016 pRet = (sqlite3_changeset_iter *)sqlite3_malloc(nByte);
3017 if( !pRet ) return SQLITE_NOMEM;
3018 memset(pRet, 0, sizeof(sqlite3_changeset_iter));
3019 pRet->in.aData = (u8 *)pChangeset;
3020 pRet->in.nData = nChangeset;
3021 pRet->in.xInput = xInput;
3022 pRet->in.pIn = pIn;
3023 pRet->in.bEof = (xInput ? 0 : 1);
3024 pRet->bInvert = bInvert;
3025 pRet->bSkipEmpty = bSkipEmpty;
3027 /* Populate the output variable and return success. */
3028 *pp = pRet;
3029 return SQLITE_OK;
3033 ** Create an iterator used to iterate through the contents of a changeset.
3035 int sqlite3changeset_start(
3036 sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */
3037 int nChangeset, /* Size of buffer pChangeset in bytes */
3038 void *pChangeset /* Pointer to buffer containing changeset */
3040 return sessionChangesetStart(pp, 0, 0, nChangeset, pChangeset, 0, 0);
3042 int sqlite3changeset_start_v2(
3043 sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */
3044 int nChangeset, /* Size of buffer pChangeset in bytes */
3045 void *pChangeset, /* Pointer to buffer containing changeset */
3046 int flags
3048 int bInvert = !!(flags & SQLITE_CHANGESETSTART_INVERT);
3049 return sessionChangesetStart(pp, 0, 0, nChangeset, pChangeset, bInvert, 0);
3053 ** Streaming version of sqlite3changeset_start().
3055 int sqlite3changeset_start_strm(
3056 sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */
3057 int (*xInput)(void *pIn, void *pData, int *pnData),
3058 void *pIn
3060 return sessionChangesetStart(pp, xInput, pIn, 0, 0, 0, 0);
3062 int sqlite3changeset_start_v2_strm(
3063 sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */
3064 int (*xInput)(void *pIn, void *pData, int *pnData),
3065 void *pIn,
3066 int flags
3068 int bInvert = !!(flags & SQLITE_CHANGESETSTART_INVERT);
3069 return sessionChangesetStart(pp, xInput, pIn, 0, 0, bInvert, 0);
3073 ** If the SessionInput object passed as the only argument is a streaming
3074 ** object and the buffer is full, discard some data to free up space.
3076 static void sessionDiscardData(SessionInput *pIn){
3077 if( pIn->xInput && pIn->iNext>=sessions_strm_chunk_size ){
3078 int nMove = pIn->buf.nBuf - pIn->iNext;
3079 assert( nMove>=0 );
3080 if( nMove>0 ){
3081 memmove(pIn->buf.aBuf, &pIn->buf.aBuf[pIn->iNext], nMove);
3083 pIn->buf.nBuf -= pIn->iNext;
3084 pIn->iNext = 0;
3085 pIn->nData = pIn->buf.nBuf;
3090 ** Ensure that there are at least nByte bytes available in the buffer. Or,
3091 ** if there are not nByte bytes remaining in the input, that all available
3092 ** data is in the buffer.
3094 ** Return an SQLite error code if an error occurs, or SQLITE_OK otherwise.
3096 static int sessionInputBuffer(SessionInput *pIn, int nByte){
3097 int rc = SQLITE_OK;
3098 if( pIn->xInput ){
3099 while( !pIn->bEof && (pIn->iNext+nByte)>=pIn->nData && rc==SQLITE_OK ){
3100 int nNew = sessions_strm_chunk_size;
3102 if( pIn->bNoDiscard==0 ) sessionDiscardData(pIn);
3103 if( SQLITE_OK==sessionBufferGrow(&pIn->buf, nNew, &rc) ){
3104 rc = pIn->xInput(pIn->pIn, &pIn->buf.aBuf[pIn->buf.nBuf], &nNew);
3105 if( nNew==0 ){
3106 pIn->bEof = 1;
3107 }else{
3108 pIn->buf.nBuf += nNew;
3112 pIn->aData = pIn->buf.aBuf;
3113 pIn->nData = pIn->buf.nBuf;
3116 return rc;
3120 ** When this function is called, *ppRec points to the start of a record
3121 ** that contains nCol values. This function advances the pointer *ppRec
3122 ** until it points to the byte immediately following that record.
3124 static void sessionSkipRecord(
3125 u8 **ppRec, /* IN/OUT: Record pointer */
3126 int nCol /* Number of values in record */
3128 u8 *aRec = *ppRec;
3129 int i;
3130 for(i=0; i<nCol; i++){
3131 int eType = *aRec++;
3132 if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
3133 int nByte;
3134 aRec += sessionVarintGet((u8*)aRec, &nByte);
3135 aRec += nByte;
3136 }else if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
3137 aRec += 8;
3141 *ppRec = aRec;
3145 ** This function sets the value of the sqlite3_value object passed as the
3146 ** first argument to a copy of the string or blob held in the aData[]
3147 ** buffer. SQLITE_OK is returned if successful, or SQLITE_NOMEM if an OOM
3148 ** error occurs.
3150 static int sessionValueSetStr(
3151 sqlite3_value *pVal, /* Set the value of this object */
3152 u8 *aData, /* Buffer containing string or blob data */
3153 int nData, /* Size of buffer aData[] in bytes */
3154 u8 enc /* String encoding (0 for blobs) */
3156 /* In theory this code could just pass SQLITE_TRANSIENT as the final
3157 ** argument to sqlite3ValueSetStr() and have the copy created
3158 ** automatically. But doing so makes it difficult to detect any OOM
3159 ** error. Hence the code to create the copy externally. */
3160 u8 *aCopy = sqlite3_malloc64((sqlite3_int64)nData+1);
3161 if( aCopy==0 ) return SQLITE_NOMEM;
3162 memcpy(aCopy, aData, nData);
3163 sqlite3ValueSetStr(pVal, nData, (char*)aCopy, enc, sqlite3_free);
3164 return SQLITE_OK;
3168 ** Deserialize a single record from a buffer in memory. See "RECORD FORMAT"
3169 ** for details.
3171 ** When this function is called, *paChange points to the start of the record
3172 ** to deserialize. Assuming no error occurs, *paChange is set to point to
3173 ** one byte after the end of the same record before this function returns.
3174 ** If the argument abPK is NULL, then the record contains nCol values. Or,
3175 ** if abPK is other than NULL, then the record contains only the PK fields
3176 ** (in other words, it is a patchset DELETE record).
3178 ** If successful, each element of the apOut[] array (allocated by the caller)
3179 ** is set to point to an sqlite3_value object containing the value read
3180 ** from the corresponding position in the record. If that value is not
3181 ** included in the record (i.e. because the record is part of an UPDATE change
3182 ** and the field was not modified), the corresponding element of apOut[] is
3183 ** set to NULL.
3185 ** It is the responsibility of the caller to free all sqlite_value structures
3186 ** using sqlite3_free().
3188 ** If an error occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned.
3189 ** The apOut[] array may have been partially populated in this case.
3191 static int sessionReadRecord(
3192 SessionInput *pIn, /* Input data */
3193 int nCol, /* Number of values in record */
3194 u8 *abPK, /* Array of primary key flags, or NULL */
3195 sqlite3_value **apOut, /* Write values to this array */
3196 int *pbEmpty
3198 int i; /* Used to iterate through columns */
3199 int rc = SQLITE_OK;
3201 assert( pbEmpty==0 || *pbEmpty==0 );
3202 if( pbEmpty ) *pbEmpty = 1;
3203 for(i=0; i<nCol && rc==SQLITE_OK; i++){
3204 int eType = 0; /* Type of value (SQLITE_NULL, TEXT etc.) */
3205 if( abPK && abPK[i]==0 ) continue;
3206 rc = sessionInputBuffer(pIn, 9);
3207 if( rc==SQLITE_OK ){
3208 if( pIn->iNext>=pIn->nData ){
3209 rc = SQLITE_CORRUPT_BKPT;
3210 }else{
3211 eType = pIn->aData[pIn->iNext++];
3212 assert( apOut[i]==0 );
3213 if( eType ){
3214 if( pbEmpty ) *pbEmpty = 0;
3215 apOut[i] = sqlite3ValueNew(0);
3216 if( !apOut[i] ) rc = SQLITE_NOMEM;
3221 if( rc==SQLITE_OK ){
3222 u8 *aVal = &pIn->aData[pIn->iNext];
3223 if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
3224 int nByte;
3225 pIn->iNext += sessionVarintGet(aVal, &nByte);
3226 rc = sessionInputBuffer(pIn, nByte);
3227 if( rc==SQLITE_OK ){
3228 if( nByte<0 || nByte>pIn->nData-pIn->iNext ){
3229 rc = SQLITE_CORRUPT_BKPT;
3230 }else{
3231 u8 enc = (eType==SQLITE_TEXT ? SQLITE_UTF8 : 0);
3232 rc = sessionValueSetStr(apOut[i],&pIn->aData[pIn->iNext],nByte,enc);
3233 pIn->iNext += nByte;
3237 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
3238 sqlite3_int64 v = sessionGetI64(aVal);
3239 if( eType==SQLITE_INTEGER ){
3240 sqlite3VdbeMemSetInt64(apOut[i], v);
3241 }else{
3242 double d;
3243 memcpy(&d, &v, 8);
3244 sqlite3VdbeMemSetDouble(apOut[i], d);
3246 pIn->iNext += 8;
3251 return rc;
3255 ** The input pointer currently points to the second byte of a table-header.
3256 ** Specifically, to the following:
3258 ** + number of columns in table (varint)
3259 ** + array of PK flags (1 byte per column),
3260 ** + table name (nul terminated).
3262 ** This function ensures that all of the above is present in the input
3263 ** buffer (i.e. that it can be accessed without any calls to xInput()).
3264 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
3265 ** The input pointer is not moved.
3267 static int sessionChangesetBufferTblhdr(SessionInput *pIn, int *pnByte){
3268 int rc = SQLITE_OK;
3269 int nCol = 0;
3270 int nRead = 0;
3272 rc = sessionInputBuffer(pIn, 9);
3273 if( rc==SQLITE_OK ){
3274 nRead += sessionVarintGet(&pIn->aData[pIn->iNext + nRead], &nCol);
3275 /* The hard upper limit for the number of columns in an SQLite
3276 ** database table is, according to sqliteLimit.h, 32676. So
3277 ** consider any table-header that purports to have more than 65536
3278 ** columns to be corrupt. This is convenient because otherwise,
3279 ** if the (nCol>65536) condition below were omitted, a sufficiently
3280 ** large value for nCol may cause nRead to wrap around and become
3281 ** negative. Leading to a crash. */
3282 if( nCol<0 || nCol>65536 ){
3283 rc = SQLITE_CORRUPT_BKPT;
3284 }else{
3285 rc = sessionInputBuffer(pIn, nRead+nCol+100);
3286 nRead += nCol;
3290 while( rc==SQLITE_OK ){
3291 while( (pIn->iNext + nRead)<pIn->nData && pIn->aData[pIn->iNext + nRead] ){
3292 nRead++;
3294 if( (pIn->iNext + nRead)<pIn->nData ) break;
3295 rc = sessionInputBuffer(pIn, nRead + 100);
3297 *pnByte = nRead+1;
3298 return rc;
3302 ** The input pointer currently points to the first byte of the first field
3303 ** of a record consisting of nCol columns. This function ensures the entire
3304 ** record is buffered. It does not move the input pointer.
3306 ** If successful, SQLITE_OK is returned and *pnByte is set to the size of
3307 ** the record in bytes. Otherwise, an SQLite error code is returned. The
3308 ** final value of *pnByte is undefined in this case.
3310 static int sessionChangesetBufferRecord(
3311 SessionInput *pIn, /* Input data */
3312 int nCol, /* Number of columns in record */
3313 int *pnByte /* OUT: Size of record in bytes */
3315 int rc = SQLITE_OK;
3316 int nByte = 0;
3317 int i;
3318 for(i=0; rc==SQLITE_OK && i<nCol; i++){
3319 int eType;
3320 rc = sessionInputBuffer(pIn, nByte + 10);
3321 if( rc==SQLITE_OK ){
3322 eType = pIn->aData[pIn->iNext + nByte++];
3323 if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
3324 int n;
3325 nByte += sessionVarintGet(&pIn->aData[pIn->iNext+nByte], &n);
3326 nByte += n;
3327 rc = sessionInputBuffer(pIn, nByte);
3328 }else if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
3329 nByte += 8;
3333 *pnByte = nByte;
3334 return rc;
3338 ** The input pointer currently points to the second byte of a table-header.
3339 ** Specifically, to the following:
3341 ** + number of columns in table (varint)
3342 ** + array of PK flags (1 byte per column),
3343 ** + table name (nul terminated).
3345 ** This function decodes the table-header and populates the p->nCol,
3346 ** p->zTab and p->abPK[] variables accordingly. The p->apValue[] array is
3347 ** also allocated or resized according to the new value of p->nCol. The
3348 ** input pointer is left pointing to the byte following the table header.
3350 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code
3351 ** is returned and the final values of the various fields enumerated above
3352 ** are undefined.
3354 static int sessionChangesetReadTblhdr(sqlite3_changeset_iter *p){
3355 int rc;
3356 int nCopy;
3357 assert( p->rc==SQLITE_OK );
3359 rc = sessionChangesetBufferTblhdr(&p->in, &nCopy);
3360 if( rc==SQLITE_OK ){
3361 int nByte;
3362 int nVarint;
3363 nVarint = sessionVarintGet(&p->in.aData[p->in.iNext], &p->nCol);
3364 if( p->nCol>0 ){
3365 nCopy -= nVarint;
3366 p->in.iNext += nVarint;
3367 nByte = p->nCol * sizeof(sqlite3_value*) * 2 + nCopy;
3368 p->tblhdr.nBuf = 0;
3369 sessionBufferGrow(&p->tblhdr, nByte, &rc);
3370 }else{
3371 rc = SQLITE_CORRUPT_BKPT;
3375 if( rc==SQLITE_OK ){
3376 size_t iPK = sizeof(sqlite3_value*)*p->nCol*2;
3377 memset(p->tblhdr.aBuf, 0, iPK);
3378 memcpy(&p->tblhdr.aBuf[iPK], &p->in.aData[p->in.iNext], nCopy);
3379 p->in.iNext += nCopy;
3382 p->apValue = (sqlite3_value**)p->tblhdr.aBuf;
3383 if( p->apValue==0 ){
3384 p->abPK = 0;
3385 p->zTab = 0;
3386 }else{
3387 p->abPK = (u8*)&p->apValue[p->nCol*2];
3388 p->zTab = p->abPK ? (char*)&p->abPK[p->nCol] : 0;
3390 return (p->rc = rc);
3394 ** Advance the changeset iterator to the next change. The differences between
3395 ** this function and sessionChangesetNext() are that
3397 ** * If pbEmpty is not NULL and the change is a no-op UPDATE (an UPDATE
3398 ** that modifies no columns), this function sets (*pbEmpty) to 1.
3400 ** * If the iterator is configured to skip no-op UPDATEs,
3401 ** sessionChangesetNext() does that. This function does not.
3403 static int sessionChangesetNextOne(
3404 sqlite3_changeset_iter *p, /* Changeset iterator */
3405 u8 **paRec, /* If non-NULL, store record pointer here */
3406 int *pnRec, /* If non-NULL, store size of record here */
3407 int *pbNew, /* If non-NULL, true if new table */
3408 int *pbEmpty
3410 int i;
3411 u8 op;
3413 assert( (paRec==0 && pnRec==0) || (paRec && pnRec) );
3414 assert( pbEmpty==0 || *pbEmpty==0 );
3416 /* If the iterator is in the error-state, return immediately. */
3417 if( p->rc!=SQLITE_OK ) return p->rc;
3419 /* Free the current contents of p->apValue[], if any. */
3420 if( p->apValue ){
3421 for(i=0; i<p->nCol*2; i++){
3422 sqlite3ValueFree(p->apValue[i]);
3424 memset(p->apValue, 0, sizeof(sqlite3_value*)*p->nCol*2);
3427 /* Make sure the buffer contains at least 10 bytes of input data, or all
3428 ** remaining data if there are less than 10 bytes available. This is
3429 ** sufficient either for the 'T' or 'P' byte and the varint that follows
3430 ** it, or for the two single byte values otherwise. */
3431 p->rc = sessionInputBuffer(&p->in, 2);
3432 if( p->rc!=SQLITE_OK ) return p->rc;
3434 /* If the iterator is already at the end of the changeset, return DONE. */
3435 if( p->in.iNext>=p->in.nData ){
3436 return SQLITE_DONE;
3439 sessionDiscardData(&p->in);
3440 p->in.iCurrent = p->in.iNext;
3442 op = p->in.aData[p->in.iNext++];
3443 while( op=='T' || op=='P' ){
3444 if( pbNew ) *pbNew = 1;
3445 p->bPatchset = (op=='P');
3446 if( sessionChangesetReadTblhdr(p) ) return p->rc;
3447 if( (p->rc = sessionInputBuffer(&p->in, 2)) ) return p->rc;
3448 p->in.iCurrent = p->in.iNext;
3449 if( p->in.iNext>=p->in.nData ) return SQLITE_DONE;
3450 op = p->in.aData[p->in.iNext++];
3453 if( p->zTab==0 || (p->bPatchset && p->bInvert) ){
3454 /* The first record in the changeset is not a table header. Must be a
3455 ** corrupt changeset. */
3456 assert( p->in.iNext==1 || p->zTab );
3457 return (p->rc = SQLITE_CORRUPT_BKPT);
3460 p->op = op;
3461 p->bIndirect = p->in.aData[p->in.iNext++];
3462 if( p->op!=SQLITE_UPDATE && p->op!=SQLITE_DELETE && p->op!=SQLITE_INSERT ){
3463 return (p->rc = SQLITE_CORRUPT_BKPT);
3466 if( paRec ){
3467 int nVal; /* Number of values to buffer */
3468 if( p->bPatchset==0 && op==SQLITE_UPDATE ){
3469 nVal = p->nCol * 2;
3470 }else if( p->bPatchset && op==SQLITE_DELETE ){
3471 nVal = 0;
3472 for(i=0; i<p->nCol; i++) if( p->abPK[i] ) nVal++;
3473 }else{
3474 nVal = p->nCol;
3476 p->rc = sessionChangesetBufferRecord(&p->in, nVal, pnRec);
3477 if( p->rc!=SQLITE_OK ) return p->rc;
3478 *paRec = &p->in.aData[p->in.iNext];
3479 p->in.iNext += *pnRec;
3480 }else{
3481 sqlite3_value **apOld = (p->bInvert ? &p->apValue[p->nCol] : p->apValue);
3482 sqlite3_value **apNew = (p->bInvert ? p->apValue : &p->apValue[p->nCol]);
3484 /* If this is an UPDATE or DELETE, read the old.* record. */
3485 if( p->op!=SQLITE_INSERT && (p->bPatchset==0 || p->op==SQLITE_DELETE) ){
3486 u8 *abPK = p->bPatchset ? p->abPK : 0;
3487 p->rc = sessionReadRecord(&p->in, p->nCol, abPK, apOld, 0);
3488 if( p->rc!=SQLITE_OK ) return p->rc;
3491 /* If this is an INSERT or UPDATE, read the new.* record. */
3492 if( p->op!=SQLITE_DELETE ){
3493 p->rc = sessionReadRecord(&p->in, p->nCol, 0, apNew, pbEmpty);
3494 if( p->rc!=SQLITE_OK ) return p->rc;
3497 if( (p->bPatchset || p->bInvert) && p->op==SQLITE_UPDATE ){
3498 /* If this is an UPDATE that is part of a patchset, then all PK and
3499 ** modified fields are present in the new.* record. The old.* record
3500 ** is currently completely empty. This block shifts the PK fields from
3501 ** new.* to old.*, to accommodate the code that reads these arrays. */
3502 for(i=0; i<p->nCol; i++){
3503 assert( p->bPatchset==0 || p->apValue[i]==0 );
3504 if( p->abPK[i] ){
3505 assert( p->apValue[i]==0 );
3506 p->apValue[i] = p->apValue[i+p->nCol];
3507 if( p->apValue[i]==0 ) return (p->rc = SQLITE_CORRUPT_BKPT);
3508 p->apValue[i+p->nCol] = 0;
3511 }else if( p->bInvert ){
3512 if( p->op==SQLITE_INSERT ) p->op = SQLITE_DELETE;
3513 else if( p->op==SQLITE_DELETE ) p->op = SQLITE_INSERT;
3516 /* If this is an UPDATE that is part of a changeset, then check that
3517 ** there are no fields in the old.* record that are not (a) PK fields,
3518 ** or (b) also present in the new.* record.
3520 ** Such records are technically corrupt, but the rebaser was at one
3521 ** point generating them. Under most circumstances this is benign, but
3522 ** can cause spurious SQLITE_RANGE errors when applying the changeset. */
3523 if( p->bPatchset==0 && p->op==SQLITE_UPDATE){
3524 for(i=0; i<p->nCol; i++){
3525 if( p->abPK[i]==0 && p->apValue[i+p->nCol]==0 ){
3526 sqlite3ValueFree(p->apValue[i]);
3527 p->apValue[i] = 0;
3533 return SQLITE_ROW;
3537 ** Advance the changeset iterator to the next change.
3539 ** If both paRec and pnRec are NULL, then this function works like the public
3540 ** API sqlite3changeset_next(). If SQLITE_ROW is returned, then the
3541 ** sqlite3changeset_new() and old() APIs may be used to query for values.
3543 ** Otherwise, if paRec and pnRec are not NULL, then a pointer to the change
3544 ** record is written to *paRec before returning and the number of bytes in
3545 ** the record to *pnRec.
3547 ** Either way, this function returns SQLITE_ROW if the iterator is
3548 ** successfully advanced to the next change in the changeset, an SQLite
3549 ** error code if an error occurs, or SQLITE_DONE if there are no further
3550 ** changes in the changeset.
3552 static int sessionChangesetNext(
3553 sqlite3_changeset_iter *p, /* Changeset iterator */
3554 u8 **paRec, /* If non-NULL, store record pointer here */
3555 int *pnRec, /* If non-NULL, store size of record here */
3556 int *pbNew /* If non-NULL, true if new table */
3558 int bEmpty;
3559 int rc;
3560 do {
3561 bEmpty = 0;
3562 rc = sessionChangesetNextOne(p, paRec, pnRec, pbNew, &bEmpty);
3563 }while( rc==SQLITE_ROW && p->bSkipEmpty && bEmpty);
3564 return rc;
3568 ** Advance an iterator created by sqlite3changeset_start() to the next
3569 ** change in the changeset. This function may return SQLITE_ROW, SQLITE_DONE
3570 ** or SQLITE_CORRUPT.
3572 ** This function may not be called on iterators passed to a conflict handler
3573 ** callback by changeset_apply().
3575 int sqlite3changeset_next(sqlite3_changeset_iter *p){
3576 return sessionChangesetNext(p, 0, 0, 0);
3580 ** The following function extracts information on the current change
3581 ** from a changeset iterator. It may only be called after changeset_next()
3582 ** has returned SQLITE_ROW.
3584 int sqlite3changeset_op(
3585 sqlite3_changeset_iter *pIter, /* Iterator handle */
3586 const char **pzTab, /* OUT: Pointer to table name */
3587 int *pnCol, /* OUT: Number of columns in table */
3588 int *pOp, /* OUT: SQLITE_INSERT, DELETE or UPDATE */
3589 int *pbIndirect /* OUT: True if change is indirect */
3591 *pOp = pIter->op;
3592 *pnCol = pIter->nCol;
3593 *pzTab = pIter->zTab;
3594 if( pbIndirect ) *pbIndirect = pIter->bIndirect;
3595 return SQLITE_OK;
3599 ** Return information regarding the PRIMARY KEY and number of columns in
3600 ** the database table affected by the change that pIter currently points
3601 ** to. This function may only be called after changeset_next() returns
3602 ** SQLITE_ROW.
3604 int sqlite3changeset_pk(
3605 sqlite3_changeset_iter *pIter, /* Iterator object */
3606 unsigned char **pabPK, /* OUT: Array of boolean - true for PK cols */
3607 int *pnCol /* OUT: Number of entries in output array */
3609 *pabPK = pIter->abPK;
3610 if( pnCol ) *pnCol = pIter->nCol;
3611 return SQLITE_OK;
3615 ** This function may only be called while the iterator is pointing to an
3616 ** SQLITE_UPDATE or SQLITE_DELETE change (see sqlite3changeset_op()).
3617 ** Otherwise, SQLITE_MISUSE is returned.
3619 ** It sets *ppValue to point to an sqlite3_value structure containing the
3620 ** iVal'th value in the old.* record. Or, if that particular value is not
3621 ** included in the record (because the change is an UPDATE and the field
3622 ** was not modified and is not a PK column), set *ppValue to NULL.
3624 ** If value iVal is out-of-range, SQLITE_RANGE is returned and *ppValue is
3625 ** not modified. Otherwise, SQLITE_OK.
3627 int sqlite3changeset_old(
3628 sqlite3_changeset_iter *pIter, /* Changeset iterator */
3629 int iVal, /* Index of old.* value to retrieve */
3630 sqlite3_value **ppValue /* OUT: Old value (or NULL pointer) */
3632 if( pIter->op!=SQLITE_UPDATE && pIter->op!=SQLITE_DELETE ){
3633 return SQLITE_MISUSE;
3635 if( iVal<0 || iVal>=pIter->nCol ){
3636 return SQLITE_RANGE;
3638 *ppValue = pIter->apValue[iVal];
3639 return SQLITE_OK;
3643 ** This function may only be called while the iterator is pointing to an
3644 ** SQLITE_UPDATE or SQLITE_INSERT change (see sqlite3changeset_op()).
3645 ** Otherwise, SQLITE_MISUSE is returned.
3647 ** It sets *ppValue to point to an sqlite3_value structure containing the
3648 ** iVal'th value in the new.* record. Or, if that particular value is not
3649 ** included in the record (because the change is an UPDATE and the field
3650 ** was not modified), set *ppValue to NULL.
3652 ** If value iVal is out-of-range, SQLITE_RANGE is returned and *ppValue is
3653 ** not modified. Otherwise, SQLITE_OK.
3655 int sqlite3changeset_new(
3656 sqlite3_changeset_iter *pIter, /* Changeset iterator */
3657 int iVal, /* Index of new.* value to retrieve */
3658 sqlite3_value **ppValue /* OUT: New value (or NULL pointer) */
3660 if( pIter->op!=SQLITE_UPDATE && pIter->op!=SQLITE_INSERT ){
3661 return SQLITE_MISUSE;
3663 if( iVal<0 || iVal>=pIter->nCol ){
3664 return SQLITE_RANGE;
3666 *ppValue = pIter->apValue[pIter->nCol+iVal];
3667 return SQLITE_OK;
3671 ** The following two macros are used internally. They are similar to the
3672 ** sqlite3changeset_new() and sqlite3changeset_old() functions, except that
3673 ** they omit all error checking and return a pointer to the requested value.
3675 #define sessionChangesetNew(pIter, iVal) (pIter)->apValue[(pIter)->nCol+(iVal)]
3676 #define sessionChangesetOld(pIter, iVal) (pIter)->apValue[(iVal)]
3679 ** This function may only be called with a changeset iterator that has been
3680 ** passed to an SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT
3681 ** conflict-handler function. Otherwise, SQLITE_MISUSE is returned.
3683 ** If successful, *ppValue is set to point to an sqlite3_value structure
3684 ** containing the iVal'th value of the conflicting record.
3686 ** If value iVal is out-of-range or some other error occurs, an SQLite error
3687 ** code is returned. Otherwise, SQLITE_OK.
3689 int sqlite3changeset_conflict(
3690 sqlite3_changeset_iter *pIter, /* Changeset iterator */
3691 int iVal, /* Index of conflict record value to fetch */
3692 sqlite3_value **ppValue /* OUT: Value from conflicting row */
3694 if( !pIter->pConflict ){
3695 return SQLITE_MISUSE;
3697 if( iVal<0 || iVal>=pIter->nCol ){
3698 return SQLITE_RANGE;
3700 *ppValue = sqlite3_column_value(pIter->pConflict, iVal);
3701 return SQLITE_OK;
3705 ** This function may only be called with an iterator passed to an
3706 ** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case
3707 ** it sets the output variable to the total number of known foreign key
3708 ** violations in the destination database and returns SQLITE_OK.
3710 ** In all other cases this function returns SQLITE_MISUSE.
3712 int sqlite3changeset_fk_conflicts(
3713 sqlite3_changeset_iter *pIter, /* Changeset iterator */
3714 int *pnOut /* OUT: Number of FK violations */
3716 if( pIter->pConflict || pIter->apValue ){
3717 return SQLITE_MISUSE;
3719 *pnOut = pIter->nCol;
3720 return SQLITE_OK;
3725 ** Finalize an iterator allocated with sqlite3changeset_start().
3727 ** This function may not be called on iterators passed to a conflict handler
3728 ** callback by changeset_apply().
3730 int sqlite3changeset_finalize(sqlite3_changeset_iter *p){
3731 int rc = SQLITE_OK;
3732 if( p ){
3733 int i; /* Used to iterate through p->apValue[] */
3734 rc = p->rc;
3735 if( p->apValue ){
3736 for(i=0; i<p->nCol*2; i++) sqlite3ValueFree(p->apValue[i]);
3738 sqlite3_free(p->tblhdr.aBuf);
3739 sqlite3_free(p->in.buf.aBuf);
3740 sqlite3_free(p);
3742 return rc;
3745 static int sessionChangesetInvert(
3746 SessionInput *pInput, /* Input changeset */
3747 int (*xOutput)(void *pOut, const void *pData, int nData),
3748 void *pOut,
3749 int *pnInverted, /* OUT: Number of bytes in output changeset */
3750 void **ppInverted /* OUT: Inverse of pChangeset */
3752 int rc = SQLITE_OK; /* Return value */
3753 SessionBuffer sOut; /* Output buffer */
3754 int nCol = 0; /* Number of cols in current table */
3755 u8 *abPK = 0; /* PK array for current table */
3756 sqlite3_value **apVal = 0; /* Space for values for UPDATE inversion */
3757 SessionBuffer sPK = {0, 0, 0}; /* PK array for current table */
3759 /* Initialize the output buffer */
3760 memset(&sOut, 0, sizeof(SessionBuffer));
3762 /* Zero the output variables in case an error occurs. */
3763 if( ppInverted ){
3764 *ppInverted = 0;
3765 *pnInverted = 0;
3768 while( 1 ){
3769 u8 eType;
3771 /* Test for EOF. */
3772 if( (rc = sessionInputBuffer(pInput, 2)) ) goto finished_invert;
3773 if( pInput->iNext>=pInput->nData ) break;
3774 eType = pInput->aData[pInput->iNext];
3776 switch( eType ){
3777 case 'T': {
3778 /* A 'table' record consists of:
3780 ** * A constant 'T' character,
3781 ** * Number of columns in said table (a varint),
3782 ** * An array of nCol bytes (sPK),
3783 ** * A nul-terminated table name.
3785 int nByte;
3786 int nVar;
3787 pInput->iNext++;
3788 if( (rc = sessionChangesetBufferTblhdr(pInput, &nByte)) ){
3789 goto finished_invert;
3791 nVar = sessionVarintGet(&pInput->aData[pInput->iNext], &nCol);
3792 sPK.nBuf = 0;
3793 sessionAppendBlob(&sPK, &pInput->aData[pInput->iNext+nVar], nCol, &rc);
3794 sessionAppendByte(&sOut, eType, &rc);
3795 sessionAppendBlob(&sOut, &pInput->aData[pInput->iNext], nByte, &rc);
3796 if( rc ) goto finished_invert;
3798 pInput->iNext += nByte;
3799 sqlite3_free(apVal);
3800 apVal = 0;
3801 abPK = sPK.aBuf;
3802 break;
3805 case SQLITE_INSERT:
3806 case SQLITE_DELETE: {
3807 int nByte;
3808 int bIndirect = pInput->aData[pInput->iNext+1];
3809 int eType2 = (eType==SQLITE_DELETE ? SQLITE_INSERT : SQLITE_DELETE);
3810 pInput->iNext += 2;
3811 assert( rc==SQLITE_OK );
3812 rc = sessionChangesetBufferRecord(pInput, nCol, &nByte);
3813 sessionAppendByte(&sOut, eType2, &rc);
3814 sessionAppendByte(&sOut, bIndirect, &rc);
3815 sessionAppendBlob(&sOut, &pInput->aData[pInput->iNext], nByte, &rc);
3816 pInput->iNext += nByte;
3817 if( rc ) goto finished_invert;
3818 break;
3821 case SQLITE_UPDATE: {
3822 int iCol;
3824 if( 0==apVal ){
3825 apVal = (sqlite3_value **)sqlite3_malloc64(sizeof(apVal[0])*nCol*2);
3826 if( 0==apVal ){
3827 rc = SQLITE_NOMEM;
3828 goto finished_invert;
3830 memset(apVal, 0, sizeof(apVal[0])*nCol*2);
3833 /* Write the header for the new UPDATE change. Same as the original. */
3834 sessionAppendByte(&sOut, eType, &rc);
3835 sessionAppendByte(&sOut, pInput->aData[pInput->iNext+1], &rc);
3837 /* Read the old.* and new.* records for the update change. */
3838 pInput->iNext += 2;
3839 rc = sessionReadRecord(pInput, nCol, 0, &apVal[0], 0);
3840 if( rc==SQLITE_OK ){
3841 rc = sessionReadRecord(pInput, nCol, 0, &apVal[nCol], 0);
3844 /* Write the new old.* record. Consists of the PK columns from the
3845 ** original old.* record, and the other values from the original
3846 ** new.* record. */
3847 for(iCol=0; iCol<nCol; iCol++){
3848 sqlite3_value *pVal = apVal[iCol + (abPK[iCol] ? 0 : nCol)];
3849 sessionAppendValue(&sOut, pVal, &rc);
3852 /* Write the new new.* record. Consists of a copy of all values
3853 ** from the original old.* record, except for the PK columns, which
3854 ** are set to "undefined". */
3855 for(iCol=0; iCol<nCol; iCol++){
3856 sqlite3_value *pVal = (abPK[iCol] ? 0 : apVal[iCol]);
3857 sessionAppendValue(&sOut, pVal, &rc);
3860 for(iCol=0; iCol<nCol*2; iCol++){
3861 sqlite3ValueFree(apVal[iCol]);
3863 memset(apVal, 0, sizeof(apVal[0])*nCol*2);
3864 if( rc!=SQLITE_OK ){
3865 goto finished_invert;
3868 break;
3871 default:
3872 rc = SQLITE_CORRUPT_BKPT;
3873 goto finished_invert;
3876 assert( rc==SQLITE_OK );
3877 if( xOutput && sOut.nBuf>=sessions_strm_chunk_size ){
3878 rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
3879 sOut.nBuf = 0;
3880 if( rc!=SQLITE_OK ) goto finished_invert;
3884 assert( rc==SQLITE_OK );
3885 if( pnInverted && ALWAYS(ppInverted) ){
3886 *pnInverted = sOut.nBuf;
3887 *ppInverted = sOut.aBuf;
3888 sOut.aBuf = 0;
3889 }else if( sOut.nBuf>0 && ALWAYS(xOutput!=0) ){
3890 rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
3893 finished_invert:
3894 sqlite3_free(sOut.aBuf);
3895 sqlite3_free(apVal);
3896 sqlite3_free(sPK.aBuf);
3897 return rc;
3902 ** Invert a changeset object.
3904 int sqlite3changeset_invert(
3905 int nChangeset, /* Number of bytes in input */
3906 const void *pChangeset, /* Input changeset */
3907 int *pnInverted, /* OUT: Number of bytes in output changeset */
3908 void **ppInverted /* OUT: Inverse of pChangeset */
3910 SessionInput sInput;
3912 /* Set up the input stream */
3913 memset(&sInput, 0, sizeof(SessionInput));
3914 sInput.nData = nChangeset;
3915 sInput.aData = (u8*)pChangeset;
3917 return sessionChangesetInvert(&sInput, 0, 0, pnInverted, ppInverted);
3921 ** Streaming version of sqlite3changeset_invert().
3923 int sqlite3changeset_invert_strm(
3924 int (*xInput)(void *pIn, void *pData, int *pnData),
3925 void *pIn,
3926 int (*xOutput)(void *pOut, const void *pData, int nData),
3927 void *pOut
3929 SessionInput sInput;
3930 int rc;
3932 /* Set up the input stream */
3933 memset(&sInput, 0, sizeof(SessionInput));
3934 sInput.xInput = xInput;
3935 sInput.pIn = pIn;
3937 rc = sessionChangesetInvert(&sInput, xOutput, pOut, 0, 0);
3938 sqlite3_free(sInput.buf.aBuf);
3939 return rc;
3943 typedef struct SessionUpdate SessionUpdate;
3944 struct SessionUpdate {
3945 sqlite3_stmt *pStmt;
3946 u32 *aMask;
3947 SessionUpdate *pNext;
3950 typedef struct SessionApplyCtx SessionApplyCtx;
3951 struct SessionApplyCtx {
3952 sqlite3 *db;
3953 sqlite3_stmt *pDelete; /* DELETE statement */
3954 sqlite3_stmt *pInsert; /* INSERT statement */
3955 sqlite3_stmt *pSelect; /* SELECT statement */
3956 int nCol; /* Size of azCol[] and abPK[] arrays */
3957 const char **azCol; /* Array of column names */
3958 u8 *abPK; /* Boolean array - true if column is in PK */
3959 u32 *aUpdateMask; /* Used by sessionUpdateFind */
3960 SessionUpdate *pUp;
3961 int bStat1; /* True if table is sqlite_stat1 */
3962 int bDeferConstraints; /* True to defer constraints */
3963 int bInvertConstraints; /* Invert when iterating constraints buffer */
3964 SessionBuffer constraints; /* Deferred constraints are stored here */
3965 SessionBuffer rebase; /* Rebase information (if any) here */
3966 u8 bRebaseStarted; /* If table header is already in rebase */
3967 u8 bRebase; /* True to collect rebase information */
3968 u8 bIgnoreNoop; /* True to ignore no-op conflicts */
3969 int bRowid;
3972 /* Number of prepared UPDATE statements to cache. */
3973 #define SESSION_UPDATE_CACHE_SZ 12
3976 ** Find a prepared UPDATE statement suitable for the UPDATE step currently
3977 ** being visited by the iterator. The UPDATE is of the form:
3979 ** UPDATE tbl SET col = ?, col2 = ? WHERE pk1 IS ? AND pk2 IS ?
3981 static int sessionUpdateFind(
3982 sqlite3_changeset_iter *pIter,
3983 SessionApplyCtx *p,
3984 int bPatchset,
3985 sqlite3_stmt **ppStmt
3987 int rc = SQLITE_OK;
3988 SessionUpdate *pUp = 0;
3989 int nCol = pIter->nCol;
3990 int nU32 = (pIter->nCol+33)/32;
3991 int ii;
3993 if( p->aUpdateMask==0 ){
3994 p->aUpdateMask = sqlite3_malloc(nU32*sizeof(u32));
3995 if( p->aUpdateMask==0 ){
3996 rc = SQLITE_NOMEM;
4000 if( rc==SQLITE_OK ){
4001 memset(p->aUpdateMask, 0, nU32*sizeof(u32));
4002 rc = SQLITE_CORRUPT;
4003 for(ii=0; ii<pIter->nCol; ii++){
4004 if( sessionChangesetNew(pIter, ii) ){
4005 p->aUpdateMask[ii/32] |= (1<<(ii%32));
4006 rc = SQLITE_OK;
4011 if( rc==SQLITE_OK ){
4012 if( bPatchset ) p->aUpdateMask[nCol/32] |= (1<<(nCol%32));
4014 if( p->pUp ){
4015 int nUp = 0;
4016 SessionUpdate **pp = &p->pUp;
4017 while( 1 ){
4018 nUp++;
4019 if( 0==memcmp(p->aUpdateMask, (*pp)->aMask, nU32*sizeof(u32)) ){
4020 pUp = *pp;
4021 *pp = pUp->pNext;
4022 pUp->pNext = p->pUp;
4023 p->pUp = pUp;
4024 break;
4027 if( (*pp)->pNext ){
4028 pp = &(*pp)->pNext;
4029 }else{
4030 if( nUp>=SESSION_UPDATE_CACHE_SZ ){
4031 sqlite3_finalize((*pp)->pStmt);
4032 sqlite3_free(*pp);
4033 *pp = 0;
4035 break;
4040 if( pUp==0 ){
4041 int nByte = sizeof(SessionUpdate) * nU32*sizeof(u32);
4042 int bStat1 = (sqlite3_stricmp(pIter->zTab, "sqlite_stat1")==0);
4043 pUp = (SessionUpdate*)sqlite3_malloc(nByte);
4044 if( pUp==0 ){
4045 rc = SQLITE_NOMEM;
4046 }else{
4047 const char *zSep = "";
4048 SessionBuffer buf;
4050 memset(&buf, 0, sizeof(buf));
4051 pUp->aMask = (u32*)&pUp[1];
4052 memcpy(pUp->aMask, p->aUpdateMask, nU32*sizeof(u32));
4054 sessionAppendStr(&buf, "UPDATE main.", &rc);
4055 sessionAppendIdent(&buf, pIter->zTab, &rc);
4056 sessionAppendStr(&buf, " SET ", &rc);
4058 /* Create the assignments part of the UPDATE */
4059 for(ii=0; ii<pIter->nCol; ii++){
4060 if( p->abPK[ii]==0 && sessionChangesetNew(pIter, ii) ){
4061 sessionAppendStr(&buf, zSep, &rc);
4062 sessionAppendIdent(&buf, p->azCol[ii], &rc);
4063 sessionAppendStr(&buf, " = ?", &rc);
4064 sessionAppendInteger(&buf, ii*2+1, &rc);
4065 zSep = ", ";
4069 /* Create the WHERE clause part of the UPDATE */
4070 zSep = "";
4071 sessionAppendStr(&buf, " WHERE ", &rc);
4072 for(ii=0; ii<pIter->nCol; ii++){
4073 if( p->abPK[ii] || (bPatchset==0 && sessionChangesetOld(pIter, ii)) ){
4074 sessionAppendStr(&buf, zSep, &rc);
4075 if( bStat1 && ii==1 ){
4076 assert( sqlite3_stricmp(p->azCol[ii], "idx")==0 );
4077 sessionAppendStr(&buf,
4078 "idx IS CASE "
4079 "WHEN length(?4)=0 AND typeof(?4)='blob' THEN NULL "
4080 "ELSE ?4 END ", &rc
4082 }else{
4083 sessionAppendIdent(&buf, p->azCol[ii], &rc);
4084 sessionAppendStr(&buf, " IS ?", &rc);
4085 sessionAppendInteger(&buf, ii*2+2, &rc);
4087 zSep = " AND ";
4091 if( rc==SQLITE_OK ){
4092 char *zSql = (char*)buf.aBuf;
4093 rc = sqlite3_prepare_v2(p->db, zSql, buf.nBuf, &pUp->pStmt, 0);
4096 if( rc!=SQLITE_OK ){
4097 sqlite3_free(pUp);
4098 pUp = 0;
4099 }else{
4100 pUp->pNext = p->pUp;
4101 p->pUp = pUp;
4103 sqlite3_free(buf.aBuf);
4108 assert( (rc==SQLITE_OK)==(pUp!=0) );
4109 if( pUp ){
4110 *ppStmt = pUp->pStmt;
4111 }else{
4112 *ppStmt = 0;
4114 return rc;
4118 ** Free all cached UPDATE statements.
4120 static void sessionUpdateFree(SessionApplyCtx *p){
4121 SessionUpdate *pUp;
4122 SessionUpdate *pNext;
4123 for(pUp=p->pUp; pUp; pUp=pNext){
4124 pNext = pUp->pNext;
4125 sqlite3_finalize(pUp->pStmt);
4126 sqlite3_free(pUp);
4128 p->pUp = 0;
4129 sqlite3_free(p->aUpdateMask);
4130 p->aUpdateMask = 0;
4134 ** Formulate a statement to DELETE a row from database db. Assuming a table
4135 ** structure like this:
4137 ** CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c));
4139 ** The DELETE statement looks like this:
4141 ** DELETE FROM x WHERE a = :1 AND c = :3 AND (:5 OR b IS :2 AND d IS :4)
4143 ** Variable :5 (nCol+1) is a boolean. It should be set to 0 if we require
4144 ** matching b and d values, or 1 otherwise. The second case comes up if the
4145 ** conflict handler is invoked with NOTFOUND and returns CHANGESET_REPLACE.
4147 ** If successful, SQLITE_OK is returned and SessionApplyCtx.pDelete is left
4148 ** pointing to the prepared version of the SQL statement.
4150 static int sessionDeleteRow(
4151 sqlite3 *db, /* Database handle */
4152 const char *zTab, /* Table name */
4153 SessionApplyCtx *p /* Session changeset-apply context */
4155 int i;
4156 const char *zSep = "";
4157 int rc = SQLITE_OK;
4158 SessionBuffer buf = {0, 0, 0};
4159 int nPk = 0;
4161 sessionAppendStr(&buf, "DELETE FROM main.", &rc);
4162 sessionAppendIdent(&buf, zTab, &rc);
4163 sessionAppendStr(&buf, " WHERE ", &rc);
4165 for(i=0; i<p->nCol; i++){
4166 if( p->abPK[i] ){
4167 nPk++;
4168 sessionAppendStr(&buf, zSep, &rc);
4169 sessionAppendIdent(&buf, p->azCol[i], &rc);
4170 sessionAppendStr(&buf, " = ?", &rc);
4171 sessionAppendInteger(&buf, i+1, &rc);
4172 zSep = " AND ";
4176 if( nPk<p->nCol ){
4177 sessionAppendStr(&buf, " AND (?", &rc);
4178 sessionAppendInteger(&buf, p->nCol+1, &rc);
4179 sessionAppendStr(&buf, " OR ", &rc);
4181 zSep = "";
4182 for(i=0; i<p->nCol; i++){
4183 if( !p->abPK[i] ){
4184 sessionAppendStr(&buf, zSep, &rc);
4185 sessionAppendIdent(&buf, p->azCol[i], &rc);
4186 sessionAppendStr(&buf, " IS ?", &rc);
4187 sessionAppendInteger(&buf, i+1, &rc);
4188 zSep = "AND ";
4191 sessionAppendStr(&buf, ")", &rc);
4194 if( rc==SQLITE_OK ){
4195 rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pDelete, 0);
4197 sqlite3_free(buf.aBuf);
4199 return rc;
4203 ** Formulate and prepare an SQL statement to query table zTab by primary
4204 ** key. Assuming the following table structure:
4206 ** CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c));
4208 ** The SELECT statement looks like this:
4210 ** SELECT * FROM x WHERE a = ?1 AND c = ?3
4212 ** If successful, SQLITE_OK is returned and SessionApplyCtx.pSelect is left
4213 ** pointing to the prepared version of the SQL statement.
4215 static int sessionSelectRow(
4216 sqlite3 *db, /* Database handle */
4217 const char *zTab, /* Table name */
4218 SessionApplyCtx *p /* Session changeset-apply context */
4220 /* TODO */
4221 return sessionSelectStmt(db, p->bIgnoreNoop,
4222 "main", zTab, p->bRowid, p->nCol, p->azCol, p->abPK, &p->pSelect
4227 ** Formulate and prepare an INSERT statement to add a record to table zTab.
4228 ** For example:
4230 ** INSERT INTO main."zTab" VALUES(?1, ?2, ?3 ...);
4232 ** If successful, SQLITE_OK is returned and SessionApplyCtx.pInsert is left
4233 ** pointing to the prepared version of the SQL statement.
4235 static int sessionInsertRow(
4236 sqlite3 *db, /* Database handle */
4237 const char *zTab, /* Table name */
4238 SessionApplyCtx *p /* Session changeset-apply context */
4240 int rc = SQLITE_OK;
4241 int i;
4242 SessionBuffer buf = {0, 0, 0};
4244 sessionAppendStr(&buf, "INSERT INTO main.", &rc);
4245 sessionAppendIdent(&buf, zTab, &rc);
4246 sessionAppendStr(&buf, "(", &rc);
4247 for(i=0; i<p->nCol; i++){
4248 if( i!=0 ) sessionAppendStr(&buf, ", ", &rc);
4249 sessionAppendIdent(&buf, p->azCol[i], &rc);
4252 sessionAppendStr(&buf, ") VALUES(?", &rc);
4253 for(i=1; i<p->nCol; i++){
4254 sessionAppendStr(&buf, ", ?", &rc);
4256 sessionAppendStr(&buf, ")", &rc);
4258 if( rc==SQLITE_OK ){
4259 rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pInsert, 0);
4261 sqlite3_free(buf.aBuf);
4262 return rc;
4265 static int sessionPrepare(sqlite3 *db, sqlite3_stmt **pp, const char *zSql){
4266 return sqlite3_prepare_v2(db, zSql, -1, pp, 0);
4270 ** Prepare statements for applying changes to the sqlite_stat1 table.
4271 ** These are similar to those created by sessionSelectRow(),
4272 ** sessionInsertRow(), sessionUpdateRow() and sessionDeleteRow() for
4273 ** other tables.
4275 static int sessionStat1Sql(sqlite3 *db, SessionApplyCtx *p){
4276 int rc = sessionSelectRow(db, "sqlite_stat1", p);
4277 if( rc==SQLITE_OK ){
4278 rc = sessionPrepare(db, &p->pInsert,
4279 "INSERT INTO main.sqlite_stat1 VALUES(?1, "
4280 "CASE WHEN length(?2)=0 AND typeof(?2)='blob' THEN NULL ELSE ?2 END, "
4281 "?3)"
4284 if( rc==SQLITE_OK ){
4285 rc = sessionPrepare(db, &p->pDelete,
4286 "DELETE FROM main.sqlite_stat1 WHERE tbl=?1 AND idx IS "
4287 "CASE WHEN length(?2)=0 AND typeof(?2)='blob' THEN NULL ELSE ?2 END "
4288 "AND (?4 OR stat IS ?3)"
4291 return rc;
4295 ** A wrapper around sqlite3_bind_value() that detects an extra problem.
4296 ** See comments in the body of this function for details.
4298 static int sessionBindValue(
4299 sqlite3_stmt *pStmt, /* Statement to bind value to */
4300 int i, /* Parameter number to bind to */
4301 sqlite3_value *pVal /* Value to bind */
4303 int eType = sqlite3_value_type(pVal);
4304 /* COVERAGE: The (pVal->z==0) branch is never true using current versions
4305 ** of SQLite. If a malloc fails in an sqlite3_value_xxx() function, either
4306 ** the (pVal->z) variable remains as it was or the type of the value is
4307 ** set to SQLITE_NULL. */
4308 if( (eType==SQLITE_TEXT || eType==SQLITE_BLOB) && pVal->z==0 ){
4309 /* This condition occurs when an earlier OOM in a call to
4310 ** sqlite3_value_text() or sqlite3_value_blob() (perhaps from within
4311 ** a conflict-handler) has zeroed the pVal->z pointer. Return NOMEM. */
4312 return SQLITE_NOMEM;
4314 return sqlite3_bind_value(pStmt, i, pVal);
4318 ** Iterator pIter must point to an SQLITE_INSERT entry. This function
4319 ** transfers new.* values from the current iterator entry to statement
4320 ** pStmt. The table being inserted into has nCol columns.
4322 ** New.* value $i from the iterator is bound to variable ($i+1) of
4323 ** statement pStmt. If parameter abPK is NULL, all values from 0 to (nCol-1)
4324 ** are transfered to the statement. Otherwise, if abPK is not NULL, it points
4325 ** to an array nCol elements in size. In this case only those values for
4326 ** which abPK[$i] is true are read from the iterator and bound to the
4327 ** statement.
4329 ** An SQLite error code is returned if an error occurs. Otherwise, SQLITE_OK.
4331 static int sessionBindRow(
4332 sqlite3_changeset_iter *pIter, /* Iterator to read values from */
4333 int(*xValue)(sqlite3_changeset_iter *, int, sqlite3_value **),
4334 int nCol, /* Number of columns */
4335 u8 *abPK, /* If not NULL, bind only if true */
4336 sqlite3_stmt *pStmt /* Bind values to this statement */
4338 int i;
4339 int rc = SQLITE_OK;
4341 /* Neither sqlite3changeset_old or sqlite3changeset_new can fail if the
4342 ** argument iterator points to a suitable entry. Make sure that xValue
4343 ** is one of these to guarantee that it is safe to ignore the return
4344 ** in the code below. */
4345 assert( xValue==sqlite3changeset_old || xValue==sqlite3changeset_new );
4347 for(i=0; rc==SQLITE_OK && i<nCol; i++){
4348 if( !abPK || abPK[i] ){
4349 sqlite3_value *pVal = 0;
4350 (void)xValue(pIter, i, &pVal);
4351 if( pVal==0 ){
4352 /* The value in the changeset was "undefined". This indicates a
4353 ** corrupt changeset blob. */
4354 rc = SQLITE_CORRUPT_BKPT;
4355 }else{
4356 rc = sessionBindValue(pStmt, i+1, pVal);
4360 return rc;
4364 ** SQL statement pSelect is as generated by the sessionSelectRow() function.
4365 ** This function binds the primary key values from the change that changeset
4366 ** iterator pIter points to to the SELECT and attempts to seek to the table
4367 ** entry. If a row is found, the SELECT statement left pointing at the row
4368 ** and SQLITE_ROW is returned. Otherwise, if no row is found and no error
4369 ** has occured, the statement is reset and SQLITE_OK is returned. If an
4370 ** error occurs, the statement is reset and an SQLite error code is returned.
4372 ** If this function returns SQLITE_ROW, the caller must eventually reset()
4373 ** statement pSelect. If any other value is returned, the statement does
4374 ** not require a reset().
4376 ** If the iterator currently points to an INSERT record, bind values from the
4377 ** new.* record to the SELECT statement. Or, if it points to a DELETE or
4378 ** UPDATE, bind values from the old.* record.
4380 static int sessionSeekToRow(
4381 sqlite3_changeset_iter *pIter, /* Changeset iterator */
4382 SessionApplyCtx *p
4384 sqlite3_stmt *pSelect = p->pSelect;
4385 int rc; /* Return code */
4386 int nCol; /* Number of columns in table */
4387 int op; /* Changset operation (SQLITE_UPDATE etc.) */
4388 const char *zDummy; /* Unused */
4390 sqlite3_clear_bindings(pSelect);
4391 sqlite3changeset_op(pIter, &zDummy, &nCol, &op, 0);
4392 rc = sessionBindRow(pIter,
4393 op==SQLITE_INSERT ? sqlite3changeset_new : sqlite3changeset_old,
4394 nCol, p->abPK, pSelect
4397 if( op!=SQLITE_DELETE && p->bIgnoreNoop ){
4398 int ii;
4399 for(ii=0; rc==SQLITE_OK && ii<nCol; ii++){
4400 if( p->abPK[ii]==0 ){
4401 sqlite3_value *pVal = 0;
4402 sqlite3changeset_new(pIter, ii, &pVal);
4403 sqlite3_bind_int(pSelect, ii+1+nCol, (pVal==0));
4404 if( pVal ) rc = sessionBindValue(pSelect, ii+1, pVal);
4409 if( rc==SQLITE_OK ){
4410 rc = sqlite3_step(pSelect);
4411 if( rc!=SQLITE_ROW ) rc = sqlite3_reset(pSelect);
4414 return rc;
4418 ** This function is called from within sqlite3changeset_apply_v2() when
4419 ** a conflict is encountered and resolved using conflict resolution
4420 ** mode eType (either SQLITE_CHANGESET_OMIT or SQLITE_CHANGESET_REPLACE)..
4421 ** It adds a conflict resolution record to the buffer in
4422 ** SessionApplyCtx.rebase, which will eventually be returned to the caller
4423 ** of apply_v2() as the "rebase" buffer.
4425 ** Return SQLITE_OK if successful, or an SQLite error code otherwise.
4427 static int sessionRebaseAdd(
4428 SessionApplyCtx *p, /* Apply context */
4429 int eType, /* Conflict resolution (OMIT or REPLACE) */
4430 sqlite3_changeset_iter *pIter /* Iterator pointing at current change */
4432 int rc = SQLITE_OK;
4433 if( p->bRebase ){
4434 int i;
4435 int eOp = pIter->op;
4436 if( p->bRebaseStarted==0 ){
4437 /* Append a table-header to the rebase buffer */
4438 const char *zTab = pIter->zTab;
4439 sessionAppendByte(&p->rebase, 'T', &rc);
4440 sessionAppendVarint(&p->rebase, p->nCol, &rc);
4441 sessionAppendBlob(&p->rebase, p->abPK, p->nCol, &rc);
4442 sessionAppendBlob(&p->rebase, (u8*)zTab, (int)strlen(zTab)+1, &rc);
4443 p->bRebaseStarted = 1;
4446 assert( eType==SQLITE_CHANGESET_REPLACE||eType==SQLITE_CHANGESET_OMIT );
4447 assert( eOp==SQLITE_DELETE || eOp==SQLITE_INSERT || eOp==SQLITE_UPDATE );
4449 sessionAppendByte(&p->rebase,
4450 (eOp==SQLITE_DELETE ? SQLITE_DELETE : SQLITE_INSERT), &rc
4452 sessionAppendByte(&p->rebase, (eType==SQLITE_CHANGESET_REPLACE), &rc);
4453 for(i=0; i<p->nCol; i++){
4454 sqlite3_value *pVal = 0;
4455 if( eOp==SQLITE_DELETE || (eOp==SQLITE_UPDATE && p->abPK[i]) ){
4456 sqlite3changeset_old(pIter, i, &pVal);
4457 }else{
4458 sqlite3changeset_new(pIter, i, &pVal);
4460 sessionAppendValue(&p->rebase, pVal, &rc);
4463 return rc;
4467 ** Invoke the conflict handler for the change that the changeset iterator
4468 ** currently points to.
4470 ** Argument eType must be either CHANGESET_DATA or CHANGESET_CONFLICT.
4471 ** If argument pbReplace is NULL, then the type of conflict handler invoked
4472 ** depends solely on eType, as follows:
4474 ** eType value Value passed to xConflict
4475 ** -------------------------------------------------
4476 ** CHANGESET_DATA CHANGESET_NOTFOUND
4477 ** CHANGESET_CONFLICT CHANGESET_CONSTRAINT
4479 ** Or, if pbReplace is not NULL, then an attempt is made to find an existing
4480 ** record with the same primary key as the record about to be deleted, updated
4481 ** or inserted. If such a record can be found, it is available to the conflict
4482 ** handler as the "conflicting" record. In this case the type of conflict
4483 ** handler invoked is as follows:
4485 ** eType value PK Record found? Value passed to xConflict
4486 ** ----------------------------------------------------------------
4487 ** CHANGESET_DATA Yes CHANGESET_DATA
4488 ** CHANGESET_DATA No CHANGESET_NOTFOUND
4489 ** CHANGESET_CONFLICT Yes CHANGESET_CONFLICT
4490 ** CHANGESET_CONFLICT No CHANGESET_CONSTRAINT
4492 ** If pbReplace is not NULL, and a record with a matching PK is found, and
4493 ** the conflict handler function returns SQLITE_CHANGESET_REPLACE, *pbReplace
4494 ** is set to non-zero before returning SQLITE_OK.
4496 ** If the conflict handler returns SQLITE_CHANGESET_ABORT, SQLITE_ABORT is
4497 ** returned. Or, if the conflict handler returns an invalid value,
4498 ** SQLITE_MISUSE. If the conflict handler returns SQLITE_CHANGESET_OMIT,
4499 ** this function returns SQLITE_OK.
4501 static int sessionConflictHandler(
4502 int eType, /* Either CHANGESET_DATA or CONFLICT */
4503 SessionApplyCtx *p, /* changeset_apply() context */
4504 sqlite3_changeset_iter *pIter, /* Changeset iterator */
4505 int(*xConflict)(void *, int, sqlite3_changeset_iter*),
4506 void *pCtx, /* First argument for conflict handler */
4507 int *pbReplace /* OUT: Set to true if PK row is found */
4509 int res = 0; /* Value returned by conflict handler */
4510 int rc;
4511 int nCol;
4512 int op;
4513 const char *zDummy;
4515 sqlite3changeset_op(pIter, &zDummy, &nCol, &op, 0);
4517 assert( eType==SQLITE_CHANGESET_CONFLICT || eType==SQLITE_CHANGESET_DATA );
4518 assert( SQLITE_CHANGESET_CONFLICT+1==SQLITE_CHANGESET_CONSTRAINT );
4519 assert( SQLITE_CHANGESET_DATA+1==SQLITE_CHANGESET_NOTFOUND );
4521 /* Bind the new.* PRIMARY KEY values to the SELECT statement. */
4522 if( pbReplace ){
4523 rc = sessionSeekToRow(pIter, p);
4524 }else{
4525 rc = SQLITE_OK;
4528 if( rc==SQLITE_ROW ){
4529 /* There exists another row with the new.* primary key. */
4530 if( p->bIgnoreNoop
4531 && sqlite3_column_int(p->pSelect, sqlite3_column_count(p->pSelect)-1)
4533 res = SQLITE_CHANGESET_OMIT;
4534 }else{
4535 pIter->pConflict = p->pSelect;
4536 res = xConflict(pCtx, eType, pIter);
4537 pIter->pConflict = 0;
4539 rc = sqlite3_reset(p->pSelect);
4540 }else if( rc==SQLITE_OK ){
4541 if( p->bDeferConstraints && eType==SQLITE_CHANGESET_CONFLICT ){
4542 /* Instead of invoking the conflict handler, append the change blob
4543 ** to the SessionApplyCtx.constraints buffer. */
4544 u8 *aBlob = &pIter->in.aData[pIter->in.iCurrent];
4545 int nBlob = pIter->in.iNext - pIter->in.iCurrent;
4546 sessionAppendBlob(&p->constraints, aBlob, nBlob, &rc);
4547 return SQLITE_OK;
4548 }else{
4549 /* No other row with the new.* primary key. */
4550 res = xConflict(pCtx, eType+1, pIter);
4551 if( res==SQLITE_CHANGESET_REPLACE ) rc = SQLITE_MISUSE;
4555 if( rc==SQLITE_OK ){
4556 switch( res ){
4557 case SQLITE_CHANGESET_REPLACE:
4558 assert( pbReplace );
4559 *pbReplace = 1;
4560 break;
4562 case SQLITE_CHANGESET_OMIT:
4563 break;
4565 case SQLITE_CHANGESET_ABORT:
4566 rc = SQLITE_ABORT;
4567 break;
4569 default:
4570 rc = SQLITE_MISUSE;
4571 break;
4573 if( rc==SQLITE_OK ){
4574 rc = sessionRebaseAdd(p, res, pIter);
4578 return rc;
4582 ** Attempt to apply the change that the iterator passed as the first argument
4583 ** currently points to to the database. If a conflict is encountered, invoke
4584 ** the conflict handler callback.
4586 ** If argument pbRetry is NULL, then ignore any CHANGESET_DATA conflict. If
4587 ** one is encountered, update or delete the row with the matching primary key
4588 ** instead. Or, if pbRetry is not NULL and a CHANGESET_DATA conflict occurs,
4589 ** invoke the conflict handler. If it returns CHANGESET_REPLACE, set *pbRetry
4590 ** to true before returning. In this case the caller will invoke this function
4591 ** again, this time with pbRetry set to NULL.
4593 ** If argument pbReplace is NULL and a CHANGESET_CONFLICT conflict is
4594 ** encountered invoke the conflict handler with CHANGESET_CONSTRAINT instead.
4595 ** Or, if pbReplace is not NULL, invoke it with CHANGESET_CONFLICT. If such
4596 ** an invocation returns SQLITE_CHANGESET_REPLACE, set *pbReplace to true
4597 ** before retrying. In this case the caller attempts to remove the conflicting
4598 ** row before invoking this function again, this time with pbReplace set
4599 ** to NULL.
4601 ** If any conflict handler returns SQLITE_CHANGESET_ABORT, this function
4602 ** returns SQLITE_ABORT. Otherwise, if no error occurs, SQLITE_OK is
4603 ** returned.
4605 static int sessionApplyOneOp(
4606 sqlite3_changeset_iter *pIter, /* Changeset iterator */
4607 SessionApplyCtx *p, /* changeset_apply() context */
4608 int(*xConflict)(void *, int, sqlite3_changeset_iter *),
4609 void *pCtx, /* First argument for the conflict handler */
4610 int *pbReplace, /* OUT: True to remove PK row and retry */
4611 int *pbRetry /* OUT: True to retry. */
4613 const char *zDummy;
4614 int op;
4615 int nCol;
4616 int rc = SQLITE_OK;
4618 assert( p->pDelete && p->pInsert && p->pSelect );
4619 assert( p->azCol && p->abPK );
4620 assert( !pbReplace || *pbReplace==0 );
4622 sqlite3changeset_op(pIter, &zDummy, &nCol, &op, 0);
4624 if( op==SQLITE_DELETE ){
4626 /* Bind values to the DELETE statement. If conflict handling is required,
4627 ** bind values for all columns and set bound variable (nCol+1) to true.
4628 ** Or, if conflict handling is not required, bind just the PK column
4629 ** values and, if it exists, set (nCol+1) to false. Conflict handling
4630 ** is not required if:
4632 ** * this is a patchset, or
4633 ** * (pbRetry==0), or
4634 ** * all columns of the table are PK columns (in this case there is
4635 ** no (nCol+1) variable to bind to).
4637 u8 *abPK = (pIter->bPatchset ? p->abPK : 0);
4638 rc = sessionBindRow(pIter, sqlite3changeset_old, nCol, abPK, p->pDelete);
4639 if( rc==SQLITE_OK && sqlite3_bind_parameter_count(p->pDelete)>nCol ){
4640 rc = sqlite3_bind_int(p->pDelete, nCol+1, (pbRetry==0 || abPK));
4642 if( rc!=SQLITE_OK ) return rc;
4644 sqlite3_step(p->pDelete);
4645 rc = sqlite3_reset(p->pDelete);
4646 if( rc==SQLITE_OK && sqlite3_changes(p->db)==0 && p->bIgnoreNoop==0 ){
4647 rc = sessionConflictHandler(
4648 SQLITE_CHANGESET_DATA, p, pIter, xConflict, pCtx, pbRetry
4650 }else if( (rc&0xff)==SQLITE_CONSTRAINT ){
4651 rc = sessionConflictHandler(
4652 SQLITE_CHANGESET_CONFLICT, p, pIter, xConflict, pCtx, 0
4656 }else if( op==SQLITE_UPDATE ){
4657 int i;
4658 sqlite3_stmt *pUp = 0;
4659 int bPatchset = (pbRetry==0 || pIter->bPatchset);
4661 rc = sessionUpdateFind(pIter, p, bPatchset, &pUp);
4663 /* Bind values to the UPDATE statement. */
4664 for(i=0; rc==SQLITE_OK && i<nCol; i++){
4665 sqlite3_value *pOld = sessionChangesetOld(pIter, i);
4666 sqlite3_value *pNew = sessionChangesetNew(pIter, i);
4667 if( p->abPK[i] || (bPatchset==0 && pOld) ){
4668 rc = sessionBindValue(pUp, i*2+2, pOld);
4670 if( rc==SQLITE_OK && pNew ){
4671 rc = sessionBindValue(pUp, i*2+1, pNew);
4674 if( rc!=SQLITE_OK ) return rc;
4676 /* Attempt the UPDATE. In the case of a NOTFOUND or DATA conflict,
4677 ** the result will be SQLITE_OK with 0 rows modified. */
4678 sqlite3_step(pUp);
4679 rc = sqlite3_reset(pUp);
4681 if( rc==SQLITE_OK && sqlite3_changes(p->db)==0 ){
4682 /* A NOTFOUND or DATA error. Search the table to see if it contains
4683 ** a row with a matching primary key. If so, this is a DATA conflict.
4684 ** Otherwise, if there is no primary key match, it is a NOTFOUND. */
4686 rc = sessionConflictHandler(
4687 SQLITE_CHANGESET_DATA, p, pIter, xConflict, pCtx, pbRetry
4690 }else if( (rc&0xff)==SQLITE_CONSTRAINT ){
4691 /* This is always a CONSTRAINT conflict. */
4692 rc = sessionConflictHandler(
4693 SQLITE_CHANGESET_CONFLICT, p, pIter, xConflict, pCtx, 0
4697 }else{
4698 assert( op==SQLITE_INSERT );
4699 if( p->bStat1 ){
4700 /* Check if there is a conflicting row. For sqlite_stat1, this needs
4701 ** to be done using a SELECT, as there is no PRIMARY KEY in the
4702 ** database schema to throw an exception if a duplicate is inserted. */
4703 rc = sessionSeekToRow(pIter, p);
4704 if( rc==SQLITE_ROW ){
4705 rc = SQLITE_CONSTRAINT;
4706 sqlite3_reset(p->pSelect);
4710 if( rc==SQLITE_OK ){
4711 rc = sessionBindRow(pIter, sqlite3changeset_new, nCol, 0, p->pInsert);
4712 if( rc!=SQLITE_OK ) return rc;
4714 sqlite3_step(p->pInsert);
4715 rc = sqlite3_reset(p->pInsert);
4718 if( (rc&0xff)==SQLITE_CONSTRAINT ){
4719 rc = sessionConflictHandler(
4720 SQLITE_CHANGESET_CONFLICT, p, pIter, xConflict, pCtx, pbReplace
4725 return rc;
4729 ** Attempt to apply the change that the iterator passed as the first argument
4730 ** currently points to to the database. If a conflict is encountered, invoke
4731 ** the conflict handler callback.
4733 ** The difference between this function and sessionApplyOne() is that this
4734 ** function handles the case where the conflict-handler is invoked and
4735 ** returns SQLITE_CHANGESET_REPLACE - indicating that the change should be
4736 ** retried in some manner.
4738 static int sessionApplyOneWithRetry(
4739 sqlite3 *db, /* Apply change to "main" db of this handle */
4740 sqlite3_changeset_iter *pIter, /* Changeset iterator to read change from */
4741 SessionApplyCtx *pApply, /* Apply context */
4742 int(*xConflict)(void*, int, sqlite3_changeset_iter*),
4743 void *pCtx /* First argument passed to xConflict */
4745 int bReplace = 0;
4746 int bRetry = 0;
4747 int rc;
4749 rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, &bReplace, &bRetry);
4750 if( rc==SQLITE_OK ){
4751 /* If the bRetry flag is set, the change has not been applied due to an
4752 ** SQLITE_CHANGESET_DATA problem (i.e. this is an UPDATE or DELETE and
4753 ** a row with the correct PK is present in the db, but one or more other
4754 ** fields do not contain the expected values) and the conflict handler
4755 ** returned SQLITE_CHANGESET_REPLACE. In this case retry the operation,
4756 ** but pass NULL as the final argument so that sessionApplyOneOp() ignores
4757 ** the SQLITE_CHANGESET_DATA problem. */
4758 if( bRetry ){
4759 assert( pIter->op==SQLITE_UPDATE || pIter->op==SQLITE_DELETE );
4760 rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, 0, 0);
4763 /* If the bReplace flag is set, the change is an INSERT that has not
4764 ** been performed because the database already contains a row with the
4765 ** specified primary key and the conflict handler returned
4766 ** SQLITE_CHANGESET_REPLACE. In this case remove the conflicting row
4767 ** before reattempting the INSERT. */
4768 else if( bReplace ){
4769 assert( pIter->op==SQLITE_INSERT );
4770 rc = sqlite3_exec(db, "SAVEPOINT replace_op", 0, 0, 0);
4771 if( rc==SQLITE_OK ){
4772 rc = sessionBindRow(pIter,
4773 sqlite3changeset_new, pApply->nCol, pApply->abPK, pApply->pDelete);
4774 sqlite3_bind_int(pApply->pDelete, pApply->nCol+1, 1);
4776 if( rc==SQLITE_OK ){
4777 sqlite3_step(pApply->pDelete);
4778 rc = sqlite3_reset(pApply->pDelete);
4780 if( rc==SQLITE_OK ){
4781 rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, 0, 0);
4783 if( rc==SQLITE_OK ){
4784 rc = sqlite3_exec(db, "RELEASE replace_op", 0, 0, 0);
4789 return rc;
4793 ** Retry the changes accumulated in the pApply->constraints buffer.
4795 static int sessionRetryConstraints(
4796 sqlite3 *db,
4797 int bPatchset,
4798 const char *zTab,
4799 SessionApplyCtx *pApply,
4800 int(*xConflict)(void*, int, sqlite3_changeset_iter*),
4801 void *pCtx /* First argument passed to xConflict */
4803 int rc = SQLITE_OK;
4805 while( pApply->constraints.nBuf ){
4806 sqlite3_changeset_iter *pIter2 = 0;
4807 SessionBuffer cons = pApply->constraints;
4808 memset(&pApply->constraints, 0, sizeof(SessionBuffer));
4810 rc = sessionChangesetStart(
4811 &pIter2, 0, 0, cons.nBuf, cons.aBuf, pApply->bInvertConstraints, 1
4813 if( rc==SQLITE_OK ){
4814 size_t nByte = 2*pApply->nCol*sizeof(sqlite3_value*);
4815 int rc2;
4816 pIter2->bPatchset = bPatchset;
4817 pIter2->zTab = (char*)zTab;
4818 pIter2->nCol = pApply->nCol;
4819 pIter2->abPK = pApply->abPK;
4820 sessionBufferGrow(&pIter2->tblhdr, nByte, &rc);
4821 pIter2->apValue = (sqlite3_value**)pIter2->tblhdr.aBuf;
4822 if( rc==SQLITE_OK ) memset(pIter2->apValue, 0, nByte);
4824 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3changeset_next(pIter2) ){
4825 rc = sessionApplyOneWithRetry(db, pIter2, pApply, xConflict, pCtx);
4828 rc2 = sqlite3changeset_finalize(pIter2);
4829 if( rc==SQLITE_OK ) rc = rc2;
4831 assert( pApply->bDeferConstraints || pApply->constraints.nBuf==0 );
4833 sqlite3_free(cons.aBuf);
4834 if( rc!=SQLITE_OK ) break;
4835 if( pApply->constraints.nBuf>=cons.nBuf ){
4836 /* No progress was made on the last round. */
4837 pApply->bDeferConstraints = 0;
4841 return rc;
4845 ** Argument pIter is a changeset iterator that has been initialized, but
4846 ** not yet passed to sqlite3changeset_next(). This function applies the
4847 ** changeset to the main database attached to handle "db". The supplied
4848 ** conflict handler callback is invoked to resolve any conflicts encountered
4849 ** while applying the change.
4851 static int sessionChangesetApply(
4852 sqlite3 *db, /* Apply change to "main" db of this handle */
4853 sqlite3_changeset_iter *pIter, /* Changeset to apply */
4854 int(*xFilter)(
4855 void *pCtx, /* Copy of sixth arg to _apply() */
4856 const char *zTab /* Table name */
4858 int(*xConflict)(
4859 void *pCtx, /* Copy of fifth arg to _apply() */
4860 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
4861 sqlite3_changeset_iter *p /* Handle describing change and conflict */
4863 void *pCtx, /* First argument passed to xConflict */
4864 void **ppRebase, int *pnRebase, /* OUT: Rebase information */
4865 int flags /* SESSION_APPLY_XXX flags */
4867 int schemaMismatch = 0;
4868 int rc = SQLITE_OK; /* Return code */
4869 const char *zTab = 0; /* Name of current table */
4870 int nTab = 0; /* Result of sqlite3Strlen30(zTab) */
4871 SessionApplyCtx sApply; /* changeset_apply() context object */
4872 int bPatchset;
4874 assert( xConflict!=0 );
4876 pIter->in.bNoDiscard = 1;
4877 memset(&sApply, 0, sizeof(sApply));
4878 sApply.bRebase = (ppRebase && pnRebase);
4879 sApply.bInvertConstraints = !!(flags & SQLITE_CHANGESETAPPLY_INVERT);
4880 sApply.bIgnoreNoop = !!(flags & SQLITE_CHANGESETAPPLY_IGNORENOOP);
4881 sqlite3_mutex_enter(sqlite3_db_mutex(db));
4882 if( (flags & SQLITE_CHANGESETAPPLY_NOSAVEPOINT)==0 ){
4883 rc = sqlite3_exec(db, "SAVEPOINT changeset_apply", 0, 0, 0);
4885 if( rc==SQLITE_OK ){
4886 rc = sqlite3_exec(db, "PRAGMA defer_foreign_keys = 1", 0, 0, 0);
4888 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3changeset_next(pIter) ){
4889 int nCol;
4890 int op;
4891 const char *zNew;
4893 sqlite3changeset_op(pIter, &zNew, &nCol, &op, 0);
4895 if( zTab==0 || sqlite3_strnicmp(zNew, zTab, nTab+1) ){
4896 u8 *abPK;
4898 rc = sessionRetryConstraints(
4899 db, pIter->bPatchset, zTab, &sApply, xConflict, pCtx
4901 if( rc!=SQLITE_OK ) break;
4903 sessionUpdateFree(&sApply);
4904 sqlite3_free((char*)sApply.azCol); /* cast works around VC++ bug */
4905 sqlite3_finalize(sApply.pDelete);
4906 sqlite3_finalize(sApply.pInsert);
4907 sqlite3_finalize(sApply.pSelect);
4908 sApply.db = db;
4909 sApply.pDelete = 0;
4910 sApply.pInsert = 0;
4911 sApply.pSelect = 0;
4912 sApply.nCol = 0;
4913 sApply.azCol = 0;
4914 sApply.abPK = 0;
4915 sApply.bStat1 = 0;
4916 sApply.bDeferConstraints = 1;
4917 sApply.bRebaseStarted = 0;
4918 sApply.bRowid = 0;
4919 memset(&sApply.constraints, 0, sizeof(SessionBuffer));
4921 /* If an xFilter() callback was specified, invoke it now. If the
4922 ** xFilter callback returns zero, skip this table. If it returns
4923 ** non-zero, proceed. */
4924 schemaMismatch = (xFilter && (0==xFilter(pCtx, zNew)));
4925 if( schemaMismatch ){
4926 zTab = sqlite3_mprintf("%s", zNew);
4927 if( zTab==0 ){
4928 rc = SQLITE_NOMEM;
4929 break;
4931 nTab = (int)strlen(zTab);
4932 sApply.azCol = (const char **)zTab;
4933 }else{
4934 int nMinCol = 0;
4935 int i;
4937 sqlite3changeset_pk(pIter, &abPK, 0);
4938 rc = sessionTableInfo(0, db, "main", zNew,
4939 &sApply.nCol, &zTab, &sApply.azCol, &sApply.abPK, &sApply.bRowid
4941 if( rc!=SQLITE_OK ) break;
4942 for(i=0; i<sApply.nCol; i++){
4943 if( sApply.abPK[i] ) nMinCol = i+1;
4946 if( sApply.nCol==0 ){
4947 schemaMismatch = 1;
4948 sqlite3_log(SQLITE_SCHEMA,
4949 "sqlite3changeset_apply(): no such table: %s", zTab
4952 else if( sApply.nCol<nCol ){
4953 schemaMismatch = 1;
4954 sqlite3_log(SQLITE_SCHEMA,
4955 "sqlite3changeset_apply(): table %s has %d columns, "
4956 "expected %d or more",
4957 zTab, sApply.nCol, nCol
4960 else if( nCol<nMinCol || memcmp(sApply.abPK, abPK, nCol)!=0 ){
4961 schemaMismatch = 1;
4962 sqlite3_log(SQLITE_SCHEMA, "sqlite3changeset_apply(): "
4963 "primary key mismatch for table %s", zTab
4966 else{
4967 sApply.nCol = nCol;
4968 if( 0==sqlite3_stricmp(zTab, "sqlite_stat1") ){
4969 if( (rc = sessionStat1Sql(db, &sApply) ) ){
4970 break;
4972 sApply.bStat1 = 1;
4973 }else{
4974 if( (rc = sessionSelectRow(db, zTab, &sApply))
4975 || (rc = sessionDeleteRow(db, zTab, &sApply))
4976 || (rc = sessionInsertRow(db, zTab, &sApply))
4978 break;
4980 sApply.bStat1 = 0;
4983 nTab = sqlite3Strlen30(zTab);
4987 /* If there is a schema mismatch on the current table, proceed to the
4988 ** next change. A log message has already been issued. */
4989 if( schemaMismatch ) continue;
4991 rc = sessionApplyOneWithRetry(db, pIter, &sApply, xConflict, pCtx);
4994 bPatchset = pIter->bPatchset;
4995 if( rc==SQLITE_OK ){
4996 rc = sqlite3changeset_finalize(pIter);
4997 }else{
4998 sqlite3changeset_finalize(pIter);
5001 if( rc==SQLITE_OK ){
5002 rc = sessionRetryConstraints(db, bPatchset, zTab, &sApply, xConflict, pCtx);
5005 if( rc==SQLITE_OK ){
5006 int nFk, notUsed;
5007 sqlite3_db_status(db, SQLITE_DBSTATUS_DEFERRED_FKS, &nFk, &notUsed, 0);
5008 if( nFk!=0 ){
5009 int res = SQLITE_CHANGESET_ABORT;
5010 sqlite3_changeset_iter sIter;
5011 memset(&sIter, 0, sizeof(sIter));
5012 sIter.nCol = nFk;
5013 res = xConflict(pCtx, SQLITE_CHANGESET_FOREIGN_KEY, &sIter);
5014 if( res!=SQLITE_CHANGESET_OMIT ){
5015 rc = SQLITE_CONSTRAINT;
5019 sqlite3_exec(db, "PRAGMA defer_foreign_keys = 0", 0, 0, 0);
5021 if( (flags & SQLITE_CHANGESETAPPLY_NOSAVEPOINT)==0 ){
5022 if( rc==SQLITE_OK ){
5023 rc = sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0);
5024 }else{
5025 sqlite3_exec(db, "ROLLBACK TO changeset_apply", 0, 0, 0);
5026 sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0);
5030 assert( sApply.bRebase || sApply.rebase.nBuf==0 );
5031 if( rc==SQLITE_OK && bPatchset==0 && sApply.bRebase ){
5032 *ppRebase = (void*)sApply.rebase.aBuf;
5033 *pnRebase = sApply.rebase.nBuf;
5034 sApply.rebase.aBuf = 0;
5036 sessionUpdateFree(&sApply);
5037 sqlite3_finalize(sApply.pInsert);
5038 sqlite3_finalize(sApply.pDelete);
5039 sqlite3_finalize(sApply.pSelect);
5040 sqlite3_free((char*)sApply.azCol); /* cast works around VC++ bug */
5041 sqlite3_free((char*)sApply.constraints.aBuf);
5042 sqlite3_free((char*)sApply.rebase.aBuf);
5043 sqlite3_mutex_leave(sqlite3_db_mutex(db));
5044 return rc;
5048 ** Apply the changeset passed via pChangeset/nChangeset to the main
5049 ** database attached to handle "db".
5051 int sqlite3changeset_apply_v2(
5052 sqlite3 *db, /* Apply change to "main" db of this handle */
5053 int nChangeset, /* Size of changeset in bytes */
5054 void *pChangeset, /* Changeset blob */
5055 int(*xFilter)(
5056 void *pCtx, /* Copy of sixth arg to _apply() */
5057 const char *zTab /* Table name */
5059 int(*xConflict)(
5060 void *pCtx, /* Copy of sixth arg to _apply() */
5061 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
5062 sqlite3_changeset_iter *p /* Handle describing change and conflict */
5064 void *pCtx, /* First argument passed to xConflict */
5065 void **ppRebase, int *pnRebase,
5066 int flags
5068 sqlite3_changeset_iter *pIter; /* Iterator to skip through changeset */
5069 int bInv = !!(flags & SQLITE_CHANGESETAPPLY_INVERT);
5070 int rc = sessionChangesetStart(&pIter, 0, 0, nChangeset, pChangeset, bInv, 1);
5071 if( rc==SQLITE_OK ){
5072 rc = sessionChangesetApply(
5073 db, pIter, xFilter, xConflict, pCtx, ppRebase, pnRebase, flags
5076 return rc;
5080 ** Apply the changeset passed via pChangeset/nChangeset to the main database
5081 ** attached to handle "db". Invoke the supplied conflict handler callback
5082 ** to resolve any conflicts encountered while applying the change.
5084 int sqlite3changeset_apply(
5085 sqlite3 *db, /* Apply change to "main" db of this handle */
5086 int nChangeset, /* Size of changeset in bytes */
5087 void *pChangeset, /* Changeset blob */
5088 int(*xFilter)(
5089 void *pCtx, /* Copy of sixth arg to _apply() */
5090 const char *zTab /* Table name */
5092 int(*xConflict)(
5093 void *pCtx, /* Copy of fifth arg to _apply() */
5094 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
5095 sqlite3_changeset_iter *p /* Handle describing change and conflict */
5097 void *pCtx /* First argument passed to xConflict */
5099 return sqlite3changeset_apply_v2(
5100 db, nChangeset, pChangeset, xFilter, xConflict, pCtx, 0, 0, 0
5105 ** Apply the changeset passed via xInput/pIn to the main database
5106 ** attached to handle "db". Invoke the supplied conflict handler callback
5107 ** to resolve any conflicts encountered while applying the change.
5109 int sqlite3changeset_apply_v2_strm(
5110 sqlite3 *db, /* Apply change to "main" db of this handle */
5111 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
5112 void *pIn, /* First arg for xInput */
5113 int(*xFilter)(
5114 void *pCtx, /* Copy of sixth arg to _apply() */
5115 const char *zTab /* Table name */
5117 int(*xConflict)(
5118 void *pCtx, /* Copy of sixth arg to _apply() */
5119 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
5120 sqlite3_changeset_iter *p /* Handle describing change and conflict */
5122 void *pCtx, /* First argument passed to xConflict */
5123 void **ppRebase, int *pnRebase,
5124 int flags
5126 sqlite3_changeset_iter *pIter; /* Iterator to skip through changeset */
5127 int bInverse = !!(flags & SQLITE_CHANGESETAPPLY_INVERT);
5128 int rc = sessionChangesetStart(&pIter, xInput, pIn, 0, 0, bInverse, 1);
5129 if( rc==SQLITE_OK ){
5130 rc = sessionChangesetApply(
5131 db, pIter, xFilter, xConflict, pCtx, ppRebase, pnRebase, flags
5134 return rc;
5136 int sqlite3changeset_apply_strm(
5137 sqlite3 *db, /* Apply change to "main" db of this handle */
5138 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
5139 void *pIn, /* First arg for xInput */
5140 int(*xFilter)(
5141 void *pCtx, /* Copy of sixth arg to _apply() */
5142 const char *zTab /* Table name */
5144 int(*xConflict)(
5145 void *pCtx, /* Copy of sixth arg to _apply() */
5146 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
5147 sqlite3_changeset_iter *p /* Handle describing change and conflict */
5149 void *pCtx /* First argument passed to xConflict */
5151 return sqlite3changeset_apply_v2_strm(
5152 db, xInput, pIn, xFilter, xConflict, pCtx, 0, 0, 0
5157 ** sqlite3_changegroup handle.
5159 struct sqlite3_changegroup {
5160 int rc; /* Error code */
5161 int bPatch; /* True to accumulate patchsets */
5162 SessionTable *pList; /* List of tables in current patch */
5166 ** This function is called to merge two changes to the same row together as
5167 ** part of an sqlite3changeset_concat() operation. A new change object is
5168 ** allocated and a pointer to it stored in *ppNew.
5170 static int sessionChangeMerge(
5171 SessionTable *pTab, /* Table structure */
5172 int bRebase, /* True for a rebase hash-table */
5173 int bPatchset, /* True for patchsets */
5174 SessionChange *pExist, /* Existing change */
5175 int op2, /* Second change operation */
5176 int bIndirect, /* True if second change is indirect */
5177 u8 *aRec, /* Second change record */
5178 int nRec, /* Number of bytes in aRec */
5179 SessionChange **ppNew /* OUT: Merged change */
5181 SessionChange *pNew = 0;
5182 int rc = SQLITE_OK;
5184 if( !pExist ){
5185 pNew = (SessionChange *)sqlite3_malloc64(sizeof(SessionChange) + nRec);
5186 if( !pNew ){
5187 return SQLITE_NOMEM;
5189 memset(pNew, 0, sizeof(SessionChange));
5190 pNew->op = op2;
5191 pNew->bIndirect = bIndirect;
5192 pNew->aRecord = (u8*)&pNew[1];
5193 if( bIndirect==0 || bRebase==0 ){
5194 pNew->nRecord = nRec;
5195 memcpy(pNew->aRecord, aRec, nRec);
5196 }else{
5197 int i;
5198 u8 *pIn = aRec;
5199 u8 *pOut = pNew->aRecord;
5200 for(i=0; i<pTab->nCol; i++){
5201 int nIn = sessionSerialLen(pIn);
5202 if( *pIn==0 ){
5203 *pOut++ = 0;
5204 }else if( pTab->abPK[i]==0 ){
5205 *pOut++ = 0xFF;
5206 }else{
5207 memcpy(pOut, pIn, nIn);
5208 pOut += nIn;
5210 pIn += nIn;
5212 pNew->nRecord = pOut - pNew->aRecord;
5214 }else if( bRebase ){
5215 if( pExist->op==SQLITE_DELETE && pExist->bIndirect ){
5216 *ppNew = pExist;
5217 }else{
5218 sqlite3_int64 nByte = nRec + pExist->nRecord + sizeof(SessionChange);
5219 pNew = (SessionChange*)sqlite3_malloc64(nByte);
5220 if( pNew==0 ){
5221 rc = SQLITE_NOMEM;
5222 }else{
5223 int i;
5224 u8 *a1 = pExist->aRecord;
5225 u8 *a2 = aRec;
5226 u8 *pOut;
5228 memset(pNew, 0, nByte);
5229 pNew->bIndirect = bIndirect || pExist->bIndirect;
5230 pNew->op = op2;
5231 pOut = pNew->aRecord = (u8*)&pNew[1];
5233 for(i=0; i<pTab->nCol; i++){
5234 int n1 = sessionSerialLen(a1);
5235 int n2 = sessionSerialLen(a2);
5236 if( *a1==0xFF || (pTab->abPK[i]==0 && bIndirect) ){
5237 *pOut++ = 0xFF;
5238 }else if( *a2==0 ){
5239 memcpy(pOut, a1, n1);
5240 pOut += n1;
5241 }else{
5242 memcpy(pOut, a2, n2);
5243 pOut += n2;
5245 a1 += n1;
5246 a2 += n2;
5248 pNew->nRecord = pOut - pNew->aRecord;
5250 sqlite3_free(pExist);
5252 }else{
5253 int op1 = pExist->op;
5256 ** op1=INSERT, op2=INSERT -> Unsupported. Discard op2.
5257 ** op1=INSERT, op2=UPDATE -> INSERT.
5258 ** op1=INSERT, op2=DELETE -> (none)
5260 ** op1=UPDATE, op2=INSERT -> Unsupported. Discard op2.
5261 ** op1=UPDATE, op2=UPDATE -> UPDATE.
5262 ** op1=UPDATE, op2=DELETE -> DELETE.
5264 ** op1=DELETE, op2=INSERT -> UPDATE.
5265 ** op1=DELETE, op2=UPDATE -> Unsupported. Discard op2.
5266 ** op1=DELETE, op2=DELETE -> Unsupported. Discard op2.
5268 if( (op1==SQLITE_INSERT && op2==SQLITE_INSERT)
5269 || (op1==SQLITE_UPDATE && op2==SQLITE_INSERT)
5270 || (op1==SQLITE_DELETE && op2==SQLITE_UPDATE)
5271 || (op1==SQLITE_DELETE && op2==SQLITE_DELETE)
5273 pNew = pExist;
5274 }else if( op1==SQLITE_INSERT && op2==SQLITE_DELETE ){
5275 sqlite3_free(pExist);
5276 assert( pNew==0 );
5277 }else{
5278 u8 *aExist = pExist->aRecord;
5279 sqlite3_int64 nByte;
5280 u8 *aCsr;
5282 /* Allocate a new SessionChange object. Ensure that the aRecord[]
5283 ** buffer of the new object is large enough to hold any record that
5284 ** may be generated by combining the input records. */
5285 nByte = sizeof(SessionChange) + pExist->nRecord + nRec;
5286 pNew = (SessionChange *)sqlite3_malloc64(nByte);
5287 if( !pNew ){
5288 sqlite3_free(pExist);
5289 return SQLITE_NOMEM;
5291 memset(pNew, 0, sizeof(SessionChange));
5292 pNew->bIndirect = (bIndirect && pExist->bIndirect);
5293 aCsr = pNew->aRecord = (u8 *)&pNew[1];
5295 if( op1==SQLITE_INSERT ){ /* INSERT + UPDATE */
5296 u8 *a1 = aRec;
5297 assert( op2==SQLITE_UPDATE );
5298 pNew->op = SQLITE_INSERT;
5299 if( bPatchset==0 ) sessionSkipRecord(&a1, pTab->nCol);
5300 sessionMergeRecord(&aCsr, pTab->nCol, aExist, a1);
5301 }else if( op1==SQLITE_DELETE ){ /* DELETE + INSERT */
5302 assert( op2==SQLITE_INSERT );
5303 pNew->op = SQLITE_UPDATE;
5304 if( bPatchset ){
5305 memcpy(aCsr, aRec, nRec);
5306 aCsr += nRec;
5307 }else{
5308 if( 0==sessionMergeUpdate(&aCsr, pTab, bPatchset, aExist, 0,aRec,0) ){
5309 sqlite3_free(pNew);
5310 pNew = 0;
5313 }else if( op2==SQLITE_UPDATE ){ /* UPDATE + UPDATE */
5314 u8 *a1 = aExist;
5315 u8 *a2 = aRec;
5316 assert( op1==SQLITE_UPDATE );
5317 if( bPatchset==0 ){
5318 sessionSkipRecord(&a1, pTab->nCol);
5319 sessionSkipRecord(&a2, pTab->nCol);
5321 pNew->op = SQLITE_UPDATE;
5322 if( 0==sessionMergeUpdate(&aCsr, pTab, bPatchset, aRec, aExist,a1,a2) ){
5323 sqlite3_free(pNew);
5324 pNew = 0;
5326 }else{ /* UPDATE + DELETE */
5327 assert( op1==SQLITE_UPDATE && op2==SQLITE_DELETE );
5328 pNew->op = SQLITE_DELETE;
5329 if( bPatchset ){
5330 memcpy(aCsr, aRec, nRec);
5331 aCsr += nRec;
5332 }else{
5333 sessionMergeRecord(&aCsr, pTab->nCol, aRec, aExist);
5337 if( pNew ){
5338 pNew->nRecord = (int)(aCsr - pNew->aRecord);
5340 sqlite3_free(pExist);
5344 *ppNew = pNew;
5345 return rc;
5349 ** Add all changes in the changeset traversed by the iterator passed as
5350 ** the first argument to the changegroup hash tables.
5352 static int sessionChangesetToHash(
5353 sqlite3_changeset_iter *pIter, /* Iterator to read from */
5354 sqlite3_changegroup *pGrp, /* Changegroup object to add changeset to */
5355 int bRebase /* True if hash table is for rebasing */
5357 u8 *aRec;
5358 int nRec;
5359 int rc = SQLITE_OK;
5360 SessionTable *pTab = 0;
5362 while( SQLITE_ROW==sessionChangesetNext(pIter, &aRec, &nRec, 0) ){
5363 const char *zNew;
5364 int nCol;
5365 int op;
5366 int iHash;
5367 int bIndirect;
5368 SessionChange *pChange;
5369 SessionChange *pExist = 0;
5370 SessionChange **pp;
5372 if( pGrp->pList==0 ){
5373 pGrp->bPatch = pIter->bPatchset;
5374 }else if( pIter->bPatchset!=pGrp->bPatch ){
5375 rc = SQLITE_ERROR;
5376 break;
5379 sqlite3changeset_op(pIter, &zNew, &nCol, &op, &bIndirect);
5380 if( !pTab || sqlite3_stricmp(zNew, pTab->zName) ){
5381 /* Search the list for a matching table */
5382 int nNew = (int)strlen(zNew);
5383 u8 *abPK;
5385 sqlite3changeset_pk(pIter, &abPK, 0);
5386 for(pTab = pGrp->pList; pTab; pTab=pTab->pNext){
5387 if( 0==sqlite3_strnicmp(pTab->zName, zNew, nNew+1) ) break;
5389 if( !pTab ){
5390 SessionTable **ppTab;
5392 pTab = sqlite3_malloc64(sizeof(SessionTable) + nCol + nNew+1);
5393 if( !pTab ){
5394 rc = SQLITE_NOMEM;
5395 break;
5397 memset(pTab, 0, sizeof(SessionTable));
5398 pTab->nCol = nCol;
5399 pTab->abPK = (u8*)&pTab[1];
5400 memcpy(pTab->abPK, abPK, nCol);
5401 pTab->zName = (char*)&pTab->abPK[nCol];
5402 memcpy(pTab->zName, zNew, nNew+1);
5404 /* The new object must be linked on to the end of the list, not
5405 ** simply added to the start of it. This is to ensure that the
5406 ** tables within the output of sqlite3changegroup_output() are in
5407 ** the right order. */
5408 for(ppTab=&pGrp->pList; *ppTab; ppTab=&(*ppTab)->pNext);
5409 *ppTab = pTab;
5410 }else if( pTab->nCol!=nCol || memcmp(pTab->abPK, abPK, nCol) ){
5411 rc = SQLITE_SCHEMA;
5412 break;
5416 if( sessionGrowHash(0, pIter->bPatchset, pTab) ){
5417 rc = SQLITE_NOMEM;
5418 break;
5420 iHash = sessionChangeHash(
5421 pTab, (pIter->bPatchset && op==SQLITE_DELETE), aRec, pTab->nChange
5424 /* Search for existing entry. If found, remove it from the hash table.
5425 ** Code below may link it back in.
5427 for(pp=&pTab->apChange[iHash]; *pp; pp=&(*pp)->pNext){
5428 int bPkOnly1 = 0;
5429 int bPkOnly2 = 0;
5430 if( pIter->bPatchset ){
5431 bPkOnly1 = (*pp)->op==SQLITE_DELETE;
5432 bPkOnly2 = op==SQLITE_DELETE;
5434 if( sessionChangeEqual(pTab, bPkOnly1, (*pp)->aRecord, bPkOnly2, aRec) ){
5435 pExist = *pp;
5436 *pp = (*pp)->pNext;
5437 pTab->nEntry--;
5438 break;
5442 rc = sessionChangeMerge(pTab, bRebase,
5443 pIter->bPatchset, pExist, op, bIndirect, aRec, nRec, &pChange
5445 if( rc ) break;
5446 if( pChange ){
5447 pChange->pNext = pTab->apChange[iHash];
5448 pTab->apChange[iHash] = pChange;
5449 pTab->nEntry++;
5453 if( rc==SQLITE_OK ) rc = pIter->rc;
5454 return rc;
5458 ** Serialize a changeset (or patchset) based on all changesets (or patchsets)
5459 ** added to the changegroup object passed as the first argument.
5461 ** If xOutput is not NULL, then the changeset/patchset is returned to the
5462 ** user via one or more calls to xOutput, as with the other streaming
5463 ** interfaces.
5465 ** Or, if xOutput is NULL, then (*ppOut) is populated with a pointer to a
5466 ** buffer containing the output changeset before this function returns. In
5467 ** this case (*pnOut) is set to the size of the output buffer in bytes. It
5468 ** is the responsibility of the caller to free the output buffer using
5469 ** sqlite3_free() when it is no longer required.
5471 ** If successful, SQLITE_OK is returned. Or, if an error occurs, an SQLite
5472 ** error code. If an error occurs and xOutput is NULL, (*ppOut) and (*pnOut)
5473 ** are both set to 0 before returning.
5475 static int sessionChangegroupOutput(
5476 sqlite3_changegroup *pGrp,
5477 int (*xOutput)(void *pOut, const void *pData, int nData),
5478 void *pOut,
5479 int *pnOut,
5480 void **ppOut
5482 int rc = SQLITE_OK;
5483 SessionBuffer buf = {0, 0, 0};
5484 SessionTable *pTab;
5485 assert( xOutput==0 || (ppOut==0 && pnOut==0) );
5487 /* Create the serialized output changeset based on the contents of the
5488 ** hash tables attached to the SessionTable objects in list p->pList.
5490 for(pTab=pGrp->pList; rc==SQLITE_OK && pTab; pTab=pTab->pNext){
5491 int i;
5492 if( pTab->nEntry==0 ) continue;
5494 sessionAppendTableHdr(&buf, pGrp->bPatch, pTab, &rc);
5495 for(i=0; i<pTab->nChange; i++){
5496 SessionChange *p;
5497 for(p=pTab->apChange[i]; p; p=p->pNext){
5498 sessionAppendByte(&buf, p->op, &rc);
5499 sessionAppendByte(&buf, p->bIndirect, &rc);
5500 sessionAppendBlob(&buf, p->aRecord, p->nRecord, &rc);
5501 if( rc==SQLITE_OK && xOutput && buf.nBuf>=sessions_strm_chunk_size ){
5502 rc = xOutput(pOut, buf.aBuf, buf.nBuf);
5503 buf.nBuf = 0;
5509 if( rc==SQLITE_OK ){
5510 if( xOutput ){
5511 if( buf.nBuf>0 ) rc = xOutput(pOut, buf.aBuf, buf.nBuf);
5512 }else if( ppOut ){
5513 *ppOut = buf.aBuf;
5514 if( pnOut ) *pnOut = buf.nBuf;
5515 buf.aBuf = 0;
5518 sqlite3_free(buf.aBuf);
5520 return rc;
5524 ** Allocate a new, empty, sqlite3_changegroup.
5526 int sqlite3changegroup_new(sqlite3_changegroup **pp){
5527 int rc = SQLITE_OK; /* Return code */
5528 sqlite3_changegroup *p; /* New object */
5529 p = (sqlite3_changegroup*)sqlite3_malloc(sizeof(sqlite3_changegroup));
5530 if( p==0 ){
5531 rc = SQLITE_NOMEM;
5532 }else{
5533 memset(p, 0, sizeof(sqlite3_changegroup));
5535 *pp = p;
5536 return rc;
5540 ** Add the changeset currently stored in buffer pData, size nData bytes,
5541 ** to changeset-group p.
5543 int sqlite3changegroup_add(sqlite3_changegroup *pGrp, int nData, void *pData){
5544 sqlite3_changeset_iter *pIter; /* Iterator opened on pData/nData */
5545 int rc; /* Return code */
5547 rc = sqlite3changeset_start(&pIter, nData, pData);
5548 if( rc==SQLITE_OK ){
5549 rc = sessionChangesetToHash(pIter, pGrp, 0);
5551 sqlite3changeset_finalize(pIter);
5552 return rc;
5556 ** Obtain a buffer containing a changeset representing the concatenation
5557 ** of all changesets added to the group so far.
5559 int sqlite3changegroup_output(
5560 sqlite3_changegroup *pGrp,
5561 int *pnData,
5562 void **ppData
5564 return sessionChangegroupOutput(pGrp, 0, 0, pnData, ppData);
5568 ** Streaming versions of changegroup_add().
5570 int sqlite3changegroup_add_strm(
5571 sqlite3_changegroup *pGrp,
5572 int (*xInput)(void *pIn, void *pData, int *pnData),
5573 void *pIn
5575 sqlite3_changeset_iter *pIter; /* Iterator opened on pData/nData */
5576 int rc; /* Return code */
5578 rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
5579 if( rc==SQLITE_OK ){
5580 rc = sessionChangesetToHash(pIter, pGrp, 0);
5582 sqlite3changeset_finalize(pIter);
5583 return rc;
5587 ** Streaming versions of changegroup_output().
5589 int sqlite3changegroup_output_strm(
5590 sqlite3_changegroup *pGrp,
5591 int (*xOutput)(void *pOut, const void *pData, int nData),
5592 void *pOut
5594 return sessionChangegroupOutput(pGrp, xOutput, pOut, 0, 0);
5598 ** Delete a changegroup object.
5600 void sqlite3changegroup_delete(sqlite3_changegroup *pGrp){
5601 if( pGrp ){
5602 sessionDeleteTable(0, pGrp->pList);
5603 sqlite3_free(pGrp);
5608 ** Combine two changesets together.
5610 int sqlite3changeset_concat(
5611 int nLeft, /* Number of bytes in lhs input */
5612 void *pLeft, /* Lhs input changeset */
5613 int nRight /* Number of bytes in rhs input */,
5614 void *pRight, /* Rhs input changeset */
5615 int *pnOut, /* OUT: Number of bytes in output changeset */
5616 void **ppOut /* OUT: changeset (left <concat> right) */
5618 sqlite3_changegroup *pGrp;
5619 int rc;
5621 rc = sqlite3changegroup_new(&pGrp);
5622 if( rc==SQLITE_OK ){
5623 rc = sqlite3changegroup_add(pGrp, nLeft, pLeft);
5625 if( rc==SQLITE_OK ){
5626 rc = sqlite3changegroup_add(pGrp, nRight, pRight);
5628 if( rc==SQLITE_OK ){
5629 rc = sqlite3changegroup_output(pGrp, pnOut, ppOut);
5631 sqlite3changegroup_delete(pGrp);
5633 return rc;
5637 ** Streaming version of sqlite3changeset_concat().
5639 int sqlite3changeset_concat_strm(
5640 int (*xInputA)(void *pIn, void *pData, int *pnData),
5641 void *pInA,
5642 int (*xInputB)(void *pIn, void *pData, int *pnData),
5643 void *pInB,
5644 int (*xOutput)(void *pOut, const void *pData, int nData),
5645 void *pOut
5647 sqlite3_changegroup *pGrp;
5648 int rc;
5650 rc = sqlite3changegroup_new(&pGrp);
5651 if( rc==SQLITE_OK ){
5652 rc = sqlite3changegroup_add_strm(pGrp, xInputA, pInA);
5654 if( rc==SQLITE_OK ){
5655 rc = sqlite3changegroup_add_strm(pGrp, xInputB, pInB);
5657 if( rc==SQLITE_OK ){
5658 rc = sqlite3changegroup_output_strm(pGrp, xOutput, pOut);
5660 sqlite3changegroup_delete(pGrp);
5662 return rc;
5666 ** Changeset rebaser handle.
5668 struct sqlite3_rebaser {
5669 sqlite3_changegroup grp; /* Hash table */
5673 ** Buffers a1 and a2 must both contain a sessions module record nCol
5674 ** fields in size. This function appends an nCol sessions module
5675 ** record to buffer pBuf that is a copy of a1, except that for
5676 ** each field that is undefined in a1[], swap in the field from a2[].
5678 static void sessionAppendRecordMerge(
5679 SessionBuffer *pBuf, /* Buffer to append to */
5680 int nCol, /* Number of columns in each record */
5681 u8 *a1, int n1, /* Record 1 */
5682 u8 *a2, int n2, /* Record 2 */
5683 int *pRc /* IN/OUT: error code */
5685 sessionBufferGrow(pBuf, n1+n2, pRc);
5686 if( *pRc==SQLITE_OK ){
5687 int i;
5688 u8 *pOut = &pBuf->aBuf[pBuf->nBuf];
5689 for(i=0; i<nCol; i++){
5690 int nn1 = sessionSerialLen(a1);
5691 int nn2 = sessionSerialLen(a2);
5692 if( *a1==0 || *a1==0xFF ){
5693 memcpy(pOut, a2, nn2);
5694 pOut += nn2;
5695 }else{
5696 memcpy(pOut, a1, nn1);
5697 pOut += nn1;
5699 a1 += nn1;
5700 a2 += nn2;
5703 pBuf->nBuf = pOut-pBuf->aBuf;
5704 assert( pBuf->nBuf<=pBuf->nAlloc );
5709 ** This function is called when rebasing a local UPDATE change against one
5710 ** or more remote UPDATE changes. The aRec/nRec buffer contains the current
5711 ** old.* and new.* records for the change. The rebase buffer (a single
5712 ** record) is in aChange/nChange. The rebased change is appended to buffer
5713 ** pBuf.
5715 ** Rebasing the UPDATE involves:
5717 ** * Removing any changes to fields for which the corresponding field
5718 ** in the rebase buffer is set to "replaced" (type 0xFF). If this
5719 ** means the UPDATE change updates no fields, nothing is appended
5720 ** to the output buffer.
5722 ** * For each field modified by the local change for which the
5723 ** corresponding field in the rebase buffer is not "undefined" (0x00)
5724 ** or "replaced" (0xFF), the old.* value is replaced by the value
5725 ** in the rebase buffer.
5727 static void sessionAppendPartialUpdate(
5728 SessionBuffer *pBuf, /* Append record here */
5729 sqlite3_changeset_iter *pIter, /* Iterator pointed at local change */
5730 u8 *aRec, int nRec, /* Local change */
5731 u8 *aChange, int nChange, /* Record to rebase against */
5732 int *pRc /* IN/OUT: Return Code */
5734 sessionBufferGrow(pBuf, 2+nRec+nChange, pRc);
5735 if( *pRc==SQLITE_OK ){
5736 int bData = 0;
5737 u8 *pOut = &pBuf->aBuf[pBuf->nBuf];
5738 int i;
5739 u8 *a1 = aRec;
5740 u8 *a2 = aChange;
5742 *pOut++ = SQLITE_UPDATE;
5743 *pOut++ = pIter->bIndirect;
5744 for(i=0; i<pIter->nCol; i++){
5745 int n1 = sessionSerialLen(a1);
5746 int n2 = sessionSerialLen(a2);
5747 if( pIter->abPK[i] || a2[0]==0 ){
5748 if( !pIter->abPK[i] && a1[0] ) bData = 1;
5749 memcpy(pOut, a1, n1);
5750 pOut += n1;
5751 }else if( a2[0]!=0xFF && a1[0] ){
5752 bData = 1;
5753 memcpy(pOut, a2, n2);
5754 pOut += n2;
5755 }else{
5756 *pOut++ = '\0';
5758 a1 += n1;
5759 a2 += n2;
5761 if( bData ){
5762 a2 = aChange;
5763 for(i=0; i<pIter->nCol; i++){
5764 int n1 = sessionSerialLen(a1);
5765 int n2 = sessionSerialLen(a2);
5766 if( pIter->abPK[i] || a2[0]!=0xFF ){
5767 memcpy(pOut, a1, n1);
5768 pOut += n1;
5769 }else{
5770 *pOut++ = '\0';
5772 a1 += n1;
5773 a2 += n2;
5775 pBuf->nBuf = (pOut - pBuf->aBuf);
5781 ** pIter is configured to iterate through a changeset. This function rebases
5782 ** that changeset according to the current configuration of the rebaser
5783 ** object passed as the first argument. If no error occurs and argument xOutput
5784 ** is not NULL, then the changeset is returned to the caller by invoking
5785 ** xOutput zero or more times and SQLITE_OK returned. Or, if xOutput is NULL,
5786 ** then (*ppOut) is set to point to a buffer containing the rebased changeset
5787 ** before this function returns. In this case (*pnOut) is set to the size of
5788 ** the buffer in bytes. It is the responsibility of the caller to eventually
5789 ** free the (*ppOut) buffer using sqlite3_free().
5791 ** If an error occurs, an SQLite error code is returned. If ppOut and
5792 ** pnOut are not NULL, then the two output parameters are set to 0 before
5793 ** returning.
5795 static int sessionRebase(
5796 sqlite3_rebaser *p, /* Rebaser hash table */
5797 sqlite3_changeset_iter *pIter, /* Input data */
5798 int (*xOutput)(void *pOut, const void *pData, int nData),
5799 void *pOut, /* Context for xOutput callback */
5800 int *pnOut, /* OUT: Number of bytes in output changeset */
5801 void **ppOut /* OUT: Inverse of pChangeset */
5803 int rc = SQLITE_OK;
5804 u8 *aRec = 0;
5805 int nRec = 0;
5806 int bNew = 0;
5807 SessionTable *pTab = 0;
5808 SessionBuffer sOut = {0,0,0};
5810 while( SQLITE_ROW==sessionChangesetNext(pIter, &aRec, &nRec, &bNew) ){
5811 SessionChange *pChange = 0;
5812 int bDone = 0;
5814 if( bNew ){
5815 const char *zTab = pIter->zTab;
5816 for(pTab=p->grp.pList; pTab; pTab=pTab->pNext){
5817 if( 0==sqlite3_stricmp(pTab->zName, zTab) ) break;
5819 bNew = 0;
5821 /* A patchset may not be rebased */
5822 if( pIter->bPatchset ){
5823 rc = SQLITE_ERROR;
5826 /* Append a table header to the output for this new table */
5827 sessionAppendByte(&sOut, pIter->bPatchset ? 'P' : 'T', &rc);
5828 sessionAppendVarint(&sOut, pIter->nCol, &rc);
5829 sessionAppendBlob(&sOut, pIter->abPK, pIter->nCol, &rc);
5830 sessionAppendBlob(&sOut,(u8*)pIter->zTab,(int)strlen(pIter->zTab)+1,&rc);
5833 if( pTab && rc==SQLITE_OK ){
5834 int iHash = sessionChangeHash(pTab, 0, aRec, pTab->nChange);
5836 for(pChange=pTab->apChange[iHash]; pChange; pChange=pChange->pNext){
5837 if( sessionChangeEqual(pTab, 0, aRec, 0, pChange->aRecord) ){
5838 break;
5843 if( pChange ){
5844 assert( pChange->op==SQLITE_DELETE || pChange->op==SQLITE_INSERT );
5845 switch( pIter->op ){
5846 case SQLITE_INSERT:
5847 if( pChange->op==SQLITE_INSERT ){
5848 bDone = 1;
5849 if( pChange->bIndirect==0 ){
5850 sessionAppendByte(&sOut, SQLITE_UPDATE, &rc);
5851 sessionAppendByte(&sOut, pIter->bIndirect, &rc);
5852 sessionAppendBlob(&sOut, pChange->aRecord, pChange->nRecord, &rc);
5853 sessionAppendBlob(&sOut, aRec, nRec, &rc);
5856 break;
5858 case SQLITE_UPDATE:
5859 bDone = 1;
5860 if( pChange->op==SQLITE_DELETE ){
5861 if( pChange->bIndirect==0 ){
5862 u8 *pCsr = aRec;
5863 sessionSkipRecord(&pCsr, pIter->nCol);
5864 sessionAppendByte(&sOut, SQLITE_INSERT, &rc);
5865 sessionAppendByte(&sOut, pIter->bIndirect, &rc);
5866 sessionAppendRecordMerge(&sOut, pIter->nCol,
5867 pCsr, nRec-(pCsr-aRec),
5868 pChange->aRecord, pChange->nRecord, &rc
5871 }else{
5872 sessionAppendPartialUpdate(&sOut, pIter,
5873 aRec, nRec, pChange->aRecord, pChange->nRecord, &rc
5876 break;
5878 default:
5879 assert( pIter->op==SQLITE_DELETE );
5880 bDone = 1;
5881 if( pChange->op==SQLITE_INSERT ){
5882 sessionAppendByte(&sOut, SQLITE_DELETE, &rc);
5883 sessionAppendByte(&sOut, pIter->bIndirect, &rc);
5884 sessionAppendRecordMerge(&sOut, pIter->nCol,
5885 pChange->aRecord, pChange->nRecord, aRec, nRec, &rc
5888 break;
5892 if( bDone==0 ){
5893 sessionAppendByte(&sOut, pIter->op, &rc);
5894 sessionAppendByte(&sOut, pIter->bIndirect, &rc);
5895 sessionAppendBlob(&sOut, aRec, nRec, &rc);
5897 if( rc==SQLITE_OK && xOutput && sOut.nBuf>sessions_strm_chunk_size ){
5898 rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
5899 sOut.nBuf = 0;
5901 if( rc ) break;
5904 if( rc!=SQLITE_OK ){
5905 sqlite3_free(sOut.aBuf);
5906 memset(&sOut, 0, sizeof(sOut));
5909 if( rc==SQLITE_OK ){
5910 if( xOutput ){
5911 if( sOut.nBuf>0 ){
5912 rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
5914 }else if( ppOut ){
5915 *ppOut = (void*)sOut.aBuf;
5916 *pnOut = sOut.nBuf;
5917 sOut.aBuf = 0;
5920 sqlite3_free(sOut.aBuf);
5921 return rc;
5925 ** Create a new rebaser object.
5927 int sqlite3rebaser_create(sqlite3_rebaser **ppNew){
5928 int rc = SQLITE_OK;
5929 sqlite3_rebaser *pNew;
5931 pNew = sqlite3_malloc(sizeof(sqlite3_rebaser));
5932 if( pNew==0 ){
5933 rc = SQLITE_NOMEM;
5934 }else{
5935 memset(pNew, 0, sizeof(sqlite3_rebaser));
5937 *ppNew = pNew;
5938 return rc;
5942 ** Call this one or more times to configure a rebaser.
5944 int sqlite3rebaser_configure(
5945 sqlite3_rebaser *p,
5946 int nRebase, const void *pRebase
5948 sqlite3_changeset_iter *pIter = 0; /* Iterator opened on pData/nData */
5949 int rc; /* Return code */
5950 rc = sqlite3changeset_start(&pIter, nRebase, (void*)pRebase);
5951 if( rc==SQLITE_OK ){
5952 rc = sessionChangesetToHash(pIter, &p->grp, 1);
5954 sqlite3changeset_finalize(pIter);
5955 return rc;
5959 ** Rebase a changeset according to current rebaser configuration
5961 int sqlite3rebaser_rebase(
5962 sqlite3_rebaser *p,
5963 int nIn, const void *pIn,
5964 int *pnOut, void **ppOut
5966 sqlite3_changeset_iter *pIter = 0; /* Iterator to skip through input */
5967 int rc = sqlite3changeset_start(&pIter, nIn, (void*)pIn);
5969 if( rc==SQLITE_OK ){
5970 rc = sessionRebase(p, pIter, 0, 0, pnOut, ppOut);
5971 sqlite3changeset_finalize(pIter);
5974 return rc;
5978 ** Rebase a changeset according to current rebaser configuration
5980 int sqlite3rebaser_rebase_strm(
5981 sqlite3_rebaser *p,
5982 int (*xInput)(void *pIn, void *pData, int *pnData),
5983 void *pIn,
5984 int (*xOutput)(void *pOut, const void *pData, int nData),
5985 void *pOut
5987 sqlite3_changeset_iter *pIter = 0; /* Iterator to skip through input */
5988 int rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
5990 if( rc==SQLITE_OK ){
5991 rc = sessionRebase(p, pIter, xOutput, pOut, 0, 0);
5992 sqlite3changeset_finalize(pIter);
5995 return rc;
5999 ** Destroy a rebaser object
6001 void sqlite3rebaser_delete(sqlite3_rebaser *p){
6002 if( p ){
6003 sessionDeleteTable(0, p->grp.pList);
6004 sqlite3_free(p);
6009 ** Global configuration
6011 int sqlite3session_config(int op, void *pArg){
6012 int rc = SQLITE_OK;
6013 switch( op ){
6014 case SQLITE_SESSION_CONFIG_STRMSIZE: {
6015 int *pInt = (int*)pArg;
6016 if( *pInt>0 ){
6017 sessions_strm_chunk_size = *pInt;
6019 *pInt = sessions_strm_chunk_size;
6020 break;
6022 default:
6023 rc = SQLITE_MISUSE;
6024 break;
6026 return rc;
6029 #endif /* SQLITE_ENABLE_SESSION && SQLITE_ENABLE_PREUPDATE_HOOK */