ReaderT SqlBackend m [Entity User] selectSPJ = select" /> ReaderT SqlBackend m [Entity User] selectSPJ = select" /> ReaderT SqlBackend m [Entity User] selectSPJ = select"/>

How to filter on more than one field with tuple using "in" combinator (<-.)

143 Views Asked by At

Documentation explains how to use "in" combinator (<-.) for one field:

selectSPJ :: MonadIO m => ReaderT SqlBackend m [Entity User]
selectSPJ = selectList [UserAge <-. [40]] []

Is there a way to use it with more than one field? Like:

selectUsers :: MonadIO m => ReaderT SqlBackend m [Entity User]
selectUsers = selectList [(UserName, UserAge) <-. [("SPJ", 40), ("John Doe", 30]] []

Which would translate to:

select user.name, user.age from user 
where (name, age) in (values ('SPJ', 40), ('John Doe', 30));

Combinator' signature relies on PersistField:

(<-.) :: forall v typ. PersistField typ => EntityField v typ -> [typ] -> Filter v

But I can't see a way to sneak in a tuple:

data EntityField record :: Type -> Type

I managed to do it with "and" ([Filter record]) and "or" (||.) combinators

mkFilters namesAndAges = foldl' mkFilter [] namesAndAges
mkFilter filters (name, age) = [UserName ==. name, UserAge ==. age] ||. filters

But the resulting query is much slower:

select user.name, user.age from user 
where (name = 'SPJ' and age = 40) or (name = 'John Doe' and age = 30);
0

There are 0 best solutions below