2022-03-16 07:12:15 +03:00
|
|
|
{-# LANGUAGE DeriveAnyClass #-}
|
2022-03-31 07:45:03 +03:00
|
|
|
{-# LANGUAGE OverloadedLists #-}
|
|
|
|
{-# LANGUAGE StandaloneDeriving #-}
|
|
|
|
{-# LANGUAGE TemplateHaskell #-}
|
2022-03-16 07:12:15 +03:00
|
|
|
|
2022-05-02 08:03:12 +03:00
|
|
|
module Hasura.Backends.DataConnector.API.V0.Expression
|
2022-03-16 07:12:15 +03:00
|
|
|
( Expression (..),
|
2022-06-02 05:06:45 +03:00
|
|
|
BinaryComparisonOperator (..),
|
|
|
|
BinaryArrayComparisonOperator (..),
|
|
|
|
UnaryComparisonOperator (..),
|
|
|
|
ComparisonValue (..),
|
2022-03-16 07:12:15 +03:00
|
|
|
)
|
|
|
|
where
|
|
|
|
|
2022-03-31 07:45:03 +03:00
|
|
|
import Autodocodec.Extended
|
|
|
|
import Autodocodec.OpenAPI ()
|
2022-04-01 04:20:23 +03:00
|
|
|
import Control.DeepSeq (NFData)
|
2022-03-31 07:45:03 +03:00
|
|
|
import Control.Lens.TH (makePrisms)
|
2022-03-16 07:12:15 +03:00
|
|
|
import Data.Aeson (FromJSON, ToJSON)
|
2022-04-01 04:20:23 +03:00
|
|
|
import Data.Data (Data)
|
|
|
|
import Data.Hashable (Hashable)
|
2022-03-31 07:45:03 +03:00
|
|
|
import Data.OpenApi (ToSchema)
|
2022-04-01 04:20:23 +03:00
|
|
|
import GHC.Generics (Generic)
|
2022-05-02 08:03:12 +03:00
|
|
|
import Hasura.Backends.DataConnector.API.V0.Column qualified as API.V0
|
|
|
|
import Hasura.Backends.DataConnector.API.V0.Scalar.Value qualified as API.V0.Scalar
|
2022-04-01 04:20:23 +03:00
|
|
|
import Prelude
|
2022-03-16 07:12:15 +03:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2022-06-02 05:06:45 +03:00
|
|
|
-- | A serializable representation of binary comparison operators.
|
|
|
|
data BinaryComparisonOperator
|
2022-03-16 07:12:15 +03:00
|
|
|
= LessThan
|
|
|
|
| LessThanOrEqual
|
|
|
|
| GreaterThan
|
|
|
|
| GreaterThanOrEqual
|
2022-04-28 04:51:58 +03:00
|
|
|
| Equal
|
2022-03-31 07:45:03 +03:00
|
|
|
deriving stock (Data, Eq, Generic, Ord, Show, Enum, Bounded)
|
2022-04-01 04:20:23 +03:00
|
|
|
deriving anyclass (Hashable, NFData)
|
2022-06-02 05:06:45 +03:00
|
|
|
deriving (FromJSON, ToJSON, ToSchema) via Autodocodec BinaryComparisonOperator
|
2022-03-31 07:45:03 +03:00
|
|
|
|
2022-06-02 05:06:45 +03:00
|
|
|
instance HasCodec BinaryComparisonOperator where
|
2022-03-31 07:45:03 +03:00
|
|
|
codec =
|
2022-06-02 05:06:45 +03:00
|
|
|
named "BinaryComparisonOperator" $
|
2022-03-31 07:45:03 +03:00
|
|
|
disjointStringConstCodec
|
|
|
|
[ (LessThan, "less_than"),
|
|
|
|
(LessThanOrEqual, "less_than_or_equal"),
|
|
|
|
(GreaterThan, "greater_than"),
|
2022-04-28 04:51:58 +03:00
|
|
|
(GreaterThanOrEqual, "greater_than_or_equal"),
|
|
|
|
(Equal, "equal")
|
2022-03-31 07:45:03 +03:00
|
|
|
]
|
|
|
|
|
2022-06-02 05:06:45 +03:00
|
|
|
-- | A serializable representation of binary array comparison operators.
|
|
|
|
data BinaryArrayComparisonOperator
|
|
|
|
= In
|
|
|
|
deriving stock (Data, Eq, Generic, Ord, Show, Enum, Bounded)
|
|
|
|
deriving anyclass (Hashable, NFData)
|
|
|
|
deriving (FromJSON, ToJSON, ToSchema) via Autodocodec BinaryArrayComparisonOperator
|
|
|
|
|
|
|
|
instance HasCodec BinaryArrayComparisonOperator where
|
|
|
|
codec =
|
|
|
|
named "BinaryArrayComparisonOperator" $
|
|
|
|
disjointStringConstCodec
|
|
|
|
[ (In, "in")
|
|
|
|
]
|
|
|
|
|
|
|
|
-- | A serializable representation of unary comparison operators.
|
|
|
|
data UnaryComparisonOperator
|
|
|
|
= IsNull
|
|
|
|
deriving stock (Data, Eq, Generic, Ord, Show, Enum, Bounded)
|
|
|
|
deriving anyclass (Hashable, NFData)
|
|
|
|
deriving (FromJSON, ToJSON, ToSchema) via Autodocodec UnaryComparisonOperator
|
|
|
|
|
|
|
|
instance HasCodec UnaryComparisonOperator where
|
|
|
|
codec =
|
|
|
|
named "UnaryComparisonOperator" $
|
|
|
|
disjointStringConstCodec
|
|
|
|
[ (IsNull, "is_null")
|
|
|
|
]
|
2022-03-31 07:45:03 +03:00
|
|
|
|
|
|
|
-- | A serializable representation of query expressions.
|
|
|
|
data Expression
|
2022-06-02 05:06:45 +03:00
|
|
|
= And (ValueWrapper "expressions" [Expression])
|
2022-03-31 07:45:03 +03:00
|
|
|
| Or (ValueWrapper "expressions" [Expression])
|
|
|
|
| Not (ValueWrapper "expression" Expression)
|
2022-06-02 05:06:45 +03:00
|
|
|
| ApplyBinaryComparisonOperator (ValueWrapper3 "operator" BinaryComparisonOperator "column" API.V0.ColumnName "value" ComparisonValue)
|
|
|
|
| ApplyBinaryArrayComparisonOperator (ValueWrapper3 "operator" BinaryArrayComparisonOperator "column" API.V0.ColumnName "values" [ComparisonValue])
|
|
|
|
| ApplyUnaryComparisonOperator (ValueWrapper2 "operator" UnaryComparisonOperator "column" API.V0.ColumnName)
|
|
|
|
deriving stock (Data, Eq, Generic, Ord, Show)
|
|
|
|
deriving anyclass (Hashable, NFData)
|
|
|
|
|
|
|
|
-- | A serializable representation of comparison values used in comparisons inside 'Expression's.
|
|
|
|
data ComparisonValue
|
|
|
|
= -- | Allows a comparison to a column on the current table
|
|
|
|
-- TODO: joins in Expressions and then comparisons to other tables involved in those joins
|
|
|
|
AnotherColumn (ValueWrapper "column" API.V0.ColumnName)
|
|
|
|
| ScalarValue (ValueWrapper "value" API.V0.Scalar.Value)
|
2022-03-16 07:12:15 +03:00
|
|
|
deriving stock (Data, Eq, Generic, Ord, Show)
|
2022-04-01 04:20:23 +03:00
|
|
|
deriving anyclass (Hashable, NFData)
|
2022-03-31 07:45:03 +03:00
|
|
|
|
2022-06-02 05:06:45 +03:00
|
|
|
$(makePrisms ''ComparisonValue)
|
2022-03-31 07:45:03 +03:00
|
|
|
$(makePrisms ''Expression)
|
|
|
|
|
|
|
|
instance HasCodec Expression where
|
|
|
|
codec =
|
|
|
|
named "Expression" $
|
|
|
|
sumTypeCodec
|
2022-06-02 05:06:45 +03:00
|
|
|
[ TypeAlternative "AndExpression" "and" _And,
|
2022-03-31 07:45:03 +03:00
|
|
|
TypeAlternative "OrExpression" "or" _Or,
|
|
|
|
TypeAlternative "NotExpression" "not" _Not,
|
2022-06-02 05:06:45 +03:00
|
|
|
TypeAlternative "ApplyBinaryComparisonOperator" "binary_op" _ApplyBinaryComparisonOperator,
|
|
|
|
TypeAlternative "ApplyBinaryArrayComparisonExpression" "binary_arr_op" _ApplyBinaryArrayComparisonOperator,
|
|
|
|
TypeAlternative "ApplyUnaryComparisonOperator" "unary_op" _ApplyUnaryComparisonOperator
|
2022-03-31 07:45:03 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
deriving via Autodocodec Expression instance FromJSON Expression
|
|
|
|
|
|
|
|
deriving via Autodocodec Expression instance ToJSON Expression
|
|
|
|
|
|
|
|
deriving via Autodocodec Expression instance ToSchema Expression
|
2022-06-02 05:06:45 +03:00
|
|
|
|
|
|
|
instance HasCodec ComparisonValue where
|
|
|
|
codec =
|
|
|
|
named "ComparisonValue" $
|
|
|
|
sumTypeCodec
|
|
|
|
[ TypeAlternative "AnotherColumnComparison" "column" _AnotherColumn,
|
|
|
|
TypeAlternative "ScalarValueComparison" "scalar" _ScalarValue
|
|
|
|
]
|
|
|
|
|
|
|
|
deriving via Autodocodec ComparisonValue instance FromJSON ComparisonValue
|
|
|
|
|
|
|
|
deriving via Autodocodec ComparisonValue instance ToJSON ComparisonValue
|
|
|
|
|
|
|
|
deriving via Autodocodec ComparisonValue instance ToSchema ComparisonValue
|