Skip to content
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

Implementing type reflection from mysql result #1068

Merged
merged 1 commit into from
Mar 16, 2020

Conversation

danhuynhdev
Copy link
Contributor

Add Mysql2::Result#field_types method to get field types from result.

Resolve: #1067

ext/mysql2/result.c Outdated Show resolved Hide resolved
ext/mysql2/result.c Outdated Show resolved Hide resolved
ext/mysql2/result.c Outdated Show resolved Hide resolved
@danhuynhdev danhuynhdev force-pushed the master branch 2 times, most recently from 0e68a95 to e811604 Compare September 8, 2019 16:37
@sodabrew sodabrew added this to the 0.6.0 milestone Oct 2, 2019
@sodabrew
Copy link
Collaborator

sodabrew commented Mar 9, 2020

This looks good! Few smaller comments, and please rebase onto current master. (It appears there are no conflicts, but we'll get confirmation of unit tests.)

@danhuynhdev danhuynhdev force-pushed the master branch 3 times, most recently from 1271210 to 5b9735b Compare March 11, 2020 18:08
@danhuynhdev
Copy link
Contributor Author

I've resolved the comments and squash the commits. Please feedback if any change is needed. cc: @sodabrew

ext/mysql2/result.c Outdated Show resolved Hide resolved
case MYSQL_TYPE_GEOMETRY: // char[]
rb_field_type = rb_str_new_cstr("geometry");
break;
default:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand it well it will return nil for unknown type, right?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder how we can force a unit test down the no-match path?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we know a unknown to test for it, would it be unknown anymore 😆?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anyway I've made it return 'unknown' for this case.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was looking into this @sodabrew and I have found out there's no easy way how to introduce custom column type in MySQL :/

But maybe there's some way how to create table including all MySQL data types (by some meta programming) and it would be possible to check if we cover all cases there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will fail if we include MYSQL_TYPE_JSON though. I cannot find any code to handle MYSQL_TYPE_JSON.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #693 for the history on how JSON is supported; basically it's a fall-through to char[].

It seems to me that the type table should be updated to explicitly include JSON so that it's not "accidentally" supported but is explictly routed through the char[] path of the result handler.

What is the failure you're seeing if MYSQL_TYPE_JSON is included in this table?

@simi
Copy link
Contributor

simi commented Mar 11, 2020

🤔 I was looking for any chance to keep the list of types updated when new type would be introduced (by exposing enum_field_types and ensuring we handle all supported types), but looking into enum_field_types history, it is rarely touched and seems pretty stable.

ext/mysql2/result.c Outdated Show resolved Hide resolved
ext/mysql2/result.c Outdated Show resolved Hide resolved
ext/mysql2/result.c Outdated Show resolved Hide resolved
case MYSQL_TYPE_GEOMETRY: // char[]
rb_field_type = rb_str_new_cstr("geometry");
break;
default:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder how we can force a unit test down the no-match path?

@danhuynhdev danhuynhdev force-pushed the master branch 4 times, most recently from 957b0ac to 17297cb Compare March 13, 2020 09:45
@danhuynhdev
Copy link
Contributor Author

Seems like mysql2 cannot handle JSON type in query result. I've attempted to add JSON type to the list but it just failed right away.

@sodabrew
Copy link
Collaborator

The JSON type should be supported, what error are you seeing?

@danhuynhdev
Copy link
Contributor Author

danhuynhdev commented Mar 15, 2020

This is actually not an error on our side but on mysql's json support. I'm using mariadb on my local machine and they don't support JSON type. Also MYSQL_TYPE_JSON only exist on mysql 5.7.8 onward. Attempt to add it into the table will result in this error:

../../../../ext/mysql2/result.c:320:12: error: ‘MYSQL_TYPE_JSON’ undeclared (first use in this function)
       case MYSQL_TYPE_JSON:     // char[]

@danhuynhdev
Copy link
Contributor Author

Not sure if we should add something like this for MYSQL_TYPE_JSON

#ifdef MYSQL_TYPE_JSON
      case MYSQL_TYPE_JSON:     // json
        rb_field_type = rb_str_new_cstr("json");
        break;
#endif

or just declare it if not exist

#ifnotdef MYSQL_TYPE_JSON
#define MYSQL_TYPE_JSON 245
#endif 

I'm inclined to do latter since it doesn't require user to install the correct header version on the client side.

@sodabrew
Copy link
Collaborator

I was going to say we should do the first one, but you're right that some clients may have been compiled with an older library, and the column type number is should always be the same for compatibility reasons.

@danhuynhdev danhuynhdev force-pushed the master branch 3 times, most recently from 41fcc21 to 371e120 Compare March 16, 2020 03:13
Copy link
Collaborator

@sodabrew sodabrew left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great! Thanks for updating your PR along with all the review comments.

@sodabrew sodabrew merged commit c3d1abc into brianmario:master Mar 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature Request: get field types from query result
3 participants