Add scalar convenience function

This commit is contained in:
rightfold 2017-01-12 17:12:59 +01:00
parent 9609e8c94f
commit b9c7dbedea
2 changed files with 16 additions and 2 deletions

View File

@ -13,7 +13,8 @@
"purescript-maybe": "^2.0.1", "purescript-maybe": "^2.0.1",
"purescript-foldable-traversable": "^2.0.0", "purescript-foldable-traversable": "^2.0.0",
"purescript-newtype": "^1.1.0", "purescript-newtype": "^1.1.0",
"purescript-bytestrings": "^0.0.3" "purescript-bytestrings": "^0.0.3",
"purescript-arrays": "^3.1.0"
}, },
"repository": { "repository": {
"type": "git", "type": "git",

View File

@ -17,11 +17,13 @@ module Database.PostgreSQL
, withTransaction , withTransaction
, execute , execute
, query , query
, scalar
) where ) where
import Control.Monad.Aff (Aff) import Control.Monad.Aff (Aff)
import Control.Monad.Error.Class (catchError, throwError) import Control.Monad.Error.Class (catchError, throwError)
import Control.Monad.Except (runExcept) import Control.Monad.Except (runExcept)
import Data.Array (head)
import Data.ByteString (ByteString) import Data.ByteString (ByteString)
import Data.Either (Either(..)) import Data.Either (Either(..))
import Data.Foreign (Foreign, readArray, readChar, readInt, readString, toForeign, unsafeFromForeign) import Data.Foreign (Foreign, readArray, readChar, readInt, readString, toForeign, unsafeFromForeign)
@ -30,7 +32,7 @@ import Data.List as List
import Data.Maybe (fromJust, Maybe(..)) import Data.Maybe (fromJust, Maybe(..))
import Data.Newtype (class Newtype) import Data.Newtype (class Newtype)
import Data.Traversable (traverse) import Data.Traversable (traverse)
import Data.Tuple (Tuple) import Data.Tuple (fst, Tuple)
import Data.Tuple.Nested ((/\), tuple1, tuple2, tuple3, tuple4, tuple5) import Data.Tuple.Nested ((/\), tuple1, tuple2, tuple3, tuple4, tuple5)
import Partial.Unsafe (unsafePartial) import Partial.Unsafe (unsafePartial)
import Prelude import Prelude
@ -203,6 +205,17 @@ query conn (Query sql) values =
_query conn sql (toSQLRow values) _query conn sql (toSQLRow values)
<#> map (unsafePartial fromJust <<< fromSQLRow) <#> map (unsafePartial fromJust <<< fromSQLRow)
scalar
:: i o eff
. (ToSQLRow i, FromSQLValue o)
=> Connection
-> Query i (Tuple o Unit)
-> i
-> Aff (postgreSQL :: POSTGRESQL | eff) (Maybe o)
scalar conn sql values =
query conn sql values
<#> map fst <<< head
foreign import _query foreign import _query
:: eff :: eff
. Connection . Connection