Make ffiUnsafeQuery better typed, fix indentation

This commit is contained in:
Petri Lehtinen 2018-11-30 08:35:29 +02:00
parent 55d3ddd1bb
commit de6404b82f
2 changed files with 70 additions and 93 deletions

View File

@ -45,11 +45,7 @@ exports.ffiUnsafeQuery = function(config) {
values: values,
rowMode: 'array',
}).then(function(result) {
if (config.queryMode === "rows") {
onSuccess(config.right(result.rows));
} else if (config.queryMode === "rowCount") {
onSuccess(config.right([[result.rowCount]]));
}
onSuccess(config.right(result))
}).catch(function(err) {
var pgError = config.nullableLeft(err);
if (pgError) {

View File

@ -26,7 +26,6 @@ import Control.Monad.Error.Class (catchError, throwError, try)
import Control.Monad.Except.Trans (ExceptT, except, runExceptT)
import Control.Monad.Trans.Class (lift)
import Data.Array (head)
import Data.Bifunctor (lmap)
import Data.Either (Either(..))
import Data.Generic.Rep (class Generic)
import Data.Generic.Rep.Show (genericShow)
@ -147,7 +146,6 @@ connect =
, right: Right
}
type ConnectResult =
{ connection :: Connection
, done :: Effect Unit
@ -188,7 +186,6 @@ withTransaction conn action =
commit = execute conn (Query "COMMIT TRANSACTION") Row0
rollback = execute conn (Query "ROLLBACK TRANSACTION") Row0
-- | Execute a PostgreSQL query and discard its results.
execute
:: i o
@ -198,7 +195,7 @@ execute
-> i
-> PG Unit
execute conn (Query sql) values =
void $ unsafeQuery Rows conn sql (toSQLRow values)
void $ unsafeQuery conn sql (toSQLRow values)
-- | Execute a PostgreSQL query and return its results.
query
@ -210,7 +207,7 @@ query
-> i
-> PG (Array o)
query conn (Query sql) values = do
unsafeQuery Rows conn sql (toSQLRow values)
_.rows <$> unsafeQuery conn sql (toSQLRow values)
>>= traverse (fromSQLRow >>> case _ of
Right row -> pure row
Left msg -> throwError $ ConversionError msg)
@ -239,48 +236,35 @@ command
-> Query i Int
-> i
-> PG Int
command conn (Query sql) values = do
result <- unsafeQuery RowCount conn sql (toSQLRow values)
case result of
[[x]] -> except $ lmap ConversionError (Value.fromSQLValue x)
_ -> throwError (ConversionError "unexpected data")
data QueryMode
= Rows
| RowCount
type QueryResult
= Array (Array Foreign)
command conn (Query sql) values =
_.rowCount <$> unsafeQuery conn sql (toSQLRow values)
type QueryResult =
{ rows :: Array (Array Foreign)
, rowCount :: Int
}
unsafeQuery
:: QueryMode
-> Connection
:: Connection
-> String
-> Array Foreign
-> PG QueryResult
unsafeQuery m c s =
unsafeQuery c s =
except <=< lift <<< fromEffectFnAff <<< ffiUnsafeQuery p c s
where
p =
{ queryMode: case m of
Rows -> "rows"
RowCount -> "rowCount"
, nullableLeft: toNullable <<< map Left <<< convertError
{ nullableLeft: toNullable <<< map Left <<< convertError
, right: Right
}
foreign import ffiUnsafeQuery
:: { queryMode :: String
, nullableLeft :: Error -> Nullable (Either PGError QueryResult)
:: { nullableLeft :: Error -> Nullable (Either PGError QueryResult)
, right :: QueryResult -> Either PGError QueryResult
}
-> Connection
-> String
-> Array Foreign
-> EffectFnAff (Either PGError (Array (Array Foreign)))
-> EffectFnAff (Either PGError QueryResult)
data PGError
= ConnectionError String
@ -294,7 +278,6 @@ data PGError
| QueryCanceledError PGErrorDetail
| TransactionRollbackError PGErrorDetail
derive instance eqPGError :: Eq PGError
derive instance genericPGError :: Generic PGError _
@ -324,7 +307,6 @@ type PGErrorDetail =
foreign import ffiSQLState :: Error -> Nullable String
foreign import ffiErrorDetail :: Error -> PGErrorDetail
convertError :: Error -> Maybe PGError
convertError err =
case toMaybe $ ffiSQLState err of
@ -358,7 +340,6 @@ convertError err =
prefix p =
maybe false (_ == 0) <<< String.indexOf (Pattern p)
onIntegrityError :: forall a. PG a -> PG a -> PG a
onIntegrityError errorResult db =
catchError db handleError