-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Expand file tree
/
Copy pathDecodeBinaryValue.zig
More file actions
218 lines (210 loc) · 10 KB
/
DecodeBinaryValue.zig
File metadata and controls
218 lines (210 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/// MySQL's "binary" pseudo-charset ID. Columns with this character_set value
/// are true binary types (BINARY, VARBINARY, BLOB), as opposed to string columns
/// with binary collations (e.g., utf8mb4_bin) which have different character_set values.
pub const binary_charset: u16 = 63;
pub fn decodeBinaryValue(globalObject: *jsc.JSGlobalObject, field_type: types.FieldType, column_length: u32, raw: bool, bigint: bool, unsigned: bool, binary: bool, character_set: u16, comptime Context: type, reader: NewReader(Context)) !SQLDataCell {
debug("decodeBinaryValue: {s}", .{@tagName(field_type)});
return switch (field_type) {
.MYSQL_TYPE_TINY => {
if (raw) {
var data = try reader.read(1);
defer data.deinit();
return SQLDataCell.raw(&data);
}
const val = try reader.byte();
if (unsigned) {
return SQLDataCell{ .tag = .uint4, .value = .{ .uint4 = val } };
}
const ival: i8 = @bitCast(val);
return SQLDataCell{ .tag = .int4, .value = .{ .int4 = ival } };
},
.MYSQL_TYPE_SHORT => {
if (raw) {
var data = try reader.read(2);
defer data.deinit();
return SQLDataCell.raw(&data);
}
if (unsigned) {
return SQLDataCell{ .tag = .uint4, .value = .{ .uint4 = try reader.int(u16) } };
}
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);
defer data.deinit();
return SQLDataCell.raw(&data);
}
if (unsigned) {
return SQLDataCell{ .tag = .uint4, .value = .{ .uint4 = try reader.int(u32) } };
}
return SQLDataCell{ .tag = .int4, .value = .{ .int4 = try reader.int(i32) } };
},
.MYSQL_TYPE_LONGLONG => {
if (raw) {
return SQLDataCell.raw(&try reader.read(8));
}
if (unsigned) {
const val = try reader.int(u64);
if (val <= std.math.maxInt(u32)) {
return SQLDataCell{ .tag = .uint4, .value = .{ .uint4 = @intCast(val) } };
}
if (bigint) {
return SQLDataCell{ .tag = .uint8, .value = .{ .uint8 = val } };
}
var buffer: [22]u8 = undefined;
const slice = std.fmt.bufPrint(&buffer, "{d}", .{val}) catch unreachable;
return SQLDataCell{ .tag = .string, .value = .{ .string = if (slice.len > 0) bun.String.cloneUTF8(slice).value.WTFStringImpl else null }, .free_value = 1 };
}
const val = try reader.int(i64);
if (val >= std.math.minInt(i32) and val <= std.math.maxInt(i32)) {
return SQLDataCell{ .tag = .int4, .value = .{ .int4 = @intCast(val) } };
}
if (bigint) {
return SQLDataCell{ .tag = .int8, .value = .{ .int8 = val } };
}
var buffer: [22]u8 = undefined;
const slice = std.fmt.bufPrint(&buffer, "{d}", .{val}) catch unreachable;
return SQLDataCell{ .tag = .string, .value = .{ .string = if (slice.len > 0) bun.String.cloneUTF8(slice).value.WTFStringImpl else null }, .free_value = 1 };
},
.MYSQL_TYPE_FLOAT => {
if (raw) {
var data = try reader.read(4);
defer data.deinit();
return SQLDataCell.raw(&data);
}
return SQLDataCell{ .tag = .float8, .value = .{ .float8 = @as(f32, @bitCast(try reader.int(u32))) } };
},
.MYSQL_TYPE_DOUBLE => {
if (raw) {
var data = try reader.read(8);
defer data.deinit();
return SQLDataCell.raw(&data);
}
return SQLDataCell{ .tag = .float8, .value = .{ .float8 = @bitCast(try reader.int(u64)) } };
},
.MYSQL_TYPE_TIME => {
return switch (try reader.byte()) {
0 => {
const slice = "00:00:00";
return SQLDataCell{ .tag = .string, .value = .{ .string = if (slice.len > 0) bun.String.cloneUTF8(slice).value.WTFStringImpl else null }, .free_value = 1 };
},
8, 12 => |l| {
var data = try reader.read(l);
defer data.deinit();
const time = try Time.fromData(&data);
const total_hours = time.hours + time.days * 24;
// -838:59:59 to 838:59:59 is valid (it only store seconds)
// it should be represented as HH:MM:SS or HHH:MM:SS if total_hours > 99
var buffer: [32]u8 = undefined;
const sign = if (time.negative) "-" else "";
const slice = brk: {
if (total_hours > 99) {
break :brk std.fmt.bufPrint(&buffer, "{s}{d:0>3}:{d:0>2}:{d:0>2}", .{ sign, total_hours, time.minutes, time.seconds }) catch return error.InvalidBinaryValue;
} else {
break :brk std.fmt.bufPrint(&buffer, "{s}{d:0>2}:{d:0>2}:{d:0>2}", .{ sign, total_hours, time.minutes, time.seconds }) catch return error.InvalidBinaryValue;
}
};
return SQLDataCell{ .tag = .string, .value = .{ .string = if (slice.len > 0) bun.String.cloneUTF8(slice).value.WTFStringImpl else null }, .free_value = 1 };
},
else => return error.InvalidBinaryValue,
};
},
.MYSQL_TYPE_DATE, .MYSQL_TYPE_TIMESTAMP, .MYSQL_TYPE_DATETIME => switch (try reader.byte()) {
0 => {
// MySQL's binary protocol sends a zero-length payload for
// zero-date sentinels like '0000-00-00'. Return NaN so the
// JS side sees an Invalid Date, matching the text-protocol
// path instead of surfacing the epoch (1970-01-01T00:00:00Z).
return SQLDataCell{ .tag = .date, .value = .{ .date = std.math.nan(f64) } };
},
11, 7, 4 => |l| {
var data = try reader.read(l);
defer data.deinit();
const time = try DateTime.fromData(&data);
return SQLDataCell{ .tag = .date, .value = .{ .date = try time.toJSTimestamp(globalObject) } };
},
else => error.InvalidBinaryValue,
},
// When the column contains a binary string we return a Buffer otherwise a string
.MYSQL_TYPE_ENUM,
.MYSQL_TYPE_SET,
.MYSQL_TYPE_GEOMETRY,
.MYSQL_TYPE_NEWDECIMAL,
.MYSQL_TYPE_STRING,
.MYSQL_TYPE_VARCHAR,
.MYSQL_TYPE_VAR_STRING,
.MYSQL_TYPE_TINY_BLOB,
.MYSQL_TYPE_MEDIUM_BLOB,
.MYSQL_TYPE_LONG_BLOB,
.MYSQL_TYPE_BLOB,
=> {
if (raw) {
var data = try reader.rawEncodeLenData();
defer data.deinit();
return SQLDataCell.raw(&data);
}
var string_data = try reader.encodeLenString();
defer string_data.deinit();
// Only treat as binary if character_set indicates the binary pseudo-charset.
// The BINARY flag alone is insufficient because VARCHAR/CHAR columns
// with _bin collations (e.g., utf8mb4_bin) also have the BINARY flag set,
// but should return strings, not buffers.
if (binary and character_set == binary_charset) {
return SQLDataCell.raw(&string_data);
}
const slice = string_data.slice();
return SQLDataCell{ .tag = .string, .value = .{ .string = if (slice.len > 0) bun.String.cloneUTF8(slice).value.WTFStringImpl else null }, .free_value = 1 };
},
.MYSQL_TYPE_JSON => {
if (raw) {
var data = try reader.rawEncodeLenData();
defer data.deinit();
return SQLDataCell.raw(&data);
}
var string_data = try reader.encodeLenString();
defer string_data.deinit();
const slice = string_data.slice();
return SQLDataCell{ .tag = .json, .value = .{ .json = if (slice.len > 0) bun.String.cloneUTF8(slice).value.WTFStringImpl else null }, .free_value = 1 };
},
.MYSQL_TYPE_BIT => {
// BIT(1) is a special case, it's a boolean
if (column_length == 1) {
var data = try reader.encodeLenString();
defer data.deinit();
const slice = data.slice();
return SQLDataCell{ .tag = .bool, .value = .{ .bool = if (slice.len > 0 and slice[0] == 1) 1 else 0 } };
} else {
var data = try reader.encodeLenString();
defer data.deinit();
return SQLDataCell.raw(&data);
}
},
else => {
var data = try reader.read(column_length);
defer data.deinit();
return SQLDataCell.raw(&data);
},
};
}
const debug = bun.Output.scoped(.MySQLDecodeBinaryValue, .visible);
const std = @import("std");
const types = @import("../MySQLTypes.zig");
const NewReader = @import("./NewReader.zig").NewReader;
const SQLDataCell = @import("../../shared/SQLDataCell.zig").SQLDataCell;
const Value = @import("../MySQLTypes.zig").Value;
const DateTime = Value.DateTime;
const Time = Value.Time;
const bun = @import("bun");
const jsc = bun.jsc;