@@ -1,6 +1,7 @@
11{-# LANGUAGE CPP, DeriveDataTypeable, DeriveFunctor #-}
22{-# LANGUAGE FlexibleInstances, TypeSynonymInstances #-}
33{-# LANGUAGE PatternGuards, ScopedTypeVariables #-}
4+{-# LANGUAGE RecordWildCards #-}
45
56------------------------------------------------------------------------------
67-- |
@@ -126,6 +127,59 @@ class FromField a where
126127 -- such a reference, and that using bytestring functions such as 'B.drop'
127128 -- and 'B.takeWhile' alone will also trigger this memory leak.
128129
130+-- | Returns the data type name. This is the preferred way of identifying
131+-- types that do not have a stable type oid, such as types provided by
132+-- extensions to PostgreSQL.
133+--
134+-- More concretely, it returns the @typname@ column associated with the
135+-- type oid in the @pg_type@ table. First, postgresql-simple will check
136+-- built-in, static table. If the type oid is not there, postgresql-simple
137+-- will check a per-connection cache, and then finally query the database's
138+-- meta-schema.
139+
140+typename :: Field -> ByteString
141+typename = typname . typ . typeinfo
142+
143+-- | Returns the name of the column. This is often determined by a table
144+-- definition, but it can be set using an @as@ clause.
145+
146+name :: Field -> Maybe ByteString
147+name Field{..} = unsafePerformIO (PQ.fname result column)
148+
149+-- | Returns the name of the object id of the @table@ associated with the
150+-- column, if any. Returns 'Nothing' when there is no such table;
151+-- for example a computed column does not have a table associated with it.
152+-- Analogous to libpq's @PQftable@.
153+
154+tableOid :: Field -> Maybe PQ.Oid
155+tableOid Field{..} = toMaybeOid (unsafePerformIO (PQ.ftable result column))
156+ where
157+ toMaybeOid x
158+ = if x == PQ.invalidOid
159+ then Nothing
160+ else Just x
161+
162+-- | If the column has a table associated with it, this returns the number
163+-- of the associated table column. Numbering starts from 0. Analogous
164+-- to libpq's @PQftablecol@.
165+
166+tableColumn :: Field -> Int
167+tableColumn Field{..} = fromCol (unsafePerformIO (PQ.ftablecol result column))
168+ where
169+ fromCol (PQ.Col x) = fromIntegral x
170+
171+-- | This returns whether the data was returned in a binary or textual format.
172+-- Analogous to libpq's @PQfformat@.
173+
174+format :: Field -> PQ.Format
175+format Field{..} = unsafePerformIO (PQ.fformat result column)
176+
177+-- | This returns the type oid associated with the column. Analogous
178+-- to libpq's @PQftype@.
179+
180+typeOid :: Field -> PQ.Oid
181+typeOid Field{..} = unsafePerformIO (PQ.ftype result column)
182+
129183instance (FromField a) => FromField (Maybe a) where
130184 fromField _ Nothing = pure Nothing
131185 fromField f bs = Just <$> fromField f bs