Debian Codegraph @

www/sessionintro.html

raw
1 <!DOCTYPE html>
2 <html><head>
3 <meta name="viewport" content="width=device-width, initial-scale=1.0">
4 <meta http-equiv="content-type" content="text/html; charset=UTF-8">
5 <link href="sqlite.css" rel="stylesheet">
6 <title>The Session Extension</title>
7 <!-- path= -->
8 </head>
9 <body>
10 <div class=nosearch>
11 <a href="index.html">
12 <img class="logo" src="images/sqlite370_banner.gif" alt="SQLite" border="0">
13 </a>
14 <div><!-- IE hack to prevent disappearing logo --></div>
15 <div class="tagline desktoponly">
16 Small. Fast. Reliable.<br>Choose any three.
17 </div>
18 <div class="menu mainmenu">
19 <ul>
20 <li><a href="index.html">Home</a>
21 <li class='mobileonly'><a href="javascript:void(0)" onclick='toggle_div("submenu")'>Menu</a>
22 <li class='wideonly'><a href='about.html'>About</a>
23 <li class='desktoponly'><a href="docs.html">Documentation</a>
24 <li class='desktoponly'><a href="download.html">Download</a>
25 <li class='wideonly'><a href='copyright.html'>License</a>
26 <li class='desktoponly'><a href="support.html">Support</a>
27 <li class='desktoponly'><a href="prosupport.html">Purchase</a>
28 <li class='search' id='search_menubutton'>
29 <a href="javascript:void(0)" onclick='toggle_search()'>Search</a>
30 </ul>
31 </div>
32 <div class="menu submenu" id="submenu">
33 <ul>
34 <li><a href='about.html'>About</a>
35 <li><a href='docs.html'>Documentation</a>
36 <li><a href='download.html'>Download</a>
37 <li><a href='support.html'>Support</a>
38 <li><a href='prosupport.html'>Purchase</a>
39 </ul>
40 </div>
41 <div class="searchmenu" id="searchmenu">
42 <form method="GET" action="search">
43 <select name="s" id="searchtype">
44 <option value="d">Search Documentation</option>
45 <option value="c">Search Changelog</option>
46 </select>
47 <input type="text" name="q" id="searchbox" value="">
48 <input type="submit" value="Go">
49 </form>
50 </div>
51 </div>
52 <script>
53 function toggle_div(nm) {
54 var w = document.getElementById(nm);
55 if( w.style.display=="block" ){
56 w.style.display = "none";
57 }else{
58 w.style.display = "block";
59 }
60 }
61 function toggle_search() {
62 var w = document.getElementById("searchmenu");
63 if( w.style.display=="block" ){
64 w.style.display = "none";
65 } else {
66 w.style.display = "block";
67 setTimeout(function(){
68 document.getElementById("searchbox").focus()
69 }, 30);
70 }
71 }
72 function div_off(nm){document.getElementById(nm).style.display="none";}
73 window.onbeforeunload = function(e){div_off("submenu");}
74 /* Disable the Search feature if we are not operating from CGI, since */
75 /* Search is accomplished using CGI and will not work without it. */
76 if( !location.origin || !location.origin.match || !location.origin.match(/http/) ){
77 document.getElementById("search_menubutton").style.display = "none";
78 }
79 /* Used by the Hide/Show button beside syntax diagrams, to toggle the */
80 function hideorshow(btn,obj){
81 var x = document.getElementById(obj);
82 var b = document.getElementById(btn);
83 if( x.style.display!='none' ){
84 x.style.display = 'none';
85 b.innerHTML='show';
86 }else{
87 x.style.display = '';
88 b.innerHTML='hide';
89 }
90 return false;
91 }
92 var antiRobot = 0;
93 function antiRobotGo(){
94 if( antiRobot!=3 ) return;
95 antiRobot = 7;
96 var j = document.getElementById("mtimelink");
97 if(j && j.hasAttribute("data-href")) j.href=j.getAttribute("data-href");
98 }
99 function antiRobotDefense(){
100 document.body.onmousedown=function(){
101 antiRobot |= 2;
102 antiRobotGo();
103 document.body.onmousedown=null;
104 }
105 document.body.onmousemove=function(){
106 antiRobot |= 2;
107 antiRobotGo();
108 document.body.onmousemove=null;
109 }
110 setTimeout(function(){
111 antiRobot |= 1;
112 antiRobotGo();
113 }, 100)
114 antiRobotGo();
115 }
116 antiRobotDefense();
117 </script>
118 <div class=fancy>
119 <div class=nosearch>
120 <div class="fancy_title">
121 The Session Extension
122 </div>
123 <div class="fancy_toc">
124 <a onclick="toggle_toc()">
125 <span class="fancy_toc_mark" id="toc_mk">&#x25ba;</span>
126 Table Of Contents
127 </a>
128 <div id="toc_sub"><div class="fancy-toc1"><a href="#introduction">1. Introduction</a></div>
129 <div class="fancy-toc2"><a href="#typical_use_case">1.1. Typical Use Case</a></div>
130 <div class="fancy-toc2"><a href="#obtaining_the_session_extension">1.2. Obtaining the Session Extension</a></div>
131 <div class="fancy-toc2"><a href="#limitations">1.3. Limitations</a></div>
132 <div class="fancy-toc1"><a href="#concepts">2. Concepts</a></div>
133 <div class="fancy-toc2"><a href="#changesets_and_patchsets">2.1. Changesets and Patchsets</a></div>
134 <div class="fancy-toc2"><a href="#conflicts">2.2. Conflicts</a></div>
135 <div class="fancy-toc2"><a href="#changeset_construction">2.3. Changeset Construction</a></div>
136 <div class="fancy-toc1"><a href="#using_the_session_extension">3. Using The Session Extension</a></div>
137 <div class="fancy-toc2"><a href="#capturing_a_changeset">3.1. Capturing a Changeset</a></div>
138 <div class="fancy-toc2"><a href="#applying_a_changeset_to_a_database">3.2. Applying a Changeset to a Database</a></div>
139 <div class="fancy-toc2"><a href="#inspecting_the_contents_of_a_changeset">3.3. Inspecting the Contents of a Changeset</a></div>
140 <div class="fancy-toc1"><a href="#extended_functionality">4. Extended Functionality</a></div>
141 </div>
142 </div>
143 <script>
144 function toggle_toc(){
145 var sub = document.getElementById("toc_sub")
146 var mk = document.getElementById("toc_mk")
147 if( sub.style.display!="block" ){
148 sub.style.display = "block";
149 mk.innerHTML = "&#x25bc;";
150 } else {
151 sub.style.display = "none";
152 mk.innerHTML = "&#x25ba;";
153 }
154 }
155 </script>
156 </div>
157
158
159
160
161
162 <h1 id="introduction"><span>1. </span>Introduction</h1>
163
164 <p>The session extension provide a mechanism for conviently recording
165 changes to some or all of certain tables in an SQLite database, and
166 packaging those changes into a "changeset" or "patchset" file that can
167 later be used to apply the same set of changes to another database with
168 the same schema and compatible starting data. A "changeset" may
169 also be inverted and used to "undo" a session.
170
171 </p><p>This document is an introduction to the session extension.
172 The details of the interface are in the separate
173 <a href="session/intro.html">Session Extension C-language Interface</a> document.
174
175 </p><h2 id="typical_use_case"><span>1.1. </span>Typical Use Case</h2>
176
177 <p>Suppose SQLite is used as the <a href="appfileformat.html">application file format</a> for a
178 particular design application. Two users, Alice and Bob, each start
179 with a baseline design that is about a gigabyte in size. They work
180 all day, in parallel, each making their own customizations and tweaks
181 to the design. At the end of the day, they would like to merge their
182 changes together into a single unified design.
183
184 </p><p>The session extension facilitates this by recording all changes to
185 both Alice's and Bob's databases and writing those changes into
186 changeset or patchset files. At the end of the day, Alice can send her
187 changeset to Bob and Bob can "apply" it to his database. The result (assuming
188 there are no conflicts) is that Bob's database then contains both his
189 changes and Alice's changes. Likewise, Bob can send a changeset of
190 his work over to Alice and she can apply his changes to her database.
191
192 </p><p>In other words, the session extension provides a facility for
193 SQLite database files that is similar to the unix
194 <a href="https://en.wikipedia.org/wiki/Patch_(Unix)">patch</a> utility program,
195 or to the "merge" capabilities of version control systems such
196 as <a href="https://www.fossil-scm.org/">Fossil</a>, <a href="https://git-scm.com">Git</a>,
197 or <a href="http://www.mercurial-scm.org/">Mercurial</a>.
198
199 </p><h2 id="obtaining_the_session_extension"><span>1.2. </span>Obtaining the Session Extension</h2>
200
201 <p> Since <a href="releaselog/3_13_0.html">version 3.13.0</a> (2016-05-18),
202 the session extension has been included in the SQLite
203 <a href="amalgamation.html">amalgamation</a> source distribution. By default, the session extension is
204 disabled. To enable it, build with the following compiler switches:
205
206 </p><div class="codeblock"><pre>-DSQLITE_ENABLE_SESSION -DSQLITE_ENABLE_PREUPDATE_HOOK
207 </pre></div>
208
209 <p> Or, if using the autoconf build system,
210 pass the --enable-session option to the configure script.
211
212 </p><h2 id="limitations"><span>1.3. </span>Limitations</h2>
213
214 <ul>
215
216 <li><p> Prior to SQLite version 3.17.0, the session extension only worked with
217 <a href="rowidtable.html">rowid tables</a>, not <a href="withoutrowid.html">WITHOUT ROWID</a> tables. As of 3.17.0, both
218 rowid and WITHOUT ROWID tables are supported. However, extra steps are
219 needed to record primary keys for WITHOUT ROWID table changes.
220
221 </p></li><li><p> There is no support for <a href="vtab.html">virtual tables</a>. Changes to virtual tables are
222 not captured.
223
224 </p></li><li><p> The session extension only works with tables that have a declared
225 PRIMARY KEY. The PRIMARY KEY of a table may be an INTEGER PRIMARY KEY
226 (rowid alias) or an external PRIMARY KEY.
227
228 </p></li><li><p> SQLite allows <a href="nulls.html">NULL values</a> to be stored in
229 PRIMARY KEY columns. However, the session extension ignores all
230 such rows. No changes affecting rows with one or more NULL values
231 in PRIMARY KEY columns are recorded by the sessions module.
232 </p></li></ul>
233
234 <h1 id="concepts"><span>2. </span>Concepts</h1>
235
236 <a name="changeset"></a>
237
238 <h2 id="changesets_and_patchsets"><span>2.1. </span>Changesets and Patchsets</h2>
239 <p> The sessions module revolves around creating and manipulating
240 changesets. A changeset is a blob of data that encodes a series of
241 changes to a database. Each change in a changeset is one of the
242 following:
243
244 </p><ul>
245 <li> <p>An <b>INSERT</b>. An INSERT change contains a single row to add to
246 a database table. The payload of the INSERT change consists of the
247 values for each field of the new row.
248
249 </p></li><li> <p>A <b>DELETE</b>. A DELETE change represents a row, identified by
250 its primary key values, to remove from a database table. The payload
251 of a DELETE change consists of the values for all fields of the
252 deleted row.
253
254 </p></li><li> <p>An <b>UPDATE</b>. An UPDATE change represents the modification of
255 one or more non-PRIMARY KEY fields of a single row within a database
256 table, identified by its PRIMARY KEY fields. The payload for an UPDATE
257 change consists of:
258 </p><ul>
259 <li> The PRIMARY KEY values identifying the modified row,
260 </li><li> The new values for each modified field of the row, and
261 </li><li> The original values for each modified field of the row.
262 </li></ul>
263 <p> An UPDATE change does not contain any information regarding
264 non-PRIMARY KEY fields that are not modified by the change. It is not
265 possible for an UPDATE change to specify modifications to PRIMARY
266 KEY fields.
267 </p></li></ul>
268
269 <p> A single changeset may contain changes that apply to more than one
270 database table. For each table that the changeset includes at least one change
271 for, it also encodes the following data:
272
273 </p><ul>
274 <li> The name of the database table,
275 </li><li> The number of columns the table has, and
276 </li><li> Which of those columns are PRIMARY KEY columns.
277 </li></ul>
278
279 <p> Changesets may only be applied to databases that contain tables
280 matching the above three criteria as stored in the changeset.
281
282 </p><p> A patchset is similar to a changeset. It is slightly more compact than
283 a changeset, but provides more limited conflict detection and resolution
284 options (see the next section for details). The differences between a
285 patchset and a changeset are that:
286
287 </p><ul>
288 <li><p> For a <b>DELETE</b> change, the payload consists of the PRIMARY KEY
289 fields only. The original values of other fields are not stored as
290 part of a patchset.
291
292 </p></li><li><p> For an <b>UPDATE</b> change, the payload consists of the PRIMARY KEY
293 fields and the new values of modified fields only. The original
294 values of modified fields are not stored as part of a patchset.
295 </p></li></ul>
296
297 <h2 id="conflicts"><span>2.2. </span>Conflicts</h2>
298
299 <p> When a changeset or patchset is applied to a database, an attempt is
300 made to insert a new row for each INSERT change, remove a row for each
301 DELETE change and modify a row for each UPDATE change. If the target
302 database is in the same state as the original database that the changeset
303 was recorded on, this is a simple matter. However, if the contents of the
304 target database is not in exactly this state, conflicts can occur when
305 applying the changeset or patchset.
306
307 </p><p>When processing an <b>INSERT</b> change, the following conflicts can
308 occur:
309
310 </p><ul>
311 <li> The target database may already contain a row with the same PRIMARY
312 KEY values as specified by the INSERT change.
313
314 </li><li> Some other database constraint, for example a UNIQUE or CHECK
315 constraint, may be violated when the new row is inserted.
316 </li></ul>
317
318 <p>When processing a <b>DELETE</b> change, the following conflicts may be
319 detected:
320
321 </p><ul>
322 <li> The target database may contain no row with the specified PRIMARY
323 KEY values to delete.
324
325 </li><li> The target database may contain a row with the specified PRIMARY
326 KEY values, but the other fields may contain values that do not
327 match those stored as part of the changeset. This type of conflict
328 is not detected when using a patchset.
329 </li></ul>
330
331 <p>When processing an <b>UPDATE</b> change, the following conflicts may be
332 detected:
333
334 </p><ul>
335 <li> The target database may contain no row with the specified PRIMARY
336 KEY values to modify.
337
338 </li><li> The target database may contain a row with the specified PRIMARY
339 KEY values, but the current values of the fields that will be modified
340 by the change may not match the original values stored within the
341 changeset. This type of conflict is not detected when using a patchset.
342
343 </li><li> Some other database constraint, for example a UNIQUE or CHECK
344 constraint, may be violated when the row is updated.
345 </li></ul>
346
347 <p> Depending on the type of conflict, a sessions application has a variety
348 of configurable options for dealing with conflicts, ranging from omitting the
349 conflicting change, aborting the entire changeset application or applying
350 the change despite the conflict. For details, refer to the documentation for
351 the <a href="session/sqlite3changeset_apply.html">sqlite3changeset_apply()</a> API.
352
353 </p><h2 id="changeset_construction"><span>2.3. </span>Changeset Construction</h2>
354
355 <p> After a session object has been configured, it begins monitoring for
356 changes to its configured tables. However, it does not record an entire
357 change each time a row within the database is modified. Instead, it records
358 just the PRIMARY KEY fields for each inserted row, and just the PRIMARY KEY
359 and all original row values for any updated or deleted rows. If a row is
360 modified more than once by a single session, no new information is recorded.
361
362 </p><p> The other information required to create a changeset or patchset is
363 read from the database file when <a href="session/sqlite3session_changeset.html">sqlite3session_changeset()</a> or
364 <a href="session/sqlite3session_patchset.html">sqlite3session_patchset()</a> is called. Specifically,
365
366 </p><ul>
367 <li> <p>For each primary key recorded as a result of an INSERT operation,
368 the sessions module checks if there is a row with a matching primary
369 key still in the table. If so, an INSERT change is added to the
370 changeset.
371
372 </p></li><li> <p>For each primary key recorded as a result of an UPDATE or DELETE
373 operation, the sessions module also checks for a row with a matching
374 primary key within the table. If one can be found, but one or more
375 of the non-PRIMARY KEY fields does not match the original recorded
376 value, an UPDATE is added to the changeset. Or, if there is no row
377 at all with the specified primary key, a DELETE is added to the
378 changeset. If the row does exist but none of the non-PRIMARY KEY
379 fields have been modified, no change is added to the changeset.
380 </p></li></ul>
381
382 <p> One implication of the above is that if a change is made and then
383 unmade within a single session (for example if a row is inserted and then
384 deleted again), the sessions module does not report any change at all. Or
385 if a row is updated multiple times within the same session, all updates
386 are coalesced into a single update within any changeset or patchset blob.
387
388 </p><h1 id="using_the_session_extension"><span>3. </span>Using The Session Extension</h1>
389
390 <p> This section provides examples that demonstrate how to use the sessions
391 extension.
392
393 </p><h2 id="capturing_a_changeset"><span>3.1. </span>Capturing a Changeset</h2>
394
395 <p> The example code below demonstrates the steps involved in capturing a
396 changeset while executing SQL commands. In summary:
397
398 </p><ol>
399 <li> <p>A session object (type sqlite3_session*) is created by making a
400 call to the <a href="session/sqlite3session_create.html">sqlite3session_create()</a> API function.
401
402 </p><p>A single session object monitors changes made to a single database
403 (i.e. "main", "temp" or an attached database) via a single
404 sqlite3* database handle.
405
406 </p></li><li> <p>The session object is configured with a set of tables to monitor
407 changes on.
408
409 </p><p> By default a session object does not monitor changes on any
410 database table. Before it does so it must be configured. There
411 are three ways to configure the set of tables to monitor changes
412 on:
413 </p><ul>
414 <li> By explicitly specifying tables using one call to
415 <a href="session/sqlite3session_attach.html">sqlite3session_attach()</a> for each table, or
416
417 </li><li> By specifying that all tables in the database should be monitored
418 for changes using a call to <a href="session/sqlite3session_attach.html">sqlite3session_attach()</a> with a
419 NULL argument, or
420
421 </li><li> By configuring a callback to be invoked the first time each table
422 is written to that indicates to the session module whether or
423 not changes on the table should be monitored.
424 </li></ul>
425 <p> The example code below uses the second of the methods enumerated
426 above - it monitors for changes on all database tables.
427
428 </p></li><li> <p> Changes are made to the database by executing SQL statements. The
429 session object records these changes.
430
431 </p></li><li> <p> A changeset blob is extracted from the session object using a call
432 to <a href="session/sqlite3session_changeset.html">sqlite3session_changeset()</a> (or, if using patchsets, a call to
433 the <a href="session/sqlite3session_patchset.html">sqlite3session_patchset()</a> function).
434
435 </p></li><li> <p> The session object is deleted using a call to the
436 <a href="session/sqlite3session_delete.html">sqlite3session_delete()</a> API function.
437
438 </p><p> It is not necessary to delete a session object after extracting
439 a changeset or patchset from it. It can be left attached to the
440 database handle and will continue monitoring for changes on the
441 configured tables as before. However, if
442 <a href="session/sqlite3session_changeset.html">sqlite3session_changeset()</a> or <a href="session/sqlite3session_patchset.html">sqlite3session_patchset()</a> is
443 called a second time on a session object, the changeset or patchset
444 will contain <em>all</em> changes that have taken place on the connection
445 since the session was created. In other words,
446 a session object is not reset or
447 zeroed by a call to sqlite3session_changeset() or
448 sqlite3session_patchset().
449 </p></li></ol>
450
451 <div class="codeblock"><pre><i>/*
452 ** Argument zSql points to a buffer containing an SQL script to execute
453 ** against the database handle passed as the first argument. As well as
454 ** executing the SQL script, this function collects a changeset recording
455 ** all changes made to the "main" database file. Assuming no error occurs,
456 ** output variables (*ppChangeset) and (*pnChangeset) are set to point
457 ** to a buffer containing the changeset and the size of the changeset in
458 ** bytes before returning SQLITE_OK. In this case it is the responsibility
459 ** of the caller to eventually free the changeset blob by passing it to
460 ** the sqlite3_free function.
461 **
462 ** Or, if an error does occur, return an SQLite error code. The final
463 ** value of (*pChangeset) and (*pnChangeset) are undefined in this case.
464 */</i>
465 int sql_exec_changeset(
466 sqlite3 *db, <i>/* Database handle */</i>
467 const char *zSql, <i>/* SQL script to execute */</i>
468 int *pnChangeset, <i>/* OUT: Size of changeset blob in bytes */</i>
469 void **ppChangeset <i>/* OUT: Pointer to changeset blob */</i>
470 ){
471 sqlite3_session *pSession = 0;
472 int rc;
473
474 <i>/* Create a new session object */</i>
475 rc = sqlite3session_create(db, "main", &pSession);
476
477 <i>/* Configure the session object to record changes to all tables */</i>
478 if( rc==SQLITE_OK ) rc = sqlite3session_attach(pSession, NULL);
479
480 <i>/* Execute the SQL script */</i>
481 if( rc==SQLITE_OK ) rc = sqlite3_exec(db, zSql, 0, 0, 0);
482
483 <i>/* Collect the changeset */</i>
484 if( rc==SQLITE_OK ){
485 rc = sqlite3session_changeset(pSession, pnChangeset, ppChangeset);
486 }
487
488 <i>/* Delete the session object */</i>
489 sqlite3session_delete(pSession);
490
491 return rc;
492 }
493 </pre></div>
494
495 <h2 id="applying_a_changeset_to_a_database"><span>3.2. </span>Applying a Changeset to a Database</h2>
496
497 <p> Applying a changeset to a database is simpler than capturing a changeset.
498 Usually, a single call to <a href="session/sqlite3changeset_apply.html">sqlite3changeset_apply()</a>, as depicted in the
499 example code below, suffices.
500
501 </p><p> In cases where it is complicated, the complications in applying a
502 changeset lie in conflict resolution. Refer to the API documentation linked
503 above for details.
504
505 </p><div class="codeblock"><pre><i>/*
506 ** Conflict handler callback used by apply_changeset(). See below.
507 */</i>
508 static int xConflict(void *pCtx, int eConflict, sqlite3_changeset_iter *pIter){
509 int ret = (int)pCtx;
510 return ret;
511 }
512
513 <i>/*
514 ** Apply the changeset contained in blob pChangeset, size nChangeset bytes,
515 ** to the main database of the database handle passed as the first argument.
516 ** Return SQLITE_OK if successful, or an SQLite error code if an error
517 ** occurs.
518 **
519 ** If parameter bIgnoreConflicts is true, then any conflicting changes
520 ** within the changeset are simply ignored. Or, if bIgnoreConflicts is
521 ** false, then this call fails with an SQLITE_ABORT error if a changeset
522 ** conflict is encountered.
523 */</i>
524 int apply_changeset(
525 sqlite3 *db, <i>/* Database handle */</i>
526 int bIgnoreConflicts, <i>/* True to ignore conflicting changes */</i>
527 int nChangeset, <i>/* Size of changeset in bytes */</i>
528 void *pChangeset <i>/* Pointer to changeset blob */</i>
529 ){
530 return sqlite3changeset_apply(
531 db,
532 nChangeset, pChangeset,
533 0, xConflict,
534 (void*)bIgnoreConflicts
535 );
536 }
537 </pre></div>
538
539 <h2 id="inspecting_the_contents_of_a_changeset"><span>3.3. </span>Inspecting the Contents of a Changeset</h2>
540
541 <p> The example code below demonstrates the techniques used to iterate
542 through and extract the data related to all changes in a changeset. To
543 summarize:
544
545 </p><ol>
546 <li><p> The <a href="session/sqlite3changeset_start.html">sqlite3changeset_start()</a> API is called to create and
547 initialize an iterator to iterate through the contents of a
548 changeset. Initially, the iterator points to no element at all.
549
550 </p></li><li><p> The first call to <a href="session/sqlite3changeset_next.html">sqlite3changeset_next()</a> on the iterator moves
551 it to point to the first change in the changeset (or to EOF, if
552 the changeset is completely empty). sqlite3changeset_next() returns
553 SQLITE_ROW if it moves the iterator to point to a valid entry,
554 SQLITE_DONE if it moves the iterator to EOF, or an SQLite error
555 code if an error occurs.
556
557 </p></li><li><p> If the iterator points to a valid entry, the <a href="session/sqlite3changeset_op.html">sqlite3changeset_op()</a>
558 API may be used to determine the type of change (INSERT, UPDATE or
559 DELETE) that the iterator points to. Additionally, the same API
560 can be used to obtain the name of the table the change applies to
561 and its expected number of columns and primary key columns.
562
563 </p></li><li><p> If the iterator points to a valid INSERT or UPDATE entry, the
564 <a href="session/sqlite3changeset_new.html">sqlite3changeset_new()</a> API may be used to obtain the new.* values
565 within the change payload.
566
567 </p></li><li><p> If the iterator points to a valid DELETE or UPDATE entry, the
568 <a href="session/sqlite3changeset_old.html">sqlite3changeset_old()</a> API may be used to obtain the old.* values
569 within the change payload.
570
571 </p></li><li><p> An iterator is deleted using a call to the
572 <a href="session/sqlite3changeset_finalize.html">sqlite3changeset_finalize()</a> API. If an error occured while
573 iterating, an SQLite error code is returned (even if the same error
574 code has already been returned by sqlite3changeset_next()). Or,
575 if no error has occurred, SQLITE_OK is returned.
576 </p></li></ol>
577
578 <div class="codeblock"><pre><i>/*
579 ** Print the contents of the changeset to stdout.
580 */</i>
581 static int print_changeset(void *pChangeset, int nChangeset){
582 int rc;
583 sqlite3_changeset_iter *pIter = 0;
584
585 <i>/* Create an iterator to iterate through the changeset */</i>
586 rc = sqlite3changeset_start(&pIter, nChangeset, pChangeset);
587 if( rc!=SQLITE_OK ) return rc;
588
589 <i>/* This loop runs once for each change in the changeset */</i>
590 while( SQLITE_ROW==sqlite3changeset_next(pIter) ){
591 const char *zTab; <i>/* Table change applies to */</i>
592 int nCol; <i>/* Number of columns in table zTab */</i>
593 int op; <i>/* SQLITE_INSERT, UPDATE or DELETE */</i>
594 sqlite3_value *pVal;
595
596 <i>/* Print the type of operation and the table it is on */</i>
597 rc = sqlite3changeset_op(pIter, &zTab, &nCol, &op, 0);
598 if( rc!=SQLITE_OK ) goto exit_print_changeset;
599 printf("%s on table %s\n",
600 op==SQLITE_INSERT?"INSERT" : op==SQLITE_UPDATE?"UPDATE" : "DELETE",
601 zTab
602 );
603
604 <i>/* If this is an UPDATE or DELETE, print the old.* values */</i>
605 if( op==SQLITE_UPDATE || op==SQLITE_DELETE ){
606 printf("Old values:");
607 for(i=0; i&lt;nCol; i++){
608 rc = sqlite3changeset_old(pIter, i, &pVal);
609 if( rc!=SQLITE_OK ) goto exit_print_changeset;
610 printf(" %s", pVal ? sqlite3_value_text(pVal) : "-");
611 }
612 printf("\n");
613 }
614
615 <i>/* If this is an UPDATE or INSERT, print the new.* values */</i>
616 if( op==SQLITE_UPDATE || op==SQLITE_INSERT ){
617 printf("New values:");
618 for(i=0; i&lt;nCol; i++){
619 rc = sqlite3changeset_new(pIter, i, &pVal);
620 if( rc!=SQLITE_OK ) goto exit_print_changeset;
621 printf(" %s", pVal ? sqlite3_value_text(pVal) : "-");
622 }
623 printf("\n");
624 }
625 }
626
627 <i>/* Clean up the changeset and return an error code (or SQLITE_OK) */</i>
628 exit_print_changeset:
629 rc2 = sqlite3changeset_finalize(pIter);
630 if( rc==SQLITE_OK ) rc = rc2;
631 return rc;
632 }
633 </pre></div>
634
635 <h1 id="extended_functionality"><span>4. </span>Extended Functionality</h1>
636
637 <p> Most applications will only use the session module functionality described
638 in the previous section. However, the following additional functionality is
639 available for the use and manipulation of changeset and patchset blobs:
640
641 </p><ul>
642 <li> <p>Two or more changeset/patchsets may be combined using the
643 <a href="session/sqlite3changeset_concat.html">sqlite3changeset_concat()</a> or <a href="session/changegroup.html">sqlite3_changegroup</a> interfaces.
644
645 </p></li><li> <p>A changeset may be "inverted" using the <a href="session/sqlite3changeset_invert.html">sqlite3changeset_invert()</a>
646 API function. An inverted changeset undoes the changes made by the
647 original. If changeset C<sup>+</sup> is the inverse of changeset C, then
648 applying C and then C<sup>+</sup> to a database should leave
649 the database unchanged.
650 </p></li></ul><p align="center"><small><i>This page last modified on <a href="https://sqlite.org/docsrc/honeypot" id="mtimelink" data-href="https://sqlite.org/docsrc/finfo/pages/sessionintro.in?m=20ce2b0e76">2024-06-01 14:57:22</a> UTC </small></i></p>
651