14fccf43aSdan 
2*4e80d5fcSdrh #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
16a2df3d9fSdan */
174fccf43aSdan typedef struct sqlite3_session sqlite3_session;
18a2df3d9fSdan 
19a2df3d9fSdan /*
20a2df3d9fSdan ** CAPI3REF: Changeset Iterator Handle
21a2df3d9fSdan */
224fccf43aSdan typedef struct sqlite3_changeset_iter sqlite3_changeset_iter;
234fccf43aSdan 
244fccf43aSdan /*
25a2df3d9fSdan ** CAPI3REF: Create A New Session Object
26a2df3d9fSdan **
2737db03bfSdan ** Create a new session object attached to database handle db. If successful,
2837db03bfSdan ** a pointer to the new object is written to *ppSession and SQLITE_OK is
2937db03bfSdan ** returned. If an error occurs, *ppSession is set to NULL and an SQLite
30c21111daSdan ** error code (e.g. SQLITE_NOMEM) is returned.
3137db03bfSdan **
3237db03bfSdan ** It is possible to create multiple session objects attached to a single
3337db03bfSdan ** database handle.
3437db03bfSdan **
3537db03bfSdan ** Session objects created using this function should be deleted using the
3637db03bfSdan ** [sqlite3session_delete()] function before the database handle that they
3737db03bfSdan ** are attached to is itself closed. If the database handle is closed before
3837db03bfSdan ** the session object is deleted, then the results of calling any session
3937db03bfSdan ** module function, including [sqlite3session_delete()] on the session object
4037db03bfSdan ** are undefined.
4137db03bfSdan **
4237db03bfSdan ** Because the session module uses the [sqlite3_preupdate_hook()] API, it
4337db03bfSdan ** is not possible for an application to register a pre-update hook on a
4437db03bfSdan ** database handle that has one or more session objects attached. Nor is
4537db03bfSdan ** it possible to create a session object attached to a database handle for
4637db03bfSdan ** which a pre-update hook is already defined. The results of attempting
4737db03bfSdan ** either of these things are undefined.
4837db03bfSdan **
4937db03bfSdan ** The session object will be used to create changesets for tables in
5037db03bfSdan ** database zDb, where zDb is either "main", or "temp", or the name of an
51ca62ad57Sdan ** attached database. It is not an error if database zDb is not attached
5237db03bfSdan ** to the database when the session object is created.
534fccf43aSdan */
544fccf43aSdan int sqlite3session_create(
554fccf43aSdan   sqlite3 *db,                    /* Database handle */
564fccf43aSdan   const char *zDb,                /* Name of db (e.g. "main") */
574fccf43aSdan   sqlite3_session **ppSession     /* OUT: New session object */
584fccf43aSdan );
594fccf43aSdan 
604fccf43aSdan /*
61a2df3d9fSdan ** CAPI3REF: Delete A Session Object
62a2df3d9fSdan **
6337db03bfSdan ** Delete a session object previously allocated using
6437db03bfSdan ** [sqlite3session_create()]. Once a session object has been deleted, the
6537db03bfSdan ** results of attempting to use pSession with any other session module
6637db03bfSdan ** function are undefined.
6737db03bfSdan **
6837db03bfSdan ** Session objects must be deleted before the database handle to which they
6937db03bfSdan ** are attached is closed. Refer to the documentation for
7037db03bfSdan ** [sqlite3session_create()] for details.
7137db03bfSdan */
7237db03bfSdan void sqlite3session_delete(sqlite3_session *pSession);
7337db03bfSdan 
747531a5a3Sdan 
7537db03bfSdan /*
76a2df3d9fSdan ** CAPI3REF: Enable Or Disable A Session Object
77a2df3d9fSdan **
784fccf43aSdan ** Enable or disable the recording of changes by a session object. When
794fccf43aSdan ** enabled, a session object records changes made to the database. When
804fccf43aSdan ** disabled - it does not. A newly created session object is enabled.
81c21111daSdan ** Refer to the documentation for [sqlite3session_changeset()] for further
82c21111daSdan ** details regarding how enabling and disabling a session object affects
83c21111daSdan ** the eventual changesets.
844fccf43aSdan **
854fccf43aSdan ** Passing zero to this function disables the session. Passing a value
864fccf43aSdan ** greater than zero enables it. Passing a value less than zero is a
874fccf43aSdan ** no-op, and may be used to query the current state of the session.
884fccf43aSdan **
894fccf43aSdan ** The return value indicates the final state of the session object: 0 if
904fccf43aSdan ** the session is disabled, or 1 if it is enabled.
914fccf43aSdan */
924fccf43aSdan int sqlite3session_enable(sqlite3_session *pSession, int bEnable);
934fccf43aSdan 
944fccf43aSdan /*
95b4480e94Sdan ** CAPI3REF: Set Or Clear the Indirect Change Flag
96b4480e94Sdan **
97b4480e94Sdan ** Each change recorded by a session object is marked as either direct or
98b4480e94Sdan ** indirect. A change is marked as indirect if either:
99b4480e94Sdan **
100b4480e94Sdan ** <ul>
101b4480e94Sdan **   <li> The session object "indirect" flag is set when the change is
102b4480e94Sdan **        made, or
103b4480e94Sdan **   <li> The change is made by an SQL trigger or foreign key action
104b4480e94Sdan **        instead of directly as a result of a users SQL statement.
105b4480e94Sdan ** </ul>
106b4480e94Sdan **
107b4480e94Sdan ** If a single row is affected by more than one operation within a session,
108b4480e94Sdan ** then the change is considered indirect if all operations meet the criteria
109b4480e94Sdan ** for an indirect change above, or direct otherwise.
110b4480e94Sdan **
111b4480e94Sdan ** This function is used to set, clear or query the session object indirect
112b4480e94Sdan ** flag.  If the second argument passed to this function is zero, then the
113b4480e94Sdan ** indirect flag is cleared. If it is greater than zero, the indirect flag
114b4480e94Sdan ** is set. Passing a value less than zero does not modify the current value
115b4480e94Sdan ** of the indirect flag, and may be used to query the current state of the
116b4480e94Sdan ** indirect flag for the specified session object.
117b4480e94Sdan **
118b4480e94Sdan ** The return value indicates the final state of the indirect flag: 0 if
119b4480e94Sdan ** it is clear, or 1 if it is set.
120b4480e94Sdan */
121b4480e94Sdan int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect);
122b4480e94Sdan 
123b4480e94Sdan /*
124157546f4Sshaneh ** CAPI3REF: Attach A Table To A Session Object
125a2df3d9fSdan **
126ff4d0f41Sdan ** If argument zTab is not NULL, then it is the name of a table to attach
127ff4d0f41Sdan ** to the session object passed as the first argument. All subsequent changes
128ff4d0f41Sdan ** made to the table while the session object is enabled will be recorded. See
129ff4d0f41Sdan ** documentation for [sqlite3session_changeset()] for further details.
130ff4d0f41Sdan **
131ff4d0f41Sdan ** Or, if argument zTab is NULL, then changes are recorded for all tables
132ff4d0f41Sdan ** in the database. If additional tables are added to the database (by
133ff4d0f41Sdan ** executing "CREATE TABLE" statements) after this call is made, changes for
134ff4d0f41Sdan ** the new tables are also recorded.
1354fccf43aSdan **
136c21111daSdan ** Changes can only be recorded for tables that have a PRIMARY KEY explicitly
137c21111daSdan ** defined as part of their CREATE TABLE statement. It does not matter if the
138c21111daSdan ** PRIMARY KEY is an "INTEGER PRIMARY KEY" (rowid alias) or not. The PRIMARY
139c21111daSdan ** KEY may consist of a single column, or may be a composite key.
140c21111daSdan **
141c21111daSdan ** It is not an error if the named table does not exist in the database. Nor
142c21111daSdan ** is it an error if the named table does not have a PRIMARY KEY. However,
143c21111daSdan ** no changes will be recorded in either of these scenarios.
144c21111daSdan **
14527453faeSdan ** Changes are not recorded for individual rows that have NULL values stored
14627453faeSdan ** in one or more of their PRIMARY KEY columns.
14727453faeSdan **
148ff4d0f41Sdan ** SQLITE_OK is returned if the call completes without error. Or, if an error
149ff4d0f41Sdan ** occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned.
1504fccf43aSdan */
1514fccf43aSdan int sqlite3session_attach(
1524fccf43aSdan   sqlite3_session *pSession,      /* Session object */
1534fccf43aSdan   const char *zTab                /* Table name */
1544fccf43aSdan );
1554fccf43aSdan 
1564fccf43aSdan /*
1577531a5a3Sdan ** CAPI3REF: Set a table filter on a Session Object.
1587531a5a3Sdan **
1597531a5a3Sdan ** The second argument (xFilter) is the "filter callback". For changes to rows
1607531a5a3Sdan ** in tables that are not attached to the Session oject, the filter is called
1617531a5a3Sdan ** to determine whether changes to the table's rows should be tracked or not.
1627531a5a3Sdan ** If xFilter returns 0, changes is not tracked. Note that once a table is
1637531a5a3Sdan ** attached, xFilter will not be called again.
1647531a5a3Sdan */
1657531a5a3Sdan void sqlite3session_table_filter(
1667531a5a3Sdan   sqlite3_session *pSession,      /* Session object */
1677531a5a3Sdan   int(*xFilter)(
1687531a5a3Sdan     void *pCtx,                   /* Copy of third arg to _filter_table() */
1697531a5a3Sdan     const char *zTab              /* Table name */
1707531a5a3Sdan   ),
1717531a5a3Sdan   void *pCtx                      /* First argument passed to xFilter */
1727531a5a3Sdan );
1737531a5a3Sdan 
1747531a5a3Sdan /*
175a2df3d9fSdan ** CAPI3REF: Generate A Changeset From A Session Object
176a2df3d9fSdan **
17737db03bfSdan ** Obtain a changeset containing changes to the tables attached to the
17837db03bfSdan ** session object passed as the first argument. If successful,
17937db03bfSdan ** set *ppChangeset to point to a buffer containing the changeset
18037db03bfSdan ** and *pnChangeset to the size of the changeset in bytes before returning
18137db03bfSdan ** SQLITE_OK. If an error occurs, set both *ppChangeset and *pnChangeset to
18237db03bfSdan ** zero and return an SQLite error code.
1834fccf43aSdan **
184c21111daSdan ** A changeset consists of zero or more INSERT, UPDATE and/or DELETE changes,
185c21111daSdan ** each representing a change to a single row of an attached table. An INSERT
186c21111daSdan ** change contains the values of each field of a new database row. A DELETE
187c21111daSdan ** contains the original values of each field of a deleted database row. An
188c21111daSdan ** UPDATE change contains the original values of each field of an updated
189c21111daSdan ** database row along with the updated values for each updated non-primary-key
190c21111daSdan ** column. It is not possible for an UPDATE change to represent a change that
191c21111daSdan ** modifies the values of primary key columns. If such a change is made, it
192c21111daSdan ** is represented in a changeset as a DELETE followed by an INSERT.
193c21111daSdan **
19427453faeSdan ** Changes are not recorded for rows that have NULL values stored in one or
19527453faeSdan ** more of their PRIMARY KEY columns. If such a row is inserted or deleted,
19627453faeSdan ** no corresponding change is present in the changesets returned by this
19727453faeSdan ** function. If an existing row with one or more NULL values stored in
19827453faeSdan ** PRIMARY KEY columns is updated so that all PRIMARY KEY columns are non-NULL,
19927453faeSdan ** only an INSERT is appears in the changeset. Similarly, if an existing row
20027453faeSdan ** with non-NULL PRIMARY KEY values is updated so that one or more of its
20127453faeSdan ** PRIMARY KEY columns are set to NULL, the resulting changeset contains a
20227453faeSdan ** DELETE change only.
20327453faeSdan **
204c21111daSdan ** The contents of a changeset may be traversed using an iterator created
205c21111daSdan ** using the [sqlite3changeset_start()] API. A changeset may be applied to
206a2df3d9fSdan ** a database with a compatible schema using the [sqlite3changeset_apply()]
207c21111daSdan ** API.
208c21111daSdan **
2096c39e6a8Sdan ** Within a changeset generated by this function, all changes related to a
2106c39e6a8Sdan ** single table are grouped together. In other words, when iterating through
2116c39e6a8Sdan ** a changeset or when applying a changeset to a database, all changes related
2126c39e6a8Sdan ** to a single table are processed before moving on to the next table. Tables
2136c39e6a8Sdan ** are sorted in the same order in which they were attached (or auto-attached)
2146c39e6a8Sdan ** to the sqlite3_session object. The order in which the changes related to
2156c39e6a8Sdan ** a single table are stored is undefined.
2166c39e6a8Sdan **
21737db03bfSdan ** Following a successful call to this function, it is the responsibility of
21837db03bfSdan ** the caller to eventually free the buffer that *ppChangeset points to using
21937db03bfSdan ** [sqlite3_free()].
220c21111daSdan **
221a2df3d9fSdan ** <h3>Changeset Generation</h3>
222c21111daSdan **
223c21111daSdan ** Once a table has been attached to a session object, the session object
224c21111daSdan ** records the primary key values of all new rows inserted into the table.
225c21111daSdan ** It also records the original primary key and other column values of any
226c21111daSdan ** deleted or updated rows. For each unique primary key value, data is only
227c21111daSdan ** recorded once - the first time a row with said primary key is inserted,
228c21111daSdan ** updated or deleted in the lifetime of the session.
229c21111daSdan **
23027453faeSdan ** There is one exception to the previous paragraph: when a row is inserted,
231157546f4Sshaneh ** updated or deleted, if one or more of its primary key columns contain a
23227453faeSdan ** NULL value, no record of the change is made.
23327453faeSdan **
234c21111daSdan ** The session object therefore accumulates two types of records - those
235c21111daSdan ** that consist of primary key values only (created when the user inserts
236c21111daSdan ** a new record) and those that consist of the primary key values and the
237c21111daSdan ** original values of other table columns (created when the users deletes
238c21111daSdan ** or updates a record).
239c21111daSdan **
240c21111daSdan ** When this function is called, the requested changeset is created using
241c21111daSdan ** both the accumulated records and the current contents of the database
242c21111daSdan ** file. Specifically:
243c21111daSdan **
244c21111daSdan ** <ul>
245c21111daSdan **   <li> For each record generated by an insert, the database is queried
246c21111daSdan **        for a row with a matching primary key. If one is found, an INSERT
247c21111daSdan **        change is added to the changeset. If no such row is found, no change
248c21111daSdan **        is added to the changeset.
249c21111daSdan **
250c21111daSdan **   <li> For each record generated by an update or delete, the database is
251c21111daSdan **        queried for a row with a matching primary key. If such a row is
252c21111daSdan **        found and one or more of the non-primary key fields have been
253c21111daSdan **        modified from their original values, an UPDATE change is added to
254c21111daSdan **        the changeset. Or, if no such row is found in the table, a DELETE
255c21111daSdan **        change is added to the changeset. If there is a row with a matching
256c21111daSdan **        primary key in the database, but all fields contain their original
257c21111daSdan **        values, no change is added to the changeset.
258c21111daSdan ** </ul>
259c21111daSdan **
260c21111daSdan ** This means, amongst other things, that if a row is inserted and then later
261157546f4Sshaneh ** deleted while a session object is active, neither the insert nor the delete
262c21111daSdan ** will be present in the changeset. Or if a row is deleted and then later a
263c21111daSdan ** row with the same primary key values inserted while a session object is
264c21111daSdan ** active, the resulting changeset will contain an UPDATE change instead of
265c21111daSdan ** a DELETE and an INSERT.
266c21111daSdan **
267c21111daSdan ** When a session object is disabled (see the [sqlite3session_enable()] API),
268c21111daSdan ** it does not accumulate records when rows are inserted, updated or deleted.
269c21111daSdan ** This may appear to have some counter-intuitive effects if a single row
270c21111daSdan ** is written to more than once during a session. For example, if a row
271c21111daSdan ** is inserted while a session object is enabled, then later deleted while
272c21111daSdan ** the same session object is disabled, no INSERT record will appear in the
273c21111daSdan ** changeset, even though the delete took place while the session was disabled.
274c21111daSdan ** Or, if one field of a row is updated while a session is disabled, and
275c21111daSdan ** another field of the same row is updated while the session is enabled, the
276c21111daSdan ** resulting changeset will contain an UPDATE change that updates both fields.
2774fccf43aSdan */
2784fccf43aSdan int sqlite3session_changeset(
2794fccf43aSdan   sqlite3_session *pSession,      /* Session object */
2804fccf43aSdan   int *pnChangeset,               /* OUT: Size of buffer at *ppChangeset */
2814fccf43aSdan   void **ppChangeset              /* OUT: Buffer containing changeset */
2824fccf43aSdan );
2834fccf43aSdan 
284cf8e9144Sdan /*
285cf8e9144Sdan ** CAPI3REF: Load The Difference Between Tables Into A Session
286cf8e9144Sdan **
287cf8e9144Sdan ** If it is not already attached to the session object passed as the first
288cf8e9144Sdan ** argument, this function attaches table zTbl in the same manner as the
289cf8e9144Sdan ** [sqlite3session_attach()] function. If zTbl does not exist, or if it
290cf8e9144Sdan ** does not have a primary key, this function is a no-op (but does not return
291cf8e9144Sdan ** an error).
292cf8e9144Sdan **
293cf8e9144Sdan ** Argument zFromDb must be the name of a database ("main", "temp" etc.)
294cf8e9144Sdan ** attached to the same database handle as the session object that contains
295cf8e9144Sdan ** a table compatible with the table attached to the session by this function.
296cf8e9144Sdan ** A table is considered compatible if it:
297cf8e9144Sdan **
298cf8e9144Sdan ** <ul>
299cf8e9144Sdan **   <li> Has the same name,
300cf8e9144Sdan **   <li> Has the same set of columns declared in the same order, and
301cf8e9144Sdan **   <li> Has the same PRIMARY KEY definition.
302cf8e9144Sdan ** </ul>
303cf8e9144Sdan **
304b9db9099Sdan ** If the tables are not compatible, SQLITE_SCHEMA is returned. If the tables
305b9db9099Sdan ** are compatible but do not have any PRIMARY KEY columns, it is not an error
306b9db9099Sdan ** but no changes are added to the session object. As with other session
307b9db9099Sdan ** APIs, tables without PRIMARY KEYs are simply ignored.
308b9db9099Sdan **
309cf8e9144Sdan ** This function adds a set of changes to the session object that could be
310cf8e9144Sdan ** used to update the table in database zFrom (call this the "from-table")
311cf8e9144Sdan ** so that its content is the same as the table attached to the session
312cf8e9144Sdan ** object (call this the "to-table"). Specifically:
313cf8e9144Sdan **
314cf8e9144Sdan ** <ul>
315cf8e9144Sdan **   <li> For each row (primary key) that exists in the to-table but not in
316cf8e9144Sdan **     the from-table, an INSERT record is added to the session object.
317cf8e9144Sdan **
318cf8e9144Sdan **   <li> For each row (primary key) that exists in the to-table but not in
319cf8e9144Sdan **     the from-table, a DELETE record is added to the session object.
320cf8e9144Sdan **
321cf8e9144Sdan **   <li> For each row (primary key) that exists in both tables, but features
322cf8e9144Sdan **     different in each, an UPDATE record is added to the session.
323cf8e9144Sdan ** </ul>
324cf8e9144Sdan **
325cf8e9144Sdan ** To clarify, if this function is called and then a changeset constructed
326cf8e9144Sdan ** using [sqlite3session_changeset()], then after applying that changeset to
327cf8e9144Sdan ** database zFrom the contents of the two compatible tables would be
328cf8e9144Sdan ** identical.
329cf8e9144Sdan **
330cf8e9144Sdan ** It an error if database zFrom does not exist or does not contain the
331cf8e9144Sdan ** required compatible table.
332cf8e9144Sdan **
333cf8e9144Sdan ** If the operation successful, SQLITE_OK is returned. Otherwise, an SQLite
334cf8e9144Sdan ** error code. In this case, if argument pzErrMsg is not NULL, *pzErrMsg
335cf8e9144Sdan ** may be set to point to a buffer containing an English language error
336cf8e9144Sdan ** message. It is the responsibility of the caller to free this buffer using
337cf8e9144Sdan ** sqlite3_free().
338cf8e9144Sdan */
339cf8e9144Sdan int sqlite3session_diff(
340cf8e9144Sdan   sqlite3_session *pSession,
341cf8e9144Sdan   const char *zFromDb,
342cf8e9144Sdan   const char *zTbl,
343cf8e9144Sdan   char **pzErrMsg
344cf8e9144Sdan );
345cf8e9144Sdan 
346ef7a6304Sdan 
347ef7a6304Sdan /*
34873b3c055Sdan ** CAPI3REF: Generate A Patchset From A Session Object
349a71d2371Sdan **
350a71d2371Sdan ** The differences between a patchset and a changeset are that:
351a71d2371Sdan **
352a71d2371Sdan ** <ul>
353a71d2371Sdan **   <li> DELETE records consist of the primary key fields only. The
354a71d2371Sdan **        original values of other fields are omitted.
355a71d2371Sdan **   <li> The original values of any modified fields are omitted from
356a71d2371Sdan **        UPDATE records.
357a71d2371Sdan ** </ul>
358a71d2371Sdan **
359a71d2371Sdan ** A patchset blob may be used with up to date versions of all
360a71d2371Sdan ** sqlite3changeset_xxx API functions except for sqlite3changeset_invert(),
361a71d2371Sdan ** which returns SQLITE_CORRUPT if it is passed a patchset. Similarly,
362a71d2371Sdan ** attempting to use a patchset blob with old versions of the
363a71d2371Sdan ** sqlite3changeset_xxx APIs also provokes an SQLITE_CORRUPT error.
364a71d2371Sdan **
365a71d2371Sdan ** Because the non-primary key "old.*" fields are omitted, no
366a71d2371Sdan ** SQLITE_CHANGESET_DATA conflicts can be detected or reported if a patchset
367a71d2371Sdan ** is passed to the sqlite3changeset_apply() API. Other conflict types work
368a71d2371Sdan ** in the same way as for changesets.
3696c39e6a8Sdan **
3706c39e6a8Sdan ** Changes within a patchset are ordered in the same way as for changesets
3716c39e6a8Sdan ** generated by the sqlite3session_changeset() function (i.e. all changes for
3726c39e6a8Sdan ** a single table are grouped together, tables appear in the order in which
3736c39e6a8Sdan ** they were attached to the session object).
37473b3c055Sdan */
37573b3c055Sdan int sqlite3session_patchset(
37673b3c055Sdan   sqlite3_session *pSession,      /* Session object */
37773b3c055Sdan   int *pnPatchset,                /* OUT: Size of buffer at *ppChangeset */
37873b3c055Sdan   void **ppPatchset               /* OUT: Buffer containing changeset */
37973b3c055Sdan );
38073b3c055Sdan 
38173b3c055Sdan /*
382b69ec348Sdan ** CAPI3REF: Test if a changeset has recorded any changes.
383b69ec348Sdan **
384b69ec348Sdan ** Return non-zero if no changes to attached tables have been recorded by
385b69ec348Sdan ** the session object passed as the first argument. Otherwise, if one or
386b69ec348Sdan ** more changes have been recorded, return zero.
387b69ec348Sdan **
388b69ec348Sdan ** Even if this function returns zero, it is possible that calling
389b69ec348Sdan ** [sqlite3session_changeset()] on the session handle may still return a
390b69ec348Sdan ** changeset that contains no changes. This can happen when a row in
391b69ec348Sdan ** an attached table is modified and then later on the original values
392b69ec348Sdan ** are restored. However, if this function returns non-zero, then it is
393b69ec348Sdan ** guaranteed that a call to sqlite3session_changeset() will return a
394b69ec348Sdan ** changeset containing zero changes.
395b69ec348Sdan */
396b69ec348Sdan int sqlite3session_isempty(sqlite3_session *pSession);
397b69ec348Sdan 
398b69ec348Sdan /*
399a2df3d9fSdan ** CAPI3REF: Create An Iterator To Traverse A Changeset
400a2df3d9fSdan **
4014fccf43aSdan ** Create an iterator used to iterate through the contents of a changeset.
402c21111daSdan ** If successful, *pp is set to point to the iterator handle and SQLITE_OK
403c21111daSdan ** is returned. Otherwise, if an error occurs, *pp is set to zero and an
404c21111daSdan ** SQLite error code is returned.
405c21111daSdan **
406c21111daSdan ** The following functions can be used to advance and query a changeset
407c21111daSdan ** iterator created by this function:
408c21111daSdan **
409c21111daSdan ** <ul>
410c21111daSdan **   <li> [sqlite3changeset_next()]
411c21111daSdan **   <li> [sqlite3changeset_op()]
412c21111daSdan **   <li> [sqlite3changeset_new()]
413c21111daSdan **   <li> [sqlite3changeset_old()]
414c21111daSdan ** </ul>
415c21111daSdan **
416c21111daSdan ** It is the responsibility of the caller to eventually destroy the iterator
417c21111daSdan ** by passing it to [sqlite3changeset_finalize()]. The buffer containing the
418c21111daSdan ** changeset (pChangeset) must remain valid until after the iterator is
419c21111daSdan ** destroyed.
420b69ec348Sdan **
421b69ec348Sdan ** Assuming the changeset blob was created by one of the
422b69ec348Sdan ** [sqlite3session_changeset()], [sqlite3changeset_concat()] or
423a93166e0Sdan ** [sqlite3changeset_invert()] functions, all changes within the changeset
424a93166e0Sdan ** that apply to a single table are grouped together. This means that when
425a93166e0Sdan ** an application iterates through a changeset using an iterator created by
426a93166e0Sdan ** this function, all changes that relate to a single table are visted
427a93166e0Sdan ** consecutively. There is no chance that the iterator will visit a change
428a93166e0Sdan ** the applies to table X, then one for table Y, and then later on visit
429a93166e0Sdan ** another change for table X.
4304fccf43aSdan */
4314fccf43aSdan int sqlite3changeset_start(
432c21111daSdan   sqlite3_changeset_iter **pp,    /* OUT: New changeset iterator handle */
433c21111daSdan   int nChangeset,                 /* Size of changeset blob in bytes */
434c21111daSdan   void *pChangeset                /* Pointer to blob containing changeset */
4354fccf43aSdan );
4364fccf43aSdan 
437ef7a6304Sdan 
438ef7a6304Sdan /*
439a2df3d9fSdan ** CAPI3REF: Advance A Changeset Iterator
440a2df3d9fSdan **
441c21111daSdan ** This function may only be used with iterators created by function
442c21111daSdan ** [sqlite3changeset_start()]. If it is called on an iterator passed to
443c21111daSdan ** a conflict-handler callback by [sqlite3changeset_apply()], SQLITE_MISUSE
444c21111daSdan ** is returned and the call has no effect.
4454fccf43aSdan **
446c21111daSdan ** Immediately after an iterator is created by sqlite3changeset_start(), it
447c21111daSdan ** does not point to any change in the changeset. Assuming the changeset
448c21111daSdan ** is not empty, the first call to this function advances the iterator to
449c21111daSdan ** point to the first change in the changeset. Each subsequent call advances
450c21111daSdan ** the iterator to point to the next change in the changeset (if any). If
451c21111daSdan ** no error occurs and the iterator points to a valid change after a call
452c21111daSdan ** to sqlite3changeset_next() has advanced it, SQLITE_ROW is returned.
453c21111daSdan ** Otherwise, if all changes in the changeset have already been visited,
454c21111daSdan ** SQLITE_DONE is returned.
455c21111daSdan **
456c21111daSdan ** If an error occurs, an SQLite error code is returned. Possible error
457c21111daSdan ** codes include SQLITE_CORRUPT (if the changeset buffer is corrupt) or
458c21111daSdan ** SQLITE_NOMEM.
4594fccf43aSdan */
4604fccf43aSdan int sqlite3changeset_next(sqlite3_changeset_iter *pIter);
4614fccf43aSdan 
4624fccf43aSdan /*
463a2df3d9fSdan ** CAPI3REF: Obtain The Current Operation From A Changeset Iterator
464a2df3d9fSdan **
465c21111daSdan ** The pIter argument passed to this function may either be an iterator
466c21111daSdan ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
467c21111daSdan ** created by [sqlite3changeset_start()]. In the latter case, the most recent
46877e65004Sdan ** call to [sqlite3changeset_next()] must have returned [SQLITE_ROW]. If this
46977e65004Sdan ** is not the case, this function returns [SQLITE_MISUSE].
470c21111daSdan **
471c21111daSdan ** If argument pzTab is not NULL, then *pzTab is set to point to a
472c21111daSdan ** nul-terminated utf-8 encoded string containing the name of the table
473c21111daSdan ** affected by the current change. The buffer remains valid until either
474c21111daSdan ** sqlite3changeset_next() is called on the iterator or until the
475c21111daSdan ** conflict-handler function returns. If pnCol is not NULL, then *pnCol is
476b4480e94Sdan ** set to the number of columns in the table affected by the change. If
477b4480e94Sdan ** pbIncorrect is not NULL, then *pbIndirect is set to true (1) if the change
478b4480e94Sdan ** is an indirect change, or false (0) otherwise. See the documentation for
479b4480e94Sdan ** [sqlite3session_indirect()] for a description of direct and indirect
480b4480e94Sdan ** changes. Finally, if pOp is not NULL, then *pOp is set to one of
481b4480e94Sdan ** [SQLITE_INSERT], [SQLITE_DELETE] or [SQLITE_UPDATE], depending on the
482b4480e94Sdan ** type of change that the iterator currently points to.
483c21111daSdan **
484c21111daSdan ** If no error occurs, SQLITE_OK is returned. If an error does occur, an
485c21111daSdan ** SQLite error code is returned. The values of the output variables may not
486c21111daSdan ** be trusted in this case.
4874fccf43aSdan */
4884fccf43aSdan int sqlite3changeset_op(
4894fccf43aSdan   sqlite3_changeset_iter *pIter,  /* Iterator object */
4904fccf43aSdan   const char **pzTab,             /* OUT: Pointer to table name */
4914fccf43aSdan   int *pnCol,                     /* OUT: Number of columns in table */
492b4480e94Sdan   int *pOp,                       /* OUT: SQLITE_INSERT, DELETE or UPDATE */
493b4480e94Sdan   int *pbIndirect                 /* OUT: True for an 'indirect' change */
4944fccf43aSdan );
495c21111daSdan 
496c21111daSdan /*
497244593c8Sdan ** CAPI3REF: Obtain The Primary Key Definition Of A Table
498244593c8Sdan **
499244593c8Sdan ** For each modified table, a changeset includes the following:
500244593c8Sdan **
501244593c8Sdan ** <ul>
502244593c8Sdan **   <li> The number of columns in the table, and
503244593c8Sdan **   <li> Which of those columns make up the tables PRIMARY KEY.
504244593c8Sdan ** </ul>
505244593c8Sdan **
506244593c8Sdan ** This function is used to find which columns comprise the PRIMARY KEY of
507244593c8Sdan ** the table modified by the change that iterator pIter currently points to.
508244593c8Sdan ** If successful, *pabPK is set to point to an array of nCol entries, where
509244593c8Sdan ** nCol is the number of columns in the table. Elements of *pabPK are set to
510244593c8Sdan ** 0x01 if the corresponding column is part of the tables primary key, or
511244593c8Sdan ** 0x00 if it is not.
512244593c8Sdan **
513244593c8Sdan ** If argumet pnCol is not NULL, then *pnCol is set to the number of columns
514244593c8Sdan ** in the table.
515244593c8Sdan **
516244593c8Sdan ** If this function is called when the iterator does not point to a valid
517244593c8Sdan ** entry, SQLITE_MISUSE is returned and the output variables zeroed. Otherwise,
518244593c8Sdan ** SQLITE_OK is returned and the output variables populated as described
519244593c8Sdan ** above.
520244593c8Sdan */
521244593c8Sdan int sqlite3changeset_pk(
522244593c8Sdan   sqlite3_changeset_iter *pIter,  /* Iterator object */
523244593c8Sdan   unsigned char **pabPK,          /* OUT: Array of boolean - true for PK cols */
524244593c8Sdan   int *pnCol                      /* OUT: Number of entries in output array */
525244593c8Sdan );
526244593c8Sdan 
527244593c8Sdan /*
528a2df3d9fSdan ** CAPI3REF: Obtain old.* Values From A Changeset Iterator
529a2df3d9fSdan **
530c21111daSdan ** The pIter argument passed to this function may either be an iterator
531c21111daSdan ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
532c21111daSdan ** created by [sqlite3changeset_start()]. In the latter case, the most recent
533c21111daSdan ** call to [sqlite3changeset_next()] must have returned SQLITE_ROW.
534c21111daSdan ** Furthermore, it may only be called if the type of change that the iterator
53577e65004Sdan ** currently points to is either [SQLITE_DELETE] or [SQLITE_UPDATE]. Otherwise,
53677e65004Sdan ** this function returns [SQLITE_MISUSE] and sets *ppValue to NULL.
537c21111daSdan **
538c21111daSdan ** Argument iVal must be greater than or equal to 0, and less than the number
539c21111daSdan ** of columns in the table affected by the current change. Otherwise,
54077e65004Sdan ** [SQLITE_RANGE] is returned and *ppValue is set to NULL.
541c21111daSdan **
542c21111daSdan ** If successful, this function sets *ppValue to point to a protected
543c21111daSdan ** sqlite3_value object containing the iVal'th value from the vector of
544c21111daSdan ** original row values stored as part of the UPDATE or DELETE change and
545c21111daSdan ** returns SQLITE_OK. The name of the function comes from the fact that this
546c21111daSdan ** is similar to the "old.*" columns available to update or delete triggers.
547c21111daSdan **
548c21111daSdan ** If some other error occurs (e.g. an OOM condition), an SQLite error code
549c21111daSdan ** is returned and *ppValue is set to NULL.
550c21111daSdan */
5514fccf43aSdan int sqlite3changeset_old(
552296c7658Sdan   sqlite3_changeset_iter *pIter,  /* Changeset iterator */
553296c7658Sdan   int iVal,                       /* Column number */
5544fccf43aSdan   sqlite3_value **ppValue         /* OUT: Old value (or NULL pointer) */
5554fccf43aSdan );
556c21111daSdan 
557c21111daSdan /*
558a2df3d9fSdan ** CAPI3REF: Obtain new.* Values From A Changeset Iterator
559a2df3d9fSdan **
560c21111daSdan ** The pIter argument passed to this function may either be an iterator
561c21111daSdan ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
562c21111daSdan ** created by [sqlite3changeset_start()]. In the latter case, the most recent
563c21111daSdan ** call to [sqlite3changeset_next()] must have returned SQLITE_ROW.
564c21111daSdan ** Furthermore, it may only be called if the type of change that the iterator
56577e65004Sdan ** currently points to is either [SQLITE_UPDATE] or [SQLITE_INSERT]. Otherwise,
56677e65004Sdan ** this function returns [SQLITE_MISUSE] and sets *ppValue to NULL.
567c21111daSdan **
568c21111daSdan ** Argument iVal must be greater than or equal to 0, and less than the number
569c21111daSdan ** of columns in the table affected by the current change. Otherwise,
57077e65004Sdan ** [SQLITE_RANGE] is returned and *ppValue is set to NULL.
571c21111daSdan **
572c21111daSdan ** If successful, this function sets *ppValue to point to a protected
573c21111daSdan ** sqlite3_value object containing the iVal'th value from the vector of
574c21111daSdan ** new row values stored as part of the UPDATE or INSERT change and
575c21111daSdan ** returns SQLITE_OK. If the change is an UPDATE and does not include
576c21111daSdan ** a new value for the requested column, *ppValue is set to NULL and
577c21111daSdan ** SQLITE_OK returned. The name of the function comes from the fact that
578c21111daSdan ** this is similar to the "new.*" columns available to update or delete
579c21111daSdan ** triggers.
580c21111daSdan **
581c21111daSdan ** If some other error occurs (e.g. an OOM condition), an SQLite error code
582c21111daSdan ** is returned and *ppValue is set to NULL.
583c21111daSdan */
5844fccf43aSdan int sqlite3changeset_new(
585296c7658Sdan   sqlite3_changeset_iter *pIter,  /* Changeset iterator */
586296c7658Sdan   int iVal,                       /* Column number */
5874fccf43aSdan   sqlite3_value **ppValue         /* OUT: New value (or NULL pointer) */
5884fccf43aSdan );
589296c7658Sdan 
590d5f0767cSdan /*
591a2df3d9fSdan ** CAPI3REF: Obtain Conflicting Row Values From A Changeset Iterator
592a2df3d9fSdan **
593c21111daSdan ** This function should only be used with iterator objects passed to a
594c21111daSdan ** conflict-handler callback by [sqlite3changeset_apply()] with either
59577e65004Sdan ** [SQLITE_CHANGESET_DATA] or [SQLITE_CHANGESET_CONFLICT]. If this function
59677e65004Sdan ** is called on any other iterator, [SQLITE_MISUSE] is returned and *ppValue
597c21111daSdan ** is set to NULL.
598d5f0767cSdan **
599c21111daSdan ** Argument iVal must be greater than or equal to 0, and less than the number
600c21111daSdan ** of columns in the table affected by the current change. Otherwise,
60177e65004Sdan ** [SQLITE_RANGE] is returned and *ppValue is set to NULL.
602c21111daSdan **
603c21111daSdan ** If successful, this function sets *ppValue to point to a protected
604c21111daSdan ** sqlite3_value object containing the iVal'th value from the
605c21111daSdan ** "conflicting row" associated with the current conflict-handler callback
606c21111daSdan ** and returns SQLITE_OK.
607c21111daSdan **
608c21111daSdan ** If some other error occurs (e.g. an OOM condition), an SQLite error code
609c21111daSdan ** is returned and *ppValue is set to NULL.
610d5f0767cSdan */
611d5f0767cSdan int sqlite3changeset_conflict(
612296c7658Sdan   sqlite3_changeset_iter *pIter,  /* Changeset iterator */
613296c7658Sdan   int iVal,                       /* Column number */
614d5f0767cSdan   sqlite3_value **ppValue         /* OUT: Value from conflicting row */
615d5f0767cSdan );
616d5f0767cSdan 
617cb3e4b79Sdan /*
618cb3e4b79Sdan ** CAPI3REF: Determine The Number Of Foreign Key Constraint Violations
619cb3e4b79Sdan **
620cb3e4b79Sdan ** This function may only be called with an iterator passed to an
621cb3e4b79Sdan ** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case
622cb3e4b79Sdan ** it sets the output variable to the total number of known foreign key
623cb3e4b79Sdan ** violations in the destination database and returns SQLITE_OK.
624cb3e4b79Sdan **
625cb3e4b79Sdan ** In all other cases this function returns SQLITE_MISUSE.
626cb3e4b79Sdan */
627cb3e4b79Sdan int sqlite3changeset_fk_conflicts(
628cb3e4b79Sdan   sqlite3_changeset_iter *pIter,  /* Changeset iterator */
629cb3e4b79Sdan   int *pnOut                      /* OUT: Number of FK violations */
630cb3e4b79Sdan );
631cb3e4b79Sdan 
6324fccf43aSdan 
6334fccf43aSdan /*
634157546f4Sshaneh ** CAPI3REF: Finalize A Changeset Iterator
635a2df3d9fSdan **
636a2df3d9fSdan ** This function is used to finalize an iterator allocated with
63777e65004Sdan ** [sqlite3changeset_start()].
6384fccf43aSdan **
639c21111daSdan ** This function should only be called on iterators created using the
640c21111daSdan ** [sqlite3changeset_start()] function. If an application calls this
641c21111daSdan ** function with an iterator passed to a conflict-handler by
64277e65004Sdan ** [sqlite3changeset_apply()], [SQLITE_MISUSE] is immediately returned and the
643c21111daSdan ** call has no effect.
644c21111daSdan **
645c21111daSdan ** If an error was encountered within a call to an sqlite3changeset_xxx()
64677e65004Sdan ** function (for example an [SQLITE_CORRUPT] in [sqlite3changeset_next()] or an
64777e65004Sdan ** [SQLITE_NOMEM] in [sqlite3changeset_new()]) then an error code corresponding
648c21111daSdan ** to that error is returned by this function. Otherwise, SQLITE_OK is
649c21111daSdan ** returned. This is to allow the following pattern (pseudo-code):
650c21111daSdan **
651c21111daSdan **   sqlite3changeset_start();
652c21111daSdan **   while( SQLITE_ROW==sqlite3changeset_next() ){
653c21111daSdan **     // Do something with change.
654c21111daSdan **   }
655c21111daSdan **   rc = sqlite3changeset_finalize();
656c21111daSdan **   if( rc!=SQLITE_OK ){
657c21111daSdan **     // An error has occurred
658c21111daSdan **   }
6594fccf43aSdan */
6604fccf43aSdan int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter);
6614fccf43aSdan 
66291ddd559Sdan /*
663a2df3d9fSdan ** CAPI3REF: Invert A Changeset
664a2df3d9fSdan **
6652635a3beSdan ** This function is used to "invert" a changeset object. Applying an inverted
6662635a3beSdan ** changeset to a database reverses the effects of applying the uninverted
6672635a3beSdan ** changeset. Specifically:
6682635a3beSdan **
6692635a3beSdan ** <ul>
6702635a3beSdan **   <li> Each DELETE change is changed to an INSERT, and
6712635a3beSdan **   <li> Each INSERT change is changed to a DELETE, and
6722635a3beSdan **   <li> For each UPDATE change, the old.* and new.* values are exchanged.
6732635a3beSdan ** </ul>
6742635a3beSdan **
6756c39e6a8Sdan ** This function does not change the order in which changes appear within
6766c39e6a8Sdan ** the changeset. It merely reverses the sense of each individual change.
6776c39e6a8Sdan **
6782635a3beSdan ** If successful, a pointer to a buffer containing the inverted changeset
6792635a3beSdan ** is stored in *ppOut, the size of the same buffer is stored in *pnOut, and
6802635a3beSdan ** SQLITE_OK is returned. If an error occurs, both *pnOut and *ppOut are
6812635a3beSdan ** zeroed and an SQLite error code returned.
6822635a3beSdan **
6832635a3beSdan ** It is the responsibility of the caller to eventually call sqlite3_free()
6842635a3beSdan ** on the *ppOut pointer to free the buffer allocation following a successful
6852635a3beSdan ** call to this function.
686f51e5f6cSdan **
687f51e5f6cSdan ** WARNING/TODO: This function currently assumes that the input is a valid
688f51e5f6cSdan ** changeset. If it is not, the results are undefined.
68991ddd559Sdan */
69091ddd559Sdan int sqlite3changeset_invert(
691cfec7eeeSdan   int nIn, const void *pIn,       /* Input changeset */
69291ddd559Sdan   int *pnOut, void **ppOut        /* OUT: Inverse of input */
69391ddd559Sdan );
69491ddd559Sdan 
6956cda207fSdan /*
69629e03e97Sdan ** CAPI3REF: Concatenate Two Changeset Objects
69729e03e97Sdan **
69829e03e97Sdan ** This function is used to concatenate two changesets, A and B, into a
69929e03e97Sdan ** single changeset. The result is a changeset equivalent to applying
70029e03e97Sdan ** changeset A followed by changeset B.
70129e03e97Sdan **
7025898ad69Sdan ** This function combines the two input changesets using an
7035898ad69Sdan ** sqlite3_changegroup object. Calling it produces similar results as the
7045898ad69Sdan ** following code fragment:
70529e03e97Sdan **
7065898ad69Sdan **   sqlite3_changegroup *pGrp;
7075898ad69Sdan **   rc = sqlite3_changegroup_new(&pGrp);
7085898ad69Sdan **   if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nA, pA);
7095898ad69Sdan **   if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nB, pB);
7105898ad69Sdan **   if( rc==SQLITE_OK ){
7115898ad69Sdan **     rc = sqlite3changegroup_output(pGrp, pnOut, ppOut);
7125898ad69Sdan **   }else{
7135898ad69Sdan **     *ppOut = 0;
7145898ad69Sdan **     *pnOut = 0;
7155898ad69Sdan **   }
71629e03e97Sdan **
7175898ad69Sdan ** Refer to the sqlite3_changegroup documentation below for details.
7186cda207fSdan */
7195d607a6eSdan int sqlite3changeset_concat(
72029e03e97Sdan   int nA,                         /* Number of bytes in buffer pA */
72129e03e97Sdan   void *pA,                       /* Pointer to buffer containing changeset A */
72229e03e97Sdan   int nB,                         /* Number of bytes in buffer pB */
72329e03e97Sdan   void *pB,                       /* Pointer to buffer containing changeset B */
72429e03e97Sdan   int *pnOut,                     /* OUT: Number of bytes in output changeset */
72529e03e97Sdan   void **ppOut                    /* OUT: Buffer containing output changeset */
7265d607a6eSdan );
7275d607a6eSdan 
7285898ad69Sdan 
7295898ad69Sdan /*
7305898ad69Sdan ** Changegroup handle.
7315898ad69Sdan */
7325898ad69Sdan typedef struct sqlite3_changegroup sqlite3_changegroup;
7335898ad69Sdan 
7345898ad69Sdan /*
7355898ad69Sdan ** CAPI3REF: Combine two or more changesets into a single changeset.
7365898ad69Sdan **
7375898ad69Sdan ** An sqlite3_changegroup object is used to combine two or more changesets
7385898ad69Sdan ** (or patchsets) into a single changeset (or patchset). A single changegroup
7395898ad69Sdan ** object may combine changesets or patchsets, but not both. The output is
7405898ad69Sdan ** always in the same format as the input.
7415898ad69Sdan **
7425898ad69Sdan ** If successful, this function returns SQLITE_OK and populates (*pp) with
7435898ad69Sdan ** a pointer to a new sqlite3_changegroup object before returning. The caller
7445898ad69Sdan ** should eventually free the returned object using a call to
7455898ad69Sdan ** sqlite3changegroup_delete(). If an error occurs, an SQLite error code
7465898ad69Sdan ** (i.e. SQLITE_NOMEM) is returned and *pp is set to NULL.
7475898ad69Sdan **
7485898ad69Sdan ** The usual usage pattern for an sqlite3_changegroup object is as follows:
7495898ad69Sdan **
7505898ad69Sdan ** <ul>
7515898ad69Sdan **   <li> It is created using a call to sqlite3changegroup_new().
7525898ad69Sdan **
7535898ad69Sdan **   <li> Zero or more changesets (or patchsets) are added to the object
7545898ad69Sdan **        by calling sqlite3changegroup_add().
7555898ad69Sdan **
7565898ad69Sdan **   <li> The result of combining all input changesets together is obtained
7575898ad69Sdan **        by the application via a call to sqlite3changegroup_output().
7585898ad69Sdan **
7595898ad69Sdan **   <li> The object is deleted using a call to sqlite3changegroup_delete().
7605898ad69Sdan ** </ul>
7615898ad69Sdan **
7625898ad69Sdan ** Any number of calls to add() and output() may be made between the calls to
7635898ad69Sdan ** new() and delete(), and in any order.
7645898ad69Sdan **
7655898ad69Sdan ** As well as the regular sqlite3changegroup_add() and
7665898ad69Sdan ** sqlite3changegroup_output() functions, also available are the streaming
7675898ad69Sdan ** versions sqlite3changegroup_add_strm() and sqlite3changegroup_output_strm().
7685898ad69Sdan */
7695898ad69Sdan int sqlite3changegroup_new(sqlite3_changegroup **pp);
7705898ad69Sdan 
7715898ad69Sdan /*
7725898ad69Sdan ** Add all changes within the changeset (or patchset) in buffer pData (size
7735898ad69Sdan ** nData bytes) to the changegroup.
7745898ad69Sdan **
7755898ad69Sdan ** If the buffer contains a patchset, then all prior calls to this function
7765898ad69Sdan ** on the same changegroup object must also have specified patchsets. Or, if
7775898ad69Sdan ** the buffer contains a changeset, so must have the earlier calls to this
7785898ad69Sdan ** function. Otherwise, SQLITE_ERROR is returned and no changes are added
7795898ad69Sdan ** to the changegroup.
7805898ad69Sdan **
7815898ad69Sdan ** Rows within the changeset and changegroup are identified by the values in
7825898ad69Sdan ** their PRIMARY KEY columns. A change in the changeset is considered to
7835898ad69Sdan ** apply to the same row as a change already present in the changegroup if
7845898ad69Sdan ** the two rows have the same primary key.
7855898ad69Sdan **
7865898ad69Sdan ** Changes to rows that that do not already appear in the changegroup are
7875898ad69Sdan ** simply copied into it. Or, if both the new changeset and the changegroup
7885898ad69Sdan ** contain changes that apply to a single row, the final contents of the
7895898ad69Sdan ** changegroup depends on the type of each change, as follows:
7905898ad69Sdan **
7915898ad69Sdan ** <table border=1 style="margin-left:8ex;margin-right:8ex">
7925898ad69Sdan **   <tr><th style="white-space:pre">Existing Change  </th>
7935898ad69Sdan **       <th style="white-space:pre">New Change       </th>
7945898ad69Sdan **       <th>Output Change
7955898ad69Sdan **   <tr><td>INSERT <td>INSERT <td>
7965898ad69Sdan **       The new change is ignored. This case does not occur if the new
7975898ad69Sdan **       changeset was recorded immediately after the changesets already
7985898ad69Sdan **       added to the changegroup.
7995898ad69Sdan **   <tr><td>INSERT <td>UPDATE <td>
8005898ad69Sdan **       The INSERT change remains in the changegroup. The values in the
8015898ad69Sdan **       INSERT change are modified as if the row was inserted by the
8025898ad69Sdan **       existing change and then updated according to the new change.
8035898ad69Sdan **   <tr><td>INSERT <td>DELETE <td>
8045898ad69Sdan **       The existing INSERT is removed from the changegroup. The DELETE is
8055898ad69Sdan **       not added.
8065898ad69Sdan **   <tr><td>UPDATE <td>INSERT <td>
8075898ad69Sdan **       The new change is ignored. This case does not occur if the new
8085898ad69Sdan **       changeset was recorded immediately after the changesets already
8095898ad69Sdan **       added to the changegroup.
8105898ad69Sdan **   <tr><td>UPDATE <td>UPDATE <td>
8115898ad69Sdan **       The existing UPDATE remains within the changegroup. It is amended
8125898ad69Sdan **       so that the accompanying values are as if the row was updated once
8135898ad69Sdan **       by the existing change and then again by the new change.
8145898ad69Sdan **   <tr><td>UPDATE <td>DELETE <td>
8155898ad69Sdan **       The existing UPDATE is replaced by the new DELETE within the
8165898ad69Sdan **       changegroup.
8175898ad69Sdan **   <tr><td>DELETE <td>INSERT <td>
8185898ad69Sdan **       If one or more of the column values in the row inserted by the
8195898ad69Sdan **       new change differ from those in the row deleted by the existing
8205898ad69Sdan **       change, the existing DELETE is replaced by an UPDATE within the
8215898ad69Sdan **       changegroup. Otherwise, if the inserted row is exactly the same
8225898ad69Sdan **       as the deleted row, the existing DELETE is simply discarded.
8235898ad69Sdan **   <tr><td>DELETE <td>UPDATE <td>
8245898ad69Sdan **       The new change is ignored. This case does not occur if the new
8255898ad69Sdan **       changeset was recorded immediately after the changesets already
8265898ad69Sdan **       added to the changegroup.
8275898ad69Sdan **   <tr><td>DELETE <td>DELETE <td>
8285898ad69Sdan **       The new change is ignored. This case does not occur if the new
8295898ad69Sdan **       changeset was recorded immediately after the changesets already
8305898ad69Sdan **       added to the changegroup.
8315898ad69Sdan ** </table>
8325898ad69Sdan **
8335898ad69Sdan ** If the new changeset contains changes to a table that is already present
8345898ad69Sdan ** in the changegroup, then the number of columns and the position of the
8355898ad69Sdan ** primary key columns for the table must be consistent. If this is not the
8365898ad69Sdan ** case, this function fails with SQLITE_SCHEMA. If the input changeset
8375898ad69Sdan ** appears to be corrupt and the corruption is detected, SQLITE_CORRUPT is
8385898ad69Sdan ** returned. Or, if an out-of-memory condition occurs during processing, this
8395898ad69Sdan ** function returns SQLITE_NOMEM. In all cases, if an error occurs the
8405898ad69Sdan ** final contents of the changegroup is undefined.
8415898ad69Sdan **
8425898ad69Sdan ** If no error occurs, SQLITE_OK is returned.
8435898ad69Sdan */
8445898ad69Sdan int sqlite3changegroup_add(sqlite3_changegroup*, int nData, void *pData);
8455898ad69Sdan 
8465898ad69Sdan /*
8475898ad69Sdan ** Obtain a buffer containing a changeset (or patchset) representing the
8485898ad69Sdan ** current contents of the changegroup. If the inputs to the changegroup
8495898ad69Sdan ** were themselves changesets, the output is a changeset. Or, if the
8505898ad69Sdan ** inputs were patchsets, the output is also a patchset.
8515898ad69Sdan **
8526c39e6a8Sdan ** As with the output of the sqlite3session_changeset() and
8536c39e6a8Sdan ** sqlite3session_patchset() functions, all changes related to a single
8546c39e6a8Sdan ** table are grouped together in the output of this function. Tables appear
8556c39e6a8Sdan ** in the same order as for the very first changeset added to the changegroup.
8566c39e6a8Sdan ** If the second or subsequent changesets added to the changegroup contain
8576c39e6a8Sdan ** changes for tables that do not appear in the first changeset, they are
8586c39e6a8Sdan ** appended onto the end of the output changeset, again in the order in
8596c39e6a8Sdan ** which they are first encountered.
8606c39e6a8Sdan **
8615898ad69Sdan ** If an error occurs, an SQLite error code is returned and the output
8625898ad69Sdan ** variables (*pnData) and (*ppData) are set to 0. Otherwise, SQLITE_OK
8635898ad69Sdan ** is returned and the output variables are set to the size of and a
8645898ad69Sdan ** pointer to the output buffer, respectively. In this case it is the
8655898ad69Sdan ** responsibility of the caller to eventually free the buffer using a
8665898ad69Sdan ** call to sqlite3_free().
8675898ad69Sdan */
8685898ad69Sdan int sqlite3changegroup_output(
8695898ad69Sdan   sqlite3_changegroup*,
8705898ad69Sdan   int *pnData,                    /* OUT: Size of output buffer in bytes */
8715898ad69Sdan   void **ppData                   /* OUT: Pointer to output buffer */
8725898ad69Sdan );
8735898ad69Sdan 
8745898ad69Sdan /*
8755898ad69Sdan ** Delete a changegroup object.
8765898ad69Sdan */
8775898ad69Sdan void sqlite3changegroup_delete(sqlite3_changegroup*);
8785898ad69Sdan 
879d5f0767cSdan /*
880a2df3d9fSdan ** CAPI3REF: Apply A Changeset To A Database
881a2df3d9fSdan **
8822635a3beSdan ** Apply a changeset to a database. This function attempts to update the
8832635a3beSdan ** "main" database attached to handle db with the changes found in the
8842635a3beSdan ** changeset passed via the second and third arguments.
8852635a3beSdan **
88640368988Sdan ** The fourth argument (xFilter) passed to this function is the "filter
88740368988Sdan ** callback". If it is not NULL, then for each table affected by at least one
88840368988Sdan ** change in the changeset, the filter callback is invoked with
88940368988Sdan ** the table name as the second argument, and a copy of the context pointer
89040368988Sdan ** passed as the sixth argument to this function as the first. If the "filter
89140368988Sdan ** callback" returns zero, then no attempt is made to apply any changes to
89240368988Sdan ** the table. Otherwise, if the return value is non-zero or the xFilter
89340368988Sdan ** argument to this function is NULL, all changes related to the table are
89440368988Sdan ** attempted.
89540368988Sdan **
89640368988Sdan ** For each table that is not excluded by the filter callback, this function
89740368988Sdan ** tests that the target database contains a compatible table. A table is
89840368988Sdan ** considered compatible if all of the following are true:
8992635a3beSdan **
9002635a3beSdan ** <ul>
9012635a3beSdan **   <li> The table has the same name as the name recorded in the
9022635a3beSdan **        changeset, and
9032635a3beSdan **   <li> The table has the same number of columns as recorded in the
9042635a3beSdan **        changeset, and
9052635a3beSdan **   <li> The table has primary key columns in the same position as
9062635a3beSdan **        recorded in the changeset.
9072635a3beSdan ** </ul>
9082635a3beSdan **
90940368988Sdan ** If there is no compatible table, it is not an error, but none of the
91040368988Sdan ** changes associated with the table are applied. A warning message is issued
91140368988Sdan ** via the sqlite3_log() mechanism with the error code SQLITE_SCHEMA. At most
91240368988Sdan ** one such warning is issued for each table in the changeset.
9132635a3beSdan **
91440368988Sdan ** For each change for which there is a compatible table, an attempt is made
91540368988Sdan ** to modify the table contents according to the UPDATE, INSERT or DELETE
91640368988Sdan ** change. If a change cannot be applied cleanly, the conflict handler
91740368988Sdan ** function passed as the fifth argument to sqlite3changeset_apply() may be
91840368988Sdan ** invoked. A description of exactly when the conflict handler is invoked for
91940368988Sdan ** each type of change is below.
9202635a3beSdan **
921082c96dfSdan ** Unlike the xFilter argument, xConflict may not be passed NULL. The results
922082c96dfSdan ** of passing anything other than a valid function pointer as the xConflict
923082c96dfSdan ** argument are undefined.
924082c96dfSdan **
9252635a3beSdan ** Each time the conflict handler function is invoked, it must return one
9262635a3beSdan ** of [SQLITE_CHANGESET_OMIT], [SQLITE_CHANGESET_ABORT] or
9272635a3beSdan ** [SQLITE_CHANGESET_REPLACE]. SQLITE_CHANGESET_REPLACE may only be returned
9282635a3beSdan ** if the second argument passed to the conflict handler is either
9292635a3beSdan ** SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If the conflict-handler
9302635a3beSdan ** returns an illegal value, any changes already made are rolled back and
9312635a3beSdan ** the call to sqlite3changeset_apply() returns SQLITE_MISUSE. Different
9322635a3beSdan ** actions are taken by sqlite3changeset_apply() depending on the value
9332635a3beSdan ** returned by each invocation of the conflict-handler function. Refer to
9342635a3beSdan ** the documentation for the three
9352635a3beSdan ** [SQLITE_CHANGESET_OMIT|available return values] for details.
9362635a3beSdan **
9372635a3beSdan ** <dl>
9382635a3beSdan ** <dt>DELETE Changes<dd>
9392635a3beSdan **   For each DELETE change, this function checks if the target database
9402635a3beSdan **   contains a row with the same primary key value (or values) as the
9412635a3beSdan **   original row values stored in the changeset. If it does, and the values
9422635a3beSdan **   stored in all non-primary key columns also match the values stored in
9432635a3beSdan **   the changeset the row is deleted from the target database.
9442635a3beSdan **
9452635a3beSdan **   If a row with matching primary key values is found, but one or more of
9462635a3beSdan **   the non-primary key fields contains a value different from the original
9472635a3beSdan **   row value stored in the changeset, the conflict-handler function is
9482635a3beSdan **   invoked with [SQLITE_CHANGESET_DATA] as the second argument.
9492635a3beSdan **
9502635a3beSdan **   If no row with matching primary key values is found in the database,
9512635a3beSdan **   the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
9522635a3beSdan **   passed as the second argument.
9532635a3beSdan **
9542635a3beSdan **   If the DELETE operation is attempted, but SQLite returns SQLITE_CONSTRAINT
9552635a3beSdan **   (which can only happen if a foreign key constraint is violated), the
9562635a3beSdan **   conflict-handler function is invoked with [SQLITE_CHANGESET_CONSTRAINT]
9572635a3beSdan **   passed as the second argument. This includes the case where the DELETE
9582635a3beSdan **   operation is attempted because an earlier call to the conflict handler
9592635a3beSdan **   function returned [SQLITE_CHANGESET_REPLACE].
9602635a3beSdan **
9612635a3beSdan ** <dt>INSERT Changes<dd>
9622635a3beSdan **   For each INSERT change, an attempt is made to insert the new row into
9632635a3beSdan **   the database.
9642635a3beSdan **
9652635a3beSdan **   If the attempt to insert the row fails because the database already
9662635a3beSdan **   contains a row with the same primary key values, the conflict handler
9672635a3beSdan **   function is invoked with the second argument set to
9682635a3beSdan **   [SQLITE_CHANGESET_CONFLICT].
9692635a3beSdan **
9702635a3beSdan **   If the attempt to insert the row fails because of some other constraint
9712635a3beSdan **   violation (e.g. NOT NULL or UNIQUE), the conflict handler function is
9722635a3beSdan **   invoked with the second argument set to [SQLITE_CHANGESET_CONSTRAINT].
9732635a3beSdan **   This includes the case where the INSERT operation is re-attempted because
9742635a3beSdan **   an earlier call to the conflict handler function returned
9752635a3beSdan **   [SQLITE_CHANGESET_REPLACE].
9762635a3beSdan **
9772635a3beSdan ** <dt>UPDATE Changes<dd>
978a2df3d9fSdan **   For each UPDATE change, this function checks if the target database
9792635a3beSdan **   contains a row with the same primary key value (or values) as the
9802635a3beSdan **   original row values stored in the changeset. If it does, and the values
9812635a3beSdan **   stored in all non-primary key columns also match the values stored in
9822635a3beSdan **   the changeset the row is updated within the target database.
9832635a3beSdan **
9842635a3beSdan **   If a row with matching primary key values is found, but one or more of
9852635a3beSdan **   the non-primary key fields contains a value different from an original
9862635a3beSdan **   row value stored in the changeset, the conflict-handler function is
9872635a3beSdan **   invoked with [SQLITE_CHANGESET_DATA] as the second argument. Since
9882635a3beSdan **   UPDATE changes only contain values for non-primary key fields that are
9892635a3beSdan **   to be modified, only those fields need to match the original values to
9902635a3beSdan **   avoid the SQLITE_CHANGESET_DATA conflict-handler callback.
9912635a3beSdan **
9922635a3beSdan **   If no row with matching primary key values is found in the database,
9932635a3beSdan **   the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
9942635a3beSdan **   passed as the second argument.
9952635a3beSdan **
9962635a3beSdan **   If the UPDATE operation is attempted, but SQLite returns
9972635a3beSdan **   SQLITE_CONSTRAINT, the conflict-handler function is invoked with
9982635a3beSdan **   [SQLITE_CHANGESET_CONSTRAINT] passed as the second argument.
9992635a3beSdan **   This includes the case where the UPDATE operation is attempted after
10002635a3beSdan **   an earlier call to the conflict handler function returned
10012635a3beSdan **   [SQLITE_CHANGESET_REPLACE].
10022635a3beSdan ** </dl>
1003d5f0767cSdan **
1004d5f0767cSdan ** It is safe to execute SQL statements, including those that write to the
1005d5f0767cSdan ** table that the callback related to, from within the xConflict callback.
1006d5f0767cSdan ** This can be used to further customize the applications conflict
1007d5f0767cSdan ** resolution strategy.
10082635a3beSdan **
10092635a3beSdan ** All changes made by this function are enclosed in a savepoint transaction.
10102635a3beSdan ** If any other error (aside from a constraint failure when attempting to
10112635a3beSdan ** write to the target database) occurs, then the savepoint transaction is
10122635a3beSdan ** rolled back, restoring the target database to its original state, and an
10132635a3beSdan ** SQLite error code returned.
1014d5f0767cSdan */
1015d5f0767cSdan int sqlite3changeset_apply(
1016296c7658Sdan   sqlite3 *db,                    /* Apply change to "main" db of this handle */
1017296c7658Sdan   int nChangeset,                 /* Size of changeset in bytes */
1018296c7658Sdan   void *pChangeset,               /* Changeset blob */
101940368988Sdan   int(*xFilter)(
102040368988Sdan     void *pCtx,                   /* Copy of sixth arg to _apply() */
102140368988Sdan     const char *zTab              /* Table name */
102240368988Sdan   ),
1023d5f0767cSdan   int(*xConflict)(
102440368988Sdan     void *pCtx,                   /* Copy of sixth arg to _apply() */
1025d5f0767cSdan     int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
1026d5f0767cSdan     sqlite3_changeset_iter *p     /* Handle describing change and conflict */
1027d5f0767cSdan   ),
1028296c7658Sdan   void *pCtx                      /* First argument passed to xConflict */
1029d5f0767cSdan );
1030d5f0767cSdan 
10310c698471Sdan /*
1032157546f4Sshaneh ** CAPI3REF: Constants Passed To The Conflict Handler
1033a2df3d9fSdan **
10342635a3beSdan ** Values that may be passed as the second argument to a conflict-handler.
10350c698471Sdan **
10362635a3beSdan ** <dl>
10372635a3beSdan ** <dt>SQLITE_CHANGESET_DATA<dd>
10380c698471Sdan **   The conflict handler is invoked with CHANGESET_DATA as the second argument
10390c698471Sdan **   when processing a DELETE or UPDATE change if a row with the required
10400c698471Sdan **   PRIMARY KEY fields is present in the database, but one or more other
10410c698471Sdan **   (non primary-key) fields modified by the update do not contain the
10420c698471Sdan **   expected "before" values.
10430c698471Sdan **
10440c698471Sdan **   The conflicting row, in this case, is the database row with the matching
10450c698471Sdan **   primary key.
10460c698471Sdan **
10472635a3beSdan ** <dt>SQLITE_CHANGESET_NOTFOUND<dd>
10480c698471Sdan **   The conflict handler is invoked with CHANGESET_NOTFOUND as the second
10490c698471Sdan **   argument when processing a DELETE or UPDATE change if a row with the
10500c698471Sdan **   required PRIMARY KEY fields is not present in the database.
10510c698471Sdan **
10520c698471Sdan **   There is no conflicting row in this case. The results of invoking the
10530c698471Sdan **   sqlite3changeset_conflict() API are undefined.
10540c698471Sdan **
10552635a3beSdan ** <dt>SQLITE_CHANGESET_CONFLICT<dd>
10560c698471Sdan **   CHANGESET_CONFLICT is passed as the second argument to the conflict
10572635a3beSdan **   handler while processing an INSERT change if the operation would result
10582635a3beSdan **   in duplicate primary key values.
10590c698471Sdan **
10600c698471Sdan **   The conflicting row in this case is the database row with the matching
10610c698471Sdan **   primary key.
10620c698471Sdan **
1063cb3e4b79Sdan ** <dt>SQLITE_CHANGESET_FOREIGN_KEY<dd>
1064cb3e4b79Sdan **   If foreign key handling is enabled, and applying a changeset leaves the
1065cb3e4b79Sdan **   database in a state containing foreign key violations, the conflict
1066cb3e4b79Sdan **   handler is invoked with CHANGESET_FOREIGN_KEY as the second argument
1067cb3e4b79Sdan **   exactly once before the changeset is committed. If the conflict handler
1068cb3e4b79Sdan **   returns CHANGESET_OMIT, the changes, including those that caused the
1069cb3e4b79Sdan **   foreign key constraint violation, are committed. Or, if it returns
1070cb3e4b79Sdan **   CHANGESET_ABORT, the changeset is rolled back.
1071cb3e4b79Sdan **
1072cb3e4b79Sdan **   No current or conflicting row information is provided. The only function
1073cb3e4b79Sdan **   it is possible to call on the supplied sqlite3_changeset_iter handle
1074cb3e4b79Sdan **   is sqlite3changeset_fk_conflicts().
1075cb3e4b79Sdan **
10762635a3beSdan ** <dt>SQLITE_CHANGESET_CONSTRAINT<dd>
10770c698471Sdan **   If any other constraint violation occurs while applying a change (i.e.
1078cb3e4b79Sdan **   a UNIQUE, CHECK or NOT NULL constraint), the conflict handler is
1079cb3e4b79Sdan **   invoked with CHANGESET_CONSTRAINT as the second argument.
10800c698471Sdan **
10810c698471Sdan **   There is no conflicting row in this case. The results of invoking the
10820c698471Sdan **   sqlite3changeset_conflict() API are undefined.
1083cb3e4b79Sdan **
10842635a3beSdan ** </dl>
10850c698471Sdan */
1086d5f0767cSdan #define SQLITE_CHANGESET_DATA        1
1087d5f0767cSdan #define SQLITE_CHANGESET_NOTFOUND    2
1088d5f0767cSdan #define SQLITE_CHANGESET_CONFLICT    3
1089d5f0767cSdan #define SQLITE_CHANGESET_CONSTRAINT  4
1090cb3e4b79Sdan #define SQLITE_CHANGESET_FOREIGN_KEY 5
1091d5f0767cSdan 
10920c698471Sdan /*
1093a2df3d9fSdan ** CAPI3REF: Constants Returned By The Conflict Handler
1094a2df3d9fSdan **
1095a2df3d9fSdan ** A conflict handler callback must return one of the following three values.
10960c698471Sdan **
10972635a3beSdan ** <dl>
10982635a3beSdan ** <dt>SQLITE_CHANGESET_OMIT<dd>
10990c698471Sdan **   If a conflict handler returns this value no special action is taken. The
11002635a3beSdan **   change that caused the conflict is not applied. The session module
11012635a3beSdan **   continues to the next change in the changeset.
11020c698471Sdan **
11032635a3beSdan ** <dt>SQLITE_CHANGESET_REPLACE<dd>
11040c698471Sdan **   This value may only be returned if the second argument to the conflict
11050c698471Sdan **   handler was SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If this
11060c698471Sdan **   is not the case, any changes applied so far are rolled back and the
11070c698471Sdan **   call to sqlite3changeset_apply() returns SQLITE_MISUSE.
11080c698471Sdan **
11090c698471Sdan **   If CHANGESET_REPLACE is returned by an SQLITE_CHANGESET_DATA conflict
11100c698471Sdan **   handler, then the conflicting row is either updated or deleted, depending
11110c698471Sdan **   on the type of change.
11120c698471Sdan **
11130c698471Sdan **   If CHANGESET_REPLACE is returned by an SQLITE_CHANGESET_CONFLICT conflict
11140c698471Sdan **   handler, then the conflicting row is removed from the database and a
11150c698471Sdan **   second attempt to apply the change is made. If this second attempt fails,
11160c698471Sdan **   the original row is restored to the database before continuing.
11170c698471Sdan **
11182635a3beSdan ** <dt>SQLITE_CHANGESET_ABORT<dd>
11190c698471Sdan **   If this value is returned, any changes applied so far are rolled back
11202635a3beSdan **   and the call to sqlite3changeset_apply() returns SQLITE_ABORT.
11212635a3beSdan ** </dl>
11220c698471Sdan */
1123d5f0767cSdan #define SQLITE_CHANGESET_OMIT       0
1124d5f0767cSdan #define SQLITE_CHANGESET_REPLACE    1
1125d5f0767cSdan #define SQLITE_CHANGESET_ABORT      2
112691ddd559Sdan 
112736828bd9Sdrh /*
112816228167Sdan ** CAPI3REF: Streaming Versions of API functions.
112916228167Sdan **
1130f1a08ad8Sdrh ** The six streaming API xxx_strm() functions serve similar purposes to the
113116228167Sdan ** corresponding non-streaming API functions:
113216228167Sdan **
113316228167Sdan ** <table border=1 style="margin-left:8ex;margin-right:8ex">
113416228167Sdan **   <tr><th>Streaming function<th>Non-streaming equivalent</th>
113516228167Sdan **   <tr><td>sqlite3changeset_apply_str<td>[sqlite3changeset_apply]
113616228167Sdan **   <tr><td>sqlite3changeset_concat_str<td>[sqlite3changeset_concat]
113716228167Sdan **   <tr><td>sqlite3changeset_invert_str<td>[sqlite3changeset_invert]
113816228167Sdan **   <tr><td>sqlite3changeset_start_str<td>[sqlite3changeset_start]
113916228167Sdan **   <tr><td>sqlite3session_changeset_str<td>[sqlite3session_changeset]
114016228167Sdan **   <tr><td>sqlite3session_patchset_str<td>[sqlite3session_patchset]
114116228167Sdan ** </table>
114216228167Sdan **
114316228167Sdan ** Non-streaming functions that accept changesets (or patchsets) as input
114416228167Sdan ** require that the entire changeset be stored in a single buffer in memory.
114516228167Sdan ** Similarly, those that return a changeset or patchset do so by returning
114616228167Sdan ** a pointer to a single large buffer allocated using sqlite3_malloc().
114716228167Sdan ** Normally this is convenient. However, if an application running in a
114816228167Sdan ** low-memory environment is required to handle very large changesets, the
114916228167Sdan ** large contiguous memory allocations required can become onerous.
115016228167Sdan **
115116228167Sdan ** In order to avoid this problem, instead of a single large buffer, input
115216228167Sdan ** is passed to a streaming API functions by way of a callback function that
115316228167Sdan ** the sessions module invokes to incrementally request input data as it is
115416228167Sdan ** required. In all cases, a pair of API function parameters such as
115516228167Sdan **
115616228167Sdan **  <pre>
115716228167Sdan **  &nbsp;     int nChangeset,
115816228167Sdan **  &nbsp;     void *pChangeset,
115916228167Sdan **  </pre>
116016228167Sdan **
116116228167Sdan ** Is replaced by:
116216228167Sdan **
116316228167Sdan **  <pre>
116416228167Sdan **  &nbsp;     int (*xInput)(void *pIn, void *pData, int *pnData),
116516228167Sdan **  &nbsp;     void *pIn,
116616228167Sdan **  </pre>
116716228167Sdan **
116816228167Sdan ** Each time the xInput callback is invoked by the sessions module, the first
116916228167Sdan ** argument passed is a copy of the supplied pIn context pointer. The second
117016228167Sdan ** argument, pData, points to a buffer (*pnData) bytes in size. Assuming no
117116228167Sdan ** error occurs the xInput method should copy up to (*pnData) bytes of data
117216228167Sdan ** into the buffer and set (*pnData) to the actual number of bytes copied
117316228167Sdan ** before returning SQLITE_OK. If the input is completely exhausted, (*pnData)
117416228167Sdan ** should be set to zero to indicate this. Or, if an error occurs, an SQLite
117516228167Sdan ** error code should be returned. In all cases, if an xInput callback returns
117616228167Sdan ** an error, all processing is abandoned and the streaming API function
117716228167Sdan ** returns a copy of the error code to the caller.
117816228167Sdan **
1179f1a08ad8Sdrh ** In the case of sqlite3changeset_start_strm(), the xInput callback may be
118016228167Sdan ** invoked by the sessions module at any point during the lifetime of the
118116228167Sdan ** iterator. If such an xInput callback returns an error, the iterator enters
118216228167Sdan ** an error state, whereby all subsequent calls to iterator functions
118316228167Sdan ** immediately fail with the same error code as returned by xInput.
118416228167Sdan **
118516228167Sdan ** Similarly, streaming API functions that return changesets (or patchsets)
118616228167Sdan ** return them in chunks by way of a callback function instead of via a
118716228167Sdan ** pointer to a single large buffer. In this case, a pair of parameters such
118816228167Sdan ** as:
118916228167Sdan **
119016228167Sdan **  <pre>
119116228167Sdan **  &nbsp;     int *pnChangeset,
119216228167Sdan **  &nbsp;     void **ppChangeset,
119316228167Sdan **  </pre>
119416228167Sdan **
119516228167Sdan ** Is replaced by:
119616228167Sdan **
119716228167Sdan **  <pre>
119816228167Sdan **  &nbsp;     int (*xOutput)(void *pOut, const void *pData, int nData),
119916228167Sdan **  &nbsp;     void *pOut
120016228167Sdan **  </pre>
120116228167Sdan **
120216228167Sdan ** The xOutput callback is invoked zero or more times to return data to
120316228167Sdan ** the application. The first parameter passed to each call is a copy of the
120416228167Sdan ** pOut pointer supplied by the application. The second parameter, pData,
120516228167Sdan ** points to a buffer nData bytes in size containing the chunk of output
120616228167Sdan ** data being returned. If the xOutput callback successfully processes the
120716228167Sdan ** supplied data, it should return SQLITE_OK to indicate success. Otherwise,
120816228167Sdan ** it should return some other SQLite error code. In this case processing
120916228167Sdan ** is immediately abandoned and the streaming API function returns a copy
121016228167Sdan ** of the xOutput error code to the application.
121116228167Sdan **
121216228167Sdan ** The sessions module never invokes an xOutput callback with the third
121316228167Sdan ** parameter set to a value less than or equal to zero. Other than this,
121416228167Sdan ** no guarantees are made as to the size of the chunks of data returned.
121516228167Sdan */
1216f1a08ad8Sdrh int sqlite3changeset_apply_strm(
121716228167Sdan   sqlite3 *db,                    /* Apply change to "main" db of this handle */
121816228167Sdan   int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
121916228167Sdan   void *pIn,                                          /* First arg for xInput */
122016228167Sdan   int(*xFilter)(
122116228167Sdan     void *pCtx,                   /* Copy of sixth arg to _apply() */
122216228167Sdan     const char *zTab              /* Table name */
122316228167Sdan   ),
122416228167Sdan   int(*xConflict)(
122516228167Sdan     void *pCtx,                   /* Copy of sixth arg to _apply() */
122616228167Sdan     int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
122716228167Sdan     sqlite3_changeset_iter *p     /* Handle describing change and conflict */
122816228167Sdan   ),
122916228167Sdan   void *pCtx                      /* First argument passed to xConflict */
123016228167Sdan );
1231f1a08ad8Sdrh int sqlite3changeset_concat_strm(
123216228167Sdan   int (*xInputA)(void *pIn, void *pData, int *pnData),
123316228167Sdan   void *pInA,
123416228167Sdan   int (*xInputB)(void *pIn, void *pData, int *pnData),
123516228167Sdan   void *pInB,
123616228167Sdan   int (*xOutput)(void *pOut, const void *pData, int nData),
123716228167Sdan   void *pOut
123816228167Sdan );
1239f1a08ad8Sdrh int sqlite3changeset_invert_strm(
124016228167Sdan   int (*xInput)(void *pIn, void *pData, int *pnData),
124116228167Sdan   void *pIn,
124216228167Sdan   int (*xOutput)(void *pOut, const void *pData, int nData),
124316228167Sdan   void *pOut
124416228167Sdan );
1245f1a08ad8Sdrh int sqlite3changeset_start_strm(
124616228167Sdan   sqlite3_changeset_iter **pp,
124716228167Sdan   int (*xInput)(void *pIn, void *pData, int *pnData),
124816228167Sdan   void *pIn
124916228167Sdan );
1250f1a08ad8Sdrh int sqlite3session_changeset_strm(
125116228167Sdan   sqlite3_session *pSession,
125216228167Sdan   int (*xOutput)(void *pOut, const void *pData, int nData),
125316228167Sdan   void *pOut
125416228167Sdan );
1255f1a08ad8Sdrh int sqlite3session_patchset_strm(
125616228167Sdan   sqlite3_session *pSession,
125716228167Sdan   int (*xOutput)(void *pOut, const void *pData, int nData),
125816228167Sdan   void *pOut
125916228167Sdan );
12605898ad69Sdan int sqlite3changegroup_add_strm(sqlite3_changegroup*,
12615898ad69Sdan     int (*xInput)(void *pIn, void *pData, int *pnData),
12625898ad69Sdan     void *pIn
12635898ad69Sdan );
12645898ad69Sdan int sqlite3changegroup_output_strm(sqlite3_changegroup*,
12655898ad69Sdan     int (*xOutput)(void *pOut, const void *pData, int nData),
12665898ad69Sdan     void *pOut
12675898ad69Sdan );
126816228167Sdan 
126916228167Sdan 
127016228167Sdan /*
127136828bd9Sdrh ** Make sure we can call this stuff from C++.
127236828bd9Sdrh */
127336828bd9Sdrh #ifdef __cplusplus
127436828bd9Sdrh }
12754fccf43aSdan #endif
12764fccf43aSdan 
1277*4e80d5fcSdrh #endif  /* !defined(__SQLITESESSION_H_) && defined(SQLITE_ENABLE_SESSION) */
1278