diff --git a/src/Opaleye/Operators.hs b/src/Opaleye/Operators.hs index 0cb74304..89dfd09a 100644 --- a/src/Opaleye/Operators.hs +++ b/src/Opaleye/Operators.hs @@ -58,6 +58,7 @@ module Opaleye.Operators , sqlLength -- * Containment operators , in_ + , inMany , inSelect -- * JSON operators , SqlIsJson @@ -289,14 +290,27 @@ sqlLength (Column e) = Column (HPQ.FunExpr "length" [e]) -- | 'in_' is designed to be used in prefix form. -- --- 'in_' @validProducts@ @product@ checks whether @product@ is a valid --- product. 'in_' @validProducts@ is a function which checks whether --- a product is a valid product. -in_ :: (Functor f, F.Foldable f) => f (Field a) -> Field a -> F.Field T.SqlBool +-- 'in_' @validUsers@ @user@ checks whether @user@ is a valid user. +-- 'in_' @validUsers@ is a function which checks whether a user is a +-- valid user. +in_ :: (F.Foldable f) => f (Field a) -> Field a -> F.Field T.SqlBool in_ fcas (Column a) = case NEL.nonEmpty (F.toList fcas) of Nothing -> T.sqlBool False Just xs -> Column $ HPQ.BinExpr HPQ.OpIn a (HPQ.ListExpr (fmap C.unColumn xs)) +-- | @inMany@ is a generalization of 'in_' to values with multiple +-- fields. It is designed to be used in prefix form. +-- +-- @inMany validUsers user@ checks whether @user@ is a valid user. +-- @inMany validUsers@ is a function which checks whether a user is a +-- valid user. +inMany :: + (F.Foldable f, D.Default O.EqPP fields fields) => + f fields -> + fields -> + F.Field T.SqlBool +inMany xs x = ors (fmap (.=== x) (F.toList xs)) + -- | True if the first argument occurs amongst the rows of the second, -- false otherwise. --