{-# LINE 1 "DB/HSQL/ODBC/Type.hsc" #-}
{-# LANGUAGE ForeignFunctionInterface #-}
{-# LINE 2 "DB/HSQL/ODBC/Type.hsc" #-}
module DB.HSQL.ODBC.Type where

import Data.Int(Int32, Int16)
import Data.Word(Word32, Word16)
import Foreign(Ptr,ForeignPtr)

import Database.HSQL.Types(SqlType(..))


{-# LINE 11 "DB/HSQL/ODBC/Type.hsc" #-}

type SQLHANDLE = Ptr ()
type HENV = SQLHANDLE
type HDBC = SQLHANDLE
type HSTMT = SQLHANDLE
type HENVRef = ForeignPtr ()

type SQLSMALLINT  = Int16
{-# LINE 19 "DB/HSQL/ODBC/Type.hsc" #-}
type SQLUSMALLINT = Word16
{-# LINE 20 "DB/HSQL/ODBC/Type.hsc" #-}
type SQLINTEGER   = Int32
{-# LINE 21 "DB/HSQL/ODBC/Type.hsc" #-}
type SQLUINTEGER  = Word32
{-# LINE 22 "DB/HSQL/ODBC/Type.hsc" #-}
type SQLRETURN	  = SQLSMALLINT
type SQLLEN       = SQLINTEGER
type SQLULEN      = SQLINTEGER

type SQL = String

-- |
mkSqlType :: SQLSMALLINT -> SQLULEN -> SQLSMALLINT -> SqlType
mkSqlType sqlChar         size    _    = SqlChar (fromIntegral size)
mkSqlType sqlVarChar      size    _    = SqlVarChar (fromIntegral size)
mkSqlType sqlLongVarChar  size    _    = SqlLongVarChar (fromIntegral size)
mkSqlType sqlDecimal      size    prec = 
    SqlDecimal (fromIntegral size) (fromIntegral prec)
mkSqlType sqlNumeric      size    prec = 
    SqlNumeric (fromIntegral size) (fromIntegral prec)
mkSqlType sqlSmallint     _       _    = SqlSmallInt
mkSqlType sqlInteger      _       _    = SqlInteger
mkSqlType sqlReal          _       _    = SqlReal
-- From: http://msdn.microsoft.com/library/en-us/odbc/htm/odappdpr_2.asp
-- "Depending on the implementation, the precision of SQL_FLOAT can be
-- either 24 or 53:
-- if it is 24, the SQL_FLOAT data type is the same as SQL_REAL;
-- if it is 53, the SQL_FLOAT data type is the same as SQL_DOUBLE."
mkSqlType sqlFloat        _        _    = SqlFloat
mkSqlType sqlDouble		_    	_    = SqlDouble
mkSqlType sqlBit          _       	_    = SqlBit
mkSqlType sqlTinyInt      _    	_    = SqlTinyInt
mkSqlType sqlBigint       _    	_    = SqlBigInt
mkSqlType sqlBinary       size    _    = SqlBinary (fromIntegral size)
mkSqlType sqlVarBinary    size    _    = SqlVarBinary (fromIntegral size)
mkSqlType sqlLongVarBinary size    _    = SqlLongVarBinary (fromIntegral size)
mkSqlType sqlDate         _    	_    = SqlDate
mkSqlType sqlTime         _    	_    = SqlTime
mkSqlType sqlTimestamp	_    	_    = SqlDateTime
mkSqlType sqlWChar        size	_    = SqlWChar (fromIntegral size)
mkSqlType sqlWVarChar     size    _    = SqlWVarChar (fromIntegral size)
mkSqlType sqlWLongVarChar	size    _    = 
    SqlWLongVarChar (fromIntegral size)
mkSqlType tp                        _       _    = SqlUnknown (fromIntegral tp)

-- | #const
sqlChar = 1
{-# LINE 64 "DB/HSQL/ODBC/Type.hsc" #-}

sqlVarChar = 12
{-# LINE 66 "DB/HSQL/ODBC/Type.hsc" #-}

sqlLongVarChar = -1
{-# LINE 68 "DB/HSQL/ODBC/Type.hsc" #-}

sqlDecimal = 3
{-# LINE 70 "DB/HSQL/ODBC/Type.hsc" #-}

sqlNumeric = 2
{-# LINE 72 "DB/HSQL/ODBC/Type.hsc" #-}

sqlSmallint = 5
{-# LINE 74 "DB/HSQL/ODBC/Type.hsc" #-}

sqlInteger = 4
{-# LINE 76 "DB/HSQL/ODBC/Type.hsc" #-}

sqlReal = 7
{-# LINE 78 "DB/HSQL/ODBC/Type.hsc" #-}

sqlFloat = 6
{-# LINE 80 "DB/HSQL/ODBC/Type.hsc" #-}

sqlDouble = 8
{-# LINE 82 "DB/HSQL/ODBC/Type.hsc" #-}

sqlBit = -7
{-# LINE 84 "DB/HSQL/ODBC/Type.hsc" #-}

sqlTinyInt = -6
{-# LINE 86 "DB/HSQL/ODBC/Type.hsc" #-}

sqlBigint = -5
{-# LINE 88 "DB/HSQL/ODBC/Type.hsc" #-}

sqlBinary = -2
{-# LINE 90 "DB/HSQL/ODBC/Type.hsc" #-}

sqlVarBinary = -3
{-# LINE 92 "DB/HSQL/ODBC/Type.hsc" #-}

sqlLongVarBinary = -4
{-# LINE 94 "DB/HSQL/ODBC/Type.hsc" #-}

sqlDate = 9
{-# LINE 96 "DB/HSQL/ODBC/Type.hsc" #-}

sqlTime = 10
{-# LINE 98 "DB/HSQL/ODBC/Type.hsc" #-}

sqlTimestamp = 11
{-# LINE 100 "DB/HSQL/ODBC/Type.hsc" #-}

sqlWChar = -8
{-# LINE 102 "DB/HSQL/ODBC/Type.hsc" #-}

sqlWVarChar = -9
{-# LINE 104 "DB/HSQL/ODBC/Type.hsc" #-}

sqlWLongVarChar = -10
{-# LINE 106 "DB/HSQL/ODBC/Type.hsc" #-}