module Database.Relational.Query.TH (
defineTable,
defineTableDefault,
unsafeInlineQuery,
inlineQuery,
defineTableTypesAndRecord,
defineTableTypesAndRecordDefault,
defineHasPrimaryKeyInstance,
defineHasPrimaryKeyInstanceWithConfig,
defineHasPrimaryKeyInstanceDefault,
defineHasNotNullKeyInstance,
defineHasNotNullKeyInstanceWithConfig,
defineHasNotNullKeyInstanceDefault,
defineScalarDegree,
defineColumns, defineColumnsDefault,
defineTableTypes, defineTableTypesWithConfig, defineTableTypesDefault,
definePrimaryQuery,
definePrimaryUpdate,
derivationExpDefault,
tableVarExpDefault,
relationVarExp,
relationVarExpDefault,
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, arrowT, 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, toDataCon)
import Language.Haskell.TH.Lib.Extra (simpleValD, maybeD, integralE)
import Database.Record.TH
(columnOffsetsVarNameDefault, recordTypeName, recordType,
defineRecordTypeWithConfig, defineHasColumnConstraintInstance)
import qualified Database.Record.TH as Record
import Database.Relational.Query
(Table, Pi, id', Relation, ProductConstructor (..),
NameConfig (..), SchemaNameMode (..), IdentifierQuotation (..),
Config (normalizedTableName, schemaNameMode, nameConfig, identifierQuotation), defaultConfig,
relationalQuerySQL, Query, relationalQuery, KeyUpdate,
Insert, derivedInsert, InsertQuery, derivedInsertQuery,
HasConstraintKey(constraintKey), Primary, NotNull, primary, primaryUpdate)
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 . recordType (recordConfig $ nameConfig config) scm
defineHasPrimaryKeyInstanceDefault :: String
-> String
-> TypeQ
-> [Int]
-> Q [Dec]
defineHasPrimaryKeyInstanceDefault =
defineHasPrimaryKeyInstanceWithConfig defaultConfig
defineHasNotNullKeyInstance :: TypeQ
-> Int
-> Q [Dec]
defineHasNotNullKeyInstance =
defineHasColumnConstraintInstance [t| NotNull |]
defineHasNotNullKeyInstanceWithConfig :: Config
-> String
-> String
-> Int
-> Q [Dec]
defineHasNotNullKeyInstanceWithConfig config scm =
defineHasNotNullKeyInstance . recordType (recordConfig $ nameConfig config) scm
defineHasNotNullKeyInstanceDefault :: String
-> String
-> Int
-> Q [Dec]
defineHasNotNullKeyInstanceDefault =
defineHasNotNullKeyInstanceWithConfig defaultConfig
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
relationVarExpDefault :: String
-> String
-> ExpQ
relationVarExpDefault = relationVarExp defaultConfig
defineProductConstructorInstance :: TypeQ -> ExpQ -> [TypeQ] -> Q [Dec]
defineProductConstructorInstance recTypeQ recData colTypes =
[d| instance ProductConstructor $(foldr (appT . (arrowT `appT`)) recTypeQ colTypes) where
productConstructor = $(recData)
|]
defineProductConstructorInstanceWithConfig :: Config -> String -> String -> [TypeQ] -> Q [Dec]
defineProductConstructorInstanceWithConfig config schema table colTypes = do
let typeName = recordTypeName (recordConfig $ nameConfig config) schema table
defineProductConstructorInstance
(toTypeCon typeName)
(toDataCon typeName)
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")
(recordType 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
defineTableTypesDefault :: Config
-> String
-> String
-> [((String, TypeQ), Maybe TypeQ)]
-> Q [Dec]
defineTableTypesDefault = defineTableTypesWithConfig
defineTableTypesAndRecord :: Config
-> String
-> String
-> [(String, TypeQ)]
-> [Name]
-> Q [Dec]
defineTableTypesAndRecord config schema table columns derives = do
recD <- defineRecordTypeWithConfig (recordConfig $ nameConfig config) schema table columns derives
rconD <- defineProductConstructorInstanceWithConfig config schema table [t | (_, t) <- columns]
tableDs <- defineTableTypesWithConfig config schema table [(c, Nothing) | c <- columns ]
return $ recD ++ rconD ++ tableDs
defineTableTypesAndRecordDefault :: Config
-> String
-> String
-> [(String, TypeQ)]
-> [Name]
-> Q [Dec]
defineTableTypesAndRecordDefault = defineTableTypesAndRecord
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 = recordType (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
defineTableDefault :: Config
-> String
-> String
-> [(String, TypeQ)]
-> [Name]
-> [Int]
-> Maybe Int
-> Q [Dec]
defineTableDefault = defineTable
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
return $ concat [pw, cs, pc]