@@ -1,6 +1,7 @@
1{-# LANGUAGE CPP, DeriveDataTypeable, DeriveFunctor #-}
1{-# LANGUAGE CPP, DeriveDataTypeable, DeriveFunctor #-}
2{-# LANGUAGE FlexibleInstances, TypeSynonymInstances #-}
2{-# LANGUAGE FlexibleInstances, TypeSynonymInstances #-}
3{-# LANGUAGE PatternGuards, ScopedTypeVariables #-}
3{-# LANGUAGE PatternGuards, ScopedTypeVariables #-}
4+{-# LANGUAGE RecordWildCards #-}
5------------------------------------------------------------------------------
6------------------------------------------------------------------------------
@@ -126,6 +127,59 @@ class FromField a where
126 -- such a reference, and that using bytestring functions such as 'B.drop'
127 -- such a reference, and that using bytestring functions such as 'B.drop'
127 -- and 'B.takeWhile' alone will also trigger this memory leak.
128 -- and 'B.takeWhile' alone will also trigger this memory leak.
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.
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
140+typename :: Field -> ByteString
141+typename = typname . typ . typeinfo
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.
146+name :: Field -> Maybe ByteString
147+name Field{..} = unsafePerformIO (PQ.fname result column)
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@.
154+tableOid :: Field -> Maybe PQ.Oid
155+tableOid Field{..} = toMaybeOid (unsafePerformIO (PQ.ftable result column))
158+ = if x == PQ.invalidOid
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@.
166+tableColumn :: Field -> Int
167+tableColumn Field{..} = fromCol (unsafePerformIO (PQ.ftablecol result column))
169+ fromCol (PQ.Col x) = fromIntegral x
171+-- | This returns whether the data was returned in a binary or textual format.
172+-- Analogous to libpq's @PQfformat@.
174+format :: Field -> PQ.Format
175+format Field{..} = unsafePerformIO (PQ.fformat result column)
177+-- | This returns the type oid associated with the column. Analogous
178+-- to libpq's @PQftype@.
180+typeOid :: Field -> PQ.Oid
181+typeOid Field{..} = unsafePerformIO (PQ.ftype result column)
129instance (FromField a) => FromField (Maybe a) where
183instance (FromField a) => FromField (Maybe a) where
130 fromField _ Nothing = pure Nothing
184 fromField _ Nothing = pure Nothing
131 fromField f bs = Just <$> fromField f bs
185 fromField f bs = Just <$> fromField f bs