Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Better support for type conversions #10

Closed
lpsmith opened this Issue · 7 comments

2 participants

Leon P Smith R. Kyle Murphy
Leon P Smith
Owner

See, for example, #4; we should add support for network addresses and universally unique identifiers among others.

Also, Haskell's UTCTime doesn't support infinity or -infinity, whereas PostgreSQL's timestamp type does.

R. Kyle Murphy

I'm interested in adding UUID to the list of supported types, pulling in the UUID type from the uuid package on hackage. I'm going to try to fork and add it, but I'm still a bit of a newb with Haskell, so not sure how well that's going to go. Before I spend a bunch of time trying to work out the details of that do you have any objection to pulling in a dependency on uuid?

R. Kyle Murphy

The work to add support for UUID is mostly done, I just need to add some tests around the new code and then I'll be ready to issue a pull request.

Leon P Smith
Owner

Sorry for not responding a little sooner. This is actually a surprisingly common question.

The answer is that you don't need to modify postgresql-simple at all, all you have to do is add a FromField instance for the Haskell type you want to represent UUIDs. I've been meaning to add this exact case as sample code in FromField, actually. Also, you might be interested in looking at the improved FromField documentation in the 0.3 branch.

Here's something that should be pretty close to being correct:

instance FromField UUID where
    fromField f mdata =
        if typeOid f /= builtin2oid UUID
        then returnError Incompatible "" 
        else case B.unpack `fmap` mdata of 
               Nothing   -> returnError UnexpectedNull ""
               Just data -> 
                   case [ x | (x,t) <- reads data, ("","") <- lex t ] of
                     [x] -> Ok x
                     _   -> returnError ConversionError data

Since postgresql's UUID type has a stable oid, we can look at that. For types provided by extensions, such as hstore, you should use the typename operator instead, e.g. if typename f /= "hstore" then ....

Leon P Smith
Owner

Also, I will say that I have considered adding out-of-box support for UUIDs, but after consulting some of the postgresql-simple users that use UUIDs we decided to keep postgresql-simple more minimal, with fewer dependencies on this count for the time being.

R. Kyle Murphy

Wouldn't you also need to create a ToField instance for the UUID type? The work I've done so far was to create a FromField and ToField instance and it seems to work, although I suppose I can see not wanting to pull in the extra dependency just to support UUID. I suppose I could always make the instances a standalone module, something like postgresql-simple-uuid.

Leon P Smith
Owner

Depends on your needs, but for whatever reason ToField instances for UUID aren't asked about as often. We should offer both for out-of-box UUID support, though.

Leon P Smith
Owner

Well, there's more work to be done, on this issue, but there's always more work to be done on this issue. I haven't added out-of-box support for network addresses (yet?), and decided against out-of-box support for UUIDs for the time being although I may change my mind, and have included a UUID FromField instance as an explicit example in the documentation. However recent versions do support arrays and hstores, and I have rewritten the time parsers.

So given that this is a fairly generic and on-going issue, I'm going to close it. If anybody wants to request support for another specific type, feel free to open another issue.

Leon P Smith lpsmith closed this
Adam Bergmark bergmark referenced this issue in yesodweb/persistent
Closed

persistent-postgresql: Support postgres UUID type #153

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.