File: insert-update-delete.hs

package info (click to toggle)
haskelldb 0.9.cvs.601-13
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 680 kB
  • ctags: 33
  • sloc: haskell: 4,392; sh: 1,900; makefile: 130
file content (48 lines) | stat: -rw-r--r-- 1,088 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import Database.HaskellDB
import Dp037.Test_tb1

import TestConnect

printTable db =
    do
    t <- query db (table test_tb1)
    mapM_ (putStrLn . showRow) t

showRow r = lpad 10 (show (r!c11)) ++ lpad 10 (showNullable (r!c12))

showNullable :: Show a => Maybe a -> String
showNullable = maybe "NULL" show

lpad :: Int -> String -> String
lpad x s = replicate (x - length s) ' ' ++ s


testInsert db = 
    do
    insert db test_tb1 (c11 << constant 157 # c12 << constant (Just 56))
    insert db test_tb1 (c11 << constant (-567) # c12 << constant Nothing)

testUpdate db = 
    do
    update db test_tb1 (\r -> r!c11 .==. constant 157) (setC12 (Just 18))
    where
    setC12 x r = c12 << constant x
testDelete db = 
    do
    delete db test_tb1 (\r -> r!c11 .==. constant 157 .||. isNull (r!c12))

test db = 
    do
    putStrLn "Before insert:"
    printTable db
    testInsert db
    putStrLn "After insert:"
    printTable db
    testUpdate db
    putStrLn "After update:"
    printTable db
    testDelete db
    putStrLn "After delete:"
    printTable db

main = argConnect test