1{-# language DataKinds #-}
3module Rel8.Query.Exists
5 , present, with, withBy
6 , absent, without, withoutBy
11import Prelude hiding ( filter )
14import qualified Opaleye.Exists as Opaleye
15import qualified Opaleye.Operators as Opaleye
18import Rel8.Expr ( Expr )
19import Rel8.Expr.Opaleye ( fromColumn, fromPrimExpr )
20import Rel8.Query ( Query )
21import Rel8.Query.Filter ( filter )
22import Rel8.Query.Opaleye ( mapOpaleye )
23import Rel8.Table.Eq ( EqTable, (==:) )
26-- | Checks if a query returns at least one row.
27exists :: Query a -> Query (Expr Bool)
28exists = fmap (fromPrimExpr . fromColumn) . mapOpaleye Opaleye.exists
31inQuery :: EqTable a => a -> Query a -> Query (Expr Bool)
32inQuery a = exists . (>>= filter (a ==:))
35-- | Produce the empty query if the given query returns no rows. @present@
36-- is equivalent to @WHERE EXISTS@ in SQL.
37present :: Query a -> Query ()
38present = mapOpaleye Opaleye.restrictExists
41-- | Produce the empty query if the given query returns rows. @absent@
42-- is equivalent to @WHERE NOT EXISTS@ in SQL.
43absent :: Query a -> Query ()
44absent = mapOpaleye Opaleye.restrictNotExists
47-- | @with@ is similar to 'filter', but allows the predicate to be a full query.
49-- @with f a = a <$ present (f a)@, but this form matches 'filter'.
50with :: (a -> Query b) -> a -> Query a
51with f a = a <$ present (f a)
54-- | Like @with@, but with a custom membership test.
55withBy :: (a -> b -> Expr Bool) -> Query b -> a -> Query a
56withBy predicate bs = with $ \a -> bs >>= filter (predicate a)
59-- | Filter rows where @a -> Query b@ yields no rows.
60without :: (a -> Query b) -> a -> Query a
61without f a = a <$ absent (f a)
64-- | Like @without@, but with a custom membership test.
65withoutBy :: (a -> b -> Expr Bool) -> Query b -> a -> Query a
66withoutBy predicate bs = without $ \a -> bs >>= filter (predicate a)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list