Skip to content
22 changes: 22 additions & 0 deletions docs/runtime/sql.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -1407,6 +1407,28 @@ const now = await mysql`SELECT NOW() as current_time`;
const uuid = await mysql`SELECT UUID() as id`;
```

By default, `affectedRows` counts rows the `WHERE` clause **matched** — the
same default as the `mysql2` and `mariadb` drivers. An `UPDATE` that matches
a row but leaves every column untouched still reports `affectedRows: 1`. If
you want the server's changed-rows semantics instead (where a no-op update
returns `0`), pass `foundRows: false`:

```ts
const mysql = new SQL({
adapter: "mysql", // or "mariadb"
hostname: "localhost",
database: "myapp",
foundRows: false, // changed-rows semantics (returns 0 for no-op UPDATEs)
});
// Equivalently as a URL query param:
new SQL("mysql://user:pass@localhost/db?foundRows=false");
```

This option toggles the
[`CLIENT_FOUND_ROWS`](https://dev.mysql.com/doc/c-api/en/mysql-affected-rows.html)
capability flag during the protocol handshake and is ignored by the Postgres
and SQLite adapters.

### MySQL Error Handling

```ts
Expand Down
17 changes: 17 additions & 0 deletions packages/bun-types/sql.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,23 @@ declare module "bun" {
* @default false
*/
allowPublicKeyRetrieval?: boolean | undefined;

/**
* MySQL / MariaDB. When enabled, the server reports the number of rows
* matched by the `WHERE` clause in `affectedRows`, rather than the
* number of rows actually changed. This is the default behavior of the
* `mysql2` and `mariadb` drivers and matches what most Node.js MySQL
* code expects. Applies to both `adapter: "mysql"` and
* `adapter: "mariadb"`; no effect on `adapter: "postgres"`.
*
* Set to `false` to use the server's changed-rows semantics: an
* `UPDATE` that matches a row but does not change any column value
* will return `affectedRows: 0`.
*
* @see [CLIENT_FOUND_ROWS](https://dev.mysql.com/doc/c-api/en/mysql-affected-rows.html)
* @default true
*/
foundRows?: boolean | undefined;
Comment thread
robobun marked this conversation as resolved.
Resolved
}

/**
Expand Down
9 changes: 8 additions & 1 deletion src/codegen/bake-codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,14 @@ async function run() {
side: JSON.stringify(side),
IS_ERROR_RUNTIME: String(file === "error"),
IS_BUN_DEVELOPMENT: String(!!debug),
OVERLAY_CSS: css("../runtime/bake/client/overlay.css", !!debug),
// `Bun.build` parses `define:` values as JSON. The other defines
// above already go through `JSON.stringify` / `String`, but the
// raw minified CSS starts with `*{...}` — bun versions that lack
// the auto-quote fallback (#30679) reject it as "Operators are
// not allowed in JSON". Quote explicitly so cold builds work with
// the older bootstrap bun (1.3.13) used in the CI Dockerfile and
// any pre-fix local install.
Comment thread
robobun marked this conversation as resolved.
Resolved
OVERLAY_CSS: JSON.stringify(css("../runtime/bake/client/overlay.css", !!debug)),
},
minify: {
syntax: !debug,
Expand Down
1 change: 1 addition & 0 deletions src/js/internal/sql/mysql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ export interface MySQLDotZig {
maxLifetime: number,
useUnnamedPreparedStatements: boolean,
allowPublicKeyRetrieval: boolean,
foundRows: boolean,
) => $ZigGeneratedClasses.MySQLConnection;
createQuery: (
sql: string,
Expand Down
25 changes: 24 additions & 1 deletion src/js/internal/sql/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,8 @@
* Resolves the password (which may be a function and/or a promise) and calls
* the driver's native createConnection with the normalized pool options.
* Extra trailing arguments past `useUnnamedPreparedStatements` (MySQL's
* `allowPublicKeyRetrieval`) are ignored by drivers that don't take them.
* `allowPublicKeyRetrieval` and `foundRows`) are ignored by drivers that
* don't take them.
Comment thread
robobun marked this conversation as resolved.
Resolved
*/
async function createPooledConnectionHandle<ConnectionHandle>(
nativeCreateConnection: (...args: any[]) => ConnectionHandle,
Expand All @@ -870,6 +871,7 @@
prepare = true,
path,
allowPublicKeyRetrieval = false,
foundRows = true,
} = options;

