-
Notifications
You must be signed in to change notification settings - Fork 6
/
post.js
59 lines (51 loc) · 1.78 KB
/
post.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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);
var fileCounter = 0;
Module['open'] = function(data) {
var filename = 'file_' + fileCounter++;
if (data) {
FS.createDataFile('/', filename, data, true, true);
}
var ret = Module['ccall']('sqlite3_open', 'number', ['string', 'number'], [filename, apiTemp]);
if (ret) throw 'SQLite exception: ' + ret;
return {
ptr: getValue(apiTemp, 'i32'),
filename: filename,
'close': function() {
var ret = Module['ccall']('sqlite3_close', 'number', ['number'], [this.ptr]);
this.ptr = null;
if (ret) throw 'SQLite exception: ' + ret;
},
'exec': function(sql) {
if (!this.ptr) throw 'Database closed!';
setValue(apiTemp, 0, 'i32');
dataTemp = [];
var ret = Module['ccall']('sqlite3_exec', 'number', ['number', 'string', 'number', 'number', 'number'],
[this.ptr, sql, callbackTemp, 0, apiTemp]);
var errPtr = getValue(apiTemp, 'i32');
if (ret || errPtr) {
var msg = 'SQLite exception: ' + ret + (errPtr ? ', ' + Pointer_stringify(errPtr) : '');
if (errPtr) _sqlite3_free(errPtr);
throw msg;
}
return dataTemp;
},
'exportData': function() {
if (!this.ptr) throw 'Database closed!';
return new Uint8Array(FS.root.contents[this.filename].contents);
}
};
};
this['SQL'] = Module;