Skip to content

Commit

Permalink
feat: write supports nullable types
Browse files Browse the repository at this point in the history
  • Loading branch information
speed2exe committed Nov 26, 2023
1 parent 4af3687 commit 9509ad9
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
2 changes: 2 additions & 0 deletions integration_tests/client.zig
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ test "binary data types" {
.{ -(1 << 7), -(1 << 15), -(1 << 23), -(1 << 31), -(1 << 63), 0, 0, 0, 0, 0 },
.{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
.{ (1 << 7) - 1, (1 << 15) - 1, (1 << 23) - 1, (1 << 31) - 1, (1 << 63) - 1, (1 << 8) - 1, (1 << 16) - 1, (1 << 24) - 1, (1 << 32) - 1, (1 << 64) - 1 },
.{ null, null, null, null, null, null, null, null, null, null },
};
inline for (params) |param| {
const exe_res = try c.execute(allocator, &prep_stmt, param);
Expand All @@ -299,6 +300,7 @@ test "binary data types" {
&.{ "-128", "-32768", "-8388608", "-2147483648", "-9223372036854775808", "0", "0", "0", "0", "0" },
&.{ "0", "0", "0", "0", "0", "0", "0", "0", "0", "0" },
&.{ "127", "32767", "8388607", "2147483647", "9223372036854775807", "255", "65535", "16777215", "4294967295", "18446744073709551615" },
&.{ null, null, null, null, null, null, null, null, null, null },
};
// std.debug.print("\n{?s}\n", .{table.rows[2][9]});
try std.testing.expectEqualDeep(expected, table.rows);
Expand Down
34 changes: 34 additions & 0 deletions src/helper.zig
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,40 @@ pub fn encodeBinaryParam(param: anytype, col_def: *const ColumnDefinition41, wri
const col_type: EnumFieldType = @enumFromInt(col_def.column_type);

switch (param_type_info) {
.Null => {
switch (col_type) {
.MYSQL_TYPE_LONGLONG => return try writer.writer.advance(8),
.MYSQL_TYPE_LONG,
.MYSQL_TYPE_INT24,
=> return try writer.writer.advance(4),
.MYSQL_TYPE_SHORT,
.MYSQL_TYPE_YEAR,
=> return try writer.writer.advance(2),
.MYSQL_TYPE_TINY => return try writer.writer.advance(1),
.MYSQL_TYPE_STRING,
.MYSQL_TYPE_VARCHAR,
.MYSQL_TYPE_VAR_STRING,
.MYSQL_TYPE_ENUM,
.MYSQL_TYPE_SET,
.MYSQL_TYPE_LONG_BLOB,
.MYSQL_TYPE_MEDIUM_BLOB,
.MYSQL_TYPE_BLOB,
.MYSQL_TYPE_TINY_BLOB,
.MYSQL_TYPE_GEOMETRY,
.MYSQL_TYPE_BIT,
.MYSQL_TYPE_DECIMAL,
.MYSQL_TYPE_NEWDECIMAL,
=> return try packet_writer.writeLengthEncodedString(writer, ""),
else => {},
}
},
.Optional => {
if (param) |p| {
return encodeBinaryParam(p, col_def, writer);
} else {
return encodeBinaryParam(null, col_def, writer);
}
},
.Int => |int| {
switch (col_type) {
.MYSQL_TYPE_LONGLONG => {
Expand Down
8 changes: 8 additions & 0 deletions src/stream_buffered.zig
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ pub const Writer = struct {
}
}

// increase the length of the buffer as if it was written to
pub fn advance(w: *Writer, offset: usize) !void {
if (w.len + offset > w.buf.len) {
return error.BufferNotEnoughSpace;
}
w.len += offset;
}

// if the buffer is full
inline fn full(w: *Writer) bool {
return w.len == w.buf.len;
Expand Down

0 comments on commit 9509ad9

Please sign in to comment.