module Database.MetaHDBC.SqlTypeIdExpQ
    ( fromSqlTypeId, toSqlTypeId
    , fromSqlColDesc, toSqlColDesc
    )
where
import Language.Haskell.TH
import Database.HDBC
import Data.Convertible
fromSqlColDesc :: SqlColDesc -> ExpQ
fromSqlColDesc desc = 
    case (colNullable desc) of
      
      
      
      (Just False) -> fromSqlTypeId (colType desc)
      _            -> [| maybeFromTypeId $(fromSqlTypeId (colType desc)) |]
maybeFromTypeId :: Convertible SqlValue a => (SqlValue -> a) -> (SqlValue -> Maybe a)
maybeFromTypeId _ = fromSql
toSqlColDesc :: SqlColDesc -> ExpQ
toSqlColDesc desc = 
    case (colNullable desc) of
      
      
      
      
      
      
      (Just True)  -> [| maybeToTypeId $(toSqlTypeId (colType desc)) |]
      _            -> toSqlTypeId (colType desc)
maybeToTypeId :: Convertible a SqlValue => (a -> SqlValue) -> (Maybe a -> SqlValue)
maybeToTypeId _ = toSql
fromSqlTypeId :: SqlTypeId -> ExpQ
fromSqlTypeId SqlCharT            = [| fromSql :: (SqlValue -> String) |]
fromSqlTypeId SqlVarCharT         = [| fromSql :: (SqlValue -> String) |]
fromSqlTypeId SqlLongVarCharT     = [| fromSql :: (SqlValue -> String) |]
fromSqlTypeId SqlWCharT           = [| fromSql :: (SqlValue -> String) |]
fromSqlTypeId SqlWVarCharT        = [| fromSql :: (SqlValue -> String) |]
fromSqlTypeId SqlWLongVarCharT    = [| fromSql :: (SqlValue -> String) |]
fromSqlTypeId SqlSmallIntT        = [| fromSql :: (SqlValue -> Int) |]
fromSqlTypeId SqlIntegerT         = [| fromSql :: (SqlValue -> Int) |]
fromSqlTypeId SqlTinyIntT         = [| fromSql :: (SqlValue -> Int) |]
fromSqlTypeId SqlBigIntT          = [| fromSql :: (SqlValue -> Integer) |]
fromSqlTypeId SqlRealT            = [| fromSql :: (SqlValue -> Float) |]
fromSqlTypeId SqlFloatT           = [| fromSql :: (SqlValue -> Float) |]
fromSqlTypeId SqlDoubleT          = [| fromSql :: (SqlValue -> Double) |]
fromSqlTypeId SqlDecimalT         = [| fromSql :: (SqlValue -> Double) |] 
fromSqlTypeId SqlNumericT         = [| fromSql :: (SqlValue -> Double) |] 
fromSqlTypeId SqlBinaryT          = [| fromSql :: (SqlValue -> String) |]
fromSqlTypeId SqlVarBinaryT       = [| fromSql :: (SqlValue -> String) |]
fromSqlTypeId SqlLongVarBinaryT   = [| fromSql :: (SqlValue -> String) |]
fromSqlTypeId SqlDateT            = [| fromSql :: (SqlValue -> String) |]
fromSqlTypeId SqlTimeT            = [| fromSql :: (SqlValue -> String) |]
fromSqlTypeId SqlTimestampT       = [| fromSql :: (SqlValue -> String) |]
fromSqlTypeId SqlUTCDateTimeT     = [| fromSql :: (SqlValue -> String) |]
fromSqlTypeId SqlUTCTimeT         = [| fromSql :: (SqlValue -> String) |]
fromSqlTypeId (SqlIntervalT _)    = [| fromSql :: (SqlValue -> String) |]
fromSqlTypeId SqlBitT             = [| fromSql :: (SqlValue -> Bool) |]
fromSqlTypeId SqlGUIDT            = [| fromSql :: (SqlValue -> Bool) |]
fromSqlTypeId (SqlUnknownT _)     = [| fromSql :: (SqlValue -> String) |]
toSqlTypeId :: SqlTypeId -> ExpQ
toSqlTypeId SqlCharT            = [| toSql :: (String -> SqlValue) |]
toSqlTypeId SqlVarCharT         = [| toSql :: (String -> SqlValue) |]
toSqlTypeId SqlLongVarCharT     = [| toSql :: (String -> SqlValue) |]
toSqlTypeId SqlWCharT           = [| toSql :: (String -> SqlValue) |]
toSqlTypeId SqlWVarCharT        = [| toSql :: (String -> SqlValue) |]
toSqlTypeId SqlWLongVarCharT    = [| toSql :: (String -> SqlValue) |]
toSqlTypeId SqlSmallIntT        = [| toSql :: (Int -> SqlValue) |]
toSqlTypeId SqlIntegerT         = [| toSql :: (Int -> SqlValue) |]
toSqlTypeId SqlTinyIntT         = [| toSql :: (Int -> SqlValue) |]
toSqlTypeId SqlBigIntT          = [| toSql :: (Integer -> SqlValue) |]
toSqlTypeId SqlRealT            = [| toSql :: (Float -> SqlValue) |]
toSqlTypeId SqlFloatT           = [| toSql :: (Float -> SqlValue) |]
toSqlTypeId SqlDoubleT          = [| toSql :: (Double -> SqlValue) |]
toSqlTypeId SqlDecimalT         = [| toSql :: (Double -> SqlValue) |] 
toSqlTypeId SqlNumericT         = [| toSql :: (Double -> SqlValue) |] 
toSqlTypeId SqlBinaryT          = [| toSql :: (String -> SqlValue) |]
toSqlTypeId SqlVarBinaryT       = [| toSql :: (String -> SqlValue) |]
toSqlTypeId SqlLongVarBinaryT   = [| toSql :: (String -> SqlValue) |]
toSqlTypeId (SqlUnknownT _)     = [| toSql :: (String -> SqlValue) |]
toSqlTypeId x = error ("toSqlTypeId: " ++ show x ++ 
                       " not implemented for SqlTypeIdExpQ.toSqlTypeId yet.")