1 2 #if !defined(__SQLITESESSION_H_) && defined(SQLITE_ENABLE_SESSION) 3 #define __SQLITESESSION_H_ 1 4 5 /* 6 ** Make sure we can call this stuff from C++. 7 */ 8 #ifdef __cplusplus 9 extern "C" { 10 #endif 11 12 #include "sqlite3.h" 13 14 /* 15 ** CAPI3REF: Session Object Handle 16 ** 17 ** An instance of this object is a [session] that can be used to 18 ** record changes to a database. 19 */ 20 typedef struct sqlite3_session sqlite3_session; 21 22 /* 23 ** CAPI3REF: Changeset Iterator Handle 24 ** 25 ** An instance of this object acts as a cursor for iterating 26 ** over the elements of a [changeset] or [patchset]. 27 */ 28 typedef struct sqlite3_changeset_iter sqlite3_changeset_iter; 29 30 /* 31 ** CAPI3REF: Create A New Session Object 32 ** CONSTRUCTOR: sqlite3_session 33 ** 34 ** Create a new session object attached to database handle db. If successful, 35 ** a pointer to the new object is written to *ppSession and SQLITE_OK is 36 ** returned. If an error occurs, *ppSession is set to NULL and an SQLite 37 ** error code (e.g. SQLITE_NOMEM) is returned. 38 ** 39 ** It is possible to create multiple session objects attached to a single 40 ** database handle. 41 ** 42 ** Session objects created using this function should be deleted using the 43 ** [sqlite3session_delete()] function before the database handle that they 44 ** are attached to is itself closed. If the database handle is closed before 45 ** the session object is deleted, then the results of calling any session 46 ** module function, including [sqlite3session_delete()] on the session object 47 ** are undefined. 48 ** 49 ** Because the session module uses the [sqlite3_preupdate_hook()] API, it 50 ** is not possible for an application to register a pre-update hook on a 51 ** database handle that has one or more session objects attached. Nor is 52 ** it possible to create a session object attached to a database handle for 53 ** which a pre-update hook is already defined. The results of attempting 54 ** either of these things are undefined. 55 ** 56 ** The session object will be used to create changesets for tables in 57 ** database zDb, where zDb is either "main", or "temp", or the name of an 58 ** attached database. It is not an error if database zDb is not attached 59 ** to the database when the session object is created. 60 */ 61 int sqlite3session_create( 62 sqlite3 *db, /* Database handle */ 63 const char *zDb, /* Name of db (e.g. "main") */ 64 sqlite3_session **ppSession /* OUT: New session object */ 65 ); 66 67 /* 68 ** CAPI3REF: Delete A Session Object 69 ** DESTRUCTOR: sqlite3_session 70 ** 71 ** Delete a session object previously allocated using 72 ** [sqlite3session_create()]. Once a session object has been deleted, the 73 ** results of attempting to use pSession with any other session module 74 ** function are undefined. 75 ** 76 ** Session objects must be deleted before the database handle to which they 77 ** are attached is closed. Refer to the documentation for 78 ** [sqlite3session_create()] for details. 79 */ 80 void sqlite3session_delete(sqlite3_session *pSession); 81 82 /* 83 ** CAPIREF: Conigure a Session Object 84 ** METHOD: sqlite3_session 85 */ 86 int sqlite3session_object_config(sqlite3_session*, int op, void *pArg); 87 88 /* 89 ** CAPI3REF: Arguments for sqlite3session_object_config() 90 ** 91 ** The following values may passed as the the 4th parameter to 92 ** [sqlite3session_object_config]. 93 ** 94 ** <dt>SQLITE_SESSION_OBJCONFIG_SIZE <dd> 95 ** This option is used to set, clear or query the flag that enables 96 ** the [sqlite3session_changeset_size()] API. Because it imposes some 97 ** computational overhead, this API is disabled by default. Argument 98 ** pArg must point to a value of type (int). If the value is initially 99 ** 0, then the sqlite3session_changeset_size() API is disabled. If it 100 ** is greater than 0, then the same API is enabled. Or, if the initial 101 ** value is less than zero, no change is made. In all cases the (int) 102 ** variable is set to 1 if the sqlite3session_changeset_size() API is 103 ** enabled following the current call, or 0 otherwise. 104 ** 105 ** It is an error (SQLITE_MISUSE) to attempt to modify this setting after 106 ** the first table has been attached to the session object. 107 */ 108 #define SQLITE_SESSION_OBJCONFIG_SIZE 1 109 110 /* 111 ** CAPI3REF: Enable Or Disable A Session Object 112 ** METHOD: sqlite3_session 113 ** 114 ** Enable or disable the recording of changes by a session object. When 115 ** enabled, a session object records changes made to the database. When 116 ** disabled - it does not. A newly created session object is enabled. 117 ** Refer to the documentation for [sqlite3session_changeset()] for further 118 ** details regarding how enabling and disabling a session object affects 119 ** the eventual changesets. 120 ** 121 ** Passing zero to this function disables the session. Passing a value 122 ** greater than zero enables it. Passing a value less than zero is a 123 ** no-op, and may be used to query the current state of the session. 124 ** 125 ** The return value indicates the final state of the session object: 0 if 126 ** the session is disabled, or 1 if it is enabled. 127 */ 128 int sqlite3session_enable(sqlite3_session *pSession, int bEnable); 129 130 /* 131 ** CAPI3REF: Set Or Clear the Indirect Change Flag 132 ** METHOD: sqlite3_session 133 ** 134 ** Each change recorded by a session object is marked as either direct or 135 ** indirect. A change is marked as indirect if either: 136 ** 137 ** <ul> 138 ** <li> The session object "indirect" flag is set when the change is 139 ** made, or 140 ** <li> The change is made by an SQL trigger or foreign key action 141 ** instead of directly as a result of a users SQL statement. 142 ** </ul> 143 ** 144 ** If a single row is affected by more than one operation within a session, 145 ** then the change is considered indirect if all operations meet the criteria 146 ** for an indirect change above, or direct otherwise. 147 ** 148 ** This function is used to set, clear or query the session object indirect 149 ** flag. If the second argument passed to this function is zero, then the 150 ** indirect flag is cleared. If it is greater than zero, the indirect flag 151 ** is set. Passing a value less than zero does not modify the current value 152 ** of the indirect flag, and may be used to query the current state of the 153 ** indirect flag for the specified session object. 154 ** 155 ** The return value indicates the final state of the indirect flag: 0 if 156 ** it is clear, or 1 if it is set. 157 */ 158 int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect); 159 160 /* 161 ** CAPI3REF: Attach A Table To A Session Object 162 ** METHOD: sqlite3_session 163 ** 164 ** If argument zTab is not NULL, then it is the name of a table to attach 165 ** to the session object passed as the first argument. All subsequent changes 166 ** made to the table while the session object is enabled will be recorded. See 167 ** documentation for [sqlite3session_changeset()] for further details. 168 ** 169 ** Or, if argument zTab is NULL, then changes are recorded for all tables 170 ** in the database. If additional tables are added to the database (by 171 ** executing "CREATE TABLE" statements) after this call is made, changes for 172 ** the new tables are also recorded. 173 ** 174 ** Changes can only be recorded for tables that have a PRIMARY KEY explicitly 175 ** defined as part of their CREATE TABLE statement. It does not matter if the 176 ** PRIMARY KEY is an "INTEGER PRIMARY KEY" (rowid alias) or not. The PRIMARY 177 ** KEY may consist of a single column, or may be a composite key. 178 ** 179 ** It is not an error if the named table does not exist in the database. Nor 180 ** is it an error if the named table does not have a PRIMARY KEY. However, 181 ** no changes will be recorded in either of these scenarios. 182 ** 183 ** Changes are not recorded for individual rows that have NULL values stored 184 ** in one or more of their PRIMARY KEY columns. 185 ** 186 ** SQLITE_OK is returned if the call completes without error. Or, if an error 187 ** occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned. 188 ** 189 ** <h3>Special sqlite_stat1 Handling</h3> 190 ** 191 ** As of SQLite version 3.22.0, the "sqlite_stat1" table is an exception to 192 ** some of the rules above. In SQLite, the schema of sqlite_stat1 is: 193 ** <pre> 194 ** CREATE TABLE sqlite_stat1(tbl,idx,stat) 195 ** </pre> 196 ** 197 ** Even though sqlite_stat1 does not have a PRIMARY KEY, changes are 198 ** recorded for it as if the PRIMARY KEY is (tbl,idx). Additionally, changes 199 ** are recorded for rows for which (idx IS NULL) is true. However, for such 200 ** rows a zero-length blob (SQL value X'') is stored in the changeset or 201 ** patchset instead of a NULL value. This allows such changesets to be 202 ** manipulated by legacy implementations of sqlite3changeset_invert(), 203 ** concat() and similar. 204 ** 205 ** The sqlite3changeset_apply() function automatically converts the 206 ** zero-length blob back to a NULL value when updating the sqlite_stat1 207 ** table. However, if the application calls sqlite3changeset_new(), 208 ** sqlite3changeset_old() or sqlite3changeset_conflict on a changeset 209 ** iterator directly (including on a changeset iterator passed to a 210 ** conflict-handler callback) then the X'' value is returned. The application 211 ** must translate X'' to NULL itself if required. 212 ** 213 ** Legacy (older than 3.22.0) versions of the sessions module cannot capture 214 ** changes made to the sqlite_stat1 table. Legacy versions of the 215 ** sqlite3changeset_apply() function silently ignore any modifications to the 216 ** sqlite_stat1 table that are part of a changeset or patchset. 217 */ 218 int sqlite3session_attach( 219 sqlite3_session *pSession, /* Session object */ 220 const char *zTab /* Table name */ 221 ); 222 223 /* 224 ** CAPI3REF: Set a table filter on a Session Object. 225 ** METHOD: sqlite3_session 226 ** 227 ** The second argument (xFilter) is the "filter callback". For changes to rows 228 ** in tables that are not attached to the Session object, the filter is called 229 ** to determine whether changes to the table's rows should be tracked or not. 230 ** If xFilter returns 0, changes are not tracked. Note that once a table is 231 ** attached, xFilter will not be called again. 232 */ 233 void sqlite3session_table_filter( 234 sqlite3_session *pSession, /* Session object */ 235 int(*xFilter)( 236 void *pCtx, /* Copy of third arg to _filter_table() */ 237 const char *zTab /* Table name */ 238 ), 239 void *pCtx /* First argument passed to xFilter */ 240 ); 241 242 /* 243 ** CAPI3REF: Generate A Changeset From A Session Object 244 ** METHOD: sqlite3_session 245 ** 246 ** Obtain a changeset containing changes to the tables attached to the 247 ** session object passed as the first argument. If successful, 248 ** set *ppChangeset to point to a buffer containing the changeset 249 ** and *pnChangeset to the size of the changeset in bytes before returning 250 ** SQLITE_OK. If an error occurs, set both *ppChangeset and *pnChangeset to 251 ** zero and return an SQLite error code. 252 ** 253 ** A changeset consists of zero or more INSERT, UPDATE and/or DELETE changes, 254 ** each representing a change to a single row of an attached table. An INSERT 255 ** change contains the values of each field of a new database row. A DELETE 256 ** contains the original values of each field of a deleted database row. An 257 ** UPDATE change contains the original values of each field of an updated 258 ** database row along with the updated values for each updated non-primary-key 259 ** column. It is not possible for an UPDATE change to represent a change that 260 ** modifies the values of primary key columns. If such a change is made, it 261 ** is represented in a changeset as a DELETE followed by an INSERT. 262 ** 263 ** Changes are not recorded for rows that have NULL values stored in one or 264 ** more of their PRIMARY KEY columns. If such a row is inserted or deleted, 265 ** no corresponding change is present in the changesets returned by this 266 ** function. If an existing row with one or more NULL values stored in 267 ** PRIMARY KEY columns is updated so that all PRIMARY KEY columns are non-NULL, 268 ** only an INSERT is appears in the changeset. Similarly, if an existing row 269 ** with non-NULL PRIMARY KEY values is updated so that one or more of its 270 ** PRIMARY KEY columns are set to NULL, the resulting changeset contains a 271 ** DELETE change only. 272 ** 273 ** The contents of a changeset may be traversed using an iterator created 274 ** using the [sqlite3changeset_start()] API. A changeset may be applied to 275 ** a database with a compatible schema using the [sqlite3changeset_apply()] 276 ** API. 277 ** 278 ** Within a changeset generated by this function, all changes related to a 279 ** single table are grouped together. In other words, when iterating through 280 ** a changeset or when applying a changeset to a database, all changes related 281 ** to a single table are processed before moving on to the next table. Tables 282 ** are sorted in the same order in which they were attached (or auto-attached) 283 ** to the sqlite3_session object. The order in which the changes related to 284 ** a single table are stored is undefined. 285 ** 286 ** Following a successful call to this function, it is the responsibility of 287 ** the caller to eventually free the buffer that *ppChangeset points to using 288 ** [sqlite3_free()]. 289 ** 290 ** <h3>Changeset Generation</h3> 291 ** 292 ** Once a table has been attached to a session object, the session object 293 ** records the primary key values of all new rows inserted into the table. 294 ** It also records the original primary key and other column values of any 295 ** deleted or updated rows. For each unique primary key value, data is only 296 ** recorded once - the first time a row with said primary key is inserted, 297 ** updated or deleted in the lifetime of the session. 298 ** 299 ** There is one exception to the previous paragraph: when a row is inserted, 300 ** updated or deleted, if one or more of its primary key columns contain a 301 ** NULL value, no record of the change is made. 302 ** 303 ** The session object therefore accumulates two types of records - those 304 ** that consist of primary key values only (created when the user inserts 305 ** a new record) and those that consist of the primary key values and the 306 ** original values of other table columns (created when the users deletes 307 ** or updates a record). 308 ** 309 ** When this function is called, the requested changeset is created using 310 ** both the accumulated records and the current contents of the database 311 ** file. Specifically: 312 ** 313 ** <ul> 314 ** <li> For each record generated by an insert, the database is queried 315 ** for a row with a matching primary key. If one is found, an INSERT 316 ** change is added to the changeset. If no such row is found, no change 317 ** is added to the changeset. 318 ** 319 ** <li> For each record generated by an update or delete, the database is 320 ** queried for a row with a matching primary key. If such a row is 321 ** found and one or more of the non-primary key fields have been 322 ** modified from their original values, an UPDATE change is added to 323 ** the changeset. Or, if no such row is found in the table, a DELETE 324 ** change is added to the changeset. If there is a row with a matching 325 ** primary key in the database, but all fields contain their original 326 ** values, no change is added to the changeset. 327 ** </ul> 328 ** 329 ** This means, amongst other things, that if a row is inserted and then later 330 ** deleted while a session object is active, neither the insert nor the delete 331 ** will be present in the changeset. Or if a row is deleted and then later a 332 ** row with the same primary key values inserted while a session object is 333 ** active, the resulting changeset will contain an UPDATE change instead of 334 ** a DELETE and an INSERT. 335 ** 336 ** When a session object is disabled (see the [sqlite3session_enable()] API), 337 ** it does not accumulate records when rows are inserted, updated or deleted. 338 ** This may appear to have some counter-intuitive effects if a single row 339 ** is written to more than once during a session. For example, if a row 340 ** is inserted while a session object is enabled, then later deleted while 341 ** the same session object is disabled, no INSERT record will appear in the 342 ** changeset, even though the delete took place while the session was disabled. 343 ** Or, if one field of a row is updated while a session is disabled, and 344 ** another field of the same row is updated while the session is enabled, the 345 ** resulting changeset will contain an UPDATE change that updates both fields. 346 */ 347 int sqlite3session_changeset( 348 sqlite3_session *pSession, /* Session object */ 349 int *pnChangeset, /* OUT: Size of buffer at *ppChangeset */ 350 void **ppChangeset /* OUT: Buffer containing changeset */ 351 ); 352 353 /* 354 ** CAPI3REF: Load The Difference Between Tables Into A Session 355 ** METHOD: sqlite3session_changeset_size() 356 ** 357 ** By default, this function always returns 0. For it to return 358 ** a useful result, the sqlite3_session object must have been configured 359 ** to enable this API using [sqlite3session_object_config()] with the 360 ** SQLITE_SESSION_OBJCONFIG_SIZE verb. 361 ** 362 ** When enabled, this function returns an upper limit, in bytes, for the size 363 ** of the changeset that might be produced if sqlite3session_changeset() were 364 ** called. The final changeset size might be equal to or smaller than the 365 ** size in bytes returned by this function. 366 */ 367 sqlite3_int64 sqlite3session_changeset_size(sqlite3_session *pSession); 368 369 /* 370 ** CAPI3REF: Load The Difference Between Tables Into A Session 371 ** METHOD: sqlite3_session 372 ** 373 ** If it is not already attached to the session object passed as the first 374 ** argument, this function attaches table zTbl in the same manner as the 375 ** [sqlite3session_attach()] function. If zTbl does not exist, or if it 376 ** does not have a primary key, this function is a no-op (but does not return 377 ** an error). 378 ** 379 ** Argument zFromDb must be the name of a database ("main", "temp" etc.) 380 ** attached to the same database handle as the session object that contains 381 ** a table compatible with the table attached to the session by this function. 382 ** A table is considered compatible if it: 383 ** 384 ** <ul> 385 ** <li> Has the same name, 386 ** <li> Has the same set of columns declared in the same order, and 387 ** <li> Has the same PRIMARY KEY definition. 388 ** </ul> 389 ** 390 ** If the tables are not compatible, SQLITE_SCHEMA is returned. If the tables 391 ** are compatible but do not have any PRIMARY KEY columns, it is not an error 392 ** but no changes are added to the session object. As with other session 393 ** APIs, tables without PRIMARY KEYs are simply ignored. 394 ** 395 ** This function adds a set of changes to the session object that could be 396 ** used to update the table in database zFrom (call this the "from-table") 397 ** so that its content is the same as the table attached to the session 398 ** object (call this the "to-table"). Specifically: 399 ** 400 ** <ul> 401 ** <li> For each row (primary key) that exists in the to-table but not in 402 ** the from-table, an INSERT record is added to the session object. 403 ** 404 ** <li> For each row (primary key) that exists in the to-table but not in 405 ** the from-table, a DELETE record is added to the session object. 406 ** 407 ** <li> For each row (primary key) that exists in both tables, but features 408 ** different non-PK values in each, an UPDATE record is added to the 409 ** session. 410 ** </ul> 411 ** 412 ** To clarify, if this function is called and then a changeset constructed 413 ** using [sqlite3session_changeset()], then after applying that changeset to 414 ** database zFrom the contents of the two compatible tables would be 415 ** identical. 416 ** 417 ** It an error if database zFrom does not exist or does not contain the 418 ** required compatible table. 419 ** 420 ** If the operation is successful, SQLITE_OK is returned. Otherwise, an SQLite 421 ** error code. In this case, if argument pzErrMsg is not NULL, *pzErrMsg 422 ** may be set to point to a buffer containing an English language error 423 ** message. It is the responsibility of the caller to free this buffer using 424 ** sqlite3_free(). 425 */ 426 int sqlite3session_diff( 427 sqlite3_session *pSession, 428 const char *zFromDb, 429 const char *zTbl, 430 char **pzErrMsg 431 ); 432 433 434 /* 435 ** CAPI3REF: Generate A Patchset From A Session Object 436 ** METHOD: sqlite3_session 437 ** 438 ** The differences between a patchset and a changeset are that: 439 ** 440 ** <ul> 441 ** <li> DELETE records consist of the primary key fields only. The 442 ** original values of other fields are omitted. 443 ** <li> The original values of any modified fields are omitted from 444 ** UPDATE records. 445 ** </ul> 446 ** 447 ** A patchset blob may be used with up to date versions of all 448 ** sqlite3changeset_xxx API functions except for sqlite3changeset_invert(), 449 ** which returns SQLITE_CORRUPT if it is passed a patchset. Similarly, 450 ** attempting to use a patchset blob with old versions of the 451 ** sqlite3changeset_xxx APIs also provokes an SQLITE_CORRUPT error. 452 ** 453 ** Because the non-primary key "old.*" fields are omitted, no 454 ** SQLITE_CHANGESET_DATA conflicts can be detected or reported if a patchset 455 ** is passed to the sqlite3changeset_apply() API. Other conflict types work 456 ** in the same way as for changesets. 457 ** 458 ** Changes within a patchset are ordered in the same way as for changesets 459 ** generated by the sqlite3session_changeset() function (i.e. all changes for 460 ** a single table are grouped together, tables appear in the order in which 461 ** they were attached to the session object). 462 */ 463 int sqlite3session_patchset( 464 sqlite3_session *pSession, /* Session object */ 465 int *pnPatchset, /* OUT: Size of buffer at *ppPatchset */ 466 void **ppPatchset /* OUT: Buffer containing patchset */ 467 ); 468 469 /* 470 ** CAPI3REF: Test if a changeset has recorded any changes. 471 ** 472 ** Return non-zero if no changes to attached tables have been recorded by 473 ** the session object passed as the first argument. Otherwise, if one or 474 ** more changes have been recorded, return zero. 475 ** 476 ** Even if this function returns zero, it is possible that calling 477 ** [sqlite3session_changeset()] on the session handle may still return a 478 ** changeset that contains no changes. This can happen when a row in 479 ** an attached table is modified and then later on the original values 480 ** are restored. However, if this function returns non-zero, then it is 481 ** guaranteed that a call to sqlite3session_changeset() will return a 482 ** changeset containing zero changes. 483 */ 484 int sqlite3session_isempty(sqlite3_session *pSession); 485 486 /* 487 ** CAPI3REF: Query for the amount of heap memory used by a session object. 488 ** 489 ** This API returns the total amount of heap memory in bytes currently 490 ** used by the session object passed as the only argument. 491 */ 492 sqlite3_int64 sqlite3session_memory_used(sqlite3_session *pSession); 493 494 /* 495 ** CAPI3REF: Create An Iterator To Traverse A Changeset 496 ** CONSTRUCTOR: sqlite3_changeset_iter 497 ** 498 ** Create an iterator used to iterate through the contents of a changeset. 499 ** If successful, *pp is set to point to the iterator handle and SQLITE_OK 500 ** is returned. Otherwise, if an error occurs, *pp is set to zero and an 501 ** SQLite error code is returned. 502 ** 503 ** The following functions can be used to advance and query a changeset 504 ** iterator created by this function: 505 ** 506 ** <ul> 507 ** <li> [sqlite3changeset_next()] 508 ** <li> [sqlite3changeset_op()] 509 ** <li> [sqlite3changeset_new()] 510 ** <li> [sqlite3changeset_old()] 511 ** </ul> 512 ** 513 ** It is the responsibility of the caller to eventually destroy the iterator 514 ** by passing it to [sqlite3changeset_finalize()]. The buffer containing the 515 ** changeset (pChangeset) must remain valid until after the iterator is 516 ** destroyed. 517 ** 518 ** Assuming the changeset blob was created by one of the 519 ** [sqlite3session_changeset()], [sqlite3changeset_concat()] or 520 ** [sqlite3changeset_invert()] functions, all changes within the changeset 521 ** that apply to a single table are grouped together. This means that when 522 ** an application iterates through a changeset using an iterator created by 523 ** this function, all changes that relate to a single table are visited 524 ** consecutively. There is no chance that the iterator will visit a change 525 ** the applies to table X, then one for table Y, and then later on visit 526 ** another change for table X. 527 ** 528 ** The behavior of sqlite3changeset_start_v2() and its streaming equivalent 529 ** may be modified by passing a combination of 530 ** [SQLITE_CHANGESETSTART_INVERT | supported flags] as the 4th parameter. 531 ** 532 ** Note that the sqlite3changeset_start_v2() API is still <b>experimental</b> 533 ** and therefore subject to change. 534 */ 535 int sqlite3changeset_start( 536 sqlite3_changeset_iter **pp, /* OUT: New changeset iterator handle */ 537 int nChangeset, /* Size of changeset blob in bytes */ 538 void *pChangeset /* Pointer to blob containing changeset */ 539 ); 540 int sqlite3changeset_start_v2( 541 sqlite3_changeset_iter **pp, /* OUT: New changeset iterator handle */ 542 int nChangeset, /* Size of changeset blob in bytes */ 543 void *pChangeset, /* Pointer to blob containing changeset */ 544 int flags /* SESSION_CHANGESETSTART_* flags */ 545 ); 546 547 /* 548 ** CAPI3REF: Flags for sqlite3changeset_start_v2 549 ** 550 ** The following flags may passed via the 4th parameter to 551 ** [sqlite3changeset_start_v2] and [sqlite3changeset_start_v2_strm]: 552 ** 553 ** <dt>SQLITE_CHANGESETAPPLY_INVERT <dd> 554 ** Invert the changeset while iterating through it. This is equivalent to 555 ** inverting a changeset using sqlite3changeset_invert() before applying it. 556 ** It is an error to specify this flag with a patchset. 557 */ 558 #define SQLITE_CHANGESETSTART_INVERT 0x0002 559 560 561 /* 562 ** CAPI3REF: Advance A Changeset Iterator 563 ** METHOD: sqlite3_changeset_iter 564 ** 565 ** This function may only be used with iterators created by the function 566 ** [sqlite3changeset_start()]. If it is called on an iterator passed to 567 ** a conflict-handler callback by [sqlite3changeset_apply()], SQLITE_MISUSE 568 ** is returned and the call has no effect. 569 ** 570 ** Immediately after an iterator is created by sqlite3changeset_start(), it 571 ** does not point to any change in the changeset. Assuming the changeset 572 ** is not empty, the first call to this function advances the iterator to 573 ** point to the first change in the changeset. Each subsequent call advances 574 ** the iterator to point to the next change in the changeset (if any). If 575 ** no error occurs and the iterator points to a valid change after a call 576 ** to sqlite3changeset_next() has advanced it, SQLITE_ROW is returned. 577 ** Otherwise, if all changes in the changeset have already been visited, 578 ** SQLITE_DONE is returned. 579 ** 580 ** If an error occurs, an SQLite error code is returned. Possible error 581 ** codes include SQLITE_CORRUPT (if the changeset buffer is corrupt) or 582 ** SQLITE_NOMEM. 583 */ 584 int sqlite3changeset_next(sqlite3_changeset_iter *pIter); 585 586 /* 587 ** CAPI3REF: Obtain The Current Operation From A Changeset Iterator 588 ** METHOD: sqlite3_changeset_iter 589 ** 590 ** The pIter argument passed to this function may either be an iterator 591 ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator 592 ** created by [sqlite3changeset_start()]. In the latter case, the most recent 593 ** call to [sqlite3changeset_next()] must have returned [SQLITE_ROW]. If this 594 ** is not the case, this function returns [SQLITE_MISUSE]. 595 ** 596 ** Arguments pOp, pnCol and pzTab may not be NULL. Upon return, three 597 ** outputs are set through these pointers: 598 ** 599 ** *pOp is set to one of [SQLITE_INSERT], [SQLITE_DELETE] or [SQLITE_UPDATE], 600 ** depending on the type of change that the iterator currently points to; 601 ** 602 ** *pnCol is set to the number of columns in the table affected by the change; and 603 ** 604 ** *pzTab is set to point to a nul-terminated utf-8 encoded string containing 605 ** the name of the table affected by the current change. The buffer remains 606 ** valid until either sqlite3changeset_next() is called on the iterator 607 ** or until the conflict-handler function returns. 608 ** 609 ** If pbIndirect is not NULL, then *pbIndirect is set to true (1) if the change 610 ** is an indirect change, or false (0) otherwise. See the documentation for 611 ** [sqlite3session_indirect()] for a description of direct and indirect 612 ** changes. 613 ** 614 ** If no error occurs, SQLITE_OK is returned. If an error does occur, an 615 ** SQLite error code is returned. The values of the output variables may not 616 ** be trusted in this case. 617 */ 618 int sqlite3changeset_op( 619 sqlite3_changeset_iter *pIter, /* Iterator object */ 620 const char **pzTab, /* OUT: Pointer to table name */ 621 int *pnCol, /* OUT: Number of columns in table */ 622 int *pOp, /* OUT: SQLITE_INSERT, DELETE or UPDATE */ 623 int *pbIndirect /* OUT: True for an 'indirect' change */ 624 ); 625 626 /* 627 ** CAPI3REF: Obtain The Primary Key Definition Of A Table 628 ** METHOD: sqlite3_changeset_iter 629 ** 630 ** For each modified table, a changeset includes the following: 631 ** 632 ** <ul> 633 ** <li> The number of columns in the table, and 634 ** <li> Which of those columns make up the tables PRIMARY KEY. 635 ** </ul> 636 ** 637 ** This function is used to find which columns comprise the PRIMARY KEY of 638 ** the table modified by the change that iterator pIter currently points to. 639 ** If successful, *pabPK is set to point to an array of nCol entries, where 640 ** nCol is the number of columns in the table. Elements of *pabPK are set to 641 ** 0x01 if the corresponding column is part of the tables primary key, or 642 ** 0x00 if it is not. 643 ** 644 ** If argument pnCol is not NULL, then *pnCol is set to the number of columns 645 ** in the table. 646 ** 647 ** If this function is called when the iterator does not point to a valid 648 ** entry, SQLITE_MISUSE is returned and the output variables zeroed. Otherwise, 649 ** SQLITE_OK is returned and the output variables populated as described 650 ** above. 651 */ 652 int sqlite3changeset_pk( 653 sqlite3_changeset_iter *pIter, /* Iterator object */ 654 unsigned char **pabPK, /* OUT: Array of boolean - true for PK cols */ 655 int *pnCol /* OUT: Number of entries in output array */ 656 ); 657 658 /* 659 ** CAPI3REF: Obtain old.* Values From A Changeset Iterator 660 ** METHOD: sqlite3_changeset_iter 661 ** 662 ** The pIter argument passed to this function may either be an iterator 663 ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator 664 ** created by [sqlite3changeset_start()]. In the latter case, the most recent 665 ** call to [sqlite3changeset_next()] must have returned SQLITE_ROW. 666 ** Furthermore, it may only be called if the type of change that the iterator 667 ** currently points to is either [SQLITE_DELETE] or [SQLITE_UPDATE]. Otherwise, 668 ** this function returns [SQLITE_MISUSE] and sets *ppValue to NULL. 669 ** 670 ** Argument iVal must be greater than or equal to 0, and less than the number 671 ** of columns in the table affected by the current change. Otherwise, 672 ** [SQLITE_RANGE] is returned and *ppValue is set to NULL. 673 ** 674 ** If successful, this function sets *ppValue to point to a protected 675 ** sqlite3_value object containing the iVal'th value from the vector of 676 ** original row values stored as part of the UPDATE or DELETE change and 677 ** returns SQLITE_OK. The name of the function comes from the fact that this 678 ** is similar to the "old.*" columns available to update or delete triggers. 679 ** 680 ** If some other error occurs (e.g. an OOM condition), an SQLite error code 681 ** is returned and *ppValue is set to NULL. 682 */ 683 int sqlite3changeset_old( 684 sqlite3_changeset_iter *pIter, /* Changeset iterator */ 685 int iVal, /* Column number */ 686 sqlite3_value **ppValue /* OUT: Old value (or NULL pointer) */ 687 ); 688 689 /* 690 ** CAPI3REF: Obtain new.* Values From A Changeset Iterator 691 ** METHOD: sqlite3_changeset_iter 692 ** 693 ** The pIter argument passed to this function may either be an iterator 694 ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator 695 ** created by [sqlite3changeset_start()]. In the latter case, the most recent 696 ** call to [sqlite3changeset_next()] must have returned SQLITE_ROW. 697 ** Furthermore, it may only be called if the type of change that the iterator 698 ** currently points to is either [SQLITE_UPDATE] or [SQLITE_INSERT]. Otherwise, 699 ** this function returns [SQLITE_MISUSE] and sets *ppValue to NULL. 700 ** 701 ** Argument iVal must be greater than or equal to 0, and less than the number 702 ** of columns in the table affected by the current change. Otherwise, 703 ** [SQLITE_RANGE] is returned and *ppValue is set to NULL. 704 ** 705 ** If successful, this function sets *ppValue to point to a protected 706 ** sqlite3_value object containing the iVal'th value from the vector of 707 ** new row values stored as part of the UPDATE or INSERT change and 708 ** returns SQLITE_OK. If the change is an UPDATE and does not include 709 ** a new value for the requested column, *ppValue is set to NULL and 710 ** SQLITE_OK returned. The name of the function comes from the fact that 711 ** this is similar to the "new.*" columns available to update or delete 712 ** triggers. 713 ** 714 ** If some other error occurs (e.g. an OOM condition), an SQLite error code 715 ** is returned and *ppValue is set to NULL. 716 */ 717 int sqlite3changeset_new( 718 sqlite3_changeset_iter *pIter, /* Changeset iterator */ 719 int iVal, /* Column number */ 720 sqlite3_value **ppValue /* OUT: New value (or NULL pointer) */ 721 ); 722 723 /* 724 ** CAPI3REF: Obtain Conflicting Row Values From A Changeset Iterator 725 ** METHOD: sqlite3_changeset_iter 726 ** 727 ** This function should only be used with iterator objects passed to a 728 ** conflict-handler callback by [sqlite3changeset_apply()] with either 729 ** [SQLITE_CHANGESET_DATA] or [SQLITE_CHANGESET_CONFLICT]. If this function 730 ** is called on any other iterator, [SQLITE_MISUSE] is returned and *ppValue 731 ** is set to NULL. 732 ** 733 ** Argument iVal must be greater than or equal to 0, and less than the number 734 ** of columns in the table affected by the current change. Otherwise, 735 ** [SQLITE_RANGE] is returned and *ppValue is set to NULL. 736 ** 737 ** If successful, this function sets *ppValue to point to a protected 738 ** sqlite3_value object containing the iVal'th value from the 739 ** "conflicting row" associated with the current conflict-handler callback 740 ** and returns SQLITE_OK. 741 ** 742 ** If some other error occurs (e.g. an OOM condition), an SQLite error code 743 ** is returned and *ppValue is set to NULL. 744 */ 745 int sqlite3changeset_conflict( 746 sqlite3_changeset_iter *pIter, /* Changeset iterator */ 747 int iVal, /* Column number */ 748 sqlite3_value **ppValue /* OUT: Value from conflicting row */ 749 ); 750 751 /* 752 ** CAPI3REF: Determine The Number Of Foreign Key Constraint Violations 753 ** METHOD: sqlite3_changeset_iter 754 ** 755 ** This function may only be called with an iterator passed to an 756 ** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case 757 ** it sets the output variable to the total number of known foreign key 758 ** violations in the destination database and returns SQLITE_OK. 759 ** 760 ** In all other cases this function returns SQLITE_MISUSE. 761 */ 762 int sqlite3changeset_fk_conflicts( 763 sqlite3_changeset_iter *pIter, /* Changeset iterator */ 764 int *pnOut /* OUT: Number of FK violations */ 765 ); 766 767 768 /* 769 ** CAPI3REF: Finalize A Changeset Iterator 770 ** METHOD: sqlite3_changeset_iter 771 ** 772 ** This function is used to finalize an iterator allocated with 773 ** [sqlite3changeset_start()]. 774 ** 775 ** This function should only be called on iterators created using the 776 ** [sqlite3changeset_start()] function. If an application calls this 777 ** function with an iterator passed to a conflict-handler by 778 ** [sqlite3changeset_apply()], [SQLITE_MISUSE] is immediately returned and the 779 ** call has no effect. 780 ** 781 ** If an error was encountered within a call to an sqlite3changeset_xxx() 782 ** function (for example an [SQLITE_CORRUPT] in [sqlite3changeset_next()] or an 783 ** [SQLITE_NOMEM] in [sqlite3changeset_new()]) then an error code corresponding 784 ** to that error is returned by this function. Otherwise, SQLITE_OK is 785 ** returned. This is to allow the following pattern (pseudo-code): 786 ** 787 ** <pre> 788 ** sqlite3changeset_start(); 789 ** while( SQLITE_ROW==sqlite3changeset_next() ){ 790 ** // Do something with change. 791 ** } 792 ** rc = sqlite3changeset_finalize(); 793 ** if( rc!=SQLITE_OK ){ 794 ** // An error has occurred 795 ** } 796 ** </pre> 797 */ 798 int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter); 799 800 /* 801 ** CAPI3REF: Invert A Changeset 802 ** 803 ** This function is used to "invert" a changeset object. Applying an inverted 804 ** changeset to a database reverses the effects of applying the uninverted 805 ** changeset. Specifically: 806 ** 807 ** <ul> 808 ** <li> Each DELETE change is changed to an INSERT, and 809 ** <li> Each INSERT change is changed to a DELETE, and 810 ** <li> For each UPDATE change, the old.* and new.* values are exchanged. 811 ** </ul> 812 ** 813 ** This function does not change the order in which changes appear within 814 ** the changeset. It merely reverses the sense of each individual change. 815 ** 816 ** If successful, a pointer to a buffer containing the inverted changeset 817 ** is stored in *ppOut, the size of the same buffer is stored in *pnOut, and 818 ** SQLITE_OK is returned. If an error occurs, both *pnOut and *ppOut are 819 ** zeroed and an SQLite error code returned. 820 ** 821 ** It is the responsibility of the caller to eventually call sqlite3_free() 822 ** on the *ppOut pointer to free the buffer allocation following a successful 823 ** call to this function. 824 ** 825 ** WARNING/TODO: This function currently assumes that the input is a valid 826 ** changeset. If it is not, the results are undefined. 827 */ 828 int sqlite3changeset_invert( 829 int nIn, const void *pIn, /* Input changeset */ 830 int *pnOut, void **ppOut /* OUT: Inverse of input */ 831 ); 832 833 /* 834 ** CAPI3REF: Concatenate Two Changeset Objects 835 ** 836 ** This function is used to concatenate two changesets, A and B, into a 837 ** single changeset. The result is a changeset equivalent to applying 838 ** changeset A followed by changeset B. 839 ** 840 ** This function combines the two input changesets using an 841 ** sqlite3_changegroup object. Calling it produces similar results as the 842 ** following code fragment: 843 ** 844 ** <pre> 845 ** sqlite3_changegroup *pGrp; 846 ** rc = sqlite3_changegroup_new(&pGrp); 847 ** if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nA, pA); 848 ** if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nB, pB); 849 ** if( rc==SQLITE_OK ){ 850 ** rc = sqlite3changegroup_output(pGrp, pnOut, ppOut); 851 ** }else{ 852 ** *ppOut = 0; 853 ** *pnOut = 0; 854 ** } 855 ** </pre> 856 ** 857 ** Refer to the sqlite3_changegroup documentation below for details. 858 */ 859 int sqlite3changeset_concat( 860 int nA, /* Number of bytes in buffer pA */ 861 void *pA, /* Pointer to buffer containing changeset A */ 862 int nB, /* Number of bytes in buffer pB */ 863 void *pB, /* Pointer to buffer containing changeset B */ 864 int *pnOut, /* OUT: Number of bytes in output changeset */ 865 void **ppOut /* OUT: Buffer containing output changeset */ 866 ); 867 868 869 /* 870 ** CAPI3REF: Changegroup Handle 871 ** 872 ** A changegroup is an object used to combine two or more 873 ** [changesets] or [patchsets] 874 */ 875 typedef struct sqlite3_changegroup sqlite3_changegroup; 876 877 /* 878 ** CAPI3REF: Create A New Changegroup Object 879 ** CONSTRUCTOR: sqlite3_changegroup 880 ** 881 ** An sqlite3_changegroup object is used to combine two or more changesets 882 ** (or patchsets) into a single changeset (or patchset). A single changegroup 883 ** object may combine changesets or patchsets, but not both. The output is 884 ** always in the same format as the input. 885 ** 886 ** If successful, this function returns SQLITE_OK and populates (*pp) with 887 ** a pointer to a new sqlite3_changegroup object before returning. The caller 888 ** should eventually free the returned object using a call to 889 ** sqlite3changegroup_delete(). If an error occurs, an SQLite error code 890 ** (i.e. SQLITE_NOMEM) is returned and *pp is set to NULL. 891 ** 892 ** The usual usage pattern for an sqlite3_changegroup object is as follows: 893 ** 894 ** <ul> 895 ** <li> It is created using a call to sqlite3changegroup_new(). 896 ** 897 ** <li> Zero or more changesets (or patchsets) are added to the object 898 ** by calling sqlite3changegroup_add(). 899 ** 900 ** <li> The result of combining all input changesets together is obtained 901 ** by the application via a call to sqlite3changegroup_output(). 902 ** 903 ** <li> The object is deleted using a call to sqlite3changegroup_delete(). 904 ** </ul> 905 ** 906 ** Any number of calls to add() and output() may be made between the calls to 907 ** new() and delete(), and in any order. 908 ** 909 ** As well as the regular sqlite3changegroup_add() and 910 ** sqlite3changegroup_output() functions, also available are the streaming 911 ** versions sqlite3changegroup_add_strm() and sqlite3changegroup_output_strm(). 912 */ 913 int sqlite3changegroup_new(sqlite3_changegroup **pp); 914 915 /* 916 ** CAPI3REF: Add A Changeset To A Changegroup 917 ** METHOD: sqlite3_changegroup 918 ** 919 ** Add all changes within the changeset (or patchset) in buffer pData (size 920 ** nData bytes) to the changegroup. 921 ** 922 ** If the buffer contains a patchset, then all prior calls to this function 923 ** on the same changegroup object must also have specified patchsets. Or, if 924 ** the buffer contains a changeset, so must have the earlier calls to this 925 ** function. Otherwise, SQLITE_ERROR is returned and no changes are added 926 ** to the changegroup. 927 ** 928 ** Rows within the changeset and changegroup are identified by the values in 929 ** their PRIMARY KEY columns. A change in the changeset is considered to 930 ** apply to the same row as a change already present in the changegroup if 931 ** the two rows have the same primary key. 932 ** 933 ** Changes to rows that do not already appear in the changegroup are 934 ** simply copied into it. Or, if both the new changeset and the changegroup 935 ** contain changes that apply to a single row, the final contents of the 936 ** changegroup depends on the type of each change, as follows: 937 ** 938 ** <table border=1 style="margin-left:8ex;margin-right:8ex"> 939 ** <tr><th style="white-space:pre">Existing Change </th> 940 ** <th style="white-space:pre">New Change </th> 941 ** <th>Output Change 942 ** <tr><td>INSERT <td>INSERT <td> 943 ** The new change is ignored. This case does not occur if the new 944 ** changeset was recorded immediately after the changesets already 945 ** added to the changegroup. 946 ** <tr><td>INSERT <td>UPDATE <td> 947 ** The INSERT change remains in the changegroup. The values in the 948 ** INSERT change are modified as if the row was inserted by the 949 ** existing change and then updated according to the new change. 950 ** <tr><td>INSERT <td>DELETE <td> 951 ** The existing INSERT is removed from the changegroup. The DELETE is 952 ** not added. 953 ** <tr><td>UPDATE <td>INSERT <td> 954 ** The new change is ignored. This case does not occur if the new 955 ** changeset was recorded immediately after the changesets already 956 ** added to the changegroup. 957 ** <tr><td>UPDATE <td>UPDATE <td> 958 ** The existing UPDATE remains within the changegroup. It is amended 959 ** so that the accompanying values are as if the row was updated once 960 ** by the existing change and then again by the new change. 961 ** <tr><td>UPDATE <td>DELETE <td> 962 ** The existing UPDATE is replaced by the new DELETE within the 963 ** changegroup. 964 ** <tr><td>DELETE <td>INSERT <td> 965 ** If one or more of the column values in the row inserted by the 966 ** new change differ from those in the row deleted by the existing 967 ** change, the existing DELETE is replaced by an UPDATE within the 968 ** changegroup. Otherwise, if the inserted row is exactly the same 969 ** as the deleted row, the existing DELETE is simply discarded. 970 ** <tr><td>DELETE <td>UPDATE <td> 971 ** The new change is ignored. This case does not occur if the new 972 ** changeset was recorded immediately after the changesets already 973 ** added to the changegroup. 974 ** <tr><td>DELETE <td>DELETE <td> 975 ** The new change is ignored. This case does not occur if the new 976 ** changeset was recorded immediately after the changesets already 977 ** added to the changegroup. 978 ** </table> 979 ** 980 ** If the new changeset contains changes to a table that is already present 981 ** in the changegroup, then the number of columns and the position of the 982 ** primary key columns for the table must be consistent. If this is not the 983 ** case, this function fails with SQLITE_SCHEMA. If the input changeset 984 ** appears to be corrupt and the corruption is detected, SQLITE_CORRUPT is 985 ** returned. Or, if an out-of-memory condition occurs during processing, this 986 ** function returns SQLITE_NOMEM. In all cases, if an error occurs the state 987 ** of the final contents of the changegroup is undefined. 988 ** 989 ** If no error occurs, SQLITE_OK is returned. 990 */ 991 int sqlite3changegroup_add(sqlite3_changegroup*, int nData, void *pData); 992 993 /* 994 ** CAPI3REF: Obtain A Composite Changeset From A Changegroup 995 ** METHOD: sqlite3_changegroup 996 ** 997 ** Obtain a buffer containing a changeset (or patchset) representing the 998 ** current contents of the changegroup. If the inputs to the changegroup 999 ** were themselves changesets, the output is a changeset. Or, if the 1000 ** inputs were patchsets, the output is also a patchset. 1001 ** 1002 ** As with the output of the sqlite3session_changeset() and 1003 ** sqlite3session_patchset() functions, all changes related to a single 1004 ** table are grouped together in the output of this function. Tables appear 1005 ** in the same order as for the very first changeset added to the changegroup. 1006 ** If the second or subsequent changesets added to the changegroup contain 1007 ** changes for tables that do not appear in the first changeset, they are 1008 ** appended onto the end of the output changeset, again in the order in 1009 ** which they are first encountered. 1010 ** 1011 ** If an error occurs, an SQLite error code is returned and the output 1012 ** variables (*pnData) and (*ppData) are set to 0. Otherwise, SQLITE_OK 1013 ** is returned and the output variables are set to the size of and a 1014 ** pointer to the output buffer, respectively. In this case it is the 1015 ** responsibility of the caller to eventually free the buffer using a 1016 ** call to sqlite3_free(). 1017 */ 1018 int sqlite3changegroup_output( 1019 sqlite3_changegroup*, 1020 int *pnData, /* OUT: Size of output buffer in bytes */ 1021 void **ppData /* OUT: Pointer to output buffer */ 1022 ); 1023 1024 /* 1025 ** CAPI3REF: Delete A Changegroup Object 1026 ** DESTRUCTOR: sqlite3_changegroup 1027 */ 1028 void sqlite3changegroup_delete(sqlite3_changegroup*); 1029 1030 /* 1031 ** CAPI3REF: Apply A Changeset To A Database 1032 ** 1033 ** Apply a changeset or patchset to a database. These functions attempt to 1034 ** update the "main" database attached to handle db with the changes found in 1035 ** the changeset passed via the second and third arguments. 1036 ** 1037 ** The fourth argument (xFilter) passed to these functions is the "filter 1038 ** callback". If it is not NULL, then for each table affected by at least one 1039 ** change in the changeset, the filter callback is invoked with 1040 ** the table name as the second argument, and a copy of the context pointer 1041 ** passed as the sixth argument as the first. If the "filter callback" 1042 ** returns zero, then no attempt is made to apply any changes to the table. 1043 ** Otherwise, if the return value is non-zero or the xFilter argument to 1044 ** is NULL, all changes related to the table are attempted. 1045 ** 1046 ** For each table that is not excluded by the filter callback, this function 1047 ** tests that the target database contains a compatible table. A table is 1048 ** considered compatible if all of the following are true: 1049 ** 1050 ** <ul> 1051 ** <li> The table has the same name as the name recorded in the 1052 ** changeset, and 1053 ** <li> The table has at least as many columns as recorded in the 1054 ** changeset, and 1055 ** <li> The table has primary key columns in the same position as 1056 ** recorded in the changeset. 1057 ** </ul> 1058 ** 1059 ** If there is no compatible table, it is not an error, but none of the 1060 ** changes associated with the table are applied. A warning message is issued 1061 ** via the sqlite3_log() mechanism with the error code SQLITE_SCHEMA. At most 1062 ** one such warning is issued for each table in the changeset. 1063 ** 1064 ** For each change for which there is a compatible table, an attempt is made 1065 ** to modify the table contents according to the UPDATE, INSERT or DELETE 1066 ** change. If a change cannot be applied cleanly, the conflict handler 1067 ** function passed as the fifth argument to sqlite3changeset_apply() may be 1068 ** invoked. A description of exactly when the conflict handler is invoked for 1069 ** each type of change is below. 1070 ** 1071 ** Unlike the xFilter argument, xConflict may not be passed NULL. The results 1072 ** of passing anything other than a valid function pointer as the xConflict 1073 ** argument are undefined. 1074 ** 1075 ** Each time the conflict handler function is invoked, it must return one 1076 ** of [SQLITE_CHANGESET_OMIT], [SQLITE_CHANGESET_ABORT] or 1077 ** [SQLITE_CHANGESET_REPLACE]. SQLITE_CHANGESET_REPLACE may only be returned 1078 ** if the second argument passed to the conflict handler is either 1079 ** SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If the conflict-handler 1080 ** returns an illegal value, any changes already made are rolled back and 1081 ** the call to sqlite3changeset_apply() returns SQLITE_MISUSE. Different 1082 ** actions are taken by sqlite3changeset_apply() depending on the value 1083 ** returned by each invocation of the conflict-handler function. Refer to 1084 ** the documentation for the three 1085 ** [SQLITE_CHANGESET_OMIT|available return values] for details. 1086 ** 1087 ** <dl> 1088 ** <dt>DELETE Changes<dd> 1089 ** For each DELETE change, the function checks if the target database 1090 ** contains a row with the same primary key value (or values) as the 1091 ** original row values stored in the changeset. If it does, and the values 1092 ** stored in all non-primary key columns also match the values stored in 1093 ** the changeset the row is deleted from the target database. 1094 ** 1095 ** If a row with matching primary key values is found, but one or more of 1096 ** the non-primary key fields contains a value different from the original 1097 ** row value stored in the changeset, the conflict-handler function is 1098 ** invoked with [SQLITE_CHANGESET_DATA] as the second argument. If the 1099 ** database table has more columns than are recorded in the changeset, 1100 ** only the values of those non-primary key fields are compared against 1101 ** the current database contents - any trailing database table columns 1102 ** are ignored. 1103 ** 1104 ** If no row with matching primary key values is found in the database, 1105 ** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND] 1106 ** passed as the second argument. 1107 ** 1108 ** If the DELETE operation is attempted, but SQLite returns SQLITE_CONSTRAINT 1109 ** (which can only happen if a foreign key constraint is violated), the 1110 ** conflict-handler function is invoked with [SQLITE_CHANGESET_CONSTRAINT] 1111 ** passed as the second argument. This includes the case where the DELETE 1112 ** operation is attempted because an earlier call to the conflict handler 1113 ** function returned [SQLITE_CHANGESET_REPLACE]. 1114 ** 1115 ** <dt>INSERT Changes<dd> 1116 ** For each INSERT change, an attempt is made to insert the new row into 1117 ** the database. If the changeset row contains fewer fields than the 1118 ** database table, the trailing fields are populated with their default 1119 ** values. 1120 ** 1121 ** If the attempt to insert the row fails because the database already 1122 ** contains a row with the same primary key values, the conflict handler 1123 ** function is invoked with the second argument set to 1124 ** [SQLITE_CHANGESET_CONFLICT]. 1125 ** 1126 ** If the attempt to insert the row fails because of some other constraint 1127 ** violation (e.g. NOT NULL or UNIQUE), the conflict handler function is 1128 ** invoked with the second argument set to [SQLITE_CHANGESET_CONSTRAINT]. 1129 ** This includes the case where the INSERT operation is re-attempted because 1130 ** an earlier call to the conflict handler function returned 1131 ** [SQLITE_CHANGESET_REPLACE]. 1132 ** 1133 ** <dt>UPDATE Changes<dd> 1134 ** For each UPDATE change, the function checks if the target database 1135 ** contains a row with the same primary key value (or values) as the 1136 ** original row values stored in the changeset. If it does, and the values 1137 ** stored in all modified non-primary key columns also match the values 1138 ** stored in the changeset the row is updated within the target database. 1139 ** 1140 ** If a row with matching primary key values is found, but one or more of 1141 ** the modified non-primary key fields contains a value different from an 1142 ** original row value stored in the changeset, the conflict-handler function 1143 ** is invoked with [SQLITE_CHANGESET_DATA] as the second argument. Since 1144 ** UPDATE changes only contain values for non-primary key fields that are 1145 ** to be modified, only those fields need to match the original values to 1146 ** avoid the SQLITE_CHANGESET_DATA conflict-handler callback. 1147 ** 1148 ** If no row with matching primary key values is found in the database, 1149 ** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND] 1150 ** passed as the second argument. 1151 ** 1152 ** If the UPDATE operation is attempted, but SQLite returns 1153 ** SQLITE_CONSTRAINT, the conflict-handler function is invoked with 1154 ** [SQLITE_CHANGESET_CONSTRAINT] passed as the second argument. 1155 ** This includes the case where the UPDATE operation is attempted after 1156 ** an earlier call to the conflict handler function returned 1157 ** [SQLITE_CHANGESET_REPLACE]. 1158 ** </dl> 1159 ** 1160 ** It is safe to execute SQL statements, including those that write to the 1161 ** table that the callback related to, from within the xConflict callback. 1162 ** This can be used to further customize the application's conflict 1163 ** resolution strategy. 1164 ** 1165 ** All changes made by these functions are enclosed in a savepoint transaction. 1166 ** If any other error (aside from a constraint failure when attempting to 1167 ** write to the target database) occurs, then the savepoint transaction is 1168 ** rolled back, restoring the target database to its original state, and an 1169 ** SQLite error code returned. 1170 ** 1171 ** If the output parameters (ppRebase) and (pnRebase) are non-NULL and 1172 ** the input is a changeset (not a patchset), then sqlite3changeset_apply_v2() 1173 ** may set (*ppRebase) to point to a "rebase" that may be used with the 1174 ** sqlite3_rebaser APIs buffer before returning. In this case (*pnRebase) 1175 ** is set to the size of the buffer in bytes. It is the responsibility of the 1176 ** caller to eventually free any such buffer using sqlite3_free(). The buffer 1177 ** is only allocated and populated if one or more conflicts were encountered 1178 ** while applying the patchset. See comments surrounding the sqlite3_rebaser 1179 ** APIs for further details. 1180 ** 1181 ** The behavior of sqlite3changeset_apply_v2() and its streaming equivalent 1182 ** may be modified by passing a combination of 1183 ** [SQLITE_CHANGESETAPPLY_NOSAVEPOINT | supported flags] as the 9th parameter. 1184 ** 1185 ** Note that the sqlite3changeset_apply_v2() API is still <b>experimental</b> 1186 ** and therefore subject to change. 1187 */ 1188 int sqlite3changeset_apply( 1189 sqlite3 *db, /* Apply change to "main" db of this handle */ 1190 int nChangeset, /* Size of changeset in bytes */ 1191 void *pChangeset, /* Changeset blob */ 1192 int(*xFilter)( 1193 void *pCtx, /* Copy of sixth arg to _apply() */ 1194 const char *zTab /* Table name */ 1195 ), 1196 int(*xConflict)( 1197 void *pCtx, /* Copy of sixth arg to _apply() */ 1198 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */ 1199 sqlite3_changeset_iter *p /* Handle describing change and conflict */ 1200 ), 1201 void *pCtx /* First argument passed to xConflict */ 1202 ); 1203 int sqlite3changeset_apply_v2( 1204 sqlite3 *db, /* Apply change to "main" db of this handle */ 1205 int nChangeset, /* Size of changeset in bytes */ 1206 void *pChangeset, /* Changeset blob */ 1207 int(*xFilter)( 1208 void *pCtx, /* Copy of sixth arg to _apply() */ 1209 const char *zTab /* Table name */ 1210 ), 1211 int(*xConflict)( 1212 void *pCtx, /* Copy of sixth arg to _apply() */ 1213 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */ 1214 sqlite3_changeset_iter *p /* Handle describing change and conflict */ 1215 ), 1216 void *pCtx, /* First argument passed to xConflict */ 1217 void **ppRebase, int *pnRebase, /* OUT: Rebase data */ 1218 int flags /* SESSION_CHANGESETAPPLY_* flags */ 1219 ); 1220 1221 /* 1222 ** CAPI3REF: Flags for sqlite3changeset_apply_v2 1223 ** 1224 ** The following flags may passed via the 9th parameter to 1225 ** [sqlite3changeset_apply_v2] and [sqlite3changeset_apply_v2_strm]: 1226 ** 1227 ** <dl> 1228 ** <dt>SQLITE_CHANGESETAPPLY_NOSAVEPOINT <dd> 1229 ** Usually, the sessions module encloses all operations performed by 1230 ** a single call to apply_v2() or apply_v2_strm() in a [SAVEPOINT]. The 1231 ** SAVEPOINT is committed if the changeset or patchset is successfully 1232 ** applied, or rolled back if an error occurs. Specifying this flag 1233 ** causes the sessions module to omit this savepoint. In this case, if the 1234 ** caller has an open transaction or savepoint when apply_v2() is called, 1235 ** it may revert the partially applied changeset by rolling it back. 1236 ** 1237 ** <dt>SQLITE_CHANGESETAPPLY_INVERT <dd> 1238 ** Invert the changeset before applying it. This is equivalent to inverting 1239 ** a changeset using sqlite3changeset_invert() before applying it. It is 1240 ** an error to specify this flag with a patchset. 1241 */ 1242 #define SQLITE_CHANGESETAPPLY_NOSAVEPOINT 0x0001 1243 #define SQLITE_CHANGESETAPPLY_INVERT 0x0002 1244 1245 /* 1246 ** CAPI3REF: Constants Passed To The Conflict Handler 1247 ** 1248 ** Values that may be passed as the second argument to a conflict-handler. 1249 ** 1250 ** <dl> 1251 ** <dt>SQLITE_CHANGESET_DATA<dd> 1252 ** The conflict handler is invoked with CHANGESET_DATA as the second argument 1253 ** when processing a DELETE or UPDATE change if a row with the required 1254 ** PRIMARY KEY fields is present in the database, but one or more other 1255 ** (non primary-key) fields modified by the update do not contain the 1256 ** expected "before" values. 1257 ** 1258 ** The conflicting row, in this case, is the database row with the matching 1259 ** primary key. 1260 ** 1261 ** <dt>SQLITE_CHANGESET_NOTFOUND<dd> 1262 ** The conflict handler is invoked with CHANGESET_NOTFOUND as the second 1263 ** argument when processing a DELETE or UPDATE change if a row with the 1264 ** required PRIMARY KEY fields is not present in the database. 1265 ** 1266 ** There is no conflicting row in this case. The results of invoking the 1267 ** sqlite3changeset_conflict() API are undefined. 1268 ** 1269 ** <dt>SQLITE_CHANGESET_CONFLICT<dd> 1270 ** CHANGESET_CONFLICT is passed as the second argument to the conflict 1271 ** handler while processing an INSERT change if the operation would result 1272 ** in duplicate primary key values. 1273 ** 1274 ** The conflicting row in this case is the database row with the matching 1275 ** primary key. 1276 ** 1277 ** <dt>SQLITE_CHANGESET_FOREIGN_KEY<dd> 1278 ** If foreign key handling is enabled, and applying a changeset leaves the 1279 ** database in a state containing foreign key violations, the conflict 1280 ** handler is invoked with CHANGESET_FOREIGN_KEY as the second argument 1281 ** exactly once before the changeset is committed. If the conflict handler 1282 ** returns CHANGESET_OMIT, the changes, including those that caused the 1283 ** foreign key constraint violation, are committed. Or, if it returns 1284 ** CHANGESET_ABORT, the changeset is rolled back. 1285 ** 1286 ** No current or conflicting row information is provided. The only function 1287 ** it is possible to call on the supplied sqlite3_changeset_iter handle 1288 ** is sqlite3changeset_fk_conflicts(). 1289 ** 1290 ** <dt>SQLITE_CHANGESET_CONSTRAINT<dd> 1291 ** If any other constraint violation occurs while applying a change (i.e. 1292 ** a UNIQUE, CHECK or NOT NULL constraint), the conflict handler is 1293 ** invoked with CHANGESET_CONSTRAINT as the second argument. 1294 ** 1295 ** There is no conflicting row in this case. The results of invoking the 1296 ** sqlite3changeset_conflict() API are undefined. 1297 ** 1298 ** </dl> 1299 */ 1300 #define SQLITE_CHANGESET_DATA 1 1301 #define SQLITE_CHANGESET_NOTFOUND 2 1302 #define SQLITE_CHANGESET_CONFLICT 3 1303 #define SQLITE_CHANGESET_CONSTRAINT 4 1304 #define SQLITE_CHANGESET_FOREIGN_KEY 5 1305 1306 /* 1307 ** CAPI3REF: Constants Returned By The Conflict Handler 1308 ** 1309 ** A conflict handler callback must return one of the following three values. 1310 ** 1311 ** <dl> 1312 ** <dt>SQLITE_CHANGESET_OMIT<dd> 1313 ** If a conflict handler returns this value no special action is taken. The 1314 ** change that caused the conflict is not applied. The session module 1315 ** continues to the next change in the changeset. 1316 ** 1317 ** <dt>SQLITE_CHANGESET_REPLACE<dd> 1318 ** This value may only be returned if the second argument to the conflict 1319 ** handler was SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If this 1320 ** is not the case, any changes applied so far are rolled back and the 1321 ** call to sqlite3changeset_apply() returns SQLITE_MISUSE. 1322 ** 1323 ** If CHANGESET_REPLACE is returned by an SQLITE_CHANGESET_DATA conflict 1324 ** handler, then the conflicting row is either updated or deleted, depending 1325 ** on the type of change. 1326 ** 1327 ** If CHANGESET_REPLACE is returned by an SQLITE_CHANGESET_CONFLICT conflict 1328 ** handler, then the conflicting row is removed from the database and a 1329 ** second attempt to apply the change is made. If this second attempt fails, 1330 ** the original row is restored to the database before continuing. 1331 ** 1332 ** <dt>SQLITE_CHANGESET_ABORT<dd> 1333 ** If this value is returned, any changes applied so far are rolled back 1334 ** and the call to sqlite3changeset_apply() returns SQLITE_ABORT. 1335 ** </dl> 1336 */ 1337 #define SQLITE_CHANGESET_OMIT 0 1338 #define SQLITE_CHANGESET_REPLACE 1 1339 #define SQLITE_CHANGESET_ABORT 2 1340 1341 /* 1342 ** CAPI3REF: Rebasing changesets 1343 ** EXPERIMENTAL 1344 ** 1345 ** Suppose there is a site hosting a database in state S0. And that 1346 ** modifications are made that move that database to state S1 and a 1347 ** changeset recorded (the "local" changeset). Then, a changeset based 1348 ** on S0 is received from another site (the "remote" changeset) and 1349 ** applied to the database. The database is then in state 1350 ** (S1+"remote"), where the exact state depends on any conflict 1351 ** resolution decisions (OMIT or REPLACE) made while applying "remote". 1352 ** Rebasing a changeset is to update it to take those conflict 1353 ** resolution decisions into account, so that the same conflicts 1354 ** do not have to be resolved elsewhere in the network. 1355 ** 1356 ** For example, if both the local and remote changesets contain an 1357 ** INSERT of the same key on "CREATE TABLE t1(a PRIMARY KEY, b)": 1358 ** 1359 ** local: INSERT INTO t1 VALUES(1, 'v1'); 1360 ** remote: INSERT INTO t1 VALUES(1, 'v2'); 1361 ** 1362 ** and the conflict resolution is REPLACE, then the INSERT change is 1363 ** removed from the local changeset (it was overridden). Or, if the 1364 ** conflict resolution was "OMIT", then the local changeset is modified 1365 ** to instead contain: 1366 ** 1367 ** UPDATE t1 SET b = 'v2' WHERE a=1; 1368 ** 1369 ** Changes within the local changeset are rebased as follows: 1370 ** 1371 ** <dl> 1372 ** <dt>Local INSERT<dd> 1373 ** This may only conflict with a remote INSERT. If the conflict 1374 ** resolution was OMIT, then add an UPDATE change to the rebased 1375 ** changeset. Or, if the conflict resolution was REPLACE, add 1376 ** nothing to the rebased changeset. 1377 ** 1378 ** <dt>Local DELETE<dd> 1379 ** This may conflict with a remote UPDATE or DELETE. In both cases the 1380 ** only possible resolution is OMIT. If the remote operation was a 1381 ** DELETE, then add no change to the rebased changeset. If the remote 1382 ** operation was an UPDATE, then the old.* fields of change are updated 1383 ** to reflect the new.* values in the UPDATE. 1384 ** 1385 ** <dt>Local UPDATE<dd> 1386 ** This may conflict with a remote UPDATE or DELETE. If it conflicts 1387 ** with a DELETE, and the conflict resolution was OMIT, then the update 1388 ** is changed into an INSERT. Any undefined values in the new.* record 1389 ** from the update change are filled in using the old.* values from 1390 ** the conflicting DELETE. Or, if the conflict resolution was REPLACE, 1391 ** the UPDATE change is simply omitted from the rebased changeset. 1392 ** 1393 ** If conflict is with a remote UPDATE and the resolution is OMIT, then 1394 ** the old.* values are rebased using the new.* values in the remote 1395 ** change. Or, if the resolution is REPLACE, then the change is copied 1396 ** into the rebased changeset with updates to columns also updated by 1397 ** the conflicting remote UPDATE removed. If this means no columns would 1398 ** be updated, the change is omitted. 1399 ** </dl> 1400 ** 1401 ** A local change may be rebased against multiple remote changes 1402 ** simultaneously. If a single key is modified by multiple remote 1403 ** changesets, they are combined as follows before the local changeset 1404 ** is rebased: 1405 ** 1406 ** <ul> 1407 ** <li> If there has been one or more REPLACE resolutions on a 1408 ** key, it is rebased according to a REPLACE. 1409 ** 1410 ** <li> If there have been no REPLACE resolutions on a key, then 1411 ** the local changeset is rebased according to the most recent 1412 ** of the OMIT resolutions. 1413 ** </ul> 1414 ** 1415 ** Note that conflict resolutions from multiple remote changesets are 1416 ** combined on a per-field basis, not per-row. This means that in the 1417 ** case of multiple remote UPDATE operations, some fields of a single 1418 ** local change may be rebased for REPLACE while others are rebased for 1419 ** OMIT. 1420 ** 1421 ** In order to rebase a local changeset, the remote changeset must first 1422 ** be applied to the local database using sqlite3changeset_apply_v2() and 1423 ** the buffer of rebase information captured. Then: 1424 ** 1425 ** <ol> 1426 ** <li> An sqlite3_rebaser object is created by calling 1427 ** sqlite3rebaser_create(). 1428 ** <li> The new object is configured with the rebase buffer obtained from 1429 ** sqlite3changeset_apply_v2() by calling sqlite3rebaser_configure(). 1430 ** If the local changeset is to be rebased against multiple remote 1431 ** changesets, then sqlite3rebaser_configure() should be called 1432 ** multiple times, in the same order that the multiple 1433 ** sqlite3changeset_apply_v2() calls were made. 1434 ** <li> Each local changeset is rebased by calling sqlite3rebaser_rebase(). 1435 ** <li> The sqlite3_rebaser object is deleted by calling 1436 ** sqlite3rebaser_delete(). 1437 ** </ol> 1438 */ 1439 typedef struct sqlite3_rebaser sqlite3_rebaser; 1440 1441 /* 1442 ** CAPI3REF: Create a changeset rebaser object. 1443 ** EXPERIMENTAL 1444 ** 1445 ** Allocate a new changeset rebaser object. If successful, set (*ppNew) to 1446 ** point to the new object and return SQLITE_OK. Otherwise, if an error 1447 ** occurs, return an SQLite error code (e.g. SQLITE_NOMEM) and set (*ppNew) 1448 ** to NULL. 1449 */ 1450 int sqlite3rebaser_create(sqlite3_rebaser **ppNew); 1451 1452 /* 1453 ** CAPI3REF: Configure a changeset rebaser object. 1454 ** EXPERIMENTAL 1455 ** 1456 ** Configure the changeset rebaser object to rebase changesets according 1457 ** to the conflict resolutions described by buffer pRebase (size nRebase 1458 ** bytes), which must have been obtained from a previous call to 1459 ** sqlite3changeset_apply_v2(). 1460 */ 1461 int sqlite3rebaser_configure( 1462 sqlite3_rebaser*, 1463 int nRebase, const void *pRebase 1464 ); 1465 1466 /* 1467 ** CAPI3REF: Rebase a changeset 1468 ** EXPERIMENTAL 1469 ** 1470 ** Argument pIn must point to a buffer containing a changeset nIn bytes 1471 ** in size. This function allocates and populates a buffer with a copy 1472 ** of the changeset rebased according to the configuration of the 1473 ** rebaser object passed as the first argument. If successful, (*ppOut) 1474 ** is set to point to the new buffer containing the rebased changeset and 1475 ** (*pnOut) to its size in bytes and SQLITE_OK returned. It is the 1476 ** responsibility of the caller to eventually free the new buffer using 1477 ** sqlite3_free(). Otherwise, if an error occurs, (*ppOut) and (*pnOut) 1478 ** are set to zero and an SQLite error code returned. 1479 */ 1480 int sqlite3rebaser_rebase( 1481 sqlite3_rebaser*, 1482 int nIn, const void *pIn, 1483 int *pnOut, void **ppOut 1484 ); 1485 1486 /* 1487 ** CAPI3REF: Delete a changeset rebaser object. 1488 ** EXPERIMENTAL 1489 ** 1490 ** Delete the changeset rebaser object and all associated resources. There 1491 ** should be one call to this function for each successful invocation 1492 ** of sqlite3rebaser_create(). 1493 */ 1494 void sqlite3rebaser_delete(sqlite3_rebaser *p); 1495 1496 /* 1497 ** CAPI3REF: Streaming Versions of API functions. 1498 ** 1499 ** The six streaming API xxx_strm() functions serve similar purposes to the 1500 ** corresponding non-streaming API functions: 1501 ** 1502 ** <table border=1 style="margin-left:8ex;margin-right:8ex"> 1503 ** <tr><th>Streaming function<th>Non-streaming equivalent</th> 1504 ** <tr><td>sqlite3changeset_apply_strm<td>[sqlite3changeset_apply] 1505 ** <tr><td>sqlite3changeset_apply_strm_v2<td>[sqlite3changeset_apply_v2] 1506 ** <tr><td>sqlite3changeset_concat_strm<td>[sqlite3changeset_concat] 1507 ** <tr><td>sqlite3changeset_invert_strm<td>[sqlite3changeset_invert] 1508 ** <tr><td>sqlite3changeset_start_strm<td>[sqlite3changeset_start] 1509 ** <tr><td>sqlite3session_changeset_strm<td>[sqlite3session_changeset] 1510 ** <tr><td>sqlite3session_patchset_strm<td>[sqlite3session_patchset] 1511 ** </table> 1512 ** 1513 ** Non-streaming functions that accept changesets (or patchsets) as input 1514 ** require that the entire changeset be stored in a single buffer in memory. 1515 ** Similarly, those that return a changeset or patchset do so by returning 1516 ** a pointer to a single large buffer allocated using sqlite3_malloc(). 1517 ** Normally this is convenient. However, if an application running in a 1518 ** low-memory environment is required to handle very large changesets, the 1519 ** large contiguous memory allocations required can become onerous. 1520 ** 1521 ** In order to avoid this problem, instead of a single large buffer, input 1522 ** is passed to a streaming API functions by way of a callback function that 1523 ** the sessions module invokes to incrementally request input data as it is 1524 ** required. In all cases, a pair of API function parameters such as 1525 ** 1526 ** <pre> 1527 ** int nChangeset, 1528 ** void *pChangeset, 1529 ** </pre> 1530 ** 1531 ** Is replaced by: 1532 ** 1533 ** <pre> 1534 ** int (*xInput)(void *pIn, void *pData, int *pnData), 1535 ** void *pIn, 1536 ** </pre> 1537 ** 1538 ** Each time the xInput callback is invoked by the sessions module, the first 1539 ** argument passed is a copy of the supplied pIn context pointer. The second 1540 ** argument, pData, points to a buffer (*pnData) bytes in size. Assuming no 1541 ** error occurs the xInput method should copy up to (*pnData) bytes of data 1542 ** into the buffer and set (*pnData) to the actual number of bytes copied 1543 ** before returning SQLITE_OK. If the input is completely exhausted, (*pnData) 1544 ** should be set to zero to indicate this. Or, if an error occurs, an SQLite 1545 ** error code should be returned. In all cases, if an xInput callback returns 1546 ** an error, all processing is abandoned and the streaming API function 1547 ** returns a copy of the error code to the caller. 1548 ** 1549 ** In the case of sqlite3changeset_start_strm(), the xInput callback may be 1550 ** invoked by the sessions module at any point during the lifetime of the 1551 ** iterator. If such an xInput callback returns an error, the iterator enters 1552 ** an error state, whereby all subsequent calls to iterator functions 1553 ** immediately fail with the same error code as returned by xInput. 1554 ** 1555 ** Similarly, streaming API functions that return changesets (or patchsets) 1556 ** return them in chunks by way of a callback function instead of via a 1557 ** pointer to a single large buffer. In this case, a pair of parameters such 1558 ** as: 1559 ** 1560 ** <pre> 1561 ** int *pnChangeset, 1562 ** void **ppChangeset, 1563 ** </pre> 1564 ** 1565 ** Is replaced by: 1566 ** 1567 ** <pre> 1568 ** int (*xOutput)(void *pOut, const void *pData, int nData), 1569 ** void *pOut 1570 ** </pre> 1571 ** 1572 ** The xOutput callback is invoked zero or more times to return data to 1573 ** the application. The first parameter passed to each call is a copy of the 1574 ** pOut pointer supplied by the application. The second parameter, pData, 1575 ** points to a buffer nData bytes in size containing the chunk of output 1576 ** data being returned. If the xOutput callback successfully processes the 1577 ** supplied data, it should return SQLITE_OK to indicate success. Otherwise, 1578 ** it should return some other SQLite error code. In this case processing 1579 ** is immediately abandoned and the streaming API function returns a copy 1580 ** of the xOutput error code to the application. 1581 ** 1582 ** The sessions module never invokes an xOutput callback with the third 1583 ** parameter set to a value less than or equal to zero. Other than this, 1584 ** no guarantees are made as to the size of the chunks of data returned. 1585 */ 1586 int sqlite3changeset_apply_strm( 1587 sqlite3 *db, /* Apply change to "main" db of this handle */ 1588 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */ 1589 void *pIn, /* First arg for xInput */ 1590 int(*xFilter)( 1591 void *pCtx, /* Copy of sixth arg to _apply() */ 1592 const char *zTab /* Table name */ 1593 ), 1594 int(*xConflict)( 1595 void *pCtx, /* Copy of sixth arg to _apply() */ 1596 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */ 1597 sqlite3_changeset_iter *p /* Handle describing change and conflict */ 1598 ), 1599 void *pCtx /* First argument passed to xConflict */ 1600 ); 1601 int sqlite3changeset_apply_v2_strm( 1602 sqlite3 *db, /* Apply change to "main" db of this handle */ 1603 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */ 1604 void *pIn, /* First arg for xInput */ 1605 int(*xFilter)( 1606 void *pCtx, /* Copy of sixth arg to _apply() */ 1607 const char *zTab /* Table name */ 1608 ), 1609 int(*xConflict)( 1610 void *pCtx, /* Copy of sixth arg to _apply() */ 1611 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */ 1612 sqlite3_changeset_iter *p /* Handle describing change and conflict */ 1613 ), 1614 void *pCtx, /* First argument passed to xConflict */ 1615 void **ppRebase, int *pnRebase, 1616 int flags 1617 ); 1618 int sqlite3changeset_concat_strm( 1619 int (*xInputA)(void *pIn, void *pData, int *pnData), 1620 void *pInA, 1621 int (*xInputB)(void *pIn, void *pData, int *pnData), 1622 void *pInB, 1623 int (*xOutput)(void *pOut, const void *pData, int nData), 1624 void *pOut 1625 ); 1626 int sqlite3changeset_invert_strm( 1627 int (*xInput)(void *pIn, void *pData, int *pnData), 1628 void *pIn, 1629 int (*xOutput)(void *pOut, const void *pData, int nData), 1630 void *pOut 1631 ); 1632 int sqlite3changeset_start_strm( 1633 sqlite3_changeset_iter **pp, 1634 int (*xInput)(void *pIn, void *pData, int *pnData), 1635 void *pIn 1636 ); 1637 int sqlite3changeset_start_v2_strm( 1638 sqlite3_changeset_iter **pp, 1639 int (*xInput)(void *pIn, void *pData, int *pnData), 1640 void *pIn, 1641 int flags 1642 ); 1643 int sqlite3session_changeset_strm( 1644 sqlite3_session *pSession, 1645 int (*xOutput)(void *pOut, const void *pData, int nData), 1646 void *pOut 1647 ); 1648 int sqlite3session_patchset_strm( 1649 sqlite3_session *pSession, 1650 int (*xOutput)(void *pOut, const void *pData, int nData), 1651 void *pOut 1652 ); 1653 int sqlite3changegroup_add_strm(sqlite3_changegroup*, 1654 int (*xInput)(void *pIn, void *pData, int *pnData), 1655 void *pIn 1656 ); 1657 int sqlite3changegroup_output_strm(sqlite3_changegroup*, 1658 int (*xOutput)(void *pOut, const void *pData, int nData), 1659 void *pOut 1660 ); 1661 int sqlite3rebaser_rebase_strm( 1662 sqlite3_rebaser *pRebaser, 1663 int (*xInput)(void *pIn, void *pData, int *pnData), 1664 void *pIn, 1665 int (*xOutput)(void *pOut, const void *pData, int nData), 1666 void *pOut 1667 ); 1668 1669 /* 1670 ** CAPI3REF: Configure global parameters 1671 ** 1672 ** The sqlite3session_config() interface is used to make global configuration 1673 ** changes to the sessions module in order to tune it to the specific needs 1674 ** of the application. 1675 ** 1676 ** The sqlite3session_config() interface is not threadsafe. If it is invoked 1677 ** while any other thread is inside any other sessions method then the 1678 ** results are undefined. Furthermore, if it is invoked after any sessions 1679 ** related objects have been created, the results are also undefined. 1680 ** 1681 ** The first argument to the sqlite3session_config() function must be one 1682 ** of the SQLITE_SESSION_CONFIG_XXX constants defined below. The 1683 ** interpretation of the (void*) value passed as the second parameter and 1684 ** the effect of calling this function depends on the value of the first 1685 ** parameter. 1686 ** 1687 ** <dl> 1688 ** <dt>SQLITE_SESSION_CONFIG_STRMSIZE<dd> 1689 ** By default, the sessions module streaming interfaces attempt to input 1690 ** and output data in approximately 1 KiB chunks. This operand may be used 1691 ** to set and query the value of this configuration setting. The pointer 1692 ** passed as the second argument must point to a value of type (int). 1693 ** If this value is greater than 0, it is used as the new streaming data 1694 ** chunk size for both input and output. Before returning, the (int) value 1695 ** pointed to by pArg is set to the final value of the streaming interface 1696 ** chunk size. 1697 ** </dl> 1698 ** 1699 ** This function returns SQLITE_OK if successful, or an SQLite error code 1700 ** otherwise. 1701 */ 1702 int sqlite3session_config(int op, void *pArg); 1703 1704 /* 1705 ** CAPI3REF: Values for sqlite3session_config(). 1706 */ 1707 #define SQLITE_SESSION_CONFIG_STRMSIZE 1 1708 1709 /* 1710 ** Make sure we can call this stuff from C++. 1711 */ 1712 #ifdef __cplusplus 1713 } 1714 #endif 1715 1716 #endif /* !defined(__SQLITESESSION_H_) && defined(SQLITE_ENABLE_SESSION) */ 1717