xref: /netbsd-src/external/public-domain/sqlite/man/sqlite3session_changeset.3 (revision 53b02e147d4ed531c0d2a5ca9b3e8026ba3e99b5)
1.Dd December 19, 2018
2.Dt SQLITE3SESSION_CHANGESET 3
3.Os
4.Sh NAME
5.Nm sqlite3session_changeset
6.Nd Generate A Changeset From A Session Object
7.Sh SYNOPSIS
8.Ft int
9.Fo sqlite3session_changeset
10.Fa "sqlite3_session *pSession"
11.Fa "int *pnChangeset"
12.Fa "void **ppChangeset              "
13.Fc
14.Sh DESCRIPTION
15Obtain a changeset containing changes to the tables attached to the
16session object passed as the first argument.
17If successful, set *ppChangeset to point to a buffer containing the
18changeset and *pnChangeset to the size of the changeset in bytes before
19returning SQLITE_OK.
20If an error occurs, set both *ppChangeset and *pnChangeset to zero
21and return an SQLite error code.
22.Pp
23A changeset consists of zero or more INSERT, UPDATE and/or DELETE changes,
24each representing a change to a single row of an attached table.
25An INSERT change contains the values of each field of a new database
26row.
27A DELETE contains the original values of each field of a deleted database
28row.
29An UPDATE change contains the original values of each field of an updated
30database row along with the updated values for each updated non-primary-key
31column.
32It is not possible for an UPDATE change to represent a change that
33modifies the values of primary key columns.
34If such a change is made, it is represented in a changeset as a DELETE
35followed by an INSERT.
36.Pp
37Changes are not recorded for rows that have NULL values stored in one
38or more of their PRIMARY KEY columns.
39If such a row is inserted or deleted, no corresponding change is present
40in the changesets returned by this function.
41If an existing row with one or more NULL values stored in PRIMARY KEY
42columns is updated so that all PRIMARY KEY columns are non-NULL, only
43an INSERT is appears in the changeset.
44Similarly, if an existing row with non-NULL PRIMARY KEY values is updated
45so that one or more of its PRIMARY KEY columns are set to NULL, the
46resulting changeset contains a DELETE change only.
47.Pp
48The contents of a changeset may be traversed using an iterator created
49using the sqlite3changeset_start() API.
50A changeset may be applied to a database with a compatible schema using
51the sqlite3changeset_apply() API.
52.Pp
53Within a changeset generated by this function, all changes related
54to a single table are grouped together.
55In other words, when iterating through a changeset or when applying
56a changeset to a database, all changes related to a single table are
57processed before moving on to the next table.
58Tables are sorted in the same order in which they were attached (or
59auto-attached) to the sqlite3_session object.
60The order in which the changes related to a single table are stored
61is undefined.
62.Pp
63Following a successful call to this function, it is the responsibility
64of the caller to eventually free the buffer that *ppChangeset points
65to using sqlite3_free().
66.Ss Changeset Generation
67Once a table has been attached to a session object, the session object
68records the primary key values of all new rows inserted into the table.
69It also records the original primary key and other column values of
70any deleted or updated rows.
71For each unique primary key value, data is only recorded once - the
72first time a row with said primary key is inserted, updated or deleted
73in the lifetime of the session.
74.Pp
75There is one exception to the previous paragraph: when a row is inserted,
76updated or deleted, if one or more of its primary key columns contain
77a NULL value, no record of the change is made.
78.Pp
79The session object therefore accumulates two types of records - those
80that consist of primary key values only (created when the user inserts
81a new record) and those that consist of the primary key values and
82the original values of other table columns (created when the users
83deletes or updates a record).
84.Pp
85When this function is called, the requested changeset is created using
86both the accumulated records and the current contents of the database
87file.
88Specifically:
89.Bl -bullet
90.It
91For each record generated by an insert, the database is queried for
92a row with a matching primary key.
93If one is found, an INSERT change is added to the changeset.
94If no such row is found, no change is added to the changeset.
95.It
96For each record generated by an update or delete, the database is queried
97for a row with a matching primary key.
98If such a row is found and one or more of the non-primary key fields
99have been modified from their original values, an UPDATE change is
100added to the changeset.
101Or, if no such row is found in the table, a DELETE change is added
102to the changeset.
103If there is a row with a matching primary key in the database, but
104all fields contain their original values, no change is added to the
105changeset.
106.El
107.Pp
108This means, amongst other things, that if a row is inserted and then
109later deleted while a session object is active, neither the insert
110nor the delete will be present in the changeset.
111Or if a row is deleted and then later a row with the same primary key
112values inserted while a session object is active, the resulting changeset
113will contain an UPDATE change instead of a DELETE and an INSERT.
114.Pp
115When a session object is disabled (see the sqlite3session_enable()
116API), it does not accumulate records when rows are inserted, updated
117or deleted.
118This may appear to have some counter-intuitive effects if a single
119row is written to more than once during a session.
120For example, if a row is inserted while a session object is enabled,
121then later deleted while the same session object is disabled, no INSERT
122record will appear in the changeset, even though the delete took place
123while the session was disabled.
124Or, if one field of a row is updated while a session is disabled, and
125another field of the same row is updated while the session is enabled,
126the resulting changeset will contain an UPDATE change that updates
127both fields.
128.Sh SEE ALSO
129.Xr sqlite3_malloc 3 ,
130.Xr sqlite3changeset_apply 3 ,
131.Xr sqlite3changeset_start 3 ,
132.Xr sqlite3session_enable 3
133