Debian Codegraph @

bin/hs/Sql.hs

raw symbols SCIP
1 module Sql where
2
3 import Data.List
4 import Database.HDBC
5 import Database.HDBC.Sqlite3
6 import Msg
7
8 ----------------------------------------------------------------
9
10 withDB :: FilePath -> (Connection -> IO a) -> IO a
11 withDB db cmd = handleSqlError $ do
12 conn <- connectSqlite3 db
13 res <- withTransaction conn cmd
14 disconnect conn
15 return res
16
17 ----------------------------------------------------------------
18
19 createDB :: Connection -> IO ()
20 createDB conn = () <$ run conn format []
21 where
22 format = "CREATE TABLE IF NOT EXISTS mew (id TEXT, path TEXT, parid TEXT, date TEXT);"
23
24 indexDB :: Connection -> IO ()
25 indexDB conn = do
26 _ <- run conn "CREATE INDEX IF NOT EXISTS mew_id ON mew (id);" []
27 _ <- run conn "REINDEX mew_id;" []
28 _ <- run conn "CREATE INDEX IF NOT EXISTS mew_parid ON mew (parid);" []
29 _ <- run conn "REINDEX mew_parid;" []
30 return ()
31
32 ----------------------------------------------------------------
33
34 getByID :: Connection -> ID -> IO [Msg]
35 getByID conn mid = map toMsg <$> quickQuery' conn format params
36 where
37 format = "SELECT * FROM mew WHERE (id = ?);"
38 params = [toSql mid]
39
40 getByPaID :: Connection -> [ID] -> IO [Msg]
41 getByPaID conn ids = map toMsg <$> quickQuery' conn format params
42 where
43 fmt = replicate (length ids) "parid = ?"
44 fmt' = intersperse " OR " fmt
45 fmt'' = concat fmt'
46 format = "SELECT * FROM mew WHERE (" ++ fmt'' ++ ");"
47 params = map toSql ids
48
49 delByID :: Connection -> ID -> IO ()
50 delByID conn mid = () <$ run conn format params
51 where
52 format = "DELETE FROM mew WHERE (id = ?);"
53 params = [toSql mid]
54
55 addByID :: Connection -> ID -> String -> IO ()
56 addByID conn mid dat = () <$ run conn format params
57 where
58 format = "INSERT INTO mew VALUES (?, ?, ?, ?);"
59 params = [toSql mid, toSql "", toSql "", toSql dat]
60
61 ----------------------------------------------------------------
62
63 idForModtime :: String
64 idForModtime = "<mew-ctime>"
65
66 dbModtime :: Connection -> IO (Maybe Integer)
67 dbModtime conn = do
68 msgs <- getByID conn idForModtime
69 if null msgs
70 then return Nothing
71 else return $ toI msgs
72 where
73 toI = Just . read . date . head
74
75 setDBModtime :: Connection -> Integer -> IO ()
76 setDBModtime conn mt = do
77 let mts = show mt
78 mint <- dbModtime conn
79 case mint of
80 Nothing -> return ()
81 Just _ -> delByID conn idForModtime
82 addByID conn idForModtime mts
83
84 ----------------------------------------------------------------
85
86 prepareGet :: Connection -> IO Statement
87 prepareGet conn = prepare conn "SELECT * FROM mew WHERE (id = ?);"
88
89 prepareDel :: Connection -> IO Statement
90 prepareDel conn = prepare conn "DELETE FROM mew WHERE (id = ?) AND (path = ?);"
91
92 prepareAdd :: Connection -> IO Statement
93 prepareAdd conn = prepare conn "INSERT INTO mew VALUES (?, ?, ?, ?);"
94
95 getMsg :: Statement -> Msg -> IO [Msg]
96 getMsg stmt msg = map toMsg <$> (execute stmt params *> fetchAllRows' stmt)
97 where
98 params = [toSql (myid msg)]
99
100 delMsg :: Statement -> Msg -> IO ()
101 delMsg stmt msg = () <$ execute stmt params
102 where
103 params = [toSql (myid msg), toSql (path msg)]
104
105 addMsg :: Statement -> Msg -> IO ()
106 addMsg stmt msg = () <$ execute stmt params
107 where
108 params = fromMsg msg