generated from tpl/purs
Make ffiUnsafeQuery better typed, fix indentation
This commit is contained in:
parent
55d3ddd1bb
commit
de6404b82f
@ -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) {
|
||||||
|
@ -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)
|
||||||
@ -94,17 +93,17 @@ derive instance newtypeQuery :: Newtype (Query i o) _
|
|||||||
-- | Create a new connection pool.
|
-- | Create a new connection pool.
|
||||||
newPool :: PoolConfiguration -> Aff Pool
|
newPool :: PoolConfiguration -> Aff Pool
|
||||||
newPool cfg =
|
newPool cfg =
|
||||||
liftEffect <<< ffiNewPool $ cfg'
|
liftEffect <<< ffiNewPool $ cfg'
|
||||||
where
|
where
|
||||||
cfg' =
|
cfg' =
|
||||||
{ user: toNullable cfg.user
|
{ user: toNullable cfg.user
|
||||||
, password: toNullable cfg.password
|
, password: toNullable cfg.password
|
||||||
, host: toNullable cfg.host
|
, host: toNullable cfg.host
|
||||||
, port: toNullable cfg.port
|
, port: toNullable cfg.port
|
||||||
, database: cfg.database
|
, database: cfg.database
|
||||||
, max: toNullable cfg.max
|
, max: toNullable cfg.max
|
||||||
, idleTimeoutMillis: toNullable cfg.idleTimeoutMillis
|
, idleTimeoutMillis: toNullable cfg.idleTimeoutMillis
|
||||||
}
|
}
|
||||||
|
|
||||||
-- | Configuration which we actually pass to FFI.
|
-- | Configuration which we actually pass to FFI.
|
||||||
type PoolConfiguration' =
|
type PoolConfiguration' =
|
||||||
@ -131,11 +130,11 @@ withConnection
|
|||||||
withConnection p k =
|
withConnection p k =
|
||||||
except <=< lift $ bracket (connect p) cleanup run
|
except <=< lift $ bracket (connect p) cleanup run
|
||||||
where
|
where
|
||||||
cleanup (Left _) = pure unit
|
cleanup (Left _) = pure unit
|
||||||
cleanup (Right { done }) = liftEffect done
|
cleanup (Right { done }) = liftEffect done
|
||||||
|
|
||||||
run (Left err) = pure $ Left err
|
run (Left err) = pure $ Left err
|
||||||
run (Right { connection }) = runExceptT $ k connection
|
run (Right { connection }) = runExceptT $ k connection
|
||||||
|
|
||||||
connect
|
connect
|
||||||
:: Pool
|
:: Pool
|
||||||
@ -147,7 +146,6 @@ connect =
|
|||||||
, right: Right
|
, right: Right
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
type ConnectResult =
|
type ConnectResult =
|
||||||
{ connection :: Connection
|
{ connection :: Connection
|
||||||
, done :: Effect Unit
|
, done :: Effect Unit
|
||||||
@ -184,10 +182,9 @@ withTransaction conn action =
|
|||||||
pure value
|
pure value
|
||||||
|
|
||||||
where
|
where
|
||||||
begin = execute conn (Query "BEGIN TRANSACTION") Row0
|
begin = execute conn (Query "BEGIN TRANSACTION") Row0
|
||||||
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
|
||||||
@ -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,92 +236,77 @@ 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"
|
, right: Right
|
||||||
RowCount -> "rowCount"
|
}
|
||||||
, nullableLeft: toNullable <<< map Left <<< convertError
|
|
||||||
, 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
|
||||||
| ConversionError String
|
| ConversionError String
|
||||||
| InternalError PGErrorDetail
|
| InternalError PGErrorDetail
|
||||||
| OperationalError PGErrorDetail
|
| OperationalError PGErrorDetail
|
||||||
| ProgrammingError PGErrorDetail
|
| ProgrammingError PGErrorDetail
|
||||||
| IntegrityError PGErrorDetail
|
| IntegrityError PGErrorDetail
|
||||||
| DataError PGErrorDetail
|
| DataError PGErrorDetail
|
||||||
| NotSupportedError PGErrorDetail
|
| NotSupportedError PGErrorDetail
|
||||||
| 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 _
|
||||||
|
|
||||||
instance showPGError :: Show PGError where
|
instance showPGError :: Show PGError where
|
||||||
show = genericShow
|
show = genericShow
|
||||||
|
|
||||||
type PGErrorDetail =
|
type PGErrorDetail =
|
||||||
{ severity :: String
|
{ severity :: String
|
||||||
, code :: String
|
, code :: String
|
||||||
, message :: String
|
, message :: String
|
||||||
, detail :: String
|
, detail :: String
|
||||||
, hint :: String
|
, hint :: String
|
||||||
, position :: String
|
, position :: String
|
||||||
, internalPosition :: String
|
, internalPosition :: String
|
||||||
, internalQuery :: String
|
, internalQuery :: String
|
||||||
, where_ :: String
|
, where_ :: String
|
||||||
, schema :: String
|
, schema :: String
|
||||||
, table :: String
|
, table :: String
|
||||||
, column :: String
|
, column :: String
|
||||||
, dataType :: String
|
, dataType :: String
|
||||||
, constraint :: String
|
, constraint :: String
|
||||||
, file :: String
|
, file :: String
|
||||||
, line :: String
|
, line :: String
|
||||||
, routine :: String
|
, routine :: String
|
||||||
}
|
}
|
||||||
|
|
||||||
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,12 +340,11 @@ 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
|
||||||
where
|
where
|
||||||
handleError e =
|
handleError e =
|
||||||
case e of
|
case e of
|
||||||
IntegrityError _ -> errorResult
|
IntegrityError _ -> errorResult
|
||||||
_ -> throwError e
|
_ -> throwError e
|
||||||
|
Loading…
Reference in New Issue
Block a user