let password: Bun.MaybePromise<string> | string | undefined | (() => Bun.MaybePromise<string>) = options.password;
Expand Down Expand Up @@ -904,6 +906,7 @@
maxLifetime,
!prepare,
!!allowPublicKeyRetrieval,
!!foundRows,
);
} catch (e) {
// defer so the callback never runs while the adapter is still filling
Expand Down Expand Up @@ -1796,6 +1799,10 @@
let bigint: boolean | undefined;
let path: string;
let prepare: boolean = true;
// MySQL-only. Defaults to `true` to match the `mysql2` and `mariadb` drivers
// (both enable CLIENT_FOUND_ROWS by default). Read from the options object
// and the URL query string below; ignored for non-MySQL adapters.
Comment thread
robobun marked this conversation as resolved.
Resolved
let foundRows: boolean = true;

if (url !== null) {
url = url instanceof URL ? url : new URL(url);
Expand Down Expand Up @@ -1828,6 +1835,14 @@
}
} else if (lowerKey === "path") {
path = queryObject[key];
} else if (lowerKey === "foundrows") {
// Accept "false"/"0" (case-insensitive) to disable; anything else
// (including "true"/"1" or empty) leaves the default enabled. Only
// consumed by the MySQL adapter. `toJSON()` returns an array when a
// key appears more than once (`?foundRows=a&foundRows=b`), so coerce
// to a string before calling `.toLowerCase`.
Comment thread
robobun marked this conversation as resolved.
Resolved
const value = `${queryObject[key]}`.toLowerCase();
foundRows = !(value === "false" || value === "0");
} else {
// this is valid for postgres for other databases it might not be valid
// check adapter then implement for other databases
Expand Down Expand Up @@ -2001,6 +2016,13 @@
prepare = false;
}

// The options-object form wins over the URL query string. `foundRows` is
// only meaningful for the MySQL wire protocol (maps to CLIENT_FOUND_ROWS);
// for Postgres/SQLite it's a no-op.
Comment thread
robobun marked this conversation as resolved.
Resolved
if (options.foundRows !== undefined) {

Check failure on line 2022 in src/js/internal/sql/shared.ts

View workflow job for this annotation

GitHub Actions / Lint JavaScript

bun(no-duplicate-conditional-property-access)

`options.foundRows` is read in the `if` condition and again in the body. Read it into a local first (e.g. `const { foundRows } = options`) so the property is only accessed once.
foundRows = !!options.foundRows;
}

onconnect ??= options.onconnect;
onclose ??= options.onclose;

Expand Down Expand Up @@ -2101,6 +2123,7 @@
sslMode,
query,
max: max || 10,
foundRows,
};

