test1.hs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. {-# LANGUAGE TypeFamilies #-}
  2. {-# LANGUAGE QuasiQuotes #-}
  3. {-# LANGUAGE GeneralizedNewtypeDeriving #-}
  4. import Prelude hiding (filter)
  5. import Database.Persist.Sqlite
  6. import Control.Monad.IO.Class
  7. mkPersist [$persist|
  8. Person sql=PersonTable
  9. name String update Eq Ne Desc In
  10. age Int update "Asc" Lt "some ignored attribute"
  11. color String null Eq Ne sql=mycolorfield NotIn Ge
  12. PersonNameKey name
  13. Pet
  14. owner PersonId
  15. name String
  16. Null
  17. field Int null Eq Ne Gt NotIn In
  18. Table
  19. table String
  20. |]
  21. main :: IO ()
  22. main = withSqlitePool "test.db3" 1 $ runSqlPool go
  23. go :: SqlPersist IO ()
  24. go = do
  25. runMigration $ do
  26. migrate (undefined :: Person)
  27. migrate (undefined :: Pet)
  28. migrate (undefined :: Null)
  29. migrate (undefined :: Table)
  30. deleteWhere ([] :: [Filter Person])
  31. pid <- insert $ Person "Michael" 25 Nothing
  32. liftIO $ print pid
  33. p1 <- get pid
  34. liftIO $ print p1
  35. replace pid $ Person "Michael" 26 Nothing
  36. p2 <- get pid
  37. liftIO $ print p2
  38. p3 <- selectList [PersonNameEq "Michael"] [] 0 0
  39. liftIO $ print p3
  40. _ <- insert $ Person "Michael2" 27 Nothing
  41. deleteWhere [PersonNameEq "Michael2"]
  42. p4 <- selectList [PersonAgeLt 28] [] 0 0
  43. liftIO $ print p4
  44. update pid [PersonAge 28]
  45. p5 <- get pid
  46. liftIO $ print p5
  47. updateWhere [PersonNameEq "Michael"] [PersonAge 29]
  48. p6 <- get pid
  49. if fmap personAge p6 /= Just 29 then error "bug 57" else return ()
  50. liftIO $ print p6
  51. _ <- insert $ Person "Eliezer" 2 $ Just "blue"
  52. p7 <- selectList [] [PersonAgeAsc] 0 0
  53. liftIO $ print p7
  54. _ <- insert $ Person "Abe" 30 $ Just "black"
  55. p8 <- selectList [PersonAgeLt 30] [PersonNameDesc] 0 0
  56. liftIO $ print p8
  57. {-
  58. insertR $ Person "Abe" 31 $ Just "brown"
  59. p9 <- select [PersonNameEq "Abe"] []
  60. liftIO $ print p9
  61. -}
  62. p10 <- getBy $ PersonNameKey "Michael"
  63. liftIO $ print p10
  64. p11 <- selectList [PersonColorEq $ Just "blue"] [] 0 0
  65. liftIO $ print p11
  66. p12 <- selectList [PersonColorEq Nothing] [] 0 0
  67. liftIO $ print p12
  68. p13 <- selectList [PersonColorNe Nothing] [] 0 0
  69. liftIO $ print p13
  70. p14 <- count [PersonColorNe Nothing]
  71. liftIO $ print p14
  72. delete pid
  73. plast <- get pid
  74. liftIO $ print plast
  75. _ <- insert $ Person "Gavriella" 0 Nothing
  76. x@(_, Person "Gavriella" 0 Nothing) <-
  77. insertBy $ Person "Gavriella" 1 $ Just "blue"
  78. liftIO $ print x
  79. False <- checkUnique $ Person "Gavriella" 2 Nothing
  80. True <- checkUnique $ Person "Gavriela (it's misspelled)" 2 Nothing
  81. return ()
  82. p15 <- selectList [PersonNameIn $ words "Michael Gavriella"] [] 0 0
  83. liftIO $ print p15
  84. _ <- insert $ Person "Miriam" 23 $ Just "red"
  85. p16 <- selectList [PersonColorNotIn [Nothing, Just "blue"]] [] 0 0
  86. liftIO $ print p16
  87. p17 <- selectList [PersonColorGe "blue"] [] 0 0
  88. liftIO $ print p17
  89. deleteWhere ([] :: [Filter Null])
  90. _ <- insert $ Null $ Just 5
  91. _ <- insert $ Null Nothing
  92. [(_, Null (Just 5))] <- selectList [NullFieldGt 4] [] 0 0
  93. [] <- selectList [NullFieldGt 5] [] 0 0
  94. [(_, Null (Just 5))] <- selectList [NullFieldEq $ Just 5] [] 0 0
  95. [(_, Null Nothing)] <- selectList [NullFieldNe $ Just 5] [] 0 0
  96. [(_, Null Nothing)] <- selectList [NullFieldEq Nothing] [] 0 0
  97. [(_, Null (Just 5))] <- selectList [NullFieldNe Nothing] [] 0 0
  98. return ()
  99. _ <- selectList ([] :: [Filter Person]) [] 0 0
  100. _ <- selectList ([] :: [Filter Person]) [] 10 0
  101. _ <- selectList ([] :: [Filter Person]) [] 10 10
  102. _ <- selectList ([] :: [Filter Person]) [] 0 10
  103. deleteWhere ([] :: [Filter Table])
  104. _ <- insert $ Table "foo"
  105. _ <- insert $ Table "bar"
  106. return ()