@@ -10,93 +10,93 @@ var worker = new Worker("../../dist/worker.sql-wasm.js");
1010worker . onerror = error ;
1111
1212// Open a database
13- worker . postMessage ( { action :'open' } ) ;
13+ worker . postMessage ( { action : 'open' } ) ;
1414
1515// Connect to the HTML element we 'print' to
1616function print ( text ) {
17- outputElm . innerHTML = text . replace ( / \n / g, '<br>' ) ;
17+ outputElm . innerHTML = text . replace ( / \n / g, '<br>' ) ;
1818}
1919function error ( e ) {
20- console . log ( e ) ;
20+ console . log ( e ) ;
2121 errorElm . style . height = '2em' ;
2222 errorElm . textContent = e . message ;
2323}
2424
2525function noerror ( ) {
26- errorElm . style . height = '0' ;
26+ errorElm . style . height = '0' ;
2727}
2828
2929// Run a command in the database
3030function execute ( commands ) {
3131 tic ( ) ;
32- worker . onmessage = function ( event ) {
32+ worker . onmessage = function ( event ) {
3333 var results = event . data . results ;
3434 toc ( "Executing SQL" ) ;
3535
3636 tic ( ) ;
3737 outputElm . innerHTML = "" ;
38- for ( var i = 0 ; i < results . length ; i ++ ) {
38+ for ( var i = 0 ; i < results . length ; i ++ ) {
3939 outputElm . appendChild ( tableCreate ( results [ i ] . columns , results [ i ] . values ) ) ;
4040 }
4141 toc ( "Displaying results" ) ;
4242 }
43- worker . postMessage ( { action :'exec' , sql :commands } ) ;
43+ worker . postMessage ( { action : 'exec' , sql : commands } ) ;
4444 outputElm . textContent = "Fetching results..." ;
4545}
4646
4747// Create an HTML table
4848var tableCreate = function ( ) {
49- function valconcat ( vals , tagName ) {
50- if ( vals . length === 0 ) return '' ;
51- var open = '<' + tagName + '>' , close = '</' + tagName + '>' ;
52- return open + vals . join ( close + open ) + close ;
53- }
54- return function ( columns , values ) {
55- var tbl = document . createElement ( 'table' ) ;
56- var html = '<thead>' + valconcat ( columns , 'th' ) + '</thead>' ;
57- var rows = values . map ( function ( v ) { return valconcat ( v , 'td' ) ; } ) ;
58- html += '<tbody>' + valconcat ( rows , 'tr' ) + '</tbody>' ;
59- tbl . innerHTML = html ;
60- return tbl ;
61- }
49+ function valconcat ( vals , tagName ) {
50+ if ( vals . length === 0 ) return '' ;
51+ var open = '<' + tagName + '>' , close = '</' + tagName + '>' ;
52+ return open + vals . join ( close + open ) + close ;
53+ }
54+ return function ( columns , values ) {
55+ var tbl = document . createElement ( 'table' ) ;
56+ var html = '<thead>' + valconcat ( columns , 'th' ) + '</thead>' ;
57+ var rows = values . map ( function ( v ) { return valconcat ( v , 'td' ) ; } ) ;
58+ html += '<tbody>' + valconcat ( rows , 'tr' ) + '</tbody>' ;
59+ tbl . innerHTML = html ;
60+ return tbl ;
61+ }
6262} ( ) ;
6363
6464// Execute the commands when the button is clicked
65- function execEditorContents ( ) {
65+ function execEditorContents ( ) {
6666 noerror ( )
67- execute ( editor . getValue ( ) + ';' ) ;
67+ execute ( editor . getValue ( ) + ';' ) ;
6868}
6969execBtn . addEventListener ( "click" , execEditorContents , true ) ;
7070
7171// Performance measurement functions
7272var tictime ;
73- if ( ! window . performance || ! performance . now ) { window . performance = { now :Date . now } }
74- function tic ( ) { tictime = performance . now ( ) }
73+ if ( ! window . performance || ! performance . now ) { window . performance = { now : Date . now } }
74+ function tic ( ) { tictime = performance . now ( ) }
7575function toc ( msg ) {
76- var dt = performance . now ( ) - tictime ;
77- console . log ( ( msg || 'toc' ) + ": " + dt + "ms" ) ;
76+ var dt = performance . now ( ) - tictime ;
77+ console . log ( ( msg || 'toc' ) + ": " + dt + "ms" ) ;
7878}
7979
8080// Add syntax highlihjting to the textarea
8181var editor = CodeMirror . fromTextArea ( commandsElm , {
82- mode : 'text/x-mysql' ,
83- viewportMargin : Infinity ,
84- indentWithTabs : true ,
85- smartIndent : true ,
86- lineNumbers : true ,
87- matchBrackets : true ,
88- autofocus : true ,
89- extraKeys : {
90- "Ctrl-Enter" : execEditorContents ,
91- "Ctrl-S" : savedb ,
92- }
82+ mode : 'text/x-mysql' ,
83+ viewportMargin : Infinity ,
84+ indentWithTabs : true ,
85+ smartIndent : true ,
86+ lineNumbers : true ,
87+ matchBrackets : true ,
88+ autofocus : true ,
89+ extraKeys : {
90+ "Ctrl-Enter" : execEditorContents ,
91+ "Ctrl-S" : savedb ,
92+ }
9393} ) ;
9494
9595// Load a db from a file
96- dbFileElm . onchange = function ( ) {
96+ dbFileElm . onchange = function ( ) {
9797 var f = dbFileElm . files [ 0 ] ;
9898 var r = new FileReader ( ) ;
99- r . onload = function ( ) {
99+ r . onload = function ( ) {
100100 worker . onmessage = function ( ) {
101101 toc ( "Loading database from file" ) ;
102102 // Show the schema of the loaded database
@@ -105,32 +105,33 @@ dbFileElm.onchange = function() {
105105 } ;
106106 tic ( ) ;
107107 try {
108- worker . postMessage ( { action :'open' , buffer :r . result } , [ r . result ] ) ;
108+ worker . postMessage ( { action : 'open' , buffer : r . result } , [ r . result ] ) ;
109109 }
110- catch ( exception ) {
111- worker . postMessage ( { action :'open' , buffer :r . result } ) ;
110+ catch ( exception ) {
111+ worker . postMessage ( { action : 'open' , buffer : r . result } ) ;
112112 }
113113 }
114114 r . readAsArrayBuffer ( f ) ;
115115}
116116
117117// Save the db to a file
118- function savedb ( ) {
119- worker . onmessage = function ( event ) {
118+ function savedb ( ) {
119+ worker . onmessage = function ( event ) {
120120 toc ( "Exporting the database" ) ;
121121 var arraybuff = event . data . buffer ;
122122 var blob = new Blob ( [ arraybuff ] ) ;
123123 var a = document . createElement ( "a" ) ;
124+ document . body . appendChild ( a ) ;
124125 a . href = window . URL . createObjectURL ( blob ) ;
125126 a . download = "sql.db" ;
126- a . onclick = function ( ) {
127- setTimeout ( function ( ) {
127+ a . onclick = function ( ) {
128+ setTimeout ( function ( ) {
128129 window . URL . revokeObjectURL ( a . href ) ;
129130 } , 1500 ) ;
130131 } ;
131132 a . click ( ) ;
132133 } ;
133134 tic ( ) ;
134- worker . postMessage ( { action :'export' } ) ;
135+ worker . postMessage ( { action : 'export' } ) ;
135136}
136137savedbElm . addEventListener ( "click" , savedb , true ) ;
0 commit comments