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