1{-# language DataKinds #-}
3module Rel8.Query.Exists
5 , whereExists, with, withBy
6 , whereNotExists, without, withoutBy
11import Prelude hiding ( filter )
14import qualified Opaleye.Operators as Opaleye
17import Rel8.Expr ( Expr )
18import Rel8.Query ( Query )
19import Rel8.Query.Filter ( filter )
20import Rel8.Query.Maybe ( optional )
21import Rel8.Query.Opaleye ( mapOpaleye )
22import Rel8.Table.Eq ( EqTable, (==:) )
23import Rel8.Table.Maybe ( isJustTable )
26-- | Checks if a query returns at least one row.
29-- mapM_ print =<< select c do
30-- author <- each authorSchema
31-- hasProjects <- exists do
32-- project <- each projectSchema
33-- where_ $ authorId author ==. projectAuthorId project
34-- return (authorName author, hasProjects)
37-- ("Bryan O'Sullivan",True)
38-- ("Emily Pillmore",False)
39exists :: Query a -> Query (Expr Bool)
40exists = fmap isJustTable . optional . whereExists
41-- FIXME: change this when b7aacc07c6392654cae439fc3b997620c3aa7a87 makes it
42-- into a release of Opaleye
45inQuery :: EqTable a => a -> Query a -> Query (Expr Bool)
46inQuery a = exists . (>>= filter (a ==:))
49-- | Produce the empty query if the given query returns no rows. @whereExists@
50-- is equivalent to @WHERE EXISTS@ in SQL.
54-- author <- each authorSchema
56-- project <- each projectSchema
57-- where_ $ projectAuthorId project ==. authorId author
58-- return $ authorName author
60-- ["Ollie","Bryan O'Sullivan"]
61whereExists :: Query a -> Query ()
62whereExists = mapOpaleye Opaleye.restrictExists
65-- | Produce the empty query if the given query returns rows. @whereNotExists@
66-- is equivalent to @WHERE NOT EXISTS@ in SQL.
70-- author <- each authorSchema
72-- project <- each projectSchema
73-- where_ $ projectAuthorId project ==. authorId author
74-- return $ authorName author
77whereNotExists :: Query a -> Query ()
78whereNotExists = mapOpaleye Opaleye.restrictNotExists
81with :: (a -> Query b) -> a -> Query a
82with f a = a <$ whereExists (f a)
85withBy :: (a -> b -> Expr Bool) -> Query b -> a -> Query a
86withBy predicate bs = with $ \a -> bs >>= filter (predicate a)
89without :: (a -> Query b) -> a -> Query a
90without f a = a <$ whereNotExists (f a)
93withoutBy :: (a -> b -> Expr Bool) -> Query b -> a -> Query a
94withoutBy 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