New functions castNum and castNumM.
This commit is contained in:
parent
39bc711563
commit
f32c98c412
@ -45,9 +45,9 @@ module Database.Esqueleto
|
|||||||
, (==.), (>=.), (>.), (<=.), (<.), (!=.), (&&.), (||.)
|
, (==.), (>=.), (>.), (<=.), (<.), (!=.), (&&.), (||.)
|
||||||
, (+.), (-.), (/.), (*.)
|
, (+.), (-.), (/.), (*.)
|
||||||
, random_, round_, ceiling_, floor_
|
, random_, round_, ceiling_, floor_
|
||||||
, min_, max_, sum_, avg_, lower_
|
, min_, max_, sum_, avg_, castNum, castNumM
|
||||||
, coalesce, coalesceDefault
|
, coalesce, coalesceDefault
|
||||||
, like, ilike, (%), concat_, (++.)
|
, lower_, like, ilike, (%), concat_, (++.)
|
||||||
, subList_select, subList_selectDistinct, valList
|
, subList_select, subList_selectDistinct, valList
|
||||||
, in_, notIn, exists, notExists
|
, in_, notIn, exists, notExists
|
||||||
, set, (=.), (+=.), (-=.), (*=.), (/=.)
|
, set, (=.), (+=.), (-=.), (*=.), (/=.)
|
||||||
|
|||||||
@ -351,6 +351,29 @@ class (Functor query, Applicative query, Monad query) =>
|
|||||||
max_ :: (PersistField a) => expr (Value a) -> expr (Value (Maybe a))
|
max_ :: (PersistField a) => expr (Value a) -> expr (Value (Maybe a))
|
||||||
avg_ :: (PersistField a, PersistField b) => expr (Value a) -> expr (Value (Maybe b))
|
avg_ :: (PersistField a, PersistField b) => expr (Value a) -> expr (Value (Maybe b))
|
||||||
|
|
||||||
|
-- | Allow a number of one type to be used as one of another
|
||||||
|
-- type via an implicit cast. An explicit cast is not made,
|
||||||
|
-- this function changes only the types on the Haskell side.
|
||||||
|
--
|
||||||
|
-- /Caveat/: Trying to use @castNum@ from @Double@ to @Int@
|
||||||
|
-- will not result in an integer, the original fractional
|
||||||
|
-- number will still be used! Use 'round_', 'ceiling_' or
|
||||||
|
-- 'floor_' instead.
|
||||||
|
--
|
||||||
|
-- /Safety/: This operation is mostly safe due to the 'Num'
|
||||||
|
-- constraint between the types and the fact that RDBMSs
|
||||||
|
-- usually allow numbers of different types to be used
|
||||||
|
-- interchangeably. However, there may still be issues with
|
||||||
|
-- the query not being accepted by the RDBMS or @persistent@
|
||||||
|
-- not being able to parse it.
|
||||||
|
--
|
||||||
|
-- /Since: 2.2.9/
|
||||||
|
castNum :: (Num a, Num b) => expr (Value a) -> expr (Value b)
|
||||||
|
-- | Same as 'castNum', but for nullable values.
|
||||||
|
--
|
||||||
|
-- /Since: 2.2.9/
|
||||||
|
castNumM :: (Num a, Num b) => expr (Value (Maybe a)) -> expr (Value (Maybe b))
|
||||||
|
|
||||||
-- | @COALESCE@ function. Evaluates the arguments in order and
|
-- | @COALESCE@ function. Evaluates the arguments in order and
|
||||||
-- returns the value of the first non-NULL expression, or NULL
|
-- returns the value of the first non-NULL expression, or NULL
|
||||||
-- (Nothing) otherwise. Some RDBMSs (such as SQLite) require
|
-- (Nothing) otherwise. Some RDBMSs (such as SQLite) require
|
||||||
|
|||||||
@ -476,6 +476,9 @@ instance Esqueleto SqlQuery SqlExpr SqlBackend where
|
|||||||
max_ = unsafeSqlFunction "MAX"
|
max_ = unsafeSqlFunction "MAX"
|
||||||
avg_ = unsafeSqlFunction "AVG"
|
avg_ = unsafeSqlFunction "AVG"
|
||||||
|
|
||||||
|
castNum = veryUnsafeCoerceSqlExprValue
|
||||||
|
castNumM = veryUnsafeCoerceSqlExprValue
|
||||||
|
|
||||||
coalesce = unsafeSqlFunctionParens "COALESCE"
|
coalesce = unsafeSqlFunctionParens "COALESCE"
|
||||||
coalesceDefault exprs = unsafeSqlFunctionParens "COALESCE" . (exprs ++) . return . just
|
coalesceDefault exprs = unsafeSqlFunctionParens "COALESCE" . (exprs ++) . return . just
|
||||||
|
|
||||||
|
|||||||
18
test/Test.hs
18
test/Test.hs
@ -118,6 +118,9 @@ share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistUpperCase|
|
|||||||
name String
|
name String
|
||||||
Foreign Point fkpoint centerX centerY
|
Foreign Point fkpoint centerX centerY
|
||||||
deriving Eq Show
|
deriving Eq Show
|
||||||
|
Numbers
|
||||||
|
int Int
|
||||||
|
double Double
|
||||||
|]
|
|]
|
||||||
|
|
||||||
-- | this could be achieved with S.fromList, but not all lists
|
-- | this could be achieved with S.fromList, but not all lists
|
||||||
@ -1205,8 +1208,8 @@ main = do
|
|||||||
ret <- select $ from (\(_::(SqlExpr (Entity BlogPost))) -> return countRows)
|
ret <- select $ from (\(_::(SqlExpr (Entity BlogPost))) -> return countRows)
|
||||||
liftIO $ ret `shouldBe` [Value (3::Int)]
|
liftIO $ ret `shouldBe` [Value (3::Int)]
|
||||||
|
|
||||||
describe "rand works" $ do
|
describe "Math-related functions" $ do
|
||||||
it "returns result in random order" $
|
it "rand returns result in random order" $
|
||||||
run $ do
|
run $ do
|
||||||
replicateM_ 20 $ do
|
replicateM_ 20 $ do
|
||||||
_ <- insert p1
|
_ <- insert p1
|
||||||
@ -1226,6 +1229,17 @@ main = do
|
|||||||
|
|
||||||
liftIO $ (ret1 == ret2) `shouldBe` False
|
liftIO $ (ret1 == ret2) `shouldBe` False
|
||||||
|
|
||||||
|
it "castNum works for multiplying Int and Double" $
|
||||||
|
run $ do
|
||||||
|
mapM_ insert [Numbers 2 3.4, Numbers 7 1.1]
|
||||||
|
ret <-
|
||||||
|
select $
|
||||||
|
from $ \n -> do
|
||||||
|
let r = castNum (n ^. NumbersInt) *. n ^. NumbersDouble
|
||||||
|
orderBy [asc r]
|
||||||
|
return r
|
||||||
|
liftIO $ ret `shouldBe` [Value 6.8, Value 7.7]
|
||||||
|
|
||||||
describe "case" $ do
|
describe "case" $ do
|
||||||
it "Works for a simple value based when - False" $
|
it "Works for a simple value based when - False" $
|
||||||
run $ do
|
run $ do
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user