module Database.Relational.Query.ProjectableExtended (
ProjectableFlattenMaybe (flatten),
flattenPiMaybe,
(!), (?!), (?!?), (!??),
(.!), (.?),
unsafeAggregateOp,
count,
sum', sumMaybe, avg, avgMaybe,
max', maxMaybe, min', minMaybe,
every, any', some',
ProjectableIdZip (leftId, rightId),
ProjectableRunIdsZip (runIds), flattenPh
) where
import Prelude hiding (pi)
import Data.Int (Int64)
import Data.Monoid ((<>))
import qualified Language.SQL.Keyword as SQL
import Database.Relational.Query.Context (Flat, Aggregated, OverWindow)
import Database.Relational.Query.Expr (Expr, fromJust)
import Database.Relational.Query.Projection (Projection)
import qualified Database.Relational.Query.Projection as Projection
import Database.Relational.Query.Projectable
(expr, PlaceHolders, unsafeUniOp,
ProjectableMaybe (flattenMaybe), ProjectableIdZip (leftId, rightId),
SqlProjectable)
import Database.Relational.Query.Pi (Pi)
class Projectable p0 p1 where
project :: p0 c a -> p1 c a
class AggregatedContext ac
instance AggregatedContext Aggregated
instance AggregatedContext OverWindow
unsafeAggregateOp :: (AggregatedContext ac, SqlProjectable (p ac))
=> SQL.Keyword -> Projection Flat a -> p ac b
unsafeAggregateOp op = unsafeUniOp ((op <>) . SQL.paren)
count :: (AggregatedContext ac, SqlProjectable (p ac))
=> Projection Flat a -> p ac Int64
count = unsafeAggregateOp SQL.COUNT
sumMaybe :: (Num a, AggregatedContext ac, SqlProjectable (p ac))
=> Projection Flat (Maybe a) -> p ac (Maybe a)
sumMaybe = unsafeAggregateOp SQL.SUM
sum' :: (Num a, AggregatedContext ac, SqlProjectable (p ac))
=> Projection Flat a -> p ac (Maybe a)
sum' = sumMaybe . Projection.just
avgMaybe :: (Num a, Fractional b, AggregatedContext ac, SqlProjectable (p ac))
=> Projection Flat (Maybe a) -> p ac (Maybe b)
avgMaybe = unsafeAggregateOp SQL.AVG
avg :: (Num a, Fractional b, AggregatedContext ac, SqlProjectable (p ac))
=> Projection Flat a -> p ac (Maybe b)
avg = avgMaybe . Projection.just
maxMaybe :: (Ord a, AggregatedContext ac, SqlProjectable (p ac))
=> Projection Flat (Maybe a) -> p ac (Maybe a)
maxMaybe = unsafeAggregateOp SQL.MAX
max' :: (Ord a, AggregatedContext ac, SqlProjectable (p ac))
=> Projection Flat a -> p ac (Maybe a)
max' = maxMaybe . Projection.just
minMaybe :: (Ord a, AggregatedContext ac, SqlProjectable (p ac))
=> Projection Flat (Maybe a) -> p ac (Maybe a)
minMaybe = unsafeAggregateOp SQL.MIN
min' :: (Ord a, AggregatedContext ac, SqlProjectable (p ac))
=> Projection Flat a -> p ac (Maybe a)
min' = minMaybe . Projection.just
every :: (AggregatedContext ac, SqlProjectable (p ac))
=> Projection Flat (Maybe Bool) -> p ac (Maybe Bool)
every = unsafeAggregateOp SQL.EVERY
any' :: (AggregatedContext ac, SqlProjectable (p ac))
=> Projection Flat (Maybe Bool) -> p ac (Maybe Bool)
any' = unsafeAggregateOp SQL.ANY
some' :: (AggregatedContext ac, SqlProjectable (p ac))
=> Projection Flat (Maybe Bool) -> p ac (Maybe Bool)
some' = unsafeAggregateOp SQL.SOME
instance Projectable Projection Projection where
project = id
instance Projectable Projection Expr where
project = expr
projectPi :: Projectable Projection p1 => Projection c a -> Pi a b -> p1 c b
projectPi p = project . Projection.pi p
projectPiMaybe :: Projectable Projection p1 => Projection c (Maybe a) -> Pi a b -> p1 c (Maybe b)
projectPiMaybe p = project . Projection.piMaybe p
projectPiMaybe' :: Projectable Projection p1 => Projection c (Maybe a) -> Pi a (Maybe b) -> p1 c (Maybe b)
projectPiMaybe' p = project . Projection.piMaybe' p
(!) :: Projectable Projection p
=> Projection c a
-> Pi a b
-> p c b
(!) = projectPi
(?!) :: Projectable Projection p
=> Projection c (Maybe a)
-> Pi a b
-> p c (Maybe b)
(?!) = projectPiMaybe
(?!?) :: Projectable Projection p
=> Projection c (Maybe a)
-> Pi a (Maybe b)
-> p c (Maybe b)
(?!?) = projectPiMaybe'
(.!) :: Projection c (Maybe a)
-> Pi a b
-> Expr c b
(.!) p = fromJust . projectPiMaybe p
(.?) :: Projection c (Maybe a)
-> Pi a (Maybe b)
-> Expr c b
(.?) p = fromJust . projectPiMaybe' p
class ProjectableFlattenMaybe a b where
flatten :: ProjectableMaybe p => p a -> p b
instance ProjectableFlattenMaybe (Maybe a) b
=> ProjectableFlattenMaybe (Maybe (Maybe a)) b where
flatten = flatten . flattenMaybe
instance ProjectableFlattenMaybe (Maybe a) (Maybe a) where
flatten = id
flattenPiMaybe :: (ProjectableMaybe (Projection cont), ProjectableFlattenMaybe (Maybe b) c)
=> Projection cont (Maybe a)
-> Pi a b
-> Projection cont c
flattenPiMaybe p = flatten . Projection.piMaybe p
projectFlattenPiMaybe :: (ProjectableMaybe (Projection cont),
Projectable Projection p1, ProjectableFlattenMaybe (Maybe b) c)
=> Projection cont (Maybe a)
-> Pi a b
-> p1 cont c
projectFlattenPiMaybe p = project . flattenPiMaybe p
(!??) :: (ProjectableFlattenMaybe (Maybe b) c,
Projectable Projection p, ProjectableMaybe (p cont))
=> Projection cont (Maybe a)
-> Pi a b
-> p cont c
(!??) = projectFlattenPiMaybe
class ProjectableRunIdsZip a b where
runIds :: ProjectableIdZip p => p a -> p b
instance ProjectableRunIdsZip a b => ProjectableRunIdsZip ((), a) b where
runIds = runIds . leftId
instance ProjectableRunIdsZip a b => ProjectableRunIdsZip (a, ()) b where
runIds = runIds . rightId
instance ProjectableRunIdsZip a a where
runIds = id
flattenPh :: ProjectableRunIdsZip a b => PlaceHolders a -> PlaceHolders b
flattenPh = runIds
infixl 8 !, ?!, ?!?, !??, .!, .?