mirror of
https://github.com/khibino/haskell-relational-record.git
synced 2024-12-03 03:52:10 +03:00
Begins TH for relational-join.
This commit is contained in:
parent
8744e55171
commit
ee80da7529
@ -28,6 +28,7 @@ library
|
||||
Database.Relational.Query.Relation
|
||||
Database.Relational.Query.Sub
|
||||
Database.Relational.Query.Type
|
||||
Database.Relational.Query.TH
|
||||
-- other-modules:
|
||||
build-depends: base <5
|
||||
, array
|
||||
|
42
relational-join/src/Database/Relational/Query/TH.hs
Normal file
42
relational-join/src/Database/Relational/Query/TH.hs
Normal file
@ -0,0 +1,42 @@
|
||||
{-# LANGUAGE TemplateHaskell #-}
|
||||
|
||||
module Database.Relational.Query.TH (
|
||||
inlineQuery
|
||||
) where
|
||||
|
||||
import Language.Haskell.TH
|
||||
(Q, reify, Info (VarI), TypeQ, Type (AppT, ConT),
|
||||
Dec, sigD, valD, varP, normalB, stringE, listE)
|
||||
import Language.Haskell.TH.Name.CamelCase (VarName, varName)
|
||||
import Language.Haskell.TH.Name.Extra (compileError, simpleValD)
|
||||
|
||||
import Database.Relational.Query.Table (Table)
|
||||
import qualified Database.Relational.Query.Table as Table
|
||||
import Database.Relational.Query.Relation (Relation, toSQL)
|
||||
import Database.Relational.Query.Type (Query, unsafeTypedQuery)
|
||||
|
||||
defineTableInfo :: VarName
|
||||
-> TypeQ
|
||||
-> String
|
||||
-> [String]
|
||||
-> Int
|
||||
-> Q [Dec]
|
||||
defineTableInfo tableVar' recordType table fields width = do
|
||||
let tableVar = varName tableVar'
|
||||
simpleValD tableVar [t| Table $(recordType) |]
|
||||
[| Table.table $(stringE table) $(listE $ map stringE fields) |]
|
||||
|
||||
inlineQuery :: VarName -> Relation r -> VarName -> TypeQ -> Q [Dec]
|
||||
inlineQuery relVar' rel qVar' paramType = do
|
||||
let relVar = varName relVar'
|
||||
qVar = varName qVar'
|
||||
relInfo <- reify relVar
|
||||
case relInfo of
|
||||
VarI _ (AppT (ConT rn) r) _ _ | rn == ''Relation -> do
|
||||
sig <- sigD qVar [t| Query $(paramType) $(return r) |]
|
||||
var <- valD (varP qVar)
|
||||
(normalB [| unsafeTypedQuery $(stringE . toSQL $ rel) |])
|
||||
[]
|
||||
return [sig, var]
|
||||
_ ->
|
||||
compileError $ "expandRelation: Variable must have Relation type: " ++ show relVar
|
Loading…
Reference in New Issue
Block a user