Skip to content

Commit 299b141

Browse files
committed
fix TH code for changed Entity typeclass
1 parent dec085c commit 299b141

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

postgresql-query.cabal

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: postgresql-query
2-
version: 1.4.0
2+
version: 2.0.0
33

44
synopsis: Sql interpolating quasiquote plus some kind of primitive ORM
55
using it
@@ -78,6 +78,8 @@ library
7878
, semigroups
7979
, template-haskell
8080
, text
81+
, th-lift
82+
, th-lift-instances
8183
, time
8284
, transformers
8385
, transformers-base

src/Database/PostgreSQL/Query/TH.hs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,18 @@ module Database.PostgreSQL.Query.TH
1717
import Prelude
1818

1919
import Control.Applicative
20+
import Control.Monad
2021
import Data.Default
2122
import Data.FileEmbed ( embedFile )
23+
import Data.String
2224
import Database.PostgreSQL.Query.Entity ( Entity(..) )
2325
import Database.PostgreSQL.Query.TH.SqlExp
26+
import Database.PostgreSQL.Query.Types ( FN(..) )
2427
import Database.PostgreSQL.Simple.FromRow ( FromRow(..), field )
2528
import Database.PostgreSQL.Simple.ToRow ( ToRow(..) )
2629
import Database.PostgreSQL.Simple.Types ( Query(..) )
2730
import Language.Haskell.TH
31+
import Language.Haskell.TH.Syntax
2832

2933
-- | Return constructor name
3034
cName :: (Monad m) => Con -> m Name
@@ -196,12 +200,15 @@ deriveEntity opts tname = do
196200
[(mkName unidname, NotStrict, idtype)]
197201
iddec = NewtypeInstD [] entityIdName [ConT tname]
198202
idcon (eoDeriveClasses opts)
199-
tblName = eoTableName opts tnames
200-
fldNames = map (eoColumnNames opts . nameBase) $ cFieldNames tcon
203+
tblName = fromString $ eoTableName opts tnames
204+
fldNames = map (fromString . eoColumnNames opts . nameBase)
205+
$ cFieldNames tcon
201206
VarE ntableName <- [e|tableName|]
202207
VarE nfieldNames <- [e|fieldNames|]
203-
let tbldec = FunD ntableName [Clause [WildP] (NormalB $ LitE $ stringL tblName) []]
204-
flddec = FunD nfieldNames [Clause [WildP] (NormalB $ ListE $ map (LitE . stringL) fldNames) []]
208+
tblExp <- lift (tblName :: FN)
209+
fldExp <- mapM lift (fldNames :: [FN])
210+
let tbldec = FunD ntableName [Clause [WildP] (NormalB tblExp) []]
211+
flddec = FunD nfieldNames [Clause [WildP] (NormalB $ ListE fldExp) []]
205212
ret = InstanceD [] econt
206213
[ iddec, tbldec, flddec ]
207214
syndec = TySynD (mkName idname) [] (AppT (ConT entityIdName) (ConT tname))

src/Database/PostgreSQL/Query/Types.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ import Database.PostgreSQL.Simple.FromField
5353
import Database.PostgreSQL.Simple.ToField
5454
( ToField )
5555
import GHC.Generics
56+
import Instances.TH.Lift ()
57+
import Language.Haskell.TH.Lift ( deriveLift )
5658

5759
import qualified Data.List as L
5860
import qualified Control.Monad.Trans.State.Lazy as STL
@@ -75,6 +77,7 @@ newtype InetText = InetText
7577
} deriving ( IsString, Eq, Ord, Read, Show
7678
, Typeable, Monoid, ToField )
7779

80+
7881
instance FromField InetText where
7982
fromField fld Nothing = returnError ConversionFailed
8083
fld "can not convert Null to InetText"
@@ -121,6 +124,8 @@ FN ["user","name"]
121124
newtype FN = FN [Text]
122125
deriving (Ord, Eq, Show, Monoid, Typeable, Generic)
123126

127+
$(deriveLift ''FN)
128+
124129
instance ToSqlBuilder FN where
125130
toSqlBuilder (FN tt) =
126131
mconcat

0 commit comments

Comments
 (0)