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

No access to field types for a query result #99

Open
dbussink opened this issue Jul 10, 2023 · 1 comment · May be fixed by #124
Open

No access to field types for a query result #99

dbussink opened this issue Jul 10, 2023 · 1 comment · May be fixed by #124

Comments

@dbussink
Copy link
Contributor

In mysql2, a result has a field_types method to get access to the field types of the result. Trilogy doesn't seem to provide any way to get access to these in the Ruby layer. The C driver does provide it, but it's not exposed in Ruby.

The Ruby driver does use it for casting purposes, so it's internally available. See also:

column_info[i].type = column.type;
column_info[i].flags = column.flags;
column_info[i].len = column.len;
column_info[i].charset = column.charset;
column_info[i].decimals = column.decimals;

There the data is read into column_info but that data is ephemeral for the result object and not included in the final result.

We were looking to move to Trilogy, but we do need access to this field type information in some way or another. From purely that perspective, it doesn't have to be compatible 1:1 with how mysql2 works, as long as it's available in some way.

I'm happy to contribute a PR to add this, but I don't really know what the preferred way of adding this would be. Should it be for example to column_info and then lazily create entries in a field_types method? Or should we build field_types directly like fields is built (although often it won't really be needed, as is clear since it's not present yet and already extensively used with Rails).

Or should there be a totally different function than field_types that returns the types not as strings like mysql2 does, but with integer constants + any of the additional flags / size attributes etc. for the type? If there's any preference with how to make this available, I'm also able to start a PR for that then.

@brianmario
Copy link
Contributor

I'd be in favor of an API that exposes all of the column info available from the C API.

Maybe something like Result#columns which returned an array of Result::Column classes that had those attributes like name, type, flags, etc. And yeah, having this method create the array of Result::Column's lazily would be ideal. This would sorta duplicate what's already available via the fields attribute, but maybe that could be removed in a future release, in favor of this new columns method, to avoid making a breaking change all at once?

ipc103 added a commit to ipc103/trilogy that referenced this issue Sep 28, 2023
Towards trilogy-libraries#99, this exposes additional field data on the query result
via a `Result#columns` method. We were already using this data in the
C extension to determine how to cast the column values. We now add a pointer
to the `column_info` struct onto the result and then lazily add the values
to the `Result` instance when called.

This method could eventually replace the `#fields` method which at the moment only
includes the column name.

Co-authored-by: Daniel Colson <[email protected]>
Co-authored-by: Charlotte Wen <[email protected]>
ipc103 added a commit to ipc103/trilogy that referenced this issue Sep 28, 2023
Towards trilogy-libraries#99, this exposes additional field data on the query result
via a `Result#columns` method. We were already using this data in the
C extension to determine how to cast the column values. We now add a pointer
to the `column_info` struct onto the result and then lazily add the values
to the `Result` instance when called.

This method could eventually replace the `#fields` method which at the moment only
includes the column name.

Co-authored-by: Daniel Colson <[email protected]>
Co-authored-by: Charlotte Wen <[email protected]>
ipc103 added a commit to ipc103/trilogy that referenced this issue Sep 28, 2023
Towards trilogy-libraries#99, this exposes additional field data on the query result
via a `Result#columns` method. We were already using this data in the
C extension to determine how to cast the column values. We now add a pointer
to the `column_info` struct onto the result and then lazily add the values
to the `Result` instance when called.

This method could eventually replace the `#fields` method which at the moment only
includes the column name.

Co-authored-by: Daniel Colson <[email protected]>
Co-authored-by: Charlotte Wen <[email protected]>
@ipc103 ipc103 linked a pull request Oct 3, 2023 that will close this issue
ipc103 added a commit to ipc103/trilogy that referenced this issue Oct 23, 2023
Towards trilogy-libraries#99, this exposes additional field data on the query result
via a `Result#columns` method. We were already using this data in the
C extension to determine how to cast the column values. We now add a pointer
to the `column_info` struct onto the result and then lazily add the values
to the `Result` instance when called.

This method could eventually replace the `#fields` method which at the moment only
includes the column name.

Co-authored-by: Daniel Colson <[email protected]>
Co-authored-by: Charlotte Wen <[email protected]>
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 a pull request may close this issue.

2 participants