4 The author disclaims copyright to this source code. In place of a
5 legal notice, here is a blessing:
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.
11 ***********************************************************************
13 A basic test script for sqlite3.wasm with kvvfs support. This file
14 must be run in main JS thread and sqlite3.js must have been loaded
19 const T
= self
.SqliteTestUtil
;
20 const toss = function(...args
){throw new Error(args
.join(' '))};
21 const debug
= console
.debug
.bind(console
);
22 const eOutput
= document
.querySelector('#test-output');
23 const logC
= console
.log
.bind(console
)
24 const logE = function(domElement
){
25 eOutput
.append(domElement
);
27 const logHtml = function(cssClass
,...args
){
28 const ln
= document
.createElement('div');
29 if(cssClass
) ln
.classList
.add(cssClass
);
30 ln
.append(document
.createTextNode(args
.join(' ')));
33 const log = function(...args
){
37 const warn = function(...args
){
38 logHtml('warning',...args
);
40 const error = function(...args
){
41 logHtml('error',...args
);
44 const runTests = function(sqlite3
){
45 const capi
= sqlite3
.capi
,
48 log("Loaded module:",capi
.sqlite3_libversion(), capi
.sqlite3_sourceid());
49 T
.assert( 0 !== capi
.sqlite3_vfs_find(null) );
50 if(!capi
.sqlite3_vfs_find('kvvfs')){
51 error("This build is not kvvfs-capable.");
55 const dbStorage
= 0 ? 'session' : 'local';
56 const theStore
= 's'===dbStorage
[0] ? sessionStorage
: localStorage
;
57 const db
= new oo
.JsStorageDb( dbStorage
);
58 // Or: oo.DB(dbStorage, 'c', 'kvvfs')
59 log("db.storageSize():",db
.storageSize());
60 document
.querySelector('#btn-clear-storage').addEventListener('click',function(){
61 const sz
= db
.clearStorage();
62 log("kvvfs",db
.filename
+"Storage cleared:",sz
,"entries.");
64 document
.querySelector('#btn-clear-log').addEventListener('click',function(){
65 eOutput
.innerText
= '';
67 document
.querySelector('#btn-init-db').addEventListener('click',function(){
71 sql
: ["drop table if exists t;",
72 "create table if not exists t(a);",
73 "insert into t(a) values(?),(?),(?)"],
74 bind
: [performance
.now() >> 0,
75 (performance
.now() * 2) >> 0,
76 (performance
.now() / 2) >> 0],
79 console
.log("saveSql =",saveSql
,theStore
);
80 log("DB (re)initialized.");
85 const btnSelect
= document
.querySelector('#btn-select1');
86 btnSelect
.addEventListener('click',function(){
90 sql
: "select * from t order by a",
98 document
.querySelector('#btn-storage-size').addEventListener('click',function(){
99 log("size.storageSize(",dbStorage
,") says", db
.storageSize(),
102 log("Storage backend:",db
.filename
);
103 if(0===db
.selectValue('select count(*) from sqlite_master')){
104 log("DB is empty. Use the init button to populate it.");
106 log("DB contains data from a previous session. Use the Clear Ctorage button to delete it.");
111 sqlite3InitModule(self
.sqlite3TestModule
).then((sqlite3
)=>{