@@ -32,7 +32,9 @@ import Database.PostgreSQL.Simple.Types (Only(..))
3232import qualified Database.PostgreSQL.LibPQ as PQ
3333import Database.PostgreSQL.Simple.Internal
3434import Database.PostgreSQL.Simple.FromField
35+import Database.PostgreSQL.Simple.Ok
3536import Database.PostgreSQL.Simple.Types ((:.)(..))
37+import Database.PostgreSQL.Simple.TypeInfo
3638import System.IO.Unsafe (unsafePerformIO)
3739
3840import Control.Monad.Trans.State.Strict
@@ -70,19 +72,26 @@ getvalue result row col = unsafePerformIO (PQ.getvalue result row col)
7072nfields :: PQ.Result -> PQ.Column
7173nfields result = unsafePerformIO (PQ.nfields result)
7274
75+getTypeInfoByCol :: Row -> PQ.Column -> Conversion TypeInfo
76+getTypeInfoByCol Row{..} col =
77+ Conversion $ \conn -> do
78+ oid <- PQ.ftype rowresult col
79+ Ok <$> getTypeInfo conn oid
80+
81+getTypenameByCol :: Row -> PQ.Column -> Conversion ByteString
82+getTypenameByCol row col = typname . typ <$> getTypeInfoByCol row col
83+
7384fieldWith :: FieldParser a -> RowParser a
7485fieldWith fieldP = RP $ do
7586 let unCol (PQ.Col x) = fromIntegral x :: Int
76− Row{..} <- ask
87+ r@Row{..} <- ask
7788 column <- lift get
7889 lift (put (column + 1))
7990 let ncols = nfields rowresult
8091 if (column >= ncols)
81− then do
82− let vals = map (\c -> ( typname (typ (typeinfos ! unCol c))
83− , fmap ellipsis (getvalue rowresult row c) ))
84− [0..ncols-1]
85− convertError = ConversionFailed
92+ then lift $ lift $ do
93+ vals <- mapM (getTypenameByCol r) [0..ncols-1]
94+ let err = ConversionFailed
8695 (show (unCol ncols) ++ " values: " ++ show vals)
8796 Nothing
8897 ""
@@ -90,10 +99,9 @@ fieldWith fieldP = RP $ do
9099 ++ " slots in target type")
91100 "mismatch between number of columns to \
92101 \convert and number in target type"
93− lift (lift (conversionError convertError))
102+ conversionError err
94103 else do
95− let typeinfo = typeinfos ! unCol column
96− result = rowresult
104+ let result = rowresult
97105 field = Field{..}
98106 lift (lift (fieldP field (getvalue result row column)))
99107