Debian Codegraph @

ext/session/sqlite3session.c

raw
1
2 #if defined(SQLITE_ENABLE_SESSION) && defined(SQLITE_ENABLE_PREUPDATE_HOOK)
3 #include "sqlite3session.h"
4 #include <assert.h>
5 #include <string.h>
6
7 #ifndef SQLITE_AMALGAMATION
8 # include "sqliteInt.h"
9 # include "vdbeInt.h"
10 #endif
11
12 typedef struct SessionTable SessionTable;
13 typedef struct SessionChange SessionChange;
14 typedef struct SessionBuffer SessionBuffer;
15 typedef struct SessionInput SessionInput;
16
17 /*
18 ** Minimum chunk size used by streaming versions of functions.
19 */
20 #ifndef SESSIONS_STRM_CHUNK_SIZE
21 # ifdef SQLITE_TEST
22 # define SESSIONS_STRM_CHUNK_SIZE 64
23 # else
24 # define SESSIONS_STRM_CHUNK_SIZE 1024
25 # endif
26 #endif
27
28 #define SESSIONS_ROWID "_rowid_"
29
30 static int sessions_strm_chunk_size = SESSIONS_STRM_CHUNK_SIZE;
31
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*);
39 };
40
41 /*
42 ** Session handle structure.
43 */
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 */
61 };
62
63 /*
64 ** Instances of this structure are used to build strings or binary records.
65 */
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 */
70 };
71
72 /*
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()).
77 **
78 ** bNoDiscard:
79 ** If true, then the only time data is discarded is as a result of explicit
80 ** sessionDiscardData() calls. Not within every sessionInputBuffer() call.
81 */
82 struct SessionInput {
83 int bNoDiscard; /* If true, do not discard in InputBuffer() */
84 int iCurrent; /* Offset in aData[] of current change */
85 int iNext; /* Offset in aData[] of next change */
86 u8 *aData; /* Pointer to buffer containing changeset */
87 int nData; /* Number of bytes in aData */
88
89 SessionBuffer buf; /* Current read buffer */
90 int (*xInput)(void*, void*, int*); /* Input stream call (or NULL) */
91 void *pIn; /* First argument to xInput */
92 int bEof; /* Set to true after xInput finished */
93 };
94
95 /*
96 ** Structure for changeset iterators.
97 */
98 struct sqlite3_changeset_iter {
99 SessionInput in; /* Input buffer or stream */
100 SessionBuffer tblhdr; /* Buffer to hold apValue/zTab/abPK/ */
101 int bPatchset; /* True if this is a patchset */
102 int bInvert; /* True to invert changeset */
103 int bSkipEmpty; /* Skip noop UPDATE changes */
104 int rc; /* Iterator error code */
105 sqlite3_stmt *pConflict; /* Points to conflicting row, if any */
106 char *zTab; /* Current table */
107 int nCol; /* Number of columns in zTab */
108 int op; /* Current operation */
109 int bIndirect; /* True if current change was indirect */
110 u8 *abPK; /* Primary key array */
111 sqlite3_value **apValue; /* old.* and new.* values */
112 };
113
114 /*
115 ** Each session object maintains a set of the following structures, one
116 ** for each table the session object is monitoring. The structures are
117 ** stored in a linked list starting at sqlite3_session.pTable.
118 **
119 ** The keys of the SessionTable.aChange[] hash table are all rows that have
120 ** been modified in any way since the session object was attached to the
121 ** table.
122 **
123 ** The data associated with each hash-table entry is a structure containing
124 ** a subset of the initial values that the modified row contained at the
125 ** start of the session. Or no initial values if the row was inserted.
126 **
127 ** pDfltStmt:
128 ** This is only used by the sqlite3changegroup_xxx() APIs, not by
129 ** regular sqlite3_session objects. It is a SELECT statement that
130 ** selects the default value for each table column. For example,
131 ** if the table is
132 **
133 ** CREATE TABLE xx(a DEFAULT 1, b, c DEFAULT 'abc')
134 **
135 ** then this variable is the compiled version of:
136 **
137 ** SELECT 1, NULL, 'abc'
138 */
139 struct SessionTable {
140 SessionTable *pNext;
141 char *zName; /* Local name of table */
142 int nCol; /* Number of non-hidden columns */
143 int nTotalCol; /* Number of columns including hidden */
144 int bStat1; /* True if this is sqlite_stat1 */
145 int bRowid; /* True if this table uses rowid for PK */
146 const char **azCol; /* Column names */
147 const char **azDflt; /* Default value expressions */
148 int *aiIdx; /* Index to pass to xNew/xOld */
149 u8 *abPK; /* Array of primary key flags */
150 int nEntry; /* Total number of entries in hash table */
151 int nChange; /* Size of apChange[] array */
152 SessionChange **apChange; /* Hash table buckets */
153 sqlite3_stmt *pDfltStmt;
154 };
155
156 /*
157 ** RECORD FORMAT:
158 **
159 ** The following record format is similar to (but not compatible with) that
160 ** used in SQLite database files. This format is used as part of the
161 ** change-set binary format, and so must be architecture independent.
162 **
163 ** Unlike the SQLite database record format, each field is self-contained -
164 ** there is no separation of header and data. Each field begins with a
165 ** single byte describing its type, as follows:
166 **
167 ** 0x00: Undefined value.
168 ** 0x01: Integer value.
169 ** 0x02: Real value.
170 ** 0x03: Text value.
171 ** 0x04: Blob value.
172 ** 0x05: SQL NULL value.
173 **
174 ** Note that the above match the definitions of SQLITE_INTEGER, SQLITE_TEXT
175 ** and so on in sqlite3.h. For undefined and NULL values, the field consists
176 ** only of the single type byte. For other types of values, the type byte
177 ** is followed by:
178 **
179 ** Text values:
180 ** A varint containing the number of bytes in the value (encoded using
181 ** UTF-8). Followed by a buffer containing the UTF-8 representation
182 ** of the text value. There is no nul terminator.
183 **
184 ** Blob values:
185 ** A varint containing the number of bytes in the value, followed by
186 ** a buffer containing the value itself.
187 **
188 ** Integer values:
189 ** An 8-byte big-endian integer value.
190 **
191 ** Real values:
192 ** An 8-byte big-endian IEEE 754-2008 real value.
193 **
194 ** Varint values are encoded in the same way as varints in the SQLite
195 ** record format.
196 **
197 ** CHANGESET FORMAT:
198 **
199 ** A changeset is a collection of DELETE, UPDATE and INSERT operations on
200 ** one or more tables. Operations on a single table are grouped together,
201 ** but may occur in any order (i.e. deletes, updates and inserts are all
202 ** mixed together).
203 **
204 ** Each group of changes begins with a table header:
205 **
206 ** 1 byte: Constant 0x54 (capital 'T')
207 ** Varint: Number of columns in the table.
208 ** nCol bytes: 0x01 for PK columns, 0x00 otherwise.
209 ** N bytes: Unqualified table name (encoded using UTF-8). Nul-terminated.
210 **
211 ** Followed by one or more changes to the table.
212 **
213 ** 1 byte: Either SQLITE_INSERT (0x12), UPDATE (0x17) or DELETE (0x09).
214 ** 1 byte: The "indirect-change" flag.
215 ** old.* record: (delete and update only)
216 ** new.* record: (insert and update only)
217 **
218 ** The "old.*" and "new.*" records, if present, are N field records in the
219 ** format described above under "RECORD FORMAT", where N is the number of
220 ** columns in the table. The i'th field of each record is associated with
221 ** the i'th column of the table, counting from left to right in the order
222 ** in which columns were declared in the CREATE TABLE statement.
223 **
224 ** The new.* record that is part of each INSERT change contains the values
225 ** that make up the new row. Similarly, the old.* record that is part of each
226 ** DELETE change contains the values that made up the row that was deleted
227 ** from the database. In the changeset format, the records that are part
228 ** of INSERT or DELETE changes never contain any undefined (type byte 0x00)
229 ** fields.
230 **
231 ** Within the old.* record associated with an UPDATE change, all fields
232 ** associated with table columns that are not PRIMARY KEY columns and are
233 ** not modified by the UPDATE change are set to "undefined". Other fields
234 ** are set to the values that made up the row before the UPDATE that the
235 ** change records took place. Within the new.* record, fields associated
236 ** with table columns modified by the UPDATE change contain the new
237 ** values. Fields associated with table columns that are not modified
238 ** are set to "undefined".
239 **
240 ** PATCHSET FORMAT:
241 **
242 ** A patchset is also a collection of changes. It is similar to a changeset,
243 ** but leaves undefined those fields that are not useful if no conflict
244 ** resolution is required when applying the changeset.
245 **
246 ** Each group of changes begins with a table header:
247 **
248 ** 1 byte: Constant 0x50 (capital 'P')
249 ** Varint: Number of columns in the table.
250 ** nCol bytes: 0x01 for PK columns, 0x00 otherwise.
251 ** N bytes: Unqualified table name (encoded using UTF-8). Nul-terminated.
252 **
253 ** Followed by one or more changes to the table.
254 **
255 ** 1 byte: Either SQLITE_INSERT (0x12), UPDATE (0x17) or DELETE (0x09).
256 ** 1 byte: The "indirect-change" flag.
257 ** single record: (PK fields for DELETE, PK and modified fields for UPDATE,
258 ** full record for INSERT).
259 **
260 ** As in the changeset format, each field of the single record that is part
261 ** of a patchset change is associated with the correspondingly positioned
262 ** table column, counting from left to right within the CREATE TABLE
263 ** statement.
264 **
265 ** For a DELETE change, all fields within the record except those associated
266 ** with PRIMARY KEY columns are omitted. The PRIMARY KEY fields contain the
267 ** values identifying the row to delete.
268 **
269 ** For an UPDATE change, all fields except those associated with PRIMARY KEY
270 ** columns and columns that are modified by the UPDATE are set to "undefined".
271 ** PRIMARY KEY fields contain the values identifying the table row to update,
272 ** and fields associated with modified columns contain the new column values.
273 **
274 ** The records associated with INSERT changes are in the same format as for
275 ** changesets. It is not possible for a record associated with an INSERT
276 ** change to contain a field set to "undefined".
277 **
278 ** REBASE BLOB FORMAT:
279 **
280 ** A rebase blob may be output by sqlite3changeset_apply_v2() and its
281 ** streaming equivalent for use with the sqlite3_rebaser APIs to rebase
282 ** existing changesets. A rebase blob contains one entry for each conflict
283 ** resolved using either the OMIT or REPLACE strategies within the apply_v2()
284 ** call.
285 **
286 ** The format used for a rebase blob is very similar to that used for
287 ** changesets. All entries related to a single table are grouped together.
288 **
289 ** Each group of entries begins with a table header in changeset format:
290 **
291 ** 1 byte: Constant 0x54 (capital 'T')
292 ** Varint: Number of columns in the table.
293 ** nCol bytes: 0x01 for PK columns, 0x00 otherwise.
294 ** N bytes: Unqualified table name (encoded using UTF-8). Nul-terminated.
295 **
296 ** Followed by one or more entries associated with the table.
297 **
298 ** 1 byte: Either SQLITE_INSERT (0x12), DELETE (0x09).
299 ** 1 byte: Flag. 0x01 for REPLACE, 0x00 for OMIT.
300 ** record: (in the record format defined above).
301 **
302 ** In a rebase blob, the first field is set to SQLITE_INSERT if the change
303 ** that caused the conflict was an INSERT or UPDATE, or to SQLITE_DELETE if
304 ** it was a DELETE. The second field is set to 0x01 if the conflict
305 ** resolution strategy was REPLACE, or 0x00 if it was OMIT.
306 **
307 ** If the change that caused the conflict was a DELETE, then the single
308 ** record is a copy of the old.* record from the original changeset. If it
309 ** was an INSERT, then the single record is a copy of the new.* record. If
310 ** the conflicting change was an UPDATE, then the single record is a copy
311 ** of the new.* record with the PK fields filled in based on the original
312 ** old.* record.
313 */
314
315 /*
316 ** For each row modified during a session, there exists a single instance of
317 ** this structure stored in a SessionTable.aChange[] hash table.
318 */
319 struct SessionChange {
320 u8 op; /* One of UPDATE, DELETE, INSERT */
321 u8 bIndirect; /* True if this change is "indirect" */
322 u16 nRecordField; /* Number of fields in aRecord[] */
323 int nMaxSize; /* Max size of eventual changeset record */
324 int nRecord; /* Number of bytes in buffer aRecord[] */
325 u8 *aRecord; /* Buffer containing old.* record */
326 SessionChange *pNext; /* For hash-table collisions */
327 };
328
329 /*
330 ** Write a varint with value iVal into the buffer at aBuf. Return the
331 ** number of bytes written.
332 */
333 static int sessionVarintPut(u8 *aBuf, int iVal){
334 return putVarint32(aBuf, iVal);
335 }
336
337 /*
338 ** Return the number of bytes required to store value iVal as a varint.
339 */
340 static int sessionVarintLen(int iVal){
341 return sqlite3VarintLen(iVal);
342 }
343
344 /*
345 ** Read a varint value from aBuf[] into *piVal. Return the number of
346 ** bytes read.
347 */
348 static int sessionVarintGet(const u8 *aBuf, int *piVal){
349 return getVarint32(aBuf, *piVal);
350 }
351
352 /*
353 ** Read a varint value from buffer aBuf[], size nBuf bytes, into *piVal.
354 ** Return the number of bytes read.
355 */
356 static int sessionVarintGetSafe(const u8 *aBuf, int nBuf, int *piVal){
357 u8 aCopy[9];
358 const u8 *aRead = aBuf;
359 memset(aCopy, 0, sizeof(aCopy));
360 if( nBuf<sizeof(aCopy) ){
361 memcpy(aCopy, aBuf, nBuf);
362 aRead = aCopy;
363 }
364 return getVarint32(aRead, *piVal);
365 }
366
367 /* Load an unaligned and unsigned 32-bit integer */
368 #define SESSION_UINT32(x) (((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
369
370 /*
371 ** Read a 64-bit big-endian integer value from buffer aRec[]. Return
372 ** the value read.
373 */
374 static sqlite3_int64 sessionGetI64(u8 *aRec){
375 u64 x = SESSION_UINT32(aRec);
376 u32 y = SESSION_UINT32(aRec+4);
377 x = (x<<32) + y;
378 return (sqlite3_int64)x;
379 }
380
381 /*
382 ** Write a 64-bit big-endian integer value to the buffer aBuf[].
383 */
384 static void sessionPutI64(u8 *aBuf, sqlite3_int64 i){
385 aBuf[0] = (i>>56) & 0xFF;
386 aBuf[1] = (i>>48) & 0xFF;
387 aBuf[2] = (i>>40) & 0xFF;
388 aBuf[3] = (i>>32) & 0xFF;
389 aBuf[4] = (i>>24) & 0xFF;
390 aBuf[5] = (i>>16) & 0xFF;
391 aBuf[6] = (i>> 8) & 0xFF;
392 aBuf[7] = (i>> 0) & 0xFF;
393 }
394
395 /*
396 ** Write a double value to the buffer aBuf[].
397 */
398 static void sessionPutDouble(u8 *aBuf, double r){
399 /* TODO: SQLite does something special to deal with mixed-endian
400 ** floating point values (e.g. ARM7). This code probably should
401 ** too. */
402 u64 i;
403 assert( sizeof(double)==8 && sizeof(u64)==8 );
404 memcpy(&i, &r, 8);
405 sessionPutI64(aBuf, i);
406 }
407
408 /*
409 ** This function is used to serialize the contents of value pValue (see
410 ** comment titled "RECORD FORMAT" above).
411 **
412 ** If it is non-NULL, the serialized form of the value is written to
413 ** buffer aBuf. *pnWrite is set to the number of bytes written before
414 ** returning. Or, if aBuf is NULL, the only thing this function does is
415 ** set *pnWrite.
416 **
417 ** If no error occurs, SQLITE_OK is returned. Or, if an OOM error occurs
418 ** within a call to sqlite3_value_text() (may fail if the db is utf-16))
419 ** SQLITE_NOMEM is returned.
420 */
421 static int sessionSerializeValue(
422 u8 *aBuf, /* If non-NULL, write serialized value here */
423 sqlite3_value *pValue, /* Value to serialize */
424 sqlite3_int64 *pnWrite /* IN/OUT: Increment by bytes written */
425 ){
426 int nByte; /* Size of serialized value in bytes */
427
428 if( pValue ){
429 int eType; /* Value type (SQLITE_NULL, TEXT etc.) */
430
431 eType = sqlite3_value_type(pValue);
432 if( aBuf ) aBuf[0] = eType;
433
434 switch( eType ){
435 case SQLITE_NULL:
436 nByte = 1;
437 break;
438
439 case SQLITE_INTEGER:
440 case SQLITE_FLOAT:
441 if( aBuf ){
442 /* TODO: SQLite does something special to deal with mixed-endian
443 ** floating point values (e.g. ARM7). This code probably should
444 ** too. */
445 if( eType==SQLITE_INTEGER ){
446 u64 i = (u64)sqlite3_value_int64(pValue);
447 sessionPutI64(&aBuf[1], i);
448 }else{
449 double r = sqlite3_value_double(pValue);
450 sessionPutDouble(&aBuf[1], r);
451 }
452 }
453 nByte = 9;
454 break;
455
456 default: {
457 u8 *z;
458 int n;
459 int nVarint;
460
461 assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
462 if( eType==SQLITE_TEXT ){
463 z = (u8 *)sqlite3_value_text(pValue);
464 }else{
465 z = (u8 *)sqlite3_value_blob(pValue);
466 }
467 n = sqlite3_value_bytes(pValue);
468 if( z==0 && (eType!=SQLITE_BLOB || n>0) ) return SQLITE_NOMEM;
469 nVarint = sessionVarintLen(n);
470
471 if( aBuf ){
472 sessionVarintPut(&aBuf[1], n);
473 if( n>0 ) memcpy(&aBuf[nVarint + 1], z, n);
474 }
475
476 nByte = 1 + nVarint + n;
477 break;
478 }
479 }
480 }else{
481 nByte = 1;
482 if( aBuf ) aBuf[0] = '\0';
483 }
484
485 if( pnWrite ) *pnWrite += nByte;
486 return SQLITE_OK;
487 }
488
489 /*
490 ** Allocate and return a pointer to a buffer nByte bytes in size. If
491 ** pSession is not NULL, increase the sqlite3_session.nMalloc variable
492 ** by the number of bytes allocated.
493 */
494 static void *sessionMalloc64(sqlite3_session *pSession, i64 nByte){
495 void *pRet = sqlite3_malloc64(nByte);
496 if( pSession ) pSession->nMalloc += sqlite3_msize(pRet);
497 return pRet;
498 }
499
500 /*
501 ** Free buffer pFree, which must have been allocated by an earlier
502 ** call to sessionMalloc64(). If pSession is not NULL, decrease the
503 ** sqlite3_session.nMalloc counter by the number of bytes freed.
504 */
505 static void sessionFree(sqlite3_session *pSession, void *pFree){
506 if( pSession ) pSession->nMalloc -= sqlite3_msize(pFree);
507 sqlite3_free(pFree);
508 }
509
510 /*
511 ** This macro is used to calculate hash key values for data structures. In
512 ** order to use this macro, the entire data structure must be represented
513 ** as a series of unsigned integers. In order to calculate a hash-key value
514 ** for a data structure represented as three such integers, the macro may
515 ** then be used as follows:
516 **
517 ** int hash_key_value;
518 ** hash_key_value = HASH_APPEND(0, <value 1>);
519 ** hash_key_value = HASH_APPEND(hash_key_value, <value 2>);
520 ** hash_key_value = HASH_APPEND(hash_key_value, <value 3>);
521 **
522 ** In practice, the data structures this macro is used for are the primary
523 ** key values of modified rows.
524 */
525 #define HASH_APPEND(hash, add) ((hash) << 3) ^ (hash) ^ (unsigned int)(add)
526
527 /*
528 ** Append the hash of the 64-bit integer passed as the second argument to the
529 ** hash-key value passed as the first. Return the new hash-key value.
530 */
531 static unsigned int sessionHashAppendI64(unsigned int h, i64 i){
532 h = HASH_APPEND(h, i & 0xFFFFFFFF);
533 return HASH_APPEND(h, (i>>32)&0xFFFFFFFF);
534 }
535
536 /*
537 ** Append the hash of the blob passed via the second and third arguments to
538 ** the hash-key value passed as the first. Return the new hash-key value.
539 */
540 static unsigned int sessionHashAppendBlob(unsigned int h, int n, const u8 *z){
541 int i;
542 for(i=0; i<n; i++) h = HASH_APPEND(h, z[i]);
543 return h;
544 }
545
546 /*
547 ** Append the hash of the data type passed as the second argument to the
548 ** hash-key value passed as the first. Return the new hash-key value.
549 */
550 static unsigned int sessionHashAppendType(unsigned int h, int eType){
551 return HASH_APPEND(h, eType);
552 }
553
554 /*
555 ** This function may only be called from within a pre-update callback.
556 ** It calculates a hash based on the primary key values of the old.* or
557 ** new.* row currently available and, assuming no error occurs, writes it to
558 ** *piHash before returning. If the primary key contains one or more NULL
559 ** values, *pbNullPK is set to true before returning.
560 **
561 ** If an error occurs, an SQLite error code is returned and the final values
562 ** of *piHash asn *pbNullPK are undefined. Otherwise, SQLITE_OK is returned
563 ** and the output variables are set as described above.
564 */
565 static int sessionPreupdateHash(
566 sqlite3_session *pSession, /* Session object that owns pTab */
567 i64 iRowid,
568 SessionTable *pTab, /* Session table handle */
569 int bNew, /* True to hash the new.* PK */
570 int *piHash, /* OUT: Hash value */
571 int *pbNullPK /* OUT: True if there are NULL values in PK */
572 ){
573 unsigned int h = 0; /* Hash value to return */
574 int i; /* Used to iterate through columns */
575
576 assert( pTab->nTotalCol==pSession->hook.xCount(pSession->hook.pCtx) );
577 if( pTab->bRowid ){
578 h = sessionHashAppendI64(h, iRowid);
579 }else{
580 assert( *pbNullPK==0 );
581 for(i=0; i<pTab->nCol; i++){
582 if( pTab->abPK[i] ){
583 int rc;
584 int eType;
585 sqlite3_value *pVal;
586 int iIdx = pTab->aiIdx[i];
587
588 if( bNew ){
589 rc = pSession->hook.xNew(pSession->hook.pCtx, iIdx, &pVal);
590 }else{
591 rc = pSession->hook.xOld(pSession->hook.pCtx, iIdx, &pVal);
592 }
593 if( rc!=SQLITE_OK ) return rc;
594
595 eType = sqlite3_value_type(pVal);
596 h = sessionHashAppendType(h, eType);
597 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
598 i64 iVal;
599 if( eType==SQLITE_INTEGER ){
600 iVal = sqlite3_value_int64(pVal);
601 }else{
602 double rVal = sqlite3_value_double(pVal);
603 assert( sizeof(iVal)==8 && sizeof(rVal)==8 );
604 memcpy(&iVal, &rVal, 8);
605 }
606 h = sessionHashAppendI64(h, iVal);
607 }else if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
608 const u8 *z;
609 int n;
610 if( eType==SQLITE_TEXT ){
611 z = (const u8 *)sqlite3_value_text(pVal);
612 }else{
613 z = (const u8 *)sqlite3_value_blob(pVal);
614 }
615 n = sqlite3_value_bytes(pVal);
616 if( !z && (eType!=SQLITE_BLOB || n>0) ) return SQLITE_NOMEM;
617 h = sessionHashAppendBlob(h, n, z);
618 }else{
619 assert( eType==SQLITE_NULL );
620 assert( pTab->bStat1==0 || i!=1 );
621 *pbNullPK = 1;
622 }
623 }
624 }
625 }
626
627 *piHash = (h % pTab->nChange);
628 return SQLITE_OK;
629 }
630
631 /*
632 ** The buffer that the argument points to contains a serialized SQL value.
633 ** Return the number of bytes of space occupied by the value (including
634 ** the type byte).
635 */
636 static int sessionSerialLen(const u8 *a){
637 int e;
638 int n;
639 assert( a!=0 );
640 e = *a;
641 if( e==SQLITE_INTEGER || e==SQLITE_FLOAT ) return 9;
642 if( e==SQLITE_TEXT || e==SQLITE_BLOB ){
643 return sessionVarintGet(&a[1], &n) + 1 + n;
644 }
645 return 1;
646 }
647
648 /*
649 ** Based on the primary key values stored in change aRecord, calculate a
650 ** hash key. Assume the has table has nBucket buckets. The hash keys
651 ** calculated by this function are compatible with those calculated by
652 ** sessionPreupdateHash().
653 **
654 ** The bPkOnly argument is non-zero if the record at aRecord[] is from
655 ** a patchset DELETE. In this case the non-PK fields are omitted entirely.
656 */
657 static unsigned int sessionChangeHash(
658 SessionTable *pTab, /* Table handle */
659 int bPkOnly, /* Record consists of PK fields only */
660 u8 *aRecord, /* Change record */
661 int nBucket /* Assume this many buckets in hash table */
662 ){
663 unsigned int h = 0; /* Value to return */
664 int i; /* Used to iterate through columns */
665 u8 *a = aRecord; /* Used to iterate through change record */
666
667 for(i=0; i<pTab->nCol; i++){
668 int isPK = pTab->abPK[i];
669 if( bPkOnly && isPK==0 ) continue;
670
671 if( isPK ){
672 int eType = *a++;
673
674 assert( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT
675 || eType==SQLITE_TEXT || eType==SQLITE_BLOB
676 || eType==SQLITE_NULL || eType==0
677 );
678
679 h = sessionHashAppendType(h, eType);
680 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
681 h = sessionHashAppendI64(h, sessionGetI64(a));
682 a += 8;
683 }else if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
684 int n;
685 a += sessionVarintGet(a, &n);
686 h = sessionHashAppendBlob(h, n, a);
687 a += n;
688 }
689 /* It should not be possible for eType to be SQLITE_NULL or 0x00 here,
690 ** as the session module does not record changes for rows with NULL
691 ** values stored in primary key columns. But a corrupt changesets
692 ** may contain such a value. */
693 }else{
694 a += sessionSerialLen(a);
695 }
696 }
697 return (h % nBucket);
698 }
699
700 /*
701 ** Arguments aLeft and aRight are pointers to change records for table pTab.
702 ** This function returns true if the two records apply to the same row (i.e.
703 ** have the same values stored in the primary key columns), or false
704 ** otherwise.
705 */
706 static int sessionChangeEqual(
707 SessionTable *pTab, /* Table used for PK definition */
708 int bLeftPkOnly, /* True if aLeft[] contains PK fields only */
709 u8 *aLeft, /* Change record */
710 int bRightPkOnly, /* True if aRight[] contains PK fields only */
711 u8 *aRight /* Change record */
712 ){
713 u8 *a1 = aLeft; /* Cursor to iterate through aLeft */
714 u8 *a2 = aRight; /* Cursor to iterate through aRight */
715 int iCol; /* Used to iterate through table columns */
716
717 for(iCol=0; iCol<pTab->nCol; iCol++){
718 if( pTab->abPK[iCol] ){
719 int n1 = sessionSerialLen(a1);
720 int n2 = sessionSerialLen(a2);
721
722 if( n1!=n2 || memcmp(a1, a2, n1) ){
723 return 0;
724 }
725 a1 += n1;
726 a2 += n2;
727 }else{
728 if( bLeftPkOnly==0 ) a1 += sessionSerialLen(a1);
729 if( bRightPkOnly==0 ) a2 += sessionSerialLen(a2);
730 }
731 }
732
733 return 1;
734 }
735
736 /*
737 ** Arguments aLeft and aRight both point to buffers containing change
738 ** records with nCol columns. This function "merges" the two records into
739 ** a single records which is written to the buffer at *paOut. *paOut is
740 ** then set to point to one byte after the last byte written before
741 ** returning.
742 **
743 ** The merging of records is done as follows: For each column, if the
744 ** aRight record contains a value for the column, copy the value from
745 ** their. Otherwise, if aLeft contains a value, copy it. If neither
746 ** record contains a value for a given column, then neither does the
747 ** output record.
748 */
749 static void sessionMergeRecord(
750 u8 **paOut,
751 int nCol,
752 u8 *aLeft,
753 u8 *aRight
754 ){
755 u8 *a1 = aLeft; /* Cursor used to iterate through aLeft */
756 u8 *a2 = aRight; /* Cursor used to iterate through aRight */
757 u8 *aOut = *paOut; /* Output cursor */
758 int iCol; /* Used to iterate from 0 to nCol */
759
760 for(iCol=0; iCol<nCol; iCol++){
761 int n1 = sessionSerialLen(a1);
762 int n2 = sessionSerialLen(a2);
763 if( *a2 ){
764 memcpy(aOut, a2, n2);
765 aOut += n2;
766 }else{
767 memcpy(aOut, a1, n1);
768 aOut += n1;
769 }
770 a1 += n1;
771 a2 += n2;
772 }
773
774 *paOut = aOut;
775 }
776
777 /*
778 ** This is a helper function used by sessionMergeUpdate().
779 **
780 ** When this function is called, both *paOne and *paTwo point to a value
781 ** within a change record. Before it returns, both have been advanced so
782 ** as to point to the next value in the record.
783 **
784 ** If, when this function is called, *paTwo points to a valid value (i.e.
785 ** *paTwo[0] is not 0x00 - the "no value" placeholder), a copy of the *paTwo
786 ** pointer is returned and *pnVal is set to the number of bytes in the
787 ** serialized value. Otherwise, a copy of *paOne is returned and *pnVal
788 ** set to the number of bytes in the value at *paOne. If *paOne points
789 ** to the "no value" placeholder, *pnVal is set to 1. In other words:
790 **
791 ** if( *paTwo is valid ) return *paTwo;
792 ** return *paOne;
793 **
794 */
795 static u8 *sessionMergeValue(
796 u8 **paOne, /* IN/OUT: Left-hand buffer pointer */
797 u8 **paTwo, /* IN/OUT: Right-hand buffer pointer */
798 int *pnVal /* OUT: Bytes in returned value */
799 ){
800 u8 *a1 = *paOne;
801 u8 *a2 = *paTwo;
802 u8 *pRet = 0;
803 int n1;
804
805 assert( a1 );
806 if( a2 ){
807 int n2 = sessionSerialLen(a2);
808 if( *a2 ){
809 *pnVal = n2;
810 pRet = a2;
811 }
812 *paTwo = &a2[n2];
813 }
814
815 n1 = sessionSerialLen(a1);
816 if( pRet==0 ){
817 *pnVal = n1;
818 pRet = a1;
819 }
820 *paOne = &a1[n1];
821
822 return pRet;
823 }
824
825 /*
826 ** This function is used by changeset_concat() to merge two UPDATE changes
827 ** on the same row.
828 */
829 static int sessionMergeUpdate(
830 u8 **paOut, /* IN/OUT: Pointer to output buffer */
831 SessionTable *pTab, /* Table change pertains to */
832 int bPatchset, /* True if records are patchset records */
833 u8 *aOldRecord1, /* old.* record for first change */
834 u8 *aOldRecord2, /* old.* record for second change */
835 u8 *aNewRecord1, /* new.* record for first change */
836 u8 *aNewRecord2 /* new.* record for second change */
837 ){
838 u8 *aOld1 = aOldRecord1;
839 u8 *aOld2 = aOldRecord2;
840 u8 *aNew1 = aNewRecord1;
841 u8 *aNew2 = aNewRecord2;
842
843 u8 *aOut = *paOut;
844 int i;
845
846 if( bPatchset==0 ){
847 int bRequired = 0;
848
849 assert( aOldRecord1 && aNewRecord1 );
850
851 /* Write the old.* vector first. */
852 for(i=0; i<pTab->nCol; i++){
853 int nOld;
854 u8 *aOld;
855 int nNew;
856 u8 *aNew;
857
858 aOld = sessionMergeValue(&aOld1, &aOld2, &nOld);
859 aNew = sessionMergeValue(&aNew1, &aNew2, &nNew);
860 if( pTab->abPK[i] || nOld!=nNew || memcmp(aOld, aNew, nNew) ){
861 if( pTab->abPK[i]==0 ) bRequired = 1;
862 memcpy(aOut, aOld, nOld);
863 aOut += nOld;
864 }else{
865 *(aOut++) = '\0';
866 }
867 }
868
869 if( !bRequired ) return 0;
870 }
871
872 /* Write the new.* vector */
873 aOld1 = aOldRecord1;
874 aOld2 = aOldRecord2;
875 aNew1 = aNewRecord1;
876 aNew2 = aNewRecord2;
877 for(i=0; i<pTab->nCol; i++){
878 int nOld;
879 u8 *aOld;
880 int nNew;
881 u8 *aNew;
882
883 aOld = sessionMergeValue(&aOld1, &aOld2, &nOld);
884 aNew = sessionMergeValue(&aNew1, &aNew2, &nNew);
885 if( bPatchset==0
886 && (pTab->abPK[i] || (nOld==nNew && 0==memcmp(aOld, aNew, nNew)))
887 ){
888 *(aOut++) = '\0';
889 }else{
890 memcpy(aOut, aNew, nNew);
891 aOut += nNew;
892 }
893 }
894
895 *paOut = aOut;
896 return 1;
897 }
898
899 /*
900 ** This function is only called from within a pre-update-hook callback.
901 ** It determines if the current pre-update-hook change affects the same row
902 ** as the change stored in argument pChange. If so, it returns true. Otherwise
903 ** if the pre-update-hook does not affect the same row as pChange, it returns
904 ** false.
905 */
906 static int sessionPreupdateEqual(
907 sqlite3_session *pSession, /* Session object that owns SessionTable */
908 i64 iRowid, /* Rowid value if pTab->bRowid */
909 SessionTable *pTab, /* Table associated with change */
910 SessionChange *pChange, /* Change to compare to */
911 int op /* Current pre-update operation */
912 ){
913 int iCol; /* Used to iterate through columns */
914 u8 *a = pChange->aRecord; /* Cursor used to scan change record */
915
916 if( pTab->bRowid ){
917 if( a[0]!=SQLITE_INTEGER ) return 0;
918 return sessionGetI64(&a[1])==iRowid;
919 }
920
921 assert( op==SQLITE_INSERT || op==SQLITE_UPDATE || op==SQLITE_DELETE );
922 for(iCol=0; iCol<pTab->nCol; iCol++){
923 if( !pTab->abPK[iCol] ){
924 a += sessionSerialLen(a);
925 }else{
926 sqlite3_value *pVal; /* Value returned by preupdate_new/old */
927 int rc; /* Error code from preupdate_new/old */
928 int eType = *a++; /* Type of value from change record */
929 int iIdx = pTab->aiIdx[iCol];
930
931 /* The following calls to preupdate_new() and preupdate_old() can not
932 ** fail. This is because they cache their return values, and by the
933 ** time control flows to here they have already been called once from
934 ** within sessionPreupdateHash(). The first two asserts below verify
935 ** this (that the method has already been called). */
936 if( op==SQLITE_INSERT ){
937 /* assert( db->pPreUpdate->pNewUnpacked || db->pPreUpdate->aNew ); */
938 rc = pSession->hook.xNew(pSession->hook.pCtx, iIdx, &pVal);
939 }else{
940 /* assert( db->pPreUpdate->pUnpacked ); */
941 rc = pSession->hook.xOld(pSession->hook.pCtx, iIdx, &pVal);
942 }
943 assert( rc==SQLITE_OK );
944 (void)rc; /* Suppress warning about unused variable */
945 if( sqlite3_value_type(pVal)!=eType ) return 0;
946
947 /* A SessionChange object never has a NULL value in a PK column */
948 assert( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT
949 || eType==SQLITE_BLOB || eType==SQLITE_TEXT
950 );
951
952 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
953 i64 iVal = sessionGetI64(a);
954 a += 8;
955 if( eType==SQLITE_INTEGER ){
956 if( sqlite3_value_int64(pVal)!=iVal ) return 0;
957 }else{
958 double rVal;
959 assert( sizeof(iVal)==8 && sizeof(rVal)==8 );
960 memcpy(&rVal, &iVal, 8);
961 if( sqlite3_value_double(pVal)!=rVal ) return 0;
962 }
963 }else{
964 int n;
965 const u8 *z;
966 a += sessionVarintGet(a, &n);
967 if( sqlite3_value_bytes(pVal)!=n ) return 0;
968 if( eType==SQLITE_TEXT ){
969 z = sqlite3_value_text(pVal);
970 }else{
971 z = sqlite3_value_blob(pVal);
972 }
973 if( n>0 && memcmp(a, z, n) ) return 0;
974 a += n;
975 }
976 }
977 }
978
979 return 1;
980 }
981
982 /*
983 ** If required, grow the hash table used to store changes on table pTab
984 ** (part of the session pSession). If a fatal OOM error occurs, set the
985 ** session object to failed and return SQLITE_ERROR. Otherwise, return
986 ** SQLITE_OK.
987 **
988 ** It is possible that a non-fatal OOM error occurs in this function. In
989 ** that case the hash-table does not grow, but SQLITE_OK is returned anyway.
990 ** Growing the hash table in this case is a performance optimization only,
991 ** it is not required for correct operation.
992 */
993 static int sessionGrowHash(
994 sqlite3_session *pSession, /* For memory accounting. May be NULL */
995 int bPatchset,
996 SessionTable *pTab
997 ){
998 if( pTab->nChange==0 || pTab->nEntry>=(pTab->nChange/2) ){
999 int i;
1000 SessionChange **apNew;
1001 sqlite3_int64 nNew = 2*(sqlite3_int64)(pTab->nChange ? pTab->nChange : 128);
1002
1003 apNew = (SessionChange**)sessionMalloc64(
1004 pSession, sizeof(SessionChange*) * nNew
1005 );
1006 if( apNew==0 ){
1007 if( pTab->nChange==0 ){
1008 return SQLITE_ERROR;
1009 }
1010 return SQLITE_OK;
1011 }
1012 memset(apNew, 0, sizeof(SessionChange *) * nNew);
1013
1014 for(i=0; i<pTab->nChange; i++){
1015 SessionChange *p;
1016 SessionChange *pNext;
1017 for(p=pTab->apChange[i]; p; p=pNext){
1018 int bPkOnly = (p->op==SQLITE_DELETE && bPatchset);
1019 int iHash = sessionChangeHash(pTab, bPkOnly, p->aRecord, nNew);
1020 pNext = p->pNext;
1021 p->pNext = apNew[iHash];
1022 apNew[iHash] = p;
1023 }
1024 }
1025
1026 sessionFree(pSession, pTab->apChange);
1027 pTab->nChange = nNew;
1028 pTab->apChange = apNew;
1029 }
1030
1031 return SQLITE_OK;
1032 }
1033
1034 /*
1035 ** This function queries the database for the names of the columns of table
1036 ** zThis, in schema zDb.
1037 **
1038 ** Otherwise, if they are not NULL, variable *pnCol is set to the number
1039 ** of columns in the database table and variable *pzTab is set to point to a
1040 ** nul-terminated copy of the table name. *pazCol (if not NULL) is set to
1041 ** point to an array of pointers to column names. And *pabPK (again, if not
1042 ** NULL) is set to point to an array of booleans - true if the corresponding
1043 ** column is part of the primary key.
1044 **
1045 ** For example, if the table is declared as:
1046 **
1047 ** CREATE TABLE tbl1(w, x DEFAULT 'abc', y, z, PRIMARY KEY(w, z));
1048 **
1049 ** Then the five output variables are populated as follows:
1050 **
1051 ** *pnCol = 4
1052 ** *pzTab = "tbl1"
1053 ** *pazCol = {"w", "x", "y", "z"}
1054 ** *pazDflt = {NULL, 'abc', NULL, NULL}
1055 ** *pabPK = {1, 0, 0, 1}
1056 **
1057 ** All returned buffers are part of the same single allocation, which must
1058 ** be freed using sqlite3_free() by the caller
1059 */
1060 static int sessionTableInfo(
1061 sqlite3_session *pSession, /* For memory accounting. May be NULL */
1062 sqlite3 *db, /* Database connection */
1063 const char *zDb, /* Name of attached database (e.g. "main") */
1064 const char *zThis, /* Table name */
1065 int *pnCol, /* OUT: number of columns */
1066 int *pnTotalCol, /* OUT: number of hidden columns */
1067 const char **pzTab, /* OUT: Copy of zThis */
1068 const char ***pazCol, /* OUT: Array of column names for table */
1069 const char ***pazDflt, /* OUT: Array of default value expressions */
1070 int **paiIdx, /* OUT: Array of xNew/xOld indexes */
1071 u8 **pabPK, /* OUT: Array of booleans - true for PK col */
1072 int *pbRowid /* OUT: True if only PK is a rowid */
1073 ){
1074 char *zPragma;
1075 sqlite3_stmt *pStmt;
1076 int rc;
1077 sqlite3_int64 nByte;
1078 int nDbCol = 0;
1079 int nThis;
1080 int i;
1081 u8 *pAlloc = 0;
1082 char **azCol = 0;
1083 char **azDflt = 0;
1084 u8 *abPK = 0;
1085 int *aiIdx = 0;
1086 int bRowid = 0; /* Set to true to use rowid as PK */
1087
1088 assert( pazCol && pabPK );
1089
1090 *pazCol = 0;
1091 *pabPK = 0;
1092 *pnCol = 0;
1093 if( pnTotalCol ) *pnTotalCol = 0;
1094 if( paiIdx ) *paiIdx = 0;
1095 if( pzTab ) *pzTab = 0;
1096 if( pazDflt ) *pazDflt = 0;
1097
1098 nThis = sqlite3Strlen30(zThis);
1099 if( nThis==12 && 0==sqlite3_stricmp("sqlite_stat1", zThis) ){
1100 rc = sqlite3_table_column_metadata(db, zDb, zThis, 0, 0, 0, 0, 0, 0);
1101 if( rc==SQLITE_OK ){
1102 /* For sqlite_stat1, pretend that (tbl,idx) is the PRIMARY KEY. */
1103 zPragma = sqlite3_mprintf(
1104 "SELECT 0, 'tbl', '', 0, '', 1, 0 UNION ALL "
1105 "SELECT 1, 'idx', '', 0, '', 2, 0 UNION ALL "
1106 "SELECT 2, 'stat', '', 0, '', 0, 0"
1107 );
1108 }else if( rc==SQLITE_ERROR ){
1109 zPragma = sqlite3_mprintf("");
1110 }else{
1111 return rc;
1112 }
1113 }else{
1114 zPragma = sqlite3_mprintf("PRAGMA '%q'.table_xinfo('%q')", zDb, zThis);
1115 }
1116 if( !zPragma ){
1117 return SQLITE_NOMEM;
1118 }
1119
1120 rc = sqlite3_prepare_v2(db, zPragma, -1, &pStmt, 0);
1121 sqlite3_free(zPragma);
1122 if( rc!=SQLITE_OK ){
1123 return rc;
1124 }
1125
1126 nByte = nThis + 1;
1127 bRowid = (pbRowid!=0);
1128 while( SQLITE_ROW==sqlite3_step(pStmt) ){
1129 nByte += sqlite3_column_bytes(pStmt, 1); /* name */
1130 nByte += sqlite3_column_bytes(pStmt, 4); /* dflt_value */
1131 if( sqlite3_column_int(pStmt, 6)==0 ){ /* !hidden */
1132 nDbCol++;
1133 }
1134 if( sqlite3_column_int(pStmt, 5) ) bRowid = 0; /* pk */
1135 }
1136 if( nDbCol==0 ) bRowid = 0;
1137 nDbCol += bRowid;
1138 nByte += strlen(SESSIONS_ROWID);
1139 rc = sqlite3_reset(pStmt);
1140
1141 if( rc==SQLITE_OK ){
1142 nByte += nDbCol * (sizeof(const char *)*2 +sizeof(int)+sizeof(u8) + 1 + 1);
1143 pAlloc = sessionMalloc64(pSession, nByte);
1144 if( pAlloc==0 ){
1145 rc = SQLITE_NOMEM;
1146 }else{
1147 memset(pAlloc, 0, nByte);
1148 }
1149 }
1150 if( rc==SQLITE_OK ){
1151 azCol = (char **)pAlloc;
1152 azDflt = (char**)&azCol[nDbCol];
1153 aiIdx = (int*)&azDflt[nDbCol];
1154 abPK = (u8 *)&aiIdx[nDbCol];
1155 pAlloc = &abPK[nDbCol];
1156 if( pzTab ){
1157 memcpy(pAlloc, zThis, nThis+1);
1158 *pzTab = (char *)pAlloc;
1159 pAlloc += nThis+1;
1160 }
1161
1162 i = 0;
1163 if( bRowid ){
1164 size_t nName = strlen(SESSIONS_ROWID);
1165 memcpy(pAlloc, SESSIONS_ROWID, nName+1);
1166 azCol[i] = (char*)pAlloc;
1167 pAlloc += nName+1;
1168 abPK[i] = 1;
1169 aiIdx[i] = -1;
1170 i++;
1171 }
1172 while( SQLITE_ROW==sqlite3_step(pStmt) ){
1173 if( sqlite3_column_int(pStmt, 6)==0 ){ /* !hidden */
1174 int nName = sqlite3_column_bytes(pStmt, 1);
1175 int nDflt = sqlite3_column_bytes(pStmt, 4);
1176 const unsigned char *zName = sqlite3_column_text(pStmt, 1);
1177 const unsigned char *zDflt = sqlite3_column_text(pStmt, 4);
1178
1179 if( zName==0 ) break;
1180 memcpy(pAlloc, zName, nName+1);
1181 azCol[i] = (char *)pAlloc;
1182 pAlloc += nName+1;
1183 if( zDflt ){
1184 memcpy(pAlloc, zDflt, nDflt+1);
1185 azDflt[i] = (char *)pAlloc;
1186 pAlloc += nDflt+1;
1187 }else{
1188 azDflt[i] = 0;
1189 }
1190 abPK[i] = sqlite3_column_int(pStmt, 5);
1191 aiIdx[i] = sqlite3_column_int(pStmt, 0);
1192 i++;
1193 }
1194 if( pnTotalCol ) (*pnTotalCol)++;
1195 }
1196 rc = sqlite3_reset(pStmt);
1197 }
1198
1199 /* If successful, populate the output variables. Otherwise, zero them and
1200 ** free any allocation made. An error code will be returned in this case.
1201 */
1202 if( rc==SQLITE_OK ){
1203 *pazCol = (const char**)azCol;
1204 if( pazDflt ) *pazDflt = (const char**)azDflt;
1205 *pabPK = abPK;
1206 *pnCol = nDbCol;
1207 if( paiIdx ) *paiIdx = aiIdx;
1208 }else{
1209 sessionFree(pSession, azCol);
1210 }
1211 if( pbRowid ) *pbRowid = bRowid;
1212 sqlite3_finalize(pStmt);
1213 return rc;
1214 }
1215
1216 /*
1217 ** This function is called to initialize the SessionTable.nCol, azCol[]
1218 ** abPK[] and azDflt[] members of SessionTable object pTab. If these
1219 ** fields are already initialized, this function is a no-op.
1220 **
1221 ** If an error occurs, an error code is stored in sqlite3_session.rc and
1222 ** non-zero returned. Or, if no error occurs but the table has no primary
1223 ** key, sqlite3_session.rc is left set to SQLITE_OK and non-zero returned to
1224 ** indicate that updates on this table should be ignored. SessionTable.abPK
1225 ** is set to NULL in this case.
1226 */
1227 static int sessionInitTable(
1228 sqlite3_session *pSession, /* Optional session handle */
1229 SessionTable *pTab, /* Table object to initialize */
1230 sqlite3 *db, /* Database handle to read schema from */
1231 const char *zDb /* Name of db - "main", "temp" etc. */
1232 ){
1233 int rc = SQLITE_OK;
1234
1235 if( pTab->nCol==0 ){
1236 u8 *abPK;
1237 assert( pTab->azCol==0 || pTab->abPK==0 );
1238 sqlite3_free(pTab->azCol);
1239 pTab->abPK = 0;
1240 rc = sessionTableInfo(pSession, db, zDb,
1241 pTab->zName, &pTab->nCol, &pTab->nTotalCol, 0, &pTab->azCol,
1242 &pTab->azDflt, &pTab->aiIdx, &abPK,
1243 ((pSession==0 || pSession->bImplicitPK) ? &pTab->bRowid : 0)
1244 );
1245 if( rc==SQLITE_OK ){
1246 int i;
1247 for(i=0; i<pTab->nCol; i++){
1248 if( abPK[i] ){
1249 pTab->abPK = abPK;
1250 break;
1251 }
1252 }
1253 if( 0==sqlite3_stricmp("sqlite_stat1", pTab->zName) ){
1254 pTab->bStat1 = 1;
1255 }
1256
1257 if( pSession && pSession->bEnableSize ){
1258 pSession->nMaxChangesetSize += (
1259 1 + sessionVarintLen(pTab->nCol) + pTab->nCol + strlen(pTab->zName)+1
1260 );
1261 }
1262 }
1263 }
1264
1265 if( pSession ){
1266 pSession->rc = rc;
1267 return (rc || pTab->abPK==0);
1268 }
1269 return rc;
1270 }
1271
1272 /*
1273 ** Re-initialize table object pTab.
1274 */
1275 static int sessionReinitTable(sqlite3_session *pSession, SessionTable *pTab){
1276 int nCol = 0;
1277 int nTotalCol = 0;
1278 const char **azCol = 0;
1279 const char **azDflt = 0;
1280 int *aiIdx = 0;
1281 u8 *abPK = 0;
1282 int bRowid = 0;
1283
1284 assert( pSession->rc==SQLITE_OK );
1285
1286 pSession->rc = sessionTableInfo(pSession, pSession->db, pSession->zDb,
1287 pTab->zName, &nCol, &nTotalCol, 0, &azCol, &azDflt, &aiIdx, &abPK,
1288 (pSession->bImplicitPK ? &bRowid : 0)
1289 );
1290 if( pSession->rc==SQLITE_OK ){
1291 if( pTab->nCol>nCol || pTab->bRowid!=bRowid ){
1292 pSession->rc = SQLITE_SCHEMA;
1293 }else{
1294 int ii;
1295 int nOldCol = pTab->nCol;
1296 for(ii=0; ii<nCol; ii++){
1297 if( ii<pTab->nCol ){
1298 if( pTab->abPK[ii]!=abPK[ii] ){
1299 pSession->rc = SQLITE_SCHEMA;
1300 }
1301 }else if( abPK[ii] ){
1302 pSession->rc = SQLITE_SCHEMA;
1303 }
1304 }
1305
1306 if( pSession->rc==SQLITE_OK ){
1307 const char **a = pTab->azCol;
1308 pTab->azCol = azCol;
1309 pTab->nCol = nCol;
1310 pTab->nTotalCol = nTotalCol;
1311 pTab->azDflt = azDflt;
1312 pTab->abPK = abPK;
1313 pTab->aiIdx = aiIdx;
1314 azCol = a;
1315 }
1316 if( pSession->bEnableSize ){
1317 pSession->nMaxChangesetSize += (nCol - nOldCol);
1318 pSession->nMaxChangesetSize += sessionVarintLen(nCol);
1319 pSession->nMaxChangesetSize -= sessionVarintLen(nOldCol);
1320 }
1321 }
1322 }
1323
1324 sqlite3_free((char*)azCol);
1325 return pSession->rc;
1326 }
1327
1328 /*
1329 ** Session-change object (*pp) contains an old.* record with fewer than
1330 ** nCol fields. This function updates it with the default values for
1331 ** the missing fields.
1332 */
1333 static void sessionUpdateOneChange(
1334 sqlite3_session *pSession, /* For memory accounting */
1335 int *pRc, /* IN/OUT: Error code */
1336 SessionChange **pp, /* IN/OUT: Change object to update */
1337 int nCol, /* Number of columns now in table */
1338 sqlite3_stmt *pDflt /* SELECT <default-values...> */
1339 ){
1340 SessionChange *pOld = *pp;
1341
1342 while( pOld->nRecordField<nCol ){
1343 SessionChange *pNew = 0;
1344 int nByte = 0;
1345 int nIncr = 0;
1346 int iField = pOld->nRecordField;
1347 int eType = sqlite3_column_type(pDflt, iField);
1348 switch( eType ){
1349 case SQLITE_NULL:
1350 nIncr = 1;
1351 break;
1352 case SQLITE_INTEGER:
1353 case SQLITE_FLOAT:
1354 nIncr = 9;
1355 break;
1356 default: {
1357 int n = sqlite3_column_bytes(pDflt, iField);
1358 nIncr = 1 + sessionVarintLen(n) + n;
1359 assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
1360 break;
1361 }
1362 }
1363
1364 nByte = nIncr + (sizeof(SessionChange) + pOld->nRecord);
1365 pNew = sessionMalloc64(pSession, nByte);
1366 if( pNew==0 ){
1367 *pRc = SQLITE_NOMEM;
1368 return;
1369 }else{
1370 memcpy(pNew, pOld, sizeof(SessionChange));
1371 pNew->aRecord = (u8*)&pNew[1];
1372 memcpy(pNew->aRecord, pOld->aRecord, pOld->nRecord);
1373 pNew->aRecord[pNew->nRecord++] = (u8)eType;
1374 switch( eType ){
1375 case SQLITE_INTEGER: {
1376 i64 iVal = sqlite3_column_int64(pDflt, iField);
1377 sessionPutI64(&pNew->aRecord[pNew->nRecord], iVal);
1378 pNew->nRecord += 8;
1379 break;
1380 }
1381
1382 case SQLITE_FLOAT: {
1383 double rVal = sqlite3_column_double(pDflt, iField);
1384 sessionPutDouble(&pNew->aRecord[pNew->nRecord], rVal);
1385 pNew->nRecord += 8;
1386 break;
1387 }
1388
1389 case SQLITE_TEXT: {
1390 int n = sqlite3_column_bytes(pDflt, iField);
1391 const char *z = (const char*)sqlite3_column_text(pDflt, iField);
1392 pNew->nRecord += sessionVarintPut(&pNew->aRecord[pNew->nRecord], n);
1393 memcpy(&pNew->aRecord[pNew->nRecord], z, n);
1394 pNew->nRecord += n;
1395 break;
1396 }
1397
1398 case SQLITE_BLOB: {
1399 int n = sqlite3_column_bytes(pDflt, iField);
1400 const u8 *z = (const u8*)sqlite3_column_blob(pDflt, iField);
1401 pNew->nRecord += sessionVarintPut(&pNew->aRecord[pNew->nRecord], n);
1402 memcpy(&pNew->aRecord[pNew->nRecord], z, n);
1403 pNew->nRecord += n;
1404 break;
1405 }
1406
1407 default:
1408 assert( eType==SQLITE_NULL );
1409 break;
1410 }
1411
1412 sessionFree(pSession, pOld);
1413 *pp = pOld = pNew;
1414 pNew->nRecordField++;
1415 pNew->nMaxSize += nIncr;
1416 if( pSession ){
1417 pSession->nMaxChangesetSize += nIncr;
1418 }
1419 }
1420 }
1421 }
1422
1423 /*
1424 ** Ensure that there is room in the buffer to append nByte bytes of data.
1425 ** If not, use sqlite3_realloc() to grow the buffer so that there is.
1426 **
1427 ** If successful, return zero. Otherwise, if an OOM condition is encountered,
1428 ** set *pRc to SQLITE_NOMEM and return non-zero.
1429 */
1430 static int sessionBufferGrow(SessionBuffer *p, i64 nByte, int *pRc){
1431 #define SESSION_MAX_BUFFER_SZ (0x7FFFFF00 - 1)
1432 i64 nReq = p->nBuf + nByte;
1433 if( *pRc==SQLITE_OK && nReq>p->nAlloc ){
1434 u8 *aNew;
1435 i64 nNew = p->nAlloc ? p->nAlloc : 128;
1436
1437 do {
1438 nNew = nNew*2;
1439 }while( nNew<nReq );
1440
1441 /* The value of SESSION_MAX_BUFFER_SZ is copied from the implementation
1442 ** of sqlite3_realloc64(). Allocations greater than this size in bytes
1443 ** always fail. It is used here to ensure that this routine can always
1444 ** allocate up to this limit - instead of up to the largest power of
1445 ** two smaller than the limit. */
1446 if( nNew>SESSION_MAX_BUFFER_SZ ){
1447 nNew = SESSION_MAX_BUFFER_SZ;
1448 if( nNew<nReq ){
1449 *pRc = SQLITE_NOMEM;
1450 return 1;
1451 }
1452 }
1453
1454 aNew = (u8 *)sqlite3_realloc64(p->aBuf, nNew);
1455 if( 0==aNew ){
1456 *pRc = SQLITE_NOMEM;
1457 }else{
1458 p->aBuf = aNew;
1459 p->nAlloc = nNew;
1460 }
1461 }
1462 return (*pRc!=SQLITE_OK);
1463 }
1464
1465
1466 /*
1467 ** This function is a no-op if *pRc is other than SQLITE_OK when it is
1468 ** called. Otherwise, append a string to the buffer. All bytes in the string
1469 ** up to (but not including) the nul-terminator are written to the buffer.
1470 **
1471 ** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
1472 ** returning.
1473 */
1474 static void sessionAppendStr(
1475 SessionBuffer *p,
1476 const char *zStr,
1477 int *pRc
1478 ){
1479 int nStr = sqlite3Strlen30(zStr);
1480 if( 0==sessionBufferGrow(p, nStr+1, pRc) ){
1481 memcpy(&p->aBuf[p->nBuf], zStr, nStr);
1482 p->nBuf += nStr;
1483 p->aBuf[p->nBuf] = 0x00;
1484 }
1485 }
1486
1487 /*
1488 ** Format a string using printf() style formatting and then append it to the
1489 ** buffer using sessionAppendString().
1490 */
1491 static void sessionAppendPrintf(
1492 SessionBuffer *p, /* Buffer to append to */
1493 int *pRc,
1494 const char *zFmt,
1495 ...
1496 ){
1497 if( *pRc==SQLITE_OK ){
1498 char *zApp = 0;
1499 va_list ap;
1500 va_start(ap, zFmt);
1501 zApp = sqlite3_vmprintf(zFmt, ap);
1502 if( zApp==0 ){
1503 *pRc = SQLITE_NOMEM;
1504 }else{
1505 sessionAppendStr(p, zApp, pRc);
1506 }
1507 va_end(ap);
1508 sqlite3_free(zApp);
1509 }
1510 }
1511
1512 /*
1513 ** Prepare a statement against database handle db that SELECTs a single
1514 ** row containing the default values for each column in table pTab. For
1515 ** example, if pTab is declared as:
1516 **
1517 ** CREATE TABLE pTab(a PRIMARY KEY, b DEFAULT 123, c DEFAULT 'abcd');
1518 **
1519 ** Then this function prepares and returns the SQL statement:
1520 **
1521 ** SELECT NULL, 123, 'abcd';
1522 */
1523 static int sessionPrepareDfltStmt(
1524 sqlite3 *db, /* Database handle */
1525 SessionTable *pTab, /* Table to prepare statement for */
1526 sqlite3_stmt **ppStmt /* OUT: Statement handle */
1527 ){
1528 SessionBuffer sql = {0,0,0};
1529 int rc = SQLITE_OK;
1530 const char *zSep = " ";
1531 int ii = 0;
1532
1533 *ppStmt = 0;
1534 sessionAppendPrintf(&sql, &rc, "SELECT");
1535 for(ii=0; ii<pTab->nCol; ii++){
1536 const char *zDflt = pTab->azDflt[ii] ? pTab->azDflt[ii] : "NULL";
1537 sessionAppendPrintf(&sql, &rc, "%s%s", zSep, zDflt);
1538 zSep = ", ";
1539 }
1540 if( rc==SQLITE_OK ){
1541 rc = sqlite3_prepare_v2(db, (const char*)sql.aBuf, -1, ppStmt, 0);
1542 }
1543 sqlite3_free(sql.aBuf);
1544
1545 return rc;
1546 }
1547
1548 /*
1549 ** Finalize statement pStmt. If (*pRc) is SQLITE_OK when this function is
1550 ** called, set it to the results of the sqlite3_finalize() call. Or, if
1551 ** it is already set to an error code, leave it as is.
1552 */
1553 static void sessionFinalizeStmt(sqlite3_stmt *pStmt, int *pRc){
1554 int rc = sqlite3_finalize(pStmt);
1555 if( *pRc==SQLITE_OK ) *pRc = rc;
1556 }
1557
1558 /*
1559 ** Table pTab has one or more existing change-records with old.* records
1560 ** with fewer than pTab->nCol columns. This function updates all such
1561 ** change-records with the default values for the missing columns.
1562 */
1563 static int sessionUpdateChanges(sqlite3_session *pSession, SessionTable *pTab){
1564 sqlite3_stmt *pStmt = 0;
1565 int rc = pSession->rc;
1566
1567 rc = sessionPrepareDfltStmt(pSession->db, pTab, &pStmt);
1568 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
1569 int ii = 0;
1570 SessionChange **pp = 0;
1571 for(ii=0; ii<pTab->nChange; ii++){
1572 for(pp=&pTab->apChange[ii]; *pp; pp=&((*pp)->pNext)){
1573 if( (*pp)->nRecordField!=pTab->nCol ){
1574 sessionUpdateOneChange(pSession, &rc, pp, pTab->nCol, pStmt);
1575 }
1576 }
1577 }
1578 }
1579
1580 sessionFinalizeStmt(pStmt, &rc);
1581 pSession->rc = rc;
1582 return pSession->rc;
1583 }
1584
1585 /*
1586 ** Versions of the four methods in object SessionHook for use with the
1587 ** sqlite_stat1 table. The purpose of this is to substitute a zero-length
1588 ** blob each time a NULL value is read from the "idx" column of the
1589 ** sqlite_stat1 table.
1590 */
1591 typedef struct SessionStat1Ctx SessionStat1Ctx;
1592 struct SessionStat1Ctx {
1593 SessionHook hook;
1594 sqlite3_session *pSession;
1595 };
1596 static int sessionStat1Old(void *pCtx, int iCol, sqlite3_value **ppVal){
1597 SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
1598 sqlite3_value *pVal = 0;
1599 int rc = p->hook.xOld(p->hook.pCtx, iCol, &pVal);
1600 if( rc==SQLITE_OK && iCol==1 && sqlite3_value_type(pVal)==SQLITE_NULL ){
1601 pVal = p->pSession->pZeroBlob;
1602 }
1603 *ppVal = pVal;
1604 return rc;
1605 }
1606 static int sessionStat1New(void *pCtx, int iCol, sqlite3_value **ppVal){
1607 SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
1608 sqlite3_value *pVal = 0;
1609 int rc = p->hook.xNew(p->hook.pCtx, iCol, &pVal);
1610 if( rc==SQLITE_OK && iCol==1 && sqlite3_value_type(pVal)==SQLITE_NULL ){
1611 pVal = p->pSession->pZeroBlob;
1612 }
1613 *ppVal = pVal;
1614 return rc;
1615 }
1616 static int sessionStat1Count(void *pCtx){
1617 SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
1618 return p->hook.xCount(p->hook.pCtx);
1619 }
1620 static int sessionStat1Depth(void *pCtx){
1621 SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
1622 return p->hook.xDepth(p->hook.pCtx);
1623 }
1624
1625 static int sessionUpdateMaxSize(
1626 int op,
1627 sqlite3_session *pSession, /* Session object pTab is attached to */
1628 SessionTable *pTab, /* Table that change applies to */
1629 SessionChange *pC /* Update pC->nMaxSize */
1630 ){
1631 i64 nNew = 2;
1632 if( pC->op==SQLITE_INSERT ){
1633 if( pTab->bRowid ) nNew += 9;
1634 if( op!=SQLITE_DELETE ){
1635 int ii;
1636 for(ii=0; ii<pTab->nCol; ii++){
1637 sqlite3_value *p = 0;
1638 pSession->hook.xNew(pSession->hook.pCtx, pTab->aiIdx[ii], &p);
1639 sessionSerializeValue(0, p, &nNew);
1640 }
1641 }
1642 }else if( op==SQLITE_DELETE ){
1643 nNew += pC->nRecord;
1644 if( sqlite3_preupdate_blobwrite(pSession->db)>=0 ){
1645 nNew += pC->nRecord;
1646 }
1647 }else{
1648 int ii;
1649 u8 *pCsr = pC->aRecord;
1650 if( pTab->bRowid ){
1651 nNew += 9 + 1;
1652 pCsr += 9;
1653 }
1654 for(ii=pTab->bRowid; ii<pTab->nCol; ii++){
1655 int bChanged = 1;
1656 int nOld = 0;
1657 int eType;
1658 int iIdx = pTab->aiIdx[ii];
1659 sqlite3_value *p = 0;
1660 pSession->hook.xNew(pSession->hook.pCtx, iIdx, &p);
1661 if( p==0 ){
1662 return SQLITE_NOMEM;
1663 }
1664
1665 eType = *pCsr++;
1666 switch( eType ){
1667 case SQLITE_NULL:
1668 bChanged = sqlite3_value_type(p)!=SQLITE_NULL;
1669 break;
1670
1671 case SQLITE_FLOAT:
1672 case SQLITE_INTEGER: {
1673 if( eType==sqlite3_value_type(p) ){
1674 sqlite3_int64 iVal = sessionGetI64(pCsr);
1675 if( eType==SQLITE_INTEGER ){
1676 bChanged = (iVal!=sqlite3_value_int64(p));
1677 }else{
1678 double dVal;
1679 memcpy(&dVal, &iVal, 8);
1680 bChanged = (dVal!=sqlite3_value_double(p));
1681 }
1682 }
1683 nOld = 8;
1684 pCsr += 8;
1685 break;
1686 }
1687
1688 default: {
1689 int nByte;
1690 nOld = sessionVarintGet(pCsr, &nByte);
1691 pCsr += nOld;
1692 nOld += nByte;
1693 assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
1694 if( eType==sqlite3_value_type(p)
1695 && nByte==sqlite3_value_bytes(p)
1696 && (nByte==0 || 0==memcmp(pCsr, sqlite3_value_blob(p), nByte))
1697 ){
1698 bChanged = 0;
1699 }
1700 pCsr += nByte;
1701 break;
1702 }
1703 }
1704
1705 if( bChanged && pTab->abPK[ii] ){
1706 nNew = pC->nRecord + 2;
1707 break;
1708 }
1709
1710 if( bChanged ){
1711 nNew += 1 + nOld;
1712 sessionSerializeValue(0, p, &nNew);
1713 }else if( pTab->abPK[ii] ){
1714 nNew += 2 + nOld;
1715 }else{
1716 nNew += 2;
1717 }
1718 }
1719 }
1720
1721 if( nNew>pC->nMaxSize ){
1722 int nIncr = nNew - pC->nMaxSize;
1723 pC->nMaxSize = nNew;
1724 pSession->nMaxChangesetSize += nIncr;
1725 }
1726 return SQLITE_OK;
1727 }
1728
1729 /*
1730 ** This function is only called from with a pre-update-hook reporting a
1731 ** change on table pTab (attached to session pSession). The type of change
1732 ** (UPDATE, INSERT, DELETE) is specified by the first argument.
1733 **
1734 ** Unless one is already present or an error occurs, an entry is added
1735 ** to the changed-rows hash table associated with table pTab.
1736 */
1737 static void sessionPreupdateOneChange(
1738 int op, /* One of SQLITE_UPDATE, INSERT, DELETE */
1739 i64 iRowid,
1740 sqlite3_session *pSession, /* Session object pTab is attached to */
1741 SessionTable *pTab /* Table that change applies to */
1742 ){
1743 int iHash;
1744 int bNull = 0;
1745 int rc = SQLITE_OK;
1746 int nExpect = 0;
1747 SessionStat1Ctx stat1 = {{0,0,0,0,0},0};
1748
1749 if( pSession->rc ) return;
1750
1751 /* Load table details if required */
1752 if( sessionInitTable(pSession, pTab, pSession->db, pSession->zDb) ) return;
1753
1754 /* Check the number of columns in this xPreUpdate call matches the
1755 ** number of columns in the table. */
1756 nExpect = pSession->hook.xCount(pSession->hook.pCtx);
1757 if( pTab->nTotalCol<nExpect ){
1758 if( sessionReinitTable(pSession, pTab) ) return;
1759 if( sessionUpdateChanges(pSession, pTab) ) return;
1760 }
1761 if( pTab->nTotalCol!=nExpect ){
1762 pSession->rc = SQLITE_SCHEMA;
1763 return;
1764 }
1765
1766 /* Grow the hash table if required */
1767 if( sessionGrowHash(pSession, 0, pTab) ){
1768 pSession->rc = SQLITE_NOMEM;
1769 return;
1770 }
1771
1772 if( pTab->bStat1 ){
1773 stat1.hook = pSession->hook;
1774 stat1.pSession = pSession;
1775 pSession->hook.pCtx = (void*)&stat1;
1776 pSession->hook.xNew = sessionStat1New;
1777 pSession->hook.xOld = sessionStat1Old;
1778 pSession->hook.xCount = sessionStat1Count;
1779 pSession->hook.xDepth = sessionStat1Depth;
1780 if( pSession->pZeroBlob==0 ){
1781 sqlite3_value *p = sqlite3ValueNew(0);
1782 if( p==0 ){
1783 rc = SQLITE_NOMEM;
1784 goto error_out;
1785 }
1786 sqlite3ValueSetStr(p, 0, "", 0, SQLITE_STATIC);
1787 pSession->pZeroBlob = p;
1788 }
1789 }
1790
1791 /* Calculate the hash-key for this change. If the primary key of the row
1792 ** includes a NULL value, exit early. Such changes are ignored by the
1793 ** session module. */
1794 rc = sessionPreupdateHash(
1795 pSession, iRowid, pTab, op==SQLITE_INSERT, &iHash, &bNull
1796 );
1797 if( rc!=SQLITE_OK ) goto error_out;
1798
1799 if( bNull==0 ){
1800 /* Search the hash table for an existing record for this row. */
1801 SessionChange *pC;
1802 for(pC=pTab->apChange[iHash]; pC; pC=pC->pNext){
1803 if( sessionPreupdateEqual(pSession, iRowid, pTab, pC, op) ) break;
1804 }
1805
1806 if( pC==0 ){
1807 /* Create a new change object containing all the old values (if
1808 ** this is an SQLITE_UPDATE or SQLITE_DELETE), or just the PK
1809 ** values (if this is an INSERT). */
1810 sqlite3_int64 nByte; /* Number of bytes to allocate */
1811 int i; /* Used to iterate through columns */
1812
1813 assert( rc==SQLITE_OK );
1814 pTab->nEntry++;
1815
1816 /* Figure out how large an allocation is required */
1817 nByte = sizeof(SessionChange);
1818 for(i=pTab->bRowid; i<pTab->nCol; i++){
1819 int iIdx = pTab->aiIdx[i];
1820 sqlite3_value *p = 0;
1821 if( op!=SQLITE_INSERT ){
1822 /* This may fail if the column has a non-NULL default and was added
1823 ** using ALTER TABLE ADD COLUMN after this record was created. */
1824 rc = pSession->hook.xOld(pSession->hook.pCtx, iIdx, &p);
1825 }else if( pTab->abPK[i] ){
1826 TESTONLY(int trc = ) pSession->hook.xNew(pSession->hook.pCtx,iIdx,&p);
1827 assert( trc==SQLITE_OK );
1828 }
1829
1830 if( rc==SQLITE_OK ){
1831 /* This may fail if SQLite value p contains a utf-16 string that must
1832 ** be converted to utf-8 and an OOM error occurs while doing so. */
1833 rc = sessionSerializeValue(0, p, &nByte);
1834 }
1835 if( rc!=SQLITE_OK ) goto error_out;
1836 }
1837 if( pTab->bRowid ){
1838 nByte += 9; /* Size of rowid field - an integer */
1839 }
1840
1841 /* Allocate the change object */
1842 pC = (SessionChange*)sessionMalloc64(pSession, nByte);
1843 if( !pC ){
1844 rc = SQLITE_NOMEM;
1845 goto error_out;
1846 }else{
1847 memset(pC, 0, sizeof(SessionChange));
1848 pC->aRecord = (u8 *)&pC[1];
1849 }
1850
1851 /* Populate the change object. None of the preupdate_old(),
1852 ** preupdate_new() or SerializeValue() calls below may fail as all
1853 ** required values and encodings have already been cached in memory.
1854 ** It is not possible for an OOM to occur in this block. */
1855 nByte = 0;
1856 if( pTab->bRowid ){
1857 pC->aRecord[0] = SQLITE_INTEGER;
1858 sessionPutI64(&pC->aRecord[1], iRowid);
1859 nByte = 9;
1860 }
1861 for(i=pTab->bRowid; i<pTab->nCol; i++){
1862 sqlite3_value *p = 0;
1863 int iIdx = pTab->aiIdx[i];
1864 if( op!=SQLITE_INSERT ){
1865 pSession->hook.xOld(pSession->hook.pCtx, iIdx, &p);
1866 }else if( pTab->abPK[i] ){
1867 pSession->hook.xNew(pSession->hook.pCtx, iIdx, &p);
1868 }
1869 sessionSerializeValue(&pC->aRecord[nByte], p, &nByte);
1870 }
1871
1872 /* Add the change to the hash-table */
1873 if( pSession->bIndirect || pSession->hook.xDepth(pSession->hook.pCtx) ){
1874 pC->bIndirect = 1;
1875 }
1876 pC->nRecordField = pTab->nCol;
1877 pC->nRecord = nByte;
1878 pC->op = op;
1879 pC->pNext = pTab->apChange[iHash];
1880 pTab->apChange[iHash] = pC;
1881
1882 }else if( pC->bIndirect ){
1883 /* If the existing change is considered "indirect", but this current
1884 ** change is "direct", mark the change object as direct. */
1885 if( pSession->hook.xDepth(pSession->hook.pCtx)==0
1886 && pSession->bIndirect==0
1887 ){
1888 pC->bIndirect = 0;
1889 }
1890 }
1891
1892 assert( rc==SQLITE_OK );
1893 if( pSession->bEnableSize ){
1894 rc = sessionUpdateMaxSize(op, pSession, pTab, pC);
1895 }
1896 }
1897
1898
1899 /* If an error has occurred, mark the session object as failed. */
1900 error_out:
1901 if( pTab->bStat1 ){
1902 pSession->hook = stat1.hook;
1903 }
1904 if( rc!=SQLITE_OK ){
1905 pSession->rc = rc;
1906 }
1907 }
1908
1909 static int sessionFindTable(
1910 sqlite3_session *pSession,
1911 const char *zName,
1912 SessionTable **ppTab
1913 ){
1914 int rc = SQLITE_OK;
1915 int nName = sqlite3Strlen30(zName);
1916 SessionTable *pRet;
1917
1918 /* Search for an existing table */
1919 for(pRet=pSession->pTable; pRet; pRet=pRet->pNext){
1920 if( 0==sqlite3_strnicmp(pRet->zName, zName, nName+1) ) break;
1921 }
1922
1923 if( pRet==0 && pSession->bAutoAttach ){
1924 /* If there is a table-filter configured, invoke it. If it returns 0,
1925 ** do not automatically add the new table. */
1926 if( pSession->xTableFilter==0
1927 || pSession->xTableFilter(pSession->pFilterCtx, zName)
1928 ){
1929 rc = sqlite3session_attach(pSession, zName);
1930 if( rc==SQLITE_OK ){
1931 pRet = pSession->pTable;
1932 while( ALWAYS(pRet) && pRet->pNext ){
1933 pRet = pRet->pNext;
1934 }
1935 assert( pRet!=0 );
1936 assert( 0==sqlite3_strnicmp(pRet->zName, zName, nName+1) );
1937 }
1938 }
1939 }
1940
1941 assert( rc==SQLITE_OK || pRet==0 );
1942 *ppTab = pRet;
1943 return rc;
1944 }
1945
1946 /*
1947 ** The 'pre-update' hook registered by this module with SQLite databases.
1948 */
1949 static void xPreUpdate(
1950 void *pCtx, /* Copy of third arg to preupdate_hook() */
1951 sqlite3 *db, /* Database handle */
1952 int op, /* SQLITE_UPDATE, DELETE or INSERT */
1953 char const *zDb, /* Database name */
1954 char const *zName, /* Table name */
1955 sqlite3_int64 iKey1, /* Rowid of row about to be deleted/updated */
1956 sqlite3_int64 iKey2 /* New rowid value (for a rowid UPDATE) */
1957 ){
1958 sqlite3_session *pSession;
1959 int nDb = sqlite3Strlen30(zDb);
1960
1961 assert( sqlite3_mutex_held(db->mutex) );
1962 (void)iKey1;
1963 (void)iKey2;
1964
1965 for(pSession=(sqlite3_session *)pCtx; pSession; pSession=pSession->pNext){
1966 SessionTable *pTab;
1967
1968 /* If this session is attached to a different database ("main", "temp"
1969 ** etc.), or if it is not currently enabled, there is nothing to do. Skip
1970 ** to the next session object attached to this database. */
1971 if( pSession->bEnable==0 ) continue;
1972 if( pSession->rc ) continue;
1973 if( sqlite3_strnicmp(zDb, pSession->zDb, nDb+1) ) continue;
1974
1975 pSession->rc = sessionFindTable(pSession, zName, &pTab);
1976 if( pTab ){
1977 assert( pSession->rc==SQLITE_OK );
1978 assert( op==SQLITE_UPDATE || iKey1==iKey2 );
1979 sessionPreupdateOneChange(op, iKey1, pSession, pTab);
1980 if( op==SQLITE_UPDATE ){
1981 sessionPreupdateOneChange(SQLITE_INSERT, iKey2, pSession, pTab);
1982 }
1983 }
1984 }
1985 }
1986
1987 /*
1988 ** The pre-update hook implementations.
1989 */
1990 static int sessionPreupdateOld(void *pCtx, int iVal, sqlite3_value **ppVal){
1991 return sqlite3_preupdate_old((sqlite3*)pCtx, iVal, ppVal);
1992 }
1993 static int sessionPreupdateNew(void *pCtx, int iVal, sqlite3_value **ppVal){
1994 return sqlite3_preupdate_new((sqlite3*)pCtx, iVal, ppVal);
1995 }
1996 static int sessionPreupdateCount(void *pCtx){
1997 return sqlite3_preupdate_count((sqlite3*)pCtx);
1998 }
1999 static int sessionPreupdateDepth(void *pCtx){
2000 return sqlite3_preupdate_depth((sqlite3*)pCtx);
2001 }
2002
2003 /*
2004 ** Install the pre-update hooks on the session object passed as the only
2005 ** argument.
2006 */
2007 static void sessionPreupdateHooks(
2008 sqlite3_session *pSession
2009 ){
2010 pSession->hook.pCtx = (void*)pSession->db;
2011 pSession->hook.xOld = sessionPreupdateOld;
2012 pSession->hook.xNew = sessionPreupdateNew;
2013 pSession->hook.xCount = sessionPreupdateCount;
2014 pSession->hook.xDepth = sessionPreupdateDepth;
2015 }
2016
2017 typedef struct SessionDiffCtx SessionDiffCtx;
2018 struct SessionDiffCtx {
2019 sqlite3_stmt *pStmt;
2020 int bRowid;
2021 int nOldOff;
2022 };
2023
2024 /*
2025 ** The diff hook implementations.
2026 */
2027 static int sessionDiffOld(void *pCtx, int iVal, sqlite3_value **ppVal){
2028 SessionDiffCtx *p = (SessionDiffCtx*)pCtx;
2029 *ppVal = sqlite3_column_value(p->pStmt, iVal+p->nOldOff+p->bRowid);
2030 return SQLITE_OK;
2031 }
2032 static int sessionDiffNew(void *pCtx, int iVal, sqlite3_value **ppVal){
2033 SessionDiffCtx *p = (SessionDiffCtx*)pCtx;
2034 *ppVal = sqlite3_column_value(p->pStmt, iVal+p->bRowid);
2035 return SQLITE_OK;
2036 }
2037 static int sessionDiffCount(void *pCtx){
2038 SessionDiffCtx *p = (SessionDiffCtx*)pCtx;
2039 return (p->nOldOff ? p->nOldOff : sqlite3_column_count(p->pStmt)) - p->bRowid;
2040 }
2041 static int sessionDiffDepth(void *pCtx){
2042 (void)pCtx;
2043 return 0;
2044 }
2045
2046 /*
2047 ** Install the diff hooks on the session object passed as the only
2048 ** argument.
2049 */
2050 static void sessionDiffHooks(
2051 sqlite3_session *pSession,
2052 SessionDiffCtx *pDiffCtx
2053 ){
2054 pSession->hook.pCtx = (void*)pDiffCtx;
2055 pSession->hook.xOld = sessionDiffOld;
2056 pSession->hook.xNew = sessionDiffNew;
2057 pSession->hook.xCount = sessionDiffCount;
2058 pSession->hook.xDepth = sessionDiffDepth;
2059 }
2060
2061 static char *sessionExprComparePK(
2062 int nCol,
2063 const char *zDb1, const char *zDb2,
2064 const char *zTab,
2065 const char **azCol, u8 *abPK
2066 ){
2067 int i;
2068 const char *zSep = "";
2069 char *zRet = 0;
2070
2071 for(i=0; i<nCol; i++){
2072 if( abPK[i] ){
2073 zRet = sqlite3_mprintf("%z%s\"%w\".\"%w\".\"%w\"=\"%w\".\"%w\".\"%w\"",
2074 zRet, zSep, zDb1, zTab, azCol[i], zDb2, zTab, azCol[i]
2075 );
2076 zSep = " AND ";
2077 if( zRet==0 ) break;
2078 }
2079 }
2080
2081 return zRet;
2082 }
2083
2084 static char *sessionExprCompareOther(
2085 int nCol,
2086 const char *zDb1, const char *zDb2,
2087 const char *zTab,
2088 const char **azCol, u8 *abPK
2089 ){
2090 int i;
2091 const char *zSep = "";
2092 char *zRet = 0;
2093 int bHave = 0;
2094
2095 for(i=0; i<nCol; i++){
2096 if( abPK[i]==0 ){
2097 bHave = 1;
2098 zRet = sqlite3_mprintf(
2099 "%z%s\"%w\".\"%w\".\"%w\" IS NOT \"%w\".\"%w\".\"%w\"",
2100 zRet, zSep, zDb1, zTab, azCol[i], zDb2, zTab, azCol[i]
2101 );
2102 zSep = " OR ";
2103 if( zRet==0 ) break;
2104 }
2105 }
2106
2107 if( bHave==0 ){
2108 assert( zRet==0 );
2109 zRet = sqlite3_mprintf("0");
2110 }
2111
2112 return zRet;
2113 }
2114
2115 static char *sessionSelectFindNew(
2116 const char *zDb1, /* Pick rows in this db only */
2117 const char *zDb2, /* But not in this one */
2118 int bRowid,
2119 const char *zTbl, /* Table name */
2120 const char *zExpr
2121 ){
2122 const char *zSel = (bRowid ? SESSIONS_ROWID ", *" : "*");
2123 char *zRet = sqlite3_mprintf(
2124 "SELECT %s FROM \"%w\".\"%w\" WHERE NOT EXISTS ("
2125 " SELECT 1 FROM \"%w\".\"%w\" WHERE %s"
2126 ")",
2127 zSel, zDb1, zTbl, zDb2, zTbl, zExpr
2128 );
2129 return zRet;
2130 }
2131
2132 static int sessionDiffFindNew(
2133 int op,
2134 sqlite3_session *pSession,
2135 SessionTable *pTab,
2136 const char *zDb1,
2137 const char *zDb2,
2138 char *zExpr
2139 ){
2140 int rc = SQLITE_OK;
2141 char *zStmt = sessionSelectFindNew(
2142 zDb1, zDb2, pTab->bRowid, pTab->zName, zExpr
2143 );
2144
2145 if( zStmt==0 ){
2146 rc = SQLITE_NOMEM;
2147 }else{
2148 sqlite3_stmt *pStmt;
2149 rc = sqlite3_prepare_v2(pSession->db, zStmt, -1, &pStmt, 0);
2150 if( rc==SQLITE_OK ){
2151 SessionDiffCtx *pDiffCtx = (SessionDiffCtx*)pSession->hook.pCtx;
2152 pDiffCtx->pStmt = pStmt;
2153 pDiffCtx->nOldOff = 0;
2154 pDiffCtx->bRowid = pTab->bRowid;
2155 while( SQLITE_ROW==sqlite3_step(pStmt) ){
2156 i64 iRowid = (pTab->bRowid ? sqlite3_column_int64(pStmt, 0) : 0);
2157 sessionPreupdateOneChange(op, iRowid, pSession, pTab);
2158 }
2159 rc = sqlite3_finalize(pStmt);
2160 }
2161 sqlite3_free(zStmt);
2162 }
2163
2164 return rc;
2165 }
2166
2167 /*
2168 ** Return a comma-separated list of the fully-qualified (with both database
2169 ** and table name) column names from table pTab. e.g.
2170 **
2171 ** "main"."t1"."a", "main"."t1"."b", "main"."t1"."c"
2172 */
2173 static char *sessionAllCols(
2174 const char *zDb,
2175 SessionTable *pTab
2176 ){
2177 int ii;
2178 char *zRet = 0;
2179 for(ii=0; ii<pTab->nCol; ii++){
2180 zRet = sqlite3_mprintf("%z%s\"%w\".\"%w\".\"%w\"",
2181 zRet, (zRet ? ", " : ""), zDb, pTab->zName, pTab->azCol[ii]
2182 );
2183 if( !zRet ) break;
2184 }
2185 return zRet;
2186 }
2187
2188 static int sessionDiffFindModified(
2189 sqlite3_session *pSession,
2190 SessionTable *pTab,
2191 const char *zFrom,
2192 const char *zExpr
2193 ){
2194 int rc = SQLITE_OK;
2195
2196 char *zExpr2 = sessionExprCompareOther(pTab->nCol,
2197 pSession->zDb, zFrom, pTab->zName, pTab->azCol, pTab->abPK
2198 );
2199 if( zExpr2==0 ){
2200 rc = SQLITE_NOMEM;
2201 }else{
2202 char *z1 = sessionAllCols(pSession->zDb, pTab);
2203 char *z2 = sessionAllCols(zFrom, pTab);
2204 char *zStmt = sqlite3_mprintf(
2205 "SELECT %s,%s FROM \"%w\".\"%w\", \"%w\".\"%w\" WHERE %s AND (%z)",
2206 z1, z2, pSession->zDb, pTab->zName, zFrom, pTab->zName, zExpr, zExpr2
2207 );
2208 if( zStmt==0 || z1==0 || z2==0 ){
2209 rc = SQLITE_NOMEM;
2210 }else{
2211 sqlite3_stmt *pStmt;
2212 rc = sqlite3_prepare_v2(pSession->db, zStmt, -1, &pStmt, 0);
2213
2214 if( rc==SQLITE_OK ){
2215 SessionDiffCtx *pDiffCtx = (SessionDiffCtx*)pSession->hook.pCtx;
2216 pDiffCtx->pStmt = pStmt;
2217 pDiffCtx->nOldOff = pTab->nCol;
2218 while( SQLITE_ROW==sqlite3_step(pStmt) ){
2219 i64 iRowid = (pTab->bRowid ? sqlite3_column_int64(pStmt, 0) : 0);
2220 sessionPreupdateOneChange(SQLITE_UPDATE, iRowid, pSession, pTab);
2221 }
2222 rc = sqlite3_finalize(pStmt);
2223 }
2224 }
2225 sqlite3_free(zStmt);
2226 sqlite3_free(z1);
2227 sqlite3_free(z2);
2228 }
2229
2230 return rc;
2231 }
2232
2233 int sqlite3session_diff(
2234 sqlite3_session *pSession,
2235 const char *zFrom,
2236 const char *zTbl,
2237 char **pzErrMsg
2238 ){
2239 const char *zDb = pSession->zDb;
2240 int rc = pSession->rc;
2241 SessionDiffCtx d;
2242
2243 memset(&d, 0, sizeof(d));
2244 sessionDiffHooks(pSession, &d);
2245
2246 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
2247 if( pzErrMsg ) *pzErrMsg = 0;
2248 if( rc==SQLITE_OK ){
2249 char *zExpr = 0;
2250 sqlite3 *db = pSession->db;
2251 SessionTable *pTo; /* Table zTbl */
2252
2253 /* Locate and if necessary initialize the target table object */
2254 pSession->bAutoAttach++;
2255 rc = sessionFindTable(pSession, zTbl, &pTo);
2256 pSession->bAutoAttach--;
2257 if( pTo==0 ) goto diff_out;
2258 if( sessionInitTable(pSession, pTo, pSession->db, pSession->zDb) ){
2259 rc = pSession->rc;
2260 goto diff_out;
2261 }
2262
2263 /* Check the table schemas match */
2264 if( rc==SQLITE_OK ){
2265 int bHasPk = 0;
2266 int bMismatch = 0;
2267 int nCol = 0; /* Columns in zFrom.zTbl */
2268 int bRowid = 0;
2269 u8 *abPK = 0;
2270 const char **azCol = 0;
2271 char *zDbExists = 0;
2272
2273 /* Check that database zFrom is attached. */
2274 zDbExists = sqlite3_mprintf("SELECT * FROM %Q.sqlite_schema", zFrom);
2275 if( zDbExists==0 ){
2276 rc = SQLITE_NOMEM;
2277 }else{
2278 sqlite3_stmt *pDbExists = 0;
2279 rc = sqlite3_prepare_v2(db, zDbExists, -1, &pDbExists, 0);
2280 if( rc==SQLITE_ERROR ){
2281 rc = SQLITE_OK;
2282 nCol = -1;
2283 }
2284 sqlite3_finalize(pDbExists);
2285 sqlite3_free(zDbExists);
2286 }
2287
2288 if( rc==SQLITE_OK && nCol==0 ){
2289 rc = sessionTableInfo(0, db, zFrom, zTbl,
2290 &nCol, 0, 0, &azCol, 0, 0, &abPK,
2291 pSession->bImplicitPK ? &bRowid : 0
2292 );
2293 }
2294 if( rc==SQLITE_OK ){
2295 if( pTo->nCol!=nCol ){
2296 if( nCol<=0 ){
2297 rc = SQLITE_SCHEMA;
2298 if( pzErrMsg ){
2299 *pzErrMsg = sqlite3_mprintf("no such table: %s.%s", zFrom, zTbl);
2300 }
2301 }else{
2302 bMismatch = 1;
2303 }
2304 }else{
2305 int i;
2306 for(i=0; i<nCol; i++){
2307 if( pTo->abPK[i]!=abPK[i] ) bMismatch = 1;
2308 if( sqlite3_stricmp(azCol[i], pTo->azCol[i]) ) bMismatch = 1;
2309 if( abPK[i] ) bHasPk = 1;
2310 }
2311 }
2312 }
2313 sqlite3_free((char*)azCol);
2314 if( bMismatch ){
2315 if( pzErrMsg ){
2316 *pzErrMsg = sqlite3_mprintf("table schemas do not match");
2317 }
2318 rc = SQLITE_SCHEMA;
2319 }
2320 if( bHasPk==0 ){
2321 /* Ignore tables with no primary keys */
2322 goto diff_out;
2323 }
2324 }
2325
2326 if( rc==SQLITE_OK ){
2327 zExpr = sessionExprComparePK(pTo->nCol,
2328 zDb, zFrom, pTo->zName, pTo->azCol, pTo->abPK
2329 );
2330 }
2331
2332 /* Find new rows */
2333 if( rc==SQLITE_OK ){
2334 rc = sessionDiffFindNew(SQLITE_INSERT, pSession, pTo, zDb, zFrom, zExpr);
2335 }
2336
2337 /* Find old rows */
2338 if( rc==SQLITE_OK ){
2339 rc = sessionDiffFindNew(SQLITE_DELETE, pSession, pTo, zFrom, zDb, zExpr);
2340 }
2341
2342 /* Find modified rows */
2343 if( rc==SQLITE_OK ){
2344 rc = sessionDiffFindModified(pSession, pTo, zFrom, zExpr);
2345 }
2346
2347 sqlite3_free(zExpr);
2348 }
2349
2350 diff_out:
2351 sessionPreupdateHooks(pSession);
2352 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
2353 return rc;
2354 }
2355
2356 /*
2357 ** Create a session object. This session object will record changes to
2358 ** database zDb attached to connection db.
2359 */
2360 int sqlite3session_create(
2361 sqlite3 *db, /* Database handle */
2362 const char *zDb, /* Name of db (e.g. "main") */
2363 sqlite3_session **ppSession /* OUT: New session object */
2364 ){
2365 sqlite3_session *pNew; /* Newly allocated session object */
2366 sqlite3_session *pOld; /* Session object already attached to db */
2367 int nDb = sqlite3Strlen30(zDb); /* Length of zDb in bytes */
2368
2369 /* Zero the output value in case an error occurs. */
2370 *ppSession = 0;
2371
2372 /* Allocate and populate the new session object. */
2373 pNew = (sqlite3_session *)sqlite3_malloc64(sizeof(sqlite3_session) + nDb + 1);
2374 if( !pNew ) return SQLITE_NOMEM;
2375 memset(pNew, 0, sizeof(sqlite3_session));
2376 pNew->db = db;
2377 pNew->zDb = (char *)&pNew[1];
2378 pNew->bEnable = 1;
2379 memcpy(pNew->zDb, zDb, nDb+1);
2380 sessionPreupdateHooks(pNew);
2381
2382 /* Add the new session object to the linked list of session objects
2383 ** attached to database handle $db. Do this under the cover of the db
2384 ** handle mutex. */
2385 sqlite3_mutex_enter(sqlite3_db_mutex(db));
2386 pOld = (sqlite3_session*)sqlite3_preupdate_hook(db, xPreUpdate, (void*)pNew);
2387 pNew->pNext = pOld;
2388 sqlite3_mutex_leave(sqlite3_db_mutex(db));
2389
2390 *ppSession = pNew;
2391 return SQLITE_OK;
2392 }
2393
2394 /*
2395 ** Free the list of table objects passed as the first argument. The contents
2396 ** of the changed-rows hash tables are also deleted.
2397 */
2398 static void sessionDeleteTable(sqlite3_session *pSession, SessionTable *pList){
2399 SessionTable *pNext;
2400 SessionTable *pTab;
2401
2402 for(pTab=pList; pTab; pTab=pNext){
2403 int i;
2404 pNext = pTab->pNext;
2405 for(i=0; i<pTab->nChange; i++){
2406 SessionChange *p;
2407 SessionChange *pNextChange;
2408 for(p=pTab->apChange[i]; p; p=pNextChange){
2409 pNextChange = p->pNext;
2410 sessionFree(pSession, p);
2411 }
2412 }
2413 sqlite3_finalize(pTab->pDfltStmt);
2414 sessionFree(pSession, (char*)pTab->azCol); /* cast works around VC++ bug */
2415 sessionFree(pSession, pTab->apChange);
2416 sessionFree(pSession, pTab);
2417 }
2418 }
2419
2420 /*
2421 ** Delete a session object previously allocated using sqlite3session_create().
2422 */
2423 void sqlite3session_delete(sqlite3_session *pSession){
2424 sqlite3 *db = pSession->db;
2425 sqlite3_session *pHead;
2426 sqlite3_session **pp;
2427
2428 /* Unlink the session from the linked list of sessions attached to the
2429 ** database handle. Hold the db mutex while doing so. */
2430 sqlite3_mutex_enter(sqlite3_db_mutex(db));
2431 pHead = (sqlite3_session*)sqlite3_preupdate_hook(db, 0, 0);
2432 for(pp=&pHead; ALWAYS((*pp)!=0); pp=&((*pp)->pNext)){
2433 if( (*pp)==pSession ){
2434 *pp = (*pp)->pNext;
2435 if( pHead ) sqlite3_preupdate_hook(db, xPreUpdate, (void*)pHead);
2436 break;
2437 }
2438 }
2439 sqlite3_mutex_leave(sqlite3_db_mutex(db));
2440 sqlite3ValueFree(pSession->pZeroBlob);
2441
2442 /* Delete all attached table objects. And the contents of their
2443 ** associated hash-tables. */
2444 sessionDeleteTable(pSession, pSession->pTable);
2445
2446 /* Free the session object. */
2447 sqlite3_free(pSession);
2448 }
2449
2450 /*
2451 ** Set a table filter on a Session Object.
2452 */
2453 void sqlite3session_table_filter(
2454 sqlite3_session *pSession,
2455 int(*xFilter)(void*, const char*),
2456 void *pCtx /* First argument passed to xFilter */
2457 ){
2458 pSession->bAutoAttach = 1;
2459 pSession->pFilterCtx = pCtx;
2460 pSession->xTableFilter = xFilter;
2461 }
2462
2463 /*
2464 ** Attach a table to a session. All subsequent changes made to the table
2465 ** while the session object is enabled will be recorded.
2466 **
2467 ** Only tables that have a PRIMARY KEY defined may be attached. It does
2468 ** not matter if the PRIMARY KEY is an "INTEGER PRIMARY KEY" (rowid alias)
2469 ** or not.
2470 */
2471 int sqlite3session_attach(
2472 sqlite3_session *pSession, /* Session object */
2473 const char *zName /* Table name */
2474 ){
2475 int rc = SQLITE_OK;
2476 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
2477
2478 if( !zName ){
2479 pSession->bAutoAttach = 1;
2480 }else{
2481 SessionTable *pTab; /* New table object (if required) */
2482 int nName; /* Number of bytes in string zName */
2483
2484 /* First search for an existing entry. If one is found, this call is
2485 ** a no-op. Return early. */
2486 nName = sqlite3Strlen30(zName);
2487 for(pTab=pSession->pTable; pTab; pTab=pTab->pNext){
2488 if( 0==sqlite3_strnicmp(pTab->zName, zName, nName+1) ) break;
2489 }
2490
2491 if( !pTab ){
2492 /* Allocate new SessionTable object. */
2493 int nByte = sizeof(SessionTable) + nName + 1;
2494 pTab = (SessionTable*)sessionMalloc64(pSession, nByte);
2495 if( !pTab ){
2496 rc = SQLITE_NOMEM;
2497 }else{
2498 /* Populate the new SessionTable object and link it into the list.
2499 ** The new object must be linked onto the end of the list, not
2500 ** simply added to the start of it in order to ensure that tables
2501 ** appear in the correct order when a changeset or patchset is
2502 ** eventually generated. */
2503 SessionTable **ppTab;
2504 memset(pTab, 0, sizeof(SessionTable));
2505 pTab->zName = (char *)&pTab[1];
2506 memcpy(pTab->zName, zName, nName+1);
2507 for(ppTab=&pSession->pTable; *ppTab; ppTab=&(*ppTab)->pNext);
2508 *ppTab = pTab;
2509 }
2510 }
2511 }
2512
2513 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
2514 return rc;
2515 }
2516
2517 /*
2518 ** Append the value passed as the second argument to the buffer passed
2519 ** as the first.
2520 **
2521 ** This function is a no-op if *pRc is non-zero when it is called.
2522 ** Otherwise, if an error occurs, *pRc is set to an SQLite error code
2523 ** before returning.
2524 */
2525 static void sessionAppendValue(SessionBuffer *p, sqlite3_value *pVal, int *pRc){
2526 int rc = *pRc;
2527 if( rc==SQLITE_OK ){
2528 sqlite3_int64 nByte = 0;
2529 rc = sessionSerializeValue(0, pVal, &nByte);
2530 sessionBufferGrow(p, nByte, &rc);
2531 if( rc==SQLITE_OK ){
2532 rc = sessionSerializeValue(&p->aBuf[p->nBuf], pVal, 0);
2533 p->nBuf += nByte;
2534 }else{
2535 *pRc = rc;
2536 }
2537 }
2538 }
2539
2540 /*
2541 ** This function is a no-op if *pRc is other than SQLITE_OK when it is
2542 ** called. Otherwise, append a single byte to the buffer.
2543 **
2544 ** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
2545 ** returning.
2546 */
2547 static void sessionAppendByte(SessionBuffer *p, u8 v, int *pRc){
2548 if( 0==sessionBufferGrow(p, 1, pRc) ){
2549 p->aBuf[p->nBuf++] = v;
2550 }
2551 }
2552
2553 /*
2554 ** This function is a no-op if *pRc is other than SQLITE_OK when it is
2555 ** called. Otherwise, append a single varint to the buffer.
2556 **
2557 ** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
2558 ** returning.
2559 */
2560 static void sessionAppendVarint(SessionBuffer *p, int v, int *pRc){
2561 if( 0==sessionBufferGrow(p, 9, pRc) ){
2562 p->nBuf += sessionVarintPut(&p->aBuf[p->nBuf], v);
2563 }
2564 }
2565
2566 /*
2567 ** This function is a no-op if *pRc is other than SQLITE_OK when it is
2568 ** called. Otherwise, append a blob of data to the buffer.
2569 **
2570 ** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
2571 ** returning.
2572 */
2573 static void sessionAppendBlob(
2574 SessionBuffer *p,
2575 const u8 *aBlob,
2576 int nBlob,
2577 int *pRc
2578 ){
2579 if( nBlob>0 && 0==sessionBufferGrow(p, nBlob, pRc) ){
2580 memcpy(&p->aBuf[p->nBuf], aBlob, nBlob);
2581 p->nBuf += nBlob;
2582 }
2583 }
2584
2585 /*
2586 ** This function is a no-op if *pRc is other than SQLITE_OK when it is
2587 ** called. Otherwise, append the string representation of integer iVal
2588 ** to the buffer. No nul-terminator is written.
2589 **
2590 ** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
2591 ** returning.
2592 */
2593 static void sessionAppendInteger(
2594 SessionBuffer *p, /* Buffer to append to */
2595 int iVal, /* Value to write the string rep. of */
2596 int *pRc /* IN/OUT: Error code */
2597 ){
2598 char aBuf[24];
2599 sqlite3_snprintf(sizeof(aBuf)-1, aBuf, "%d", iVal);
2600 sessionAppendStr(p, aBuf, pRc);
2601 }
2602
2603 /*
2604 ** This function is a no-op if *pRc is other than SQLITE_OK when it is
2605 ** called. Otherwise, append the string zStr enclosed in quotes (") and
2606 ** with any embedded quote characters escaped to the buffer. No
2607 ** nul-terminator byte is written.
2608 **
2609 ** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
2610 ** returning.
2611 */
2612 static void sessionAppendIdent(
2613 SessionBuffer *p, /* Buffer to a append to */
2614 const char *zStr, /* String to quote, escape and append */
2615 int *pRc /* IN/OUT: Error code */
2616 ){
2617 int nStr = sqlite3Strlen30(zStr)*2 + 2 + 2;
2618 if( 0==sessionBufferGrow(p, nStr, pRc) ){
2619 char *zOut = (char *)&p->aBuf[p->nBuf];
2620 const char *zIn = zStr;
2621 *zOut++ = '"';
2622 if( zIn!=0 ){
2623 while( *zIn ){
2624 if( *zIn=='"' ) *zOut++ = '"';
2625 *zOut++ = *(zIn++);
2626 }
2627 }
2628 *zOut++ = '"';
2629 p->nBuf = (int)((u8 *)zOut - p->aBuf);
2630 p->aBuf[p->nBuf] = 0x00;
2631 }
2632 }
2633
2634 /*
2635 ** This function is a no-op if *pRc is other than SQLITE_OK when it is
2636 ** called. Otherwse, it appends the serialized version of the value stored
2637 ** in column iCol of the row that SQL statement pStmt currently points
2638 ** to to the buffer.
2639 */
2640 static void sessionAppendCol(
2641 SessionBuffer *p, /* Buffer to append to */
2642 sqlite3_stmt *pStmt, /* Handle pointing to row containing value */
2643 int iCol, /* Column to read value from */
2644 int *pRc /* IN/OUT: Error code */
2645 ){
2646 if( *pRc==SQLITE_OK ){
2647 int eType = sqlite3_column_type(pStmt, iCol);
2648 sessionAppendByte(p, (u8)eType, pRc);
2649 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
2650 u8 aBuf[8];
2651 if( eType==SQLITE_INTEGER ){
2652 sqlite3_int64 i = sqlite3_column_int64(pStmt, iCol);
2653 sessionPutI64(aBuf, i);
2654 }else{
2655 double r = sqlite3_column_double(pStmt, iCol);
2656 sessionPutDouble(aBuf, r);
2657 }
2658 sessionAppendBlob(p, aBuf, 8, pRc);
2659 }
2660 if( eType==SQLITE_BLOB || eType==SQLITE_TEXT ){
2661 u8 *z;
2662 int nByte;
2663 if( eType==SQLITE_BLOB ){
2664 z = (u8 *)sqlite3_column_blob(pStmt, iCol);
2665 }else{
2666 z = (u8 *)sqlite3_column_text(pStmt, iCol);
2667 }
2668 nByte = sqlite3_column_bytes(pStmt, iCol);
2669 if( z || (eType==SQLITE_BLOB && nByte==0) ){
2670 sessionAppendVarint(p, nByte, pRc);
2671 sessionAppendBlob(p, z, nByte, pRc);
2672 }else{
2673 *pRc = SQLITE_NOMEM;
2674 }
2675 }
2676 }
2677 }
2678
2679 /*
2680 **
2681 ** This function appends an update change to the buffer (see the comments
2682 ** under "CHANGESET FORMAT" at the top of the file). An update change
2683 ** consists of:
2684 **
2685 ** 1 byte: SQLITE_UPDATE (0x17)
2686 ** n bytes: old.* record (see RECORD FORMAT)
2687 ** m bytes: new.* record (see RECORD FORMAT)
2688 **
2689 ** The SessionChange object passed as the third argument contains the
2690 ** values that were stored in the row when the session began (the old.*
2691 ** values). The statement handle passed as the second argument points
2692 ** at the current version of the row (the new.* values).
2693 **
2694 ** If all of the old.* values are equal to their corresponding new.* value
2695 ** (i.e. nothing has changed), then no data at all is appended to the buffer.
2696 **
2697 ** Otherwise, the old.* record contains all primary key values and the
2698 ** original values of any fields that have been modified. The new.* record
2699 ** contains the new values of only those fields that have been modified.
2700 */
2701 static int sessionAppendUpdate(
2702 SessionBuffer *pBuf, /* Buffer to append to */
2703 int bPatchset, /* True for "patchset", 0 for "changeset" */
2704 sqlite3_stmt *pStmt, /* Statement handle pointing at new row */
2705 SessionChange *p, /* Object containing old values */
2706 u8 *abPK /* Boolean array - true for PK columns */
2707 ){
2708 int rc = SQLITE_OK;
2709 SessionBuffer buf2 = {0,0,0}; /* Buffer to accumulate new.* record in */
2710 int bNoop = 1; /* Set to zero if any values are modified */
2711 int nRewind = pBuf->nBuf; /* Set to zero if any values are modified */
2712 int i; /* Used to iterate through columns */
2713 u8 *pCsr = p->aRecord; /* Used to iterate through old.* values */
2714
2715 assert( abPK!=0 );
2716 sessionAppendByte(pBuf, SQLITE_UPDATE, &rc);
2717 sessionAppendByte(pBuf, p->bIndirect, &rc);
2718 for(i=0; i<sqlite3_column_count(pStmt); i++){
2719 int bChanged = 0;
2720 int nAdvance;
2721 int eType = *pCsr;
2722 switch( eType ){
2723 case SQLITE_NULL:
2724 nAdvance = 1;
2725 if( sqlite3_column_type(pStmt, i)!=SQLITE_NULL ){
2726 bChanged = 1;
2727 }
2728 break;
2729
2730 case SQLITE_FLOAT:
2731 case SQLITE_INTEGER: {
2732 nAdvance = 9;
2733 if( eType==sqlite3_column_type(pStmt, i) ){
2734 sqlite3_int64 iVal = sessionGetI64(&pCsr[1]);
2735 if( eType==SQLITE_INTEGER ){
2736 if( iVal==sqlite3_column_int64(pStmt, i) ) break;
2737 }else{
2738 double dVal;
2739 memcpy(&dVal, &iVal, 8);
2740 if( dVal==sqlite3_column_double(pStmt, i) ) break;
2741 }
2742 }
2743 bChanged = 1;
2744 break;
2745 }
2746
2747 default: {
2748 int n;
2749 int nHdr = 1 + sessionVarintGet(&pCsr[1], &n);
2750 assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
2751 nAdvance = nHdr + n;
2752 if( eType==sqlite3_column_type(pStmt, i)
2753 && n==sqlite3_column_bytes(pStmt, i)
2754 && (n==0 || 0==memcmp(&pCsr[nHdr], sqlite3_column_blob(pStmt, i), n))
2755 ){
2756 break;
2757 }
2758 bChanged = 1;
2759 }
2760 }
2761
2762 /* If at least one field has been modified, this is not a no-op. */
2763 if( bChanged ) bNoop = 0;
2764
2765 /* Add a field to the old.* record. This is omitted if this module is
2766 ** currently generating a patchset. */
2767 if( bPatchset==0 ){
2768 if( bChanged || abPK[i] ){
2769 sessionAppendBlob(pBuf, pCsr, nAdvance, &rc);
2770 }else{
2771 sessionAppendByte(pBuf, 0, &rc);
2772 }
2773 }
2774
2775 /* Add a field to the new.* record. Or the only record if currently
2776 ** generating a patchset. */
2777 if( bChanged || (bPatchset && abPK[i]) ){
2778 sessionAppendCol(&buf2, pStmt, i, &rc);
2779 }else{
2780 sessionAppendByte(&buf2, 0, &rc);
2781 }
2782
2783 pCsr += nAdvance;
2784 }
2785
2786 if( bNoop ){
2787 pBuf->nBuf = nRewind;
2788 }else{
2789 sessionAppendBlob(pBuf, buf2.aBuf, buf2.nBuf, &rc);
2790 }
2791 sqlite3_free(buf2.aBuf);
2792
2793 return rc;
2794 }
2795
2796 /*
2797 ** Append a DELETE change to the buffer passed as the first argument. Use
2798 ** the changeset format if argument bPatchset is zero, or the patchset
2799 ** format otherwise.
2800 */
2801 static int sessionAppendDelete(
2802 SessionBuffer *pBuf, /* Buffer to append to */
2803 int bPatchset, /* True for "patchset", 0 for "changeset" */
2804 SessionChange *p, /* Object containing old values */
2805 int nCol, /* Number of columns in table */
2806 u8 *abPK /* Boolean array - true for PK columns */
2807 ){
2808 int rc = SQLITE_OK;
2809
2810 sessionAppendByte(pBuf, SQLITE_DELETE, &rc);
2811 sessionAppendByte(pBuf, p->bIndirect, &rc);
2812
2813 if( bPatchset==0 ){
2814 sessionAppendBlob(pBuf, p->aRecord, p->nRecord, &rc);
2815 }else{
2816 int i;
2817 u8 *a = p->aRecord;
2818 for(i=0; i<nCol; i++){
2819 u8 *pStart = a;
2820 int eType = *a++;
2821
2822 switch( eType ){
2823 case 0:
2824 case SQLITE_NULL:
2825 assert( abPK[i]==0 );
2826 break;
2827
2828 case SQLITE_FLOAT:
2829 case SQLITE_INTEGER:
2830 a += 8;
2831 break;
2832
2833 default: {
2834 int n;
2835 a += sessionVarintGet(a, &n);
2836 a += n;
2837 break;
2838 }
2839 }
2840 if( abPK[i] ){
2841 sessionAppendBlob(pBuf, pStart, (int)(a-pStart), &rc);
2842 }
2843 }
2844 assert( (a - p->aRecord)==p->nRecord );
2845 }
2846
2847 return rc;
2848 }
2849
2850 static int sessionPrepare(
2851 sqlite3 *db,
2852 sqlite3_stmt **pp,
2853 char **pzErrmsg,
2854 const char *zSql
2855 ){
2856 int rc = sqlite3_prepare_v2(db, zSql, -1, pp, 0);
2857 if( pzErrmsg && rc!=SQLITE_OK ){
2858 *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
2859 }
2860 return rc;
2861 }
2862
2863 /*
2864 ** Formulate and prepare a SELECT statement to retrieve a row from table
2865 ** zTab in database zDb based on its primary key. i.e.
2866 **
2867 ** SELECT *, <noop-test> FROM zDb.zTab WHERE (pk1, pk2,...) IS (?1, ?2,...)
2868 **
2869 ** where <noop-test> is:
2870 **
2871 ** 1 AND (?A OR ?1 IS <column>) AND ...
2872 **
2873 ** for each non-pk <column>.
2874 */
2875 static int sessionSelectStmt(
2876 sqlite3 *db, /* Database handle */
2877 int bIgnoreNoop,
2878 const char *zDb, /* Database name */
2879 const char *zTab, /* Table name */
2880 int bRowid,
2881 int nCol, /* Number of columns in table */
2882 const char **azCol, /* Names of table columns */
2883 u8 *abPK, /* PRIMARY KEY array */
2884 sqlite3_stmt **ppStmt, /* OUT: Prepared SELECT statement */
2885 char **pzErrmsg /* OUT: Error message */
2886 ){
2887 int rc = SQLITE_OK;
2888 char *zSql = 0;
2889 const char *zSep = "";
2890 int i;
2891
2892 SessionBuffer cols = {0, 0, 0};
2893 SessionBuffer nooptest = {0, 0, 0};
2894 SessionBuffer pkfield = {0, 0, 0};
2895 SessionBuffer pkvar = {0, 0, 0};
2896
2897 sessionAppendStr(&nooptest, ", 1", &rc);
2898
2899 if( 0==sqlite3_stricmp("sqlite_stat1", zTab) ){
2900 sessionAppendStr(&nooptest, " AND (?6 OR ?3 IS stat)", &rc);
2901 sessionAppendStr(&pkfield, "tbl, idx", &rc);
2902 sessionAppendStr(&pkvar,
2903 "?1, (CASE WHEN ?2=X'' THEN NULL ELSE ?2 END)", &rc
2904 );
2905 sessionAppendStr(&cols, "tbl, ?2, stat", &rc);
2906 }else{
2907 #if 0
2908 if( bRowid ){
2909 sessionAppendStr(&cols, SESSIONS_ROWID, &rc);
2910 }
2911 #endif
2912 for(i=0; i<nCol; i++){
2913 if( cols.nBuf ) sessionAppendStr(&cols, ", ", &rc);
2914 sessionAppendIdent(&cols, azCol[i], &rc);
2915 if( abPK[i] ){
2916 sessionAppendStr(&pkfield, zSep, &rc);
2917 sessionAppendStr(&pkvar, zSep, &rc);
2918 zSep = ", ";
2919 sessionAppendIdent(&pkfield, azCol[i], &rc);
2920 sessionAppendPrintf(&pkvar, &rc, "?%d", i+1);
2921 }else{
2922 sessionAppendPrintf(&nooptest, &rc,
2923 " AND (?%d OR ?%d IS %w.%w)", i+1+nCol, i+1, zTab, azCol[i]
2924 );
2925 }
2926 }
2927 }
2928
2929 if( rc==SQLITE_OK ){
2930 zSql = sqlite3_mprintf(
2931 "SELECT %s%s FROM %Q.%Q WHERE (%s) IS (%s)",
2932 (char*)cols.aBuf, (bIgnoreNoop ? (char*)nooptest.aBuf : ""),
2933 zDb, zTab, (char*)pkfield.aBuf, (char*)pkvar.aBuf
2934 );
2935 if( zSql==0 ) rc = SQLITE_NOMEM;
2936 }
2937
2938 #if 0
2939 if( 0==sqlite3_stricmp("sqlite_stat1", zTab) ){
2940 zSql = sqlite3_mprintf(
2941 "SELECT tbl, ?2, stat FROM %Q.sqlite_stat1 WHERE tbl IS ?1 AND "
2942 "idx IS (CASE WHEN ?2=X'' THEN NULL ELSE ?2 END)", zDb
2943 );
2944 if( zSql==0 ) rc = SQLITE_NOMEM;
2945 }else{
2946 const char *zSep = "";
2947 SessionBuffer buf = {0, 0, 0};
2948
2949 sessionAppendStr(&buf, "SELECT * FROM ", &rc);
2950 sessionAppendIdent(&buf, zDb, &rc);
2951 sessionAppendStr(&buf, ".", &rc);
2952 sessionAppendIdent(&buf, zTab, &rc);
2953 sessionAppendStr(&buf, " WHERE ", &rc);
2954 for(i=0; i<nCol; i++){
2955 if( abPK[i] ){
2956 sessionAppendStr(&buf, zSep, &rc);
2957 sessionAppendIdent(&buf, azCol[i], &rc);
2958 sessionAppendStr(&buf, " IS ?", &rc);
2959 sessionAppendInteger(&buf, i+1, &rc);
2960 zSep = " AND ";
2961 }
2962 }
2963 zSql = (char*)buf.aBuf;
2964 nSql = buf.nBuf;
2965 }
2966 #endif
2967
2968 if( rc==SQLITE_OK ){
2969 rc = sessionPrepare(db, ppStmt, pzErrmsg, zSql);
2970 }
2971 sqlite3_free(zSql);
2972 sqlite3_free(nooptest.aBuf);
2973 sqlite3_free(pkfield.aBuf);
2974 sqlite3_free(pkvar.aBuf);
2975 sqlite3_free(cols.aBuf);
2976 return rc;
2977 }
2978
2979 /*
2980 ** Bind the PRIMARY KEY values from the change passed in argument pChange
2981 ** to the SELECT statement passed as the first argument. The SELECT statement
2982 ** is as prepared by function sessionSelectStmt().
2983 **
2984 ** Return SQLITE_OK if all PK values are successfully bound, or an SQLite
2985 ** error code (e.g. SQLITE_NOMEM) otherwise.
2986 */
2987 static int sessionSelectBind(
2988 sqlite3_stmt *pSelect, /* SELECT from sessionSelectStmt() */
2989 int nCol, /* Number of columns in table */
2990 u8 *abPK, /* PRIMARY KEY array */
2991 SessionChange *pChange /* Change structure */
2992 ){
2993 int i;
2994 int rc = SQLITE_OK;
2995 u8 *a = pChange->aRecord;
2996
2997 for(i=0; i<nCol && rc==SQLITE_OK; i++){
2998 int eType = *a++;
2999
3000 switch( eType ){
3001 case 0:
3002 case SQLITE_NULL:
3003 assert( abPK[i]==0 );
3004 break;
3005
3006 case SQLITE_INTEGER: {
3007 if( abPK[i] ){
3008 i64 iVal = sessionGetI64(a);
3009 rc = sqlite3_bind_int64(pSelect, i+1, iVal);
3010 }
3011 a += 8;
3012 break;
3013 }
3014
3015 case SQLITE_FLOAT: {
3016 if( abPK[i] ){
3017 double rVal;
3018 i64 iVal = sessionGetI64(a);
3019 memcpy(&rVal, &iVal, 8);
3020 rc = sqlite3_bind_double(pSelect, i+1, rVal);
3021 }
3022 a += 8;
3023 break;
3024 }
3025
3026 case SQLITE_TEXT: {
3027 int n;
3028 a += sessionVarintGet(a, &n);
3029 if( abPK[i] ){
3030 rc = sqlite3_bind_text(pSelect, i+1, (char *)a, n, SQLITE_TRANSIENT);
3031 }
3032 a += n;
3033 break;
3034 }
3035
3036 default: {
3037 int n;
3038 assert( eType==SQLITE_BLOB );
3039 a += sessionVarintGet(a, &n);
3040 if( abPK[i] ){
3041 rc = sqlite3_bind_blob(pSelect, i+1, a, n, SQLITE_TRANSIENT);
3042 }
3043 a += n;
3044 break;
3045 }
3046 }
3047 }
3048
3049 return rc;
3050 }
3051
3052 /*
3053 ** This function is a no-op if *pRc is set to other than SQLITE_OK when it
3054 ** is called. Otherwise, append a serialized table header (part of the binary
3055 ** changeset format) to buffer *pBuf. If an error occurs, set *pRc to an
3056 ** SQLite error code before returning.
3057 */
3058 static void sessionAppendTableHdr(
3059 SessionBuffer *pBuf, /* Append header to this buffer */
3060 int bPatchset, /* Use the patchset format if true */
3061 SessionTable *pTab, /* Table object to append header for */
3062 int *pRc /* IN/OUT: Error code */
3063 ){
3064 /* Write a table header */
3065 sessionAppendByte(pBuf, (bPatchset ? 'P' : 'T'), pRc);
3066 sessionAppendVarint(pBuf, pTab->nCol, pRc);
3067 sessionAppendBlob(pBuf, pTab->abPK, pTab->nCol, pRc);
3068 sessionAppendBlob(pBuf, (u8 *)pTab->zName, (int)strlen(pTab->zName)+1, pRc);
3069 }
3070
3071 /*
3072 ** Generate either a changeset (if argument bPatchset is zero) or a patchset
3073 ** (if it is non-zero) based on the current contents of the session object
3074 ** passed as the first argument.
3075 **
3076 ** If no error occurs, SQLITE_OK is returned and the new changeset/patchset
3077 ** stored in output variables *pnChangeset and *ppChangeset. Or, if an error
3078 ** occurs, an SQLite error code is returned and both output variables set
3079 ** to 0.
3080 */
3081 static int sessionGenerateChangeset(
3082 sqlite3_session *pSession, /* Session object */
3083 int bPatchset, /* True for patchset, false for changeset */
3084 int (*xOutput)(void *pOut, const void *pData, int nData),
3085 void *pOut, /* First argument for xOutput */
3086 int *pnChangeset, /* OUT: Size of buffer at *ppChangeset */
3087 void **ppChangeset /* OUT: Buffer containing changeset */
3088 ){
3089 sqlite3 *db = pSession->db; /* Source database handle */
3090 SessionTable *pTab; /* Used to iterate through attached tables */
3091 SessionBuffer buf = {0,0,0}; /* Buffer in which to accumulate changeset */
3092 int rc; /* Return code */
3093
3094 assert( xOutput==0 || (pnChangeset==0 && ppChangeset==0) );
3095 assert( xOutput!=0 || (pnChangeset!=0 && ppChangeset!=0) );
3096
3097 /* Zero the output variables in case an error occurs. If this session
3098 ** object is already in the error state (sqlite3_session.rc != SQLITE_OK),
3099 ** this call will be a no-op. */
3100 if( xOutput==0 ){
3101 assert( pnChangeset!=0 && ppChangeset!=0 );
3102 *pnChangeset = 0;
3103 *ppChangeset = 0;
3104 }
3105
3106 if( pSession->rc ) return pSession->rc;
3107
3108 sqlite3_mutex_enter(sqlite3_db_mutex(db));
3109 rc = sqlite3_exec(pSession->db, "SAVEPOINT changeset", 0, 0, 0);
3110 if( rc!=SQLITE_OK ){
3111 sqlite3_mutex_leave(sqlite3_db_mutex(db));
3112 return rc;
3113 }
3114
3115 for(pTab=pSession->pTable; rc==SQLITE_OK && pTab; pTab=pTab->pNext){
3116 if( pTab->nEntry ){
3117 const char *zName = pTab->zName;
3118 int i; /* Used to iterate through hash buckets */
3119 sqlite3_stmt *pSel = 0; /* SELECT statement to query table pTab */
3120 int nRewind = buf.nBuf; /* Initial size of write buffer */
3121 int nNoop; /* Size of buffer after writing tbl header */
3122 int nOldCol = pTab->nCol;
3123
3124 /* Check the table schema is still Ok. */
3125 rc = sessionReinitTable(pSession, pTab);
3126 if( rc==SQLITE_OK && pTab->nCol!=nOldCol ){
3127 rc = sessionUpdateChanges(pSession, pTab);
3128 }
3129
3130 /* Write a table header */
3131 sessionAppendTableHdr(&buf, bPatchset, pTab, &rc);
3132
3133 /* Build and compile a statement to execute: */
3134 if( rc==SQLITE_OK ){
3135 rc = sessionSelectStmt(db, 0, pSession->zDb,
3136 zName, pTab->bRowid, pTab->nCol, pTab->azCol, pTab->abPK, &pSel, 0
3137 );
3138 }
3139
3140 nNoop = buf.nBuf;
3141 for(i=0; i<pTab->nChange && rc==SQLITE_OK; i++){
3142 SessionChange *p; /* Used to iterate through changes */
3143
3144 for(p=pTab->apChange[i]; rc==SQLITE_OK && p; p=p->pNext){
3145 rc = sessionSelectBind(pSel, pTab->nCol, pTab->abPK, p);
3146 if( rc!=SQLITE_OK ) continue;
3147 if( sqlite3_step(pSel)==SQLITE_ROW ){
3148 if( p->op==SQLITE_INSERT ){
3149 int iCol;
3150 sessionAppendByte(&buf, SQLITE_INSERT, &rc);
3151 sessionAppendByte(&buf, p->bIndirect, &rc);
3152 for(iCol=0; iCol<pTab->nCol; iCol++){
3153 sessionAppendCol(&buf, pSel, iCol, &rc);
3154 }
3155 }else{
3156 assert( pTab->abPK!=0 );
3157 rc = sessionAppendUpdate(&buf, bPatchset, pSel, p, pTab->abPK);
3158 }
3159 }else if( p->op!=SQLITE_INSERT ){
3160 rc = sessionAppendDelete(&buf, bPatchset, p, pTab->nCol,pTab->abPK);
3161 }
3162 if( rc==SQLITE_OK ){
3163 rc = sqlite3_reset(pSel);
3164 }
3165
3166 /* If the buffer is now larger than sessions_strm_chunk_size, pass
3167 ** its contents to the xOutput() callback. */
3168 if( xOutput
3169 && rc==SQLITE_OK
3170 && buf.nBuf>nNoop
3171 && buf.nBuf>sessions_strm_chunk_size
3172 ){
3173 rc = xOutput(pOut, (void*)buf.aBuf, buf.nBuf);
3174 nNoop = -1;
3175 buf.nBuf = 0;
3176 }
3177
3178 }
3179 }
3180
3181 sqlite3_finalize(pSel);
3182 if( buf.nBuf==nNoop ){
3183 buf.nBuf = nRewind;
3184 }
3185 }
3186 }
3187
3188 if( rc==SQLITE_OK ){
3189 if( xOutput==0 ){
3190 *pnChangeset = buf.nBuf;
3191 *ppChangeset = buf.aBuf;
3192 buf.aBuf = 0;
3193 }else if( buf.nBuf>0 ){
3194 rc = xOutput(pOut, (void*)buf.aBuf, buf.nBuf);
3195 }
3196 }
3197
3198 sqlite3_free(buf.aBuf);
3199 sqlite3_exec(db, "RELEASE changeset", 0, 0, 0);
3200 sqlite3_mutex_leave(sqlite3_db_mutex(db));
3201 return rc;
3202 }
3203
3204 /*
3205 ** Obtain a changeset object containing all changes recorded by the
3206 ** session object passed as the first argument.
3207 **
3208 ** It is the responsibility of the caller to eventually free the buffer
3209 ** using sqlite3_free().
3210 */
3211 int sqlite3session_changeset(
3212 sqlite3_session *pSession, /* Session object */
3213 int *pnChangeset, /* OUT: Size of buffer at *ppChangeset */
3214 void **ppChangeset /* OUT: Buffer containing changeset */
3215 ){
3216 int rc;
3217
3218 if( pnChangeset==0 || ppChangeset==0 ) return SQLITE_MISUSE;
3219 rc = sessionGenerateChangeset(pSession, 0, 0, 0, pnChangeset, ppChangeset);
3220 assert( rc || pnChangeset==0
3221 || pSession->bEnableSize==0 || *pnChangeset<=pSession->nMaxChangesetSize
3222 );
3223 return rc;
3224 }
3225
3226 /*
3227 ** Streaming version of sqlite3session_changeset().
3228 */
3229 int sqlite3session_changeset_strm(
3230 sqlite3_session *pSession,
3231 int (*xOutput)(void *pOut, const void *pData, int nData),
3232 void *pOut
3233 ){
3234 if( xOutput==0 ) return SQLITE_MISUSE;
3235 return sessionGenerateChangeset(pSession, 0, xOutput, pOut, 0, 0);
3236 }
3237
3238 /*
3239 ** Streaming version of sqlite3session_patchset().
3240 */
3241 int sqlite3session_patchset_strm(
3242 sqlite3_session *pSession,
3243 int (*xOutput)(void *pOut, const void *pData, int nData),
3244 void *pOut
3245 ){
3246 if( xOutput==0 ) return SQLITE_MISUSE;
3247 return sessionGenerateChangeset(pSession, 1, xOutput, pOut, 0, 0);
3248 }
3249
3250 /*
3251 ** Obtain a patchset object containing all changes recorded by the
3252 ** session object passed as the first argument.
3253 **
3254 ** It is the responsibility of the caller to eventually free the buffer
3255 ** using sqlite3_free().
3256 */
3257 int sqlite3session_patchset(
3258 sqlite3_session *pSession, /* Session object */
3259 int *pnPatchset, /* OUT: Size of buffer at *ppChangeset */
3260 void **ppPatchset /* OUT: Buffer containing changeset */
3261 ){
3262 if( pnPatchset==0 || ppPatchset==0 ) return SQLITE_MISUSE;
3263 return sessionGenerateChangeset(pSession, 1, 0, 0, pnPatchset, ppPatchset);
3264 }
3265
3266 /*
3267 ** Enable or disable the session object passed as the first argument.
3268 */
3269 int sqlite3session_enable(sqlite3_session *pSession, int bEnable){
3270 int ret;
3271 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
3272 if( bEnable>=0 ){
3273 pSession->bEnable = bEnable;
3274 }
3275 ret = pSession->bEnable;
3276 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
3277 return ret;
3278 }
3279
3280 /*
3281 ** Enable or disable the session object passed as the first argument.
3282 */
3283 int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect){
3284 int ret;
3285 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
3286 if( bIndirect>=0 ){
3287 pSession->bIndirect = bIndirect;
3288 }
3289 ret = pSession->bIndirect;
3290 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
3291 return ret;
3292 }
3293
3294 /*
3295 ** Return true if there have been no changes to monitored tables recorded
3296 ** by the session object passed as the only argument.
3297 */
3298 int sqlite3session_isempty(sqlite3_session *pSession){
3299 int ret = 0;
3300 SessionTable *pTab;
3301
3302 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
3303 for(pTab=pSession->pTable; pTab && ret==0; pTab=pTab->pNext){
3304 ret = (pTab->nEntry>0);
3305 }
3306 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
3307
3308 return (ret==0);
3309 }
3310
3311 /*
3312 ** Return the amount of heap memory in use.
3313 */
3314 sqlite3_int64 sqlite3session_memory_used(sqlite3_session *pSession){
3315 return pSession->nMalloc;
3316 }
3317
3318 /*
3319 ** Configure the session object passed as the first argument.
3320 */
3321 int sqlite3session_object_config(sqlite3_session *pSession, int op, void *pArg){
3322 int rc = SQLITE_OK;
3323 switch( op ){
3324 case SQLITE_SESSION_OBJCONFIG_SIZE: {
3325 int iArg = *(int*)pArg;
3326 if( iArg>=0 ){
3327 if( pSession->pTable ){
3328 rc = SQLITE_MISUSE;
3329 }else{
3330 pSession->bEnableSize = (iArg!=0);
3331 }
3332 }
3333 *(int*)pArg = pSession->bEnableSize;
3334 break;
3335 }
3336
3337 case SQLITE_SESSION_OBJCONFIG_ROWID: {
3338 int iArg = *(int*)pArg;
3339 if( iArg>=0 ){
3340 if( pSession->pTable ){
3341 rc = SQLITE_MISUSE;
3342 }else{
3343 pSession->bImplicitPK = (iArg!=0);
3344 }
3345 }
3346 *(int*)pArg = pSession->bImplicitPK;
3347 break;
3348 }
3349
3350 default:
3351 rc = SQLITE_MISUSE;
3352 }
3353
3354 return rc;
3355 }
3356
3357 /*
3358 ** Return the maximum size of sqlite3session_changeset() output.
3359 */
3360 sqlite3_int64 sqlite3session_changeset_size(sqlite3_session *pSession){
3361 return pSession->nMaxChangesetSize;
3362 }
3363
3364 /*
3365 ** Do the work for either sqlite3changeset_start() or start_strm().
3366 */
3367 static int sessionChangesetStart(
3368 sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */
3369 int (*xInput)(void *pIn, void *pData, int *pnData),
3370 void *pIn,
3371 int nChangeset, /* Size of buffer pChangeset in bytes */
3372 void *pChangeset, /* Pointer to buffer containing changeset */
3373 int bInvert, /* True to invert changeset */
3374 int bSkipEmpty /* True to skip empty UPDATE changes */
3375 ){
3376 sqlite3_changeset_iter *pRet; /* Iterator to return */
3377 int nByte; /* Number of bytes to allocate for iterator */
3378
3379 assert( xInput==0 || (pChangeset==0 && nChangeset==0) );
3380
3381 /* Zero the output variable in case an error occurs. */
3382 *pp = 0;
3383
3384 /* Allocate and initialize the iterator structure. */
3385 nByte = sizeof(sqlite3_changeset_iter);
3386 pRet = (sqlite3_changeset_iter *)sqlite3_malloc(nByte);
3387 if( !pRet ) return SQLITE_NOMEM;
3388 memset(pRet, 0, sizeof(sqlite3_changeset_iter));
3389 pRet->in.aData = (u8 *)pChangeset;
3390 pRet->in.nData = nChangeset;
3391 pRet->in.xInput = xInput;
3392 pRet->in.pIn = pIn;
3393 pRet->in.bEof = (xInput ? 0 : 1);
3394 pRet->bInvert = bInvert;
3395 pRet->bSkipEmpty = bSkipEmpty;
3396
3397 /* Populate the output variable and return success. */
3398 *pp = pRet;
3399 return SQLITE_OK;
3400 }
3401
3402 /*
3403 ** Create an iterator used to iterate through the contents of a changeset.
3404 */
3405 int sqlite3changeset_start(
3406 sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */
3407 int nChangeset, /* Size of buffer pChangeset in bytes */
3408 void *pChangeset /* Pointer to buffer containing changeset */
3409 ){
3410 return sessionChangesetStart(pp, 0, 0, nChangeset, pChangeset, 0, 0);
3411 }
3412 int sqlite3changeset_start_v2(
3413 sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */
3414 int nChangeset, /* Size of buffer pChangeset in bytes */
3415 void *pChangeset, /* Pointer to buffer containing changeset */
3416 int flags
3417 ){
3418 int bInvert = !!(flags & SQLITE_CHANGESETSTART_INVERT);
3419 return sessionChangesetStart(pp, 0, 0, nChangeset, pChangeset, bInvert, 0);
3420 }
3421
3422 /*
3423 ** Streaming version of sqlite3changeset_start().
3424 */
3425 int sqlite3changeset_start_strm(
3426 sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */
3427 int (*xInput)(void *pIn, void *pData, int *pnData),
3428 void *pIn
3429 ){
3430 return sessionChangesetStart(pp, xInput, pIn, 0, 0, 0, 0);
3431 }
3432 int sqlite3changeset_start_v2_strm(
3433 sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */
3434 int (*xInput)(void *pIn, void *pData, int *pnData),
3435 void *pIn,
3436 int flags
3437 ){
3438 int bInvert = !!(flags & SQLITE_CHANGESETSTART_INVERT);
3439 return sessionChangesetStart(pp, xInput, pIn, 0, 0, bInvert, 0);
3440 }
3441
3442 /*
3443 ** If the SessionInput object passed as the only argument is a streaming
3444 ** object and the buffer is full, discard some data to free up space.
3445 */
3446 static void sessionDiscardData(SessionInput *pIn){
3447 if( pIn->xInput && pIn->iCurrent>=sessions_strm_chunk_size ){
3448 int nMove = pIn->buf.nBuf - pIn->iCurrent;
3449 assert( nMove>=0 );
3450 if( nMove>0 ){
3451 memmove(pIn->buf.aBuf, &pIn->buf.aBuf[pIn->iCurrent], nMove);
3452 }
3453 pIn->buf.nBuf -= pIn->iCurrent;
3454 pIn->iNext -= pIn->iCurrent;
3455 pIn->iCurrent = 0;
3456 pIn->nData = pIn->buf.nBuf;
3457 }
3458 }
3459
3460 /*
3461 ** Ensure that there are at least nByte bytes available in the buffer. Or,
3462 ** if there are not nByte bytes remaining in the input, that all available
3463 ** data is in the buffer.
3464 **
3465 ** Return an SQLite error code if an error occurs, or SQLITE_OK otherwise.
3466 */
3467 static int sessionInputBuffer(SessionInput *pIn, int nByte){
3468 int rc = SQLITE_OK;
3469 if( pIn->xInput ){
3470 while( !pIn->bEof && (pIn->iNext+nByte)>=pIn->nData && rc==SQLITE_OK ){
3471 int nNew = sessions_strm_chunk_size;
3472
3473 if( pIn->bNoDiscard==0 ) sessionDiscardData(pIn);
3474 if( SQLITE_OK==sessionBufferGrow(&pIn->buf, nNew, &rc) ){
3475 rc = pIn->xInput(pIn->pIn, &pIn->buf.aBuf[pIn->buf.nBuf], &nNew);
3476 if( nNew==0 ){
3477 pIn->bEof = 1;
3478 }else{
3479 pIn->buf.nBuf += nNew;
3480 }
3481 }
3482
3483 pIn->aData = pIn->buf.aBuf;
3484 pIn->nData = pIn->buf.nBuf;
3485 }
3486 }
3487 return rc;
3488 }
3489
3490 /*
3491 ** When this function is called, *ppRec points to the start of a record
3492 ** that contains nCol values. This function advances the pointer *ppRec
3493 ** until it points to the byte immediately following that record.
3494 */
3495 static void sessionSkipRecord(
3496 u8 **ppRec, /* IN/OUT: Record pointer */
3497 int nCol /* Number of values in record */
3498 ){
3499 u8 *aRec = *ppRec;
3500 int i;
3501 for(i=0; i<nCol; i++){
3502 int eType = *aRec++;
3503 if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
3504 int nByte;
3505 aRec += sessionVarintGet((u8*)aRec, &nByte);
3506 aRec += nByte;
3507 }else if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
3508 aRec += 8;
3509 }
3510 }
3511
3512 *ppRec = aRec;
3513 }
3514
3515 /*
3516 ** This function sets the value of the sqlite3_value object passed as the
3517 ** first argument to a copy of the string or blob held in the aData[]
3518 ** buffer. SQLITE_OK is returned if successful, or SQLITE_NOMEM if an OOM
3519 ** error occurs.
3520 */
3521 static int sessionValueSetStr(
3522 sqlite3_value *pVal, /* Set the value of this object */
3523 u8 *aData, /* Buffer containing string or blob data */
3524 int nData, /* Size of buffer aData[] in bytes */
3525 u8 enc /* String encoding (0 for blobs) */
3526 ){
3527 /* In theory this code could just pass SQLITE_TRANSIENT as the final
3528 ** argument to sqlite3ValueSetStr() and have the copy created
3529 ** automatically. But doing so makes it difficult to detect any OOM
3530 ** error. Hence the code to create the copy externally. */
3531 u8 *aCopy = sqlite3_malloc64((sqlite3_int64)nData+1);
3532 if( aCopy==0 ) return SQLITE_NOMEM;
3533 memcpy(aCopy, aData, nData);
3534 sqlite3ValueSetStr(pVal, nData, (char*)aCopy, enc, sqlite3_free);
3535 return SQLITE_OK;
3536 }
3537
3538 /*
3539 ** Deserialize a single record from a buffer in memory. See "RECORD FORMAT"
3540 ** for details.
3541 **
3542 ** When this function is called, *paChange points to the start of the record
3543 ** to deserialize. Assuming no error occurs, *paChange is set to point to
3544 ** one byte after the end of the same record before this function returns.
3545 ** If the argument abPK is NULL, then the record contains nCol values. Or,
3546 ** if abPK is other than NULL, then the record contains only the PK fields
3547 ** (in other words, it is a patchset DELETE record).
3548 **
3549 ** If successful, each element of the apOut[] array (allocated by the caller)
3550 ** is set to point to an sqlite3_value object containing the value read
3551 ** from the corresponding position in the record. If that value is not
3552 ** included in the record (i.e. because the record is part of an UPDATE change
3553 ** and the field was not modified), the corresponding element of apOut[] is
3554 ** set to NULL.
3555 **
3556 ** It is the responsibility of the caller to free all sqlite_value structures
3557 ** using sqlite3_free().
3558 **
3559 ** If an error occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned.
3560 ** The apOut[] array may have been partially populated in this case.
3561 */
3562 static int sessionReadRecord(
3563 SessionInput *pIn, /* Input data */
3564 int nCol, /* Number of values in record */
3565 u8 *abPK, /* Array of primary key flags, or NULL */
3566 sqlite3_value **apOut, /* Write values to this array */
3567 int *pbEmpty
3568 ){
3569 int i; /* Used to iterate through columns */
3570 int rc = SQLITE_OK;
3571
3572 assert( pbEmpty==0 || *pbEmpty==0 );
3573 if( pbEmpty ) *pbEmpty = 1;
3574 for(i=0; i<nCol && rc==SQLITE_OK; i++){
3575 int eType = 0; /* Type of value (SQLITE_NULL, TEXT etc.) */
3576 if( abPK && abPK[i]==0 ) continue;
3577 rc = sessionInputBuffer(pIn, 9);
3578 if( rc==SQLITE_OK ){
3579 if( pIn->iNext>=pIn->nData ){
3580 rc = SQLITE_CORRUPT_BKPT;
3581 }else{
3582 eType = pIn->aData[pIn->iNext++];
3583 assert( apOut[i]==0 );
3584 if( eType ){
3585 if( pbEmpty ) *pbEmpty = 0;
3586 apOut[i] = sqlite3ValueNew(0);
3587 if( !apOut[i] ) rc = SQLITE_NOMEM;
3588 }
3589 }
3590 }
3591
3592 if( rc==SQLITE_OK ){
3593 u8 *aVal = &pIn->aData[pIn->iNext];
3594 if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
3595 int nByte;
3596 int nRem = pIn->nData - pIn->iNext;
3597 pIn->iNext += sessionVarintGetSafe(aVal, nRem, &nByte);
3598 rc = sessionInputBuffer(pIn, nByte);
3599 if( rc==SQLITE_OK ){
3600 if( nByte<0 || nByte>pIn->nData-pIn->iNext ){
3601 rc = SQLITE_CORRUPT_BKPT;
3602 }else{
3603 u8 enc = (eType==SQLITE_TEXT ? SQLITE_UTF8 : 0);
3604 rc = sessionValueSetStr(apOut[i],&pIn->aData[pIn->iNext],nByte,enc);
3605 pIn->iNext += nByte;
3606 }
3607 }
3608 }
3609 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
3610 if( (pIn->nData-pIn->iNext)<8 ){
3611 rc = SQLITE_CORRUPT_BKPT;
3612 }else{
3613 sqlite3_int64 v = sessionGetI64(aVal);
3614 if( eType==SQLITE_INTEGER ){
3615 sqlite3VdbeMemSetInt64(apOut[i], v);
3616 }else{
3617 double d;
3618 memcpy(&d, &v, 8);
3619 sqlite3VdbeMemSetDouble(apOut[i], d);
3620 }
3621 pIn->iNext += 8;
3622 }
3623 }
3624 }
3625 }
3626
3627 return rc;
3628 }
3629
3630 /*
3631 ** The input pointer currently points to the second byte of a table-header.
3632 ** Specifically, to the following:
3633 **
3634 ** + number of columns in table (varint)
3635 ** + array of PK flags (1 byte per column),
3636 ** + table name (nul terminated).
3637 **
3638 ** This function ensures that all of the above is present in the input
3639 ** buffer (i.e. that it can be accessed without any calls to xInput()).
3640 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
3641 ** The input pointer is not moved.
3642 */
3643 static int sessionChangesetBufferTblhdr(SessionInput *pIn, int *pnByte){
3644 int rc = SQLITE_OK;
3645 int nCol = 0;
3646 int nRead = 0;
3647
3648 rc = sessionInputBuffer(pIn, 9);
3649 if( rc==SQLITE_OK ){
3650 int nBuf = pIn->nData - pIn->iNext;
3651 nRead += sessionVarintGetSafe(&pIn->aData[pIn->iNext], nBuf, &nCol);
3652 /* The hard upper limit for the number of columns in an SQLite
3653 ** database table is, according to sqliteLimit.h, 32676. So
3654 ** consider any table-header that purports to have more than 65536
3655 ** columns to be corrupt. This is convenient because otherwise,
3656 ** if the (nCol>65536) condition below were omitted, a sufficiently
3657 ** large value for nCol may cause nRead to wrap around and become
3658 ** negative. Leading to a crash. */
3659 if( nCol<0 || nCol>65536 ){
3660 rc = SQLITE_CORRUPT_BKPT;
3661 }else{
3662 rc = sessionInputBuffer(pIn, nRead+nCol+100);
3663 nRead += nCol;
3664 }
3665 }
3666
3667 while( rc==SQLITE_OK ){
3668 while( (pIn->iNext + nRead)<pIn->nData && pIn->aData[pIn->iNext + nRead] ){
3669 nRead++;
3670 }
3671
3672 /* Break out of the loop if if the nul-terminator byte has been found.
3673 ** Otherwise, read some more input data and keep seeking. If there is
3674 ** no more input data, consider the changeset corrupt. */
3675 if( (pIn->iNext + nRead)<pIn->nData ) break;
3676 rc = sessionInputBuffer(pIn, nRead + 100);
3677 if( rc==SQLITE_OK && (pIn->iNext + nRead)>=pIn->nData ){
3678 rc = SQLITE_CORRUPT_BKPT;
3679 }
3680 }
3681 *pnByte = nRead+1;
3682 return rc;
3683 }
3684
3685 /*
3686 ** The input pointer currently points to the first byte of the first field
3687 ** of a record consisting of nCol columns. This function ensures the entire
3688 ** record is buffered. It does not move the input pointer.
3689 **
3690 ** If successful, SQLITE_OK is returned and *pnByte is set to the size of
3691 ** the record in bytes. Otherwise, an SQLite error code is returned. The
3692 ** final value of *pnByte is undefined in this case.
3693 */
3694 static int sessionChangesetBufferRecord(
3695 SessionInput *pIn, /* Input data */
3696 int nCol, /* Number of columns in record */
3697 int *pnByte /* OUT: Size of record in bytes */
3698 ){
3699 int rc = SQLITE_OK;
3700 i64 nByte = 0;
3701 int i;
3702 for(i=0; rc==SQLITE_OK && i<nCol; i++){
3703 int eType;
3704 rc = sessionInputBuffer(pIn, nByte + 10);
3705 if( rc==SQLITE_OK ){
3706 eType = pIn->aData[pIn->iNext + nByte++];
3707 if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
3708 int n;
3709 int nRem = pIn->nData - (pIn->iNext + nByte);
3710 nByte += sessionVarintGetSafe(&pIn->aData[pIn->iNext+nByte], nRem, &n);
3711 nByte += n;
3712 rc = sessionInputBuffer(pIn, nByte);
3713 }else if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
3714 nByte += 8;
3715 }else if( eType!=0 && eType!=SQLITE_NULL ){
3716 rc = SQLITE_CORRUPT_BKPT;
3717 }
3718 }
3719 if( rc==SQLITE_OK && (pIn->iNext+nByte)>pIn->nData ){
3720 rc = SQLITE_CORRUPT_BKPT;
3721 }
3722 }
3723 *pnByte = nByte;
3724 return rc;
3725 }
3726
3727 /*
3728 ** The input pointer currently points to the second byte of a table-header.
3729 ** Specifically, to the following:
3730 **
3731 ** + number of columns in table (varint)
3732 ** + array of PK flags (1 byte per column),
3733 ** + table name (nul terminated).
3734 **
3735 ** This function decodes the table-header and populates the p->nCol,
3736 ** p->zTab and p->abPK[] variables accordingly. The p->apValue[] array is
3737 ** also allocated or resized according to the new value of p->nCol. The
3738 ** input pointer is left pointing to the byte following the table header.
3739 **
3740 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code
3741 ** is returned and the final values of the various fields enumerated above
3742 ** are undefined.
3743 */
3744 static int sessionChangesetReadTblhdr(sqlite3_changeset_iter *p){
3745 int rc;
3746 int nCopy;
3747 assert( p->rc==SQLITE_OK );
3748
3749 rc = sessionChangesetBufferTblhdr(&p->in, &nCopy);
3750 if( rc==SQLITE_OK ){
3751 int nByte;
3752 int nVarint;
3753 nVarint = sessionVarintGet(&p->in.aData[p->in.iNext], &p->nCol);
3754 if( p->nCol>0 ){
3755 nCopy -= nVarint;
3756 p->in.iNext += nVarint;
3757 nByte = p->nCol * sizeof(sqlite3_value*) * 2 + nCopy;
3758 p->tblhdr.nBuf = 0;
3759 sessionBufferGrow(&p->tblhdr, nByte, &rc);
3760 }else{
3761 rc = SQLITE_CORRUPT_BKPT;
3762 }
3763 }
3764
3765 if( rc==SQLITE_OK ){
3766 size_t iPK = sizeof(sqlite3_value*)*p->nCol*2;
3767 memset(p->tblhdr.aBuf, 0, iPK);
3768 memcpy(&p->tblhdr.aBuf[iPK], &p->in.aData[p->in.iNext], nCopy);
3769 p->in.iNext += nCopy;
3770 }
3771
3772 p->apValue = (sqlite3_value**)p->tblhdr.aBuf;
3773 if( p->apValue==0 ){
3774 p->abPK = 0;
3775 p->zTab = 0;
3776 }else{
3777 p->abPK = (u8*)&p->apValue[p->nCol*2];
3778 p->zTab = p->abPK ? (char*)&p->abPK[p->nCol] : 0;
3779 }
3780 return (p->rc = rc);
3781 }
3782
3783 /*
3784 ** Advance the changeset iterator to the next change. The differences between
3785 ** this function and sessionChangesetNext() are that
3786 **
3787 ** * If pbEmpty is not NULL and the change is a no-op UPDATE (an UPDATE
3788 ** that modifies no columns), this function sets (*pbEmpty) to 1.
3789 **
3790 ** * If the iterator is configured to skip no-op UPDATEs,
3791 ** sessionChangesetNext() does that. This function does not.
3792 */
3793 static int sessionChangesetNextOne(
3794 sqlite3_changeset_iter *p, /* Changeset iterator */
3795 u8 **paRec, /* If non-NULL, store record pointer here */
3796 int *pnRec, /* If non-NULL, store size of record here */
3797 int *pbNew, /* If non-NULL, true if new table */
3798 int *pbEmpty
3799 ){
3800 int i;
3801 u8 op;
3802
3803 assert( (paRec==0 && pnRec==0) || (paRec && pnRec) );
3804 assert( pbEmpty==0 || *pbEmpty==0 );
3805
3806 /* If the iterator is in the error-state, return immediately. */
3807 if( p->rc!=SQLITE_OK ) return p->rc;
3808
3809 /* Free the current contents of p->apValue[], if any. */
3810 if( p->apValue ){
3811 for(i=0; i<p->nCol*2; i++){
3812 sqlite3ValueFree(p->apValue[i]);
3813 }
3814 memset(p->apValue, 0, sizeof(sqlite3_value*)*p->nCol*2);
3815 }
3816
3817 /* Make sure the buffer contains at least 2 bytes of input data, or all
3818 ** remaining data if there are less than 2 bytes available. This is
3819 ** sufficient either for the 'T' or 'P' byte that begins a new table,
3820 ** or for the "op" and "bIndirect" single bytes otherwise. */
3821 p->rc = sessionInputBuffer(&p->in, 2);
3822 if( p->rc!=SQLITE_OK ) return p->rc;
3823
3824 p->in.iCurrent = p->in.iNext;
3825 sessionDiscardData(&p->in);
3826
3827 /* If the iterator is already at the end of the changeset, return DONE. */
3828 if( p->in.iNext>=p->in.nData ){
3829 return SQLITE_DONE;
3830 }
3831
3832 op = p->in.aData[p->in.iNext++];
3833 while( op=='T' || op=='P' ){
3834 if( pbNew ) *pbNew = 1;
3835 p->bPatchset = (op=='P');
3836 if( sessionChangesetReadTblhdr(p) ) return p->rc;
3837 if( (p->rc = sessionInputBuffer(&p->in, 2)) ) return p->rc;
3838 p->in.iCurrent = p->in.iNext;
3839 if( p->in.iNext>=p->in.nData ) return SQLITE_DONE;
3840 op = p->in.aData[p->in.iNext++];
3841 }
3842
3843 if( p->zTab==0 || (p->bPatchset && p->bInvert) ){
3844 /* The first record in the changeset is not a table header. Must be a
3845 ** corrupt changeset. */
3846 assert( p->in.iNext==1 || p->zTab );
3847 return (p->rc = SQLITE_CORRUPT_BKPT);
3848 }
3849
3850 if( (op!=SQLITE_UPDATE && op!=SQLITE_DELETE && op!=SQLITE_INSERT)
3851 || (p->in.iNext>=p->in.nData)
3852 ){
3853 return (p->rc = SQLITE_CORRUPT_BKPT);
3854 }
3855 p->op = op;
3856 p->bIndirect = p->in.aData[p->in.iNext++];
3857
3858 if( paRec ){
3859 int nVal; /* Number of values to buffer */
3860 if( p->bPatchset==0 && op==SQLITE_UPDATE ){
3861 nVal = p->nCol * 2;
3862 }else if( p->bPatchset && op==SQLITE_DELETE ){
3863 nVal = 0;
3864 for(i=0; i<p->nCol; i++) if( p->abPK[i] ) nVal++;
3865 }else{
3866 nVal = p->nCol;
3867 }
3868 p->rc = sessionChangesetBufferRecord(&p->in, nVal, pnRec);
3869 if( p->rc!=SQLITE_OK ) return p->rc;
3870 *paRec = &p->in.aData[p->in.iNext];
3871 p->in.iNext += *pnRec;
3872 }else{
3873 sqlite3_value **apOld = (p->bInvert ? &p->apValue[p->nCol] : p->apValue);
3874 sqlite3_value **apNew = (p->bInvert ? p->apValue : &p->apValue[p->nCol]);
3875
3876 /* If this is an UPDATE or DELETE, read the old.* record. */
3877 if( p->op!=SQLITE_INSERT && (p->bPatchset==0 || p->op==SQLITE_DELETE) ){
3878 u8 *abPK = p->bPatchset ? p->abPK : 0;
3879 p->rc = sessionReadRecord(&p->in, p->nCol, abPK, apOld, 0);
3880 if( p->rc!=SQLITE_OK ) return p->rc;
3881 }
3882
3883 /* If this is an INSERT or UPDATE, read the new.* record. */
3884 if( p->op!=SQLITE_DELETE ){
3885 p->rc = sessionReadRecord(&p->in, p->nCol, 0, apNew, pbEmpty);
3886 if( p->rc!=SQLITE_OK ) return p->rc;
3887 }
3888
3889 if( (p->bPatchset || p->bInvert) && p->op==SQLITE_UPDATE ){
3890 /* If this is an UPDATE that is part of a patchset, then all PK and
3891 ** modified fields are present in the new.* record. The old.* record
3892 ** is currently completely empty. This block shifts the PK fields from
3893 ** new.* to old.*, to accommodate the code that reads these arrays. */
3894 for(i=0; i<p->nCol; i++){
3895 assert( p->bPatchset==0 || p->apValue[i]==0 );
3896 if( p->abPK[i] ){
3897 assert( p->apValue[i]==0 );
3898 p->apValue[i] = p->apValue[i+p->nCol];
3899 if( p->apValue[i]==0 ) return (p->rc = SQLITE_CORRUPT_BKPT);
3900 p->apValue[i+p->nCol] = 0;
3901 }
3902 }
3903 }else if( p->bInvert ){
3904 if( p->op==SQLITE_INSERT ) p->op = SQLITE_DELETE;
3905 else if( p->op==SQLITE_DELETE ) p->op = SQLITE_INSERT;
3906 }
3907
3908 /* If this is an UPDATE that is part of a changeset, then check that
3909 ** there are no fields in the old.* record that are not (a) PK fields,
3910 ** or (b) also present in the new.* record.
3911 **
3912 ** Such records are technically corrupt, but the rebaser was at one
3913 ** point generating them. Under most circumstances this is benign, but
3914 ** can cause spurious SQLITE_RANGE errors when applying the changeset. */
3915 if( p->bPatchset==0 && p->op==SQLITE_UPDATE){
3916 for(i=0; i<p->nCol; i++){
3917 if( p->abPK[i]==0 && p->apValue[i+p->nCol]==0 ){
3918 sqlite3ValueFree(p->apValue[i]);
3919 p->apValue[i] = 0;
3920 }
3921 }
3922 }
3923 }
3924
3925 return SQLITE_ROW;
3926 }
3927
3928 /*
3929 ** Advance the changeset iterator to the next change.
3930 **
3931 ** If both paRec and pnRec are NULL, then this function works like the public
3932 ** API sqlite3changeset_next(). If SQLITE_ROW is returned, then the
3933 ** sqlite3changeset_new() and old() APIs may be used to query for values.
3934 **
3935 ** Otherwise, if paRec and pnRec are not NULL, then a pointer to the change
3936 ** record is written to *paRec before returning and the number of bytes in
3937 ** the record to *pnRec.
3938 **
3939 ** Either way, this function returns SQLITE_ROW if the iterator is
3940 ** successfully advanced to the next change in the changeset, an SQLite
3941 ** error code if an error occurs, or SQLITE_DONE if there are no further
3942 ** changes in the changeset.
3943 */
3944 static int sessionChangesetNext(
3945 sqlite3_changeset_iter *p, /* Changeset iterator */
3946 u8 **paRec, /* If non-NULL, store record pointer here */
3947 int *pnRec, /* If non-NULL, store size of record here */
3948 int *pbNew /* If non-NULL, true if new table */
3949 ){
3950 int bEmpty;
3951 int rc;
3952 do {
3953 bEmpty = 0;
3954 rc = sessionChangesetNextOne(p, paRec, pnRec, pbNew, &bEmpty);
3955 }while( rc==SQLITE_ROW && p->bSkipEmpty && bEmpty);
3956 return rc;
3957 }
3958
3959 /*
3960 ** Advance an iterator created by sqlite3changeset_start() to the next
3961 ** change in the changeset. This function may return SQLITE_ROW, SQLITE_DONE
3962 ** or SQLITE_CORRUPT.
3963 **
3964 ** This function may not be called on iterators passed to a conflict handler
3965 ** callback by changeset_apply().
3966 */
3967 int sqlite3changeset_next(sqlite3_changeset_iter *p){
3968 return sessionChangesetNext(p, 0, 0, 0);
3969 }
3970
3971 /*
3972 ** The following function extracts information on the current change
3973 ** from a changeset iterator. It may only be called after changeset_next()
3974 ** has returned SQLITE_ROW.
3975 */
3976 int sqlite3changeset_op(
3977 sqlite3_changeset_iter *pIter, /* Iterator handle */
3978 const char **pzTab, /* OUT: Pointer to table name */
3979 int *pnCol, /* OUT: Number of columns in table */
3980 int *pOp, /* OUT: SQLITE_INSERT, DELETE or UPDATE */
3981 int *pbIndirect /* OUT: True if change is indirect */
3982 ){
3983 *pOp = pIter->op;
3984 *pnCol = pIter->nCol;
3985 *pzTab = pIter->zTab;
3986 if( pbIndirect ) *pbIndirect = pIter->bIndirect;
3987 return SQLITE_OK;
3988 }
3989
3990 /*
3991 ** Return information regarding the PRIMARY KEY and number of columns in
3992 ** the database table affected by the change that pIter currently points
3993 ** to. This function may only be called after changeset_next() returns
3994 ** SQLITE_ROW.
3995 */
3996 int sqlite3changeset_pk(
3997 sqlite3_changeset_iter *pIter, /* Iterator object */
3998 unsigned char **pabPK, /* OUT: Array of boolean - true for PK cols */
3999 int *pnCol /* OUT: Number of entries in output array */
4000 ){
4001 *pabPK = pIter->abPK;
4002 if( pnCol ) *pnCol = pIter->nCol;
4003 return SQLITE_OK;
4004 }
4005
4006 /*
4007 ** This function may only be called while the iterator is pointing to an
4008 ** SQLITE_UPDATE or SQLITE_DELETE change (see sqlite3changeset_op()).
4009 ** Otherwise, SQLITE_MISUSE is returned.
4010 **
4011 ** It sets *ppValue to point to an sqlite3_value structure containing the
4012 ** iVal'th value in the old.* record. Or, if that particular value is not
4013 ** included in the record (because the change is an UPDATE and the field
4014 ** was not modified and is not a PK column), set *ppValue to NULL.
4015 **
4016 ** If value iVal is out-of-range, SQLITE_RANGE is returned and *ppValue is
4017 ** not modified. Otherwise, SQLITE_OK.
4018 */
4019 int sqlite3changeset_old(
4020 sqlite3_changeset_iter *pIter, /* Changeset iterator */
4021 int iVal, /* Index of old.* value to retrieve */
4022 sqlite3_value **ppValue /* OUT: Old value (or NULL pointer) */
4023 ){
4024 if( pIter->op!=SQLITE_UPDATE && pIter->op!=SQLITE_DELETE ){
4025 return SQLITE_MISUSE;
4026 }
4027 if( iVal<0 || iVal>=pIter->nCol ){
4028 return SQLITE_RANGE;
4029 }
4030 *ppValue = pIter->apValue[iVal];
4031 return SQLITE_OK;
4032 }
4033
4034 /*
4035 ** This function may only be called while the iterator is pointing to an
4036 ** SQLITE_UPDATE or SQLITE_INSERT change (see sqlite3changeset_op()).
4037 ** Otherwise, SQLITE_MISUSE is returned.
4038 **
4039 ** It sets *ppValue to point to an sqlite3_value structure containing the
4040 ** iVal'th value in the new.* record. Or, if that particular value is not
4041 ** included in the record (because the change is an UPDATE and the field
4042 ** was not modified), set *ppValue to NULL.
4043 **
4044 ** If value iVal is out-of-range, SQLITE_RANGE is returned and *ppValue is
4045 ** not modified. Otherwise, SQLITE_OK.
4046 */
4047 int sqlite3changeset_new(
4048 sqlite3_changeset_iter *pIter, /* Changeset iterator */
4049 int iVal, /* Index of new.* value to retrieve */
4050 sqlite3_value **ppValue /* OUT: New value (or NULL pointer) */
4051 ){
4052 if( pIter->op!=SQLITE_UPDATE && pIter->op!=SQLITE_INSERT ){
4053 return SQLITE_MISUSE;
4054 }
4055 if( iVal<0 || iVal>=pIter->nCol ){
4056 return SQLITE_RANGE;
4057 }
4058 *ppValue = pIter->apValue[pIter->nCol+iVal];
4059 return SQLITE_OK;
4060 }
4061
4062 /*
4063 ** The following two macros are used internally. They are similar to the
4064 ** sqlite3changeset_new() and sqlite3changeset_old() functions, except that
4065 ** they omit all error checking and return a pointer to the requested value.
4066 */
4067 #define sessionChangesetNew(pIter, iVal) (pIter)->apValue[(pIter)->nCol+(iVal)]
4068 #define sessionChangesetOld(pIter, iVal) (pIter)->apValue[(iVal)]
4069
4070 /*
4071 ** This function may only be called with a changeset iterator that has been
4072 ** passed to an SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT
4073 ** conflict-handler function. Otherwise, SQLITE_MISUSE is returned.
4074 **
4075 ** If successful, *ppValue is set to point to an sqlite3_value structure
4076 ** containing the iVal'th value of the conflicting record.
4077 **
4078 ** If value iVal is out-of-range or some other error occurs, an SQLite error
4079 ** code is returned. Otherwise, SQLITE_OK.
4080 */
4081 int sqlite3changeset_conflict(
4082 sqlite3_changeset_iter *pIter, /* Changeset iterator */
4083 int iVal, /* Index of conflict record value to fetch */
4084 sqlite3_value **ppValue /* OUT: Value from conflicting row */
4085 ){
4086 if( !pIter->pConflict ){
4087 return SQLITE_MISUSE;
4088 }
4089 if( iVal<0 || iVal>=pIter->nCol ){
4090 return SQLITE_RANGE;
4091 }
4092 *ppValue = sqlite3_column_value(pIter->pConflict, iVal);
4093 return SQLITE_OK;
4094 }
4095
4096 /*
4097 ** This function may only be called with an iterator passed to an
4098 ** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case
4099 ** it sets the output variable to the total number of known foreign key
4100 ** violations in the destination database and returns SQLITE_OK.
4101 **
4102 ** In all other cases this function returns SQLITE_MISUSE.
4103 */
4104 int sqlite3changeset_fk_conflicts(
4105 sqlite3_changeset_iter *pIter, /* Changeset iterator */
4106 int *pnOut /* OUT: Number of FK violations */
4107 ){
4108 if( pIter->pConflict || pIter->apValue ){
4109 return SQLITE_MISUSE;
4110 }
4111 *pnOut = pIter->nCol;
4112 return SQLITE_OK;
4113 }
4114
4115
4116 /*
4117 ** Finalize an iterator allocated with sqlite3changeset_start().
4118 **
4119 ** This function may not be called on iterators passed to a conflict handler
4120 ** callback by changeset_apply().
4121 */
4122 int sqlite3changeset_finalize(sqlite3_changeset_iter *p){
4123 int rc = SQLITE_OK;
4124 if( p ){
4125 int i; /* Used to iterate through p->apValue[] */
4126 rc = p->rc;
4127 if( p->apValue ){
4128 for(i=0; i<p->nCol*2; i++) sqlite3ValueFree(p->apValue[i]);
4129 }
4130 sqlite3_free(p->tblhdr.aBuf);
4131 sqlite3_free(p->in.buf.aBuf);
4132 sqlite3_free(p);
4133 }
4134 return rc;
4135 }
4136
4137 static int sessionChangesetInvert(
4138 SessionInput *pInput, /* Input changeset */
4139 int (*xOutput)(void *pOut, const void *pData, int nData),
4140 void *pOut,
4141 int *pnInverted, /* OUT: Number of bytes in output changeset */
4142 void **ppInverted /* OUT: Inverse of pChangeset */
4143 ){
4144 int rc = SQLITE_OK; /* Return value */
4145 SessionBuffer sOut; /* Output buffer */
4146 int nCol = 0; /* Number of cols in current table */
4147 u8 *abPK = 0; /* PK array for current table */
4148 sqlite3_value **apVal = 0; /* Space for values for UPDATE inversion */
4149 SessionBuffer sPK = {0, 0, 0}; /* PK array for current table */
4150
4151 /* Initialize the output buffer */
4152 memset(&sOut, 0, sizeof(SessionBuffer));
4153
4154 /* Zero the output variables in case an error occurs. */
4155 if( ppInverted ){
4156 *ppInverted = 0;
4157 *pnInverted = 0;
4158 }
4159
4160 while( 1 ){
4161 u8 eType;
4162
4163 /* Test for EOF. */
4164 if( (rc = sessionInputBuffer(pInput, 2)) ) goto finished_invert;
4165 if( pInput->iNext+1>=pInput->nData ){
4166 if( pInput->iNext!=pInput->nData ){
4167 rc = SQLITE_CORRUPT_BKPT;
4168 goto finished_invert;
4169 }
4170 break;
4171 }
4172 eType = pInput->aData[pInput->iNext];
4173
4174 switch( eType ){
4175 case 'T': {
4176 /* A 'table' record consists of:
4177 **
4178 ** * A constant 'T' character,
4179 ** * Number of columns in said table (a varint),
4180 ** * An array of nCol bytes (sPK),
4181 ** * A nul-terminated table name.
4182 */
4183 int nByte;
4184 int nVar;
4185 pInput->iNext++;
4186 if( (rc = sessionChangesetBufferTblhdr(pInput, &nByte)) ){
4187 goto finished_invert;
4188 }
4189 nVar = sessionVarintGet(&pInput->aData[pInput->iNext], &nCol);
4190 sPK.nBuf = 0;
4191 sessionAppendBlob(&sPK, &pInput->aData[pInput->iNext+nVar], nCol, &rc);
4192 sessionAppendByte(&sOut, eType, &rc);
4193 sessionAppendBlob(&sOut, &pInput->aData[pInput->iNext], nByte, &rc);
4194 if( rc ) goto finished_invert;
4195
4196 pInput->iNext += nByte;
4197 sqlite3_free(apVal);
4198 apVal = 0;
4199 abPK = sPK.aBuf;
4200 break;
4201 }
4202
4203 case SQLITE_INSERT:
4204 case SQLITE_DELETE: {
4205 int nByte;
4206 int bIndirect = pInput->aData[pInput->iNext+1];
4207 int eType2 = (eType==SQLITE_DELETE ? SQLITE_INSERT : SQLITE_DELETE);
4208 pInput->iNext += 2;
4209 assert( rc==SQLITE_OK );
4210 rc = sessionChangesetBufferRecord(pInput, nCol, &nByte);
4211 sessionAppendByte(&sOut, eType2, &rc);
4212 sessionAppendByte(&sOut, bIndirect, &rc);
4213 sessionAppendBlob(&sOut, &pInput->aData[pInput->iNext], nByte, &rc);
4214 pInput->iNext += nByte;
4215 if( rc ) goto finished_invert;
4216 break;
4217 }
4218
4219 case SQLITE_UPDATE: {
4220 int iCol;
4221
4222 if( 0==apVal ){
4223 apVal = (sqlite3_value **)sqlite3_malloc64(sizeof(apVal[0])*nCol*2);
4224 if( 0==apVal ){
4225 rc = SQLITE_NOMEM;
4226 goto finished_invert;
4227 }
4228 memset(apVal, 0, sizeof(apVal[0])*nCol*2);
4229 }
4230
4231 /* Write the header for the new UPDATE change. Same as the original. */
4232 sessionAppendByte(&sOut, eType, &rc);
4233 sessionAppendByte(&sOut, pInput->aData[pInput->iNext+1], &rc);
4234
4235 /* Read the old.* and new.* records for the update change. */
4236 pInput->iNext += 2;
4237 rc = sessionReadRecord(pInput, nCol, 0, &apVal[0], 0);
4238 if( rc==SQLITE_OK ){
4239 rc = sessionReadRecord(pInput, nCol, 0, &apVal[nCol], 0);
4240 }
4241
4242 /* Write the new old.* record. Consists of the PK columns from the
4243 ** original old.* record, and the other values from the original
4244 ** new.* record. */
4245 for(iCol=0; iCol<nCol; iCol++){
4246 sqlite3_value *pVal = apVal[iCol + (abPK[iCol] ? 0 : nCol)];
4247 sessionAppendValue(&sOut, pVal, &rc);
4248 }
4249
4250 /* Write the new new.* record. Consists of a copy of all values
4251 ** from the original old.* record, except for the PK columns, which
4252 ** are set to "undefined". */
4253 for(iCol=0; iCol<nCol; iCol++){
4254 sqlite3_value *pVal = (abPK[iCol] ? 0 : apVal[iCol]);
4255 sessionAppendValue(&sOut, pVal, &rc);
4256 }
4257
4258 for(iCol=0; iCol<nCol*2; iCol++){
4259 sqlite3ValueFree(apVal[iCol]);
4260 }
4261 memset(apVal, 0, sizeof(apVal[0])*nCol*2);
4262 if( rc!=SQLITE_OK ){
4263 goto finished_invert;
4264 }
4265
4266 break;
4267 }
4268
4269 default:
4270 rc = SQLITE_CORRUPT_BKPT;
4271 goto finished_invert;
4272 }
4273
4274 assert( rc==SQLITE_OK );
4275 if( xOutput && sOut.nBuf>=sessions_strm_chunk_size ){
4276 rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
4277 sOut.nBuf = 0;
4278 if( rc!=SQLITE_OK ) goto finished_invert;
4279 }
4280 }
4281
4282 assert( rc==SQLITE_OK );
4283 if( pnInverted && ALWAYS(ppInverted) ){
4284 *pnInverted = sOut.nBuf;
4285 *ppInverted = sOut.aBuf;
4286 sOut.aBuf = 0;
4287 }else if( sOut.nBuf>0 && ALWAYS(xOutput!=0) ){
4288 rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
4289 }
4290
4291 finished_invert:
4292 sqlite3_free(sOut.aBuf);
4293 sqlite3_free(apVal);
4294 sqlite3_free(sPK.aBuf);
4295 return rc;
4296 }
4297
4298
4299 /*
4300 ** Invert a changeset object.
4301 */
4302 int sqlite3changeset_invert(
4303 int nChangeset, /* Number of bytes in input */
4304 const void *pChangeset, /* Input changeset */
4305 int *pnInverted, /* OUT: Number of bytes in output changeset */
4306 void **ppInverted /* OUT: Inverse of pChangeset */
4307 ){
4308 SessionInput sInput;
4309
4310 /* Set up the input stream */
4311 memset(&sInput, 0, sizeof(SessionInput));
4312 sInput.nData = nChangeset;
4313 sInput.aData = (u8*)pChangeset;
4314
4315 return sessionChangesetInvert(&sInput, 0, 0, pnInverted, ppInverted);
4316 }
4317
4318 /*
4319 ** Streaming version of sqlite3changeset_invert().
4320 */
4321 int sqlite3changeset_invert_strm(
4322 int (*xInput)(void *pIn, void *pData, int *pnData),
4323 void *pIn,
4324 int (*xOutput)(void *pOut, const void *pData, int nData),
4325 void *pOut
4326 ){
4327 SessionInput sInput;
4328 int rc;
4329
4330 /* Set up the input stream */
4331 memset(&sInput, 0, sizeof(SessionInput));
4332 sInput.xInput = xInput;
4333 sInput.pIn = pIn;
4334
4335 rc = sessionChangesetInvert(&sInput, xOutput, pOut, 0, 0);
4336 sqlite3_free(sInput.buf.aBuf);
4337 return rc;
4338 }
4339
4340
4341 typedef struct SessionUpdate SessionUpdate;
4342 struct SessionUpdate {
4343 sqlite3_stmt *pStmt;
4344 u32 *aMask;
4345 SessionUpdate *pNext;
4346 };
4347
4348 typedef struct SessionApplyCtx SessionApplyCtx;
4349 struct SessionApplyCtx {
4350 sqlite3 *db;
4351 sqlite3_stmt *pDelete; /* DELETE statement */
4352 sqlite3_stmt *pInsert; /* INSERT statement */
4353 sqlite3_stmt *pSelect; /* SELECT statement */
4354 int nCol; /* Size of azCol[] and abPK[] arrays */
4355 const char **azCol; /* Array of column names */
4356 u8 *abPK; /* Boolean array - true if column is in PK */
4357 u32 *aUpdateMask; /* Used by sessionUpdateFind */
4358 SessionUpdate *pUp;
4359 int bStat1; /* True if table is sqlite_stat1 */
4360 int bDeferConstraints; /* True to defer constraints */
4361 int bInvertConstraints; /* Invert when iterating constraints buffer */
4362 SessionBuffer constraints; /* Deferred constraints are stored here */
4363 SessionBuffer rebase; /* Rebase information (if any) here */
4364 u8 bRebaseStarted; /* If table header is already in rebase */
4365 u8 bRebase; /* True to collect rebase information */
4366 u8 bIgnoreNoop; /* True to ignore no-op conflicts */
4367 u8 bNoUpdateLoop; /* No update-loop processing */
4368 int bRowid;
4369 char *zErr; /* Error message, if any */
4370 };
4371
4372 /* Number of prepared UPDATE statements to cache. */
4373 #define SESSION_UPDATE_CACHE_SZ 12
4374
4375 /*
4376 ** Find a prepared UPDATE statement suitable for the UPDATE step currently
4377 ** being visited by the iterator. The UPDATE is of the form:
4378 **
4379 ** UPDATE tbl SET col = ?, col2 = ? WHERE pk1 IS ? AND pk2 IS ?
4380 */
4381 static int sessionUpdateFind(
4382 sqlite3_changeset_iter *pIter,
4383 SessionApplyCtx *p,
4384 int bPatchset,
4385 sqlite3_stmt **ppStmt
4386 ){
4387 int rc = SQLITE_OK;
4388 SessionUpdate *pUp = 0;
4389 int nCol = pIter->nCol;
4390 int nU32 = (pIter->nCol+33)/32;
4391 int ii;
4392
4393 if( p->aUpdateMask==0 ){
4394 p->aUpdateMask = sqlite3_malloc(nU32*sizeof(u32));
4395 if( p->aUpdateMask==0 ){
4396 rc = SQLITE_NOMEM;
4397 }
4398 }
4399
4400 if( rc==SQLITE_OK ){
4401 memset(p->aUpdateMask, 0, nU32*sizeof(u32));
4402 rc = SQLITE_CORRUPT;
4403 for(ii=0; ii<pIter->nCol; ii++){
4404 if( sessionChangesetNew(pIter, ii) ){
4405 p->aUpdateMask[ii/32] |= (1<<(ii%32));
4406 rc = SQLITE_OK;
4407 }
4408 }
4409 }
4410
4411 if( rc==SQLITE_OK ){
4412 if( bPatchset ) p->aUpdateMask[nCol/32] |= (1<<(nCol%32));
4413
4414 if( p->pUp ){
4415 int nUp = 0;
4416 SessionUpdate **pp = &p->pUp;
4417 while( 1 ){
4418 nUp++;
4419 if( 0==memcmp(p->aUpdateMask, (*pp)->aMask, nU32*sizeof(u32)) ){
4420 pUp = *pp;
4421 *pp = pUp->pNext;
4422 pUp->pNext = p->pUp;
4423 p->pUp = pUp;
4424 break;
4425 }
4426
4427 if( (*pp)->pNext ){
4428 pp = &(*pp)->pNext;
4429 }else{
4430 if( nUp>=SESSION_UPDATE_CACHE_SZ ){
4431 sqlite3_finalize((*pp)->pStmt);
4432 sqlite3_free(*pp);
4433 *pp = 0;
4434 }
4435 break;
4436 }
4437 }
4438 }
4439
4440 if( pUp==0 ){
4441 int nByte = sizeof(SessionUpdate) * nU32*sizeof(u32);
4442 int bStat1 = (sqlite3_stricmp(pIter->zTab, "sqlite_stat1")==0);
4443 pUp = (SessionUpdate*)sqlite3_malloc(nByte);
4444 if( pUp==0 ){
4445 rc = SQLITE_NOMEM;
4446 }else{
4447 const char *zSep = "";
4448 SessionBuffer buf;
4449
4450 memset(&buf, 0, sizeof(buf));
4451 pUp->aMask = (u32*)&pUp[1];
4452 memcpy(pUp->aMask, p->aUpdateMask, nU32*sizeof(u32));
4453
4454 sessionAppendStr(&buf, "UPDATE main.", &rc);
4455 sessionAppendIdent(&buf, pIter->zTab, &rc);
4456 sessionAppendStr(&buf, " SET ", &rc);
4457
4458 /* Create the assignments part of the UPDATE */
4459 for(ii=0; ii<pIter->nCol; ii++){
4460 if( p->abPK[ii]==0 && sessionChangesetNew(pIter, ii) ){
4461 sessionAppendStr(&buf, zSep, &rc);
4462 sessionAppendIdent(&buf, p->azCol[ii], &rc);
4463 sessionAppendStr(&buf, " = ?", &rc);
4464 sessionAppendInteger(&buf, ii*2+1, &rc);
4465 zSep = ", ";
4466 }
4467 }
4468
4469 /* Create the WHERE clause part of the UPDATE */
4470 zSep = "";
4471 sessionAppendStr(&buf, " WHERE ", &rc);
4472 for(ii=0; ii<pIter->nCol; ii++){
4473 if( p->abPK[ii] || (bPatchset==0 && sessionChangesetOld(pIter, ii)) ){
4474 sessionAppendStr(&buf, zSep, &rc);
4475 if( bStat1 && ii==1 ){
4476 assert( sqlite3_stricmp(p->azCol[ii], "idx")==0 );
4477 sessionAppendStr(&buf,
4478 "idx IS CASE "
4479 "WHEN length(?4)=0 AND typeof(?4)='blob' THEN NULL "
4480 "ELSE ?4 END ", &rc
4481 );
4482 }else{
4483 sessionAppendIdent(&buf, p->azCol[ii], &rc);
4484 sessionAppendStr(&buf, " IS ?", &rc);
4485 sessionAppendInteger(&buf, ii*2+2, &rc);
4486 }
4487 zSep = " AND ";
4488 }
4489 }
4490
4491 if( rc==SQLITE_OK ){
4492 char *zSql = (char*)buf.aBuf;
4493 rc = sqlite3_prepare_v2(p->db, zSql, buf.nBuf, &pUp->pStmt, 0);
4494 }
4495
4496 if( rc!=SQLITE_OK ){
4497 sqlite3_free(pUp);
4498 pUp = 0;
4499 }else{
4500 pUp->pNext = p->pUp;
4501 p->pUp = pUp;
4502 }
4503 sqlite3_free(buf.aBuf);
4504 }
4505 }
4506 }
4507
4508 assert( (rc==SQLITE_OK)==(pUp!=0) );
4509 if( pUp ){
4510 *ppStmt = pUp->pStmt;
4511 }else{
4512 *ppStmt = 0;
4513 }
4514 return rc;
4515 }
4516
4517 /*
4518 ** Free all cached UPDATE statements.
4519 */
4520 static void sessionUpdateFree(SessionApplyCtx *p){
4521 SessionUpdate *pUp;
4522 SessionUpdate *pNext;
4523 for(pUp=p->pUp; pUp; pUp=pNext){
4524 pNext = pUp->pNext;
4525 sqlite3_finalize(pUp->pStmt);
4526 sqlite3_free(pUp);
4527 }
4528 p->pUp = 0;
4529 sqlite3_free(p->aUpdateMask);
4530 p->aUpdateMask = 0;
4531 }
4532
4533 /*
4534 ** Formulate a statement to DELETE a row from database db. Assuming a table
4535 ** structure like this:
4536 **
4537 ** CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c));
4538 **
4539 ** The DELETE statement looks like this:
4540 **
4541 ** DELETE FROM x WHERE a = :1 AND c = :3 AND (:5 OR b IS :2 AND d IS :4)
4542 **
4543 ** Variable :5 (nCol+1) is a boolean. It should be set to 0 if we require
4544 ** matching b and d values, or 1 otherwise. The second case comes up if the
4545 ** conflict handler is invoked with NOTFOUND and returns CHANGESET_REPLACE.
4546 **
4547 ** If successful, SQLITE_OK is returned and SessionApplyCtx.pDelete is left
4548 ** pointing to the prepared version of the SQL statement.
4549 */
4550 static int sessionDeleteRow(
4551 sqlite3 *db, /* Database handle */
4552 const char *zTab, /* Table name */
4553 SessionApplyCtx *p /* Session changeset-apply context */
4554 ){
4555 int i;
4556 const char *zSep = "";
4557 int rc = SQLITE_OK;
4558 SessionBuffer buf = {0, 0, 0};
4559 int nPk = 0;
4560
4561 sessionAppendStr(&buf, "DELETE FROM main.", &rc);
4562 sessionAppendIdent(&buf, zTab, &rc);
4563 sessionAppendStr(&buf, " WHERE ", &rc);
4564
4565 for(i=0; i<p->nCol; i++){
4566 if( p->abPK[i] ){
4567 nPk++;
4568 sessionAppendStr(&buf, zSep, &rc);
4569 sessionAppendIdent(&buf, p->azCol[i], &rc);
4570 sessionAppendStr(&buf, " = ?", &rc);
4571 sessionAppendInteger(&buf, i+1, &rc);
4572 zSep = " AND ";
4573 }
4574 }
4575
4576 if( nPk<p->nCol ){
4577 sessionAppendStr(&buf, " AND (?", &rc);
4578 sessionAppendInteger(&buf, p->nCol+1, &rc);
4579 sessionAppendStr(&buf, " OR ", &rc);
4580
4581 zSep = "";
4582 for(i=0; i<p->nCol; i++){
4583 if( !p->abPK[i] ){
4584 sessionAppendStr(&buf, zSep, &rc);
4585 sessionAppendIdent(&buf, p->azCol[i], &rc);
4586 sessionAppendStr(&buf, " IS ?", &rc);
4587 sessionAppendInteger(&buf, i+1, &rc);
4588 zSep = "AND ";
4589 }
4590 }
4591 sessionAppendStr(&buf, ")", &rc);
4592 }
4593
4594 if( rc==SQLITE_OK ){
4595 rc = sessionPrepare(db, &p->pDelete, &p->zErr, (char*)buf.aBuf);
4596 }
4597 sqlite3_free(buf.aBuf);
4598
4599 return rc;
4600 }
4601
4602 /*
4603 ** Formulate and prepare an SQL statement to query table zTab by primary
4604 ** key. Assuming the following table structure:
4605 **
4606 ** CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c));
4607 **
4608 ** The SELECT statement looks like this:
4609 **
4610 ** SELECT * FROM x WHERE a = ?1 AND c = ?3
4611 **
4612 ** If successful, SQLITE_OK is returned and SessionApplyCtx.pSelect is left
4613 ** pointing to the prepared version of the SQL statement.
4614 */
4615 static int sessionSelectRow(
4616 sqlite3 *db, /* Database handle */
4617 const char *zTab, /* Table name */
4618 SessionApplyCtx *p /* Session changeset-apply context */
4619 ){
4620 /* TODO */
4621 return sessionSelectStmt(db, p->bIgnoreNoop,
4622 "main", zTab, p->bRowid, p->nCol, p->azCol, p->abPK, &p->pSelect, &p->zErr
4623 );
4624 }
4625
4626 /*
4627 ** Formulate and prepare an INSERT statement to add a record to table zTab.
4628 ** For example:
4629 **
4630 ** INSERT INTO main."zTab" VALUES(?1, ?2, ?3 ...);
4631 **
4632 ** If successful, SQLITE_OK is returned and SessionApplyCtx.pInsert is left
4633 ** pointing to the prepared version of the SQL statement.
4634 */
4635 static int sessionInsertRow(
4636 sqlite3 *db, /* Database handle */
4637 const char *zTab, /* Table name */
4638 SessionApplyCtx *p /* Session changeset-apply context */
4639 ){
4640 int rc = SQLITE_OK;
4641 int i;
4642 SessionBuffer buf = {0, 0, 0};
4643
4644 sessionAppendStr(&buf, "INSERT INTO main.", &rc);
4645 sessionAppendIdent(&buf, zTab, &rc);
4646 sessionAppendStr(&buf, "(", &rc);
4647 for(i=0; i<p->nCol; i++){
4648 if( i!=0 ) sessionAppendStr(&buf, ", ", &rc);
4649 sessionAppendIdent(&buf, p->azCol[i], &rc);
4650 }
4651
4652 sessionAppendStr(&buf, ") VALUES(?", &rc);
4653 for(i=1; i<p->nCol; i++){
4654 sessionAppendStr(&buf, ", ?", &rc);
4655 }
4656 sessionAppendStr(&buf, ")", &rc);
4657
4658 if( rc==SQLITE_OK ){
4659 rc = sessionPrepare(db, &p->pInsert, &p->zErr, (char*)buf.aBuf);
4660 }
4661 sqlite3_free(buf.aBuf);
4662 return rc;
4663 }
4664
4665 /*
4666 ** Prepare statements for applying changes to the sqlite_stat1 table.
4667 ** These are similar to those created by sessionSelectRow(),
4668 ** sessionInsertRow(), sessionUpdateRow() and sessionDeleteRow() for
4669 ** other tables.
4670 */
4671 static int sessionStat1Sql(sqlite3 *db, SessionApplyCtx *p){
4672 int rc = sessionSelectRow(db, "sqlite_stat1", p);
4673 if( rc==SQLITE_OK ){
4674 rc = sessionPrepare(db, &p->pInsert, 0,
4675 "INSERT INTO main.sqlite_stat1 VALUES(?1, "
4676 "CASE WHEN length(?2)=0 AND typeof(?2)='blob' THEN NULL ELSE ?2 END, "
4677 "?3)"
4678 );
4679 }
4680 if( rc==SQLITE_OK ){
4681 rc = sessionPrepare(db, &p->pDelete, 0,
4682 "DELETE FROM main.sqlite_stat1 WHERE tbl=?1 AND idx IS "
4683 "CASE WHEN length(?2)=0 AND typeof(?2)='blob' THEN NULL ELSE ?2 END "
4684 "AND (?4 OR stat IS ?3)"
4685 );
4686 }
4687 return rc;
4688 }
4689
4690 /*
4691 ** A wrapper around sqlite3_bind_value() that detects an extra problem.
4692 ** See comments in the body of this function for details.
4693 */
4694 static int sessionBindValue(
4695 sqlite3_stmt *pStmt, /* Statement to bind value to */
4696 int i, /* Parameter number to bind to */
4697 sqlite3_value *pVal /* Value to bind */
4698 ){
4699 int eType = sqlite3_value_type(pVal);
4700 /* COVERAGE: The (pVal->z==0) branch is never true using current versions
4701 ** of SQLite. If a malloc fails in an sqlite3_value_xxx() function, either
4702 ** the (pVal->z) variable remains as it was or the type of the value is
4703 ** set to SQLITE_NULL. */
4704 if( (eType==SQLITE_TEXT || eType==SQLITE_BLOB) && pVal->z==0 ){
4705 /* This condition occurs when an earlier OOM in a call to
4706 ** sqlite3_value_text() or sqlite3_value_blob() (perhaps from within
4707 ** a conflict-handler) has zeroed the pVal->z pointer. Return NOMEM. */
4708 return SQLITE_NOMEM;
4709 }
4710 return sqlite3_bind_value(pStmt, i, pVal);
4711 }
4712
4713 /*
4714 ** Iterator pIter must point to an SQLITE_INSERT entry. This function
4715 ** transfers new.* values from the current iterator entry to statement
4716 ** pStmt. The table being inserted into has nCol columns.
4717 **
4718 ** New.* value $i from the iterator is bound to variable ($i+1) of
4719 ** statement pStmt. If parameter abPK is NULL, all values from 0 to (nCol-1)
4720 ** are transfered to the statement. Otherwise, if abPK is not NULL, it points
4721 ** to an array nCol elements in size. In this case only those values for
4722 ** which abPK[$i] is true are read from the iterator and bound to the
4723 ** statement.
4724 **
4725 ** An SQLite error code is returned if an error occurs. Otherwise, SQLITE_OK.
4726 */
4727 static int sessionBindRow(
4728 sqlite3_changeset_iter *pIter, /* Iterator to read values from */
4729 int(*xValue)(sqlite3_changeset_iter *, int, sqlite3_value **),
4730 int nCol, /* Number of columns */
4731 u8 *abPK, /* If not NULL, bind only if true */
4732 sqlite3_stmt *pStmt /* Bind values to this statement */
4733 ){
4734 int i;
4735 int rc = SQLITE_OK;
4736
4737 /* Neither sqlite3changeset_old or sqlite3changeset_new can fail if the
4738 ** argument iterator points to a suitable entry. Make sure that xValue
4739 ** is one of these to guarantee that it is safe to ignore the return
4740 ** in the code below. */
4741 assert( xValue==sqlite3changeset_old || xValue==sqlite3changeset_new );
4742
4743 for(i=0; rc==SQLITE_OK && i<nCol; i++){
4744 if( !abPK || abPK[i] ){
4745 sqlite3_value *pVal = 0;
4746 (void)xValue(pIter, i, &pVal);
4747 if( pVal==0 ){
4748 /* The value in the changeset was "undefined". This indicates a
4749 ** corrupt changeset blob. */
4750 rc = SQLITE_CORRUPT_BKPT;
4751 }else{
4752 rc = sessionBindValue(pStmt, i+1, pVal);
4753 }
4754 }
4755 }
4756 return rc;
4757 }
4758
4759 /*
4760 ** SQL statement pSelect is as generated by the sessionSelectRow() function.
4761 ** This function binds the primary key values from the change that changeset
4762 ** iterator pIter points to to the SELECT and attempts to seek to the table
4763 ** entry. If a row is found, the SELECT statement left pointing at the row
4764 ** and SQLITE_ROW is returned. Otherwise, if no row is found and no error
4765 ** has occured, the statement is reset and SQLITE_OK is returned. If an
4766 ** error occurs, the statement is reset and an SQLite error code is returned.
4767 **
4768 ** If this function returns SQLITE_ROW, the caller must eventually reset()
4769 ** statement pSelect. If any other value is returned, the statement does
4770 ** not require a reset().
4771 **
4772 ** If the iterator currently points to an INSERT record, bind values from the
4773 ** new.* record to the SELECT statement. Or, if it points to a DELETE or
4774 ** UPDATE, bind values from the old.* record.
4775 */
4776 static int sessionSeekToRow(
4777 sqlite3_changeset_iter *pIter, /* Changeset iterator */
4778 SessionApplyCtx *p
4779 ){
4780 sqlite3_stmt *pSelect = p->pSelect;
4781 int rc; /* Return code */
4782 int nCol; /* Number of columns in table */
4783 int op; /* Changset operation (SQLITE_UPDATE etc.) */
4784 const char *zDummy; /* Unused */
4785
4786 sqlite3_clear_bindings(pSelect);
4787 sqlite3changeset_op(pIter, &zDummy, &nCol, &op, 0);
4788 rc = sessionBindRow(pIter,
4789 op==SQLITE_INSERT ? sqlite3changeset_new : sqlite3changeset_old,
4790 nCol, p->abPK, pSelect
4791 );
4792
4793 if( op!=SQLITE_DELETE && p->bIgnoreNoop ){
4794 int ii;
4795 for(ii=0; rc==SQLITE_OK && ii<nCol; ii++){
4796 if( p->abPK[ii]==0 ){
4797 sqlite3_value *pVal = 0;
4798 sqlite3changeset_new(pIter, ii, &pVal);
4799 sqlite3_bind_int(pSelect, ii+1+nCol, (pVal==0));
4800 if( pVal ) rc = sessionBindValue(pSelect, ii+1, pVal);
4801 }
4802 }
4803 }
4804
4805 if( rc==SQLITE_OK ){
4806 rc = sqlite3_step(pSelect);
4807 if( rc!=SQLITE_ROW ) rc = sqlite3_reset(pSelect);
4808 }
4809
4810 return rc;
4811 }
4812
4813 /*
4814 ** This function is called from within sqlite3changeset_apply_v2() when
4815 ** a conflict is encountered and resolved using conflict resolution
4816 ** mode eType (either SQLITE_CHANGESET_OMIT or SQLITE_CHANGESET_REPLACE)..
4817 ** It adds a conflict resolution record to the buffer in
4818 ** SessionApplyCtx.rebase, which will eventually be returned to the caller
4819 ** of apply_v2() as the "rebase" buffer.
4820 **
4821 ** Return SQLITE_OK if successful, or an SQLite error code otherwise.
4822 */
4823 static int sessionRebaseAdd(
4824 SessionApplyCtx *p, /* Apply context */
4825 int eType, /* Conflict resolution (OMIT or REPLACE) */
4826 sqlite3_changeset_iter *pIter /* Iterator pointing at current change */
4827 ){
4828 int rc = SQLITE_OK;
4829 if( p->bRebase ){
4830 int i;
4831 int eOp = pIter->op;
4832 if( p->bRebaseStarted==0 ){
4833 /* Append a table-header to the rebase buffer */
4834 const char *zTab = pIter->zTab;
4835 sessionAppendByte(&p->rebase, 'T', &rc);
4836 sessionAppendVarint(&p->rebase, p->nCol, &rc);
4837 sessionAppendBlob(&p->rebase, p->abPK, p->nCol, &rc);
4838 sessionAppendBlob(&p->rebase, (u8*)zTab, (int)strlen(zTab)+1, &rc);
4839 p->bRebaseStarted = 1;
4840 }
4841
4842 assert( eType==SQLITE_CHANGESET_REPLACE||eType==SQLITE_CHANGESET_OMIT );
4843 assert( eOp==SQLITE_DELETE || eOp==SQLITE_INSERT || eOp==SQLITE_UPDATE );
4844
4845 sessionAppendByte(&p->rebase,
4846 (eOp==SQLITE_DELETE ? SQLITE_DELETE : SQLITE_INSERT), &rc
4847 );
4848 sessionAppendByte(&p->rebase, (eType==SQLITE_CHANGESET_REPLACE), &rc);
4849 for(i=0; i<p->nCol; i++){
4850 sqlite3_value *pVal = 0;
4851 if( eOp==SQLITE_DELETE || (eOp==SQLITE_UPDATE && p->abPK[i]) ){
4852 sqlite3changeset_old(pIter, i, &pVal);
4853 }else{
4854 sqlite3changeset_new(pIter, i, &pVal);
4855 }
4856 sessionAppendValue(&p->rebase, pVal, &rc);
4857 }
4858 }
4859 return rc;
4860 }
4861
4862 /*
4863 ** Invoke the conflict handler for the change that the changeset iterator
4864 ** currently points to.
4865 **
4866 ** Argument eType must be either CHANGESET_DATA or CHANGESET_CONFLICT.
4867 ** If argument pbReplace is NULL, then the type of conflict handler invoked
4868 ** depends solely on eType, as follows:
4869 **
4870 ** eType value Value passed to xConflict
4871 ** -------------------------------------------------
4872 ** CHANGESET_DATA CHANGESET_NOTFOUND
4873 ** CHANGESET_CONFLICT CHANGESET_CONSTRAINT
4874 **
4875 ** Or, if pbReplace is not NULL, then an attempt is made to find an existing
4876 ** record with the same primary key as the record about to be deleted, updated
4877 ** or inserted. If such a record can be found, it is available to the conflict
4878 ** handler as the "conflicting" record. In this case the type of conflict
4879 ** handler invoked is as follows:
4880 **
4881 ** eType value PK Record found? Value passed to xConflict
4882 ** ----------------------------------------------------------------
4883 ** CHANGESET_DATA Yes CHANGESET_DATA
4884 ** CHANGESET_DATA No CHANGESET_NOTFOUND
4885 ** CHANGESET_CONFLICT Yes CHANGESET_CONFLICT
4886 ** CHANGESET_CONFLICT No CHANGESET_CONSTRAINT
4887 **
4888 ** If pbReplace is not NULL, and a record with a matching PK is found, and
4889 ** the conflict handler function returns SQLITE_CHANGESET_REPLACE, *pbReplace
4890 ** is set to non-zero before returning SQLITE_OK.
4891 **
4892 ** If the conflict handler returns SQLITE_CHANGESET_ABORT, SQLITE_ABORT is
4893 ** returned. Or, if the conflict handler returns an invalid value,
4894 ** SQLITE_MISUSE. If the conflict handler returns SQLITE_CHANGESET_OMIT,
4895 ** this function returns SQLITE_OK.
4896 */
4897 static int sessionConflictHandler(
4898 int eType, /* Either CHANGESET_DATA or CONFLICT */
4899 SessionApplyCtx *p, /* changeset_apply() context */
4900 sqlite3_changeset_iter *pIter, /* Changeset iterator */
4901 int(*xConflict)(void *, int, sqlite3_changeset_iter*),
4902 void *pCtx, /* First argument for conflict handler */
4903 int *pbReplace /* OUT: Set to true if PK row is found */
4904 ){
4905 int res = SQLITE_CHANGESET_OMIT;/* Value returned by conflict handler */
4906 int rc;
4907 int nCol;
4908 int op;
4909 const char *zDummy;
4910
4911 sqlite3changeset_op(pIter, &zDummy, &nCol, &op, 0);
4912
4913 assert( eType==SQLITE_CHANGESET_CONFLICT || eType==SQLITE_CHANGESET_DATA );
4914 assert( SQLITE_CHANGESET_CONFLICT+1==SQLITE_CHANGESET_CONSTRAINT );
4915 assert( SQLITE_CHANGESET_DATA+1==SQLITE_CHANGESET_NOTFOUND );
4916
4917 /* Bind the new.* PRIMARY KEY values to the SELECT statement. */
4918 if( pbReplace ){
4919 rc = sessionSeekToRow(pIter, p);
4920 }else{
4921 rc = SQLITE_OK;
4922 }
4923
4924 if( rc==SQLITE_ROW ){
4925 /* There exists another row with the new.* primary key. */
4926 if( 0==p->bIgnoreNoop
4927 || 0==sqlite3_column_int(p->pSelect, sqlite3_column_count(p->pSelect)-1)
4928 ){
4929 pIter->pConflict = p->pSelect;
4930 res = xConflict(pCtx, eType, pIter);
4931 pIter->pConflict = 0;
4932 }
4933 rc = sqlite3_reset(p->pSelect);
4934 }else if( rc==SQLITE_OK ){
4935 if( p->bDeferConstraints && eType==SQLITE_CHANGESET_CONFLICT ){
4936 /* Instead of invoking the conflict handler, append the change blob
4937 ** to the SessionApplyCtx.constraints buffer. */
4938 u8 *aBlob = &pIter->in.aData[pIter->in.iCurrent];
4939 int nBlob = pIter->in.iNext - pIter->in.iCurrent;
4940 sessionAppendBlob(&p->constraints, aBlob, nBlob, &rc);
4941 return rc;
4942 }else if( p->bIgnoreNoop==0 || op!=SQLITE_DELETE
4943 || eType==SQLITE_CHANGESET_CONFLICT
4944 ){
4945 /* No other row with the new.* primary key. */
4946 res = xConflict(pCtx, eType+1, pIter);
4947 if( res==SQLITE_CHANGESET_REPLACE ) rc = SQLITE_MISUSE;
4948 }
4949 }
4950
4951 if( rc==SQLITE_OK ){
4952 switch( res ){
4953 case SQLITE_CHANGESET_REPLACE:
4954 assert( pbReplace );
4955 *pbReplace = 1;
4956 break;
4957
4958 case SQLITE_CHANGESET_OMIT:
4959 break;
4960
4961 case SQLITE_CHANGESET_ABORT:
4962 rc = SQLITE_ABORT;
4963 break;
4964
4965 default:
4966 rc = SQLITE_MISUSE;
4967 break;
4968 }
4969 if( rc==SQLITE_OK ){
4970 rc = sessionRebaseAdd(p, res, pIter);
4971 }
4972 }
4973
4974 return rc;
4975 }
4976
4977 /*
4978 ** Attempt to apply the change that the iterator passed as the first argument
4979 ** currently points to to the database. If a conflict is encountered, invoke
4980 ** the conflict handler callback.
4981 **
4982 ** If argument pbRetry is NULL, then ignore any CHANGESET_DATA conflict. If
4983 ** one is encountered, update or delete the row with the matching primary key
4984 ** instead. Or, if pbRetry is not NULL and a CHANGESET_DATA conflict occurs,
4985 ** invoke the conflict handler. If it returns CHANGESET_REPLACE, set *pbRetry
4986 ** to true before returning. In this case the caller will invoke this function
4987 ** again, this time with pbRetry set to NULL.
4988 **
4989 ** If argument pbReplace is NULL and a CHANGESET_CONFLICT conflict is
4990 ** encountered invoke the conflict handler with CHANGESET_CONSTRAINT instead.
4991 ** Or, if pbReplace is not NULL, invoke it with CHANGESET_CONFLICT. If such
4992 ** an invocation returns SQLITE_CHANGESET_REPLACE, set *pbReplace to true
4993 ** before retrying. In this case the caller attempts to remove the conflicting
4994 ** row before invoking this function again, this time with pbReplace set
4995 ** to NULL.
4996 **
4997 ** If any conflict handler returns SQLITE_CHANGESET_ABORT, this function
4998 ** returns SQLITE_ABORT. Otherwise, if no error occurs, SQLITE_OK is
4999 ** returned.
5000 */
5001 static int sessionApplyOneOp(
5002 sqlite3_changeset_iter *pIter, /* Changeset iterator */
5003 SessionApplyCtx *p, /* changeset_apply() context */
5004 int(*xConflict)(void *, int, sqlite3_changeset_iter *),
5005 void *pCtx, /* First argument for the conflict handler */
5006 int *pbReplace, /* OUT: True to remove PK row and retry */
5007 int *pbRetry /* OUT: True to retry. */
5008 ){
5009 const char *zDummy;
5010 int op;
5011 int nCol;
5012 int rc = SQLITE_OK;
5013
5014 assert( p->pDelete && p->pInsert && p->pSelect );
5015 assert( p->azCol && p->abPK );
5016 assert( !pbReplace || *pbReplace==0 );
5017
5018 sqlite3changeset_op(pIter, &zDummy, &nCol, &op, 0);
5019
5020 if( op==SQLITE_DELETE ){
5021
5022 /* Bind values to the DELETE statement. If conflict handling is required,
5023 ** bind values for all columns and set bound variable (nCol+1) to true.
5024 ** Or, if conflict handling is not required, bind just the PK column
5025 ** values and, if it exists, set (nCol+1) to false. Conflict handling
5026 ** is not required if:
5027 **
5028 ** * this is a patchset, or
5029 ** * (pbRetry==0), or
5030 ** * all columns of the table are PK columns (in this case there is
5031 ** no (nCol+1) variable to bind to).
5032 */
5033 u8 *abPK = (pIter->bPatchset ? p->abPK : 0);
5034 rc = sessionBindRow(pIter, sqlite3changeset_old, nCol, abPK, p->pDelete);
5035 if( rc==SQLITE_OK && sqlite3_bind_parameter_count(p->pDelete)>nCol ){
5036 rc = sqlite3_bind_int(p->pDelete, nCol+1, (pbRetry==0 || abPK));
5037 }
5038 if( rc!=SQLITE_OK ) return rc;
5039
5040 sqlite3_step(p->pDelete);
5041 rc = sqlite3_reset(p->pDelete);
5042 if( rc==SQLITE_OK && sqlite3_changes(p->db)==0 ){
5043 rc = sessionConflictHandler(
5044 SQLITE_CHANGESET_DATA, p, pIter, xConflict, pCtx, pbRetry
5045 );
5046 }else if( (rc&0xff)==SQLITE_CONSTRAINT ){
5047 rc = sessionConflictHandler(
5048 SQLITE_CHANGESET_CONFLICT, p, pIter, xConflict, pCtx, 0
5049 );
5050 }
5051
5052 }else if( op==SQLITE_UPDATE ){
5053 int i;
5054 sqlite3_stmt *pUp = 0;
5055 int bPatchset = (pbRetry==0 || pIter->bPatchset);
5056
5057 rc = sessionUpdateFind(pIter, p, bPatchset, &pUp);
5058
5059 /* Bind values to the UPDATE statement. */
5060 for(i=0; rc==SQLITE_OK && i<nCol; i++){
5061 sqlite3_value *pOld = sessionChangesetOld(pIter, i);
5062 sqlite3_value *pNew = sessionChangesetNew(pIter, i);
5063 if( pOld && (p->abPK[i] || bPatchset==0) ){
5064 rc = sessionBindValue(pUp, i*2+2, pOld);
5065 }
5066 if( rc==SQLITE_OK && pNew ){
5067 rc = sessionBindValue(pUp, i*2+1, pNew);
5068 }
5069 }
5070 if( rc!=SQLITE_OK ) return rc;
5071
5072 /* Attempt the UPDATE. In the case of a NOTFOUND or DATA conflict,
5073 ** the result will be SQLITE_OK with 0 rows modified. */
5074 sqlite3_step(pUp);
5075 rc = sqlite3_reset(pUp);
5076
5077 if( rc==SQLITE_OK && sqlite3_changes(p->db)==0 ){
5078 /* A NOTFOUND or DATA error. Search the table to see if it contains
5079 ** a row with a matching primary key. If so, this is a DATA conflict.
5080 ** Otherwise, if there is no primary key match, it is a NOTFOUND. */
5081
5082 rc = sessionConflictHandler(
5083 SQLITE_CHANGESET_DATA, p, pIter, xConflict, pCtx, pbRetry
5084 );
5085
5086 }else if( (rc&0xff)==SQLITE_CONSTRAINT ){
5087 /* This is always a CONSTRAINT conflict. */
5088 rc = sessionConflictHandler(
5089 SQLITE_CHANGESET_CONFLICT, p, pIter, xConflict, pCtx, 0
5090 );
5091 }
5092
5093 }else{
5094 assert( op==SQLITE_INSERT );
5095 if( p->bStat1 ){
5096 /* Check if there is a conflicting row. For sqlite_stat1, this needs
5097 ** to be done using a SELECT, as there is no PRIMARY KEY in the
5098 ** database schema to throw an exception if a duplicate is inserted. */
5099 rc = sessionSeekToRow(pIter, p);
5100 if( rc==SQLITE_ROW ){
5101 rc = SQLITE_CONSTRAINT;
5102 sqlite3_reset(p->pSelect);
5103 }
5104 }
5105
5106 if( rc==SQLITE_OK ){
5107 rc = sessionBindRow(pIter, sqlite3changeset_new, nCol, 0, p->pInsert);
5108 if( rc!=SQLITE_OK ) return rc;
5109
5110 sqlite3_step(p->pInsert);
5111 rc = sqlite3_reset(p->pInsert);
5112 }
5113
5114 if( (rc&0xff)==SQLITE_CONSTRAINT ){
5115 rc = sessionConflictHandler(
5116 SQLITE_CHANGESET_CONFLICT, p, pIter, xConflict, pCtx, pbReplace
5117 );
5118 }
5119 }
5120
5121 return rc;
5122 }
5123
5124 /*
5125 ** Attempt to apply the change that the iterator passed as the first argument
5126 ** currently points to to the database. If a conflict is encountered, invoke
5127 ** the conflict handler callback.
5128 **
5129 ** The difference between this function and sessionApplyOne() is that this
5130 ** function handles the case where the conflict-handler is invoked and
5131 ** returns SQLITE_CHANGESET_REPLACE - indicating that the change should be
5132 ** retried in some manner.
5133 */
5134 static int sessionApplyOneWithRetry(
5135 sqlite3 *db, /* Apply change to "main" db of this handle */
5136 sqlite3_changeset_iter *pIter, /* Changeset iterator to read change from */
5137 SessionApplyCtx *pApply, /* Apply context */
5138 int(*xConflict)(void*, int, sqlite3_changeset_iter*),
5139 void *pCtx /* First argument passed to xConflict */
5140 ){
5141 int bReplace = 0;
5142 int bRetry = 0;
5143 int rc;
5144
5145 rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, &bReplace, &bRetry);
5146 if( rc==SQLITE_OK ){
5147 /* If the bRetry flag is set, the change has not been applied due to an
5148 ** SQLITE_CHANGESET_DATA problem (i.e. this is an UPDATE or DELETE and
5149 ** a row with the correct PK is present in the db, but one or more other
5150 ** fields do not contain the expected values) and the conflict handler
5151 ** returned SQLITE_CHANGESET_REPLACE. In this case retry the operation,
5152 ** but pass NULL as the final argument so that sessionApplyOneOp() ignores
5153 ** the SQLITE_CHANGESET_DATA problem. */
5154 if( bRetry ){
5155 assert( pIter->op==SQLITE_UPDATE || pIter->op==SQLITE_DELETE );
5156 rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, 0, 0);
5157 }
5158
5159 /* If the bReplace flag is set, the change is an INSERT that has not
5160 ** been performed because the database already contains a row with the
5161 ** specified primary key and the conflict handler returned
5162 ** SQLITE_CHANGESET_REPLACE. In this case remove the conflicting row
5163 ** before reattempting the INSERT. */
5164 else if( bReplace ){
5165 assert( pIter->op==SQLITE_INSERT );
5166 rc = sqlite3_exec(db, "SAVEPOINT replace_op", 0, 0, 0);
5167 if( rc==SQLITE_OK ){
5168 rc = sessionBindRow(pIter,
5169 sqlite3changeset_new, pApply->nCol, pApply->abPK, pApply->pDelete);
5170 sqlite3_bind_int(pApply->pDelete, pApply->nCol+1, 1);
5171 }
5172 if( rc==SQLITE_OK ){
5173 sqlite3_step(pApply->pDelete);
5174 rc = sqlite3_reset(pApply->pDelete);
5175 }
5176 if( rc==SQLITE_OK ){
5177 rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, 0, 0);
5178 }
5179 if( rc==SQLITE_OK ){
5180 rc = sqlite3_exec(db, "RELEASE replace_op", 0, 0, 0);
5181 }
5182 }
5183 }
5184
5185 return rc;
5186 }
5187
5188 /*
5189 ** Create an iterator to iterate through the retry buffer pRetry.
5190 */
5191 static int sessionRetryIterInit(
5192 SessionBuffer *pRetry, /* Buffer to iterate through */
5193 int bPatchset, /* True for patchset, false for changeset */
5194 const char *zTab, /* Table name */
5195 SessionApplyCtx *pApply, /* Session apply context */
5196 sqlite3_changeset_iter **ppIter /* OUT: New iterator */
5197 ){
5198 sqlite3_changeset_iter *pRet = 0;
5199 int rc = SQLITE_OK;
5200
5201 rc = sessionChangesetStart(
5202 &pRet, 0, 0, pRetry->nBuf, pRetry->aBuf, pApply->bInvertConstraints, 1
5203 );
5204 if( rc==SQLITE_OK ){
5205 size_t nByte = 2*pApply->nCol*sizeof(sqlite3_value*);
5206 pRet->bPatchset = bPatchset;
5207 pRet->zTab = (char*)zTab;
5208 pRet->nCol = pApply->nCol;
5209 pRet->abPK = pApply->abPK;
5210 sessionBufferGrow(&pRet->tblhdr, nByte, &rc);
5211 pRet->apValue = (sqlite3_value**)pRet->tblhdr.aBuf;
5212 if( rc==SQLITE_OK ){
5213 memset(pRet->apValue, 0, nByte);
5214 }else{
5215 sqlite3changeset_finalize(pRet);
5216 pRet = 0;
5217 }
5218 }
5219
5220 *ppIter = pRet;
5221 return rc;
5222 }
5223
5224 /*
5225 ** Attempt to apply all the changes in retry buffer pRetry to the database.
5226 ** Except, if parameter iSkip is greater than or equal to 0, skip change
5227 ** iSkip.
5228 */
5229 static int sessionApplyRetryBuffer(
5230 SessionBuffer *pRetry, /* Buffer to apply changes from */
5231 int iSkip, /* If >=0, index of change to omit */
5232 sqlite3 *db, /* Database handle */
5233 int bPatchset, /* True for patchset, false for changeset */
5234 const char *zTab, /* Name of table to write to */
5235 SessionApplyCtx *pApply, /* Apply context */
5236 int(*xConflict)(void*, int, sqlite3_changeset_iter*),
5237 void *pCtx /* First argument passed to xConflict */
5238 ){
5239 int rc = SQLITE_OK;
5240 int rc2 = SQLITE_OK;
5241 int ii = 0;
5242 sqlite3_changeset_iter *pIter = 0;
5243
5244 assert( pApply->constraints.nBuf==0 );
5245
5246 rc = sessionRetryIterInit(pRetry, bPatchset, zTab, pApply, &pIter);
5247
5248 for(ii=0; rc==SQLITE_OK && SQLITE_ROW==sqlite3changeset_next(pIter); ii++){
5249 if( ii!=iSkip ){
5250 rc = sessionApplyOneWithRetry(db, pIter, pApply, xConflict, pCtx);
5251 }
5252 }
5253
5254 rc2 = sqlite3changeset_finalize(pIter);
5255 if( rc==SQLITE_OK ) rc = rc2;
5256 assert( pApply->bDeferConstraints || pApply->constraints.nBuf==0 );
5257
5258 return rc;
5259 }
5260
5261 /*
5262 ** Check if table zTab in the "main" database of db is a WITHOUT ROWID
5263 ** table.
5264 **
5265 ** If no error occurs, return SQLITE_OK and set output variable (*pbWR) to
5266 ** true if zTab is a WITHOUT ROWID table, or false otherwise. Or, if an
5267 ** error does occur, return an SQLite error code. The final value of (*pbWR)
5268 ** is undefined in this case.
5269 */
5270 static int sessionTableIsWithoutRowid(sqlite3 *db, const char *zTab, int *pbWR){
5271 sqlite3_stmt *pList = 0;
5272 char *zSql = 0;
5273 int rc = SQLITE_OK;
5274
5275 zSql = sqlite3_mprintf("PRAGMA table_list = %Q", zTab);
5276 if( zSql==0 ){
5277 rc = SQLITE_NOMEM;
5278 }else{
5279 rc = sqlite3_prepare_v2(db, zSql, -1, &pList, 0);
5280 sqlite3_free(zSql);
5281 }
5282
5283 if( rc==SQLITE_OK ){
5284 sqlite3_step(pList);
5285 *pbWR = sqlite3_column_int(pList, 4);
5286 rc = sqlite3_finalize(pList);
5287 }
5288
5289 return rc;
5290 }
5291
5292 /*
5293 ** Iterator pUp points to an UPDATE change. This function deletes the
5294 ** affected row from the database and creates an INSERT statement that
5295 ** may be used to reinsert the row as it is after the UPDATE change
5296 ** has been applied.
5297 **
5298 ** If successful, SQLITE_OK is returned and output variable (*ppInsert)
5299 ** is left pointing to a prepared INSERT statement. It is the responsibility
5300 ** of the caller to eventually free this statement using sqlite3_finalize().
5301 ** Or, if an error occurs, an SQLite error code is returned and (*ppInsert)
5302 ** set to NULL. pApply->zErr may be set to an error message in this case.
5303 */
5304 static int sessionUpdateToDeleteInsert(
5305 sqlite3 *db, /* Database to write to */
5306 const char *zTab, /* Table name */
5307 SessionApplyCtx *pApply, /* Apply context */
5308 sqlite3_changeset_iter *pUp, /* Iterator pointing to UPDATE change */
5309 sqlite3_stmt **ppInsert /* OUT: INSERT statement */
5310 ){
5311 sqlite3_stmt *pRet = 0; /* The INSERT statement */
5312 sqlite3_stmt *pSelect = 0; /* SELECT to read current values of row */
5313 int rc = SQLITE_OK;
5314 int bWR = 0;
5315
5316 rc = sessionTableIsWithoutRowid(db, zTab, &bWR);
5317 if( rc==SQLITE_OK ){
5318 char *zSelect = 0;
5319 char *zInsert = 0;
5320 SessionBuffer cols = {0, 0, 0};
5321 SessionBuffer insbind = {0, 0, 0};
5322 SessionBuffer pkcols = {0, 0, 0};
5323 SessionBuffer selbind = {0, 0, 0};
5324
5325 const char *zComma = "";
5326 const char *zComma2 = "";
5327 int ii;
5328 for(ii=0; ii<pApply->nCol; ii++){
5329 sessionAppendStr(&cols, zComma, &rc);
5330 sessionAppendIdent(&cols, pApply->azCol[ii], &rc);
5331 sessionAppendStr(&insbind, zComma, &rc);
5332 sessionAppendStr(&insbind, "?", &rc);
5333 zComma = ", ";
5334
5335 if( pApply->abPK[ii] ){
5336 sessionAppendStr(&pkcols, zComma2, &rc);
5337 sessionAppendIdent(&pkcols, pApply->azCol[ii], &rc);
5338 sessionAppendStr(&selbind, zComma2, &rc);
5339 sessionAppendPrintf(&selbind, &rc, "?%d", ii+1);
5340 zComma2 = ", ";
5341 }
5342 }
5343 if( bWR==0 ){
5344 sessionAppendStr(&cols, zComma, &rc);
5345 sessionAppendStr(&cols, SESSIONS_ROWID, &rc);
5346 sessionAppendStr(&insbind, zComma, &rc);
5347 sessionAppendStr(&insbind, "?", &rc);
5348 }
5349
5350 if( rc==SQLITE_OK ){
5351 zSelect = sqlite3_mprintf("SELECT %s FROM %Q WHERE (%s) IS (%s)",
5352 cols.aBuf, zTab, pkcols.aBuf, selbind.aBuf
5353 );
5354 if( zSelect==0 ) rc = SQLITE_NOMEM;
5355 }
5356 if( rc==SQLITE_OK ){
5357 zInsert = sqlite3_mprintf("INSERT INTO %Q(%s) VALUES(%s)",
5358 zTab, cols.aBuf, insbind.aBuf
5359 );
5360 if( zInsert==0 ) rc = SQLITE_NOMEM;
5361 }
5362
5363 if( rc==SQLITE_OK ){
5364 rc = sessionPrepare(db, &pSelect, &pApply->zErr, zSelect);
5365 }
5366 if( rc==SQLITE_OK ){
5367 rc = sessionPrepare(db, &pRet, &pApply->zErr, zInsert);
5368 }
5369
5370 sqlite3_free(zSelect);
5371 sqlite3_free(zInsert);
5372 sqlite3_free(cols.aBuf);
5373 sqlite3_free(insbind.aBuf);
5374 sqlite3_free(pkcols.aBuf);
5375 sqlite3_free(selbind.aBuf);
5376 }
5377
5378 if( rc==SQLITE_OK ){
5379 rc = sessionBindRow(
5380 pUp, sqlite3changeset_old, pApply->nCol, pApply->abPK, pSelect
5381 );
5382 }
5383
5384 if( rc==SQLITE_OK && sqlite3_step(pSelect)==SQLITE_ROW ){
5385 int iCol;
5386 for(iCol=0; iCol<pApply->nCol; iCol++){
5387 sqlite3_value *pVal = pUp->apValue[iCol+pApply->nCol];
5388 if( pVal==0 ){
5389 pVal = sqlite3_column_value(pSelect, iCol);
5390 }
5391 rc = sqlite3_bind_value(pRet, iCol+1, pVal);
5392 }
5393 if( bWR==0 ){
5394 sqlite3_bind_int64(pRet, iCol+1, sqlite3_column_int64(pSelect, iCol));
5395 }
5396 }
5397 sessionFinalizeStmt(pSelect, &rc);
5398
5399 /* Delete the row from the database. */
5400 if( rc==SQLITE_OK ){
5401 rc = sessionBindRow(
5402 pUp, sqlite3changeset_old, pApply->nCol, pApply->abPK, pApply->pDelete
5403 );
5404 sqlite3_bind_int(pApply->pDelete, pApply->nCol+1, 1);
5405 }
5406 if( rc==SQLITE_OK ){
5407 sqlite3_step(pApply->pDelete);
5408 rc = sqlite3_reset(pApply->pDelete);
5409 }
5410
5411 if( rc!=SQLITE_OK ){
5412 sqlite3_finalize(pRet);
5413 pRet = 0;
5414 }
5415
5416 *ppInsert = pRet;
5417 return rc;
5418 }
5419
5420 /*
5421 ** Retry the changes accumulated in the pApply->constraints buffer. The
5422 ** pApply->constraints buffer contains all changes to table zTab that
5423 ** could not be applied due to SQLITE_CONSTRAINT errors. This function
5424 ** attempts to apply them as follows:
5425 **
5426 ** 1) It runs through the buffer and attempts to retry each change,
5427 ** removing any that are successfully applied from the buffer. This
5428 ** is repeated until no further progress can be made.
5429 **
5430 ** 2) For each UPDATE change in the buffer, try the following in a
5431 ** savepoint transaction:
5432 **
5433 ** a) DELETE the affected row,
5434 ** b) Attempt step (1) with remaining changes,
5435 ** c) Attempt to INSERT a row equivalent to the one that would be
5436 ** created by applying this UPDATE change.
5437 **
5438 ** If the INSERT in (c) succeeds, the savepoint is committed and all
5439 ** successfully applied changes are removed from the buffer. Step (2)
5440 ** is then repeated.
5441 **
5442 ** 3) Once step (2) has been attempted for each UPDATE in the change,
5443 ** a final attempt is made to apply each remaining change. This time,
5444 ** if an SQLITE_CONSTRAINT error is encountered, the conflict handler
5445 ** is invoked and the user has to decide whether to omit the change
5446 ** or rollback the entire _apply() operation.
5447 */
5448 static int sessionRetryConstraints(
5449 sqlite3 *db,
5450 int bPatchset,
5451 const char *zTab,
5452 SessionApplyCtx *pApply,
5453 int(*xConflict)(void*, int, sqlite3_changeset_iter*),
5454 void *pCtx /* First argument passed to xConflict */
5455 ){
5456 int rc = SQLITE_OK;
5457 int iUpdate = 0;
5458
5459 /* Step (1) */
5460 while( pApply->constraints.nBuf ){
5461 SessionBuffer cons = pApply->constraints;
5462 memset(&pApply->constraints, 0, sizeof(SessionBuffer));
5463
5464 rc = sessionApplyRetryBuffer(
5465 &cons, -1, db, bPatchset, zTab, pApply, xConflict, pCtx
5466 );
5467
5468 sqlite3_free(cons.aBuf);
5469 if( rc!=SQLITE_OK ) break;
5470
5471 /* If no progress has been made this round, break out of the loop. */
5472 if( pApply->constraints.nBuf>=cons.nBuf ) break;
5473 }
5474
5475 /* Step (2) */
5476 while( rc==SQLITE_OK && pApply->constraints.nBuf && !pApply->bNoUpdateLoop ){
5477 SessionBuffer cons = {0, 0, 0};
5478 sqlite3_changeset_iter *pUp = 0;
5479 sqlite3_stmt *pInsert = 0;
5480 int iSkip = 0;
5481
5482 rc = sessionRetryIterInit(
5483 &pApply->constraints, bPatchset, zTab, pApply, &pUp
5484 );
5485 if( rc==SQLITE_OK ){
5486 int iThis = -1;
5487 while( SQLITE_ROW==sqlite3changeset_next(pUp) ){
5488 if( pUp->op==SQLITE_UPDATE ) iThis++;
5489 if( iThis==iUpdate ) break;
5490 iSkip++;
5491 }
5492 if( iThis==iUpdate ){
5493 rc = sqlite3_exec(db, "SAVEPOINT update_op", 0, 0, 0);
5494 if( rc==SQLITE_OK ){
5495 rc = sessionUpdateToDeleteInsert(db, zTab, pApply, pUp, &pInsert);
5496 }
5497 }
5498 sqlite3changeset_finalize(pUp);
5499 if( iThis!=iUpdate ) break;
5500 }
5501
5502 if( rc==SQLITE_OK ){
5503 cons = pApply->constraints;
5504
5505 while( rc==SQLITE_OK && pApply->constraints.nBuf>0 ){
5506 SessionBuffer app = pApply->constraints;
5507 memset(&pApply->constraints, 0, sizeof(SessionBuffer));
5508 rc = sessionApplyRetryBuffer(
5509 &app, iSkip, db, bPatchset, zTab, pApply, xConflict, pCtx
5510 );
5511 if( app.aBuf!=cons.aBuf ){
5512 sqlite3_free(app.aBuf);
5513 }
5514 if( pApply->constraints.nBuf>=app.nBuf ){
5515 break;
5516 }
5517 iSkip = -1;
5518 }
5519 }
5520
5521 iUpdate++;
5522 if( rc==SQLITE_OK ){
5523 sqlite3_step(pInsert);
5524 rc = sqlite3_finalize(pInsert);
5525 if( rc==SQLITE_CONSTRAINT ){
5526 rc = sqlite3_exec(db, "ROLLBACK TO update_op", 0, 0, 0);
5527 sqlite3_free(pApply->constraints.aBuf);
5528 pApply->constraints = cons;
5529 memset(&cons, 0, sizeof(cons));
5530 }else if( rc==SQLITE_OK ){
5531 iUpdate = 0;
5532 }
5533 if( rc==SQLITE_OK ){
5534 rc = sqlite3_exec(db, "RELEASE update_op", 0, 0, 0);
5535 }
5536 }else{
5537 sqlite3_finalize(pInsert);
5538 }
5539
5540 sqlite3_free(cons.aBuf);
5541 }
5542
5543 /* Step (3) */
5544 if( rc==SQLITE_OK && pApply->constraints.nBuf ){
5545 SessionBuffer cons = pApply->constraints;
5546 memset(&pApply->constraints, 0, sizeof(SessionBuffer));
5547 pApply->bDeferConstraints = 0;
5548 rc = sessionApplyRetryBuffer(
5549 &cons, -1, db, bPatchset, zTab, pApply, xConflict, pCtx
5550 );
5551 sqlite3_free(cons.aBuf);
5552 }
5553
5554 return rc;
5555 }
5556
5557 /*
5558 ** Argument pIter is a changeset iterator that has been initialized, but
5559 ** not yet passed to sqlite3changeset_next(). This function applies the
5560 ** changeset to the main database attached to handle "db". The supplied
5561 ** conflict handler callback is invoked to resolve any conflicts encountered
5562 ** while applying the change.
5563 */
5564 static int sessionChangesetApply(
5565 sqlite3 *db, /* Apply change to "main" db of this handle */
5566 sqlite3_changeset_iter *pIter, /* Changeset to apply */
5567 int(*xFilter)(
5568 void *pCtx, /* Copy of sixth arg to _apply() */
5569 const char *zTab /* Table name */
5570 ),
5571 int(*xFilterIter)(
5572 void *pCtx, /* Copy of sixth arg to _apply() */
5573 sqlite3_changeset_iter *p
5574 ),
5575 int(*xConflict)(
5576 void *pCtx, /* Copy of fifth arg to _apply() */
5577 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
5578 sqlite3_changeset_iter *p /* Handle describing change and conflict */
5579 ),
5580 void *pCtx, /* First argument passed to xConflict */
5581 void **ppRebase, int *pnRebase, /* OUT: Rebase information */
5582 int flags /* SESSION_APPLY_XXX flags */
5583 ){
5584 int schemaMismatch = 0;
5585 int rc = SQLITE_OK; /* Return code */
5586 const char *zTab = 0; /* Name of current table */
5587 int nTab = 0; /* Result of sqlite3Strlen30(zTab) */
5588 SessionApplyCtx sApply; /* changeset_apply() context object */
5589 int bPatchset;
5590 u64 savedFlag = db->flags & SQLITE_FkNoAction;
5591
5592 assert( xConflict!=0 );
5593
5594 sqlite3_mutex_enter(sqlite3_db_mutex(db));
5595 if( flags & SQLITE_CHANGESETAPPLY_FKNOACTION ){
5596 db->flags |= ((u64)SQLITE_FkNoAction);
5597 db->aDb[0].pSchema->schema_cookie -= 32;
5598 }
5599
5600 pIter->in.bNoDiscard = 1;
5601 memset(&sApply, 0, sizeof(sApply));
5602 sApply.bRebase = (ppRebase && pnRebase);
5603 sApply.bInvertConstraints = !!(flags & SQLITE_CHANGESETAPPLY_INVERT);
5604 sApply.bIgnoreNoop = !!(flags & SQLITE_CHANGESETAPPLY_IGNORENOOP);
5605 sApply.bNoUpdateLoop = !!(flags & SQLITE_CHANGESETAPPLY_NOUPDATELOOP);
5606 if( (flags & SQLITE_CHANGESETAPPLY_NOSAVEPOINT)==0 ){
5607 rc = sqlite3_exec(db, "SAVEPOINT changeset_apply", 0, 0, 0);
5608 }
5609 if( rc==SQLITE_OK ){
5610 rc = sqlite3_exec(db, "PRAGMA defer_foreign_keys = 1", 0, 0, 0);
5611 }
5612 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3changeset_next(pIter) ){
5613 int nCol;
5614 int op;
5615 const char *zNew;
5616
5617 sqlite3changeset_op(pIter, &zNew, &nCol, &op, 0);
5618
5619 if( zTab==0 || sqlite3_strnicmp(zNew, zTab, nTab+1) ){
5620 u8 *abPK;
5621
5622 rc = sessionRetryConstraints(
5623 db, pIter->bPatchset, zTab, &sApply, xConflict, pCtx
5624 );
5625 if( rc!=SQLITE_OK ) break;
5626
5627 sessionUpdateFree(&sApply);
5628 sqlite3_free((char*)sApply.azCol); /* cast works around VC++ bug */
5629 sqlite3_finalize(sApply.pDelete);
5630 sqlite3_finalize(sApply.pInsert);
5631 sqlite3_finalize(sApply.pSelect);
5632 sApply.db = db;
5633 sApply.pDelete = 0;
5634 sApply.pInsert = 0;
5635 sApply.pSelect = 0;
5636 sApply.nCol = 0;
5637 sApply.azCol = 0;
5638 sApply.abPK = 0;
5639 sApply.bStat1 = 0;
5640 sApply.bDeferConstraints = 1;
5641 sApply.bRebaseStarted = 0;
5642 sApply.bRowid = 0;
5643 memset(&sApply.constraints, 0, sizeof(SessionBuffer));
5644
5645 /* If an xFilter() callback was specified, invoke it now. If the
5646 ** xFilter callback returns zero, skip this table. If it returns
5647 ** non-zero, proceed. */
5648 schemaMismatch = (xFilter && (0==xFilter(pCtx, zNew)));
5649 if( schemaMismatch ){
5650 zTab = sqlite3_mprintf("%s", zNew);
5651 if( zTab==0 ){
5652 rc = SQLITE_NOMEM;
5653 break;
5654 }
5655 nTab = (int)strlen(zTab);
5656 sApply.azCol = (const char **)zTab;
5657 }else{
5658 int nMinCol = 0;
5659 int i;
5660
5661 sqlite3changeset_pk(pIter, &abPK, 0);
5662 rc = sessionTableInfo(0, db, "main", zNew,
5663 &sApply.nCol, 0, &zTab, &sApply.azCol, 0, 0,
5664 &sApply.abPK, &sApply.bRowid
5665 );
5666 if( rc!=SQLITE_OK ) break;
5667 for(i=0; i<sApply.nCol; i++){
5668 if( sApply.abPK[i] ) nMinCol = i+1;
5669 }
5670
5671 if( sApply.nCol==0 ){
5672 schemaMismatch = 1;
5673 sqlite3_log(SQLITE_SCHEMA,
5674 "sqlite3changeset_apply(): no such table: %s", zTab
5675 );
5676 }
5677 else if( sApply.nCol<nCol ){
5678 schemaMismatch = 1;
5679 sqlite3_log(SQLITE_SCHEMA,
5680 "sqlite3changeset_apply(): table %s has %d columns, "
5681 "expected %d or more",
5682 zTab, sApply.nCol, nCol
5683 );
5684 }
5685 else if( nCol<nMinCol || memcmp(sApply.abPK, abPK, nCol)!=0 ){
5686 schemaMismatch = 1;
5687 sqlite3_log(SQLITE_SCHEMA, "sqlite3changeset_apply(): "
5688 "primary key mismatch for table %s", zTab
5689 );
5690 }
5691 else{
5692 sApply.nCol = nCol;
5693 if( 0==sqlite3_stricmp(zTab, "sqlite_stat1") ){
5694 if( (rc = sessionStat1Sql(db, &sApply) ) ){
5695 break;
5696 }
5697 sApply.bStat1 = 1;
5698 }else{
5699 if( (rc = sessionSelectRow(db, zTab, &sApply))
5700 || (rc = sessionDeleteRow(db, zTab, &sApply))
5701 || (rc = sessionInsertRow(db, zTab, &sApply))
5702 ){
5703 break;
5704 }
5705 sApply.bStat1 = 0;
5706 }
5707 }
5708 nTab = sqlite3Strlen30(zTab);
5709 }
5710 }
5711
5712 /* If there is a schema mismatch on the current table, proceed to the
5713 ** next change. A log message has already been issued. */
5714 if( schemaMismatch ) continue;
5715
5716 /* If this is a call to apply_v3(), invoke xFilterIter here. */
5717 if( xFilterIter && 0==xFilterIter(pCtx, pIter) ) continue;
5718
5719 rc = sessionApplyOneWithRetry(db, pIter, &sApply, xConflict, pCtx);
5720 }
5721
5722 bPatchset = pIter->bPatchset;
5723 if( rc==SQLITE_OK ){
5724 rc = sqlite3changeset_finalize(pIter);
5725 }else{
5726 sqlite3changeset_finalize(pIter);
5727 }
5728
5729 if( rc==SQLITE_OK ){
5730 rc = sessionRetryConstraints(db, bPatchset, zTab, &sApply, xConflict, pCtx);
5731 }
5732
5733 if( rc==SQLITE_OK ){
5734 int nFk, notUsed;
5735 sqlite3_db_status(db, SQLITE_DBSTATUS_DEFERRED_FKS, &nFk, &notUsed, 0);
5736 if( nFk!=0 ){
5737 int res = SQLITE_CHANGESET_ABORT;
5738 sqlite3_changeset_iter sIter;
5739 memset(&sIter, 0, sizeof(sIter));
5740 sIter.nCol = nFk;
5741 res = xConflict(pCtx, SQLITE_CHANGESET_FOREIGN_KEY, &sIter);
5742 if( res!=SQLITE_CHANGESET_OMIT ){
5743 rc = SQLITE_CONSTRAINT;
5744 }
5745 }
5746 }
5747
5748 {
5749 int rc2 = sqlite3_exec(db, "PRAGMA defer_foreign_keys = 0", 0, 0, 0);
5750 if( rc==SQLITE_OK ) rc = rc2;
5751 }
5752
5753 if( (flags & SQLITE_CHANGESETAPPLY_NOSAVEPOINT)==0 ){
5754 if( rc==SQLITE_OK ){
5755 rc = sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0);
5756 }
5757 if( rc!=SQLITE_OK ){
5758 sqlite3_exec(db, "ROLLBACK TO changeset_apply", 0, 0, 0);
5759 sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0);
5760 }
5761 }
5762
5763 assert( sApply.bRebase || sApply.rebase.nBuf==0 );
5764 if( rc==SQLITE_OK && bPatchset==0 && sApply.bRebase ){
5765 assert( ppRebase!=0 && pnRebase!=0 );
5766 *ppRebase = (void*)sApply.rebase.aBuf;
5767 *pnRebase = sApply.rebase.nBuf;
5768 sApply.rebase.aBuf = 0;
5769 }
5770 sessionUpdateFree(&sApply);
5771 sqlite3_finalize(sApply.pInsert);
5772 sqlite3_finalize(sApply.pDelete);
5773 sqlite3_finalize(sApply.pSelect);
5774 sqlite3_free((char*)sApply.azCol); /* cast works around VC++ bug */
5775 sqlite3_free((char*)sApply.constraints.aBuf);
5776 sqlite3_free((char*)sApply.rebase.aBuf);
5777
5778 if( (flags & SQLITE_CHANGESETAPPLY_FKNOACTION) && savedFlag==0 ){
5779 assert( db->flags & SQLITE_FkNoAction );
5780 db->flags &= ~((u64)SQLITE_FkNoAction);
5781 db->aDb[0].pSchema->schema_cookie -= 32;
5782 }
5783
5784 assert( rc!=SQLITE_OK || sApply.zErr==0 );
5785 sqlite3_set_errmsg(db, rc, sApply.zErr);
5786 sqlite3_free(sApply.zErr);
5787
5788 sqlite3_mutex_leave(sqlite3_db_mutex(db));
5789 return rc;
5790 }
5791
5792 /*
5793 ** This function is called by all six sqlite3changeset_apply() variants:
5794 **
5795 ** + sqlite3changeset_apply()
5796 ** + sqlite3changeset_apply_v2()
5797 ** + sqlite3changeset_apply_v3()
5798 ** + sqlite3changeset_apply_strm()
5799 ** + sqlite3changeset_apply_strm_v2()
5800 ** + sqlite3changeset_apply_strm_v3()
5801 **
5802 ** Arguments passed to this function are as follows:
5803 **
5804 ** db:
5805 ** Database handle to apply changeset to main database of.
5806 **
5807 ** nChangeset/pChangeset:
5808 ** These are both passed zero for the streaming variants. For the normal
5809 ** apply() functions, these are passed the size of and the buffer containing
5810 ** the changeset, respectively.
5811 **
5812 ** xInput/pIn:
5813 ** These are both passed zero for the normal variants. For the streaming
5814 ** apply() functions, these are passed the input callback and context
5815 ** pointer, respectively.
5816 **
5817 ** xFilter:
5818 ** The filter function as passed to apply() or apply_v2() (to filter by
5819 ** table name), if any. This is always NULL for apply_v3() calls.
5820 **
5821 ** xFilterIter:
5822 ** The filter function as passed to apply_v3(), if any.
5823 **
5824 ** xConflict:
5825 ** The conflict handler callback (must not be NULL).
5826 **
5827 ** pCtx:
5828 ** The context pointer passed to the xFilter and xConflict handler callbacks.
5829 **
5830 ** ppRebase, pnRebase:
5831 ** Zero for apply(). The rebase changeset output pointers, if any, for
5832 ** apply_v2() and apply_v3().
5833 **
5834 ** flags:
5835 ** Zero for apply(). The flags parameter for apply_v2() and apply_v3().
5836 */
5837 static int sessionChangesetApplyV23(
5838 sqlite3 *db, /* Apply change to "main" db of this handle */
5839 int nChangeset, /* Size of changeset in bytes */
5840 void *pChangeset, /* Changeset blob */
5841 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
5842 void *pIn, /* First arg for xInput */
5843 int(*xFilter)(
5844 void *pCtx, /* Copy of sixth arg to _apply() */
5845 const char *zTab /* Table name */
5846 ),
5847 int(*xFilterIter)(
5848 void *pCtx, /* Copy of sixth arg to _apply() */
5849 sqlite3_changeset_iter *p /* Handle describing current change */
5850 ),
5851 int(*xConflict)(
5852 void *pCtx, /* Copy of sixth arg to _apply() */
5853 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
5854 sqlite3_changeset_iter *p /* Handle describing change and conflict */
5855 ),
5856 void *pCtx, /* First argument passed to xConflict */
5857 void **ppRebase, int *pnRebase,
5858 int flags
5859 ){
5860 sqlite3_changeset_iter *pIter; /* Iterator to skip through changeset */
5861 int bInverse = !!(flags & SQLITE_CHANGESETAPPLY_INVERT);
5862 int rc = sessionChangesetStart(
5863 &pIter, xInput, pIn, nChangeset, pChangeset, bInverse, 1
5864 );
5865 if( rc==SQLITE_OK ){
5866 rc = sessionChangesetApply(db, pIter,
5867 xFilter, xFilterIter, xConflict, pCtx, ppRebase, pnRebase, flags
5868 );
5869 }
5870 return rc;
5871 }
5872
5873 /*
5874 ** Apply the changeset passed via pChangeset/nChangeset to the main
5875 ** database attached to handle "db".
5876 */
5877 int sqlite3changeset_apply_v2(
5878 sqlite3 *db, /* Apply change to "main" db of this handle */
5879 int nChangeset, /* Size of changeset in bytes */
5880 void *pChangeset, /* Changeset blob */
5881 int(*xFilter)(
5882 void *pCtx, /* Copy of sixth arg to _apply() */
5883 const char *zTab /* Table name */
5884 ),
5885 int(*xConflict)(
5886 void *pCtx, /* Copy of sixth arg to _apply() */
5887 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
5888 sqlite3_changeset_iter *p /* Handle describing change and conflict */
5889 ),
5890 void *pCtx, /* First argument passed to xConflict */
5891 void **ppRebase, int *pnRebase,
5892 int flags
5893 ){
5894 return sessionChangesetApplyV23(db,
5895 nChangeset, pChangeset, 0, 0,
5896 xFilter, 0, xConflict, pCtx,
5897 ppRebase, pnRebase, flags
5898 );
5899 }
5900
5901 /*
5902 ** Apply the changeset passed via pChangeset/nChangeset to the main
5903 ** database attached to handle "db".
5904 */
5905 int sqlite3changeset_apply_v3(
5906 sqlite3 *db, /* Apply change to "main" db of this handle */
5907 int nChangeset, /* Size of changeset in bytes */
5908 void *pChangeset, /* Changeset blob */
5909 int(*xFilter)(
5910 void *pCtx, /* Copy of sixth arg to _apply() */
5911 sqlite3_changeset_iter *p /* Handle describing current change */
5912 ),
5913 int(*xConflict)(
5914 void *pCtx, /* Copy of sixth arg to _apply() */
5915 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
5916 sqlite3_changeset_iter *p /* Handle describing change and conflict */
5917 ),
5918 void *pCtx, /* First argument passed to xConflict */
5919 void **ppRebase, int *pnRebase,
5920 int flags
5921 ){
5922 return sessionChangesetApplyV23(db,
5923 nChangeset, pChangeset, 0, 0,
5924 0, xFilter, xConflict, pCtx,
5925 ppRebase, pnRebase, flags
5926 );
5927 }
5928
5929 /*
5930 ** Apply the changeset passed via pChangeset/nChangeset to the main database
5931 ** attached to handle "db". Invoke the supplied conflict handler callback
5932 ** to resolve any conflicts encountered while applying the change.
5933 */
5934 int sqlite3changeset_apply(
5935 sqlite3 *db, /* Apply change to "main" db of this handle */
5936 int nChangeset, /* Size of changeset in bytes */
5937 void *pChangeset, /* Changeset blob */
5938 int(*xFilter)(
5939 void *pCtx, /* Copy of sixth arg to _apply() */
5940 const char *zTab /* Table name */
5941 ),
5942 int(*xConflict)(
5943 void *pCtx, /* Copy of fifth arg to _apply() */
5944 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
5945 sqlite3_changeset_iter *p /* Handle describing change and conflict */
5946 ),
5947 void *pCtx /* First argument passed to xConflict */
5948 ){
5949 return sessionChangesetApplyV23(db,
5950 nChangeset, pChangeset, 0, 0,
5951 xFilter, 0, xConflict, pCtx,
5952 0, 0, 0
5953 );
5954 }
5955
5956 /*
5957 ** Apply the changeset passed via xInput/pIn to the main database
5958 ** attached to handle "db". Invoke the supplied conflict handler callback
5959 ** to resolve any conflicts encountered while applying the change.
5960 */
5961 int sqlite3changeset_apply_v3_strm(
5962 sqlite3 *db, /* Apply change to "main" db of this handle */
5963 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
5964 void *pIn, /* First arg for xInput */
5965 int(*xFilter)(
5966 void *pCtx, /* Copy of sixth arg to _apply() */
5967 sqlite3_changeset_iter *p
5968 ),
5969 int(*xConflict)(
5970 void *pCtx, /* Copy of sixth arg to _apply() */
5971 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
5972 sqlite3_changeset_iter *p /* Handle describing change and conflict */
5973 ),
5974 void *pCtx, /* First argument passed to xConflict */
5975 void **ppRebase, int *pnRebase,
5976 int flags
5977 ){
5978 return sessionChangesetApplyV23(db,
5979 0, 0, xInput, pIn,
5980 0, xFilter, xConflict, pCtx,
5981 ppRebase, pnRebase, flags
5982 );
5983 }
5984 int sqlite3changeset_apply_v2_strm(
5985 sqlite3 *db, /* Apply change to "main" db of this handle */
5986 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
5987 void *pIn, /* First arg for xInput */
5988 int(*xFilter)(
5989 void *pCtx, /* Copy of sixth arg to _apply() */
5990 const char *zTab /* Table name */
5991 ),
5992 int(*xConflict)(
5993 void *pCtx, /* Copy of sixth arg to _apply() */
5994 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
5995 sqlite3_changeset_iter *p /* Handle describing change and conflict */
5996 ),
5997 void *pCtx, /* First argument passed to xConflict */
5998 void **ppRebase, int *pnRebase,
5999 int flags
6000 ){
6001 return sessionChangesetApplyV23(db,
6002 0, 0, xInput, pIn,
6003 xFilter, 0, xConflict, pCtx,
6004 ppRebase, pnRebase, flags
6005 );
6006 }
6007 int sqlite3changeset_apply_strm(
6008 sqlite3 *db, /* Apply change to "main" db of this handle */
6009 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
6010 void *pIn, /* First arg for xInput */
6011 int(*xFilter)(
6012 void *pCtx, /* Copy of sixth arg to _apply() */
6013 const char *zTab /* Table name */
6014 ),
6015 int(*xConflict)(
6016 void *pCtx, /* Copy of sixth arg to _apply() */
6017 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
6018 sqlite3_changeset_iter *p /* Handle describing change and conflict */
6019 ),
6020 void *pCtx /* First argument passed to xConflict */
6021 ){
6022 return sessionChangesetApplyV23(db,
6023 0, 0, xInput, pIn,
6024 xFilter, 0, xConflict, pCtx,
6025 0, 0, 0
6026 );
6027 }
6028
6029 /*
6030 ** The parts of the sqlite3_changegroup structure used by the
6031 ** sqlite3changegroup_change_xxx() APIs.
6032 */
6033 typedef struct ChangeData ChangeData;
6034 struct ChangeData {
6035 SessionTable *pTab;
6036 int bIndirect;
6037 int eOp;
6038
6039 int nBufAlloc;
6040 SessionBuffer *aBuf;
6041 SessionBuffer record;
6042 };
6043
6044 /*
6045 ** sqlite3_changegroup handle.
6046 */
6047 struct sqlite3_changegroup {
6048 int rc; /* Error code */
6049 int bPatch; /* True to accumulate patchsets */
6050 SessionTable *pList; /* List of tables in current patch */
6051 SessionBuffer rec;
6052
6053 sqlite3 *db; /* Configured by changegroup_schema() */
6054 char *zDb; /* Configured by changegroup_schema() */
6055 ChangeData cd; /* Used by changegroup_change_xxx() APIs. */
6056 };
6057
6058 /*
6059 ** This function is called to merge two changes to the same row together as
6060 ** part of an sqlite3changeset_concat() operation. A new change object is
6061 ** allocated and a pointer to it stored in *ppNew.
6062 **
6063 ** Because they have been vetted by sqlite3changegroup_add() or similar,
6064 ** both the aRec[] change and the pExist change are safe to use without
6065 ** checking for buffer overflows.
6066 */
6067 static int sessionChangeMerge(
6068 SessionTable *pTab, /* Table structure */
6069 int bRebase, /* True for a rebase hash-table */
6070 int bPatchset, /* True for patchsets */
6071 SessionChange *pExist, /* Existing change */
6072 int op2, /* Second change operation */
6073 int bIndirect, /* True if second change is indirect */
6074 u8 *aRec, /* Second change record */
6075 int nRec, /* Number of bytes in aRec */
6076 SessionChange **ppNew /* OUT: Merged change */
6077 ){
6078 SessionChange *pNew = 0;
6079 int rc = SQLITE_OK;
6080 assert( aRec!=0 );
6081
6082 if( !pExist ){
6083 pNew = (SessionChange *)sqlite3_malloc64(sizeof(SessionChange) + nRec);
6084 if( !pNew ){
6085 return SQLITE_NOMEM;
6086 }
6087 memset(pNew, 0, sizeof(SessionChange));
6088 pNew->op = op2;
6089 pNew->bIndirect = bIndirect;
6090 pNew->aRecord = (u8*)&pNew[1];
6091 if( bIndirect==0 || bRebase==0 ){
6092 pNew->nRecord = nRec;
6093 memcpy(pNew->aRecord, aRec, nRec);
6094 }else{
6095 int i;
6096 u8 *pIn = aRec;
6097 u8 *pOut = pNew->aRecord;
6098 for(i=0; i<pTab->nCol; i++){
6099 int nIn = sessionSerialLen(pIn);
6100 if( *pIn==0 ){
6101 *pOut++ = 0;
6102 }else if( pTab->abPK[i]==0 ){
6103 *pOut++ = 0xFF;
6104 }else{
6105 memcpy(pOut, pIn, nIn);
6106 pOut += nIn;
6107 }
6108 pIn += nIn;
6109 }
6110 pNew->nRecord = pOut - pNew->aRecord;
6111 }
6112 }else if( bRebase ){
6113 if( pExist->op==SQLITE_DELETE && pExist->bIndirect ){
6114 *ppNew = pExist;
6115 }else{
6116 sqlite3_int64 nByte = nRec + pExist->nRecord + sizeof(SessionChange);
6117 pNew = (SessionChange*)sqlite3_malloc64(nByte);
6118 if( pNew==0 ){
6119 rc = SQLITE_NOMEM;
6120 }else{
6121 int i;
6122 u8 *a1 = pExist->aRecord;
6123 u8 *a2 = aRec;
6124 u8 *pOut;
6125
6126 memset(pNew, 0, nByte);
6127 pNew->bIndirect = bIndirect || pExist->bIndirect;
6128 pNew->op = op2;
6129 pOut = pNew->aRecord = (u8*)&pNew[1];
6130
6131 for(i=0; i<pTab->nCol; i++){
6132 int n1 = sessionSerialLen(a1);
6133 int n2 = sessionSerialLen(a2);
6134 if( *a1==0xFF || (pTab->abPK[i]==0 && bIndirect) ){
6135 *pOut++ = 0xFF;
6136 }else if( *a2==0 ){
6137 memcpy(pOut, a1, n1);
6138 pOut += n1;
6139 }else{
6140 memcpy(pOut, a2, n2);
6141 pOut += n2;
6142 }
6143 a1 += n1;
6144 a2 += n2;
6145 }
6146 pNew->nRecord = pOut - pNew->aRecord;
6147 }
6148 sqlite3_free(pExist);
6149 }
6150 }else{
6151 int op1 = pExist->op;
6152
6153 /*
6154 ** op1=INSERT, op2=INSERT -> Unsupported. Discard op2.
6155 ** op1=INSERT, op2=UPDATE -> INSERT.
6156 ** op1=INSERT, op2=DELETE -> (none)
6157 **
6158 ** op1=UPDATE, op2=INSERT -> Unsupported. Discard op2.
6159 ** op1=UPDATE, op2=UPDATE -> UPDATE.
6160 ** op1=UPDATE, op2=DELETE -> DELETE.
6161 **
6162 ** op1=DELETE, op2=INSERT -> UPDATE.
6163 ** op1=DELETE, op2=UPDATE -> Unsupported. Discard op2.
6164 ** op1=DELETE, op2=DELETE -> Unsupported. Discard op2.
6165 */
6166 if( (op1==SQLITE_INSERT && op2==SQLITE_INSERT)
6167 || (op1==SQLITE_UPDATE && op2==SQLITE_INSERT)
6168 || (op1==SQLITE_DELETE && op2==SQLITE_UPDATE)
6169 || (op1==SQLITE_DELETE && op2==SQLITE_DELETE)
6170 ){
6171 pNew = pExist;
6172 }else if( op1==SQLITE_INSERT && op2==SQLITE_DELETE ){
6173 sqlite3_free(pExist);
6174 assert( pNew==0 );
6175 }else{
6176 u8 *aExist = pExist->aRecord;
6177 sqlite3_int64 nByte;
6178 u8 *aCsr;
6179
6180 /* Allocate a new SessionChange object. Ensure that the aRecord[]
6181 ** buffer of the new object is large enough to hold any record that
6182 ** may be generated by combining the input records. */
6183 nByte = sizeof(SessionChange) + pExist->nRecord + nRec;
6184 pNew = (SessionChange *)sqlite3_malloc64(nByte);
6185 if( !pNew ){
6186 sqlite3_free(pExist);
6187 return SQLITE_NOMEM;
6188 }
6189 memset(pNew, 0, sizeof(SessionChange));
6190 pNew->bIndirect = (bIndirect && pExist->bIndirect);
6191 aCsr = pNew->aRecord = (u8 *)&pNew[1];
6192
6193 if( op1==SQLITE_INSERT ){ /* INSERT + UPDATE */
6194 u8 *a1 = aRec;
6195 assert( op2==SQLITE_UPDATE );
6196 pNew->op = SQLITE_INSERT;
6197 if( bPatchset==0 ) sessionSkipRecord(&a1, pTab->nCol);
6198 sessionMergeRecord(&aCsr, pTab->nCol, aExist, a1);
6199 }else if( op1==SQLITE_DELETE ){ /* DELETE + INSERT */
6200 assert( op2==SQLITE_INSERT );
6201 pNew->op = SQLITE_UPDATE;
6202 if( bPatchset ){
6203 memcpy(aCsr, aRec, nRec);
6204 aCsr += nRec;
6205 }else{
6206 if( 0==sessionMergeUpdate(&aCsr, pTab, bPatchset, aExist,0,aRec,0) ){
6207 sqlite3_free(pNew);
6208 pNew = 0;
6209 }
6210 }
6211 }else if( op2==SQLITE_UPDATE ){ /* UPDATE + UPDATE */
6212 u8 *a1 = aExist;
6213 u8 *a2 = aRec;
6214 assert( op1==SQLITE_UPDATE );
6215 if( bPatchset==0 ){
6216 sessionSkipRecord(&a1, pTab->nCol);
6217 sessionSkipRecord(&a2, pTab->nCol);
6218 }
6219 pNew->op = SQLITE_UPDATE;
6220 if( 0==sessionMergeUpdate(&aCsr, pTab, bPatchset, aRec, aExist,a1,a2) ){
6221 sqlite3_free(pNew);
6222 pNew = 0;
6223 }
6224 }else{ /* UPDATE + DELETE */
6225 assert( op1==SQLITE_UPDATE && op2==SQLITE_DELETE );
6226 pNew->op = SQLITE_DELETE;
6227 if( bPatchset ){
6228 memcpy(aCsr, aRec, nRec);
6229 aCsr += nRec;
6230 }else{
6231 sessionMergeRecord(&aCsr, pTab->nCol, aRec, aExist);
6232 }
6233 }
6234
6235 if( pNew ){
6236 pNew->nRecord = (int)(aCsr - pNew->aRecord);
6237 }
6238 sqlite3_free(pExist);
6239 }
6240 }
6241
6242 *ppNew = pNew;
6243 return rc;
6244 }
6245
6246 /*
6247 ** Check if a changeset entry with nCol columns and the PK array passed
6248 ** as the final argument to this function is compatible with SessionTable
6249 ** pTab. If so, return 1. Otherwise, if they are incompatible in some way,
6250 ** return 0.
6251 */
6252 static int sessionChangesetCheckCompat(
6253 SessionTable *pTab,
6254 int nCol,
6255 u8 *abPK
6256 ){
6257 if( pTab->azCol && nCol<pTab->nCol ){
6258 int ii;
6259 for(ii=0; ii<pTab->nCol; ii++){
6260 u8 bPK = (ii < nCol) ? abPK[ii] : 0;
6261 if( pTab->abPK[ii]!=bPK ) return 0;
6262 }
6263 return 1;
6264 }
6265 return (pTab->nCol==nCol && 0==memcmp(abPK, pTab->abPK, nCol));
6266 }
6267
6268 static int sessionChangesetExtendRecord(
6269 sqlite3_changegroup *pGrp,
6270 SessionTable *pTab,
6271 int nCol,
6272 int op,
6273 const u8 *aRec,
6274 int nRec,
6275 SessionBuffer *pOut
6276 ){
6277 int rc = SQLITE_OK;
6278 int ii = 0;
6279
6280 assert( pTab->azCol );
6281 assert( nCol<pTab->nCol );
6282
6283 pOut->nBuf = 0;
6284 if( op==SQLITE_INSERT || (op==SQLITE_DELETE && pGrp->bPatch==0) ){
6285 /* Append the missing default column values to the record. */
6286 sessionAppendBlob(pOut, aRec, nRec, &rc);
6287 if( rc==SQLITE_OK && pTab->pDfltStmt==0 ){
6288 rc = sessionPrepareDfltStmt(pGrp->db, pTab, &pTab->pDfltStmt);
6289 if( rc==SQLITE_OK && SQLITE_ROW!=sqlite3_step(pTab->pDfltStmt) ){
6290 rc = sqlite3_errcode(pGrp->db);
6291 }
6292 }
6293 for(ii=nCol; rc==SQLITE_OK && ii<pTab->nCol; ii++){
6294 int eType = sqlite3_column_type(pTab->pDfltStmt, ii);
6295 sessionAppendByte(pOut, eType, &rc);
6296 switch( eType ){
6297 case SQLITE_FLOAT:
6298 case SQLITE_INTEGER: {
6299 if( SQLITE_OK==sessionBufferGrow(pOut, 8, &rc) ){
6300 if( eType==SQLITE_INTEGER ){
6301 sqlite3_int64 iVal = sqlite3_column_int64(pTab->pDfltStmt, ii);
6302 sessionPutI64(&pOut->aBuf[pOut->nBuf], iVal);
6303 }else{
6304 double rVal = sqlite3_column_double(pTab->pDfltStmt, ii);
6305 sessionPutDouble(&pOut->aBuf[pOut->nBuf], rVal);
6306 }
6307 pOut->nBuf += 8;
6308 }
6309 break;
6310 }
6311
6312 case SQLITE_BLOB:
6313 case SQLITE_TEXT: {
6314 int n = sqlite3_column_bytes(pTab->pDfltStmt, ii);
6315 sessionAppendVarint(pOut, n, &rc);
6316 if( eType==SQLITE_TEXT ){
6317 const u8 *z = (const u8*)sqlite3_column_text(pTab->pDfltStmt, ii);
6318 sessionAppendBlob(pOut, z, n, &rc);
6319 }else{
6320 const u8 *z = (const u8*)sqlite3_column_blob(pTab->pDfltStmt, ii);
6321 sessionAppendBlob(pOut, z, n, &rc);
6322 }
6323 break;
6324 }
6325
6326 default:
6327 assert( eType==SQLITE_NULL );
6328 break;
6329 }
6330 }
6331 }else if( op==SQLITE_UPDATE ){
6332 /* Append missing "undefined" entries to the old.* record. And, if this
6333 ** is an UPDATE, to the new.* record as well. */
6334 int iOff = 0;
6335 if( pGrp->bPatch==0 ){
6336 for(ii=0; ii<nCol; ii++){
6337 iOff += sessionSerialLen(&aRec[iOff]);
6338 }
6339 sessionAppendBlob(pOut, aRec, iOff, &rc);
6340 for(ii=0; ii<(pTab->nCol-nCol); ii++){
6341 sessionAppendByte(pOut, 0x00, &rc);
6342 }
6343 }
6344
6345 sessionAppendBlob(pOut, &aRec[iOff], nRec-iOff, &rc);
6346 for(ii=0; ii<(pTab->nCol-nCol); ii++){
6347 sessionAppendByte(pOut, 0x00, &rc);
6348 }
6349 }else{
6350 assert( op==SQLITE_DELETE && pGrp->bPatch );
6351 sessionAppendBlob(pOut, aRec, nRec, &rc);
6352 }
6353
6354 return rc;
6355 }
6356
6357 /*
6358 ** Locate or create a SessionTable object that may be used to add the
6359 ** change currently pointed to by iterator pIter to changegroup pGrp.
6360 ** If successful, set output variable (*ppTab) to point to the table
6361 ** object and return SQLITE_OK. Otherwise, if some error occurs, return
6362 ** an SQLite error code and leave (*ppTab) set to NULL.
6363 */
6364 static int sessionChangesetFindTable(
6365 sqlite3_changegroup *pGrp,
6366 const char *zTab,
6367 sqlite3_changeset_iter *pIter,
6368 SessionTable **ppTab
6369 ){
6370 int rc = SQLITE_OK;
6371 SessionTable *pTab = 0;
6372 int nTab = (int)strlen(zTab);
6373 u8 *abPK = 0;
6374 int nCol = 0;
6375
6376 *ppTab = 0;
6377
6378 /* Search the list for an existing table */
6379 for(pTab = pGrp->pList; pTab; pTab=pTab->pNext){
6380 if( 0==sqlite3_strnicmp(pTab->zName, zTab, nTab+1) ) break;
6381 }
6382
6383
6384 if( pIter ){
6385 sqlite3changeset_pk(pIter, &abPK, &nCol);
6386 }else if( !pTab && !pGrp->db ){
6387 return SQLITE_OK;
6388 }
6389
6390 /* If one was not found above, create a new table now */
6391 if( !pTab ){
6392 SessionTable **ppNew;
6393
6394 pTab = sqlite3_malloc64(sizeof(SessionTable) + nCol + nTab+1);
6395 if( !pTab ){
6396 return SQLITE_NOMEM;
6397 }
6398 memset(pTab, 0, sizeof(SessionTable));
6399 pTab->nCol = nCol;
6400 pTab->abPK = (u8*)&pTab[1];
6401 if( nCol>0 ){
6402 memcpy(pTab->abPK, abPK, nCol);
6403 }
6404 pTab->zName = (char*)&pTab->abPK[nCol];
6405 memcpy(pTab->zName, zTab, nTab+1);
6406
6407 if( pGrp->db ){
6408 pTab->nCol = 0;
6409 rc = sessionInitTable(0, pTab, pGrp->db, pGrp->zDb);
6410 if( rc || pTab->nCol==0 ){
6411 sqlite3_free(pTab->azCol);
6412 sqlite3_free(pTab);
6413 return rc;
6414 }
6415 }
6416
6417 /* The new object must be linked on to the end of the list, not
6418 ** simply added to the start of it. This is to ensure that the
6419 ** tables within the output of sqlite3changegroup_output() are in
6420 ** the right order. */
6421 for(ppNew=&pGrp->pList; *ppNew; ppNew=&(*ppNew)->pNext);
6422 *ppNew = pTab;
6423 }
6424
6425 /* Check that the table is compatible. */
6426 if( pIter && !sessionChangesetCheckCompat(pTab, nCol, abPK) ){
6427 rc = SQLITE_SCHEMA;
6428 }
6429
6430 *ppTab = pTab;
6431 return rc;
6432 }
6433
6434 /*
6435 ** Add a single change to the changegroup pGrp.
6436 */
6437 static int sessionOneChangeToHash(
6438 sqlite3_changegroup *pGrp, /* Changegroup to update */
6439 SessionTable *pTab, /* Table change pertains to */
6440 int op, /* One of SQLITE_INSERT, UPDATE, DELETE */
6441 int bIndirect, /* True to flag change as "indirect" */
6442 int nCol, /* Number of columns in record(s) */
6443 u8 *aRec, /* Serialized change record(s) */
6444 int nRec, /* Size of aRec[] in bytes */
6445 int bRebase /* True if this is a rebase blob */
6446 ){
6447 int rc = SQLITE_OK;
6448 int iHash = 0;
6449 SessionChange *pChange = 0;
6450 SessionChange *pExist = 0;
6451 SessionChange **pp = 0;
6452
6453 assert( nRec>0 );
6454
6455 if( nCol<pTab->nCol ){
6456 SessionBuffer *pBuf = &pGrp->rec;
6457 rc = sessionChangesetExtendRecord(pGrp, pTab, nCol, op, aRec, nRec, pBuf);
6458 aRec = pBuf->aBuf;
6459 nRec = pBuf->nBuf;
6460 assert( pGrp->db );
6461 }
6462
6463 if( rc==SQLITE_OK && sessionGrowHash(0, pGrp->bPatch, pTab) ){
6464 rc = SQLITE_NOMEM;
6465 }
6466
6467 if( rc==SQLITE_OK ){
6468 /* Search for existing entry. If found, remove it from the hash table.
6469 ** Code below may link it back in. */
6470 iHash = sessionChangeHash(
6471 pTab, (pGrp->bPatch && op==SQLITE_DELETE), aRec, pTab->nChange
6472 );
6473 for(pp=&pTab->apChange[iHash]; *pp; pp=&(*pp)->pNext){
6474 int bPkOnly1 = 0;
6475 int bPkOnly2 = 0;
6476 if( pGrp->bPatch ){
6477 bPkOnly1 = (*pp)->op==SQLITE_DELETE;
6478 bPkOnly2 = op==SQLITE_DELETE;
6479 }
6480 if( sessionChangeEqual(pTab, bPkOnly1, (*pp)->aRecord, bPkOnly2, aRec) ){
6481 pExist = *pp;
6482 *pp = (*pp)->pNext;
6483 pTab->nEntry--;
6484 break;
6485 }
6486 }
6487 }
6488
6489 if( rc==SQLITE_OK ){
6490 rc = sessionChangeMerge(pTab, bRebase,
6491 pGrp->bPatch, pExist, op, bIndirect, aRec, nRec, &pChange
6492 );
6493 }
6494 if( rc==SQLITE_OK && pChange ){
6495 pChange->pNext = pTab->apChange[iHash];
6496 pTab->apChange[iHash] = pChange;
6497 pTab->nEntry++;
6498 }
6499
6500 return rc;
6501 }
6502
6503 /*
6504 ** Add the change currently indicated by iterator pIter to the hash table
6505 ** belonging to changegroup pGrp.
6506 */
6507 static int sessionOneChangeIterToHash(
6508 sqlite3_changegroup *pGrp,
6509 sqlite3_changeset_iter *pIter,
6510 int bRebase
6511 ){
6512 u8 *aRec = &pIter->in.aData[pIter->in.iCurrent + 2];
6513 int nRec = (pIter->in.iNext - pIter->in.iCurrent) - 2;
6514 const char *zTab = 0;
6515 int nCol = 0;
6516 int op = 0;
6517 int bIndirect = 0;
6518 int rc = SQLITE_OK;
6519 SessionTable *pTab = 0;
6520
6521 /* Ensure that only changesets, or only patchsets, but not a mixture
6522 ** of both, are being combined. It is an error to try to combine a
6523 ** changeset and a patchset. */
6524 if( pGrp->pList==0 ){
6525 pGrp->bPatch = pIter->bPatchset;
6526 }else if( pIter->bPatchset!=pGrp->bPatch ){
6527 rc = SQLITE_ERROR;
6528 }
6529
6530 if( rc==SQLITE_OK ){
6531 sqlite3changeset_op(pIter, &zTab, &nCol, &op, &bIndirect);
6532 rc = sessionChangesetFindTable(pGrp, zTab, pIter, &pTab);
6533 }
6534
6535 if( rc==SQLITE_OK ){
6536 rc = sessionOneChangeToHash(
6537 pGrp, pTab, op, bIndirect, nCol, aRec, nRec, bRebase
6538 );
6539 }
6540
6541 if( rc==SQLITE_OK ) rc = pIter->rc;
6542 return rc;
6543 }
6544
6545 /*
6546 ** Add all changes in the changeset traversed by the iterator passed as
6547 ** the first argument to the changegroup hash tables.
6548 */
6549 static int sessionChangesetToHash(
6550 sqlite3_changeset_iter *pIter, /* Iterator to read from */
6551 sqlite3_changegroup *pGrp, /* Changegroup object to add changeset to */
6552 int bRebase /* True if hash table is for rebasing */
6553 ){
6554 u8 *aRec;
6555 int nRec;
6556 int rc = SQLITE_OK;
6557
6558 pIter->in.bNoDiscard = 1;
6559 while( SQLITE_ROW==(sessionChangesetNext(pIter, &aRec, &nRec, 0)) ){
6560 rc = sessionOneChangeIterToHash(pGrp, pIter, bRebase);
6561 if( rc!=SQLITE_OK ) break;
6562 }
6563
6564 if( rc==SQLITE_OK ) rc = pIter->rc;
6565 return rc;
6566 }
6567
6568 /*
6569 ** Serialize a changeset (or patchset) based on all changesets (or patchsets)
6570 ** added to the changegroup object passed as the first argument.
6571 **
6572 ** If xOutput is not NULL, then the changeset/patchset is returned to the
6573 ** user via one or more calls to xOutput, as with the other streaming
6574 ** interfaces.
6575 **
6576 ** Or, if xOutput is NULL, then (*ppOut) is populated with a pointer to a
6577 ** buffer containing the output changeset before this function returns. In
6578 ** this case (*pnOut) is set to the size of the output buffer in bytes. It
6579 ** is the responsibility of the caller to free the output buffer using
6580 ** sqlite3_free() when it is no longer required.
6581 **
6582 ** If successful, SQLITE_OK is returned. Or, if an error occurs, an SQLite
6583 ** error code. If an error occurs and xOutput is NULL, (*ppOut) and (*pnOut)
6584 ** are both set to 0 before returning.
6585 */
6586 static int sessionChangegroupOutput(
6587 sqlite3_changegroup *pGrp,
6588 int (*xOutput)(void *pOut, const void *pData, int nData),
6589 void *pOut,
6590 int *pnOut,
6591 void **ppOut
6592 ){
6593 int rc = SQLITE_OK;
6594 SessionBuffer buf = {0, 0, 0};
6595 SessionTable *pTab;
6596 assert( xOutput==0 || (ppOut==0 && pnOut==0) );
6597
6598 /* Create the serialized output changeset based on the contents of the
6599 ** hash tables attached to the SessionTable objects in list p->pList.
6600 */
6601 for(pTab=pGrp->pList; rc==SQLITE_OK && pTab; pTab=pTab->pNext){
6602 int i;
6603 if( pTab->nEntry==0 ) continue;
6604
6605 sessionAppendTableHdr(&buf, pGrp->bPatch, pTab, &rc);
6606 for(i=0; i<pTab->nChange; i++){
6607 SessionChange *p;
6608 for(p=pTab->apChange[i]; p; p=p->pNext){
6609 sessionAppendByte(&buf, p->op, &rc);
6610 sessionAppendByte(&buf, p->bIndirect, &rc);
6611 sessionAppendBlob(&buf, p->aRecord, p->nRecord, &rc);
6612 if( rc==SQLITE_OK && xOutput && buf.nBuf>=sessions_strm_chunk_size ){
6613 rc = xOutput(pOut, buf.aBuf, buf.nBuf);
6614 buf.nBuf = 0;
6615 }
6616 }
6617 }
6618 }
6619
6620 if( rc==SQLITE_OK ){
6621 if( xOutput ){
6622 if( buf.nBuf>0 ) rc = xOutput(pOut, buf.aBuf, buf.nBuf);
6623 }else if( ppOut ){
6624 *ppOut = buf.aBuf;
6625 if( pnOut ) *pnOut = buf.nBuf;
6626 buf.aBuf = 0;
6627 }
6628 }
6629 sqlite3_free(buf.aBuf);
6630
6631 return rc;
6632 }
6633
6634 /*
6635 ** Allocate a new, empty, sqlite3_changegroup.
6636 */
6637 int sqlite3changegroup_new(sqlite3_changegroup **pp){
6638 int rc = SQLITE_OK; /* Return code */
6639 sqlite3_changegroup *p; /* New object */
6640 p = (sqlite3_changegroup*)sqlite3_malloc(sizeof(sqlite3_changegroup));
6641 if( p==0 ){
6642 rc = SQLITE_NOMEM;
6643 }else{
6644 memset(p, 0, sizeof(sqlite3_changegroup));
6645 }
6646 *pp = p;
6647 return rc;
6648 }
6649
6650 /*
6651 ** Configure a changegroup object.
6652 */
6653 int sqlite3changegroup_config(
6654 sqlite3_changegroup *pGrp,
6655 int op,
6656 void *pArg
6657 ){
6658 int rc = SQLITE_OK;
6659
6660 switch( op ){
6661 case SQLITE_CHANGEGROUP_CONFIG_PATCHSET: {
6662 int arg = *(int*)pArg;
6663 if( pGrp->pList==0 && arg>=0 ){
6664 pGrp->bPatch = (arg>0);
6665 }
6666 *(int*)pArg = pGrp->bPatch;
6667 break;
6668 }
6669 default:
6670 rc = SQLITE_MISUSE;
6671 break;
6672 }
6673
6674 return rc;
6675 }
6676
6677 /*
6678 ** Provide a database schema to the changegroup object.
6679 */
6680 int sqlite3changegroup_schema(
6681 sqlite3_changegroup *pGrp,
6682 sqlite3 *db,
6683 const char *zDb
6684 ){
6685 int rc = SQLITE_OK;
6686
6687 if( pGrp->pList || pGrp->db ){
6688 /* Cannot add a schema after one or more calls to sqlite3changegroup_add(),
6689 ** or after sqlite3changegroup_schema() has already been called. */
6690 rc = SQLITE_MISUSE;
6691 }else{
6692 pGrp->zDb = sqlite3_mprintf("%s", zDb);
6693 if( pGrp->zDb==0 ){
6694 rc = SQLITE_NOMEM;
6695 }else{
6696 pGrp->db = db;
6697 }
6698 }
6699 return rc;
6700 }
6701
6702 /*
6703 ** Add the changeset currently stored in buffer pData, size nData bytes,
6704 ** to changeset-group p.
6705 */
6706 int sqlite3changegroup_add(sqlite3_changegroup *pGrp, int nData, void *pData){
6707 sqlite3_changeset_iter *pIter; /* Iterator opened on pData/nData */
6708 int rc; /* Return code */
6709
6710 rc = sqlite3changeset_start(&pIter, nData, pData);
6711 if( rc==SQLITE_OK ){
6712 rc = sessionChangesetToHash(pIter, pGrp, 0);
6713 }
6714 sqlite3changeset_finalize(pIter);
6715 return rc;
6716 }
6717
6718 /*
6719 ** Add a single change to a changeset-group.
6720 */
6721 int sqlite3changegroup_add_change(
6722 sqlite3_changegroup *pGrp,
6723 sqlite3_changeset_iter *pIter
6724 ){
6725 int rc = SQLITE_OK;
6726
6727 if( pIter->in.iCurrent==pIter->in.iNext
6728 || pIter->rc!=SQLITE_OK
6729 || pIter->bInvert
6730 ){
6731 /* Iterator does not point to any valid entry or is an INVERT iterator. */
6732 rc = SQLITE_ERROR;
6733 }else{
6734 pIter->in.bNoDiscard = 1;
6735 rc = sessionOneChangeIterToHash(pGrp, pIter, 0);
6736 }
6737 return rc;
6738 }
6739
6740 /*
6741 ** Obtain a buffer containing a changeset representing the concatenation
6742 ** of all changesets added to the group so far.
6743 */
6744 int sqlite3changegroup_output(
6745 sqlite3_changegroup *pGrp,
6746 int *pnData,
6747 void **ppData
6748 ){
6749 return sessionChangegroupOutput(pGrp, 0, 0, pnData, ppData);
6750 }
6751
6752 /*
6753 ** Streaming versions of changegroup_add().
6754 */
6755 int sqlite3changegroup_add_strm(
6756 sqlite3_changegroup *pGrp,
6757 int (*xInput)(void *pIn, void *pData, int *pnData),
6758 void *pIn
6759 ){
6760 sqlite3_changeset_iter *pIter; /* Iterator opened on pData/nData */
6761 int rc; /* Return code */
6762
6763 rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
6764 if( rc==SQLITE_OK ){
6765 rc = sessionChangesetToHash(pIter, pGrp, 0);
6766 }
6767 sqlite3changeset_finalize(pIter);
6768 return rc;
6769 }
6770
6771 /*
6772 ** Streaming versions of changegroup_output().
6773 */
6774 int sqlite3changegroup_output_strm(
6775 sqlite3_changegroup *pGrp,
6776 int (*xOutput)(void *pOut, const void *pData, int nData),
6777 void *pOut
6778 ){
6779 return sessionChangegroupOutput(pGrp, xOutput, pOut, 0, 0);
6780 }
6781
6782 /*
6783 ** Delete a changegroup object.
6784 */
6785 void sqlite3changegroup_delete(sqlite3_changegroup *pGrp){
6786 if( pGrp ){
6787 int ii;
6788 for(ii=0; ii<pGrp->cd.nBufAlloc; ii++){
6789 sqlite3_free(pGrp->cd.aBuf[ii].aBuf);
6790 }
6791 sqlite3_free(pGrp->cd.record.aBuf);
6792 sqlite3_free(pGrp->cd.aBuf);
6793 sqlite3_free(pGrp->zDb);
6794 sessionDeleteTable(0, pGrp->pList);
6795 sqlite3_free(pGrp->rec.aBuf);
6796 sqlite3_free(pGrp);
6797 }
6798 }
6799
6800 /*
6801 ** Combine two changesets together.
6802 */
6803 int sqlite3changeset_concat(
6804 int nLeft, /* Number of bytes in lhs input */
6805 void *pLeft, /* Lhs input changeset */
6806 int nRight /* Number of bytes in rhs input */,
6807 void *pRight, /* Rhs input changeset */
6808 int *pnOut, /* OUT: Number of bytes in output changeset */
6809 void **ppOut /* OUT: changeset (left <concat> right) */
6810 ){
6811 sqlite3_changegroup *pGrp;
6812 int rc;
6813
6814 rc = sqlite3changegroup_new(&pGrp);
6815 if( rc==SQLITE_OK ){
6816 rc = sqlite3changegroup_add(pGrp, nLeft, pLeft);
6817 }
6818 if( rc==SQLITE_OK ){
6819 rc = sqlite3changegroup_add(pGrp, nRight, pRight);
6820 }
6821 if( rc==SQLITE_OK ){
6822 rc = sqlite3changegroup_output(pGrp, pnOut, ppOut);
6823 }
6824 sqlite3changegroup_delete(pGrp);
6825
6826 return rc;
6827 }
6828
6829 /*
6830 ** Streaming version of sqlite3changeset_concat().
6831 */
6832 int sqlite3changeset_concat_strm(
6833 int (*xInputA)(void *pIn, void *pData, int *pnData),
6834 void *pInA,
6835 int (*xInputB)(void *pIn, void *pData, int *pnData),
6836 void *pInB,
6837 int (*xOutput)(void *pOut, const void *pData, int nData),
6838 void *pOut
6839 ){
6840 sqlite3_changegroup *pGrp;
6841 int rc;
6842
6843 rc = sqlite3changegroup_new(&pGrp);
6844 if( rc==SQLITE_OK ){
6845 rc = sqlite3changegroup_add_strm(pGrp, xInputA, pInA);
6846 }
6847 if( rc==SQLITE_OK ){
6848 rc = sqlite3changegroup_add_strm(pGrp, xInputB, pInB);
6849 }
6850 if( rc==SQLITE_OK ){
6851 rc = sqlite3changegroup_output_strm(pGrp, xOutput, pOut);
6852 }
6853 sqlite3changegroup_delete(pGrp);
6854
6855 return rc;
6856 }
6857
6858 /*
6859 ** Changeset rebaser handle.
6860 */
6861 struct sqlite3_rebaser {
6862 sqlite3_changegroup grp; /* Hash table */
6863 };
6864
6865 /*
6866 ** Buffers a1 and a2 must both contain a sessions module record nCol
6867 ** fields in size. This function appends an nCol sessions module
6868 ** record to buffer pBuf that is a copy of a1, except that for
6869 ** each field that is undefined in a1[], swap in the field from a2[].
6870 */
6871 static void sessionAppendRecordMerge(
6872 SessionBuffer *pBuf, /* Buffer to append to */
6873 int nCol, /* Number of columns in each record */
6874 u8 *a1, int n1, /* Record 1 */
6875 u8 *a2, int n2, /* Record 2 */
6876 int *pRc /* IN/OUT: error code */
6877 ){
6878 sessionBufferGrow(pBuf, n1+n2, pRc);
6879 if( *pRc==SQLITE_OK ){
6880 int i;
6881 u8 *pOut = &pBuf->aBuf[pBuf->nBuf];
6882 for(i=0; i<nCol; i++){
6883 int nn1 = sessionSerialLen(a1);
6884 int nn2 = sessionSerialLen(a2);
6885 if( *a1==0 || *a1==0xFF ){
6886 memcpy(pOut, a2, nn2);
6887 pOut += nn2;
6888 }else{
6889 memcpy(pOut, a1, nn1);
6890 pOut += nn1;
6891 }
6892 a1 += nn1;
6893 a2 += nn2;
6894 }
6895
6896 pBuf->nBuf = pOut-pBuf->aBuf;
6897 assert( pBuf->nBuf<=pBuf->nAlloc );
6898 }
6899 }
6900
6901 /*
6902 ** This function is called when rebasing a local UPDATE change against one
6903 ** or more remote UPDATE changes. The aRec/nRec buffer contains the current
6904 ** old.* and new.* records for the change. The rebase buffer (a single
6905 ** record) is in aChange/nChange. The rebased change is appended to buffer
6906 ** pBuf.
6907 **
6908 ** Rebasing the UPDATE involves:
6909 **
6910 ** * Removing any changes to fields for which the corresponding field
6911 ** in the rebase buffer is set to "replaced" (type 0xFF). If this
6912 ** means the UPDATE change updates no fields, nothing is appended
6913 ** to the output buffer.
6914 **
6915 ** * For each field modified by the local change for which the
6916 ** corresponding field in the rebase buffer is not "undefined" (0x00)
6917 ** or "replaced" (0xFF), the old.* value is replaced by the value
6918 ** in the rebase buffer.
6919 */
6920 static void sessionAppendPartialUpdate(
6921 SessionBuffer *pBuf, /* Append record here */
6922 sqlite3_changeset_iter *pIter, /* Iterator pointed at local change */
6923 u8 *aRec, int nRec, /* Local change */
6924 u8 *aChange, int nChange, /* Record to rebase against */
6925 int *pRc /* IN/OUT: Return Code */
6926 ){
6927 sessionBufferGrow(pBuf, 2+nRec+nChange, pRc);
6928 if( *pRc==SQLITE_OK ){
6929 int bData = 0;
6930 u8 *pOut = &pBuf->aBuf[pBuf->nBuf];
6931 int i;
6932 u8 *a1 = aRec;
6933 u8 *a2 = aChange;
6934
6935 *pOut++ = SQLITE_UPDATE;
6936 *pOut++ = pIter->bIndirect;
6937 for(i=0; i<pIter->nCol; i++){
6938 int n1 = sessionSerialLen(a1);
6939 int n2 = sessionSerialLen(a2);
6940 if( pIter->abPK[i] || a2[0]==0 ){
6941 if( !pIter->abPK[i] && a1[0] ) bData = 1;
6942 memcpy(pOut, a1, n1);
6943 pOut += n1;
6944 }else if( a2[0]!=0xFF && a1[0] ){
6945 bData = 1;
6946 memcpy(pOut, a2, n2);
6947 pOut += n2;
6948 }else{
6949 *pOut++ = '\0';
6950 }
6951 a1 += n1;
6952 a2 += n2;
6953 }
6954 if( bData ){
6955 a2 = aChange;
6956 for(i=0; i<pIter->nCol; i++){
6957 int n1 = sessionSerialLen(a1);
6958 int n2 = sessionSerialLen(a2);
6959 if( pIter->abPK[i] || a2[0]!=0xFF ){
6960 memcpy(pOut, a1, n1);
6961 pOut += n1;
6962 }else{
6963 *pOut++ = '\0';
6964 }
6965 a1 += n1;
6966 a2 += n2;
6967 }
6968 pBuf->nBuf = (pOut - pBuf->aBuf);
6969 }
6970 }
6971 }
6972
6973 /*
6974 ** pIter is configured to iterate through a changeset. This function rebases
6975 ** that changeset according to the current configuration of the rebaser
6976 ** object passed as the first argument. If no error occurs and argument xOutput
6977 ** is not NULL, then the changeset is returned to the caller by invoking
6978 ** xOutput zero or more times and SQLITE_OK returned. Or, if xOutput is NULL,
6979 ** then (*ppOut) is set to point to a buffer containing the rebased changeset
6980 ** before this function returns. In this case (*pnOut) is set to the size of
6981 ** the buffer in bytes. It is the responsibility of the caller to eventually
6982 ** free the (*ppOut) buffer using sqlite3_free().
6983 **
6984 ** If an error occurs, an SQLite error code is returned. If ppOut and
6985 ** pnOut are not NULL, then the two output parameters are set to 0 before
6986 ** returning.
6987 */
6988 static int sessionRebase(
6989 sqlite3_rebaser *p, /* Rebaser hash table */
6990 sqlite3_changeset_iter *pIter, /* Input data */
6991 int (*xOutput)(void *pOut, const void *pData, int nData),
6992 void *pOut, /* Context for xOutput callback */
6993 int *pnOut, /* OUT: Number of bytes in output changeset */
6994 void **ppOut /* OUT: Inverse of pChangeset */
6995 ){
6996 int rc = SQLITE_OK;
6997 u8 *aRec = 0;
6998 int nRec = 0;
6999 int bNew = 0;
7000 SessionTable *pTab = 0;
7001 SessionBuffer sOut = {0,0,0};
7002
7003 while( SQLITE_ROW==sessionChangesetNext(pIter, &aRec, &nRec, &bNew) ){
7004 SessionChange *pChange = 0;
7005 int bDone = 0;
7006
7007 if( bNew ){
7008 const char *zTab = pIter->zTab;
7009 for(pTab=p->grp.pList; pTab; pTab=pTab->pNext){
7010 if( 0==sqlite3_stricmp(pTab->zName, zTab) ) break;
7011 }
7012 bNew = 0;
7013
7014 /* A patchset may not be rebased */
7015 if( pIter->bPatchset ){
7016 rc = SQLITE_ERROR;
7017 }
7018
7019 /* Append a table header to the output for this new table */
7020 sessionAppendByte(&sOut, pIter->bPatchset ? 'P' : 'T', &rc);
7021 sessionAppendVarint(&sOut, pIter->nCol, &rc);
7022 sessionAppendBlob(&sOut, pIter->abPK, pIter->nCol, &rc);
7023 sessionAppendBlob(&sOut,(u8*)pIter->zTab,(int)strlen(pIter->zTab)+1,&rc);
7024 }
7025
7026 if( pTab && rc==SQLITE_OK ){
7027 int iHash = sessionChangeHash(pTab, 0, aRec, pTab->nChange);
7028
7029 for(pChange=pTab->apChange[iHash]; pChange; pChange=pChange->pNext){
7030 if( sessionChangeEqual(pTab, 0, aRec, 0, pChange->aRecord) ){
7031 break;
7032 }
7033 }
7034 }
7035
7036 if( pChange ){
7037 assert( pChange->op==SQLITE_DELETE || pChange->op==SQLITE_INSERT );
7038 switch( pIter->op ){
7039 case SQLITE_INSERT:
7040 if( pChange->op==SQLITE_INSERT ){
7041 bDone = 1;
7042 if( pChange->bIndirect==0 ){
7043 sessionAppendByte(&sOut, SQLITE_UPDATE, &rc);
7044 sessionAppendByte(&sOut, pIter->bIndirect, &rc);
7045 sessionAppendBlob(&sOut, pChange->aRecord, pChange->nRecord, &rc);
7046 sessionAppendBlob(&sOut, aRec, nRec, &rc);
7047 }
7048 }
7049 break;
7050
7051 case SQLITE_UPDATE:
7052 bDone = 1;
7053 if( pChange->op==SQLITE_DELETE ){
7054 if( pChange->bIndirect==0 ){
7055 u8 *pCsr = aRec;
7056 sessionSkipRecord(&pCsr, pIter->nCol);
7057 sessionAppendByte(&sOut, SQLITE_INSERT, &rc);
7058 sessionAppendByte(&sOut, pIter->bIndirect, &rc);
7059 sessionAppendRecordMerge(&sOut, pIter->nCol,
7060 pCsr, nRec-(pCsr-aRec),
7061 pChange->aRecord, pChange->nRecord, &rc
7062 );
7063 }
7064 }else{
7065 sessionAppendPartialUpdate(&sOut, pIter,
7066 aRec, nRec, pChange->aRecord, pChange->nRecord, &rc
7067 );
7068 }
7069 break;
7070
7071 default:
7072 assert( pIter->op==SQLITE_DELETE );
7073 bDone = 1;
7074 if( pChange->op==SQLITE_INSERT ){
7075 sessionAppendByte(&sOut, SQLITE_DELETE, &rc);
7076 sessionAppendByte(&sOut, pIter->bIndirect, &rc);
7077 sessionAppendRecordMerge(&sOut, pIter->nCol,
7078 pChange->aRecord, pChange->nRecord, aRec, nRec, &rc
7079 );
7080 }
7081 break;
7082 }
7083 }
7084
7085 if( bDone==0 ){
7086 sessionAppendByte(&sOut, pIter->op, &rc);
7087 sessionAppendByte(&sOut, pIter->bIndirect, &rc);
7088 sessionAppendBlob(&sOut, aRec, nRec, &rc);
7089 }
7090 if( rc==SQLITE_OK && xOutput && sOut.nBuf>sessions_strm_chunk_size ){
7091 rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
7092 sOut.nBuf = 0;
7093 }
7094 if( rc ) break;
7095 }
7096
7097 if( rc!=SQLITE_OK ){
7098 sqlite3_free(sOut.aBuf);
7099 memset(&sOut, 0, sizeof(sOut));
7100 }
7101
7102 if( rc==SQLITE_OK ){
7103 if( xOutput ){
7104 if( sOut.nBuf>0 ){
7105 rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
7106 }
7107 }else if( ppOut ){
7108 *ppOut = (void*)sOut.aBuf;
7109 *pnOut = sOut.nBuf;
7110 sOut.aBuf = 0;
7111 }
7112 }
7113 sqlite3_free(sOut.aBuf);
7114 return rc;
7115 }
7116
7117 /*
7118 ** Create a new rebaser object.
7119 */
7120 int sqlite3rebaser_create(sqlite3_rebaser **ppNew){
7121 int rc = SQLITE_OK;
7122 sqlite3_rebaser *pNew;
7123
7124 pNew = sqlite3_malloc(sizeof(sqlite3_rebaser));
7125 if( pNew==0 ){
7126 rc = SQLITE_NOMEM;
7127 }else{
7128 memset(pNew, 0, sizeof(sqlite3_rebaser));
7129 }
7130 *ppNew = pNew;
7131 return rc;
7132 }
7133
7134 /*
7135 ** Call this one or more times to configure a rebaser.
7136 */
7137 int sqlite3rebaser_configure(
7138 sqlite3_rebaser *p,
7139 int nRebase, const void *pRebase
7140 ){
7141 sqlite3_changeset_iter *pIter = 0; /* Iterator opened on pData/nData */
7142 int rc; /* Return code */
7143 rc = sqlite3changeset_start(&pIter, nRebase, (void*)pRebase);
7144 if( rc==SQLITE_OK ){
7145 rc = sessionChangesetToHash(pIter, &p->grp, 1);
7146 }
7147 sqlite3changeset_finalize(pIter);
7148 return rc;
7149 }
7150
7151 /*
7152 ** Rebase a changeset according to current rebaser configuration
7153 */
7154 int sqlite3rebaser_rebase(
7155 sqlite3_rebaser *p,
7156 int nIn, const void *pIn,
7157 int *pnOut, void **ppOut
7158 ){
7159 sqlite3_changeset_iter *pIter = 0; /* Iterator to skip through input */
7160 int rc = sqlite3changeset_start(&pIter, nIn, (void*)pIn);
7161
7162 if( rc==SQLITE_OK ){
7163 rc = sessionRebase(p, pIter, 0, 0, pnOut, ppOut);
7164 sqlite3changeset_finalize(pIter);
7165 }
7166
7167 return rc;
7168 }
7169
7170 /*
7171 ** Rebase a changeset according to current rebaser configuration
7172 */
7173 int sqlite3rebaser_rebase_strm(
7174 sqlite3_rebaser *p,
7175 int (*xInput)(void *pIn, void *pData, int *pnData),
7176 void *pIn,
7177 int (*xOutput)(void *pOut, const void *pData, int nData),
7178 void *pOut
7179 ){
7180 sqlite3_changeset_iter *pIter = 0; /* Iterator to skip through input */
7181 int rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
7182
7183 if( rc==SQLITE_OK ){
7184 rc = sessionRebase(p, pIter, xOutput, pOut, 0, 0);
7185 sqlite3changeset_finalize(pIter);
7186 }
7187
7188 return rc;
7189 }
7190
7191 /*
7192 ** Destroy a rebaser object
7193 */
7194 void sqlite3rebaser_delete(sqlite3_rebaser *p){
7195 if( p ){
7196 sessionDeleteTable(0, p->grp.pList);
7197 sqlite3_free(p->grp.rec.aBuf);
7198 sqlite3_free(p);
7199 }
7200 }
7201
7202 /*
7203 ** Global configuration
7204 */
7205 int sqlite3session_config(int op, void *pArg){
7206 int rc = SQLITE_OK;
7207 switch( op ){
7208 case SQLITE_SESSION_CONFIG_STRMSIZE: {
7209 int *pInt = (int*)pArg;
7210 if( *pInt>0 ){
7211 sessions_strm_chunk_size = *pInt;
7212 }
7213 *pInt = sessions_strm_chunk_size;
7214 break;
7215 }
7216 default:
7217 rc = SQLITE_MISUSE;
7218 break;
7219 }
7220 return rc;
7221 }
7222
7223 /*
7224 ** Begin adding a change to a changegroup object.
7225 */
7226 int sqlite3changegroup_change_begin(
7227 sqlite3_changegroup *pGrp,
7228 int eOp,
7229 const char *zTab,
7230 int bIndirect,
7231 char **pzErr
7232 ){
7233 SessionTable *pTab = 0;
7234 int rc = SQLITE_OK;
7235
7236 if( pGrp->cd.pTab ){
7237 rc = SQLITE_MISUSE;
7238 }else if( eOp!=SQLITE_INSERT && eOp!=SQLITE_UPDATE && eOp!=SQLITE_DELETE ){
7239 rc = SQLITE_ERROR;
7240 }else{
7241 rc = sessionChangesetFindTable(pGrp, zTab, 0, &pTab);
7242 }
7243 if( rc==SQLITE_OK ){
7244 if( pTab==0 ){
7245 if( pzErr ){
7246 *pzErr = sqlite3_mprintf("no such table: %s", zTab);
7247 }
7248 rc = SQLITE_ERROR;
7249 }else{
7250 int nReq = pTab->nCol * (eOp==SQLITE_UPDATE ? 2 : 1);
7251 pGrp->cd.pTab = pTab;
7252 pGrp->cd.eOp = eOp;
7253 pGrp->cd.bIndirect = bIndirect;
7254
7255 if( pGrp->cd.nBufAlloc<nReq ){
7256 SessionBuffer *aBuf = (SessionBuffer*)sqlite3_realloc(
7257 pGrp->cd.aBuf, nReq * sizeof(SessionBuffer)
7258 );
7259 if( aBuf==0 ){
7260 rc = SQLITE_NOMEM;
7261 }else{
7262 memset(&aBuf[pGrp->cd.nBufAlloc], 0,
7263 sizeof(SessionBuffer) * (nReq - pGrp->cd.nBufAlloc)
7264 );
7265 pGrp->cd.aBuf = aBuf;
7266 pGrp->cd.nBufAlloc = nReq;
7267 }
7268 }
7269
7270 #ifdef SQLITE_DEBUG
7271 {
7272 /* Assert that all column values are currently undefined */
7273 int ii;
7274 for(ii=0; ii<pGrp->cd.nBufAlloc; ii++){
7275 assert( pGrp->cd.aBuf[ii].nBuf==0 );
7276 }
7277 }
7278 #endif
7279 }
7280 }
7281
7282 return rc;
7283 }
7284
7285 /*
7286 ** This function does processing common to the _change_int64(), _change_text()
7287 ** and other similar APIs.
7288 */
7289 static int checkChangeParams(
7290 sqlite3_changegroup *pGrp,
7291 int bNew,
7292 int iCol,
7293 sqlite3_int64 nReq,
7294 SessionBuffer **ppBuf
7295 ){
7296 int rc = SQLITE_OK;
7297 if( pGrp->cd.pTab==0 ){
7298 rc = SQLITE_MISUSE;
7299 }else if( iCol<0 || iCol>=pGrp->cd.pTab->nCol ){
7300 rc = SQLITE_RANGE;
7301 }else if(
7302 (bNew && pGrp->cd.eOp==SQLITE_DELETE)
7303 || (!bNew && pGrp->cd.eOp==SQLITE_INSERT)
7304 ){
7305 rc = SQLITE_ERROR;
7306 }else{
7307 SessionBuffer *pBuf = &pGrp->cd.aBuf[iCol];
7308 if( pGrp->cd.eOp==SQLITE_UPDATE && bNew ){
7309 pBuf += pGrp->cd.pTab->nCol;
7310 }
7311 pBuf->nBuf = 0;
7312 sessionBufferGrow(pBuf, nReq, &rc);
7313 pBuf->nBuf = nReq;
7314 *ppBuf = pBuf;
7315 }
7316 return rc;
7317 }
7318
7319 /*
7320 ** Configure the change currently under construction with an integer value.
7321 */
7322 int sqlite3changegroup_change_int64(
7323 sqlite3_changegroup *pGrp,
7324 int bNew,
7325 int iCol,
7326 sqlite3_int64 iVal
7327 ){
7328 int rc = SQLITE_OK;
7329 SessionBuffer *pBuf = 0;
7330
7331 if( SQLITE_OK!=(rc = checkChangeParams(pGrp, bNew, iCol, 9, &pBuf)) ){
7332 return rc;
7333 }
7334
7335 pBuf->aBuf[0] = SQLITE_INTEGER;
7336 sessionPutI64(&pBuf->aBuf[1], iVal);
7337 return SQLITE_OK;
7338 }
7339
7340 /*
7341 ** Configure the change currently under construction with a null value.
7342 */
7343 int sqlite3changegroup_change_null(
7344 sqlite3_changegroup *pGrp,
7345 int bNew,
7346 int iCol
7347 ){
7348 int rc = SQLITE_OK;
7349 SessionBuffer *pBuf = 0;
7350
7351 if( SQLITE_OK!=(rc = checkChangeParams(pGrp, bNew, iCol, 1, &pBuf)) ){
7352 return rc;
7353 }
7354
7355 pBuf->aBuf[0] = SQLITE_NULL;
7356 return SQLITE_OK;
7357 }
7358
7359 /*
7360 ** Configure the change currently under construction with a real value.
7361 */
7362 int sqlite3changegroup_change_double(
7363 sqlite3_changegroup *pGrp,
7364 int bNew,
7365 int iCol,
7366 double fVal
7367 ){
7368 int rc = SQLITE_OK;
7369 SessionBuffer *pBuf = 0;
7370
7371 if( SQLITE_OK!=(rc = checkChangeParams(pGrp, bNew, iCol, 9, &pBuf)) ){
7372 return rc;
7373 }
7374
7375 pBuf->aBuf[0] = SQLITE_FLOAT;
7376 sessionPutDouble(&pBuf->aBuf[1], fVal);
7377 return SQLITE_OK;
7378 }
7379
7380 /*
7381 ** Configure the change currently under construction with a text value.
7382 */
7383 int sqlite3changegroup_change_text(
7384 sqlite3_changegroup *pGrp,
7385 int bNew,
7386 int iCol,
7387 const char *pVal,
7388 int nVal
7389 ){
7390 int nText = nVal>=0 ? nVal : strlen(pVal);
7391 sqlite3_int64 nByte = 1 + sessionVarintLen(nText) + nText;
7392 int rc = SQLITE_OK;
7393 SessionBuffer *pBuf = 0;
7394
7395 if( SQLITE_OK!=(rc = checkChangeParams(pGrp, bNew, iCol, nByte, &pBuf)) ){
7396 return rc;
7397 }
7398
7399 pBuf->aBuf[0] = SQLITE_TEXT;
7400 pBuf->nBuf = (1 + sessionVarintPut(&pBuf->aBuf[1], nText));
7401 memcpy(&pBuf->aBuf[pBuf->nBuf], pVal, nText);
7402 pBuf->nBuf += nText;
7403
7404 return SQLITE_OK;
7405 }
7406
7407 /*
7408 ** Configure the change currently under construction with a blob value.
7409 */
7410 int sqlite3changegroup_change_blob(
7411 sqlite3_changegroup *pGrp,
7412 int bNew,
7413 int iCol,
7414 const void *pVal,
7415 int nVal
7416 ){
7417 sqlite3_int64 nByte = 1 + sessionVarintLen(nVal) + (i64)nVal;
7418 int rc = SQLITE_OK;
7419 SessionBuffer *pBuf = 0;
7420
7421 if( SQLITE_OK!=(rc = checkChangeParams(pGrp, bNew, iCol, nByte, &pBuf)) ){
7422 return rc;
7423 }
7424
7425 pBuf->aBuf[0] = SQLITE_BLOB;
7426 pBuf->nBuf = (1 + sessionVarintPut(&pBuf->aBuf[1], nVal));
7427 memcpy(&pBuf->aBuf[pBuf->nBuf], pVal, nVal);
7428 pBuf->nBuf += nVal;
7429
7430 return SQLITE_OK;
7431 }
7432
7433 /*
7434 ** Finish any change currently being constructed by the changegroup object.
7435 */
7436 int sqlite3changegroup_change_finish(
7437 sqlite3_changegroup *pGrp,
7438 int bDiscard,
7439 char **pzErr
7440 ){
7441 int rc = SQLITE_OK;
7442 if( pGrp->cd.pTab ){
7443 SessionBuffer *aBuf = pGrp->cd.aBuf;
7444 int ii;
7445
7446 if( bDiscard==0 ){
7447 int nBuf = pGrp->cd.pTab->nCol;
7448 u8 eUndef = SQLITE_NULL;
7449 if( pGrp->cd.eOp==SQLITE_UPDATE ){
7450 for(ii=0; ii<nBuf; ii++){
7451 if( pGrp->cd.pTab->abPK[ii] ){
7452 if( aBuf[ii].nBuf<=1 ){
7453 *pzErr = sqlite3_mprintf(
7454 "invalid change: %s value in PK of old.* record",
7455 aBuf[ii].nBuf==1 ? "null" : "undefined"
7456 );
7457 rc = SQLITE_ERROR;
7458 break;
7459 }else if( aBuf[ii + nBuf].nBuf>0 ){
7460 *pzErr = sqlite3_mprintf(
7461 "invalid change: defined value in PK of new.* record"
7462 );
7463 rc = SQLITE_ERROR;
7464 break;
7465 }
7466 }else
7467 if( pGrp->bPatch==0 && (aBuf[ii].nBuf>0)!=(aBuf[ii+nBuf].nBuf>0) ){
7468 *pzErr = sqlite3_mprintf(
7469 "invalid change: column %d "
7470 "- old.* value is %sdefined but new.* is %sdefined",
7471 ii, aBuf[ii].nBuf ? "" : "un", aBuf[ii+nBuf].nBuf ? "" : "un"
7472 );
7473 rc = SQLITE_ERROR;
7474 break;
7475 }
7476 }
7477 eUndef = 0x00;
7478 if( pGrp->bPatch==0 ) nBuf = nBuf * 2;
7479 }else{
7480 for(ii=0; ii<nBuf; ii++){
7481 int isPK = pGrp->cd.pTab->abPK[ii];
7482 if( (pGrp->cd.eOp==SQLITE_INSERT || pGrp->bPatch==0 || isPK)
7483 && aBuf[ii].nBuf==0
7484 ){
7485 *pzErr = sqlite3_mprintf(
7486 "invalid change: column %d is undefined", ii
7487 );
7488 rc = SQLITE_ERROR;
7489 break;
7490 }
7491 if( aBuf[ii].nBuf==1 && isPK ){
7492 *pzErr = sqlite3_mprintf(
7493 "invalid change: null value in PK"
7494 );
7495 rc = SQLITE_ERROR;
7496 break;
7497 }
7498 }
7499 }
7500
7501 pGrp->cd.record.nBuf = 0;
7502 for(ii=0; ii<nBuf; ii++){
7503 SessionBuffer *p = &pGrp->cd.aBuf[ii];
7504 if( pGrp->bPatch ){
7505 if( pGrp->cd.pTab->abPK[ii]==0 ){
7506 if( pGrp->cd.eOp==SQLITE_UPDATE ){
7507 p += pGrp->cd.pTab->nCol;
7508 }else if( pGrp->cd.eOp==SQLITE_DELETE ){
7509 continue;
7510 }
7511 }
7512 }
7513 if( 0==sessionBufferGrow(&pGrp->cd.record, p->nBuf?p->nBuf:1, &rc) ){
7514 if( p->nBuf ){
7515 memcpy(&pGrp->cd.record.aBuf[pGrp->cd.record.nBuf],p->aBuf,p->nBuf);
7516 pGrp->cd.record.nBuf += p->nBuf;
7517 }else{
7518 pGrp->cd.record.aBuf[pGrp->cd.record.nBuf++] = eUndef;
7519 }
7520 }
7521 }
7522 if( rc==SQLITE_OK ){
7523 rc = sessionOneChangeToHash(
7524 pGrp, pGrp->cd.pTab,
7525 pGrp->cd.eOp, pGrp->cd.bIndirect, pGrp->cd.pTab->nCol,
7526 pGrp->cd.record.aBuf, pGrp->cd.record.nBuf, 0
7527 );
7528 }
7529 }
7530
7531 /* Reset all aBuf[] entries to "undefined". */
7532 {
7533 int nZero = pGrp->cd.pTab->nCol;
7534 if( pGrp->cd.eOp==SQLITE_UPDATE ) nZero += nZero;
7535 for(ii=0; ii<nZero; ii++){
7536 pGrp->cd.aBuf[ii].nBuf = 0;
7537 }
7538 }
7539 pGrp->cd.pTab = 0;
7540 }
7541
7542 return rc;
7543 }
7544
7545 #endif /* SQLITE_ENABLE_SESSION && SQLITE_ENABLE_PREUPDATE_HOOK */