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