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

ToField Action typing #265

Open
defanor opened this issue Jul 23, 2018 · 2 comments
Open

ToField Action typing #265

defanor opened this issue Jul 23, 2018 · 2 comments

Comments

@defanor
Copy link

defanor commented Jul 23, 2018

Sometimes PostgreSQL gets confused about types if there's no explicit typing; I've noticed it with a non-empty array (PGArray) of enum elements, which gets recognized as text[] instead of user_role[] in my case. Setting types in queries explicitly is a bit cumbersome in some cases, and it would be handy if postgresql-simple facilitated it. For instance, the following works, but it uses Plain for something other than its intended purpose as it's described in the documentation (that is, it's a hack):

instance ToField Role where
  toField r = Many [Escape $ BS8.pack $ show r, Plain ":: user_role"]

Maybe something like a Typed Action String action constructor should be introduced, or the Plain description should be adjusted. It may be out of scope of postgresql-simple, but seems like ToField is the appropriate place for adding type information.

@lpsmith
Copy link
Owner

lpsmith commented Aug 13, 2018

Yes, I absolutely do want to implement something like this. However, my circumstances have changed, and I haven't been working on postgresql-simple for some time now; though I do plan on returning to this project someday. I really should bear down and get a simpler new release finished in the meantime, though.

Personally, I don't think this instance is that terrible of a hack. And, something along these lines may be necessary for implementing protocol-level parameters, as every such parameter is tagged with a type oid. I haven't investigated all the issues surrounding this, but I would imagine that picking the right type oid is far more critical for binary protocol-level parameters than it is for textual protocol-level parameters.

@lpsmith
Copy link
Owner

lpsmith commented Aug 13, 2018

Oh, I would point out you can already create something like this more generically (and more robustly):

data TypeAnnotation a = TypeAnnotation a QualifiedIdentifier

instance ToField a => ToField (TypeAnnotation a) where
  toField (TypeAnnotation x t) = Many [toField x, Plain " :: ", toField t]

I'm not sure if I've ever actually used this definition, though I know it's occurred to me several times. That doesn't really address the issue of how to come up with the QualifiedIdentifier automatically, though, which is sometimes nice or perhaps even necessary.

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

No branches or pull requests

2 participants