summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ws2015/ffp/blaetter/12/FFP_U12_Persist.hs82
1 files changed, 62 insertions, 20 deletions
diff --git a/ws2015/ffp/blaetter/12/FFP_U12_Persist.hs b/ws2015/ffp/blaetter/12/FFP_U12_Persist.hs
index c087fae..a9ff044 100644
--- a/ws2015/ffp/blaetter/12/FFP_U12_Persist.hs
+++ b/ws2015/ffp/blaetter/12/FFP_U12_Persist.hs
@@ -32,11 +32,15 @@ import Database.Persist
32import Database.Persist.Sqlite 32import Database.Persist.Sqlite
33import Database.Persist.TH 33import Database.Persist.TH
34import System.Environment (getArgs) 34import System.Environment (getArgs)
35import Control.Monad.Logger (NoLoggingT)
36import Control.Monad.Trans.Resource (ResourceT)
35 37
36import Control.Applicative 38import Control.Applicative
37import Control.Monad 39import Control.Monad
38import Control.Monad.IO.Class 40import Control.Monad.IO.Class
39 41
42import Text.Read (readMaybe)
43
40-- Nur für A11-2 benötigt: 44-- Nur für A11-2 benötigt:
41import Language.Haskell.TH 45import Language.Haskell.TH
42 46
@@ -70,31 +74,68 @@ import Language.Haskell.TH
70-- 74--
71-- Ein Demo folgt weiter unten. 75-- Ein Demo folgt weiter unten.
72 76
77share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase|
78Student
79 name String
80 matrikel Int
81 UniqueStudent name matrikel
82Abgabe
83 erstellerId StudentId
84 ubung Int
85 abgabe String
86|]
73 87
74sqliteDB = "demo.sqlite" 88sqliteDB = "demo.sqlite"
75 89
90type SQLite a = SqlPersistT (NoLoggingT (ResourceT IO)) a
91
76main :: IO () 92main :: IO ()
77main = do 93main = runSqlite sqliteDB $ do
78 putStrLn "Hallo zu HuniworX!" 94 liftIO $ putStrLn "Hallo zu HuniworX!"
79 args <- getArgs 95
80 -- !!! TODO !!! 96 runMigration migrateAll
81 return () 97
98 liftIO getArgs >>= action
99
100action :: [String] -> SQLite ()
101action [(readMaybe -> Just ubungsNr)] = mapM_ listAbgabe =<< selectList [AbgabeUbung ==. ubungsNr] []
102
103action [name] = mapM_ listAbgabenCount =<< selectList [StudentName ==. name] []
104
105action [name, (readMaybe -> Just matrikel), (readMaybe -> Just ubungsNr), abgabe] = do
106 ersteller <- getBy $ UniqueStudent name matrikel
107 erstellerId <- case ersteller of
108 Just (Entity id _) -> return id
109 Nothing -> insert $ Student name matrikel
110 insert $ Abgabe erstellerId ubungsNr abgabe
111 liftIO . putStrLn $ "Abgabe von " ++ name ++ " für Übung " ++ (show ubungsNr) ++ " angenommen"
112
113action _ = liftIO . putStr . unlines $ [ "Optionen sind:"
114 , " 1) Studentenname"
115 , " 2) Übungsnummer"
116 , " 3) Studentenname, Matrikel, Übungsnr, Abgabe"
117 , "Optionen 1 & 2 sind Abfragen; 3 ist Eingabe"
118 ]
119
120listAbgabe :: Entity Abgabe -> SQLite ()
121listAbgabe (Entity _ (Abgabe erstellerId ubung abgabe)) = do
122 (Just (Student name matrikel)) <- get $ erstellerId -- If the database is inconsistent we simply crash since I couldn't be bothered to fail more gracefully
123 liftIO . putStrLn $ "----\n" ++ name ++ " " ++ show matrikel ++ " für Übung " ++ show ubung
124 liftIO . mapM_ (putStrLn . ("\t" ++)) . lines $ abgabe
82 125
126listAbgabenCount :: Entity Student -> SQLite ()
127listAbgabenCount (Entity studentId (Student name matrikel)) = do
128 abgabenCount <- length <$> selectList [AbgabeErstellerId ==. studentId] []
129 liftIO . putStrLn $ name ++ " " ++ show matrikel ++ " Abgaben: " ++ show abgabenCount
83 130
84hilfstext :: IO ()
85hilfstext = do
86 putStrLn "Optionen sind:"
87 putStrLn " 1) Studentenname"
88 putStrLn " 2) Übungsnummer"
89 putStrLn " 3) Studentenname, Matrikel, Übungsnr, Abgabe"
90 putStrLn "Optionen 1 & 2 sind Abfragen; 3 ist Eingabe"
91 131
92maybeRead :: Read a => String -> Maybe a 132-- Just use Text.Read.readMaybe
93maybeRead (reads -> [(x,"")]) = Just x 133-- maybeRead :: Read a => String -> Maybe a
94maybeRead _ = Nothing 134-- maybeRead (reads -> [(x,"")]) = Just x
135-- maybeRead _ = Nothing
95 136
96maybeToInt :: String -> Maybe Int 137-- maybeToInt :: String -> Maybe Int
97maybeToInt = maybeRead 138-- maybeToInt = maybeRead
98 139
99 140
100{- DEMO: 141{- DEMO:
@@ -162,7 +203,7 @@ Optionen 1 & 2 sind Abfragen; 3 ist Eingabe
162-- (zum Ausführen in ghci die Option -XTemplateHaskell nicht vergessen) 203-- (zum Ausführen in ghci die Option -XTemplateHaskell nicht vergessen)
163-- 204--
164 205
165-- a) 206-- a) Extract ith member of n-tuple
166foobar :: Int -> Int -> ExpQ 207foobar :: Int -> Int -> ExpQ
167foobar n i = lamE [pat] rhs 208foobar n i = lamE [pat] rhs
168 where pat = tupP (map varP xs) 209 where pat = tupP (map varP xs)
@@ -170,14 +211,15 @@ foobar n i = lamE [pat] rhs
170 xs = [ mkName $ "x" ++ show j | j <- [1..n] ] 211 xs = [ mkName $ "x" ++ show j | j <- [1..n] ]
171 212
172 213
173-- b) 214-- b) Convert list of length ≥ n to n-tuple
174barfoo :: Int -> ExpQ 215barfoo :: Int -> ExpQ
175barfoo n = do 216barfoo n = do
176 ns <- replicateM n (newName "x") 217 ns <- replicateM n (newName "x")
177 lamE [foldr (\x y -> conP '(:) [varP x,y]) wildP ns] (tupE $ map varE ns) 218 lamE [foldr (\x y -> conP '(:) [varP x,y]) wildP ns] (tupE $ map varE ns)
178 219
179 220
180-- c) 221-- c) @$(fuubaz n m) f@ is a m-ary function on n-tuples returning an n-tuple whose ith component is the result of applying the m-ary function f to the ith components of the codomain
222-- It's the curried variant of transposition on tuples followed by pointwise application of an uncurried variant of f
181fuubaz:: Int -> Int -> ExpQ 223fuubaz:: Int -> Int -> ExpQ
182fuubaz n m = do 224fuubaz n m = do
183 let f = mkName "f" 225 let f = mkName "f"