2022-08-16 19:36:19 +03:00
|
|
|
/*
|
2022-09-19 06:57:31 +03:00
|
|
|
2022-09-19
|
2022-08-16 19:36:19 +03:00
|
|
|
|
|
|
|
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 13:13:01 +03:00
|
|
|
A basic demonstration of the SQLite3 "OO#1" API.
|
2022-08-16 19:36:19 +03:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
(function(){
|
2022-09-19 06:57:31 +03:00
|
|
|
/**
|
|
|
|
Set up our output channel differently depending
|
|
|
|
on whether we are running in a worker thread or
|
|
|
|
the main (UI) thread.
|
|
|
|
*/
|
|
|
|
let logHtml;
|
2023-11-30 23:34:24 +03:00
|
|
|
if(globalThis.window === globalThis /* UI thread */){
|
2022-09-30 01:08:22 +03:00
|
|
|
console.log("Running demo from main UI thread.");
|
2022-09-19 06:57:31 +03:00
|
|
|
logHtml = function(cssClass,...args){
|
|
|
|
const ln = document.createElement('div');
|
|
|
|
if(cssClass) ln.classList.add(cssClass);
|
|
|
|
ln.append(document.createTextNode(args.join(' ')));
|
|
|
|
document.body.append(ln);
|
|
|
|
};
|
|
|
|
}else{ /* Worker thread */
|
2022-09-30 01:08:22 +03:00
|
|
|
console.log("Running demo from Worker thread.");
|
2022-09-19 06:57:31 +03:00
|
|
|
logHtml = function(cssClass,...args){
|
|
|
|
postMessage({
|
|
|
|
type:'log',
|
|
|
|
payload:{cssClass, args}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
const log = (...args)=>logHtml('',...args);
|
|
|
|
const warn = (...args)=>logHtml('warning',...args);
|
|
|
|
const error = (...args)=>logHtml('error',...args);
|
2022-08-16 19:36:19 +03:00
|
|
|
|
2022-08-16 20:29:59 +03:00
|
|
|
const demo1 = function(sqlite3){
|
2022-09-19 06:57:31 +03:00
|
|
|
const capi = sqlite3.capi/*C-style API*/,
|
|
|
|
oo = sqlite3.oo1/*high-level OO API*/;
|
|
|
|
log("sqlite3 version",capi.sqlite3_libversion(), capi.sqlite3_sourceid());
|
2022-10-03 21:07:25 +03:00
|
|
|
const db = new oo.DB("/mydb.sqlite3",'ct');
|
2022-09-27 12:17:37 +03:00
|
|
|
log("transient db =",db.filename);
|
2022-08-16 19:36:19 +03:00
|
|
|
/**
|
|
|
|
Never(!) rely on garbage collection to clean up DBs and
|
2022-09-19 13:13:01 +03:00
|
|
|
(especially) prepared statements. Always wrap their lifetimes
|
|
|
|
in a try/finally construct, as demonstrated below. By and
|
|
|
|
large, client code can entirely avoid lifetime-related
|
|
|
|
complications of prepared statement objects by using the
|
|
|
|
DB.exec() method for SQL execution.
|
2022-08-16 19:36:19 +03:00
|
|
|
*/
|
|
|
|
try {
|
|
|
|
log("Create a table...");
|
|
|
|
db.exec("CREATE TABLE IF NOT EXISTS t(a,b)");
|
|
|
|
//Equivalent:
|
|
|
|
db.exec({
|
|
|
|
sql:"CREATE TABLE IF NOT EXISTS t(a,b)"
|
|
|
|
// ... numerous other options ...
|
|
|
|
});
|
|
|
|
// SQL can be either a string or a byte array
|
2022-09-19 06:57:31 +03:00
|
|
|
// or an array of strings which get concatenated
|
|
|
|
// together as-is (so be sure to end each statement
|
|
|
|
// with a semicolon).
|
2022-08-16 19:36:19 +03:00
|
|
|
|
|
|
|
log("Insert some data using exec()...");
|
|
|
|
let i;
|
2022-09-19 06:57:31 +03:00
|
|
|
for( i = 20; i <= 25; ++i ){
|
2022-08-16 19:36:19 +03:00
|
|
|
db.exec({
|
|
|
|
sql: "insert into t(a,b) values (?,?)",
|
|
|
|
// bind by parameter index...
|
|
|
|
bind: [i, i*2]
|
|
|
|
});
|
|
|
|
db.exec({
|
|
|
|
sql: "insert into t(a,b) values ($a,$b)",
|
|
|
|
// bind by parameter name...
|
2022-09-19 06:57:31 +03:00
|
|
|
bind: {$a: i * 10, $b: i * 20}
|
2022-08-16 19:36:19 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
log("Insert using a prepared statement...");
|
2022-09-27 12:17:37 +03:00
|
|
|
let q = db.prepare([
|
|
|
|
// SQL may be a string or array of strings
|
|
|
|
// (concatenated w/o separators).
|
|
|
|
"insert into t(a,b) ",
|
|
|
|
"values(?,?)"
|
|
|
|
]);
|
2022-08-16 19:36:19 +03:00
|
|
|
try {
|
|
|
|
for( i = 100; i < 103; ++i ){
|
|
|
|
q.bind( [i, i*2] ).step();
|
|
|
|
q.reset();
|
|
|
|
}
|
|
|
|
// Equivalent...
|
|
|
|
for( i = 103; i <= 105; ++i ){
|
|
|
|
q.bind(1, i).bind(2, i*2).stepReset();
|
|
|
|
}
|
|
|
|
}finally{
|
|
|
|
q.finalize();
|
|
|
|
}
|
|
|
|
|
|
|
|
log("Query data with exec() using rowMode 'array'...");
|
|
|
|
db.exec({
|
|
|
|
sql: "select a from t order by a limit 3",
|
2022-09-27 20:03:01 +03:00
|
|
|
rowMode: 'array', // 'array' (default), 'object', or 'stmt'
|
2022-08-16 19:36:19 +03:00
|
|
|
callback: function(row){
|
|
|
|
log("row ",++this.counter,"=",row);
|
|
|
|
}.bind({counter: 0})
|
|
|
|
});
|
|
|
|
|
|
|
|
log("Query data with exec() using rowMode 'object'...");
|
|
|
|
db.exec({
|
|
|
|
sql: "select a as aa, b as bb from t order by aa limit 3",
|
|
|
|
rowMode: 'object',
|
|
|
|
callback: function(row){
|
2022-09-19 06:57:31 +03:00
|
|
|
log("row ",++this.counter,"=",JSON.stringify(row));
|
2022-08-16 19:36:19 +03:00
|
|
|
}.bind({counter: 0})
|
|
|
|
});
|
|
|
|
|
|
|
|
log("Query data with exec() using rowMode 'stmt'...");
|
|
|
|
db.exec({
|
|
|
|
sql: "select a from t order by a limit 3",
|
2022-09-27 20:03:01 +03:00
|
|
|
rowMode: 'stmt',
|
2022-08-16 19:36:19 +03:00
|
|
|
callback: function(row){
|
|
|
|
log("row ",++this.counter,"get(0) =",row.get(0));
|
|
|
|
}.bind({counter: 0})
|
|
|
|
});
|
|
|
|
|
|
|
|
log("Query data with exec() using rowMode INTEGER (result column index)...");
|
|
|
|
db.exec({
|
|
|
|
sql: "select a, b from t order by a limit 3",
|
|
|
|
rowMode: 1, // === result column 1
|
|
|
|
callback: function(row){
|
|
|
|
log("row ",++this.counter,"b =",row);
|
|
|
|
}.bind({counter: 0})
|
|
|
|
});
|
|
|
|
|
2022-09-19 06:57:31 +03:00
|
|
|
log("Query data with exec() using rowMode $COLNAME (result column name)...");
|
|
|
|
db.exec({
|
|
|
|
sql: "select a a, b from t order by a limit 3",
|
|
|
|
rowMode: '$a',
|
|
|
|
callback: function(value){
|
|
|
|
log("row ",++this.counter,"a =",value);
|
|
|
|
}.bind({counter: 0})
|
|
|
|
});
|
|
|
|
|
2022-08-16 19:36:19 +03:00
|
|
|
log("Query data with exec() without a callback...");
|
|
|
|
let resultRows = [];
|
|
|
|
db.exec({
|
|
|
|
sql: "select a, b from t order by a limit 3",
|
|
|
|
rowMode: 'object',
|
|
|
|
resultRows: resultRows
|
|
|
|
});
|
2022-09-19 06:57:31 +03:00
|
|
|
log("Result rows:",JSON.stringify(resultRows,undefined,2));
|
2022-08-16 19:36:19 +03:00
|
|
|
|
|
|
|
log("Create a scalar UDF...");
|
|
|
|
db.createFunction({
|
|
|
|
name: 'twice',
|
2022-10-16 21:50:55 +03:00
|
|
|
xFunc: function(pCx, arg){ // note the call arg count
|
2022-08-16 19:36:19 +03:00
|
|
|
return arg + arg;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
log("Run scalar UDF and collect result column names...");
|
|
|
|
let columnNames = [];
|
|
|
|
db.exec({
|
|
|
|
sql: "select a, twice(a), twice(''||a) from t order by a desc limit 3",
|
|
|
|
columnNames: columnNames,
|
2022-08-25 14:39:12 +03:00
|
|
|
rowMode: 'stmt',
|
2022-08-16 19:36:19 +03:00
|
|
|
callback: function(row){
|
2022-08-25 14:39:12 +03:00
|
|
|
log("a =",row.get(0), "twice(a) =", row.get(1),
|
|
|
|
"twice(''||a) =",row.get(2));
|
2022-08-16 19:36:19 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
log("Result column names:",columnNames);
|
|
|
|
|
2022-09-19 06:57:31 +03:00
|
|
|
try{
|
|
|
|
log("The following use of the twice() UDF will",
|
|
|
|
"fail because of incorrect arg count...");
|
2022-08-16 19:36:19 +03:00
|
|
|
db.exec("select twice(1,2,3)");
|
2022-09-19 06:57:31 +03:00
|
|
|
}catch(e){
|
|
|
|
warn("Got expected exception:",e.message);
|
2022-08-16 19:36:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2022-08-18 15:21:58 +03:00
|
|
|
db.transaction( function(D) {
|
2022-08-16 19:36:19 +03:00
|
|
|
D.exec("delete from t");
|
|
|
|
log("In transaction: count(*) from t =",db.selectValue("select count(*) from t"));
|
2022-08-18 15:21:58 +03:00
|
|
|
throw new sqlite3.SQLite3Error("Demonstrating transaction() rollback");
|
|
|
|
});
|
|
|
|
}catch(e){
|
|
|
|
if(e instanceof sqlite3.SQLite3Error){
|
|
|
|
log("Got expected exception from db.transaction():",e.message);
|
|
|
|
log("count(*) from t =",db.selectValue("select count(*) from t"));
|
|
|
|
}else{
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
db.savepoint( function(D) {
|
|
|
|
D.exec("delete from t");
|
|
|
|
log("In savepoint: count(*) from t =",db.selectValue("select count(*) from t"));
|
|
|
|
D.savepoint(function(DD){
|
|
|
|
const rows = [];
|
2022-09-19 06:57:31 +03:00
|
|
|
DD.exec({
|
2022-08-18 15:21:58 +03:00
|
|
|
sql: ["insert into t(a,b) values(99,100);",
|
|
|
|
"select count(*) from t"],
|
|
|
|
rowMode: 0,
|
|
|
|
resultRows: rows
|
|
|
|
});
|
|
|
|
log("In nested savepoint. Row count =",rows[0]);
|
|
|
|
throw new sqlite3.SQLite3Error("Demonstrating nested savepoint() rollback");
|
|
|
|
})
|
2022-08-16 19:36:19 +03:00
|
|
|
});
|
|
|
|
}catch(e){
|
2022-08-18 14:16:27 +03:00
|
|
|
if(e instanceof sqlite3.SQLite3Error){
|
2022-08-18 15:21:58 +03:00
|
|
|
log("Got expected exception from nested db.savepoint():",e.message);
|
2022-08-18 14:16:27 +03:00
|
|
|
log("count(*) from t =",db.selectValue("select count(*) from t"));
|
|
|
|
}else{
|
|
|
|
throw e;
|
|
|
|
}
|
2022-08-16 19:36:19 +03:00
|
|
|
}
|
|
|
|
}finally{
|
|
|
|
db.close();
|
|
|
|
}
|
|
|
|
|
2022-09-19 06:57:31 +03:00
|
|
|
log("That's all, folks!");
|
|
|
|
|
2022-08-16 19:36:19 +03:00
|
|
|
/**
|
2022-09-19 06:57:31 +03:00
|
|
|
Some of the features of the OO API not demonstrated above...
|
2022-08-16 19:36:19 +03:00
|
|
|
|
|
|
|
- get change count (total or statement-local, 32- or 64-bit)
|
2022-09-19 06:57:31 +03:00
|
|
|
- get a DB's file name
|
2023-07-24 18:41:58 +03:00
|
|
|
|
2022-08-16 19:36:19 +03:00
|
|
|
Misc. Stmt features:
|
|
|
|
|
2023-07-24 18:41:58 +03:00
|
|
|
- Various forms of bind()
|
2022-08-16 19:36:19 +03:00
|
|
|
- clearBindings()
|
|
|
|
- reset()
|
|
|
|
- Various forms of step()
|
|
|
|
- Variants of get() for explicit type treatment/conversion,
|
|
|
|
e.g. getInt(), getFloat(), getBlob(), getJSON()
|
|
|
|
- getColumnName(ndx), getColumnNames()
|
|
|
|
- getParamIndex(name)
|
|
|
|
*/
|
|
|
|
}/*demo1()*/;
|
|
|
|
|
2022-09-19 06:57:31 +03:00
|
|
|
log("Loading and initializing sqlite3 module...");
|
2023-11-30 23:34:24 +03:00
|
|
|
if(globalThis.window!==globalThis) /*worker thread*/{
|
2022-10-19 07:44:58 +03:00
|
|
|
/*
|
|
|
|
If sqlite3.js is in a directory other than this script, in order
|
|
|
|
to get sqlite3.js to resolve sqlite3.wasm properly, we have to
|
|
|
|
explicitly tell it where sqlite3.js is being loaded from. We do
|
|
|
|
that by passing the `sqlite3.dir=theDirName` URL argument to
|
|
|
|
_this_ script. That URL argument will be seen by the JS/WASM
|
|
|
|
loader and it will adjust the sqlite3.wasm path accordingly. If
|
|
|
|
sqlite3.js/.wasm are in the same directory as this script then
|
|
|
|
that's not needed.
|
|
|
|
|
|
|
|
URL arguments passed as part of the filename via importScripts()
|
2023-11-30 23:34:24 +03:00
|
|
|
are simply lost, and such scripts see the globalThis.location of
|
2022-10-19 07:44:58 +03:00
|
|
|
_this_ script.
|
|
|
|
*/
|
|
|
|
let sqlite3Js = 'sqlite3.js';
|
2023-11-30 23:34:24 +03:00
|
|
|
const urlParams = new URL(globalThis.location.href).searchParams;
|
2022-10-19 07:44:58 +03:00
|
|
|
if(urlParams.has('sqlite3.dir')){
|
|
|
|
sqlite3Js = urlParams.get('sqlite3.dir') + '/' + sqlite3Js;
|
|
|
|
}
|
|
|
|
importScripts(sqlite3Js);
|
2022-09-19 06:57:31 +03:00
|
|
|
}
|
2023-11-30 23:34:24 +03:00
|
|
|
globalThis.sqlite3InitModule({
|
|
|
|
/* We can redirect any stdout/stderr from the module like so, but
|
|
|
|
note that doing so makes use of Emscripten-isms, not
|
|
|
|
well-defined sqlite APIs. */
|
2022-09-19 06:57:31 +03:00
|
|
|
print: log,
|
|
|
|
printErr: error
|
2022-09-29 16:17:50 +03:00
|
|
|
}).then(function(sqlite3){
|
|
|
|
//console.log('sqlite3 =',sqlite3);
|
2022-09-19 06:57:31 +03:00
|
|
|
log("Done initializing. Running demo...");
|
2022-08-16 19:36:19 +03:00
|
|
|
try {
|
2022-09-29 16:17:50 +03:00
|
|
|
demo1(sqlite3);
|
2022-08-16 19:36:19 +03:00
|
|
|
}catch(e){
|
|
|
|
error("Exception:",e.message);
|
|
|
|
}
|
2022-09-19 06:57:31 +03:00
|
|
|
});
|
2022-08-16 19:36:19 +03:00
|
|
|
})();
|