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
45 ss <- (wikiParser <|> return [])
58 pEol = char '\n' <|> do
64 l <- many1 $ noneOf ['|']
66 d <- many1 $ noneOf [']']
71 l <- many1 $ noneOf ['|','[',']']
75 c <- satisfy validChar
79 t <- many1 alphaNum--(noneOf ['\n','\r','[',']'])
103 newtype PGB = PGB DBService
105 createPGB :: String -> String -> String -> String -> IO PGB
106 createPGB host database user password = let db = createDBService host database "" user password Nothing in return (PGB db)
109 testDB = createPGB "wave" "wiki" "wiki" "12wiki34db"
111 instance Backend PGB where
113 getCurrent (PGB db) key = do
114 result <- selectReturnTuples db $ "SELECT fulltext,timestamp FROM curtexts WHERE keyword="++tov key
116 [text,date]:_ -> return (Just (text,date))
119 getList (PGB db) key = do
120 list <- selectReturnTuples db $ "SELECT id, timestamp, author, comment from fulltexts WHERE keyword = "++tov key
121 return $ map (\[id,date,author,comment] -> (id,date,author,comment)) list
123 get (PGB db) key id = return ""
125 setCurrent (PGB db) key id = do
126 full <- selectReturnTuples db $ "SELECT keyword FROM fulltexts WHERE keyword="++tov key++" AND id='"++id++"'"
127 cur <- selectReturnTuples db $ "SELECT keyword FROM curtexts WHERE keyword="++tov key
134 execute db $ "INSERT INTO current (keyword, id) VALUES ("++tov key++","++tov id++")"
136 execute db $ "UPDATE current SET id = "++tov id++" WHERE keyword = "++tov key
137 if rows == 1 then return True
140 update (PGB db) key text author comment = do
141 rows <- execute db $ "INSERT INTO fulltexts (keyword,fulltext, author, comment) VALUES ("++tov key++","++tov text++","++tov author++","++tov comment++")"
142 if rows == 0 then return ""
144 [[id]] <- selectReturnTuples db $ "SELECT currval('fulltexts_id_seq')"
145 setCurrent (PGB db) key id
149 tov :: String -> String
150 tov s = '\'':escapeQuery s++"'"