@@ -224,6 +224,7 @@ finishExecute :: Connection -> Query -> PQ.Result -> IO Int64
224finishExecute _conn q result = do
224finishExecute _conn q result = do
225 status <- PQ.resultStatus result
225 status <- PQ.resultStatus result
226 case status of
226 case status of
227+ PQ.EmptyQuery -> throwIO $ QueryError "execute: Empty query" q
227 PQ.CommandOk -> do
228 PQ.CommandOk -> do
228 ncols <- PQ.nfields result
229 ncols <- PQ.nfields result
229 if ncols /= 0
230 if ncols /= 0
@@ -238,16 +239,13 @@ finishExecute _conn q result = do
238 ncols <- PQ.nfields result
239 ncols <- PQ.nfields result
239 throwIO $ QueryError ("execute resulted in " ++ show ncols ++
240 throwIO $ QueryError ("execute resulted in " ++ show ncols ++
240 "-column result") q
241 "-column result") q
241− PQ.CopyIn -> fail "FIXME: postgresql-simple does not currently support COPY IN"
242+ PQ.CopyOut ->
242− PQ.CopyOut -> fail "FIXME: postgresql-simple does not currently support COPY OUT"
243+ throwIO $ QueryError "execute: COPY TO is not supported" q
243− _ -> do
244+ PQ.CopyIn ->
244− errormsg <- maybe "" id <$> PQ.resultErrorMessage result
245+ throwIO $ QueryError "execute: COPY FROM is not supported" q
245− statusmsg <- PQ.resStatus status
246+ PQ.BadResponse -> throwResultError "execute" result status
246− state <- maybe "" id <$> PQ.resultErrorField result PQ.DiagSqlstate
247+ PQ.NonfatalError -> throwResultError "execute" result status
247− throwIO $ SqlError { sqlState = state
248+ PQ.FatalError -> throwResultError "execute" result status
248− , sqlNativeError = fromEnum status
249− , sqlErrorMsg = B.concat [ "execute: ", statusmsg
252 toInteger str = B8.foldl' delta 0 str
250 toInteger str = B8.foldl' delta 0 str
@@ -256,6 +254,15 @@ finishExecute _conn q result = do
256 then 10 * acc + fromIntegral (ord c - ord '0')
254 then 10 * acc + fromIntegral (ord c - ord '0')
257 else error ("finishExecute: not an int: " ++ B8.unpack str)
255 else error ("finishExecute: not an int: " ++ B8.unpack str)
257+throwResultError :: ByteString -> PQ.Result -> PQ.ExecStatus -> IO a
258+throwResultError context result status = do
259+ errormsg <- maybe "" id <$> PQ.resultErrorMessage result
260+ statusmsg <- PQ.resStatus status
261+ state <- maybe "" id <$> PQ.resultErrorField result PQ.DiagSqlstate
262+ throwIO $ SqlError { sqlState = state
263+ , sqlNativeError = fromEnum status
264+ , sqlErrorMsg = B.concat [ context, ": ", statusmsg
260disconnectedError :: SqlError
267disconnectedError :: SqlError
261disconnectedError = SqlError {
268disconnectedError = SqlError {