Skip to content

Commit

Permalink
have .exec return the data
Browse files Browse the repository at this point in the history
  • Loading branch information
kripken committed Mar 3, 2012
1 parent 6832e9a commit cc35f34
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 23 deletions.
11 changes: 7 additions & 4 deletions benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ function TIME(msg) {
}

var db = SQL.open();
function report(data) {
for (var i = 0; i < data.length; i++) {
print(data[i].column + ' = ' + data[i].value + '\n');
function report(all) {
for (var j = 0; j < all.length; j++) {
var data = all[j];
for (var i = 0; i < data.length; i++) {
print(data[i].column + ' = ' + data[i].value + '\n');
}
}
}
function RUN(cmd) {
db.exec(cmd, report);
report(db.exec(cmd));
}

TIME("'startup'");
Expand Down
32 changes: 17 additions & 15 deletions post.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
var apiTemp = Runtime.stackAlloc(4);
var dataTemp;

var callbackTemp = FUNCTION_TABLE.length;
FUNCTION_TABLE[callbackTemp] = function(notUsed, argc, argv, colNames) {
var curr = [];
for (var i = 0; i < argc; i++) {
curr.push({
'column': Pointer_stringify(getValue(colNames + i*Runtime.QUANTUM_SIZE, 'i32')),
'value': Pointer_stringify(getValue(argv + i*Runtime.QUANTUM_SIZE, 'i32'))
});
}
dataTemp.push(curr);
};
FUNCTION_TABLE.push(0, 0);

Module['open'] = function(filename) {
Expand All @@ -14,28 +26,18 @@ Module['open'] = function(filename) {
if (ret) throw 'SQLite exception: ' + ret;
},

'exec': function(sql, callback) {
'exec': function(sql) {
setValue(apiTemp, 0, 'i32');
if (callback) {
FUNCTION_TABLE[callbackTemp] = function(notUsed, argc, argv, colNames) {
var data = [];
for (var i = 0; i < argc; i++) {
data.push({
'column': Pointer_stringify(getValue(colNames + i*Runtime.QUANTUM_SIZE, 'i32')),
'value': Pointer_stringify(getValue(argv + i*Runtime.QUANTUM_SIZE, 'i32'))
});
}
callback(data);
};
}
dataTemp = [];
var ret = Module['ccall']('sqlite3_exec', 'number', ['number', 'string', 'number', 'number', 'number'],
[this.ptr, sql, callback ? callbackTemp : 0, 0, apiTemp]);
[this.ptr, sql, callbackTemp, 0, apiTemp]);
var errPtr = getValue(apiTemp, 'i32');
if (ret || errPtr) {
var msg = 'SQLite exception: ' + ret + ', ' + (errPtr ? Pointer_stringify(errPtr) : '');
var msg = 'SQLite exception: ' + ret + (errPtr ? ', ' + Pointer_stringify(errPtr) : '');
if (errPtr) _sqlite3_free(errPtr);
throw msg;
}
return dataTemp;
}
};
};
Expand Down
2 changes: 1 addition & 1 deletion sql.js

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,20 @@ function test() {
print(JSON.stringify(data, null, ' '));
}

db.exec("SELECT count(*) FROM my_table;", report);
report(db.exec("SELECT count(*) FROM my_table;"));

// prints [{ "column": "count(*)", "value": "2" }]

print('printed one report');

var db2 = SQL.open();
try {
db2.exec("SELECT a, b, c FROM my_table;", report);
db2.exec("SELECT a, b, c FROM my_table;");
} catch(e) {
// Failure is expected, as the other db doesn't have that table!
db2.close();

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

Expand Down

0 comments on commit cc35f34

Please sign in to comment.