module Opaleye.Internal.Column where
import qualified Opaleye.Internal.HaskellDB.PrimQuery as HPQ
import qualified Opaleye.Internal.HaskellDB.Query as Q
import GHC.Int (Int64)
newtype Column a = Column HPQ.PrimExpr deriving Show
data Nullable a = Nullable
unColumn :: Column a -> HPQ.PrimExpr
unColumn (Column e) = e
unsafeCoerce :: Column a -> Column b
unsafeCoerce (Column e) = Column e
constant :: Q.ShowConstant a => a -> Column a
constant = Column . HPQ.ConstExpr . Q.showConstant
binOp :: HPQ.BinOp -> Column a -> Column b -> Column c
binOp op (Column e) (Column e') = Column (HPQ.BinExpr op e e')
unOp :: HPQ.UnOp -> Column a -> Column b
unOp op (Column e) = Column (HPQ.UnExpr op e)
case_ :: [(Column Bool, Column a)] -> Column a -> Column a
case_ alts (Column otherwise_) = Column (HPQ.CaseExpr (unColumns alts) otherwise_)
where unColumns = map (\(Column e, Column e') -> (e, e'))
ifThenElse :: Column Bool -> Column a -> Column a -> Column a
ifThenElse cond t f = case_ [(cond, t)] f
(.>) :: Column a -> Column a -> Column Bool
(.>) = binOp HPQ.OpGt
(.==) :: Column a -> Column a -> Column Bool
(.==) = binOp HPQ.OpEq
instance Q.ShowConstant Int64 where
showConstant = HPQ.IntegerLit . fromIntegral
instance (Q.ShowConstant a, Num a) => Num (Column a) where
fromInteger = constant . fromInteger
(*) = binOp HPQ.OpMul
(+) = binOp HPQ.OpPlus
() = binOp HPQ.OpMinus
abs (Column e) = Column (HPQ.UnExpr (HPQ.UnOpOther "@") e)
negate (Column e) = Column (HPQ.UnExpr (HPQ.UnOpOther "-") e)
signum c = case_ [(c .> 0, 1), (c .== 0, 0)] (1)
instance (Q.ShowConstant a, Fractional a) => Fractional (Column a) where
fromRational = constant . fromRational
(/) = binOp HPQ.OpDiv