fix(Bun.SQL) handle MySQL Int24 (#22241)

### What does this PR do?
handle Int24 to be numbers
### How did you verify your code works?
tests

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
Ciro Spaciari
2025-08-29 17:03:26 -07:00
committed by GitHub
parent 684f7ecd09
commit a34e10db53
4 changed files with 31 additions and 1 deletions

View File

@@ -247,7 +247,7 @@ pub const FieldType = enum(u8) {
MYSQL_TYPE_NULL = 0x06,
MYSQL_TYPE_TIMESTAMP = 0x07,
MYSQL_TYPE_LONGLONG = 0x08,
MYSQL_TYPE_INT24 = 0x09,
MYSQL_TYPE_INT24 = 0x09, // MEDIUMINT
MYSQL_TYPE_DATE = 0x0a,
MYSQL_TYPE_TIME = 0x0b,
MYSQL_TYPE_DATETIME = 0x0c,

View File

@@ -25,6 +25,17 @@ pub fn decodeBinaryValue(globalObject: *jsc.JSGlobalObject, field_type: types.Fi
}
return SQLDataCell{ .tag = .int4, .value = .{ .int4 = try reader.int(i16) } };
},
.MYSQL_TYPE_INT24 => {
if (raw) {
var data = try reader.read(3);
defer data.deinit();
return SQLDataCell.raw(&data);
}
if (unsigned) {
return SQLDataCell{ .tag = .uint4, .value = .{ .uint4 = try reader.int(u24) } };
}
return SQLDataCell{ .tag = .int4, .value = .{ .int4 = try reader.int(i24) } };
},
.MYSQL_TYPE_LONG => {
if (raw) {
var data = try reader.read(4);

View File

@@ -73,6 +73,15 @@ pub const Row = struct {
cell.* = SQLDataCell{ .tag = .int4, .value = .{ .int4 = val } };
}
},
.MYSQL_TYPE_INT24 => {
if (column.flags.UNSIGNED) {
const val: u24 = std.fmt.parseInt(u24, value.slice(), 10) catch 0;
cell.* = SQLDataCell{ .tag = .uint4, .value = .{ .uint4 = val } };
} else {
const val: i24 = std.fmt.parseInt(i24, value.slice(), 10) catch std.math.minInt(i24);
cell.* = SQLDataCell{ .tag = .int4, .value = .{ .int4 = val } };
}
},
.MYSQL_TYPE_LONGLONG => {
if (column.flags.UNSIGNED) {
const val: u64 = std.fmt.parseInt(u64, value.slice(), 10) catch 0;

View File

@@ -276,6 +276,16 @@ describeWithContainer(
expect((await sql`select ${"hello"} as x`)[0].x).toBe("hello");
});
test("MediumInt/Int24", async () => {
let random_name = ("t_" + Bun.randomUUIDv7("hex").replaceAll("-", "")).toLowerCase();
await sql`CREATE TEMPORARY TABLE ${sql(random_name)} (a mediumint unsigned)`;
await sql`INSERT INTO ${sql(random_name)} VALUES (${1})`;
const result = await sql`select * from ${sql(random_name)}`;
expect(result[0].a).toBe(1);
const result2 = await sql`select * from ${sql(random_name)}`.simple();
expect(result2[0].a).toBe(1);
});
test("Boolean/TinyInt/BIT", async () => {
// Protocol will always return 0 or 1 for TRUE and FALSE when not using a table.
expect((await sql`select ${false} as x`)[0].x).toBe(0);