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 qualified Language.SQL.Keyword as SQL
import Database.Record (PersistableWidth)
import Database.Relational.Query.Context (Flat, Aggregated, OverWindow)
import Database.Relational.Query.Projection (Projection)
import qualified Database.Relational.Query.Projection as Projection
import Database.Relational.Query.Projectable
(PlaceHolders, unsafeUniOp,
ProjectableMaybe (flattenMaybe), ProjectableIdZip (leftId, rightId),
SqlProjectable)
import Database.Relational.Query.Pi (Pi)
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.<++>) . SQL.paren)
count :: (Integral b, AggregatedContext ac, SqlProjectable (p ac))
=> Projection Flat a -> p ac b
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
(!) :: PersistableWidth a
=> Projection c a
-> Pi a b
-> Projection c b
(!) = Projection.pi
(?!) :: PersistableWidth a
=> Projection c (Maybe a)
-> Pi a b
-> Projection c (Maybe b)
(?!) = Projection.piMaybe
(?!?) :: PersistableWidth a
=> Projection c (Maybe a)
-> Pi a (Maybe b)
-> Projection c (Maybe b)
(?!?) = Projection.piMaybe'
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 :: (PersistableWidth a, ProjectableMaybe (Projection cont), ProjectableFlattenMaybe (Maybe b) c)
=> Projection cont (Maybe a)
-> Pi a b
-> Projection cont c
flattenPiMaybe p = flatten . Projection.piMaybe p
(!??) :: (PersistableWidth a, ProjectableMaybe (Projection cont), ProjectableFlattenMaybe (Maybe b) c)
=> Projection cont (Maybe a)
-> Pi a b
-> Projection cont c
(!??) = flattenPiMaybe
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 !, ?!, ?!?, !??