Skip to content

Commit

Permalink
rename 'b' type variable to 'backend'
Browse files Browse the repository at this point in the history
used sed carefully. all tests pass on all backends
  • Loading branch information
gregwebs committed Aug 1, 2012
1 parent 00f902b commit 274a9b0
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 60 deletions.
2 changes: 1 addition & 1 deletion persistent-mongoDB/Database/Persist/MongoDB.hs
Expand Up @@ -105,7 +105,7 @@ withMongoDBPool dbname hostname mauth connectionPoolSize connectionReader = do
pool <- createMongoDBPool dbname hostname mauth connectionPoolSize
connectionReader pool

runMongoDBConn :: (Trans.MonadIO m) => DB.AccessMode -> DB.Action m b -> ConnectionPool -> m b
runMongoDBConn :: (Trans.MonadIO m) => DB.AccessMode -> DB.Action m backend -> ConnectionPool -> m backend
runMongoDBConn accessMode action (pool, databaseName) = do
pipe <- Trans.liftIO $ DB.runIOE $ Pool.aResource pool
res <- DB.access pipe accessMode databaseName action
Expand Down
2 changes: 1 addition & 1 deletion persistent-test/DataTypeTest.hs
Expand Up @@ -49,7 +49,7 @@ DataTypeTable no-json
zonedTime ZonedTime
|]

cleanDB :: PersistQuery b m => b m ()
cleanDB :: PersistQuery backend m => backend m ()
cleanDB = do
deleteWhere ([] :: [Filter DataTypeTable])

Expand Down
2 changes: 1 addition & 1 deletion persistent-test/EmbedTest.hs
Expand Up @@ -58,7 +58,7 @@ share [mkPersist sqlSettings, mkMigrate "embedMigrate"] [persist|
deriving Show Eq Read Ord
|]
#ifdef WITH_MONGODB
cleanDB :: PersistQuery b m => b m ()
cleanDB :: PersistQuery backend m => backend m ()
cleanDB = do
deleteWhere ([] :: [Filter HasEmbed])
deleteWhere ([] :: [Filter HasEmbeds])
Expand Down
2 changes: 1 addition & 1 deletion persistent-test/HtmlTest.hs
Expand Up @@ -33,7 +33,7 @@ HtmlTable
deriving
|]

cleanDB :: PersistQuery b m => b m ()
cleanDB :: PersistQuery backend m => backend m ()
cleanDB = do
deleteWhere ([] :: [Filter HtmlTable])

Expand Down
2 changes: 1 addition & 1 deletion persistent-test/Init.hs
Expand Up @@ -113,7 +113,7 @@ persistSettings :: MkPersistSettings
persistSettings = MkPersistSettings { mpsBackend = ConT ''Action }

type BackendMonad = Action
runConn :: (MonadIO m, MonadBaseControl IO m) => Action m b -> m ()
runConn :: (MonadIO m, MonadBaseControl IO m) => Action m backend -> m ()
runConn f = do
-- withMongoDBConn ("test") "127.0.0.1" $ runMongoDBConn MongoDB.safe MongoDB.Master f
_<-withMongoDBConn "test" "127.0.0.1" Nothing $ runMongoDBConn MongoDB.master f
Expand Down
10 changes: 5 additions & 5 deletions persistent-test/JoinTest.hs
Expand Up @@ -44,7 +44,7 @@ share [mkPersist sqlSettings, mkMigrate "joinMigrate"] [persistUpperCase|
deriving Show Eq
|]
#ifdef WITH_MONGODB
cleanDB :: PersistQuery b m => b m ()
cleanDB :: PersistQuery backend m => backend m ()
cleanDB = do
deleteWhere ([] :: [Filter Author])
deleteWhere ([] :: [Filter Entry])
Expand All @@ -61,11 +61,11 @@ specs = describe "joins" $ do
#endif


