Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Changes as per PR comments
  • Loading branch information
kileha3 committed Aug 11, 2021
commit d0ea2dbddb33ac4c12d091e0f7151fbde4740274
4 changes: 2 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module.exports = {
env: {
browser: true,
es2020: true,
es6: true,
node: true
},
extends: [
Expand All @@ -24,7 +24,7 @@ module.exports = {
"!/.eslintrc.js"
],
parserOptions: {
ecmaVersion: 2020,
ecmaVersion: 5,
sourceType: "script"
},
rules: {
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,10 @@ If you need ```BigInt``` support, it is partially supported since most browsers
const config = {useBigInt: true};
/*Pass optional config param to the get function*/
while (stmt.step()) console.log(stmt.get(null, config));

/*OR*/
const result = db.exec("SELECT * FROM test", config);
console.log(results[0].values)
</script>
```
On WebWorker, you can just add ```config``` param before posting a message. With this, you wont have to pass config param on ```get``` function.
Expand Down
31 changes: 11 additions & 20 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,6 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
["number", "number", "number"]
);

var sqlite3_bind_int64 = cwrap(
"sqlite3_bind_int64",
"number",
["number", "number"]
);

var sqlite3_bind_parameter_index = cwrap(
"sqlite3_bind_parameter_index",
"number",
Expand Down Expand Up @@ -371,6 +365,10 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
this.pos += 1;
}
var text = sqlite3_column_text(this.stmt, pos);
if (typeof BigInt !== "function") {
throw new Error("BigInt is not supported");
}
/* global BigInt */
return BigInt(text);
};

Expand Down Expand Up @@ -411,7 +409,7 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
var stmt = db.prepare("SELECT * FROM test");
while (stmt.step()) console.log(stmt.get(null, {useBigInt: true}));
*/
Statement.prototype["get"] = function get(params, config = {}) {
Statement.prototype["get"] = function get(params, config) {
if (params != null && this["bind"](params)) {
this["step"]();
}
Expand All @@ -420,7 +418,7 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
for (var field = 0; field < ref; field += 1) {
switch (sqlite3_column_type(this.stmt, field)) {
case SQLITE_INTEGER:
var getfunc = config.useBigInt
var getfunc = config && config.useBigInt
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe instead of config && add config = config || {} at the start of the function. This will make it easier to extend in the future.

Copy link
Contributor Author

@kileha3 kileha3 Aug 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe instead of config && add config = config || {} at the start of the function. This will make it easier to extend in the future.

I think I did miss something, I see an issue on minified files. When we pass config object { useBigInt: true } , config.useBigInt ? .... will always be evaluated as false since object property name useBigInt will be mangled to something like l.Ty. So, it will be something like l.Ty ? .... , of which Ty is not a property of l at run time. Will it be possible to have a way to prevent object properties mangling ? any idea on how to move forward on this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you should use config["useBigInt"]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe instead of config && add config = config || {} at the start of the function. This will make it easier to extend in the future.

Just pushed the changes, thanks

? this.getBigInt(field)
: this.getNumber(field);
results1.push(getfunc);
Expand Down Expand Up @@ -476,7 +474,7 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
console.log(stmt.getAsObject());
// Will print {nbr:5, data: Uint8Array([1,2,3]), null_value:null}
*/
Statement.prototype["getAsObject"] = function getAsObject(params, config = {}) {
Statement.prototype["getAsObject"] = function getAsObject(params, config) {
var values = this["get"](params, config);
var names = this["getColumnNames"]();
var rowObject = {};
Expand Down Expand Up @@ -572,15 +570,6 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
return true;
};

Statement.prototype.bindBigInt = function bindBigInt(num, pos) {
if (pos == null) {
pos = this.pos;
this.pos += 1;
}
this.db.handleError(sqlite3_bind_int64(this.stmt, pos, num));
return true;
};

Statement.prototype.bindNull = function bindNull(pos) {
if (pos == null) {
pos = this.pos;
Expand Down Expand Up @@ -938,7 +927,7 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
(separated by `;`). This limitation does not apply to params as an object.
* @return {Database.QueryExecResult[]} The results of each statement
*/
Database.prototype["exec"] = function exec(sql, params, config = {}) {
Database.prototype["exec"] = function exec(sql, params, config) {
if (!this.db) {
throw "Database closed";
}
Expand Down Expand Up @@ -1010,7 +999,9 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
function (row){console.log(row.name + " is a grown-up.")}
);
*/
Database.prototype["each"] = function each(sql, params, callback, done, config = {}) {
Database.prototype["each"] = function each(
sql, params, callback, done, config
) {
var stmt;
if (typeof params === "function") {
done = callback;
Expand Down
1 change: 0 additions & 1 deletion src/exported_functions.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
"_sqlite3_bind_blob",
"_sqlite3_bind_double",
"_sqlite3_bind_int",
"_sqlite3_bind_int64",
"_sqlite3_bind_parameter_index",
"_sqlite3_step",
"_sqlite3_column_count",
Expand Down
6 changes: 3 additions & 3 deletions src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,20 @@ function onModuleReady(SQL) {
if (db === null) {
createDb();
}
var callbackfunc = function callback(row) {
var callback = function callback(row) {
return postMessage({
id: data["id"],
row: row,
finished: false
});
};
var donefunc = function done() {
var done = function done() {
return postMessage({
id: data["id"],
finished: true
});
};
return db.each(data["sql"], data["params"], callbackfunc, donefunc, config);
return db.each(data["sql"], data["params"], callback, done, config);
case "export":
buff = db["export"]();
result = {
Expand Down
35 changes: 35 additions & 0 deletions test/test_big_int.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
exports.test = function(sql, assert){
// Create a database
var db = new sql.Database();

// Create table, insert data
sqlstr = "CREATE TABLE IF NOT EXISTS Test_BigInt (someNumber BIGINT NOT NULL);" +
"INSERT INTO Test_BigInt (someNumber) VALUES (1628675501000);";
db.exec(sqlstr);

var config = {useBigInt: true};

var stmt = db.prepare("SELECT * FROM Test_BigInt;");
stmt.step();

assert.strictEqual(typeof stmt.get()[0], 'number', "Reading number value");
assert.strictEqual(typeof stmt.get(null, config)[0], 'bigint', "Reading bigint value");

db.close();
};

if (module == require.main) {
const target_file = process.argv[2];
const sql_loader = require('./load_sql_lib');
sql_loader(target_file).then((sql)=>{
require('test').run({
'test big int': function(assert){
exports.test(sql, assert);
}
});
})
.catch((e)=>{
console.error(e);
assert.fail(e);
});
}