Implement orderBy[rand] modifier.
This commit is contained in:
parent
8df36fb9c7
commit
d37331e04d
@ -38,7 +38,7 @@ module Database.Esqueleto
|
|||||||
-- $gettingstarted
|
-- $gettingstarted
|
||||||
|
|
||||||
-- * @esqueleto@'s Language
|
-- * @esqueleto@'s Language
|
||||||
Esqueleto( where_, on, groupBy, orderBy, asc, desc, limit, offset, having
|
Esqueleto( where_, on, groupBy, orderBy, rand, asc, desc, limit, offset, having
|
||||||
, sub_select, sub_selectDistinct, (^.), (?.)
|
, sub_select, sub_selectDistinct, (^.), (?.)
|
||||||
, val, isNothing, just, nothing, joinV, countRows, count, not_
|
, val, isNothing, just, nothing, joinV, countRows, count, not_
|
||||||
, (==.), (>=.), (>.), (<=.), (<.), (!=.), (&&.), (||.)
|
, (==.), (>=.), (>.), (<=.), (<.), (!=.), (&&.), (||.)
|
||||||
|
|||||||
@ -174,6 +174,9 @@ class (Functor query, Applicative query, Monad query) =>
|
|||||||
-- | @OFFSET@. Usually used with 'limit'.
|
-- | @OFFSET@. Usually used with 'limit'.
|
||||||
offset :: Int64 -> query ()
|
offset :: Int64 -> query ()
|
||||||
|
|
||||||
|
-- | @ORDER BY random()@ clause.
|
||||||
|
rand :: expr OrderBy
|
||||||
|
|
||||||
-- | @HAVING@.
|
-- | @HAVING@.
|
||||||
--
|
--
|
||||||
-- /Since: 1.2.2/
|
-- /Since: 1.2.2/
|
||||||
|
|||||||
@ -256,6 +256,7 @@ data SqlExpr a where
|
|||||||
|
|
||||||
-- A 'SqlExpr' accepted only by 'orderBy'.
|
-- A 'SqlExpr' accepted only by 'orderBy'.
|
||||||
EOrderBy :: OrderByType -> SqlExpr (Value a) -> SqlExpr OrderBy
|
EOrderBy :: OrderByType -> SqlExpr (Value a) -> SqlExpr OrderBy
|
||||||
|
EOrderRandom :: SqlExpr OrderBy
|
||||||
|
|
||||||
-- A 'SqlExpr' accepted only by 'set'.
|
-- A 'SqlExpr' accepted only by 'set'.
|
||||||
ESet :: (SqlExpr (Entity val) -> SqlExpr (Value ())) -> SqlExpr (Update val)
|
ESet :: (SqlExpr (Entity val) -> SqlExpr (Value ())) -> SqlExpr (Update val)
|
||||||
@ -322,6 +323,8 @@ instance Esqueleto SqlQuery SqlExpr SqlBackend where
|
|||||||
asc = EOrderBy ASC
|
asc = EOrderBy ASC
|
||||||
desc = EOrderBy DESC
|
desc = EOrderBy DESC
|
||||||
|
|
||||||
|
rand = EOrderRandom
|
||||||
|
|
||||||
limit n = Q $ W.tell mempty { sdLimitClause = Limit (Just n) Nothing }
|
limit n = Q $ W.tell mempty { sdLimitClause = Limit (Just n) Nothing }
|
||||||
offset n = Q $ W.tell mempty { sdLimitClause = Limit Nothing (Just n) }
|
offset n = Q $ W.tell mempty { sdLimitClause = Limit Nothing (Just n) }
|
||||||
|
|
||||||
@ -903,7 +906,9 @@ makeOrderBy :: IdentInfo -> [OrderByClause] -> (TLB.Builder, [PersistValue])
|
|||||||
makeOrderBy _ [] = mempty
|
makeOrderBy _ [] = mempty
|
||||||
makeOrderBy info os = first ("\nORDER BY " <>) $ uncommas' (map mk os)
|
makeOrderBy info os = first ("\nORDER BY " <>) $ uncommas' (map mk os)
|
||||||
where
|
where
|
||||||
|
mk :: OrderByClause -> (TLB.Builder, [PersistValue])
|
||||||
mk (EOrderBy t (ERaw p f)) = first ((<> orderByType t) . parensM p) (f info)
|
mk (EOrderBy t (ERaw p f)) = first ((<> orderByType t) . parensM p) (f info)
|
||||||
|
mk EOrderRandom = first ((<> "RANDOM()")) mempty
|
||||||
orderByType ASC = " ASC"
|
orderByType ASC = " ASC"
|
||||||
orderByType DESC = " DESC"
|
orderByType DESC = " DESC"
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user