module Database.Relational.Query.TH (
defineTable,
unsafeInlineQuery,
inlineQuery,
defineTableTypesAndRecord,
defineHasPrimaryKeyInstance,
defineHasPrimaryKeyInstanceWithConfig,
defineHasNotNullKeyInstance,
defineHasNotNullKeyInstanceWithConfig,
defineScalarDegree,
defineColumns, defineColumnsDefault,
defineTuplePi,
defineTableTypes, defineTableTypesWithConfig,
definePrimaryQuery,
definePrimaryUpdate,
derivationExpDefault,
tableVarExpDefault,
relationVarExp,
defineSqlsWithPrimaryKey,
defineSqlsWithPrimaryKeyDefault,
defineProductConstructorInstance,
makeRelationalRecordDefault,
reifyRelation,
) where
import Data.Char (toUpper, toLower)
import Data.List (foldl1')
import Data.Array.IArray ((!))
import Language.Haskell.TH
(Name, nameBase, Q, reify, TypeQ, Type (AppT, ConT), ExpQ,
tupleT, appT, Dec, stringE, listE)
import Language.Haskell.TH.Compat.Reify (unVarI)
import Language.Haskell.TH.Name.CamelCase
(VarName, varName, ConName (ConName), conName, varNameWithPrefix, varCamelcaseName, toVarExp, toTypeCon)
import Language.Haskell.TH.Lib.Extra (simpleValD, maybeD, integralE)
import Database.Record.TH
(columnOffsetsVarNameDefault, recordTypeName, recordTemplate,
defineRecordTypeWithConfig, defineHasColumnConstraintInstance)
import qualified Database.Record.TH as Record
import Database.Relational.Query
(Table, Pi, id', Relation, ShowConstantTermsSQL,
NameConfig (..), SchemaNameMode (..), IdentifierQuotation (..),
Config (normalizedTableName, schemaNameMode, nameConfig, identifierQuotation),
relationalQuerySQL, Query, relationalQuery, KeyUpdate,
Insert, derivedInsert, InsertQuery, derivedInsertQuery,
HasConstraintKey(constraintKey), Primary, NotNull, primary, primaryUpdate)
import Database.Relational.Query.BaseTH (defineProductConstructorInstance, defineTuplePi)
import Database.Relational.Query.Scalar (defineScalarDegree)
import Database.Relational.Query.Constraint (Key, unsafeDefineConstraintKey)
import Database.Relational.Query.Table (TableDerivable (..))
import qualified Database.Relational.Query.Table as Table
import Database.Relational.Query.Relation (derivedRelation)
import Database.Relational.Query.SQL (QuerySuffix)
import Database.Relational.Query.Type (unsafeTypedQuery)
import qualified Database.Relational.Query.Pi.Unsafe as UnsafePi
defineHasConstraintKeyInstance :: TypeQ
-> TypeQ
-> TypeQ
-> [Int]
-> Q [Dec]
defineHasConstraintKeyInstance constraint recType colType indexes = do
ck <- [d| instance HasConstraintKey $constraint $recType $colType where
constraintKey = unsafeDefineConstraintKey $(listE [integralE ix | ix <- indexes])
|]
return ck
defineHasPrimaryKeyInstance :: TypeQ
-> TypeQ
-> [Int]
-> Q [Dec]
defineHasPrimaryKeyInstance recType colType indexes = do
kc <- Record.defineHasPrimaryKeyInstance recType indexes
ck <- defineHasConstraintKeyInstance [t| Primary |] recType colType indexes
return $ kc ++ ck
defineHasPrimaryKeyInstanceWithConfig :: Config
-> String
-> String
-> TypeQ
-> [Int]
-> Q [Dec]
defineHasPrimaryKeyInstanceWithConfig config scm =
defineHasPrimaryKeyInstance . fst . recordTemplate (recordConfig $ nameConfig config) scm
defineHasNotNullKeyInstance :: TypeQ
-> Int
-> Q [Dec]
defineHasNotNullKeyInstance =
defineHasColumnConstraintInstance [t| NotNull |]
defineHasNotNullKeyInstanceWithConfig :: Config
-> String
-> String
-> Int
-> Q [Dec]
defineHasNotNullKeyInstanceWithConfig config scm =
defineHasNotNullKeyInstance . fst . recordTemplate (recordConfig $ nameConfig config) scm
columnTemplate' :: TypeQ
-> VarName
-> ExpQ
-> TypeQ
-> Q [Dec]
columnTemplate' recType var' iExp colType = do
let var = varName var'
simpleValD var [t| Pi $recType $colType |]
[| UnsafePi.definePi $(iExp) |]
columnTemplate :: Maybe (TypeQ, VarName)
-> TypeQ
-> VarName
-> ExpQ
-> TypeQ
-> Q [Dec]
columnTemplate mayConstraint recType var' iExp colType = do
col <- columnTemplate' recType var' iExp colType
cr <- maybe
(return [])
( \(constraint, cname') -> do
simpleValD (varName cname') [t| Key $constraint $recType $colType |]
[| unsafeDefineConstraintKey $(iExp) |] )
mayConstraint
return $ col ++ cr
defineColumns :: ConName
-> [((VarName, TypeQ), Maybe (TypeQ, VarName))]
-> Q [Dec]
defineColumns recTypeName cols = do
let defC ((cn, ct), mayCon) ix = columnTemplate mayCon (toTypeCon recTypeName) cn
[| $(toVarExp . columnOffsetsVarNameDefault $ conName recTypeName) ! $(integralE ix) |] ct
fmap concat . sequence $ zipWith defC cols [0 :: Int ..]
defineColumnsDefault :: ConName
-> [((String, TypeQ), Maybe TypeQ)]
-> Q [Dec]
defineColumnsDefault recTypeName cols =
defineColumns recTypeName [((varN n, ct), fmap (withCName n) mayC) | ((n, ct), mayC) <- cols]
where varN name = varCamelcaseName (name ++ "'")
withCName name t = (t, varCamelcaseName ("constraint_key_" ++ name))
defineTableDerivableInstance :: TypeQ -> String -> [String] -> Q [Dec]
defineTableDerivableInstance recordType' table columns =
[d| instance TableDerivable $recordType' where
derivedTable = Table.table $(stringE table) $(listE $ map stringE columns)
|]
defineTableDerivations :: VarName
-> VarName
-> VarName
-> VarName
-> TypeQ
-> Q [Dec]
defineTableDerivations tableVar' relVar' insVar' insQVar' recordType' = do
let tableVar = varName tableVar'
tableDs <- simpleValD tableVar [t| Table $recordType' |]
[| derivedTable |]
let relVar = varName relVar'
relDs <- simpleValD relVar [t| Relation () $recordType' |]
[| derivedRelation |]
let insVar = varName insVar'
insDs <- simpleValD insVar [t| Insert $recordType' |]
[| derivedInsert id' |]
let insQVar = varName insQVar'
insQDs <- simpleValD insQVar [t| forall p . Relation p $recordType' -> InsertQuery p |]
[| derivedInsertQuery id' |]
return $ concat [tableDs, relDs, insDs, insQDs]
defineTableTypes :: VarName
-> VarName
-> VarName
-> VarName
-> TypeQ
-> String
-> [String]
-> Q [Dec]
defineTableTypes tableVar' relVar' insVar' insQVar' recordType' table columns = do
iDs <- defineTableDerivableInstance recordType' table columns
dDs <- defineTableDerivations tableVar' relVar' insVar' insQVar' recordType'
return $ iDs ++ dDs
tableSQL :: Bool -> SchemaNameMode -> IdentifierQuotation -> String -> String -> String
tableSQL normalize snm iq schema table = case snm of
SchemaQualified -> (qt normalizeS) ++ '.' : (qt normalizeT)
SchemaNotQualified -> (qt normalizeT)
where
normalizeS
| normalize = map toUpper schema
| otherwise = schema
normalizeT
| normalize = map toLower table
| otherwise = table
qt = quote iq
quote :: IdentifierQuotation -> String -> String
quote NoQuotation s = s
quote (Quotation q) s = q : (escape s) ++ q : []
where escape = (>>= (\c -> if c == q then [q, q] else [c]))
derivationVarNameDefault :: String -> VarName
derivationVarNameDefault = (`varNameWithPrefix` "derivationFrom")
derivationExpDefault :: String
-> ExpQ
derivationExpDefault = toVarExp . derivationVarNameDefault
tableVarNameDefault :: String -> VarName
tableVarNameDefault = (`varNameWithPrefix` "tableOf")
tableVarExpDefault :: String
-> ExpQ
tableVarExpDefault = toVarExp . tableVarNameDefault
relationVarExp :: Config
-> String
-> String
-> ExpQ
relationVarExp config scm = toVarExp . relationVarName (nameConfig config) scm
defineProductConstructorInstanceWithConfig :: Config -> String -> String -> [TypeQ] -> Q [Dec]
defineProductConstructorInstanceWithConfig config schema table colTypes = do
let tp = recordTemplate (recordConfig $ nameConfig config) schema table
uncurry defineProductConstructorInstance tp colTypes
defineTableTypesWithConfig :: Config
-> String
-> String
-> [((String, TypeQ), Maybe TypeQ)]
-> Q [Dec]
defineTableTypesWithConfig config schema table columns = do
let nmconfig = nameConfig config
recConfig = recordConfig nmconfig
tableDs <- defineTableTypes
(tableVarNameDefault table)
(relationVarName nmconfig schema table)
(table `varNameWithPrefix` "insert")
(table `varNameWithPrefix` "insertQuery")
(fst $ recordTemplate recConfig schema table)
(tableSQL (normalizedTableName config) (schemaNameMode config) (identifierQuotation config) schema table)
(map ((quote (identifierQuotation config)) . fst . fst) columns)
colsDs <- defineColumnsDefault (recordTypeName recConfig schema table) columns
return $ tableDs ++ colsDs
defineTableTypesAndRecord :: Config
-> String
-> String
-> [(String, TypeQ)]
-> [Name]
-> Q [Dec]
defineTableTypesAndRecord config schema table columns derives = do
let recConfig = recordConfig $ nameConfig config
recD <- defineRecordTypeWithConfig recConfig schema table columns derives
rconD <- defineProductConstructorInstanceWithConfig config schema table [t | (_, t) <- columns]
ctD <- [d| instance ShowConstantTermsSQL $(fst $ recordTemplate recConfig schema table) |]
tableDs <- defineTableTypesWithConfig config schema table [(c, Nothing) | c <- columns ]
return $ recD ++ rconD ++ ctD ++ tableDs
definePrimaryQuery :: VarName
-> TypeQ
-> TypeQ
-> ExpQ
-> Q [Dec]
definePrimaryQuery toDef' paramType recType relE = do
let toDef = varName toDef'
simpleValD toDef
[t| Query $paramType $recType |]
[| relationalQuery (primary $relE) |]
definePrimaryUpdate :: VarName
-> TypeQ
-> TypeQ
-> ExpQ
-> Q [Dec]
definePrimaryUpdate toDef' paramType recType tableE = do
let toDef = varName toDef'
simpleValD toDef
[t| KeyUpdate $paramType $recType |]
[| primaryUpdate $tableE |]
defineSqlsWithPrimaryKey :: VarName
-> VarName
-> TypeQ
-> TypeQ
-> ExpQ
-> ExpQ
-> Q [Dec]
defineSqlsWithPrimaryKey sel upd paramType recType relE tableE = do
selD <- definePrimaryQuery sel paramType recType relE
updD <- definePrimaryUpdate upd paramType recType tableE
return $ selD ++ updD
defineSqlsWithPrimaryKeyDefault :: String
-> TypeQ
-> TypeQ
-> ExpQ
-> ExpQ
-> Q [Dec]
defineSqlsWithPrimaryKeyDefault table =
defineSqlsWithPrimaryKey sel upd
where
sel = table `varNameWithPrefix` "select"
upd = table `varNameWithPrefix` "update"
defineWithPrimaryKey :: Config
-> String
-> String
-> TypeQ
-> [Int]
-> Q [Dec]
defineWithPrimaryKey config schema table keyType ixs = do
instD <- defineHasPrimaryKeyInstanceWithConfig config schema table keyType ixs
let recType = fst $ recordTemplate (recordConfig $ nameConfig config) schema table
tableE = tableVarExpDefault table
relE = relationVarExp config schema table
sqlsD <- defineSqlsWithPrimaryKeyDefault table keyType recType relE tableE
return $ instD ++ sqlsD
defineWithNotNullKeyWithConfig :: Config -> String -> String -> Int -> Q [Dec]
defineWithNotNullKeyWithConfig = defineHasNotNullKeyInstanceWithConfig
defineTable :: Config
-> String
-> String
-> [(String, TypeQ)]
-> [Name]
-> [Int]
-> Maybe Int
-> Q [Dec]
defineTable config schema table columns derives primaryIxs mayNotNullIdx = do
tblD <- defineTableTypesAndRecord config schema table columns derives
let pairT x y = appT (appT (tupleT 2) x) y
keyType = foldl1' pairT . map (snd . (columns !!)) $ primaryIxs
primD <- case primaryIxs of
[] -> return []
ixs -> defineWithPrimaryKey config schema table keyType ixs
nnD <- maybeD (\i -> defineWithNotNullKeyWithConfig config schema table i) mayNotNullIdx
return $ tblD ++ primD ++ nnD
unsafeInlineQuery :: TypeQ
-> TypeQ
-> String
-> VarName
-> Q [Dec]
unsafeInlineQuery p r sql qVar' =
simpleValD (varName qVar')
[t| Query $p $r |]
[| unsafeTypedQuery $(stringE sql) |]
reifyRelation :: Name
-> Q (Type, Type)
reifyRelation relVar = do
relInfo <- reify relVar
case unVarI relInfo of
Just (_, (AppT (AppT (ConT prn) p) r), _)
| prn == ''Relation -> return (p, r)
_ ->
fail $ "expandRelation: Variable must have Relation type: " ++ show relVar
inlineQuery :: Name
-> Relation p r
-> Config
-> QuerySuffix
-> String
-> Q [Dec]
inlineQuery relVar rel config sufs qns = do
(p, r) <- reifyRelation relVar
unsafeInlineQuery (return p) (return r)
(relationalQuerySQL config rel sufs)
(varCamelcaseName qns)
makeRelationalRecordDefault :: Name
-> Q [Dec]
makeRelationalRecordDefault recTypeName = do
let recTypeConName = ConName recTypeName
((tyCon, dataCon), (mayNs, cts)) <- Record.reifyRecordType recTypeName
pw <- Record.defineColumnOffsets recTypeConName cts
cs <- maybe
(return [])
(\ns -> defineColumnsDefault recTypeConName
[ ((nameBase n, ct), Nothing) | n <- ns | ct <- cts ])
mayNs
pc <- defineProductConstructorInstance tyCon dataCon cts
ct <- [d| instance ShowConstantTermsSQL $tyCon |]
return $ concat [pw, cs, pc, ct]