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