From a5914e63e846dcd3b2127fe298dfe841134d5b62 Mon Sep 17 00:00:00 2001 From: Fu Zi Xiang Date: Mon, 25 Dec 2023 11:40:15 +0800 Subject: [PATCH] added more datetime test case --- integration_tests/client.zig | 47 +++++++++++++++++++++++++----------- src/helper.zig | 18 +++++++++++++- 2 files changed, 50 insertions(+), 15 deletions(-) diff --git a/integration_tests/client.zig b/integration_tests/client.zig index e085962..a8d376f 100644 --- a/integration_tests/client.zig +++ b/integration_tests/client.zig @@ -442,27 +442,44 @@ test "binary data types - temporal" { try queryExpectOk(&c, \\ \\CREATE TABLE test.temporal_types_example ( - \\ event_time DATETIME(6) NOT NULL + \\ event_time DATETIME(6) NOT NULL, + \\ event_time2 DATETIME(2) NOT NULL, + \\ event_time3 DATETIME 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 (?)"); + 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 my_time: DateTime = .{ + .year = 2023, + .month = 11, + .day = 30, + .hour = 6, + .minute = 50, + .second = 58, + .microsecond = 123456, + }; + const without_ms: DateTime = .{ + .year = 2023, + .month = 11, + .day = 30, + .hour = 6, + .minute = 50, + .second = 58, + }; + const only_day: DateTime = .{ + .year = 2023, + .month = 11, + .day = 30, + }; + const params = .{ - .{ - @as(DateTime, .{ - .year = 2023, - .month = 11, - .day = 30, - .hour = 6, - .minute = 50, - .second = 58, - .microsecond = 123456, - }), - }, + .{ my_time, my_time, my_time }, + .{ without_ms, without_ms, without_ms }, + .{ only_day, only_day, only_day }, }; inline for (params) |param| { @@ -480,7 +497,9 @@ test "binary data types - temporal" { defer table.deinit(allocator); const expected: []const []const ?[]const u8 = &.{ - &.{"2023-11-30 06:50:58.123456"}, + &.{ "2023-11-30 06:50:58.123456", "2023-11-30 06:50:58.12", "2023-11-30 06:50:58" }, + &.{ "2023-11-30 06:50:58.000000", "2023-11-30 06:50:58.00", "2023-11-30 06:50:58" }, + &.{ "2023-11-30 00:00:00.000000", "2023-11-30 00:00:00.00", "2023-11-30 00:00:00" }, }; try std.testing.expectEqualDeep(expected, table.rows); diff --git a/src/helper.zig b/src/helper.zig index 9e31fa9..f23d48a 100644 --- a/src/helper.zig +++ b/src/helper.zig @@ -35,7 +35,10 @@ pub fn encodeBinaryParam(param: anytype, col_def: *const ColumnDefinition41, wri switch (param_type_info) { .Struct => { switch (col_type) { - .MYSQL_TYPE_DATETIME => { + .MYSQL_TYPE_DATE, + .MYSQL_TYPE_DATETIME, + .MYSQL_TYPE_TIMESTAMP, + => { return try encodeDateTime(param, writer); }, else => {}, @@ -454,4 +457,17 @@ pub const TableTexts = struct { allocator.free(t.rows); allocator.free(t.elems); } + + pub fn debugPrint(t: *const TableTexts) void { + const print = std.debug.print; + for (t.rows, 0..) |row, i| { + print("row: {d} -> ", .{i}); + print("|", .{}); + for (row) |elem| { + print("{?s}", .{elem}); + print("|", .{}); + } + print("\n", .{}); + } + } };