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