Skip to content

Type Overrides in Column Names and Bind Arguments - #397

Merged
mehcode merged 9 commits into
masterfrom
ab/type-overrides
Jun 21, 2020
Merged

mehcode merged 9 commits into
masterfrom
ab/type-overrides

Conversation

@abonander

@abonander abonander commented Jun 11, 2020 •

Copy link
Copy Markdown
Collaborator

BASED ON #399

closes #339

TODO:

  • test bind argument overrides
  • test column name overrides for MySQL, SQLite
  • document column type overrides
  • document bind arg type overrides
  • implement, doc and test foo as "foo?" override (forgot this one)

@phated

phated commented Jun 14, 2020 •

Copy link
Copy Markdown
Contributor

Testing this out in my project and it seems to be working pretty well! The only trouble I ran into was a nullable bind param. I'm not sure if that falls into the WIP nullable override.

If I do my_val as Option<MyType>, it seems to wrap the input in another Option and the error says it expected Option<Option<MyType>>

Edit: Oh, the other issue is arrays of custom types. I expected my_column AS "my_column: Vec<MyType>" to work in Postgres.

@abonander

Copy link
Copy Markdown
Collaborator Author

Can you give the error message you're getting in the second case?

@phated

phated commented Jun 14, 2020

Copy link
Copy Markdown
Contributor

@abonander for my_column AS "my_column: Vec<MyType>", I get the message: error: encountered unexpected or invalid data: user-defined array types are unsupported

@abonander

Copy link
Copy Markdown
Collaborator Author

Hmm, that's coming from the protocol layer in core: https://github.com/launchbadge/sqlx/blob/8a83c98ee848a9bb9460d1824cf0150f64f557fd/sqlx-core/src/postgres/connection/describe.rs#L137

Unfortunately there's no comment here explaining why they're currently unsupported cough@mehcodecough

@abonander

Copy link
Copy Markdown
Collaborator Author

I think we just never really discussed how to represent arrays of user-defined types in the output of Describe. I think there's some refactoring necessary for PgTypeInfo probably, maybe elem_type() -> Option<PgTypeInfo> and array_type() -> Option<PgTypeInfo> getters.

@mehcode

mehcode commented Jun 15, 2020

Copy link
Copy Markdown
Contributor

Custom types in arrays is orthogonal to this pull request. The complicated part is how to do this with PgTypeInfo.

We currently have PgTypeInfo::with_name("my_custom_type"). That lazily resolves to the OID for the scalar type. Arrays are special in postgres in that they don't have a defined name (I mean they do have a name but its not something we or the user will know, it normally is a single-underscore prefix of the scalar name but that isn't guaranteed).

We need like PgTypeInfo::array_with_name("my_custom_type") or PgTypeInfo::array(PgTypeInfo::with_name("my_custom_type")) that would resolve the array type OID of the given scalar type name.

Once we figure out how we're doing that, actually making this work is a 5 line addition to the Type derive.

@phated

phated commented Jun 15, 2020

Copy link
Copy Markdown
Contributor

While poking at this today, I noticed that Postgres provides another column in the types table called typelem which is the OID of the inner element of the array. Not sure if that could help.

@abonander
abonander force-pushed the ab/type-overrides branch 3 times, most recently from 5e99f0f to 4cf09ba Compare June 16, 2020 03:29
@abonander
abonander marked this pull request as ready for review June 16, 2020 03:30
@abonander

abonander commented Jun 16, 2020 •

Copy link
Copy Markdown
Collaborator Author

@phated sorry try your my_val as Option<MyType> test now.

@abonander
abonander force-pushed the ab/type-overrides branch from 767c89d to 5ecf81b Compare June 18, 2020 00:58
@mehcode
mehcode merged commit 36a53b4 into master Jun 21, 2020
@mehcode
mehcode deleted the ab/type-overrides branch June 21, 2020 13:06
@phated

phated commented Jul 7, 2020

Copy link
Copy Markdown
Contributor

@abonander Sorry for the delay here. I was just able to test my_val as Option<MyType> and it works great! Did y'all want me to open an issue about user-defined array types?

@joaquindk

Copy link
Copy Markdown

Hi there, is there a way to use the query_as! macro to select all columns on a table and only override the type of 1 of them without having to list all of the columns?

Ie. I want to avoid having to do SELECT id as "id?", first_name, last_name, email from users WHERE id = $1 and instead do something like SELECT * WITH id as "id?" from users WHERE id = $1

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.

[Proposal] Type Override Annotation Syntax for Individual Columns

4 participants