@@ -10,93 +10,93 @@ var worker = new Worker("../../dist/worker.sql-wasm.js");
10
10
worker . onerror = error ;
11
11
12
12
// Open a database
13
- worker . postMessage ( { action :'open' } ) ;
13
+ worker . postMessage ( { action : 'open' } ) ;
14
14
15
15
// Connect to the HTML element we 'print' to
16
16
function print ( text ) {
17
- outputElm . innerHTML = text . replace ( / \n / g, '<br>' ) ;
17
+ outputElm . innerHTML = text . replace ( / \n / g, '<br>' ) ;
18
18
}
19
19
function error ( e ) {
20
- console . log ( e ) ;
20
+ console . log ( e ) ;
21
21
errorElm . style . height = '2em' ;
22
22
errorElm . textContent = e . message ;
23
23
}
24
24
25
25
function noerror ( ) {
26
- errorElm . style . height = '0' ;
26
+ errorElm . style . height = '0' ;
27
27
}
28
28
29
29
// Run a command in the database
30
30
function execute ( commands ) {
31
31
tic ( ) ;
32
- worker . onmessage = function ( event ) {
32
+ worker . onmessage = function ( event ) {
33
33
var results = event . data . results ;
34
34
toc ( "Executing SQL" ) ;
35
35
36
36
tic ( ) ;
37
37
outputElm . innerHTML = "" ;
38
- for ( var i = 0 ; i < results . length ; i ++ ) {
38
+ for ( var i = 0 ; i < results . length ; i ++ ) {
39
39
outputElm . appendChild ( tableCreate ( results [ i ] . columns , results [ i ] . values ) ) ;
40
40
}
41
41
toc ( "Displaying results" ) ;
42
42
}
43
- worker . postMessage ( { action :'exec' , sql :commands } ) ;
43
+ worker . postMessage ( { action : 'exec' , sql : commands } ) ;
44
44
outputElm . textContent = "Fetching results..." ;
45
45
}
46
46
47
47
// Create an HTML table
48
48
var 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
+ }
62
62
} ( ) ;
63
63
64
64
// Execute the commands when the button is clicked
65
- function execEditorContents ( ) {
65
+ function execEditorContents ( ) {
66
66
noerror ( )
67
- execute ( editor . getValue ( ) + ';' ) ;
67
+ execute ( editor . getValue ( ) + ';' ) ;
68
68
}
69
69
execBtn . addEventListener ( "click" , execEditorContents , true ) ;
70
70
71
71
// Performance measurement functions
72
72
var 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 ( ) }
75
75
function 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" ) ;
78
78
}
79
79
80
80
// Add syntax highlihjting to the textarea
81
81
var 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
+ }
93
93
} ) ;
94
94
95
95
// Load a db from a file
96
- dbFileElm . onchange = function ( ) {
96
+ dbFileElm . onchange = function ( ) {
97
97
var f = dbFileElm . files [ 0 ] ;
98
98
var r = new FileReader ( ) ;
99
- r . onload = function ( ) {
99
+ r . onload = function ( ) {
100
100
worker . onmessage = function ( ) {
101
101
toc ( "Loading database from file" ) ;
102
102
// Show the schema of the loaded database
@@ -105,32 +105,32 @@ dbFileElm.onchange = function() {
105
105
} ;
106
106
tic ( ) ;
107
107
try {
108
- worker . postMessage ( { action :'open' , buffer :r . result } , [ r . result ] ) ;
108
+ worker . postMessage ( { action : 'open' , buffer : r . result } , [ r . result ] ) ;
109
109
}
110
- catch ( exception ) {
111
- worker . postMessage ( { action :'open' , buffer :r . result } ) ;
110
+ catch ( exception ) {
111
+ worker . postMessage ( { action : 'open' , buffer : r . result } ) ;
112
112
}
113
113
}
114
114
r . readAsArrayBuffer ( f ) ;
115
115
}
116
116
117
117
// Save the db to a file
118
- function savedb ( ) {
119
- worker . onmessage = function ( event ) {
118
+ function savedb ( ) {
119
+ worker . onmessage = function ( event ) {
120
120
toc ( "Exporting the database" ) ;
121
121
var arraybuff = event . data . buffer ;
122
122
var blob = new Blob ( [ arraybuff ] ) ;
123
123
var a = document . createElement ( "a" ) ;
124
124
a . href = window . URL . createObjectURL ( blob ) ;
125
125
a . download = "sql.db" ;
126
- a . onclick = function ( ) {
127
- setTimeout ( function ( ) {
126
+ a . onclick = function ( ) {
127
+ setTimeout ( function ( ) {
128
128
window . URL . revokeObjectURL ( a . href ) ;
129
129
} , 1500 ) ;
130
130
} ;
131
131
a . click ( ) ;
132
132
} ;
133
133
tic ( ) ;
134
- worker . postMessage ( { action :'export' } ) ;
134
+ worker . postMessage ( { action : 'export' } ) ;
135
135
}
136
136
savedbElm . addEventListener ( "click" , savedb , true ) ;
0 commit comments