Skip to content

Commit 1620280

Browse files
committed
Add precision to time, datetime, and timestamp field types
1 parent 57b8df1 commit 1620280

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

ext/mysql2/result.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -287,17 +287,17 @@ static VALUE rb_mysql_result_fetch_field_type(VALUE self, unsigned int idx) {
287287
rb_field_type = rb_sprintf("double(%ld,%d)", field->length, field->decimals);
288288
break;
289289
case MYSQL_TYPE_TIME: // MYSQL_TIME
290-
rb_field_type = rb_str_new_cstr("time");
290+
rb_field_type = rb_sprintf("time(%d)", field->decimals);
291291
break;
292292
case MYSQL_TYPE_DATE: // MYSQL_TIME
293293
case MYSQL_TYPE_NEWDATE: // MYSQL_TIME
294294
rb_field_type = rb_str_new_cstr("date");
295295
break;
296296
case MYSQL_TYPE_DATETIME: // MYSQL_TIME
297-
rb_field_type = rb_str_new_cstr("datetime");
297+
rb_field_type = rb_sprintf("datetime(%d)", field->decimals);
298298
break;
299299
case MYSQL_TYPE_TIMESTAMP: // MYSQL_TIME
300-
rb_field_type = rb_str_new_cstr("timestamp");
300+
rb_field_type = rb_sprintf("timestamp(%d)", field->decimals);
301301
break;
302302
case MYSQL_TYPE_DECIMAL: // char[]
303303
case MYSQL_TYPE_NEWDECIMAL: // char[]

spec/mysql2/result_spec.rb

+19-3
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,9 @@
148148
decimal(10,3)
149149
decimal(10,3)
150150
date
151-
datetime
152-
timestamp
153-
time
151+
datetime(0)
152+
timestamp(0)
153+
time(0)
154154
year(4)
155155
char(13)
156156
varchar(13)
@@ -199,6 +199,22 @@
199199
expect(result.field_types).to eql(expected_types)
200200
end
201201

202+
it "should return precision for timestamps" do
203+
result = @client.query(
204+
"SELECT now(), " \
205+
"cast(now() as datetime(3)), " \
206+
"cast(now() as datetime(6))",
207+
)
208+
209+
expected_types = %w[
210+
timestamp(0)
211+
datetime(3)
212+
datetime(6)
213+
]
214+
215+
expect(result.field_types).to eql(expected_types)
216+
end
217+
202218
it "should return json type on mysql 8.0" do
203219
next unless /8.\d+.\d+/ =~ @client.server_info[:version]
204220

0 commit comments

Comments
 (0)