hasql/library/Hasql/Settings.hs

40 lines
1.3 KiB
Haskell
Raw Normal View History

module Hasql.Settings where
2015-11-08 21:09:42 +03:00
2024-04-19 07:38:30 +03:00
import Data.ByteString qualified as B
import Data.ByteString.Builder qualified as BB
import Data.ByteString.Lazy qualified as BL
import Hasql.Prelude
2015-11-08 21:09:42 +03:00
2022-06-20 13:54:54 +03:00
-- |
-- All settings encoded in a single byte-string according to
2015-11-21 17:46:36 +03:00
-- <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 =
2023-10-13 02:24:12 +03:00
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)
]