2020-11-12 12:25:48 +03:00
|
|
|
module Hasura.RQL.IR.OrderBy
|
2021-09-24 01:56:37 +03:00
|
|
|
( OrderByCol (..),
|
|
|
|
OrderByItemG (..),
|
|
|
|
OrderByItem,
|
|
|
|
-- used by RQL.DML.Types
|
|
|
|
orderByColFromTxt,
|
|
|
|
)
|
|
|
|
where
|
2020-11-12 12:25:48 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
import Data.Aeson
|
|
|
|
import Data.Text qualified as T
|
|
|
|
import Hasura.Prelude
|
|
|
|
import Hasura.RQL.Types.Backend
|
|
|
|
import Hasura.RQL.Types.Common
|
|
|
|
import Hasura.SQL.Backend
|
2020-11-12 12:25:48 +03:00
|
|
|
|
|
|
|
-- order by col
|
|
|
|
|
|
|
|
data OrderByCol
|
|
|
|
= OCPG !FieldName
|
|
|
|
| OCRel !FieldName !OrderByCol
|
2020-11-26 16:57:03 +03:00
|
|
|
deriving (Show, Eq)
|
2020-11-12 12:25:48 +03:00
|
|
|
|
|
|
|
instance FromJSON OrderByCol where
|
|
|
|
parseJSON = \case
|
2021-09-24 01:56:37 +03:00
|
|
|
(String t) -> orderByColFromToks $ T.split (== '.') t
|
|
|
|
v -> parseJSON v >>= orderByColFromToks
|
2020-11-12 12:25:48 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
orderByColFromToks ::
|
|
|
|
(MonadFail m) =>
|
|
|
|
[Text] ->
|
|
|
|
m OrderByCol
|
2020-11-12 12:25:48 +03:00
|
|
|
orderByColFromToks toks = do
|
|
|
|
when (any T.null toks) $ fail "col/rel cannot be empty"
|
|
|
|
case toks of
|
2021-09-24 01:56:37 +03:00
|
|
|
[] -> fail "failed to parse an OrderByCol: found empty cols"
|
|
|
|
x : xs -> return $ go (FieldName x) xs
|
2020-11-12 12:25:48 +03:00
|
|
|
where
|
|
|
|
go fld = \case
|
2021-09-24 01:56:37 +03:00
|
|
|
[] -> OCPG fld
|
|
|
|
x : xs -> OCRel fld $ go (FieldName x) xs
|
2020-11-12 12:25:48 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
orderByColFromTxt ::
|
|
|
|
(MonadFail m) =>
|
|
|
|
Text ->
|
|
|
|
m OrderByCol
|
2020-11-12 12:25:48 +03:00
|
|
|
orderByColFromTxt =
|
2021-09-24 01:56:37 +03:00
|
|
|
orderByColFromToks . T.split (== '.')
|
2020-11-12 12:25:48 +03:00
|
|
|
|
|
|
|
-- order by item
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
data OrderByItemG (b :: BackendType) a = OrderByItemG
|
|
|
|
{ obiType :: !(Maybe (BasicOrderType b)),
|
|
|
|
obiColumn :: !a,
|
|
|
|
obiNulls :: !(Maybe (NullsOrderType b))
|
|
|
|
}
|
|
|
|
deriving (Functor, Foldable, Traversable, Generic)
|
|
|
|
|
2020-11-12 12:25:48 +03:00
|
|
|
deriving instance (Backend b, Show a) => Show (OrderByItemG b a)
|
2021-09-24 01:56:37 +03:00
|
|
|
|
|
|
|
deriving instance (Backend b, Eq a) => Eq (OrderByItemG b a)
|
|
|
|
|
2020-11-12 12:25:48 +03:00
|
|
|
instance (Backend b, Hashable a) => Hashable (OrderByItemG b a)
|
|
|
|
|
|
|
|
type OrderByItem b = OrderByItemG b OrderByCol
|