PersistentTestModels.hs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. {-# LANGUAGE ExistentialQuantification #-}
  2. {-# LANGUAGE FlexibleContexts #-}
  3. {-# LANGUAGE FlexibleInstances #-}
  4. {-# LANGUAGE GeneralizedNewtypeDeriving #-}
  5. {-# LANGUAGE MultiParamTypeClasses #-}
  6. {-# LANGUAGE OverloadedStrings #-}
  7. {-# LANGUAGE QuasiQuotes #-}
  8. {-# LANGUAGE StandaloneDeriving #-}
  9. {-# LANGUAGE TemplateHaskell #-}
  10. {-# LANGUAGE TypeFamilies #-}
  11. {-# LANGUAGE UndecidableInstances #-} -- FIXME
  12. module PersistentTestModels where
  13. import Control.Monad.Reader
  14. import Data.Aeson
  15. import Data.Text (Text)
  16. import Database.Persist.Sql
  17. import Database.Persist.TH
  18. import PersistTestPetType
  19. import PersistTestPetCollarType
  20. share
  21. [ mkPersist sqlSettings { mpsGeneric = True }
  22. , mkMigrate "testMigrate"
  23. ] [persistUpperCase|
  24. -- Dedented comment
  25. -- Header-level comment
  26. -- Indented comment
  27. Person json
  28. name Text
  29. age Int "some ignored -- \" attribute"
  30. color Text Maybe -- this is a comment sql=foobarbaz
  31. PersonNameKey name -- this is a comment sql=foobarbaz
  32. deriving Show Eq
  33. Person1
  34. -- Dedented comment
  35. -- Header-level comment
  36. -- Indented comment
  37. name Text
  38. age Int
  39. deriving Show Eq
  40. PersonMaybeAge
  41. name Text
  42. age Int Maybe
  43. PersonMay json
  44. name Text Maybe
  45. color Text Maybe
  46. deriving Show Eq
  47. Pet
  48. ownerId PersonId
  49. name Text
  50. -- deriving Show Eq
  51. -- Dedented comment
  52. -- Header-level comment
  53. -- Indented comment
  54. type PetType
  55. MaybeOwnedPet
  56. ownerId PersonId Maybe
  57. name Text
  58. type PetType
  59. -- Dedented comment
  60. -- Header-level comment
  61. -- Indented comment
  62. NeedsPet
  63. petKey PetId
  64. OutdoorPet
  65. ownerId PersonId
  66. collar PetCollar
  67. type PetType
  68. -- From the scaffold
  69. UserPT
  70. ident Text
  71. password Text Maybe
  72. UniqueUserPT ident
  73. EmailPT
  74. email Text
  75. user UserPTId Maybe
  76. verkey Text Maybe
  77. UniqueEmailPT email
  78. Upsert
  79. email Text
  80. attr Text
  81. extra Text
  82. age Int
  83. UniqueUpsert email
  84. deriving Eq Show
  85. UpsertBy
  86. email Text
  87. city Text
  88. attr Text
  89. UniqueUpsertBy email
  90. UniqueUpsertByCity city
  91. deriving Eq Show
  92. Strict
  93. !yes Int
  94. ~no Int
  95. def Int
  96. |]
  97. deriving instance Show (BackendKey backend) => Show (PetGeneric backend)
  98. deriving instance Eq (BackendKey backend) => Eq (PetGeneric backend)
  99. share [ mkPersist sqlSettings { mpsPrefixFields = False, mpsGeneric = True }
  100. , mkMigrate "noPrefixMigrate"
  101. ] [persistLowerCase|
  102. NoPrefix1
  103. someFieldName Int
  104. NoPrefix2
  105. someOtherFieldName Int
  106. unprefixedRef NoPrefix1Id
  107. +NoPrefixSum
  108. unprefixedLeft Int
  109. unprefixedRight String
  110. deriving Show Eq
  111. |]
  112. deriving instance Show (BackendKey backend) => Show (NoPrefix1Generic backend)
  113. deriving instance Eq (BackendKey backend) => Eq (NoPrefix1Generic backend)
  114. deriving instance Show (BackendKey backend) => Show (NoPrefix2Generic backend)
  115. deriving instance Eq (BackendKey backend) => Eq (NoPrefix2Generic backend)
  116. -- | Reverses the order of the fields of an entity. Used to test
  117. -- @??@ placeholders of 'rawSql'.
  118. newtype ReverseFieldOrder a = RFO {unRFO :: a} deriving (Eq, Show)
  119. instance ToJSON (Key (ReverseFieldOrder a)) where toJSON = error "ReverseFieldOrder"
  120. instance FromJSON (Key (ReverseFieldOrder a)) where parseJSON = error "ReverseFieldOrder"
  121. instance (PersistEntity a) => PersistEntity (ReverseFieldOrder a) where
  122. type PersistEntityBackend (ReverseFieldOrder a) = PersistEntityBackend a
  123. newtype Key (ReverseFieldOrder a) = RFOKey { unRFOKey :: BackendKey SqlBackend } deriving (Show, Read, Eq, Ord, PersistField, PersistFieldSql)
  124. keyFromValues = fmap RFOKey . fromPersistValue . head
  125. keyToValues = (:[]) . toPersistValue . unRFOKey
  126. entityDef = revFields . entityDef . liftM unRFO
  127. where
  128. revFields ed = ed { entityFields = reverse (entityFields ed) }
  129. toPersistFields = reverse . toPersistFields . unRFO
  130. newtype EntityField (ReverseFieldOrder a) b = EFRFO {unEFRFO :: EntityField a b}
  131. persistFieldDef = persistFieldDef . unEFRFO
  132. fromPersistValues = fmap RFO . fromPersistValues . reverse
  133. newtype Unique (ReverseFieldOrder a) = URFO {unURFO :: Unique a }
  134. persistUniqueToFieldNames = reverse . persistUniqueToFieldNames . unURFO
  135. persistUniqueToValues = reverse . persistUniqueToValues . unURFO
  136. persistUniqueKeys = map URFO . reverse . persistUniqueKeys . unRFO
  137. persistIdField = error "ReverseFieldOrder.persistIdField"
  138. fieldLens = error "ReverseFieldOrder.fieldLens"
  139. cleanDB
  140. :: (MonadIO m, PersistQuery backend, PersistStoreWrite (BaseBackend backend))
  141. => ReaderT backend m ()
  142. cleanDB = do
  143. deleteWhere ([] :: [Filter (PersonGeneric backend)])
  144. deleteWhere ([] :: [Filter (Person1Generic backend)])
  145. deleteWhere ([] :: [Filter (PetGeneric backend)])
  146. deleteWhere ([] :: [Filter (MaybeOwnedPetGeneric backend)])
  147. deleteWhere ([] :: [Filter (NeedsPetGeneric backend)])
  148. deleteWhere ([] :: [Filter (OutdoorPetGeneric backend)])
  149. deleteWhere ([] :: [Filter (UserPTGeneric backend)])
  150. deleteWhere ([] :: [Filter (EmailPTGeneric backend)])