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 
826d29a4feSdan /*
836d29a4feSdan ** CAPIREF: Conigure a Session Object
846d29a4feSdan ** METHOD: sqlite3_session
85743b5fd5Sdan **
86743b5fd5Sdan ** This method is used to configure a session object after it has been
87743b5fd5Sdan ** created. At present the only valid value for the second parameter is
88743b5fd5Sdan ** [SQLITE_SESSION_OBJCONFIG_SIZE].
89*5f5719bdSlarrybr **
90*5f5719bdSlarrybr ** Arguments for sqlite3session_object_config()
916d29a4feSdan **
926d29a4feSdan ** The following values may passed as the the 4th parameter to
93*5f5719bdSlarrybr ** sqlite3session_object_config().
946d29a4feSdan **
956d29a4feSdan ** <dt>SQLITE_SESSION_OBJCONFIG_SIZE <dd>
966d29a4feSdan **   This option is used to set, clear or query the flag that enables
976d29a4feSdan **   the [sqlite3session_changeset_size()] API. Because it imposes some
986d29a4feSdan **   computational overhead, this API is disabled by default. Argument
996d29a4feSdan **   pArg must point to a value of type (int). If the value is initially
1006d29a4feSdan **   0, then the sqlite3session_changeset_size() API is disabled. If it
1016d29a4feSdan **   is greater than 0, then the same API is enabled. Or, if the initial
1026d29a4feSdan **   value is less than zero, no change is made. In all cases the (int)
1036d29a4feSdan **   variable is set to 1 if the sqlite3session_changeset_size() API is
1046d29a4feSdan **   enabled following the current call, or 0 otherwise.
1056d29a4feSdan **
1066d29a4feSdan **   It is an error (SQLITE_MISUSE) to attempt to modify this setting after
1076d29a4feSdan **   the first table has been attached to the session object.
1086d29a4feSdan */
109*5f5719bdSlarrybr int sqlite3session_object_config(sqlite3_session*, int op, void *pArg);
110*5f5719bdSlarrybr 
111*5f5719bdSlarrybr /*
112*5f5719bdSlarrybr */
1136d29a4feSdan #define SQLITE_SESSION_OBJCONFIG_SIZE 1
1147531a5a3Sdan 
11537db03bfSdan /*
116a2df3d9fSdan ** CAPI3REF: Enable Or Disable A Session Object
117bda30ce4Sdrh ** METHOD: sqlite3_session
118a2df3d9fSdan **
1194fccf43aSdan ** Enable or disable the recording of changes by a session object. When
1204fccf43aSdan ** enabled, a session object records changes made to the database. When
1214fccf43aSdan ** disabled - it does not. A newly created session object is enabled.
122c21111daSdan ** Refer to the documentation for [sqlite3session_changeset()] for further
123c21111daSdan ** details regarding how enabling and disabling a session object affects
124c21111daSdan ** the eventual changesets.
1254fccf43aSdan **
1264fccf43aSdan ** Passing zero to this function disables the session. Passing a value
1274fccf43aSdan ** greater than zero enables it. Passing a value less than zero is a
1284fccf43aSdan ** no-op, and may be used to query the current state of the session.
1294fccf43aSdan **
1304fccf43aSdan ** The return value indicates the final state of the session object: 0 if
1314fccf43aSdan ** the session is disabled, or 1 if it is enabled.
1324fccf43aSdan */
1334fccf43aSdan int sqlite3session_enable(sqlite3_session *pSession, int bEnable);
1344fccf43aSdan 
1354fccf43aSdan /*
136b4480e94Sdan ** CAPI3REF: Set Or Clear the Indirect Change Flag
137bda30ce4Sdrh ** METHOD: sqlite3_session
138b4480e94Sdan **
139b4480e94Sdan ** Each change recorded by a session object is marked as either direct or
140b4480e94Sdan ** indirect. A change is marked as indirect if either:
141b4480e94Sdan **
142b4480e94Sdan ** <ul>
143b4480e94Sdan **   <li> The session object "indirect" flag is set when the change is
144b4480e94Sdan **        made, or
145b4480e94Sdan **   <li> The change is made by an SQL trigger or foreign key action
146b4480e94Sdan **        instead of directly as a result of a users SQL statement.
147b4480e94Sdan ** </ul>
148b4480e94Sdan **
149b4480e94Sdan ** If a single row is affected by more than one operation within a session,
150b4480e94Sdan ** then the change is considered indirect if all operations meet the criteria
151b4480e94Sdan ** for an indirect change above, or direct otherwise.
152b4480e94Sdan **
153b4480e94Sdan ** This function is used to set, clear or query the session object indirect
154b4480e94Sdan ** flag.  If the second argument passed to this function is zero, then the
155b4480e94Sdan ** indirect flag is cleared. If it is greater than zero, the indirect flag
156b4480e94Sdan ** is set. Passing a value less than zero does not modify the current value
157b4480e94Sdan ** of the indirect flag, and may be used to query the current state of the
158b4480e94Sdan ** indirect flag for the specified session object.
159b4480e94Sdan **
160b4480e94Sdan ** The return value indicates the final state of the indirect flag: 0 if
161b4480e94Sdan ** it is clear, or 1 if it is set.
162b4480e94Sdan */
163b4480e94Sdan int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect);
164b4480e94Sdan 
165b4480e94Sdan /*
166157546f4Sshaneh ** CAPI3REF: Attach A Table To A Session Object
167bda30ce4Sdrh ** METHOD: sqlite3_session
168a2df3d9fSdan **
169ff4d0f41Sdan ** If argument zTab is not NULL, then it is the name of a table to attach
170ff4d0f41Sdan ** to the session object passed as the first argument. All subsequent changes
171ff4d0f41Sdan ** made to the table while the session object is enabled will be recorded. See
172ff4d0f41Sdan ** documentation for [sqlite3session_changeset()] for further details.
173ff4d0f41Sdan **
174ff4d0f41Sdan ** Or, if argument zTab is NULL, then changes are recorded for all tables
175ff4d0f41Sdan ** in the database. If additional tables are added to the database (by
176ff4d0f41Sdan ** executing "CREATE TABLE" statements) after this call is made, changes for
177ff4d0f41Sdan ** the new tables are also recorded.
1784fccf43aSdan **
179c21111daSdan ** Changes can only be recorded for tables that have a PRIMARY KEY explicitly
180c21111daSdan ** defined as part of their CREATE TABLE statement. It does not matter if the
181c21111daSdan ** PRIMARY KEY is an "INTEGER PRIMARY KEY" (rowid alias) or not. The PRIMARY
182c21111daSdan ** KEY may consist of a single column, or may be a composite key.
183c21111daSdan **
184c21111daSdan ** It is not an error if the named table does not exist in the database. Nor
185c21111daSdan ** is it an error if the named table does not have a PRIMARY KEY. However,
186c21111daSdan ** no changes will be recorded in either of these scenarios.
187c21111daSdan **
18827453faeSdan ** Changes are not recorded for individual rows that have NULL values stored
18927453faeSdan ** in one or more of their PRIMARY KEY columns.
19027453faeSdan **
191ff4d0f41Sdan ** SQLITE_OK is returned if the call completes without error. Or, if an error
192ff4d0f41Sdan ** occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned.
193e3ca3831Sdan **
194e3ca3831Sdan ** <h3>Special sqlite_stat1 Handling</h3>
195e3ca3831Sdan **
196cae5b9feSdan ** As of SQLite version 3.22.0, the "sqlite_stat1" table is an exception to
197cae5b9feSdan ** some of the rules above. In SQLite, the schema of sqlite_stat1 is:
198e3ca3831Sdan **  <pre>
199e3ca3831Sdan **  &nbsp;     CREATE TABLE sqlite_stat1(tbl,idx,stat)
200e3ca3831Sdan **  </pre>
201e3ca3831Sdan **
202e3ca3831Sdan ** Even though sqlite_stat1 does not have a PRIMARY KEY, changes are
203e3ca3831Sdan ** recorded for it as if the PRIMARY KEY is (tbl,idx). Additionally, changes
204e3ca3831Sdan ** are recorded for rows for which (idx IS NULL) is true. However, for such
205e3ca3831Sdan ** rows a zero-length blob (SQL value X'') is stored in the changeset or
206e3ca3831Sdan ** patchset instead of a NULL value. This allows such changesets to be
207e3ca3831Sdan ** manipulated by legacy implementations of sqlite3changeset_invert(),
208e3ca3831Sdan ** concat() and similar.
209e3ca3831Sdan **
210e3ca3831Sdan ** The sqlite3changeset_apply() function automatically converts the
211e3ca3831Sdan ** zero-length blob back to a NULL value when updating the sqlite_stat1
212e3ca3831Sdan ** table. However, if the application calls sqlite3changeset_new(),
213e3ca3831Sdan ** sqlite3changeset_old() or sqlite3changeset_conflict on a changeset
214e3ca3831Sdan ** iterator directly (including on a changeset iterator passed to a
215e3ca3831Sdan ** conflict-handler callback) then the X'' value is returned. The application
216e3ca3831Sdan ** must translate X'' to NULL itself if required.
217cae5b9feSdan **
218cae5b9feSdan ** Legacy (older than 3.22.0) versions of the sessions module cannot capture
219cae5b9feSdan ** changes made to the sqlite_stat1 table. Legacy versions of the
220cae5b9feSdan ** sqlite3changeset_apply() function silently ignore any modifications to the
221cae5b9feSdan ** sqlite_stat1 table that are part of a changeset or patchset.
2224fccf43aSdan */
2234fccf43aSdan int sqlite3session_attach(
2244fccf43aSdan   sqlite3_session *pSession,      /* Session object */
2254fccf43aSdan   const char *zTab                /* Table name */
2264fccf43aSdan );
2274fccf43aSdan 
2284fccf43aSdan /*
2297531a5a3Sdan ** CAPI3REF: Set a table filter on a Session Object.
230bda30ce4Sdrh ** METHOD: sqlite3_session
2317531a5a3Sdan **
2327531a5a3Sdan ** The second argument (xFilter) is the "filter callback". For changes to rows
233ed204d1fSdrh ** in tables that are not attached to the Session object, the filter is called
2347531a5a3Sdan ** to determine whether changes to the table's rows should be tracked or not.
2352bbcaee8Sdrh ** If xFilter returns 0, changes are not tracked. Note that once a table is
2367531a5a3Sdan ** attached, xFilter will not be called again.
2377531a5a3Sdan */
2387531a5a3Sdan void sqlite3session_table_filter(
2397531a5a3Sdan   sqlite3_session *pSession,      /* Session object */
2407531a5a3Sdan   int(*xFilter)(
2417531a5a3Sdan     void *pCtx,                   /* Copy of third arg to _filter_table() */
2427531a5a3Sdan     const char *zTab              /* Table name */
2437531a5a3Sdan   ),
2447531a5a3Sdan   void *pCtx                      /* First argument passed to xFilter */
2457531a5a3Sdan );
2467531a5a3Sdan 
2477531a5a3Sdan /*
248a2df3d9fSdan ** CAPI3REF: Generate A Changeset From A Session Object
249bda30ce4Sdrh ** METHOD: sqlite3_session
250a2df3d9fSdan **
25137db03bfSdan ** Obtain a changeset containing changes to the tables attached to the
25237db03bfSdan ** session object passed as the first argument. If successful,
25337db03bfSdan ** set *ppChangeset to point to a buffer containing the changeset
25437db03bfSdan ** and *pnChangeset to the size of the changeset in bytes before returning
25537db03bfSdan ** SQLITE_OK. If an error occurs, set both *ppChangeset and *pnChangeset to
25637db03bfSdan ** zero and return an SQLite error code.
2574fccf43aSdan **
258c21111daSdan ** A changeset consists of zero or more INSERT, UPDATE and/or DELETE changes,
259c21111daSdan ** each representing a change to a single row of an attached table. An INSERT
260c21111daSdan ** change contains the values of each field of a new database row. A DELETE
261c21111daSdan ** contains the original values of each field of a deleted database row. An
262c21111daSdan ** UPDATE change contains the original values of each field of an updated
263c21111daSdan ** database row along with the updated values for each updated non-primary-key
264c21111daSdan ** column. It is not possible for an UPDATE change to represent a change that
265c21111daSdan ** modifies the values of primary key columns. If such a change is made, it
266c21111daSdan ** is represented in a changeset as a DELETE followed by an INSERT.
267c21111daSdan **
26827453faeSdan ** Changes are not recorded for rows that have NULL values stored in one or
26927453faeSdan ** more of their PRIMARY KEY columns. If such a row is inserted or deleted,
27027453faeSdan ** no corresponding change is present in the changesets returned by this
27127453faeSdan ** function. If an existing row with one or more NULL values stored in
27227453faeSdan ** PRIMARY KEY columns is updated so that all PRIMARY KEY columns are non-NULL,
27327453faeSdan ** only an INSERT is appears in the changeset. Similarly, if an existing row
27427453faeSdan ** with non-NULL PRIMARY KEY values is updated so that one or more of its
27527453faeSdan ** PRIMARY KEY columns are set to NULL, the resulting changeset contains a
27627453faeSdan ** DELETE change only.
27727453faeSdan **
278c21111daSdan ** The contents of a changeset may be traversed using an iterator created
279c21111daSdan ** using the [sqlite3changeset_start()] API. A changeset may be applied to
280a2df3d9fSdan ** a database with a compatible schema using the [sqlite3changeset_apply()]
281c21111daSdan ** API.
282c21111daSdan **
2836c39e6a8Sdan ** Within a changeset generated by this function, all changes related to a
2846c39e6a8Sdan ** single table are grouped together. In other words, when iterating through
2856c39e6a8Sdan ** a changeset or when applying a changeset to a database, all changes related
2866c39e6a8Sdan ** to a single table are processed before moving on to the next table. Tables
2876c39e6a8Sdan ** are sorted in the same order in which they were attached (or auto-attached)
2886c39e6a8Sdan ** to the sqlite3_session object. The order in which the changes related to
2896c39e6a8Sdan ** a single table are stored is undefined.
2906c39e6a8Sdan **
29137db03bfSdan ** Following a successful call to this function, it is the responsibility of
29237db03bfSdan ** the caller to eventually free the buffer that *ppChangeset points to using
29337db03bfSdan ** [sqlite3_free()].
294c21111daSdan **
295a2df3d9fSdan ** <h3>Changeset Generation</h3>
296c21111daSdan **
297c21111daSdan ** Once a table has been attached to a session object, the session object
298c21111daSdan ** records the primary key values of all new rows inserted into the table.
299c21111daSdan ** It also records the original primary key and other column values of any
300c21111daSdan ** deleted or updated rows. For each unique primary key value, data is only
301c21111daSdan ** recorded once - the first time a row with said primary key is inserted,
302c21111daSdan ** updated or deleted in the lifetime of the session.
303c21111daSdan **
30427453faeSdan ** There is one exception to the previous paragraph: when a row is inserted,
305157546f4Sshaneh ** updated or deleted, if one or more of its primary key columns contain a
30627453faeSdan ** NULL value, no record of the change is made.
30727453faeSdan **
308c21111daSdan ** The session object therefore accumulates two types of records - those
309c21111daSdan ** that consist of primary key values only (created when the user inserts
310c21111daSdan ** a new record) and those that consist of the primary key values and the
311c21111daSdan ** original values of other table columns (created when the users deletes
312c21111daSdan ** or updates a record).
313c21111daSdan **
314c21111daSdan ** When this function is called, the requested changeset is created using
315c21111daSdan ** both the accumulated records and the current contents of the database
316c21111daSdan ** file. Specifically:
317c21111daSdan **
318c21111daSdan ** <ul>
319c21111daSdan **   <li> For each record generated by an insert, the database is queried
320c21111daSdan **        for a row with a matching primary key. If one is found, an INSERT
321c21111daSdan **        change is added to the changeset. If no such row is found, no change
322c21111daSdan **        is added to the changeset.
323c21111daSdan **
324c21111daSdan **   <li> For each record generated by an update or delete, the database is
325c21111daSdan **        queried for a row with a matching primary key. If such a row is
326c21111daSdan **        found and one or more of the non-primary key fields have been
327c21111daSdan **        modified from their original values, an UPDATE change is added to
328c21111daSdan **        the changeset. Or, if no such row is found in the table, a DELETE
329c21111daSdan **        change is added to the changeset. If there is a row with a matching
330c21111daSdan **        primary key in the database, but all fields contain their original
331c21111daSdan **        values, no change is added to the changeset.
332c21111daSdan ** </ul>
333c21111daSdan **
334c21111daSdan ** This means, amongst other things, that if a row is inserted and then later
335157546f4Sshaneh ** deleted while a session object is active, neither the insert nor the delete
336c21111daSdan ** will be present in the changeset. Or if a row is deleted and then later a
337c21111daSdan ** row with the same primary key values inserted while a session object is
338c21111daSdan ** active, the resulting changeset will contain an UPDATE change instead of
339c21111daSdan ** a DELETE and an INSERT.
340c21111daSdan **
341c21111daSdan ** When a session object is disabled (see the [sqlite3session_enable()] API),
342c21111daSdan ** it does not accumulate records when rows are inserted, updated or deleted.
343c21111daSdan ** This may appear to have some counter-intuitive effects if a single row
344c21111daSdan ** is written to more than once during a session. For example, if a row
345c21111daSdan ** is inserted while a session object is enabled, then later deleted while
346c21111daSdan ** the same session object is disabled, no INSERT record will appear in the
347c21111daSdan ** changeset, even though the delete took place while the session was disabled.
348c21111daSdan ** Or, if one field of a row is updated while a session is disabled, and
349c21111daSdan ** another field of the same row is updated while the session is enabled, the
350c21111daSdan ** resulting changeset will contain an UPDATE change that updates both fields.
3514fccf43aSdan */
3524fccf43aSdan int sqlite3session_changeset(
3534fccf43aSdan   sqlite3_session *pSession,      /* Session object */
3544fccf43aSdan   int *pnChangeset,               /* OUT: Size of buffer at *ppChangeset */
3554fccf43aSdan   void **ppChangeset              /* OUT: Buffer containing changeset */
3564fccf43aSdan );
3574fccf43aSdan 
358cf8e9144Sdan /*
359743b5fd5Sdan ** CAPI3REF: Return An Upper-limit For The Size Of The Changeset
360*5f5719bdSlarrybr ** METHOD: sqlite3_session
361a23a873fSdan **
3629ad39a33Sdan ** By default, this function always returns 0. For it to return
3639ad39a33Sdan ** a useful result, the sqlite3_session object must have been configured
364*5f5719bdSlarrybr ** to enable this API using sqlite3session_object_config() with the
3659ad39a33Sdan ** SQLITE_SESSION_OBJCONFIG_SIZE verb.
3669ad39a33Sdan **
3679ad39a33Sdan ** When enabled, this function returns an upper limit, in bytes, for the size
3689ad39a33Sdan ** of the changeset that might be produced if sqlite3session_changeset() were
3699ad39a33Sdan ** called. The final changeset size might be equal to or smaller than the
3709ad39a33Sdan ** size in bytes returned by this function.
371a23a873fSdan */
372a23a873fSdan sqlite3_int64 sqlite3session_changeset_size(sqlite3_session *pSession);
373a23a873fSdan 
374a23a873fSdan /*
375a23a873fSdan ** CAPI3REF: Load The Difference Between Tables Into A Session
376bda30ce4Sdrh ** METHOD: sqlite3_session
377cf8e9144Sdan **
378cf8e9144Sdan ** If it is not already attached to the session object passed as the first
379cf8e9144Sdan ** argument, this function attaches table zTbl in the same manner as the
380cf8e9144Sdan ** [sqlite3session_attach()] function. If zTbl does not exist, or if it
381cf8e9144Sdan ** does not have a primary key, this function is a no-op (but does not return
382cf8e9144Sdan ** an error).
383cf8e9144Sdan **
384cf8e9144Sdan ** Argument zFromDb must be the name of a database ("main", "temp" etc.)
385cf8e9144Sdan ** attached to the same database handle as the session object that contains
386cf8e9144Sdan ** a table compatible with the table attached to the session by this function.
387cf8e9144Sdan ** A table is considered compatible if it:
388cf8e9144Sdan **
389cf8e9144Sdan ** <ul>
390cf8e9144Sdan **   <li> Has the same name,
391cf8e9144Sdan **   <li> Has the same set of columns declared in the same order, and
392cf8e9144Sdan **   <li> Has the same PRIMARY KEY definition.
393cf8e9144Sdan ** </ul>
394cf8e9144Sdan **
395b9db9099Sdan ** If the tables are not compatible, SQLITE_SCHEMA is returned. If the tables
396b9db9099Sdan ** are compatible but do not have any PRIMARY KEY columns, it is not an error
397b9db9099Sdan ** but no changes are added to the session object. As with other session
398b9db9099Sdan ** APIs, tables without PRIMARY KEYs are simply ignored.
399b9db9099Sdan **
400cf8e9144Sdan ** This function adds a set of changes to the session object that could be
401cf8e9144Sdan ** used to update the table in database zFrom (call this the "from-table")
402cf8e9144Sdan ** so that its content is the same as the table attached to the session
403cf8e9144Sdan ** object (call this the "to-table"). Specifically:
404cf8e9144Sdan **
405cf8e9144Sdan ** <ul>
406cf8e9144Sdan **   <li> For each row (primary key) that exists in the to-table but not in
407cf8e9144Sdan **     the from-table, an INSERT record is added to the session object.
408cf8e9144Sdan **
409cf8e9144Sdan **   <li> For each row (primary key) that exists in the to-table but not in
410cf8e9144Sdan **     the from-table, a DELETE record is added to the session object.
411cf8e9144Sdan **
412cf8e9144Sdan **   <li> For each row (primary key) that exists in both tables, but features
413ff677b20Sdan **     different non-PK values in each, an UPDATE record is added to the
414ff677b20Sdan **     session.
415cf8e9144Sdan ** </ul>
416cf8e9144Sdan **
417cf8e9144Sdan ** To clarify, if this function is called and then a changeset constructed
418cf8e9144Sdan ** using [sqlite3session_changeset()], then after applying that changeset to
419cf8e9144Sdan ** database zFrom the contents of the two compatible tables would be
420cf8e9144Sdan ** identical.
421cf8e9144Sdan **
422cf8e9144Sdan ** It an error if database zFrom does not exist or does not contain the
423cf8e9144Sdan ** required compatible table.
424cf8e9144Sdan **
4252bbcaee8Sdrh ** If the operation is successful, SQLITE_OK is returned. Otherwise, an SQLite
426cf8e9144Sdan ** error code. In this case, if argument pzErrMsg is not NULL, *pzErrMsg
427cf8e9144Sdan ** may be set to point to a buffer containing an English language error
428cf8e9144Sdan ** message. It is the responsibility of the caller to free this buffer using
429cf8e9144Sdan ** sqlite3_free().
430cf8e9144Sdan */
431cf8e9144Sdan int sqlite3session_diff(
432cf8e9144Sdan   sqlite3_session *pSession,
433cf8e9144Sdan   const char *zFromDb,
434cf8e9144Sdan   const char *zTbl,
435cf8e9144Sdan   char **pzErrMsg
436cf8e9144Sdan );
437cf8e9144Sdan 
438ef7a6304Sdan 
439ef7a6304Sdan /*
44073b3c055Sdan ** CAPI3REF: Generate A Patchset From A Session Object
441bda30ce4Sdrh ** METHOD: sqlite3_session
442a71d2371Sdan **
443a71d2371Sdan ** The differences between a patchset and a changeset are that:
444a71d2371Sdan **
445a71d2371Sdan ** <ul>
446a71d2371Sdan **   <li> DELETE records consist of the primary key fields only. The
447a71d2371Sdan **        original values of other fields are omitted.
448a71d2371Sdan **   <li> The original values of any modified fields are omitted from
449a71d2371Sdan **        UPDATE records.
450a71d2371Sdan ** </ul>
451a71d2371Sdan **
452a71d2371Sdan ** A patchset blob may be used with up to date versions of all
453a71d2371Sdan ** sqlite3changeset_xxx API functions except for sqlite3changeset_invert(),
454a71d2371Sdan ** which returns SQLITE_CORRUPT if it is passed a patchset. Similarly,
455a71d2371Sdan ** attempting to use a patchset blob with old versions of the
456a71d2371Sdan ** sqlite3changeset_xxx APIs also provokes an SQLITE_CORRUPT error.
457a71d2371Sdan **
458a71d2371Sdan ** Because the non-primary key "old.*" fields are omitted, no
459a71d2371Sdan ** SQLITE_CHANGESET_DATA conflicts can be detected or reported if a patchset
460a71d2371Sdan ** is passed to the sqlite3changeset_apply() API. Other conflict types work
461a71d2371Sdan ** in the same way as for changesets.
4626c39e6a8Sdan **
4636c39e6a8Sdan ** Changes within a patchset are ordered in the same way as for changesets
4646c39e6a8Sdan ** generated by the sqlite3session_changeset() function (i.e. all changes for
4656c39e6a8Sdan ** a single table are grouped together, tables appear in the order in which
4666c39e6a8Sdan ** they were attached to the session object).
46773b3c055Sdan */
46873b3c055Sdan int sqlite3session_patchset(
46973b3c055Sdan   sqlite3_session *pSession,      /* Session object */
4703dfbe9b3Smistachkin   int *pnPatchset,                /* OUT: Size of buffer at *ppPatchset */
4713dfbe9b3Smistachkin   void **ppPatchset               /* OUT: Buffer containing patchset */
47273b3c055Sdan );
47373b3c055Sdan 
47473b3c055Sdan /*
475b69ec348Sdan ** CAPI3REF: Test if a changeset has recorded any changes.
476b69ec348Sdan **
477b69ec348Sdan ** Return non-zero if no changes to attached tables have been recorded by
478b69ec348Sdan ** the session object passed as the first argument. Otherwise, if one or
479b69ec348Sdan ** more changes have been recorded, return zero.
480b69ec348Sdan **
481b69ec348Sdan ** Even if this function returns zero, it is possible that calling
482b69ec348Sdan ** [sqlite3session_changeset()] on the session handle may still return a
483b69ec348Sdan ** changeset that contains no changes. This can happen when a row in
484b69ec348Sdan ** an attached table is modified and then later on the original values
485b69ec348Sdan ** are restored. However, if this function returns non-zero, then it is
486b69ec348Sdan ** guaranteed that a call to sqlite3session_changeset() will return a
487b69ec348Sdan ** changeset containing zero changes.
488b69ec348Sdan */
489b69ec348Sdan int sqlite3session_isempty(sqlite3_session *pSession);
490b69ec348Sdan 
491b69ec348Sdan /*
4920cb735b9Sdan ** CAPI3REF: Query for the amount of heap memory used by a session object.
4930cb735b9Sdan **
4940cb735b9Sdan ** This API returns the total amount of heap memory in bytes currently
4950cb735b9Sdan ** used by the session object passed as the only argument.
4960cb735b9Sdan */
4970cb735b9Sdan sqlite3_int64 sqlite3session_memory_used(sqlite3_session *pSession);
4980cb735b9Sdan 
4990cb735b9Sdan /*
500a2df3d9fSdan ** CAPI3REF: Create An Iterator To Traverse A Changeset
501bda30ce4Sdrh ** CONSTRUCTOR: sqlite3_changeset_iter
502a2df3d9fSdan **
5034fccf43aSdan ** Create an iterator used to iterate through the contents of a changeset.
504c21111daSdan ** If successful, *pp is set to point to the iterator handle and SQLITE_OK
505c21111daSdan ** is returned. Otherwise, if an error occurs, *pp is set to zero and an
506c21111daSdan ** SQLite error code is returned.
507c21111daSdan **
508c21111daSdan ** The following functions can be used to advance and query a changeset
509c21111daSdan ** iterator created by this function:
510c21111daSdan **
511c21111daSdan ** <ul>
512c21111daSdan **   <li> [sqlite3changeset_next()]
513c21111daSdan **   <li> [sqlite3changeset_op()]
514c21111daSdan **   <li> [sqlite3changeset_new()]
515c21111daSdan **   <li> [sqlite3changeset_old()]
516c21111daSdan ** </ul>
517c21111daSdan **
518c21111daSdan ** It is the responsibility of the caller to eventually destroy the iterator
519c21111daSdan ** by passing it to [sqlite3changeset_finalize()]. The buffer containing the
520c21111daSdan ** changeset (pChangeset) must remain valid until after the iterator is
521c21111daSdan ** destroyed.
522b69ec348Sdan **
523b69ec348Sdan ** Assuming the changeset blob was created by one of the
524b69ec348Sdan ** [sqlite3session_changeset()], [sqlite3changeset_concat()] or
525a93166e0Sdan ** [sqlite3changeset_invert()] functions, all changes within the changeset
526a93166e0Sdan ** that apply to a single table are grouped together. This means that when
527a93166e0Sdan ** an application iterates through a changeset using an iterator created by
5285e769a50Sdrh ** this function, all changes that relate to a single table are visited
529a93166e0Sdan ** consecutively. There is no chance that the iterator will visit a change
530a93166e0Sdan ** the applies to table X, then one for table Y, and then later on visit
531a93166e0Sdan ** another change for table X.
53246de0728Sdan **
53346de0728Sdan ** The behavior of sqlite3changeset_start_v2() and its streaming equivalent
53446de0728Sdan ** may be modified by passing a combination of
53546de0728Sdan ** [SQLITE_CHANGESETSTART_INVERT | supported flags] as the 4th parameter.
53646de0728Sdan **
53746de0728Sdan ** Note that the sqlite3changeset_start_v2() API is still <b>experimental</b>
53846de0728Sdan ** and therefore subject to change.
5394fccf43aSdan */
5404fccf43aSdan int sqlite3changeset_start(
541c21111daSdan   sqlite3_changeset_iter **pp,    /* OUT: New changeset iterator handle */
542c21111daSdan   int nChangeset,                 /* Size of changeset blob in bytes */
543c21111daSdan   void *pChangeset                /* Pointer to blob containing changeset */
5444fccf43aSdan );
54546de0728Sdan int sqlite3changeset_start_v2(
54646de0728Sdan   sqlite3_changeset_iter **pp,    /* OUT: New changeset iterator handle */
54746de0728Sdan   int nChangeset,                 /* Size of changeset blob in bytes */
54846de0728Sdan   void *pChangeset,               /* Pointer to blob containing changeset */
54946de0728Sdan   int flags                       /* SESSION_CHANGESETSTART_* flags */
55046de0728Sdan );
55146de0728Sdan 
55246de0728Sdan /*
55346de0728Sdan ** CAPI3REF: Flags for sqlite3changeset_start_v2
55446de0728Sdan **
55546de0728Sdan ** The following flags may passed via the 4th parameter to
55646de0728Sdan ** [sqlite3changeset_start_v2] and [sqlite3changeset_start_v2_strm]:
55746de0728Sdan **
55846de0728Sdan ** <dt>SQLITE_CHANGESETAPPLY_INVERT <dd>
55946de0728Sdan **   Invert the changeset while iterating through it. This is equivalent to
56046de0728Sdan **   inverting a changeset using sqlite3changeset_invert() before applying it.
56146de0728Sdan **   It is an error to specify this flag with a patchset.
56246de0728Sdan */
56346de0728Sdan #define SQLITE_CHANGESETSTART_INVERT        0x0002
5644fccf43aSdan 
565ef7a6304Sdan 
566ef7a6304Sdan /*
567a2df3d9fSdan ** CAPI3REF: Advance A Changeset Iterator
568bda30ce4Sdrh ** METHOD: sqlite3_changeset_iter
569a2df3d9fSdan **
5702bbcaee8Sdrh ** This function may only be used with iterators created by the function
571c21111daSdan ** [sqlite3changeset_start()]. If it is called on an iterator passed to
572c21111daSdan ** a conflict-handler callback by [sqlite3changeset_apply()], SQLITE_MISUSE
573c21111daSdan ** is returned and the call has no effect.
5744fccf43aSdan **
575c21111daSdan ** Immediately after an iterator is created by sqlite3changeset_start(), it
576c21111daSdan ** does not point to any change in the changeset. Assuming the changeset
577c21111daSdan ** is not empty, the first call to this function advances the iterator to
578c21111daSdan ** point to the first change in the changeset. Each subsequent call advances
579c21111daSdan ** the iterator to point to the next change in the changeset (if any). If
580c21111daSdan ** no error occurs and the iterator points to a valid change after a call
581c21111daSdan ** to sqlite3changeset_next() has advanced it, SQLITE_ROW is returned.
582c21111daSdan ** Otherwise, if all changes in the changeset have already been visited,
583c21111daSdan ** SQLITE_DONE is returned.
584c21111daSdan **
585c21111daSdan ** If an error occurs, an SQLite error code is returned. Possible error
586c21111daSdan ** codes include SQLITE_CORRUPT (if the changeset buffer is corrupt) or
587c21111daSdan ** SQLITE_NOMEM.
5884fccf43aSdan */
5894fccf43aSdan int sqlite3changeset_next(sqlite3_changeset_iter *pIter);
5904fccf43aSdan 
5914fccf43aSdan /*
592a2df3d9fSdan ** CAPI3REF: Obtain The Current Operation From A Changeset Iterator
593bda30ce4Sdrh ** METHOD: sqlite3_changeset_iter
594a2df3d9fSdan **
595c21111daSdan ** The pIter argument passed to this function may either be an iterator
596c21111daSdan ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
597c21111daSdan ** created by [sqlite3changeset_start()]. In the latter case, the most recent
59877e65004Sdan ** call to [sqlite3changeset_next()] must have returned [SQLITE_ROW]. If this
59977e65004Sdan ** is not the case, this function returns [SQLITE_MISUSE].
600c21111daSdan **
601ab6098cfSlarrybr ** Arguments pOp, pnCol and pzTab may not be NULL. Upon return, three
602ab6098cfSlarrybr ** outputs are set through these pointers:
603ab6098cfSlarrybr **
604ab6098cfSlarrybr ** *pOp is set to one of [SQLITE_INSERT], [SQLITE_DELETE] or [SQLITE_UPDATE],
605ab6098cfSlarrybr ** depending on the type of change that the iterator currently points to;
606ab6098cfSlarrybr **
607ab6098cfSlarrybr ** *pnCol is set to the number of columns in the table affected by the change; and
608ab6098cfSlarrybr **
609ab6098cfSlarrybr ** *pzTab is set to point to a nul-terminated utf-8 encoded string containing
610ab6098cfSlarrybr ** the name of the table affected by the current change. The buffer remains
611ab6098cfSlarrybr ** valid until either sqlite3changeset_next() is called on the iterator
612ab6098cfSlarrybr ** or until the conflict-handler function returns.
613ab6098cfSlarrybr **
614ab6098cfSlarrybr ** If pbIndirect is not NULL, then *pbIndirect is set to true (1) if the change
615b4480e94Sdan ** is an indirect change, or false (0) otherwise. See the documentation for
616b4480e94Sdan ** [sqlite3session_indirect()] for a description of direct and indirect
617ab6098cfSlarrybr ** changes.
618c21111daSdan **
619c21111daSdan ** If no error occurs, SQLITE_OK is returned. If an error does occur, an
620c21111daSdan ** SQLite error code is returned. The values of the output variables may not
621c21111daSdan ** be trusted in this case.
6224fccf43aSdan */
6234fccf43aSdan int sqlite3changeset_op(
6244fccf43aSdan   sqlite3_changeset_iter *pIter,  /* Iterator object */
6254fccf43aSdan   const char **pzTab,             /* OUT: Pointer to table name */
6264fccf43aSdan   int *pnCol,                     /* OUT: Number of columns in table */
627b4480e94Sdan   int *pOp,                       /* OUT: SQLITE_INSERT, DELETE or UPDATE */
628b4480e94Sdan   int *pbIndirect                 /* OUT: True for an 'indirect' change */
6294fccf43aSdan );
630c21111daSdan 
631c21111daSdan /*
632244593c8Sdan ** CAPI3REF: Obtain The Primary Key Definition Of A Table
633bda30ce4Sdrh ** METHOD: sqlite3_changeset_iter
634244593c8Sdan **
635244593c8Sdan ** For each modified table, a changeset includes the following:
636244593c8Sdan **
637244593c8Sdan ** <ul>
638244593c8Sdan **   <li> The number of columns in the table, and
639244593c8Sdan **   <li> Which of those columns make up the tables PRIMARY KEY.
640244593c8Sdan ** </ul>
641244593c8Sdan **
642244593c8Sdan ** This function is used to find which columns comprise the PRIMARY KEY of
643244593c8Sdan ** the table modified by the change that iterator pIter currently points to.
644244593c8Sdan ** If successful, *pabPK is set to point to an array of nCol entries, where
645244593c8Sdan ** nCol is the number of columns in the table. Elements of *pabPK are set to
646244593c8Sdan ** 0x01 if the corresponding column is part of the tables primary key, or
647244593c8Sdan ** 0x00 if it is not.
648244593c8Sdan **
649ed204d1fSdrh ** If argument pnCol is not NULL, then *pnCol is set to the number of columns
650244593c8Sdan ** in the table.
651244593c8Sdan **
652244593c8Sdan ** If this function is called when the iterator does not point to a valid
653244593c8Sdan ** entry, SQLITE_MISUSE is returned and the output variables zeroed. Otherwise,
654244593c8Sdan ** SQLITE_OK is returned and the output variables populated as described
655244593c8Sdan ** above.
656244593c8Sdan */
657244593c8Sdan int sqlite3changeset_pk(
658244593c8Sdan   sqlite3_changeset_iter *pIter,  /* Iterator object */
659244593c8Sdan   unsigned char **pabPK,          /* OUT: Array of boolean - true for PK cols */
660244593c8Sdan   int *pnCol                      /* OUT: Number of entries in output array */
661244593c8Sdan );
662244593c8Sdan 
663244593c8Sdan /*
664a2df3d9fSdan ** CAPI3REF: Obtain old.* Values From A Changeset Iterator
665bda30ce4Sdrh ** METHOD: sqlite3_changeset_iter
666a2df3d9fSdan **
667c21111daSdan ** The pIter argument passed to this function may either be an iterator
668c21111daSdan ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
669c21111daSdan ** created by [sqlite3changeset_start()]. In the latter case, the most recent
670c21111daSdan ** call to [sqlite3changeset_next()] must have returned SQLITE_ROW.
671c21111daSdan ** Furthermore, it may only be called if the type of change that the iterator
67277e65004Sdan ** currently points to is either [SQLITE_DELETE] or [SQLITE_UPDATE]. Otherwise,
67377e65004Sdan ** this function returns [SQLITE_MISUSE] and sets *ppValue to NULL.
674c21111daSdan **
675c21111daSdan ** Argument iVal must be greater than or equal to 0, and less than the number
676c21111daSdan ** of columns in the table affected by the current change. Otherwise,
67777e65004Sdan ** [SQLITE_RANGE] is returned and *ppValue is set to NULL.
678c21111daSdan **
679c21111daSdan ** If successful, this function sets *ppValue to point to a protected
680c21111daSdan ** sqlite3_value object containing the iVal'th value from the vector of
681c21111daSdan ** original row values stored as part of the UPDATE or DELETE change and
682c21111daSdan ** returns SQLITE_OK. The name of the function comes from the fact that this
683c21111daSdan ** is similar to the "old.*" columns available to update or delete triggers.
684c21111daSdan **
685c21111daSdan ** If some other error occurs (e.g. an OOM condition), an SQLite error code
686c21111daSdan ** is returned and *ppValue is set to NULL.
687c21111daSdan */
6884fccf43aSdan int sqlite3changeset_old(
689296c7658Sdan   sqlite3_changeset_iter *pIter,  /* Changeset iterator */
690296c7658Sdan   int iVal,                       /* Column number */
6914fccf43aSdan   sqlite3_value **ppValue         /* OUT: Old value (or NULL pointer) */
6924fccf43aSdan );
693c21111daSdan 
694c21111daSdan /*
695a2df3d9fSdan ** CAPI3REF: Obtain new.* Values From A Changeset Iterator
696bda30ce4Sdrh ** METHOD: sqlite3_changeset_iter
697a2df3d9fSdan **
698c21111daSdan ** The pIter argument passed to this function may either be an iterator
699c21111daSdan ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
700c21111daSdan ** created by [sqlite3changeset_start()]. In the latter case, the most recent
701c21111daSdan ** call to [sqlite3changeset_next()] must have returned SQLITE_ROW.
702c21111daSdan ** Furthermore, it may only be called if the type of change that the iterator
70377e65004Sdan ** currently points to is either [SQLITE_UPDATE] or [SQLITE_INSERT]. Otherwise,
70477e65004Sdan ** this function returns [SQLITE_MISUSE] and sets *ppValue to NULL.
705c21111daSdan **
706c21111daSdan ** Argument iVal must be greater than or equal to 0, and less than the number
707c21111daSdan ** of columns in the table affected by the current change. Otherwise,
70877e65004Sdan ** [SQLITE_RANGE] is returned and *ppValue is set to NULL.
709c21111daSdan **
710c21111daSdan ** If successful, this function sets *ppValue to point to a protected
711c21111daSdan ** sqlite3_value object containing the iVal'th value from the vector of
712c21111daSdan ** new row values stored as part of the UPDATE or INSERT change and
713c21111daSdan ** returns SQLITE_OK. If the change is an UPDATE and does not include
714c21111daSdan ** a new value for the requested column, *ppValue is set to NULL and
715c21111daSdan ** SQLITE_OK returned. The name of the function comes from the fact that
716c21111daSdan ** this is similar to the "new.*" columns available to update or delete
717c21111daSdan ** triggers.
718c21111daSdan **
719c21111daSdan ** If some other error occurs (e.g. an OOM condition), an SQLite error code
720c21111daSdan ** is returned and *ppValue is set to NULL.
721c21111daSdan */
7224fccf43aSdan int sqlite3changeset_new(
723296c7658Sdan   sqlite3_changeset_iter *pIter,  /* Changeset iterator */
724296c7658Sdan   int iVal,                       /* Column number */
7254fccf43aSdan   sqlite3_value **ppValue         /* OUT: New value (or NULL pointer) */
7264fccf43aSdan );
727296c7658Sdan 
728d5f0767cSdan /*
729a2df3d9fSdan ** CAPI3REF: Obtain Conflicting Row Values From A Changeset Iterator
730bda30ce4Sdrh ** METHOD: sqlite3_changeset_iter
731a2df3d9fSdan **
732c21111daSdan ** This function should only be used with iterator objects passed to a
733c21111daSdan ** conflict-handler callback by [sqlite3changeset_apply()] with either
73477e65004Sdan ** [SQLITE_CHANGESET_DATA] or [SQLITE_CHANGESET_CONFLICT]. If this function
73577e65004Sdan ** is called on any other iterator, [SQLITE_MISUSE] is returned and *ppValue
736c21111daSdan ** is set to NULL.
737d5f0767cSdan **
738c21111daSdan ** Argument iVal must be greater than or equal to 0, and less than the number
739c21111daSdan ** of columns in the table affected by the current change. Otherwise,
74077e65004Sdan ** [SQLITE_RANGE] is returned and *ppValue is set to NULL.
741c21111daSdan **
742c21111daSdan ** If successful, this function sets *ppValue to point to a protected
743c21111daSdan ** sqlite3_value object containing the iVal'th value from the
744c21111daSdan ** "conflicting row" associated with the current conflict-handler callback
745c21111daSdan ** and returns SQLITE_OK.
746c21111daSdan **
747c21111daSdan ** If some other error occurs (e.g. an OOM condition), an SQLite error code
748c21111daSdan ** is returned and *ppValue is set to NULL.
749d5f0767cSdan */
750d5f0767cSdan int sqlite3changeset_conflict(
751296c7658Sdan   sqlite3_changeset_iter *pIter,  /* Changeset iterator */
752296c7658Sdan   int iVal,                       /* Column number */
753d5f0767cSdan   sqlite3_value **ppValue         /* OUT: Value from conflicting row */
754d5f0767cSdan );
755d5f0767cSdan 
756cb3e4b79Sdan /*
757cb3e4b79Sdan ** CAPI3REF: Determine The Number Of Foreign Key Constraint Violations
758bda30ce4Sdrh ** METHOD: sqlite3_changeset_iter
759cb3e4b79Sdan **
760cb3e4b79Sdan ** This function may only be called with an iterator passed to an
761cb3e4b79Sdan ** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case
762cb3e4b79Sdan ** it sets the output variable to the total number of known foreign key
763cb3e4b79Sdan ** violations in the destination database and returns SQLITE_OK.
764cb3e4b79Sdan **
765cb3e4b79Sdan ** In all other cases this function returns SQLITE_MISUSE.
766cb3e4b79Sdan */
767cb3e4b79Sdan int sqlite3changeset_fk_conflicts(
768cb3e4b79Sdan   sqlite3_changeset_iter *pIter,  /* Changeset iterator */
769cb3e4b79Sdan   int *pnOut                      /* OUT: Number of FK violations */
770cb3e4b79Sdan );
771cb3e4b79Sdan 
7724fccf43aSdan 
7734fccf43aSdan /*
774157546f4Sshaneh ** CAPI3REF: Finalize A Changeset Iterator
775bda30ce4Sdrh ** METHOD: sqlite3_changeset_iter
776a2df3d9fSdan **
777a2df3d9fSdan ** This function is used to finalize an iterator allocated with
77877e65004Sdan ** [sqlite3changeset_start()].
7794fccf43aSdan **
780c21111daSdan ** This function should only be called on iterators created using the
781c21111daSdan ** [sqlite3changeset_start()] function. If an application calls this
782c21111daSdan ** function with an iterator passed to a conflict-handler by
78377e65004Sdan ** [sqlite3changeset_apply()], [SQLITE_MISUSE] is immediately returned and the
784c21111daSdan ** call has no effect.
785c21111daSdan **
786c21111daSdan ** If an error was encountered within a call to an sqlite3changeset_xxx()
78777e65004Sdan ** function (for example an [SQLITE_CORRUPT] in [sqlite3changeset_next()] or an
78877e65004Sdan ** [SQLITE_NOMEM] in [sqlite3changeset_new()]) then an error code corresponding
789c21111daSdan ** to that error is returned by this function. Otherwise, SQLITE_OK is
790c21111daSdan ** returned. This is to allow the following pattern (pseudo-code):
791c21111daSdan **
7926a8a629eSdrh ** <pre>
793c21111daSdan **   sqlite3changeset_start();
794c21111daSdan **   while( SQLITE_ROW==sqlite3changeset_next() ){
795c21111daSdan **     // Do something with change.
796c21111daSdan **   }
797c21111daSdan **   rc = sqlite3changeset_finalize();
798c21111daSdan **   if( rc!=SQLITE_OK ){
799c21111daSdan **     // An error has occurred
800c21111daSdan **   }
8016a8a629eSdrh ** </pre>
8024fccf43aSdan */
8034fccf43aSdan int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter);
8044fccf43aSdan 
80591ddd559Sdan /*
806a2df3d9fSdan ** CAPI3REF: Invert A Changeset
807a2df3d9fSdan **
8082635a3beSdan ** This function is used to "invert" a changeset object. Applying an inverted
8092635a3beSdan ** changeset to a database reverses the effects of applying the uninverted
8102635a3beSdan ** changeset. Specifically:
8112635a3beSdan **
8122635a3beSdan ** <ul>
8132635a3beSdan **   <li> Each DELETE change is changed to an INSERT, and
8142635a3beSdan **   <li> Each INSERT change is changed to a DELETE, and
8152635a3beSdan **   <li> For each UPDATE change, the old.* and new.* values are exchanged.
8162635a3beSdan ** </ul>
8172635a3beSdan **
8186c39e6a8Sdan ** This function does not change the order in which changes appear within
8196c39e6a8Sdan ** the changeset. It merely reverses the sense of each individual change.
8206c39e6a8Sdan **
8212635a3beSdan ** If successful, a pointer to a buffer containing the inverted changeset
8222635a3beSdan ** is stored in *ppOut, the size of the same buffer is stored in *pnOut, and
8232635a3beSdan ** SQLITE_OK is returned. If an error occurs, both *pnOut and *ppOut are
8242635a3beSdan ** zeroed and an SQLite error code returned.
8252635a3beSdan **
8262635a3beSdan ** It is the responsibility of the caller to eventually call sqlite3_free()
8272635a3beSdan ** on the *ppOut pointer to free the buffer allocation following a successful
8282635a3beSdan ** call to this function.
829f51e5f6cSdan **
830f51e5f6cSdan ** WARNING/TODO: This function currently assumes that the input is a valid
831f51e5f6cSdan ** changeset. If it is not, the results are undefined.
83291ddd559Sdan */
83391ddd559Sdan int sqlite3changeset_invert(
834cfec7eeeSdan   int nIn, const void *pIn,       /* Input changeset */
83591ddd559Sdan   int *pnOut, void **ppOut        /* OUT: Inverse of input */
83691ddd559Sdan );
83791ddd559Sdan 
8386cda207fSdan /*
83929e03e97Sdan ** CAPI3REF: Concatenate Two Changeset Objects
84029e03e97Sdan **
84129e03e97Sdan ** This function is used to concatenate two changesets, A and B, into a
84229e03e97Sdan ** single changeset. The result is a changeset equivalent to applying
84329e03e97Sdan ** changeset A followed by changeset B.
84429e03e97Sdan **
8455898ad69Sdan ** This function combines the two input changesets using an
8465898ad69Sdan ** sqlite3_changegroup object. Calling it produces similar results as the
8475898ad69Sdan ** following code fragment:
84829e03e97Sdan **
8496a8a629eSdrh ** <pre>
8505898ad69Sdan **   sqlite3_changegroup *pGrp;
8515898ad69Sdan **   rc = sqlite3_changegroup_new(&pGrp);
8525898ad69Sdan **   if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nA, pA);
8535898ad69Sdan **   if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nB, pB);
8545898ad69Sdan **   if( rc==SQLITE_OK ){
8555898ad69Sdan **     rc = sqlite3changegroup_output(pGrp, pnOut, ppOut);
8565898ad69Sdan **   }else{
8575898ad69Sdan **     *ppOut = 0;
8585898ad69Sdan **     *pnOut = 0;
8595898ad69Sdan **   }
8606a8a629eSdrh ** </pre>
86129e03e97Sdan **
8625898ad69Sdan ** Refer to the sqlite3_changegroup documentation below for details.
8636cda207fSdan */
8645d607a6eSdan int sqlite3changeset_concat(
86529e03e97Sdan   int nA,                         /* Number of bytes in buffer pA */
86629e03e97Sdan   void *pA,                       /* Pointer to buffer containing changeset A */
86729e03e97Sdan   int nB,                         /* Number of bytes in buffer pB */
86829e03e97Sdan   void *pB,                       /* Pointer to buffer containing changeset B */
86929e03e97Sdan   int *pnOut,                     /* OUT: Number of bytes in output changeset */
87029e03e97Sdan   void **ppOut                    /* OUT: Buffer containing output changeset */
8715d607a6eSdan );
8725d607a6eSdan 
8735898ad69Sdan 
8745898ad69Sdan /*
87566501908Sdan ** CAPI3REF: Changegroup Handle
876bda30ce4Sdrh **
877bda30ce4Sdrh ** A changegroup is an object used to combine two or more
878bda30ce4Sdrh ** [changesets] or [patchsets]
8795898ad69Sdan */
8805898ad69Sdan typedef struct sqlite3_changegroup sqlite3_changegroup;
8815898ad69Sdan 
8825898ad69Sdan /*
88366501908Sdan ** CAPI3REF: Create A New Changegroup Object
884bda30ce4Sdrh ** CONSTRUCTOR: sqlite3_changegroup
8855898ad69Sdan **
8865898ad69Sdan ** An sqlite3_changegroup object is used to combine two or more changesets
8875898ad69Sdan ** (or patchsets) into a single changeset (or patchset). A single changegroup
8885898ad69Sdan ** object may combine changesets or patchsets, but not both. The output is
8895898ad69Sdan ** always in the same format as the input.
8905898ad69Sdan **
8915898ad69Sdan ** If successful, this function returns SQLITE_OK and populates (*pp) with
8925898ad69Sdan ** a pointer to a new sqlite3_changegroup object before returning. The caller
8935898ad69Sdan ** should eventually free the returned object using a call to
8945898ad69Sdan ** sqlite3changegroup_delete(). If an error occurs, an SQLite error code
8955898ad69Sdan ** (i.e. SQLITE_NOMEM) is returned and *pp is set to NULL.
8965898ad69Sdan **
8975898ad69Sdan ** The usual usage pattern for an sqlite3_changegroup object is as follows:
8985898ad69Sdan **
8995898ad69Sdan ** <ul>
9005898ad69Sdan **   <li> It is created using a call to sqlite3changegroup_new().
9015898ad69Sdan **
9025898ad69Sdan **   <li> Zero or more changesets (or patchsets) are added to the object
9035898ad69Sdan **        by calling sqlite3changegroup_add().
9045898ad69Sdan **
9055898ad69Sdan **   <li> The result of combining all input changesets together is obtained
9065898ad69Sdan **        by the application via a call to sqlite3changegroup_output().
9075898ad69Sdan **
9085898ad69Sdan **   <li> The object is deleted using a call to sqlite3changegroup_delete().
9095898ad69Sdan ** </ul>
9105898ad69Sdan **
9115898ad69Sdan ** Any number of calls to add() and output() may be made between the calls to
9125898ad69Sdan ** new() and delete(), and in any order.
9135898ad69Sdan **
9145898ad69Sdan ** As well as the regular sqlite3changegroup_add() and
9155898ad69Sdan ** sqlite3changegroup_output() functions, also available are the streaming
9165898ad69Sdan ** versions sqlite3changegroup_add_strm() and sqlite3changegroup_output_strm().
9175898ad69Sdan */
9185898ad69Sdan int sqlite3changegroup_new(sqlite3_changegroup **pp);
9195898ad69Sdan 
9205898ad69Sdan /*
92166501908Sdan ** CAPI3REF: Add A Changeset To A Changegroup
922bda30ce4Sdrh ** METHOD: sqlite3_changegroup
92366501908Sdan **
9245898ad69Sdan ** Add all changes within the changeset (or patchset) in buffer pData (size
9255898ad69Sdan ** nData bytes) to the changegroup.
9265898ad69Sdan **
9275898ad69Sdan ** If the buffer contains a patchset, then all prior calls to this function
9285898ad69Sdan ** on the same changegroup object must also have specified patchsets. Or, if
9295898ad69Sdan ** the buffer contains a changeset, so must have the earlier calls to this
9305898ad69Sdan ** function. Otherwise, SQLITE_ERROR is returned and no changes are added
9315898ad69Sdan ** to the changegroup.
9325898ad69Sdan **
9335898ad69Sdan ** Rows within the changeset and changegroup are identified by the values in
9345898ad69Sdan ** their PRIMARY KEY columns. A change in the changeset is considered to
9355898ad69Sdan ** apply to the same row as a change already present in the changegroup if
9365898ad69Sdan ** the two rows have the same primary key.
9375898ad69Sdan **
9385e769a50Sdrh ** Changes to rows that do not already appear in the changegroup are
9395898ad69Sdan ** simply copied into it. Or, if both the new changeset and the changegroup
9405898ad69Sdan ** contain changes that apply to a single row, the final contents of the
9415898ad69Sdan ** changegroup depends on the type of each change, as follows:
9425898ad69Sdan **
9435898ad69Sdan ** <table border=1 style="margin-left:8ex;margin-right:8ex">
9445898ad69Sdan **   <tr><th style="white-space:pre">Existing Change  </th>
9455898ad69Sdan **       <th style="white-space:pre">New Change       </th>
9465898ad69Sdan **       <th>Output Change
9475898ad69Sdan **   <tr><td>INSERT <td>INSERT <td>
9485898ad69Sdan **       The new change is ignored. This case does not occur if the new
9495898ad69Sdan **       changeset was recorded immediately after the changesets already
9505898ad69Sdan **       added to the changegroup.
9515898ad69Sdan **   <tr><td>INSERT <td>UPDATE <td>
9525898ad69Sdan **       The INSERT change remains in the changegroup. The values in the
9535898ad69Sdan **       INSERT change are modified as if the row was inserted by the
9545898ad69Sdan **       existing change and then updated according to the new change.
9555898ad69Sdan **   <tr><td>INSERT <td>DELETE <td>
9565898ad69Sdan **       The existing INSERT is removed from the changegroup. The DELETE is
9575898ad69Sdan **       not added.
9585898ad69Sdan **   <tr><td>UPDATE <td>INSERT <td>
9595898ad69Sdan **       The new change is ignored. This case does not occur if the new
9605898ad69Sdan **       changeset was recorded immediately after the changesets already
9615898ad69Sdan **       added to the changegroup.
9625898ad69Sdan **   <tr><td>UPDATE <td>UPDATE <td>
9635898ad69Sdan **       The existing UPDATE remains within the changegroup. It is amended
9645898ad69Sdan **       so that the accompanying values are as if the row was updated once
9655898ad69Sdan **       by the existing change and then again by the new change.
9665898ad69Sdan **   <tr><td>UPDATE <td>DELETE <td>
9675898ad69Sdan **       The existing UPDATE is replaced by the new DELETE within the
9685898ad69Sdan **       changegroup.
9695898ad69Sdan **   <tr><td>DELETE <td>INSERT <td>
9705898ad69Sdan **       If one or more of the column values in the row inserted by the
9715898ad69Sdan **       new change differ from those in the row deleted by the existing
9725898ad69Sdan **       change, the existing DELETE is replaced by an UPDATE within the
9735898ad69Sdan **       changegroup. Otherwise, if the inserted row is exactly the same
9745898ad69Sdan **       as the deleted row, the existing DELETE is simply discarded.
9755898ad69Sdan **   <tr><td>DELETE <td>UPDATE <td>
9765898ad69Sdan **       The new change is ignored. This case does not occur if the new
9775898ad69Sdan **       changeset was recorded immediately after the changesets already
9785898ad69Sdan **       added to the changegroup.
9795898ad69Sdan **   <tr><td>DELETE <td>DELETE <td>
9805898ad69Sdan **       The new change is ignored. This case does not occur if the new
9815898ad69Sdan **       changeset was recorded immediately after the changesets already
9825898ad69Sdan **       added to the changegroup.
9835898ad69Sdan ** </table>
9845898ad69Sdan **
9855898ad69Sdan ** If the new changeset contains changes to a table that is already present
9865898ad69Sdan ** in the changegroup, then the number of columns and the position of the
9875898ad69Sdan ** primary key columns for the table must be consistent. If this is not the
9885898ad69Sdan ** case, this function fails with SQLITE_SCHEMA. If the input changeset
9895898ad69Sdan ** appears to be corrupt and the corruption is detected, SQLITE_CORRUPT is
9905898ad69Sdan ** returned. Or, if an out-of-memory condition occurs during processing, this
9912bbcaee8Sdrh ** function returns SQLITE_NOMEM. In all cases, if an error occurs the state
9922bbcaee8Sdrh ** of the final contents of the changegroup is undefined.
9935898ad69Sdan **
9945898ad69Sdan ** If no error occurs, SQLITE_OK is returned.
9955898ad69Sdan */
9965898ad69Sdan int sqlite3changegroup_add(sqlite3_changegroup*, int nData, void *pData);
9975898ad69Sdan 
9985898ad69Sdan /*
99966501908Sdan ** CAPI3REF: Obtain A Composite Changeset From A Changegroup
1000bda30ce4Sdrh ** METHOD: sqlite3_changegroup
100166501908Sdan **
10025898ad69Sdan ** Obtain a buffer containing a changeset (or patchset) representing the
10035898ad69Sdan ** current contents of the changegroup. If the inputs to the changegroup
10045898ad69Sdan ** were themselves changesets, the output is a changeset. Or, if the
10055898ad69Sdan ** inputs were patchsets, the output is also a patchset.
10065898ad69Sdan **
10076c39e6a8Sdan ** As with the output of the sqlite3session_changeset() and
10086c39e6a8Sdan ** sqlite3session_patchset() functions, all changes related to a single
10096c39e6a8Sdan ** table are grouped together in the output of this function. Tables appear
10106c39e6a8Sdan ** in the same order as for the very first changeset added to the changegroup.
10116c39e6a8Sdan ** If the second or subsequent changesets added to the changegroup contain
10126c39e6a8Sdan ** changes for tables that do not appear in the first changeset, they are
10136c39e6a8Sdan ** appended onto the end of the output changeset, again in the order in
10146c39e6a8Sdan ** which they are first encountered.
10156c39e6a8Sdan **
10165898ad69Sdan ** If an error occurs, an SQLite error code is returned and the output
10175898ad69Sdan ** variables (*pnData) and (*ppData) are set to 0. Otherwise, SQLITE_OK
10185898ad69Sdan ** is returned and the output variables are set to the size of and a
10195898ad69Sdan ** pointer to the output buffer, respectively. In this case it is the
10205898ad69Sdan ** responsibility of the caller to eventually free the buffer using a
10215898ad69Sdan ** call to sqlite3_free().
10225898ad69Sdan */
10235898ad69Sdan int sqlite3changegroup_output(
10245898ad69Sdan   sqlite3_changegroup*,
10255898ad69Sdan   int *pnData,                    /* OUT: Size of output buffer in bytes */
10265898ad69Sdan   void **ppData                   /* OUT: Pointer to output buffer */
10275898ad69Sdan );
10285898ad69Sdan 
10295898ad69Sdan /*
103066501908Sdan ** CAPI3REF: Delete A Changegroup Object
1031bda30ce4Sdrh ** DESTRUCTOR: sqlite3_changegroup
10325898ad69Sdan */
10335898ad69Sdan void sqlite3changegroup_delete(sqlite3_changegroup*);
10345898ad69Sdan 
1035d5f0767cSdan /*
1036a2df3d9fSdan ** CAPI3REF: Apply A Changeset To A Database
1037a2df3d9fSdan **
103895ccb6dcSdan ** Apply a changeset or patchset to a database. These functions attempt to
103995ccb6dcSdan ** update the "main" database attached to handle db with the changes found in
104095ccb6dcSdan ** the changeset passed via the second and third arguments.
10412635a3beSdan **
104295ccb6dcSdan ** The fourth argument (xFilter) passed to these functions is the "filter
104340368988Sdan ** callback". If it is not NULL, then for each table affected by at least one
104440368988Sdan ** change in the changeset, the filter callback is invoked with
104540368988Sdan ** the table name as the second argument, and a copy of the context pointer
104695ccb6dcSdan ** passed as the sixth argument as the first. If the "filter callback"
104795ccb6dcSdan ** returns zero, then no attempt is made to apply any changes to the table.
104895ccb6dcSdan ** Otherwise, if the return value is non-zero or the xFilter argument to
104995ccb6dcSdan ** is NULL, all changes related to the table are attempted.
105040368988Sdan **
105140368988Sdan ** For each table that is not excluded by the filter callback, this function
105240368988Sdan ** tests that the target database contains a compatible table. A table is
105340368988Sdan ** considered compatible if all of the following are true:
10542635a3beSdan **
10552635a3beSdan ** <ul>
10562635a3beSdan **   <li> The table has the same name as the name recorded in the
10572635a3beSdan **        changeset, and
1058ff677b20Sdan **   <li> The table has at least as many columns as recorded in the
10592635a3beSdan **        changeset, and
10602635a3beSdan **   <li> The table has primary key columns in the same position as
10612635a3beSdan **        recorded in the changeset.
10622635a3beSdan ** </ul>
10632635a3beSdan **
106440368988Sdan ** If there is no compatible table, it is not an error, but none of the
106540368988Sdan ** changes associated with the table are applied. A warning message is issued
106640368988Sdan ** via the sqlite3_log() mechanism with the error code SQLITE_SCHEMA. At most
106740368988Sdan ** one such warning is issued for each table in the changeset.
10682635a3beSdan **
106940368988Sdan ** For each change for which there is a compatible table, an attempt is made
107040368988Sdan ** to modify the table contents according to the UPDATE, INSERT or DELETE
107140368988Sdan ** change. If a change cannot be applied cleanly, the conflict handler
107240368988Sdan ** function passed as the fifth argument to sqlite3changeset_apply() may be
107340368988Sdan ** invoked. A description of exactly when the conflict handler is invoked for
107440368988Sdan ** each type of change is below.
10752635a3beSdan **
1076082c96dfSdan ** Unlike the xFilter argument, xConflict may not be passed NULL. The results
1077082c96dfSdan ** of passing anything other than a valid function pointer as the xConflict
1078082c96dfSdan ** argument are undefined.
1079082c96dfSdan **
10802635a3beSdan ** Each time the conflict handler function is invoked, it must return one
10812635a3beSdan ** of [SQLITE_CHANGESET_OMIT], [SQLITE_CHANGESET_ABORT] or
10822635a3beSdan ** [SQLITE_CHANGESET_REPLACE]. SQLITE_CHANGESET_REPLACE may only be returned
10832635a3beSdan ** if the second argument passed to the conflict handler is either
10842635a3beSdan ** SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If the conflict-handler
10852635a3beSdan ** returns an illegal value, any changes already made are rolled back and
10862635a3beSdan ** the call to sqlite3changeset_apply() returns SQLITE_MISUSE. Different
10872635a3beSdan ** actions are taken by sqlite3changeset_apply() depending on the value
10882635a3beSdan ** returned by each invocation of the conflict-handler function. Refer to
10892635a3beSdan ** the documentation for the three
10902635a3beSdan ** [SQLITE_CHANGESET_OMIT|available return values] for details.
10912635a3beSdan **
10922635a3beSdan ** <dl>
10932635a3beSdan ** <dt>DELETE Changes<dd>
109495ccb6dcSdan **   For each DELETE change, the function checks if the target database
10952635a3beSdan **   contains a row with the same primary key value (or values) as the
10962635a3beSdan **   original row values stored in the changeset. If it does, and the values
10972635a3beSdan **   stored in all non-primary key columns also match the values stored in
10982635a3beSdan **   the changeset the row is deleted from the target database.
10992635a3beSdan **
11002635a3beSdan **   If a row with matching primary key values is found, but one or more of
11012635a3beSdan **   the non-primary key fields contains a value different from the original
11022635a3beSdan **   row value stored in the changeset, the conflict-handler function is
1103ff677b20Sdan **   invoked with [SQLITE_CHANGESET_DATA] as the second argument. If the
1104ff677b20Sdan **   database table has more columns than are recorded in the changeset,
1105ff677b20Sdan **   only the values of those non-primary key fields are compared against
1106ff677b20Sdan **   the current database contents - any trailing database table columns
1107ff677b20Sdan **   are ignored.
11082635a3beSdan **
11092635a3beSdan **   If no row with matching primary key values is found in the database,
11102635a3beSdan **   the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
11112635a3beSdan **   passed as the second argument.
11122635a3beSdan **
11132635a3beSdan **   If the DELETE operation is attempted, but SQLite returns SQLITE_CONSTRAINT
11142635a3beSdan **   (which can only happen if a foreign key constraint is violated), the
11152635a3beSdan **   conflict-handler function is invoked with [SQLITE_CHANGESET_CONSTRAINT]
11162635a3beSdan **   passed as the second argument. This includes the case where the DELETE
11172635a3beSdan **   operation is attempted because an earlier call to the conflict handler
11182635a3beSdan **   function returned [SQLITE_CHANGESET_REPLACE].
11192635a3beSdan **
11202635a3beSdan ** <dt>INSERT Changes<dd>
11212635a3beSdan **   For each INSERT change, an attempt is made to insert the new row into
1122ff677b20Sdan **   the database. If the changeset row contains fewer fields than the
1123ff677b20Sdan **   database table, the trailing fields are populated with their default
1124ff677b20Sdan **   values.
11252635a3beSdan **
11262635a3beSdan **   If the attempt to insert the row fails because the database already
11272635a3beSdan **   contains a row with the same primary key values, the conflict handler
11282635a3beSdan **   function is invoked with the second argument set to
11292635a3beSdan **   [SQLITE_CHANGESET_CONFLICT].
11302635a3beSdan **
11312635a3beSdan **   If the attempt to insert the row fails because of some other constraint
11322635a3beSdan **   violation (e.g. NOT NULL or UNIQUE), the conflict handler function is
11332635a3beSdan **   invoked with the second argument set to [SQLITE_CHANGESET_CONSTRAINT].
11342635a3beSdan **   This includes the case where the INSERT operation is re-attempted because
11352635a3beSdan **   an earlier call to the conflict handler function returned
11362635a3beSdan **   [SQLITE_CHANGESET_REPLACE].
11372635a3beSdan **
11382635a3beSdan ** <dt>UPDATE Changes<dd>
113995ccb6dcSdan **   For each UPDATE change, the function checks if the target database
11402635a3beSdan **   contains a row with the same primary key value (or values) as the
11412635a3beSdan **   original row values stored in the changeset. If it does, and the values
1142ff677b20Sdan **   stored in all modified non-primary key columns also match the values
1143ff677b20Sdan **   stored in the changeset the row is updated within the target database.
11442635a3beSdan **
11452635a3beSdan **   If a row with matching primary key values is found, but one or more of
1146ff677b20Sdan **   the modified non-primary key fields contains a value different from an
1147ff677b20Sdan **   original row value stored in the changeset, the conflict-handler function
1148ff677b20Sdan **   is invoked with [SQLITE_CHANGESET_DATA] as the second argument. Since
11492635a3beSdan **   UPDATE changes only contain values for non-primary key fields that are
11502635a3beSdan **   to be modified, only those fields need to match the original values to
11512635a3beSdan **   avoid the SQLITE_CHANGESET_DATA conflict-handler callback.
11522635a3beSdan **
11532635a3beSdan **   If no row with matching primary key values is found in the database,
11542635a3beSdan **   the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
11552635a3beSdan **   passed as the second argument.
11562635a3beSdan **
11572635a3beSdan **   If the UPDATE operation is attempted, but SQLite returns
11582635a3beSdan **   SQLITE_CONSTRAINT, the conflict-handler function is invoked with
11592635a3beSdan **   [SQLITE_CHANGESET_CONSTRAINT] passed as the second argument.
11602635a3beSdan **   This includes the case where the UPDATE operation is attempted after
11612635a3beSdan **   an earlier call to the conflict handler function returned
11622635a3beSdan **   [SQLITE_CHANGESET_REPLACE].
11632635a3beSdan ** </dl>
1164d5f0767cSdan **
1165d5f0767cSdan ** It is safe to execute SQL statements, including those that write to the
1166d5f0767cSdan ** table that the callback related to, from within the xConflict callback.
11672bbcaee8Sdrh ** This can be used to further customize the application's conflict
1168d5f0767cSdan ** resolution strategy.
11692635a3beSdan **
117095ccb6dcSdan ** All changes made by these functions are enclosed in a savepoint transaction.
11712635a3beSdan ** If any other error (aside from a constraint failure when attempting to
11722635a3beSdan ** write to the target database) occurs, then the savepoint transaction is
11732635a3beSdan ** rolled back, restoring the target database to its original state, and an
11742635a3beSdan ** SQLite error code returned.
117595ccb6dcSdan **
117695ccb6dcSdan ** If the output parameters (ppRebase) and (pnRebase) are non-NULL and
117795ccb6dcSdan ** the input is a changeset (not a patchset), then sqlite3changeset_apply_v2()
117895ccb6dcSdan ** may set (*ppRebase) to point to a "rebase" that may be used with the
117995ccb6dcSdan ** sqlite3_rebaser APIs buffer before returning. In this case (*pnRebase)
118095ccb6dcSdan ** is set to the size of the buffer in bytes. It is the responsibility of the
118195ccb6dcSdan ** caller to eventually free any such buffer using sqlite3_free(). The buffer
118295ccb6dcSdan ** is only allocated and populated if one or more conflicts were encountered
118395ccb6dcSdan ** while applying the patchset. See comments surrounding the sqlite3_rebaser
118495ccb6dcSdan ** APIs for further details.
1185fe55da38Sdan **
1186fe55da38Sdan ** The behavior of sqlite3changeset_apply_v2() and its streaming equivalent
1187fe55da38Sdan ** may be modified by passing a combination of
1188fe55da38Sdan ** [SQLITE_CHANGESETAPPLY_NOSAVEPOINT | supported flags] as the 9th parameter.
1189fe55da38Sdan **
1190fe55da38Sdan ** Note that the sqlite3changeset_apply_v2() API is still <b>experimental</b>
1191fe55da38Sdan ** and therefore subject to change.
1192d5f0767cSdan */
1193d5f0767cSdan int sqlite3changeset_apply(
1194296c7658Sdan   sqlite3 *db,                    /* Apply change to "main" db of this handle */
1195296c7658Sdan   int nChangeset,                 /* Size of changeset in bytes */
1196296c7658Sdan   void *pChangeset,               /* Changeset blob */
119740368988Sdan   int(*xFilter)(
119840368988Sdan     void *pCtx,                   /* Copy of sixth arg to _apply() */
119940368988Sdan     const char *zTab              /* Table name */
120040368988Sdan   ),
1201d5f0767cSdan   int(*xConflict)(
120240368988Sdan     void *pCtx,                   /* Copy of sixth arg to _apply() */
1203d5f0767cSdan     int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
1204d5f0767cSdan     sqlite3_changeset_iter *p     /* Handle describing change and conflict */
1205d5f0767cSdan   ),
1206296c7658Sdan   void *pCtx                      /* First argument passed to xConflict */
1207d5f0767cSdan );
1208a38e6c57Sdan int sqlite3changeset_apply_v2(
1209a38e6c57Sdan   sqlite3 *db,                    /* Apply change to "main" db of this handle */
1210a38e6c57Sdan   int nChangeset,                 /* Size of changeset in bytes */
1211a38e6c57Sdan   void *pChangeset,               /* Changeset blob */
1212a38e6c57Sdan   int(*xFilter)(
1213a38e6c57Sdan     void *pCtx,                   /* Copy of sixth arg to _apply() */
1214a38e6c57Sdan     const char *zTab              /* Table name */
1215a38e6c57Sdan   ),
1216a38e6c57Sdan   int(*xConflict)(
1217a38e6c57Sdan     void *pCtx,                   /* Copy of sixth arg to _apply() */
1218a38e6c57Sdan     int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
1219a38e6c57Sdan     sqlite3_changeset_iter *p     /* Handle describing change and conflict */
1220a38e6c57Sdan   ),
1221a38e6c57Sdan   void *pCtx,                     /* First argument passed to xConflict */
1222fe55da38Sdan   void **ppRebase, int *pnRebase, /* OUT: Rebase data */
122346de0728Sdan   int flags                       /* SESSION_CHANGESETAPPLY_* flags */
1224a38e6c57Sdan );
1225a38e6c57Sdan 
12260c698471Sdan /*
1227fe55da38Sdan ** CAPI3REF: Flags for sqlite3changeset_apply_v2
1228fe55da38Sdan **
1229fe55da38Sdan ** The following flags may passed via the 9th parameter to
1230fe55da38Sdan ** [sqlite3changeset_apply_v2] and [sqlite3changeset_apply_v2_strm]:
1231fe55da38Sdan **
1232fe55da38Sdan ** <dl>
1233fe55da38Sdan ** <dt>SQLITE_CHANGESETAPPLY_NOSAVEPOINT <dd>
1234fe55da38Sdan **   Usually, the sessions module encloses all operations performed by
1235fe55da38Sdan **   a single call to apply_v2() or apply_v2_strm() in a [SAVEPOINT]. The
1236fe55da38Sdan **   SAVEPOINT is committed if the changeset or patchset is successfully
1237fe55da38Sdan **   applied, or rolled back if an error occurs. Specifying this flag
1238fe55da38Sdan **   causes the sessions module to omit this savepoint. In this case, if the
1239fe55da38Sdan **   caller has an open transaction or savepoint when apply_v2() is called,
1240fe55da38Sdan **   it may revert the partially applied changeset by rolling it back.
124144748f27Sdan **
124244748f27Sdan ** <dt>SQLITE_CHANGESETAPPLY_INVERT <dd>
124344748f27Sdan **   Invert the changeset before applying it. This is equivalent to inverting
124444748f27Sdan **   a changeset using sqlite3changeset_invert() before applying it. It is
124544748f27Sdan **   an error to specify this flag with a patchset.
1246fe55da38Sdan */
1247fe55da38Sdan #define SQLITE_CHANGESETAPPLY_NOSAVEPOINT   0x0001
124844748f27Sdan #define SQLITE_CHANGESETAPPLY_INVERT        0x0002
1249fe55da38Sdan 
1250fe55da38Sdan /*
1251157546f4Sshaneh ** CAPI3REF: Constants Passed To The Conflict Handler
1252a2df3d9fSdan **
12532635a3beSdan ** Values that may be passed as the second argument to a conflict-handler.
12540c698471Sdan **
12552635a3beSdan ** <dl>
12562635a3beSdan ** <dt>SQLITE_CHANGESET_DATA<dd>
12570c698471Sdan **   The conflict handler is invoked with CHANGESET_DATA as the second argument
12580c698471Sdan **   when processing a DELETE or UPDATE change if a row with the required
12590c698471Sdan **   PRIMARY KEY fields is present in the database, but one or more other
12600c698471Sdan **   (non primary-key) fields modified by the update do not contain the
12610c698471Sdan **   expected "before" values.
12620c698471Sdan **
12630c698471Sdan **   The conflicting row, in this case, is the database row with the matching
12640c698471Sdan **   primary key.
12650c698471Sdan **
12662635a3beSdan ** <dt>SQLITE_CHANGESET_NOTFOUND<dd>
12670c698471Sdan **   The conflict handler is invoked with CHANGESET_NOTFOUND as the second
12680c698471Sdan **   argument when processing a DELETE or UPDATE change if a row with the
12690c698471Sdan **   required PRIMARY KEY fields is not present in the database.
12700c698471Sdan **
12710c698471Sdan **   There is no conflicting row in this case. The results of invoking the
12720c698471Sdan **   sqlite3changeset_conflict() API are undefined.
12730c698471Sdan **
12742635a3beSdan ** <dt>SQLITE_CHANGESET_CONFLICT<dd>
12750c698471Sdan **   CHANGESET_CONFLICT is passed as the second argument to the conflict
12762635a3beSdan **   handler while processing an INSERT change if the operation would result
12772635a3beSdan **   in duplicate primary key values.
12780c698471Sdan **
12790c698471Sdan **   The conflicting row in this case is the database row with the matching
12800c698471Sdan **   primary key.
12810c698471Sdan **
1282cb3e4b79Sdan ** <dt>SQLITE_CHANGESET_FOREIGN_KEY<dd>
1283cb3e4b79Sdan **   If foreign key handling is enabled, and applying a changeset leaves the
1284cb3e4b79Sdan **   database in a state containing foreign key violations, the conflict
1285cb3e4b79Sdan **   handler is invoked with CHANGESET_FOREIGN_KEY as the second argument
1286cb3e4b79Sdan **   exactly once before the changeset is committed. If the conflict handler
1287cb3e4b79Sdan **   returns CHANGESET_OMIT, the changes, including those that caused the
1288cb3e4b79Sdan **   foreign key constraint violation, are committed. Or, if it returns
1289cb3e4b79Sdan **   CHANGESET_ABORT, the changeset is rolled back.
1290cb3e4b79Sdan **
1291cb3e4b79Sdan **   No current or conflicting row information is provided. The only function
1292cb3e4b79Sdan **   it is possible to call on the supplied sqlite3_changeset_iter handle
1293cb3e4b79Sdan **   is sqlite3changeset_fk_conflicts().
1294cb3e4b79Sdan **
12952635a3beSdan ** <dt>SQLITE_CHANGESET_CONSTRAINT<dd>
12960c698471Sdan **   If any other constraint violation occurs while applying a change (i.e.
1297cb3e4b79Sdan **   a UNIQUE, CHECK or NOT NULL constraint), the conflict handler is
1298cb3e4b79Sdan **   invoked with CHANGESET_CONSTRAINT as the second argument.
12990c698471Sdan **
13000c698471Sdan **   There is no conflicting row in this case. The results of invoking the
13010c698471Sdan **   sqlite3changeset_conflict() API are undefined.
1302cb3e4b79Sdan **
13032635a3beSdan ** </dl>
13040c698471Sdan */
1305d5f0767cSdan #define SQLITE_CHANGESET_DATA        1
1306d5f0767cSdan #define SQLITE_CHANGESET_NOTFOUND    2
1307d5f0767cSdan #define SQLITE_CHANGESET_CONFLICT    3
1308d5f0767cSdan #define SQLITE_CHANGESET_CONSTRAINT  4
1309cb3e4b79Sdan #define SQLITE_CHANGESET_FOREIGN_KEY 5
1310d5f0767cSdan 
13110c698471Sdan /*
1312a2df3d9fSdan ** CAPI3REF: Constants Returned By The Conflict Handler
1313a2df3d9fSdan **
1314a2df3d9fSdan ** A conflict handler callback must return one of the following three values.
13150c698471Sdan **
13162635a3beSdan ** <dl>
13172635a3beSdan ** <dt>SQLITE_CHANGESET_OMIT<dd>
13180c698471Sdan **   If a conflict handler returns this value no special action is taken. The
13192635a3beSdan **   change that caused the conflict is not applied. The session module
13202635a3beSdan **   continues to the next change in the changeset.
13210c698471Sdan **
13222635a3beSdan ** <dt>SQLITE_CHANGESET_REPLACE<dd>
13230c698471Sdan **   This value may only be returned if the second argument to the conflict
13240c698471Sdan **   handler was SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If this
13250c698471Sdan **   is not the case, any changes applied so far are rolled back and the
13260c698471Sdan **   call to sqlite3changeset_apply() returns SQLITE_MISUSE.
13270c698471Sdan **
13280c698471Sdan **   If CHANGESET_REPLACE is returned by an SQLITE_CHANGESET_DATA conflict
13290c698471Sdan **   handler, then the conflicting row is either updated or deleted, depending
13300c698471Sdan **   on the type of change.
13310c698471Sdan **
13320c698471Sdan **   If CHANGESET_REPLACE is returned by an SQLITE_CHANGESET_CONFLICT conflict
13330c698471Sdan **   handler, then the conflicting row is removed from the database and a
13340c698471Sdan **   second attempt to apply the change is made. If this second attempt fails,
13350c698471Sdan **   the original row is restored to the database before continuing.
13360c698471Sdan **
13372635a3beSdan ** <dt>SQLITE_CHANGESET_ABORT<dd>
13380c698471Sdan **   If this value is returned, any changes applied so far are rolled back
13392635a3beSdan **   and the call to sqlite3changeset_apply() returns SQLITE_ABORT.
13402635a3beSdan ** </dl>
13410c698471Sdan */
1342d5f0767cSdan #define SQLITE_CHANGESET_OMIT       0
1343d5f0767cSdan #define SQLITE_CHANGESET_REPLACE    1
1344d5f0767cSdan #define SQLITE_CHANGESET_ABORT      2
134591ddd559Sdan 
1346f01d3a7eSdan /*
1347f01d3a7eSdan ** CAPI3REF: Rebasing changesets
1348cbeee957Sdan ** EXPERIMENTAL
1349f01d3a7eSdan **
1350bd45374cSdan ** Suppose there is a site hosting a database in state S0. And that
1351bd45374cSdan ** modifications are made that move that database to state S1 and a
1352bd45374cSdan ** changeset recorded (the "local" changeset). Then, a changeset based
1353bd45374cSdan ** on S0 is received from another site (the "remote" changeset) and
1354bd45374cSdan ** applied to the database. The database is then in state
1355bd45374cSdan ** (S1+"remote"), where the exact state depends on any conflict
1356bd45374cSdan ** resolution decisions (OMIT or REPLACE) made while applying "remote".
1357bd45374cSdan ** Rebasing a changeset is to update it to take those conflict
1358bd45374cSdan ** resolution decisions into account, so that the same conflicts
1359bd45374cSdan ** do not have to be resolved elsewhere in the network.
1360bd45374cSdan **
1361bd45374cSdan ** For example, if both the local and remote changesets contain an
1362bd45374cSdan ** INSERT of the same key on "CREATE TABLE t1(a PRIMARY KEY, b)":
1363bd45374cSdan **
1364bd45374cSdan **   local:  INSERT INTO t1 VALUES(1, 'v1');
1365bd45374cSdan **   remote: INSERT INTO t1 VALUES(1, 'v2');
1366bd45374cSdan **
1367bd45374cSdan ** and the conflict resolution is REPLACE, then the INSERT change is
1368bd45374cSdan ** removed from the local changeset (it was overridden). Or, if the
1369bd45374cSdan ** conflict resolution was "OMIT", then the local changeset is modified
1370bd45374cSdan ** to instead contain:
1371bd45374cSdan **
1372bd45374cSdan **           UPDATE t1 SET b = 'v2' WHERE a=1;
1373bd45374cSdan **
1374bd45374cSdan ** Changes within the local changeset are rebased as follows:
1375f01d3a7eSdan **
1376f01d3a7eSdan ** <dl>
1377bd45374cSdan ** <dt>Local INSERT<dd>
1378f01d3a7eSdan **   This may only conflict with a remote INSERT. If the conflict
1379f01d3a7eSdan **   resolution was OMIT, then add an UPDATE change to the rebased
1380f01d3a7eSdan **   changeset. Or, if the conflict resolution was REPLACE, add
1381f01d3a7eSdan **   nothing to the rebased changeset.
1382f01d3a7eSdan **
1383bd45374cSdan ** <dt>Local DELETE<dd>
1384f01d3a7eSdan **   This may conflict with a remote UPDATE or DELETE. In both cases the
1385f01d3a7eSdan **   only possible resolution is OMIT. If the remote operation was a
1386f01d3a7eSdan **   DELETE, then add no change to the rebased changeset. If the remote
1387bd45374cSdan **   operation was an UPDATE, then the old.* fields of change are updated
1388bd45374cSdan **   to reflect the new.* values in the UPDATE.
1389f01d3a7eSdan **
1390bd45374cSdan ** <dt>Local UPDATE<dd>
1391f01d3a7eSdan **   This may conflict with a remote UPDATE or DELETE. If it conflicts
1392f01d3a7eSdan **   with a DELETE, and the conflict resolution was OMIT, then the update
1393f01d3a7eSdan **   is changed into an INSERT. Any undefined values in the new.* record
1394bd45374cSdan **   from the update change are filled in using the old.* values from
1395f01d3a7eSdan **   the conflicting DELETE. Or, if the conflict resolution was REPLACE,
1396f01d3a7eSdan **   the UPDATE change is simply omitted from the rebased changeset.
1397f01d3a7eSdan **
1398f01d3a7eSdan **   If conflict is with a remote UPDATE and the resolution is OMIT, then
1399f01d3a7eSdan **   the old.* values are rebased using the new.* values in the remote
1400f01d3a7eSdan **   change. Or, if the resolution is REPLACE, then the change is copied
1401f01d3a7eSdan **   into the rebased changeset with updates to columns also updated by
140224a0c453Sdan **   the conflicting remote UPDATE removed. If this means no columns would
140324a0c453Sdan **   be updated, the change is omitted.
1404f01d3a7eSdan ** </dl>
1405bd45374cSdan **
1406bd45374cSdan ** A local change may be rebased against multiple remote changes
140724a0c453Sdan ** simultaneously. If a single key is modified by multiple remote
140824a0c453Sdan ** changesets, they are combined as follows before the local changeset
140924a0c453Sdan ** is rebased:
141024a0c453Sdan **
141124a0c453Sdan ** <ul>
141224a0c453Sdan **    <li> If there has been one or more REPLACE resolutions on a
141324a0c453Sdan **         key, it is rebased according to a REPLACE.
141424a0c453Sdan **
141524a0c453Sdan **    <li> If there have been no REPLACE resolutions on a key, then
141624a0c453Sdan **         the local changeset is rebased according to the most recent
141724a0c453Sdan **         of the OMIT resolutions.
141824a0c453Sdan ** </ul>
141924a0c453Sdan **
142024a0c453Sdan ** Note that conflict resolutions from multiple remote changesets are
142124a0c453Sdan ** combined on a per-field basis, not per-row. This means that in the
142224a0c453Sdan ** case of multiple remote UPDATE operations, some fields of a single
142324a0c453Sdan ** local change may be rebased for REPLACE while others are rebased for
142424a0c453Sdan ** OMIT.
142595ccb6dcSdan **
142695ccb6dcSdan ** In order to rebase a local changeset, the remote changeset must first
142795ccb6dcSdan ** be applied to the local database using sqlite3changeset_apply_v2() and
142895ccb6dcSdan ** the buffer of rebase information captured. Then:
142995ccb6dcSdan **
143095ccb6dcSdan ** <ol>
143195ccb6dcSdan **   <li> An sqlite3_rebaser object is created by calling
143295ccb6dcSdan **        sqlite3rebaser_create().
143395ccb6dcSdan **   <li> The new object is configured with the rebase buffer obtained from
143495ccb6dcSdan **        sqlite3changeset_apply_v2() by calling sqlite3rebaser_configure().
143595ccb6dcSdan **        If the local changeset is to be rebased against multiple remote
143695ccb6dcSdan **        changesets, then sqlite3rebaser_configure() should be called
143795ccb6dcSdan **        multiple times, in the same order that the multiple
143895ccb6dcSdan **        sqlite3changeset_apply_v2() calls were made.
143995ccb6dcSdan **   <li> Each local changeset is rebased by calling sqlite3rebaser_rebase().
144095ccb6dcSdan **   <li> The sqlite3_rebaser object is deleted by calling
144195ccb6dcSdan **        sqlite3rebaser_delete().
144295ccb6dcSdan ** </ol>
1443f01d3a7eSdan */
1444c0a499eaSdan typedef struct sqlite3_rebaser sqlite3_rebaser;
1445c0a499eaSdan 
144695ccb6dcSdan /*
1447cbeee957Sdan ** CAPI3REF: Create a changeset rebaser object.
1448cbeee957Sdan ** EXPERIMENTAL
144995ccb6dcSdan **
145095ccb6dcSdan ** Allocate a new changeset rebaser object. If successful, set (*ppNew) to
145195ccb6dcSdan ** point to the new object and return SQLITE_OK. Otherwise, if an error
145295ccb6dcSdan ** occurs, return an SQLite error code (e.g. SQLITE_NOMEM) and set (*ppNew)
145395ccb6dcSdan ** to NULL.
145495ccb6dcSdan */
1455c0a499eaSdan int sqlite3rebaser_create(sqlite3_rebaser **ppNew);
1456c0a499eaSdan 
145795ccb6dcSdan /*
1458cbeee957Sdan ** CAPI3REF: Configure a changeset rebaser object.
1459cbeee957Sdan ** EXPERIMENTAL
146095ccb6dcSdan **
146195ccb6dcSdan ** Configure the changeset rebaser object to rebase changesets according
146295ccb6dcSdan ** to the conflict resolutions described by buffer pRebase (size nRebase
146395ccb6dcSdan ** bytes), which must have been obtained from a previous call to
146495ccb6dcSdan ** sqlite3changeset_apply_v2().
146595ccb6dcSdan */
1466c0a499eaSdan int sqlite3rebaser_configure(
1467c0a499eaSdan   sqlite3_rebaser*,
1468c0a499eaSdan   int nRebase, const void *pRebase
1469c0a499eaSdan );
1470c0a499eaSdan 
147195ccb6dcSdan /*
1472cbeee957Sdan ** CAPI3REF: Rebase a changeset
1473cbeee957Sdan ** EXPERIMENTAL
147495ccb6dcSdan **
147595ccb6dcSdan ** Argument pIn must point to a buffer containing a changeset nIn bytes
147695ccb6dcSdan ** in size. This function allocates and populates a buffer with a copy
14772bbcaee8Sdrh ** of the changeset rebased according to the configuration of the
147895ccb6dcSdan ** rebaser object passed as the first argument. If successful, (*ppOut)
1479a920b209Sdrh ** is set to point to the new buffer containing the rebased changeset and
148095ccb6dcSdan ** (*pnOut) to its size in bytes and SQLITE_OK returned. It is the
148195ccb6dcSdan ** responsibility of the caller to eventually free the new buffer using
148295ccb6dcSdan ** sqlite3_free(). Otherwise, if an error occurs, (*ppOut) and (*pnOut)
148395ccb6dcSdan ** are set to zero and an SQLite error code returned.
148495ccb6dcSdan */
1485c0a499eaSdan int sqlite3rebaser_rebase(
1486c0a499eaSdan   sqlite3_rebaser*,
1487c0a499eaSdan   int nIn, const void *pIn,
1488c0a499eaSdan   int *pnOut, void **ppOut
1489c0a499eaSdan );
1490c0a499eaSdan 
149195ccb6dcSdan /*
1492cbeee957Sdan ** CAPI3REF: Delete a changeset rebaser object.
1493cbeee957Sdan ** EXPERIMENTAL
149495ccb6dcSdan **
149595ccb6dcSdan ** Delete the changeset rebaser object and all associated resources. There
149695ccb6dcSdan ** should be one call to this function for each successful invocation
149795ccb6dcSdan ** of sqlite3rebaser_create().
149895ccb6dcSdan */
1499f1b40e83Sdan void sqlite3rebaser_delete(sqlite3_rebaser *p);
1500c0a499eaSdan 
150136828bd9Sdrh /*
150216228167Sdan ** CAPI3REF: Streaming Versions of API functions.
150316228167Sdan **
1504f1a08ad8Sdrh ** The six streaming API xxx_strm() functions serve similar purposes to the
150516228167Sdan ** corresponding non-streaming API functions:
150616228167Sdan **
150716228167Sdan ** <table border=1 style="margin-left:8ex;margin-right:8ex">
150816228167Sdan **   <tr><th>Streaming function<th>Non-streaming equivalent</th>
15093dfbe9b3Smistachkin **   <tr><td>sqlite3changeset_apply_strm<td>[sqlite3changeset_apply]
1510fe55da38Sdan **   <tr><td>sqlite3changeset_apply_strm_v2<td>[sqlite3changeset_apply_v2]
15113dfbe9b3Smistachkin **   <tr><td>sqlite3changeset_concat_strm<td>[sqlite3changeset_concat]
15123dfbe9b3Smistachkin **   <tr><td>sqlite3changeset_invert_strm<td>[sqlite3changeset_invert]
15133dfbe9b3Smistachkin **   <tr><td>sqlite3changeset_start_strm<td>[sqlite3changeset_start]
15143dfbe9b3Smistachkin **   <tr><td>sqlite3session_changeset_strm<td>[sqlite3session_changeset]
15153dfbe9b3Smistachkin **   <tr><td>sqlite3session_patchset_strm<td>[sqlite3session_patchset]
151616228167Sdan ** </table>
151716228167Sdan **
151816228167Sdan ** Non-streaming functions that accept changesets (or patchsets) as input
151916228167Sdan ** require that the entire changeset be stored in a single buffer in memory.
152016228167Sdan ** Similarly, those that return a changeset or patchset do so by returning
152116228167Sdan ** a pointer to a single large buffer allocated using sqlite3_malloc().
152216228167Sdan ** Normally this is convenient. However, if an application running in a
152316228167Sdan ** low-memory environment is required to handle very large changesets, the
152416228167Sdan ** large contiguous memory allocations required can become onerous.
152516228167Sdan **
152616228167Sdan ** In order to avoid this problem, instead of a single large buffer, input
152716228167Sdan ** is passed to a streaming API functions by way of a callback function that
152816228167Sdan ** the sessions module invokes to incrementally request input data as it is
152916228167Sdan ** required. In all cases, a pair of API function parameters such as
153016228167Sdan **
153116228167Sdan **  <pre>
153216228167Sdan **  &nbsp;     int nChangeset,
153316228167Sdan **  &nbsp;     void *pChangeset,
153416228167Sdan **  </pre>
153516228167Sdan **
153616228167Sdan ** Is replaced by:
153716228167Sdan **
153816228167Sdan **  <pre>
153916228167Sdan **  &nbsp;     int (*xInput)(void *pIn, void *pData, int *pnData),
154016228167Sdan **  &nbsp;     void *pIn,
154116228167Sdan **  </pre>
154216228167Sdan **
154316228167Sdan ** Each time the xInput callback is invoked by the sessions module, the first
154416228167Sdan ** argument passed is a copy of the supplied pIn context pointer. The second
154516228167Sdan ** argument, pData, points to a buffer (*pnData) bytes in size. Assuming no
154616228167Sdan ** error occurs the xInput method should copy up to (*pnData) bytes of data
154716228167Sdan ** into the buffer and set (*pnData) to the actual number of bytes copied
154816228167Sdan ** before returning SQLITE_OK. If the input is completely exhausted, (*pnData)
154916228167Sdan ** should be set to zero to indicate this. Or, if an error occurs, an SQLite
155016228167Sdan ** error code should be returned. In all cases, if an xInput callback returns
155116228167Sdan ** an error, all processing is abandoned and the streaming API function
155216228167Sdan ** returns a copy of the error code to the caller.
155316228167Sdan **
1554f1a08ad8Sdrh ** In the case of sqlite3changeset_start_strm(), the xInput callback may be
155516228167Sdan ** invoked by the sessions module at any point during the lifetime of the
155616228167Sdan ** iterator. If such an xInput callback returns an error, the iterator enters
155716228167Sdan ** an error state, whereby all subsequent calls to iterator functions
155816228167Sdan ** immediately fail with the same error code as returned by xInput.
155916228167Sdan **
156016228167Sdan ** Similarly, streaming API functions that return changesets (or patchsets)
156116228167Sdan ** return them in chunks by way of a callback function instead of via a
156216228167Sdan ** pointer to a single large buffer. In this case, a pair of parameters such
156316228167Sdan ** as:
156416228167Sdan **
156516228167Sdan **  <pre>
156616228167Sdan **  &nbsp;     int *pnChangeset,
156716228167Sdan **  &nbsp;     void **ppChangeset,
156816228167Sdan **  </pre>
156916228167Sdan **
157016228167Sdan ** Is replaced by:
157116228167Sdan **
157216228167Sdan **  <pre>
157316228167Sdan **  &nbsp;     int (*xOutput)(void *pOut, const void *pData, int nData),
157416228167Sdan **  &nbsp;     void *pOut
157516228167Sdan **  </pre>
157616228167Sdan **
157716228167Sdan ** The xOutput callback is invoked zero or more times to return data to
157816228167Sdan ** the application. The first parameter passed to each call is a copy of the
157916228167Sdan ** pOut pointer supplied by the application. The second parameter, pData,
158016228167Sdan ** points to a buffer nData bytes in size containing the chunk of output
158116228167Sdan ** data being returned. If the xOutput callback successfully processes the
158216228167Sdan ** supplied data, it should return SQLITE_OK to indicate success. Otherwise,
158316228167Sdan ** it should return some other SQLite error code. In this case processing
158416228167Sdan ** is immediately abandoned and the streaming API function returns a copy
158516228167Sdan ** of the xOutput error code to the application.
158616228167Sdan **
158716228167Sdan ** The sessions module never invokes an xOutput callback with the third
158816228167Sdan ** parameter set to a value less than or equal to zero. Other than this,
158916228167Sdan ** no guarantees are made as to the size of the chunks of data returned.
159016228167Sdan */
1591f1a08ad8Sdrh int sqlite3changeset_apply_strm(
159216228167Sdan   sqlite3 *db,                    /* Apply change to "main" db of this handle */
159316228167Sdan   int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
159416228167Sdan   void *pIn,                                          /* First arg for xInput */
159516228167Sdan   int(*xFilter)(
159616228167Sdan     void *pCtx,                   /* Copy of sixth arg to _apply() */
159716228167Sdan     const char *zTab              /* Table name */
159816228167Sdan   ),
159916228167Sdan   int(*xConflict)(
160016228167Sdan     void *pCtx,                   /* Copy of sixth arg to _apply() */
160116228167Sdan     int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
160216228167Sdan     sqlite3_changeset_iter *p     /* Handle describing change and conflict */
160316228167Sdan   ),
160416228167Sdan   void *pCtx                      /* First argument passed to xConflict */
160516228167Sdan );
1606a38e6c57Sdan int sqlite3changeset_apply_v2_strm(
1607a38e6c57Sdan   sqlite3 *db,                    /* Apply change to "main" db of this handle */
1608a38e6c57Sdan   int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
1609a38e6c57Sdan   void *pIn,                                          /* First arg for xInput */
1610a38e6c57Sdan   int(*xFilter)(
1611a38e6c57Sdan     void *pCtx,                   /* Copy of sixth arg to _apply() */
1612a38e6c57Sdan     const char *zTab              /* Table name */
1613a38e6c57Sdan   ),
1614a38e6c57Sdan   int(*xConflict)(
1615a38e6c57Sdan     void *pCtx,                   /* Copy of sixth arg to _apply() */
1616a38e6c57Sdan     int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
1617a38e6c57Sdan     sqlite3_changeset_iter *p     /* Handle describing change and conflict */
1618a38e6c57Sdan   ),
1619a38e6c57Sdan   void *pCtx,                     /* First argument passed to xConflict */
1620fe55da38Sdan   void **ppRebase, int *pnRebase,
1621fe55da38Sdan   int flags
1622a38e6c57Sdan );
1623f1a08ad8Sdrh int sqlite3changeset_concat_strm(
162416228167Sdan   int (*xInputA)(void *pIn, void *pData, int *pnData),
162516228167Sdan   void *pInA,
162616228167Sdan   int (*xInputB)(void *pIn, void *pData, int *pnData),
162716228167Sdan   void *pInB,
162816228167Sdan   int (*xOutput)(void *pOut, const void *pData, int nData),
162916228167Sdan   void *pOut
163016228167Sdan );
1631f1a08ad8Sdrh int sqlite3changeset_invert_strm(
163216228167Sdan   int (*xInput)(void *pIn, void *pData, int *pnData),
163316228167Sdan   void *pIn,
163416228167Sdan   int (*xOutput)(void *pOut, const void *pData, int nData),
163516228167Sdan   void *pOut
163616228167Sdan );
1637f1a08ad8Sdrh int sqlite3changeset_start_strm(
163816228167Sdan   sqlite3_changeset_iter **pp,
163916228167Sdan   int (*xInput)(void *pIn, void *pData, int *pnData),
164016228167Sdan   void *pIn
164116228167Sdan );
164246de0728Sdan int sqlite3changeset_start_v2_strm(
164346de0728Sdan   sqlite3_changeset_iter **pp,
164446de0728Sdan   int (*xInput)(void *pIn, void *pData, int *pnData),
164546de0728Sdan   void *pIn,
164646de0728Sdan   int flags
164746de0728Sdan );
1648f1a08ad8Sdrh int sqlite3session_changeset_strm(
164916228167Sdan   sqlite3_session *pSession,
165016228167Sdan   int (*xOutput)(void *pOut, const void *pData, int nData),
165116228167Sdan   void *pOut
165216228167Sdan );
1653f1a08ad8Sdrh int sqlite3session_patchset_strm(
165416228167Sdan   sqlite3_session *pSession,
165516228167Sdan   int (*xOutput)(void *pOut, const void *pData, int nData),
165616228167Sdan   void *pOut
165716228167Sdan );
16585898ad69Sdan int sqlite3changegroup_add_strm(sqlite3_changegroup*,
16595898ad69Sdan     int (*xInput)(void *pIn, void *pData, int *pnData),
16605898ad69Sdan     void *pIn
16615898ad69Sdan );
16625898ad69Sdan int sqlite3changegroup_output_strm(sqlite3_changegroup*,
16635898ad69Sdan     int (*xOutput)(void *pOut, const void *pData, int nData),
16645898ad69Sdan     void *pOut
16655898ad69Sdan );
1666c0a499eaSdan int sqlite3rebaser_rebase_strm(
1667c0a499eaSdan   sqlite3_rebaser *pRebaser,
1668c0a499eaSdan   int (*xInput)(void *pIn, void *pData, int *pnData),
1669c0a499eaSdan   void *pIn,
1670c0a499eaSdan   int (*xOutput)(void *pOut, const void *pData, int nData),
1671c0a499eaSdan   void *pOut
1672c0a499eaSdan );
167316228167Sdan 
16741f48e67dSdan /*
16751f48e67dSdan ** CAPI3REF: Configure global parameters
16761f48e67dSdan **
16771f48e67dSdan ** The sqlite3session_config() interface is used to make global configuration
16781f48e67dSdan ** changes to the sessions module in order to tune it to the specific needs
16791f48e67dSdan ** of the application.
16801f48e67dSdan **
16811f48e67dSdan ** The sqlite3session_config() interface is not threadsafe. If it is invoked
16821f48e67dSdan ** while any other thread is inside any other sessions method then the
16831f48e67dSdan ** results are undefined. Furthermore, if it is invoked after any sessions
16841f48e67dSdan ** related objects have been created, the results are also undefined.
16851f48e67dSdan **
16861f48e67dSdan ** The first argument to the sqlite3session_config() function must be one
16871f48e67dSdan ** of the SQLITE_SESSION_CONFIG_XXX constants defined below. The
16881f48e67dSdan ** interpretation of the (void*) value passed as the second parameter and
16891f48e67dSdan ** the effect of calling this function depends on the value of the first
16901f48e67dSdan ** parameter.
16911f48e67dSdan **
16921f48e67dSdan ** <dl>
16931f48e67dSdan ** <dt>SQLITE_SESSION_CONFIG_STRMSIZE<dd>
16941f48e67dSdan **    By default, the sessions module streaming interfaces attempt to input
16951f48e67dSdan **    and output data in approximately 1 KiB chunks. This operand may be used
16961f48e67dSdan **    to set and query the value of this configuration setting. The pointer
16971f48e67dSdan **    passed as the second argument must point to a value of type (int).
16981f48e67dSdan **    If this value is greater than 0, it is used as the new streaming data
16991f48e67dSdan **    chunk size for both input and output. Before returning, the (int) value
17001f48e67dSdan **    pointed to by pArg is set to the final value of the streaming interface
17011f48e67dSdan **    chunk size.
17021f48e67dSdan ** </dl>
17031f48e67dSdan **
17041f48e67dSdan ** This function returns SQLITE_OK if successful, or an SQLite error code
17051f48e67dSdan ** otherwise.
17061f48e67dSdan */
17071f48e67dSdan int sqlite3session_config(int op, void *pArg);
17081f48e67dSdan 
17091f48e67dSdan /*
17101f48e67dSdan ** CAPI3REF: Values for sqlite3session_config().
17111f48e67dSdan */
17121f48e67dSdan #define SQLITE_SESSION_CONFIG_STRMSIZE 1
171316228167Sdan 
171416228167Sdan /*
171536828bd9Sdrh ** Make sure we can call this stuff from C++.
171636828bd9Sdrh */
171736828bd9Sdrh #ifdef __cplusplus
171836828bd9Sdrh }
17194fccf43aSdan #endif
17204fccf43aSdan 
17214e80d5fcSdrh #endif  /* !defined(__SQLITESESSION_H_) && defined(SQLITE_ENABLE_SESSION) */
1722