]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Add a "flags" parameter to experimental API sqlite3changeset_apply_v2(). Also
authordan <dan@noemail.net>
Mon, 9 Apr 2018 11:43:58 +0000 (11:43 +0000)
committerdan <dan@noemail.net>
Mon, 9 Apr 2018 11:43:58 +0000 (11:43 +0000)
add the SQLITE_CHANGESETAPPLY_NOSAVEPOINT flag.

FossilOrigin-Name: 3d29631fa7270b18f51de8cfe1285959e42ad9a673d5d2d5c016126f5f1b31a8

ext/session/session1.test
ext/session/sqlite3session.c
ext/session/sqlite3session.h
ext/session/test_session.c
manifest
manifest.uuid

index 9e3fc4b87abd79e5a39973cf5c2722a4587b9bad..f423a0cf4b49d9cd2a70298f70de05a24288413b 100644 (file)
@@ -612,6 +612,49 @@ do_iterator_test $tn.12.2 * {
   {UPDATE t1 0 X.. {i 3 {} {} i 3} {{} {} {} {} t one}}
 }
 
+#-------------------------------------------------------------------------
+# Test that no savepoint is used if -nosavepoint is specified.
+#
+do_execsql_test $tn.13.1 {
+  CREATE TABLE x1(a INTEGER PRIMARY KEY, b)%WR%;
+}
+do_test $tn.13.2 {
+  execsql BEGIN
+  set C [changeset_from_sql {
+    INSERT INTO x1 VALUES(1, 'one');
+    INSERT INTO x1 VALUES(2, 'two');
+    INSERT INTO x1 VALUES(3, 'three');
+  }]
+  execsql ROLLBACK
+  execsql {
+    INSERT INTO x1 VALUES(1, 'i');
+    INSERT INTO x1 VALUES(2, 'ii');
+    INSERT INTO x1 VALUES(3, 'iii');
+  }
+} {}
+
+proc xConflict {args} {
+  set ret [lindex $::CONFLICT_HANDLERS 0]
+  set ::CONFLICT_HANDLERS [lrange $::CONFLICT_HANDLERS 1 end]
+  set ret
+}
+do_test $tn.13.3 {
+  set CONFLICT_HANDLERS [list REPLACE REPLACE ABORT]
+  execsql BEGIN
+  catch { sqlite3changeset_apply_v2 db $C xConflict } msg
+  execsql {
+    SELECT * FROM x1
+  }
+} {1 i 2 ii 3 iii}
+do_test $tn.13.3 {
+  set CONFLICT_HANDLERS [list REPLACE REPLACE ABORT]
+  execsql ROLLBACK
+  execsql BEGIN
+  catch { sqlite3changeset_apply_v2 -nosavepoint db $C xConflict } msg
+  execsql { SELECT * FROM x1 }
+} {1 one 2 two 3 iii}
+execsql ROLLBACK
+
 }]
 }
 
index a59d38c804c192244ad3394fe382e6b0f055a0b9..e432b894ca5cf7ae8bc2306809203337065f8929 100644 (file)
@@ -4234,10 +4234,11 @@ static int sessionChangesetApply(
     sqlite3_changeset_iter *p     /* Handle describing change and conflict */
   ),
   void *pCtx,                     /* First argument passed to xConflict */
