persistent 1.2.2.0 → 1.2.3.0
raw patch · 4 files changed
+38/−4 lines, 4 filesdep ~base
Dependency ranges changed: base
Files
- Database/Persist/Class/PersistEntity.hs +30/−2
- Database/Persist/Class/PersistUnique.hs +2/−1
- Database/Persist/Sql/Class.hs +5/−0
- persistent.cabal +1/−1
Database/Persist/Class/PersistEntity.hs view
@@ -15,8 +15,10 @@ import Database.Persist.Types.Base import Database.Persist.Class.PersistField import Data.Text (Text)+import qualified Data.Text as T import Data.Aeson (ToJSON (..), FromJSON (..), object, (.:), (.=), Value (Object)) import Control.Applicative ((<$>), (<*>))+import Data.Monoid (mappend) -- | A single database entity. For example, if writing a blog application, a -- blog entry would be an entry, containing fields such as title and content.@@ -74,10 +76,10 @@ -- Since 1.1.0 type Key val = KeyBackend (PersistEntityBackend val) val --- | Datatype that represents an entity, with both its key and+-- | Datatype that represents an entity, with both its 'Key' and -- its Haskell representation. ----- When using the an SQL-based backend (such as SQLite or+-- When using a SQL-based backend (such as SQLite or -- PostgreSQL), an 'Entity' may take any number of columns -- depending on how many fields it has. In order to reconstruct -- your entity on the Haskell side, @persistent@ needs all of@@ -119,3 +121,29 @@ <$> o .: "key" <*> o .: "value" parseJSON _ = fail "FromJSON Entity: not an object"++instance PersistField entity => PersistField (Entity entity) where+ toPersistValue (Entity k v) = case toPersistValue v of+ (PersistMap alist) -> PersistMap ((idField, toPersistValue k) : alist)+ _ -> error $ T.unpack $ errMsg "expected PersistMap"++ fromPersistValue (PersistMap alist) = case after of+ [] -> Left $ errMsg $ "did not find " `mappend` idField `mappend` " field"+ ("_id", k):afterRest ->+ case fromPersistValue (PersistMap (before ++ afterRest)) of+ Right record -> Right $ Entity (Key k) record+ Left err -> Left err+ _ -> Left $ errMsg $ "impossible id field: " `mappend` T.pack (show alist)+ where+ (before, after) = break ((== idField) . fst) alist++ fromPersistValue x = Left $+ errMsg "Expected PersistMap, received: " `mappend` T.pack (show x)++errMsg :: Text -> Text+errMsg = mappend "PersistField entity fromPersistValue: "++-- | Realistically this is only going to be used for MongoDB,+-- so lets use MongoDB conventions+idField :: Text+idField = "_id"
Database/Persist/Class/PersistUnique.hs view
@@ -5,6 +5,7 @@ , getByValue , insertBy , replaceUnique+ , checkUnique ) where import qualified Prelude@@ -107,7 +108,7 @@ Nothing -> replace key datumNew >> return Nothing (Just conflictingKey) -> return $ Just conflictingKey where- changedKeys = uniqueKeysOriginal \\ uniqueKeysNew+ changedKeys = uniqueKeysNew \\ uniqueKeysOriginal uniqueKeysOriginal = persistUniqueKeys original -- | Check whether there are any conflicts for unique keys with this entity and
Database/Persist/Sql/Class.hs view
@@ -312,3 +312,8 @@ _mn = return n `asTypeOf` a instance PersistFieldSql Rational where sqlType _ = SqlNumeric 22 12 -- FIXME: Ambigous, 12 is from Pico which is used to convert Rational to number string++-- perhaps a SQL user can figure this sqlType out?+-- It is really intended for MongoDB though.+instance PersistField entity => PersistFieldSql (Entity entity) where+ sqlType _ = SqlOther "embedded entity, hard to type"
persistent.cabal view
@@ -1,5 +1,5 @@ name: persistent-version: 1.2.2.0+version: 1.2.3.0 license: MIT license-file: LICENSE author: Michael Snoyman <michael@snoyman.com>