hasql/library/Hasql/Private/Settings.hs
Viktor Dukhovni 0a1eef918c Deal with removed deprecated ByteString modules
The ByteString 0.11 package included with GHC 9.2 droppped a few
long deprecated modules and functions.
2021-11-01 16:12:33 -04:00

39 lines
1.2 KiB
Haskell

module Hasql.Private.Settings where
import Hasql.Private.Prelude
import qualified Data.ByteString as B
import qualified Data.ByteString.Builder as BB
import qualified Data.ByteString.Lazy as BL
-- |
-- All settings encoded in a single byte-string according to
-- <http://www.postgresql.org/docs/9.4/static/libpq-connect.html#LIBPQ-CONNSTRING the PostgreSQL format>.
type Settings =
ByteString
-- |
-- Encode a host, a port, a user, a password and a database into the PostgreSQL settings byte-string.
{-# INLINE settings #-}
settings :: ByteString -> Word16 -> ByteString -> ByteString -> ByteString -> Settings
settings host port user password database =
BL.toStrict $ BB.toLazyByteString $ mconcat $ intersperse (BB.char7 ' ') $ catMaybes $
[
mappend (BB.string7 "host=") . BB.byteString <$>
mfilter (not . B.null) (pure host)
,
mappend (BB.string7 "port=") . BB.word16Dec <$>
mfilter (/= 0) (pure port)
,
mappend (BB.string7 "user=") . BB.byteString <$>
mfilter (not . B.null) (pure user)
,
mappend (BB.string7 "password=") . BB.byteString <$>
mfilter (not . B.null) (pure password)
,
mappend (BB.string7 "dbname=") . BB.byteString <$>
mfilter (not . B.null) (pure database)
]