on db148ff help sign in
rel8/src/Rel8/Expr/Num.hs
77 lines 2.2 kB history raw
1{-# language FlexibleContexts #-}
2{-# language TypeFamilies #-}
4{-# options_ghc -fno-warn-redundant-constraints #-}
6module Rel8.Expr.Num
7 ( fromIntegral, realToFrac, div, mod, ceiling, floor, round, truncate
8 )
9where
11-- base
12import Prelude ()
14-- rel
15import Rel8.Expr ( Expr( Expr ) )
16import Rel8.Expr.Function ( function )
17import Rel8.Expr.Opaleye ( castExpr )
18import Rel8.Schema.Null ( Homonullable, Sql )
19import Rel8.Type.Num ( DBFractional, DBIntegral, DBNum )
22-- | Cast 'DBIntegral' types to 'DBNum' types. For example, this can be useful
23-- if you need to turn an @Expr Int32@ into an @Expr Double@.
24fromIntegral :: (Sql DBIntegral a, Sql DBNum b, Homonullable a b)
25 => Expr a -> Expr b
26fromIntegral (Expr a) = castExpr (Expr a)
29-- | Cast 'DBNum' types to 'DBFractional' types. For example, his can be useful
30-- to convert @Expr Float@ to @Expr Double@.
31realToFrac :: (Sql DBNum a, Sql DBFractional b, Homonullable a b)
32 => Expr a -> Expr b
33realToFrac (Expr a) = castExpr (Expr a)
36-- | Round a 'DBFractional' to a 'DBIntegral' by rounding to the nearest larger
37-- integer.
38--
39-- Corresponds to the @ceiling()@ function.
40ceiling :: (Sql DBFractional a, Sql DBIntegral b, Homonullable a b)
41 => Expr a -> Expr b
42ceiling = function "CEILING"
45-- | Perform integral division. Corresponds to the @div()@ function.
46div :: Sql DBIntegral a => Expr a -> Expr a -> Expr a
47div = function "DIV"
50-- | Corresponds to the @mod()@ function.
51mod :: Sql DBIntegral a => Expr a -> Expr a -> Expr a
52mod = function "MOD"
55-- | Round a 'DFractional' to a 'DBIntegral' by rounding to the nearest smaller
56-- integer.
57--
58-- Corresponds to the @floor()@ function.
59floor :: (Sql DBFractional a, Sql DBIntegral b, Homonullable a b)
60 => Expr a -> Expr b
61floor = function "FLOOR"
64-- | Round a 'DBFractional' to a 'DBIntegral' by rounding to the nearest
65-- integer.
66--
67-- Corresponds to the @round()@ function.
68round :: (Sql DBFractional a, Sql DBIntegral b, Homonullable a b)
69 => Expr a -> Expr b
70round = function "ROUND"
73-- | Round a 'DBFractional' to a 'DBIntegral' by rounding to the nearest
74-- integer towards zero.
75truncate :: (Sql DBFractional a, Sql DBIntegral b, Homonullable a b)
76 => Expr a -> Expr b
77truncate = function "TRUNC"