Skip to content

Commit

Permalink
prepare for temporal types
Browse files Browse the repository at this point in the history
  • Loading branch information
speed2exe committed Dec 23, 2023
1 parent 0ed09ec commit 627e2c7
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
57 changes: 56 additions & 1 deletion integration_tests/client.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const allocator = std.testing.allocator;
const ErrorPacket = @import("../src/protocol.zig").generic_response.ErrorPacket;
const minInt = std.math.minInt;
const maxInt = std.math.maxInt;
const DateTime = @import("../src/temporal.zig").DateTime;

// convenient function for testing
fn queryExpectOk(c: *Client, query: []const u8) !void {
Expand Down Expand Up @@ -431,5 +432,59 @@ test "binary data types - string" {
}
}

// test "binary data types - temporal" {
// var c = Client.init(test_config);
// defer c.deinit();
//
//// SELECT CONCAT(?, ?) AS col1
// try queryExpectOk(&c, "CREATE DATABASE test");
// defer queryExpectOk(&c, "DROP DATABASE test") catch {};
//
// try queryExpectOk(&c,
// \\
// \\CREATE TABLE test.temporal_types_example (
// \\ event_time DATETIME(6) NOT NULL
// \\)
// );
// defer queryExpectOk(&c, "DROP TABLE test.temporal_types_example") catch {};
//
// const prep_res = try c.prepare(allocator, "INSERT INTO test.temporal_types_example VALUES (?)");
// defer prep_res.deinit(allocator);
// const prep_stmt = try prep_res.expect(.ok);
//
// const params = .{
// .{
// @as(DateTime, .{
// .year = 2023,
// .month = 11,
// .day = 30,
// .hour = 6,
// .minute = 50,
// .second = 58,
// .microsecond = 123456,
// }),
// },
// };
// inline for (params) |param| {
// const exe_res = try c.execute(allocator, &prep_stmt, param);
// defer exe_res.deinit(allocator);
// _ = try exe_res.expect(.ok);
// }
//
// {
// const res = try c.query(allocator, "SELECT * FROM test.temporal_types_example");
// defer res.deinit(allocator);
// const rows_iter = (try res.expect(.rows)).iter();
//
// const table = try rows_iter.collect(allocator);
// defer table.deinit(allocator);
//
// const expected: []const []const ?[]const u8 = &.{
// &.{""},
// };
// try std.testing.expectEqualDeep(expected, table.rows);
// }
// }

//
// SELECT CONCAT(?, ?) AS col1
// event_time DATETIME(6) NOT NULL
22 changes: 22 additions & 0 deletions src/temporal.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Type for MYSQL_TYPE_DATE, MYSQL_TYPE_DATETIME and MYSQL_TYPE_TIMESTAMP, i.e. When was it?
// https://dev.mysql.com/doc/dev/mysql-server/latest/page_protocol_binary_resultset.html#sect_protocol_binary_resultset_row_value_date
pub const DateTime = struct {
year: u16,
month: u8,
day: u8,
hour: u8,
minute: u8,
second: u8,
microsecond: u32,
};

// Type for MYSQL_TYPE_TIME, i.e. How long did it take?
// `Time` is ambigious and confusing, `Duration` was chosen as the name instead
// https://dev.mysql.com/doc/dev/mysql-server/latest/page_protocol_binary_resultset.html#sect_protocol_binary_resultset_row_value_time
pub const Duration = struct {
days: u32,
hour: u8,
minute: u8,
second: u8,
microsecond: u32,
};

0 comments on commit 627e2c7

Please sign in to comment.