Guest User

Untitled

a guest
Aug 20th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. Basic example of using HaskellDB to unmap records of a table
  2. CREATE TABLE books (
  3. id serial NOT NULL,
  4. title character varying NOT NULL,
  5.  
  6. PRIMARY KEY (id)
  7. );
  8.  
  9. data Book =
  10. { id :: Int
  11. , title :: String
  12. }
  13.  
  14. {-# LANGUAGE TemplateHaskell #-}
  15.  
  16. module Tables.Books (
  17. books
  18. , id
  19. , title
  20. , Books
  21. ) where
  22.  
  23. import Database.HaskellDB.CodeGen
  24. import Prelude hiding (id)
  25.  
  26. mkDBDirectTable "Books" [
  27. ("id", [t|Int|])
  28. , ("title", [t|String|])
  29. ]
  30.  
  31. allBooks db = query db $ do
  32. books <- table B.books
  33. return books
  34.  
  35. main :: IO ()
  36. main = do
  37. books <- postgresqlConnect [("host", "localhost"), ("user", "test"), ("password", "********")] allBooks
  38. mapM_ putStrLn (map (r -> r!B.title) books)
  39. return ()
  40.  
  41. main :: IO ()
  42. main = do
  43. books <- postgresqlConnect [("host", "localhost"), ("user", "test"), ("password", "********")] allBooks
  44. mapM_ putStrLn (map (r -> r!B.title) books)
  45. return ()
Add Comment
Please, Sign In to add comment