if (idleTimeout != null) {
Expand Down
2 changes: 1 addition & 1 deletion src/js/private.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ declare module "bun" {
/**
* Represents the result of the `parseOptions()` function in the postgres, mysql or mariadb path
*/
type DefinedPostgresOrMySQLOptions = Define<Bun.SQL.PostgresOrMySQLOptions, "max" | "prepare" | "max"> & {
type DefinedPostgresOrMySQLOptions = Define<Bun.SQL.PostgresOrMySQLOptions, "max" | "prepare" | "foundRows"> & {
sslMode: import("internal/sql/shared").SSLMode;
query: string;
};
Expand Down
11 changes: 11 additions & 0 deletions src/sql_jsc/mysql/JSMySQLConnection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,16 @@ impl JSMySQLConnection {
// MySQL doesn't support unnamed prepared statements
let _ = use_unnamed_prepared_statements;
let allow_public_key_retrieval = callframe.argument(15).to_boolean();
// `foundRows: true` (default) asks the server to return matched-rows
// counts instead of changed-rows counts in OK_Packet.affected_rows,
// matching the `mysql2` / `mariadb` driver defaults. Enabled at
// handshake time by OR-ing `CLIENT_FOUND_ROWS` into the client
// capability set. `callframe.argument(16)` returns UNDEFINED when
// the JS layer omits the arg; `to_boolean` coerces UNDEFINED to
// `false` so we fall back to Bun's pre-fix changed-rows default in
// that case. Current `src/js/internal/sql/mysql.ts` always passes
// an explicit JS boolean so the default still fires there.
Comment thread
robobun marked this conversation as resolved.
Resolved
let found_rows = callframe.argument(16).to_boolean();

// Ownership transferred into `ptr.connection`; disarm the errdefer so the
// connect-fail `ptr.deref()` is the sole cleanup path from here on.
Expand All @@ -523,6 +533,7 @@ impl JSMySQLConnection {
secure,
args.ssl_mode,
allow_public_key_retrieval,
found_rows,
)),
auto_flusher: JsCell::new(AutoFlusher::default()),
idle_timeout_interval_ms: u32::try_from(idle_timeout).expect("int cast"),
Expand Down
19 changes: 16 additions & 3 deletions src/sql_jsc/mysql/MySQLConnection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ pub struct MySQLConnection {
ssl_mode: SSLMode,
allow_public_key_retrieval: bool,
flags: ConnectionFlags,
/// When `true`, request `CLIENT_FOUND_ROWS` during handshake so
/// `affected_rows` counts rows matched by `WHERE` instead of rows whose
/// column values actually changed (mysql2 / mariadb default).
Comment thread
robobun marked this conversation as resolved.
Resolved
found_rows: bool,
}

impl Default for MySQLConnection {
Expand Down Expand Up @@ -125,6 +129,7 @@ impl Default for MySQLConnection {
ssl_mode: SSLMode::Disable,
allow_public_key_retrieval: false,
flags: ConnectionFlags::default(),
found_rows: true,
}
}
}
Expand All @@ -144,6 +149,7 @@ impl MySQLConnection {
secure: Option<*mut SslCtx>,
ssl_mode: SSLMode,
allow_public_key_retrieval: bool,
found_rows: bool,
) -> Self {
Self {
database,
Expand All @@ -164,6 +170,7 @@ impl MySQLConnection {
TLSStatus::None
},
character_set: CharacterSet::default(),
found_rows,
..Default::default()
}
}
Expand Down Expand Up @@ -664,11 +671,17 @@ impl MySQLConnection {
// server's advertised capabilities. This ensures features like CLIENT_DEPRECATE_EOF
// are only used when the server actually supports them (critical for MySQL-compatible
// databases like StarRocks, TiDB, SingleStore, etc.).
self.capabilities = Capabilities::get_default_capabilities(
let mut requested = Capabilities::get_default_capabilities(
self.ssl_mode != SSLMode::Disable,
!self.database.is_empty(),
)
.intersect(handshake.capability_flags);
);
// CLIENT_FOUND_ROWS (`foundRows` connection option; default on) tells
// the server to report rows matched by `WHERE` in affected_rows rather
// than rows actually changed. Matches the mysql2 / mariadb driver
// defaults so an `UPDATE` that matches but doesn't change values still
// returns `affectedRows: 1`.
Comment thread
robobun marked this conversation as resolved.
Resolved
requested.CLIENT_FOUND_ROWS = self.found_rows;
self.capabilities = requested.intersect(handshake.capability_flags);
self.mariadb_capabilities = MariaDBCapabilities::get_default_capabilities()
.intersect(handshake.mariadb_capability_flags);

Expand Down
Loading
Loading