Skip to content

Commit 461b638

Browse files
committed
refactored utils
1 parent a5e988f commit 461b638

9 files changed

Lines changed: 186 additions & 160 deletions

File tree

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ script:
4747
- cabal build
4848
- cabal test --show-details=always
4949
- cabal sdist
50-
- cabal haddock | grep "100%" | wc -l | grep "5"
50+
- cabal haddock | grep "100%" | wc -l | grep "6"
5151

5252
branches:
5353
only:

default.nix

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{ mkDerivation, attoparsec, base, bytestring, case-conversion
2-
, heredoc, hspec, HUnit, postgresql-simple, stdenv
2+
, data-default, heredoc, hspec, postgresql-simple, stdenv
33
, template-haskell, text, time
44
}:
55
mkDerivation {
@@ -11,7 +11,7 @@ mkDerivation {
1111
template-haskell text time
1212
];
1313
testHaskellDepends = [
14-
attoparsec base bytestring case-conversion hspec HUnit
14+
attoparsec base bytestring case-conversion data-default hspec
1515
postgresql-simple text
1616
];
1717
description = "A FFI-like bindings for PostgreSQL stored functions";

examples/Common.hs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,21 @@ module Common (
55
, bindOptions
66
) where
77

8+
import Data.Default (def)
89
import Control.Exception.Base (bracket)
910
import Database.PostgreSQL.Simple (Connection, ConnectInfo, connect, begin, rollback, close, execute_)
10-
import Database.PostgreSQL.Simple.Bind (Options(..), defaultOptions)
11+
import Database.PostgreSQL.Simple.Bind (PostgresBindOptions(..), PGFunction(..))
1112
import Database.PostgreSQL.Simple.Types (Query(..))
1213
import Text.CaseConversion (convertCase, WordCase(..))
1314
import qualified Data.ByteString.Char8 as BS
1415

15-
bindOptions :: Options
16-
bindOptions = defaultOptions {
17-
nameModifier = convertCase Snake Camel . ("sql_" ++)
16+
mkFunctionName :: PGFunction -> String
17+
mkFunctionName (PGFunction _schema name _args _result)
18+
= convertCase Snake Camel . ("sql_" ++) $ name
19+
20+
bindOptions :: PostgresBindOptions
21+
bindOptions = (def :: PostgresBindOptions) {
22+
pboFunctionName = mkFunctionName
1823
}
1924

2025
withDB :: ConnectInfo -> (Connection -> IO a) -> IO a

postgresql-simple-bind.cabal

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ library
2525
build-depends: base >= 4.7 && < 5.0
2626
, attoparsec >= 0.13.0 && < 0.14
2727
, bytestring >= 0.10.8 && < 0.11
28+
, data-default
2829
, heredoc >= 0.2.0 && < 0.3
2930
, postgresql-simple >= 0.5.2 && < 0.6
3031
, template-haskell >= 2.11.0 && < 2.12
@@ -34,9 +35,10 @@ library
3435
hs-source-dirs: src
3536
exposed-modules:
3637
Database.PostgreSQL.Simple.Bind
37-
Database.PostgreSQL.Simple.Bind.Types
38-
Database.PostgreSQL.Simple.Bind.Util
38+
Database.PostgreSQL.Simple.Bind.Common
3939
Database.PostgreSQL.Simple.Bind.Representation
40+
Database.PostgreSQL.Simple.Bind.Types
41+
Database.PostgreSQL.Simple.Bind.Utils
4042

4143
other-modules:
4244
Database.PostgreSQL.Simple.Bind.Implementation
@@ -84,6 +86,7 @@ test-suite examples
8486
, attoparsec >= 0.13.0 && < 0.14
8587
, bytestring >= 0.10.8 && < 0.11
8688
, case-conversion
89+
, data-default
8790
, hspec
8891
, postgresql-simple >= 0.5.2 && < 0.6
8992
, postgresql-simple-bind

src/Database/PostgreSQL/Simple/Bind.hs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,18 @@
1717

1818
module Database.PostgreSQL.Simple.Bind (
1919
bindFunction
20-
, Options(..)
21-
, defaultOptions
20+
21+
, PostgresBindOptions(..)
2222
, PostgresType
23+
24+
, PGFunction(..)
25+
, PGArgument(..)
26+
, PGColumn(..)
27+
, PGResult(..)
2328
) where
2429

2530

2631
import Database.PostgreSQL.Simple.Bind.Implementation
27-
import Database.PostgreSQL.Simple.Bind.Util (Options (..), defaultOptions)
32+
import Database.PostgreSQL.Simple.Bind.Representation (PGFunction(..), PGArgument(..), PGColumn(..), PGResult(..))
33+
import Database.PostgreSQL.Simple.Bind.Common (PostgresBindOptions(..))
2834

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{-# LANGUAGE DataKinds #-}
2+
{-# LANGUAGE DeriveGeneric #-}
3+
{-# LANGUAGE EmptyDataDecls #-}
4+
{-# LANGUAGE FlexibleContexts #-}
5+
{-# LANGUAGE GADTs #-}
6+
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
7+
{-# LANGUAGE MultiParamTypeClasses #-}
8+
{-# LANGUAGE OverloadedStrings #-}
9+
{-# LANGUAGE QuasiQuotes #-}
10+
{-# LANGUAGE TemplateHaskell #-}
11+
{-# LANGUAGE TypeFamilies #-}
12+
{-# LANGUAGE TypeOperators #-}
13+
{-# LANGUAGE ScopedTypeVariables #-}
14+
{-# LANGUAGE FlexibleInstances #-}
15+
16+
{-|
17+
Module: Database.PostgreSQL.Simple.Bind.Common
18+
Copyright: (c) 2016 Al Zohali
19+
License: GPL3
20+
Maintainer: Al Zohali <zohl@fmap.me>
21+
Stability: experimental
22+
23+
Common functions and types.
24+
-}
25+
26+
27+
module Database.PostgreSQL.Simple.Bind.Common (
28+
unwrapRow
29+
, unwrapColumn
30+
, PostgresBindOptions(..)
31+
) where
32+
33+
34+
import Data.Default (Default, def)
35+
import Database.PostgreSQL.Simple (Only(..))
36+
import Database.PostgreSQL.Simple.Bind.Representation (PGFunction(..))
37+
38+
39+
-- | Options that specify how to construct the function binding.
40+
data PostgresBindOptions = PostgresBindOptions
41+
{ pboFunctionName :: PGFunction -> String
42+
-- ^ Function that generates name of a binding
43+
}
44+
45+
instance Default PostgresBindOptions where
46+
def = PostgresBindOptions
47+
{ pboFunctionName = \(PGFunction _schema name _args _result) -> name }
48+
49+
-- | Remove 'Only' constructor.
50+
unwrapColumn :: [Only a] -> [a]
51+
unwrapColumn = map (\(Only x) -> x)
52+
53+
-- | Remove list and 'Only' constructors.
54+
unwrapRow :: [Only a] -> a
55+
unwrapRow = head . unwrapColumn
56+

src/Database/PostgreSQL/Simple/Bind/Implementation.hs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
{-# LANGUAGE TypeOperators #-}
1313
{-# LANGUAGE ScopedTypeVariables #-}
1414
{-# LANGUAGE FlexibleInstances #-}
15+
{-# LANGUAGE RecordWildCards #-}
1516

1617
{-|
1718
Module: Database.PostgreSQL.Simple.Bind.Implementation
@@ -31,7 +32,7 @@ import Data.List (intersperse)
3132
import Data.Text (Text)
3233
import Database.PostgreSQL.Simple
3334
import Database.PostgreSQL.Simple.Bind.Representation
34-
import Database.PostgreSQL.Simple.Bind.Util (unwrapRow, unwrapColumn, mkFunctionName, Options(..))
35+
import Database.PostgreSQL.Simple.Bind.Common (unwrapRow, unwrapColumn, PostgresBindOptions(..))
3536
import Database.PostgreSQL.Simple.FromField (FromField)
3637
import Database.PostgreSQL.Simple.ToField
3738
import Database.PostgreSQL.Simple.Types
@@ -45,7 +46,7 @@ type family PostgresType (a :: Symbol)
4546

4647

4748
-- | Function that constructs binding for PostgreSQL stored function by it's signature.
48-
bindFunction :: Options -> Text -> Q [Dec]
49+
bindFunction :: PostgresBindOptions -> Text -> Q [Dec]
4950
bindFunction opt = (mkFunction opt) . parsePGFunction
5051

5152

@@ -79,7 +80,7 @@ filterArguments = filter isActual
7980

8081

8182

82-
mkFunction :: Options -> PGFunction -> Q [Dec]
83+
mkFunction :: PostgresBindOptions -> PGFunction -> Q [Dec]
8384
mkFunction opt f = sequence $ (($ f) . ($ opt)) <$> [mkFunctionT, mkFunctionE]
8485

8586
postgresT :: String -> Type
@@ -121,8 +122,8 @@ mkArgsT cs = do
121122
return (names, context, clause)
122123

123124

124-
mkFunctionT :: Options -> PGFunction -> Q Dec
125-
mkFunctionT opt f@(PGFunction _schema _name args ret) = do
125+
mkFunctionT :: PostgresBindOptions -> PGFunction -> Q Dec
126+
mkFunctionT (PostgresBindOptions {..}) f@(PGFunction _schema _name args ret) = do
126127
(argNames, argContext, argClause) <- mkArgsT args
127128
(retNames, retContext, retClause) <- mkResultT ret
128129

@@ -132,7 +133,7 @@ mkFunctionT opt f@(PGFunction _schema _name args ret) = do
132133
let chain x = AppT (AppT ArrowT x)
133134
let clause = foldr1 chain $ (ConT ''Connection):(argClause ++ [AppT (ConT ''IO) retClause])
134135

135-
return $ SigD (mkName (mkFunctionName opt f)) $ ForallT vars context clause
136+
return $ SigD (mkName $ pboFunctionName f) $ ForallT vars context clause
136137

137138

138139

@@ -162,14 +163,14 @@ unwrapE (PGSetOf _) q = (VarE 'fmap) `AppE` (VarE 'unwrapColumn) `AppE` q
162163
unwrapE (PGTable _) q = q
163164

164165

165-
mkFunctionE :: Options -> PGFunction -> Q Dec
166-
mkFunctionE opt f@(PGFunction _schema _name args ret) = do
166+
mkFunctionE :: PostgresBindOptions -> PGFunction -> Q Dec
167+
mkFunctionE (PostgresBindOptions {..}) f@(PGFunction _schema _name args ret) = do
167168
names <- sequence $ replicate (length args) (newName "x")
168169

169170
connName <- newName "conn"
170171
argsName <- newName "args"
171172

172-
let funcName = mkName $ mkFunctionName opt f
173+
let funcName = mkName $ pboFunctionName f
173174

174175
let funcArgs = (VarP connName):(map VarP names)
175176

src/Database/PostgreSQL/Simple/Bind/Util.hs

Lines changed: 0 additions & 139 deletions
This file was deleted.

0 commit comments

Comments
 (0)