Repository navigation
sql(mysql): add foundRows option to opt into CLIENT_FOUND_ROWS #30848
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
Open
robobun
wants to merge
9
commits into
main
Choose a base branch
from
farm/a5a2fbce/mysql-found-rows
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
53b7050
sql(mysql): add foundRows option to opt into CLIENT_FOUND_ROWS
robobun 9b46050
build: JSON-stringify OVERLAY_CSS define value in bake-codegen
robobun 0e23bc4
ci: retrigger
robobun 5879c44
sql(mysql): coerce foundRows URL value through String() before lowerc…
robobun 792a2e2
docs(sql): note foundRows applies to mariadb + add runtime/sql.mdx se…
robobun 07ff6f3
[autofix.ci] apply automated fixes
autofix-ci[bot] 717469d
sql: tighten foundRows URL query comment
robobun 52a6739
sql: trim foundRows comments to one line each
robobun 9f51b7d
sql(mysql): use shared wire-frames helpers in foundRows test, fix lint
robobun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,169 @@ | ||
| // Bun.SQL MySQL `foundRows` option: toggles CLIENT_FOUND_ROWS in the | ||
| // HandshakeResponse41 (default on, matching mysql2 / mariadb), which makes | ||
| // the server report WHERE-matched rows instead of changed rows in affectedRows. | ||
|
|
||
| import { SQL } from "bun"; | ||
| import { describe, expect, test } from "bun:test"; | ||
| import { | ||
| listeningServer, | ||
| MYSQL_CLIENT_FOUND_ROWS, | ||
| MYSQL_DEFAULT_CAPABILITIES, | ||
| mysqlHandshakeV10, | ||
| mysqlOkPacket, | ||
| mysqlParseHandshakeResponse41ClientFlags, | ||
| mysqlReadPackets, | ||
| } from "./wire-frames"; | ||
|
|
||
| interface CapturedHandshake { | ||
| capabilityFlags: number; | ||
| } | ||
|
|
||
| // Completes the handshake, captures the client's HandshakeResponse41 capability | ||
| // flags, and answers each COM_QUERY with an OK_Packet carrying `affectedRows`. | ||
| async function startMockMysql(affectedRows: number): Promise<{ | ||
| port: number; | ||
| captured: Promise<CapturedHandshake>; | ||
| close: () => void; | ||
| }> { | ||
| const captured = Promise.withResolvers<CapturedHandshake>(); | ||
| const { port, server } = await listeningServer(socket => { | ||
| let buffered = Buffer.alloc(0); | ||
| let sawHandshakeResponse = false; | ||
|
|
||
| // Advertise CLIENT_FOUND_ROWS so the client's intersect keeps it when requested. | ||
| socket.write(mysqlHandshakeV10({ capabilities: MYSQL_DEFAULT_CAPABILITIES | MYSQL_CLIENT_FOUND_ROWS })); | ||
|
|
||
| socket.on("data", chunk => { | ||
| buffered = mysqlReadPackets(Buffer.concat([buffered, chunk]), (seq, payload) => { | ||
| if (!sawHandshakeResponse) { | ||
| sawHandshakeResponse = true; | ||
| captured.resolve({ capabilityFlags: mysqlParseHandshakeResponse41ClientFlags(payload) }); | ||
| socket.write(mysqlOkPacket(seq + 1)); | ||
| } else if (payload[0] === 0x03) { | ||
| // COM_QUERY | ||
| socket.write(mysqlOkPacket(seq + 1, 0x00, affectedRows)); | ||
| } else if (payload[0] === 0x01) { | ||
| // COM_QUIT | ||
| socket.end(); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| socket.on("error", () => {}); | ||
| // Don't leak the captured waiter if the client drops early. | ||
| socket.on("close", () => captured.resolve({ capabilityFlags: 0 })); | ||
| }); | ||
| return { port, captured: captured.promise, close: () => server.close() }; | ||
| } | ||
|
|
||
| async function runHandshakeCase(options: Bun.SQL.Options): Promise<number> { | ||
| const mock = await startMockMysql(1); | ||
| try { | ||
| await using db = new SQL({ | ||
| ...options, | ||
| adapter: "mysql", | ||
| hostname: "127.0.0.1", | ||
| port: mock.port, | ||
| username: "root", | ||
| password: "", | ||
| database: "test", | ||
| max: 1, | ||
| idleTimeout: 1, | ||
| } as Bun.SQL.Options); | ||
|
|
||
| // Triggering any query forces the handshake to complete. | ||
| await db.unsafe("UPDATE t SET v = 1"); | ||
| const { capabilityFlags } = await mock.captured; | ||
| return capabilityFlags; | ||
| } finally { | ||
| mock.close(); | ||
| } | ||
| } | ||
|
|
||
| describe("Bun.SQL MySQL foundRows (CLIENT_FOUND_ROWS)", () => { | ||
| test("default: CLIENT_FOUND_ROWS is enabled (matches mysql2 / mariadb defaults)", async () => { | ||
| const caps = await runHandshakeCase({}); | ||
| expect((caps & MYSQL_CLIENT_FOUND_ROWS) !== 0).toBe(true); | ||
| }); | ||
|
|
||
| test("foundRows: true enables CLIENT_FOUND_ROWS", async () => { | ||
| const caps = await runHandshakeCase({ foundRows: true } as Bun.SQL.Options); | ||
| expect((caps & MYSQL_CLIENT_FOUND_ROWS) !== 0).toBe(true); | ||
| }); | ||
|
|
||
| test("foundRows: false disables CLIENT_FOUND_ROWS", async () => { | ||
| const caps = await runHandshakeCase({ foundRows: false } as Bun.SQL.Options); | ||
| expect((caps & MYSQL_CLIENT_FOUND_ROWS) !== 0).toBe(false); | ||
| }); | ||
|
|
||
| test("URL ?foundRows=false disables CLIENT_FOUND_ROWS", async () => { | ||
| const mock = await startMockMysql(1); | ||
| try { | ||
| await using db = new SQL({ | ||
| url: `mysql://root:@127.0.0.1:${mock.port}/test?foundRows=false`, | ||
| max: 1, | ||
| idleTimeout: 1, | ||
| }); | ||
| await db.unsafe("UPDATE t SET v = 1"); | ||
| const { capabilityFlags } = await mock.captured; | ||
| expect((capabilityFlags & MYSQL_CLIENT_FOUND_ROWS) !== 0).toBe(false); | ||
| } finally { | ||
| mock.close(); | ||
| } | ||
| }); | ||
|
|
||
| test("URL with duplicate foundRows keys doesn't throw", async () => { | ||
| // toJSON() returns an array for duplicate keys; the coerced "true,false" | ||
| // is neither "false" nor "0", so the default (enabled) wins. | ||
| const mock = await startMockMysql(1); | ||
| try { | ||
| await using db = new SQL({ | ||
| url: `mysql://root:@127.0.0.1:${mock.port}/test?foundRows=true&foundRows=false`, | ||
| max: 1, | ||
| idleTimeout: 1, | ||
| }); | ||
| await db.unsafe("UPDATE t SET v = 1"); | ||
| const { capabilityFlags } = await mock.captured; | ||
| expect((capabilityFlags & MYSQL_CLIENT_FOUND_ROWS) !== 0).toBe(true); | ||
| } finally { | ||
| mock.close(); | ||
| } | ||
| }); | ||
|
|
||
| test("options object wins over URL query string", async () => { | ||
| const mock = await startMockMysql(1); | ||
| try { | ||
| await using db = new SQL({ | ||
| url: `mysql://root:@127.0.0.1:${mock.port}/test?foundRows=false`, | ||
| foundRows: true, | ||
| max: 1, | ||
| idleTimeout: 1, | ||
| } as Bun.SQL.Options); | ||
| await db.unsafe("UPDATE t SET v = 1"); | ||
| const { capabilityFlags } = await mock.captured; | ||
| expect((capabilityFlags & MYSQL_CLIENT_FOUND_ROWS) !== 0).toBe(true); | ||
| } finally { | ||
| mock.close(); | ||
| } | ||
| }); | ||
|
|
||
| test("affectedRows reflects the server's OK_Packet.affected_rows value", async () => { | ||
| const mock = await startMockMysql(1); | ||
| try { | ||
| await using db = new SQL({ | ||
| adapter: "mysql", | ||
| hostname: "127.0.0.1", | ||
| port: mock.port, | ||
| username: "root", | ||
| password: "", | ||
| database: "test", | ||
| max: 1, | ||
| idleTimeout: 1, | ||
| }); | ||
| const result: any = await db.unsafe("UPDATE t SET v = v WHERE id = 1"); | ||
| expect(result.affectedRows).toBe(1); | ||
| } finally { | ||
| mock.close(); | ||
| } | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.