@@ -32,7 +32,9 @@ import Database.PostgreSQL.Simple.Types (Only(..))
32import qualified Database.PostgreSQL.LibPQ as PQ
32import qualified Database.PostgreSQL.LibPQ as PQ
33import Database.PostgreSQL.Simple.Internal
33import Database.PostgreSQL.Simple.Internal
34import Database.PostgreSQL.Simple.FromField
34import Database.PostgreSQL.Simple.FromField
35+import Database.PostgreSQL.Simple.Ok
35import Database.PostgreSQL.Simple.Types ((:.)(..))
36import Database.PostgreSQL.Simple.Types ((:.)(..))
37+import Database.PostgreSQL.Simple.TypeInfo
36import System.IO.Unsafe (unsafePerformIO)
38import System.IO.Unsafe (unsafePerformIO)
38import Control.Monad.Trans.State.Strict
40import Control.Monad.Trans.State.Strict
@@ -70,19 +72,26 @@ getvalue result row col = unsafePerformIO (PQ.getvalue result row col)
70nfields :: PQ.Result -> PQ.Column
72nfields :: PQ.Result -> PQ.Column
71nfields result = unsafePerformIO (PQ.nfields result)
73nfields result = unsafePerformIO (PQ.nfields result)
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
81+getTypenameByCol :: Row -> PQ.Column -> Conversion ByteString
82+getTypenameByCol row col = typname . typ <$> getTypeInfoByCol row col
73fieldWith :: FieldParser a -> RowParser a
84fieldWith :: FieldParser a -> RowParser a
74fieldWith fieldP = RP $ do
85fieldWith fieldP = RP $ do
75 let unCol (PQ.Col x) = fromIntegral x :: Int
86 let unCol (PQ.Col x) = fromIntegral x :: Int
76− Row{..} <- ask
87+ r@Row{..} <- ask
77 column <- lift get
88 column <- lift get
78 lift (put (column + 1))
89 lift (put (column + 1))
79 let ncols = nfields rowresult
90 let ncols = nfields rowresult
80 if (column >= ncols)
91 if (column >= ncols)
81− then do
92+ then lift $ lift $ do
82− let vals = map (\c -> ( typname (typ (typeinfos ! unCol c))
93+ vals <- mapM (getTypenameByCol r) [0..ncols-1]
83− , fmap ellipsis (getvalue rowresult row c) ))
85− convertError = ConversionFailed
94+ let err = ConversionFailed
86 (show (unCol ncols) ++ " values: " ++ show vals)
95 (show (unCol ncols) ++ " values: " ++ show vals)
@@ -90,10 +99,9 @@ fieldWith fieldP = RP $ do
90 ++ " slots in target type")
99 ++ " slots in target type")
91 "mismatch between number of columns to \
100 "mismatch between number of columns to \
92 \convert and number in target type"
101 \convert and number in target type"
93− lift (lift (conversionError convertError))
102+ conversionError err
95− let typeinfo = typeinfos ! unCol column
96− result = rowresult
104+ let result = rowresult
97 field = Field{..}
105 field = Field{..}
98 lift (lift (fieldP field (getvalue result row column)))
106 lift (lift (fieldP field (getvalue result row column)))