optional :: Default Unpackspec a a=> SelectArr i aInput query
-> SelectArr i (MaybeFields a)The rows of the input query wrapped in "Just", unless the input query has no rows in which case a single row of "Nothing"
Convenient access to lateral left/right join
functionality. Performs a LATERAL LEFT JOIN under the hood and
has behaviour equivalent to the following Haskell function:
optional :: [a] -> [Maybe a]
optional q = case q of
[] -> [Nothing]
xs -> map Just xs
That is, if q :: returns no rows, SelectArr i aoptional q
:: returns exactly one "Nothing"
row. Otherwise, SelectArr i (MaybeFields a)optional q returns exactly the rows of q
wrapped in "Just". For example,
> let l1 = ["one", "two", "three"] :: [Field SqlText]
> runSelectI conn (optional (values l1))
[Just "one", Just "two", Just "three"]
> let l2 = [] :: [Field SqlText]
> runSelectI conn (optional (values l2))
[Nothing]
optionalRestrict is a special case of optional and could be
written in terms of optional as follows (except that
optionalRestrict doesn't use LATERAL under the hood and
optional does).
optionalRestrict q = optional $ proc cond -> do
a <- q -< ()
restrict -< cond a
returnA -< a