{-# language FlexibleContexts #-}
{-# language TupleSections #-}
module Rel8.Query.Evaluate
( evaluate
)
where
import Control.Monad ( (>=>) )
import Data.Foldable ( foldl' )
import Data.List.NonEmpty ( NonEmpty( (:|) ), nonEmpty )
import Data.Monoid ( Any( Any ) )
import Prelude hiding ( undefined )
import qualified Opaleye.Internal.HaskellDB.PrimQuery as Opaleye
import Rel8.Expr ( Expr )
import Rel8.Expr.Bool ( (&&.) )
import Rel8.Expr.Opaleye ( fromPrimExpr )
import Rel8.Query ( Query( Query ) )
import Rel8.Query.Rebind ( rebind )
import Rel8.Table ( Table )
import Rel8.Table.Bool ( case_ )
import Rel8.Table.Undefined ( undefined )
evaluate :: Table Expr a => a -> Query a
evaluate :: forall a. Table Expr a => a -> Query a
evaluate = forall a. Table Expr a => a -> Query a
laterally forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall a. Table Expr a => String -> a -> Query a
rebind String
"eval"
laterally :: Table Expr a => a -> Query a
laterally :: forall a. Table Expr a => a -> Query a
laterally a
a = forall a. ([PrimExpr] -> Select (Any, a)) -> Query a
Query forall a b. (a -> b) -> a -> b
$ \[PrimExpr]
bindings -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ (Bool -> Any
Any Bool
True,) forall a b. (a -> b) -> a -> b
$
case forall a. [a] -> Maybe (NonEmpty a)
nonEmpty [PrimExpr]
bindings of
Maybe (NonEmpty PrimExpr)
Nothing -> a
a
Just NonEmpty PrimExpr
bindings' -> forall a. Table Expr a => [(Expr Bool, a)] -> a -> a
case_ [(Expr Bool
condition, a
a)] forall a. Table Expr a => a
undefined
where
condition :: Expr Bool
condition = forall a. (a -> a -> a) -> NonEmpty a -> a
foldl1' Expr Bool -> Expr Bool -> Expr Bool
(&&.) (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall {a}. PrimExpr -> Expr a
go NonEmpty PrimExpr
bindings')
where
go :: PrimExpr -> Expr a
go = forall {a}. PrimExpr -> Expr a
fromPrimExpr forall b c a. (b -> c) -> (a -> b) -> a -> c
. UnOp -> PrimExpr -> PrimExpr
Opaleye.UnExpr UnOp
Opaleye.OpIsNotNull
foldl1' :: (a -> a -> a) -> NonEmpty a -> a
foldl1' :: forall a. (a -> a -> a) -> NonEmpty a -> a
foldl1' a -> a -> a
f (a
a :| [a]
as) = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' a -> a -> a
f a
a [a]
as