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