Skip to content

Commit d0ea2db

Browse files
committed
Changes as per PR comments
1 parent ed47799 commit d0ea2db

File tree

6 files changed

+55
-26
lines changed

6 files changed

+55
-26
lines changed

.eslintrc.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
module.exports = {
44
env: {
55
browser: true,
6-
es2020: true,
6+
es6: true,
77
node: true
88
},
99
extends: [
@@ -24,7 +24,7 @@ module.exports = {
2424
"!/.eslintrc.js"
2525
],
2626
parserOptions: {
27-
ecmaVersion: 2020,
27+
ecmaVersion: 5,
2828
sourceType: "script"
2929
},
3030
rules: {

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,10 @@ If you need ```BigInt``` support, it is partially supported since most browsers
240240
const config = {useBigInt: true};
241241
/*Pass optional config param to the get function*/
242242
while (stmt.step()) console.log(stmt.get(null, config));
243+
244+
/*OR*/
245+
const result = db.exec("SELECT * FROM test", config);
246+
console.log(results[0].values)
243247
</script>
244248
```
245249
On WebWorker, you can just add ```config``` param before posting a message. With this, you wont have to pass config param on ```get``` function.

src/api.js

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,6 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
117117
["number", "number", "number"]
118118
);
119119

120-
var sqlite3_bind_int64 = cwrap(
121-
"sqlite3_bind_int64",
122-
"number",
123-
["number", "number"]
124-
);
125-
126120
var sqlite3_bind_parameter_index = cwrap(
127121
"sqlite3_bind_parameter_index",
128122
"number",
@@ -371,6 +365,10 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
371365
this.pos += 1;
372366
}
373367
var text = sqlite3_column_text(this.stmt, pos);
368+
if (typeof BigInt !== "function") {
369+
throw new Error("BigInt is not supported");
370+
}
371+
/* global BigInt */
374372
return BigInt(text);
375373
};
376374

@@ -411,7 +409,7 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
411409
var stmt = db.prepare("SELECT * FROM test");
412410
while (stmt.step()) console.log(stmt.get(null, {useBigInt: true}));
413411
*/
414-
Statement.prototype["get"] = function get(params, config = {}) {
412+
Statement.prototype["get"] = function get(params, config) {
415413
if (params != null && this["bind"](params)) {
416414
this["step"]();
417415
}
@@ -420,7 +418,7 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
420418
for (var field = 0; field < ref; field += 1) {
421419
switch (sqlite3_column_type(this.stmt, field)) {
422420
case SQLITE_INTEGER:
423-
var getfunc = config.useBigInt
421+
var getfunc = config && config.useBigInt
424422
? this.getBigInt(field)
425423
: this.getNumber(field);
426424
results1.push(getfunc);
@@ -476,7 +474,7 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
476474
console.log(stmt.getAsObject());
477475
// Will print {nbr:5, data: Uint8Array([1,2,3]), null_value:null}
478476
*/
479-
Statement.prototype["getAsObject"] = function getAsObject(params, config = {}) {
477+
Statement.prototype["getAsObject"] = function getAsObject(params, config) {
480478
var values = this["get"](params, config);
481479
var names = this["getColumnNames"]();
482480
var rowObject = {};
@@ -572,15 +570,6 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
572570
return true;
573571
};
574572

575-
Statement.prototype.bindBigInt = function bindBigInt(num, pos) {
576-
if (pos == null) {
577-
pos = this.pos;
578-
this.pos += 1;
579-
}
580-
this.db.handleError(sqlite3_bind_int64(this.stmt, pos, num));
581-
return true;
582-
};
583-
584573
Statement.prototype.bindNull = function bindNull(pos) {
585574
if (pos == null) {
586575
pos = this.pos;
@@ -938,7 +927,7 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
938927
(separated by `;`). This limitation does not apply to params as an object.
939928
* @return {Database.QueryExecResult[]} The results of each statement
940929
*/
941-
Database.prototype["exec"] = function exec(sql, params, config = {}) {
930+
Database.prototype["exec"] = function exec(sql, params, config) {
942931
if (!this.db) {
943932
throw "Database closed";
944933
}
@@ -1010,7 +999,9 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
1010999
function (row){console.log(row.name + " is a grown-up.")}
10111000
);
10121001
*/
1013-
Database.prototype["each"] = function each(sql, params, callback, done, config = {}) {
1002+
Database.prototype["each"] = function each(
1003+
sql, params, callback, done, config
1004+
) {
10141005
var stmt;
10151006
if (typeof params === "function") {
10161007
done = callback;

src/exported_functions.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
"_sqlite3_bind_blob",
1414
"_sqlite3_bind_double",
1515
"_sqlite3_bind_int",
16-
"_sqlite3_bind_int64",
1716
"_sqlite3_bind_parameter_index",
1817
"_sqlite3_step",
1918
"_sqlite3_column_count",

src/worker.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,20 @@ function onModuleReady(SQL) {
3939
if (db === null) {
4040
createDb();
4141
}
42-
var callbackfunc = function callback(row) {
42+
var callback = function callback(row) {
4343
return postMessage({
4444
id: data["id"],
4545
row: row,
4646
finished: false
4747
});
4848
};
49-
var donefunc = function done() {
49+
var done = function done() {
5050
return postMessage({
5151
id: data["id"],
5252
finished: true
5353
});
5454
};
55-
return db.each(data["sql"], data["params"], callbackfunc, donefunc, config);
55+
return db.each(data["sql"], data["params"], callback, done, config);
5656
case "export":
5757
buff = db["export"]();
5858
result = {

test/test_big_int.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
exports.test = function(sql, assert){
2+
// Create a database
3+
var db = new sql.Database();
4+
5+
// Create table, insert data
6+
sqlstr = "CREATE TABLE IF NOT EXISTS Test_BigInt (someNumber BIGINT NOT NULL);" +
7+
"INSERT INTO Test_BigInt (someNumber) VALUES (1628675501000);";
8+
db.exec(sqlstr);
9+
10+
var config = {useBigInt: true};
11+
12+
var stmt = db.prepare("SELECT * FROM Test_BigInt;");
13+
stmt.step();
14+
15+
assert.strictEqual(typeof stmt.get()[0], 'number', "Reading number value");
16+
assert.strictEqual(typeof stmt.get(null, config)[0], 'bigint', "Reading bigint value");
17+
18+
db.close();
19+
};
20+
21+
if (module == require.main) {
22+
const target_file = process.argv[2];
23+
const sql_loader = require('./load_sql_lib');
24+
sql_loader(target_file).then((sql)=>{
25+
require('test').run({
26+
'test big int': function(assert){
27+
exports.test(sql, assert);
28+
}
29+
});
30+
})
31+
.catch((e)=>{
32+
console.error(e);
33+
assert.fail(e);
34+
});
35+
}

0 commit comments

Comments
 (0)