Closed k0001 closed 9 years ago
Hmm... nevermind this pull-request, I think it was a bit premature, as it still doesn't let me keep bindings as organized as I desired.
In case somebody finds it useful, I switched to using this TH internally in my project:
defineTable
:: String -- ^ Haskell record name and fields prefix.
-> String -- ^ SQL schema name.
-> String -- ^ SQL table name.
-> [(String, String, TypeQ)]
-- ^ SQL column name, Haskell record field name, Haskell type.
-> Q [Dec]
defineTable nRecord nSchema nTable cols = do
recD <- defineRecordType
(toConName nRecord)
[(toVarName ("_" ++ nRecord ++ "_" ++ f ++ "_"), t) | (_,f,t) <- cols]
(toConName <$> ["Eq", "Show", "Data", "Typeable", "Generic"])
rconD <- defineProductConstructorInstance
(toTypeCon (toConName nRecord))
(toDataCon (toConName nRecord))
[t | (_,_,t) <- cols]
tableD <- defineTableTypes
(varNameWithPrefix nRecord "table")
(varNameWithPrefix nRecord "rel")
(varNameWithPrefix nRecord "insert")
(varNameWithPrefix nRecord "insertQuery")
(toTypeCon (toConName nRecord))
(mconcat ["\"", nSchema , "\".\"", nTable, "\""])
[c | (c,_,_) <- cols]
colD <- defineColumns
(toConName nRecord)
[((toVarName ("_" ++ nRecord ++ "_" ++ f), t), Nothing) | (_,f,t) <- cols]
sqlvD <- makeRecordPersistableWithSqlType
[t|SqlValue|]
(varNameWithPrefix nRecord "fromSql",
varNameWithPrefix nRecord "toSql")
(toTypeCon (toConName nRecord), toDataCon (toConName nRecord))
(length cols)
return $ recD ++ rconD ++ tableD ++ colD ++ sqlvD
This feature allows the user to have more control about the names that are generated so that they can be made more friendly to either qualified imports or unqualified imports as desired. It also allows the SQL column names to be different from the Haskell field names, thus allowing for more flexibility as well.
No camel-case or similar conversion of the names specified by the user is done.
Example usage:
Expands into: