packages feed

postgresql-config 0.0.2 → 0.1.0

raw patch · 9 files changed

+121/−22 lines, 9 files

Files

CHANGELOG.md view
@@ -1,5 +1,10 @@ # CHANGELOG +## 0.1.0+### Added+* `createPGPoolWithCallback` accepting callback which must be executed+  on each connection just after it opened by pool.+ ## 0.0.2  * `base` constraints relaxed
README.md view
@@ -9,13 +9,13 @@  ```yml database:    "dbname"-host:        "127.0.0.1"        # optional-port:        "5432"             # optional+host:        "127.0.0.1"       # optional+port:         5432             # optional user:        "dbuser" password:    "pass"-poolsize:    "10"               # optional maximum connections in pool-pooltimeout: "60"               # optional minimum connection lifetime-poolstripes: "1"                # optional count of stripes in pool+poolsize:    10               # optional maximum connections in pool+pooltimeout: 60               # optional minimum connection lifetime+poolstripes: 1                # optional count of stripes in pool ```  and then in your program something like that
+ examples/LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2015, Aleksey Uimanov++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++    * Redistributions of source code must retain the above copyright+      notice, this list of conditions and the following disclaimer.++    * Redistributions in binary form must reproduce the above+      copyright notice, this list of conditions and the following+      disclaimer in the documentation and/or other materials provided+      with the distribution.++    * Neither the name of Aleksey Uimanov nor the names of other+      contributors may be used to endorse or promote products derived+      from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
examples/Main.hs view
@@ -1,14 +1,28 @@ module Main where +import Control.Monad ( void )+import Data.Monoid+import Data.String import Data.Yaml import Database.PostgreSQL.Config+import Database.PostgreSQL.Simple+import System.Environment  + main :: IO () main = do     conf <- decodeFile "pgconfig.yml"             >>= maybe (fail "Oups!") return-    pool <- createPGPool conf+    pool <- getArgs >>= \case+      [] -> createPGPool conf+      [spath] ->+        let callback con =+              void+              $ execute_ con+              $ "SET search_path TO " <> fromString spath+        in createPGPoolWithCallback conf callback+      _ -> fail "wrong arguments"     putStrLn "Pool created"     pingPGPool pool     putStrLn "Connection performed"
+ examples/Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ examples/pgconfig-examples.cabal view
@@ -0,0 +1,25 @@+name:                pgconfig-examples+version:             0.1.0.0+synopsis:            examles+license:             BSD3+license-file:        LICENSE+author:              Aleksey Uimanov+maintainer:          s9gf4ult@gmail.com+-- copyright:+category:            Examples+build-type:          Simple+-- extra-source-files:+cabal-version:       >=1.10++executable pgconfig-examples+  main-is:             Main.hs++  default-extensions:  LambdaCase+                     , OverloadedStrings++  build-depends:       base >=4.8 && < 5+                     , yaml+                     , postgresql-simple+                     , postgresql-config+  -- hs-source-dirs:+  default-language:    Haskell2010
examples/pgconfig.yml view
@@ -1,8 +1,8 @@ database:    "dbname"-host:        "127.0.0.1"        # optional-port:        "5432"             # optional+host:        "127.0.0.1"       # optional+port:         5432             # optional user:        "dbuser" password:    "pass"-poolsize:    "10"               # optional maximum connections in pool-pooltimeout: "60"               # optional minimum connection lifetime-poolstripes: "1"                # optional count of stripes in pool+poolsize:    10               # optional maximum connections in pool+pooltimeout: 60               # optional minimum connection lifetime+poolstripes: 1                # optional count of stripes in pool
postgresql-config.cabal view
@@ -1,5 +1,5 @@ name:                postgresql-config-version:             0.0.2+version:             0.1.0  synopsis:            Types for easy adding postgresql configuration to your program @@ -14,7 +14,9 @@  extra-source-files: CHANGELOG.md                   , README.md-                  , examples/Main.hs+                  , examples/*.hs+                  , examples/LICENSE+                  , examples/pgconfig-examples.cabal                   , examples/pgconfig.yml  homepage: https://bitbucket.org/s9gf4ult/postgresql-config@@ -30,7 +32,6 @@                     , DeriveGeneric                     , FlexibleContexts                     , OverloadedStrings-                    , RecordWildCards    build-depends:     base >=4.6  &&  <= 5.0                    , aeson
src/Database/PostgreSQL/Config.hs view
@@ -4,6 +4,8 @@        , PGPool(..)          -- * Pool creation        , createPGPool+       , PGCallback+       , createPGPoolWithCallback        , pingPGPool          -- * Helpers for __postgresql-query__        , withPGPool@@ -25,8 +27,7 @@  -- | Connection pool. Must be created from settings using -- 'createPGPool'-newtype PGPool =-    PGPool+newtype PGPool = PGPool     { unPGPool :: (Pool PG.Connection)     } deriving ( Show, Typeable, Generic ) @@ -84,15 +85,36 @@  -- | Create pool from parsed configuration createPGPool :: PostgresConf -> IO PGPool-createPGPool PostgresConf{..} =+createPGPool = flip createPGPoolWithCallback mock+  where mock = const $ return ()++type PGCallback = PG.Connection -> IO ()++-- | Create pool from parsed configuration,+-- which enables to fire a callback after creating each connection.+createPGPoolWithCallback+  :: PostgresConf -- ^ connection data+  -> PGCallback   -- ^ callback action, can be used for performing+                  -- arbitrary action on connection+  -> IO PGPool+createPGPoolWithCallback pgc callback =     fmap PGPool     $ createPool-    (PG.connectPostgreSQL pgConnStr)-    PG.close-    pgPoolStripes-    pgPoolTimeout-    pgPoolSize+        (connectAndExecQuery pgc callback)+        PG.close+        (pgPoolStripes pgc)+        (pgPoolTimeout pgc)+        (pgPoolSize pgc) +-- | Create pool from parsed configuration and execute callback+-- for each connection.+connectAndExecQuery :: PostgresConf+                    -> PGCallback+                    -> IO PG.Connection+connectAndExecQuery pgc callback = do+    conn <- PG.connectPostgreSQL $ pgConnStr pgc+    _ <- callback conn+    return conn  {- | Combinator for simple implementation of 'withPGConnection' method from package __postgresql-query__.  Typical usage is: