sqlite3session.h 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829
  1. #if !defined(__SQLITESESSION_H_) && defined(SQLITE_ENABLE_SESSION)
  2. #define __SQLITESESSION_H_ 1
  3. /*
  4. ** Make sure we can call this stuff from C++.
  5. */
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9. #include "sqlite3.h"
  10. /*
  11. ** CAPI3REF: Session Object Handle
  12. **
  13. ** An instance of this object is a [session] that can be used to
  14. ** record changes to a database.
  15. */
  16. typedef struct sqlite3_session sqlite3_session;
  17. /*
  18. ** CAPI3REF: Changeset Iterator Handle
  19. **
  20. ** An instance of this object acts as a cursor for iterating
  21. ** over the elements of a [changeset] or [patchset].
  22. */
  23. typedef struct sqlite3_changeset_iter sqlite3_changeset_iter;
  24. /*
  25. ** CAPI3REF: Create A New Session Object
  26. ** CONSTRUCTOR: sqlite3_session
  27. **
  28. ** Create a new session object attached to database handle db. If successful,
  29. ** a pointer to the new object is written to *ppSession and SQLITE_OK is
  30. ** returned. If an error occurs, *ppSession is set to NULL and an SQLite
  31. ** error code (e.g. SQLITE_NOMEM) is returned.
  32. **
  33. ** It is possible to create multiple session objects attached to a single
  34. ** database handle.
  35. **
  36. ** Session objects created using this function should be deleted using the
  37. ** [sqlite3session_delete()] function before the database handle that they
  38. ** are attached to is itself closed. If the database handle is closed before
  39. ** the session object is deleted, then the results of calling any session
  40. ** module function, including [sqlite3session_delete()] on the session object
  41. ** are undefined.
  42. **
  43. ** Because the session module uses the [sqlite3_preupdate_hook()] API, it
  44. ** is not possible for an application to register a pre-update hook on a
  45. ** database handle that has one or more session objects attached. Nor is
  46. ** it possible to create a session object attached to a database handle for
  47. ** which a pre-update hook is already defined. The results of attempting
  48. ** either of these things are undefined.
  49. **
  50. ** The session object will be used to create changesets for tables in
  51. ** database zDb, where zDb is either "main", or "temp", or the name of an
  52. ** attached database. It is not an error if database zDb is not attached
  53. ** to the database when the session object is created.
  54. */
  55. int sqlite3session_create(
  56. sqlite3 *db, /* Database handle */
  57. const char *zDb, /* Name of db (e.g. "main") */
  58. sqlite3_session **ppSession /* OUT: New session object */
  59. );
  60. /*
  61. ** CAPI3REF: Delete A Session Object
  62. ** DESTRUCTOR: sqlite3_session
  63. **
  64. ** Delete a session object previously allocated using
  65. ** [sqlite3session_create()]. Once a session object has been deleted, the
  66. ** results of attempting to use pSession with any other session module
  67. ** function are undefined.
  68. **
  69. ** Session objects must be deleted before the database handle to which they
  70. ** are attached is closed. Refer to the documentation for
  71. ** [sqlite3session_create()] for details.
  72. */
  73. void sqlite3session_delete(sqlite3_session *pSession);
  74. /*
  75. ** CAPI3REF: Configure a Session Object
  76. ** METHOD: sqlite3_session
  77. **
  78. ** This method is used to configure a session object after it has been
  79. ** created. At present the only valid values for the second parameter are
  80. ** [SQLITE_SESSION_OBJCONFIG_SIZE] and [SQLITE_SESSION_OBJCONFIG_ROWID].
  81. **
  82. */
  83. int sqlite3session_object_config(sqlite3_session*, int op, void *pArg);
  84. /*
  85. ** CAPI3REF: Options for sqlite3session_object_config
  86. **
  87. ** The following values may passed as the the 2nd parameter to
  88. ** sqlite3session_object_config().
  89. **
  90. ** <dt>SQLITE_SESSION_OBJCONFIG_SIZE <dd>
  91. ** This option is used to set, clear or query the flag that enables
  92. ** the [sqlite3session_changeset_size()] API. Because it imposes some
  93. ** computational overhead, this API is disabled by default. Argument
  94. ** pArg must point to a value of type (int). If the value is initially
  95. ** 0, then the sqlite3session_changeset_size() API is disabled. If it
  96. ** is greater than 0, then the same API is enabled. Or, if the initial
  97. ** value is less than zero, no change is made. In all cases the (int)
  98. ** variable is set to 1 if the sqlite3session_changeset_size() API is
  99. ** enabled following the current call, or 0 otherwise.
  100. **
  101. ** It is an error (SQLITE_MISUSE) to attempt to modify this setting after
  102. ** the first table has been attached to the session object.
  103. **
  104. ** <dt>SQLITE_SESSION_OBJCONFIG_ROWID <dd>
  105. ** This option is used to set, clear or query the flag that enables
  106. ** collection of data for tables with no explicit PRIMARY KEY.
  107. **
  108. ** Normally, tables with no explicit PRIMARY KEY are simply ignored
  109. ** by the sessions module. However, if this flag is set, it behaves
  110. ** as if such tables have a column "_rowid_ INTEGER PRIMARY KEY" inserted
  111. ** as their leftmost columns.
  112. **
  113. ** It is an error (SQLITE_MISUSE) to attempt to modify this setting after
  114. ** the first table has been attached to the session object.
  115. */
  116. #define SQLITE_SESSION_OBJCONFIG_SIZE 1
  117. #define SQLITE_SESSION_OBJCONFIG_ROWID 2
  118. /*
  119. ** CAPI3REF: Enable Or Disable A Session Object
  120. ** METHOD: sqlite3_session
  121. **
  122. ** Enable or disable the recording of changes by a session object. When
  123. ** enabled, a session object records changes made to the database. When
  124. ** disabled - it does not. A newly created session object is enabled.
  125. ** Refer to the documentation for [sqlite3session_changeset()] for further
  126. ** details regarding how enabling and disabling a session object affects
  127. ** the eventual changesets.
  128. **
  129. ** Passing zero to this function disables the session. Passing a value
  130. ** greater than zero enables it. Passing a value less than zero is a
  131. ** no-op, and may be used to query the current state of the session.
  132. **
  133. ** The return value indicates the final state of the session object: 0 if
  134. ** the session is disabled, or 1 if it is enabled.
  135. */
  136. int sqlite3session_enable(sqlite3_session *pSession, int bEnable);
  137. /*
  138. ** CAPI3REF: Set Or Clear the Indirect Change Flag
  139. ** METHOD: sqlite3_session
  140. **
  141. ** Each change recorded by a session object is marked as either direct or
  142. ** indirect. A change is marked as indirect if either:
  143. **
  144. ** <ul>
  145. ** <li> The session object "indirect" flag is set when the change is
  146. ** made, or
  147. ** <li> The change is made by an SQL trigger or foreign key action
  148. ** instead of directly as a result of a users SQL statement.
  149. ** </ul>
  150. **
  151. ** If a single row is affected by more than one operation within a session,
  152. ** then the change is considered indirect if all operations meet the criteria
  153. ** for an indirect change above, or direct otherwise.
  154. **
  155. ** This function is used to set, clear or query the session object indirect
  156. ** flag. If the second argument passed to this function is zero, then the
  157. ** indirect flag is cleared. If it is greater than zero, the indirect flag
  158. ** is set. Passing a value less than zero does not modify the current value
  159. ** of the indirect flag, and may be used to query the current state of the
  160. ** indirect flag for the specified session object.
  161. **
  162. ** The return value indicates the final state of the indirect flag: 0 if
  163. ** it is clear, or 1 if it is set.
  164. */
  165. int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect);
  166. /*
  167. ** CAPI3REF: Attach A Table To A Session Object
  168. ** METHOD: sqlite3_session
  169. **
  170. ** If argument zTab is not NULL, then it is the name of a table to attach
  171. ** to the session object passed as the first argument. All subsequent changes
  172. ** made to the table while the session object is enabled will be recorded. See
  173. ** documentation for [sqlite3session_changeset()] for further details.
  174. **
  175. ** Or, if argument zTab is NULL, then changes are recorded for all tables
  176. ** in the database. If additional tables are added to the database (by
  177. ** executing "CREATE TABLE" statements) after this call is made, changes for
  178. ** the new tables are also recorded.
  179. **
  180. ** Changes can only be recorded for tables that have a PRIMARY KEY explicitly
  181. ** defined as part of their CREATE TABLE statement. It does not matter if the
  182. ** PRIMARY KEY is an "INTEGER PRIMARY KEY" (rowid alias) or not. The PRIMARY
  183. ** KEY may consist of a single column, or may be a composite key.
  184. **
  185. ** It is not an error if the named table does not exist in the database. Nor
  186. ** is it an error if the named table does not have a PRIMARY KEY. However,
  187. ** no changes will be recorded in either of these scenarios.
  188. **
  189. ** Changes are not recorded for individual rows that have NULL values stored
  190. ** in one or more of their PRIMARY KEY columns.
  191. **
  192. ** SQLITE_OK is returned if the call completes without error. Or, if an error
  193. ** occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned.
  194. **
  195. ** <h3>Special sqlite_stat1 Handling</h3>
  196. **
  197. ** As of SQLite version 3.22.0, the "sqlite_stat1" table is an exception to
  198. ** some of the rules above. In SQLite, the schema of sqlite_stat1 is:
  199. ** <pre>
  200. ** &nbsp; CREATE TABLE sqlite_stat1(tbl,idx,stat)
  201. ** </pre>
  202. **
  203. ** Even though sqlite_stat1 does not have a PRIMARY KEY, changes are
  204. ** recorded for it as if the PRIMARY KEY is (tbl,idx). Additionally, changes
  205. ** are recorded for rows for which (idx IS NULL) is true. However, for such
  206. ** rows a zero-length blob (SQL value X'') is stored in the changeset or
  207. ** patchset instead of a NULL value. This allows such changesets to be
  208. ** manipulated by legacy implementations of sqlite3changeset_invert(),
  209. ** concat() and similar.
  210. **
  211. ** The sqlite3changeset_apply() function automatically converts the
  212. ** zero-length blob back to a NULL value when updating the sqlite_stat1
  213. ** table. However, if the application calls sqlite3changeset_new(),
  214. ** sqlite3changeset_old() or sqlite3changeset_conflict on a changeset
  215. ** iterator directly (including on a changeset iterator passed to a
  216. ** conflict-handler callback) then the X'' value is returned. The application
  217. ** must translate X'' to NULL itself if required.
  218. **
  219. ** Legacy (older than 3.22.0) versions of the sessions module cannot capture
  220. ** changes made to the sqlite_stat1 table. Legacy versions of the
  221. ** sqlite3changeset_apply() function silently ignore any modifications to the
  222. ** sqlite_stat1 table that are part of a changeset or patchset.
  223. */
  224. int sqlite3session_attach(
  225. sqlite3_session *pSession, /* Session object */
  226. const char *zTab /* Table name */
  227. );
  228. /*
  229. ** CAPI3REF: Set a table filter on a Session Object.
  230. ** METHOD: sqlite3_session
  231. **
  232. ** The second argument (xFilter) is the "filter callback". For changes to rows
  233. ** in tables that are not attached to the Session object, the filter is called
  234. ** to determine whether changes to the table's rows should be tracked or not.
  235. ** If xFilter returns 0, changes are not tracked. Note that once a table is
  236. ** attached, xFilter will not be called again.
  237. */
  238. void sqlite3session_table_filter(
  239. sqlite3_session *pSession, /* Session object */
  240. int(*xFilter)(
  241. void *pCtx, /* Copy of third arg to _filter_table() */
  242. const char *zTab /* Table name */
  243. ),
  244. void *pCtx /* First argument passed to xFilter */
  245. );
  246. /*
  247. ** CAPI3REF: Generate A Changeset From A Session Object
  248. ** METHOD: sqlite3_session
  249. **
  250. ** Obtain a changeset containing changes to the tables attached to the
  251. ** session object passed as the first argument. If successful,
  252. ** set *ppChangeset to point to a buffer containing the changeset
  253. ** and *pnChangeset to the size of the changeset in bytes before returning
  254. ** SQLITE_OK. If an error occurs, set both *ppChangeset and *pnChangeset to
  255. ** zero and return an SQLite error code.
  256. **
  257. ** A changeset consists of zero or more INSERT, UPDATE and/or DELETE changes,
  258. ** each representing a change to a single row of an attached table. An INSERT
  259. ** change contains the values of each field of a new database row. A DELETE
  260. ** contains the original values of each field of a deleted database row. An
  261. ** UPDATE change contains the original values of each field of an updated
  262. ** database row along with the updated values for each updated non-primary-key
  263. ** column. It is not possible for an UPDATE change to represent a change that
  264. ** modifies the values of primary key columns. If such a change is made, it
  265. ** is represented in a changeset as a DELETE followed by an INSERT.
  266. **
  267. ** Changes are not recorded for rows that have NULL values stored in one or
  268. ** more of their PRIMARY KEY columns. If such a row is inserted or deleted,
  269. ** no corresponding change is present in the changesets returned by this
  270. ** function. If an existing row with one or more NULL values stored in
  271. ** PRIMARY KEY columns is updated so that all PRIMARY KEY columns are non-NULL,
  272. ** only an INSERT is appears in the changeset. Similarly, if an existing row
  273. ** with non-NULL PRIMARY KEY values is updated so that one or more of its
  274. ** PRIMARY KEY columns are set to NULL, the resulting changeset contains a
  275. ** DELETE change only.
  276. **
  277. ** The contents of a changeset may be traversed using an iterator created
  278. ** using the [sqlite3changeset_start()] API. A changeset may be applied to
  279. ** a database with a compatible schema using the [sqlite3changeset_apply()]
  280. ** API.
  281. **
  282. ** Within a changeset generated by this function, all changes related to a
  283. ** single table are grouped together. In other words, when iterating through
  284. ** a changeset or when applying a changeset to a database, all changes related
  285. ** to a single table are processed before moving on to the next table. Tables
  286. ** are sorted in the same order in which they were attached (or auto-attached)
  287. ** to the sqlite3_session object. The order in which the changes related to
  288. ** a single table are stored is undefined.
  289. **
  290. ** Following a successful call to this function, it is the responsibility of
  291. ** the caller to eventually free the buffer that *ppChangeset points to using
  292. ** [sqlite3_free()].
  293. **
  294. ** <h3>Changeset Generation</h3>
  295. **
  296. ** Once a table has been attached to a session object, the session object
  297. ** records the primary key values of all new rows inserted into the table.
  298. ** It also records the original primary key and other column values of any
  299. ** deleted or updated rows. For each unique primary key value, data is only
  300. ** recorded once - the first time a row with said primary key is inserted,
  301. ** updated or deleted in the lifetime of the session.
  302. **
  303. ** There is one exception to the previous paragraph: when a row is inserted,
  304. ** updated or deleted, if one or more of its primary key columns contain a
  305. ** NULL value, no record of the change is made.
  306. **
  307. ** The session object therefore accumulates two types of records - those
  308. ** that consist of primary key values only (created when the user inserts
  309. ** a new record) and those that consist of the primary key values and the
  310. ** original values of other table columns (created when the users deletes
  311. ** or updates a record).
  312. **
  313. ** When this function is called, the requested changeset is created using
  314. ** both the accumulated records and the current contents of the database
  315. ** file. Specifically:
  316. **
  317. ** <ul>
  318. ** <li> For each record generated by an insert, the database is queried
  319. ** for a row with a matching primary key. If one is found, an INSERT
  320. ** change is added to the changeset. If no such row is found, no change
  321. ** is added to the changeset.
  322. **
  323. ** <li> For each record generated by an update or delete, the database is
  324. ** queried for a row with a matching primary key. If such a row is
  325. ** found and one or more of the non-primary key fields have been
  326. ** modified from their original values, an UPDATE change is added to
  327. ** the changeset. Or, if no such row is found in the table, a DELETE
  328. ** change is added to the changeset. If there is a row with a matching
  329. ** primary key in the database, but all fields contain their original
  330. ** values, no change is added to the changeset.
  331. ** </ul>
  332. **
  333. ** This means, amongst other things, that if a row is inserted and then later
  334. ** deleted while a session object is active, neither the insert nor the delete
  335. ** will be present in the changeset. Or if a row is deleted and then later a
  336. ** row with the same primary key values inserted while a session object is
  337. ** active, the resulting changeset will contain an UPDATE change instead of
  338. ** a DELETE and an INSERT.
  339. **
  340. ** When a session object is disabled (see the [sqlite3session_enable()] API),
  341. ** it does not accumulate records when rows are inserted, updated or deleted.
  342. ** This may appear to have some counter-intuitive effects if a single row
  343. ** is written to more than once during a session. For example, if a row
  344. ** is inserted while a session object is enabled, then later deleted while
  345. ** the same session object is disabled, no INSERT record will appear in the
  346. ** changeset, even though the delete took place while the session was disabled.
  347. ** Or, if one field of a row is updated while a session is disabled, and
  348. ** another field of the same row is updated while the session is enabled, the
  349. ** resulting changeset will contain an UPDATE change that updates both fields.
  350. */
  351. int sqlite3session_changeset(
  352. sqlite3_session *pSession, /* Session object */
  353. int *pnChangeset, /* OUT: Size of buffer at *ppChangeset */
  354. void **ppChangeset /* OUT: Buffer containing changeset */
  355. );
  356. /*
  357. ** CAPI3REF: Return An Upper-limit For The Size Of The Changeset
  358. ** METHOD: sqlite3_session
  359. **
  360. ** By default, this function always returns 0. For it to return
  361. ** a useful result, the sqlite3_session object must have been configured
  362. ** to enable this API using sqlite3session_object_config() with the
  363. ** SQLITE_SESSION_OBJCONFIG_SIZE verb.
  364. **
  365. ** When enabled, this function returns an upper limit, in bytes, for the size
  366. ** of the changeset that might be produced if sqlite3session_changeset() were
  367. ** called. The final changeset size might be equal to or smaller than the
  368. ** size in bytes returned by this function.
  369. */
  370. sqlite3_int64 sqlite3session_changeset_size(sqlite3_session *pSession);
  371. /*
  372. ** CAPI3REF: Load The Difference Between Tables Into A Session
  373. ** METHOD: sqlite3_session
  374. **
  375. ** If it is not already attached to the session object passed as the first
  376. ** argument, this function attaches table zTbl in the same manner as the
  377. ** [sqlite3session_attach()] function. If zTbl does not exist, or if it
  378. ** does not have a primary key, this function is a no-op (but does not return
  379. ** an error).
  380. **
  381. ** Argument zFromDb must be the name of a database ("main", "temp" etc.)
  382. ** attached to the same database handle as the session object that contains
  383. ** a table compatible with the table attached to the session by this function.
  384. ** A table is considered compatible if it:
  385. **
  386. ** <ul>
  387. ** <li> Has the same name,
  388. ** <li> Has the same set of columns declared in the same order, and
  389. ** <li> Has the same PRIMARY KEY definition.
  390. ** </ul>
  391. **
  392. ** If the tables are not compatible, SQLITE_SCHEMA is returned. If the tables
  393. ** are compatible but do not have any PRIMARY KEY columns, it is not an error
  394. ** but no changes are added to the session object. As with other session
  395. ** APIs, tables without PRIMARY KEYs are simply ignored.
  396. **
  397. ** This function adds a set of changes to the session object that could be
  398. ** used to update the table in database zFrom (call this the "from-table")
  399. ** so that its content is the same as the table attached to the session
  400. ** object (call this the "to-table"). Specifically:
  401. **
  402. ** <ul>
  403. ** <li> For each row (primary key) that exists in the to-table but not in
  404. ** the from-table, an INSERT record is added to the session object.
  405. **
  406. ** <li> For each row (primary key) that exists in the to-table but not in
  407. ** the from-table, a DELETE record is added to the session object.
  408. **
  409. ** <li> For each row (primary key) that exists in both tables, but features
  410. ** different non-PK values in each, an UPDATE record is added to the
  411. ** session.
  412. ** </ul>
  413. **
  414. ** To clarify, if this function is called and then a changeset constructed
  415. ** using [sqlite3session_changeset()], then after applying that changeset to
  416. ** database zFrom the contents of the two compatible tables would be
  417. ** identical.
  418. **
  419. ** It an error if database zFrom does not exist or does not contain the
  420. ** required compatible table.
  421. **
  422. ** If the operation is successful, SQLITE_OK is returned. Otherwise, an SQLite
  423. ** error code. In this case, if argument pzErrMsg is not NULL, *pzErrMsg
  424. ** may be set to point to a buffer containing an English language error
  425. ** message. It is the responsibility of the caller to free this buffer using
  426. ** sqlite3_free().
  427. */
  428. int sqlite3session_diff(
  429. sqlite3_session *pSession,
  430. const char *zFromDb,
  431. const char *zTbl,
  432. char **pzErrMsg
  433. );
  434. /*
  435. ** CAPI3REF: Generate A Patchset From A Session Object
  436. ** METHOD: sqlite3_session
  437. **
  438. ** The differences between a patchset and a changeset are that:
  439. **
  440. ** <ul>
  441. ** <li> DELETE records consist of the primary key fields only. The
  442. ** original values of other fields are omitted.
  443. ** <li> The original values of any modified fields are omitted from
  444. ** UPDATE records.
  445. ** </ul>
  446. **
  447. ** A patchset blob may be used with up to date versions of all
  448. ** sqlite3changeset_xxx API functions except for sqlite3changeset_invert(),
  449. ** which returns SQLITE_CORRUPT if it is passed a patchset. Similarly,
  450. ** attempting to use a patchset blob with old versions of the
  451. ** sqlite3changeset_xxx APIs also provokes an SQLITE_CORRUPT error.
  452. **
  453. ** Because the non-primary key "old.*" fields are omitted, no
  454. ** SQLITE_CHANGESET_DATA conflicts can be detected or reported if a patchset
  455. ** is passed to the sqlite3changeset_apply() API. Other conflict types work
  456. ** in the same way as for changesets.
  457. **
  458. ** Changes within a patchset are ordered in the same way as for changesets
  459. ** generated by the sqlite3session_changeset() function (i.e. all changes for
  460. ** a single table are grouped together, tables appear in the order in which
  461. ** they were attached to the session object).
  462. */
  463. int sqlite3session_patchset(
  464. sqlite3_session *pSession, /* Session object */
  465. int *pnPatchset, /* OUT: Size of buffer at *ppPatchset */
  466. void **ppPatchset /* OUT: Buffer containing patchset */
  467. );
  468. /*
  469. ** CAPI3REF: Test if a changeset has recorded any changes.
  470. **
  471. ** Return non-zero if no changes to attached tables have been recorded by
  472. ** the session object passed as the first argument. Otherwise, if one or
  473. ** more changes have been recorded, return zero.
  474. **
  475. ** Even if this function returns zero, it is possible that calling
  476. ** [sqlite3session_changeset()] on the session handle may still return a
  477. ** changeset that contains no changes. This can happen when a row in
  478. ** an attached table is modified and then later on the original values
  479. ** are restored. However, if this function returns non-zero, then it is
  480. ** guaranteed that a call to sqlite3session_changeset() will return a
  481. ** changeset containing zero changes.
  482. */
  483. int sqlite3session_isempty(sqlite3_session *pSession);
  484. /*
  485. ** CAPI3REF: Query for the amount of heap memory used by a session object.
  486. **
  487. ** This API returns the total amount of heap memory in bytes currently
  488. ** used by the session object passed as the only argument.
  489. */
  490. sqlite3_int64 sqlite3session_memory_used(sqlite3_session *pSession);
  491. /*
  492. ** CAPI3REF: Create An Iterator To Traverse A Changeset
  493. ** CONSTRUCTOR: sqlite3_changeset_iter
  494. **
  495. ** Create an iterator used to iterate through the contents of a changeset.
  496. ** If successful, *pp is set to point to the iterator handle and SQLITE_OK
  497. ** is returned. Otherwise, if an error occurs, *pp is set to zero and an
  498. ** SQLite error code is returned.
  499. **
  500. ** The following functions can be used to advance and query a changeset
  501. ** iterator created by this function:
  502. **
  503. ** <ul>
  504. ** <li> [sqlite3changeset_next()]
  505. ** <li> [sqlite3changeset_op()]
  506. ** <li> [sqlite3changeset_new()]
  507. ** <li> [sqlite3changeset_old()]
  508. ** </ul>
  509. **
  510. ** It is the responsibility of the caller to eventually destroy the iterator
  511. ** by passing it to [sqlite3changeset_finalize()]. The buffer containing the
  512. ** changeset (pChangeset) must remain valid until after the iterator is
  513. ** destroyed.
  514. **
  515. ** Assuming the changeset blob was created by one of the
  516. ** [sqlite3session_changeset()], [sqlite3changeset_concat()] or
  517. ** [sqlite3changeset_invert()] functions, all changes within the changeset
  518. ** that apply to a single table are grouped together. This means that when
  519. ** an application iterates through a changeset using an iterator created by
  520. ** this function, all changes that relate to a single table are visited
  521. ** consecutively. There is no chance that the iterator will visit a change
  522. ** the applies to table X, then one for table Y, and then later on visit
  523. ** another change for table X.
  524. **
  525. ** The behavior of sqlite3changeset_start_v2() and its streaming equivalent
  526. ** may be modified by passing a combination of
  527. ** [SQLITE_CHANGESETSTART_INVERT | supported flags] as the 4th parameter.
  528. **
  529. ** Note that the sqlite3changeset_start_v2() API is still <b>experimental</b>
  530. ** and therefore subject to change.
  531. */
  532. int sqlite3changeset_start(
  533. sqlite3_changeset_iter **pp, /* OUT: New changeset iterator handle */
  534. int nChangeset, /* Size of changeset blob in bytes */
  535. void *pChangeset /* Pointer to blob containing changeset */
  536. );
  537. int sqlite3changeset_start_v2(
  538. sqlite3_changeset_iter **pp, /* OUT: New changeset iterator handle */
  539. int nChangeset, /* Size of changeset blob in bytes */
  540. void *pChangeset, /* Pointer to blob containing changeset */
  541. int flags /* SESSION_CHANGESETSTART_* flags */
  542. );
  543. /*
  544. ** CAPI3REF: Flags for sqlite3changeset_start_v2
  545. **
  546. ** The following flags may passed via the 4th parameter to
  547. ** [sqlite3changeset_start_v2] and [sqlite3changeset_start_v2_strm]:
  548. **
  549. ** <dt>SQLITE_CHANGESETAPPLY_INVERT <dd>
  550. ** Invert the changeset while iterating through it. This is equivalent to
  551. ** inverting a changeset using sqlite3changeset_invert() before applying it.
  552. ** It is an error to specify this flag with a patchset.
  553. */
  554. #define SQLITE_CHANGESETSTART_INVERT 0x0002
  555. /*
  556. ** CAPI3REF: Advance A Changeset Iterator
  557. ** METHOD: sqlite3_changeset_iter
  558. **
  559. ** This function may only be used with iterators created by the function
  560. ** [sqlite3changeset_start()]. If it is called on an iterator passed to
  561. ** a conflict-handler callback by [sqlite3changeset_apply()], SQLITE_MISUSE
  562. ** is returned and the call has no effect.
  563. **
  564. ** Immediately after an iterator is created by sqlite3changeset_start(), it
  565. ** does not point to any change in the changeset. Assuming the changeset
  566. ** is not empty, the first call to this function advances the iterator to
  567. ** point to the first change in the changeset. Each subsequent call advances
  568. ** the iterator to point to the next change in the changeset (if any). If
  569. ** no error occurs and the iterator points to a valid change after a call
  570. ** to sqlite3changeset_next() has advanced it, SQLITE_ROW is returned.
  571. ** Otherwise, if all changes in the changeset have already been visited,
  572. ** SQLITE_DONE is returned.
  573. **
  574. ** If an error occurs, an SQLite error code is returned. Possible error
  575. ** codes include SQLITE_CORRUPT (if the changeset buffer is corrupt) or
  576. ** SQLITE_NOMEM.
  577. */
  578. int sqlite3changeset_next(sqlite3_changeset_iter *pIter);
  579. /*
  580. ** CAPI3REF: Obtain The Current Operation From A Changeset Iterator
  581. ** METHOD: sqlite3_changeset_iter
  582. **
  583. ** The pIter argument passed to this function may either be an iterator
  584. ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
  585. ** created by [sqlite3changeset_start()]. In the latter case, the most recent
  586. ** call to [sqlite3changeset_next()] must have returned [SQLITE_ROW]. If this
  587. ** is not the case, this function returns [SQLITE_MISUSE].
  588. **
  589. ** Arguments pOp, pnCol and pzTab may not be NULL. Upon return, three
  590. ** outputs are set through these pointers:
  591. **
  592. ** *pOp is set to one of [SQLITE_INSERT], [SQLITE_DELETE] or [SQLITE_UPDATE],
  593. ** depending on the type of change that the iterator currently points to;
  594. **
  595. ** *pnCol is set to the number of columns in the table affected by the change; and
  596. **
  597. ** *pzTab is set to point to a nul-terminated utf-8 encoded string containing
  598. ** the name of the table affected by the current change. The buffer remains
  599. ** valid until either sqlite3changeset_next() is called on the iterator
  600. ** or until the conflict-handler function returns.
  601. **
  602. ** If pbIndirect is not NULL, then *pbIndirect is set to true (1) if the change
  603. ** is an indirect change, or false (0) otherwise. See the documentation for
  604. ** [sqlite3session_indirect()] for a description of direct and indirect
  605. ** changes.
  606. **
  607. ** If no error occurs, SQLITE_OK is returned. If an error does occur, an
  608. ** SQLite error code is returned. The values of the output variables may not
  609. ** be trusted in this case.
  610. */
  611. int sqlite3changeset_op(
  612. sqlite3_changeset_iter *pIter, /* Iterator object */
  613. const char **pzTab, /* OUT: Pointer to table name */
  614. int *pnCol, /* OUT: Number of columns in table */
  615. int *pOp, /* OUT: SQLITE_INSERT, DELETE or UPDATE */
  616. int *pbIndirect /* OUT: True for an 'indirect' change */
  617. );
  618. /*
  619. ** CAPI3REF: Obtain The Primary Key Definition Of A Table
  620. ** METHOD: sqlite3_changeset_iter
  621. **
  622. ** For each modified table, a changeset includes the following:
  623. **
  624. ** <ul>
  625. ** <li> The number of columns in the table, and
  626. ** <li> Which of those columns make up the tables PRIMARY KEY.
  627. ** </ul>
  628. **
  629. ** This function is used to find which columns comprise the PRIMARY KEY of
  630. ** the table modified by the change that iterator pIter currently points to.
  631. ** If successful, *pabPK is set to point to an array of nCol entries, where
  632. ** nCol is the number of columns in the table. Elements of *pabPK are set to
  633. ** 0x01 if the corresponding column is part of the tables primary key, or
  634. ** 0x00 if it is not.
  635. **
  636. ** If argument pnCol is not NULL, then *pnCol is set to the number of columns
  637. ** in the table.
  638. **
  639. ** If this function is called when the iterator does not point to a valid
  640. ** entry, SQLITE_MISUSE is returned and the output variables zeroed. Otherwise,
  641. ** SQLITE_OK is returned and the output variables populated as described
  642. ** above.
  643. */
  644. int sqlite3changeset_pk(
  645. sqlite3_changeset_iter *pIter, /* Iterator object */
  646. unsigned char **pabPK, /* OUT: Array of boolean - true for PK cols */
  647. int *pnCol /* OUT: Number of entries in output array */
  648. );
  649. /*
  650. ** CAPI3REF: Obtain old.* Values From A Changeset Iterator
  651. ** METHOD: sqlite3_changeset_iter
  652. **
  653. ** The pIter argument passed to this function may either be an iterator
  654. ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
  655. ** created by [sqlite3changeset_start()]. In the latter case, the most recent
  656. ** call to [sqlite3changeset_next()] must have returned SQLITE_ROW.
  657. ** Furthermore, it may only be called if the type of change that the iterator
  658. ** currently points to is either [SQLITE_DELETE] or [SQLITE_UPDATE]. Otherwise,
  659. ** this function returns [SQLITE_MISUSE] and sets *ppValue to NULL.
  660. **
  661. ** Argument iVal must be greater than or equal to 0, and less than the number
  662. ** of columns in the table affected by the current change. Otherwise,
  663. ** [SQLITE_RANGE] is returned and *ppValue is set to NULL.
  664. **
  665. ** If successful, this function sets *ppValue to point to a protected
  666. ** sqlite3_value object containing the iVal'th value from the vector of
  667. ** original row values stored as part of the UPDATE or DELETE change and
  668. ** returns SQLITE_OK. The name of the function comes from the fact that this
  669. ** is similar to the "old.*" columns available to update or delete triggers.
  670. **
  671. ** If some other error occurs (e.g. an OOM condition), an SQLite error code
  672. ** is returned and *ppValue is set to NULL.
  673. */
  674. int sqlite3changeset_old(
  675. sqlite3_changeset_iter *pIter, /* Changeset iterator */
  676. int iVal, /* Column number */
  677. sqlite3_value **ppValue /* OUT: Old value (or NULL pointer) */
  678. );
  679. /*
  680. ** CAPI3REF: Obtain new.* Values From A Changeset Iterator
  681. ** METHOD: sqlite3_changeset_iter
  682. **
  683. ** The pIter argument passed to this function may either be an iterator
  684. ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
  685. ** created by [sqlite3changeset_start()]. In the latter case, the most recent
  686. ** call to [sqlite3changeset_next()] must have returned SQLITE_ROW.
  687. ** Furthermore, it may only be called if the type of change that the iterator
  688. ** currently points to is either [SQLITE_UPDATE] or [SQLITE_INSERT]. Otherwise,
  689. ** this function returns [SQLITE_MISUSE] and sets *ppValue to NULL.
  690. **
  691. ** Argument iVal must be greater than or equal to 0, and less than the number
  692. ** of columns in the table affected by the current change. Otherwise,
  693. ** [SQLITE_RANGE] is returned and *ppValue is set to NULL.
  694. **
  695. ** If successful, this function sets *ppValue to point to a protected
  696. ** sqlite3_value object containing the iVal'th value from the vector of
  697. ** new row values stored as part of the UPDATE or INSERT change and
  698. ** returns SQLITE_OK. If the change is an UPDATE and does not include
  699. ** a new value for the requested column, *ppValue is set to NULL and
  700. ** SQLITE_OK returned. The name of the function comes from the fact that
  701. ** this is similar to the "new.*" columns available to update or delete
  702. ** triggers.
  703. **
  704. ** If some other error occurs (e.g. an OOM condition), an SQLite error code
  705. ** is returned and *ppValue is set to NULL.
  706. */
  707. int sqlite3changeset_new(
  708. sqlite3_changeset_iter *pIter, /* Changeset iterator */
  709. int iVal, /* Column number */
  710. sqlite3_value **ppValue /* OUT: New value (or NULL pointer) */
  711. );
  712. /*
  713. ** CAPI3REF: Obtain Conflicting Row Values From A Changeset Iterator
  714. ** METHOD: sqlite3_changeset_iter
  715. **
  716. ** This function should only be used with iterator objects passed to a
  717. ** conflict-handler callback by [sqlite3changeset_apply()] with either
  718. ** [SQLITE_CHANGESET_DATA] or [SQLITE_CHANGESET_CONFLICT]. If this function
  719. ** is called on any other iterator, [SQLITE_MISUSE] is returned and *ppValue
  720. ** is set to NULL.
  721. **
  722. ** Argument iVal must be greater than or equal to 0, and less than the number
  723. ** of columns in the table affected by the current change. Otherwise,
  724. ** [SQLITE_RANGE] is returned and *ppValue is set to NULL.
  725. **
  726. ** If successful, this function sets *ppValue to point to a protected
  727. ** sqlite3_value object containing the iVal'th value from the
  728. ** "conflicting row" associated with the current conflict-handler callback
  729. ** and returns SQLITE_OK.
  730. **
  731. ** If some other error occurs (e.g. an OOM condition), an SQLite error code
  732. ** is returned and *ppValue is set to NULL.
  733. */
  734. int sqlite3changeset_conflict(
  735. sqlite3_changeset_iter *pIter, /* Changeset iterator */
  736. int iVal, /* Column number */
  737. sqlite3_value **ppValue /* OUT: Value from conflicting row */
  738. );
  739. /*
  740. ** CAPI3REF: Determine The Number Of Foreign Key Constraint Violations
  741. ** METHOD: sqlite3_changeset_iter
  742. **
  743. ** This function may only be called with an iterator passed to an
  744. ** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case
  745. ** it sets the output variable to the total number of known foreign key
  746. ** violations in the destination database and returns SQLITE_OK.
  747. **
  748. ** In all other cases this function returns SQLITE_MISUSE.
  749. */
  750. int sqlite3changeset_fk_conflicts(
  751. sqlite3_changeset_iter *pIter, /* Changeset iterator */
  752. int *pnOut /* OUT: Number of FK violations */
  753. );
  754. /*
  755. ** CAPI3REF: Finalize A Changeset Iterator
  756. ** METHOD: sqlite3_changeset_iter
  757. **
  758. ** This function is used to finalize an iterator allocated with
  759. ** [sqlite3changeset_start()].
  760. **
  761. ** This function should only be called on iterators created using the
  762. ** [sqlite3changeset_start()] function. If an application calls this
  763. ** function with an iterator passed to a conflict-handler by
  764. ** [sqlite3changeset_apply()], [SQLITE_MISUSE] is immediately returned and the
  765. ** call has no effect.
  766. **
  767. ** If an error was encountered within a call to an sqlite3changeset_xxx()
  768. ** function (for example an [SQLITE_CORRUPT] in [sqlite3changeset_next()] or an
  769. ** [SQLITE_NOMEM] in [sqlite3changeset_new()]) then an error code corresponding
  770. ** to that error is returned by this function. Otherwise, SQLITE_OK is
  771. ** returned. This is to allow the following pattern (pseudo-code):
  772. **
  773. ** <pre>
  774. ** sqlite3changeset_start();
  775. ** while( SQLITE_ROW==sqlite3changeset_next() ){
  776. ** // Do something with change.
  777. ** }
  778. ** rc = sqlite3changeset_finalize();
  779. ** if( rc!=SQLITE_OK ){
  780. ** // An error has occurred
  781. ** }
  782. ** </pre>
  783. */
  784. int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter);
  785. /*
  786. ** CAPI3REF: Invert A Changeset
  787. **
  788. ** This function is used to "invert" a changeset object. Applying an inverted
  789. ** changeset to a database reverses the effects of applying the uninverted
  790. ** changeset. Specifically:
  791. **
  792. ** <ul>
  793. ** <li> Each DELETE change is changed to an INSERT, and
  794. ** <li> Each INSERT change is changed to a DELETE, and
  795. ** <li> For each UPDATE change, the old.* and new.* values are exchanged.
  796. ** </ul>
  797. **
  798. ** This function does not change the order in which changes appear within
  799. ** the changeset. It merely reverses the sense of each individual change.
  800. **
  801. ** If successful, a pointer to a buffer containing the inverted changeset
  802. ** is stored in *ppOut, the size of the same buffer is stored in *pnOut, and
  803. ** SQLITE_OK is returned. If an error occurs, both *pnOut and *ppOut are
  804. ** zeroed and an SQLite error code returned.
  805. **
  806. ** It is the responsibility of the caller to eventually call sqlite3_free()
  807. ** on the *ppOut pointer to free the buffer allocation following a successful
  808. ** call to this function.
  809. **
  810. ** WARNING/TODO: This function currently assumes that the input is a valid
  811. ** changeset. If it is not, the results are undefined.
  812. */
  813. int sqlite3changeset_invert(
  814. int nIn, const void *pIn, /* Input changeset */
  815. int *pnOut, void **ppOut /* OUT: Inverse of input */
  816. );
  817. /*
  818. ** CAPI3REF: Concatenate Two Changeset Objects
  819. **
  820. ** This function is used to concatenate two changesets, A and B, into a
  821. ** single changeset. The result is a changeset equivalent to applying
  822. ** changeset A followed by changeset B.
  823. **
  824. ** This function combines the two input changesets using an
  825. ** sqlite3_changegroup object. Calling it produces similar results as the
  826. ** following code fragment:
  827. **
  828. ** <pre>
  829. ** sqlite3_changegroup *pGrp;
  830. ** rc = sqlite3_changegroup_new(&pGrp);
  831. ** if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nA, pA);
  832. ** if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nB, pB);
  833. ** if( rc==SQLITE_OK ){
  834. ** rc = sqlite3changegroup_output(pGrp, pnOut, ppOut);
  835. ** }else{
  836. ** *ppOut = 0;
  837. ** *pnOut = 0;
  838. ** }
  839. ** </pre>
  840. **
  841. ** Refer to the sqlite3_changegroup documentation below for details.
  842. */
  843. int sqlite3changeset_concat(
  844. int nA, /* Number of bytes in buffer pA */
  845. void *pA, /* Pointer to buffer containing changeset A */
  846. int nB, /* Number of bytes in buffer pB */
  847. void *pB, /* Pointer to buffer containing changeset B */
  848. int *pnOut, /* OUT: Number of bytes in output changeset */
  849. void **ppOut /* OUT: Buffer containing output changeset */
  850. );
  851. /*
  852. ** CAPI3REF: Upgrade the Schema of a Changeset/Patchset
  853. */
  854. int sqlite3changeset_upgrade(
  855. sqlite3 *db,
  856. const char *zDb,
  857. int nIn, const void *pIn, /* Input changeset */
  858. int *pnOut, void **ppOut /* OUT: Inverse of input */
  859. );
  860. /*
  861. ** CAPI3REF: Changegroup Handle
  862. **
  863. ** A changegroup is an object used to combine two or more
  864. ** [changesets] or [patchsets]
  865. */
  866. typedef struct sqlite3_changegroup sqlite3_changegroup;
  867. /*
  868. ** CAPI3REF: Create A New Changegroup Object
  869. ** CONSTRUCTOR: sqlite3_changegroup
  870. **
  871. ** An sqlite3_changegroup object is used to combine two or more changesets
  872. ** (or patchsets) into a single changeset (or patchset). A single changegroup
  873. ** object may combine changesets or patchsets, but not both. The output is
  874. ** always in the same format as the input.
  875. **
  876. ** If successful, this function returns SQLITE_OK and populates (*pp) with
  877. ** a pointer to a new sqlite3_changegroup object before returning. The caller
  878. ** should eventually free the returned object using a call to
  879. ** sqlite3changegroup_delete(). If an error occurs, an SQLite error code
  880. ** (i.e. SQLITE_NOMEM) is returned and *pp is set to NULL.
  881. **
  882. ** The usual usage pattern for an sqlite3_changegroup object is as follows:
  883. **
  884. ** <ul>
  885. ** <li> It is created using a call to sqlite3changegroup_new().
  886. **
  887. ** <li> Zero or more changesets (or patchsets) are added to the object
  888. ** by calling sqlite3changegroup_add().
  889. **
  890. ** <li> The result of combining all input changesets together is obtained
  891. ** by the application via a call to sqlite3changegroup_output().
  892. **
  893. ** <li> The object is deleted using a call to sqlite3changegroup_delete().
  894. ** </ul>
  895. **
  896. ** Any number of calls to add() and output() may be made between the calls to
  897. ** new() and delete(), and in any order.
  898. **
  899. ** As well as the regular sqlite3changegroup_add() and
  900. ** sqlite3changegroup_output() functions, also available are the streaming
  901. ** versions sqlite3changegroup_add_strm() and sqlite3changegroup_output_strm().
  902. */
  903. int sqlite3changegroup_new(sqlite3_changegroup **pp);
  904. /*
  905. ** CAPI3REF: Add a Schema to a Changegroup
  906. ** METHOD: sqlite3_changegroup_schema
  907. **
  908. ** This method may be used to optionally enforce the rule that the changesets
  909. ** added to the changegroup handle must match the schema of database zDb
  910. ** ("main", "temp", or the name of an attached database). If
  911. ** sqlite3changegroup_add() is called to add a changeset that is not compatible
  912. ** with the configured schema, SQLITE_SCHEMA is returned and the changegroup
  913. ** object is left in an undefined state.
  914. **
  915. ** A changeset schema is considered compatible with the database schema in
  916. ** the same way as for sqlite3changeset_apply(). Specifically, for each
  917. ** table in the changeset, there exists a database table with:
  918. **
  919. ** <ul>
  920. ** <li> The name identified by the changeset, and
  921. ** <li> at least as many columns as recorded in the changeset, and
  922. ** <li> the primary key columns in the same position as recorded in
  923. ** the changeset.
  924. ** </ul>
  925. **
  926. ** The output of the changegroup object always has the same schema as the
  927. ** database nominated using this function. In cases where changesets passed
  928. ** to sqlite3changegroup_add() have fewer columns than the corresponding table
  929. ** in the database schema, these are filled in using the default column
  930. ** values from the database schema. This makes it possible to combined
  931. ** changesets that have different numbers of columns for a single table
  932. ** within a changegroup, provided that they are otherwise compatible.
  933. */
  934. int sqlite3changegroup_schema(sqlite3_changegroup*, sqlite3*, const char *zDb);
  935. /*
  936. ** CAPI3REF: Add A Changeset To A Changegroup
  937. ** METHOD: sqlite3_changegroup
  938. **
  939. ** Add all changes within the changeset (or patchset) in buffer pData (size
  940. ** nData bytes) to the changegroup.
  941. **
  942. ** If the buffer contains a patchset, then all prior calls to this function
  943. ** on the same changegroup object must also have specified patchsets. Or, if
  944. ** the buffer contains a changeset, so must have the earlier calls to this
  945. ** function. Otherwise, SQLITE_ERROR is returned and no changes are added
  946. ** to the changegroup.
  947. **
  948. ** Rows within the changeset and changegroup are identified by the values in
  949. ** their PRIMARY KEY columns. A change in the changeset is considered to
  950. ** apply to the same row as a change already present in the changegroup if
  951. ** the two rows have the same primary key.
  952. **
  953. ** Changes to rows that do not already appear in the changegroup are
  954. ** simply copied into it. Or, if both the new changeset and the changegroup
  955. ** contain changes that apply to a single row, the final contents of the
  956. ** changegroup depends on the type of each change, as follows:
  957. **
  958. ** <table border=1 style="margin-left:8ex;margin-right:8ex">
  959. ** <tr><th style="white-space:pre">Existing Change </th>
  960. ** <th style="white-space:pre">New Change </th>
  961. ** <th>Output Change
  962. ** <tr><td>INSERT <td>INSERT <td>
  963. ** The new change is ignored. This case does not occur if the new
  964. ** changeset was recorded immediately after the changesets already
  965. ** added to the changegroup.
  966. ** <tr><td>INSERT <td>UPDATE <td>
  967. ** The INSERT change remains in the changegroup. The values in the
  968. ** INSERT change are modified as if the row was inserted by the
  969. ** existing change and then updated according to the new change.
  970. ** <tr><td>INSERT <td>DELETE <td>
  971. ** The existing INSERT is removed from the changegroup. The DELETE is
  972. ** not added.
  973. ** <tr><td>UPDATE <td>INSERT <td>
  974. ** The new change is ignored. This case does not occur if the new
  975. ** changeset was recorded immediately after the changesets already
  976. ** added to the changegroup.
  977. ** <tr><td>UPDATE <td>UPDATE <td>
  978. ** The existing UPDATE remains within the changegroup. It is amended
  979. ** so that the accompanying values are as if the row was updated once
  980. ** by the existing change and then again by the new change.
  981. ** <tr><td>UPDATE <td>DELETE <td>
  982. ** The existing UPDATE is replaced by the new DELETE within the
  983. ** changegroup.
  984. ** <tr><td>DELETE <td>INSERT <td>
  985. ** If one or more of the column values in the row inserted by the
  986. ** new change differ from those in the row deleted by the existing
  987. ** change, the existing DELETE is replaced by an UPDATE within the
  988. ** changegroup. Otherwise, if the inserted row is exactly the same
  989. ** as the deleted row, the existing DELETE is simply discarded.
  990. ** <tr><td>DELETE <td>UPDATE <td>
  991. ** The new change is ignored. This case does not occur if the new
  992. ** changeset was recorded immediately after the changesets already
  993. ** added to the changegroup.
  994. ** <tr><td>DELETE <td>DELETE <td>
  995. ** The new change is ignored. This case does not occur if the new
  996. ** changeset was recorded immediately after the changesets already
  997. ** added to the changegroup.
  998. ** </table>
  999. **
  1000. ** If the new changeset contains changes to a table that is already present
  1001. ** in the changegroup, then the number of columns and the position of the
  1002. ** primary key columns for the table must be consistent. If this is not the
  1003. ** case, this function fails with SQLITE_SCHEMA. Except, if the changegroup
  1004. ** object has been configured with a database schema using the
  1005. ** sqlite3changegroup_schema() API, then it is possible to combine changesets
  1006. ** with different numbers of columns for a single table, provided that
  1007. ** they are otherwise compatible.
  1008. **
  1009. ** If the input changeset appears to be corrupt and the corruption is
  1010. ** detected, SQLITE_CORRUPT is returned. Or, if an out-of-memory condition
  1011. ** occurs during processing, this function returns SQLITE_NOMEM.
  1012. **
  1013. ** In all cases, if an error occurs the state of the final contents of the
  1014. ** changegroup is undefined. If no error occurs, SQLITE_OK is returned.
  1015. */
  1016. int sqlite3changegroup_add(sqlite3_changegroup*, int nData, void *pData);
  1017. /*
  1018. ** CAPI3REF: Add A Single Change To A Changegroup
  1019. ** METHOD: sqlite3_changegroup
  1020. **
  1021. ** This function adds the single change currently indicated by the iterator
  1022. ** passed as the second argument to the changegroup object. The rules for
  1023. ** adding the change are just as described for [sqlite3changegroup_add()].
  1024. **
  1025. ** If the change is successfully added to the changegroup, SQLITE_OK is
  1026. ** returned. Otherwise, an SQLite error code is returned.
  1027. **
  1028. ** The iterator must point to a valid entry when this function is called.
  1029. ** If it does not, SQLITE_ERROR is returned and no change is added to the
  1030. ** changegroup. Additionally, the iterator must not have been opened with
  1031. ** the SQLITE_CHANGESETAPPLY_INVERT flag. In this case SQLITE_ERROR is also
  1032. ** returned.
  1033. */
  1034. int sqlite3changegroup_add_change(
  1035. sqlite3_changegroup*,
  1036. sqlite3_changeset_iter*
  1037. );
  1038. /*
  1039. ** CAPI3REF: Obtain A Composite Changeset From A Changegroup
  1040. ** METHOD: sqlite3_changegroup
  1041. **
  1042. ** Obtain a buffer containing a changeset (or patchset) representing the
  1043. ** current contents of the changegroup. If the inputs to the changegroup
  1044. ** were themselves changesets, the output is a changeset. Or, if the
  1045. ** inputs were patchsets, the output is also a patchset.
  1046. **
  1047. ** As with the output of the sqlite3session_changeset() and
  1048. ** sqlite3session_patchset() functions, all changes related to a single
  1049. ** table are grouped together in the output of this function. Tables appear
  1050. ** in the same order as for the very first changeset added to the changegroup.
  1051. ** If the second or subsequent changesets added to the changegroup contain
  1052. ** changes for tables that do not appear in the first changeset, they are
  1053. ** appended onto the end of the output changeset, again in the order in
  1054. ** which they are first encountered.
  1055. **
  1056. ** If an error occurs, an SQLite error code is returned and the output
  1057. ** variables (*pnData) and (*ppData) are set to 0. Otherwise, SQLITE_OK
  1058. ** is returned and the output variables are set to the size of and a
  1059. ** pointer to the output buffer, respectively. In this case it is the
  1060. ** responsibility of the caller to eventually free the buffer using a
  1061. ** call to sqlite3_free().
  1062. */
  1063. int sqlite3changegroup_output(
  1064. sqlite3_changegroup*,
  1065. int *pnData, /* OUT: Size of output buffer in bytes */
  1066. void **ppData /* OUT: Pointer to output buffer */
  1067. );
  1068. /*
  1069. ** CAPI3REF: Delete A Changegroup Object
  1070. ** DESTRUCTOR: sqlite3_changegroup
  1071. */
  1072. void sqlite3changegroup_delete(sqlite3_changegroup*);
  1073. /*
  1074. ** CAPI3REF: Apply A Changeset To A Database
  1075. **
  1076. ** Apply a changeset or patchset to a database. These functions attempt to
  1077. ** update the "main" database attached to handle db with the changes found in
  1078. ** the changeset passed via the second and third arguments.
  1079. **
  1080. ** The fourth argument (xFilter) passed to these functions is the "filter
  1081. ** callback". If it is not NULL, then for each table affected by at least one
  1082. ** change in the changeset, the filter callback is invoked with
  1083. ** the table name as the second argument, and a copy of the context pointer
  1084. ** passed as the sixth argument as the first. If the "filter callback"
  1085. ** returns zero, then no attempt is made to apply any changes to the table.
  1086. ** Otherwise, if the return value is non-zero or the xFilter argument to
  1087. ** is NULL, all changes related to the table are attempted.
  1088. **
  1089. ** For each table that is not excluded by the filter callback, this function
  1090. ** tests that the target database contains a compatible table. A table is
  1091. ** considered compatible if all of the following are true:
  1092. **
  1093. ** <ul>
  1094. ** <li> The table has the same name as the name recorded in the
  1095. ** changeset, and
  1096. ** <li> The table has at least as many columns as recorded in the
  1097. ** changeset, and
  1098. ** <li> The table has primary key columns in the same position as
  1099. ** recorded in the changeset.
  1100. ** </ul>
  1101. **
  1102. ** If there is no compatible table, it is not an error, but none of the
  1103. ** changes associated with the table are applied. A warning message is issued
  1104. ** via the sqlite3_log() mechanism with the error code SQLITE_SCHEMA. At most
  1105. ** one such warning is issued for each table in the changeset.
  1106. **
  1107. ** For each change for which there is a compatible table, an attempt is made
  1108. ** to modify the table contents according to the UPDATE, INSERT or DELETE
  1109. ** change. If a change cannot be applied cleanly, the conflict handler
  1110. ** function passed as the fifth argument to sqlite3changeset_apply() may be
  1111. ** invoked. A description of exactly when the conflict handler is invoked for
  1112. ** each type of change is below.
  1113. **
  1114. ** Unlike the xFilter argument, xConflict may not be passed NULL. The results
  1115. ** of passing anything other than a valid function pointer as the xConflict
  1116. ** argument are undefined.
  1117. **
  1118. ** Each time the conflict handler function is invoked, it must return one
  1119. ** of [SQLITE_CHANGESET_OMIT], [SQLITE_CHANGESET_ABORT] or
  1120. ** [SQLITE_CHANGESET_REPLACE]. SQLITE_CHANGESET_REPLACE may only be returned
  1121. ** if the second argument passed to the conflict handler is either
  1122. ** SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If the conflict-handler
  1123. ** returns an illegal value, any changes already made are rolled back and
  1124. ** the call to sqlite3changeset_apply() returns SQLITE_MISUSE. Different
  1125. ** actions are taken by sqlite3changeset_apply() depending on the value
  1126. ** returned by each invocation of the conflict-handler function. Refer to
  1127. ** the documentation for the three
  1128. ** [SQLITE_CHANGESET_OMIT|available return values] for details.
  1129. **
  1130. ** <dl>
  1131. ** <dt>DELETE Changes<dd>
  1132. ** For each DELETE change, the function checks if the target database
  1133. ** contains a row with the same primary key value (or values) as the
  1134. ** original row values stored in the changeset. If it does, and the values
  1135. ** stored in all non-primary key columns also match the values stored in
  1136. ** the changeset the row is deleted from the target database.
  1137. **
  1138. ** If a row with matching primary key values is found, but one or more of
  1139. ** the non-primary key fields contains a value different from the original
  1140. ** row value stored in the changeset, the conflict-handler function is
  1141. ** invoked with [SQLITE_CHANGESET_DATA] as the second argument. If the
  1142. ** database table has more columns than are recorded in the changeset,
  1143. ** only the values of those non-primary key fields are compared against
  1144. ** the current database contents - any trailing database table columns
  1145. ** are ignored.
  1146. **
  1147. ** If no row with matching primary key values is found in the database,
  1148. ** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
  1149. ** passed as the second argument.
  1150. **
  1151. ** If the DELETE operation is attempted, but SQLite returns SQLITE_CONSTRAINT
  1152. ** (which can only happen if a foreign key constraint is violated), the
  1153. ** conflict-handler function is invoked with [SQLITE_CHANGESET_CONSTRAINT]
  1154. ** passed as the second argument. This includes the case where the DELETE
  1155. ** operation is attempted because an earlier call to the conflict handler
  1156. ** function returned [SQLITE_CHANGESET_REPLACE].
  1157. **
  1158. ** <dt>INSERT Changes<dd>
  1159. ** For each INSERT change, an attempt is made to insert the new row into
  1160. ** the database. If the changeset row contains fewer fields than the
  1161. ** database table, the trailing fields are populated with their default
  1162. ** values.
  1163. **
  1164. ** If the attempt to insert the row fails because the database already
  1165. ** contains a row with the same primary key values, the conflict handler
  1166. ** function is invoked with the second argument set to
  1167. ** [SQLITE_CHANGESET_CONFLICT].
  1168. **
  1169. ** If the attempt to insert the row fails because of some other constraint
  1170. ** violation (e.g. NOT NULL or UNIQUE), the conflict handler function is
  1171. ** invoked with the second argument set to [SQLITE_CHANGESET_CONSTRAINT].
  1172. ** This includes the case where the INSERT operation is re-attempted because
  1173. ** an earlier call to the conflict handler function returned
  1174. ** [SQLITE_CHANGESET_REPLACE].
  1175. **
  1176. ** <dt>UPDATE Changes<dd>
  1177. ** For each UPDATE change, the function checks if the target database
  1178. ** contains a row with the same primary key value (or values) as the
  1179. ** original row values stored in the changeset. If it does, and the values
  1180. ** stored in all modified non-primary key columns also match the values
  1181. ** stored in the changeset the row is updated within the target database.
  1182. **
  1183. ** If a row with matching primary key values is found, but one or more of
  1184. ** the modified non-primary key fields contains a value different from an
  1185. ** original row value stored in the changeset, the conflict-handler function
  1186. ** is invoked with [SQLITE_CHANGESET_DATA] as the second argument. Since
  1187. ** UPDATE changes only contain values for non-primary key fields that are
  1188. ** to be modified, only those fields need to match the original values to
  1189. ** avoid the SQLITE_CHANGESET_DATA conflict-handler callback.
  1190. **
  1191. ** If no row with matching primary key values is found in the database,
  1192. ** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
  1193. ** passed as the second argument.
  1194. **
  1195. ** If the UPDATE operation is attempted, but SQLite returns
  1196. ** SQLITE_CONSTRAINT, the conflict-handler function is invoked with
  1197. ** [SQLITE_CHANGESET_CONSTRAINT] passed as the second argument.
  1198. ** This includes the case where the UPDATE operation is attempted after
  1199. ** an earlier call to the conflict handler function returned
  1200. ** [SQLITE_CHANGESET_REPLACE].
  1201. ** </dl>
  1202. **
  1203. ** It is safe to execute SQL statements, including those that write to the
  1204. ** table that the callback related to, from within the xConflict callback.
  1205. ** This can be used to further customize the application's conflict
  1206. ** resolution strategy.
  1207. **
  1208. ** All changes made by these functions are enclosed in a savepoint transaction.
  1209. ** If any other error (aside from a constraint failure when attempting to
  1210. ** write to the target database) occurs, then the savepoint transaction is
  1211. ** rolled back, restoring the target database to its original state, and an
  1212. ** SQLite error code returned.
  1213. **
  1214. ** If the output parameters (ppRebase) and (pnRebase) are non-NULL and
  1215. ** the input is a changeset (not a patchset), then sqlite3changeset_apply_v2()
  1216. ** may set (*ppRebase) to point to a "rebase" that may be used with the
  1217. ** sqlite3_rebaser APIs buffer before returning. In this case (*pnRebase)
  1218. ** is set to the size of the buffer in bytes. It is the responsibility of the
  1219. ** caller to eventually free any such buffer using sqlite3_free(). The buffer
  1220. ** is only allocated and populated if one or more conflicts were encountered
  1221. ** while applying the patchset. See comments surrounding the sqlite3_rebaser
  1222. ** APIs for further details.
  1223. **
  1224. ** The behavior of sqlite3changeset_apply_v2() and its streaming equivalent
  1225. ** may be modified by passing a combination of
  1226. ** [SQLITE_CHANGESETAPPLY_NOSAVEPOINT | supported flags] as the 9th parameter.
  1227. **
  1228. ** Note that the sqlite3changeset_apply_v2() API is still <b>experimental</b>
  1229. ** and therefore subject to change.
  1230. */
  1231. int sqlite3changeset_apply(
  1232. sqlite3 *db, /* Apply change to "main" db of this handle */
  1233. int nChangeset, /* Size of changeset in bytes */
  1234. void *pChangeset, /* Changeset blob */
  1235. int(*xFilter)(
  1236. void *pCtx, /* Copy of sixth arg to _apply() */
  1237. const char *zTab /* Table name */
  1238. ),
  1239. int(*xConflict)(
  1240. void *pCtx, /* Copy of sixth arg to _apply() */
  1241. int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
  1242. sqlite3_changeset_iter *p /* Handle describing change and conflict */
  1243. ),
  1244. void *pCtx /* First argument passed to xConflict */
  1245. );
  1246. int sqlite3changeset_apply_v2(
  1247. sqlite3 *db, /* Apply change to "main" db of this handle */
  1248. int nChangeset, /* Size of changeset in bytes */
  1249. void *pChangeset, /* Changeset blob */
  1250. int(*xFilter)(
  1251. void *pCtx, /* Copy of sixth arg to _apply() */
  1252. const char *zTab /* Table name */
  1253. ),
  1254. int(*xConflict)(
  1255. void *pCtx, /* Copy of sixth arg to _apply() */
  1256. int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
  1257. sqlite3_changeset_iter *p /* Handle describing change and conflict */
  1258. ),
  1259. void *pCtx, /* First argument passed to xConflict */
  1260. void **ppRebase, int *pnRebase, /* OUT: Rebase data */
  1261. int flags /* SESSION_CHANGESETAPPLY_* flags */
  1262. );
  1263. /*
  1264. ** CAPI3REF: Flags for sqlite3changeset_apply_v2
  1265. **
  1266. ** The following flags may passed via the 9th parameter to
  1267. ** [sqlite3changeset_apply_v2] and [sqlite3changeset_apply_v2_strm]:
  1268. **
  1269. ** <dl>
  1270. ** <dt>SQLITE_CHANGESETAPPLY_NOSAVEPOINT <dd>
  1271. ** Usually, the sessions module encloses all operations performed by
  1272. ** a single call to apply_v2() or apply_v2_strm() in a [SAVEPOINT]. The
  1273. ** SAVEPOINT is committed if the changeset or patchset is successfully
  1274. ** applied, or rolled back if an error occurs. Specifying this flag
  1275. ** causes the sessions module to omit this savepoint. In this case, if the
  1276. ** caller has an open transaction or savepoint when apply_v2() is called,
  1277. ** it may revert the partially applied changeset by rolling it back.
  1278. **
  1279. ** <dt>SQLITE_CHANGESETAPPLY_INVERT <dd>
  1280. ** Invert the changeset before applying it. This is equivalent to inverting
  1281. ** a changeset using sqlite3changeset_invert() before applying it. It is
  1282. ** an error to specify this flag with a patchset.
  1283. **
  1284. ** <dt>SQLITE_CHANGESETAPPLY_IGNORENOOP <dd>
  1285. ** Do not invoke the conflict handler callback for any changes that
  1286. ** would not actually modify the database even if they were applied.
  1287. ** Specifically, this means that the conflict handler is not invoked
  1288. ** for:
  1289. ** <ul>
  1290. ** <li>a delete change if the row being deleted cannot be found,
  1291. ** <li>an update change if the modified fields are already set to
  1292. ** their new values in the conflicting row, or
  1293. ** <li>an insert change if all fields of the conflicting row match
  1294. ** the row being inserted.
  1295. ** </ul>
  1296. **
  1297. ** <dt>SQLITE_CHANGESETAPPLY_FKNOACTION <dd>
  1298. ** If this flag it set, then all foreign key constraints in the target
  1299. ** database behave as if they were declared with "ON UPDATE NO ACTION ON
  1300. ** DELETE NO ACTION", even if they are actually CASCADE, RESTRICT, SET NULL
  1301. ** or SET DEFAULT.
  1302. */
  1303. #define SQLITE_CHANGESETAPPLY_NOSAVEPOINT 0x0001
  1304. #define SQLITE_CHANGESETAPPLY_INVERT 0x0002
  1305. #define SQLITE_CHANGESETAPPLY_IGNORENOOP 0x0004
  1306. #define SQLITE_CHANGESETAPPLY_FKNOACTION 0x0008
  1307. /*
  1308. ** CAPI3REF: Constants Passed To The Conflict Handler
  1309. **
  1310. ** Values that may be passed as the second argument to a conflict-handler.
  1311. **
  1312. ** <dl>
  1313. ** <dt>SQLITE_CHANGESET_DATA<dd>
  1314. ** The conflict handler is invoked with CHANGESET_DATA as the second argument
  1315. ** when processing a DELETE or UPDATE change if a row with the required
  1316. ** PRIMARY KEY fields is present in the database, but one or more other
  1317. ** (non primary-key) fields modified by the update do not contain the
  1318. ** expected "before" values.
  1319. **
  1320. ** The conflicting row, in this case, is the database row with the matching
  1321. ** primary key.
  1322. **
  1323. ** <dt>SQLITE_CHANGESET_NOTFOUND<dd>
  1324. ** The conflict handler is invoked with CHANGESET_NOTFOUND as the second
  1325. ** argument when processing a DELETE or UPDATE change if a row with the
  1326. ** required PRIMARY KEY fields is not present in the database.
  1327. **
  1328. ** There is no conflicting row in this case. The results of invoking the
  1329. ** sqlite3changeset_conflict() API are undefined.
  1330. **
  1331. ** <dt>SQLITE_CHANGESET_CONFLICT<dd>
  1332. ** CHANGESET_CONFLICT is passed as the second argument to the conflict
  1333. ** handler while processing an INSERT change if the operation would result
  1334. ** in duplicate primary key values.
  1335. **
  1336. ** The conflicting row in this case is the database row with the matching
  1337. ** primary key.
  1338. **
  1339. ** <dt>SQLITE_CHANGESET_FOREIGN_KEY<dd>
  1340. ** If foreign key handling is enabled, and applying a changeset leaves the
  1341. ** database in a state containing foreign key violations, the conflict
  1342. ** handler is invoked with CHANGESET_FOREIGN_KEY as the second argument
  1343. ** exactly once before the changeset is committed. If the conflict handler
  1344. ** returns CHANGESET_OMIT, the changes, including those that caused the
  1345. ** foreign key constraint violation, are committed. Or, if it returns
  1346. ** CHANGESET_ABORT, the changeset is rolled back.
  1347. **
  1348. ** No current or conflicting row information is provided. The only function
  1349. ** it is possible to call on the supplied sqlite3_changeset_iter handle
  1350. ** is sqlite3changeset_fk_conflicts().
  1351. **
  1352. ** <dt>SQLITE_CHANGESET_CONSTRAINT<dd>
  1353. ** If any other constraint violation occurs while applying a change (i.e.
  1354. ** a UNIQUE, CHECK or NOT NULL constraint), the conflict handler is
  1355. ** invoked with CHANGESET_CONSTRAINT as the second argument.
  1356. **
  1357. ** There is no conflicting row in this case. The results of invoking the
  1358. ** sqlite3changeset_conflict() API are undefined.
  1359. **
  1360. ** </dl>
  1361. */
  1362. #define SQLITE_CHANGESET_DATA 1
  1363. #define SQLITE_CHANGESET_NOTFOUND 2
  1364. #define SQLITE_CHANGESET_CONFLICT 3
  1365. #define SQLITE_CHANGESET_CONSTRAINT 4
  1366. #define SQLITE_CHANGESET_FOREIGN_KEY 5
  1367. /*
  1368. ** CAPI3REF: Constants Returned By The Conflict Handler
  1369. **
  1370. ** A conflict handler callback must return one of the following three values.
  1371. **
  1372. ** <dl>
  1373. ** <dt>SQLITE_CHANGESET_OMIT<dd>
  1374. ** If a conflict handler returns this value no special action is taken. The
  1375. ** change that caused the conflict is not applied. The session module
  1376. ** continues to the next change in the changeset.
  1377. **
  1378. ** <dt>SQLITE_CHANGESET_REPLACE<dd>
  1379. ** This value may only be returned if the second argument to the conflict
  1380. ** handler was SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If this
  1381. ** is not the case, any changes applied so far are rolled back and the
  1382. ** call to sqlite3changeset_apply() returns SQLITE_MISUSE.
  1383. **
  1384. ** If CHANGESET_REPLACE is returned by an SQLITE_CHANGESET_DATA conflict
  1385. ** handler, then the conflicting row is either updated or deleted, depending
  1386. ** on the type of change.
  1387. **
  1388. ** If CHANGESET_REPLACE is returned by an SQLITE_CHANGESET_CONFLICT conflict
  1389. ** handler, then the conflicting row is removed from the database and a
  1390. ** second attempt to apply the change is made. If this second attempt fails,
  1391. ** the original row is restored to the database before continuing.
  1392. **
  1393. ** <dt>SQLITE_CHANGESET_ABORT<dd>
  1394. ** If this value is returned, any changes applied so far are rolled back
  1395. ** and the call to sqlite3changeset_apply() returns SQLITE_ABORT.
  1396. ** </dl>
  1397. */
  1398. #define SQLITE_CHANGESET_OMIT 0
  1399. #define SQLITE_CHANGESET_REPLACE 1
  1400. #define SQLITE_CHANGESET_ABORT 2
  1401. /*
  1402. ** CAPI3REF: Rebasing changesets
  1403. ** EXPERIMENTAL
  1404. **
  1405. ** Suppose there is a site hosting a database in state S0. And that
  1406. ** modifications are made that move that database to state S1 and a
  1407. ** changeset recorded (the "local" changeset). Then, a changeset based
  1408. ** on S0 is received from another site (the "remote" changeset) and
  1409. ** applied to the database. The database is then in state
  1410. ** (S1+"remote"), where the exact state depends on any conflict
  1411. ** resolution decisions (OMIT or REPLACE) made while applying "remote".
  1412. ** Rebasing a changeset is to update it to take those conflict
  1413. ** resolution decisions into account, so that the same conflicts
  1414. ** do not have to be resolved elsewhere in the network.
  1415. **
  1416. ** For example, if both the local and remote changesets contain an
  1417. ** INSERT of the same key on "CREATE TABLE t1(a PRIMARY KEY, b)":
  1418. **
  1419. ** local: INSERT INTO t1 VALUES(1, 'v1');
  1420. ** remote: INSERT INTO t1 VALUES(1, 'v2');
  1421. **
  1422. ** and the conflict resolution is REPLACE, then the INSERT change is
  1423. ** removed from the local changeset (it was overridden). Or, if the
  1424. ** conflict resolution was "OMIT", then the local changeset is modified
  1425. ** to instead contain:
  1426. **
  1427. ** UPDATE t1 SET b = 'v2' WHERE a=1;
  1428. **
  1429. ** Changes within the local changeset are rebased as follows:
  1430. **
  1431. ** <dl>
  1432. ** <dt>Local INSERT<dd>
  1433. ** This may only conflict with a remote INSERT. If the conflict
  1434. ** resolution was OMIT, then add an UPDATE change to the rebased
  1435. ** changeset. Or, if the conflict resolution was REPLACE, add
  1436. ** nothing to the rebased changeset.
  1437. **
  1438. ** <dt>Local DELETE<dd>
  1439. ** This may conflict with a remote UPDATE or DELETE. In both cases the
  1440. ** only possible resolution is OMIT. If the remote operation was a
  1441. ** DELETE, then add no change to the rebased changeset. If the remote
  1442. ** operation was an UPDATE, then the old.* fields of change are updated
  1443. ** to reflect the new.* values in the UPDATE.
  1444. **
  1445. ** <dt>Local UPDATE<dd>
  1446. ** This may conflict with a remote UPDATE or DELETE. If it conflicts
  1447. ** with a DELETE, and the conflict resolution was OMIT, then the update
  1448. ** is changed into an INSERT. Any undefined values in the new.* record
  1449. ** from the update change are filled in using the old.* values from
  1450. ** the conflicting DELETE. Or, if the conflict resolution was REPLACE,
  1451. ** the UPDATE change is simply omitted from the rebased changeset.
  1452. **
  1453. ** If conflict is with a remote UPDATE and the resolution is OMIT, then
  1454. ** the old.* values are rebased using the new.* values in the remote
  1455. ** change. Or, if the resolution is REPLACE, then the change is copied
  1456. ** into the rebased changeset with updates to columns also updated by
  1457. ** the conflicting remote UPDATE removed. If this means no columns would
  1458. ** be updated, the change is omitted.
  1459. ** </dl>
  1460. **
  1461. ** A local change may be rebased against multiple remote changes
  1462. ** simultaneously. If a single key is modified by multiple remote
  1463. ** changesets, they are combined as follows before the local changeset
  1464. ** is rebased:
  1465. **
  1466. ** <ul>
  1467. ** <li> If there has been one or more REPLACE resolutions on a
  1468. ** key, it is rebased according to a REPLACE.
  1469. **
  1470. ** <li> If there have been no REPLACE resolutions on a key, then
  1471. ** the local changeset is rebased according to the most recent
  1472. ** of the OMIT resolutions.
  1473. ** </ul>
  1474. **
  1475. ** Note that conflict resolutions from multiple remote changesets are
  1476. ** combined on a per-field basis, not per-row. This means that in the
  1477. ** case of multiple remote UPDATE operations, some fields of a single
  1478. ** local change may be rebased for REPLACE while others are rebased for
  1479. ** OMIT.
  1480. **
  1481. ** In order to rebase a local changeset, the remote changeset must first
  1482. ** be applied to the local database using sqlite3changeset_apply_v2() and
  1483. ** the buffer of rebase information captured. Then:
  1484. **
  1485. ** <ol>
  1486. ** <li> An sqlite3_rebaser object is created by calling
  1487. ** sqlite3rebaser_create().
  1488. ** <li> The new object is configured with the rebase buffer obtained from
  1489. ** sqlite3changeset_apply_v2() by calling sqlite3rebaser_configure().
  1490. ** If the local changeset is to be rebased against multiple remote
  1491. ** changesets, then sqlite3rebaser_configure() should be called
  1492. ** multiple times, in the same order that the multiple
  1493. ** sqlite3changeset_apply_v2() calls were made.
  1494. ** <li> Each local changeset is rebased by calling sqlite3rebaser_rebase().
  1495. ** <li> The sqlite3_rebaser object is deleted by calling
  1496. ** sqlite3rebaser_delete().
  1497. ** </ol>
  1498. */
  1499. typedef struct sqlite3_rebaser sqlite3_rebaser;
  1500. /*
  1501. ** CAPI3REF: Create a changeset rebaser object.
  1502. ** EXPERIMENTAL
  1503. **
  1504. ** Allocate a new changeset rebaser object. If successful, set (*ppNew) to
  1505. ** point to the new object and return SQLITE_OK. Otherwise, if an error
  1506. ** occurs, return an SQLite error code (e.g. SQLITE_NOMEM) and set (*ppNew)
  1507. ** to NULL.
  1508. */
  1509. int sqlite3rebaser_create(sqlite3_rebaser **ppNew);
  1510. /*
  1511. ** CAPI3REF: Configure a changeset rebaser object.
  1512. ** EXPERIMENTAL
  1513. **
  1514. ** Configure the changeset rebaser object to rebase changesets according
  1515. ** to the conflict resolutions described by buffer pRebase (size nRebase
  1516. ** bytes), which must have been obtained from a previous call to
  1517. ** sqlite3changeset_apply_v2().
  1518. */
  1519. int sqlite3rebaser_configure(
  1520. sqlite3_rebaser*,
  1521. int nRebase, const void *pRebase
  1522. );
  1523. /*
  1524. ** CAPI3REF: Rebase a changeset
  1525. ** EXPERIMENTAL
  1526. **
  1527. ** Argument pIn must point to a buffer containing a changeset nIn bytes
  1528. ** in size. This function allocates and populates a buffer with a copy
  1529. ** of the changeset rebased according to the configuration of the
  1530. ** rebaser object passed as the first argument. If successful, (*ppOut)
  1531. ** is set to point to the new buffer containing the rebased changeset and
  1532. ** (*pnOut) to its size in bytes and SQLITE_OK returned. It is the
  1533. ** responsibility of the caller to eventually free the new buffer using
  1534. ** sqlite3_free(). Otherwise, if an error occurs, (*ppOut) and (*pnOut)
  1535. ** are set to zero and an SQLite error code returned.
  1536. */
  1537. int sqlite3rebaser_rebase(
  1538. sqlite3_rebaser*,
  1539. int nIn, const void *pIn,
  1540. int *pnOut, void **ppOut
  1541. );
  1542. /*
  1543. ** CAPI3REF: Delete a changeset rebaser object.
  1544. ** EXPERIMENTAL
  1545. **
  1546. ** Delete the changeset rebaser object and all associated resources. There
  1547. ** should be one call to this function for each successful invocation
  1548. ** of sqlite3rebaser_create().
  1549. */
  1550. void sqlite3rebaser_delete(sqlite3_rebaser *p);
  1551. /*
  1552. ** CAPI3REF: Streaming Versions of API functions.
  1553. **
  1554. ** The six streaming API xxx_strm() functions serve similar purposes to the
  1555. ** corresponding non-streaming API functions:
  1556. **
  1557. ** <table border=1 style="margin-left:8ex;margin-right:8ex">
  1558. ** <tr><th>Streaming function<th>Non-streaming equivalent</th>
  1559. ** <tr><td>sqlite3changeset_apply_strm<td>[sqlite3changeset_apply]
  1560. ** <tr><td>sqlite3changeset_apply_strm_v2<td>[sqlite3changeset_apply_v2]
  1561. ** <tr><td>sqlite3changeset_concat_strm<td>[sqlite3changeset_concat]
  1562. ** <tr><td>sqlite3changeset_invert_strm<td>[sqlite3changeset_invert]
  1563. ** <tr><td>sqlite3changeset_start_strm<td>[sqlite3changeset_start]
  1564. ** <tr><td>sqlite3session_changeset_strm<td>[sqlite3session_changeset]
  1565. ** <tr><td>sqlite3session_patchset_strm<td>[sqlite3session_patchset]
  1566. ** </table>
  1567. **
  1568. ** Non-streaming functions that accept changesets (or patchsets) as input
  1569. ** require that the entire changeset be stored in a single buffer in memory.
  1570. ** Similarly, those that return a changeset or patchset do so by returning
  1571. ** a pointer to a single large buffer allocated using sqlite3_malloc().
  1572. ** Normally this is convenient. However, if an application running in a
  1573. ** low-memory environment is required to handle very large changesets, the
  1574. ** large contiguous memory allocations required can become onerous.
  1575. **
  1576. ** In order to avoid this problem, instead of a single large buffer, input
  1577. ** is passed to a streaming API functions by way of a callback function that
  1578. ** the sessions module invokes to incrementally request input data as it is
  1579. ** required. In all cases, a pair of API function parameters such as
  1580. **
  1581. ** <pre>
  1582. ** &nbsp; int nChangeset,
  1583. ** &nbsp; void *pChangeset,
  1584. ** </pre>
  1585. **
  1586. ** Is replaced by:
  1587. **
  1588. ** <pre>
  1589. ** &nbsp; int (*xInput)(void *pIn, void *pData, int *pnData),
  1590. ** &nbsp; void *pIn,
  1591. ** </pre>
  1592. **
  1593. ** Each time the xInput callback is invoked by the sessions module, the first
  1594. ** argument passed is a copy of the supplied pIn context pointer. The second
  1595. ** argument, pData, points to a buffer (*pnData) bytes in size. Assuming no
  1596. ** error occurs the xInput method should copy up to (*pnData) bytes of data
  1597. ** into the buffer and set (*pnData) to the actual number of bytes copied
  1598. ** before returning SQLITE_OK. If the input is completely exhausted, (*pnData)
  1599. ** should be set to zero to indicate this. Or, if an error occurs, an SQLite
  1600. ** error code should be returned. In all cases, if an xInput callback returns
  1601. ** an error, all processing is abandoned and the streaming API function
  1602. ** returns a copy of the error code to the caller.
  1603. **
  1604. ** In the case of sqlite3changeset_start_strm(), the xInput callback may be
  1605. ** invoked by the sessions module at any point during the lifetime of the
  1606. ** iterator. If such an xInput callback returns an error, the iterator enters
  1607. ** an error state, whereby all subsequent calls to iterator functions
  1608. ** immediately fail with the same error code as returned by xInput.
  1609. **
  1610. ** Similarly, streaming API functions that return changesets (or patchsets)
  1611. ** return them in chunks by way of a callback function instead of via a
  1612. ** pointer to a single large buffer. In this case, a pair of parameters such
  1613. ** as:
  1614. **
  1615. ** <pre>
  1616. ** &nbsp; int *pnChangeset,
  1617. ** &nbsp; void **ppChangeset,
  1618. ** </pre>
  1619. **
  1620. ** Is replaced by:
  1621. **
  1622. ** <pre>
  1623. ** &nbsp; int (*xOutput)(void *pOut, const void *pData, int nData),
  1624. ** &nbsp; void *pOut
  1625. ** </pre>
  1626. **
  1627. ** The xOutput callback is invoked zero or more times to return data to
  1628. ** the application. The first parameter passed to each call is a copy of the
  1629. ** pOut pointer supplied by the application. The second parameter, pData,
  1630. ** points to a buffer nData bytes in size containing the chunk of output
  1631. ** data being returned. If the xOutput callback successfully processes the
  1632. ** supplied data, it should return SQLITE_OK to indicate success. Otherwise,
  1633. ** it should return some other SQLite error code. In this case processing
  1634. ** is immediately abandoned and the streaming API function returns a copy
  1635. ** of the xOutput error code to the application.
  1636. **
  1637. ** The sessions module never invokes an xOutput callback with the third
  1638. ** parameter set to a value less than or equal to zero. Other than this,
  1639. ** no guarantees are made as to the size of the chunks of data returned.
  1640. */
  1641. int sqlite3changeset_apply_strm(
  1642. sqlite3 *db, /* Apply change to "main" db of this handle */
  1643. int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
  1644. void *pIn, /* First arg for xInput */
  1645. int(*xFilter)(
  1646. void *pCtx, /* Copy of sixth arg to _apply() */
  1647. const char *zTab /* Table name */
  1648. ),
  1649. int(*xConflict)(
  1650. void *pCtx, /* Copy of sixth arg to _apply() */
  1651. int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
  1652. sqlite3_changeset_iter *p /* Handle describing change and conflict */
  1653. ),
  1654. void *pCtx /* First argument passed to xConflict */
  1655. );
  1656. int sqlite3changeset_apply_v2_strm(
  1657. sqlite3 *db, /* Apply change to "main" db of this handle */
  1658. int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
  1659. void *pIn, /* First arg for xInput */
  1660. int(*xFilter)(
  1661. void *pCtx, /* Copy of sixth arg to _apply() */
  1662. const char *zTab /* Table name */
  1663. ),
  1664. int(*xConflict)(
  1665. void *pCtx, /* Copy of sixth arg to _apply() */
  1666. int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
  1667. sqlite3_changeset_iter *p /* Handle describing change and conflict */
  1668. ),
  1669. void *pCtx, /* First argument passed to xConflict */
  1670. void **ppRebase, int *pnRebase,
  1671. int flags
  1672. );
  1673. int sqlite3changeset_concat_strm(
  1674. int (*xInputA)(void *pIn, void *pData, int *pnData),
  1675. void *pInA,
  1676. int (*xInputB)(void *pIn, void *pData, int *pnData),
  1677. void *pInB,
  1678. int (*xOutput)(void *pOut, const void *pData, int nData),
  1679. void *pOut
  1680. );
  1681. int sqlite3changeset_invert_strm(
  1682. int (*xInput)(void *pIn, void *pData, int *pnData),
  1683. void *pIn,
  1684. int (*xOutput)(void *pOut, const void *pData, int nData),
  1685. void *pOut
  1686. );
  1687. int sqlite3changeset_start_strm(
  1688. sqlite3_changeset_iter **pp,
  1689. int (*xInput)(void *pIn, void *pData, int *pnData),
  1690. void *pIn
  1691. );
  1692. int sqlite3changeset_start_v2_strm(
  1693. sqlite3_changeset_iter **pp,
  1694. int (*xInput)(void *pIn, void *pData, int *pnData),
  1695. void *pIn,
  1696. int flags
  1697. );
  1698. int sqlite3session_changeset_strm(
  1699. sqlite3_session *pSession,
  1700. int (*xOutput)(void *pOut, const void *pData, int nData),
  1701. void *pOut
  1702. );
  1703. int sqlite3session_patchset_strm(
  1704. sqlite3_session *pSession,
  1705. int (*xOutput)(void *pOut, const void *pData, int nData),
  1706. void *pOut
  1707. );
  1708. int sqlite3changegroup_add_strm(sqlite3_changegroup*,
  1709. int (*xInput)(void *pIn, void *pData, int *pnData),
  1710. void *pIn
  1711. );
  1712. int sqlite3changegroup_output_strm(sqlite3_changegroup*,
  1713. int (*xOutput)(void *pOut, const void *pData, int nData),
  1714. void *pOut
  1715. );
  1716. int sqlite3rebaser_rebase_strm(
  1717. sqlite3_rebaser *pRebaser,
  1718. int (*xInput)(void *pIn, void *pData, int *pnData),
  1719. void *pIn,
  1720. int (*xOutput)(void *pOut, const void *pData, int nData),
  1721. void *pOut
  1722. );
  1723. /*
  1724. ** CAPI3REF: Configure global parameters
  1725. **
  1726. ** The sqlite3session_config() interface is used to make global configuration
  1727. ** changes to the sessions module in order to tune it to the specific needs
  1728. ** of the application.
  1729. **
  1730. ** The sqlite3session_config() interface is not threadsafe. If it is invoked
  1731. ** while any other thread is inside any other sessions method then the
  1732. ** results are undefined. Furthermore, if it is invoked after any sessions
  1733. ** related objects have been created, the results are also undefined.
  1734. **
  1735. ** The first argument to the sqlite3session_config() function must be one
  1736. ** of the SQLITE_SESSION_CONFIG_XXX constants defined below. The
  1737. ** interpretation of the (void*) value passed as the second parameter and
  1738. ** the effect of calling this function depends on the value of the first
  1739. ** parameter.
  1740. **
  1741. ** <dl>
  1742. ** <dt>SQLITE_SESSION_CONFIG_STRMSIZE<dd>
  1743. ** By default, the sessions module streaming interfaces attempt to input
  1744. ** and output data in approximately 1 KiB chunks. This operand may be used
  1745. ** to set and query the value of this configuration setting. The pointer
  1746. ** passed as the second argument must point to a value of type (int).
  1747. ** If this value is greater than 0, it is used as the new streaming data
  1748. ** chunk size for both input and output. Before returning, the (int) value
  1749. ** pointed to by pArg is set to the final value of the streaming interface
  1750. ** chunk size.
  1751. ** </dl>
  1752. **
  1753. ** This function returns SQLITE_OK if successful, or an SQLite error code
  1754. ** otherwise.
  1755. */
  1756. int sqlite3session_config(int op, void *pArg);
  1757. /*
  1758. ** CAPI3REF: Values for sqlite3session_config().
  1759. */
  1760. #define SQLITE_SESSION_CONFIG_STRMSIZE 1
  1761. /*
  1762. ** Make sure we can call this stuff from C++.
  1763. */
  1764. #ifdef __cplusplus
  1765. }
  1766. #endif
  1767. #endif /* !defined(__SQLITESESSION_H_) && defined(SQLITE_ENABLE_SESSION) */