From: stephan Date: Mon, 19 Sep 2022 03:57:31 +0000 (+0000) Subject: Rename demo-oo1.* to demo-123.* and add demo-123-worker.html, which runs the same... X-Git-Tag: version-3.40.0~169^2~98 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=ac51eb77546653a6e0563adc0f3460ae69744485;p=thirdparty%2Fsqlite.git Rename demo-oo1.* to demo-123.* and add demo-123-worker.html, which runs the same demo via a Worker. Doc typo fixes. FossilOrigin-Name: 2e4a005bd35424caeaa99ace23162cf79e2ebdb159475ffad92b85dc864ad764 --- diff --git a/ext/wasm/api/sqlite3-api-worker1.js b/ext/wasm/api/sqlite3-api-worker1.js index 97f2677e6c..b41a837e9f 100644 --- a/ext/wasm/api/sqlite3-api-worker1.js +++ b/ext/wasm/api/sqlite3-api-worker1.js @@ -326,13 +326,12 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){ sqlite3.initWorker1API = function(){ 'use strict'; const toss = (...args)=>{throw new Error(args.join(' '))}; - if('function' !== typeof importScripts){ - toss("Cannot initalize the sqlite3 worker API in the main thread."); + if(self.window === self || 'function' !== typeof importScripts){ + toss("initWorker1API() must be run from a Worker thread."); } const self = this.self; const sqlite3 = this.sqlite3 || toss("Missing this.sqlite3 object."); - const SQLite3 = sqlite3.oo1 || toss("Missing this.sqlite3.oo1 OO API."); - const DB = SQLite3.DB; + const DB = sqlite3.oo1.DB; /** Returns the app-wide unique ID for the given db, creating one if diff --git a/ext/wasm/demo-123-worker.html b/ext/wasm/demo-123-worker.html new file mode 100644 index 0000000000..7480037a16 --- /dev/null +++ b/ext/wasm/demo-123-worker.html @@ -0,0 +1,40 @@ + + + + + + + Hello, sqlite3 + + + +

1-2-sqlite3 worker demo

+ + + diff --git a/ext/wasm/demo-123.html b/ext/wasm/demo-123.html new file mode 100644 index 0000000000..25fa974910 --- /dev/null +++ b/ext/wasm/demo-123.html @@ -0,0 +1,24 @@ + + + + + + + Hello, sqlite3 + + + +

1-2-sqlite3 demo

+ + + + diff --git a/ext/wasm/demo-oo1.js b/ext/wasm/demo-123.js similarity index 67% rename from ext/wasm/demo-oo1.js rename to ext/wasm/demo-123.js index 4564fe0ddd..7a4843a4d9 100644 --- a/ext/wasm/demo-oo1.js +++ b/ext/wasm/demo-123.js @@ -1,5 +1,5 @@ /* - 2022-08-16 + 2022-09-19 The author disclaims copyright to this source code. In place of a legal notice, here is a blessing: @@ -10,34 +10,48 @@ *********************************************************************** - A basic demonstration of the SQLite3 OO API #1, shorn of assertions - and the like to improve readability. + A basic demonstration of the SQLite3 OO API. */ 'use strict'; (function(){ - const toss = function(...args){throw new Error(args.join(' '))}; - const debug = console.debug.bind(console), - log = console.log.bind(console), - warn = console.warn.bind(console), - error = console.error.bind(console); + /** + Set up our output channel differently depending + on whether we are running in a worker thread or + the main (UI) thread. + */ + let logHtml; + if(self.window === self /* UI thread */){ + logHtml = function(cssClass,...args){ + const ln = document.createElement('div'); + if(cssClass) ln.classList.add(cssClass); + ln.append(document.createTextNode(args.join(' '))); + document.body.append(ln); + }; + }else{ /* Worker thread */ + logHtml = function(cssClass,...args){ + postMessage({ + type:'log', + payload:{cssClass, args} + }); + }; + } + const log = (...args)=>logHtml('',...args); + const warn = (...args)=>logHtml('warning',...args); + const error = (...args)=>logHtml('error',...args); const demo1 = function(sqlite3){ - const capi = sqlite3.capi, - oo = sqlite3.oo1, - wasm = capi.wasm; - - const dbName = ( - 0 ? "" : capi.sqlite3_web_persistent_dir() - )+"/mydb.sqlite3" - if(0 && capi.sqlite3_web_persistent_dir()){ - capi.wasm.sqlite3_wasm_vfs_unlink(dbName); - } - const db = new oo.DB(dbName); - log("db =",db.filename); + const capi = sqlite3.capi/*C-style API*/, + oo = sqlite3.oo1/*high-level OO API*/; + log("sqlite3 version",capi.sqlite3_libversion(), capi.sqlite3_sourceid()); + const db = new oo.DB("/mydb.sqlite3"); + log("transient b =",db.filename); /** Never(!) rely on garbage collection to clean up DBs and (especially) statements. Always wrap their lifetimes in - try/finally construct... + try/finally construct, as demonstrated below. By and large, + client code can avoid lifetime-related complications of + prepared statement objects by using the DB.exec() method for + SQL execution. */ try { log("Create a table..."); @@ -48,10 +62,13 @@ // ... numerous other options ... }); // SQL can be either a string or a byte array + // or an array of strings which get concatenated + // together as-is (so be sure to end each statement + // with a semicolon). log("Insert some data using exec()..."); let i; - for( i = 1; i <= 5; ++i ){ + for( i = 20; i <= 25; ++i ){ db.exec({ sql: "insert into t(a,b) values (?,?)", // bind by parameter index... @@ -60,7 +77,7 @@ db.exec({ sql: "insert into t(a,b) values ($a,$b)", // bind by parameter name... - bind: {$a: i * 3, $b: i * 4} + bind: {$a: i * 10, $b: i * 20} }); } @@ -93,7 +110,7 @@ sql: "select a as aa, b as bb from t order by aa limit 3", rowMode: 'object', callback: function(row){ - log("row ",++this.counter,"=",row); + log("row ",++this.counter,"=",JSON.stringify(row)); }.bind({counter: 0}) }); @@ -115,6 +132,15 @@ }.bind({counter: 0}) }); + log("Query data with exec() using rowMode $COLNAME (result column name)..."); + db.exec({ + sql: "select a a, b from t order by a limit 3", + rowMode: '$a', + callback: function(value){ + log("row ",++this.counter,"a =",value); + }.bind({counter: 0}) + }); + log("Query data with exec() without a callback..."); let resultRows = []; db.exec({ @@ -122,7 +148,7 @@ rowMode: 'object', resultRows: resultRows }); - log("Result rows:",resultRows); + log("Result rows:",JSON.stringify(resultRows,undefined,2)); log("Create a scalar UDF..."); db.createFunction({ @@ -144,9 +170,12 @@ }); log("Result column names:",columnNames); - if(0){ - warn("UDF will throw because of incorrect arg count..."); + try{ + log("The following use of the twice() UDF will", + "fail because of incorrect arg count..."); db.exec("select twice(1,2,3)"); + }catch(e){ + warn("Got expected exception:",e.message); } try { @@ -170,7 +199,7 @@ log("In savepoint: count(*) from t =",db.selectValue("select count(*) from t")); D.savepoint(function(DD){ const rows = []; - D.exec({ + DD.exec({ sql: ["insert into t(a,b) values(99,100);", "select count(*) from t"], rowMode: 0, @@ -188,17 +217,17 @@ throw e; } } - }finally{ db.close(); } + log("That's all, folks!"); + /** - Misc. DB features: + Some of the features of the OO API not demonstrated above... - get change count (total or statement-local, 32- or 64-bit) - - get its file name - - selectValue() takes SQL and returns first column of first row. + - get a DB's file name Misc. Stmt features: @@ -213,20 +242,21 @@ */ }/*demo1()*/; - const runDemos = function(Module){ - //log("Module.sqlite3",Module); - const sqlite3 = Module.sqlite3, - capi = sqlite3.capi; - log("Loaded module:",capi.sqlite3_libversion(), capi.sqlite3_sourceid()); - log("sqlite3 namespace:",sqlite3); + log("Loading and initializing sqlite3 module..."); + if(self.window!==self) /*worker thread*/{ + importScripts("sqlite3.js"); + } + self.sqlite3InitModule({ + // We can redirect any stdout/stderr from the module + // like so... + print: log, + printErr: error + }).then(function(EmscriptenModule){ + log("Done initializing. Running demo..."); try { - demo1(sqlite3); + demo1(EmscriptenModule.sqlite3); }catch(e){ error("Exception:",e.message); - throw e; } - }; - - //self.sqlite3TestModule.sqlite3ApiConfig.persistentDirName = "/hi"; - self.sqlite3TestModule.initSqlite3().then(runDemos); + }); })(); diff --git a/ext/wasm/demo-oo1.html b/ext/wasm/demo-oo1.html deleted file mode 100644 index 9b6e8cbfa3..0000000000 --- a/ext/wasm/demo-oo1.html +++ /dev/null @@ -1,34 +0,0 @@ - - - - - - - - - sqlite3-api OO #1 Demo - - -
sqlite3-api OO #1 Demo
- -
-
-
Initializing app...
-
- On a slow internet connection this may take a moment. If this - message displays for "a long time", intialization may have - failed and the JavaScript console may contain clues as to why. -
-
-
Downloading...
-
- -
-
Most stuff on this page happens in the dev console.
-
-
- - - - - diff --git a/ext/wasm/index.html b/ext/wasm/index.html index d13f9bb7f3..68b43d7652 100644 --- a/ext/wasm/index.html +++ b/ext/wasm/index.html @@ -22,20 +22,30 @@ the web server emit the so-called COOP and COEP headers. The default build of althttpd does not. +
  • Any OPFS-related pages require very recent + version of Chrome or Chromium (v102 at least, possibly + newer). OPFS support in the other major browsers is + pending.
  • Whether or not WASMFS/OPFS support is enabled on any given page may depend on build-time options which are off by default because they currently (as of 2022-09-08) break - with Worker-based pages. + with Worker-based pages. Similarly, WASMFS does not work on + some platforms, e.g. Raspberry Pi 4.
  • The tests...