]> ruin.nu Git - yawbih.git/commitdiff
Backend mostly done
authorMichael Andreen <harv@ruin.nu>
Thu, 9 Dec 2004 08:55:57 +0000 (08:55 +0000)
committerMichael Andreen <harv@ruin.nu>
Thu, 9 Dec 2004 08:55:57 +0000 (08:55 +0000)
Wiki.hs

diff --git a/Wiki.hs b/Wiki.hs
index d7ce7bcd912305223137ad8a72f141f255cfb165..bcfd23694a025b65363372dbb3f4f1ac0fa48978 100644 (file)
--- a/Wiki.hs
+++ b/Wiki.hs
@@ -1,5 +1,5 @@
 module Wiki (
 module Wiki (
-       WB
+       Backend
        ,PGB
 
 ) where
        ,PGB
 
 ) where
@@ -7,9 +7,9 @@ module Wiki (
 import Dbconnect
 import Data.Char
 
 import Dbconnect
 import Data.Char
 
-class WB a where
+class Backend a where
        --Keyword -> (Full text,date)
        --Keyword -> (Full text,date)
-       getCurrent :: a -> String -> IO (String,String)
+       getCurrent :: a -> String -> IO (Maybe (String,String))
 
        --Keyword -> [(id,date)]
        getList :: a -> String -> IO [(String, String, String, String)]
 
        --Keyword -> [(id,date)]
        getList :: a -> String -> IO [(String, String, String, String)]
@@ -18,12 +18,12 @@ class WB a where
        get :: a -> String -> String -> IO String
 
        --Keyword -> id -> ()
        get :: a -> String -> String -> IO String
 
        --Keyword -> id -> ()
-       setCurrent :: a -> String -> String -> IO Int
+       setCurrent :: a -> String -> String -> IO Bool
 
        --Keyword -> Full text -> id
 
        --Keyword -> Full text -> id
-       addNew :: a -> String -> String -> IO String
+       update :: a -> String -> String -> String -> String -> IO String
 
 
-data PGB = PGB DBService
+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)
 
 createPGB :: String -> String -> String -> String -> IO PGB
 createPGB host database user password = let db = createDBService host database "" user password Nothing in return (PGB db)
@@ -31,20 +31,49 @@ createPGB host database user password = let db = createDBService host database "
 
 testDB = createPGB "wave" "wiki" "wiki" "12wiki34db"
 
 
 testDB = createPGB "wave" "wiki" "wiki" "12wiki34db"
 
-instance WB PGB where
+instance Backend PGB where
 
        getCurrent (PGB db) key = do 
 
        getCurrent (PGB db) key = do 
-               [[text,date]] <- selectReturnTuples db $ "SELECT fulltext,timestamp FROM curtexts where keyword='"++(escapeQuery key)++"'"
-               return (text,date)
+               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
 
        getList (PGB db) key = do
-               list <- selectReturnTuples db $ "SELECT id, timestamp, author, comment from fulltexts where keyword = '"++(escapeQuery key)++"'"
+               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 ""
 
                return $ map (\[id,date,author,comment] -> (id,date,author,comment)) list
 
        get (PGB db) key id = return ""
 
-       setCurrent (PGB db) key id = 
-               execute db $ "UPDATE current SET id = '"++(escapeQuery id)++"' where keyword = '"++(escapeQuery key)++"'"
-
-       addNew (PGB db) key text = 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
+                               print "No text found with this id and key"
+                               return False
+                       _  -> do
+                               rows <- case cur of
+                                       [] -> do
+                                               print "No link found in current, inserting"
+                                               execute db $ "INSERT INTO current (keyword, id) VALUES ("++tov id++","++tov key++")"
+                                       _  -> do
+                                               print "Link found in current, updating"
+                                               execute db $ "UPDATE current SET id = "++tov id++" WHERE keyword = "++tov key
+                               print rows
+                               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++"'"
+