mirror of
https://github.com/circuithub/rel8.git
synced 2024-09-11 16:05:41 +03:00
Restore Num (Expr a) and Fractional (Expr a)
This commit is contained in:
parent
3235887375
commit
3f94330a35
31
src/Rel8.hs
31
src/Rel8.hs
@ -196,7 +196,9 @@ import qualified Data.ByteString.Lazy
|
||||
import Data.CaseInsensitive ( CI )
|
||||
|
||||
-- opaleye
|
||||
import qualified Opaleye ( Delete(..), Insert(..), OnConflict(..), Update(..), runDelete_, runInsert_, runUpdate_, valuesExplicit )
|
||||
|
||||
-- opaleye
|
||||
import qualified Opaleye ( Delete(..), Insert(..), OnConflict(..), Update(..), runDelete_, runInsert_, runUpdate_, valuesExplicit, PGInt8 )
|
||||
import qualified Opaleye.Aggregate as Opaleye
|
||||
import qualified Opaleye.Binary as Opaleye
|
||||
import qualified Opaleye.Distinct as Opaleye
|
||||
@ -429,6 +431,33 @@ class DBType a => DBEq (a :: Type) where
|
||||
newtype Expr (a :: Type) = Expr { toPrimExpr :: Opaleye.PrimExpr }
|
||||
|
||||
|
||||
-- | It is assumed that any Haskell types that have a 'Num' instance also have
|
||||
-- the corresponding operations in the database. Hence, Num a => Num (Expr a).
|
||||
-- *However*, if this is not the case, you should `newtype` the Haskell type
|
||||
-- and avoid providing a 'Num' instance, or you may write be able to write
|
||||
-- ill-typed queries!
|
||||
instance (DBType a, Num a) => Num (Expr a) where
|
||||
a + b = columnToExpr (Opaleye.binOp (Opaleye.:+) (exprToColumn a) (exprToColumn b))
|
||||
a * b = columnToExpr (Opaleye.binOp (Opaleye.:*) (exprToColumn a) (exprToColumn b))
|
||||
abs = function "abs"
|
||||
signum = columnToExpr @Opaleye.PGInt8 . signum . exprToColumn
|
||||
fromInteger = lit . fromInteger
|
||||
negate = columnToExpr @Opaleye.PGInt8 . negate . exprToColumn
|
||||
|
||||
|
||||
instance (DBType a, Fractional a) => Fractional (Expr a) where
|
||||
a / b = columnToExpr (Opaleye.binOp (Opaleye.:/) (exprToColumn a) (exprToColumn b))
|
||||
fromRational = lit . fromRational
|
||||
|
||||
|
||||
exprToColumn :: Expr a -> Opaleye.Column b
|
||||
exprToColumn (Expr a) = Opaleye.Column a
|
||||
|
||||
|
||||
columnToExpr :: Opaleye.Column b -> Expr a
|
||||
columnToExpr (Opaleye.Column a) = Expr a
|
||||
|
||||
|
||||
-- | Unsafely treat an 'Expr' that returns @a@s as returning @b@s.
|
||||
unsafeCoerceExpr :: Expr a -> Expr b
|
||||
unsafeCoerceExpr (Expr x) = Expr x
|
||||
|
Loading…
Reference in New Issue
Block a user