interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / ext / wasm / demo-worker1-promiser.c-pp.js

287 lines · 9038 bytes Raw Blame History
1/*
2 2022-08-23
3
4 The author disclaims copyright to this source code. In place of a
5 legal notice, here is a blessing:
6
7 * May you do good and not evil.
8 * May you find forgiveness for yourself and forgive others.
9 * May you share freely, never taking more than you give.
10
11 ***********************************************************************
12
13 Demonstration of the sqlite3 Worker API #1 Promiser: a Promise-based
14 proxy for for the sqlite3 Worker #1 API.
15*/
16//#if target:es6-module
17import {default as promiserFactory} from "./jswasm/sqlite3-worker1-promiser.mjs";
18//#else
19"use strict";
20const promiserFactory = sqlite3Worker1Promiser.v2;
21//#/if
22(async function(){
23 const T = globalThis.SqliteTestUtil;
24 const eOutput = document.querySelector('#test-output');
25 const warn = console.warn.bind(console);
26 const error = console.error.bind(console);
27 const log = console.log.bind(console);
28 const logHtml = async function(cssClass,...args){
29 log.apply(this, args);
30 const ln = document.createElement('div');
31 if(cssClass) ln.classList.add(cssClass);
32 ln.append(document.createTextNode(args.join(' ')));
33 eOutput.append(ln);
34 };
35
36 let startTime;
37 const testCount = async ()=>{
38 logHtml("","Total test count:",T.counter+". Total time =",(performance.now() - startTime),"ms");
39 };
40
41 const promiserConfig = {
42//#if not target:es6-module
43 /**
44 The v1 interfaces uses an onready function. The v2 interface optionally
45 accepts one but does not require it. If provided, it is called _before_
46 the promise is resolved, and the promise is rejected if onready() throws.
47 */
48 onready: function(f){
49 /* f === the function returned by promiserFactory().
50 Ostensibly (f === workerPromise) but this function is
51 called before the promiserFactory() Promise resolves, so
52 before workerPromise is set. */
53 console.warn("This is the v2 interface - you don't need an onready() function.");
54 },
55//#/if
56 debug: 1 ? undefined : (...args)=>console.debug('worker debug',...args),
57 onunhandled: function(ev){
58 error("Unhandled worker message:",ev.data);
59 },
60 onerror: function(ev){
61 error("worker1 error:",ev);
62 }
63 };
64 const workerPromise = await promiserFactory(promiserConfig)
65 .then((func)=>{
66 console.log("Init complete. Starting tests momentarily.");
67 globalThis.sqlite3TestModule.setStatus(null)/*hide the HTML-side is-loading spinner*/;
68 return func;
69 });
70
71 const wtest = async function(msgType, msgArgs, callback){
72 if(2===arguments.length && 'function'===typeof msgArgs){
73 callback = msgArgs;
74 msgArgs = undefined;
75 }
76 const p = 1
77 ? workerPromise({type: msgType, args:msgArgs})
78 : workerPromise(msgType, msgArgs);
79 return callback ? p.then(callback).finally(testCount) : p;
80 };
81
82 let sqConfig;
83 const runTests = async function(){
84 const dbFilename = '/testing2.sqlite3';
85 startTime = performance.now();
86
87 await wtest('config-get', (ev)=>{
88 const r = ev.result;
89 log('sqlite3.config subset:', r);
90 T.assert('boolean' === typeof r.bigIntEnabled);
91 sqConfig = r;
92 });
93 logHtml('',
94 "Sending 'open' message and waiting for its response before continuing...");
95
96 await wtest('open', {
97 filename: dbFilename,
98 simulateError: 0 /* if true, fail the 'open' */,
99 }, function(ev){
100 const r = ev.result;
101 log("then open result",r);
102 T.assert(ev.dbId === r.dbId)
103 .assert(ev.messageId)
104 .assert('string' === typeof r.vfs);
105 promiserConfig.dbId = ev.dbId;
106 }).then(runTests2);
107 };
108
109 const runTests2 = async function(){
110 const mustNotReach = ()=>toss("This is not supposed to be reached.");
111
112 await wtest('exec',{
113 sql: ["create table t(a,b)",
114 "insert into t(a,b) values(1,2),(3,4),(5,6)"
115 ].join(';'),
116 resultRows: [], columnNames: [],
117 lastInsertRowId: true,
118 countChanges: sqConfig.bigIntEnabled ? 64 : true
119 }, function(ev){
120 ev = ev.result;
121 T.assert(0===ev.resultRows.length)
122 .assert(0===ev.columnNames.length)
123 .assert(sqConfig.bigIntEnabled
124 ? (3n===ev.changeCount)
125 : (3===ev.changeCount))
126 .assert('bigint'===typeof ev.lastInsertRowId)
127 .assert(ev.lastInsertRowId>=3);
128 });
129
130 await wtest('exec',{
131 sql: 'select a a, b b from t order by a',
132 resultRows: [], columnNames: [],
133 }, function(ev){
134 ev = ev.result;
135 T.assert(3===ev.resultRows.length)
136 .assert(1===ev.resultRows[0][0])
137 .assert(6===ev.resultRows[2][1])
138 .assert(2===ev.columnNames.length)
139 .assert('b'===ev.columnNames[1]);
140 });
141
142 await wtest('exec',{
143 sql: 'select a a, b b from t order by a',
144 resultRows: [], columnNames: [],
145 rowMode: 'object',
146 countChanges: true
147 }, function(ev){
148 ev = ev.result;
149 T.assert(3===ev.resultRows.length)
150 .assert(1===ev.resultRows[0].a)
151 .assert(6===ev.resultRows[2].b)
152 .assert(0===ev.changeCount);
153 });
154
155 await wtest(
156 'exec',
157 {sql:'intentional_error'},
158 mustNotReach
159 ).catch((e)=>{
160 warn("Intentional error:",e);
161 });
162
163 await wtest('exec',{
164 sql:'select 1 union all select 3',
165 resultRows: []
166 }, function(ev){
167 ev = ev.result;
168 T.assert(2 === ev.resultRows.length)
169 .assert(1 === ev.resultRows[0][0])
170 .assert(3 === ev.resultRows[1][0])
171 .assert(undefined === ev.changeCount);
172 });
173
174 const resultRowTest1 = function f(ev){
175 if(undefined === f.counter) f.counter = 0;
176 if(null === ev.rowNumber){
177 /* End of result set. */
178 T.assert(undefined === ev.row)
179 .assert(2===ev.columnNames.length)
180 .assert('a'===ev.columnNames[0])
181 .assert('B'===ev.columnNames[1]);
182 }else{
183 T.assert(ev.rowNumber > 0);
184 ++f.counter;
185 }
186 log("exec() result row:",ev);
187 T.assert(null === ev.rowNumber || 'number' === typeof ev.row.B);
188 };
189 await wtest('exec',{
190 sql: 'select a a, b B from t order by a limit 3',
191 callback: resultRowTest1,
192 rowMode: 'object'
193 }, function(ev){
194 T.assert(3===resultRowTest1.counter);
195 resultRowTest1.counter = 0;
196 });
197
198 const resultRowTest2 = function f(ev){
199 if(null === ev.rowNumber){
200 /* End of result set. */
201 T.assert(undefined === ev.row)
202 .assert(1===ev.columnNames.length)
203 .assert('a'===ev.columnNames[0])
204 }else{
205 T.assert(ev.rowNumber > 0);
206 f.counter = ev.rowNumber;
207 }
208 log("exec() result row:",ev);
209 T.assert(null === ev.rowNumber || 'number' === typeof ev.row);
210 };
211 await wtest('exec',{
212 sql: 'select a a from t limit 3',
213 callback: resultRowTest2,
214 rowMode: 0
215 }, function(ev){
216 T.assert(3===resultRowTest2.counter);
217 });
218
219 const resultRowTest3 = function f(ev){
220 if(null === ev.rowNumber){
221 T.assert(3===ev.columnNames.length)
222 .assert('foo'===ev.columnNames[0])
223 .assert('bar'===ev.columnNames[1])
224 .assert('baz'===ev.columnNames[2]);
225 }else{
226 f.counter = ev.rowNumber;
227 T.assert('number' === typeof ev.row);
228 }
229 };
230 await wtest('exec',{
231 sql: "select 'foo' foo, a bar, 'baz' baz from t limit 2",
232 callback: resultRowTest3,
233 columnNames: [],
234 rowMode: '$bar'
235 }, function(ev){
236 log("exec() result row:",ev);
237 T.assert(2===resultRowTest3.counter);
238 });
239
240 await wtest('exec',{
241 sql:[
242 'pragma foreign_keys=0;',
243 // ^^^ arbitrary query with no result columns
244 'select a, b from t order by a desc; select a from t;'
245 // exec() only honors SELECT results from the first
246 // statement with result columns (regardless of whether
247 // it has any rows).
248 ],
249 rowMode: 1,
250 resultRows: []
251 },function(ev){
252 const rows = ev.result.resultRows;
253 T.assert(3===rows.length).
254 assert(6===rows[0]);
255 });
256
257 await wtest('exec',{sql: 'delete from t where a>3'});
258
259 await wtest('exec',{
260 sql: 'select count(a) from t',
261 resultRows: []
262 },function(ev){
263 ev = ev.result;
264 T.assert(1===ev.resultRows.length)
265 .assert(2===ev.resultRows[0][0]);
266 });
267
268 await wtest('export', function(ev){
269 ev = ev.result;
270 T.assert('string' === typeof ev.filename)
271 .assert(ev.byteArray instanceof Uint8Array)
272 .assert(ev.byteArray.length > 1024)
273 .assert('application/x-sqlite3' === ev.mimetype);
274 });
275
276 /***** close() tests must come last. *****/
277 await wtest('close',{},function(ev){
278 T.assert('string' === typeof ev.result.filename);
279 });
280
281 await wtest('close', (ev)=>{
282 T.assert(undefined === ev.result.filename);
283 }).finally(()=>logHtml('',"That's all, folks!"));
284 }/*runTests2()*/;
285
286 runTests();
287})();