]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
wasm OO API #1: added DB.callInTransaction() and Stmt.stepFinalize().
authorstephan <stephan@noemail.net>
Sat, 13 Aug 2022 13:46:19 +0000 (13:46 +0000)
committerstephan <stephan@noemail.net>
Sat, 13 Aug 2022 13:46:19 +0000 (13:46 +0000)
FossilOrigin-Name: e37dddc1dd9c0530e4b1c6cb0ca7cba7451caa37734d383c9b47f378d7222242

ext/wasm/api/sqlite3-api-oo1.js
manifest
manifest.uuid

index 9e547339668aee13dc37f6fdc44b823db85b2160..b6e63d540a60728e4b3d05b81a9559e157c8e278 100644 (file)
             while(stmt.step()){
               stmt._isLocked = true;
               const row = arg.cbArg(stmt);
-              if(callback) callback(row, stmt);
               if(resultRows) resultRows.push(row);
+              if(callback) callback(row, stmt);
               stmt._isLocked = false;
             }
             rowMode = undefined;
       return this.pointer ? Object.keys(__stmtMap.get(this)).length : 0;
     },
 
+    /**
+       Starts a transaction, calls the given callback, and then either
+       rolls back or commits the transaction, depending on whether the
+       callback throw. The callback is pass this db object as its only
+       argument.  On success, returns the result of the callback.
+       Throws on error.
+     */
+    callInTransaction: function(callback){
+      affirmDbOpen(this);
+      let err, rc;
+      this.exec("BEGIN");
+      try { rc = callback(this); }
+      catch(e){
+        err = e;
+        throw e;
+      }finally{
+        if(err) this.exec("ROLLBACK");
+        else this.exec("COMMIT");
+      }
+      return rc;
+    },
+
     /**
        This function currently does nothing and always throws.  It
        WILL BE REMOVED pending other refactoring, to eliminate a hard
           console.warn("Unsupported bind() argument type:",val);
           toss3("Unsupported bind() argument type: "+(typeof val));
     }
-    if(rc) checkDbRc(stmt.db.pointer, rc);
+    if(rc) DB.checkRc(stmt.db.pointer, rc);
     return stmt;
   };
 
       return this;
     },
     /**
-       Steps the statement one time. If the result indicates that
-       a row of data is available, true is returned.  If no row of
-       data is available, false is returned.  Throws on error.
+       Steps the statement one time. If the result indicates that a
+       row of data is available, a truthy value is returned.
+       If no row of data is available, a falsy
+       value is returned.  Throws on error.
     */
     step: function(){
       affirmUnlocked(this, 'step()');
             this._mayGet = false;
             console.warn("sqlite3_step() rc=",rc,"SQL =",
                          capi.sqlite3_sql(this.pointer));
-            checkDbRc(this.db.pointer, rc);
-      };
+            DB.checkRc(this.db.pointer, rc);
+      }
+    },
+    /**
+       Functions like step() except that
+       it finalizes this statement immediately after stepping unless
+       the step cannot be performed because the statement is
+       locked. Throws on error, but any error other than the
+       statement-is-locked case will also trigger finalization of this
+       statement.
+
+       On success, it returns true if the step indicated that a row of
+       data was available, else it returns false.
+
+       This is intended to simplify use cases such as:
+
+       ```
+       aDb.prepare("insert in foo(a) values(?)").bind(123).stepFinalize();
+       ```
+    */
+    stepFinalize: function(){
+      affirmUnlocked(this, 'step()');
+      const rc = capi.sqlite3_step(affirmStmtOpen(this).pointer);
+      switch(rc){
+          case capi.SQLITE_DONE: this.finalize(); return false;
+          case capi.SQLITE_ROW: this.finalize(); return true;
+          default:
+            this.finalize();
+            console.warn("sqlite3_step() rc=",rc,"SQL =",
+                         capi.sqlite3_sql(this.pointer));
+            DB.checkRc(this.db.pointer, rc);
+      }
     },
     /**
        Fetches the value from the given 0-based column index of
index 1c235509d7970119b4301bfe644bf3ec23d1ff91..e90ec6ca061977bae5774202a655718260b44d9c 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Corrected\sTextDecoder.decode()\susage\sto\srun\swhen\sits\sinput\sreferences\sa\sSharedArrayBuffer.
-D 2022-08-13T13:42:07.359
+C wasm\sOO\sAPI\s#1:\sadded\sDB.callInTransaction()\sand\sStmt.stepFinalize().
+D 2022-08-13T13:46:19.722
 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
 F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@@ -483,7 +483,7 @@ F ext/wasm/api/post-js-footer.js b64319261d920211b8700004d08b956a6c285f3b0bba814
 F ext/wasm/api/post-js-header.js 0e853b78db83cb1c06b01663549e0e8b4f377f12f5a2d9a4a06cb776c003880b
 F ext/wasm/api/sqlite3-api-cleanup.js 149fd63a0400cd1d69548887ffde2ed89c13283384a63c2e9fcfc695e38a9e11
 F ext/wasm/api/sqlite3-api-glue.js 82c09f49c69984009ba5af2b628e67cc26c5dd203d383cd3091d40dab4e6514b
-F ext/wasm/api/sqlite3-api-oo1.js e9612cb704c0563c5d71ed2a8dccd95bf6394fa4de3115d1b978dc269c49ab02
+F ext/wasm/api/sqlite3-api-oo1.js a3469bbb217b9787ba9aa6216423ec55cf9457fecefb9698e433d0e1cc4cc918
 F ext/wasm/api/sqlite3-api-opfs.js c93cdd14f81a26b3a64990515ee05c7e29827fbc8fba4e4c2fef3a37a984db89
 F ext/wasm/api/sqlite3-api-prologue.js 0fb0703d2d8ac89fa2d4dd8f9726b0ea226b8708ac34e5b482df046e147de0eb
 F ext/wasm/api/sqlite3-api-worker.js 1124f404ecdf3c14d9f829425cef778cd683911a9883f0809a463c3c7773c9fd
@@ -1999,8 +1999,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
 F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
 F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
-P b3a93ec75acb38535691d6eaceb5a1a218a5ee3f755a1e12c9255b90567fc795
-R 243b6e88d1ed7db99fd955f733548f1c
+P d4d773405c579e7efd95be8d81fe14d71218e62e44c523d38e02f89424ba6ce8
+R d6004d575cfc0551ba9a0fec437e8b0f
 U stephan
-Z ddf468f7ec78d048a46e361913445144
+Z 111c1d0636763c3347de9dc9f9b9e9af
 # Remove this line to create a well-formed Fossil manifest.
index ab645ba532d514652e9532e85275abfdc886fee3..d1493580ab77a029e63ad06705966ca1d290454a 100644 (file)
@@ -1 +1 @@
-d4d773405c579e7efd95be8d81fe14d71218e62e44c523d38e02f89424ba6ce8
\ No newline at end of file
+e37dddc1dd9c0530e4b1c6cb0ca7cba7451caa37734d383c9b47f378d7222242
\ No newline at end of file