@@ -18,12 +18,13 @@ module Database.Postgres
1818
1919import Prelude
2020import Control.Monad.Eff (Eff )
21- import Data.Either (either )
21+ import Data.Either (Either , either )
2222import Data.Function.Uncurried (Fn2 (), runFn2 )
2323import Data.Array ((!!))
24- import Data.Foreign (Foreign , ForeignError )
24+ import Data.Foreign (Foreign , MultipleErrors )
2525import Data.Foreign.Class (class IsForeign , read )
2626import Data.Maybe (Maybe (Just, Nothing), maybe )
27+ import Control.Monad.Except (runExcept )
2728import Control.Monad.Aff (Aff , finally )
2829import Control.Monad.Eff.Class (liftEff )
2930import Control.Monad.Eff.Exception (error )
@@ -75,41 +76,41 @@ query :: forall eff a
7576 => Query a -> Array SqlValue -> Client -> Aff (db :: DB | eff ) (Array a )
7677query (Query sql) params client = do
7778 rows <- runQuery sql params client
78- either liftError pure (sequence $ read <$> rows)
79+ either liftError pure (runExcept ( sequence $ read <$> rows) )
7980
8081-- | Just like `query` but does not make any param replacement
8182query_ :: forall eff a . (IsForeign a ) => Query a -> Client -> Aff (db :: DB | eff ) (Array a )
8283query_ (Query sql) client = do
8384 rows <- runQuery_ sql client
84- either liftError pure (sequence $ read <$> rows)
85+ either liftError pure (runExcept ( sequence $ read <$> rows) )
8586
8687-- | Runs a query and returns the first row, if any
8788queryOne :: forall eff a
8889 . (IsForeign a )
8990 => Query a -> Array SqlValue -> Client -> Aff (db :: DB | eff ) (Maybe a )
9091queryOne (Query sql) params client = do
9192 rows <- runQuery sql params client
92- maybe (pure Nothing ) (either liftError (pure <<< Just )) $ read <$> (rows !! 0 )
93+ maybe (pure Nothing ) (either liftError (pure <<< Just )) (readFirst rows )
9394
9495-- | Just like `queryOne` but does not make any param replacement
9596queryOne_ :: forall eff a . (IsForeign a ) => Query a -> Client -> Aff (db :: DB | eff ) (Maybe a )
9697queryOne_ (Query sql) client = do
9798 rows <- runQuery_ sql client
98- maybe (pure Nothing ) (either liftError (pure <<< Just )) $ read <$> (rows !! 0 )
99+ maybe (pure Nothing ) (either liftError (pure <<< Just )) (readFirst rows )
99100
100101-- | Runs a query and returns a single value, if any.
101102queryValue :: forall eff a
102103 . (IsForeign a )
103104 => Query a -> Array SqlValue -> Client -> Aff (db :: DB | eff ) (Maybe a )
104105queryValue (Query sql) params client = do
105106 val <- runQueryValue sql params client
106- pure $ either (const Nothing ) Just (read val)
107+ pure $ either (const Nothing ) Just (runExcept ( read val) )
107108
108109-- | Just like `queryValue` but does not make any param replacement
109110queryValue_ :: forall eff a . (IsForeign a ) => Query a -> Client -> Aff (db :: DB | eff ) (Maybe a )
110111queryValue_ (Query sql) client = do
111112 val <- runQueryValue_ sql client
112- either liftError (pure <<< Just ) $ read val
113+ either liftError (pure <<< Just ) $ runExcept ( read val)
113114
114115-- | Connects to the database, calls the provided function with the client
115116-- | and returns the results.
@@ -129,8 +130,11 @@ withClient :: forall eff a
129130 -> Aff (db :: DB | eff ) a
130131withClient info p = runFn2 _withClient (mkConnectionString info) p
131132
132- liftError :: forall e a . ForeignError -> Aff e a
133- liftError err = throwError $ error (show err)
133+ readFirst :: forall a . IsForeign a => Array Foreign -> Maybe (Either MultipleErrors a )
134+ readFirst rows = runExcept <<< read <$> (rows !! 0 )
135+
136+ liftError :: forall e a . MultipleErrors -> Aff e a
137+ liftError errs = throwError $ error (show errs)
134138
135139foreign import connect' :: forall eff . String -> Aff (db :: DB | eff ) Client
136140
0 commit comments