14fccf43aSdan 
24e80d5fcSdrh #if !defined(__SQLITESESSION_H_) && defined(SQLITE_ENABLE_SESSION)
34fccf43aSdan #define __SQLITESESSION_H_ 1
44fccf43aSdan 
54fccf43aSdan /*
64fccf43aSdan ** Make sure we can call this stuff from C++.
74fccf43aSdan */
84fccf43aSdan #ifdef __cplusplus
94fccf43aSdan extern "C" {
104fccf43aSdan #endif
114fccf43aSdan 
124fccf43aSdan #include "sqlite3.h"
134fccf43aSdan 
14a2df3d9fSdan /*
15a2df3d9fSdan ** CAPI3REF: Session Object Handle
16bda30ce4Sdrh **
17bda30ce4Sdrh ** An instance of this object is a [session] that can be used to
18bda30ce4Sdrh ** record changes to a database.
19a2df3d9fSdan */
204fccf43aSdan typedef struct sqlite3_session sqlite3_session;
21a2df3d9fSdan 
22a2df3d9fSdan /*
23a2df3d9fSdan ** CAPI3REF: Changeset Iterator Handle
24bda30ce4Sdrh **
255e9825ecSmistachkin ** An instance of this object acts as a cursor for iterating
26bda30ce4Sdrh ** over the elements of a [changeset] or [patchset].
27a2df3d9fSdan */
284fccf43aSdan typedef struct sqlite3_changeset_iter sqlite3_changeset_iter;
294fccf43aSdan 
304fccf43aSdan /*
31a2df3d9fSdan ** CAPI3REF: Create A New Session Object
32bda30ce4Sdrh ** CONSTRUCTOR: sqlite3_session
33a2df3d9fSdan **
3437db03bfSdan ** Create a new session object attached to database handle db. If successful,
3537db03bfSdan ** a pointer to the new object is written to *ppSession and SQLITE_OK is
3637db03bfSdan ** returned. If an error occurs, *ppSession is set to NULL and an SQLite
37c21111daSdan ** error code (e.g. SQLITE_NOMEM) is returned.
3837db03bfSdan **
3937db03bfSdan ** It is possible to create multiple session objects attached to a single
4037db03bfSdan ** database handle.
4137db03bfSdan **
4237db03bfSdan ** Session objects created using this function should be deleted using the
4337db03bfSdan ** [sqlite3session_delete()] function before the database handle that they
4437db03bfSdan ** are attached to is itself closed. If the database handle is closed before
4537db03bfSdan ** the session object is deleted, then the results of calling any session
4637db03bfSdan ** module function, including [sqlite3session_delete()] on the session object
4737db03bfSdan ** are undefined.
4837db03bfSdan **
4937db03bfSdan ** Because the session module uses the [sqlite3_preupdate_hook()] API, it
5037db03bfSdan ** is not possible for an application to register a pre-update hook on a
5137db03bfSdan ** database handle that has one or more session objects attached. Nor is
5237db03bfSdan ** it possible to create a session object attached to a database handle for
5337db03bfSdan ** which a pre-update hook is already defined. The results of attempting
5437db03bfSdan ** either of these things are undefined.
5537db03bfSdan **
5637db03bfSdan ** The session object will be used to create changesets for tables in
5737db03bfSdan ** database zDb, where zDb is either "main", or "temp", or the name of an
58ca62ad57Sdan ** attached database. It is not an error if database zDb is not attached
5937db03bfSdan ** to the database when the session object is created.
604fccf43aSdan */
614fccf43aSdan int sqlite3session_create(
624fccf43aSdan   sqlite3 *db,                    /* Database handle */
634fccf43aSdan   const char *zDb,                /* Name of db (e.g. "main") */
644fccf43aSdan   sqlite3_session **ppSession     /* OUT: New session object */
654fccf43aSdan );
664fccf43aSdan 
674fccf43aSdan /*
68a2df3d9fSdan ** CAPI3REF: Delete A Session Object
69bda30ce4Sdrh ** DESTRUCTOR: sqlite3_session
70a2df3d9fSdan **
7137db03bfSdan ** Delete a session object previously allocated using
7237db03bfSdan ** [sqlite3session_create()]. Once a session object has been deleted, the
7337db03bfSdan ** results of attempting to use pSession with any other session module
7437db03bfSdan ** function are undefined.
7537db03bfSdan **
7637db03bfSdan ** Session objects must be deleted before the database handle to which they
7737db03bfSdan ** are attached is closed. Refer to the documentation for
7837db03bfSdan ** [sqlite3session_create()] for details.
7937db03bfSdan */
8037db03bfSdan void sqlite3session_delete(sqlite3_session *pSession);
8137db03bfSdan 
827531a5a3Sdan 
8337db03bfSdan /*
84a2df3d9fSdan ** CAPI3REF: Enable Or Disable A Session Object
85bda30ce4Sdrh ** METHOD: sqlite3_session
86a2df3d9fSdan **
874fccf43aSdan ** Enable or disable the recording of changes by a session object. When
884fccf43aSdan ** enabled, a session object records changes made to the database. When
894fccf43aSdan ** disabled - it does not. A newly created session object is enabled.
90c21111daSdan ** Refer to the documentation for [sqlite3session_changeset()] for further
91c21111daSdan ** details regarding how enabling and disabling a session object affects
92c21111daSdan ** the eventual changesets.
934fccf43aSdan **
944fccf43aSdan ** Passing zero to this function disables the session. Passing a value
954fccf43aSdan ** greater than zero enables it. Passing a value less than zero is a
964fccf43aSdan ** no-op, and may be used to query the current state of the session.
974fccf43aSdan **
984fccf43aSdan ** The return value indicates the final state of the session object: 0 if
994fccf43aSdan ** the session is disabled, or 1 if it is enabled.
1004fccf43aSdan */
1014fccf43aSdan int sqlite3session_enable(sqlite3_session *pSession, int bEnable);
1024fccf43aSdan 
1034fccf43aSdan /*
104b4480e94Sdan ** CAPI3REF: Set Or Clear the Indirect Change Flag
105bda30ce4Sdrh ** METHOD: sqlite3_session
106b4480e94Sdan **
107b4480e94Sdan ** Each change recorded by a session object is marked as either direct or
108b4480e94Sdan ** indirect. A change is marked as indirect if either:
109b4480e94Sdan **
110b4480e94Sdan ** <ul>
111b4480e94Sdan **   <li> The session object "indirect" flag is set when the change is
112b4480e94Sdan **        made, or
113b4480e94Sdan **   <li> The change is made by an SQL trigger or foreign key action
114b4480e94Sdan **        instead of directly as a result of a users SQL statement.
115b4480e94Sdan ** </ul>
116b4480e94Sdan **
117b4480e94Sdan ** If a single row is affected by more than one operation within a session,
118b4480e94Sdan ** then the change is considered indirect if all operations meet the criteria
119b4480e94Sdan ** for an indirect change above, or direct otherwise.
120b4480e94Sdan **
121b4480e94Sdan ** This function is used to set, clear or query the session object indirect
122b4480e94Sdan ** flag.  If the second argument passed to this function is zero, then the
123b4480e94Sdan ** indirect flag is cleared. If it is greater than zero, the indirect flag
124b4480e94Sdan ** is set. Passing a value less than zero does not modify the current value
125b4480e94Sdan ** of the indirect flag, and may be used to query the current state of the
126b4480e94Sdan ** indirect flag for the specified session object.
127b4480e94Sdan **
128b4480e94Sdan ** The return value indicates the final state of the indirect flag: 0 if
129b4480e94Sdan ** it is clear, or 1 if it is set.
130b4480e94Sdan */
131b4480e94Sdan int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect);
132b4480e94Sdan 
133b4480e94Sdan /*
134157546f4Sshaneh ** CAPI3REF: Attach A Table To A Session Object
135bda30ce4Sdrh ** METHOD: sqlite3_session
136a2df3d9fSdan **
137ff4d0f41Sdan ** If argument zTab is not NULL, then it is the name of a table to attach
138ff4d0f41Sdan ** to the session object passed as the first argument. All subsequent changes
139ff4d0f41Sdan ** made to the table while the session object is enabled will be recorded. See
140ff4d0f41Sdan ** documentation for [sqlite3session_changeset()] for further details.
141ff4d0f41Sdan **
142ff4d0f41Sdan ** Or, if argument zTab is NULL, then changes are recorded for all tables
143ff4d0f41Sdan ** in the database. If additional tables are added to the database (by
144ff4d0f41Sdan ** executing "CREATE TABLE" statements) after this call is made, changes for
145ff4d0f41Sdan ** the new tables are also recorded.
1464fccf43aSdan **
147c21111daSdan ** Changes can only be recorded for tables that have a PRIMARY KEY explicitly
148c21111daSdan ** defined as part of their CREATE TABLE statement. It does not matter if the
149c21111daSdan ** PRIMARY KEY is an "INTEGER PRIMARY KEY" (rowid alias) or not. The PRIMARY
150c21111daSdan ** KEY may consist of a single column, or may be a composite key.
151c21111daSdan **
152c21111daSdan ** It is not an error if the named table does not exist in the database. Nor
153c21111daSdan ** is it an error if the named table does not have a PRIMARY KEY. However,
154c21111daSdan ** no changes will be recorded in either of these scenarios.
155c21111daSdan **
15627453faeSdan ** Changes are not recorded for individual rows that have NULL values stored
15727453faeSdan ** in one or more of their PRIMARY KEY columns.
15827453faeSdan **
159ff4d0f41Sdan ** SQLITE_OK is returned if the call completes without error. Or, if an error
160ff4d0f41Sdan ** occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned.
161e3ca3831Sdan **
162e3ca3831Sdan ** <h3>Special sqlite_stat1 Handling</h3>
163e3ca3831Sdan **
164cae5b9feSdan ** As of SQLite version 3.22.0, the "sqlite_stat1" table is an exception to
165cae5b9feSdan ** some of the rules above. In SQLite, the schema of sqlite_stat1 is:
166e3ca3831Sdan **  <pre>
167e3ca3831Sdan **  &nbsp;     CREATE TABLE sqlite_stat1(tbl,idx,stat)
168e3ca3831Sdan **  </pre>
169e3ca3831Sdan **
170e3ca3831Sdan ** Even though sqlite_stat1 does not have a PRIMARY KEY, changes are
171e3ca3831Sdan ** recorded for it as if the PRIMARY KEY is (tbl,idx). Additionally, changes
172e3ca3831Sdan ** are recorded for rows for which (idx IS NULL) is true. However, for such
173e3ca3831Sdan ** rows a zero-length blob (SQL value X'') is stored in the changeset or
174e3ca3831Sdan ** patchset instead of a NULL value. This allows such changesets to be
175e3ca3831Sdan ** manipulated by legacy implementations of sqlite3changeset_invert(),
176e3ca3831Sdan ** concat() and similar.
177e3ca3831Sdan **
178e3ca3831Sdan ** The sqlite3changeset_apply() function automatically converts the
179e3ca3831Sdan ** zero-length blob back to a NULL value when updating the sqlite_stat1
180e3ca3831Sdan ** table. However, if the application calls sqlite3changeset_new(),
181e3ca3831Sdan ** sqlite3changeset_old() or sqlite3changeset_conflict on a changeset
182e3ca3831Sdan ** iterator directly (including on a changeset iterator passed to a
183e3ca3831Sdan ** conflict-handler callback) then the X'' value is returned. The application
184e3ca3831Sdan ** must translate X'' to NULL itself if required.
185cae5b9feSdan **
186cae5b9feSdan ** Legacy (older than 3.22.0) versions of the sessions module cannot capture
187cae5b9feSdan ** changes made to the sqlite_stat1 table. Legacy versions of the
188cae5b9feSdan ** sqlite3changeset_apply() function silently ignore any modifications to the
189cae5b9feSdan ** sqlite_stat1 table that are part of a changeset or patchset.
1904fccf43aSdan */
1914fccf43aSdan int sqlite3session_attach(
1924fccf43aSdan   sqlite3_session *pSession,      /* Session object */
1934fccf43aSdan   const char *zTab                /* Table name */
1944fccf43aSdan );
1954fccf43aSdan 
1964fccf43aSdan /*
1977531a5a3Sdan ** CAPI3REF: Set a table filter on a Session Object.
198bda30ce4Sdrh ** METHOD: sqlite3_session
1997531a5a3Sdan **
2007531a5a3Sdan ** The second argument (xFilter) is the "filter callback". For changes to rows
201ed204d1fSdrh ** in tables that are not attached to the Session object, the filter is called
2027531a5a3Sdan ** to determine whether changes to the table's rows should be tracked or not.
203*2bbcaee8Sdrh ** If xFilter returns 0, changes are not tracked. Note that once a table is
2047531a5a3Sdan ** attached, xFilter will not be called again.
2057531a5a3Sdan */
2067531a5a3Sdan void sqlite3session_table_filter(
2077531a5a3Sdan   sqlite3_session *pSession,      /* Session object */
2087531a5a3Sdan   int(*xFilter)(
2097531a5a3Sdan     void *pCtx,                   /* Copy of third arg to _filter_table() */
2107531a5a3Sdan     const char *zTab              /* Table name */
2117531a5a3Sdan   ),
2127531a5a3Sdan   void *pCtx                      /* First argument passed to xFilter */
2137531a5a3Sdan );
2147531a5a3Sdan 
2157531a5a3Sdan /*
216a2df3d9fSdan ** CAPI3REF: Generate A Changeset From A Session Object
217bda30ce4Sdrh ** METHOD: sqlite3_session
218a2df3d9fSdan **
21937db03bfSdan ** Obtain a changeset containing changes to the tables attached to the
22037db03bfSdan ** session object passed as the first argument. If successful,
22137db03bfSdan ** set *ppChangeset to point to a buffer containing the changeset
22237db03bfSdan ** and *pnChangeset to the size of the changeset in bytes before returning
22337db03bfSdan ** SQLITE_OK. If an error occurs, set both *ppChangeset and *pnChangeset to
22437db03bfSdan ** zero and return an SQLite error code.
2254fccf43aSdan **
226c21111daSdan ** A changeset consists of zero or more INSERT, UPDATE and/or DELETE changes,
227c21111daSdan ** each representing a change to a single row of an attached table. An INSERT
228c21111daSdan ** change contains the values of each field of a new database row. A DELETE
229c21111daSdan ** contains the original values of each field of a deleted database row. An
230c21111daSdan ** UPDATE change contains the original values of each field of an updated
231c21111daSdan ** database row along with the updated values for each updated non-primary-key
232c21111daSdan ** column. It is not possible for an UPDATE change to represent a change that
233c21111daSdan ** modifies the values of primary key columns. If such a change is made, it
234c21111daSdan ** is represented in a changeset as a DELETE followed by an INSERT.
235c21111daSdan **
23627453faeSdan ** Changes are not recorded for rows that have NULL values stored in one or
23727453faeSdan ** more of their PRIMARY KEY columns. If such a row is inserted or deleted,
23827453faeSdan ** no corresponding change is present in the changesets returned by this
23927453faeSdan ** function. If an existing row with one or more NULL values stored in
24027453faeSdan ** PRIMARY KEY columns is updated so that all PRIMARY KEY columns are non-NULL,
24127453faeSdan ** only an INSERT is appears in the changeset. Similarly, if an existing row
24227453faeSdan ** with non-NULL PRIMARY KEY values is updated so that one or more of its
24327453faeSdan ** PRIMARY KEY columns are set to NULL, the resulting changeset contains a
24427453faeSdan ** DELETE change only.
24527453faeSdan **
246c21111daSdan ** The contents of a changeset may be traversed using an iterator created
247c21111daSdan ** using the [sqlite3changeset_start()] API. A changeset may be applied to
248a2df3d9fSdan ** a database with a compatible schema using the [sqlite3changeset_apply()]
249c21111daSdan ** API.
250c21111daSdan **
2516c39e6a8Sdan ** Within a changeset generated by this function, all changes related to a
2526c39e6a8Sdan ** single table are grouped together. In other words, when iterating through
2536c39e6a8Sdan ** a changeset or when applying a changeset to a database, all changes related
2546c39e6a8Sdan ** to a single table are processed before moving on to the next table. Tables
2556c39e6a8Sdan ** are sorted in the same order in which they were attached (or auto-attached)
2566c39e6a8Sdan ** to the sqlite3_session object. The order in which the changes related to
2576c39e6a8Sdan ** a single table are stored is undefined.
2586c39e6a8Sdan **
25937db03bfSdan ** Following a successful call to this function, it is the responsibility of
26037db03bfSdan ** the caller to eventually free the buffer that *ppChangeset points to using
26137db03bfSdan ** [sqlite3_free()].
262c21111daSdan **
263a2df3d9fSdan ** <h3>Changeset Generation</h3>
264c21111daSdan **
265c21111daSdan ** Once a table has been attached to a session object, the session object
266c21111daSdan ** records the primary key values of all new rows inserted into the table.
267c21111daSdan ** It also records the original primary key and other column values of any
268c21111daSdan ** deleted or updated rows. For each unique primary key value, data is only
269c21111daSdan ** recorded once - the first time a row with said primary key is inserted,
270c21111daSdan ** updated or deleted in the lifetime of the session.
271c21111daSdan **
27227453faeSdan ** There is one exception to the previous paragraph: when a row is inserted,
273157546f4Sshaneh ** updated or deleted, if one or more of its primary key columns contain a
27427453faeSdan ** NULL value, no record of the change is made.
27527453faeSdan **
276c21111daSdan ** The session object therefore accumulates two types of records - those
277c21111daSdan ** that consist of primary key values only (created when the user inserts
278c21111daSdan ** a new record) and those that consist of the primary key values and the
279c21111daSdan ** original values of other table columns (created when the users deletes
280c21111daSdan ** or updates a record).
281c21111daSdan **
282c21111daSdan ** When this function is called, the requested changeset is created using
283c21111daSdan ** both the accumulated records and the current contents of the database
284c21111daSdan ** file. Specifically:
285c21111daSdan **
286c21111daSdan ** <ul>
287c21111daSdan **   <li> For each record generated by an insert, the database is queried
288c21111daSdan **        for a row with a matching primary key. If one is found, an INSERT
289c21111daSdan **        change is added to the changeset. If no such row is found, no change
290c21111daSdan **        is added to the changeset.
291c21111daSdan **
292c21111daSdan **   <li> For each record generated by an update or delete, the database is
293c21111daSdan **        queried for a row with a matching primary key. If such a row is
294c21111daSdan **        found and one or more of the non-primary key fields have been
295c21111daSdan **        modified from their original values, an UPDATE change is added to
296c21111daSdan **        the changeset. Or, if no such row is found in the table, a DELETE
297c21111daSdan **        change is added to the changeset. If there is a row with a matching
298c21111daSdan **        primary key in the database, but all fields contain their original
299c21111daSdan **        values, no change is added to the changeset.
300c21111daSdan ** </ul>
301c21111daSdan **
302c21111daSdan ** This means, amongst other things, that if a row is inserted and then later
303157546f4Sshaneh ** deleted while a session object is active, neither the insert nor the delete
304c21111daSdan ** will be present in the changeset. Or if a row is deleted and then later a
305c21111daSdan ** row with the same primary key values inserted while a session object is
306c21111daSdan ** active, the resulting changeset will contain an UPDATE change instead of
307c21111daSdan ** a DELETE and an INSERT.
308c21111daSdan **
309c21111daSdan ** When a session object is disabled (see the [sqlite3session_enable()] API),
310c21111daSdan ** it does not accumulate records when rows are inserted, updated or deleted.
311c21111daSdan ** This may appear to have some counter-intuitive effects if a single row
312c21111daSdan ** is written to more than once during a session. For example, if a row
313c21111daSdan ** is inserted while a session object is enabled, then later deleted while
314c21111daSdan ** the same session object is disabled, no INSERT record will appear in the
315c21111daSdan ** changeset, even though the delete took place while the session was disabled.
316c21111daSdan ** Or, if one field of a row is updated while a session is disabled, and
317c21111daSdan ** another field of the same row is updated while the session is enabled, the
318c21111daSdan ** resulting changeset will contain an UPDATE change that updates both fields.
3194fccf43aSdan */
3204fccf43aSdan int sqlite3session_changeset(
3214fccf43aSdan   sqlite3_session *pSession,      /* Session object */
3224fccf43aSdan   int *pnChangeset,               /* OUT: Size of buffer at *ppChangeset */
3234fccf43aSdan   void **ppChangeset              /* OUT: Buffer containing changeset */
3244fccf43aSdan );
3254fccf43aSdan 
326cf8e9144Sdan /*
327cf8e9144Sdan ** CAPI3REF: Load The Difference Between Tables Into A Session
328bda30ce4Sdrh ** METHOD: sqlite3_session
329cf8e9144Sdan **
330cf8e9144Sdan ** If it is not already attached to the session object passed as the first
331cf8e9144Sdan ** argument, this function attaches table zTbl in the same manner as the
332cf8e9144Sdan ** [sqlite3session_attach()] function. If zTbl does not exist, or if it
333cf8e9144Sdan ** does not have a primary key, this function is a no-op (but does not return
334cf8e9144Sdan ** an error).
335cf8e9144Sdan **
336cf8e9144Sdan ** Argument zFromDb must be the name of a database ("main", "temp" etc.)
337cf8e9144Sdan ** attached to the same database handle as the session object that contains
338cf8e9144Sdan ** a table compatible with the table attached to the session by this function.
339cf8e9144Sdan ** A table is considered compatible if it:
340cf8e9144Sdan **
341cf8e9144Sdan ** <ul>
342cf8e9144Sdan **   <li> Has the same name,
343cf8e9144Sdan **   <li> Has the same set of columns declared in the same order, and
344cf8e9144Sdan **   <li> Has the same PRIMARY KEY definition.
345cf8e9144Sdan ** </ul>
346cf8e9144Sdan **
347b9db9099Sdan ** If the tables are not compatible, SQLITE_SCHEMA is returned. If the tables
348b9db9099Sdan ** are compatible but do not have any PRIMARY KEY columns, it is not an error
349b9db9099Sdan ** but no changes are added to the session object. As with other session
350b9db9099Sdan ** APIs, tables without PRIMARY KEYs are simply ignored.
351b9db9099Sdan **
352cf8e9144Sdan ** This function adds a set of changes to the session object that could be
353cf8e9144Sdan ** used to update the table in database zFrom (call this the "from-table")
354cf8e9144Sdan ** so that its content is the same as the table attached to the session
355cf8e9144Sdan ** object (call this the "to-table"). Specifically:
356cf8e9144Sdan **
357cf8e9144Sdan ** <ul>
358cf8e9144Sdan **   <li> For each row (primary key) that exists in the to-table but not in
359cf8e9144Sdan **     the from-table, an INSERT record is added to the session object.
360cf8e9144Sdan **
361cf8e9144Sdan **   <li> For each row (primary key) that exists in the to-table but not in
362cf8e9144Sdan **     the from-table, a DELETE record is added to the session object.
363cf8e9144Sdan **
364cf8e9144Sdan **   <li> For each row (primary key) that exists in both tables, but features
365ff677b20Sdan **     different non-PK values in each, an UPDATE record is added to the
366ff677b20Sdan **     session.
367cf8e9144Sdan ** </ul>
368cf8e9144Sdan **
369cf8e9144Sdan ** To clarify, if this function is called and then a changeset constructed
370cf8e9144Sdan ** using [sqlite3session_changeset()], then after applying that changeset to
371cf8e9144Sdan ** database zFrom the contents of the two compatible tables would be
372cf8e9144Sdan ** identical.
373cf8e9144Sdan **
374cf8e9144Sdan ** It an error if database zFrom does not exist or does not contain the
375cf8e9144Sdan ** required compatible table.
376cf8e9144Sdan **
377*2bbcaee8Sdrh ** If the operation is successful, SQLITE_OK is returned. Otherwise, an SQLite
378cf8e9144Sdan ** error code. In this case, if argument pzErrMsg is not NULL, *pzErrMsg
379cf8e9144Sdan ** may be set to point to a buffer containing an English language error
380cf8e9144Sdan ** message. It is the responsibility of the caller to free this buffer using
381cf8e9144Sdan ** sqlite3_free().
382cf8e9144Sdan */
383cf8e9144Sdan int sqlite3session_diff(
384cf8e9144Sdan   sqlite3_session *pSession,
385cf8e9144Sdan   const char *zFromDb,
386cf8e9144Sdan   const char *zTbl,
387cf8e9144Sdan   char **pzErrMsg
388cf8e9144Sdan );
389cf8e9144Sdan 
390ef7a6304Sdan 
391ef7a6304Sdan /*
39273b3c055Sdan ** CAPI3REF: Generate A Patchset From A Session Object
393bda30ce4Sdrh ** METHOD: sqlite3_session
394a71d2371Sdan **
395a71d2371Sdan ** The differences between a patchset and a changeset are that:
396a71d2371Sdan **
397a71d2371Sdan ** <ul>
398a71d2371Sdan **   <li> DELETE records consist of the primary key fields only. The
399a71d2371Sdan **        original values of other fields are omitted.
400a71d2371Sdan **   <li> The original values of any modified fields are omitted from
401a71d2371Sdan **        UPDATE records.
402a71d2371Sdan ** </ul>
403a71d2371Sdan **
404a71d2371Sdan ** A patchset blob may be used with up to date versions of all
405a71d2371Sdan ** sqlite3changeset_xxx API functions except for sqlite3changeset_invert(),
406a71d2371Sdan ** which returns SQLITE_CORRUPT if it is passed a patchset. Similarly,
407a71d2371Sdan ** attempting to use a patchset blob with old versions of the
408a71d2371Sdan ** sqlite3changeset_xxx APIs also provokes an SQLITE_CORRUPT error.
409a71d2371Sdan **
410a71d2371Sdan ** Because the non-primary key "old.*" fields are omitted, no
411a71d2371Sdan ** SQLITE_CHANGESET_DATA conflicts can be detected or reported if a patchset
412a71d2371Sdan ** is passed to the sqlite3changeset_apply() API. Other conflict types work
413a71d2371Sdan ** in the same way as for changesets.
4146c39e6a8Sdan **
4156c39e6a8Sdan ** Changes within a patchset are ordered in the same way as for changesets
4166c39e6a8Sdan ** generated by the sqlite3session_changeset() function (i.e. all changes for
4176c39e6a8Sdan ** a single table are grouped together, tables appear in the order in which
4186c39e6a8Sdan ** they were attached to the session object).
41973b3c055Sdan */
42073b3c055Sdan int sqlite3session_patchset(
42173b3c055Sdan   sqlite3_session *pSession,      /* Session object */
4223dfbe9b3Smistachkin   int *pnPatchset,                /* OUT: Size of buffer at *ppPatchset */
4233dfbe9b3Smistachkin   void **ppPatchset               /* OUT: Buffer containing patchset */
42473b3c055Sdan );
42573b3c055Sdan 
42673b3c055Sdan /*
427b69ec348Sdan ** CAPI3REF: Test if a changeset has recorded any changes.
428b69ec348Sdan **
429b69ec348Sdan ** Return non-zero if no changes to attached tables have been recorded by
430b69ec348Sdan ** the session object passed as the first argument. Otherwise, if one or
431b69ec348Sdan ** more changes have been recorded, return zero.
432b69ec348Sdan **
433b69ec348Sdan ** Even if this function returns zero, it is possible that calling
434b69ec348Sdan ** [sqlite3session_changeset()] on the session handle may still return a
435b69ec348Sdan ** changeset that contains no changes. This can happen when a row in
436b69ec348Sdan ** an attached table is modified and then later on the original values
437b69ec348Sdan ** are restored. However, if this function returns non-zero, then it is
438b69ec348Sdan ** guaranteed that a call to sqlite3session_changeset() will return a
439b69ec348Sdan ** changeset containing zero changes.
440b69ec348Sdan */
441b69ec348Sdan int sqlite3session_isempty(sqlite3_session *pSession);
442b69ec348Sdan 
443b69ec348Sdan /*
444a2df3d9fSdan ** CAPI3REF: Create An Iterator To Traverse A Changeset
445bda30ce4Sdrh ** CONSTRUCTOR: sqlite3_changeset_iter
446a2df3d9fSdan **
4474fccf43aSdan ** Create an iterator used to iterate through the contents of a changeset.
448c21111daSdan ** If successful, *pp is set to point to the iterator handle and SQLITE_OK
449c21111daSdan ** is returned. Otherwise, if an error occurs, *pp is set to zero and an
450c21111daSdan ** SQLite error code is returned.
451c21111daSdan **
452c21111daSdan ** The following functions can be used to advance and query a changeset
453c21111daSdan ** iterator created by this function:
454c21111daSdan **
455c21111daSdan ** <ul>
456c21111daSdan **   <li> [sqlite3changeset_next()]
457c21111daSdan **   <li> [sqlite3changeset_op()]
458c21111daSdan **   <li> [sqlite3changeset_new()]
459c21111daSdan **   <li> [sqlite3changeset_old()]
460c21111daSdan ** </ul>
461c21111daSdan **
462c21111daSdan ** It is the responsibility of the caller to eventually destroy the iterator
463c21111daSdan ** by passing it to [sqlite3changeset_finalize()]. The buffer containing the
464c21111daSdan ** changeset (pChangeset) must remain valid until after the iterator is
465c21111daSdan ** destroyed.
466b69ec348Sdan **
467b69ec348Sdan ** Assuming the changeset blob was created by one of the
468b69ec348Sdan ** [sqlite3session_changeset()], [sqlite3changeset_concat()] or
469a93166e0Sdan ** [sqlite3changeset_invert()] functions, all changes within the changeset
470a93166e0Sdan ** that apply to a single table are grouped together. This means that when
471a93166e0Sdan ** an application iterates through a changeset using an iterator created by
4725e769a50Sdrh ** this function, all changes that relate to a single table are visited
473a93166e0Sdan ** consecutively. There is no chance that the iterator will visit a change
474a93166e0Sdan ** the applies to table X, then one for table Y, and then later on visit
475a93166e0Sdan ** another change for table X.
47646de0728Sdan **
47746de0728Sdan ** The behavior of sqlite3changeset_start_v2() and its streaming equivalent
47846de0728Sdan ** may be modified by passing a combination of
47946de0728Sdan ** [SQLITE_CHANGESETSTART_INVERT | supported flags] as the 4th parameter.
48046de0728Sdan **
48146de0728Sdan ** Note that the sqlite3changeset_start_v2() API is still <b>experimental</b>
48246de0728Sdan ** and therefore subject to change.
4834fccf43aSdan */
4844fccf43aSdan int sqlite3changeset_start(
485c21111daSdan   sqlite3_changeset_iter **pp,    /* OUT: New changeset iterator handle */
486c21111daSdan   int nChangeset,                 /* Size of changeset blob in bytes */
487c21111daSdan   void *pChangeset                /* Pointer to blob containing changeset */
4884fccf43aSdan );
48946de0728Sdan int sqlite3changeset_start_v2(
49046de0728Sdan   sqlite3_changeset_iter **pp,    /* OUT: New changeset iterator handle */
49146de0728Sdan   int nChangeset,                 /* Size of changeset blob in bytes */
49246de0728Sdan   void *pChangeset,               /* Pointer to blob containing changeset */
49346de0728Sdan   int flags                       /* SESSION_CHANGESETSTART_* flags */
49446de0728Sdan );
49546de0728Sdan 
49646de0728Sdan /*
49746de0728Sdan ** CAPI3REF: Flags for sqlite3changeset_start_v2
49846de0728Sdan **
49946de0728Sdan ** The following flags may passed via the 4th parameter to
50046de0728Sdan ** [sqlite3changeset_start_v2] and [sqlite3changeset_start_v2_strm]:
50146de0728Sdan **
50246de0728Sdan ** <dt>SQLITE_CHANGESETAPPLY_INVERT <dd>
50346de0728Sdan **   Invert the changeset while iterating through it. This is equivalent to
50446de0728Sdan **   inverting a changeset using sqlite3changeset_invert() before applying it.
50546de0728Sdan **   It is an error to specify this flag with a patchset.
50646de0728Sdan */
50746de0728Sdan #define SQLITE_CHANGESETSTART_INVERT        0x0002
5084fccf43aSdan 
509ef7a6304Sdan 
510ef7a6304Sdan /*
511a2df3d9fSdan ** CAPI3REF: Advance A Changeset Iterator
512bda30ce4Sdrh ** METHOD: sqlite3_changeset_iter
513a2df3d9fSdan **
514*2bbcaee8Sdrh ** This function may only be used with iterators created by the function
515c21111daSdan ** [sqlite3changeset_start()]. If it is called on an iterator passed to
516c21111daSdan ** a conflict-handler callback by [sqlite3changeset_apply()], SQLITE_MISUSE
517c21111daSdan ** is returned and the call has no effect.
5184fccf43aSdan **
519c21111daSdan ** Immediately after an iterator is created by sqlite3changeset_start(), it
520c21111daSdan ** does not point to any change in the changeset. Assuming the changeset
521c21111daSdan ** is not empty, the first call to this function advances the iterator to
522c21111daSdan ** point to the first change in the changeset. Each subsequent call advances
523c21111daSdan ** the iterator to point to the next change in the changeset (if any). If
524c21111daSdan ** no error occurs and the iterator points to a valid change after a call
525c21111daSdan ** to sqlite3changeset_next() has advanced it, SQLITE_ROW is returned.
526c21111daSdan ** Otherwise, if all changes in the changeset have already been visited,
527c21111daSdan ** SQLITE_DONE is returned.
528c21111daSdan **
529c21111daSdan ** If an error occurs, an SQLite error code is returned. Possible error
530c21111daSdan ** codes include SQLITE_CORRUPT (if the changeset buffer is corrupt) or
531c21111daSdan ** SQLITE_NOMEM.
5324fccf43aSdan */
5334fccf43aSdan int sqlite3changeset_next(sqlite3_changeset_iter *pIter);
5344fccf43aSdan 
5354fccf43aSdan /*
536a2df3d9fSdan ** CAPI3REF: Obtain The Current Operation From A Changeset Iterator
537bda30ce4Sdrh ** METHOD: sqlite3_changeset_iter
538a2df3d9fSdan **
539c21111daSdan ** The pIter argument passed to this function may either be an iterator
540c21111daSdan ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
541c21111daSdan ** created by [sqlite3changeset_start()]. In the latter case, the most recent
54277e65004Sdan ** call to [sqlite3changeset_next()] must have returned [SQLITE_ROW]. If this
54377e65004Sdan ** is not the case, this function returns [SQLITE_MISUSE].
544c21111daSdan **
545c21111daSdan ** If argument pzTab is not NULL, then *pzTab is set to point to a
546c21111daSdan ** nul-terminated utf-8 encoded string containing the name of the table
547c21111daSdan ** affected by the current change. The buffer remains valid until either
548c21111daSdan ** sqlite3changeset_next() is called on the iterator or until the
549c21111daSdan ** conflict-handler function returns. If pnCol is not NULL, then *pnCol is
550b4480e94Sdan ** set to the number of columns in the table affected by the change. If
551e4c26c41Sdrh ** pbIndirect is not NULL, then *pbIndirect is set to true (1) if the change
552b4480e94Sdan ** is an indirect change, or false (0) otherwise. See the documentation for
553b4480e94Sdan ** [sqlite3session_indirect()] for a description of direct and indirect
554b4480e94Sdan ** changes. Finally, if pOp is not NULL, then *pOp is set to one of
555b4480e94Sdan ** [SQLITE_INSERT], [SQLITE_DELETE] or [SQLITE_UPDATE], depending on the
556b4480e94Sdan ** type of change that the iterator currently points to.
557c21111daSdan **
558c21111daSdan ** If no error occurs, SQLITE_OK is returned. If an error does occur, an
559c21111daSdan ** SQLite error code is returned. The values of the output variables may not
560c21111daSdan ** be trusted in this case.
5614fccf43aSdan */
5624fccf43aSdan int sqlite3changeset_op(
5634fccf43aSdan   sqlite3_changeset_iter *pIter,  /* Iterator object */
5644fccf43aSdan   const char **pzTab,             /* OUT: Pointer to table name */
5654fccf43aSdan   int *pnCol,                     /* OUT: Number of columns in table */
566b4480e94Sdan   int *pOp,                       /* OUT: SQLITE_INSERT, DELETE or UPDATE */
567b4480e94Sdan   int *pbIndirect                 /* OUT: True for an 'indirect' change */
5684fccf43aSdan );
569c21111daSdan 
570c21111daSdan /*
571244593c8Sdan ** CAPI3REF: Obtain The Primary Key Definition Of A Table
572bda30ce4Sdrh ** METHOD: sqlite3_changeset_iter
573244593c8Sdan **
574244593c8Sdan ** For each modified table, a changeset includes the following:
575244593c8Sdan **
576244593c8Sdan ** <ul>
577244593c8Sdan **   <li> The number of columns in the table, and
578244593c8Sdan **   <li> Which of those columns make up the tables PRIMARY KEY.
579244593c8Sdan ** </ul>
580244593c8Sdan **
581244593c8Sdan ** This function is used to find which columns comprise the PRIMARY KEY of
582244593c8Sdan ** the table modified by the change that iterator pIter currently points to.
583244593c8Sdan ** If successful, *pabPK is set to point to an array of nCol entries, where
584244593c8Sdan ** nCol is the number of columns in the table. Elements of *pabPK are set to
585244593c8Sdan ** 0x01 if the corresponding column is part of the tables primary key, or
586244593c8Sdan ** 0x00 if it is not.
587244593c8Sdan **
588ed204d1fSdrh ** If argument pnCol is not NULL, then *pnCol is set to the number of columns
589244593c8Sdan ** in the table.
590244593c8Sdan **
591244593c8Sdan ** If this function is called when the iterator does not point to a valid
592244593c8Sdan ** entry, SQLITE_MISUSE is returned and the output variables zeroed. Otherwise,
593244593c8Sdan ** SQLITE_OK is returned and the output variables populated as described
594244593c8Sdan ** above.
595244593c8Sdan */
596244593c8Sdan int sqlite3changeset_pk(
597244593c8Sdan   sqlite3_changeset_iter *pIter,  /* Iterator object */
598244593c8Sdan   unsigned char **pabPK,          /* OUT: Array of boolean - true for PK cols */
599244593c8Sdan   int *pnCol                      /* OUT: Number of entries in output array */
600244593c8Sdan );
601244593c8Sdan 
602244593c8Sdan /*
603a2df3d9fSdan ** CAPI3REF: Obtain old.* Values From A Changeset Iterator
604bda30ce4Sdrh ** METHOD: sqlite3_changeset_iter
605a2df3d9fSdan **
606c21111daSdan ** The pIter argument passed to this function may either be an iterator
607c21111daSdan ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
608c21111daSdan ** created by [sqlite3changeset_start()]. In the latter case, the most recent
609c21111daSdan ** call to [sqlite3changeset_next()] must have returned SQLITE_ROW.
610c21111daSdan ** Furthermore, it may only be called if the type of change that the iterator
61177e65004Sdan ** currently points to is either [SQLITE_DELETE] or [SQLITE_UPDATE]. Otherwise,
61277e65004Sdan ** this function returns [SQLITE_MISUSE] and sets *ppValue to NULL.
613c21111daSdan **
614c21111daSdan ** Argument iVal must be greater than or equal to 0, and less than the number
615c21111daSdan ** of columns in the table affected by the current change. Otherwise,
61677e65004Sdan ** [SQLITE_RANGE] is returned and *ppValue is set to NULL.
617c21111daSdan **
618c21111daSdan ** If successful, this function sets *ppValue to point to a protected
619c21111daSdan ** sqlite3_value object containing the iVal'th value from the vector of
620c21111daSdan ** original row values stored as part of the UPDATE or DELETE change and
621c21111daSdan ** returns SQLITE_OK. The name of the function comes from the fact that this
622c21111daSdan ** is similar to the "old.*" columns available to update or delete triggers.
623c21111daSdan **
624c21111daSdan ** If some other error occurs (e.g. an OOM condition), an SQLite error code
625c21111daSdan ** is returned and *ppValue is set to NULL.
626c21111daSdan */
6274fccf43aSdan int sqlite3changeset_old(
628296c7658Sdan   sqlite3_changeset_iter *pIter,  /* Changeset iterator */
629296c7658Sdan   int iVal,                       /* Column number */
6304fccf43aSdan   sqlite3_value **ppValue         /* OUT: Old value (or NULL pointer) */
6314fccf43aSdan );
632c21111daSdan 
633c21111daSdan /*
634a2df3d9fSdan ** CAPI3REF: Obtain new.* Values From A Changeset Iterator
635bda30ce4Sdrh ** METHOD: sqlite3_changeset_iter
636a2df3d9fSdan **
637c21111daSdan ** The pIter argument passed to this function may either be an iterator
638c21111daSdan ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
639c21111daSdan ** created by [sqlite3changeset_start()]. In the latter case, the most recent
640c21111daSdan ** call to [sqlite3changeset_next()] must have returned SQLITE_ROW.
641c21111daSdan ** Furthermore, it may only be called if the type of change that the iterator
64277e65004Sdan ** currently points to is either [SQLITE_UPDATE] or [SQLITE_INSERT]. Otherwise,
64377e65004Sdan ** this function returns [SQLITE_MISUSE] and sets *ppValue to NULL.
644c21111daSdan **
645c21111daSdan ** Argument iVal must be greater than or equal to 0, and less than the number
646c21111daSdan ** of columns in the table affected by the current change. Otherwise,
64777e65004Sdan ** [SQLITE_RANGE] is returned and *ppValue is set to NULL.
648c21111daSdan **
649c21111daSdan ** If successful, this function sets *ppValue to point to a protected
650c21111daSdan ** sqlite3_value object containing the iVal'th value from the vector of
651c21111daSdan ** new row values stored as part of the UPDATE or INSERT change and
652c21111daSdan ** returns SQLITE_OK. If the change is an UPDATE and does not include
653c21111daSdan ** a new value for the requested column, *ppValue is set to NULL and
654c21111daSdan ** SQLITE_OK returned. The name of the function comes from the fact that
655c21111daSdan ** this is similar to the "new.*" columns available to update or delete
656c21111daSdan ** triggers.
657c21111daSdan **
658c21111daSdan ** If some other error occurs (e.g. an OOM condition), an SQLite error code
659c21111daSdan ** is returned and *ppValue is set to NULL.
660c21111daSdan */
6614fccf43aSdan int sqlite3changeset_new(
662296c7658Sdan   sqlite3_changeset_iter *pIter,  /* Changeset iterator */
663296c7658Sdan   int iVal,                       /* Column number */
6644fccf43aSdan   sqlite3_value **ppValue         /* OUT: New value (or NULL pointer) */
6654fccf43aSdan );
666296c7658Sdan 
667d5f0767cSdan /*
668a2df3d9fSdan ** CAPI3REF: Obtain Conflicting Row Values From A Changeset Iterator
669bda30ce4Sdrh ** METHOD: sqlite3_changeset_iter
670a2df3d9fSdan **
671c21111daSdan ** This function should only be used with iterator objects passed to a
672c21111daSdan ** conflict-handler callback by [sqlite3changeset_apply()] with either
67377e65004Sdan ** [SQLITE_CHANGESET_DATA] or [SQLITE_CHANGESET_CONFLICT]. If this function
67477e65004Sdan ** is called on any other iterator, [SQLITE_MISUSE] is returned and *ppValue
675c21111daSdan ** is set to NULL.
676d5f0767cSdan **
677c21111daSdan ** Argument iVal must be greater than or equal to 0, and less than the number
678c21111daSdan ** of columns in the table affected by the current change. Otherwise,
67977e65004Sdan ** [SQLITE_RANGE] is returned and *ppValue is set to NULL.
680c21111daSdan **
681c21111daSdan ** If successful, this function sets *ppValue to point to a protected
682c21111daSdan ** sqlite3_value object containing the iVal'th value from the
683c21111daSdan ** "conflicting row" associated with the current conflict-handler callback
684c21111daSdan ** and returns SQLITE_OK.
685c21111daSdan **
686c21111daSdan ** If some other error occurs (e.g. an OOM condition), an SQLite error code
687c21111daSdan ** is returned and *ppValue is set to NULL.
688d5f0767cSdan */
689d5f0767cSdan int sqlite3changeset_conflict(
690296c7658Sdan   sqlite3_changeset_iter *pIter,  /* Changeset iterator */
691296c7658Sdan   int iVal,                       /* Column number */
692d5f0767cSdan   sqlite3_value **ppValue         /* OUT: Value from conflicting row */
693d5f0767cSdan );
694d5f0767cSdan 
695cb3e4b79Sdan /*
696cb3e4b79Sdan ** CAPI3REF: Determine The Number Of Foreign Key Constraint Violations
697bda30ce4Sdrh ** METHOD: sqlite3_changeset_iter
698cb3e4b79Sdan **
699cb3e4b79Sdan ** This function may only be called with an iterator passed to an
700cb3e4b79Sdan ** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case
701cb3e4b79Sdan ** it sets the output variable to the total number of known foreign key
702cb3e4b79Sdan ** violations in the destination database and returns SQLITE_OK.
703cb3e4b79Sdan **
704cb3e4b79Sdan ** In all other cases this function returns SQLITE_MISUSE.
705cb3e4b79Sdan */
706cb3e4b79Sdan int sqlite3changeset_fk_conflicts(
707cb3e4b79Sdan   sqlite3_changeset_iter *pIter,  /* Changeset iterator */
708cb3e4b79Sdan   int *pnOut                      /* OUT: Number of FK violations */
709cb3e4b79Sdan );
710cb3e4b79Sdan 
7114fccf43aSdan 
7124fccf43aSdan /*
713157546f4Sshaneh ** CAPI3REF: Finalize A Changeset Iterator
714bda30ce4Sdrh ** METHOD: sqlite3_changeset_iter
715a2df3d9fSdan **
716a2df3d9fSdan ** This function is used to finalize an iterator allocated with
71777e65004Sdan ** [sqlite3changeset_start()].
7184fccf43aSdan **
719c21111daSdan ** This function should only be called on iterators created using the
720c21111daSdan ** [sqlite3changeset_start()] function. If an application calls this
721c21111daSdan ** function with an iterator passed to a conflict-handler by
72277e65004Sdan ** [sqlite3changeset_apply()], [SQLITE_MISUSE] is immediately returned and the
723c21111daSdan ** call has no effect.
724c21111daSdan **
725c21111daSdan ** If an error was encountered within a call to an sqlite3changeset_xxx()
72677e65004Sdan ** function (for example an [SQLITE_CORRUPT] in [sqlite3changeset_next()] or an
72777e65004Sdan ** [SQLITE_NOMEM] in [sqlite3changeset_new()]) then an error code corresponding
728c21111daSdan ** to that error is returned by this function. Otherwise, SQLITE_OK is
729c21111daSdan ** returned. This is to allow the following pattern (pseudo-code):
730c21111daSdan **
7316a8a629eSdrh ** <pre>
732c21111daSdan **   sqlite3changeset_start();
733c21111daSdan **   while( SQLITE_ROW==sqlite3changeset_next() ){
734c21111daSdan **     // Do something with change.
735c21111daSdan **   }
736c21111daSdan **   rc = sqlite3changeset_finalize();
737c21111daSdan **   if( rc!=SQLITE_OK ){
738c21111daSdan **     // An error has occurred
739c21111daSdan **   }
7406a8a629eSdrh ** </pre>
7414fccf43aSdan */
7424fccf43aSdan int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter);
7434fccf43aSdan 
74491ddd559Sdan /*
745a2df3d9fSdan ** CAPI3REF: Invert A Changeset
746a2df3d9fSdan **
7472635a3beSdan ** This function is used to "invert" a changeset object. Applying an inverted
7482635a3beSdan ** changeset to a database reverses the effects of applying the uninverted
7492635a3beSdan ** changeset. Specifically:
7502635a3beSdan **
7512635a3beSdan ** <ul>
7522635a3beSdan **   <li> Each DELETE change is changed to an INSERT, and
7532635a3beSdan **   <li> Each INSERT change is changed to a DELETE, and
7542635a3beSdan **   <li> For each UPDATE change, the old.* and new.* values are exchanged.
7552635a3beSdan ** </ul>
7562635a3beSdan **
7576c39e6a8Sdan ** This function does not change the order in which changes appear within
7586c39e6a8Sdan ** the changeset. It merely reverses the sense of each individual change.
7596c39e6a8Sdan **
7602635a3beSdan ** If successful, a pointer to a buffer containing the inverted changeset
7612635a3beSdan ** is stored in *ppOut, the size of the same buffer is stored in *pnOut, and
7622635a3beSdan ** SQLITE_OK is returned. If an error occurs, both *pnOut and *ppOut are
7632635a3beSdan ** zeroed and an SQLite error code returned.
7642635a3beSdan **
7652635a3beSdan ** It is the responsibility of the caller to eventually call sqlite3_free()
7662635a3beSdan ** on the *ppOut pointer to free the buffer allocation following a successful
7672635a3beSdan ** call to this function.
768f51e5f6cSdan **
769f51e5f6cSdan ** WARNING/TODO: This function currently assumes that the input is a valid
770f51e5f6cSdan ** changeset. If it is not, the results are undefined.
77191ddd559Sdan */
77291ddd559Sdan int sqlite3changeset_invert(
773cfec7eeeSdan   int nIn, const void *pIn,       /* Input changeset */
77491ddd559Sdan   int *pnOut, void **ppOut        /* OUT: Inverse of input */
77591ddd559Sdan );
77691ddd559Sdan 
7776cda207fSdan /*
77829e03e97Sdan ** CAPI3REF: Concatenate Two Changeset Objects
77929e03e97Sdan **
78029e03e97Sdan ** This function is used to concatenate two changesets, A and B, into a
78129e03e97Sdan ** single changeset. The result is a changeset equivalent to applying
78229e03e97Sdan ** changeset A followed by changeset B.
78329e03e97Sdan **
7845898ad69Sdan ** This function combines the two input changesets using an
7855898ad69Sdan ** sqlite3_changegroup object. Calling it produces similar results as the
7865898ad69Sdan ** following code fragment:
78729e03e97Sdan **
7886a8a629eSdrh ** <pre>
7895898ad69Sdan **   sqlite3_changegroup *pGrp;
7905898ad69Sdan **   rc = sqlite3_changegroup_new(&pGrp);
7915898ad69Sdan **   if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nA, pA);
7925898ad69Sdan **   if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nB, pB);
7935898ad69Sdan **   if( rc==SQLITE_OK ){
7945898ad69Sdan **     rc = sqlite3changegroup_output(pGrp, pnOut, ppOut);
7955898ad69Sdan **   }else{
7965898ad69Sdan **     *ppOut = 0;
7975898ad69Sdan **     *pnOut = 0;
7985898ad69Sdan **   }
7996a8a629eSdrh ** </pre>
80029e03e97Sdan **
8015898ad69Sdan ** Refer to the sqlite3_changegroup documentation below for details.
8026cda207fSdan */
8035d607a6eSdan int sqlite3changeset_concat(
80429e03e97Sdan   int nA,                         /* Number of bytes in buffer pA */
80529e03e97Sdan   void *pA,                       /* Pointer to buffer containing changeset A */
80629e03e97Sdan   int nB,                         /* Number of bytes in buffer pB */
80729e03e97Sdan   void *pB,                       /* Pointer to buffer containing changeset B */
80829e03e97Sdan   int *pnOut,                     /* OUT: Number of bytes in output changeset */
80929e03e97Sdan   void **ppOut                    /* OUT: Buffer containing output changeset */
8105d607a6eSdan );
8115d607a6eSdan 
8125898ad69Sdan 
8135898ad69Sdan /*
81466501908Sdan ** CAPI3REF: Changegroup Handle
815bda30ce4Sdrh **
816bda30ce4Sdrh ** A changegroup is an object used to combine two or more
817bda30ce4Sdrh ** [changesets] or [patchsets]
8185898ad69Sdan */
8195898ad69Sdan typedef struct sqlite3_changegroup sqlite3_changegroup;
8205898ad69Sdan 
8215898ad69Sdan /*
82266501908Sdan ** CAPI3REF: Create A New Changegroup Object
823bda30ce4Sdrh ** CONSTRUCTOR: sqlite3_changegroup
8245898ad69Sdan **
8255898ad69Sdan ** An sqlite3_changegroup object is used to combine two or more changesets
8265898ad69Sdan ** (or patchsets) into a single changeset (or patchset). A single changegroup
8275898ad69Sdan ** object may combine changesets or patchsets, but not both. The output is
8285898ad69Sdan ** always in the same format as the input.
8295898ad69Sdan **
8305898ad69Sdan ** If successful, this function returns SQLITE_OK and populates (*pp) with
8315898ad69Sdan ** a pointer to a new sqlite3_changegroup object before returning. The caller
8325898ad69Sdan ** should eventually free the returned object using a call to
8335898ad69Sdan ** sqlite3changegroup_delete(). If an error occurs, an SQLite error code
8345898ad69Sdan ** (i.e. SQLITE_NOMEM) is returned and *pp is set to NULL.
8355898ad69Sdan **
8365898ad69Sdan ** The usual usage pattern for an sqlite3_changegroup object is as follows:
8375898ad69Sdan **
8385898ad69Sdan ** <ul>
8395898ad69Sdan **   <li> It is created using a call to sqlite3changegroup_new().
8405898ad69Sdan **
8415898ad69Sdan **   <li> Zero or more changesets (or patchsets) are added to the object
8425898ad69Sdan **        by calling sqlite3changegroup_add().
8435898ad69Sdan **
8445898ad69Sdan **   <li> The result of combining all input changesets together is obtained
8455898ad69Sdan **        by the application via a call to sqlite3changegroup_output().
8465898ad69Sdan **
8475898ad69Sdan **   <li> The object is deleted using a call to sqlite3changegroup_delete().
8485898ad69Sdan ** </ul>
8495898ad69Sdan **
8505898ad69Sdan ** Any number of calls to add() and output() may be made between the calls to
8515898ad69Sdan ** new() and delete(), and in any order.
8525898ad69Sdan **
8535898ad69Sdan ** As well as the regular sqlite3changegroup_add() and
8545898ad69Sdan ** sqlite3changegroup_output() functions, also available are the streaming
8555898ad69Sdan ** versions sqlite3changegroup_add_strm() and sqlite3changegroup_output_strm().
8565898ad69Sdan */
8575898ad69Sdan int sqlite3changegroup_new(sqlite3_changegroup **pp);
8585898ad69Sdan 
8595898ad69Sdan /*
86066501908Sdan ** CAPI3REF: Add A Changeset To A Changegroup
861bda30ce4Sdrh ** METHOD: sqlite3_changegroup
86266501908Sdan **
8635898ad69Sdan ** Add all changes within the changeset (or patchset) in buffer pData (size
8645898ad69Sdan ** nData bytes) to the changegroup.
8655898ad69Sdan **
8665898ad69Sdan ** If the buffer contains a patchset, then all prior calls to this function
8675898ad69Sdan ** on the same changegroup object must also have specified patchsets. Or, if
8685898ad69Sdan ** the buffer contains a changeset, so must have the earlier calls to this
8695898ad69Sdan ** function. Otherwise, SQLITE_ERROR is returned and no changes are added
8705898ad69Sdan ** to the changegroup.
8715898ad69Sdan **
8725898ad69Sdan ** Rows within the changeset and changegroup are identified by the values in
8735898ad69Sdan ** their PRIMARY KEY columns. A change in the changeset is considered to
8745898ad69Sdan ** apply to the same row as a change already present in the changegroup if
8755898ad69Sdan ** the two rows have the same primary key.
8765898ad69Sdan **
8775e769a50Sdrh ** Changes to rows that do not already appear in the changegroup are
8785898ad69Sdan ** simply copied into it. Or, if both the new changeset and the changegroup
8795898ad69Sdan ** contain changes that apply to a single row, the final contents of the
8805898ad69Sdan ** changegroup depends on the type of each change, as follows:
8815898ad69Sdan **
8825898ad69Sdan ** <table border=1 style="margin-left:8ex;margin-right:8ex">
8835898ad69Sdan **   <tr><th style="white-space:pre">Existing Change  </th>
8845898ad69Sdan **       <th style="white-space:pre">New Change       </th>
8855898ad69Sdan **       <th>Output Change
8865898ad69Sdan **   <tr><td>INSERT <td>INSERT <td>
8875898ad69Sdan **       The new change is ignored. This case does not occur if the new
8885898ad69Sdan **       changeset was recorded immediately after the changesets already
8895898ad69Sdan **       added to the changegroup.
8905898ad69Sdan **   <tr><td>INSERT <td>UPDATE <td>
8915898ad69Sdan **       The INSERT change remains in the changegroup. The values in the
8925898ad69Sdan **       INSERT change are modified as if the row was inserted by the
8935898ad69Sdan **       existing change and then updated according to the new change.
8945898ad69Sdan **   <tr><td>INSERT <td>DELETE <td>
8955898ad69Sdan **       The existing INSERT is removed from the changegroup. The DELETE is
8965898ad69Sdan **       not added.
8975898ad69Sdan **   <tr><td>UPDATE <td>INSERT <td>
8985898ad69Sdan **       The new change is ignored. This case does not occur if the new
8995898ad69Sdan **       changeset was recorded immediately after the changesets already
9005898ad69Sdan **       added to the changegroup.
9015898ad69Sdan **   <tr><td>UPDATE <td>UPDATE <td>
9025898ad69Sdan **       The existing UPDATE remains within the changegroup. It is amended
9035898ad69Sdan **       so that the accompanying values are as if the row was updated once
9045898ad69Sdan **       by the existing change and then again by the new change.
9055898ad69Sdan **   <tr><td>UPDATE <td>DELETE <td>
9065898ad69Sdan **       The existing UPDATE is replaced by the new DELETE within the
9075898ad69Sdan **       changegroup.
9085898ad69Sdan **   <tr><td>DELETE <td>INSERT <td>
9095898ad69Sdan **       If one or more of the column values in the row inserted by the
9105898ad69Sdan **       new change differ from those in the row deleted by the existing
9115898ad69Sdan **       change, the existing DELETE is replaced by an UPDATE within the
9125898ad69Sdan **       changegroup. Otherwise, if the inserted row is exactly the same
9135898ad69Sdan **       as the deleted row, the existing DELETE is simply discarded.
9145898ad69Sdan **   <tr><td>DELETE <td>UPDATE <td>
9155898ad69Sdan **       The new change is ignored. This case does not occur if the new
9165898ad69Sdan **       changeset was recorded immediately after the changesets already
9175898ad69Sdan **       added to the changegroup.
9185898ad69Sdan **   <tr><td>DELETE <td>DELETE <td>
9195898ad69Sdan **       The new change is ignored. This case does not occur if the new
9205898ad69Sdan **       changeset was recorded immediately after the changesets already
9215898ad69Sdan **       added to the changegroup.
9225898ad69Sdan ** </table>
9235898ad69Sdan **
9245898ad69Sdan ** If the new changeset contains changes to a table that is already present
9255898ad69Sdan ** in the changegroup, then the number of columns and the position of the
9265898ad69Sdan ** primary key columns for the table must be consistent. If this is not the
9275898ad69Sdan ** case, this function fails with SQLITE_SCHEMA. If the input changeset
9285898ad69Sdan ** appears to be corrupt and the corruption is detected, SQLITE_CORRUPT is
9295898ad69Sdan ** returned. Or, if an out-of-memory condition occurs during processing, this
930*2bbcaee8Sdrh ** function returns SQLITE_NOMEM. In all cases, if an error occurs the state
931*2bbcaee8Sdrh ** of the final contents of the changegroup is undefined.
9325898ad69Sdan **
9335898ad69Sdan ** If no error occurs, SQLITE_OK is returned.
9345898ad69Sdan */
9355898ad69Sdan int sqlite3changegroup_add(sqlite3_changegroup*, int nData, void *pData);
9365898ad69Sdan 
9375898ad69Sdan /*
93866501908Sdan ** CAPI3REF: Obtain A Composite Changeset From A Changegroup
939bda30ce4Sdrh ** METHOD: sqlite3_changegroup
94066501908Sdan **
9415898ad69Sdan ** Obtain a buffer containing a changeset (or patchset) representing the
9425898ad69Sdan ** current contents of the changegroup. If the inputs to the changegroup
9435898ad69Sdan ** were themselves changesets, the output is a changeset. Or, if the
9445898ad69Sdan ** inputs were patchsets, the output is also a patchset.
9455898ad69Sdan **
9466c39e6a8Sdan ** As with the output of the sqlite3session_changeset() and
9476c39e6a8Sdan ** sqlite3session_patchset() functions, all changes related to a single
9486c39e6a8Sdan ** table are grouped together in the output of this function. Tables appear
9496c39e6a8Sdan ** in the same order as for the very first changeset added to the changegroup.
9506c39e6a8Sdan ** If the second or subsequent changesets added to the changegroup contain
9516c39e6a8Sdan ** changes for tables that do not appear in the first changeset, they are
9526c39e6a8Sdan ** appended onto the end of the output changeset, again in the order in
9536c39e6a8Sdan ** which they are first encountered.
9546c39e6a8Sdan **
9555898ad69Sdan ** If an error occurs, an SQLite error code is returned and the output
9565898ad69Sdan ** variables (*pnData) and (*ppData) are set to 0. Otherwise, SQLITE_OK
9575898ad69Sdan ** is returned and the output variables are set to the size of and a
9585898ad69Sdan ** pointer to the output buffer, respectively. In this case it is the
9595898ad69Sdan ** responsibility of the caller to eventually free the buffer using a
9605898ad69Sdan ** call to sqlite3_free().
9615898ad69Sdan */
9625898ad69Sdan int sqlite3changegroup_output(
9635898ad69Sdan   sqlite3_changegroup*,
9645898ad69Sdan   int *pnData,                    /* OUT: Size of output buffer in bytes */
9655898ad69Sdan   void **ppData                   /* OUT: Pointer to output buffer */
9665898ad69Sdan );
9675898ad69Sdan 
9685898ad69Sdan /*
96966501908Sdan ** CAPI3REF: Delete A Changegroup Object
970bda30ce4Sdrh ** DESTRUCTOR: sqlite3_changegroup
9715898ad69Sdan */
9725898ad69Sdan void sqlite3changegroup_delete(sqlite3_changegroup*);
9735898ad69Sdan 
974d5f0767cSdan /*
975a2df3d9fSdan ** CAPI3REF: Apply A Changeset To A Database
976a2df3d9fSdan **
97795ccb6dcSdan ** Apply a changeset or patchset to a database. These functions attempt to
97895ccb6dcSdan ** update the "main" database attached to handle db with the changes found in
97995ccb6dcSdan ** the changeset passed via the second and third arguments.
9802635a3beSdan **
98195ccb6dcSdan ** The fourth argument (xFilter) passed to these functions is the "filter
98240368988Sdan ** callback". If it is not NULL, then for each table affected by at least one
98340368988Sdan ** change in the changeset, the filter callback is invoked with
98440368988Sdan ** the table name as the second argument, and a copy of the context pointer
98595ccb6dcSdan ** passed as the sixth argument as the first. If the "filter callback"
98695ccb6dcSdan ** returns zero, then no attempt is made to apply any changes to the table.
98795ccb6dcSdan ** Otherwise, if the return value is non-zero or the xFilter argument to
98895ccb6dcSdan ** is NULL, all changes related to the table are attempted.
98940368988Sdan **
99040368988Sdan ** For each table that is not excluded by the filter callback, this function
99140368988Sdan ** tests that the target database contains a compatible table. A table is
99240368988Sdan ** considered compatible if all of the following are true:
9932635a3beSdan **
9942635a3beSdan ** <ul>
9952635a3beSdan **   <li> The table has the same name as the name recorded in the
9962635a3beSdan **        changeset, and
997ff677b20Sdan **   <li> The table has at least as many columns as recorded in the
9982635a3beSdan **        changeset, and
9992635a3beSdan **   <li> The table has primary key columns in the same position as
10002635a3beSdan **        recorded in the changeset.
10012635a3beSdan ** </ul>
10022635a3beSdan **
100340368988Sdan ** If there is no compatible table, it is not an error, but none of the
100440368988Sdan ** changes associated with the table are applied. A warning message is issued
100540368988Sdan ** via the sqlite3_log() mechanism with the error code SQLITE_SCHEMA. At most
100640368988Sdan ** one such warning is issued for each table in the changeset.
10072635a3beSdan **
100840368988Sdan ** For each change for which there is a compatible table, an attempt is made
100940368988Sdan ** to modify the table contents according to the UPDATE, INSERT or DELETE
101040368988Sdan ** change. If a change cannot be applied cleanly, the conflict handler
101140368988Sdan ** function passed as the fifth argument to sqlite3changeset_apply() may be
101240368988Sdan ** invoked. A description of exactly when the conflict handler is invoked for
101340368988Sdan ** each type of change is below.
10142635a3beSdan **
1015082c96dfSdan ** Unlike the xFilter argument, xConflict may not be passed NULL. The results
1016082c96dfSdan ** of passing anything other than a valid function pointer as the xConflict
1017082c96dfSdan ** argument are undefined.
1018082c96dfSdan **
10192635a3beSdan ** Each time the conflict handler function is invoked, it must return one
10202635a3beSdan ** of [SQLITE_CHANGESET_OMIT], [SQLITE_CHANGESET_ABORT] or
10212635a3beSdan ** [SQLITE_CHANGESET_REPLACE]. SQLITE_CHANGESET_REPLACE may only be returned
10222635a3beSdan ** if the second argument passed to the conflict handler is either
10232635a3beSdan ** SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If the conflict-handler
10242635a3beSdan ** returns an illegal value, any changes already made are rolled back and
10252635a3beSdan ** the call to sqlite3changeset_apply() returns SQLITE_MISUSE. Different
10262635a3beSdan ** actions are taken by sqlite3changeset_apply() depending on the value
10272635a3beSdan ** returned by each invocation of the conflict-handler function. Refer to
10282635a3beSdan ** the documentation for the three
10292635a3beSdan ** [SQLITE_CHANGESET_OMIT|available return values] for details.
10302635a3beSdan **
10312635a3beSdan ** <dl>
10322635a3beSdan ** <dt>DELETE Changes<dd>
103395ccb6dcSdan **   For each DELETE change, the function checks if the target database
10342635a3beSdan **   contains a row with the same primary key value (or values) as the
10352635a3beSdan **   original row values stored in the changeset. If it does, and the values
10362635a3beSdan **   stored in all non-primary key columns also match the values stored in
10372635a3beSdan **   the changeset the row is deleted from the target database.
10382635a3beSdan **
10392635a3beSdan **   If a row with matching primary key values is found, but one or more of
10402635a3beSdan **   the non-primary key fields contains a value different from the original
10412635a3beSdan **   row value stored in the changeset, the conflict-handler function is
1042ff677b20Sdan **   invoked with [SQLITE_CHANGESET_DATA] as the second argument. If the
1043ff677b20Sdan **   database table has more columns than are recorded in the changeset,
1044ff677b20Sdan **   only the values of those non-primary key fields are compared against
1045ff677b20Sdan **   the current database contents - any trailing database table columns
1046ff677b20Sdan **   are ignored.
10472635a3beSdan **
10482635a3beSdan **   If no row with matching primary key values is found in the database,
10492635a3beSdan **   the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
10502635a3beSdan **   passed as the second argument.
10512635a3beSdan **
10522635a3beSdan **   If the DELETE operation is attempted, but SQLite returns SQLITE_CONSTRAINT
10532635a3beSdan **   (which can only happen if a foreign key constraint is violated), the
10542635a3beSdan **   conflict-handler function is invoked with [SQLITE_CHANGESET_CONSTRAINT]
10552635a3beSdan **   passed as the second argument. This includes the case where the DELETE
10562635a3beSdan **   operation is attempted because an earlier call to the conflict handler
10572635a3beSdan **   function returned [SQLITE_CHANGESET_REPLACE].
10582635a3beSdan **
10592635a3beSdan ** <dt>INSERT Changes<dd>
10602635a3beSdan **   For each INSERT change, an attempt is made to insert the new row into
1061ff677b20Sdan **   the database. If the changeset row contains fewer fields than the
1062ff677b20Sdan **   database table, the trailing fields are populated with their default
1063ff677b20Sdan **   values.
10642635a3beSdan **
10652635a3beSdan **   If the attempt to insert the row fails because the database already
10662635a3beSdan **   contains a row with the same primary key values, the conflict handler
10672635a3beSdan **   function is invoked with the second argument set to
10682635a3beSdan **   [SQLITE_CHANGESET_CONFLICT].
10692635a3beSdan **
10702635a3beSdan **   If the attempt to insert the row fails because of some other constraint
10712635a3beSdan **   violation (e.g. NOT NULL or UNIQUE), the conflict handler function is
10722635a3beSdan **   invoked with the second argument set to [SQLITE_CHANGESET_CONSTRAINT].
10732635a3beSdan **   This includes the case where the INSERT operation is re-attempted because
10742635a3beSdan **   an earlier call to the conflict handler function returned
10752635a3beSdan **   [SQLITE_CHANGESET_REPLACE].
10762635a3beSdan **
10772635a3beSdan ** <dt>UPDATE Changes<dd>
107895ccb6dcSdan **   For each UPDATE change, the function checks if the target database
10792635a3beSdan **   contains a row with the same primary key value (or values) as the
10802635a3beSdan **   original row values stored in the changeset. If it does, and the values
1081ff677b20Sdan **   stored in all modified non-primary key columns also match the values
1082ff677b20Sdan **   stored in the changeset the row is updated within the target database.
10832635a3beSdan **
10842635a3beSdan **   If a row with matching primary key values is found, but one or more of
1085ff677b20Sdan **   the modified non-primary key fields contains a value different from an
1086ff677b20Sdan **   original row value stored in the changeset, the conflict-handler function
1087ff677b20Sdan **   is invoked with [SQLITE_CHANGESET_DATA] as the second argument. Since
10882635a3beSdan **   UPDATE changes only contain values for non-primary key fields that are
10892635a3beSdan **   to be modified, only those fields need to match the original values to
10902635a3beSdan **   avoid the SQLITE_CHANGESET_DATA conflict-handler callback.
10912635a3beSdan **
10922635a3beSdan **   If no row with matching primary key values is found in the database,
10932635a3beSdan **   the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
10942635a3beSdan **   passed as the second argument.
10952635a3beSdan **
10962635a3beSdan **   If the UPDATE operation is attempted, but SQLite returns
10972635a3beSdan **   SQLITE_CONSTRAINT, the conflict-handler function is invoked with
10982635a3beSdan **   [SQLITE_CHANGESET_CONSTRAINT] passed as the second argument.
10992635a3beSdan **   This includes the case where the UPDATE operation is attempted after
11002635a3beSdan **   an earlier call to the conflict handler function returned
11012635a3beSdan **   [SQLITE_CHANGESET_REPLACE].
11022635a3beSdan ** </dl>
1103d5f0767cSdan **
1104d5f0767cSdan ** It is safe to execute SQL statements, including those that write to the
1105d5f0767cSdan ** table that the callback related to, from within the xConflict callback.
1106*2bbcaee8Sdrh ** This can be used to further customize the application's conflict
1107d5f0767cSdan ** resolution strategy.
11082635a3beSdan **
110995ccb6dcSdan ** All changes made by these functions are enclosed in a savepoint transaction.
11102635a3beSdan ** If any other error (aside from a constraint failure when attempting to
11112635a3beSdan ** write to the target database) occurs, then the savepoint transaction is
11122635a3beSdan ** rolled back, restoring the target database to its original state, and an
11132635a3beSdan ** SQLite error code returned.
111495ccb6dcSdan **
111595ccb6dcSdan ** If the output parameters (ppRebase) and (pnRebase) are non-NULL and
111695ccb6dcSdan ** the input is a changeset (not a patchset), then sqlite3changeset_apply_v2()
111795ccb6dcSdan ** may set (*ppRebase) to point to a "rebase" that may be used with the
111895ccb6dcSdan ** sqlite3_rebaser APIs buffer before returning. In this case (*pnRebase)
111995ccb6dcSdan ** is set to the size of the buffer in bytes. It is the responsibility of the
112095ccb6dcSdan ** caller to eventually free any such buffer using sqlite3_free(). The buffer
112195ccb6dcSdan ** is only allocated and populated if one or more conflicts were encountered
112295ccb6dcSdan ** while applying the patchset. See comments surrounding the sqlite3_rebaser
112395ccb6dcSdan ** APIs for further details.
1124fe55da38Sdan **
1125fe55da38Sdan ** The behavior of sqlite3changeset_apply_v2() and its streaming equivalent
1126fe55da38Sdan ** may be modified by passing a combination of
1127fe55da38Sdan ** [SQLITE_CHANGESETAPPLY_NOSAVEPOINT | supported flags] as the 9th parameter.
1128fe55da38Sdan **
1129fe55da38Sdan ** Note that the sqlite3changeset_apply_v2() API is still <b>experimental</b>
1130fe55da38Sdan ** and therefore subject to change.
1131d5f0767cSdan */
1132d5f0767cSdan int sqlite3changeset_apply(
1133296c7658Sdan   sqlite3 *db,                    /* Apply change to "main" db of this handle */
1134296c7658Sdan   int nChangeset,                 /* Size of changeset in bytes */
1135296c7658Sdan   void *pChangeset,               /* Changeset blob */
113640368988Sdan   int(*xFilter)(
113740368988Sdan     void *pCtx,                   /* Copy of sixth arg to _apply() */
113840368988Sdan     const char *zTab              /* Table name */
113940368988Sdan   ),
1140d5f0767cSdan   int(*xConflict)(
114140368988Sdan     void *pCtx,                   /* Copy of sixth arg to _apply() */
1142d5f0767cSdan     int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
1143d5f0767cSdan     sqlite3_changeset_iter *p     /* Handle describing change and conflict */
1144d5f0767cSdan   ),
1145296c7658Sdan   void *pCtx                      /* First argument passed to xConflict */
1146d5f0767cSdan );
1147a38e6c57Sdan int sqlite3changeset_apply_v2(
1148a38e6c57Sdan   sqlite3 *db,                    /* Apply change to "main" db of this handle */
1149a38e6c57Sdan   int nChangeset,                 /* Size of changeset in bytes */
1150a38e6c57Sdan   void *pChangeset,               /* Changeset blob */
1151a38e6c57Sdan   int(*xFilter)(
1152a38e6c57Sdan     void *pCtx,                   /* Copy of sixth arg to _apply() */
1153a38e6c57Sdan     const char *zTab              /* Table name */
1154a38e6c57Sdan   ),
1155a38e6c57Sdan   int(*xConflict)(
1156a38e6c57Sdan     void *pCtx,                   /* Copy of sixth arg to _apply() */
1157a38e6c57Sdan     int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
1158a38e6c57Sdan     sqlite3_changeset_iter *p     /* Handle describing change and conflict */
1159a38e6c57Sdan   ),
1160a38e6c57Sdan   void *pCtx,                     /* First argument passed to xConflict */
1161fe55da38Sdan   void **ppRebase, int *pnRebase, /* OUT: Rebase data */
116246de0728Sdan   int flags                       /* SESSION_CHANGESETAPPLY_* flags */
1163a38e6c57Sdan );
1164a38e6c57Sdan 
11650c698471Sdan /*
1166fe55da38Sdan ** CAPI3REF: Flags for sqlite3changeset_apply_v2
1167fe55da38Sdan **
1168fe55da38Sdan ** The following flags may passed via the 9th parameter to
1169fe55da38Sdan ** [sqlite3changeset_apply_v2] and [sqlite3changeset_apply_v2_strm]:
1170fe55da38Sdan **
1171fe55da38Sdan ** <dl>
1172fe55da38Sdan ** <dt>SQLITE_CHANGESETAPPLY_NOSAVEPOINT <dd>
1173fe55da38Sdan **   Usually, the sessions module encloses all operations performed by
1174fe55da38Sdan **   a single call to apply_v2() or apply_v2_strm() in a [SAVEPOINT]. The
1175fe55da38Sdan **   SAVEPOINT is committed if the changeset or patchset is successfully
1176fe55da38Sdan **   applied, or rolled back if an error occurs. Specifying this flag
1177fe55da38Sdan **   causes the sessions module to omit this savepoint. In this case, if the
1178fe55da38Sdan **   caller has an open transaction or savepoint when apply_v2() is called,
1179fe55da38Sdan **   it may revert the partially applied changeset by rolling it back.
118044748f27Sdan **
118144748f27Sdan ** <dt>SQLITE_CHANGESETAPPLY_INVERT <dd>
118244748f27Sdan **   Invert the changeset before applying it. This is equivalent to inverting
118344748f27Sdan **   a changeset using sqlite3changeset_invert() before applying it. It is
118444748f27Sdan **   an error to specify this flag with a patchset.
1185fe55da38Sdan */
1186fe55da38Sdan #define SQLITE_CHANGESETAPPLY_NOSAVEPOINT   0x0001
118744748f27Sdan #define SQLITE_CHANGESETAPPLY_INVERT        0x0002
1188fe55da38Sdan 
1189fe55da38Sdan /*
1190157546f4Sshaneh ** CAPI3REF: Constants Passed To The Conflict Handler
1191a2df3d9fSdan **
11922635a3beSdan ** Values that may be passed as the second argument to a conflict-handler.
11930c698471Sdan **
11942635a3beSdan ** <dl>
11952635a3beSdan ** <dt>SQLITE_CHANGESET_DATA<dd>
11960c698471Sdan **   The conflict handler is invoked with CHANGESET_DATA as the second argument
11970c698471Sdan **   when processing a DELETE or UPDATE change if a row with the required
11980c698471Sdan **   PRIMARY KEY fields is present in the database, but one or more other
11990c698471Sdan **   (non primary-key) fields modified by the update do not contain the
12000c698471Sdan **   expected "before" values.
12010c698471Sdan **
12020c698471Sdan **   The conflicting row, in this case, is the database row with the matching
12030c698471Sdan **   primary key.
12040c698471Sdan **
12052635a3beSdan ** <dt>SQLITE_CHANGESET_NOTFOUND<dd>
12060c698471Sdan **   The conflict handler is invoked with CHANGESET_NOTFOUND as the second
12070c698471Sdan **   argument when processing a DELETE or UPDATE change if a row with the
12080c698471Sdan **   required PRIMARY KEY fields is not present in the database.
12090c698471Sdan **
12100c698471Sdan **   There is no conflicting row in this case. The results of invoking the
12110c698471Sdan **   sqlite3changeset_conflict() API are undefined.
12120c698471Sdan **
12132635a3beSdan ** <dt>SQLITE_CHANGESET_CONFLICT<dd>
12140c698471Sdan **   CHANGESET_CONFLICT is passed as the second argument to the conflict
12152635a3beSdan **   handler while processing an INSERT change if the operation would result
12162635a3beSdan **   in duplicate primary key values.
12170c698471Sdan **
12180c698471Sdan **   The conflicting row in this case is the database row with the matching
12190c698471Sdan **   primary key.
12200c698471Sdan **
1221cb3e4b79Sdan ** <dt>SQLITE_CHANGESET_FOREIGN_KEY<dd>
1222cb3e4b79Sdan **   If foreign key handling is enabled, and applying a changeset leaves the
1223cb3e4b79Sdan **   database in a state containing foreign key violations, the conflict
1224cb3e4b79Sdan **   handler is invoked with CHANGESET_FOREIGN_KEY as the second argument
1225cb3e4b79Sdan **   exactly once before the changeset is committed. If the conflict handler
1226cb3e4b79Sdan **   returns CHANGESET_OMIT, the changes, including those that caused the
1227cb3e4b79Sdan **   foreign key constraint violation, are committed. Or, if it returns
1228cb3e4b79Sdan **   CHANGESET_ABORT, the changeset is rolled back.
1229cb3e4b79Sdan **
1230cb3e4b79Sdan **   No current or conflicting row information is provided. The only function
1231cb3e4b79Sdan **   it is possible to call on the supplied sqlite3_changeset_iter handle
1232cb3e4b79Sdan **   is sqlite3changeset_fk_conflicts().
1233cb3e4b79Sdan **
12342635a3beSdan ** <dt>SQLITE_CHANGESET_CONSTRAINT<dd>
12350c698471Sdan **   If any other constraint violation occurs while applying a change (i.e.
1236cb3e4b79Sdan **   a UNIQUE, CHECK or NOT NULL constraint), the conflict handler is
1237cb3e4b79Sdan **   invoked with CHANGESET_CONSTRAINT as the second argument.
12380c698471Sdan **
12390c698471Sdan **   There is no conflicting row in this case. The results of invoking the
12400c698471Sdan **   sqlite3changeset_conflict() API are undefined.
1241cb3e4b79Sdan **
12422635a3beSdan ** </dl>
12430c698471Sdan */
1244d5f0767cSdan #define SQLITE_CHANGESET_DATA        1
1245d5f0767cSdan #define SQLITE_CHANGESET_NOTFOUND    2
1246d5f0767cSdan #define SQLITE_CHANGESET_CONFLICT    3
1247d5f0767cSdan #define SQLITE_CHANGESET_CONSTRAINT  4
1248cb3e4b79Sdan #define SQLITE_CHANGESET_FOREIGN_KEY 5
1249d5f0767cSdan 
12500c698471Sdan /*
1251a2df3d9fSdan ** CAPI3REF: Constants Returned By The Conflict Handler
1252a2df3d9fSdan **
1253a2df3d9fSdan ** A conflict handler callback must return one of the following three values.
12540c698471Sdan **
12552635a3beSdan ** <dl>
12562635a3beSdan ** <dt>SQLITE_CHANGESET_OMIT<dd>
12570c698471Sdan **   If a conflict handler returns this value no special action is taken. The
12582635a3beSdan **   change that caused the conflict is not applied. The session module
12592635a3beSdan **   continues to the next change in the changeset.
12600c698471Sdan **
12612635a3beSdan ** <dt>SQLITE_CHANGESET_REPLACE<dd>
12620c698471Sdan **   This value may only be returned if the second argument to the conflict
12630c698471Sdan **   handler was SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If this
12640c698471Sdan **   is not the case, any changes applied so far are rolled back and the
12650c698471Sdan **   call to sqlite3changeset_apply() returns SQLITE_MISUSE.
12660c698471Sdan **
12670c698471Sdan **   If CHANGESET_REPLACE is returned by an SQLITE_CHANGESET_DATA conflict
12680c698471Sdan **   handler, then the conflicting row is either updated or deleted, depending
12690c698471Sdan **   on the type of change.
12700c698471Sdan **
12710c698471Sdan **   If CHANGESET_REPLACE is returned by an SQLITE_CHANGESET_CONFLICT conflict
12720c698471Sdan **   handler, then the conflicting row is removed from the database and a
12730c698471Sdan **   second attempt to apply the change is made. If this second attempt fails,
12740c698471Sdan **   the original row is restored to the database before continuing.
12750c698471Sdan **
12762635a3beSdan ** <dt>SQLITE_CHANGESET_ABORT<dd>
12770c698471Sdan **   If this value is returned, any changes applied so far are rolled back
12782635a3beSdan **   and the call to sqlite3changeset_apply() returns SQLITE_ABORT.
12792635a3beSdan ** </dl>
12800c698471Sdan */
1281d5f0767cSdan #define SQLITE_CHANGESET_OMIT       0
1282d5f0767cSdan #define SQLITE_CHANGESET_REPLACE    1
1283d5f0767cSdan #define SQLITE_CHANGESET_ABORT      2
128491ddd559Sdan 
1285f01d3a7eSdan /*
1286f01d3a7eSdan ** CAPI3REF: Rebasing changesets
1287cbeee957Sdan ** EXPERIMENTAL
1288f01d3a7eSdan **
1289bd45374cSdan ** Suppose there is a site hosting a database in state S0. And that
1290bd45374cSdan ** modifications are made that move that database to state S1 and a
1291bd45374cSdan ** changeset recorded (the "local" changeset). Then, a changeset based
1292bd45374cSdan ** on S0 is received from another site (the "remote" changeset) and
1293bd45374cSdan ** applied to the database. The database is then in state
1294bd45374cSdan ** (S1+"remote"), where the exact state depends on any conflict
1295bd45374cSdan ** resolution decisions (OMIT or REPLACE) made while applying "remote".
1296bd45374cSdan ** Rebasing a changeset is to update it to take those conflict
1297bd45374cSdan ** resolution decisions into account, so that the same conflicts
1298bd45374cSdan ** do not have to be resolved elsewhere in the network.
1299bd45374cSdan **
1300bd45374cSdan ** For example, if both the local and remote changesets contain an
1301bd45374cSdan ** INSERT of the same key on "CREATE TABLE t1(a PRIMARY KEY, b)":
1302bd45374cSdan **
1303bd45374cSdan **   local:  INSERT INTO t1 VALUES(1, 'v1');
1304bd45374cSdan **   remote: INSERT INTO t1 VALUES(1, 'v2');
1305bd45374cSdan **
1306bd45374cSdan ** and the conflict resolution is REPLACE, then the INSERT change is
1307bd45374cSdan ** removed from the local changeset (it was overridden). Or, if the
1308bd45374cSdan ** conflict resolution was "OMIT", then the local changeset is modified
1309bd45374cSdan ** to instead contain:
1310bd45374cSdan **
1311bd45374cSdan **           UPDATE t1 SET b = 'v2' WHERE a=1;
1312bd45374cSdan **
1313bd45374cSdan ** Changes within the local changeset are rebased as follows:
1314f01d3a7eSdan **
1315f01d3a7eSdan ** <dl>
1316bd45374cSdan ** <dt>Local INSERT<dd>
1317f01d3a7eSdan **   This may only conflict with a remote INSERT. If the conflict
1318f01d3a7eSdan **   resolution was OMIT, then add an UPDATE change to the rebased
1319f01d3a7eSdan **   changeset. Or, if the conflict resolution was REPLACE, add
1320f01d3a7eSdan **   nothing to the rebased changeset.
1321f01d3a7eSdan **
1322bd45374cSdan ** <dt>Local DELETE<dd>
1323f01d3a7eSdan **   This may conflict with a remote UPDATE or DELETE. In both cases the
1324f01d3a7eSdan **   only possible resolution is OMIT. If the remote operation was a
1325f01d3a7eSdan **   DELETE, then add no change to the rebased changeset. If the remote
1326bd45374cSdan **   operation was an UPDATE, then the old.* fields of change are updated
1327bd45374cSdan **   to reflect the new.* values in the UPDATE.
1328f01d3a7eSdan **
1329bd45374cSdan ** <dt>Local UPDATE<dd>
1330f01d3a7eSdan **   This may conflict with a remote UPDATE or DELETE. If it conflicts
1331f01d3a7eSdan **   with a DELETE, and the conflict resolution was OMIT, then the update
1332f01d3a7eSdan **   is changed into an INSERT. Any undefined values in the new.* record
1333bd45374cSdan **   from the update change are filled in using the old.* values from
1334f01d3a7eSdan **   the conflicting DELETE. Or, if the conflict resolution was REPLACE,
1335f01d3a7eSdan **   the UPDATE change is simply omitted from the rebased changeset.
1336f01d3a7eSdan **
1337f01d3a7eSdan **   If conflict is with a remote UPDATE and the resolution is OMIT, then
1338f01d3a7eSdan **   the old.* values are rebased using the new.* values in the remote
1339f01d3a7eSdan **   change. Or, if the resolution is REPLACE, then the change is copied
1340f01d3a7eSdan **   into the rebased changeset with updates to columns also updated by
134124a0c453Sdan **   the conflicting remote UPDATE removed. If this means no columns would
134224a0c453Sdan **   be updated, the change is omitted.
1343f01d3a7eSdan ** </dl>
1344bd45374cSdan **
1345bd45374cSdan ** A local change may be rebased against multiple remote changes
134624a0c453Sdan ** simultaneously. If a single key is modified by multiple remote
134724a0c453Sdan ** changesets, they are combined as follows before the local changeset
134824a0c453Sdan ** is rebased:
134924a0c453Sdan **
135024a0c453Sdan ** <ul>
135124a0c453Sdan **    <li> If there has been one or more REPLACE resolutions on a
135224a0c453Sdan **         key, it is rebased according to a REPLACE.
135324a0c453Sdan **
135424a0c453Sdan **    <li> If there have been no REPLACE resolutions on a key, then
135524a0c453Sdan **         the local changeset is rebased according to the most recent
135624a0c453Sdan **         of the OMIT resolutions.
135724a0c453Sdan ** </ul>
135824a0c453Sdan **
135924a0c453Sdan ** Note that conflict resolutions from multiple remote changesets are
136024a0c453Sdan ** combined on a per-field basis, not per-row. This means that in the
136124a0c453Sdan ** case of multiple remote UPDATE operations, some fields of a single
136224a0c453Sdan ** local change may be rebased for REPLACE while others are rebased for
136324a0c453Sdan ** OMIT.
136495ccb6dcSdan **
136595ccb6dcSdan ** In order to rebase a local changeset, the remote changeset must first
136695ccb6dcSdan ** be applied to the local database using sqlite3changeset_apply_v2() and
136795ccb6dcSdan ** the buffer of rebase information captured. Then:
136895ccb6dcSdan **
136995ccb6dcSdan ** <ol>
137095ccb6dcSdan **   <li> An sqlite3_rebaser object is created by calling
137195ccb6dcSdan **        sqlite3rebaser_create().
137295ccb6dcSdan **   <li> The new object is configured with the rebase buffer obtained from
137395ccb6dcSdan **        sqlite3changeset_apply_v2() by calling sqlite3rebaser_configure().
137495ccb6dcSdan **        If the local changeset is to be rebased against multiple remote
137595ccb6dcSdan **        changesets, then sqlite3rebaser_configure() should be called
137695ccb6dcSdan **        multiple times, in the same order that the multiple
137795ccb6dcSdan **        sqlite3changeset_apply_v2() calls were made.
137895ccb6dcSdan **   <li> Each local changeset is rebased by calling sqlite3rebaser_rebase().
137995ccb6dcSdan **   <li> The sqlite3_rebaser object is deleted by calling
138095ccb6dcSdan **        sqlite3rebaser_delete().
138195ccb6dcSdan ** </ol>
1382f01d3a7eSdan */
1383c0a499eaSdan typedef struct sqlite3_rebaser sqlite3_rebaser;
1384c0a499eaSdan 
138595ccb6dcSdan /*
1386cbeee957Sdan ** CAPI3REF: Create a changeset rebaser object.
1387cbeee957Sdan ** EXPERIMENTAL
138895ccb6dcSdan **
138995ccb6dcSdan ** Allocate a new changeset rebaser object. If successful, set (*ppNew) to
139095ccb6dcSdan ** point to the new object and return SQLITE_OK. Otherwise, if an error
139195ccb6dcSdan ** occurs, return an SQLite error code (e.g. SQLITE_NOMEM) and set (*ppNew)
139295ccb6dcSdan ** to NULL.
139395ccb6dcSdan */
1394c0a499eaSdan int sqlite3rebaser_create(sqlite3_rebaser **ppNew);
1395c0a499eaSdan 
139695ccb6dcSdan /*
1397cbeee957Sdan ** CAPI3REF: Configure a changeset rebaser object.
1398cbeee957Sdan ** EXPERIMENTAL
139995ccb6dcSdan **
140095ccb6dcSdan ** Configure the changeset rebaser object to rebase changesets according
140195ccb6dcSdan ** to the conflict resolutions described by buffer pRebase (size nRebase
140295ccb6dcSdan ** bytes), which must have been obtained from a previous call to
140395ccb6dcSdan ** sqlite3changeset_apply_v2().
140495ccb6dcSdan */
1405c0a499eaSdan int sqlite3rebaser_configure(
1406c0a499eaSdan   sqlite3_rebaser*,
1407c0a499eaSdan   int nRebase, const void *pRebase
1408c0a499eaSdan );
1409c0a499eaSdan 
141095ccb6dcSdan /*
1411cbeee957Sdan ** CAPI3REF: Rebase a changeset
1412cbeee957Sdan ** EXPERIMENTAL
141395ccb6dcSdan **
141495ccb6dcSdan ** Argument pIn must point to a buffer containing a changeset nIn bytes
141595ccb6dcSdan ** in size. This function allocates and populates a buffer with a copy
1416*2bbcaee8Sdrh ** of the changeset rebased according to the configuration of the
141795ccb6dcSdan ** rebaser object passed as the first argument. If successful, (*ppOut)
1418a920b209Sdrh ** is set to point to the new buffer containing the rebased changeset and
141995ccb6dcSdan ** (*pnOut) to its size in bytes and SQLITE_OK returned. It is the
142095ccb6dcSdan ** responsibility of the caller to eventually free the new buffer using
142195ccb6dcSdan ** sqlite3_free(). Otherwise, if an error occurs, (*ppOut) and (*pnOut)
142295ccb6dcSdan ** are set to zero and an SQLite error code returned.
142395ccb6dcSdan */
1424c0a499eaSdan int sqlite3rebaser_rebase(
1425c0a499eaSdan   sqlite3_rebaser*,
1426c0a499eaSdan   int nIn, const void *pIn,
1427c0a499eaSdan   int *pnOut, void **ppOut
1428c0a499eaSdan );
1429c0a499eaSdan 
143095ccb6dcSdan /*
1431cbeee957Sdan ** CAPI3REF: Delete a changeset rebaser object.
1432cbeee957Sdan ** EXPERIMENTAL
143395ccb6dcSdan **
143495ccb6dcSdan ** Delete the changeset rebaser object and all associated resources. There
143595ccb6dcSdan ** should be one call to this function for each successful invocation
143695ccb6dcSdan ** of sqlite3rebaser_create().
143795ccb6dcSdan */
1438f1b40e83Sdan void sqlite3rebaser_delete(sqlite3_rebaser *p);
1439c0a499eaSdan 
144036828bd9Sdrh /*
144116228167Sdan ** CAPI3REF: Streaming Versions of API functions.
144216228167Sdan **
1443f1a08ad8Sdrh ** The six streaming API xxx_strm() functions serve similar purposes to the
144416228167Sdan ** corresponding non-streaming API functions:
144516228167Sdan **
144616228167Sdan ** <table border=1 style="margin-left:8ex;margin-right:8ex">
144716228167Sdan **   <tr><th>Streaming function<th>Non-streaming equivalent</th>
14483dfbe9b3Smistachkin **   <tr><td>sqlite3changeset_apply_strm<td>[sqlite3changeset_apply]
1449fe55da38Sdan **   <tr><td>sqlite3changeset_apply_strm_v2<td>[sqlite3changeset_apply_v2]
14503dfbe9b3Smistachkin **   <tr><td>sqlite3changeset_concat_strm<td>[sqlite3changeset_concat]
14513dfbe9b3Smistachkin **   <tr><td>sqlite3changeset_invert_strm<td>[sqlite3changeset_invert]
14523dfbe9b3Smistachkin **   <tr><td>sqlite3changeset_start_strm<td>[sqlite3changeset_start]
14533dfbe9b3Smistachkin **   <tr><td>sqlite3session_changeset_strm<td>[sqlite3session_changeset]
14543dfbe9b3Smistachkin **   <tr><td>sqlite3session_patchset_strm<td>[sqlite3session_patchset]
145516228167Sdan ** </table>
145616228167Sdan **
145716228167Sdan ** Non-streaming functions that accept changesets (or patchsets) as input
145816228167Sdan ** require that the entire changeset be stored in a single buffer in memory.
145916228167Sdan ** Similarly, those that return a changeset or patchset do so by returning
146016228167Sdan ** a pointer to a single large buffer allocated using sqlite3_malloc().
146116228167Sdan ** Normally this is convenient. However, if an application running in a
146216228167Sdan ** low-memory environment is required to handle very large changesets, the
146316228167Sdan ** large contiguous memory allocations required can become onerous.
146416228167Sdan **
146516228167Sdan ** In order to avoid this problem, instead of a single large buffer, input
146616228167Sdan ** is passed to a streaming API functions by way of a callback function that
146716228167Sdan ** the sessions module invokes to incrementally request input data as it is
146816228167Sdan ** required. In all cases, a pair of API function parameters such as
146916228167Sdan **
147016228167Sdan **  <pre>
147116228167Sdan **  &nbsp;     int nChangeset,
147216228167Sdan **  &nbsp;     void *pChangeset,
147316228167Sdan **  </pre>
147416228167Sdan **
147516228167Sdan ** Is replaced by:
147616228167Sdan **
147716228167Sdan **  <pre>
147816228167Sdan **  &nbsp;     int (*xInput)(void *pIn, void *pData, int *pnData),
147916228167Sdan **  &nbsp;     void *pIn,
148016228167Sdan **  </pre>
148116228167Sdan **
148216228167Sdan ** Each time the xInput callback is invoked by the sessions module, the first
148316228167Sdan ** argument passed is a copy of the supplied pIn context pointer. The second
148416228167Sdan ** argument, pData, points to a buffer (*pnData) bytes in size. Assuming no
148516228167Sdan ** error occurs the xInput method should copy up to (*pnData) bytes of data
148616228167Sdan ** into the buffer and set (*pnData) to the actual number of bytes copied
148716228167Sdan ** before returning SQLITE_OK. If the input is completely exhausted, (*pnData)
148816228167Sdan ** should be set to zero to indicate this. Or, if an error occurs, an SQLite
148916228167Sdan ** error code should be returned. In all cases, if an xInput callback returns
149016228167Sdan ** an error, all processing is abandoned and the streaming API function
149116228167Sdan ** returns a copy of the error code to the caller.
149216228167Sdan **
1493f1a08ad8Sdrh ** In the case of sqlite3changeset_start_strm(), the xInput callback may be
149416228167Sdan ** invoked by the sessions module at any point during the lifetime of the
149516228167Sdan ** iterator. If such an xInput callback returns an error, the iterator enters
149616228167Sdan ** an error state, whereby all subsequent calls to iterator functions
149716228167Sdan ** immediately fail with the same error code as returned by xInput.
149816228167Sdan **
149916228167Sdan ** Similarly, streaming API functions that return changesets (or patchsets)
150016228167Sdan ** return them in chunks by way of a callback function instead of via a
150116228167Sdan ** pointer to a single large buffer. In this case, a pair of parameters such
150216228167Sdan ** as:
150316228167Sdan **
150416228167Sdan **  <pre>
150516228167Sdan **  &nbsp;     int *pnChangeset,
150616228167Sdan **  &nbsp;     void **ppChangeset,
150716228167Sdan **  </pre>
150816228167Sdan **
150916228167Sdan ** Is replaced by:
151016228167Sdan **
151116228167Sdan **  <pre>
151216228167Sdan **  &nbsp;     int (*xOutput)(void *pOut, const void *pData, int nData),
151316228167Sdan **  &nbsp;     void *pOut
151416228167Sdan **  </pre>
151516228167Sdan **
151616228167Sdan ** The xOutput callback is invoked zero or more times to return data to
151716228167Sdan ** the application. The first parameter passed to each call is a copy of the
151816228167Sdan ** pOut pointer supplied by the application. The second parameter, pData,
151916228167Sdan ** points to a buffer nData bytes in size containing the chunk of output
152016228167Sdan ** data being returned. If the xOutput callback successfully processes the
152116228167Sdan ** supplied data, it should return SQLITE_OK to indicate success. Otherwise,
152216228167Sdan ** it should return some other SQLite error code. In this case processing
152316228167Sdan ** is immediately abandoned and the streaming API function returns a copy
152416228167Sdan ** of the xOutput error code to the application.
152516228167Sdan **
152616228167Sdan ** The sessions module never invokes an xOutput callback with the third
152716228167Sdan ** parameter set to a value less than or equal to zero. Other than this,
152816228167Sdan ** no guarantees are made as to the size of the chunks of data returned.
152916228167Sdan */
1530f1a08ad8Sdrh int sqlite3changeset_apply_strm(
153116228167Sdan   sqlite3 *db,                    /* Apply change to "main" db of this handle */
153216228167Sdan   int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
153316228167Sdan   void *pIn,                                          /* First arg for xInput */
153416228167Sdan   int(*xFilter)(
153516228167Sdan     void *pCtx,                   /* Copy of sixth arg to _apply() */
153616228167Sdan     const char *zTab              /* Table name */
153716228167Sdan   ),
153816228167Sdan   int(*xConflict)(
153916228167Sdan     void *pCtx,                   /* Copy of sixth arg to _apply() */
154016228167Sdan     int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
154116228167Sdan     sqlite3_changeset_iter *p     /* Handle describing change and conflict */
154216228167Sdan   ),
154316228167Sdan   void *pCtx                      /* First argument passed to xConflict */
154416228167Sdan );
1545a38e6c57Sdan int sqlite3changeset_apply_v2_strm(
1546a38e6c57Sdan   sqlite3 *db,                    /* Apply change to "main" db of this handle */
1547a38e6c57Sdan   int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
1548a38e6c57Sdan   void *pIn,                                          /* First arg for xInput */
1549a38e6c57Sdan   int(*xFilter)(
1550a38e6c57Sdan     void *pCtx,                   /* Copy of sixth arg to _apply() */
1551a38e6c57Sdan     const char *zTab              /* Table name */
1552a38e6c57Sdan   ),
1553a38e6c57Sdan   int(*xConflict)(
1554a38e6c57Sdan     void *pCtx,                   /* Copy of sixth arg to _apply() */
1555a38e6c57Sdan     int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
1556a38e6c57Sdan     sqlite3_changeset_iter *p     /* Handle describing change and conflict */
1557a38e6c57Sdan   ),
1558a38e6c57Sdan   void *pCtx,                     /* First argument passed to xConflict */
1559fe55da38Sdan   void **ppRebase, int *pnRebase,
1560fe55da38Sdan   int flags
1561a38e6c57Sdan );
1562f1a08ad8Sdrh int sqlite3changeset_concat_strm(
156316228167Sdan   int (*xInputA)(void *pIn, void *pData, int *pnData),
156416228167Sdan   void *pInA,
156516228167Sdan   int (*xInputB)(void *pIn, void *pData, int *pnData),
156616228167Sdan   void *pInB,
156716228167Sdan   int (*xOutput)(void *pOut, const void *pData, int nData),
156816228167Sdan   void *pOut
156916228167Sdan );
1570f1a08ad8Sdrh int sqlite3changeset_invert_strm(
157116228167Sdan   int (*xInput)(void *pIn, void *pData, int *pnData),
157216228167Sdan   void *pIn,
157316228167Sdan   int (*xOutput)(void *pOut, const void *pData, int nData),
157416228167Sdan   void *pOut
157516228167Sdan );
1576f1a08ad8Sdrh int sqlite3changeset_start_strm(
157716228167Sdan   sqlite3_changeset_iter **pp,
157816228167Sdan   int (*xInput)(void *pIn, void *pData, int *pnData),
157916228167Sdan   void *pIn
158016228167Sdan );
158146de0728Sdan int sqlite3changeset_start_v2_strm(
158246de0728Sdan   sqlite3_changeset_iter **pp,
158346de0728Sdan   int (*xInput)(void *pIn, void *pData, int *pnData),
158446de0728Sdan   void *pIn,
158546de0728Sdan   int flags
158646de0728Sdan );
1587f1a08ad8Sdrh int sqlite3session_changeset_strm(
158816228167Sdan   sqlite3_session *pSession,
158916228167Sdan   int (*xOutput)(void *pOut, const void *pData, int nData),
159016228167Sdan   void *pOut
159116228167Sdan );
1592f1a08ad8Sdrh int sqlite3session_patchset_strm(
159316228167Sdan   sqlite3_session *pSession,
159416228167Sdan   int (*xOutput)(void *pOut, const void *pData, int nData),
159516228167Sdan   void *pOut
159616228167Sdan );
15975898ad69Sdan int sqlite3changegroup_add_strm(sqlite3_changegroup*,
15985898ad69Sdan     int (*xInput)(void *pIn, void *pData, int *pnData),
15995898ad69Sdan     void *pIn
16005898ad69Sdan );
16015898ad69Sdan int sqlite3changegroup_output_strm(sqlite3_changegroup*,
16025898ad69Sdan     int (*xOutput)(void *pOut, const void *pData, int nData),
16035898ad69Sdan     void *pOut
16045898ad69Sdan );
1605c0a499eaSdan int sqlite3rebaser_rebase_strm(
1606c0a499eaSdan   sqlite3_rebaser *pRebaser,
1607c0a499eaSdan   int (*xInput)(void *pIn, void *pData, int *pnData),
1608c0a499eaSdan   void *pIn,
1609c0a499eaSdan   int (*xOutput)(void *pOut, const void *pData, int nData),
1610c0a499eaSdan   void *pOut
1611c0a499eaSdan );
161216228167Sdan 
16131f48e67dSdan /*
16141f48e67dSdan ** CAPI3REF: Configure global parameters
16151f48e67dSdan **
16161f48e67dSdan ** The sqlite3session_config() interface is used to make global configuration
16171f48e67dSdan ** changes to the sessions module in order to tune it to the specific needs
16181f48e67dSdan ** of the application.
16191f48e67dSdan **
16201f48e67dSdan ** The sqlite3session_config() interface is not threadsafe. If it is invoked
16211f48e67dSdan ** while any other thread is inside any other sessions method then the
16221f48e67dSdan ** results are undefined. Furthermore, if it is invoked after any sessions
16231f48e67dSdan ** related objects have been created, the results are also undefined.
16241f48e67dSdan **
16251f48e67dSdan ** The first argument to the sqlite3session_config() function must be one
16261f48e67dSdan ** of the SQLITE_SESSION_CONFIG_XXX constants defined below. The
16271f48e67dSdan ** interpretation of the (void*) value passed as the second parameter and
16281f48e67dSdan ** the effect of calling this function depends on the value of the first
16291f48e67dSdan ** parameter.
16301f48e67dSdan **
16311f48e67dSdan ** <dl>
16321f48e67dSdan ** <dt>SQLITE_SESSION_CONFIG_STRMSIZE<dd>
16331f48e67dSdan **    By default, the sessions module streaming interfaces attempt to input
16341f48e67dSdan **    and output data in approximately 1 KiB chunks. This operand may be used
16351f48e67dSdan **    to set and query the value of this configuration setting. The pointer
16361f48e67dSdan **    passed as the second argument must point to a value of type (int).
16371f48e67dSdan **    If this value is greater than 0, it is used as the new streaming data
16381f48e67dSdan **    chunk size for both input and output. Before returning, the (int) value
16391f48e67dSdan **    pointed to by pArg is set to the final value of the streaming interface
16401f48e67dSdan **    chunk size.
16411f48e67dSdan ** </dl>
16421f48e67dSdan **
16431f48e67dSdan ** This function returns SQLITE_OK if successful, or an SQLite error code
16441f48e67dSdan ** otherwise.
16451f48e67dSdan */
16461f48e67dSdan int sqlite3session_config(int op, void *pArg);
16471f48e67dSdan 
16481f48e67dSdan /*
16491f48e67dSdan ** CAPI3REF: Values for sqlite3session_config().
16501f48e67dSdan */
16511f48e67dSdan #define SQLITE_SESSION_CONFIG_STRMSIZE 1
165216228167Sdan 
165316228167Sdan /*
165436828bd9Sdrh ** Make sure we can call this stuff from C++.
165536828bd9Sdrh */
165636828bd9Sdrh #ifdef __cplusplus
165736828bd9Sdrh }
16584fccf43aSdan #endif
16594fccf43aSdan 
16604e80d5fcSdrh #endif  /* !defined(__SQLITESESSION_H_) && defined(SQLITE_ENABLE_SESSION) */
1661