{-# LINE 1 "Database/HDBC/ODBC/TypeConv.hsc" #-}
-- -*- mode: haskell; -*-
{-# CFILES hdbc-odbc-helper.c #-}
-- Above line for hugs
module Database.HDBC.ODBC.TypeConv(fromOTypeInfo, fromOTypeCol) where
import Database.HDBC.Types
import Database.HDBC
import Database.HDBC.DriverUtils
import Database.HDBC.ODBC.Types
import Database.HDBC.ODBC.Utils
import Foreign.C.Types
import Foreign.ForeignPtr
import Foreign.Ptr
import Control.Concurrent.MVar
import Foreign.C.String
import Foreign.Marshal
import Foreign.Storable
import Control.Monad
import Data.List
import Data.Word
import Data.Int
import Control.Exception
import System.IO
import Data.Maybe

l _ = return ()
-- l m = hPutStrLn stderr ("\n" ++ m)


{-# LINE 31 "Database/HDBC/ODBC/TypeConv.hsc" #-}




fromOTypeInfo :: String         -- ^ Column name
              -> Int16 -- ^ Data type
{-# LINE 37 "Database/HDBC/ODBC/TypeConv.hsc" #-}
              -> Word64     -- ^ Column size
{-# LINE 38 "Database/HDBC/ODBC/TypeConv.hsc" #-}
              -> Int16 -- ^ Is it nullable
{-# LINE 39 "Database/HDBC/ODBC/TypeConv.hsc" #-}
              -> (String, SqlColDesc)
fromOTypeInfo colname datatype colsize nullable =
    (colname,
     SqlColDesc {colType = convdatatype datatype,
                 colOctetLength = Nothing,
                 colDecDigits = Nothing,
                 colSize = Just (fromIntegral colsize),
                 colNullable = case nullable of
                                 0 -> Just False
{-# LINE 48 "Database/HDBC/ODBC/TypeConv.hsc" #-}
                                 1 -> Just True
{-# LINE 49 "Database/HDBC/ODBC/TypeConv.hsc" #-}
                                 _ -> Nothing
                }
    )

fromOTypeCol (_:_:_:colname:datatype:_:colsize:buflen:decdig:precrad:nullable:_:_:_:subtype:octetlen:_) =
    fromOTypeInfo (fromSql colname)
                  (fromIntegral ((fromSql datatype)::Int))
                  (fromSql colsize)
                  (fromIntegral ((fromSql nullable)::Int))
fromOTypeCol x = error $ "fromOTypeCol: unexpected result set: " ++ show x

convdatatype :: Int16 -> SqlTypeId
{-# LINE 61 "Database/HDBC/ODBC/TypeConv.hsc" #-}
convdatatype intype =
    case intype of
      1 -> SqlCharT
{-# LINE 64 "Database/HDBC/ODBC/TypeConv.hsc" #-}
      12 -> SqlVarCharT
{-# LINE 65 "Database/HDBC/ODBC/TypeConv.hsc" #-}
      -1 -> SqlLongVarCharT
{-# LINE 66 "Database/HDBC/ODBC/TypeConv.hsc" #-}
      -8 -> SqlWCharT
{-# LINE 67 "Database/HDBC/ODBC/TypeConv.hsc" #-}
      -9 -> SqlWVarCharT
{-# LINE 68 "Database/HDBC/ODBC/TypeConv.hsc" #-}
      -10 -> SqlWLongVarCharT
{-# LINE 69 "Database/HDBC/ODBC/TypeConv.hsc" #-}
      3 -> SqlDecimalT
{-# LINE 70 "Database/HDBC/ODBC/TypeConv.hsc" #-}
      2 -> SqlNumericT
{-# LINE 71 "Database/HDBC/ODBC/TypeConv.hsc" #-}
      5 -> SqlSmallIntT
{-# LINE 72 "Database/HDBC/ODBC/TypeConv.hsc" #-}
      4 -> SqlIntegerT
{-# LINE 73 "Database/HDBC/ODBC/TypeConv.hsc" #-}
      7 -> SqlRealT
{-# LINE 74 "Database/HDBC/ODBC/TypeConv.hsc" #-}
      6 -> SqlFloatT
{-# LINE 75 "Database/HDBC/ODBC/TypeConv.hsc" #-}
      8 -> SqlDoubleT
{-# LINE 76 "Database/HDBC/ODBC/TypeConv.hsc" #-}
      -7 -> SqlBitT
{-# LINE 77 "Database/HDBC/ODBC/TypeConv.hsc" #-}
      -6 -> SqlTinyIntT
{-# LINE 78 "Database/HDBC/ODBC/TypeConv.hsc" #-}
      -5 -> SqlBigIntT
{-# LINE 79 "Database/HDBC/ODBC/TypeConv.hsc" #-}
      -2 -> SqlBinaryT
{-# LINE 80 "Database/HDBC/ODBC/TypeConv.hsc" #-}
      -3 -> SqlVarBinaryT
{-# LINE 81 "Database/HDBC/ODBC/TypeConv.hsc" #-}
      -4 -> SqlLongVarBinaryT
{-# LINE 82 "Database/HDBC/ODBC/TypeConv.hsc" #-}
      91 -> SqlDateT
{-# LINE 83 "Database/HDBC/ODBC/TypeConv.hsc" #-}
      92 -> SqlTimeT
{-# LINE 84 "Database/HDBC/ODBC/TypeConv.hsc" #-}
      93 -> SqlTimestampT
{-# LINE 85 "Database/HDBC/ODBC/TypeConv.hsc" #-}
      -- ODBC libraries don't seem to define the UTC items
       -- {const SQL_TYPE_UTCDATETIME} -> SqlUTCDateTimeT
       -- {const SQL_TYPE_UTCTIME} -> SqlUTCTimeT
      102 -> SqlIntervalT SqlIntervalMonthT
{-# LINE 89 "Database/HDBC/ODBC/TypeConv.hsc" #-}
      101 -> SqlIntervalT SqlIntervalYearT
{-# LINE 90 "Database/HDBC/ODBC/TypeConv.hsc" #-}
      107 -> SqlIntervalT SqlIntervalYearToMonthT
{-# LINE 91 "Database/HDBC/ODBC/TypeConv.hsc" #-}
      103 -> SqlIntervalT SqlIntervalDayT
{-# LINE 92 "Database/HDBC/ODBC/TypeConv.hsc" #-}
      104 -> SqlIntervalT SqlIntervalHourT
{-# LINE 93 "Database/HDBC/ODBC/TypeConv.hsc" #-}
      105 -> SqlIntervalT SqlIntervalMinuteT
{-# LINE 94 "Database/HDBC/ODBC/TypeConv.hsc" #-}
      106 -> SqlIntervalT SqlIntervalSecondT
{-# LINE 95 "Database/HDBC/ODBC/TypeConv.hsc" #-}
      108 -> SqlIntervalT SqlIntervalDayToHourT
{-# LINE 96 "Database/HDBC/ODBC/TypeConv.hsc" #-}
      109 -> SqlIntervalT SqlIntervalDayToMinuteT
{-# LINE 97 "Database/HDBC/ODBC/TypeConv.hsc" #-}
      110 -> SqlIntervalT SqlIntervalDayToSecondT
{-# LINE 98 "Database/HDBC/ODBC/TypeConv.hsc" #-}
      111 -> SqlIntervalT SqlIntervalHourToMinuteT
{-# LINE 99 "Database/HDBC/ODBC/TypeConv.hsc" #-}
      112 -> SqlIntervalT SqlIntervalHourToSecondT
{-# LINE 100 "Database/HDBC/ODBC/TypeConv.hsc" #-}
      113 -> SqlIntervalT SqlIntervalMinuteToSecondT
{-# LINE 101 "Database/HDBC/ODBC/TypeConv.hsc" #-}
      -11 -> SqlGUIDT
{-# LINE 102 "Database/HDBC/ODBC/TypeConv.hsc" #-}
      x -> SqlUnknownT (show x)