11 --Keyword -> (Full text,date)
12 getCurrent :: a -> String -> IO (Maybe (String,String))
14 --Keyword -> [(id,date)]
15 getList :: a -> String -> IO [(String, String, String, String)]
17 --Keyword -> id -> Full text
18 get :: a -> String -> String -> IO String
21 setCurrent :: a -> String -> String -> IO Bool
23 --Keyword -> Full text -> id
24 update :: a -> String -> String -> String -> String -> IO String
26 newtype PGB = PGB DBService
28 createPGB :: String -> String -> String -> String -> IO PGB
29 createPGB host database user password = let db = createDBService host database "" user password Nothing in return (PGB db)
32 testDB = createPGB "wave" "wiki" "wiki" "12wiki34db"
34 instance Backend PGB where
36 getCurrent (PGB db) key = do
37 result <- selectReturnTuples db $ "SELECT fulltext,timestamp FROM curtexts WHERE keyword="++tov key
39 [text,date]:_ -> return (Just (text,date))
42 getList (PGB db) key = do
43 list <- selectReturnTuples db $ "SELECT id, timestamp, author, comment from fulltexts WHERE keyword = "++tov key
44 return $ map (\[id,date,author,comment] -> (id,date,author,comment)) list
46 get (PGB db) key id = return ""
48 setCurrent (PGB db) key id = do
49 full <- selectReturnTuples db $ "SELECT keyword FROM fulltexts WHERE keyword="++tov key++" AND id='"++id++"'"
50 cur <- selectReturnTuples db $ "SELECT keyword FROM curtexts WHERE keyword='"++tov key++"'"
53 print "No text found with this id and key"
58 print "No link found in current, inserting"
59 execute db $ "INSERT INTO current (keyword, id) VALUES ("++tov id++","++tov key++")"
61 print "Link found in current, updating"
62 execute db $ "UPDATE current SET id = "++tov id++" WHERE keyword = "++tov key
64 if rows == 1 then return True
67 update (PGB db) key text author comment = do
68 rows <- execute db $ "INSERT INTO fulltexts (keyword,fulltext, author, comment) VALUES ("++tov key++","++tov text++","++tov author++","++tov comment++")"
69 if rows == 0 then return ""
71 [[id]] <- selectReturnTuples db $ "SELECT currval('fulltexts_id_seq')"
72 setCurrent (PGB db) key id
76 tov :: String -> String
77 tov s = '\'':escapeQuery s++"'"