diff options
Diffstat (limited to 'ws2015/ffp/blaetter')
-rw-r--r-- | ws2015/ffp/blaetter/12/FFP_U12_Persist.hs | 82 |
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 | |||
32 | import Database.Persist.Sqlite | 32 | import Database.Persist.Sqlite |
33 | import Database.Persist.TH | 33 | import Database.Persist.TH |
34 | import System.Environment (getArgs) | 34 | import System.Environment (getArgs) |
35 | import Control.Monad.Logger (NoLoggingT) | ||
36 | import Control.Monad.Trans.Resource (ResourceT) | ||
35 | 37 | ||
36 | import Control.Applicative | 38 | import Control.Applicative |
37 | import Control.Monad | 39 | import Control.Monad |
38 | import Control.Monad.IO.Class | 40 | import Control.Monad.IO.Class |
39 | 41 | ||
42 | import Text.Read (readMaybe) | ||
43 | |||
40 | -- Nur für A11-2 benötigt: | 44 | -- Nur für A11-2 benötigt: |
41 | import Language.Haskell.TH | 45 | import 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 | ||
77 | share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase| | ||
78 | Student | ||
79 | name String | ||
80 | matrikel Int | ||
81 | UniqueStudent name matrikel | ||
82 | Abgabe | ||
83 | erstellerId StudentId | ||
84 | ubung Int | ||
85 | abgabe String | ||
86 | |] | ||
73 | 87 | ||
74 | sqliteDB = "demo.sqlite" | 88 | sqliteDB = "demo.sqlite" |
75 | 89 | ||
90 | type SQLite a = SqlPersistT (NoLoggingT (ResourceT IO)) a | ||
91 | |||
76 | main :: IO () | 92 | main :: IO () |
77 | main = do | 93 | main = 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 | |||
100 | action :: [String] -> SQLite () | ||
101 | action [(readMaybe -> Just ubungsNr)] = mapM_ listAbgabe =<< selectList [AbgabeUbung ==. ubungsNr] [] | ||
102 | |||
103 | action [name] = mapM_ listAbgabenCount =<< selectList [StudentName ==. name] [] | ||
104 | |||
105 | action [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 | |||
113 | action _ = 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 | |||
120 | listAbgabe :: Entity Abgabe -> SQLite () | ||
121 | listAbgabe (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 | ||
126 | listAbgabenCount :: Entity Student -> SQLite () | ||
127 | listAbgabenCount (Entity studentId (Student name matrikel)) = do | ||
128 | abgabenCount <- length <$> selectList [AbgabeErstellerId ==. studentId] [] | ||
129 | liftIO . putStrLn $ name ++ " " ++ show matrikel ++ " Abgaben: " ++ show abgabenCount | ||
83 | 130 | ||
84 | hilfstext :: IO () | ||
85 | hilfstext = 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 | ||
92 | maybeRead :: Read a => String -> Maybe a | 132 | -- Just use Text.Read.readMaybe |
93 | maybeRead (reads -> [(x,"")]) = Just x | 133 | -- maybeRead :: Read a => String -> Maybe a |
94 | maybeRead _ = Nothing | 134 | -- maybeRead (reads -> [(x,"")]) = Just x |
135 | -- maybeRead _ = Nothing | ||
95 | 136 | ||
96 | maybeToInt :: String -> Maybe Int | 137 | -- maybeToInt :: String -> Maybe Int |
97 | maybeToInt = 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 |
166 | foobar :: Int -> Int -> ExpQ | 207 | foobar :: Int -> Int -> ExpQ |
167 | foobar n i = lamE [pat] rhs | 208 | foobar 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 |
174 | barfoo :: Int -> ExpQ | 215 | barfoo :: Int -> ExpQ |
175 | barfoo n = do | 216 | barfoo 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 | ||
181 | fuubaz:: Int -> Int -> ExpQ | 223 | fuubaz:: Int -> Int -> ExpQ |
182 | fuubaz n m = do | 224 | fuubaz n m = do |
183 | let f = mkName "f" | 225 | let f = mkName "f" |