module Database.PostgreSQL.PQTypes.Utils (
throwDB
, raw
, runQuery_
, runQuery01
, runQuery01_
, runSQL
, runSQL_
, runSQL01
, runSQL01_
, hpqTypesError
) where
import Control.Monad
import Control.Monad.Catch
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
{-# INLINABLE throwDB #-}
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
{-# INLINABLE runQuery_ #-}
runQuery_ :: (IsSQL sql, MonadDB m) => sql -> m ()
runQuery_ = void . runQuery
{-# INLINABLE runQuery01 #-}
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
{-# INLINABLE runQuery01_ #-}
runQuery01_ :: (IsSQL sql, MonadDB m, MonadThrow m) => sql -> m ()
runQuery01_ = void . runQuery01
{-# INLINABLE runSQL #-}
runSQL :: MonadDB m => SQL -> m Int
runSQL = runQuery
{-# INLINABLE runSQL_ #-}
runSQL_ :: MonadDB m => SQL -> m ()
runSQL_ = runQuery_
{-# INLINABLE runSQL01 #-}
runSQL01 :: (MonadDB m, MonadThrow m) => SQL -> m Bool
runSQL01 = runQuery01
{-# INLINABLE runSQL01_ #-}
runSQL01_ :: (MonadDB m, MonadThrow m) => SQL -> m ()
runSQL01_ = runQuery01_