opaleye-0.9.5.1: An SQL-generating DSL targeting PostgreSQL
Safe HaskellSafe-Inferred
LanguageHaskell2010

Opaleye.Order

Description

ORDER BY, LIMIT, OFFSET and DISTINCT ON

Synopsis

Order by

orderBy :: Order a -> Select a -> Select a Source #

Order the rows of a Select according to the Order.

import Data.Monoid ((<>))

-- Order by the first field ascending.  When first fields are equal
-- order by second field descending.
example :: Select (Field SqlInt4, Field SqlText)
        -> Select (Field SqlInt4, Field SqlText)
example = orderBy (asc fst <> desc snd)

data Order a Source #

An Order a represents a sort order and direction for the elements of the type a. Multiple Orders can be composed with mappend or (<>) from Data.Monoid. If two rows are equal according to the first Order in the mappend, the second is used, and so on.

Instances

Instances details
Contravariant Order Source # 
Instance details

Defined in Opaleye.Internal.Order

Methods

contramap :: (a' -> a) -> Order a -> Order a' #

(>$) :: b -> Order b -> Order a #

Decidable Order Source # 
Instance details

Defined in Opaleye.Internal.Order

Methods

lose :: (a -> Void) -> Order a #

choose :: (a -> Either b c) -> Order b -> Order c -> Order a #

Divisible Order Source # 
Instance details

Defined in Opaleye.Internal.Order

Methods

divide :: (a -> (b, c)) -> Order b -> Order c -> Order a #

conquer :: Order a #

Monoid (Order a) Source # 
Instance details

Defined in Opaleye.Internal.Order

Methods

mempty :: Order a #

mappend :: Order a -> Order a -> Order a #

mconcat :: [Order a] -> Order a #

Semigroup (Order a) Source # 
Instance details

Defined in Opaleye.Internal.Order

Methods

(<>) :: Order a -> Order a -> Order a #

sconcat :: NonEmpty (Order a) -> Order a #

stimes :: Integral b => b -> Order a -> Order a #

Order direction

asc :: SqlOrd b => (a -> Field b) -> Order a Source #

Specify an ascending ordering by the given expression.

desc :: SqlOrd b => (a -> Field b) -> Order a Source #

Specify an descending ordering by the given expression.

ascNullsFirst :: SqlOrd b => (a -> Field_ n b) -> Order a Source #

Specify an ascending ordering by the given expression. (Any NULLs appear first)

ascNullsLast :: SqlOrd b => (a -> Field_ n b) -> Order a Source #

Specify an ascending ordering by the given expression. (Any NULLs appear last)

descNullsLast :: SqlOrd b => (a -> Field_ n b) -> Order a Source #

Specify an descending ordering by the given expression. (Any NULLs appear last)

descNullsFirst :: SqlOrd b => (a -> Field_ n b) -> Order a Source #

Specify an descending ordering by the given expression. (Any NULLs appear first)

Limit and offset

limit :: Int -> Select a -> Select a Source #

Limit the results of the given Select to the given maximum number of items.

WARNING: If you're planning on using limit/offset together please use offset before you use limit, e.g.:

limit 10 (offset 50 yourSelect)

This is because Opaleye applies OFFSET and LIMIT to the SELECT separately. The result of the Select given above is the following, which will return 10 rows after skipping the first 50 (probably what you want).

SELECT * FROM (SELECT * FROM yourTable OFFSET 50) LIMIT 10

However, reversing the order of the limit/offset will result in the following, which will result in no rows being returned (probably not what you want).

SELECT * FROM (SELECT * FROM yourTable LIMIT 10) OFFSET 50

offset :: Int -> Select a -> Select a Source #

Offset the results of the given Select by the given amount, skipping that many result rows.

WARNING: Please read the documentation of limit before combining offset with limit.

Distinct on

distinctOn :: Default Unpackspec b b => (a -> b) -> Select a -> Select a Source #

Keep a row from each set where the given function returns the same result. No ordering is guaranteed. Multiple fields may be distinguished by projecting out tuples of Field_s. Use distinctOnBy to control how the rows are chosen.

distinctOnBy :: Default Unpackspec b b => (a -> b) -> Order a -> Select a -> Select a Source #

Keep the row from each set where the given function returns the same result. The row is chosen according to which comes first by the supplied ordering. However, no output ordering is guaranteed. Multiple fields may be distinguished by projecting out tuples of Field_s.

Exact ordering

exact :: [Field_ n b] -> (a -> Field_ n b) -> Order a Source #

Order the results of a given query exactly, as determined by the given list of input fields. Note that this list does not have to contain an entry for every result in your query: you may exactly order only a subset of results, if you wish. Rows that are not ordered according to the input list are returned after the ordered results, in the usual order the database would return them (e.g. sorted by primary key). Exactly-ordered results always come first in a result set. Entries in the input list that are not present in result of a query are ignored.

Other

class SqlOrd a Source #

Typeclass for Postgres types which support ordering operations.

Instances

Instances details
SqlOrd SqlBool Source # 
Instance details

Defined in Opaleye.Order

SqlOrd SqlCitext Source # 
Instance details

Defined in Opaleye.Order

SqlOrd SqlDate Source # 
Instance details

Defined in Opaleye.Order

SqlOrd SqlFloat4 Source # 
Instance details

Defined in Opaleye.Order

SqlOrd SqlFloat8 Source # 
Instance details

Defined in Opaleye.Order

SqlOrd SqlInt2 Source # 
Instance details

Defined in Opaleye.Order

SqlOrd SqlInt4 Source # 
Instance details

Defined in Opaleye.Order

SqlOrd SqlInt8 Source # 
Instance details

Defined in Opaleye.Order

SqlOrd SqlNumeric Source # 
Instance details

Defined in Opaleye.Order

SqlOrd SqlText Source # 
Instance details

Defined in Opaleye.Order

SqlOrd SqlTime Source # 
Instance details

Defined in Opaleye.Order

SqlOrd SqlTimestamp Source # 
Instance details

Defined in Opaleye.Order

SqlOrd SqlTimestamptz Source # 
Instance details

Defined in Opaleye.Order

SqlOrd SqlUuid Source # 
Instance details

Defined in Opaleye.Order

SqlOrd SqlVarcharN Source # 
Instance details

Defined in Opaleye.Order

Explicit versions

distinctOnExplicit :: Unpackspec b b -> (a -> b) -> Select a -> Select a Source #

distinctOnByExplicit :: Unpackspec b b -> (a -> b) -> Order a -> Select a -> Select a Source #

Deprecated

distinctOnCorrect :: Default Unpackspec b b => (a -> b) -> Select a -> Select a Source #

Use distinctOn instead. Will be deprecated in 0.10.

distinctOnByCorrect :: Default Unpackspec b b => (a -> b) -> Order a -> Select a -> Select a Source #

Use distinctOnBy instead. Will be deprecated in 0.10.