git.iko.soy/typeable/postgresql-simple
on master help sign in
Improve Large Object Support
commit66ebc83e75e34f3f174251abbd200143efde7a6c authorLeon P Smith <leon@melding-monads.com> dateTue 3 Jan 2012, 17:31 (15 years ago) parent762005f
1 file changed+59−16
src/Database/PostgreSQL/Simple/LargeObjects.hs+59−16
src/Database/PostgreSQL/Simple/LargeObjects.hs+59−16
@@ -11,37 +11,80 @@
1111-----------------------------------------------------------------------------
1212
1313module Database.PostgreSQL.Simple.LargeObjects
14− ( loImport
14+ ( loCreat
15+ , loCreate
16+ , loImport
17+ , loImportWithOid
1518 , loExport
19+ , loOpen
20+ , loWrite
21+ , loRead
22+ , loSeek
23+ , loTell
24+ , loTruncate
25+ , loClose
26+ , loUnlink
1627 , Oid(..)
28+ , LoFd
29+ , IOMode(..)
30+ , SeekMode(..)
1731 ) where
1832
1933import Control.Applicative ((<$>))
2034import Control.Exception (throwIO)
21−import Database.PostgreSQL.LibPQ (Oid(..))
35+import qualified Data.ByteString as B
36+import Database.PostgreSQL.LibPQ (Oid(..),LoFd(..))
2237import qualified Database.PostgreSQL.LibPQ as PQ
2338import Database.PostgreSQL.Simple.Internal
24−import Foreign.C.Types(CInt)
39+import Foreign.C.Types (CInt)
40+import System.IO (IOMode(..),SeekMode(..))
2541
26−loImport :: Connection -> FilePath -> IO Oid
27−loImport conn path = withConnection conn $ \c -> do
28− res <- PQ.loImport c path
42+liftPQ :: B.ByteString -> Connection -> (PQ.Connection -> IO (Maybe a)) -> IO a
43+liftPQ str conn m = withConnection conn $ \c -> do
44+ res <- m c
2945 case res of
3046 Nothing -> do
31− msg <- maybe "loImport error" id <$> PQ.errorMessage c
47+ msg <- maybe str id <$> PQ.errorMessage c
3248 throwIO $ SqlError { sqlNativeError = -1 -- FIXME?
3349 , sqlErrorMsg = msg
3450 , sqlState = "" }
3551 Just x -> return x
3652
53+loCreat :: Connection -> IO Oid
54+loCreat conn = liftPQ "loCreat" conn (\c -> PQ.loCreat c)
55+
56+loCreate :: Connection -> Oid -> IO Oid
57+loCreate conn oid = liftPQ "loCreate" conn (\c -> PQ.loCreate c oid)
58+
59+loImport :: Connection -> FilePath -> IO Oid
60+loImport conn path = liftPQ "loImport" conn (\c -> PQ.loImport c path)
61+
62+loImportWithOid :: Connection -> FilePath -> Oid -> IO Oid
63+loImportWithOid conn path oid = liftPQ "loImportWithOid" conn (\c -> PQ.loImportWithOid c path oid)
3764
3865loExport :: Connection -> Oid -> FilePath -> IO ()
39−loExport conn oid path = withConnection conn $ \c -> do
40− res <- PQ.loExport c oid path
41− case res of
42− Nothing -> do
43− msg <- maybe "loExport error" id <$> PQ.errorMessage c
44− throwIO $ SqlError { sqlNativeError = -1 -- FIXME?
45− , sqlErrorMsg = msg
46− , sqlState = "" }
47− Just x -> return x
66+loExport conn oid path = liftPQ "loExport" conn (\c -> PQ.loExport c oid path)
67+
68+loOpen :: Connection -> Oid -> IOMode -> IO LoFd
69+loOpen conn oid mode = liftPQ "loOpen" conn (\c -> PQ.loOpen c oid mode )
70+
71+loWrite :: Connection -> LoFd -> B.ByteString -> IO Int
72+loWrite conn fd dat = liftPQ "loWrite" conn (\c -> PQ.loWrite c fd dat)
73+
74+loRead :: Connection -> LoFd -> Int -> IO B.ByteString
75+loRead conn fd maxlen = liftPQ "loRead" conn (\c -> PQ.loRead c fd maxlen)
76+
77+loSeek :: Connection -> LoFd -> SeekMode -> Int -> IO Int
78+loSeek conn fd seekmode offset = liftPQ "loSeek" conn (\c -> PQ.loSeek c fd seekmode offset)
79+
80+loTell :: Connection -> LoFd -> IO Int
81+loTell conn fd = liftPQ "loTell" conn (\c -> PQ.loTell c fd)
82+
83+loTruncate :: Connection -> LoFd -> Int -> IO ()
84+loTruncate conn fd len = liftPQ "loTruncate" conn (\c -> PQ.loTruncate c fd len)
85+
86+loClose :: Connection -> LoFd -> IO ()
87+loClose conn fd = liftPQ "loClose" conn (\c -> PQ.loClose c fd)
88+
89+loUnlink :: Connection -> Oid -> IO ()
90+loUnlink conn oid = liftPQ "loUnlink" conn (\c -> PQ.loUnlink c oid)