2 Backend (getCurrent,getList,get,setCurrent,update)
5 ,Markup (Text, Paragraph, Link)
13 import Text.ParserCombinators.Parsec
16 --Keyword -> (Full text,date)
17 getCurrent :: a -> String -> IO (Maybe (String,String))
19 --Keyword -> [(id,date,author,comment)]
20 getList :: a -> String -> IO [(String, String, String, String)]
22 --Keyword -> id -> Full text
23 get :: a -> String -> String -> IO String
26 setCurrent :: a -> String -> String -> IO Bool
28 --Keyword -> Full text -> id
29 update :: a -> String -> String -> String -> String -> IO String
31 data Markup = Text String
35 type Document = [Markup]
37 wikiParser :: GenParser Char st Document
44 ss <- (wikiParser <|> return [])
56 pEol = char '\n' <|> do
62 l <- many1 $ noneOf ['|','[',']']
64 d <- many1 $ noneOf ['|','[',']']
69 l <- many1 $ noneOf ['|','[',']']
74 t <- many1 (noneOf ['\n','\r','[',']'])
77 newtype PGB = PGB DBService
79 createPGB :: String -> String -> String -> String -> IO PGB
80 createPGB host database user password = let db = createDBService host database "" user password Nothing in return (PGB db)
83 testDB = createPGB "wave" "wiki" "wiki" "12wiki34db"
85 instance Backend PGB where
87 getCurrent (PGB db) key = do
88 result <- selectReturnTuples db $ "SELECT fulltext,timestamp FROM curtexts WHERE keyword="++tov key
90 [text,date]:_ -> return (Just (text,date))
93 getList (PGB db) key = do
94 list <- selectReturnTuples db $ "SELECT id, timestamp, author, comment from fulltexts WHERE keyword = "++tov key
95 return $ map (\[id,date,author,comment] -> (id,date,author,comment)) list
97 get (PGB db) key id = return ""
99 setCurrent (PGB db) key id = do
100 full <- selectReturnTuples db $ "SELECT keyword FROM fulltexts WHERE keyword="++tov key++" AND id='"++id++"'"
101 cur <- selectReturnTuples db $ "SELECT keyword FROM curtexts WHERE keyword="++tov key
108 execute db $ "INSERT INTO current (keyword, id) VALUES ("++tov key++","++tov id++")"
110 execute db $ "UPDATE current SET id = "++tov id++" WHERE keyword = "++tov key
111 if rows == 1 then return True
114 update (PGB db) key text author comment = do
115 rows <- execute db $ "INSERT INTO fulltexts (keyword,fulltext, author, comment) VALUES ("++tov key++","++tov text++","++tov author++","++tov comment++")"
116 if rows == 0 then return ""
118 [[id]] <- selectReturnTuples db $ "SELECT currval('fulltexts_id_seq')"
119 setCurrent (PGB db) key id
123 tov :: String -> String
124 tov s = '\'':escapeQuery s++"'"