Skip to content

Add precision to time, datetime, and timestamp field types #1394

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions ext/mysql2/result.c
Original file line number Diff line number Diff line change
@@ -287,17 +287,17 @@ static VALUE rb_mysql_result_fetch_field_type(VALUE self, unsigned int idx) {
rb_field_type = rb_sprintf("double(%ld,%d)", field->length, field->decimals);
break;
case MYSQL_TYPE_TIME: // MYSQL_TIME
rb_field_type = rb_str_new_cstr("time");
rb_field_type = rb_sprintf("time(%d)", field->decimals);
break;
case MYSQL_TYPE_DATE: // MYSQL_TIME
case MYSQL_TYPE_NEWDATE: // MYSQL_TIME
rb_field_type = rb_str_new_cstr("date");
break;
case MYSQL_TYPE_DATETIME: // MYSQL_TIME
rb_field_type = rb_str_new_cstr("datetime");
rb_field_type = rb_sprintf("datetime(%d)", field->decimals);
break;
case MYSQL_TYPE_TIMESTAMP: // MYSQL_TIME
rb_field_type = rb_str_new_cstr("timestamp");
rb_field_type = rb_sprintf("timestamp(%d)", field->decimals);
break;
case MYSQL_TYPE_DECIMAL: // char[]
case MYSQL_TYPE_NEWDECIMAL: // char[]
22 changes: 19 additions & 3 deletions spec/mysql2/result_spec.rb
Original file line number Diff line number Diff line change
@@ -148,9 +148,9 @@
decimal(10,3)
decimal(10,3)
date
datetime
timestamp
time
datetime(0)
timestamp(0)
time(0)
year(4)
char(13)
varchar(13)
@@ -199,6 +199,22 @@
expect(result.field_types).to eql(expected_types)
end

it "should return precision for datetimes" do
result = @client.query(
"SELECT cast(now() as datetime), " \
"cast(now() as datetime(3)), " \
"cast(now() as datetime(6))",
)

expected_types = %w[
datetime(0)
datetime(3)
datetime(6)
]

expect(result.field_types).to eql(expected_types)
end

it "should return json type on mysql 8.0" do
next unless /8.\d+.\d+/ =~ @client.server_info[:version]

Loading