postgresql-simple 0.4.4.0 → 0.4.4.1
raw patch · 4 files changed
+54/−3 lines, 4 files
Files
- postgresql-simple.cabal +2/−2
- src/Database/PostgreSQL/Simple/Copy.hs +10/−0
- src/Database/PostgreSQL/Simple/Internal.hs +11/−1
- test/Main.hs +31/−0
postgresql-simple.cabal view
@@ -1,5 +1,5 @@ Name: postgresql-simple-Version: 0.4.4.0+Version: 0.4.4.1 Synopsis: Mid-Level PostgreSQL client library Description: Mid-Level PostgreSQL client library, forked from mysql-simple.@@ -81,7 +81,7 @@ source-repository this type: git location: http://github.com/lpsmith/postgresql-simple- tag: v0.4.4.0+ tag: v0.4.4.1 test-suite test type: exitcode-stdio-1.0
src/Database/PostgreSQL/Simple/Copy.hs view
@@ -186,6 +186,7 @@ putCopyError :: Connection -> B.ByteString -> IO () putCopyError conn err = withConnection conn $ \pqconn -> do doCopyIn funcName (\c -> PQ.putCopyEnd c (Just err)) pqconn+ consumeResults pqconn where funcName = "Database.PostgreSQL.Simple.Copy.putCopyError" @@ -220,6 +221,7 @@ getCopyCommandTag funcName pqconn = do result <- maybe (fail errCmdStatus) return =<< PQ.getResult pqconn cmdStat <- maybe (fail errCmdStatus) return =<< PQ.cmdStatus result+ consumeResults pqconn let rowCount = P.string "COPY " *> (P.decimal <* P.endOfInput) case P.parseOnly rowCount cmdStat of Left _ -> fail errCmdStatusFmt@@ -227,3 +229,11 @@ where errCmdStatus = B.unpack funcName ++ ": failed to fetch command status" errCmdStatusFmt = B.unpack funcName ++ ": failed to parse command status"+++consumeResults :: PQ.Connection -> IO ()+consumeResults pqconn = do+ mres <- PQ.getResult pqconn+ case mres of+ Nothing -> return ()+ Just _ -> consumeResults pqconn
src/Database/PostgreSQL/Simple/Internal.hs view
@@ -311,7 +311,17 @@ Nothing -> case mres of Nothing -> throwLibPQError h "PQgetResult returned no results" Just res -> return res- Just _ -> getResult h mres'+ Just res -> do+ status <- PQ.resultStatus res+ case status of+ PQ.EmptyQuery -> getResult h mres'+ PQ.CommandOk -> getResult h mres'+ PQ.TuplesOk -> getResult h mres'+ PQ.CopyOut -> return res+ PQ.CopyIn -> return res+ PQ.BadResponse -> getResult h mres'+ PQ.NonfatalError -> getResult h mres'+ PQ.FatalError -> getResult h mres' #endif -- | A version of 'execute' that does not perform query substitution.
test/Main.hs view
@@ -5,6 +5,7 @@ import Database.PostgreSQL.Simple.FromField (FromField) import Database.PostgreSQL.Simple.Types(Query(..),Values(..)) import Database.PostgreSQL.Simple.HStore+import Database.PostgreSQL.Simple.Copy import qualified Database.PostgreSQL.Simple.Transaction as ST import Control.Applicative import Control.Exception as E@@ -41,6 +42,7 @@ , TestLabel "Savepoint" . testSavepoint , TestLabel "Unicode" . testUnicode , TestLabel "Values" . testValues+ , TestLabel "Copy" . testCopy ] testBytea :: TestEnv -> Test@@ -274,6 +276,35 @@ execute conn "INSERT INTO values_test ?" (Only table) vals' <- query_ conn "DELETE FROM values_test RETURNING *" sort vals @?= sort vals'+++testCopy :: TestEnv -> Test+testCopy TestEnv{..} = TestCase $ do+ execute_ conn "CREATE TEMPORARY TABLE copy_test (x int, y text)"+ copy_ conn "COPY copy_test FROM STDIN (FORMAT CSV)"+ mapM_ (putCopyData conn) copyRows+ putCopyEnd conn+ copy_ conn "COPY copy_test FROM STDIN (FORMAT CSV)"+ mapM_ (putCopyData conn) abortRows+ putCopyError conn "aborted"+ -- Hmm, does postgres always produce \n as an end-of-line here, or+ -- are there cases where it will use a \r\n as well?+ copy_ conn "COPY copy_test TO STDOUT (FORMAT CSV)"+ rows <- loop []+ sort rows @?= sort copyRows+ -- Now, let's just verify that the connection state is back to ready,+ -- so that we can issue more queries:+ [Only (x::Int)] <- query_ conn "SELECT 2 + 2"+ x @?= 4+ where+ copyRows = ["1,foo\n"+ ,"2,bar\n"]+ abortRows = ["3,baz\n"]+ loop rows = do+ mrow <- getCopyData conn+ case mrow of+ CopyOutDone _ -> return rows+ CopyOutRow row -> loop (row:rows) data TestException = TestException