demo-123.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. 2022-09-19
  3. The author disclaims copyright to this source code. In place of a
  4. legal notice, here is a blessing:
  5. * May you do good and not evil.
  6. * May you find forgiveness for yourself and forgive others.
  7. * May you share freely, never taking more than you give.
  8. ***********************************************************************
  9. A basic demonstration of the SQLite3 "OO#1" API.
  10. */
  11. 'use strict';
  12. (function(){
  13. /**
  14. Set up our output channel differently depending
  15. on whether we are running in a worker thread or
  16. the main (UI) thread.
  17. */
  18. let logHtml;
  19. if(globalThis.window === globalThis /* UI thread */){
  20. console.log("Running demo from main UI thread.");
  21. logHtml = function(cssClass,...args){
  22. const ln = document.createElement('div');
  23. if(cssClass) ln.classList.add(cssClass);
  24. ln.append(document.createTextNode(args.join(' ')));
  25. document.body.append(ln);
  26. };
  27. }else{ /* Worker thread */
  28. console.log("Running demo from Worker thread.");
  29. logHtml = function(cssClass,...args){
  30. postMessage({
  31. type:'log',
  32. payload:{cssClass, args}
  33. });
  34. };
  35. }
  36. const log = (...args)=>logHtml('',...args);
  37. const warn = (...args)=>logHtml('warning',...args);
  38. const error = (...args)=>logHtml('error',...args);
  39. const demo1 = function(sqlite3){
  40. const capi = sqlite3.capi/*C-style API*/,
  41. oo = sqlite3.oo1/*high-level OO API*/;
  42. log("sqlite3 version",capi.sqlite3_libversion(), capi.sqlite3_sourceid());
  43. const db = new oo.DB("/mydb.sqlite3",'ct');
  44. log("transient db =",db.filename);
  45. /**
  46. Never(!) rely on garbage collection to clean up DBs and
  47. (especially) prepared statements. Always wrap their lifetimes
  48. in a try/finally construct, as demonstrated below. By and
  49. large, client code can entirely avoid lifetime-related
  50. complications of prepared statement objects by using the
  51. DB.exec() method for SQL execution.
  52. */
  53. try {
  54. log("Create a table...");
  55. db.exec("CREATE TABLE IF NOT EXISTS t(a,b)");
  56. //Equivalent:
  57. db.exec({
  58. sql:"CREATE TABLE IF NOT EXISTS t(a,b)"
  59. // ... numerous other options ...
  60. });
  61. // SQL can be either a string or a byte array
  62. // or an array of strings which get concatenated
  63. // together as-is (so be sure to end each statement
  64. // with a semicolon).
  65. log("Insert some data using exec()...");
  66. let i;
  67. for( i = 20; i <= 25; ++i ){
  68. db.exec({
  69. sql: "insert into t(a,b) values (?,?)",
  70. // bind by parameter index...
  71. bind: [i, i*2]
  72. });
  73. db.exec({
  74. sql: "insert into t(a,b) values ($a,$b)",
  75. // bind by parameter name...
  76. bind: {$a: i * 10, $b: i * 20}
  77. });
  78. }
  79. log("Insert using a prepared statement...");
  80. let q = db.prepare([
  81. // SQL may be a string or array of strings
  82. // (concatenated w/o separators).
  83. "insert into t(a,b) ",
  84. "values(?,?)"
  85. ]);
  86. try {
  87. for( i = 100; i < 103; ++i ){
  88. q.bind( [i, i*2] ).step();
  89. q.reset();
  90. }
  91. // Equivalent...
  92. for( i = 103; i <= 105; ++i ){
  93. q.bind(1, i).bind(2, i*2).stepReset();
  94. }
  95. }finally{
  96. q.finalize();
  97. }
  98. log("Query data with exec() using rowMode 'array'...");
  99. db.exec({
  100. sql: "select a from t order by a limit 3",
  101. rowMode: 'array', // 'array' (default), 'object', or 'stmt'
  102. callback: function(row){
  103. log("row ",++this.counter,"=",row);
  104. }.bind({counter: 0})
  105. });
  106. log("Query data with exec() using rowMode 'object'...");
  107. db.exec({
  108. sql: "select a as aa, b as bb from t order by aa limit 3",
  109. rowMode: 'object',
  110. callback: function(row){
  111. log("row ",++this.counter,"=",JSON.stringify(row));
  112. }.bind({counter: 0})
  113. });
  114. log("Query data with exec() using rowMode 'stmt'...");
  115. db.exec({
  116. sql: "select a from t order by a limit 3",
  117. rowMode: 'stmt',
  118. callback: function(row){
  119. log("row ",++this.counter,"get(0) =",row.get(0));
  120. }.bind({counter: 0})
  121. });
  122. log("Query data with exec() using rowMode INTEGER (result column index)...");
  123. db.exec({
  124. sql: "select a, b from t order by a limit 3",
  125. rowMode: 1, // === result column 1
  126. callback: function(row){
  127. log("row ",++this.counter,"b =",row);
  128. }.bind({counter: 0})
  129. });
  130. log("Query data with exec() using rowMode $COLNAME (result column name)...");
  131. db.exec({
  132. sql: "select a a, b from t order by a limit 3",
  133. rowMode: '$a',
  134. callback: function(value){
  135. log("row ",++this.counter,"a =",value);
  136. }.bind({counter: 0})
  137. });
  138. log("Query data with exec() without a callback...");
  139. let resultRows = [];
  140. db.exec({
  141. sql: "select a, b from t order by a limit 3",
  142. rowMode: 'object',
  143. resultRows: resultRows
  144. });
  145. log("Result rows:",JSON.stringify(resultRows,undefined,2));
  146. log("Create a scalar UDF...");
  147. db.createFunction({
  148. name: 'twice',
  149. xFunc: function(pCx, arg){ // note the call arg count
  150. return arg + arg;
  151. }
  152. });
  153. log("Run scalar UDF and collect result column names...");
  154. let columnNames = [];
  155. db.exec({
  156. sql: "select a, twice(a), twice(''||a) from t order by a desc limit 3",
  157. columnNames: columnNames,
  158. rowMode: 'stmt',
  159. callback: function(row){
  160. log("a =",row.get(0), "twice(a) =", row.get(1),
  161. "twice(''||a) =",row.get(2));
  162. }
  163. });
  164. log("Result column names:",columnNames);
  165. try{
  166. log("The following use of the twice() UDF will",
  167. "fail because of incorrect arg count...");
  168. db.exec("select twice(1,2,3)");
  169. }catch(e){
  170. warn("Got expected exception:",e.message);
  171. }
  172. try {
  173. db.transaction( function(D) {
  174. D.exec("delete from t");
  175. log("In transaction: count(*) from t =",db.selectValue("select count(*) from t"));
  176. throw new sqlite3.SQLite3Error("Demonstrating transaction() rollback");
  177. });
  178. }catch(e){
  179. if(e instanceof sqlite3.SQLite3Error){
  180. log("Got expected exception from db.transaction():",e.message);
  181. log("count(*) from t =",db.selectValue("select count(*) from t"));
  182. }else{
  183. throw e;
  184. }
  185. }
  186. try {
  187. db.savepoint( function(D) {
  188. D.exec("delete from t");
  189. log("In savepoint: count(*) from t =",db.selectValue("select count(*) from t"));
  190. D.savepoint(function(DD){
  191. const rows = [];
  192. DD.exec({
  193. sql: ["insert into t(a,b) values(99,100);",
  194. "select count(*) from t"],
  195. rowMode: 0,
  196. resultRows: rows
  197. });
  198. log("In nested savepoint. Row count =",rows[0]);
  199. throw new sqlite3.SQLite3Error("Demonstrating nested savepoint() rollback");
  200. })
  201. });
  202. }catch(e){
  203. if(e instanceof sqlite3.SQLite3Error){
  204. log("Got expected exception from nested db.savepoint():",e.message);
  205. log("count(*) from t =",db.selectValue("select count(*) from t"));
  206. }else{
  207. throw e;
  208. }
  209. }
  210. }finally{
  211. db.close();
  212. }
  213. log("That's all, folks!");
  214. /**
  215. Some of the features of the OO API not demonstrated above...
  216. - get change count (total or statement-local, 32- or 64-bit)
  217. - get a DB's file name
  218. Misc. Stmt features:
  219. - Various forms of bind()
  220. - clearBindings()
  221. - reset()
  222. - Various forms of step()
  223. - Variants of get() for explicit type treatment/conversion,
  224. e.g. getInt(), getFloat(), getBlob(), getJSON()
  225. - getColumnName(ndx), getColumnNames()
  226. - getParamIndex(name)
  227. */
  228. }/*demo1()*/;
  229. log("Loading and initializing sqlite3 module...");
  230. if(globalThis.window!==globalThis) /*worker thread*/{
  231. /*
  232. If sqlite3.js is in a directory other than this script, in order
  233. to get sqlite3.js to resolve sqlite3.wasm properly, we have to
  234. explicitly tell it where sqlite3.js is being loaded from. We do
  235. that by passing the `sqlite3.dir=theDirName` URL argument to
  236. _this_ script. That URL argument will be seen by the JS/WASM
  237. loader and it will adjust the sqlite3.wasm path accordingly. If
  238. sqlite3.js/.wasm are in the same directory as this script then
  239. that's not needed.
  240. URL arguments passed as part of the filename via importScripts()
  241. are simply lost, and such scripts see the globalThis.location of
  242. _this_ script.
  243. */
  244. let sqlite3Js = 'sqlite3.js';
  245. const urlParams = new URL(globalThis.location.href).searchParams;
  246. if(urlParams.has('sqlite3.dir')){
  247. sqlite3Js = urlParams.get('sqlite3.dir') + '/' + sqlite3Js;
  248. }
  249. importScripts(sqlite3Js);
  250. }
  251. globalThis.sqlite3InitModule({
  252. /* We can redirect any stdout/stderr from the module like so, but
  253. note that doing so makes use of Emscripten-isms, not
  254. well-defined sqlite APIs. */
  255. print: log,
  256. printErr: error
  257. }).then(function(sqlite3){
  258. //console.log('sqlite3 =',sqlite3);
  259. log("Done initializing. Running demo...");
  260. try {
  261. demo1(sqlite3);
  262. }catch(e){
  263. error("Exception:",e.message);
  264. }
  265. });
  266. })();