Skip to content

Commit 5be9574

Browse files
committed
fix formatting and indentation
1 parent fbe8427 commit 5be9574

File tree

6 files changed

+222
-207
lines changed

6 files changed

+222
-207
lines changed

examples/GUI/gui.js

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -10,93 +10,93 @@ var worker = new Worker("../../dist/worker.sql-wasm.js");
1010
worker.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
1616
function print(text) {
17-
outputElm.innerHTML = text.replace(/\n/g, '<br>');
17+
outputElm.innerHTML = text.replace(/\n/g, '<br>');
1818
}
1919
function error(e) {
20-
console.log(e);
20+
console.log(e);
2121
errorElm.style.height = '2em';
2222
errorElm.textContent = e.message;
2323
}
2424

2525
function noerror() {
26-
errorElm.style.height = '0';
26+
errorElm.style.height = '0';
2727
}
2828

2929
// Run a command in the database
3030
function 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
4848
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+
}
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
}
6969
execBtn.addEventListener("click", execEditorContents, true);
7070

7171
// Performance measurement functions
7272
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() }
7575
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");
7878
}
7979

8080
// Add syntax highlihjting to the textarea
8181
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+
}
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,32 @@ 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");
124124
a.href = window.URL.createObjectURL(blob);
125125
a.download = "sql.db";
126-
a.onclick = function() {
127-
setTimeout(function() {
126+
a.onclick = function () {
127+
setTimeout(function () {
128128
window.URL.revokeObjectURL(a.href);
129129
}, 1500);
130130
};
131131
a.click();
132132
};
133133
tic();
134-
worker.postMessage({action:'export'});
134+
worker.postMessage({ action: 'export' });
135135
}
136136
savedbElm.addEventListener("click", savedb, true);

examples/GUI/index.html

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<!doctype html>
22
<html>
3+
34
<head>
45
<meta charset="utf8">
56
<title>sql.js demo: Online SQL interpreter</title>
@@ -8,13 +9,17 @@
89
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.46.0/codemirror.js"></script>
910

1011
</head>
12+
1113
<body>
12-
<!-- Github ribbon -->
13-
<a href="https://github.com/kripken/sql.js"><img style="position: absolute; top: 0; left: 0; border: 0;" src="https://camo.githubusercontent.com/82b228a3648bf44fc1163ef44c62fcc60081495e/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f6c6566745f7265645f6161303030302e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_left_red_aa0000.png"></a>
14+
<!-- Github ribbon -->
15+
<a href="https://github.com/kripken/sql.js"><img style="position: absolute; top: 0; left: 0; border: 0;"
16+
src="https://camo.githubusercontent.com/82b228a3648bf44fc1163ef44c62fcc60081495e/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f6c6566745f7265645f6161303030302e706e67"
17+
alt="Fork me on GitHub"
18+
data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_left_red_aa0000.png"></a>
1419

15-
<h1>Online SQL interpreter</h1>
20+
<h1>Online SQL interpreter</h1>
1621

17-
<main>
22+
<main>
1823
<label for='commands'>Enter some SQL</label>
1924
<br>
2025

@@ -42,23 +47,24 @@ <h1>Online SQL interpreter</h1>
4247
SELECT designation,COUNT(*) AS nbr, (AVG(salary)) AS avg_salary FROM employees GROUP BY designation ORDER BY avg_salary DESC;
4348
SELECT name,hired_on FROM employees ORDER BY hired_on;</textarea>
4449

45-
<button id="execute" class="button">Execute</button>
46-
<button id='savedb' class="button">Save the db</button>
47-
<label class="button">Load an SQLite database file: <input type='file' id='dbfile' ></label>
50+
<button id="execute" class="button">Execute</button>
51+
<button id='savedb' class="button">Save the db</button>
52+
<label class="button">Load an SQLite database file: <input type='file' id='dbfile'></label>
4853

49-
<div id="error" class="error"></div>
54+
<div id="error" class="error"></div>
5055

51-
<pre id="output">Results will be displayed here</pre>
52-
</main>
56+
<pre id="output">Results will be displayed here</pre>
57+
</main>
5358

54-
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.46.0/mode/sql/sql.min.js"></script>
59+
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.46.0/mode/sql/sql.min.js"></script>
5560

56-
<footer>
57-
Original work by kripken (<a href='https://github.com/kripken/sql.js'>sql.js</a>).
58-
C to Javascript compiler by kripken (<a href='https://github.com/kripken/emscripten'>emscripten</a>).
59-
Project now maintained by <a href='https://github.com/lovasoa'>lovasoa</a>
60-
</footer>
61+
<footer>
62+
Original work by kripken (<a href='https://github.com/kripken/sql.js'>sql.js</a>).
63+
C to Javascript compiler by kripken (<a href='https://github.com/kripken/emscripten'>emscripten</a>).
64+
Project now maintained by <a href='https://github.com/lovasoa'>lovasoa</a>
65+
</footer>
6166

62-
<script type="text/javascript" src="gui.js"></script>
67+
<script type="text/javascript" src="gui.js"></script>
6368
</body>
64-
</html>
69+
70+
</html>

examples/persistent.html

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,70 @@
11
<!doctype html>
22
<html>
3+
34
<head>
45
<meta charset="utf8">
56
<title>Persistent sqlite</title>
67
<script src="../dist/sql-wasm.js"></script>
78
</head>
9+
810
<body>
911
<p>You have seen this page <span id="views">0</span> times.</p>
1012
<div>
1113
You have been here on the following dates: <ol id="dates"></ol>
1214
</div>
1315
<script>
14-
var baseUrl = '../dist/';
16+
var baseUrl = '../dist/';
1517

16-
function toBinArray (str) {
17-
var l = str.length,
18+
function toBinArray(str) {
19+
var l = str.length,
1820
arr = new Uint8Array(l);
19-
for (var i=0; i<l; i++) arr[i] = str.charCodeAt(i);
20-
return arr;
21-
}
21+
for (var i = 0; i < l; i++) arr[i] = str.charCodeAt(i);
22+
return arr;
23+
}
2224

23-
function toBinString (arr) {
24-
var uarr = new Uint8Array(arr);
25-
var strings = [], chunksize = 0xffff;
26-
// There is a maximum stack size. We cannot call String.fromCharCode with as many arguments as we want
27-
for (var i=0; i*chunksize < uarr.length; i++){
28-
strings.push(String.fromCharCode.apply(null, uarr.subarray(i*chunksize, (i+1)*chunksize)));
25+
function toBinString(arr) {
26+
var uarr = new Uint8Array(arr);
27+
var strings = [], chunksize = 0xffff;
28+
// There is a maximum stack size. We cannot call String.fromCharCode with as many arguments as we want
29+
for (var i = 0; i * chunksize < uarr.length; i++) {
30+
strings.push(String.fromCharCode.apply(null, uarr.subarray(i * chunksize, (i + 1) * chunksize)));
31+
}
32+
return strings.join('');
2933
}
30-
return strings.join('');
31-
}
3234

33-
// Normally Sql.js tries to load sql-wasm.wasm relative to the page, not relative to the javascript
34-
// doing the loading. So, we help it find the .wasm file with this function.
35-
var config = {
36-
locateFile: filename => `${baseUrl}/${filename}`
37-
}
38-
initSqlJs(config).then(function(SQL){
39-
var dbstr = window.localStorage.getItem("viewcount.sqlite");
40-
if (dbstr) {
41-
var db = new SQL.Database(toBinArray(dbstr));
42-
} else {
43-
var db = new SQL.Database();
44-
db.run("CREATE TABLE views (date INTEGER PRIMARY KEY)");
35+
// Normally Sql.js tries to load sql-wasm.wasm relative to the page, not relative to the javascript
36+
// doing the loading. So, we help it find the .wasm file with this function.
37+
var config = {
38+
locateFile: filename => `${baseUrl}/${filename}`
4539
}
46-
db.run("INSERT INTO views(date) VALUES (?)", [Date.now()]);
40+
initSqlJs(config).then(function (SQL) {
41+
var dbstr = window.localStorage.getItem("viewcount.sqlite");
42+
if (dbstr) {
43+
var db = new SQL.Database(toBinArray(dbstr));
44+
} else {
45+
var db = new SQL.Database();
46+
db.run("CREATE TABLE views (date INTEGER PRIMARY KEY)");
47+
}
48+
db.run("INSERT INTO views(date) VALUES (?)", [Date.now()]);
4749

48-
document.getElementById('views').textContent = db.exec("SELECT COUNT(*) FROM views")[0].values[0][0];
50+
document.getElementById('views').textContent = db.exec("SELECT COUNT(*) FROM views")[0].values[0][0];
4951

50-
var count = 0,
52+
var count = 0,
5153
dates = document.getElementById("dates");
5254

53-
db.each("SELECT date FROM views ORDER BY date ASC",
54-
function callback (row) {
55+
db.each("SELECT date FROM views ORDER BY date ASC",
56+
function callback(row) {
5557
var li = document.createElement("li");
5658
li.textContent = new Date(row.date);
5759
dates.appendChild(li);
58-
}, function done () {
60+
}, function done() {
5961
var dbstr = toBinString(db.export());
6062
window.localStorage.setItem("viewcount.sqlite", dbstr);
6163
}
62-
);
63-
});
64+
);
65+
});
6466

6567
</script>
66-
</body>
67-
</html>
68+
</body>
69+
70+
</html>

0 commit comments

Comments
 (0)