module Database.PostgreSQL.PQTypes.Utils (
throwDB
, raw
, runQuery_
, runQuery01
, runQuery01_
, runSQL
, runSQL_
, runSQL01
, runSQL01_
, hpqTypesError
) where
import Control.Monad
import Control.Monad.Catch
import Prelude
import Database.PostgreSQL.PQTypes.Class
import Database.PostgreSQL.PQTypes.Internal.Error
import Database.PostgreSQL.PQTypes.Internal.Exception
import Database.PostgreSQL.PQTypes.Internal.Utils
import Database.PostgreSQL.PQTypes.SQL
import Database.PostgreSQL.PQTypes.SQL.Class
import Database.PostgreSQL.PQTypes.SQL.Raw
throwDB :: (Exception e, MonadDB m, MonadThrow m) => e -> m a
throwDB e = case fromException $ toException e of
Just (dbe::DBException) -> throwM dbe
Nothing -> do
SomeSQL sql <- getLastQuery
throwM DBException {
dbeQueryContext = sql
, dbeError = e
}
raw :: RawSQL () -> SQL
raw = mkSQL . unRawSQL
runQuery_ :: (IsSQL sql, MonadDB m) => sql -> m ()
runQuery_ = void . runQuery
runQuery01 :: (IsSQL sql, MonadDB m, MonadThrow m) => sql -> m Bool
runQuery01 sql = do
n <- runQuery sql
when (n > 1) $ throwDB AffectedRowsMismatch {
rowsExpected = [(0, 1)]
, rowsDelivered = n
}
return $ n == 1
runQuery01_ :: (IsSQL sql, MonadDB m, MonadThrow m) => sql -> m ()
runQuery01_ = void . runQuery01
runSQL :: MonadDB m => SQL -> m Int
runSQL = runQuery
runSQL_ :: MonadDB m => SQL -> m ()
runSQL_ = runQuery_
runSQL01 :: (MonadDB m, MonadThrow m) => SQL -> m Bool
runSQL01 = runQuery01
runSQL01_ :: (MonadDB m, MonadThrow m) => SQL -> m ()
runSQL01_ = runQuery01_