Skip to content

Commit

Permalink
feat: fix runtime parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
speed2exe committed Oct 28, 2024
1 parent ed19b17 commit 5279117
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 47 deletions.
8 changes: 1 addition & 7 deletions .github/workflows/integrations.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@ name: Integration Tests

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
build:
Expand All @@ -20,8 +15,7 @@ jobs:

- name: install zig
run: |
ZIG_VERSION=0.12.0
wget https://ziglang.org/builds/zig-linux-x86_64-$ZIG_VERSION.tar.xz
wget https://ziglang.org/download/0.13.0/zig-linux-x86_64-0.13.0.tar.xz
tar xf zig-linux-x86_64-$ZIG_VERSION.tar.xz
mv zig-linux-x86_64-$ZIG_VERSION $HOME/zig-build
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/unit_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ jobs:
- uses: actions/checkout@v3
- name: install zig
run: |
ZIG_VERSION=0.12.0-dev.3291+17bad9f88
wget https://ziglang.org/builds/zig-linux-x86_64-$ZIG_VERSION.tar.xz
wget https://ziglang.org/download/0.13.0/zig-linux-x86_64-0.13.0.tar.xz
tar xf zig-linux-x86_64-$ZIG_VERSION.tar.xz
mv zig-linux-x86_64-$ZIG_VERSION $HOME/zig-build
Expand Down
6 changes: 5 additions & 1 deletion integration_tests/conn.zig
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ test "select concat with params" {
const prep_res = try c.prepare(allocator, "SELECT CONCAT(?, ?) AS col1");
defer prep_res.deinit(allocator);
const prep_stmt = try prep_res.expect(.stmt);
const res = try c.executeRows(&prep_stmt, .{ "hello", "world" });
const res = try c.executeRows(&prep_stmt, .{ runtimeValue("hello"), runtimeValue("world") });
const rows: ResultSet(BinaryResultRow) = try res.expect(.rows);
const rows_iter = rows.iter();

Expand All @@ -707,3 +707,7 @@ test "select concat with params" {
try std.testing.expectEqualDeep(expected, structs.struct_list.items);
}
}

fn runtimeValue(a: anytype) @TypeOf(a) {
return a;
}
50 changes: 13 additions & 37 deletions src/protocol/prepared_statements.zig
Original file line number Diff line number Diff line change
Expand Up @@ -111,19 +111,13 @@ pub const ExecuteRequest = struct {
//if (e.new_params_bind_flag > 0) {
comptime var enum_field_types: [params.len]constants.EnumFieldType = undefined;
inline for (params, &enum_field_types) |param, *enum_field_type| {
enum_field_type.* = comptime enumFieldTypeFromParam(param);
enum_field_type.* = comptime enumFieldTypeFromParam(@TypeOf(param));
}

inline for (enum_field_types, params) |enum_field_type, param| {
try writer.writeInt(u8, @intFromEnum(enum_field_type));
const sign_flag = comptime switch (@typeInfo(@TypeOf(param))) {
.ComptimeInt => switch (enum_field_type) {
.MYSQL_TYPE_TINY => if (param > maxInt(i8)) 0x80 else 0,
.MYSQL_TYPE_SHORT => if (param > maxInt(i16)) 0x80 else 0,
.MYSQL_TYPE_LONG => if (param > maxInt(i32)) 0x80 else 0,
.MYSQL_TYPE_LONGLONG => if (param > maxInt(i64)) 0x80 else 0,
else => 0,
},
.ComptimeInt => if (param > maxInt(i64)) 0x80 else 0,
.Int => |int| if (int.signedness == .unsigned) 0x80 else 0,
else => 0,
};
Expand All @@ -146,10 +140,11 @@ pub const ExecuteRequest = struct {
// TODO: Write params and attr as binary values
// Write params as binary values
inline for (params, enum_field_types) |param, enum_field_type| {
// if (enum_field_type == constants.EnumFieldType.MYSQL_TYPE_NULL) {
// continue;
// }
try writeParamAsFieldType(writer, enum_field_type, param);
if (isNull(param)) {
try writeParamAsFieldType(writer, constants.EnumFieldType.MYSQL_TYPE_NULL, param);
} else {
try writeParamAsFieldType(writer, enum_field_type, param);
}
}

// if (has_attributes_to_write) {
Expand All @@ -161,21 +156,14 @@ pub const ExecuteRequest = struct {
}
};

fn enumFieldTypeFromParam(param: anytype) constants.EnumFieldType {
const Param = @TypeOf(param);
fn enumFieldTypeFromParam(Param: type) constants.EnumFieldType {
const param_type_info = @typeInfo(Param);
return switch (Param) {
DateTime => constants.EnumFieldType.MYSQL_TYPE_DATETIME,
Duration => constants.EnumFieldType.MYSQL_TYPE_TIME,
else => switch (param_type_info) {
.Null => return constants.EnumFieldType.MYSQL_TYPE_NULL,
.Optional => {
if (param) |p| {
return enumFieldTypeFromParam(p);
} else {
return constants.EnumFieldType.MYSQL_TYPE_NULL;
}
},
.Optional => |o| return enumFieldTypeFromParam(o.child),
.Int => |int| {
if (int.bits <= 8) {
return constants.EnumFieldType.MYSQL_TYPE_TINY;
Expand All @@ -187,19 +175,7 @@ fn enumFieldTypeFromParam(param: anytype) constants.EnumFieldType {
return constants.EnumFieldType.MYSQL_TYPE_LONGLONG;
}
},
.ComptimeInt => {
if (std.math.minInt(i8) <= param and param <= std.math.maxInt(u8)) {
return constants.EnumFieldType.MYSQL_TYPE_TINY;
} else if (std.math.minInt(i16) <= param and param <= std.math.maxInt(u16)) {
return constants.EnumFieldType.MYSQL_TYPE_SHORT;
} else if (std.math.minInt(i32) <= param and param <= std.math.maxInt(u32)) {
return constants.EnumFieldType.MYSQL_TYPE_LONG;
} else if (std.math.minInt(i64) <= param and param <= std.math.maxInt(u64)) {
return constants.EnumFieldType.MYSQL_TYPE_LONGLONG;
} else {
@compileLog("hello");
}
},
.ComptimeInt => return constants.EnumFieldType.MYSQL_TYPE_LONGLONG,
.Float => |float| {
if (float.bits <= 32) {
return constants.EnumFieldType.MYSQL_TYPE_FLOAT;
Expand All @@ -221,7 +197,7 @@ fn enumFieldTypeFromParam(param: anytype) constants.EnumFieldType {
.Enum => return constants.EnumFieldType.MYSQL_TYPE_STRING,
.Pointer => |pointer| {
switch (pointer.size) {
.One => return enumFieldTypeFromParam(param.*),
.One => return enumFieldTypeFromParam(pointer.child),
else => {},
}
switch (@typeInfo(pointer.child)) {
Expand All @@ -237,7 +213,7 @@ fn enumFieldTypeFromParam(param: anytype) constants.EnumFieldType {
}
},
else => {
@compileLog(param);
@compileLog(Param);
@compileError("unsupported type");
},
},
Expand Down Expand Up @@ -471,7 +447,7 @@ pub fn nullBitsParamsAttrs(params: anytype, start: usize, attrs: []const BinaryP
}

inline fn isNull(param: anytype) bool {
return switch (@typeInfo(@TypeOf(param))) {
return comptime switch (@typeInfo(@TypeOf(param))) {
inline .Optional => if (param) |p| isNull(p) else true,
inline .Null => true,
inline else => false,
Expand Down

0 comments on commit 5279117

Please sign in to comment.