@@ -6,7 +6,7 @@
7Module: Database.PostgreSQL.Simple.FromField
7Module: Database.PostgreSQL.Simple.FromField
8Copyright: (c) 2011 MailRank, Inc.
8Copyright: (c) 2011 MailRank, Inc.
9− (c) 2011-2012 Leon P Smith
9+ (c) 2011-2013 Leon P Smith
10License: BSD3
10License: BSD3
11Maintainer: Leon P Smith <leon@melding-monads.com>
11Maintainer: Leon P Smith <leon@melding-monads.com>
12Stability: experimental
12Stability: experimental
@@ -31,13 +31,14 @@ might look like for a UUID type that implements the @Read@ class:
33import Data.UUID ( UUID )
33import Data.UUID ( UUID )
34−import Database.PostgreSQL.Simple.BuiltinTypes
34+import Database.PostgreSQL.Simple.FromField
35− ( BuiltinType(UUID), builtin2oid )
35+ ( typeOid, returnError, ResultError(..) )
36+import Database.PostgreSQL.Simple.TypeInfo.Static (typoid, uuid)
36import qualified Data.ByteString as B
37import qualified Data.ByteString as B
38instance FromField UUID where
39instance FromField UUID where
39 fromField f mdata =
40 fromField f mdata =
40− if typeOid f /= builtin2oid UUID
41+ if typeOid f /= typoid uuid
41 then returnError Incompatible f \"\"
42 then returnError Incompatible f \"\"
42 else case B.unpack \`fmap\` mdata of
43 else case B.unpack \`fmap\` mdata of
43 Nothing -> returnError UnexpectedNull f \"\"
44 Nothing -> returnError UnexpectedNull f \"\"
@@ -49,8 +50,9 @@ instance FromField UUID where
50Note that because PostgreSQL's @uuid@ type is built into postgres and is
51Note that because PostgreSQL's @uuid@ type is built into postgres and is
51not provided by an extension, the 'typeOid' of @uuid@ does not change and
52not provided by an extension, the 'typeOid' of @uuid@ does not change and
52−thus we can examine it directly. Here, we simply pull the type oid out
53+thus we can examine it directly. One could hard-code the type oid, or
54+obtain it by other means, but in this case we simply pull it out of the
53−of the static table provided by postgresql-simple.
55+static table provided by postgresql-simple.
55On the other hand if the type is provided by an extension, such as
57On the other hand if the type is provided by an extension, such as
56@PostGIS@ or @hstore@, then the 'typeOid' is not stable and can vary from
58@PostGIS@ or @hstore@, then the 'typeOid' is not stable and can vary from
@@ -175,9 +177,9 @@ class FromField a where
176-- More concretely, it returns the @typname@ column associated with the
178-- More concretely, it returns the @typname@ column associated with the
177-- type oid in the @pg_type@ table. First, postgresql-simple will check
179-- type oid in the @pg_type@ table. First, postgresql-simple will check
178−-- built-in, static table. If the type oid is not there, postgresql-simple
180+-- the built-in, static table. If the type oid is not there,
179−-- will check a per-connection cache, and then finally query the database's
181+-- postgresql-simple will check a per-connection cache, and then
180−-- meta-schema.
182+-- finally query the database's meta-schema.
182typename :: Field -> Conversion ByteString
184typename :: Field -> Conversion ByteString
183typename field = typname <$> typeInfo field
185typename field = typname <$> typeInfo field