11 --Keyword -> (Full text,date)
12 getCurrent :: a -> String -> IO (Maybe (String,String))
14 --Keyword -> [(id,date,author,comment)]
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 data Markup = Text String
30 type Document = [Markup]
32 newtype PGB = PGB DBService
34 createPGB :: String -> String -> String -> String -> IO PGB
35 createPGB host database user password = let db = createDBService host database "" user password Nothing in return (PGB db)
38 testDB = createPGB "wave" "wiki" "wiki" "12wiki34db"
40 instance Backend PGB where
42 getCurrent (PGB db) key = do
43 result <- selectReturnTuples db $ "SELECT fulltext,timestamp FROM curtexts WHERE keyword="++tov key
45 [text,date]:_ -> return (Just (text,date))
48 getList (PGB db) key = do
49 list <- selectReturnTuples db $ "SELECT id, timestamp, author, comment from fulltexts WHERE keyword = "++tov key
50 return $ map (\[id,date,author,comment] -> (id,date,author,comment)) list
52 get (PGB db) key id = return ""
54 setCurrent (PGB db) key id = do
55 full <- selectReturnTuples db $ "SELECT keyword FROM fulltexts WHERE keyword="++tov key++" AND id='"++id++"'"
56 cur <- selectReturnTuples db $ "SELECT keyword FROM curtexts WHERE keyword='"++tov key++"'"
59 print "No text found with this id and key"
64 print "No link found in current, inserting"
65 execute db $ "INSERT INTO current (keyword, id) VALUES ("++tov id++","++tov key++")"
67 print "Link found in current, updating"
68 execute db $ "UPDATE current SET id = "++tov id++" WHERE keyword = "++tov key
70 if rows == 1 then return True
73 update (PGB db) key text author comment = do
74 rows <- execute db $ "INSERT INTO fulltexts (keyword,fulltext, author, comment) VALUES ("++tov key++","++tov text++","++tov author++","++tov comment++")"
75 if rows == 0 then return ""
77 [[id]] <- selectReturnTuples db $ "SELECT currval('fulltexts_id_seq')"
78 setCurrent (PGB db) key id
82 tov :: String -> String
83 tov s = '\'':escapeQuery s++"'"