Internal.hs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. module Database.Persist.Redis.Internal
  2. ( toKey
  3. , unKey
  4. , mkEntity
  5. , toKeyId
  6. , toKeyText
  7. , toInsertFields
  8. , toB
  9. ) where
  10. import qualified Data.ByteString as B
  11. import qualified Data.ByteString.UTF8 as U
  12. import Data.Text (Text, unpack)
  13. import qualified Data.Text as T
  14. import Database.Persist.Class
  15. import Database.Persist.Types
  16. import Database.Persist.Redis.Parser
  17. toLabel :: FieldDef -> B.ByteString
  18. toLabel = U.fromString . unpack . unDBName . fieldDB
  19. toEntityString :: PersistEntity val => val -> Text
  20. toEntityString = unDBName . entityDB . entityDef . Just
  21. toEntityName :: EntityDef -> B.ByteString
  22. toEntityName = U.fromString . unpack . unDBName . entityDB
  23. mkEntity :: (Monad m, PersistEntity val) => Key val -> [(B.ByteString, B.ByteString)] -> m (Entity val)
  24. mkEntity key fields = do
  25. let values = redisToPerisistValues fields
  26. let v = fromPersistValues values
  27. case v of
  28. Right body -> return $ Entity key body
  29. Left a -> fail (unpack a)
  30. zipAndConvert :: PersistField t => [FieldDef] -> [t] -> [(B.ByteString, B.ByteString)]
  31. zipAndConvert [] _ = []
  32. zipAndConvert _ [] = []
  33. zipAndConvert (e:efields) (p:pfields) =
  34. let pv = toPersistValue p
  35. in
  36. if pv == PersistNull then zipAndConvert efields pfields
  37. else (toLabel e, toValue pv) : zipAndConvert efields pfields
  38. -- | Create a list for create/update in Redis store
  39. toInsertFields :: PersistEntity val => val -> [(B.ByteString, B.ByteString)]
  40. toInsertFields record = zipAndConvert entity fields
  41. where
  42. entity = entityFields $ entityDef $ Just record
  43. fields = toPersistFields record
  44. underscoreBs :: B.ByteString
  45. underscoreBs = U.fromString "_"
  46. -- | Make a key for given entity and id
  47. toKeyText :: PersistEntity val => val -> Integer -> Text
  48. toKeyText val k = toEntityString val `T.append` T.pack "_" `T.append` T.pack (show k)
  49. toB :: Text -> B.ByteString
  50. toB = U.fromString . unpack
  51. -- | Create a string key for given entity
  52. toObjectPrefix :: PersistEntity val => val -> B.ByteString
  53. toObjectPrefix val = B.append (toEntityName $ entityDef $ Just val) underscoreBs
  54. idBs :: B.ByteString
  55. idBs = U.fromString "id"
  56. -- | Construct an id key, that is incremented for access
  57. toKeyId :: PersistEntity val => val -> B.ByteString
  58. toKeyId val = B.append (toObjectPrefix val) idBs
  59. unKey :: (PersistEntity val) => Key val -> B.ByteString
  60. unKey = toValue . head . keyToValues
  61. toKey :: (Monad m, PersistEntity val) => Text -> m (Key val)
  62. toKey x = case q of
  63. Right z -> return z
  64. Left a -> fail (unpack a)
  65. where
  66. q = keyFromValues [PersistText x]