Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Added partial BigInt support #471

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,33 @@ Example:
});
</script>
```
### Enabling BigInt support
If you need ```BigInt``` support, it is partially supported since most browsers now supports it including Safari.Binding ```BigInt``` is still not supported, only getting ```BigInt``` from the database is supported for now.

```html
<script>
const stmt = db.prepare("SELECT * FROM test");
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.

```html
<script>
worker.postMessage({
id:1,
action:"exec",
sql: "SELECT * FROM test",
config: {useBigInt: true}, /*Optional param*/
});
</script>
```

See [examples/GUI/gui.js](examples/GUI/gui.js) for a full working example.

Expand Down
45 changes: 38 additions & 7 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
"number",
["number", "number", "number"]
);

var sqlite3_bind_parameter_index = cwrap(
"sqlite3_bind_parameter_index",
"number",
Expand Down Expand Up @@ -358,6 +359,19 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
return sqlite3_column_double(this.stmt, pos);
};

Statement.prototype.getBigInt = function getBigInt(pos) {
if (pos == null) {
pos = this.pos;
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);
};

Statement.prototype.getString = function getString(pos) {
if (pos == null) {
pos = this.pos;
Expand Down Expand Up @@ -390,8 +404,13 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
<caption>Print all the rows of the table test to the console</caption>
var stmt = db.prepare("SELECT * FROM test");
while (stmt.step()) console.log(stmt.get());

<caption>Enable BigInt support</caption>
var stmt = db.prepare("SELECT * FROM test");
while (stmt.step()) console.log(stmt.get(null, {useBigInt: true}));
*/
Statement.prototype["get"] = function get(params) {
Statement.prototype["get"] = function get(params, config) {
config = config || {};
if (params != null && this["bind"](params)) {
this["step"]();
}
Expand All @@ -400,6 +419,11 @@ 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"]
? this.getBigInt(field)
: this.getNumber(field);
results1.push(getfunc);
break;
case SQLITE_FLOAT:
results1.push(this.getNumber(field));
break;
Expand Down Expand Up @@ -451,8 +475,8 @@ 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) {
var values = this["get"](params);
Statement.prototype["getAsObject"] = function getAsObject(params, config) {
var values = this["get"](params, config);
var names = this["getColumnNames"]();
var rowObject = {};
for (var i = 0; i < names.length; i += 1) {
Expand Down Expand Up @@ -560,10 +584,15 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
pos = this.pos;
this.pos += 1;
}

switch (typeof val) {
case "string":
return this.bindString(val, pos);
case "number":
return this.bindNumber(val + 0, pos);
case "bigint":
// BigInt is not fully supported yet at WASM level.
return this.bindString(val.toString(), pos);
case "boolean":
return this.bindNumber(val + 0, pos);
case "object":
Expand Down Expand Up @@ -899,7 +928,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) {
Database.prototype["exec"] = function exec(sql, params, config) {
if (!this.db) {
throw "Database closed";
}
Expand Down Expand Up @@ -937,7 +966,7 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
};
results.push(curresult);
}
curresult["values"].push(stmt["get"]());
curresult["values"].push(stmt["get"](null, config));
}
stmt["free"]();
}
Expand Down Expand Up @@ -971,7 +1000,9 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
function (row){console.log(row.name + " is a grown-up.")}
);
*/
Database.prototype["each"] = function each(sql, params, callback, done) {
Database.prototype["each"] = function each(
sql, params, callback, done, config
) {
var stmt;
if (typeof params === "function") {
done = callback;
Expand All @@ -981,7 +1012,7 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
stmt = this["prepare"](sql, params);
try {
while (stmt["step"]()) {
callback(stmt["getAsObject"]());
callback(stmt["getAsObject"](null, config));
}
} finally {
stmt["free"]();
Expand Down
5 changes: 3 additions & 2 deletions src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ function onModuleReady(SQL) {

var buff; var data; var result;
data = this["data"];
var config = data["config"] ? data["config"] : {};
switch (data && data["action"]) {
case "open":
buff = data["buffer"];
Expand All @@ -32,7 +33,7 @@ function onModuleReady(SQL) {
}
return postMessage({
id: data["id"],
results: db.exec(data["sql"], data["params"])
results: db.exec(data["sql"], data["params"], config)
});
case "each":
if (db === null) {
Expand All @@ -51,7 +52,7 @@ function onModuleReady(SQL) {
finished: true
});
};
return db.each(data["sql"], data["params"], callback, done);
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);
});
}