-  void **ppRebase, int *pnRebase  /* OUT: Rebase information */
+  void **ppRebase, int *pnRebase, /* OUT: Rebase information */
+  int flags                       /* SESSION_APPLY_XXX flags */
 ){
   int schemaMismatch = 0;
-  int rc;                         /* Return code */
+  int rc = SQLITE_OK;             /* Return code */
   const char *zTab = 0;           /* Name of current table */
   int nTab = 0;                   /* Result of sqlite3Strlen30(zTab) */
   SessionApplyCtx sApply;         /* changeset_apply() context object */
@@ -4248,7 +4249,9 @@ static int sessionChangesetApply(
   pIter->in.bNoDiscard = 1;
   memset(&sApply, 0, sizeof(sApply));
   sqlite3_mutex_enter(sqlite3_db_mutex(db));
-  rc = sqlite3_exec(db, "SAVEPOINT changeset_apply", 0, 0, 0);
+  if( (flags & SQLITE_CHANGESETAPPLY_NOSAVEPOINT)==0 ){
+    rc = sqlite3_exec(db, "SAVEPOINT changeset_apply", 0, 0, 0);
+  }
   if( rc==SQLITE_OK ){
     rc = sqlite3_exec(db, "PRAGMA defer_foreign_keys = 1", 0, 0, 0);
   }
@@ -4386,11 +4389,13 @@ static int sessionChangesetApply(
   }
   sqlite3_exec(db, "PRAGMA defer_foreign_keys = 0", 0, 0, 0);
 
-  if( rc==SQLITE_OK ){
-    rc = sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0);
-  }else{
-    sqlite3_exec(db, "ROLLBACK TO changeset_apply", 0, 0, 0);
-    sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0);
+  if( (flags & SQLITE_CHANGESETAPPLY_NOSAVEPOINT)==0 ){
+    if( rc==SQLITE_OK ){
+      rc = sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0);
+    }else{
+      sqlite3_exec(db, "ROLLBACK TO changeset_apply", 0, 0, 0);
+      sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0);
+    }
   }
 
   if( rc==SQLITE_OK && bPatchset==0 && ppRebase && pnRebase ){
@@ -4427,13 +4432,14 @@ int sqlite3changeset_apply_v2(
     sqlite3_changeset_iter *p     /* Handle describing change and conflict */
   ),
   void *pCtx,                     /* First argument passed to xConflict */
-  void **ppRebase, int *pnRebase
+  void **ppRebase, int *pnRebase,
+  int flags
 ){
   sqlite3_changeset_iter *pIter;  /* Iterator to skip through changeset */  
   int rc = sqlite3changeset_start(&pIter, nChangeset, pChangeset);
   if( rc==SQLITE_OK ){
     rc = sessionChangesetApply(
-        db, pIter, xFilter, xConflict, pCtx, ppRebase, pnRebase
+        db, pIter, xFilter, xConflict, pCtx, ppRebase, pnRebase, flags
     );
   }
   return rc;
@@ -4460,7 +4466,7 @@ int sqlite3changeset_apply(
   void *pCtx                      /* First argument passed to xConflict */
 ){
   return sqlite3changeset_apply_v2(
-      db, nChangeset, pChangeset, xFilter, xConflict, pCtx, 0, 0
+      db, nChangeset, pChangeset, xFilter, xConflict, pCtx, 0, 0, 0
   );
 }
 
@@ -4483,13 +4489,14 @@ int sqlite3changeset_apply_v2_strm(
     sqlite3_changeset_iter *p     /* Handle describing change and conflict */
   ),
   void *pCtx,                     /* First argument passed to xConflict */
-  void **ppRebase, int *pnRebase
+  void **ppRebase, int *pnRebase,
+  int flags
 ){
   sqlite3_changeset_iter *pIter;  /* Iterator to skip through changeset */  
   int rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
   if( rc==SQLITE_OK ){
     rc = sessionChangesetApply(
-        db, pIter, xFilter, xConflict, pCtx, ppRebase, pnRebase
+        db, pIter, xFilter, xConflict, pCtx, ppRebase, pnRebase, flags
     );
   }
   return rc;
@@ -4510,7 +4517,7 @@ int sqlite3changeset_apply_strm(
   void *pCtx                      /* First argument passed to xConflict */
 ){
   return sqlite3changeset_apply_v2_strm(
-      db, xInput, pIn, xFilter, xConflict, pCtx, 0, 0
+      db, xInput, pIn, xFilter, xConflict, pCtx, 0, 0, 0
   );
 }
 
index d4da20dbb9309f2c6fdddf08e2a7782a7215906c..a3def5f1df1bf1359ef9d3d2407cadbf5c32a3b1 100644 (file)
@@ -1095,6 +1095,13 @@ void sqlite3changegroup_delete(sqlite3_changegroup*);
 ** is only allocated and populated if one or more conflicts were encountered
 ** while applying the patchset. See comments surrounding the sqlite3_rebaser
 ** APIs for further details.
+**
+** The behavior of sqlite3changeset_apply_v2() and its streaming equivalent
+** may be modified by passing a combination of
+** [SQLITE_CHANGESETAPPLY_NOSAVEPOINT | supported flags] as the 9th parameter.
+**
+** Note that the sqlite3changeset_apply_v2() API is still <b>experimental</b>
+** and therefore subject to change.
 */
 int sqlite3changeset_apply(
   sqlite3 *db,                    /* Apply change to "main" db of this handle */
@@ -1125,9 +1132,28 @@ int sqlite3changeset_apply_v2(
     sqlite3_changeset_iter *p     /* Handle describing change and conflict */
   ),
   void *pCtx,                     /* First argument passed to xConflict */
-  void **ppRebase, int *pnRebase
+  void **ppRebase, int *pnRebase, /* OUT: Rebase data */
+  int flags                       /* Combination of SESSION_APPLY_* flags */
 );
 
+/*
+** CAPI3REF: Flags for sqlite3changeset_apply_v2
+**
+** The following flags may passed via the 9th parameter to
+** [sqlite3changeset_apply_v2] and [sqlite3changeset_apply_v2_strm]:
+**
+** <dl>
+** <dt>SQLITE_CHANGESETAPPLY_NOSAVEPOINT <dd>
+**   Usually, the sessions module encloses all operations performed by
+**   a single call to apply_v2() or apply_v2_strm() in a [SAVEPOINT]. The
+**   SAVEPOINT is committed if the changeset or patchset is successfully
+**   applied, or rolled back if an error occurs. Specifying this flag
+**   causes the sessions module to omit this savepoint. In this case, if the
+**   caller has an open transaction or savepoint when apply_v2() is called, 
+**   it may revert the partially applied changeset by rolling it back.
+*/
+#define SQLITE_CHANGESETAPPLY_NOSAVEPOINT   0x0001
+
 /* 
 ** CAPI3REF: Constants Passed To The Conflict Handler
 **
@@ -1388,6 +1414,7 @@ void sqlite3rebaser_delete(sqlite3_rebaser *p);
 ** <table border=1 style="margin-left:8ex;margin-right:8ex">
 **   <tr><th>Streaming function<th>Non-streaming equivalent</th>
 **   <tr><td>sqlite3changeset_apply_strm<td>[sqlite3changeset_apply] 
+**   <tr><td>sqlite3changeset_apply_strm_v2<td>[sqlite3changeset_apply_v2] 
 **   <tr><td>sqlite3changeset_concat_strm<td>[sqlite3changeset_concat] 
 **   <tr><td>sqlite3changeset_invert_strm<td>[sqlite3changeset_invert] 
 **   <tr><td>sqlite3changeset_start_strm<td>[sqlite3changeset_start] 
@@ -1497,7 +1524,8 @@ int sqlite3changeset_apply_v2_strm(
     sqlite3_changeset_iter *p     /* Handle describing change and conflict */
   ),
   void *pCtx,                     /* First argument passed to xConflict */
