Skip to content

Commit

Permalink
Renamed Key
Browse files Browse the repository at this point in the history
  • Loading branch information
snoyberg committed Nov 27, 2012
1 parent 493f78b commit 7ce42e8
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 38 deletions.
3 changes: 2 additions & 1 deletion persistent/Database/Persist.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ module Database.Persist
, PersistStore (..)
, PersistUnique (..)
, PersistQuery (..)
, Key (..)
, KeyBackend (..)
, Key
, Entity (..)
, insertBy
, getJust
Expand Down
8 changes: 4 additions & 4 deletions persistent/Database/Persist/GenericSql.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module Database.Persist.GenericSql
, Statement
, runSqlConn
, runSqlPool
, Key (..)
, Key

-- * Raw SQL queries
-- $rawSql
Expand Down Expand Up @@ -69,7 +69,7 @@ import Control.Monad.Base (liftBase)

type ConnectionPool = Pool Connection

instance PathPiece (Key R.SqlBackend entity) where
instance PathPiece (KeyBackend R.SqlBackend entity) where
toPathPiece (Key (PersistInt64 i)) = toPathPiece i
toPathPiece k = throw $ PersistInvalidField $ "Invalid Key: " ++ show k
fromPathPiece t =
Expand Down Expand Up @@ -181,7 +181,7 @@ instance (C.MonadResource m, MonadLogger m) => PersistStore (SqlPersist m) where

insrepHelper :: (MonadIO m, PersistEntity val, MonadLogger m)
=> Text
-> Key R.SqlBackend val
-> Key val
-> val
-> SqlPersist m ()
insrepHelper command (Key k) val = do
Expand Down Expand Up @@ -249,7 +249,7 @@ instance (C.MonadResource m, MonadLogger m) => PersistUnique (SqlPersist m) wher
t = entityDef $ dummyFromUnique uniq
toFieldNames' = map snd . persistUniqueToFieldNames

dummyFromKey :: Key R.SqlBackend v -> v
dummyFromKey :: KeyBackend R.SqlBackend v -> v
dummyFromKey _ = error "dummyFromKey"

{- FIXME
Expand Down
2 changes: 1 addition & 1 deletion persistent/Database/Persist/Query/GenericSql.hs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ instance (MonadResource m, MonadLogger m) => PersistQuery (SqlPersist m) where
updatePersistValue :: Update v -> PersistValue
updatePersistValue (Update _ v _) = toPersistValue v

dummyFromKey :: Key R.SqlBackend v -> v
dummyFromKey :: KeyBackend R.SqlBackend v -> v
dummyFromKey _ = error "dummyFromKey"

execute' :: (MonadLogger m, MonadIO m) => Text -> [PersistValue] -> SqlPersist m ()
Expand Down
10 changes: 5 additions & 5 deletions persistent/Database/Persist/Query/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ instance Exception UpdateGetException
class PersistStore m => PersistQuery m where
-- | Update individual fields on a specific record.
update :: (PersistEntity val, PersistEntityBackend val ~ PersistMonadBackend m)
=> Key' val -> [Update val] -> m ()
=> Key val -> [Update val] -> m ()

-- | Update individual fields on a specific record, and retrieve the
-- updated value from the database.
--
-- Note that this function will throw an exception if the given key is not
-- found in the database.
updateGet :: (PersistEntity val, PersistMonadBackend m ~ PersistEntityBackend val)
=> Key' val -> [Update val] -> m val
=> Key val -> [Update val] -> m val
updateGet key ups = do
update key ups
get key >>= maybe (liftIO $ throwIO $ KeyNotFound $ show key) return
Expand Down Expand Up @@ -99,11 +99,11 @@ class PersistStore m => PersistQuery m where
selectFirst filts opts = selectSource filts ((LimitTo 1):opts) C.$$ CL.head


-- | Get the 'Key's of all records matching the given criterion.
-- | Get the 'Keys of all records matching the given criterion.

This comment has been minimized.

Copy link
@meteficha

meteficha Nov 27, 2012

Member

This seems incorrect =).

This comment has been minimized.

Copy link
@snoyberg

snoyberg Nov 27, 2012

Author Member

Heh, good catch. ff52ce7

