git.iko.soy/typeable/postgresql-simple
on master help sign in
Fix command tag handling for COPY IN
ced6664Leon P Smith
postgresql-simple.cabal+2−2
@@ -1,5 +1,5 @@
1Name: postgresql-simple
1Name: postgresql-simple
2−Version: 0.3.3.2
2+Version: 0.3.4.0
3Synopsis: Mid-Level PostgreSQL client library
3Synopsis: Mid-Level PostgreSQL client library
4Description:
4Description:
5 Mid-Level PostgreSQL client library, forked from mysql-simple.
5 Mid-Level PostgreSQL client library, forked from mysql-simple.
@@ -76,7 +76,7 @@ source-repository head
76source-repository this
76source-repository this
77 type: git
77 type: git
78 location: http://github.com/lpsmith/postgresql-simple
78 location: http://github.com/lpsmith/postgresql-simple
79− tag: v0.3.3.2
79+ tag: v0.3.4.0
80
80
81test-suite test
81test-suite test
82 type: exitcode-stdio-1.0
82 type: exitcode-stdio-1.0
src/Database/PostgreSQL/Simple/Copy.hs+32−24
@@ -69,8 +69,6 @@ getCopyData :: Connection -> IO CopyOutResult
69getCopyData conn = withConnection conn loop
69getCopyData conn = withConnection conn loop
70 where
70 where
71 funcName = "Database.PostgreSQL.Simple.Copy.getCopyData"
71 funcName = "Database.PostgreSQL.Simple.Copy.getCopyData"
72− errCmdStatus = B.unpack funcName ++ ": failed to fetch command status"
73− errCmdStatusFmt = B.unpack funcName ++ ": failed to parse command status"
74 loop pqconn = do
72 loop pqconn = do
75#if defined(mingw32_HOST_OS)
73#if defined(mingw32_HOST_OS)
76 row <- PQ.getCopyData pqconn False
74 row <- PQ.getCopyData pqconn False
@@ -80,12 +78,7 @@ getCopyData conn = withConnection conn loop
80 case row of
78 case row of
81 PQ.CopyOutRow rowdata -> return $! CopyOutRow rowdata
79 PQ.CopyOutRow rowdata -> return $! CopyOutRow rowdata
82 PQ.CopyOutDone -> do
80 PQ.CopyOutDone -> do
83− result <- maybe (fail errCmdStatus) return =<< PQ.getResult pqconn
81+ CopyOutDone <$> getCopyCommandTag funcName pqconn
84− cmdStat <- maybe (fail errCmdStatus) return =<< PQ.cmdStatus result
85− let rowCount = P.string "COPY " *> P.decimal
86− case P.parseOnly (rowCount <* P.endOfInput) cmdStat of
87− Left _ -> fail errCmdStatusFmt
88− Right n -> return $! CopyOutDone n
89#if defined(mingw32_HOST_OS)
82#if defined(mingw32_HOST_OS)
90 PQ.CopyOutWouldBlock -> do
83 PQ.CopyOutWouldBlock -> do
91 fail (B.unpack funcName ++ ": the impossible happened")
84 fail (B.unpack funcName ++ ": the impossible happened")
@@ -110,26 +103,29 @@ getCopyData conn = withConnection conn loop
110 }
103 }
111
104
112putCopyData :: Connection -> B.ByteString -> IO ()
105putCopyData :: Connection -> B.ByteString -> IO ()
113−putCopyData conn dat =
106+putCopyData conn dat = withConnection conn $ \pqconn -> do
107+ doCopyIn funcName (\c -> PQ.putCopyData c dat) pqconn
108+ where
114− doCopyIn "Database.PostgreSQL.Simple.Copy.putCopyData"
109+ funcName = "Database.PostgreSQL.Simple.Copy.putCopyData"
115− (\c -> PQ.putCopyData c dat)
110+
116− conn
111+putCopyEnd :: Connection -> IO Int64
112+putCopyEnd conn = withConnection conn $ \pqconn -> do
113+ doCopyIn funcName (\c -> PQ.putCopyEnd c Nothing) pqconn
114+ getCopyCommandTag funcName pqconn
115+ where
116+ funcName = "Database.PostgreSQL.Simple.Copy.putCopyEnd"
117
117
118−putCopyEnd :: Connection -> IO ()
118+putCopyError :: Connection -> B.ByteString -> IO Int64
119−putCopyEnd conn = do
119+putCopyError conn err = withConnection conn $ \pqconn -> do
120+ doCopyIn funcName (\c -> PQ.putCopyEnd c (Just err)) pqconn
121+ getCopyCommandTag funcName pqconn
122+ where
120− doCopyIn "Database.PostgreSQL.Simple.Copy.putCopyEnd"
123+ funcName = "Database.PostgreSQL.Simple.Copy.putCopyError"
121− (\c -> PQ.putCopyEnd c Nothing)
122− conn
123
124
124−putCopyError :: Connection -> B.ByteString -> IO ()
125−putCopyError conn err = do
126− doCopyIn "Database.PostgreSQL.Simple.Copy.putCopyError"
127− (\c -> PQ.putCopyEnd c (Just err))
128− conn
129
125
130doCopyIn :: B.ByteString -> (PQ.Connection -> IO PQ.CopyInResult)
126doCopyIn :: B.ByteString -> (PQ.Connection -> IO PQ.CopyInResult)
131− -> Connection -> IO ()
127+ -> PQ.Connection -> IO ()
132−doCopyIn funcName action conn = withConnection conn loop
128+doCopyIn funcName action = loop
133 where
129 where
134 loop pqconn = do
130 loop pqconn = do
135 stat <- action pqconn
131 stat <- action pqconn
@@ -152,3 +148,15 @@ doCopyIn funcName action conn = withConnection conn loop
152 threadWaitWrite fd
148 threadWaitWrite fd
153 loop pqconn
149 loop pqconn
154{-# INLINE doCopyIn #-}
150{-# INLINE doCopyIn #-}
151+
152+getCopyCommandTag :: B.ByteString -> PQ.Connection -> IO Int64
153+getCopyCommandTag funcName pqconn = do
154+ result <- maybe (fail errCmdStatus) return =<< PQ.getResult pqconn
155+ cmdStat <- maybe (fail errCmdStatus) return =<< PQ.cmdStatus result
156+ let rowCount = P.string "COPY " *> (P.decimal <* P.endOfInput)
157+ case P.parseOnly rowCount cmdStat of
158+ Left _ -> fail errCmdStatusFmt
159+ Right n -> return $! n
160+ where
161+ errCmdStatus = B.unpack funcName ++ ": failed to fetch command status"
162+ errCmdStatusFmt = B.unpack funcName ++ ": failed to parse command status"