Lseed.DB
authorJoachim Breitner <mail@joachim-breitner.de>
Sun, 17 May 2009 20:21:46 +0000 (22:21 +0200)
committerJoachim Breitner <mail@joachim-breitner.de>
Sun, 17 May 2009 20:21:46 +0000 (22:21 +0200)
db.conf-example [new file with mode: 0644]
src/Lseed/DB.hs [new file with mode: 0644]

diff --git a/db.conf-example b/db.conf-example
new file mode 100644 (file)
index 0000000..c7cc528
--- /dev/null
@@ -0,0 +1 @@
+Driver=MySQL;Server=svenhecht.is-a-geek.org;Database=lseed;Uid=lseedbackend;Pwd=XXX;Port=3306;
diff --git a/src/Lseed/DB.hs b/src/Lseed/DB.hs
new file mode 100644 (file)
index 0000000..1bddf7a
--- /dev/null
@@ -0,0 +1,40 @@
+module Lseed.DB 
+       ( 
+       getCodeToRun
+       )
+       where
+
+import Database.HDBC
+import Database.HDBC.ODBC
+import Data.Map((!))
+
+data DBCode = DBCode
+       { dbcUserName :: String
+       , dbcUserID :: Int
+       , dbcPlantName :: String
+       , dbcPlantID :: Int
+       , dbcCode :: String
+       }
+       deriving (Show)
+
+withLseedDB ::  (Connection -> IO t) -> IO t
+withLseedDB what = do
+       dn <- readFile "../db.conf"
+       conn <- connectODBC dn  
+       res <- what conn
+       disconnect conn
+       return res
+
+getCodeToRun ::  IO [DBCode]
+getCodeToRun = withLseedDB $ \conn -> do
+       let getCodeQuery = "SELECT plant.ID AS plantid, user.ID AS userid, code, plant.Name AS plantname, user.Name AS username from plant, user WHERE user.NextSeed = plant.ID;"
+       stmt <- prepare conn getCodeQuery
+       execute stmt []
+       result <- fetchAllRowsMap' stmt
+       return $ flip map result $ \m -> 
+               DBCode (fromSql (m ! "username"))
+                      (fromSql (m ! "userid"))
+                      (fromSql (m ! "plantname"))
+                      (fromSql (m ! "plantid"))
+                      (fromSql (m ! "code"))
+