Added support for nulls first / nulls last
This commit is contained in:
committed by
Joe Nelson
parent
6e6769ea16
commit
97c9bfe93f
+13
-5
@@ -1,4 +1,4 @@
|
|||||||
{-# LANGUAGE TypeSynonymInstances, FlexibleInstances #-}
|
{-# LANGUAGE TypeSynonymInstances, FlexibleInstances, MultiWayIf #-}
|
||||||
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
||||||
|
|
||||||
module PgQuery where
|
module PgQuery where
|
||||||
@@ -39,6 +39,7 @@ data QualifiedTable = QualifiedTable {
|
|||||||
data OrderTerm = OrderTerm {
|
data OrderTerm = OrderTerm {
|
||||||
otTerm :: T.Text
|
otTerm :: T.Text
|
||||||
, otDirection :: BS.ByteString
|
, otDirection :: BS.ByteString
|
||||||
|
, otNullOrder :: Maybe BS.ByteString
|
||||||
}
|
}
|
||||||
|
|
||||||
limitT :: Maybe NonnegRange -> StatementT
|
limitT :: Maybe NonnegRange -> StatementT
|
||||||
@@ -67,7 +68,8 @@ orderT ts q =
|
|||||||
queryTerm :: OrderTerm -> PStmt
|
queryTerm :: OrderTerm -> PStmt
|
||||||
queryTerm t = B.Stmt
|
queryTerm t = B.Stmt
|
||||||
(" " <> cs (pgFmtIdent $ otTerm t) <> " "
|
(" " <> cs (pgFmtIdent $ otTerm t) <> " "
|
||||||
<> cs (otDirection t) <> " ")
|
<> cs (otDirection t) <> " "
|
||||||
|
<> maybe "" cs (otNullOrder t) <> " ")
|
||||||
empty True
|
empty True
|
||||||
|
|
||||||
parentheticT :: StatementT
|
parentheticT :: StatementT
|
||||||
@@ -175,10 +177,16 @@ orderParse q =
|
|||||||
orderParseTerm :: T.Text -> Maybe OrderTerm
|
orderParseTerm :: T.Text -> Maybe OrderTerm
|
||||||
orderParseTerm s =
|
orderParseTerm s =
|
||||||
case T.split (=='.') s of
|
case T.split (=='.') s of
|
||||||
[c,d] ->
|
(c:d:nls) ->
|
||||||
if d `elem` ["asc", "desc"]
|
if d `elem` ["asc", "desc"]
|
||||||
then Just $ OrderTerm c $
|
then Just $ OrderTerm c
|
||||||
if d == "asc" then "asc" else "desc"
|
( if d == "asc" then "asc" else "desc" )
|
||||||
|
( case nls of
|
||||||
|
[n] -> if | n == "nullsfirst" -> Just "nulls first"
|
||||||
|
| n == "nullslast" -> Just "nulls last"
|
||||||
|
| otherwise -> Nothing
|
||||||
|
_ -> Nothing
|
||||||
|
)
|
||||||
else Nothing
|
else Nothing
|
||||||
_ -> Nothing
|
_ -> Nothing
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user