Skip to content
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ 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.
A ```BigInt``` parameter is bound as a 64-bit INTEGER, and binding one outside the 64-bit range throws a ```RangeError```. To read INTEGER values as ```BigInt```, pass the ```useBigInt``` option:

```html
<script>
Expand Down
35 changes: 33 additions & 2 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,16 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
"number",
["number", "number", "number"]
);
var sqlite3_bind_int64 = cwrap(
"sqlite3_bind_int64",
"number",
["number", "number", "number"]
);
var sqlite3_memory_used = cwrap("sqlite3_memory_used", "number", []);
// The WebAssembly builds pass a 64-bit integer between JavaScript and C
// as a BigInt. The asm.js builds can't, so Emscripten splits it into its
// low and high 32 bits there.
var int64AsBigInt = typeof sqlite3_memory_used() === "bigint";

var sqlite3_bind_parameter_index = cwrap(
"sqlite3_bind_parameter_index",
Expand Down Expand Up @@ -598,6 +608,28 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
return true;
};

Statement.prototype.bindBigInt = function bindBigInt(num, pos) {
if (pos == null) {
pos = this.pos;
this.pos += 1;
}
if (BigInt.asIntN(64, num) !== num) {
throw new RangeError(
"BigInt value is out of range for a 64-bit integer: " + num
);
}
var ret = int64AsBigInt
? sqlite3_bind_int64(this.stmt, pos, num)
: sqlite3_bind_int64(
this.stmt,
pos,
Number(BigInt.asIntN(32, num)),
Number(num >> BigInt(32))
);
this.db.handleError(ret);
return true;
};

Statement.prototype.bindNull = function bindNull(pos) {
if (pos == null) {
pos = this.pos;
Expand All @@ -618,8 +650,7 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
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);
return this.bindBigInt(val, pos);
case "boolean":
return this.bindNumber(val + 0, pos);
case "object":
Expand Down
2 changes: 2 additions & 0 deletions src/exported_functions.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"_sqlite3_backup_finish",
"_sqlite3_exec",
"_sqlite3_free",
"_sqlite3_memory_used",
"_sqlite3_errmsg",
"_sqlite3_changes",
"_sqlite3_prepare_v2",
Expand All @@ -16,6 +17,7 @@
"_sqlite3_bind_blob",
"_sqlite3_bind_double",
"_sqlite3_bind_int",
"_sqlite3_bind_int64",
"_sqlite3_bind_parameter_index",
"_sqlite3_step",
"_sqlite3_column_count",
Expand Down
52 changes: 52 additions & 0 deletions test/test_big_int.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,58 @@ exports.test = function(sql, assert){

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

// Binding a BigInt
var probe = db.prepare("SELECT typeof(?1), ?1");
var bound = function(value) {
var row = probe.get([value], config);
probe.reset();
return row;
};
assert.deepEqual(bound(BigInt(42)), ["integer", BigInt(42)],
"A BigInt is bound as an INTEGER");
assert.deepEqual(bound(BigInt(-42)), ["integer", BigInt(-42)],
"A negative BigInt is bound as an INTEGER");
assert.deepEqual(bound(BigInt("9007199254740993")),
["integer", BigInt("9007199254740993")],
"A BigInt past Number.MAX_SAFE_INTEGER is bound exactly");
assert.deepEqual(bound(BigInt("9223372036854775807")),
["integer", BigInt("9223372036854775807")],
"The largest 64-bit integer is bound exactly");
assert.deepEqual(bound(BigInt("-9223372036854775808")),
["integer", BigInt("-9223372036854775808")],
"The smallest 64-bit integer is bound exactly");
assert.deepEqual(bound(BigInt("-4294967296")),
["integer", BigInt("-4294967296")],
"A BigInt with a zero low word is bound exactly");

assert.throws(function() {
probe.bind([BigInt("9223372036854775808")]);
}, RangeError, "A BigInt above the 64-bit range is refused");
assert.throws(function() {
probe.bind([BigInt("-9223372036854775809")]);
}, RangeError, "A BigInt below the 64-bit range is refused");
probe.free();

// A bound BigInt compares as an integer, not as text
db.run("CREATE TABLE ids (id INTEGER PRIMARY KEY, name TEXT)");
db.run("INSERT INTO ids VALUES (?, 'big')", [BigInt("9007199254740995")]);
assert.deepEqual(
db.exec("SELECT name FROM ids WHERE id = ?", [BigInt("9007199254740995")]),
[{columns: ["name"], values: [["big"]]}],
"A BigInt parameter matches the integer it was stored as");
assert.deepEqual(
db.exec("SELECT ? + 1", [BigInt(41)]),
[{columns: ["? + 1"], values: [[42]]}],
"A BigInt parameter takes part in arithmetic as an integer");

var named = db.prepare("SELECT typeof(:v), :v");
assert.deepEqual(
named.get({":v": BigInt("-9007199254740993")}, config),
["integer", BigInt("-9007199254740993")],
"A BigInt bound by name is bound as an INTEGER");
named.free();

db.close();
};
Expand Down
Loading