selectKeys :: (PersistEntity val, PersistEntityBackend val ~ PersistMonadBackend m)
=> [Filter val]
-> [SelectOpt val]
-> C.Source m (Key' val)
-> C.Source m (Key val)

-- | The total number of records fulfilling the given criterion.
count :: (PersistEntity val, PersistEntityBackend val ~ PersistMonadBackend m)
Expand Down Expand Up @@ -164,7 +164,7 @@ selectList a b = selectSource a b C.$$ CL.consume
selectKeysList :: (PersistEntity val, PersistQuery m, PersistEntityBackend val ~ PersistMonadBackend m)
=> [Filter val]
-> [SelectOpt val]
-> m [Key' val]
-> m [Key val]
selectKeysList a b = selectKeys a b C.$$ CL.consume

data PersistUpdate = Assign | Add | Subtract | Multiply | Divide -- FIXME need something else here
Expand Down
8 changes: 4 additions & 4 deletions persistent/Database/Persist/Query/Join.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ data SelectOneMany one many = SelectOneMany
, somOrderOne :: [SelectOpt one]
, somFilterMany :: [Filter many]
, somOrderMany :: [SelectOpt many]
, somFilterKeys :: [Key' one] -> Filter many
, somGetKey :: many -> Key' one
, somFilterKeys :: [Key one] -> Filter many
, somGetKey :: many -> Key one
, somIncludeNoMatch :: Bool
}

selectOneMany :: ([Key' one] -> Filter many) -> (many -> Key' one) -> SelectOneMany one many
selectOneMany :: ([Key one] -> Filter many) -> (many -> Key one) -> SelectOneMany one many
selectOneMany filts get' = SelectOneMany [] [] [] [] filts get' False

instance ( PersistEntity one
, PersistEntity many
, Ord (Key (PersistEntityBackend one) one)
, Ord (Key one)
, PersistQuery monad
, PersistMonadBackend monad ~ PersistEntityBackend one
, PersistEntityBackend one ~ PersistEntityBackend many
Expand Down
2 changes: 1 addition & 1 deletion persistent/Database/Persist/Query/Join/Sql.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fromPersistValuesId _ = Left "fromPersistValuesId: invalid ID"
class RunJoin a where
runJoin :: (C.MonadResource m, MonadLogger m, MonadSqlPersist m) => a -> m (J.Result a)

instance (PersistEntity one, PersistEntity many, Eq (Key' one))
instance (PersistEntity one, PersistEntity many, Eq (Key one))
=> RunJoin (SelectOneMany one many) where
runJoin (SelectOneMany oneF oneO manyF manyO eq _getKey isOuter) = do
conn <- askSqlConn
Expand Down
44 changes: 22 additions & 22 deletions persistent/Database/Persist/Store.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ module Database.Persist.Store
, checkUnique
, DeleteCascade (..)
, PersistException (..)
, Key (..)
, KeyBackend (..)
, Key
, Entity (..)

-- * Helpers
, getPersistMap
, listToJSON
, mapToJSON
, Key'

-- * Config
, PersistConfig (..)
Expand Down Expand Up @@ -427,7 +427,7 @@ instance PersistField a => PersistField (Maybe a) where
-- | Helper wrapper, equivalent to @Key (PersistEntityBackend val) val@.
--
-- Since 1.1.0
type Key' val = Key (PersistEntityBackend val) val
type Key val = KeyBackend (PersistEntityBackend val) val

-- | 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.
Expand All @@ -450,7 +450,7 @@ class PersistEntity val where
persistUniqueToValues :: Unique val -> [PersistValue]
persistUniqueKeys :: val -> [Unique val]

persistIdField :: EntityField val (Key (PersistEntityBackend val) val)
persistIdField :: EntityField val (Key val)

instance PersistField a => PersistField [a] where
toPersistValue = PersistList . map toPersistValue
Expand Down Expand Up @@ -518,13 +518,13 @@ instance PersistField SomePersistField where
fromPersistValue x = fmap SomePersistField (fromPersistValue x :: Either T.Text T.Text)
sqlType (SomePersistField a) = sqlType a

newtype Key backend entity = Key { unKey :: PersistValue }
newtype KeyBackend backend entity = Key { unKey :: PersistValue }
deriving (Show, Read, Eq, Ord, PersistField)

instance A.ToJSON (Key backend entity) where
instance A.ToJSON (KeyBackend backend entity) where
toJSON (Key val) = A.toJSON val

instance A.FromJSON (Key backend entity) where
instance A.FromJSON (KeyBackend backend entity) where
parseJSON = fmap Key . A.parseJSON

-- | Datatype that represents an entity, with both its key and
Expand Down Expand Up @@ -558,7 +558,7 @@ instance A.FromJSON (Key backend entity) where
-- Entity backend b)@), then you must you use @SELECT ??, ??
-- WHERE ...@, and so on.
data Entity entity =
Entity { entityKey :: Key (PersistEntityBackend entity) entity
Entity { entityKey :: Key entity
, entityVal :: entity }
deriving (Eq, Ord, Show, Read)

Expand All @@ -568,33 +568,33 @@ class MonadIO m => PersistStore m where
-- | Create a new record in the database, returning an automatically created
-- key (in SQL an auto-increment id).
insert :: (PersistMonadBackend m ~ PersistEntityBackend val, PersistEntity val)
=> val -> m (Key' val)
=> val -> m (Key val)

-- | Create a new record in the database using the given key.
insertKey :: (PersistMonadBackend m ~ PersistEntityBackend val, PersistEntity val)
=> Key' val -> val -> m ()
=> Key val -> val -> m ()

-- | Put the record in the database with the given key.
-- Unlike 'replace', if a record with the given key does not
-- exist then a new record will be inserted.
repsert :: (PersistMonadBackend m ~ PersistEntityBackend val, PersistEntity val)
=> Key' val -> val -> m ()
=> Key val -> val -> m ()

-- | Replace the record in the database with the given
-- key. Note that the result is undefined if such record does
-- not exist, so you must use 'insertKey' or 'repsert' in
-- not exist, so you must use 'insertKey or 'repsert' in
-- these cases.
replace :: (PersistMonadBackend m ~ PersistEntityBackend val, PersistEntity val)
=> Key' val -> val -> m ()
=> Key val -> val -> m ()

-- | Delete a specific record by identifier. Does nothing if record does
-- not exist.
delete :: (PersistMonadBackend m ~ PersistEntityBackend val, PersistEntity val)
=> Key' val -> m ()
=> Key val -> m ()

-- | Get a record by identifier, if available.
get :: (PersistMonadBackend m ~ PersistEntityBackend val, PersistEntity val)
=> Key' val -> m (Maybe val)
=> Key val -> m (Maybe val)

#define DEF(T) { type PersistMonadBackend (T m) = PersistMonadBackend m; insert = lift . insert; insertKey k = lift . insertKey k; repsert k = lift . repsert k; replace k = lift . replace k; delete = lift . delete; get = lift . get }
#define GO(T) instance (PersistStore m) => PersistStore (T m) where DEF(T)
Expand Down Expand Up @@ -629,7 +629,7 @@ class PersistStore m => PersistUnique m where

-- | Like 'insert', but returns 'Nothing' when the record
-- couldn't be inserted because of a uniqueness constraint.
insertUnique :: (PersistEntityBackend val ~ PersistMonadBackend m, PersistEntity val) => val -> m (Maybe (Key' val))
insertUnique :: (PersistEntityBackend val ~ PersistMonadBackend m, PersistEntity val) => val -> m (Maybe (Key val))
insertUnique datum = do
isUnique <- checkUnique datum
if isUnique then Just `liftM` insert datum else return Nothing
Expand Down Expand Up @@ -660,9 +660,9 @@ GOX(Monoid w, Strict.WriterT w)

-- | Insert a value, checking for conflicts with any unique constraints. If a
-- duplicate exists in the database, it is returned as 'Left'. Otherwise, the
-- new 'Key' is returned as 'Right'.
-- new 'Key is returned as 'Right'.
insertBy :: (PersistEntity v, PersistStore m, PersistUnique m, PersistMonadBackend m ~ PersistEntityBackend v)
=> v -> m (Either (Entity v) (Key' v))
=> v -> m (Either (Entity v) (Key v))
insertBy val =
go $ persistUniqueKeys val
where
Expand Down Expand Up @@ -696,7 +696,7 @@ belongsTo ::
, PersistEntity ent1
, PersistEntity ent2
, PersistMonadBackend m ~ PersistEntityBackend ent2
) => (ent1 -> Maybe (Key' ent2)) -> ent1 -> m (Maybe ent2)
) => (ent1 -> Maybe (Key ent2)) -> ent1 -> m (Maybe ent2)
belongsTo foreignKeyField model = case foreignKeyField model of
Nothing -> return Nothing
Just f -> get f
Expand All @@ -707,12 +707,12 @@ belongsToJust ::
, PersistEntity ent1
, PersistEntity ent2
, PersistMonadBackend m ~ PersistEntityBackend ent2)
=> (ent1 -> Key' ent2) -> ent1 -> m ent2
=> (ent1 -> Key ent2) -> ent1 -> m ent2
belongsToJust getForeignKey model = getJust $ getForeignKey model

-- | Same as get, but for a non-null (not Maybe) foreign key
-- Unsafe unless your database is enforcing that the foreign key is valid
getJust :: (PersistStore m, PersistEntity val, Show (Key' val), PersistMonadBackend m ~ PersistEntityBackend val) => Key' val -> m val
getJust :: (PersistStore m, PersistEntity val, Show (Key val), PersistMonadBackend m ~ PersistEntityBackend val) => Key val -> m val
getJust key = get key >>= maybe
(liftIO $ E.throwIO $ PersistForeignConstraintUnmet $ show key)
return
Expand All @@ -739,7 +739,7 @@ data PersistFilter = Eq | Ne | Gt | Lt | Ge | Le | In | NotIn
deriving (Read, Show)

class PersistEntity a => DeleteCascade a where
deleteCascade :: (PersistStore m, PersistEntityBackend a ~ PersistMonadBackend m) => Key' a -> m ()
deleteCascade :: (PersistStore m, PersistEntityBackend a ~ PersistMonadBackend m) => Key a -> m ()

instance PersistField PersistValue where
toPersistValue = id
Expand Down

0 comments on commit 7ce42e8

Please sign in to comment.