mirror of
https://github.com/oven-sh/bun
synced 2026-02-09 18:38:55 +00:00
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:
@@ -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,
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user