Skip to content

Commit 1917cfd

Browse files
committed
wip: fixing pg_enum
1 parent a9695cc commit 1917cfd

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed

src/Database/PostgreSQL/Query/TH/Entity.hs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,28 @@ import Data.String
1010
import Database.PostgreSQL.Query.Entity ( Entity(..) )
1111
import Database.PostgreSQL.Query.TH.Common
1212
import Database.PostgreSQL.Query.Types ( FN(..) )
13+
import Database.PostgreSQL.Simple.FromField
14+
import Database.PostgreSQL.Simple.ToField
15+
import GHC.Generics (Generic)
1316
import Language.Haskell.TH
1417
import Language.Haskell.TH.Syntax
18+
import Text.Inflections
1519

1620
-- | Options for deriving `Entity`
1721
data EntityOptions = EntityOptions
1822
{ eoTableName :: String -> String -- ^ Type name to table name converter
1923
, eoColumnNames :: String -> String -- ^ Record field to column name converter
2024
, eoDeriveClasses :: [Name] -- ^ Typeclasses to derive for Id
2125
, eoIdType :: Name -- ^ Base type for Id
22-
}
26+
} deriving (Generic)
2327

2428
instance Default EntityOptions where
25-
def = EntityOptions
26-
{ eoTableName = id
27-
, eoColumnNames = id
28-
-- FIXME: ++ FromField, ToField
29-
, eoDeriveClasses = [''Ord, ''Eq, ''Show]
30-
, eoIdType = ''Integer
29+
def = EntityOptions
30+
{ eoTableName = toUnderscore
31+
, eoColumnNames = toUnderscore
32+
, eoDeriveClasses = [ ''Ord, ''Eq, ''Show
33+
, ''FromField, ''ToField ]
34+
, eoIdType = ''Integer
3135
}
3236

3337
{- | Derives instance for 'Entity' using type name and field names. Also

src/Database/PostgreSQL/Query/TH/Enum.hs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,33 @@ module Database.PostgreSQL.Query.TH.Enum
77
, GeneratorOptions(..)
88
) where
99

10-
import Prelude
10+
import Prelude
1111

12-
import Data.Default
13-
import Database.PostgreSQL.Simple.FromField
14-
import Database.PostgreSQL.Simple.ToField
15-
import Language.Haskell.TH
16-
import Text.Inflections
12+
import Data.Default
13+
import Data.FileEmbed
14+
import Database.PostgreSQL.Simple.FromField
15+
import Database.PostgreSQL.Simple.ToField
16+
import GHC.Generics (Generic)
17+
import Language.Haskell.TH
18+
import Text.Inflections
1719

18-
import qualified Data.ByteString.Char8 as C8
20+
import qualified Data.Text.Encoding as T
21+
import qualified Data.Text as T
1922

2023
-- | Function to transform constructor name into its PG enum conterpart.
2124
-- It should have type 'String -> String'
22-
type InflectorFunc = Name
25+
type InflectorFunc = String -> String
2326

2427
type Typ = Name
2528

2629
-- | Option record to pass to the TH generators.
2730
data GeneratorOptions
2831
= GeneratorOptions
29-
{ inflectorFunc :: InflectorFunc }
32+
{ inflectorFunc :: InflectorFunc
33+
} deriving (Generic)
3034

3135
instance Default GeneratorOptions where
32-
def = GeneratorOptions 'toUnderscore
36+
def = GeneratorOptions toUnderscore
3337

3438
{-| derives 'FromField' and 'ToField' instances for a sum-type enum like
3539
@@ -122,15 +126,15 @@ withEnumConstructor :: InflectorFunc
122126
-> (Name -> ExpQ -> Q a)
123127
-- ^ callback function from:
124128
-- 1. haskell constructor name and
125-
-- 2. PG enum option
129+
-- 2. PG enum option (ByteString)
126130
-> Con
127131
-- ^ constructor to decompose
128132
-> Q a
129133
withEnumConstructor i f = \case
130134
(NormalC _ (_:_)) ->
131135
error "constructors with arguments are not supported in makeToFieldClause"
132136
(NormalC nam [] ) -> f nam inflectedBs
133-
where inflected = appE (varE i) (litE (stringL (nameBase nam)))
134-
inflectedBs = [|C8.pack $inflected|]
137+
where inflectedT = T.pack $ i $ nameBase nam
138+
inflectedBs = bsToExp $ T.encodeUtf8 inflectedT
135139
_ ->
136140
error "unsupported constructor in makeFromFieldClause"

0 commit comments

Comments
 (0)