joinGeneric :: (MonadIO (b m), PersistQuery b m) =>
(SelectOneMany b (AuthorGeneric b) (EntryGeneric b)
-> b m [(Entity (AuthorGeneric b), [Entity (EntryGeneric b)])])
joinGeneric :: (MonadIO (backend m), PersistQuery backend m) =>
(SelectOneMany backend (AuthorGeneric backend) (EntryGeneric backend)
-> backend m [(Entity (AuthorGeneric backend), [Entity (EntryGeneric backend)])])
-> Bool
-> b m ()
-> backend m ()

joinGeneric run _ = do
a <- insert $ Author "a"
Expand Down
2 changes: 1 addition & 1 deletion persistent-test/LargeNumberTest.hs
Expand Up @@ -27,7 +27,7 @@ share [mkPersist sqlSettings, mkMigrate "numberMigrate"] [persist|
deriving Show Eq
|]
#ifdef WITH_MONGODB
cleanDB :: PersistQuery b m => b m ()
cleanDB :: PersistQuery backend m => backend m ()
cleanDB = do
deleteWhere ([] :: [Filter Number])
db :: Action IO () -> Assertion
Expand Down
10 changes: 5 additions & 5 deletions persistent-test/PersistentTest.hs
Expand Up @@ -133,7 +133,7 @@ share [mkPersist sqlSettings, mkMigrate "testMigrate", mkDeleteCascade] [persis
verkey Text Maybe
UniqueEmail email
|]
cleanDB :: PersistQuery b m => b m ()
cleanDB :: PersistQuery backend m => backend m ()
cleanDB = do
deleteWhere ([] :: [Filter Person])
deleteWhere ([] :: [Filter Person1])
Expand All @@ -148,10 +148,10 @@ db :: Action IO () -> Assertion
db = db' cleanDB
#endif

petOwner :: PersistStore b m => PetGeneric b -> b m (PersonGeneric b)
petOwner :: PersistStore backend m => PetGeneric backend -> backend m (PersonGeneric backend)
petOwner = belongsToJust petOwnerId

maybeOwnedPetOwner :: PersistStore b m => MaybeOwnedPetGeneric b -> b m (Maybe (PersonGeneric b))
maybeOwnedPetOwner :: PersistStore backend m => MaybeOwnedPetGeneric backend -> backend m (Maybe (PersonGeneric backend))
maybeOwnedPetOwner = belongsTo maybeOwnedPetOwnerId


Expand Down Expand Up @@ -605,7 +605,7 @@ specs = describe "persistent" $ do
liftIO $ ret @?= [Single (2::Int)]

it "rawSql/entity" $ db $ do
let insert' :: (PersistStore b m, PersistEntity val) => val -> b m (Key b val, val)
let insert' :: (PersistStore backend m, PersistEntity val) => val -> backend m (Key backend val, val)
insert' v = insert v >>= \k -> return (k, v)
(p1k, p1) <- insert' $ Person "Mathias" 23 Nothing
(p2k, p2) <- insert' $ Person "Norbert" 44 Nothing
Expand Down Expand Up @@ -719,7 +719,7 @@ caseAfterException = withSqlitePool sqlite_database 1 $ runSqlPool $ do
#endif

