14import qualified Opaleye.Internal.MaybeFields as Opaleye
17import Rel8.Expr ( Col( E ), Expr )
18import Rel8.Expr.Eq ( (==.) )
19import Rel8.Expr.Opaleye ( fromColumn, fromPrimExpr )
20import Rel8.Query ( Query )
21import Rel8.Query.Filter ( where_ )
22import Rel8.Query.Opaleye ( mapOpaleye )
23import Rel8.Table.Maybe ( MaybeTable( MaybeTable ), isJustTable )
26-- | Convert a query that might return zero rows to a query that always returns
29-- To speak in more concrete terms, 'optional' is most useful to write @LEFT
31optional :: Query a -> Query (MaybeTable Expr a)
34 Opaleye.optionalInternal $
35 MaybeTable . E . fromPrimExpr . fromColumn
38-- | Filter out 'MaybeTable's, returning only the tables that are not-null.
40-- This operation can be used to "undo" the effect of 'optional', which
41-- operationally is like turning a @LEFT JOIN@ back into a full @JOIN@. You
42-- can think of this as analogous to 'Data.Maybe.catMaybes'.
43catMaybeTable :: MaybeTable Expr a -> Query a
44catMaybeTable ma@(MaybeTable _ a) = do
45 where_ $ isJustTable ma
49-- | Extend an optional query with another query. This is useful if you want
50-- to step through multiple @LEFT JOINs@.
52-- Note that @traverseMaybeTable@ takes a @a -> Query b@ function, which means
53-- you also have the ability to "expand" one row into multiple rows. If the
54-- @a -> Query b@ function returns no rows, then the resulting query will also
55-- have no rows. However, regardless of the given @a -> Query b@ function, if
56-- the input is @nothingTable@, you will always get exactly one @nothingTable@
58traverseMaybeTable :: (a -> Query b) -> MaybeTable Expr a -> Query (MaybeTable Expr b)
59traverseMaybeTable query ma@(MaybeTable (E input) _) = do
60 MaybeTable (E output) b <- optional (query =<< catMaybeTable ma)
61 where_ $ output ==. input
62 pure $ MaybeTable (E input) b
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