Skip to content

Commit

Permalink
[breaking][bun:sqlite] .values() returns [] instead of null for…
Browse files Browse the repository at this point in the history
… queries returning 0 results (oven-sh#3219)

Co-authored-by: Jarred Sumner <[email protected]>
  • Loading branch information
Jarred-Sumner and Jarred-Sumner authored Jun 7, 2023
1 parent b9a705f commit 7e296a1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
9 changes: 7 additions & 2 deletions packages/bun-types/sqlite.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,9 @@ declare module "bun:sqlite" {
/**
* Execute the prepared statement and return the results as an array of arrays.
*
* This is a little faster than {@link all}.
* In Bun v0.6.7 and earlier, this method returned `null` if there were no
* results instead of `[]`. This was changed in v0.6.8 to align
* more with what people expect.
*
* @param params optional values to bind to the statement. If omitted, the statement is run with the last bound values or no parameters if there are none.
*
Expand All @@ -595,12 +597,15 @@ declare module "bun:sqlite" {
*
* stmt.values("foo");
* // => [['foo']]
*
* stmt.values("not-found");
* // => []
* ```
*
* The following types can be used when binding parameters:
*
* | JavaScript type | SQLite type |
* | -------------- | ----------- |
* | ---------------|-------------|
* | `string` | `TEXT` |
* | `number` | `INTEGER` or `DECIMAL` |
* | `boolean` | `INTEGER` (1 or 0) |
Expand Down
3 changes: 3 additions & 0 deletions src/bun.js/bindings/sqlite/JSSQLStatement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1417,6 +1417,9 @@ JSC_DEFINE_HOST_FUNCTION(jsSQLStatementExecuteStatementFunctionRows, (JSC::JSGlo

result = resultArray;
}
} else if (status == SQLITE_DONE && columnCount != 0) {
// breaking change in Bun v0.6.8
result = JSC::constructEmptyArray(lexicalGlobalObject, nullptr, 0);
}

if (UNLIKELY(status != SQLITE_DONE && status != SQLITE_OK)) {
Expand Down
8 changes: 8 additions & 0 deletions test/js/bun/sqlite/sqlite.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,14 @@ it("latin1 supplement chars", () => {
greeting: "¿Qué sucedió?",
},
]);

expect(db.query("SELECT * FROM foo").values()).toEqual([
[1, "Welcome to bun!"],
[2, "Español"],
[3, "¿Qué sucedió?"],
]);
expect(db.query("SELECT * FROM foo WHERE id > 9999").all()).toEqual([]);
expect(db.query("SELECT * FROM foo WHERE id > 9999").values()).toEqual([]);
});

describe("Database.run", () => {
Expand Down

0 comments on commit 7e296a1

Please sign in to comment.