on 137ebed help sign in
rel8/src/Rel8/Query/List.hs
118 lines 4.2 kB history raw
1{-# language FlexibleContexts #-}
2{-# language GADTs #-}
3{-# language NamedFieldPuns #-}
5module Rel8.Query.List
6 ( many, some
7 , manyExpr, someExpr
8 , catListTable, catNonEmptyTable
9 , catList, catNonEmpty
10 )
11where
13-- base
14import Data.Functor.Identity ( runIdentity )
15import Data.List.NonEmpty ( NonEmpty )
16import Prelude
18-- opaleye
19import qualified Opaleye.Internal.HaskellDB.PrimQuery as Opaleye
21-- rel8
22import Rel8.Expr ( Col( E, unE ), Expr )
23import Rel8.Expr.Aggregate ( listAggExpr, nonEmptyAggExpr )
24import Rel8.Expr.Opaleye ( mapPrimExpr )
25import Rel8.Query ( Query )
26import Rel8.Query.Aggregate ( aggregate )
27import Rel8.Query.Maybe ( optional )
28import Rel8.Schema.HTable.Vectorize ( hunvectorize )
29import Rel8.Schema.Null ( Sql, Unnullify )
30import Rel8.Schema.Spec ( SSpec( SSpec, info ) )
31import Rel8.Table ( Table, fromColumns, toColumns )
32import Rel8.Table.Aggregate ( listAgg, nonEmptyAgg )
33import Rel8.Table.List ( ListTable( ListTable ) )
34import Rel8.Table.Maybe ( maybeTable )
35import Rel8.Table.NonEmpty ( NonEmptyTable( NonEmptyTable ) )
36import Rel8.Type ( DBType, typeInformation )
37import Rel8.Type.Array ( extractArrayElement )
38import Rel8.Type.Information ( TypeInformation )
41-- | Aggregate a 'Query' into a 'ListTable'. If the supplied query returns 0
42-- rows, this function will produce a 'Query' that returns one row containing
43-- the empty @ListTable@. If the supplied @Query@ does return rows, @many@ will
44-- return exactly one row, with a @ListTable@ collecting all returned rows.
45--
46-- @many@ is analogous to 'Control.Applicative.many' from
47-- @Control.Applicative@.
48many :: Table Expr a => Query a -> Query (ListTable a)
49many =
50 fmap (maybeTable mempty (\(ListTable a) -> ListTable a)) .
51 optional .
52 aggregate .
53 fmap (listAgg . toColumns)
56-- | Aggregate a 'Query' into a 'NonEmptyTable'. If the supplied query returns
57-- 0 rows, this function will produce a 'Query' that is empty - that is, will
58-- produce zero @NonEmptyTable@s. If the supplied @Query@ does return rows,
59-- @some@ will return exactly one row, with a @NonEmptyTable@ collecting all
60-- returned rows.
61--
62-- @some@ is analogous to 'Control.Applicative.some' from
63-- @Control.Applicative@.
64some :: Table Expr a => Query a -> Query (NonEmptyTable a)
65some =
66 fmap (\(NonEmptyTable a) -> NonEmptyTable a) .
67 aggregate .
68 fmap (nonEmptyAgg . toColumns)
71-- | A version of 'many' specialised to single expressions.
72manyExpr :: Sql DBType a => Query (Expr a) -> Query (Expr [a])
73manyExpr = fmap (maybeTable mempty id) . optional . aggregate . fmap listAggExpr
76-- | A version of 'many' specialised to single expressions.
77someExpr :: Sql DBType a => Query (Expr a) -> Query (Expr (NonEmpty a))
78someExpr = aggregate . fmap nonEmptyAggExpr
81-- | Expand a 'ListTable' into a 'Query', where each row in the query is an
82-- element of the given @ListTable@.
83--
84-- @catListTable@ is an inverse to 'many'.
85catListTable :: Table Expr a => ListTable a -> Query a
86catListTable (ListTable as) = pure $ fromColumns $ runIdentity $
87 hunvectorize (\SSpec {info} -> pure . E . sunnest info . unE) as
90-- | Expand a 'NonEmptyTable' into a 'Query', where each row in the query is an
91-- element of the given @NonEmptyTable@.
92--
93-- @catNonEmptyTable@ is an inverse to 'some'.
94catNonEmptyTable :: Table Expr a => NonEmptyTable a -> Query a
95catNonEmptyTable (NonEmptyTable as) = pure $ fromColumns $ runIdentity $
96 hunvectorize (\SSpec {info} -> pure . E . sunnest info . unE) as
99-- | Expand an expression that contains a list into a 'Query', where each row
100-- in the query is an element of the given list.
101--
102-- @catList@ is an inverse to 'manyExpr'.
103catList :: Sql DBType a => Expr [a] -> Query (Expr a)
104catList = pure . sunnest typeInformation
107-- | Expand an expression that contains a non-empty list into a 'Query', where
108-- each row in the query is an element of the given list.
109--
110-- @catNonEmpty@ is an inverse to 'someExpr'.
111catNonEmpty :: Sql DBType a => Expr (NonEmpty a) -> Query (Expr a)
112catNonEmpty = pure . sunnest typeInformation
115sunnest :: TypeInformation (Unnullify a) -> Expr (list a) -> Expr a
116sunnest info = mapPrimExpr $
117 extractArrayElement info .
118 Opaleye.UnExpr (Opaleye.UnOpOther "UNNEST")