From: stephan Date: Tue, 24 May 2022 19:01:21 +0000 (+0000) Subject: fiddle: initial work on loading a client-side db file. Works but requires some cleanu... X-Git-Tag: version-3.39.0~102^2~3 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=de1e02ee52ddcd909024bea66643415e2471416c;p=thirdparty%2Fsqlite.git fiddle: initial work on loading a client-side db file. Works but requires some cleanup. Export is not yet implemented. FossilOrigin-Name: 0fa8378c006fcf2311772d36cf2e3c2cd8e8648f671de89ee9832e2e1a06ef49 --- diff --git a/ext/fiddle/fiddle-worker.js b/ext/fiddle/fiddle-worker.js index 16da63606e..6a687e42cb 100644 --- a/ext/fiddle/fiddle-worker.js +++ b/ext/fiddle/fiddle-worker.js @@ -159,30 +159,73 @@ self.Module = { } }; -const shellExec = function f(sql){ - if(!f._) f._ = Module.cwrap('fiddle_exec', null, ['string']); - if(Module._isDead){ - wMsg('stderr', "shell module has exit()ed. Cannot run SQL."); - return; - } - wMsg('working','start'); - try { - if(f._running) wMsg('stderr','Cannot run multiple commands concurrently.'); - else{ - f._running = true; - f._(sql); +const Sqlite3Shell = { + exec: function f(sql){ + if(!f._) f._ = Module.cwrap('fiddle_exec', null, ['string']); + if(Module._isDead){ + wMsg('stderr', "shell module has exit()ed. Cannot run SQL."); + return; + } + wMsg('working','start'); + try { + if(f._running) wMsg('stderr','Cannot run multiple commands concurrently.'); + else{ + f._running = true; + f._(sql); + } + } finally { + wMsg('working','end'); + delete f._running; } - } finally { - wMsg('working','end'); - delete f._running; + }, + /* Interrupt can't work: this Worker is tied up working, so won't get the + interrupt event which would be needed to perform the interrupt. */ + interrupt: function f(){ + if(!f._) f._ = Module.cwrap('fiddle_interrupt', null); + wMsg('stdout',"Requesting interrupt."); + f._(); } }; -self.onmessage = function(ev){ +self.onmessage = function f(ev){ ev = ev.data; + if(!f.cache){ + f.cache = { + prevFilename: null + }; + } //console.debug("worker: onmessage.data",ev); switch(ev.type){ - case 'shellExec': shellExec(ev.data); return; + case 'shellExec': Sqlite3Shell.exec(ev.data); return; + case 'interrupt': Sqlite3Shell.interrupt(); return; + case 'open': { + /* Expects: { + buffer: ArrayBuffer | Uint8Array, + filename: for logging/informational purposes only + } */ + const opt = ev.data; + let buffer = opt.buffer; + if(buffer instanceof Uint8Array){ + }else if(buffer instanceof ArrayBuffer){ + buffer = new Uint8Array(buffer); + }else{ + wMsg('stderr',"'open' expects {buffer:Uint8Array} containing an uploaded db."); + return; + } + if(f.cache.prevFilename){ + FS.unlink(f.cache.prevFilename); + /* Noting that it might not actually be removed until + the current db handle closes it. */ + f.cache.prevFilename = null; + } + const fn = "db-"+((Math.random() * 10000000) | 0)+ + "-"+((Math.random() * 10000000) | 0)+".sqlite3"; + FS.createDataFile("/", fn, buffer, true, true); + f.cache.prevFilename = fn; + Sqlite3Shell.exec(".open /"+fn); + wMsg('stdout',"Replaced DB with "+(opt.filename || fn)+"."); + return; + } }; console.warn("Unknown fiddle-worker message type:",ev); }; diff --git a/ext/fiddle/fiddle.html b/ext/fiddle/fiddle.html index 52ae2467ec..8db780e4d7 100644 --- a/ext/fiddle/fiddle.html +++ b/ext/fiddle/fiddle.html @@ -76,6 +76,10 @@ fieldset > legend { padding: 0 0.5em; } + fieldset.options > div { + display: flex; + flex-wrap: wrap; + } span.labeled-input { padding: 0.25em; margin: 0.25em 0.5em; @@ -108,7 +112,7 @@ } #view-split { display: flex; - flex-direction: column; + flex-direction: column-reverse; } @@ -161,6 +165,10 @@ data-config='autoClearOutput'> + + + +
@@ -185,6 +193,11 @@ SELECT * FROM t; placeholder="Shell output.">
+ +