2022-09-12 15:39:28 +03:00
|
|
|
/*
|
|
|
|
2022-09-12
|
|
|
|
|
|
|
|
The author disclaims copyright to this source code. In place of a
|
|
|
|
legal notice, here is a blessing:
|
|
|
|
|
|
|
|
* May you do good and not evil.
|
|
|
|
* May you find forgiveness for yourself and forgive others.
|
|
|
|
* May you share freely, never taking more than you give.
|
|
|
|
|
|
|
|
***********************************************************************
|
|
|
|
|
2022-09-19 16:16:35 +03:00
|
|
|
A basic test script for sqlite3.wasm with kvvfs support. This file
|
|
|
|
must be run in main JS thread and sqlite3.js must have been loaded
|
|
|
|
before it.
|
2022-09-12 15:39:28 +03:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
(function(){
|
|
|
|
const T = self.SqliteTestUtil;
|
|
|
|
const toss = function(...args){throw new Error(args.join(' '))};
|
|
|
|
const debug = console.debug.bind(console);
|
|
|
|
const eOutput = document.querySelector('#test-output');
|
2022-09-12 20:59:04 +03:00
|
|
|
const logC = console.log.bind(console)
|
|
|
|
const logE = function(domElement){
|
|
|
|
eOutput.append(domElement);
|
|
|
|
};
|
|
|
|
const logHtml = function(cssClass,...args){
|
2022-09-12 15:39:28 +03:00
|
|
|
const ln = document.createElement('div');
|
2022-09-12 20:59:04 +03:00
|
|
|
if(cssClass) ln.classList.add(cssClass);
|
2022-09-12 15:39:28 +03:00
|
|
|
ln.append(document.createTextNode(args.join(' ')));
|
2022-09-12 20:59:04 +03:00
|
|
|
logE(ln);
|
|
|
|
}
|
|
|
|
const log = function(...args){
|
|
|
|
logC(...args);
|
|
|
|
logHtml('',...args);
|
|
|
|
};
|
|
|
|
const warn = function(...args){
|
|
|
|
logHtml('warning',...args);
|
|
|
|
};
|
|
|
|
const error = function(...args){
|
|
|
|
logHtml('error',...args);
|
2022-09-12 15:39:28 +03:00
|
|
|
};
|
2022-09-19 16:16:35 +03:00
|
|
|
|
2022-09-29 16:17:50 +03:00
|
|
|
const runTests = function(sqlite3){
|
|
|
|
const capi = sqlite3.capi,
|
2022-09-12 15:39:28 +03:00
|
|
|
oo = sqlite3.oo1,
|
2022-10-29 10:54:10 +03:00
|
|
|
wasm = sqlite3.wasm;
|
2022-09-12 15:39:28 +03:00
|
|
|
log("Loaded module:",capi.sqlite3_libversion(), capi.sqlite3_sourceid());
|
2022-09-12 19:09:50 +03:00
|
|
|
T.assert( 0 !== capi.sqlite3_vfs_find(null) );
|
2022-09-13 22:27:03 +03:00
|
|
|
if(!capi.sqlite3_vfs_find('kvvfs')){
|
2022-09-19 16:16:35 +03:00
|
|
|
error("This build is not kvvfs-capable.");
|
2022-09-12 20:59:04 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-30 14:01:44 +03:00
|
|
|
const dbStorage = 0 ? 'session' : 'local';
|
2022-09-13 22:27:03 +03:00
|
|
|
const theStore = 's'===dbStorage[0] ? sessionStorage : localStorage;
|
2022-09-30 14:01:44 +03:00
|
|
|
const db = new oo.JsStorageDb( dbStorage );
|
|
|
|
// Or: oo.DB(dbStorage, 'c', 'kvvfs')
|
2022-09-30 18:46:08 +03:00
|
|
|
log("db.storageSize():",db.storageSize());
|
2022-09-12 20:59:04 +03:00
|
|
|
document.querySelector('#btn-clear-storage').addEventListener('click',function(){
|
2022-09-30 18:46:08 +03:00
|
|
|
const sz = db.clearStorage();
|
|
|
|
log("kvvfs",db.filename+"Storage cleared:",sz,"entries.");
|
2022-09-12 20:59:04 +03:00
|
|
|
});
|
|
|
|
document.querySelector('#btn-clear-log').addEventListener('click',function(){
|
|
|
|
eOutput.innerText = '';
|
|
|
|
});
|
|
|
|
document.querySelector('#btn-init-db').addEventListener('click',function(){
|
|
|
|
try{
|
2022-09-13 22:27:03 +03:00
|
|
|
const saveSql = [];
|
2022-09-12 20:59:04 +03:00
|
|
|
db.exec({
|
2022-09-13 22:27:03 +03:00
|
|
|
sql: ["drop table if exists t;",
|
|
|
|
"create table if not exists t(a);",
|
|
|
|
"insert into t(a) values(?),(?),(?)"],
|
2022-09-12 20:59:04 +03:00
|
|
|
bind: [performance.now() >> 0,
|
|
|
|
(performance.now() * 2) >> 0,
|
|
|
|
(performance.now() / 2) >> 0],
|
|
|
|
saveSql
|
|
|
|
});
|
|
|
|
console.log("saveSql =",saveSql,theStore);
|
|
|
|
log("DB (re)initialized.");
|
|
|
|
}catch(e){
|
|
|
|
error(e.message);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const btnSelect = document.querySelector('#btn-select1');
|
|
|
|
btnSelect.addEventListener('click',function(){
|
|
|
|
log("DB rows:");
|
|
|
|
try{
|
2022-09-12 16:05:54 +03:00
|
|
|
db.exec({
|
|
|
|
sql: "select * from t order by a",
|
|
|
|
rowMode: 0,
|
2022-09-12 20:59:04 +03:00
|
|
|
callback: (v)=>log(v)
|
2022-09-12 16:05:54 +03:00
|
|
|
});
|
2022-09-12 20:59:04 +03:00
|
|
|
}catch(e){
|
|
|
|
error(e.message);
|
2022-09-12 16:05:54 +03:00
|
|
|
}
|
2022-09-12 20:59:04 +03:00
|
|
|
});
|
2022-09-13 22:27:03 +03:00
|
|
|
document.querySelector('#btn-storage-size').addEventListener('click',function(){
|
2022-09-30 18:46:08 +03:00
|
|
|
log("size.storageSize(",dbStorage,") says", db.storageSize(),
|
|
|
|
"bytes");
|
2022-09-13 22:27:03 +03:00
|
|
|
});
|
2022-09-15 05:27:48 +03:00
|
|
|
log("Storage backend:",db.filename);
|
2022-09-12 20:59:04 +03:00
|
|
|
if(0===db.selectValue('select count(*) from sqlite_master')){
|
|
|
|
log("DB is empty. Use the init button to populate it.");
|
|
|
|
}else{
|
|
|
|
log("DB contains data from a previous session. Use the Clear Ctorage button to delete it.");
|
|
|
|
btnSelect.click();
|
2022-09-12 16:05:54 +03:00
|
|
|
}
|
2022-09-12 15:39:28 +03:00
|
|
|
};
|
|
|
|
|
2022-09-29 16:17:50 +03:00
|
|
|
sqlite3InitModule(self.sqlite3TestModule).then((sqlite3)=>{
|
|
|
|
runTests(sqlite3);
|
2022-09-12 15:39:28 +03:00
|
|
|
});
|
|
|
|
})();
|