-  void **ppRebase, int *pnRebase
+  void **ppRebase, int *pnRebase,
+  int flags
 );
 int sqlite3changeset_concat_strm(
   int (*xInputA)(void *pIn, void *pData, int *pnData),
index bdd144b5fc22b81a2d7c2c8c9f1a5973f4f97761..3b6c24fd118c938a4be377d02e800ce8dc75f33c 100644 (file)
@@ -731,18 +731,34 @@ static int SQLITE_TCLAPI testSqlite3changesetApply(
   TestStreamInput sStr;
   void *pRebase = 0;
   int nRebase = 0;
+  int flags = 0;                  /* Flags for apply_v2() */
 
   memset(&sStr, 0, sizeof(sStr));
   sStr.nStream = test_tcl_integer(interp, SESSION_STREAM_TCL_VAR);
 
+  /* Check for the -nosavepoint flag */
+  if( bV2 && objc>1 ){
+    const char *z1 = Tcl_GetString(objv[1]);
+    int n = strlen(z1);
+    if( n>1 && n<=12 && 0==sqlite3_strnicmp("-nosavepoint", z1, n) ){
+      flags = SQLITE_CHANGESETAPPLY_NOSAVEPOINT;
+      objc--;
+      objv++;
+    }
+  }
+
   if( objc!=4 && objc!=5 ){
-    Tcl_WrongNumArgs(interp, 1, objv, 
-        "DB CHANGESET CONFLICT-SCRIPT ?FILTER-SCRIPT?"
-    );
+    const char *zMsg;
+    if( bV2 ){
+      zMsg = "?-nosavepoint? DB CHANGESET CONFLICT-SCRIPT ?FILTER-SCRIPT?";
+    }else{
+      zMsg = "DB CHANGESET CONFLICT-SCRIPT ?FILTER-SCRIPT?";
+    }
+    Tcl_WrongNumArgs(interp, 1, objv, zMsg);
     return TCL_ERROR;
   }
   if( 0==Tcl_GetCommandInfo(interp, Tcl_GetString(objv[1]), &info) ){
-    Tcl_AppendResult(interp, "no such handle: ", Tcl_GetString(objv[2]), 0);
+    Tcl_AppendResult(interp, "no such handle: ", Tcl_GetString(objv[1]), 0);
     return TCL_ERROR;
   }
   db = *(sqlite3 **)info.objClientData;
@@ -759,7 +775,7 @@ static int SQLITE_TCLAPI testSqlite3changesetApply(
     }else{
       rc = sqlite3changeset_apply_v2(db, nChangeset, pChangeset, 
           (objc==5)?test_filter_handler:0, test_conflict_handler, (void *)&ctx,
-          &pRebase, &nRebase
+          &pRebase, &nRebase, flags
       );
     }
   }else{
@@ -774,7 +790,7 @@ static int SQLITE_TCLAPI testSqlite3changesetApply(
       rc = sqlite3changeset_apply_v2_strm(db, testStreamInput, (void*)&sStr,
           (objc==5) ? test_filter_handler : 0, 
           test_conflict_handler, (void *)&ctx,
-          &pRebase, &nRebase
+          &pRebase, &nRebase, flags
       );
     }
   }
index 2dcc7b599f2889a11b530a41593c7bce624dabf4..99cfce3ff98007c7b18edc002c93328ce29052f5 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Fix\sa\s(harmless)\ssigned\sinteger\soverflow\swarning.
-D 2018-04-09T00:46:42.147
+C Add\sa\s"flags"\sparameter\sto\sexperimental\sAPI\ssqlite3changeset_apply_v2().\sAlso\nadd\sthe\sSQLITE_CHANGESETAPPLY_NOSAVEPOINT\sflag.
+D 2018-04-09T11:43:58.567
 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
 F Makefile.in 7016fc56c6b9bfe5daac4f34be8be38d8c0b5fab79ccbfb764d3b23bf1c6fff3
@@ -379,7 +379,7 @@ F ext/rtree/sqlite3rtree.h 9c5777af3d2921c7b4ae4954e8e5697502289d28
 F ext/rtree/tkt3363.test 142ab96eded44a3615ec79fba98c7bde7d0f96de
 F ext/rtree/viewrtree.tcl eea6224b3553599ae665b239bd827e182b466024
 F ext/session/changeset.c 4ccbaa4531944c24584bf6a61ba3a39c62b6267a
-F ext/session/session1.test 736d7ff178662f0b717c37f46531b84a5ce0210ccb0c4edf629c55dbcbbc3ea1
+F ext/session/session1.test 4532116484f525110eb4cfff7030c59354c0cde9def4d109466b0df2b35ad5cc
 F ext/session/session2.test 284de45abae4cc1082bc52012ee81521d5ac58e0
 F ext/session/session3.test ce9ce3dfa489473987f899e9f6a0f2db9bde3479
 F ext/session/session4.test 6778997065b44d99c51ff9cece047ff9244a32856b328735ae27ddef68979c40
@@ -404,9 +404,9 @@ F ext/session/sessionfault2.test 555a8504de03d59b369ef20209585da5aeb2671dedabc45
 F ext/session/sessionrebase.test 4e1bcfd26fd8ed8ac571746f56cceeb45184f4d65490ea0d405227cfc8a9cba8
 F ext/session/sessionstat1.test 41cd97c2e48619a41cdf8ae749e1b25f34719de638689221aa43971be693bf4e
 F ext/session/sessionwor.test 2f3744236dc8b170a695b7d8ddc8c743c7e79fdc
-F ext/session/sqlite3session.c 305010d5bc85f362584bfb72a5efea30e8826f13c44ab9dddd19484d627e2b5a
-F ext/session/sqlite3session.h 85fd2dc3df1532b0695beb345e2ff375c2745a4654b405fcbe33afa18baa6cc7
-F ext/session/test_session.c f253742ea01b089326f189b5ae15a5b55c1c9e97452e4a195ee759ba51b404d5
+F ext/session/sqlite3session.c 2d29bbd888599b94b2c8b31ff433675e008273a4d225b336508b18e6187fec1d
+F ext/session/sqlite3session.h c01820d5b6e73e86d88008f4d1c1c7dfb83422963018292b864028a0400ceccf
+F ext/session/test_session.c dba36c6c0153b22501112d3e8882b5c946cf617c955153b6712bd2f8ba1428c0
 F ext/userauth/sqlite3userauth.h 7f3ea8c4686db8e40b0a0e7a8e0b00fac13aa7a3
 F ext/userauth/user-auth.txt e6641021a9210364665fe625d067617d03f27b04
 F ext/userauth/userauth.c 3410be31283abba70255d71fd24734e017a4497f
@@ -1717,7 +1717,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
 F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
 F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
-P 602fbd8149b53d8f0e9a223cc1aec912e7df03fca35071e8d707776ce225371c
-R f3b5c5a80988cc5d56eaf38f6939e0a5
-U drh
-Z c4a7043373b61a2ea17d642e99e4923e
+P 43c2c60caf0ba105f57e32fc7fe6938dc29538c04d477a35b9b8bba2fb8fdd07
+R 21c05c965caaf57e504e2c1c78a6bf82
+U dan
+Z 920b588edeffe44ebf0eb4cdf592b8b3
index 78aa03eb249b753b79ceab2451c40619f9252f44..3bfe6363f7fdd81f90e7bb983829270052d06b0c 100644 (file)
@@ -1 +1 @@
-43c2c60caf0ba105f57e32fc7fe6938dc29538c04d477a35b9b8bba2fb8fdd07
\ No newline at end of file
+3d29631fa7270b18f51de8cfe1285959e42ad9a673d5d2d5c016126f5f1b31a8
\ No newline at end of file