haskell-relational-record/relational-record-examples/orf/DataSource.hs
2014-12-21 15:30:23 +09:00

36 lines
998 B
Haskell

{-# LANGUAGE TemplateHaskell #-}
module DataSource (
connect, convTypes, defineTable
) where
import Data.Time (Day, LocalTime)
import Database.HDBC.Query.TH (defineTableFromDB)
import Database.HDBC.Schema.Driver (typeMap)
import Database.HDBC.Schema.SQLite3 (driverSQLite3)
import Database.HDBC.Sqlite3 (Connection, connectSqlite3)
import Database.Record.TH (derivingShow)
import Language.Haskell.TH (Q, Dec, TypeQ)
import Language.Haskell.TH.Name.CamelCase (ConName)
connect :: IO Connection
connect = connectSqlite3 "examples.db"
convTypes :: [(String, TypeQ)]
convTypes =
[ ("float", [t|Double|])
, ("date", [t|Day|])
, ("datetime", [t|LocalTime|])
, ("double", [t|Double|])
, ("varchar", [t|String|])
]
defineTable :: String -> Q [Dec]
defineTable tableName =
defineTableFromDB
connect
(driverSQLite3 { typeMap = convTypes }) -- overwrite the default type map with yours
"main" -- schema name, ignored by SQLite
tableName
[derivingShow]