{-# LINE 1 "Database/HDBC/PostgreSQL/PTypeConv.hsc" #-}
-- -*- mode: haskell; -*-
{-# LINE 2 "Database/HDBC/PostgreSQL/PTypeConv.hsc" #-}
module Database.HDBC.PostgreSQL.PTypeConv where
import Database.HDBC.ColTypes
import Data.Word


{-# LINE 7 "Database/HDBC/PostgreSQL/PTypeConv.hsc" #-}

{-# LINE 8 "Database/HDBC/PostgreSQL/PTypeConv.hsc" #-}


colDescForPGAttr :: Word32 -> Int -> String -> Bool -> SqlColDesc
{-# LINE 11 "Database/HDBC/PostgreSQL/PTypeConv.hsc" #-}
colDescForPGAttr atttypeid attlen formattedtype attnotnull =
    let
        coltype = oidToColType atttypeid

        size = if attlen == -1 then maybeExtractFirstParenthesizedNumber formattedtype
               else Just attlen

        decDigs = if coltype == SqlNumericT then maybeExtractSecondParenthesizedNumber formattedtype
                  else Nothing
    in
      SqlColDesc { colType = coltype,
                   colSize = size,
                   colOctetLength = Nothing, -- not available in postgres
                   colDecDigits = decDigs,
                   colNullable = Just attnotnull }
    where
      maybeExtractFirstParenthesizedNumber s = case extractParenthesizedInts s of n:_ -> Just n; _ -> Nothing

      maybeExtractSecondParenthesizedNumber s = case extractParenthesizedInts s of _:n2:_ -> Just n2; _ -> Nothing

      extractParenthesizedInts :: String -> [Int]
      extractParenthesizedInts s =
          case takeWhile (/=')') $ dropWhile (/='(') s of
            '(':textBetweenParens ->
                case map fst $ reads $ "[" ++ textBetweenParens ++ "]" of
                  l:_ -> l
                  [] -> []
            _ -> []



oidToColDef :: Word32 -> SqlColDesc
{-# LINE 43 "Database/HDBC/PostgreSQL/PTypeConv.hsc" #-}
oidToColDef oid =
    SqlColDesc {colType = (oidToColType oid),
                colSize = Nothing,
                colOctetLength = Nothing,
                colDecDigits = Nothing,
                colNullable = Nothing}

oidToColType :: Word32 -> SqlTypeId
{-# LINE 51 "Database/HDBC/PostgreSQL/PTypeConv.hsc" #-}
oidToColType oid =
    case oid of
      18 -> SqlCharT
{-# LINE 54 "Database/HDBC/PostgreSQL/PTypeConv.hsc" #-}
      409 -> SqlCharT
{-# LINE 55 "Database/HDBC/PostgreSQL/PTypeConv.hsc" #-}
      410 -> SqlCharT
{-# LINE 56 "Database/HDBC/PostgreSQL/PTypeConv.hsc" #-}
      411 -> SqlCharT
{-# LINE 57 "Database/HDBC/PostgreSQL/PTypeConv.hsc" #-}
      19 -> SqlVarCharT
{-# LINE 58 "Database/HDBC/PostgreSQL/PTypeConv.hsc" #-}
      1042 -> SqlCharT
{-# LINE 59 "Database/HDBC/PostgreSQL/PTypeConv.hsc" #-}
      1043 -> SqlVarCharT
{-# LINE 60 "Database/HDBC/PostgreSQL/PTypeConv.hsc" #-}
      25 -> SqlVarCharT
{-# LINE 61 "Database/HDBC/PostgreSQL/PTypeConv.hsc" #-}
      142 -> SqlVarCharT
{-# LINE 62 "Database/HDBC/PostgreSQL/PTypeConv.hsc" #-}
      17 -> SqlVarBinaryT
{-# LINE 63 "Database/HDBC/PostgreSQL/PTypeConv.hsc" #-}
      21 -> SqlSmallIntT
{-# LINE 64 "Database/HDBC/PostgreSQL/PTypeConv.hsc" #-}
      26 -> SqlIntegerT
{-# LINE 65 "Database/HDBC/PostgreSQL/PTypeConv.hsc" #-}
      28 -> SqlIntegerT
{-# LINE 66 "Database/HDBC/PostgreSQL/PTypeConv.hsc" #-}
      23 -> SqlBigIntT
{-# LINE 67 "Database/HDBC/PostgreSQL/PTypeConv.hsc" #-}
      20 -> SqlBigIntT
{-# LINE 68 "Database/HDBC/PostgreSQL/PTypeConv.hsc" #-}
      1700 -> SqlNumericT
{-# LINE 69 "Database/HDBC/PostgreSQL/PTypeConv.hsc" #-}
      700 -> SqlRealT
{-# LINE 70 "Database/HDBC/PostgreSQL/PTypeConv.hsc" #-}
      701 -> SqlFloatT
{-# LINE 71 "Database/HDBC/PostgreSQL/PTypeConv.hsc" #-}
      1082 -> SqlDateT
{-# LINE 72 "Database/HDBC/PostgreSQL/PTypeConv.hsc" #-}
      702 -> SqlTimestampWithZoneT
{-# LINE 73 "Database/HDBC/PostgreSQL/PTypeConv.hsc" #-}

      1184 -> SqlTimestampWithZoneT
{-# LINE 75 "Database/HDBC/PostgreSQL/PTypeConv.hsc" #-}
      1114 -> SqlTimestampT
{-# LINE 76 "Database/HDBC/PostgreSQL/PTypeConv.hsc" #-}
      1296 -> SqlTimestampT
{-# LINE 77 "Database/HDBC/PostgreSQL/PTypeConv.hsc" #-}

      1083 -> SqlTimeT
{-# LINE 79 "Database/HDBC/PostgreSQL/PTypeConv.hsc" #-}
      1266 -> SqlTimeWithZoneT
{-# LINE 80 "Database/HDBC/PostgreSQL/PTypeConv.hsc" #-}

      704 -> SqlIntervalT SqlIntervalMonthT -- SqlIntervalMonthT chosen arbitrarily in these two. PG allows any parts
{-# LINE 82 "Database/HDBC/PostgreSQL/PTypeConv.hsc" #-}
      703   -> SqlIntervalT SqlIntervalMonthT -- of an interval (microsecond to millennium) to be specified together.
{-# LINE 83 "Database/HDBC/PostgreSQL/PTypeConv.hsc" #-}
      1186 -> SqlIntervalT SqlIntervalMonthT
      16 -> SqlBitT
{-# LINE 85 "Database/HDBC/PostgreSQL/PTypeConv.hsc" #-}
      x -> SqlUnknownT (show x)