Permalink
Browse files

Switch from unsafePerformIO to unsafeDupablePerformIO

  • Loading branch information...
lpsmith committed Feb 25, 2013
1 parent 3a67b8d commit 4194297be27e7ccca610e1459c3959a88d74c30d
@@ -4,11 +4,21 @@
module Database.PostgreSQL.Simple.Compat
( mask
, (<>)
+ , unsafeDupablePerformIO
) where
import qualified Control.Exception as E
import Data.Monoid
+#if __GLASGOW_HASKELL__ >= 702
+import System.IO.Unsafe (unsafeDupablePerformIO)
+#elif __GLASGOW_HASKELL__ >= 611
+import GHC.IO (unsafeDupablePerformIO)
+#else
+import GHC.IOBase (unsafeDupablePerformIO)
+#endif
+
+
-- | Like 'E.mask', but backported to base before version 4.3.0.
--
-- Note that the restore callback is monomorphic, unlike in 'E.mask'. This
@@ -103,13 +103,13 @@ import qualified Data.Vector as V
import Data.Word (Word64)
import Database.PostgreSQL.Simple.Internal
import Database.PostgreSQL.Simple.BuiltinTypes
+import Database.PostgreSQL.Simple.Compat
import Database.PostgreSQL.Simple.Ok
import Database.PostgreSQL.Simple.Types (Binary(..), Null(..))
import Database.PostgreSQL.Simple.TypeInfo
import Database.PostgreSQL.Simple.Time
import Database.PostgreSQL.Simple.Arrays
import qualified Database.PostgreSQL.LibPQ as PQ
-import System.IO.Unsafe (unsafePerformIO)
import qualified Data.ByteString as SB
import qualified Data.ByteString.Char8 as B8
import qualified Data.ByteString.Lazy as LB
@@ -189,15 +189,15 @@ typeinfo Field{..} = Conversion $ \conn -> do
-- definition, but it can be set using an @as@ clause.
name :: Field -> Maybe ByteString
-name Field{..} = unsafePerformIO (PQ.fname result column)
+name Field{..} = unsafeDupablePerformIO (PQ.fname result column)
-- | Returns the name of the object id of the @table@ associated with the
-- column, if any. Returns 'Nothing' when there is no such table;
-- for example a computed column does not have a table associated with it.
-- Analogous to libpq's @PQftable@.
tableOid :: Field -> Maybe PQ.Oid
-tableOid Field{..} = toMaybeOid (unsafePerformIO (PQ.ftable result column))
+tableOid Field{..} = toMaybeOid (unsafeDupablePerformIO (PQ.ftable result column))
where
toMaybeOid x
= if x == PQ.invalidOid
@@ -209,15 +209,15 @@ tableOid Field{..} = toMaybeOid (unsafePerformIO (PQ.ftable result column))
-- to libpq's @PQftablecol@.
tableColumn :: Field -> Int
-tableColumn Field{..} = fromCol (unsafePerformIO (PQ.ftablecol result column))
+tableColumn Field{..} = fromCol (unsafeDupablePerformIO (PQ.ftablecol result column))
where
fromCol (PQ.Col x) = fromIntegral x
-- | This returns whether the data was returned in a binary or textual format.
-- Analogous to libpq's @PQfformat@.
format :: Field -> PQ.Format
-format Field{..} = unsafePerformIO (PQ.fformat result column)
+format Field{..} = unsafeDupablePerformIO (PQ.fformat result column)
instance (FromField a) => FromField (Maybe a) where
fromField _ Nothing = pure Nothing
@@ -278,7 +278,7 @@ instance FromField LB.ByteString where
unescapeBytea :: Field -> SB.ByteString
-> Conversion (Binary SB.ByteString)
-unescapeBytea f str = case unsafePerformIO (PQ.unescapeBytea str) of
+unescapeBytea f str = case unsafeDupablePerformIO (PQ.unescapeBytea str) of
Nothing -> returnError ConversionFailed f "unescapeBytea failed"
Just str -> pure (Binary str)
@@ -31,11 +31,11 @@ import qualified Data.ByteString.Char8 as B
import Database.PostgreSQL.Simple.Types (Only(..))
import qualified Database.PostgreSQL.LibPQ as PQ
import Database.PostgreSQL.Simple.Internal
+import Database.PostgreSQL.Simple.Compat
import Database.PostgreSQL.Simple.FromField
import Database.PostgreSQL.Simple.Ok
import Database.PostgreSQL.Simple.Types ((:.)(..))
import Database.PostgreSQL.Simple.TypeInfo
-import System.IO.Unsafe (unsafePerformIO)
import Control.Monad.Trans.State.Strict
import Control.Monad.Trans.Reader
@@ -67,10 +67,10 @@ class FromRow a where
fromRow :: RowParser a
getvalue :: PQ.Result -> PQ.Row -> PQ.Column -> Maybe ByteString
-getvalue result row col = unsafePerformIO (PQ.getvalue result row col)
+getvalue result row col = unsafeDupablePerformIO (PQ.getvalue result row col)
nfields :: PQ.Result -> PQ.Column
-nfields result = unsafePerformIO (PQ.nfields result)
+nfields result = unsafeDupablePerformIO (PQ.nfields result)
getTypeInfoByCol :: Row -> PQ.Column -> Conversion TypeInfo
getTypeInfoByCol Row{..} col =
@@ -102,7 +102,7 @@ fieldWith fieldP = RP $ do
conversionError err
else do
let !result = rowresult
- !typeOid = unsafePerformIO (PQ.ftype result column)
+ !typeOid = unsafeDupablePerformIO (PQ.ftype result column)
!field = Field{..}
lift (lift (fieldP field (getvalue result row column)))

0 comments on commit 4194297

Please sign in to comment.