X-Git-Url: https://ruin.nu/git/?a=blobdiff_plain;ds=sidebyside;f=Wiki.hs;h=d6ece0dc5f4d94829dd09bfd86f657be3927f781;hb=a3c05f5d3f4be5f28a3eae7739f138a07fa263ad;hp=916829e11a872db3fd7b99d37fb5e68954bf844e;hpb=0bf2b690d5e8399175531005f9ca03df947cf878;p=yawbih.git diff --git a/Wiki.hs b/Wiki.hs index 916829e..d6ece0d 100644 --- a/Wiki.hs +++ b/Wiki.hs @@ -1,21 +1,82 @@ module Wiki ( + Backend (getCurrent,getList,get,setCurrent,update) + ,PGB + ,createPGB ) where -import Graphics.UI.WXCore.Db +import Dbconnect +import Data.Char -{-Keyword -> Full text -getCurrent :: String -> IO String +class Backend a where + --Keyword -> (Full text,date) + getCurrent :: a -> String -> IO (Maybe (String,String)) ---Keyword -> (id,date) -getList :: String -> IO (Int, String) + --Keyword -> [(id,date,author,comment)] + getList :: a -> String -> IO [(String, String, String, String)] ---Keyword -> id -> Full Text -get :: String -> Int -> IO String + --Keyword -> id -> Full text + get :: a -> String -> String -> IO String ---Keyword -> id -> () -setCurrent :: String -> Int -> IO () + --Keyword -> id -> () + setCurrent :: a -> String -> String -> IO Bool + + --Keyword -> Full text -> id + update :: a -> String -> String -> String -> String -> IO String + +data Markup = Text String + | Bold String + | Paragraph [Markup] + +type Document = [Markup] + +newtype PGB = PGB DBService + +createPGB :: String -> String -> String -> String -> IO PGB +createPGB host database user password = let db = createDBService host database "" user password Nothing in return (PGB db) + + +testDB = createPGB "wave" "wiki" "wiki" "12wiki34db" + +instance Backend PGB where + + getCurrent (PGB db) key = do + result <- selectReturnTuples db $ "SELECT fulltext,timestamp FROM curtexts WHERE keyword="++tov key + case result of + [text,date]:_ -> return (Just (text,date)) + _ -> return Nothing + + getList (PGB db) key = do + list <- selectReturnTuples db $ "SELECT id, timestamp, author, comment from fulltexts WHERE keyword = "++tov key + return $ map (\[id,date,author,comment] -> (id,date,author,comment)) list + + get (PGB db) key id = return "" + + setCurrent (PGB db) key id = do + full <- selectReturnTuples db $ "SELECT keyword FROM fulltexts WHERE keyword="++tov key++" AND id='"++id++"'" + cur <- selectReturnTuples db $ "SELECT keyword FROM curtexts WHERE keyword="++tov key + case full of + [[]] -> do + return False + _ -> do + rows <- case cur of + [[]] -> do + execute db $ "INSERT INTO current (keyword, id) VALUES ("++tov key++","++tov id++")" + _ -> do + execute db $ "UPDATE current SET id = "++tov id++" WHERE keyword = "++tov key + if rows == 1 then return True + else return False + + update (PGB db) key text author comment = do + rows <- execute db $ "INSERT INTO fulltexts (keyword,fulltext, author, comment) VALUES ("++tov key++","++tov text++","++tov author++","++tov comment++")" + if rows == 0 then return "" + else do + [[id]] <- selectReturnTuples db $ "SELECT currval('fulltexts_id_seq')" + setCurrent (PGB db) key id + return id + + +tov :: String -> String +tov s = '\'':escapeQuery s++"'" + ---Keyword -> Full text -> id -addNew :: String -> String -> IO Int --}