sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
sqlite / ext / wasm / scratchpad-wasmfs.mjs
| 1 | /* |
| 2 | 2022-05-22 |
| 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 | A basic test script for sqlite3-api.js. This file must be run in |
| 14 | main JS thread and sqlite3.js must have been loaded before it. |
| 15 | */ |
| 16 | import sqlite3InitModule from './jswasm/sqlite3-wasmfs.mjs'; |
| 17 | //console.log('sqlite3InitModule =',sqlite3InitModule); |
| 18 | const toss = function(...args){throw new Error(args.join(' '))}; |
| 19 | const log = console.log.bind(console), |
| 20 | warn = console.warn.bind(console), |
| 21 | error = console.error.bind(console); |
| 22 | |
| 23 | const stdout = log; |
| 24 | const stderr = error; |
| 25 | |
| 26 | const test1 = function(db){ |
| 27 | db.exec("create table if not exists t(a);") |
| 28 | .transaction(function(db){ |
| 29 | db.prepare("insert into t(a) values(?)") |
| 30 | .bind(new Date().getTime()) |
| 31 | .stepFinalize(); |
| 32 | stdout("Number of values in table t:", |
| 33 | db.selectValue("select count(*) from t")); |
| 34 | }); |
| 35 | }; |
| 36 | |
| 37 | const runTests = function(sqlite3){ |
| 38 | const capi = sqlite3.capi, |
| 39 | oo = sqlite3.oo1, |
| 40 | wasm = sqlite3.wasm; |
| 41 | stdout("Loaded module:",sqlite3); |
| 42 | stdout("Loaded sqlite3:",capi.sqlite3_libversion(), capi.sqlite3_sourceid()); |
| 43 | const persistentDir = capi.sqlite3_wasmfs_opfs_dir(); |
| 44 | if(persistentDir){ |
| 45 | stdout("Persistent storage dir:",persistentDir); |
| 46 | }else{ |
| 47 | stderr("No persistent storage available."); |
| 48 | } |
| 49 | const startTime = performance.now(); |
| 50 | let db; |
| 51 | try { |
| 52 | db = new oo.DB(persistentDir+'/foo.db'); |
| 53 | stdout("DB filename:",db.filename); |
| 54 | const banner1 = '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>', |
| 55 | banner2 = '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<'; |
| 56 | [ |
| 57 | test1 |
| 58 | ].forEach((f)=>{ |
| 59 | const n = performance.now(); |
| 60 | stdout(banner1,"Running",f.name+"()..."); |
| 61 | f(db, sqlite3); |
| 62 | stdout(banner2,f.name+"() took ",(performance.now() - n),"ms"); |
| 63 | }); |
| 64 | }finally{ |
| 65 | if(db) db.close(); |
| 66 | } |
| 67 | stdout("Total test time:",(performance.now() - startTime),"ms"); |
| 68 | }; |
| 69 | |
| 70 | sqlite3InitModule().then(runTests); |