-- To get symptom for a particular id
import System.IO
import System.Environment
import Data.Maybe
import Control.Exception
import Database.HDBC
import Database.HDBC.PostgreSQL

-- ------------------------------

pqGetValue :: [[SqlValue]] -> Int -> Int -> String
pqGetValue xs tuple_num field_num = fromSql $ (xs !! tuple_num) !! field_num

-- ------------------------------

-- ==============================

main = 
 Control.Exception.catch (do

  e <- getEnv "WWW_id"

  passString <- getEnv "HOMOEOPIM_POOKAI_PASSWORD"
  let connString = "host=localhost port=5433 dbname=homoeopim user=pookai password=" ++ passString
  c <- connectPostgreSQL connString -- open database connection

  let query1 = "select symptom from symptom where id=" ++ e
  moja1 <- prepare c query1

  execute moja1 []
  result1 <- fetchAllRows moja1
  let res = pqGetValue result1 0 0

  putStrLn "Content-type: text/plain\n"

  putStr res

  disconnect c)
  handler where
   handler :: SqlError -> IO ()
   handler err = putStrLn $ "Content-type: text/plain\n\nOh no: " ++ show err