-- Test proper polymorphism
_polymorphic :: PersistQuery b m => b m ()
_polymorphic :: PersistQuery backend m => backend m ()
_polymorphic = do
((Entity id' _):_) <- selectList [] [LimitTo 1]
_ <- selectList [PetOwnerId ==. id'] []
Expand Down
4 changes: 2 additions & 2 deletions persistent-test/persistent-test.cabal
Expand Up @@ -17,15 +17,15 @@ extra-source-files:
test/from-cabal

Flag mongodb
Description: test only MongoDB.
Description: test only MongoDB. default is to test just sqlite.
Default: False

Flag postgresql
Description: test postgresql. default is to test just sqlite.
Default: False

Flag mysql
Description: test MySQL
Description: test MySQL. default is to test just sqlite.
Default: False

library
Expand Down
4 changes: 2 additions & 2 deletions persistent/Database/Persist/GenericSql/Raw.hs
Expand Up @@ -40,10 +40,10 @@ newtype SqlPersist m a = SqlPersist { unSqlPersist :: ReaderT Connection m a }
instance MonadThrow m => MonadThrow (SqlPersist m) where
monadThrow = lift . monadThrow

instance MonadBase b m => MonadBase b (SqlPersist m) where
instance MonadBase backend m => MonadBase backend (SqlPersist m) where
liftBase = lift . liftBase

instance MonadBaseControl b m => MonadBaseControl b (SqlPersist m) where
instance MonadBaseControl backend m => MonadBaseControl backend (SqlPersist m) where
newtype StM (SqlPersist m) a = StMSP {unStMSP :: ComposeSt SqlPersist m a}
liftBaseWith = defaultLiftBaseWith StMSP
restoreM = defaultRestoreM unStMSP
Expand Down
30 changes: 15 additions & 15 deletions persistent/Database/Persist/Query/Internal.hs
Expand Up @@ -36,39 +36,39 @@ instance Show UpdateGetException where
show (KeyNotFound key) = "Key not found during updateGet: " ++ key
instance Exception UpdateGetException

class PersistStore b m => PersistQuery b m where
class PersistStore backend m => PersistQuery backend m where
-- | Update individual fields on a specific record.
update :: PersistEntity val => Key b val -> [Update val] -> b m ()
update :: PersistEntity val => Key backend val -> [Update val] -> backend 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 => Key b val -> [Update val] -> b m val
updateGet :: PersistEntity val => Key backend val -> [Update val] -> backend m val
updateGet key ups = do
update key ups
get key >>= maybe (liftBase $ throwIO $ KeyNotFound $ show key) return

-- | Update individual fields on any record matching the given criterion.
updateWhere :: PersistEntity val => [Filter val] -> [Update val] -> b m ()
updateWhere :: PersistEntity val => [Filter val] -> [Update val] -> backend m ()

-- | Delete all records matching the given criterion.
deleteWhere :: PersistEntity val => [Filter val] -> b m ()
deleteWhere :: PersistEntity val => [Filter val] -> backend m ()

-- | Get all records matching the given criterion in the specified order.
-- Returns also the identifiers.
selectSource
:: (PersistEntity val, PersistEntityBackend val ~ b)
:: (PersistEntity val, PersistEntityBackend val ~ backend)
=> [Filter val]
-> [SelectOpt val]
-> C.Source (C.ResourceT (b m)) (Entity val)
-> C.Source (C.ResourceT (backend m)) (Entity val)

-- | get just the first record for the criterion
selectFirst :: (PersistEntity val, PersistEntityBackend val ~ b)
selectFirst :: (PersistEntity val, PersistEntityBackend val ~ backend)
=> [Filter val]
-> [SelectOpt val]
-> b m (Maybe (Entity val))
-> backend m (Maybe (Entity val))
selectFirst filts opts = C.runResourceT
$ selectSource filts ((LimitTo 1):opts) C.$$ CL.head

Expand All @@ -77,10 +77,10 @@ class PersistStore b m => PersistQuery b m where
selectKeys :: PersistEntity val
=> [Filter val]
-> [SelectOpt val]
-> C.Source (C.ResourceT (b m)) (Key b val)
-> C.Source (C.ResourceT (backend m)) (Key backend val)

-- | The total number of records fulfilling the given criterion.
count :: PersistEntity val => [Filter val] -> b m Int
count :: PersistEntity val => [Filter val] -> backend m Int

-- | Filters which are available for 'select', 'updateWhere' and
-- 'deleteWhere'. Each filter constructor specifies the field being
Expand All @@ -101,10 +101,10 @@ data SelectOpt v = forall typ. Asc (EntityField v typ)
| LimitTo Int

-- | Call 'select' but return the result as a list.
selectList :: (PersistEntity val, PersistQuery b m, PersistEntityBackend val ~ b)
selectList :: (PersistEntity val, PersistQuery backend m, PersistEntityBackend val ~ backend)
=> [Filter val]
-> [SelectOpt val]
-> b m [Entity val]
-> backend m [Entity val]
selectList a b = C.runResourceT $ selectSource a b C.$$ CL.consume

data PersistUpdate = Assign | Add | Subtract | Multiply | Divide -- FIXME need something else here
Expand All @@ -127,7 +127,7 @@ limitOffsetOrder opts =
updateFieldDef :: PersistEntity v => Update v -> FieldDef
updateFieldDef (Update f _ _) = persistFieldDef f

deleteCascadeWhere :: (DeleteCascade a b m, PersistQuery b m)
=> [Filter a] -> b m ()
deleteCascadeWhere :: (DeleteCascade a backend m, PersistQuery backend m)
=> [Filter a] -> backend m ()
deleteCascadeWhere filts = do
C.runResourceT $ selectKeys filts [] C.$$ CL.mapM_ (lift . deleteCascade)
4 changes: 2 additions & 2 deletions persistent/Database/Persist/Query/Join.hs
Expand Up @@ -16,9 +16,9 @@ import Data.Maybe (mapMaybe)
import qualified Data.Map as Map
import Data.List (foldl')

class PersistQuery b m => RunJoin a b m where
class PersistQuery backend m => RunJoin a backend m where
type Result a
runJoin :: a -> b m (Result a)
runJoin :: a -> backend m (Result a)

data SelectOneMany backend one many = SelectOneMany
{ somFilterOne :: [Filter one]
Expand Down
46 changes: 23 additions & 23 deletions persistent/Database/Persist/Store.hs
Expand Up @@ -539,44 +539,44 @@ data Entity entity =
, entityVal :: entity }
deriving (Eq, Ord, Show, Read)

class (MonadBaseControl IO m, MonadBaseControl IO (b m)) => PersistStore b m where
class (MonadBaseControl IO m, MonadBaseControl IO (backend m)) => PersistStore backend m where

-- | Create a new record in the database, returning an automatically created
-- key (in SQL an auto-increment id).
insert :: PersistEntity val => val -> b m (Key b val)
insert :: PersistEntity val => val -> backend m (Key backend val)

-- | Create a new record in the database using the given key.
insertKey :: PersistEntity val => Key b val -> val -> b m ()
insertKey :: PersistEntity val => Key backend val -> val -> backend 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 :: PersistEntity val => Key b val -> val -> b m ()
repsert :: PersistEntity val => Key backend val -> val -> backend 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
-- these cases.
replace :: PersistEntity val => Key b val -> val -> b m ()
replace :: PersistEntity val => Key backend val -> val -> backend m ()

-- | Delete a specific record by identifier. Does nothing if record does
-- not exist.
delete :: PersistEntity val => Key b val -> b m ()
delete :: PersistEntity val => Key backend val -> backend m ()

-- | Get a record by identifier, if available.
get :: PersistEntity val => Key b val -> b m (Maybe val)
get :: PersistEntity val => Key backend val -> backend m (Maybe val)

class PersistStore b m => PersistUnique b m where
class PersistStore backend m => PersistUnique backend m where
-- | Get a record by unique key, if available. Returns also the identifier.
getBy :: (PersistEntityBackend val ~ b, PersistEntity val) => Unique val b -> b m (Maybe (Entity val))
getBy :: (PersistEntityBackend val ~ backend, PersistEntity val) => Unique val backend -> backend m (Maybe (Entity val))

-- | Delete a specific record by unique key. Does nothing if no record
-- matches.
deleteBy :: PersistEntity val => Unique val b -> b m ()
deleteBy :: PersistEntity val => Unique val backend -> backend m ()

-- | Like 'insert', but returns 'Nothing' when the record
-- couldn't be inserted because of a uniqueness constraint.
insertUnique :: (b ~ PersistEntityBackend val, PersistEntity val) => val -> b m (Maybe (Key b val))
insertUnique :: (backend ~ PersistEntityBackend val, PersistEntity val) => val -> backend m (Maybe (Key backend val))
insertUnique datum = do
isUnique <- checkUnique datum
if isUnique then Just `liftM` insert datum else return Nothing
Expand All @@ -586,8 +586,8 @@ class PersistStore b m => PersistUnique b m where
-- | 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'.
insertBy :: (PersistEntity v, PersistStore b m, PersistUnique b m, b ~ PersistEntityBackend v)
=> v -> b m (Either (Entity v) (Key b v))
insertBy :: (PersistEntity v, PersistStore backend m, PersistUnique backend m, backend ~ PersistEntityBackend v)
=> v -> backend m (Either (Entity v) (Key backend v))
insertBy val =
go $ persistUniqueKeys val
where
Expand All @@ -602,8 +602,8 @@ insertBy val =
-- of a 'Unique' value. Returns a value matching /one/ of the unique keys. This
-- function makes the most sense on entities with a single 'Unique'
-- constructor.
getByValue :: (PersistEntity v, PersistUnique b m, PersistEntityBackend v ~ b)
=> v -> b m (Maybe (Entity v))
getByValue :: (PersistEntity v, PersistUnique backend m, PersistEntityBackend v ~ backend)
=> v -> backend m (Maybe (Entity v))
getByValue val =
go $ persistUniqueKeys val
where
Expand All @@ -617,23 +617,23 @@ getByValue val =
-- | curry this to make a convenience function that loads an associated model
-- > foreign = belongsTo foeignId
belongsTo ::
(PersistStore b m
(PersistStore backend m
, PersistEntity ent1
, PersistEntity ent2) => (ent1 -> Maybe (Key b ent2)) -> ent1 -> b m (Maybe ent2)
, PersistEntity ent2) => (ent1 -> Maybe (Key backend ent2)) -> ent1 -> backend m (Maybe ent2)
belongsTo foreignKeyField model = case foreignKeyField model of
Nothing -> return Nothing
Just f -> get f

-- | same as belongsTo, but uses @getJust@ and therefore is similarly unsafe
belongsToJust ::
(PersistStore b m
(PersistStore backend m
, PersistEntity ent1
, PersistEntity ent2) => (ent1 -> Key b ent2) -> ent1 -> b m ent2
, PersistEntity ent2) => (ent1 -> Key backend ent2) -> ent1 -> backend 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 b m, PersistEntity val, Show (Key b val)) => Key b val -> b m val
getJust :: (PersistStore backend m, PersistEntity val, Show (Key backend val)) => Key backend val -> backend m val
getJust key = get key >>= maybe
(liftBase $ E.throwIO $ PersistForeignConstraintUnmet $ show key)
return
Expand All @@ -644,7 +644,7 @@ getJust key = get key >>= maybe
--
-- Returns 'True' if the entity would be unique, and could thus safely be
-- 'insert'ed; returns 'False' on a conflict.
checkUnique :: (PersistEntityBackend val ~ b, PersistEntity val, PersistUnique b m) => val -> b m Bool
checkUnique :: (PersistEntityBackend val ~ backend, PersistEntity val, PersistUnique backend m) => val -> backend m Bool
checkUnique val =
go $ persistUniqueKeys val
where
Expand All @@ -659,8 +659,8 @@ data PersistFilter = Eq | Ne | Gt | Lt | Ge | Le | In | NotIn
| BackendSpecificFilter T.Text
deriving (Read, Show)

class PersistEntity a => DeleteCascade a b m where
deleteCascade :: Key b a -> b m ()
class PersistEntity a => DeleteCascade a backend m where
deleteCascade :: Key backend a -> backend m ()

instance PersistField PersistValue where
toPersistValue = id
Expand Down

0 comments on commit 274a9b0

Please sign in to comment.