From ed23377f2eee839fceca90ced740a2e35cf5d1aa Mon Sep 17 00:00:00 2001 From: Nikita Volkov Date: Sat, 21 Nov 2015 17:46:36 +0300 Subject: [PATCH] Simplify Settings --- benchmark/Main.hs | 2 +- library/Hasql.hs | 4 ++- library/Hasql/IO.hs | 7 ++--- library/Hasql/Settings.hs | 63 +++++++++++++++++---------------------- tasty/Main/DSL.hs | 2 +- 5 files changed, 36 insertions(+), 42 deletions(-) diff --git a/benchmark/Main.hs b/benchmark/Main.hs index cc94559..9bc196e 100644 --- a/benchmark/Main.hs +++ b/benchmark/Main.hs @@ -12,7 +12,7 @@ main = H.connect settings >>= either (fail . show) use where settings = - H.ParametricSettings host port user password database + H.settings host port user password database where host = "localhost" port = 5432 diff --git a/library/Hasql.hs b/library/Hasql.hs index 244c6c5..f9fd530 100644 --- a/library/Hasql.hs +++ b/library/Hasql.hs @@ -4,9 +4,11 @@ -- The API is completely disinfected from exceptions. All error-reporting is explicit and is presented using the 'Either' type. module Hasql ( + -- * Connection settings + Settings.Settings(..), + Settings.settings, -- * Connection Connection, - Settings.Settings(..), ConnectionError(..), connect, disconnect, diff --git a/library/Hasql/IO.hs b/library/Hasql/IO.hs index e4a2a85..f958da1 100644 --- a/library/Hasql/IO.hs +++ b/library/Hasql/IO.hs @@ -10,14 +10,13 @@ import qualified Hasql.PreparedStatementRegistry as PreparedStatementRegistry import qualified Hasql.Decoding.Result as ResultDecoding import qualified Hasql.Decoding.Results as ResultsDecoding import qualified Hasql.Encoding.Params as ParamsEncoding -import qualified Hasql.Settings as Settings import qualified Data.DList as DList {-# INLINE acquireConnection #-} -acquireConnection :: Settings.Settings -> IO LibPQ.Connection -acquireConnection settings = - LibPQ.connectdb (Settings.asBytes settings) +acquireConnection :: ByteString -> IO LibPQ.Connection +acquireConnection = + LibPQ.connectdb {-# INLINE acquirePreparedStatementRegistry #-} acquirePreparedStatementRegistry :: IO PreparedStatementRegistry.PreparedStatementRegistry diff --git a/library/Hasql/Settings.hs b/library/Hasql/Settings.hs index 29b53db..0d87a4b 100644 --- a/library/Hasql/Settings.hs +++ b/library/Hasql/Settings.hs @@ -7,40 +7,33 @@ import qualified Data.ByteString.Lazy.Builder.ASCII as BB import qualified Data.ByteString.Lazy as BL + +-- | +-- All settings encoded in a single byte-string according to +-- . +type Settings = + ByteString + -- | --- Connection settings. -data Settings = - -- | - -- A host, a port, a user, a password and a database. - ParametricSettings !ByteString !Word16 !ByteString !ByteString !ByteString | - -- | - -- All settings encoded in a single byte string according to - -- . - RawSettings !ByteString - deriving (Show) - -{-# INLINE asBytes #-} -asBytes :: Settings -> ByteString -asBytes = - \case - ParametricSettings 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) - ] - RawSettings bytes -> - bytes +-- 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) + ] diff --git a/tasty/Main/DSL.hs b/tasty/Main/DSL.hs index 3a120dd..1897d55 100644 --- a/tasty/Main/DSL.hs +++ b/tasty/Main/DSL.hs @@ -23,7 +23,7 @@ session (Session impl) = EitherT $ fmap (mapLeft ConnectionError) $ H.connect settings where settings = - H.ParametricSettings host port user password database + H.settings host port user password database where host = "localhost" port = 5432