{- |
demonstrate mutable tables
-}
module Example.UpdateMonad where

import Company
import Query (showTable, )
import qualified Query
import qualified Table

import Control.Monad (guard, )


main :: IO ()
main = do
   empTable <- Table.create emp
   deptTable <- Table.create dept

   Table.insert empTable
      (Emp
         (EmpNo 7974)
         "LEHMAN"
         Clerk
         (Just (EmpNo 7788))
         1020
         (DeptNo 20))

   emp0 <- Table.from empTable
   showTable (do
      e <- emp0
      guard (job e == Clerk)
      return e)

   Table.delete empTable
      (\e -> ename e == "WARD")

   Table.update empTable
      (\e -> e{sal = sal e + 100})

   emp1 <- Table.from empTable
   dept0 <- Table.from deptTable
   showTable (do
      (dm,es) <- Query.groupBy deptno emp1
      d <- dept0
      guard (deptno d == dm)
      return (dname d, Query.averageInt (map sal es)))
