@@ -18,12 +18,13 @@ module Database.Postgres
18
18
19
19
import Prelude
20
20
import Control.Monad.Eff (Eff )
21
- import Data.Either (either )
21
+ import Data.Either (Either , either )
22
22
import Data.Function.Uncurried (Fn2 (), runFn2 )
23
23
import Data.Array ((!!))
24
- import Data.Foreign (Foreign , ForeignError )
24
+ import Data.Foreign (Foreign , MultipleErrors )
25
25
import Data.Foreign.Class (class IsForeign , read )
26
26
import Data.Maybe (Maybe (Just, Nothing), maybe )
27
+ import Control.Monad.Except (runExcept )
27
28
import Control.Monad.Aff (Aff , finally )
28
29
import Control.Monad.Eff.Class (liftEff )
29
30
import Control.Monad.Eff.Exception (error )
@@ -75,41 +76,41 @@ query :: forall eff a
75
76
=> Query a -> Array SqlValue -> Client -> Aff (db :: DB | eff ) (Array a )
76
77
query (Query sql) params client = do
77
78
rows <- runQuery sql params client
78
- either liftError pure (sequence $ read <$> rows)
79
+ either liftError pure (runExcept ( sequence $ read <$> rows) )
79
80
80
81
-- | Just like `query` but does not make any param replacement
81
82
query_ :: forall eff a . (IsForeign a ) => Query a -> Client -> Aff (db :: DB | eff ) (Array a )
82
83
query_ (Query sql) client = do
83
84
rows <- runQuery_ sql client
84
- either liftError pure (sequence $ read <$> rows)
85
+ either liftError pure (runExcept ( sequence $ read <$> rows) )
85
86
86
87
-- | Runs a query and returns the first row, if any
87
88
queryOne :: forall eff a
88
89
. (IsForeign a )
89
90
=> Query a -> Array SqlValue -> Client -> Aff (db :: DB | eff ) (Maybe a )
90
91
queryOne (Query sql) params client = do
91
92
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 )
93
94
94
95
-- | Just like `queryOne` but does not make any param replacement
95
96
queryOne_ :: forall eff a . (IsForeign a ) => Query a -> Client -> Aff (db :: DB | eff ) (Maybe a )
96
97
queryOne_ (Query sql) client = do
97
98
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 )
99
100
100
101
-- | Runs a query and returns a single value, if any.
101
102
queryValue :: forall eff a
102
103
. (IsForeign a )
103
104
=> Query a -> Array SqlValue -> Client -> Aff (db :: DB | eff ) (Maybe a )
104
105
queryValue (Query sql) params client = do
105
106
val <- runQueryValue sql params client
106
- pure $ either (const Nothing ) Just (read val)
107
+ pure $ either (const Nothing ) Just (runExcept ( read val) )
107
108
108
109
-- | Just like `queryValue` but does not make any param replacement
109
110
queryValue_ :: forall eff a . (IsForeign a ) => Query a -> Client -> Aff (db :: DB | eff ) (Maybe a )
110
111
queryValue_ (Query sql) client = do
111
112
val <- runQueryValue_ sql client
112
- either liftError (pure <<< Just ) $ read val
113
+ either liftError (pure <<< Just ) $ runExcept ( read val)
113
114
114
115
-- | Connects to the database, calls the provided function with the client
115
116
-- | and returns the results.
@@ -129,8 +130,11 @@ withClient :: forall eff a
129
130
-> Aff (db :: DB | eff ) a
130
131
withClient info p = runFn2 _withClient (mkConnectionString info) p
131
132
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)
134
138
135
139
foreign import connect' :: forall eff . String -> Aff (db :: DB | eff ) Client
136
140
0 commit comments