Skip to content

Commit 9e283a9

Browse files
committed
nicer API
1 parent 88f7f24 commit 9e283a9

File tree

7 files changed

+58
-54
lines changed

7 files changed

+58
-54
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ CFLAGS=-DSQLITE_OMIT_LOAD_EXTENSION
77
all: sql.js benchmark.html benchmark
88

99
sql.js: sqlite3.c
10-
$(EMCC) $(CFLAGS) sqlite3.c main.c --post-js bindings.js -o sql.js -s EXPORTED_FUNCTIONS="['_sqlite3_open', '_sqlite3_close', '_sqlite3_exec']"
10+
$(EMCC) $(CFLAGS) sqlite3.c main.c --pre-js pre.js --post-js post.js -o sql.js -s EXPORTED_FUNCTIONS="['_sqlite3_open', '_sqlite3_close', '_sqlite3_exec']"
1111

1212
benchmark.html: sqlite3.c benchmark.c
1313
$(EMCC) $(CFLAGS) sqlite3.c benchmark.c -o benchmark.html

benchmark.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ function TIME(msg) {
77
then = now;
88
}
99

10-
var db = SQL.open(":memory:");
10+
var db = SQL.open();
1111
function report(data) {
1212
for (var i = 0; i < data.length; i++) {
1313
print(data[i].column + ' = ' + data[i].value + '\n');
1414
}
1515
}
1616
function RUN(cmd) {
17-
SQL.exec(db, cmd, report);
17+
db.exec(cmd, report);
1818
}
1919

2020
TIME("'startup'");
@@ -53,5 +53,5 @@ RUN("SELECT count(*) FROM t1 WHERE a == 4");
5353
RUN("SELECT count(*) FROM t1 WHERE b > 20000 AND b < 50000;");
5454
TIME("selects with indexes");
5555

56-
SQL.close(db);
56+
db.close();
5757

bindings.js

Lines changed: 0 additions & 42 deletions
This file was deleted.

post.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
var apiTemp = Runtime.stackAlloc(4);
2+
var callbackTemp = FUNCTION_TABLE.length;
3+
FUNCTION_TABLE.push(0, 0);
4+
5+
Module['open'] = function(filename) {
6+
filename = filename || ":memory:";
7+
var ret = Module['ccall']('sqlite3_open', 'number', ['string', 'number'], [filename, apiTemp]);
8+
if (ret) throw 'SQLite exception: ' + ret;
9+
return {
10+
ptr: getValue(apiTemp, 'i32'),
11+
12+
'close': function() {
13+
var ret = Module['ccall']('sqlite3_close', 'number', ['number'], [this.ptr]);
14+
if (ret) throw 'SQLite exception: ' + ret;
15+
},
16+
17+
'exec': function(sql, callback) {
18+
setValue(apiTemp, 0, 'i32');
19+
if (callback) {
20+
FUNCTION_TABLE[callbackTemp] = function(notUsed, argc, argv, colNames) {
21+
var data = [];
22+
for (var i = 0; i < argc; i++) {
23+
data.push({
24+
'column': Pointer_stringify(getValue(colNames + i*Runtime.QUANTUM_SIZE, 'i32')),
25+
'value': Pointer_stringify(getValue(argv + i*Runtime.QUANTUM_SIZE, 'i32'))
26+
});
27+
}
28+
callback(data);
29+
};
30+
}
31+
var ret = Module['ccall']('sqlite3_exec', 'number', ['number', 'string', 'number', 'number', 'number'],
32+
[this.ptr, sql, callback ? callbackTemp : 0, 0, apiTemp]);
33+
var errPtr = getValue(apiTemp, 'i32');
34+
if (ret || errPtr) {
35+
var msg = 'SQLite exception: ' + ret + ', ' + (errPtr ? Pointer_stringify(errPtr) : '');
36+
if (errPtr) _sqlite3_free(errPtr);
37+
throw msg;
38+
}
39+
}
40+
};
41+
};
42+
43+
this['SQL'] = Module;
44+

pre.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// This is sql.js, a port of SQLite to JavaScript using Emscripten
2+

sql.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
// js -m -n -e "load('sql.js')" test.js
22

3-
var db = SQL.open(":memory:");
3+
var db = SQL.open();
44

55
print('created db');
66

7-
SQL.exec(db, "CREATE TABLE my_table(a INTEGER, b INTEGER, c VARCHAR(100));");
7+
db.exec("CREATE TABLE my_table(a INTEGER, b INTEGER, c VARCHAR(100));");
88

99
print('executed one command');
1010

11-
SQL.exec(db, "INSERT INTO my_table VALUES(1,13153,'thirteen thousand one hundred fifty three');");
12-
SQL.exec(db, "INSERT INTO my_table VALUES(1,987,'some other number');");
11+
db.exec("INSERT INTO my_table VALUES(1,13153,'thirteen thousand one hundred fifty three');");
12+
db.exec("INSERT INTO my_table VALUES(1,987,'some other number');");
1313

1414
function report(data) {
1515
print(JSON.stringify(data, null, ' '));
1616
}
1717

18-
SQL.exec(db, "SELECT count(*) FROM my_table;", report);
18+
db.exec("SELECT count(*) FROM my_table;", report);
1919
// prints [{ "column": "count(*)", "value": "2" }]
2020

2121
print('printed one report');
2222

23-
SQL.exec(db, "SELECT a, b, c FROM my_table;", report);
23+
db.exec("SELECT a, b, c FROM my_table;", report);
2424
// prints [{ "column": "a", "value": "1" }, { "column": "b", "value": "13153" }, { "column": "c", "value": "thirteen thousand one hundred fifty three" }]
2525
// [{ "column": "a", "value": "1" }, { "column": "b", "value": "987" }, { "column": "c", "value": "some other number" }]
2626

27-
SQL.close(db);
27+
db.close();
2828

0 commit comments

Comments
 (0)