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 u8 bRebaseStarted; /* If table header is already in rebase */ 3447 u8 bRebase; /* True to collect rebase information */ 3448 }; 3449 3450 /* 3451 ** Formulate a statement to DELETE a row from database db. Assuming a table 3452 ** structure like this: 3453 ** 3454 ** CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c)); 3455 ** 3456 ** The DELETE statement looks like this: 3457 ** 3458 ** DELETE FROM x WHERE a = :1 AND c = :3 AND (:5 OR b IS :2 AND d IS :4) 3459 ** 3460 ** Variable :5 (nCol+1) is a boolean. It should be set to 0 if we require 3461 ** matching b and d values, or 1 otherwise. The second case comes up if the 3462 ** conflict handler is invoked with NOTFOUND and returns CHANGESET_REPLACE. 3463 ** 3464 ** If successful, SQLITE_OK is returned and SessionApplyCtx.pDelete is left 3465 ** pointing to the prepared version of the SQL statement. 3466 */ 3467 static int sessionDeleteRow( 3468 sqlite3 *db, /* Database handle */ 3469 const char *zTab, /* Table name */ 3470 SessionApplyCtx *p /* Session changeset-apply context */ 3471 ){ 3472 int i; 3473 const char *zSep = ""; 3474 int rc = SQLITE_OK; 3475 SessionBuffer buf = {0, 0, 0}; 3476 int nPk = 0; 3477 3478 sessionAppendStr(&buf, "DELETE FROM ", &rc); 3479 sessionAppendIdent(&buf, zTab, &rc); 3480 sessionAppendStr(&buf, " WHERE ", &rc); 3481 3482 for(i=0; i<p->nCol; i++){ 3483 if( p->abPK[i] ){ 3484 nPk++; 3485 sessionAppendStr(&buf, zSep, &rc); 3486 sessionAppendIdent(&buf, p->azCol[i], &rc); 3487 sessionAppendStr(&buf, " = ?", &rc); 3488 sessionAppendInteger(&buf, i+1, &rc); 3489 zSep = " AND "; 3490 } 3491 } 3492 3493 if( nPk<p->nCol ){ 3494 sessionAppendStr(&buf, " AND (?", &rc); 3495 sessionAppendInteger(&buf, p->nCol+1, &rc); 3496 sessionAppendStr(&buf, " OR ", &rc); 3497 3498 zSep = ""; 3499 for(i=0; i<p->nCol; i++){ 3500 if( !p->abPK[i] ){ 3501 sessionAppendStr(&buf, zSep, &rc); 3502 sessionAppendIdent(&buf, p->azCol[i], &rc); 3503 sessionAppendStr(&buf, " IS ?", &rc); 3504 sessionAppendInteger(&buf, i+1, &rc); 3505 zSep = "AND "; 3506 } 3507 } 3508 sessionAppendStr(&buf, ")", &rc); 3509 } 3510 3511 if( rc==SQLITE_OK ){ 3512 rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pDelete, 0); 3513 } 3514 sqlite3_free(buf.aBuf); 3515 3516 return rc; 3517 } 3518 3519 /* 3520 ** Formulate and prepare a statement to UPDATE a row from database db. 3521 ** Assuming a table structure like this: 3522 ** 3523 ** CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c)); 3524 ** 3525 ** The UPDATE statement looks like this: 3526 ** 3527 ** UPDATE x SET 3528 ** a = CASE WHEN ?2 THEN ?3 ELSE a END, 3529 ** b = CASE WHEN ?5 THEN ?6 ELSE b END, 3530 ** c = CASE WHEN ?8 THEN ?9 ELSE c END, 3531 ** d = CASE WHEN ?11 THEN ?12 ELSE d END 3532 ** WHERE a = ?1 AND c = ?7 AND (?13 OR 3533 ** (?5==0 OR b IS ?4) AND (?11==0 OR d IS ?10) AND 3534 ** ) 3535 ** 3536 ** For each column in the table, there are three variables to bind: 3537 ** 3538 ** ?(i*3+1) The old.* value of the column, if any. 3539 ** ?(i*3+2) A boolean flag indicating that the value is being modified. 3540 ** ?(i*3+3) The new.* value of the column, if any. 3541 ** 3542 ** Also, a boolean flag that, if set to true, causes the statement to update 3543 ** a row even if the non-PK values do not match. This is required if the 3544 ** conflict-handler is invoked with CHANGESET_DATA and returns 3545 ** CHANGESET_REPLACE. This is variable "?(nCol*3+1)". 3546 ** 3547 ** If successful, SQLITE_OK is returned and SessionApplyCtx.pUpdate is left 3548 ** pointing to the prepared version of the SQL statement. 3549 */ 3550 static int sessionUpdateRow( 3551 sqlite3 *db, /* Database handle */ 3552 const char *zTab, /* Table name */ 3553 SessionApplyCtx *p /* Session changeset-apply context */ 3554 ){ 3555 int rc = SQLITE_OK; 3556 int i; 3557 const char *zSep = ""; 3558 SessionBuffer buf = {0, 0, 0}; 3559 3560 /* Append "UPDATE tbl SET " */ 3561 sessionAppendStr(&buf, "UPDATE ", &rc); 3562 sessionAppendIdent(&buf, zTab, &rc); 3563 sessionAppendStr(&buf, " SET ", &rc); 3564 3565 /* Append the assignments */ 3566 for(i=0; i<p->nCol; i++){ 3567 sessionAppendStr(&buf, zSep, &rc); 3568 sessionAppendIdent(&buf, p->azCol[i], &rc); 3569 sessionAppendStr(&buf, " = CASE WHEN ?", &rc); 3570 sessionAppendInteger(&buf, i*3+2, &rc); 3571 sessionAppendStr(&buf, " THEN ?", &rc); 3572 sessionAppendInteger(&buf, i*3+3, &rc); 3573 sessionAppendStr(&buf, " ELSE ", &rc); 3574 sessionAppendIdent(&buf, p->azCol[i], &rc); 3575 sessionAppendStr(&buf, " END", &rc); 3576 zSep = ", "; 3577 } 3578 3579 /* Append the PK part of the WHERE clause */ 3580 sessionAppendStr(&buf, " WHERE ", &rc); 3581 for(i=0; i<p->nCol; i++){ 3582 if( p->abPK[i] ){ 3583 sessionAppendIdent(&buf, p->azCol[i], &rc); 3584 sessionAppendStr(&buf, " = ?", &rc); 3585 sessionAppendInteger(&buf, i*3+1, &rc); 3586 sessionAppendStr(&buf, " AND ", &rc); 3587 } 3588 } 3589 3590 /* Append the non-PK part of the WHERE clause */ 3591 sessionAppendStr(&buf, " (?", &rc); 3592 sessionAppendInteger(&buf, p->nCol*3+1, &rc); 3593 sessionAppendStr(&buf, " OR 1", &rc); 3594 for(i=0; i<p->nCol; i++){ 3595 if( !p->abPK[i] ){ 3596 sessionAppendStr(&buf, " AND (?", &rc); 3597 sessionAppendInteger(&buf, i*3+2, &rc); 3598 sessionAppendStr(&buf, "=0 OR ", &rc); 3599 sessionAppendIdent(&buf, p->azCol[i], &rc); 3600 sessionAppendStr(&buf, " IS ?", &rc); 3601 sessionAppendInteger(&buf, i*3+1, &rc); 3602 sessionAppendStr(&buf, ")", &rc); 3603 } 3604 } 3605 sessionAppendStr(&buf, ")", &rc); 3606 3607 if( rc==SQLITE_OK ){ 3608 rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pUpdate, 0); 3609 } 3610 sqlite3_free(buf.aBuf); 3611 3612 return rc; 3613 } 3614 3615 3616 /* 3617 ** Formulate and prepare an SQL statement to query table zTab by primary 3618 ** key. Assuming the following table structure: 3619 ** 3620 ** CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c)); 3621 ** 3622 ** The SELECT statement looks like this: 3623 ** 3624 ** SELECT * FROM x WHERE a = ?1 AND c = ?3 3625 ** 3626 ** If successful, SQLITE_OK is returned and SessionApplyCtx.pSelect is left 3627 ** pointing to the prepared version of the SQL statement. 3628 */ 3629 static int sessionSelectRow( 3630 sqlite3 *db, /* Database handle */ 3631 const char *zTab, /* Table name */ 3632 SessionApplyCtx *p /* Session changeset-apply context */ 3633 ){ 3634 return sessionSelectStmt( 3635 db, "main", zTab, p->nCol, p->azCol, p->abPK, &p->pSelect); 3636 } 3637 3638 /* 3639 ** Formulate and prepare an INSERT statement to add a record to table zTab. 3640 ** For example: 3641 ** 3642 ** INSERT INTO main."zTab" VALUES(?1, ?2, ?3 ...); 3643 ** 3644 ** If successful, SQLITE_OK is returned and SessionApplyCtx.pInsert is left 3645 ** pointing to the prepared version of the SQL statement. 3646 */ 3647 static int sessionInsertRow( 3648 sqlite3 *db, /* Database handle */ 3649 const char *zTab, /* Table name */ 3650 SessionApplyCtx *p /* Session changeset-apply context */ 3651 ){ 3652 int rc = SQLITE_OK; 3653 int i; 3654 SessionBuffer buf = {0, 0, 0}; 3655 3656 sessionAppendStr(&buf, "INSERT INTO main.", &rc); 3657 sessionAppendIdent(&buf, zTab, &rc); 3658 sessionAppendStr(&buf, "(", &rc); 3659 for(i=0; i<p->nCol; i++){ 3660 if( i!=0 ) sessionAppendStr(&buf, ", ", &rc); 3661 sessionAppendIdent(&buf, p->azCol[i], &rc); 3662 } 3663 3664 sessionAppendStr(&buf, ") VALUES(?", &rc); 3665 for(i=1; i<p->nCol; i++){ 3666 sessionAppendStr(&buf, ", ?", &rc); 3667 } 3668 sessionAppendStr(&buf, ")", &rc); 3669 3670 if( rc==SQLITE_OK ){ 3671 rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pInsert, 0); 3672 } 3673 sqlite3_free(buf.aBuf); 3674 return rc; 3675 } 3676 3677 static int sessionPrepare(sqlite3 *db, sqlite3_stmt **pp, const char *zSql){ 3678 return sqlite3_prepare_v2(db, zSql, -1, pp, 0); 3679 } 3680 3681 /* 3682 ** Prepare statements for applying changes to the sqlite_stat1 table. 3683 ** These are similar to those created by sessionSelectRow(), 3684 ** sessionInsertRow(), sessionUpdateRow() and sessionDeleteRow() for 3685 ** other tables. 3686 */ 3687 static int sessionStat1Sql(sqlite3 *db, SessionApplyCtx *p){ 3688 int rc = sessionSelectRow(db, "sqlite_stat1", p); 3689 if( rc==SQLITE_OK ){ 3690 rc = sessionPrepare(db, &p->pInsert, 3691 "INSERT INTO main.sqlite_stat1 VALUES(?1, " 3692 "CASE WHEN length(?2)=0 AND typeof(?2)='blob' THEN NULL ELSE ?2 END, " 3693 "?3)" 3694 ); 3695 } 3696 if( rc==SQLITE_OK ){ 3697 rc = sessionPrepare(db, &p->pUpdate, 3698 "UPDATE main.sqlite_stat1 SET " 3699 "tbl = CASE WHEN ?2 THEN ?3 ELSE tbl END, " 3700 "idx = CASE WHEN ?5 THEN ?6 ELSE idx END, " 3701 "stat = CASE WHEN ?8 THEN ?9 ELSE stat END " 3702 "WHERE tbl=?1 AND idx IS " 3703 "CASE WHEN length(?4)=0 AND typeof(?4)='blob' THEN NULL ELSE ?4 END " 3704 "AND (?10 OR ?8=0 OR stat IS ?7)" 3705 ); 3706 } 3707 if( rc==SQLITE_OK ){ 3708 rc = sessionPrepare(db, &p->pDelete, 3709 "DELETE FROM main.sqlite_stat1 WHERE tbl=?1 AND idx IS " 3710 "CASE WHEN length(?2)=0 AND typeof(?2)='blob' THEN NULL ELSE ?2 END " 3711 "AND (?4 OR stat IS ?3)" 3712 ); 3713 } 3714 return rc; 3715 } 3716 3717 /* 3718 ** A wrapper around sqlite3_bind_value() that detects an extra problem. 3719 ** See comments in the body of this function for details. 3720 */ 3721 static int sessionBindValue( 3722 sqlite3_stmt *pStmt, /* Statement to bind value to */ 3723 int i, /* Parameter number to bind to */ 3724 sqlite3_value *pVal /* Value to bind */ 3725 ){ 3726 int eType = sqlite3_value_type(pVal); 3727 /* COVERAGE: The (pVal->z==0) branch is never true using current versions 3728 ** of SQLite. If a malloc fails in an sqlite3_value_xxx() function, either 3729 ** the (pVal->z) variable remains as it was or the type of the value is 3730 ** set to SQLITE_NULL. */ 3731 if( (eType==SQLITE_TEXT || eType==SQLITE_BLOB) && pVal->z==0 ){ 3732 /* This condition occurs when an earlier OOM in a call to 3733 ** sqlite3_value_text() or sqlite3_value_blob() (perhaps from within 3734 ** a conflict-handler) has zeroed the pVal->z pointer. Return NOMEM. */ 3735 return SQLITE_NOMEM; 3736 } 3737 return sqlite3_bind_value(pStmt, i, pVal); 3738 } 3739 3740 /* 3741 ** Iterator pIter must point to an SQLITE_INSERT entry. This function 3742 ** transfers new.* values from the current iterator entry to statement 3743 ** pStmt. The table being inserted into has nCol columns. 3744 ** 3745 ** New.* value $i from the iterator is bound to variable ($i+1) of 3746 ** statement pStmt. If parameter abPK is NULL, all values from 0 to (nCol-1) 3747 ** are transfered to the statement. Otherwise, if abPK is not NULL, it points 3748 ** to an array nCol elements in size. In this case only those values for 3749 ** which abPK[$i] is true are read from the iterator and bound to the 3750 ** statement. 3751 ** 3752 ** An SQLite error code is returned if an error occurs. Otherwise, SQLITE_OK. 3753 */ 3754 static int sessionBindRow( 3755 sqlite3_changeset_iter *pIter, /* Iterator to read values from */ 3756 int(*xValue)(sqlite3_changeset_iter *, int, sqlite3_value **), 3757 int nCol, /* Number of columns */ 3758 u8 *abPK, /* If not NULL, bind only if true */ 3759 sqlite3_stmt *pStmt /* Bind values to this statement */ 3760 ){ 3761 int i; 3762 int rc = SQLITE_OK; 3763 3764 /* Neither sqlite3changeset_old or sqlite3changeset_new can fail if the 3765 ** argument iterator points to a suitable entry. Make sure that xValue 3766 ** is one of these to guarantee that it is safe to ignore the return 3767 ** in the code below. */ 3768 assert( xValue==sqlite3changeset_old || xValue==sqlite3changeset_new ); 3769 3770 for(i=0; rc==SQLITE_OK && i<nCol; i++){ 3771 if( !abPK || abPK[i] ){ 3772 sqlite3_value *pVal; 3773 (void)xValue(pIter, i, &pVal); 3774 if( pVal==0 ){ 3775 /* The value in the changeset was "undefined". This indicates a 3776 ** corrupt changeset blob. */ 3777 rc = SQLITE_CORRUPT_BKPT; 3778 }else{ 3779 rc = sessionBindValue(pStmt, i+1, pVal); 3780 } 3781 } 3782 } 3783 return rc; 3784 } 3785 3786 /* 3787 ** SQL statement pSelect is as generated by the sessionSelectRow() function. 3788 ** This function binds the primary key values from the change that changeset 3789 ** iterator pIter points to to the SELECT and attempts to seek to the table 3790 ** entry. If a row is found, the SELECT statement left pointing at the row 3791 ** and SQLITE_ROW is returned. Otherwise, if no row is found and no error 3792 ** has occured, the statement is reset and SQLITE_OK is returned. If an 3793 ** error occurs, the statement is reset and an SQLite error code is returned. 3794 ** 3795 ** If this function returns SQLITE_ROW, the caller must eventually reset() 3796 ** statement pSelect. If any other value is returned, the statement does 3797 ** not require a reset(). 3798 ** 3799 ** If the iterator currently points to an INSERT record, bind values from the 3800 ** new.* record to the SELECT statement. Or, if it points to a DELETE or 3801 ** UPDATE, bind values from the old.* record. 3802 */ 3803 static int sessionSeekToRow( 3804 sqlite3 *db, /* Database handle */ 3805 sqlite3_changeset_iter *pIter, /* Changeset iterator */ 3806 u8 *abPK, /* Primary key flags array */ 3807 sqlite3_stmt *pSelect /* SELECT statement from sessionSelectRow() */ 3808 ){ 3809 int rc; /* Return code */ 3810 int nCol; /* Number of columns in table */ 3811 int op; /* Changset operation (SQLITE_UPDATE etc.) */ 3812 const char *zDummy; /* Unused */ 3813 3814 sqlite3changeset_op(pIter, &zDummy, &nCol, &op, 0); 3815 rc = sessionBindRow(pIter, 3816 op==SQLITE_INSERT ? sqlite3changeset_new : sqlite3changeset_old, 3817 nCol, abPK, pSelect 3818 ); 3819 3820 if( rc==SQLITE_OK ){ 3821 rc = sqlite3_step(pSelect); 3822 if( rc!=SQLITE_ROW ) rc = sqlite3_reset(pSelect); 3823 } 3824 3825 return rc; 3826 } 3827 3828 /* 3829 ** This function is called from within sqlite3changset_apply_v2() when 3830 ** a conflict is encountered and resolved using conflict resolution 3831 ** mode eType (either SQLITE_CHANGESET_OMIT or SQLITE_CHANGESET_REPLACE).. 3832 ** It adds a conflict resolution record to the buffer in 3833 ** SessionApplyCtx.rebase, which will eventually be returned to the caller 3834 ** of apply_v2() as the "rebase" buffer. 3835 ** 3836 ** Return SQLITE_OK if successful, or an SQLite error code otherwise. 3837 */ 3838 static int sessionRebaseAdd( 3839 SessionApplyCtx *p, /* Apply context */ 3840 int eType, /* Conflict resolution (OMIT or REPLACE) */ 3841 sqlite3_changeset_iter *pIter /* Iterator pointing at current change */ 3842 ){ 3843 int rc = SQLITE_OK; 3844 if( p->bRebase ){ 3845 int i; 3846 int eOp = pIter->op; 3847 if( p->bRebaseStarted==0 ){ 3848 /* Append a table-header to the rebase buffer */ 3849 const char *zTab = pIter->zTab; 3850 sessionAppendByte(&p->rebase, 'T', &rc); 3851 sessionAppendVarint(&p->rebase, p->nCol, &rc); 3852 sessionAppendBlob(&p->rebase, p->abPK, p->nCol, &rc); 3853 sessionAppendBlob(&p->rebase, (u8*)zTab, (int)strlen(zTab)+1, &rc); 3854 p->bRebaseStarted = 1; 3855 } 3856 3857 assert( eType==SQLITE_CHANGESET_REPLACE||eType==SQLITE_CHANGESET_OMIT ); 3858 assert( eOp==SQLITE_DELETE || eOp==SQLITE_INSERT || eOp==SQLITE_UPDATE ); 3859 3860 sessionAppendByte(&p->rebase, 3861 (eOp==SQLITE_DELETE ? SQLITE_DELETE : SQLITE_INSERT), &rc 3862 ); 3863 sessionAppendByte(&p->rebase, (eType==SQLITE_CHANGESET_REPLACE), &rc); 3864 for(i=0; i<p->nCol; i++){ 3865 sqlite3_value *pVal = 0; 3866 if( eOp==SQLITE_DELETE || (eOp==SQLITE_UPDATE && p->abPK[i]) ){ 3867 sqlite3changeset_old(pIter, i, &pVal); 3868 }else{ 3869 sqlite3changeset_new(pIter, i, &pVal); 3870 } 3871 sessionAppendValue(&p->rebase, pVal, &rc); 3872 } 3873 } 3874 return rc; 3875 } 3876 3877 /* 3878 ** Invoke the conflict handler for the change that the changeset iterator 3879 ** currently points to. 3880 ** 3881 ** Argument eType must be either CHANGESET_DATA or CHANGESET_CONFLICT. 3882 ** If argument pbReplace is NULL, then the type of conflict handler invoked 3883 ** depends solely on eType, as follows: 3884 ** 3885 ** eType value Value passed to xConflict 3886 ** ------------------------------------------------- 3887 ** CHANGESET_DATA CHANGESET_NOTFOUND 3888 ** CHANGESET_CONFLICT CHANGESET_CONSTRAINT 3889 ** 3890 ** Or, if pbReplace is not NULL, then an attempt is made to find an existing 3891 ** record with the same primary key as the record about to be deleted, updated 3892 ** or inserted. If such a record can be found, it is available to the conflict 3893 ** handler as the "conflicting" record. In this case the type of conflict 3894 ** handler invoked is as follows: 3895 ** 3896 ** eType value PK Record found? Value passed to xConflict 3897 ** ---------------------------------------------------------------- 3898 ** CHANGESET_DATA Yes CHANGESET_DATA 3899 ** CHANGESET_DATA No CHANGESET_NOTFOUND 3900 ** CHANGESET_CONFLICT Yes CHANGESET_CONFLICT 3901 ** CHANGESET_CONFLICT No CHANGESET_CONSTRAINT 3902 ** 3903 ** If pbReplace is not NULL, and a record with a matching PK is found, and 3904 ** the conflict handler function returns SQLITE_CHANGESET_REPLACE, *pbReplace 3905 ** is set to non-zero before returning SQLITE_OK. 3906 ** 3907 ** If the conflict handler returns SQLITE_CHANGESET_ABORT, SQLITE_ABORT is 3908 ** returned. Or, if the conflict handler returns an invalid value, 3909 ** SQLITE_MISUSE. If the conflict handler returns SQLITE_CHANGESET_OMIT, 3910 ** this function returns SQLITE_OK. 3911 */ 3912 static int sessionConflictHandler( 3913 int eType, /* Either CHANGESET_DATA or CONFLICT */ 3914 SessionApplyCtx *p, /* changeset_apply() context */ 3915 sqlite3_changeset_iter *pIter, /* Changeset iterator */ 3916 int(*xConflict)(void *, int, sqlite3_changeset_iter*), 3917 void *pCtx, /* First argument for conflict handler */ 3918 int *pbReplace /* OUT: Set to true if PK row is found */ 3919 ){ 3920 int res = 0; /* Value returned by conflict handler */ 3921 int rc; 3922 int nCol; 3923 int op; 3924 const char *zDummy; 3925 3926 sqlite3changeset_op(pIter, &zDummy, &nCol, &op, 0); 3927 3928 assert( eType==SQLITE_CHANGESET_CONFLICT || eType==SQLITE_CHANGESET_DATA ); 3929 assert( SQLITE_CHANGESET_CONFLICT+1==SQLITE_CHANGESET_CONSTRAINT ); 3930 assert( SQLITE_CHANGESET_DATA+1==SQLITE_CHANGESET_NOTFOUND ); 3931 3932 /* Bind the new.* PRIMARY KEY values to the SELECT statement. */ 3933 if( pbReplace ){ 3934 rc = sessionSeekToRow(p->db, pIter, p->abPK, p->pSelect); 3935 }else{ 3936 rc = SQLITE_OK; 3937 } 3938 3939 if( rc==SQLITE_ROW ){ 3940 /* There exists another row with the new.* primary key. */ 3941 pIter->pConflict = p->pSelect; 3942 res = xConflict(pCtx, eType, pIter); 3943 pIter->pConflict = 0; 3944 rc = sqlite3_reset(p->pSelect); 3945 }else if( rc==SQLITE_OK ){ 3946 if( p->bDeferConstraints && eType==SQLITE_CHANGESET_CONFLICT ){ 3947 /* Instead of invoking the conflict handler, append the change blob 3948 ** to the SessionApplyCtx.constraints buffer. */ 3949 u8 *aBlob = &pIter->in.aData[pIter->in.iCurrent]; 3950 int nBlob = pIter->in.iNext - pIter->in.iCurrent; 3951 sessionAppendBlob(&p->constraints, aBlob, nBlob, &rc); 3952 return SQLITE_OK; 3953 }else{ 3954 /* No other row with the new.* primary key. */ 3955 res = xConflict(pCtx, eType+1, pIter); 3956 if( res==SQLITE_CHANGESET_REPLACE ) rc = SQLITE_MISUSE; 3957 } 3958 } 3959 3960 if( rc==SQLITE_OK ){ 3961 switch( res ){ 3962 case SQLITE_CHANGESET_REPLACE: 3963 assert( pbReplace ); 3964 *pbReplace = 1; 3965 break; 3966 3967 case SQLITE_CHANGESET_OMIT: 3968 break; 3969 3970 case SQLITE_CHANGESET_ABORT: 3971 rc = SQLITE_ABORT; 3972 break; 3973 3974 default: 3975 rc = SQLITE_MISUSE; 3976 break; 3977 } 3978 if( rc==SQLITE_OK ){ 3979 rc = sessionRebaseAdd(p, res, pIter); 3980 } 3981 } 3982 3983 return rc; 3984 } 3985 3986 /* 3987 ** Attempt to apply the change that the iterator passed as the first argument 3988 ** currently points to to the database. If a conflict is encountered, invoke 3989 ** the conflict handler callback. 3990 ** 3991 ** If argument pbRetry is NULL, then ignore any CHANGESET_DATA conflict. If 3992 ** one is encountered, update or delete the row with the matching primary key 3993 ** instead. Or, if pbRetry is not NULL and a CHANGESET_DATA conflict occurs, 3994 ** invoke the conflict handler. If it returns CHANGESET_REPLACE, set *pbRetry 3995 ** to true before returning. In this case the caller will invoke this function 3996 ** again, this time with pbRetry set to NULL. 3997 ** 3998 ** If argument pbReplace is NULL and a CHANGESET_CONFLICT conflict is 3999 ** encountered invoke the conflict handler with CHANGESET_CONSTRAINT instead. 4000 ** Or, if pbReplace is not NULL, invoke it with CHANGESET_CONFLICT. If such 4001 ** an invocation returns SQLITE_CHANGESET_REPLACE, set *pbReplace to true 4002 ** before retrying. In this case the caller attempts to remove the conflicting 4003 ** row before invoking this function again, this time with pbReplace set 4004 ** to NULL. 4005 ** 4006 ** If any conflict handler returns SQLITE_CHANGESET_ABORT, this function 4007 ** returns SQLITE_ABORT. Otherwise, if no error occurs, SQLITE_OK is 4008 ** returned. 4009 */ 4010 static int sessionApplyOneOp( 4011 sqlite3_changeset_iter *pIter, /* Changeset iterator */ 4012 SessionApplyCtx *p, /* changeset_apply() context */ 4013 int(*xConflict)(void *, int, sqlite3_changeset_iter *), 4014 void *pCtx, /* First argument for the conflict handler */ 4015 int *pbReplace, /* OUT: True to remove PK row and retry */ 4016 int *pbRetry /* OUT: True to retry. */ 4017 ){ 4018 const char *zDummy; 4019 int op; 4020 int nCol; 4021 int rc = SQLITE_OK; 4022 4023 assert( p->pDelete && p->pUpdate && p->pInsert && p->pSelect ); 4024 assert( p->azCol && p->abPK ); 4025 assert( !pbReplace || *pbReplace==0 ); 4026 4027 sqlite3changeset_op(pIter, &zDummy, &nCol, &op, 0); 4028 4029 if( op==SQLITE_DELETE ){ 4030 4031 /* Bind values to the DELETE statement. If conflict handling is required, 4032 ** bind values for all columns and set bound variable (nCol+1) to true. 4033 ** Or, if conflict handling is not required, bind just the PK column 4034 ** values and, if it exists, set (nCol+1) to false. Conflict handling 4035 ** is not required if: 4036 ** 4037 ** * this is a patchset, or 4038 ** * (pbRetry==0), or 4039 ** * all columns of the table are PK columns (in this case there is 4040 ** no (nCol+1) variable to bind to). 4041 */ 4042 u8 *abPK = (pIter->bPatchset ? p->abPK : 0); 4043 rc = sessionBindRow(pIter, sqlite3changeset_old, nCol, abPK, p->pDelete); 4044 if( rc==SQLITE_OK && sqlite3_bind_parameter_count(p->pDelete)>nCol ){ 4045 rc = sqlite3_bind_int(p->pDelete, nCol+1, (pbRetry==0 || abPK)); 4046 } 4047 if( rc!=SQLITE_OK ) return rc; 4048 4049 sqlite3_step(p->pDelete); 4050 rc = sqlite3_reset(p->pDelete); 4051 if( rc==SQLITE_OK && sqlite3_changes(p->db)==0 ){ 4052 rc = sessionConflictHandler( 4053 SQLITE_CHANGESET_DATA, p, pIter, xConflict, pCtx, pbRetry 4054 ); 4055 }else if( (rc&0xff)==SQLITE_CONSTRAINT ){ 4056 rc = sessionConflictHandler( 4057 SQLITE_CHANGESET_CONFLICT, p, pIter, xConflict, pCtx, 0 4058 ); 4059 } 4060 4061 }else if( op==SQLITE_UPDATE ){ 4062 int i; 4063 4064 /* Bind values to the UPDATE statement. */ 4065 for(i=0; rc==SQLITE_OK && i<nCol; i++){ 4066 sqlite3_value *pOld = sessionChangesetOld(pIter, i); 4067 sqlite3_value *pNew = sessionChangesetNew(pIter, i); 4068 4069 sqlite3_bind_int(p->pUpdate, i*3+2, !!pNew); 4070 if( pOld ){ 4071 rc = sessionBindValue(p->pUpdate, i*3+1, pOld); 4072 } 4073 if( rc==SQLITE_OK && pNew ){ 4074 rc = sessionBindValue(p->pUpdate, i*3+3, pNew); 4075 } 4076 } 4077 if( rc==SQLITE_OK ){ 4078 sqlite3_bind_int(p->pUpdate, nCol*3+1, pbRetry==0 || pIter->bPatchset); 4079 } 4080 if( rc!=SQLITE_OK ) return rc; 4081 4082 /* Attempt the UPDATE. In the case of a NOTFOUND or DATA conflict, 4083 ** the result will be SQLITE_OK with 0 rows modified. */ 4084 sqlite3_step(p->pUpdate); 4085 rc = sqlite3_reset(p->pUpdate); 4086 4087 if( rc==SQLITE_OK && sqlite3_changes(p->db)==0 ){ 4088 /* A NOTFOUND or DATA error. Search the table to see if it contains 4089 ** a row with a matching primary key. If so, this is a DATA conflict. 4090 ** Otherwise, if there is no primary key match, it is a NOTFOUND. */ 4091 4092 rc = sessionConflictHandler( 4093 SQLITE_CHANGESET_DATA, p, pIter, xConflict, pCtx, pbRetry 4094 ); 4095 4096 }else if( (rc&0xff)==SQLITE_CONSTRAINT ){ 4097 /* This is always a CONSTRAINT conflict. */ 4098 rc = sessionConflictHandler( 4099 SQLITE_CHANGESET_CONFLICT, p, pIter, xConflict, pCtx, 0 4100 ); 4101 } 4102 4103 }else{ 4104 assert( op==SQLITE_INSERT ); 4105 if( p->bStat1 ){ 4106 /* Check if there is a conflicting row. For sqlite_stat1, this needs 4107 ** to be done using a SELECT, as there is no PRIMARY KEY in the 4108 ** database schema to throw an exception if a duplicate is inserted. */ 4109 rc = sessionSeekToRow(p->db, pIter, p->abPK, p->pSelect); 4110 if( rc==SQLITE_ROW ){ 4111 rc = SQLITE_CONSTRAINT; 4112 sqlite3_reset(p->pSelect); 4113 } 4114 } 4115 4116 if( rc==SQLITE_OK ){ 4117 rc = sessionBindRow(pIter, sqlite3changeset_new, nCol, 0, p->pInsert); 4118 if( rc!=SQLITE_OK ) return rc; 4119 4120 sqlite3_step(p->pInsert); 4121 rc = sqlite3_reset(p->pInsert); 4122 } 4123 4124 if( (rc&0xff)==SQLITE_CONSTRAINT ){ 4125 rc = sessionConflictHandler( 4126 SQLITE_CHANGESET_CONFLICT, p, pIter, xConflict, pCtx, pbReplace 4127 ); 4128 } 4129 } 4130 4131 return rc; 4132 } 4133 4134 /* 4135 ** Attempt to apply the change that the iterator passed as the first argument 4136 ** currently points to to the database. If a conflict is encountered, invoke 4137 ** the conflict handler callback. 4138 ** 4139 ** The difference between this function and sessionApplyOne() is that this 4140 ** function handles the case where the conflict-handler is invoked and 4141 ** returns SQLITE_CHANGESET_REPLACE - indicating that the change should be 4142 ** retried in some manner. 4143 */ 4144 static int sessionApplyOneWithRetry( 4145 sqlite3 *db, /* Apply change to "main" db of this handle */ 4146 sqlite3_changeset_iter *pIter, /* Changeset iterator to read change from */ 4147 SessionApplyCtx *pApply, /* Apply context */ 4148 int(*xConflict)(void*, int, sqlite3_changeset_iter*), 4149 void *pCtx /* First argument passed to xConflict */ 4150 ){ 4151 int bReplace = 0; 4152 int bRetry = 0; 4153 int rc; 4154 4155 rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, &bReplace, &bRetry); 4156 if( rc==SQLITE_OK ){ 4157 /* If the bRetry flag is set, the change has not been applied due to an 4158 ** SQLITE_CHANGESET_DATA problem (i.e. this is an UPDATE or DELETE and 4159 ** a row with the correct PK is present in the db, but one or more other 4160 ** fields do not contain the expected values) and the conflict handler 4161 ** returned SQLITE_CHANGESET_REPLACE. In this case retry the operation, 4162 ** but pass NULL as the final argument so that sessionApplyOneOp() ignores 4163 ** the SQLITE_CHANGESET_DATA problem. */ 4164 if( bRetry ){ 4165 assert( pIter->op==SQLITE_UPDATE || pIter->op==SQLITE_DELETE ); 4166 rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, 0, 0); 4167 } 4168 4169 /* If the bReplace flag is set, the change is an INSERT that has not 4170 ** been performed because the database already contains a row with the 4171 ** specified primary key and the conflict handler returned 4172 ** SQLITE_CHANGESET_REPLACE. In this case remove the conflicting row 4173 ** before reattempting the INSERT. */ 4174 else if( bReplace ){ 4175 assert( pIter->op==SQLITE_INSERT ); 4176 rc = sqlite3_exec(db, "SAVEPOINT replace_op", 0, 0, 0); 4177 if( rc==SQLITE_OK ){ 4178 rc = sessionBindRow(pIter, 4179 sqlite3changeset_new, pApply->nCol, pApply->abPK, pApply->pDelete); 4180 sqlite3_bind_int(pApply->pDelete, pApply->nCol+1, 1); 4181 } 4182 if( rc==SQLITE_OK ){ 4183 sqlite3_step(pApply->pDelete); 4184 rc = sqlite3_reset(pApply->pDelete); 4185 } 4186 if( rc==SQLITE_OK ){ 4187 rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, 0, 0); 4188 } 4189 if( rc==SQLITE_OK ){ 4190 rc = sqlite3_exec(db, "RELEASE replace_op", 0, 0, 0); 4191 } 4192 } 4193 } 4194 4195 return rc; 4196 } 4197 4198 /* 4199 ** Retry the changes accumulated in the pApply->constraints buffer. 4200 */ 4201 static int sessionRetryConstraints( 4202 sqlite3 *db, 4203 int bPatchset, 4204 const char *zTab, 4205 SessionApplyCtx *pApply, 4206 int(*xConflict)(void*, int, sqlite3_changeset_iter*), 4207 void *pCtx /* First argument passed to xConflict */ 4208 ){ 4209 int rc = SQLITE_OK; 4210 4211 while( pApply->constraints.nBuf ){ 4212 sqlite3_changeset_iter *pIter2 = 0; 4213 SessionBuffer cons = pApply->constraints; 4214 memset(&pApply->constraints, 0, sizeof(SessionBuffer)); 4215 4216 rc = sessionChangesetStart(&pIter2, 0, 0, cons.nBuf, cons.aBuf, 0); 4217 if( rc==SQLITE_OK ){ 4218 int nByte = 2*pApply->nCol*sizeof(sqlite3_value*); 4219 int rc2; 4220 pIter2->bPatchset = bPatchset; 4221 pIter2->zTab = (char*)zTab; 4222 pIter2->nCol = pApply->nCol; 4223 pIter2->abPK = pApply->abPK; 4224 sessionBufferGrow(&pIter2->tblhdr, nByte, &rc); 4225 pIter2->apValue = (sqlite3_value**)pIter2->tblhdr.aBuf; 4226 if( rc==SQLITE_OK ) memset(pIter2->apValue, 0, nByte); 4227 4228 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3changeset_next(pIter2) ){ 4229 rc = sessionApplyOneWithRetry(db, pIter2, pApply, xConflict, pCtx); 4230 } 4231 4232 rc2 = sqlite3changeset_finalize(pIter2); 4233 if( rc==SQLITE_OK ) rc = rc2; 4234 } 4235 assert( pApply->bDeferConstraints || pApply->constraints.nBuf==0 ); 4236 4237 sqlite3_free(cons.aBuf); 4238 if( rc!=SQLITE_OK ) break; 4239 if( pApply->constraints.nBuf>=cons.nBuf ){ 4240 /* No progress was made on the last round. */ 4241 pApply->bDeferConstraints = 0; 4242 } 4243 } 4244 4245 return rc; 4246 } 4247 4248 /* 4249 ** Argument pIter is a changeset iterator that has been initialized, but 4250 ** not yet passed to sqlite3changeset_next(). This function applies the 4251 ** changeset to the main database attached to handle "db". The supplied 4252 ** conflict handler callback is invoked to resolve any conflicts encountered 4253 ** while applying the change. 4254 */ 4255 static int sessionChangesetApply( 4256 sqlite3 *db, /* Apply change to "main" db of this handle */ 4257 sqlite3_changeset_iter *pIter, /* Changeset to apply */ 4258 int(*xFilter)( 4259 void *pCtx, /* Copy of sixth arg to _apply() */ 4260 const char *zTab /* Table name */ 4261 ), 4262 int(*xConflict)( 4263 void *pCtx, /* Copy of fifth arg to _apply() */ 4264 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */ 4265 sqlite3_changeset_iter *p /* Handle describing change and conflict */ 4266 ), 4267 void *pCtx, /* First argument passed to xConflict */ 4268 void **ppRebase, int *pnRebase, /* OUT: Rebase information */ 4269 int flags /* SESSION_APPLY_XXX flags */ 4270 ){ 4271 int schemaMismatch = 0; 4272 int rc = SQLITE_OK; /* Return code */ 4273 const char *zTab = 0; /* Name of current table */ 4274 int nTab = 0; /* Result of sqlite3Strlen30(zTab) */ 4275 SessionApplyCtx sApply; /* changeset_apply() context object */ 4276 int bPatchset; 4277 4278 assert( xConflict!=0 ); 4279 4280 pIter->in.bNoDiscard = 1; 4281 memset(&sApply, 0, sizeof(sApply)); 4282 sApply.bRebase = (ppRebase && pnRebase); 4283 sqlite3_mutex_enter(sqlite3_db_mutex(db)); 4284 if( (flags & SQLITE_CHANGESETAPPLY_NOSAVEPOINT)==0 ){ 4285 rc = sqlite3_exec(db, "SAVEPOINT changeset_apply", 0, 0, 0); 4286 } 4287 if( rc==SQLITE_OK ){ 4288 rc = sqlite3_exec(db, "PRAGMA defer_foreign_keys = 1", 0, 0, 0); 4289 } 4290 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3changeset_next(pIter) ){ 4291 int nCol; 4292 int op; 4293 const char *zNew; 4294 4295 sqlite3changeset_op(pIter, &zNew, &nCol, &op, 0); 4296 4297 if( zTab==0 || sqlite3_strnicmp(zNew, zTab, nTab+1) ){ 4298 u8 *abPK; 4299 4300 rc = sessionRetryConstraints( 4301 db, pIter->bPatchset, zTab, &sApply, xConflict, pCtx 4302 ); 4303 if( rc!=SQLITE_OK ) break; 4304 4305 sqlite3_free((char*)sApply.azCol); /* cast works around VC++ bug */ 4306 sqlite3_finalize(sApply.pDelete); 4307 sqlite3_finalize(sApply.pUpdate); 4308 sqlite3_finalize(sApply.pInsert); 4309 sqlite3_finalize(sApply.pSelect); 4310 sApply.db = db; 4311 sApply.pDelete = 0; 4312 sApply.pUpdate = 0; 4313 sApply.pInsert = 0; 4314 sApply.pSelect = 0; 4315 sApply.nCol = 0; 4316 sApply.azCol = 0; 4317 sApply.abPK = 0; 4318 sApply.bStat1 = 0; 4319 sApply.bDeferConstraints = 1; 4320 sApply.bRebaseStarted = 0; 4321 memset(&sApply.constraints, 0, sizeof(SessionBuffer)); 4322 4323 /* If an xFilter() callback was specified, invoke it now. If the 4324 ** xFilter callback returns zero, skip this table. If it returns 4325 ** non-zero, proceed. */ 4326 schemaMismatch = (xFilter && (0==xFilter(pCtx, zNew))); 4327 if( schemaMismatch ){ 4328 zTab = sqlite3_mprintf("%s", zNew); 4329 if( zTab==0 ){ 4330 rc = SQLITE_NOMEM; 4331 break; 4332 } 4333 nTab = (int)strlen(zTab); 4334 sApply.azCol = (const char **)zTab; 4335 }else{ 4336 int nMinCol = 0; 4337 int i; 4338 4339 sqlite3changeset_pk(pIter, &abPK, 0); 4340 rc = sessionTableInfo( 4341 db, "main", zNew, &sApply.nCol, &zTab, &sApply.azCol, &sApply.abPK 4342 ); 4343 if( rc!=SQLITE_OK ) break; 4344 for(i=0; i<sApply.nCol; i++){ 4345 if( sApply.abPK[i] ) nMinCol = i+1; 4346 } 4347 4348 if( sApply.nCol==0 ){ 4349 schemaMismatch = 1; 4350 sqlite3_log(SQLITE_SCHEMA, 4351 "sqlite3changeset_apply(): no such table: %s", zTab 4352 ); 4353 } 4354 else if( sApply.nCol<nCol ){ 4355 schemaMismatch = 1; 4356 sqlite3_log(SQLITE_SCHEMA, 4357 "sqlite3changeset_apply(): table %s has %d columns, " 4358 "expected %d or more", 4359 zTab, sApply.nCol, nCol 4360 ); 4361 } 4362 else if( nCol<nMinCol || memcmp(sApply.abPK, abPK, nCol)!=0 ){ 4363 schemaMismatch = 1; 4364 sqlite3_log(SQLITE_SCHEMA, "sqlite3changeset_apply(): " 4365 "primary key mismatch for table %s", zTab 4366 ); 4367 } 4368 else{ 4369 sApply.nCol = nCol; 4370 if( 0==sqlite3_stricmp(zTab, "sqlite_stat1") ){ 4371 if( (rc = sessionStat1Sql(db, &sApply) ) ){ 4372 break; 4373 } 4374 sApply.bStat1 = 1; 4375 }else{ 4376 if((rc = sessionSelectRow(db, zTab, &sApply)) 4377 || (rc = sessionUpdateRow(db, zTab, &sApply)) 4378 || (rc = sessionDeleteRow(db, zTab, &sApply)) 4379 || (rc = sessionInsertRow(db, zTab, &sApply)) 4380 ){ 4381 break; 4382 } 4383 sApply.bStat1 = 0; 4384 } 4385 } 4386 nTab = sqlite3Strlen30(zTab); 4387 } 4388 } 4389 4390 /* If there is a schema mismatch on the current table, proceed to the 4391 ** next change. A log message has already been issued. */ 4392 if( schemaMismatch ) continue; 4393 4394 rc = sessionApplyOneWithRetry(db, pIter, &sApply, xConflict, pCtx); 4395 } 4396 4397 bPatchset = pIter->bPatchset; 4398 if( rc==SQLITE_OK ){ 4399 rc = sqlite3changeset_finalize(pIter); 4400 }else{ 4401 sqlite3changeset_finalize(pIter); 4402 } 4403 4404 if( rc==SQLITE_OK ){ 4405 rc = sessionRetryConstraints(db, bPatchset, zTab, &sApply, xConflict, pCtx); 4406 } 4407 4408 if( rc==SQLITE_OK ){ 4409 int nFk, notUsed; 4410 sqlite3_db_status(db, SQLITE_DBSTATUS_DEFERRED_FKS, &nFk, ¬Used, 0); 4411 if( nFk!=0 ){ 4412 int res = SQLITE_CHANGESET_ABORT; 4413 sqlite3_changeset_iter sIter; 4414 memset(&sIter, 0, sizeof(sIter)); 4415 sIter.nCol = nFk; 4416 res = xConflict(pCtx, SQLITE_CHANGESET_FOREIGN_KEY, &sIter); 4417 if( res!=SQLITE_CHANGESET_OMIT ){ 4418 rc = SQLITE_CONSTRAINT; 4419 } 4420 } 4421 } 4422 sqlite3_exec(db, "PRAGMA defer_foreign_keys = 0", 0, 0, 0); 4423 4424 if( (flags & SQLITE_CHANGESETAPPLY_NOSAVEPOINT)==0 ){ 4425 if( rc==SQLITE_OK ){ 4426 rc = sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0); 4427 }else{ 4428 sqlite3_exec(db, "ROLLBACK TO changeset_apply", 0, 0, 0); 4429 sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0); 4430 } 4431 } 4432 4433 assert( sApply.bRebase || sApply.rebase.nBuf==0 ); 4434 if( rc==SQLITE_OK && bPatchset==0 && sApply.bRebase ){ 4435 *ppRebase = (void*)sApply.rebase.aBuf; 4436 *pnRebase = sApply.rebase.nBuf; 4437 sApply.rebase.aBuf = 0; 4438 } 4439 sqlite3_finalize(sApply.pInsert); 4440 sqlite3_finalize(sApply.pDelete); 4441 sqlite3_finalize(sApply.pUpdate); 4442 sqlite3_finalize(sApply.pSelect); 4443 sqlite3_free((char*)sApply.azCol); /* cast works around VC++ bug */ 4444 sqlite3_free((char*)sApply.constraints.aBuf); 4445 sqlite3_free((char*)sApply.rebase.aBuf); 4446 sqlite3_mutex_leave(sqlite3_db_mutex(db)); 4447 return rc; 4448 } 4449 4450 /* 4451 ** Apply the changeset passed via pChangeset/nChangeset to the main 4452 ** database attached to handle "db". 4453 */ 4454 int sqlite3changeset_apply_v2( 4455 sqlite3 *db, /* Apply change to "main" db of this handle */ 4456 int nChangeset, /* Size of changeset in bytes */ 4457 void *pChangeset, /* Changeset blob */ 4458 int(*xFilter)( 4459 void *pCtx, /* Copy of sixth arg to _apply() */ 4460 const char *zTab /* Table name */ 4461 ), 4462 int(*xConflict)( 4463 void *pCtx, /* Copy of sixth arg to _apply() */ 4464 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */ 4465 sqlite3_changeset_iter *p /* Handle describing change and conflict */ 4466 ), 4467 void *pCtx, /* First argument passed to xConflict */ 4468 void **ppRebase, int *pnRebase, 4469 int flags 4470 ){ 4471 sqlite3_changeset_iter *pIter; /* Iterator to skip through changeset */ 4472 int bInverse = !!(flags & SQLITE_CHANGESETAPPLY_INVERT); 4473 int rc = sessionChangesetStart(&pIter, 0, 0, nChangeset, pChangeset,bInverse); 4474 if( rc==SQLITE_OK ){ 4475 rc = sessionChangesetApply( 4476 db, pIter, xFilter, xConflict, pCtx, ppRebase, pnRebase, flags 4477 ); 4478 } 4479 return rc; 4480 } 4481 4482 /* 4483 ** Apply the changeset passed via pChangeset/nChangeset to the main database 4484 ** attached to handle "db". Invoke the supplied conflict handler callback 4485 ** to resolve any conflicts encountered while applying the change. 4486 */ 4487 int sqlite3changeset_apply( 4488 sqlite3 *db, /* Apply change to "main" db of this handle */ 4489 int nChangeset, /* Size of changeset in bytes */ 4490 void *pChangeset, /* Changeset blob */ 4491 int(*xFilter)( 4492 void *pCtx, /* Copy of sixth arg to _apply() */ 4493 const char *zTab /* Table name */ 4494 ), 4495 int(*xConflict)( 4496 void *pCtx, /* Copy of fifth arg to _apply() */ 4497 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */ 4498 sqlite3_changeset_iter *p /* Handle describing change and conflict */ 4499 ), 4500 void *pCtx /* First argument passed to xConflict */ 4501 ){ 4502 return sqlite3changeset_apply_v2( 4503 db, nChangeset, pChangeset, xFilter, xConflict, pCtx, 0, 0, 0 4504 ); 4505 } 4506 4507 /* 4508 ** Apply the changeset passed via xInput/pIn to the main database 4509 ** attached to handle "db". Invoke the supplied conflict handler callback 4510 ** to resolve any conflicts encountered while applying the change. 4511 */ 4512 int sqlite3changeset_apply_v2_strm( 4513 sqlite3 *db, /* Apply change to "main" db of this handle */ 4514 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */ 4515 void *pIn, /* First arg for xInput */ 4516 int(*xFilter)( 4517 void *pCtx, /* Copy of sixth arg to _apply() */ 4518 const char *zTab /* Table name */ 4519 ), 4520 int(*xConflict)( 4521 void *pCtx, /* Copy of sixth arg to _apply() */ 4522 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */ 4523 sqlite3_changeset_iter *p /* Handle describing change and conflict */ 4524 ), 4525 void *pCtx, /* First argument passed to xConflict */ 4526 void **ppRebase, int *pnRebase, 4527 int flags 4528 ){ 4529 sqlite3_changeset_iter *pIter; /* Iterator to skip through changeset */ 4530 int bInverse = !!(flags & SQLITE_CHANGESETAPPLY_INVERT); 4531 int rc = sessionChangesetStart(&pIter, xInput, pIn, 0, 0, bInverse); 4532 if( rc==SQLITE_OK ){ 4533 rc = sessionChangesetApply( 4534 db, pIter, xFilter, xConflict, pCtx, ppRebase, pnRebase, flags 4535 ); 4536 } 4537 return rc; 4538 } 4539 int sqlite3changeset_apply_strm( 4540 sqlite3 *db, /* Apply change to "main" db of this handle */ 4541 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */ 4542 void *pIn, /* First arg for xInput */ 4543 int(*xFilter)( 4544 void *pCtx, /* Copy of sixth arg to _apply() */ 4545 const char *zTab /* Table name */ 4546 ), 4547 int(*xConflict)( 4548 void *pCtx, /* Copy of sixth arg to _apply() */ 4549 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */ 4550 sqlite3_changeset_iter *p /* Handle describing change and conflict */ 4551 ), 4552 void *pCtx /* First argument passed to xConflict */ 4553 ){ 4554 return sqlite3changeset_apply_v2_strm( 4555 db, xInput, pIn, xFilter, xConflict, pCtx, 0, 0, 0 4556 ); 4557 } 4558 4559 /* 4560 ** sqlite3_changegroup handle. 4561 */ 4562 struct sqlite3_changegroup { 4563 int rc; /* Error code */ 4564 int bPatch; /* True to accumulate patchsets */ 4565 SessionTable *pList; /* List of tables in current patch */ 4566 }; 4567 4568 /* 4569 ** This function is called to merge two changes to the same row together as 4570 ** part of an sqlite3changeset_concat() operation. A new change object is 4571 ** allocated and a pointer to it stored in *ppNew. 4572 */ 4573 static int sessionChangeMerge( 4574 SessionTable *pTab, /* Table structure */ 4575 int bRebase, /* True for a rebase hash-table */ 4576 int bPatchset, /* True for patchsets */ 4577 SessionChange *pExist, /* Existing change */ 4578 int op2, /* Second change operation */ 4579 int bIndirect, /* True if second change is indirect */ 4580 u8 *aRec, /* Second change record */ 4581 int nRec, /* Number of bytes in aRec */ 4582 SessionChange **ppNew /* OUT: Merged change */ 4583 ){ 4584 SessionChange *pNew = 0; 4585 int rc = SQLITE_OK; 4586 4587 if( !pExist ){ 4588 pNew = (SessionChange *)sqlite3_malloc(sizeof(SessionChange) + nRec); 4589 if( !pNew ){ 4590 return SQLITE_NOMEM; 4591 } 4592 memset(pNew, 0, sizeof(SessionChange)); 4593 pNew->op = op2; 4594 pNew->bIndirect = bIndirect; 4595 pNew->aRecord = (u8*)&pNew[1]; 4596 if( bIndirect==0 || bRebase==0 ){ 4597 pNew->nRecord = nRec; 4598 memcpy(pNew->aRecord, aRec, nRec); 4599 }else{ 4600 int i; 4601 u8 *pIn = aRec; 4602 u8 *pOut = pNew->aRecord; 4603 for(i=0; i<pTab->nCol; i++){ 4604 int nIn = sessionSerialLen(pIn); 4605 if( *pIn==0 ){ 4606 *pOut++ = 0; 4607 }else if( pTab->abPK[i]==0 ){ 4608 *pOut++ = 0xFF; 4609 }else{ 4610 memcpy(pOut, pIn, nIn); 4611 pOut += nIn; 4612 } 4613 pIn += nIn; 4614 } 4615 pNew->nRecord = pOut - pNew->aRecord; 4616 } 4617 }else if( bRebase ){ 4618 if( pExist->op==SQLITE_DELETE && pExist->bIndirect ){ 4619 *ppNew = pExist; 4620 }else{ 4621 int nByte = nRec + pExist->nRecord + sizeof(SessionChange); 4622 pNew = (SessionChange*)sqlite3_malloc(nByte); 4623 if( pNew==0 ){ 4624 rc = SQLITE_NOMEM; 4625 }else{ 4626 int i; 4627 u8 *a1 = pExist->aRecord; 4628 u8 *a2 = aRec; 4629 u8 *pOut; 4630 4631 memset(pNew, 0, nByte); 4632 pNew->bIndirect = bIndirect || pExist->bIndirect; 4633 pNew->op = op2; 4634 pOut = pNew->aRecord = (u8*)&pNew[1]; 4635 4636 for(i=0; i<pTab->nCol; i++){ 4637 int n1 = sessionSerialLen(a1); 4638 int n2 = sessionSerialLen(a2); 4639 if( *a1==0xFF || (pTab->abPK[i]==0 && bIndirect) ){ 4640 *pOut++ = 0xFF; 4641 }else if( *a2==0 ){ 4642 memcpy(pOut, a1, n1); 4643 pOut += n1; 4644 }else{ 4645 memcpy(pOut, a2, n2); 4646 pOut += n2; 4647 } 4648 a1 += n1; 4649 a2 += n2; 4650 } 4651 pNew->nRecord = pOut - pNew->aRecord; 4652 } 4653 sqlite3_free(pExist); 4654 } 4655 }else{ 4656 int op1 = pExist->op; 4657 4658 /* 4659 ** op1=INSERT, op2=INSERT -> Unsupported. Discard op2. 4660 ** op1=INSERT, op2=UPDATE -> INSERT. 4661 ** op1=INSERT, op2=DELETE -> (none) 4662 ** 4663 ** op1=UPDATE, op2=INSERT -> Unsupported. Discard op2. 4664 ** op1=UPDATE, op2=UPDATE -> UPDATE. 4665 ** op1=UPDATE, op2=DELETE -> DELETE. 4666 ** 4667 ** op1=DELETE, op2=INSERT -> UPDATE. 4668 ** op1=DELETE, op2=UPDATE -> Unsupported. Discard op2. 4669 ** op1=DELETE, op2=DELETE -> Unsupported. Discard op2. 4670 */ 4671 if( (op1==SQLITE_INSERT && op2==SQLITE_INSERT) 4672 || (op1==SQLITE_UPDATE && op2==SQLITE_INSERT) 4673 || (op1==SQLITE_DELETE && op2==SQLITE_UPDATE) 4674 || (op1==SQLITE_DELETE && op2==SQLITE_DELETE) 4675 ){ 4676 pNew = pExist; 4677 }else if( op1==SQLITE_INSERT && op2==SQLITE_DELETE ){ 4678 sqlite3_free(pExist); 4679 assert( pNew==0 ); 4680 }else{ 4681 u8 *aExist = pExist->aRecord; 4682 int nByte; 4683 u8 *aCsr; 4684 4685 /* Allocate a new SessionChange object. Ensure that the aRecord[] 4686 ** buffer of the new object is large enough to hold any record that 4687 ** may be generated by combining the input records. */ 4688 nByte = sizeof(SessionChange) + pExist->nRecord + nRec; 4689 pNew = (SessionChange *)sqlite3_malloc(nByte); 4690 if( !pNew ){ 4691 sqlite3_free(pExist); 4692 return SQLITE_NOMEM; 4693 } 4694 memset(pNew, 0, sizeof(SessionChange)); 4695 pNew->bIndirect = (bIndirect && pExist->bIndirect); 4696 aCsr = pNew->aRecord = (u8 *)&pNew[1]; 4697 4698 if( op1==SQLITE_INSERT ){ /* INSERT + UPDATE */ 4699 u8 *a1 = aRec; 4700 assert( op2==SQLITE_UPDATE ); 4701 pNew->op = SQLITE_INSERT; 4702 if( bPatchset==0 ) sessionSkipRecord(&a1, pTab->nCol); 4703 sessionMergeRecord(&aCsr, pTab->nCol, aExist, a1); 4704 }else if( op1==SQLITE_DELETE ){ /* DELETE + INSERT */ 4705 assert( op2==SQLITE_INSERT ); 4706 pNew->op = SQLITE_UPDATE; 4707 if( bPatchset ){ 4708 memcpy(aCsr, aRec, nRec); 4709 aCsr += nRec; 4710 }else{ 4711 if( 0==sessionMergeUpdate(&aCsr, pTab, bPatchset, aExist, 0,aRec,0) ){ 4712 sqlite3_free(pNew); 4713 pNew = 0; 4714 } 4715 } 4716 }else if( op2==SQLITE_UPDATE ){ /* UPDATE + UPDATE */ 4717 u8 *a1 = aExist; 4718 u8 *a2 = aRec; 4719 assert( op1==SQLITE_UPDATE ); 4720 if( bPatchset==0 ){ 4721 sessionSkipRecord(&a1, pTab->nCol); 4722 sessionSkipRecord(&a2, pTab->nCol); 4723 } 4724 pNew->op = SQLITE_UPDATE; 4725 if( 0==sessionMergeUpdate(&aCsr, pTab, bPatchset, aRec, aExist,a1,a2) ){ 4726 sqlite3_free(pNew); 4727 pNew = 0; 4728 } 4729 }else{ /* UPDATE + DELETE */ 4730 assert( op1==SQLITE_UPDATE && op2==SQLITE_DELETE ); 4731 pNew->op = SQLITE_DELETE; 4732 if( bPatchset ){ 4733 memcpy(aCsr, aRec, nRec); 4734 aCsr += nRec; 4735 }else{ 4736 sessionMergeRecord(&aCsr, pTab->nCol, aRec, aExist); 4737 } 4738 } 4739 4740 if( pNew ){ 4741 pNew->nRecord = (int)(aCsr - pNew->aRecord); 4742 } 4743 sqlite3_free(pExist); 4744 } 4745 } 4746 4747 *ppNew = pNew; 4748 return rc; 4749 } 4750 4751 /* 4752 ** Add all changes in the changeset traversed by the iterator passed as 4753 ** the first argument to the changegroup hash tables. 4754 */ 4755 static int sessionChangesetToHash( 4756 sqlite3_changeset_iter *pIter, /* Iterator to read from */ 4757 sqlite3_changegroup *pGrp, /* Changegroup object to add changeset to */ 4758 int bRebase /* True if hash table is for rebasing */ 4759 ){ 4760 u8 *aRec; 4761 int nRec; 4762 int rc = SQLITE_OK; 4763 SessionTable *pTab = 0; 4764 4765 while( SQLITE_ROW==sessionChangesetNext(pIter, &aRec, &nRec, 0) ){ 4766 const char *zNew; 4767 int nCol; 4768 int op; 4769 int iHash; 4770 int bIndirect; 4771 SessionChange *pChange; 4772 SessionChange *pExist = 0; 4773 SessionChange **pp; 4774 4775 if( pGrp->pList==0 ){ 4776 pGrp->bPatch = pIter->bPatchset; 4777 }else if( pIter->bPatchset!=pGrp->bPatch ){ 4778 rc = SQLITE_ERROR; 4779 break; 4780 } 4781 4782 sqlite3changeset_op(pIter, &zNew, &nCol, &op, &bIndirect); 4783 if( !pTab || sqlite3_stricmp(zNew, pTab->zName) ){ 4784 /* Search the list for a matching table */ 4785 int nNew = (int)strlen(zNew); 4786 u8 *abPK; 4787 4788 sqlite3changeset_pk(pIter, &abPK, 0); 4789 for(pTab = pGrp->pList; pTab; pTab=pTab->pNext){ 4790 if( 0==sqlite3_strnicmp(pTab->zName, zNew, nNew+1) ) break; 4791 } 4792 if( !pTab ){ 4793 SessionTable **ppTab; 4794 4795 pTab = sqlite3_malloc(sizeof(SessionTable) + nCol + nNew+1); 4796 if( !pTab ){ 4797 rc = SQLITE_NOMEM; 4798 break; 4799 } 4800 memset(pTab, 0, sizeof(SessionTable)); 4801 pTab->nCol = nCol; 4802 pTab->abPK = (u8*)&pTab[1]; 4803 memcpy(pTab->abPK, abPK, nCol); 4804 pTab->zName = (char*)&pTab->abPK[nCol]; 4805 memcpy(pTab->zName, zNew, nNew+1); 4806 4807 /* The new object must be linked on to the end of the list, not 4808 ** simply added to the start of it. This is to ensure that the 4809 ** tables within the output of sqlite3changegroup_output() are in 4810 ** the right order. */ 4811 for(ppTab=&pGrp->pList; *ppTab; ppTab=&(*ppTab)->pNext); 4812 *ppTab = pTab; 4813 }else if( pTab->nCol!=nCol || memcmp(pTab->abPK, abPK, nCol) ){ 4814 rc = SQLITE_SCHEMA; 4815 break; 4816 } 4817 } 4818 4819 if( sessionGrowHash(pIter->bPatchset, pTab) ){ 4820 rc = SQLITE_NOMEM; 4821 break; 4822 } 4823 iHash = sessionChangeHash( 4824 pTab, (pIter->bPatchset && op==SQLITE_DELETE), aRec, pTab->nChange 4825 ); 4826 4827 /* Search for existing entry. If found, remove it from the hash table. 4828 ** Code below may link it back in. 4829 */ 4830 for(pp=&pTab->apChange[iHash]; *pp; pp=&(*pp)->pNext){ 4831 int bPkOnly1 = 0; 4832 int bPkOnly2 = 0; 4833 if( pIter->bPatchset ){ 4834 bPkOnly1 = (*pp)->op==SQLITE_DELETE; 4835 bPkOnly2 = op==SQLITE_DELETE; 4836 } 4837 if( sessionChangeEqual(pTab, bPkOnly1, (*pp)->aRecord, bPkOnly2, aRec) ){ 4838 pExist = *pp; 4839 *pp = (*pp)->pNext; 4840 pTab->nEntry--; 4841 break; 4842 } 4843 } 4844 4845 rc = sessionChangeMerge(pTab, bRebase, 4846 pIter->bPatchset, pExist, op, bIndirect, aRec, nRec, &pChange 4847 ); 4848 if( rc ) break; 4849 if( pChange ){ 4850 pChange->pNext = pTab->apChange[iHash]; 4851 pTab->apChange[iHash] = pChange; 4852 pTab->nEntry++; 4853 } 4854 } 4855 4856 if( rc==SQLITE_OK ) rc = pIter->rc; 4857 return rc; 4858 } 4859 4860 /* 4861 ** Serialize a changeset (or patchset) based on all changesets (or patchsets) 4862 ** added to the changegroup object passed as the first argument. 4863 ** 4864 ** If xOutput is not NULL, then the changeset/patchset is returned to the 4865 ** user via one or more calls to xOutput, as with the other streaming 4866 ** interfaces. 4867 ** 4868 ** Or, if xOutput is NULL, then (*ppOut) is populated with a pointer to a 4869 ** buffer containing the output changeset before this function returns. In 4870 ** this case (*pnOut) is set to the size of the output buffer in bytes. It 4871 ** is the responsibility of the caller to free the output buffer using 4872 ** sqlite3_free() when it is no longer required. 4873 ** 4874 ** If successful, SQLITE_OK is returned. Or, if an error occurs, an SQLite 4875 ** error code. If an error occurs and xOutput is NULL, (*ppOut) and (*pnOut) 4876 ** are both set to 0 before returning. 4877 */ 4878 static int sessionChangegroupOutput( 4879 sqlite3_changegroup *pGrp, 4880 int (*xOutput)(void *pOut, const void *pData, int nData), 4881 void *pOut, 4882 int *pnOut, 4883 void **ppOut 4884 ){ 4885 int rc = SQLITE_OK; 4886 SessionBuffer buf = {0, 0, 0}; 4887 SessionTable *pTab; 4888 assert( xOutput==0 || (ppOut==0 && pnOut==0) ); 4889 4890 /* Create the serialized output changeset based on the contents of the 4891 ** hash tables attached to the SessionTable objects in list p->pList. 4892 */ 4893 for(pTab=pGrp->pList; rc==SQLITE_OK && pTab; pTab=pTab->pNext){ 4894 int i; 4895 if( pTab->nEntry==0 ) continue; 4896 4897 sessionAppendTableHdr(&buf, pGrp->bPatch, pTab, &rc); 4898 for(i=0; i<pTab->nChange; i++){ 4899 SessionChange *p; 4900 for(p=pTab->apChange[i]; p; p=p->pNext){ 4901 sessionAppendByte(&buf, p->op, &rc); 4902 sessionAppendByte(&buf, p->bIndirect, &rc); 4903 sessionAppendBlob(&buf, p->aRecord, p->nRecord, &rc); 4904 if( rc==SQLITE_OK && xOutput && buf.nBuf>=sessions_strm_chunk_size ){ 4905 rc = xOutput(pOut, buf.aBuf, buf.nBuf); 4906 buf.nBuf = 0; 4907 } 4908 } 4909 } 4910 } 4911 4912 if( rc==SQLITE_OK ){ 4913 if( xOutput ){ 4914 if( buf.nBuf>0 ) rc = xOutput(pOut, buf.aBuf, buf.nBuf); 4915 }else{ 4916 *ppOut = buf.aBuf; 4917 *pnOut = buf.nBuf; 4918 buf.aBuf = 0; 4919 } 4920 } 4921 sqlite3_free(buf.aBuf); 4922 4923 return rc; 4924 } 4925 4926 /* 4927 ** Allocate a new, empty, sqlite3_changegroup. 4928 */ 4929 int sqlite3changegroup_new(sqlite3_changegroup **pp){ 4930 int rc = SQLITE_OK; /* Return code */ 4931 sqlite3_changegroup *p; /* New object */ 4932 p = (sqlite3_changegroup*)sqlite3_malloc(sizeof(sqlite3_changegroup)); 4933 if( p==0 ){ 4934 rc = SQLITE_NOMEM; 4935 }else{ 4936 memset(p, 0, sizeof(sqlite3_changegroup)); 4937 } 4938 *pp = p; 4939 return rc; 4940 } 4941 4942 /* 4943 ** Add the changeset currently stored in buffer pData, size nData bytes, 4944 ** to changeset-group p. 4945 */ 4946 int sqlite3changegroup_add(sqlite3_changegroup *pGrp, int nData, void *pData){ 4947 sqlite3_changeset_iter *pIter; /* Iterator opened on pData/nData */ 4948 int rc; /* Return code */ 4949 4950 rc = sqlite3changeset_start(&pIter, nData, pData); 4951 if( rc==SQLITE_OK ){ 4952 rc = sessionChangesetToHash(pIter, pGrp, 0); 4953 } 4954 sqlite3changeset_finalize(pIter); 4955 return rc; 4956 } 4957 4958 /* 4959 ** Obtain a buffer containing a changeset representing the concatenation 4960 ** of all changesets added to the group so far. 4961 */ 4962 int sqlite3changegroup_output( 4963 sqlite3_changegroup *pGrp, 4964 int *pnData, 4965 void **ppData 4966 ){ 4967 return sessionChangegroupOutput(pGrp, 0, 0, pnData, ppData); 4968 } 4969 4970 /* 4971 ** Streaming versions of changegroup_add(). 4972 */ 4973 int sqlite3changegroup_add_strm( 4974 sqlite3_changegroup *pGrp, 4975 int (*xInput)(void *pIn, void *pData, int *pnData), 4976 void *pIn 4977 ){ 4978 sqlite3_changeset_iter *pIter; /* Iterator opened on pData/nData */ 4979 int rc; /* Return code */ 4980 4981 rc = sqlite3changeset_start_strm(&pIter, xInput, pIn); 4982 if( rc==SQLITE_OK ){ 4983 rc = sessionChangesetToHash(pIter, pGrp, 0); 4984 } 4985 sqlite3changeset_finalize(pIter); 4986 return rc; 4987 } 4988 4989 /* 4990 ** Streaming versions of changegroup_output(). 4991 */ 4992 int sqlite3changegroup_output_strm( 4993 sqlite3_changegroup *pGrp, 4994 int (*xOutput)(void *pOut, const void *pData, int nData), 4995 void *pOut 4996 ){ 4997 return sessionChangegroupOutput(pGrp, xOutput, pOut, 0, 0); 4998 } 4999 5000 /* 5001 ** Delete a changegroup object. 5002 */ 5003 void sqlite3changegroup_delete(sqlite3_changegroup *pGrp){ 5004 if( pGrp ){ 5005 sessionDeleteTable(pGrp->pList); 5006 sqlite3_free(pGrp); 5007 } 5008 } 5009 5010 /* 5011 ** Combine two changesets together. 5012 */ 5013 int sqlite3changeset_concat( 5014 int nLeft, /* Number of bytes in lhs input */ 5015 void *pLeft, /* Lhs input changeset */ 5016 int nRight /* Number of bytes in rhs input */, 5017 void *pRight, /* Rhs input changeset */ 5018 int *pnOut, /* OUT: Number of bytes in output changeset */ 5019 void **ppOut /* OUT: changeset (left <concat> right) */ 5020 ){ 5021 sqlite3_changegroup *pGrp; 5022 int rc; 5023 5024 rc = sqlite3changegroup_new(&pGrp); 5025 if( rc==SQLITE_OK ){ 5026 rc = sqlite3changegroup_add(pGrp, nLeft, pLeft); 5027 } 5028 if( rc==SQLITE_OK ){ 5029 rc = sqlite3changegroup_add(pGrp, nRight, pRight); 5030 } 5031 if( rc==SQLITE_OK ){ 5032 rc = sqlite3changegroup_output(pGrp, pnOut, ppOut); 5033 } 5034 sqlite3changegroup_delete(pGrp); 5035 5036 return rc; 5037 } 5038 5039 /* 5040 ** Streaming version of sqlite3changeset_concat(). 5041 */ 5042 int sqlite3changeset_concat_strm( 5043 int (*xInputA)(void *pIn, void *pData, int *pnData), 5044 void *pInA, 5045 int (*xInputB)(void *pIn, void *pData, int *pnData), 5046 void *pInB, 5047 int (*xOutput)(void *pOut, const void *pData, int nData), 5048 void *pOut 5049 ){ 5050 sqlite3_changegroup *pGrp; 5051 int rc; 5052 5053 rc = sqlite3changegroup_new(&pGrp); 5054 if( rc==SQLITE_OK ){ 5055 rc = sqlite3changegroup_add_strm(pGrp, xInputA, pInA); 5056 } 5057 if( rc==SQLITE_OK ){ 5058 rc = sqlite3changegroup_add_strm(pGrp, xInputB, pInB); 5059 } 5060 if( rc==SQLITE_OK ){ 5061 rc = sqlite3changegroup_output_strm(pGrp, xOutput, pOut); 5062 } 5063 sqlite3changegroup_delete(pGrp); 5064 5065 return rc; 5066 } 5067 5068 /* 5069 ** Changeset rebaser handle. 5070 */ 5071 struct sqlite3_rebaser { 5072 sqlite3_changegroup grp; /* Hash table */ 5073 }; 5074 5075 /* 5076 ** Buffers a1 and a2 must both contain a sessions module record nCol 5077 ** fields in size. This function appends an nCol sessions module 5078 ** record to buffer pBuf that is a copy of a1, except that for 5079 ** each field that is undefined in a1[], swap in the field from a2[]. 5080 */ 5081 static void sessionAppendRecordMerge( 5082 SessionBuffer *pBuf, /* Buffer to append to */ 5083 int nCol, /* Number of columns in each record */ 5084 u8 *a1, int n1, /* Record 1 */ 5085 u8 *a2, int n2, /* Record 2 */ 5086 int *pRc /* IN/OUT: error code */ 5087 ){ 5088 sessionBufferGrow(pBuf, n1+n2, pRc); 5089 if( *pRc==SQLITE_OK ){ 5090 int i; 5091 u8 *pOut = &pBuf->aBuf[pBuf->nBuf]; 5092 for(i=0; i<nCol; i++){ 5093 int nn1 = sessionSerialLen(a1); 5094 int nn2 = sessionSerialLen(a2); 5095 if( *a1==0 || *a1==0xFF ){ 5096 memcpy(pOut, a2, nn2); 5097 pOut += nn2; 5098 }else{ 5099 memcpy(pOut, a1, nn1); 5100 pOut += nn1; 5101 } 5102 a1 += nn1; 5103 a2 += nn2; 5104 } 5105 5106 pBuf->nBuf = pOut-pBuf->aBuf; 5107 assert( pBuf->nBuf<=pBuf->nAlloc ); 5108 } 5109 } 5110 5111 /* 5112 ** This function is called when rebasing a local UPDATE change against one 5113 ** or more remote UPDATE changes. The aRec/nRec buffer contains the current 5114 ** old.* and new.* records for the change. The rebase buffer (a single 5115 ** record) is in aChange/nChange. The rebased change is appended to buffer 5116 ** pBuf. 5117 ** 5118 ** Rebasing the UPDATE involves: 5119 ** 5120 ** * Removing any changes to fields for which the corresponding field 5121 ** in the rebase buffer is set to "replaced" (type 0xFF). If this 5122 ** means the UPDATE change updates no fields, nothing is appended 5123 ** to the output buffer. 5124 ** 5125 ** * For each field modified by the local change for which the 5126 ** corresponding field in the rebase buffer is not "undefined" (0x00) 5127 ** or "replaced" (0xFF), the old.* value is replaced by the value 5128 ** in the rebase buffer. 5129 */ 5130 static void sessionAppendPartialUpdate( 5131 SessionBuffer *pBuf, /* Append record here */ 5132 sqlite3_changeset_iter *pIter, /* Iterator pointed at local change */ 5133 u8 *aRec, int nRec, /* Local change */ 5134 u8 *aChange, int nChange, /* Record to rebase against */ 5135 int *pRc /* IN/OUT: Return Code */ 5136 ){ 5137 sessionBufferGrow(pBuf, 2+nRec+nChange, pRc); 5138 if( *pRc==SQLITE_OK ){ 5139 int bData = 0; 5140 u8 *pOut = &pBuf->aBuf[pBuf->nBuf]; 5141 int i; 5142 u8 *a1 = aRec; 5143 u8 *a2 = aChange; 5144 5145 *pOut++ = SQLITE_UPDATE; 5146 *pOut++ = pIter->bIndirect; 5147 for(i=0; i<pIter->nCol; i++){ 5148 int n1 = sessionSerialLen(a1); 5149 int n2 = sessionSerialLen(a2); 5150 if( pIter->abPK[i] || a2[0]==0 ){ 5151 if( !pIter->abPK[i] ) bData = 1; 5152 memcpy(pOut, a1, n1); 5153 pOut += n1; 5154 }else if( a2[0]!=0xFF ){ 5155 bData = 1; 5156 memcpy(pOut, a2, n2); 5157 pOut += n2; 5158 }else{ 5159 *pOut++ = '\0'; 5160 } 5161 a1 += n1; 5162 a2 += n2; 5163 } 5164 if( bData ){ 5165 a2 = aChange; 5166 for(i=0; i<pIter->nCol; i++){ 5167 int n1 = sessionSerialLen(a1); 5168 int n2 = sessionSerialLen(a2); 5169 if( pIter->abPK[i] || a2[0]!=0xFF ){ 5170 memcpy(pOut, a1, n1); 5171 pOut += n1; 5172 }else{ 5173 *pOut++ = '\0'; 5174 } 5175 a1 += n1; 5176 a2 += n2; 5177 } 5178 pBuf->nBuf = (pOut - pBuf->aBuf); 5179 } 5180 } 5181 } 5182 5183 /* 5184 ** pIter is configured to iterate through a changeset. This function rebases 5185 ** that changeset according to the current configuration of the rebaser 5186 ** object passed as the first argument. If no error occurs and argument xOutput 5187 ** is not NULL, then the changeset is returned to the caller by invoking 5188 ** xOutput zero or more times and SQLITE_OK returned. Or, if xOutput is NULL, 5189 ** then (*ppOut) is set to point to a buffer containing the rebased changeset 5190 ** before this function returns. In this case (*pnOut) is set to the size of 5191 ** the buffer in bytes. It is the responsibility of the caller to eventually 5192 ** free the (*ppOut) buffer using sqlite3_free(). 5193 ** 5194 ** If an error occurs, an SQLite error code is returned. If ppOut and 5195 ** pnOut are not NULL, then the two output parameters are set to 0 before 5196 ** returning. 5197 */ 5198 static int sessionRebase( 5199 sqlite3_rebaser *p, /* Rebaser hash table */ 5200 sqlite3_changeset_iter *pIter, /* Input data */ 5201 int (*xOutput)(void *pOut, const void *pData, int nData), 5202 void *pOut, /* Context for xOutput callback */ 5203 int *pnOut, /* OUT: Number of bytes in output changeset */ 5204 void **ppOut /* OUT: Inverse of pChangeset */ 5205 ){ 5206 int rc = SQLITE_OK; 5207 u8 *aRec = 0; 5208 int nRec = 0; 5209 int bNew = 0; 5210 SessionTable *pTab = 0; 5211 SessionBuffer sOut = {0,0,0}; 5212 5213 while( SQLITE_ROW==sessionChangesetNext(pIter, &aRec, &nRec, &bNew) ){ 5214 SessionChange *pChange = 0; 5215 int bDone = 0; 5216 5217 if( bNew ){ 5218 const char *zTab = pIter->zTab; 5219 for(pTab=p->grp.pList; pTab; pTab=pTab->pNext){ 5220 if( 0==sqlite3_stricmp(pTab->zName, zTab) ) break; 5221 } 5222 bNew = 0; 5223 5224 /* A patchset may not be rebased */ 5225 if( pIter->bPatchset ){ 5226 rc = SQLITE_ERROR; 5227 } 5228 5229 /* Append a table header to the output for this new table */ 5230 sessionAppendByte(&sOut, pIter->bPatchset ? 'P' : 'T', &rc); 5231 sessionAppendVarint(&sOut, pIter->nCol, &rc); 5232 sessionAppendBlob(&sOut, pIter->abPK, pIter->nCol, &rc); 5233 sessionAppendBlob(&sOut,(u8*)pIter->zTab,(int)strlen(pIter->zTab)+1,&rc); 5234 } 5235 5236 if( pTab && rc==SQLITE_OK ){ 5237 int iHash = sessionChangeHash(pTab, 0, aRec, pTab->nChange); 5238 5239 for(pChange=pTab->apChange[iHash]; pChange; pChange=pChange->pNext){ 5240 if( sessionChangeEqual(pTab, 0, aRec, 0, pChange->aRecord) ){ 5241 break; 5242 } 5243 } 5244 } 5245 5246 if( pChange ){ 5247 assert( pChange->op==SQLITE_DELETE || pChange->op==SQLITE_INSERT ); 5248 switch( pIter->op ){ 5249 case SQLITE_INSERT: 5250 if( pChange->op==SQLITE_INSERT ){ 5251 bDone = 1; 5252 if( pChange->bIndirect==0 ){ 5253 sessionAppendByte(&sOut, SQLITE_UPDATE, &rc); 5254 sessionAppendByte(&sOut, pIter->bIndirect, &rc); 5255 sessionAppendBlob(&sOut, pChange->aRecord, pChange->nRecord, &rc); 5256 sessionAppendBlob(&sOut, aRec, nRec, &rc); 5257 } 5258 } 5259 break; 5260 5261 case SQLITE_UPDATE: 5262 bDone = 1; 5263 if( pChange->op==SQLITE_DELETE ){ 5264 if( pChange->bIndirect==0 ){ 5265 u8 *pCsr = aRec; 5266 sessionSkipRecord(&pCsr, pIter->nCol); 5267 sessionAppendByte(&sOut, SQLITE_INSERT, &rc); 5268 sessionAppendByte(&sOut, pIter->bIndirect, &rc); 5269 sessionAppendRecordMerge(&sOut, pIter->nCol, 5270 pCsr, nRec-(pCsr-aRec), 5271 pChange->aRecord, pChange->nRecord, &rc 5272 ); 5273 } 5274 }else{ 5275 sessionAppendPartialUpdate(&sOut, pIter, 5276 aRec, nRec, pChange->aRecord, pChange->nRecord, &rc 5277 ); 5278 } 5279 break; 5280 5281 default: 5282 assert( pIter->op==SQLITE_DELETE ); 5283 bDone = 1; 5284 if( pChange->op==SQLITE_INSERT ){ 5285 sessionAppendByte(&sOut, SQLITE_DELETE, &rc); 5286 sessionAppendByte(&sOut, pIter->bIndirect, &rc); 5287 sessionAppendRecordMerge(&sOut, pIter->nCol, 5288 pChange->aRecord, pChange->nRecord, aRec, nRec, &rc 5289 ); 5290 } 5291 break; 5292 } 5293 } 5294 5295 if( bDone==0 ){ 5296 sessionAppendByte(&sOut, pIter->op, &rc); 5297 sessionAppendByte(&sOut, pIter->bIndirect, &rc); 5298 sessionAppendBlob(&sOut, aRec, nRec, &rc); 5299 } 5300 if( rc==SQLITE_OK && xOutput && sOut.nBuf>sessions_strm_chunk_size ){ 5301 rc = xOutput(pOut, sOut.aBuf, sOut.nBuf); 5302 sOut.nBuf = 0; 5303 } 5304 if( rc ) break; 5305 } 5306 5307 if( rc!=SQLITE_OK ){ 5308 sqlite3_free(sOut.aBuf); 5309 memset(&sOut, 0, sizeof(sOut)); 5310 } 5311 5312 if( rc==SQLITE_OK ){ 5313 if( xOutput ){ 5314 if( sOut.nBuf>0 ){ 5315 rc = xOutput(pOut, sOut.aBuf, sOut.nBuf); 5316 } 5317 }else{ 5318 *ppOut = (void*)sOut.aBuf; 5319 *pnOut = sOut.nBuf; 5320 sOut.aBuf = 0; 5321 } 5322 } 5323 sqlite3_free(sOut.aBuf); 5324 return rc; 5325 } 5326 5327 /* 5328 ** Create a new rebaser object. 5329 */ 5330 int sqlite3rebaser_create(sqlite3_rebaser **ppNew){ 5331 int rc = SQLITE_OK; 5332 sqlite3_rebaser *pNew; 5333 5334 pNew = sqlite3_malloc(sizeof(sqlite3_rebaser)); 5335 if( pNew==0 ){ 5336 rc = SQLITE_NOMEM; 5337 }else{ 5338 memset(pNew, 0, sizeof(sqlite3_rebaser)); 5339 } 5340 *ppNew = pNew; 5341 return rc; 5342 } 5343 5344 /* 5345 ** Call this one or more times to configure a rebaser. 5346 */ 5347 int sqlite3rebaser_configure( 5348 sqlite3_rebaser *p, 5349 int nRebase, const void *pRebase 5350 ){ 5351 sqlite3_changeset_iter *pIter = 0; /* Iterator opened on pData/nData */ 5352 int rc; /* Return code */ 5353 rc = sqlite3changeset_start(&pIter, nRebase, (void*)pRebase); 5354 if( rc==SQLITE_OK ){ 5355 rc = sessionChangesetToHash(pIter, &p->grp, 1); 5356 } 5357 sqlite3changeset_finalize(pIter); 5358 return rc; 5359 } 5360 5361 /* 5362 ** Rebase a changeset according to current rebaser configuration 5363 */ 5364 int sqlite3rebaser_rebase( 5365 sqlite3_rebaser *p, 5366 int nIn, const void *pIn, 5367 int *pnOut, void **ppOut 5368 ){ 5369 sqlite3_changeset_iter *pIter = 0; /* Iterator to skip through input */ 5370 int rc = sqlite3changeset_start(&pIter, nIn, (void*)pIn); 5371 5372 if( rc==SQLITE_OK ){ 5373 rc = sessionRebase(p, pIter, 0, 0, pnOut, ppOut); 5374 sqlite3changeset_finalize(pIter); 5375 } 5376 5377 return rc; 5378 } 5379 5380 /* 5381 ** Rebase a changeset according to current rebaser configuration 5382 */ 5383 int sqlite3rebaser_rebase_strm( 5384 sqlite3_rebaser *p, 5385 int (*xInput)(void *pIn, void *pData, int *pnData), 5386 void *pIn, 5387 int (*xOutput)(void *pOut, const void *pData, int nData), 5388 void *pOut 5389 ){ 5390 sqlite3_changeset_iter *pIter = 0; /* Iterator to skip through input */ 5391 int rc = sqlite3changeset_start_strm(&pIter, xInput, pIn); 5392 5393 if( rc==SQLITE_OK ){ 5394 rc = sessionRebase(p, pIter, xOutput, pOut, 0, 0); 5395 sqlite3changeset_finalize(pIter); 5396 } 5397 5398 return rc; 5399 } 5400 5401 /* 5402 ** Destroy a rebaser object 5403 */ 5404 void sqlite3rebaser_delete(sqlite3_rebaser *p){ 5405 if( p ){ 5406 sessionDeleteTable(p->grp.pList); 5407 sqlite3_free(p); 5408 } 5409 } 5410 5411 /* 5412 ** Global configuration 5413 */ 5414 int sqlite3session_config(int op, void *pArg){ 5415 int rc = SQLITE_OK; 5416 switch( op ){ 5417 case SQLITE_SESSION_CONFIG_STRMSIZE: { 5418 int *pInt = (int*)pArg; 5419 if( *pInt>0 ){ 5420 sessions_strm_chunk_size = *pInt; 5421 } 5422 *pInt = sessions_strm_chunk_size; 5423 break; 5424 } 5425 default: 5426 rc = SQLITE_MISUSE; 5427 break; 5428 } 5429 return rc; 5430 } 5431 5432 #endif /* SQLITE_ENABLE_SESSION && SQLITE_ENABLE_PREUPDATE_HOOK */ 5433