sqlx icon indicating copy to clipboard operation
sqlx copied to clipboard

Export `PgType`

Open forrest-akin opened this issue 3 years ago • 8 comments

This issue is to request exporting of the PgType enum from the postgres module.

I'm struggling a bit with serializing rows from arbitrary queries to JSON. Ideally, PgRows would have a convenient way of being serialized to JSON (even if you don't know the structure ahead of time). I haven't found a good way to do this, so instead I'm iterating over column metadata for each row and mapping the value to rust types according to the value's type_info per the documented type mappings. Unfortunately, I'm having to match against the .name() of each type since it's the only public method that seems to be available for me to see the type. Deref is implemented for PgTypeInfo so I'm able to get a PgType, but since the enum isn't exported by the postgres module I'm not able to match on it.

forrest-akin avatar Aug 10 '21 20:08 forrest-akin

+1

I'm waiting this feature too. I tried to implement Type<Postgres> for my structure and I also need PgType.

Lispython avatar Aug 19 '21 18:08 Lispython

Yeah it is a really simple fix to make the type enums public instead of protected. Is there a good reason we only have access to the type via it's string name?

garmoned avatar Sep 27 '21 16:09 garmoned

This will be done but the main issue is how breaking a change it will be to change the type enumeration around before we stabilize it. We've tried to hide or make private most of the internal api for now because of not wanting to worry about making larger changes until we settle on a nice api.

mehcode avatar Sep 27 '21 17:09 mehcode

Just spent 30m trying to figure out what I was missing and finally landed here. With sufficient warning, I (and I think others) would prefer an unstable way to do this than no way or having to write extra fallible conversion boilerplate.

Edit: I thought I could work around this somehow with oids but nope, those methods are pub(crate) too. I don't see any way to do this legitimately and I don't know the implications of using with_name or with_oid which seem to use different enum variants.

andrewbaxter avatar Nov 12 '22 04:11 andrewbaxter

Found a workaround for producing the type info at least: for most types I think you can do String::type_info() or i64::type_info() and piggy back off the blanket implementations to get the internal values.

andrewbaxter avatar Nov 12 '22 04:11 andrewbaxter

Also voting for making this part of public API. I know that it's more about architecture decision than development effort, but will be able to help with dev part once architecture decision to make it public will be done.

humb1t avatar Aug 08 '23 13:08 humb1t

FWIW In my project I copied PgTypeInfo and PgType and converted from the upstream version to my own via the oid: https://github.com/geoarrow/geoarrow-rs/blob/ac9b72db0eb7696330440909f5ecf2700ef16139/src/io/postgis/type_info.rs

kylebarron avatar Mar 15 '24 15:03 kylebarron

I have a similar use case where I need to know what the underlying PgType is. Even though PgValueRef exposes PgTypeInfo I can't do any comparisons on PgType because it's private. I could compare based on the PgTypeInfo::display_name but this seems to be brittle and I want to avoid it if possible.

Up until now I was able to copy the PgTypeInfo code into my own repo and use the pg_catalog data to directly convert udt_name into a PgType but this doesn't work when detecting type names from the PgValueRef struct.

sumeetattree avatar Jun 21 '24 14:06 sumeetattree